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