From 59fa4cfeaac30d4b38625074c8fe9512c1c1ab57 Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Sun, 13 Oct 2019 12:38:47 +0000 Subject: [PATCH] Move output handling into its own source file --- include/output.h | 16 +++++++++ meson.build | 1 + src/main.c | 87 +++++------------------------------------------- src/output.c | 83 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 79 deletions(-) create mode 100644 include/output.h create mode 100644 src/output.c diff --git a/include/output.h b/include/output.h new file mode 100644 index 0000000..3477d2b --- /dev/null +++ b/include/output.h @@ -0,0 +1,16 @@ +#pragma once + +struct output { + struct wl_output* wl_output; + struct wl_list link; + + uint32_t id; + uint32_t width; + uint32_t height; + char make[256]; + char model[256]; +}; + +struct output* output_new(struct wl_output* wl_output, uint32_t id); +void output_destroy(struct output* output); +void output_list_destroy(struct wl_list* list); diff --git a/meson.build b/meson.build index 02de0a2..f252c24 100644 --- a/meson.build +++ b/meson.build @@ -45,6 +45,7 @@ sources = [ 'src/strlcpy.c', 'src/shm.c', 'src/screencopy.c', + 'src/output.c', ] dependencies = [ diff --git a/src/main.c b/src/main.c index 158c227..3b7fecf 100644 --- a/src/main.c +++ b/src/main.c @@ -38,6 +38,7 @@ #include "screencopy.h" #include "strlcpy.h" #include "logging.h" +#include "output.h" enum frame_capture_backend_type { FRAME_CAPTURE_BACKEND_NONE = 0, @@ -45,17 +46,6 @@ enum frame_capture_backend_type { FRAME_CAPTURE_BACKEND_DMABUF, }; -struct output { - struct wl_output* wl_output; - struct wl_list link; - - uint32_t id; - uint32_t width; - uint32_t height; - char make[256]; - char model[256]; -}; - struct wayvnc { struct wl_display* display; struct wl_registry* registry; @@ -95,53 +85,6 @@ frame_capture_backend_from_string(const char* str) return FRAME_CAPTURE_BACKEND_NONE; } -static void output_handle_geometry(void* data, struct wl_output* wl_output, - int32_t x, int32_t y, int32_t phys_width, - int32_t phys_height, int32_t subpixel, - const char* make, const char* model, - int32_t transform) -{ - struct output* output = data; - - strlcpy(output->make, make, sizeof(output->make)); - strlcpy(output->model, model, sizeof(output->make)); -} - -static void output_handle_mode(void* data, struct wl_output* wl_output, - uint32_t flags, int32_t width, int32_t height, - int32_t refresh) -{ - struct output* output = data; - - if (!(flags & WL_OUTPUT_MODE_CURRENT)) - return; - - output->width = width; - output->height = height; -} - -static void output_handle_done(void* data, struct wl_output* wl_output) -{ -} - -static void output_handle_scale(void* data, struct wl_output* wl_output, - int32_t factor) -{ -} - -static const struct wl_output_listener output_listener = { - .geometry = output_handle_geometry, - .mode = output_handle_mode, - .done = output_handle_done, - .scale = output_handle_scale, -}; - -void output_destroy(struct output* output) -{ - wl_output_destroy(output->wl_output); - free(output); -} - struct output* wayvnc_output_find(struct wayvnc* self, uint32_t id) { struct output* output; @@ -153,17 +96,6 @@ struct output* wayvnc_output_find(struct wayvnc* self, uint32_t id) return NULL; } -void output_list_destroy(struct wl_list* list) -{ - struct output* output; - struct output* tmp; - - wl_list_for_each_safe(output, tmp, list, link) { - wl_list_remove(&output->link); - output_destroy(output); - } -} - static void registry_add(void* data, struct wl_registry* registry, uint32_t id, const char* interface, uint32_t version) @@ -171,19 +103,16 @@ static void registry_add(void* data, struct wl_registry* registry, struct wayvnc* self = data; if (strcmp(interface, wl_output_interface.name) == 0) { - struct output* output = calloc(1, sizeof(*output)); - if (!output) { - log_error("OOM\n"); + struct wl_output* wl_output = + wl_registry_bind(registry, id, &wl_output_interface, + version); + if (!wl_output) return; - } - output->id = id; - output->wl_output = wl_registry_bind(registry, id, - &wl_output_interface, - version); + struct output* output = output_new(wl_output, id); + if (!output) + return; - wl_output_add_listener(output->wl_output, &output_listener, - output); wl_list_insert(&self->outputs, &output->link); return; } diff --git a/src/output.c b/src/output.c new file mode 100644 index 0000000..9145383 --- /dev/null +++ b/src/output.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include + +#include "output.h" +#include "strlcpy.h" +#include "logging.h" + +static void output_handle_geometry(void* data, struct wl_output* wl_output, + int32_t x, int32_t y, int32_t phys_width, + int32_t phys_height, int32_t subpixel, + const char* make, const char* model, + int32_t transform) +{ + struct output* output = data; + + strlcpy(output->make, make, sizeof(output->make)); + strlcpy(output->model, model, sizeof(output->make)); +} + +static void output_handle_mode(void* data, struct wl_output* wl_output, + uint32_t flags, int32_t width, int32_t height, + int32_t refresh) +{ + struct output* output = data; + + if (!(flags & WL_OUTPUT_MODE_CURRENT)) + return; + + output->width = width; + output->height = height; +} + +static void output_handle_done(void* data, struct wl_output* wl_output) +{ +} + +static void output_handle_scale(void* data, struct wl_output* wl_output, + int32_t factor) +{ +} + +static const struct wl_output_listener output_listener = { + .geometry = output_handle_geometry, + .mode = output_handle_mode, + .done = output_handle_done, + .scale = output_handle_scale, +}; + +void output_destroy(struct output* output) +{ + wl_output_destroy(output->wl_output); + free(output); +} + +void output_list_destroy(struct wl_list* list) +{ + struct output* output; + struct output* tmp; + + wl_list_for_each_safe(output, tmp, list, link) { + wl_list_remove(&output->link); + output_destroy(output); + } +} + +struct output* output_new(struct wl_output* wl_output, uint32_t id) +{ + struct output* output = calloc(1, sizeof(*output)); + if (!output) { + log_error("OOM\n"); + return NULL; + } + + output->wl_output = wl_output; + output->id = id; + + wl_output_add_listener(output->wl_output, &output_listener, + output); + + return output; +}