14 #include <xcb/composite.h>
15 #include <xcb/xfixes.h>
17 #define ROOT_MASK (XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | \
18 XCB_EVENT_MASK_STRUCTURE_NOTIFY | \
19 XCB_EVENT_MASK_PROPERTY_CHANGE)
21 #define SELECTION_MASK (XCB_EVENT_MASK_STRUCTURE_NOTIFY | \
22 XCB_EVENT_MASK_PROPERTY_CHANGE)
24 static gboolean screen_init(d_screen_t *sc);
25 static xcb_timestamp_t screen_timestamp(d_screen_t *sc);
26 static void screen_add_existing_windows(d_screen_t *sc);
29 xcb_window_hash(xcb_window_t *w) { return *w; }
32 xcb_window_equal(xcb_window_t *w1, xcb_window_t *w2) { return *w1 == *w2; }
36 screen_new(struct d_display *dpy, int num, xcb_screen_t *xcb)
40 sc = malloc(sizeof(d_screen_t));
46 sc->need_repaint = TRUE;
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();
57 screen_ref(d_screen_t *sc)
63 screen_unref(d_screen_t *sc)
65 if (sc && --sc->ref == 0) {
66 d_list_it_t *it, *next;
68 /* clean up pending requests */
69 screen_get_root_pixmap(sc);
71 g_hash_table_unref(sc->winhash);
72 for (it = list_top(sc->stacking); it; it = next) {
74 window_unref(it->data);
76 list_unref(sc->stacking);
77 list_unref(sc->plugin_data);
83 screen_register(d_screen_t *sc)
87 xcb_intern_atom_cookie_t ack;
88 xcb_intern_atom_reply_t *arep;
89 xcb_get_selection_owner_cookie_t sck;
90 xcb_get_selection_owner_reply_t *srep;
94 w = xcb_generate_id(sc->dpy->conn);
95 event_mask = SELECTION_MASK;
96 xcb_create_window(sc->dpy->conn, XCB_COPY_FROM_PARENT, w, sc->super->root,
97 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_ONLY,
98 sc->super->root_visual, XCB_CW_EVENT_MASK, &event_mask);
100 name = g_strdup_printf("_NET_WM_CM_S%d", sc->num);
101 ack = xcb_intern_atom(sc->dpy->conn, FALSE, strlen(name), name);
102 arep = xcb_intern_atom_reply(sc->dpy->conn, ack, NULL);
105 xcb_grab_server(sc->dpy->conn);
107 sck = xcb_get_selection_owner(sc->dpy->conn, arep->atom);
108 srep = xcb_get_selection_owner_reply(sc->dpy->conn, sck, NULL);
109 taken = !!srep->owner;
112 printf(_("screen %d already has a composite manager, skipping\n"),
117 xcb_timestamp_t time;
120 sc->selatom = arep->atom;
122 time = screen_timestamp(sc);
124 xcb_set_selection_owner(sc->dpy->conn, w, arep->atom, time);
125 sck = xcb_get_selection_owner(sc->dpy->conn, arep->atom);
126 srep = xcb_get_selection_owner_reply(sc->dpy->conn, sck, NULL);
127 taken = srep->owner == w;
129 if (taken && screen_init(sc)) {
130 screen_add_existing_windows(sc);
134 xcb_destroy_window(sc->dpy->conn, w);
140 xcb_ungrab_server(sc->dpy->conn);
141 xcb_flush(sc->dpy->conn);
147 screen_init(d_screen_t *sc)
150 xcb_generic_error_t *err;
151 xcb_void_cookie_t redir_ck;
152 xcb_composite_get_overlay_window_cookie_t overlay_ck;
153 xcb_composite_get_overlay_window_reply_t *overlay_rep;
156 xcb_composite_redirect_subwindows(sc->dpy->conn, sc->super->root,
157 XCB_COMPOSITE_REDIRECT_AUTOMATIC);
161 xcb_composite_redirect_subwindows(sc->dpy->conn, sc->super->root,
162 XCB_COMPOSITE_REDIRECT_MANUAL);
164 overlay_ck = xcb_composite_get_overlay_window(sc->dpy->conn,
167 /* check that the redirect worked */
168 err = xcb_request_check(sc->dpy->conn, redir_ck);
170 printf(_("unable to redirect rendering, another composite manager must be running"));
175 /* get the overlay window reply */
176 overlay_rep = xcb_composite_get_overlay_window_reply(sc->dpy->conn,
180 printf(_("unable to get the composite overlay window\n"));
183 sc->overlay = overlay_rep->overlay_win;
186 /* make the overlay window click-through */
188 xcb_xfixes_region_t region;
189 xcb_get_window_attributes_cookie_t attr_ck;
190 xcb_get_window_attributes_reply_t *attr_rep;
192 region = xcb_generate_id(sc->dpy->conn);
193 xcb_xfixes_create_region(sc->dpy->conn, region, 0, NULL);
194 xcb_xfixes_set_window_shape_region(sc->dpy->conn,
196 XCB_SHAPE_SK_BOUNDING,
198 xcb_xfixes_set_window_shape_region(sc->dpy->conn,
202 xcb_xfixes_destroy_region(sc->dpy->conn, region);
204 attr_ck = xcb_get_window_attributes(sc->dpy->conn, sc->overlay);
205 attr_rep = xcb_get_window_attributes_reply(sc->dpy->conn, attr_ck,
209 sc->overlay_visual = attr_rep->visual;
216 mask = SELECTION_MASK;
217 xcb_change_window_attributes(sc->dpy->conn, sc->selwin,
218 XCB_CW_EVENT_MASK, &mask);
220 xcb_change_window_attributes(sc->dpy->conn, sc->super->root,
221 XCB_CW_EVENT_MASK, &mask);
223 screen_update_root_pixmap(sc);
229 screen_update_root_pixmap(d_screen_t *sc)
231 if (sc->root_pixmap_waiting) {
232 xcb_get_property_reply_t *rep;
233 rep = xcb_get_property_reply(sc->dpy->conn, sc->root_pixmap_ck[0],
236 rep = xcb_get_property_reply(sc->dpy->conn, sc->root_pixmap_ck[1],
239 rep = xcb_get_property_reply(sc->dpy->conn, sc->root_pixmap_ck[2],
243 sc->root_pixmap_ck[0] =
244 xcb_get_property_unchecked(sc->dpy->conn, FALSE,
245 sc->super->root, sc->dpy->a.xrootpmap_id,
246 sc->dpy->a.pixmap, 0, 1);
247 sc->root_pixmap_ck[1] =
248 xcb_get_property_unchecked(sc->dpy->conn, FALSE,
249 sc->super->root,sc->dpy->a.esetroot_pmap_id,
250 sc->dpy->a.pixmap, 0, 1);
251 sc->root_pixmap_ck[2] =
252 xcb_get_property_unchecked(sc->dpy->conn, FALSE,
253 sc->super->root, sc->dpy->a.xsetroot_id,
254 sc->dpy->a.pixmap, 0, 1);
255 sc->root_pixmap_waiting = TRUE;
259 screen_add_window(d_screen_t *sc, xcb_window_t wid)
263 assert(wid != sc->overlay);
265 w = window_new(wid, sc);
266 g_hash_table_insert(sc->winhash, &w->id, w);
268 window_create_damage(w);
270 //printf("screen added window 0x%x\n", w->id);
275 screen_add_existing_windows(d_screen_t *sc)
277 xcb_query_tree_cookie_t ck;
278 xcb_query_tree_reply_t *rep;
280 ck = xcb_query_tree(sc->dpy->conn, sc->super->root);
281 rep = xcb_query_tree_reply(sc->dpy->conn, ck, NULL);
283 xcb_window_iterator_t it;
285 it = xcb_query_tree_children_iterator(rep);
286 for (; it.rem; xcb_window_next(&it))
287 screen_add_window(sc, *it.data);
294 screen_remove_window(d_screen_t *sc, struct d_window *w)
296 //printf("screen removed window 0x%x\n", w->id);
298 window_destroy_damage(w);
299 g_hash_table_remove(sc->winhash, &w->id);
304 screen_find_window(d_screen_t *sc, xcb_window_t id)
306 return g_hash_table_lookup(sc->winhash, &id);
309 static xcb_timestamp_t
310 screen_timestamp(d_screen_t *sc)
312 xcb_void_cookie_t ck;
313 xcb_timestamp_t time;
315 ck = xcb_change_property(sc->dpy->conn, XCB_PROP_MODE_REPLACE, sc->selwin,
316 sc->selatom, sc->selatom, 32, 0, NULL);
317 xcb_flush(sc->dpy->conn);
319 xcb_generic_event_t *ev;
321 ev = xcb_wait_for_event(sc->dpy->conn);
323 printf(_("IO error\n"));
327 /* expect errors.. */
328 if (!ev->response_type) {
329 display_error(sc->dpy, (xcb_generic_error_t*)ev);
334 if (ev->response_type == XCB_PROPERTY_NOTIFY &&
335 ev->full_sequence == ck.sequence)
337 time = ((xcb_property_notify_event_t*)ev)->time;
342 //printf("created timestamp %lu\n", (unsigned long) time);
347 screen_stacking_add(d_screen_t *sc, struct d_window *w)
349 list_prepend(sc->stacking, w);
353 screen_stacking_remove(d_screen_t *sc, struct d_window *w)
355 list_remove(sc->stacking, w);
359 screen_stacking_move_above(d_screen_t *sc, struct d_window *w,
360 struct d_window *above)
362 d_list_it_t *wit = list_find(sc->stacking, w);
363 d_list_it_t *ait = list_find(sc->stacking, above);
364 list_move_before(sc->stacking, wit, ait);
367 void screen_stacking_move_to_top(d_screen_t *sc, struct d_window *w)
369 d_list_it_t *wit = list_find(sc->stacking, w);
370 d_list_it_t *ait = list_top(sc->stacking);
371 list_move_before(sc->stacking, wit, ait);
374 void screen_stacking_move_to_bottom(d_screen_t *sc, struct d_window *w)
376 d_list_it_t *wit = list_find(sc->stacking, w);
377 list_move_before(sc->stacking, wit, NULL);
381 screen_set_need_repaint(d_screen_t *sc)
383 sc->need_repaint = FALSE;
387 screen_setup_default_functions(d_screen_t *sc)
389 sc->screen_paint = screen_set_need_repaint;
390 sc->window_show = window_show;
391 sc->window_hide = window_hide;
392 sc->window_become_zombie = window_become_zombie;
393 sc->window_zombie_dead = window_zombie_dead;
394 sc->window_move = window_move;
395 sc->window_resize = window_resize;
396 sc->window_reshape = window_reshape;
397 sc->window_opacity_change = window_opacity_change;
398 sc->window_damage = window_damage;
399 sc->window_restack = window_restack;
400 sc->screen_root_pixmap_change = screen_update_root_pixmap;
404 screen_add_plugin_data(d_screen_t *sc, int id, void *data)
406 plugin_data_add(sc->plugin_data, id, data);
410 screen_find_plugin_data(d_screen_t *sc, int id)
412 return plugin_data_find(sc->plugin_data, id);
416 screen_remove_plugin_data(d_screen_t *sc, int id)
418 plugin_data_remove(sc->plugin_data, id);
422 screen_refresh(d_screen_t *sc)
424 sc->need_repaint = TRUE;
425 //printf("*** need repaint! ***\n");
429 screen_get_root_pixmap(d_screen_t *sc)
431 if (sc->root_pixmap_waiting) {
432 xcb_get_property_reply_t *rep;
435 sc->root_pixmap = XCB_NONE;
436 for (i = 2; i >= 0; --i) {
437 rep = xcb_get_property_reply(sc->dpy->conn, sc->root_pixmap_ck[i],
440 if (rep->type == sc->dpy->a.pixmap && rep->length >= 1) {
442 ((xcb_pixmap_t*)xcb_get_property_value(rep))[0];
443 //printf("got root pixmap 0x%x\n", sc->root_pixmap);
449 return sc->root_pixmap;