3 Copyright 1989, 1998 The Open Group
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
28 * Author: Jim Fulton, MIT X Consortium
37 #include <X11/Xresource.h>
38 #include <X11/Xutil.h>
39 #include <X11/Xmu/CvtCache.h>
40 #include <X11/Xmu/Drawing.h>
41 #include <X11/Xmu/SysUtil.h>
51 #endif /* X_NOT_POSIX */
56 #include <sys/param.h>
60 #define PATH_MAX MAXPATHLEN
70 static char **split_path_string(char*);
73 * XmuLocateBitmapFile - read a bitmap file using the normal defaults
77 XmuLocateBitmapFile(Screen *screen, _Xconst char *name, char *srcname,
78 int srcnamelen, int *widthp, int *heightp,
79 int *xhotp, int *yhotp)
81 return XmuLocatePixmapFile (screen, name,
82 (unsigned long) 1, (unsigned long) 0,
83 (unsigned int) 1, srcname, srcnamelen,
84 widthp, heightp, xhotp, yhotp);
89 * version that reads pixmap data as well as bitmap data
92 XmuLocatePixmapFile(Screen *screen, _Xconst char *name,
93 unsigned long fore, unsigned long back,
95 char *srcname, int srcnamelen,
96 int *widthp, int *heightp, int *xhotp, int *yhotp)
100 #define BITMAPDIR "/usr/include/X11/bitmaps"
103 Display *dpy = DisplayOfScreen (screen);
104 Window root = RootWindowOfScreen (screen);
105 Bool try_plain_name = True;
106 XmuCvtCache *cache = _XmuCCLookupDisplay (dpy);
107 char **file_paths = (char **) NULL;
108 char filename[PATH_MAX];
110 char* bitmapdir = BITMAPDIR;
112 unsigned int width, height;
117 * look in cache for bitmap path
120 if (!cache->string_to_bitmap.bitmapFilePath) {
122 XrmClass xrm_class[2];
123 XrmRepresentation rep_type;
126 xrm_name[0] = XrmPermStringToQuark ("bitmapFilePath");
127 xrm_name[1] = NULLQUARK;
128 xrm_class[0] = XrmPermStringToQuark ("BitmapFilePath");
129 xrm_class[1] = NULLQUARK;
130 if (!XrmGetDatabase(dpy)) {
131 /* what a hack; need to initialize it */
132 (void) XGetDefault (dpy, "", "");
134 if (XrmQGetResource (XrmGetDatabase(dpy), xrm_name, xrm_class,
135 &rep_type, &value) &&
136 rep_type == XrmPermStringToQuark("String")) {
137 cache->string_to_bitmap.bitmapFilePath =
138 split_path_string (value.addr);
141 file_paths = cache->string_to_bitmap.bitmapFilePath;
146 * 1. name if it begins with / or ./
147 * 2. "each prefix in file_paths"/name
149 * 4. name if didn't begin with / or .
152 for (i = 1; i <= 4; i++) {
153 const char *fn = filename;
159 if (!(name[0] == '/' || ((name[0] == '.') && name[1] == '/')))
162 try_plain_name = False;
165 if (file_paths && *file_paths) {
166 XmuSnprintf(filename, sizeof(filename),
167 "%s/%s", *file_paths, name);
174 XmuSnprintf(filename, sizeof(filename), "%s/%s", BITMAPDIR, name);
177 if (!try_plain_name) continue;
184 if (XmuReadBitmapDataFromFile (fn, &width, &height, &data,
185 &xhot, &yhot) == BitmapSuccess) {
186 pixmap = XCreatePixmapFromBitmapData (dpy, root, (char *) data,
189 XFree ((char *)data);
193 if (widthp) *widthp = (int)width;
194 if (heightp) *heightp = (int)height;
195 if (xhotp) *xhotp = xhot;
196 if (yhotp) *yhotp = yhot;
197 if (srcname && srcnamelen > 0) {
198 strncpy (srcname, fn, srcnamelen - 1);
199 srcname[srcnamelen - 1] = '\0';
210 * split_path_string - split a colon-separated list into its constituent
211 * parts; to release, free list[0] and list.
214 split_path_string(register char *src)
218 char **elemlist, **elem;
220 /* count the number of elements */
221 for (dst = src; *dst; dst++) if (*dst == ':') nelems++;
223 /* get memory for everything */
224 dst = (char *) malloc (dst - src + 1);
225 if (!dst) return NULL;
226 elemlist = (char **) calloc ((nelems + 1), sizeof (char *));
232 /* copy to new list and walk up nulling colons and setting list pointers */
234 for (elem = elemlist, src = dst; *src; src++) {
248 _XmuStringToBitmapInitCache(register XmuCvtCache *c)
250 c->string_to_bitmap.bitmapFilePath = NULL;
254 _XmuStringToBitmapFreeCache(register XmuCvtCache *c)
256 if (c->string_to_bitmap.bitmapFilePath) {
257 if (c->string_to_bitmap.bitmapFilePath[0])
258 free (c->string_to_bitmap.bitmapFilePath[0]);
259 free ((char *) (c->string_to_bitmap.bitmapFilePath));