-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathkeymon.c
More file actions
195 lines (148 loc) · 3.53 KB
/
keymon.c
File metadata and controls
195 lines (148 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <sys/file.h>
#include <sys/ppoll.h>
#include <sys/signal.h>
#include <string.h>
#include <sigset.h>
#include <util.h>
#include <main.h>
#include "keymon.h"
ERRTAG("keymon");
ERRLIST(NEPERM NENOENT NENOTDIR NEACCES NENOTTY NEFAULT NEINVAL NEISDIR
NELIBBAD NELOOP NEMFILE NENFILE NENOEXEC NENOMEM NETXTBSY);
#define PFDS (1 + NDEVICES)
static sigset_t defsigset;
struct pollfd pfds[PFDS];
int npfds, pfdkeys[PFDS];
char** environ;
int pollready;
int sigterm;
typedef struct timespec timespec;
static void sighandler(int sig)
{
switch(sig) {
case SIGINT:
case SIGTERM: sigterm = 1; break;
}
}
void setup_signals(void)
{
SIGHANDLER(sa, sighandler, SA_RESTART);
int ret;
sigemptyset(&sa.mask);
if((ret = sys_sigprocmask(SIG_BLOCK, &sa.mask, &defsigset)) < 0)
fail("sigprocmask", "SIG_BLOCK", ret);
if((ret = sys_sigaction(SIGINT, &sa, NULL)) < 0)
fail("sigaction", "SIGINT", ret);
if((ret = sys_sigaction(SIGTERM, &sa, NULL)) < 0)
fail("sigaction", "SIGTERM", ret);
if((ret = sys_sigaction(SIGALRM, &sa, NULL)) < 0)
fail("sigaction", "SIGALRM", ret);
}
static int add_poll_fd(int n, int fd, int key)
{
if(fd <= 0)
return n;
struct pollfd* pf = &pfds[n];
pf->fd = fd;
pf->events = POLLIN;
pfdkeys[n] = key;
return n + 1;
}
void update_poll_fds(void)
{
int i, n = 0;
n = add_poll_fd(n, inotifyfd, 0);
for(i = 0; i < ndevices; i++)
n = add_poll_fd(n, devices[i].fd, 1 + i);
npfds = n;
pollready = 1;
}
/* Failing fds are dealt with immediately, to avoid re-queueing
them from ppoll. For keyboards, this is also the only place
where the fds get closed. Control fds are closed either here
or in closevt(). */
static void close_device(struct device* kb)
{
sys_close(kb->fd);
memzero(kb, sizeof(kb));
pollready = 0;
}
static void recv_device(struct pollfd* pf, struct device* kb)
{
if(pf->revents & POLLIN)
handle_input(kb, pf->fd);
if(pf->revents & ~POLLIN)
close_device(kb);
}
static void recv_inotify(struct pollfd* pf)
{
if(pf->revents & POLLIN)
handle_inotify(pf->fd);
if(pf->revents & ~POLLIN)
fail("inotify fd gone", NULL, 0);
}
void check_polled_fds(void)
{
int i, key;
for(i = 0; i < npfds; i++)
if((key = pfdkeys[i]) == 0)
recv_inotify(&pfds[i]);
else if(key > 0)
recv_device(&pfds[i], &devices[key-1]);
}
static timespec* prep_poll_time(timespec* ts0, timespec* ts1)
{
struct action* ka;
int timetowait = 0;
for(ka = actions; ka < actions + nactions; ka++)
if(!ka->time)
continue;
else if(!timetowait || ka->time < timetowait)
timetowait = ka->time;
if(timetowait <= 0)
return NULL;
ts0->sec = timetowait / 1000;
ts0->nsec = (timetowait % 1000) * 1000000;
*ts1 = *ts0;
return ts1;
}
static void update_timers(timespec* ts0, timespec* ts1)
{
long wait = ts0->sec*1000 + ts0->nsec/1000000;
long left = ts1->sec*1000 + ts1->nsec/1000000;
long diff = wait - left;
if(diff <= 0)
return;
struct action* ka;
for(ka = actions; ka < actions + nactions; ka++)
if(!ka->time)
continue;
else if(ka->time <= diff)
hold_done(ka);
else
ka->time -= diff;
}
int main(int argc, char** argv)
{
int ret;
timespec ts0, ts1, *pts;
if(argc > 1)
fail("too many arguments", NULL, 0);
environ = argv + argc + 1;
load_config();
setup_signals();
setup_devices();
while(!sigterm) {
if(!pollready)
update_poll_fds();
pts = prep_poll_time(&ts0, &ts1);
ret = sys_ppoll(pfds, npfds, pts, &defsigset);
if(ret < 0 && ret != -EINTR)
fail("ppoll", NULL, ret);
else if(ret > 0)
check_polled_fds();
if(pts)
update_timers(&ts0, &ts1);
}
return 0;
}