xgetshape

view src/main.c @ 0:2f02f100b20f

getting the window shape of another window
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 03 Nov 2015 00:42:08 +0200
parents
children 9b560415bad4
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <X11/Xlib.h>
4 #include <X11/extensions/shape.h>
5 #include <GL/glx.h>
6 #include "image.h"
7 #include "texture.h"
9 void redraw(void);
10 void reshape(int x, int y);
11 int handle_event(XEvent *ev);
13 /* X helper functions */
14 Window create_window(int xsz, int ysz);
15 void set_window_title(Window win, const char *title);
16 int get_window_shape(Window win, struct image *img);
18 Display *dpy;
19 Window win;
20 GLXContext ctx;
21 int width, height;
22 int mapped;
23 int redisp_pending;
24 Atom xa_wm_prot, xa_wm_del_win;
25 struct image img;
26 struct texture tex;
27 struct texture chess_tex;
28 float aspect;
30 unsigned int target_xid = 0;
32 int main(int argc, char **argv)
33 {
34 int event_base, error_base;
35 char *endp;
36 struct image chess_img;
38 if(!argv[1]) {
39 fprintf(stderr, "pass the window id to use\n");
40 return 1;
41 }
42 if(!(target_xid = strtol(argv[1], &endp, 0))) {
43 fprintf(stderr, "invalid argument: %s\n", argv[1]);
44 return 1;
45 }
47 if(!(dpy = XOpenDisplay(0))) {
48 fprintf(stderr, "failed to open display\n");
49 return 1;
50 }
51 xa_wm_prot = XInternAtom(dpy, "WM_PROTOCOLS", False);
52 xa_wm_del_win = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
54 if(!XShapeQueryExtension(dpy, &event_base, &error_base)) {
55 fprintf(stderr, "shape extension unsupported by this X server\n");
56 XCloseDisplay(dpy);
57 return 1;
58 }
59 if(get_window_shape(target_xid, &img) == -1) {
60 XCloseDisplay(dpy);
61 return 1;
62 }
64 if(!(win = create_window(1280, 800))) {
65 return 1;
66 }
68 image_texture(&tex, &img);
70 image_create(&chess_img, 256, 256);
71 image_chess(&chess_img, 8, 255, 128, 64, 64, 128, 255);
72 image_texture(&chess_tex, &chess_img);
73 image_destroy(&chess_img);
75 for(;;) {
76 XEvent ev;
77 XNextEvent(dpy, &ev);
79 if(handle_event(&ev) == -1) {
80 break;
81 }
82 }
84 XDestroyWindow(dpy, win);
85 XCloseDisplay(dpy);
86 return 0;
87 }
89 void redraw(void)
90 {
91 glClearColor(0.4, 0.4, 0.4, 1);
92 glClear(GL_COLOR_BUFFER_BIT);
94 glMatrixMode(GL_MODELVIEW);
95 glLoadIdentity();
97 glEnable(GL_TEXTURE_2D);
98 glBindTexture(GL_TEXTURE_2D, chess_tex.id);
101 /* background */
102 glBegin(GL_QUADS);
103 glTexCoord2f(0, 0); glVertex2f(-aspect, -1);
104 glTexCoord2f(1, 0); glVertex2f(aspect, -1);
105 glTexCoord2f(1, 1); glVertex2f(aspect, 1);
106 glTexCoord2f(0, 1); glVertex2f(-aspect, 1);
107 glEnd();
109 /* draw the window shape */
110 glEnable(GL_BLEND);
111 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
113 glBindTexture(GL_TEXTURE_2D, tex.id);
114 glMatrixMode(GL_TEXTURE);
115 glLoadIdentity();
116 glScalef((float)tex.width / (float)tex.tex_width,
117 (float)tex.height / (float)tex.tex_height,
118 1.0);
120 glMatrixMode(GL_MODELVIEW);
121 glPushMatrix();
122 glScalef(0.7, 0.7, 0.7);
124 /* shadow */
125 glPushMatrix();
126 glTranslatef(0.1, -0.1, 0);
127 glBegin(GL_QUADS);
128 glColor3f(0, 0, 0);
129 glTexCoord2f(0, 1); glVertex2f(-1, -1);
130 glTexCoord2f(1, 1); glVertex2f(1, -1);
131 glTexCoord2f(1, 0); glVertex2f(1, 1);
132 glTexCoord2f(0, 0); glVertex2f(-1, 1);
133 glEnd();
134 glPopMatrix();
136 /* window */
137 glBegin(GL_QUADS);
138 glColor3f(1, 1, 0);
139 glTexCoord2f(0, 1); glVertex2f(-1, -1);
140 glTexCoord2f(1, 1); glVertex2f(1, -1);
141 glTexCoord2f(1, 0); glVertex2f(1, 1);
142 glTexCoord2f(0, 0); glVertex2f(-1, 1);
143 glEnd();
145 glMatrixMode(GL_TEXTURE);
146 glLoadIdentity();
147 glMatrixMode(GL_MODELVIEW);
148 glPopMatrix();
150 glDisable(GL_BLEND);
152 glXSwapBuffers(dpy, win);
153 }
155 void reshape(int x, int y)
156 {
157 aspect = (float)x / (float)y;
158 glViewport(0, 0, x, y);
160 glMatrixMode(GL_PROJECTION);
161 glLoadIdentity();
162 glScalef(1.0 / aspect, 1.0, 1.0);
163 }
165 int handle_event(XEvent *ev)
166 {
167 switch(ev->type) {
168 case MapNotify:
169 case UnmapNotify:
170 mapped = ev->type == MapNotify ? 1 : 0;
171 break;
173 case Expose:
174 if(mapped && ev->xexpose.count == 0) {
175 redraw();
176 }
177 break;
179 case KeyPress:
180 {
181 KeySym sym = XLookupKeysym(&ev->xkey, 0);
183 switch(sym) {
184 case XK_Escape:
185 return -1;
186 }
187 }
188 break;
190 case ConfigureNotify:
191 if(ev->xconfigure.width != width || ev->xconfigure.height != height) {
192 width = ev->xconfigure.width;
193 height = ev->xconfigure.height;
194 reshape(width, height);
195 }
196 break;
198 case ClientMessage:
199 if(ev->xclient.message_type == xa_wm_prot) {
200 if(ev->xclient.data.l[0] == xa_wm_del_win) {
201 return -1;
202 }
203 }
204 break;
205 }
206 return 0;
207 }
209 Window create_window(int xsz, int ysz)
210 {
211 Window w, root;
212 XVisualInfo *vis;
213 XClassHint chint;
214 XSetWindowAttributes xattr;
215 unsigned int evmask, xattr_mask;
216 int scr;
217 int glxattr[] = {
218 GLX_RGBA, GLX_DOUBLEBUFFER,
219 GLX_RED_SIZE, 8,
220 GLX_GREEN_SIZE, 8,
221 GLX_BLUE_SIZE, 8,
222 GLX_DEPTH_SIZE, 24,
223 GLX_USE_GL, 1,
224 None
225 };
227 scr = DefaultScreen(dpy);
228 root = RootWindow(dpy, scr);
230 if(!(vis = glXChooseVisual(dpy, scr, glxattr))) {
231 printf("failed to find a suitable visual\n");
232 return 0;
233 }
234 if(!(ctx = glXCreateContext(dpy, vis, 0, True))) {
235 XFree(vis);
236 return 0;
237 }
239 xattr.background_pixel = xattr.border_pixel = BlackPixel(dpy, scr);
240 xattr.colormap = XCreateColormap(dpy, root, vis->visual, AllocNone);
241 xattr_mask = CWColormap | CWBackPixel | CWBorderPixel;
243 if(!(w = XCreateWindow(dpy, root, 0, 0, xsz, ysz, 0, vis->depth, InputOutput,
244 vis->visual, xattr_mask, &xattr))) {
245 printf("failed to create window\n");
246 glXDestroyContext(dpy, ctx);
247 XFree(vis);
248 return 0;
249 }
250 XFree(vis);
252 evmask = StructureNotifyMask | VisibilityChangeMask | KeyPressMask |
253 ExposureMask;
254 XSelectInput(dpy, w, evmask);
256 XSetWMProtocols(dpy, w, &xa_wm_del_win, 1);
258 chint.res_name = chint.res_class = "xgetshape";
259 XSetClassHint(dpy, w, &chint);
261 set_window_title(w, "GL xgetshape");
263 glXMakeCurrent(dpy, w, ctx);
264 XMapWindow(dpy, w);
265 return w;
266 }
268 void set_window_title(Window win, const char *title)
269 {
270 XTextProperty wm_name;
271 XStringListToTextProperty((char**)&title, 1, &wm_name);
272 XSetWMName(dpy, win, &wm_name);
273 XSetWMIconName(dpy, win, &wm_name);
274 XFree(wm_name.value);
275 }
277 int get_window_shape(Window win, struct image *img)
278 {
279 Bool buse, cuse;
280 int bx, by, cx, cy;
281 unsigned int bw, bh, cw, ch;
282 int kind;
283 int x, y, w, h;
284 XRectangle *rects;
285 int i, rect_count, rect_order;
287 XShapeQueryExtents(dpy, win, &buse, &bx, &by, &bw, &bh,
288 &cuse, &cx, &cy, &bw, &bh);
289 if(cuse) {
290 x = cx;
291 y = cy;
292 w = cw;
293 h = ch;
294 kind = ShapeClip;
295 } else if(buse) {
296 x = bx;
297 y = by;
298 w = bw;
299 h = bh;
300 kind = ShapeBounding;
301 } else {
302 fprintf(stderr, "XShapeQueryExtents returned no extents\n");
303 return -1;
304 }
306 if(image_create(img, w, h) == -1) {
307 fprintf(stderr, "failed to create shape image (%dx%d)\n", w, h);
308 return -1;
309 }
310 image_clear(img, 0, 0, 0, 0);
312 if(!(rects = XShapeGetRectangles(dpy, win, kind, &rect_count, &rect_order))) {
313 fprintf(stderr, "failed to get the shape rectangles\n");
314 image_destroy(img);
315 return -1;
316 }
318 for(i=0; i<rect_count; i++) {
319 image_fillrect(img, rects[i].x, rects[i].y, rects[i].width, rects[i].height,
320 255, 255, 255, 255);
321 }
322 return 0;
323 }