From 8c89495736b365c4fc098a19419b68f6e3f2ab9f Mon Sep 17 00:00:00 2001 From: Midgard Date: Thu, 20 Oct 2022 20:48:59 +0200 Subject: [PATCH] Add --time argument to automatically disappear --- state.h | 1 + wayland.c | 37 +++++++++++++++++++++++++++++++++++-- wl-overlay.c | 10 ++++++++-- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/state.h b/state.h index f864742..b55ea9f 100644 --- a/state.h +++ b/state.h @@ -56,6 +56,7 @@ struct user_request { int width; int height; int padding; + int time; }; struct wayland_state { diff --git a/wayland.c b/wayland.c index bc4ab83..5b05559 100644 --- a/wayland.c +++ b/wayland.c @@ -54,6 +54,7 @@ #include #include #include +#include #include "wlr-layer-shell-protocol.h" #include "common.h" @@ -206,6 +207,14 @@ static const struct wl_registry_listener wl_registry_listener = { +#define NSEC_PER_MSEC (1000 * 1000) + +long int now_milliseconds() { + struct timespec now = {0, 0}; + clock_gettime(CLOCK_MONOTONIC, &now); + return (now.tv_sec * 1000) + (now.tv_nsec / NSEC_PER_MSEC); +} + void wlo_wl_main(struct client_state *state) { state->wl_state.wl_display = wl_display_connect(NULL); if (!state->wl_state.wl_display) { @@ -235,7 +244,31 @@ void wlo_wl_main(struct client_state *state) { wl_surface_commit(state->wl_state.wl_surface); - while (wl_display_dispatch(state->wl_state.wl_display) && state->wl_state.wl_surface != NULL) { - /* This space deliberately left blank */ + if (state->user_request.time >= 0) { + struct pollfd pollfd = { + .fd = wl_display_get_fd(state->wl_state.wl_display), + .events = POLLIN, + .revents = 0 + }; + + long int now = now_milliseconds(); + long int end = now + state->user_request.time; + while (now < end && state->wl_state.wl_surface != NULL) { + wl_display_flush(state->wl_state.wl_display); + int changed_fds = poll(&pollfd, 1, (int)(end - now)); + if (changed_fds == -1 || (pollfd.revents & (POLLERR | POLLHUP))) { + break; + } + if (pollfd.revents & POLLIN) { + if (wl_display_dispatch(state->wl_state.wl_display) == -1) { + state->wl_state.wl_surface = NULL; + } + } + now = now_milliseconds(); + } + } else { + while (wl_display_dispatch(state->wl_state.wl_display) != -1 && state->wl_state.wl_surface != NULL) { + /* This space deliberately left blank */ + } } } diff --git a/wl-overlay.c b/wl-overlay.c index fe081ca..44761b8 100644 --- a/wl-overlay.c +++ b/wl-overlay.c @@ -34,6 +34,7 @@ #define DEFAULT_FONT "Fira Sans 17" #define DEFAULT_BACKDROP_COLOR \ ((struct color_argb){0.85, 0x11 / 255.0, 0x11 / 255.0, 0x11 / 255.0}) +#define DEFAULT_TIME -1 @@ -79,6 +80,7 @@ print_usage() " --width= \n" " --height= Set width and height (default %d×%d)\n" " --padding= Set padding around image and text (default %d)\n" + " --time= Make overlay disappear after this amount of milliseconds, or -1 to never disappear (default %d)\n" "\n" "Copyright information:\n" " This software is © 2022 Midgard, released under GPLv3+\n" @@ -86,7 +88,8 @@ print_usage() " Call wl-overlay --copyright for more information\n", DEFAULT_BORDER_RADIUS, DEFAULT_WIDTH, DEFAULT_HEIGHT, - DEFAULT_PADDING); + DEFAULT_PADDING, + DEFAULT_TIME); } static void @@ -144,6 +147,7 @@ main(int argc, char *argv[]) state.user_request.width = DEFAULT_WIDTH; state.user_request.height = DEFAULT_HEIGHT; state.user_request.padding = DEFAULT_PADDING; + state.user_request.time = DEFAULT_TIME; int c; @@ -160,7 +164,8 @@ main(int argc, char *argv[]) {"width", required_argument, 0, 0 }, {"height", required_argument, 0, 0 }, {"padding", required_argument, 0, 0 }, - {0, 0, 0, 0 } + {"time", required_argument, 0, 0 }, + {0, required_argument, 0, 0 } }; c = getopt_long(argc, argv, "h", long_options, &option_index); @@ -178,6 +183,7 @@ main(int argc, char *argv[]) case 7: state.user_request.width = atoi(optarg); break; case 8: state.user_request.height = atoi(optarg); break; case 9: state.user_request.padding = atoi(optarg); break; + case 10: state.user_request.time = atoi(optarg); break; default: break; } break;