10 #include <xcb/composite.h>
12 #define ROOT_MASK (XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | \
13 XCB_EVENT_MASK_STRUCTURE_NOTIFY)
15 #define SELECTION_MASK (XCB_EVENT_MASK_STRUCTURE_NOTIFY | \
16 XCB_EVENT_MASK_PROPERTY_CHANGE)
18 static gboolean screen_init(d_screen_t *sc);
19 static xcb_timestamp_t screen_timestamp(d_screen_t *sc);
20 static void screen_add_existing_windows(d_screen_t *sc);
21 static void screen_set_next_repaint(d_screen_t *sc);
24 xcb_window_hash(xcb_window_t *w) { return *w; }
27 xcb_window_equal(xcb_window_t *w1, xcb_window_t *w2) { return *w1 == *w2; }
31 screen_new(struct d_display *dpy, int num, xcb_screen_t *xcb)
35 sc = malloc(sizeof(d_screen_t));
41 gettimeofday(&sc->next_repaint, NULL);
42 sc->need_repaint = TRUE;
44 sc->winhash = g_hash_table_new((GHashFunc)xcb_window_hash,
45 (GEqualFunc)xcb_window_equal);
46 sc->stacking = list_new();
47 sc->plugin_data = g_hash_table_new_full((GHashFunc)g_str_hash,
48 (GEqualFunc)g_str_equal,
55 screen_ref(d_screen_t *sc)
61 screen_unref(d_screen_t *sc)
63 if (sc && --sc->ref == 0) {
64 g_hash_table_unref(sc->winhash);
65 list_unref(sc->stacking);
66 g_hash_table_unref(sc->plugin_data);
72 screen_register(d_screen_t *sc)
76 xcb_intern_atom_cookie_t ack;
77 xcb_intern_atom_reply_t *arep;
78 xcb_get_selection_owner_cookie_t sck;
79 xcb_get_selection_owner_reply_t *srep;
83 w = xcb_generate_id(sc->dpy->conn);
84 event_mask = SELECTION_MASK;
85 xcb_create_window(sc->dpy->conn, XCB_COPY_FROM_PARENT, w, sc->super.root,
86 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_ONLY,
87 sc->super.root_visual, XCB_CW_EVENT_MASK, &event_mask);
89 name = g_strdup_printf("_NET_WM_CM_S%d", sc->num);
90 ack = xcb_intern_atom(sc->dpy->conn, FALSE, strlen(name), name);
91 arep = xcb_intern_atom_reply(sc->dpy->conn, ack, NULL);
94 xcb_grab_server(sc->dpy->conn);
96 sck = xcb_get_selection_owner(sc->dpy->conn, arep->atom);
97 srep = xcb_get_selection_owner_reply(sc->dpy->conn, sck, NULL);
98 taken = !!srep->owner;
101 printf(_("screen %d already has a composite manager, skipping\n"),
106 xcb_timestamp_t time;
109 sc->selatom = arep->atom;
111 time = screen_timestamp(sc);
113 xcb_set_selection_owner(sc->dpy->conn, w, arep->atom, time);
114 sck = xcb_get_selection_owner(sc->dpy->conn, arep->atom);
115 srep = xcb_get_selection_owner_reply(sc->dpy->conn, sck, NULL);
116 taken = srep->owner == w;
117 if (taken && screen_init(sc)) {
118 screen_add_existing_windows(sc);
122 xcb_destroy_window(sc->dpy->conn, w);
127 xcb_ungrab_server(sc->dpy->conn);
128 xcb_flush(sc->dpy->conn);
133 screen_init(d_screen_t *sc)
137 xcb_generic_error_t *err;
138 xcb_void_cookie_t redir_ck;
139 xcb_composite_get_overlay_window_cookie_t overlay_ck;
140 xcb_composite_get_overlay_window_reply_t *overlay_rep;
143 xcb_composite_redirect_subwindows(sc->dpy->conn, sc->super.root,
144 XCB_COMPOSITE_REDIRECT_MANUAL);
146 overlay_ck = xcb_composite_get_overlay_window(sc->dpy->conn,
149 /* check that the redirect worked */
150 err = xcb_request_check(sc->dpy->conn, redir_ck);
152 printf(_("unable to redirect rendering, another composite manager must be running"));
157 /* get the overlay window reply */
158 overlay_rep = xcb_composite_get_overlay_window_reply(sc->dpy->conn,
162 printf(_("unable to get the composite overlay window\n"));
165 sc->overlay = overlay_rep->overlay_win;
169 mask = SELECTION_MASK;
170 xcb_change_window_attributes(sc->dpy->conn, sc->selwin,
171 XCB_CW_EVENT_MASK, &mask);
173 xcb_change_window_attributes(sc->dpy->conn, sc->super.root,
174 XCB_CW_EVENT_MASK, &mask);
180 screen_add_window(d_screen_t *sc, xcb_window_t wid)
184 /* XXX xgetwindowattributes - size and tings */
186 w = window_new(wid, sc);
187 g_hash_table_insert(sc->winhash, &w->id, w);
189 printf("screen added window 0x%x\n", w->id);
193 screen_add_existing_windows(d_screen_t *sc)
195 xcb_query_tree_cookie_t ck;
196 xcb_query_tree_reply_t *rep;
198 ck = xcb_query_tree(sc->dpy->conn, sc->super.root);
199 rep = xcb_query_tree_reply(sc->dpy->conn, ck, NULL);
201 xcb_window_iterator_t it;
204 it = xcb_query_tree_children_iterator(rep);
205 for (; it.rem; xcb_window_next(&it))
206 screen_add_window(sc, *it.data);
213 screen_remove_window(d_screen_t *sc, struct d_window *w)
215 printf("screen removed window 0x%x\n", w->id);
217 g_hash_table_remove(sc->winhash, &w->id);
218 sc->window_become_zombie(w);
223 screen_find_window(d_screen_t *sc, xcb_window_t id)
225 return g_hash_table_lookup(sc->winhash, &id);
228 static xcb_timestamp_t
229 screen_timestamp(d_screen_t *sc)
231 xcb_void_cookie_t ck;
232 xcb_timestamp_t time;
234 ck = xcb_change_property(sc->dpy->conn, XCB_PROP_MODE_REPLACE, sc->selwin,
235 sc->selatom, sc->selatom, 32, 0, NULL);
236 xcb_flush(sc->dpy->conn);
238 xcb_generic_event_t *ev;
240 ev = xcb_wait_for_event(sc->dpy->conn);
242 printf(_("IO error\n"));
246 /* expect errors.. */
247 if (!ev->response_type) {
248 display_error(sc->dpy, (xcb_generic_error_t*)ev);
253 if (ev->response_type == XCB_PROPERTY_NOTIFY &&
254 ev->full_sequence == ck.sequence)
256 time = ((xcb_property_notify_event_t*)ev)->time;
261 printf("created timestamp %lu\n", (unsigned long) time);
266 screen_stacking_add(d_screen_t *sc, struct d_window *w)
268 list_prepend(sc->stacking, w);
272 screen_stacking_remove(d_screen_t *sc, struct d_window *w)
274 list_remove(sc->stacking, w);
278 screen_set_next_repaint(d_screen_t *sc)
280 gettimeofday(&sc->next_repaint, NULL);
281 /* add time for the refresh rate (60 hz) */
282 time_add(&sc->next_repaint, 1000000/60);
283 sc->need_repaint = FALSE;
287 screen_setup_default_functions(d_screen_t *sc)
289 sc->screen_paint = screen_set_next_repaint;
290 sc->window_show = window_show;
291 sc->window_hide = window_hide;
292 sc->window_become_zombie = window_become_zombie;
297 screen_add_plugin_data(d_screen_t *sc, const char *key, void *data)
299 char *skey = g_strdup(key);
300 g_hash_table_insert(sc->plugin_data, skey, data);
304 screen_find_plugin_data(d_screen_t *sc, const char *key)
306 return g_hash_table_lookup(sc->plugin_data, key);
310 screen_remove_plugin_data(d_screen_t *sc, const char *key)
312 g_hash_table_remove(sc->plugin_data, key);
315 void screen_refresh(d_screen_t *sc)
317 sc->need_repaint = TRUE;