Add --time argument to automatically disappear

This commit is contained in:
Midgard 2022-10-20 20:48:59 +02:00
parent ac32252983
commit 8c89495736
Signed by: midgard
GPG key ID: 511C112F1331BBB4
3 changed files with 44 additions and 4 deletions

View file

@ -56,6 +56,7 @@ struct user_request {
int width;
int height;
int padding;
int time;
};
struct wayland_state {

View file

@ -54,6 +54,7 @@
#include <getopt.h>
#include <cairo/cairo.h>
#include <pango/pangocairo.h>
#include <poll.h>
#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 */
}
}
}

View file

@ -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=<int> \n"
" --height=<int> Set width and height (default %d×%d)\n"
" --padding=<int> Set padding around image and text (default %d)\n"
" --time=<int> 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;