home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xmu / DisplayQue.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-28  |  4.8 KB  |  187 lines

  1. /*
  2.  * $XConsortium: DisplayQue.c,v 1.4 91/05/28 16:16:46 converse Exp $
  3.  *
  4.  * Copyright 1989 Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software and its
  7.  * documentation for any purpose and without fee is hereby granted, provided
  8.  * that the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of M.I.T. not be used in advertising
  11.  * or publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  M.I.T. makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * Author:  Jim Fulton, MIT X Consortium
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <X11/Xos.h>
  28. #include <X11/Xlib.h>
  29. #include <X11/Xmu/DisplayQue.h>
  30.  
  31. static int _DQCloseDisplay();
  32.  
  33. #define CallCloseCallback(q,e) (void) (*((q)->closefunc)) ((q), (e))
  34. #define CallFreeCallback(q) (void) (*((q)->freefunc)) ((q))
  35.  
  36. /*
  37.  * XmuDQCreate - create a display queue
  38.  */
  39. XmuDisplayQueue *XmuDQCreate (closefunc, freefunc, data)
  40.     XmuCloseDisplayQueueProc closefunc;
  41.     XmuFreeDisplayQueueProc freefunc;
  42.     caddr_t data;
  43. {
  44.     XmuDisplayQueue *q = (XmuDisplayQueue *) malloc (sizeof (XmuDisplayQueue));
  45.     if (q) {
  46.     q->nentries = 0;
  47.     q->head = q->tail = NULL;
  48.     q->closefunc = closefunc;
  49.     q->freefunc = freefunc;
  50.     q->data = data;
  51.     }
  52.     return q;
  53. }
  54.  
  55.  
  56. /*
  57.  * XmuDQDestroy - free all storage associated with this display queue, 
  58.  * optionally invoking the close callbacks.
  59.  */
  60.  
  61. Bool XmuDQDestroy (q, docallbacks)
  62.     XmuDisplayQueue *q;
  63.     Bool docallbacks;
  64. {
  65.     XmuDisplayQueueEntry *e = q->head;
  66.  
  67.     while (e) {
  68.     XmuDisplayQueueEntry *nexte = e->next;
  69.     if (docallbacks && q->closefunc) CallCloseCallback (q, e);
  70.     free ((char *) e);
  71.     e = nexte;
  72.     }
  73.     free ((char *) q);
  74.     return True;
  75. }
  76.  
  77.  
  78. /*
  79.  * XmuDQLookupDisplay - finds the indicated display on the given queue
  80.  */
  81. XmuDisplayQueueEntry *XmuDQLookupDisplay (q, dpy)
  82.     XmuDisplayQueue *q;
  83.     Display *dpy;
  84. {
  85.     XmuDisplayQueueEntry *e;
  86.  
  87.     for (e = q->head; e; e = e->next) {
  88.     if (e->display == dpy) return e;
  89.     }
  90.     return NULL;
  91. }
  92.  
  93.  
  94. /*
  95.  * XmuDQAddDisplay - add the specified display to the queue; set data as a
  96.  * convenience.  Does not ensure that dpy hasn't already been added.
  97.  */
  98. XmuDisplayQueueEntry *XmuDQAddDisplay (q, dpy, data)
  99.     XmuDisplayQueue *q;
  100.     Display *dpy;
  101.     caddr_t data;
  102. {
  103.     XmuDisplayQueueEntry *e;
  104.  
  105.     if (!(e = (XmuDisplayQueueEntry *) malloc (sizeof (XmuDisplayQueueEntry)))) {
  106.     return NULL;
  107.     }
  108.     if (!(e->closehook = XmuAddCloseDisplayHook (dpy, _DQCloseDisplay,
  109.                          (caddr_t) q))) {
  110.     free ((char *) e);
  111.     return NULL;
  112.     }
  113.  
  114.     e->display = dpy;
  115.     e->next = NULL;
  116.     e->data = data;
  117.  
  118.     if (q->tail) {
  119.     q->tail->next = e;
  120.     e->prev = q->tail;
  121.     } else {
  122.     q->head = e;
  123.     e->prev = NULL;
  124.     }
  125.     q->tail = e;
  126.     q->nentries++;
  127.     return e;
  128. }
  129.  
  130.  
  131. /*
  132.  * XmuDQRemoveDisplay - remove the specified display from the queue
  133.  */
  134. Bool XmuDQRemoveDisplay (q, dpy)
  135.     XmuDisplayQueue *q;
  136.     Display *dpy;
  137. {
  138.     XmuDisplayQueueEntry *e;
  139.  
  140.     for (e = q->head; e; e = e->next) {
  141.     if (e->display == dpy) {
  142.         if (q->head == e)
  143.           q->head = e->next;    /* if at head, then bump head */
  144.         else
  145.           e->prev->next = e->next;    /* else splice out */
  146.         if (q->tail == e)
  147.           q->tail = e->prev;    /* if at tail, then bump tail */
  148.         else
  149.           e->next->prev = e->prev;    /* else splice out */
  150.         (void) XmuRemoveCloseDisplayHook (dpy, e->closehook,
  151.                           _DQCloseDisplay, (caddr_t) q);
  152.         free ((char *) e);
  153.         q->nentries--;
  154.         return True;
  155.     }
  156.     }
  157.     return False;
  158. }
  159.  
  160.  
  161. /*****************************************************************************
  162.  *                   private functions                             *
  163.  *****************************************************************************/
  164.  
  165. /*
  166.  * _DQCloseDisplay - upcalled from CloseHook to notify this queue; remove the
  167.  * display when finished
  168.  */
  169. static int _DQCloseDisplay (dpy, arg)
  170.     Display *dpy;
  171.     caddr_t arg;
  172. {
  173.     XmuDisplayQueue *q = (XmuDisplayQueue *) arg;
  174.     XmuDisplayQueueEntry *e;
  175.  
  176.     for (e = q->head; e; e = e->next) {
  177.     if (e->display == dpy) {
  178.         if (q->closefunc) CallCloseCallback (q, e);
  179.         (void) XmuDQRemoveDisplay (q, dpy);
  180.         if (q->nentries == 0 && q->freefunc) CallFreeCallback (q);
  181.         return 1;
  182.     }
  183.     }
  184.  
  185.     return 0;
  186. }
  187.