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->io_event_source);
70 sd_event_source_unref(b->check_event_source);
73 /* If the device has been unplugged close() returns
74 * ENODEV, let's ignore this, hence we don't use
84 int button_set_seat(Button *b, const char *sn) {
100 static int button_recheck(sd_event_source *e, void *userdata) {
101 Button *b = userdata;
104 assert(b->lid_closed);
106 manager_handle_action(b->manager, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, false);
110 static int button_install_check_event_source(Button *b) {
114 /* Install a post handler, so that we keep rechecking as long as the lid is closed. */
116 if (b->check_event_source)
119 r = sd_event_add_post(b->manager->event, &b->check_event_source, button_recheck, b);
123 return sd_event_source_set_priority(b->check_event_source, SD_EVENT_PRIORITY_IDLE+1);
126 static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
127 Button *b = userdata;
128 struct input_event ev;
135 l = read(b->fd, &ev, sizeof(ev));
137 return errno != EAGAIN ? -errno : 0;
138 if ((size_t) l < sizeof(ev))
141 if (ev.type == EV_KEY && ev.value > 0) {
148 "MESSAGE=Power key pressed.",
149 MESSAGE_ID(SD_MESSAGE_POWER_KEY),
152 manager_handle_action(b->manager, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true);
155 /* The kernel is a bit confused here:
157 KEY_SLEEP = suspend-to-ram, which everybody else calls "suspend"
158 KEY_SUSPEND = suspend-to-disk, which everybody else calls "hibernate"
163 "MESSAGE=Suspend key pressed.",
164 MESSAGE_ID(SD_MESSAGE_SUSPEND_KEY),
167 manager_handle_action(b->manager, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true);
172 "MESSAGE=Hibernate key pressed.",
173 MESSAGE_ID(SD_MESSAGE_HIBERNATE_KEY),
176 manager_handle_action(b->manager, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true);
180 } else if (ev.type == EV_SW && ev.value > 0) {
182 if (ev.code == SW_LID) {
184 "MESSAGE=Lid closed.",
185 MESSAGE_ID(SD_MESSAGE_LID_CLOSED),
188 b->lid_closed = true;
189 manager_handle_action(b->manager, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, true);
190 button_install_check_event_source(b);
192 } else if (ev.code == SW_DOCK) {
194 "MESSAGE=System docked.",
195 MESSAGE_ID(SD_MESSAGE_SYSTEM_DOCKED),
201 } else if (ev.type == EV_SW && ev.value == 0) {
203 if (ev.code == SW_LID) {
205 "MESSAGE=Lid opened.",
206 MESSAGE_ID(SD_MESSAGE_LID_OPENED),
209 b->lid_closed = false;
210 b->check_event_source = sd_event_source_unref(b->check_event_source);
212 } else if (ev.code == SW_DOCK) {
214 "MESSAGE=System undocked.",
215 MESSAGE_ID(SD_MESSAGE_SYSTEM_UNDOCKED),
225 int button_open(Button *b) {
236 p = strappenda("/dev/input/", b->name);
238 b->fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
240 log_warning("Failed to open %s: %m", b->name);
244 if (ioctl(b->fd, EVIOCGNAME(sizeof(name)), name) < 0) {
245 log_error("Failed to get input name: %m");
250 r = sd_event_add_io(b->manager->event, &b->io_event_source, b->fd, EPOLLIN, button_dispatch, b);
252 log_error("Failed to add button event: %s", strerror(-r));
256 log_info("Watching system buttons on /dev/input/%s (%s)", b->name, name);
266 int button_check_switches(Button *b) {
267 uint8_t switches[SW_MAX/8+1] = {};
273 if (ioctl(b->fd, EVIOCGSW(sizeof(switches)), switches) < 0)
276 b->lid_closed = (switches[SW_LID/8] >> (SW_LID % 8)) & 1;
277 b->docked = (switches[SW_DOCK/8] >> (SW_DOCK % 8)) & 1;
280 button_install_check_event_source(b);