xglcomp

view src/cwin.cc @ 12:1c0d056ec360

moving slowly
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 05 Feb 2016 03:33:18 +0200
parents b0081a0c211f
children
line source
1 #include <string.h>
2 #include <vector>
3 #include <algorithm>
4 #include <X11/Xlib.h>
5 #include <X11/extensions/Xcomposite.h>
6 #include <X11/extensions/Xdamage.h>
7 #include "cwin.h"
8 #include "logger.h"
10 extern Display *dpy;
12 static std::vector<CompWindow*> cwinlist;
14 CompWindow::CompWindow(Window xid)
15 {
16 xwin = xid;
17 xpixmap = 0;
18 pixmap_valid = false;
19 memset(&attr, 0, sizeof attr);
20 mapped = false;
21 damage = 0;
22 damaged = false;
23 tex_valid = false;
24 }
26 CompWindow::~CompWindow()
27 {
28 if(xpixmap) {
29 XFreePixmap(dpy, xpixmap);
30 }
31 if(damage) {
32 XDamageDestroy(dpy, damage);
33 }
34 }
36 bool CompWindow::update_attr()
37 {
38 if(!xwin) return false;
40 XGetWindowAttributes(dpy, xwin, &attr);
41 return true;
42 }
44 bool CompWindow::update_pixmap()
45 {
46 if(!xwin) return false;
48 Pixmap pix = XCompositeNameWindowPixmap(dpy, xwin);
49 if(!pix) {
50 log_error("failed to get window %x pixmap\n", xwin);
51 return false;
52 }
54 if(pix != xpixmap) {
55 if(xpixmap) {
56 XFreePixmap(dpy, xpixmap);
57 }
58 xpixmap = pix;
59 tex_valid = false;
60 }
61 pixmap_valid = true;
62 return true;
63 }
65 bool CompWindow::update_texture()
66 {
67 if(!xwin || !xpixmap) return false;
68 tex.set_image(dpy, xpixmap);
69 tex_valid = true;
70 return true;
71 }
73 void add_window(CompWindow *cwin)
74 {
75 if(have_window(cwin)) {
76 log_warning("add_window trying to add duplicate, ignoring\n");
77 return;
78 }
80 XGetWindowAttributes(dpy, cwin->xwin, &cwin->attr);
82 // create the damage structure to track dirty regions in this window
83 if(!cwin->damage && cwin->attr.c_class != InputOnly) {
84 cwin->damage = XDamageCreate(dpy, cwin->xwin, XDamageReportNonEmpty);
85 }
87 cwinlist.push_back(cwin);
88 }
90 bool remove_window(CompWindow *cwin)
91 {
92 auto it = std::find(cwinlist.begin(), cwinlist.end(), cwin);
93 if(it != cwinlist.end()) {
94 cwinlist.erase(it);
95 return true;
96 }
97 return false;
98 }
100 bool have_window(CompWindow *cwin)
101 {
102 return std::find(cwinlist.begin(), cwinlist.end(), cwin) != cwinlist.end();
103 }
105 CompWindow *find_window_xid(Window xid)
106 {
107 if(!xid) return 0;
109 for(size_t i=0; i<cwinlist.size(); i++) {
110 if(cwinlist[i]->xwin == xid) {
111 return cwinlist[i];
112 }
113 }
114 return 0;
115 }
117 CompWindow *find_window_pixmap(Pixmap pix)
118 {
119 if(!pix) return 0;
121 for(size_t i=0; i<cwinlist.size(); i++) {
122 if(cwinlist[i]->xpixmap == pix) {
123 return cwinlist[i];
124 }
125 }
126 return 0;
127 }
129 int get_window_count()
130 {
131 return (int)cwinlist.size();
132 }
134 CompWindow *get_window(int idx)
135 {
136 if(idx < 0 || idx >= (int)cwinlist.size()) {
137 return 0;
138 }
139 return cwinlist[idx];
140 }