xglcomp

annotate src/main.cc @ 11:cb636a23f4f2

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