chiark / gitweb /
logind: add support for automatic suspend/hibernate/shutdown on idle
[elogind.git] / src / login / logind-button.c
index d023294a5919b9bc1477674b8d58b8a15ac4c70b..dbf3d3c446dd260f3ab81d8ed22a2287affe0d7d 100644 (file)
@@ -150,129 +150,22 @@ fail:
         return r;
 }
 
-static Session *button_get_session(Button *b) {
-        Seat *seat;
-        assert(b);
-
-        if (!b->seat)
-                return NULL;
-
-        seat = hashmap_get(b->manager->seats, b->seat);
-        if (!seat)
-                return NULL;
+static int button_handle(
+                Button *b,
+                InhibitWhat inhibit_key,
+                HandleAction handle,
+                bool ignore_inhibited,
+                bool is_edge) {
 
-        return seat->active;
-}
-
-static int button_power_off(Button *b, HandleButton handle) {
-        DBusError error;
         int r;
 
         assert(b);
 
-        if (handle == HANDLE_OFF)
-                return 0;
-
-        if (handle == HANDLE_NO_SESSION) {
-                if (hashmap_size(b->manager->sessions) > 0) {
-                        log_error("Refusing power-off, user is logged in.");
-                        warn_melody();
-                        return -EPERM;
-                }
-
-        } else if (handle == HANDLE_TTY_SESSION ||
-                   handle == HANDLE_ANY_SESSION) {
-                unsigned n;
-                Session *s;
-
-                n = hashmap_size(b->manager->sessions);
-                s = button_get_session(b);
-
-                /* Silently ignore events of graphical sessions */
-                if (handle == HANDLE_TTY_SESSION &&
-                    s && s->type == SESSION_X11)
-                        return 0;
-
-                if (n > 1 || (n == 1 && !s)) {
-                        log_error("Refusing power-off, other user is logged in.");
-                        warn_melody();
-                        return -EPERM;
-                }
-
-        }
-
-        if (handle != HANDLE_ALWAYS) {
-                if (manager_is_inhibited(b->manager, INHIBIT_SHUTDOWN, INHIBIT_BLOCK, NULL)) {
-                        log_error("Refusing power-off, shutdown is inhibited.");
-                        warn_melody();
-                        return -EPERM;
-                }
-        }
-
-        log_info("Powering off...");
-
-        dbus_error_init(&error);
-        r = bus_manager_shutdown_or_sleep_now_or_later(b->manager, SPECIAL_POWEROFF_TARGET, INHIBIT_SHUTDOWN, &error);
-        if (r < 0) {
-                log_error("Failed to power off: %s", bus_error_message(&error));
-                dbus_error_free(&error);
-        }
-
-        return r;
-}
-
-static int button_suspend(Button *b, HandleButton handle) {
-        DBusError error;
-        int r;
-
-        assert(b);
-
-        if (handle == HANDLE_OFF)
-                return 0;
-
-        if (handle == HANDLE_NO_SESSION) {
-                if (hashmap_size(b->manager->sessions) > 0) {
-                        log_error("Refusing suspend, user is logged in.");
-                        warn_melody();
-                        return -EPERM;
-                }
-
-        } else if (handle == HANDLE_TTY_SESSION ||
-                   handle == HANDLE_ANY_SESSION) {
-                unsigned n;
-                Session *s;
-
-                n = hashmap_size(b->manager->sessions);
-                s = button_get_session(b);
-
-                /* Silently ignore events of graphical sessions */
-                if (handle == HANDLE_TTY_SESSION &&
-                    s && s->type == SESSION_X11)
-                        return 0;
-
-                if (n > 1 || (n == 1 && !s)) {
-                        log_error("Refusing suspend, other user is logged in.");
-                        warn_melody();
-                        return -EPERM;
-                }
-        }
-
-        if (handle != HANDLE_ALWAYS) {
-                if (manager_is_inhibited(b->manager, INHIBIT_SLEEP, INHIBIT_BLOCK, NULL)) {
-                        log_error("Refusing suspend, sleeping is inhibited.");
-                        warn_melody();
-                        return -EPERM;
-                }
-        }
-
-        log_info("Suspending...");
-
-        dbus_error_init(&error);
-        r = bus_manager_shutdown_or_sleep_now_or_later(b->manager, SPECIAL_SUSPEND_TARGET, INHIBIT_SLEEP, &error);
-        if (r < 0) {
-                log_error("Failed to suspend: %s", bus_error_message(&error));
-                dbus_error_free(&error);
-        }
+        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;
 
         return r;
 }
@@ -296,33 +189,53 @@ int button_process(Button *b) {
                 case KEY_POWER:
                 case KEY_POWER2:
                         log_info("Power key pressed.");
-                        return button_power_off(b, b->manager->handle_power_key);
+                        return button_handle(b, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true);
+
+                /* 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"
+                */
 
                 case KEY_SLEEP:
-                case KEY_SUSPEND:
-                        log_info("Sleep key pressed.");
-                        return button_suspend(b, b->manager->handle_sleep_key);
+                        log_info("Suspend key pressed.");
+                        return button_handle(b, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true);
 
+                case KEY_SUSPEND:
+                        log_info("Hibernate key pressed.");
+                        return button_handle(b, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true);
                 }
+
         } else if (ev.type == EV_SW && ev.value > 0) {
 
                 switch (ev.code) {
 
                 case SW_LID:
                         log_info("Lid closed.");
-                        return button_suspend(b, b->manager->handle_lid_switch);
+                        b->lid_close_queued = true;
+
+                        return button_handle(b, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, true);
+                }
+
+        } else if (ev.type == EV_SW && ev.value == 0) {
+
+                switch (ev.code) {
+
+                case SW_LID:
+                        log_info("Lid opened.");
+                        b->lid_close_queued = false;
+                        break;
                 }
         }
 
         return 0;
 }
 
-static const char* const handle_button_table[_HANDLE_BUTTON_MAX] = {
-        [HANDLE_OFF] = "off",
-        [HANDLE_NO_SESSION] = "no-session",
-        [HANDLE_TTY_SESSION] = "tty-session",
-        [HANDLE_ANY_SESSION] = "any-session",
-        [HANDLE_ALWAYS] = "always"
-};
-DEFINE_STRING_TABLE_LOOKUP(handle_button, HandleButton);
-DEFINE_CONFIG_PARSE_ENUM(config_parse_handle_button, handle_button, HandleButton, "Failed to parse handle button setting");
+int button_recheck(Button *b) {
+        assert(b);
+
+        if (!b->lid_close_queued)
+                return 0;
+
+        return button_handle(b, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, false);
+}