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