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