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