wayclip

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

commit 097c780f13caab5ee6ca836bba23a73111892670
parent f4058338ccc4ef237a1e5a8ae3328736d9817605
Author: Nihal Jere <nihal@nihaljere.xyz>
Date:   Sat,  2 Jul 2022 22:00:45 -0500

remove util.[ch]

It serves the same purpose as common.[ch] so move stuff there.

Diffstat:
MMakefile | 4++--
Mcommon.c | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcommon.h | 7++++++-
Dutil.c | 101-------------------------------------------------------------------------------
Dutil.h | 4----
Mwaycopy.c | 1-
Mwaypaste.c | 1-
7 files changed, 100 insertions(+), 110 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,8 +1,8 @@ .POSIX: PREFIX = /usr/local LIB = -lwayland-client -WAYCOPY_OBJ = protocol/wlr-data-control-unstable-v1.o waycopy.o common.o util.o -WAYPASTE_OBJ = protocol/wlr-data-control-unstable-v1.o waypaste.o common.o util.o +WAYCOPY_OBJ = protocol/wlr-data-control-unstable-v1.o waycopy.o common.o +WAYPASTE_OBJ = protocol/wlr-data-control-unstable-v1.o waypaste.o common.o EXE = waycopy waypaste all: $(EXE) diff --git a/common.c b/common.c @@ -1,15 +1,24 @@ +#define _POSIX_C_SOURCE 2 #include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> #include <string.h> +#include <unistd.h> #include <wayland-client.h> #include "protocol/wlr-data-control-unstable-v1-client-protocol.h" #include "common.h" +const char *argv0; bool seat_found = false; struct wl_seat *seat; struct zwlr_data_control_manager_v1 *data_control_manager; +struct options options = { + .type = "text/plain;charset=utf-8" +}; + static void seat_capabilities(void *data, struct wl_seat *seat, uint32_t cap) { @@ -52,3 +61,86 @@ const struct wl_registry_listener registry_listener = { .global = registry_global, .global_remove = registry_global_remove, }; + +void +die(const char *const error) +{ + fprintf(stderr, "%s: %s\n", argv0, error); + exit(1); +} + +void +warn(const char *const error) +{ + fprintf(stderr, "%s: warning: %s\n", argv0, error); +} + +void +copyfd(const int out, const int in) +{ + char buf[BUFSIZ]; + + do { + ssize_t rcount = read(in, buf, BUFSIZ); + if (rcount == -1) { + warn("read failed"); + return; + } + + size_t wcount = 0; + char *ptr = buf; + while (wcount < rcount) { + ssize_t len = write(out, ptr, rcount); + if (len == -1) { + warn("read failed"); + return; + } + + ptr += len; + wcount += len; + } + if (!rcount) + break; + } while (1); +} + +static void +usage(int code) +{ + fprintf(stderr, "usage: %s [-s seat] [-t mimetype]\n", argv0); + exit(code); +} + +void +parseopts(const char *opts, int argc, char *const argv[]) +{ + while (1) { + int next = getopt(argc, argv, opts); + if (next == -1) { + if (argv[optind] && *argv[optind] != '-') + usage(1); + + break; + } + + if (next == ':' || next == '?') + exit(1); + + switch (next) { + case 'f': + options.foreground = true; + break; + case 'h': + usage(0); + case 's': + options.seat = optarg; + break; + case 't': + if (strlen(optarg) > 255) + die("mimetype can be at most 255 characters"); + + options.type = optarg; + break; + } + } +} diff --git a/common.h b/common.h @@ -6,8 +6,13 @@ extern const struct wl_registry_listener registry_listener; extern const char *argv0; -extern struct { +extern struct options { const char *type; const char *seat; bool foreground; } options; + +void die(const char *const error); +void warn(const char *const error); +void copyfd(int out, int in); +void parseopts(const char *opts, int argc, char *const argv[]); diff --git a/util.c b/util.c @@ -1,101 +0,0 @@ -#define _POSIX_C_SOURCE 2 -#include <stdlib.h> -#include <stdbool.h> -#include <stdio.h> -#include <string.h> -#include <unistd.h> - -#include "util.h" - -const char *argv0; - -void -die(const char *const error) -{ - fprintf(stderr, "%s: %s\n", argv0, error); - exit(1); -} - -void -warn(const char *const error) -{ - fprintf(stderr, "%s: warning: %s\n", argv0, error); -} - -void -copyfd(const int out, const int in) -{ - char buf[BUFSIZ]; - - do { - ssize_t rcount = read(in, buf, BUFSIZ); - if (rcount == -1) { - warn("read failed"); - return; - } - - size_t wcount = 0; - char *ptr = buf; - while (wcount < rcount) { - ssize_t len = write(out, ptr, rcount); - if (len == -1) { - warn("read failed"); - return; - } - - ptr += len; - wcount += len; - } - if (!rcount) - break; - } while (1); -} - -struct { - const char *type; - const char *seat; - bool foreground; -} options = { - .type = "text/plain;charset=utf-8" -}; - -static void -usage(int code) -{ - fprintf(stderr, "usage: %s [-s seat] [-t mimetype]\n", argv0); - exit(code); -} - -void -parseopts(const char *opts, int argc, char *const argv[]) -{ - while (1) { - int next = getopt(argc, argv, opts); - if (next == -1) { - if (argv[optind] && *argv[optind] != '-') - usage(1); - - break; - } - - if (next == ':' || next == '?') - exit(1); - - switch (next) { - case 'f': - options.foreground = true; - break; - case 'h': - usage(0); - case 's': - options.seat = optarg; - break; - case 't': - if (strlen(optarg) > 255) - die("mimetype can be at most 255 characters"); - - options.type = optarg; - break; - } - } -} diff --git a/util.h b/util.h @@ -1,4 +0,0 @@ -void die(const char *const error); -void warn(const char *const error); -void copyfd(int out, int in); -void parseopts(const char *opts, int argc, char *const argv[]); diff --git a/waycopy.c b/waycopy.c @@ -12,7 +12,6 @@ #include "protocol/wlr-data-control-unstable-v1-client-protocol.h" #include "common.h" -#include "util.h" struct wl_registry *registry; int temp; diff --git a/waypaste.c b/waypaste.c @@ -9,7 +9,6 @@ #include "protocol/wlr-data-control-unstable-v1-client-protocol.h" #include "common.h" -#include "util.h" struct wl_display *display;