1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/ioctl.h>
28 #include <linux/input.h>
30 #include "sd-messages.h"
31 #include "conf-parser.h"
34 #include "logind-button.h"
36 Button* button_new(Manager *m, const char *name) {
46 b->name = strdup(name);
52 if (hashmap_put(m->buttons, b->name, b) < 0) {
64 void button_free(Button *b) {
67 hashmap_remove(b->manager->buttons, b->name);
69 sd_event_source_unref(b->event_source);
72 /* If the device has been unplugged close() returns
73 * ENODEV, let's ignore this, hence we don't use
74 * close_nointr_nofail() */
83 int button_set_seat(Button *b, const char *sn) {
99 static int button_handle(
101 InhibitWhat inhibit_key,
103 bool ignore_inhibited,
110 r = manager_handle_action(b->manager, inhibit_key, handle, ignore_inhibited, is_edge);
112 /* We are executing the operation, so make sure we don't
113 * execute another one until the lid is opened/closed again */
114 b->lid_close_queued = false;
119 static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
120 Button *b = userdata;
121 struct input_event ev;
128 l = read(b->fd, &ev, sizeof(ev));
130 return errno != EAGAIN ? -errno : 0;
131 if ((size_t) l < sizeof(ev))
134 if (ev.type == EV_KEY && ev.value > 0) {
141 "MESSAGE=Power key pressed.",
142 MESSAGE_ID(SD_MESSAGE_POWER_KEY),
145 button_handle(b, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true);
148 /* The kernel is a bit confused here:
150 KEY_SLEEP = suspend-to-ram, which everybody else calls "suspend"
151 KEY_SUSPEND = suspend-to-disk, which everybody else calls "hibernate"
156 "MESSAGE=Suspend key pressed.",
157 MESSAGE_ID(SD_MESSAGE_SUSPEND_KEY),
160 button_handle(b, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true);
165 "MESSAGE=Hibernate key pressed.",
166 MESSAGE_ID(SD_MESSAGE_HIBERNATE_KEY),
169 button_handle(b, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true);
173 } else if (ev.type == EV_SW && ev.value > 0) {
175 if (ev.code == SW_LID) {
177 "MESSAGE=Lid closed.",
178 MESSAGE_ID(SD_MESSAGE_LID_CLOSED),
181 b->lid_close_queued = true;
182 button_handle(b, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, true);
185 } else if (ev.type == EV_SW && ev.value == 0) {
187 if (ev.code == SW_LID) {
189 "MESSAGE=Lid opened.",
190 MESSAGE_ID(SD_MESSAGE_LID_OPENED),
193 b->lid_close_queued = false;
200 int button_open(Button *b) {
211 p = strappenda("/dev/input/", b->name);
213 b->fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
215 log_warning("Failed to open %s: %m", b->name);
219 if (ioctl(b->fd, EVIOCGNAME(sizeof(name)), name) < 0) {
220 log_error("Failed to get input name: %m");
225 r = sd_event_add_io(b->manager->event, b->fd, EPOLLIN, button_dispatch, b, &b->event_source);
227 log_error("Failed to add button event: %s", strerror(-r));
231 log_info("Watching system buttons on /dev/input/%s (%s)", b->name, name);
241 int button_recheck(Button *b) {
244 if (!b->lid_close_queued)
247 return button_handle(b, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, false);