DisplayQue.c revision 6c321187
16c321187Smrg/* $Xorg: DisplayQue.c,v 1.4 2001/02/09 02:03:52 xorgcvs Exp $ */
26c321187Smrg
36c321187Smrg/*
46c321187Smrg
56c321187SmrgCopyright 1989, 1998  The Open Group
66c321187Smrg
76c321187SmrgPermission to use, copy, modify, distribute, and sell this software and its
86c321187Smrgdocumentation for any purpose is hereby granted without fee, provided that
96c321187Smrgthe above copyright notice appear in all copies and that both that
106c321187Smrgcopyright notice and this permission notice appear in supporting
116c321187Smrgdocumentation.
126c321187Smrg
136c321187SmrgThe above copyright notice and this permission notice shall be included in
146c321187Smrgall copies or substantial portions of the Software.
156c321187Smrg
166c321187SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
176c321187SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
186c321187SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
196c321187SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
206c321187SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
216c321187SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
226c321187Smrg
236c321187SmrgExcept as contained in this notice, the name of The Open Group shall not be
246c321187Smrgused in advertising or otherwise to promote the sale, use or other dealings
256c321187Smrgin this Software without prior written authorization from The Open Group.
266c321187Smrg
276c321187Smrg*/
286c321187Smrg/* $XFree86: xc/lib/Xmu/DisplayQue.c,v 3.4 2001/07/25 15:04:50 dawes Exp $ */
296c321187Smrg
306c321187Smrg/*
316c321187Smrg * Author:  Jim Fulton, MIT X Consortium
326c321187Smrg */
336c321187Smrg
346c321187Smrg#ifdef HAVE_CONFIG_H
356c321187Smrg#include <config.h>
366c321187Smrg#endif
376c321187Smrg#include <stdio.h>
386c321187Smrg#include <X11/Xlib.h>
396c321187Smrg#include <stdlib.h>
406c321187Smrg#include <X11/Xmu/DisplayQue.h>
416c321187Smrg
426c321187Smrg/*
436c321187Smrg * Prototypes
446c321187Smrg */
456c321187Smrgstatic int _DQCloseDisplay(Display*, XPointer);
466c321187Smrg
476c321187Smrg#define CallCloseCallback(q,e) (void) (*((q)->closefunc)) ((q), (e))
486c321187Smrg#define CallFreeCallback(q) (void) (*((q)->freefunc)) ((q))
496c321187Smrg
506c321187Smrg/*
516c321187Smrg * XmuDQCreate - create a display queue
526c321187Smrg */
536c321187SmrgXmuDisplayQueue *
546c321187SmrgXmuDQCreate(XmuCloseDisplayQueueProc closefunc,
556c321187Smrg	    XmuFreeDisplayQueueProc freefunc,
566c321187Smrg	    XPointer data)
576c321187Smrg{
586c321187Smrg    XmuDisplayQueue *q = (XmuDisplayQueue *) malloc (sizeof (XmuDisplayQueue));
596c321187Smrg    if (q) {
606c321187Smrg	q->nentries = 0;
616c321187Smrg	q->head = q->tail = NULL;
626c321187Smrg	q->closefunc = closefunc;
636c321187Smrg	q->freefunc = freefunc;
646c321187Smrg	q->data = data;
656c321187Smrg    }
666c321187Smrg    return q;
676c321187Smrg}
686c321187Smrg
696c321187Smrg
706c321187Smrg/*
716c321187Smrg * XmuDQDestroy - free all storage associated with this display queue,
726c321187Smrg * optionally invoking the close callbacks.
736c321187Smrg */
746c321187Smrg
756c321187SmrgBool
766c321187SmrgXmuDQDestroy(XmuDisplayQueue *q, Bool docallbacks)
776c321187Smrg{
786c321187Smrg    XmuDisplayQueueEntry *e = q->head;
796c321187Smrg
806c321187Smrg    while (e) {
816c321187Smrg	XmuDisplayQueueEntry *nexte = e->next;
826c321187Smrg	if (docallbacks && q->closefunc) CallCloseCallback (q, e);
836c321187Smrg	free ((char *) e);
846c321187Smrg	e = nexte;
856c321187Smrg    }
866c321187Smrg    free ((char *) q);
876c321187Smrg    return True;
886c321187Smrg}
896c321187Smrg
906c321187Smrg
916c321187Smrg/*
926c321187Smrg * XmuDQLookupDisplay - finds the indicated display on the given queue
936c321187Smrg */
946c321187SmrgXmuDisplayQueueEntry *
956c321187SmrgXmuDQLookupDisplay(XmuDisplayQueue *q, Display *dpy)
966c321187Smrg{
976c321187Smrg    XmuDisplayQueueEntry *e;
986c321187Smrg
996c321187Smrg    for (e = q->head; e; e = e->next) {
1006c321187Smrg	if (e->display == dpy) return e;
1016c321187Smrg    }
1026c321187Smrg    return NULL;
1036c321187Smrg}
1046c321187Smrg
1056c321187Smrg
1066c321187Smrg/*
1076c321187Smrg * XmuDQAddDisplay - add the specified display to the queue; set data as a
1086c321187Smrg * convenience.  Does not ensure that dpy hasn't already been added.
1096c321187Smrg */
1106c321187SmrgXmuDisplayQueueEntry *
1116c321187SmrgXmuDQAddDisplay(XmuDisplayQueue *q, Display *dpy, XPointer data)
1126c321187Smrg{
1136c321187Smrg    XmuDisplayQueueEntry *e;
1146c321187Smrg
1156c321187Smrg    if (!(e = (XmuDisplayQueueEntry *) malloc (sizeof (XmuDisplayQueueEntry)))) {
1166c321187Smrg	return NULL;
1176c321187Smrg    }
1186c321187Smrg    if (!(e->closehook = XmuAddCloseDisplayHook (dpy, _DQCloseDisplay,
1196c321187Smrg						 (XPointer) q))) {
1206c321187Smrg	free ((char *) e);
1216c321187Smrg	return NULL;
1226c321187Smrg    }
1236c321187Smrg
1246c321187Smrg    e->display = dpy;
1256c321187Smrg    e->next = NULL;
1266c321187Smrg    e->data = data;
1276c321187Smrg
1286c321187Smrg    if (q->tail) {
1296c321187Smrg	q->tail->next = e;
1306c321187Smrg	e->prev = q->tail;
1316c321187Smrg    } else {
1326c321187Smrg	q->head = e;
1336c321187Smrg	e->prev = NULL;
1346c321187Smrg    }
1356c321187Smrg    q->tail = e;
1366c321187Smrg    q->nentries++;
1376c321187Smrg    return e;
1386c321187Smrg}
1396c321187Smrg
1406c321187Smrg
1416c321187Smrg/*
1426c321187Smrg * XmuDQRemoveDisplay - remove the specified display from the queue
1436c321187Smrg */
1446c321187SmrgBool
1456c321187SmrgXmuDQRemoveDisplay(XmuDisplayQueue *q, Display *dpy)
1466c321187Smrg{
1476c321187Smrg    XmuDisplayQueueEntry *e;
1486c321187Smrg
1496c321187Smrg    for (e = q->head; e; e = e->next) {
1506c321187Smrg	if (e->display == dpy) {
1516c321187Smrg	    if (q->head == e)
1526c321187Smrg	      q->head = e->next;	/* if at head, then bump head */
1536c321187Smrg	    else
1546c321187Smrg	      e->prev->next = e->next;	/* else splice out */
1556c321187Smrg	    if (q->tail == e)
1566c321187Smrg	      q->tail = e->prev;	/* if at tail, then bump tail */
1576c321187Smrg	    else
1586c321187Smrg	      e->next->prev = e->prev;	/* else splice out */
1596c321187Smrg	    (void) XmuRemoveCloseDisplayHook (dpy, e->closehook,
1606c321187Smrg					      _DQCloseDisplay, (XPointer) q);
1616c321187Smrg	    free ((char *) e);
1626c321187Smrg	    q->nentries--;
1636c321187Smrg	    return True;
1646c321187Smrg	}
1656c321187Smrg    }
1666c321187Smrg    return False;
1676c321187Smrg}
1686c321187Smrg
1696c321187Smrg
1706c321187Smrg/*****************************************************************************
1716c321187Smrg *			       private functions                             *
1726c321187Smrg *****************************************************************************/
1736c321187Smrg
1746c321187Smrg/*
1756c321187Smrg * _DQCloseDisplay - upcalled from CloseHook to notify this queue; remove the
1766c321187Smrg * display when finished
1776c321187Smrg */
1786c321187Smrgstatic int
1796c321187Smrg_DQCloseDisplay(Display *dpy, XPointer arg)
1806c321187Smrg{
1816c321187Smrg    XmuDisplayQueue *q = (XmuDisplayQueue *) arg;
1826c321187Smrg    XmuDisplayQueueEntry *e;
1836c321187Smrg
1846c321187Smrg    for (e = q->head; e; e = e->next) {
1856c321187Smrg	if (e->display == dpy) {
1866c321187Smrg	    if (q->closefunc) CallCloseCallback (q, e);
1876c321187Smrg	    (void) XmuDQRemoveDisplay (q, dpy);
1886c321187Smrg	    if (q->nentries == 0 && q->freefunc) CallFreeCallback (q);
1896c321187Smrg	    return 1;
1906c321187Smrg	}
1916c321187Smrg    }
1926c321187Smrg
1936c321187Smrg    return 0;
1946c321187Smrg}
195