From: pmartz@dsd.es.com (Paul Martz)
Subject: Re: How to open a TrueColor window?
Reply-To: pmartz@dsd.es.com (Paul Martz)
Organization: Evans & Sutherland Computer Corp., Salt Lake City, UT
Date: Mon, 23 Nov 92 19:43:38 GMT

In article <1992Nov19.031410.22186%ptlai@et.nthu.edu.tw>, ptlai@et.nthu.edu.tw (Pao-Tzung Lai) writes:
> I have a problem in Xlib programming:
> 
> I want to create a window with TrueColor (depth 24) visual under a
> parent window (root window) which visual is PseudoColor (depth 8).
> Does anyone have experience on this? Any hint is appreciated.

This code looks for "-24" on the command line and opens up 24bit
TrueColor if it is there. Hope it's not too confusing, it does a bunch
of wierd colormap stuff too...
-- 

    -paul	pmartz@dsd.es.com
		 Evans & Sutherland

------------------------------------------------------
#include <stdio.h>
#include <math.h>
#include <sys/time.h>

#include <Xlib.h>
#include <Xutil.h>


#define MAPW	100
#define MAPH	100
#define BDWIDTH	10
#define LONGW	11
#define LONGH	10

void loop ();
double bif_time ();

Display	*dpy;
Window	win;
Pixmap	pmap, bdpmap, longpmap, d1pmap;
GC	gc, d1gc;
Colormap	cmap;
XColor	colors[256];
unsigned	line1, line2, line3, line4, line5, line6;

int	planemask;
int	depth;
int	class;
int	nocmap;

int		gx = 0, gy = 0;
unsigned int	gw = 128, gh = 128;


main (argc, argv)
     int	argc;
     char	*argv[];
{
    int	i;

    depth = 8;
    planemask = 0xff;
    class = TrueColor;
    cmap = 0;
    for (i = 1; i<argc; i++ ) {
	if ((strncmp(argv[i],"-24",strlen(argv[i]))) == 0) {
	    planemask = 0xffffff;
	    depth = 24;
	}
	if ((strncmp(argv[i],"-direct",strlen(argv[i]))) == 0) {
	    class = DirectColor;
	}
	if ((strncmp(argv[i],"-nocmap",strlen(argv[i]))) == 0) {
	    nocmap = 1;
	}
	if ((strncmp(argv[i],"-geom",strlen(argv[i]))) == 0) {
	    XParseGeometry (argv[i+1], &gx, &gy, &gw, &gh);
	}
    }

    if (openWindow ())
	exit (1);

    loop ();

    XDestroyWindow (dpy, win);
    XCloseDisplay (dpy);

    return (0);
}



int openWindow ()
{
    XSizeHints xsh;
    XSetWindowAttributes	xswa;
    XVisualInfo	vi, *viret;
    int	nitems, i;
    XGCValues	gcvals;
    Colormap	*imaps;


    if (!(dpy = XOpenDisplay (NULL))) {
	fprintf (stderr, "openWindow: Can't open display.\n");
	return (1);
    }
    XSynchronize (dpy, 1);


    if (depth == 24) {
	vi.depth = 24;
	vi.class = class;
    }
    else {
	vi.depth = 8;
	vi.class = PseudoColor;
    }
	if (viret = XGetVisualInfo (dpy, VisualDepthMask | VisualClassMask,
				    &vi, &nitems)) {
		printf ("using depth %d\n", viret[0].depth);
	}
	else
	  printf ("No visuals of depth %d\n", vi.depth);

    if (depth == 24) {
	line1 = 0xf1ff80;
	line2 = 0xdf83f2;
	line3 = 0x4040f3;
	line4 = 0xdf6600;
	line5 = 0x0;
	line6 = 0x00ff00;

	if (class == TrueColor)
	    cmap = XCreateColormap (dpy, RootWindow (dpy, 0),
				    viret[0].visual, AllocNone);

	else {
	    cmap = XCreateColormap (dpy, RootWindow (dpy, 0),
				    viret[0].visual, AllocAll);

	    colors[0].pixel = line1;
	    colors[0].red = 0x8000;
	    colors[0].green = 0xff00;
	    colors[0].blue = 0xf100;
	    colors[0].flags = DoRed | DoGreen | DoBlue;
	    colors[1].pixel = line2;
	    colors[1].red = 0xf200;
	    colors[1].green = 0x8300;
	    colors[1].blue = 0xdf00;
	    colors[1].flags = DoRed | DoGreen | DoBlue;
	    colors[2].pixel = line3;
	    colors[2].red = 0xf300;
	    colors[2].green = 0x4000;
	    colors[2].blue = 0x4000;
	    colors[2].flags = DoRed | DoGreen | DoBlue;
	    colors[3].pixel = line4;
	    colors[3].red = 0x0000;
	    colors[3].green = 0x6600;
	    colors[3].blue = 0xdf00;
	    colors[3].flags = DoRed | DoGreen | DoBlue;
	    colors[4].pixel = line5;
	    colors[4].red = 0x0;
	    colors[4].green = 0x0;
	    colors[4].blue = 0x0;
	    colors[4].flags = DoRed | DoGreen | DoBlue;
	    colors[5].pixel = line6;
	    colors[5].red = 0x0;
	    colors[5].green = 0xff00;
	    colors[5].blue = 0x0;
	    colors[5].flags = DoRed | DoGreen | DoBlue;

	    XStoreColors(dpy, cmap, colors, 6);
	}
    }
    else {
	if (!nocmap) {
	cmap = XCreateColormap (dpy, RootWindow (dpy, 0),
				viret[0].visual, AllocAll);

	line1 = 2;
	line2 = 3;
	line3 = 4;
	line4 = 5;
	line5 = 6;
	line6 = 7;

	colors[0].pixel = line1;
	colors[0].red = 0x8000;
	colors[0].green = 0xff00;
	colors[0].blue = 0xf100;
	colors[0].flags = DoRed | DoGreen | DoBlue;
	colors[1].pixel = line2;
	colors[1].red = 0xf200;
	colors[1].green = 0x8300;
	colors[1].blue = 0xdf00;
	colors[1].flags = DoRed | DoGreen | DoBlue;
	colors[2].pixel = line3;
	colors[2].red = 0xf300;
	colors[2].green = 0x4000;
	colors[2].blue = 0x4000;
	colors[2].flags = DoRed | DoGreen | DoBlue;
	colors[3].pixel = line4;
	colors[3].red = 0x0000;
	colors[3].green = 0x6600;
	colors[3].blue = 0xdf00;
	colors[3].flags = DoRed | DoGreen | DoBlue;
	colors[4].pixel = line5;
	colors[4].red = 0x0;
	colors[4].green = 0x0;
	colors[4].blue = 0x0;
	colors[4].flags = DoRed | DoGreen | DoBlue;
	colors[5].pixel = line6;
	colors[5].red = 0x0;
	colors[5].green = 0xff00;
	colors[5].blue = 0x0;
	colors[5].flags = DoRed | DoGreen | DoBlue;

	XStoreColors(dpy, cmap, colors, 6);
    }
	else {

	cmap = XCreateColormap (dpy, RootWindow (dpy, 0),
				viret[0].visual, AllocAll);

	line1 = 2;
	line2 = 3;
	line3 = 4;
	line4 = 5;
	line5 = 6;
	line6 = 7;

	colors[0].pixel = line4;
	colors[0].red = 0xff00;
	colors[0].green = 0x0000;
	colors[0].blue = 0x0000;

	XStoreColors(dpy, cmap, colors, 1);
    }
    }
    XInstallColormap (dpy, cmap);

    xsh.flags  = (PPosition | PSize);
    xsh.width = gw;
    xsh.height = gh;
    xsh.x = gx;
    xsh.y = gy;
    xswa.background_pixel = BlackPixel (dpy, DefaultScreen (dpy));
    xswa.border_pixel = line4;
    xswa.colormap = cmap;
    win = XCreateWindow (dpy, RootWindow (dpy, DefaultScreen(dpy)),
			 xsh.x, xsh.y, xsh.width, xsh.height, BDWIDTH,
			 vi.depth, InputOutput,
			 viret[0].visual,
			 CWBackPixel | CWColormap | CWBorderPixel,
			 &xswa);
    XSetStandardProperties (dpy, win, "Test", "test", None, NULL, 0,
			    &xsh);


    pmap = XCreatePixmap (dpy, win, MAPW, MAPH, vi.depth);
    d1pmap = XCreatePixmap (dpy, win, MAPW, MAPH, 1);
    bdpmap = XCreatePixmap (dpy, win, BDWIDTH, BDWIDTH, vi.depth);
    longpmap = XCreatePixmap (dpy, win, LONGW, LONGH, vi.depth);

    XSelectInput (dpy, win, ButtonPressMask | KeyPressMask | ExposureMask);

    gcvals.plane_mask = planemask;
    gc = XCreateGC (dpy, win, GCPlaneMask, &gcvals);
    d1gc = XCreateGC (dpy, d1pmap, 0, &gcvals);

    XMapWindow (dpy, win);

    XFree (viret);
    return (0);
}



void loop ()
{
    XEvent	event;
    int	i;


    while (1) {
	XNextEvent (dpy, &event);
	if (event.type == ButtonPress) {
	    return;
	}
	else if ((event.type == KeyPress) ||
		 (event.type == Expose)) {
	    do_stuff ();
	}
    }
}



do_stuff (bp)
    unsigned	bp;
{
    XPoint	pts[20];
    XRectangle	rects[2];
    XImage	*fooimage;
    double	startTime, endTime, theTime;
    XWindowAttributes	attrs;
    int	i;


    XClearWindow (dpy, win);

    XSetForeground (dpy, gc, 0xffffff);
    for (i=0; i<20; i++) {
	pts[i].y = 20;
	pts[i].x = i * 11;
    }
    XDrawPoints (dpy, win, gc, pts, 20, CoordModeOrigin);

    printf ("window refresh complete\n");
}