1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2012 Lennart Poettering
11 #include <sys/ioctl.h>
13 #include <linux/input.h>
15 #include "sd-messages.h"
17 #include "alloc-util.h"
19 #include "logind-button.h"
20 #include "string-util.h"
23 #define CONST_MAX4(a, b, c, d) CONST_MAX(CONST_MAX(a, b), CONST_MAX(c, d))
25 #define ULONG_BITS (sizeof(unsigned long)*8)
27 static bool bitset_get(const unsigned long *bits, unsigned i) {
28 return (bits[i / ULONG_BITS] >> (i % ULONG_BITS)) & 1UL;
31 static void bitset_put(unsigned long *bits, unsigned i) {
32 bits[i / ULONG_BITS] |= (unsigned long) 1 << (i % ULONG_BITS);
35 Button* button_new(Manager *m, const char *name) {
45 b->name = strdup(name);
49 if (hashmap_put(m->buttons, b->name, b) < 0) {
60 void button_free(Button *b) {
63 hashmap_remove(b->manager->buttons, b->name);
65 sd_event_source_unref(b->io_event_source);
66 sd_event_source_unref(b->check_event_source);
69 /* If the device has been unplugged close() returns
70 * ENODEV, let's ignore this, hence we don't use
79 int button_set_seat(Button *b, const char *sn) {
95 static void button_lid_switch_handle_action(Manager *manager, bool is_edge) {
96 HandleAction handle_action;
100 /* If we are docked or on external power, handle the lid switch
102 if (manager_is_docked_or_external_displays(manager))
103 handle_action = manager->handle_lid_switch_docked;
104 else if (manager->handle_lid_switch_ep != _HANDLE_ACTION_INVALID &&
105 manager_is_on_external_power())
106 handle_action = manager->handle_lid_switch_ep;
108 handle_action = manager->handle_lid_switch;
110 manager_handle_action(manager, INHIBIT_HANDLE_LID_SWITCH, handle_action, manager->lid_switch_ignore_inhibited, is_edge);
113 static int button_recheck(sd_event_source *e, void *userdata) {
114 Button *b = userdata;
117 assert(b->lid_closed);
119 button_lid_switch_handle_action(b->manager, false);
123 static int button_install_check_event_source(Button *b) {
127 /* Install a post handler, so that we keep rechecking as long as the lid is closed. */
129 if (b->check_event_source)
132 r = sd_event_add_post(b->manager->event, &b->check_event_source, button_recheck, b);
136 return sd_event_source_set_priority(b->check_event_source, SD_EVENT_PRIORITY_IDLE+1);
139 static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
140 Button *b = userdata;
141 struct input_event ev;
148 l = read(b->fd, &ev, sizeof(ev));
150 return errno != EAGAIN ? -errno : 0;
151 if ((size_t) l < sizeof(ev))
154 if (ev.type == EV_KEY && ev.value > 0) {
161 LOG_MESSAGE("Power key pressed."),
162 "MESSAGE_ID=" SD_MESSAGE_POWER_KEY_STR,
165 manager_handle_action(b->manager, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true);
168 /* The kernel is a bit confused here:
170 KEY_SLEEP = suspend-to-ram, which everybody else calls "suspend"
171 KEY_SUSPEND = suspend-to-disk, which everybody else calls "hibernate"
176 LOG_MESSAGE("Suspend key pressed."),
177 "MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_STR,
180 manager_handle_action(b->manager, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true);
185 LOG_MESSAGE("Hibernate key pressed."),
186 "MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_STR,
189 manager_handle_action(b->manager, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true);
193 } else if (ev.type == EV_SW && ev.value > 0) {
195 if (ev.code == SW_LID) {
197 LOG_MESSAGE("Lid closed."),
198 "MESSAGE_ID=" SD_MESSAGE_LID_CLOSED_STR,
201 b->lid_closed = true;
202 button_lid_switch_handle_action(b->manager, true);
203 button_install_check_event_source(b);
205 } else if (ev.code == SW_DOCK) {
207 LOG_MESSAGE("System docked."),
208 "MESSAGE_ID=" SD_MESSAGE_SYSTEM_DOCKED_STR,
214 } else if (ev.type == EV_SW && ev.value == 0) {
216 if (ev.code == SW_LID) {
218 LOG_MESSAGE("Lid opened."),
219 "MESSAGE_ID=" SD_MESSAGE_LID_OPENED_STR,
222 b->lid_closed = false;
223 b->check_event_source = sd_event_source_unref(b->check_event_source);
225 } else if (ev.code == SW_DOCK) {
227 LOG_MESSAGE("System undocked."),
228 "MESSAGE_ID=" SD_MESSAGE_SYSTEM_UNDOCKED_STR,
238 static int button_suitable(Button *b) {
239 unsigned long types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1];
244 if (ioctl(b->fd, EVIOCGBIT(EV_SYN, sizeof(types)), types) < 0)
247 if (bitset_get(types, EV_KEY)) {
248 unsigned long keys[CONST_MAX4(KEY_POWER, KEY_POWER2, KEY_SLEEP, KEY_SUSPEND)/ULONG_BITS+1];
250 if (ioctl(b->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))
260 if (bitset_get(types, EV_SW)) {
261 unsigned long switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1];
263 if (ioctl(b->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(Button *b) {
276 types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1] = {},
277 keys[CONST_MAX4(KEY_POWER, KEY_POWER2, KEY_SLEEP, KEY_SUSPEND)/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(b->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", b->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);
303 mask = (struct input_mask) {
305 .codes_size = sizeof(keys),
306 .codes_ptr = PTR_TO_UINT64(keys),
309 if (ioctl(b->fd, EVIOCSMASK, &mask) < 0)
310 return log_warning_errno(errno, "Failed to set EV_KEY event mask on /dev/input/%s: %m", b->name);
312 bitset_put(switches, SW_LID);
313 bitset_put(switches, SW_DOCK);
315 mask = (struct input_mask) {
317 .codes_size = sizeof(switches),
318 .codes_ptr = PTR_TO_UINT64(switches),
321 if (ioctl(b->fd, EVIOCSMASK, &mask) < 0)
322 return log_warning_errno(errno, "Failed to set EV_SW event mask on /dev/input/%s: %m", b->name);
327 int button_open(Button *b) {
333 b->fd = safe_close(b->fd);
335 p = strjoina("/dev/input/", b->name);
337 b->fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
339 return log_warning_errno(errno, "Failed to open %s: %m", p);
341 r = button_suitable(b);
343 return log_warning_errno(r, "Failed to determine whether input device is relevant to us: %m");
345 log_debug("Device %s does not expose keys or switches relevant to us, ignoring.", p);
346 return -EADDRNOTAVAIL;
349 if (ioctl(b->fd, EVIOCGNAME(sizeof(name)), name) < 0) {
350 r = log_error_errno(errno, "Failed to get input name: %m");
354 (void) button_set_mask(b);
356 r = sd_event_add_io(b->manager->event, &b->io_event_source, b->fd, EPOLLIN, button_dispatch, b);
358 log_error_errno(r, "Failed to add button event: %m");
362 log_info("Watching system buttons on /dev/input/%s (%s)", b->name, name);
367 b->fd = safe_close(b->fd);
371 int button_check_switches(Button *b) {
372 unsigned long switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1] = {};
378 if (ioctl(b->fd, EVIOCGSW(sizeof(switches)), switches) < 0)
381 b->lid_closed = bitset_get(switches, SW_LID);
382 b->docked = bitset_get(switches, SW_DOCK);
385 button_install_check_event_source(b);