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