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