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