[gedit/wip/gtkapp: 11/13] Add startup notification timestamp hack



commit 9c6e58e21a14189995ad9b4770cb51ac278709d9
Author: Ryan Lortie <desrt desrt ca>
Date:   Tue Nov 27 16:23:04 2012 -0500

    Add startup notification timestamp hack
    
    In the case that we are invoked with a DISPLAY but not a
    DESKTOP_STARTUP_ID, "steal" the current timestamp out of the server and
    act like it's the one we were invokved with.
    
    This deals with the case of launching gedit from a terminal and
    expecting the existing window to raise itself.

 gedit/gedit-app-x11.c |   62 +++++++++++++++++++++++++++++++++++++++++++++++++
 gedit/gedit-app.c     |    2 +
 2 files changed, 64 insertions(+), 0 deletions(-)
---
diff --git a/gedit/gedit-app-x11.c b/gedit/gedit-app-x11.c
index 4667b5e..037dace 100644
--- a/gedit/gedit-app-x11.c
+++ b/gedit/gedit-app-x11.c
@@ -32,12 +32,74 @@ gedit_app_x11_finalize (GObject *object)
 	G_OBJECT_CLASS (gedit_app_x11_parent_class)->finalize (object);
 }
 
+/* This should go in GtkApplication at some point... */
+
+#include <X11/Xatom.h>
+#include <X11/Xlib.h>
+
+static void
+gedit_app_add_platform_data (GApplication    *app,
+                             GVariantBuilder *builder)
+{
+  G_APPLICATION_CLASS (gedit_app_x11_parent_class)->add_platform_data (app, builder);
+
+  /* In the event that we are launched from a terminal we will probably
+   * see that we have DISPLAY set, but not DESKTOP_STARTUP_ID.
+   *
+   * If we are the second instance, we want to bring the window of the
+   * existing instance forward.  Unfortunately, without
+   * DESKTOP_STARTUP_ID being set, we have no timestamp on which to base
+   * the claim that we are the representative of the user's last
+   * interaction.
+   *
+   * We need to fake it by finding out the current timestamp of the
+   * server and sending that along as if we had DESKTOP_STARTUP_ID given
+   * in the first place.
+   *
+   * We want to do this without initialising all of Gtk+, so we can just
+   * talk directly to the X server to find out...
+   *
+   * In order to get the current timestamp, we need to see a
+   * timestamp-tagged event sent by the server.  Property change
+   * notifications are probably the easiest to generate.  We create our
+   * own window so that we don't spam other people who may be watching
+   * for property notifies on the root window...
+   *
+   * We could use any property at all, but we may as well use the
+   * "_NET_WM_USER_TIME_WINDOW" one since that's what we're doing
+   * here...
+   */
+  if (!g_getenv ("DESKTOP_STARTUP_ID") && g_getenv ("DISPLAY"))
+    {
+      gchar *startup_id;
+      Display *display;
+      Window window;
+      XEvent event;
+      Atom atom;
+
+      display = XOpenDisplay (0);
+      window = XCreateWindow (display, DefaultRootWindow (display), 0, 0, 1, 1, 0, 0, InputOnly, 0, 0, NULL);
+      XSelectInput (display, window, PropertyChangeMask);
+      atom = XInternAtom (display, "_NET_WM_USER_TIME_WINDOW", False);
+      XChangeProperty (display, window, atom, XA_WINDOW, 32, PropModeReplace, (void *) &window, 1);
+      XNextEvent (display, &event);
+      g_assert (event.type == PropertyNotify);
+      XCloseDisplay (display);
+
+      startup_id = g_strdup_printf ("_TIME%u", (guint) ((XPropertyEvent *) &event)->time);
+      g_variant_builder_add (builder, "{sv}", "desktop-startup-id", g_variant_new_string (startup_id));
+      g_free (startup_id);
+    }
+}
+
 static void
 gedit_app_x11_class_init (GeditAppX11Class *klass)
 {
 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+	GApplicationClass *app_class = G_APPLICATION_CLASS (klass);
 
 	object_class->finalize = gedit_app_x11_finalize;
+	app_class->add_platform_data = gedit_app_add_platform_data;
 }
 
 static void
diff --git a/gedit/gedit-app.c b/gedit/gedit-app.c
index fcdb82c..c197c5c 100644
--- a/gedit/gedit-app.c
+++ b/gedit/gedit-app.c
@@ -459,6 +459,8 @@ gedit_app_activate (GApplication *application)
 		gedit_debug_message (DEBUG_APP, "Create tab");
 		gedit_window_create_tab (window, TRUE);
 	}
+
+	gtk_window_present (GTK_WINDOW (window));
 }
 
 static GOptionContext *



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