/* Build with: gcc -o cairoframe -g cairoframe.c $(pkg-config --libs --cflags cairo x11) */ #include #include #include #include #include #include #include #include #define red 0.1 #define green 0.5 #define blue 1.0 #define alpha 0.7 #define linew 2 #define width 400 #define height 300 #define x 100 #define y 100 int main () { Display *xdpy; Window xwindow; Window xroot; XSetWindowAttributes attrs; XVisualInfo xvisual_info; Visual *xvisual; XEvent xevent; int depth; int scr; cairo_surface_t *surface; cairo_t *cr; xdpy = XOpenDisplay(NULL); if (!xdpy) { fprintf (stderr, "Cannot open display\n"); return -1; } scr = DefaultScreen(xdpy); xroot = DefaultRootWindow(xdpy); if (!XMatchVisualInfo (xdpy, scr, 32, TrueColor, &xvisual_info)) { fprintf (stderr, "Cannot find matching visual\n"); return -1; } xvisual = xvisual_info.visual; depth = xvisual_info.depth; attrs.override_redirect = True; attrs.colormap = XCreateColormap (xdpy, xroot, xvisual, AllocNone); attrs.background_pixel = BlackPixel (xdpy, scr); attrs.border_pixel = BlackPixel (xdpy, scr); xwindow = XCreateWindow (xdpy, xroot, x, y, width, height, 0, depth, InputOutput, xvisual, CWOverrideRedirect | CWColormap | CWBackPixel | CWBorderPixel, &attrs); XSelectInput(xdpy, xwindow, ExposureMask); XMapWindow (xdpy, xwindow); surface = cairo_xlib_surface_create (xdpy, xwindow, xvisual, width, height); cr = cairo_create (surface); cairo_set_line_width (cr, linew); cairo_set_line_join (cr, CAIRO_LINE_JOIN_MITER); while (1) { XNextEvent(xdpy, &xevent); if (xevent.type == Expose) { XClearWindow (xdpy, xwindow); cairo_set_source_rgba (cr, red, green, blue, alpha); cairo_paint (cr); cairo_set_source_rgba (cr, red, green, blue, 1.0); cairo_rectangle (cr, linew / 2, linew / 2, width - linew, height - linew); cairo_stroke (cr); XFlush (xdpy); } } XCloseDisplay(xdpy); return 0; }