| 1 | #ifndef VX11_COMPOSITE_BRIDGE_H |
| 2 | #define VX11_COMPOSITE_BRIDGE_H |
| 3 | #include <X11/Xlib.h> |
| 4 | #include <X11/extensions/Xcomposite.h> |
| 5 | #include <X11/extensions/Xrender.h> |
| 6 | #include <stdlib.h> |
| 7 | |
| 8 | /* Traps are scoped to this single-threaded client's own requests. */ |
| 9 | static int vx11_comp_error; |
| 10 | static int vx11_comp_trap(Display *d, XErrorEvent *e) { (void)d; (void)e; vx11_comp_error = 1; return 0; } |
| 11 | typedef struct Vx11CompositePreview { |
| 12 | Window window; |
| 13 | Pixmap thumbnail; |
| 14 | int width, height, valid, redirected; |
| 15 | } Vx11CompositePreview; |
| 16 | |
| 17 | static int vx11_comp_extensions(Display *d) { |
| 18 | int a, b, major = 0, minor = 2; |
| 19 | return XCompositeQueryExtension(d, &a, &b) && |
| 20 | XCompositeQueryVersion(d, &major, &minor) && (major > 0 || minor >= 2) && |
| 21 | XRenderQueryExtension(d, &a, &b); |
| 22 | } |
| 23 | |
| 24 | static Vx11CompositePreview *vx11_comp_preview_new(Display *d, Window window) { |
| 25 | Vx11CompositePreview *p = calloc(1, sizeof(*p)); |
| 26 | if (!p) return NULL; |
| 27 | p->window = window; |
| 28 | XSync(d, False); |
| 29 | XErrorHandler old = XSetErrorHandler(vx11_comp_trap); vx11_comp_error = 0; |
| 30 | XCompositeRedirectWindow(d, window, CompositeRedirectAutomatic); |
| 31 | XSync(d, False); p->redirected = !vx11_comp_error; XSetErrorHandler(old); |
| 32 | return p; |
| 33 | } |
| 34 | |
| 35 | static void vx11_comp_preview_free(Display *d, Vx11CompositePreview *p) { |
| 36 | if (!p) return; |
| 37 | XSync(d, False); |
| 38 | XErrorHandler old = XSetErrorHandler(vx11_comp_trap); vx11_comp_error = 0; |
| 39 | if (p->thumbnail) XFreePixmap(d, p->thumbnail); |
| 40 | if (p->redirected) XCompositeUnredirectWindow(d, p->window, CompositeRedirectAutomatic); |
| 41 | XSync(d, False); XSetErrorHandler(old); free(p); |
| 42 | } |
| 43 | |
| 44 | /* Preserve the last good image when the source is unmapped or vanishes. */ |
| 45 | static int vx11_comp_preview_update(Display *d, Vx11CompositePreview *p, int w, int h) { |
| 46 | if (!p || w < 1 || h < 1) return 0; |
| 47 | XSync(d, False); |
| 48 | XErrorHandler old = XSetErrorHandler(vx11_comp_trap); vx11_comp_error = 0; |
| 49 | XWindowAttributes a; |
| 50 | Pixmap source = 0, target = 0; |
| 51 | Picture src = 0, dst = 0; |
| 52 | if (!XGetWindowAttributes(d, p->window, &a) || a.map_state != IsViewable || a.width < 1 || a.height < 1) goto done; |
| 53 | source = XCompositeNameWindowPixmap(d, p->window); |
| 54 | XSync(d, False); |
| 55 | if (vx11_comp_error || !source) { source = 0; goto done; } |
| 56 | XRenderPictFormat *sf = XRenderFindVisualFormat(d, a.visual); |
| 57 | XRenderPictFormat *df = XRenderFindVisualFormat(d, DefaultVisual(d, DefaultScreen(d))); |
| 58 | if (!sf || !df) goto done; |
| 59 | target = XCreatePixmap(d, DefaultRootWindow(d), w, h, DefaultDepth(d, DefaultScreen(d))); |
| 60 | src = XRenderCreatePicture(d, source, sf, 0, NULL); |
| 61 | dst = XRenderCreatePicture(d, target, df, 0, NULL); |
| 62 | XTransform transform = {{{XDoubleToFixed((double)a.width/w), 0, 0}, |
| 63 | {0, XDoubleToFixed((double)a.height/h), 0}, {0, 0, XDoubleToFixed(1)}}}; |
| 64 | XRenderSetPictureTransform(d, src, &transform); |
| 65 | XRenderSetPictureFilter(d, src, FilterBilinear, NULL, 0); |
| 66 | XRenderComposite(d, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
| 67 | XSync(d, False); |
| 68 | if (!vx11_comp_error) { |
| 69 | if (p->thumbnail) XFreePixmap(d, p->thumbnail); |
| 70 | p->thumbnail = target; target = 0; |
| 71 | p->width = w; p->height = h; p->valid = 1; |
| 72 | } |
| 73 | done: |
| 74 | if (src) XRenderFreePicture(d, src); |
| 75 | if (dst) XRenderFreePicture(d, dst); |
| 76 | if (source) XFreePixmap(d, source); |
| 77 | if (target) XFreePixmap(d, target); |
| 78 | XSync(d, False); XSetErrorHandler(old); |
| 79 | return p->valid; |
| 80 | } |
| 81 | |
| 82 | static int vx11_comp_preview_draw(Display *d, Vx11CompositePreview *p, Drawable target, int x, int y, int w, int h) { |
| 83 | if (!p || !p->valid) return 0; |
| 84 | XRenderPictFormat *f = XRenderFindVisualFormat(d, DefaultVisual(d, DefaultScreen(d))); |
| 85 | Picture src = XRenderCreatePicture(d, p->thumbnail, f, 0, NULL); |
| 86 | Picture dst = XRenderCreatePicture(d, target, f, 0, NULL); |
| 87 | XTransform t = {{{XDoubleToFixed((double)p->width/w), 0, 0}, |
| 88 | {0, XDoubleToFixed((double)p->height/h), 0}, {0, 0, XDoubleToFixed(1)}}}; |
| 89 | XRenderSetPictureTransform(d, src, &t); |
| 90 | XRenderSetPictureFilter(d, src, FilterBilinear, NULL, 0); |
| 91 | XRenderComposite(d, PictOpSrc, src, None, dst, 0, 0, 0, 0, x, y, w, h); |
| 92 | XRenderFreePicture(d, src); XRenderFreePicture(d, dst); |
| 93 | return 1; |
| 94 | } |
| 95 | |
| 96 | #endif |
| 97 | |