chiark / gitweb /
tree-wide: drop license boilerplate
[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                                    NULL);
164
165                         manager_handle_action(b->manager, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true);
166                         break;
167
168                 /* The kernel is a bit confused here:
169
170                    KEY_SLEEP   = suspend-to-ram, which everybody else calls "suspend"
171                    KEY_SUSPEND = suspend-to-disk, which everybody else calls "hibernate"
172                 */
173
174                 case KEY_SLEEP:
175                         log_struct(LOG_INFO,
176                                    LOG_MESSAGE("Suspend key pressed."),
177                                    "MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_STR,
178                                    NULL);
179
180                         manager_handle_action(b->manager, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true);
181                         break;
182
183                 case KEY_SUSPEND:
184                         log_struct(LOG_INFO,
185                                    LOG_MESSAGE("Hibernate key pressed."),
186                                    "MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_STR,
187                                    NULL);
188
189                         manager_handle_action(b->manager, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true);
190                         break;
191                 }
192
193         } else if (ev.type == EV_SW && ev.value > 0) {
194
195                 if (ev.code == SW_LID) {
196                         log_struct(LOG_INFO,
197                                    LOG_MESSAGE("Lid closed."),
198                                    "MESSAGE_ID=" SD_MESSAGE_LID_CLOSED_STR,
199                                    NULL);
200
201                         b->lid_closed = true;
202                         button_lid_switch_handle_action(b->manager, true);
203                         button_install_check_event_source(b);
204
205                 } else if (ev.code == SW_DOCK) {
206                         log_struct(LOG_INFO,
207                                    LOG_MESSAGE("System docked."),
208                                    "MESSAGE_ID=" SD_MESSAGE_SYSTEM_DOCKED_STR,
209                                    NULL);
210
211                         b->docked = true;
212                 }
213
214         } else if (ev.type == EV_SW && ev.value == 0) {
215
216                 if (ev.code == SW_LID) {
217                         log_struct(LOG_INFO,
218                                    LOG_MESSAGE("Lid opened."),
219                                    "MESSAGE_ID=" SD_MESSAGE_LID_OPENED_STR,
220                                    NULL);
221
222                         b->lid_closed = false;
223                         b->check_event_source = sd_event_source_unref(b->check_event_source);
224
225                 } else if (ev.code == SW_DOCK) {
226                         log_struct(LOG_INFO,
227                                    LOG_MESSAGE("System undocked."),
228                                    "MESSAGE_ID=" SD_MESSAGE_SYSTEM_UNDOCKED_STR,
229                                    NULL);
230
231                         b->docked = false;
232                 }
233         }
234
235         return 0;
236 }
237
238 static int button_suitable(Button *b) {
239         unsigned long types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1];
240
241         assert(b);
242         assert(b->fd);
243
244         if (ioctl(b->fd, EVIOCGBIT(EV_SYN, sizeof(types)), types) < 0)
245                 return -errno;
246
247         if (bitset_get(types, EV_KEY)) {
248                 unsigned long keys[CONST_MAX4(KEY_POWER, KEY_POWER2, KEY_SLEEP, KEY_SUSPEND)/ULONG_BITS+1];
249
250                 if (ioctl(b->fd, EVIOCGBIT(EV_KEY, sizeof(keys)), keys) < 0)
251                         return -errno;
252
253                 if (bitset_get(keys, KEY_POWER) ||
254                     bitset_get(keys, KEY_POWER2) ||
255                     bitset_get(keys, KEY_SLEEP) ||
256                     bitset_get(keys, KEY_SUSPEND))
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
263                 if (ioctl(b->fd, EVIOCGBIT(EV_SW, sizeof(switches)), switches) < 0)
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
274 static int button_set_mask(Button *b) {
275         unsigned long
276                 types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1] = {},
277                 keys[CONST_MAX4(KEY_POWER, KEY_POWER2, KEY_SLEEP, KEY_SUSPEND)/ULONG_BITS+1] = {},
278                 switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1] = {};
279         struct input_mask mask;
280
281         assert(b);
282         assert(b->fd >= 0);
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
293         if (ioctl(b->fd, EVIOCSMASK, &mask) < 0)
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,
296                                       errno, "Failed to set EV_SYN event mask on /dev/input/%s: %m", b->name);
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);
302
303         mask = (struct input_mask) {
304                 .type = EV_KEY,
305                 .codes_size = sizeof(keys),
306                 .codes_ptr = PTR_TO_UINT64(keys),
307         };
308
309         if (ioctl(b->fd, EVIOCSMASK, &mask) < 0)
310                 return log_warning_errno(errno, "Failed to set EV_KEY event mask on /dev/input/%s: %m", b->name);
311
312         bitset_put(switches, SW_LID);
313         bitset_put(switches, SW_DOCK);
314
315         mask = (struct input_mask) {
316                 .type = EV_SW,
317                 .codes_size = sizeof(switches),
318                 .codes_ptr = PTR_TO_UINT64(switches),
319         };
320
321         if (ioctl(b->fd, EVIOCSMASK, &mask) < 0)
322                 return log_warning_errno(errno, "Failed to set EV_SW event mask on /dev/input/%s: %m", b->name);
323
324         return 0;
325 }
326
327 int button_open(Button *b) {
328         char *p, name[256];
329         int r;
330
331         assert(b);
332
333         b->fd = safe_close(b->fd);
334
335         p = strjoina("/dev/input/", b->name);
336
337         b->fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
338         if (b->fd < 0)
339                 return log_warning_errno(errno, "Failed to open %s: %m", p);
340
341         r = button_suitable(b);
342         if (r < 0)
343                 return log_warning_errno(r, "Failed to determine whether input device is relevant to us: %m");
344         if (r == 0) {
345                 log_debug("Device %s does not expose keys or switches relevant to us, ignoring.", p);
346                 return -EADDRNOTAVAIL;
347         }
348
349         if (ioctl(b->fd, EVIOCGNAME(sizeof(name)), name) < 0) {
350                 r = log_error_errno(errno, "Failed to get input name: %m");
351                 goto fail;
352         }
353
354         (void) button_set_mask(b);
355
356         r = sd_event_add_io(b->manager->event, &b->io_event_source, b->fd, EPOLLIN, button_dispatch, b);
357         if (r < 0) {
358                 log_error_errno(r, "Failed to add button event: %m");
359                 goto fail;
360         }
361
362         log_info("Watching system buttons on /dev/input/%s (%s)", b->name, name);
363
364         return 0;
365
366 fail:
367         b->fd = safe_close(b->fd);
368         return r;
369 }
370
371 int button_check_switches(Button *b) {
372         unsigned long switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1] = {};
373         assert(b);
374
375         if (b->fd < 0)
376                 return -EINVAL;
377
378         if (ioctl(b->fd, EVIOCGSW(sizeof(switches)), switches) < 0)
379                 return -errno;
380
381         b->lid_closed = bitset_get(switches, SW_LID);
382         b->docked = bitset_get(switches, SW_DOCK);
383
384         if (b->lid_closed)
385                 button_install_check_event_source(b);
386
387         return 0;
388 }