commit a3764b4ccd67c4e0625e42917f0cbf159a51b746
parent d51d7cd19fad8a5b3f6aa33f224dfafc3c30c2aa
Author: Luke Willis <lukejw@loquat.dev>
Date:   Sat, 28 Mar 2026 23:23:28 -0400

Add .clang-format and format code

Diffstat:
A.clang-format | 3+++
Msrc/main.c | 358+++++++++++++++++++++++++++++++++++++------------------------------------------
Msrc/wl.c | 70+++++++++++++++++++++++++++++++++-------------------------------------
3 files changed, 203 insertions(+), 228 deletions(-)

diff --git a/.clang-format b/.clang-format @@ -0,0 +1,3 @@ +--- +BasedOnStyle: Google +... diff --git a/src/main.c b/src/main.c @@ -1,114 +1,103 @@ -#include <stdio.h> +#include <libguile.h> #include <stdint.h> +#include <stdio.h> #include <string.h> #include <sys/mman.h> #include <unistd.h> - #include <wayland-client.h> -#include <libguile.h> - -#include "wlr-layer-shell-unstable-v1-client-protocol.h" #include "wl.h" +#include "wlr-layer-shell-unstable-v1-client-protocol.h" struct wig_window { - char *label; - struct wl_surface *wl_surface; - struct zwlr_layer_surface_v1 *wlr_layer_surface; + char* label; + struct wl_surface* wl_surface; + struct zwlr_layer_surface_v1* wlr_layer_surface; - struct wl_shm *shm; + struct wl_shm* shm; }; struct wig_state { - struct wl_display *display; - struct wl_registry *registry; + struct wl_display* display; + struct wl_registry* registry; - struct wl_compositor *compositor; - struct wl_shm *shm; - struct zwlr_layer_shell_v1 *layer_shell; + struct wl_compositor* compositor; + struct wl_shm* shm; + struct zwlr_layer_shell_v1* layer_shell; - struct wig_window *windows; + struct wig_window* windows; }; /* * Buffer */ -static void wl_buffer_release(void *data, struct wl_buffer *wl_buffer) -{ - wl_buffer_destroy(wl_buffer); +static void wl_buffer_release(void* data, struct wl_buffer* wl_buffer) { + wl_buffer_destroy(wl_buffer); } static const struct wl_buffer_listener wl_buffer_listener = { .release = wl_buffer_release, }; -static struct wl_buffer *draw_frame(struct wig_window *state, int width, - int height) -{ - int stride = width * 4; - int size = stride * height; - - int fd = allocate_shm_file(size); - if (fd == -1) { - return NULL; - } +static struct wl_buffer* draw_frame(struct wig_window* state, int width, + int height) { + int stride = width * 4; + int size = stride * height; - uint32_t *data = mmap(NULL, size, - PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - if (data == MAP_FAILED) { - close(fd); - return NULL; - } + int fd = allocate_shm_file(size); + if (fd == -1) { + return NULL; + } - struct wl_shm_pool *pool = wl_shm_create_pool(state->shm, fd, size); - struct wl_buffer *buffer = wl_shm_pool_create_buffer(pool, 0, - width, height, - stride, - WL_SHM_FORMAT_XRGB8888); - wl_shm_pool_destroy(pool); + uint32_t* data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (data == MAP_FAILED) { close(fd); - - /* Draw checkerboxed background */ - for (int y = 0; y < height; ++y) { - for (int x = 0; x < width; ++x) { - if ((x + y / 8 * 8) % 16 < 8) - data[y * width + x] = 0xFF666666; - else - data[y * width + x] = 0xFFEEEEEE; - } + return NULL; + } + + struct wl_shm_pool* pool = wl_shm_create_pool(state->shm, fd, size); + struct wl_buffer* buffer = wl_shm_pool_create_buffer( + pool, 0, width, height, stride, WL_SHM_FORMAT_XRGB8888); + wl_shm_pool_destroy(pool); + close(fd); + + /* Draw checkerboxed background */ + for (int y = 0; y < height; ++y) { + for (int x = 0; x < width; ++x) { + if ((x + y / 8 * 8) % 16 < 8) + data[y * width + x] = 0xFF666666; + else + data[y * width + x] = 0xFFEEEEEE; } + } - munmap(data, size); - wl_buffer_add_listener(buffer, &wl_buffer_listener, NULL); - return buffer; + munmap(data, size); + wl_buffer_add_listener(buffer, &wl_buffer_listener, NULL); + return buffer; } /* * Surface */ -static void zwlr_layer_surface_configure(void *data, struct zwlr_layer_surface_v1 - *zwlr_layer_surface_v1, - uint32_t serial, uint32_t width, - uint32_t height) -{ - struct wig_window *window = data; - zwlr_layer_surface_v1_ack_configure(zwlr_layer_surface_v1, serial); - - struct wl_buffer *buffer = draw_frame(window, width, height); - wl_surface_attach(window->wl_surface, buffer, 0, 0); - wl_surface_commit(window->wl_surface); +static void zwlr_layer_surface_configure( + void* data, struct zwlr_layer_surface_v1* zwlr_layer_surface_v1, + uint32_t serial, uint32_t width, uint32_t height) { + struct wig_window* window = data; + zwlr_layer_surface_v1_ack_configure(zwlr_layer_surface_v1, serial); - fprintf(stderr, "\"%s\" reconfigured\n", window->label); - fprintf(stderr, " %u x %u\n", width, height); + struct wl_buffer* buffer = draw_frame(window, width, height); + wl_surface_attach(window->wl_surface, buffer, 0, 0); + wl_surface_commit(window->wl_surface); + fprintf(stderr, "\"%s\" reconfigured\n", window->label); + fprintf(stderr, " %u x %u\n", width, height); } -static void zwlr_layer_surface_closed(void *data, struct zwlr_layer_surface_v1 - *zwlr_layer_surface_v1) -{ - /* TODO */ +static void zwlr_layer_surface_closed( + void* data, struct zwlr_layer_surface_v1* zwlr_layer_surface_v1) { + /* TODO */ } static const struct zwlr_layer_surface_v1_listener layer_surface_listener = { @@ -120,144 +109,131 @@ static const struct zwlr_layer_surface_v1_listener layer_surface_listener = { * Registry */ -static void registry_handle_global(void *data, - struct wl_registry *registry, - uint32_t name, const char *interface, - uint32_t version) -{ - struct wig_state *state = data; - - if (strcmp(interface, wl_compositor_interface.name) == 0) { - state->compositor = - wl_registry_bind(registry, name, &wl_compositor_interface, 4); - } else if (strcmp(interface, wl_shm_interface.name) == 0) { - state->shm = - wl_registry_bind(registry, name, &wl_shm_interface, 1); - } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { - state->layer_shell = - wl_registry_bind(registry, name, - &zwlr_layer_shell_v1_interface, 2); - } +static void registry_handle_global(void* data, struct wl_registry* registry, + uint32_t name, const char* interface, + uint32_t version) { + struct wig_state* state = data; + + if (strcmp(interface, wl_compositor_interface.name) == 0) { + state->compositor = + wl_registry_bind(registry, name, &wl_compositor_interface, 4); + } else if (strcmp(interface, wl_shm_interface.name) == 0) { + state->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); + } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { + state->layer_shell = + wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface, 2); + } } -static void -registry_handle_global_remove(void *data, struct wl_registry *registry, - uint32_t name) -{ - /* TODO */ +static void registry_handle_global_remove(void* data, + struct wl_registry* registry, + uint32_t name) { + /* TODO */ } static const struct wl_registry_listener registry_listener = { -.global = registry_handle_global,.global_remove = - registry_handle_global_remove,}; + .global = registry_handle_global, + .global_remove = registry_handle_global_remove, +}; /* Main */ -static void inner_main(void *data, int argc, char **argv) -{ - struct wig_state *state = data; - - /* Evaluate configuration */ - SCM wig_configuration_type = - scm_c_private_ref("wig", "<configuration>"); - - SCM config = scm_c_primitive_load("wig.scm"); - - /* TODO: Catch errors properly */ - if (scm_is_false - (scm_eq_p(scm_struct_vtable(config), wig_configuration_type))) { - fprintf(stderr, "Invalid configuration!\n"); - return; +static void inner_main(void* data, int argc, char** argv) { + struct wig_state* state = data; + + /* Evaluate configuration */ + SCM wig_configuration_type = scm_c_private_ref("wig", "<configuration>"); + + SCM config = scm_c_primitive_load("wig.scm"); + + /* TODO: Catch errors properly */ + if (scm_is_false( + scm_eq_p(scm_struct_vtable(config), wig_configuration_type))) { + fprintf(stderr, "Invalid configuration!\n"); + return; + } + + SCM windows = scm_struct_ref(config, scm_from_int(0)); + size_t num_windows = scm_to_size_t(scm_length(windows)); + state->windows = malloc(num_windows * sizeof(struct wig_window)); + + for (size_t i = 0; i < num_windows; i++) { + SCM scm_window = scm_car(windows); + + char* label = + scm_to_utf8_stringn(scm_struct_ref(scm_window, scm_from_int(0)), NULL); + + SCM anchors = scm_struct_ref(scm_window, scm_from_int(1)); + uint32_t anchor = 0; + for (uint32_t i = 0; i < 4; i++) + if (scm_is_true(scm_struct_ref(anchors, scm_from_uint32(i)))) + anchor |= (1 << i); + + int exclusive = scm_is_true(scm_struct_ref(scm_window, scm_from_int(2))); + + uint32_t width = scm_to_uint32(scm_struct_ref(scm_window, scm_from_int(3))); + uint32_t height = + scm_to_uint32(scm_struct_ref(scm_window, scm_from_int(4))); + uint32_t margin = + scm_to_uint32(scm_struct_ref(scm_window, scm_from_int(5))); + + struct wig_window* window = &state->windows[i]; + window->label = label; + window->shm = state->shm; + + window->wl_surface = wl_compositor_create_surface(state->compositor); + window->wlr_layer_surface = zwlr_layer_shell_v1_get_layer_surface( + state->layer_shell, window->wl_surface, NULL, + ZWLR_LAYER_SHELL_V1_LAYER_TOP, label); + + zwlr_layer_surface_v1_add_listener(window->wlr_layer_surface, + &layer_surface_listener, window); + zwlr_layer_surface_v1_set_size(window->wlr_layer_surface, width, height); + zwlr_layer_surface_v1_set_anchor(window->wlr_layer_surface, anchor); + zwlr_layer_surface_v1_set_margin(window->wlr_layer_surface, margin, margin, + margin, margin); + + if (exclusive) { + if ((anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP) && + (anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM)) { + zwlr_layer_surface_v1_set_exclusive_zone(window->wlr_layer_surface, + width); + } else if ((anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT) && + (anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT)) { + zwlr_layer_surface_v1_set_exclusive_zone(window->wlr_layer_surface, + height); + } } - SCM windows = scm_struct_ref(config, scm_from_int(0)); - size_t num_windows = scm_to_size_t(scm_length(windows)); - state->windows = malloc(num_windows * sizeof(struct wig_window)); - - for (size_t i = 0; i < num_windows; i++) { - SCM scm_window = scm_car(windows); - - char *label = - scm_to_utf8_stringn(scm_struct_ref - (scm_window, scm_from_int(0)), NULL); - - SCM anchors = scm_struct_ref(scm_window, scm_from_int(1)); - uint32_t anchor = 0; - for (uint32_t i = 0; i < 4; i++) - if (scm_is_true(scm_struct_ref(anchors, scm_from_uint32(i)))) - anchor |= (1 << i); - - int exclusive = scm_is_true(scm_struct_ref(scm_window, scm_from_int(2))); - - uint32_t width = - scm_to_uint32(scm_struct_ref(scm_window, scm_from_int(3))); - uint32_t height = - scm_to_uint32(scm_struct_ref(scm_window, scm_from_int(4))); - uint32_t margin = - scm_to_uint32(scm_struct_ref(scm_window, scm_from_int(5))); - - struct wig_window *window = &state->windows[i]; - window->label = label; - window->shm = state->shm; - - window->wl_surface = - wl_compositor_create_surface(state->compositor); - window->wlr_layer_surface = - zwlr_layer_shell_v1_get_layer_surface(state->layer_shell, - window->wl_surface, NULL, - ZWLR_LAYER_SHELL_V1_LAYER_TOP, - label); - - zwlr_layer_surface_v1_add_listener(window->wlr_layer_surface, - &layer_surface_listener, - window); - zwlr_layer_surface_v1_set_size(window->wlr_layer_surface, - width, height); - zwlr_layer_surface_v1_set_anchor(window->wlr_layer_surface, anchor); - zwlr_layer_surface_v1_set_margin(window->wlr_layer_surface, margin, margin, margin, margin); - - if (exclusive) { - if ((anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP) && - (anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM)) { - zwlr_layer_surface_v1_set_exclusive_zone(window->wlr_layer_surface, - width); - } else if ((anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT) && - (anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT)) { - zwlr_layer_surface_v1_set_exclusive_zone(window->wlr_layer_surface, - height); - } - } - - wl_surface_commit(window->wl_surface); - - windows = scm_cdr(windows); - } + wl_surface_commit(window->wl_surface); - /* Loop */ - while (wl_display_dispatch(state->display) != -1) { - /* TODO */ - } + windows = scm_cdr(windows); + } - /* Cleanup */ - wl_display_disconnect(state->display); + /* Loop */ + while (wl_display_dispatch(state->display) != -1) { + /* TODO */ + } + + /* Cleanup */ + wl_display_disconnect(state->display); } -int main(int argc, char *argv[]) -{ - struct wig_state state = { NULL }; +int main(int argc, char* argv[]) { + struct wig_state state = {NULL}; - state.display = wl_display_connect(NULL); - if (!state.display) { - fprintf(stderr, "Failed to connect to Wayland display.\n"); - return 1; - } + state.display = wl_display_connect(NULL); + if (!state.display) { + fprintf(stderr, "Failed to connect to Wayland display.\n"); + return 1; + } - state.registry = wl_display_get_registry(state.display); - wl_registry_add_listener(state.registry, &registry_listener, &state); - wl_display_roundtrip(state.display); + state.registry = wl_display_get_registry(state.display); + wl_registry_add_listener(state.registry, &registry_listener, &state); + wl_display_roundtrip(state.display); - /* TODO: Validate registry */ + /* TODO: Validate registry */ - scm_boot_guile(argc, argv, inner_main, &state); - return 0; + scm_boot_guile(argc, argv, inner_main, &state); + return 0; } diff --git a/src/wl.c b/src/wl.c @@ -4,45 +4,41 @@ #include <time.h> #include <unistd.h> -static void randname(char *buf) -{ - struct timespec ts; - clock_gettime(CLOCK_REALTIME, &ts); - long r = ts.tv_nsec; - for (int i = 0; i < 6; ++i) { - buf[i] = 'A' + (r & 15) + (r & 16) * 2; - r >>= 5; - } +static void randname(char* buf) { + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + long r = ts.tv_nsec; + for (int i = 0; i < 6; ++i) { + buf[i] = 'A' + (r & 15) + (r & 16) * 2; + r >>= 5; + } } -static int create_shm_file(void) -{ - int retries = 100; - do { - char name[] = "/wl_shm-XXXXXX"; - randname(name + sizeof(name) - 7); - --retries; - int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600); - if (fd >= 0) { - shm_unlink(name); - return fd; - } - } while (retries > 0 && errno == EEXIST); - return -1; +static int create_shm_file(void) { + int retries = 100; + do { + char name[] = "/wl_shm-XXXXXX"; + randname(name + sizeof(name) - 7); + --retries; + int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600); + if (fd >= 0) { + shm_unlink(name); + return fd; + } + } while (retries > 0 && errno == EEXIST); + return -1; } -int allocate_shm_file(size_t size) -{ - int fd = create_shm_file(); - if (fd < 0) - return -1; - int ret; - do { - ret = ftruncate(fd, size); - } while (ret < 0 && errno == EINTR); - if (ret < 0) { - close(fd); - return -1; - } - return fd; +int allocate_shm_file(size_t size) { + int fd = create_shm_file(); + if (fd < 0) return -1; + int ret; + do { + ret = ftruncate(fd, size); + } while (ret < 0 && errno == EINTR); + if (ret < 0) { + close(fd); + return -1; + } + return fd; }