rev |
line source |
nuclear@0
|
1 #include <stdio.h>
|
nuclear@0
|
2 #include <stdlib.h>
|
nuclear@0
|
3 #include <unistd.h>
|
nuclear@0
|
4 #include <X11/Xlib.h>
|
nuclear@0
|
5 #include <X11/Xutil.h>
|
nuclear@0
|
6 #include <X11/Xatom.h>
|
nuclear@0
|
7 #include <X11/extensions/Xcomposite.h>
|
nuclear@1
|
8 #include <X11/extensions/Xdamage.h>
|
nuclear@5
|
9 #include <X11/extensions/shape.h>
|
nuclear@0
|
10 #include "cwin.h"
|
nuclear@2
|
11 #include "opengl.h"
|
nuclear@1
|
12 #include "logger.h"
|
nuclear@9
|
13 #include "xerr.h"
|
nuclear@9
|
14
|
nuclear@1
|
15
|
nuclear@5
|
16 static bool query_extensions();
|
nuclear@1
|
17 static bool register_compositor();
|
nuclear@2
|
18 static void start_comp();
|
nuclear@2
|
19 static void manage_window(Window xwin);
|
nuclear@2
|
20 static void unmanage_window(Window xwin);
|
nuclear@7
|
21 static void damage_window(XDamageNotifyEvent *ev);
|
nuclear@2
|
22 static void redraw();
|
nuclear@3
|
23 static void draw_window(CompWindow *cwin);
|
nuclear@2
|
24 static void reshape(int x, int y);
|
nuclear@1
|
25
|
nuclear@2
|
26 Display *dpy;
|
nuclear@2
|
27 int screen_num;
|
nuclear@2
|
28 Window root_win, comp_win;
|
nuclear@2
|
29 int root_width, root_height;
|
nuclear@1
|
30
|
nuclear@5
|
31 static int xdmg_ev_base, xdmg_err_base;
|
nuclear@5
|
32 static int xshape_ev_base, xshape_err_base;
|
nuclear@5
|
33
|
nuclear@1
|
34 int main(int argc, char **argv)
|
nuclear@1
|
35 {
|
nuclear@1
|
36 if(!(dpy = XOpenDisplay(0))) {
|
nuclear@1
|
37 log_error("failed to open X display\n");
|
nuclear@1
|
38 return 1;
|
nuclear@1
|
39 }
|
nuclear@2
|
40 screen_num = DefaultScreen(dpy);
|
nuclear@2
|
41 root_win = RootWindow(dpy, screen_num);
|
nuclear@2
|
42 root_width = DisplayWidth(dpy, screen_num);
|
nuclear@2
|
43 root_height = DisplayHeight(dpy, screen_num);
|
nuclear@1
|
44 log_info("display size %dx%d\n", root_width, root_height);
|
nuclear@1
|
45
|
nuclear@5
|
46 if(!query_extensions()) {
|
nuclear@1
|
47 return 1;
|
nuclear@1
|
48 }
|
nuclear@1
|
49
|
nuclear@1
|
50 if(!register_compositor()) {
|
nuclear@1
|
51 return 1;
|
nuclear@1
|
52 }
|
nuclear@1
|
53
|
nuclear@2
|
54 comp_win = XCompositeGetOverlayWindow(dpy, root_win);
|
nuclear@2
|
55 if(!create_gl(comp_win)) {
|
nuclear@2
|
56 return 1;
|
nuclear@2
|
57 }
|
nuclear@2
|
58
|
nuclear@2
|
59 start_comp();
|
nuclear@2
|
60 reshape(root_width, root_height);
|
nuclear@2
|
61 redraw();
|
nuclear@1
|
62
|
nuclear@1
|
63 for(;;) {
|
nuclear@1
|
64 XEvent ev;
|
nuclear@1
|
65 XNextEvent(dpy, &ev);
|
nuclear@2
|
66
|
nuclear@3
|
67 CompWindow *cwin;
|
nuclear@2
|
68 switch(ev.type) {
|
nuclear@2
|
69 case CreateNotify:
|
nuclear@9
|
70 log_debug("CreateNotify: %x\n", ev.xcreatewindow.window);
|
nuclear@2
|
71 manage_window(ev.xcreatewindow.window);
|
nuclear@2
|
72 break;
|
nuclear@2
|
73
|
nuclear@2
|
74 case ConfigureNotify:
|
nuclear@13
|
75 if((cwin = find_window_xid(eoriginal ibm pc boardv.xconfigure.window))) {
|
nuclear@13
|
76 cwin->attr.x = ev.xconfigure.x;
|
nuclear@13
|
77 cwin->attr.y = ev.xconfigure.y;
|
nuclear@13
|
78 cwin->attr.width = ev.xconfigure.width;
|
nuclear@13
|
79 cwin->attr.height = ev.xconfigure.height;
|
nuclear@12
|
80 cwin->update_attr();
|
nuclear@5
|
81 }
|
nuclear@2
|
82 break;
|
nuclear@2
|
83
|
nuclear@2
|
84 case DestroyNotify:
|
nuclear@9
|
85 log_debug("DestroyNotify: %x\n", ev.xdestroywindow.window);
|
nuclear@2
|
86 unmanage_window(ev.xdestroywindow.window);
|
nuclear@2
|
87 break;
|
nuclear@2
|
88
|
nuclear@2
|
89 case MapNotify:
|
nuclear@3
|
90 if((cwin = find_window_xid(ev.xmap.window))) {
|
nuclear@3
|
91 cwin->mapped = true;
|
nuclear@3
|
92 }
|
nuclear@2
|
93 break;
|
nuclear@3
|
94
|
nuclear@2
|
95 case UnmapNotify:
|
nuclear@3
|
96 if((cwin = find_window_xid(ev.xunmap.window))) {
|
nuclear@3
|
97 cwin->mapped = false;
|
nuclear@3
|
98 }
|
nuclear@2
|
99 break;
|
nuclear@2
|
100
|
nuclear@2
|
101 case ReparentNotify:
|
nuclear@2
|
102 if(ev.xreparent.parent == root_win) {
|
nuclear@2
|
103 manage_window(ev.xreparent.window);
|
nuclear@2
|
104 } else {
|
nuclear@2
|
105 unmanage_window(ev.xreparent.window);
|
nuclear@2
|
106 }
|
nuclear@2
|
107 break;
|
nuclear@2
|
108
|
nuclear@2
|
109 case CirculateNotify:
|
nuclear@2
|
110 break;
|
nuclear@2
|
111
|
nuclear@2
|
112 case Expose:
|
nuclear@3
|
113 // TODO?
|
nuclear@2
|
114 break;
|
nuclear@2
|
115
|
nuclear@2
|
116 default:
|
nuclear@2
|
117 if(ev.type == xdmg_ev_base + XDamageNotify) {
|
nuclear@7
|
118 damage_window((XDamageNotifyEvent*)&ev);
|
nuclear@2
|
119 }
|
nuclear@2
|
120 }
|
nuclear@5
|
121
|
nuclear@5
|
122 redraw();
|
nuclear@1
|
123 }
|
nuclear@1
|
124 }
|
nuclear@1
|
125
|
nuclear@5
|
126 static bool query_extensions()
|
nuclear@5
|
127 {
|
nuclear@5
|
128 int xcomp_ev_base, xcomp_err_base;
|
nuclear@5
|
129 if(!XCompositeQueryExtension(dpy, &xcomp_ev_base, &xcomp_err_base)) {
|
nuclear@5
|
130 log_error("X server doesn't support the composite extension\n");
|
nuclear@5
|
131 return false;
|
nuclear@5
|
132 }
|
nuclear@5
|
133 int xcomp_ver_major, xcomp_ver_minor;
|
nuclear@5
|
134 XCompositeQueryVersion(dpy, &xcomp_ver_major, &xcomp_ver_minor);
|
nuclear@5
|
135 log_info("Found composite extension version %d.%d\n", xcomp_ver_major, xcomp_ver_minor);
|
nuclear@5
|
136 if(xcomp_ver_major <= 0 && xcomp_ver_minor < 3) {
|
nuclear@5
|
137 // for NameWindowPixmap & CompositeGetoverlayWindow
|
nuclear@5
|
138 log_error("I need at least version 0.3\n");
|
nuclear@5
|
139 return false;
|
nuclear@5
|
140 }
|
nuclear@5
|
141
|
nuclear@5
|
142 if(!XDamageQueryExtension(dpy, &xdmg_ev_base, &xdmg_err_base)) {
|
nuclear@5
|
143 log_error("X server doesn't support the damage extension\n");
|
nuclear@5
|
144 return false;
|
nuclear@5
|
145 }
|
nuclear@5
|
146
|
nuclear@5
|
147 if(!XShapeQueryExtension(dpy, &xshape_ev_base, &xshape_err_base)) {
|
nuclear@5
|
148 log_error("X server doesn't support the shape extension\n");
|
nuclear@5
|
149 return false;
|
nuclear@5
|
150 }
|
nuclear@5
|
151
|
nuclear@5
|
152 int xfix_ev_base, xfix_err_base;
|
nuclear@5
|
153 if(!XFixesQueryExtension(dpy, &xfix_ev_base, &xfix_err_base)) {
|
nuclear@5
|
154 log_error("X server doesn't support the Xfixes extension\n");
|
nuclear@5
|
155 return false;
|
nuclear@5
|
156 }
|
nuclear@5
|
157
|
nuclear@5
|
158 int xfix_ver_major, xfix_ver_minor;
|
nuclear@5
|
159 XFixesQueryVersion(dpy, &xfix_ver_major, &xfix_ver_minor);
|
nuclear@5
|
160 log_info("Found xfixes version %d.%d\n", xfix_ver_major, xfix_ver_minor);
|
nuclear@5
|
161 if(xfix_ver_major < 2) {
|
nuclear@5
|
162 // for SetWindowShapeRegion
|
nuclear@5
|
163 log_error("I need at least version 2.0\n");
|
nuclear@5
|
164 return false;
|
nuclear@5
|
165 }
|
nuclear@5
|
166 return true;
|
nuclear@5
|
167 }
|
nuclear@5
|
168
|
nuclear@1
|
169 static bool register_compositor()
|
nuclear@1
|
170 {
|
nuclear@1
|
171 char atom_name[64];
|
nuclear@2
|
172 sprintf(atom_name, "_NET_WM_CM_S%d", screen_num);
|
nuclear@1
|
173 Atom xa_wm_cm = XInternAtom(dpy, atom_name, False);
|
nuclear@1
|
174
|
nuclear@1
|
175 Window win = XGetSelectionOwner(dpy, xa_wm_cm);
|
nuclear@1
|
176 if(win != None) {
|
nuclear@1
|
177 log_error("Another compositor is running. Stop it and try again.\n");
|
nuclear@1
|
178 return false;
|
nuclear@1
|
179 }
|
nuclear@1
|
180
|
nuclear@1
|
181 win = XCreateSimpleWindow(dpy, root_win, 0, 0, 1, 1, 0, None, None);
|
nuclear@1
|
182 XSetSelectionOwner(dpy, xa_wm_cm, win, 0);
|
nuclear@1
|
183 return true;
|
nuclear@1
|
184 }
|
nuclear@2
|
185
|
nuclear@2
|
186 static void start_comp()
|
nuclear@2
|
187 {
|
nuclear@2
|
188 XGrabServer(dpy);
|
nuclear@2
|
189 XCompositeRedirectSubwindows(dpy, root_win, CompositeRedirectManual);
|
nuclear@2
|
190 XSelectInput(dpy, root_win, SubstructureNotifyMask | ExposureMask | StructureNotifyMask);
|
nuclear@2
|
191
|
nuclear@2
|
192 // manage all top-level windows
|
nuclear@2
|
193 Window root_ret, parent;
|
nuclear@2
|
194 Window *children;
|
nuclear@2
|
195 unsigned int num_children;
|
nuclear@2
|
196 XQueryTree(dpy, root_win, &root_ret, &parent, &children, &num_children);
|
nuclear@2
|
197
|
nuclear@2
|
198 for(unsigned int i=0; i<num_children; i++) {
|
nuclear@2
|
199 manage_window(children[i]);
|
nuclear@2
|
200 }
|
nuclear@2
|
201 log_info("starting compositor, managing %u top-level windows\n", num_children);
|
nuclear@2
|
202 XFree(children);
|
nuclear@5
|
203 XUngrabServer(dpy);
|
nuclear@2
|
204
|
nuclear@5
|
205 /* set a zero-area input-shape for the overlay window to let events
|
nuclear@5
|
206 * through to the windows under it
|
nuclear@5
|
207 */
|
nuclear@5
|
208 XserverRegion region = XFixesCreateRegion(dpy, 0, 0);
|
nuclear@5
|
209 XFixesSetWindowShapeRegion(dpy, comp_win, ShapeInput, 0, 0, region);
|
nuclear@5
|
210 XFixesDestroyRegion(dpy, region);
|
nuclear@2
|
211 }
|
nuclear@2
|
212
|
nuclear@2
|
213 static void manage_window(Window xwin)
|
nuclear@2
|
214 {
|
nuclear@2
|
215 //log_debug("manage_window %u\n", xwin);
|
nuclear@2
|
216 CompWindow *cwin = new CompWindow(xwin);
|
nuclear@8
|
217 add_window(cwin);
|
nuclear@3
|
218 cwin->mapped = cwin->attr.map_state != IsUnmapped;
|
nuclear@2
|
219 }
|
nuclear@2
|
220
|
nuclear@2
|
221 static void unmanage_window(Window xwin)
|
nuclear@2
|
222 {
|
nuclear@2
|
223 //log_debug("unmanage_window %u\n", xwin);
|
nuclear@2
|
224 CompWindow *cwin = find_window_xid(xwin);
|
nuclear@3
|
225 if(!cwin) return;
|
nuclear@3
|
226
|
nuclear@3
|
227 remove_window(cwin);
|
nuclear@3
|
228 delete cwin;
|
nuclear@2
|
229 }
|
nuclear@2
|
230
|
nuclear@7
|
231 static void damage_window(XDamageNotifyEvent *ev)
|
nuclear@7
|
232 {
|
nuclear@7
|
233 XserverRegion region;
|
nuclear@7
|
234 CompWindow *cwin = find_window_xid(ev->drawable);
|
nuclear@9
|
235 if(!cwin || !cwin->damage) return;
|
nuclear@7
|
236
|
nuclear@7
|
237 region = XFixesCreateRegion(dpy, 0, 0);
|
nuclear@9
|
238
|
nuclear@9
|
239 push_xerr_handler(xerr_debug);
|
nuclear@7
|
240 XDamageSubtract(dpy, cwin->damage, None, region);
|
nuclear@9
|
241 pop_xerr_handler();
|
nuclear@11
|
242
|
nuclear@11
|
243 XFixesDestroyRegion(dpy, region);
|
nuclear@12
|
244 cwin->damaged = true;
|
nuclear@7
|
245 }
|
nuclear@7
|
246
|
nuclear@2
|
247 static void redraw()
|
nuclear@2
|
248 {
|
nuclear@2
|
249 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@2
|
250
|
nuclear@3
|
251 glPushAttrib(GL_ENABLE_BIT);
|
nuclear@3
|
252 glEnable(GL_TEXTURE_2D);
|
nuclear@2
|
253 glEnable(GL_BLEND);
|
nuclear@2
|
254 glBlendFunc(GL_ONE, GL_ONE);
|
nuclear@2
|
255
|
nuclear@2
|
256 int num_win = get_window_count();
|
nuclear@2
|
257 for(int i=0; i<num_win; i++) {
|
nuclear@2
|
258 CompWindow *cwin = get_window(i);
|
nuclear@2
|
259
|
nuclear@3
|
260 if(!cwin->mapped) continue;
|
nuclear@3
|
261
|
nuclear@3
|
262 if(cwin->attr.x + cwin->attr.width <= 0 ||
|
nuclear@3
|
263 cwin->attr.y + cwin->attr.height <= 0 ||
|
nuclear@3
|
264 cwin->attr.x >= root_width || cwin->attr.y >= root_height) {
|
nuclear@3
|
265 continue;
|
nuclear@3
|
266 }
|
nuclear@3
|
267
|
nuclear@3
|
268 draw_window(cwin);
|
nuclear@2
|
269 }
|
nuclear@2
|
270
|
nuclear@3
|
271 glPopAttrib();
|
nuclear@2
|
272
|
nuclear@2
|
273 glXSwapBuffers(dpy, comp_win);
|
nuclear@2
|
274 }
|
nuclear@2
|
275
|
nuclear@3
|
276 static void draw_window(CompWindow *cwin)
|
nuclear@3
|
277 {
|
nuclear@12
|
278 if(!cwin->xpixmap || !cwin->pixmap_valid) {
|
nuclear@12
|
279 cwin->update_pixmap();
|
nuclear@12
|
280 if(!cwin->tex_valid) {
|
nuclear@12
|
281 cwin->update_texture();
|
nuclear@3
|
282 }
|
nuclear@3
|
283 }
|
nuclear@3
|
284
|
nuclear@3
|
285 glBindTexture(GL_TEXTURE_2D, cwin->tex.get_id());
|
nuclear@3
|
286
|
nuclear@3
|
287 glBegin(GL_QUADS);
|
nuclear@3
|
288 glColor3f(0.3, 0.3, 0.3);
|
nuclear@3
|
289 glTexCoord2f(0, 0);
|
nuclear@3
|
290 glVertex2f(cwin->attr.x, cwin->attr.y);
|
nuclear@3
|
291 glTexCoord2f(1, 0);
|
nuclear@3
|
292 glVertex2f(cwin->attr.x + cwin->attr.width, cwin->attr.y);
|
nuclear@3
|
293 glTexCoord2f(1, 1);
|
nuclear@3
|
294 glVertex2f(cwin->attr.x + cwin->attr.width, cwin->attr.y + cwin->attr.height);
|
nuclear@3
|
295 glTexCoord2f(0, 1);
|
nuclear@3
|
296 glVertex2f(cwin->attr.x, cwin->attr.y + cwin->attr.height);
|
nuclear@3
|
297 glEnd();
|
nuclear@12
|
298
|
nuclear@12
|
299 cwin->damaged = false;
|
nuclear@3
|
300 }
|
nuclear@3
|
301
|
nuclear@2
|
302 static void reshape(int x, int y)
|
nuclear@2
|
303 {
|
nuclear@2
|
304 glViewport(0, 0, x, y);
|
nuclear@2
|
305
|
nuclear@2
|
306 glMatrixMode(GL_PROJECTION);
|
nuclear@2
|
307 glLoadIdentity();
|
nuclear@2
|
308 glOrtho(0, x, y, 0, -1, 1);
|
nuclear@2
|
309 }
|