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)
19 #define SELECTION_MASK (XCB_EVENT_MASK_STRUCTURE_NOTIFY | \
20 XCB_EVENT_MASK_PROPERTY_CHANGE)
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);
28 xcb_window_hash(xcb_window_t *w) { return *w; }
31 xcb_window_equal(xcb_window_t *w1, xcb_window_t *w2) { return *w1 == *w2; }
35 screen_new(struct d_display *dpy, int num, xcb_screen_t *xcb)
39 sc = malloc(sizeof(d_screen_t));
45 gettimeofday(&sc->next_repaint, NULL);
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 g_hash_table_unref(sc->winhash);
69 for (it = list_top(sc->stacking); it; it = next) {
71 window_unref(it->data);
73 list_unref(sc->stacking);
74 list_unref(sc->plugin_data);
80 screen_register(d_screen_t *sc)
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;
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);
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);
102 xcb_grab_server(sc->dpy->conn);
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;
109 printf(_("screen %d already has a composite manager, skipping\n"),
114 xcb_timestamp_t time;
117 sc->selatom = arep->atom;
119 time = screen_timestamp(sc);
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;
126 if (taken && screen_init(sc)) {
127 screen_add_existing_windows(sc);
131 xcb_destroy_window(sc->dpy->conn, w);
137 xcb_ungrab_server(sc->dpy->conn);
138 xcb_flush(sc->dpy->conn);
144 screen_init(d_screen_t *sc)
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;
153 xcb_composite_redirect_subwindows(sc->dpy->conn, sc->super.root,
154 XCB_COMPOSITE_REDIRECT_AUTOMATIC);
158 xcb_composite_redirect_subwindows(sc->dpy->conn, sc->super.root,
159 XCB_COMPOSITE_REDIRECT_MANUAL);
161 overlay_ck = xcb_composite_get_overlay_window(sc->dpy->conn,
164 /* check that the redirect worked */
165 err = xcb_request_check(sc->dpy->conn, redir_ck);
167 printf(_("unable to redirect rendering, another composite manager must be running"));
172 /* get the overlay window reply */
173 overlay_rep = xcb_composite_get_overlay_window_reply(sc->dpy->conn,
177 printf(_("unable to get the composite overlay window\n"));
180 sc->overlay = overlay_rep->overlay_win;
183 /* make the overlay window click-through */
185 xcb_xfixes_region_t region;
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,
191 XCB_SHAPE_SK_BOUNDING,
193 xcb_xfixes_set_window_shape_region(sc->dpy->conn,
197 xcb_xfixes_destroy_region(sc->dpy->conn, region);
201 mask = SELECTION_MASK;
202 xcb_change_window_attributes(sc->dpy->conn, sc->selwin,
203 XCB_CW_EVENT_MASK, &mask);
205 xcb_change_window_attributes(sc->dpy->conn, sc->super.root,
206 XCB_CW_EVENT_MASK, &mask);
212 screen_add_window(d_screen_t *sc, xcb_window_t wid)
216 w = window_new(wid, sc);
217 g_hash_table_insert(sc->winhash, &w->id, w);
219 window_create_damage(w);
221 //printf("screen added window 0x%x\n", w->id);
226 screen_add_existing_windows(d_screen_t *sc)
228 xcb_query_tree_cookie_t ck;
229 xcb_query_tree_reply_t *rep;
231 ck = xcb_query_tree(sc->dpy->conn, sc->super.root);
232 rep = xcb_query_tree_reply(sc->dpy->conn, ck, NULL);
234 xcb_window_iterator_t it;
236 it = xcb_query_tree_children_iterator(rep);
237 for (; it.rem; xcb_window_next(&it))
238 screen_add_window(sc, *it.data);
245 screen_remove_window(d_screen_t *sc, struct d_window *w)
247 //printf("screen removed window 0x%x\n", w->id);
249 window_destroy_damage(w);
250 g_hash_table_remove(sc->winhash, &w->id);
251 sc->window_become_zombie(w);
256 screen_find_window(d_screen_t *sc, xcb_window_t id)
258 return g_hash_table_lookup(sc->winhash, &id);
261 static xcb_timestamp_t
262 screen_timestamp(d_screen_t *sc)
264 xcb_void_cookie_t ck;
265 xcb_timestamp_t time;
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);
271 xcb_generic_event_t *ev;
273 ev = xcb_wait_for_event(sc->dpy->conn);
275 printf(_("IO error\n"));
279 /* expect errors.. */
280 if (!ev->response_type) {
281 display_error(sc->dpy, (xcb_generic_error_t*)ev);
286 if (ev->response_type == XCB_PROPERTY_NOTIFY &&
287 ev->full_sequence == ck.sequence)
289 time = ((xcb_property_notify_event_t*)ev)->time;
294 printf("created timestamp %lu\n", (unsigned long) time);
299 screen_stacking_add(d_screen_t *sc, struct d_window *w)
301 list_prepend(sc->stacking, w);
305 screen_stacking_remove(d_screen_t *sc, struct d_window *w)
307 list_remove(sc->stacking, w);
311 screen_stacking_move_above(d_screen_t *sc, struct d_window *w,
312 struct d_window *above)
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);
320 screen_set_next_repaint(d_screen_t *sc)
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;
329 screen_setup_default_functions(d_screen_t *sc)
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;
340 screen_add_plugin_data(d_screen_t *sc, int id, void *data)
342 plugin_data_add(sc->plugin_data, id, data);
346 screen_find_plugin_data(d_screen_t *sc, int id)
348 return plugin_data_find(sc->plugin_data, id);
352 screen_remove_plugin_data(d_screen_t *sc, int id)
354 plugin_data_remove(sc->plugin_data, id);
358 screen_refresh(d_screen_t *sc)
360 sc->need_repaint = TRUE;
361 //printf("*** need repaint! ***\n");