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