logind-button.c revision 63c372cb9df3bee01e3bf8cd7f96f336bddda846
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering This file is part of systemd.
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering Copyright 2012 Lennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering systemd is free software; you can redistribute it and/or modify it
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering under the terms of the GNU Lesser General Public License as published by
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering (at your option) any later version.
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering systemd is distributed in the hope that it will be useful, but
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering Lesser General Public License for more details.
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering You should have received a copy of the GNU Lesser General Public License
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart PoetteringButton* button_new(Manager *m, const char *name) {
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering if (hashmap_put(m->buttons, b->name, b) < 0) {
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering hashmap_remove(b->manager->buttons, b->name);
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering sd_event_source_unref(b->io_event_source);
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering sd_event_source_unref(b->check_event_source);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering if (b->fd >= 0) {
c30a0c62fdbf6f11902be9db64ade99fb508adfdLennart Poettering /* If the device has been unplugged close() returns
c30a0c62fdbf6f11902be9db64ade99fb508adfdLennart Poettering * ENODEV, let's ignore this, hence we don't use
03e334a1c7dc8c20c38902aa039440763acc9b17Lennart Poettering * safe_close() */
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poetteringint button_set_seat(Button *b, const char *sn) {
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsiefferstatic void button_lid_switch_handle_action(Manager *manager, bool is_edge) {
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsieffer /* If we are docked, handle the lid switch differently */
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsieffer if (manager_is_docked_or_multiple_displays(manager))
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsieffer handle_action = manager->handle_lid_switch_docked;
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsieffer manager_handle_action(manager, INHIBIT_HANDLE_LID_SWITCH, handle_action, manager->lid_switch_ignore_inhibited, is_edge);
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poetteringstatic int button_recheck(sd_event_source *e, void *userdata) {
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsieffer button_lid_switch_handle_action(b->manager, false);
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poetteringstatic int button_install_check_event_source(Button *b) {
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering /* Install a post handler, so that we keep rechecking as long as the lid is closed. */
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering r = sd_event_add_post(b->manager->event, &b->check_event_source, button_recheck, b);
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering return sd_event_source_set_priority(b->check_event_source, SD_EVENT_PRIORITY_IDLE+1);
cc3773810855956bad92337cee8fa193584ab62eLennart Poetteringstatic int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering manager_handle_action(b->manager, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true);
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering /* The kernel is a bit confused here:
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering KEY_SLEEP = suspend-to-ram, which everybody else calls "suspend"
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering KEY_SUSPEND = suspend-to-disk, which everybody else calls "hibernate"
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering manager_handle_action(b->manager, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true);
e2cc6eca73cd1df8be552d7c23f9ff3d69c06f1eLennart Poettering LOG_MESSAGE_ID(SD_MESSAGE_HIBERNATE_KEY),
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering manager_handle_action(b->manager, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering } else if (ev.type == EV_SW && ev.value > 0) {
3c56cab44150ad47323970cfadfb0257c6305a74Ben Wolsieffer button_lid_switch_handle_action(b->manager, true);
e2cc6eca73cd1df8be552d7c23f9ff3d69c06f1eLennart Poettering LOG_MESSAGE_ID(SD_MESSAGE_SYSTEM_DOCKED),
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering } else if (ev.type == EV_SW && ev.value == 0) {
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering b->check_event_source = sd_event_source_unref(b->check_event_source);
e2cc6eca73cd1df8be552d7c23f9ff3d69c06f1eLennart Poettering LOG_MESSAGE_ID(SD_MESSAGE_SYSTEM_UNDOCKED),
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering if (b->fd >= 0) {
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering b->fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt return log_warning_errno(errno, "Failed to open %s: %m", b->name);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering if (ioctl(b->fd, EVIOCGNAME(sizeof(name)), name) < 0) {
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt log_error_errno(errno, "Failed to get input name: %m");
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering r = sd_event_add_io(b->manager->event, &b->io_event_source, b->fd, EPOLLIN, button_dispatch, b);
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_error_errno(r, "Failed to add button event: %m");
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering log_info("Watching system buttons on /dev/input/%s (%s)", b->name, name);
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering if (ioctl(b->fd, EVIOCGSW(sizeof(switches)), switches) < 0)
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering b->lid_closed = (switches[SW_LID/8] >> (SW_LID % 8)) & 1;
2d62c530d2b4c2730abff715b7342f1402114513Lennart Poettering b->docked = (switches[SW_DOCK/8] >> (SW_DOCK % 8)) & 1;