[gnome-settings-daemon] common: Add systemd power state helpers
- From: Bastien Nocera <hadess src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-settings-daemon] common: Add systemd power state helpers
- Date: Thu, 14 Jun 2012 16:44:24 +0000 (UTC)
commit 18e84d4ec13cee504756c718c6dfff7c4b19cd2f
Author: Matthias Clasen <mclasen redhat com>
Date: Thu Jun 14 17:25:45 2012 +0100
common: Add systemd power state helpers
Add systemd helpers to replace ConsoleKit shutdown, and upower
hibernation and suspend.
https://bugzilla.gnome.org/show_bug.cgi?id=677241
plugins/common/Makefile.am | 5 +-
plugins/common/gsd-power-helper.c | 199 +++++++++++++++++++++++++++++++++++++
plugins/common/gsd-power-helper.h | 35 +++++++
3 files changed, 237 insertions(+), 2 deletions(-)
---
diff --git a/plugins/common/Makefile.am b/plugins/common/Makefile.am
index 64ababc..bb84a84 100644
--- a/plugins/common/Makefile.am
+++ b/plugins/common/Makefile.am
@@ -7,7 +7,8 @@ libcommon_la_SOURCES = \
gsd-keygrab.h \
gsd-input-helper.c \
gsd-input-helper.h \
- test-plugin.h
+ gsd-power-helper.c \
+ gsd-power-helper.h
libcommon_la_CPPFLAGS = \
$(AM_CPPFLAGS)
@@ -38,4 +39,4 @@ test_egg_key_parsing_CFLAGS = $(libcommon_la_CFLAGS)
scriptsdir = $(datadir)/gnome-settings-daemon- GSD_API_VERSION@
scripts_DATA = input-device-example.sh
-EXTRA_DIST = $(scripts_DATA)
+EXTRA_DIST = $(scripts_DATA) test-plugin.h
diff --git a/plugins/common/gsd-power-helper.c b/plugins/common/gsd-power-helper.c
new file mode 100644
index 0000000..bda2ef6
--- /dev/null
+++ b/plugins/common/gsd-power-helper.c
@@ -0,0 +1,199 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2012 Bastien Nocera <hadess hadess net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#include "config.h"
+
+#include "gsd-power-helper.h"
+
+#define SYSTEMD_DBUS_NAME "org.freedesktop.login1"
+#define SYSTEMD_DBUS_PATH "/org/freedesktop/login1"
+#define SYSTEMD_DBUS_INTERFACE "org.freedesktop.login1.Manager"
+
+#ifdef HAVE_SYSTEMD
+static void
+systemd_stop (void)
+{
+ GDBusConnection *bus;
+
+ bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL);
+ g_dbus_connection_call (bus,
+ SYSTEMD_DBUS_NAME,
+ SYSTEMD_DBUS_PATH,
+ SYSTEMD_DBUS_INTERFACE,
+ "PowerOff",
+ g_variant_new ("(b)", FALSE),
+ NULL, 0, G_MAXINT, NULL, NULL, NULL);
+ g_object_unref (bus);
+}
+
+static void
+systemd_suspend (void)
+{
+ GDBusConnection *bus;
+
+ bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL);
+ g_dbus_connection_call (bus,
+ SYSTEMD_DBUS_NAME,
+ SYSTEMD_DBUS_PATH,
+ SYSTEMD_DBUS_INTERFACE,
+ "Suspend",
+ g_variant_new ("(b)", TRUE),
+ NULL, 0, G_MAXINT, NULL, NULL, NULL);
+ g_object_unref (bus);
+}
+
+static void
+systemd_hibernate (void)
+{
+ GDBusConnection *bus;
+
+ bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL);
+ g_dbus_connection_call (bus,
+ SYSTEMD_DBUS_NAME,
+ SYSTEMD_DBUS_PATH,
+ SYSTEMD_DBUS_INTERFACE,
+ "Hibernate",
+ g_variant_new ("(b)", TRUE),
+ NULL, 0, G_MAXINT, NULL, NULL, NULL);
+ g_object_unref (bus);
+}
+
+#else /* HAVE_SYSTEMD */
+
+static void
+consolekit_stop_cb (GObject *source_object,
+ GAsyncResult *res,
+ gpointer user_data)
+{
+ GVariant *result;
+ GError *error = NULL;
+
+ result = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object),
+ res,
+ &error);
+ if (result == NULL) {
+ g_warning ("couldn't stop using ConsoleKit: %s",
+ error->message);
+ g_error_free (error);
+ } else {
+ g_variant_unref (result);
+ }
+}
+
+static void
+consolekit_stop (void)
+{
+ GError *error = NULL;
+ GDBusProxy *proxy;
+
+ /* power down the machine in a safe way */
+ proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
+ G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
+ NULL,
+ CONSOLEKIT_DBUS_NAME,
+ CONSOLEKIT_DBUS_PATH_MANAGER,
+ CONSOLEKIT_DBUS_INTERFACE_MANAGER,
+ NULL, &error);
+ if (proxy == NULL) {
+ g_warning ("cannot connect to ConsoleKit: %s",
+ error->message);
+ g_error_free (error);
+ return;
+ }
+ g_dbus_proxy_call (proxy,
+ "Stop",
+ NULL,
+ G_DBUS_CALL_FLAGS_NONE,
+ -1, NULL,
+ consolekit_stop_cb, NULL);
+ g_object_unref (proxy);
+}
+static void
+upower_sleep_cb (GObject *source_object,
+ GAsyncResult *res,
+ gpointer user_data)
+{
+ GVariant *result;
+ GError *error = NULL;
+
+ result = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object),
+ res,
+ &error);
+ if (result == NULL) {
+ g_warning ("couldn't sleep using UPower: %s",
+ error->message);
+ g_error_free (error);
+ } else {
+ g_variant_unref (result);
+ }
+}
+
+static void
+upower_suspend (GDBusProxy *upower_proxy)
+{
+ g_dbus_proxy_call (upower_proxy,
+ "Suspend",
+ NULL,
+ G_DBUS_CALL_FLAGS_NONE,
+ -1, NULL,
+ upower_sleep_cb, NULL);
+}
+
+static void
+upower_hibernate (GDBusProxy *upower_proxy)
+{
+ g_dbus_proxy_call (upower_proxy,
+ "Hibernate",
+ NULL,
+ G_DBUS_CALL_FLAGS_NONE,
+ -1, NULL,
+ upower_sleep_cb, NULL);
+}
+#endif /* HAVE_SYSTEMD */
+
+void
+gsd_power_suspend (GDBusProxy *upower_proxy)
+{
+#ifdef HAVE_SYSTEMD
+ systemd_suspend ();
+#else
+ upower_suspend (upower_proxy);
+#endif
+}
+
+void
+gsd_power_poweroff (void)
+{
+#ifdef HAVE_SYSTEMD
+ systemd_stop ();
+#else
+ consolekit_stop ();
+#endif
+}
+
+void
+gsd_power_hibernate (GDBusProxy *upower_proxy)
+{
+#ifdef HAVE_SYSTEMD
+ systemd_hibernate ();
+#else
+ upower_hibernate (upower_proxy);
+#endif
+}
diff --git a/plugins/common/gsd-power-helper.h b/plugins/common/gsd-power-helper.h
new file mode 100644
index 0000000..e3be14f
--- /dev/null
+++ b/plugins/common/gsd-power-helper.h
@@ -0,0 +1,35 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2012 Bastien Nocera <hadess hadess net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __GSD_POWER_HELPER_H
+#define __GSD_POWER_HELPER_H
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+#include <gio/gio.h>
+
+void gsd_power_suspend (GDBusProxy *upower_proxy);
+void gsd_power_hibernate (GDBusProxy *upower_proxy);
+void gsd_power_poweroff (void);
+
+G_END_DECLS
+
+#endif /* __GSD_POWER_HELPER_H */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]