make rendering much faster
[dana/dcompmgr.git] / dcompmgr.c
1 #include "screen.h"
2 #include "window.h"
3 #include "list.h"
4 #include "display.h"
5 #include "gettext.h"
6 #include "time.h"
7 #include "render.h"
8
9 #include <glib.h>
10 #include <stdio.h>
11 #include <sys/select.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <xcb/xcb.h>
15 #include <xcb/damage.h>
16
17 typedef struct {
18     int foo;
19 } d_options_t;
20
21 static void
22 read_options(int argc, char **argv, d_options_t *opts)
23 {
24     opts->foo = argc && argv;
25 }
26
27 static void
28 event(d_display_t *dpy)
29 {
30     xcb_generic_event_t *ev;
31
32     while ((ev = xcb_poll_for_event(dpy->conn))) {
33         //printf("event %d\n", ev->response_type);
34
35         if (!ev->response_type) {
36             display_error(dpy, (xcb_generic_error_t*)ev);
37             free(ev);
38             continue;
39         }
40
41         switch (ev->response_type) {
42         case XCB_CREATE_NOTIFY:
43         {
44             xcb_create_notify_event_t *cev;
45             d_screen_t *sc;
46
47             cev = (xcb_create_notify_event_t*)ev;
48             sc = display_screen_from_root(dpy, cev->parent);
49             if (!sc) break;
50             screen_add_window(sc, cev->window);
51             break;
52         }
53         case XCB_DESTROY_NOTIFY:
54         {
55             xcb_destroy_notify_event_t *dev;
56             d_screen_t *sc;
57             d_window_t *w;
58             gboolean vis;
59
60             dev = (xcb_destroy_notify_event_t*)ev;
61             sc = display_screen_from_root(dpy, dev->event);
62             if (!sc) break;
63             w = screen_find_window(sc, dev->window);
64             vis = window_is_mapped(w);
65             sc->window_hide(w);
66             screen_remove_window(sc, w);
67             if (vis) screen_refresh(sc);
68             break;
69         }
70         case XCB_REPARENT_NOTIFY:
71         {
72             xcb_reparent_notify_event_t *rev;
73             d_screen_t *sc;
74             d_window_t *w;
75
76             rev = (xcb_reparent_notify_event_t*)ev;
77             sc = display_screen_from_root(dpy, rev->event);
78             if (!sc) break;
79             w = screen_find_window(sc, rev->window);
80             if (rev->parent == sc->super.root)
81                 screen_add_window(sc, rev->window);
82             else {
83                 sc->window_hide(w);
84                 screen_remove_window(sc, w);
85             }
86             screen_refresh(w->sc);
87             break;
88         }
89         case XCB_MAP_NOTIFY:
90         {
91             xcb_map_notify_event_t *mev;
92             d_screen_t *sc;
93             d_window_t *w;
94
95             mev = (xcb_map_notify_event_t*)ev;
96             sc = display_screen_from_root(dpy, mev->event);
97             if (!sc) break;
98             w = screen_find_window(sc, mev->window);
99             sc->window_show(w);
100             screen_refresh(w->sc);
101             break;
102         }
103         case XCB_UNMAP_NOTIFY:
104         {
105             xcb_unmap_notify_event_t *mev;
106             d_screen_t *sc;
107             d_window_t *w;
108
109             mev = (xcb_unmap_notify_event_t*)ev;
110             sc = display_screen_from_root(dpy, mev->event);
111             if (!sc) break;
112             w = screen_find_window(sc, mev->window);
113             sc->window_hide(w);
114             screen_refresh(w->sc);
115             break;
116         }
117         case XCB_CONFIGURE_NOTIFY:
118         {
119             xcb_configure_notify_event_t *cev;
120             d_screen_t *sc;
121             d_window_t *w, *above;
122             int x, y, width, height, bwidth;
123
124             cev = (xcb_configure_notify_event_t*)ev;
125             sc = display_screen_from_root(dpy, cev->event);
126             if (!sc) break;
127             w = screen_find_window(sc, cev->window);
128             window_get_area(w, &x, &y, &width, &height, &bwidth);
129             if (x != cev->x || y != cev->y || width != cev->width ||
130                 height != cev->height || bwidth != cev->border_width)
131             {
132                 window_configure(w, cev->x, cev->y,
133                                  cev->width, cev->height,
134                                  cev->border_width);
135                 if (window_is_mapped(w) &&
136                     (width != cev->width ||
137                      height != cev->height || bwidth != cev->border_width))
138                     sc->window_resize(w);
139             }
140             above = screen_find_window(sc, cev->above_sibling);
141             screen_stacking_move_above(sc, w, above);
142             screen_refresh(w->sc);
143             break;
144         }
145         default:
146             if (ev->response_type - dpy->damage.event == XCB_DAMAGE_NOTIFY) {
147                 xcb_damage_notify_event_t *dev;
148                 d_list_it_t *it;
149
150                 dev = (xcb_damage_notify_event_t*)ev;
151                 for (it = list_top(dpy->screens); it; it = it->next) {
152                     d_screen_t *sc = it->data;
153                     d_window_t *w;
154
155                     w = screen_find_window(sc, dev->drawable);
156                     if (w) {
157                         screen_refresh(w->sc);
158                         break;
159                     }
160                 }
161                 xcb_damage_subtract(dpy->conn, dev->damage,
162                                     XCB_NONE, XCB_NONE);
163             }
164             break;
165         }
166         free(ev);
167         xcb_flush(dpy->conn);
168     }
169 }
170
171 static void
172 paint(d_display_t *dpy)
173 {
174     d_list_it_t *it;
175     struct timeval now;
176
177     gettimeofday(&now, NULL);
178     
179     for (it = list_top(dpy->screens); it; it = it->next) {
180         d_screen_t *sc = it->data;
181
182         if (time_compare(&sc->next_repaint, &now) <= 0)
183             sc->screen_paint(sc);
184     }
185 }
186
187 static void
188 run(d_display_t *dpy)
189 {
190     gboolean quit;
191
192     quit = FALSE;
193     while (!quit) {
194         struct timeval next, now, *wait;
195         int            r, npaint;
196         d_list_it_t   *it;
197         fd_set         fds;
198
199         event(dpy);
200
201         npaint = 0;
202         for (it = list_top(dpy->screens); it; it = it->next) {
203             d_screen_t *sc = it->data;
204             if (sc->need_repaint &&
205                 (!npaint || time_compare(&sc->next_repaint, &next) < 0))
206             {
207                 next = sc->next_repaint;
208                 ++npaint;
209             }
210         }
211
212         gettimeofday(&now, 0);
213
214         if (!npaint)
215             /* wait forever, there is nothing that needs drawing */
216             wait = NULL;
217         else if (time_compare(&next, &now) > 0) {
218             /* wait until the next allowed redraw time */
219             time_difference(&next, &now, &next);
220             wait = &next;
221         }
222         else {
223             /* don't wait cuz a redraw is due now already */
224             next.tv_sec = 0;
225             next.tv_usec = 1;
226             wait = &next;
227         }
228
229         FD_ZERO(&fds);
230         FD_SET(dpy->fd, &fds);
231
232         //printf("select %d\n", npaint);
233
234         r = select(dpy->fd+1, &fds, NULL, NULL, wait);
235         if (r < 0)
236             printf("select error\n");
237         else if (r == 0) {
238             //printf("select timeout\n");
239             paint(dpy);
240             xcb_flush(dpy->conn);
241         }
242
243         if (xcb_connection_has_error(dpy->conn))
244             quit = TRUE;
245     }
246 }
247
248 static void
249 setup_functions(d_display_t *dpy)
250 {
251     d_list_it_t *it;
252
253     for (it = list_top(dpy->screens); it; it = it->next) {
254         d_screen_t *sc = it->data;
255         int id;
256         screen_setup_default_functions(sc);
257
258         /* these can be plugins.. */
259         id = 1;
260         render_init(sc, id++);
261     }
262 }
263
264 static void
265 cleanup_functions(d_display_t *dpy)
266 {
267     d_list_it_t *it;
268
269     for (it = list_top(dpy->screens); it; it = it->next) {
270         d_screen_t *sc = it->data;
271
272         /* these can be plugins.. */
273         render_free(sc);
274     }
275 }
276
277 int
278 main(int argc, char **argv)
279 {
280     d_display_t         *dpy;
281     d_options_t          opts;
282
283     read_options(argc, argv, &opts);
284
285     dpy = display_open(NULL);
286     if (!dpy) {
287         printf(_("Unable to connect to display\n"));
288         return 1;
289     }
290
291     if (!dpy->composite.present) {
292         printf(_("no composite extension present on the display\n"));
293         display_unref(dpy);
294         return 1;
295     }
296     if (!dpy->xfixes.present) {
297         printf(_("no xfixes extension present on the display\n"));
298         display_unref(dpy);
299         return 1;
300     }
301     if (!dpy->damage.present) {
302         printf(_("no damage extension present on the display\n"));
303         display_unref(dpy);
304         return 1;
305     }
306     if (!dpy->render.present) {
307         printf(_("no render extension present on the display\n"));
308         display_unref(dpy);
309         return 1;
310     }
311     if (dpy->composite.major_version <= 0 && dpy->composite.minor_version < 3)
312     {
313         printf(_("composite extension does not support the overlay window"));
314         display_unref(dpy);
315         return 1;
316     }
317
318     if (!display_claim_screens(dpy)) {
319         printf(_("found no screens to run on\n"));
320         display_unref(dpy);
321         return 0;
322     }
323
324     setup_functions(dpy);
325
326     {
327         /* some of the windows may already be visible */
328         d_list_it_t *sc_it;
329
330         for (sc_it = list_top(dpy->screens); sc_it; sc_it = sc_it->next) {
331             d_screen_t *sc = sc_it->data;
332             d_list_it_t *it;
333             for (it = list_top(sc->stacking); it; it = it->next)
334                 if (window_is_mapped(it->data))
335                     sc->window_show(it->data);
336         }
337     }
338
339     run(dpy);
340
341     cleanup_functions(dpy);
342
343     display_unref(dpy);
344     return 0;
345 }