[Xfce4-commits] [xfce/xfce4-power-manager] 01/01: Handle screensaver activity with it's own class (Bug #12603)

noreply at xfce.org noreply at xfce.org
Tue May 31 07:57:42 CEST 2016


This is an automated email from the git hooks/post-receive script.

eric pushed a commit to branch master
in repository xfce/xfce4-power-manager.

commit 9e70d2e4aa8b9287a4d06d35010a01ba3bebd857
Author: Eric Koegel <eric.koegel at gmail.com>
Date:   Fri May 27 15:45:13 2016 +0300

    Handle screensaver activity with it's own class (Bug #12603)
    
    Move the screensaver inhibit, heartbeat, and lock code to its own
    set of files we can easily share with xfce4-session. Use the
    screensaver's dbus API if it supports it for inhibit and lock
    calls. Otherwise, use the heartbeat-command and LockCommand
    from xfpm and xfsm if available. Finally, for the lock function,
    fallback to trying xdg-screensaver, xflock4, and xscreensaver-command
    in that order.
---
 common/xfpm-common.c   |  29 ---
 common/xfpm-common.h   |   2 -
 configure.ac.in        |   4 -
 src/Makefile.am        |   2 +
 src/xfce-screensaver.c | 565 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/xfce-screensaver.h |  58 +++++
 src/xfpm-manager.c     |   5 +-
 src/xfpm-power.c       | 232 +-------------------
 8 files changed, 638 insertions(+), 259 deletions(-)

diff --git a/common/xfpm-common.c b/common/xfpm-common.c
index fc02e2f..b0bb475 100644
--- a/common/xfpm-common.c
+++ b/common/xfpm-common.c
@@ -52,35 +52,6 @@ GtkBuilder *xfpm_builder_new_from_string (const gchar *ui, GError **error)
     return builder;
 }
 
-gboolean
-xfpm_lock_screen (void)
-{
-    gboolean ret = g_spawn_command_line_async ("xflock4", NULL);
-    
-    if ( !ret )
-    {
-        ret = g_spawn_command_line_async ("gnome-screensaver-command -l", NULL);
-    }
-    
-    if ( !ret )
-    {
-        /* this should be the default*/
-        ret = g_spawn_command_line_async ("xdg-screensaver lock", NULL);
-    }
-    
-    if ( !ret )
-    {
-        ret = g_spawn_command_line_async ("xscreensaver-command -lock", NULL);
-    }
-    
-    if ( !ret )
-    {
-        g_critical ("Connot lock screen\n");
-    }
-
-    return ret;
-}
-
 void       
 xfpm_preferences (void) 
 {
diff --git a/common/xfpm-common.h b/common/xfpm-common.h
index 9f4e080..10c0b4d 100644
--- a/common/xfpm-common.h
+++ b/common/xfpm-common.h
@@ -40,8 +40,6 @@ gboolean        xfpm_string_to_bool     	(const gchar *string) G_GNUC_PURE;
 GtkBuilder     *xfpm_builder_new_from_string   	(const gchar *file,
 						 GError **error);
 
-gboolean   	xfpm_lock_screen  		(void);
-
 void       	xfpm_preferences		(void);
 
 void        xfpm_preferences_device_id (const gchar* object_path);
diff --git a/configure.ac.in b/configure.ac.in
index 1bea291..9a54083 100644
--- a/configure.ac.in
+++ b/configure.ac.in
@@ -50,10 +50,6 @@ AC_CHECK_FUNCS([getpwuid setsid sigaction])
 # ===================================================== #
 AC_CHECK_LIB([m], [round])
 
-# ===================================================== #
-# 		Check for XScreenSaverSuspend		#
-# ===================================================== #
-AC_CHECK_LIB([Xss], [XScreenSaverSuspend])
 
 # ===================================================== #
 # 		Check for i18n support 			#
diff --git a/src/Makefile.am b/src/Makefile.am
index f4c4a97..9e63f85 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -37,6 +37,8 @@ xfce4_power_manager_SOURCES =                   \
 	xfpm-errors.h				\
 	xfpm-suspend.c				\
 	xfpm-suspend.h				\
+	xfce-screensaver.c			\
+	xfce-screensaver.h			\
 	../panel-plugins/power-manager-plugin/power-manager-button.c	\
 	../panel-plugins/power-manager-plugin/power-manager-button.h	\
 	../panel-plugins/power-manager-plugin/scalemenuitem.c			\
diff --git a/src/xfce-screensaver.c b/src/xfce-screensaver.c
new file mode 100644
index 0000000..c8d1b97
--- /dev/null
+++ b/src/xfce-screensaver.c
@@ -0,0 +1,565 @@
+/*
+ * * Copyright (C) 2016 Eric Koegel <eric at xfce.org>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif
+
+#include <gtk/gtk.h>
+#include <gdk/gdk.h>
+#include <gdk/gdkx.h>
+#include <gio/gio.h>
+
+#include <libxfce4util/libxfce4util.h>
+#include <xfconf/xfconf.h>
+
+#include "xfce-screensaver.h"
+
+
+#define HEARTBEAT_COMMAND       "heartbeat-command"
+#define LOCK_COMMAND            "LockCommand"
+#define XFPM_CHANNEL            "xfce4-power-manager"
+#define XFPM_PROPERTIES_PREFIX  "/xfce4-power-manager/"
+#define XFSM_CHANNEL            "xfce4-session"
+#define XFSM_PROPERTIES_PREFIX  "/general/"
+
+static void xfce_screensvaer_finalize   (GObject *object);
+
+static void xfce_screensaver_set_property(GObject *object,
+                                          guint property_id,
+                                          const GValue *value,
+                                          GParamSpec *pspec);
+static void xfce_screensaver_get_property(GObject *object,
+                                          guint property_id,
+                                          GValue *value,
+                                          GParamSpec *pspec);
+
+
+#define XFCE_SCREENSAVER_GET_PRIVATE(o) \
+(G_TYPE_INSTANCE_GET_PRIVATE ((o), XFCE_TYPE_SCREENSAVER, XfceScreenSaverPrivate))
+
+
+typedef enum
+{
+    SCREENSAVER_TYPE_NONE,
+    SCREENSAVER_TYPE_FREEDESKTOP,
+    SCREENSAVER_TYPE_CINNAMON,
+    SCREENSAVER_TYPE_MATE,
+    SCREENSAVER_TYPE_GNOME,
+    SCREENSAVER_TYPE_OTHER,
+    N_SCREENSAVER_TYPE
+} ScreenSaverType;
+
+enum
+{
+    PROP_0 = 0,
+    PROP_HEARTBEAT_COMMAND,
+    PROP_LOCK_COMMAND
+};
+
+struct XfceScreenSaverPrivate
+{
+    guint            cookie;
+    gchar           *heartbeat_command;
+    gchar           *lock_command;
+    GDBusProxy      *proxy;
+    guint            screensaver_id;
+    ScreenSaverType  screensaver_type;
+    XfconfChannel   *xfpm_channel;
+    XfconfChannel   *xfsm_channel;
+};
+
+
+G_DEFINE_TYPE (XfceScreenSaver, xfce_screensaver, G_TYPE_OBJECT)
+
+
+static void
+xfce_screensaver_class_init (XfceScreenSaverClass *klass)
+{
+    GObjectClass *object_class = G_OBJECT_CLASS(klass);
+
+    object_class->finalize = xfce_screensvaer_finalize;
+    object_class->set_property = xfce_screensaver_set_property;
+    object_class->get_property = xfce_screensaver_get_property;
+
+    g_type_class_add_private (klass, sizeof (XfceScreenSaverPrivate));
+
+#define XFCE_PARAM_FLAGS  (G_PARAM_READWRITE \
+                         | G_PARAM_CONSTRUCT \
+                         | G_PARAM_STATIC_NAME \
+                         | G_PARAM_STATIC_NICK \
+                         | G_PARAM_STATIC_BLURB)
+
+    /* heartbeat command - to inhibit the screensaver from activating,
+     * i.e. xscreensaver-command -deactivate */
+    g_object_class_install_property(object_class, PROP_HEARTBEAT_COMMAND,
+                                    g_param_spec_string(HEARTBEAT_COMMAND,
+                                                        HEARTBEAT_COMMAND,
+                                                        "Inhibit the screensaver from activating, "
+                                                        "i.e. xscreensaver-command -deactivate",
+                                                        NULL,
+                                                        XFCE_PARAM_FLAGS));
+
+    /* lock command - to lock the desktop, i.e. xscreensaver-command -lock */
+    g_object_class_install_property(object_class, PROP_LOCK_COMMAND,
+                                    g_param_spec_string(LOCK_COMMAND,
+                                                        LOCK_COMMAND,
+                                                        "Lock the desktop, i.e. "
+                                                        "xscreensaver-command -lock",
+                                                        NULL,
+                                                        XFCE_PARAM_FLAGS));
+#undef XFCE_PARAM_FLAGS
+}
+
+static void
+xfce_screensaver_set_property(GObject *object,
+                              guint property_id,
+                              const GValue *value,
+                              GParamSpec *pspec)
+{
+    XfceScreenSaver *saver = XFCE_SCREENSAVER (object);
+
+    switch(property_id) {
+        case PROP_HEARTBEAT_COMMAND:
+        {
+            g_free (saver->priv->heartbeat_command);
+            saver->priv->heartbeat_command = g_value_dup_string (value);
+            break;
+        }
+        case PROP_LOCK_COMMAND:
+        {
+            g_free (saver->priv->lock_command);
+            saver->priv->lock_command = g_value_dup_string (value);
+            break;
+        }
+        default:
+        {
+            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
+            break;
+        }
+    }
+}
+
+static void
+xfce_screensaver_get_property(GObject *object,
+                              guint property_id,
+                              GValue *value,
+                              GParamSpec *pspec)
+{
+    XfceScreenSaver *saver = XFCE_SCREENSAVER (object);
+
+    switch(property_id) {
+        case PROP_HEARTBEAT_COMMAND:
+            g_value_set_string (value, saver->priv->heartbeat_command);
+            break;
+
+        case PROP_LOCK_COMMAND:
+            g_value_set_string (value, saver->priv->lock_command);
+            break;
+
+        default:
+            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
+            break;
+    }
+}
+
+static gboolean
+screen_saver_proxy_setup(XfceScreenSaver *saver,
+                         const gchar     *name,
+                         const gchar     *object_path,
+                         const gchar     *interface)
+{
+    GDBusProxy *proxy;
+
+    proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
+                                           G_DBUS_PROXY_FLAGS_NONE,
+                                           NULL,
+                                           name,
+                                           object_path,
+                                           interface,
+                                           NULL,
+                                           NULL);
+
+    if (proxy != NULL)
+    {
+        gchar *owner = NULL;
+        /* is there anyone actually providing a service? */
+        owner = g_dbus_proxy_get_name_owner (proxy);
+        if (owner != NULL)
+        {
+            DBG ("proxy owner: %s", owner);
+            saver->priv->proxy = proxy;
+            g_free (owner);
+            return TRUE;
+        }
+        else
+        {
+            /* not using this proxy, nobody's home */
+            g_object_unref (proxy);
+        }
+    }
+
+    return FALSE;
+}
+
+static void
+xfce_screensaver_setup(XfceScreenSaver *saver)
+{
+    /* Try to use the freedesktop dbus API */
+    if (screen_saver_proxy_setup (saver,
+                                  "org.freedesktop.ScreenSaver",
+                                  "/org/freedesktop/ScreenSaver",
+                                  "org.freedesktop.ScreenSaver"))
+    {
+        DBG ("using freedesktop compliant screensaver daemon");
+        saver->priv->screensaver_type = SCREENSAVER_TYPE_FREEDESKTOP;
+    } else if (screen_saver_proxy_setup (saver,
+                                         "org.cinnamon.ScreenSaver",
+                                         "/org/cinnamon/ScreenSaver",
+                                         "org.cinnamon.ScreenSaver"))
+    {
+        DBG ("using cinnamon screensaver daemon");
+        saver->priv->screensaver_type = SCREENSAVER_TYPE_CINNAMON;
+    } else if (screen_saver_proxy_setup (saver,
+                                         "org.mate.ScreenSaver",
+                                         "/org/mate/ScreenSaver",
+                                         "org.mate.ScreenSaver"))
+    {
+        DBG ("using mate screensaver daemon");
+        saver->priv->screensaver_type = SCREENSAVER_TYPE_MATE;
+    } else if (screen_saver_proxy_setup (saver,
+                                         "org.gnome.ScreenSaver",
+                                         "/org/gnome/ScreenSaver",
+                                         "org.gnome.ScreenSaver"))
+    {
+        DBG ("using gnome screensaver daemon");
+        saver->priv->screensaver_type = SCREENSAVER_TYPE_GNOME;
+    }
+    else
+    {
+        DBG ("using command line screensaver interface");
+        saver->priv->screensaver_type = SCREENSAVER_TYPE_OTHER;
+    }
+}
+
+static void
+xfce_screensaver_init (XfceScreenSaver *saver)
+{
+    GError *error = NULL;
+
+    saver->priv = XFCE_SCREENSAVER_GET_PRIVATE (saver);
+
+    if ( !xfconf_init (&error) )
+    {
+        g_critical ("xfconf_init failed: %s\n", error->message);
+        g_clear_error (&error);
+    }
+    else
+    {
+        saver->priv->xfpm_channel = xfconf_channel_get (XFPM_CHANNEL);
+        saver->priv->xfsm_channel = xfconf_channel_get (XFSM_CHANNEL);
+
+        xfconf_g_property_bind (saver->priv->xfpm_channel,
+                                XFPM_PROPERTIES_PREFIX HEARTBEAT_COMMAND,
+                                G_TYPE_STRING,
+                                G_OBJECT(saver),
+                                HEARTBEAT_COMMAND);
+
+        xfconf_g_property_bind (saver->priv->xfsm_channel,
+                                XFSM_PROPERTIES_PREFIX LOCK_COMMAND,
+                                G_TYPE_STRING,
+                                G_OBJECT(saver),
+                                LOCK_COMMAND);
+    }
+
+    xfce_screensaver_setup (saver);
+}
+
+static void
+xfce_screensvaer_finalize (GObject *object)
+{
+    XfceScreenSaver *saver = XFCE_SCREENSAVER (object);
+
+    if (saver->priv->screensaver_id != 0)
+    {
+        g_source_remove (saver->priv->screensaver_id);
+        saver->priv->screensaver_id = 0;
+    }
+
+    if (saver->priv->proxy)
+    {
+        g_object_unref (saver->priv->proxy);
+        saver->priv->proxy = NULL;
+    }
+
+    if (saver->priv->heartbeat_command)
+    {
+        g_free (saver->priv->heartbeat_command);
+        saver->priv->heartbeat_command = NULL;
+    }
+
+    if (saver->priv->lock_command)
+    {
+        g_free (saver->priv->heartbeat_command);
+        saver->priv->heartbeat_command = NULL;
+    }
+}
+
+/**
+ * xfce_screensaver_new:
+ *
+ * Creates a new XfceScreenSaver object or increases the refrence count
+ * of the current object. Call g_object_unref when finished.
+ *
+ * RETURNS: an XfceScreenSaver object
+ **/
+XfceScreenSaver *
+xfce_screensaver_new (void)
+{
+    static gpointer *saver = NULL;
+
+    if (saver != NULL)
+    {
+        g_object_ref (saver);
+    }
+    else
+    {
+        saver = g_object_new (XFCE_TYPE_SCREENSAVER, NULL);
+
+        g_object_add_weak_pointer (G_OBJECT (saver), (gpointer *) &saver);
+    }
+    
+    return XFCE_SCREENSAVER (saver);
+}
+
+static gboolean
+xfce_reset_screen_saver (XfceScreenSaver *saver)
+{
+    TRACE("entering");
+
+    /* If we found an interface during the setup, use it */
+    if (saver->priv->proxy)
+    {
+        GVariant *response = g_dbus_proxy_call_sync (saver->priv->proxy,
+                                                     "SimulateUserActivity",
+                                                     NULL,
+                                                     G_DBUS_CALL_FLAGS_NONE,
+                                                     -1,
+                                                     NULL,
+                                                     NULL);
+        if (response != NULL)
+        {
+            g_variant_unref (response);
+        }
+    } else if (saver->priv->heartbeat_command)
+    {
+        g_spawn_command_line_async (saver->priv->heartbeat_command, NULL);
+    }
+
+    /* continue until we're removed */
+    return TRUE;
+}
+
+/**
+ * xfce_screensaver_inhibit:
+ * @saver: The XfceScreenSaver object
+ * @inhibit: Wether to inhibit the screensaver from activating.
+ *
+ * Calling this function with inhibit as TRUE will prevent the user's
+ * screensaver from activating. This is useful when the user is watching
+ * a movie or giving a presentation.
+ * 
+ * Calling this function with inhibit as FALSE will remove any current
+ * screensaver inhibit the XfceScreenSaver object has.
+ *
+ **/
+void
+xfce_screensaver_inhibit (XfceScreenSaver *saver,
+                          gboolean inhibit)
+{
+    if (saver->priv->screensaver_type != SCREENSAVER_TYPE_FREEDESKTOP &&
+        saver->priv->screensaver_type != SCREENSAVER_TYPE_MATE)
+    {
+        /* remove any existing keepalive */
+        if (saver->priv->screensaver_id != 0)
+        {
+            g_source_remove (saver->priv->screensaver_id);
+            saver->priv->screensaver_id = 0;
+        }
+
+        if (inhibit)
+        {
+            /* Reset the screensaver timers every so often so they don't activate */
+            saver->priv->screensaver_id = g_timeout_add_seconds (20,
+                                                                 (GSourceFunc)xfce_reset_screen_saver,
+                                                                 saver);
+        }
+        return;
+    }
+
+    /* SCREENSAVER_TYPE_FREEDESKTOP & SCREENSAVER_TYPE_MATE
+     * don't need a periodic timer because they have an actual
+     * inhibit/uninhibit setup */
+    if (inhibit)
+    {
+        GVariant *response = NULL;
+        response = g_dbus_proxy_call_sync (saver->priv->proxy,
+                                           "Inhibit",
+                                           g_variant_new ("(ss)",
+                                                          PACKAGE_NAME,
+                                                          "Inhibit requested"),
+                                           G_DBUS_CALL_FLAGS_NONE,
+                                           -1,
+                                           NULL,
+                                           NULL);
+        if (response != NULL)
+        {
+            g_variant_get (response, "(u)", &saver->priv->cookie);
+            g_variant_unref (response);
+        }
+    }
+    else
+    {
+        GVariant *response = NULL;
+        response = g_dbus_proxy_call_sync (saver->priv->proxy,
+                                           "UnInhibit",
+                                           g_variant_new ("(u)",
+                                                          saver->priv->cookie),
+                                           G_DBUS_CALL_FLAGS_NONE,
+                                           -1,
+                                           NULL,
+                                           NULL);
+
+        saver->priv->cookie = 0;
+        if (response != NULL)
+        {
+            g_variant_unref (response);
+        }
+    }
+}
+
+/**
+ * xfce_screensaver_lock:
+ * @saver: The XfceScreenSaver object
+ *
+ * Attempts to lock the screen, either with one of the screensaver
+ * dbus proxies, the xfconf lock command, or one of the
+ * fallback scripts such as xdg-screensaver.
+ *
+ * RETURNS TRUE if the lock attempt returns success.
+ **/
+gboolean
+xfce_screensaver_lock (XfceScreenSaver *saver)
+{
+    switch (saver->priv->screensaver_type) {
+        case SCREENSAVER_TYPE_FREEDESKTOP:
+        case SCREENSAVER_TYPE_MATE:
+        case SCREENSAVER_TYPE_GNOME:
+        {
+            GVariant *response = NULL;
+            response = g_dbus_proxy_call_sync (saver->priv->proxy,
+                                               "Lock",
+                                               g_variant_new ("()"),
+                                               G_DBUS_CALL_FLAGS_NONE,
+                                               -1,
+                                               NULL,
+                                               NULL);
+            if (response != NULL)
+            {
+                g_variant_unref (response);
+                return TRUE;
+            }
+            else
+            {
+                return FALSE;
+            }
+            break;
+        }
+        case SCREENSAVER_TYPE_CINNAMON:
+        {
+            GVariant *response = NULL;
+            response = g_dbus_proxy_call_sync (saver->priv->proxy,
+                                               "Lock",
+                                               g_variant_new ("(s)", PACKAGE_NAME),
+                                               G_DBUS_CALL_FLAGS_NONE,
+                                               -1,
+                                               NULL,
+                                               NULL);
+            if (response != NULL)
+            {
+                g_variant_unref (response);
+                return TRUE;
+            }
+            else
+            {
+                return FALSE;
+            }
+            break;
+        }
+        case SCREENSAVER_TYPE_OTHER:
+        {
+            gboolean ret = g_spawn_command_line_async (saver->priv->lock_command, NULL);
+
+            if (!ret)
+            {
+                g_error ("Screensaver lock command not set when attempting to lock the screen.\n"
+                         "Please set the xfconf property %s%s to the desired lock command",
+                         XFSM_PROPERTIES_PREFIX, LOCK_COMMAND);
+
+                /* Fall back to trying a couple others, using the xdg standard
+                 * one first */
+                ret = g_spawn_command_line_async ("xdg-screensaver lock", NULL);
+            }
+
+            if (!ret)
+            {
+                ret = g_spawn_command_line_async ("xflock4", NULL);
+            }
+            
+            if (!ret)
+            {
+                ret = g_spawn_command_line_async ("xscreensaver-command -lock", NULL);
+            }
+
+            return ret;
+            /* obviously we don't need this break statement but I'm sure some
+             * compiler or static analysis tool will complain */
+            break;
+        }
+        default:
+        {
+            g_warning ("Unknown screensaver type set when calling xfce_screensaver_lock");
+            break;
+        }
+    }
+
+    return FALSE;
+}
diff --git a/src/xfce-screensaver.h b/src/xfce-screensaver.h
new file mode 100644
index 0000000..260e1c2
--- /dev/null
+++ b/src/xfce-screensaver.h
@@ -0,0 +1,58 @@
+/*
+ * * Copyright (C) 2016 Eric Koegel <eric at xfce.org>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef __XFCE_SCREENSAVER_H
+#define __XFCE_SCREENSAVER_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define XFCE_TYPE_SCREENSAVER  (xfce_screensaver_get_type () )
+#define XFCE_SCREENSAVER(o)    (G_TYPE_CHECK_INSTANCE_CAST ((o), XFCE_TYPE_SCREENSAVER, XfceScreenSaver))
+#define XFCE_IS_POWER(o)       (G_TYPE_CHECK_INSTANCE_TYPE ((o), XFCE_TYPE_SCREENSAVER))
+
+typedef struct XfceScreenSaverPrivate XfceScreenSaverPrivate;
+
+typedef struct
+{
+    GObject                 parent;
+    XfceScreenSaverPrivate *priv;
+} XfceScreenSaver;
+
+typedef struct
+{
+    GObjectClass parent_class;
+} XfceScreenSaverClass;
+
+GType            xfce_screensaver_get_type      (void) G_GNUC_CONST;
+
+XfceScreenSaver *xfce_screensaver_new           (void);
+
+void             xfce_screensaver_inhibit       (XfceScreenSaver *saver,
+                                                 gboolean suspend);
+
+gboolean         xfce_screensaver_lock          (XfceScreenSaver *saver);
+
+
+
+G_END_DECLS
+
+#endif /* __XFCE_SCREENSAVER_H */
diff --git a/src/xfpm-manager.c b/src/xfpm-manager.c
index 5329392..675c42c 100644
--- a/src/xfpm-manager.c
+++ b/src/xfpm-manager.c
@@ -61,6 +61,7 @@
 #include "xfpm-enum-types.h"
 #include "xfpm-dbus-monitor.h"
 #include "xfpm-systemd.h"
+#include "xfce-screensaver.h"
 #include "../panel-plugins/power-manager-plugin/power-manager-button.h"
 
 static void xfpm_manager_finalize   (GObject *object);
@@ -102,6 +103,7 @@ struct XfpmManagerPrivate
     XfpmSystemd        *systemd;
     XfpmDBusMonitor    *monitor;
     XfpmInhibit        *inhibit;
+    XfceScreenSaver    *screensaver;
     EggIdletime        *idle;
     GtkStatusIcon      *adapter_icon;
     GtkWidget          *power_button;
@@ -446,7 +448,7 @@ xfpm_manager_lid_changed_cb (XfpmPower *power, gboolean lid_is_closed, XfpmManag
 	{
 	    if ( !xfpm_is_multihead_connected () )
 	    {
-		if (!xfpm_lock_screen ())
+		if (!xfce_screensaver_lock (manager->priv->screensaver))
 		{
 		    xfce_dialog_show_error (NULL, NULL,
 					    _("None of the screen lock tools ran "
@@ -843,6 +845,7 @@ void xfpm_manager_start (XfpmManager *manager)
     manager->priv->power = xfpm_power_get ();
     manager->priv->button = xfpm_button_new ();
     manager->priv->conf = xfpm_xfconf_new ();
+    manager->priv->screensaver = xfce_screensaver_new ();
     manager->priv->console = NULL;
     manager->priv->systemd = NULL;
 
diff --git a/src/xfpm-power.c b/src/xfpm-power.c
index cd987a5..172cff8 100644
--- a/src/xfpm-power.c
+++ b/src/xfpm-power.c
@@ -60,10 +60,7 @@
 #include "xfpm-systemd.h"
 #include "xfpm-suspend.h"
 #include "xfpm-brightness.h"
-
-#ifdef HAVE_LIBXSS
-#include <X11/extensions/scrnsaver.h>
-#endif  /* HAVE_LIBXSS */
+#include "xfce-screensaver.h"
 
 static void xfpm_power_finalize     (GObject *object);
 
@@ -114,10 +111,7 @@ struct XfpmPowerPrivate
 
     gboolean	     inhibited;
     gboolean	     screensaver_inhibited;
-    gulong	     screensaver_id;
-    GDBusProxy	    *screen_saver_proxy;
-    guint	     screen_saver_cookie;
-    gchar	    *heartbeat_command;
+    XfceScreenSaver *screensaver;
 
     XfpmNotify	    *notify;
 #ifdef ENABLE_POLKIT
@@ -431,7 +425,7 @@ xfpm_power_sleep (XfpmPower *power, const gchar *sleep_time, gboolean force)
             g_usleep (2000000);
 	}
 #endif
-        if (!xfpm_lock_screen ())
+        if (!xfce_screensaver_lock (power->priv->screensaver))
         {
 	    GtkWidget *dialog;
 	    gboolean ret;
@@ -955,209 +949,6 @@ xfpm_power_remove_device (XfpmPower *power, const gchar *object_path)
     g_hash_table_remove (power->priv->hash, object_path);
 }
 
-static gboolean
-idle_reset_screen_saver (XfpmPower *power)
-{
-    Display *dpy = gdk_x11_get_default_xdisplay ();
-
-    TRACE("entering");
-
-    XResetScreenSaver (dpy);
-
-    XFlush (dpy);
-
-    /* If we found an interface during the setup, use it */
-    if (power->priv->screen_saver_proxy)
-    {
-	GVariant *response = g_dbus_proxy_call_sync (power->priv->screen_saver_proxy,
-						     "SimulateUserActivity",
-						     NULL,
-						     G_DBUS_CALL_FLAGS_NONE,
-						     -1,
-						     NULL,
-						     NULL);
-	if (response)
-	{
-	    g_variant_unref (response);
-	}
-    } else if (power->priv->heartbeat_command)
-    {
-	g_spawn_command_line_async (power->priv->heartbeat_command, NULL);
-    }
-
-    /* continue until we're removed */
-    return TRUE;
-}
-
-static gboolean
-screen_saver_proxy_setup(XfpmPower *power,
-			 const gchar *name,
-			 const gchar *object_path,
-			 const gchar *interface)
-{
-    GDBusProxy *proxy;
-    /* Try to inhibit via the freedesktop dbus API */
-    proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
-					   G_DBUS_PROXY_FLAGS_NONE,
-					   NULL,
-					   name,
-					   object_path,
-					   interface,
-					   NULL,
-					   NULL);
-
-    if (proxy != NULL)
-    {
-	gchar *owner = NULL;
-	/* is there anyone actually providing a service? */
-	owner = g_dbus_proxy_get_name_owner (proxy);
-	if (owner != NULL)
-	{
-	    XFPM_DEBUG ("proxy owner: %s", owner);
-	    power->priv->screen_saver_proxy = proxy;
-	    g_free (owner);
-	    return TRUE;
-	}
-    }
-
-    return FALSE;
-}
-
-static void
-screen_saver_suspend(XfpmPower *power, gboolean suspend)
-{
-#ifndef HAVE_LIBXSS
-    TRACE("!HAVE_XSS");
-#else
-    int event, dummy, major, minor;
-    Display *dpy = gdk_x11_get_default_xdisplay();
-
-    TRACE("entering");
-
-    if (XScreenSaverQueryExtension(dpy, &event, &dummy) != TRUE ||
-        XScreenSaverQueryVersion(dpy, &major, &minor) != TRUE)
-    {
-        XFPM_DEBUG ("XScreenSaverQueryExtension or XScreenSaverQueryVersion call failed");
-    }
-
-    /* XScreenSaverSuspend was introduced in MIT-SCREEN-SAVER 1.1 */
-    if (major < 1 || (major == 1 && minor < 1))
-    {
-        XFPM_DEBUG ("version %d.%d not supported", major, minor);
-    }
-
-    XFPM_DEBUG ("XScreenSaverSuspend: %s", suspend ? "TRUE" : "FALSE");
-    XScreenSaverSuspend(dpy, suspend);
-#endif /* HAVE_LIBXSS */
-
-
-    if (power->priv->screensaver_id != 0)
-    {
-        g_source_remove (power->priv->screensaver_id);
-        power->priv->screensaver_id = 0;
-    }
-
-    if (suspend == FALSE)
-    {
-	if (power->priv->screen_saver_proxy)
-	{
-	    g_object_unref (power->priv->screen_saver_proxy);
-	    power->priv->screen_saver_proxy = NULL;
-	}
-	if (power->priv->heartbeat_command)
-	{
-	    g_free (power->priv->heartbeat_command);
-	    power->priv->heartbeat_command = NULL;
-	}
-    }
-
-    /* Try to inhibit via the freedesktop dbus API */
-    if (screen_saver_proxy_setup (power,
-				  "org.freedesktop.ScreenSaver",
-				  "/org/freedesktop/ScreenSaver",
-				  "org.freedesktop.ScreenSaver"))
-    {
-	GVariant *response = NULL;
-	XFPM_DEBUG ("found freedesktop screensaver daemon");
-	if (suspend)
-	{
-	    response = g_dbus_proxy_call_sync (power->priv->screen_saver_proxy,
-					       "Inhibit",
-					       g_variant_new ("(ss)",
-							      "xfce4-power-manager",
-							      ""),
-					       G_DBUS_CALL_FLAGS_NONE,
-					       -1,
-					       NULL,
-					       NULL);
-	    if (response != NULL)
-	    {
-		power->priv->screen_saver_cookie = g_variant_get_uint32 (response);
-		g_variant_unref (response);
-	    }
-	} else {
-	    response = g_dbus_proxy_call_sync (power->priv->screen_saver_proxy,
-					       "UnInhibit",
-					       g_variant_new ("(u)",
-							      power->priv->screen_saver_cookie),
-					       G_DBUS_CALL_FLAGS_NONE,
-					       -1,
-					       NULL,
-					       NULL);
-
-	    power->priv->screen_saver_cookie = 0;
-	    if (response != NULL)
-	    {
-		g_variant_unref (response);
-	    }
-	}
-	g_object_unref (power->priv->screen_saver_proxy);
-	power->priv->screen_saver_proxy = NULL;
-	return;
-    }
-
-    if (suspend == FALSE)
-	return;
-
-    /* So much for standards, let's try some random interfaces */
-    if (screen_saver_proxy_setup (power,
-				  "org.cinnamon.ScreenSaver",
-				  "/org/cinnamon/ScreenSaver",
-				  "org.cinnamon.ScreenSaver"))
-    {
-	XFPM_DEBUG ("found cinnamon screensaver daemon");
-    } else if (screen_saver_proxy_setup (power,
-					 "org.mate.ScreenSaver",
-					 "/org/mate/ScreenSaver",
-					 "org.mate.ScreenSaver"))
-    {
-	XFPM_DEBUG ("found mate screensaver daemon");
-    } else if (screen_saver_proxy_setup (power,
-					 "org.gnome.ScreenSaver",
-					 "/org/gnome/ScreenSaver",
-					 "org.gnome.ScreenSaver"))
-    {
-	XFPM_DEBUG ("found gnome screensaver daemon");
-    }
-    else
-    {
-	gchar *heartbeat_command = NULL;
-	g_object_get (G_OBJECT (power->priv->conf),
-		      HEARTBEAT_COMMAND, &heartbeat_command,
-		      NULL);
-	if (heartbeat_command != NULL)
-	{
-	    XFPM_DEBUG ("found heartbeat command %s", heartbeat_command);
-	    power->priv->heartbeat_command = heartbeat_command;
-	}
-    }
-
-    /* Reset the screensaver timers every so often so they don't activate */
-    power->priv->screensaver_id = g_timeout_add_seconds (20,
-							 (GSourceFunc)idle_reset_screen_saver,
-							 power);
-}
-
 static void
 xfpm_power_inhibit_changed_cb (XfpmInhibit *inhibit, gboolean is_inhibit, XfpmPower *power)
 {
@@ -1175,7 +966,7 @@ xfpm_power_inhibit_changed_cb (XfpmInhibit *inhibit, gboolean is_inhibit, XfpmPo
         {
             if (!power->priv->screensaver_inhibited)
             {
-                screen_saver_suspend (power, TRUE);
+                xfce_screensaver_inhibit (power->priv->screensaver, TRUE);
                 power->priv->screensaver_inhibited = TRUE;
             }
         }
@@ -1184,7 +975,7 @@ xfpm_power_inhibit_changed_cb (XfpmInhibit *inhibit, gboolean is_inhibit, XfpmPo
             /* Or make sure we remove the screensaver inhibit */
             if (power->priv->screensaver_inhibited && !power->priv->presentation_mode)
             {
-                screen_saver_suspend (power, FALSE);
+                xfce_screensaver_inhibit (power->priv->screensaver, FALSE);
                 power->priv->screensaver_inhibited = FALSE;
             }
         }
@@ -1415,6 +1206,7 @@ xfpm_power_init (XfpmPower *power)
     power->priv->notify  = xfpm_notify_new ();
     power->priv->conf    = xfpm_xfconf_new ();
     power->priv->upower  = up_client_new ();
+    power->priv->screensaver = xfce_screensaver_new ();
 
     power->priv->systemd = NULL;
     power->priv->console = NULL;
@@ -1544,6 +1336,7 @@ xfpm_power_finalize (GObject *object)
     g_object_unref (power->priv->inhibit);
     g_object_unref (power->priv->notify);
     g_object_unref (power->priv->conf);
+    g_object_unref (power->priv->screensaver);
 
     if ( power->priv->systemd != NULL )
         g_object_unref (power->priv->systemd);
@@ -1560,12 +1353,6 @@ xfpm_power_finalize (GObject *object)
 
     g_object_unref(power->priv->dpms);
 
-    if (power->priv->screensaver_id != 0)
-    {
-	g_source_remove (power->priv->screensaver_id);
-	power->priv->screensaver_id = 0;
-    }
-
     G_OBJECT_CLASS (xfpm_power_parent_class)->finalize (object);
 }
 
@@ -1686,7 +1473,7 @@ xfpm_power_change_presentation_mode (XfpmPower *power, gboolean presentation_mod
 	/* presentation mode inhibits the screensaver */
 	if (!power->priv->screensaver_inhibited)
 	{
-	    screen_saver_suspend (power, TRUE);
+	    xfce_screensaver_inhibit (power->priv->screensaver, TRUE);
 	    power->priv->screensaver_inhibited = TRUE;
 	}
     }
@@ -1697,8 +1484,7 @@ xfpm_power_change_presentation_mode (XfpmPower *power, gboolean presentation_mod
 	/* make sure we remove the screensaver inhibit */
 	if (power->priv->screensaver_inhibited && !power->priv->inhibited)
 	{
-	    DBG("here");
-	    screen_saver_suspend (power, FALSE);
+	    xfce_screensaver_inhibit (power->priv->screensaver, FALSE);
 	    power->priv->screensaver_inhibited = FALSE;
 	}
 

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list