Fix a typo that prevented setting client size for the height only (???)
[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 #include "obt/xqueue.h"
24
25 #ifdef HAVE_STDLIB_H
26 #  include <stdlib.h>
27 #endif
28
29 #ifndef USE_LIBSN
30
31 void sn_startup(gboolean reconfig) {}
32 void sn_shutdown(gboolean reconfig) {}
33 gboolean sn_app_starting() { return FALSE; }
34 Time sn_app_started(const gchar *id, const gchar *wmclass, const gchar *name)
35 {
36     return CurrentTime;
37 }
38 gboolean sn_get_desktop(gchar *id, guint *desktop) { return FALSE; }
39 void sn_setup_spawn_environment(const gchar *program, const gchar *name,
40                                 const gchar *icon_name, const gchar *wmclass,
41                                 gint desktop) {}
42 void sn_spawn_cancel() {}
43
44 #else
45
46 #include "openbox.h"
47 #include "screen.h"
48
49 #define SN_API_NOT_YET_FROZEN
50 #include <libsn/sn.h>
51
52 static SnDisplay *sn_display;
53 static SnMonitorContext *sn_context;
54 static SnLauncherContext *sn_launcher;
55 static GSList *sn_waits; /* list of SnStartupSequences we're waiting on */
56
57 static SnStartupSequence* sequence_find(const gchar *id);
58
59 static void sn_handler(const XEvent *e, gpointer data);
60 static void sn_event_func(SnMonitorEvent *event, gpointer data);
61
62 void sn_startup(gboolean reconfig)
63 {
64     if (reconfig) return;
65
66     sn_display = sn_display_new(obt_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     xqueue_add_callback(sn_handler, NULL);
72 }
73
74 void sn_shutdown(gboolean reconfig)
75 {
76     GSList *it;
77
78     if (reconfig) return;
79
80     xqueue_remove_callback(sn_handler, NULL);
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(void)
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         g_timeout_add_full(G_PRIORITY_DEFAULT,
144                            20 * 1000, sn_wait_timeout, seq,
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             g_source_remove_by_user_data(seq);
157             change = TRUE;
158         }
159         break;
160     };
161
162     if (change)
163         screen_set_root_cursor();
164 }
165
166 Time sn_app_started(const gchar *id, const gchar *wmclass, const gchar *name)
167 {
168     GSList *it;
169     Time t = CurrentTime;
170
171     if (!id && !wmclass)
172         return t;
173
174     for (it = sn_waits; it; it = g_slist_next(it)) {
175         SnStartupSequence *seq = it->data;
176         gboolean found = FALSE;
177         const gchar *seqid, *seqclass, *seqbin;
178         seqid = sn_startup_sequence_get_id(seq);
179         seqclass = sn_startup_sequence_get_wmclass(seq);
180         seqbin = sn_startup_sequence_get_binary_name(seq);
181
182         if (id && seqid) {
183             /* if the app has a startup id, then look for that for highest
184                accuracy */
185             if (!strcmp(seqid, id))
186                 found = TRUE;
187         }
188         else if (seqclass) {
189             /* seqclass = "a string to match against the "resource name" or
190                "resource class" hints.  These are WM_CLASS[0] and WM_CLASS[1]"
191                - from the startup-notification spec
192             */
193             found = (seqclass && !strcmp(seqclass, wmclass)) ||
194                 (seqclass && !strcmp(seqclass, name));
195         }
196         else if (seqbin) {
197             /* Check the binary name against the class and name hints
198                as well, to help apps that don't have the class set
199                correctly */
200             found = (seqbin && !g_ascii_strcasecmp(seqbin, wmclass)) ||
201                 (seqbin && !g_ascii_strcasecmp(seqbin, name));
202         }
203
204         if (found) {
205             sn_startup_sequence_complete(seq);
206             t = sn_startup_sequence_get_timestamp(seq);
207             break;
208         }
209     }
210     return t;
211 }
212
213 gboolean sn_get_desktop(gchar *id, guint *desktop)
214 {
215     SnStartupSequence *seq;
216
217     if (id && (seq = sequence_find(id))) {
218         gint desk = sn_startup_sequence_get_workspace(seq);
219         if (desk != -1) {
220             *desktop = desk;
221             return TRUE;
222         }
223     }
224     return FALSE;
225 }
226
227 static gboolean sn_launch_wait_timeout(gpointer data)
228 {
229     SnLauncherContext *sn = data;
230     sn_launcher_context_complete(sn);
231     return FALSE; /* don't repeat */
232 }
233
234 void sn_setup_spawn_environment(const gchar *program, const gchar *name,
235                                 const gchar *icon_name, const gchar *wmclass,
236                                 gint desktop)
237 {
238     gchar *desc;
239     const char *id;
240
241     desc = g_strdup_printf(_("Running %s"), 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 (wmclass) sn_launcher_context_set_wmclass(sn_launcher, wmclass);
254     if (desktop >= 0 && (unsigned) desktop < screen_num_desktops)
255         sn_launcher_context_set_workspace(sn_launcher, (signed) desktop);
256     sn_launcher_context_initiate(sn_launcher, "openbox", program,
257                                  event_time());
258     id = sn_launcher_context_get_startup_id(sn_launcher);
259
260     /* 20 second timeout for apps to start */
261     sn_launcher_context_ref(sn_launcher);
262     g_timeout_add_full(G_PRIORITY_DEFAULT,
263                        20 * 1000, sn_launch_wait_timeout, sn_launcher,
264                        (GDestroyNotify)sn_launcher_context_unref);
265
266     g_setenv("DESKTOP_STARTUP_ID", id, TRUE);
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