]> icculus.org git repositories - dana/openbox.git/blob - openbox/startupnotify.c
the old event code
[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) 2003        Ben Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "startupnotify.h"
20
21 #ifndef USE_LIBSN
22
23 void sn_startup(gboolean reconfig) {}
24 void sn_shutdown(gboolean reconfig) {}
25 gboolean sn_app_starting() { return FALSE; }
26 void sn_app_started(gchar *wmclass) {}
27 gboolean sn_get_desktop(gchar *id, guint *desktop) { return FALSE; }
28
29 #else
30
31 #include "openbox.h"
32 #include "mainloop.h"
33 #include "screen.h"
34
35 #define SN_API_NOT_YET_FROZEN
36 #include <libsn/sn.h>
37
38 typedef struct {
39     SnStartupSequence *seq;
40     gboolean feedback;
41 } ObWaitData;
42
43 static SnDisplay *sn_display;
44 static SnMonitorContext *sn_context;
45 static GSList *sn_waits; /* list of ObWaitDatas */
46
47 static ObWaitData* wait_data_new(SnStartupSequence *seq);
48 static void wait_data_free(ObWaitData *d);
49 static ObWaitData* wait_find(const gchar *id);
50
51 static void sn_handler(const XEvent *e, gpointer data);
52 static void sn_event_func(SnMonitorEvent *event, gpointer data);
53
54 void sn_startup(gboolean reconfig)
55 {
56     if (reconfig) return;
57
58     sn_display = sn_display_new(ob_display, NULL, NULL);
59     sn_context = sn_monitor_context_new(sn_display, ob_screen,
60                                         sn_event_func, NULL, NULL);
61
62     ob_main_loop_x_add(ob_main_loop, sn_handler, NULL, NULL);
63 }
64
65 void sn_shutdown(gboolean reconfig)
66 {
67     GSList *it;
68
69     if (reconfig) return;
70
71     ob_main_loop_x_remove(ob_main_loop, sn_handler);
72
73     for (it = sn_waits; it; it = g_slist_next(it))
74         wait_data_free(it->data);
75     g_slist_free(sn_waits);
76     sn_waits = NULL;
77
78     screen_set_root_cursor();
79
80     sn_monitor_context_unref(sn_context);
81     sn_display_unref(sn_display);
82 }
83
84 static ObWaitData* wait_data_new(SnStartupSequence *seq)
85 {
86     ObWaitData *d = g_new(ObWaitData, 1);
87     d->seq = seq;
88     d->feedback = TRUE;
89
90     sn_startup_sequence_ref(d->seq);
91
92     return d;
93 }
94
95 static void wait_data_free(ObWaitData *d)
96 {
97     if (d) {
98         sn_startup_sequence_unref(d->seq);
99
100         g_free(d);
101     }
102 }
103
104 static ObWaitData* wait_find(const gchar *id)
105 {
106     ObWaitData *ret = NULL;
107     GSList *it;
108
109     for (it = sn_waits; it; it = g_slist_next(it)) {
110         ObWaitData *d = it->data;
111         if (!strcmp(id, sn_startup_sequence_get_id(d->seq))) {
112             ret = d;
113             break;
114         }
115     }
116     return ret;
117 }
118
119 gboolean sn_app_starting()
120 {
121     GSList *it;
122
123     for (it = sn_waits; it; it = g_slist_next(it)) {
124         ObWaitData *d = it->data;
125         if (d->feedback)
126             return TRUE;
127     }
128     return FALSE;
129 }
130
131 static gboolean sn_wait_timeout(gpointer data)
132 {
133     ObWaitData *d = data;
134     d->feedback = FALSE;
135     screen_set_root_cursor();
136     return FALSE; /* don't repeat */
137 }
138
139 static void sn_wait_destroy(gpointer data)
140 {
141     ObWaitData *d = data;
142     sn_waits = g_slist_remove(sn_waits, d);
143     wait_data_free(d);
144 }
145
146 static void sn_handler(const XEvent *e, gpointer data)
147 {
148     XEvent ec;
149     ec = *e;
150     sn_display_process_event(sn_display, &ec);
151 }
152
153 static void sn_event_func(SnMonitorEvent *ev, gpointer data)
154 {
155     SnStartupSequence *seq;
156     gboolean change = FALSE;
157     ObWaitData *d;
158
159     if (!(seq = sn_monitor_event_get_startup_sequence(ev)))
160         return;
161
162     switch (sn_monitor_event_get_type(ev)) {
163     case SN_MONITOR_EVENT_INITIATED:
164         d = wait_data_new(seq);
165         sn_waits = g_slist_prepend(sn_waits, d);
166         /* 30 second timeout for apps to start */
167         ob_main_loop_timeout_add(ob_main_loop, 30 * G_USEC_PER_SEC,
168                                  sn_wait_timeout, d, sn_wait_destroy);
169         change = TRUE;
170         break;
171     case SN_MONITOR_EVENT_CHANGED:
172         /* XXX feedback changed? */
173         change = TRUE;
174         break;
175     case SN_MONITOR_EVENT_COMPLETED:
176     case SN_MONITOR_EVENT_CANCELED:
177         if ((d = wait_find(sn_startup_sequence_get_id(seq)))) {
178             d->feedback = FALSE;
179             ob_main_loop_timeout_remove_data(ob_main_loop, sn_wait_timeout, d);
180             change = TRUE;
181         }
182         break;
183     };
184
185     if (change)
186         screen_set_root_cursor();
187 }
188
189 void sn_app_started(gchar *wmclass)
190 {
191     GSList *it;
192
193     for (it = sn_waits; it; it = g_slist_next(it)) {
194         ObWaitData *d = it->data;
195         if (sn_startup_sequence_get_wmclass(d->seq) &&
196             !strcmp(sn_startup_sequence_get_wmclass(d->seq), wmclass))
197         {
198             sn_startup_sequence_complete(d->seq);
199             break;
200         }
201     }
202 }
203
204 gboolean sn_get_desktop(gchar *id, guint *desktop)
205 {
206     ObWaitData *d;
207
208     if (id && (d = wait_find(id))) {
209         gint desk = sn_startup_sequence_get_workspace(d->seq);
210         if (desk != -1) {
211             *desktop = desk;
212             return TRUE;
213         }
214     }
215     return FALSE;
216 }
217
218 #endif