xglcomp

changeset 1:b2b7cb950c28

window list and compositor registration
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 21 Jan 2016 22:50:10 +0200
parents d9b3fba68705
children 876efea9424c
files Makefile src/cwin.cc src/cwin.h src/main.cc
diffstat 4 files changed, 129 insertions(+), 5 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Thu Jan 21 08:45:31 2016 +0200
     1.2 +++ b/Makefile	Thu Jan 21 22:50:10 2016 +0200
     1.3 @@ -12,8 +12,8 @@
     1.4  incpaths = -Isrc/optcfg
     1.5  
     1.6  CFLAGS = $(warn) $(dbg) $(opt) $(incpaths)
     1.7 -CXXFLAGS = $(warn) $(dbg) $(opt) $(incpaths)
     1.8 -LDFLAGS = -lGL -lGLEW -lX11 -lXext -lXcomposite -lm
     1.9 +CXXFLAGS = -std=c++11 $(warn) $(dbg) $(opt) $(incpaths)
    1.10 +LDFLAGS = -lGL -lGLEW -lX11 -lXext -lXcomposite -lXdamage -lm -lpthread
    1.11  
    1.12  $(bin): $(obj)
    1.13  	$(CXX) -o $@ $(obj) $(LDFLAGS)
     2.1 --- a/src/cwin.cc	Thu Jan 21 08:45:31 2016 +0200
     2.2 +++ b/src/cwin.cc	Thu Jan 21 22:50:10 2016 +0200
     2.3 @@ -1,8 +1,9 @@
     2.4  #include <vector>
     2.5 +#include <algorithm>
     2.6  #include "cwin.h"
     2.7  #include "logger.h"
     2.8  
     2.9 -static std::vector<CompWindow> cwinlist;
    2.10 +static std::vector<CompWindow*> cwinlist;
    2.11  
    2.12  void add_window(CompWindow *cwin)
    2.13  {
    2.14 @@ -14,4 +15,54 @@
    2.15  	cwinlist.push_back(cwin);
    2.16  }
    2.17  
    2.18 -void delete_window(
    2.19 +bool delete_window(CompWindow *cwin)
    2.20 +{
    2.21 +	auto it = std::find(cwinlist.begin(), cwinlist.end(), cwin);
    2.22 +	if(it != cwinlist.end()) {
    2.23 +		cwinlist.erase(it);
    2.24 +		return true;
    2.25 +	}
    2.26 +	return false;
    2.27 +}
    2.28 +
    2.29 +bool have_window(CompWindow *cwin)
    2.30 +{
    2.31 +	return std::find(cwinlist.begin(), cwinlist.end(), cwin) != cwinlist.end();
    2.32 +}
    2.33 +
    2.34 +CompWindow *find_window_xid(Window xid)
    2.35 +{
    2.36 +	if(!xid) return 0;
    2.37 +
    2.38 +	for(size_t i=0; i<cwinlist.size(); i++) {
    2.39 +		if(cwinlist[i]->xwin == xid) {
    2.40 +			return cwinlist[i];
    2.41 +		}
    2.42 +	}
    2.43 +	return 0;
    2.44 +}
    2.45 +
    2.46 +CompWindow *find_window_pixmap(Pixmap pix)
    2.47 +{
    2.48 +	if(!pix) return 0;
    2.49 +
    2.50 +	for(size_t i=0; i<cwinlist.size(); i++) {
    2.51 +		if(cwinlist[i]->xpixmap == pix) {
    2.52 +			return cwinlist[i];
    2.53 +		}
    2.54 +	}
    2.55 +	return 0;
    2.56 +}
    2.57 +
    2.58 +int get_window_count()
    2.59 +{
    2.60 +	return (int)cwinlist.size();
    2.61 +}
    2.62 +
    2.63 +CompWindow *get_window(int idx)
    2.64 +{
    2.65 +	if(idx < 0 || idx >= (int)cwinlist.size()) {
    2.66 +		return 0;
    2.67 +	}
    2.68 +	return cwinlist[idx];
    2.69 +}
     3.1 --- a/src/cwin.h	Thu Jan 21 08:45:31 2016 +0200
     3.2 +++ b/src/cwin.h	Thu Jan 21 22:50:10 2016 +0200
     3.3 @@ -11,9 +11,10 @@
     3.4  };
     3.5  
     3.6  void add_window(CompWindow *cwin);
     3.7 -void delete_window(CompWindow *cwin);
     3.8 +bool delete_window(CompWindow *cwin);
     3.9  bool have_window(CompWindow *cwin);
    3.10  CompWindow *find_window_xid(Window xid);
    3.11 +CompWindow *find_window_pixmap(Pixmap pix);
    3.12  
    3.13  int get_window_count();
    3.14  CompWindow *get_window(int idx);
     4.1 --- a/src/main.cc	Thu Jan 21 08:45:31 2016 +0200
     4.2 +++ b/src/main.cc	Thu Jan 21 22:50:10 2016 +0200
     4.3 @@ -5,5 +5,77 @@
     4.4  #include <X11/Xutil.h>
     4.5  #include <X11/Xatom.h>
     4.6  #include <X11/extensions/Xcomposite.h>
     4.7 +#include <X11/extensions/Xdamage.h>
     4.8  #include <GL/glew.h>
     4.9  #include "cwin.h"
    4.10 +#include "logger.h"
    4.11 +
    4.12 +static bool register_compositor();
    4.13 +
    4.14 +static Display *dpy;
    4.15 +static int scr;
    4.16 +static Window root_win;
    4.17 +static int root_width, root_height;
    4.18 +
    4.19 +int main(int argc, char **argv)
    4.20 +{
    4.21 +	if(!(dpy = XOpenDisplay(0))) {
    4.22 +		log_error("failed to open X display\n");
    4.23 +		return 1;
    4.24 +	}
    4.25 +	scr = DefaultScreen(dpy);
    4.26 +	root_win = RootWindow(dpy, scr);
    4.27 +	root_width = DisplayWidth(dpy, scr);
    4.28 +	root_height = DisplayHeight(dpy, scr);
    4.29 +	log_info("display size %dx%d\n", root_width, root_height);
    4.30 +
    4.31 +	int xcomp_ev_base, xcomp_err_base, xcomp_ver_major, xcomp_ver_minor;
    4.32 +	if(!XCompositeQueryExtension(dpy, &xcomp_ev_base, &xcomp_err_base)) {
    4.33 +		log_error("X server doesn't support the composite extension\n");
    4.34 +		return 1;
    4.35 +	}
    4.36 +	XCompositeQueryVersion(dpy, &xcomp_ver_major, &xcomp_ver_minor);
    4.37 +	log_info("Found composite extension version %d.%d\n", xcomp_ver_major, xcomp_ver_minor);
    4.38 +	if(xcomp_ver_major <= 10 && xcomp_ver_minor < 2) {
    4.39 +		log_error("I need at least version 1.2\n");	// for NameWindowPixmap
    4.40 +		return 1;
    4.41 +	}
    4.42 +
    4.43 +	int xdmg_ev_base, xdmg_err_base;
    4.44 +	if(!XDamageQueryExtension(dpy, &xdmg_ev_base, &xdmg_err_base)) {
    4.45 +		log_error("X server doesn't support the damage extension\n");
    4.46 +		return 1;
    4.47 +	}
    4.48 +	// TODO also XFixes ?
    4.49 +
    4.50 +	if(!register_compositor()) {
    4.51 +		return 1;
    4.52 +	}
    4.53 +
    4.54 +	XGrabServer(dpy);
    4.55 +	XCompositeRedirectSubwindows(dpy, root_win, CompositeRedirectAutomatic);
    4.56 +	XUngrabServer(dpy);
    4.57 +
    4.58 +	for(;;) {
    4.59 +		XEvent ev;
    4.60 +		XNextEvent(dpy, &ev);
    4.61 +		// TODO
    4.62 +	}
    4.63 +}
    4.64 +
    4.65 +static bool register_compositor()
    4.66 +{
    4.67 +	char atom_name[64];
    4.68 +	sprintf(atom_name, "_NET_WM_CM_S%d", scr);
    4.69 +	Atom xa_wm_cm = XInternAtom(dpy, atom_name, False);
    4.70 +
    4.71 +	Window win = XGetSelectionOwner(dpy, xa_wm_cm);
    4.72 +	if(win != None) {
    4.73 +		log_error("Another compositor is running. Stop it and try again.\n");
    4.74 +		return false;
    4.75 +	}
    4.76 +
    4.77 +	win = XCreateSimpleWindow(dpy, root_win, 0, 0, 1, 1, 0, None, None);
    4.78 +	XSetSelectionOwner(dpy, xa_wm_cm, win, 0);
    4.79 +	return true;
    4.80 +}