xglcomp

view src/main.cc @ 12:1c0d056ec360

moving slowly
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 05 Feb 2016 03:33:18 +0200
parents cb636a23f4f2
children 593a5e4a351f
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 log_debug("updating window attributes for: %x\n", cwin->xwin);
77 cwin->update_attr();
78 }
79 break;
81 case DestroyNotify:
82 log_debug("DestroyNotify: %x\n", ev.xdestroywindow.window);
83 unmanage_window(ev.xdestroywindow.window);
84 break;
86 case MapNotify:
87 if((cwin = find_window_xid(ev.xmap.window))) {
88 cwin->mapped = true;
89 }
90 break;
92 case UnmapNotify:
93 if((cwin = find_window_xid(ev.xunmap.window))) {
94 cwin->mapped = false;
95 }
96 break;
98 case ReparentNotify:
99 if(ev.xreparent.parent == root_win) {
100 manage_window(ev.xreparent.window);
101 } else {
102 unmanage_window(ev.xreparent.window);
103 }
104 break;
106 case CirculateNotify:
107 break;
109 case Expose:
110 // TODO?
111 break;
113 default:
114 if(ev.type == xdmg_ev_base + XDamageNotify) {
115 damage_window((XDamageNotifyEvent*)&ev);
116 }
117 }
119 redraw();
120 }
121 }
123 static bool query_extensions()
124 {
125 int xcomp_ev_base, xcomp_err_base;
126 if(!XCompositeQueryExtension(dpy, &xcomp_ev_base, &xcomp_err_base)) {
127 log_error("X server doesn't support the composite extension\n");
128 return false;
129 }
130 int xcomp_ver_major, xcomp_ver_minor;
131 XCompositeQueryVersion(dpy, &xcomp_ver_major, &xcomp_ver_minor);
132 log_info("Found composite extension version %d.%d\n", xcomp_ver_major, xcomp_ver_minor);
133 if(xcomp_ver_major <= 0 && xcomp_ver_minor < 3) {
134 // for NameWindowPixmap & CompositeGetoverlayWindow
135 log_error("I need at least version 0.3\n");
136 return false;
137 }
139 if(!XDamageQueryExtension(dpy, &xdmg_ev_base, &xdmg_err_base)) {
140 log_error("X server doesn't support the damage extension\n");
141 return false;
142 }
144 if(!XShapeQueryExtension(dpy, &xshape_ev_base, &xshape_err_base)) {
145 log_error("X server doesn't support the shape extension\n");
146 return false;
147 }
149 int xfix_ev_base, xfix_err_base;
150 if(!XFixesQueryExtension(dpy, &xfix_ev_base, &xfix_err_base)) {
151 log_error("X server doesn't support the Xfixes extension\n");
152 return false;
153 }
155 int xfix_ver_major, xfix_ver_minor;
156 XFixesQueryVersion(dpy, &xfix_ver_major, &xfix_ver_minor);
157 log_info("Found xfixes version %d.%d\n", xfix_ver_major, xfix_ver_minor);
158 if(xfix_ver_major < 2) {
159 // for SetWindowShapeRegion
160 log_error("I need at least version 2.0\n");
161 return false;
162 }
163 return true;
164 }
166 static bool register_compositor()
167 {
168 char atom_name[64];
169 sprintf(atom_name, "_NET_WM_CM_S%d", screen_num);
170 Atom xa_wm_cm = XInternAtom(dpy, atom_name, False);
172 Window win = XGetSelectionOwner(dpy, xa_wm_cm);
173 if(win != None) {
174 log_error("Another compositor is running. Stop it and try again.\n");
175 return false;
176 }
178 win = XCreateSimpleWindow(dpy, root_win, 0, 0, 1, 1, 0, None, None);
179 XSetSelectionOwner(dpy, xa_wm_cm, win, 0);
180 return true;
181 }
183 static void start_comp()
184 {
185 XGrabServer(dpy);
186 XCompositeRedirectSubwindows(dpy, root_win, CompositeRedirectManual);
187 XSelectInput(dpy, root_win, SubstructureNotifyMask | ExposureMask | StructureNotifyMask);
189 // manage all top-level windows
190 Window root_ret, parent;
191 Window *children;
192 unsigned int num_children;
193 XQueryTree(dpy, root_win, &root_ret, &parent, &children, &num_children);
195 for(unsigned int i=0; i<num_children; i++) {
196 manage_window(children[i]);
197 }
198 log_info("starting compositor, managing %u top-level windows\n", num_children);
199 XFree(children);
200 XUngrabServer(dpy);
202 /* set a zero-area input-shape for the overlay window to let events
203 * through to the windows under it
204 */
205 XserverRegion region = XFixesCreateRegion(dpy, 0, 0);
206 XFixesSetWindowShapeRegion(dpy, comp_win, ShapeInput, 0, 0, region);
207 XFixesDestroyRegion(dpy, region);
208 }
210 static void manage_window(Window xwin)
211 {
212 //log_debug("manage_window %u\n", xwin);
213 CompWindow *cwin = new CompWindow(xwin);
214 add_window(cwin);
215 cwin->mapped = cwin->attr.map_state != IsUnmapped;
216 }
218 static void unmanage_window(Window xwin)
219 {
220 //log_debug("unmanage_window %u\n", xwin);
221 CompWindow *cwin = find_window_xid(xwin);
222 if(!cwin) return;
224 remove_window(cwin);
225 delete cwin;
226 }
228 static void damage_window(XDamageNotifyEvent *ev)
229 {
230 XserverRegion region;
231 CompWindow *cwin = find_window_xid(ev->drawable);
232 if(!cwin || !cwin->damage) return;
234 region = XFixesCreateRegion(dpy, 0, 0);
236 push_xerr_handler(xerr_debug);
237 XDamageSubtract(dpy, cwin->damage, None, region);
238 pop_xerr_handler();
240 XFixesDestroyRegion(dpy, region);
241 cwin->damaged = true;
242 }
244 static void redraw()
245 {
246 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
248 glPushAttrib(GL_ENABLE_BIT);
249 glEnable(GL_TEXTURE_2D);
250 glEnable(GL_BLEND);
251 glBlendFunc(GL_ONE, GL_ONE);
253 int num_win = get_window_count();
254 for(int i=0; i<num_win; i++) {
255 CompWindow *cwin = get_window(i);
257 if(!cwin->mapped) continue;
259 if(cwin->attr.x + cwin->attr.width <= 0 ||
260 cwin->attr.y + cwin->attr.height <= 0 ||
261 cwin->attr.x >= root_width || cwin->attr.y >= root_height) {
262 continue;
263 }
265 draw_window(cwin);
266 }
268 glPopAttrib();
270 glXSwapBuffers(dpy, comp_win);
271 }
273 static void draw_window(CompWindow *cwin)
274 {
275 if(!cwin->xpixmap || !cwin->pixmap_valid) {
276 cwin->update_pixmap();
277 if(!cwin->tex_valid) {
278 cwin->update_texture();
279 }
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();
296 cwin->damaged = false;
297 }
299 static void reshape(int x, int y)
300 {
301 glViewport(0, 0, x, y);
303 glMatrixMode(GL_PROJECTION);
304 glLoadIdentity();
305 glOrtho(0, x, y, 0, -1, 1);
306 }