This is quite useful on laptops such as the Lenovo Yoga, where the power
button is placed on the front side of the laptop and can be pressed by
accident even if the lid is closed.
This reworks a bit of the logind logic to repeatedly try to suspend the
system as long as a lid is closed. We use the new "post" event source
for this, so that we don't keep things busy.
This also adds some code to check the lid status on boot, so that a
powered-off machine that is accidentaly powered on goes into suspend
immediately.
Yay! From now on I can put my Yoga safely in my backpack without fearing
that it might turn itself on and drain the battery.
/* Locking is handled differently from the rest. */
if (handle == HANDLE_LOCK) {
/* Locking is handled differently from the rest. */
if (handle == HANDLE_LOCK) {
+
+ if (!is_edge)
+ return 0;
+
log_info("Locking sessions...");
session_send_lock_all(m, true);
return 1;
log_info("Locking sessions...");
session_send_lock_all(m, true);
return 1;
hashmap_remove(b->manager->buttons, b->name);
hashmap_remove(b->manager->buttons, b->name);
- sd_event_source_unref(b->event_source);
+ sd_event_source_unref(b->io_event_source);
+ sd_event_source_unref(b->check_event_source);
if (b->fd >= 0) {
/* If the device has been unplugged close() returns
if (b->fd >= 0) {
/* If the device has been unplugged close() returns
-static int button_handle(
- Button *b,
- InhibitWhat inhibit_key,
- HandleAction handle,
- bool ignore_inhibited,
- bool is_edge) {
+static int button_recheck(sd_event_source *e, void *userdata) {
+ Button *b = userdata;
+ assert(b);
+ assert(b->lid_closed);
+
+ manager_handle_action(b->manager, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, false);
+ return 1;
+}
+static int button_install_check_event_source(Button *b) {
+ int r;
- r = manager_handle_action(b->manager, inhibit_key, handle, ignore_inhibited, is_edge);
- if (r > 0)
- /* We are executing the operation, so make sure we don't
- * execute another one until the lid is opened/closed again */
- b->lid_close_queued = false;
+ /* Install a post handler, so that we keep rechecking as long as the lid is closed. */
+ 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);
}
static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
}
static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
MESSAGE_ID(SD_MESSAGE_POWER_KEY),
NULL);
MESSAGE_ID(SD_MESSAGE_POWER_KEY),
NULL);
- button_handle(b, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true);
+ manager_handle_action(b->manager, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true);
break;
/* The kernel is a bit confused here:
break;
/* The kernel is a bit confused here:
MESSAGE_ID(SD_MESSAGE_SUSPEND_KEY),
NULL);
MESSAGE_ID(SD_MESSAGE_SUSPEND_KEY),
NULL);
- button_handle(b, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true);
+ manager_handle_action(b->manager, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true);
MESSAGE_ID(SD_MESSAGE_HIBERNATE_KEY),
NULL);
MESSAGE_ID(SD_MESSAGE_HIBERNATE_KEY),
NULL);
- button_handle(b, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true);
+ manager_handle_action(b->manager, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true);
MESSAGE_ID(SD_MESSAGE_LID_CLOSED),
NULL);
MESSAGE_ID(SD_MESSAGE_LID_CLOSED),
NULL);
- b->lid_close_queued = true;
- button_handle(b, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, true);
+ b->lid_closed = true;
+ manager_handle_action(b->manager, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, true);
+ button_install_check_event_source(b);
}
} else if (ev.type == EV_SW && ev.value == 0) {
}
} else if (ev.type == EV_SW && ev.value == 0) {
MESSAGE_ID(SD_MESSAGE_LID_OPENED),
NULL);
MESSAGE_ID(SD_MESSAGE_LID_OPENED),
NULL);
- b->lid_close_queued = false;
+ b->lid_closed = false;
+ b->check_event_source = sd_event_source_unref(b->check_event_source);
- r = sd_event_add_io(b->manager->event, &b->event_source, b->fd, EPOLLIN, button_dispatch, b);
+ r = sd_event_add_io(b->manager->event, &b->io_event_source, b->fd, EPOLLIN, button_dispatch, b);
if (r < 0) {
log_error("Failed to add button event: %s", strerror(-r));
goto fail;
if (r < 0) {
log_error("Failed to add button event: %s", strerror(-r));
goto fail;
-int button_recheck(Button *b) {
+int button_check_lid(Button *b) {
+ uint8_t switches[SW_MAX/8+1] = {};
- if (!b->lid_close_queued)
- return 0;
+ if (b->fd < 0)
+ return -EINVAL;
+
+ if (ioctl(b->fd, EVIOCGSW(sizeof(switches)), switches) < 0)
+ return -errno;
- return button_handle(b, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, false);
+ b->lid_closed = (switches[SW_LID/8] >> (SW_LID % 8)) & 1;
+
+ if (b->lid_closed) {
+ manager_handle_action(b->manager, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, true);
+ button_install_check_event_source(b);
+ }
+
+ return 0;
struct Button {
Manager *manager;
struct Button {
Manager *manager;
- sd_event_source *event_source;
+ sd_event_source *io_event_source;
+ sd_event_source *check_event_source;
char *name;
char *seat;
int fd;
char *name;
char *seat;
int fd;
};
Button* button_new(Manager *m, const char *name);
void button_free(Button*b);
int button_open(Button *b);
};
Button* button_new(Manager *m, const char *name);
void button_free(Button*b);
int button_open(Button *b);
-int button_recheck(Button *b);
int button_set_seat(Button *b, const char *sn);
int button_set_seat(Button *b, const char *sn);
+int button_check_lid(Button *b);
m->busnames = set_new(string_hash_func, string_compare_func);
if (!m->devices || !m->seats || !m->sessions || !m->users || !m->inhibitors || !m->buttons || !m->busnames ||
m->busnames = set_new(string_hash_func, string_compare_func);
if (!m->devices || !m->seats || !m->sessions || !m->users || !m->inhibitors || !m->buttons || !m->busnames ||
- !m->user_units || !m->session_units) {
- manager_free(m);
- return NULL;
- }
+ !m->user_units || !m->session_units)
+ goto fail;
m->kill_exclude_users = strv_new("root", NULL);
m->kill_exclude_users = strv_new("root", NULL);
- if (!m->kill_exclude_users) {
- manager_free(m);
- return NULL;
- }
+ if (!m->kill_exclude_users)
+ goto fail;
- if (!m->udev) {
- manager_free(m);
- return NULL;
- }
+ if (!m->udev)
+ goto fail;
r = sd_event_default(&m->event);
r = sd_event_default(&m->event);
- if (r < 0) {
- manager_free(m);
- return NULL;
- }
+ if (r < 0)
+ goto fail;
sd_event_set_watchdog(m->event, true);
return m;
sd_event_set_watchdog(m->event, true);
return m;
+
+fail:
+ manager_free(m);
+ return NULL;
}
void manager_free(Manager *m) {
}
void manager_free(Manager *m) {
Seat *seat;
Session *session;
User *user;
Seat *seat;
Session *session;
User *user;
Inhibitor *inhibitor;
Iterator i;
Inhibitor *inhibitor;
Iterator i;
HASHMAP_FOREACH(inhibitor, m->inhibitors, i)
inhibitor_start(inhibitor);
HASHMAP_FOREACH(inhibitor, m->inhibitors, i)
inhibitor_start(inhibitor);
+ HASHMAP_FOREACH(button, m->buttons, i)
+ button_check_lid(button);
+
manager_dispatch_idle_action(NULL, 0, m);
return 0;
}
manager_dispatch_idle_action(NULL, 0, m);
return 0;
}
-static int manager_recheck_buttons(Manager *m) {
- Iterator i;
- Button *b;
- int r = 0;
-
- assert(m);
-
- HASHMAP_FOREACH(b, m->buttons, i) {
- int q;
-
- q = button_recheck(b);
- if (q > 0)
- return 1;
- if (q < 0)
- r = q;
- }
-
- return r;
-}
-
int manager_run(Manager *m) {
int r;
int manager_run(Manager *m) {
int r;
if (manager_dispatch_delayed(m) > 0)
continue;
if (manager_dispatch_delayed(m) > 0)
continue;
- if (manager_recheck_buttons(m) > 0)
- continue;
-
if (m->action_what != 0 && !m->action_job) {
usec_t x, y;
if (m->action_what != 0 && !m->action_job) {
usec_t x, y;