Skip to content

Commit

Permalink
use timerfd(2) instead of alarm
Browse files Browse the repository at this point in the history
no more "Alarm clock" message on stderr
  • Loading branch information
notchoc committed Aug 26, 2023
1 parent 4246d8c commit 533afb5
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions dtao.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <string.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <sys/timerfd.h>
#include <time.h>
#include <unistd.h>
#include <wayland-client.h>
Expand Down Expand Up @@ -423,12 +424,22 @@ event_loop(void)
int ret;
int wlfd = wl_display_get_fd(display);

fprintf(stderr, "%d\n", persist);
int tfd = 0;
struct itimerspec ts;
if (persist > 0) {
tfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
ts = (struct itimerspec) {.it_value.tv_sec = persist};
}

while (run_display && !(eof_stdin && persist == 0)) {
fd_set rfds;
FD_ZERO(&rfds);
if (!eof_stdin)
if (!eof_stdin) {
FD_SET(STDIN_FILENO, &rfds);
} else if (persist > 0) {
timerfd_settime(tfd, 0, &ts, NULL);
FD_SET(tfd, &rfds);
}

FD_SET(wlfd, &rfds);

Expand All @@ -439,17 +450,14 @@ event_loop(void)
if (ret < 0)
EBARF("select");

if (!eof_stdin && FD_ISSET(STDIN_FILENO, &rfds))
if (FD_ISSET(STDIN_FILENO, &rfds))
read_stdin();
else if (FD_ISSET(tfd, &rfds))
return;

if (FD_ISSET(wlfd, &rfds))
if (wl_display_dispatch(display) == -1)
break;

if (eof_stdin && persist > 0) {
alarm(persist);
persist = -1;
}
}
}

Expand Down

0 comments on commit 533afb5

Please sign in to comment.