setting up architecture for plugins. have a render-based drawer that doesn't do...
[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
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 static void screen_set_next_repaint(d_screen_t *sc);
22
23 gboolean
24 screen_register(struct d_display *dpy, int num, d_screen_t *sc)
25 {
26     char *name;
27     xcb_window_t w;
28     xcb_intern_atom_cookie_t ack;
29     xcb_intern_atom_reply_t *arep;
30     xcb_get_selection_owner_cookie_t sck;
31     xcb_get_selection_owner_reply_t *srep;
32     uint32_t event_mask;
33     gboolean taken, ret;
34
35     sc->dpy = dpy;
36     sc->num = num;
37     w = xcb_generate_id(sc->dpy->conn);
38     event_mask = SELECTION_MASK;
39     xcb_create_window(sc->dpy->conn, XCB_COPY_FROM_PARENT, w, sc->super.root,
40                       0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_ONLY,
41                       sc->super.root_visual, XCB_CW_EVENT_MASK, &event_mask);
42
43     name = g_strdup_printf("_NET_WM_CM_S%d", sc->num);
44     ack = xcb_intern_atom(sc->dpy->conn, FALSE, strlen(name), name);
45     arep = xcb_intern_atom_reply(sc->dpy->conn, ack, NULL);
46     g_free(name);
47
48     xcb_grab_server(sc->dpy->conn);
49
50     sck = xcb_get_selection_owner(sc->dpy->conn, arep->atom);
51     srep = xcb_get_selection_owner_reply(sc->dpy->conn, sck, NULL);
52     taken = !!srep->owner;
53     free(srep);
54     if (taken) {
55         printf(_("screen %d already has a composite manager, skipping\n"),
56                sc->num);
57         ret = FALSE;
58     }
59     else {
60         xcb_timestamp_t time;
61
62         sc->selwin = w;
63         sc->selatom = arep->atom;
64
65         time = screen_timestamp(sc);
66
67         xcb_set_selection_owner(sc->dpy->conn, w, arep->atom, time);
68         sck = xcb_get_selection_owner(sc->dpy->conn, arep->atom);
69         srep = xcb_get_selection_owner_reply(sc->dpy->conn, sck, NULL);
70         taken = srep->owner == w;
71         if (taken && screen_init(sc)) {
72             screen_add_existing_windows(sc);
73             ret = TRUE;
74         }
75         else {
76             xcb_destroy_window(sc->dpy->conn, w);
77             ret = FALSE;
78         }
79     }
80
81     xcb_ungrab_server(sc->dpy->conn);
82     xcb_flush(sc->dpy->conn);
83     return ret;
84 }
85
86 static guint
87 xcb_window_hash(xcb_window_t *w) { return *w; }
88
89 static gboolean
90 xcb_window_equal(xcb_window_t *w1, xcb_window_t *w2) { return *w1 == *w2; }
91
92 static gboolean
93 screen_init(d_screen_t *sc)
94 {
95     uint32_t mask;
96 #if DO_COMP
97     xcb_generic_error_t *err;
98     xcb_void_cookie_t redir_ck;
99     xcb_composite_get_overlay_window_cookie_t overlay_ck;
100     xcb_composite_get_overlay_window_reply_t *overlay_rep;
101
102     redir_ck =
103         xcb_composite_redirect_subwindows(sc->dpy->conn, sc->super.root,
104                                           XCB_COMPOSITE_REDIRECT_MANUAL);
105
106     overlay_ck = xcb_composite_get_overlay_window(sc->dpy->conn,
107                                                   sc->super.root);
108
109     /* check that the redirect worked */
110     err = xcb_request_check(sc->dpy->conn, redir_ck);
111     if (err) {
112         printf(_("unable to redirect rendering, another composite manager must be running"));
113         free(err);
114         return FALSE;
115     }
116
117     /* get the overlay window reply */
118     overlay_rep = xcb_composite_get_overlay_window_reply(sc->dpy->conn,
119                                                          overlay_ck,
120                                                          NULL);
121     if (!overlay_rep) {
122         printf(_("unable to get the composite overlay window\n"));
123         return FALSE;
124     }
125     sc->overlay = overlay_rep->overlay_win;
126     free(overlay_rep);
127 #endif
128
129     mask = SELECTION_MASK;
130     xcb_change_window_attributes(sc->dpy->conn, sc->selwin,
131                                  XCB_CW_EVENT_MASK, &mask);
132     mask = ROOT_MASK;
133     xcb_change_window_attributes(sc->dpy->conn, sc->super.root,
134                                  XCB_CW_EVENT_MASK, &mask);
135
136     gettimeofday(&sc->next_repaint, NULL);
137
138     sc->winhash = g_hash_table_new((GHashFunc)xcb_window_hash,
139                                    (GEqualFunc)xcb_window_equal);
140     sc->stacking = list_new();
141     sc->plugin_data = g_hash_table_new_full((GHashFunc)g_str_hash,
142                                             (GEqualFunc)g_str_equal,
143                                             g_free, NULL);
144
145     return TRUE;
146 }
147
148 void screen_free(d_screen_t *sc)
149 {
150     g_hash_table_unref(sc->winhash);
151     list_unref(sc->stacking);
152     g_hash_table_unref(sc->plugin_data);
153 }
154
155 void
156 screen_add_window(d_screen_t *sc, xcb_window_t wid)
157 {
158     d_window_t *w;
159
160     /* XXX xgetwindowattributes - size and tings */
161
162     w = window_new(wid, sc);
163     g_hash_table_insert(sc->winhash, &w->id, w);
164
165     printf("screen added window 0x%x\n", w->id);
166 }
167
168 static void
169 screen_add_existing_windows(d_screen_t *sc)
170 {
171     xcb_query_tree_cookie_t ck;
172     xcb_query_tree_reply_t *rep;
173
174     ck = xcb_query_tree(sc->dpy->conn, sc->super.root);
175     rep = xcb_query_tree_reply(sc->dpy->conn, ck, NULL);
176     if (rep) {
177         xcb_window_iterator_t it;
178
179         printf("query\n");
180         it = xcb_query_tree_children_iterator(rep);
181         for (; it.rem; xcb_window_next(&it))
182             screen_add_window(sc, *it.data);
183
184         free(rep);
185     }
186 }
187
188 void
189 screen_remove_window(d_screen_t *sc, struct d_window *w)
190 {
191     printf("screen removed window 0x%x\n", w->id);
192
193     g_hash_table_remove(sc->winhash, &w->id);
194     sc->window_become_zombie(w);
195     window_unref(w);
196 }
197
198 d_window_t*
199 screen_find_window(d_screen_t *sc, xcb_window_t id)
200 {
201     return g_hash_table_lookup(sc->winhash, &id);
202 }
203
204 static xcb_timestamp_t
205 screen_timestamp(d_screen_t *sc)
206 {
207     xcb_void_cookie_t ck;
208     xcb_timestamp_t   time;
209
210     ck = xcb_change_property(sc->dpy->conn, XCB_PROP_MODE_REPLACE, sc->selwin,
211                              sc->selatom, sc->selatom, 32, 0, NULL);
212     xcb_flush(sc->dpy->conn);
213     while (1) {
214         xcb_generic_event_t *ev;
215
216         ev = xcb_wait_for_event(sc->dpy->conn);
217         if (!ev) {
218             printf(_("IO error\n"));
219             exit(0);
220         }
221
222         /* expect errors.. */
223         if (!ev->response_type) {
224             display_error(sc->dpy, (xcb_generic_error_t*)ev);
225             free(ev);
226             continue;
227         }
228
229         if (ev->response_type == XCB_PROPERTY_NOTIFY &&
230             ev->full_sequence == ck.sequence)
231         {
232             time = ((xcb_property_notify_event_t*)ev)->time;
233             free(ev);
234             break;
235         }
236     }
237     printf("created timestamp %lu\n", (unsigned long) time);
238     return time;
239 }
240
241 void
242 screen_stacking_add(d_screen_t *sc, struct d_window *w)
243 {
244     list_prepend(sc->stacking, w);
245 }
246
247 void
248 screen_stacking_remove(d_screen_t *sc, struct d_window *w)
249 {
250     list_remove(sc->stacking, w);
251 }
252
253 static void
254 screen_set_next_repaint(d_screen_t *sc)
255 {
256     gettimeofday(&sc->next_repaint, NULL);
257     /* add time for the refresh rate (60 hz) */
258     time_add(&sc->next_repaint, 1000000/60);
259 }
260
261 void
262 screen_setup_default_functions(d_screen_t *sc)
263 {
264     sc->screen_paint = screen_set_next_repaint;
265     sc->window_show = window_show;
266     sc->window_hide = window_hide;
267     sc->window_become_zombie = window_become_zombie;
268
269 }
270
271 void
272 screen_add_plugin_data(d_screen_t *sc, const char *key, void *data)
273 {
274     char *skey = g_strdup(key);
275     g_hash_table_insert(sc->plugin_data, skey, data);
276 }
277
278 void*
279 screen_find_plugin_data(d_screen_t *sc, const char *key)
280 {
281     return g_hash_table_lookup(sc->plugin_data, key);
282 }
283
284 void
285 screen_remove_plugin_data(d_screen_t *sc, const char *key)
286 {
287     g_hash_table_remove(sc->plugin_data, key);
288 }