DisplayQue.c

Go to the documentation of this file.
00001 /* $Xorg: DisplayQue.c,v 1.4 2001/02/09 02:03:52 xorgcvs Exp $ */
00002 
00003 /*
00004  
00005 Copyright 1989, 1998  The Open Group
00006 
00007 Permission to use, copy, modify, distribute, and sell this software and its
00008 documentation for any purpose is hereby granted without fee, provided that
00009 the above copyright notice appear in all copies and that both that
00010 copyright notice and this permission notice appear in supporting
00011 documentation.
00012 
00013 The above copyright notice and this permission notice shall be included in
00014 all copies or substantial portions of the Software.
00015 
00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00019 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00020 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00021 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022 
00023 Except as contained in this notice, the name of The Open Group shall not be
00024 used in advertising or otherwise to promote the sale, use or other dealings
00025 in this Software without prior written authorization from The Open Group.
00026 
00027 */
00028 /* $XFree86: xc/lib/Xmu/DisplayQue.c,v 3.4 2001/07/25 15:04:50 dawes Exp $ */
00029 
00030 /*
00031  * Author:  Jim Fulton, MIT X Consortium
00032  */
00033 
00034 #include <stdio.h>
00035 #include <X11/Xlib.h>
00036 #include <stdlib.h>
00037 #include <X11/Xmu/DisplayQue.h>
00038 
00039 /*
00040  * Prototypes
00041  */
00042 static int _DQCloseDisplay(Display*, XPointer);
00043 
00044 #define CallCloseCallback(q,e) (void) (*((q)->closefunc)) ((q), (e))
00045 #define CallFreeCallback(q) (void) (*((q)->freefunc)) ((q))
00046 
00047 /*
00048  * XmuDQCreate - create a display queue
00049  */
00050 XmuDisplayQueue *
00051 XmuDQCreate(XmuCloseDisplayQueueProc closefunc,
00052             XmuFreeDisplayQueueProc freefunc,
00053             XPointer data)
00054 {
00055     XmuDisplayQueue *q = (XmuDisplayQueue *) malloc (sizeof (XmuDisplayQueue));
00056     if (q) {
00057         q->nentries = 0;
00058         q->head = q->tail = NULL;
00059         q->closefunc = closefunc;
00060         q->freefunc = freefunc;
00061         q->data = data;
00062     }
00063     return q;
00064 }
00065 
00066 
00067 /*
00068  * XmuDQDestroy - free all storage associated with this display queue, 
00069  * optionally invoking the close callbacks.
00070  */
00071 
00072 Bool
00073 XmuDQDestroy(XmuDisplayQueue *q, Bool docallbacks)
00074 {
00075     XmuDisplayQueueEntry *e = q->head;
00076 
00077     while (e) {
00078         XmuDisplayQueueEntry *nexte = e->next;
00079         if (docallbacks && q->closefunc) CallCloseCallback (q, e);
00080         free ((char *) e);
00081         e = nexte;
00082     }
00083     free ((char *) q);
00084     return True;
00085 }
00086 
00087 
00088 /*
00089  * XmuDQLookupDisplay - finds the indicated display on the given queue
00090  */
00091 XmuDisplayQueueEntry *
00092 XmuDQLookupDisplay(XmuDisplayQueue *q, Display *dpy)
00093 {
00094     XmuDisplayQueueEntry *e;
00095 
00096     for (e = q->head; e; e = e->next) {
00097         if (e->display == dpy) return e;
00098     }
00099     return NULL;
00100 }
00101 
00102 
00103 /*
00104  * XmuDQAddDisplay - add the specified display to the queue; set data as a
00105  * convenience.  Does not ensure that dpy hasn't already been added.
00106  */
00107 XmuDisplayQueueEntry *
00108 XmuDQAddDisplay(XmuDisplayQueue *q, Display *dpy, XPointer data)
00109 {
00110     XmuDisplayQueueEntry *e;
00111 
00112     if (!(e = (XmuDisplayQueueEntry *) malloc (sizeof (XmuDisplayQueueEntry)))) {
00113         return NULL;
00114     }
00115     if (!(e->closehook = XmuAddCloseDisplayHook (dpy, _DQCloseDisplay,
00116                                                  (XPointer) q))) {
00117         free ((char *) e);
00118         return NULL;
00119     }
00120 
00121     e->display = dpy;
00122     e->next = NULL;
00123     e->data = data;
00124 
00125     if (q->tail) {
00126         q->tail->next = e;
00127         e->prev = q->tail;
00128     } else {
00129         q->head = e;
00130         e->prev = NULL;
00131     }
00132     q->tail = e;
00133     q->nentries++;
00134     return e;
00135 }
00136 
00137 
00138 /*
00139  * XmuDQRemoveDisplay - remove the specified display from the queue
00140  */
00141 Bool
00142 XmuDQRemoveDisplay(XmuDisplayQueue *q, Display *dpy)
00143 {
00144     XmuDisplayQueueEntry *e;
00145 
00146     for (e = q->head; e; e = e->next) {
00147         if (e->display == dpy) {
00148             if (q->head == e)
00149               q->head = e->next;        /* if at head, then bump head */
00150             else
00151               e->prev->next = e->next;  /* else splice out */
00152             if (q->tail == e)
00153               q->tail = e->prev;        /* if at tail, then bump tail */
00154             else
00155               e->next->prev = e->prev;  /* else splice out */
00156             (void) XmuRemoveCloseDisplayHook (dpy, e->closehook,
00157                                               _DQCloseDisplay, (XPointer) q);
00158             free ((char *) e);
00159             q->nentries--;
00160             return True;
00161         }
00162     }
00163     return False;
00164 }
00165 
00166 
00167 /*****************************************************************************
00168  *                             private functions                             *
00169  *****************************************************************************/
00170 
00171 /*
00172  * _DQCloseDisplay - upcalled from CloseHook to notify this queue; remove the
00173  * display when finished
00174  */
00175 static int
00176 _DQCloseDisplay(Display *dpy, XPointer arg)
00177 {
00178     XmuDisplayQueue *q = (XmuDisplayQueue *) arg;
00179     XmuDisplayQueueEntry *e;
00180 
00181     for (e = q->head; e; e = e->next) {
00182         if (e->display == dpy) {
00183             if (q->closefunc) CallCloseCallback (q, e);
00184             (void) XmuDQRemoveDisplay (q, dpy);
00185             if (q->nentries == 0 && q->freefunc) CallFreeCallback (q);
00186             return 1;
00187         }
00188     }
00189 
00190     return 0;
00191 }

Generated on Sun Mar 4 15:03:56 2007 for Scilab [trunk] by  doxygen 1.5.1