brewery
notashelf /
b36d490de55646507f200ee2e3a71164c79c4c67

chroma

public

Lightweight wallpaper daemon for Wayland

Star0tarball
clone
ssh://git@git.notashelf.dev:33/notashelf/chroma.git
include/chroma.hC318 lines11.3 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 <GLES2/gl2.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.toml"
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
  int ref_count; // Number of outputs using this image
77
} chroma_image_t;
78
79
// Wayland output information
80
typedef struct {
81
  struct wl_output *wl_output;
82
  uint32_t id;
83
  int32_t x, y;
84
  int32_t width, height;
85
  int32_t scale;
86
  enum wl_output_transform transform;
87
  char *name;
88
  char *description;
89
  bool active;
90
91
  // Back reference to state
92
  struct chroma_state *state;
93
94
  // Rendering context
95
  struct wl_surface *surface;
96
  struct zwlr_layer_surface_v1 *layer_surface;
97
  struct wl_egl_window *egl_window;
98
  EGLSurface egl_surface;
99
  uint32_t configure_serial;
100
101
  // Associated wallpaper
102
  chroma_image_t *image;
103
104
  // Configuration for this output
105
  chroma_scale_mode_t scale_mode;
106
  chroma_filter_quality_t filter_quality;
107
  chroma_anchor_t anchor;
108
  float anchor_x; // custom X offset (0-100, 50=center)
109
  float anchor_y; // custom Y offset (0-100, 50=center)
110
  bool config_loaded;
111
112
  // OpenGL resource cache
113
  GLuint texture_id;
114
  GLuint shader_program;
115
  GLuint vbo;
116
  GLuint ebo;
117
  bool gl_resources_initialized;
118
  bool texture_uploaded;
119
  bool vbo_dirty; // track VBO needs update
120
  bool configured; // track if initial configure received
121
} chroma_output_t;
122
123
// Config mapping structure
124
typedef struct {
125
  char output_name[256];
126
  char image_path[MAX_PATH_LEN];
127
  chroma_scale_mode_t scale_mode;
128
  chroma_filter_quality_t filter_quality;
129
  chroma_anchor_t anchor;
130
  float anchor_x; // custom X offset (0-100, 50=center, 0=left, 100=right)
131
  float anchor_y; // custom Y offset (0-100, 50=center, 0=top, 100=bottom)
132
} chroma_config_mapping_t;
133
134
// Application configuration
135
typedef struct {
136
  chroma_config_mapping_t mappings[MAX_OUTPUTS];
137
  int mapping_count;
138
  char default_image[MAX_PATH_LEN];
139
  bool daemon_mode;
140
141
  // Global scaling and filtering settings (used as defaults)
142
  chroma_scale_mode_t default_scale_mode;
143
  chroma_filter_quality_t default_filter_quality;
144
  chroma_anchor_t default_anchor;
145
  float default_anchor_x; // custom anchor X offset (0-100, 50=center)
146
  float default_anchor_y; // custom anchor Y offset (0-100, 50=center)
147
148
  // Image downsampling settings
149
  bool enable_downsampling; // enable automatic downsampling
150
  int max_output_width;     // maximum expected output width
151
  int max_output_height;    // maximum expected output height
152
  float min_scale_factor;   // minimum scale factor (don't scale below this)
153
} chroma_config_t;
154
155
// Main application state
156
typedef struct chroma_state {
157
  // Wayland globals
158
  struct wl_display *display;
159
  struct wl_registry *registry;
160
  struct wl_compositor *compositor;
161
  struct zwlr_layer_shell_v1 *layer_shell;
162
163
  // EGL context
164
  EGLDisplay egl_display;
165
  EGLContext egl_context;
166
  EGLConfig egl_config;
167
  
168
  // Shared OpenGL resources
169
  GLuint shader_program;
170
171
  // Outputs
172
  chroma_output_t outputs[MAX_OUTPUTS];
173
  int output_count;
174
175
  // Images
176
  chroma_image_t images[MAX_OUTPUTS];
177
  int image_count;
178
179
  // Configuration
180
  chroma_config_t config;
181
182
  // State flags
183
  bool running;
184
  bool initialized;
185
} chroma_state_t;
186
187
// Function declarations
188
189
// Initialization and cleanup
190
int chroma_init(chroma_state_t *state);
191
void chroma_cleanup(chroma_state_t *state);
192
193
// Wayland management
194
int chroma_wayland_connect(chroma_state_t *state);
195
void chroma_wayland_disconnect(chroma_state_t *state);
196
void chroma_registry_listener(void *data, struct wl_registry *registry,
197
                              uint32_t id, const char *interface,
198
                              uint32_t version);
199
void chroma_registry_remove(void *data, struct wl_registry *registry,
200
                            uint32_t id);
201
202
// Output management
203
int chroma_output_add(chroma_state_t *state, uint32_t id,
204
                      struct wl_output *output);
205
void chroma_output_remove(chroma_state_t *state, uint32_t id);
206
chroma_output_t *chroma_output_find_by_id(chroma_state_t *state, uint32_t id);
207
chroma_output_t *chroma_output_find_by_name(chroma_state_t *state,
208
                                            const char *name);
209
210
// Output event handlers
211
void chroma_output_geometry(void *data, struct wl_output *output, int32_t x,
212
                            int32_t y, int32_t physical_width,
213
                            int32_t physical_height, int32_t subpixel,
214
                            const char *make, const char *model,
215
                            int32_t transform);
216
void chroma_output_mode(void *data, struct wl_output *output, uint32_t flags,
217
                        int32_t width, int32_t height, int32_t refresh);
218
void chroma_output_scale(void *data, struct wl_output *output, int32_t scale);
219
void chroma_output_name(void *data, struct wl_output *output, const char *name);
220
void chroma_output_description(void *data, struct wl_output *output,
221
                               const char *description);
222
void chroma_output_done(void *data, struct wl_output *output);
223
224
// EGL and rendering
225
int chroma_egl_init(chroma_state_t *state);
226
void chroma_egl_cleanup(chroma_state_t *state);
227
int chroma_surface_create(chroma_state_t *state, chroma_output_t *output);
228
void chroma_surface_destroy(chroma_output_t *output);
229
int chroma_render_wallpaper(chroma_state_t *state, chroma_output_t *output);
230
void chroma_output_invalidate_texture(chroma_output_t *output);
231
232
// Layer shell functions
233
void chroma_layer_surface_configure(void *data,
234
                                    struct zwlr_layer_surface_v1 *layer_surface,
235
                                    uint32_t serial, uint32_t width,
236
                                    uint32_t height);
237
void chroma_layer_surface_closed(void *data,
238
                                 struct zwlr_layer_surface_v1 *layer_surface);
239
240
// Image loading
241
void chroma_image_init_stb(void);
242
int chroma_image_load(chroma_image_t *image, const char *path,
243
                      const chroma_config_t *config, int output_width,
244
                      int output_height);
245
void chroma_image_free(chroma_image_t *image);
246
void chroma_image_release(chroma_image_t *image);
247
chroma_image_t *chroma_image_find_by_path(chroma_state_t *state,
248
                                          const char *path);
249
chroma_image_t *chroma_image_get_or_load(chroma_state_t *state,
250
                                         const char *path, int output_width,
251
                                         int output_height);
252
int chroma_image_validate(const char *path);
253
int chroma_image_get_info(const char *path, int *width, int *height,
254
                          int *channels);
255
void chroma_images_cleanup(chroma_state_t *state);
256
257
// Configuration
258
int chroma_config_load(chroma_config_t *config, const char *config_file);
259
int chroma_config_load_toml(chroma_config_t *config, const char *config_file);
260
void chroma_config_free(chroma_config_t *config);
261
const char *chroma_config_get_image_for_output(chroma_config_t *config,
262
                                               const char *output_name,
263
                                               const char *output_description);
264
int chroma_config_get_mapping_for_output(
265
    chroma_config_t *config, const char *output_name,
266
    const char *output_description, chroma_scale_mode_t *scale_mode,
267
    chroma_filter_quality_t *filter_quality, chroma_anchor_t *anchor,
268
    float *anchor_x, float *anchor_y);
269
270
void chroma_config_print(const chroma_config_t *config);
271
272
// Main loop and events
273
int chroma_run(chroma_state_t *state);
274
void chroma_handle_signals(void);
275
int chroma_reload_config(chroma_state_t *state, const char *config_file);
276
int chroma_update_outputs(chroma_state_t *state);
277
void chroma_get_stats(chroma_state_t *state, int *active_outputs,
278
                      int *loaded_images);
279
void handle_output_done(chroma_state_t *state, chroma_output_t *output);
280
281
// Utilities
282
void chroma_log(const char *level, const char *format, ...);
283
const char *chroma_error_string(chroma_error_t error);
284
void chroma_set_log_level(int level);
285
int chroma_get_log_level(void);
286
void chroma_set_signal_state(chroma_state_t *state, const char *config_file);
287
void chroma_cleanup_signals(void);
288
char *chroma_expand_path(const char *path);
289
int chroma_mkdir_recursive(const char *path, mode_t mode);
290
char *chroma_get_config_dir(void);
291
bool chroma_path_exists(const char *path);
292
bool chroma_is_regular_file(const char *path);
293
bool chroma_is_directory(const char *path);
294
long chroma_get_file_size(const char *path);
295
const char *chroma_get_file_extension(const char *path);
296
long long chroma_get_time_ms(void);
297
void chroma_sleep_ms(long ms);
298
void chroma_format_memory_size(size_t bytes, char *buffer, size_t buffer_size);
299
void chroma_utils_cleanup(void);
300
301
// Memory tracking and logging
302
void chroma_log_memory_stats(const char *context);
303
size_t chroma_get_memory_usage(void);
304
void chroma_log_resource_allocation(const char *resource_type, size_t size,
305
                                    const char *description);
306
void chroma_log_resource_deallocation(const char *resource_type, size_t size,
307
                                      const char *description);
308
309
// Listener structures
310
extern const struct wl_registry_listener chroma_registry_listener_impl;
311
extern const struct wl_output_listener chroma_output_listener_impl;
312
extern const struct zwlr_layer_surface_v1_listener
313
    chroma_layer_surface_listener_impl;
314
315
// Global state for signal handling
316
extern volatile sig_atomic_t chroma_should_quit;
317
318
#endif // CHROMA_H