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@5
|
9 #include <X11/extensions/shape.h>
|
nuclear@0
|
10 #include "cwin.h"
|
nuclear@2
|
11 #include "opengl.h"
|
nuclear@1
|
12 #include "logger.h"
|
nuclear@1
|
13
|
nuclear@5
|
14 static bool query_extensions();
|
nuclear@1
|
15 static bool register_compositor();
|
nuclear@2
|
16 static void start_comp();
|
nuclear@2
|
17 static void manage_window(Window xwin);
|
nuclear@2
|
18 static void unmanage_window(Window xwin);
|
nuclear@2
|
19 static void redraw();
|
nuclear@3
|
20 static void draw_window(CompWindow *cwin);
|
nuclear@2
|
21 static void reshape(int x, int y);
|
nuclear@1
|
22
|
nuclear@2
|
23 Display *dpy;
|
nuclear@2
|
24 int screen_num;
|
nuclear@2
|
25 Window root_win, comp_win;
|
nuclear@2
|
26 int root_width, root_height;
|
nuclear@1
|
27
|
nuclear@5
|
28 static int xdmg_ev_base, xdmg_err_base;
|
nuclear@5
|
29 static int xshape_ev_base, xshape_err_base;
|
nuclear@5
|
30
|
nuclear@1
|
31 int main(int argc, char **argv)
|
nuclear@1
|
32 {
|
nuclear@1
|
33 if(!(dpy = XOpenDisplay(0))) {
|
nuclear@1
|
34 log_error("failed to open X display\n");
|
nuclear@1
|
35 return 1;
|
nuclear@1
|
36 }
|
nuclear@2
|
37 screen_num = DefaultScreen(dpy);
|
nuclear@2
|
38 root_win = RootWindow(dpy, screen_num);
|
nuclear@2
|
39 root_width = DisplayWidth(dpy, screen_num);
|
nuclear@2
|
40 root_height = DisplayHeight(dpy, screen_num);
|
nuclear@1
|
41 log_info("display size %dx%d\n", root_width, root_height);
|
nuclear@1
|
42
|
nuclear@5
|
43 if(!query_extensions()) {
|
nuclear@1
|
44 return 1;
|
nuclear@1
|
45 }
|
nuclear@1
|
46
|
nuclear@1
|
47 if(!register_compositor()) {
|
nuclear@1
|
48 return 1;
|
nuclear@1
|
49 }
|
nuclear@1
|
50
|
nuclear@2
|
51 comp_win = XCompositeGetOverlayWindow(dpy, root_win);
|
nuclear@2
|
52 if(!create_gl(comp_win)) {
|
nuclear@2
|
53 return 1;
|
nuclear@2
|
54 }
|
nuclear@2
|
55
|
nuclear@2
|
56 start_comp();
|
nuclear@2
|
57 reshape(root_width, root_height);
|
nuclear@2
|
58 redraw();
|
nuclear@1
|
59
|
nuclear@1
|
60 for(;;) {
|
nuclear@1
|
61 XEvent ev;
|
nuclear@1
|
62 XNextEvent(dpy, &ev);
|
nuclear@2
|
63
|
nuclear@3
|
64 CompWindow *cwin;
|
nuclear@2
|
65 switch(ev.type) {
|
nuclear@2
|
66 case CreateNotify:
|
nuclear@2
|
67 manage_window(ev.xcreatewindow.window);
|
nuclear@2
|
68 break;
|
nuclear@2
|
69
|
nuclear@2
|
70 case ConfigureNotify:
|
nuclear@5
|
71 // XXX is this right?
|
nuclear@5
|
72 if((cwin = find_window_xid(ev.xconfigure.window))) {
|
nuclear@5
|
73 log_debug("updating window attributes\n");
|
nuclear@5
|
74 XGetWindowAttributes(dpy, cwin->xwin, &cwin->attr);
|
nuclear@5
|
75 }
|
nuclear@2
|
76 break;
|
nuclear@2
|
77
|
nuclear@2
|
78 case DestroyNotify:
|
nuclear@2
|
79 unmanage_window(ev.xdestroywindow.window);
|
nuclear@2
|
80 break;
|
nuclear@2
|
81
|
nuclear@2
|
82 case MapNotify:
|
nuclear@3
|
83 if((cwin = find_window_xid(ev.xmap.window))) {
|
nuclear@3
|
84 cwin->mapped = true;
|
nuclear@3
|
85 }
|
nuclear@2
|
86 break;
|
nuclear@3
|
87
|
nuclear@2
|
88 case UnmapNotify:
|
nuclear@3
|
89 if((cwin = find_window_xid(ev.xunmap.window))) {
|
nuclear@3
|
90 cwin->mapped = false;
|
nuclear@3
|
91 }
|
nuclear@2
|
92 break;
|
nuclear@2
|
93
|
nuclear@2
|
94 case ReparentNotify:
|
nuclear@2
|
95 if(ev.xreparent.parent == root_win) {
|
nuclear@2
|
96 manage_window(ev.xreparent.window);
|
nuclear@2
|
97 } else {
|
nuclear@2
|
98 unmanage_window(ev.xreparent.window);
|
nuclear@2
|
99 }
|
nuclear@2
|
100 break;
|
nuclear@2
|
101
|
nuclear@2
|
102 case CirculateNotify:
|
nuclear@2
|
103 break;
|
nuclear@2
|
104
|
nuclear@2
|
105 case Expose:
|
nuclear@3
|
106 // TODO?
|
nuclear@2
|
107 break;
|
nuclear@2
|
108
|
nuclear@2
|
109 default:
|
nuclear@2
|
110 if(ev.type == xdmg_ev_base + XDamageNotify) {
|
nuclear@2
|
111 }
|
nuclear@2
|
112 }
|
nuclear@5
|
113
|
nuclear@5
|
114 redraw();
|
nuclear@1
|
115 }
|
nuclear@1
|
116 }
|
nuclear@1
|
117
|
nuclear@5
|
118 static bool query_extensions()
|
nuclear@5
|
119 {
|
nuclear@5
|
120 int xcomp_ev_base, xcomp_err_base;
|
nuclear@5
|
121 if(!XCompositeQueryExtension(dpy, &xcomp_ev_base, &xcomp_err_base)) {
|
nuclear@5
|
122 log_error("X server doesn't support the composite extension\n");
|
nuclear@5
|
123 return false;
|
nuclear@5
|
124 }
|
nuclear@5
|
125 int xcomp_ver_major, xcomp_ver_minor;
|
nuclear@5
|
126 XCompositeQueryVersion(dpy, &xcomp_ver_major, &xcomp_ver_minor);
|
nuclear@5
|
127 log_info("Found composite extension version %d.%d\n", xcomp_ver_major, xcomp_ver_minor);
|
nuclear@5
|
128 if(xcomp_ver_major <= 0 && xcomp_ver_minor < 3) {
|
nuclear@5
|
129 // for NameWindowPixmap & CompositeGetoverlayWindow
|
nuclear@5
|
130 log_error("I need at least version 0.3\n");
|
nuclear@5
|
131 return false;
|
nuclear@5
|
132 }
|
nuclear@5
|
133
|
nuclear@5
|
134 if(!XDamageQueryExtension(dpy, &xdmg_ev_base, &xdmg_err_base)) {
|
nuclear@5
|
135 log_error("X server doesn't support the damage extension\n");
|
nuclear@5
|
136 return false;
|
nuclear@5
|
137 }
|
nuclear@5
|
138
|
nuclear@5
|
139 if(!XShapeQueryExtension(dpy, &xshape_ev_base, &xshape_err_base)) {
|
nuclear@5
|
140 log_error("X server doesn't support the shape extension\n");
|
nuclear@5
|
141 return false;
|
nuclear@5
|
142 }
|
nuclear@5
|
143
|
nuclear@5
|
144 int xfix_ev_base, xfix_err_base;
|
nuclear@5
|
145 if(!XFixesQueryExtension(dpy, &xfix_ev_base, &xfix_err_base)) {
|
nuclear@5
|
146 log_error("X server doesn't support the Xfixes extension\n");
|
nuclear@5
|
147 return false;
|
nuclear@5
|
148 }
|
nuclear@5
|
149
|
nuclear@5
|
150 int xfix_ver_major, xfix_ver_minor;
|
nuclear@5
|
151 XFixesQueryVersion(dpy, &xfix_ver_major, &xfix_ver_minor);
|
nuclear@5
|
152 log_info("Found xfixes version %d.%d\n", xfix_ver_major, xfix_ver_minor);
|
nuclear@5
|
153 if(xfix_ver_major < 2) {
|
nuclear@5
|
154 // for SetWindowShapeRegion
|
nuclear@5
|
155 log_error("I need at least version 2.0\n");
|
nuclear@5
|
156 return false;
|
nuclear@5
|
157 }
|
nuclear@5
|
158 return true;
|
nuclear@5
|
159 }
|
nuclear@5
|
160
|
nuclear@1
|
161 static bool register_compositor()
|
nuclear@1
|
162 {
|
nuclear@1
|
163 char atom_name[64];
|
nuclear@2
|
164 sprintf(atom_name, "_NET_WM_CM_S%d", screen_num);
|
nuclear@1
|
165 Atom xa_wm_cm = XInternAtom(dpy, atom_name, False);
|
nuclear@1
|
166
|
nuclear@1
|
167 Window win = XGetSelectionOwner(dpy, xa_wm_cm);
|
nuclear@1
|
168 if(win != None) {
|
nuclear@1
|
169 log_error("Another compositor is running. Stop it and try again.\n");
|
nuclear@1
|
170 return false;
|
nuclear@1
|
171 }
|
nuclear@1
|
172
|
nuclear@1
|
173 win = XCreateSimpleWindow(dpy, root_win, 0, 0, 1, 1, 0, None, None);
|
nuclear@1
|
174 XSetSelectionOwner(dpy, xa_wm_cm, win, 0);
|
nuclear@1
|
175 return true;
|
nuclear@1
|
176 }
|
nuclear@2
|
177
|
nuclear@2
|
178 static void start_comp()
|
nuclear@2
|
179 {
|
nuclear@2
|
180 XGrabServer(dpy);
|
nuclear@2
|
181 XCompositeRedirectSubwindows(dpy, root_win, CompositeRedirectManual);
|
nuclear@2
|
182 XSelectInput(dpy, root_win, SubstructureNotifyMask | ExposureMask | StructureNotifyMask);
|
nuclear@2
|
183
|
nuclear@2
|
184 // manage all top-level windows
|
nuclear@2
|
185 Window root_ret, parent;
|
nuclear@2
|
186 Window *children;
|
nuclear@2
|
187 unsigned int num_children;
|
nuclear@2
|
188 XQueryTree(dpy, root_win, &root_ret, &parent, &children, &num_children);
|
nuclear@2
|
189
|
nuclear@2
|
190 for(unsigned int i=0; i<num_children; i++) {
|
nuclear@2
|
191 manage_window(children[i]);
|
nuclear@2
|
192 }
|
nuclear@2
|
193 log_info("starting compositor, managing %u top-level windows\n", num_children);
|
nuclear@2
|
194 XFree(children);
|
nuclear@5
|
195 XUngrabServer(dpy);
|
nuclear@2
|
196
|
nuclear@5
|
197 /* set a zero-area input-shape for the overlay window to let events
|
nuclear@5
|
198 * through to the windows under it
|
nuclear@5
|
199 */
|
nuclear@5
|
200 XserverRegion region = XFixesCreateRegion(dpy, 0, 0);
|
nuclear@5
|
201 XFixesSetWindowShapeRegion(dpy, comp_win, ShapeInput, 0, 0, region);
|
nuclear@5
|
202 XFixesDestroyRegion(dpy, region);
|
nuclear@2
|
203 }
|
nuclear@2
|
204
|
nuclear@2
|
205 static void manage_window(Window xwin)
|
nuclear@2
|
206 {
|
nuclear@2
|
207 //log_debug("manage_window %u\n", xwin);
|
nuclear@2
|
208 CompWindow *cwin = new CompWindow(xwin);
|
nuclear@2
|
209 XGetWindowAttributes(dpy, xwin, &cwin->attr);
|
nuclear@3
|
210 cwin->mapped = cwin->attr.map_state != IsUnmapped;
|
nuclear@2
|
211 add_window(cwin);
|
nuclear@2
|
212 }
|
nuclear@2
|
213
|
nuclear@2
|
214 static void unmanage_window(Window xwin)
|
nuclear@2
|
215 {
|
nuclear@2
|
216 //log_debug("unmanage_window %u\n", xwin);
|
nuclear@2
|
217 CompWindow *cwin = find_window_xid(xwin);
|
nuclear@3
|
218 if(!cwin) return;
|
nuclear@3
|
219
|
nuclear@3
|
220 if(cwin->xpixmap) {
|
nuclear@3
|
221 XFreePixmap(dpy, cwin->xpixmap);
|
nuclear@2
|
222 }
|
nuclear@3
|
223
|
nuclear@3
|
224 remove_window(cwin);
|
nuclear@3
|
225 delete cwin;
|
nuclear@2
|
226 }
|
nuclear@2
|
227
|
nuclear@2
|
228 static void redraw()
|
nuclear@2
|
229 {
|
nuclear@2
|
230 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@2
|
231
|
nuclear@3
|
232 glPushAttrib(GL_ENABLE_BIT);
|
nuclear@3
|
233 glEnable(GL_TEXTURE_2D);
|
nuclear@2
|
234 glEnable(GL_BLEND);
|
nuclear@2
|
235 glBlendFunc(GL_ONE, GL_ONE);
|
nuclear@2
|
236
|
nuclear@2
|
237 int num_win = get_window_count();
|
nuclear@2
|
238 for(int i=0; i<num_win; i++) {
|
nuclear@2
|
239 CompWindow *cwin = get_window(i);
|
nuclear@2
|
240
|
nuclear@3
|
241 if(!cwin->mapped) continue;
|
nuclear@3
|
242
|
nuclear@3
|
243 if(cwin->attr.x + cwin->attr.width <= 0 ||
|
nuclear@3
|
244 cwin->attr.y + cwin->attr.height <= 0 ||
|
nuclear@3
|
245 cwin->attr.x >= root_width || cwin->attr.y >= root_height) {
|
nuclear@3
|
246 continue;
|
nuclear@3
|
247 }
|
nuclear@3
|
248
|
nuclear@3
|
249 draw_window(cwin);
|
nuclear@2
|
250 }
|
nuclear@2
|
251
|
nuclear@3
|
252 glPopAttrib();
|
nuclear@2
|
253
|
nuclear@2
|
254 glXSwapBuffers(dpy, comp_win);
|
nuclear@2
|
255 }
|
nuclear@2
|
256
|
nuclear@3
|
257 static void draw_window(CompWindow *cwin)
|
nuclear@3
|
258 {
|
nuclear@3
|
259 if(!cwin->xpixmap) {
|
nuclear@3
|
260 cwin->xpixmap = XCompositeNameWindowPixmap(dpy, cwin->xwin);
|
nuclear@3
|
261 if(!cwin->xpixmap) {
|
nuclear@3
|
262 log_warning("failed to get pixmap\n");
|
nuclear@3
|
263 return;
|
nuclear@3
|
264 }
|
nuclear@3
|
265 cwin->tex.set_image(dpy, cwin->xpixmap);
|
nuclear@3
|
266 }
|
nuclear@3
|
267
|
nuclear@3
|
268 glBindTexture(GL_TEXTURE_2D, cwin->tex.get_id());
|
nuclear@3
|
269
|
nuclear@3
|
270 glBegin(GL_QUADS);
|
nuclear@3
|
271 glColor3f(0.3, 0.3, 0.3);
|
nuclear@3
|
272 glTexCoord2f(0, 0);
|
nuclear@3
|
273 glVertex2f(cwin->attr.x, cwin->attr.y);
|
nuclear@3
|
274 glTexCoord2f(1, 0);
|
nuclear@3
|
275 glVertex2f(cwin->attr.x + cwin->attr.width, cwin->attr.y);
|
nuclear@3
|
276 glTexCoord2f(1, 1);
|
nuclear@3
|
277 glVertex2f(cwin->attr.x + cwin->attr.width, cwin->attr.y + cwin->attr.height);
|
nuclear@3
|
278 glTexCoord2f(0, 1);
|
nuclear@3
|
279 glVertex2f(cwin->attr.x, cwin->attr.y + cwin->attr.height);
|
nuclear@3
|
280 glEnd();
|
nuclear@3
|
281 }
|
nuclear@3
|
282
|
nuclear@2
|
283 static void reshape(int x, int y)
|
nuclear@2
|
284 {
|
nuclear@2
|
285 glViewport(0, 0, x, y);
|
nuclear@2
|
286
|
nuclear@2
|
287 glMatrixMode(GL_PROJECTION);
|
nuclear@2
|
288 glLoadIdentity();
|
nuclear@2
|
289 glOrtho(0, x, y, 0, -1, 1);
|
nuclear@2
|
290 }
|