1 /* SPDX-License-Identifier: LGPL-2.1+ */
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"
17 #define CONST_MAX5(a, b, c, d, e) CONST_MAX(CONST_MAX(a, b), CONST_MAX(CONST_MAX(c, d), e))
19 #define ULONG_BITS (sizeof(unsigned long)*8)
21 static bool bitset_get(const unsigned long *bits
, unsigned i
) {
22 return (bits
[i
/ ULONG_BITS
] >> (i
% ULONG_BITS
)) & 1UL;
25 static void bitset_put(unsigned long *bits
, unsigned i
) {
26 bits
[i
/ ULONG_BITS
] |= (unsigned long) 1 << (i
% ULONG_BITS
);
29 Button
* button_new(Manager
*m
, const char *name
) {
39 b
->name
= strdup(name
);
43 if (hashmap_put(m
->buttons
, b
->name
, b
) < 0) {
54 void button_free(Button
*b
) {
57 hashmap_remove(b
->manager
->buttons
, b
->name
);
59 sd_event_source_unref(b
->io_event_source
);
60 sd_event_source_unref(b
->check_event_source
);
63 /* If the device has been unplugged close() returns
64 * ENODEV, let's ignore this, hence we don't use
73 int button_set_seat(Button
*b
, const char *sn
) {
89 static void button_lid_switch_handle_action(Manager
*manager
, bool is_edge
) {
90 HandleAction handle_action
;
94 /* If we are docked or on external power, handle the lid switch
96 if (manager_is_docked_or_external_displays(manager
))
97 handle_action
= manager
->handle_lid_switch_docked
;
98 else if (manager
->handle_lid_switch_ep
!= _HANDLE_ACTION_INVALID
&&
99 manager_is_on_external_power())
100 handle_action
= manager
->handle_lid_switch_ep
;
102 handle_action
= manager
->handle_lid_switch
;
104 manager_handle_action(manager
, INHIBIT_HANDLE_LID_SWITCH
, handle_action
, manager
->lid_switch_ignore_inhibited
, is_edge
);
107 static int button_recheck(sd_event_source
*e
, void *userdata
) {
108 Button
*b
= userdata
;
111 assert(b
->lid_closed
);
113 button_lid_switch_handle_action(b
->manager
, false);
117 static int button_install_check_event_source(Button
*b
) {
121 /* Install a post handler, so that we keep rechecking as long as the lid is closed. */
123 if (b
->check_event_source
)
126 r
= sd_event_add_post(b
->manager
->event
, &b
->check_event_source
, button_recheck
, b
);
130 return sd_event_source_set_priority(b
->check_event_source
, SD_EVENT_PRIORITY_IDLE
+1);
133 static int button_dispatch(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
134 Button
*b
= userdata
;
135 struct input_event ev
;
142 l
= read(b
->fd
, &ev
, sizeof(ev
));
144 return errno
!= EAGAIN
? -errno
: 0;
145 if ((size_t) l
< sizeof(ev
))
148 if (ev
.type
== EV_KEY
&& ev
.value
> 0) {
155 LOG_MESSAGE("Power key pressed."),
156 "MESSAGE_ID=" SD_MESSAGE_POWER_KEY_STR
);
158 manager_handle_action(b
->manager
, INHIBIT_HANDLE_POWER_KEY
, b
->manager
->handle_power_key
, b
->manager
->power_key_ignore_inhibited
, true);
161 /* The kernel naming is a bit confusing here:
162 KEY_RESTART was probably introduced for media playback purposes, but
163 is now being predominantly used to indicate device reboot.
168 LOG_MESSAGE("Reboot key pressed."),
169 "MESSAGE_ID=" SD_MESSAGE_REBOOT_KEY_STR
);
171 manager_handle_action(b
->manager
, INHIBIT_HANDLE_REBOOT_KEY
, b
->manager
->handle_reboot_key
, b
->manager
->reboot_key_ignore_inhibited
, true);
174 /* The kernel naming is a bit confusing here:
176 KEY_SLEEP = suspend-to-ram, which everybody else calls "suspend"
177 KEY_SUSPEND = suspend-to-disk, which everybody else calls "hibernate"
182 LOG_MESSAGE("Suspend key pressed."),
183 "MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_STR
);
185 manager_handle_action(b
->manager
, INHIBIT_HANDLE_SUSPEND_KEY
, b
->manager
->handle_suspend_key
, b
->manager
->suspend_key_ignore_inhibited
, true);
190 LOG_MESSAGE("Hibernate key pressed."),
191 "MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_STR
);
193 manager_handle_action(b
->manager
, INHIBIT_HANDLE_HIBERNATE_KEY
, b
->manager
->handle_hibernate_key
, b
->manager
->hibernate_key_ignore_inhibited
, true);
197 } else if (ev
.type
== EV_SW
&& ev
.value
> 0) {
199 if (ev
.code
== SW_LID
) {
201 LOG_MESSAGE("Lid closed."),
202 "MESSAGE_ID=" SD_MESSAGE_LID_CLOSED_STR
);
204 b
->lid_closed
= true;
205 button_lid_switch_handle_action(b
->manager
, true);
206 button_install_check_event_source(b
);
208 } else if (ev
.code
== SW_DOCK
) {
210 LOG_MESSAGE("System docked."),
211 "MESSAGE_ID=" SD_MESSAGE_SYSTEM_DOCKED_STR
);
216 } else if (ev
.type
== EV_SW
&& ev
.value
== 0) {
218 if (ev
.code
== SW_LID
) {
220 LOG_MESSAGE("Lid opened."),
221 "MESSAGE_ID=" SD_MESSAGE_LID_OPENED_STR
);
223 b
->lid_closed
= false;
224 b
->check_event_source
= sd_event_source_unref(b
->check_event_source
);
226 } else if (ev
.code
== SW_DOCK
) {
228 LOG_MESSAGE("System undocked."),
229 "MESSAGE_ID=" SD_MESSAGE_SYSTEM_UNDOCKED_STR
);
238 static int button_suitable(int fd
) {
239 unsigned long types
[CONST_MAX(EV_KEY
, EV_SW
)/ULONG_BITS
+1];
243 if (ioctl(fd
, EVIOCGBIT(EV_SYN
, sizeof types
), types
) < 0)
246 if (bitset_get(types
, EV_KEY
)) {
247 unsigned long keys
[CONST_MAX5(KEY_POWER
, KEY_POWER2
, KEY_SLEEP
, KEY_SUSPEND
, KEY_RESTART
)/ULONG_BITS
+1];
249 if (ioctl(fd
, EVIOCGBIT(EV_KEY
, sizeof keys
), keys
) < 0)
252 if (bitset_get(keys
, KEY_POWER
) ||
253 bitset_get(keys
, KEY_POWER2
) ||
254 bitset_get(keys
, KEY_SLEEP
) ||
255 bitset_get(keys
, KEY_SUSPEND
) ||
256 bitset_get(keys
, KEY_RESTART
))
260 if (bitset_get(types
, EV_SW
)) {
261 unsigned long switches
[CONST_MAX(SW_LID
, SW_DOCK
)/ULONG_BITS
+1];
263 if (ioctl(fd
, EVIOCGBIT(EV_SW
, sizeof switches
), switches
) < 0)
266 if (bitset_get(switches
, SW_LID
) ||
267 bitset_get(switches
, SW_DOCK
))
274 static int button_set_mask(const char *name
, int fd
) {
276 types
[CONST_MAX(EV_KEY
, EV_SW
)/ULONG_BITS
+1] = {},
277 keys
[CONST_MAX5(KEY_POWER
, KEY_POWER2
, KEY_SLEEP
, KEY_SUSPEND
, KEY_RESTART
)/ULONG_BITS
+1] = {},
278 switches
[CONST_MAX(SW_LID
, SW_DOCK
)/ULONG_BITS
+1] = {};
279 struct input_mask mask
;
284 bitset_put(types
, EV_KEY
);
285 bitset_put(types
, EV_SW
);
287 mask
= (struct input_mask
) {
289 .codes_size
= sizeof(types
),
290 .codes_ptr
= PTR_TO_UINT64(types
),
293 if (ioctl(fd
, EVIOCSMASK
, &mask
) < 0)
294 /* Log only at debug level if the kernel doesn't do EVIOCSMASK yet */
295 return log_full_errno(IN_SET(errno
, ENOTTY
, EOPNOTSUPP
, EINVAL
) ? LOG_DEBUG
: LOG_WARNING
,
296 errno
, "Failed to set EV_SYN event mask on /dev/input/%s: %m", name
);
298 bitset_put(keys
, KEY_POWER
);
299 bitset_put(keys
, KEY_POWER2
);
300 bitset_put(keys
, KEY_SLEEP
);
301 bitset_put(keys
, KEY_SUSPEND
);
302 bitset_put(keys
, KEY_RESTART
);
304 mask
= (struct input_mask
) {
306 .codes_size
= sizeof(keys
),
307 .codes_ptr
= PTR_TO_UINT64(keys
),
310 if (ioctl(fd
, EVIOCSMASK
, &mask
) < 0)
311 return log_warning_errno(errno
, "Failed to set EV_KEY event mask on /dev/input/%s: %m", name
);
313 bitset_put(switches
, SW_LID
);
314 bitset_put(switches
, SW_DOCK
);
316 mask
= (struct input_mask
) {
318 .codes_size
= sizeof(switches
),
319 .codes_ptr
= PTR_TO_UINT64(switches
),
322 if (ioctl(fd
, EVIOCSMASK
, &mask
) < 0)
323 return log_warning_errno(errno
, "Failed to set EV_SW event mask on /dev/input/%s: %m", name
);
328 int button_open(Button
*b
) {
329 _cleanup_close_
int fd
= -1;
336 b
->fd
= safe_close(b
->fd
);
338 p
= strjoina("/dev/input/", b
->name
);
340 fd
= open(p
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
);
342 return log_warning_errno(errno
, "Failed to open %s: %m", p
);
344 r
= button_suitable(fd
);
346 return log_warning_errno(r
, "Failed to determine whether input device %s is relevant to us: %m", p
);
348 return log_debug_errno(SYNTHETIC_ERRNO(EADDRNOTAVAIL
),
349 "Device %s does not expose keys or switches relevant to us, ignoring.", p
);
351 if (ioctl(fd
, EVIOCGNAME(sizeof name
), name
) < 0)
352 return log_error_errno(errno
, "Failed to get input name for %s: %m", p
);
354 (void) button_set_mask(b
->name
, fd
);
356 b
->io_event_source
= sd_event_source_unref(b
->io_event_source
);
357 r
= sd_event_add_io(b
->manager
->event
, &b
->io_event_source
, fd
, EPOLLIN
, button_dispatch
, b
);
359 return log_error_errno(r
, "Failed to add button event for %s: %m", p
);
362 log_info("Watching system buttons on %s (%s)", p
, name
);
366 int button_check_switches(Button
*b
) {
367 unsigned long switches
[CONST_MAX(SW_LID
, SW_DOCK
)/ULONG_BITS
+1] = {};
373 if (ioctl(b
->fd
, EVIOCGSW(sizeof(switches
)), switches
) < 0)
376 b
->lid_closed
= bitset_get(switches
, SW_LID
);
377 b
->docked
= bitset_get(switches
, SW_DOCK
);
380 button_install_check_event_source(b
);