add an error handler
[dana/dcompmgr.git] / dcompmgr.c
1 #include "screen.h"
2 #include "window.h"
3 #include "gettext.h"
4
5 #include <glib.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <xcb/xcb.h>
10
11 static gint
12 all_screens(xcb_connection_t *conn, d_screen_t **list)
13 {
14     static const xcb_setup_t *setup;
15     xcb_screen_iterator_t it;
16     int count, i;
17     d_screen_t sc;
18
19     setup = xcb_get_setup(conn);
20
21     count = i = 0;
22     for (it = xcb_setup_roots_iterator(setup); it.rem; xcb_screen_next(&it)) {
23         sc.super = *it.data;
24         sc.conn = conn;
25         sc.num = i++;
26         if (screen_register(&sc)) {
27             ++count;
28             *list = g_renew(d_screen_t, *list, count);
29             (*list)[count-1] = sc;
30             printf(_("managing screen %d\n"), sc.num);
31         }
32     }
33     return count;
34 }
35
36 d_screen_t*
37 screen_from_root(d_screen_t *list, int n, xcb_window_t root)
38 {
39     int i;
40     for (i = 0; i < n; ++i)
41         if (list->super.root == root) return &list[i];
42     g_assert_not_reached();
43     return NULL;
44 }
45
46 int
47 main(int argc, char **argv)
48 {
49     xcb_connection_t    *conn;
50     d_screen_t          *screens = NULL;
51     int                  nscreens;
52     xcb_generic_event_t *ev;
53
54     conn = xcb_connect(NULL, NULL);
55     if (!conn) {
56         printf(_("Unable to connect to display\n"));
57         return 1;
58     }
59
60     nscreens = all_screens(conn, &screens);
61     if (nscreens < 1) {
62         printf(_("found no screens to run on\n"));
63         xcb_disconnect(conn);
64         return 0;
65     }
66
67     while ((ev = xcb_wait_for_event(conn))) {
68         printf("event %d\n", ev->response_type);
69
70         if (!ev->response_type) {
71             error(conn, (xcb_generic_error_t*)ev);
72             free(ev);
73             continue;
74         }
75
76         switch (ev->response_type) {
77         case XCB_CREATE_NOTIFY:
78         {
79             xcb_create_notify_event_t *cev;
80             d_screen_t *sc;
81
82             cev = (xcb_create_notify_event_t*)ev;
83             sc = screen_from_root(screens, nscreens, cev->parent);
84             if (!sc) break;
85             screen_add_window(sc, cev->window);
86             break;
87         }
88         case XCB_DESTROY_NOTIFY:
89         {
90             xcb_destroy_notify_event_t *dev;
91             d_screen_t *sc;
92             d_window_t *w;
93
94             dev = (xcb_destroy_notify_event_t*)ev;
95             sc = screen_from_root(screens, nscreens, dev->event);
96             if (!sc) break;
97             w = screen_find_window(sc, dev->window);
98             w->hide(w);
99             screen_remove_window(sc, w);
100             break;
101         }
102         case XCB_REPARENT_NOTIFY:
103         {
104             xcb_reparent_notify_event_t *rev;
105             d_screen_t *sc;
106             d_window_t *w;
107
108             rev = (xcb_reparent_notify_event_t*)ev;
109             sc = screen_from_root(screens, nscreens, rev->event);
110             if (!sc) break;
111             w = screen_find_window(sc, rev->window);
112             if (rev->parent == sc->super.root)
113                 screen_add_window(sc, rev->window);
114             else {
115                 w->hide(w);
116                 screen_remove_window(sc, w);
117             }
118             break;
119         }
120         default:
121             break;
122         }
123         free(ev);
124     }
125
126     xcb_disconnect(conn);
127     return 0;
128 }