brewery
notashelf /
1f9050b69ec8435d340c92ea91db9a17d1bb7e2e

chroma

public

Lightweight wallpaper daemon for Wayland

Star0tarball
clone
ssh://git@git.notashelf.dev:33/notashelf/chroma.git
include/chroma.hC279 lines9.4 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
} chroma_output_t;
103
104
// Config mapping structure
105
typedef struct {
106
  char output_name[256];
107
  char image_path[MAX_PATH_LEN];
108
  chroma_scale_mode_t scale_mode;
109
  chroma_filter_quality_t filter_quality;
110
} chroma_config_mapping_t;
111
112
// Application configuration
113
typedef struct {
114
  chroma_config_mapping_t mappings[MAX_OUTPUTS];
115
  int mapping_count;
116
  char default_image[MAX_PATH_LEN];
117
  bool daemon_mode;
118
119
  // Global scaling and filtering settings (used as defaults)
120
  chroma_scale_mode_t default_scale_mode;
121
  chroma_filter_quality_t default_filter_quality;
122
} chroma_config_t;
123
124
// Main application state
125
typedef struct chroma_state {
126
  // Wayland globals
127
  struct wl_display *display;
128
  struct wl_registry *registry;
129
  struct wl_compositor *compositor;
130
  struct zwlr_layer_shell_v1 *layer_shell;
131
132
  // EGL context
133
  EGLDisplay egl_display;
134
  EGLContext egl_context;
135
  EGLConfig egl_config;
136
137
  // Outputs
138
  chroma_output_t outputs[MAX_OUTPUTS];
139
  int output_count;
140
141
  // Images
142
  chroma_image_t images[MAX_OUTPUTS];
143
  int image_count;
144
145
  // Configuration
146
  chroma_config_t config;
147
148
  // State flags
149
  bool running;
150
  bool initialized;
151
} chroma_state_t;
152
153
// Function declarations
154
155
// Initialization and cleanup
156
int chroma_init(chroma_state_t *state);
157
void chroma_cleanup(chroma_state_t *state);
158
159
// Wayland management
160
int chroma_wayland_connect(chroma_state_t *state);
161
void chroma_wayland_disconnect(chroma_state_t *state);
162
void chroma_registry_listener(void *data, struct wl_registry *registry,
163
                              uint32_t id, const char *interface,
164
                              uint32_t version);
165
void chroma_registry_remove(void *data, struct wl_registry *registry,
166
                            uint32_t id);
167
168
// Output management
169
int chroma_output_add(chroma_state_t *state, uint32_t id,
170
                      struct wl_output *output);
171
void chroma_output_remove(chroma_state_t *state, uint32_t id);
172
chroma_output_t *chroma_output_find_by_id(chroma_state_t *state, uint32_t id);
173
chroma_output_t *chroma_output_find_by_name(chroma_state_t *state,
174
                                            const char *name);
175
176
// Output event handlers
177
void chroma_output_geometry(void *data, struct wl_output *output, int32_t x,
178
                            int32_t y, int32_t physical_width,
179
                            int32_t physical_height, int32_t subpixel,
180
                            const char *make, const char *model,
181
                            int32_t transform);
182
void chroma_output_mode(void *data, struct wl_output *output, uint32_t flags,
183
                        int32_t width, int32_t height, int32_t refresh);
184
void chroma_output_scale(void *data, struct wl_output *output, int32_t scale);
185
void chroma_output_name(void *data, struct wl_output *output, const char *name);
186
void chroma_output_description(void *data, struct wl_output *output,
187
                               const char *description);
188
void chroma_output_done(void *data, struct wl_output *output);
189
190
// EGL and rendering
191
int chroma_egl_init(chroma_state_t *state);
192
void chroma_egl_cleanup(chroma_state_t *state);
193
int chroma_surface_create(chroma_state_t *state, chroma_output_t *output);
194
void chroma_surface_destroy(chroma_output_t *output);
195
int chroma_render_wallpaper(chroma_state_t *state, chroma_output_t *output);
196
void chroma_output_invalidate_texture(chroma_output_t *output);
197
198
// Layer shell functions
199
void chroma_layer_surface_configure(void *data,
200
                                    struct zwlr_layer_surface_v1 *layer_surface,
201
                                    uint32_t serial, uint32_t width,
202
                                    uint32_t height);
203
void chroma_layer_surface_closed(void *data,
204
                                 struct zwlr_layer_surface_v1 *layer_surface);
205
206
// Image loading
207
void chroma_image_init_stb(void);
208
int chroma_image_load(chroma_image_t *image, const char *path);
209
void chroma_image_free(chroma_image_t *image);
210
chroma_image_t *chroma_image_find_by_path(chroma_state_t *state,
211
                                          const char *path);
212
chroma_image_t *chroma_image_get_or_load(chroma_state_t *state,
213
                                         const char *path);
214
int chroma_image_validate(const char *path);
215
int chroma_image_get_info(const char *path, int *width, int *height,
216
                          int *channels);
217
void chroma_images_cleanup(chroma_state_t *state);
218
219
// Configuration
220
int chroma_config_load(chroma_config_t *config, const char *config_file);
221
void chroma_config_free(chroma_config_t *config);
222
const char *chroma_config_get_image_for_output(chroma_config_t *config,
223
                                               const char *output_name);
224
int chroma_config_get_mapping_for_output(
225
    chroma_config_t *config, const char *output_name,
226
    chroma_scale_mode_t *scale_mode, chroma_filter_quality_t *filter_quality);
227
int chroma_config_create_sample(const char *config_file);
228
void chroma_config_print(const chroma_config_t *config);
229
230
// Main loop and events
231
int chroma_run(chroma_state_t *state);
232
void chroma_handle_signals(void);
233
int chroma_reload_config(chroma_state_t *state, const char *config_file);
234
int chroma_update_outputs(chroma_state_t *state);
235
void chroma_get_stats(chroma_state_t *state, int *active_outputs,
236
                      int *loaded_images);
237
void handle_output_done(chroma_state_t *state, chroma_output_t *output);
238
239
// Utilities
240
void chroma_log(const char *level, const char *format, ...);
241
const char *chroma_error_string(chroma_error_t error);
242
void chroma_set_log_level(int level);
243
int chroma_get_log_level(void);
244
void chroma_set_signal_state(chroma_state_t *state, const char *config_file);
245
void chroma_cleanup_signals(void);
246
char *chroma_expand_path(const char *path);
247
int chroma_mkdir_recursive(const char *path, mode_t mode);
248
char *chroma_get_config_dir(void);
249
bool chroma_path_exists(const char *path);
250
bool chroma_is_regular_file(const char *path);
251
bool chroma_is_directory(const char *path);
252
long chroma_get_file_size(const char *path);
253
const char *chroma_get_file_extension(const char *path);
254
int chroma_strcasecmp(const char *s1, const char *s2);
255
size_t chroma_strlcpy(char *dst, const char *src, size_t size);
256
size_t chroma_strlcat(char *dst, const char *src, size_t size);
257
long long chroma_get_time_ms(void);
258
void chroma_sleep_ms(long ms);
259
void chroma_format_memory_size(size_t bytes, char *buffer, size_t buffer_size);
260
void chroma_utils_cleanup(void);
261
262
// Memory tracking and logging
263
void chroma_log_memory_stats(const char *context);
264
size_t chroma_get_memory_usage(void);
265
void chroma_log_resource_allocation(const char *resource_type, size_t size,
266
                                    const char *description);
267
void chroma_log_resource_deallocation(const char *resource_type, size_t size,
268
                                      const char *description);
269
270
// Listener structures
271
extern const struct wl_registry_listener chroma_registry_listener_impl;
272
extern const struct wl_output_listener chroma_output_listener_impl;
273
extern const struct zwlr_layer_surface_v1_listener
274
    chroma_layer_surface_listener_impl;
275
276
// Global state for signal handling
277
extern volatile sig_atomic_t chroma_should_quit;
278
279
#endif // CHROMA_H