Beispiel #1
0
void
SplashCreateWindow(Splash * splash) {
    XSizeHints sizeHints;

    XSetWindowAttributes attr;

    attr.backing_store = NotUseful;
    attr.colormap = XDefaultColormapOfScreen(splash->screen);
    attr.save_under = True;
    attr.cursor = splash->cursor = XCreateFontCursor(splash->display, XC_watch);
    attr.event_mask = ExposureMask;

    SplashCenter(splash);

    splash->window = XCreateWindow(splash->display, XRootWindowOfScreen(splash->screen),
        splash->x, splash->y, splash->width, splash->height, 0, CopyFromParent,
        InputOutput, CopyFromParent, CWColormap | CWBackingStore | CWSaveUnder | CWCursor | CWEventMask,
        &attr);
    SplashUpdateSizeHints(splash);


    splash->wmHints = XAllocWMHints();
    if (splash->wmHints) {
        splash->wmHints->flags = InputHint | StateHint;
        splash->wmHints->input = False;
        splash->wmHints->initial_state = NormalState;
        XSetWMHints(splash->display, splash->window, splash->wmHints);
    }
}
Beispiel #2
0
void
FreeColors(Display * display, Screen * screen, int numColors,
        unsigned long *pr) {
    Colormap cmap = XDefaultColormapOfScreen(screen);

    XFreeColors(display, cmap, pr, numColors, 0);
}
Beispiel #3
0
int
GetNumAvailableColors(Display * display, Screen * screen, unsigned map_entries) {
    unsigned long pmr[1];
    unsigned long pr[SPLASH_COLOR_MAP_SIZE];
    unsigned nFailed, nAllocated, done = 0, nPlanes = 0;
    Colormap cmap;
    unsigned numColors = SPLASH_COLOR_MAP_SIZE; // never try allocating more than that

    if (numColors > map_entries) {
        numColors = map_entries;
    }
    cmap = XDefaultColormapOfScreen(screen);
    nAllocated = 0;             /* lower bound */
    nFailed = numColors + 1;    /* upper bound */

    /* Binary search to determine the number of available cells */
    for (done = 0; !done;) {
        if (XAllocColorCells(display, cmap, 0, pmr, nPlanes, pr, numColors)) {
            nAllocated = numColors;
            XFreeColors(display, cmap, pr, numColors, 0);
            if (nAllocated < (nFailed - 1)) {
                numColors = (nAllocated + nFailed) / 2;
            } else
                done = 1;
        } else {
            nFailed = numColors;
            if (nFailed > (nAllocated + 1))
                numColors = (nAllocated + nFailed) / 2;
            else
                done = 1;
        }
    }
    return nAllocated;
}
Beispiel #4
0
Colormap
AllocColors(Display * display, Screen * screen, int numColors,
        unsigned long *pr) {
    unsigned long pmr[1];
    Colormap cmap = XDefaultColormapOfScreen(screen);

    XAllocColorCells(display, cmap, 0, pmr, 0, pr, numColors);
    return cmap;
}
Beispiel #5
0
/* Create the icon window */
static Window
create_icon(Widget shell)
{
    Display *display = XtDisplay(shell);
    Screen *screen = XtScreen(shell);
    Colormap colormap = XDefaultColormapOfScreen(screen);
    int depth = DefaultDepthOfScreen(screen);
    unsigned long black = BlackPixelOfScreen(screen);
    Window window;
    Pixmap pixmap, mask;
    XColor color;
    GC gc;
    XGCValues values;

    /* Create the actual icon window */
    window = XCreateSimpleWindow(
        display, RootWindowOfScreen(screen),
        0, 0, mask_width, mask_height, 0,
        CopyFromParent, CopyFromParent);

    /* Allocate the color red by name */
    XAllocNamedColor(display, colormap, "red", &color, &color);

    /* Create a pixmap from the red bitmap data */
    pixmap = XCreatePixmapFromBitmapData(
        display, window, (char *)red_bits, red_width, red_height,
        color.pixel, black, depth);

    /* Create a graphics context */
    values.function = GXxor;
    gc = XCreateGC(display, pixmap, GCFunction, &values);

    /* Create a pixmap for the white 'e' and paint it on top */
    mask = XCreatePixmapFromBitmapData(
        display, pixmap, (char *)white_bits, white_width, white_height,
        WhitePixelOfScreen(screen) ^ black, 0, depth);
    XCopyArea(display, mask, pixmap, gc, 0, 0,
              white_width, white_height, 0, 0);
    XFreePixmap(display, mask);
    XFreeGC(display, gc);

#ifdef HAVE_LIBXEXT
    /* Create a shape mask and apply it to the window */
    mask = XCreateBitmapFromData(display, pixmap, (char *)mask_bits,
                                 mask_width, mask_height);
    XShapeCombineMask(display, window, ShapeBounding, 0, 0, mask, ShapeSet);
#endif /* HAVE_LIBXEXT */

    /* Set the window's background to be the pixmap */
    XSetWindowBackgroundPixmap(display, window, pixmap);
    return window;
}
Beispiel #6
0
P op_mapcolor(void)
{
#if X_DISPLAY_MISSING
	return NO_XWINDOWS;
#else
  XColor color; 
  B f[FRAMEBYTES]; 
  D val[3]; 
  P k;
 
  if (dvtdisplay == NULL) return NO_XWINDOWS;
  if (o_1< FLOORopds) return OPDS_UNF;
  if (CLASS(o_1) != ARRAY) return OPD_CLA;
  if (ARRAY_SIZE(o_1) != 3) return RNG_CHK;
  TAG(f) = (ARRAY | DOUBLETYPE); 
  ATTR(f) = 0;
  VALUE_BASE(f) = (P)val; 
  ARRAY_SIZE(f) = 3;
  MOVE(o_1,f);

  for (k=0; k<3; k++) if ((val[k] < 0.0) | (val[k] > 1.0)) return RNG_CHK;
  color.red = val[0] * 65535.0;
  color.green = val[1] * 65535.0;
  color.blue = val[2] * 65535.0;
  if ((k = HXAllocColor(dvtdisplay,
			XDefaultColormapOfScreen(dvtscreen),
			&color)) 
      == 0)
    return RNG_CHK;

  TAG(o_1) = (NUM | LONGBIGTYPE); 
  ATTR(o_1) = 0;
  LONGBIG_VAL(o_1) = color.pixel;
  return OK;
#endif
}
Beispiel #7
0
void initx(xxfmt *xx)
   {
   int rslt;
   char title[64];

   xx->dpy = XOpenDisplay(NULL);

   if (xx->dpy == NULL)
      {
      fprintf(stderr,"X Windows failure\n");
      exit(1);
      } /* if X Windows is not active */

   assert(xx->dpy);

   /* get dimensions of root window */
   xx->rootww = XDisplayWidth(xx->dpy,0);
   xx->rootwh = XDisplayHeight(xx->dpy,0);

   /* make display window smaller than root window */
   /* allow for menu bar on top */
   // xx->dpywdth = xx->rootww -  80;
   // xx->dpyhght = xx->rootwh - 100;
   xx->dpywdth = 512;        /* need a power of 2 */
   xx->dpyhght = 512;        /* need a power of 2 */

   xx->whiteColor = WhitePixel(xx->dpy, DefaultScreen(xx->dpy));
   xx->blackColor = BlackPixel(xx->dpy, DefaultScreen(xx->dpy));

   xx->w = XCreateSimpleWindow(xx->dpy,
      DefaultRootWindow(xx->dpy),
      0, 0, 
      xx->dpywdth, xx->dpyhght,
      0, xx->whiteColor,
      xx->whiteColor);

   XSelectInput(xx->dpy, xx->w, StructureNotifyMask);

   XMapWindow(xx->dpy, xx->w);

   xx->gc = XCreateGC(xx->dpy, xx->w, 0, NULL);

   xx->fontid = (Font) XLoadFont(xx->dpy,"12x24");

   XSetFont(xx->dpy,xx->gc,xx->fontid);

   XSetForeground(xx->dpy, xx->gc, xx->blackColor);

   xx->dfltscr = XDefaultScreenOfDisplay(xx->dpy);
   if (xx->dfltscr == NULL)
      {
      fprintf(stderr,"XDefaultScreenOfDisplay failed\n");
      perror("XDefaultScreenOfDisplay failed");
      exit(1);
      } /* if error */

   xx->cmap = XDefaultColormapOfScreen(xx->dfltscr);

   rslt = XAllocNamedColor(xx->dpy,xx->cmap,"red",
      &xx->scrdef,&xx->exctdef);

   if (rslt < 0)
      {
      fprintf(stderr,"XAllocNamedColor failed\n");
      perror("XAllocNamedColor failed");
      exit(1);
      } /* if error */
   xx->red = xx->scrdef.pixel;

   XSetWindowBorderWidth(xx->dpy, xx->w, 40);

   sprintf(title,"Random Screen");
   XStoreName(xx->dpy,xx->w,title);
   XSetIconName(xx->dpy,xx->w,title);

   while(1)
      {
      XEvent e;
      XNextEvent(xx->dpy, &e);
      if (e.type == MapNotify) break;
      } /* wait for window initialization */

   } /* initx */
Beispiel #8
0
// filename:  regular piece xpm data
// num:       ID for object instance
// closeness: Color closeness for xpm library
void
Mahjong::ReadFile(Widget w, char *filename, int num, int closeness)
{
    XpmAttributes attributes;
    int result;
    Pixmap mask;
    Colormap colormap = XDefaultColormapOfScreen(XtScreen(w));
    GC gc;
    XGCValues gcv;

    if (rdata != 0 && data != rdata) {
        XFreePixmap(XtDisplay(w), rdata);
        XFreePixmap(XtDisplay(w), rdatag);
    }
    if (data != 0) {
        XFreePixmap(XtDisplay(w), data);
        XFreePixmap(XtDisplay(w), datag);
    }
    attributes.valuemask = XpmColormap | XpmSize | XpmCloseness;
    attributes.colormap  = colormap;
    attributes.exactColors = False;
    attributes.closeness = closeness;
    id = num;
    result = XpmReadFileToPixmap(XtDisplay(w), XtWindow(w), filename,
                                  &data, &mask, &attributes);
    width  = original_width  = attributes.width;
    height = original_height = attributes.height;
    if (result != XpmSuccess && result != XpmColorError) {
        fprintf(stderr, "XpmReadFileToPixmap failed ");
        switch(result) {
        case XpmOpenFailed:
            fprintf(stderr, "(Cannot open xpm files)\n");
            break;
        case XpmFileInvalid:
            fprintf(stderr, "(xpm file may be broken)\n");
            break;
        case XpmNoMemory:
            fprintf(stderr, "(memory exhausted)\n");
            break;
        case XpmColorFailed:
            fprintf(stderr, "(color allocation failed [closeness:%d])\n",
                    closeness);
            break;
        default:
            fprintf(stderr, "(reason unknown)\n");
            break;
        }
        fprintf(stderr, " %s\n", filename);
        exit(1);
    } else {
        attributes.valuemask = XpmColormap;
        gcv.foreground = WhitePixelOfScreen(XtScreen(w));
        gc = XCreateGC(XtDisplay(w), XtWindow(w), GCForeground, &gcv);
        datag = MakeHalfBrightPixmap(data, gc);
        result = XpmSuccess;
        XFreeGC(XtDisplay(w), gc);
    }
    rdata  = data;
    rdatag = datag;
    resized = 0;
}
Beispiel #9
0
int initsystem(void)  /* retourne 1 si initialisation reussie */
{
    int i;
    int windowW;
    int windowH;
    XSetWindowAttributes xswa;
    XGCValues xgcv;
    XSizeHints xsh;

    windowW=WIDTH*RATIO;
    windowH=HEIGHT*RATIO;
    display=XOpenDisplay(0);

    if (!display) return(0);
    else
    {
        mousek=0;
        XAutoRepeatOff(display);
        timestart=last=systime();
        screen=XDefaultScreenOfDisplay(display);
        rdepth=sdepth=XDefaultDepthOfScreen(screen);
        if (emulmono) sdepth=1;
        XSynchronize(display,0);

        black=XBlackPixelOfScreen(screen);
        white=XWhitePixelOfScreen(screen);
        colormap=XDefaultColormapOfScreen(screen);
        visual=XDefaultVisualOfScreen(screen);

        xswa.event_mask=VisibilityChangeMask;
        xswa.background_pixel=black;
        xswa.border_pixel=white;
        xswa.override_redirect=0;
        xswa.backing_store=Always;
        xswa.bit_gravity=StaticGravity;
        xswa.win_gravity=CenterGravity;
        window=XCreateWindow(display,XRootWindowOfScreen(screen),
                             randval(50),randval(100),windowW,windowH,0,
                             XDefaultDepthOfScreen(screen),InputOutput,
                             XDefaultVisualOfScreen(screen),
                             CWWinGravity|CWBitGravity|CWBackingStore|CWEventMask|
                             CWBackPixel|CWOverrideRedirect|CWBorderPixel,&xswa);

        if (sdepth!=1)
            xcolormap=XCreateColormap(display,window,
                                      visual,AllocAll);

        xgcv.foreground = white;
        xgcv.background = black;
        gc=XCreateGC(display,window,GCForeground | GCBackground,&xgcv);

        XSetGraphicsExposures(display,gc,False);
        /* CAPITAL!!! : evite d'accumuler sans cesse des expose events */

        xsh.x=0;
        xsh.y=0;
        xsh.width=windowW;
        xsh.height=windowH;
        xsh.min_width=windowW;
        xsh.max_width=windowW;
        xsh.min_height=windowH;
        xsh.max_height=windowH;
        xsh.flags=PPosition|PSize|PMinSize|PMaxSize;

        XSetNormalHints(display, window, &xsh);
        XStoreName(display,window,"");
        XMapWindow(display, window);
        XSelectInput(display,window,PointerMotionMask|ButtonPressMask|
                     ButtonReleaseMask|KeyPressMask|KeyReleaseMask);
        XFlush(display);
        XSync(display,0);

        for (i=0; i<(NBCOLORS+2); i++)
        {
            if (i&1) pixels[i]=white;
            else pixels[i]=black;
            if (i==NBCOLORS) pixels[i]=0;
            if (i==(NBCOLORS+1)) pixels[i]=(1<<rdepth)-1;
            xgcv.foreground=pixels[i];
            xgcv.background=black;
            gctab[i]=XCreateGC(display,window,
                               GCForeground|GCBackground,&xgcv);
            XSetFunction(display,gctab[i],GXcopy);
            XSetFillStyle(display,gctab[i],FillSolid);
        }

        ecran[0]=XCreatePixmap(display,window,windowW,windowH,rdepth);
        ecran[1]=XCreatePixmap(display,window,windowW,windowH,rdepth);

        for(i=0; i<9; i++)
            graypixmap[i]=XCreatePixmapFromBitmapData(display,window,
                          &graypat[i][0],8,8,white,black,rdepth);

        setpalette(egapal);

        cls();
        swap();
        cls();

        empty();
        waitdelay(500);

        return(1);
    }
}
Beispiel #10
0
static void prnt(Pixmap pixmap, FILE *fp)
{
        unsigned int width, height, depth = 8;
        unsigned int ncolors, cpp;
        int x, y, col;
        char *title = "Plugged in image";
        time_t t;
        char **data, *key, *color;
        XpmAttributes xa;
        int i, n;
        colors *cm;

        xa.valuemask = 0;

        /* figure out a thing or two */
        n = XpmCreateDataFromPixmap(XtDisplay(topLevel), &data,
                pixmap, None, &xa);
        if (n != XpmSuccess) {
                printf("503 XpmCreateDataFromPixmap returns %d\n", n);
                return;
        }
        sscanf(data[0], "%d %d %d %d", &width, &height, &ncolors, &cpp);

        cm = (colors *)MwMalloc(ncolors*sizeof(colors));

        for (i = 0; i < ncolors; i++) {
                cm[i].chars = (char *)MwMalloc(cpp);
                memcpy(cm[i].chars, data[i+1], cpp);
                key = strtok(data[i+1]+cpp, " \t");     /* skip past chars */
                color = strtok(NULL, " \t");
                while (key && color && strcmp(key, "c")) {
                        key = strtok(NULL, " \t");
                        color = strtok(NULL, " \t");
                };
                if (!color) {
                        printf("504 No such color\n");
                        return;
                }
                XParseColor(XtDisplay(topLevel),
                        XDefaultColormapOfScreen(XtScreen(topLevel)),
                        color, &cm[i].xcolor);
        }

        /* print postscript preblurb */
        fprintf(fp, "%%!PS-Adobe-2.0 EPSF-2.0\n");
        fprintf(fp, "%%%%Creator: Image plugin for Siag Office\n");
        fprintf(fp, "%%%%Title: %s\n", title);
        fprintf(fp, "%%%%Pages: 1\n");
        fprintf(fp, "%%%%BoundingBox: %d %d %d %d\n", 0, 0, width, height);
        t = time(NULL);
        fprintf(fp, "%%%%CreationDate: %s", ctime(&t));
        fprintf(fp, "%%%%EndComments\n");
        fprintf(fp, "%%%%EndProlog\n");
        fprintf(fp, "%%%%Page: 1 1\n\n\n");
        fprintf(fp, "gsave\n\n");
        fprintf(fp, "/inch {72 mul} def\n");
        fprintf(fp, "%d %d scale\n", width, height);
        fprintf(fp, "/line %d string def\n", 3*width);
        fprintf(fp, "%d %d %d\n", width, height, depth);
        fprintf(fp, "[ %d %d %d %d %d %d ]\n", width, 0, 0, -height, 0, height);
        fprintf(fp, "{currentfile line readhexstring pop}\n");
        fprintf(fp, "false 3 colorimage\n");

        /* print all the pixels */
        col = 0;
        for (y = 0; y < height; y++) {
                char *line = data[y+ncolors+1];
                for (x = 0; x < width; x++) {
                        char *pix = line+cpp*x;
                        for (i = 0; i < ncolors; i++)
                                if (!memcmp(cm[i].chars, pix, cpp)) break;
                        if (i == ncolors) i = 0;
                        fprintf(fp, "%02hx%02hx%02hx",
                                (cm[i].xcolor.red / 256) & 255,
                                (cm[i].xcolor.green / 256) & 255,
                                (cm[i].xcolor.blue / 256) & 255);
                        col += 6;
                        if (col >= 72) {
                                fprintf(fp, "\n");
                                col = 0;
                        }
                }
        }
        if (col) fprintf(fp, "\n");

        /* print postscript postblurb */
        fprintf(fp, "%%\n\n");
        fprintf(fp, "grestore\n");

        for (i = 0; i < ncolors; i++) MwFree(cm[i].chars);
        MwFree(cm);
        MwFree(data);
}
int main (int argc, char* argv[])
{
	/* Determine number of iterations. */
	if (2 != argc)
	{
		printf("usage: %s <num-iterations>\n", argv[0]);
		exit(1);
	}
	int numIterations = atoi(argv[1]);

	int returnCode = 0;
	Display* xDisplay = NULL;
	Window xWindow = 0;
	Picture xPictureWindow = 0;
	Pixmap xPixmapMain = 0;
	Picture xPictureMain = 0;
	Pixmap xPixmapOverlay = 0;
	Picture xPictureOverlay = 0;
	Pixmap xPixmapMask = 0;
	Picture xPictureMask = 0;
	GC gcMask = 0;

	/* Access X display. */
	if (NULL == (xDisplay = XOpenDisplay(NULL)))
	{
		printf("XOpenDisplay(NULL) failed\n");
		returnCode = 1;
		goto error;
	}

	/* Access info about the screen. */
	Screen* xScreen = XDefaultScreenOfDisplay(xDisplay);
	GC gc = XDefaultGCOfScreen(xScreen);
	Colormap xColormap = XDefaultColormapOfScreen(xScreen);

	/* Create main X window */
	xWindow = XCreateSimpleWindow(
			xDisplay,
			RootWindow(xDisplay, 0),
			0, 0,
			IMAGE_WIDTH*2,
			IMAGE_HEIGHT*2,
			0,
			BlackPixel(xDisplay, 0),
			BlackPixel(xDisplay, 0));
	if (0 == xWindow)
	{
		printf("XCreateSimpleWindow failed\n");
		returnCode = 1;
		goto error;
	}
	XMapWindow(xDisplay, xWindow);
	XSync(xDisplay, False);

	/* Get the attributes associated with the main window. */
	XWindowAttributes xWindowAttr;
	if (!XGetWindowAttributes(xDisplay, xWindow, &xWindowAttr))
	{
		printf("XGetWindowAttributes failed\n");
		returnCode = 1;
		goto error;
	}

	/* Find the X render picture format associated with the visual */
	/* for the main window */
	XRenderPictFormat* xRenderPictFormatWindow =
		XRenderFindVisualFormat(xDisplay, xWindowAttr.visual);
	if (NULL == xRenderPictFormatWindow)
	{
		printf("XRenderFindVisualFormat failed\n");
		returnCode = 1;
		goto error;
	}

	/* Find the X render picture format associated with 8 bit alpha. */
	XRenderPictFormat xRenderPictFormatTemplate;
	xRenderPictFormatTemplate.depth = 8;
	xRenderPictFormatTemplate.type = PictTypeDirect;
	xRenderPictFormatTemplate.direct.alphaMask = 0x0FF;
	unsigned long xRenderPictFormatTemplateMask =
			PictFormatDepth | PictFormatType | PictFormatAlphaMask;
	XRenderPictFormat* xRenderPictFormatMask =
		XRenderFindFormat(
			xDisplay,
			xRenderPictFormatTemplateMask,
			&xRenderPictFormatTemplate,
			0);
	if (NULL == xRenderPictFormatMask)
	{
		printf("XRenderFindFormat failed\n");
		returnCode = 1;
		goto error;
	}

	/* Create X render picture associated with the screen. */
	/* Having the same visual format as the window. */
	xPictureWindow = XRenderCreatePicture(
				xDisplay,
				xWindow,
				xRenderPictFormatWindow,
				0,
				NULL);
	if (0 == xPictureWindow)
	{
		printf("XRenderCreatePicture (window) failed\n");
		returnCode = 1;
		goto error;
	}

	/* Create backing pixmap for the main window. */
	xPixmapMain = XCreatePixmap(
			xDisplay,
			xWindow,
			xWindowAttr.width,
			xWindowAttr.height,
			xWindowAttr.depth);
	if (0 == xPixmapMain)
	{
		printf("XCreatePixmap (main) failed\n");
		returnCode = 1;
		goto error;
	}

	/* Create X render picture associated with the backing pixmap. */
	/* Having the same visual format as the window. */
	xPictureMain = XRenderCreatePicture(
			xDisplay,
			xPixmapMain,
			xRenderPictFormatWindow,
			0,
			NULL);
	if (0 == xPictureMain)
	{
		printf("XRenderCreatePicture (main) failed\n");
		returnCode = 1;
		goto error;
	}

	/* Draw concentric rectangles of different gray. */
	unsigned i;
	for (i = 0; i < 256; ++i)
	{
		float fGray = i / 255.0;

		/* Find the color gray. */
		XcmsColor xColorGray;
		xColorGray.spec.RGBi.red = fGray;
		xColorGray.spec.RGBi.green = fGray;
		xColorGray.spec.RGBi.blue = fGray;
		xColorGray.format = XcmsRGBiFormat;
		if (0 == XcmsAllocColor(
				xDisplay,
				xColormap,
				&xColorGray,
				XcmsRGBFormat))
		{
			printf("XcmsAllocColor failed\n");
			returnCode = 1;
			goto error;
		}

		/* Change the drawing color for the main window. */
		XSetForeground(xDisplay, gc, xColorGray.pixel);

		XDrawRectangle(
			xDisplay,
			xPixmapMain,
			gc,
			i, i,
			(IMAGE_WIDTH - i) * 2 - 1,
			(IMAGE_HEIGHT - i) * 2 - 1);
	}
	XRenderComposite(
		xDisplay,
		PictOpSrc,
		xPictureMain,	/* src */
		0,		/* mask */
		xPictureWindow,	/* dst */
		0, 0,		/* src (x,y) */
		0, 0,		/* mask (x,y) */
		0, 0,		/* dst (x,y) */
		xWindowAttr.width,
		xWindowAttr.height);
	XSync(xDisplay, False);

	/* Create pixmap for the overlay content. */
	xPixmapOverlay = XCreatePixmap(
				xDisplay,
				xWindow,
				IMAGE_WIDTH,
				IMAGE_HEIGHT,
				xWindowAttr.depth);
	if (0 == xPixmapOverlay)
	{
		printf("XCreatePixmap (overlay) failed\n");
		returnCode = 1;
		goto error;
	}

	/* Create X render picture assocaited with the overlay pixmap. */
	/* Having the same visual format as the window. */
	xPictureOverlay = XRenderCreatePicture(
				xDisplay,
				xPixmapOverlay,
				xRenderPictFormatWindow,
				0,
				NULL);
	if (0 == xPictureOverlay)
	{
		printf("XRenderCreatePicture (overlay) failed\n");
		returnCode = 1;
		goto error;
	}

	/* Fill the overlay with black to be used for overlay color. */
	XSetForeground(xDisplay, gc, XBlackPixelOfScreen(xScreen));
	XFillRectangle(
		xDisplay,
		xPixmapOverlay,
		gc,
		0, 0,
		IMAGE_WIDTH,
		IMAGE_HEIGHT);

	/* Create pixmap for the mask content. */
	xPixmapMask = XCreatePixmap(
				xDisplay,
				xWindow,
				IMAGE_WIDTH,
				IMAGE_HEIGHT,
				8);
	if (0 == xPixmapMask)
	{
		printf("XCreatePixmap (mask) failed\n");
		returnCode = 1;
		goto error;
	}

	/* Create X render picture assocaited with the mask pixmap. */
	xPictureMask = XRenderCreatePicture(
				xDisplay,
				xPixmapMask,
				xRenderPictFormatMask,
				0,
				NULL);
	if (0 == xPictureMask)
	{
		printf("XRenderCreatePicture (mask) failed\n");
		returnCode = 1;
		goto error;
	}

	/* Create a GC to go with mask */
	gcMask = XCreateGC(xDisplay, xPixmapMask, 0, NULL);
	XSetForeground(xDisplay, gcMask, 0x00000000);
	XFillRectangle(
		xDisplay,
		xPixmapMask,
		gcMask,
		0, 0,
		IMAGE_WIDTH,
		IMAGE_HEIGHT);
	XSetForeground(xDisplay, gcMask, 0x40404040);
	XDrawRectangle(
		xDisplay,
		xPixmapMask,
		gcMask,
		0, 0,
		IMAGE_WIDTH-1,
		IMAGE_HEIGHT-1);
	XFillArc(
		xDisplay,
		xPixmapMask,
		gcMask,
		100, 100,
		100, 100,
		0,		/* start angle-degrees * 64 */
		360 * 64);	/* extent angle-degrees * 64 */

	Bool bIncX = True;
	Bool bIncY = True;
	Bool bNextRow = False;
	int x = 0;
	int y = 0;
	struct timeval timeStart;
	gettimeofday(&timeStart, NULL);
	int iter;
	for (iter = 0; iter < numIterations; ++iter)
	{
		XRenderComposite(
			xDisplay,
			PictOpSrc,
			xPictureMain,	/* src */
			0,		/* mask */
			xPictureWindow,	/* dst */
			x, y,		/* src (x,y) */
			0, 0,		/* mask (x,y) */
			x,		/* dst x */
			y,		/* dst y */
			IMAGE_WIDTH,
			IMAGE_HEIGHT);

		if (bNextRow)
		{
			if (bIncY)
			{
				if ((y += 10) >= IMAGE_HEIGHT)
				{
					y = IMAGE_HEIGHT - 1;
					bIncY = False;
				}
			}
			else
			{
				if ((y -= 10) < 0)
				{
					y = 0;
					bIncY = True;
				}
			}

			bNextRow = False;
		}
		else
		{
			if (bIncX)
			{
				if (++x >= IMAGE_WIDTH)
				{
					x = IMAGE_WIDTH - 1;
					bIncX = False;
					bNextRow = True;
				}
			}
			else
			{
				if (--x < 0)
				{
					x = 0;
					bIncX = True;
					bNextRow = True;
				}
			}
		}

		XRenderComposite(
			xDisplay,
			PictOpOver,
			xPictureOverlay,/* src */
			xPictureMask,	/* mask */
			xPictureWindow,	/* dst */
			0, 0,		/* src (x,y) */
			0, 0,		/* mask (x,y) */
			x,		/* dst x */
			y,		/* dst y */
			IMAGE_WIDTH,
			IMAGE_HEIGHT);
	}
	XSync(xDisplay, False);

	struct timeval timeEnd;
	gettimeofday(&timeEnd, NULL);
	double elapsedSec =
		getElapsedMicroseconds(&timeStart, &timeEnd) / 1000000L;
	double fps = numIterations / elapsedSec;
	printf("average update rate = %.1lf FPS\n", fps);

error:

	if (0 != gcMask)
	{
		XFreeGC(xDisplay, gcMask);
		gcMask = 0;
	}

	if (0 != xPictureMask)
	{
		XRenderFreePicture(xDisplay, xPictureMask);
		xPictureMask = 0;
	}

	if (0 != xPixmapMask)
	{
		XFreePixmap(xDisplay, xPixmapMask);
		xPixmapMask = 0;
	}

	if (0 != xPictureOverlay)
	{
		XRenderFreePicture(xDisplay, xPictureOverlay);
		xPictureOverlay = 0;
	}

	if (0 != xPixmapOverlay)
	{
		XFreePixmap(xDisplay, xPixmapOverlay);
		xPixmapOverlay = 0;
	}

	if (0 != xPictureMain)
	{
		XRenderFreePicture(xDisplay, xPictureMain);
		xPictureMain = 0;
	}

	if (0 != xPixmapMain)
	{
		XFreePixmap(xDisplay, xPixmapMain);
		xPixmapMain = 0;
	}

	if (0 != xPictureWindow)
	{
		XRenderFreePicture(xDisplay, xPictureWindow);
		xPictureWindow = 0;
	}

	if (0 != xWindow)
	{
		XDestroyWindow(xDisplay, xWindow);
		xWindow = 0;
	}

	if (NULL != xDisplay)
	{
		XCloseDisplay(xDisplay);
		xDisplay = NULL;
	}

	return returnCode;
}