[Xfce4-commits] <xfce4-mixer:gber/improvements> Make xfce4-mixer a singleton application (bug #5676)

Guido Berhoerster noreply at xfce.org
Fri Sep 21 17:18:23 CEST 2012


Updating branch refs/heads/gber/improvements
         to 9ebcc4e61a394cecf298f859810f2ae81b3df1fa (commit)
       from 94fe3cdf3f8a1b9d49db6a4c360c584e2fd66709 (commit)

commit 9ebcc4e61a394cecf298f859810f2ae81b3df1fa
Author: Guido Berhoerster <guido+xfce at berhoerster.name>
Date:   Fri Sep 21 12:00:36 2012 +0200

    Make xfce4-mixer a singleton application (bug #5676)
    
    Make xfce4-mixer a singleton application using libunique, executing xfce4-mixer
    while another instance is already running brings the running instance to the
    foreground.

 NEWS                    |    3 ++
 configure.in.in         |    1 +
 xfce4-mixer/Makefile.am |    2 +
 xfce4-mixer/main.c      |   73 ++++++++++++++++++++++++++++++++++++++++------
 4 files changed, 69 insertions(+), 10 deletions(-)

diff --git a/NEWS b/NEWS
index e5e133a..7cab556 100644
--- a/NEWS
+++ b/NEWS
@@ -31,6 +31,9 @@
 - Add a popup with a scale for setting the volume to the panel plugin which is
   opened on left click, allow running the uder-defined command previously bound
   to left click from the panel plugin context menu instead.
+- Make xfce4-mixer a singleton application, executing xfce4-mixer while another
+  instance is already running brings the running instance to the foreground
+  (bug #5676).
 
 
 4.8.0
diff --git a/configure.in.in b/configure.in.in
index cbefa48..a1f7c12 100644
--- a/configure.in.in
+++ b/configure.in.in
@@ -94,6 +94,7 @@ XDT_CHECK_PACKAGE([GTHREAD], [gthread-2.0], [2.24.0])
 XDT_CHECK_PACKAGE([DBUS_GLIB], [dbus-glib-1], [0.84])
 XDT_CHECK_PACKAGE([GST_PLUGINS_BASE], [gstreamer-plugins-base-0.10], [0.10.23])
 XDT_CHECK_PACKAGE([GTK], [gtk+-2.0], [2.20.0])
+XDT_CHECK_PACKAGE([UNIQUE], [unique-1.0], [1.1])
 XDT_CHECK_PACKAGE([LIBXFCE4UTIL], [libxfce4util-1.0], [4.10.0])
 XDT_CHECK_PACKAGE([LIBXFCE4UI], [libxfce4ui-1], [4.10.0])
 XDT_CHECK_PACKAGE([LIBXFCE4PANEL], [libxfce4panel-1.0], [4.10.0])
diff --git a/xfce4-mixer/Makefile.am b/xfce4-mixer/Makefile.am
index fd17aeb..e5a45f0 100644
--- a/xfce4-mixer/Makefile.am
+++ b/xfce4-mixer/Makefile.am
@@ -26,6 +26,7 @@ xfce4_mixer_CFLAGS = 							\
 	-DMIXER_DATADIR=\"$(pkgdatadir)\"				\
 	$(GLIB_CFLAGS)							\
 	$(GTK_CFLAGS)							\
+	$(UNIQUE_CFLAGS)						\
 	$(LIBXFCE4UTIL_CFLAGS)						\
 	$(LIBXFCE4UI_CFLAGS)						\
 	$(XFCONF_CFLAGS)						\
@@ -39,6 +40,7 @@ xfce4_mixer_LDFLAGS = 							\
 	$(GLIB_LIBS)							\
 	$(GTHREAD_LIBS)							\
 	$(GTK_LIBS)							\
+	$(UNIQUE_LIBS)							\
 	$(LIBXFCE4UTIL_LIBS)						\
 	$(LIBXFCE4UI_LIBS)						\
 	$(XFCONF_LIBS)							\
diff --git a/xfce4-mixer/main.c b/xfce4-mixer/main.c
index a4c32d7..5a65f0b 100644
--- a/xfce4-mixer/main.c
+++ b/xfce4-mixer/main.c
@@ -1,6 +1,7 @@
 /* vi:set expandtab sw=2 sts=2: */
 /*-
  * Copyright (c) 2008 Jannis Pohlmann <jannis at xfce.org>
+ * Copyright (c) 2012 Guido Berhoerster <guido+xfce at berhoerster.name>
  *
  * 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
@@ -28,6 +29,9 @@
 
 #include <gst/gst.h>
 
+#include <gtk/gtk.h>
+#include <unique/unique.h>
+
 #include <libxfce4util/libxfce4util.h>
 #include <libxfce4ui/libxfce4ui.h>
 #include <xfconf/xfconf.h>
@@ -38,12 +42,42 @@
 
 
 
+static UniqueResponse
+message_received (UniqueApp         *app,
+                  UniqueCommand      command,
+                  UniqueMessageData *message,
+                  guint              time_,
+                  GtkWidget         *window)
+{
+  UniqueResponse response;
+
+  switch (command)
+    {
+      case UNIQUE_ACTIVATE:
+        /* Move window to the screen the command was started on */
+        gtk_window_set_screen (GTK_WINDOW (window), unique_message_data_get_screen (message));
+        /* Bring window to the foreground */
+        gtk_window_present_with_time (GTK_WINDOW (window), time_);
+        response = UNIQUE_RESPONSE_OK;
+        break;
+      default:
+        /* Invalid command */
+        response = UNIQUE_RESPONSE_FAIL;
+        break;
+    }
+
+  return response;
+}
+
+
+
 int 
 main (int    argc,
       char **argv)
 {
-  GtkWidget *window;
-  GError    *error = NULL;
+  UniqueApp     *app;
+  GtkWidget     *window;
+  GError        *error = NULL;
 
   /* Setup translation domain */
   xfce_textdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR, "UTF-8");
@@ -96,17 +130,36 @@ main (int    argc,
       return EXIT_FAILURE;
     }
 
-  /* Create the mixer window */
-  window = xfce_mixer_window_new ();
+  /* Create unique application */
+  app = unique_app_new ("org.xfce.xfce4-mixer", NULL);
+  if (unique_app_is_running (app))
+    {
+      unique_app_send_message (app, UNIQUE_ACTIVATE, NULL);
+
+      g_object_unref (app);
+    }
+  else
+    {
+      /* Create the mixer window */
+      window = xfce_mixer_window_new ();
 
-  /* Display the mixer window */
-  gtk_widget_show (window);
+      /* Display the mixer window */
+      gtk_widget_show (window);
 
-  /* Enter the GTK+ main loop */
-  gtk_main ();
+      /* Watch mixer window */
+      unique_app_watch_window (app, GTK_WINDOW (window));
 
-  /* Destroy the window */
-  gtk_widget_destroy (window);
+      /* Handle messages */
+      g_signal_connect (app, "message-received", G_CALLBACK (message_received), window);
+
+      /* Enter the GTK+ main loop */
+      gtk_main ();
+
+      g_object_unref (app);
+
+      /* Destroy the window */
+      gtk_widget_destroy (window);
+    }
 
   /* Shutdown the mixer library */
   xfce_mixer_shutdown ();


More information about the Xfce4-commits mailing list