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++) {
160 if (!(name[0] == '/' || ((name[0] == '.') && name[1] == '/')))
162 if (!(name[0] == '/' || (name[0] == '.' && name[1] == '/') ||
163 (isalpha(name[0]) && name[1] == ':')))
167 try_plain_name = False;
170 if (file_paths && *file_paths) {
171 XmuSnprintf(filename, sizeof(filename),
172 "%s/%s", *file_paths, name);
179 XmuSnprintf(filename, sizeof(filename), "%s/%s", BITMAPDIR, name);
182 if (!try_plain_name) continue;
190 fn = (char*)__XOS2RedirRoot(fn);
192 if (XmuReadBitmapDataFromFile (fn, &width, &height, &data,
193 &xhot, &yhot) == BitmapSuccess) {
194 pixmap = XCreatePixmapFromBitmapData (dpy, root, (char *) data,
197 XFree ((char *)data);
201 if (widthp) *widthp = (int)width;
202 if (heightp) *heightp = (int)height;
203 if (xhotp) *xhotp = xhot;
204 if (yhotp) *yhotp = yhot;
205 if (srcname && srcnamelen > 0) {
206 strncpy (srcname, fn, srcnamelen - 1);
207 srcname[srcnamelen - 1] = '\0';
218 * split_path_string - split a colon-separated list into its constituent
219 * parts; to release, free list[0] and list.
222 split_path_string(register char *src)
226 char **elemlist, **elem;
228 /* count the number of elements */
229 for (dst = src; *dst; dst++) if (*dst == ':') nelems++;
231 /* get memory for everything */
232 dst = (char *) malloc (dst - src + 1);
233 if (!dst) return NULL;
234 elemlist = (char **) calloc ((nelems + 1), sizeof (char *));
240 /* copy to new list and walk up nulling colons and setting list pointers */
242 for (elem = elemlist, src = dst; *src; src++) {
256 _XmuStringToBitmapInitCache(register XmuCvtCache *c)
258 c->string_to_bitmap.bitmapFilePath = NULL;
262 _XmuStringToBitmapFreeCache(register XmuCvtCache *c)
264 if (c->string_to_bitmap.bitmapFilePath) {
265 if (c->string_to_bitmap.bitmapFilePath[0])
266 free (c->string_to_bitmap.bitmapFilePath[0]);
267 free ((char *) (c->string_to_bitmap.bitmapFilePath));