1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "sd-messages.h"
10 #include "alloc-util.h"
12 #include "logind-button.h"
13 #include "missing_input.h"
14 #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 static bool bitset_get(const unsigned long *bits
, unsigned i
) {
23 return (bits
[i
/ ULONG_BITS
] >> (i
% ULONG_BITS
)) & 1UL;
26 static void bitset_put(unsigned long *bits
, unsigned i
) {
27 bits
[i
/ ULONG_BITS
] |= (unsigned long) 1 << (i
% ULONG_BITS
);
30 Button
* button_new(Manager
*m
, const char *name
) {
40 b
->name
= strdup(name
);
44 if (hashmap_put(m
->buttons
, b
->name
, b
) < 0) {
55 void button_free(Button
*b
) {
58 hashmap_remove(b
->manager
->buttons
, b
->name
);
60 sd_event_source_unref(b
->io_event_source
);
61 sd_event_source_unref(b
->check_event_source
);
64 /* If the device has been unplugged close() returns
65 * ENODEV, let's ignore this, hence we don't use
67 (void) asynchronous_close(b
->fd
);
74 int button_set_seat(Button
*b
, const char *sn
) {
90 static void button_lid_switch_handle_action(Manager
*manager
, bool is_edge
) {
91 HandleAction handle_action
;
95 /* If we are docked or on external power, handle the lid switch
97 if (manager_is_docked_or_external_displays(manager
))
98 handle_action
= manager
->handle_lid_switch_docked
;
99 else if (manager
->handle_lid_switch_ep
!= _HANDLE_ACTION_INVALID
&&
100 manager_is_on_external_power())
101 handle_action
= manager
->handle_lid_switch_ep
;
103 handle_action
= manager
->handle_lid_switch
;
105 manager_handle_action(manager
, INHIBIT_HANDLE_LID_SWITCH
, handle_action
, manager
->lid_switch_ignore_inhibited
, is_edge
);
108 static int button_recheck(sd_event_source
*e
, void *userdata
) {
109 Button
*b
= userdata
;
112 assert(b
->lid_closed
);
114 button_lid_switch_handle_action(b
->manager
, false);
118 static int button_install_check_event_source(Button
*b
) {
122 /* Install a post handler, so that we keep rechecking as long as the lid is closed. */
124 if (b
->check_event_source
)
127 r
= sd_event_add_post(b
->manager
->event
, &b
->check_event_source
, button_recheck
, b
);
131 return sd_event_source_set_priority(b
->check_event_source
, SD_EVENT_PRIORITY_IDLE
+1);
134 static int button_dispatch(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
135 Button
*b
= userdata
;
136 struct input_event ev
;
143 l
= read(b
->fd
, &ev
, sizeof(ev
));
145 return errno
!= EAGAIN
? -errno
: 0;
146 if ((size_t) l
< sizeof(ev
))
149 if (ev
.type
== EV_KEY
&& ev
.value
> 0) {
156 LOG_MESSAGE("Power key pressed."),
157 "MESSAGE_ID=" SD_MESSAGE_POWER_KEY_STR
);
159 manager_handle_action(b
->manager
, INHIBIT_HANDLE_POWER_KEY
, b
->manager
->handle_power_key
, b
->manager
->power_key_ignore_inhibited
, true);
162 /* The kernel naming is a bit confusing here:
163 KEY_RESTART was probably introduced for media playback purposes, but
164 is now being predominantly used to indicate device reboot.
169 LOG_MESSAGE("Reboot key pressed."),
170 "MESSAGE_ID=" SD_MESSAGE_REBOOT_KEY_STR
);
172 manager_handle_action(b
->manager
, INHIBIT_HANDLE_REBOOT_KEY
, b
->manager
->handle_reboot_key
, b
->manager
->reboot_key_ignore_inhibited
, true);
175 /* The kernel naming is a bit confusing here:
177 KEY_SLEEP = suspend-to-ram, which everybody else calls "suspend"
178 KEY_SUSPEND = suspend-to-disk, which everybody else calls "hibernate"
183 LOG_MESSAGE("Suspend key pressed."),
184 "MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_STR
);
186 manager_handle_action(b
->manager
, INHIBIT_HANDLE_SUSPEND_KEY
, b
->manager
->handle_suspend_key
, b
->manager
->suspend_key_ignore_inhibited
, true);
191 LOG_MESSAGE("Hibernate key pressed."),
192 "MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_STR
);
194 manager_handle_action(b
->manager
, INHIBIT_HANDLE_HIBERNATE_KEY
, b
->manager
->handle_hibernate_key
, b
->manager
->hibernate_key_ignore_inhibited
, true);
198 } else if (ev
.type
== EV_SW
&& ev
.value
> 0) {
200 if (ev
.code
== SW_LID
) {
202 LOG_MESSAGE("Lid closed."),
203 "MESSAGE_ID=" SD_MESSAGE_LID_CLOSED_STR
);
205 b
->lid_closed
= true;
206 button_lid_switch_handle_action(b
->manager
, true);
207 button_install_check_event_source(b
);
209 } else if (ev
.code
== SW_DOCK
) {
211 LOG_MESSAGE("System docked."),
212 "MESSAGE_ID=" SD_MESSAGE_SYSTEM_DOCKED_STR
);
217 } else if (ev
.type
== EV_SW
&& ev
.value
== 0) {
219 if (ev
.code
== SW_LID
) {
221 LOG_MESSAGE("Lid opened."),
222 "MESSAGE_ID=" SD_MESSAGE_LID_OPENED_STR
);
224 b
->lid_closed
= false;
225 b
->check_event_source
= sd_event_source_unref(b
->check_event_source
);
227 } else if (ev
.code
== SW_DOCK
) {
229 LOG_MESSAGE("System undocked."),
230 "MESSAGE_ID=" SD_MESSAGE_SYSTEM_UNDOCKED_STR
);
239 static int button_suitable(int fd
) {
240 unsigned long types
[CONST_MAX(EV_KEY
, EV_SW
)/ULONG_BITS
+1];
244 if (ioctl(fd
, EVIOCGBIT(EV_SYN
, sizeof types
), types
) < 0)
247 if (bitset_get(types
, EV_KEY
)) {
248 unsigned long keys
[CONST_MAX5(KEY_POWER
, KEY_POWER2
, KEY_SLEEP
, KEY_SUSPEND
, KEY_RESTART
)/ULONG_BITS
+1];
250 if (ioctl(fd
, EVIOCGBIT(EV_KEY
, sizeof keys
), keys
) < 0)
253 if (bitset_get(keys
, KEY_POWER
) ||
254 bitset_get(keys
, KEY_POWER2
) ||
255 bitset_get(keys
, KEY_SLEEP
) ||
256 bitset_get(keys
, KEY_SUSPEND
) ||
257 bitset_get(keys
, KEY_RESTART
))
261 if (bitset_get(types
, EV_SW
)) {
262 unsigned long switches
[CONST_MAX(SW_LID
, SW_DOCK
)/ULONG_BITS
+1];
264 if (ioctl(fd
, EVIOCGBIT(EV_SW
, sizeof switches
), switches
) < 0)
267 if (bitset_get(switches
, SW_LID
) ||
268 bitset_get(switches
, SW_DOCK
))
275 static int button_set_mask(const char *name
, int fd
) {
277 types
[CONST_MAX(EV_KEY
, EV_SW
)/ULONG_BITS
+1] = {},
278 keys
[CONST_MAX5(KEY_POWER
, KEY_POWER2
, KEY_SLEEP
, KEY_SUSPEND
, KEY_RESTART
)/ULONG_BITS
+1] = {},
279 switches
[CONST_MAX(SW_LID
, SW_DOCK
)/ULONG_BITS
+1] = {};
280 struct input_mask mask
;
285 bitset_put(types
, EV_KEY
);
286 bitset_put(types
, EV_SW
);
288 mask
= (struct input_mask
) {
290 .codes_size
= sizeof(types
),
291 .codes_ptr
= PTR_TO_UINT64(types
),
294 if (ioctl(fd
, EVIOCSMASK
, &mask
) < 0)
295 /* Log only at debug level if the kernel doesn't do EVIOCSMASK yet */
296 return log_full_errno(IN_SET(errno
, ENOTTY
, EOPNOTSUPP
, EINVAL
) ? LOG_DEBUG
: LOG_WARNING
,
297 errno
, "Failed to set EV_SYN event mask on /dev/input/%s: %m", name
);
299 bitset_put(keys
, KEY_POWER
);
300 bitset_put(keys
, KEY_POWER2
);
301 bitset_put(keys
, KEY_SLEEP
);
302 bitset_put(keys
, KEY_SUSPEND
);
303 bitset_put(keys
, KEY_RESTART
);
305 mask
= (struct input_mask
) {
307 .codes_size
= sizeof(keys
),
308 .codes_ptr
= PTR_TO_UINT64(keys
),
311 if (ioctl(fd
, EVIOCSMASK
, &mask
) < 0)
312 return log_warning_errno(errno
, "Failed to set EV_KEY event mask on /dev/input/%s: %m", name
);
314 bitset_put(switches
, SW_LID
);
315 bitset_put(switches
, SW_DOCK
);
317 mask
= (struct input_mask
) {
319 .codes_size
= sizeof(switches
),
320 .codes_ptr
= PTR_TO_UINT64(switches
),
323 if (ioctl(fd
, EVIOCSMASK
, &mask
) < 0)
324 return log_warning_errno(errno
, "Failed to set EV_SW event mask on /dev/input/%s: %m", name
);
329 int button_open(Button
*b
) {
330 _cleanup_close_
int fd
= -1;
337 b
->fd
= safe_close(b
->fd
);
339 p
= strjoina("/dev/input/", b
->name
);
341 fd
= open(p
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
);
343 return log_warning_errno(errno
, "Failed to open %s: %m", p
);
345 r
= button_suitable(fd
);
347 return log_warning_errno(r
, "Failed to determine whether input device %s is relevant to us: %m", p
);
350 return log_debug_errno(SYNTHETIC_ERRNO(EADDRNOTAVAIL
),
351 "Device %s does not expose keys or switches relevant to us, ignoring.", p
);
354 if (ioctl(fd
, EVIOCGNAME(sizeof name
), name
) < 0)
355 return log_error_errno(errno
, "Failed to get input name for %s: %m", p
);
357 (void) button_set_mask(b
->name
, fd
);
359 b
->io_event_source
= sd_event_source_unref(b
->io_event_source
);
360 r
= sd_event_add_io(b
->manager
->event
, &b
->io_event_source
, fd
, EPOLLIN
, button_dispatch
, b
);
362 return log_error_errno(r
, "Failed to add button event for %s: %m", p
);
365 log_info("Watching system buttons on %s (%s)", p
, name
);
369 int button_check_switches(Button
*b
) {
370 unsigned long switches
[CONST_MAX(SW_LID
, SW_DOCK
)/ULONG_BITS
+1] = {};
376 if (ioctl(b
->fd
, EVIOCGSW(sizeof(switches
)), switches
) < 0)
379 b
->lid_closed
= bitset_get(switches
, SW_LID
);
380 b
->docked
= bitset_get(switches
, SW_DOCK
);
383 button_install_check_event_source(b
);