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