| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <linux/input.h> |
| 5 |
#include <sys/ioctl.h> |
| 6 |
#include <unistd.h> |
| 7 |
|
| 8 |
#include "sd-event.h" |
| 9 |
#include "sd-messages.h" |
| 10 |
|
| 11 |
#include "alloc-util.h" |
| 12 |
#include "async.h" |
| 13 |
#include "fd-util.h" |
| 14 |
#include "hashmap.h" |
| 15 |
#include "log.h" |
| 16 |
#include "logind.h" |
| 17 |
#include "logind-button.h" |
| 18 |
#include "logind-dbus.h" |
| 19 |
#include "string-util.h" |
| 20 |
|
| 21 |
/* KEY_RESTART is the highest value key in keys_interested. */ |
| 22 |
#define _KEY_MAX_INTERESTED KEY_RESTART |
| 23 |
|
| 24 |
/* Adding more values here may require _KEY_MAX_INTERESTED to be updated, this array must be sorted by key value. */ |
| 25 |
static const int keys_interested[] = { |
| 26 |
KEY_ESC, |
| 27 |
KEY_LEFTCTRL, |
| 28 |
KEY_LEFTSHIFT, |
| 29 |
KEY_RIGHTSHIFT, |
| 30 |
KEY_LEFTALT, |
| 31 |
KEY_RIGHTCTRL, |
| 32 |
KEY_RIGHTALT, |
| 33 |
KEY_POWER, |
| 34 |
KEY_SLEEP, |
| 35 |
KEY_SUSPEND, |
| 36 |
KEY_POWER2, |
| 37 |
KEY_RESTART, |
| 38 |
}; |
| 39 |
|
| 40 |
static const struct { |
| 41 |
int keycode; |
| 42 |
ButtonModifierMask modifier_mask; |
| 43 |
} keycode_modifier_table[] = { |
| 44 |
{ KEY_LEFTSHIFT, BUTTON_MODIFIER_LEFT_SHIFT }, |
| 45 |
{ KEY_RIGHTSHIFT, BUTTON_MODIFIER_RIGHT_SHIFT }, |
| 46 |
{ KEY_LEFTCTRL, BUTTON_MODIFIER_LEFT_CTRL }, |
| 47 |
{ KEY_RIGHTCTRL, BUTTON_MODIFIER_RIGHT_CTRL }, |
| 48 |
{ KEY_LEFTALT, BUTTON_MODIFIER_LEFT_ALT }, |
| 49 |
{ KEY_RIGHTALT, BUTTON_MODIFIER_RIGHT_ALT }, |
| 50 |
}; |
| 51 |
|
| 52 |
#define ULONG_BITS (sizeof(unsigned long)*8) |
| 53 |
|
| 54 |
#define LONG_PRESS_DURATION (5 * USEC_PER_SEC) |
| 55 |
|
| 56 |
static bool bitset_get(const unsigned long *bits, unsigned i) { |
| 57 |
return (bits[i / ULONG_BITS] >> (i % ULONG_BITS)) & 1UL; |
| 58 |
} |
| 59 |
|
| 60 |
static void bitset_put(unsigned long *bits, unsigned i) { |
| 61 |
bits[i / ULONG_BITS] |= (unsigned long) 1 << (i % ULONG_BITS); |
| 62 |
} |
| 63 |
|
| 64 |
Button* button_new(Manager *m, const char *name) { |
| 65 |
Button *b; |
| 66 |
|
| 67 |
assert(m); |
| 68 |
assert(name); |
| 69 |
|
| 70 |
b = new0(Button, 1); |
| 71 |
if (!b) |
| 72 |
return NULL; |
| 73 |
|
| 74 |
b->name = strdup(name); |
| 75 |
if (!b->name) |
| 76 |
return mfree(b); |
| 77 |
|
| 78 |
if (hashmap_put(m->buttons, b->name, b) < 0) { |
| 79 |
free(b->name); |
| 80 |
return mfree(b); |
| 81 |
} |
| 82 |
|
| 83 |
b->button_modifier_mask = BUTTON_MODIFIER_NONE; |
| 84 |
|
| 85 |
b->manager = m; |
| 86 |
b->fd = -EBADF; |
| 87 |
|
| 88 |
return b; |
| 89 |
} |
| 90 |
|
| 91 |
Button *button_free(Button *b) { |
| 92 |
if (!b) |
| 93 |
return NULL; |
| 94 |
|
| 95 |
hashmap_remove(b->manager->buttons, b->name); |
| 96 |
|
| 97 |
sd_event_source_unref(b->io_event_source); |
| 98 |
sd_event_source_unref(b->check_event_source); |
| 99 |
|
| 100 |
asynchronous_close(b->fd); |
| 101 |
|
| 102 |
free(b->name); |
| 103 |
free(b->seat); |
| 104 |
|
| 105 |
return mfree(b); |
| 106 |
} |
| 107 |
|
| 108 |
int button_set_seat(Button *b, const char *sn) { |
| 109 |
assert(b); |
| 110 |
|
| 111 |
return free_and_strdup(&b->seat, sn); |
| 112 |
} |
| 113 |
|
| 114 |
static ButtonModifierMask button_get_keycode_modifier_mask(int keycode) { |
| 115 |
|
| 116 |
assert(keycode > 0); |
| 117 |
|
| 118 |
FOREACH_ELEMENT(i, keycode_modifier_table) |
| 119 |
if (i->keycode == keycode) |
| 120 |
return i->modifier_mask; |
| 121 |
|
| 122 |
return BUTTON_MODIFIER_NONE; |
| 123 |
} |
| 124 |
|
| 125 |
static void button_lid_switch_handle_action(Manager *manager, bool is_edge, const char* seat) { |
| 126 |
HandleAction handle_action; |
| 127 |
|
| 128 |
assert(manager); |
| 129 |
|
| 130 |
/* If we are docked or on external power, handle the lid switch |
| 131 |
* differently */ |
| 132 |
if (manager_is_docked_or_external_displays(manager)) |
| 133 |
handle_action = manager->handle_lid_switch_docked; |
| 134 |
else if (handle_action_valid(manager->handle_lid_switch_ep) && manager_is_on_external_power()) |
| 135 |
handle_action = manager->handle_lid_switch_ep; |
| 136 |
else |
| 137 |
handle_action = manager->handle_lid_switch; |
| 138 |
|
| 139 |
manager_handle_action(manager, INHIBIT_HANDLE_LID_SWITCH, handle_action, manager->lid_switch_ignore_inhibited, is_edge, seat); |
| 140 |
} |
| 141 |
|
| 142 |
static int button_recheck(sd_event_source *e, void *userdata) { |
| 143 |
Button *b = ASSERT_PTR(userdata); |
| 144 |
|
| 145 |
assert(b->lid_closed); |
| 146 |
|
| 147 |
button_lid_switch_handle_action(b->manager, /* is_edge= */ false, b->seat); |
| 148 |
return 1; |
| 149 |
} |
| 150 |
|
| 151 |
static int button_install_check_event_source(Button *b) { |
| 152 |
int r; |
| 153 |
assert(b); |
| 154 |
|
| 155 |
/* Install a post handler, so that we keep rechecking as long as the lid is closed. */ |
| 156 |
|
| 157 |
if (b->check_event_source) |
| 158 |
return 0; |
| 159 |
|
| 160 |
r = sd_event_add_post(b->manager->event, &b->check_event_source, button_recheck, b); |
| 161 |
if (r < 0) |
| 162 |
return r; |
| 163 |
|
| 164 |
return sd_event_source_set_priority(b->check_event_source, SD_EVENT_PRIORITY_IDLE+1); |
| 165 |
} |
| 166 |
|
| 167 |
static int long_press_of_power_key_handler(sd_event_source *e, uint64_t usec, void *userdata) { |
| 168 |
Button *b = ASSERT_PTR(userdata); |
| 169 |
Manager *m = ASSERT_PTR(b->manager); |
| 170 |
|
| 171 |
assert(e); |
| 172 |
|
| 173 |
m->power_key_long_press_event_source = sd_event_source_unref(m->power_key_long_press_event_source); |
| 174 |
|
| 175 |
log_struct(LOG_INFO, |
| 176 |
LOG_MESSAGE("Power key pressed long."), |
| 177 |
LOG_MESSAGE_ID(SD_MESSAGE_POWER_KEY_LONG_PRESS_STR)); |
| 178 |
|
| 179 |
manager_handle_action(m, INHIBIT_HANDLE_POWER_KEY, m->handle_power_key_long_press, m->power_key_ignore_inhibited, /* is_edge= */ true, b->seat); |
| 180 |
return 0; |
| 181 |
} |
| 182 |
|
| 183 |
static int long_press_of_reboot_key_handler(sd_event_source *e, uint64_t usec, void *userdata) { |
| 184 |
Button *b = ASSERT_PTR(userdata); |
| 185 |
Manager *m = ASSERT_PTR(b->manager); |
| 186 |
|
| 187 |
assert(e); |
| 188 |
|
| 189 |
m->reboot_key_long_press_event_source = sd_event_source_unref(m->reboot_key_long_press_event_source); |
| 190 |
|
| 191 |
log_struct(LOG_INFO, |
| 192 |
LOG_MESSAGE("Reboot key pressed long."), |
| 193 |
LOG_MESSAGE_ID(SD_MESSAGE_REBOOT_KEY_LONG_PRESS_STR)); |
| 194 |
|
| 195 |
manager_handle_action(m, INHIBIT_HANDLE_REBOOT_KEY, m->handle_reboot_key_long_press, m->reboot_key_ignore_inhibited, /* is_edge= */ true, b->seat); |
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
static int long_press_of_suspend_key_handler(sd_event_source *e, uint64_t usec, void *userdata) { |
| 200 |
Button *b = ASSERT_PTR(userdata); |
| 201 |
Manager *m = ASSERT_PTR(b->manager); |
| 202 |
|
| 203 |
assert(e); |
| 204 |
|
| 205 |
m->suspend_key_long_press_event_source = sd_event_source_unref(m->suspend_key_long_press_event_source); |
| 206 |
|
| 207 |
log_struct(LOG_INFO, |
| 208 |
LOG_MESSAGE("Suspend key pressed long."), |
| 209 |
LOG_MESSAGE_ID(SD_MESSAGE_SUSPEND_KEY_LONG_PRESS_STR)); |
| 210 |
|
| 211 |
manager_handle_action(m, INHIBIT_HANDLE_SUSPEND_KEY, m->handle_suspend_key_long_press, m->suspend_key_ignore_inhibited, /* is_edge= */ true, b->seat); |
| 212 |
return 0; |
| 213 |
} |
| 214 |
|
| 215 |
static int long_press_of_hibernate_key_handler(sd_event_source *e, uint64_t usec, void *userdata) { |
| 216 |
Button *b = ASSERT_PTR(userdata); |
| 217 |
Manager *m = ASSERT_PTR(b->manager); |
| 218 |
|
| 219 |
assert(e); |
| 220 |
|
| 221 |
m->hibernate_key_long_press_event_source = sd_event_source_unref(m->hibernate_key_long_press_event_source); |
| 222 |
|
| 223 |
log_struct(LOG_INFO, |
| 224 |
LOG_MESSAGE("Hibernate key pressed long."), |
| 225 |
LOG_MESSAGE_ID(SD_MESSAGE_HIBERNATE_KEY_LONG_PRESS_STR)); |
| 226 |
|
| 227 |
manager_handle_action(m, INHIBIT_HANDLE_HIBERNATE_KEY, m->handle_hibernate_key_long_press, m->hibernate_key_ignore_inhibited, /* is_edge= */ true, b->seat); |
| 228 |
return 0; |
| 229 |
} |
| 230 |
|
| 231 |
static void start_long_press(Button *b, sd_event_source **e, sd_event_time_handler_t callback) { |
| 232 |
Manager *m; |
| 233 |
int r; |
| 234 |
|
| 235 |
assert(b); |
| 236 |
assert(e); |
| 237 |
m = ASSERT_PTR(b->manager); |
| 238 |
|
| 239 |
if (*e) |
| 240 |
return; |
| 241 |
|
| 242 |
r = sd_event_add_time_relative( |
| 243 |
m->event, |
| 244 |
e, |
| 245 |
CLOCK_MONOTONIC, |
| 246 |
LONG_PRESS_DURATION, 0, |
| 247 |
callback, b); |
| 248 |
if (r < 0) |
| 249 |
log_warning_errno(r, "Failed to add long press timer event, ignoring: %m"); |
| 250 |
} |
| 251 |
|
| 252 |
static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 253 |
Button *b = ASSERT_PTR(userdata); |
| 254 |
struct input_event ev; |
| 255 |
ssize_t l; |
| 256 |
|
| 257 |
assert(s); |
| 258 |
assert(fd == b->fd); |
| 259 |
|
| 260 |
l = read(b->fd, &ev, sizeof(ev)); |
| 261 |
if (l < 0) |
| 262 |
return errno != EAGAIN ? -errno : 0; |
| 263 |
if ((size_t) l < sizeof(ev)) |
| 264 |
return -EIO; |
| 265 |
|
| 266 |
if (ev.type == EV_KEY && ev.value > 0) { |
| 267 |
|
| 268 |
switch (ev.code) { |
| 269 |
|
| 270 |
case KEY_POWER: |
| 271 |
case KEY_POWER2: |
| 272 |
if (b->manager->handle_power_key_long_press != HANDLE_IGNORE && b->manager->handle_power_key_long_press != b->manager->handle_power_key) { |
| 273 |
log_debug("Power key pressed. Further action depends on the key press duration."); |
| 274 |
start_long_press(b, &b->manager->power_key_long_press_event_source, long_press_of_power_key_handler); |
| 275 |
} else { |
| 276 |
log_struct(LOG_INFO, |
| 277 |
LOG_MESSAGE("Power key pressed short."), |
| 278 |
LOG_MESSAGE_ID(SD_MESSAGE_POWER_KEY_STR)); |
| 279 |
manager_handle_action(b->manager, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, /* is_edge= */ true, b->seat); |
| 280 |
} |
| 281 |
break; |
| 282 |
|
| 283 |
/* The kernel naming is a bit confusing here: |
| 284 |
KEY_RESTART was probably introduced for media playback purposes, but |
| 285 |
is now being predominantly used to indicate device reboot. |
| 286 |
*/ |
| 287 |
|
| 288 |
case KEY_RESTART: |
| 289 |
if (b->manager->handle_reboot_key_long_press != HANDLE_IGNORE && b->manager->handle_reboot_key_long_press != b->manager->handle_reboot_key) { |
| 290 |
log_debug("Reboot key pressed. Further action depends on the key press duration."); |
| 291 |
start_long_press(b, &b->manager->reboot_key_long_press_event_source, long_press_of_reboot_key_handler); |
| 292 |
} else { |
| 293 |
log_struct(LOG_INFO, |
| 294 |
LOG_MESSAGE("Reboot key pressed short."), |
| 295 |
LOG_MESSAGE_ID(SD_MESSAGE_REBOOT_KEY_STR)); |
| 296 |
manager_handle_action(b->manager, INHIBIT_HANDLE_REBOOT_KEY, b->manager->handle_reboot_key, b->manager->reboot_key_ignore_inhibited, /* is_edge= */ true, b->seat); |
| 297 |
} |
| 298 |
break; |
| 299 |
|
| 300 |
/* The kernel naming is a bit confusing here: |
| 301 |
|
| 302 |
KEY_SLEEP = suspend-to-ram, which everybody else calls "suspend" |
| 303 |
KEY_SUSPEND = suspend-to-disk, which everybody else calls "hibernate" |
| 304 |
*/ |
| 305 |
|
| 306 |
case KEY_SLEEP: |
| 307 |
if (b->manager->handle_suspend_key_long_press != HANDLE_IGNORE && b->manager->handle_suspend_key_long_press != b->manager->handle_suspend_key) { |
| 308 |
log_debug("Suspend key pressed. Further action depends on the key press duration."); |
| 309 |
start_long_press(b, &b->manager->suspend_key_long_press_event_source, long_press_of_suspend_key_handler); |
| 310 |
} else { |
| 311 |
log_struct(LOG_INFO, |
| 312 |
LOG_MESSAGE("Suspend key pressed short."), |
| 313 |
LOG_MESSAGE_ID(SD_MESSAGE_SUSPEND_KEY_STR)); |
| 314 |
manager_handle_action(b->manager, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, /* is_edge= */ true, b->seat); |
| 315 |
} |
| 316 |
break; |
| 317 |
|
| 318 |
case KEY_SUSPEND: |
| 319 |
if (b->manager->handle_hibernate_key_long_press != HANDLE_IGNORE && b->manager->handle_hibernate_key_long_press != b->manager->handle_hibernate_key) { |
| 320 |
log_debug("Hibernate key pressed. Further action depends on the key press duration."); |
| 321 |
start_long_press(b, &b->manager->hibernate_key_long_press_event_source, long_press_of_hibernate_key_handler); |
| 322 |
} else { |
| 323 |
log_struct(LOG_INFO, |
| 324 |
LOG_MESSAGE("Hibernate key pressed short."), |
| 325 |
LOG_MESSAGE_ID(SD_MESSAGE_HIBERNATE_KEY_STR)); |
| 326 |
manager_handle_action(b->manager, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, /* is_edge= */ true, b->seat); |
| 327 |
} |
| 328 |
break; |
| 329 |
|
| 330 |
case KEY_ESC: |
| 331 |
if (b->manager->handle_secure_attention_key == HANDLE_IGNORE) |
| 332 |
break; |
| 333 |
if (!BUTTON_MODIFIER_HAS_SHIFT(b->button_modifier_mask) || |
| 334 |
!BUTTON_MODIFIER_HAS_CTRL(b->button_modifier_mask) || |
| 335 |
!BUTTON_MODIFIER_HAS_ALT(b->button_modifier_mask)) |
| 336 |
break; |
| 337 |
|
| 338 |
log_debug("Secure Attention Key sequence pressed."); |
| 339 |
manager_handle_action(b->manager, /* inhibit_key= */ 0, b->manager->handle_secure_attention_key, /* ignore_inhibited= */ true, /* is_edge= */ true, b->seat); |
| 340 |
break; |
| 341 |
|
| 342 |
default: |
| 343 |
b->button_modifier_mask |= button_get_keycode_modifier_mask(ev.code); |
| 344 |
} |
| 345 |
|
| 346 |
} else if (ev.type == EV_KEY && ev.value == 0) { |
| 347 |
|
| 348 |
switch (ev.code) { |
| 349 |
|
| 350 |
case KEY_POWER: |
| 351 |
case KEY_POWER2: |
| 352 |
if (b->manager->power_key_long_press_event_source) { |
| 353 |
/* Long press event timer is still pending and key release |
| 354 |
event happened. This means that key press duration was |
| 355 |
insufficient to trigger a long press event |
| 356 |
*/ |
| 357 |
log_struct(LOG_INFO, |
| 358 |
LOG_MESSAGE("Power key pressed short."), |
| 359 |
LOG_MESSAGE_ID(SD_MESSAGE_POWER_KEY_STR)); |
| 360 |
|
| 361 |
b->manager->power_key_long_press_event_source = sd_event_source_unref(b->manager->power_key_long_press_event_source); |
| 362 |
|
| 363 |
manager_handle_action(b->manager, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, /* is_edge= */ true, b->seat); |
| 364 |
} |
| 365 |
break; |
| 366 |
|
| 367 |
case KEY_RESTART: |
| 368 |
if (b->manager->reboot_key_long_press_event_source) { |
| 369 |
log_struct(LOG_INFO, |
| 370 |
LOG_MESSAGE("Reboot key pressed short."), |
| 371 |
LOG_MESSAGE_ID(SD_MESSAGE_REBOOT_KEY_STR)); |
| 372 |
|
| 373 |
b->manager->reboot_key_long_press_event_source = sd_event_source_unref(b->manager->reboot_key_long_press_event_source); |
| 374 |
|
| 375 |
manager_handle_action(b->manager, INHIBIT_HANDLE_REBOOT_KEY, b->manager->handle_reboot_key, b->manager->reboot_key_ignore_inhibited, /* is_edge= */ true, b->seat); |
| 376 |
} |
| 377 |
break; |
| 378 |
|
| 379 |
case KEY_SLEEP: |
| 380 |
if (b->manager->suspend_key_long_press_event_source) { |
| 381 |
log_struct(LOG_INFO, |
| 382 |
LOG_MESSAGE("Suspend key pressed short."), |
| 383 |
LOG_MESSAGE_ID(SD_MESSAGE_SUSPEND_KEY_STR)); |
| 384 |
|
| 385 |
b->manager->suspend_key_long_press_event_source = sd_event_source_unref(b->manager->suspend_key_long_press_event_source); |
| 386 |
|
| 387 |
manager_handle_action(b->manager, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, /* is_edge= */ true, b->seat); |
| 388 |
} |
| 389 |
break; |
| 390 |
case KEY_SUSPEND: |
| 391 |
if (b->manager->hibernate_key_long_press_event_source) { |
| 392 |
log_struct(LOG_INFO, |
| 393 |
LOG_MESSAGE("Hibernate key pressed short."), |
| 394 |
LOG_MESSAGE_ID(SD_MESSAGE_HIBERNATE_KEY_STR)); |
| 395 |
|
| 396 |
b->manager->hibernate_key_long_press_event_source = sd_event_source_unref(b->manager->hibernate_key_long_press_event_source); |
| 397 |
|
| 398 |
manager_handle_action(b->manager, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, /* is_edge= */ true, b->seat); |
| 399 |
} |
| 400 |
break; |
| 401 |
|
| 402 |
case KEY_ESC: |
| 403 |
break; |
| 404 |
|
| 405 |
default: |
| 406 |
b->button_modifier_mask &= ~button_get_keycode_modifier_mask(ev.code); |
| 407 |
} |
| 408 |
|
| 409 |
} else if (ev.type == EV_SW && ev.value > 0) { |
| 410 |
|
| 411 |
if (ev.code == SW_LID) { |
| 412 |
log_struct(LOG_INFO, |
| 413 |
LOG_MESSAGE("Lid closed."), |
| 414 |
LOG_MESSAGE_ID(SD_MESSAGE_LID_CLOSED_STR)); |
| 415 |
|
| 416 |
b->lid_closed = true; |
| 417 |
button_lid_switch_handle_action(b->manager, /* is_edge= */ true, b->seat); |
| 418 |
button_install_check_event_source(b); |
| 419 |
manager_send_changed(b->manager, "LidClosed"); |
| 420 |
|
| 421 |
} else if (ev.code == SW_DOCK) { |
| 422 |
log_struct(LOG_INFO, |
| 423 |
LOG_MESSAGE("System docked."), |
| 424 |
LOG_MESSAGE_ID(SD_MESSAGE_SYSTEM_DOCKED_STR)); |
| 425 |
|
| 426 |
b->docked = true; |
| 427 |
} |
| 428 |
|
| 429 |
} else if (ev.type == EV_SW && ev.value == 0) { |
| 430 |
|
| 431 |
if (ev.code == SW_LID) { |
| 432 |
log_struct(LOG_INFO, |
| 433 |
LOG_MESSAGE("Lid opened."), |
| 434 |
LOG_MESSAGE_ID(SD_MESSAGE_LID_OPENED_STR)); |
| 435 |
|
| 436 |
b->lid_closed = false; |
| 437 |
b->check_event_source = sd_event_source_unref(b->check_event_source); |
| 438 |
manager_send_changed(b->manager, "LidClosed"); |
| 439 |
|
| 440 |
} else if (ev.code == SW_DOCK) { |
| 441 |
log_struct(LOG_INFO, |
| 442 |
LOG_MESSAGE("System undocked."), |
| 443 |
LOG_MESSAGE_ID(SD_MESSAGE_SYSTEM_UNDOCKED_STR)); |
| 444 |
|
| 445 |
b->docked = false; |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
return 0; |
| 450 |
} |
| 451 |
|
| 452 |
static int button_suitable(int fd) { |
| 453 |
unsigned long types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1]; |
| 454 |
|
| 455 |
assert(fd >= 0); |
| 456 |
|
| 457 |
if (ioctl(fd, EVIOCGBIT(EV_SYN, sizeof types), types) < 0) |
| 458 |
return -errno; |
| 459 |
|
| 460 |
if (bitset_get(types, EV_KEY)) { |
| 461 |
unsigned long keys[_KEY_MAX_INTERESTED/ULONG_BITS+1]; |
| 462 |
|
| 463 |
if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof keys), keys) < 0) |
| 464 |
return -errno; |
| 465 |
|
| 466 |
/* If the device has power related keys, then accept the device. */ |
| 467 |
if (bitset_get(keys, KEY_POWER) || |
| 468 |
bitset_get(keys, KEY_POWER2) || |
| 469 |
bitset_get(keys, KEY_SLEEP) || |
| 470 |
bitset_get(keys, KEY_SUSPEND) || |
| 471 |
bitset_get(keys, KEY_RESTART)) |
| 472 |
return true; |
| 473 |
|
| 474 |
/* If the device has keys for the Secure Attention Key sequence, then accept the device. */ |
| 475 |
if ((bitset_get(keys, KEY_LEFTSHIFT) || bitset_get(keys, KEY_RIGHTSHIFT)) && |
| 476 |
(bitset_get(keys, KEY_LEFTCTRL) || bitset_get(keys, KEY_RIGHTCTRL)) && |
| 477 |
(bitset_get(keys, KEY_LEFTALT) || bitset_get(keys, KEY_RIGHTALT)) && |
| 478 |
bitset_get(keys, KEY_ESC)) |
| 479 |
return true; |
| 480 |
} |
| 481 |
|
| 482 |
if (bitset_get(types, EV_SW)) { |
| 483 |
unsigned long switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1]; |
| 484 |
|
| 485 |
if (ioctl(fd, EVIOCGBIT(EV_SW, sizeof switches), switches) < 0) |
| 486 |
return -errno; |
| 487 |
|
| 488 |
if (bitset_get(switches, SW_LID) || |
| 489 |
bitset_get(switches, SW_DOCK)) |
| 490 |
return true; |
| 491 |
} |
| 492 |
|
| 493 |
return false; |
| 494 |
} |
| 495 |
|
| 496 |
static int button_initialize_modifier_mask(Button *b) { |
| 497 |
unsigned long keys[KEY_MAX/ULONG_BITS+1]; |
| 498 |
|
| 499 |
assert(b); |
| 500 |
|
| 501 |
if (ioctl(b->fd, EVIOCGKEY(sizeof(keys)), keys) < 0) |
| 502 |
return log_error_errno(errno, "Failed to initialize modifier mask on /dev/input/%s: %m", b->name); |
| 503 |
|
| 504 |
b->button_modifier_mask = BUTTON_MODIFIER_NONE; |
| 505 |
|
| 506 |
FOREACH_ELEMENT(i, keycode_modifier_table) |
| 507 |
if (bitset_get(keys, i->keycode)) |
| 508 |
b->button_modifier_mask |= i->modifier_mask; |
| 509 |
|
| 510 |
return 0; |
| 511 |
} |
| 512 |
|
| 513 |
static int button_set_mask(const char *name, int fd) { |
| 514 |
unsigned long |
| 515 |
types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1] = {}, |
| 516 |
keys[_KEY_MAX_INTERESTED/ULONG_BITS+1] = {}, |
| 517 |
switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1] = {}; |
| 518 |
struct input_mask mask; |
| 519 |
|
| 520 |
assert(name); |
| 521 |
assert(fd >= 0); |
| 522 |
|
| 523 |
bitset_put(types, EV_KEY); |
| 524 |
bitset_put(types, EV_SW); |
| 525 |
|
| 526 |
mask = (struct input_mask) { |
| 527 |
.type = EV_SYN, |
| 528 |
.codes_size = sizeof(types), |
| 529 |
.codes_ptr = PTR_TO_UINT64(types), |
| 530 |
}; |
| 531 |
|
| 532 |
if (ioctl(fd, EVIOCSMASK, &mask) < 0) |
| 533 |
/* Log only at debug level if the kernel doesn't do EVIOCSMASK yet */ |
| 534 |
return log_full_errno(IN_SET(errno, ENOTTY, EOPNOTSUPP, EINVAL) ? LOG_DEBUG : LOG_WARNING, |
| 535 |
errno, "Failed to set EV_SYN event mask on /dev/input/%s: %m", name); |
| 536 |
|
| 537 |
FOREACH_ELEMENT(key, keys_interested) { |
| 538 |
assert(*key <= _KEY_MAX_INTERESTED); |
| 539 |
|
| 540 |
bitset_put(keys, *key); |
| 541 |
} |
| 542 |
|
| 543 |
mask = (struct input_mask) { |
| 544 |
.type = EV_KEY, |
| 545 |
.codes_size = sizeof(keys), |
| 546 |
.codes_ptr = PTR_TO_UINT64(keys), |
| 547 |
}; |
| 548 |
|
| 549 |
if (ioctl(fd, EVIOCSMASK, &mask) < 0) |
| 550 |
return log_warning_errno(errno, "Failed to set EV_KEY event mask on /dev/input/%s: %m", name); |
| 551 |
|
| 552 |
bitset_put(switches, SW_LID); |
| 553 |
bitset_put(switches, SW_DOCK); |
| 554 |
|
| 555 |
mask = (struct input_mask) { |
| 556 |
.type = EV_SW, |
| 557 |
.codes_size = sizeof(switches), |
| 558 |
.codes_ptr = PTR_TO_UINT64(switches), |
| 559 |
}; |
| 560 |
|
| 561 |
if (ioctl(fd, EVIOCSMASK, &mask) < 0) |
| 562 |
return log_warning_errno(errno, "Failed to set EV_SW event mask on /dev/input/%s: %m", name); |
| 563 |
|
| 564 |
return 0; |
| 565 |
} |
| 566 |
|
| 567 |
int button_open(Button *b) { |
| 568 |
_cleanup_(asynchronous_closep) int fd = -EBADF; |
| 569 |
const char *p; |
| 570 |
char name[256]; |
| 571 |
int r; |
| 572 |
|
| 573 |
assert(b); |
| 574 |
|
| 575 |
b->fd = asynchronous_close(b->fd); |
| 576 |
|
| 577 |
p = strjoina("/dev/input/", b->name); |
| 578 |
|
| 579 |
fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); |
| 580 |
if (fd < 0) |
| 581 |
return log_warning_errno(errno, "Failed to open %s: %m", p); |
| 582 |
|
| 583 |
r = button_suitable(fd); |
| 584 |
if (r < 0) |
| 585 |
return log_warning_errno(r, "Failed to determine whether input device %s is relevant to us: %m", p); |
| 586 |
if (r == 0) |
| 587 |
return log_debug_errno(SYNTHETIC_ERRNO(EADDRNOTAVAIL), |
| 588 |
"Device %s does not expose keys or switches relevant to us, ignoring.", p); |
| 589 |
|
| 590 |
if (ioctl(fd, EVIOCGNAME(sizeof name), name) < 0) |
| 591 |
return log_error_errno(errno, "Failed to get input name for %s: %m", p); |
| 592 |
|
| 593 |
(void) button_set_mask(b->name, fd); |
| 594 |
|
| 595 |
b->io_event_source = sd_event_source_unref(b->io_event_source); |
| 596 |
r = sd_event_add_io(b->manager->event, &b->io_event_source, fd, EPOLLIN, button_dispatch, b); |
| 597 |
if (r < 0) |
| 598 |
return log_error_errno(r, "Failed to add button event for %s: %m", p); |
| 599 |
|
| 600 |
b->fd = TAKE_FD(fd); |
| 601 |
log_info("Watching system buttons on %s (%s)", p, name); |
| 602 |
|
| 603 |
r = button_initialize_modifier_mask(b); |
| 604 |
if (r < 0) |
| 605 |
return r; |
| 606 |
|
| 607 |
return 0; |
| 608 |
} |
| 609 |
|
| 610 |
int button_check_switches(Button *b) { |
| 611 |
unsigned long switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1] = {}; |
| 612 |
assert(b); |
| 613 |
|
| 614 |
if (b->fd < 0) |
| 615 |
return -EINVAL; |
| 616 |
|
| 617 |
if (ioctl(b->fd, EVIOCGSW(sizeof(switches)), switches) < 0) |
| 618 |
return -errno; |
| 619 |
|
| 620 |
b->lid_closed = bitset_get(switches, SW_LID); |
| 621 |
b->docked = bitset_get(switches, SW_DOCK); |
| 622 |
manager_send_changed(b->manager, "LidClosed"); |
| 623 |
|
| 624 |
if (b->lid_closed) |
| 625 |
button_install_check_event_source(b); |
| 626 |
|
| 627 |
return 0; |
| 628 |
} |