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