DisplayQue.c revision 6c321187
1/* $Xorg: DisplayQue.c,v 1.4 2001/02/09 02:03:52 xorgcvs Exp $ */ 2 3/* 4 5Copyright 1989, 1998 The Open Group 6 7Permission to use, copy, modify, distribute, and sell this software and its 8documentation for any purpose is hereby granted without fee, provided that 9the above copyright notice appear in all copies and that both that 10copyright notice and this permission notice appear in supporting 11documentation. 12 13The above copyright notice and this permission notice shall be included in 14all copies or substantial portions of the Software. 15 16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 20AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 23Except as contained in this notice, the name of The Open Group shall not be 24used in advertising or otherwise to promote the sale, use or other dealings 25in this Software without prior written authorization from The Open Group. 26 27*/ 28/* $XFree86: xc/lib/Xmu/DisplayQue.c,v 3.4 2001/07/25 15:04:50 dawes Exp $ */ 29 30/* 31 * Author: Jim Fulton, MIT X Consortium 32 */ 33 34#ifdef HAVE_CONFIG_H 35#include <config.h> 36#endif 37#include <stdio.h> 38#include <X11/Xlib.h> 39#include <stdlib.h> 40#include <X11/Xmu/DisplayQue.h> 41 42/* 43 * Prototypes 44 */ 45static int _DQCloseDisplay(Display*, XPointer); 46 47#define CallCloseCallback(q,e) (void) (*((q)->closefunc)) ((q), (e)) 48#define CallFreeCallback(q) (void) (*((q)->freefunc)) ((q)) 49 50/* 51 * XmuDQCreate - create a display queue 52 */ 53XmuDisplayQueue * 54XmuDQCreate(XmuCloseDisplayQueueProc closefunc, 55 XmuFreeDisplayQueueProc freefunc, 56 XPointer data) 57{ 58 XmuDisplayQueue *q = (XmuDisplayQueue *) malloc (sizeof (XmuDisplayQueue)); 59 if (q) { 60 q->nentries = 0; 61 q->head = q->tail = NULL; 62 q->closefunc = closefunc; 63 q->freefunc = freefunc; 64 q->data = data; 65 } 66 return q; 67} 68 69 70/* 71 * XmuDQDestroy - free all storage associated with this display queue, 72 * optionally invoking the close callbacks. 73 */ 74 75Bool 76XmuDQDestroy(XmuDisplayQueue *q, Bool docallbacks) 77{ 78 XmuDisplayQueueEntry *e = q->head; 79 80 while (e) { 81 XmuDisplayQueueEntry *nexte = e->next; 82 if (docallbacks && q->closefunc) CallCloseCallback (q, e); 83 free ((char *) e); 84 e = nexte; 85 } 86 free ((char *) q); 87 return True; 88} 89 90 91/* 92 * XmuDQLookupDisplay - finds the indicated display on the given queue 93 */ 94XmuDisplayQueueEntry * 95XmuDQLookupDisplay(XmuDisplayQueue *q, Display *dpy) 96{ 97 XmuDisplayQueueEntry *e; 98 99 for (e = q->head; e; e = e->next) { 100 if (e->display == dpy) return e; 101 } 102 return NULL; 103} 104 105 106/* 107 * XmuDQAddDisplay - add the specified display to the queue; set data as a 108 * convenience. Does not ensure that dpy hasn't already been added. 109 */ 110XmuDisplayQueueEntry * 111XmuDQAddDisplay(XmuDisplayQueue *q, Display *dpy, XPointer data) 112{ 113 XmuDisplayQueueEntry *e; 114 115 if (!(e = (XmuDisplayQueueEntry *) malloc (sizeof (XmuDisplayQueueEntry)))) { 116 return NULL; 117 } 118 if (!(e->closehook = XmuAddCloseDisplayHook (dpy, _DQCloseDisplay, 119 (XPointer) q))) { 120 free ((char *) e); 121 return NULL; 122 } 123 124 e->display = dpy; 125 e->next = NULL; 126 e->data = data; 127 128 if (q->tail) { 129 q->tail->next = e; 130 e->prev = q->tail; 131 } else { 132 q->head = e; 133 e->prev = NULL; 134 } 135 q->tail = e; 136 q->nentries++; 137 return e; 138} 139 140 141/* 142 * XmuDQRemoveDisplay - remove the specified display from the queue 143 */ 144Bool 145XmuDQRemoveDisplay(XmuDisplayQueue *q, Display *dpy) 146{ 147 XmuDisplayQueueEntry *e; 148 149 for (e = q->head; e; e = e->next) { 150 if (e->display == dpy) { 151 if (q->head == e) 152 q->head = e->next; /* if at head, then bump head */ 153 else 154 e->prev->next = e->next; /* else splice out */ 155 if (q->tail == e) 156 q->tail = e->prev; /* if at tail, then bump tail */ 157 else 158 e->next->prev = e->prev; /* else splice out */ 159 (void) XmuRemoveCloseDisplayHook (dpy, e->closehook, 160 _DQCloseDisplay, (XPointer) q); 161 free ((char *) e); 162 q->nentries--; 163 return True; 164 } 165 } 166 return False; 167} 168 169 170/***************************************************************************** 171 * private functions * 172 *****************************************************************************/ 173 174/* 175 * _DQCloseDisplay - upcalled from CloseHook to notify this queue; remove the 176 * display when finished 177 */ 178static int 179_DQCloseDisplay(Display *dpy, XPointer arg) 180{ 181 XmuDisplayQueue *q = (XmuDisplayQueue *) arg; 182 XmuDisplayQueueEntry *e; 183 184 for (e = q->head; e; e = e->next) { 185 if (e->display == dpy) { 186 if (q->closefunc) CallCloseCallback (q, e); 187 (void) XmuDQRemoveDisplay (q, dpy); 188 if (q->nentries == 0 && q->freefunc) CallFreeCallback (q); 189 return 1; 190 } 191 } 192 193 return 0; 194} 195