]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/login/logind-button.c
homed: make it easier to run multiple instances of homed
[thirdparty/systemd.git] / src / login / logind-button.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
e9d21f24 2
e9d21f24
LP
3#include <errno.h>
4#include <fcntl.h>
5#include <sys/ioctl.h>
6#include <unistd.h>
e9d21f24 7
cc377381 8#include "sd-messages.h"
07630cea 9
b5efdb8a 10#include "alloc-util.h"
3ffd4af2
LP
11#include "fd-util.h"
12#include "logind-button.h"
36dd5ffd 13#include "missing_input.h"
07630cea 14#include "string-util.h"
e9d21f24 15#include "util.h"
e9d21f24 16
adbb2b6a 17#define CONST_MAX5(a, b, c, d, e) CONST_MAX(CONST_MAX(a, b), CONST_MAX(CONST_MAX(c, d), e))
d5dd44b0
LP
18
19#define ULONG_BITS (sizeof(unsigned long)*8)
20
21static bool bitset_get(const unsigned long *bits, unsigned i) {
22 return (bits[i / ULONG_BITS] >> (i % ULONG_BITS)) & 1UL;
23}
24
25static void bitset_put(unsigned long *bits, unsigned i) {
26 bits[i / ULONG_BITS] |= (unsigned long) 1 << (i % ULONG_BITS);
27}
28
e9d21f24
LP
29Button* button_new(Manager *m, const char *name) {
30 Button *b;
31
32 assert(m);
33 assert(name);
34
35 b = new0(Button, 1);
36 if (!b)
37 return NULL;
38
39 b->name = strdup(name);
6b430fdb
ZJS
40 if (!b->name)
41 return mfree(b);
e9d21f24
LP
42
43 if (hashmap_put(m->buttons, b->name, b) < 0) {
44 free(b->name);
6b430fdb 45 return mfree(b);
e9d21f24
LP
46 }
47
48 b->manager = m;
49 b->fd = -1;
50
51 return b;
52}
53
54void button_free(Button *b) {
55 assert(b);
56
57 hashmap_remove(b->manager->buttons, b->name);
58
ed4ba7e4
LP
59 sd_event_source_unref(b->io_event_source);
60 sd_event_source_unref(b->check_event_source);
c30a0c62 61
ece174c5 62 if (b->fd >= 0)
c30a0c62
LP
63 /* If the device has been unplugged close() returns
64 * ENODEV, let's ignore this, hence we don't use
03e334a1 65 * safe_close() */
4fba5796 66 (void) close(b->fd);
e9d21f24
LP
67
68 free(b->name);
69 free(b->seat);
70 free(b);
71}
72
73int button_set_seat(Button *b, const char *sn) {
74 char *s;
75
76 assert(b);
77 assert(sn);
78
79 s = strdup(sn);
80 if (!s)
81 return -ENOMEM;
82
83 free(b->seat);
84 b->seat = s;
85
86 return 0;
87}
88
3c56cab4
BW
89static void button_lid_switch_handle_action(Manager *manager, bool is_edge) {
90 HandleAction handle_action;
91
92 assert(manager);
93
e25937a3
SF
94 /* If we are docked or on external power, handle the lid switch
95 * differently */
602a41c2 96 if (manager_is_docked_or_external_displays(manager))
3c56cab4 97 handle_action = manager->handle_lid_switch_docked;
e25937a3
SF
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;
3c56cab4
BW
101 else
102 handle_action = manager->handle_lid_switch;
103
104 manager_handle_action(manager, INHIBIT_HANDLE_LID_SWITCH, handle_action, manager->lid_switch_ignore_inhibited, is_edge);
105}
106
ed4ba7e4
LP
107static int button_recheck(sd_event_source *e, void *userdata) {
108 Button *b = userdata;
beaafb2e 109
ed4ba7e4
LP
110 assert(b);
111 assert(b->lid_closed);
112
3c56cab4 113 button_lid_switch_handle_action(b->manager, false);
ed4ba7e4
LP
114 return 1;
115}
e9d21f24 116
ed4ba7e4
LP
117static int button_install_check_event_source(Button *b) {
118 int r;
e9d21f24
LP
119 assert(b);
120
ed4ba7e4 121 /* Install a post handler, so that we keep rechecking as long as the lid is closed. */
6de0e0e5 122
ed4ba7e4
LP
123 if (b->check_event_source)
124 return 0;
125
126 r = sd_event_add_post(b->manager->event, &b->check_event_source, button_recheck, b);
127 if (r < 0)
128 return r;
129
130 return sd_event_source_set_priority(b->check_event_source, SD_EVENT_PRIORITY_IDLE+1);
e9d21f24
LP
131}
132
cc377381
LP
133static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
134 Button *b = userdata;
e9d21f24
LP
135 struct input_event ev;
136 ssize_t l;
137
cc377381
LP
138 assert(s);
139 assert(fd == b->fd);
e9d21f24
LP
140 assert(b);
141
142 l = read(b->fd, &ev, sizeof(ev));
143 if (l < 0)
144 return errno != EAGAIN ? -errno : 0;
145 if ((size_t) l < sizeof(ev))
146 return -EIO;
147
e9d21f24
LP
148 if (ev.type == EV_KEY && ev.value > 0) {
149
150 switch (ev.code) {
151
152 case KEY_POWER:
153 case KEY_POWER2:
c485437f 154 log_struct(LOG_INFO,
e2cc6eca 155 LOG_MESSAGE("Power key pressed."),
a1230ff9 156 "MESSAGE_ID=" SD_MESSAGE_POWER_KEY_STR);
7b77ed8c 157
ed4ba7e4 158 manager_handle_action(b->manager, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true);
7b77ed8c 159 break;
e9d21f24 160
adbb2b6a
RM
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.
164 */
165
166 case KEY_RESTART:
167 log_struct(LOG_INFO,
168 LOG_MESSAGE("Reboot key pressed."),
169 "MESSAGE_ID=" SD_MESSAGE_REBOOT_KEY_STR);
170
171 manager_handle_action(b->manager, INHIBIT_HANDLE_REBOOT_KEY, b->manager->handle_reboot_key, b->manager->reboot_key_ignore_inhibited, true);
172 break;
173
174 /* The kernel naming is a bit confusing here:
8e7fd6ad
LP
175
176 KEY_SLEEP = suspend-to-ram, which everybody else calls "suspend"
177 KEY_SUSPEND = suspend-to-disk, which everybody else calls "hibernate"
178 */
179
e9d21f24 180 case KEY_SLEEP:
c485437f 181 log_struct(LOG_INFO,
e2cc6eca 182 LOG_MESSAGE("Suspend key pressed."),
a1230ff9 183 "MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_STR);
7b77ed8c 184
ed4ba7e4 185 manager_handle_action(b->manager, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true);
7b77ed8c 186 break;
e9d21f24 187
8e7fd6ad 188 case KEY_SUSPEND:
c485437f 189 log_struct(LOG_INFO,
e2cc6eca 190 LOG_MESSAGE("Hibernate key pressed."),
a1230ff9 191 "MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_STR);
7b77ed8c 192
ed4ba7e4 193 manager_handle_action(b->manager, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true);
7b77ed8c 194 break;
e9d21f24 195 }
8e7fd6ad 196
e9d21f24
LP
197 } else if (ev.type == EV_SW && ev.value > 0) {
198
7b77ed8c 199 if (ev.code == SW_LID) {
c485437f 200 log_struct(LOG_INFO,
e2cc6eca 201 LOG_MESSAGE("Lid closed."),
a1230ff9 202 "MESSAGE_ID=" SD_MESSAGE_LID_CLOSED_STR);
65b51162 203
ed4ba7e4 204 b->lid_closed = true;
3c56cab4 205 button_lid_switch_handle_action(b->manager, true);
ed4ba7e4 206 button_install_check_event_source(b);
2d62c530
LP
207
208 } else if (ev.code == SW_DOCK) {
209 log_struct(LOG_INFO,
e2cc6eca 210 LOG_MESSAGE("System docked."),
a1230ff9 211 "MESSAGE_ID=" SD_MESSAGE_SYSTEM_DOCKED_STR);
2d62c530
LP
212
213 b->docked = true;
65b51162
LP
214 }
215
216 } else if (ev.type == EV_SW && ev.value == 0) {
217
7b77ed8c 218 if (ev.code == SW_LID) {
c485437f 219 log_struct(LOG_INFO,
e2cc6eca 220 LOG_MESSAGE("Lid opened."),
a1230ff9 221 "MESSAGE_ID=" SD_MESSAGE_LID_OPENED_STR);
7b77ed8c 222
ed4ba7e4
LP
223 b->lid_closed = false;
224 b->check_event_source = sd_event_source_unref(b->check_event_source);
2d62c530
LP
225
226 } else if (ev.code == SW_DOCK) {
227 log_struct(LOG_INFO,
e2cc6eca 228 LOG_MESSAGE("System undocked."),
a1230ff9 229 "MESSAGE_ID=" SD_MESSAGE_SYSTEM_UNDOCKED_STR);
2d62c530
LP
230
231 b->docked = false;
e9d21f24
LP
232 }
233 }
234
235 return 0;
236}
237
92c60579 238static int button_suitable(int fd) {
2546b70a
LP
239 unsigned long types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1];
240
92c60579 241 assert(fd >= 0);
2546b70a 242
92c60579 243 if (ioctl(fd, EVIOCGBIT(EV_SYN, sizeof types), types) < 0)
2546b70a
LP
244 return -errno;
245
246 if (bitset_get(types, EV_KEY)) {
adbb2b6a 247 unsigned long keys[CONST_MAX5(KEY_POWER, KEY_POWER2, KEY_SLEEP, KEY_SUSPEND, KEY_RESTART)/ULONG_BITS+1];
2546b70a 248
92c60579 249 if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof keys), keys) < 0)
2546b70a
LP
250 return -errno;
251
252 if (bitset_get(keys, KEY_POWER) ||
253 bitset_get(keys, KEY_POWER2) ||
254 bitset_get(keys, KEY_SLEEP) ||
adbb2b6a
RM
255 bitset_get(keys, KEY_SUSPEND) ||
256 bitset_get(keys, KEY_RESTART))
2546b70a
LP
257 return true;
258 }
259
260 if (bitset_get(types, EV_SW)) {
261 unsigned long switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1];
262
92c60579 263 if (ioctl(fd, EVIOCGBIT(EV_SW, sizeof switches), switches) < 0)
2546b70a
LP
264 return -errno;
265
266 if (bitset_get(switches, SW_LID) ||
267 bitset_get(switches, SW_DOCK))
268 return true;
269 }
270
271 return false;
272}
273
92c60579 274static int button_set_mask(const char *name, int fd) {
d5dd44b0
LP
275 unsigned long
276 types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1] = {},
adbb2b6a 277 keys[CONST_MAX5(KEY_POWER, KEY_POWER2, KEY_SLEEP, KEY_SUSPEND, KEY_RESTART)/ULONG_BITS+1] = {},
d5dd44b0
LP
278 switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1] = {};
279 struct input_mask mask;
280
92c60579
ZJS
281 assert(name);
282 assert(fd >= 0);
d5dd44b0
LP
283
284 bitset_put(types, EV_KEY);
285 bitset_put(types, EV_SW);
286
287 mask = (struct input_mask) {
288 .type = EV_SYN,
289 .codes_size = sizeof(types),
290 .codes_ptr = PTR_TO_UINT64(types),
291 };
292
92c60579 293 if (ioctl(fd, EVIOCSMASK, &mask) < 0)
d5dd44b0
LP
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,
92c60579 296 errno, "Failed to set EV_SYN event mask on /dev/input/%s: %m", name);
d5dd44b0
LP
297
298 bitset_put(keys, KEY_POWER);
299 bitset_put(keys, KEY_POWER2);
300 bitset_put(keys, KEY_SLEEP);
301 bitset_put(keys, KEY_SUSPEND);
adbb2b6a 302 bitset_put(keys, KEY_RESTART);
d5dd44b0
LP
303
304 mask = (struct input_mask) {
305 .type = EV_KEY,
306 .codes_size = sizeof(keys),
307 .codes_ptr = PTR_TO_UINT64(keys),
308 };
309
92c60579
ZJS
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);
d5dd44b0
LP
312
313 bitset_put(switches, SW_LID);
314 bitset_put(switches, SW_DOCK);
315
316 mask = (struct input_mask) {
317 .type = EV_SW,
318 .codes_size = sizeof(switches),
319 .codes_ptr = PTR_TO_UINT64(switches),
320 };
321
92c60579
ZJS
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);
d5dd44b0
LP
324
325 return 0;
326}
327
cc377381 328int button_open(Button *b) {
92c60579
ZJS
329 _cleanup_close_ int fd = -1;
330 const char *p;
331 char name[256];
cc377381
LP
332 int r;
333
334 assert(b);
335
7f6e12b0 336 b->fd = safe_close(b->fd);
cc377381 337
63c372cb 338 p = strjoina("/dev/input/", b->name);
cc377381 339
92c60579
ZJS
340 fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
341 if (fd < 0)
2546b70a
LP
342 return log_warning_errno(errno, "Failed to open %s: %m", p);
343
92c60579 344 r = button_suitable(fd);
2546b70a 345 if (r < 0)
92c60579 346 return log_warning_errno(r, "Failed to determine whether input device %s is relevant to us: %m", p);
baaa35ad
ZJS
347 if (r == 0)
348 return log_debug_errno(SYNTHETIC_ERRNO(EADDRNOTAVAIL),
92c60579 349 "Device %s does not expose keys or switches relevant to us, ignoring.", p);
cc377381 350
92c60579
ZJS
351 if (ioctl(fd, EVIOCGNAME(sizeof name), name) < 0)
352 return log_error_errno(errno, "Failed to get input name for %s: %m", p);
cc377381 353
92c60579 354 (void) button_set_mask(b->name, fd);
b518e026 355
b2774a3a 356 b->io_event_source = sd_event_source_unref(b->io_event_source);
12e98242 357 r = sd_event_add_io(b->manager->event, &b->io_event_source, fd, EPOLLIN, button_dispatch, b);
92c60579
ZJS
358 if (r < 0)
359 return log_error_errno(r, "Failed to add button event for %s: %m", p);
cc377381 360
92c60579
ZJS
361 b->fd = TAKE_FD(fd);
362 log_info("Watching system buttons on %s (%s)", p, name);
cc377381 363 return 0;
cc377381
LP
364}
365
2d62c530 366int button_check_switches(Button *b) {
d5dd44b0 367 unsigned long switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1] = {};
65b51162
LP
368 assert(b);
369
ed4ba7e4
LP
370 if (b->fd < 0)
371 return -EINVAL;
372
373 if (ioctl(b->fd, EVIOCGSW(sizeof(switches)), switches) < 0)
374 return -errno;
65b51162 375
d5dd44b0
LP
376 b->lid_closed = bitset_get(switches, SW_LID);
377 b->docked = bitset_get(switches, SW_DOCK);
ed4ba7e4 378
2d62c530 379 if (b->lid_closed)
ed4ba7e4 380 button_install_check_event_source(b);
ed4ba7e4
LP
381
382 return 0;
65b51162 383}