2 This file is part of systemd.
4 Copyright 2012 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/ioctl.h>
25 #include <linux/input.h>
27 #include "sd-messages.h"
29 #include "alloc-util.h"
31 #include "logind-button.h"
32 #include "string-util.h"
35 #define CONST_MAX4(a, b, c, d) CONST_MAX(CONST_MAX(a, b), CONST_MAX(c, d))
37 #define ULONG_BITS (sizeof(unsigned long)*8)
39 static bool bitset_get(const unsigned long *bits, unsigned i) {
40 return (bits[i / ULONG_BITS] >> (i % ULONG_BITS)) & 1UL;
43 static void bitset_put(unsigned long *bits, unsigned i) {
44 bits[i / ULONG_BITS] |= (unsigned long) 1 << (i % ULONG_BITS);
47 Button* button_new(Manager *m, const char *name) {
57 b->name = strdup(name);
61 if (hashmap_put(m->buttons, b->name, b) < 0) {
72 void button_free(Button *b) {
75 hashmap_remove(b->manager->buttons, b->name);
77 sd_event_source_unref(b->io_event_source);
78 sd_event_source_unref(b->check_event_source);
81 /* If the device has been unplugged close() returns
82 * ENODEV, let's ignore this, hence we don't use
91 int button_set_seat(Button *b, const char *sn) {
107 static void button_lid_switch_handle_action(Manager *manager, bool is_edge) {
108 HandleAction handle_action;
112 /* If we are docked, handle the lid switch differently */
113 if (manager_is_docked_or_external_displays(manager))
114 handle_action = manager->handle_lid_switch_docked;
116 handle_action = manager->handle_lid_switch;
118 manager_handle_action(manager, INHIBIT_HANDLE_LID_SWITCH, handle_action, manager->lid_switch_ignore_inhibited, is_edge);
121 static int button_recheck(sd_event_source *e, void *userdata) {
122 Button *b = userdata;
125 assert(b->lid_closed);
127 button_lid_switch_handle_action(b->manager, false);
131 static int button_install_check_event_source(Button *b) {
135 /* Install a post handler, so that we keep rechecking as long as the lid is closed. */
137 if (b->check_event_source)
140 r = sd_event_add_post(b->manager->event, &b->check_event_source, button_recheck, b);
144 return sd_event_source_set_priority(b->check_event_source, SD_EVENT_PRIORITY_IDLE+1);
147 static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
148 Button *b = userdata;
149 struct input_event ev;
156 l = read(b->fd, &ev, sizeof(ev));
158 return errno != EAGAIN ? -errno : 0;
159 if ((size_t) l < sizeof(ev))
162 if (ev.type == EV_KEY && ev.value > 0) {
169 LOG_MESSAGE("Power key pressed."),
170 "MESSAGE_ID=" SD_MESSAGE_POWER_KEY_STR,
173 manager_handle_action(b->manager, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true);
176 /* The kernel is a bit confused here:
178 KEY_SLEEP = suspend-to-ram, which everybody else calls "suspend"
179 KEY_SUSPEND = suspend-to-disk, which everybody else calls "hibernate"
184 LOG_MESSAGE("Suspend key pressed."),
185 "MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_STR,
188 manager_handle_action(b->manager, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true);
193 LOG_MESSAGE("Hibernate key pressed."),
194 "MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_STR,
197 manager_handle_action(b->manager, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true);
201 } else if (ev.type == EV_SW && ev.value > 0) {
203 if (ev.code == SW_LID) {
205 LOG_MESSAGE("Lid closed."),
206 "MESSAGE_ID=" SD_MESSAGE_LID_CLOSED_STR,
209 b->lid_closed = true;
210 button_lid_switch_handle_action(b->manager, true);
211 button_install_check_event_source(b);
213 } else if (ev.code == SW_DOCK) {
215 LOG_MESSAGE("System docked."),
216 "MESSAGE_ID=" SD_MESSAGE_SYSTEM_DOCKED_STR,
222 } else if (ev.type == EV_SW && ev.value == 0) {
224 if (ev.code == SW_LID) {
226 LOG_MESSAGE("Lid opened."),
227 "MESSAGE_ID=" SD_MESSAGE_LID_OPENED_STR,
230 b->lid_closed = false;
231 b->check_event_source = sd_event_source_unref(b->check_event_source);
233 } else if (ev.code == SW_DOCK) {
235 LOG_MESSAGE("System undocked."),
236 "MESSAGE_ID=" SD_MESSAGE_SYSTEM_UNDOCKED_STR,
246 static int button_suitable(Button *b) {
247 unsigned long types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1];
252 if (ioctl(b->fd, EVIOCGBIT(EV_SYN, sizeof(types)), types) < 0)
255 if (bitset_get(types, EV_KEY)) {
256 unsigned long keys[CONST_MAX4(KEY_POWER, KEY_POWER2, KEY_SLEEP, KEY_SUSPEND)/ULONG_BITS+1];
258 if (ioctl(b->fd, EVIOCGBIT(EV_KEY, sizeof(keys)), keys) < 0)
261 if (bitset_get(keys, KEY_POWER) ||
262 bitset_get(keys, KEY_POWER2) ||
263 bitset_get(keys, KEY_SLEEP) ||
264 bitset_get(keys, KEY_SUSPEND))
268 if (bitset_get(types, EV_SW)) {
269 unsigned long switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1];
271 if (ioctl(b->fd, EVIOCGBIT(EV_SW, sizeof(switches)), switches) < 0)
274 if (bitset_get(switches, SW_LID) ||
275 bitset_get(switches, SW_DOCK))
282 static int button_set_mask(Button *b) {
284 types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1] = {},
285 keys[CONST_MAX4(KEY_POWER, KEY_POWER2, KEY_SLEEP, KEY_SUSPEND)/ULONG_BITS+1] = {},
286 switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1] = {};
287 struct input_mask mask;
292 bitset_put(types, EV_KEY);
293 bitset_put(types, EV_SW);
295 mask = (struct input_mask) {
297 .codes_size = sizeof(types),
298 .codes_ptr = PTR_TO_UINT64(types),
301 if (ioctl(b->fd, EVIOCSMASK, &mask) < 0)
302 /* Log only at debug level if the kernel doesn't do EVIOCSMASK yet */
303 return log_full_errno(IN_SET(errno, ENOTTY, EOPNOTSUPP, EINVAL) ? LOG_DEBUG : LOG_WARNING,
304 errno, "Failed to set EV_SYN event mask on /dev/input/%s: %m", b->name);
306 bitset_put(keys, KEY_POWER);
307 bitset_put(keys, KEY_POWER2);
308 bitset_put(keys, KEY_SLEEP);
309 bitset_put(keys, KEY_SUSPEND);
311 mask = (struct input_mask) {
313 .codes_size = sizeof(keys),
314 .codes_ptr = PTR_TO_UINT64(keys),
317 if (ioctl(b->fd, EVIOCSMASK, &mask) < 0)
318 return log_warning_errno(errno, "Failed to set EV_KEY event mask on /dev/input/%s: %m", b->name);
320 bitset_put(switches, SW_LID);
321 bitset_put(switches, SW_DOCK);
323 mask = (struct input_mask) {
325 .codes_size = sizeof(switches),
326 .codes_ptr = PTR_TO_UINT64(switches),
329 if (ioctl(b->fd, EVIOCSMASK, &mask) < 0)
330 return log_warning_errno(errno, "Failed to set EV_SW event mask on /dev/input/%s: %m", b->name);
335 int button_open(Button *b) {
341 b->fd = safe_close(b->fd);
343 p = strjoina("/dev/input/", b->name);
345 b->fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
347 return log_warning_errno(errno, "Failed to open %s: %m", p);
349 r = button_suitable(b);
351 return log_warning_errno(r, "Failed to determine whether input device is relevant to us: %m");
353 log_debug("Device %s does not expose keys or switches relevant to us, ignoring.", p);
354 return -EADDRNOTAVAIL;
357 if (ioctl(b->fd, EVIOCGNAME(sizeof(name)), name) < 0) {
358 r = log_error_errno(errno, "Failed to get input name: %m");
362 (void) button_set_mask(b);
364 r = sd_event_add_io(b->manager->event, &b->io_event_source, b->fd, EPOLLIN, button_dispatch, b);
366 log_error_errno(r, "Failed to add button event: %m");
370 log_info("Watching system buttons on /dev/input/%s (%s)", b->name, name);
375 b->fd = safe_close(b->fd);
379 int button_check_switches(Button *b) {
380 unsigned long switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1] = {};
386 if (ioctl(b->fd, EVIOCGSW(sizeof(switches)), switches) < 0)
389 b->lid_closed = bitset_get(switches, SW_LID);
390 b->docked = bitset_get(switches, SW_DOCK);
393 button_install_check_event_source(b);