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