diff options
Diffstat (limited to 'src/wld/wayland-shm.c')
-rw-r--r-- | src/wld/wayland-shm.c | 336 |
1 files changed, 180 insertions, 156 deletions
diff --git a/src/wld/wayland-shm.c b/src/wld/wayland-shm.c index 4c58133..520eb2c 100644 --- a/src/wld/wayland-shm.c +++ b/src/wld/wayland-shm.c @@ -23,10 +23,10 @@ #define _GNU_SOURCE /* Required for mkostemp */ -#include "wld/pixman.h" -#include "wld/wayland-private.h" -#include "wld/wayland.h" -#include "wld/wld-private.h" +#include "wayland.h" +#include "wayland-private.h" +#include "wld-private.h" +#include "pixman.h" #include <fcntl.h> #include <stdlib.h> @@ -35,236 +35,260 @@ #include <unistd.h> #include <wayland-client.h> -struct shm_context { - struct wayland_context base; - struct wl_registry *registry; - struct wl_shm *wl; - struct wl_array formats; +struct shm_context +{ + struct wayland_context base; + struct wl_registry * registry; + struct wl_shm * wl; + struct wl_array formats; }; -struct shm_buffer { - struct buffer base; - int fd; +struct shm_buffer +{ + struct buffer base; + int fd; }; #define WAYLAND_IMPL_NAME shm -#include "interface/buffer.h" #include "interface/context.h" +#include "interface/buffer.h" #include "interface/wayland.h" IMPL(shm_context, wld_context) IMPL(shm_buffer, wld_buffer) -static void registry_global(void *data, struct wl_registry *registry, - uint32_t name, const char *interface, +static void registry_global(void * data, struct wl_registry * registry, + uint32_t name, const char * interface, uint32_t version); -static void registry_global_remove(void *data, struct wl_registry *registry, +static void registry_global_remove(void * data, struct wl_registry * registry, uint32_t name); -static void shm_format(void *data, struct wl_shm *wl, uint32_t format); +static void shm_format(void * data, struct wl_shm * wl, uint32_t format); const static struct wl_registry_listener registry_listener = { - .global = ®istry_global, .global_remove = ®istry_global_remove}; + .global = ®istry_global, + .global_remove = ®istry_global_remove +}; const static struct wl_shm_listener shm_listener = { .format = &shm_format, }; -static inline uint32_t format_wld_to_shm(uint32_t format) { - switch (format) { - case WLD_FORMAT_ARGB8888: - return WL_SHM_FORMAT_ARGB8888; - case WLD_FORMAT_XRGB8888: - return WL_SHM_FORMAT_XRGB8888; - default: - return 0; - } +static inline uint32_t format_wld_to_shm(uint32_t format) +{ + switch (format) + { + case WLD_FORMAT_ARGB8888: + return WL_SHM_FORMAT_ARGB8888; + case WLD_FORMAT_XRGB8888: + return WL_SHM_FORMAT_XRGB8888; + default: + return 0; + } } -struct wayland_context *wayland_create_context(struct wl_display *display, - struct wl_event_queue *queue) { - struct shm_context *context; +struct wayland_context * wayland_create_context(struct wl_display * display, + struct wl_event_queue * queue) +{ + struct shm_context * context; - if (!(context = malloc(sizeof *context))) - goto error0; + if (!(context = malloc(sizeof *context))) + goto error0; - context_initialize(&context->base.base, &wld_context_impl); - context->wl = NULL; - wl_array_init(&context->formats); + context_initialize(&context->base.base, &wld_context_impl); + context->wl = NULL; + wl_array_init(&context->formats); - if (!(context->registry = wl_display_get_registry(display))) { - DEBUG("Couldn't get registry\n"); - goto error1; - } + if (!(context->registry = wl_display_get_registry(display))) + { + DEBUG("Couldn't get registry\n"); + goto error1; + } - wl_registry_add_listener(context->registry, ®istry_listener, context); - wl_proxy_set_queue((struct wl_proxy *)context->registry, queue); + wl_registry_add_listener(context->registry, ®istry_listener, context); + wl_proxy_set_queue((struct wl_proxy *) context->registry, queue); - /* Wait for wl_shm global. */ - wl_display_roundtrip_queue(display, queue); + /* Wait for wl_shm global. */ + wl_display_roundtrip_queue(display, queue); - if (!context->wl) { - DEBUG("No wl_shm global\n"); - goto error2; - } + if (!context->wl) + { + DEBUG("No wl_shm global\n"); + goto error2; + } - wl_shm_add_listener(context->wl, &shm_listener, context); + wl_shm_add_listener(context->wl, &shm_listener, context); - /* Wait for SHM formats. */ - wl_display_roundtrip_queue(display, queue); + /* Wait for SHM formats. */ + wl_display_roundtrip_queue(display, queue); - return &context->base; + return &context->base; -error2: - wl_registry_destroy(context->registry); -error1: - wl_array_release(&context->formats); - free(context); -error0: - return NULL; + error2: + wl_registry_destroy(context->registry); + error1: + wl_array_release(&context->formats); + free(context); + error0: + return NULL; } -bool wayland_has_format(struct wld_context *base, uint32_t format) { - struct shm_context *context = shm_context(base); - uint32_t *supported_format; - uint32_t shm_format = format_wld_to_shm(format); +bool wayland_has_format(struct wld_context * base, uint32_t format) +{ + struct shm_context * context = shm_context(base); + uint32_t * supported_format; + uint32_t shm_format = format_wld_to_shm(format); - wl_array_for_each(supported_format, &context->formats) { - if (*supported_format == shm_format) - return true; - } + wl_array_for_each(supported_format, &context->formats) + { + if (*supported_format == shm_format) + return true; + } - return false; + return false; } -struct wld_renderer *context_create_renderer(struct wld_context *context) { - return wld_create_renderer(wld_pixman_context); +struct wld_renderer * context_create_renderer(struct wld_context * context) +{ + return wld_create_renderer(wld_pixman_context); } -struct buffer *context_create_buffer(struct wld_context *base, uint32_t width, - uint32_t height, uint32_t format, - uint32_t flags) { - struct shm_context *context = shm_context(base); - struct shm_buffer *buffer; - char name[] = "/tmp/wld-XXXXXX"; - uint32_t pitch = width * format_bytes_per_pixel(format); - size_t size = pitch * height; - int fd; - struct wl_shm_pool *pool; - struct wl_buffer *wl; +struct buffer * context_create_buffer(struct wld_context * base, + uint32_t width, uint32_t height, + uint32_t format, uint32_t flags) +{ + struct shm_context * context = shm_context(base); + struct shm_buffer * buffer; + char name[] = "/tmp/wld-XXXXXX"; + uint32_t pitch = width * format_bytes_per_pixel(format); + size_t size = pitch * height; + int fd; + struct wl_shm_pool * pool; + struct wl_buffer * wl; - if (!wayland_has_format(base, format)) - goto error0; + if (!wayland_has_format(base, format)) + goto error0; - if (!(buffer = malloc(sizeof *buffer))) - goto error0; + if (!(buffer = malloc(sizeof *buffer))) + goto error0; - fd = mkostemp(name, O_CLOEXEC); + fd = mkostemp(name, O_CLOEXEC); - if (fd < 0) - goto error1; + if (fd < 0) + goto error1; - unlink(name); + unlink(name); - if (posix_fallocate(fd, 0, size) != 0) - goto error2; + if (posix_fallocate(fd, 0, size) != 0) + goto error2; - if (!(pool = wl_shm_create_pool(context->wl, fd, size))) - goto error2; + if (!(pool = wl_shm_create_pool(context->wl, fd, size))) + goto error2; - wl = wl_shm_pool_create_buffer(pool, 0, width, height, pitch, - format_wld_to_shm(format)); - wl_shm_pool_destroy(pool); + wl = wl_shm_pool_create_buffer(pool, 0, width, height, pitch, + format_wld_to_shm(format)); + wl_shm_pool_destroy(pool); - if (!wl) - goto error2; + if (!wl) + goto error2; - buffer_initialize(&buffer->base, &wld_buffer_impl, width, height, format, - pitch); - buffer->fd = fd; + buffer_initialize(&buffer->base, &wld_buffer_impl, + width, height, format, pitch); + buffer->fd = fd; - if (!(wayland_buffer_add_exporter(&buffer->base, wl))) - goto error3; + if (!(wayland_buffer_add_exporter(&buffer->base, wl))) + goto error3; - return &buffer->base; + return &buffer->base; -error3: - wl_buffer_destroy(wl); -error2: - close(fd); -error1: - free(buffer); -error0: - return NULL; + error3: + wl_buffer_destroy(wl); + error2: + close(fd); + error1: + free(buffer); + error0: + return NULL; } -struct buffer *context_import_buffer(struct wld_context *context, uint32_t type, - union wld_object object, uint32_t width, - uint32_t height, uint32_t format, - uint32_t pitch) { - return NULL; +struct buffer * context_import_buffer(struct wld_context * context, + uint32_t type, union wld_object object, + uint32_t width, uint32_t height, + uint32_t format, uint32_t pitch) +{ + return NULL; } -void context_destroy(struct wld_context *base) { - struct shm_context *context = shm_context(base); +void context_destroy(struct wld_context * base) +{ + struct shm_context * context = shm_context(base); - wl_shm_destroy(context->wl); - wl_registry_destroy(context->registry); - wl_array_release(&context->formats); - wl_event_queue_destroy(context->base.queue); - free(context); + wl_shm_destroy(context->wl); + wl_registry_destroy(context->registry); + wl_array_release(&context->formats); + wl_event_queue_destroy(context->base.queue); + free(context); } /**** Buffer ****/ -bool buffer_map(struct buffer *base) { - struct shm_buffer *buffer = shm_buffer(&base->base); - void *data; +bool buffer_map(struct buffer * base) +{ + struct shm_buffer * buffer = shm_buffer(&base->base); + void * data; - data = mmap(NULL, buffer->base.base.pitch * buffer->base.base.height, - PROT_READ | PROT_WRITE, MAP_SHARED, buffer->fd, 0); + data = mmap(NULL, buffer->base.base.pitch * buffer->base.base.height, + PROT_READ | PROT_WRITE, MAP_SHARED, buffer->fd, 0); - if (data == MAP_FAILED) - return false; + if (data == MAP_FAILED) + return false; - buffer->base.base.map = data; + buffer->base.base.map = data; - return true; + return true; } -bool buffer_unmap(struct buffer *buffer) { - if (munmap(buffer->base.map, buffer->base.pitch * buffer->base.height) == - -1) { - return false; - } +bool buffer_unmap(struct buffer * buffer) +{ + if (munmap(buffer->base.map, + buffer->base.pitch * buffer->base.height) == -1) + { + return false; + } - buffer->base.map = NULL; + buffer->base.map = NULL; - return true; + return true; } -void buffer_destroy(struct buffer *base) { - struct shm_buffer *buffer = shm_buffer(&base->base); +void buffer_destroy(struct buffer * base) +{ + struct shm_buffer * buffer = shm_buffer(&base->base); - close(buffer->fd); - free(buffer); + close(buffer->fd); + free(buffer); } -void registry_global(void *data, struct wl_registry *registry, uint32_t name, - const char *interface, uint32_t version) { - struct shm_context *context = data; +void registry_global(void * data, struct wl_registry * registry, uint32_t name, + const char * interface, uint32_t version) +{ + struct shm_context * context = data; - if (strcmp(interface, "wl_shm") == 0) - context->wl = wl_registry_bind(registry, name, &wl_shm_interface, 1); + if (strcmp(interface, "wl_shm") == 0) + context->wl = wl_registry_bind(registry, name, &wl_shm_interface, 1); } -void registry_global_remove(void *data, struct wl_registry *registry, - uint32_t name) {} +void registry_global_remove(void * data, struct wl_registry * registry, + uint32_t name) +{ +} -void shm_format(void *data, struct wl_shm *wl, uint32_t format) { - struct shm_context *context = data; - uint32_t *added_format; +void shm_format(void * data, struct wl_shm * wl, uint32_t format) +{ + struct shm_context * context = data; + uint32_t * added_format; - if (!(added_format = wl_array_add(&context->formats, sizeof format))) - return; - *added_format = format; + if (!(added_format = wl_array_add(&context->formats, sizeof format))) + return; + *added_format = format; } + |