check them startupnotify-provided wmclass against both parts of a window's wm_class...
[dana/openbox.git] / openbox / startupnotify.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    startupnotify.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "startupnotify.h"
21 #include "gettext.h"
22 #include "event.h"
23
24 #include <stdlib.h>
25
26 #ifndef USE_LIBSN
27
28 void sn_startup(gboolean reconfig) {}
29 void sn_shutdown(gboolean reconfig) {}
30 gboolean sn_app_starting() { return FALSE; }
31 Time sn_app_started(const gchar *id, const gchar *wmclass, const gchar *name)
32 {
33     return CurrentTime;
34 }
35 gboolean sn_get_desktop(gchar *id, guint *desktop) { return FALSE; }
36 void sn_setup_spawn_environment(gchar *program, gchar *name,
37                                 gchar *icon_name, gint desktop) {}
38 void sn_spawn_cancel() {}
39
40 #else
41
42 #include "openbox.h"
43 #include "mainloop.h"
44 #include "screen.h"
45
46 #define SN_API_NOT_YET_FROZEN
47 #include <libsn/sn.h>
48
49 static SnDisplay *sn_display;
50 static SnMonitorContext *sn_context;
51 static SnLauncherContext *sn_launcher;
52 static GSList *sn_waits; /* list of SnStartupSequences we're waiting on */
53
54 static SnStartupSequence* sequence_find(const gchar *id);
55
56 static void sn_handler(const XEvent *e, gpointer data);
57 static void sn_event_func(SnMonitorEvent *event, gpointer data);
58
59 void sn_startup(gboolean reconfig)
60 {
61     gchar *s;
62
63     if (reconfig) return;
64
65     /* unset this so we don't pass it on unknowingly */
66     s = g_strdup("DESKTOP_STARTUP_ID");
67     putenv(s);
68     g_free(s);
69
70     sn_display = sn_display_new(ob_display, NULL, NULL);
71     sn_context = sn_monitor_context_new(sn_display, ob_screen,
72                                         sn_event_func, NULL, NULL);
73     sn_launcher = sn_launcher_context_new(sn_display, ob_screen);
74
75     ob_main_loop_x_add(ob_main_loop, sn_handler, NULL, NULL);
76 }
77
78 void sn_shutdown(gboolean reconfig)
79 {
80     GSList *it;
81
82     if (reconfig) return;
83
84     ob_main_loop_x_remove(ob_main_loop, sn_handler);
85
86     for (it = sn_waits; it; it = g_slist_next(it))
87         sn_startup_sequence_unref((SnStartupSequence*)it->data);
88     g_slist_free(sn_waits);
89     sn_waits = NULL;
90
91     screen_set_root_cursor();
92
93     sn_launcher_context_unref(sn_launcher);
94     sn_monitor_context_unref(sn_context);
95     sn_display_unref(sn_display);
96 }
97
98 static SnStartupSequence* sequence_find(const gchar *id)
99 {
100     SnStartupSequence*ret = NULL;
101     GSList *it;
102
103     for (it = sn_waits; it; it = g_slist_next(it)) {
104         SnStartupSequence *seq = it->data;
105         if (!strcmp(id, sn_startup_sequence_get_id(seq))) {
106             ret = seq;
107             break;
108         }
109     }
110     return ret;
111 }
112
113 gboolean sn_app_starting(void)
114 {
115     return sn_waits != NULL;
116 }
117
118 static gboolean sn_wait_timeout(gpointer data)
119 {
120     SnStartupSequence *seq = data;
121     sn_waits = g_slist_remove(sn_waits, seq);
122     screen_set_root_cursor();
123     return FALSE; /* don't repeat */
124 }
125
126 static void sn_handler(const XEvent *e, gpointer data)
127 {
128     XEvent ec;
129     ec = *e;
130     sn_display_process_event(sn_display, &ec);
131 }
132
133 static void sn_event_func(SnMonitorEvent *ev, gpointer data)
134 {
135     SnStartupSequence *seq;
136     gboolean change = FALSE;
137
138     if (!(seq = sn_monitor_event_get_startup_sequence(ev)))
139         return;
140
141     switch (sn_monitor_event_get_type(ev)) {
142     case SN_MONITOR_EVENT_INITIATED:
143         sn_startup_sequence_ref(seq);
144         sn_waits = g_slist_prepend(sn_waits, seq);
145         /* 20 second timeout for apps to start if the launcher doesn't
146            have a timeout */
147         ob_main_loop_timeout_add(ob_main_loop, 20 * G_USEC_PER_SEC,
148                                  sn_wait_timeout, seq,
149                                  g_direct_equal,
150                                  (GDestroyNotify)sn_startup_sequence_unref);
151         change = TRUE;
152         break;
153     case SN_MONITOR_EVENT_CHANGED:
154         /* XXX feedback changed? */
155         change = TRUE;
156         break;
157     case SN_MONITOR_EVENT_COMPLETED:
158     case SN_MONITOR_EVENT_CANCELED:
159         if ((seq = sequence_find(sn_startup_sequence_get_id(seq)))) {
160             sn_waits = g_slist_remove(sn_waits, seq);
161             ob_main_loop_timeout_remove_data(ob_main_loop, sn_wait_timeout,
162                                              seq, FALSE);
163             change = TRUE;
164         }
165         break;
166     };
167
168     if (change)
169         screen_set_root_cursor();
170 }
171
172 Time sn_app_started(const gchar *id, const gchar *wmclass, const gchar *name)
173 {
174     GSList *it;
175     Time t = CurrentTime;
176
177     if (!id && !wmclass)
178         return t;
179
180     for (it = sn_waits; it; it = g_slist_next(it)) {
181         SnStartupSequence *seq = it->data;
182         gboolean found = FALSE;
183         const gchar *seqid, *seqclass, *seqname, *seqbin;
184         seqid = sn_startup_sequence_get_id(seq);
185         seqclass = sn_startup_sequence_get_wmclass(seq);
186         seqname = sn_startup_sequence_get_name(seq);
187         seqbin = sn_startup_sequence_get_binary_name(seq);
188
189         if (id && seqid) {
190             /* if the app has a startup id, then look for that for highest
191                accuracy */
192             if (!strcmp(seqid, id))
193                 found = TRUE;
194         } else {
195             seqclass = sn_startup_sequence_get_wmclass(seq);
196             seqbin = sn_startup_sequence_get_binary_name(seq);
197
198             /* seqclass = "a string to match against the "resource name" or
199                "resource class" hints.  These are WM_CLASS[0] and WM_CLASS[1]"
200                - from the startup-notification spec
201             */
202             if ((seqclass && !strcmp(seqclass, wmclass)) ||
203                 (seqclass && !strcmp(seqclass, name)) ||
204                 /* Check the binary name against the class and name hints
205                    as well, to help apps that don't have the class set
206                    correctly */
207                 (seqbin && !g_ascii_strcasecmp(seqbin, wmclass)) ||
208                 (seqbin && !g_ascii_strcasecmp(seqbin, name)))
209             {
210                 found = TRUE;
211             }
212         }
213
214         if (found) {
215             sn_startup_sequence_complete(seq);
216             t = sn_startup_sequence_get_timestamp(seq);
217             break;
218         }
219     }
220     return t;
221 }
222
223 gboolean sn_get_desktop(gchar *id, guint *desktop)
224 {
225     SnStartupSequence *seq;
226
227     if (id && (seq = sequence_find(id))) {
228         gint desk = sn_startup_sequence_get_workspace(seq);
229         if (desk != -1) {
230             *desktop = desk;
231             return TRUE;
232         }
233     }
234     return FALSE;
235 }
236
237 static gboolean sn_launch_wait_timeout(gpointer data)
238 {
239     SnLauncherContext *sn = data;
240     sn_launcher_context_complete(sn);
241     return FALSE; /* don't repeat */
242 }
243
244 void sn_setup_spawn_environment(gchar *program, gchar *name,
245                                 gchar *icon_name, gint desktop)
246 {
247     gchar *desc;
248     const char *id;
249
250     desc = g_strdup_printf(_("Running %s\n"), program);
251
252     if (sn_launcher_context_get_initiated(sn_launcher)) {
253         sn_launcher_context_unref(sn_launcher);
254         sn_launcher = sn_launcher_context_new(sn_display, ob_screen);
255     }
256
257     sn_launcher_context_set_name(sn_launcher, name ? name : program);
258     sn_launcher_context_set_description(sn_launcher, desc);
259     sn_launcher_context_set_icon_name(sn_launcher, icon_name ?
260                                       icon_name : program);
261     sn_launcher_context_set_binary_name(sn_launcher, program);
262     if (desktop >= 0 && (unsigned) desktop < screen_num_desktops)
263         sn_launcher_context_set_workspace(sn_launcher, (signed) desktop);
264     sn_launcher_context_initiate(sn_launcher, "openbox", program,
265                                  event_curtime);
266     id = sn_launcher_context_get_startup_id(sn_launcher);
267
268     /* 20 second timeout for apps to start */
269     sn_launcher_context_ref(sn_launcher);
270     ob_main_loop_timeout_add(ob_main_loop, 20 * G_USEC_PER_SEC,
271                              sn_launch_wait_timeout, sn_launcher,
272                              g_direct_equal,
273                              (GDestroyNotify)sn_launcher_context_unref);
274
275     putenv(g_strdup_printf("DESKTOP_STARTUP_ID=%s", id));
276
277     g_free(desc);
278 }
279
280 void sn_spawn_cancel(void)
281 {
282     sn_launcher_context_complete(sn_launcher);
283 }
284
285 #endif