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