[gthumb] disable the screensaver when playing videos or slideshows



commit 1ede67e6d73081f222e91eb780a7a31c8aa93280
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Tue May 31 13:14:30 2011 +0200

    disable the screensaver when playing videos or slideshows
    
    [new feature]

 extensions/gstreamer_tools/gth-media-viewer-page.c |   23 ++-
 extensions/slideshow/gth-slideshow.c               |    9 +
 gthumb/Makefile.am                                 |    2 +
 gthumb/gth-screensaver.c                           |  317 ++++++++++++++++++++
 gthumb/gth-screensaver.h                           |   60 ++++
 5 files changed, 408 insertions(+), 3 deletions(-)
---
diff --git a/extensions/gstreamer_tools/gth-media-viewer-page.c b/extensions/gstreamer_tools/gth-media-viewer-page.c
index 0c95bb3..6bf7b84 100644
--- a/extensions/gstreamer_tools/gth-media-viewer-page.c
+++ b/extensions/gstreamer_tools/gth-media-viewer-page.c
@@ -64,6 +64,7 @@ struct _GthMediaViewerPagePrivate {
 	GdkCursor      *cursor;
 	GdkCursor      *cursor_void;
 	gboolean        cursor_visible;
+	GthScreensaver *screensaver;
 };
 
 
@@ -622,11 +623,25 @@ update_progress_cb (gpointer user_data)
 
 
 static void
+set_playing_state (GthMediaViewerPage *self,
+	           gboolean            playing)
+{
+	self->priv->playing = playing;
+	if (self->priv->playing)
+		gth_screensaver_inhibit (self->priv->screensaver,
+					 GTK_WIDGET (self->priv->browser),
+					 _("Playing video"));
+	else
+		gth_screensaver_uninhibit (self->priv->screensaver);
+}
+
+
+static void
 update_play_button (GthMediaViewerPage *self,
 		    GstState            new_state)
 {
 	if (! self->priv->playing && (new_state == GST_STATE_PLAYING)) {
-		self->priv->playing = TRUE;
+		set_playing_state (self, TRUE);
 		gtk_image_set_from_stock (GTK_IMAGE (GET_WIDGET ("button_play_image")), GTK_STOCK_MEDIA_PAUSE, GTK_ICON_SIZE_LARGE_TOOLBAR);
 		gtk_widget_set_tooltip_text (GET_WIDGET ("button_play_image"), _("Pause"));
 
@@ -636,7 +651,7 @@ update_play_button (GthMediaViewerPage *self,
 		update_playback_info (self);
 	}
 	else if (self->priv->playing && (new_state != GST_STATE_PLAYING)) {
-		self->priv->playing = FALSE;
+		set_playing_state (self, FALSE);
 		gtk_image_set_from_stock (GTK_IMAGE (GET_WIDGET ("button_play_image")), GTK_STOCK_MEDIA_PLAY, GTK_ICON_SIZE_LARGE_TOOLBAR);
 		gtk_widget_set_tooltip_text (GET_WIDGET ("button_play_image"), _("Play"));
 
@@ -831,8 +846,8 @@ reset_player_state (GthMediaViewerPage *self)
         }
 
 	update_play_button (self, GST_STATE_NULL);
-	self->priv->playing = FALSE;
 	self->priv->rate = 1.0;
+	set_playing_state (self, FALSE);
 }
 
 
@@ -1268,6 +1283,7 @@ gth_media_viewer_page_finalize (GObject *obj)
 	}
 	_g_object_unref (self->priv->icon);
 	_g_object_unref (self->priv->file_data);
+	_g_object_unref (self->priv->screensaver);
 
 	G_OBJECT_CLASS (gth_media_viewer_page_parent_class)->finalize (obj);
 }
@@ -1316,6 +1332,7 @@ gth_media_viewer_page_instance_init (GthMediaViewerPage *self)
 	self->priv->video_fps_d = 0;
 	self->priv->icon = NULL;
 	self->priv->cursor_visible = TRUE;
+	self->priv->screensaver = gth_screensaver_new (NULL);
 }
 
 
diff --git a/extensions/slideshow/gth-slideshow.c b/extensions/slideshow/gth-slideshow.c
index 283434d..e894b79 100644
--- a/extensions/slideshow/gth-slideshow.c
+++ b/extensions/slideshow/gth-slideshow.c
@@ -83,6 +83,7 @@ struct _GthSlideshowPrivate {
 	guint                  hide_paused_sign;
 	gboolean               animating;
 	gboolean               random_order;
+	GthScreensaver        *screensaver;
 };
 
 
@@ -222,6 +223,11 @@ next_image_cb (gpointer user_data)
 static void
 view_next_image_automatically (GthSlideshow *self)
 {
+	if (self->priv->automatic && ! self->priv->paused)
+		gth_screensaver_inhibit (self->priv->screensaver, GTK_WIDGET (self), _("Playing slideshow"));
+	else
+		gth_screensaver_uninhibit (self->priv->screensaver);
+
 	if (self->priv->automatic) {
 		if (self->priv->next_event != 0)
 			g_source_remove (self->priv->next_event);
@@ -279,6 +285,7 @@ gth_slideshow_init (GthSlideshow *self)
 	self->priv->direction = GTH_SLIDESHOW_DIRECTION_FORWARD;
 	self->priv->random_order = FALSE;
 	self->priv->current_pixbuf = NULL;
+	self->priv->screensaver = gth_screensaver_new (NULL);
 
 	self->priv->preloader = gth_image_preloader_new (GTH_LOAD_POLICY_ONE_STEP, 3);
 	g_signal_connect (self->priv->preloader,
@@ -316,6 +323,8 @@ gth_slideshow_finalize (GObject *object)
 	}
 #endif
 
+	_g_object_unref (self->priv->screensaver);
+
 	G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
diff --git a/gthumb/Makefile.am b/gthumb/Makefile.am
index 314a8f9..37ee516 100644
--- a/gthumb/Makefile.am
+++ b/gthumb/Makefile.am
@@ -90,6 +90,7 @@ PUBLIC_HEADER_FILES = 					\
 	gth-pixbuf-saver.h				\
 	gth-preferences.h				\
 	gth-progress-dialog.h				\
+	gth-screensaver.h				\
 	gth-sidebar.h					\
 	gth-statusbar.h					\
 	gth-source-tree.h				\
@@ -215,6 +216,7 @@ gthumb_SOURCES = 					\
 	gth-pixbuf-saver.c				\
 	gth-preferences.c				\
 	gth-progress-dialog.c				\
+	gth-screensaver.c				\
 	gth-sidebar.c					\
 	gth-source-tree.c				\
 	gth-statusbar.c					\
diff --git a/gthumb/gth-screensaver.c b/gthumb/gth-screensaver.c
new file mode 100644
index 0000000..34fd153
--- /dev/null
+++ b/gthumb/gth-screensaver.c
@@ -0,0 +1,317 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+/*
+ *  GThumb
+ *
+ *  Copyright (C) 2011 Free Software Foundation, Inc.
+ *
+ *  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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include "gth-screensaver.h"
+#include <gdk/gdkx.h>
+
+
+#define GNOME_SESSION_MANAGER_INHIBIT_IDLE 8
+
+
+/* Properties */
+enum {
+        PROP_0,
+        PROP_APP_ID
+};
+
+
+struct _GthScreensaverPrivate {
+	char       *app_id;
+	guint       cookie;
+	GDBusProxy *proxy;
+};
+
+
+static gpointer parent_class = NULL;
+
+
+static void
+gth_screensaver_finalize (GObject *object)
+{
+	GthScreensaver *self;
+
+	self = GTH_SCREENSAVER (object);
+
+	gth_screensaver_uninhibit (self);
+
+	g_free (self->priv->app_id);
+	if (self->priv->proxy != NULL)
+		g_object_unref (self->priv->proxy);
+
+	G_OBJECT_CLASS (parent_class)->finalize (object);
+}
+
+
+static void
+gth_screensaver_set_property (GObject      *object,
+			      guint         property_id,
+			      const GValue *value,
+			      GParamSpec   *pspec)
+{
+	GthScreensaver *self;
+
+	self = GTH_SCREENSAVER (object);
+
+	switch (property_id) {
+	case PROP_APP_ID:
+		g_free (self->priv->app_id);
+		self->priv->app_id = g_value_dup_string (value);
+		break;
+	default:
+		break;
+	}
+}
+
+
+static void
+gth_screensaver_get_property (GObject    *object,
+			      guint       property_id,
+			      GValue     *value,
+			      GParamSpec *pspec)
+{
+	GthScreensaver *self;
+
+	self = GTH_SCREENSAVER (object);
+
+	switch (property_id) {
+	case PROP_APP_ID:
+		g_value_set_string (value, self->priv->app_id);
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+		break;
+	}
+}
+
+
+static void
+gth_screensaver_class_init (GthScreensaverClass *klass)
+{
+	GObjectClass *object_class;
+
+	parent_class = g_type_class_peek_parent (klass);
+	g_type_class_add_private (klass, sizeof (GthScreensaverPrivate));
+
+	object_class = (GObjectClass*) klass;
+	object_class->set_property = gth_screensaver_set_property;
+	object_class->get_property = gth_screensaver_get_property;
+	object_class->finalize = gth_screensaver_finalize;
+
+	/* properties */
+
+	g_object_class_install_property (object_class,
+					 PROP_APP_ID,
+					 g_param_spec_string ("app-id",
+                                                              "Application ID",
+                                                              "The application identifier",
+                                                              NULL,
+                                                              G_PARAM_READWRITE));
+}
+
+
+static void
+gth_screensaver_init (GthScreensaver *self)
+{
+	self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTH_TYPE_SCREENSAVER, GthScreensaverPrivate);
+	self->priv->app_id = NULL;
+	self->priv->cookie = 0;
+	self->priv->proxy = NULL;
+}
+
+
+GType
+gth_screensaver_get_type (void)
+{
+        static GType type = 0;
+
+        if (! type) {
+                GTypeInfo type_info = {
+			sizeof (GthScreensaverClass),
+			NULL,
+			NULL,
+			(GClassInitFunc) gth_screensaver_class_init,
+			NULL,
+			NULL,
+			sizeof (GthScreensaver),
+			0,
+			(GInstanceInitFunc) gth_screensaver_init
+		};
+
+		type = g_type_register_static (G_TYPE_OBJECT,
+					       "GthScreensaver",
+					       &type_info,
+					       0);
+	}
+
+        return type;
+}
+
+
+GthScreensaver *
+gth_screensaver_new (const char *application_id)
+{
+	if (application_id == NULL)
+		application_id = g_get_application_name ();
+
+	return (GthScreensaver*) g_object_new (GTH_TYPE_SCREENSAVER,
+					       "app-id", application_id,
+					       NULL);
+}
+
+
+static void
+_gth_screensaver_create_sm_proxy (GthScreensaver  *self,
+			          GError         **error)
+{
+	GDBusConnection *connection;
+
+	if (self->priv->proxy != NULL)
+		return;
+
+	connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
+	if (connection == NULL)
+		return;
+
+	self->priv->proxy = g_dbus_proxy_new_sync (connection,
+						   G_DBUS_PROXY_FLAGS_NONE,
+						   NULL,
+						   "org.gnome.SessionManager",
+						   "/org/gnome/SessionManager",
+						   "org.gnome.SessionManager",
+						   NULL,
+						   error);
+}
+
+
+static void
+org_gnome_session_manager_inhibit_ready_cb (GObject      *source_object,
+					    GAsyncResult *res,
+					    gpointer      user_data)
+{
+	GthScreensaver  *self = user_data;
+	GVariant        *value;
+	GError          *error = NULL;
+
+	value = g_dbus_proxy_call_finish (self->priv->proxy, res, &error);
+	if (value == NULL) {
+		g_warning ("%s\n", error->message);
+		g_clear_error (&error);
+		return;
+	}
+
+	g_print ("idle inhibited\n");
+
+	g_variant_get (value, "(u)", &self->priv->cookie);
+
+	g_variant_unref (value);
+}
+
+
+void
+gth_screensaver_inhibit (GthScreensaver *self,
+			 GtkWidget      *widget,
+			 const char     *reason)
+{
+	GError    *error = NULL;
+	guint      xid;
+	GtkWidget *toplevel_window;
+
+	if (self->priv->cookie != 0)
+		return;
+
+	_gth_screensaver_create_sm_proxy (self, &error);
+
+	if (error != NULL) {
+		g_warning ("%s\n", error->message);
+		g_clear_error (&error);
+		return;
+	}
+
+	xid = 0;
+	toplevel_window = gtk_widget_get_toplevel (widget);
+	if (gtk_widget_is_toplevel (toplevel_window))
+		xid = GDK_WINDOW_XID (gtk_widget_get_window (toplevel_window));
+
+	g_dbus_proxy_call (self->priv->proxy,
+			   "Inhibit",
+			   g_variant_new ("(susu)",
+					  self->priv->app_id,
+					  xid,
+					  reason,
+					  GNOME_SESSION_MANAGER_INHIBIT_IDLE),
+			   G_DBUS_CALL_FLAGS_NONE,
+			   G_MAXINT,
+			   NULL,
+			   org_gnome_session_manager_inhibit_ready_cb,
+			   self);
+}
+
+
+static void
+org_gnome_session_manager_uninhibit_ready_cb (GObject      *source_object,
+					      GAsyncResult *res,
+					      gpointer      user_data)
+{
+	GthScreensaver  *self = user_data;
+	GVariant        *value;
+	GError          *error = NULL;
+
+	value = g_dbus_proxy_call_finish (self->priv->proxy, res, &error);
+	if (value == NULL) {
+		g_warning ("%s\n", error->message);
+		g_clear_error (&error);
+	}
+
+	g_print ("idle uninhibited\n");
+
+	self->priv->cookie = 0;
+
+	if (value != NULL)
+		g_variant_unref (value);
+}
+
+
+void
+gth_screensaver_uninhibit (GthScreensaver *self)
+{
+	GError *error = NULL;
+
+	if (self->priv->cookie == 0)
+		return;
+
+	_gth_screensaver_create_sm_proxy (self, &error);
+
+	if (error != NULL) {
+		g_warning ("%s\n", error->message);
+		g_clear_error (&error);
+		return;
+	}
+
+	g_dbus_proxy_call (self->priv->proxy,
+			   "Uninhibit",
+			   g_variant_new ("(u)", self->priv->cookie),
+			   G_DBUS_CALL_FLAGS_NONE,
+			   G_MAXINT,
+			   NULL,
+			   org_gnome_session_manager_uninhibit_ready_cb,
+			   self);
+}
diff --git a/gthumb/gth-screensaver.h b/gthumb/gth-screensaver.h
new file mode 100644
index 0000000..2068b6c
--- /dev/null
+++ b/gthumb/gth-screensaver.h
@@ -0,0 +1,60 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+/*
+ *  GThumb
+ *
+ *  Copyright (C) 2011 Free Software Foundation, Inc.
+ *
+ *  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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GTH_SCREENSAVER_H
+#define GTH_SCREENSAVER_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GTH_TYPE_SCREENSAVER         (gth_screensaver_get_type ())
+#define GTH_SCREENSAVER(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GTH_TYPE_SCREENSAVER, GthScreensaver))
+#define GTH_SCREENSAVER_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST ((k), GTH_TYPE_SCREENSAVER, GthScreensaverClass))
+#define GTH_IS_SCREENSAVER(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTH_TYPE_SCREENSAVER))
+#define GTH_IS_SCREENSAVER_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), GTH_TYPE_SCREENSAVER))
+#define GTH_SCREENSAVER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS((o), GTH_TYPE_SCREENSAVER, GthScreensaverClass))
+
+typedef struct _GthScreensaver         GthScreensaver;
+typedef struct _GthScreensaverPrivate  GthScreensaverPrivate;
+typedef struct _GthScreensaverClass    GthScreensaverClass;
+
+
+struct _GthScreensaver {
+	GObject __parent;
+	GthScreensaverPrivate *priv;
+};
+
+struct _GthScreensaverClass {
+	GObjectClass __parent_class;
+};
+
+GType             gth_screensaver_get_type   (void) G_GNUC_CONST;
+GthScreensaver *  gth_screensaver_new        (const char     *application_id);
+void              gth_screensaver_inhibit    (GthScreensaver *self,
+					      GtkWidget      *widget,
+					      const char     *reason);
+void              gth_screensaver_uninhibit  (GthScreensaver *self);
+
+G_END_DECLS
+
+#endif /* GTH_SCREENSAVER_H */
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]