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