[comp.windows.x] Blinking Colors Anyone?

amccutchen@nasamail.nasa.GOV (ALAN MCCUTCHEN) (03/06/91)

PROBLEM:
The following code was written to produce an X-based software color blink.
The code makes a copy of the default colormap, installs it, and then
periodically blinks specific cells in the colormap. The main obstacle is
X's restrictions against having cells be writable *and* sharable.
The colors selected for blinking in this example code are named
"blinkRed", "blinkBlue", "blink...", and were manually added as named X colors.

GOTCHA:
This works most of the time, but seems to depend upon whether or not
the applications wishing to share the blinking colors start up before or
after the blinking begins. The colormap changes/rearranges based upon this,
but looking at the code here, I didn't think it should.

QUESTION:
Does anybody have any insights into this problem/solution?!?
Can this even be done successfully using X?!?


V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~ CUT HERE V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~
#include <X11/Intrinsic.h>
#include <X11/Xatom.h>


struct BlinkColorStruct {
        char    Name[20];
        XColor  Def;
};
XColor  BackgroundColorDef;

/*
** Here is the list of named colors which should be blinked.
** Also, the "blink-off" color is defined to be black.
*/
struct BlinkColorStruct BlinkColors[] = {
        { "blinkRed" },
        { "blinkGreen" },
        { "blinkBlue" },
        { "blinkYellow" },
        { "blinkCyan" },
        { "blinkMagenta" },
        { "blinkWhite" },
        { "blinkBlack" }
        };
#define BACKGROUND_COLOR_NAME   "black"
#define BLINK_DURATION_MS       500


void            blink_color();
Colormap        cmap, new_cmap;
XStandardColormap std_cmap;
Display         *BlinkDisplay;
XtAppContext    AppCtx;
#define         MAX_COLORS      256
#define         NUM_BLINK_COLORS  \
                        (sizeof(BlinkColors)/sizeof(struct BlinkColorStruct))


/*
** Call this routine to start up blinking colors.
*/
InstallBlinkCMAP(dpy, ctx)
        Display *dpy;
        XtAppContext ctx;
{
        XColor          exact, color_list[MAX_COLORS];
        int             scr, i, ncolors;
        Arg             al[10];
        register int    ac;


        AppCtx = ctx;
        BlinkDisplay = dpy;
        scr =  DefaultScreen(BlinkDisplay);
        cmap = DefaultColormap(BlinkDisplay, scr);


        /*
        ** Given list of color names to blink, get their XColor structures,
        ** as well as the XColor struct for the background color.
        ** NOTE that the *original* copy of the default colormap is
        ** used for the AllocNamedColor calls.
        ** Also, set flags for future StreColor calls.
        */
        for (i=0; i<NUM_BLINK_COLORS; i++)
                {
                BlinkColors[i].Def.pixel = exact.pixel = 0;
                XAllocNamedColor(BlinkDisplay, cmap, BlinkColors[i].Name,
                                 &BlinkColors[i].Def, &exact);
                BlinkColors[i].Def.flags = DoRed | DoGreen | DoBlue;
                }
        BackgroundColorDef.pixel = exact.pixel = 0;
        XAllocNamedColor(BlinkDisplay, cmap, BACKGROUND_COLOR_NAME,
                         &BackgroundColorDef, &exact);
        BackgroundColorDef.flags = DoRed | DoGreen | DoBlue;


        /*
        ** copy default colormap with all cells allocated.
        */
        ncolors = DisplayCells(BlinkDisplay, scr);
        for (i=0; i<ncolors; i++)

                {
                color_list[i].pixel = i;
                color_list[i].flags = DoRed | DoGreen | DoBlue;
                }
        XQueryColors(BlinkDisplay, cmap, color_list, ncolors);
        for (i=0; i<ncolors; i++)
                color_list[i].pixel = i;
        new_cmap = XCreateColormap( BlinkDisplay,
                        DefaultRootWindow(BlinkDisplay),
                        DefaultVisual(BlinkDisplay, scr), AllocAll);
        XStoreColors(BlinkDisplay, new_cmap, color_list, ncolors);



        /*
        ** install the colormap.
        */
        XInstallColormap(BlinkDisplay, new_cmap);


        XtAppAddTimeOut(AppCtx, (long)BLINK_DURATION_MS,
                        blink_color, NULL);
}


void blink_color(w, client_data, call_data)
        Widget w;
        XtPointer client_data;
        XtPointer call_data;
{
        static int      blink_is_on = False;
        XColor          color_list[NUM_BLINK_COLORS];
        int             i;
        long            next_duration = (long)BLINK_DURATION_MS;

        if (blink_is_on)
                {
                for (i=0; i<NUM_BLINK_COLORS; i++)
                        {
                        color_list[i] = BackgroundColorDef;
                        color_list[i].pixel = BlinkColors[i].Def.pixel;
                        }
                next_duration /= 2.0;
                }
        else
                {
                for (i=0; i<NUM_BLINK_COLORS; i++)
                        {
                        color_list[i] = BlinkColors[i].Def;
                        }
                }
        XStoreColors(BlinkDisplay, new_cmap, color_list, NUM_BLINK_COLORS);
        blink_is_on = (blink_is_on == False) ? True : False;
        XtAppAddTimeOut(AppCtx, next_duration,
                        blink_color, BlinkDisplay);
}

V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~ CUT HERE V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~


-------------------------------------------------------------------------------

Alan McCutchen                                  amccutchen@nasamail.nasa.gov
Harris Space Systems Corporation                407-633-3835  FAX 407-633-3900

    "No hunt beware open and close no credit"   - Ernest T. Bass

-------------------------------------------------------------------------------