xglcomp
view src/main.cc @ 14:db2e91f95531
bad commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 07 Feb 2016 11:52:56 +0200 |
parents | 593a5e4a351f |
children | 6726f29ebbb8 |
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 if((cwin = find_window_xid(ev.xconfigure.window))) {
76 cwin->attr.x = ev.xconfigure.x;
77 cwin->attr.y = ev.xconfigure.y;
78 cwin->attr.width = ev.xconfigure.width;
79 cwin->attr.height = ev.xconfigure.height;
80 cwin->update_attr();
81 }
82 break;
84 case DestroyNotify:
85 log_debug("DestroyNotify: %x\n", ev.xdestroywindow.window);
86 unmanage_window(ev.xdestroywindow.window);
87 break;
89 case MapNotify:
90 if((cwin = find_window_xid(ev.xmap.window))) {
91 cwin->mapped = true;
92 }
93 break;
95 case UnmapNotify:
96 if((cwin = find_window_xid(ev.xunmap.window))) {
97 cwin->mapped = false;
98 }
99 break;
101 case ReparentNotify:
102 if(ev.xreparent.parent == root_win) {
103 manage_window(ev.xreparent.window);
104 } else {
105 unmanage_window(ev.xreparent.window);
106 }
107 break;
109 case CirculateNotify:
110 break;
112 case Expose:
113 // TODO?
114 break;
116 default:
117 if(ev.type == xdmg_ev_base + XDamageNotify) {
118 damage_window((XDamageNotifyEvent*)&ev);
119 }
120 }
122 redraw();
123 }
124 }
126 static bool query_extensions()
127 {
128 int xcomp_ev_base, xcomp_err_base;
129 if(!XCompositeQueryExtension(dpy, &xcomp_ev_base, &xcomp_err_base)) {
130 log_error("X server doesn't support the composite extension\n");
131 return false;
132 }
133 int xcomp_ver_major, xcomp_ver_minor;
134 XCompositeQueryVersion(dpy, &xcomp_ver_major, &xcomp_ver_minor);
135 log_info("Found composite extension version %d.%d\n", xcomp_ver_major, xcomp_ver_minor);
136 if(xcomp_ver_major <= 0 && xcomp_ver_minor < 3) {
137 // for NameWindowPixmap & CompositeGetoverlayWindow
138 log_error("I need at least version 0.3\n");
139 return false;
140 }
142 if(!XDamageQueryExtension(dpy, &xdmg_ev_base, &xdmg_err_base)) {
143 log_error("X server doesn't support the damage extension\n");
144 return false;
145 }
147 if(!XShapeQueryExtension(dpy, &xshape_ev_base, &xshape_err_base)) {
148 log_error("X server doesn't support the shape extension\n");
149 return false;
150 }
152 int xfix_ev_base, xfix_err_base;
153 if(!XFixesQueryExtension(dpy, &xfix_ev_base, &xfix_err_base)) {
154 log_error("X server doesn't support the Xfixes extension\n");
155 return false;
156 }
158 int xfix_ver_major, xfix_ver_minor;
159 XFixesQueryVersion(dpy, &xfix_ver_major, &xfix_ver_minor);
160 log_info("Found xfixes version %d.%d\n", xfix_ver_major, xfix_ver_minor);
161 if(xfix_ver_major < 2) {
162 // for SetWindowShapeRegion
163 log_error("I need at least version 2.0\n");
164 return false;
165 }
166 return true;
167 }
169 static bool register_compositor()
170 {
171 char atom_name[64];
172 sprintf(atom_name, "_NET_WM_CM_S%d", screen_num);
173 Atom xa_wm_cm = XInternAtom(dpy, atom_name, False);
175 Window win = XGetSelectionOwner(dpy, xa_wm_cm);
176 if(win != None) {
177 log_error("Another compositor is running. Stop it and try again.\n");
178 return false;
179 }
181 win = XCreateSimpleWindow(dpy, root_win, 0, 0, 1, 1, 0, None, None);
182 XSetSelectionOwner(dpy, xa_wm_cm, win, 0);
183 return true;
184 }
186 static void start_comp()
187 {
188 XGrabServer(dpy);
189 XCompositeRedirectSubwindows(dpy, root_win, CompositeRedirectManual);
190 XSelectInput(dpy, root_win, SubstructureNotifyMask | ExposureMask | StructureNotifyMask);
192 // manage all top-level windows
193 Window root_ret, parent;
194 Window *children;
195 unsigned int num_children;
196 XQueryTree(dpy, root_win, &root_ret, &parent, &children, &num_children);
198 for(unsigned int i=0; i<num_children; i++) {
199 manage_window(children[i]);
200 }
201 log_info("starting compositor, managing %u top-level windows\n", num_children);
202 XFree(children);
203 XUngrabServer(dpy);
205 /* set a zero-area input-shape for the overlay window to let events
206 * through to the windows under it
207 */
208 XserverRegion region = XFixesCreateRegion(dpy, 0, 0);
209 XFixesSetWindowShapeRegion(dpy, comp_win, ShapeInput, 0, 0, region);
210 XFixesDestroyRegion(dpy, region);
211 }
213 static void manage_window(Window xwin)
214 {
215 //log_debug("manage_window %u\n", xwin);
216 CompWindow *cwin = new CompWindow(xwin);
217 add_window(cwin);
218 cwin->mapped = cwin->attr.map_state != IsUnmapped;
219 }
221 static void unmanage_window(Window xwin)
222 {
223 //log_debug("unmanage_window %u\n", xwin);
224 CompWindow *cwin = find_window_xid(xwin);
225 if(!cwin) return;
227 remove_window(cwin);
228 delete cwin;
229 }
231 static void damage_window(XDamageNotifyEvent *ev)
232 {
233 XserverRegion region;
234 CompWindow *cwin = find_window_xid(ev->drawable);
235 if(!cwin || !cwin->damage) return;
237 region = XFixesCreateRegion(dpy, 0, 0);
239 push_xerr_handler(xerr_debug);
240 XDamageSubtract(dpy, cwin->damage, None, region);
241 pop_xerr_handler();
243 XFixesDestroyRegion(dpy, region);
244 cwin->damaged = true;
245 }
247 static void redraw()
248 {
249 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
251 glPushAttrib(GL_ENABLE_BIT);
252 glEnable(GL_TEXTURE_2D);
253 glEnable(GL_BLEND);
254 glBlendFunc(GL_ONE, GL_ONE);
256 int num_win = get_window_count();
257 for(int i=0; i<num_win; i++) {
258 CompWindow *cwin = get_window(i);
260 if(!cwin->mapped) continue;
262 if(cwin->attr.x + cwin->attr.width <= 0 ||
263 cwin->attr.y + cwin->attr.height <= 0 ||
264 cwin->attr.x >= root_width || cwin->attr.y >= root_height) {
265 continue;
266 }
268 draw_window(cwin);
269 }
271 glPopAttrib();
273 glXSwapBuffers(dpy, comp_win);
274 }
276 static void draw_window(CompWindow *cwin)
277 {
278 if(!cwin->xpixmap || !cwin->pixmap_valid) {
279 cwin->update_pixmap();
280 if(!cwin->tex_valid) {
281 cwin->update_texture();
282 }
283 }
285 glBindTexture(GL_TEXTURE_2D, cwin->tex.get_id());
287 glBegin(GL_QUADS);
288 glColor3f(0.3, 0.3, 0.3);
289 glTexCoord2f(0, 0);
290 glVertex2f(cwin->attr.x, cwin->attr.y);
291 glTexCoord2f(1, 0);
292 glVertex2f(cwin->attr.x + cwin->attr.width, cwin->attr.y);
293 glTexCoord2f(1, 1);
294 glVertex2f(cwin->attr.x + cwin->attr.width, cwin->attr.y + cwin->attr.height);
295 glTexCoord2f(0, 1);
296 glVertex2f(cwin->attr.x, cwin->attr.y + cwin->attr.height);
297 glEnd();
299 cwin->damaged = false;
300 }
302 static void reshape(int x, int y)
303 {
304 glViewport(0, 0, x, y);
306 glMatrixMode(GL_PROJECTION);
307 glLoadIdentity();
308 glOrtho(0, x, y, 0, -1, 1);
309 }