]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
logind: remember our idle state and use it to detect idle level transitions
authorMichal Sekletar <msekleta@redhat.com>
Wed, 20 Apr 2022 08:13:43 +0000 (10:13 +0200)
committerMichal Sekletar <msekleta@redhat.com>
Thu, 30 Jun 2022 16:16:58 +0000 (18:16 +0200)
Fixes #16391

src/login/logind.c
src/login/logind.h

index aa8548054841794a4a3d8d9d2146abd8fe9c4e76..d14a17274bb0796700b41cf91905806d343324ad 100644 (file)
@@ -963,18 +963,33 @@ static int manager_dispatch_idle_action(sd_event_source *s, uint64_t t, void *us
         n = now(CLOCK_MONOTONIC);
 
         r = manager_get_idle_hint(m, &since);
-        if (r <= 0)
+        if (r <= 0) {
                 /* Not idle. Let's check if after a timeout it might be idle then. */
                 elapse = n + m->idle_action_usec;
-        else {
+                m->was_idle = false;
+        } else {
+
                 /* Idle! Let's see if it's time to do something, or if
                  * we shall sleep for longer. */
 
                 if (n >= since.monotonic + m->idle_action_usec &&
                     (m->idle_action_not_before_usec <= 0 || n >= m->idle_action_not_before_usec + m->idle_action_usec)) {
-                        log_info("System idle. Will %s now.", handle_action_verb_to_string(m->idle_action));
+                        bool is_edge = false;
+
+                        /* We weren't idle previously or some activity happened while we were sleeping, and now we are
+                         * idle. Let's remember that for the next time and make this an edge transition. */
+                        if (!m->was_idle || since.monotonic >= m->idle_action_not_before_usec) {
+                                is_edge = true;
+                                m->was_idle = true;
+                        }
+
+                        if (m->idle_action == HANDLE_LOCK && !is_edge)
+                                /* We are idle and we were before so we are actually not taking any action. */
+                                log_debug("System idle.");
+                        else
+                                log_info("System idle. Will %s now.", handle_action_verb_to_string(m->idle_action));
 
-                        manager_handle_action(m, 0, m->idle_action, false, false);
+                        manager_handle_action(m, 0, m->idle_action, false, is_edge);
                         m->idle_action_not_before_usec = n;
                 }
 
index 27f9e9729fc91503c63becb560aceaccd2fcd5e0..58677c94918215d38655af4dee8de89d00831174 100644 (file)
@@ -94,6 +94,7 @@ struct Manager {
         usec_t idle_action_usec;
         usec_t idle_action_not_before_usec;
         HandleAction idle_action;
+        bool was_idle;
 
         HandleAction handle_power_key;
         HandleAction handle_power_key_long_press;