2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2012-05-30 15:07:19 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <unistd.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"
|
2018-12-04 08:26:09 +01:00
|
|
|
#include "missing_input.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2012-05-30 15:07:19 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
2020-08-14 13:10:18 +02:00
|
|
|
#define CONST_MAX5(a, b, c, d, e) CONST_MAX(CONST_MAX(a, b), CONST_MAX(CONST_MAX(c, d), e))
|
2017-06-26 21:35:12 +02:00
|
|
|
|
|
|
|
#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);
|
|
|
|
|
2018-02-09 17:37:39 +01:00
|
|
|
/* If we are docked or on external power, 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;
|
2018-02-09 17:37:39 +01:00
|
|
|
else if (manager->handle_lid_switch_ep != _HANDLE_ACTION_INVALID &&
|
|
|
|
manager_is_on_external_power())
|
|
|
|
handle_action = manager->handle_lid_switch_ep;
|
2014-08-26 22:08:02 +02:00
|
|
|
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."),
|
2018-06-04 12:59:22 +02:00
|
|
|
"MESSAGE_ID=" SD_MESSAGE_POWER_KEY_STR);
|
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
|
|
|
|
2020-08-14 13:10:18 +02:00
|
|
|
/* The kernel naming is a bit confusing here:
|
|
|
|
KEY_RESTART was probably introduced for media playback purposes, but
|
|
|
|
is now being predominantly used to indicate device reboot.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case KEY_RESTART:
|
|
|
|
log_struct(LOG_INFO,
|
|
|
|
LOG_MESSAGE("Reboot key pressed."),
|
|
|
|
"MESSAGE_ID=" SD_MESSAGE_REBOOT_KEY_STR);
|
|
|
|
|
|
|
|
manager_handle_action(b->manager, INHIBIT_HANDLE_REBOOT_KEY, b->manager->handle_reboot_key, b->manager->reboot_key_ignore_inhibited, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* The kernel naming is a bit confusing here:
|
2012-09-21 15:44:14 +02:00
|
|
|
|
|
|
|
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."),
|
2018-06-04 12:59:22 +02:00
|
|
|
"MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_STR);
|
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."),
|
2018-06-04 12:59:22 +02:00
|
|
|
"MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_STR);
|
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."),
|
2018-06-04 12:59:22 +02:00
|
|
|
"MESSAGE_ID=" SD_MESSAGE_LID_CLOSED_STR);
|
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."),
|
2018-06-04 12:59:22 +02:00
|
|
|
"MESSAGE_ID=" SD_MESSAGE_SYSTEM_DOCKED_STR);
|
2014-02-24 16:22:23 +01:00
|
|
|
|
|
|
|
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."),
|
2018-06-04 12:59:22 +02:00
|
|
|
"MESSAGE_ID=" SD_MESSAGE_LID_OPENED_STR);
|
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."),
|
2018-06-04 12:59:22 +02:00
|
|
|
"MESSAGE_ID=" SD_MESSAGE_SYSTEM_UNDOCKED_STR);
|
2014-02-24 16:22:23 +01:00
|
|
|
|
|
|
|
b->docked = false;
|
2012-05-30 15:07:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-22 10:45:13 +02:00
|
|
|
static int button_suitable(int fd) {
|
2017-06-27 17:46:28 +02:00
|
|
|
unsigned long types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1];
|
|
|
|
|
2019-05-22 10:45:13 +02:00
|
|
|
assert(fd >= 0);
|
2017-06-27 17:46:28 +02:00
|
|
|
|
2019-05-22 10:45:13 +02:00
|
|
|
if (ioctl(fd, EVIOCGBIT(EV_SYN, sizeof types), types) < 0)
|
2017-06-27 17:46:28 +02:00
|
|
|
return -errno;
|
|
|
|
|
|
|
|
if (bitset_get(types, EV_KEY)) {
|
2020-08-14 13:10:18 +02:00
|
|
|
unsigned long keys[CONST_MAX5(KEY_POWER, KEY_POWER2, KEY_SLEEP, KEY_SUSPEND, KEY_RESTART)/ULONG_BITS+1];
|
2017-06-27 17:46:28 +02:00
|
|
|
|
2019-05-22 10:45:13 +02:00
|
|
|
if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof keys), keys) < 0)
|
2017-06-27 17:46:28 +02:00
|
|
|
return -errno;
|
|
|
|
|
|
|
|
if (bitset_get(keys, KEY_POWER) ||
|
|
|
|
bitset_get(keys, KEY_POWER2) ||
|
|
|
|
bitset_get(keys, KEY_SLEEP) ||
|
2020-08-14 13:10:18 +02:00
|
|
|
bitset_get(keys, KEY_SUSPEND) ||
|
|
|
|
bitset_get(keys, KEY_RESTART))
|
2017-06-27 17:46:28 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bitset_get(types, EV_SW)) {
|
|
|
|
unsigned long switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1];
|
|
|
|
|
2019-05-22 10:45:13 +02:00
|
|
|
if (ioctl(fd, EVIOCGBIT(EV_SW, sizeof switches), switches) < 0)
|
2017-06-27 17:46:28 +02:00
|
|
|
return -errno;
|
|
|
|
|
|
|
|
if (bitset_get(switches, SW_LID) ||
|
|
|
|
bitset_get(switches, SW_DOCK))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-22 10:45:13 +02:00
|
|
|
static int button_set_mask(const char *name, int fd) {
|
2017-06-26 21:35:12 +02:00
|
|
|
unsigned long
|
|
|
|
types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1] = {},
|
2020-08-14 13:10:18 +02:00
|
|
|
keys[CONST_MAX5(KEY_POWER, KEY_POWER2, KEY_SLEEP, KEY_SUSPEND, KEY_RESTART)/ULONG_BITS+1] = {},
|
2017-06-26 21:35:12 +02:00
|
|
|
switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1] = {};
|
|
|
|
struct input_mask mask;
|
|
|
|
|
2019-05-22 10:45:13 +02:00
|
|
|
assert(name);
|
|
|
|
assert(fd >= 0);
|
2017-06-26 21:35:12 +02:00
|
|
|
|
|
|
|
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),
|
|
|
|
};
|
|
|
|
|
2019-05-22 10:45:13 +02:00
|
|
|
if (ioctl(fd, EVIOCSMASK, &mask) < 0)
|
2017-06-26 21:35:12 +02:00
|
|
|
/* 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,
|
2019-05-22 10:45:13 +02:00
|
|
|
errno, "Failed to set EV_SYN event mask on /dev/input/%s: %m", name);
|
2017-06-26 21:35:12 +02:00
|
|
|
|
|
|
|
bitset_put(keys, KEY_POWER);
|
|
|
|
bitset_put(keys, KEY_POWER2);
|
|
|
|
bitset_put(keys, KEY_SLEEP);
|
|
|
|
bitset_put(keys, KEY_SUSPEND);
|
2020-08-14 13:10:18 +02:00
|
|
|
bitset_put(keys, KEY_RESTART);
|
2017-06-26 21:35:12 +02:00
|
|
|
|
|
|
|
mask = (struct input_mask) {
|
|
|
|
.type = EV_KEY,
|
|
|
|
.codes_size = sizeof(keys),
|
|
|
|
.codes_ptr = PTR_TO_UINT64(keys),
|
|
|
|
};
|
|
|
|
|
2019-05-22 10:45:13 +02:00
|
|
|
if (ioctl(fd, EVIOCSMASK, &mask) < 0)
|
|
|
|
return log_warning_errno(errno, "Failed to set EV_KEY event mask on /dev/input/%s: %m", name);
|
2017-06-26 21:35:12 +02:00
|
|
|
|
|
|
|
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),
|
|
|
|
};
|
|
|
|
|
2019-05-22 10:45:13 +02:00
|
|
|
if (ioctl(fd, EVIOCSMASK, &mask) < 0)
|
|
|
|
return log_warning_errno(errno, "Failed to set EV_SW event mask on /dev/input/%s: %m", name);
|
2017-06-26 21:35:12 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-05 01:10:21 +01:00
|
|
|
int button_open(Button *b) {
|
2019-05-22 10:45:13 +02:00
|
|
|
_cleanup_close_ int fd = -1;
|
|
|
|
const char *p;
|
|
|
|
char name[256];
|
2013-11-05 01:10:21 +01:00
|
|
|
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
|
|
|
|
2019-05-22 10:45:13 +02:00
|
|
|
fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
|
|
|
|
if (fd < 0)
|
2017-06-27 17:46:28 +02:00
|
|
|
return log_warning_errno(errno, "Failed to open %s: %m", p);
|
|
|
|
|
2019-05-22 10:45:13 +02:00
|
|
|
r = button_suitable(fd);
|
2017-06-27 17:46:28 +02:00
|
|
|
if (r < 0)
|
2019-05-22 10:45:13 +02:00
|
|
|
return log_warning_errno(r, "Failed to determine whether input device %s is relevant to us: %m", p);
|
2018-11-20 23:40:44 +01:00
|
|
|
if (r == 0)
|
|
|
|
return log_debug_errno(SYNTHETIC_ERRNO(EADDRNOTAVAIL),
|
2019-05-22 10:45:13 +02:00
|
|
|
"Device %s does not expose keys or switches relevant to us, ignoring.", p);
|
2013-11-05 01:10:21 +01:00
|
|
|
|
2019-05-22 10:45:13 +02:00
|
|
|
if (ioctl(fd, EVIOCGNAME(sizeof name), name) < 0)
|
|
|
|
return log_error_errno(errno, "Failed to get input name for %s: %m", p);
|
2013-11-05 01:10:21 +01:00
|
|
|
|
2019-05-22 10:45:13 +02:00
|
|
|
(void) button_set_mask(b->name, fd);
|
2019-08-03 22:42:53 +02:00
|
|
|
|
2019-05-22 08:24:28 +02:00
|
|
|
b->io_event_source = sd_event_source_unref(b->io_event_source);
|
2019-05-23 01:10:23 +02:00
|
|
|
r = sd_event_add_io(b->manager->event, &b->io_event_source, fd, EPOLLIN, button_dispatch, b);
|
2019-05-22 10:45:13 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to add button event for %s: %m", p);
|
2013-11-05 01:10:21 +01:00
|
|
|
|
2019-05-22 10:45:13 +02:00
|
|
|
b->fd = TAKE_FD(fd);
|
|
|
|
log_info("Watching system buttons on %s (%s)", p, name);
|
2013-11-05 01:10:21 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|