]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/startupnotify.c
maybe the new actions framework is kinda there now
[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
23 #include <stdlib.h>
24
25 #ifndef USE_LIBSN
26
27 void sn_startup(gboolean reconfig) {}
28 void sn_shutdown(gboolean reconfig) {}
29 gboolean sn_app_starting() { return FALSE; }
30 Time sn_app_started(const gchar *id, const gchar *wmclass)
31 {
32     return CurrentTime;
33 }
34 gboolean sn_get_desktop(gchar *id, guint *desktop) { return FALSE; }
35 void sn_setup_spawn_environment(gchar *program, gchar *name,
36                                 gchar *icon_name, gint desktop, Time time) {}
37 void sn_spawn_cancel() {}
38
39 #else
40
41 #include "openbox.h"
42 #include "mainloop.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     if (reconfig) return;
61
62     /* unset this so we don't pass it on unknowingly */
63     unsetenv("DESKTOP_STARTUP_ID");
64
65     sn_display = sn_display_new(ob_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);
69
70     ob_main_loop_x_add(ob_main_loop, sn_handler, NULL, NULL);
71 }
72
73 void sn_shutdown(gboolean reconfig)
74 {
75     GSList *it;
76
77     if (reconfig) return;
78
79     ob_main_loop_x_remove(ob_main_loop, sn_handler);
80
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;
85
86     screen_set_root_cursor();
87
88     sn_launcher_context_unref(sn_launcher);
89     sn_monitor_context_unref(sn_context);
90     sn_display_unref(sn_display);
91 }
92
93 static SnStartupSequence* sequence_find(const gchar *id)
94 {
95     SnStartupSequence*ret = NULL;
96     GSList *it;
97
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;
103         }
104     }
105     return ret;
106 }
107
108 gboolean sn_app_starting()
109 {
110     return sn_waits != NULL;
111 }
112
113 static gboolean sn_wait_timeout(gpointer data)
114 {
115     SnStartupSequence *seq = data;
116     sn_waits = g_slist_remove(sn_waits, seq);
117     screen_set_root_cursor();
118     return FALSE; /* don't repeat */
119 }
120
121 static void sn_handler(const XEvent *e, gpointer data)
122 {
123     XEvent ec;
124     ec = *e;
125     sn_display_process_event(sn_display, &ec);
126 }
127
128 static void sn_event_func(SnMonitorEvent *ev, gpointer data)
129 {
130     SnStartupSequence *seq;
131     gboolean change = FALSE;
132
133     if (!(seq = sn_monitor_event_get_startup_sequence(ev)))
134         return;
135
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         ob_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             ob_main_loop_timeout_remove_data(ob_main_loop, sn_wait_timeout,
157                                              seq, FALSE);
158             change = TRUE;
159         }
160         break;
161     };
162
163     if (change)
164         screen_set_root_cursor();
165 }
166
167 Time sn_app_started(const gchar *id, const gchar *wmclass)
168 {
169     GSList *it;
170     Time t = CurrentTime;
171
172     if (!id && !wmclass)
173         return t;
174
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, *seqname, *seqbin;
179         seqid = sn_startup_sequence_get_id(seq);
180         seqclass = sn_startup_sequence_get_wmclass(seq);
181         seqname = sn_startup_sequence_get_name(seq);
182         seqbin = sn_startup_sequence_get_binary_name(seq);
183
184         if (id && seqid) {
185             /* if the app has a startup id, then look for that for highest
186                accuracy */
187             if (!strcmp(seqid, id))
188                 found = TRUE;
189         } else {
190             seqclass = sn_startup_sequence_get_wmclass(seq);
191             seqname = sn_startup_sequence_get_name(seq);
192             seqbin = sn_startup_sequence_get_binary_name(seq);
193
194             if ((seqname && !g_ascii_strcasecmp(seqname, wmclass)) ||
195                 (seqbin && !g_ascii_strcasecmp(seqbin, wmclass)) ||
196                 (seqclass && !strcmp(seqclass, wmclass)))
197                 found = TRUE;
198         }
199
200         if (found) {
201             sn_startup_sequence_complete(seq);
202             t = sn_startup_sequence_get_timestamp(seq);
203             break;
204         }
205     }
206     return t;
207 }
208
209 gboolean sn_get_desktop(gchar *id, guint *desktop)
210 {
211     SnStartupSequence *seq;
212
213     if (id && (seq = sequence_find(id))) {
214         gint desk = sn_startup_sequence_get_workspace(seq);
215         if (desk != -1) {
216             *desktop = desk;
217             return TRUE;
218         }
219     }
220     return FALSE;
221 }
222
223 static gboolean sn_launch_wait_timeout(gpointer data)
224 {
225     SnLauncherContext *sn = data;
226     sn_launcher_context_complete(sn);
227     return FALSE; /* don't repeat */
228 }
229
230 void sn_setup_spawn_environment(gchar *program, gchar *name,
231                                 gchar *icon_name, gint desktop,
232                                 Time time)
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 ? icon_name : program);
247     sn_launcher_context_set_binary_name(sn_launcher, program);
248     if (desktop >= 0 && (unsigned) desktop < screen_num_desktops)
249         sn_launcher_context_set_workspace(sn_launcher, (signed) desktop);
250     sn_launcher_context_initiate(sn_launcher, "openbox", program, time);
251     id = sn_launcher_context_get_startup_id(sn_launcher);
252
253     /* 20 second timeout for apps to start */
254     sn_launcher_context_ref(sn_launcher);
255     ob_main_loop_timeout_add(ob_main_loop, 20 * G_USEC_PER_SEC,
256                              sn_launch_wait_timeout, sn_launcher,
257                              g_direct_equal,
258                              (GDestroyNotify)sn_launcher_context_unref);
259
260     setenv("DESKTOP_STARTUP_ID", id, TRUE);
261
262     g_free(desc);
263 }
264
265 void sn_spawn_cancel()
266 {
267     sn_launcher_context_complete(sn_launcher);
268 }
269
270 #endif