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