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