brewery
notashelf /
55012e16f94f4b1a9d421d57ec38ede6687ef192

chroma

public

Lightweight wallpaper daemon for Wayland

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