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