brewery
notashelf /
000258df5c7d33095135ef826ae619307619d822

chroma

public

Lightweight wallpaper daemon for Wayland

Star0tarball
clone
ssh://git@git.notashelf.dev:33/notashelf/chroma.git
include/chroma.hC307 lines10.7 KB
1
#ifndef CHROMA_H
2
#define CHROMA_H
3
4
#include "wlr-layer-shell-unstable-v1.h"
5
#include "xdg-shell.h"
6
#include <EGL/egl.h>
7
#include <GL/gl.h>
8
#include <signal.h>
9
#include <stdbool.h>
10
#include <stdint.h>
11
#include <sys/types.h>
12
#include <wayland-client.h>
13
#include <wayland-egl.h>
14
15
#include "chroma_version.h"
16
17
#define MAX_OUTPUTS 16
18
#define MAX_PATH_LEN 4096
19
#define CONFIG_FILE_NAME "chroma.conf"
20
21
// Log levels
22
#define CHROMA_LOG_ERROR 0
23
#define CHROMA_LOG_WARN 1
24
#define CHROMA_LOG_INFO 2
25
#define CHROMA_LOG_DEBUG 3
26
#define CHROMA_LOG_TRACE 4
27
28
// Error codes
29
typedef enum {
30
  CHROMA_OK = 0,
31
  CHROMA_ERROR_INIT = -1,
32
  CHROMA_ERROR_WAYLAND = -2,
33
  CHROMA_ERROR_EGL = -3,
34
  CHROMA_ERROR_IMAGE = -4,
35
  CHROMA_ERROR_CONFIG = -5,
36
  CHROMA_ERROR_MEMORY = -6
37
} chroma_error_t;
38
39
// Scaling modes for wallpaper display
40
typedef enum {
41
  CHROMA_SCALE_FILL = 0,    // fill entire output, crop if necessary
42
  CHROMA_SCALE_FIT = 1,     // fit image within output, add borders if needed
43
  CHROMA_SCALE_STRETCH = 2, // stretch to fill output, may distort aspect ratio
44
  CHROMA_SCALE_CENTER = 3   // center image at original size
45
} chroma_scale_mode_t;
46
47
// Anchor positions for wallpaper placement
48
typedef enum {
49
  CHROMA_ANCHOR_CENTER = 0,      // center of the output (default)
50
  CHROMA_ANCHOR_TOP = 1,         // top edge, centered horizontally
51
  CHROMA_ANCHOR_BOTTOM = 2,      // bottom edge, centered horizontally
52
  CHROMA_ANCHOR_LEFT = 3,        // left edge, centered vertically
53
  CHROMA_ANCHOR_RIGHT = 4,       // right edge, centered vertically
54
  CHROMA_ANCHOR_TOP_LEFT = 5,    // top-left corner
55
  CHROMA_ANCHOR_TOP_RIGHT = 6,   // top-right corner
56
  CHROMA_ANCHOR_BOTTOM_LEFT = 7, // bottom-left corner
57
  CHROMA_ANCHOR_BOTTOM_RIGHT = 8 // bottom-right corner
58
} chroma_anchor_t;
59
60
// Image filtering quality settings
61
typedef enum {
62
  CHROMA_FILTER_NEAREST = 0,  // nearest neighbor filtering (pixelated)
63
  CHROMA_FILTER_LINEAR = 1,   // linear filtering (smooth)
64
  CHROMA_FILTER_BILINEAR = 2, // bilinear filtering (smoother)
65
  CHROMA_FILTER_TRILINEAR = 3 // trilinear filtering (smoothest)
66
} chroma_filter_quality_t;
67
68
// Image data structure
69
typedef struct {
70
  unsigned char *data; // RGBA pixel data
71
  int width;
72
  int height;
73
  int channels;
74
  char path[MAX_PATH_LEN];
75
  bool loaded;
76
} chroma_image_t;
77
78
// Wayland output information
79
typedef struct {
80
  struct wl_output *wl_output;
81
  uint32_t id;
82
  int32_t x, y;
83
  int32_t width, height;
84
  int32_t scale;
85
  enum wl_output_transform transform;
86
  char *name;
87
  char *description;
88
  bool active;
89
90
  // Back reference to state
91
  struct chroma_state *state;
92
93
  // Rendering context
94
  struct wl_surface *surface;
95
  struct zwlr_layer_surface_v1 *layer_surface;
96
  struct wl_egl_window *egl_window;
97
  EGLSurface egl_surface;
98
  uint32_t configure_serial;
99
100
  // Associated wallpaper
101
  chroma_image_t *image;
102
103
  // Configuration for this output
104
  chroma_scale_mode_t scale_mode;
105
  chroma_filter_quality_t filter_quality;
106
  chroma_anchor_t anchor;
107
  float anchor_x; // custom X offset (0-100, 50=center)
108
  float anchor_y; // custom Y offset (0-100, 50=center)
109
  bool config_loaded;
110
111
  // OpenGL resource cache
112
  GLuint texture_id;
113
  GLuint shader_program;
114
  GLuint vbo;
115
  GLuint ebo;
116
  bool gl_resources_initialized;
117
  bool texture_uploaded;
118
  bool vbo_dirty; // track VBO needs update
119
} chroma_output_t;
120
121
// Config mapping structure
122
typedef struct {
123
  char output_name[256];
124
  char image_path[MAX_PATH_LEN];
125
  chroma_scale_mode_t scale_mode;
126
  chroma_filter_quality_t filter_quality;
127
  chroma_anchor_t anchor;
128
  float anchor_x; // custom X offset (0-100, 50=center, 0=left, 100=right)
129
  float anchor_y; // custom Y offset (0-100, 50=center, 0=top, 100=bottom)
130
} chroma_config_mapping_t;
131
132
// Application configuration
133
typedef struct {
134
  chroma_config_mapping_t mappings[MAX_OUTPUTS];
135
  int mapping_count;
136
  char default_image[MAX_PATH_LEN];
137
  bool daemon_mode;
138
139
  // Global scaling and filtering settings (used as defaults)
140
  chroma_scale_mode_t default_scale_mode;
141
  chroma_filter_quality_t default_filter_quality;
142
  chroma_anchor_t default_anchor;
143
  float default_anchor_x; // custom anchor X offset (0-100, 50=center)
144
  float default_anchor_y; // custom anchor Y offset (0-100, 50=center)
145
146
  // Image downsampling settings
147
  bool enable_downsampling; // enable automatic downsampling
148
  int max_output_width;     // maximum expected output width
149
  int max_output_height;    // maximum expected output height
150
  float min_scale_factor;   // minimum scale factor (don't scale below this)
151
} chroma_config_t;
152
153
// Main application state
154
typedef struct chroma_state {
155
  // Wayland globals
156
  struct wl_display *display;
157
  struct wl_registry *registry;
158
  struct wl_compositor *compositor;
159
  struct zwlr_layer_shell_v1 *layer_shell;
160
161
  // EGL context
162
  EGLDisplay egl_display;
163
  EGLContext egl_context;
164
  EGLConfig egl_config;
165
166
  // Outputs
167
  chroma_output_t outputs[MAX_OUTPUTS];
168
  int output_count;
169
170
  // Images
171
  chroma_image_t images[MAX_OUTPUTS];
172
  int image_count;
173
174
  // Configuration
175
  chroma_config_t config;
176
177
  // State flags
178
  bool running;
179
  bool initialized;
180
} chroma_state_t;
181
182
// Function declarations
183
184
// Initialization and cleanup
185
int chroma_init(chroma_state_t *state);
186
void chroma_cleanup(chroma_state_t *state);
187
188
// Wayland management
189
int chroma_wayland_connect(chroma_state_t *state);
190
void chroma_wayland_disconnect(chroma_state_t *state);
191
void chroma_registry_listener(void *data, struct wl_registry *registry,
192
                              uint32_t id, const char *interface,
193
                              uint32_t version);
194
void chroma_registry_remove(void *data, struct wl_registry *registry,
195
                            uint32_t id);
196
197
// Output management
198
int chroma_output_add(chroma_state_t *state, uint32_t id,
199
                      struct wl_output *output);
200
void chroma_output_remove(chroma_state_t *state, uint32_t id);
201
chroma_output_t *chroma_output_find_by_id(chroma_state_t *state, uint32_t id);
202
chroma_output_t *chroma_output_find_by_name(chroma_state_t *state,
203
                                            const char *name);
204
205
// Output event handlers
206
void chroma_output_geometry(void *data, struct wl_output *output, int32_t x,
207
                            int32_t y, int32_t physical_width,
208
                            int32_t physical_height, int32_t subpixel,
209
                            const char *make, const char *model,
210
                            int32_t transform);
211
void chroma_output_mode(void *data, struct wl_output *output, uint32_t flags,
212
                        int32_t width, int32_t height, int32_t refresh);
213
void chroma_output_scale(void *data, struct wl_output *output, int32_t scale);
214
void chroma_output_name(void *data, struct wl_output *output, const char *name);
215
void chroma_output_description(void *data, struct wl_output *output,
216
                               const char *description);
217
void chroma_output_done(void *data, struct wl_output *output);
218
219
// EGL and rendering
220
int chroma_egl_init(chroma_state_t *state);
221
void chroma_egl_cleanup(chroma_state_t *state);
222
int chroma_surface_create(chroma_state_t *state, chroma_output_t *output);
223
void chroma_surface_destroy(chroma_output_t *output);
224
int chroma_render_wallpaper(chroma_state_t *state, chroma_output_t *output);
225
void chroma_output_invalidate_texture(chroma_output_t *output);
226
227
// Layer shell functions
228
void chroma_layer_surface_configure(void *data,
229
                                    struct zwlr_layer_surface_v1 *layer_surface,
230
                                    uint32_t serial, uint32_t width,
231
                                    uint32_t height);
232
void chroma_layer_surface_closed(void *data,
233
                                 struct zwlr_layer_surface_v1 *layer_surface);
234
235
// Image loading
236
void chroma_image_init_stb(void);
237
int chroma_image_load(chroma_image_t *image, const char *path,
238
                      const chroma_config_t *config);
239
void chroma_image_free(chroma_image_t *image);
240
chroma_image_t *chroma_image_find_by_path(chroma_state_t *state,
241
                                          const char *path);
242
chroma_image_t *chroma_image_get_or_load(chroma_state_t *state,
243
                                         const char *path);
244
int chroma_image_validate(const char *path);
245
int chroma_image_get_info(const char *path, int *width, int *height,
246
                          int *channels);
247
void chroma_images_cleanup(chroma_state_t *state);
248
249
// Configuration
250
int chroma_config_load(chroma_config_t *config, const char *config_file);
251
void chroma_config_free(chroma_config_t *config);
252
const char *chroma_config_get_image_for_output(chroma_config_t *config,
253
                                               const char *output_name);
254
int chroma_config_get_mapping_for_output(
255
    chroma_config_t *config, const char *output_name,
256
    chroma_scale_mode_t *scale_mode, chroma_filter_quality_t *filter_quality,
257
    chroma_anchor_t *anchor, float *anchor_x, float *anchor_y);
258
259
void chroma_config_print(const chroma_config_t *config);
260
261
// Main loop and events
262
int chroma_run(chroma_state_t *state);
263
void chroma_handle_signals(void);
264
int chroma_reload_config(chroma_state_t *state, const char *config_file);
265
int chroma_update_outputs(chroma_state_t *state);
266
void chroma_get_stats(chroma_state_t *state, int *active_outputs,
267
                      int *loaded_images);
268
void handle_output_done(chroma_state_t *state, chroma_output_t *output);
269
270
// Utilities
271
void chroma_log(const char *level, const char *format, ...);
272
const char *chroma_error_string(chroma_error_t error);
273
void chroma_set_log_level(int level);
274
int chroma_get_log_level(void);
275
void chroma_set_signal_state(chroma_state_t *state, const char *config_file);
276
void chroma_cleanup_signals(void);
277
char *chroma_expand_path(const char *path);
278
int chroma_mkdir_recursive(const char *path, mode_t mode);
279
char *chroma_get_config_dir(void);
280
bool chroma_path_exists(const char *path);
281
bool chroma_is_regular_file(const char *path);
282
bool chroma_is_directory(const char *path);
283
long chroma_get_file_size(const char *path);
284
const char *chroma_get_file_extension(const char *path);
285
long long chroma_get_time_ms(void);
286
void chroma_sleep_ms(long ms);
287
void chroma_format_memory_size(size_t bytes, char *buffer, size_t buffer_size);
288
void chroma_utils_cleanup(void);
289
290
// Memory tracking and logging
291
void chroma_log_memory_stats(const char *context);
292
size_t chroma_get_memory_usage(void);
293
void chroma_log_resource_allocation(const char *resource_type, size_t size,
294
                                    const char *description);
295
void chroma_log_resource_deallocation(const char *resource_type, size_t size,
296
                                      const char *description);
297
298
// Listener structures
299
extern const struct wl_registry_listener chroma_registry_listener_impl;
300
extern const struct wl_output_listener chroma_output_listener_impl;
301
extern const struct zwlr_layer_surface_v1_listener
302
    chroma_layer_surface_listener_impl;
303
304
// Global state for signal handling
305
extern volatile sig_atomic_t chroma_should_quit;
306
307
#endif // CHROMA_H