xglcomp

annotate 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
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 if((cwin = find_window_xid(ev.xconfigure.window))) {
nuclear@9 76 log_debug("updating window attributes for: %x\n", cwin->xwin);
nuclear@12 77 cwin->update_attr();
nuclear@5 78 }
nuclear@2 79 break;
nuclear@2 80
nuclear@2 81 case DestroyNotify:
nuclear@9 82 log_debug("DestroyNotify: %x\n", ev.xdestroywindow.window);
nuclear@2 83 unmanage_window(ev.xdestroywindow.window);
nuclear@2 84 break;
nuclear@2 85
nuclear@2 86 case MapNotify:
nuclear@3 87 if((cwin = find_window_xid(ev.xmap.window))) {
nuclear@3 88 cwin->mapped = true;
nuclear@3 89 }
nuclear@2 90 break;
nuclear@3 91
nuclear@2 92 case UnmapNotify:
nuclear@3 93 if((cwin = find_window_xid(ev.xunmap.window))) {
nuclear@3 94 cwin->mapped = false;
nuclear@3 95 }
nuclear@2 96 break;
nuclear@2 97
nuclear@2 98 case ReparentNotify:
nuclear@2 99 if(ev.xreparent.parent == root_win) {
nuclear@2 100 manage_window(ev.xreparent.window);
nuclear@2 101 } else {
nuclear@2 102 unmanage_window(ev.xreparent.window);
nuclear@2 103 }
nuclear@2 104 break;
nuclear@2 105
nuclear@2 106 case CirculateNotify:
nuclear@2 107 break;
nuclear@2 108
nuclear@2 109 case Expose:
nuclear@3 110 // TODO?
nuclear@2 111 break;
nuclear@2 112
nuclear@2 113 default:
nuclear@2 114 if(ev.type == xdmg_ev_base + XDamageNotify) {
nuclear@7 115 damage_window((XDamageNotifyEvent*)&ev);
nuclear@2 116 }
nuclear@2 117 }
nuclear@5 118
nuclear@5 119 redraw();
nuclear@1 120 }
nuclear@1 121 }
nuclear@1 122
nuclear@5 123 static bool query_extensions()
nuclear@5 124 {
nuclear@5 125 int xcomp_ev_base, xcomp_err_base;
nuclear@5 126 if(!XCompositeQueryExtension(dpy, &xcomp_ev_base, &xcomp_err_base)) {
nuclear@5 127 log_error("X server doesn't support the composite extension\n");
nuclear@5 128 return false;
nuclear@5 129 }
nuclear@5 130 int xcomp_ver_major, xcomp_ver_minor;
nuclear@5 131 XCompositeQueryVersion(dpy, &xcomp_ver_major, &xcomp_ver_minor);
nuclear@5 132 log_info("Found composite extension version %d.%d\n", xcomp_ver_major, xcomp_ver_minor);
nuclear@5 133 if(xcomp_ver_major <= 0 && xcomp_ver_minor < 3) {
nuclear@5 134 // for NameWindowPixmap & CompositeGetoverlayWindow
nuclear@5 135 log_error("I need at least version 0.3\n");
nuclear@5 136 return false;
nuclear@5 137 }
nuclear@5 138
nuclear@5 139 if(!XDamageQueryExtension(dpy, &xdmg_ev_base, &xdmg_err_base)) {
nuclear@5 140 log_error("X server doesn't support the damage extension\n");
nuclear@5 141 return false;
nuclear@5 142 }
nuclear@5 143
nuclear@5 144 if(!XShapeQueryExtension(dpy, &xshape_ev_base, &xshape_err_base)) {
nuclear@5 145 log_error("X server doesn't support the shape extension\n");
nuclear@5 146 return false;
nuclear@5 147 }
nuclear@5 148
nuclear@5 149 int xfix_ev_base, xfix_err_base;
nuclear@5 150 if(!XFixesQueryExtension(dpy, &xfix_ev_base, &xfix_err_base)) {
nuclear@5 151 log_error("X server doesn't support the Xfixes extension\n");
nuclear@5 152 return false;
nuclear@5 153 }
nuclear@5 154
nuclear@5 155 int xfix_ver_major, xfix_ver_minor;
nuclear@5 156 XFixesQueryVersion(dpy, &xfix_ver_major, &xfix_ver_minor);
nuclear@5 157 log_info("Found xfixes version %d.%d\n", xfix_ver_major, xfix_ver_minor);
nuclear@5 158 if(xfix_ver_major < 2) {
nuclear@5 159 // for SetWindowShapeRegion
nuclear@5 160 log_error("I need at least version 2.0\n");
nuclear@5 161 return false;
nuclear@5 162 }
nuclear@5 163 return true;
nuclear@5 164 }
nuclear@5 165
nuclear@1 166 static bool register_compositor()
nuclear@1 167 {
nuclear@1 168 char atom_name[64];
nuclear@2 169 sprintf(atom_name, "_NET_WM_CM_S%d", screen_num);
nuclear@1 170 Atom xa_wm_cm = XInternAtom(dpy, atom_name, False);
nuclear@1 171
nuclear@1 172 Window win = XGetSelectionOwner(dpy, xa_wm_cm);
nuclear@1 173 if(win != None) {
nuclear@1 174 log_error("Another compositor is running. Stop it and try again.\n");
nuclear@1 175 return false;
nuclear@1 176 }
nuclear@1 177
nuclear@1 178 win = XCreateSimpleWindow(dpy, root_win, 0, 0, 1, 1, 0, None, None);
nuclear@1 179 XSetSelectionOwner(dpy, xa_wm_cm, win, 0);
nuclear@1 180 return true;
nuclear@1 181 }
nuclear@2 182
nuclear@2 183 static void start_comp()
nuclear@2 184 {
nuclear@2 185 XGrabServer(dpy);
nuclear@2 186 XCompositeRedirectSubwindows(dpy, root_win, CompositeRedirectManual);
nuclear@2 187 XSelectInput(dpy, root_win, SubstructureNotifyMask | ExposureMask | StructureNotifyMask);
nuclear@2 188
nuclear@2 189 // manage all top-level windows
nuclear@2 190 Window root_ret, parent;
nuclear@2 191 Window *children;
nuclear@2 192 unsigned int num_children;
nuclear@2 193 XQueryTree(dpy, root_win, &root_ret, &parent, &children, &num_children);
nuclear@2 194
nuclear@2 195 for(unsigned int i=0; i<num_children; i++) {
nuclear@2 196 manage_window(children[i]);
nuclear@2 197 }
nuclear@2 198 log_info("starting compositor, managing %u top-level windows\n", num_children);
nuclear@2 199 XFree(children);
nuclear@5 200 XUngrabServer(dpy);
nuclear@2 201
nuclear@5 202 /* set a zero-area input-shape for the overlay window to let events
nuclear@5 203 * through to the windows under it
nuclear@5 204 */
nuclear@5 205 XserverRegion region = XFixesCreateRegion(dpy, 0, 0);
nuclear@5 206 XFixesSetWindowShapeRegion(dpy, comp_win, ShapeInput, 0, 0, region);
nuclear@5 207 XFixesDestroyRegion(dpy, region);
nuclear@2 208 }
nuclear@2 209
nuclear@2 210 static void manage_window(Window xwin)
nuclear@2 211 {
nuclear@2 212 //log_debug("manage_window %u\n", xwin);
nuclear@2 213 CompWindow *cwin = new CompWindow(xwin);
nuclear@8 214 add_window(cwin);
nuclear@3 215 cwin->mapped = cwin->attr.map_state != IsUnmapped;
nuclear@2 216 }
nuclear@2 217
nuclear@2 218 static void unmanage_window(Window xwin)
nuclear@2 219 {
nuclear@2 220 //log_debug("unmanage_window %u\n", xwin);
nuclear@2 221 CompWindow *cwin = find_window_xid(xwin);
nuclear@3 222 if(!cwin) return;
nuclear@3 223
nuclear@3 224 remove_window(cwin);
nuclear@3 225 delete cwin;
nuclear@2 226 }
nuclear@2 227
nuclear@7 228 static void damage_window(XDamageNotifyEvent *ev)
nuclear@7 229 {
nuclear@7 230 XserverRegion region;
nuclear@7 231 CompWindow *cwin = find_window_xid(ev->drawable);
nuclear@9 232 if(!cwin || !cwin->damage) return;
nuclear@7 233
nuclear@7 234 region = XFixesCreateRegion(dpy, 0, 0);
nuclear@9 235
nuclear@9 236 push_xerr_handler(xerr_debug);
nuclear@7 237 XDamageSubtract(dpy, cwin->damage, None, region);
nuclear@9 238 pop_xerr_handler();
nuclear@11 239
nuclear@11 240 XFixesDestroyRegion(dpy, region);
nuclear@12 241 cwin->damaged = true;
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@12 275 if(!cwin->xpixmap || !cwin->pixmap_valid) {
nuclear@12 276 cwin->update_pixmap();
nuclear@12 277 if(!cwin->tex_valid) {
nuclear@12 278 cwin->update_texture();
nuclear@3 279 }
nuclear@3 280 }
nuclear@3 281
nuclear@3 282 glBindTexture(GL_TEXTURE_2D, cwin->tex.get_id());
nuclear@3 283
nuclear@3 284 glBegin(GL_QUADS);
nuclear@3 285 glColor3f(0.3, 0.3, 0.3);
nuclear@3 286 glTexCoord2f(0, 0);
nuclear@3 287 glVertex2f(cwin->attr.x, cwin->attr.y);
nuclear@3 288 glTexCoord2f(1, 0);
nuclear@3 289 glVertex2f(cwin->attr.x + cwin->attr.width, cwin->attr.y);
nuclear@3 290 glTexCoord2f(1, 1);
nuclear@3 291 glVertex2f(cwin->attr.x + cwin->attr.width, cwin->attr.y + cwin->attr.height);
nuclear@3 292 glTexCoord2f(0, 1);
nuclear@3 293 glVertex2f(cwin->attr.x, cwin->attr.y + cwin->attr.height);
nuclear@3 294 glEnd();
nuclear@12 295
nuclear@12 296 cwin->damaged = false;
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 }