forked from keyd-project/keyd-fork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevloop.cpp
173 lines (140 loc) · 3.52 KB
/
evloop.cpp
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
#include "keyd.h"
static int aux_fd = -1;
// Expected to be initialized as zeros
// Expected to terminate if fd 0 or -1
std::array<device, 128> device_table{};
static void panic_check(uint16_t code, uint8_t pressed)
{
static uint8_t enter, backspace, escape;
switch (code) {
case KEYD_ENTER:
enter = pressed;
break;
case KEYD_BACKSPACE:
backspace = pressed;
break;
case KEYD_ESC:
escape = pressed;
break;
}
if (backspace && enter && escape)
die("panic sequence detected");
}
static int64_t get_time_ms()
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return int64_t(ts.tv_sec) * 1000 + ts.tv_nsec / 1000'000;
}
int evloop(int (*event_handler)(struct event* ev), bool monitor)
{
size_t n_dev;
int timeout = 0;
int monfd;
struct pollfd pfds[device_table.size() + 3]{};
struct event ev{};
monfd = devmon_create();
n_dev = device_scan(device_table);
for (size_t i = 0; i < n_dev; i++) {
ev.type = EV_DEV_ADD;
ev.dev = &device_table[i];
event_handler(&ev);
}
pfds[0].fd = monfd;
pfds[0].events = POLLIN;
pfds[1].fd = aux_fd;
pfds[1].events = POLLIN;
pfds[2].fd = STDOUT_FILENO;
pfds[2].events = 0;
auto pfdsd = pfds + 3;
while (1) {
int removed = 0;
int64_t start_time;
int64_t elapsed;
for (size_t i = 0; i < n_dev; i++) {
pfdsd[i].fd = device_table[i].fd;
pfdsd[i].events = 0;
if (monitor || device_table[i].grabbed)
pfdsd[i].events = POLLIN;
else if (device_table[i].capabilities & CAP_KEYBOARD && device_table[i].is_virtual)
pfdsd[i].events = POLLIN;
}
start_time = get_time_ms();
poll(pfds, n_dev + (pfdsd - pfds), timeout > 0 ? timeout : -1);
ev.timestamp = get_time_ms();
elapsed = ev.timestamp - start_time;
if (pfds[2].revents) {
// Handle pipe closure
break;
}
if (timeout > 0 && elapsed >= timeout) {
ev.type = EV_TIMEOUT;
ev.dev = NULL;
ev.devev = NULL;
timeout = event_handler(&ev);
} else {
timeout -= elapsed;
}
for (size_t i = 0; i < n_dev; i++) {
if (pfdsd[i].revents) {
struct device_event *devev = nullptr;
while ((pfdsd[i].revents & (POLLERR | POLLHUP)) || (devev = device_read_event(&device_table[i]))) {
if (!devev || devev->type == DEV_REMOVED) {
ev.type = EV_DEV_REMOVE;
ev.dev = &device_table[i];
timeout = event_handler(&ev);
close(device_table[i].fd);
device_table[i].fd = -1;
removed = 1;
break;
} else {
//Handle device event
panic_check(devev->code, devev->pressed);
ev.type = EV_DEV_EVENT;
ev.devev = devev;
ev.dev = &device_table[i];
timeout = event_handler(&ev);
}
}
}
}
{
if (auto events = pfds[1].revents) {
ev.type = events & POLLERR ? EV_FD_ERR : EV_FD_ACTIVITY;
ev.fd = aux_fd;
timeout = event_handler(&ev);
}
}
if (pfds[0].revents) {
struct device dev;
while (devmon_read_device(monfd, &dev) == 0) {
if (n_dev >= device_table.size()) {
keyd_log("Too many devices, ignoring.\n");
close(dev.fd);
break;
}
device_table[n_dev] = std::move(dev);
ev.type = EV_DEV_ADD;
ev.dev = &device_table[n_dev];
timeout = event_handler(&ev);
n_dev++;
}
}
if (removed) {
// Maintain contiguous list of devices
auto end = std::remove_if(device_table.begin(), device_table.begin() + n_dev, [](device& dev) {
return dev.fd <= 0;
});
if (end < device_table.begin() + n_dev) {
end->fd = -1;
n_dev = end - device_table.data();
}
}
}
return 0;
}
void evloop_add_fd(int fd)
{
assert(aux_fd < 0);
aux_fd = fd;
}