Merge branch 'backport' into work
[dana/openbox.git] / openbox / startupnotify.c
index 1542be0..3e8799f 100644 (file)
 #include "gettext.h"
 #include "event.h"
 
-#include <stdlib.h>
+#ifdef HAVE_STDLIB_H
+#  include <stdlib.h>
+#endif
 
 #ifndef USE_LIBSN
 
 void sn_startup(gboolean reconfig) {}
 void sn_shutdown(gboolean reconfig) {}
 gboolean sn_app_starting() { return FALSE; }
-Time sn_app_started(const gchar *id, const gchar *wmclass)
+Time sn_app_started(const gchar *id, const gchar *wmclass, const gchar *name)
 {
     return CurrentTime;
 }
 gboolean sn_get_desktop(gchar *id, guint *desktop) { return FALSE; }
-void sn_setup_spawn_environment(gchar *program, gchar *name,
-                                gchar *icon_name, gint desktop) {}
+void sn_setup_spawn_environment(const gchar *program, const gchar *name,
+                                const gchar *icon_name, const gchar *wmclass,
+                                gint desktop) {}
 void sn_spawn_cancel() {}
 
 #else
@@ -57,15 +60,8 @@ static void sn_event_func(SnMonitorEvent *event, gpointer data);
 
 void sn_startup(gboolean reconfig)
 {
-    gchar *s;
-
     if (reconfig) return;
 
-    /* unset this so we don't pass it on unknowingly */
-    s = g_strdup("DESKTOP_STARTUP_ID");
-    putenv(s);
-    g_free(s);
-
     sn_display = sn_display_new(obt_display, NULL, NULL);
     sn_context = sn_monitor_context_new(sn_display, ob_screen,
                                         sn_event_func, NULL, NULL);
@@ -168,7 +164,7 @@ static void sn_event_func(SnMonitorEvent *ev, gpointer data)
         screen_set_root_cursor();
 }
 
-Time sn_app_started(const gchar *id, const gchar *wmclass)
+Time sn_app_started(const gchar *id, const gchar *wmclass, const gchar *name)
 {
     GSList *it;
     Time t = CurrentTime;
@@ -179,10 +175,9 @@ Time sn_app_started(const gchar *id, const gchar *wmclass)
     for (it = sn_waits; it; it = g_slist_next(it)) {
         SnStartupSequence *seq = it->data;
         gboolean found = FALSE;
-        const gchar *seqid, *seqclass, *seqname, *seqbin;
+        const gchar *seqid, *seqclass, *seqbin;
         seqid = sn_startup_sequence_get_id(seq);
         seqclass = sn_startup_sequence_get_wmclass(seq);
-        seqname = sn_startup_sequence_get_name(seq);
         seqbin = sn_startup_sequence_get_binary_name(seq);
 
         if (id && seqid) {
@@ -190,15 +185,21 @@ Time sn_app_started(const gchar *id, const gchar *wmclass)
                accuracy */
             if (!strcmp(seqid, id))
                 found = TRUE;
-        } else {
-            seqclass = sn_startup_sequence_get_wmclass(seq);
-            seqname = sn_startup_sequence_get_name(seq);
-            seqbin = sn_startup_sequence_get_binary_name(seq);
-
-            if ((seqname && !g_ascii_strcasecmp(seqname, wmclass)) ||
-                (seqbin && !g_ascii_strcasecmp(seqbin, wmclass)) ||
-                (seqclass && !strcmp(seqclass, wmclass)))
-                found = TRUE;
+        }
+        else if (seqclass) {
+            /* seqclass = "a string to match against the "resource name" or
+               "resource class" hints.  These are WM_CLASS[0] and WM_CLASS[1]"
+               - from the startup-notification spec
+            */
+            found = (seqclass && !strcmp(seqclass, wmclass)) ||
+                (seqclass && !strcmp(seqclass, name));
+        }
+        else if (seqbin) {
+            /* Check the binary name against the class and name hints
+               as well, to help apps that don't have the class set
+               correctly */
+            found = (seqbin && !g_ascii_strcasecmp(seqbin, wmclass)) ||
+                (seqbin && !g_ascii_strcasecmp(seqbin, name));
         }
 
         if (found) {
@@ -231,8 +232,9 @@ static gboolean sn_launch_wait_timeout(gpointer data)
     return FALSE; /* don't repeat */
 }
 
-void sn_setup_spawn_environment(gchar *program, gchar *name,
-                                gchar *icon_name, gint desktop)
+void sn_setup_spawn_environment(const gchar *program, const gchar *name,
+                                const gchar *icon_name, const gchar *wmclass,
+                                gint desktop)
 {
     gchar *desc;
     const char *id;
@@ -249,6 +251,7 @@ void sn_setup_spawn_environment(gchar *program, gchar *name,
     sn_launcher_context_set_icon_name(sn_launcher, icon_name ?
                                       icon_name : program);
     sn_launcher_context_set_binary_name(sn_launcher, program);
+    if (wmclass) sn_launcher_context_set_wmclass(sn_launcher, wmclass);
     if (desktop >= 0 && (unsigned) desktop < screen_num_desktops)
         sn_launcher_context_set_workspace(sn_launcher, (signed) desktop);
     sn_launcher_context_initiate(sn_launcher, "openbox", program,
@@ -262,7 +265,7 @@ void sn_setup_spawn_environment(gchar *program, gchar *name,
                               g_direct_equal,
                               (GDestroyNotify)sn_launcher_context_unref);
 
-    putenv(g_strdup_printf("DESKTOP_STARTUP_ID=%s", id));
+    setenv("DESKTOP_STARTUP_ID", id, TRUE);
 
     g_free(desc);
 }