brewery
notashelf /
5ba4407367ee95b5861d396a7e175731fd6b47bc

chroma

public

Lightweight wallpaper daemon for Wayland

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