/* gcc -o test test.c -Wall -g `pkg-config --cflags --libs x11 cairo-xlib` */ #include #include #include #include #include #include #include static Visual *findArgbVisual (Display *d) { int screen = DefaultScreen (d); XVisualInfo *xvi; XVisualInfo temp; int nvi; int i; XRenderPictFormat *format; Visual *visual; temp.screen = screen; temp.depth = 32; temp.class = TrueColor; xvi = XGetVisualInfo (d, VisualScreenMask | VisualDepthMask | VisualClassMask, &temp, &nvi); if (!xvi) return 0; visual = 0; for (i = 0; i < nvi; i++) { format = XRenderFindVisualFormat (d, xvi[i].visual); if (format->type == PictTypeDirect && format->direct.alphaMask) { visual = xvi[i].visual; break; } } XFree (xvi); return visual; } int main (int argc, char **argv) { Display *d; Visual *v; XWindowAttributes wa; Window w, w2; cairo_surface_t *s, *s2; cairo_t *cr; d = XOpenDisplay(NULL); if(!d) return 1; v = findArgbVisual (d); if(!v) abort (); XSetWindowAttributes attr = {}; attr.background_pixel = 0; attr.border_pixel = 0; attr.colormap = XCreateColormap (d, RootWindow(d, 0), v, AllocNone); w = XCreateWindow(d, RootWindow(d, 0), 0, 0, 1024, 768, 0, 32, InputOutput, v, CWBackPixel | CWBorderPixel | CWColormap, &attr); XMapWindow (d, w); s = cairo_xlib_surface_create (d, w, v, 1024, 768); cr = cairo_create (s); if(argc==2) w2 = XCreateWindow(d, RootWindow(d, 0), 0, 0, 100, 100, 0, 32, InputOutput, v, CWBackPixel | CWBorderPixel | CWColormap, &attr); else w2 = XCreateSimpleWindow (d, RootWindow(d, 0), 0, 0, 100, 100, 0, 0, 1234567); XMapWindow (d, w2); XGetWindowAttributes(d, w2, &wa); s2 = cairo_xlib_surface_create (d, w2, wa.visual, wa.width, wa.height); cairo_t *cr2 = cairo_create (s2); cairo_set_operator (cr2, CAIRO_OPERATOR_SOURCE); cairo_set_source_rgba (cr2, 0.0, 0.0, 1.0, 0.5); cairo_paint (cr2); cairo_destroy (cr2); XFlush (d); XSync (d, w); while(1) { cairo_save (cr); cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE); cairo_set_source_rgba (cr, 1.0, 0.0, 0.0, 0.5); cairo_paint (cr); cairo_restore (cr); cairo_matrix_t m; m.xx = m.yy = 0.5; m.xy = m.yx = 0.0; m.x0 = m.y0 = -32; cairo_pattern_t *p = cairo_pattern_create_for_surface (s2); cairo_pattern_set_matrix (p, &m); cairo_set_source (cr, p); cairo_paint (cr); cairo_pattern_destroy (p); XFlush (d); XSync (d, w); sleep (1); } return 0; }