]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/startupnotify.c
make startup notification a little more robust. obconf is just broken, between
[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
22 #ifndef USE_LIBSN
23
24 void sn_startup(gboolean reconfig) {}
25 void sn_shutdown(gboolean reconfig) {}
26 gboolean sn_app_starting() { return FALSE; }
27 void sn_app_started(gchar *wmclass) {}
28 gboolean sn_get_desktop(gchar *id, guint *desktop) { return FALSE; }
29
30 #else
31
32 #include "openbox.h"
33 #include "mainloop.h"
34 #include "screen.h"
35
36 #define SN_API_NOT_YET_FROZEN
37 #include <libsn/sn.h>
38
39 typedef struct {
40     SnStartupSequence *seq;
41     gboolean feedback;
42 } ObWaitData;
43
44 static SnDisplay *sn_display;
45 static SnMonitorContext *sn_context;
46 static GSList *sn_waits; /* list of ObWaitDatas */
47
48 static ObWaitData* wait_data_new(SnStartupSequence *seq);
49 static void wait_data_free(ObWaitData *d);
50 static ObWaitData* wait_find(const gchar *id);
51
52 static void sn_handler(const XEvent *e, gpointer data);
53 static void sn_event_func(SnMonitorEvent *event, gpointer data);
54
55 void sn_startup(gboolean reconfig)
56 {
57     if (reconfig) return;
58
59     sn_display = sn_display_new(ob_display, NULL, NULL);
60     sn_context = sn_monitor_context_new(sn_display, ob_screen,
61                                         sn_event_func, NULL, NULL);
62
63     ob_main_loop_x_add(ob_main_loop, sn_handler, NULL, NULL);
64 }
65
66 void sn_shutdown(gboolean reconfig)
67 {
68     GSList *it;
69
70     if (reconfig) return;
71
72     ob_main_loop_x_remove(ob_main_loop, sn_handler);
73
74     for (it = sn_waits; it; it = g_slist_next(it))
75         wait_data_free(it->data);
76     g_slist_free(sn_waits);
77     sn_waits = NULL;
78
79     screen_set_root_cursor();
80
81     sn_monitor_context_unref(sn_context);
82     sn_display_unref(sn_display);
83 }
84
85 static ObWaitData* wait_data_new(SnStartupSequence *seq)
86 {
87     ObWaitData *d = g_new(ObWaitData, 1);
88     d->seq = seq;
89     d->feedback = TRUE;
90
91     sn_startup_sequence_ref(d->seq);
92
93     return d;
94 }
95
96 static void wait_data_free(ObWaitData *d)
97 {
98     if (d) {
99         sn_startup_sequence_unref(d->seq);
100
101         g_free(d);
102     }
103 }
104
105 static ObWaitData* wait_find(const gchar *id)
106 {
107     ObWaitData *ret = NULL;
108     GSList *it;
109
110     for (it = sn_waits; it; it = g_slist_next(it)) {
111         ObWaitData *d = it->data;
112         if (!strcmp(id, sn_startup_sequence_get_id(d->seq))) {
113             ret = d;
114             break;
115         }
116     }
117     return ret;
118 }
119
120 gboolean sn_app_starting()
121 {
122     GSList *it;
123
124     for (it = sn_waits; it; it = g_slist_next(it)) {
125         ObWaitData *d = it->data;
126         if (d->feedback)
127             return TRUE;
128     }
129     return FALSE;
130 }
131
132 static gboolean sn_wait_timeout(gpointer data)
133 {
134     ObWaitData *d = data;
135     d->feedback = FALSE;
136     screen_set_root_cursor();
137     return FALSE; /* don't repeat */
138 }
139
140 static void sn_wait_destroy(gpointer data)
141 {
142     ObWaitData *d = data;
143     sn_waits = g_slist_remove(sn_waits, d);
144     wait_data_free(d);
145 }
146
147 static void sn_handler(const XEvent *e, gpointer data)
148 {
149     XEvent ec;
150     ec = *e;
151     sn_display_process_event(sn_display, &ec);
152 }
153
154 static void sn_event_func(SnMonitorEvent *ev, gpointer data)
155 {
156     SnStartupSequence *seq;
157     gboolean change = FALSE;
158     ObWaitData *d;
159
160     if (!(seq = sn_monitor_event_get_startup_sequence(ev)))
161         return;
162
163     switch (sn_monitor_event_get_type(ev)) {
164     case SN_MONITOR_EVENT_INITIATED:
165         d = wait_data_new(seq);
166         sn_waits = g_slist_prepend(sn_waits, d);
167         /* 15 second timeout for apps to start */
168         ob_main_loop_timeout_add(ob_main_loop, 15 * G_USEC_PER_SEC,
169                                  sn_wait_timeout, d, sn_wait_destroy);
170         change = TRUE;
171         break;
172     case SN_MONITOR_EVENT_CHANGED:
173         /* XXX feedback changed? */
174         change = TRUE;
175         break;
176     case SN_MONITOR_EVENT_COMPLETED:
177     case SN_MONITOR_EVENT_CANCELED:
178         if ((d = wait_find(sn_startup_sequence_get_id(seq)))) {
179             d->feedback = FALSE;
180             ob_main_loop_timeout_remove_data(ob_main_loop, sn_wait_timeout,
181                                              d, FALSE);
182             change = TRUE;
183         }
184         break;
185     };
186
187     if (change)
188         screen_set_root_cursor();
189 }
190
191 void sn_app_started(const gchar *id, const gchar *wmclass)
192 {
193     GSList *it;
194
195     for (it = sn_waits; it; it = g_slist_next(it)) {
196         ObWaitData *d = it->data;
197         const gchar *seqid, *seqclass;
198         seqid = sn_startup_sequence_get_id(d->seq);
199         seqclass = sn_startup_sequence_get_wmclass(d->seq);
200         if ((seqid && id && !strcmp(seqid, id)) ||
201             (seqclass && wmclass && !strcmp(seqclass, wmclass)))
202         {
203             sn_startup_sequence_complete(d->seq);
204             break;
205         }
206     }
207 }
208
209 gboolean sn_get_desktop(gchar *id, guint *desktop)
210 {
211     ObWaitData *d;
212
213     if (id && (d = wait_find(id))) {
214         gint desk = sn_startup_sequence_get_workspace(d->seq);
215         if (desk != -1) {
216             *desktop = desk;
217             return TRUE;
218         }
219     }
220     return FALSE;
221 }
222
223 #endif