LocBitmap.c

Go to the documentation of this file.
00001 /* $Xorg: LocBitmap.c,v 1.7 2001/02/09 02:03:52 xorgcvs Exp $ */
00002 
00003 /*
00004  
00005 Copyright 1989, 1998  The Open Group
00006 
00007 Permission to use, copy, modify, distribute, and sell this software and its
00008 documentation for any purpose is hereby granted without fee, provided that
00009 the above copyright notice appear in all copies and that both that
00010 copyright notice and this permission notice appear in supporting
00011 documentation.
00012 
00013 The above copyright notice and this permission notice shall be included in
00014 all copies or substantial portions of the Software.
00015 
00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00019 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00020 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00021 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022 
00023 Except as contained in this notice, the name of The Open Group shall not be
00024 used in advertising or otherwise to promote the sale, use or other dealings
00025 in this Software without prior written authorization from The Open Group.
00026 
00027 */
00028 /* $XFree86: xc/lib/Xmu/LocBitmap.c,v 3.9 2001/12/14 19:55:47 dawes Exp $ */
00029 
00030 /*
00031  * Author:  Jim Fulton, MIT X Consortium
00032  */
00033 
00034 #include <X11/Xlib.h>
00035 #include <stdlib.h>
00036 #include <string.h>
00037 #include <X11/Xresource.h>
00038 #include <X11/Xutil.h>
00039 #include <X11/Xmu/CvtCache.h>
00040 #include <X11/Xmu/Drawing.h>
00041 #include <X11/Xmu/SysUtil.h>
00042 
00043 #ifndef X_NOT_POSIX
00044 #ifdef _POSIX_SOURCE
00045 #include <limits.h>
00046 #else
00047 #define _POSIX_SOURCE
00048 #include <limits.h>
00049 #undef _POSIX_SOURCE
00050 #endif
00051 #endif /* X_NOT_POSIX */
00052 #ifndef PATH_MAX
00053 #ifdef WIN32
00054 #define PATH_MAX 512
00055 #else
00056 #include <sys/param.h>
00057 #endif
00058 #ifndef PATH_MAX
00059 #ifdef MAXPATHLEN
00060 #define PATH_MAX MAXPATHLEN
00061 #else
00062 #define PATH_MAX 1024
00063 #endif
00064 #endif
00065 #endif /* PATH_MAX */
00066 
00067 /*
00068  * Prototypes
00069  */
00070 static char **split_path_string(char*);
00071 
00072 /*
00073  * XmuLocateBitmapFile - read a bitmap file using the normal defaults
00074  */
00075 
00076 Pixmap
00077 XmuLocateBitmapFile(Screen *screen, _Xconst char *name, char *srcname,
00078                             int srcnamelen, int *widthp, int *heightp, 
00079                             int *xhotp, int *yhotp)
00080 {
00081     return XmuLocatePixmapFile (screen, name, 
00082                                 (unsigned long) 1, (unsigned long) 0,
00083                                 (unsigned int) 1, srcname, srcnamelen,
00084                                 widthp, heightp, xhotp, yhotp);
00085 }
00086 
00087 
00088 /*
00089  * version that reads pixmap data as well as bitmap data
00090  */
00091 Pixmap
00092 XmuLocatePixmapFile(Screen *screen, _Xconst char *name, 
00093                             unsigned long fore, unsigned long back, 
00094                             unsigned int depth, 
00095                             char *srcname, int srcnamelen,
00096                             int *widthp, int *heightp, int *xhotp, int *yhotp)
00097 {
00098 
00099 #ifndef BITMAPDIR
00100 #define BITMAPDIR "/usr/include/X11/bitmaps"
00101 #endif
00102 
00103     Display *dpy = DisplayOfScreen (screen);
00104     Window root = RootWindowOfScreen (screen);
00105     Bool try_plain_name = True;
00106     XmuCvtCache *cache = _XmuCCLookupDisplay (dpy);
00107     char **file_paths = (char **) NULL;
00108     char filename[PATH_MAX];
00109 #if 0
00110     char* bitmapdir = BITMAPDIR;
00111 #endif
00112     unsigned int width, height;
00113     int xhot, yhot;
00114     int i;
00115 
00116     /*
00117      * look in cache for bitmap path
00118      */
00119     if (cache) {
00120         if (!cache->string_to_bitmap.bitmapFilePath) {
00121             XrmName xrm_name[2];
00122             XrmClass xrm_class[2];
00123             XrmRepresentation rep_type;
00124             XrmValue value;
00125 
00126             xrm_name[0] = XrmPermStringToQuark ("bitmapFilePath");
00127             xrm_name[1] = NULLQUARK;
00128             xrm_class[0] = XrmPermStringToQuark ("BitmapFilePath");
00129             xrm_class[1] = NULLQUARK;
00130             if (!XrmGetDatabase(dpy)) {
00131                 /* what a hack; need to initialize it */
00132                 (void) XGetDefault (dpy, "", "");
00133             }
00134             if (XrmQGetResource (XrmGetDatabase(dpy), xrm_name, xrm_class, 
00135                                  &rep_type, &value) &&
00136                 rep_type == XrmPermStringToQuark("String")) {
00137                 cache->string_to_bitmap.bitmapFilePath = 
00138                   split_path_string (value.addr);
00139             }
00140         }
00141         file_paths = cache->string_to_bitmap.bitmapFilePath;
00142     }
00143 
00144     /*
00145      * Search order:
00146      *    1.  name if it begins with / or ./
00147      *    2.  "each prefix in file_paths"/name
00148      *    3.  BITMAPDIR/name
00149      *    4.  name if didn't begin with / or .
00150      */
00151 
00152     for (i = 1; i <= 4; i++) {
00153         char *fn = filename;
00154         Pixmap pixmap;
00155         unsigned char *data;
00156 
00157         switch (i) {
00158           case 1:
00159 #ifndef __UNIXOS2__
00160             if (!(name[0] == '/' || ((name[0] == '.') && name[1] == '/')))
00161 #else
00162             if (!(name[0] == '/' || (name[0] == '.' && name[1] == '/') ||
00163                   (isalpha(name[0]) && name[1] == ':')))
00164 #endif
00165               continue;
00166             fn = (char *) name;
00167             try_plain_name = False;
00168             break;
00169           case 2:
00170             if (file_paths && *file_paths) {
00171                 XmuSnprintf(filename, sizeof(filename),
00172                             "%s/%s", *file_paths, name);
00173                 file_paths++;
00174                 i--;
00175                 break;
00176             }
00177             continue;
00178           case 3:
00179             XmuSnprintf(filename, sizeof(filename), "%s/%s", BITMAPDIR, name);
00180             break;
00181           case 4:
00182             if (!try_plain_name) continue;
00183             fn = (char *) name;
00184             break;
00185         }
00186 
00187         data = NULL;
00188         pixmap = None;
00189 #ifdef __UNIXOS2__
00190         fn = (char*)__XOS2RedirRoot(fn);
00191 #endif
00192         if (XmuReadBitmapDataFromFile (fn, &width, &height, &data,
00193                                        &xhot, &yhot) == BitmapSuccess) {
00194             pixmap = XCreatePixmapFromBitmapData (dpy, root, (char *) data,
00195                                                   width, height,
00196                                                   fore, back, depth);
00197             XFree ((char *)data);
00198         }
00199 
00200         if (pixmap) {
00201             if (widthp) *widthp = (int)width;
00202             if (heightp) *heightp = (int)height;
00203             if (xhotp) *xhotp = xhot;
00204             if (yhotp) *yhotp = yhot;
00205             if (srcname && srcnamelen > 0) {
00206                 strncpy (srcname, fn, srcnamelen - 1);
00207                 srcname[srcnamelen - 1] = '\0';
00208             }
00209             return pixmap;
00210         }
00211     }
00212 
00213     return None;
00214 }
00215 
00216 
00217 /*
00218  * split_path_string - split a colon-separated list into its constituent
00219  * parts; to release, free list[0] and list.
00220  */
00221 static char **
00222 split_path_string(register char *src)
00223 {
00224     int nelems = 1;
00225     register char *dst;
00226     char **elemlist, **elem;
00227 
00228     /* count the number of elements */
00229     for (dst = src; *dst; dst++) if (*dst == ':') nelems++;
00230 
00231     /* get memory for everything */
00232     dst = (char *) malloc (dst - src + 1);
00233     if (!dst) return NULL;
00234     elemlist = (char **) calloc ((nelems + 1), sizeof (char *));
00235     if (!elemlist) {
00236         free (dst);
00237         return NULL;
00238     }
00239 
00240     /* copy to new list and walk up nulling colons and setting list pointers */
00241     strcpy (dst, src);
00242     for (elem = elemlist, src = dst; *src; src++) {
00243         if (*src == ':') {
00244             *elem++ = dst;
00245             *src = '\0';
00246             dst = src + 1;
00247         }
00248     }
00249     *elem = dst;
00250 
00251     return elemlist;
00252 }
00253 
00254 
00255 void
00256 _XmuStringToBitmapInitCache(register XmuCvtCache *c)
00257 {
00258     c->string_to_bitmap.bitmapFilePath = NULL;
00259 }
00260 
00261 void
00262 _XmuStringToBitmapFreeCache(register XmuCvtCache *c)
00263 {
00264     if (c->string_to_bitmap.bitmapFilePath) {
00265         if (c->string_to_bitmap.bitmapFilePath[0]) 
00266           free (c->string_to_bitmap.bitmapFilePath[0]);
00267         free ((char *) (c->string_to_bitmap.bitmapFilePath));
00268     }
00269 }

Generated on Sun Mar 4 15:03:56 2007 for Scilab [trunk] by  doxygen 1.5.1