wayclip

Wayland clipboard utility
git clone git://git.nihaljere.xyz/wayclip
Log | Files | Refs | README | LICENSE

commit 03f1fc69a08f61594048b881581089aaf51626b9
parent bba0eb3949da2dc612dfaafa68a7d575fc4a0cec
Author: Nihal Jere <nihal@nihaljere.xyz>
Date:   Wed, 23 Feb 2022 12:53:35 -0600

extract common code into common.c

Diffstat:
MMakefile | 8++++----
Acommon.c | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acommon.h | 12++++++++++++
Mwaycopy.c | 57++-------------------------------------------------------
Mwaypaste.c | 55+------------------------------------------------------
5 files changed, 79 insertions(+), 113 deletions(-)

diff --git a/Makefile b/Makefile @@ -3,11 +3,11 @@ all: waycopy waypaste -waypaste: protocol/wlr-data-control-unstable-v1-client-protocol.h protocol/wlr-data-control-unstable-v1.o waypaste.o util.o - $(CC) protocol/wlr-data-control-unstable-v1.o waypaste.o util.o -lwayland-client -o waypaste +waypaste: protocol/wlr-data-control-unstable-v1-client-protocol.h protocol/wlr-data-control-unstable-v1.o waypaste.o common.o util.o + $(CC) protocol/wlr-data-control-unstable-v1.o waypaste.o common.o util.o -lwayland-client -o waypaste -waycopy: protocol/wlr-data-control-unstable-v1-client-protocol.h protocol/wlr-data-control-unstable-v1.o waycopy.o util.o - $(CC) protocol/wlr-data-control-unstable-v1.o waycopy.o util.o -lwayland-client -o waycopy +waycopy: protocol/wlr-data-control-unstable-v1-client-protocol.h protocol/wlr-data-control-unstable-v1.o waycopy.o common.o util.o + $(CC) protocol/wlr-data-control-unstable-v1.o waycopy.o util.o common.o -lwayland-client -o waycopy protocol/wlr-data-control-unstable-v1.c: wayland-scanner private-code protocol/wlr-data-control-unstable-v1.xml protocol/wlr-data-control-unstable-v1.c diff --git a/common.c b/common.c @@ -0,0 +1,60 @@ +#include <stdbool.h> +#include <string.h> +#include <wayland-client.h> + +#include "protocol/wlr-data-control-unstable-v1-client-protocol.h" + +extern struct { + const char *type; + const char *seat; +} options; + +bool seat_found = false; + +struct wl_seat *seat; +struct zwlr_data_control_manager_v1 *data_control_manager; + +static void +seat_capabilities(void *data, struct wl_seat *seat, uint32_t cap) +{ +} + +static void +seat_name(void *data, struct wl_seat *_seat, const char *name) +{ + if (!seat_found && strcmp(name, options.seat) == 0) { + seat_found = true; + seat = _seat; + } + else + wl_seat_destroy(_seat); +} + +const struct wl_seat_listener seat_listener = { + .capabilities = seat_capabilities, + .name = seat_name, +}; + +void +registry_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) +{ + if (!seat_found && strcmp(interface, "wl_seat") == 0) { + seat = wl_registry_bind(registry, name, &wl_seat_interface, 2); + if (options.seat) { + wl_seat_add_listener(seat, &seat_listener, NULL); + seat = NULL; + } else seat_found = true; + } else if (strcmp(interface, "zwlr_data_control_manager_v1") == 0) { + data_control_manager = wl_registry_bind(registry, name, &zwlr_data_control_manager_v1_interface, 1); + } +} + +void +registry_global_remove(void *data, struct wl_registry *registry, uint32_t name) +{ +} + +const struct wl_registry_listener registry_listener = { + .global = registry_global, + .global_remove = registry_global_remove, +}; diff --git a/common.h b/common.h @@ -0,0 +1,12 @@ +extern bool seat_found; +extern struct wl_seat *seat; +extern struct zwlr_data_control_manager_v1 *data_control_manager; +extern const struct wl_seat_listener seat_listener; +extern const struct wl_registry_listener registry_listener; + +extern const char *argv0; + +extern struct { + const char *type; + const char *seat; +} options; diff --git a/waycopy.c b/waycopy.c @@ -9,69 +9,16 @@ #include <unistd.h> #include "protocol/wlr-data-control-unstable-v1-client-protocol.h" +#include "common.h" #include "util.h" -extern const char *argv0; - -extern struct { - const char *type; - const char *seat; -} options; - #define MIMETYPE_MAX_SIZE 256 char mimetype[MIMETYPE_MAX_SIZE]; -struct zwlr_data_control_manager_v1 *data_control_manager; -struct wl_seat *seat; struct wl_registry *registry; int temp; -bool running = true, seat_found = false; - -void -seat_capabilities(void *data, struct wl_seat *seat, uint32_t cap) -{ -} - -void -seat_name(void *data, struct wl_seat *_seat, const char *name) -{ - if (!seat_found && strcmp(name, options.seat) == 0) { - seat_found = true; - seat = _seat; - } - else - wl_seat_destroy(_seat); -} - -struct wl_seat_listener seat_listener = { - .capabilities = seat_capabilities, - .name = seat_name, -}; - -void -registry_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) -{ - if (!seat_found && strcmp(interface, "wl_seat") == 0) { - seat = wl_registry_bind(registry, name, &wl_seat_interface, 2); - if (options.seat) { - wl_seat_add_listener(seat, &seat_listener, NULL); - seat = NULL; - } else seat_found = true; - } else if (strcmp(interface, "zwlr_data_control_manager_v1") == 0) { - data_control_manager = wl_registry_bind(registry, name, &zwlr_data_control_manager_v1_interface, 1); - } -} - -void -registry_global_remove(void *data, struct wl_registry *registry, uint32_t name) -{ -} - -static const struct wl_registry_listener registry_listener = { - .global = registry_global, - .global_remove = registry_global_remove, -}; +bool running = true; void data_source_send(void *data, struct zwlr_data_control_source_v1 *source, const char *mime_type, int32_t fd) diff --git a/waypaste.c b/waypaste.c @@ -8,65 +8,12 @@ #include <unistd.h> #include "protocol/wlr-data-control-unstable-v1-client-protocol.h" +#include "common.h" #include "util.h" -extern const char *argv0; - -extern struct { - const char *type; - const char *seat; -} options; - -bool seat_found = false; - -struct zwlr_data_control_manager_v1 *data_control_manager; -struct wl_seat *seat; struct wl_display *display; void -seat_capabilities(void *data, struct wl_seat *seat, uint32_t cap) -{ -} - -void -seat_name(void *data, struct wl_seat *_seat, const char *name) -{ - if (!seat_found && strcmp(name, options.seat) == 0) { - seat_found = true; - seat = _seat; - } else wl_seat_destroy(_seat); -} - -struct wl_seat_listener seat_listener = { - .capabilities = seat_capabilities, - .name = seat_name, -}; - -void -registry_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) -{ - if (!seat_found && strcmp(interface, "wl_seat") == 0) { - seat = wl_registry_bind(registry, name, &wl_seat_interface, 2); - if (options.seat) { - wl_seat_add_listener(seat, &seat_listener, NULL); - seat = NULL; - } else seat_found = true; - } else if (strcmp(interface, "zwlr_data_control_manager_v1") == 0) { - data_control_manager = wl_registry_bind(registry, name, &zwlr_data_control_manager_v1_interface, 1); - } -} - -void -registry_global_remove(void *data, struct wl_registry *registry, uint32_t name) -{ -} - -static const struct wl_registry_listener registry_listener = { - .global = registry_global, - .global_remove = registry_global_remove, -}; - -void offer_offer(void *data, struct zwlr_data_control_offer_v1 *offer, const char *mime_type) { int pipes[2];