chiark / gitweb /
Add short-iso-precise for journalctl output (#5884)
[elogind.git] / src / login / logind-button.c
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
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <string.h>
23 #include <sys/ioctl.h>
24 #include <unistd.h>
25 #include <linux/input.h>
26
27 #include "sd-messages.h"
28
29 #include "alloc-util.h"
30 #include "fd-util.h"
31 #include "logind-button.h"
32 #include "string-util.h"
33 #include "util.h"
34
35 Button* 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);
46         if (!b->name)
47                 return mfree(b);
48
49         if (hashmap_put(m->buttons, b->name, b) < 0) {
50                 free(b->name);
51                 return mfree(b);
52         }
53
54         b->manager = m;
55         b->fd = -1;
56
57         return b;
58 }
59
60 void button_free(Button *b) {
61         assert(b);
62
63         hashmap_remove(b->manager->buttons, b->name);
64
65         sd_event_source_unref(b->io_event_source);
66         sd_event_source_unref(b->check_event_source);
67
68         if (b->fd >= 0)
69                 /* If the device has been unplugged close() returns
70                  * ENODEV, let's ignore this, hence we don't use
71                  * safe_close() */
72                 (void) close(b->fd);
73
74         free(b->name);
75         free(b->seat);
76         free(b);
77 }
78
79 int 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
95 static 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 */
101         if (manager_is_docked_or_external_displays(manager))
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
109 static int button_recheck(sd_event_source *e, void *userdata) {
110         Button *b = userdata;
111
112         assert(b);
113         assert(b->lid_closed);
114
115         button_lid_switch_handle_action(b->manager, false);
116         return 1;
117 }
118
119 static int button_install_check_event_source(Button *b) {
120         int r;
121         assert(b);
122
123         /* Install a post handler, so that we keep rechecking as long as the lid is closed. */
124
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);
133 }
134
135 static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
136         Button *b = userdata;
137         struct input_event ev;
138         ssize_t l;
139
140         assert(s);
141         assert(fd == b->fd);
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
150         if (ev.type == EV_KEY && ev.value > 0) {
151
152                 switch (ev.code) {
153
154                 case KEY_POWER:
155                 case KEY_POWER2:
156                         log_struct(LOG_INFO,
157                                    LOG_MESSAGE("Power key pressed."),
158                                    "MESSAGE_ID=" SD_MESSAGE_POWER_KEY_STR,
159                                    NULL);
160
161                         manager_handle_action(b->manager, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true);
162                         break;
163
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
170                 case KEY_SLEEP:
171                         log_struct(LOG_INFO,
172                                    LOG_MESSAGE("Suspend key pressed."),
173                                    "MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_STR,
174                                    NULL);
175
176                         manager_handle_action(b->manager, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true);
177                         break;
178
179                 case KEY_SUSPEND:
180                         log_struct(LOG_INFO,
181                                    LOG_MESSAGE("Hibernate key pressed."),
182                                    "MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_STR,
183                                    NULL);
184
185                         manager_handle_action(b->manager, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true);
186                         break;
187                 }
188
189         } else if (ev.type == EV_SW && ev.value > 0) {
190
191                 if (ev.code == SW_LID) {
192                         log_struct(LOG_INFO,
193                                    LOG_MESSAGE("Lid closed."),
194                                    "MESSAGE_ID=" SD_MESSAGE_LID_CLOSED_STR,
195                                    NULL);
196
197                         b->lid_closed = true;
198                         button_lid_switch_handle_action(b->manager, true);
199                         button_install_check_event_source(b);
200
201                 } else if (ev.code == SW_DOCK) {
202                         log_struct(LOG_INFO,
203                                    LOG_MESSAGE("System docked."),
204                                    "MESSAGE_ID=" SD_MESSAGE_SYSTEM_DOCKED_STR,
205                                    NULL);
206
207                         b->docked = true;
208                 }
209
210         } else if (ev.type == EV_SW && ev.value == 0) {
211
212                 if (ev.code == SW_LID) {
213                         log_struct(LOG_INFO,
214                                    LOG_MESSAGE("Lid opened."),
215                                    "MESSAGE_ID=" SD_MESSAGE_LID_OPENED_STR,
216                                    NULL);
217
218                         b->lid_closed = false;
219                         b->check_event_source = sd_event_source_unref(b->check_event_source);
220
221                 } else if (ev.code == SW_DOCK) {
222                         log_struct(LOG_INFO,
223                                    LOG_MESSAGE("System undocked."),
224                                    "MESSAGE_ID=" SD_MESSAGE_SYSTEM_UNDOCKED_STR,
225                                    NULL);
226
227                         b->docked = false;
228                 }
229         }
230
231         return 0;
232 }
233
234 int button_open(Button *b) {
235         char *p, name[256];
236         int r;
237
238         assert(b);
239
240         b->fd = safe_close(b->fd);
241
242         p = strjoina("/dev/input/", b->name);
243
244         b->fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
245         if (b->fd < 0)
246                 return log_warning_errno(errno, "Failed to open %s: %m", b->name);
247
248         if (ioctl(b->fd, EVIOCGNAME(sizeof(name)), name) < 0) {
249                 r = log_error_errno(errno, "Failed to get input name: %m");
250                 goto fail;
251         }
252
253         r = sd_event_add_io(b->manager->event, &b->io_event_source, b->fd, EPOLLIN, button_dispatch, b);
254         if (r < 0) {
255                 log_error_errno(r, "Failed to add button event: %m");
256                 goto fail;
257         }
258
259         log_info("Watching system buttons on /dev/input/%s (%s)", b->name, name);
260
261         return 0;
262
263 fail:
264         b->fd = safe_close(b->fd);
265         return r;
266 }
267
268 int button_check_switches(Button *b) {
269         uint8_t switches[SW_MAX/8+1] = {};
270         assert(b);
271
272         if (b->fd < 0)
273                 return -EINVAL;
274
275         if (ioctl(b->fd, EVIOCGSW(sizeof(switches)), switches) < 0)
276                 return -errno;
277
278         b->lid_closed = (switches[SW_LID/8] >> (SW_LID % 8)) & 1;
279         b->docked = (switches[SW_DOCK/8] >> (SW_DOCK % 8)) & 1;
280
281         if (b->lid_closed)
282                 button_install_check_event_source(b);
283
284         return 0;
285 }