use signalfd instead

This commit is contained in:
John Sullivan
2020-08-20 19:30:47 -07:00
parent ba735340dc
commit b236c2fb35
+46 -24
View File
@@ -3,6 +3,7 @@
#include <libinput.h> #include <libinput.h>
#include <math.h> #include <math.h>
#include <signal.h> #include <signal.h>
#include <sys/signalfd.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -230,16 +231,23 @@ touchup(struct libinput_event *e)
} }
} }
void int
registerorientchange() { sigusr1fd() {
struct sigaction action; sigset_t mask;
int sfd;
action.sa_flags = SA_SIGINFO; sigemptyset(&mask);
action.sa_sigaction = &changeorientation; sigaddset(&mask, SIGUSR1);
if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
if(sigaction(SIGUSR1, &action, NULL) == -1) { die("Can't block SIGUSR1.");
die("Couldn't register reorient signal action.");
} }
sfd = signalfd(-1, &mask, 0);
if (sfd == -1) {
die("Can't open signalfd on SIGUSR1.");
}
return sfd;
} }
void void
@@ -251,7 +259,12 @@ run()
struct libinput_event_touch *tevent; struct libinput_event_touch *tevent;
struct libinput_device *d; struct libinput_device *d;
int selectresult; int selectresult;
fd_set fdset; fd_set fdset;
int li_fd;
int sig_fd;
int sfdrsize;
struct signalfd_siginfo sfdinfo;
const static struct libinput_interface interface = { const static struct libinput_interface interface = {
.open_restricted = libinputopenrestricted, .open_restricted = libinputopenrestricted,
@@ -276,25 +289,37 @@ run()
ystart[i] = NOMOTION; ystart[i] = NOMOTION;
} }
FD_ZERO(&fdset); sig_fd = sigusr1fd();
FD_SET(libinput_get_fd(li), &fdset); li_fd = libinput_get_fd(li);
for (;;) { for (;;) {
FD_ZERO(&fdset);
FD_SET(sig_fd, &fdset);
FD_SET(li_fd, &fdset);
selectresult = select(FD_SETSIZE, &fdset, NULL, NULL, NULL); selectresult = select(FD_SETSIZE, &fdset, NULL, NULL, NULL);
if (selectresult == -1) { if (selectresult == -1) {
die("Can't select on device node?"); die("Can't select on device node?");
} else { }
libinput_dispatch(li); else {
while ((event = libinput_get_event(li)) != NULL) { if (FD_ISSET(sig_fd, &fdset)) {
if (orientationdirty) { sfdrsize = read(sig_fd, &sfdinfo, sizeof(struct signalfd_siginfo));
reorientgestures(orientation);
orientationdirty = 0; if (sfdrsize != sizeof(struct signalfd_siginfo)) {
die("Couldn't read reorient signal.");
} }
switch(libinput_event_get_type(event)) { reorientgestures(sfdinfo.ssi_int);
case LIBINPUT_EVENT_TOUCH_DOWN: touchdown(event); break; }
case LIBINPUT_EVENT_TOUCH_UP: touchup(event); break; if (FD_ISSET(li_fd, &fdset)) {
case LIBINPUT_EVENT_TOUCH_MOTION: touchmotion(event); break; libinput_dispatch(li);
while ((event = libinput_get_event(li)) != NULL) {
switch(libinput_event_get_type(event)) {
case LIBINPUT_EVENT_TOUCH_DOWN: touchdown(event); break;
case LIBINPUT_EVENT_TOUCH_UP: touchup(event); break;
case LIBINPUT_EVENT_TOUCH_MOTION: touchmotion(event); break;
}
libinput_event_destroy(event);
} }
libinput_event_destroy(event);
} }
} }
} }
@@ -370,9 +395,6 @@ main(int argc, char *argv[])
// Modify gestures swipes based on orientation provided // Modify gestures swipes based on orientation provided
reorientgestures(orientation); reorientgestures(orientation);
// Change orientation at runtime (via SIGUSR1)
registerorientchange();
run(); run();
return 0; return 0;
} }