]> icculus.org git repositories - dana/openbox.git/blob - openbox/startupnotify.c
only unset the startup_desktop_id once, and dont free the string we pass to putenv
[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     if (reconfig) return;
62
63     sn_display = sn_display_new(ob_display, NULL, NULL);
64     sn_context = sn_monitor_context_new(sn_display, ob_screen,
65                                         sn_event_func, NULL, NULL);
66     sn_launcher = sn_launcher_context_new(sn_display, ob_screen);
67
68     ob_main_loop_x_add(ob_main_loop, sn_handler, NULL, NULL);
69 }
70
71 void sn_shutdown(gboolean reconfig)
72 {
73     GSList *it;
74
75     if (reconfig) return;
76
77     ob_main_loop_x_remove(ob_main_loop, sn_handler);
78
79     for (it = sn_waits; it; it = g_slist_next(it))
80         sn_startup_sequence_unref((SnStartupSequence*)it->data);
81     g_slist_free(sn_waits);
82     sn_waits = NULL;
83
84     screen_set_root_cursor();
85
86     sn_launcher_context_unref(sn_launcher);
87     sn_monitor_context_unref(sn_context);
88     sn_display_unref(sn_display);
89 }
90
91 static SnStartupSequence* sequence_find(const gchar *id)
92 {
93     SnStartupSequence*ret = NULL;
94     GSList *it;
95
96     for (it = sn_waits; it; it = g_slist_next(it)) {
97         SnStartupSequence *seq = it->data;
98         if (!strcmp(id, sn_startup_sequence_get_id(seq))) {
99             ret = seq;
100             break;
101         }
102     }
103     return ret;
104 }
105
106 gboolean sn_app_starting(void)
107 {
108     return sn_waits != NULL;
109 }
110
111 static gboolean sn_wait_timeout(gpointer data)
112 {
113     SnStartupSequence *seq = data;
114     sn_waits = g_slist_remove(sn_waits, seq);
115     screen_set_root_cursor();
116     return FALSE; /* don't repeat */
117 }
118
119 static void sn_handler(const XEvent *e, gpointer data)
120 {
121     XEvent ec;
122     ec = *e;
123     sn_display_process_event(sn_display, &ec);
124 }
125
126 static void sn_event_func(SnMonitorEvent *ev, gpointer data)
127 {
128     SnStartupSequence *seq;
129     gboolean change = FALSE;
130
131     if (!(seq = sn_monitor_event_get_startup_sequence(ev)))
132         return;
133
134     switch (sn_monitor_event_get_type(ev)) {
135     case SN_MONITOR_EVENT_INITIATED:
136         sn_startup_sequence_ref(seq);
137         sn_waits = g_slist_prepend(sn_waits, seq);
138         /* 20 second timeout for apps to start if the launcher doesn't
139            have a timeout */
140         ob_main_loop_timeout_add(ob_main_loop, 20 * G_USEC_PER_SEC,
141                                  sn_wait_timeout, seq,
142                                  g_direct_equal,
143                                  (GDestroyNotify)sn_startup_sequence_unref);
144         change = TRUE;
145         break;
146     case SN_MONITOR_EVENT_CHANGED:
147         /* XXX feedback changed? */
148         change = TRUE;
149         break;
150     case SN_MONITOR_EVENT_COMPLETED:
151     case SN_MONITOR_EVENT_CANCELED:
152         if ((seq = sequence_find(sn_startup_sequence_get_id(seq)))) {
153             sn_waits = g_slist_remove(sn_waits, seq);
154             ob_main_loop_timeout_remove_data(ob_main_loop, sn_wait_timeout,
155                                              seq, FALSE);
156             change = TRUE;
157         }
158         break;
159     };
160
161     if (change)
162         screen_set_root_cursor();
163 }
164
165 Time sn_app_started(const gchar *id, const gchar *wmclass, const gchar *name)
166 {
167     GSList *it;
168     Time t = CurrentTime;
169
170     if (!id && !wmclass)
171         return t;
172
173     for (it = sn_waits; it; it = g_slist_next(it)) {
174         SnStartupSequence *seq = it->data;
175         gboolean found = FALSE;
176         const gchar *seqid, *seqclass, *seqname, *seqbin;
177         seqid = sn_startup_sequence_get_id(seq);
178         seqclass = sn_startup_sequence_get_wmclass(seq);
179         seqname = sn_startup_sequence_get_name(seq);
180         seqbin = sn_startup_sequence_get_binary_name(seq);
181
182         if (id && seqid) {
183             /* if the app has a startup id, then look for that for highest
184                accuracy */
185             if (!strcmp(seqid, id))
186                 found = TRUE;
187         } else {
188             seqclass = sn_startup_sequence_get_wmclass(seq);
189             seqbin = sn_startup_sequence_get_binary_name(seq);
190
191             /* seqclass = "a string to match against the "resource name" or
192                "resource class" hints.  These are WM_CLASS[0] and WM_CLASS[1]"
193                - from the startup-notification spec
194             */
195             if ((seqclass && !strcmp(seqclass, wmclass)) ||
196                 (seqclass && !strcmp(seqclass, name)) ||
197                 /* Check the binary name against the class and name hints
198                    as well, to help apps that don't have the class set
199                    correctly */
200                 (seqbin && !g_ascii_strcasecmp(seqbin, wmclass)) ||
201                 (seqbin && !g_ascii_strcasecmp(seqbin, name)))
202             {
203                 found = TRUE;
204             }
205         }
206
207         if (found) {
208             sn_startup_sequence_complete(seq);
209             t = sn_startup_sequence_get_timestamp(seq);
210             break;
211         }
212     }
213     return t;
214 }
215
216 gboolean sn_get_desktop(gchar *id, guint *desktop)
217 {
218     SnStartupSequence *seq;
219
220     if (id && (seq = sequence_find(id))) {
221         gint desk = sn_startup_sequence_get_workspace(seq);
222         if (desk != -1) {
223             *desktop = desk;
224             return TRUE;
225         }
226     }
227     return FALSE;
228 }
229
230 static gboolean sn_launch_wait_timeout(gpointer data)
231 {
232     SnLauncherContext *sn = data;
233     sn_launcher_context_complete(sn);
234     return FALSE; /* don't repeat */
235 }
236
237 void sn_setup_spawn_environment(gchar *program, gchar *name,
238                                 gchar *icon_name, gint desktop)
239 {
240     gchar *desc;
241     const char *id;
242
243     desc = g_strdup_printf(_("Running %s\n"), program);
244
245     if (sn_launcher_context_get_initiated(sn_launcher)) {
246         sn_launcher_context_unref(sn_launcher);
247         sn_launcher = sn_launcher_context_new(sn_display, ob_screen);
248     }
249
250     sn_launcher_context_set_name(sn_launcher, name ? name : program);
251     sn_launcher_context_set_description(sn_launcher, desc);
252     sn_launcher_context_set_icon_name(sn_launcher, icon_name ?
253                                       icon_name : program);
254     sn_launcher_context_set_binary_name(sn_launcher, program);
255     if (desktop >= 0 && (unsigned) desktop < screen_num_desktops)
256         sn_launcher_context_set_workspace(sn_launcher, (signed) desktop);
257     sn_launcher_context_initiate(sn_launcher, "openbox", program,
258                                  event_curtime);
259     id = sn_launcher_context_get_startup_id(sn_launcher);
260
261     /* 20 second timeout for apps to start */
262     sn_launcher_context_ref(sn_launcher);
263     ob_main_loop_timeout_add(ob_main_loop, 20 * G_USEC_PER_SEC,
264                              sn_launch_wait_timeout, sn_launcher,
265                              g_direct_equal,
266                              (GDestroyNotify)sn_launcher_context_unref);
267
268     putenv(g_strdup_printf("DESKTOP_STARTUP_ID=%s", id));
269
270     g_free(desc);
271 }
272
273 void sn_spawn_cancel(void)
274 {
275     sn_launcher_context_complete(sn_launcher);
276 }
277
278 #endif