[gnome-color-manager] Make the DBus service exit 60 seconds after it was last used, to save resources



commit 09a4a07dc72fcd5b7513c8033d4f2a7f53050558
Author: Richard Hughes <richard hughsie com>
Date:   Fri Nov 6 11:44:25 2009 +0000

    Make the DBus service exit 60 seconds after it was last used, to save resources

 src/gcm-dbus.c    |   18 ++++++++++++++++++
 src/gcm-dbus.h    |    2 ++
 src/gcm-session.c |   38 +++++++++++++++++++++++++++++++-------
 3 files changed, 51 insertions(+), 7 deletions(-)
---
diff --git a/src/gcm-dbus.c b/src/gcm-dbus.c
index b671dd1..e2aee10 100644
--- a/src/gcm-dbus.c
+++ b/src/gcm-dbus.c
@@ -38,6 +38,7 @@ struct GcmDbusPrivate
 {
 	GConfClient		*gconf_client;
 	GcmClient		*client;
+	GTimer			*timer;
 };
 
 G_DEFINE_TYPE (GcmDbus, gcm_dbus, G_TYPE_OBJECT)
@@ -77,6 +78,18 @@ gcm_dbus_error_get_type (void)
 }
 
 /**
+ * gcm_dbus_get_idle_time:
+ **/
+guint
+gcm_dbus_get_idle_time (GcmDbus	*dbus)
+{
+	guint idle;
+	idle = (guint) g_timer_elapsed (dbus->priv->timer, NULL);
+	egg_debug ("we've been idle for %is", idle);
+	return idle;
+}
+
+/**
  * gcm_dbus_get_profiles_for_device:
  **/
 void
@@ -122,6 +135,9 @@ gcm_dbus_get_profiles_for_device (GcmDbus *dbus, const gchar *sysfs_path, const
 	/* return profiles */
 	dbus_g_method_return (context, profiles);
 
+	/* reset time */
+	g_timer_reset (dbus->priv->timer);
+
 	g_strfreev (profiles);
 	g_ptr_array_unref (array);
 }
@@ -151,6 +167,7 @@ gcm_dbus_init (GcmDbus *dbus)
 	dbus->priv = GCM_DBUS_GET_PRIVATE (dbus);
 	dbus->priv->gconf_client = gconf_client_get_default ();
 	dbus->priv->client = gcm_client_new ();
+	dbus->priv->timer = g_timer_new ();
 
 	/* get all devices */
 	ret = gcm_client_coldplug (dbus->priv->client, &error);
@@ -174,6 +191,7 @@ gcm_dbus_finalize (GObject *object)
 	g_return_if_fail (dbus->priv != NULL);
 	g_object_unref (dbus->priv->client);
 	g_object_unref (dbus->priv->gconf_client);
+	g_timer_destroy (dbus->priv->timer);
 
 	G_OBJECT_CLASS (gcm_dbus_parent_class)->finalize (object);
 }
diff --git a/src/gcm-dbus.h b/src/gcm-dbus.h
index 4380cbc..041bb8f 100644
--- a/src/gcm-dbus.h
+++ b/src/gcm-dbus.h
@@ -61,6 +61,8 @@ GType		 gcm_dbus_error_get_type		(void);
 GType		 gcm_dbus_get_type			(void);
 GcmDbus		*gcm_dbus_new				(void);
 
+guint		 gcm_dbus_get_idle_time			(GcmDbus	*dbus);
+
 /* org.gnome.ColorManager */
 void		 gcm_dbus_get_profiles_for_device	(GcmDbus	*dbus,
 							 const gchar	*sysfs_path,
diff --git a/src/gcm-session.c b/src/gcm-session.c
index 46d3f68..060605e 100644
--- a/src/gcm-session.c
+++ b/src/gcm-session.c
@@ -29,9 +29,12 @@
 #include "gcm-dbus.h"
 #include "org.gnome.ColorManager.h"
 
+static GMainLoop *loop = NULL;
+
 #define GCM_DBUS_SERVICE	"org.gnome.ColorManager"
 #define GCM_DBUS_INTERFACE	"org.gnome.ColorManager"
 #define GCM_DBUS_PATH		"/org/gnome/ColorManager"
+#define GCM_SESSION_IDLE_EXIT	60 /* seconds */
 
 /**
  * gcm_session_object_register:
@@ -83,13 +86,32 @@ gcm_session_object_register (DBusGConnection *connection, GObject *object)
 }
 
 /**
+ * gcm_session_check_idle_cb:
+ **/
+static gboolean
+gcm_session_check_idle_cb (GcmDbus *dbus)
+{
+	guint idle;
+
+	/* get the idle time */
+	idle = gcm_dbus_get_idle_time (dbus);
+	if (idle > GCM_SESSION_IDLE_EXIT) {
+		egg_debug ("exiting loop as idle");
+		g_main_loop_quit (loop);
+		return FALSE;
+	}
+	/* continue to poll */
+	return TRUE;
+}
+
+/**
  * main:
  **/
 int
 main (int argc, char *argv[])
 {
 	gboolean verbose = FALSE;
-	gboolean timed_exit = FALSE;
+	gboolean no_timed_exit = FALSE;
 	GcmDbus *dbus = NULL;
 	GOptionContext *context;
 	GError *error = NULL;
@@ -100,8 +122,8 @@ main (int argc, char *argv[])
 	const GOptionEntry options[] = {
 		{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
 		  _("Show extra debugging information"), NULL },
-		{ "timed-exit", '\0', 0, G_OPTION_ARG_NONE, &timed_exit,
-		  _("Exit after a small delay"), NULL },
+		{ "no-timed-exit", '\0', 0, G_OPTION_ARG_NONE, &no_timed_exit,
+		  _("Do not exit after the request has been processed"), NULL },
 		{ NULL}
 	};
 
@@ -123,6 +145,7 @@ main (int argc, char *argv[])
 
 	/* create new objects */
 	dbus = gcm_dbus_new ();
+	loop = g_main_loop_new (NULL, FALSE);
 
 	/* get the bus */
 	connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
@@ -141,13 +164,14 @@ main (int argc, char *argv[])
 		goto out;
 	}
 
-	/* Only timeout if we have specified iton the command line */
-	if (timed_exit)
-		g_timeout_add_seconds (120, (GSourceFunc) gtk_main_quit, NULL);
+	/* only timeout if we have specified iton the command line */
+	if (!no_timed_exit)
+		g_timeout_add_seconds (5, (GSourceFunc) gcm_session_check_idle_cb, dbus);
 
 	/* wait */
-	gtk_main ();
+	g_main_loop_run (loop);
 out:
+	g_main_loop_unref (loop);
 	g_object_unref (dbus);
 	return retval;
 }



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