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