Merge branch 'backport' into work
[openbox.git] / openbox / startupnotify.c
blob3e8799f8dd9ea7f4a9bc4b0e5d6fda66cdca2e3e
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 startupnotify.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
20 #include "startupnotify.h"
21 #include "gettext.h"
22 #include "event.h"
24 #ifdef HAVE_STDLIB_H
25 # include <stdlib.h>
26 #endif
28 #ifndef USE_LIBSN
30 void sn_startup(gboolean reconfig) {}
31 void sn_shutdown(gboolean reconfig) {}
32 gboolean sn_app_starting() { return FALSE; }
33 Time sn_app_started(const gchar *id, const gchar *wmclass, const gchar *name)
35 return CurrentTime;
37 gboolean sn_get_desktop(gchar *id, guint *desktop) { return FALSE; }
38 void sn_setup_spawn_environment(const gchar *program, const gchar *name,
39 const gchar *icon_name, const gchar *wmclass,
40 gint desktop) {}
41 void sn_spawn_cancel() {}
43 #else
45 #include "openbox.h"
46 #include "screen.h"
48 #define SN_API_NOT_YET_FROZEN
49 #include <libsn/sn.h>
51 static SnDisplay *sn_display;
52 static SnMonitorContext *sn_context;
53 static SnLauncherContext *sn_launcher;
54 static GSList *sn_waits; /* list of SnStartupSequences we're waiting on */
56 static SnStartupSequence* sequence_find(const gchar *id);
58 static void sn_handler(const XEvent *e, gpointer data);
59 static void sn_event_func(SnMonitorEvent *event, gpointer data);
61 void sn_startup(gboolean reconfig)
63 if (reconfig) return;
65 sn_display = sn_display_new(obt_display, NULL, NULL);
66 sn_context = sn_monitor_context_new(sn_display, ob_screen,
67 sn_event_func, NULL, NULL);
68 sn_launcher = sn_launcher_context_new(sn_display, ob_screen);
70 obt_main_loop_x_add(ob_main_loop, sn_handler, NULL, NULL);
73 void sn_shutdown(gboolean reconfig)
75 GSList *it;
77 if (reconfig) return;
79 obt_main_loop_x_remove(ob_main_loop, sn_handler);
81 for (it = sn_waits; it; it = g_slist_next(it))
82 sn_startup_sequence_unref((SnStartupSequence*)it->data);
83 g_slist_free(sn_waits);
84 sn_waits = NULL;
86 screen_set_root_cursor();
88 sn_launcher_context_unref(sn_launcher);
89 sn_monitor_context_unref(sn_context);
90 sn_display_unref(sn_display);
93 static SnStartupSequence* sequence_find(const gchar *id)
95 SnStartupSequence*ret = NULL;
96 GSList *it;
98 for (it = sn_waits; it; it = g_slist_next(it)) {
99 SnStartupSequence *seq = it->data;
100 if (!strcmp(id, sn_startup_sequence_get_id(seq))) {
101 ret = seq;
102 break;
105 return ret;
108 gboolean sn_app_starting(void)
110 return sn_waits != NULL;
113 static gboolean sn_wait_timeout(gpointer data)
115 SnStartupSequence *seq = data;
116 sn_waits = g_slist_remove(sn_waits, seq);
117 screen_set_root_cursor();
118 return FALSE; /* don't repeat */
121 static void sn_handler(const XEvent *e, gpointer data)
123 XEvent ec;
124 ec = *e;
125 sn_display_process_event(sn_display, &ec);
128 static void sn_event_func(SnMonitorEvent *ev, gpointer data)
130 SnStartupSequence *seq;
131 gboolean change = FALSE;
133 if (!(seq = sn_monitor_event_get_startup_sequence(ev)))
134 return;
136 switch (sn_monitor_event_get_type(ev)) {
137 case SN_MONITOR_EVENT_INITIATED:
138 sn_startup_sequence_ref(seq);
139 sn_waits = g_slist_prepend(sn_waits, seq);
140 /* 20 second timeout for apps to start if the launcher doesn't
141 have a timeout */
142 obt_main_loop_timeout_add(ob_main_loop, 20 * G_USEC_PER_SEC,
143 sn_wait_timeout, seq,
144 g_direct_equal,
145 (GDestroyNotify)sn_startup_sequence_unref);
146 change = TRUE;
147 break;
148 case SN_MONITOR_EVENT_CHANGED:
149 /* XXX feedback changed? */
150 change = TRUE;
151 break;
152 case SN_MONITOR_EVENT_COMPLETED:
153 case SN_MONITOR_EVENT_CANCELED:
154 if ((seq = sequence_find(sn_startup_sequence_get_id(seq)))) {
155 sn_waits = g_slist_remove(sn_waits, seq);
156 obt_main_loop_timeout_remove_data(ob_main_loop, sn_wait_timeout,
157 seq, FALSE);
158 change = TRUE;
160 break;
163 if (change)
164 screen_set_root_cursor();
167 Time sn_app_started(const gchar *id, const gchar *wmclass, const gchar *name)
169 GSList *it;
170 Time t = CurrentTime;
172 if (!id && !wmclass)
173 return t;
175 for (it = sn_waits; it; it = g_slist_next(it)) {
176 SnStartupSequence *seq = it->data;
177 gboolean found = FALSE;
178 const gchar *seqid, *seqclass, *seqbin;
179 seqid = sn_startup_sequence_get_id(seq);
180 seqclass = sn_startup_sequence_get_wmclass(seq);
181 seqbin = sn_startup_sequence_get_binary_name(seq);
183 if (id && seqid) {
184 /* if the app has a startup id, then look for that for highest
185 accuracy */
186 if (!strcmp(seqid, id))
187 found = TRUE;
189 else if (seqclass) {
190 /* seqclass = "a string to match against the "resource name" or
191 "resource class" hints. These are WM_CLASS[0] and WM_CLASS[1]"
192 - from the startup-notification spec
194 found = (seqclass && !strcmp(seqclass, wmclass)) ||
195 (seqclass && !strcmp(seqclass, name));
197 else if (seqbin) {
198 /* Check the binary name against the class and name hints
199 as well, to help apps that don't have the class set
200 correctly */
201 found = (seqbin && !g_ascii_strcasecmp(seqbin, wmclass)) ||
202 (seqbin && !g_ascii_strcasecmp(seqbin, name));
205 if (found) {
206 sn_startup_sequence_complete(seq);
207 t = sn_startup_sequence_get_timestamp(seq);
208 break;
211 return t;
214 gboolean sn_get_desktop(gchar *id, guint *desktop)
216 SnStartupSequence *seq;
218 if (id && (seq = sequence_find(id))) {
219 gint desk = sn_startup_sequence_get_workspace(seq);
220 if (desk != -1) {
221 *desktop = desk;
222 return TRUE;
225 return FALSE;
228 static gboolean sn_launch_wait_timeout(gpointer data)
230 SnLauncherContext *sn = data;
231 sn_launcher_context_complete(sn);
232 return FALSE; /* don't repeat */
235 void sn_setup_spawn_environment(const gchar *program, const gchar *name,
236 const gchar *icon_name, const gchar *wmclass,
237 gint desktop)
239 gchar *desc;
240 const char *id;
242 desc = g_strdup_printf(_("Running %s\n"), program);
244 if (sn_launcher_context_get_initiated(sn_launcher)) {
245 sn_launcher_context_unref(sn_launcher);
246 sn_launcher = sn_launcher_context_new(sn_display, ob_screen);
249 sn_launcher_context_set_name(sn_launcher, name ? name : program);
250 sn_launcher_context_set_description(sn_launcher, desc);
251 sn_launcher_context_set_icon_name(sn_launcher, icon_name ?
252 icon_name : program);
253 sn_launcher_context_set_binary_name(sn_launcher, program);
254 if (wmclass) sn_launcher_context_set_wmclass(sn_launcher, wmclass);
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);
261 /* 20 second timeout for apps to start */
262 sn_launcher_context_ref(sn_launcher);
263 obt_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);
268 setenv("DESKTOP_STARTUP_ID", id, TRUE);
270 g_free(desc);
273 void sn_spawn_cancel(void)
275 sn_launcher_context_complete(sn_launcher);
278 #endif