]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/logind-button.c
util-lib: split out fd-related operations into fd-util.[ch]
[thirdparty/systemd.git] / src / login / logind-button.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2012 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <sys/ioctl.h>
26 #include <unistd.h>
27 #include <linux/input.h>
28
29 #include "sd-messages.h"
30
31 #include "fd-util.h"
32 #include "logind-button.h"
33 #include "string-util.h"
34 #include "util.h"
35
36 Button* button_new(Manager *m, const char *name) {
37 Button *b;
38
39 assert(m);
40 assert(name);
41
42 b = new0(Button, 1);
43 if (!b)
44 return NULL;
45
46 b->name = strdup(name);
47 if (!b->name) {
48 free(b);
49 return NULL;
50 }
51
52 if (hashmap_put(m->buttons, b->name, b) < 0) {
53 free(b->name);
54 free(b);
55 return NULL;
56 }
57
58 b->manager = m;
59 b->fd = -1;
60
61 return b;
62 }
63
64 void button_free(Button *b) {
65 assert(b);
66
67 hashmap_remove(b->manager->buttons, b->name);
68
69 sd_event_source_unref(b->io_event_source);
70 sd_event_source_unref(b->check_event_source);
71
72 if (b->fd >= 0)
73 /* If the device has been unplugged close() returns
74 * ENODEV, let's ignore this, hence we don't use
75 * safe_close() */
76 (void) close(b->fd);
77
78 free(b->name);
79 free(b->seat);
80 free(b);
81 }
82
83 int button_set_seat(Button *b, const char *sn) {
84 char *s;
85
86 assert(b);
87 assert(sn);
88
89 s = strdup(sn);
90 if (!s)
91 return -ENOMEM;
92
93 free(b->seat);
94 b->seat = s;
95
96 return 0;
97 }
98
99 static void button_lid_switch_handle_action(Manager *manager, bool is_edge) {
100 HandleAction handle_action;
101
102 assert(manager);
103
104 /* If we are docked, handle the lid switch differently */
105 if (manager_is_docked_or_external_displays(manager))
106 handle_action = manager->handle_lid_switch_docked;
107 else
108 handle_action = manager->handle_lid_switch;
109
110 manager_handle_action(manager, INHIBIT_HANDLE_LID_SWITCH, handle_action, manager->lid_switch_ignore_inhibited, is_edge);
111 }
112
113 static int button_recheck(sd_event_source *e, void *userdata) {
114 Button *b = userdata;
115
116 assert(b);
117 assert(b->lid_closed);
118
119 button_lid_switch_handle_action(b->manager, false);
120 return 1;
121 }
122
123 static int button_install_check_event_source(Button *b) {
124 int r;
125 assert(b);
126
127 /* Install a post handler, so that we keep rechecking as long as the lid is closed. */
128
129 if (b->check_event_source)
130 return 0;
131
132 r = sd_event_add_post(b->manager->event, &b->check_event_source, button_recheck, b);
133 if (r < 0)
134 return r;
135
136 return sd_event_source_set_priority(b->check_event_source, SD_EVENT_PRIORITY_IDLE+1);
137 }
138
139 static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
140 Button *b = userdata;
141 struct input_event ev;
142 ssize_t l;
143
144 assert(s);
145 assert(fd == b->fd);
146 assert(b);
147
148 l = read(b->fd, &ev, sizeof(ev));
149 if (l < 0)
150 return errno != EAGAIN ? -errno : 0;
151 if ((size_t) l < sizeof(ev))
152 return -EIO;
153
154 if (ev.type == EV_KEY && ev.value > 0) {
155
156 switch (ev.code) {
157
158 case KEY_POWER:
159 case KEY_POWER2:
160 log_struct(LOG_INFO,
161 LOG_MESSAGE("Power key pressed."),
162 LOG_MESSAGE_ID(SD_MESSAGE_POWER_KEY),
163 NULL);
164
165 manager_handle_action(b->manager, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true);
166 break;
167
168 /* The kernel is a bit confused here:
169
170 KEY_SLEEP = suspend-to-ram, which everybody else calls "suspend"
171 KEY_SUSPEND = suspend-to-disk, which everybody else calls "hibernate"
172 */
173
174 case KEY_SLEEP:
175 log_struct(LOG_INFO,
176 LOG_MESSAGE("Suspend key pressed."),
177 LOG_MESSAGE_ID(SD_MESSAGE_SUSPEND_KEY),
178 NULL);
179
180 manager_handle_action(b->manager, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true);
181 break;
182
183 case KEY_SUSPEND:
184 log_struct(LOG_INFO,
185 LOG_MESSAGE("Hibernate key pressed."),
186 LOG_MESSAGE_ID(SD_MESSAGE_HIBERNATE_KEY),
187 NULL);
188
189 manager_handle_action(b->manager, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true);
190 break;
191 }
192
193 } else if (ev.type == EV_SW && ev.value > 0) {
194
195 if (ev.code == SW_LID) {
196 log_struct(LOG_INFO,
197 LOG_MESSAGE("Lid closed."),
198 LOG_MESSAGE_ID(SD_MESSAGE_LID_CLOSED),
199 NULL);
200
201 b->lid_closed = true;
202 button_lid_switch_handle_action(b->manager, true);
203 button_install_check_event_source(b);
204
205 } else if (ev.code == SW_DOCK) {
206 log_struct(LOG_INFO,
207 LOG_MESSAGE("System docked."),
208 LOG_MESSAGE_ID(SD_MESSAGE_SYSTEM_DOCKED),
209 NULL);
210
211 b->docked = true;
212 }
213
214 } else if (ev.type == EV_SW && ev.value == 0) {
215
216 if (ev.code == SW_LID) {
217 log_struct(LOG_INFO,
218 LOG_MESSAGE("Lid opened."),
219 LOG_MESSAGE_ID(SD_MESSAGE_LID_OPENED),
220 NULL);
221
222 b->lid_closed = false;
223 b->check_event_source = sd_event_source_unref(b->check_event_source);
224
225 } else if (ev.code == SW_DOCK) {
226 log_struct(LOG_INFO,
227 LOG_MESSAGE("System undocked."),
228 LOG_MESSAGE_ID(SD_MESSAGE_SYSTEM_UNDOCKED),
229 NULL);
230
231 b->docked = false;
232 }
233 }
234
235 return 0;
236 }
237
238 int button_open(Button *b) {
239 char *p, name[256];
240 int r;
241
242 assert(b);
243
244 b->fd = safe_close(b->fd);
245
246 p = strjoina("/dev/input/", b->name);
247
248 b->fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
249 if (b->fd < 0)
250 return log_warning_errno(errno, "Failed to open %s: %m", b->name);
251
252 if (ioctl(b->fd, EVIOCGNAME(sizeof(name)), name) < 0) {
253 r = log_error_errno(errno, "Failed to get input name: %m");
254 goto fail;
255 }
256
257 r = sd_event_add_io(b->manager->event, &b->io_event_source, b->fd, EPOLLIN, button_dispatch, b);
258 if (r < 0) {
259 log_error_errno(r, "Failed to add button event: %m");
260 goto fail;
261 }
262
263 log_info("Watching system buttons on /dev/input/%s (%s)", b->name, name);
264
265 return 0;
266
267 fail:
268 b->fd = safe_close(b->fd);
269 return r;
270 }
271
272 int button_check_switches(Button *b) {
273 uint8_t switches[SW_MAX/8+1] = {};
274 assert(b);
275
276 if (b->fd < 0)
277 return -EINVAL;
278
279 if (ioctl(b->fd, EVIOCGSW(sizeof(switches)), switches) < 0)
280 return -errno;
281
282 b->lid_closed = (switches[SW_LID/8] >> (SW_LID % 8)) & 1;
283 b->docked = (switches[SW_DOCK/8] >> (SW_DOCK % 8)) & 1;
284
285 if (b->lid_closed)
286 button_install_check_event_source(b);
287
288 return 0;
289 }