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