All pastes #635234 Raw Edit

argb_demo.c

public c v1 · immutable
#635234 ·published 2007-07-26 22:43 UTC
rendered paste body
/* gcc -o argb_demo argb_demo.c -L/usr/X11R6/lib -lXrender -lX11 */#include <stdio.h>#include <X11/Xlib.h>#include <X11/extensions/Xrender.h>#define WIDTH_DEFAULT 200#define HEIGHT_DEFAULT 100/* find_argb_visual courtesy of Keith Packard */Visual *find_argb_visual (Display *dpy, int scr){    XVisualInfo	*xvi;    XVisualInfo	template;    int nvi, i;    XRenderPictFormat *format;    Visual *visual;    template.screen = scr;    template.depth = 32;    template.class = TrueColor;    xvi = XGetVisualInfo (dpy, 			  VisualScreenMask |			  VisualDepthMask |			  VisualClassMask,			  &template,			  &nvi);    if (xvi == NULL)	return NULL;    visual = NULL;    for (i = 0; i < nvi; i++)    {	format = XRenderFindVisualFormat (dpy, xvi[i].visual);	if (format->type == PictTypeDirect && format->direct.alphaMask)	{	    visual = xvi[i].visual;	    break;	}    }    XFree (xvi);    return visual;}/* Naming a window shouldn't require specifying width/height, but * that's what Xlib makes us do here. */static voidname_window (Display *dpy, Window win, int width, int height,	     const char *name){    XSizeHints *size_hints;    XWMHints *wm_hints;    XClassHint *class_hint;    size_hints = XAllocSizeHints ();    size_hints->flags = 0;    size_hints->x = 0;    size_hints->y = 0;    size_hints->width = width;    size_hints->height = height;    wm_hints = XAllocWMHints ();    wm_hints->flags = InputHint;    wm_hints->input = True;        class_hint = XAllocClassHint ();    class_hint->res_name  = "argb-demo";    class_hint->res_class = "ARGB-demo";        Xutf8SetWMProperties (dpy, win, name, name, 0, 0, 			  size_hints, wm_hints, class_hint);    XFree (size_hints);    XFree (wm_hints);    XFree (class_hint);}/* Colors as 0xAARRGGBB, pre-multiplied */static const int transparent     = 0x00000000;static const int opaque_blue     = 0xff0000ff;static const int translucent_red = 0x80800000;static voiddraw (Display *dpy, Window win, GC gc,      unsigned int width, unsigned int height){    int off;    if (width < height)	off = width / 10;    else	off = height / 10;    XClearWindow (dpy, win);    XSetForeground(dpy, gc, opaque_blue);    XFillRectangle (dpy, win, gc,		    off, off, width - 2 * off, height - 2 * off);    XSetForeground(dpy, gc, translucent_red);    XFillRectangle (dpy, win, gc,		    2 * off, 2 * off, width - 4 * off, height - 4 * off);}intmain(void){    Display *dpy;    int scr;    Window win, root;    GC gc;    Visual *visual;    XSetWindowAttributes wattr;    unsigned long wmask;    unsigned int width  = WIDTH_DEFAULT;    unsigned int height = HEIGHT_DEFAULT;    int needs_redraw;    dpy = XOpenDisplay (0);    if (dpy == NULL) {	fprintf(stderr, "Failed to open display: %s\n", XDisplayName(0));	return 1;    }    XSynchronize (dpy, 1);    scr = DefaultScreen (dpy);    root = RootWindow (dpy, scr);    visual = find_argb_visual (dpy, scr);    if (visual == NULL) {	fprintf (stderr, "Could not find ARGB visual. Aborting.\n");	return 1;    }    wattr.event_mask = ExposureMask | StructureNotifyMask;    wattr.background_pixel = transparent;    wattr.border_pixel = 0;    wattr.colormap = XCreateColormap (dpy, root, visual, AllocNone);    wmask = CWEventMask | CWBackPixel | CWBorderPixel | CWColormap;    win = XCreateWindow (dpy, root, 0, 0, width, height, 0,			 32, InputOutput, visual, wmask, &wattr);    name_window (dpy, win, width, height, "ARGB visual demo");    XSelectInput (dpy, win, KeyPressMask | StructureNotifyMask | ExposureMask);    XMapWindow (dpy, win);    gc = XCreateGC (dpy, win, 0, NULL);    needs_redraw = 1;    while (1) {	XEvent xev;	if (! XPending (dpy) && needs_redraw) {	    draw (dpy, win, gc, width, height);	    needs_redraw = 0;	}		XNextEvent (dpy, &xev);	/* Quit on key press. */	switch (xev.xany.type) {	case KeyPress:	    goto DONE;	case ConfigureNotify:	    width = xev.xconfigure.width;	    height = xev.xconfigure.height;	    /* fall-through */	default:	    needs_redraw = 1;	}    }    DONE:    XFreeGC (dpy, gc);    XCloseDisplay (dpy);        return 0;}