xglcomp

view src/main.cc @ 8:b0081a0c211f

damage issues
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 30 Jan 2016 07:58:07 +0200
parents 03ca0fd49916
children 245dd960f0b3
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"
14 static bool query_extensions();
15 static bool register_compositor();
16 static void start_comp();
17 static void manage_window(Window xwin);
18 static void unmanage_window(Window xwin);
19 static void damage_window(XDamageNotifyEvent *ev);
20 static void redraw();
21 static void draw_window(CompWindow *cwin);
22 static void reshape(int x, int y);
24 Display *dpy;
25 int screen_num;
26 Window root_win, comp_win;
27 int root_width, root_height;
29 static int xdmg_ev_base, xdmg_err_base;
30 static int xshape_ev_base, xshape_err_base;
32 int main(int argc, char **argv)
33 {
34 if(!(dpy = XOpenDisplay(0))) {
35 log_error("failed to open X display\n");
36 return 1;
37 }
38 screen_num = DefaultScreen(dpy);
39 root_win = RootWindow(dpy, screen_num);
40 root_width = DisplayWidth(dpy, screen_num);
41 root_height = DisplayHeight(dpy, screen_num);
42 log_info("display size %dx%d\n", root_width, root_height);
44 if(!query_extensions()) {
45 return 1;
46 }
48 if(!register_compositor()) {
49 return 1;
50 }
52 comp_win = XCompositeGetOverlayWindow(dpy, root_win);
53 if(!create_gl(comp_win)) {
54 return 1;
55 }
57 start_comp();
58 reshape(root_width, root_height);
59 redraw();
61 for(;;) {
62 XEvent ev;
63 XNextEvent(dpy, &ev);
65 CompWindow *cwin;
66 switch(ev.type) {
67 case CreateNotify:
68 manage_window(ev.xcreatewindow.window);
69 break;
71 case ConfigureNotify:
72 // XXX is this right?
73 if((cwin = find_window_xid(ev.xconfigure.window))) {
74 log_debug("updating window attributes\n");
75 XGetWindowAttributes(dpy, cwin->xwin, &cwin->attr);
76 }
77 break;
79 case DestroyNotify:
80 unmanage_window(ev.xdestroywindow.window);
81 break;
83 case MapNotify:
84 if((cwin = find_window_xid(ev.xmap.window))) {
85 cwin->mapped = true;
86 }
87 break;
89 case UnmapNotify:
90 if((cwin = find_window_xid(ev.xunmap.window))) {
91 cwin->mapped = false;
92 }
93 break;
95 case ReparentNotify:
96 if(ev.xreparent.parent == root_win) {
97 manage_window(ev.xreparent.window);
98 } else {
99 unmanage_window(ev.xreparent.window);
100 }
101 break;
103 case CirculateNotify:
104 break;
106 case Expose:
107 // TODO?
108 break;
110 default:
111 if(ev.type == xdmg_ev_base + XDamageNotify) {
112 damage_window((XDamageNotifyEvent*)&ev);
113 }
114 }
116 redraw();
117 }
118 }
120 static bool query_extensions()
121 {
122 int xcomp_ev_base, xcomp_err_base;
123 if(!XCompositeQueryExtension(dpy, &xcomp_ev_base, &xcomp_err_base)) {
124 log_error("X server doesn't support the composite extension\n");
125 return false;
126 }
127 int xcomp_ver_major, xcomp_ver_minor;
128 XCompositeQueryVersion(dpy, &xcomp_ver_major, &xcomp_ver_minor);
129 log_info("Found composite extension version %d.%d\n", xcomp_ver_major, xcomp_ver_minor);
130 if(xcomp_ver_major <= 0 && xcomp_ver_minor < 3) {
131 // for NameWindowPixmap & CompositeGetoverlayWindow
132 log_error("I need at least version 0.3\n");
133 return false;
134 }
136 if(!XDamageQueryExtension(dpy, &xdmg_ev_base, &xdmg_err_base)) {
137 log_error("X server doesn't support the damage extension\n");
138 return false;
139 }
141 if(!XShapeQueryExtension(dpy, &xshape_ev_base, &xshape_err_base)) {
142 log_error("X server doesn't support the shape extension\n");
143 return false;
144 }
146 int xfix_ev_base, xfix_err_base;
147 if(!XFixesQueryExtension(dpy, &xfix_ev_base, &xfix_err_base)) {
148 log_error("X server doesn't support the Xfixes extension\n");
149 return false;
150 }
152 int xfix_ver_major, xfix_ver_minor;
153 XFixesQueryVersion(dpy, &xfix_ver_major, &xfix_ver_minor);
154 log_info("Found xfixes version %d.%d\n", xfix_ver_major, xfix_ver_minor);
155 if(xfix_ver_major < 2) {
156 // for SetWindowShapeRegion
157 log_error("I need at least version 2.0\n");
158 return false;
159 }
160 return true;
161 }
163 static bool register_compositor()
164 {
165 char atom_name[64];
166 sprintf(atom_name, "_NET_WM_CM_S%d", screen_num);
167 Atom xa_wm_cm = XInternAtom(dpy, atom_name, False);
169 Window win = XGetSelectionOwner(dpy, xa_wm_cm);
170 if(win != None) {
171 log_error("Another compositor is running. Stop it and try again.\n");
172 return false;
173 }
175 win = XCreateSimpleWindow(dpy, root_win, 0, 0, 1, 1, 0, None, None);
176 XSetSelectionOwner(dpy, xa_wm_cm, win, 0);
177 return true;
178 }
180 static void start_comp()
181 {
182 XGrabServer(dpy);
183 XCompositeRedirectSubwindows(dpy, root_win, CompositeRedirectManual);
184 XSelectInput(dpy, root_win, SubstructureNotifyMask | ExposureMask | StructureNotifyMask);
186 // manage all top-level windows
187 Window root_ret, parent;
188 Window *children;
189 unsigned int num_children;
190 XQueryTree(dpy, root_win, &root_ret, &parent, &children, &num_children);
192 for(unsigned int i=0; i<num_children; i++) {
193 manage_window(children[i]);
194 }
195 log_info("starting compositor, managing %u top-level windows\n", num_children);
196 XFree(children);
197 XUngrabServer(dpy);
199 /* set a zero-area input-shape for the overlay window to let events
200 * through to the windows under it
201 */
202 XserverRegion region = XFixesCreateRegion(dpy, 0, 0);
203 XFixesSetWindowShapeRegion(dpy, comp_win, ShapeInput, 0, 0, region);
204 XFixesDestroyRegion(dpy, region);
205 }
207 static void manage_window(Window xwin)
208 {
209 //log_debug("manage_window %u\n", xwin);
210 CompWindow *cwin = new CompWindow(xwin);
211 add_window(cwin);
212 cwin->mapped = cwin->attr.map_state != IsUnmapped;
213 }
215 static void unmanage_window(Window xwin)
216 {
217 //log_debug("unmanage_window %u\n", xwin);
218 CompWindow *cwin = find_window_xid(xwin);
219 if(!cwin) return;
221 remove_window(cwin);
222 delete cwin;
223 }
225 static void damage_window(XDamageNotifyEvent *ev)
226 {
227 XserverRegion region;
228 CompWindow *cwin = find_window_xid(ev->drawable);
229 if(!cwin) return;
231 region = XFixesCreateRegion(dpy, 0, 0);
232 XDamageSubtract(dpy, cwin->damage, None, region);
233 }
235 static void redraw()
236 {
237 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
239 glPushAttrib(GL_ENABLE_BIT);
240 glEnable(GL_TEXTURE_2D);
241 glEnable(GL_BLEND);
242 glBlendFunc(GL_ONE, GL_ONE);
244 int num_win = get_window_count();
245 for(int i=0; i<num_win; i++) {
246 CompWindow *cwin = get_window(i);
248 if(!cwin->mapped) continue;
250 if(cwin->attr.x + cwin->attr.width <= 0 ||
251 cwin->attr.y + cwin->attr.height <= 0 ||
252 cwin->attr.x >= root_width || cwin->attr.y >= root_height) {
253 continue;
254 }
256 draw_window(cwin);
257 }
259 glPopAttrib();
261 glXSwapBuffers(dpy, comp_win);
262 }
264 static void draw_window(CompWindow *cwin)
265 {
266 if(!cwin->xpixmap) {
267 cwin->xpixmap = XCompositeNameWindowPixmap(dpy, cwin->xwin);
268 if(!cwin->xpixmap) {
269 log_warning("failed to get pixmap\n");
270 return;
271 }
272 cwin->tex.set_image(dpy, cwin->xpixmap);
273 }
275 glBindTexture(GL_TEXTURE_2D, cwin->tex.get_id());
277 glBegin(GL_QUADS);
278 glColor3f(0.3, 0.3, 0.3);
279 glTexCoord2f(0, 0);
280 glVertex2f(cwin->attr.x, cwin->attr.y);
281 glTexCoord2f(1, 0);
282 glVertex2f(cwin->attr.x + cwin->attr.width, cwin->attr.y);
283 glTexCoord2f(1, 1);
284 glVertex2f(cwin->attr.x + cwin->attr.width, cwin->attr.y + cwin->attr.height);
285 glTexCoord2f(0, 1);
286 glVertex2f(cwin->attr.x, cwin->attr.y + cwin->attr.height);
287 glEnd();
288 }
290 static void reshape(int x, int y)
291 {
292 glViewport(0, 0, x, y);
294 glMatrixMode(GL_PROJECTION);
295 glLoadIdentity();
296 glOrtho(0, x, y, 0, -1, 1);
297 }