Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / src / xfns.c
blob39c895301dcf586bab219a02f2a4faccb3a34a97
1 /* Functions for the X window system.
3 Copyright (C) 1989, 1992-2014 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs 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 GNU Emacs 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 #include <config.h>
21 #include <stdio.h>
22 #include <math.h>
23 #include <unistd.h>
25 #include "lisp.h"
26 #include "xterm.h"
27 #include "frame.h"
28 #include "window.h"
29 #include "character.h"
30 #include "buffer.h"
31 #include "intervals.h"
32 #include "dispextern.h"
33 #include "keyboard.h"
34 #include "blockinput.h"
35 #include <epaths.h>
36 #include "charset.h"
37 #include "coding.h"
38 #include "fontset.h"
39 #include "systime.h"
40 #include "termhooks.h"
41 #include "atimer.h"
42 #include "termchar.h"
43 #include "font.h"
45 #include <sys/types.h>
46 #include <sys/stat.h>
48 #if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work. */
49 #include "bitmaps/gray.xbm"
50 #else
51 #include <X11/bitmaps/gray>
52 #endif
54 #include "xsettings.h"
56 #ifdef HAVE_XRANDR
57 #include <X11/extensions/Xrandr.h>
58 #endif
59 #ifdef HAVE_XINERAMA
60 #include <X11/extensions/Xinerama.h>
61 #endif
63 #ifdef USE_GTK
64 #include "gtkutil.h"
65 #endif
67 #ifdef USE_X_TOOLKIT
68 #include <X11/Shell.h>
70 #ifndef USE_MOTIF
71 #ifdef HAVE_XAW3D
72 #include <X11/Xaw3d/Paned.h>
73 #include <X11/Xaw3d/Label.h>
74 #else /* !HAVE_XAW3D */
75 #include <X11/Xaw/Paned.h>
76 #include <X11/Xaw/Label.h>
77 #endif /* HAVE_XAW3D */
78 #endif /* USE_MOTIF */
80 #ifdef USG
81 #undef USG /* ####KLUDGE for Solaris 2.2 and up */
82 #include <X11/Xos.h>
83 #define USG
84 #ifdef USG /* Pacify gcc -Wunused-macros. */
85 #endif
86 #else
87 #include <X11/Xos.h>
88 #endif
90 #include "widget.h"
92 #include "../lwlib/lwlib.h"
94 #ifdef USE_MOTIF
95 #include <Xm/Xm.h>
96 #include <Xm/DialogS.h>
97 #include <Xm/FileSB.h>
98 #include <Xm/List.h>
99 #include <Xm/TextF.h>
100 #endif
102 #ifdef USE_LUCID
103 #include "../lwlib/xlwmenu.h"
104 #endif
106 #if !defined (NO_EDITRES)
107 #define HACK_EDITRES
108 extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
109 #endif /* not defined NO_EDITRES */
111 /* Unique id counter for widgets created by the Lucid Widget Library. */
113 extern LWLIB_ID widget_id_tick;
115 #ifdef USE_MOTIF
117 #endif /* USE_MOTIF */
119 #endif /* USE_X_TOOLKIT */
121 #ifdef USE_GTK
123 #endif /* USE_GTK */
125 #define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
127 static Lisp_Object Qsuppress_icon;
128 static Lisp_Object Qundefined_color;
129 static Lisp_Object Qcompound_text, Qcancel_timer;
130 Lisp_Object Qfont_param;
132 #ifdef GLYPH_DEBUG
133 static ptrdiff_t image_cache_refcount;
134 static int dpyinfo_refcount;
135 #endif
137 static struct x_display_info *x_display_info_for_name (Lisp_Object);
139 /* Let the user specify an X display with a Lisp object.
140 OBJECT may be nil, a frame or a terminal object.
141 nil stands for the selected frame--or, if that is not an X frame,
142 the first X display on the list. */
144 struct x_display_info *
145 check_x_display_info (Lisp_Object object)
147 struct x_display_info *dpyinfo = NULL;
149 if (NILP (object))
151 struct frame *sf = XFRAME (selected_frame);
153 if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
154 dpyinfo = FRAME_DISPLAY_INFO (sf);
155 else if (x_display_list != 0)
156 dpyinfo = x_display_list;
157 else
158 error ("X windows are not in use or not initialized");
160 else if (TERMINALP (object))
162 struct terminal *t = get_terminal (object, 1);
164 if (t->type != output_x_window)
165 error ("Terminal %d is not an X display", t->id);
167 dpyinfo = t->display_info.x;
169 else if (STRINGP (object))
170 dpyinfo = x_display_info_for_name (object);
171 else
173 struct frame *f = decode_window_system_frame (object);
174 dpyinfo = FRAME_DISPLAY_INFO (f);
177 return dpyinfo;
180 /* Store the screen positions of frame F into XPTR and YPTR.
181 These are the positions of the containing window manager window,
182 not Emacs's own window. */
184 void
185 x_real_positions (struct frame *f, int *xptr, int *yptr)
187 int win_x, win_y, outer_x IF_LINT (= 0), outer_y IF_LINT (= 0);
188 int real_x = 0, real_y = 0;
189 int had_errors = 0;
190 Window win = f->output_data.x->parent_desc;
191 Atom actual_type;
192 unsigned long actual_size, bytes_remaining;
193 int rc, actual_format;
194 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
195 long max_len = 400;
196 Display *dpy = FRAME_X_DISPLAY (f);
197 unsigned char *tmp_data = NULL;
198 Atom target_type = XA_CARDINAL;
200 block_input ();
202 x_catch_errors (dpy);
204 if (win == dpyinfo->root_window)
205 win = FRAME_OUTER_WINDOW (f);
207 /* This loop traverses up the containment tree until we hit the root
208 window. Window managers may intersect many windows between our window
209 and the root window. The window we find just before the root window
210 should be the outer WM window. */
211 for (;;)
213 Window wm_window, rootw;
214 Window *tmp_children;
215 unsigned int tmp_nchildren;
216 int success;
218 success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
219 &wm_window, &tmp_children, &tmp_nchildren);
221 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
223 /* Don't free tmp_children if XQueryTree failed. */
224 if (! success)
225 break;
227 XFree (tmp_children);
229 if (wm_window == rootw || had_errors)
230 break;
232 win = wm_window;
235 if (! had_errors)
237 unsigned int ign;
238 Window child, rootw;
240 /* Get the real coordinates for the WM window upper left corner */
241 XGetGeometry (FRAME_X_DISPLAY (f), win,
242 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
244 /* Translate real coordinates to coordinates relative to our
245 window. For our window, the upper left corner is 0, 0.
246 Since the upper left corner of the WM window is outside
247 our window, win_x and win_y will be negative:
249 ------------------ ---> x
250 | title |
251 | ----------------- v y
252 | | our window
254 XTranslateCoordinates (FRAME_X_DISPLAY (f),
256 /* From-window, to-window. */
257 FRAME_DISPLAY_INFO (f)->root_window,
258 FRAME_X_WINDOW (f),
260 /* From-position, to-position. */
261 real_x, real_y, &win_x, &win_y,
263 /* Child of win. */
264 &child);
266 if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
268 outer_x = win_x;
269 outer_y = win_y;
271 else
273 XTranslateCoordinates (FRAME_X_DISPLAY (f),
275 /* From-window, to-window. */
276 FRAME_DISPLAY_INFO (f)->root_window,
277 FRAME_OUTER_WINDOW (f),
279 /* From-position, to-position. */
280 real_x, real_y, &outer_x, &outer_y,
282 /* Child of win. */
283 &child);
286 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
290 if (dpyinfo->root_window == f->output_data.x->parent_desc)
292 /* Try _NET_FRAME_EXTENTS if our parent is the root window. */
293 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_frame_extents,
294 0, max_len, False, target_type,
295 &actual_type, &actual_format, &actual_size,
296 &bytes_remaining, &tmp_data);
298 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
299 && actual_size == 4 && actual_format == 32)
301 unsigned int ign;
302 Window rootw;
303 long *fe = (long *)tmp_data;
305 XGetGeometry (FRAME_X_DISPLAY (f), win,
306 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
307 outer_x = -fe[0];
308 outer_y = -fe[2];
309 real_x -= fe[0];
310 real_y -= fe[2];
314 if (tmp_data) XFree (tmp_data);
316 x_uncatch_errors ();
318 unblock_input ();
320 if (had_errors) return;
322 f->x_pixels_diff = -win_x;
323 f->y_pixels_diff = -win_y;
325 FRAME_X_OUTPUT (f)->x_pixels_outer_diff = -outer_x;
326 FRAME_X_OUTPUT (f)->y_pixels_outer_diff = -outer_y;
328 *xptr = real_x;
329 *yptr = real_y;
335 /* Gamma-correct COLOR on frame F. */
337 void
338 gamma_correct (struct frame *f, XColor *color)
340 if (f->gamma)
342 color->red = pow (color->red / 65535.0, f->gamma) * 65535.0 + 0.5;
343 color->green = pow (color->green / 65535.0, f->gamma) * 65535.0 + 0.5;
344 color->blue = pow (color->blue / 65535.0, f->gamma) * 65535.0 + 0.5;
349 /* Decide if color named COLOR_NAME is valid for use on frame F. If
350 so, return the RGB values in COLOR. If ALLOC_P,
351 allocate the color. Value is false if COLOR_NAME is invalid, or
352 no color could be allocated. */
354 bool
355 x_defined_color (struct frame *f, const char *color_name,
356 XColor *color, bool alloc_p)
358 bool success_p = 0;
359 Display *dpy = FRAME_X_DISPLAY (f);
360 Colormap cmap = FRAME_X_COLORMAP (f);
362 block_input ();
363 #ifdef USE_GTK
364 success_p = xg_check_special_colors (f, color_name, color);
365 #endif
366 if (!success_p)
367 success_p = XParseColor (dpy, cmap, color_name, color) != 0;
368 if (success_p && alloc_p)
369 success_p = x_alloc_nearest_color (f, cmap, color);
370 unblock_input ();
372 return success_p;
376 /* Return the pixel color value for color COLOR_NAME on frame F. If F
377 is a monochrome frame, return MONO_COLOR regardless of what ARG says.
378 Signal an error if color can't be allocated. */
380 static int
381 x_decode_color (struct frame *f, Lisp_Object color_name, int mono_color)
383 XColor cdef;
385 CHECK_STRING (color_name);
387 #if 0 /* Don't do this. It's wrong when we're not using the default
388 colormap, it makes freeing difficult, and it's probably not
389 an important optimization. */
390 if (strcmp (SDATA (color_name), "black") == 0)
391 return BLACK_PIX_DEFAULT (f);
392 else if (strcmp (SDATA (color_name), "white") == 0)
393 return WHITE_PIX_DEFAULT (f);
394 #endif
396 /* Return MONO_COLOR for monochrome frames. */
397 if (FRAME_DISPLAY_INFO (f)->n_planes == 1)
398 return mono_color;
400 /* x_defined_color is responsible for coping with failures
401 by looking for a near-miss. */
402 if (x_defined_color (f, SSDATA (color_name), &cdef, 1))
403 return cdef.pixel;
405 signal_error ("Undefined color", color_name);
410 /* Change the `wait-for-wm' frame parameter of frame F. OLD_VALUE is
411 the previous value of that parameter, NEW_VALUE is the new value.
412 See also the comment of wait_for_wm in struct x_output. */
414 static void
415 x_set_wait_for_wm (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
417 f->output_data.x->wait_for_wm = !NILP (new_value);
420 static void
421 x_set_tool_bar_position (struct frame *f,
422 Lisp_Object new_value,
423 Lisp_Object old_value)
425 if (! EQ (new_value, Qleft) && ! EQ (new_value, Qright)
426 && ! EQ (new_value, Qbottom) && ! EQ (new_value, Qtop))
427 return;
428 if (EQ (new_value, old_value)) return;
430 #ifdef USE_GTK
431 xg_change_toolbar_position (f, new_value);
432 fset_tool_bar_position (f, new_value);
433 #endif
436 #ifdef USE_GTK
438 /* Set icon from FILE for frame F. By using GTK functions the icon
439 may be any format that GdkPixbuf knows about, i.e. not just bitmaps. */
442 xg_set_icon (struct frame *f, Lisp_Object file)
444 int result = 0;
445 Lisp_Object found;
447 found = x_find_image_file (file);
449 if (! NILP (found))
451 GdkPixbuf *pixbuf;
452 GError *err = NULL;
453 char *filename = SSDATA (found);
454 block_input ();
456 pixbuf = gdk_pixbuf_new_from_file (filename, &err);
458 if (pixbuf)
460 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
461 pixbuf);
462 g_object_unref (pixbuf);
464 result = 1;
466 else
467 g_error_free (err);
469 unblock_input ();
472 return result;
476 xg_set_icon_from_xpm_data (struct frame *f, const char **data)
478 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data (data);
480 if (!pixbuf)
481 return 0;
483 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
484 g_object_unref (pixbuf);
485 return 1;
487 #endif /* USE_GTK */
490 /* Functions called only from `x_set_frame_param'
491 to set individual parameters.
493 If FRAME_X_WINDOW (f) is 0,
494 the frame is being created and its X-window does not exist yet.
495 In that case, just record the parameter's new value
496 in the standard place; do not attempt to change the window. */
498 static void
499 x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
501 struct x_output *x = f->output_data.x;
502 unsigned long fg, old_fg;
504 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
505 old_fg = FRAME_FOREGROUND_PIXEL (f);
506 FRAME_FOREGROUND_PIXEL (f) = fg;
508 if (FRAME_X_WINDOW (f) != 0)
510 Display *dpy = FRAME_X_DISPLAY (f);
512 block_input ();
513 XSetForeground (dpy, x->normal_gc, fg);
514 XSetBackground (dpy, x->reverse_gc, fg);
516 if (x->cursor_pixel == old_fg)
518 unload_color (f, x->cursor_pixel);
519 x->cursor_pixel = x_copy_color (f, fg);
520 XSetBackground (dpy, x->cursor_gc, x->cursor_pixel);
523 unblock_input ();
525 update_face_from_frame_parameter (f, Qforeground_color, arg);
527 if (FRAME_VISIBLE_P (f))
528 redraw_frame (f);
531 unload_color (f, old_fg);
534 static void
535 x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
537 struct x_output *x = f->output_data.x;
538 unsigned long bg;
540 bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
541 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
542 FRAME_BACKGROUND_PIXEL (f) = bg;
544 if (FRAME_X_WINDOW (f) != 0)
546 Display *dpy = FRAME_X_DISPLAY (f);
548 block_input ();
549 XSetBackground (dpy, x->normal_gc, bg);
550 XSetForeground (dpy, x->reverse_gc, bg);
551 XSetWindowBackground (dpy, FRAME_X_WINDOW (f), bg);
552 XSetForeground (dpy, x->cursor_gc, bg);
554 #ifdef USE_GTK
555 xg_set_background_color (f, bg);
556 #endif
558 #ifndef USE_TOOLKIT_SCROLL_BARS /* Turns out to be annoying with
559 toolkit scroll bars. */
561 Lisp_Object bar;
562 for (bar = FRAME_SCROLL_BARS (f);
563 !NILP (bar);
564 bar = XSCROLL_BAR (bar)->next)
566 Window window = XSCROLL_BAR (bar)->x_window;
567 XSetWindowBackground (dpy, window, bg);
570 #endif /* USE_TOOLKIT_SCROLL_BARS */
572 unblock_input ();
573 update_face_from_frame_parameter (f, Qbackground_color, arg);
575 if (FRAME_VISIBLE_P (f))
576 redraw_frame (f);
580 static Cursor
581 make_invisible_cursor (struct frame *f)
583 Display *dpy = FRAME_X_DISPLAY (f);
584 static char const no_data[] = { 0 };
585 Pixmap pix;
586 XColor col;
587 Cursor c = 0;
589 x_catch_errors (dpy);
590 pix = XCreateBitmapFromData (dpy, FRAME_DISPLAY_INFO (f)->root_window,
591 no_data, 1, 1);
592 if (! x_had_errors_p (dpy) && pix != None)
594 Cursor pixc;
595 col.pixel = 0;
596 col.red = col.green = col.blue = 0;
597 col.flags = DoRed | DoGreen | DoBlue;
598 pixc = XCreatePixmapCursor (dpy, pix, pix, &col, &col, 0, 0);
599 if (! x_had_errors_p (dpy) && pixc != None)
600 c = pixc;
601 XFreePixmap (dpy, pix);
604 x_uncatch_errors ();
606 return c;
609 static void
610 x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
612 struct x_output *x = f->output_data.x;
613 Display *dpy = FRAME_X_DISPLAY (f);
614 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
615 Cursor hourglass_cursor, horizontal_drag_cursor, vertical_drag_cursor;
616 unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
617 unsigned long mask_color = FRAME_BACKGROUND_PIXEL (f);
619 /* Don't let pointers be invisible. */
620 if (mask_color == pixel)
622 x_free_colors (f, &pixel, 1);
623 pixel = x_copy_color (f, FRAME_FOREGROUND_PIXEL (f));
626 unload_color (f, x->mouse_pixel);
627 x->mouse_pixel = pixel;
629 block_input ();
631 /* It's not okay to crash if the user selects a screwy cursor. */
632 x_catch_errors (dpy);
634 if (!NILP (Vx_pointer_shape))
636 CHECK_NUMBER (Vx_pointer_shape);
637 cursor = XCreateFontCursor (dpy, XINT (Vx_pointer_shape));
639 else
640 cursor = XCreateFontCursor (dpy, XC_xterm);
641 x_check_errors (dpy, "bad text pointer cursor: %s");
643 if (!NILP (Vx_nontext_pointer_shape))
645 CHECK_NUMBER (Vx_nontext_pointer_shape);
646 nontext_cursor
647 = XCreateFontCursor (dpy, XINT (Vx_nontext_pointer_shape));
649 else
650 nontext_cursor = XCreateFontCursor (dpy, XC_left_ptr);
651 x_check_errors (dpy, "bad nontext pointer cursor: %s");
653 if (!NILP (Vx_hourglass_pointer_shape))
655 CHECK_NUMBER (Vx_hourglass_pointer_shape);
656 hourglass_cursor
657 = XCreateFontCursor (dpy, XINT (Vx_hourglass_pointer_shape));
659 else
660 hourglass_cursor = XCreateFontCursor (dpy, XC_watch);
661 x_check_errors (dpy, "bad hourglass pointer cursor: %s");
663 if (!NILP (Vx_mode_pointer_shape))
665 CHECK_NUMBER (Vx_mode_pointer_shape);
666 mode_cursor = XCreateFontCursor (dpy, XINT (Vx_mode_pointer_shape));
668 else
669 mode_cursor = XCreateFontCursor (dpy, XC_xterm);
670 x_check_errors (dpy, "bad modeline pointer cursor: %s");
672 if (!NILP (Vx_sensitive_text_pointer_shape))
674 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
675 hand_cursor
676 = XCreateFontCursor (dpy, XINT (Vx_sensitive_text_pointer_shape));
678 else
679 hand_cursor = XCreateFontCursor (dpy, XC_hand2);
681 if (!NILP (Vx_window_horizontal_drag_shape))
683 CHECK_TYPE_RANGED_INTEGER (unsigned, Vx_window_horizontal_drag_shape);
684 horizontal_drag_cursor
685 = XCreateFontCursor (dpy, XINT (Vx_window_horizontal_drag_shape));
687 else
688 horizontal_drag_cursor
689 = XCreateFontCursor (dpy, XC_sb_h_double_arrow);
691 if (!NILP (Vx_window_vertical_drag_shape))
693 CHECK_NUMBER (Vx_window_vertical_drag_shape);
694 vertical_drag_cursor
695 = XCreateFontCursor (dpy, XINT (Vx_window_vertical_drag_shape));
697 else
698 vertical_drag_cursor
699 = XCreateFontCursor (dpy, XC_sb_v_double_arrow);
701 /* Check and report errors with the above calls. */
702 x_check_errors (dpy, "can't set cursor shape: %s");
703 x_uncatch_errors ();
706 XColor fore_color, back_color;
708 fore_color.pixel = x->mouse_pixel;
709 x_query_color (f, &fore_color);
710 back_color.pixel = mask_color;
711 x_query_color (f, &back_color);
713 XRecolorCursor (dpy, cursor, &fore_color, &back_color);
714 XRecolorCursor (dpy, nontext_cursor, &fore_color, &back_color);
715 XRecolorCursor (dpy, mode_cursor, &fore_color, &back_color);
716 XRecolorCursor (dpy, hand_cursor, &fore_color, &back_color);
717 XRecolorCursor (dpy, hourglass_cursor, &fore_color, &back_color);
718 XRecolorCursor (dpy, horizontal_drag_cursor, &fore_color, &back_color);
721 if (FRAME_X_WINDOW (f) != 0)
722 XDefineCursor (dpy, FRAME_X_WINDOW (f),
723 f->output_data.x->current_cursor = cursor);
725 if (FRAME_DISPLAY_INFO (f)->invisible_cursor == 0)
726 FRAME_DISPLAY_INFO (f)->invisible_cursor = make_invisible_cursor (f);
728 if (cursor != x->text_cursor
729 && x->text_cursor != 0)
730 XFreeCursor (dpy, x->text_cursor);
731 x->text_cursor = cursor;
733 if (nontext_cursor != x->nontext_cursor
734 && x->nontext_cursor != 0)
735 XFreeCursor (dpy, x->nontext_cursor);
736 x->nontext_cursor = nontext_cursor;
738 if (hourglass_cursor != x->hourglass_cursor
739 && x->hourglass_cursor != 0)
740 XFreeCursor (dpy, x->hourglass_cursor);
741 x->hourglass_cursor = hourglass_cursor;
743 if (mode_cursor != x->modeline_cursor
744 && x->modeline_cursor != 0)
745 XFreeCursor (dpy, f->output_data.x->modeline_cursor);
746 x->modeline_cursor = mode_cursor;
748 if (hand_cursor != x->hand_cursor
749 && x->hand_cursor != 0)
750 XFreeCursor (dpy, x->hand_cursor);
751 x->hand_cursor = hand_cursor;
753 if (horizontal_drag_cursor != x->horizontal_drag_cursor
754 && x->horizontal_drag_cursor != 0)
755 XFreeCursor (dpy, x->horizontal_drag_cursor);
756 x->horizontal_drag_cursor = horizontal_drag_cursor;
758 if (vertical_drag_cursor != x->vertical_drag_cursor
759 && x->vertical_drag_cursor != 0)
760 XFreeCursor (dpy, x->vertical_drag_cursor);
761 x->vertical_drag_cursor = vertical_drag_cursor;
763 XFlush (dpy);
764 unblock_input ();
766 update_face_from_frame_parameter (f, Qmouse_color, arg);
769 static void
770 x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
772 unsigned long fore_pixel, pixel;
773 bool fore_pixel_allocated_p = 0, pixel_allocated_p = 0;
774 struct x_output *x = f->output_data.x;
776 if (!NILP (Vx_cursor_fore_pixel))
778 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
779 WHITE_PIX_DEFAULT (f));
780 fore_pixel_allocated_p = 1;
782 else
783 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
785 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
786 pixel_allocated_p = 1;
788 /* Make sure that the cursor color differs from the background color. */
789 if (pixel == FRAME_BACKGROUND_PIXEL (f))
791 if (pixel_allocated_p)
793 x_free_colors (f, &pixel, 1);
794 pixel_allocated_p = 0;
797 pixel = x->mouse_pixel;
798 if (pixel == fore_pixel)
800 if (fore_pixel_allocated_p)
802 x_free_colors (f, &fore_pixel, 1);
803 fore_pixel_allocated_p = 0;
805 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
809 unload_color (f, x->cursor_foreground_pixel);
810 if (!fore_pixel_allocated_p)
811 fore_pixel = x_copy_color (f, fore_pixel);
812 x->cursor_foreground_pixel = fore_pixel;
814 unload_color (f, x->cursor_pixel);
815 if (!pixel_allocated_p)
816 pixel = x_copy_color (f, pixel);
817 x->cursor_pixel = pixel;
819 if (FRAME_X_WINDOW (f) != 0)
821 block_input ();
822 XSetBackground (FRAME_X_DISPLAY (f), x->cursor_gc, x->cursor_pixel);
823 XSetForeground (FRAME_X_DISPLAY (f), x->cursor_gc, fore_pixel);
824 unblock_input ();
826 if (FRAME_VISIBLE_P (f))
828 x_update_cursor (f, 0);
829 x_update_cursor (f, 1);
833 update_face_from_frame_parameter (f, Qcursor_color, arg);
836 /* Set the border-color of frame F to pixel value PIX.
837 Note that this does not fully take effect if done before
838 F has an x-window. */
840 static void
841 x_set_border_pixel (struct frame *f, int pix)
843 unload_color (f, f->output_data.x->border_pixel);
844 f->output_data.x->border_pixel = pix;
846 if (FRAME_X_WINDOW (f) != 0 && f->border_width > 0)
848 block_input ();
849 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), pix);
850 unblock_input ();
852 if (FRAME_VISIBLE_P (f))
853 redraw_frame (f);
857 /* Set the border-color of frame F to value described by ARG.
858 ARG can be a string naming a color.
859 The border-color is used for the border that is drawn by the X server.
860 Note that this does not fully take effect if done before
861 F has an x-window; it must be redone when the window is created.
863 Note: this is done in two routines because of the way X10 works.
865 Note: under X11, this is normally the province of the window manager,
866 and so emacs's border colors may be overridden. */
868 static void
869 x_set_border_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
871 int pix;
873 CHECK_STRING (arg);
874 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
875 x_set_border_pixel (f, pix);
876 update_face_from_frame_parameter (f, Qborder_color, arg);
880 static void
881 x_set_cursor_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
883 set_frame_cursor_types (f, arg);
886 static void
887 x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
889 int result;
891 if (STRINGP (arg))
893 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
894 return;
896 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
897 return;
899 block_input ();
900 if (NILP (arg))
901 result = x_text_icon (f,
902 SSDATA ((!NILP (f->icon_name)
903 ? f->icon_name
904 : f->name)));
905 else
906 result = x_bitmap_icon (f, arg);
908 if (result)
910 unblock_input ();
911 error ("No icon window available");
914 XFlush (FRAME_X_DISPLAY (f));
915 unblock_input ();
918 static void
919 x_set_icon_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
921 int result;
923 if (STRINGP (arg))
925 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
926 return;
928 else if (!NILP (arg) || NILP (oldval))
929 return;
931 fset_icon_name (f, arg);
933 if (f->output_data.x->icon_bitmap != 0)
934 return;
936 block_input ();
938 result = x_text_icon (f,
939 SSDATA ((!NILP (f->icon_name)
940 ? f->icon_name
941 : !NILP (f->title)
942 ? f->title
943 : f->name)));
945 if (result)
947 unblock_input ();
948 error ("No icon window available");
951 XFlush (FRAME_X_DISPLAY (f));
952 unblock_input ();
956 void
957 x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
959 int nlines;
960 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
961 int olines = FRAME_MENU_BAR_LINES (f);
962 #endif
964 /* Right now, menu bars don't work properly in minibuf-only frames;
965 most of the commands try to apply themselves to the minibuffer
966 frame itself, and get an error because you can't switch buffers
967 in or split the minibuffer window. */
968 if (FRAME_MINIBUF_ONLY_P (f))
969 return;
971 if (TYPE_RANGED_INTEGERP (int, value))
972 nlines = XINT (value);
973 else
974 nlines = 0;
976 /* Make sure we redisplay all windows in this frame. */
977 windows_or_buffers_changed = 59;
979 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
980 FRAME_MENU_BAR_LINES (f) = 0;
981 FRAME_MENU_BAR_HEIGHT (f) = 0;
982 if (nlines)
984 FRAME_EXTERNAL_MENU_BAR (f) = 1;
985 if (FRAME_X_P (f) && f->output_data.x->menubar_widget == 0)
986 /* Make sure next redisplay shows the menu bar. */
987 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = 1;
989 else
991 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
992 free_frame_menubar (f);
993 FRAME_EXTERNAL_MENU_BAR (f) = 0;
994 if (FRAME_X_P (f))
995 f->output_data.x->menubar_widget = 0;
997 #else /* not USE_X_TOOLKIT && not USE_GTK */
998 FRAME_MENU_BAR_LINES (f) = nlines;
999 FRAME_MENU_BAR_HEIGHT (f) = nlines * FRAME_LINE_HEIGHT (f);
1000 resize_frame_windows (f, FRAME_TEXT_HEIGHT (f), 0, 1);
1002 /* If the menu bar height gets changed, the internal border below
1003 the top margin has to be cleared. Also, if the menu bar gets
1004 larger, the area for the added lines has to be cleared except for
1005 the first menu bar line that is to be drawn later. */
1006 if (nlines != olines)
1008 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1009 int width = FRAME_PIXEL_WIDTH (f);
1010 int y;
1012 /* height can be zero here. */
1013 if (FRAME_X_WINDOW (f) && height > 0 && width > 0)
1015 y = FRAME_TOP_MARGIN_HEIGHT (f);
1017 block_input ();
1018 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1019 0, y, width, height);
1020 unblock_input ();
1023 if (nlines > 1 && nlines > olines)
1025 y = (olines == 0 ? 1 : olines) * FRAME_LINE_HEIGHT (f);
1026 height = nlines * FRAME_LINE_HEIGHT (f) - y;
1028 block_input ();
1029 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1030 0, y, width, height);
1031 unblock_input ();
1034 if (nlines == 0 && WINDOWP (f->menu_bar_window))
1035 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
1037 #endif /* not USE_X_TOOLKIT && not USE_GTK */
1038 adjust_frame_glyphs (f);
1039 run_window_configuration_change_hook (f);
1043 /* Set the number of lines used for the tool bar of frame F to VALUE.
1044 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1045 is the old number of tool bar lines. This function changes the
1046 height of all windows on frame F to match the new tool bar height.
1047 The frame's height doesn't change. */
1049 void
1050 x_set_tool_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1052 int nlines;
1053 #if ! defined (USE_GTK)
1054 int delta, root_height;
1055 int unit = FRAME_LINE_HEIGHT (f);
1056 #endif
1058 /* Treat tool bars like menu bars. */
1059 if (FRAME_MINIBUF_ONLY_P (f))
1060 return;
1062 /* Use VALUE only if an int >= 0. */
1063 if (RANGED_INTEGERP (0, value, INT_MAX))
1064 nlines = XFASTINT (value);
1065 else
1066 nlines = 0;
1068 #ifdef USE_GTK
1070 FRAME_TOOL_BAR_LINES (f) = 0;
1071 FRAME_TOOL_BAR_HEIGHT (f) = 0;
1072 if (nlines)
1074 FRAME_EXTERNAL_TOOL_BAR (f) = 1;
1075 if (FRAME_X_P (f) && f->output_data.x->toolbar_widget == 0)
1076 /* Make sure next redisplay shows the tool bar. */
1077 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = 1;
1078 update_frame_tool_bar (f);
1080 else
1082 if (FRAME_EXTERNAL_TOOL_BAR (f))
1083 free_frame_tool_bar (f);
1084 FRAME_EXTERNAL_TOOL_BAR (f) = 0;
1087 #else /* !USE_GTK */
1089 /* Make sure we redisplay all windows in this frame. */
1090 windows_or_buffers_changed = 60;
1092 /* DELTA is in pixels now. */
1093 delta = (nlines - FRAME_TOOL_BAR_LINES (f)) * unit;
1095 /* Don't resize the tool-bar to more than we have room for. Note: The
1096 calculations below and the subsequent call to resize_frame_windows
1097 are inherently flawed because they can make the toolbar higher than
1098 the containing frame. */
1099 if (delta > 0)
1101 root_height = WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)));
1102 if (root_height - delta < unit)
1104 delta = root_height - unit;
1105 /* When creating a new frame and toolbar mode is enabled, we
1106 need at least one toolbar line. */
1107 nlines = max (FRAME_TOOL_BAR_LINES (f) + delta / unit, 1);
1111 FRAME_TOOL_BAR_LINES (f) = nlines;
1112 FRAME_TOOL_BAR_HEIGHT (f) = nlines * FRAME_LINE_HEIGHT (f);
1113 ++windows_or_buffers_changed;
1114 resize_frame_windows (f, FRAME_TEXT_HEIGHT (f), 0, 1);
1115 adjust_frame_glyphs (f);
1117 /* We also have to make sure that the internal border at the top of
1118 the frame, below the menu bar or tool bar, is redrawn when the
1119 tool bar disappears. This is so because the internal border is
1120 below the tool bar if one is displayed, but is below the menu bar
1121 if there isn't a tool bar. The tool bar draws into the area
1122 below the menu bar. */
1123 if (FRAME_X_WINDOW (f) && FRAME_TOOL_BAR_HEIGHT (f) == 0)
1125 clear_frame (f);
1126 clear_current_matrices (f);
1129 /* If the tool bar gets smaller, the internal border below it
1130 has to be cleared. It was formerly part of the display
1131 of the larger tool bar, and updating windows won't clear it. */
1132 if (delta < 0)
1134 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1135 int width = FRAME_PIXEL_WIDTH (f);
1136 int y = nlines * unit;
1138 /* height can be zero here. */
1139 if (height > 0 && width > 0)
1141 block_input ();
1142 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1143 0, y, width, height);
1144 unblock_input ();
1147 if (WINDOWP (f->tool_bar_window))
1148 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1151 run_window_configuration_change_hook (f);
1152 #endif /* USE_GTK */
1156 /* Set the foreground color for scroll bars on frame F to VALUE.
1157 VALUE should be a string, a color name. If it isn't a string or
1158 isn't a valid color name, do nothing. OLDVAL is the old value of
1159 the frame parameter. */
1161 static void
1162 x_set_scroll_bar_foreground (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1164 unsigned long pixel;
1166 if (STRINGP (value))
1167 pixel = x_decode_color (f, value, BLACK_PIX_DEFAULT (f));
1168 else
1169 pixel = -1;
1171 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
1172 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
1174 f->output_data.x->scroll_bar_foreground_pixel = pixel;
1175 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1177 /* Remove all scroll bars because they have wrong colors. */
1178 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1179 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1180 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1181 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1183 update_face_from_frame_parameter (f, Qscroll_bar_foreground, value);
1184 redraw_frame (f);
1189 /* Set the background color for scroll bars on frame F to VALUE VALUE
1190 should be a string, a color name. If it isn't a string or isn't a
1191 valid color name, do nothing. OLDVAL is the old value of the frame
1192 parameter. */
1194 static void
1195 x_set_scroll_bar_background (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1197 unsigned long pixel;
1199 if (STRINGP (value))
1200 pixel = x_decode_color (f, value, WHITE_PIX_DEFAULT (f));
1201 else
1202 pixel = -1;
1204 if (f->output_data.x->scroll_bar_background_pixel != -1)
1205 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
1207 #ifdef USE_TOOLKIT_SCROLL_BARS
1208 /* Scrollbar shadow colors. */
1209 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
1211 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
1212 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
1214 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
1216 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
1217 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
1219 #endif /* USE_TOOLKIT_SCROLL_BARS */
1221 f->output_data.x->scroll_bar_background_pixel = pixel;
1222 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1224 /* Remove all scroll bars because they have wrong colors. */
1225 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1226 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1227 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1228 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1230 update_face_from_frame_parameter (f, Qscroll_bar_background, value);
1231 redraw_frame (f);
1236 /* Encode Lisp string STRING as a text in a format appropriate for
1237 XICCC (X Inter Client Communication Conventions).
1239 This can call Lisp code, so callers must GCPRO.
1241 If STRING contains only ASCII characters, do no conversion and
1242 return the string data of STRING. Otherwise, encode the text by
1243 CODING_SYSTEM, and return a newly allocated memory area which
1244 should be freed by `xfree' by a caller.
1246 SELECTIONP non-zero means the string is being encoded for an X
1247 selection, so it is safe to run pre-write conversions (which
1248 may run Lisp code).
1250 Store the byte length of resulting text in *TEXT_BYTES.
1252 If the text contains only ASCII and Latin-1, store 1 in *STRING_P,
1253 which means that the `encoding' of the result can be `STRING'.
1254 Otherwise store 0 in *STRINGP, which means that the `encoding' of
1255 the result should be `COMPOUND_TEXT'. */
1257 static unsigned char *
1258 x_encode_text (Lisp_Object string, Lisp_Object coding_system, int selectionp,
1259 ptrdiff_t *text_bytes, int *stringp, int *freep)
1261 int result = string_xstring_p (string);
1262 struct coding_system coding;
1264 if (result == 0)
1266 /* No multibyte character in OBJ. We need not encode it. */
1267 *text_bytes = SBYTES (string);
1268 *stringp = 1;
1269 *freep = 0;
1270 return SDATA (string);
1273 setup_coding_system (coding_system, &coding);
1274 coding.mode |= (CODING_MODE_SAFE_ENCODING | CODING_MODE_LAST_BLOCK);
1275 /* We suppress producing escape sequences for composition. */
1276 coding.common_flags &= ~CODING_ANNOTATION_MASK;
1277 coding.destination = xnmalloc (SCHARS (string), 2);
1278 coding.dst_bytes = SCHARS (string) * 2;
1279 encode_coding_object (&coding, string, 0, 0,
1280 SCHARS (string), SBYTES (string), Qnil);
1281 *text_bytes = coding.produced;
1282 *stringp = (result == 1 || !EQ (coding_system, Qcompound_text));
1283 *freep = 1;
1284 return coding.destination;
1288 /* Set the WM name to NAME for frame F. Also set the icon name.
1289 If the frame already has an icon name, use that, otherwise set the
1290 icon name to NAME. */
1292 static void
1293 x_set_name_internal (struct frame *f, Lisp_Object name)
1295 if (FRAME_X_WINDOW (f))
1297 block_input ();
1299 XTextProperty text, icon;
1300 ptrdiff_t bytes;
1301 int stringp;
1302 int do_free_icon_value = 0, do_free_text_value = 0;
1303 Lisp_Object coding_system;
1304 Lisp_Object encoded_name;
1305 Lisp_Object encoded_icon_name;
1306 struct gcpro gcpro1;
1308 /* As ENCODE_UTF_8 may cause GC and relocation of string data,
1309 we use it before x_encode_text that may return string data. */
1310 GCPRO1 (name);
1311 encoded_name = ENCODE_UTF_8 (name);
1312 UNGCPRO;
1314 coding_system = Qcompound_text;
1315 /* Note: Encoding strategy
1317 We encode NAME by compound-text and use "COMPOUND-TEXT" in
1318 text.encoding. But, there are non-internationalized window
1319 managers which don't support that encoding. So, if NAME
1320 contains only ASCII and 8859-1 characters, encode it by
1321 iso-latin-1, and use "STRING" in text.encoding hoping that
1322 such window managers at least analyze this format correctly,
1323 i.e. treat 8-bit bytes as 8859-1 characters.
1325 We may also be able to use "UTF8_STRING" in text.encoding
1326 in the future which can encode all Unicode characters.
1327 But, for the moment, there's no way to know that the
1328 current window manager supports it or not.
1330 Either way, we also set the _NET_WM_NAME and _NET_WM_ICON_NAME
1331 properties. Per the EWMH specification, those two properties
1332 are always UTF8_STRING. This matches what gtk_window_set_title()
1333 does in the USE_GTK case. */
1334 text.value = x_encode_text (name, coding_system, 0, &bytes, &stringp,
1335 &do_free_text_value);
1336 text.encoding = (stringp ? XA_STRING
1337 : FRAME_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1338 text.format = 8;
1339 text.nitems = bytes;
1340 if (text.nitems != bytes)
1341 error ("Window name too large");
1343 if (!STRINGP (f->icon_name))
1345 icon = text;
1346 encoded_icon_name = encoded_name;
1348 else
1350 /* See the above comment "Note: Encoding strategy". */
1351 icon.value = x_encode_text (f->icon_name, coding_system, 0,
1352 &bytes, &stringp, &do_free_icon_value);
1353 icon.encoding = (stringp ? XA_STRING
1354 : FRAME_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1355 icon.format = 8;
1356 icon.nitems = bytes;
1357 if (icon.nitems != bytes)
1358 error ("Icon name too large");
1360 encoded_icon_name = ENCODE_UTF_8 (f->icon_name);
1363 #ifdef USE_GTK
1364 gtk_window_set_title (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1365 SSDATA (encoded_name));
1366 #else /* not USE_GTK */
1367 XSetWMName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
1368 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1369 FRAME_DISPLAY_INFO (f)->Xatom_net_wm_name,
1370 FRAME_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1371 8, PropModeReplace,
1372 SDATA (encoded_name),
1373 SBYTES (encoded_name));
1374 #endif /* not USE_GTK */
1376 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &icon);
1377 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1378 FRAME_DISPLAY_INFO (f)->Xatom_net_wm_icon_name,
1379 FRAME_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1380 8, PropModeReplace,
1381 SDATA (encoded_icon_name),
1382 SBYTES (encoded_icon_name));
1384 if (do_free_icon_value)
1385 xfree (icon.value);
1386 if (do_free_text_value)
1387 xfree (text.value);
1389 unblock_input ();
1393 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1394 x_id_name.
1396 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1397 name; if NAME is a string, set F's name to NAME and set
1398 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1400 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1401 suggesting a new name, which lisp code should override; if
1402 F->explicit_name is set, ignore the new name; otherwise, set it. */
1404 static void
1405 x_set_name (struct frame *f, Lisp_Object name, int explicit)
1407 /* Make sure that requests from lisp code override requests from
1408 Emacs redisplay code. */
1409 if (explicit)
1411 /* If we're switching from explicit to implicit, we had better
1412 update the mode lines and thereby update the title. */
1413 if (f->explicit_name && NILP (name))
1414 update_mode_lines = 37;
1416 f->explicit_name = ! NILP (name);
1418 else if (f->explicit_name)
1419 return;
1421 /* If NAME is nil, set the name to the x_id_name. */
1422 if (NILP (name))
1424 /* Check for no change needed in this very common case
1425 before we do any consing. */
1426 if (!strcmp (FRAME_DISPLAY_INFO (f)->x_id_name,
1427 SSDATA (f->name)))
1428 return;
1429 name = build_string (FRAME_DISPLAY_INFO (f)->x_id_name);
1431 else
1432 CHECK_STRING (name);
1434 /* Don't change the name if it's already NAME. */
1435 if (! NILP (Fstring_equal (name, f->name)))
1436 return;
1438 fset_name (f, name);
1440 /* For setting the frame title, the title parameter should override
1441 the name parameter. */
1442 if (! NILP (f->title))
1443 name = f->title;
1445 x_set_name_internal (f, name);
1448 /* This function should be called when the user's lisp code has
1449 specified a name for the frame; the name will override any set by the
1450 redisplay code. */
1451 static void
1452 x_explicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1454 x_set_name (f, arg, 1);
1457 /* This function should be called by Emacs redisplay code to set the
1458 name; names set this way will never override names set by the user's
1459 lisp code. */
1460 void
1461 x_implicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1463 x_set_name (f, arg, 0);
1466 /* Change the title of frame F to NAME.
1467 If NAME is nil, use the frame name as the title. */
1469 static void
1470 x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
1472 /* Don't change the title if it's already NAME. */
1473 if (EQ (name, f->title))
1474 return;
1476 update_mode_lines = 38;
1478 fset_title (f, name);
1480 if (NILP (name))
1481 name = f->name;
1482 else
1483 CHECK_STRING (name);
1485 x_set_name_internal (f, name);
1488 void
1489 x_set_scroll_bar_default_width (struct frame *f)
1491 int wid = FRAME_COLUMN_WIDTH (f);
1492 #ifdef USE_TOOLKIT_SCROLL_BARS
1493 #ifdef USE_GTK
1494 int minw = xg_get_default_scrollbar_width ();
1495 #else
1496 int minw = 16;
1497 #endif
1498 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
1499 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (minw + wid - 1) / wid;
1500 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = minw;
1501 #else
1502 /* Make the actual width 16 pixels and a multiple of a
1503 character width. */
1504 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (16 + wid - 1) / wid;
1506 /* Use all of that space (aside from required margins) for the
1507 scroll bar. */
1508 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = 16;
1509 #endif
1513 /* Record in frame F the specified or default value according to ALIST
1514 of the parameter named PROP (a Lisp symbol). If no value is
1515 specified for PROP, look for an X default for XPROP on the frame
1516 named NAME. If that is not found either, use the value DEFLT. */
1518 static Lisp_Object
1519 x_default_scroll_bar_color_parameter (struct frame *f,
1520 Lisp_Object alist, Lisp_Object prop,
1521 const char *xprop, const char *xclass,
1522 int foreground_p)
1524 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1525 Lisp_Object tem;
1527 tem = x_get_arg (dpyinfo, alist, prop, xprop, xclass, RES_TYPE_STRING);
1528 if (EQ (tem, Qunbound))
1530 #ifdef USE_TOOLKIT_SCROLL_BARS
1532 /* See if an X resource for the scroll bar color has been
1533 specified. */
1534 tem = display_x_get_resource (dpyinfo,
1535 build_string (foreground_p
1536 ? "foreground"
1537 : "background"),
1538 empty_unibyte_string,
1539 build_string ("verticalScrollBar"),
1540 empty_unibyte_string);
1541 if (!STRINGP (tem))
1543 /* If nothing has been specified, scroll bars will use a
1544 toolkit-dependent default. Because these defaults are
1545 difficult to get at without actually creating a scroll
1546 bar, use nil to indicate that no color has been
1547 specified. */
1548 tem = Qnil;
1551 #else /* not USE_TOOLKIT_SCROLL_BARS */
1553 tem = Qnil;
1555 #endif /* not USE_TOOLKIT_SCROLL_BARS */
1558 x_set_frame_parameters (f, list1 (Fcons (prop, tem)));
1559 return tem;
1565 #ifdef USE_X_TOOLKIT
1567 /* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
1568 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
1569 already be present because of the toolkit (Motif adds some of them,
1570 for example, but Xt doesn't). */
1572 static void
1573 hack_wm_protocols (struct frame *f, Widget widget)
1575 Display *dpy = XtDisplay (widget);
1576 Window w = XtWindow (widget);
1577 int need_delete = 1;
1578 int need_focus = 1;
1579 int need_save = 1;
1581 block_input ();
1583 Atom type;
1584 unsigned char *catoms;
1585 int format = 0;
1586 unsigned long nitems = 0;
1587 unsigned long bytes_after;
1589 if ((XGetWindowProperty (dpy, w,
1590 FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
1591 (long)0, (long)100, False, XA_ATOM,
1592 &type, &format, &nitems, &bytes_after,
1593 &catoms)
1594 == Success)
1595 && format == 32 && type == XA_ATOM)
1597 Atom *atoms = (Atom *) catoms;
1598 while (nitems > 0)
1600 nitems--;
1601 if (atoms[nitems]
1602 == FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window)
1603 need_delete = 0;
1604 else if (atoms[nitems]
1605 == FRAME_DISPLAY_INFO (f)->Xatom_wm_take_focus)
1606 need_focus = 0;
1607 else if (atoms[nitems]
1608 == FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself)
1609 need_save = 0;
1612 if (catoms)
1613 XFree (catoms);
1616 Atom props [10];
1617 int count = 0;
1618 if (need_delete)
1619 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window;
1620 if (need_focus)
1621 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_take_focus;
1622 if (need_save)
1623 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
1624 if (count)
1625 XChangeProperty (dpy, w, FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
1626 XA_ATOM, 32, PropModeAppend,
1627 (unsigned char *) props, count);
1629 unblock_input ();
1631 #endif
1635 /* Support routines for XIC (X Input Context). */
1637 #ifdef HAVE_X_I18N
1639 static XFontSet xic_create_xfontset (struct frame *);
1640 static XIMStyle best_xim_style (XIMStyles *, XIMStyles *);
1643 /* Supported XIM styles, ordered by preference. */
1645 static XIMStyle supported_xim_styles[] =
1647 XIMPreeditPosition | XIMStatusArea,
1648 XIMPreeditPosition | XIMStatusNothing,
1649 XIMPreeditPosition | XIMStatusNone,
1650 XIMPreeditNothing | XIMStatusArea,
1651 XIMPreeditNothing | XIMStatusNothing,
1652 XIMPreeditNothing | XIMStatusNone,
1653 XIMPreeditNone | XIMStatusArea,
1654 XIMPreeditNone | XIMStatusNothing,
1655 XIMPreeditNone | XIMStatusNone,
1660 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
1661 /* Create an X fontset on frame F with base font name BASE_FONTNAME. */
1663 static const char xic_default_fontset[] = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
1665 /* Create an Xt fontset spec from the name of a base font.
1666 If `motif' is True use the Motif syntax. */
1667 char *
1668 xic_create_fontsetname (const char *base_fontname, int motif)
1670 const char *sep = motif ? ";" : ",";
1671 char *fontsetname;
1673 /* Make a fontset name from the base font name. */
1674 if (xic_default_fontset == base_fontname)
1676 /* There is no base font name, use the default. */
1677 fontsetname = xmalloc (strlen (base_fontname) + 2);
1678 strcpy (fontsetname, base_fontname);
1680 else
1682 /* Make a fontset name from the base font name.
1683 The font set will be made of the following elements:
1684 - the base font.
1685 - the base font where the charset spec is replaced by -*-*.
1686 - the same but with the family also replaced with -*-*-. */
1687 const char *p = base_fontname;
1688 ptrdiff_t i;
1690 for (i = 0; *p; p++)
1691 if (*p == '-') i++;
1692 if (i != 14)
1694 /* As the font name doesn't conform to XLFD, we can't
1695 modify it to generalize it to allcs and allfamilies.
1696 Use the specified font plus the default. */
1697 fontsetname = xmalloc (strlen (base_fontname)
1698 + strlen (xic_default_fontset) + 3);
1699 strcpy (fontsetname, base_fontname);
1700 strcat (fontsetname, sep);
1701 strcat (fontsetname, xic_default_fontset);
1703 else
1705 ptrdiff_t len;
1706 const char *p1 = NULL, *p2 = NULL, *p3 = NULL;
1707 char *font_allcs = NULL;
1708 char *font_allfamilies = NULL;
1709 char *font_all = NULL;
1710 const char *allcs = "*-*-*-*-*-*-*";
1711 const char *allfamilies = "-*-*-";
1712 const char *all = "*-*-*-*-";
1713 char *base;
1715 for (i = 0, p = base_fontname; i < 8; p++)
1717 if (*p == '-')
1719 i++;
1720 if (i == 3)
1721 p1 = p + 1;
1722 else if (i == 7)
1723 p2 = p + 1;
1724 else if (i == 6)
1725 p3 = p + 1;
1728 /* If base_fontname specifies ADSTYLE, make it a
1729 wildcard. */
1730 if (*p3 != '*')
1732 ptrdiff_t diff = (p2 - p3) - 2;
1734 base = alloca (strlen (base_fontname) + 1);
1735 memcpy (base, base_fontname, p3 - base_fontname);
1736 base[p3 - base_fontname] = '*';
1737 base[(p3 - base_fontname) + 1] = '-';
1738 strcpy (base + (p3 - base_fontname) + 2, p2);
1739 p = base + (p - base_fontname) - diff;
1740 p1 = base + (p1 - base_fontname);
1741 p2 = base + (p2 - base_fontname) - diff;
1742 base_fontname = base;
1745 /* Build the font spec that matches all charsets. */
1746 len = p - base_fontname + strlen (allcs) + 1;
1747 font_allcs = alloca (len);
1748 memcpy (font_allcs, base_fontname, p - base_fontname);
1749 strcat (font_allcs, allcs);
1751 /* Build the font spec that matches all families and
1752 add-styles. */
1753 len = p - p1 + strlen (allcs) + strlen (allfamilies) + 1;
1754 font_allfamilies = alloca (len);
1755 strcpy (font_allfamilies, allfamilies);
1756 memcpy (font_allfamilies + strlen (allfamilies), p1, p - p1);
1757 strcat (font_allfamilies, allcs);
1759 /* Build the font spec that matches all. */
1760 len = p - p2 + strlen (allcs) + strlen (all) + strlen (allfamilies) + 1;
1761 font_all = alloca (len);
1762 strcpy (font_all, allfamilies);
1763 strcat (font_all, all);
1764 memcpy (font_all + strlen (all) + strlen (allfamilies), p2, p - p2);
1765 strcat (font_all, allcs);
1767 /* Build the actual font set name. */
1768 len = strlen (base_fontname) + strlen (font_allcs)
1769 + strlen (font_allfamilies) + strlen (font_all) + 5;
1770 fontsetname = xmalloc (len);
1771 strcpy (fontsetname, base_fontname);
1772 strcat (fontsetname, sep);
1773 strcat (fontsetname, font_allcs);
1774 strcat (fontsetname, sep);
1775 strcat (fontsetname, font_allfamilies);
1776 strcat (fontsetname, sep);
1777 strcat (fontsetname, font_all);
1780 if (motif)
1781 return strcat (fontsetname, ":");
1782 return fontsetname;
1784 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
1786 #ifdef DEBUG_XIC_FONTSET
1787 static void
1788 print_fontset_result (XFontSet xfs, char *name, char **missing_list,
1789 int missing_count)
1791 if (xfs)
1792 fprintf (stderr, "XIC Fontset created: %s\n", name);
1793 else
1795 fprintf (stderr, "XIC Fontset failed: %s\n", name);
1796 while (missing_count-- > 0)
1798 fprintf (stderr, " missing: %s\n", *missing_list);
1799 missing_list++;
1804 #endif
1806 static XFontSet
1807 xic_create_xfontset (struct frame *f)
1809 XFontSet xfs = NULL;
1810 struct font *font = FRAME_FONT (f);
1811 int pixel_size = font->pixel_size;
1812 Lisp_Object rest, frame;
1814 /* See if there is another frame already using same fontset. */
1815 FOR_EACH_FRAME (rest, frame)
1817 struct frame *cf = XFRAME (frame);
1819 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
1820 && FRAME_DISPLAY_INFO (cf) == FRAME_DISPLAY_INFO (f)
1821 && FRAME_FONT (f)
1822 && FRAME_FONT (f)->pixel_size == pixel_size)
1824 xfs = FRAME_XIC_FONTSET (cf);
1825 break;
1829 if (! xfs)
1831 char buf[256];
1832 char **missing_list;
1833 int missing_count;
1834 char *def_string;
1835 const char *xlfd_format = "-*-*-medium-r-normal--%d-*-*-*-*-*";
1837 sprintf (buf, xlfd_format, pixel_size);
1838 missing_list = NULL;
1839 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
1840 &missing_list, &missing_count, &def_string);
1841 #ifdef DEBUG_XIC_FONTSET
1842 print_fontset_result (xfs, buf, missing_list, missing_count);
1843 #endif
1844 if (missing_list)
1845 XFreeStringList (missing_list);
1846 if (! xfs)
1848 /* List of pixel sizes most likely available. Find one that
1849 is closest to pixel_size. */
1850 int sizes[] = {0, 8, 10, 11, 12, 14, 17, 18, 20, 24, 26, 34, 0};
1851 int *smaller, *larger;
1853 for (smaller = sizes; smaller[1]; smaller++)
1854 if (smaller[1] >= pixel_size)
1855 break;
1856 larger = smaller + 1;
1857 if (*larger == pixel_size)
1858 larger++;
1859 while (*smaller || *larger)
1861 int this_size;
1863 if (! *larger)
1864 this_size = *smaller--;
1865 else if (! *smaller)
1866 this_size = *larger++;
1867 else if (pixel_size - *smaller < *larger - pixel_size)
1868 this_size = *smaller--;
1869 else
1870 this_size = *larger++;
1871 sprintf (buf, xlfd_format, this_size);
1872 missing_list = NULL;
1873 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
1874 &missing_list, &missing_count, &def_string);
1875 #ifdef DEBUG_XIC_FONTSET
1876 print_fontset_result (xfs, buf, missing_list, missing_count);
1877 #endif
1878 if (missing_list)
1879 XFreeStringList (missing_list);
1880 if (xfs)
1881 break;
1884 if (! xfs)
1886 const char *last_resort = "-*-*-*-r-normal--*-*-*-*-*-*";
1888 missing_list = NULL;
1889 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), last_resort,
1890 &missing_list, &missing_count, &def_string);
1891 #ifdef DEBUG_XIC_FONTSET
1892 print_fontset_result (xfs, last_resort, missing_list, missing_count);
1893 #endif
1894 if (missing_list)
1895 XFreeStringList (missing_list);
1900 return xfs;
1903 /* Free the X fontset of frame F if it is the last frame using it. */
1905 void
1906 xic_free_xfontset (struct frame *f)
1908 Lisp_Object rest, frame;
1909 bool shared_p = 0;
1911 if (!FRAME_XIC_FONTSET (f))
1912 return;
1914 /* See if there is another frame sharing the same fontset. */
1915 FOR_EACH_FRAME (rest, frame)
1917 struct frame *cf = XFRAME (frame);
1918 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
1919 && FRAME_DISPLAY_INFO (cf) == FRAME_DISPLAY_INFO (f)
1920 && FRAME_XIC_FONTSET (cf) == FRAME_XIC_FONTSET (f))
1922 shared_p = 1;
1923 break;
1927 if (!shared_p)
1928 /* The fontset is not used anymore. It is safe to free it. */
1929 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
1931 FRAME_XIC_FONTSET (f) = NULL;
1935 /* Value is the best input style, given user preferences USER (already
1936 checked to be supported by Emacs), and styles supported by the
1937 input method XIM. */
1939 static XIMStyle
1940 best_xim_style (XIMStyles *user, XIMStyles *xim)
1942 int i, j;
1944 for (i = 0; i < user->count_styles; ++i)
1945 for (j = 0; j < xim->count_styles; ++j)
1946 if (user->supported_styles[i] == xim->supported_styles[j])
1947 return user->supported_styles[i];
1949 /* Return the default style. */
1950 return XIMPreeditNothing | XIMStatusNothing;
1953 /* Create XIC for frame F. */
1955 static XIMStyle xic_style;
1957 void
1958 create_frame_xic (struct frame *f)
1960 XIM xim;
1961 XIC xic = NULL;
1962 XFontSet xfs = NULL;
1964 if (FRAME_XIC (f))
1965 return;
1967 /* Create X fontset. */
1968 xfs = xic_create_xfontset (f);
1969 xim = FRAME_X_XIM (f);
1970 if (xim)
1972 XRectangle s_area;
1973 XPoint spot;
1974 XVaNestedList preedit_attr;
1975 XVaNestedList status_attr;
1977 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
1978 spot.x = 0; spot.y = 1;
1980 /* Determine XIC style. */
1981 if (xic_style == 0)
1983 XIMStyles supported_list;
1984 supported_list.count_styles = (sizeof supported_xim_styles
1985 / sizeof supported_xim_styles[0]);
1986 supported_list.supported_styles = supported_xim_styles;
1987 xic_style = best_xim_style (&supported_list,
1988 FRAME_X_XIM_STYLES (f));
1991 preedit_attr = XVaCreateNestedList (0,
1992 XNFontSet, xfs,
1993 XNForeground,
1994 FRAME_FOREGROUND_PIXEL (f),
1995 XNBackground,
1996 FRAME_BACKGROUND_PIXEL (f),
1997 (xic_style & XIMPreeditPosition
1998 ? XNSpotLocation
1999 : NULL),
2000 &spot,
2001 NULL);
2002 status_attr = XVaCreateNestedList (0,
2003 XNArea,
2004 &s_area,
2005 XNFontSet,
2006 xfs,
2007 XNForeground,
2008 FRAME_FOREGROUND_PIXEL (f),
2009 XNBackground,
2010 FRAME_BACKGROUND_PIXEL (f),
2011 NULL);
2013 xic = XCreateIC (xim,
2014 XNInputStyle, xic_style,
2015 XNClientWindow, FRAME_X_WINDOW (f),
2016 XNFocusWindow, FRAME_X_WINDOW (f),
2017 XNStatusAttributes, status_attr,
2018 XNPreeditAttributes, preedit_attr,
2019 NULL);
2020 XFree (preedit_attr);
2021 XFree (status_attr);
2024 FRAME_XIC (f) = xic;
2025 FRAME_XIC_STYLE (f) = xic_style;
2026 FRAME_XIC_FONTSET (f) = xfs;
2030 /* Destroy XIC and free XIC fontset of frame F, if any. */
2032 void
2033 free_frame_xic (struct frame *f)
2035 if (FRAME_XIC (f) == NULL)
2036 return;
2038 XDestroyIC (FRAME_XIC (f));
2039 xic_free_xfontset (f);
2041 FRAME_XIC (f) = NULL;
2045 /* Place preedit area for XIC of window W's frame to specified
2046 pixel position X/Y. X and Y are relative to window W. */
2048 void
2049 xic_set_preeditarea (struct window *w, int x, int y)
2051 struct frame *f = XFRAME (w->frame);
2052 XVaNestedList attr;
2053 XPoint spot;
2055 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x) + WINDOW_LEFT_FRINGE_WIDTH (w);
2056 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
2057 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
2058 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2059 XFree (attr);
2063 /* Place status area for XIC in bottom right corner of frame F.. */
2065 void
2066 xic_set_statusarea (struct frame *f)
2068 XIC xic = FRAME_XIC (f);
2069 XVaNestedList attr;
2070 XRectangle area;
2071 XRectangle *needed;
2073 /* Negotiate geometry of status area. If input method has existing
2074 status area, use its current size. */
2075 area.x = area.y = area.width = area.height = 0;
2076 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
2077 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2078 XFree (attr);
2080 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
2081 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2082 XFree (attr);
2084 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
2086 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
2087 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2088 XFree (attr);
2091 area.width = needed->width;
2092 area.height = needed->height;
2093 area.x = FRAME_PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
2094 area.y = (FRAME_PIXEL_HEIGHT (f) - area.height
2095 - FRAME_MENUBAR_HEIGHT (f)
2096 - FRAME_TOOLBAR_TOP_HEIGHT (f)
2097 - FRAME_INTERNAL_BORDER_WIDTH (f));
2098 XFree (needed);
2100 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
2101 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2102 XFree (attr);
2106 /* Set X fontset for XIC of frame F, using base font name
2107 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
2109 void
2110 xic_set_xfontset (struct frame *f, const char *base_fontname)
2112 XVaNestedList attr;
2113 XFontSet xfs;
2115 xic_free_xfontset (f);
2117 xfs = xic_create_xfontset (f);
2119 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
2120 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
2121 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2122 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
2123 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
2124 XFree (attr);
2126 FRAME_XIC_FONTSET (f) = xfs;
2129 #endif /* HAVE_X_I18N */
2133 #ifdef USE_X_TOOLKIT
2135 /* Create and set up the X widget for frame F. */
2137 static void
2138 x_window (struct frame *f, long window_prompting, int minibuffer_only)
2140 XClassHint class_hints;
2141 XSetWindowAttributes attributes;
2142 unsigned long attribute_mask;
2143 Widget shell_widget;
2144 Widget pane_widget;
2145 Widget frame_widget;
2146 Arg al [25];
2147 int ac;
2149 block_input ();
2151 /* Use the resource name as the top-level widget name
2152 for looking up resources. Make a non-Lisp copy
2153 for the window manager, so GC relocation won't bother it.
2155 Elsewhere we specify the window name for the window manager. */
2156 f->namebuf = xstrdup (SSDATA (Vx_resource_name));
2158 ac = 0;
2159 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
2160 XtSetArg (al[ac], XtNinput, 1); ac++;
2161 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2162 XtSetArg (al[ac], XtNborderWidth, f->border_width); ac++;
2163 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2164 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2165 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2166 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
2167 applicationShellWidgetClass,
2168 FRAME_X_DISPLAY (f), al, ac);
2170 f->output_data.x->widget = shell_widget;
2171 /* maybe_set_screen_title_format (shell_widget); */
2173 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
2174 NULL, shell_widget, False,
2175 NULL, NULL, NULL, NULL);
2177 ac = 0;
2178 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2179 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2180 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2181 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2182 XtSetValues (pane_widget, al, ac);
2183 f->output_data.x->column_widget = pane_widget;
2185 /* mappedWhenManaged to false tells to the paned window to not map/unmap
2186 the emacs screen when changing menubar. This reduces flickering. */
2188 ac = 0;
2189 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2190 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
2191 XtSetArg (al[ac], XtNallowResize, 1); ac++;
2192 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
2193 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
2194 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2195 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2196 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2197 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2198 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
2199 al, ac);
2201 f->output_data.x->edit_widget = frame_widget;
2203 XtManageChild (frame_widget);
2205 /* Do some needed geometry management. */
2207 char *tem, shell_position[sizeof "=x++" + 4 * INT_STRLEN_BOUND (int)];
2208 Arg gal[10];
2209 int gac = 0;
2210 int extra_borders = 0;
2211 int menubar_size
2212 = (f->output_data.x->menubar_widget
2213 ? (f->output_data.x->menubar_widget->core.height
2214 + f->output_data.x->menubar_widget->core.border_width)
2215 : 0);
2217 #if 0 /* Experimentally, we now get the right results
2218 for -geometry -0-0 without this. 24 Aug 96, rms. */
2219 if (FRAME_EXTERNAL_MENU_BAR (f))
2221 Dimension ibw = 0;
2222 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
2223 menubar_size += ibw;
2225 #endif
2227 f->output_data.x->menubar_height = menubar_size;
2229 #ifndef USE_LUCID
2230 /* Motif seems to need this amount added to the sizes
2231 specified for the shell widget. The Athena/Lucid widgets don't.
2232 Both conclusions reached experimentally. -- rms. */
2233 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
2234 &extra_borders, NULL);
2235 extra_borders *= 2;
2236 #endif
2238 /* Convert our geometry parameters into a geometry string
2239 and specify it.
2240 Note that we do not specify here whether the position
2241 is a user-specified or program-specified one.
2242 We pass that information later, in x_wm_set_size_hints. */
2244 int left = f->left_pos;
2245 int xneg = window_prompting & XNegative;
2246 int top = f->top_pos;
2247 int yneg = window_prompting & YNegative;
2248 if (xneg)
2249 left = -left;
2250 if (yneg)
2251 top = -top;
2253 if (window_prompting & USPosition)
2254 sprintf (shell_position, "=%dx%d%c%d%c%d",
2255 FRAME_PIXEL_WIDTH (f) + extra_borders,
2256 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders,
2257 (xneg ? '-' : '+'), left,
2258 (yneg ? '-' : '+'), top);
2259 else
2261 sprintf (shell_position, "=%dx%d",
2262 FRAME_PIXEL_WIDTH (f) + extra_borders,
2263 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders);
2265 /* Setting x and y when the position is not specified in
2266 the geometry string will set program position in the WM hints.
2267 If Emacs had just one program position, we could set it in
2268 fallback resources, but since each make-frame call can specify
2269 different program positions, this is easier. */
2270 XtSetArg (gal[gac], XtNx, left); gac++;
2271 XtSetArg (gal[gac], XtNy, top); gac++;
2275 /* We don't free this because we don't know whether
2276 it is safe to free it while the frame exists.
2277 It isn't worth the trouble of arranging to free it
2278 when the frame is deleted. */
2279 tem = xstrdup (shell_position);
2280 XtSetArg (gal[gac], XtNgeometry, tem); gac++;
2281 XtSetValues (shell_widget, gal, gac);
2284 XtManageChild (pane_widget);
2285 XtRealizeWidget (shell_widget);
2287 if (FRAME_X_EMBEDDED_P (f))
2288 XReparentWindow (FRAME_X_DISPLAY (f), XtWindow (shell_widget),
2289 f->output_data.x->parent_desc, 0, 0);
2291 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
2293 validate_x_resource_name ();
2295 class_hints.res_name = SSDATA (Vx_resource_name);
2296 class_hints.res_class = SSDATA (Vx_resource_class);
2297 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
2299 #ifdef HAVE_X_I18N
2300 FRAME_XIC (f) = NULL;
2301 if (use_xim)
2302 create_frame_xic (f);
2303 #endif
2305 f->output_data.x->wm_hints.input = True;
2306 f->output_data.x->wm_hints.flags |= InputHint;
2307 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2308 &f->output_data.x->wm_hints);
2310 hack_wm_protocols (f, shell_widget);
2312 #ifdef HACK_EDITRES
2313 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
2314 #endif
2316 /* Do a stupid property change to force the server to generate a
2317 PropertyNotify event so that the event_stream server timestamp will
2318 be initialized to something relevant to the time we created the window.
2320 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
2321 FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
2322 XA_ATOM, 32, PropModeAppend, NULL, 0);
2324 /* Make all the standard events reach the Emacs frame. */
2325 attributes.event_mask = STANDARD_EVENT_SET;
2327 #ifdef HAVE_X_I18N
2328 if (FRAME_XIC (f))
2330 /* XIM server might require some X events. */
2331 unsigned long fevent = NoEventMask;
2332 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2333 attributes.event_mask |= fevent;
2335 #endif /* HAVE_X_I18N */
2337 attribute_mask = CWEventMask;
2338 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
2339 attribute_mask, &attributes);
2341 XtMapWidget (frame_widget);
2343 /* x_set_name normally ignores requests to set the name if the
2344 requested name is the same as the current name. This is the one
2345 place where that assumption isn't correct; f->name is set, but
2346 the X server hasn't been told. */
2348 Lisp_Object name;
2349 int explicit = f->explicit_name;
2351 f->explicit_name = 0;
2352 name = f->name;
2353 fset_name (f, Qnil);
2354 x_set_name (f, name, explicit);
2357 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2358 f->output_data.x->current_cursor
2359 = f->output_data.x->text_cursor);
2361 unblock_input ();
2363 /* This is a no-op, except under Motif. Make sure main areas are
2364 set to something reasonable, in case we get an error later. */
2365 lw_set_main_areas (pane_widget, 0, frame_widget);
2368 #else /* not USE_X_TOOLKIT */
2369 #ifdef USE_GTK
2370 static void
2371 x_window (struct frame *f)
2373 if (! xg_create_frame_widgets (f))
2374 error ("Unable to create window");
2376 #ifdef HAVE_X_I18N
2377 FRAME_XIC (f) = NULL;
2378 if (use_xim)
2380 block_input ();
2381 create_frame_xic (f);
2382 if (FRAME_XIC (f))
2384 /* XIM server might require some X events. */
2385 unsigned long fevent = NoEventMask;
2386 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2388 if (fevent != NoEventMask)
2390 XSetWindowAttributes attributes;
2391 XWindowAttributes wattr;
2392 unsigned long attribute_mask;
2394 XGetWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2395 &wattr);
2396 attributes.event_mask = wattr.your_event_mask | fevent;
2397 attribute_mask = CWEventMask;
2398 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2399 attribute_mask, &attributes);
2402 unblock_input ();
2404 #endif
2407 #else /*! USE_GTK */
2408 /* Create and set up the X window for frame F. */
2410 static void
2411 x_window (struct frame *f)
2413 XClassHint class_hints;
2414 XSetWindowAttributes attributes;
2415 unsigned long attribute_mask;
2417 attributes.background_pixel = FRAME_BACKGROUND_PIXEL (f);
2418 attributes.border_pixel = f->output_data.x->border_pixel;
2419 attributes.bit_gravity = StaticGravity;
2420 attributes.backing_store = NotUseful;
2421 attributes.save_under = True;
2422 attributes.event_mask = STANDARD_EVENT_SET;
2423 attributes.colormap = FRAME_X_COLORMAP (f);
2424 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
2425 | CWColormap);
2427 block_input ();
2428 FRAME_X_WINDOW (f)
2429 = XCreateWindow (FRAME_X_DISPLAY (f),
2430 f->output_data.x->parent_desc,
2431 f->left_pos,
2432 f->top_pos,
2433 FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f),
2434 f->border_width,
2435 CopyFromParent, /* depth */
2436 InputOutput, /* class */
2437 FRAME_X_VISUAL (f),
2438 attribute_mask, &attributes);
2440 #ifdef HAVE_X_I18N
2441 if (use_xim)
2443 create_frame_xic (f);
2444 if (FRAME_XIC (f))
2446 /* XIM server might require some X events. */
2447 unsigned long fevent = NoEventMask;
2448 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2449 attributes.event_mask |= fevent;
2450 attribute_mask = CWEventMask;
2451 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2452 attribute_mask, &attributes);
2455 #endif /* HAVE_X_I18N */
2457 validate_x_resource_name ();
2459 class_hints.res_name = SSDATA (Vx_resource_name);
2460 class_hints.res_class = SSDATA (Vx_resource_class);
2461 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
2463 /* The menubar is part of the ordinary display;
2464 it does not count in addition to the height of the window. */
2465 f->output_data.x->menubar_height = 0;
2467 /* This indicates that we use the "Passive Input" input model.
2468 Unless we do this, we don't get the Focus{In,Out} events that we
2469 need to draw the cursor correctly. Accursed bureaucrats.
2470 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
2472 f->output_data.x->wm_hints.input = True;
2473 f->output_data.x->wm_hints.flags |= InputHint;
2474 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2475 &f->output_data.x->wm_hints);
2476 f->output_data.x->wm_hints.icon_pixmap = None;
2478 /* Request "save yourself" and "delete window" commands from wm. */
2480 Atom protocols[2];
2481 protocols[0] = FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window;
2482 protocols[1] = FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
2483 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
2486 /* x_set_name normally ignores requests to set the name if the
2487 requested name is the same as the current name. This is the one
2488 place where that assumption isn't correct; f->name is set, but
2489 the X server hasn't been told. */
2491 Lisp_Object name;
2492 int explicit = f->explicit_name;
2494 f->explicit_name = 0;
2495 name = f->name;
2496 fset_name (f, Qnil);
2497 x_set_name (f, name, explicit);
2500 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2501 f->output_data.x->current_cursor
2502 = f->output_data.x->text_cursor);
2504 unblock_input ();
2506 if (FRAME_X_WINDOW (f) == 0)
2507 error ("Unable to create window");
2510 #endif /* not USE_GTK */
2511 #endif /* not USE_X_TOOLKIT */
2513 /* Verify that the icon position args for this window are valid. */
2515 static void
2516 x_icon_verify (struct frame *f, Lisp_Object parms)
2518 Lisp_Object icon_x, icon_y;
2520 /* Set the position of the icon. Note that twm groups all
2521 icons in an icon window. */
2522 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2523 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2524 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2526 CHECK_NUMBER (icon_x);
2527 CHECK_NUMBER (icon_y);
2529 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2530 error ("Both left and top icon corners of icon must be specified");
2533 /* Handle the icon stuff for this window. Perhaps later we might
2534 want an x_set_icon_position which can be called interactively as
2535 well. */
2537 static void
2538 x_icon (struct frame *f, Lisp_Object parms)
2540 Lisp_Object icon_x, icon_y;
2541 #if 0
2542 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2543 #endif
2545 /* Set the position of the icon. Note that twm groups all
2546 icons in an icon window. */
2547 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2548 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2549 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2551 CHECK_TYPE_RANGED_INTEGER (int, icon_x);
2552 CHECK_TYPE_RANGED_INTEGER (int, icon_y);
2554 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2555 error ("Both left and top icon corners of icon must be specified");
2557 block_input ();
2559 if (! EQ (icon_x, Qunbound))
2560 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
2562 #if 0 /* x_get_arg removes the visibility parameter as a side effect,
2563 but x_create_frame still needs it. */
2564 /* Start up iconic or window? */
2565 x_wm_set_window_state
2566 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
2567 Qicon)
2568 ? IconicState
2569 : NormalState));
2570 #endif
2572 x_text_icon (f, SSDATA ((!NILP (f->icon_name)
2573 ? f->icon_name
2574 : f->name)));
2576 unblock_input ();
2579 /* Make the GCs needed for this window, setting the
2580 background, border and mouse colors; also create the
2581 mouse cursor and the gray border tile. */
2583 static void
2584 x_make_gc (struct frame *f)
2586 XGCValues gc_values;
2588 block_input ();
2590 /* Create the GCs of this frame.
2591 Note that many default values are used. */
2593 gc_values.foreground = FRAME_FOREGROUND_PIXEL (f);
2594 gc_values.background = FRAME_BACKGROUND_PIXEL (f);
2595 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
2596 f->output_data.x->normal_gc
2597 = XCreateGC (FRAME_X_DISPLAY (f),
2598 FRAME_X_WINDOW (f),
2599 GCLineWidth | GCForeground | GCBackground,
2600 &gc_values);
2602 /* Reverse video style. */
2603 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2604 gc_values.background = FRAME_FOREGROUND_PIXEL (f);
2605 f->output_data.x->reverse_gc
2606 = XCreateGC (FRAME_X_DISPLAY (f),
2607 FRAME_X_WINDOW (f),
2608 GCForeground | GCBackground | GCLineWidth,
2609 &gc_values);
2611 /* Cursor has cursor-color background, background-color foreground. */
2612 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2613 gc_values.background = f->output_data.x->cursor_pixel;
2614 gc_values.fill_style = FillOpaqueStippled;
2615 f->output_data.x->cursor_gc
2616 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2617 (GCForeground | GCBackground
2618 | GCFillStyle | GCLineWidth),
2619 &gc_values);
2621 /* Create the gray border tile used when the pointer is not in
2622 the frame. Since this depends on the frame's pixel values,
2623 this must be done on a per-frame basis. */
2624 f->output_data.x->border_tile
2625 = (XCreatePixmapFromBitmapData
2626 (FRAME_X_DISPLAY (f), FRAME_DISPLAY_INFO (f)->root_window,
2627 gray_bits, gray_width, gray_height,
2628 FRAME_FOREGROUND_PIXEL (f),
2629 FRAME_BACKGROUND_PIXEL (f),
2630 DefaultDepth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f))));
2632 unblock_input ();
2636 /* Free what was allocated in x_make_gc. */
2638 void
2639 x_free_gcs (struct frame *f)
2641 Display *dpy = FRAME_X_DISPLAY (f);
2643 block_input ();
2645 if (f->output_data.x->normal_gc)
2647 XFreeGC (dpy, f->output_data.x->normal_gc);
2648 f->output_data.x->normal_gc = 0;
2651 if (f->output_data.x->reverse_gc)
2653 XFreeGC (dpy, f->output_data.x->reverse_gc);
2654 f->output_data.x->reverse_gc = 0;
2657 if (f->output_data.x->cursor_gc)
2659 XFreeGC (dpy, f->output_data.x->cursor_gc);
2660 f->output_data.x->cursor_gc = 0;
2663 if (f->output_data.x->border_tile)
2665 XFreePixmap (dpy, f->output_data.x->border_tile);
2666 f->output_data.x->border_tile = 0;
2669 unblock_input ();
2673 /* Handler for signals raised during x_create_frame and
2674 x_create_tip_frame. FRAME is the frame which is partially
2675 constructed. */
2677 static Lisp_Object
2678 unwind_create_frame (Lisp_Object frame)
2680 struct frame *f = XFRAME (frame);
2682 /* If frame is already dead, nothing to do. This can happen if the
2683 display is disconnected after the frame has become official, but
2684 before x_create_frame removes the unwind protect. */
2685 if (!FRAME_LIVE_P (f))
2686 return Qnil;
2688 /* If frame is ``official'', nothing to do. */
2689 if (NILP (Fmemq (frame, Vframe_list)))
2691 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2692 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2693 #endif
2695 x_free_frame_resources (f);
2696 free_glyphs (f);
2698 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2699 /* Check that reference counts are indeed correct. */
2700 eassert (dpyinfo->reference_count == dpyinfo_refcount);
2701 eassert (dpyinfo->terminal->image_cache->refcount == image_cache_refcount);
2702 #endif
2703 return Qt;
2706 return Qnil;
2709 static void
2710 do_unwind_create_frame (Lisp_Object frame)
2712 unwind_create_frame (frame);
2715 static void
2716 unwind_create_frame_1 (Lisp_Object val)
2718 inhibit_lisp_code = val;
2721 static void
2722 x_default_font_parameter (struct frame *f, Lisp_Object parms)
2724 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2725 Lisp_Object font_param = x_get_arg (dpyinfo, parms, Qfont, NULL, NULL,
2726 RES_TYPE_STRING);
2727 Lisp_Object font = Qnil;
2728 if (EQ (font_param, Qunbound))
2729 font_param = Qnil;
2731 if (NILP (font_param))
2733 /* System font should take precedence over X resources. We suggest this
2734 regardless of font-use-system-font because .emacs may not have been
2735 read yet. */
2736 const char *system_font = xsettings_get_system_font ();
2737 if (system_font)
2738 font = font_open_by_name (f, build_unibyte_string (system_font));
2741 if (NILP (font))
2742 font = !NILP (font_param) ? font_param
2743 : x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
2745 if (! FONTP (font) && ! STRINGP (font))
2747 const char *names[]
2749 #ifdef HAVE_XFT
2750 /* This will find the normal Xft font. */
2751 "monospace-10",
2752 #endif
2753 "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1",
2754 "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2755 "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2756 /* This was formerly the first thing tried, but it finds
2757 too many fonts and takes too long. */
2758 "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1",
2759 /* If those didn't work, look for something which will
2760 at least work. */
2761 "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1",
2762 "fixed",
2763 NULL };
2764 int i;
2766 for (i = 0; names[i]; i++)
2768 font = font_open_by_name (f, build_unibyte_string (names[i]));
2769 if (! NILP (font))
2770 break;
2772 if (NILP (font))
2773 error ("No suitable font was found");
2775 else if (!NILP (font_param))
2777 /* Remember the explicit font parameter, so we can re-apply it after
2778 we've applied the `default' face settings. */
2779 x_set_frame_parameters (f, list1 (Fcons (Qfont_param, font_param)));
2782 /* This call will make X resources override any system font setting. */
2783 x_default_parameter (f, parms, Qfont, font, "font", "Font", RES_TYPE_STRING);
2787 DEFUN ("x-wm-set-size-hint", Fx_wm_set_size_hint, Sx_wm_set_size_hint,
2788 0, 1, 0,
2789 doc: /* Send the size hints for frame FRAME to the window manager.
2790 If FRAME is omitted or nil, use the selected frame.
2791 Signal error if FRAME is not an X frame. */)
2792 (Lisp_Object frame)
2794 struct frame *f = decode_window_system_frame (frame);
2796 block_input ();
2797 x_wm_set_size_hint (f, 0, 0);
2798 unblock_input ();
2799 return Qnil;
2802 static void
2803 set_machine_and_pid_properties (struct frame *f)
2805 long pid = (long) getpid ();
2807 /* This will set WM_CLIENT_MACHINE and WM_LOCALE_NAME. */
2808 XSetWMProperties (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), NULL, NULL,
2809 NULL, 0, NULL, NULL, NULL);
2810 XChangeProperty (FRAME_X_DISPLAY (f),
2811 FRAME_OUTER_WINDOW (f),
2812 XInternAtom (FRAME_X_DISPLAY (f),
2813 "_NET_WM_PID",
2814 False),
2815 XA_CARDINAL, 32, PropModeReplace,
2816 (unsigned char *) &pid, 1);
2819 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
2820 1, 1, 0,
2821 doc: /* Make a new X window, which is called a "frame" in Emacs terms.
2822 Return an Emacs frame object.
2823 PARMS is an alist of frame parameters.
2824 If the parameters specify that the frame should not have a minibuffer,
2825 and do not specify a specific minibuffer window to use,
2826 then `default-minibuffer-frame' must be a frame whose minibuffer can
2827 be shared by the new frame.
2829 This function is an internal primitive--use `make-frame' instead. */)
2830 (Lisp_Object parms)
2832 struct frame *f;
2833 Lisp_Object frame, tem;
2834 Lisp_Object name;
2835 int minibuffer_only = 0;
2836 long window_prompting = 0;
2837 int width, height;
2838 ptrdiff_t count = SPECPDL_INDEX ();
2839 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
2840 Lisp_Object display;
2841 struct x_display_info *dpyinfo = NULL;
2842 Lisp_Object parent;
2843 struct kboard *kb;
2845 parms = Fcopy_alist (parms);
2847 /* Use this general default value to start with
2848 until we know if this frame has a specified name. */
2849 Vx_resource_name = Vinvocation_name;
2851 display = x_get_arg (dpyinfo, parms, Qterminal, 0, 0, RES_TYPE_NUMBER);
2852 if (EQ (display, Qunbound))
2853 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
2854 if (EQ (display, Qunbound))
2855 display = Qnil;
2856 dpyinfo = check_x_display_info (display);
2857 kb = dpyinfo->terminal->kboard;
2859 if (!dpyinfo->terminal->name)
2860 error ("Terminal is not live, can't create new frames on it");
2862 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
2863 if (!STRINGP (name)
2864 && ! EQ (name, Qunbound)
2865 && ! NILP (name))
2866 error ("Invalid frame name--not a string or nil");
2868 if (STRINGP (name))
2869 Vx_resource_name = name;
2871 /* See if parent window is specified. */
2872 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
2873 if (EQ (parent, Qunbound))
2874 parent = Qnil;
2875 if (! NILP (parent))
2876 CHECK_NUMBER (parent);
2878 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
2879 /* No need to protect DISPLAY because that's not used after passing
2880 it to make_frame_without_minibuffer. */
2881 frame = Qnil;
2882 GCPRO4 (parms, parent, name, frame);
2883 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
2884 RES_TYPE_SYMBOL);
2885 if (EQ (tem, Qnone) || NILP (tem))
2886 f = make_frame_without_minibuffer (Qnil, kb, display);
2887 else if (EQ (tem, Qonly))
2889 f = make_minibuffer_frame ();
2890 minibuffer_only = 1;
2892 else if (WINDOWP (tem))
2893 f = make_frame_without_minibuffer (tem, kb, display);
2894 else
2895 f = make_frame (1);
2897 XSETFRAME (frame, f);
2899 f->terminal = dpyinfo->terminal;
2901 f->output_method = output_x_window;
2902 f->output_data.x = xzalloc (sizeof *f->output_data.x);
2903 f->output_data.x->icon_bitmap = -1;
2904 FRAME_FONTSET (f) = -1;
2905 f->output_data.x->scroll_bar_foreground_pixel = -1;
2906 f->output_data.x->scroll_bar_background_pixel = -1;
2907 #ifdef USE_TOOLKIT_SCROLL_BARS
2908 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
2909 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
2910 #endif /* USE_TOOLKIT_SCROLL_BARS */
2911 f->output_data.x->white_relief.pixel = -1;
2912 f->output_data.x->black_relief.pixel = -1;
2914 fset_icon_name (f,
2915 x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
2916 RES_TYPE_STRING));
2917 if (! STRINGP (f->icon_name))
2918 fset_icon_name (f, Qnil);
2920 FRAME_DISPLAY_INFO (f) = dpyinfo;
2922 /* With FRAME_DISPLAY_INFO set up, this unwind-protect is safe. */
2923 record_unwind_protect (do_unwind_create_frame, frame);
2925 /* These colors will be set anyway later, but it's important
2926 to get the color reference counts right, so initialize them! */
2928 Lisp_Object black;
2929 struct gcpro gcpro1;
2931 /* Function x_decode_color can signal an error. Make
2932 sure to initialize color slots so that we won't try
2933 to free colors we haven't allocated. */
2934 FRAME_FOREGROUND_PIXEL (f) = -1;
2935 FRAME_BACKGROUND_PIXEL (f) = -1;
2936 f->output_data.x->cursor_pixel = -1;
2937 f->output_data.x->cursor_foreground_pixel = -1;
2938 f->output_data.x->border_pixel = -1;
2939 f->output_data.x->mouse_pixel = -1;
2941 black = build_string ("black");
2942 GCPRO1 (black);
2943 FRAME_FOREGROUND_PIXEL (f)
2944 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
2945 FRAME_BACKGROUND_PIXEL (f)
2946 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
2947 f->output_data.x->cursor_pixel
2948 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
2949 f->output_data.x->cursor_foreground_pixel
2950 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
2951 f->output_data.x->border_pixel
2952 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
2953 f->output_data.x->mouse_pixel
2954 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
2955 UNGCPRO;
2958 /* Specify the parent under which to make this X window. */
2960 if (!NILP (parent))
2962 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
2963 f->output_data.x->explicit_parent = 1;
2965 else
2967 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
2968 f->output_data.x->explicit_parent = 0;
2971 /* Set the name; the functions to which we pass f expect the name to
2972 be set. */
2973 if (EQ (name, Qunbound) || NILP (name))
2975 fset_name (f, build_string (dpyinfo->x_id_name));
2976 f->explicit_name = 0;
2978 else
2980 fset_name (f, name);
2981 f->explicit_name = 1;
2982 /* use the frame's title when getting resources for this frame. */
2983 specbind (Qx_resource_name, name);
2986 #ifdef HAVE_FREETYPE
2987 #ifdef HAVE_XFT
2988 register_font_driver (&xftfont_driver, f);
2989 #else /* not HAVE_XFT */
2990 register_font_driver (&ftxfont_driver, f);
2991 #endif /* not HAVE_XFT */
2992 #endif /* HAVE_FREETYPE */
2993 register_font_driver (&xfont_driver, f);
2995 x_default_parameter (f, parms, Qfont_backend, Qnil,
2996 "fontBackend", "FontBackend", RES_TYPE_STRING);
2998 /* Extract the window parameters from the supplied values
2999 that are needed to determine window geometry. */
3000 x_default_font_parameter (f, parms);
3001 if (!FRAME_FONT (f))
3003 delete_frame (frame, Qnoelisp);
3004 error ("Invalid frame font");
3007 /* Frame contents get displaced if an embedded X window has a border. */
3008 if (! FRAME_X_EMBEDDED_P (f))
3009 x_default_parameter (f, parms, Qborder_width, make_number (0),
3010 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
3012 /* This defaults to 1 in order to match xterm. We recognize either
3013 internalBorderWidth or internalBorder (which is what xterm calls
3014 it). */
3015 if (NILP (Fassq (Qinternal_border_width, parms)))
3017 Lisp_Object value;
3019 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
3020 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
3021 if (! EQ (value, Qunbound))
3022 parms = Fcons (Fcons (Qinternal_border_width, value),
3023 parms);
3025 x_default_parameter (f, parms, Qinternal_border_width,
3026 #ifdef USE_GTK /* We used to impose 0 in xg_create_frame_widgets. */
3027 make_number (0),
3028 #else
3029 make_number (1),
3030 #endif
3031 "internalBorderWidth", "internalBorderWidth",
3032 RES_TYPE_NUMBER);
3033 x_default_parameter (f, parms, Qvertical_scroll_bars,
3034 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3035 Qright,
3036 #else
3037 Qleft,
3038 #endif
3039 "verticalScrollBars", "ScrollBars",
3040 RES_TYPE_SYMBOL);
3042 /* Also do the stuff which must be set before the window exists. */
3043 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
3044 "foreground", "Foreground", RES_TYPE_STRING);
3045 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
3046 "background", "Background", RES_TYPE_STRING);
3047 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
3048 "pointerColor", "Foreground", RES_TYPE_STRING);
3049 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
3050 "borderColor", "BorderColor", RES_TYPE_STRING);
3051 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
3052 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
3053 x_default_parameter (f, parms, Qline_spacing, Qnil,
3054 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
3055 x_default_parameter (f, parms, Qleft_fringe, Qnil,
3056 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
3057 x_default_parameter (f, parms, Qright_fringe, Qnil,
3058 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
3060 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
3061 "scrollBarForeground",
3062 "ScrollBarForeground", 1);
3063 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
3064 "scrollBarBackground",
3065 "ScrollBarBackground", 0);
3067 #ifdef GLYPH_DEBUG
3068 image_cache_refcount =
3069 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
3070 dpyinfo_refcount = dpyinfo->reference_count;
3071 #endif /* GLYPH_DEBUG */
3073 /* Init faces before x_default_parameter is called for scroll-bar
3074 parameters because that function calls x_set_scroll_bar_width,
3075 which calls change_frame_size, which calls Fset_window_buffer,
3076 which runs hooks, which call Fvertical_motion. At the end, we
3077 end up in init_iterator with a null face cache, which should not
3078 happen. */
3079 init_frame_faces (f);
3081 /* PXW: This is a duplicate from below. We have to do it here since
3082 otherwise x_set_tool_bar_lines will work with the character sizes
3083 installed by init_frame_faces while the frame's pixel size is still
3084 calculated from a character size of 1 and we subsequently hit the
3085 eassert (height >= 0) assertion in window_box_height. The
3086 non-pixelwise code apparently worked around this because it had one
3087 frame line vs one toolbar line which left us with a zero root
3088 window height which was obviously wrong as well ... */
3089 change_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
3090 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 1, 0, 0, 1);
3092 /* Set the menu-bar-lines and tool-bar-lines parameters. We don't
3093 look up the X resources controlling the menu-bar and tool-bar
3094 here; they are processed specially at startup, and reflected in
3095 the values of the mode variables.
3097 Avoid calling window-configuration-change-hook; otherwise we
3098 could get an infloop in next_frame since the frame is not yet in
3099 Vframe_list. */
3101 ptrdiff_t count2 = SPECPDL_INDEX ();
3102 record_unwind_protect (unwind_create_frame_1, inhibit_lisp_code);
3103 inhibit_lisp_code = Qt;
3105 x_default_parameter (f, parms, Qmenu_bar_lines,
3106 NILP (Vmenu_bar_mode)
3107 ? make_number (0) : make_number (1),
3108 NULL, NULL, RES_TYPE_NUMBER);
3109 x_default_parameter (f, parms, Qtool_bar_lines,
3110 NILP (Vtool_bar_mode)
3111 ? make_number (0) : make_number (1),
3112 NULL, NULL, RES_TYPE_NUMBER);
3114 unbind_to (count2, Qnil);
3117 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
3118 "bufferPredicate", "BufferPredicate",
3119 RES_TYPE_SYMBOL);
3120 x_default_parameter (f, parms, Qtitle, Qnil,
3121 "title", "Title", RES_TYPE_STRING);
3122 x_default_parameter (f, parms, Qwait_for_wm, Qt,
3123 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN);
3124 x_default_parameter (f, parms, Qfullscreen, Qnil,
3125 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
3126 x_default_parameter (f, parms, Qtool_bar_position,
3127 f->tool_bar_position, 0, 0, RES_TYPE_SYMBOL);
3129 /* Compute the size of the X window. */
3130 window_prompting = x_figure_window_size (f, parms, 1);
3132 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
3133 f->no_split = minibuffer_only || EQ (tem, Qt);
3135 x_icon_verify (f, parms);
3137 /* Create the X widget or window. */
3138 #ifdef USE_X_TOOLKIT
3139 x_window (f, window_prompting, minibuffer_only);
3140 #else
3141 x_window (f);
3142 #endif
3144 x_icon (f, parms);
3145 x_make_gc (f);
3147 /* Now consider the frame official. */
3148 f->terminal->reference_count++;
3149 FRAME_DISPLAY_INFO (f)->reference_count++;
3150 Vframe_list = Fcons (frame, Vframe_list);
3152 /* We need to do this after creating the X window, so that the
3153 icon-creation functions can say whose icon they're describing. */
3154 x_default_parameter (f, parms, Qicon_type, Qt,
3155 "bitmapIcon", "BitmapIcon", RES_TYPE_BOOLEAN);
3157 x_default_parameter (f, parms, Qauto_raise, Qnil,
3158 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3159 x_default_parameter (f, parms, Qauto_lower, Qnil,
3160 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3161 x_default_parameter (f, parms, Qcursor_type, Qbox,
3162 "cursorType", "CursorType", RES_TYPE_SYMBOL);
3163 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
3164 "scrollBarWidth", "ScrollBarWidth",
3165 RES_TYPE_NUMBER);
3166 x_default_parameter (f, parms, Qalpha, Qnil,
3167 "alpha", "Alpha", RES_TYPE_NUMBER);
3169 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
3170 Change will not be effected unless different from the current
3171 FRAME_LINES (f). */
3172 width = FRAME_TEXT_WIDTH (f);
3173 height = FRAME_TEXT_HEIGHT (f);
3174 FRAME_TEXT_HEIGHT (f) = 0;
3175 SET_FRAME_WIDTH (f, 0);
3176 change_frame_size (f, width, height, 1, 0, 0, 1);
3178 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3179 /* Create the menu bar. */
3180 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3182 /* If this signals an error, we haven't set size hints for the
3183 frame and we didn't make it visible. */
3184 initialize_frame_menubar (f);
3186 #ifndef USE_GTK
3187 /* This is a no-op, except under Motif where it arranges the
3188 main window for the widgets on it. */
3189 lw_set_main_areas (f->output_data.x->column_widget,
3190 f->output_data.x->menubar_widget,
3191 f->output_data.x->edit_widget);
3192 #endif /* not USE_GTK */
3194 #endif /* USE_X_TOOLKIT || USE_GTK */
3196 /* Tell the server what size and position, etc, we want, and how
3197 badly we want them. This should be done after we have the menu
3198 bar so that its size can be taken into account. */
3199 block_input ();
3200 x_wm_set_size_hint (f, window_prompting, 0);
3201 unblock_input ();
3203 /* Make the window appear on the frame and enable display, unless
3204 the caller says not to. However, with explicit parent, Emacs
3205 cannot control visibility, so don't try. */
3206 if (! f->output_data.x->explicit_parent)
3208 Lisp_Object visibility;
3210 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
3211 RES_TYPE_SYMBOL);
3212 if (EQ (visibility, Qunbound))
3213 visibility = Qt;
3215 if (EQ (visibility, Qicon))
3216 x_iconify_frame (f);
3217 else if (! NILP (visibility))
3218 x_make_frame_visible (f);
3219 else
3221 /* Must have been Qnil. */
3225 block_input ();
3227 /* Set machine name and pid for the purpose of window managers. */
3228 set_machine_and_pid_properties (f);
3230 /* Set the WM leader property. GTK does this itself, so this is not
3231 needed when using GTK. */
3232 if (dpyinfo->client_leader_window != 0)
3234 XChangeProperty (FRAME_X_DISPLAY (f),
3235 FRAME_OUTER_WINDOW (f),
3236 dpyinfo->Xatom_wm_client_leader,
3237 XA_WINDOW, 32, PropModeReplace,
3238 (unsigned char *) &dpyinfo->client_leader_window, 1);
3241 unblock_input ();
3243 /* Initialize `default-minibuffer-frame' in case this is the first
3244 frame on this terminal. */
3245 if (FRAME_HAS_MINIBUF_P (f)
3246 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
3247 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
3248 kset_default_minibuffer_frame (kb, frame);
3250 /* All remaining specified parameters, which have not been "used"
3251 by x_get_arg and friends, now go in the misc. alist of the frame. */
3252 for (tem = parms; CONSP (tem); tem = XCDR (tem))
3253 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
3254 fset_param_alist (f, Fcons (XCAR (tem), f->param_alist));
3256 UNGCPRO;
3258 /* Make sure windows on this frame appear in calls to next-window
3259 and similar functions. */
3260 Vwindow_list = Qnil;
3262 return unbind_to (count, frame);
3266 /* FRAME is used only to get a handle on the X display. We don't pass the
3267 display info directly because we're called from frame.c, which doesn't
3268 know about that structure. */
3270 Lisp_Object
3271 x_get_focus_frame (struct frame *frame)
3273 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (frame);
3274 Lisp_Object xfocus;
3275 if (! dpyinfo->x_focus_frame)
3276 return Qnil;
3278 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
3279 return xfocus;
3283 /* In certain situations, when the window manager follows a
3284 click-to-focus policy, there seems to be no way around calling
3285 XSetInputFocus to give another frame the input focus .
3287 In an ideal world, XSetInputFocus should generally be avoided so
3288 that applications don't interfere with the window manager's focus
3289 policy. But I think it's okay to use when it's clearly done
3290 following a user-command. */
3292 void
3293 x_focus_frame (struct frame *f)
3295 Display *dpy = FRAME_X_DISPLAY (f);
3297 block_input ();
3298 x_catch_errors (dpy);
3300 if (FRAME_X_EMBEDDED_P (f))
3302 /* For Xembedded frames, normally the embedder forwards key
3303 events. See XEmbed Protocol Specification at
3304 http://freedesktop.org/wiki/Specifications/xembed-spec */
3305 xembed_request_focus (f);
3307 else
3309 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3310 RevertToParent, CurrentTime);
3311 x_ewmh_activate_frame (f);
3314 x_uncatch_errors ();
3315 unblock_input ();
3319 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
3320 doc: /* Internal function called by `color-defined-p', which see
3321 .\(Note that the Nextstep version of this function ignores FRAME.) */)
3322 (Lisp_Object color, Lisp_Object frame)
3324 XColor foo;
3325 struct frame *f = decode_window_system_frame (frame);
3327 CHECK_STRING (color);
3329 if (x_defined_color (f, SSDATA (color), &foo, 0))
3330 return Qt;
3331 else
3332 return Qnil;
3335 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
3336 doc: /* Internal function called by `color-values', which see. */)
3337 (Lisp_Object color, Lisp_Object frame)
3339 XColor foo;
3340 struct frame *f = decode_window_system_frame (frame);
3342 CHECK_STRING (color);
3344 if (x_defined_color (f, SSDATA (color), &foo, 0))
3345 return list3i (foo.red, foo.green, foo.blue);
3346 else
3347 return Qnil;
3350 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
3351 doc: /* Internal function called by `display-color-p', which see. */)
3352 (Lisp_Object terminal)
3354 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3356 if (dpyinfo->n_planes <= 2)
3357 return Qnil;
3359 switch (dpyinfo->visual->class)
3361 case StaticColor:
3362 case PseudoColor:
3363 case TrueColor:
3364 case DirectColor:
3365 return Qt;
3367 default:
3368 return Qnil;
3372 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
3373 0, 1, 0,
3374 doc: /* Return t if the X display supports shades of gray.
3375 Note that color displays do support shades of gray.
3376 The optional argument TERMINAL specifies which display to ask about.
3377 TERMINAL should be a terminal object, a frame or a display name (a string).
3378 If omitted or nil, that stands for the selected frame's display. */)
3379 (Lisp_Object terminal)
3381 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3383 if (dpyinfo->n_planes <= 1)
3384 return Qnil;
3386 switch (dpyinfo->visual->class)
3388 case StaticColor:
3389 case PseudoColor:
3390 case TrueColor:
3391 case DirectColor:
3392 case StaticGray:
3393 case GrayScale:
3394 return Qt;
3396 default:
3397 return Qnil;
3401 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
3402 0, 1, 0,
3403 doc: /* Return the width in pixels of the X display TERMINAL.
3404 The optional argument TERMINAL specifies which display to ask about.
3405 TERMINAL should be a terminal object, a frame or a display name (a string).
3406 If omitted or nil, that stands for the selected frame's display.
3408 On \"multi-monitor\" setups this refers to the pixel width for all
3409 physical monitors associated with TERMINAL. To get information for
3410 each physical monitor, use `display-monitor-attributes-list'. */)
3411 (Lisp_Object terminal)
3413 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3415 return make_number (x_display_pixel_width (dpyinfo));
3418 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
3419 Sx_display_pixel_height, 0, 1, 0,
3420 doc: /* Return the height in pixels of the X display TERMINAL.
3421 The optional argument TERMINAL specifies which display to ask about.
3422 TERMINAL should be a terminal object, a frame or a display name (a string).
3423 If omitted or nil, that stands for the selected frame's display.
3425 On \"multi-monitor\" setups this refers to the pixel height for all
3426 physical monitors associated with TERMINAL. To get information for
3427 each physical monitor, use `display-monitor-attributes-list'. */)
3428 (Lisp_Object terminal)
3430 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3432 return make_number (x_display_pixel_height (dpyinfo));
3435 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
3436 0, 1, 0,
3437 doc: /* Return the number of bitplanes of the X display TERMINAL.
3438 The optional argument TERMINAL specifies which display to ask about.
3439 TERMINAL should be a terminal object, a frame or a display name (a string).
3440 If omitted or nil, that stands for the selected frame's display. */)
3441 (Lisp_Object terminal)
3443 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3445 return make_number (dpyinfo->n_planes);
3448 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
3449 0, 1, 0,
3450 doc: /* Return the number of color cells of the X display TERMINAL.
3451 The optional argument TERMINAL specifies which display to ask about.
3452 TERMINAL should be a terminal object, a frame or a display name (a string).
3453 If omitted or nil, that stands for the selected frame's display. */)
3454 (Lisp_Object terminal)
3456 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3458 int nr_planes = DisplayPlanes (dpyinfo->display,
3459 XScreenNumberOfScreen (dpyinfo->screen));
3461 /* Truncate nr_planes to 24 to avoid integer overflow.
3462 Some displays says 32, but only 24 bits are actually significant.
3463 There are only very few and rare video cards that have more than
3464 24 significant bits. Also 24 bits is more than 16 million colors,
3465 it "should be enough for everyone". */
3466 if (nr_planes > 24) nr_planes = 24;
3468 return make_number (1 << nr_planes);
3471 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
3472 Sx_server_max_request_size,
3473 0, 1, 0,
3474 doc: /* Return the maximum request size of the X server of display TERMINAL.
3475 The optional argument TERMINAL specifies which display to ask about.
3476 TERMINAL should be a terminal object, a frame or a display name (a string).
3477 If omitted or nil, that stands for the selected frame's display. */)
3478 (Lisp_Object terminal)
3480 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3482 return make_number (MAXREQUEST (dpyinfo->display));
3485 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
3486 doc: /* Return the "vendor ID" string of the X server of display TERMINAL.
3487 \(Labeling every distributor as a "vendor" embodies the false assumption
3488 that operating systems cannot be developed and distributed noncommercially.)
3489 The optional argument TERMINAL specifies which display to ask about.
3490 TERMINAL should be a terminal object, a frame or a display name (a string).
3491 If omitted or nil, that stands for the selected frame's display. */)
3492 (Lisp_Object terminal)
3494 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3495 const char *vendor = ServerVendor (dpyinfo->display);
3497 if (! vendor) vendor = "";
3498 return build_string (vendor);
3501 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
3502 doc: /* Return the version numbers of the X server of display TERMINAL.
3503 The value is a list of three integers: the major and minor
3504 version numbers of the X Protocol in use, and the distributor-specific release
3505 number. See also the function `x-server-vendor'.
3507 The optional argument TERMINAL specifies which display to ask about.
3508 TERMINAL should be a terminal object, a frame or a display name (a string).
3509 If omitted or nil, that stands for the selected frame's display. */)
3510 (Lisp_Object terminal)
3512 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3513 Display *dpy = dpyinfo->display;
3515 return list3i (ProtocolVersion (dpy), ProtocolRevision (dpy),
3516 VendorRelease (dpy));
3519 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
3520 doc: /* Return the number of screens on the X server of display TERMINAL.
3521 The optional argument TERMINAL specifies which display to ask about.
3522 TERMINAL should be a terminal object, a frame or a display name (a string).
3523 If omitted or nil, that stands for the selected frame's display. */)
3524 (Lisp_Object terminal)
3526 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3528 return make_number (ScreenCount (dpyinfo->display));
3531 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
3532 doc: /* Return the height in millimeters of the X display TERMINAL.
3533 The optional argument TERMINAL specifies which display to ask about.
3534 TERMINAL should be a terminal object, a frame or a display name (a string).
3535 If omitted or nil, that stands for the selected frame's display.
3537 On \"multi-monitor\" setups this refers to the height in millimeters for
3538 all physical monitors associated with TERMINAL. To get information
3539 for each physical monitor, use `display-monitor-attributes-list'. */)
3540 (Lisp_Object terminal)
3542 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3544 return make_number (HeightMMOfScreen (dpyinfo->screen));
3547 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
3548 doc: /* Return the width in millimeters of the X display TERMINAL.
3549 The optional argument TERMINAL specifies which display to ask about.
3550 TERMINAL should be a terminal object, a frame or a display name (a string).
3551 If omitted or nil, that stands for the selected frame's display.
3553 On \"multi-monitor\" setups this refers to the width in millimeters for
3554 all physical monitors associated with TERMINAL. To get information
3555 for each physical monitor, use `display-monitor-attributes-list'. */)
3556 (Lisp_Object terminal)
3558 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3560 return make_number (WidthMMOfScreen (dpyinfo->screen));
3563 DEFUN ("x-display-backing-store", Fx_display_backing_store,
3564 Sx_display_backing_store, 0, 1, 0,
3565 doc: /* Return an indication of whether X display TERMINAL does backing store.
3566 The value may be `always', `when-mapped', or `not-useful'.
3567 The optional argument TERMINAL specifies which display to ask about.
3568 TERMINAL should be a terminal object, a frame or a display name (a string).
3569 If omitted or nil, that stands for the selected frame's display. */)
3570 (Lisp_Object terminal)
3572 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3573 Lisp_Object result;
3575 switch (DoesBackingStore (dpyinfo->screen))
3577 case Always:
3578 result = intern ("always");
3579 break;
3581 case WhenMapped:
3582 result = intern ("when-mapped");
3583 break;
3585 case NotUseful:
3586 result = intern ("not-useful");
3587 break;
3589 default:
3590 error ("Strange value for BackingStore parameter of screen");
3593 return result;
3596 DEFUN ("x-display-visual-class", Fx_display_visual_class,
3597 Sx_display_visual_class, 0, 1, 0,
3598 doc: /* Return the visual class of the X display TERMINAL.
3599 The value is one of the symbols `static-gray', `gray-scale',
3600 `static-color', `pseudo-color', `true-color', or `direct-color'.
3602 The optional argument TERMINAL specifies which display to ask about.
3603 TERMINAL should a terminal object, a frame or a display name (a string).
3604 If omitted or nil, that stands for the selected frame's display. */)
3605 (Lisp_Object terminal)
3607 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3608 Lisp_Object result;
3610 switch (dpyinfo->visual->class)
3612 case StaticGray:
3613 result = intern ("static-gray");
3614 break;
3615 case GrayScale:
3616 result = intern ("gray-scale");
3617 break;
3618 case StaticColor:
3619 result = intern ("static-color");
3620 break;
3621 case PseudoColor:
3622 result = intern ("pseudo-color");
3623 break;
3624 case TrueColor:
3625 result = intern ("true-color");
3626 break;
3627 case DirectColor:
3628 result = intern ("direct-color");
3629 break;
3630 default:
3631 error ("Display has an unknown visual class");
3634 return result;
3637 DEFUN ("x-display-save-under", Fx_display_save_under,
3638 Sx_display_save_under, 0, 1, 0,
3639 doc: /* Return t if the X display TERMINAL supports the save-under feature.
3640 The optional argument TERMINAL specifies which display to ask about.
3641 TERMINAL should be a terminal object, a frame or a display name (a string).
3642 If omitted or nil, that stands for the selected frame's display. */)
3643 (Lisp_Object terminal)
3645 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3647 if (DoesSaveUnders (dpyinfo->screen) == True)
3648 return Qt;
3649 else
3650 return Qnil;
3653 /* Store the geometry of the workarea on display DPYINFO into *RECT.
3654 Return false if and only if the workarea information cannot be
3655 obtained via the _NET_WORKAREA root window property. */
3657 #if ! GTK_CHECK_VERSION (3, 4, 0)
3658 static bool
3659 x_get_net_workarea (struct x_display_info *dpyinfo, XRectangle *rect)
3661 Display *dpy = dpyinfo->display;
3662 long offset, max_len;
3663 Atom target_type, actual_type;
3664 unsigned long actual_size, bytes_remaining;
3665 int rc, actual_format;
3666 unsigned char *tmp_data = NULL;
3667 bool result = false;
3669 x_catch_errors (dpy);
3670 offset = 0;
3671 max_len = 1;
3672 target_type = XA_CARDINAL;
3673 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3674 dpyinfo->Xatom_net_current_desktop,
3675 offset, max_len, False, target_type,
3676 &actual_type, &actual_format, &actual_size,
3677 &bytes_remaining, &tmp_data);
3678 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3679 && actual_format == 32 && actual_size == max_len)
3681 long current_desktop = ((long *) tmp_data)[0];
3683 XFree (tmp_data);
3684 tmp_data = NULL;
3686 offset = 4 * current_desktop;
3687 max_len = 4;
3688 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3689 dpyinfo->Xatom_net_workarea,
3690 offset, max_len, False, target_type,
3691 &actual_type, &actual_format, &actual_size,
3692 &bytes_remaining, &tmp_data);
3693 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3694 && actual_format == 32 && actual_size == max_len)
3696 long *values = (long *) tmp_data;
3698 rect->x = values[0];
3699 rect->y = values[1];
3700 rect->width = values[2];
3701 rect->height = values[3];
3703 XFree (tmp_data);
3704 tmp_data = NULL;
3706 result = true;
3709 if (tmp_data)
3710 XFree (tmp_data);
3711 x_uncatch_errors ();
3713 return result;
3715 #endif
3717 #ifndef USE_GTK
3719 /* Return monitor number where F is "most" or closest to. */
3720 static int
3721 x_get_monitor_for_frame (struct frame *f,
3722 struct MonitorInfo *monitors,
3723 int n_monitors)
3725 XRectangle frect;
3726 int area = 0, dist = -1;
3727 int best_area = -1, best_dist = -1;
3728 int i;
3730 if (n_monitors == 1) return 0;
3731 frect.x = f->left_pos;
3732 frect.y = f->top_pos;
3733 frect.width = FRAME_PIXEL_WIDTH (f);
3734 frect.height = FRAME_PIXEL_HEIGHT (f);
3736 for (i = 0; i < n_monitors; ++i)
3738 struct MonitorInfo *mi = &monitors[i];
3739 XRectangle res;
3740 int a = 0;
3742 if (mi->geom.width == 0) continue;
3744 if (x_intersect_rectangles (&mi->geom, &frect, &res))
3746 a = res.width * res.height;
3747 if (a > area)
3749 area = a;
3750 best_area = i;
3754 if (a == 0 && area == 0)
3756 int dx, dy, d;
3757 if (frect.x + frect.width < mi->geom.x)
3758 dx = mi->geom.x - frect.x + frect.width;
3759 else if (frect.x > mi->geom.x + mi->geom.width)
3760 dx = frect.x - mi->geom.x + mi->geom.width;
3761 else
3762 dx = 0;
3763 if (frect.y + frect.height < mi->geom.y)
3764 dy = mi->geom.y - frect.y + frect.height;
3765 else if (frect.y > mi->geom.y + mi->geom.height)
3766 dy = frect.y - mi->geom.y + mi->geom.height;
3767 else
3768 dy = 0;
3770 d = dx*dx + dy*dy;
3771 if (dist == -1 || dist > d)
3773 dist = d;
3774 best_dist = i;
3779 return best_area != -1 ? best_area : (best_dist != -1 ? best_dist : 0);
3782 static Lisp_Object
3783 x_make_monitor_attribute_list (struct MonitorInfo *monitors,
3784 int n_monitors,
3785 int primary_monitor,
3786 struct x_display_info *dpyinfo,
3787 const char *source)
3789 Lisp_Object monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
3790 Lisp_Object frame, rest;
3792 FOR_EACH_FRAME (rest, frame)
3794 struct frame *f = XFRAME (frame);
3796 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo
3797 && !EQ (frame, tip_frame))
3799 int i = x_get_monitor_for_frame (f, monitors, n_monitors);
3800 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
3804 return make_monitor_attribute_list (monitors, n_monitors, primary_monitor,
3805 monitor_frames, source);
3808 static Lisp_Object
3809 x_get_monitor_attributes_fallback (struct x_display_info *dpyinfo)
3811 struct MonitorInfo monitor;
3812 XRectangle workarea_r;
3814 /* Fallback: treat (possibly) multiple physical monitors as if they
3815 formed a single monitor as a whole. This should provide a
3816 consistent result at least on single monitor environments. */
3817 monitor.geom.x = monitor.geom.y = 0;
3818 monitor.geom.width = x_display_pixel_width (dpyinfo);
3819 monitor.geom.height = x_display_pixel_height (dpyinfo);
3820 monitor.mm_width = WidthMMOfScreen (dpyinfo->screen);
3821 monitor.mm_height = HeightMMOfScreen (dpyinfo->screen);
3822 monitor.name = xstrdup ("combined screen");
3824 if (x_get_net_workarea (dpyinfo, &workarea_r))
3825 monitor.work = workarea_r;
3826 else
3827 monitor.work = monitor.geom;
3828 return x_make_monitor_attribute_list (&monitor, 1, 0, dpyinfo, "fallback");
3832 #ifdef HAVE_XINERAMA
3833 static Lisp_Object
3834 x_get_monitor_attributes_xinerama (struct x_display_info *dpyinfo)
3836 int n_monitors, i;
3837 Lisp_Object attributes_list = Qnil;
3838 Display *dpy = dpyinfo->display;
3839 XineramaScreenInfo *info = XineramaQueryScreens (dpy, &n_monitors);
3840 struct MonitorInfo *monitors;
3841 double mm_width_per_pixel, mm_height_per_pixel;
3843 if (! info || n_monitors == 0)
3845 if (info)
3846 XFree (info);
3847 return attributes_list;
3850 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
3851 / x_display_pixel_width (dpyinfo));
3852 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
3853 / x_display_pixel_height (dpyinfo));
3854 monitors = xzalloc (n_monitors * sizeof *monitors);
3855 for (i = 0; i < n_monitors; ++i)
3857 struct MonitorInfo *mi = &monitors[i];
3858 XRectangle workarea_r;
3860 mi->geom.x = info[i].x_org;
3861 mi->geom.y = info[i].y_org;
3862 mi->geom.width = info[i].width;
3863 mi->geom.height = info[i].height;
3864 mi->mm_width = mi->geom.width * mm_width_per_pixel + 0.5;
3865 mi->mm_height = mi->geom.height * mm_height_per_pixel + 0.5;
3866 mi->name = 0;
3868 /* Xinerama usually have primary monitor first, just use that. */
3869 if (i == 0 && x_get_net_workarea (dpyinfo, &workarea_r))
3871 mi->work = workarea_r;
3872 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
3873 mi->work = mi->geom;
3875 else
3876 mi->work = mi->geom;
3878 XFree (info);
3880 attributes_list = x_make_monitor_attribute_list (monitors,
3881 n_monitors,
3883 dpyinfo,
3884 "Xinerama");
3885 free_monitors (monitors, n_monitors);
3886 return attributes_list;
3888 #endif /* HAVE_XINERAMA */
3891 #ifdef HAVE_XRANDR
3892 static Lisp_Object
3893 x_get_monitor_attributes_xrandr (struct x_display_info *dpyinfo)
3895 Lisp_Object attributes_list = Qnil;
3896 XRRScreenResources *resources;
3897 Display *dpy = dpyinfo->display;
3898 int i, n_monitors, primary = -1;
3899 RROutput pxid = None;
3900 struct MonitorInfo *monitors;
3902 #ifdef HAVE_XRRGETSCREENRESOURCESCURRENT
3903 resources = XRRGetScreenResourcesCurrent (dpy, dpyinfo->root_window);
3904 #else
3905 resources = XRRGetScreenResources (dpy, dpyinfo->root_window);
3906 #endif
3907 if (! resources || resources->noutput == 0)
3909 if (resources)
3910 XRRFreeScreenResources (resources);
3911 return Qnil;
3913 n_monitors = resources->noutput;
3914 monitors = xzalloc (n_monitors * sizeof *monitors);
3916 #ifdef HAVE_XRRGETOUTPUTPRIMARY
3917 pxid = XRRGetOutputPrimary (dpy, dpyinfo->root_window);
3918 #endif
3920 for (i = 0; i < n_monitors; ++i)
3922 XRROutputInfo *info = XRRGetOutputInfo (dpy, resources,
3923 resources->outputs[i]);
3924 Connection conn = info ? info->connection : RR_Disconnected;
3925 RRCrtc id = info ? info->crtc : None;
3927 if (strcmp (info->name, "default") == 0)
3929 /* Non XRandr 1.2 driver, does not give useful data. */
3930 XRRFreeOutputInfo (info);
3931 XRRFreeScreenResources (resources);
3932 free_monitors (monitors, n_monitors);
3933 return Qnil;
3936 if (conn != RR_Disconnected && id != None)
3938 XRRCrtcInfo *crtc = XRRGetCrtcInfo (dpy, resources, id);
3939 struct MonitorInfo *mi = &monitors[i];
3940 XRectangle workarea_r;
3942 if (! crtc)
3944 XRRFreeOutputInfo (info);
3945 continue;
3948 mi->geom.x = crtc->x;
3949 mi->geom.y = crtc->y;
3950 mi->geom.width = crtc->width;
3951 mi->geom.height = crtc->height;
3952 mi->mm_width = info->mm_width;
3953 mi->mm_height = info->mm_height;
3954 mi->name = xstrdup (info->name);
3956 if (pxid != None && pxid == resources->outputs[i])
3957 primary = i;
3958 else if (primary == -1 && strcmp (info->name, "LVDS") == 0)
3959 primary = i;
3961 if (i == primary && x_get_net_workarea (dpyinfo, &workarea_r))
3963 mi->work= workarea_r;
3964 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
3965 mi->work = mi->geom;
3967 else
3968 mi->work = mi->geom;
3970 XRRFreeCrtcInfo (crtc);
3972 XRRFreeOutputInfo (info);
3974 XRRFreeScreenResources (resources);
3976 attributes_list = x_make_monitor_attribute_list (monitors,
3977 n_monitors,
3978 primary,
3979 dpyinfo,
3980 "XRandr");
3981 free_monitors (monitors, n_monitors);
3982 return attributes_list;
3984 #endif /* HAVE_XRANDR */
3986 static Lisp_Object
3987 x_get_monitor_attributes (struct x_display_info *dpyinfo)
3989 Lisp_Object attributes_list = Qnil;
3990 Display *dpy = dpyinfo->display;
3992 (void) dpy; /* Suppress unused variable warning. */
3994 #ifdef HAVE_XRANDR
3995 int xrr_event_base, xrr_error_base;
3996 bool xrr_ok = false;
3997 xrr_ok = XRRQueryExtension (dpy, &xrr_event_base, &xrr_error_base);
3998 if (xrr_ok)
4000 int xrr_major, xrr_minor;
4001 XRRQueryVersion (dpy, &xrr_major, &xrr_minor);
4002 xrr_ok = (xrr_major == 1 && xrr_minor >= 2) || xrr_major > 1;
4005 if (xrr_ok)
4006 attributes_list = x_get_monitor_attributes_xrandr (dpyinfo);
4007 #endif /* HAVE_XRANDR */
4009 #ifdef HAVE_XINERAMA
4010 if (NILP (attributes_list))
4012 int xin_event_base, xin_error_base;
4013 bool xin_ok = false;
4014 xin_ok = XineramaQueryExtension (dpy, &xin_event_base, &xin_error_base);
4015 if (xin_ok && XineramaIsActive (dpy))
4016 attributes_list = x_get_monitor_attributes_xinerama (dpyinfo);
4018 #endif /* HAVE_XINERAMA */
4020 if (NILP (attributes_list))
4021 attributes_list = x_get_monitor_attributes_fallback (dpyinfo);
4023 return attributes_list;
4026 #endif /* !USE_GTK */
4028 DEFUN ("x-display-monitor-attributes-list", Fx_display_monitor_attributes_list,
4029 Sx_display_monitor_attributes_list,
4030 0, 1, 0,
4031 doc: /* Return a list of physical monitor attributes on the X display TERMINAL.
4033 The optional argument TERMINAL specifies which display to ask about.
4034 TERMINAL should be a terminal object, a frame or a display name (a string).
4035 If omitted or nil, that stands for the selected frame's display.
4037 In addition to the standard attribute keys listed in
4038 `display-monitor-attributes-list', the following keys are contained in
4039 the attributes:
4041 source -- String describing the source from which multi-monitor
4042 information is obtained, one of \"Gdk\", \"XRandr\",
4043 \"Xinerama\", or \"fallback\"
4045 Internal use only, use `display-monitor-attributes-list' instead. */)
4046 (Lisp_Object terminal)
4048 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4049 Lisp_Object attributes_list = Qnil;
4051 #ifdef USE_GTK
4052 double mm_width_per_pixel, mm_height_per_pixel;
4053 GdkDisplay *gdpy;
4054 GdkScreen *gscreen;
4055 gint primary_monitor = 0, n_monitors, i;
4056 Lisp_Object monitor_frames, rest, frame;
4057 static const char *source = "Gdk";
4058 struct MonitorInfo *monitors;
4060 block_input ();
4061 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
4062 / x_display_pixel_width (dpyinfo));
4063 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
4064 / x_display_pixel_height (dpyinfo));
4065 gdpy = gdk_x11_lookup_xdisplay (dpyinfo->display);
4066 gscreen = gdk_display_get_default_screen (gdpy);
4067 #if GTK_CHECK_VERSION (2, 20, 0)
4068 primary_monitor = gdk_screen_get_primary_monitor (gscreen);
4069 #endif
4070 n_monitors = gdk_screen_get_n_monitors (gscreen);
4071 monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
4072 monitors = xzalloc (n_monitors * sizeof *monitors);
4074 FOR_EACH_FRAME (rest, frame)
4076 struct frame *f = XFRAME (frame);
4078 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo
4079 && !EQ (frame, tip_frame))
4081 GdkWindow *gwin = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
4083 i = gdk_screen_get_monitor_at_window (gscreen, gwin);
4084 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
4088 for (i = 0; i < n_monitors; ++i)
4090 gint width_mm = -1, height_mm = -1;
4091 GdkRectangle rec, work;
4092 struct MonitorInfo *mi = &monitors[i];
4094 gdk_screen_get_monitor_geometry (gscreen, i, &rec);
4096 #if GTK_CHECK_VERSION (2, 14, 0)
4097 width_mm = gdk_screen_get_monitor_width_mm (gscreen, i);
4098 height_mm = gdk_screen_get_monitor_height_mm (gscreen, i);
4099 #endif
4100 if (width_mm < 0)
4101 width_mm = rec.width * mm_width_per_pixel + 0.5;
4102 if (height_mm < 0)
4103 height_mm = rec.height * mm_height_per_pixel + 0.5;
4105 #if GTK_CHECK_VERSION (3, 4, 0)
4106 gdk_screen_get_monitor_workarea (gscreen, i, &work);
4107 #else
4108 /* Emulate the behavior of GTK+ 3.4. */
4110 XRectangle workarea_r;
4112 if (i == primary_monitor && x_get_net_workarea (dpyinfo, &workarea_r))
4114 work.x = workarea_r.x;
4115 work.y = workarea_r.y;
4116 work.width = workarea_r.width;
4117 work.height = workarea_r.height;
4118 if (! gdk_rectangle_intersect (&rec, &work, &work))
4119 work = rec;
4121 else
4122 work = rec;
4124 #endif
4127 mi->geom.x = rec.x;
4128 mi->geom.y = rec.y;
4129 mi->geom.width = rec.width;
4130 mi->geom.height = rec.height;
4131 mi->work.x = work.x;
4132 mi->work.y = work.y;
4133 mi->work.width = work.width;
4134 mi->work.height = work.height;
4135 mi->mm_width = width_mm;
4136 mi->mm_height = height_mm;
4138 #if GTK_CHECK_VERSION (2, 14, 0)
4139 mi->name = gdk_screen_get_monitor_plug_name (gscreen, i);
4140 #endif
4143 attributes_list = make_monitor_attribute_list (monitors,
4144 n_monitors,
4145 primary_monitor,
4146 monitor_frames,
4147 source);
4148 unblock_input ();
4149 #else /* not USE_GTK */
4151 block_input ();
4152 attributes_list = x_get_monitor_attributes (dpyinfo);
4153 unblock_input ();
4155 #endif /* not USE_GTK */
4157 return attributes_list;
4160 /************************************************************************
4161 X Displays
4162 ************************************************************************/
4165 /* Mapping visual names to visuals. */
4167 static struct visual_class
4169 const char *name;
4170 int class;
4172 visual_classes[] =
4174 {"StaticGray", StaticGray},
4175 {"GrayScale", GrayScale},
4176 {"StaticColor", StaticColor},
4177 {"PseudoColor", PseudoColor},
4178 {"TrueColor", TrueColor},
4179 {"DirectColor", DirectColor},
4180 {NULL, 0}
4184 #ifndef HAVE_XSCREENNUMBEROFSCREEN
4186 /* Value is the screen number of screen SCR. This is a substitute for
4187 the X function with the same name when that doesn't exist. */
4190 XScreenNumberOfScreen (scr)
4191 register Screen *scr;
4193 Display *dpy = scr->display;
4194 int i;
4196 for (i = 0; i < dpy->nscreens; ++i)
4197 if (scr == dpy->screens + i)
4198 break;
4200 return i;
4203 #endif /* not HAVE_XSCREENNUMBEROFSCREEN */
4206 /* Select the visual that should be used on display DPYINFO. Set
4207 members of DPYINFO appropriately. Called from x_term_init. */
4209 void
4210 select_visual (struct x_display_info *dpyinfo)
4212 Display *dpy = dpyinfo->display;
4213 Screen *screen = dpyinfo->screen;
4214 Lisp_Object value;
4216 /* See if a visual is specified. */
4217 value = display_x_get_resource (dpyinfo,
4218 build_string ("visualClass"),
4219 build_string ("VisualClass"),
4220 Qnil, Qnil);
4221 if (STRINGP (value))
4223 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
4224 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
4225 depth, a decimal number. NAME is compared with case ignored. */
4226 char *s = alloca (SBYTES (value) + 1);
4227 char *dash;
4228 int i, class = -1;
4229 XVisualInfo vinfo;
4231 strcpy (s, SSDATA (value));
4232 dash = strchr (s, '-');
4233 if (dash)
4235 dpyinfo->n_planes = atoi (dash + 1);
4236 *dash = '\0';
4238 else
4239 /* We won't find a matching visual with depth 0, so that
4240 an error will be printed below. */
4241 dpyinfo->n_planes = 0;
4243 /* Determine the visual class. */
4244 for (i = 0; visual_classes[i].name; ++i)
4245 if (xstrcasecmp (s, visual_classes[i].name) == 0)
4247 class = visual_classes[i].class;
4248 break;
4251 /* Look up a matching visual for the specified class. */
4252 if (class == -1
4253 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
4254 dpyinfo->n_planes, class, &vinfo))
4255 fatal ("Invalid visual specification `%s'", SDATA (value));
4257 dpyinfo->visual = vinfo.visual;
4259 else
4261 int n_visuals;
4262 XVisualInfo *vinfo, vinfo_template;
4264 dpyinfo->visual = DefaultVisualOfScreen (screen);
4266 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
4267 vinfo_template.screen = XScreenNumberOfScreen (screen);
4268 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
4269 &vinfo_template, &n_visuals);
4270 if (n_visuals <= 0)
4271 fatal ("Can't get proper X visual info");
4273 dpyinfo->n_planes = vinfo->depth;
4274 XFree (vinfo);
4279 /* Return the X display structure for the display named NAME.
4280 Open a new connection if necessary. */
4282 static struct x_display_info *
4283 x_display_info_for_name (Lisp_Object name)
4285 struct x_display_info *dpyinfo;
4287 CHECK_STRING (name);
4289 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
4290 if (!NILP (Fstring_equal (XCAR (dpyinfo->name_list_element), name)))
4291 return dpyinfo;
4293 /* Use this general default value to start with. */
4294 Vx_resource_name = Vinvocation_name;
4296 validate_x_resource_name ();
4298 dpyinfo = x_term_init (name, 0, SSDATA (Vx_resource_name));
4300 if (dpyinfo == 0)
4301 error ("Cannot connect to X server %s", SDATA (name));
4303 XSETFASTINT (Vwindow_system_version, 11);
4305 return dpyinfo;
4309 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
4310 1, 3, 0,
4311 doc: /* Open a connection to a display server.
4312 DISPLAY is the name of the display to connect to.
4313 Optional second arg XRM-STRING is a string of resources in xrdb format.
4314 If the optional third arg MUST-SUCCEED is non-nil,
4315 terminate Emacs if we can't open the connection.
4316 \(In the Nextstep version, the last two arguments are currently ignored.) */)
4317 (Lisp_Object display, Lisp_Object xrm_string, Lisp_Object must_succeed)
4319 char *xrm_option;
4320 struct x_display_info *dpyinfo;
4322 CHECK_STRING (display);
4323 if (! NILP (xrm_string))
4324 CHECK_STRING (xrm_string);
4326 xrm_option = NILP (xrm_string) ? 0 : SSDATA (xrm_string);
4328 validate_x_resource_name ();
4330 /* This is what opens the connection and sets x_current_display.
4331 This also initializes many symbols, such as those used for input. */
4332 dpyinfo = x_term_init (display, xrm_option,
4333 SSDATA (Vx_resource_name));
4335 if (dpyinfo == 0)
4337 if (!NILP (must_succeed))
4338 fatal ("Cannot connect to X server %s.\n\
4339 Check the DISPLAY environment variable or use `-d'.\n\
4340 Also use the `xauth' program to verify that you have the proper\n\
4341 authorization information needed to connect the X server.\n\
4342 An insecure way to solve the problem may be to use `xhost'.\n",
4343 SDATA (display));
4344 else
4345 error ("Cannot connect to X server %s", SDATA (display));
4348 XSETFASTINT (Vwindow_system_version, 11);
4349 return Qnil;
4352 DEFUN ("x-close-connection", Fx_close_connection,
4353 Sx_close_connection, 1, 1, 0,
4354 doc: /* Close the connection to TERMINAL's X server.
4355 For TERMINAL, specify a terminal object, a frame or a display name (a
4356 string). If TERMINAL is nil, that stands for the selected frame's
4357 terminal. */)
4358 (Lisp_Object terminal)
4360 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4362 if (dpyinfo->reference_count > 0)
4363 error ("Display still has frames on it");
4365 x_delete_terminal (dpyinfo->terminal);
4367 return Qnil;
4370 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
4371 doc: /* Return the list of display names that Emacs has connections to. */)
4372 (void)
4374 Lisp_Object result = Qnil;
4375 struct x_display_info *xdi;
4377 for (xdi = x_display_list; xdi; xdi = xdi->next)
4378 result = Fcons (XCAR (xdi->name_list_element), result);
4380 return result;
4383 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
4384 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
4385 This function only has an effect on X Windows. With MS Windows, it is
4386 defined but does nothing.
4388 If ON is nil, allow buffering of requests.
4389 Turning on synchronization prohibits the Xlib routines from buffering
4390 requests and seriously degrades performance, but makes debugging much
4391 easier.
4392 The optional second argument TERMINAL specifies which display to act on.
4393 TERMINAL should be a terminal object, a frame or a display name (a string).
4394 If TERMINAL is omitted or nil, that stands for the selected frame's display. */)
4395 (Lisp_Object on, Lisp_Object terminal)
4397 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4399 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
4401 return Qnil;
4404 /* Wait for responses to all X commands issued so far for frame F. */
4406 void
4407 x_sync (struct frame *f)
4409 block_input ();
4410 XSync (FRAME_X_DISPLAY (f), False);
4411 unblock_input ();
4415 /***********************************************************************
4416 Window properties
4417 ***********************************************************************/
4419 DEFUN ("x-change-window-property", Fx_change_window_property,
4420 Sx_change_window_property, 2, 6, 0,
4421 doc: /* Change window property PROP to VALUE on the X window of FRAME.
4422 PROP must be a string. VALUE may be a string or a list of conses,
4423 numbers and/or strings. If an element in the list is a string, it is
4424 converted to an atom and the value of the atom is used. If an element
4425 is a cons, it is converted to a 32 bit number where the car is the 16
4426 top bits and the cdr is the lower 16 bits.
4428 FRAME nil or omitted means use the selected frame.
4429 If TYPE is given and non-nil, it is the name of the type of VALUE.
4430 If TYPE is not given or nil, the type is STRING.
4431 FORMAT gives the size in bits of each element if VALUE is a list.
4432 It must be one of 8, 16 or 32.
4433 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
4434 If OUTER-P is non-nil, the property is changed for the outer X window of
4435 FRAME. Default is to change on the edit X window. */)
4436 (Lisp_Object prop, Lisp_Object value, Lisp_Object frame,
4437 Lisp_Object type, Lisp_Object format, Lisp_Object outer_p)
4439 struct frame *f = decode_window_system_frame (frame);
4440 Atom prop_atom;
4441 Atom target_type = XA_STRING;
4442 int element_format = 8;
4443 unsigned char *data;
4444 int nelements;
4445 Window w;
4447 CHECK_STRING (prop);
4449 if (! NILP (format))
4451 CHECK_NUMBER (format);
4453 if (XINT (format) != 8 && XINT (format) != 16
4454 && XINT (format) != 32)
4455 error ("FORMAT must be one of 8, 16 or 32");
4456 element_format = XINT (format);
4459 if (CONSP (value))
4461 ptrdiff_t elsize;
4463 nelements = x_check_property_data (value);
4464 if (nelements == -1)
4465 error ("Bad data in VALUE, must be number, string or cons");
4467 /* The man page for XChangeProperty:
4468 "If the specified format is 32, the property data must be a
4469 long array."
4470 This applies even if long is more than 32 bits. The X library
4471 converts to 32 bits before sending to the X server. */
4472 elsize = element_format == 32 ? sizeof (long) : element_format >> 3;
4473 data = xnmalloc (nelements, elsize);
4475 x_fill_property_data (FRAME_X_DISPLAY (f), value, data, element_format);
4477 else
4479 CHECK_STRING (value);
4480 data = SDATA (value);
4481 if (INT_MAX < SBYTES (value))
4482 error ("VALUE too long");
4483 nelements = SBYTES (value);
4486 block_input ();
4487 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4488 if (! NILP (type))
4490 CHECK_STRING (type);
4491 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4494 if (! NILP (outer_p)) w = FRAME_OUTER_WINDOW (f);
4495 else w = FRAME_X_WINDOW (f);
4497 XChangeProperty (FRAME_X_DISPLAY (f), w,
4498 prop_atom, target_type, element_format, PropModeReplace,
4499 data, nelements);
4501 if (CONSP (value)) xfree (data);
4503 /* Make sure the property is set when we return. */
4504 XFlush (FRAME_X_DISPLAY (f));
4505 unblock_input ();
4507 return value;
4511 DEFUN ("x-delete-window-property", Fx_delete_window_property,
4512 Sx_delete_window_property, 1, 2, 0,
4513 doc: /* Remove window property PROP from X window of FRAME.
4514 FRAME nil or omitted means use the selected frame. Value is PROP. */)
4515 (Lisp_Object prop, Lisp_Object frame)
4517 struct frame *f = decode_window_system_frame (frame);
4518 Atom prop_atom;
4520 CHECK_STRING (prop);
4521 block_input ();
4522 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4523 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
4525 /* Make sure the property is removed when we return. */
4526 XFlush (FRAME_X_DISPLAY (f));
4527 unblock_input ();
4529 return prop;
4533 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
4534 1, 6, 0,
4535 doc: /* Value is the value of window property PROP on FRAME.
4536 If FRAME is nil or omitted, use the selected frame.
4538 On X Windows, the following optional arguments are also accepted:
4539 If TYPE is nil or omitted, get the property as a string.
4540 Otherwise TYPE is the name of the atom that denotes the type expected.
4541 If SOURCE is non-nil, get the property on that window instead of from
4542 FRAME. The number 0 denotes the root window.
4543 If DELETE-P is non-nil, delete the property after retrieving it.
4544 If VECTOR-RET-P is non-nil, don't return a string but a vector of values.
4546 On MS Windows, this function accepts but ignores those optional arguments.
4548 Value is nil if FRAME hasn't a property with name PROP or if PROP has
4549 no value of TYPE (always string in the MS Windows case). */)
4550 (Lisp_Object prop, Lisp_Object frame, Lisp_Object type,
4551 Lisp_Object source, Lisp_Object delete_p, Lisp_Object vector_ret_p)
4553 struct frame *f = decode_window_system_frame (frame);
4554 Atom prop_atom;
4555 int rc;
4556 Lisp_Object prop_value = Qnil;
4557 unsigned char *tmp_data = NULL;
4558 Atom actual_type;
4559 Atom target_type = XA_STRING;
4560 int actual_format;
4561 unsigned long actual_size, bytes_remaining;
4562 Window target_window = FRAME_X_WINDOW (f);
4563 struct gcpro gcpro1;
4565 GCPRO1 (prop_value);
4566 CHECK_STRING (prop);
4568 if (! NILP (source))
4570 CONS_TO_INTEGER (source, Window, target_window);
4571 if (! target_window)
4572 target_window = FRAME_DISPLAY_INFO (f)->root_window;
4575 block_input ();
4576 if (STRINGP (type))
4578 if (strcmp ("AnyPropertyType", SSDATA (type)) == 0)
4579 target_type = AnyPropertyType;
4580 else
4581 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4584 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4585 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4586 prop_atom, 0, 0, False, target_type,
4587 &actual_type, &actual_format, &actual_size,
4588 &bytes_remaining, &tmp_data);
4589 if (rc == Success)
4591 int size = bytes_remaining;
4593 XFree (tmp_data);
4594 tmp_data = NULL;
4596 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4597 prop_atom, 0, bytes_remaining,
4598 ! NILP (delete_p), target_type,
4599 &actual_type, &actual_format,
4600 &actual_size, &bytes_remaining,
4601 &tmp_data);
4602 if (rc == Success && tmp_data)
4604 /* The man page for XGetWindowProperty says:
4605 "If the returned format is 32, the returned data is represented
4606 as a long array and should be cast to that type to obtain the
4607 elements."
4608 This applies even if long is more than 32 bits, the X library
4609 converts from 32 bit elements received from the X server to long
4610 and passes the long array to us. Thus, for that case memcpy can not
4611 be used. We convert to a 32 bit type here, because so much code
4612 assume on that.
4614 The bytes and offsets passed to XGetWindowProperty refers to the
4615 property and those are indeed in 32 bit quantities if format is
4616 32. */
4618 if (BITS_PER_LONG > 32 && actual_format == 32)
4620 unsigned long i;
4621 int *idata = (int *) tmp_data;
4622 long *ldata = (long *) tmp_data;
4624 for (i = 0; i < actual_size; ++i)
4625 idata[i] = (int) ldata[i];
4628 if (NILP (vector_ret_p))
4629 prop_value = make_string ((char *) tmp_data, size);
4630 else
4631 prop_value = x_property_data_to_lisp (f,
4632 tmp_data,
4633 actual_type,
4634 actual_format,
4635 actual_size);
4638 if (tmp_data) XFree (tmp_data);
4641 unblock_input ();
4642 UNGCPRO;
4643 return prop_value;
4648 /***********************************************************************
4649 Busy cursor
4650 ***********************************************************************/
4652 /* Timer function of hourglass_atimer. TIMER is equal to
4653 hourglass_atimer.
4655 Display an hourglass pointer on all frames by mapping the frames'
4656 hourglass_window. Set the hourglass_p flag in the frames'
4657 output_data.x structure to indicate that an hourglass cursor is
4658 shown on the frames. */
4660 void
4661 show_hourglass (struct atimer *timer)
4663 /* The timer implementation will cancel this timer automatically
4664 after this function has run. Set hourglass_atimer to null
4665 so that we know the timer doesn't have to be canceled. */
4666 hourglass_atimer = NULL;
4668 if (!hourglass_shown_p)
4670 Lisp_Object rest, frame;
4672 block_input ();
4674 FOR_EACH_FRAME (rest, frame)
4676 struct frame *f = XFRAME (frame);
4678 if (FRAME_LIVE_P (f) && FRAME_X_P (f) && FRAME_X_DISPLAY (f))
4680 Display *dpy = FRAME_X_DISPLAY (f);
4682 #ifdef USE_X_TOOLKIT
4683 if (f->output_data.x->widget)
4684 #else
4685 if (FRAME_OUTER_WINDOW (f))
4686 #endif
4688 f->output_data.x->hourglass_p = 1;
4690 if (!f->output_data.x->hourglass_window)
4692 unsigned long mask = CWCursor;
4693 XSetWindowAttributes attrs;
4694 #ifdef USE_GTK
4695 Window parent = FRAME_X_WINDOW (f);
4696 #else
4697 Window parent = FRAME_OUTER_WINDOW (f);
4698 #endif
4699 attrs.cursor = f->output_data.x->hourglass_cursor;
4701 f->output_data.x->hourglass_window
4702 = XCreateWindow (dpy, parent,
4703 0, 0, 32000, 32000, 0, 0,
4704 InputOnly,
4705 CopyFromParent,
4706 mask, &attrs);
4709 XMapRaised (dpy, f->output_data.x->hourglass_window);
4710 XFlush (dpy);
4715 hourglass_shown_p = 1;
4716 unblock_input ();
4721 /* Hide the hourglass pointer on all frames, if it is currently
4722 shown. */
4724 void
4725 hide_hourglass (void)
4727 if (hourglass_shown_p)
4729 Lisp_Object rest, frame;
4731 block_input ();
4732 FOR_EACH_FRAME (rest, frame)
4734 struct frame *f = XFRAME (frame);
4736 if (FRAME_X_P (f)
4737 /* Watch out for newly created frames. */
4738 && f->output_data.x->hourglass_window)
4740 XUnmapWindow (FRAME_X_DISPLAY (f),
4741 f->output_data.x->hourglass_window);
4742 /* Sync here because XTread_socket looks at the
4743 hourglass_p flag that is reset to zero below. */
4744 XSync (FRAME_X_DISPLAY (f), False);
4745 f->output_data.x->hourglass_p = 0;
4749 hourglass_shown_p = 0;
4750 unblock_input ();
4756 /***********************************************************************
4757 Tool tips
4758 ***********************************************************************/
4760 static Lisp_Object x_create_tip_frame (struct x_display_info *,
4761 Lisp_Object, Lisp_Object);
4762 static void compute_tip_xy (struct frame *, Lisp_Object, Lisp_Object,
4763 Lisp_Object, int, int, int *, int *);
4765 /* The frame of a currently visible tooltip. */
4767 Lisp_Object tip_frame;
4769 /* If non-nil, a timer started that hides the last tooltip when it
4770 fires. */
4772 static Lisp_Object tip_timer;
4773 Window tip_window;
4775 /* If non-nil, a vector of 3 elements containing the last args
4776 with which x-show-tip was called. See there. */
4778 static Lisp_Object last_show_tip_args;
4781 static void
4782 unwind_create_tip_frame (Lisp_Object frame)
4784 Lisp_Object deleted;
4786 deleted = unwind_create_frame (frame);
4787 if (EQ (deleted, Qt))
4789 tip_window = None;
4790 tip_frame = Qnil;
4795 /* Create a frame for a tooltip on the display described by DPYINFO.
4796 PARMS is a list of frame parameters. TEXT is the string to
4797 display in the tip frame. Value is the frame.
4799 Note that functions called here, esp. x_default_parameter can
4800 signal errors, for instance when a specified color name is
4801 undefined. We have to make sure that we're in a consistent state
4802 when this happens. */
4804 static Lisp_Object
4805 x_create_tip_frame (struct x_display_info *dpyinfo,
4806 Lisp_Object parms,
4807 Lisp_Object text)
4809 struct frame *f;
4810 Lisp_Object frame;
4811 Lisp_Object name;
4812 int width, height;
4813 ptrdiff_t count = SPECPDL_INDEX ();
4814 struct gcpro gcpro1, gcpro2, gcpro3;
4815 int face_change_count_before = face_change_count;
4816 Lisp_Object buffer;
4817 struct buffer *old_buffer;
4819 if (!dpyinfo->terminal->name)
4820 error ("Terminal is not live, can't create new frames on it");
4822 parms = Fcopy_alist (parms);
4824 /* Get the name of the frame to use for resource lookup. */
4825 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
4826 if (!STRINGP (name)
4827 && !EQ (name, Qunbound)
4828 && !NILP (name))
4829 error ("Invalid frame name--not a string or nil");
4831 frame = Qnil;
4832 GCPRO3 (parms, name, frame);
4833 f = make_frame (1);
4834 XSETFRAME (frame, f);
4836 buffer = Fget_buffer_create (build_string (" *tip*"));
4837 /* Use set_window_buffer instead of Fset_window_buffer (see
4838 discussion of bug#11984, bug#12025, bug#12026). */
4839 set_window_buffer (FRAME_ROOT_WINDOW (f), buffer, 0, 0);
4840 old_buffer = current_buffer;
4841 set_buffer_internal_1 (XBUFFER (buffer));
4842 bset_truncate_lines (current_buffer, Qnil);
4843 specbind (Qinhibit_read_only, Qt);
4844 specbind (Qinhibit_modification_hooks, Qt);
4845 Ferase_buffer ();
4846 Finsert (1, &text);
4847 set_buffer_internal_1 (old_buffer);
4849 record_unwind_protect (unwind_create_tip_frame, frame);
4851 f->terminal = dpyinfo->terminal;
4853 /* By setting the output method, we're essentially saying that
4854 the frame is live, as per FRAME_LIVE_P. If we get a signal
4855 from this point on, x_destroy_window might screw up reference
4856 counts etc. */
4857 f->output_method = output_x_window;
4858 f->output_data.x = xzalloc (sizeof *f->output_data.x);
4859 f->output_data.x->icon_bitmap = -1;
4860 FRAME_FONTSET (f) = -1;
4861 f->output_data.x->scroll_bar_foreground_pixel = -1;
4862 f->output_data.x->scroll_bar_background_pixel = -1;
4863 #ifdef USE_TOOLKIT_SCROLL_BARS
4864 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
4865 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
4866 #endif /* USE_TOOLKIT_SCROLL_BARS */
4867 f->output_data.x->white_relief.pixel = -1;
4868 f->output_data.x->black_relief.pixel = -1;
4870 fset_icon_name (f, Qnil);
4871 FRAME_DISPLAY_INFO (f) = dpyinfo;
4872 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
4873 f->output_data.x->explicit_parent = 0;
4875 /* These colors will be set anyway later, but it's important
4876 to get the color reference counts right, so initialize them! */
4878 Lisp_Object black;
4879 struct gcpro gcpro1;
4881 /* Function x_decode_color can signal an error. Make
4882 sure to initialize color slots so that we won't try
4883 to free colors we haven't allocated. */
4884 FRAME_FOREGROUND_PIXEL (f) = -1;
4885 FRAME_BACKGROUND_PIXEL (f) = -1;
4886 f->output_data.x->cursor_pixel = -1;
4887 f->output_data.x->cursor_foreground_pixel = -1;
4888 f->output_data.x->border_pixel = -1;
4889 f->output_data.x->mouse_pixel = -1;
4891 black = build_string ("black");
4892 GCPRO1 (black);
4893 FRAME_FOREGROUND_PIXEL (f)
4894 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4895 FRAME_BACKGROUND_PIXEL (f)
4896 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4897 f->output_data.x->cursor_pixel
4898 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4899 f->output_data.x->cursor_foreground_pixel
4900 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4901 f->output_data.x->border_pixel
4902 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4903 f->output_data.x->mouse_pixel
4904 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4905 UNGCPRO;
4908 /* Set the name; the functions to which we pass f expect the name to
4909 be set. */
4910 if (EQ (name, Qunbound) || NILP (name))
4912 fset_name (f, build_string (dpyinfo->x_id_name));
4913 f->explicit_name = 0;
4915 else
4917 fset_name (f, name);
4918 f->explicit_name = 1;
4919 /* use the frame's title when getting resources for this frame. */
4920 specbind (Qx_resource_name, name);
4923 register_font_driver (&xfont_driver, f);
4924 #ifdef HAVE_FREETYPE
4925 #ifdef HAVE_XFT
4926 register_font_driver (&xftfont_driver, f);
4927 #else /* not HAVE_XFT */
4928 register_font_driver (&ftxfont_driver, f);
4929 #endif /* not HAVE_XFT */
4930 #endif /* HAVE_FREETYPE */
4932 x_default_parameter (f, parms, Qfont_backend, Qnil,
4933 "fontBackend", "FontBackend", RES_TYPE_STRING);
4935 /* Extract the window parameters from the supplied values that are
4936 needed to determine window geometry. */
4937 x_default_font_parameter (f, parms);
4939 x_default_parameter (f, parms, Qborder_width, make_number (0),
4940 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4942 /* This defaults to 2 in order to match xterm. We recognize either
4943 internalBorderWidth or internalBorder (which is what xterm calls
4944 it). */
4945 if (NILP (Fassq (Qinternal_border_width, parms)))
4947 Lisp_Object value;
4949 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
4950 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
4951 if (! EQ (value, Qunbound))
4952 parms = Fcons (Fcons (Qinternal_border_width, value),
4953 parms);
4956 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
4957 "internalBorderWidth", "internalBorderWidth",
4958 RES_TYPE_NUMBER);
4959 x_default_parameter (f, parms, Qright_divider_width, make_number (0),
4960 NULL, NULL, RES_TYPE_NUMBER);
4961 x_default_parameter (f, parms, Qbottom_divider_width, make_number (0),
4962 NULL, NULL, RES_TYPE_NUMBER);
4964 /* Also do the stuff which must be set before the window exists. */
4965 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
4966 "foreground", "Foreground", RES_TYPE_STRING);
4967 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
4968 "background", "Background", RES_TYPE_STRING);
4969 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
4970 "pointerColor", "Foreground", RES_TYPE_STRING);
4971 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
4972 "cursorColor", "Foreground", RES_TYPE_STRING);
4973 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
4974 "borderColor", "BorderColor", RES_TYPE_STRING);
4976 #ifdef GLYPH_DEBUG
4977 image_cache_refcount =
4978 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
4979 dpyinfo_refcount = dpyinfo->reference_count;
4980 #endif /* GLYPH_DEBUG */
4982 /* Init faces before x_default_parameter is called for scroll-bar
4983 parameters because that function calls x_set_scroll_bar_width,
4984 which calls change_frame_size, which calls Fset_window_buffer,
4985 which runs hooks, which call Fvertical_motion. At the end, we
4986 end up in init_iterator with a null face cache, which should not
4987 happen. */
4988 init_frame_faces (f);
4990 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
4992 x_figure_window_size (f, parms, 0);
4995 XSetWindowAttributes attrs;
4996 unsigned long mask;
4997 Atom type = FRAME_DISPLAY_INFO (f)->Xatom_net_window_type_tooltip;
4999 block_input ();
5000 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
5001 if (DoesSaveUnders (dpyinfo->screen))
5002 mask |= CWSaveUnder;
5004 /* Window managers look at the override-redirect flag to determine
5005 whether or net to give windows a decoration (Xlib spec, chapter
5006 3.2.8). */
5007 attrs.override_redirect = True;
5008 attrs.save_under = True;
5009 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
5010 /* Arrange for getting MapNotify and UnmapNotify events. */
5011 attrs.event_mask = StructureNotifyMask;
5012 tip_window
5013 = FRAME_X_WINDOW (f)
5014 = XCreateWindow (FRAME_X_DISPLAY (f),
5015 FRAME_DISPLAY_INFO (f)->root_window,
5016 /* x, y, width, height */
5017 0, 0, 1, 1,
5018 /* Border. */
5019 f->border_width,
5020 CopyFromParent, InputOutput, CopyFromParent,
5021 mask, &attrs);
5022 XChangeProperty (FRAME_X_DISPLAY (f), tip_window,
5023 FRAME_DISPLAY_INFO (f)->Xatom_net_window_type,
5024 XA_ATOM, 32, PropModeReplace,
5025 (unsigned char *)&type, 1);
5026 unblock_input ();
5029 x_make_gc (f);
5031 x_default_parameter (f, parms, Qauto_raise, Qnil,
5032 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5033 x_default_parameter (f, parms, Qauto_lower, Qnil,
5034 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5035 x_default_parameter (f, parms, Qcursor_type, Qbox,
5036 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5038 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
5039 Change will not be effected unless different from the current
5040 FRAME_LINES (f). */
5041 width = FRAME_COLS (f);
5042 height = FRAME_LINES (f);
5043 SET_FRAME_COLS (f, 0);
5044 FRAME_LINES (f) = 0;
5045 change_frame_size (f, width, height, 1, 0, 0, 0);
5047 /* Add `tooltip' frame parameter's default value. */
5048 if (NILP (Fframe_parameter (frame, Qtooltip)))
5049 Fmodify_frame_parameters (frame, list1 (Fcons (Qtooltip, Qt)));
5051 /* FIXME - can this be done in a similar way to normal frames?
5052 http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00641.html */
5054 /* Set the `display-type' frame parameter before setting up faces. */
5056 Lisp_Object disptype;
5058 if (FRAME_DISPLAY_INFO (f)->n_planes == 1)
5059 disptype = intern ("mono");
5060 else if (FRAME_DISPLAY_INFO (f)->visual->class == GrayScale
5061 || FRAME_DISPLAY_INFO (f)->visual->class == StaticGray)
5062 disptype = intern ("grayscale");
5063 else
5064 disptype = intern ("color");
5066 if (NILP (Fframe_parameter (frame, Qdisplay_type)))
5067 Fmodify_frame_parameters (frame, list1 (Fcons (Qdisplay_type, disptype)));
5070 /* Set up faces after all frame parameters are known. This call
5071 also merges in face attributes specified for new frames.
5073 Frame parameters may be changed if .Xdefaults contains
5074 specifications for the default font. For example, if there is an
5075 `Emacs.default.attributeBackground: pink', the `background-color'
5076 attribute of the frame get's set, which let's the internal border
5077 of the tooltip frame appear in pink. Prevent this. */
5079 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
5081 /* Set tip_frame here, so that */
5082 tip_frame = frame;
5083 call2 (Qface_set_after_frame_default, frame, Qnil);
5085 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
5086 Fmodify_frame_parameters (frame, list1 (Fcons (Qbackground_color, bg)));
5089 f->no_split = 1;
5091 UNGCPRO;
5093 /* Now that the frame will be official, it counts as a reference to
5094 its display and terminal. */
5095 FRAME_DISPLAY_INFO (f)->reference_count++;
5096 f->terminal->reference_count++;
5098 /* It is now ok to make the frame official even if we get an error
5099 below. And the frame needs to be on Vframe_list or making it
5100 visible won't work. */
5101 Vframe_list = Fcons (frame, Vframe_list);
5104 /* Setting attributes of faces of the tooltip frame from resources
5105 and similar will increment face_change_count, which leads to the
5106 clearing of all current matrices. Since this isn't necessary
5107 here, avoid it by resetting face_change_count to the value it
5108 had before we created the tip frame. */
5109 face_change_count = face_change_count_before;
5111 /* Discard the unwind_protect. */
5112 return unbind_to (count, frame);
5116 /* Compute where to display tip frame F. PARMS is the list of frame
5117 parameters for F. DX and DY are specified offsets from the current
5118 location of the mouse. WIDTH and HEIGHT are the width and height
5119 of the tooltip. Return coordinates relative to the root window of
5120 the display in *ROOT_X, and *ROOT_Y. */
5122 static void
5123 compute_tip_xy (struct frame *f, Lisp_Object parms, Lisp_Object dx, Lisp_Object dy, int width, int height, int *root_x, int *root_y)
5125 Lisp_Object left, top;
5126 int win_x, win_y;
5127 Window root, child;
5128 unsigned pmask;
5130 /* User-specified position? */
5131 left = Fcdr (Fassq (Qleft, parms));
5132 top = Fcdr (Fassq (Qtop, parms));
5134 /* Move the tooltip window where the mouse pointer is. Resize and
5135 show it. */
5136 if (!INTEGERP (left) || !INTEGERP (top))
5138 block_input ();
5139 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_DISPLAY_INFO (f)->root_window,
5140 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
5141 unblock_input ();
5144 if (INTEGERP (top))
5145 *root_y = XINT (top);
5146 else if (*root_y + XINT (dy) <= 0)
5147 *root_y = 0; /* Can happen for negative dy */
5148 else if (*root_y + XINT (dy) + height
5149 <= x_display_pixel_height (FRAME_DISPLAY_INFO (f)))
5150 /* It fits below the pointer */
5151 *root_y += XINT (dy);
5152 else if (height + XINT (dy) <= *root_y)
5153 /* It fits above the pointer. */
5154 *root_y -= height + XINT (dy);
5155 else
5156 /* Put it on the top. */
5157 *root_y = 0;
5159 if (INTEGERP (left))
5160 *root_x = XINT (left);
5161 else if (*root_x + XINT (dx) <= 0)
5162 *root_x = 0; /* Can happen for negative dx */
5163 else if (*root_x + XINT (dx) + width
5164 <= x_display_pixel_width (FRAME_DISPLAY_INFO (f)))
5165 /* It fits to the right of the pointer. */
5166 *root_x += XINT (dx);
5167 else if (width + XINT (dx) <= *root_x)
5168 /* It fits to the left of the pointer. */
5169 *root_x -= width + XINT (dx);
5170 else
5171 /* Put it left-justified on the screen--it ought to fit that way. */
5172 *root_x = 0;
5176 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
5177 doc: /* Show STRING in a "tooltip" window on frame FRAME.
5178 A tooltip window is a small X window displaying a string.
5180 This is an internal function; Lisp code should call `tooltip-show'.
5182 FRAME nil or omitted means use the selected frame.
5184 PARMS is an optional list of frame parameters which can be used to
5185 change the tooltip's appearance.
5187 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
5188 means use the default timeout of 5 seconds.
5190 If the list of frame parameters PARMS contains a `left' parameters,
5191 the tooltip is displayed at that x-position. Otherwise it is
5192 displayed at the mouse position, with offset DX added (default is 5 if
5193 DX isn't specified). Likewise for the y-position; if a `top' frame
5194 parameter is specified, it determines the y-position of the tooltip
5195 window, otherwise it is displayed at the mouse position, with offset
5196 DY added (default is -10).
5198 A tooltip's maximum size is specified by `x-max-tooltip-size'.
5199 Text larger than the specified size is clipped. */)
5200 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
5202 struct frame *f;
5203 struct window *w;
5204 int root_x, root_y;
5205 struct buffer *old_buffer;
5206 struct text_pos pos;
5207 int i, width, height, seen_reversed_p;
5208 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5209 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5210 ptrdiff_t count = SPECPDL_INDEX ();
5212 specbind (Qinhibit_redisplay, Qt);
5214 GCPRO4 (string, parms, frame, timeout);
5216 CHECK_STRING (string);
5217 if (SCHARS (string) == 0)
5218 string = make_unibyte_string (" ", 1);
5220 f = decode_window_system_frame (frame);
5221 if (NILP (timeout))
5222 timeout = make_number (5);
5223 else
5224 CHECK_NATNUM (timeout);
5226 if (NILP (dx))
5227 dx = make_number (5);
5228 else
5229 CHECK_NUMBER (dx);
5231 if (NILP (dy))
5232 dy = make_number (-10);
5233 else
5234 CHECK_NUMBER (dy);
5236 #ifdef USE_GTK
5237 if (x_gtk_use_system_tooltips)
5239 bool ok;
5241 /* Hide a previous tip, if any. */
5242 Fx_hide_tip ();
5244 block_input ();
5245 ok = xg_prepare_tooltip (f, string, &width, &height);
5246 if (ok)
5248 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5249 xg_show_tooltip (f, root_x, root_y);
5250 /* This is used in Fx_hide_tip. */
5251 XSETFRAME (tip_frame, f);
5253 unblock_input ();
5254 if (ok) goto start_timer;
5256 #endif /* USE_GTK */
5258 if (NILP (last_show_tip_args))
5259 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5261 if (!NILP (tip_frame))
5263 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5264 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5265 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5267 if (EQ (frame, last_frame)
5268 && !NILP (Fequal (last_string, string))
5269 && !NILP (Fequal (last_parms, parms)))
5271 struct frame *tip_f = XFRAME (tip_frame);
5273 /* Only DX and DY have changed. */
5274 if (!NILP (tip_timer))
5276 Lisp_Object timer = tip_timer;
5277 tip_timer = Qnil;
5278 call1 (Qcancel_timer, timer);
5281 block_input ();
5282 compute_tip_xy (tip_f, parms, dx, dy, FRAME_PIXEL_WIDTH (tip_f),
5283 FRAME_PIXEL_HEIGHT (tip_f), &root_x, &root_y);
5284 XMoveWindow (FRAME_X_DISPLAY (tip_f), FRAME_X_WINDOW (tip_f),
5285 root_x, root_y);
5286 unblock_input ();
5287 goto start_timer;
5291 /* Hide a previous tip, if any. */
5292 Fx_hide_tip ();
5294 ASET (last_show_tip_args, 0, string);
5295 ASET (last_show_tip_args, 1, frame);
5296 ASET (last_show_tip_args, 2, parms);
5298 /* Add default values to frame parameters. */
5299 if (NILP (Fassq (Qname, parms)))
5300 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5301 if (NILP (Fassq (Qinternal_border_width, parms)))
5302 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5303 if (NILP (Fassq (Qborder_width, parms)))
5304 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5305 if (NILP (Fassq (Qbottom_divider_width, parms)))
5306 parms = Fcons (Fcons (Qbottom_divider_width, make_number (0)), parms);
5307 if (NILP (Fassq (Qright_divider_width, parms)))
5308 parms = Fcons (Fcons (Qright_divider_width, make_number (0)), parms);
5309 if (NILP (Fassq (Qborder_color, parms)))
5310 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5311 if (NILP (Fassq (Qbackground_color, parms)))
5312 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5313 parms);
5315 /* Create a frame for the tooltip, and record it in the global
5316 variable tip_frame. */
5317 frame = x_create_tip_frame (FRAME_DISPLAY_INFO (f), parms, string);
5318 f = XFRAME (frame);
5320 /* Set up the frame's root window. */
5321 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5322 w->left_col = 0;
5323 w->top_line = 0;
5324 w->pixel_left = 0;
5325 w->pixel_top = 0;
5327 if (CONSP (Vx_max_tooltip_size)
5328 && RANGED_INTEGERP (1, XCAR (Vx_max_tooltip_size), INT_MAX)
5329 && RANGED_INTEGERP (1, XCDR (Vx_max_tooltip_size), INT_MAX))
5331 w->total_cols = XFASTINT (XCAR (Vx_max_tooltip_size));
5332 w->total_lines = XFASTINT (XCDR (Vx_max_tooltip_size));
5334 else
5336 w->total_cols = 80;
5337 w->total_lines = 40;
5340 w->pixel_width = w->total_cols * FRAME_COLUMN_WIDTH (f);
5341 w->pixel_height = w->total_lines * FRAME_LINE_HEIGHT (f);
5343 FRAME_TOTAL_COLS (f) = w->total_cols;
5344 adjust_frame_glyphs (f);
5345 w->pseudo_window_p = 1;
5347 /* Display the tooltip text in a temporary buffer. */
5348 old_buffer = current_buffer;
5349 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->contents));
5350 bset_truncate_lines (current_buffer, Qnil);
5351 clear_glyph_matrix (w->desired_matrix);
5352 clear_glyph_matrix (w->current_matrix);
5353 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5354 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
5356 /* Compute width and height of the tooltip. */
5357 width = height = seen_reversed_p = 0;
5358 for (i = 0; i < w->desired_matrix->nrows; ++i)
5360 struct glyph_row *row = &w->desired_matrix->rows[i];
5361 struct glyph *last;
5362 int row_width;
5364 /* Stop at the first empty row at the end. */
5365 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5366 break;
5368 /* Let the row go over the full width of the frame. */
5369 row->full_width_p = 1;
5371 row_width = row->pixel_width;
5372 if (row->used[TEXT_AREA])
5374 /* There's a glyph at the end of rows that is used to place
5375 the cursor there. Don't include the width of this glyph. */
5376 if (!row->reversed_p)
5378 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5379 if (INTEGERP (last->object))
5380 row_width -= last->pixel_width;
5382 else
5384 /* There could be a stretch glyph at the beginning of R2L
5385 rows that is produced by extend_face_to_end_of_line.
5386 Don't count that glyph. */
5387 struct glyph *g = row->glyphs[TEXT_AREA];
5389 if (g->type == STRETCH_GLYPH && INTEGERP (g->object))
5391 row_width -= g->pixel_width;
5392 seen_reversed_p = 1;
5397 height += row->height;
5398 width = max (width, row_width);
5401 /* If we've seen partial-length R2L rows, we need to re-adjust the
5402 tool-tip frame width and redisplay it again, to avoid over-wide
5403 tips due to the stretch glyph that extends R2L lines to full
5404 width of the frame. */
5405 if (seen_reversed_p)
5407 /* w->total_cols and FRAME_TOTAL_COLS want the width in columns,
5408 not in pixels. */
5409 w->pixel_width = width;
5410 width /= WINDOW_FRAME_COLUMN_WIDTH (w);
5411 w->total_cols = width;
5412 FRAME_TOTAL_COLS (f) = width;
5413 SET_FRAME_WIDTH (f, width);
5414 adjust_frame_glyphs (f);
5415 clear_glyph_matrix (w->desired_matrix);
5416 clear_glyph_matrix (w->current_matrix);
5417 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
5418 width = height = 0;
5419 /* Recompute width and height of the tooltip. */
5420 for (i = 0; i < w->desired_matrix->nrows; ++i)
5422 struct glyph_row *row = &w->desired_matrix->rows[i];
5423 struct glyph *last;
5424 int row_width;
5426 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5427 break;
5428 row->full_width_p = 1;
5429 row_width = row->pixel_width;
5430 if (row->used[TEXT_AREA] && !row->reversed_p)
5432 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5433 if (INTEGERP (last->object))
5434 row_width -= last->pixel_width;
5437 height += row->height;
5438 width = max (width, row_width);
5442 /* Add the frame's internal border to the width and height the X
5443 window should have. */
5444 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5445 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5447 /* Move the tooltip window where the mouse pointer is. Resize and
5448 show it. */
5449 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5451 block_input ();
5452 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5453 root_x, root_y, width, height);
5454 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5455 unblock_input ();
5457 /* Draw into the window. */
5458 w->must_be_updated_p = 1;
5459 update_single_window (w, 1);
5461 /* Restore original current buffer. */
5462 set_buffer_internal_1 (old_buffer);
5463 windows_or_buffers_changed = old_windows_or_buffers_changed;
5465 start_timer:
5466 /* Let the tip disappear after timeout seconds. */
5467 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5468 intern ("x-hide-tip"));
5470 UNGCPRO;
5471 return unbind_to (count, Qnil);
5475 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5476 doc: /* Hide the current tooltip window, if there is any.
5477 Value is t if tooltip was open, nil otherwise. */)
5478 (void)
5480 ptrdiff_t count;
5481 Lisp_Object deleted, frame, timer;
5482 struct gcpro gcpro1, gcpro2;
5484 /* Return quickly if nothing to do. */
5485 if (NILP (tip_timer) && NILP (tip_frame))
5486 return Qnil;
5488 frame = tip_frame;
5489 timer = tip_timer;
5490 GCPRO2 (frame, timer);
5491 tip_frame = tip_timer = deleted = Qnil;
5493 count = SPECPDL_INDEX ();
5494 specbind (Qinhibit_redisplay, Qt);
5495 specbind (Qinhibit_quit, Qt);
5497 if (!NILP (timer))
5498 call1 (Qcancel_timer, timer);
5500 #ifdef USE_GTK
5502 /* When using system tooltip, tip_frame is the Emacs frame on which
5503 the tip is shown. */
5504 struct frame *f = XFRAME (frame);
5505 if (FRAME_LIVE_P (f) && xg_hide_tooltip (f))
5506 frame = Qnil;
5508 #endif
5510 if (FRAMEP (frame))
5512 delete_frame (frame, Qnil);
5513 deleted = Qt;
5515 #ifdef USE_LUCID
5516 /* Bloodcurdling hack alert: The Lucid menu bar widget's
5517 redisplay procedure is not called when a tip frame over menu
5518 items is unmapped. Redisplay the menu manually... */
5520 Widget w;
5521 struct frame *f = SELECTED_FRAME ();
5522 w = f->output_data.x->menubar_widget;
5524 if (!DoesSaveUnders (FRAME_DISPLAY_INFO (f)->screen)
5525 && w != NULL)
5527 block_input ();
5528 xlwmenu_redisplay (w);
5529 unblock_input ();
5532 #endif /* USE_LUCID */
5535 UNGCPRO;
5536 return unbind_to (count, deleted);
5541 /***********************************************************************
5542 File selection dialog
5543 ***********************************************************************/
5545 DEFUN ("x-uses-old-gtk-dialog", Fx_uses_old_gtk_dialog,
5546 Sx_uses_old_gtk_dialog,
5547 0, 0, 0,
5548 doc: /* Return t if the old Gtk+ file selection dialog is used. */)
5549 (void)
5551 #ifdef USE_GTK
5552 if (use_dialog_box
5553 && use_file_dialog
5554 && window_system_available (SELECTED_FRAME ())
5555 && xg_uses_old_file_dialog ())
5556 return Qt;
5557 #endif
5558 return Qnil;
5562 #ifdef USE_MOTIF
5563 /* Callback for "OK" and "Cancel" on file selection dialog. */
5565 static void
5566 file_dialog_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5568 int *result = client_data;
5569 XmAnyCallbackStruct *cb = call_data;
5570 *result = cb->reason;
5574 /* Callback for unmapping a file selection dialog. This is used to
5575 capture the case where a dialog is closed via a window manager's
5576 closer button, for example. Using a XmNdestroyCallback didn't work
5577 in this case. */
5579 static void
5580 file_dialog_unmap_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5582 int *result = client_data;
5583 *result = XmCR_CANCEL;
5586 static void
5587 clean_up_file_dialog (void *arg)
5589 Widget dialog = arg;
5591 /* Clean up. */
5592 block_input ();
5593 XtUnmanageChild (dialog);
5594 XtDestroyWidget (dialog);
5595 x_menu_set_in_use (0);
5596 unblock_input ();
5600 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5601 doc: /* Read file name, prompting with PROMPT in directory DIR.
5602 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5603 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5604 or directory must exist.
5606 This function is only defined on NS, MS Windows, and X Windows with the
5607 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5608 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories. */)
5609 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename,
5610 Lisp_Object mustmatch, Lisp_Object only_dir_p)
5612 int result;
5613 struct frame *f = SELECTED_FRAME ();
5614 Lisp_Object file = Qnil;
5615 Lisp_Object decoded_file;
5616 Widget dialog, text, help;
5617 Arg al[10];
5618 int ac = 0;
5619 XmString dir_xmstring, pattern_xmstring;
5620 ptrdiff_t count = SPECPDL_INDEX ();
5621 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5623 check_window_system (f);
5625 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5627 if (popup_activated ())
5628 error ("Trying to use a menu from within a menu-entry");
5630 CHECK_STRING (prompt);
5631 CHECK_STRING (dir);
5633 /* Prevent redisplay. */
5634 specbind (Qinhibit_redisplay, Qt);
5636 block_input ();
5638 /* Create the dialog with PROMPT as title, using DIR as initial
5639 directory and using "*" as pattern. */
5640 dir = Fexpand_file_name (dir, Qnil);
5641 dir_xmstring = XmStringCreateLocalized (SSDATA (dir));
5642 pattern_xmstring = XmStringCreateLocalized ("*");
5644 XtSetArg (al[ac], XmNtitle, SDATA (prompt)); ++ac;
5645 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
5646 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
5647 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
5648 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
5649 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
5650 "fsb", al, ac);
5651 XmStringFree (dir_xmstring);
5652 XmStringFree (pattern_xmstring);
5654 /* Add callbacks for OK and Cancel. */
5655 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
5656 (XtPointer) &result);
5657 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
5658 (XtPointer) &result);
5659 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
5660 (XtPointer) &result);
5662 /* Remove the help button since we can't display help. */
5663 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
5664 XtUnmanageChild (help);
5666 /* Mark OK button as default. */
5667 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
5668 XmNshowAsDefault, True, NULL);
5670 /* If MUSTMATCH is non-nil, disable the file entry field of the
5671 dialog, so that the user must select a file from the files list
5672 box. We can't remove it because we wouldn't have a way to get at
5673 the result file name, then. */
5674 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5675 if (!NILP (mustmatch))
5677 Widget label;
5678 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
5679 XtSetSensitive (text, False);
5680 XtSetSensitive (label, False);
5683 /* Manage the dialog, so that list boxes get filled. */
5684 XtManageChild (dialog);
5686 if (STRINGP (default_filename))
5688 XmString default_xmstring;
5689 Widget wtext = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5690 Widget list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
5692 XmTextPosition last_pos = XmTextFieldGetLastPosition (wtext);
5693 XmTextFieldReplace (wtext, 0, last_pos,
5694 (SSDATA (Ffile_name_nondirectory (default_filename))));
5696 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
5697 must include the path for this to work. */
5699 default_xmstring = XmStringCreateLocalized (SSDATA (default_filename));
5701 if (XmListItemExists (list, default_xmstring))
5703 int item_pos = XmListItemPos (list, default_xmstring);
5704 /* Select the item and scroll it into view. */
5705 XmListSelectPos (list, item_pos, True);
5706 XmListSetPos (list, item_pos);
5709 XmStringFree (default_xmstring);
5712 record_unwind_protect_ptr (clean_up_file_dialog, dialog);
5714 /* Process events until the user presses Cancel or OK. */
5715 x_menu_set_in_use (1);
5716 result = 0;
5717 while (result == 0)
5719 XEvent event;
5720 x_menu_wait_for_event (0);
5721 XtAppNextEvent (Xt_app_con, &event);
5722 if (event.type == KeyPress
5723 && FRAME_X_DISPLAY (f) == event.xkey.display)
5725 KeySym keysym = XLookupKeysym (&event.xkey, 0);
5727 /* Pop down on C-g. */
5728 if (keysym == XK_g && (event.xkey.state & ControlMask) != 0)
5729 XtUnmanageChild (dialog);
5732 (void) x_dispatch_event (&event, FRAME_X_DISPLAY (f));
5735 /* Get the result. */
5736 if (result == XmCR_OK)
5738 XmString text_string;
5739 String data;
5741 XtVaGetValues (dialog, XmNtextString, &text_string, NULL);
5742 XmStringGetLtoR (text_string, XmFONTLIST_DEFAULT_TAG, &data);
5743 XmStringFree (text_string);
5744 file = build_string (data);
5745 XtFree (data);
5747 else
5748 file = Qnil;
5750 unblock_input ();
5751 UNGCPRO;
5753 /* Make "Cancel" equivalent to C-g. */
5754 if (NILP (file))
5755 Fsignal (Qquit, Qnil);
5757 decoded_file = DECODE_FILE (file);
5759 return unbind_to (count, decoded_file);
5762 #endif /* USE_MOTIF */
5764 #ifdef USE_GTK
5766 static void
5767 clean_up_dialog (void)
5769 x_menu_set_in_use (0);
5772 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5773 doc: /* Read file name, prompting with PROMPT in directory DIR.
5774 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5775 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5776 or directory must exist.
5778 This function is only defined on NS, MS Windows, and X Windows with the
5779 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5780 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories. */)
5781 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
5783 struct frame *f = SELECTED_FRAME ();
5784 char *fn;
5785 Lisp_Object file = Qnil;
5786 Lisp_Object decoded_file;
5787 ptrdiff_t count = SPECPDL_INDEX ();
5788 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5789 char *cdef_file;
5791 check_window_system (f);
5793 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5795 if (popup_activated ())
5796 error ("Trying to use a menu from within a menu-entry");
5798 CHECK_STRING (prompt);
5799 CHECK_STRING (dir);
5801 /* Prevent redisplay. */
5802 specbind (Qinhibit_redisplay, Qt);
5803 record_unwind_protect_void (clean_up_dialog);
5805 block_input ();
5807 if (STRINGP (default_filename))
5808 cdef_file = SSDATA (default_filename);
5809 else
5810 cdef_file = SSDATA (dir);
5812 fn = xg_get_file_name (f, SSDATA (prompt), cdef_file,
5813 ! NILP (mustmatch),
5814 ! NILP (only_dir_p));
5816 if (fn)
5818 file = build_string (fn);
5819 xfree (fn);
5822 unblock_input ();
5823 UNGCPRO;
5825 /* Make "Cancel" equivalent to C-g. */
5826 if (NILP (file))
5827 Fsignal (Qquit, Qnil);
5829 decoded_file = DECODE_FILE (file);
5831 return unbind_to (count, decoded_file);
5835 #ifdef HAVE_FREETYPE
5837 DEFUN ("x-select-font", Fx_select_font, Sx_select_font, 0, 2, 0,
5838 doc: /* Read a font using a GTK dialog.
5839 Return either a font spec (for GTK versions >= 3.2) or a string
5840 containing a GTK-style font name.
5842 FRAME is the frame on which to pop up the font chooser. If omitted or
5843 nil, it defaults to the selected frame. */)
5844 (Lisp_Object frame, Lisp_Object ignored)
5846 struct frame *f = decode_window_system_frame (frame);
5847 Lisp_Object font;
5848 Lisp_Object font_param;
5849 char *default_name = NULL;
5850 struct gcpro gcpro1, gcpro2;
5851 ptrdiff_t count = SPECPDL_INDEX ();
5853 if (popup_activated ())
5854 error ("Trying to use a menu from within a menu-entry");
5856 /* Prevent redisplay. */
5857 specbind (Qinhibit_redisplay, Qt);
5858 record_unwind_protect_void (clean_up_dialog);
5860 block_input ();
5862 GCPRO2 (font_param, font);
5864 XSETFONT (font, FRAME_FONT (f));
5865 font_param = Ffont_get (font, intern (":name"));
5866 if (STRINGP (font_param))
5867 default_name = xstrdup (SSDATA (font_param));
5868 else
5870 font_param = Fframe_parameter (frame, Qfont_param);
5871 if (STRINGP (font_param))
5872 default_name = xstrdup (SSDATA (font_param));
5875 font = xg_get_font (f, default_name);
5876 xfree (default_name);
5878 unblock_input ();
5880 if (NILP (font))
5881 Fsignal (Qquit, Qnil);
5883 return unbind_to (count, font);
5885 #endif /* HAVE_FREETYPE */
5887 #endif /* USE_GTK */
5890 /***********************************************************************
5891 Keyboard
5892 ***********************************************************************/
5894 #ifdef HAVE_XKB
5895 #include <X11/XKBlib.h>
5896 #include <X11/keysym.h>
5897 #endif
5899 DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
5900 Sx_backspace_delete_keys_p, 0, 1, 0,
5901 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
5902 FRAME nil means use the selected frame.
5903 Value is t if we know that both keys are present, and are mapped to the
5904 usual X keysyms. Value is `lambda' if we cannot determine if both keys are
5905 present and mapped to the usual X keysyms. */)
5906 (Lisp_Object frame)
5908 #ifndef HAVE_XKB
5909 return Qlambda;
5910 #else
5911 XkbDescPtr kb;
5912 struct frame *f = decode_window_system_frame (frame);
5913 Display *dpy = FRAME_X_DISPLAY (f);
5914 Lisp_Object have_keys;
5915 int major, minor, op, event, error_code;
5917 block_input ();
5919 /* Check library version in case we're dynamically linked. */
5920 major = XkbMajorVersion;
5921 minor = XkbMinorVersion;
5922 if (!XkbLibraryVersion (&major, &minor))
5924 unblock_input ();
5925 return Qlambda;
5928 /* Check that the server supports XKB. */
5929 major = XkbMajorVersion;
5930 minor = XkbMinorVersion;
5931 if (!XkbQueryExtension (dpy, &op, &event, &error_code, &major, &minor))
5933 unblock_input ();
5934 return Qlambda;
5937 /* In this code we check that the keyboard has physical keys with names
5938 that start with BKSP (Backspace) and DELE (Delete), and that they
5939 generate keysym XK_BackSpace and XK_Delete respectively.
5940 This function is used to test if normal-erase-is-backspace should be
5941 turned on.
5942 An alternative approach would be to just check if XK_BackSpace and
5943 XK_Delete are mapped to any key. But if any of those are mapped to
5944 some non-intuitive key combination (Meta-Shift-Ctrl-whatever) and the
5945 user doesn't know about it, it is better to return false here.
5946 It is more obvious to the user what to do if she/he has two keys
5947 clearly marked with names/symbols and one key does something not
5948 expected (i.e. she/he then tries the other).
5949 The cases where Backspace/Delete is mapped to some other key combination
5950 are rare, and in those cases, normal-erase-is-backspace can be turned on
5951 manually. */
5953 have_keys = Qnil;
5954 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
5955 if (kb)
5957 int delete_keycode = 0, backspace_keycode = 0, i;
5959 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
5961 for (i = kb->min_key_code;
5962 (i < kb->max_key_code
5963 && (delete_keycode == 0 || backspace_keycode == 0));
5964 ++i)
5966 /* The XKB symbolic key names can be seen most easily in
5967 the PS file generated by `xkbprint -label name
5968 $DISPLAY'. */
5969 if (memcmp ("DELE", kb->names->keys[i].name, 4) == 0)
5970 delete_keycode = i;
5971 else if (memcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
5972 backspace_keycode = i;
5975 XkbFreeNames (kb, 0, True);
5978 XkbFreeClientMap (kb, 0, True);
5980 if (delete_keycode
5981 && backspace_keycode
5982 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
5983 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
5984 have_keys = Qt;
5986 unblock_input ();
5987 return have_keys;
5988 #endif
5993 /***********************************************************************
5994 Initialization
5995 ***********************************************************************/
5997 /* Keep this list in the same order as frame_parms in frame.c.
5998 Use 0 for unsupported frame parameters. */
6000 frame_parm_handler x_frame_parm_handlers[] =
6002 x_set_autoraise,
6003 x_set_autolower,
6004 x_set_background_color,
6005 x_set_border_color,
6006 x_set_border_width,
6007 x_set_cursor_color,
6008 x_set_cursor_type,
6009 x_set_font,
6010 x_set_foreground_color,
6011 x_set_icon_name,
6012 x_set_icon_type,
6013 x_set_internal_border_width,
6014 x_set_right_divider_width,
6015 x_set_bottom_divider_width,
6016 x_set_menu_bar_lines,
6017 x_set_mouse_color,
6018 x_explicitly_set_name,
6019 x_set_scroll_bar_width,
6020 x_set_title,
6021 x_set_unsplittable,
6022 x_set_vertical_scroll_bars,
6023 x_set_visibility,
6024 x_set_tool_bar_lines,
6025 x_set_scroll_bar_foreground,
6026 x_set_scroll_bar_background,
6027 x_set_screen_gamma,
6028 x_set_line_spacing,
6029 x_set_fringe_width,
6030 x_set_fringe_width,
6031 x_set_wait_for_wm,
6032 x_set_fullscreen,
6033 x_set_font_backend,
6034 x_set_alpha,
6035 x_set_sticky,
6036 x_set_tool_bar_position,
6039 void
6040 syms_of_xfns (void)
6042 /* The section below is built by the lisp expression at the top of the file,
6043 just above where these variables are declared. */
6044 /*&&& init symbols here &&&*/
6045 DEFSYM (Qsuppress_icon, "suppress-icon");
6046 DEFSYM (Qundefined_color, "undefined-color");
6047 DEFSYM (Qcompound_text, "compound-text");
6048 DEFSYM (Qcancel_timer, "cancel-timer");
6049 DEFSYM (Qfont_param, "font-parameter");
6050 /* This is the end of symbol initialization. */
6052 Fput (Qundefined_color, Qerror_conditions,
6053 listn (CONSTYPE_PURE, 2, Qundefined_color, Qerror));
6054 Fput (Qundefined_color, Qerror_message,
6055 build_pure_c_string ("Undefined color"));
6057 DEFVAR_LISP ("x-pointer-shape", Vx_pointer_shape,
6058 doc: /* The shape of the pointer when over text.
6059 Changing the value does not affect existing frames
6060 unless you set the mouse color. */);
6061 Vx_pointer_shape = Qnil;
6063 #if 0 /* This doesn't really do anything. */
6064 DEFVAR_LISP ("x-nontext-pointer-shape", Vx_nontext_pointer_shape,
6065 doc: /* The shape of the pointer when not over text.
6066 This variable takes effect when you create a new frame
6067 or when you set the mouse color. */);
6068 #endif
6069 Vx_nontext_pointer_shape = Qnil;
6071 DEFVAR_LISP ("x-hourglass-pointer-shape", Vx_hourglass_pointer_shape,
6072 doc: /* The shape of the pointer when Emacs is busy.
6073 This variable takes effect when you create a new frame
6074 or when you set the mouse color. */);
6075 Vx_hourglass_pointer_shape = Qnil;
6077 #if 0 /* This doesn't really do anything. */
6078 DEFVAR_LISP ("x-mode-pointer-shape", Vx_mode_pointer_shape,
6079 doc: /* The shape of the pointer when over the mode line.
6080 This variable takes effect when you create a new frame
6081 or when you set the mouse color. */);
6082 #endif
6083 Vx_mode_pointer_shape = Qnil;
6085 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
6086 Vx_sensitive_text_pointer_shape,
6087 doc: /* The shape of the pointer when over mouse-sensitive text.
6088 This variable takes effect when you create a new frame
6089 or when you set the mouse color. */);
6090 Vx_sensitive_text_pointer_shape = Qnil;
6092 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
6093 Vx_window_horizontal_drag_shape,
6094 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
6095 This variable takes effect when you create a new frame
6096 or when you set the mouse color. */);
6097 Vx_window_horizontal_drag_shape = Qnil;
6099 DEFVAR_LISP ("x-window-vertical-drag-cursor",
6100 Vx_window_vertical_drag_shape,
6101 doc: /* Pointer shape to use for indicating a window can be dragged vertically.
6102 This variable takes effect when you create a new frame
6103 or when you set the mouse color. */);
6104 Vx_window_vertical_drag_shape = Qnil;
6106 DEFVAR_LISP ("x-cursor-fore-pixel", Vx_cursor_fore_pixel,
6107 doc: /* A string indicating the foreground color of the cursor box. */);
6108 Vx_cursor_fore_pixel = Qnil;
6110 DEFVAR_LISP ("x-max-tooltip-size", Vx_max_tooltip_size,
6111 doc: /* Maximum size for tooltips.
6112 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
6113 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
6115 DEFVAR_LISP ("x-no-window-manager", Vx_no_window_manager,
6116 doc: /* Non-nil if no X window manager is in use.
6117 Emacs doesn't try to figure this out; this is always nil
6118 unless you set it to something else. */);
6119 /* We don't have any way to find this out, so set it to nil
6120 and maybe the user would like to set it to t. */
6121 Vx_no_window_manager = Qnil;
6123 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
6124 Vx_pixel_size_width_font_regexp,
6125 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
6127 Since Emacs gets width of a font matching with this regexp from
6128 PIXEL_SIZE field of the name, font finding mechanism gets faster for
6129 such a font. This is especially effective for such large fonts as
6130 Chinese, Japanese, and Korean. */);
6131 Vx_pixel_size_width_font_regexp = Qnil;
6133 /* This is not ifdef:ed, so other builds than GTK can customize it. */
6134 DEFVAR_BOOL ("x-gtk-use-old-file-dialog", x_gtk_use_old_file_dialog,
6135 doc: /* Non-nil means prompt with the old GTK file selection dialog.
6136 If nil or if the file selection dialog is not available, the new GTK file
6137 chooser is used instead. To turn off all file dialogs set the
6138 variable `use-file-dialog'. */);
6139 x_gtk_use_old_file_dialog = 0;
6141 DEFVAR_BOOL ("x-gtk-show-hidden-files", x_gtk_show_hidden_files,
6142 doc: /* If non-nil, the GTK file chooser will by default show hidden files.
6143 Note that this is just the default, there is a toggle button on the file
6144 chooser to show or not show hidden files on a case by case basis. */);
6145 x_gtk_show_hidden_files = 0;
6147 DEFVAR_BOOL ("x-gtk-file-dialog-help-text", x_gtk_file_dialog_help_text,
6148 doc: /* If non-nil, the GTK file chooser will show additional help text.
6149 If more space for files in the file chooser dialog is wanted, set this to nil
6150 to turn the additional text off. */);
6151 x_gtk_file_dialog_help_text = 1;
6153 DEFVAR_BOOL ("x-gtk-whole-detached-tool-bar", x_gtk_whole_detached_tool_bar,
6154 doc: /* If non-nil, a detached tool bar is shown in full.
6155 The default is to just show an arrow and pressing on that arrow shows
6156 the tool bar buttons. */);
6157 x_gtk_whole_detached_tool_bar = 0;
6159 DEFVAR_BOOL ("x-gtk-use-system-tooltips", x_gtk_use_system_tooltips,
6160 doc: /* If non-nil with a Gtk+ built Emacs, the Gtk+ tooltip is used.
6161 Otherwise use Emacs own tooltip implementation.
6162 When using Gtk+ tooltips, the tooltip face is not used. */);
6163 x_gtk_use_system_tooltips = 1;
6165 /* Tell Emacs about this window system. */
6166 Fprovide (Qx, Qnil);
6168 #ifdef USE_X_TOOLKIT
6169 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6170 #ifdef USE_MOTIF
6171 Fprovide (intern_c_string ("motif"), Qnil);
6173 DEFVAR_LISP ("motif-version-string", Vmotif_version_string,
6174 doc: /* Version info for LessTif/Motif. */);
6175 Vmotif_version_string = build_string (XmVERSION_STRING);
6176 #endif /* USE_MOTIF */
6177 #endif /* USE_X_TOOLKIT */
6179 #ifdef USE_GTK
6180 /* Provide x-toolkit also for GTK. Internally GTK does not use Xt so it
6181 is not an X toolkit in that sense (USE_X_TOOLKIT is not defined).
6182 But for a user it is a toolkit for X, and indeed, configure
6183 accepts --with-x-toolkit=gtk. */
6184 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6185 Fprovide (intern_c_string ("gtk"), Qnil);
6186 Fprovide (intern_c_string ("move-toolbar"), Qnil);
6188 DEFVAR_LISP ("gtk-version-string", Vgtk_version_string,
6189 doc: /* Version info for GTK+. */);
6191 char gtk_version[sizeof ".." + 3 * INT_STRLEN_BOUND (int)];
6192 int len = sprintf (gtk_version, "%d.%d.%d",
6193 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
6194 Vgtk_version_string = make_pure_string (gtk_version, len, len, 0);
6196 #endif /* USE_GTK */
6198 /* X window properties. */
6199 defsubr (&Sx_change_window_property);
6200 defsubr (&Sx_delete_window_property);
6201 defsubr (&Sx_window_property);
6203 defsubr (&Sxw_display_color_p);
6204 defsubr (&Sx_display_grayscale_p);
6205 defsubr (&Sxw_color_defined_p);
6206 defsubr (&Sxw_color_values);
6207 defsubr (&Sx_server_max_request_size);
6208 defsubr (&Sx_server_vendor);
6209 defsubr (&Sx_server_version);
6210 defsubr (&Sx_display_pixel_width);
6211 defsubr (&Sx_display_pixel_height);
6212 defsubr (&Sx_display_mm_width);
6213 defsubr (&Sx_display_mm_height);
6214 defsubr (&Sx_display_screens);
6215 defsubr (&Sx_display_planes);
6216 defsubr (&Sx_display_color_cells);
6217 defsubr (&Sx_display_visual_class);
6218 defsubr (&Sx_display_backing_store);
6219 defsubr (&Sx_display_save_under);
6220 defsubr (&Sx_display_monitor_attributes_list);
6221 defsubr (&Sx_wm_set_size_hint);
6222 defsubr (&Sx_create_frame);
6223 defsubr (&Sx_open_connection);
6224 defsubr (&Sx_close_connection);
6225 defsubr (&Sx_display_list);
6226 defsubr (&Sx_synchronize);
6227 defsubr (&Sx_backspace_delete_keys_p);
6229 defsubr (&Sx_show_tip);
6230 defsubr (&Sx_hide_tip);
6231 tip_timer = Qnil;
6232 staticpro (&tip_timer);
6233 tip_frame = Qnil;
6234 staticpro (&tip_frame);
6236 last_show_tip_args = Qnil;
6237 staticpro (&last_show_tip_args);
6239 defsubr (&Sx_uses_old_gtk_dialog);
6240 #if defined (USE_MOTIF) || defined (USE_GTK)
6241 defsubr (&Sx_file_dialog);
6242 #endif
6244 #if defined (USE_GTK) && defined (HAVE_FREETYPE)
6245 defsubr (&Sx_select_font);
6246 #endif