00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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
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
00066
00067
00068
00069
00070 static char **split_path_string(char*);
00071
00072
00073
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
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
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
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
00146
00147
00148
00149
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
00219
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
00229 for (dst = src; *dst; dst++) if (*dst == ':') nelems++;
00230
00231
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
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 }