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