]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/login/logind-button.c
tree-wide: use mfree more
[thirdparty/systemd.git] / src / login / logind-button.c
CommitLineData
e9d21f24
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2012 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
e9d21f24
LP
20#include <errno.h>
21#include <fcntl.h>
07630cea 22#include <string.h>
e9d21f24
LP
23#include <sys/ioctl.h>
24#include <unistd.h>
25#include <linux/input.h>
e9d21f24 26
cc377381 27#include "sd-messages.h"
07630cea 28
b5efdb8a 29#include "alloc-util.h"
3ffd4af2
LP
30#include "fd-util.h"
31#include "logind-button.h"
07630cea 32#include "string-util.h"
e9d21f24 33#include "util.h"
e9d21f24
LP
34
35Button* button_new(Manager *m, const char *name) {
36 Button *b;
37
38 assert(m);
39 assert(name);
40
41 b = new0(Button, 1);
42 if (!b)
43 return NULL;
44
45 b->name = strdup(name);
6b430fdb
ZJS
46 if (!b->name)
47 return mfree(b);
e9d21f24
LP
48
49 if (hashmap_put(m->buttons, b->name, b) < 0) {
50 free(b->name);
6b430fdb 51 return mfree(b);
e9d21f24
LP
52 }
53
54 b->manager = m;
55 b->fd = -1;
56
57 return b;
58}
59
60void button_free(Button *b) {
61 assert(b);
62
63 hashmap_remove(b->manager->buttons, b->name);
64
ed4ba7e4
LP
65 sd_event_source_unref(b->io_event_source);
66 sd_event_source_unref(b->check_event_source);
c30a0c62 67
ece174c5 68 if (b->fd >= 0)
c30a0c62
LP
69 /* If the device has been unplugged close() returns
70 * ENODEV, let's ignore this, hence we don't use
03e334a1 71 * safe_close() */
4fba5796 72 (void) close(b->fd);
e9d21f24
LP
73
74 free(b->name);
75 free(b->seat);
76 free(b);
77}
78
79int button_set_seat(Button *b, const char *sn) {
80 char *s;
81
82 assert(b);
83 assert(sn);
84
85 s = strdup(sn);
86 if (!s)
87 return -ENOMEM;
88
89 free(b->seat);
90 b->seat = s;
91
92 return 0;
93}
94
3c56cab4
BW
95static void button_lid_switch_handle_action(Manager *manager, bool is_edge) {
96 HandleAction handle_action;
97
98 assert(manager);
99
100 /* If we are docked, handle the lid switch differently */
602a41c2 101 if (manager_is_docked_or_external_displays(manager))
3c56cab4
BW
102 handle_action = manager->handle_lid_switch_docked;
103 else
104 handle_action = manager->handle_lid_switch;
105
106 manager_handle_action(manager, INHIBIT_HANDLE_LID_SWITCH, handle_action, manager->lid_switch_ignore_inhibited, is_edge);
107}
108
ed4ba7e4
LP
109static int button_recheck(sd_event_source *e, void *userdata) {
110 Button *b = userdata;
beaafb2e 111
ed4ba7e4
LP
112 assert(b);
113 assert(b->lid_closed);
114
3c56cab4 115 button_lid_switch_handle_action(b->manager, false);
ed4ba7e4
LP
116 return 1;
117}
e9d21f24 118
ed4ba7e4
LP
119static int button_install_check_event_source(Button *b) {
120 int r;
e9d21f24
LP
121 assert(b);
122
ed4ba7e4 123 /* Install a post handler, so that we keep rechecking as long as the lid is closed. */
6de0e0e5 124
ed4ba7e4
LP
125 if (b->check_event_source)
126 return 0;
127
128 r = sd_event_add_post(b->manager->event, &b->check_event_source, button_recheck, b);
129 if (r < 0)
130 return r;
131
132 return sd_event_source_set_priority(b->check_event_source, SD_EVENT_PRIORITY_IDLE+1);
e9d21f24
LP
133}
134
cc377381
LP
135static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
136 Button *b = userdata;
e9d21f24
LP
137 struct input_event ev;
138 ssize_t l;
139
cc377381
LP
140 assert(s);
141 assert(fd == b->fd);
e9d21f24
LP
142 assert(b);
143
144 l = read(b->fd, &ev, sizeof(ev));
145 if (l < 0)
146 return errno != EAGAIN ? -errno : 0;
147 if ((size_t) l < sizeof(ev))
148 return -EIO;
149
e9d21f24
LP
150 if (ev.type == EV_KEY && ev.value > 0) {
151
152 switch (ev.code) {
153
154 case KEY_POWER:
155 case KEY_POWER2:
c485437f 156 log_struct(LOG_INFO,
e2cc6eca
LP
157 LOG_MESSAGE("Power key pressed."),
158 LOG_MESSAGE_ID(SD_MESSAGE_POWER_KEY),
c485437f 159 NULL);
7b77ed8c 160
ed4ba7e4 161 manager_handle_action(b->manager, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true);
7b77ed8c 162 break;
e9d21f24 163
8e7fd6ad
LP
164 /* The kernel is a bit confused here:
165
166 KEY_SLEEP = suspend-to-ram, which everybody else calls "suspend"
167 KEY_SUSPEND = suspend-to-disk, which everybody else calls "hibernate"
168 */
169
e9d21f24 170 case KEY_SLEEP:
c485437f 171 log_struct(LOG_INFO,
e2cc6eca
LP
172 LOG_MESSAGE("Suspend key pressed."),
173 LOG_MESSAGE_ID(SD_MESSAGE_SUSPEND_KEY),
c485437f 174 NULL);
7b77ed8c 175
ed4ba7e4 176 manager_handle_action(b->manager, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true);
7b77ed8c 177 break;
e9d21f24 178
8e7fd6ad 179 case KEY_SUSPEND:
c485437f 180 log_struct(LOG_INFO,
e2cc6eca
LP
181 LOG_MESSAGE("Hibernate key pressed."),
182 LOG_MESSAGE_ID(SD_MESSAGE_HIBERNATE_KEY),
c485437f 183 NULL);
7b77ed8c 184
ed4ba7e4 185 manager_handle_action(b->manager, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true);
7b77ed8c 186 break;
e9d21f24 187 }
8e7fd6ad 188
e9d21f24
LP
189 } else if (ev.type == EV_SW && ev.value > 0) {
190
7b77ed8c 191 if (ev.code == SW_LID) {
c485437f 192 log_struct(LOG_INFO,
e2cc6eca
LP
193 LOG_MESSAGE("Lid closed."),
194 LOG_MESSAGE_ID(SD_MESSAGE_LID_CLOSED),
c485437f 195 NULL);
65b51162 196
ed4ba7e4 197 b->lid_closed = true;
3c56cab4 198 button_lid_switch_handle_action(b->manager, true);
ed4ba7e4 199 button_install_check_event_source(b);
2d62c530
LP
200
201 } else if (ev.code == SW_DOCK) {
202 log_struct(LOG_INFO,
e2cc6eca
LP
203 LOG_MESSAGE("System docked."),
204 LOG_MESSAGE_ID(SD_MESSAGE_SYSTEM_DOCKED),
2d62c530
LP
205 NULL);
206
207 b->docked = true;
65b51162
LP
208 }
209
210 } else if (ev.type == EV_SW && ev.value == 0) {
211
7b77ed8c 212 if (ev.code == SW_LID) {
c485437f 213 log_struct(LOG_INFO,
e2cc6eca
LP
214 LOG_MESSAGE("Lid opened."),
215 LOG_MESSAGE_ID(SD_MESSAGE_LID_OPENED),
c485437f 216 NULL);
7b77ed8c 217
ed4ba7e4
LP
218 b->lid_closed = false;
219 b->check_event_source = sd_event_source_unref(b->check_event_source);
2d62c530
LP
220
221 } else if (ev.code == SW_DOCK) {
222 log_struct(LOG_INFO,
e2cc6eca
LP
223 LOG_MESSAGE("System undocked."),
224 LOG_MESSAGE_ID(SD_MESSAGE_SYSTEM_UNDOCKED),
2d62c530
LP
225 NULL);
226
227 b->docked = false;
e9d21f24
LP
228 }
229 }
230
231 return 0;
232}
233
cc377381
LP
234int button_open(Button *b) {
235 char *p, name[256];
236 int r;
237
238 assert(b);
239
7f6e12b0 240 b->fd = safe_close(b->fd);
cc377381 241
63c372cb 242 p = strjoina("/dev/input/", b->name);
cc377381
LP
243
244 b->fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
4a62c710
MS
245 if (b->fd < 0)
246 return log_warning_errno(errno, "Failed to open %s: %m", b->name);
cc377381
LP
247
248 if (ioctl(b->fd, EVIOCGNAME(sizeof(name)), name) < 0) {
76ef789d 249 r = log_error_errno(errno, "Failed to get input name: %m");
cc377381
LP
250 goto fail;
251 }
252
ed4ba7e4 253 r = sd_event_add_io(b->manager->event, &b->io_event_source, b->fd, EPOLLIN, button_dispatch, b);
cc377381 254 if (r < 0) {
da927ba9 255 log_error_errno(r, "Failed to add button event: %m");
cc377381
LP
256 goto fail;
257 }
258
259 log_info("Watching system buttons on /dev/input/%s (%s)", b->name, name);
260
261 return 0;
262
263fail:
66e40583 264 b->fd = safe_close(b->fd);
cc377381
LP
265 return r;
266}
267
2d62c530 268int button_check_switches(Button *b) {
ed4ba7e4 269 uint8_t switches[SW_MAX/8+1] = {};
65b51162
LP
270 assert(b);
271
ed4ba7e4
LP
272 if (b->fd < 0)
273 return -EINVAL;
274
275 if (ioctl(b->fd, EVIOCGSW(sizeof(switches)), switches) < 0)
276 return -errno;
65b51162 277
ed4ba7e4 278 b->lid_closed = (switches[SW_LID/8] >> (SW_LID % 8)) & 1;
2d62c530 279 b->docked = (switches[SW_DOCK/8] >> (SW_DOCK % 8)) & 1;
ed4ba7e4 280
2d62c530 281 if (b->lid_closed)
ed4ba7e4 282 button_install_check_event_source(b);
ed4ba7e4
LP
283
284 return 0;
65b51162 285}