1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "sd-messages.h"
10 #include "alloc-util.h"
13 #include "logind-button.h"
14 #include "missing_input.h"
15 #include "string-util.h"
18 #define CONST_MAX5(a, b, c, d, e) CONST_MAX(CONST_MAX(a, b), CONST_MAX(CONST_MAX(c, d), e))
20 #define ULONG_BITS (sizeof(unsigned long)*8)
22 #define LONG_PRESS_DURATION (5 * USEC_PER_SEC)
24 static bool bitset_get(const unsigned long *bits
, unsigned i
) {
25 return (bits
[i
/ ULONG_BITS
] >> (i
% ULONG_BITS
)) & 1UL;
28 static void bitset_put(unsigned long *bits
, unsigned i
) {
29 bits
[i
/ ULONG_BITS
] |= (unsigned long) 1 << (i
% ULONG_BITS
);
32 Button
* button_new(Manager
*m
, const char *name
) {
42 b
->name
= strdup(name
);
46 if (hashmap_put(m
->buttons
, b
->name
, b
) < 0) {
57 void button_free(Button
*b
) {
60 hashmap_remove(b
->manager
->buttons
, b
->name
);
62 sd_event_source_unref(b
->io_event_source
);
63 sd_event_source_unref(b
->check_event_source
);
65 asynchronous_close(b
->fd
);
72 int button_set_seat(Button
*b
, const char *sn
) {
75 return free_and_strdup(&b
->seat
, sn
);
78 static void button_lid_switch_handle_action(Manager
*manager
, bool is_edge
) {
79 HandleAction handle_action
;
83 /* If we are docked or on external power, handle the lid switch
85 if (manager_is_docked_or_external_displays(manager
))
86 handle_action
= manager
->handle_lid_switch_docked
;
87 else if (handle_action_valid(manager
->handle_lid_switch_ep
) && manager_is_on_external_power())
88 handle_action
= manager
->handle_lid_switch_ep
;
90 handle_action
= manager
->handle_lid_switch
;
92 manager_handle_action(manager
, INHIBIT_HANDLE_LID_SWITCH
, handle_action
, manager
->lid_switch_ignore_inhibited
, is_edge
);
95 static int button_recheck(sd_event_source
*e
, void *userdata
) {
99 assert(b
->lid_closed
);
101 button_lid_switch_handle_action(b
->manager
, false);
105 static int button_install_check_event_source(Button
*b
) {
109 /* Install a post handler, so that we keep rechecking as long as the lid is closed. */
111 if (b
->check_event_source
)
114 r
= sd_event_add_post(b
->manager
->event
, &b
->check_event_source
, button_recheck
, b
);
118 return sd_event_source_set_priority(b
->check_event_source
, SD_EVENT_PRIORITY_IDLE
+1);
121 static int long_press_of_power_key_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
122 Manager
*m
= userdata
;
127 m
->power_key_long_press_event_source
= sd_event_source_unref(m
->power_key_long_press_event_source
);
130 LOG_MESSAGE("Power key pressed long."),
131 "MESSAGE_ID=" SD_MESSAGE_POWER_KEY_LONG_PRESS_STR
);
133 manager_handle_action(m
, INHIBIT_HANDLE_POWER_KEY
, m
->handle_power_key_long_press
, m
->power_key_ignore_inhibited
, true);
137 static int long_press_of_reboot_key_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
138 Manager
*m
= userdata
;
143 m
->reboot_key_long_press_event_source
= sd_event_source_unref(m
->reboot_key_long_press_event_source
);
146 LOG_MESSAGE("Reboot key pressed long."),
147 "MESSAGE_ID=" SD_MESSAGE_REBOOT_KEY_LONG_PRESS_STR
);
149 manager_handle_action(m
, INHIBIT_HANDLE_REBOOT_KEY
, m
->handle_reboot_key_long_press
, m
->reboot_key_ignore_inhibited
, true);
153 static int long_press_of_suspend_key_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
154 Manager
*m
= userdata
;
159 m
->suspend_key_long_press_event_source
= sd_event_source_unref(m
->suspend_key_long_press_event_source
);
162 LOG_MESSAGE("Suspend key pressed long."),
163 "MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_LONG_PRESS_STR
);
165 manager_handle_action(m
, INHIBIT_HANDLE_SUSPEND_KEY
, m
->handle_suspend_key_long_press
, m
->suspend_key_ignore_inhibited
, true);
169 static int long_press_of_hibernate_key_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
170 Manager
*m
= userdata
;
175 m
->hibernate_key_long_press_event_source
= sd_event_source_unref(m
->hibernate_key_long_press_event_source
);
178 LOG_MESSAGE("Hibernate key pressed long."),
179 "MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_LONG_PRESS_STR
);
181 manager_handle_action(m
, INHIBIT_HANDLE_HIBERNATE_KEY
, m
->handle_hibernate_key_long_press
, m
->hibernate_key_ignore_inhibited
, true);
185 static void start_long_press(Manager
*m
, sd_event_source
**e
, sd_event_time_handler_t callback
) {
194 r
= sd_event_add_time_relative(
198 LONG_PRESS_DURATION
, 0,
201 log_warning_errno(r
, "Failed to add long press timer event, ignoring: %m");
204 static int button_dispatch(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
205 Button
*b
= userdata
;
206 struct input_event ev
;
213 l
= read(b
->fd
, &ev
, sizeof(ev
));
215 return errno
!= EAGAIN
? -errno
: 0;
216 if ((size_t) l
< sizeof(ev
))
219 if (ev
.type
== EV_KEY
&& ev
.value
> 0) {
225 if (b
->manager
->handle_power_key_long_press
!= HANDLE_IGNORE
&& b
->manager
->handle_power_key_long_press
!= b
->manager
->handle_power_key
) {
226 log_debug("Power key pressed. Further action depends on the key press duration.");
227 start_long_press(b
->manager
, &b
->manager
->power_key_long_press_event_source
, long_press_of_power_key_handler
);
230 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);
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.
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
);
247 LOG_MESSAGE("Reboot key pressed short."),
248 "MESSAGE_ID=" SD_MESSAGE_REBOOT_KEY_STR
);
249 manager_handle_action(b
->manager
, INHIBIT_HANDLE_REBOOT_KEY
, b
->manager
->handle_reboot_key
, b
->manager
->reboot_key_ignore_inhibited
, true);
253 /* The kernel naming is a bit confusing here:
255 KEY_SLEEP = suspend-to-ram, which everybody else calls "suspend"
256 KEY_SUSPEND = suspend-to-disk, which everybody else calls "hibernate"
260 if (b
->manager
->handle_suspend_key_long_press
!= HANDLE_IGNORE
&& b
->manager
->handle_suspend_key_long_press
!= b
->manager
->handle_suspend_key
) {
261 log_debug("Suspend key pressed. Further action depends on the key press duration.");
262 start_long_press(b
->manager
, &b
->manager
->suspend_key_long_press_event_source
, long_press_of_suspend_key_handler
);
265 LOG_MESSAGE("Suspend key pressed short."),
266 "MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_STR
);
267 manager_handle_action(b
->manager
, INHIBIT_HANDLE_SUSPEND_KEY
, b
->manager
->handle_suspend_key
, b
->manager
->suspend_key_ignore_inhibited
, true);
272 if (b
->manager
->handle_hibernate_key_long_press
!= HANDLE_IGNORE
&& b
->manager
->handle_hibernate_key_long_press
!= b
->manager
->handle_hibernate_key
) {
273 log_debug("Hibernate key pressed. Further action depends on the key press duration.");
274 start_long_press(b
->manager
, &b
->manager
->hibernate_key_long_press_event_source
, long_press_of_hibernate_key_handler
);
277 LOG_MESSAGE("Hibernate key pressed short."),
278 "MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_STR
);
279 manager_handle_action(b
->manager
, INHIBIT_HANDLE_HIBERNATE_KEY
, b
->manager
->handle_hibernate_key
, b
->manager
->hibernate_key_ignore_inhibited
, true);
284 } else if (ev
.type
== EV_KEY
&& ev
.value
== 0) {
290 if (b
->manager
->power_key_long_press_event_source
) {
291 /* Long press event timer is still pending and key release
292 event happened. This means that key press duration was
293 insufficient to trigger a long press event
296 LOG_MESSAGE("Power key pressed short."),
297 "MESSAGE_ID=" SD_MESSAGE_POWER_KEY_STR
);
299 b
->manager
->power_key_long_press_event_source
= sd_event_source_unref(b
->manager
->power_key_long_press_event_source
);
301 manager_handle_action(b
->manager
, INHIBIT_HANDLE_POWER_KEY
, b
->manager
->handle_power_key
, b
->manager
->power_key_ignore_inhibited
, true);
306 if (b
->manager
->reboot_key_long_press_event_source
) {
308 LOG_MESSAGE("Reboot key pressed short."),
309 "MESSAGE_ID=" SD_MESSAGE_REBOOT_KEY_STR
);
311 b
->manager
->reboot_key_long_press_event_source
= sd_event_source_unref(b
->manager
->reboot_key_long_press_event_source
);
313 manager_handle_action(b
->manager
, INHIBIT_HANDLE_REBOOT_KEY
, b
->manager
->handle_reboot_key
, b
->manager
->reboot_key_ignore_inhibited
, true);
318 if (b
->manager
->suspend_key_long_press_event_source
) {
320 LOG_MESSAGE("Suspend key pressed short."),
321 "MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_STR
);
323 b
->manager
->suspend_key_long_press_event_source
= sd_event_source_unref(b
->manager
->suspend_key_long_press_event_source
);
325 manager_handle_action(b
->manager
, INHIBIT_HANDLE_SUSPEND_KEY
, b
->manager
->handle_suspend_key
, b
->manager
->suspend_key_ignore_inhibited
, true);
329 if (b
->manager
->hibernate_key_long_press_event_source
) {
331 LOG_MESSAGE("Hibernate key pressed short."),
332 "MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_STR
);
334 b
->manager
->hibernate_key_long_press_event_source
= sd_event_source_unref(b
->manager
->hibernate_key_long_press_event_source
);
336 manager_handle_action(b
->manager
, INHIBIT_HANDLE_HIBERNATE_KEY
, b
->manager
->handle_hibernate_key
, b
->manager
->hibernate_key_ignore_inhibited
, true);
341 } else if (ev
.type
== EV_SW
&& ev
.value
> 0) {
343 if (ev
.code
== SW_LID
) {
345 LOG_MESSAGE("Lid closed."),
346 "MESSAGE_ID=" SD_MESSAGE_LID_CLOSED_STR
);
348 b
->lid_closed
= true;
349 button_lid_switch_handle_action(b
->manager
, true);
350 button_install_check_event_source(b
);
352 } else if (ev
.code
== SW_DOCK
) {
354 LOG_MESSAGE("System docked."),
355 "MESSAGE_ID=" SD_MESSAGE_SYSTEM_DOCKED_STR
);
360 } else if (ev
.type
== EV_SW
&& ev
.value
== 0) {
362 if (ev
.code
== SW_LID
) {
364 LOG_MESSAGE("Lid opened."),
365 "MESSAGE_ID=" SD_MESSAGE_LID_OPENED_STR
);
367 b
->lid_closed
= false;
368 b
->check_event_source
= sd_event_source_unref(b
->check_event_source
);
370 } else if (ev
.code
== SW_DOCK
) {
372 LOG_MESSAGE("System undocked."),
373 "MESSAGE_ID=" SD_MESSAGE_SYSTEM_UNDOCKED_STR
);
382 static int button_suitable(int fd
) {
383 unsigned long types
[CONST_MAX(EV_KEY
, EV_SW
)/ULONG_BITS
+1];
387 if (ioctl(fd
, EVIOCGBIT(EV_SYN
, sizeof types
), types
) < 0)
390 if (bitset_get(types
, EV_KEY
)) {
391 unsigned long keys
[CONST_MAX5(KEY_POWER
, KEY_POWER2
, KEY_SLEEP
, KEY_SUSPEND
, KEY_RESTART
)/ULONG_BITS
+1];
393 if (ioctl(fd
, EVIOCGBIT(EV_KEY
, sizeof keys
), keys
) < 0)
396 if (bitset_get(keys
, KEY_POWER
) ||
397 bitset_get(keys
, KEY_POWER2
) ||
398 bitset_get(keys
, KEY_SLEEP
) ||
399 bitset_get(keys
, KEY_SUSPEND
) ||
400 bitset_get(keys
, KEY_RESTART
))
404 if (bitset_get(types
, EV_SW
)) {
405 unsigned long switches
[CONST_MAX(SW_LID
, SW_DOCK
)/ULONG_BITS
+1];
407 if (ioctl(fd
, EVIOCGBIT(EV_SW
, sizeof switches
), switches
) < 0)
410 if (bitset_get(switches
, SW_LID
) ||
411 bitset_get(switches
, SW_DOCK
))
418 static int button_set_mask(const char *name
, int fd
) {
420 types
[CONST_MAX(EV_KEY
, EV_SW
)/ULONG_BITS
+1] = {},
421 keys
[CONST_MAX5(KEY_POWER
, KEY_POWER2
, KEY_SLEEP
, KEY_SUSPEND
, KEY_RESTART
)/ULONG_BITS
+1] = {},
422 switches
[CONST_MAX(SW_LID
, SW_DOCK
)/ULONG_BITS
+1] = {};
423 struct input_mask mask
;
428 bitset_put(types
, EV_KEY
);
429 bitset_put(types
, EV_SW
);
431 mask
= (struct input_mask
) {
433 .codes_size
= sizeof(types
),
434 .codes_ptr
= PTR_TO_UINT64(types
),
437 if (ioctl(fd
, EVIOCSMASK
, &mask
) < 0)
438 /* Log only at debug level if the kernel doesn't do EVIOCSMASK yet */
439 return log_full_errno(IN_SET(errno
, ENOTTY
, EOPNOTSUPP
, EINVAL
) ? LOG_DEBUG
: LOG_WARNING
,
440 errno
, "Failed to set EV_SYN event mask on /dev/input/%s: %m", name
);
442 bitset_put(keys
, KEY_POWER
);
443 bitset_put(keys
, KEY_POWER2
);
444 bitset_put(keys
, KEY_SLEEP
);
445 bitset_put(keys
, KEY_SUSPEND
);
446 bitset_put(keys
, KEY_RESTART
);
448 mask
= (struct input_mask
) {
450 .codes_size
= sizeof(keys
),
451 .codes_ptr
= PTR_TO_UINT64(keys
),
454 if (ioctl(fd
, EVIOCSMASK
, &mask
) < 0)
455 return log_warning_errno(errno
, "Failed to set EV_KEY event mask on /dev/input/%s: %m", name
);
457 bitset_put(switches
, SW_LID
);
458 bitset_put(switches
, SW_DOCK
);
460 mask
= (struct input_mask
) {
462 .codes_size
= sizeof(switches
),
463 .codes_ptr
= PTR_TO_UINT64(switches
),
466 if (ioctl(fd
, EVIOCSMASK
, &mask
) < 0)
467 return log_warning_errno(errno
, "Failed to set EV_SW event mask on /dev/input/%s: %m", name
);
472 int button_open(Button
*b
) {
473 _cleanup_(asynchronous_closep
) int fd
= -1;
480 b
->fd
= asynchronous_close(b
->fd
);
482 p
= strjoina("/dev/input/", b
->name
);
484 fd
= open(p
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
);
486 return log_warning_errno(errno
, "Failed to open %s: %m", p
);
488 r
= button_suitable(fd
);
490 return log_warning_errno(r
, "Failed to determine whether input device %s is relevant to us: %m", p
);
492 return log_debug_errno(SYNTHETIC_ERRNO(EADDRNOTAVAIL
),
493 "Device %s does not expose keys or switches relevant to us, ignoring.", p
);
495 if (ioctl(fd
, EVIOCGNAME(sizeof name
), name
) < 0)
496 return log_error_errno(errno
, "Failed to get input name for %s: %m", p
);
498 (void) button_set_mask(b
->name
, fd
);
500 b
->io_event_source
= sd_event_source_unref(b
->io_event_source
);
501 r
= sd_event_add_io(b
->manager
->event
, &b
->io_event_source
, fd
, EPOLLIN
, button_dispatch
, b
);
503 return log_error_errno(r
, "Failed to add button event for %s: %m", p
);
506 log_info("Watching system buttons on %s (%s)", p
, name
);
510 int button_check_switches(Button
*b
) {
511 unsigned long switches
[CONST_MAX(SW_LID
, SW_DOCK
)/ULONG_BITS
+1] = {};
517 if (ioctl(b
->fd
, EVIOCGSW(sizeof(switches
)), switches
) < 0)
520 b
->lid_closed
= bitset_get(switches
, SW_LID
);
521 b
->docked
= bitset_get(switches
, SW_DOCK
);
524 button_install_check_event_source(b
);