little bit of an actions overhaul, added action_run* so that duplicated code can...
[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     for (it = sn_waits; it; it = g_slist_next(it))
54         wait_data_free(it->data);
55     g_slist_free(sn_waits);
56     sn_waits = NULL;
57
58     screen_set_root_cursor();
59
60     sn_monitor_context_unref(sn_context);
61     sn_display_unref(sn_display);
62 }
63
64 static ObWaitData* wait_data_new(SnStartupSequence *seq)
65 {
66     ObWaitData *d = g_new(ObWaitData, 1);
67     d->seq = seq;
68     d->feedback = TRUE;
69
70     sn_startup_sequence_ref(d->seq);
71
72     return d;
73 }
74
75 static void wait_data_free(ObWaitData *d)
76 {
77     if (d) {
78         sn_startup_sequence_unref(d->seq);
79
80         g_free(d);
81     }
82 }
83
84 static ObWaitData* wait_find(const gchar *id)
85 {
86     ObWaitData *ret = NULL;
87     GSList *it;
88
89     for (it = sn_waits; it; it = g_slist_next(it)) {
90         ObWaitData *d = it->data;
91         if (!strcmp(id, sn_startup_sequence_get_id(d->seq))) {
92             ret = d;
93             break;
94         }
95     }
96     return ret;
97 }
98
99 gboolean sn_app_starting()
100 {
101     GSList *it;
102
103     for (it = sn_waits; it; it = g_slist_next(it)) {
104         ObWaitData *d = it->data;
105         if (d->feedback)
106             return TRUE;
107     }
108     return FALSE;
109 }
110
111 static gboolean sn_wait_timeout(gpointer data)
112 {
113     ObWaitData *d = data;
114     d->feedback = FALSE;
115     screen_set_root_cursor();
116     return FALSE; /* don't repeat */
117 }
118
119 static void sn_wait_destroy(gpointer data)
120 {
121     ObWaitData *d = data;
122     sn_waits = g_slist_remove(sn_waits, d);
123     wait_data_free(d);
124 }
125
126 static void sn_handler(const XEvent *e, gpointer data)
127 {
128     XEvent ec;
129     ec = *e;
130     sn_display_process_event(sn_display, &ec);
131 }
132
133 static void sn_event_func(SnMonitorEvent *ev, gpointer data)
134 {
135     SnStartupSequence *seq;
136     gboolean change = FALSE;
137     ObWaitData *d;
138
139     if (!(seq = sn_monitor_event_get_startup_sequence(ev)))
140         return;
141
142     switch (sn_monitor_event_get_type(ev)) {
143     case SN_MONITOR_EVENT_INITIATED:
144         g_message("starting");
145         d = wait_data_new(seq);
146         sn_waits = g_slist_prepend(sn_waits, d);
147         /* 30 second timeout for apps to start */
148         ob_main_loop_timeout_add(ob_main_loop, 30 * G_USEC_PER_SEC,
149                                  sn_wait_timeout, d, sn_wait_destroy);
150         change = TRUE;
151         break;
152     case SN_MONITOR_EVENT_CHANGED:
153         /* XXX feedback changed? */
154         change = TRUE;
155         break;
156     case SN_MONITOR_EVENT_COMPLETED:
157     case SN_MONITOR_EVENT_CANCELED:
158         if ((d = wait_find(sn_startup_sequence_get_id(seq)))) {
159             d->feedback = FALSE;
160             ob_main_loop_timeout_remove_data(ob_main_loop, sn_wait_timeout, d);
161             change = TRUE;
162         }
163         break;
164     };
165
166     if (change)
167         screen_set_root_cursor();
168 }
169
170 void sn_app_started(gchar *wmclass)
171 {
172     GSList *it;
173
174     for (it = sn_waits; it; it = g_slist_next(it)) {
175         ObWaitData *d = it->data;
176         if (sn_startup_sequence_get_wmclass(d->seq) &&
177             !strcmp(sn_startup_sequence_get_wmclass(d->seq), wmclass))
178         {
179             sn_startup_sequence_complete(d->seq);
180             break;
181         }
182     }
183 }
184
185 gboolean sn_get_desktop(gchar *id, guint *desktop)
186 {
187     ObWaitData *d;
188
189     if (id && (d = wait_find(id))) {
190         gint desk = sn_startup_sequence_get_workspace(d->seq);
191         if (desk != -1) {
192             *desktop = desk;
193             return TRUE;
194         }
195     }
196     return FALSE;
197 }
198
199 #endif