[comp.sys.apollo] xlcm: X client to fix up colormap

dawson@apollo.HP.COM (Keith Dawson) (11/20/89)

Here is source and a help file for xlcm, a simple X client that straightens
out the colormap (insofar as the X server knows it) after it has been dis-
rupted by some uncooperative program.
  
-->Keith
____________________________________________________________   
Keith Dawson  Hewlett Packard Co.         508-256-0176 x5739   
              Graphics Technology Division / East  
              300 Apollo Dr.  (CHR.03.DE)
              Chelmsford, MA  01824                      USA   
              Internet: dawson@apollo.hp.com                   
              UUCP: {mit-eddie,yale,uw-beaver}!apollo!dawson   
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	xlcm.help
#	xlcm.c
# This archive created: Fri Nov 17 08:05:01 1989
export PATH; PATH=/bin:/usr/bin:$PATH
if test -f 'xlcm.help'
then
	echo shar: "will not over-write existing file 'xlcm.help'"
else
cat << \SHAR_EOF > 'xlcm.help'

xlcm is a client program for the X Window System that repairs the colors that
X has allocated after some uncooperative program has overrwritten them. 

Background:
-----------
The X server cooperates with the Display Manager (via the use of calls to the
Color-Table Manager, CTM) in allocating and using slots in the default color
table. Thus, any colors allocated on behalf of requesting X clients will stay
out of the way of colors allocated by the DM.

Some applications (including common 3rd-party packages) are less polite: they
change the values of particular slots in the hardware color table without
consulting anyone else. The utility /usr/apollo/bin/lcm acts this way, and
therefore can be useful for reloading the DM's initial colors after some other
application has trashed them. When used without options, lcm restores the low-
order 16 colors in the colormap to the DM's default values.

xlcm provides a similar service for colors that X has allocated. It restores
up to 5 client colors plus 2 cursor colors on a 4-plane display, and up to 243
plus 2 on an 8-plane display.

Building xlcm:
--------------
From a C-shell, type "make xlcm" in the directory where you have placed xlcm.c.

Usage:
------
To restore both DM and X colors, first run /usr/apollo/bi/lcm, then run xlcm.

  xlcm [-options ...]

  where options are:
      -help                print this message
      -display host:dpy    X server to contact
      -default             use default colormap (default)

Details:
--------
The InstallColormap routines in Xapollo are careful not to overwrite slots X
doesn't own. They don't do anything, however, if the colormap is already
installed. 

xlcm uninstalls the selected colormap, then re-installs it. The uninstall is
accomplished by first trying XUninstallColormap. But this won't usually work
in the case of the default colormap. If it doesn't work, dummy colormaps are
created and installed, flushing out the default map. No entries are allocated
in the dummy maps, so no colors are changed when they are installed. After the
default map has finally been flushed out, it is re-installed, and the dummy
maps are freed.

Example:
--------
To see the effects of xlcm, try the following example in a C-shell. (It's in-
formative first to run /systest/ssr_util/color_probe in another window.)

    % ico -faces -colors red green blue &     # displays bouncing icosohedron
    % foreach i ( 16 17 18 19 )
    ? /usr/apollo/bin/lcm -s $i 0 0 0
    ? end                                     # turns all ico colors to black
    % xlcm                                    # restores ico colors
SHAR_EOF
chmod +x 'xlcm.help'
fi
if test -f 'xlcm.c'
then
	echo shar: "will not over-write existing file 'xlcm.c'"
else
cat << \SHAR_EOF > 'xlcm.c'
/******************************************************************
Copyright 1989 by Apollo Computer Inc., Chelmsford, Massachusetts.
Copyright 1989 by Hewlett-Packard Company.

                        All Rights Reserved

Permission to use, duplicate, change, and distribute this software and
its documentation for any purpose and without fee is granted, provided
that the above copyright notice appear in such copy and that this
copyright notice appear in all supporting documentation, and that the
names of Apollo Computer Inc. or the Hewlett-Packard Company not
be used in advertising or publicity pertaining to distribution of the
software without written prior permission.

HEWLETT-PACKARD MAKES NO WARRANTY OF ANY KIND WITH REGARD
TO THIS SOFTWARE, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE.  Hewlett-Packard shall not be liable for errors
contained herein or direct, indirect, special, incidental or
consequential damages in connection with the furnishing,
performance, or use of this material.

This software is not subject to any license of the American
Telephone and Telegraph Company or of the Regents of the
University of California.
******************************************************************/

/*
 * xlcm.c	force colormap reload
 *
 *  Author:	Joe Bowbeer
 *		16-Oct-89
 */

#include <stdio.h>
#include <X11/Xlib.h>

char *program_name;

void
usage()
{
    fprintf (stderr,
	"usage:  %s [-options ...]\n\n", program_name);
    fprintf (stderr,
	"where options include:\n");
    fprintf (stderr,
	"    -help                print this message\n");
    fprintf (stderr,
	"    -display host:dpy    X server to contact\n");
    fprintf (stderr,
	"    -default             use default colormap (default)\n");
    exit (1);
    /*NOTREACHED*/
}

/*VARARGS1*/
void
failure(f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9) /* limit of ten args */
    char *f;
    char *s0, *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8, *s9;
{
    fprintf(stderr, "%s:  ", program_name);
    fprintf(stderr, f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9);
    exit(1);
    /*NOTREACHED*/
}

Bool
installed(dpy, wid, cmap)
    Display	*dpy;
    Window	wid;
    Colormap	cmap;
{
    int	num, i;
    Colormap *clist, *cptr;
    Bool result = False;

    clist = XListInstalledColormaps(dpy, wid, &num);
    for (cptr = clist, i = num; i; --i, cptr++)
        if (cmap == *cptr) {
            result = True;
            break;
        }

    XFree(clist);
    return(result);
}

main(argc, argv) 
    int argc;
    char **argv;
{
    int i;
    char *display_name = NULL;
    Display	*dpy;
    Screen      *screen;
    Window	wid;
    Colormap	cmap;

    program_name=argv[0];

    for (i = 1; i < argc; i++) {
	if (!strcmp("-display", argv[i])) {
	    if (++i>=argc) usage ();
	    display_name = argv[i];
	    continue;
	}
	if (!strcmp("-default", argv[i])) {
            /* "default" is the default and the only option */
	    continue;
	}
	usage();
    } 

    /* open connection to X server */
    if ( !(dpy = XOpenDisplay(display_name)) )
        failure("cannot open display '%s'\n", display_name);

    /* select window and colormap */
    wid = DefaultRootWindow(dpy);
    screen = DefaultScreenOfDisplay(dpy);
    cmap = DefaultColormapOfScreen(screen);

    /* uninstall selected colormap */
    XUninstallColormap(dpy, cmap);

    /* if that didn't work, create and install a(nother) dummy colormap */
    for (i = 0; i < MaxCmapsOfScreen(screen); i++) {
        if (!installed(dpy, wid, cmap)) break;
        XInstallColormap(dpy,
            XCreateColormap(dpy, wid, DefaultVisualOfScreen(screen), False));
    }

    /* re-install selected colormap */
    XInstallColormap(dpy, cmap);
    XFlush(dpy);

    exit(0);
}

SHAR_EOF
chmod +x 'xlcm.c'
fi
exit 0
#	End of shell archive