]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/startupnotify.c
remove the x event handler
[mikachu/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         g_message("starting");
147         d = wait_data_new(seq);
148         sn_waits = g_slist_prepend(sn_waits, d);
149         /* 30 second timeout for apps to start */
150         ob_main_loop_timeout_add(ob_main_loop, 30 * G_USEC_PER_SEC,
151                                  sn_wait_timeout, d, sn_wait_destroy);
152         change = TRUE;
153         break;
154     case SN_MONITOR_EVENT_CHANGED:
155         /* XXX feedback changed? */
156         change = TRUE;
157         break;
158     case SN_MONITOR_EVENT_COMPLETED:
159     case SN_MONITOR_EVENT_CANCELED:
160         if ((d = wait_find(sn_startup_sequence_get_id(seq)))) {
161             d->feedback = FALSE;
162             ob_main_loop_timeout_remove_data(ob_main_loop, sn_wait_timeout, d);
163             change = TRUE;
164         }
165         break;
166     };
167
168     if (change)
169         screen_set_root_cursor();
170 }
171
172 void sn_app_started(gchar *wmclass)
173 {
174     GSList *it;
175
176     for (it = sn_waits; it; it = g_slist_next(it)) {
177         ObWaitData *d = it->data;
178         if (sn_startup_sequence_get_wmclass(d->seq) &&
179             !strcmp(sn_startup_sequence_get_wmclass(d->seq), wmclass))
180         {
181             sn_startup_sequence_complete(d->seq);
182             break;
183         }
184     }
185 }
186
187 gboolean sn_get_desktop(gchar *id, guint *desktop)
188 {
189     ObWaitData *d;
190
191     if (id && (d = wait_find(id))) {
192         gint desk = sn_startup_sequence_get_workspace(d->seq);
193         if (desk != -1) {
194             *desktop = desk;
195             return TRUE;
196         }
197     }
198     return FALSE;
199 }
200
201 #endif