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
) {
96 Button
*b
= ASSERT_PTR(userdata
);
98 assert(b
->lid_closed
);
100 button_lid_switch_handle_action(b
->manager
, false);
104 static int button_install_check_event_source(Button
*b
) {
108 /* Install a post handler, so that we keep rechecking as long as the lid is closed. */
110 if (b
->check_event_source
)
113 r
= sd_event_add_post(b
->manager
->event
, &b
->check_event_source
, button_recheck
, b
);
117 return sd_event_source_set_priority(b
->check_event_source
, SD_EVENT_PRIORITY_IDLE
+1);
120 static int long_press_of_power_key_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
121 Manager
*m
= ASSERT_PTR(userdata
);
125 m
->power_key_long_press_event_source
= sd_event_source_unref(m
->power_key_long_press_event_source
);
128 LOG_MESSAGE("Power key pressed long."),
129 "MESSAGE_ID=" SD_MESSAGE_POWER_KEY_LONG_PRESS_STR
);
131 manager_handle_action(m
, INHIBIT_HANDLE_POWER_KEY
, m
->handle_power_key_long_press
, m
->power_key_ignore_inhibited
, true);
135 static int long_press_of_reboot_key_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
136 Manager
*m
= ASSERT_PTR(userdata
);
140 m
->reboot_key_long_press_event_source
= sd_event_source_unref(m
->reboot_key_long_press_event_source
);
143 LOG_MESSAGE("Reboot key pressed long."),
144 "MESSAGE_ID=" SD_MESSAGE_REBOOT_KEY_LONG_PRESS_STR
);
146 manager_handle_action(m
, INHIBIT_HANDLE_REBOOT_KEY
, m
->handle_reboot_key_long_press
, m
->reboot_key_ignore_inhibited
, true);
150 static int long_press_of_suspend_key_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
151 Manager
*m
= ASSERT_PTR(userdata
);
155 m
->suspend_key_long_press_event_source
= sd_event_source_unref(m
->suspend_key_long_press_event_source
);
158 LOG_MESSAGE("Suspend key pressed long."),
159 "MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_LONG_PRESS_STR
);
161 manager_handle_action(m
, INHIBIT_HANDLE_SUSPEND_KEY
, m
->handle_suspend_key_long_press
, m
->suspend_key_ignore_inhibited
, true);
165 static int long_press_of_hibernate_key_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
166 Manager
*m
= ASSERT_PTR(userdata
);
170 m
->hibernate_key_long_press_event_source
= sd_event_source_unref(m
->hibernate_key_long_press_event_source
);
173 LOG_MESSAGE("Hibernate key pressed long."),
174 "MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_LONG_PRESS_STR
);
176 manager_handle_action(m
, INHIBIT_HANDLE_HIBERNATE_KEY
, m
->handle_hibernate_key_long_press
, m
->hibernate_key_ignore_inhibited
, true);
180 static void start_long_press(Manager
*m
, sd_event_source
**e
, sd_event_time_handler_t callback
) {
189 r
= sd_event_add_time_relative(
193 LONG_PRESS_DURATION
, 0,
196 log_warning_errno(r
, "Failed to add long press timer event, ignoring: %m");
199 static int button_dispatch(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
200 Button
*b
= ASSERT_PTR(userdata
);
201 struct input_event ev
;
207 l
= read(b
->fd
, &ev
, sizeof(ev
));
209 return errno
!= EAGAIN
? -errno
: 0;
210 if ((size_t) l
< sizeof(ev
))
213 if (ev
.type
== EV_KEY
&& ev
.value
> 0) {
219 if (b
->manager
->handle_power_key_long_press
!= HANDLE_IGNORE
&& b
->manager
->handle_power_key_long_press
!= b
->manager
->handle_power_key
) {
220 log_debug("Power key pressed. Further action depends on the key press duration.");
221 start_long_press(b
->manager
, &b
->manager
->power_key_long_press_event_source
, long_press_of_power_key_handler
);
224 LOG_MESSAGE("Power key pressed short."),
225 "MESSAGE_ID=" SD_MESSAGE_POWER_KEY_STR
);
226 manager_handle_action(b
->manager
, INHIBIT_HANDLE_POWER_KEY
, b
->manager
->handle_power_key
, b
->manager
->power_key_ignore_inhibited
, true);
230 /* The kernel naming is a bit confusing here:
231 KEY_RESTART was probably introduced for media playback purposes, but
232 is now being predominantly used to indicate device reboot.
236 if (b
->manager
->handle_reboot_key_long_press
!= HANDLE_IGNORE
&& b
->manager
->handle_reboot_key_long_press
!= b
->manager
->handle_reboot_key
) {
237 log_debug("Reboot key pressed. Further action depends on the key press duration.");
238 start_long_press(b
->manager
, &b
->manager
->reboot_key_long_press_event_source
, long_press_of_reboot_key_handler
);
241 LOG_MESSAGE("Reboot key pressed short."),
242 "MESSAGE_ID=" SD_MESSAGE_REBOOT_KEY_STR
);
243 manager_handle_action(b
->manager
, INHIBIT_HANDLE_REBOOT_KEY
, b
->manager
->handle_reboot_key
, b
->manager
->reboot_key_ignore_inhibited
, true);
247 /* The kernel naming is a bit confusing here:
249 KEY_SLEEP = suspend-to-ram, which everybody else calls "suspend"
250 KEY_SUSPEND = suspend-to-disk, which everybody else calls "hibernate"
254 if (b
->manager
->handle_suspend_key_long_press
!= HANDLE_IGNORE
&& b
->manager
->handle_suspend_key_long_press
!= b
->manager
->handle_suspend_key
) {
255 log_debug("Suspend key pressed. Further action depends on the key press duration.");
256 start_long_press(b
->manager
, &b
->manager
->suspend_key_long_press_event_source
, long_press_of_suspend_key_handler
);
259 LOG_MESSAGE("Suspend key pressed short."),
260 "MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_STR
);
261 manager_handle_action(b
->manager
, INHIBIT_HANDLE_SUSPEND_KEY
, b
->manager
->handle_suspend_key
, b
->manager
->suspend_key_ignore_inhibited
, true);
266 if (b
->manager
->handle_hibernate_key_long_press
!= HANDLE_IGNORE
&& b
->manager
->handle_hibernate_key_long_press
!= b
->manager
->handle_hibernate_key
) {
267 log_debug("Hibernate key pressed. Further action depends on the key press duration.");
268 start_long_press(b
->manager
, &b
->manager
->hibernate_key_long_press_event_source
, long_press_of_hibernate_key_handler
);
271 LOG_MESSAGE("Hibernate key pressed short."),
272 "MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_STR
);
273 manager_handle_action(b
->manager
, INHIBIT_HANDLE_HIBERNATE_KEY
, b
->manager
->handle_hibernate_key
, b
->manager
->hibernate_key_ignore_inhibited
, true);
278 } else if (ev
.type
== EV_KEY
&& ev
.value
== 0) {
284 if (b
->manager
->power_key_long_press_event_source
) {
285 /* Long press event timer is still pending and key release
286 event happened. This means that key press duration was
287 insufficient to trigger a long press event
290 LOG_MESSAGE("Power key pressed short."),
291 "MESSAGE_ID=" SD_MESSAGE_POWER_KEY_STR
);
293 b
->manager
->power_key_long_press_event_source
= sd_event_source_unref(b
->manager
->power_key_long_press_event_source
);
295 manager_handle_action(b
->manager
, INHIBIT_HANDLE_POWER_KEY
, b
->manager
->handle_power_key
, b
->manager
->power_key_ignore_inhibited
, true);
300 if (b
->manager
->reboot_key_long_press_event_source
) {
302 LOG_MESSAGE("Reboot key pressed short."),
303 "MESSAGE_ID=" SD_MESSAGE_REBOOT_KEY_STR
);
305 b
->manager
->reboot_key_long_press_event_source
= sd_event_source_unref(b
->manager
->reboot_key_long_press_event_source
);
307 manager_handle_action(b
->manager
, INHIBIT_HANDLE_REBOOT_KEY
, b
->manager
->handle_reboot_key
, b
->manager
->reboot_key_ignore_inhibited
, true);
312 if (b
->manager
->suspend_key_long_press_event_source
) {
314 LOG_MESSAGE("Suspend key pressed short."),
315 "MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_STR
);
317 b
->manager
->suspend_key_long_press_event_source
= sd_event_source_unref(b
->manager
->suspend_key_long_press_event_source
);
319 manager_handle_action(b
->manager
, INHIBIT_HANDLE_SUSPEND_KEY
, b
->manager
->handle_suspend_key
, b
->manager
->suspend_key_ignore_inhibited
, true);
323 if (b
->manager
->hibernate_key_long_press_event_source
) {
325 LOG_MESSAGE("Hibernate key pressed short."),
326 "MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_STR
);
328 b
->manager
->hibernate_key_long_press_event_source
= sd_event_source_unref(b
->manager
->hibernate_key_long_press_event_source
);
330 manager_handle_action(b
->manager
, INHIBIT_HANDLE_HIBERNATE_KEY
, b
->manager
->handle_hibernate_key
, b
->manager
->hibernate_key_ignore_inhibited
, true);
335 } else if (ev
.type
== EV_SW
&& ev
.value
> 0) {
337 if (ev
.code
== SW_LID
) {
339 LOG_MESSAGE("Lid closed."),
340 "MESSAGE_ID=" SD_MESSAGE_LID_CLOSED_STR
);
342 b
->lid_closed
= true;
343 button_lid_switch_handle_action(b
->manager
, true);
344 button_install_check_event_source(b
);
346 } else if (ev
.code
== SW_DOCK
) {
348 LOG_MESSAGE("System docked."),
349 "MESSAGE_ID=" SD_MESSAGE_SYSTEM_DOCKED_STR
);
354 } else if (ev
.type
== EV_SW
&& ev
.value
== 0) {
356 if (ev
.code
== SW_LID
) {
358 LOG_MESSAGE("Lid opened."),
359 "MESSAGE_ID=" SD_MESSAGE_LID_OPENED_STR
);
361 b
->lid_closed
= false;
362 b
->check_event_source
= sd_event_source_unref(b
->check_event_source
);
364 } else if (ev
.code
== SW_DOCK
) {
366 LOG_MESSAGE("System undocked."),
367 "MESSAGE_ID=" SD_MESSAGE_SYSTEM_UNDOCKED_STR
);
376 static int button_suitable(int fd
) {
377 unsigned long types
[CONST_MAX(EV_KEY
, EV_SW
)/ULONG_BITS
+1];
381 if (ioctl(fd
, EVIOCGBIT(EV_SYN
, sizeof types
), types
) < 0)
384 if (bitset_get(types
, EV_KEY
)) {
385 unsigned long keys
[CONST_MAX5(KEY_POWER
, KEY_POWER2
, KEY_SLEEP
, KEY_SUSPEND
, KEY_RESTART
)/ULONG_BITS
+1];
387 if (ioctl(fd
, EVIOCGBIT(EV_KEY
, sizeof keys
), keys
) < 0)
390 if (bitset_get(keys
, KEY_POWER
) ||
391 bitset_get(keys
, KEY_POWER2
) ||
392 bitset_get(keys
, KEY_SLEEP
) ||
393 bitset_get(keys
, KEY_SUSPEND
) ||
394 bitset_get(keys
, KEY_RESTART
))
398 if (bitset_get(types
, EV_SW
)) {
399 unsigned long switches
[CONST_MAX(SW_LID
, SW_DOCK
)/ULONG_BITS
+1];
401 if (ioctl(fd
, EVIOCGBIT(EV_SW
, sizeof switches
), switches
) < 0)
404 if (bitset_get(switches
, SW_LID
) ||
405 bitset_get(switches
, SW_DOCK
))
412 static int button_set_mask(const char *name
, int fd
) {
414 types
[CONST_MAX(EV_KEY
, EV_SW
)/ULONG_BITS
+1] = {},
415 keys
[CONST_MAX5(KEY_POWER
, KEY_POWER2
, KEY_SLEEP
, KEY_SUSPEND
, KEY_RESTART
)/ULONG_BITS
+1] = {},
416 switches
[CONST_MAX(SW_LID
, SW_DOCK
)/ULONG_BITS
+1] = {};
417 struct input_mask mask
;
422 bitset_put(types
, EV_KEY
);
423 bitset_put(types
, EV_SW
);
425 mask
= (struct input_mask
) {
427 .codes_size
= sizeof(types
),
428 .codes_ptr
= PTR_TO_UINT64(types
),
431 if (ioctl(fd
, EVIOCSMASK
, &mask
) < 0)
432 /* Log only at debug level if the kernel doesn't do EVIOCSMASK yet */
433 return log_full_errno(IN_SET(errno
, ENOTTY
, EOPNOTSUPP
, EINVAL
) ? LOG_DEBUG
: LOG_WARNING
,
434 errno
, "Failed to set EV_SYN event mask on /dev/input/%s: %m", name
);
436 bitset_put(keys
, KEY_POWER
);
437 bitset_put(keys
, KEY_POWER2
);
438 bitset_put(keys
, KEY_SLEEP
);
439 bitset_put(keys
, KEY_SUSPEND
);
440 bitset_put(keys
, KEY_RESTART
);
442 mask
= (struct input_mask
) {
444 .codes_size
= sizeof(keys
),
445 .codes_ptr
= PTR_TO_UINT64(keys
),
448 if (ioctl(fd
, EVIOCSMASK
, &mask
) < 0)
449 return log_warning_errno(errno
, "Failed to set EV_KEY event mask on /dev/input/%s: %m", name
);
451 bitset_put(switches
, SW_LID
);
452 bitset_put(switches
, SW_DOCK
);
454 mask
= (struct input_mask
) {
456 .codes_size
= sizeof(switches
),
457 .codes_ptr
= PTR_TO_UINT64(switches
),
460 if (ioctl(fd
, EVIOCSMASK
, &mask
) < 0)
461 return log_warning_errno(errno
, "Failed to set EV_SW event mask on /dev/input/%s: %m", name
);
466 int button_open(Button
*b
) {
467 _cleanup_(asynchronous_closep
) int fd
= -1;
474 b
->fd
= asynchronous_close(b
->fd
);
476 p
= strjoina("/dev/input/", b
->name
);
478 fd
= open(p
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
);
480 return log_warning_errno(errno
, "Failed to open %s: %m", p
);
482 r
= button_suitable(fd
);
484 return log_warning_errno(r
, "Failed to determine whether input device %s is relevant to us: %m", p
);
486 return log_debug_errno(SYNTHETIC_ERRNO(EADDRNOTAVAIL
),
487 "Device %s does not expose keys or switches relevant to us, ignoring.", p
);
489 if (ioctl(fd
, EVIOCGNAME(sizeof name
), name
) < 0)
490 return log_error_errno(errno
, "Failed to get input name for %s: %m", p
);
492 (void) button_set_mask(b
->name
, fd
);
494 b
->io_event_source
= sd_event_source_unref(b
->io_event_source
);
495 r
= sd_event_add_io(b
->manager
->event
, &b
->io_event_source
, fd
, EPOLLIN
, button_dispatch
, b
);
497 return log_error_errno(r
, "Failed to add button event for %s: %m", p
);
500 log_info("Watching system buttons on %s (%s)", p
, name
);
504 int button_check_switches(Button
*b
) {
505 unsigned long switches
[CONST_MAX(SW_LID
, SW_DOCK
)/ULONG_BITS
+1] = {};
511 if (ioctl(b
->fd
, EVIOCGSW(sizeof(switches
)), switches
) < 0)
514 b
->lid_closed
= bitset_get(switches
, SW_LID
);
515 b
->docked
= bitset_get(switches
, SW_DOCK
);
518 button_install_check_event_source(b
);