66bce264c13a68db853e53cacfbbb2e022bdd46b
[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)
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)
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             seqname = sn_startup_sequence_get_name(seq);
197             seqbin = sn_startup_sequence_get_binary_name(seq);
198
199             if ((seqname && !g_ascii_strcasecmp(seqname, wmclass)) ||
200                 (seqbin && !g_ascii_strcasecmp(seqbin, wmclass)) ||
201                 (seqclass && !strcmp(seqclass, wmclass)))
202                 found = TRUE;
203         }
204
205         if (found) {
206             sn_startup_sequence_complete(seq);
207             t = sn_startup_sequence_get_timestamp(seq);
208             break;
209         }
210     }
211     return t;
212 }
213
214 gboolean sn_get_desktop(gchar *id, guint *desktop)
215 {
216     SnStartupSequence *seq;
217
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;
223         }
224     }
225     return FALSE;
226 }
227
228 static gboolean sn_launch_wait_timeout(gpointer data)
229 {
230     SnLauncherContext *sn = data;
231     sn_launcher_context_complete(sn);
232     return FALSE; /* don't repeat */
233 }
234
235 void sn_setup_spawn_environment(gchar *program, gchar *name,
236                                 gchar *icon_name, gint desktop)
237 {
238     gchar *desc;
239     const char *id;
240
241     desc = g_strdup_printf(_("Running %s\n"), program);
242
243     if (sn_launcher_context_get_initiated(sn_launcher)) {
244         sn_launcher_context_unref(sn_launcher);
245         sn_launcher = sn_launcher_context_new(sn_display, ob_screen);
246     }
247
248     sn_launcher_context_set_name(sn_launcher, name ? name : program);
249     sn_launcher_context_set_description(sn_launcher, desc);
250     sn_launcher_context_set_icon_name(sn_launcher, icon_name ?
251                                       icon_name : program);
252     sn_launcher_context_set_binary_name(sn_launcher, program);
253     if (desktop >= 0 && (unsigned) desktop < screen_num_desktops)
254         sn_launcher_context_set_workspace(sn_launcher, (signed) desktop);
255     sn_launcher_context_initiate(sn_launcher, "openbox", program,
256                                  event_curtime);
257     id = sn_launcher_context_get_startup_id(sn_launcher);
258
259     /* 20 second timeout for apps to start */
260     sn_launcher_context_ref(sn_launcher);
261     ob_main_loop_timeout_add(ob_main_loop, 20 * G_USEC_PER_SEC,
262                              sn_launch_wait_timeout, sn_launcher,
263                              g_direct_equal,
264                              (GDestroyNotify)sn_launcher_context_unref);
265
266     putenv(g_strdup_printf("DESKTOP_STARTUP_ID=%s", id));
267
268     g_free(desc);
269 }
270
271 void sn_spawn_cancel(void)
272 {
273     sn_launcher_context_complete(sn_launcher);
274 }
275
276 #endif