list.c revision c2535118
13e747e6dSmrg/*****************************************************************************/
23e747e6dSmrg/*
33e747e6dSmrg
43e747e6dSmrgCopyright 1989, 1998  The Open Group
53e747e6dSmrg
63e747e6dSmrgPermission to use, copy, modify, distribute, and sell this software and its
73e747e6dSmrgdocumentation for any purpose is hereby granted without fee, provided that
83e747e6dSmrgthe above copyright notice appear in all copies and that both that
93e747e6dSmrgcopyright notice and this permission notice appear in supporting
103e747e6dSmrgdocumentation.
113e747e6dSmrg
123e747e6dSmrgThe above copyright notice and this permission notice shall be included in
133e747e6dSmrgall copies or substantial portions of the Software.
143e747e6dSmrg
153e747e6dSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
163e747e6dSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
173e747e6dSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
183e747e6dSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
193e747e6dSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
203e747e6dSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
213e747e6dSmrg
223e747e6dSmrgExcept as contained in this notice, the name of The Open Group shall not be
233e747e6dSmrgused in advertising or otherwise to promote the sale, use or other dealings
243e747e6dSmrgin this Software without prior written authorization from The Open Group.
253e747e6dSmrg
263e747e6dSmrg*/
273e747e6dSmrg/**       Copyright 1988 by Evans & Sutherland Computer Corporation,        **/
283e747e6dSmrg/**                          Salt Lake City, Utah                           **/
293e747e6dSmrg/**                        Cambridge, Massachusetts                         **/
303e747e6dSmrg/**                                                                         **/
313e747e6dSmrg/**                           All Rights Reserved                           **/
323e747e6dSmrg/**                                                                         **/
333e747e6dSmrg/**    Permission to use, copy, modify, and distribute this software and    **/
343e747e6dSmrg/**    its documentation  for  any  purpose  and  without  fee is hereby    **/
353e747e6dSmrg/**    granted, provided that the above copyright notice appear  in  all    **/
363e747e6dSmrg/**    copies and that both  that  copyright  notice  and  this  permis-    **/
373e747e6dSmrg/**    sion  notice appear in supporting  documentation,  and  that  the    **/
383e747e6dSmrg/**    name of Evans & Sutherland not be used in advertising    **/
393e747e6dSmrg/**    in publicity pertaining to distribution of the  software  without    **/
403e747e6dSmrg/**    specific, written prior permission.                                  **/
413e747e6dSmrg/**                                                                         **/
423e747e6dSmrg/**    EVANS & SUTHERLAND DISCLAIMs ALL WARRANTIES WITH REGARD    **/
433e747e6dSmrg/**    TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES  OF  MERCHANT-    **/
443e747e6dSmrg/**    ABILITY  AND  FITNESS,  IN  NO  EVENT SHALL EVANS & SUTHERLAND    **/
453e747e6dSmrg/**    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL  DAM-    **/
463e747e6dSmrg/**    AGES OR  ANY DAMAGES WHATSOEVER  RESULTING FROM LOSS OF USE, DATA    **/
473e747e6dSmrg/**    OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER    **/
483e747e6dSmrg/**    TORTIOUS ACTION, ARISING OUT OF OR IN  CONNECTION  WITH  THE  USE    **/
493e747e6dSmrg/**    OR PERFORMANCE OF THIS SOFTWARE.                                     **/
503e747e6dSmrg/*****************************************************************************/
513e747e6dSmrg
523e747e6dSmrg
533e747e6dSmrg/**********************************************************************
543e747e6dSmrg *
553e747e6dSmrg * TWM code to deal with the name lists for the NoTitle list and
563e747e6dSmrg * the AutoRaise list
573e747e6dSmrg *
583e747e6dSmrg * 11-Apr-88 Tom LaStrange        Initial Version.
593e747e6dSmrg *
603e747e6dSmrg **********************************************************************/
613e747e6dSmrg
623e747e6dSmrg#include <stdio.h>
633e747e6dSmrg#include "twm.h"
643e747e6dSmrg#include "screen.h"
653e747e6dSmrg#include "gram.h"
663e747e6dSmrg#include "util.h"
673e747e6dSmrg
683e747e6dSmrgstruct name_list_struct
693e747e6dSmrg{
703e747e6dSmrg    name_list *next;    /**< pointer to the next name */
713e747e6dSmrg    char *name;         /**< the name of the window */
723e747e6dSmrg    char *ptr;          /**< list dependent data */
733e747e6dSmrg};
743e747e6dSmrg
753e747e6dSmrg/**
763e747e6dSmrg * add a window name to the appropriate list.
773e747e6dSmrg *
78ffd25bcaSmrg *	If the list does not use the ptr value, a non-null value
793e747e6dSmrg *	should be placed in it.  LookInList returns this ptr value
80ffd25bcaSmrg *	and procedures calling LookInList will check for a non-null
813e747e6dSmrg *	return value as an indication of success.
823e747e6dSmrg *
833e747e6dSmrg *  \param list  the address of the pointer to the head of a list
84ffd25bcaSmrg *  \param name  a pointer to the name of the window
853e747e6dSmrg *  \param ptr   pointer to list dependent data
863e747e6dSmrg */
873e747e6dSmrgvoid
883e747e6dSmrgAddToList(name_list **list_head, char *name, char *ptr)
893e747e6dSmrg{
903e747e6dSmrg    name_list *nptr;
913e747e6dSmrg
923e747e6dSmrg    if (!list_head) return;	/* ignore empty inserts */
933e747e6dSmrg
94c2535118Smrg    nptr = malloc(sizeof(name_list));
953e747e6dSmrg    if (nptr == NULL)
963e747e6dSmrg    {
973e747e6dSmrg	twmrc_error_prefix();
983e747e6dSmrg	fprintf (stderr, "unable to allocate %ld bytes for name_list\n",
993e747e6dSmrg		 (unsigned long)sizeof(name_list));
1003e747e6dSmrg	Done(NULL, NULL);
1013e747e6dSmrg    }
1023e747e6dSmrg
1033e747e6dSmrg    nptr->next = *list_head;
1043e747e6dSmrg    nptr->name = name;
1053e747e6dSmrg    nptr->ptr = (ptr == NULL) ? (char *)TRUE : ptr;
1063e747e6dSmrg    *list_head = nptr;
107ffd25bcaSmrg}
1083e747e6dSmrg
1093e747e6dSmrg/**
1103e747e6dSmrg * look through a list for a window name, or class
1113e747e6dSmrg *
112ffd25bcaSmrg *  \return the ptr field of the list structure or NULL if the name
1133e747e6dSmrg *	or class was not found in the list
1143e747e6dSmrg *
1153e747e6dSmrg *	\param list   a pointer to the head of a list
1163e747e6dSmrg *	\param name   a pointer to the name to look for
1173e747e6dSmrg *  \param class  a pointer to the class to look for
1183e747e6dSmrg */
1193e747e6dSmrgchar *
1203e747e6dSmrgLookInList(name_list *list_head, char *name, XClassHint *class)
1213e747e6dSmrg{
1223e747e6dSmrg    name_list *nptr;
1233e747e6dSmrg
1243e747e6dSmrg    /* look for the name first */
1253e747e6dSmrg    for (nptr = list_head; nptr != NULL; nptr = nptr->next)
1263e747e6dSmrg	if (strcmp(name, nptr->name) == 0)
1273e747e6dSmrg	    return (nptr->ptr);
1283e747e6dSmrg
1293e747e6dSmrg    if (class)
1303e747e6dSmrg    {
1313e747e6dSmrg	/* look for the res_name next */
1323e747e6dSmrg	for (nptr = list_head; nptr != NULL; nptr = nptr->next)
1333e747e6dSmrg	    if (strcmp(class->res_name, nptr->name) == 0)
1343e747e6dSmrg		return (nptr->ptr);
1353e747e6dSmrg
1363e747e6dSmrg	/* finally look for the res_class */
1373e747e6dSmrg	for (nptr = list_head; nptr != NULL; nptr = nptr->next)
1383e747e6dSmrg	    if (strcmp(class->res_class, nptr->name) == 0)
1393e747e6dSmrg		return (nptr->ptr);
1403e747e6dSmrg    }
1413e747e6dSmrg    return (NULL);
1423e747e6dSmrg}
1433e747e6dSmrg
1443e747e6dSmrgchar *
1453e747e6dSmrgLookInNameList(name_list *list_head, char *name)
1463e747e6dSmrg{
1473e747e6dSmrg    return (LookInList(list_head, name, NULL));
1483e747e6dSmrg}
1493e747e6dSmrg
1503e747e6dSmrg/**
1513e747e6dSmrg * look through a list for a window name, or class
1523e747e6dSmrg *
1533e747e6dSmrg *  \return TRUE  if the name was found
1543e747e6dSmrg *  \return FALSE if the name was not found
1553e747e6dSmrg *
1563e747e6dSmrg *  \param      list  a pointer to the head of a list
1573e747e6dSmrg *  \param      name  a pointer to the name to look for
1583e747e6dSmrg *  \param      class a pointer to the class to look for
1593e747e6dSmrg *	\param[out] ptr   fill in the list value if the name was found
1603e747e6dSmrg */
161ffd25bcaSmrgint GetColorFromList(name_list *list_head, char *name, XClassHint *class,
1623e747e6dSmrg                     Pixel *ptr)
1633e747e6dSmrg{
1643e747e6dSmrg    int save;
1653e747e6dSmrg    name_list *nptr;
1663e747e6dSmrg
1673e747e6dSmrg    for (nptr = list_head; nptr != NULL; nptr = nptr->next)
1683e747e6dSmrg	if (strcmp(name, nptr->name) == 0)
1693e747e6dSmrg	{
1703e747e6dSmrg	    save = Scr->FirstTime;
1713e747e6dSmrg	    Scr->FirstTime = TRUE;
1723e747e6dSmrg	    GetColor(Scr->Monochrome, ptr, nptr->ptr);
1733e747e6dSmrg	    Scr->FirstTime = save;
1743e747e6dSmrg	    return (TRUE);
1753e747e6dSmrg	}
1763e747e6dSmrg
1773e747e6dSmrg    if (class)
1783e747e6dSmrg    {
1793e747e6dSmrg	for (nptr = list_head; nptr != NULL; nptr = nptr->next)
1803e747e6dSmrg	    if (strcmp(class->res_name, nptr->name) == 0)
1813e747e6dSmrg	    {
1823e747e6dSmrg		save = Scr->FirstTime;
1833e747e6dSmrg		Scr->FirstTime = TRUE;
1843e747e6dSmrg		GetColor(Scr->Monochrome, ptr, nptr->ptr);
1853e747e6dSmrg		Scr->FirstTime = save;
1863e747e6dSmrg		return (TRUE);
1873e747e6dSmrg	    }
1883e747e6dSmrg
1893e747e6dSmrg	for (nptr = list_head; nptr != NULL; nptr = nptr->next)
1903e747e6dSmrg	    if (strcmp(class->res_class, nptr->name) == 0)
1913e747e6dSmrg	    {
1923e747e6dSmrg		save = Scr->FirstTime;
1933e747e6dSmrg		Scr->FirstTime = TRUE;
1943e747e6dSmrg		GetColor(Scr->Monochrome, ptr, nptr->ptr);
1953e747e6dSmrg		Scr->FirstTime = save;
1963e747e6dSmrg		return (TRUE);
1973e747e6dSmrg	    }
1983e747e6dSmrg    }
1993e747e6dSmrg    return (FALSE);
2003e747e6dSmrg}
2013e747e6dSmrg
2023e747e6dSmrg/**
2033e747e6dSmrg * free up a list
2043e747e6dSmrg */
2053e747e6dSmrgvoid FreeList(name_list **list)
2063e747e6dSmrg{
2073e747e6dSmrg    name_list *nptr;
2083e747e6dSmrg    name_list *tmp;
2093e747e6dSmrg
2103e747e6dSmrg    for (nptr = *list; nptr != NULL; )
2113e747e6dSmrg    {
2123e747e6dSmrg	tmp = nptr->next;
213c2535118Smrg	free(nptr);
2143e747e6dSmrg	nptr = tmp;
2153e747e6dSmrg    }
2163e747e6dSmrg    *list = NULL;
2173e747e6dSmrg}
218