source for "how many free color cells" xcmap

Steven A. DuChene sad at sduchene.mindspring.com
Sat Feb 17 00:23:41 EST 1996


	Here is some code that has been wandering around HP for quite
	a while. It tells you how many free color cells you have when
	running a Psuedocolor visual (256 colors). It has never been
	signed by the author. The code follows, then the makefile

-- 
Steven A. DuChene       Linux Weenie!    http://www.ysu.edu/~sduchene

Those who are mentally and emotionally healthy are those who have
learned when to say yes, when to say no and when to say whoopee.
                -- W.S. Krabill

----------------------start of xcmap.c--------------------------------

/*  This program will determine how many free colors are available in
    the default colormap.  It does so by trying to allocate as many
    color cells as possible, using a binary search to determine the
    maximum.
*/

#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

#define SYNTAX printf("Syntax: xcmap [-display <displayname>]\n")

char *classTable[]=
{
	"StaticGray",
	"GrayScale",
	"StaticColor",
	"PseudoColor",
	"TrueColor",
	"DirectColor"
};

main(argc, argv)
int argc;
char **argv;
{
	Display *display;
	Colormap cmap;
	unsigned long pmr[1], pr[256];
	unsigned int nplanes = 0;
	unsigned int npixels = 0;
	int allocated, failed, done;
	char *name = NULL;

	XVisualInfo vTemplate;
	XVisualInfo *visualList;
	int visualsMatched;

	/* Do the parameter processing */
	if (argc != 1)  /* if parameters are supplied */
	{
		if (argc != 3)  /* but not the right number */
		{
			printf("Warning: incorrect number of parameters. Parameters ignored\n");
			SYNTAX;
		}
		else /* correct number of parms */
		{
			/* check for display parm */
			if (strcmp(argv[1], "-display") && strcmp(argv[1], "-d"))
			{
				printf("Warning: unrecognized option: %s. Parameters ignored\n", 
					argv[1]);
				SYNTAX;
			}
			else /* option is good and number is correct; get the display */
				name = argv[2];
		}
	}
	if (name == NULL)
		name = getenv("DISPLAY");
	if (name == NULL)
	{
		printf("Error: Either -display option must be specified or\n");
		printf("DISPLAY environment variable must be set\n");
		SYNTAX;
		exit(4);
	}

	/* Open the display */
	if ((display = XOpenDisplay(name)) == NULL)
	{
		printf("Error: failed to open display\n");
		exit(1);
	}
	else
		printf("Successfully opened display %s\n", DisplayString(display));

	cmap = DefaultColormap(display, DefaultScreen(display));

	/* Get the default visual information */
	vTemplate.visual = DefaultVisual(display, DefaultScreen(display));
	vTemplate.visualid = XVisualIDFromVisual(vTemplate.visual);
	visualList = XGetVisualInfo (display, VisualIDMask, &vTemplate,
		&visualsMatched);
	if (visualsMatched != 1)
	{
		/* this is a diagnostic message; it should never happen */
		printf("Error: %d visuals matched visual ID %d\n", visualsMatched,
			visualList[0].visualid);
		exit(2);
	}
	else
	{
		if ((visualList[0].class == StaticGray) || 
		    (visualList[0].class == StaticColor) || 
				(visualList[0].class == TrueColor))
		{
			printf("To allocate private colors, visual may not be");
			printf(" StaticColor, TrueColor, or StaticGray.\n");
			printf("Your visual is %s.\n", classTable[visualList[0].class]);
			exit(0);
		}
		else
		  printf("Default visual is %d-color %s\n", visualList[0].colormap_size,
				classTable[visualList[0].class]);
	}

	npixels = visualList[0].colormap_size;	/* starting value */
	allocated = 0;	/* lower bound */
	failed = npixels + 1;	/* upper bound */

	/* Binary search to determine the number of available cells */
	for (done = 0; !done;)
	{
		if (XAllocColorCells(display, cmap, 0, pmr, nplanes, pr, npixels))
		{
			XFreeColors(display, cmap, pr, npixels, 0);
			allocated = npixels;
			if (allocated < (failed - 1))
				npixels = (allocated + failed) / 2;
			else
				done = 1;
		}
		else
		{
			failed = npixels;
			if (failed > (allocated + 1))
				npixels = (allocated + failed) / 2;
			else
				done = 1;
		}
	}
	printf("%d private color cells available\n", allocated);
	return (0);
}


------------------------makefile---------------------------------------

xcmap:
	gcc -o xcmap xcmap.c -I/usr/include/X11 -L/usr/X11R6/lib -lX11
xcmapr5:
	gcc -o xcmap xcmap.c -I/usr/X11/include -L/usr/X11/lib -lX11
xcmapr6:
	gcc -o xcmap xcmap.c -I/usr/include/X11 -L/usr/X11R6/lib -lX11






More information about the Ale mailing list