xglcomp
view src/main.cc @ 6:3f908f812ec7
added docs
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 29 Jan 2016 10:23:08 +0200 |
parents | e831d38e6faa |
children | 03ca0fd49916 |
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 redraw();
20 static void draw_window(CompWindow *cwin);
21 static void reshape(int x, int y);
23 Display *dpy;
24 int screen_num;
25 Window root_win, comp_win;
26 int root_width, root_height;
28 static int xdmg_ev_base, xdmg_err_base;
29 static int xshape_ev_base, xshape_err_base;
31 int main(int argc, char **argv)
32 {
33 if(!(dpy = XOpenDisplay(0))) {
34 log_error("failed to open X display\n");
35 return 1;
36 }
37 screen_num = DefaultScreen(dpy);
38 root_win = RootWindow(dpy, screen_num);
39 root_width = DisplayWidth(dpy, screen_num);
40 root_height = DisplayHeight(dpy, screen_num);
41 log_info("display size %dx%d\n", root_width, root_height);
43 if(!query_extensions()) {
44 return 1;
45 }
47 if(!register_compositor()) {
48 return 1;
49 }
51 comp_win = XCompositeGetOverlayWindow(dpy, root_win);
52 if(!create_gl(comp_win)) {
53 return 1;
54 }
56 start_comp();
57 reshape(root_width, root_height);
58 redraw();
60 for(;;) {
61 XEvent ev;
62 XNextEvent(dpy, &ev);
64 CompWindow *cwin;
65 switch(ev.type) {
66 case CreateNotify:
67 manage_window(ev.xcreatewindow.window);
68 break;
70 case ConfigureNotify:
71 // XXX is this right?
72 if((cwin = find_window_xid(ev.xconfigure.window))) {
73 log_debug("updating window attributes\n");
74 XGetWindowAttributes(dpy, cwin->xwin, &cwin->attr);
75 }
76 break;
78 case DestroyNotify:
79 unmanage_window(ev.xdestroywindow.window);
80 break;
82 case MapNotify:
83 if((cwin = find_window_xid(ev.xmap.window))) {
84 cwin->mapped = true;
85 }
86 break;
88 case UnmapNotify:
89 if((cwin = find_window_xid(ev.xunmap.window))) {
90 cwin->mapped = false;
91 }
92 break;
94 case ReparentNotify:
95 if(ev.xreparent.parent == root_win) {
96 manage_window(ev.xreparent.window);
97 } else {
98 unmanage_window(ev.xreparent.window);
99 }
100 break;
102 case CirculateNotify:
103 break;
105 case Expose:
106 // TODO?
107 break;
109 default:
110 if(ev.type == xdmg_ev_base + XDamageNotify) {
111 }
112 }
114 redraw();
115 }
116 }
118 static bool query_extensions()
119 {
120 int xcomp_ev_base, xcomp_err_base;
121 if(!XCompositeQueryExtension(dpy, &xcomp_ev_base, &xcomp_err_base)) {
122 log_error("X server doesn't support the composite extension\n");
123 return false;
124 }
125 int xcomp_ver_major, xcomp_ver_minor;
126 XCompositeQueryVersion(dpy, &xcomp_ver_major, &xcomp_ver_minor);
127 log_info("Found composite extension version %d.%d\n", xcomp_ver_major, xcomp_ver_minor);
128 if(xcomp_ver_major <= 0 && xcomp_ver_minor < 3) {
129 // for NameWindowPixmap & CompositeGetoverlayWindow
130 log_error("I need at least version 0.3\n");
131 return false;
132 }
134 if(!XDamageQueryExtension(dpy, &xdmg_ev_base, &xdmg_err_base)) {
135 log_error("X server doesn't support the damage extension\n");
136 return false;
137 }
139 if(!XShapeQueryExtension(dpy, &xshape_ev_base, &xshape_err_base)) {
140 log_error("X server doesn't support the shape extension\n");
141 return false;
142 }
144 int xfix_ev_base, xfix_err_base;
145 if(!XFixesQueryExtension(dpy, &xfix_ev_base, &xfix_err_base)) {
146 log_error("X server doesn't support the Xfixes extension\n");
147 return false;
148 }
150 int xfix_ver_major, xfix_ver_minor;
151 XFixesQueryVersion(dpy, &xfix_ver_major, &xfix_ver_minor);
152 log_info("Found xfixes version %d.%d\n", xfix_ver_major, xfix_ver_minor);
153 if(xfix_ver_major < 2) {
154 // for SetWindowShapeRegion
155 log_error("I need at least version 2.0\n");
156 return false;
157 }
158 return true;
159 }
161 static bool register_compositor()
162 {
163 char atom_name[64];
164 sprintf(atom_name, "_NET_WM_CM_S%d", screen_num);
165 Atom xa_wm_cm = XInternAtom(dpy, atom_name, False);
167 Window win = XGetSelectionOwner(dpy, xa_wm_cm);
168 if(win != None) {
169 log_error("Another compositor is running. Stop it and try again.\n");
170 return false;
171 }
173 win = XCreateSimpleWindow(dpy, root_win, 0, 0, 1, 1, 0, None, None);
174 XSetSelectionOwner(dpy, xa_wm_cm, win, 0);
175 return true;
176 }
178 static void start_comp()
179 {
180 XGrabServer(dpy);
181 XCompositeRedirectSubwindows(dpy, root_win, CompositeRedirectManual);
182 XSelectInput(dpy, root_win, SubstructureNotifyMask | ExposureMask | StructureNotifyMask);
184 // manage all top-level windows
185 Window root_ret, parent;
186 Window *children;
187 unsigned int num_children;
188 XQueryTree(dpy, root_win, &root_ret, &parent, &children, &num_children);
190 for(unsigned int i=0; i<num_children; i++) {
191 manage_window(children[i]);
192 }
193 log_info("starting compositor, managing %u top-level windows\n", num_children);
194 XFree(children);
195 XUngrabServer(dpy);
197 /* set a zero-area input-shape for the overlay window to let events
198 * through to the windows under it
199 */
200 XserverRegion region = XFixesCreateRegion(dpy, 0, 0);
201 XFixesSetWindowShapeRegion(dpy, comp_win, ShapeInput, 0, 0, region);
202 XFixesDestroyRegion(dpy, region);
203 }
205 static void manage_window(Window xwin)
206 {
207 //log_debug("manage_window %u\n", xwin);
208 CompWindow *cwin = new CompWindow(xwin);
209 XGetWindowAttributes(dpy, xwin, &cwin->attr);
210 cwin->mapped = cwin->attr.map_state != IsUnmapped;
211 add_window(cwin);
212 }
214 static void unmanage_window(Window xwin)
215 {
216 //log_debug("unmanage_window %u\n", xwin);
217 CompWindow *cwin = find_window_xid(xwin);
218 if(!cwin) return;
220 if(cwin->xpixmap) {
221 XFreePixmap(dpy, cwin->xpixmap);
222 }
224 remove_window(cwin);
225 delete cwin;
226 }
228 static void redraw()
229 {
230 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
232 glPushAttrib(GL_ENABLE_BIT);
233 glEnable(GL_TEXTURE_2D);
234 glEnable(GL_BLEND);
235 glBlendFunc(GL_ONE, GL_ONE);
237 int num_win = get_window_count();
238 for(int i=0; i<num_win; i++) {
239 CompWindow *cwin = get_window(i);
241 if(!cwin->mapped) continue;
243 if(cwin->attr.x + cwin->attr.width <= 0 ||
244 cwin->attr.y + cwin->attr.height <= 0 ||
245 cwin->attr.x >= root_width || cwin->attr.y >= root_height) {
246 continue;
247 }
249 draw_window(cwin);
250 }
252 glPopAttrib();
254 glXSwapBuffers(dpy, comp_win);
255 }
257 static void draw_window(CompWindow *cwin)
258 {
259 if(!cwin->xpixmap) {
260 cwin->xpixmap = XCompositeNameWindowPixmap(dpy, cwin->xwin);
261 if(!cwin->xpixmap) {
262 log_warning("failed to get pixmap\n");
263 return;
264 }
265 cwin->tex.set_image(dpy, cwin->xpixmap);
266 }
268 glBindTexture(GL_TEXTURE_2D, cwin->tex.get_id());
270 glBegin(GL_QUADS);
271 glColor3f(0.3, 0.3, 0.3);
272 glTexCoord2f(0, 0);
273 glVertex2f(cwin->attr.x, cwin->attr.y);
274 glTexCoord2f(1, 0);
275 glVertex2f(cwin->attr.x + cwin->attr.width, cwin->attr.y);
276 glTexCoord2f(1, 1);
277 glVertex2f(cwin->attr.x + cwin->attr.width, cwin->attr.y + cwin->attr.height);
278 glTexCoord2f(0, 1);
279 glVertex2f(cwin->attr.x, cwin->attr.y + cwin->attr.height);
280 glEnd();
281 }
283 static void reshape(int x, int y)
284 {
285 glViewport(0, 0, x, y);
287 glMatrixMode(GL_PROJECTION);
288 glLoadIdentity();
289 glOrtho(0, x, y, 0, -1, 1);
290 }