13 #include <xcb/composite.h>
14 #include <xcb/xfixes.h>
16 #define ROOT_MASK (XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | \
17 XCB_EVENT_MASK_STRUCTURE_NOTIFY | \
18 XCB_EVENT_MASK_PROPERTY_CHANGE)
20 #define SELECTION_MASK (XCB_EVENT_MASK_STRUCTURE_NOTIFY | \
21 XCB_EVENT_MASK_PROPERTY_CHANGE)
23 static gboolean screen_init(d_screen_t *sc);
24 static xcb_timestamp_t screen_timestamp(d_screen_t *sc);
25 static void screen_add_existing_windows(d_screen_t *sc);
26 static void screen_set_next_repaint(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 gettimeofday(&sc->next_repaint, NULL);
47 sc->need_repaint = TRUE;
49 sc->winhash = g_hash_table_new((GHashFunc)xcb_window_hash,
50 (GEqualFunc)xcb_window_equal);
51 sc->stacking = list_new();
52 sc->plugin_data = list_new();
58 screen_ref(d_screen_t *sc)
64 screen_unref(d_screen_t *sc)
66 if (sc && --sc->ref == 0) {
67 d_list_it_t *it, *next;
69 /* clean up pending requests */
70 screen_get_root_pixmap(sc);
72 g_hash_table_unref(sc->winhash);
73 for (it = list_top(sc->stacking); it; it = next) {
75 window_unref(it->data);
77 list_unref(sc->stacking);
78 list_unref(sc->plugin_data);
84 screen_register(d_screen_t *sc)
88 xcb_intern_atom_cookie_t ack;
89 xcb_intern_atom_reply_t *arep;
90 xcb_get_selection_owner_cookie_t sck;
91 xcb_get_selection_owner_reply_t *srep;
95 w = xcb_generate_id(sc->dpy->conn);
96 event_mask = SELECTION_MASK;
97 xcb_create_window(sc->dpy->conn, XCB_COPY_FROM_PARENT, w, sc->super.root,
98 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_ONLY,
99 sc->super.root_visual, XCB_CW_EVENT_MASK, &event_mask);
101 name = g_strdup_printf("_NET_WM_CM_S%d", sc->num);
102 ack = xcb_intern_atom(sc->dpy->conn, FALSE, strlen(name), name);
103 arep = xcb_intern_atom_reply(sc->dpy->conn, ack, NULL);
106 xcb_grab_server(sc->dpy->conn);
108 sck = xcb_get_selection_owner(sc->dpy->conn, arep->atom);
109 srep = xcb_get_selection_owner_reply(sc->dpy->conn, sck, NULL);
110 taken = !!srep->owner;
113 printf(_("screen %d already has a composite manager, skipping\n"),
118 xcb_timestamp_t time;
121 sc->selatom = arep->atom;
123 time = screen_timestamp(sc);
125 xcb_set_selection_owner(sc->dpy->conn, w, arep->atom, time);
126 sck = xcb_get_selection_owner(sc->dpy->conn, arep->atom);
127 srep = xcb_get_selection_owner_reply(sc->dpy->conn, sck, NULL);
128 taken = srep->owner == w;
130 if (taken && screen_init(sc)) {
131 screen_add_existing_windows(sc);
135 xcb_destroy_window(sc->dpy->conn, w);
141 xcb_ungrab_server(sc->dpy->conn);
142 xcb_flush(sc->dpy->conn);
148 screen_init(d_screen_t *sc)
151 xcb_generic_error_t *err;
152 xcb_void_cookie_t redir_ck;
153 xcb_composite_get_overlay_window_cookie_t overlay_ck;
154 xcb_composite_get_overlay_window_reply_t *overlay_rep;
157 xcb_composite_redirect_subwindows(sc->dpy->conn, sc->super.root,
158 XCB_COMPOSITE_REDIRECT_AUTOMATIC);
162 xcb_composite_redirect_subwindows(sc->dpy->conn, sc->super.root,
163 XCB_COMPOSITE_REDIRECT_MANUAL);
165 overlay_ck = xcb_composite_get_overlay_window(sc->dpy->conn,
168 /* check that the redirect worked */
169 err = xcb_request_check(sc->dpy->conn, redir_ck);
171 printf(_("unable to redirect rendering, another composite manager must be running"));
176 /* get the overlay window reply */
177 overlay_rep = xcb_composite_get_overlay_window_reply(sc->dpy->conn,
181 printf(_("unable to get the composite overlay window\n"));
184 sc->overlay = overlay_rep->overlay_win;
187 /* make the overlay window click-through */
189 xcb_xfixes_region_t region;
191 region = xcb_generate_id(sc->dpy->conn);
192 xcb_xfixes_create_region(sc->dpy->conn, region, 0, NULL);
193 xcb_xfixes_set_window_shape_region(sc->dpy->conn,
195 XCB_SHAPE_SK_BOUNDING,
197 xcb_xfixes_set_window_shape_region(sc->dpy->conn,
201 xcb_xfixes_destroy_region(sc->dpy->conn, region);
205 mask = SELECTION_MASK;
206 xcb_change_window_attributes(sc->dpy->conn, sc->selwin,
207 XCB_CW_EVENT_MASK, &mask);
209 xcb_change_window_attributes(sc->dpy->conn, sc->super.root,
210 XCB_CW_EVENT_MASK, &mask);
212 screen_update_root_pixmap(sc);
218 screen_update_root_pixmap(d_screen_t *sc)
220 if (sc->root_pixmap_waiting) {
221 xcb_get_property_reply_t *rep;
222 rep = xcb_get_property_reply(sc->dpy->conn, sc->root_pixmap_ck[0],
225 rep = xcb_get_property_reply(sc->dpy->conn, sc->root_pixmap_ck[1],
228 rep = xcb_get_property_reply(sc->dpy->conn, sc->root_pixmap_ck[2],
232 sc->root_pixmap_ck[0] =
233 xcb_get_property_unchecked(sc->dpy->conn, FALSE,
234 sc->super.root, sc->dpy->a.xrootpmap_id,
235 sc->dpy->a.pixmap, 0, 1);
236 sc->root_pixmap_ck[1] =
237 xcb_get_property_unchecked(sc->dpy->conn, FALSE,
238 sc->super.root, sc->dpy->a.esetroot_pmap_id,
239 sc->dpy->a.pixmap, 0, 1);
240 sc->root_pixmap_ck[2] =
241 xcb_get_property_unchecked(sc->dpy->conn, FALSE,
242 sc->super.root, sc->dpy->a.xsetroot_id,
243 sc->dpy->a.pixmap, 0, 1);
244 sc->root_pixmap_waiting = TRUE;
248 screen_add_window(d_screen_t *sc, xcb_window_t wid)
252 w = window_new(wid, sc);
253 g_hash_table_insert(sc->winhash, &w->id, w);
255 window_create_damage(w);
257 //printf("screen added window 0x%x\n", w->id);
262 screen_add_existing_windows(d_screen_t *sc)
264 xcb_query_tree_cookie_t ck;
265 xcb_query_tree_reply_t *rep;
267 ck = xcb_query_tree(sc->dpy->conn, sc->super.root);
268 rep = xcb_query_tree_reply(sc->dpy->conn, ck, NULL);
270 xcb_window_iterator_t it;
272 it = xcb_query_tree_children_iterator(rep);
273 for (; it.rem; xcb_window_next(&it))
274 screen_add_window(sc, *it.data);
281 screen_remove_window(d_screen_t *sc, struct d_window *w)
283 //printf("screen removed window 0x%x\n", w->id);
285 window_destroy_damage(w);
286 g_hash_table_remove(sc->winhash, &w->id);
287 sc->window_become_zombie(w);
292 screen_find_window(d_screen_t *sc, xcb_window_t id)
294 return g_hash_table_lookup(sc->winhash, &id);
297 static xcb_timestamp_t
298 screen_timestamp(d_screen_t *sc)
300 xcb_void_cookie_t ck;
301 xcb_timestamp_t time;
303 ck = xcb_change_property(sc->dpy->conn, XCB_PROP_MODE_REPLACE, sc->selwin,
304 sc->selatom, sc->selatom, 32, 0, NULL);
305 xcb_flush(sc->dpy->conn);
307 xcb_generic_event_t *ev;
309 ev = xcb_wait_for_event(sc->dpy->conn);
311 printf(_("IO error\n"));
315 /* expect errors.. */
316 if (!ev->response_type) {
317 display_error(sc->dpy, (xcb_generic_error_t*)ev);
322 if (ev->response_type == XCB_PROPERTY_NOTIFY &&
323 ev->full_sequence == ck.sequence)
325 time = ((xcb_property_notify_event_t*)ev)->time;
330 printf("created timestamp %lu\n", (unsigned long) time);
335 screen_stacking_add(d_screen_t *sc, struct d_window *w)
337 list_prepend(sc->stacking, w);
341 screen_stacking_remove(d_screen_t *sc, struct d_window *w)
343 list_remove(sc->stacking, w);
347 screen_stacking_move_above(d_screen_t *sc, struct d_window *w,
348 struct d_window *above)
350 d_list_it_t *wit = list_find(sc->stacking, w);
351 d_list_it_t *ait = list_find(sc->stacking, above);
352 list_move_before(sc->stacking, wit, ait);
355 void screen_stacking_move_to_top(d_screen_t *sc, struct d_window *w)
357 d_list_it_t *wit = list_find(sc->stacking, w);
358 d_list_it_t *ait = list_top(sc->stacking);
359 list_move_before(sc->stacking, wit, ait);
362 void screen_stacking_move_to_bottom(d_screen_t *sc, struct d_window *w)
364 d_list_it_t *wit = list_find(sc->stacking, w);
365 list_move_before(sc->stacking, wit, NULL);
369 screen_set_next_repaint(d_screen_t *sc)
371 gettimeofday(&sc->next_repaint, NULL);
372 /* add time for the refresh rate (60 hz) */
373 time_add(&sc->next_repaint, 1000000/60);
374 sc->need_repaint = FALSE;
378 screen_setup_default_functions(d_screen_t *sc)
380 sc->screen_paint = screen_set_next_repaint;
381 sc->window_show = window_show;
382 sc->window_hide = window_hide;
383 sc->window_become_zombie = window_become_zombie;
384 sc->window_move = window_move;
385 sc->window_resize = window_resize;
386 sc->window_reshape = window_reshape;
387 sc->window_opacity_change = window_opacity_change;
388 sc->screen_root_pixmap_change = screen_update_root_pixmap;
392 screen_add_plugin_data(d_screen_t *sc, int id, void *data)
394 plugin_data_add(sc->plugin_data, id, data);
398 screen_find_plugin_data(d_screen_t *sc, int id)
400 return plugin_data_find(sc->plugin_data, id);
404 screen_remove_plugin_data(d_screen_t *sc, int id)
406 plugin_data_remove(sc->plugin_data, id);
410 screen_refresh(d_screen_t *sc)
412 sc->need_repaint = TRUE;
413 //printf("*** need repaint! ***\n");
417 screen_get_root_pixmap(d_screen_t *sc)
419 if (sc->root_pixmap_waiting) {
420 xcb_get_property_reply_t *rep;
423 sc->root_pixmap = XCB_NONE;
424 for (i = 2; i >= 0; --i) {
425 rep = xcb_get_property_reply(sc->dpy->conn, sc->root_pixmap_ck[i],
428 if (rep->type == sc->dpy->a.pixmap && rep->length >= 1) {
430 ((xcb_pixmap_t*)xcb_get_property_value(rep))[0];
431 printf("got root pixmap 0x%x\n", sc->root_pixmap);
437 return sc->root_pixmap;