XEmacs 21.4.17 "Jumbo Shrimp".
[chise/xemacs-chise.git.1] / src / device-x.c
1 /* Device functions for X windows.
2    Copyright (C) 1994, 1995 Board of Trustees, University of Illinois.
3    Copyright (C) 1994, 1995 Free Software Foundation, Inc.
4
5 This file is part of XEmacs.
6
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
11
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING.  If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 /* Synched up with: Not in FSF. */
23
24 /* 7-8-00 !!#### This file needs definite Mule review. */
25
26 /* Original authors: Jamie Zawinski and the FSF */
27 /* Rewritten by Ben Wing and Chuck Thompson. */
28
29 #include <config.h>
30 #include "lisp.h"
31
32 #include "console-x.h"
33 #include "xintrinsicp.h"        /* CoreP.h needs this */
34 #include <X11/CoreP.h>          /* Numerous places access the fields of
35                                    a core widget directly.  We could
36                                    use XtGetValues(), but ... */
37 #include "xgccache.h"
38 #include <X11/Shell.h>
39 #include "xmu.h"
40 #include "glyphs-x.h"
41 #include "objects-x.h"
42
43 #include "buffer.h"
44 #include "elhash.h"
45 #include "events.h"
46 #include "faces.h"
47 #include "frame.h"
48 #include "redisplay.h"
49 #include "sysdep.h"
50 #include "window.h"
51
52 #include "sysfile.h"
53 #include "systime.h"
54
55 #if defined(HAVE_SHLIB) && defined(LWLIB_USES_ATHENA) && !defined(HAVE_ATHENA_3D)
56 #include "sysdll.h"
57 #endif /* HAVE_SHLIB and LWLIB_USES_ATHENA and not HAVE_ATHENA_3D */
58
59 #ifdef HAVE_OFFIX_DND
60 #include "offix.h"
61 #endif
62
63 Lisp_Object Vdefault_x_device;
64 #if defined(MULE) && (defined(LWLIB_MENUBARS_MOTIF) || defined(HAVE_XIM) || defined (USE_XFONTSET))
65 Lisp_Object Vx_app_defaults_directory;
66 #endif
67
68 /* Qdisplay in general.c */
69 Lisp_Object Qx_error;
70 Lisp_Object Qinit_pre_x_win, Qinit_post_x_win;
71
72 /* The application class of Emacs. */
73 Lisp_Object Vx_emacs_application_class;
74
75 Lisp_Object Vx_initial_argv_list; /* #### ugh! */
76
77 static XrmOptionDescRec emacs_options[] =
78 {
79   {"-geometry", ".geometry", XrmoptionSepArg, NULL},
80   {"-iconic", ".iconic", XrmoptionNoArg, "yes"},
81
82   {"-internal-border-width", "*EmacsFrame.internalBorderWidth", XrmoptionSepArg, NULL},
83   {"-ib",                    "*EmacsFrame.internalBorderWidth", XrmoptionSepArg, NULL},
84   {"-scrollbar-width",       "*EmacsFrame.scrollBarWidth",      XrmoptionSepArg, NULL},
85   {"-scrollbar-height",      "*EmacsFrame.scrollBarHeight",     XrmoptionSepArg, NULL},
86
87   {"-privatecolormap", ".privateColormap", XrmoptionNoArg,  "yes"},
88   {"-visual",   ".EmacsVisual",     XrmoptionSepArg, NULL},
89
90   /* #### Beware!  If the type of the shell changes, update this. */
91   {"-T",        "*TopLevelEmacsShell.title",    XrmoptionSepArg, NULL},
92   {"-wn",       "*TopLevelEmacsShell.title",    XrmoptionSepArg, NULL},
93   {"-title",    "*TopLevelEmacsShell.title",    XrmoptionSepArg, NULL},
94
95   {"-iconname", "*TopLevelEmacsShell.iconName", XrmoptionSepArg, NULL},
96   {"-in",       "*TopLevelEmacsShell.iconName", XrmoptionSepArg, NULL},
97   {"-mc",       "*pointerColor",                XrmoptionSepArg, NULL},
98   {"-cr",       "*cursorColor",                 XrmoptionSepArg, NULL},
99   {"-fontset",  "*FontSet",                     XrmoptionSepArg, NULL},
100 };
101
102 /* Functions to synchronize mirroring resources and specifiers */
103 int in_resource_setting;
104 \f
105 /************************************************************************/
106 /*                          helper functions                            */
107 /************************************************************************/
108
109 /* JH 97/11/25 removed the static declaration because I need it during setup in event-Xt... */
110 struct device * get_device_from_display_1 (Display *dpy);
111 struct device *
112 get_device_from_display_1 (Display *dpy)
113 {
114   Lisp_Object devcons, concons;
115
116   DEVICE_LOOP_NO_BREAK (devcons, concons)
117     {
118       struct device *d = XDEVICE (XCAR (devcons));
119       if (DEVICE_X_P (d) && DEVICE_X_DISPLAY (d) == dpy)
120         return d;
121     }
122
123   return 0;
124 }
125
126 struct device *
127 get_device_from_display (Display *dpy)
128 {
129   struct device *d = get_device_from_display_1 (dpy);
130
131 #if !defined(INFODOCK)
132 # define FALLBACK_RESOURCE_NAME "xemacs"
133 # else
134 # define FALLBACK_RESOURCE_NAME "infodock"
135 #endif
136
137   if (!d) {
138     /* This isn't one of our displays.  Let's crash? */
139     stderr_out
140       ("\n%s: Fatal X Condition.  Asked about display we don't own: \"%s\"\n",
141        (STRINGP (Vinvocation_name) ?
142         (char *) XSTRING_DATA (Vinvocation_name) : FALLBACK_RESOURCE_NAME),
143        DisplayString (dpy) ? DisplayString (dpy) : "???");
144     ABORT();
145   }
146
147 #undef FALLBACK_RESOURCE_NAME
148
149   return d;
150 }
151
152 struct device *
153 decode_x_device (Lisp_Object device)
154 {
155   XSETDEVICE (device, decode_device (device));
156   CHECK_X_DEVICE (device);
157   return XDEVICE (device);
158 }
159
160 static Display *
161 get_x_display (Lisp_Object device)
162 {
163   return DEVICE_X_DISPLAY (decode_x_device (device));
164 }
165
166 \f
167 /************************************************************************/
168 /*                    initializing an X connection                      */
169 /************************************************************************/
170
171 static struct device *device_being_initialized = NULL;
172
173 static void
174 allocate_x_device_struct (struct device *d)
175 {
176   d->device_data = xnew_and_zero (struct x_device);
177 }
178
179 static void
180 Xatoms_of_device_x (struct device *d)
181 {
182   Display *D = DEVICE_X_DISPLAY (d);
183
184   DEVICE_XATOM_WM_PROTOCOLS    (d) = XInternAtom (D, "WM_PROTOCOLS",    False);
185   DEVICE_XATOM_WM_DELETE_WINDOW(d) = XInternAtom (D, "WM_DELETE_WINDOW",False);
186   DEVICE_XATOM_WM_SAVE_YOURSELF(d) = XInternAtom (D, "WM_SAVE_YOURSELF",False);
187   DEVICE_XATOM_WM_TAKE_FOCUS   (d) = XInternAtom (D, "WM_TAKE_FOCUS",   False);
188   DEVICE_XATOM_WM_STATE        (d) = XInternAtom (D, "WM_STATE",        False);
189 }
190
191 static void
192 sanity_check_geometry_resource (Display *dpy)
193 {
194   char *app_name, *app_class, *s;
195   char buf1 [255], buf2 [255];
196   char *type;
197   XrmValue value;
198   XtGetApplicationNameAndClass (dpy, &app_name, &app_class);
199   strcpy (buf1, app_name);
200   strcpy (buf2, app_class);
201   for (s = buf1; *s; s++) if (*s == '.') *s = '_';
202   strcat (buf1, "._no_._such_._resource_.geometry");
203   strcat (buf2, "._no_._such_._resource_.Geometry");
204   if (XrmGetResource (XtDatabase (dpy), buf1, buf2, &type, &value) == True)
205     {
206       warn_when_safe (Qgeometry, Qerror,
207                       "\n"
208 "Apparently \"%s*geometry: %s\" or \"%s*geometry: %s\" was\n"
209 "specified in the resource database.  Specifying \"*geometry\" will make\n"
210 "XEmacs (and most other X programs) malfunction in obscure ways. (i.e.\n"
211 "the Xt or Xm libraries will probably crash, which is a very bad thing.)\n"
212 "You should always use \".geometry\" or \"*EmacsFrame.geometry\" instead.\n",
213                   app_name, (char *) value.addr,
214                   app_class, (char *) value.addr);
215       suppress_early_error_handler_backtrace = 1;
216       error ("Invalid geometry resource");
217     }
218 }
219
220 static void
221 x_init_device_class (struct device *d)
222 {
223   if (DEVICE_X_DEPTH(d) > 2)
224     {
225       switch (DEVICE_X_VISUAL(d)->class)
226         {
227         case StaticGray:
228         case GrayScale:
229           DEVICE_CLASS (d) = Qgrayscale;
230           break;
231         default:
232           DEVICE_CLASS (d) = Qcolor;
233         }
234     }
235   else
236     DEVICE_CLASS (d) = Qmono;
237 }
238
239 /*
240  * Figure out what application name to use for xemacs
241  *
242  * Since we have decomposed XtOpenDisplay into XOpenDisplay and
243  * XtDisplayInitialize, we no longer get this for free.
244  *
245  * If there is a `-name' argument in argv, use that.
246  * Otherwise use the last component of argv[0].
247  *
248  * I have removed the gratuitous use of getenv("RESOURCE_NAME")
249  * which was in X11R5, but left the matching of any prefix of `-name'.
250  * Finally, if all else fails, return `xemacs', as it is more
251  * appropriate (X11R5 returns `main').
252  */
253 static Extbyte *
254 compute_x_app_name (int argc, Extbyte **argv)
255 {
256   int i;
257   Extbyte *ptr;
258
259   for (i = 1; i < argc - 1; i++)
260     if (!strncmp(argv[i], "-name", max (2, strlen (argv[1]))))
261       return argv[i+1];
262
263   if (argc > 0 && argv[0] && *argv[0])
264     return (ptr = strrchr (argv[0], '/')) ? ++ptr : argv[0];
265
266   return "xemacs";
267 }
268
269 /*
270  * This function figures out whether the user has any resources of the
271  * form "XEmacs.foo" or "XEmacs*foo".
272  *
273  * Currently we only consult the display's global resources; to look
274  * for screen specific resources, we would need to also consult:
275  * xdefs = XScreenResourceString(ScreenOfDisplay(dpy, scrno));
276  */
277 static int
278 have_xemacs_resources_in_xrdb (Display *dpy)
279 {
280   char *xdefs, *key;
281   int len;
282
283 #ifdef INFODOCK
284   key = "InfoDock";
285 #else
286   key = "XEmacs";
287 #endif
288   len = strlen (key);
289
290   if (!dpy)
291     return 0;
292
293   xdefs = XResourceManagerString (dpy);      /* don't free - owned by X */
294   while (xdefs && *xdefs)
295     {
296       if (strncmp (xdefs, key, len) == 0  &&
297           (xdefs[len] == '*' || xdefs[len] == '.'))
298         return 1;
299
300       while (*xdefs && *xdefs++ != '\n')     /* find start of next entry.. */
301         ;
302     }
303
304   return 0;
305 }
306
307 /* Only the characters [-_A-Za-z0-9] are allowed in the individual
308    components of a resource.  Convert invalid characters to `-' */
309
310 static char valid_resource_char_p[256];
311
312 static void
313 validify_resource_component (char *str, size_t len)
314 {
315   for (; len; len--, str++)
316     if (!valid_resource_char_p[(unsigned char) (*str)])
317       *str = '-';
318 }
319
320 static void
321 Dynarr_add_validified_lisp_string (char_dynarr *cda, Lisp_Object str)
322 {
323   Bytecount len = XSTRING_LENGTH (str);
324   Dynarr_add_many (cda, (char *) XSTRING_DATA (str), len);
325   validify_resource_component (Dynarr_atp (cda, Dynarr_length (cda) - len), len);
326 }
327
328 #if 0
329 /* compare visual info for qsorting */
330 static int
331 x_comp_visual_info (const void *elem1, const void *elem2)
332 {
333   XVisualInfo *left, *right;
334
335   left = (XVisualInfo *)elem1;
336   right = (XVisualInfo *)elem2;
337
338   if ( left == NULL )
339     return -1;
340   if ( right == NULL )
341     return 1;
342
343   if ( left->depth > right->depth ) {
344     return 1;
345   }
346   else if ( left->depth == right->depth ) {
347     if ( left->colormap_size > right->colormap_size )
348       return 1;
349     if ( left->class > right->class )
350       return 1;
351     else if ( left->class < right->class )
352       return -1;
353     else
354       return 0;
355   }
356   else {
357     return -1;
358   }
359
360 }
361 #endif /* if 0 */
362
363 #define XXX_IMAGE_LIBRARY_IS_SOMEWHAT_BROKEN
364 static Visual *
365 x_try_best_visual_class (Screen *screen, int scrnum, int visual_class)
366 {
367   Display *dpy = DisplayOfScreen (screen);
368   XVisualInfo vi_in;
369   XVisualInfo *vi_out = NULL;
370   int out_count;
371
372   vi_in.class = visual_class;
373   vi_in.screen = scrnum;
374   vi_out = XGetVisualInfo (dpy, (VisualClassMask | VisualScreenMask),
375                            &vi_in, &out_count);
376   if ( vi_out )
377     {
378       int i, best;
379       Visual *visual;
380       for (i = 0, best = 0; i < out_count; i++)
381         /* It's better if it's deeper, or if it's the same depth with
382            more cells (does that ever happen?  Well, it could...)
383            NOTE: don't allow pseudo color to get larger than 8! */
384         if (((vi_out [i].depth > vi_out [best].depth) ||
385              ((vi_out [i].depth == vi_out [best].depth) &&
386               (vi_out [i].colormap_size > vi_out [best].colormap_size)))
387 #ifdef XXX_IMAGE_LIBRARY_IS_SOMEWHAT_BROKEN
388             /* For now, the image library doesn't like PseudoColor visuals
389                of depths other than 1 or 8.  Depths greater than 8 only occur
390                on machines which have TrueColor anyway, so probably we'll end
391                up using that (it is the one that `Best' would pick) but if a
392                PseudoColor visual is explicitly specified, pick the 8 bit one.
393             */
394             && (visual_class != PseudoColor ||
395                 vi_out [i].depth == 1 ||
396                 vi_out [i].depth == 8)
397 #endif
398
399             /* SGI has 30-bit deep visuals.  Ignore them.
400                 (We only have 24-bit data anyway.)
401               */
402             && (vi_out [i].depth <= 24)
403             )
404           best = i;
405       visual = vi_out[best].visual;
406       XFree ((char *) vi_out);
407       return visual;
408     }
409   else
410     return 0;
411 }
412
413 static int
414 x_get_visual_depth (Display *dpy, Visual *visual)
415 {
416   XVisualInfo vi_in;
417   XVisualInfo *vi_out;
418   int out_count, d;
419
420   vi_in.visualid = XVisualIDFromVisual (visual);
421   vi_out = XGetVisualInfo (dpy, /*VisualScreenMask|*/VisualIDMask,
422                            &vi_in, &out_count);
423   if (! vi_out) ABORT ();
424   d = vi_out [0].depth;
425   XFree ((char *) vi_out);
426   return d;
427 }
428
429 static Visual *
430 x_try_best_visual (Display *dpy, int scrnum)
431 {
432   Visual *visual = NULL;
433   Screen *screen = ScreenOfDisplay (dpy, scrnum);
434   if ((visual = x_try_best_visual_class (screen, scrnum, TrueColor))
435       && x_get_visual_depth (dpy, visual) >= 16 )
436     return visual;
437   if ((visual = x_try_best_visual_class (screen, scrnum, PseudoColor)))
438     return visual;
439   if ((visual = x_try_best_visual_class (screen, scrnum, TrueColor)))
440     return visual;
441 #ifdef DIRECTCOLOR_WORKS
442   if ((visual = x_try_best_visual_class (screen, scrnum, DirectColor)))
443     return visual;
444 #endif
445
446   visual = DefaultVisualOfScreen (screen);
447   if ( x_get_visual_depth (dpy, visual) >= 8 )
448     return visual;
449
450   if ((visual = x_try_best_visual_class (screen, scrnum, StaticGray)))
451     return visual;
452   if ((visual = x_try_best_visual_class (screen, scrnum, GrayScale)))
453     return visual;
454   return DefaultVisualOfScreen (screen);
455 }
456
457
458 static void
459 x_init_device (struct device *d, Lisp_Object props)
460 {
461   Lisp_Object display;
462   Lisp_Object device;
463   Display *dpy;
464   Widget app_shell;
465   int argc;
466   Extbyte **argv;
467   const char *app_class;
468   const char *app_name;
469   const char *disp_name;
470   Visual *visual = NULL;
471   int depth = 8;                /* shut up the compiler */
472   Colormap cmap;
473   int screen;
474   /* */
475   int best_visual_found = 0;
476
477 #if defined(HAVE_SHLIB) && defined(LWLIB_USES_ATHENA) && !defined(HAVE_ATHENA_3D)
478   /*
479    * In order to avoid the lossage with flat Athena widgets dynamically
480    * linking to one of the ThreeD variants, using the dynamic symbol helpers
481    * to look for symbols that shouldn't be there and refusing to run if they
482    * are seems a less toxic idea than having XEmacs crash when we try and
483    * use a subclass of a widget that has changed size.
484    *
485    * It's ugly, I know, and not going to work everywhere. It seems better to
486    * do our damnedest to try and tell the user what to expect rather than
487    * simply blow up though.
488    *
489    * All the ThreeD variants I have access to define the following function
490    * symbols in the shared library. The flat Xaw library does not define them:
491    *
492    * Xaw3dComputeBottomShadowRGB
493    * Xaw3dComputeTopShadowRGB
494    *
495    * So far only Linux has shown this problem. This seems to be portable to
496    * all the distributions (certainly all the ones I checked - Debian and
497    * Redhat)
498    *
499    * This will only work, sadly, with dlopen() -- the other dynamic linkers
500    * are simply not capable of doing what is needed. :/
501    */
502
503   {
504     /* Get a dll handle to the main process. */
505     dll_handle xaw_dll_handle = dll_open (NULL);
506
507     /* Did that fail?  If so, continue without error.
508      * We could die here but, well, that's unfriendly and all -- plus I feel
509      * better about some crashing somewhere rather than preventing a perfectly
510      * good configuration working just because dll_open failed.
511      */
512     if (xaw_dll_handle != NULL)
513       {
514         /* Look for the Xaw3d function */
515         dll_func xaw_function_handle =
516           dll_function (xaw_dll_handle, "Xaw3dComputeTopShadowRGB");
517
518         /* If we found it, warn the user in big, nasty, unfriendly letters */
519         if (xaw_function_handle != NULL)
520           {
521             warn_when_safe (Qdevice, Qerror, "\n"
522 "It seems that XEmacs is built dynamically linked to the flat Athena widget\n"
523 "library but it finds a 3D Athena variant with the same name at runtime.\n"
524 "\n"
525 "This WILL cause your XEmacs process to dump core at some point.\n"
526 "You should not continue to use this binary without resolving this issue.\n"
527 "\n"
528 "This can be solved with the xaw-wrappers package under Debian\n"
529 "(register XEmacs as incompatible with all 3d widget sets, see\n"
530 "update-xaw-wrappers(8) and .../doc/xaw-wrappers/README.packagers).  It\n"
531 "can be verified by checking the runtime path in /etc/ld.so.conf and by\n"
532 "using `ldd /path/to/xemacs' under other Linux distributions.  One\n"
533 "solution is to use LD_PRELOAD or LD_LIBRARY_PATH to force ld.so to\n"
534 "load the flat Athena widget library instead of the aliased 3D widget\n"
535 "library (see ld.so(8) for use of these environment variables).\n\n"
536                             );
537
538           }
539
540         /* Otherwise release the handle to the library
541          * No error catch here; I can't think of a way to recover anyhow.
542          */
543         dll_close (xaw_dll_handle);
544       }
545   }
546 #endif /* HAVE_SHLIB and LWLIB_USES_ATHENA and not HAVE_ATHENA_3D */
547
548
549   XSETDEVICE (device, d);
550   display = DEVICE_CONNECTION (d);
551
552   allocate_x_device_struct (d);
553
554   make_argc_argv (Vx_initial_argv_list, &argc, &argv);
555
556   LISP_STRING_TO_EXTERNAL (display, disp_name, Qctext);
557
558   /*
559    * Break apart the old XtOpenDisplay call into XOpenDisplay and
560    * XtDisplayInitialize so we can figure out whether there
561    * are any XEmacs resources in the resource database before
562    * we initialize Xt.  This is so we can automagically support
563    * both `Emacs' and `XEmacs' application classes.
564    */
565   slow_down_interrupts ();
566   /* May not be needed but XtOpenDisplay could not deal with signals here. */
567   device_being_initialized = d;
568   dpy = DEVICE_X_DISPLAY (d) = XOpenDisplay (disp_name);
569   device_being_initialized = NULL;
570   speed_up_interrupts ();
571
572   if (dpy == 0)
573     {
574       suppress_early_error_handler_backtrace = 1;
575       signal_simple_error ("X server not responding\n", display);
576     }
577
578   if (STRINGP (Vx_emacs_application_class) &&
579       XSTRING_LENGTH (Vx_emacs_application_class) > 0)
580     LISP_STRING_TO_EXTERNAL (Vx_emacs_application_class, app_class, Qctext);
581   else
582     {
583       app_class = (NILP (Vx_emacs_application_class)  &&
584                    have_xemacs_resources_in_xrdb (dpy))
585 #ifdef INFODOCK
586                   ? "InfoDock"
587 #else
588                   ? "XEmacs"
589 #endif
590                   : "Emacs";
591       /* need to update Vx_emacs_application_class: */
592       Vx_emacs_application_class = build_string (app_class);
593     }
594
595   slow_down_interrupts ();
596   /* May not be needed but XtOpenDisplay could not deal with signals here.
597      Yuck. */
598   XtDisplayInitialize (Xt_app_con, dpy, compute_x_app_name (argc, argv),
599                        app_class, emacs_options,
600                        XtNumber (emacs_options), &argc, (char **) argv);
601   speed_up_interrupts ();
602
603   screen = DefaultScreen (dpy);
604   if (NILP (Vdefault_x_device))
605     Vdefault_x_device = device;
606
607 #ifdef MULE
608 #if defined(LWLIB_MENUBARS_MOTIF) || defined(HAVE_XIM) || defined (USE_XFONTSET)
609   {
610     /* Read in locale-specific resources from
611        data-directory/app-defaults/$LANG/Emacs.
612        This is in addition to the standard app-defaults files, and
613        does not override resources defined elsewhere */
614     const char *data_dir;
615     char *path;
616     XrmDatabase db = XtDatabase (dpy); /* #### XtScreenDatabase(dpy) ? */
617     const char *locale = XrmLocaleOfDatabase (db);
618
619     if (STRINGP (Vx_app_defaults_directory) &&
620         XSTRING_LENGTH (Vx_app_defaults_directory) > 0)
621       {
622         LISP_STRING_TO_EXTERNAL (Vx_app_defaults_directory, data_dir, Qfile_name);
623         path = (char *)alloca (strlen (data_dir) + strlen (locale) + 7);
624         sprintf (path, "%s%s/Emacs", data_dir, locale);
625         if (!access (path, R_OK))
626           XrmCombineFileDatabase (path, &db, False);
627       }
628     else if (STRINGP (Vdata_directory) && XSTRING_LENGTH (Vdata_directory) > 0)
629       {
630         LISP_STRING_TO_EXTERNAL (Vdata_directory, data_dir, Qfile_name);
631         path = (char *)alloca (strlen (data_dir) + 13 + strlen (locale) + 7);
632         sprintf (path, "%sapp-defaults/%s/Emacs", data_dir, locale);
633         if (!access (path, R_OK))
634           XrmCombineFileDatabase (path, &db, False);
635       }
636  }
637 #endif /* LWLIB_MENUBARS_MOTIF or HAVE_XIM USE_XFONTSET */
638 #endif /* MULE */
639
640   if (NILP (DEVICE_NAME (d)))
641     DEVICE_NAME (d) = display;
642
643   /* We're going to modify the string in-place, so be a nice XEmacs */
644   DEVICE_NAME (d) = Fcopy_sequence (DEVICE_NAME (d));
645   /* colons and periods can't appear in individual elements of resource
646      strings */
647
648   XtGetApplicationNameAndClass (dpy, (char **) &app_name, (char **) &app_class);
649   /* search for a matching visual if requested by the user, or setup the display default */
650   {
651     int resource_name_length = max (sizeof (".emacsVisual"),
652                                     sizeof (".privateColormap"));
653     char *buf1 = alloca_array (char, strlen (app_name)  + resource_name_length);
654     char *buf2 = alloca_array (char, strlen (app_class) + resource_name_length);
655     char *type;
656     XrmValue value;
657
658     sprintf (buf1, "%s.emacsVisual", app_name);
659     sprintf (buf2, "%s.EmacsVisual", app_class);
660     if (XrmGetResource (XtDatabase (dpy), buf1, buf2, &type, &value) == True)
661       {
662         int cnt = 0;
663         int vis_class = PseudoColor;
664         XVisualInfo vinfo;
665         char *str = (char*) value.addr;
666
667 #define CHECK_VIS_CLASS(visual_class)                                   \
668  else if (memcmp (str, #visual_class, sizeof (#visual_class) - 1) == 0) \
669         cnt = sizeof (#visual_class) - 1, vis_class = visual_class
670
671         if (1)
672           ;
673         CHECK_VIS_CLASS (StaticGray);
674         CHECK_VIS_CLASS (StaticColor);
675         CHECK_VIS_CLASS (TrueColor);
676         CHECK_VIS_CLASS (GrayScale);
677         CHECK_VIS_CLASS (PseudoColor);
678         CHECK_VIS_CLASS (DirectColor);
679
680         if (cnt)
681           {
682             depth = atoi (str + cnt);
683             if (depth == 0)
684               {
685                 stderr_out ("Invalid Depth specification in %s... ignoring...\n", str);
686               }
687             else
688               {
689                 if (XMatchVisualInfo (dpy, screen, depth, vis_class, &vinfo))
690                   {
691                     visual = vinfo.visual;
692                   }
693                 else
694                   {
695                     stderr_out ("Can't match the requested visual %s... using defaults\n", str);
696                   }
697               }
698           }
699         else
700           {
701             stderr_out( "Invalid Visual specification in %s... ignoring.\n", str);
702           }
703       }
704     if (visual == NULL)
705       {
706         /*
707           visual = DefaultVisual(dpy, screen);
708           depth = DefaultDepth(dpy, screen);
709         */
710         visual = x_try_best_visual (dpy, screen);
711         depth = x_get_visual_depth (dpy, visual);
712         best_visual_found = (visual != DefaultVisual (dpy, screen));
713       }
714
715     /* If we've got the same visual as the default and it's PseudoColor,
716        check to see if the user specified that we need a private colormap */
717     if (visual == DefaultVisual (dpy, screen))
718       {
719         sprintf (buf1, "%s.privateColormap", app_name);
720         sprintf (buf2, "%s.PrivateColormap", app_class);
721         if ((visual->class == PseudoColor) &&
722             (XrmGetResource (XtDatabase (dpy), buf1, buf2, &type, &value) == True))
723           {
724              cmap = XCopyColormapAndFree (dpy, DefaultColormap (dpy, screen));
725           }
726         else
727           {
728             cmap = DefaultColormap (dpy, screen);
729           }
730       }
731     else
732       {
733         if ( best_visual_found )
734           {
735             cmap = XCreateColormap (dpy,  RootWindow (dpy, screen), visual, AllocNone);
736           }
737         else
738           {
739             /* We have to create a matching colormap anyway...
740                #### think about using standard colormaps (need the Xmu libs?) */
741             cmap = XCreateColormap(dpy, RootWindow(dpy, screen), visual, AllocNone);
742             XInstallColormap(dpy, cmap);
743           }
744       }
745   }
746
747   DEVICE_X_VISUAL   (d) = visual;
748   DEVICE_X_COLORMAP (d) = cmap;
749   DEVICE_X_DEPTH    (d) = depth;
750   validify_resource_component ((char *) XSTRING_DATA (DEVICE_NAME (d)),
751                                XSTRING_LENGTH (DEVICE_NAME (d)));
752
753   {
754     Arg al[3];
755     XtSetArg (al[0], XtNvisual,   visual);
756     XtSetArg (al[1], XtNdepth,    depth);
757     XtSetArg (al[2], XtNcolormap, cmap);
758
759     app_shell = XtAppCreateShell (NULL, app_class,
760                                   applicationShellWidgetClass,
761                                   dpy, al, countof (al));
762   }
763
764   DEVICE_XT_APP_SHELL (d) = app_shell;
765
766 #ifdef HAVE_XIM
767   XIM_init_device(d);
768 #endif /* HAVE_XIM */
769
770   /* Realize the app_shell so that its window exists for GC creation purposes,
771      and set it to the size of the root window for child placement purposes */
772   {
773     Arg al[5];
774     XtSetArg (al[0], XtNmappedWhenManaged, False);
775     XtSetArg (al[1], XtNx, 0);
776     XtSetArg (al[2], XtNy, 0);
777     XtSetArg (al[3], XtNwidth,  WidthOfScreen  (ScreenOfDisplay (dpy, screen)));
778     XtSetArg (al[4], XtNheight, HeightOfScreen (ScreenOfDisplay (dpy, screen)));
779     XtSetValues (app_shell, al, countof (al));
780     XtRealizeWidget (app_shell);
781   }
782
783 #ifdef HAVE_WMCOMMAND
784   {
785     int new_argc;
786     Extbyte **new_argv;
787     make_argc_argv (Vcommand_line_args, &new_argc, &new_argv);
788     XSetCommand (XtDisplay (app_shell), XtWindow (app_shell),
789                  (char **) new_argv, new_argc);
790     free_argc_argv (new_argv);
791   }
792 #endif /* HAVE_WMCOMMAND */
793
794
795 #ifdef HAVE_OFFIX_DND
796   DndInitialize ( app_shell );
797 #endif
798
799   Vx_initial_argv_list = make_arg_list (argc, argv);
800   free_argc_argv (argv);
801
802   DEVICE_X_WM_COMMAND_FRAME (d) = Qnil;
803
804   sanity_check_geometry_resource (dpy);
805
806   /* In event-Xt.c */
807   x_init_modifier_mapping (d);
808
809   DEVICE_INFD (d) = DEVICE_OUTFD (d) = ConnectionNumber (dpy);
810   init_baud_rate (d);
811   init_one_device (d);
812
813   DEVICE_X_GC_CACHE (d) = make_gc_cache (dpy, XtWindow(app_shell));
814   DEVICE_X_GRAY_PIXMAP (d) = None;
815   Xatoms_of_device_x (d);
816   Xatoms_of_select_x (d);
817   Xatoms_of_objects_x (d);
818   x_init_device_class (d);
819
820   /* Run the elisp side of the X device initialization. */
821   call0 (Qinit_pre_x_win);
822 }
823
824 static void
825 x_finish_init_device (struct device *d, Lisp_Object props)
826 {
827   call0 (Qinit_post_x_win);
828 }
829
830 static void
831 x_mark_device (struct device *d)
832 {
833   mark_object (DEVICE_X_WM_COMMAND_FRAME (d));
834   mark_object (DEVICE_X_DATA (d)->x_keysym_map_hash_table);
835 }
836
837 \f
838 /************************************************************************/
839 /*                       closing an X connection                        */
840 /************************************************************************/
841
842 static void
843 free_x_device_struct (struct device *d)
844 {
845   xfree (d->device_data);
846 }
847
848 static void
849 x_delete_device (struct device *d)
850 {
851   Lisp_Object device;
852   Display *display;
853 #ifdef FREE_CHECKING
854   extern void (*__free_hook) (void *);
855   int checking_free;
856 #endif
857
858   XSETDEVICE (device, d);
859   display = DEVICE_X_DISPLAY (d);
860
861   if (display)
862     {
863 #ifdef FREE_CHECKING
864       checking_free = (__free_hook != 0);
865
866       /* Disable strict free checking, to avoid bug in X library */
867       if (checking_free)
868         disable_strict_free_check ();
869 #endif
870
871       free_gc_cache (DEVICE_X_GC_CACHE (d));
872       if (DEVICE_X_DATA (d)->x_modifier_keymap)
873         XFreeModifiermap (DEVICE_X_DATA (d)->x_modifier_keymap);
874       if (DEVICE_X_DATA (d)->x_keysym_map)
875         XFree ((char *) DEVICE_X_DATA (d)->x_keysym_map);
876
877       if (DEVICE_XT_APP_SHELL (d))
878         {
879           XtDestroyWidget (DEVICE_XT_APP_SHELL (d));
880           DEVICE_XT_APP_SHELL (d) = NULL;
881         }
882
883       XtCloseDisplay (display);
884       DEVICE_X_DISPLAY (d) = 0;
885 #ifdef FREE_CHECKING
886       if (checking_free)
887         enable_strict_free_check ();
888 #endif
889     }
890
891   if (EQ (device, Vdefault_x_device))
892     {
893       Lisp_Object devcons, concons;
894       /* #### handle deleting last X device */
895       Vdefault_x_device = Qnil;
896       DEVICE_LOOP_NO_BREAK (devcons, concons)
897         {
898           if (DEVICE_X_P (XDEVICE (XCAR (devcons))) &&
899               !EQ (device, XCAR (devcons)))
900             {
901               Vdefault_x_device = XCAR (devcons);
902               goto double_break;
903             }
904         }
905     }
906  double_break:
907   free_x_device_struct (d);
908 }
909
910 \f
911 /************************************************************************/
912 /*                              handle X errors                         */
913 /************************************************************************/
914
915 const char *
916 x_event_name (int event_type)
917 {
918   static const char *events[] =
919   {
920     "0: ERROR!",
921     "1: REPLY",
922     "KeyPress",
923     "KeyRelease",
924     "ButtonPress",
925     "ButtonRelease",
926     "MotionNotify",
927     "EnterNotify",
928     "LeaveNotify",
929     "FocusIn",
930     "FocusOut",
931     "KeymapNotify",
932     "Expose",
933     "GraphicsExpose",
934     "NoExpose",
935     "VisibilityNotify",
936     "CreateNotify",
937     "DestroyNotify",
938     "UnmapNotify",
939     "MapNotify",
940     "MapRequest",
941     "ReparentNotify",
942     "ConfigureNotify",
943     "ConfigureRequest",
944     "GravityNotify",
945     "ResizeRequest",
946     "CirculateNotify",
947     "CirculateRequest",
948     "PropertyNotify",
949     "SelectionClear",
950     "SelectionRequest",
951     "SelectionNotify",
952     "ColormapNotify",
953     "ClientMessage",
954     "MappingNotify",
955     "LASTEvent"
956   };
957
958   if (event_type < 0 || event_type >= countof (events))
959     return NULL;
960   return events [event_type];
961 }
962
963 /* Handling errors.
964
965    If an X error occurs which we are not expecting, we have no alternative
966    but to print it to stderr.  It would be nice to stuff it into a pop-up
967    buffer, or to print it in the minibuffer, but that's not possible, because
968    one is not allowed to do any I/O on the display connection from an error
969    handler. The guts of Xlib expect these functions to either return or exit.
970
971    However, there are occasions when we might expect an error to reasonably
972    occur.  The interface to this is as follows:
973
974    Before calling some X routine which may error, call
975         expect_x_error (dpy);
976
977    Just after calling the X routine, call either:
978
979         x_error_occurred_p (dpy);
980
981    to ask whether an error happened (and was ignored), or:
982
983         signal_if_x_error (dpy, resumable_p);
984
985    which will call Fsignal() with args appropriate to the X error, if there
986    was one.  (Resumable_p is whether the debugger should be allowed to
987    continue from the call to signal.)
988
989    You must call one of these two routines immediately after calling the X
990    routine; think of them as bookends like BLOCK_INPUT and UNBLOCK_INPUT.
991  */
992
993 static int error_expected;
994 static int error_occurred;
995 static XErrorEvent last_error;
996
997 /* OVERKILL! */
998
999 #ifdef EXTERNAL_WIDGET
1000 static Lisp_Object
1001 x_error_handler_do_enqueue (Lisp_Object frame)
1002 {
1003   enqueue_magic_eval_event (io_error_delete_frame, frame);
1004   return Qt;
1005 }
1006
1007 static Lisp_Object
1008 x_error_handler_error (Lisp_Object data, Lisp_Object dummy)
1009 {
1010   return Qnil;
1011 }
1012 #endif /* EXTERNAL_WIDGET */
1013
1014 int
1015 x_error_handler (Display *disp, XErrorEvent *event)
1016 {
1017   if (error_expected)
1018     {
1019       error_expected = 0;
1020       error_occurred = 1;
1021       last_error = *event;
1022     }
1023   else
1024     {
1025 #ifdef EXTERNAL_WIDGET
1026       struct frame *f;
1027       struct device *d = get_device_from_display (disp);
1028
1029       if ((event->error_code == BadWindow ||
1030            event->error_code == BadDrawable)
1031           && ((f = x_any_window_to_frame (d, event->resourceid)) != 0))
1032         {
1033           Lisp_Object frame;
1034
1035         /* one of the windows comprising one of our frames has died.
1036            This occurs particularly with ExternalShell frames when the
1037            client that owns the ExternalShell's window dies.
1038
1039            We cannot do any I/O on the display connection so we need
1040            to enqueue an eval event so that the deletion happens
1041            later.
1042
1043            Furthermore, we need to trap any errors (out-of-memory) that
1044            may occur when Fenqueue_eval_event is called.
1045          */
1046
1047         if (f->being_deleted)
1048           return 0;
1049         XSETFRAME (frame, f);
1050         if (!NILP (condition_case_1 (Qerror, x_error_handler_do_enqueue,
1051                                      frame, x_error_handler_error, Qnil)))
1052           {
1053             f->being_deleted = 1;
1054             f->visible = 0;
1055           }
1056         return 0;
1057       }
1058 #endif /* EXTERNAL_WIDGET */
1059
1060 #if 0
1061       /* This ends up calling X, which isn't allowed in an X error handler
1062        */
1063       stderr_out ("\n%s: ",
1064                   (STRINGP (Vinvocation_name)
1065                    ? (char *) XSTRING_DATA (Vinvocation_name)
1066                    : "xemacs"));
1067 #endif
1068       XmuPrintDefaultErrorMessage (disp, event, stderr);
1069     }
1070   return 0;
1071 }
1072
1073 void
1074 expect_x_error (Display *dpy)
1075 {
1076   assert (!error_expected);
1077   XSync (dpy, 0);       /* handle pending errors before setting flag */
1078   error_expected = 1;
1079   error_occurred = 0;
1080 }
1081
1082 int
1083 x_error_occurred_p (Display *dpy)
1084 {
1085   int val;
1086   XSync (dpy, 0);       /* handle pending errors before setting flag */
1087   val = error_occurred;
1088   error_expected = 0;
1089   error_occurred = 0;
1090   return val;
1091 }
1092
1093 int
1094 signal_if_x_error (Display *dpy, int resumable_p)
1095 {
1096   char buf[1024];
1097   Lisp_Object data;
1098   if (! x_error_occurred_p (dpy))
1099     return 0;
1100   data = Qnil;
1101   sprintf (buf, "0x%X", (unsigned int) last_error.resourceid);
1102   data = Fcons (build_string (buf), data);
1103   {
1104     char num [32];
1105     sprintf (num, "%d", last_error.request_code);
1106     XGetErrorDatabaseText (last_error.display, "XRequest", num, "",
1107                            buf, sizeof (buf));
1108     if (! *buf)
1109       sprintf (buf, "Request-%d", last_error.request_code);
1110     data = Fcons (build_string (buf), data);
1111   }
1112   XGetErrorText (last_error.display, last_error.error_code, buf, sizeof (buf));
1113   data = Fcons (build_string (buf), data);
1114  again:
1115   Fsignal (Qx_error, data);
1116   if (! resumable_p) goto again;
1117   return 1;
1118 }
1119
1120 int
1121 x_IO_error_handler (Display *disp)
1122 {
1123   /* This function can GC */
1124   Lisp_Object dev;
1125   struct device *d = get_device_from_display_1 (disp);
1126
1127   if (!d)
1128     d = device_being_initialized;
1129
1130   assert (d != NULL);
1131   XSETDEVICE (dev, d);
1132
1133   if (NILP (find_nonminibuffer_frame_not_on_device (dev)))
1134     {
1135       /* We're going down. */
1136       stderr_out
1137         ("\n%s: Fatal I/O Error %d (%s) on display connection \"%s\"\n",
1138          (STRINGP (Vinvocation_name) ?
1139           (char *) XSTRING_DATA (Vinvocation_name) : "xemacs"),
1140          errno, strerror (errno), DisplayString (disp));
1141       stderr_out
1142         ("  after %lu requests (%lu known processed) with %d events remaining.\n",
1143          NextRequest (disp) - 1, LastKnownRequestProcessed (disp),
1144          QLength (disp));
1145       /* assert (!_Xdebug); */
1146     }
1147   else
1148     {
1149       warn_when_safe
1150         (Qx, Qcritical,
1151          "I/O Error %d (%s) on display connection\n"
1152          "  \"%s\" after after %lu requests (%lu known processed)\n"
1153          "  with %d events remaining.\n"
1154          "  Throwing to top level.\n",
1155          errno, strerror (errno), DisplayString (disp),
1156          NextRequest (disp) - 1, LastKnownRequestProcessed (disp),
1157          QLength (disp));
1158     }
1159
1160   /* According to X specs, we should not return from this function, or
1161      Xlib might just decide to exit().  So we mark the offending
1162      console for deletion and throw to top level.  */
1163   if (d)
1164     enqueue_magic_eval_event (io_error_delete_device, dev);
1165   DEVICE_X_BEING_DELETED (d) = 1;
1166   Fthrow (Qtop_level, Qnil);
1167
1168   return 0; /* not reached */
1169 }
1170
1171 DEFUN ("x-debug-mode", Fx_debug_mode, 1, 2, 0, /*
1172 With a true arg, make the connection to the X server synchronous.
1173 With false, make it asynchronous.  Synchronous connections are much slower,
1174 but are useful for debugging. (If you get X errors, make the connection
1175 synchronous, and use a debugger to set a breakpoint on `x_error_handler'.
1176 Your backtrace of the C stack will now be useful.  In asynchronous mode,
1177 the stack above `x_error_handler' isn't helpful because of buffering.)
1178 If DEVICE is not specified, the selected device is assumed.
1179
1180 Calling this function is the same as calling the C function `XSynchronize',
1181 or starting the program with the `-sync' command line argument.
1182 */
1183        (arg, device))
1184 {
1185   struct device *d = decode_x_device (device);
1186
1187   XSynchronize (DEVICE_X_DISPLAY (d), !NILP (arg));
1188
1189   if (!NILP (arg))
1190     message ("X connection is synchronous");
1191   else
1192     message ("X connection is asynchronous");
1193
1194   return arg;
1195 }
1196
1197 \f
1198 /************************************************************************/
1199 /*                             X resources                              */
1200 /************************************************************************/
1201
1202 #if 0 /* bah humbug.  The whole "widget == resource" stuff is such
1203          a crock of shit that I'm just going to ignore it all. */
1204
1205 /* If widget is NULL, we are retrieving device or global face data. */
1206
1207 static void
1208 construct_name_list (Display *display, Widget widget, char *fake_name,
1209                      char *fake_class, char *name, char *class)
1210 {
1211   char *stack [100][2];
1212   Widget this;
1213   int count = 0;
1214   char *name_tail, *class_tail;
1215
1216   if (widget)
1217     {
1218       for (this = widget; this; this = XtParent (this))
1219         {
1220           stack [count][0] = this->core.name;
1221           stack [count][1] = XtClass (this)->core_class.class_name;
1222           count++;
1223         }
1224       count--;
1225     }
1226   else if (fake_name && fake_class)
1227     {
1228       stack [count][0] = fake_name;
1229       stack [count][1] = fake_class;
1230       count++;
1231     }
1232
1233   /* The root widget is an application shell; resource lookups use the
1234      specified application name and application class in preference to
1235      the name/class of that widget (which is argv[0] / "ApplicationShell").
1236      Generally the app name and class will be argv[0] / "Emacs" but
1237      the former can be set via the -name command-line option, and the
1238      latter can be set by changing `x-emacs-application-class' in
1239      lisp/term/x-win.el.
1240    */
1241   XtGetApplicationNameAndClass (display,
1242                                 &stack [count][0],
1243                                 &stack [count][1]);
1244
1245   name [0] = 0;
1246   class [0] = 0;
1247
1248   name_tail  = name;
1249   class_tail = class;
1250   for (; count >= 0; count--)
1251     {
1252       strcat (name_tail,  stack [count][0]);
1253       for (; *name_tail; name_tail++)
1254         if (*name_tail == '.') *name_tail = '_';
1255       strcat (name_tail, ".");
1256       name_tail++;
1257
1258       strcat (class_tail, stack [count][1]);
1259       for (; *class_tail; class_tail++)
1260         if (*class_tail == '.') *class_tail = '_';
1261       strcat (class_tail, ".");
1262       class_tail++;
1263     }
1264 }
1265
1266 #endif /* 0 */
1267
1268 /* strcasecmp() is not sufficiently portable or standard,
1269    and it's easier just to write our own. */
1270 static int
1271 ascii_strcasecmp (const char *s1, const char *s2)
1272 {
1273   while (1)
1274     {
1275       char c1 = *s1++;
1276       char c2 = *s2++;
1277       if (c1 >= 'A' && c1 <= 'Z') c1 += 'a' - 'A';
1278       if (c2 >= 'A' && c2 <= 'Z') c2 += 'a' - 'A';
1279       if (c1 != c2) return c1 - c2;
1280       if (c1 == '\0') return 0;
1281     }
1282 }
1283
1284 static char_dynarr *name_char_dynarr;
1285 static char_dynarr *class_char_dynarr;
1286
1287 /* Given a locale and device specification from x-get-resource or
1288 x-get-resource-prefix, return the resource prefix and display to
1289 fetch the resource on. */
1290
1291 static void
1292 x_get_resource_prefix (Lisp_Object locale, Lisp_Object device,
1293                        Display **display_out, char_dynarr *name,
1294                        char_dynarr *class)
1295 {
1296   if (NILP (locale))
1297     locale = Qglobal;
1298   if (NILP (Fvalid_specifier_locale_p (locale)))
1299     signal_simple_error ("Invalid locale", locale);
1300   if (WINDOWP (locale))
1301     /* #### I can't come up with any coherent way of naming windows.
1302        By relative position?  That seems tricky because windows
1303        can change position, be split, etc.  By order of creation?
1304        That seems less than useful. */
1305     signal_simple_error ("Windows currently can't be resourced", locale);
1306
1307   if (!NILP (device) && !DEVICEP (device))
1308     CHECK_DEVICE (device);
1309   if (DEVICEP (device) && !DEVICE_X_P (XDEVICE (device)))
1310     device = Qnil;
1311   if (NILP (device))
1312     {
1313       device = DFW_DEVICE (locale);
1314       if (DEVICEP (device) && !DEVICE_X_P (XDEVICE (device)))
1315         device = Qnil;
1316       if (NILP (device))
1317         device = Vdefault_x_device;
1318       if (NILP (device))
1319         {
1320           *display_out = 0;
1321           return;
1322         }
1323     }
1324
1325   *display_out = DEVICE_X_DISPLAY (XDEVICE (device));
1326
1327   {
1328     char *appname, *appclass;
1329     int name_len, class_len;
1330     XtGetApplicationNameAndClass (*display_out, &appname, &appclass);
1331     name_len  = strlen (appname);
1332     class_len = strlen (appclass);
1333     Dynarr_add_many (name , appname,  name_len);
1334     Dynarr_add_many (class, appclass, class_len);
1335     validify_resource_component (Dynarr_atp (name,  0), name_len);
1336     validify_resource_component (Dynarr_atp (class, 0), class_len);
1337   }
1338
1339   if (EQ (locale, Qglobal))
1340     return;
1341   if (BUFFERP (locale))
1342     {
1343       Dynarr_add_literal_string (name, ".buffer.");
1344       /* we know buffer is live; otherwise we got an error above. */
1345       Dynarr_add_validified_lisp_string (name, Fbuffer_name (locale));
1346       Dynarr_add_literal_string (class, ".EmacsLocaleType.EmacsBuffer");
1347     }
1348   else if (FRAMEP (locale))
1349     {
1350       Dynarr_add_literal_string (name, ".frame.");
1351       /* we know frame is live; otherwise we got an error above. */
1352       Dynarr_add_validified_lisp_string (name, Fframe_name (locale));
1353       Dynarr_add_literal_string (class, ".EmacsLocaleType.EmacsFrame");
1354     }
1355   else
1356     {
1357       assert (DEVICEP (locale));
1358       Dynarr_add_literal_string (name, ".device.");
1359       /* we know device is live; otherwise we got an error above. */
1360       Dynarr_add_validified_lisp_string (name, Fdevice_name (locale));
1361       Dynarr_add_literal_string (class, ".EmacsLocaleType.EmacsDevice");
1362     }
1363   return;
1364 }
1365
1366 DEFUN ("x-get-resource", Fx_get_resource, 3, 6, 0, /*
1367 Retrieve an X resource from the resource manager.
1368
1369 The first arg is the name of the resource to retrieve, such as "font".
1370 The second arg is the class of the resource to retrieve, such as "Font".
1371 The third arg must be one of the symbols 'string, 'integer, 'natnum, or
1372   'boolean, specifying the type of object that the database is searched for.
1373 The fourth arg is the locale to search for the resources on, and can
1374   currently be a buffer, a frame, a device, or 'global.  If omitted, it
1375   defaults to 'global.
1376 The fifth arg is the device to search for the resources on. (The resource
1377   database for a particular device is constructed by combining non-device-
1378   specific resources such as any command-line resources specified and any
1379   app-defaults files found [or the fallback resources supplied by XEmacs,
1380   if no app-defaults file is found] with device-specific resources such as
1381   those supplied using xrdb.) If omitted, it defaults to the device of
1382   LOCALE, if a device can be derived (i.e. if LOCALE is a frame or device),
1383   and otherwise defaults to the value of `default-x-device'.
1384 The sixth arg NOERROR, if non-nil, means do not signal an error if a
1385   bogus resource specification was retrieved (e.g. if a non-integer was
1386   given when an integer was requested).  In this case, a warning is issued
1387   instead, unless NOERROR is t, in which case no warning is issued.
1388
1389 The resource names passed to this function are looked up relative to the
1390 locale.
1391
1392 If you want to search for a subresource, you just need to specify the
1393 resource levels in NAME and CLASS.  For example, NAME could be
1394 "modeline.attributeFont", and CLASS "Face.AttributeFont".
1395
1396 Specifically,
1397
1398 1) If LOCALE is a buffer, a call
1399
1400     (x-get-resource "foreground" "Foreground" 'string SOME-BUFFER)
1401
1402 is an interface to a C call something like
1403
1404     XrmGetResource (db, "xemacs.buffer.BUFFER-NAME.foreground",
1405                         "Emacs.EmacsLocaleType.EmacsBuffer.Foreground",
1406                         "String");
1407
1408 2) If LOCALE is a frame, a call
1409
1410     (x-get-resource "foreground" "Foreground" 'string SOME-FRAME)
1411
1412 is an interface to a C call something like
1413
1414     XrmGetResource (db, "xemacs.frame.FRAME-NAME.foreground",
1415                         "Emacs.EmacsLocaleType.EmacsFrame.Foreground",
1416                         "String");
1417
1418 3) If LOCALE is a device, a call
1419
1420     (x-get-resource "foreground" "Foreground" 'string SOME-DEVICE)
1421
1422 is an interface to a C call something like
1423
1424     XrmGetResource (db, "xemacs.device.DEVICE-NAME.foreground",
1425                         "Emacs.EmacsLocaleType.EmacsDevice.Foreground",
1426                         "String");
1427
1428 4) If LOCALE is 'global, a call
1429
1430     (x-get-resource "foreground" "Foreground" 'string 'global)
1431
1432 is an interface to a C call something like
1433
1434     XrmGetResource (db, "xemacs.foreground",
1435                         "Emacs.Foreground",
1436                         "String");
1437
1438 Note that for 'global, no prefix is added other than that of the
1439 application itself; thus, you can use this locale to retrieve
1440 arbitrary application resources, if you really want to.
1441
1442 The returned value of this function is nil if the queried resource is not
1443 found.  If the third arg is `string', a string is returned, and if it is
1444 `integer', an integer is returned.  If the third arg is `boolean', then the
1445 returned value is the list (t) for true, (nil) for false, and is nil to
1446 mean ``unspecified''.
1447 */
1448        (name, class, type, locale, device, noerror))
1449 {
1450   char* name_string, *class_string;
1451   char *raw_result;
1452   XrmDatabase db;
1453   Display *display;
1454   Error_behavior errb = decode_error_behavior_flag (noerror);
1455
1456   CHECK_STRING (name);
1457   CHECK_STRING (class);
1458   CHECK_SYMBOL (type);
1459
1460   Dynarr_reset (name_char_dynarr);
1461   Dynarr_reset (class_char_dynarr);
1462
1463   x_get_resource_prefix (locale, device, &display,
1464                          name_char_dynarr, class_char_dynarr);
1465   if (!display)
1466     return Qnil;
1467
1468   db = XtDatabase (display);
1469
1470   Dynarr_add (name_char_dynarr, '.');
1471   Dynarr_add_lisp_string (name_char_dynarr, name);
1472   Dynarr_add (class_char_dynarr, '.');
1473   Dynarr_add_lisp_string (class_char_dynarr, class);
1474   Dynarr_add (name_char_dynarr,  '\0');
1475   Dynarr_add (class_char_dynarr, '\0');
1476
1477   name_string  = Dynarr_atp (name_char_dynarr,  0);
1478   class_string = Dynarr_atp (class_char_dynarr, 0);
1479
1480   {
1481     XrmValue xrm_value;
1482     XrmName namelist[100];
1483     XrmClass classlist[100];
1484     XrmName *namerest = namelist;
1485     XrmClass *classrest = classlist;
1486     XrmRepresentation xrm_type;
1487     XrmRepresentation string_quark;
1488     int result;
1489     XrmStringToNameList (name_string, namelist);
1490     XrmStringToClassList (class_string, classlist);
1491     string_quark = XrmStringToQuark ("String");
1492
1493     /* ensure that they have the same length */
1494     while (namerest[0] && classrest[0])
1495       namerest++, classrest++;
1496     if (namerest[0] || classrest[0])
1497       signal_simple_error_2
1498         ("class list and name list must be the same length", name, class);
1499     result = XrmQGetResource (db, namelist, classlist, &xrm_type, &xrm_value);
1500
1501     if (result != True || xrm_type != string_quark)
1502       return Qnil;
1503     raw_result = (char *) xrm_value.addr;
1504   }
1505
1506   if (EQ (type, Qstring))
1507     return build_string (raw_result);
1508   else if (EQ (type, Qboolean))
1509     {
1510       if (!ascii_strcasecmp (raw_result, "off")   ||
1511           !ascii_strcasecmp (raw_result, "false") ||
1512           !ascii_strcasecmp (raw_result, "no"))
1513         return Fcons (Qnil, Qnil);
1514       if (!ascii_strcasecmp (raw_result, "on")   ||
1515           !ascii_strcasecmp (raw_result, "true") ||
1516           !ascii_strcasecmp (raw_result, "yes"))
1517         return Fcons (Qt, Qnil);
1518       return maybe_continuable_error
1519         (Qresource, errb,
1520          "can't convert %s: %s to a Boolean", name_string, raw_result);
1521     }
1522   else if (EQ (type, Qinteger) || EQ (type, Qnatnum))
1523     {
1524       int i;
1525       char c;
1526       if (1 != sscanf (raw_result, "%d%c", &i, &c))
1527         return maybe_continuable_error
1528           (Qresource, errb,
1529            "can't convert %s: %s to an integer", name_string, raw_result);
1530       else if (EQ (type, Qnatnum) && i < 0)
1531         return maybe_continuable_error
1532           (Qresource, errb,
1533            "invalid numerical value %d for resource %s", i, name_string);
1534       else
1535         return make_int (i);
1536     }
1537   else
1538     {
1539       return maybe_signal_continuable_error
1540         (Qwrong_type_argument,
1541          list2 (build_translated_string
1542                 ("should be string, integer, natnum or boolean"),
1543                 type),
1544          Qresource, errb);
1545     }
1546 }
1547
1548 DEFUN ("x-get-resource-prefix", Fx_get_resource_prefix, 1, 2, 0, /*
1549 Return the resource prefix for LOCALE on DEVICE.
1550 The resource prefix is the strings used to prefix resources if
1551 the LOCALE and DEVICE arguments were passed to `x-get-resource'.
1552 The returned value is a cons of a name prefix and a class prefix.
1553 For example, if LOCALE is a frame, the returned value might be
1554 \("xemacs.frame.FRAME-NAME" . "Emacs.EmacsLocaleType.EmacsFrame").
1555 If no valid X device for resourcing can be obtained, this function
1556 returns nil. (In such a case, `x-get-resource' would always return nil.)
1557 */
1558        (locale, device))
1559 {
1560   Display *display;
1561
1562   Dynarr_reset (name_char_dynarr );
1563   Dynarr_reset (class_char_dynarr);
1564
1565   x_get_resource_prefix (locale, device, &display,
1566                          name_char_dynarr, class_char_dynarr);
1567   if (!display)
1568     return Qnil;
1569
1570   return Fcons (make_string ((Bufbyte *) Dynarr_atp (name_char_dynarr, 0),
1571                              Dynarr_length (name_char_dynarr)),
1572                 make_string ((Bufbyte *) Dynarr_atp (class_char_dynarr, 0),
1573                              Dynarr_length (class_char_dynarr)));
1574 }
1575
1576 DEFUN ("x-put-resource", Fx_put_resource, 1, 2, 0, /*
1577 Add a resource to the resource database for DEVICE.
1578 RESOURCE-LINE specifies the resource to add and should be a
1579 standard resource specification.
1580 */
1581        (resource_line, device))
1582 {
1583   struct device *d = decode_device (device);
1584   char *str, *colon_pos;
1585
1586   CHECK_STRING (resource_line);
1587   str = (char *) XSTRING_DATA (resource_line);
1588   if (!(colon_pos = strchr (str, ':')) || strchr (str, '\n'))
1589   invalid:
1590     signal_simple_error ("Invalid resource line", resource_line);
1591   if (strspn (str,
1592               /* Only the following chars are allowed before the colon */
1593               " \t.*?abcdefghijklmnopqrstuvwxyz"
1594               "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-")
1595       != (size_t) (colon_pos - str))
1596     goto invalid;
1597
1598   if (DEVICE_X_P (d))
1599     {
1600       XrmDatabase db = XtDatabase (DEVICE_X_DISPLAY (d));
1601       XrmPutLineResource (&db, str);
1602     }
1603
1604   return Qnil;
1605 }
1606
1607 \f
1608 /************************************************************************/
1609 /*                   display information functions                      */
1610 /************************************************************************/
1611
1612 DEFUN ("default-x-device", Fdefault_x_device, 0, 0, 0, /*
1613 Return the default X device for resourcing.
1614 This is the first-created X device that still exists.
1615 */
1616        ())
1617 {
1618   return Vdefault_x_device;
1619 }
1620
1621 DEFUN ("x-display-visual-class", Fx_display_visual_class, 0, 1, 0, /*
1622 Return the visual class of the X display DEVICE is using.
1623 This can be altered from the default at startup using the XResource "EmacsVisual".
1624 The returned value will be one of the symbols `static-gray', `gray-scale',
1625 `static-color', `pseudo-color', `true-color', or `direct-color'.
1626 */
1627        (device))
1628 {
1629   Visual *vis = DEVICE_X_VISUAL (decode_x_device (device));
1630   switch (vis->class)
1631     {
1632     case StaticGray:  return intern ("static-gray");
1633     case GrayScale:   return intern ("gray-scale");
1634     case StaticColor: return intern ("static-color");
1635     case PseudoColor: return intern ("pseudo-color");
1636     case TrueColor:   return intern ("true-color");
1637     case DirectColor: return intern ("direct-color");
1638     default:
1639       error ("display has an unknown visual class");
1640       return Qnil;      /* suppress compiler warning */
1641     }
1642 }
1643
1644 DEFUN ("x-display-visual-depth", Fx_display_visual_depth, 0, 1, 0, /*
1645 Return the bitplane depth of the visual the X display DEVICE is using.
1646 */
1647        (device))
1648 {
1649    return make_int (DEVICE_X_DEPTH (decode_x_device (device)));
1650 }
1651
1652 static Lisp_Object
1653 x_device_system_metrics (struct device *d,
1654                          enum device_metrics m)
1655 {
1656   Display *dpy = DEVICE_X_DISPLAY (d);
1657
1658   switch (m)
1659     {
1660     case DM_size_device:
1661       return Fcons (make_int (DisplayWidth (dpy, DefaultScreen (dpy))),
1662                     make_int (DisplayHeight (dpy, DefaultScreen (dpy))));
1663     case DM_size_device_mm:
1664       return Fcons (make_int (DisplayWidthMM (dpy, DefaultScreen (dpy))),
1665                     make_int (DisplayHeightMM (dpy, DefaultScreen (dpy))));
1666     case DM_num_bit_planes:
1667       return make_int (DisplayPlanes (dpy, DefaultScreen (dpy)));
1668     case DM_num_color_cells:
1669       return make_int (DisplayCells (dpy, DefaultScreen (dpy)));
1670     default: /* No such device metric property for X devices  */
1671       return Qunbound;
1672     }
1673 }
1674
1675 DEFUN ("x-server-vendor", Fx_server_vendor, 0, 1, 0, /*
1676 Return the vendor ID string of the X server DEVICE is on.
1677 Return the empty string if the vendor ID string cannot be determined.
1678 */
1679        (device))
1680 {
1681   Display *dpy = get_x_display (device);
1682   char *vendor = ServerVendor (dpy);
1683
1684   return build_string (vendor ? vendor : "");
1685 }
1686
1687 DEFUN ("x-server-version", Fx_server_version, 0, 1, 0, /*
1688 Return the version numbers of the X server DEVICE is on.
1689 The returned value is a list of three integers: the major and minor
1690 version numbers of the X Protocol in use, and the vendor-specific release
1691 number.  See also `x-server-vendor'.
1692 */
1693        (device))
1694 {
1695   Display *dpy = get_x_display (device);
1696
1697   return list3 (make_int (ProtocolVersion  (dpy)),
1698                 make_int (ProtocolRevision (dpy)),
1699                 make_int (VendorRelease    (dpy)));
1700 }
1701
1702 DEFUN ("x-valid-keysym-name-p", Fx_valid_keysym_name_p, 1, 1, 0, /*
1703 Return true if KEYSYM names a keysym that the X library knows about.
1704 Valid keysyms are listed in the files /usr/include/X11/keysymdef.h and in
1705 /usr/lib/X11/XKeysymDB, or whatever the equivalents are on your system.
1706 */
1707        (keysym))
1708 {
1709   const char *keysym_ext;
1710
1711   CHECK_STRING (keysym);
1712   LISP_STRING_TO_EXTERNAL (keysym, keysym_ext, Qctext);
1713
1714   return XStringToKeysym (keysym_ext) ? Qt : Qnil;
1715 }
1716
1717 DEFUN ("x-keysym-hash-table", Fx_keysym_hash_table, 0, 1, 0, /*
1718 Return a hash table containing a key for all keysyms on DEVICE.
1719 DEVICE must be an X11 display device.  See `x-keysym-on-keyboard-p'.
1720 */
1721        (device))
1722 {
1723   struct device *d = decode_device (device);
1724   if (!DEVICE_X_P (d))
1725     signal_simple_error ("Not an X device", device);
1726
1727   return DEVICE_X_DATA (d)->x_keysym_map_hash_table;
1728 }
1729
1730 DEFUN ("x-keysym-on-keyboard-sans-modifiers-p", Fx_keysym_on_keyboard_sans_modifiers_p,
1731        1, 2, 0, /*
1732 Return true if KEYSYM names a key on the keyboard of DEVICE.
1733 More precisely, return true if pressing a physical key
1734 on the keyboard of DEVICE without any modifier keys generates KEYSYM.
1735 Valid keysyms are listed in the files /usr/include/X11/keysymdef.h and in
1736 /usr/lib/X11/XKeysymDB, or whatever the equivalents are on your system.
1737 The keysym name can be provided in two forms:
1738 - if keysym is a string, it must be the name as known to X windows.
1739 - if keysym is a symbol, it must be the name as known to XEmacs.
1740 The two names differ in capitalization and underscoring.
1741 */
1742        (keysym, device))
1743 {
1744   struct device *d = decode_device (device);
1745   if (!DEVICE_X_P (d))
1746     signal_simple_error ("Not an X device", device);
1747
1748   return (EQ (Qsans_modifiers,
1749               Fgethash (keysym, DEVICE_X_KEYSYM_MAP_HASH_TABLE (d), Qnil)) ?
1750           Qt : Qnil);
1751 }
1752
1753
1754 DEFUN ("x-keysym-on-keyboard-p", Fx_keysym_on_keyboard_p, 1, 2, 0, /*
1755 Return true if KEYSYM names a key on the keyboard of DEVICE.
1756 More precisely, return true if some keystroke (possibly including modifiers)
1757 on the keyboard of DEVICE keys generates KEYSYM.
1758 Valid keysyms are listed in the files /usr/include/X11/keysymdef.h and in
1759 /usr/lib/X11/XKeysymDB, or whatever the equivalents are on your system.
1760 The keysym name can be provided in two forms:
1761 - if keysym is a string, it must be the name as known to X windows.
1762 - if keysym is a symbol, it must be the name as known to XEmacs.
1763 The two names differ in capitalization and underscoring.
1764 */
1765        (keysym, device))
1766 {
1767   struct device *d = decode_device (device);
1768   if (!DEVICE_X_P (d))
1769     signal_simple_error ("Not an X device", device);
1770
1771   return (NILP (Fgethash (keysym, DEVICE_X_KEYSYM_MAP_HASH_TABLE (d), Qnil)) ?
1772           Qnil : Qt);
1773 }
1774
1775 \f
1776 /************************************************************************/
1777 /*                          grabs and ungrabs                           */
1778 /************************************************************************/
1779
1780 DEFUN ("x-grab-pointer", Fx_grab_pointer, 0, 3, 0, /*
1781 Grab the pointer and restrict it to its current window.
1782 If optional DEVICE argument is nil, the default device will be used.
1783 If optional CURSOR argument is non-nil, change the pointer shape to that
1784  until `x-ungrab-pointer' is called (it should be an object returned by the
1785  `make-cursor-glyph' function).
1786 If the second optional argument IGNORE-KEYBOARD is non-nil, ignore all
1787   keyboard events during the grab.
1788 Returns t if the grab is successful, nil otherwise.
1789 */
1790        (device, cursor, ignore_keyboard))
1791 {
1792   Window w;
1793   int pointer_mode, result;
1794   struct device *d = decode_x_device (device);
1795
1796   if (!NILP (cursor))
1797     {
1798       CHECK_POINTER_GLYPH (cursor);
1799       cursor = glyph_image_instance (cursor, device, ERROR_ME, 0);
1800     }
1801
1802   if (!NILP (ignore_keyboard))
1803     pointer_mode = GrabModeSync;
1804   else
1805     pointer_mode = GrabModeAsync;
1806
1807   w = XtWindow (FRAME_X_TEXT_WIDGET (device_selected_frame (d)));
1808
1809   /* #### Possibly this needs to gcpro the cursor somehow, but it doesn't
1810      seem to cause a problem if XFreeCursor is called on a cursor in use
1811      in a grab; I suppose the X server counts the grab as a reference
1812      and doesn't free it until it exits? */
1813   result = XGrabPointer (DEVICE_X_DISPLAY (d), w,
1814                          False,
1815                          ButtonMotionMask  |
1816                          ButtonPressMask   |
1817                          ButtonReleaseMask |
1818                          PointerMotionHintMask,
1819                          GrabModeAsync,       /* Keep pointer events flowing */
1820                          pointer_mode,        /* Stall keyboard events */
1821                          w,                   /* Stay in this window */
1822                          (NILP (cursor) ? 0
1823                           : XIMAGE_INSTANCE_X_CURSOR (cursor)),
1824                          CurrentTime);
1825   return (result == GrabSuccess) ? Qt : Qnil;
1826 }
1827
1828 DEFUN ("x-ungrab-pointer", Fx_ungrab_pointer, 0, 1, 0, /*
1829 Release a pointer grab made with `x-grab-pointer'.
1830 If optional first arg DEVICE is nil the default device is used.
1831 If it is t the pointer will be released on all X devices.
1832 */
1833        (device))
1834 {
1835   if (!EQ (device, Qt))
1836     {
1837       Display *dpy = get_x_display (device);
1838       XUngrabPointer (dpy, CurrentTime);
1839     }
1840   else
1841     {
1842       Lisp_Object devcons, concons;
1843
1844       DEVICE_LOOP_NO_BREAK (devcons, concons)
1845         {
1846           struct device *d = XDEVICE (XCAR (devcons));
1847
1848           if (DEVICE_X_P (d))
1849             XUngrabPointer (DEVICE_X_DISPLAY (d), CurrentTime);
1850         }
1851     }
1852
1853   return Qnil;
1854 }
1855
1856 DEFUN ("x-grab-keyboard", Fx_grab_keyboard, 0, 1, 0, /*
1857 Grab the keyboard on the given device (defaulting to the selected one).
1858 So long as the keyboard is grabbed, all keyboard events will be delivered
1859 to emacs -- it is not possible for other X clients to eavesdrop on them.
1860 Ungrab the keyboard with `x-ungrab-keyboard' (use an unwind-protect).
1861 Returns t if the grab is successful, nil otherwise.
1862 */
1863        (device))
1864 {
1865   struct device *d = decode_x_device (device);
1866   Window w = XtWindow (FRAME_X_TEXT_WIDGET (device_selected_frame (d)));
1867   Display *dpy = DEVICE_X_DISPLAY (d);
1868   Status status;
1869   XSync (dpy, False);
1870   status = XGrabKeyboard (dpy, w, True,
1871                           /* I don't really understand sync-vs-async
1872                              grabs, but this is what xterm does. */
1873                           GrabModeAsync, GrabModeAsync,
1874                           /* Use the timestamp of the last user action
1875                              read by emacs proper; xterm uses CurrentTime
1876                              but there's a comment that says "wrong"...
1877                              (Despite the name this is the time of the
1878                              last key or mouse event.) */
1879                           DEVICE_X_MOUSE_TIMESTAMP (d));
1880   if (status == GrabSuccess)
1881     {
1882       /* The XUngrabKeyboard should generate a FocusIn back to this
1883          window but it doesn't unless we explicitly set focus to the
1884          window first (which should already have it.  The net result
1885          is that without this call when x-ungrab-keyboard is called
1886          the selected frame ends up not having focus. */
1887       XSetInputFocus (dpy, w, RevertToParent, DEVICE_X_MOUSE_TIMESTAMP (d));
1888       return Qt;
1889     }
1890   else
1891     return Qnil;
1892 }
1893
1894 DEFUN ("x-ungrab-keyboard", Fx_ungrab_keyboard, 0, 1, 0, /*
1895 Release a keyboard grab made with `x-grab-keyboard'.
1896 */
1897        (device))
1898 {
1899   Display *dpy = get_x_display (device);
1900   XUngrabKeyboard (dpy, CurrentTime);
1901   return Qnil;
1902 }
1903
1904 DEFUN ("x-get-font-path", Fx_get_font_path, 0, 1, 0, /*
1905 Get the X Server's font path.
1906
1907 See also `x-set-font-path'.
1908 */
1909        (device))
1910 {
1911   Display *dpy = get_x_display (device);
1912   int ndirs_return;
1913   const char **directories = (const char **) XGetFontPath (dpy, &ndirs_return);
1914   Lisp_Object font_path = Qnil;
1915
1916   if (!directories)
1917     signal_simple_error ("Can't get X font path", device);
1918
1919   while (ndirs_return--)
1920       font_path = Fcons (build_ext_string (directories[ndirs_return],
1921                                            Qfile_name),
1922                          font_path);
1923
1924   return font_path;
1925 }
1926
1927 DEFUN ("x-set-font-path", Fx_set_font_path, 1, 2, 0, /*
1928 Set the X Server's font path to FONT-PATH.
1929
1930 There is only one font path per server, not one per client.  Use this
1931 sparingly.  It uncaches all of the X server's font information.
1932
1933 Font directories should end in the path separator and should contain
1934 a file called fonts.dir usually created with the program mkfontdir.
1935
1936 Setting the FONT-PATH to nil tells the X server to use the default
1937 font path.
1938
1939 See also `x-get-font-path'.
1940 */
1941        (font_path, device))
1942 {
1943   Display *dpy = get_x_display (device);
1944   Lisp_Object path_entry;
1945   const char **directories;
1946   int i=0,ndirs=0;
1947
1948   EXTERNAL_LIST_LOOP (path_entry, font_path)
1949     {
1950       CHECK_STRING (XCAR (path_entry));
1951       ndirs++;
1952     }
1953
1954   directories = alloca_array (const char *, ndirs);
1955
1956   EXTERNAL_LIST_LOOP (path_entry, font_path)
1957     {
1958       LISP_STRING_TO_EXTERNAL (XCAR (path_entry), directories[i++], Qfile_name);
1959     }
1960
1961   expect_x_error (dpy);
1962   XSetFontPath (dpy, (char **) directories, ndirs);
1963   signal_if_x_error (dpy, 1/*resumable_p*/);
1964
1965   return Qnil;
1966 }
1967
1968 \f
1969 /************************************************************************/
1970 /*                            initialization                            */
1971 /************************************************************************/
1972
1973 void
1974 syms_of_device_x (void)
1975 {
1976   DEFSUBR (Fx_debug_mode);
1977   DEFSUBR (Fx_get_resource);
1978   DEFSUBR (Fx_get_resource_prefix);
1979   DEFSUBR (Fx_put_resource);
1980
1981   DEFSUBR (Fdefault_x_device);
1982   DEFSUBR (Fx_display_visual_class);
1983   DEFSUBR (Fx_display_visual_depth);
1984   DEFSUBR (Fx_server_vendor);
1985   DEFSUBR (Fx_server_version);
1986   DEFSUBR (Fx_valid_keysym_name_p);
1987   DEFSUBR (Fx_keysym_hash_table);
1988   DEFSUBR (Fx_keysym_on_keyboard_p);
1989   DEFSUBR (Fx_keysym_on_keyboard_sans_modifiers_p);
1990
1991   DEFSUBR (Fx_grab_pointer);
1992   DEFSUBR (Fx_ungrab_pointer);
1993   DEFSUBR (Fx_grab_keyboard);
1994   DEFSUBR (Fx_ungrab_keyboard);
1995
1996   DEFSUBR (Fx_get_font_path);
1997   DEFSUBR (Fx_set_font_path);
1998
1999   defsymbol (&Qx_error, "x-error");
2000   defsymbol (&Qinit_pre_x_win, "init-pre-x-win");
2001   defsymbol (&Qinit_post_x_win, "init-post-x-win");
2002 }
2003
2004 void
2005 reinit_console_type_create_device_x (void)
2006 {
2007   /* Initialize variables to speed up X resource interactions */
2008   const char *valid_resource_chars =
2009     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
2010   while (*valid_resource_chars)
2011     valid_resource_char_p[(unsigned int) (*valid_resource_chars++)] = 1;
2012
2013   name_char_dynarr  = Dynarr_new (char);
2014   class_char_dynarr = Dynarr_new (char);
2015 }
2016
2017 void
2018 console_type_create_device_x (void)
2019 {
2020   reinit_console_type_create_device_x ();
2021   CONSOLE_HAS_METHOD (x, init_device);
2022   CONSOLE_HAS_METHOD (x, finish_init_device);
2023   CONSOLE_HAS_METHOD (x, mark_device);
2024   CONSOLE_HAS_METHOD (x, delete_device);
2025   CONSOLE_HAS_METHOD (x, device_system_metrics);
2026 }
2027
2028 void
2029 reinit_vars_of_device_x (void)
2030 {
2031   error_expected = 0;
2032   error_occurred = 0;
2033
2034   in_resource_setting = 0;
2035 }
2036
2037 void
2038 vars_of_device_x (void)
2039 {
2040   reinit_vars_of_device_x ();
2041
2042   DEFVAR_LISP ("x-emacs-application-class", &Vx_emacs_application_class /*
2043 The X application class of the XEmacs process.
2044 This controls, among other things, the name of the `app-defaults' file
2045 that XEmacs will use.  For changes to this variable to take effect, they
2046 must be made before the connection to the X server is initialized, that is,
2047 this variable may only be changed before emacs is dumped, or by setting it
2048 in the file lisp/term/x-win.el.
2049
2050 If this variable is nil before the connection to the X server is first
2051 initialized (which it is by default), the X resource database will be
2052 consulted and the value will be set according to whether any resources
2053 are found for the application class `XEmacs'.  If the user has set any
2054 resources for the XEmacs application class, the XEmacs process will use
2055 the application class `XEmacs'.  Otherwise, the XEmacs process will use
2056 the application class `Emacs' which is backwards compatible to previous
2057 XEmacs versions but may conflict with resources intended for GNU Emacs.
2058 */ );
2059   Vx_emacs_application_class = Qnil;
2060
2061   DEFVAR_LISP ("x-initial-argv-list", &Vx_initial_argv_list /*
2062 You don't want to know.
2063 This is used during startup to communicate the remaining arguments in
2064 `command-line-args-left' to the C code, which passes the args to
2065 the X initialization code, which removes some args, and then the
2066 args are placed back into `x-initial-arg-list' and thence into
2067 `command-line-args-left'.  Perhaps `command-line-args-left' should
2068 just reside in C.
2069 */ );
2070   Vx_initial_argv_list = Qnil;
2071
2072 #if defined(MULE) && (defined(LWLIB_MENUBARS_MOTIF) || defined(HAVE_XIM) || defined (USE_XFONTSET))
2073   DEFVAR_LISP ("x-app-defaults-directory", &Vx_app_defaults_directory /*
2074 Used by the Lisp code to communicate to the low level X initialization
2075 where the localized init files are.
2076 */ );
2077   Vx_app_defaults_directory = Qnil;
2078 #endif
2079
2080   Fprovide (Qx);
2081
2082   staticpro (&Vdefault_x_device);
2083   Vdefault_x_device = Qnil;
2084 }