brewery
notashelf /
1a366d24451c9631348753d884da9d7636fa5996

chroma

public

Lightweight wallpaper daemon for Wayland

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