xglcomp

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