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