1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2012 Lennart Poettering
11 #include <linux/input.h>
13 #include "sd-messages.h"
15 #include "alloc-util.h"
17 #include "logind-button.h"
18 #include "string-util.h"
21 #define CONST_MAX4(a, b, c, d) CONST_MAX(CONST_MAX(a, b), CONST_MAX(c, d))
23 #define ULONG_BITS (sizeof(unsigned long)*8)
25 static bool bitset_get(const unsigned long *bits, unsigned i) {
26 return (bits[i / ULONG_BITS] >> (i % ULONG_BITS)) & 1UL;
29 static void bitset_put(unsigned long *bits, unsigned i) {
30 bits[i / ULONG_BITS] |= (unsigned long) 1 << (i % ULONG_BITS);
33 Button* button_new(Manager *m, const char *name) {
43 b->name = strdup(name);
47 if (hashmap_put(m->buttons, b->name, b) < 0) {
58 void button_free(Button *b) {
61 hashmap_remove(b->manager->buttons, b->name);
63 sd_event_source_unref(b->io_event_source);
64 sd_event_source_unref(b->check_event_source);
67 /* If the device has been unplugged close() returns
68 * ENODEV, let's ignore this, hence we don't use
77 int button_set_seat(Button *b, const char *sn) {
93 static void button_lid_switch_handle_action(Manager *manager, bool is_edge) {
94 HandleAction handle_action;
98 /* If we are docked or on external power, handle the lid switch
100 if (manager_is_docked_or_external_displays(manager))
101 handle_action = manager->handle_lid_switch_docked;
102 else if (manager->handle_lid_switch_ep != _HANDLE_ACTION_INVALID &&
103 manager_is_on_external_power())
104 handle_action = manager->handle_lid_switch_ep;
106 handle_action = manager->handle_lid_switch;
108 manager_handle_action(manager, INHIBIT_HANDLE_LID_SWITCH, handle_action, manager->lid_switch_ignore_inhibited, is_edge);
111 static int button_recheck(sd_event_source *e, void *userdata) {
112 Button *b = userdata;
115 assert(b->lid_closed);
117 button_lid_switch_handle_action(b->manager, false);
121 static int button_install_check_event_source(Button *b) {
125 /* Install a post handler, so that we keep rechecking as long as the lid is closed. */
127 if (b->check_event_source)
130 r = sd_event_add_post(b->manager->event, &b->check_event_source, button_recheck, b);
134 return sd_event_source_set_priority(b->check_event_source, SD_EVENT_PRIORITY_IDLE+1);
137 static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
138 Button *b = userdata;
139 struct input_event ev;
146 l = read(b->fd, &ev, sizeof(ev));
148 return errno != EAGAIN ? -errno : 0;
149 if ((size_t) l < sizeof(ev))
152 if (ev.type == EV_KEY && ev.value > 0) {
159 LOG_MESSAGE("Power key pressed."),
160 "MESSAGE_ID=" SD_MESSAGE_POWER_KEY_STR);
162 manager_handle_action(b->manager, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true);
165 /* The kernel is a bit confused here:
167 KEY_SLEEP = suspend-to-ram, which everybody else calls "suspend"
168 KEY_SUSPEND = suspend-to-disk, which everybody else calls "hibernate"
173 LOG_MESSAGE("Suspend key pressed."),
174 "MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_STR);
176 manager_handle_action(b->manager, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true);
181 LOG_MESSAGE("Hibernate key pressed."),
182 "MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_STR);
184 manager_handle_action(b->manager, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true);
188 } else if (ev.type == EV_SW && ev.value > 0) {
190 if (ev.code == SW_LID) {
192 LOG_MESSAGE("Lid closed."),
193 "MESSAGE_ID=" SD_MESSAGE_LID_CLOSED_STR);
195 b->lid_closed = true;
196 button_lid_switch_handle_action(b->manager, true);
197 button_install_check_event_source(b);
199 } else if (ev.code == SW_DOCK) {
201 LOG_MESSAGE("System docked."),
202 "MESSAGE_ID=" SD_MESSAGE_SYSTEM_DOCKED_STR);
207 } else if (ev.type == EV_SW && ev.value == 0) {
209 if (ev.code == SW_LID) {
211 LOG_MESSAGE("Lid opened."),
212 "MESSAGE_ID=" SD_MESSAGE_LID_OPENED_STR);
214 b->lid_closed = false;
215 b->check_event_source = sd_event_source_unref(b->check_event_source);
217 } else if (ev.code == SW_DOCK) {
219 LOG_MESSAGE("System undocked."),
220 "MESSAGE_ID=" SD_MESSAGE_SYSTEM_UNDOCKED_STR);
229 static int button_suitable(Button *b) {
230 unsigned long types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1];
235 if (ioctl(b->fd, EVIOCGBIT(EV_SYN, sizeof(types)), types) < 0)
238 if (bitset_get(types, EV_KEY)) {
239 unsigned long keys[CONST_MAX4(KEY_POWER, KEY_POWER2, KEY_SLEEP, KEY_SUSPEND)/ULONG_BITS+1];
241 if (ioctl(b->fd, EVIOCGBIT(EV_KEY, sizeof(keys)), keys) < 0)
244 if (bitset_get(keys, KEY_POWER) ||
245 bitset_get(keys, KEY_POWER2) ||
246 bitset_get(keys, KEY_SLEEP) ||
247 bitset_get(keys, KEY_SUSPEND))
251 if (bitset_get(types, EV_SW)) {
252 unsigned long switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1];
254 if (ioctl(b->fd, EVIOCGBIT(EV_SW, sizeof(switches)), switches) < 0)
257 if (bitset_get(switches, SW_LID) ||
258 bitset_get(switches, SW_DOCK))
265 static int button_set_mask(Button *b) {
267 types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1] = {},
268 keys[CONST_MAX4(KEY_POWER, KEY_POWER2, KEY_SLEEP, KEY_SUSPEND)/ULONG_BITS+1] = {},
269 switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1] = {};
270 struct input_mask mask;
275 bitset_put(types, EV_KEY);
276 bitset_put(types, EV_SW);
278 mask = (struct input_mask) {
280 .codes_size = sizeof(types),
281 .codes_ptr = PTR_TO_UINT64(types),
284 if (ioctl(b->fd, EVIOCSMASK, &mask) < 0)
285 /* Log only at debug level if the kernel doesn't do EVIOCSMASK yet */
286 return log_full_errno(IN_SET(errno, ENOTTY, EOPNOTSUPP, EINVAL) ? LOG_DEBUG : LOG_WARNING,
287 errno, "Failed to set EV_SYN event mask on /dev/input/%s: %m", b->name);
289 bitset_put(keys, KEY_POWER);
290 bitset_put(keys, KEY_POWER2);
291 bitset_put(keys, KEY_SLEEP);
292 bitset_put(keys, KEY_SUSPEND);
294 mask = (struct input_mask) {
296 .codes_size = sizeof(keys),
297 .codes_ptr = PTR_TO_UINT64(keys),
300 if (ioctl(b->fd, EVIOCSMASK, &mask) < 0)
301 return log_warning_errno(errno, "Failed to set EV_KEY event mask on /dev/input/%s: %m", b->name);
303 bitset_put(switches, SW_LID);
304 bitset_put(switches, SW_DOCK);
306 mask = (struct input_mask) {
308 .codes_size = sizeof(switches),
309 .codes_ptr = PTR_TO_UINT64(switches),
312 if (ioctl(b->fd, EVIOCSMASK, &mask) < 0)
313 return log_warning_errno(errno, "Failed to set EV_SW event mask on /dev/input/%s: %m", b->name);
318 int button_open(Button *b) {
324 b->fd = safe_close(b->fd);
326 p = strjoina("/dev/input/", b->name);
328 b->fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
330 return log_warning_errno(errno, "Failed to open %s: %m", p);
332 r = button_suitable(b);
334 return log_warning_errno(r, "Failed to determine whether input device is relevant to us: %m");
336 log_debug("Device %s does not expose keys or switches relevant to us, ignoring.", p);
337 return -EADDRNOTAVAIL;
340 if (ioctl(b->fd, EVIOCGNAME(sizeof(name)), name) < 0) {
341 r = log_error_errno(errno, "Failed to get input name: %m");
345 (void) button_set_mask(b);
347 r = sd_event_add_io(b->manager->event, &b->io_event_source, b->fd, EPOLLIN, button_dispatch, b);
349 log_error_errno(r, "Failed to add button event: %m");
353 log_info("Watching system buttons on /dev/input/%s (%s)", b->name, name);
358 b->fd = safe_close(b->fd);
362 int button_check_switches(Button *b) {
363 unsigned long switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1] = {};
369 if (ioctl(b->fd, EVIOCGSW(sizeof(switches)), switches) < 0)
372 b->lid_closed = bitset_get(switches, SW_LID);
373 b->docked = bitset_get(switches, SW_DOCK);
376 button_install_check_event_source(b);