From 33ab28012b1641df9f6f543d0fb0e83b00585487 Mon Sep 17 00:00:00 2001
From: Michael Stapelberg <michael@stapelberg.de>
Date: Sat, 12 Nov 2011 15:35:20 +0000
Subject: [PATCH] Implement support for freedesktop startup notifications

This makes desktop environments/window managers able to display a progress
indicator while urxvt is starting up.
---
 configure.ac    |   33 +++++++++++++++++++++++++++++++++
 doc/rxvt.7.pod  |    5 +++++
 src/Makefile.in |    4 ++--
 src/init.C      |   29 +++++++++++++++++++++++++++++
 4 files changed, 69 insertions(+), 2 deletions(-)

diff --git a/configure.ac b/configure.ac
index 347be82..65017c9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -96,6 +96,7 @@ support_scroll_next=yes
 support_scroll_xterm=yes
 support_xim=yes
 support_pixbuf=yes
+support_startup_notification=yes
 support_afterimage=yes
 support_xft=yes
 support_unicode3=no
@@ -132,6 +133,7 @@ AC_ARG_ENABLE(everything,
        support_wtmp=no
        support_xim=no
        support_pixbuf=no
+       support_startup_notification=no
        support_afterimage=no
        support_xft=no
        support_unicode3=no
@@ -160,6 +162,7 @@ AC_ARG_ENABLE(everything,
        support_wtmp=yes
        support_xim=yes
        support_pixbuf=yes
+       support_startup_notification=yes
        support_afterimage=yes
        support_xft=yes
        support_unicode3=yes
@@ -250,6 +253,12 @@ AC_ARG_ENABLE(pixbuf,
     support_pixbuf=$enableval
   fi])
 
+AC_ARG_ENABLE(startup-notification,
+  [  --enable-startup-notification  enable freedesktop startup notification support],
+  [if test x$enableval = xyes -o x$enableval = xno; then
+    support_startup_notification=$enableval
+  fi])
+
 AC_ARG_ENABLE(transparency,
   [  --enable-transparency   enable transparent backgrounds],
   [if test x$enableval = xyes -o x$enableval = xno; then
@@ -465,6 +474,30 @@ fi
 AC_SUBST(PIXBUF_CFLAGS)
 AC_SUBST(PIXBUF_LIBS)
 
+STARTUP_NOTIFICATION_CFLAGS=
+STARTUP_NOTIFICATION_LIBS=
+
+if test x$support_startup_notification = xyes; then
+  support_startup_notification=no
+  AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
+  AC_MSG_CHECKING(for libstartup-notification)
+  if test $PKG_CONFIG != no && $PKG_CONFIG --exists libstartup-notification-1.0; then
+    STARTUP_NOTIFICATION_CFLAGS="`$PKG_CONFIG libstartup-notification-1.0 --cflags`"
+    STARTUP_NOTIFICATION_LIBS="`$PKG_CONFIG libstartup-notification-1.0 --libs`"
+    support_startup_notification=yes
+  fi
+
+  if test x$support_startup_notification = xyes; then
+    AC_MSG_RESULT(ok)
+    AC_DEFINE(HAVE_STARTUP_NOTIFICATION, 1, Define if freedesktop startup notifications should be supported)
+  else
+    AC_MSG_RESULT(no)
+  fi
+fi
+
+AC_SUBST(STARTUP_NOTIFICATION_CFLAGS)
+AC_SUBST(STARTUP_NOTIFICATION_LIBS)
+
 AFTERIMAGE_CFLAGS=
 AFTERIMAGE_LIBS=
 AFTERIMAGE_VERSION=
diff --git a/doc/rxvt.7.pod b/doc/rxvt.7.pod
index 7c829f2..b6d6631 100644
--- a/doc/rxvt.7.pod
+++ b/doc/rxvt.7.pod
@@ -2374,6 +2374,11 @@ Add support for GDK-PixBuf to be used for background images.
 It adds support for many file formats including JPG, PNG,
 TIFF, GIF, XPM, BMP, ICO and TGA.
 
+=item --enable-startup-notification (default: on)
+
+Add support for freedesktop startup notifications. This allows window managers
+to display some kind of progress indicator during startup.
+
 =item --enable-transparency (default: on)
 
 Add support for using the root pixmap as background to simulate transparency.
diff --git a/src/Makefile.in b/src/Makefile.in
index f52658f..8232c5f 100644
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -19,8 +19,8 @@ CPPFLAGS = @CPPFLAGS@
 LDFLAGS = @LDFLAGS@
 DEFS = @DEFS@
 LIBS = @LIBS@
-XINC = @X_CFLAGS@ @AFTERIMAGE_CFLAGS@ @PIXBUF_CFLAGS@
-XLIB = @X_LIBS@ @AFTERIMAGE_LIBS@ -lX11 @X_EXTRA_LIBS@ @PIXBUF_LIBS@
+XINC = @X_CFLAGS@ @AFTERIMAGE_CFLAGS@ @PIXBUF_CFLAGS@ @STARTUP_NOTIFICATION_CFLAGS@
+XLIB = @X_LIBS@ @AFTERIMAGE_LIBS@ -lX11 @X_EXTRA_LIBS@ @PIXBUF_LIBS@ @STARTUP_NOTIFICATION_LIBS@
 COMPILE = $(CXX) -I.. -I$(srcdir) -I. -I$(srcdir)/../libev -I$(srcdir)/../libptytty/src -I$(srcdir)/../libecb $(DEFS) $(CPPFLAGS) $(CXXFLAGS) $(XINC)
 LINK = @LINKER@ $(LDFLAGS)
 EXEEXT = @EXEEXT@
diff --git a/src/init.C b/src/init.C
index fbb0270..38a7928 100644
--- a/src/init.C
+++ b/src/init.C
@@ -57,6 +57,11 @@
 # include <langinfo.h>
 #endif
 
+#ifdef HAVE_STARTUP_NOTIFICATION
+# define SN_API_NOT_YET_FROZEN
+# include <libsn/sn-launchee.h>
+#endif
+
 #ifdef DISPLAY_IS_IP
 /* On Solaris link with -lsocket and -lnsl */
 #include <sys/types.h>
@@ -872,9 +877,33 @@ rxvt_term::init (int argc, const char *const *argv, stringvec *envv)
     }
 #endif
 
+#if HAVE_STARTUP_NOTIFICATION
+  SnDisplay *snDisplay;
+  SnLauncheeContext *snContext;
+
+  snDisplay = sn_display_new (dpy, NULL, NULL);
+  snContext = sn_launchee_context_new_from_environment (snDisplay, DefaultScreen (dpy));
+
+  /* Tell the window manager that this window is part of the startup context */
+  if (snContext)
+    sn_launchee_context_setup_window (snContext, parent);
+#endif
+
   XMapWindow (dpy, vt);
   XMapWindow (dpy, parent);
 
+#if HAVE_STARTUP_NOTIFICATION
+  if (snContext)
+    {
+      /* Mark the startup process as complete */
+      sn_launchee_context_complete (snContext);
+
+      sn_launchee_context_unref (snContext);
+    }
+
+  sn_display_unref(snDisplay);
+#endif
+
   refresh_check ();
 }
 
-- 
1.7.5.4

