logo

bytemedia

Home to byte-level sounds, images, videos, … git clone https://hacktivis.me/git/bytemedia.git
commit: 4be5f4d3b12af833ebe9c29fe84785d30881feaa
parent fc0d2fc7d05c1f61e822a250de497c099b2b8cd9
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sun,  5 Mar 2023 05:11:07 +0100

C/dev_draw: Add

Diffstat:

MC/Makefile7+++++--
AC/dev_draw.c295+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 300 insertions(+), 2 deletions(-)

diff --git a/C/Makefile b/C/Makefile @@ -5,7 +5,7 @@ LDFLAGS=-Wl,--gc-sections .SUFFIXES: .au -all: au8b_8kHz au8b_44.1kHz SDL_YUV_visualisation wayland kagome_kagome.au battery_low.au battery_back.au +all: au8b_8kHz au8b_44.1kHz SDL_YUV_visualisation wayland kagome_kagome.au battery_low.au battery_back.au dev_draw SDL_YUV_visualisation: SDL_YUV_visualisation.c $(CC) $(CFLAGS) SDL_YUV_visualisation.c -o SDL_YUV_visualisation `sdl2-config --cflags --libs` @@ -20,6 +20,9 @@ wayland: wayland.c xdg-shell-protocol.c $(CC) $(CFLAGS) -o $@ $< xdg-shell-protocol.c -lwayland-client -lrt $(LDFLAGS) strip --strip-all wayland +dev_draw: dev_draw.c xdg-shell-protocol.c Makefile + $(CC) $(CFLAGS) -o $@ $< xdg-shell-protocol.c -lwayland-client -lrt $(LDFLAGS) + .c.au: $(CC) $(CFLAGS) -o $* $< -lm ./$* > $@ @@ -27,4 +30,4 @@ wayland: wayland.c xdg-shell-protocol.c .PHONY: clean clean: - rm SDL_YUV_visualisation wayland + rm SDL_YUV_visualisation wayland dev_draw diff --git a/C/dev_draw.c b/C/dev_draw.c @@ -0,0 +1,295 @@ +#define _POSIX_C_SOURCE 200112L +#include "xdg-shell-client-protocol.h" + +#include <errno.h> +#include <fcntl.h> +#include <limits.h> +#include <stdbool.h> +#include <stdlib.h> /* exit(), EXIT_FAILURE */ +#include <stdio.h> /* fputs(), stderr */ +#include <string.h> +#include <sys/mman.h> +#include <time.h> +#include <unistd.h> +#include <wayland-client.h> + +/* Shared memory support code */ +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 +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; +} + +/* Wayland code */ +struct client_state +{ + /* Globals */ + struct wl_display *wl_display; + struct wl_registry *wl_registry; + struct wl_compositor *wl_compositor; + struct xdg_wm_base *xdg_wm_base; + struct wl_shm *wl_shm; + struct wl_buffer *wl_buffer; + uint32_t *shm_data; + /* Objects */ + struct wl_surface *wl_surface; + struct xdg_surface *xdg_surface; + struct xdg_toplevel *xdg_toplevel; + /* State */ + float offset; + uint32_t last_frame; + int32_t width, height, size, stride; + bool closed; +}; + +static void +draw_frame(struct client_state *state) +{ + int t = (int)state->offset; + /* Let the fun begin! */ + if(fread(state->shm_data, state->size, 1, stdin) == -1) { + state->closed = true; + } +} + +static void +allocate_wl_shm_buffer(struct client_state *state) +{ + struct wl_shm_pool *pool; + + int shm_fd = allocate_shm_file(state->size); + if(shm_fd == -1) + { + return; + } + + state->shm_data = mmap(NULL, state->size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); + if(state->shm_data == MAP_FAILED) + { + close(shm_fd); + return; + } + + pool = wl_shm_create_pool(state->wl_shm, shm_fd, state->size); + state->wl_buffer = wl_shm_pool_create_buffer( + pool, 0, state->width, state->height, state->stride, WL_SHM_FORMAT_XRGB8888); + wl_shm_pool_destroy(pool); + close(shm_fd); +} + +static void +xdg_toplevel_configure(void *data, + struct xdg_toplevel *xdg_toplevel, + int32_t width, + int32_t height, + struct wl_array *states) +{ + struct client_state *state = data; + if(width != 0 && height != 0) + { + munmap(state->shm_data, state->width * state->height * 4); + state->width = width; + state->height = height; + state->stride = width * 4; + state->size = state->stride * height; + allocate_wl_shm_buffer(state); + } +} + +static void +xdg_toplevel_close(void *data, struct xdg_toplevel *toplevel) +{ + struct client_state *state = data; + state->closed = true; +} + +static const struct xdg_toplevel_listener xdg_toplevel_listener = { + .configure = xdg_toplevel_configure, + .close = xdg_toplevel_close, +}; + +static void +xdg_surface_configure(void *data, struct xdg_surface *xdg_surface, uint32_t serial) +{ + struct client_state *state = data; + xdg_surface_ack_configure(xdg_surface, serial); + + draw_frame(state); + wl_surface_attach(state->wl_surface, state->wl_buffer, 0, 0); + wl_surface_commit(state->wl_surface); +} + +static const struct xdg_surface_listener xdg_surface_listener = { + .configure = xdg_surface_configure, +}; + +static void +xdg_wm_base_ping(void *data, struct xdg_wm_base *xdg_wm_base, uint32_t serial) +{ + xdg_wm_base_pong(xdg_wm_base, serial); +} + +static const struct xdg_wm_base_listener xdg_wm_base_listener = { + .ping = xdg_wm_base_ping, +}; + +static const struct wl_callback_listener wl_surface_frame_listener; +static void +wl_surface_frame_done(void *data, struct wl_callback *cb, uint32_t time) +{ + /* Destroy this callback */ + wl_callback_destroy(cb); + + /* Request another frame */ + struct client_state *state = data; + cb = wl_surface_frame(state->wl_surface); + wl_callback_add_listener(cb, &wl_surface_frame_listener, state); + + /* Update scroll amount at 24 pixels per second */ + if(state->last_frame != 0) + { + int elapsed = time - state->last_frame; + state->offset += elapsed / 1000.0 * 24; + } + + /* Submit a frame for this event */ + draw_frame(state); + wl_surface_attach(state->wl_surface, state->wl_buffer, 0, 0); + wl_surface_damage_buffer(state->wl_surface, 0, 0, INT32_MAX, INT32_MAX); + wl_surface_commit(state->wl_surface); + + state->last_frame = time; +} + +static const struct wl_callback_listener wl_surface_frame_listener = { + .done = wl_surface_frame_done, +}; + +static void +registry_global(void *data, + struct wl_registry *wl_registry, + + uint32_t name, + const char *interface, + uint32_t version) +{ + struct client_state *state = data; + if(strcmp(interface, wl_shm_interface.name) == 0) + { + state->wl_shm = wl_registry_bind(wl_registry, name, &wl_shm_interface, 1); + allocate_wl_shm_buffer(state); + } + else if(strcmp(interface, wl_compositor_interface.name) == 0) + { + state->wl_compositor = wl_registry_bind(wl_registry, name, &wl_compositor_interface, 4); + } + else if(strcmp(interface, xdg_wm_base_interface.name) == 0) + { + state->xdg_wm_base = wl_registry_bind(wl_registry, name, &xdg_wm_base_interface, 1); + xdg_wm_base_add_listener(state->xdg_wm_base, &xdg_wm_base_listener, state); + } +} + +static void +registry_global_remove(void *data, struct wl_registry *wl_registry, uint32_t name) +{ + /* This space deliberately left blank */ +} + +static const struct wl_registry_listener wl_registry_listener = { + .global = registry_global, + .global_remove = registry_global_remove, +}; + +static void +fatal(const char *message) { + fputs(message, stderr); + exit(EXIT_FAILURE); +} + +int +main(int argc, char *argv[]) +{ + struct client_state state = {0}; + state.width = 800; + state.height = 600; + state.closed = false; + state.stride = state.width * 4; + state.size = state.stride * state.height; + + state.wl_display = wl_display_connect(NULL); + if(!state.wl_display) fatal("Failed to connect to wayland display, exiting...\n"); + + state.wl_registry = wl_display_get_registry(state.wl_display); + wl_registry_add_listener(state.wl_registry, &wl_registry_listener, &state); + + wl_display_roundtrip(state.wl_display); + + if(!state.wl_shm) fatal("wl_shm protocol not found, exiting...\n"); + if(!state.wl_compositor) fatal("wl_compositor protocol not found, exiting...\n"); + if(!state.xdg_wm_base) fatal("xdg_wm_base protocol not found, exiting...\n"); + + state.wl_surface = wl_compositor_create_surface(state.wl_compositor); + state.xdg_surface = xdg_wm_base_get_xdg_surface(state.xdg_wm_base, state.wl_surface); + xdg_surface_add_listener(state.xdg_surface, &xdg_surface_listener, &state); + state.xdg_toplevel = xdg_surface_get_toplevel(state.xdg_surface); + xdg_toplevel_add_listener(state.xdg_toplevel, &xdg_toplevel_listener, &state); + xdg_toplevel_set_title(state.xdg_toplevel, "/dev/draw for wayland"); + wl_surface_commit(state.wl_surface); + + struct wl_callback *cb = wl_surface_frame(state.wl_surface); + wl_callback_add_listener(cb, &wl_surface_frame_listener, &state); + + while(wl_display_dispatch(state.wl_display) && !state.closed) + { + /* This space deliberately left blank */ + } + + munmap(state.shm_data, state.size); + + return 0; +}