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