11 #include <xcb/composite.h>
13 #define ROOT_MASK (XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | \
14 XCB_EVENT_MASK_STRUCTURE_NOTIFY)
16 #define SELECTION_MASK (XCB_EVENT_MASK_STRUCTURE_NOTIFY | \
17 XCB_EVENT_MASK_PROPERTY_CHANGE)
19 static gboolean screen_init(d_screen_t *sc);
20 static xcb_timestamp_t screen_timestamp(d_screen_t *sc);
21 static void screen_add_existing_windows(d_screen_t *sc);
24 screen_register(struct d_display *dpy, int num, d_screen_t *sc)
28 xcb_intern_atom_cookie_t ack;
29 xcb_intern_atom_reply_t *arep;
30 xcb_get_selection_owner_cookie_t sck;
31 xcb_get_selection_owner_reply_t *srep;
37 w = xcb_generate_id(sc->dpy->conn);
38 event_mask = SELECTION_MASK;
39 xcb_create_window(sc->dpy->conn, XCB_COPY_FROM_PARENT, w, sc->super.root,
40 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_ONLY,
41 sc->super.root_visual, XCB_CW_EVENT_MASK, &event_mask);
43 name = g_strdup_printf("_NET_WM_CM_S%d", sc->num);
44 ack = xcb_intern_atom(sc->dpy->conn, FALSE, strlen(name), name);
45 arep = xcb_intern_atom_reply(sc->dpy->conn, ack, NULL);
48 xcb_grab_server(sc->dpy->conn);
50 sck = xcb_get_selection_owner(sc->dpy->conn, arep->atom);
51 srep = xcb_get_selection_owner_reply(sc->dpy->conn, sck, NULL);
52 taken = !!srep->owner;
55 printf(_("screen %d already has a composite manager, skipping\n"),
63 sc->selatom = arep->atom;
65 time = screen_timestamp(sc);
67 xcb_set_selection_owner(sc->dpy->conn, w, arep->atom, time);
68 sck = xcb_get_selection_owner(sc->dpy->conn, arep->atom);
69 srep = xcb_get_selection_owner_reply(sc->dpy->conn, sck, NULL);
70 taken = srep->owner == w;
71 if (taken && screen_init(sc)) {
72 screen_add_existing_windows(sc);
76 xcb_destroy_window(sc->dpy->conn, w);
81 xcb_ungrab_server(sc->dpy->conn);
82 xcb_flush(sc->dpy->conn);
87 xcb_window_hash(xcb_window_t *w) { return *w; }
90 xcb_window_compare(xcb_window_t *w1, xcb_window_t *w2) { return *w1 == *w2; }
93 screen_init(d_screen_t *sc)
97 xcb_generic_error_t *err;
98 xcb_void_cookie_t redir_ck;
99 xcb_composite_get_overlay_window_cookie_t overlay_ck;
100 xcb_composite_get_overlay_window_reply_t *overlay_rep;
103 xcb_composite_redirect_subwindows(sc->dpy->conn, sc->super.root,
104 XCB_COMPOSITE_REDIRECT_MANUAL);
106 overlay_ck = xcb_composite_get_overlay_window(sc->dpy->conn,
109 /* check that the redirect worked */
110 err = xcb_request_check(sc->dpy->conn, redir_ck);
112 printf(_("unable to redirect rendering, another composite manager must be running"));
117 /* get the overlay window reply */
118 overlay_rep = xcb_composite_get_overlay_window_reply(sc->dpy->conn,
122 printf(_("unable to get the composite overlay window\n"));
125 sc->overlay = overlay_rep->overlay_win;
129 mask = SELECTION_MASK;
130 xcb_change_window_attributes(sc->dpy->conn, sc->selwin,
131 XCB_CW_EVENT_MASK, &mask);
133 xcb_change_window_attributes(sc->dpy->conn, sc->super.root,
134 XCB_CW_EVENT_MASK, &mask);
136 /* use the render backend */
137 sc->paint = render_paint_screen;
139 gettimeofday(&sc->next_repaint, NULL);
141 sc->winhash = g_hash_table_new((GHashFunc)xcb_window_hash,
142 (GCompareFunc)xcb_window_compare);
143 sc->stacking = list_new();
148 void screen_free(d_screen_t *sc)
150 g_hash_table_unref(sc->winhash);
151 list_unref(sc->stacking);
155 screen_add_window(d_screen_t *sc, xcb_window_t wid)
159 /* XXX xgetwindowattributes - size and tings */
161 w = window_new(wid, sc);
162 g_hash_table_insert(sc->winhash, &w->id, w);
164 printf("screen added window 0x%x\n", w->id);
168 screen_add_existing_windows(d_screen_t *sc)
170 xcb_query_tree_cookie_t ck;
171 xcb_query_tree_reply_t *rep;
173 ck = xcb_query_tree(sc->dpy->conn, sc->super.root);
174 rep = xcb_query_tree_reply(sc->dpy->conn, ck, NULL);
176 xcb_window_iterator_t it;
179 it = xcb_query_tree_children_iterator(rep);
180 for (; it.rem; xcb_window_next(&it))
181 screen_add_window(sc, *it.data);
188 screen_remove_window(d_screen_t *sc, struct d_window *w)
190 printf("screen removed window 0x%x\n", w->id);
192 g_hash_table_remove(sc->winhash, &w->id);
198 screen_find_window(d_screen_t *sc, xcb_window_t id)
200 return g_hash_table_lookup(sc->winhash, &id);
203 static xcb_timestamp_t
204 screen_timestamp(d_screen_t *sc)
206 xcb_void_cookie_t ck;
207 xcb_timestamp_t time;
209 ck = xcb_change_property(sc->dpy->conn, XCB_PROP_MODE_REPLACE, sc->selwin,
210 sc->selatom, sc->selatom, 32, 0, NULL);
211 xcb_flush(sc->dpy->conn);
213 xcb_generic_event_t *ev;
215 ev = xcb_wait_for_event(sc->dpy->conn);
217 printf(_("IO error\n"));
221 /* expect errors.. */
222 if (!ev->response_type) {
223 display_error(sc->dpy, (xcb_generic_error_t*)ev);
228 if (ev->response_type == XCB_PROPERTY_NOTIFY &&
229 ev->full_sequence == ck.sequence)
231 time = ((xcb_property_notify_event_t*)ev)->time;
236 printf("created timestamp %lu\n", (unsigned long) time);
241 screen_stacking_add(d_screen_t *sc, struct d_window *w)
243 list_prepend(sc->stacking, w);
247 screen_stacking_remove(d_screen_t *sc, struct d_window *w)
249 list_remove(sc->stacking, w);
253 screen_set_next_repaint(d_screen_t *sc)
255 gettimeofday(&sc->next_repaint, NULL);
256 /* add time for the refresh rate (60 hz) */
257 time_add(&sc->next_repaint, 1000000/60);