2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2012-05-30 15:07:19 +02:00
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2012 Lennart Poettering
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <string.h>
|
2012-05-30 15:07:19 +02:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <linux/input.h>
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
#include "sd-messages.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
|
|
|
#include "logind-button.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2012-05-30 15:07:19 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
2017-06-26 21:35:12 +02:00
|
|
|
#define CONST_MAX4(a, b, c, d) CONST_MAX(CONST_MAX(a, b), CONST_MAX(c, d))
|
|
|
|
|
|
|
|
#define ULONG_BITS (sizeof(unsigned long)*8)
|
|
|
|
|
|
|
|
static bool bitset_get(const unsigned long *bits, unsigned i) {
|
|
|
|
return (bits[i / ULONG_BITS] >> (i % ULONG_BITS)) & 1UL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bitset_put(unsigned long *bits, unsigned i) {
|
|
|
|
bits[i / ULONG_BITS] |= (unsigned long) 1 << (i % ULONG_BITS);
|
|
|
|
}
|
|
|
|
|
2012-05-30 15:07:19 +02:00
|
|
|
Button* button_new(Manager *m, const char *name) {
|
|
|
|
Button *b;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
assert(name);
|
|
|
|
|
|
|
|
b = new0(Button, 1);
|
|
|
|
if (!b)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
b->name = strdup(name);
|
2016-10-17 00:28:30 +02:00
|
|
|
if (!b->name)
|
|
|
|
return mfree(b);
|
2012-05-30 15:07:19 +02:00
|
|
|
|
|
|
|
if (hashmap_put(m->buttons, b->name, b) < 0) {
|
|
|
|
free(b->name);
|
2016-10-17 00:28:30 +02:00
|
|
|
return mfree(b);
|
2012-05-30 15:07:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
b->manager = m;
|
|
|
|
b->fd = -1;
|
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
void button_free(Button *b) {
|
|
|
|
assert(b);
|
|
|
|
|
|
|
|
hashmap_remove(b->manager->buttons, b->name);
|
|
|
|
|
2014-02-21 21:10:00 +01:00
|
|
|
sd_event_source_unref(b->io_event_source);
|
|
|
|
sd_event_source_unref(b->check_event_source);
|
2013-03-07 16:48:14 +01:00
|
|
|
|
2015-09-08 23:03:38 +02:00
|
|
|
if (b->fd >= 0)
|
2013-03-07 16:48:14 +01:00
|
|
|
/* If the device has been unplugged close() returns
|
|
|
|
* ENODEV, let's ignore this, hence we don't use
|
2014-03-18 19:22:43 +01:00
|
|
|
* safe_close() */
|
2015-06-16 01:55:20 +02:00
|
|
|
(void) close(b->fd);
|
2012-05-30 15:07:19 +02:00
|
|
|
|
|
|
|
free(b->name);
|
|
|
|
free(b->seat);
|
|
|
|
free(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
int button_set_seat(Button *b, const char *sn) {
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
assert(b);
|
|
|
|
assert(sn);
|
|
|
|
|
|
|
|
s = strdup(sn);
|
|
|
|
if (!s)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
free(b->seat);
|
|
|
|
b->seat = s;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-26 22:08:02 +02:00
|
|
|
static void button_lid_switch_handle_action(Manager *manager, bool is_edge) {
|
|
|
|
HandleAction handle_action;
|
|
|
|
|
|
|
|
assert(manager);
|
|
|
|
|
|
|
|
/* If we are docked, handle the lid switch differently */
|
2015-06-17 00:24:05 +02:00
|
|
|
if (manager_is_docked_or_external_displays(manager))
|
2014-08-26 22:08:02 +02:00
|
|
|
handle_action = manager->handle_lid_switch_docked;
|
|
|
|
else
|
|
|
|
handle_action = manager->handle_lid_switch;
|
|
|
|
|
|
|
|
manager_handle_action(manager, INHIBIT_HANDLE_LID_SWITCH, handle_action, manager->lid_switch_ignore_inhibited, is_edge);
|
|
|
|
}
|
|
|
|
|
2014-02-21 21:10:00 +01:00
|
|
|
static int button_recheck(sd_event_source *e, void *userdata) {
|
|
|
|
Button *b = userdata;
|
2012-09-19 13:10:10 +02:00
|
|
|
|
2014-02-21 21:10:00 +01:00
|
|
|
assert(b);
|
|
|
|
assert(b->lid_closed);
|
|
|
|
|
2014-08-26 22:08:02 +02:00
|
|
|
button_lid_switch_handle_action(b->manager, false);
|
2014-02-21 21:10:00 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2012-05-30 15:07:19 +02:00
|
|
|
|
2014-02-21 21:10:00 +01:00
|
|
|
static int button_install_check_event_source(Button *b) {
|
|
|
|
int r;
|
2012-05-30 15:07:19 +02:00
|
|
|
assert(b);
|
|
|
|
|
2014-02-21 21:10:00 +01:00
|
|
|
/* Install a post handler, so that we keep rechecking as long as the lid is closed. */
|
2012-05-30 21:40:32 +02:00
|
|
|
|
2014-02-21 21:10:00 +01:00
|
|
|
if (b->check_event_source)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
r = sd_event_add_post(b->manager->event, &b->check_event_source, button_recheck, b);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return sd_event_source_set_priority(b->check_event_source, SD_EVENT_PRIORITY_IDLE+1);
|
2012-05-30 15:07:19 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
|
|
|
|
Button *b = userdata;
|
2012-05-30 15:07:19 +02:00
|
|
|
struct input_event ev;
|
|
|
|
ssize_t l;
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
assert(s);
|
|
|
|
assert(fd == b->fd);
|
2012-05-30 15:07:19 +02:00
|
|
|
assert(b);
|
|
|
|
|
|
|
|
l = read(b->fd, &ev, sizeof(ev));
|
|
|
|
if (l < 0)
|
|
|
|
return errno != EAGAIN ? -errno : 0;
|
|
|
|
if ((size_t) l < sizeof(ev))
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
if (ev.type == EV_KEY && ev.value > 0) {
|
|
|
|
|
|
|
|
switch (ev.code) {
|
|
|
|
|
|
|
|
case KEY_POWER:
|
|
|
|
case KEY_POWER2:
|
2013-01-28 03:14:14 +01:00
|
|
|
log_struct(LOG_INFO,
|
2014-11-28 02:05:14 +01:00
|
|
|
LOG_MESSAGE("Power key pressed."),
|
tree-wide: add SD_ID128_MAKE_STR, remove LOG_MESSAGE_ID
Embedding sd_id128_t's in constant strings was rather cumbersome. We had
SD_ID128_CONST_STR which returned a const char[], but it had two problems:
- it wasn't possible to statically concatanate this array with a normal string
- gcc wasn't really able to optimize this, and generated code to perform the
"conversion" at runtime.
Because of this, even our own code in coredumpctl wasn't using
SD_ID128_CONST_STR.
Add a new macro to generate a constant string: SD_ID128_MAKE_STR.
It is not as elegant as SD_ID128_CONST_STR, because it requires a repetition
of the numbers, but in practice it is more convenient to use, and allows gcc
to generate smarter code:
$ size .libs/systemd{,-logind,-journald}{.old,}
text data bss dec hex filename
1265204 149564 4808 1419576 15a938 .libs/systemd.old
1260268 149564 4808 1414640 1595f0 .libs/systemd
246805 13852 209 260866 3fb02 .libs/systemd-logind.old
240973 13852 209 255034 3e43a .libs/systemd-logind
146839 4984 34 151857 25131 .libs/systemd-journald.old
146391 4984 34 151409 24f71 .libs/systemd-journald
It is also much easier to check if a certain binary uses a certain MESSAGE_ID:
$ strings .libs/systemd.old|grep MESSAGE_ID
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
$ strings .libs/systemd|grep MESSAGE_ID
MESSAGE_ID=c7a787079b354eaaa9e77b371893cd27
MESSAGE_ID=b07a249cd024414a82dd00cd181378ff
MESSAGE_ID=641257651c1b4ec9a8624d7a40a9e1e7
MESSAGE_ID=de5b426a63be47a7b6ac3eaac82e2f6f
MESSAGE_ID=d34d037fff1847e6ae669a370e694725
MESSAGE_ID=7d4958e842da4a758f6c1cdc7b36dcc5
MESSAGE_ID=1dee0369c7fc4736b7099b38ecb46ee7
MESSAGE_ID=39f53479d3a045ac8e11786248231fbf
MESSAGE_ID=be02cf6855d2428ba40df7e9d022f03d
MESSAGE_ID=7b05ebc668384222baa8881179cfda54
MESSAGE_ID=9d1aaa27d60140bd96365438aad20286
2016-11-06 18:48:23 +01:00
|
|
|
"MESSAGE_ID=" SD_MESSAGE_POWER_KEY_STR,
|
2013-01-28 03:14:14 +01:00
|
|
|
NULL);
|
2013-12-13 03:30:42 +01:00
|
|
|
|
2014-02-21 21:10:00 +01:00
|
|
|
manager_handle_action(b->manager, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true);
|
2013-12-13 03:30:42 +01:00
|
|
|
break;
|
2012-05-30 15:07:19 +02:00
|
|
|
|
2012-09-21 15:44:14 +02:00
|
|
|
/* The kernel is a bit confused here:
|
|
|
|
|
|
|
|
KEY_SLEEP = suspend-to-ram, which everybody else calls "suspend"
|
|
|
|
KEY_SUSPEND = suspend-to-disk, which everybody else calls "hibernate"
|
|
|
|
*/
|
|
|
|
|
2012-05-30 15:07:19 +02:00
|
|
|
case KEY_SLEEP:
|
2013-01-28 03:14:14 +01:00
|
|
|
log_struct(LOG_INFO,
|
2014-11-28 02:05:14 +01:00
|
|
|
LOG_MESSAGE("Suspend key pressed."),
|
tree-wide: add SD_ID128_MAKE_STR, remove LOG_MESSAGE_ID
Embedding sd_id128_t's in constant strings was rather cumbersome. We had
SD_ID128_CONST_STR which returned a const char[], but it had two problems:
- it wasn't possible to statically concatanate this array with a normal string
- gcc wasn't really able to optimize this, and generated code to perform the
"conversion" at runtime.
Because of this, even our own code in coredumpctl wasn't using
SD_ID128_CONST_STR.
Add a new macro to generate a constant string: SD_ID128_MAKE_STR.
It is not as elegant as SD_ID128_CONST_STR, because it requires a repetition
of the numbers, but in practice it is more convenient to use, and allows gcc
to generate smarter code:
$ size .libs/systemd{,-logind,-journald}{.old,}
text data bss dec hex filename
1265204 149564 4808 1419576 15a938 .libs/systemd.old
1260268 149564 4808 1414640 1595f0 .libs/systemd
246805 13852 209 260866 3fb02 .libs/systemd-logind.old
240973 13852 209 255034 3e43a .libs/systemd-logind
146839 4984 34 151857 25131 .libs/systemd-journald.old
146391 4984 34 151409 24f71 .libs/systemd-journald
It is also much easier to check if a certain binary uses a certain MESSAGE_ID:
$ strings .libs/systemd.old|grep MESSAGE_ID
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
$ strings .libs/systemd|grep MESSAGE_ID
MESSAGE_ID=c7a787079b354eaaa9e77b371893cd27
MESSAGE_ID=b07a249cd024414a82dd00cd181378ff
MESSAGE_ID=641257651c1b4ec9a8624d7a40a9e1e7
MESSAGE_ID=de5b426a63be47a7b6ac3eaac82e2f6f
MESSAGE_ID=d34d037fff1847e6ae669a370e694725
MESSAGE_ID=7d4958e842da4a758f6c1cdc7b36dcc5
MESSAGE_ID=1dee0369c7fc4736b7099b38ecb46ee7
MESSAGE_ID=39f53479d3a045ac8e11786248231fbf
MESSAGE_ID=be02cf6855d2428ba40df7e9d022f03d
MESSAGE_ID=7b05ebc668384222baa8881179cfda54
MESSAGE_ID=9d1aaa27d60140bd96365438aad20286
2016-11-06 18:48:23 +01:00
|
|
|
"MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_STR,
|
2013-01-28 03:14:14 +01:00
|
|
|
NULL);
|
2013-12-13 03:30:42 +01:00
|
|
|
|
2014-02-21 21:10:00 +01:00
|
|
|
manager_handle_action(b->manager, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true);
|
2013-12-13 03:30:42 +01:00
|
|
|
break;
|
2012-05-30 15:07:19 +02:00
|
|
|
|
2012-09-21 15:44:14 +02:00
|
|
|
case KEY_SUSPEND:
|
2013-01-28 03:14:14 +01:00
|
|
|
log_struct(LOG_INFO,
|
2014-11-28 02:05:14 +01:00
|
|
|
LOG_MESSAGE("Hibernate key pressed."),
|
tree-wide: add SD_ID128_MAKE_STR, remove LOG_MESSAGE_ID
Embedding sd_id128_t's in constant strings was rather cumbersome. We had
SD_ID128_CONST_STR which returned a const char[], but it had two problems:
- it wasn't possible to statically concatanate this array with a normal string
- gcc wasn't really able to optimize this, and generated code to perform the
"conversion" at runtime.
Because of this, even our own code in coredumpctl wasn't using
SD_ID128_CONST_STR.
Add a new macro to generate a constant string: SD_ID128_MAKE_STR.
It is not as elegant as SD_ID128_CONST_STR, because it requires a repetition
of the numbers, but in practice it is more convenient to use, and allows gcc
to generate smarter code:
$ size .libs/systemd{,-logind,-journald}{.old,}
text data bss dec hex filename
1265204 149564 4808 1419576 15a938 .libs/systemd.old
1260268 149564 4808 1414640 1595f0 .libs/systemd
246805 13852 209 260866 3fb02 .libs/systemd-logind.old
240973 13852 209 255034 3e43a .libs/systemd-logind
146839 4984 34 151857 25131 .libs/systemd-journald.old
146391 4984 34 151409 24f71 .libs/systemd-journald
It is also much easier to check if a certain binary uses a certain MESSAGE_ID:
$ strings .libs/systemd.old|grep MESSAGE_ID
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
$ strings .libs/systemd|grep MESSAGE_ID
MESSAGE_ID=c7a787079b354eaaa9e77b371893cd27
MESSAGE_ID=b07a249cd024414a82dd00cd181378ff
MESSAGE_ID=641257651c1b4ec9a8624d7a40a9e1e7
MESSAGE_ID=de5b426a63be47a7b6ac3eaac82e2f6f
MESSAGE_ID=d34d037fff1847e6ae669a370e694725
MESSAGE_ID=7d4958e842da4a758f6c1cdc7b36dcc5
MESSAGE_ID=1dee0369c7fc4736b7099b38ecb46ee7
MESSAGE_ID=39f53479d3a045ac8e11786248231fbf
MESSAGE_ID=be02cf6855d2428ba40df7e9d022f03d
MESSAGE_ID=7b05ebc668384222baa8881179cfda54
MESSAGE_ID=9d1aaa27d60140bd96365438aad20286
2016-11-06 18:48:23 +01:00
|
|
|
"MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_STR,
|
2013-01-28 03:14:14 +01:00
|
|
|
NULL);
|
2013-12-13 03:30:42 +01:00
|
|
|
|
2014-02-21 21:10:00 +01:00
|
|
|
manager_handle_action(b->manager, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true);
|
2013-12-13 03:30:42 +01:00
|
|
|
break;
|
2012-05-30 15:07:19 +02:00
|
|
|
}
|
2012-09-21 15:44:14 +02:00
|
|
|
|
2012-05-30 15:07:19 +02:00
|
|
|
} else if (ev.type == EV_SW && ev.value > 0) {
|
|
|
|
|
2013-12-13 03:30:42 +01:00
|
|
|
if (ev.code == SW_LID) {
|
2013-01-28 03:14:14 +01:00
|
|
|
log_struct(LOG_INFO,
|
2014-11-28 02:05:14 +01:00
|
|
|
LOG_MESSAGE("Lid closed."),
|
tree-wide: add SD_ID128_MAKE_STR, remove LOG_MESSAGE_ID
Embedding sd_id128_t's in constant strings was rather cumbersome. We had
SD_ID128_CONST_STR which returned a const char[], but it had two problems:
- it wasn't possible to statically concatanate this array with a normal string
- gcc wasn't really able to optimize this, and generated code to perform the
"conversion" at runtime.
Because of this, even our own code in coredumpctl wasn't using
SD_ID128_CONST_STR.
Add a new macro to generate a constant string: SD_ID128_MAKE_STR.
It is not as elegant as SD_ID128_CONST_STR, because it requires a repetition
of the numbers, but in practice it is more convenient to use, and allows gcc
to generate smarter code:
$ size .libs/systemd{,-logind,-journald}{.old,}
text data bss dec hex filename
1265204 149564 4808 1419576 15a938 .libs/systemd.old
1260268 149564 4808 1414640 1595f0 .libs/systemd
246805 13852 209 260866 3fb02 .libs/systemd-logind.old
240973 13852 209 255034 3e43a .libs/systemd-logind
146839 4984 34 151857 25131 .libs/systemd-journald.old
146391 4984 34 151409 24f71 .libs/systemd-journald
It is also much easier to check if a certain binary uses a certain MESSAGE_ID:
$ strings .libs/systemd.old|grep MESSAGE_ID
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
$ strings .libs/systemd|grep MESSAGE_ID
MESSAGE_ID=c7a787079b354eaaa9e77b371893cd27
MESSAGE_ID=b07a249cd024414a82dd00cd181378ff
MESSAGE_ID=641257651c1b4ec9a8624d7a40a9e1e7
MESSAGE_ID=de5b426a63be47a7b6ac3eaac82e2f6f
MESSAGE_ID=d34d037fff1847e6ae669a370e694725
MESSAGE_ID=7d4958e842da4a758f6c1cdc7b36dcc5
MESSAGE_ID=1dee0369c7fc4736b7099b38ecb46ee7
MESSAGE_ID=39f53479d3a045ac8e11786248231fbf
MESSAGE_ID=be02cf6855d2428ba40df7e9d022f03d
MESSAGE_ID=7b05ebc668384222baa8881179cfda54
MESSAGE_ID=9d1aaa27d60140bd96365438aad20286
2016-11-06 18:48:23 +01:00
|
|
|
"MESSAGE_ID=" SD_MESSAGE_LID_CLOSED_STR,
|
2013-01-28 03:14:14 +01:00
|
|
|
NULL);
|
2012-09-19 15:42:29 +02:00
|
|
|
|
2014-02-21 21:10:00 +01:00
|
|
|
b->lid_closed = true;
|
2014-08-26 22:08:02 +02:00
|
|
|
button_lid_switch_handle_action(b->manager, true);
|
2014-02-21 21:10:00 +01:00
|
|
|
button_install_check_event_source(b);
|
2014-02-24 16:22:23 +01:00
|
|
|
|
|
|
|
} else if (ev.code == SW_DOCK) {
|
|
|
|
log_struct(LOG_INFO,
|
2014-11-28 02:05:14 +01:00
|
|
|
LOG_MESSAGE("System docked."),
|
tree-wide: add SD_ID128_MAKE_STR, remove LOG_MESSAGE_ID
Embedding sd_id128_t's in constant strings was rather cumbersome. We had
SD_ID128_CONST_STR which returned a const char[], but it had two problems:
- it wasn't possible to statically concatanate this array with a normal string
- gcc wasn't really able to optimize this, and generated code to perform the
"conversion" at runtime.
Because of this, even our own code in coredumpctl wasn't using
SD_ID128_CONST_STR.
Add a new macro to generate a constant string: SD_ID128_MAKE_STR.
It is not as elegant as SD_ID128_CONST_STR, because it requires a repetition
of the numbers, but in practice it is more convenient to use, and allows gcc
to generate smarter code:
$ size .libs/systemd{,-logind,-journald}{.old,}
text data bss dec hex filename
1265204 149564 4808 1419576 15a938 .libs/systemd.old
1260268 149564 4808 1414640 1595f0 .libs/systemd
246805 13852 209 260866 3fb02 .libs/systemd-logind.old
240973 13852 209 255034 3e43a .libs/systemd-logind
146839 4984 34 151857 25131 .libs/systemd-journald.old
146391 4984 34 151409 24f71 .libs/systemd-journald
It is also much easier to check if a certain binary uses a certain MESSAGE_ID:
$ strings .libs/systemd.old|grep MESSAGE_ID
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
$ strings .libs/systemd|grep MESSAGE_ID
MESSAGE_ID=c7a787079b354eaaa9e77b371893cd27
MESSAGE_ID=b07a249cd024414a82dd00cd181378ff
MESSAGE_ID=641257651c1b4ec9a8624d7a40a9e1e7
MESSAGE_ID=de5b426a63be47a7b6ac3eaac82e2f6f
MESSAGE_ID=d34d037fff1847e6ae669a370e694725
MESSAGE_ID=7d4958e842da4a758f6c1cdc7b36dcc5
MESSAGE_ID=1dee0369c7fc4736b7099b38ecb46ee7
MESSAGE_ID=39f53479d3a045ac8e11786248231fbf
MESSAGE_ID=be02cf6855d2428ba40df7e9d022f03d
MESSAGE_ID=7b05ebc668384222baa8881179cfda54
MESSAGE_ID=9d1aaa27d60140bd96365438aad20286
2016-11-06 18:48:23 +01:00
|
|
|
"MESSAGE_ID=" SD_MESSAGE_SYSTEM_DOCKED_STR,
|
2014-02-24 16:22:23 +01:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
b->docked = true;
|
2012-09-19 15:42:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (ev.type == EV_SW && ev.value == 0) {
|
|
|
|
|
2013-12-13 03:30:42 +01:00
|
|
|
if (ev.code == SW_LID) {
|
2013-01-28 03:14:14 +01:00
|
|
|
log_struct(LOG_INFO,
|
2014-11-28 02:05:14 +01:00
|
|
|
LOG_MESSAGE("Lid opened."),
|
tree-wide: add SD_ID128_MAKE_STR, remove LOG_MESSAGE_ID
Embedding sd_id128_t's in constant strings was rather cumbersome. We had
SD_ID128_CONST_STR which returned a const char[], but it had two problems:
- it wasn't possible to statically concatanate this array with a normal string
- gcc wasn't really able to optimize this, and generated code to perform the
"conversion" at runtime.
Because of this, even our own code in coredumpctl wasn't using
SD_ID128_CONST_STR.
Add a new macro to generate a constant string: SD_ID128_MAKE_STR.
It is not as elegant as SD_ID128_CONST_STR, because it requires a repetition
of the numbers, but in practice it is more convenient to use, and allows gcc
to generate smarter code:
$ size .libs/systemd{,-logind,-journald}{.old,}
text data bss dec hex filename
1265204 149564 4808 1419576 15a938 .libs/systemd.old
1260268 149564 4808 1414640 1595f0 .libs/systemd
246805 13852 209 260866 3fb02 .libs/systemd-logind.old
240973 13852 209 255034 3e43a .libs/systemd-logind
146839 4984 34 151857 25131 .libs/systemd-journald.old
146391 4984 34 151409 24f71 .libs/systemd-journald
It is also much easier to check if a certain binary uses a certain MESSAGE_ID:
$ strings .libs/systemd.old|grep MESSAGE_ID
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
$ strings .libs/systemd|grep MESSAGE_ID
MESSAGE_ID=c7a787079b354eaaa9e77b371893cd27
MESSAGE_ID=b07a249cd024414a82dd00cd181378ff
MESSAGE_ID=641257651c1b4ec9a8624d7a40a9e1e7
MESSAGE_ID=de5b426a63be47a7b6ac3eaac82e2f6f
MESSAGE_ID=d34d037fff1847e6ae669a370e694725
MESSAGE_ID=7d4958e842da4a758f6c1cdc7b36dcc5
MESSAGE_ID=1dee0369c7fc4736b7099b38ecb46ee7
MESSAGE_ID=39f53479d3a045ac8e11786248231fbf
MESSAGE_ID=be02cf6855d2428ba40df7e9d022f03d
MESSAGE_ID=7b05ebc668384222baa8881179cfda54
MESSAGE_ID=9d1aaa27d60140bd96365438aad20286
2016-11-06 18:48:23 +01:00
|
|
|
"MESSAGE_ID=" SD_MESSAGE_LID_OPENED_STR,
|
2013-01-28 03:14:14 +01:00
|
|
|
NULL);
|
2013-12-13 03:30:42 +01:00
|
|
|
|
2014-02-21 21:10:00 +01:00
|
|
|
b->lid_closed = false;
|
|
|
|
b->check_event_source = sd_event_source_unref(b->check_event_source);
|
2014-02-24 16:22:23 +01:00
|
|
|
|
|
|
|
} else if (ev.code == SW_DOCK) {
|
|
|
|
log_struct(LOG_INFO,
|
2014-11-28 02:05:14 +01:00
|
|
|
LOG_MESSAGE("System undocked."),
|
tree-wide: add SD_ID128_MAKE_STR, remove LOG_MESSAGE_ID
Embedding sd_id128_t's in constant strings was rather cumbersome. We had
SD_ID128_CONST_STR which returned a const char[], but it had two problems:
- it wasn't possible to statically concatanate this array with a normal string
- gcc wasn't really able to optimize this, and generated code to perform the
"conversion" at runtime.
Because of this, even our own code in coredumpctl wasn't using
SD_ID128_CONST_STR.
Add a new macro to generate a constant string: SD_ID128_MAKE_STR.
It is not as elegant as SD_ID128_CONST_STR, because it requires a repetition
of the numbers, but in practice it is more convenient to use, and allows gcc
to generate smarter code:
$ size .libs/systemd{,-logind,-journald}{.old,}
text data bss dec hex filename
1265204 149564 4808 1419576 15a938 .libs/systemd.old
1260268 149564 4808 1414640 1595f0 .libs/systemd
246805 13852 209 260866 3fb02 .libs/systemd-logind.old
240973 13852 209 255034 3e43a .libs/systemd-logind
146839 4984 34 151857 25131 .libs/systemd-journald.old
146391 4984 34 151409 24f71 .libs/systemd-journald
It is also much easier to check if a certain binary uses a certain MESSAGE_ID:
$ strings .libs/systemd.old|grep MESSAGE_ID
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
$ strings .libs/systemd|grep MESSAGE_ID
MESSAGE_ID=c7a787079b354eaaa9e77b371893cd27
MESSAGE_ID=b07a249cd024414a82dd00cd181378ff
MESSAGE_ID=641257651c1b4ec9a8624d7a40a9e1e7
MESSAGE_ID=de5b426a63be47a7b6ac3eaac82e2f6f
MESSAGE_ID=d34d037fff1847e6ae669a370e694725
MESSAGE_ID=7d4958e842da4a758f6c1cdc7b36dcc5
MESSAGE_ID=1dee0369c7fc4736b7099b38ecb46ee7
MESSAGE_ID=39f53479d3a045ac8e11786248231fbf
MESSAGE_ID=be02cf6855d2428ba40df7e9d022f03d
MESSAGE_ID=7b05ebc668384222baa8881179cfda54
MESSAGE_ID=9d1aaa27d60140bd96365438aad20286
2016-11-06 18:48:23 +01:00
|
|
|
"MESSAGE_ID=" SD_MESSAGE_SYSTEM_UNDOCKED_STR,
|
2014-02-24 16:22:23 +01:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
b->docked = false;
|
2012-05-30 15:07:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-27 17:46:28 +02:00
|
|
|
static int button_suitable(Button *b) {
|
|
|
|
unsigned long types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1];
|
|
|
|
|
|
|
|
assert(b);
|
|
|
|
assert(b->fd);
|
|
|
|
|
|
|
|
if (ioctl(b->fd, EVIOCGBIT(EV_SYN, sizeof(types)), types) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
if (bitset_get(types, EV_KEY)) {
|
|
|
|
unsigned long keys[CONST_MAX4(KEY_POWER, KEY_POWER2, KEY_SLEEP, KEY_SUSPEND)/ULONG_BITS+1];
|
|
|
|
|
|
|
|
if (ioctl(b->fd, EVIOCGBIT(EV_KEY, sizeof(keys)), keys) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
if (bitset_get(keys, KEY_POWER) ||
|
|
|
|
bitset_get(keys, KEY_POWER2) ||
|
|
|
|
bitset_get(keys, KEY_SLEEP) ||
|
|
|
|
bitset_get(keys, KEY_SUSPEND))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bitset_get(types, EV_SW)) {
|
|
|
|
unsigned long switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1];
|
|
|
|
|
|
|
|
if (ioctl(b->fd, EVIOCGBIT(EV_SW, sizeof(switches)), switches) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
if (bitset_get(switches, SW_LID) ||
|
|
|
|
bitset_get(switches, SW_DOCK))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-26 21:35:12 +02:00
|
|
|
static int button_set_mask(Button *b) {
|
|
|
|
unsigned long
|
|
|
|
types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1] = {},
|
|
|
|
keys[CONST_MAX4(KEY_POWER, KEY_POWER2, KEY_SLEEP, KEY_SUSPEND)/ULONG_BITS+1] = {},
|
|
|
|
switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1] = {};
|
|
|
|
struct input_mask mask;
|
|
|
|
|
|
|
|
assert(b);
|
|
|
|
assert(b->fd >= 0);
|
|
|
|
|
|
|
|
bitset_put(types, EV_KEY);
|
|
|
|
bitset_put(types, EV_SW);
|
|
|
|
|
|
|
|
mask = (struct input_mask) {
|
|
|
|
.type = EV_SYN,
|
|
|
|
.codes_size = sizeof(types),
|
|
|
|
.codes_ptr = PTR_TO_UINT64(types),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (ioctl(b->fd, EVIOCSMASK, &mask) < 0)
|
|
|
|
/* Log only at debug level if the kernel doesn't do EVIOCSMASK yet */
|
|
|
|
return log_full_errno(IN_SET(errno, ENOTTY, EOPNOTSUPP, EINVAL) ? LOG_DEBUG : LOG_WARNING,
|
|
|
|
errno, "Failed to set EV_SYN event mask on /dev/input/%s: %m", b->name);
|
|
|
|
|
|
|
|
bitset_put(keys, KEY_POWER);
|
|
|
|
bitset_put(keys, KEY_POWER2);
|
|
|
|
bitset_put(keys, KEY_SLEEP);
|
|
|
|
bitset_put(keys, KEY_SUSPEND);
|
|
|
|
|
|
|
|
mask = (struct input_mask) {
|
|
|
|
.type = EV_KEY,
|
|
|
|
.codes_size = sizeof(keys),
|
|
|
|
.codes_ptr = PTR_TO_UINT64(keys),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (ioctl(b->fd, EVIOCSMASK, &mask) < 0)
|
|
|
|
return log_warning_errno(errno, "Failed to set EV_KEY event mask on /dev/input/%s: %m", b->name);
|
|
|
|
|
|
|
|
bitset_put(switches, SW_LID);
|
|
|
|
bitset_put(switches, SW_DOCK);
|
|
|
|
|
|
|
|
mask = (struct input_mask) {
|
|
|
|
.type = EV_SW,
|
|
|
|
.codes_size = sizeof(switches),
|
|
|
|
.codes_ptr = PTR_TO_UINT64(switches),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (ioctl(b->fd, EVIOCSMASK, &mask) < 0)
|
|
|
|
return log_warning_errno(errno, "Failed to set EV_SW event mask on /dev/input/%s: %m", b->name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
int button_open(Button *b) {
|
|
|
|
char *p, name[256];
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(b);
|
|
|
|
|
2015-09-08 18:58:28 +02:00
|
|
|
b->fd = safe_close(b->fd);
|
2013-11-05 01:10:21 +01:00
|
|
|
|
2015-02-03 02:05:59 +01:00
|
|
|
p = strjoina("/dev/input/", b->name);
|
2013-11-05 01:10:21 +01:00
|
|
|
|
|
|
|
b->fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
|
2014-11-28 19:57:32 +01:00
|
|
|
if (b->fd < 0)
|
2017-06-27 17:46:28 +02:00
|
|
|
return log_warning_errno(errno, "Failed to open %s: %m", p);
|
|
|
|
|
|
|
|
r = button_suitable(b);
|
|
|
|
if (r < 0)
|
|
|
|
return log_warning_errno(r, "Failed to determine whether input device is relevant to us: %m");
|
|
|
|
if (r == 0) {
|
|
|
|
log_debug("Device %s does not expose keys or switches relevant to us, ignoring.", p);
|
|
|
|
return -EADDRNOTAVAIL;
|
|
|
|
}
|
2013-11-05 01:10:21 +01:00
|
|
|
|
|
|
|
if (ioctl(b->fd, EVIOCGNAME(sizeof(name)), name) < 0) {
|
2015-09-08 19:30:45 +02:00
|
|
|
r = log_error_errno(errno, "Failed to get input name: %m");
|
2013-11-05 01:10:21 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2017-06-26 21:35:12 +02:00
|
|
|
(void) button_set_mask(b);
|
|
|
|
|
2014-02-21 21:10:00 +01:00
|
|
|
r = sd_event_add_io(b->manager->event, &b->io_event_source, b->fd, EPOLLIN, button_dispatch, b);
|
2013-11-05 01:10:21 +01:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to add button event: %m");
|
2013-11-05 01:10:21 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_info("Watching system buttons on /dev/input/%s (%s)", b->name, name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
2015-09-08 18:53:19 +02:00
|
|
|
b->fd = safe_close(b->fd);
|
2013-11-05 01:10:21 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-02-24 16:22:23 +01:00
|
|
|
int button_check_switches(Button *b) {
|
2017-06-26 21:35:12 +02:00
|
|
|
unsigned long switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1] = {};
|
2012-09-19 15:42:29 +02:00
|
|
|
assert(b);
|
|
|
|
|
2014-02-21 21:10:00 +01:00
|
|
|
if (b->fd < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (ioctl(b->fd, EVIOCGSW(sizeof(switches)), switches) < 0)
|
|
|
|
return -errno;
|
2012-09-19 15:42:29 +02:00
|
|
|
|
2017-06-26 21:35:12 +02:00
|
|
|
b->lid_closed = bitset_get(switches, SW_LID);
|
|
|
|
b->docked = bitset_get(switches, SW_DOCK);
|
2014-02-21 21:10:00 +01:00
|
|
|
|
2014-02-24 16:22:23 +01:00
|
|
|
if (b->lid_closed)
|
2014-02-21 21:10:00 +01:00
|
|
|
button_install_check_event_source(b);
|
|
|
|
|
|
|
|
return 0;
|
2012-09-19 15:42:29 +02:00
|
|
|
}
|