1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "sd-messages.h"
10 #include "alloc-util.h"
12 #include "logind-button.h"
13 #include "missing_input.h"
14 #include "string-util.h"
17 #define CONST_MAX4(a, b, c, d) CONST_MAX(CONST_MAX(a, b), CONST_MAX(c, d))
19 #define ULONG_BITS (sizeof(unsigned long)*8)
21 static bool bitset_get(const unsigned long *bits
, unsigned i
) {
22 return (bits
[i
/ ULONG_BITS
] >> (i
% ULONG_BITS
)) & 1UL;
25 static void bitset_put(unsigned long *bits
, unsigned i
) {
26 bits
[i
/ ULONG_BITS
] |= (unsigned long) 1 << (i
% ULONG_BITS
);
29 Button
* button_new(Manager
*m
, const char *name
) {
39 b
->name
= strdup(name
);
43 if (hashmap_put(m
->buttons
, b
->name
, b
) < 0) {
54 void button_free(Button
*b
) {
57 hashmap_remove(b
->manager
->buttons
, b
->name
);
59 sd_event_source_unref(b
->io_event_source
);
60 sd_event_source_unref(b
->check_event_source
);
63 /* If the device has been unplugged close() returns
64 * ENODEV, let's ignore this, hence we don't use
73 int button_set_seat(Button
*b
, const char *sn
) {
89 static void button_lid_switch_handle_action(Manager
*manager
, bool is_edge
) {
90 HandleAction handle_action
;
94 /* If we are docked or on external power, handle the lid switch
96 if (manager_is_docked_or_external_displays(manager
))
97 handle_action
= manager
->handle_lid_switch_docked
;
98 else if (manager
->handle_lid_switch_ep
!= _HANDLE_ACTION_INVALID
&&
99 manager_is_on_external_power())
100 handle_action
= manager
->handle_lid_switch_ep
;
102 handle_action
= manager
->handle_lid_switch
;
104 manager_handle_action(manager
, INHIBIT_HANDLE_LID_SWITCH
, handle_action
, manager
->lid_switch_ignore_inhibited
, is_edge
);
107 static int button_recheck(sd_event_source
*e
, void *userdata
) {
108 Button
*b
= userdata
;
111 assert(b
->lid_closed
);
113 button_lid_switch_handle_action(b
->manager
, false);
117 static int button_install_check_event_source(Button
*b
) {
121 /* Install a post handler, so that we keep rechecking as long as the lid is closed. */
123 if (b
->check_event_source
)
126 r
= sd_event_add_post(b
->manager
->event
, &b
->check_event_source
, button_recheck
, b
);
130 return sd_event_source_set_priority(b
->check_event_source
, SD_EVENT_PRIORITY_IDLE
+1);
133 static int button_dispatch(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
134 Button
*b
= userdata
;
135 struct input_event ev
;
142 l
= read(b
->fd
, &ev
, sizeof(ev
));
144 return errno
!= EAGAIN
? -errno
: 0;
145 if ((size_t) l
< sizeof(ev
))
148 if (ev
.type
== EV_KEY
&& ev
.value
> 0) {
155 LOG_MESSAGE("Power key pressed."),
156 "MESSAGE_ID=" SD_MESSAGE_POWER_KEY_STR
);
158 manager_handle_action(b
->manager
, INHIBIT_HANDLE_POWER_KEY
, b
->manager
->handle_power_key
, b
->manager
->power_key_ignore_inhibited
, true);
161 /* The kernel is a bit confused here:
163 KEY_SLEEP = suspend-to-ram, which everybody else calls "suspend"
164 KEY_SUSPEND = suspend-to-disk, which everybody else calls "hibernate"
169 LOG_MESSAGE("Suspend key pressed."),
170 "MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_STR
);
172 manager_handle_action(b
->manager
, INHIBIT_HANDLE_SUSPEND_KEY
, b
->manager
->handle_suspend_key
, b
->manager
->suspend_key_ignore_inhibited
, true);
177 LOG_MESSAGE("Hibernate key pressed."),
178 "MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_STR
);
180 manager_handle_action(b
->manager
, INHIBIT_HANDLE_HIBERNATE_KEY
, b
->manager
->handle_hibernate_key
, b
->manager
->hibernate_key_ignore_inhibited
, true);
184 } else if (ev
.type
== EV_SW
&& ev
.value
> 0) {
186 if (ev
.code
== SW_LID
) {
188 LOG_MESSAGE("Lid closed."),
189 "MESSAGE_ID=" SD_MESSAGE_LID_CLOSED_STR
);
191 b
->lid_closed
= true;
192 button_lid_switch_handle_action(b
->manager
, true);
193 button_install_check_event_source(b
);
195 } else if (ev
.code
== SW_DOCK
) {
197 LOG_MESSAGE("System docked."),
198 "MESSAGE_ID=" SD_MESSAGE_SYSTEM_DOCKED_STR
);
203 } else if (ev
.type
== EV_SW
&& ev
.value
== 0) {
205 if (ev
.code
== SW_LID
) {
207 LOG_MESSAGE("Lid opened."),
208 "MESSAGE_ID=" SD_MESSAGE_LID_OPENED_STR
);
210 b
->lid_closed
= false;
211 b
->check_event_source
= sd_event_source_unref(b
->check_event_source
);
213 } else if (ev
.code
== SW_DOCK
) {
215 LOG_MESSAGE("System undocked."),
216 "MESSAGE_ID=" SD_MESSAGE_SYSTEM_UNDOCKED_STR
);
225 static int button_suitable(int fd
) {
226 unsigned long types
[CONST_MAX(EV_KEY
, EV_SW
)/ULONG_BITS
+1];
230 if (ioctl(fd
, EVIOCGBIT(EV_SYN
, sizeof types
), types
) < 0)
233 if (bitset_get(types
, EV_KEY
)) {
234 unsigned long keys
[CONST_MAX4(KEY_POWER
, KEY_POWER2
, KEY_SLEEP
, KEY_SUSPEND
)/ULONG_BITS
+1];
236 if (ioctl(fd
, EVIOCGBIT(EV_KEY
, sizeof keys
), keys
) < 0)
239 if (bitset_get(keys
, KEY_POWER
) ||
240 bitset_get(keys
, KEY_POWER2
) ||
241 bitset_get(keys
, KEY_SLEEP
) ||
242 bitset_get(keys
, KEY_SUSPEND
))
246 if (bitset_get(types
, EV_SW
)) {
247 unsigned long switches
[CONST_MAX(SW_LID
, SW_DOCK
)/ULONG_BITS
+1];
249 if (ioctl(fd
, EVIOCGBIT(EV_SW
, sizeof switches
), switches
) < 0)
252 if (bitset_get(switches
, SW_LID
) ||
253 bitset_get(switches
, SW_DOCK
))
260 static int button_set_mask(const char *name
, int fd
) {
262 types
[CONST_MAX(EV_KEY
, EV_SW
)/ULONG_BITS
+1] = {},
263 keys
[CONST_MAX4(KEY_POWER
, KEY_POWER2
, KEY_SLEEP
, KEY_SUSPEND
)/ULONG_BITS
+1] = {},
264 switches
[CONST_MAX(SW_LID
, SW_DOCK
)/ULONG_BITS
+1] = {};
265 struct input_mask mask
;
270 bitset_put(types
, EV_KEY
);
271 bitset_put(types
, EV_SW
);
273 mask
= (struct input_mask
) {
275 .codes_size
= sizeof(types
),
276 .codes_ptr
= PTR_TO_UINT64(types
),
279 if (ioctl(fd
, EVIOCSMASK
, &mask
) < 0)
280 /* Log only at debug level if the kernel doesn't do EVIOCSMASK yet */
281 return log_full_errno(IN_SET(errno
, ENOTTY
, EOPNOTSUPP
, EINVAL
) ? LOG_DEBUG
: LOG_WARNING
,
282 errno
, "Failed to set EV_SYN event mask on /dev/input/%s: %m", name
);
284 bitset_put(keys
, KEY_POWER
);
285 bitset_put(keys
, KEY_POWER2
);
286 bitset_put(keys
, KEY_SLEEP
);
287 bitset_put(keys
, KEY_SUSPEND
);
289 mask
= (struct input_mask
) {
291 .codes_size
= sizeof(keys
),
292 .codes_ptr
= PTR_TO_UINT64(keys
),
295 if (ioctl(fd
, EVIOCSMASK
, &mask
) < 0)
296 return log_warning_errno(errno
, "Failed to set EV_KEY event mask on /dev/input/%s: %m", name
);
298 bitset_put(switches
, SW_LID
);
299 bitset_put(switches
, SW_DOCK
);
301 mask
= (struct input_mask
) {
303 .codes_size
= sizeof(switches
),
304 .codes_ptr
= PTR_TO_UINT64(switches
),
307 if (ioctl(fd
, EVIOCSMASK
, &mask
) < 0)
308 return log_warning_errno(errno
, "Failed to set EV_SW event mask on /dev/input/%s: %m", name
);
313 int button_open(Button
*b
) {
314 _cleanup_close_
int fd
= -1;
321 b
->fd
= safe_close(b
->fd
);
323 p
= strjoina("/dev/input/", b
->name
);
325 fd
= open(p
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
);
327 return log_warning_errno(errno
, "Failed to open %s: %m", p
);
329 r
= button_suitable(fd
);
331 return log_warning_errno(r
, "Failed to determine whether input device %s is relevant to us: %m", p
);
333 return log_debug_errno(SYNTHETIC_ERRNO(EADDRNOTAVAIL
),
334 "Device %s does not expose keys or switches relevant to us, ignoring.", p
);
336 if (ioctl(fd
, EVIOCGNAME(sizeof name
), name
) < 0)
337 return log_error_errno(errno
, "Failed to get input name for %s: %m", p
);
339 (void) button_set_mask(b
->name
, fd
);
341 b
->io_event_source
= sd_event_source_unref(b
->io_event_source
);
342 r
= sd_event_add_io(b
->manager
->event
, &b
->io_event_source
, fd
, EPOLLIN
, button_dispatch
, b
);
344 return log_error_errno(r
, "Failed to add button event for %s: %m", p
);
347 log_info("Watching system buttons on %s (%s)", p
, name
);
351 int button_check_switches(Button
*b
) {
352 unsigned long switches
[CONST_MAX(SW_LID
, SW_DOCK
)/ULONG_BITS
+1] = {};
358 if (ioctl(b
->fd
, EVIOCGSW(sizeof(switches
)), switches
) < 0)
361 b
->lid_closed
= bitset_get(switches
, SW_LID
);
362 b
->docked
= bitset_get(switches
, SW_DOCK
);
365 button_install_check_event_source(b
);