xglcomp
view src/main.cc @ 7:03ca0fd49916
merged
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 30 Jan 2016 07:48:51 +0200 |
parents | 86e57e56a454 |
children | b0081a0c211f |
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 XGetWindowAttributes(dpy, xwin, &cwin->attr);
212 cwin->mapped = cwin->attr.map_state != IsUnmapped;
213 add_window(cwin);
214 }
216 static void unmanage_window(Window xwin)
217 {
218 //log_debug("unmanage_window %u\n", xwin);
219 CompWindow *cwin = find_window_xid(xwin);
220 if(!cwin) return;
222 remove_window(cwin);
223 delete cwin;
224 }
226 static void damage_window(XDamageNotifyEvent *ev)
227 {
228 XserverRegion region;
229 CompWindow *cwin = find_window_xid(ev->drawable);
230 if(!cwin) return;
232 region = XFixesCreateRegion(dpy, 0, 0);
233 XDamageSubtract(dpy, cwin->damage, None, region);
234 }
236 static void redraw()
237 {
238 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
240 glPushAttrib(GL_ENABLE_BIT);
241 glEnable(GL_TEXTURE_2D);
242 glEnable(GL_BLEND);
243 glBlendFunc(GL_ONE, GL_ONE);
245 int num_win = get_window_count();
246 for(int i=0; i<num_win; i++) {
247 CompWindow *cwin = get_window(i);
249 if(!cwin->mapped) continue;
251 if(cwin->attr.x + cwin->attr.width <= 0 ||
252 cwin->attr.y + cwin->attr.height <= 0 ||
253 cwin->attr.x >= root_width || cwin->attr.y >= root_height) {
254 continue;
255 }
257 draw_window(cwin);
258 }
260 glPopAttrib();
262 glXSwapBuffers(dpy, comp_win);
263 }
265 static void draw_window(CompWindow *cwin)
266 {
267 if(!cwin->xpixmap) {
268 cwin->xpixmap = XCompositeNameWindowPixmap(dpy, cwin->xwin);
269 if(!cwin->xpixmap) {
270 log_warning("failed to get pixmap\n");
271 return;
272 }
273 cwin->tex.set_image(dpy, cwin->xpixmap);
274 }
276 glBindTexture(GL_TEXTURE_2D, cwin->tex.get_id());
278 glBegin(GL_QUADS);
279 glColor3f(0.3, 0.3, 0.3);
280 glTexCoord2f(0, 0);
281 glVertex2f(cwin->attr.x, cwin->attr.y);
282 glTexCoord2f(1, 0);
283 glVertex2f(cwin->attr.x + cwin->attr.width, cwin->attr.y);
284 glTexCoord2f(1, 1);
285 glVertex2f(cwin->attr.x + cwin->attr.width, cwin->attr.y + cwin->attr.height);
286 glTexCoord2f(0, 1);
287 glVertex2f(cwin->attr.x, cwin->attr.y + cwin->attr.height);
288 glEnd();
289 }
291 static void reshape(int x, int y)
292 {
293 glViewport(0, 0, x, y);
295 glMatrixMode(GL_PROJECTION);
296 glLoadIdentity();
297 glOrtho(0, x, y, 0, -1, 1);
298 }