add a signal handler
[dana/dcompmgr.git] / screen.c
1 #include "screen.h"
2 #include "display.h"
3 #include "plugin.h"
4 #include "list.h"
5 #include "time.h"
6 #include "window.h"
7 #include "gettext.h"
8 #include <string.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <xcb/composite.h>
12 #include <xcb/xfixes.h>
13
14 #define ROOT_MASK      (XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | \
15                         XCB_EVENT_MASK_STRUCTURE_NOTIFY)
16
17 #define SELECTION_MASK (XCB_EVENT_MASK_STRUCTURE_NOTIFY | \
18                         XCB_EVENT_MASK_PROPERTY_CHANGE)
19
20 static gboolean screen_init(d_screen_t *sc);
21 static xcb_timestamp_t screen_timestamp(d_screen_t *sc);
22 static void screen_add_existing_windows(d_screen_t *sc);
23 static void screen_set_next_repaint(d_screen_t *sc);
24
25 static guint
26 xcb_window_hash(xcb_window_t *w) { return *w; }
27
28 static gboolean
29 xcb_window_equal(xcb_window_t *w1, xcb_window_t *w2) { return *w1 == *w2; }
30
31
32 d_screen_t*
33 screen_new(struct d_display *dpy, int num, xcb_screen_t *xcb)
34 {
35     d_screen_t *sc;
36
37     sc = malloc(sizeof(d_screen_t));
38     sc->super = *xcb;
39     sc->ref = 1;
40     sc->dpy = dpy;
41     sc->num = num;
42
43     gettimeofday(&sc->next_repaint, NULL);
44     sc->need_repaint = TRUE;
45
46     sc->winhash = g_hash_table_new((GHashFunc)xcb_window_hash,
47                                    (GEqualFunc)xcb_window_equal);
48     sc->stacking = list_new();
49     sc->plugin_data = list_new();
50
51     return sc;
52 }
53
54 void
55 screen_ref(d_screen_t *sc)
56 {
57     ++sc->ref;
58 }
59
60 void
61 screen_unref(d_screen_t *sc)
62 {
63     if (sc && --sc->ref == 0) {
64         g_hash_table_unref(sc->winhash);
65         list_unref(sc->stacking);
66         list_unref(sc->plugin_data);
67         free(sc);
68     }
69 }
70
71 gboolean
72 screen_register(d_screen_t *sc)
73 {
74     char *name;
75     xcb_window_t w;
76     xcb_intern_atom_cookie_t ack;
77     xcb_intern_atom_reply_t *arep;
78     xcb_get_selection_owner_cookie_t sck;
79     xcb_get_selection_owner_reply_t *srep;
80     uint32_t event_mask;
81     gboolean taken, ret;
82
83     w = xcb_generate_id(sc->dpy->conn);
84     event_mask = SELECTION_MASK;
85     xcb_create_window(sc->dpy->conn, XCB_COPY_FROM_PARENT, w, sc->super.root,
86                       0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_ONLY,
87                       sc->super.root_visual, XCB_CW_EVENT_MASK, &event_mask);
88
89     name = g_strdup_printf("_NET_WM_CM_S%d", sc->num);
90     ack = xcb_intern_atom(sc->dpy->conn, FALSE, strlen(name), name);
91     arep = xcb_intern_atom_reply(sc->dpy->conn, ack, NULL);
92     g_free(name);
93
94     xcb_grab_server(sc->dpy->conn);
95
96     sck = xcb_get_selection_owner(sc->dpy->conn, arep->atom);
97     srep = xcb_get_selection_owner_reply(sc->dpy->conn, sck, NULL);
98     taken = !!srep->owner;
99     free(srep);
100     if (taken) {
101         printf(_("screen %d already has a composite manager, skipping\n"),
102                sc->num);
103         ret = FALSE;
104     }
105     else {
106         xcb_timestamp_t time;
107
108         sc->selwin = w;
109         sc->selatom = arep->atom;
110
111         time = screen_timestamp(sc);
112
113         xcb_set_selection_owner(sc->dpy->conn, w, arep->atom, time);
114         sck = xcb_get_selection_owner(sc->dpy->conn, arep->atom);
115         srep = xcb_get_selection_owner_reply(sc->dpy->conn, sck, NULL);
116         taken = srep->owner == w;
117         if (taken && screen_init(sc)) {
118             screen_add_existing_windows(sc);
119             ret = TRUE;
120         }
121         else {
122             xcb_destroy_window(sc->dpy->conn, w);
123             ret = FALSE;
124         }
125     }
126
127     xcb_ungrab_server(sc->dpy->conn);
128     xcb_flush(sc->dpy->conn);
129
130     return ret;
131 }
132
133 static gboolean
134 screen_init(d_screen_t *sc)
135 {
136     uint32_t mask;
137     xcb_generic_error_t *err;
138     xcb_void_cookie_t redir_ck;
139     xcb_composite_get_overlay_window_cookie_t overlay_ck;
140     xcb_composite_get_overlay_window_reply_t *overlay_rep;
141
142     redir_ck =
143         xcb_composite_redirect_subwindows(sc->dpy->conn, sc->super.root,
144                                           XCB_COMPOSITE_REDIRECT_AUTOMATIC);
145
146 #if 1
147     redir_ck =
148         xcb_composite_redirect_subwindows(sc->dpy->conn, sc->super.root,
149                                           XCB_COMPOSITE_REDIRECT_MANUAL);
150
151     overlay_ck = xcb_composite_get_overlay_window(sc->dpy->conn,
152                                                   sc->super.root);
153
154     /* check that the redirect worked */
155     err = xcb_request_check(sc->dpy->conn, redir_ck);
156     if (err) {
157         printf(_("unable to redirect rendering, another composite manager must be running"));
158         free(err);
159         return FALSE;
160     }
161
162     /* get the overlay window reply */
163     overlay_rep = xcb_composite_get_overlay_window_reply(sc->dpy->conn,
164                                                          overlay_ck,
165                                                          NULL);
166     if (!overlay_rep) {
167         printf(_("unable to get the composite overlay window\n"));
168         return FALSE;
169     }
170     sc->overlay = overlay_rep->overlay_win;
171     free(overlay_rep);
172
173     /* make the overlay window click-through */
174     if (sc->overlay) {
175         xcb_xfixes_region_t region;
176
177         region = xcb_generate_id(sc->dpy->conn);
178         xcb_xfixes_create_region(sc->dpy->conn, region, 0, NULL);
179         xcb_xfixes_set_window_shape_region(sc->dpy->conn,
180                                            sc->overlay,
181                                            XCB_SHAPE_SK_BOUNDING,
182                                            0, 0, XCB_NONE);
183         xcb_xfixes_set_window_shape_region(sc->dpy->conn,
184                                            sc->overlay,
185                                            XCB_SHAPE_SK_INPUT,
186                                            0, 0, region);
187         xcb_xfixes_destroy_region(sc->dpy->conn, region);
188     }
189 #endif
190
191     mask = SELECTION_MASK;
192     xcb_change_window_attributes(sc->dpy->conn, sc->selwin,
193                                  XCB_CW_EVENT_MASK, &mask);
194     mask = ROOT_MASK;
195     xcb_change_window_attributes(sc->dpy->conn, sc->super.root,
196                                  XCB_CW_EVENT_MASK, &mask);
197
198     return TRUE;
199 }
200
201 d_window_t*
202 screen_add_window(d_screen_t *sc, xcb_window_t wid)
203 {
204     d_window_t *w;
205
206     w = window_new(wid, sc);
207     g_hash_table_insert(sc->winhash, &w->id, w);
208
209     window_create_damage(w);
210
211     //printf("screen added window 0x%x\n", w->id);
212     return w;
213 }
214
215 static void
216 screen_add_existing_windows(d_screen_t *sc)
217 {
218     xcb_query_tree_cookie_t ck;
219     xcb_query_tree_reply_t *rep;
220
221     ck = xcb_query_tree(sc->dpy->conn, sc->super.root);
222     rep = xcb_query_tree_reply(sc->dpy->conn, ck, NULL);
223     if (rep) {
224         xcb_window_iterator_t it;
225
226         it = xcb_query_tree_children_iterator(rep);
227         for (; it.rem; xcb_window_next(&it))
228             screen_add_window(sc, *it.data);
229
230         free(rep);
231     }
232 }
233
234 void
235 screen_remove_window(d_screen_t *sc, struct d_window *w)
236 {
237     //printf("screen removed window 0x%x\n", w->id);
238
239     window_destroy_damage(w);
240     g_hash_table_remove(sc->winhash, &w->id);
241     sc->window_become_zombie(w);
242     window_unref(w);
243 }
244
245 d_window_t*
246 screen_find_window(d_screen_t *sc, xcb_window_t id)
247 {
248     return g_hash_table_lookup(sc->winhash, &id);
249 }
250
251 static xcb_timestamp_t
252 screen_timestamp(d_screen_t *sc)
253 {
254     xcb_void_cookie_t ck;
255     xcb_timestamp_t   time;
256
257     ck = xcb_change_property(sc->dpy->conn, XCB_PROP_MODE_REPLACE, sc->selwin,
258                              sc->selatom, sc->selatom, 32, 0, NULL);
259     xcb_flush(sc->dpy->conn);
260     while (1) {
261         xcb_generic_event_t *ev;
262
263         ev = xcb_wait_for_event(sc->dpy->conn);
264         if (!ev) {
265             printf(_("IO error\n"));
266             exit(0);
267         }
268
269         /* expect errors.. */
270         if (!ev->response_type) {
271             display_error(sc->dpy, (xcb_generic_error_t*)ev);
272             free(ev);
273             continue;
274         }
275
276         if (ev->response_type == XCB_PROPERTY_NOTIFY &&
277             ev->full_sequence == ck.sequence)
278         {
279             time = ((xcb_property_notify_event_t*)ev)->time;
280             free(ev);
281             break;
282         }
283     }
284     printf("created timestamp %lu\n", (unsigned long) time);
285     return time;
286 }
287
288 void
289 screen_stacking_add(d_screen_t *sc, struct d_window *w)
290 {
291     list_prepend(sc->stacking, w);
292 }
293
294 void
295 screen_stacking_remove(d_screen_t *sc, struct d_window *w)
296 {
297     list_remove(sc->stacking, w);
298 }
299
300 void
301 screen_stacking_move_above(d_screen_t *sc, struct d_window *w,
302                            struct d_window *above)
303 {
304     d_list_it_t *wit = list_find(sc->stacking, w);
305     d_list_it_t *ait = list_find(sc->stacking, above);
306     list_move_before(sc->stacking, wit, ait);
307 }
308
309 static void
310 screen_set_next_repaint(d_screen_t *sc)
311 {
312     gettimeofday(&sc->next_repaint, NULL);
313     /* add time for the refresh rate (60 hz) */
314     time_add(&sc->next_repaint, 1000000/60);
315     sc->need_repaint = FALSE;
316 }
317
318 void
319 screen_setup_default_functions(d_screen_t *sc)
320 {
321     sc->screen_paint = screen_set_next_repaint;
322     sc->window_show = window_show;
323     sc->window_hide = window_hide;
324     sc->window_become_zombie = window_become_zombie;
325     sc->window_move = window_move;
326     sc->window_resize = window_resize;
327 }
328
329 void
330 screen_add_plugin_data(d_screen_t *sc, int id, void *data)
331 {
332     plugin_data_add(sc->plugin_data, id, data);
333 }
334
335 void*
336 screen_find_plugin_data(d_screen_t *sc, int id)
337 {
338     return plugin_data_find(sc->plugin_data, id);
339 }
340
341 void
342 screen_remove_plugin_data(d_screen_t *sc, int id)
343 {
344     plugin_data_remove(sc->plugin_data, id);
345 }
346
347 void screen_refresh(d_screen_t *sc)
348 {
349     sc->need_repaint = TRUE;
350     //printf("*** need repaint! ***\n");
351 }