40d983a74033cf1d99b881b12861180ae9fa7958
[dana/dcompmgr.git] / dcompmgr.c
1 #include "screen.h"
2 #include "window.h"
3 #include "display.h"
4 #include "gettext.h"
5
6 #include <glib.h>
7 #include <stdio.h>
8 #include <sys/select.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <xcb/xcb.h>
12
13 typedef struct {
14     int foo;
15 } d_options_t;
16
17 static void
18 read_options(int argc, char **argv, d_options_t *opts)
19 {
20     opts->foo = argc && argv;
21 }
22
23 static void
24 event(d_display_t *dpy)
25 {
26     xcb_generic_event_t *ev;
27
28     while ((ev = xcb_poll_for_event(dpy->conn))) {
29         printf("event %d\n", ev->response_type);
30
31         if (!ev->response_type) {
32             display_error(dpy, (xcb_generic_error_t*)ev);
33             free(ev);
34             return;
35         }
36
37         switch (ev->response_type) {
38         case XCB_CREATE_NOTIFY:
39         {
40             xcb_create_notify_event_t *cev;
41             d_screen_t *sc;
42
43             cev = (xcb_create_notify_event_t*)ev;
44             sc = display_screen_from_root(dpy, cev->parent);
45             if (!sc) break;
46             screen_add_window(sc, cev->window);
47             break;
48         }
49         case XCB_DESTROY_NOTIFY:
50         {
51             xcb_destroy_notify_event_t *dev;
52             d_screen_t *sc;
53             d_window_t *w;
54
55             dev = (xcb_destroy_notify_event_t*)ev;
56             sc = display_screen_from_root(dpy, dev->event);
57             if (!sc) break;
58             w = screen_find_window(sc, dev->window);
59             w->hide(w);
60             screen_remove_window(sc, w);
61             break;
62         }
63         case XCB_REPARENT_NOTIFY:
64         {
65             xcb_reparent_notify_event_t *rev;
66             d_screen_t *sc;
67             d_window_t *w;
68
69             rev = (xcb_reparent_notify_event_t*)ev;
70             sc = display_screen_from_root(dpy, rev->event);
71             if (!sc) break;
72             w = screen_find_window(sc, rev->window);
73             if (rev->parent == sc->super.root)
74                 screen_add_window(sc, rev->window);
75             else {
76                 w->hide(w);
77                 screen_remove_window(sc, w);
78             }
79             break;
80         }
81         case XCB_MAP_NOTIFY:
82         {
83             xcb_map_notify_event_t *mev;
84             d_screen_t *sc;
85             d_window_t *w;
86
87             mev = (xcb_map_notify_event_t*)ev;
88             sc = display_screen_from_root(dpy, mev->event);
89             if (!sc) break;
90             w = screen_find_window(sc, mev->window);
91             w->show(w);
92             break;
93         }
94         case XCB_UNMAP_NOTIFY:
95         {
96             xcb_unmap_notify_event_t *mev;
97             d_screen_t *sc;
98             d_window_t *w;
99
100             mev = (xcb_unmap_notify_event_t*)ev;
101             sc = display_screen_from_root(dpy, mev->event);
102             if (!sc) break;
103             w = screen_find_window(sc, mev->window);
104             w->hide(w);
105             break;
106         }
107         default:
108             break;
109         }
110         free(ev);
111     }
112 }
113
114 static void
115 paint(d_display_t *dpy)
116 {
117     int i;
118
119     for (i = 0; i < dpy->nscreens; ++i) {
120         d_screen_t *sc = display_screen_n(dpy, i);
121
122         sc->paint(sc);
123     }
124 }
125
126 static void
127 run(d_display_t *dpy)
128 {
129     gboolean quit;
130     fd_set   fds;
131     int      max = -1;
132
133     FD_ZERO(&fds);
134     FD_SET(dpy->fd, &fds);
135     max = MAX(max, dpy->fd);
136
137     paint(dpy);
138
139     quit = FALSE;
140     while (!quit) {
141         int r;
142
143         r = select(max+1, &fds, NULL, NULL, NULL);
144         if (r < 0)
145             printf("select error\n");
146         else if (r == 0)
147             printf("select timeout\n");
148         else {
149             printf("select data\n");
150             /*if (FD_ISSET(dpy->fd, &fds))*/ {
151                 event(dpy);
152                 paint(dpy);
153             }
154         }
155
156         if (xcb_connection_has_error(dpy->conn))
157             quit = TRUE;
158     }
159 }
160
161 int
162 main(int argc, char **argv)
163 {
164     d_display_t         *dpy;
165     d_options_t          opts;
166
167     read_options(argc, argv, &opts);
168
169     dpy = display_open(NULL);
170     if (!dpy) {
171         printf(_("Unable to connect to display\n"));
172         return 1;
173     }
174
175     if (!dpy->composite.present) {
176         printf(_("no composite extension present on the display\n"));
177         display_unref(dpy);
178         return 1;
179     }
180     if (!dpy->xfixes.present) {
181         printf(_("no xfixes extension present on the display\n"));
182         display_unref(dpy);
183         return 1;
184     }
185     if (!dpy->damage.present) {
186         printf(_("no damage extension present on the display\n"));
187         display_unref(dpy);
188         return 1;
189     }
190     if (!dpy->render.present) {
191         printf(_("no render extension present on the display\n"));
192         display_unref(dpy);
193         return 1;
194     }
195     if (dpy->composite.major_version <= 0 && dpy->composite.minor_version < 3)
196     {
197         printf(_("composite extension does not support the overlay window"));
198         display_unref(dpy);
199         return 1;
200     }
201
202     if (!display_claim_screens(dpy)) {
203         printf(_("found no screens to run on\n"));
204         display_unref(dpy);
205         return 0;
206     }
207
208     run(dpy);
209
210     display_unref(dpy);
211     return 0;
212 }