xglcomp

view src/cwin.cc @ 11:cb636a23f4f2

lifted X error reporting from Xlib
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 04 Feb 2016 04:15:15 +0200
parents 03ca0fd49916
children 1c0d056ec360
line source
1 #include <string.h>
2 #include <vector>
3 #include <algorithm>
4 #include <X11/Xlib.h>
5 #include <X11/extensions/Xdamage.h>
6 #include "cwin.h"
7 #include "logger.h"
9 extern Display *dpy;
11 static std::vector<CompWindow*> cwinlist;
13 CompWindow::CompWindow(Window xid)
14 {
15 xwin = xid;
16 xpixmap = 0;
17 memset(&attr, 0, sizeof attr);
18 mapped = false;
19 damage = 0;
20 }
22 CompWindow::~CompWindow()
23 {
24 if(xpixmap) {
25 XFreePixmap(dpy, xpixmap);
26 }
27 if(damage) {
28 XDamageDestroy(dpy, damage);
29 }
30 }
32 void add_window(CompWindow *cwin)
33 {
34 if(have_window(cwin)) {
35 log_warning("add_window trying to add duplicate, ignoring\n");
36 return;
37 }
39 XGetWindowAttributes(dpy, cwin->xwin, &cwin->attr);
41 // create the damage structure to track dirty regions in this window
42 if(!cwin->damage && cwin->attr.c_class != InputOnly) {
43 cwin->damage = XDamageCreate(dpy, cwin->xwin, XDamageReportNonEmpty);
44 }
46 cwinlist.push_back(cwin);
47 }
49 bool remove_window(CompWindow *cwin)
50 {
51 auto it = std::find(cwinlist.begin(), cwinlist.end(), cwin);
52 if(it != cwinlist.end()) {
53 cwinlist.erase(it);
54 return true;
55 }
56 return false;
57 }
59 bool have_window(CompWindow *cwin)
60 {
61 return std::find(cwinlist.begin(), cwinlist.end(), cwin) != cwinlist.end();
62 }
64 CompWindow *find_window_xid(Window xid)
65 {
66 if(!xid) return 0;
68 for(size_t i=0; i<cwinlist.size(); i++) {
69 if(cwinlist[i]->xwin == xid) {
70 return cwinlist[i];
71 }
72 }
73 return 0;
74 }
76 CompWindow *find_window_pixmap(Pixmap pix)
77 {
78 if(!pix) return 0;
80 for(size_t i=0; i<cwinlist.size(); i++) {
81 if(cwinlist[i]->xpixmap == pix) {
82 return cwinlist[i];
83 }
84 }
85 return 0;
86 }
88 int get_window_count()
89 {
90 return (int)cwinlist.size();
91 }
93 CompWindow *get_window(int idx)
94 {
95 if(idx < 0 || idx >= (int)cwinlist.size()) {
96 return 0;
97 }
98 return cwinlist[idx];
99 }