home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xmu / LocBitmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-01  |  6.8 KB  |  260 lines

  1. /*
  2.  * $XConsortium: LocBitmap.c,v 1.16 91/07/02 09:09:59 rws 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 <X11/Xlib.h>
  27. #include <X11/Xresource.h>
  28. #include <X11/Xutil.h>
  29. #include <X11/Xmu/CvtCache.h>
  30. #include <X11/Xmu/Drawing.h>
  31.  
  32. #ifndef X_NOT_POSIX
  33. #ifdef _POSIX_SOURCE
  34. #include <limits.h>
  35. #else
  36. #define _POSIX_SOURCE
  37. #include <limits.h>
  38. #undef _POSIX_SOURCE
  39. #endif
  40. #endif /* X_NOT_POSIX */
  41. #ifndef PATH_MAX
  42. #include <sys/param.h>
  43. #ifdef MAXPATHLEN
  44. #define PATH_MAX MAXPATHLEN
  45. #else
  46. #define PATH_MAX 1024
  47. #endif
  48. #endif /* PATH_MAX */
  49.  
  50. static char **split_path_string();
  51.  
  52.  
  53. /*
  54.  * XmuLocateBitmapFile - read a bitmap file using the normal defaults
  55.  */
  56.  
  57. #if NeedFunctionPrototypes
  58. Pixmap XmuLocateBitmapFile (Screen *screen, _Xconst char *name, char *srcname,
  59.                 int srcnamelen, int *widthp, int *heightp, 
  60.                 int *xhotp, int *yhotp)
  61. #else
  62. Pixmap XmuLocateBitmapFile (screen, name, srcname, srcnamelen,
  63.                 widthp, heightp, xhotp, yhotp)
  64.     Screen *screen;
  65.     char *name;
  66.     char *srcname;            /* RETURN */
  67.     int srcnamelen;
  68.     int *widthp, *heightp, *xhotp, *yhotp;  /* RETURN */
  69. #endif
  70. {
  71.     return XmuLocatePixmapFile (screen, name, 
  72.                 (unsigned long) 1, (unsigned long) 0,
  73.                 (unsigned int) 1, srcname, srcnamelen,
  74.                 widthp, heightp, xhotp, yhotp);
  75. }
  76.  
  77.  
  78. /*
  79.  * version that reads pixmap data as well as bitmap data
  80.  */
  81. #if NeedFunctionPrototypes
  82. Pixmap XmuLocatePixmapFile (Screen *screen, _Xconst char *name, 
  83.                 unsigned long fore, unsigned long back, 
  84.                 unsigned int depth, 
  85.                 char *srcname, int srcnamelen,
  86.                 int *widthp, int *heightp, int *xhotp, int *yhotp)
  87. #else
  88. Pixmap XmuLocatePixmapFile (screen, name, fore, back, depth, 
  89.                 srcname, srcnamelen,
  90.                 widthp, heightp, xhotp, yhotp)
  91.     Screen *screen;
  92.     char *name;
  93.     unsigned long fore, back;
  94.     unsigned int depth;
  95.     char *srcname;            /* RETURN */
  96.     int srcnamelen;
  97.     int *widthp, *heightp, *xhotp, *yhotp;  /* RETURN */
  98. #endif
  99. {
  100.     Display *dpy = DisplayOfScreen (screen);
  101.     Window root = RootWindowOfScreen (screen);
  102.     Bool try_plain_name = True;
  103.     XmuCvtCache *cache = _XmuCCLookupDisplay (dpy);
  104.     char **file_paths = (char **) NULL;
  105.     char filename[PATH_MAX];
  106.     unsigned int width, height;
  107.     int xhot, yhot;
  108.     int i;
  109.  
  110.     /*
  111.      * look in cache for bitmap path
  112.      */
  113.     if (cache) {
  114.     if (!cache->string_to_bitmap.bitmapFilePath) {
  115.         XrmName xrm_name[2];
  116.         XrmClass xrm_class[2];
  117.         XrmRepresentation rep_type;
  118.         XrmValue value;
  119.  
  120.         xrm_name[0] = XrmPermStringToQuark ("bitmapFilePath");
  121.         xrm_name[1] = NULLQUARK;
  122.         xrm_class[0] = XrmPermStringToQuark ("BitmapFilePath");
  123.         xrm_class[1] = NULLQUARK;
  124.         if (!XrmGetDatabase(dpy)) {
  125.         /* what a hack; need to initialize it */
  126.         (void) XGetDefault (dpy, "", "");
  127.         }
  128.         if (XrmQGetResource (XrmGetDatabase(dpy), xrm_name, xrm_class, 
  129.                  &rep_type, &value) &&
  130.         rep_type == XrmPermStringToQuark("String")) {
  131.         cache->string_to_bitmap.bitmapFilePath = 
  132.           split_path_string (value.addr);
  133.         }
  134.     }
  135.     file_paths = cache->string_to_bitmap.bitmapFilePath;
  136.     }
  137.  
  138.  
  139.     /*
  140.      * Search order:
  141.      *    1.  name if it begins with / or ./
  142.      *    2.  "each prefix in file_paths"/name
  143.      *    3.  BITMAPDIR/name
  144.      *    4.  name if didn't begin with / or .
  145.      */
  146.  
  147. #ifndef BITMAPDIR
  148. #define BITMAPDIR "/usr/include/X11/bitmaps"
  149. #endif
  150.  
  151.     for (i = 1; i <= 4; i++) {
  152.     char *fn = filename;
  153.     Pixmap pixmap;
  154.     unsigned char *data;
  155.  
  156.     switch (i) {
  157.       case 1:
  158.         if (!(name[0] == '/' || (name[0] == '.') && name[1] == '/')) 
  159.           continue;
  160.         fn = (char *) name;
  161.         try_plain_name = False;
  162.         break;
  163.       case 2:
  164.         if (file_paths && *file_paths) {
  165.         sprintf (filename, "%s/%s", *file_paths, name);
  166.         file_paths++;
  167.         i--;
  168.         break;
  169.         }
  170.         continue;
  171.       case 3:
  172.         sprintf (filename, "%s/%s", BITMAPDIR, name);
  173.         break;
  174.       case 4:
  175.         if (!try_plain_name) continue;
  176.         fn = (char *) name;
  177.         break;
  178.     }
  179.  
  180.     data = NULL;
  181.     pixmap = None;
  182.     if (XmuReadBitmapDataFromFile (fn, &width, &height, &data,
  183.                        &xhot, &yhot) == BitmapSuccess) {
  184.         pixmap = XCreatePixmapFromBitmapData (dpy, root, (char *) data,
  185.                           width, height,
  186.                           fore, back, depth);
  187.         XFree ((char *)data);
  188.     }
  189.  
  190.     if (pixmap) {
  191.         if (widthp) *widthp = (int)width;
  192.         if (heightp) *heightp = (int)height;
  193.         if (xhotp) *xhotp = xhot;
  194.         if (yhotp) *yhotp = yhot;
  195.         if (srcname && srcnamelen > 0) {
  196.         strncpy (srcname, fn, srcnamelen - 1);
  197.         srcname[srcnamelen - 1] = '\0';
  198.         }
  199.         return pixmap;
  200.     }
  201.     }
  202.  
  203.     return None;
  204. }
  205.  
  206.  
  207. /*
  208.  * split_path_string - split a colon-separated list into its constituent
  209.  * parts; to release, free list[0] and list.
  210.  */
  211. static char **split_path_string (src)
  212.     register char *src;
  213. {
  214.     int nelems = 1;
  215.     register char *dst;
  216.     char **elemlist, **elem;
  217.  
  218.     /* count the number of elements */
  219.     for (dst = src; *dst; dst++) if (*dst == ':') nelems++;
  220.  
  221.     /* get memory for everything */
  222.     dst = (char *) malloc (dst - src + 1);
  223.     if (!dst) return NULL;
  224.     elemlist = (char **) calloc ((nelems + 1), sizeof (char *));
  225.     if (!elemlist) {
  226.     free (dst);
  227.     return NULL;
  228.     }
  229.  
  230.     /* copy to new list and walk up nulling colons and setting list pointers */
  231.     strcpy (dst, src);
  232.     for (elem = elemlist, src = dst; *src; src++) {
  233.     if (*src == ':') {
  234.         *elem++ = dst;
  235.         *src = '\0';
  236.         dst = src + 1;
  237.     }
  238.     }
  239.     *elem = dst;
  240.  
  241.     return elemlist;
  242. }
  243.  
  244.  
  245. void _XmuStringToBitmapInitCache (c)
  246.     register XmuCvtCache *c;
  247. {
  248.     c->string_to_bitmap.bitmapFilePath = NULL;
  249. }
  250.  
  251. void _XmuStringToBitmapFreeCache (c)
  252.     register XmuCvtCache *c;
  253. {
  254.     if (c->string_to_bitmap.bitmapFilePath) {
  255.     if (c->string_to_bitmap.bitmapFilePath[0]) 
  256.       free (c->string_to_bitmap.bitmapFilePath[0]);
  257.     free ((char *) (c->string_to_bitmap.bitmapFilePath));
  258.     }
  259. }
  260.