e238bc7680c0ad9c050b482ab24b0cc9db887c68
[dana/dcompmgr.git] / screen.c
1 #include "screen.h"
2 #include "display.h"
3 #include "list.h"
4 #include "window.h"
5 #include "render.h"
6 #include "gettext.h"
7 #include <string.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <xcb/composite.h>
11
12 #define ROOT_MASK      (XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | \
13                         XCB_EVENT_MASK_STRUCTURE_NOTIFY)
14
15 #define SELECTION_MASK (XCB_EVENT_MASK_STRUCTURE_NOTIFY | \
16                         XCB_EVENT_MASK_PROPERTY_CHANGE)
17
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
22 gboolean
23 screen_register(struct d_display *dpy, int num, d_screen_t *sc)
24 {
25     char *name;
26     xcb_window_t w;
27     xcb_intern_atom_cookie_t ack;
28     xcb_intern_atom_reply_t *arep;
29     xcb_get_selection_owner_cookie_t sck;
30     xcb_get_selection_owner_reply_t *srep;
31     uint32_t event_mask;
32     gboolean taken, ret;
33
34     sc->dpy = dpy;
35     sc->num = num;
36     w = xcb_generate_id(sc->dpy->conn);
37     event_mask = SELECTION_MASK;
38     xcb_create_window(sc->dpy->conn, XCB_COPY_FROM_PARENT, w, sc->super.root,
39                       0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_ONLY,
40                       sc->super.root_visual, XCB_CW_EVENT_MASK, &event_mask);
41
42     name = g_strdup_printf("_NET_WM_CM_S%d", sc->num);
43     ack = xcb_intern_atom(sc->dpy->conn, FALSE, strlen(name), name);
44     arep = xcb_intern_atom_reply(sc->dpy->conn, ack, NULL);
45     g_free(name);
46
47     xcb_grab_server(sc->dpy->conn);
48
49     sck = xcb_get_selection_owner(sc->dpy->conn, arep->atom);
50     srep = xcb_get_selection_owner_reply(sc->dpy->conn, sck, NULL);
51     taken = !!srep->owner;
52     free(srep);
53     if (taken) {
54         printf(_("screen %d already has a composite manager, skipping\n"),
55                sc->num);
56         ret = FALSE;
57     }
58     else {
59         xcb_timestamp_t time;
60
61         sc->selwin = w;
62         sc->selatom = arep->atom;
63
64         time = screen_timestamp(sc);
65
66         xcb_set_selection_owner(sc->dpy->conn, w, arep->atom, time);
67         sck = xcb_get_selection_owner(sc->dpy->conn, arep->atom);
68         srep = xcb_get_selection_owner_reply(sc->dpy->conn, sck, NULL);
69         taken = srep->owner == w;
70         if (taken && screen_init(sc)) {
71             screen_add_existing_windows(sc);
72             ret = TRUE;
73         }
74         else {
75             xcb_destroy_window(sc->dpy->conn, w);
76             ret = FALSE;
77         }
78     }
79
80     xcb_ungrab_server(sc->dpy->conn);
81     xcb_flush(sc->dpy->conn);
82     return ret;
83 }
84
85 static guint
86 xcb_window_hash(xcb_window_t *w) { return *w; }
87
88 static gboolean
89 xcb_window_compare(xcb_window_t *w1, xcb_window_t *w2) { return *w1 == *w2; }
90
91 static gboolean
92 screen_init(d_screen_t *sc)
93 {
94     uint32_t mask;
95     xcb_generic_error_t *err;
96     xcb_void_cookie_t redir_ck;
97     xcb_composite_get_overlay_window_cookie_t overlay_ck;
98     xcb_composite_get_overlay_window_reply_t *overlay_rep;
99
100     redir_ck =
101         xcb_composite_redirect_subwindows(sc->dpy->conn, sc->super.root,
102                                           XCB_COMPOSITE_REDIRECT_MANUAL);
103
104     overlay_ck = xcb_composite_get_overlay_window(sc->dpy->conn,
105                                                   sc->super.root);
106
107     /* check that the redirect worked */
108     err = xcb_request_check(sc->dpy->conn, redir_ck);
109     if (err) {
110         printf(_("unable to redirect rendering, another composite manager must be running"));
111         free(err);
112         return FALSE;
113     }
114
115     /* get the overlay window reply */
116     overlay_rep = xcb_composite_get_overlay_window_reply(sc->dpy->conn,
117                                                          overlay_ck,
118                                                          NULL);
119     if (!overlay_rep) {
120         printf(_("unable to get the composite overlay window\n"));
121         return FALSE;
122     }
123     sc->overlay = overlay_rep->overlay_win;
124     free(overlay_rep);
125
126     mask = SELECTION_MASK;
127     xcb_change_window_attributes(sc->dpy->conn, sc->selwin,
128                                  XCB_CW_EVENT_MASK, &mask);
129     mask = ROOT_MASK;
130     xcb_change_window_attributes(sc->dpy->conn, sc->super.root,
131                                  XCB_CW_EVENT_MASK, &mask);
132
133     /* use the render backend */
134     sc->paint = render_paint_screen;
135
136     sc->winhash = g_hash_table_new((GHashFunc)xcb_window_hash,
137                                    (GCompareFunc)xcb_window_compare);
138     sc->stacking = list_new();
139
140     return TRUE;
141 }
142
143 void screen_free(d_screen_t *sc)
144 {
145     g_hash_table_unref(sc->winhash);
146     list_unref(sc->stacking);
147 }
148
149 void
150 screen_add_window(d_screen_t *sc, xcb_window_t wid)
151 {
152     d_window_t *w;
153
154     /* XXX xgetwindowattributes - size and tings */
155
156     w = window_new(wid, sc);
157     g_hash_table_insert(sc->winhash, &w->id, w);
158
159     printf("screen added window 0x%x\n", w->id);
160 }
161
162 static void
163 screen_add_existing_windows(d_screen_t *sc)
164 {
165     xcb_query_tree_cookie_t ck;
166     xcb_query_tree_reply_t *rep;
167
168     ck = xcb_query_tree(sc->dpy->conn, sc->super.root);
169     rep = xcb_query_tree_reply(sc->dpy->conn, ck, NULL);
170     if (rep) {
171         xcb_window_iterator_t it;
172
173         printf("query\n");
174         it = xcb_query_tree_children_iterator(rep);
175         for (; it.rem; xcb_window_next(&it))
176             screen_add_window(sc, *it.data);
177
178         free(rep);
179     }
180 }
181
182 void
183 screen_remove_window(d_screen_t *sc, struct d_window *w)
184 {
185     printf("screen removed window 0x%x\n", w->id);
186
187     g_hash_table_remove(sc->winhash, &w->id);
188     w->become_zombie(w);
189     window_unref(w);
190 }
191
192 d_window_t*
193 screen_find_window(d_screen_t *sc, xcb_window_t id)
194 {
195     return g_hash_table_lookup(sc->winhash, &id);
196 }
197
198 static xcb_timestamp_t
199 screen_timestamp(d_screen_t *sc)
200 {
201     xcb_void_cookie_t ck;
202     xcb_timestamp_t   time;
203
204     ck = xcb_change_property(sc->dpy->conn, XCB_PROP_MODE_REPLACE, sc->selwin,
205                              sc->selatom, sc->selatom, 32, 0, NULL);
206     xcb_flush(sc->dpy->conn);
207     while (1) {
208         xcb_generic_event_t *ev;
209
210         ev = xcb_wait_for_event(sc->dpy->conn);
211         if (!ev) {
212             printf(_("IO error\n"));
213             exit(0);
214         }
215
216         /* expect errors.. */
217         if (!ev->response_type) {
218             display_error(sc->dpy, (xcb_generic_error_t*)ev);
219             free(ev);
220             continue;
221         }
222
223         if (ev->response_type == XCB_PROPERTY_NOTIFY &&
224             ev->full_sequence == ck.sequence)
225         {
226             time = ((xcb_property_notify_event_t*)ev)->time;
227             free(ev);
228             break;
229         }
230     }
231     printf("created timestamp %lu\n", (unsigned long) time);
232     return time;
233 }
234
235 void
236 screen_stacking_add(d_screen_t *sc, struct d_window *w)
237 {
238     list_prepend(sc->stacking, w);
239 }
240
241 void
242 screen_stacking_remove(d_screen_t *sc, struct d_window *w)
243 {
244     list_remove(sc->stacking, w);
245 }