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