f6b70193139408a66387258dcdede55fcd954169
[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
185             pev = (xcb_property_notify_event_t*)ev;
186             if (pev->atom == dpy->a.xrootpmap_id ||
187                 pev->atom == dpy->a.esetroot_pmap_id ||
188                 pev->atom == dpy->a.xsetroot_id)
189             {
190                 d_screen_t *sc;
191
192                 sc = display_screen_from_root(dpy, pev->window);
193                 if (sc) sc->screen_root_pixmap_change(sc);
194             }
195             else if (pev->atom == dpy->a.net_wm_window_opacity) {
196                 d_list_it_t *it;
197
198                 for (it = list_top(dpy->screens); it; it = it->next) {
199                     d_screen_t *sc = it->data;
200                     d_window_t *w;
201
202                     w = screen_find_window(sc, pev->window);
203                     if (w) {
204                         window_update_user_opacity(w);
205                         if (window_is_mapped(w)) {
206                             sc->window_opacity_change(w);
207                             screen_refresh(w->sc);
208                         }
209                     }
210                 }
211                     
212             }
213             break;
214         }
215         default:
216             if (ev->response_type - dpy->damage.event == XCB_DAMAGE_NOTIFY) {
217                 xcb_damage_notify_event_t *dev;
218                 d_list_it_t *it;
219
220                 dev = (xcb_damage_notify_event_t*)ev;
221                 for (it = list_top(dpy->screens); it; it = it->next) {
222                     d_screen_t *sc = it->data;
223                     d_window_t *w;
224
225                     w = screen_find_window(sc, dev->drawable);
226                     if (w) {
227                         screen_refresh(w->sc);
228                         break;
229                     }
230                 }
231                 xcb_damage_subtract(dpy->conn, dev->damage,
232                                     XCB_NONE, XCB_NONE);
233             }
234             else if (dpy->shape.present &&
235                      ev->response_type - dpy->shape.event == XCB_SHAPE_NOTIFY)
236             {
237                 xcb_shape_notify_event_t *sev;
238                 d_list_it_t *it;
239
240                 sev = (xcb_shape_notify_event_t*)ev;
241                 for (it = list_top(dpy->screens); it; it = it->next) {
242                     d_screen_t *sc = it->data;
243                     d_window_t *w;
244
245                     w = screen_find_window(sc, sev->affected_window);
246                     if (w) {
247                         sc->window_reshape(w);
248                         screen_refresh(w->sc);
249                         break;
250                     }
251                 }
252             }
253             break;
254         }
255         free(ev);
256         xcb_flush(dpy->conn);
257     }
258 }
259
260 static void
261 paint(d_display_t *dpy)
262 {
263     d_list_it_t *it;
264     struct timeval now;
265
266     gettimeofday(&now, NULL);
267     
268     for (it = list_top(dpy->screens); it; it = it->next) {
269         d_screen_t *sc = it->data;
270
271         if (time_compare(&sc->next_repaint, &now) <= 0)
272             sc->screen_paint(sc);
273     }
274 }
275
276 static void
277 run(d_display_t *dpy)
278 {
279     while (!quit) {
280         struct timeval next, now, *wait;
281         int            r, npaint;
282         d_list_it_t   *it;
283         fd_set         fds;
284
285         event(dpy);
286
287         npaint = 0;
288         for (it = list_top(dpy->screens); it; it = it->next) {
289             d_screen_t *sc = it->data;
290             if (sc->need_repaint &&
291                 (!npaint || time_compare(&sc->next_repaint, &next) < 0))
292             {
293                 next = sc->next_repaint;
294                 ++npaint;
295             }
296         }
297
298         gettimeofday(&now, 0);
299
300         if (!npaint)
301             /* wait forever, there is nothing that needs drawing */
302             wait = NULL;
303         else if (time_compare(&next, &now) > 0) {
304             /* wait until the next allowed redraw time */
305             time_difference(&next, &now, &next);
306             wait = &next;
307         }
308         else {
309             /* don't wait cuz a redraw is due now already */
310             next.tv_sec = 0;
311             next.tv_usec = 100;
312             wait = &next;
313         }
314
315         FD_ZERO(&fds);
316         FD_SET(dpy->fd, &fds);
317
318         //printf("select %d\n", npaint);
319
320         r = select(dpy->fd+1, &fds, NULL, NULL, wait);
321         if (r == 0) {
322             //printf("select timeout\n");
323             paint(dpy);
324             xcb_flush(dpy->conn);
325         }
326
327         if (xcb_connection_has_error(dpy->conn))
328             quit = TRUE;
329     }
330 }
331
332 static void
333 setup_functions(d_display_t *dpy)
334 {
335     d_list_it_t *it;
336
337     for (it = list_top(dpy->screens); it; it = it->next) {
338         d_screen_t *sc = it->data;
339         int id;
340         screen_setup_default_functions(sc);
341
342         /* these can be plugins.. */
343         id = 1;
344         render_init(sc, id++);
345     }
346 }
347
348 static void
349 cleanup_functions(d_display_t *dpy)
350 {
351     d_list_it_t *it;
352
353     for (it = list_top(dpy->screens); it; it = it->next) {
354         d_screen_t *sc = it->data;
355
356         /* these can be plugins.. */
357         render_free(sc);
358     }
359 }
360
361 int
362 main(int argc, char **argv)
363 {
364     d_display_t         *dpy;
365     d_options_t          opts;
366
367     read_options(argc, argv, &opts);
368
369     dpy = display_open(NULL);
370     if (!dpy) {
371         printf(_("Unable to connect to display\n"));
372         return 1;
373     }
374
375     if (!dpy->composite.present) {
376         printf(_("no composite extension present on the display\n"));
377         display_unref(dpy);
378         return 1;
379     }
380     if (!dpy->xfixes.present) {
381         printf(_("no xfixes extension present on the display\n"));
382         display_unref(dpy);
383         return 1;
384     }
385     if (!dpy->damage.present) {
386         printf(_("no damage extension present on the display\n"));
387         display_unref(dpy);
388         return 1;
389     }
390     if (!dpy->render.present) {
391         printf(_("no render extension present on the display\n"));
392         display_unref(dpy);
393         return 1;
394     }
395     if (dpy->composite.major_version <= 0 && dpy->composite.minor_version < 3)
396     {
397         printf(_("composite extension does not support the overlay window"));
398         display_unref(dpy);
399         return 1;
400     }
401
402     if (!display_claim_screens(dpy)) {
403         printf(_("found no screens to run on\n"));
404         display_unref(dpy);
405         return 0;
406     }
407
408     signal(SIGINT, signal_quit_handler);
409     signal(SIGHUP, signal_quit_handler);
410     signal(SIGTERM, signal_quit_handler);
411     signal(SIGQUIT, signal_quit_handler);
412
413     setup_functions(dpy);
414
415     {
416         /* some of the windows may already be visible */
417         d_list_it_t *sc_it;
418
419         for (sc_it = list_top(dpy->screens); sc_it; sc_it = sc_it->next) {
420             d_screen_t *sc = sc_it->data;
421             d_list_it_t *it;
422             for (it = list_bottom(sc->stacking); it; it = it->prev)
423                 if (window_is_attr_mapped(it->data))
424                     sc->window_show(it->data);
425         }
426     }
427
428     run(dpy);
429
430     {
431         /* make everything hidden */
432         d_list_it_t *sc_it;
433
434         for (sc_it = list_top(dpy->screens); sc_it; sc_it = sc_it->next) {
435             d_screen_t *sc = sc_it->data;
436             d_list_it_t *it;
437             for (it = list_top(sc->stacking); it; it = it->next) {
438                 if (window_is_mapped(it->data))
439                     sc->window_hide(it->data);
440             }
441         }
442     }
443
444     cleanup_functions(dpy);
445
446     display_unref(dpy);
447     return 0;
448 }