xglcomp

changeset 3:e831d38e6faa

textures
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 22 Jan 2016 07:46:47 +0200
parents 876efea9424c
children 57050ca14de6
files src/cwin.cc src/cwin.h src/main.cc
diffstat 3 files changed, 63 insertions(+), 15 deletions(-) [+]
line diff
     1.1 --- a/src/cwin.cc	Fri Jan 22 06:31:03 2016 +0200
     1.2 +++ b/src/cwin.cc	Fri Jan 22 07:46:47 2016 +0200
     1.3 @@ -1,3 +1,4 @@
     1.4 +#include <string.h>
     1.5  #include <vector>
     1.6  #include <algorithm>
     1.7  #include "cwin.h"
     1.8 @@ -9,6 +10,8 @@
     1.9  {
    1.10  	xwin = xid;
    1.11  	xpixmap = 0;
    1.12 +	memset(&attr, 0, sizeof attr);
    1.13 +	mapped = false;
    1.14  }
    1.15  
    1.16  void add_window(CompWindow *cwin)
     2.1 --- a/src/cwin.h	Fri Jan 22 06:31:03 2016 +0200
     2.2 +++ b/src/cwin.h	Fri Jan 22 07:46:47 2016 +0200
     2.3 @@ -2,6 +2,7 @@
     2.4  #define COMP_WIN_H_
     2.5  
     2.6  #include <X11/Xlib.h>
     2.7 +#include "texture.h"
     2.8  
     2.9  class CompWindow {
    2.10  public:
    2.11 @@ -9,6 +10,9 @@
    2.12  	Pixmap xpixmap;
    2.13  
    2.14  	XWindowAttributes attr;
    2.15 +	bool mapped;
    2.16 +
    2.17 +	Texture tex;
    2.18  
    2.19  	CompWindow(Window xid = 0);
    2.20  };
     3.1 --- a/src/main.cc	Fri Jan 22 06:31:03 2016 +0200
     3.2 +++ b/src/main.cc	Fri Jan 22 07:46:47 2016 +0200
     3.3 @@ -15,6 +15,7 @@
     3.4  static void manage_window(Window xwin);
     3.5  static void unmanage_window(Window xwin);
     3.6  static void redraw();
     3.7 +static void draw_window(CompWindow *cwin);
     3.8  static void reshape(int x, int y);
     3.9  
    3.10  Display *dpy;
    3.11 @@ -72,6 +73,7 @@
    3.12  		XEvent ev;
    3.13  		XNextEvent(dpy, &ev);
    3.14  
    3.15 +		CompWindow *cwin;
    3.16  		switch(ev.type) {
    3.17  		case CreateNotify:
    3.18  			manage_window(ev.xcreatewindow.window);
    3.19 @@ -85,8 +87,15 @@
    3.20  			break;
    3.21  
    3.22  		case MapNotify:
    3.23 +			if((cwin = find_window_xid(ev.xmap.window))) {
    3.24 +				cwin->mapped = true;
    3.25 +			}
    3.26  			break;
    3.27 +
    3.28  		case UnmapNotify:
    3.29 +			if((cwin = find_window_xid(ev.xunmap.window))) {
    3.30 +				cwin->mapped = false;
    3.31 +			}
    3.32  			break;
    3.33  
    3.34  		case ReparentNotify:
    3.35 @@ -101,10 +110,7 @@
    3.36  			break;
    3.37  
    3.38  		case Expose:
    3.39 -			if(ev.xexpose.window == root_win) {
    3.40 -				log_debug("expose\n");
    3.41 -				// TODO
    3.42 -			}
    3.43 +			// TODO?
    3.44  			break;
    3.45  
    3.46  		default:
    3.47 @@ -157,6 +163,7 @@
    3.48  	//log_debug("manage_window %u\n", xwin);
    3.49  	CompWindow *cwin = new CompWindow(xwin);
    3.50  	XGetWindowAttributes(dpy, xwin, &cwin->attr);
    3.51 +	cwin->mapped = cwin->attr.map_state != IsUnmapped;
    3.52  	add_window(cwin);
    3.53  }
    3.54  
    3.55 @@ -164,16 +171,22 @@
    3.56  {
    3.57  	//log_debug("unmanage_window %u\n", xwin);
    3.58  	CompWindow *cwin = find_window_xid(xwin);
    3.59 -	if(cwin) {
    3.60 -		remove_window(cwin);
    3.61 -		delete cwin;
    3.62 +	if(!cwin) return;
    3.63 +
    3.64 +	if(cwin->xpixmap) {
    3.65 +		XFreePixmap(dpy, cwin->xpixmap);
    3.66  	}
    3.67 +
    3.68 +	remove_window(cwin);
    3.69 +	delete cwin;
    3.70  }
    3.71  
    3.72  static void redraw()
    3.73  {
    3.74  	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    3.75  
    3.76 +	glPushAttrib(GL_ENABLE_BIT);
    3.77 +	glEnable(GL_TEXTURE_2D);
    3.78  	glEnable(GL_BLEND);
    3.79  	glBlendFunc(GL_ONE, GL_ONE);
    3.80  
    3.81 @@ -181,20 +194,48 @@
    3.82  	for(int i=0; i<num_win; i++) {
    3.83  		CompWindow *cwin = get_window(i);
    3.84  
    3.85 -		glBegin(GL_QUADS);
    3.86 -		glColor3f(0.4, 0.1, 0.05);
    3.87 -		glVertex2f(cwin->attr.x, cwin->attr.y);
    3.88 -		glVertex2f(cwin->attr.x + cwin->attr.width, cwin->attr.y);
    3.89 -		glVertex2f(cwin->attr.x + cwin->attr.width, cwin->attr.y + cwin->attr.height);
    3.90 -		glVertex2f(cwin->attr.x, cwin->attr.y + cwin->attr.height);
    3.91 -		glEnd();
    3.92 +		if(!cwin->mapped) continue;
    3.93 +
    3.94 +		if(cwin->attr.x + cwin->attr.width <= 0 ||
    3.95 +				cwin->attr.y + cwin->attr.height <= 0 ||
    3.96 +				cwin->attr.x >= root_width || cwin->attr.y >= root_height) {
    3.97 +			continue;
    3.98 +		}
    3.99 +
   3.100 +		draw_window(cwin);
   3.101  	}
   3.102  
   3.103 -	glDisable(GL_BLEND);
   3.104 +	glPopAttrib();
   3.105  
   3.106  	glXSwapBuffers(dpy, comp_win);
   3.107  }
   3.108  
   3.109 +static void draw_window(CompWindow *cwin)
   3.110 +{
   3.111 +	if(!cwin->xpixmap) {
   3.112 +		cwin->xpixmap = XCompositeNameWindowPixmap(dpy, cwin->xwin);
   3.113 +		if(!cwin->xpixmap) {
   3.114 +			log_warning("failed to get pixmap\n");
   3.115 +			return;
   3.116 +		}
   3.117 +		cwin->tex.set_image(dpy, cwin->xpixmap);
   3.118 +	}
   3.119 +
   3.120 +	glBindTexture(GL_TEXTURE_2D, cwin->tex.get_id());
   3.121 +
   3.122 +	glBegin(GL_QUADS);
   3.123 +	glColor3f(0.3, 0.3, 0.3);
   3.124 +	glTexCoord2f(0, 0);
   3.125 +	glVertex2f(cwin->attr.x, cwin->attr.y);
   3.126 +	glTexCoord2f(1, 0);
   3.127 +	glVertex2f(cwin->attr.x + cwin->attr.width, cwin->attr.y);
   3.128 +	glTexCoord2f(1, 1);
   3.129 +	glVertex2f(cwin->attr.x + cwin->attr.width, cwin->attr.y + cwin->attr.height);
   3.130 +	glTexCoord2f(0, 1);
   3.131 +	glVertex2f(cwin->attr.x, cwin->attr.y + cwin->attr.height);
   3.132 +	glEnd();
   3.133 +}
   3.134 +
   3.135  static void reshape(int x, int y)
   3.136  {
   3.137  	glViewport(0, 0, x, y);