the compmgr works.. hooray!
[dana/dcompmgr.git] / screen.c
1 #include "screen.h"
2 #include "display.h"
3 #include "list.h"
4 #include "time.h"
5 #include "window.h"
6 #include "gettext.h"
7 #include <string.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <xcb/composite.h>
11 #include <xcb/xfixes.h>
12
13 #define ROOT_MASK      (XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | \
14                         XCB_EVENT_MASK_STRUCTURE_NOTIFY)
15
16 #define SELECTION_MASK (XCB_EVENT_MASK_STRUCTURE_NOTIFY | \
17                         XCB_EVENT_MASK_PROPERTY_CHANGE)
18
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);
22 static void screen_set_next_repaint(d_screen_t *sc);
23
24 static guint
25 xcb_window_hash(xcb_window_t *w) { return *w; }
26
27 static gboolean
28 xcb_window_equal(xcb_window_t *w1, xcb_window_t *w2) { return *w1 == *w2; }
29
30
31 d_screen_t*
32 screen_new(struct d_display *dpy, int num, xcb_screen_t *xcb)
33 {
34     d_screen_t *sc;
35
36     sc = malloc(sizeof(d_screen_t));
37     sc->super = *xcb;
38     sc->ref = 1;
39     sc->dpy = dpy;
40     sc->num = num;
41
42     gettimeofday(&sc->next_repaint, NULL);
43     sc->need_repaint = TRUE;
44
45     sc->winhash = g_hash_table_new((GHashFunc)xcb_window_hash,
46                                    (GEqualFunc)xcb_window_equal);
47     sc->stacking = list_new();
48     sc->plugin_data = g_hash_table_new_full((GHashFunc)g_str_hash,
49                                             (GEqualFunc)g_str_equal,
50                                             g_free, NULL);
51
52     return sc;
53 }
54
55 void
56 screen_ref(d_screen_t *sc)
57 {
58     ++sc->ref;
59 }
60
61 void
62 screen_unref(d_screen_t *sc)
63 {
64     if (sc && --sc->ref == 0) {
65         g_hash_table_unref(sc->winhash);
66         list_unref(sc->stacking);
67         g_hash_table_unref(sc->plugin_data);
68         free(sc);
69     }
70 }
71
72 gboolean
73 screen_register(d_screen_t *sc)
74 {
75     char *name;
76     xcb_window_t w;
77     xcb_intern_atom_cookie_t ack;
78     xcb_intern_atom_reply_t *arep;
79     xcb_get_selection_owner_cookie_t sck;
80     xcb_get_selection_owner_reply_t *srep;
81     uint32_t event_mask;
82     gboolean taken, ret;
83
84     w = xcb_generate_id(sc->dpy->conn);
85     event_mask = SELECTION_MASK;
86     xcb_create_window(sc->dpy->conn, XCB_COPY_FROM_PARENT, w, sc->super.root,
87                       0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_ONLY,
88                       sc->super.root_visual, XCB_CW_EVENT_MASK, &event_mask);
89
90     name = g_strdup_printf("_NET_WM_CM_S%d", sc->num);
91     ack = xcb_intern_atom(sc->dpy->conn, FALSE, strlen(name), name);
92     arep = xcb_intern_atom_reply(sc->dpy->conn, ack, NULL);
93     g_free(name);
94
95     xcb_grab_server(sc->dpy->conn);
96
97     sck = xcb_get_selection_owner(sc->dpy->conn, arep->atom);
98     srep = xcb_get_selection_owner_reply(sc->dpy->conn, sck, NULL);
99     taken = !!srep->owner;
100     free(srep);
101     if (taken) {
102         printf(_("screen %d already has a composite manager, skipping\n"),
103                sc->num);
104         ret = FALSE;
105     }
106     else {
107         xcb_timestamp_t time;
108
109         sc->selwin = w;
110         sc->selatom = arep->atom;
111
112         time = screen_timestamp(sc);
113
114         xcb_set_selection_owner(sc->dpy->conn, w, arep->atom, time);
115         sck = xcb_get_selection_owner(sc->dpy->conn, arep->atom);
116         srep = xcb_get_selection_owner_reply(sc->dpy->conn, sck, NULL);
117         taken = srep->owner == w;
118         if (taken && screen_init(sc)) {
119             screen_add_existing_windows(sc);
120             ret = TRUE;
121         }
122         else {
123             xcb_destroy_window(sc->dpy->conn, w);
124             ret = FALSE;
125         }
126     }
127
128     xcb_ungrab_server(sc->dpy->conn);
129     xcb_flush(sc->dpy->conn);
130
131     return ret;
132 }
133
134 static gboolean
135 screen_init(d_screen_t *sc)
136 {
137     uint32_t mask;
138     xcb_generic_error_t *err;
139     xcb_void_cookie_t redir_ck;
140     xcb_composite_get_overlay_window_cookie_t overlay_ck;
141     xcb_composite_get_overlay_window_reply_t *overlay_rep;
142
143     redir_ck =
144         xcb_composite_redirect_subwindows(sc->dpy->conn, sc->super.root,
145                                           XCB_COMPOSITE_REDIRECT_AUTOMATIC);
146
147 #if 1
148     redir_ck =
149         xcb_composite_redirect_subwindows(sc->dpy->conn, sc->super.root,
150                                           XCB_COMPOSITE_REDIRECT_MANUAL);
151
152     overlay_ck = xcb_composite_get_overlay_window(sc->dpy->conn,
153                                                   sc->super.root);
154
155     /* check that the redirect worked */
156     err = xcb_request_check(sc->dpy->conn, redir_ck);
157     if (err) {
158         printf(_("unable to redirect rendering, another composite manager must be running"));
159         free(err);
160         return FALSE;
161     }
162
163     /* get the overlay window reply */
164     overlay_rep = xcb_composite_get_overlay_window_reply(sc->dpy->conn,
165                                                          overlay_ck,
166                                                          NULL);
167     if (!overlay_rep) {
168         printf(_("unable to get the composite overlay window\n"));
169         return FALSE;
170     }
171     sc->overlay = overlay_rep->overlay_win;
172     free(overlay_rep);
173
174     /* make the overlay window click-through */
175     if (sc->overlay) {
176         xcb_xfixes_region_t region;
177
178         region = xcb_generate_id(sc->dpy->conn);
179         xcb_xfixes_create_region(sc->dpy->conn, region, 0, NULL);
180         xcb_xfixes_set_window_shape_region(sc->dpy->conn,
181                                            sc->overlay,
182                                            XCB_SHAPE_SK_BOUNDING,
183                                            0, 0, XCB_NONE);
184         xcb_xfixes_set_window_shape_region(sc->dpy->conn,
185                                            sc->overlay,
186                                            XCB_SHAPE_SK_INPUT,
187                                            0, 0, region);
188         xcb_xfixes_destroy_region(sc->dpy->conn, region);
189     }
190 #endif
191
192     mask = SELECTION_MASK;
193     xcb_change_window_attributes(sc->dpy->conn, sc->selwin,
194                                  XCB_CW_EVENT_MASK, &mask);
195     mask = ROOT_MASK;
196     xcb_change_window_attributes(sc->dpy->conn, sc->super.root,
197                                  XCB_CW_EVENT_MASK, &mask);
198
199     return TRUE;
200 }
201
202 d_window_t*
203 screen_add_window(d_screen_t *sc, xcb_window_t wid)
204 {
205     d_window_t *w;
206
207     w = window_new(wid, sc);
208     g_hash_table_insert(sc->winhash, &w->id, w);
209
210     window_create_damage(w);
211
212     //printf("screen added window 0x%x\n", w->id);
213     return w;
214 }
215
216 static void
217 screen_add_existing_windows(d_screen_t *sc)
218 {
219     xcb_query_tree_cookie_t ck;
220     xcb_query_tree_reply_t *rep;
221
222     ck = xcb_query_tree(sc->dpy->conn, sc->super.root);
223     rep = xcb_query_tree_reply(sc->dpy->conn, ck, NULL);
224     if (rep) {
225         xcb_window_iterator_t it;
226
227         it = xcb_query_tree_children_iterator(rep);
228         for (; it.rem; xcb_window_next(&it))
229             screen_add_window(sc, *it.data);
230
231         free(rep);
232     }
233 }
234
235 void
236 screen_remove_window(d_screen_t *sc, struct d_window *w)
237 {
238     //printf("screen removed window 0x%x\n", w->id);
239
240     window_destroy_damage(w);
241     g_hash_table_remove(sc->winhash, &w->id);
242     sc->window_become_zombie(w);
243     window_unref(w);
244 }
245
246 d_window_t*
247 screen_find_window(d_screen_t *sc, xcb_window_t id)
248 {
249     return g_hash_table_lookup(sc->winhash, &id);
250 }
251
252 static xcb_timestamp_t
253 screen_timestamp(d_screen_t *sc)
254 {
255     xcb_void_cookie_t ck;
256     xcb_timestamp_t   time;
257
258     ck = xcb_change_property(sc->dpy->conn, XCB_PROP_MODE_REPLACE, sc->selwin,
259                              sc->selatom, sc->selatom, 32, 0, NULL);
260     xcb_flush(sc->dpy->conn);
261     while (1) {
262         xcb_generic_event_t *ev;
263
264         ev = xcb_wait_for_event(sc->dpy->conn);
265         if (!ev) {
266             printf(_("IO error\n"));
267             exit(0);
268         }
269
270         /* expect errors.. */
271         if (!ev->response_type) {
272             display_error(sc->dpy, (xcb_generic_error_t*)ev);
273             free(ev);
274             continue;
275         }
276
277         if (ev->response_type == XCB_PROPERTY_NOTIFY &&
278             ev->full_sequence == ck.sequence)
279         {
280             time = ((xcb_property_notify_event_t*)ev)->time;
281             free(ev);
282             break;
283         }
284     }
285     printf("created timestamp %lu\n", (unsigned long) time);
286     return time;
287 }
288
289 void
290 screen_stacking_add(d_screen_t *sc, struct d_window *w)
291 {
292     list_prepend(sc->stacking, w);
293 }
294
295 void
296 screen_stacking_remove(d_screen_t *sc, struct d_window *w)
297 {
298     list_remove(sc->stacking, w);
299 }
300
301 void
302 screen_stacking_move_above(d_screen_t *sc, struct d_window *w,
303                            struct d_window *above)
304 {
305     d_list_it_t *wit = list_find(sc->stacking, w);
306     d_list_it_t *ait = list_find(sc->stacking, above);
307     list_move_before(sc->stacking, wit, ait);
308 }
309
310 static void
311 screen_set_next_repaint(d_screen_t *sc)
312 {
313     gettimeofday(&sc->next_repaint, NULL);
314     /* add time for the refresh rate (60 hz) */
315     time_add(&sc->next_repaint, 1000000/60);
316     sc->need_repaint = FALSE;
317 }
318
319 void
320 screen_setup_default_functions(d_screen_t *sc)
321 {
322     sc->screen_paint = screen_set_next_repaint;
323     sc->window_show = window_show;
324     sc->window_hide = window_hide;
325     sc->window_become_zombie = window_become_zombie;
326     sc->window_reconfigure = window_reconfigure;
327 }
328
329 void
330 screen_add_plugin_data(d_screen_t *sc, const char *key, void *data)
331 {
332     char *skey = g_strdup(key);
333     g_hash_table_replace(sc->plugin_data, skey, data);
334 }
335
336 void*
337 screen_find_plugin_data(d_screen_t *sc, const char *key)
338 {
339     return g_hash_table_lookup(sc->plugin_data, key);
340 }
341
342 void
343 screen_remove_plugin_data(d_screen_t *sc, const char *key)
344 {
345     g_hash_table_remove(sc->plugin_data, key);
346 }
347
348 void screen_refresh(d_screen_t *sc)
349 {
350     sc->need_repaint = TRUE;
351     //printf("*** need repaint! ***\n");
352 }