]> Dogcows Code - chaz/openbox/blob - openbox/startupnotify.c
look for more things to cancel startup notification on, from the wmclass. if the...
[chaz/openbox] / 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 Ben 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
23 extern gchar **environ;
24
25 #ifndef USE_LIBSN
26
27 void sn_startup(gboolean reconfig) {}
28 void sn_shutdown(gboolean reconfig) {}
29 gboolean sn_app_starting() { return FALSE; }
30 Time sn_app_started(const gchar *id, const gchar *wmclass)
31 {
32 return CurrentTime;
33 }
34 gboolean sn_get_desktop(gchar *id, guint *desktop) { return FALSE; }
35 gchar **sn_get_spawn_environment(char *program, Time time)
36 {
37 return g_strdupv(environ);
38 }
39 void sn_spawn_cancel() {}
40
41 #else
42
43 #include "openbox.h"
44 #include "mainloop.h"
45 #include "screen.h"
46
47 #define SN_API_NOT_YET_FROZEN
48 #include <libsn/sn.h>
49
50 static SnDisplay *sn_display;
51 static SnMonitorContext *sn_context;
52 static SnLauncherContext *sn_launcher;
53 static GSList *sn_waits; /* list of SnStartupSequences we're waiting on */
54
55 static SnStartupSequence* sequence_find(const gchar *id);
56
57 static void sn_handler(const XEvent *e, gpointer data);
58 static void sn_event_func(SnMonitorEvent *event, gpointer data);
59
60 void sn_startup(gboolean reconfig)
61 {
62 if (reconfig) return;
63
64 sn_display = sn_display_new(ob_display, NULL, NULL);
65 sn_context = sn_monitor_context_new(sn_display, ob_screen,
66 sn_event_func, NULL, NULL);
67 sn_launcher = sn_launcher_context_new(sn_display, ob_screen);
68
69 ob_main_loop_x_add(ob_main_loop, sn_handler, NULL, NULL);
70 }
71
72 void sn_shutdown(gboolean reconfig)
73 {
74 GSList *it;
75
76 if (reconfig) return;
77
78 ob_main_loop_x_remove(ob_main_loop, sn_handler);
79
80 for (it = sn_waits; it; it = g_slist_next(it))
81 sn_startup_sequence_unref((SnStartupSequence*)it->data);
82 g_slist_free(sn_waits);
83 sn_waits = NULL;
84
85 screen_set_root_cursor();
86
87 sn_launcher_context_unref(sn_launcher);
88 sn_monitor_context_unref(sn_context);
89 sn_display_unref(sn_display);
90 }
91
92 static SnStartupSequence* sequence_find(const gchar *id)
93 {
94 SnStartupSequence*ret = NULL;
95 GSList *it;
96
97 for (it = sn_waits; it; it = g_slist_next(it)) {
98 SnStartupSequence *seq = it->data;
99 if (!strcmp(id, sn_startup_sequence_get_id(seq))) {
100 ret = seq;
101 break;
102 }
103 }
104 return ret;
105 }
106
107 gboolean sn_app_starting()
108 {
109 return sn_waits != NULL;
110 }
111
112 static gboolean sn_wait_timeout(gpointer data)
113 {
114 SnStartupSequence *seq = data;
115 sn_waits = g_slist_remove(sn_waits, seq);
116 screen_set_root_cursor();
117 return FALSE; /* don't repeat */
118 }
119
120 static void sn_handler(const XEvent *e, gpointer data)
121 {
122 XEvent ec;
123 ec = *e;
124 sn_display_process_event(sn_display, &ec);
125 }
126
127 static void sn_event_func(SnMonitorEvent *ev, gpointer data)
128 {
129 SnStartupSequence *seq;
130 gboolean change = FALSE;
131
132 if (!(seq = sn_monitor_event_get_startup_sequence(ev)))
133 return;
134
135 switch (sn_monitor_event_get_type(ev)) {
136 case SN_MONITOR_EVENT_INITIATED:
137 sn_startup_sequence_ref(seq);
138 sn_waits = g_slist_prepend(sn_waits, seq);
139 /* 30 second timeout for apps to start if the launcher doesn't
140 have a timeout */
141 ob_main_loop_timeout_add(ob_main_loop, 30 * G_USEC_PER_SEC,
142 sn_wait_timeout, seq,
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)
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 seqname = sn_startup_sequence_get_name(seq);
190 seqbin = sn_startup_sequence_get_binary_name(seq);
191
192 if ((seqname && !g_ascii_strcasecmp(seqname, wmclass)) ||
193 (seqbin && !g_ascii_strcasecmp(seqbin, wmclass)) ||
194 (seqclass && !strcmp(seqclass, wmclass)))
195 found = TRUE;
196 }
197
198 if (found) {
199 sn_startup_sequence_complete(seq);
200 t = sn_startup_sequence_get_timestamp(seq);
201 break;
202 }
203 }
204 return t;
205 }
206
207 gboolean sn_get_desktop(gchar *id, guint *desktop)
208 {
209 SnStartupSequence *seq;
210
211 if (id && (seq = sequence_find(id))) {
212 gint desk = sn_startup_sequence_get_workspace(seq);
213 if (desk != -1) {
214 *desktop = desk;
215 return TRUE;
216 }
217 }
218 return FALSE;
219 }
220
221 static gboolean sn_launch_wait_timeout(gpointer data)
222 {
223 SnLauncherContext *sn = data;
224 sn_launcher_context_complete(sn);
225 return FALSE; /* don't repeat */
226 }
227
228 gchar **sn_get_spawn_environment(char *program, Time time)
229 {
230 gchar **env, *desc;
231 guint len;
232 const char *id;
233
234 desc = g_strdup_printf(_("Running %s\n"), program);
235
236 if (sn_launcher_context_get_initiated(sn_launcher)) {
237 sn_launcher_context_unref(sn_launcher);
238 sn_launcher = sn_launcher_context_new(sn_display, ob_screen);
239 }
240
241 sn_launcher_context_set_name(sn_launcher, program);
242 sn_launcher_context_set_description(sn_launcher, desc);
243 sn_launcher_context_set_icon_name(sn_launcher, program);
244 sn_launcher_context_set_binary_name(sn_launcher, program);
245 sn_launcher_context_initiate(sn_launcher, "openbox", program, time);
246 id = sn_launcher_context_get_startup_id(sn_launcher);
247
248 /* 30 second timeout for apps to start */
249 sn_launcher_context_ref(sn_launcher);
250 ob_main_loop_timeout_add(ob_main_loop, 30 * G_USEC_PER_SEC,
251 sn_launch_wait_timeout, sn_launcher,
252 (GDestroyNotify)sn_launcher_context_unref);
253
254 env = g_strdupv(environ);
255 len = g_strv_length(env); /* includes last null */
256 env = g_renew(gchar*, env, ++len); /* add one spot */
257 env[len-2] = g_strdup_printf("DESKTOP_STARTUP_ID=%s", id);
258 env[len-1] = NULL;
259
260 g_free(desc);
261
262 return env;
263 }
264
265 void sn_spawn_cancel()
266 {
267 sn_launcher_context_complete(sn_launcher);
268 }
269
270 #endif
This page took 0.048471 seconds and 5 git commands to generate.