]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
e9d21f24 | 2 | |
e9d21f24 LP |
3 | #include <errno.h> |
4 | #include <fcntl.h> | |
5 | #include <sys/ioctl.h> | |
6 | #include <unistd.h> | |
e9d21f24 | 7 | |
cc377381 | 8 | #include "sd-messages.h" |
07630cea | 9 | |
b5efdb8a | 10 | #include "alloc-util.h" |
c74d5fe2 | 11 | #include "async.h" |
3ffd4af2 LP |
12 | #include "fd-util.h" |
13 | #include "logind-button.h" | |
36dd5ffd | 14 | #include "missing_input.h" |
07630cea | 15 | #include "string-util.h" |
e9d21f24 | 16 | |
adbb2b6a | 17 | #define CONST_MAX5(a, b, c, d, e) CONST_MAX(CONST_MAX(a, b), CONST_MAX(CONST_MAX(c, d), e)) |
d5dd44b0 LP |
18 | |
19 | #define ULONG_BITS (sizeof(unsigned long)*8) | |
20 | ||
a520bb66 YA |
21 | #define LONG_PRESS_DURATION (5 * USEC_PER_SEC) |
22 | ||
d5dd44b0 LP |
23 | static bool bitset_get(const unsigned long *bits, unsigned i) { |
24 | return (bits[i / ULONG_BITS] >> (i % ULONG_BITS)) & 1UL; | |
25 | } | |
26 | ||
27 | static void bitset_put(unsigned long *bits, unsigned i) { | |
28 | bits[i / ULONG_BITS] |= (unsigned long) 1 << (i % ULONG_BITS); | |
29 | } | |
30 | ||
e9d21f24 LP |
31 | Button* button_new(Manager *m, const char *name) { |
32 | Button *b; | |
33 | ||
34 | assert(m); | |
35 | assert(name); | |
36 | ||
37 | b = new0(Button, 1); | |
38 | if (!b) | |
39 | return NULL; | |
40 | ||
41 | b->name = strdup(name); | |
6b430fdb ZJS |
42 | if (!b->name) |
43 | return mfree(b); | |
e9d21f24 LP |
44 | |
45 | if (hashmap_put(m->buttons, b->name, b) < 0) { | |
46 | free(b->name); | |
6b430fdb | 47 | return mfree(b); |
e9d21f24 LP |
48 | } |
49 | ||
50 | b->manager = m; | |
254d1313 | 51 | b->fd = -EBADF; |
e9d21f24 LP |
52 | |
53 | return b; | |
54 | } | |
55 | ||
56 | void button_free(Button *b) { | |
57 | assert(b); | |
58 | ||
59 | hashmap_remove(b->manager->buttons, b->name); | |
60 | ||
ed4ba7e4 LP |
61 | sd_event_source_unref(b->io_event_source); |
62 | sd_event_source_unref(b->check_event_source); | |
c30a0c62 | 63 | |
c74d5fe2 | 64 | asynchronous_close(b->fd); |
e9d21f24 LP |
65 | |
66 | free(b->name); | |
67 | free(b->seat); | |
68 | free(b); | |
69 | } | |
70 | ||
71 | int button_set_seat(Button *b, const char *sn) { | |
e9d21f24 | 72 | assert(b); |
e9d21f24 | 73 | |
f2835dd4 | 74 | return free_and_strdup(&b->seat, sn); |
e9d21f24 LP |
75 | } |
76 | ||
3c56cab4 BW |
77 | static void button_lid_switch_handle_action(Manager *manager, bool is_edge) { |
78 | HandleAction handle_action; | |
79 | ||
80 | assert(manager); | |
81 | ||
e25937a3 SF |
82 | /* If we are docked or on external power, handle the lid switch |
83 | * differently */ | |
602a41c2 | 84 | if (manager_is_docked_or_external_displays(manager)) |
3c56cab4 | 85 | handle_action = manager->handle_lid_switch_docked; |
9fa9c66d | 86 | else if (handle_action_valid(manager->handle_lid_switch_ep) && manager_is_on_external_power()) |
e25937a3 | 87 | handle_action = manager->handle_lid_switch_ep; |
3c56cab4 BW |
88 | else |
89 | handle_action = manager->handle_lid_switch; | |
90 | ||
91 | manager_handle_action(manager, INHIBIT_HANDLE_LID_SWITCH, handle_action, manager->lid_switch_ignore_inhibited, is_edge); | |
92 | } | |
93 | ||
ed4ba7e4 | 94 | static int button_recheck(sd_event_source *e, void *userdata) { |
99534007 | 95 | Button *b = ASSERT_PTR(userdata); |
beaafb2e | 96 | |
ed4ba7e4 LP |
97 | assert(b->lid_closed); |
98 | ||
3c56cab4 | 99 | button_lid_switch_handle_action(b->manager, false); |
ed4ba7e4 LP |
100 | return 1; |
101 | } | |
e9d21f24 | 102 | |
ed4ba7e4 LP |
103 | static int button_install_check_event_source(Button *b) { |
104 | int r; | |
e9d21f24 LP |
105 | assert(b); |
106 | ||
ed4ba7e4 | 107 | /* Install a post handler, so that we keep rechecking as long as the lid is closed. */ |
6de0e0e5 | 108 | |
ed4ba7e4 LP |
109 | if (b->check_event_source) |
110 | return 0; | |
111 | ||
112 | r = sd_event_add_post(b->manager->event, &b->check_event_source, button_recheck, b); | |
113 | if (r < 0) | |
114 | return r; | |
115 | ||
116 | return sd_event_source_set_priority(b->check_event_source, SD_EVENT_PRIORITY_IDLE+1); | |
e9d21f24 LP |
117 | } |
118 | ||
a520bb66 | 119 | static int long_press_of_power_key_handler(sd_event_source *e, uint64_t usec, void *userdata) { |
99534007 | 120 | Manager *m = ASSERT_PTR(userdata); |
a520bb66 YA |
121 | |
122 | assert(e); | |
a520bb66 YA |
123 | |
124 | m->power_key_long_press_event_source = sd_event_source_unref(m->power_key_long_press_event_source); | |
125 | ||
126 | log_struct(LOG_INFO, | |
127 | LOG_MESSAGE("Power key pressed long."), | |
128 | "MESSAGE_ID=" SD_MESSAGE_POWER_KEY_LONG_PRESS_STR); | |
129 | ||
130 | manager_handle_action(m, INHIBIT_HANDLE_POWER_KEY, m->handle_power_key_long_press, m->power_key_ignore_inhibited, true); | |
131 | return 0; | |
132 | } | |
133 | ||
134 | static int long_press_of_reboot_key_handler(sd_event_source *e, uint64_t usec, void *userdata) { | |
99534007 | 135 | Manager *m = ASSERT_PTR(userdata); |
a520bb66 YA |
136 | |
137 | assert(e); | |
a520bb66 YA |
138 | |
139 | m->reboot_key_long_press_event_source = sd_event_source_unref(m->reboot_key_long_press_event_source); | |
140 | ||
141 | log_struct(LOG_INFO, | |
142 | LOG_MESSAGE("Reboot key pressed long."), | |
143 | "MESSAGE_ID=" SD_MESSAGE_REBOOT_KEY_LONG_PRESS_STR); | |
144 | ||
145 | manager_handle_action(m, INHIBIT_HANDLE_REBOOT_KEY, m->handle_reboot_key_long_press, m->reboot_key_ignore_inhibited, true); | |
146 | return 0; | |
147 | } | |
148 | ||
149 | static int long_press_of_suspend_key_handler(sd_event_source *e, uint64_t usec, void *userdata) { | |
99534007 | 150 | Manager *m = ASSERT_PTR(userdata); |
a520bb66 YA |
151 | |
152 | assert(e); | |
a520bb66 YA |
153 | |
154 | m->suspend_key_long_press_event_source = sd_event_source_unref(m->suspend_key_long_press_event_source); | |
155 | ||
156 | log_struct(LOG_INFO, | |
157 | LOG_MESSAGE("Suspend key pressed long."), | |
158 | "MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_LONG_PRESS_STR); | |
159 | ||
160 | manager_handle_action(m, INHIBIT_HANDLE_SUSPEND_KEY, m->handle_suspend_key_long_press, m->suspend_key_ignore_inhibited, true); | |
161 | return 0; | |
162 | } | |
163 | ||
164 | static int long_press_of_hibernate_key_handler(sd_event_source *e, uint64_t usec, void *userdata) { | |
99534007 | 165 | Manager *m = ASSERT_PTR(userdata); |
a520bb66 YA |
166 | |
167 | assert(e); | |
a520bb66 YA |
168 | |
169 | m->hibernate_key_long_press_event_source = sd_event_source_unref(m->hibernate_key_long_press_event_source); | |
170 | ||
171 | log_struct(LOG_INFO, | |
172 | LOG_MESSAGE("Hibernate key pressed long."), | |
173 | "MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_LONG_PRESS_STR); | |
174 | ||
175 | manager_handle_action(m, INHIBIT_HANDLE_HIBERNATE_KEY, m->handle_hibernate_key_long_press, m->hibernate_key_ignore_inhibited, true); | |
176 | return 0; | |
177 | } | |
178 | ||
179 | static void start_long_press(Manager *m, sd_event_source **e, sd_event_time_handler_t callback) { | |
180 | int r; | |
181 | ||
182 | assert(m); | |
183 | assert(e); | |
184 | ||
185 | if (*e) | |
186 | return; | |
187 | ||
188 | r = sd_event_add_time_relative( | |
189 | m->event, | |
190 | e, | |
191 | CLOCK_MONOTONIC, | |
192 | LONG_PRESS_DURATION, 0, | |
193 | callback, m); | |
194 | if (r < 0) | |
195 | log_warning_errno(r, "Failed to add long press timer event, ignoring: %m"); | |
196 | } | |
197 | ||
cc377381 | 198 | static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
99534007 | 199 | Button *b = ASSERT_PTR(userdata); |
e9d21f24 LP |
200 | struct input_event ev; |
201 | ssize_t l; | |
202 | ||
cc377381 LP |
203 | assert(s); |
204 | assert(fd == b->fd); | |
e9d21f24 LP |
205 | |
206 | l = read(b->fd, &ev, sizeof(ev)); | |
207 | if (l < 0) | |
208 | return errno != EAGAIN ? -errno : 0; | |
209 | if ((size_t) l < sizeof(ev)) | |
210 | return -EIO; | |
211 | ||
e9d21f24 LP |
212 | if (ev.type == EV_KEY && ev.value > 0) { |
213 | ||
214 | switch (ev.code) { | |
215 | ||
216 | case KEY_POWER: | |
217 | case KEY_POWER2: | |
a520bb66 YA |
218 | if (b->manager->handle_power_key_long_press != HANDLE_IGNORE && b->manager->handle_power_key_long_press != b->manager->handle_power_key) { |
219 | log_debug("Power key pressed. Further action depends on the key press duration."); | |
220 | start_long_press(b->manager, &b->manager->power_key_long_press_event_source, long_press_of_power_key_handler); | |
221 | } else { | |
92663a5e ZJS |
222 | log_struct(LOG_INFO, |
223 | LOG_MESSAGE("Power key pressed short."), | |
a520bb66 YA |
224 | "MESSAGE_ID=" SD_MESSAGE_POWER_KEY_STR); |
225 | manager_handle_action(b->manager, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true); | |
226 | } | |
7b77ed8c | 227 | break; |
e9d21f24 | 228 | |
adbb2b6a RM |
229 | /* The kernel naming is a bit confusing here: |
230 | KEY_RESTART was probably introduced for media playback purposes, but | |
231 | is now being predominantly used to indicate device reboot. | |
232 | */ | |
233 | ||
234 | case KEY_RESTART: | |
a520bb66 YA |
235 | if (b->manager->handle_reboot_key_long_press != HANDLE_IGNORE && b->manager->handle_reboot_key_long_press != b->manager->handle_reboot_key) { |
236 | log_debug("Reboot key pressed. Further action depends on the key press duration."); | |
237 | start_long_press(b->manager, &b->manager->reboot_key_long_press_event_source, long_press_of_reboot_key_handler); | |
238 | } else { | |
92663a5e ZJS |
239 | log_struct(LOG_INFO, |
240 | LOG_MESSAGE("Reboot key pressed short."), | |
a520bb66 YA |
241 | "MESSAGE_ID=" SD_MESSAGE_REBOOT_KEY_STR); |
242 | manager_handle_action(b->manager, INHIBIT_HANDLE_REBOOT_KEY, b->manager->handle_reboot_key, b->manager->reboot_key_ignore_inhibited, true); | |
243 | } | |
adbb2b6a RM |
244 | break; |
245 | ||
246 | /* The kernel naming is a bit confusing here: | |
8e7fd6ad LP |
247 | |
248 | KEY_SLEEP = suspend-to-ram, which everybody else calls "suspend" | |
249 | KEY_SUSPEND = suspend-to-disk, which everybody else calls "hibernate" | |
250 | */ | |
251 | ||
e9d21f24 | 252 | case KEY_SLEEP: |
a520bb66 YA |
253 | if (b->manager->handle_suspend_key_long_press != HANDLE_IGNORE && b->manager->handle_suspend_key_long_press != b->manager->handle_suspend_key) { |
254 | log_debug("Suspend key pressed. Further action depends on the key press duration."); | |
255 | start_long_press(b->manager, &b->manager->suspend_key_long_press_event_source, long_press_of_suspend_key_handler); | |
256 | } else { | |
92663a5e ZJS |
257 | log_struct(LOG_INFO, |
258 | LOG_MESSAGE("Suspend key pressed short."), | |
a520bb66 YA |
259 | "MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_STR); |
260 | manager_handle_action(b->manager, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true); | |
261 | } | |
7b77ed8c | 262 | break; |
e9d21f24 | 263 | |
8e7fd6ad | 264 | case KEY_SUSPEND: |
a520bb66 YA |
265 | if (b->manager->handle_hibernate_key_long_press != HANDLE_IGNORE && b->manager->handle_hibernate_key_long_press != b->manager->handle_hibernate_key) { |
266 | log_debug("Hibernate key pressed. Further action depends on the key press duration."); | |
267 | start_long_press(b->manager, &b->manager->hibernate_key_long_press_event_source, long_press_of_hibernate_key_handler); | |
268 | } else { | |
92663a5e ZJS |
269 | log_struct(LOG_INFO, |
270 | LOG_MESSAGE("Hibernate key pressed short."), | |
a520bb66 YA |
271 | "MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_STR); |
272 | manager_handle_action(b->manager, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true); | |
273 | } | |
7b77ed8c | 274 | break; |
e9d21f24 | 275 | } |
8e7fd6ad | 276 | |
a520bb66 | 277 | } else if (ev.type == EV_KEY && ev.value == 0) { |
caf8c6ea YA |
278 | |
279 | switch (ev.code) { | |
280 | ||
281 | case KEY_POWER: | |
282 | case KEY_POWER2: | |
283 | if (b->manager->power_key_long_press_event_source) { | |
a520bb66 YA |
284 | /* Long press event timer is still pending and key release |
285 | event happened. This means that key press duration was | |
286 | insufficient to trigger a long press event | |
287 | */ | |
caf8c6ea YA |
288 | log_struct(LOG_INFO, |
289 | LOG_MESSAGE("Power key pressed short."), | |
290 | "MESSAGE_ID=" SD_MESSAGE_POWER_KEY_STR); | |
291 | ||
292 | b->manager->power_key_long_press_event_source = sd_event_source_unref(b->manager->power_key_long_press_event_source); | |
293 | ||
294 | manager_handle_action(b->manager, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true); | |
295 | } | |
296 | break; | |
297 | ||
298 | case KEY_RESTART: | |
299 | if (b->manager->reboot_key_long_press_event_source) { | |
a520bb66 YA |
300 | log_struct(LOG_INFO, |
301 | LOG_MESSAGE("Reboot key pressed short."), | |
302 | "MESSAGE_ID=" SD_MESSAGE_REBOOT_KEY_STR); | |
303 | ||
304 | b->manager->reboot_key_long_press_event_source = sd_event_source_unref(b->manager->reboot_key_long_press_event_source); | |
305 | ||
306 | manager_handle_action(b->manager, INHIBIT_HANDLE_REBOOT_KEY, b->manager->handle_reboot_key, b->manager->reboot_key_ignore_inhibited, true); | |
307 | } | |
caf8c6ea YA |
308 | break; |
309 | ||
310 | case KEY_SLEEP: | |
311 | if (b->manager->suspend_key_long_press_event_source) { | |
312 | log_struct(LOG_INFO, | |
313 | LOG_MESSAGE("Suspend key pressed short."), | |
314 | "MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_STR); | |
315 | ||
316 | b->manager->suspend_key_long_press_event_source = sd_event_source_unref(b->manager->suspend_key_long_press_event_source); | |
317 | ||
318 | manager_handle_action(b->manager, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true); | |
319 | } | |
320 | break; | |
321 | case KEY_SUSPEND: | |
322 | if (b->manager->hibernate_key_long_press_event_source) { | |
323 | log_struct(LOG_INFO, | |
324 | LOG_MESSAGE("Hibernate key pressed short."), | |
325 | "MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_STR); | |
326 | ||
327 | b->manager->hibernate_key_long_press_event_source = sd_event_source_unref(b->manager->hibernate_key_long_press_event_source); | |
328 | ||
329 | manager_handle_action(b->manager, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true); | |
330 | } | |
331 | break; | |
a520bb66 YA |
332 | } |
333 | ||
e9d21f24 LP |
334 | } else if (ev.type == EV_SW && ev.value > 0) { |
335 | ||
7b77ed8c | 336 | if (ev.code == SW_LID) { |
c485437f | 337 | log_struct(LOG_INFO, |
e2cc6eca | 338 | LOG_MESSAGE("Lid closed."), |
a1230ff9 | 339 | "MESSAGE_ID=" SD_MESSAGE_LID_CLOSED_STR); |
65b51162 | 340 | |
ed4ba7e4 | 341 | b->lid_closed = true; |
3c56cab4 | 342 | button_lid_switch_handle_action(b->manager, true); |
ed4ba7e4 | 343 | button_install_check_event_source(b); |
2d62c530 LP |
344 | |
345 | } else if (ev.code == SW_DOCK) { | |
346 | log_struct(LOG_INFO, | |
e2cc6eca | 347 | LOG_MESSAGE("System docked."), |
a1230ff9 | 348 | "MESSAGE_ID=" SD_MESSAGE_SYSTEM_DOCKED_STR); |
2d62c530 LP |
349 | |
350 | b->docked = true; | |
65b51162 LP |
351 | } |
352 | ||
353 | } else if (ev.type == EV_SW && ev.value == 0) { | |
354 | ||
7b77ed8c | 355 | if (ev.code == SW_LID) { |
c485437f | 356 | log_struct(LOG_INFO, |
e2cc6eca | 357 | LOG_MESSAGE("Lid opened."), |
a1230ff9 | 358 | "MESSAGE_ID=" SD_MESSAGE_LID_OPENED_STR); |
7b77ed8c | 359 | |
ed4ba7e4 LP |
360 | b->lid_closed = false; |
361 | b->check_event_source = sd_event_source_unref(b->check_event_source); | |
2d62c530 LP |
362 | |
363 | } else if (ev.code == SW_DOCK) { | |
364 | log_struct(LOG_INFO, | |
e2cc6eca | 365 | LOG_MESSAGE("System undocked."), |
a1230ff9 | 366 | "MESSAGE_ID=" SD_MESSAGE_SYSTEM_UNDOCKED_STR); |
2d62c530 LP |
367 | |
368 | b->docked = false; | |
e9d21f24 LP |
369 | } |
370 | } | |
371 | ||
372 | return 0; | |
373 | } | |
374 | ||
92c60579 | 375 | static int button_suitable(int fd) { |
2546b70a LP |
376 | unsigned long types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1]; |
377 | ||
92c60579 | 378 | assert(fd >= 0); |
2546b70a | 379 | |
92c60579 | 380 | if (ioctl(fd, EVIOCGBIT(EV_SYN, sizeof types), types) < 0) |
2546b70a LP |
381 | return -errno; |
382 | ||
383 | if (bitset_get(types, EV_KEY)) { | |
adbb2b6a | 384 | unsigned long keys[CONST_MAX5(KEY_POWER, KEY_POWER2, KEY_SLEEP, KEY_SUSPEND, KEY_RESTART)/ULONG_BITS+1]; |
2546b70a | 385 | |
92c60579 | 386 | if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof keys), keys) < 0) |
2546b70a LP |
387 | return -errno; |
388 | ||
389 | if (bitset_get(keys, KEY_POWER) || | |
390 | bitset_get(keys, KEY_POWER2) || | |
391 | bitset_get(keys, KEY_SLEEP) || | |
adbb2b6a RM |
392 | bitset_get(keys, KEY_SUSPEND) || |
393 | bitset_get(keys, KEY_RESTART)) | |
2546b70a LP |
394 | return true; |
395 | } | |
396 | ||
397 | if (bitset_get(types, EV_SW)) { | |
398 | unsigned long switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1]; | |
399 | ||
92c60579 | 400 | if (ioctl(fd, EVIOCGBIT(EV_SW, sizeof switches), switches) < 0) |
2546b70a LP |
401 | return -errno; |
402 | ||
403 | if (bitset_get(switches, SW_LID) || | |
404 | bitset_get(switches, SW_DOCK)) | |
405 | return true; | |
406 | } | |
407 | ||
408 | return false; | |
409 | } | |
410 | ||
92c60579 | 411 | static int button_set_mask(const char *name, int fd) { |
d5dd44b0 LP |
412 | unsigned long |
413 | types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1] = {}, | |
adbb2b6a | 414 | keys[CONST_MAX5(KEY_POWER, KEY_POWER2, KEY_SLEEP, KEY_SUSPEND, KEY_RESTART)/ULONG_BITS+1] = {}, |
d5dd44b0 LP |
415 | switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1] = {}; |
416 | struct input_mask mask; | |
417 | ||
92c60579 ZJS |
418 | assert(name); |
419 | assert(fd >= 0); | |
d5dd44b0 LP |
420 | |
421 | bitset_put(types, EV_KEY); | |
422 | bitset_put(types, EV_SW); | |
423 | ||
424 | mask = (struct input_mask) { | |
425 | .type = EV_SYN, | |
426 | .codes_size = sizeof(types), | |
427 | .codes_ptr = PTR_TO_UINT64(types), | |
428 | }; | |
429 | ||
92c60579 | 430 | if (ioctl(fd, EVIOCSMASK, &mask) < 0) |
d5dd44b0 LP |
431 | /* Log only at debug level if the kernel doesn't do EVIOCSMASK yet */ |
432 | return log_full_errno(IN_SET(errno, ENOTTY, EOPNOTSUPP, EINVAL) ? LOG_DEBUG : LOG_WARNING, | |
92c60579 | 433 | errno, "Failed to set EV_SYN event mask on /dev/input/%s: %m", name); |
d5dd44b0 LP |
434 | |
435 | bitset_put(keys, KEY_POWER); | |
436 | bitset_put(keys, KEY_POWER2); | |
437 | bitset_put(keys, KEY_SLEEP); | |
438 | bitset_put(keys, KEY_SUSPEND); | |
adbb2b6a | 439 | bitset_put(keys, KEY_RESTART); |
d5dd44b0 LP |
440 | |
441 | mask = (struct input_mask) { | |
442 | .type = EV_KEY, | |
443 | .codes_size = sizeof(keys), | |
444 | .codes_ptr = PTR_TO_UINT64(keys), | |
445 | }; | |
446 | ||
92c60579 ZJS |
447 | if (ioctl(fd, EVIOCSMASK, &mask) < 0) |
448 | return log_warning_errno(errno, "Failed to set EV_KEY event mask on /dev/input/%s: %m", name); | |
d5dd44b0 LP |
449 | |
450 | bitset_put(switches, SW_LID); | |
451 | bitset_put(switches, SW_DOCK); | |
452 | ||
453 | mask = (struct input_mask) { | |
454 | .type = EV_SW, | |
455 | .codes_size = sizeof(switches), | |
456 | .codes_ptr = PTR_TO_UINT64(switches), | |
457 | }; | |
458 | ||
92c60579 ZJS |
459 | if (ioctl(fd, EVIOCSMASK, &mask) < 0) |
460 | return log_warning_errno(errno, "Failed to set EV_SW event mask on /dev/input/%s: %m", name); | |
d5dd44b0 LP |
461 | |
462 | return 0; | |
463 | } | |
464 | ||
cc377381 | 465 | int button_open(Button *b) { |
254d1313 | 466 | _cleanup_(asynchronous_closep) int fd = -EBADF; |
92c60579 ZJS |
467 | const char *p; |
468 | char name[256]; | |
cc377381 LP |
469 | int r; |
470 | ||
471 | assert(b); | |
472 | ||
c74d5fe2 | 473 | b->fd = asynchronous_close(b->fd); |
cc377381 | 474 | |
63c372cb | 475 | p = strjoina("/dev/input/", b->name); |
cc377381 | 476 | |
92c60579 ZJS |
477 | fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); |
478 | if (fd < 0) | |
2546b70a LP |
479 | return log_warning_errno(errno, "Failed to open %s: %m", p); |
480 | ||
92c60579 | 481 | r = button_suitable(fd); |
2546b70a | 482 | if (r < 0) |
92c60579 | 483 | return log_warning_errno(r, "Failed to determine whether input device %s is relevant to us: %m", p); |
c74d5fe2 | 484 | if (r == 0) |
baaa35ad | 485 | return log_debug_errno(SYNTHETIC_ERRNO(EADDRNOTAVAIL), |
92c60579 | 486 | "Device %s does not expose keys or switches relevant to us, ignoring.", p); |
c74d5fe2 | 487 | |
92c60579 ZJS |
488 | if (ioctl(fd, EVIOCGNAME(sizeof name), name) < 0) |
489 | return log_error_errno(errno, "Failed to get input name for %s: %m", p); | |
cc377381 | 490 | |
92c60579 | 491 | (void) button_set_mask(b->name, fd); |
b518e026 | 492 | |
b2774a3a | 493 | b->io_event_source = sd_event_source_unref(b->io_event_source); |
12e98242 | 494 | r = sd_event_add_io(b->manager->event, &b->io_event_source, fd, EPOLLIN, button_dispatch, b); |
92c60579 ZJS |
495 | if (r < 0) |
496 | return log_error_errno(r, "Failed to add button event for %s: %m", p); | |
cc377381 | 497 | |
92c60579 ZJS |
498 | b->fd = TAKE_FD(fd); |
499 | log_info("Watching system buttons on %s (%s)", p, name); | |
cc377381 | 500 | return 0; |
cc377381 LP |
501 | } |
502 | ||
2d62c530 | 503 | int button_check_switches(Button *b) { |
d5dd44b0 | 504 | unsigned long switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1] = {}; |
65b51162 LP |
505 | assert(b); |
506 | ||
ed4ba7e4 LP |
507 | if (b->fd < 0) |
508 | return -EINVAL; | |
509 | ||
510 | if (ioctl(b->fd, EVIOCGSW(sizeof(switches)), switches) < 0) | |
511 | return -errno; | |
65b51162 | 512 | |
d5dd44b0 LP |
513 | b->lid_closed = bitset_get(switches, SW_LID); |
514 | b->docked = bitset_get(switches, SW_DOCK); | |
ed4ba7e4 | 515 | |
2d62c530 | 516 | if (b->lid_closed) |
ed4ba7e4 | 517 | button_install_check_event_source(b); |
ed4ba7e4 LP |
518 | |
519 | return 0; | |
65b51162 | 520 | } |