home *** CD-ROM | disk | FTP | other *** search
/ PC Shareware 1999 March / PCShareware-3-99.iso / IMPLE / DJGPP.RAR / DJGPP2 / XLIB-SR0.ZIP / SRC / EXTENSIO / EXTUTIL.C next >
C/C++ Source or Header  |  1994-09-03  |  8KB  |  262 lines

  1. /*
  2.  * $XConsortium: extutil.c,v 1.10 91/07/12 10:28:36 rws Exp $
  3.  *
  4.  * Copyright 1989 Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * 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 or
  11.  * 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.  *                Xlib Extension-Writing Utilities
  27.  * 
  28.  * This package contains utilities for writing the client API for various
  29.  * protocol extensions.  THESE INTERFACES ARE NOT PART OF THE X STANDARD AND
  30.  * ARE SUBJECT TO CHANGE!
  31.  * 
  32.  *  Routines include:
  33.  * 
  34.  *         XextCreateExtension        called once per extension
  35.  *         XextDestroyExtension        if no longer using extension
  36.  *         XextAddDisplay        add another display
  37.  *         XextRemoveDisplay        remove a display
  38.  *         XextFindDisplay        is a display open
  39.  * 
  40.  * In addition, the following Xlib-style interfaces are provided:
  41.  * 
  42.  *         XSetExtensionErrorHandler    establish an extension error handler
  43.  *         XMissingExtension        raise an error about missing ext
  44.  */
  45.  
  46. #include <stdio.h>
  47. #include "Xlibint.h"
  48. #include "Xext.h"
  49. #include "extutil.h"
  50.  
  51.  
  52. /*
  53.  * XextCreateExtension - return an extension descriptor containing context
  54.  * information for this extension.  This object is passed to all Xext 
  55.  * routines.
  56.  */
  57. XExtensionInfo *XextCreateExtension ()
  58. {
  59.     register XExtensionInfo *info =
  60.       (XExtensionInfo *) Xmalloc (sizeof (XExtensionInfo));
  61.  
  62.     if (info) {
  63.     info->head = NULL;
  64.     info->cur = NULL;
  65.     info->ndisplays = 0;
  66.     }
  67.     return info;
  68. }
  69.  
  70.  
  71. /*
  72.  * XextDestroyExtension - free memory the given extension descriptor
  73.  */
  74. void XextDestroyExtension (info)
  75.     XExtensionInfo *info;
  76. {
  77.     info->head = NULL;            /* to catch refs after this */
  78.     info->cur = NULL;
  79.     info->ndisplays = 0;
  80.     XFree ((char *) info);
  81. }
  82.  
  83.  
  84.  
  85. /*
  86.  * XextAddDisplay - add a display to this extension
  87.  */
  88. XExtDisplayInfo *XextAddDisplay (extinfo, dpy, ext_name, hooks, nevents, data)
  89.     XExtensionInfo *extinfo;
  90.     Display *dpy;
  91.     char *ext_name;
  92.     XExtensionHooks *hooks;
  93.     int nevents;
  94.     caddr_t data;
  95. {
  96.     XExtDisplayInfo *dpyinfo;
  97.  
  98.     dpyinfo = (XExtDisplayInfo *) Xmalloc (sizeof (XExtDisplayInfo));
  99.     if (!dpyinfo) return NULL;
  100.     dpyinfo->next = extinfo->head;
  101.     dpyinfo->display = dpy;
  102.     dpyinfo->data = data;
  103.     dpyinfo->codes = XInitExtension (dpy, ext_name);
  104.  
  105.     /*
  106.      * if the server has the extension, then we can initialize the 
  107.      * appropriate function vectors
  108.      */
  109.     if (dpyinfo->codes) {
  110.     int i, j;
  111.  
  112.     for (i = 0, j = dpyinfo->codes->first_event; i < nevents; i++, j++) {
  113.         XESetWireToEvent (dpy, j, hooks->wire_to_event);
  114.         XESetEventToWire (dpy, j, hooks->event_to_wire);
  115.     }
  116.     if (hooks->create_gc)
  117.       XESetCreateGC (dpy, dpyinfo->codes->extension, hooks->create_gc);
  118.     if (hooks->copy_gc)
  119.       XESetCopyGC (dpy, dpyinfo->codes->extension, hooks->copy_gc);
  120.     if (hooks->flush_gc)
  121.       XESetFlushGC (dpy, dpyinfo->codes->extension, hooks->flush_gc);
  122.     if (hooks->free_gc)
  123.       XESetFreeGC (dpy, dpyinfo->codes->extension, hooks->free_gc);
  124.     if (hooks->create_font)
  125.       XESetCreateFont (dpy, dpyinfo->codes->extension, hooks->create_font);
  126.     if (hooks->free_font)
  127.       XESetFreeFont (dpy, dpyinfo->codes->extension, hooks->free_font);
  128.     if (hooks->close_display)
  129.       XESetCloseDisplay (dpy, dpyinfo->codes->extension, 
  130.                  hooks->close_display);
  131.     if (hooks->error)
  132.       XESetError (dpy, dpyinfo->codes->extension, hooks->error);
  133.     if (hooks->error_string)
  134.       XESetErrorString (dpy, dpyinfo->codes->extension,
  135.                 hooks->error_string);
  136.     }
  137.  
  138.     /*
  139.      * now, chain it onto the list
  140.      */
  141.     extinfo->head = dpyinfo;
  142.     extinfo->cur = dpyinfo;
  143.     extinfo->ndisplays++;
  144.     return dpyinfo;
  145. }
  146.  
  147.  
  148. /*
  149.  * XextRemoveDisplay - remove the indicated display from the extension object
  150.  */
  151. int XextRemoveDisplay (extinfo, dpy)
  152.     XExtensionInfo *extinfo;
  153.     Display *dpy;
  154. {
  155.     XExtDisplayInfo *dpyinfo, *prev;
  156.  
  157.     /*
  158.      * locate this display and its back link so that it can be removed
  159.      */
  160.     prev = NULL;
  161.     for (dpyinfo = extinfo->head; dpyinfo; dpyinfo = dpyinfo->next) {
  162.     if (dpyinfo->display == dpy) break;
  163.     prev = dpyinfo;
  164.     }
  165.     if (!dpyinfo) return 0;        /* hmm, actually an error */
  166.  
  167.     /*
  168.      * remove the display from the list; handles going to zero
  169.      */
  170.     if (prev)
  171.     prev->next = dpyinfo->next;
  172.     else
  173.     extinfo->head = dpyinfo->next;
  174.  
  175.     extinfo->ndisplays--;
  176.     if (dpyinfo == extinfo->cur) extinfo->cur = NULL;  /* flush cache */
  177.  
  178.     Xfree ((char *) dpyinfo);
  179.     return 1;
  180. }
  181.  
  182.  
  183. /*
  184.  * XextFindDisplay - look for a display in this extension; keeps a cache
  185.  * of the most-recently used for efficiency.
  186.  */
  187. XExtDisplayInfo *XextFindDisplay (extinfo, dpy)
  188.     XExtensionInfo *extinfo;
  189.     Display *dpy;
  190. {
  191.     register XExtDisplayInfo *dpyinfo;
  192.  
  193.     /*
  194.      * see if this was the most recently accessed display
  195.      */
  196.     if ((dpyinfo = extinfo->cur)&& dpyinfo->display == dpy) return dpyinfo;
  197.  
  198.  
  199.     /*
  200.      * look for display in list
  201.      */
  202.     for (dpyinfo = extinfo->head; dpyinfo; dpyinfo = dpyinfo->next) {
  203.     if (dpyinfo->display == dpy) {
  204.         extinfo->cur = dpyinfo;    /* cache most recently used */
  205.         return dpyinfo;
  206.     }
  207.     }
  208.  
  209.     return NULL;
  210. }
  211.  
  212.  
  213.  
  214. static int _default_exterror (dpy, ext_name, reason)
  215.     Display *dpy;
  216.     char *ext_name;
  217.     char *reason;
  218. {
  219.     fprintf (stderr, "Xlib:  extension \"%s\" %s on display \"%s\".\n",
  220.          ext_name, reason, DisplayString(dpy));
  221.     return 0;
  222. }
  223.  
  224.  
  225. /*
  226.  * XSetExtensionErrorHandler - sets the handler that gets called when a 
  227.  * requested extension is referenced.  This should eventually move into Xlib.
  228.  */
  229.  
  230. extern int (*_XExtensionErrorFunction)();
  231.  
  232. int (*XSetExtensionErrorHandler(handler))()
  233.     int (*handler)();
  234. {
  235.     int (*oldhandler)() = _XExtensionErrorFunction;
  236.  
  237.     _XExtensionErrorFunction = (handler ? handler :
  238.                 _default_exterror);
  239.     return oldhandler;
  240. }
  241.  
  242.  
  243. /*
  244.  * XMissingExtension - call the extension error handler
  245.  */
  246. #if NeedFunctionPrototypes
  247. int XMissingExtension (
  248.     Display *dpy,
  249.     _Xconst char *ext_name)
  250. #else
  251. int XMissingExtension (dpy, ext_name)
  252.     Display *dpy;
  253.     char *ext_name;
  254. #endif
  255. {
  256.     int (*func)() = (_XExtensionErrorFunction ?
  257.              _XExtensionErrorFunction : _default_exterror);
  258.  
  259.     if (!ext_name) ext_name = X_EXTENSION_UNKNOWN;
  260.     return (*func) (dpy, ext_name, X_EXTENSION_MISSING);
  261. }
  262.