save the window's geometry when fetching the window's region so that if it changes...
[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
10 /* these can be plugins */
11 #include "render.h"
12 #include "glxrender.h"
13 #include "fade.h"
14
15 #include <glib.h>
16 #include <stdio.h>
17 #include <sys/select.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <signal.h>
21 #include <xcb/xcb.h>
22 #include <xcb/damage.h>
23 #include <xcb/shape.h>
24
25 typedef enum {
26     USE_RENDER,
27     USE_OPENGL
28 } render_type_t;
29
30 typedef struct {
31     render_type_t render;
32 } d_options_t;
33
34 static gboolean quit = FALSE;
35 static gboolean running = FALSE;
36
37 static void
38 print_help(void)
39 {
40     const char *s =
41 "\n"
42 "Usage: dcompmgr [OPTIONS]\n"
43 "\n"
44 "Options:\n"
45 "  --gl          Use OpenGL for drawing\n"
46 "  --xrender     Use XRender for drawing (the default)\n"
47 "\n"
48 "Copyright (C) 2008    Dana Jansens\n"
49 "\n"
50 "";
51     printf(s);
52 }
53
54 static void
55 read_options(int argc, char **argv, d_options_t *opts)
56 {
57     int i;
58
59     opts->render = USE_RENDER;
60
61     for (i = 1; i < argc; ++i) {
62         if (strcmp(argv[i], "--gl") == 0)
63             opts->render = USE_OPENGL;
64         else if (strcmp(argv[i], "--xrender") == 0)
65             opts->render = USE_RENDER;
66         else if (strcmp(argv[i], "--help") == 0) {
67             print_help();
68             exit(0);
69         }
70     }
71 }
72
73 static void
74 signal_quit_handler(int sig)
75 {
76     printf("caught signal %d, quitting\n", sig);
77     quit = TRUE;
78 }
79
80 static void
81 event(d_display_t *dpy)
82 {
83     xcb_generic_event_t *ev;
84
85     while ((ev = xcb_poll_for_event(dpy->conn)) && !quit) {
86         //printf("event %d\n", ev->response_type);
87
88         if (!ev->response_type) {
89             display_error(dpy, (xcb_generic_error_t*)ev);
90             free(ev);
91             continue;
92         }
93
94         switch (ev->response_type) {
95         case XCB_CREATE_NOTIFY:
96         {
97             xcb_create_notify_event_t *cev;
98             d_screen_t *sc;
99             d_window_t *w;
100
101             cev = (xcb_create_notify_event_t*)ev;
102             sc = display_screen_from_root(dpy, cev->parent);
103             if (!sc) break;
104             w = screen_add_window(sc, cev->window);
105             break;
106         }
107         case XCB_DESTROY_NOTIFY:
108         {
109             xcb_destroy_notify_event_t *dev;
110             d_screen_t *sc;
111             d_window_t *w;
112             gboolean vis;
113
114             dev = (xcb_destroy_notify_event_t*)ev;
115             sc = display_screen_from_root(dpy, dev->event);
116             if (!sc) break;
117             w = screen_find_window(sc, dev->window);
118             vis = window_is_mapped(w);
119             if (vis) {
120                 sc->window_become_zombie(w);
121                 sc->window_hide(w);
122             }
123             screen_remove_window(sc, w);
124             if (vis) screen_refresh(sc);
125             break;
126         }
127         case XCB_REPARENT_NOTIFY:
128         {
129             xcb_reparent_notify_event_t *rev;
130             d_screen_t *sc;
131             d_window_t *w;
132
133             rev = (xcb_reparent_notify_event_t*)ev;
134             sc = display_screen_from_root(dpy, rev->event);
135             if (!sc) break;
136             w = screen_find_window(sc, rev->window);
137             if (rev->parent == sc->super->root)
138                 screen_add_window(sc, rev->window);
139             else {
140                 if (window_is_mapped(w)) {
141                     sc->window_become_zombie(w);
142                     sc->window_hide(w);
143                 }
144                 screen_remove_window(sc, w);
145             }
146             screen_refresh(sc);
147             break;
148         }
149         case XCB_MAP_NOTIFY:
150         {
151             xcb_map_notify_event_t *mev;
152             d_screen_t *sc;
153             d_window_t *w;
154
155             mev = (xcb_map_notify_event_t*)ev;
156             sc = display_screen_from_root(dpy, mev->event);
157             if (!sc) break;
158             w = screen_find_window(sc, mev->window);
159             sc->window_show(w);
160             screen_refresh(w->sc);
161             break;
162         }
163         case XCB_UNMAP_NOTIFY:
164         {
165             xcb_unmap_notify_event_t *mev;
166             d_screen_t *sc;
167             d_window_t *w;
168
169             mev = (xcb_unmap_notify_event_t*)ev;
170             sc = display_screen_from_root(dpy, mev->event);
171             if (!sc) break;
172             w = screen_find_window(sc, mev->window);
173             sc->window_become_zombie(w);
174             sc->window_hide(w);
175             screen_refresh(w->sc);
176             break;
177         }
178         case XCB_CIRCULATE_NOTIFY:
179         {
180             xcb_circulate_notify_event_t *cev;
181             d_screen_t *sc;
182             d_window_t *w;
183
184             cev = (xcb_circulate_notify_event_t*)ev;
185             sc = display_screen_from_root(dpy, cev->event);
186             if (!sc) break;
187             w = screen_find_window(sc, cev->window);
188             if (cev->place == XCB_PLACE_ON_TOP)
189                 screen_stacking_move_to_top(sc, w);
190             else
191                 screen_stacking_move_to_bottom(sc, w);
192             screen_refresh(w->sc);
193         }
194         case XCB_CONFIGURE_NOTIFY:
195         {
196             xcb_configure_notify_event_t *cev;
197             d_screen_t *sc;
198             d_window_t *w, *above;
199             int x, y, width, height, bwidth;
200
201             cev = (xcb_configure_notify_event_t*)ev;
202             sc = display_screen_from_root(dpy, cev->event);
203             if (!sc) break;
204             //printf("configure 0x%x", cev->window);
205             w = screen_find_window(sc, cev->window);
206             window_get_area(w, &x, &y, &width, &height, &bwidth);
207             if (x != cev->x || y != cev->y || width != cev->width ||
208                 height != cev->height || bwidth != cev->border_width)
209             {
210                 window_configure(w, cev->x, cev->y,
211                                  cev->width, cev->height,
212                                  cev->border_width);
213                 if (window_is_mapped(w)) {
214                     if (x != cev->x || y != cev->y)
215                         sc->window_move(w);
216                     if (width != cev->width ||
217                         height != cev->height || bwidth != cev->border_width)
218                         sc->window_resize(w);
219                 }
220             }
221             above = screen_find_window(sc, cev->above_sibling);
222             sc->window_restack(w, above);
223             if (window_is_mapped(w))
224                 screen_refresh(w->sc);
225             break;
226         }
227         case XCB_PROPERTY_NOTIFY:
228         {
229             xcb_property_notify_event_t *pev;
230
231             pev = (xcb_property_notify_event_t*)ev;
232             if (pev->atom == dpy->a.xrootpmap_id ||
233                 pev->atom == dpy->a.esetroot_pmap_id ||
234                 pev->atom == dpy->a.xsetroot_id)
235             {
236                 d_screen_t *sc;
237
238                 sc = display_screen_from_root(dpy, pev->window);
239                 if (sc) sc->screen_root_pixmap_change(sc);
240             }
241             else if (pev->atom == dpy->a.net_wm_window_opacity) {
242                 d_list_it_t *it;
243
244                 for (it = list_top(dpy->screens); it; it = it->next) {
245                     d_screen_t *sc = it->data;
246                     d_window_t *w;
247
248                     w = screen_find_window(sc, pev->window);
249                     if (w) {
250                         window_update_user_opacity(w);
251                         if (window_is_mapped(w)) {
252                             sc->window_opacity_change(w);
253                             screen_refresh(w->sc);
254                         }
255                     }
256                 }
257                     
258             }
259             break;
260         }
261         default:
262             if (ev->response_type - dpy->damage.event == XCB_DAMAGE_NOTIFY) {
263                 xcb_damage_notify_event_t *dev;
264                 d_list_it_t *it;
265
266                 dev = (xcb_damage_notify_event_t*)ev;
267                 for (it = list_top(dpy->screens); it; it = it->next) {
268                     d_screen_t *sc = it->data;
269                     d_window_t *w;
270
271                     w = screen_find_window(sc, dev->drawable);
272                     if (w) {
273                         sc->window_damage(w);
274                         screen_refresh(w->sc);
275                         break;
276                     }
277                 }
278                 xcb_damage_subtract(dpy->conn, dev->damage,
279                                     XCB_NONE, XCB_NONE);
280             }
281             else if (dpy->shape.present &&
282                      ev->response_type - dpy->shape.event == XCB_SHAPE_NOTIFY)
283             {
284                 xcb_shape_notify_event_t *sev;
285                 d_list_it_t *it;
286
287                 sev = (xcb_shape_notify_event_t*)ev;
288                 for (it = list_top(dpy->screens); it; it = it->next) {
289                     d_screen_t *sc = it->data;
290                     d_window_t *w;
291
292                     w = screen_find_window(sc, sev->affected_window);
293                     if (w && window_is_mapped(w)) {
294                         sc->window_reshape(w);
295                         screen_refresh(w->sc);
296                         break;
297                     }
298                 }
299             }
300             break;
301         }
302         free(ev);
303         xcb_flush(dpy->conn);
304     }
305 }
306
307 static void
308 timeouts(d_display_t *dpy, const struct timeval *now)
309 {
310     d_list_it_t *it;
311
312     for (it = list_top(dpy->screens); it; it = it->next) {
313         d_screen_t *sc = it->data;
314         struct timeval tv;
315
316         if (glxrender_next_timeout(sc, &tv) && time_compare(&tv, now) <= 0)
317             glxrender_timeout(sc, now);
318         if (render_next_timeout(sc, &tv) && time_compare(&tv, now) <= 0)
319             render_timeout(sc, now);
320         if (fade_next_timeout(sc, &tv) && time_compare(&tv, now) <= 0)
321             fade_timeout(sc, now);
322     }
323 }
324
325 static void
326 paint(d_display_t *dpy)
327 {
328     d_list_it_t *it;
329
330     for (it = list_top(dpy->screens); it; it = it->next) {
331         d_screen_t *sc = it->data;
332         if (sc->need_repaint)
333             sc->screen_paint(sc);
334     }
335 }
336
337 static void
338 run(d_display_t *dpy)
339 {
340     struct timeval now, next_repaint;
341
342     running = TRUE;
343
344     gettimeofday(&now, NULL);
345     next_repaint = now;
346
347     while (!quit) {
348         struct timeval next, *wait;
349         int            r, npaint, ntime;
350         d_list_it_t   *it;
351         fd_set         fds;
352         gboolean due;
353
354         event(dpy);
355
356         npaint = 0;
357         ntime = 0;
358         for (it = list_top(dpy->screens); it; it = it->next) {
359             d_screen_t *sc = it->data;
360             struct timeval next_timeout;
361
362             if (glxrender_next_timeout(sc, &next_timeout)) {
363                 if (!ntime || time_compare(&next_timeout, &next) < 0) {
364                     next = next_timeout;
365                     ++ntime;
366                 }
367             }
368             if (render_next_timeout(sc, &next_timeout)) {
369                 if (!ntime || time_compare(&next_timeout, &next) < 0) {
370                     next = next_timeout;
371                     ++ntime;
372                 }
373             }
374             if (fade_next_timeout(sc, &next_timeout)) {
375                 if (!ntime || time_compare(&next_timeout, &next) < 0) {
376                     next = next_timeout;
377                     ++ntime;
378                 }
379             }
380
381             if (sc->need_repaint)
382                 ++npaint;
383         }
384
385         if (npaint) {
386             if (!ntime || time_compare(&next_repaint, &next) < 0) {
387                 next = next_repaint;
388             }
389         }
390
391         if (!npaint && !ntime) {
392             /* wait forever, there is nothing that needs drawing */
393             wait = NULL;
394             due = FALSE;
395         }
396         else if (time_compare(&next, &now) > 0) {
397             /* wait until the next allowed redraw time */
398             time_difference(&next, &now, &next);
399             wait = &next;
400             due = FALSE;
401         }
402         else {
403             /* don't wait cuz a timer is due now already */
404             due = TRUE;
405         }
406
407         //printf("select? %d %d\n", due, npaint);
408
409         if (!due) {
410             FD_ZERO(&fds);
411             FD_SET(dpy->fd, &fds);
412             r = select(dpy->fd+1, &fds, NULL, NULL, wait);
413         }
414         else
415             r = 0;
416
417         gettimeofday(&now, NULL);
418         if (r == 0) {
419             //printf("select timeout\n");
420             timeouts(dpy, &now);
421             if (time_compare(&next_repaint, &now) <= 0) {
422                 paint(dpy);
423                 next_repaint = now;
424                 time_add(&next_repaint, 1000000/90); /* 60hz */
425             }
426             xcb_flush(dpy->conn);
427         }
428
429         if (xcb_connection_has_error(dpy->conn))
430             quit = TRUE;
431     }
432
433     running = FALSE;
434 }
435
436 static void
437 setup_functions(d_display_t *dpy, d_options_t *opts)
438 {
439     d_list_it_t *it;
440
441     for (it = list_top(dpy->screens); it; it = it->next) {
442         d_screen_t *sc = it->data;
443         int id;
444         screen_setup_default_functions(sc);
445
446         /* these can be plugins.. */
447         id = 1;
448
449         if (opts->render == USE_RENDER)
450             render_init(sc, id++);
451         else
452             glxrender_init(sc, id++);
453
454         fade_init(sc, id++);
455     }
456 }
457
458 static void
459 cleanup_functions(d_display_t *dpy)
460 {
461     d_list_it_t *it;
462
463     for (it = list_top(dpy->screens); it; it = it->next) {
464         d_screen_t *sc = it->data;
465
466         /* these can be plugins.. */
467         fade_free(sc);
468         render_free(sc);
469         glxrender_free(sc);
470     }
471 }
472
473 int
474 main(int argc, char **argv)
475 {
476     d_display_t         *dpy;
477     d_options_t          opts;
478
479     read_options(argc, argv, &opts);
480
481     dpy = display_open(NULL);
482     if (!dpy) {
483         printf(_("Unable to connect to display\n"));
484         return 1;
485     }
486
487     if (!dpy->composite.present) {
488         printf(_("no composite extension present on the display\n"));
489         display_unref(dpy);
490         return 1;
491     }
492     if (!dpy->xfixes.present) {
493         printf(_("no xfixes extension present on the display\n"));
494         display_unref(dpy);
495         return 1;
496     }
497     if (!dpy->damage.present) {
498         printf(_("no damage extension present on the display\n"));
499         display_unref(dpy);
500         return 1;
501     }
502     if (!dpy->render.present) {
503         printf(_("no render extension present on the display\n"));
504         display_unref(dpy);
505         return 1;
506     }
507     if (dpy->composite.major_version <= 0 && dpy->composite.minor_version < 3)
508     {
509         printf(_("composite extension does not support the overlay window"));
510         display_unref(dpy);
511         return 1;
512     }
513
514     if (!display_claim_screens(dpy)) {
515         printf(_("found no screens to run on\n"));
516         display_unref(dpy);
517         return 0;
518     }
519
520     signal(SIGINT, signal_quit_handler);
521     signal(SIGHUP, signal_quit_handler);
522     signal(SIGTERM, signal_quit_handler);
523     signal(SIGQUIT, signal_quit_handler);
524
525     setup_functions(dpy, &opts);
526
527     {
528         /* some of the windows may already be visible */
529         d_list_it_t *sc_it;
530
531         for (sc_it = list_top(dpy->screens); sc_it; sc_it = sc_it->next) {
532             d_screen_t *sc = sc_it->data;
533             d_list_it_t *it;
534             for (it = list_bottom(sc->stacking); it; it = it->prev)
535                 if (window_is_attr_mapped(it->data))
536                     sc->window_show(it->data);
537         }
538     }
539
540     printf("running\n");
541     run(dpy);
542
543     {
544         /* make everything hidden */
545         d_list_it_t *sc_it;
546
547         for (sc_it = list_top(dpy->screens); sc_it; sc_it = sc_it->next) {
548             d_screen_t *sc = sc_it->data;
549             d_list_it_t *it;
550             for (it = list_top(sc->stacking); it; it = it->next) {
551                 if (window_is_mapped(it->data))
552                     sc->window_hide(it->data);
553             }
554         }
555     }
556
557     cleanup_functions(dpy);
558
559     display_unref(dpy);
560     return 0;
561 }
562
563 gboolean
564 dcompmgr_running(void)
565 {
566     return running;
567 }