xglcomp

annotate src/main.cc @ 2:876efea9424c

OpenGL
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 22 Jan 2016 06:31:03 +0200
parents b2b7cb950c28
children e831d38e6faa
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@0 9 #include "cwin.h"
nuclear@2 10 #include "opengl.h"
nuclear@1 11 #include "logger.h"
nuclear@1 12
nuclear@1 13 static bool register_compositor();
nuclear@2 14 static void start_comp();
nuclear@2 15 static void manage_window(Window xwin);
nuclear@2 16 static void unmanage_window(Window xwin);
nuclear@2 17 static void redraw();
nuclear@2 18 static void reshape(int x, int y);
nuclear@1 19
nuclear@2 20 Display *dpy;
nuclear@2 21 int screen_num;
nuclear@2 22 Window root_win, comp_win;
nuclear@2 23 int root_width, root_height;
nuclear@1 24
nuclear@1 25 int main(int argc, char **argv)
nuclear@1 26 {
nuclear@1 27 if(!(dpy = XOpenDisplay(0))) {
nuclear@1 28 log_error("failed to open X display\n");
nuclear@1 29 return 1;
nuclear@1 30 }
nuclear@2 31 screen_num = DefaultScreen(dpy);
nuclear@2 32 root_win = RootWindow(dpy, screen_num);
nuclear@2 33 root_width = DisplayWidth(dpy, screen_num);
nuclear@2 34 root_height = DisplayHeight(dpy, screen_num);
nuclear@1 35 log_info("display size %dx%d\n", root_width, root_height);
nuclear@1 36
nuclear@2 37 int xcomp_ev_base, xcomp_err_base;
nuclear@1 38 if(!XCompositeQueryExtension(dpy, &xcomp_ev_base, &xcomp_err_base)) {
nuclear@1 39 log_error("X server doesn't support the composite extension\n");
nuclear@1 40 return 1;
nuclear@1 41 }
nuclear@2 42 int xcomp_ver_major = 0, xcomp_ver_minor = 4;
nuclear@1 43 XCompositeQueryVersion(dpy, &xcomp_ver_major, &xcomp_ver_minor);
nuclear@1 44 log_info("Found composite extension version %d.%d\n", xcomp_ver_major, xcomp_ver_minor);
nuclear@2 45 if(xcomp_ver_major <= 0 && xcomp_ver_minor < 3) {
nuclear@2 46 // for NameWindowPixmap & CompositeGetoverlayWindow
nuclear@2 47 log_error("I need at least version 0.3\n");
nuclear@1 48 return 1;
nuclear@1 49 }
nuclear@1 50
nuclear@1 51 int xdmg_ev_base, xdmg_err_base;
nuclear@1 52 if(!XDamageQueryExtension(dpy, &xdmg_ev_base, &xdmg_err_base)) {
nuclear@1 53 log_error("X server doesn't support the damage extension\n");
nuclear@1 54 return 1;
nuclear@1 55 }
nuclear@1 56 // TODO also XFixes ?
nuclear@1 57
nuclear@1 58 if(!register_compositor()) {
nuclear@1 59 return 1;
nuclear@1 60 }
nuclear@1 61
nuclear@2 62 comp_win = XCompositeGetOverlayWindow(dpy, root_win);
nuclear@2 63 if(!create_gl(comp_win)) {
nuclear@2 64 return 1;
nuclear@2 65 }
nuclear@2 66
nuclear@2 67 start_comp();
nuclear@2 68 reshape(root_width, root_height);
nuclear@2 69 redraw();
nuclear@1 70
nuclear@1 71 for(;;) {
nuclear@1 72 XEvent ev;
nuclear@1 73 XNextEvent(dpy, &ev);
nuclear@2 74
nuclear@2 75 switch(ev.type) {
nuclear@2 76 case CreateNotify:
nuclear@2 77 manage_window(ev.xcreatewindow.window);
nuclear@2 78 break;
nuclear@2 79
nuclear@2 80 case ConfigureNotify:
nuclear@2 81 break;
nuclear@2 82
nuclear@2 83 case DestroyNotify:
nuclear@2 84 unmanage_window(ev.xdestroywindow.window);
nuclear@2 85 break;
nuclear@2 86
nuclear@2 87 case MapNotify:
nuclear@2 88 break;
nuclear@2 89 case UnmapNotify:
nuclear@2 90 break;
nuclear@2 91
nuclear@2 92 case ReparentNotify:
nuclear@2 93 if(ev.xreparent.parent == root_win) {
nuclear@2 94 manage_window(ev.xreparent.window);
nuclear@2 95 } else {
nuclear@2 96 unmanage_window(ev.xreparent.window);
nuclear@2 97 }
nuclear@2 98 break;
nuclear@2 99
nuclear@2 100 case CirculateNotify:
nuclear@2 101 break;
nuclear@2 102
nuclear@2 103 case Expose:
nuclear@2 104 if(ev.xexpose.window == root_win) {
nuclear@2 105 log_debug("expose\n");
nuclear@2 106 // TODO
nuclear@2 107 }
nuclear@2 108 break;
nuclear@2 109
nuclear@2 110 default:
nuclear@2 111 if(ev.type == xdmg_ev_base + XDamageNotify) {
nuclear@2 112 }
nuclear@2 113 }
nuclear@1 114 }
nuclear@1 115 }
nuclear@1 116
nuclear@1 117 static bool register_compositor()
nuclear@1 118 {
nuclear@1 119 char atom_name[64];
nuclear@2 120 sprintf(atom_name, "_NET_WM_CM_S%d", screen_num);
nuclear@1 121 Atom xa_wm_cm = XInternAtom(dpy, atom_name, False);
nuclear@1 122
nuclear@1 123 Window win = XGetSelectionOwner(dpy, xa_wm_cm);
nuclear@1 124 if(win != None) {
nuclear@1 125 log_error("Another compositor is running. Stop it and try again.\n");
nuclear@1 126 return false;
nuclear@1 127 }
nuclear@1 128
nuclear@1 129 win = XCreateSimpleWindow(dpy, root_win, 0, 0, 1, 1, 0, None, None);
nuclear@1 130 XSetSelectionOwner(dpy, xa_wm_cm, win, 0);
nuclear@1 131 return true;
nuclear@1 132 }
nuclear@2 133
nuclear@2 134 static void start_comp()
nuclear@2 135 {
nuclear@2 136 XGrabServer(dpy);
nuclear@2 137 XCompositeRedirectSubwindows(dpy, root_win, CompositeRedirectManual);
nuclear@2 138 XSelectInput(dpy, root_win, SubstructureNotifyMask | ExposureMask | StructureNotifyMask);
nuclear@2 139
nuclear@2 140 // manage all top-level windows
nuclear@2 141 Window root_ret, parent;
nuclear@2 142 Window *children;
nuclear@2 143 unsigned int num_children;
nuclear@2 144 XQueryTree(dpy, root_win, &root_ret, &parent, &children, &num_children);
nuclear@2 145
nuclear@2 146 for(unsigned int i=0; i<num_children; i++) {
nuclear@2 147 manage_window(children[i]);
nuclear@2 148 }
nuclear@2 149 log_info("starting compositor, managing %u top-level windows\n", num_children);
nuclear@2 150 XFree(children);
nuclear@2 151
nuclear@2 152 XUngrabServer(dpy);
nuclear@2 153 }
nuclear@2 154
nuclear@2 155 static void manage_window(Window xwin)
nuclear@2 156 {
nuclear@2 157 //log_debug("manage_window %u\n", xwin);
nuclear@2 158 CompWindow *cwin = new CompWindow(xwin);
nuclear@2 159 XGetWindowAttributes(dpy, xwin, &cwin->attr);
nuclear@2 160 add_window(cwin);
nuclear@2 161 }
nuclear@2 162
nuclear@2 163 static void unmanage_window(Window xwin)
nuclear@2 164 {
nuclear@2 165 //log_debug("unmanage_window %u\n", xwin);
nuclear@2 166 CompWindow *cwin = find_window_xid(xwin);
nuclear@2 167 if(cwin) {
nuclear@2 168 remove_window(cwin);
nuclear@2 169 delete cwin;
nuclear@2 170 }
nuclear@2 171 }
nuclear@2 172
nuclear@2 173 static void redraw()
nuclear@2 174 {
nuclear@2 175 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@2 176
nuclear@2 177 glEnable(GL_BLEND);
nuclear@2 178 glBlendFunc(GL_ONE, GL_ONE);
nuclear@2 179
nuclear@2 180 int num_win = get_window_count();
nuclear@2 181 for(int i=0; i<num_win; i++) {
nuclear@2 182 CompWindow *cwin = get_window(i);
nuclear@2 183
nuclear@2 184 glBegin(GL_QUADS);
nuclear@2 185 glColor3f(0.4, 0.1, 0.05);
nuclear@2 186 glVertex2f(cwin->attr.x, cwin->attr.y);
nuclear@2 187 glVertex2f(cwin->attr.x + cwin->attr.width, cwin->attr.y);
nuclear@2 188 glVertex2f(cwin->attr.x + cwin->attr.width, cwin->attr.y + cwin->attr.height);
nuclear@2 189 glVertex2f(cwin->attr.x, cwin->attr.y + cwin->attr.height);
nuclear@2 190 glEnd();
nuclear@2 191 }
nuclear@2 192
nuclear@2 193 glDisable(GL_BLEND);
nuclear@2 194
nuclear@2 195 glXSwapBuffers(dpy, comp_win);
nuclear@2 196 }
nuclear@2 197
nuclear@2 198 static void reshape(int x, int y)
nuclear@2 199 {
nuclear@2 200 glViewport(0, 0, x, y);
nuclear@2 201
nuclear@2 202 glMatrixMode(GL_PROJECTION);
nuclear@2 203 glLoadIdentity();
nuclear@2 204 glOrtho(0, x, y, 0, -1, 1);
nuclear@2 205 }