1 /* This file contains compatibility routines for systems without Xmu.
2 * You would be better served by installing Xmu on your machine or
3 * yelling at your vendor to ship it.
6 /* XEmacs changes: rindex -> strrchr */
8 /* Synched up with: Not in FSF. */
14 * Copyright 1989 Massachusetts Institute of Technology
16 * Permission to use, copy, modify, and distribute this software and its
17 * documentation for any purpose and without fee is hereby granted, provided
18 * that the above copyright notice appear in all copies and that both that
19 * copyright notice and this permission notice appear in supporting
20 * documentation, and that the name of M.I.T. not be used in advertising
21 * or publicity pertaining to distribution of the software without specific,
22 * written prior permission. M.I.T. makes no representations about the
23 * suitability of this software for any purpose. It is provided "as is"
24 * without express or implied warranty.
26 * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
28 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
29 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
30 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
31 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
33 #include <X11/cursorfont.h>
35 #include <X11/Xutil.h>
36 #include <X11/Intrinsic.h>
38 /* for XmuCopyISOLatin1Lowered */
40 #include <X11/keysymdef.h>
43 #if (XtSpecificationRelease >= 5)
45 * Don't know why, but this works with X11R5, not X11R4.
46 * Anyway, _XExtension is defined in Xlib.h in X11R4, so we do not need
47 * Xlibint in that case...
49 #include <X11/Xlibint.h>
51 #include <X11/Xproto.h>
56 int XmuCursorNameToIndex (const char *name)
58 static const struct _CursorName {
62 {"x_cursor", XC_X_cursor},
64 {"based_arrow_down", XC_based_arrow_down},
65 {"based_arrow_up", XC_based_arrow_up},
67 {"bogosity", XC_bogosity},
68 {"bottom_left_corner", XC_bottom_left_corner},
69 {"bottom_right_corner", XC_bottom_right_corner},
70 {"bottom_side", XC_bottom_side},
71 {"bottom_tee", XC_bottom_tee},
72 {"box_spiral", XC_box_spiral},
73 {"center_ptr", XC_center_ptr},
74 {"circle", XC_circle},
76 {"coffee_mug", XC_coffee_mug},
78 {"cross_reverse", XC_cross_reverse},
79 {"crosshair", XC_crosshair},
80 {"diamond_cross", XC_diamond_cross},
82 {"dotbox", XC_dotbox},
83 {"double_arrow", XC_double_arrow},
84 {"draft_large", XC_draft_large},
85 {"draft_small", XC_draft_small},
86 {"draped_box", XC_draped_box},
87 {"exchange", XC_exchange},
89 {"gobbler", XC_gobbler},
95 {"iron_cross", XC_iron_cross},
96 {"left_ptr", XC_left_ptr},
97 {"left_side", XC_left_side},
98 {"left_tee", XC_left_tee},
99 {"leftbutton", XC_leftbutton},
100 {"ll_angle", XC_ll_angle},
101 {"lr_angle", XC_lr_angle},
103 {"middlebutton", XC_middlebutton},
105 {"pencil", XC_pencil},
106 {"pirate", XC_pirate},
108 {"question_arrow", XC_question_arrow},
109 {"right_ptr", XC_right_ptr},
110 {"right_side", XC_right_side},
111 {"right_tee", XC_right_tee},
112 {"rightbutton", XC_rightbutton},
113 {"rtl_logo", XC_rtl_logo},
114 {"sailboat", XC_sailboat},
115 {"sb_down_arrow", XC_sb_down_arrow},
116 {"sb_h_double_arrow", XC_sb_h_double_arrow},
117 {"sb_left_arrow", XC_sb_left_arrow},
118 {"sb_right_arrow", XC_sb_right_arrow},
119 {"sb_up_arrow", XC_sb_up_arrow},
120 {"sb_v_double_arrow", XC_sb_v_double_arrow},
121 {"shuttle", XC_shuttle},
122 {"sizing", XC_sizing},
123 {"spider", XC_spider},
124 {"spraycan", XC_spraycan},
126 {"target", XC_target},
127 {"tcross", XC_tcross},
128 {"top_left_arrow", XC_top_left_arrow},
129 {"top_left_corner", XC_top_left_corner},
130 {"top_right_corner", XC_top_right_corner},
131 {"top_side", XC_top_side},
132 {"top_tee", XC_top_tee},
134 {"ul_angle", XC_ul_angle},
135 {"umbrella", XC_umbrella},
136 {"ur_angle", XC_ur_angle},
140 const struct _CursorName *table;
144 if (strlen (name) >= sizeof tmp) return -1;
145 for (i=0; i<strlen(name); i++)
146 if (isupper((unsigned char) name[i]))
147 tmp[i] = tolower((unsigned char) name[i]);
152 for (i=0, table=cursor_names; i < XtNumber(cursor_names); i++, table++ ) {
153 if (strcmp(tmp, table->name) == 0) return table->shape;
161 * Based on an optimized version provided by Jim Becker, August 5, 1988.
167 /* shared data for the image read/parse logic */
168 static short hexTable[256]; /* conversion value */
169 static int hex_initialized; /* easier to fill in at run time */
173 * Table index for the hex values. Initialized once, first time.
174 * Used for translation value or delimiter significance lookup.
176 static void initHexTable (void)
179 * We build the table at run time for several reasons:
181 * 1. portable to non-ASCII machines.
182 * 2. still reentrant since we set the init flag after setting table.
183 * 3. easier to extend.
184 * 4. less prone to bugs.
186 hexTable['0'] = 0; hexTable['1'] = 1;
187 hexTable['2'] = 2; hexTable['3'] = 3;
188 hexTable['4'] = 4; hexTable['5'] = 5;
189 hexTable['6'] = 6; hexTable['7'] = 7;
190 hexTable['8'] = 8; hexTable['9'] = 9;
191 hexTable['A'] = 10; hexTable['B'] = 11;
192 hexTable['C'] = 12; hexTable['D'] = 13;
193 hexTable['E'] = 14; hexTable['F'] = 15;
194 hexTable['a'] = 10; hexTable['b'] = 11;
195 hexTable['c'] = 12; hexTable['d'] = 13;
196 hexTable['e'] = 14; hexTable['f'] = 15;
198 /* delimiters of significance are flagged w/ negative value */
199 hexTable[' '] = -1; hexTable[','] = -1;
200 hexTable['}'] = -1; hexTable['\n'] = -1;
207 * read next hex value in the input stream, return -1 if EOF
209 static int NextInt (FILE *fstream)
216 /* loop, accumulate hex value until find delimiter */
217 /* skip any initial delimiters found in read stream */
225 /* trim high bits, check type and accumulate */
227 if (isascii(ch) && isxdigit(ch)) {
228 value = (value << 4) + hexTable[ch];
230 } else if ((hexTable[ch]) < 0 && gotone)
239 * The data returned by the following routine is always in left-most byte
240 * first and left-most bit first. If it doesn't return BitmapSuccess then
241 * its arguments won't have been touched. This routine should look as much
242 * like the Xlib routine XReadBitmapfile as possible.
244 int XmuReadBitmapData (
245 FILE *fstream, /* handle on file */
246 unsigned int *width, /* RETURNED */
247 unsigned int *height, /* RETURNED */
248 unsigned char **datap, /* RETURNED */
249 int *x_hot, int *y_hot) /* RETURNED */
251 unsigned char *data = NULL; /* working variable */
252 char line[MAX_SIZE]; /* input line from file */
253 int size; /* number of bytes of data */
254 char name_and_type[MAX_SIZE]; /* an input line */
255 char *type; /* for parsing */
256 int value; /* from an input line */
257 int version10p; /* boolean, old format */
258 int padding; /* to handle alignment */
259 int bytes_per_line; /* per scanline of data */
260 unsigned int ww = 0; /* width */
261 unsigned int hh = 0; /* height */
262 int hx = -1; /* x hotspot */
263 int hy = -1; /* y hotspot */
266 #define Xmalloc(size) malloc(size)
269 /* first time initialization */
270 if (!hex_initialized) initHexTable();
272 /* error cleanup and return macro */
273 #define RETURN(code) { if (data) free (data); return code; }
275 while (fgets(line, MAX_SIZE, fstream)) {
276 if (strlen(line) == MAX_SIZE-1) {
277 RETURN (BitmapFileInvalid);
279 if (sscanf(line,"#define %s %d",name_and_type,&value) == 2) {
280 if (!(type = strrchr(name_and_type, '_')))
281 type = name_and_type;
285 if (!strcmp("width", type))
286 ww = (unsigned int) value;
287 if (!strcmp("height", type))
288 hh = (unsigned int) value;
289 if (!strcmp("hot", type)) {
290 if (type-- == name_and_type || type-- == name_and_type)
292 if (!strcmp("x_hot", type))
294 if (!strcmp("y_hot", type))
300 if (sscanf(line, "static short %s = {", name_and_type) == 1)
302 else if (sscanf(line,"static unsigned char %s = {",name_and_type) == 1)
304 else if (sscanf(line, "static char %s = {", name_and_type) == 1)
309 if (!(type = strrchr(name_and_type, '_')))
310 type = name_and_type;
314 if (strcmp("bits[]", type))
318 RETURN (BitmapFileInvalid);
320 if ((ww % 16) && ((ww % 16) < 9) && version10p)
325 bytes_per_line = (ww+7)/8 + padding;
327 size = bytes_per_line * hh;
328 data = (unsigned char *) Xmalloc ((unsigned int) size);
330 RETURN (BitmapNoMemory);
336 for (bytes=0, ptr=data; bytes<size; (bytes += 2)) {
337 if ((value = NextInt(fstream)) < 0)
338 RETURN (BitmapFileInvalid);
340 if (!padding || ((bytes+2) % bytes_per_line))
341 *(ptr++) = value >> 8;
347 for (bytes=0, ptr=data; bytes<size; bytes++, ptr++) {
348 if ((value = NextInt(fstream)) < 0)
349 RETURN (BitmapFileInvalid);
357 RETURN (BitmapFileInvalid);
364 if (x_hot) *x_hot = hx;
365 if (y_hot) *y_hot = hy;
367 RETURN (BitmapSuccess);
371 int XmuReadBitmapDataFromFile (const char *filename,
372 /* Remaining args are RETURNED */
374 unsigned int *height,
375 unsigned char **datap,
376 int *x_hot, int *y_hot)
381 if ((fstream = fopen (filename, "r")) == NULL) {
382 return BitmapOpenFailed;
384 status = XmuReadBitmapData (fstream, width, height, datap, x_hot, y_hot);
390 * XmuPrintDefaultErrorMessage - print a nice error that looks like the usual
391 * message. Return 1 if the caller should consider exiting, else 0.
393 int XmuPrintDefaultErrorMessage (Display *dpy, XErrorEvent *event, FILE *fp)
398 char *mtype = "XlibMessage";
399 _XExtension *ext = (_XExtension *)NULL;
400 XGetErrorText(dpy, event->error_code, buffer, BUFSIZ);
401 XGetErrorDatabaseText(dpy, mtype, "XError", "X Error", mesg, BUFSIZ);
402 fprintf(fp, "%s: %s\n ", mesg, buffer);
403 XGetErrorDatabaseText(dpy, mtype, "MajorCode", "Request Major code %d",
405 fprintf(fp, mesg, event->request_code);
406 if (event->request_code < 128) {
407 sprintf(number, "%d", event->request_code);
408 XGetErrorDatabaseText(dpy, "XRequest", number, "", buffer, BUFSIZ);
410 /* XXX this is non-portable */
411 for (ext = dpy->ext_procs;
412 ext && (ext->codes.major_opcode != event->request_code);
416 strcpy(buffer, ext->name);
420 fprintf(fp, " (%s)", buffer);
422 #if (XtSpecificationRelease >= 5)
423 if (event->request_code >= 128) {
424 XGetErrorDatabaseText(dpy, mtype, "MinorCode", "Request Minor code %d",
426 fprintf(fp, mesg, event->minor_code);
428 sprintf(mesg, "%s.%d", ext->name, event->minor_code);
429 XGetErrorDatabaseText(dpy, "XRequest", mesg, "", buffer, BUFSIZ);
430 fprintf(fp, " (%s)", buffer);
434 if (event->error_code >= 128) {
435 /* let extensions try to print the values */
436 /* XXX this is non-portable code */
437 for (ext = dpy->ext_procs; ext; ext = ext->next) {
438 if (ext->error_values)
439 (*ext->error_values)(dpy, event, fp);
441 /* the rest is a fallback, providing a simple default */
442 /* kludge, try to find the extension that caused it */
444 for (ext = dpy->ext_procs; ext; ext = ext->next) {
445 if (ext->error_string)
446 (*ext->error_string)(dpy, event->error_code, &ext->codes,
452 sprintf(buffer, "%s.%d", ext->name,
453 event->error_code - ext->codes.first_error);
455 strcpy(buffer, "Value");
456 XGetErrorDatabaseText(dpy, mtype, buffer, "", mesg, BUFSIZ);
458 fprintf(fp, mesg, event->resourceid);
461 } else if ((event->error_code == BadWindow) ||
462 (event->error_code == BadPixmap) ||
463 (event->error_code == BadCursor) ||
464 (event->error_code == BadFont) ||
465 (event->error_code == BadDrawable) ||
466 (event->error_code == BadColor) ||
467 (event->error_code == BadGC) ||
468 (event->error_code == BadIDChoice) ||
469 (event->error_code == BadValue) ||
470 (event->error_code == BadAtom)) {
471 if (event->error_code == BadValue)
472 XGetErrorDatabaseText(dpy, mtype, "Value", "Value 0x%x",
474 else if (event->error_code == BadAtom)
475 XGetErrorDatabaseText(dpy, mtype, "AtomID", "AtomID 0x%x",
478 XGetErrorDatabaseText(dpy, mtype, "ResourceID", "ResourceID 0x%x",
480 fprintf(fp, mesg, event->resourceid);
483 #elif (XtSpecificationRelease == 4)
484 XGetErrorDatabaseText(dpy, mtype, "MinorCode", "Request Minor code %d",
486 fprintf(fp, mesg, event->minor_code);
489 sprintf(mesg, "%s.%d", ext->name, event->minor_code);
490 XGetErrorDatabaseText(dpy, "XRequest", mesg, "", buffer, BUFSIZ);
491 fprintf(fp, " (%s)", buffer);
493 XGetErrorDatabaseText(dpy, mtype, "ResourceID", "ResourceID 0x%x",
495 fprintf(fp, mesg, event->resourceid);
498 ERROR! Unsupported release of X11
500 XGetErrorDatabaseText(dpy, mtype, "ErrorSerial", "Error Serial #%d",
502 fprintf(fp, mesg, event->serial);
504 XGetErrorDatabaseText(dpy, mtype, "CurrentSerial", "Current Serial #%d",
506 fprintf(fp, mesg, NextRequest(dpy)-1);
508 if (event->error_code == BadImplementation) return 0;
514 * XmuSimpleErrorHandler - ignore errors for XQueryTree, XGetWindowAttributes,
515 * and XGetGeometry; print a message for everything else. In all case, do
518 int XmuSimpleErrorHandler (Display *dpy, XErrorEvent *errorp)
520 switch (errorp->request_code) {
522 case X_GetWindowAttributes:
523 if (errorp->error_code == BadWindow) return 0;
526 if (errorp->error_code == BadDrawable) return 0;
529 /* got a "real" X error */
530 return XmuPrintDefaultErrorMessage (dpy, errorp, stderr);
533 void XmuCopyISOLatin1Lowered(char *dst, const char *src)
535 unsigned char *dest = (unsigned char *) dst;
536 unsigned char *source = (unsigned char *) src;
538 for ( ; *source; source++, dest++)
540 if ((*source >= XK_A) && (*source <= XK_Z))
541 *dest = *source + (XK_a - XK_A);
542 else if ((*source >= XK_Agrave) && (*source <= XK_Odiaeresis))
543 *dest = *source + (XK_agrave - XK_Agrave);
544 else if ((*source >= XK_Ooblique) && (*source <= XK_Thorn))
545 *dest = *source + (XK_oslash - XK_Ooblique);
551 #endif /* !HAVE_XMU */