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