brewery
notashelf /
bc77b887ad02db102d2cbdfd48dc19ad6232143d

chroma

public

Lightweight wallpaper daemon for Wayland

Star0tarball
clone
ssh://git@git.notashelf.dev:33/notashelf/chroma.git
include/chroma.hC246 lines8.1 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
#define CHROMA_VERSION "1.0.0"
16
#define MAX_OUTPUTS 16
17
#define MAX_PATH_LEN 4096
18
#define CONFIG_FILE_NAME "chroma.conf"
19
20
// Log levels
21
#define CHROMA_LOG_ERROR 0
22
#define CHROMA_LOG_WARN 1
23
#define CHROMA_LOG_INFO 2
24
#define CHROMA_LOG_DEBUG 3
25
#define CHROMA_LOG_TRACE 4
26
27
// Error codes
28
typedef enum {
29
  CHROMA_OK = 0,
30
  CHROMA_ERROR_INIT = -1,
31
  CHROMA_ERROR_WAYLAND = -2,
32
  CHROMA_ERROR_EGL = -3,
33
  CHROMA_ERROR_IMAGE = -4,
34
  CHROMA_ERROR_CONFIG = -5,
35
  CHROMA_ERROR_MEMORY = -6
36
} chroma_error_t;
37
38
// Image data structure
39
typedef struct {
40
  unsigned char *data; // RGBA pixel data
41
  int width;
42
  int height;
43
  int channels;
44
  char path[MAX_PATH_LEN];
45
  bool loaded;
46
} chroma_image_t;
47
48
// Wayland output information
49
typedef struct {
50
  struct wl_output *wl_output;
51
  uint32_t id;
52
  int32_t x, y;
53
  int32_t width, height;
54
  int32_t scale;
55
  enum wl_output_transform transform;
56
  char *name;
57
  char *description;
58
  bool active;
59
60
  // Back reference to state
61
  struct chroma_state *state;
62
63
  // Rendering context
64
  struct wl_surface *surface;
65
  struct zwlr_layer_surface_v1 *layer_surface;
66
  struct wl_egl_window *egl_window;
67
  EGLSurface egl_surface;
68
  uint32_t configure_serial;
69
70
  // Associated wallpaper
71
  chroma_image_t *image;
72
73
  // OpenGL resource cache
74
  GLuint texture_id;
75
  GLuint shader_program;
76
  GLuint vbo;
77
  GLuint ebo;
78
  bool gl_resources_initialized;
79
  bool texture_uploaded;
80
} chroma_output_t;
81
82
// Config mapping structure
83
typedef struct {
84
  char output_name[256];
85
  char image_path[MAX_PATH_LEN];
86
} chroma_config_mapping_t;
87
88
// Application configuration
89
typedef struct {
90
  chroma_config_mapping_t mappings[MAX_OUTPUTS];
91
  int mapping_count;
92
  char default_image[MAX_PATH_LEN];
93
  bool daemon_mode;
94
} chroma_config_t;
95
96
// Main application state
97
typedef struct chroma_state {
98
  // Wayland globals
99
  struct wl_display *display;
100
  struct wl_registry *registry;
101
  struct wl_compositor *compositor;
102
  struct zwlr_layer_shell_v1 *layer_shell;
103
104
  // EGL context
105
  EGLDisplay egl_display;
106
  EGLContext egl_context;
107
  EGLConfig egl_config;
108
109
  // Outputs
110
  chroma_output_t outputs[MAX_OUTPUTS];
111
  int output_count;
112
113
  // Images
114
  chroma_image_t images[MAX_OUTPUTS];
115
  int image_count;
116
117
  // Configuration
118
  chroma_config_t config;
119
120
  // State flags
121
  bool running;
122
  bool initialized;
123
} chroma_state_t;
124
125
// Function declarations
126
127
// Initialization and cleanup
128
int chroma_init(chroma_state_t *state);
129
void chroma_cleanup(chroma_state_t *state);
130
131
// Wayland management
132
int chroma_wayland_connect(chroma_state_t *state);
133
void chroma_wayland_disconnect(chroma_state_t *state);
134
void chroma_registry_listener(void *data, struct wl_registry *registry,
135
                              uint32_t id, const char *interface,
136
                              uint32_t version);
137
void chroma_registry_remove(void *data, struct wl_registry *registry,
138
                            uint32_t id);
139
140
// Output management
141
int chroma_output_add(chroma_state_t *state, uint32_t id,
142
                      struct wl_output *output);
143
void chroma_output_remove(chroma_state_t *state, uint32_t id);
144
chroma_output_t *chroma_output_find_by_id(chroma_state_t *state, uint32_t id);
145
chroma_output_t *chroma_output_find_by_name(chroma_state_t *state,
146
                                            const char *name);
147
148
// Output event handlers
149
void chroma_output_geometry(void *data, struct wl_output *output, int32_t x,
150
                            int32_t y, int32_t physical_width,
151
                            int32_t physical_height, int32_t subpixel,
152
                            const char *make, const char *model,
153
                            int32_t transform);
154
void chroma_output_mode(void *data, struct wl_output *output, uint32_t flags,
155
                        int32_t width, int32_t height, int32_t refresh);
156
void chroma_output_scale(void *data, struct wl_output *output, int32_t scale);
157
void chroma_output_name(void *data, struct wl_output *output, const char *name);
158
void chroma_output_description(void *data, struct wl_output *output,
159
                               const char *description);
160
void chroma_output_done(void *data, struct wl_output *output);
161
162
// EGL and rendering
163
int chroma_egl_init(chroma_state_t *state);
164
void chroma_egl_cleanup(chroma_state_t *state);
165
int chroma_surface_create(chroma_state_t *state, chroma_output_t *output);
166
void chroma_surface_destroy(chroma_output_t *output);
167
int chroma_render_wallpaper(chroma_state_t *state, chroma_output_t *output);
168
void chroma_output_invalidate_texture(chroma_output_t *output);
169
170
// Layer shell functions
171
void chroma_layer_surface_configure(void *data,
172
                                    struct zwlr_layer_surface_v1 *layer_surface,
173
                                    uint32_t serial, uint32_t width,
174
                                    uint32_t height);
175
void chroma_layer_surface_closed(void *data,
176
                                 struct zwlr_layer_surface_v1 *layer_surface);
177
178
// Image loading
179
void chroma_image_init_stb(void);
180
int chroma_image_load(chroma_image_t *image, const char *path);
181
void chroma_image_free(chroma_image_t *image);
182
chroma_image_t *chroma_image_find_by_path(chroma_state_t *state,
183
                                          const char *path);
184
chroma_image_t *chroma_image_get_or_load(chroma_state_t *state,
185
                                         const char *path);
186
int chroma_image_validate(const char *path);
187
int chroma_image_get_info(const char *path, int *width, int *height,
188
                          int *channels);
189
void chroma_images_cleanup(chroma_state_t *state);
190
191
// Configuration
192
int chroma_config_load(chroma_config_t *config, const char *config_file);
193
void chroma_config_free(chroma_config_t *config);
194
const char *chroma_config_get_image_for_output(chroma_config_t *config,
195
                                               const char *output_name);
196
197
// Main loop and events
198
int chroma_run(chroma_state_t *state);
199
void chroma_handle_signals(void);
200
int chroma_reload_config(chroma_state_t *state, const char *config_file);
201
int chroma_update_outputs(chroma_state_t *state);
202
void chroma_get_stats(chroma_state_t *state, int *active_outputs,
203
                      int *loaded_images);
204
void handle_output_done(chroma_state_t *state, chroma_output_t *output);
205
206
// Utilities
207
void chroma_log(const char *level, const char *format, ...);
208
const char *chroma_error_string(chroma_error_t error);
209
void chroma_set_log_level(int level);
210
int chroma_get_log_level(void);
211
void chroma_set_signal_state(chroma_state_t *state, const char *config_file);
212
void chroma_cleanup_signals(void);
213
char *chroma_expand_path(const char *path);
214
int chroma_mkdir_recursive(const char *path, mode_t mode);
215
char *chroma_get_config_dir(void);
216
bool chroma_path_exists(const char *path);
217
bool chroma_is_regular_file(const char *path);
218
bool chroma_is_directory(const char *path);
219
long chroma_get_file_size(const char *path);
220
const char *chroma_get_file_extension(const char *path);
221
int chroma_strcasecmp(const char *s1, const char *s2);
222
size_t chroma_strlcpy(char *dst, const char *src, size_t size);
223
size_t chroma_strlcat(char *dst, const char *src, size_t size);
224
long long chroma_get_time_ms(void);
225
void chroma_sleep_ms(long ms);
226
void chroma_format_memory_size(size_t bytes, char *buffer, size_t buffer_size);
227
void chroma_utils_cleanup(void);
228
229
// Memory tracking and logging
230
void chroma_log_memory_stats(const char *context);
231
size_t chroma_get_memory_usage(void);
232
void chroma_log_resource_allocation(const char *resource_type, size_t size,
233
                                    const char *description);
234
void chroma_log_resource_deallocation(const char *resource_type, size_t size,
235
                                      const char *description);
236
237
// Listener structures
238
extern const struct wl_registry_listener chroma_registry_listener_impl;
239
extern const struct wl_output_listener chroma_output_listener_impl;
240
extern const struct zwlr_layer_surface_v1_listener
241
    chroma_layer_surface_listener_impl;
242
243
// Global state for signal handling
244
extern volatile sig_atomic_t chroma_should_quit;
245
246
#endif // CHROMA_H