chiark / gitweb /
Prep v239: Add support for the new 'suspend-then-hibernate' method.
[elogind.git] / src / sleep / sleep.c
index c3248becd470fceb307b6aa02683985457721f5d..fba7b9baf59d415a72b65ca46922609faf86012b 100644 (file)
@@ -1,26 +1,25 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 /***
-  Copyright © 2013 Zbigniew Jędrzejewski-Szmek
   Copyright © 2010-2017 Canonical
   Copyright © 2018 Dell Inc.
 ***/
 
 //#include <errno.h>
 //#include <getopt.h>
-//#include <linux/fiemap.h>
+#include <linux/fiemap.h>
 //#include <stdio.h>
 
 #include "sd-messages.h"
 
-//#include "parse-util.h"
+#include "parse-util.h"
 #include "def.h"
 #include "exec-util.h"
 #include "fd-util.h"
 #include "fileio.h"
 //#include "log.h"
-//#include "sleep-config.h"
-//#include "stdio-util.h"
-//#include "string-util.h"
+#include "sleep-config.h"
+#include "stdio-util.h"
+#include "string-util.h"
 #include "strv.h"
 //#include "util.h"
 
@@ -134,7 +133,22 @@ static int write_state(FILE **f, char **states) {
         return r;
 }
 
+#if 0 /// elogind uses the values stored in its manager instance
 static int execute(char **modes, char **states) {
+#else
+static int execute(Manager *m, const char *verb) {
+        assert(m);
+
+        if (verb)
+                arg_verb = (char*)verb;
+
+        char **modes  = streq(arg_verb, "suspend")   ? m->suspend_mode     :
+                        streq(arg_verb, "hibernate") ? m->hibernate_mode   :
+                                                       m->hybrid_sleep_mode;
+        char **states = streq(arg_verb, "suspend")   ? m->suspend_state     :
+                        streq(arg_verb, "hibernate") ? m->hibernate_state   :
+                                                       m->hybrid_sleep_state;
+#endif // 0
 
         char *arguments[] = {
                 NULL,
@@ -212,14 +226,22 @@ static int write_wakealarm(const char *str) {
         return 0;
 }
 
+#if 0 /// elogind uses the values stored in its manager instance
 static int execute_s2h(usec_t hibernate_delay_sec) {
 
         _cleanup_strv_free_ char **hibernate_modes = NULL, **hibernate_states = NULL,
                                  **suspend_modes = NULL, **suspend_states = NULL;
+#else
+static int execute_s2h(Manager *m) {
+        assert(m);
+
+        usec_t hibernate_delay_sec = m->hibernate_delay_sec;
+#endif // 0
         usec_t orig_time, cmp_time;
         char time_str[DECIMAL_STR_MAX(uint64_t)];
         int r;
 
+#if 0 /// Already parsed by elogind config
         r = parse_sleep_config("suspend", &suspend_modes, &suspend_states,
                                NULL);
         if (r < 0)
@@ -229,6 +251,7 @@ static int execute_s2h(usec_t hibernate_delay_sec) {
                                &hibernate_states, NULL);
         if (r < 0)
                 return r;
+#endif // 0
 
         r = read_wakealarm(&orig_time);
         if (r < 0)
@@ -243,7 +266,11 @@ static int execute_s2h(usec_t hibernate_delay_sec) {
 
         log_debug("Set RTC wake alarm for %s", time_str);
 
+#if 0 /// elogind uses its manager instance values
         r = execute(suspend_modes, suspend_states);
+#else
+        r = execute(m, "suspend");
+#endif // 0
         if (r < 0)
                 return r;
 
@@ -260,7 +287,11 @@ static int execute_s2h(usec_t hibernate_delay_sec) {
 
         /* if woken up after alarm time, hibernate */
         if (cmp_time >= orig_time)
+#if 0 /// elogind uses its manager instance values
                 r = execute(hibernate_modes, hibernate_states);
+#else
+                r = execute(m, "hibernate");
+#endif // 0
 
         return r;
 }
@@ -352,13 +383,18 @@ int main(int argc, char *argv[]) {
                 r = execute_s2h(delay);
         else
                 r = execute(modes, states);
-finish:
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }
 #else
-int do_sleep(const char *verb, char **modes, char **states) {
+int do_sleep(Manager *m, const char *verb) {
         assert(verb);
+        assert(m);
+
         arg_verb = (char*)verb;
-        return execute(modes, states);
+
+        if (streq(arg_verb, "suspend-then-hibernate"))
+                return execute_s2h(m);
+
+        return execute(m, NULL);
 }
 #endif // 0