xglcomp

view src/main.cc @ 10:be18500d76d1

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