chiark / gitweb /
loginctl: show pager also for status command
[elogind.git] / src / login / logind-button.c
index 7cb3f383bcb2c926954f81d7e582d29ac0583a30..8fdab789bcb346f2b73222d984376023bea7ba0e 100644 (file)
@@ -150,7 +150,12 @@ fail:
         return r;
 }
 
-static int button_handle(Button *b, InhibitWhat inhibit_key, HandleButton handle, bool ignore_inhibited) {
+static int button_handle(
+                Button *b,
+                InhibitWhat inhibit_key,
+                HandleButton handle,
+                bool ignore_inhibited,
+                bool is_edge) {
 
         static const char * const message_table[_HANDLE_BUTTON_MAX] = {
                 [HANDLE_POWEROFF] = "Powering Off...",
@@ -183,7 +188,7 @@ static int button_handle(Button *b, InhibitWhat inhibit_key, HandleButton handle
         }
 
         /* If the key handling is inhibited, don't do anything */
-        if (manager_is_inhibited(b->manager, inhibit_key, INHIBIT_BLOCK, NULL, true)) {
+        if (manager_is_inhibited(b->manager, inhibit_key, INHIBIT_BLOCK, NULL, true, false, 0)) {
                 log_debug("Refusing key handling, %s is inhibited.", inhibit_what_to_string(inhibit_key));
                 return 0;
         }
@@ -192,7 +197,15 @@ static int button_handle(Button *b, InhibitWhat inhibit_key, HandleButton handle
 
         /* If the actual operation is inhibited, warn and fail */
         if (!ignore_inhibited &&
-            manager_is_inhibited(b->manager, inhibit_operation, INHIBIT_BLOCK, NULL, false)) {
+            manager_is_inhibited(b->manager, inhibit_operation, INHIBIT_BLOCK, NULL, false, false, 0)) {
+
+
+                /* If this is just a recheck of the lid switch then don't warn about anything */
+                if (!is_edge) {
+                        log_debug("Refusing operation, %s is inhibited.", inhibit_what_to_string(inhibit_operation));
+                        return 0;
+                }
+
                 log_error("Refusing operation, %s is inhibited.", inhibit_what_to_string(inhibit_operation));
                 warn_melody();
                 return -EPERM;
@@ -200,14 +213,19 @@ static int button_handle(Button *b, InhibitWhat inhibit_key, HandleButton handle
 
         log_info("%s", message_table[handle]);
 
+        /* 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;
+
         dbus_error_init(&error);
         r = bus_manager_shutdown_or_sleep_now_or_later(b->manager, target_table[handle], inhibit_operation, &error);
         if (r < 0) {
                 log_error("Failed to execute operation: %s", bus_error_message(&error));
                 dbus_error_free(&error);
+                return r;
         }
 
-        return r;
+        return 1;
 }
 
 int button_process(Button *b) {
@@ -229,27 +247,57 @@ int button_process(Button *b) {
                 case KEY_POWER:
                 case KEY_POWER2:
                         log_info("Power key pressed.");
-                        return button_handle(b, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited);
+                        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_handle(b, INHIBIT_HANDLE_SLEEP_KEY, b->manager->handle_sleep_key, b->manager->sleep_key_ignore_inhibited);
+                        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_handle(b, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited);
+                        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;
 }
 
+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);
+}
+
 static const char* const handle_button_table[_HANDLE_BUTTON_MAX] = {
         [HANDLE_IGNORE] = "ignore",
         [HANDLE_POWEROFF] = "poweroff",