00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <stdio.h>
00035 #include <X11/Xlib.h>
00036 #include <stdlib.h>
00037 #include <X11/Xmu/DisplayQue.h>
00038
00039
00040
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
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
00069
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
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
00105
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
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;
00150 else
00151 e->prev->next = e->next;
00152 if (q->tail == e)
00153 q->tail = e->prev;
00154 else
00155 e->next->prev = e->prev;
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
00169
00170
00171
00172
00173
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 }