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.
5 This file is part of SXEmacs
7 SXEmacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 SXEmacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* Synched up with: Not in FSF. */
23 /* 7-8-00 !!#### This file needs definite Mule review. */
25 /* Original authors: Jamie Zawinski and the FSF */
26 /* Rewritten by Ben Wing and Chuck Thompson. */
31 #include "console-x.h"
32 #include "xintrinsicp.h" /* CoreP.h needs this */
33 #include <X11/CoreP.h> /* Numerous places access the fields of
34 a core widget directly. We could
35 use XtGetValues(), but ... */
37 #include <X11/Shell.h>
40 #include "objects-x.h"
44 #include "events/events.h"
47 #include "ui/redisplay.h"
49 #include "ui/window.h"
54 #if defined WITH_EMODULES && defined HAVE_EMODULES && \
55 defined(LWLIB_USES_ATHENA) && !defined(HAVE_ATHENA_3D) || 1
56 #include "emodules-ng.h"
57 #endif /* HAVE_SHLIB and LWLIB_USES_ATHENA and not HAVE_ATHENA_3D */
59 #if defined(HAVE_OFFIX_DND) && SOMEONE_FIXED_THAT_DND_STUFF
63 Lisp_Object Vdefault_x_device;
64 #if defined(MULE) && (defined(LWLIB_MENUBARS_MOTIF) || \
65 defined(HAVE_XIM) || defined (USE_XFONTSET))
66 Lisp_Object Vx_app_defaults_directory;
69 /* Qdisplay in general.c */
71 Lisp_Object Qinit_pre_x_win, Qinit_post_x_win;
73 /* The application class of Emacs. */
74 Lisp_Object Vx_emacs_application_class;
76 Lisp_Object Vx_initial_argv_list; /* #### ugh! */
78 static XrmOptionDescRec emacs_options[] = {
79 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
80 {"-iconic", ".iconic", XrmoptionNoArg, "yes"},
82 {"-internal-border-width", "*EmacsFrame.internalBorderWidth",
83 XrmoptionSepArg, NULL},
84 {"-ib", "*EmacsFrame.internalBorderWidth", XrmoptionSepArg, NULL},
85 {"-scrollbar-width", "*EmacsFrame.scrollBarWidth", XrmoptionSepArg,
87 {"-scrollbar-height", "*EmacsFrame.scrollBarHeight", XrmoptionSepArg,
90 {"-privatecolormap", ".privateColormap", XrmoptionNoArg, "yes"},
91 {"-visual", ".EmacsVisual", XrmoptionSepArg, NULL},
93 /* #### Beware! If the type of the shell changes, update this. */
94 {"-T", "*TopLevelEmacsShell.title", XrmoptionSepArg, NULL},
95 {"-wn", "*TopLevelEmacsShell.title", XrmoptionSepArg, NULL},
96 {"-title", "*TopLevelEmacsShell.title", XrmoptionSepArg, NULL},
98 {"-iconname", "*TopLevelEmacsShell.iconName", XrmoptionSepArg, NULL},
99 {"-in", "*TopLevelEmacsShell.iconName", XrmoptionSepArg, NULL},
100 {"-mc", "*pointerColor", XrmoptionSepArg, NULL},
101 {"-cr", "*cursorColor", XrmoptionSepArg, NULL},
102 {"-fontset", "*FontSet", XrmoptionSepArg, NULL},
105 /* Functions to synchronize mirroring resources and specifiers */
106 int in_resource_setting;
108 /************************************************************************/
109 /* helper functions */
110 /************************************************************************/
112 /* JH 97/11/25 removed the static declaration because I need it during setup in
114 struct device *get_device_from_display_1(Display * dpy);
115 struct device *get_device_from_display_1(Display * dpy)
117 Lisp_Object devcons, concons;
119 DEVICE_LOOP_NO_BREAK(devcons, concons) {
120 struct device *d = XDEVICE(XCAR(devcons));
121 if (DEVICE_X_P(d) && DEVICE_X_DISPLAY(d) == dpy) {
128 struct device *get_device_from_display(Display *dpy)
130 struct device *d = get_device_from_display_1(dpy);
132 #if !defined(INFODOCK)
133 # define FALLBACK_RESOURCE_NAME "sxemacs"
135 # define FALLBACK_RESOURCE_NAME "infodock"
139 /* This isn't one of our displays. Let's crash? */
140 stderr_out("\n%s: Fatal X Condition. "
141 "Asked about display we don't own: \"%s\"\n",
142 (STRINGP(Vinvocation_name)
143 ? (char *)XSTRING_DATA(Vinvocation_name)
144 : FALLBACK_RESOURCE_NAME),
150 #undef FALLBACK_RESOURCE_NAME
155 struct device *decode_x_device(Lisp_Object device)
157 XSETDEVICE(device, decode_device(device));
158 CHECK_X_DEVICE(device);
159 return XDEVICE(device);
162 static Display *get_x_display(Lisp_Object device)
164 return DEVICE_X_DISPLAY(decode_x_device(device));
167 /************************************************************************/
168 /* initializing an X connection */
169 /************************************************************************/
171 static struct device *device_being_initialized = NULL;
173 static void allocate_x_device_struct(struct device *d)
175 d->device_data = xnew_and_zero(struct x_device);
178 static void Xatoms_of_device_x(struct device *d)
180 Display *D = DEVICE_X_DISPLAY(d);
182 DEVICE_XATOM_WM_PROTOCOLS(d) = XInternAtom(D, "WM_PROTOCOLS", False);
183 DEVICE_XATOM_WM_DELETE_WINDOW(d) =
184 XInternAtom(D, "WM_DELETE_WINDOW", False);
185 DEVICE_XATOM_WM_SAVE_YOURSELF(d) =
186 XInternAtom(D, "WM_SAVE_YOURSELF", False);
187 DEVICE_XATOM_WM_TAKE_FOCUS(d) = XInternAtom(D, "WM_TAKE_FOCUS", False);
188 DEVICE_XATOM_WM_STATE(d) = XInternAtom(D, "WM_STATE", False);
191 static void sanity_check_geometry_resource(Display * dpy)
193 char *app_name, *app_class, *s;
194 char buf1[255], buf2[255];
197 XtGetApplicationNameAndClass(dpy, &app_name, &app_class);
199 strncpy(buf1, app_name, sizeof(buf1));
200 buf1[sizeof(buf1)-1] = '\0';
201 strncpy(buf2, app_class, sizeof(buf2));
202 buf2[sizeof(buf2)-1] = '\0';
204 for (s = buf1; *s; s++) {
209 strncat(buf1, "._no_._such_._resource_.geometry",
210 sizeof(buf1) - strlen(buf1) - 1);
211 buf1[sizeof(buf1)-1] = '\0';
212 strncat(buf2, "._no_._such_._resource_.Geometry",
213 sizeof(buf2) - strlen(buf2) - 1);
214 buf2[sizeof(buf1)-1]='\0';
215 if (XrmGetResource(XtDatabase(dpy), buf1, buf2, &type, &value)
217 warn_when_safe(Qgeometry, Qerror,
219 "Apparently \"%s*geometry: %s\" or "
220 "\"%s*geometry: %s\" was\n"
221 "specified in the resource database. "
222 "Specifying \"*geometry\" will make\n"
223 "SXEmacs (and most other X programs) "
224 "malfunction in obscure ways. (i.e.\n"
225 "the Xt or Xm libraries will probably crash, "
226 "which is a very bad thing.)\n"
227 "You should always use \".geometry\" or "
228 "\"*EmacsFrame.geometry\" instead.\n",
229 app_name, (char *)value.addr,
230 app_class, (char *)value.addr);
231 suppress_early_error_handler_backtrace = 1;
232 error("Invalid geometry resource");
237 x_init_device_class(struct device *d)
239 if (DEVICE_X_DEPTH(d) > 2) {
240 switch (DEVICE_X_VISUAL(d)->class) {
243 DEVICE_CLASS(d) = Qgrayscale;
246 DEVICE_CLASS(d) = Qcolor;
249 DEVICE_CLASS(d) = Qmono;
255 * Figure out what application name to use for sxemacs
257 * Since we have decomposed XtOpenDisplay into XOpenDisplay and
258 * XtDisplayInitialize, we no longer get this for free.
260 * If there is a `-name' argument in argv, use that.
261 * Otherwise use the last component of argv[0].
263 * I have removed the gratuitous use of getenv("RESOURCE_NAME")
264 * which was in X11R5, but left the matching of any prefix of `-name'.
265 * Finally, if all else fails, return `sxemacs', as it is more
266 * appropriate (X11R5 returns `main').
269 compute_x_app_name(int argc, Extbyte ** argv)
274 for (i = 1; i < argc - 1; i++) {
275 if (!strncmp(argv[i], "-name", max(2, strlen(argv[1])))) {
279 if (argc > 0 && argv[0] && *argv[0]) {
280 return (ptr = strrchr(argv[0], '/')) ? ++ptr : argv[0];
286 * This function figures out whether the user has any resources of the
287 * form "SXEmacs.foo" or "SXEmacs*foo".
289 * Currently we only consult the display's global resources; to look
290 * for screen specific resources, we would need to also consult:
291 * xdefs = XScreenResourceString(ScreenOfDisplay(dpy, scrno));
293 static int have_sxemacs_resources_in_xrdb(Display * dpy)
308 /* don't free - owned by X */
309 xdefs = XResourceManagerString(dpy);
310 while (xdefs && *xdefs) {
311 if (strncmp(xdefs, key, len) == 0 &&
312 (xdefs[len] == '*' || xdefs[len] == '.'))
315 /* find start of next entry.. */
316 while (*xdefs && *xdefs++ != '\n');
321 /* Only the characters [-_A-Za-z0-9] are allowed in the individual
322 components of a resource. Convert invalid characters to `-' */
324 static char valid_resource_char_p[256];
327 validify_resource_component(char *str, size_t len)
329 for (; len; len--, str++) {
330 if (!valid_resource_char_p[(unsigned char)(*str)]) {
338 Dynarr_add_validified_lisp_string(char_dynarr * cda, Lisp_Object str)
340 Bytecount len = XSTRING_LENGTH(str);
341 Dynarr_add_many(cda, (char *)XSTRING_DATA(str), len);
342 validify_resource_component(Dynarr_atp(cda, Dynarr_length(cda) - len),
347 /* compare visual info for qsorting */
348 static int x_comp_visual_info(const void *elem1, const void *elem2)
350 XVisualInfo *left, *right;
352 left = (XVisualInfo *) elem1;
353 right = (XVisualInfo *) elem2;
360 if (left->depth > right->depth) {
362 } else if (left->depth == right->depth) {
363 if (left->colormap_size > right->colormap_size)
365 if (left->class > right->class)
367 else if (left->class < right->class)
378 #define XXX_IMAGE_LIBRARY_IS_SOMEWHAT_BROKEN
379 static Visual *x_try_best_visual_class(Screen * screen, int scrnum,
382 Display *dpy = DisplayOfScreen(screen);
384 XVisualInfo *vi_out = NULL;
387 vi_in.class = visual_class;
388 vi_in.screen = scrnum;
389 vi_out = XGetVisualInfo(dpy, (VisualClassMask | VisualScreenMask),
394 for (i = 0, best = 0; i < out_count; i++)
395 /* It's better if it's deeper, or if it's the same depth
396 with more cells (does that ever happen? Well, it
397 could...) NOTE: don't allow pseudo color to get
399 if (((vi_out[i].depth > vi_out[best].depth) ||
400 ((vi_out[i].depth == vi_out[best].depth) &&
401 (vi_out[i].colormap_size >
402 vi_out[best].colormap_size)))
403 #ifdef XXX_IMAGE_LIBRARY_IS_SOMEWHAT_BROKEN
404 /* For now, the image library doesn't like
405 PseudoColor visuals of depths other than 1 or 8.
406 Depths greater than 8 only occur on machines
407 which have TrueColor anyway, so probably we'll
408 end up using that (it is the one that `Best'
409 would pick) but if a PseudoColor visual is
410 explicitly specified, pick the 8 bit one.
412 && (visual_class != PseudoColor ||
413 vi_out[i].depth == 1 || vi_out[i].depth == 8)
415 /* SGI has 30-bit deep visuals. Ignore them.
416 (We only have 24-bit data anyway.)
418 && (vi_out[i].depth <= 24)
421 visual = vi_out[best].visual;
422 XFree((char *)vi_out);
428 static int x_get_visual_depth(Display * dpy, Visual * visual)
434 vi_in.visualid = XVisualIDFromVisual(visual);
435 vi_out = XGetVisualInfo(dpy, /*VisualScreenMask| */ VisualIDMask,
440 XFree((char *)vi_out);
444 static Visual *x_try_best_visual(Display * dpy, int scrnum)
446 Visual *visual = NULL;
447 Screen *screen = ScreenOfDisplay(dpy, scrnum);
448 if ((visual = x_try_best_visual_class(screen, scrnum, TrueColor))
449 && x_get_visual_depth(dpy, visual) >= 16)
451 if ((visual = x_try_best_visual_class(screen, scrnum, PseudoColor)))
453 if ((visual = x_try_best_visual_class(screen, scrnum, TrueColor)))
455 #ifdef DIRECTCOLOR_WORKS
456 if ((visual = x_try_best_visual_class(screen, scrnum, DirectColor)))
460 visual = DefaultVisualOfScreen(screen);
461 if (x_get_visual_depth(dpy, visual) >= 8)
464 if ((visual = x_try_best_visual_class(screen, scrnum, StaticGray)))
466 if ((visual = x_try_best_visual_class(screen, scrnum, GrayScale)))
468 return DefaultVisualOfScreen(screen);
471 /* helpers, actually these have been factored out of init_device */
473 check_for_3d_shebang() __attribute__((always_inline));
475 read_locale_specific_resources() __attribute__((always_inline));
477 #if defined LWLIB_USES_ATHENA && !defined HAVE_ATHENA_3D && \
478 defined HAVE_LT_DLSYM
479 static const char big_messy_error_string[] =
481 "It seems that SXEmacs is built dynamically linked "
482 "to the flat Athena widget\n"
483 "library but it finds a 3D Athena variant with the "
484 "same name at runtime.\n"
486 "This WILL cause your SXEmacs process to dump core "
488 "You should not continue to use this binary without "
489 "resolving this issue.\n"
491 "This can be solved with the xaw-wrappers package "
493 "(register SXEmacs as incompatible with all 3d widget "
495 "update-xaw-wrappers(8) and .../doc/xaw-wrappers/README.packagers). "
496 "It\ncan be verified by checking the runtime path in "
497 "/etc/ld.so.conf and by\n"
498 "using `ldd /path/to/sxemacs' under other Linux "
499 "distributions. One\nsolution is to use LD_PRELOAD "
500 "or LD_LIBRARY_PATH to force ld.so to\n"
501 "load the flat Athena widget library instead of the "
502 "aliased 3D widget\n"
503 "library (see ld.so(8) for use of these environment "
507 check_for_3d_shebang(void)
509 /* In order to avoid the lossage with flat Athena widgets dynamically
510 * linking to one of the ThreeD variants, using the dynamic symbol helpers
511 * to look for symbols that shouldn't be there and refusing to run if they
512 * are seems a less toxic idea than having SXEmacs crash when we try and
513 * use a subclass of a widget that has changed size.
515 * It's ugly, I know, and not going to work everywhere. It seems better to
516 * do our damnedest to try and tell the user what to expect rather than
517 * simply blow up though.
519 * All the ThreeD variants I have access to define the following function
520 * symbols in the shared library. The flat Xaw library does not define them:
522 * Xaw3dComputeBottomShadowRGB
523 * Xaw3dComputeTopShadowRGB
525 * So far only Linux has shown this problem. This seems to be portable to
526 * all the distributions (certainly all the ones I checked - Debian and
529 * This will only work, sadly, with dlopen() -- the other dynamic linkers
530 * are simply not capable of doing what is needed. :/
532 /* Get a dll handle to the main process. */
533 lt_dlhandle xaw_dll_handle = lt_dlopen(NULL);
534 void *xaw_function_handle;
536 /* Did that fail? If so, continue without error.
537 * We could die here but, well, that's unfriendly and all --
539 * better about some crashing somewhere rather than preventing a
541 * good configuration working just because dll_open failed.
543 if (UNLIKELY(xaw_dll_handle == NULL)) {
548 /* Look for the Xaw3d function */
549 xaw_function_handle =
550 lt_dlsym(xaw_dll_handle, "Xaw3dComputeTopShadowRGB");
552 /* If we found it, warn the user in big, nasty, unfriendly letters */
553 if (xaw_function_handle != NULL) {
554 warn_when_safe(Qdevice, Qerror, big_messy_error_string);
557 /* Otherwise release the handle to the library
558 * No error catch here; I can't think of a way to
561 lt_dlclose(xaw_dll_handle);
566 check_for_3d_shebang(void)
570 #endif /* HAVE_SHLIB and LWLIB_USES_ATHENA and not HAVE_ATHENA_3D */
572 #if defined LWLIB_MENUBARS_MOTIF || defined HAVE_XIM || defined USE_XFONTSET
574 read_locale_specific_resources(Display *dpy)
576 /* Read in locale-specific resources from
577 data-directory/app-defaults/$LANG/Emacs.
578 This is in addition to the standard app-defaults files, and
579 does not override resources defined elsewhere */
580 const char *data_dir = NULL;
581 /* #### XtScreenDatabase(dpy) ? */
582 XrmDatabase db = XtDatabase(dpy);
583 const char *locale = XrmLocaleOfDatabase(db);
585 if (STRINGP(Vx_app_defaults_directory) &&
586 XSTRING_LENGTH(Vx_app_defaults_directory) > 0) {
587 LISP_STRING_TO_EXTERNAL(Vx_app_defaults_directory,
588 data_dir, Qfile_name);
590 /* C99 we need you ... VLA */
591 char path[strlen(data_dir) + strlen(locale) + 7];
593 snprintf(path, countof(path),
594 "%s%s/Emacs", data_dir, locale);
595 if (!access(path, R_OK)) {
596 XrmCombineFileDatabase(path, &db, False);
599 } else if (STRINGP(Vdata_directory)
600 && XSTRING_LENGTH(Vdata_directory) > 0) {
601 LISP_STRING_TO_EXTERNAL(Vdata_directory, data_dir,
605 /* C99 we need you ... VLA */
606 char path[strlen(data_dir) + 13 + strlen(locale) + 7];
609 snprintf(path, countof(path), "%sapp-defaults/%s/Emacs",
611 if (!access(path, R_OK)) {
612 XrmCombineFileDatabase(path, &db, False);
618 #else /* !LWLIB_MENUBARS_MOTIF && !HAVE_XIM && !USE_XFONTSET */
620 read_locale_specific_resources(Display *UNUSED(dpy))
624 #endif /* LWLIB_MENUBARS_MOTIF || HAVE_XIM || USE_XFONTSET */
626 static void x_init_device(struct device *d, Lisp_Object props)
637 Visual *visual = NULL;
638 int depth = 8; /* shut up the compiler */
642 int best_visual_found = 0;
644 /* is that still needed? -hrop */
645 check_for_3d_shebang();
647 XSETDEVICE(device, d);
648 display = DEVICE_CONNECTION(d);
650 allocate_x_device_struct(d);
652 make_argc_argv(Vx_initial_argv_list, &argc, &argv);
654 LISP_STRING_TO_EXTERNAL(display, disp_name, Qctext);
657 * Break apart the old XtOpenDisplay call into XOpenDisplay and
658 * XtDisplayInitialize so we can figure out whether there
659 * are any SXEmacs resources in the resource database before
660 * we initialize Xt. This is so we can automagically support
661 * both `Emacs' and `SXEmacs' application classes.
663 slow_down_interrupts();
664 device_being_initialized = d;
665 dpy = DEVICE_X_DISPLAY(d) = XOpenDisplay(disp_name);
666 device_being_initialized = NULL;
667 speed_up_interrupts();
670 suppress_early_error_handler_backtrace = 1;
671 signal_simple_error("X server not responding\n", display);
674 if (STRINGP(Vx_emacs_application_class) &&
675 XSTRING_LENGTH(Vx_emacs_application_class) > 0) {
676 LISP_STRING_TO_EXTERNAL(Vx_emacs_application_class, app_class,
679 app_class = (NILP(Vx_emacs_application_class) &&
680 have_sxemacs_resources_in_xrdb(dpy))
683 /* need to update Vx_emacs_application_class: */
684 Vx_emacs_application_class = build_string(app_class);
687 slow_down_interrupts();
688 XtDisplayInitialize(Xt_app_con, dpy, compute_x_app_name(argc, argv),
689 app_class, emacs_options,
690 XtNumber(emacs_options), &argc, (char **)argv);
691 speed_up_interrupts();
693 screen = DefaultScreen(dpy);
694 if (NILP(Vdefault_x_device))
695 Vdefault_x_device = device;
698 read_locale_specific_resources(dpy);
701 if (NILP(DEVICE_NAME(d)))
702 DEVICE_NAME(d) = display;
704 /* We're going to modify the string in-place, so be a nice SXEmacs */
705 DEVICE_NAME(d) = Fcopy_sequence(DEVICE_NAME(d));
706 /* colons and periods can't appear in individual elements of resource
709 XtGetApplicationNameAndClass(
710 dpy, (String*)&app_name, (String*)&app_class);
711 /* search for a matching visual if requested by the user, or setup the
714 int resource_name_length = max(sizeof(".emacsVisual"),
715 sizeof(".privateColormap"));
716 char buf1[strlen(app_name) + resource_name_length];
717 char buf2[strlen(app_class) + resource_name_length];
721 sprintf(buf1, "%s.emacsVisual", app_name);
722 sprintf(buf2, "%s.EmacsVisual", app_class);
723 if (XrmGetResource(XtDatabase(dpy), buf1, buf2, &type, &value)
726 int vis_class = PseudoColor;
728 char *str = (char *)value.addr;
730 #define CHECK_VIS_CLASS(visual_class) \
731 else if (memcmp(str, #visual_class, sizeof(#visual_class) - 1) == 0) \
732 cnt = sizeof (#visual_class) - 1, vis_class = visual_class
735 CHECK_VIS_CLASS(StaticGray);
736 CHECK_VIS_CLASS(StaticColor);
737 CHECK_VIS_CLASS(TrueColor);
738 CHECK_VIS_CLASS(GrayScale);
739 CHECK_VIS_CLASS(PseudoColor);
740 CHECK_VIS_CLASS(DirectColor);
743 depth = atoi(str + cnt);
746 "Invalid Depth specification "
747 "in %s... ignoring...\n", str);
750 (dpy, screen, depth, vis_class,
752 visual = vinfo.visual;
756 "requested visual %s..."
762 stderr_out("Invalid Visual specification "
763 "in %s... ignoring.\n", str);
766 if (visual == NULL) {
768 visual = DefaultVisual(dpy, screen);
769 depth = DefaultDepth(dpy, screen);
771 visual = x_try_best_visual(dpy, screen);
772 depth = x_get_visual_depth(dpy, visual);
774 (visual != DefaultVisual(dpy, screen));
777 /* If we've got the same visual as the default and it's
778 PseudoColor, check to see if the user specified that we need
779 a private colormap */
780 if (visual == DefaultVisual(dpy, screen)) {
781 sprintf(buf1, "%s.privateColormap", app_name);
782 sprintf(buf2, "%s.PrivateColormap", app_class);
783 if ((visual->class == PseudoColor) &&
785 (XtDatabase(dpy), buf1, buf2, &type,
788 XCopyColormapAndFree(dpy,
792 cmap = DefaultColormap(dpy, screen);
795 if (best_visual_found) {
798 RootWindow(dpy, screen),
801 /* We have to create a matching colormap
803 #### think about using standard colormaps
804 (need the Xmu libs?) */
807 RootWindow(dpy, screen),
809 XInstallColormap(dpy, cmap);
814 DEVICE_X_VISUAL(d) = visual;
815 DEVICE_X_COLORMAP(d) = cmap;
816 DEVICE_X_DEPTH(d) = depth;
817 validify_resource_component((char *)XSTRING_DATA(DEVICE_NAME(d)),
818 XSTRING_LENGTH(DEVICE_NAME(d)));
821 /* inevitable warnings coming up */
823 XtSetArg(al[0], XtNvisual, visual);
824 XtSetArg(al[1], XtNdepth, depth);
825 XtSetArg(al[2], XtNcolormap, cmap);
827 app_shell = XtAppCreateShell(NULL, app_class,
828 applicationShellWidgetClass,
829 dpy, al, countof(al));
832 DEVICE_XT_APP_SHELL(d) = app_shell;
836 #endif /* HAVE_XIM */
838 /* Realize the app_shell so that its window exists for GC creation purposes,
839 and set it to the size of the root window for child placement purposes */
841 /* inevitable warnings coming up */
843 XtSetArg(al[0], XtNmappedWhenManaged, False);
844 XtSetArg(al[1], XtNx, 0);
845 XtSetArg(al[2], XtNy, 0);
846 XtSetArg(al[3], XtNwidth,
847 WidthOfScreen(ScreenOfDisplay(dpy, screen)));
848 XtSetArg(al[4], XtNheight,
849 HeightOfScreen(ScreenOfDisplay(dpy, screen)));
850 XtSetValues(app_shell, al, countof(al));
851 XtRealizeWidget(app_shell);
854 #ifdef HAVE_WMCOMMAND
858 make_argc_argv(Vcommand_line_args, &new_argc, &new_argv);
859 XSetCommand(XtDisplay(app_shell), XtWindow(app_shell),
860 (char **)new_argv, new_argc);
861 free_argc_argv(new_argv);
863 #endif /* HAVE_WMCOMMAND */
865 Vx_initial_argv_list = make_arg_list(argc, argv);
866 free_argc_argv(argv);
868 DEVICE_X_WM_COMMAND_FRAME(d) = Qnil;
870 sanity_check_geometry_resource(dpy);
873 x_init_modifier_mapping(d);
875 DEVICE_INFD(d) = DEVICE_OUTFD(d) = ConnectionNumber(dpy);
879 DEVICE_X_GC_CACHE(d) = make_gc_cache(dpy, XtWindow(app_shell));
880 DEVICE_X_GRAY_PIXMAP(d) = None;
881 Xatoms_of_device_x(d);
882 Xatoms_of_select_x(d);
883 Xatoms_of_objects_x(d);
884 x_init_device_class(d);
886 /* Run the elisp side of the X device initialization. */
887 call0(Qinit_pre_x_win);
890 static void x_finish_init_device(struct device *d, Lisp_Object props)
892 call0(Qinit_post_x_win);
895 static void x_mark_device(struct device *d)
897 mark_object(DEVICE_X_WM_COMMAND_FRAME(d));
898 mark_object(DEVICE_X_DATA(d)->x_keysym_map_hash_table);
901 /************************************************************************/
902 /* closing an X connection */
903 /************************************************************************/
905 static void free_x_device_struct(struct device *d)
907 xfree(d->device_data);
910 static void x_delete_device(struct device *d)
915 extern void (*__free_hook) (void *);
919 XSETDEVICE(device, d);
920 display = DEVICE_X_DISPLAY(d);
924 checking_free = (__free_hook != 0);
926 /* Disable strict free checking, to avoid bug in X library */
928 disable_strict_free_check();
931 free_gc_cache(DEVICE_X_GC_CACHE(d));
932 if (DEVICE_X_DATA(d)->x_modifier_keymap)
933 XFreeModifiermap(DEVICE_X_DATA(d)->x_modifier_keymap);
934 if (DEVICE_X_DATA(d)->x_keysym_map)
935 XFree((char *)DEVICE_X_DATA(d)->x_keysym_map);
937 if (DEVICE_XT_APP_SHELL(d)) {
938 XtDestroyWidget(DEVICE_XT_APP_SHELL(d));
939 DEVICE_XT_APP_SHELL(d) = NULL;
942 XtCloseDisplay(display);
943 DEVICE_X_DISPLAY(d) = 0;
946 enable_strict_free_check();
950 if (EQ(device, Vdefault_x_device)) {
951 Lisp_Object devcons, concons;
952 /* #### handle deleting last X device */
953 Vdefault_x_device = Qnil;
954 DEVICE_LOOP_NO_BREAK(devcons, concons) {
955 if (DEVICE_X_P(XDEVICE(XCAR(devcons))) &&
956 !EQ(device, XCAR(devcons))) {
957 Vdefault_x_device = XCAR(devcons);
963 free_x_device_struct(d);
966 /************************************************************************/
967 /* handle X errors */
968 /************************************************************************/
970 const char *x_event_name(int event_type)
972 static const char *events[] = {
1011 if (event_type < 0 || event_type >= countof(events))
1013 return events[event_type];
1018 If an X error occurs which we are not expecting, we have no alternative
1019 but to print it to stderr. It would be nice to stuff it into a pop-up
1020 buffer, or to print it in the minibuffer, but that's not possible, because
1021 one is not allowed to do any I/O on the display connection from an error
1022 handler. The guts of Xlib expect these functions to either return or exit.
1024 However, there are occasions when we might expect an error to reasonably
1025 occur. The interface to this is as follows:
1027 Before calling some X routine which may error, call
1028 expect_x_error (dpy);
1030 Just after calling the X routine, call either:
1032 x_error_occurred_p (dpy);
1034 to ask whether an error happened (and was ignored), or:
1036 signal_if_x_error (dpy, resumable_p);
1038 which will call Fsignal() with args appropriate to the X error, if there
1039 was one. (Resumable_p is whether the debugger should be allowed to
1040 continue from the call to signal.)
1042 You must call one of these two routines immediately after calling the X
1043 routine; think of them as bookends like BLOCK_INPUT and UNBLOCK_INPUT.
1046 static int error_expected;
1047 static int error_occurred;
1048 static XErrorEvent last_error;
1052 #ifdef EXTERNAL_WIDGET
1053 static Lisp_Object x_error_handler_do_enqueue(Lisp_Object frame)
1055 enqueue_magic_eval_event(io_error_delete_frame, frame);
1059 static Lisp_Object x_error_handler_error(Lisp_Object data, Lisp_Object dummy)
1063 #endif /* EXTERNAL_WIDGET */
1065 int x_error_handler(Display * disp, XErrorEvent * event)
1067 if (error_expected) {
1070 last_error = *event;
1072 #ifdef EXTERNAL_WIDGET
1074 struct device *d = get_device_from_display(disp);
1076 if ((event->error_code == BadWindow ||
1077 event->error_code == BadDrawable)
1078 && ((f = x_any_window_to_frame(d, event->resourceid)) != 0)) {
1081 /* one of the windows comprising one of our frames has died.
1082 This occurs particularly with ExternalShell frames when the
1083 client that owns the ExternalShell's window dies.
1085 We cannot do any I/O on the display connection so we need
1086 to enqueue an eval event so that the deletion happens
1089 Furthermore, we need to trap any errors (out-of-memory) that
1090 may occur when Fenqueue_eval_event is called.
1093 if (f->being_deleted)
1095 XSETFRAME(frame, f);
1098 (Qerror, x_error_handler_do_enqueue, frame,
1099 x_error_handler_error, Qnil))) {
1100 f->being_deleted = 1;
1105 #endif /* EXTERNAL_WIDGET */
1108 /* This ends up calling X, which isn't allowed in an X error handler
1110 stderr_out("\n%s: ", (STRINGP(Vinvocation_name)
1111 ? (char *)XSTRING_DATA(Vinvocation_name)
1114 XmuPrintDefaultErrorMessage(disp, event, stderr);
1119 void expect_x_error(Display * dpy)
1121 assert(!error_expected);
1122 XSync(dpy, 0); /* handle pending errors before setting flag */
1127 int x_error_occurred_p(Display * dpy)
1130 XSync(dpy, 0); /* handle pending errors before setting flag */
1131 val = error_occurred;
1137 int signal_if_x_error(Display * dpy, int resumable_p)
1141 if (!x_error_occurred_p(dpy))
1144 sprintf(buf, "0x%X", (unsigned int)last_error.resourceid);
1145 data = Fcons(build_string(buf), data);
1148 sprintf(num, "%d", last_error.request_code);
1149 XGetErrorDatabaseText(last_error.display, "XRequest", num, "",
1152 sprintf(buf, "Request-%d", last_error.request_code);
1153 data = Fcons(build_string(buf), data);
1155 XGetErrorText(last_error.display, last_error.error_code, buf,
1157 data = Fcons(build_string(buf), data);
1158 Fsignal(Qx_error, data);
1161 Fsignal(Qx_error, data);
1166 int x_IO_error_handler(Display * disp)
1168 /* This function can GC */
1170 struct device *d = get_device_from_display_1(disp);
1173 d = device_being_initialized;
1178 if (NILP(find_nonminibuffer_frame_not_on_device(dev))) {
1179 /* We're going down. */
1181 ("\n%s: Fatal I/O Error %d (%s) on display connection \"%s\"\n",
1182 (STRINGP(Vinvocation_name) ?
1183 (char *)XSTRING_DATA(Vinvocation_name) : "sxemacs"),
1184 errno, strerror(errno), DisplayString(disp));
1186 (" after %lu requests (%lu known processed) with %d events remaining.\n",
1187 NextRequest(disp) - 1, LastKnownRequestProcessed(disp),
1189 /* assert (!_Xdebug); */
1193 "I/O Error %d (%s) on display connection\n"
1194 " \"%s\" after after %lu requests (%lu known processed)\n"
1195 " with %d events remaining.\n"
1196 " Throwing to top level.\n",
1197 errno, strerror(errno), DisplayString(disp),
1198 NextRequest(disp) - 1, LastKnownRequestProcessed(disp),
1202 /* According to X specs, we should not return from this function, or
1203 Xlib might just decide to exit(). So we mark the offending
1204 console for deletion and throw to top level. */
1206 enqueue_magic_eval_event(io_error_delete_device, dev);
1207 DEVICE_X_BEING_DELETED(d) = 1;
1209 Fthrow(Qtop_level, Qnil);
1211 return 0; /* not reached */
1214 DEFUN("x-debug-mode", Fx_debug_mode, 1, 2, 0, /*
1215 With a true arg, make the connection to the X server synchronous.
1216 With false, make it asynchronous. Synchronous connections are much slower,
1217 but are useful for debugging. (If you get X errors, make the connection
1218 synchronous, and use a debugger to set a breakpoint on `x_error_handler'.
1219 Your backtrace of the C stack will now be useful. In asynchronous mode,
1220 the stack above `x_error_handler' isn't helpful because of buffering.)
1221 If DEVICE is not specified, the selected device is assumed.
1223 Calling this function is the same as calling the C function `XSynchronize',
1224 or starting the program with the `-sync' command line argument.
1228 struct device *d = decode_x_device(device);
1230 XSynchronize(DEVICE_X_DISPLAY(d), !NILP(arg));
1233 message("X connection is synchronous");
1235 message("X connection is asynchronous");
1240 /************************************************************************/
1242 /************************************************************************/
1244 #if 0 /* bah humbug. The whole "widget == resource" stuff is such
1245 a crock of shit that I'm just going to ignore it all. */
1247 /* If widget is NULL, we are retrieving device or global face data. */
1250 construct_name_list(Display * display, Widget widget, char *fake_name,
1251 char *fake_class, char *name, char *class)
1253 char *stack[100][2];
1256 char *name_tail, *class_tail;
1259 for (this = widget; this; this = XtParent(this)) {
1260 stack[count][0] = this->core.name;
1261 stack[count][1] = XtClass(this)->core_class.class_name;
1265 } else if (fake_name && fake_class) {
1266 stack[count][0] = fake_name;
1267 stack[count][1] = fake_class;
1271 /* The root widget is an application shell; resource lookups use the
1272 specified application name and application class in preference to
1273 the name/class of that widget (which is argv[0] / "ApplicationShell").
1274 Generally the app name and class will be argv[0] / "Emacs" but
1275 the former can be set via the -name command-line option, and the
1276 latter can be set by changing `x-emacs-application-class' in
1279 XtGetApplicationNameAndClass(display,
1280 &stack[count][0], &stack[count][1]);
1287 for (; count >= 0; count--) {
1288 strcat(name_tail, stack[count][0]);
1289 for (; *name_tail; name_tail++)
1290 if (*name_tail == '.')
1292 strcat(name_tail, ".");
1295 strcat(class_tail, stack[count][1]);
1296 for (; *class_tail; class_tail++)
1297 if (*class_tail == '.')
1299 strcat(class_tail, ".");
1306 /* strcasecmp() is not sufficiently portable or standard,
1307 and it's easier just to write our own. */
1308 static int ascii_strcasecmp(const char *s1, const char *s2)
1313 if (c1 >= 'A' && c1 <= 'Z')
1315 if (c2 >= 'A' && c2 <= 'Z')
1324 static char_dynarr *name_char_dynarr;
1325 static char_dynarr *class_char_dynarr;
1327 /* Given a locale and device specification from x-get-resource or
1328 x-get-resource-prefix, return the resource prefix and display to
1329 fetch the resource on. */
1332 x_get_resource_prefix(Lisp_Object locale, Lisp_Object device,
1333 Display ** display_out, char_dynarr * name,
1334 char_dynarr * class)
1338 if (NILP(Fvalid_specifier_locale_p(locale)))
1339 signal_simple_error("Invalid locale", locale);
1340 if (WINDOWP(locale))
1341 /* #### I can't come up with any coherent way of naming windows.
1342 By relative position? That seems tricky because windows
1343 can change position, be split, etc. By order of creation?
1344 That seems less than useful. */
1345 signal_simple_error("Windows currently can't be resourced",
1348 if (!NILP(device) && !DEVICEP(device))
1349 CHECK_DEVICE(device);
1350 if (DEVICEP(device) && !DEVICE_X_P(XDEVICE(device)))
1353 device = DFW_DEVICE(locale);
1354 if (DEVICEP(device) && !DEVICE_X_P(XDEVICE(device)))
1357 device = Vdefault_x_device;
1364 *display_out = DEVICE_X_DISPLAY(XDEVICE(device));
1367 char *appname, *appclass;
1368 int name_len, class_len;
1369 XtGetApplicationNameAndClass(*display_out, &appname, &appclass);
1370 name_len = strlen(appname);
1371 class_len = strlen(appclass);
1372 Dynarr_add_many(name, appname, name_len);
1373 Dynarr_add_many(class, appclass, class_len);
1374 validify_resource_component(Dynarr_atp(name, 0), name_len);
1375 validify_resource_component(Dynarr_atp(class, 0), class_len);
1378 if (EQ(locale, Qglobal))
1380 if (BUFFERP(locale)) {
1381 Dynarr_add_literal_string(name, ".buffer.");
1382 /* we know buffer is live; otherwise we got an error above. */
1383 Dynarr_add_validified_lisp_string(name, Fbuffer_name(locale));
1384 Dynarr_add_literal_string(class,
1385 ".EmacsLocaleType.EmacsBuffer");
1386 } else if (FRAMEP(locale)) {
1387 Dynarr_add_literal_string(name, ".frame.");
1388 /* we know frame is live; otherwise we got an error above. */
1389 Dynarr_add_validified_lisp_string(name, Fframe_name(locale));
1390 Dynarr_add_literal_string(class, ".EmacsLocaleType.EmacsFrame");
1392 assert(DEVICEP(locale));
1393 Dynarr_add_literal_string(name, ".device.");
1394 /* we know device is live; otherwise we got an error above. */
1395 Dynarr_add_validified_lisp_string(name, Fdevice_name(locale));
1396 Dynarr_add_literal_string(class,
1397 ".EmacsLocaleType.EmacsDevice");
1402 DEFUN("x-get-resource", Fx_get_resource, 3, 6, 0, /*
1403 Retrieve an X resource from the resource manager.
1405 The first arg is the name of the resource to retrieve, such as "font".
1406 The second arg is the class of the resource to retrieve, such as "Font".
1407 The third arg must be one of the symbols 'string, 'integer, 'natnum, or
1408 'boolean, specifying the type of object that the database is searched for.
1409 The fourth arg is the locale to search for the resources on, and can
1410 currently be a buffer, a frame, a device, or 'global. If omitted, it
1411 defaults to 'global.
1412 The fifth arg is the device to search for the resources on. (The resource
1413 database for a particular device is constructed by combining non-device-
1414 specific resources such as any command-line resources specified and any
1415 app-defaults files found [or the fallback resources supplied by SXEmacs,
1416 if no app-defaults file is found] with device-specific resources such as
1417 those supplied using xrdb.) If omitted, it defaults to the device of
1418 LOCALE, if a device can be derived (i.e. if LOCALE is a frame or device),
1419 and otherwise defaults to the value of `default-x-device'.
1420 The sixth arg NOERROR, if non-nil, means do not signal an error if a
1421 bogus resource specification was retrieved (e.g. if a non-integer was
1422 given when an integer was requested). In this case, a warning is issued
1423 instead, unless NOERROR is t, in which case no warning is issued.
1425 The resource names passed to this function are looked up relative to the
1428 If you want to search for a subresource, you just need to specify the
1429 resource levels in NAME and CLASS. For example, NAME could be
1430 "modeline.attributeFont", and CLASS "Face.AttributeFont".
1434 1) If LOCALE is a buffer, a call
1436 (x-get-resource "foreground" "Foreground" 'string SOME-BUFFER)
1438 is an interface to a C call something like
1440 XrmGetResource (db, "sxemacs.buffer.BUFFER-NAME.foreground",
1441 "Emacs.EmacsLocaleType.EmacsBuffer.Foreground",
1444 2) If LOCALE is a frame, a call
1446 (x-get-resource "foreground" "Foreground" 'string SOME-FRAME)
1448 is an interface to a C call something like
1450 XrmGetResource (db, "sxemacs.frame.FRAME-NAME.foreground",
1451 "Emacs.EmacsLocaleType.EmacsFrame.Foreground",
1454 3) If LOCALE is a device, a call
1456 (x-get-resource "foreground" "Foreground" 'string SOME-DEVICE)
1458 is an interface to a C call something like
1460 XrmGetResource (db, "sxemacs.device.DEVICE-NAME.foreground",
1461 "Emacs.EmacsLocaleType.EmacsDevice.Foreground",
1464 4) If LOCALE is 'global, a call
1466 (x-get-resource "foreground" "Foreground" 'string 'global)
1468 is an interface to a C call something like
1470 XrmGetResource (db, "sxemacs.foreground",
1474 Note that for 'global, no prefix is added other than that of the
1475 application itself; thus, you can use this locale to retrieve
1476 arbitrary application resources, if you really want to.
1478 The returned value of this function is nil if the queried resource is not
1479 found. If the third arg is `string', a string is returned, and if it is
1480 `integer', an integer is returned. If the third arg is `boolean', then the
1481 returned value is the list (t) for true, (nil) for false, and is nil to
1482 mean ``unspecified''.
1484 (name, class, type, locale, device, noerror))
1486 char *name_string, *class_string;
1490 Error_behavior errb = decode_error_behavior_flag(noerror);
1493 CHECK_STRING(class);
1496 Dynarr_reset(name_char_dynarr);
1497 Dynarr_reset(class_char_dynarr);
1499 x_get_resource_prefix(locale, device, &display,
1500 name_char_dynarr, class_char_dynarr);
1504 db = XtDatabase(display);
1506 Dynarr_add(name_char_dynarr, '.');
1507 Dynarr_add_lisp_string(name_char_dynarr, name);
1508 Dynarr_add(class_char_dynarr, '.');
1509 Dynarr_add_lisp_string(class_char_dynarr, class);
1510 Dynarr_add(name_char_dynarr, '\0');
1511 Dynarr_add(class_char_dynarr, '\0');
1513 name_string = Dynarr_atp(name_char_dynarr, 0);
1514 class_string = Dynarr_atp(class_char_dynarr, 0);
1518 XrmName namelist[100];
1519 XrmClass classlist[100];
1520 XrmName *namerest = namelist;
1521 XrmClass *classrest = classlist;
1522 XrmRepresentation xrm_type;
1523 XrmRepresentation string_quark;
1525 XrmStringToNameList(name_string, namelist);
1526 XrmStringToClassList(class_string, classlist);
1527 string_quark = XrmStringToQuark("String");
1529 /* ensure that they have the same length */
1530 while (namerest[0] && classrest[0])
1531 namerest++, classrest++;
1532 if (namerest[0] || classrest[0])
1533 signal_simple_error_2
1534 ("class list and name list must be the same length",
1537 XrmQGetResource(db, namelist, classlist, &xrm_type,
1540 if (result != True || xrm_type != string_quark)
1542 raw_result = (char *)xrm_value.addr;
1545 if (EQ(type, Qstring))
1546 return build_string(raw_result);
1547 else if (EQ(type, Qboolean)) {
1548 if (!ascii_strcasecmp(raw_result, "off") ||
1549 !ascii_strcasecmp(raw_result, "false") ||
1550 !ascii_strcasecmp(raw_result, "no"))
1551 return Fcons(Qnil, Qnil);
1552 if (!ascii_strcasecmp(raw_result, "on") ||
1553 !ascii_strcasecmp(raw_result, "true") ||
1554 !ascii_strcasecmp(raw_result, "yes"))
1555 return Fcons(Qt, Qnil);
1556 return maybe_continuable_error
1558 "can't convert %s: %s to a Boolean", name_string,
1560 } else if (EQ(type, Qinteger) || EQ(type, Qnatnum)) {
1563 if (1 != sscanf(raw_result, "%d%c", &i, &c))
1564 return maybe_continuable_error
1566 "can't convert %s: %s to an integer", name_string,
1568 else if (EQ(type, Qnatnum) && i < 0)
1569 return maybe_continuable_error
1571 "invalid numerical value %d for resource %s", i,
1576 return maybe_signal_continuable_error
1577 (Qwrong_type_argument,
1578 list2(build_translated_string
1579 ("should be string, integer, natnum or boolean"),
1580 type), Qresource, errb);
1584 DEFUN("x-get-resource-prefix", Fx_get_resource_prefix, 1, 2, 0, /*
1585 Return the resource prefix for LOCALE on DEVICE.
1586 The resource prefix is the strings used to prefix resources if
1587 the LOCALE and DEVICE arguments were passed to `x-get-resource'.
1588 The returned value is a cons of a name prefix and a class prefix.
1589 For example, if LOCALE is a frame, the returned value might be
1590 \("sxemacs.frame.FRAME-NAME" . "Emacs.EmacsLocaleType.EmacsFrame").
1591 If no valid X device for resourcing can be obtained, this function
1592 returns nil. (In such a case, `x-get-resource' would always return nil.)
1598 Dynarr_reset(name_char_dynarr);
1599 Dynarr_reset(class_char_dynarr);
1601 x_get_resource_prefix(locale, device, &display,
1602 name_char_dynarr, class_char_dynarr);
1606 return Fcons(make_string((Bufbyte *) Dynarr_atp(name_char_dynarr, 0),
1607 Dynarr_length(name_char_dynarr)),
1608 make_string((Bufbyte *) Dynarr_atp(class_char_dynarr, 0),
1609 Dynarr_length(class_char_dynarr)));
1612 DEFUN("x-put-resource", Fx_put_resource, 1, 2, 0, /*
1613 Add a resource to the resource database for DEVICE.
1614 RESOURCE-LINE specifies the resource to add and should be a
1615 standard resource specification.
1617 (resource_line, device))
1619 struct device *d = decode_device(device);
1620 char *str, *colon_pos;
1622 CHECK_STRING(resource_line);
1623 str = (char *)XSTRING_DATA(resource_line);
1624 if (!(colon_pos = strchr(str, ':')) || strchr(str, '\n'))
1626 signal_simple_error("Invalid resource line", resource_line);
1628 /* Only the following chars are allowed before the colon */
1629 " \t.*?abcdefghijklmnopqrstuvwxyz"
1630 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-")
1631 != (size_t) (colon_pos - str))
1634 if (DEVICE_X_P(d)) {
1635 XrmDatabase db = XtDatabase(DEVICE_X_DISPLAY(d));
1636 XrmPutLineResource(&db, str);
1642 /************************************************************************/
1643 /* display information functions */
1644 /************************************************************************/
1646 DEFUN("default-x-device", Fdefault_x_device, 0, 0, 0, /*
1647 Return the default X device for resourcing.
1648 This is the first-created X device that still exists.
1652 return Vdefault_x_device;
1655 DEFUN("x-display-visual-class", Fx_display_visual_class, 0, 1, 0, /*
1656 Return the visual class of the X display DEVICE is using.
1657 This can be altered from the default at startup using the XResource "EmacsVisual".
1658 The returned value will be one of the symbols `static-gray', `gray-scale',
1659 `static-color', `pseudo-color', `true-color', or `direct-color'.
1663 Visual *vis = DEVICE_X_VISUAL(decode_x_device(device));
1664 switch (vis->class) {
1666 return intern("static-gray");
1668 return intern("gray-scale");
1670 return intern("static-color");
1672 return intern("pseudo-color");
1674 return intern("true-color");
1676 return intern("direct-color");
1678 error("display has an unknown visual class");
1679 return Qnil; /* suppress compiler warning */
1683 DEFUN("x-display-visual-depth", Fx_display_visual_depth, 0, 1, 0, /*
1684 Return the bitplane depth of the visual the X display DEVICE is using.
1688 return make_int(DEVICE_X_DEPTH(decode_x_device(device)));
1692 x_device_system_metrics(struct device *d, enum device_metrics m)
1694 Display *dpy = DEVICE_X_DISPLAY(d);
1696 switch ((unsigned int)m) {
1697 case DM_size_device:
1698 return Fcons(make_int(DisplayWidth(dpy, DefaultScreen(dpy))),
1699 make_int(DisplayHeight(dpy, DefaultScreen(dpy))));
1700 case DM_size_device_mm:
1701 return Fcons(make_int(DisplayWidthMM(dpy, DefaultScreen(dpy))),
1702 make_int(DisplayHeightMM
1703 (dpy, DefaultScreen(dpy))));
1704 case DM_num_bit_planes:
1705 return make_int(DisplayPlanes(dpy, DefaultScreen(dpy)));
1706 case DM_num_color_cells:
1707 return make_int(DisplayCells(dpy, DefaultScreen(dpy)));
1710 /* No such device metric property for X devices */
1715 DEFUN("x-server-vendor", Fx_server_vendor, 0, 1, 0, /*
1716 Return the vendor ID string of the X server DEVICE is on.
1717 Return the empty string if the vendor ID string cannot be determined.
1721 Display *dpy = get_x_display(device);
1722 char *vendor = ServerVendor(dpy);
1724 return build_string(vendor ? vendor : "");
1727 DEFUN("x-server-version", Fx_server_version, 0, 1, 0, /*
1728 Return the version numbers of the X server DEVICE is on.
1729 The returned value is a list of three integers: the major and minor
1730 version numbers of the X Protocol in use, and the vendor-specific release
1731 number. See also `x-server-vendor'.
1735 Display *dpy = get_x_display(device);
1737 return list3(make_int(ProtocolVersion(dpy)),
1738 make_int(ProtocolRevision(dpy)),
1739 make_int(VendorRelease(dpy)));
1742 DEFUN("x-valid-keysym-name-p", Fx_valid_keysym_name_p, 1, 1, 0, /*
1743 Return true if KEYSYM names a keysym that the X library knows about.
1744 Valid keysyms are listed in the files /usr/include/X11/keysymdef.h and in
1745 /usr/lib/X11/XKeysymDB, or whatever the equivalents are on your system.
1749 const char *keysym_ext;
1751 CHECK_STRING(keysym);
1752 LISP_STRING_TO_EXTERNAL(keysym, keysym_ext, Qctext);
1754 return XStringToKeysym(keysym_ext) ? Qt : Qnil;
1757 DEFUN("x-keysym-hash-table", Fx_keysym_hash_table, 0, 1, 0, /*
1758 Return a hash table containing a key for all keysyms on DEVICE.
1759 DEVICE must be an X11 display device. See `x-keysym-on-keyboard-p'.
1763 struct device *d = decode_device(device);
1765 signal_simple_error("Not an X device", device);
1767 return DEVICE_X_DATA(d)->x_keysym_map_hash_table;
1770 DEFUN("x-keysym-on-keyboard-sans-modifiers-p", Fx_keysym_on_keyboard_sans_modifiers_p, 1, 2, 0, /*
1771 Return true if KEYSYM names a key on the keyboard of DEVICE.
1772 More precisely, return true if pressing a physical key
1773 on the keyboard of DEVICE without any modifier keys generates KEYSYM.
1774 Valid keysyms are listed in the files /usr/include/X11/keysymdef.h and in
1775 /usr/lib/X11/XKeysymDB, or whatever the equivalents are on your system.
1776 The keysym name can be provided in two forms:
1777 - if keysym is a string, it must be the name as known to X windows.
1778 - if keysym is a symbol, it must be the name as known to SXEmacs.
1779 The two names differ in capitalization and underscoring.
1783 struct device *d = decode_device(device);
1785 signal_simple_error("Not an X device", device);
1787 return (EQ(Qsans_modifiers,
1788 Fgethash(keysym, DEVICE_X_KEYSYM_MAP_HASH_TABLE(d), Qnil)) ?
1792 DEFUN("x-keysym-on-keyboard-p", Fx_keysym_on_keyboard_p, 1, 2, 0, /*
1793 Return true if KEYSYM names a key on the keyboard of DEVICE.
1794 More precisely, return true if some keystroke (possibly including modifiers)
1795 on the keyboard of DEVICE keys generates KEYSYM.
1796 Valid keysyms are listed in the files /usr/include/X11/keysymdef.h and in
1797 /usr/lib/X11/XKeysymDB, or whatever the equivalents are on your system.
1798 The keysym name can be provided in two forms:
1799 - if keysym is a string, it must be the name as known to X windows.
1800 - if keysym is a symbol, it must be the name as known to SXEmacs.
1801 The two names differ in capitalization and underscoring.
1805 struct device *d = decode_device(device);
1807 signal_simple_error("Not an X device", device);
1809 return (NILP(Fgethash(keysym, DEVICE_X_KEYSYM_MAP_HASH_TABLE(d), Qnil))
1813 /************************************************************************/
1814 /* grabs and ungrabs */
1815 /************************************************************************/
1817 DEFUN("x-grab-pointer", Fx_grab_pointer, 0, 3, 0, /*
1818 Grab the pointer and restrict it to its current window.
1819 If optional DEVICE argument is nil, the default device will be used.
1820 If optional CURSOR argument is non-nil, change the pointer shape to that
1821 until `x-ungrab-pointer' is called (it should be an object returned by the
1822 `make-cursor-glyph' function).
1823 If the second optional argument IGNORE-KEYBOARD is non-nil, ignore all
1824 keyboard events during the grab.
1825 Returns t if the grab is successful, nil otherwise.
1827 (device, cursor, ignore_keyboard))
1830 int pointer_mode, result;
1831 struct device *d = decode_x_device(device);
1833 if (!NILP(cursor)) {
1834 CHECK_POINTER_GLYPH(cursor);
1835 cursor = glyph_image_instance(cursor, device, ERROR_ME, 0);
1838 if (!NILP(ignore_keyboard))
1839 pointer_mode = GrabModeSync;
1841 pointer_mode = GrabModeAsync;
1843 w = XtWindow(FRAME_X_TEXT_WIDGET(device_selected_frame(d)));
1845 /* #### Possibly this needs to gcpro the cursor somehow, but it doesn't
1846 seem to cause a problem if XFreeCursor is called on a cursor in use
1847 in a grab; I suppose the X server counts the grab as a reference
1848 and doesn't free it until it exits? */
1849 result = XGrabPointer(DEVICE_X_DISPLAY(d), w, False, ButtonMotionMask | ButtonPressMask | ButtonReleaseMask | PointerMotionHintMask, GrabModeAsync, /* Keep pointer events flowing */
1850 pointer_mode, /* Stall keyboard events */
1851 w, /* Stay in this window */
1853 : XIMAGE_INSTANCE_X_CURSOR(cursor)),
1855 return (result == GrabSuccess) ? Qt : Qnil;
1858 DEFUN("x-ungrab-pointer", Fx_ungrab_pointer, 0, 1, 0, /*
1859 Release a pointer grab made with `x-grab-pointer'.
1860 If optional first arg DEVICE is nil the default device is used.
1861 If it is t the pointer will be released on all X devices.
1865 if (!EQ(device, Qt)) {
1866 Display *dpy = get_x_display(device);
1867 XUngrabPointer(dpy, CurrentTime);
1869 Lisp_Object devcons, concons;
1871 DEVICE_LOOP_NO_BREAK(devcons, concons) {
1872 struct device *d = XDEVICE(XCAR(devcons));
1875 XUngrabPointer(DEVICE_X_DISPLAY(d),
1883 DEFUN("x-grab-keyboard", Fx_grab_keyboard, 0, 1, 0, /*
1884 Grab the keyboard on the given device (defaulting to the selected one).
1885 So long as the keyboard is grabbed, all keyboard events will be delivered
1886 to emacs -- it is not possible for other X clients to eavesdrop on them.
1887 Ungrab the keyboard with `x-ungrab-keyboard' (use an unwind-protect).
1888 Returns t if the grab is successful, nil otherwise.
1892 struct device *d = decode_x_device(device);
1893 Window w = XtWindow(FRAME_X_TEXT_WIDGET(device_selected_frame(d)));
1894 Display *dpy = DEVICE_X_DISPLAY(d);
1897 status = XGrabKeyboard(dpy, w, True,
1898 /* I don't really understand sync-vs-async
1899 grabs, but this is what xterm does. */
1900 GrabModeAsync, GrabModeAsync,
1901 /* Use the timestamp of the last user action
1902 read by emacs proper; xterm uses CurrentTime
1903 but there's a comment that says "wrong"...
1904 (Despite the name this is the time of the
1905 last key or mouse event.) */
1906 DEVICE_X_MOUSE_TIMESTAMP(d));
1907 if (status == GrabSuccess) {
1908 /* The XUngrabKeyboard should generate a FocusIn back to this
1909 window but it doesn't unless we explicitly set focus to the
1910 window first (which should already have it. The net result
1911 is that without this call when x-ungrab-keyboard is called
1912 the selected frame ends up not having focus. */
1913 XSetInputFocus(dpy, w, RevertToParent,
1914 DEVICE_X_MOUSE_TIMESTAMP(d));
1920 DEFUN("x-ungrab-keyboard", Fx_ungrab_keyboard, 0, 1, 0, /*
1921 Release a keyboard grab made with `x-grab-keyboard'.
1925 Display *dpy = get_x_display(device);
1926 XUngrabKeyboard(dpy, CurrentTime);
1930 DEFUN("x-get-font-path", Fx_get_font_path, 0, 1, 0, /*
1931 Get the X Server's font path.
1933 See also `x-set-font-path'.
1937 Display *dpy = get_x_display(device);
1939 const char **directories =
1940 (const char **)XGetFontPath(dpy, &ndirs_return);
1941 Lisp_Object font_path = Qnil;
1944 signal_simple_error("Can't get X font path", device);
1946 while (ndirs_return--)
1947 font_path = Fcons(build_ext_string(directories[ndirs_return],
1948 Qfile_name), font_path);
1953 DEFUN("x-set-font-path", Fx_set_font_path, 1, 2, 0, /*
1954 Set the X Server's font path to FONT-PATH.
1956 There is only one font path per server, not one per client. Use this
1957 sparingly. It uncaches all of the X server's font information.
1959 Font directories should end in the path separator and should contain
1960 a file called fonts.dir usually created with the program mkfontdir.
1962 Setting the FONT-PATH to nil tells the X server to use the default
1965 See also `x-get-font-path'.
1967 (font_path, device))
1969 Display *dpy = get_x_display(device);
1970 Lisp_Object path_entry;
1971 int i = 0, ndirs = 0;
1973 EXTERNAL_LIST_LOOP(path_entry, font_path) {
1974 CHECK_STRING(XCAR(path_entry));
1979 char *directories[ndirs];
1981 EXTERNAL_LIST_LOOP(path_entry, font_path) {
1982 LISP_STRING_TO_EXTERNAL(XCAR(path_entry),
1987 expect_x_error(dpy);
1988 XSetFontPath(dpy, (char **)directories, ndirs);
1989 signal_if_x_error(dpy, 1 /*resumable_p */ );
1994 /************************************************************************/
1995 /* initialization */
1996 /************************************************************************/
1998 void syms_of_device_x(void)
2000 DEFSUBR(Fx_debug_mode);
2001 DEFSUBR(Fx_get_resource);
2002 DEFSUBR(Fx_get_resource_prefix);
2003 DEFSUBR(Fx_put_resource);
2005 DEFSUBR(Fdefault_x_device);
2006 DEFSUBR(Fx_display_visual_class);
2007 DEFSUBR(Fx_display_visual_depth);
2008 DEFSUBR(Fx_server_vendor);
2009 DEFSUBR(Fx_server_version);
2010 DEFSUBR(Fx_valid_keysym_name_p);
2011 DEFSUBR(Fx_keysym_hash_table);
2012 DEFSUBR(Fx_keysym_on_keyboard_p);
2013 DEFSUBR(Fx_keysym_on_keyboard_sans_modifiers_p);
2015 DEFSUBR(Fx_grab_pointer);
2016 DEFSUBR(Fx_ungrab_pointer);
2017 DEFSUBR(Fx_grab_keyboard);
2018 DEFSUBR(Fx_ungrab_keyboard);
2020 DEFSUBR(Fx_get_font_path);
2021 DEFSUBR(Fx_set_font_path);
2023 defsymbol(&Qx_error, "x-error");
2024 defsymbol(&Qinit_pre_x_win, "init-pre-x-win");
2025 defsymbol(&Qinit_post_x_win, "init-post-x-win");
2028 void reinit_console_type_create_device_x(void)
2030 /* Initialize variables to speed up X resource interactions */
2031 const char *valid_resource_chars =
2032 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
2033 while (*valid_resource_chars)
2034 valid_resource_char_p[(unsigned int)(*valid_resource_chars++)] =
2037 name_char_dynarr = Dynarr_new(char);
2038 class_char_dynarr = Dynarr_new(char);
2041 void console_type_create_device_x(void)
2043 reinit_console_type_create_device_x();
2044 CONSOLE_HAS_METHOD(x, init_device);
2045 CONSOLE_HAS_METHOD(x, finish_init_device);
2046 CONSOLE_HAS_METHOD(x, mark_device);
2047 CONSOLE_HAS_METHOD(x, delete_device);
2048 CONSOLE_HAS_METHOD(x, device_system_metrics);
2051 void reinit_vars_of_device_x(void)
2056 in_resource_setting = 0;
2059 void vars_of_device_x(void)
2061 reinit_vars_of_device_x();
2063 DEFVAR_LISP("x-emacs-application-class", &Vx_emacs_application_class /*
2064 The X application class of the SXEmacs process.
2065 This controls, among other things, the name of the `app-defaults' file
2066 that SXEmacs will use. For changes to this variable to take effect, they
2067 must be made before the connection to the X server is initialized, that is,
2068 this variable may only be changed before emacs is dumped, or by setting it
2069 in the file lisp/term/x-win.el.
2071 If this variable is nil before the connection to the X server is first
2072 initialized (which it is by default), the X resource database will be
2073 consulted and the value will be set according to whether any resources
2074 are found for the application class `SXEmacs'. If the user has set any
2075 resources for the SXEmacs application class, the SXEmacs process will use
2076 the application class `SXEmacs'. Otherwise, the SXEmacs process will use
2077 the application class `Emacs' which is backwards compatible to previous
2078 SXEmacs versions but may conflict with resources intended for GNU Emacs.
2080 Vx_emacs_application_class = Qnil;
2082 DEFVAR_LISP("x-initial-argv-list", &Vx_initial_argv_list /*
2083 You don't want to know.
2084 This is used during startup to communicate the remaining arguments in
2085 `command-line-args-left' to the C code, which passes the args to
2086 the X initialization code, which removes some args, and then the
2087 args are placed back into `x-initial-arg-list' and thence into
2088 `command-line-args-left'. Perhaps `command-line-args-left' should
2091 Vx_initial_argv_list = Qnil;
2093 #if defined(MULE) && (defined(LWLIB_MENUBARS_MOTIF) || defined(HAVE_XIM) || defined (USE_XFONTSET))
2094 DEFVAR_LISP("x-app-defaults-directory", &Vx_app_defaults_directory /*
2095 Used by the Lisp code to communicate to the low level X initialization
2096 where the localized init files are.
2098 Vx_app_defaults_directory = Qnil;
2103 staticpro(&Vdefault_x_device);
2104 Vdefault_x_device = Qnil;