summaryrefslogtreecommitdiff
blob: a246dcb4f9e452e78127b9c98fbfea49ceef4978 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
Patch from http://bugzilla.xfce.org/show_bug.cgi?id=9952 with modification
to edit configure.ac (release tarball) instead of configure.ac.in (git)

From ee2228c756e40a4289476f40a6d0045d17418bce Mon Sep 17 00:00:00 2001
From: Eric Koegel <eric.koegel@gmail.com>
Date: Thu, 29 May 2014 19:17:26 +0300
Subject: [PATCH] Add suspend/hibernate to shutdown-helper

Upower 0.99 dropped support for suspend/hibernate as an obsolete
feature in this commit:
http://cgit.freedesktop.org/upower/commit/?id=1ee642e705a63f5ad56a6b55e4bf5c4a64c959b8
Add that functionality into the shutdown-helper so these features
will work again.
---
 configure.ac.in               |  27 +++++++++
 xfce4-session/Makefile.am     |   4 +-
 xfce4-session/xfsm-shutdown.c | 126 ++++++++++++++++++++++++++++++++++++++++--
 xfce4-session/xfsm-upower.c   |   5 +-
 xfce4-session/xfsm-upower.h   |   4 ++
 xfsm-shutdown-helper/main.c   |  26 +++++++++
 6 files changed, 186 insertions(+), 6 deletions(-)

diff --git a/configure.ac b/configure.ac
index 910fd35..d4557a4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -95,6 +95,7 @@ XDT_CHECK_PACKAGE([LIBWNCK], [libwnck-1.0], [2.30])
 XDT_CHECK_PACKAGE([DBUS], [dbus-1], [1.1.0])
 XDT_CHECK_PACKAGE([DBUS_GLIB], [dbus-glib-1], [0.84])
 XDT_CHECK_PACKAGE([XFCONF], [libxfconf-0], [4.9.0])
+XDT_CHECK_PACKAGE([UPOWER],[upower-glib], [0.9.8])
 
 dnl Check for polkit / systemd integration
 XDT_CHECK_OPTIONAL_PACKAGE([SYSTEMD], [polkit-gobject-1], [0.100],
@@ -157,6 +158,31 @@ if test "x$linux_ioprio_works" = "xyes"; then
             [Defined if linux/ioprio.h not only exists, but works properly])
 fi
 
+dnl Compile time default choice of backend
+AC_ARG_WITH([backend],
+	    AS_HELP_STRING([--with-backend=<option>],
+			   [Default backend to use linux, freebsd, openbsd]))
+# default to a sane option
+AC_CANONICAL_HOST
+if test x$with_backend = x; then
+	AS_CASE([$host],
+		[*-linux*],   [with_backend=linux],
+		[*-*freebsd*], [with_backend=freebsd],
+		[*-openbsd*], [with_backend=openbsd])
+fi
+AC_DEFINE_UNQUOTED(BACKEND, "$with_backend", [backend])
+AC_SUBST(BACKEND, "$with_backend")
+
+if test x$with_backend = xlinux; then
+    AC_DEFINE(BACKEND_TYPE_LINUX, 1, [Linux suspend/hibernate backend])
+fi
+if test x$with_backend = xfreebsd; then
+    AC_DEFINE(BACKEND_TYPE_FREEBSD, 1, [FreeBSD suspend/hibernate backend])
+fi
+if test x$with_backend = xopenbsd; then
+    AC_DEFINE(BACKEND_TYPE_OPENBSD, 1, [OpenBSD suspend/hibernate backend])
+fi
+
 dnl check for location Xfce glade files were installed to
 XFCE_GLADE_CATALOG_PATH="`pkg-config --variable glade_catalogdir libxfce4ui-1`"
 XFCE_GLADE_PIXMAP_PATH="`pkg-config --variable glade_pixmapdir libxfce4ui-1`"
@@ -207,5 +233,6 @@ echo " * Gnome Keyring support:          yes"
 else
 echo " * Gnome Keyring support:          no"
 fi
+echo " * Backend:                        ${with_backend}"
 
 echo
diff --git a/xfce4-session/Makefile.am b/xfce4-session/Makefile.am
index 5472b33..6b5968e 100644
--- a/xfce4-session/Makefile.am
+++ b/xfce4-session/Makefile.am
@@ -83,7 +83,8 @@ xfce4_session_CFLAGS =							\
 	$(SYSTEMD_CFLAGS)						\
 	$(XFCONF_CFLAGS)						\
 	$(GMODULE_CFLAGS)						\
-	$(PLATFORM_CFLAGS)
+	$(PLATFORM_CFLAGS)						\
+	$(UPOWER_CFLAGS)
 
 xfce4_session_LDFLAGS =							\
 	-no-undefined							\
@@ -102,6 +103,7 @@ xfce4_session_LDADD =							\
 	$(LIBWNCK_LIBS)							\
 	$(SYSTEMD_LIBS)							\
 	$(XFCONF_LIBS)							\
+	$(UPOWER_LIBS)							\
 	-lm
 
 xfce4_session_DEPENDENCIES =						\
diff --git a/xfce4-session/xfsm-shutdown.c b/xfce4-session/xfsm-shutdown.c
index 4c483a7..cba08b8 100644
--- a/xfce4-session/xfsm-shutdown.c
+++ b/xfce4-session/xfsm-shutdown.c
@@ -57,6 +57,7 @@
 #include <dbus/dbus-glib-lowlevel.h>
 #include <libxfce4util/libxfce4util.h>
 #include <gtk/gtk.h>
+#include <upower.h>
 
 #include <libxfsm/xfsm-util.h>
 
@@ -76,6 +77,8 @@
 
 static void xfsm_shutdown_finalize  (GObject      *object);
 static void xfsm_shutdown_sudo_free (XfsmShutdown *shutdown);
+static gboolean xfsm_shutdown_fallback_can_hibernate (void);
+static gboolean xfsm_shutdown_fallback_can_suspend   (void);
 
 
 
@@ -403,14 +406,16 @@ xfsm_shutdown_sudo_try_action (XfsmShutdown      *shutdown,
   g_return_val_if_fail (shutdown->helper_state == SUDO_AVAILABLE, FALSE);
   g_return_val_if_fail (shutdown->helper_outfile != NULL, FALSE);
   g_return_val_if_fail (shutdown->helper_infile != NULL, FALSE);
-  g_return_val_if_fail (type == XFSM_SHUTDOWN_SHUTDOWN
-                        || type == XFSM_SHUTDOWN_RESTART, FALSE);
 
   /* the command we send to sudo */
   if (type == XFSM_SHUTDOWN_SHUTDOWN)
     action = "POWEROFF";
   else if (type == XFSM_SHUTDOWN_RESTART)
     action = "REBOOT";
+  else if (type == XFSM_SHUTDOWN_SUSPEND)
+    action = "SUSPEND";
+  else if (type == XFSM_SHUTDOWN_HIBERNATE)
+    action = "HIBERNATE";
   else
     return FALSE;
 
@@ -692,7 +697,17 @@ xfsm_shutdown_try_suspend (XfsmShutdown  *shutdown,
 {
   g_return_val_if_fail (XFSM_IS_SHUTDOWN (shutdown), FALSE);
 
-  return xfsm_upower_try_suspend (shutdown->upower, error);
+#if UP_CHECK_VERSION(0, 99, 0)
+  if (shutdown->helper_state == SUDO_AVAILABLE)
+    {
+      xfsm_upower_lock_screen (shutdown->upower, "Suspend", error);
+      return xfsm_shutdown_sudo_try_action (shutdown, XFSM_SHUTDOWN_SUSPEND, error);
+    }
+  else
+    return FALSE;
+#else
+    return xfsm_upower_try_suspend (shutdown->upower, error);
+#endif
 }
 
 
@@ -703,7 +718,17 @@ xfsm_shutdown_try_hibernate (XfsmShutdown  *shutdown,
 {
   g_return_val_if_fail (XFSM_IS_SHUTDOWN (shutdown), FALSE);
 
-  return xfsm_upower_try_hibernate (shutdown->upower, error);
+#if UP_CHECK_VERSION(0, 99, 0)
+  if (shutdown->helper_state == SUDO_AVAILABLE)
+    {
+      xfsm_upower_lock_screen (shutdown->upower, "Hibernate", error);
+      return xfsm_shutdown_sudo_try_action (shutdown, XFSM_SHUTDOWN_HIBERNATE, error);
+    }
+  else
+    return FALSE;
+#else
+    return xfsm_upower_try_hibernate (shutdown->upower, error);
+#endif
 }
 
 
@@ -784,8 +809,13 @@ xfsm_shutdown_can_suspend (XfsmShutdown  *shutdown,
       return TRUE;
     }
 
+#if UP_CHECK_VERSION(0, 99, 0)
+  *can_suspend = xfsm_shutdown_fallback_can_suspend ();
+  return TRUE;
+#else
   return xfsm_upower_can_suspend (shutdown->upower, can_suspend, 
                                   auth_suspend, error);
+#endif
 }
 
 
@@ -804,8 +834,13 @@ xfsm_shutdown_can_hibernate (XfsmShutdown  *shutdown,
       return TRUE;
     }
 
+#if UP_CHECK_VERSION(0, 99, 0)
+  *can_hibernate = xfsm_shutdown_fallback_can_hibernate ();
+  return TRUE;
+#else
   return xfsm_upower_can_hibernate (shutdown->upower, can_hibernate,
                                     auth_hibernate, error);
+#endif
 }
 
 
@@ -816,3 +851,86 @@ xfsm_shutdown_can_save_session (XfsmShutdown *shutdown)
   g_return_val_if_fail (XFSM_IS_SHUTDOWN (shutdown), FALSE);
   return shutdown->kiosk_can_save_session;
 }
+
+
+
+#ifdef BACKEND_TYPE_FREEBSD
+static gboolean
+freebsd_supports_sleep_state (const gchar *state)
+{
+  gboolean ret = FALSE;
+  gchar *sleep_states;
+
+  sleep_states = up_get_string_sysctl (NULL, "hw.acpi.supported_sleep_state");
+  if (sleep_states != NULL)
+    {
+      if (strstr (sleep_states, state) != NULL)
+          ret = TRUE;
+    }
+
+  g_free (sleep_states);
+
+  return ret;
+}
+#endif
+
+#ifdef BACKEND_TYPE_LINUX
+static gboolean
+linux_supports_sleep_state (const gchar *state)
+{
+  gboolean ret = FALSE;
+  gchar *command;
+  GError *error = NULL;
+  gint exit_status;
+
+  /* run script from pm-utils */
+  command = g_strdup_printf ("/usr/bin/pm-is-supported --%s", state);
+
+  ret = g_spawn_command_line_sync (command, NULL, NULL, &exit_status, &error);
+  if (!ret)
+    {
+      g_warning ("failed to run script: %s", error->message);
+      g_error_free (error);
+      goto out;
+    }
+  ret = (WIFEXITED(exit_status) && (WEXITSTATUS(exit_status) == EXIT_SUCCESS));
+
+out:
+  g_free (command);
+
+  return ret;
+}
+#endif
+
+
+static gboolean
+xfsm_shutdown_fallback_can_suspend (void)
+{
+#ifdef BACKEND_TYPE_FREEBSD
+  return freebsd_supports_sleep_state ("S3");
+#endif
+#ifdef BACKEND_TYPE_LINUX
+  return linux_supports_sleep_state ("suspend");
+#endif
+#ifdef BACKEND_TYPE_OPENBSD
+  return TRUE;
+#endif
+
+  return FALSE;
+}
+
+static gboolean
+xfsm_shutdown_fallback_can_hibernate (void)
+{
+#ifdef BACKEND_TYPE_FREEBSD
+  return freebsd_supports_sleep_state ("S4");
+#endif
+#ifdef BACKEND_TYPE_LINUX
+  return linux_supports_sleep_state ("hibernate");
+#endif
+#ifdef BACKEND_TYPE_OPENBSD
+  return FALSE;
+#endif
+
+  return FALSE;
+}
diff --git a/xfce4-session/xfsm-upower.c b/xfce4-session/xfsm-upower.c
index 57402ec..8112829 100644
--- a/xfce4-session/xfsm-upower.c
+++ b/xfce4-session/xfsm-upower.c
@@ -21,6 +21,7 @@
 
 #include <dbus/dbus-glib.h>
 #include <dbus/dbus-glib-lowlevel.h>
+#include <upower.h>
 
 #include <libxfsm/xfsm-util.h>
 #include <xfce4-session/xfsm-upower.h>
@@ -283,7 +284,7 @@ xfsm_upower_try_method (XfsmUPower   *upower,
 
 
 
-static gboolean
+gboolean
 xfsm_upower_lock_screen (XfsmUPower   *upower,
                          const gchar  *sleep_kind,
                          GError      **error)
@@ -299,6 +300,7 @@ xfsm_upower_lock_screen (XfsmUPower   *upower,
     {
       if (xfsm_upower_proxy_ensure (upower, error))
         {
+#if !UP_CHECK_VERSION(0, 99, 0)
           /* tell upower we're going to sleep, this saves some
            * time while we sleep 1 second if xflock4 is spawned */
           ret = dbus_g_proxy_call (upower->upower_proxy,
@@ -312,6 +314,7 @@ xfsm_upower_lock_screen (XfsmUPower   *upower,
               g_warning ("Couldn't sent that we were about to sleep: %s", err->message);
               g_error_free (err);
             }
+#endif
         }
       else
         {
diff --git a/xfce4-session/xfsm-upower.h b/xfce4-session/xfsm-upower.h
index a492f7d..4e6a53b 100644
--- a/xfce4-session/xfsm-upower.h
+++ b/xfce4-session/xfsm-upower.h
@@ -51,4 +51,8 @@ gboolean        xfsm_upower_can_hibernate    (XfsmUPower      *upower,
                                               gboolean        *auth_hibernate,
                                               GError         **error);
 
+gboolean        xfsm_upower_lock_screen      (XfsmUPower      *upower,
+                                              const gchar     *sleep_kind,
+                                              GError         **error);
+
 #endif /* !__XFSM_UPOWER_HELPER_H__ */
diff --git a/xfsm-shutdown-helper/main.c b/xfsm-shutdown-helper/main.c
index 667f288..60c621b 100644
--- a/xfsm-shutdown-helper/main.c
+++ b/xfsm-shutdown-helper/main.c
@@ -57,6 +57,12 @@
 #ifdef REBOOT_CMD
 #undef REBOOT_CMD
 #endif
+#ifdef UP_BACKEND_SUSPEND_COMMAND
+#undef UP_BACKEND_SUSPEND_COMMAND
+#endif
+#ifdef UP_BACKEND_HIBERNATE_COMMAND
+#undef UP_BACKEND_HIBERNATE_COMMAND
+#endif
 
 #if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
 #define POWEROFF_CMD  "/sbin/shutdown -p now"
@@ -68,6 +74,18 @@
 #define POWEROFF_CMD  "/sbin/shutdown -h now"
 #define REBOOT_CMD    "/sbin/shutdown -r now"
 #endif
+#ifdef BACKEND_TYPE_FREEBSD
+#define UP_BACKEND_SUSPEND_COMMAND "/usr/sbin/zzz"
+#define UP_BACKEND_HIBERNATE_COMMAND "/usr/sbin/acpiconf -s 4"
+#endif
+#if BACKEND_TYPE_LINUX
+#define UP_BACKEND_SUSPEND_COMMAND "/usr/sbin/pm-suspend"
+#define UP_BACKEND_HIBERNATE_COMMAND "/usr/sbin/pm-hibernate"
+#endif
+#ifdef BACKEND_TYPE_OPENBSD
+#define UP_BACKEND_SUSPEND_COMMAND	"/usr/sbin/zzz"
+#define UP_BACKEND_HIBERNATE_COMMAND "/dev/null"
+#endif
 
 
 static gboolean
@@ -143,6 +161,14 @@ main (int argc, char **argv)
     {
       succeed = run (REBOOT_CMD);
     }
+  else if (strncasecmp (action, "SUSPEND", 7) == 0)
+    {
+      succeed = run (UP_BACKEND_SUSPEND_COMMAND);
+    }
+  else if (strncasecmp (action, "HIBERNATE", 9) == 0)
+    {
+      succeed = run (UP_BACKEND_HIBERNATE_COMMAND);
+    }
 
   if (succeed)
     {
-- 
1.9.3