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