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