#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <X11/Xlib.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xresource.h>
#include <X11/Xutil.h>
#include <X11/Xmu/CvtCache.h>
#include <X11/Xmu/Drawing.h>
#include <X11/Xmu/SysUtil.h>
#ifndef X_NOT_POSIX
#ifdef _POSIX_SOURCE
#include <limits.h>
#else
#define _POSIX_SOURCE
#include <limits.h>
#undef _POSIX_SOURCE
#endif
#endif
#ifndef PATH_MAX
#ifdef WIN32
#define PATH_MAX 512
#else
#include <sys/param.h>
#endif
#ifndef PATH_MAX
#ifdef MAXPATHLEN
#define PATH_MAX MAXPATHLEN
#else
#define PATH_MAX 1024
#endif
#endif
#endif
static char **split_path_string(char*);
Pixmap
XmuLocateBitmapFile(Screen *screen, _Xconst char *name, char *srcname,
int srcnamelen, int *widthp, int *heightp,
int *xhotp, int *yhotp)
{
return XmuLocatePixmapFile (screen, name,
(unsigned long) 1, (unsigned long) 0,
(unsigned int) 1, srcname, srcnamelen,
widthp, heightp, xhotp, yhotp);
}
Pixmap
XmuLocatePixmapFile(Screen *screen, _Xconst char *name,
unsigned long fore, unsigned long back,
unsigned int depth,
char *srcname, int srcnamelen,
int *widthp, int *heightp, int *xhotp, int *yhotp)
{
#ifndef BITMAPDIR
#define BITMAPDIR "/usr/include/X11/bitmaps"
#endif
Display *dpy = DisplayOfScreen (screen);
Window root = RootWindowOfScreen (screen);
Bool try_plain_name = True;
XmuCvtCache *cache = _XmuCCLookupDisplay (dpy);
char **file_paths = (char **) NULL;
char filename[PATH_MAX];
#if 0
char* bitmapdir = BITMAPDIR;
#endif
unsigned int width, height;
int xhot, yhot;
int i;
if (cache) {
if (!cache->string_to_bitmap.bitmapFilePath) {
XrmName xrm_name[2];
XrmClass xrm_class[2];
XrmRepresentation rep_type;
XrmValue value;
xrm_name[0] = XrmPermStringToQuark ("bitmapFilePath");
xrm_name[1] = NULLQUARK;
xrm_class[0] = XrmPermStringToQuark ("BitmapFilePath");
xrm_class[1] = NULLQUARK;
if (!XrmGetDatabase(dpy)) {
(void) XGetDefault (dpy, "", "");
}
if (XrmQGetResource (XrmGetDatabase(dpy), xrm_name, xrm_class,
&rep_type, &value) &&
rep_type == XrmPermStringToQuark("String")) {
cache->string_to_bitmap.bitmapFilePath =
split_path_string (value.addr);
}
}
file_paths = cache->string_to_bitmap.bitmapFilePath;
}
for (i = 1; i <= 4; i++) {
char *fn = filename;
Pixmap pixmap;
unsigned char *data;
switch (i) {
case 1:
#ifndef __UNIXOS2__
if (!(name[0] == '/' || ((name[0] == '.') && name[1] == '/')))
#else
if (!(name[0] == '/' || (name[0] == '.' && name[1] == '/') ||
(isalpha(name[0]) && name[1] == ':')))
#endif
continue;
fn = (char *) name;
try_plain_name = False;
break;
case 2:
if (file_paths && *file_paths) {
XmuSnprintf(filename, sizeof(filename),
"%s/%s", *file_paths, name);
file_paths++;
i--;
break;
}
continue;
case 3:
XmuSnprintf(filename, sizeof(filename), "%s/%s", BITMAPDIR, name);
break;
case 4:
if (!try_plain_name) continue;
fn = (char *) name;
break;
}
data = NULL;
pixmap = None;
#ifdef __UNIXOS2__
fn = (char*)__XOS2RedirRoot(fn);
#endif
if (XmuReadBitmapDataFromFile (fn, &width, &height, &data,
&xhot, &yhot) == BitmapSuccess) {
pixmap = XCreatePixmapFromBitmapData (dpy, root, (char *) data,
width, height,
fore, back, depth);
XFree ((char *)data);
}
if (pixmap) {
if (widthp) *widthp = (int)width;
if (heightp) *heightp = (int)height;
if (xhotp) *xhotp = xhot;
if (yhotp) *yhotp = yhot;
if (srcname && srcnamelen > 0) {
strncpy (srcname, fn, srcnamelen - 1);
srcname[srcnamelen - 1] = '\0';
}
return pixmap;
}
}
return None;
}
static char **
split_path_string(register char *src)
{
int nelems = 1;
register char *dst;
char **elemlist, **elem;
for (dst = src; *dst; dst++) if (*dst == ':') nelems++;
dst = (char *) malloc (dst - src + 1);
if (!dst) return NULL;
elemlist = (char **) calloc ((nelems + 1), sizeof (char *));
if (!elemlist) {
free (dst);
return NULL;
}
strcpy (dst, src);
for (elem = elemlist, src = dst; *src; src++) {
if (*src == ':') {
*elem++ = dst;
*src = '\0';
dst = src + 1;
}
}
*elem = dst;
return elemlist;
}
void
_XmuStringToBitmapInitCache(register XmuCvtCache *c)
{
c->string_to_bitmap.bitmapFilePath = NULL;
}
void
_XmuStringToBitmapFreeCache(register XmuCvtCache *c)
{
if (c->string_to_bitmap.bitmapFilePath) {
if (c->string_to_bitmap.bitmapFilePath[0])
free (c->string_to_bitmap.bitmapFilePath[0]);
free ((char *) (c->string_to_bitmap.bitmapFilePath));
}
}