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