From 568754d3392a8da18ee86fb037f495951bcd05b8 Mon Sep 17 00:00:00 2001 From: nia Date: Wed, 11 Dec 2019 19:08:40 +0000 Subject: [PATCH 1/5] Initial bits to make this compile on NetBSD --- Makefile | 6 +++++- clients/status_bar.c | 4 ++++ config.c | 8 ++++++++ velox.c | 4 ++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ed2291a..1f5a2be 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ TARGETS := velox.pc velox SUBDIRS := protocol clients CLEAN_FILES := $(TARGETS) -VELOX_PACKAGES = swc xkbcommon libinput +VELOX_PACKAGES = swc xkbcommon VELOX_SOURCES = \ config.c \ layout.c \ @@ -31,6 +31,10 @@ VELOX_SOURCES = \ window.c \ protocol/velox-protocol.c +ifneq ($(shell uname),NetBSD) +VELOX_PACKAGES += libinput +endif + ifeq ($(if $(V),$(V),0), 0) define quiet @echo " $1 $@" diff --git a/clients/status_bar.c b/clients/status_bar.c index 3006e91..b05cd5d 100644 --- a/clients/status_bar.c +++ b/clients/status_bar.c @@ -27,7 +27,9 @@ #include #include #include +#ifndef __NetBSD__ #include +#endif #include #include #include @@ -521,8 +523,10 @@ run(void) fds[0].fd = wl_display_get_fd(display); fds[0].events = POLLIN; +#ifdef SFD_CLOEXEC fds[1].fd = signalfd(-1, &signals, SFD_CLOEXEC); fds[1].events = POLLIN; +#endif timer_settime(timer, 0, &timer_value, NULL); running = true; diff --git a/config.c b/config.c index f120e73..2bb79f3 100644 --- a/config.c +++ b/config.c @@ -27,7 +27,15 @@ #include "velox.h" #include +#if defined(__linux__) || defined(__FreeBSD__) #include +#else +#define BTN_LEFT (0x110) +#define BTN_RIGHT (0x111) +#define BTN_MIDDLE (0x112) +#define BTN_SIDE (0x113) +#define BTN_EXTRA (0x114) +#endif #include #include #include diff --git a/velox.c b/velox.c index 324cfb2..3f64e85 100644 --- a/velox.c +++ b/velox.c @@ -30,7 +30,9 @@ #include "window.h" #include "protocol/velox-server-protocol.h" +#ifndef __NetBSD__ #include +#endif #include #include #include @@ -72,8 +74,10 @@ new_window(struct swc_window *swc) static void new_device(struct libinput_device *device) { +#ifndef __NetBSD__ libinput_device_config_tap_set_enabled(device, tap_to_click ? LIBINPUT_CONFIG_TAP_ENABLED : LIBINPUT_CONFIG_TAP_DISABLED); +#endif } const struct swc_manager manager = { From 3d61a5aabae40de88dbf90e2369dec2129cdbc6f Mon Sep 17 00:00:00 2001 From: nia Date: Sat, 11 Jan 2020 16:57:54 +0000 Subject: [PATCH 2/5] status_bar: Convert to kqueue --- clients/status_bar.c | 61 ++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/clients/status_bar.c b/clients/status_bar.c index b05cd5d..cdd28ee 100644 --- a/clients/status_bar.c +++ b/clients/status_bar.c @@ -21,15 +21,15 @@ * SOFTWARE. */ +#define _NETBSD_SOURCE #include #include #include #include #include -#include -#ifndef __NetBSD__ -#include -#endif +#include +#include +#include #include #include #include @@ -514,42 +514,53 @@ run(void) .it_interval = { 1, 0 }, .it_value = { 0, 1 } }; - struct pollfd fds[2]; + struct kevent events[2]; + int i, kq, nev; struct screen *screen; sigemptyset(&signals); sigaddset(&signals, SIGALRM); sigprocmask(SIG_BLOCK, &signals, NULL); - fds[0].fd = wl_display_get_fd(display); - fds[0].events = POLLIN; -#ifdef SFD_CLOEXEC - fds[1].fd = signalfd(-1, &signals, SFD_CLOEXEC); - fds[1].events = POLLIN; -#endif + if ((kq = kqueue()) == -1) { + err(EXIT_FAILURE, "could not create kqueue"); + } + + EV_SET(&events[0], wl_display_get_fd(display), EVFILT_READ, EV_ADD, 0, -1, 0); + EV_SET(&events[1], SIGALRM, EVFILT_SIGNAL, EV_ADD, 0, -1, 1); + + if ((nev = kevent(kq, events, 2, NULL, 0, NULL)) == -1) { + err(EXIT_FAILURE, "kqueue failure"); + } timer_settime(timer, 0, &timer_value, NULL); running = true; while (true) { - if (poll(fds, sizeof(fds) / sizeof(fds[0]), -1) == -1) + if ((nev = kevent(kq, NULL, 0, events, 2, NULL)) == -1) { + err(EXIT_FAILURE, "kevent failure"); break; - - if (fds[0].revents & POLLIN) { - if (wl_display_dispatch(display) == -1) { - fprintf(stderr, "Wayland dispatch error: %s\n", - strerror(wl_display_get_error(display))); - break; - } } - if (fds[1].revents & POLLIN) { - time_t raw_time = time(NULL); - struct tm *local_time = localtime(&raw_time); + if (nev == 0) { + continue; + } + for (i = 0; i < nev; ++i) { + if (events[i].udata == 0) { + if (wl_display_dispatch(display) == -1) { + fprintf(stderr, "Wayland dispatch error: %s\n", + strerror(wl_display_get_error(display))); + break; + } + } + if (events[i].udata == 1) { + time_t raw_time = time(NULL); + struct tm *local_time = localtime(&raw_time); - sigwaitinfo(&signals, NULL); + sigwaitinfo(&signals, NULL); - strftime(clock_text, sizeof(clock_text), "%A %T %F", local_time); - update_text_item_data(&clock_data); + strftime(clock_text, sizeof(clock_text), "%A %T %F", local_time); + update_text_item_data(&clock_data); + } } if (need_draw) { From 6c24e991e5e73b26807b410331a11e4324eb208a Mon Sep 17 00:00:00 2001 From: nia Date: Sat, 11 Jan 2020 17:24:45 +0000 Subject: [PATCH 3/5] status_bar: restore poll code for linux only --- clients/status_bar.c | 93 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 75 insertions(+), 18 deletions(-) diff --git a/clients/status_bar.c b/clients/status_bar.c index cdd28ee..9397a89 100644 --- a/clients/status_bar.c +++ b/clients/status_bar.c @@ -21,15 +21,19 @@ * SOFTWARE. */ -#define _NETBSD_SOURCE #include #include #include #include #include +#ifdef __linux__ +#include +#include +#else +#define _NETBSD_SOURCE #include -#include #include +#endif #include #include #include @@ -506,21 +510,56 @@ setup(void) wl_display_flush(display); } +#ifdef __linux__ static void -run(void) +run_eventloop(sigset_t *signals) { - sigset_t signals; - struct itimerspec timer_value = { - .it_interval = { 1, 0 }, - .it_value = { 0, 1 } - }; - struct kevent events[2]; - int i, kq, nev; + struct pollfd fds[2]; struct screen *screen; - sigemptyset(&signals); - sigaddset(&signals, SIGALRM); - sigprocmask(SIG_BLOCK, &signals, NULL); + fds[0].fd = wl_display_get_fd(display); + fds[0].events = POLLIN; + fds[1].fd = signalfd(-1, &signals, SFD_CLOEXEC); + fds[1].events = POLLIN; + + while (true) { + if (poll(fds, sizeof(fds) / sizeof(fds[0]), -1) == -1) + break; + + if (fds[0].revents & POLLIN) { + if (wl_display_dispatch(display) == -1) { + fprintf(stderr, "Wayland dispatch error: %s\n", + strerror(wl_display_get_error(display))); + break; + } + } + if (fds[1].revents & POLLIN) { + time_t raw_time = time(NULL); + struct tm *local_time = localtime(&raw_time); + + sigwaitinfo(signals, NULL); + + strftime(clock_text, sizeof(clock_text), "%A %T %F", local_time); + update_text_item_data(&clock_data); + } + + if (need_draw) { + wl_list_for_each (screen, &screens, link) + draw(&screen->status_bar); + need_draw = false; + } + + wl_display_flush(display); + } + +} +#else +static void +run_eventloop(sigset_t *signals) +{ + struct screen *screen; + struct kevent events[2]; + int i, kq, nev; if ((kq = kqueue()) == -1) { err(EXIT_FAILURE, "could not create kqueue"); @@ -533,10 +572,7 @@ run(void) err(EXIT_FAILURE, "kqueue failure"); } - timer_settime(timer, 0, &timer_value, NULL); - running = true; - - while (true) { + for (;;) { if ((nev = kevent(kq, NULL, 0, events, 2, NULL)) == -1) { err(EXIT_FAILURE, "kevent failure"); break; @@ -556,7 +592,7 @@ run(void) time_t raw_time = time(NULL); struct tm *local_time = localtime(&raw_time); - sigwaitinfo(&signals, NULL); + sigwaitinfo(signals, NULL); strftime(clock_text, sizeof(clock_text), "%A %T %F", local_time); update_text_item_data(&clock_data); @@ -571,6 +607,27 @@ run(void) wl_display_flush(display); } + +} +#endif + +static void +run(void) +{ + sigset_t signals; + struct itimerspec timer_value = { + .it_interval = { 1, 0 }, + .it_value = { 0, 1 } + }; + + sigemptyset(&signals); + sigaddset(&signals, SIGALRM); + sigprocmask(SIG_BLOCK, &signals, NULL); + + timer_settime(timer, 0, &timer_value, NULL); + running = true; + + run_eventloop(&signals); } int From 5c5afcc57444326b95b24171c74f5e08b8877317 Mon Sep 17 00:00:00 2001 From: nia Date: Thu, 20 Feb 2020 12:41:16 +0000 Subject: [PATCH 4/5] Replace #ifdef OS with #ifdef HAVE_... Make kqueue usage in status_bar less spammy. --- Makefile | 12 ++++++ clients/status_bar.c | 99 ++++++++++++++++++++++---------------------- config.c | 2 +- velox.c | 4 +- 4 files changed, 65 insertions(+), 52 deletions(-) diff --git a/Makefile b/Makefile index 1f5a2be..2895bd7 100644 --- a/Makefile +++ b/Makefile @@ -66,6 +66,18 @@ else FINAL_CPPFLAGS += -DNDEBUG endif +ifeq ($(shell uname),Linux) + FINAL_CPPFLAGS += -DHAVE_LINUX_INPUT_H + FINAL_CPPFLAGS += -DHAVE_LIBINPUT +else ifeq ($(shell uname),FreeBSD) + FINAL_CPPFLAGS += -DHAVE_LINUX_INPUT_H + FINAL_CPPFLAGS += -DHAVE_LIBINPUT + FINAL_CPPFLAGS += -DHAVE_KQUEUE +else ifeq ($(shell uname),NetBSD) + FINAL_CPPFLAGS += -D_NETBSD_SOURCE + FINAL_CPPFLAGS += -DHAVE_KQUEUE +endif + compile = $(call quiet,CC) $(FINAL_CPPFLAGS) $(FINAL_CFLAGS) -c -o $@ $< \ -MMD -MP -MF .deps/$(basename $<).d -MT $(basename $@).o link = $(call quiet,CCLD,$(CC)) $(LDFLAGS) -o $@ $^ diff --git a/clients/status_bar.c b/clients/status_bar.c index 9397a89..bcd3dca 100644 --- a/clients/status_bar.c +++ b/clients/status_bar.c @@ -26,13 +26,12 @@ #include #include #include -#ifdef __linux__ -#include -#include -#else -#define _NETBSD_SOURCE +#ifdef HAVE_KQUEUE #include #include +#else +#include +#include #endif #include #include @@ -510,50 +509,8 @@ setup(void) wl_display_flush(display); } -#ifdef __linux__ -static void -run_eventloop(sigset_t *signals) -{ - struct pollfd fds[2]; - struct screen *screen; - - fds[0].fd = wl_display_get_fd(display); - fds[0].events = POLLIN; - fds[1].fd = signalfd(-1, &signals, SFD_CLOEXEC); - fds[1].events = POLLIN; - - while (true) { - if (poll(fds, sizeof(fds) / sizeof(fds[0]), -1) == -1) - break; - - if (fds[0].revents & POLLIN) { - if (wl_display_dispatch(display) == -1) { - fprintf(stderr, "Wayland dispatch error: %s\n", - strerror(wl_display_get_error(display))); - break; - } - } - if (fds[1].revents & POLLIN) { - time_t raw_time = time(NULL); - struct tm *local_time = localtime(&raw_time); - - sigwaitinfo(signals, NULL); - - strftime(clock_text, sizeof(clock_text), "%A %T %F", local_time); - update_text_item_data(&clock_data); - } - - if (need_draw) { - wl_list_for_each (screen, &screens, link) - draw(&screen->status_bar); - need_draw = false; - } - - wl_display_flush(display); - } -} -#else +#ifdef HAVE_KQUEUE static void run_eventloop(sigset_t *signals) { @@ -572,7 +529,7 @@ run_eventloop(sigset_t *signals) err(EXIT_FAILURE, "kqueue failure"); } - for (;;) { + while (running) { if ((nev = kevent(kq, NULL, 0, events, 2, NULL)) == -1) { err(EXIT_FAILURE, "kevent failure"); break; @@ -585,6 +542,7 @@ run_eventloop(sigset_t *signals) if (wl_display_dispatch(display) == -1) { fprintf(stderr, "Wayland dispatch error: %s\n", strerror(wl_display_get_error(display))); + running = false; break; } } @@ -608,6 +566,49 @@ run_eventloop(sigset_t *signals) wl_display_flush(display); } +} +#else +static void +run_eventloop(sigset_t *signals) +{ + struct pollfd fds[2]; + struct screen *screen; + + fds[0].fd = wl_display_get_fd(display); + fds[0].events = POLLIN; + fds[1].fd = signalfd(-1, &signals, SFD_CLOEXEC); + fds[1].events = POLLIN; + + while (true) { + if (poll(fds, sizeof(fds) / sizeof(fds[0]), -1) == -1) + break; + + if (fds[0].revents & POLLIN) { + if (wl_display_dispatch(display) == -1) { + fprintf(stderr, "Wayland dispatch error: %s\n", + strerror(wl_display_get_error(display))); + break; + } + } + if (fds[1].revents & POLLIN) { + time_t raw_time = time(NULL); + struct tm *local_time = localtime(&raw_time); + + sigwaitinfo(signals, NULL); + + strftime(clock_text, sizeof(clock_text), "%A %T %F", local_time); + update_text_item_data(&clock_data); + } + + if (need_draw) { + wl_list_for_each (screen, &screens, link) + draw(&screen->status_bar); + need_draw = false; + } + + wl_display_flush(display); + } + } #endif diff --git a/config.c b/config.c index 2bb79f3..ebb3942 100644 --- a/config.c +++ b/config.c @@ -27,7 +27,7 @@ #include "velox.h" #include -#if defined(__linux__) || defined(__FreeBSD__) +#ifdef HAVE_LINUX_INPUT_H #include #else #define BTN_LEFT (0x110) diff --git a/velox.c b/velox.c index 3f64e85..e04b1ca 100644 --- a/velox.c +++ b/velox.c @@ -30,7 +30,7 @@ #include "window.h" #include "protocol/velox-server-protocol.h" -#ifndef __NetBSD__ +#ifdef HAVE_LIBINPUT #include #endif #include @@ -74,7 +74,7 @@ new_window(struct swc_window *swc) static void new_device(struct libinput_device *device) { -#ifndef __NetBSD__ +#ifdef HAVE_LIBINPUT libinput_device_config_tap_set_enabled(device, tap_to_click ? LIBINPUT_CONFIG_TAP_ENABLED : LIBINPUT_CONFIG_TAP_DISABLED); #endif From fe4443e9789ed5895d933bc107dde3d3d97c7467 Mon Sep 17 00:00:00 2001 From: nia Date: Fri, 6 Mar 2020 15:26:37 +0000 Subject: [PATCH 5/5] status_bar: convert to selfpipe --- clients/status_bar.c | 146 ++++++++++++++++++------------------------- 1 file changed, 60 insertions(+), 86 deletions(-) diff --git a/clients/status_bar.c b/clients/status_bar.c index bcd3dca..b328aef 100644 --- a/clients/status_bar.c +++ b/clients/status_bar.c @@ -26,14 +26,10 @@ #include #include #include -#ifdef HAVE_KQUEUE -#include -#include -#else #include -#include -#endif #include +#include +#include #include #include #include @@ -177,6 +173,7 @@ static bool running, need_draw; static char clock_text[32]; static struct item_data divider_data = {.width = 14 }; static struct text_item_data clock_data = {.text = clock_text }; +static int pfd[2]; static void __attribute__((noreturn)) die(const char *const format, ...) { @@ -509,79 +506,74 @@ setup(void) wl_display_flush(display); } - -#ifdef HAVE_KQUEUE static void -run_eventloop(sigset_t *signals) +sigalrm_handler(int sig) { - struct screen *screen; - struct kevent events[2]; - int i, kq, nev; + int p_errno = errno; /* Preserve errno */ + int ret; - if ((kq = kqueue()) == -1) { - err(EXIT_FAILURE, "could not create kqueue"); - } + if ((ret = write(pfd[1], ".", 1)) != 1) + die("self-pipe write failed\n"); + errno = p_errno; +} - EV_SET(&events[0], wl_display_get_fd(display), EVFILT_READ, EV_ADD, 0, -1, 0); - EV_SET(&events[1], SIGALRM, EVFILT_SIGNAL, EV_ADD, 0, -1, 1); +static int +set_nonblock(int fd) +{ + int flags; - if ((nev = kevent(kq, events, 2, NULL, 0, NULL)) == -1) { - err(EXIT_FAILURE, "kqueue failure"); + if ((flags = fcntl(fd, F_GETFL)) == -1) { + return -1; } - while (running) { - if ((nev = kevent(kq, NULL, 0, events, 2, NULL)) == -1) { - err(EXIT_FAILURE, "kevent failure"); - break; - } - if (nev == 0) { - continue; - } - for (i = 0; i < nev; ++i) { - if (events[i].udata == 0) { - if (wl_display_dispatch(display) == -1) { - fprintf(stderr, "Wayland dispatch error: %s\n", - strerror(wl_display_get_error(display))); - running = false; - break; - } - } - if (events[i].udata == 1) { - time_t raw_time = time(NULL); - struct tm *local_time = localtime(&raw_time); - - sigwaitinfo(signals, NULL); - - strftime(clock_text, sizeof(clock_text), "%A %T %F", local_time); - update_text_item_data(&clock_data); - } - } - - if (need_draw) { - wl_list_for_each (screen, &screens, link) - draw(&screen->status_bar); - need_draw = false; - } - - wl_display_flush(display); + if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) { + return -1; } - + return 0; } -#else + + static void -run_eventloop(sigset_t *signals) +run(void) { + struct itimerspec timer_value = { + .it_interval = { 1, 0 }, + .it_value = { 0, 1 } + }; struct pollfd fds[2]; struct screen *screen; + struct sigaction sa = {0}; + + if (pipe(pfd) == -1) + die("creating pipe failed: %s\n", strerror(errno)); + + if (set_nonblock(pfd[0]) == -1) + die("could not set O_NONBLOCK on pipe fd: %s\n", strerror(errno)); + + if (set_nonblock(pfd[1]) == -1) + die("could not set O_NONBLOCK on pipe fd\n", strerror(errno)); + + sa.sa_flags = SA_RESTART; + sa.sa_handler = sigalrm_handler; + + if (sigaction(SIGALRM, &sa, NULL) == -1) + die("sigaction failed\n", strerror(errno)); fds[0].fd = wl_display_get_fd(display); fds[0].events = POLLIN; - fds[1].fd = signalfd(-1, &signals, SFD_CLOEXEC); + fds[1].fd = pfd[0]; fds[1].events = POLLIN; + timer_settime(timer, 0, &timer_value, NULL); + running = true; + while (true) { - if (poll(fds, sizeof(fds) / sizeof(fds[0]), -1) == -1) - break; + if (poll(fds, 2, -1) == -1) { + if (errno != EINTR) { + perror("poll"); + break; + } + } if (fds[0].revents & POLLIN) { if (wl_display_dispatch(display) == -1) { @@ -591,11 +583,14 @@ run_eventloop(sigset_t *signals) } } if (fds[1].revents & POLLIN) { - time_t raw_time = time(NULL); - struct tm *local_time = localtime(&raw_time); - - sigwaitinfo(signals, NULL); - + uint8_t discard; + time_t raw_time; + struct tm *local_time; + int nbytes; + + while ((nbytes = read(pfd[0], &discard, 1)) > 0); + raw_time = time(NULL); + local_time = localtime(&raw_time); strftime(clock_text, sizeof(clock_text), "%A %T %F", local_time); update_text_item_data(&clock_data); } @@ -608,27 +603,6 @@ run_eventloop(sigset_t *signals) wl_display_flush(display); } - -} -#endif - -static void -run(void) -{ - sigset_t signals; - struct itimerspec timer_value = { - .it_interval = { 1, 0 }, - .it_value = { 0, 1 } - }; - - sigemptyset(&signals); - sigaddset(&signals, SIGALRM); - sigprocmask(SIG_BLOCK, &signals, NULL); - - timer_settime(timer, 0, &timer_value, NULL); - running = true; - - run_eventloop(&signals); } int