logind-button.c revision cc3773810855956bad92337cee8fa193584ab62e
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);
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
c30a0c62fdbf6f11902be9db64ade99fb508adfdLennart Poettering * close_nointr_nofail() */
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poetteringint button_set_seat(Button *b, const char *sn) {
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering r = manager_handle_action(b->manager, inhibit_key, handle, ignore_inhibited, is_edge);
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering /* We are executing the operation, so make sure we don't
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * execute another one until the lid is opened/closed again */
cc3773810855956bad92337cee8fa193584ab62eLennart Poetteringstatic int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek "MESSAGE=Power key pressed.",
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek MESSAGE_ID(SD_MESSAGE_POWER_KEY),
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering return button_handle(b, 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"
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek "MESSAGE=Suspend key pressed.",
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek MESSAGE_ID(SD_MESSAGE_SUSPEND_KEY),
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering return button_handle(b, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true);
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek "MESSAGE=Hibernate key pressed.",
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek MESSAGE_ID(SD_MESSAGE_HIBERNATE_KEY),
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering return button_handle(b, 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) {
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek "MESSAGE=Lid closed.",
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek MESSAGE_ID(SD_MESSAGE_LID_CLOSED),
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering return button_handle(b, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, true);
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering } else if (ev.type == EV_SW && ev.value == 0) {
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek "MESSAGE=Lid opened.",
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek MESSAGE_ID(SD_MESSAGE_LID_OPENED),
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering if (b->fd >= 0) {
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering b->fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering if (b->fd < 0) {
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering log_warning("Failed to open %s: %m", b->name);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering if (ioctl(b->fd, EVIOCGNAME(sizeof(name)), name) < 0) {
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering log_error("Failed to get input name: %m");
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering r = sd_event_add_io(b->manager->event, b->fd, EPOLLIN, button_dispatch, b, &b->event_source);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering log_error("Failed to add button event: %s", strerror(-r));
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering log_info("Watching system buttons on /dev/input/%s (%s)", b->name, name);