Auto-commit of loaddefs files.
[emacs/old-mirror.git] / src / xfns.c
blob1787d0297e6e2c923b6a8dd7f17f3c11af98de25
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 "menu.h"
28 #include "frame.h"
29 #include "window.h"
30 #include "character.h"
31 #include "buffer.h"
32 #include "intervals.h"
33 #include "dispextern.h"
34 #include "keyboard.h"
35 #include "blockinput.h"
36 #include <epaths.h>
37 #include "charset.h"
38 #include "coding.h"
39 #include "fontset.h"
40 #include "systime.h"
41 #include "termhooks.h"
42 #include "atimer.h"
43 #include "termchar.h"
44 #include "font.h"
46 #include <sys/types.h>
47 #include <sys/stat.h>
49 #if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work. */
50 #include "bitmaps/gray.xbm"
51 #else
52 #include <X11/bitmaps/gray>
53 #endif
55 #include "xsettings.h"
57 #ifdef HAVE_XRANDR
58 #include <X11/extensions/Xrandr.h>
59 #endif
60 #ifdef HAVE_XINERAMA
61 #include <X11/extensions/Xinerama.h>
62 #endif
64 #ifdef USE_GTK
65 #include "gtkutil.h"
66 #endif
68 #ifdef USE_X_TOOLKIT
69 #include <X11/Shell.h>
71 #ifndef USE_MOTIF
72 #ifdef HAVE_XAW3D
73 #include <X11/Xaw3d/Paned.h>
74 #include <X11/Xaw3d/Label.h>
75 #else /* !HAVE_XAW3D */
76 #include <X11/Xaw/Paned.h>
77 #include <X11/Xaw/Label.h>
78 #endif /* HAVE_XAW3D */
79 #endif /* USE_MOTIF */
81 #ifdef USG
82 #undef USG /* ####KLUDGE for Solaris 2.2 and up */
83 #include <X11/Xos.h>
84 #define USG
85 #ifdef USG /* Pacify gcc -Wunused-macros. */
86 #endif
87 #else
88 #include <X11/Xos.h>
89 #endif
91 #include "widget.h"
93 #include "../lwlib/lwlib.h"
95 #ifdef USE_MOTIF
96 #include <Xm/Xm.h>
97 #include <Xm/DialogS.h>
98 #include <Xm/FileSB.h>
99 #include <Xm/List.h>
100 #include <Xm/TextF.h>
101 #endif
103 #ifdef USE_LUCID
104 #include "../lwlib/xlwmenu.h"
105 #endif
107 #if !defined (NO_EDITRES)
108 #define HACK_EDITRES
109 extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
110 #endif /* not defined NO_EDITRES */
112 /* Unique id counter for widgets created by the Lucid Widget Library. */
114 extern LWLIB_ID widget_id_tick;
116 #ifdef USE_MOTIF
118 #endif /* USE_MOTIF */
120 #endif /* USE_X_TOOLKIT */
122 #ifdef USE_GTK
124 #endif /* USE_GTK */
126 #define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
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;
332 /* Get the mouse position in frame relative coordinates. */
334 void
335 x_relative_mouse_position (struct frame *f, int *x, int *y)
337 Window root, dummy_window;
338 int dummy;
340 eassert (FRAME_X_P (f));
342 block_input ();
344 XQueryPointer (FRAME_X_DISPLAY (f),
345 DefaultRootWindow (FRAME_X_DISPLAY (f)),
347 /* The root window which contains the pointer. */
348 &root,
350 /* Window pointer is on, not used */
351 &dummy_window,
353 /* The position on that root window. */
354 x, y,
356 /* x/y in dummy_window coordinates, not used. */
357 &dummy, &dummy,
359 /* Modifier keys and pointer buttons, about which
360 we don't care. */
361 (unsigned int *) &dummy);
363 unblock_input ();
365 /* Translate root window coordinates to window coordinates. */
366 *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
367 *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
370 /* Gamma-correct COLOR on frame F. */
372 void
373 gamma_correct (struct frame *f, XColor *color)
375 if (f->gamma)
377 color->red = pow (color->red / 65535.0, f->gamma) * 65535.0 + 0.5;
378 color->green = pow (color->green / 65535.0, f->gamma) * 65535.0 + 0.5;
379 color->blue = pow (color->blue / 65535.0, f->gamma) * 65535.0 + 0.5;
384 /* Decide if color named COLOR_NAME is valid for use on frame F. If
385 so, return the RGB values in COLOR. If ALLOC_P,
386 allocate the color. Value is false if COLOR_NAME is invalid, or
387 no color could be allocated. */
389 bool
390 x_defined_color (struct frame *f, const char *color_name,
391 XColor *color, bool alloc_p)
393 bool success_p = 0;
394 Display *dpy = FRAME_X_DISPLAY (f);
395 Colormap cmap = FRAME_X_COLORMAP (f);
397 block_input ();
398 #ifdef USE_GTK
399 success_p = xg_check_special_colors (f, color_name, color);
400 #endif
401 if (!success_p)
402 success_p = XParseColor (dpy, cmap, color_name, color) != 0;
403 if (success_p && alloc_p)
404 success_p = x_alloc_nearest_color (f, cmap, color);
405 unblock_input ();
407 return success_p;
411 /* Return the pixel color value for color COLOR_NAME on frame F. If F
412 is a monochrome frame, return MONO_COLOR regardless of what ARG says.
413 Signal an error if color can't be allocated. */
415 static int
416 x_decode_color (struct frame *f, Lisp_Object color_name, int mono_color)
418 XColor cdef;
420 CHECK_STRING (color_name);
422 #if 0 /* Don't do this. It's wrong when we're not using the default
423 colormap, it makes freeing difficult, and it's probably not
424 an important optimization. */
425 if (strcmp (SDATA (color_name), "black") == 0)
426 return BLACK_PIX_DEFAULT (f);
427 else if (strcmp (SDATA (color_name), "white") == 0)
428 return WHITE_PIX_DEFAULT (f);
429 #endif
431 /* Return MONO_COLOR for monochrome frames. */
432 if (FRAME_DISPLAY_INFO (f)->n_planes == 1)
433 return mono_color;
435 /* x_defined_color is responsible for coping with failures
436 by looking for a near-miss. */
437 if (x_defined_color (f, SSDATA (color_name), &cdef, 1))
438 return cdef.pixel;
440 signal_error ("Undefined color", color_name);
445 /* Change the `wait-for-wm' frame parameter of frame F. OLD_VALUE is
446 the previous value of that parameter, NEW_VALUE is the new value.
447 See also the comment of wait_for_wm in struct x_output. */
449 static void
450 x_set_wait_for_wm (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
452 f->output_data.x->wait_for_wm = !NILP (new_value);
455 static void
456 x_set_tool_bar_position (struct frame *f,
457 Lisp_Object new_value,
458 Lisp_Object old_value)
460 Lisp_Object choice = list4 (Qleft, Qright, Qtop, Qbottom);
462 if (!NILP (Fmemq (new_value, choice)))
464 #ifdef USE_GTK
465 if (!EQ (new_value, old_value))
467 xg_change_toolbar_position (f, new_value);
468 fset_tool_bar_position (f, new_value);
470 #else
471 if (!EQ (new_value, Qtop))
472 error ("The only supported tool bar position is top");
473 #endif
475 else
476 wrong_choice (choice, new_value);
479 #ifdef USE_GTK
481 /* Set icon from FILE for frame F. By using GTK functions the icon
482 may be any format that GdkPixbuf knows about, i.e. not just bitmaps. */
485 xg_set_icon (struct frame *f, Lisp_Object file)
487 int result = 0;
488 Lisp_Object found;
490 found = x_find_image_file (file);
492 if (! NILP (found))
494 GdkPixbuf *pixbuf;
495 GError *err = NULL;
496 char *filename = SSDATA (found);
497 block_input ();
499 pixbuf = gdk_pixbuf_new_from_file (filename, &err);
501 if (pixbuf)
503 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
504 pixbuf);
505 g_object_unref (pixbuf);
507 result = 1;
509 else
510 g_error_free (err);
512 unblock_input ();
515 return result;
519 xg_set_icon_from_xpm_data (struct frame *f, const char **data)
521 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data (data);
523 if (!pixbuf)
524 return 0;
526 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
527 g_object_unref (pixbuf);
528 return 1;
530 #endif /* USE_GTK */
533 /* Functions called only from `x_set_frame_param'
534 to set individual parameters.
536 If FRAME_X_WINDOW (f) is 0,
537 the frame is being created and its X-window does not exist yet.
538 In that case, just record the parameter's new value
539 in the standard place; do not attempt to change the window. */
541 static void
542 x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
544 struct x_output *x = f->output_data.x;
545 unsigned long fg, old_fg;
547 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
548 old_fg = FRAME_FOREGROUND_PIXEL (f);
549 FRAME_FOREGROUND_PIXEL (f) = fg;
551 if (FRAME_X_WINDOW (f) != 0)
553 Display *dpy = FRAME_X_DISPLAY (f);
555 block_input ();
556 XSetForeground (dpy, x->normal_gc, fg);
557 XSetBackground (dpy, x->reverse_gc, fg);
559 if (x->cursor_pixel == old_fg)
561 unload_color (f, x->cursor_pixel);
562 x->cursor_pixel = x_copy_color (f, fg);
563 XSetBackground (dpy, x->cursor_gc, x->cursor_pixel);
566 unblock_input ();
568 update_face_from_frame_parameter (f, Qforeground_color, arg);
570 if (FRAME_VISIBLE_P (f))
571 redraw_frame (f);
574 unload_color (f, old_fg);
577 static void
578 x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
580 struct x_output *x = f->output_data.x;
581 unsigned long bg;
583 bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
584 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
585 FRAME_BACKGROUND_PIXEL (f) = bg;
587 if (FRAME_X_WINDOW (f) != 0)
589 Display *dpy = FRAME_X_DISPLAY (f);
591 block_input ();
592 XSetBackground (dpy, x->normal_gc, bg);
593 XSetForeground (dpy, x->reverse_gc, bg);
594 XSetWindowBackground (dpy, FRAME_X_WINDOW (f), bg);
595 XSetForeground (dpy, x->cursor_gc, bg);
597 #ifdef USE_GTK
598 xg_set_background_color (f, bg);
599 #endif
601 #ifndef USE_TOOLKIT_SCROLL_BARS /* Turns out to be annoying with
602 toolkit scroll bars. */
604 Lisp_Object bar;
605 for (bar = FRAME_SCROLL_BARS (f);
606 !NILP (bar);
607 bar = XSCROLL_BAR (bar)->next)
609 Window window = XSCROLL_BAR (bar)->x_window;
610 XSetWindowBackground (dpy, window, bg);
613 #endif /* USE_TOOLKIT_SCROLL_BARS */
615 unblock_input ();
616 update_face_from_frame_parameter (f, Qbackground_color, arg);
618 if (FRAME_VISIBLE_P (f))
619 redraw_frame (f);
623 static void
624 x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
626 struct x_output *x = f->output_data.x;
627 Display *dpy = FRAME_X_DISPLAY (f);
628 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
629 Cursor hourglass_cursor, horizontal_drag_cursor, vertical_drag_cursor;
630 unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
631 unsigned long mask_color = FRAME_BACKGROUND_PIXEL (f);
633 /* Don't let pointers be invisible. */
634 if (mask_color == pixel)
636 x_free_colors (f, &pixel, 1);
637 pixel = x_copy_color (f, FRAME_FOREGROUND_PIXEL (f));
640 unload_color (f, x->mouse_pixel);
641 x->mouse_pixel = pixel;
643 block_input ();
645 /* It's not okay to crash if the user selects a screwy cursor. */
646 x_catch_errors (dpy);
648 if (!NILP (Vx_pointer_shape))
650 CHECK_NUMBER (Vx_pointer_shape);
651 cursor = XCreateFontCursor (dpy, XINT (Vx_pointer_shape));
653 else
654 cursor = XCreateFontCursor (dpy, XC_xterm);
655 x_check_errors (dpy, "bad text pointer cursor: %s");
657 if (!NILP (Vx_nontext_pointer_shape))
659 CHECK_NUMBER (Vx_nontext_pointer_shape);
660 nontext_cursor
661 = XCreateFontCursor (dpy, XINT (Vx_nontext_pointer_shape));
663 else
664 nontext_cursor = XCreateFontCursor (dpy, XC_left_ptr);
665 x_check_errors (dpy, "bad nontext pointer cursor: %s");
667 if (!NILP (Vx_hourglass_pointer_shape))
669 CHECK_NUMBER (Vx_hourglass_pointer_shape);
670 hourglass_cursor
671 = XCreateFontCursor (dpy, XINT (Vx_hourglass_pointer_shape));
673 else
674 hourglass_cursor = XCreateFontCursor (dpy, XC_watch);
675 x_check_errors (dpy, "bad hourglass pointer cursor: %s");
677 if (!NILP (Vx_mode_pointer_shape))
679 CHECK_NUMBER (Vx_mode_pointer_shape);
680 mode_cursor = XCreateFontCursor (dpy, XINT (Vx_mode_pointer_shape));
682 else
683 mode_cursor = XCreateFontCursor (dpy, XC_xterm);
684 x_check_errors (dpy, "bad modeline pointer cursor: %s");
686 if (!NILP (Vx_sensitive_text_pointer_shape))
688 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
689 hand_cursor
690 = XCreateFontCursor (dpy, XINT (Vx_sensitive_text_pointer_shape));
692 else
693 hand_cursor = XCreateFontCursor (dpy, XC_hand2);
695 if (!NILP (Vx_window_horizontal_drag_shape))
697 CHECK_TYPE_RANGED_INTEGER (unsigned, Vx_window_horizontal_drag_shape);
698 horizontal_drag_cursor
699 = XCreateFontCursor (dpy, XINT (Vx_window_horizontal_drag_shape));
701 else
702 horizontal_drag_cursor
703 = XCreateFontCursor (dpy, XC_sb_h_double_arrow);
705 if (!NILP (Vx_window_vertical_drag_shape))
707 CHECK_NUMBER (Vx_window_vertical_drag_shape);
708 vertical_drag_cursor
709 = XCreateFontCursor (dpy, XINT (Vx_window_vertical_drag_shape));
711 else
712 vertical_drag_cursor
713 = XCreateFontCursor (dpy, XC_sb_v_double_arrow);
715 /* Check and report errors with the above calls. */
716 x_check_errors (dpy, "can't set cursor shape: %s");
717 x_uncatch_errors ();
720 XColor fore_color, back_color;
722 fore_color.pixel = x->mouse_pixel;
723 x_query_color (f, &fore_color);
724 back_color.pixel = mask_color;
725 x_query_color (f, &back_color);
727 XRecolorCursor (dpy, cursor, &fore_color, &back_color);
728 XRecolorCursor (dpy, nontext_cursor, &fore_color, &back_color);
729 XRecolorCursor (dpy, mode_cursor, &fore_color, &back_color);
730 XRecolorCursor (dpy, hand_cursor, &fore_color, &back_color);
731 XRecolorCursor (dpy, hourglass_cursor, &fore_color, &back_color);
732 XRecolorCursor (dpy, horizontal_drag_cursor, &fore_color, &back_color);
733 XRecolorCursor (dpy, vertical_drag_cursor, &fore_color, &back_color);
736 if (FRAME_X_WINDOW (f) != 0)
737 XDefineCursor (dpy, FRAME_X_WINDOW (f),
738 f->output_data.x->current_cursor = cursor);
740 if (cursor != x->text_cursor
741 && x->text_cursor != 0)
742 XFreeCursor (dpy, x->text_cursor);
743 x->text_cursor = cursor;
745 if (nontext_cursor != x->nontext_cursor
746 && x->nontext_cursor != 0)
747 XFreeCursor (dpy, x->nontext_cursor);
748 x->nontext_cursor = nontext_cursor;
750 if (hourglass_cursor != x->hourglass_cursor
751 && x->hourglass_cursor != 0)
752 XFreeCursor (dpy, x->hourglass_cursor);
753 x->hourglass_cursor = hourglass_cursor;
755 if (mode_cursor != x->modeline_cursor
756 && x->modeline_cursor != 0)
757 XFreeCursor (dpy, f->output_data.x->modeline_cursor);
758 x->modeline_cursor = mode_cursor;
760 if (hand_cursor != x->hand_cursor
761 && x->hand_cursor != 0)
762 XFreeCursor (dpy, x->hand_cursor);
763 x->hand_cursor = hand_cursor;
765 if (horizontal_drag_cursor != x->horizontal_drag_cursor
766 && x->horizontal_drag_cursor != 0)
767 XFreeCursor (dpy, x->horizontal_drag_cursor);
768 x->horizontal_drag_cursor = horizontal_drag_cursor;
770 if (vertical_drag_cursor != x->vertical_drag_cursor
771 && x->vertical_drag_cursor != 0)
772 XFreeCursor (dpy, x->vertical_drag_cursor);
773 x->vertical_drag_cursor = vertical_drag_cursor;
775 XFlush (dpy);
776 unblock_input ();
778 update_face_from_frame_parameter (f, Qmouse_color, arg);
781 static void
782 x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
784 unsigned long fore_pixel, pixel;
785 bool fore_pixel_allocated_p = 0, pixel_allocated_p = 0;
786 struct x_output *x = f->output_data.x;
788 if (!NILP (Vx_cursor_fore_pixel))
790 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
791 WHITE_PIX_DEFAULT (f));
792 fore_pixel_allocated_p = 1;
794 else
795 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
797 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
798 pixel_allocated_p = 1;
800 /* Make sure that the cursor color differs from the background color. */
801 if (pixel == FRAME_BACKGROUND_PIXEL (f))
803 if (pixel_allocated_p)
805 x_free_colors (f, &pixel, 1);
806 pixel_allocated_p = 0;
809 pixel = x->mouse_pixel;
810 if (pixel == fore_pixel)
812 if (fore_pixel_allocated_p)
814 x_free_colors (f, &fore_pixel, 1);
815 fore_pixel_allocated_p = 0;
817 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
821 unload_color (f, x->cursor_foreground_pixel);
822 if (!fore_pixel_allocated_p)
823 fore_pixel = x_copy_color (f, fore_pixel);
824 x->cursor_foreground_pixel = fore_pixel;
826 unload_color (f, x->cursor_pixel);
827 if (!pixel_allocated_p)
828 pixel = x_copy_color (f, pixel);
829 x->cursor_pixel = pixel;
831 if (FRAME_X_WINDOW (f) != 0)
833 block_input ();
834 XSetBackground (FRAME_X_DISPLAY (f), x->cursor_gc, x->cursor_pixel);
835 XSetForeground (FRAME_X_DISPLAY (f), x->cursor_gc, fore_pixel);
836 unblock_input ();
838 if (FRAME_VISIBLE_P (f))
840 x_update_cursor (f, 0);
841 x_update_cursor (f, 1);
845 update_face_from_frame_parameter (f, Qcursor_color, arg);
848 /* Set the border-color of frame F to pixel value PIX.
849 Note that this does not fully take effect if done before
850 F has an x-window. */
852 static void
853 x_set_border_pixel (struct frame *f, int pix)
855 unload_color (f, f->output_data.x->border_pixel);
856 f->output_data.x->border_pixel = pix;
858 if (FRAME_X_WINDOW (f) != 0 && f->border_width > 0)
860 block_input ();
861 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), pix);
862 unblock_input ();
864 if (FRAME_VISIBLE_P (f))
865 redraw_frame (f);
869 /* Set the border-color of frame F to value described by ARG.
870 ARG can be a string naming a color.
871 The border-color is used for the border that is drawn by the X server.
872 Note that this does not fully take effect if done before
873 F has an x-window; it must be redone when the window is created.
875 Note: this is done in two routines because of the way X10 works.
877 Note: under X11, this is normally the province of the window manager,
878 and so emacs's border colors may be overridden. */
880 static void
881 x_set_border_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
883 int pix;
885 CHECK_STRING (arg);
886 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
887 x_set_border_pixel (f, pix);
888 update_face_from_frame_parameter (f, Qborder_color, arg);
892 static void
893 x_set_cursor_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
895 set_frame_cursor_types (f, arg);
898 static void
899 x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
901 int result;
903 if (STRINGP (arg))
905 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
906 return;
908 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
909 return;
911 block_input ();
912 if (NILP (arg))
913 result = x_text_icon (f,
914 SSDATA ((!NILP (f->icon_name)
915 ? f->icon_name
916 : f->name)));
917 else
918 result = x_bitmap_icon (f, arg);
920 if (result)
922 unblock_input ();
923 error ("No icon window available");
926 XFlush (FRAME_X_DISPLAY (f));
927 unblock_input ();
930 static void
931 x_set_icon_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
933 int result;
935 if (STRINGP (arg))
937 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
938 return;
940 else if (!NILP (arg) || NILP (oldval))
941 return;
943 fset_icon_name (f, arg);
945 if (f->output_data.x->icon_bitmap != 0)
946 return;
948 block_input ();
950 result = x_text_icon (f,
951 SSDATA ((!NILP (f->icon_name)
952 ? f->icon_name
953 : !NILP (f->title)
954 ? f->title
955 : f->name)));
957 if (result)
959 unblock_input ();
960 error ("No icon window available");
963 XFlush (FRAME_X_DISPLAY (f));
964 unblock_input ();
968 void
969 x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
971 int nlines;
972 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
973 int olines = FRAME_MENU_BAR_LINES (f);
974 #endif
976 /* Right now, menu bars don't work properly in minibuf-only frames;
977 most of the commands try to apply themselves to the minibuffer
978 frame itself, and get an error because you can't switch buffers
979 in or split the minibuffer window. */
980 if (FRAME_MINIBUF_ONLY_P (f))
981 return;
983 if (TYPE_RANGED_INTEGERP (int, value))
984 nlines = XINT (value);
985 else
986 nlines = 0;
988 /* Make sure we redisplay all windows in this frame. */
989 windows_or_buffers_changed = 59;
991 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
992 FRAME_MENU_BAR_LINES (f) = 0;
993 FRAME_MENU_BAR_HEIGHT (f) = 0;
994 if (nlines)
996 FRAME_EXTERNAL_MENU_BAR (f) = 1;
997 if (FRAME_X_P (f) && f->output_data.x->menubar_widget == 0)
998 /* Make sure next redisplay shows the menu bar. */
999 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = 1;
1001 else
1003 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1004 free_frame_menubar (f);
1005 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1006 if (FRAME_X_P (f))
1007 f->output_data.x->menubar_widget = 0;
1009 #else /* not USE_X_TOOLKIT && not USE_GTK */
1010 FRAME_MENU_BAR_LINES (f) = nlines;
1011 FRAME_MENU_BAR_HEIGHT (f) = nlines * FRAME_LINE_HEIGHT (f);
1012 adjust_frame_size (f, -1, -1, 2, 1);
1013 if (FRAME_X_WINDOW (f))
1014 x_clear_under_internal_border (f);
1016 /* If the menu bar height gets changed, the internal border below
1017 the top margin has to be cleared. Also, if the menu bar gets
1018 larger, the area for the added lines has to be cleared except for
1019 the first menu bar line that is to be drawn later. */
1020 if (nlines != olines)
1022 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1023 int width = FRAME_PIXEL_WIDTH (f);
1024 int y;
1026 /* height can be zero here. */
1027 if (FRAME_X_WINDOW (f) && height > 0 && width > 0)
1029 y = FRAME_TOP_MARGIN_HEIGHT (f);
1031 block_input ();
1032 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1033 0, y, width, height);
1034 unblock_input ();
1037 if (nlines > 1 && nlines > olines)
1039 y = (olines == 0 ? 1 : olines) * FRAME_LINE_HEIGHT (f);
1040 height = nlines * FRAME_LINE_HEIGHT (f) - y;
1042 block_input ();
1043 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1044 0, y, width, height);
1045 unblock_input ();
1048 if (nlines == 0 && WINDOWP (f->menu_bar_window))
1049 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
1051 #endif /* not USE_X_TOOLKIT && not USE_GTK */
1052 adjust_frame_glyphs (f);
1053 run_window_configuration_change_hook (f);
1057 /* Set the number of lines used for the tool bar of frame F to VALUE.
1058 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1059 is the old number of tool bar lines. This function changes the
1060 height of all windows on frame F to match the new tool bar height.
1061 The frame's height doesn't change. */
1063 void
1064 x_set_tool_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1066 int nlines;
1068 /* Treat tool bars like menu bars. */
1069 if (FRAME_MINIBUF_ONLY_P (f))
1070 return;
1072 /* Use VALUE only if an int >= 0. */
1073 if (RANGED_INTEGERP (0, value, INT_MAX))
1074 nlines = XFASTINT (value);
1075 else
1076 nlines = 0;
1078 x_change_tool_bar_height (f, nlines * FRAME_LINE_HEIGHT (f));
1082 /* Set the pixel height of the tool bar of frame F to HEIGHT. */
1083 void
1084 x_change_tool_bar_height (struct frame *f, int height)
1086 #ifdef USE_GTK
1087 FRAME_TOOL_BAR_LINES (f) = 0;
1088 FRAME_TOOL_BAR_HEIGHT (f) = 0;
1089 if (height)
1091 FRAME_EXTERNAL_TOOL_BAR (f) = 1;
1092 if (FRAME_X_P (f) && f->output_data.x->toolbar_widget == 0)
1093 /* Make sure next redisplay shows the tool bar. */
1094 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = 1;
1095 update_frame_tool_bar (f);
1097 else
1099 if (FRAME_EXTERNAL_TOOL_BAR (f))
1100 free_frame_tool_bar (f);
1101 FRAME_EXTERNAL_TOOL_BAR (f) = 0;
1103 #else /* !USE_GTK */
1104 int unit = FRAME_LINE_HEIGHT (f);
1105 int old_height = FRAME_TOOL_BAR_HEIGHT (f);
1106 int lines = (height + unit - 1) / unit;
1108 /* Make sure we redisplay all windows in this frame. */
1109 windows_or_buffers_changed = 60;
1112 /* Recalculate tool bar and frame text sizes. */
1113 FRAME_TOOL_BAR_HEIGHT (f) = height;
1114 FRAME_TOOL_BAR_LINES (f) = lines;
1115 FRAME_TEXT_HEIGHT (f)
1116 = FRAME_PIXEL_TO_TEXT_HEIGHT (f, FRAME_PIXEL_HEIGHT (f));
1117 FRAME_LINES (f)
1118 = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, FRAME_PIXEL_HEIGHT (f));
1119 /* Store the `tool-bar-lines' and `height' frame parameters. */
1120 store_frame_param (f, Qtool_bar_lines, make_number (lines));
1121 store_frame_param (f, Qheight, make_number (FRAME_LINES (f)));
1123 /* We also have to make sure that the internal border at the top of
1124 the frame, below the menu bar or tool bar, is redrawn when the
1125 tool bar disappears. This is so because the internal border is
1126 below the tool bar if one is displayed, but is below the menu bar
1127 if there isn't a tool bar. The tool bar draws into the area
1128 below the menu bar. */
1129 if (FRAME_X_WINDOW (f) && FRAME_TOOL_BAR_HEIGHT (f) == 0)
1131 clear_frame (f);
1132 clear_current_matrices (f);
1135 if ((height < old_height) && WINDOWP (f->tool_bar_window))
1136 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1138 /* Recalculate toolbar height. */
1139 f->n_tool_bar_rows = 0;
1141 adjust_frame_size (f, -1, -1, 4, 0);
1143 /** #if !defined USE_X_TOOLKIT **/
1144 if (FRAME_X_WINDOW (f))
1145 x_clear_under_internal_border (f);
1146 /** #endif **/
1148 #endif /* USE_GTK */
1152 void
1153 x_set_internal_border_width (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1155 int border;
1157 CHECK_TYPE_RANGED_INTEGER (int, arg);
1158 border = max (XINT (arg), 0);
1160 if (border != FRAME_INTERNAL_BORDER_WIDTH (f))
1162 FRAME_INTERNAL_BORDER_WIDTH (f) = border;
1164 #ifdef USE_X_TOOLKIT
1165 if (FRAME_X_OUTPUT (f)->edit_widget)
1166 widget_store_internal_border (FRAME_X_OUTPUT (f)->edit_widget);
1167 #endif
1169 if (FRAME_X_WINDOW (f) != 0)
1171 adjust_frame_size (f, -1, -1, 3, 0);
1173 #ifdef USE_GTK
1174 xg_clear_under_internal_border (f);
1175 #else
1176 x_clear_under_internal_border (f);
1177 #endif
1184 /* Set the foreground color for scroll bars on frame F to VALUE.
1185 VALUE should be a string, a color name. If it isn't a string or
1186 isn't a valid color name, do nothing. OLDVAL is the old value of
1187 the frame parameter. */
1189 static void
1190 x_set_scroll_bar_foreground (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1192 unsigned long pixel;
1194 if (STRINGP (value))
1195 pixel = x_decode_color (f, value, BLACK_PIX_DEFAULT (f));
1196 else
1197 pixel = -1;
1199 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
1200 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
1202 f->output_data.x->scroll_bar_foreground_pixel = pixel;
1203 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1205 /* Remove all scroll bars because they have wrong colors. */
1206 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1207 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1208 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1209 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1211 update_face_from_frame_parameter (f, Qscroll_bar_foreground, value);
1212 redraw_frame (f);
1217 /* Set the background color for scroll bars on frame F to VALUE VALUE
1218 should be a string, a color name. If it isn't a string or isn't a
1219 valid color name, do nothing. OLDVAL is the old value of the frame
1220 parameter. */
1222 static void
1223 x_set_scroll_bar_background (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1225 unsigned long pixel;
1227 if (STRINGP (value))
1228 pixel = x_decode_color (f, value, WHITE_PIX_DEFAULT (f));
1229 else
1230 pixel = -1;
1232 if (f->output_data.x->scroll_bar_background_pixel != -1)
1233 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
1235 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
1236 /* Scrollbar shadow colors. */
1237 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
1239 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
1240 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
1242 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
1244 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
1245 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
1247 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
1249 f->output_data.x->scroll_bar_background_pixel = pixel;
1250 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1252 /* Remove all scroll bars because they have wrong colors. */
1253 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1254 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1255 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1256 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1258 update_face_from_frame_parameter (f, Qscroll_bar_background, value);
1259 redraw_frame (f);
1264 /* Encode Lisp string STRING as a text in a format appropriate for
1265 XICCC (X Inter Client Communication Conventions).
1267 This can call Lisp code, so callers must GCPRO.
1269 If STRING contains only ASCII characters, do no conversion and
1270 return the string data of STRING. Otherwise, encode the text by
1271 CODING_SYSTEM, and return a newly allocated memory area which
1272 should be freed by `xfree' by a caller.
1274 Store the byte length of resulting text in *TEXT_BYTES.
1276 If the text contains only ASCII and Latin-1, store 1 in *STRING_P,
1277 which means that the `encoding' of the result can be `STRING'.
1278 Otherwise store 0 in *STRINGP, which means that the `encoding' of
1279 the result should be `COMPOUND_TEXT'. */
1281 static unsigned char *
1282 x_encode_text (Lisp_Object string, Lisp_Object coding_system,
1283 ptrdiff_t *text_bytes, int *stringp, bool *freep)
1285 int result = string_xstring_p (string);
1286 struct coding_system coding;
1288 if (result == 0)
1290 /* No multibyte character in OBJ. We need not encode it. */
1291 *text_bytes = SBYTES (string);
1292 *stringp = 1;
1293 *freep = 0;
1294 return SDATA (string);
1297 setup_coding_system (coding_system, &coding);
1298 coding.mode |= (CODING_MODE_SAFE_ENCODING | CODING_MODE_LAST_BLOCK);
1299 /* We suppress producing escape sequences for composition. */
1300 coding.common_flags &= ~CODING_ANNOTATION_MASK;
1301 coding.destination = xnmalloc (SCHARS (string), 2);
1302 coding.dst_bytes = SCHARS (string) * 2;
1303 encode_coding_object (&coding, string, 0, 0,
1304 SCHARS (string), SBYTES (string), Qnil);
1305 *text_bytes = coding.produced;
1306 *stringp = (result == 1 || !EQ (coding_system, Qcompound_text));
1307 *freep = 1;
1308 return coding.destination;
1312 /* Set the WM name to NAME for frame F. Also set the icon name.
1313 If the frame already has an icon name, use that, otherwise set the
1314 icon name to NAME. */
1316 static void
1317 x_set_name_internal (struct frame *f, Lisp_Object name)
1319 if (FRAME_X_WINDOW (f))
1321 block_input ();
1323 XTextProperty text, icon;
1324 ptrdiff_t bytes;
1325 int stringp;
1326 bool do_free_icon_value = 0, do_free_text_value = 0;
1327 Lisp_Object coding_system;
1328 Lisp_Object encoded_name;
1329 Lisp_Object encoded_icon_name;
1330 struct gcpro gcpro1;
1332 /* As ENCODE_UTF_8 may cause GC and relocation of string data,
1333 we use it before x_encode_text that may return string data. */
1334 GCPRO1 (name);
1335 encoded_name = ENCODE_UTF_8 (name);
1336 UNGCPRO;
1338 coding_system = Qcompound_text;
1339 /* Note: Encoding strategy
1341 We encode NAME by compound-text and use "COMPOUND-TEXT" in
1342 text.encoding. But, there are non-internationalized window
1343 managers which don't support that encoding. So, if NAME
1344 contains only ASCII and 8859-1 characters, encode it by
1345 iso-latin-1, and use "STRING" in text.encoding hoping that
1346 such window managers at least analyze this format correctly,
1347 i.e. treat 8-bit bytes as 8859-1 characters.
1349 We may also be able to use "UTF8_STRING" in text.encoding
1350 in the future which can encode all Unicode characters.
1351 But, for the moment, there's no way to know that the
1352 current window manager supports it or not.
1354 Either way, we also set the _NET_WM_NAME and _NET_WM_ICON_NAME
1355 properties. Per the EWMH specification, those two properties
1356 are always UTF8_STRING. This matches what gtk_window_set_title()
1357 does in the USE_GTK case. */
1358 text.value = x_encode_text (name, coding_system, &bytes,
1359 &stringp, &do_free_text_value);
1360 text.encoding = (stringp ? XA_STRING
1361 : FRAME_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1362 text.format = 8;
1363 text.nitems = bytes;
1365 if (!STRINGP (f->icon_name))
1367 icon = text;
1368 encoded_icon_name = encoded_name;
1370 else
1372 /* See the above comment "Note: Encoding strategy". */
1373 icon.value = x_encode_text (f->icon_name, coding_system, &bytes,
1374 &stringp, &do_free_icon_value);
1375 icon.encoding = (stringp ? XA_STRING
1376 : FRAME_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1377 icon.format = 8;
1378 icon.nitems = bytes;
1380 encoded_icon_name = ENCODE_UTF_8 (f->icon_name);
1383 #ifdef USE_GTK
1384 gtk_window_set_title (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1385 SSDATA (encoded_name));
1386 #else /* not USE_GTK */
1387 XSetWMName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
1388 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1389 FRAME_DISPLAY_INFO (f)->Xatom_net_wm_name,
1390 FRAME_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1391 8, PropModeReplace,
1392 SDATA (encoded_name),
1393 SBYTES (encoded_name));
1394 #endif /* not USE_GTK */
1396 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &icon);
1397 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1398 FRAME_DISPLAY_INFO (f)->Xatom_net_wm_icon_name,
1399 FRAME_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1400 8, PropModeReplace,
1401 SDATA (encoded_icon_name),
1402 SBYTES (encoded_icon_name));
1404 if (do_free_icon_value)
1405 xfree (icon.value);
1406 if (do_free_text_value)
1407 xfree (text.value);
1409 unblock_input ();
1413 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1414 x_id_name.
1416 If EXPLICIT is true, that indicates that lisp code is setting the
1417 name; if NAME is a string, set F's name to NAME and set
1418 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1420 If EXPLICIT is false, that indicates that Emacs redisplay code is
1421 suggesting a new name, which lisp code should override; if
1422 F->explicit_name is set, ignore the new name; otherwise, set it. */
1424 static void
1425 x_set_name (struct frame *f, Lisp_Object name, bool explicit)
1427 /* Make sure that requests from lisp code override requests from
1428 Emacs redisplay code. */
1429 if (explicit)
1431 /* If we're switching from explicit to implicit, we had better
1432 update the mode lines and thereby update the title. */
1433 if (f->explicit_name && NILP (name))
1434 update_mode_lines = 37;
1436 f->explicit_name = ! NILP (name);
1438 else if (f->explicit_name)
1439 return;
1441 /* If NAME is nil, set the name to the x_id_name. */
1442 if (NILP (name))
1444 /* Check for no change needed in this very common case
1445 before we do any consing. */
1446 if (!strcmp (FRAME_DISPLAY_INFO (f)->x_id_name,
1447 SSDATA (f->name)))
1448 return;
1449 name = build_string (FRAME_DISPLAY_INFO (f)->x_id_name);
1451 else
1452 CHECK_STRING (name);
1454 /* Don't change the name if it's already NAME. */
1455 if (! NILP (Fstring_equal (name, f->name)))
1456 return;
1458 fset_name (f, name);
1460 /* For setting the frame title, the title parameter should override
1461 the name parameter. */
1462 if (! NILP (f->title))
1463 name = f->title;
1465 x_set_name_internal (f, name);
1468 /* This function should be called when the user's lisp code has
1469 specified a name for the frame; the name will override any set by the
1470 redisplay code. */
1471 static void
1472 x_explicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1474 x_set_name (f, arg, 1);
1477 /* This function should be called by Emacs redisplay code to set the
1478 name; names set this way will never override names set by the user's
1479 lisp code. */
1480 void
1481 x_implicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1483 x_set_name (f, arg, 0);
1486 /* Change the title of frame F to NAME.
1487 If NAME is nil, use the frame name as the title. */
1489 static void
1490 x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
1492 /* Don't change the title if it's already NAME. */
1493 if (EQ (name, f->title))
1494 return;
1496 update_mode_lines = 38;
1498 fset_title (f, name);
1500 if (NILP (name))
1501 name = f->name;
1502 else
1503 CHECK_STRING (name);
1505 x_set_name_internal (f, name);
1508 void
1509 x_set_scroll_bar_default_width (struct frame *f)
1511 int unit = FRAME_COLUMN_WIDTH (f);
1512 #ifdef USE_TOOLKIT_SCROLL_BARS
1513 #ifdef USE_GTK
1514 int minw = xg_get_default_scrollbar_width ();
1515 #else
1516 int minw = 16;
1517 #endif
1518 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
1519 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (minw + unit - 1) / unit;
1520 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = minw;
1521 #else
1522 /* The width of a non-toolkit scrollbar is 14 pixels. */
1523 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
1524 FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
1525 = FRAME_CONFIG_SCROLL_BAR_COLS (f) * unit;
1526 #endif
1529 void
1530 x_set_scroll_bar_default_height (struct frame *f)
1532 int height = FRAME_LINE_HEIGHT (f);
1533 #ifdef USE_TOOLKIT_SCROLL_BARS
1534 #ifdef USE_GTK
1535 int min_height = xg_get_default_scrollbar_height ();
1536 #else
1537 int min_height = 16;
1538 #endif
1539 /* A minimum height of 14 doesn't look good for toolkit scroll bars. */
1540 FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) = min_height;
1541 FRAME_CONFIG_SCROLL_BAR_LINES (f) = (min_height + height - 1) / height;
1542 #else
1543 /* The height of a non-toolkit scrollbar is 14 pixels. */
1544 FRAME_CONFIG_SCROLL_BAR_LINES (f) = (14 + height - 1) / height;
1546 /* Use all of that space (aside from required margins) for the
1547 scroll bar. */
1548 FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) = 14;
1549 #endif
1553 /* Record in frame F the specified or default value according to ALIST
1554 of the parameter named PROP (a Lisp symbol). If no value is
1555 specified for PROP, look for an X default for XPROP on the frame
1556 named NAME. If that is not found either, use the value DEFLT. */
1558 static Lisp_Object
1559 x_default_scroll_bar_color_parameter (struct frame *f,
1560 Lisp_Object alist, Lisp_Object prop,
1561 const char *xprop, const char *xclass,
1562 int foreground_p)
1564 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1565 Lisp_Object tem;
1567 tem = x_get_arg (dpyinfo, alist, prop, xprop, xclass, RES_TYPE_STRING);
1568 if (EQ (tem, Qunbound))
1570 #ifdef USE_TOOLKIT_SCROLL_BARS
1572 /* See if an X resource for the scroll bar color has been
1573 specified. */
1574 tem = display_x_get_resource (dpyinfo,
1575 build_string (foreground_p
1576 ? "foreground"
1577 : "background"),
1578 empty_unibyte_string,
1579 build_string ("verticalScrollBar"),
1580 empty_unibyte_string);
1581 if (!STRINGP (tem))
1583 /* If nothing has been specified, scroll bars will use a
1584 toolkit-dependent default. Because these defaults are
1585 difficult to get at without actually creating a scroll
1586 bar, use nil to indicate that no color has been
1587 specified. */
1588 tem = Qnil;
1591 #else /* not USE_TOOLKIT_SCROLL_BARS */
1593 tem = Qnil;
1595 #endif /* not USE_TOOLKIT_SCROLL_BARS */
1598 x_set_frame_parameters (f, list1 (Fcons (prop, tem)));
1599 return tem;
1605 #ifdef USE_X_TOOLKIT
1607 /* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
1608 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
1609 already be present because of the toolkit (Motif adds some of them,
1610 for example, but Xt doesn't). */
1612 static void
1613 hack_wm_protocols (struct frame *f, Widget widget)
1615 Display *dpy = XtDisplay (widget);
1616 Window w = XtWindow (widget);
1617 int need_delete = 1;
1618 int need_focus = 1;
1619 int need_save = 1;
1621 block_input ();
1623 Atom type;
1624 unsigned char *catoms;
1625 int format = 0;
1626 unsigned long nitems = 0;
1627 unsigned long bytes_after;
1629 if ((XGetWindowProperty (dpy, w,
1630 FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
1631 0, 100, False, XA_ATOM,
1632 &type, &format, &nitems, &bytes_after,
1633 &catoms)
1634 == Success)
1635 && format == 32 && type == XA_ATOM)
1637 Atom *atoms = (Atom *) catoms;
1638 while (nitems > 0)
1640 nitems--;
1641 if (atoms[nitems]
1642 == FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window)
1643 need_delete = 0;
1644 else if (atoms[nitems]
1645 == FRAME_DISPLAY_INFO (f)->Xatom_wm_take_focus)
1646 need_focus = 0;
1647 else if (atoms[nitems]
1648 == FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself)
1649 need_save = 0;
1652 if (catoms)
1653 XFree (catoms);
1656 Atom props [10];
1657 int count = 0;
1658 if (need_delete)
1659 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window;
1660 if (need_focus)
1661 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_take_focus;
1662 if (need_save)
1663 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
1664 if (count)
1665 XChangeProperty (dpy, w, FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
1666 XA_ATOM, 32, PropModeAppend,
1667 (unsigned char *) props, count);
1669 unblock_input ();
1671 #endif
1675 /* Support routines for XIC (X Input Context). */
1677 #ifdef HAVE_X_I18N
1679 static XFontSet xic_create_xfontset (struct frame *);
1680 static XIMStyle best_xim_style (XIMStyles *);
1683 /* Supported XIM styles, ordered by preference. */
1685 static const XIMStyle supported_xim_styles[] =
1687 XIMPreeditPosition | XIMStatusArea,
1688 XIMPreeditPosition | XIMStatusNothing,
1689 XIMPreeditPosition | XIMStatusNone,
1690 XIMPreeditNothing | XIMStatusArea,
1691 XIMPreeditNothing | XIMStatusNothing,
1692 XIMPreeditNothing | XIMStatusNone,
1693 XIMPreeditNone | XIMStatusArea,
1694 XIMPreeditNone | XIMStatusNothing,
1695 XIMPreeditNone | XIMStatusNone,
1700 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
1701 /* Create an X fontset on frame F with base font name BASE_FONTNAME. */
1703 static const char xic_default_fontset[] = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
1705 /* Create an Xt fontset spec from the name of a base font.
1706 If `motif' is True use the Motif syntax. */
1707 char *
1708 xic_create_fontsetname (const char *base_fontname, int motif)
1710 const char *sep = motif ? ";" : ",";
1711 char *fontsetname;
1713 /* Make a fontset name from the base font name. */
1714 if (xic_default_fontset == base_fontname)
1716 /* There is no base font name, use the default. */
1717 fontsetname = xmalloc (strlen (base_fontname) + 2);
1718 strcpy (fontsetname, base_fontname);
1720 else
1722 /* Make a fontset name from the base font name.
1723 The font set will be made of the following elements:
1724 - the base font.
1725 - the base font where the charset spec is replaced by -*-*.
1726 - the same but with the family also replaced with -*-*-. */
1727 const char *p = base_fontname;
1728 ptrdiff_t i;
1730 for (i = 0; *p; p++)
1731 if (*p == '-') i++;
1732 if (i != 14)
1734 /* As the font name doesn't conform to XLFD, we can't
1735 modify it to generalize it to allcs and allfamilies.
1736 Use the specified font plus the default. */
1737 fontsetname = xmalloc (strlen (base_fontname)
1738 + strlen (xic_default_fontset) + 3);
1739 strcpy (fontsetname, base_fontname);
1740 strcat (fontsetname, sep);
1741 strcat (fontsetname, xic_default_fontset);
1743 else
1745 ptrdiff_t len;
1746 const char *p1 = NULL, *p2 = NULL, *p3 = NULL;
1747 char *font_allcs = NULL;
1748 char *font_allfamilies = NULL;
1749 char *font_all = NULL;
1750 const char *allcs = "*-*-*-*-*-*-*";
1751 const char *allfamilies = "-*-*-";
1752 const char *all = "*-*-*-*-";
1753 char *base;
1755 for (i = 0, p = base_fontname; i < 8; p++)
1757 if (*p == '-')
1759 i++;
1760 if (i == 3)
1761 p1 = p + 1;
1762 else if (i == 7)
1763 p2 = p + 1;
1764 else if (i == 6)
1765 p3 = p + 1;
1768 /* If base_fontname specifies ADSTYLE, make it a
1769 wildcard. */
1770 if (*p3 != '*')
1772 ptrdiff_t diff = (p2 - p3) - 2;
1774 base = alloca (strlen (base_fontname) + 1);
1775 memcpy (base, base_fontname, p3 - base_fontname);
1776 base[p3 - base_fontname] = '*';
1777 base[(p3 - base_fontname) + 1] = '-';
1778 strcpy (base + (p3 - base_fontname) + 2, p2);
1779 p = base + (p - base_fontname) - diff;
1780 p1 = base + (p1 - base_fontname);
1781 p2 = base + (p2 - base_fontname) - diff;
1782 base_fontname = base;
1785 /* Build the font spec that matches all charsets. */
1786 len = p - base_fontname + strlen (allcs) + 1;
1787 font_allcs = alloca (len);
1788 memcpy (font_allcs, base_fontname, p - base_fontname);
1789 strcat (font_allcs, allcs);
1791 /* Build the font spec that matches all families and
1792 add-styles. */
1793 len = p - p1 + strlen (allcs) + strlen (allfamilies) + 1;
1794 font_allfamilies = alloca (len);
1795 strcpy (font_allfamilies, allfamilies);
1796 memcpy (font_allfamilies + strlen (allfamilies), p1, p - p1);
1797 strcat (font_allfamilies, allcs);
1799 /* Build the font spec that matches all. */
1800 len = p - p2 + strlen (allcs) + strlen (all) + strlen (allfamilies) + 1;
1801 font_all = alloca (len);
1802 strcpy (font_all, allfamilies);
1803 strcat (font_all, all);
1804 memcpy (font_all + strlen (all) + strlen (allfamilies), p2, p - p2);
1805 strcat (font_all, allcs);
1807 /* Build the actual font set name. */
1808 len = strlen (base_fontname) + strlen (font_allcs)
1809 + strlen (font_allfamilies) + strlen (font_all) + 5;
1810 fontsetname = xmalloc (len);
1811 strcpy (fontsetname, base_fontname);
1812 strcat (fontsetname, sep);
1813 strcat (fontsetname, font_allcs);
1814 strcat (fontsetname, sep);
1815 strcat (fontsetname, font_allfamilies);
1816 strcat (fontsetname, sep);
1817 strcat (fontsetname, font_all);
1820 if (motif)
1821 return strcat (fontsetname, ":");
1822 return fontsetname;
1824 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
1826 #ifdef DEBUG_XIC_FONTSET
1827 static void
1828 print_fontset_result (XFontSet xfs, char *name, char **missing_list,
1829 int missing_count)
1831 if (xfs)
1832 fprintf (stderr, "XIC Fontset created: %s\n", name);
1833 else
1835 fprintf (stderr, "XIC Fontset failed: %s\n", name);
1836 while (missing_count-- > 0)
1838 fprintf (stderr, " missing: %s\n", *missing_list);
1839 missing_list++;
1844 #endif
1846 static XFontSet
1847 xic_create_xfontset (struct frame *f)
1849 XFontSet xfs = NULL;
1850 struct font *font = FRAME_FONT (f);
1851 int pixel_size = font->pixel_size;
1852 Lisp_Object rest, frame;
1854 /* See if there is another frame already using same fontset. */
1855 FOR_EACH_FRAME (rest, frame)
1857 struct frame *cf = XFRAME (frame);
1859 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
1860 && FRAME_DISPLAY_INFO (cf) == FRAME_DISPLAY_INFO (f)
1861 && FRAME_FONT (f)
1862 && FRAME_FONT (f)->pixel_size == pixel_size)
1864 xfs = FRAME_XIC_FONTSET (cf);
1865 break;
1869 if (! xfs)
1871 char buf[256];
1872 char **missing_list;
1873 int missing_count;
1874 char *def_string;
1875 const char *xlfd_format = "-*-*-medium-r-normal--%d-*-*-*-*-*";
1877 sprintf (buf, xlfd_format, pixel_size);
1878 missing_list = NULL;
1879 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
1880 &missing_list, &missing_count, &def_string);
1881 #ifdef DEBUG_XIC_FONTSET
1882 print_fontset_result (xfs, buf, missing_list, missing_count);
1883 #endif
1884 if (missing_list)
1885 XFreeStringList (missing_list);
1886 if (! xfs)
1888 /* List of pixel sizes most likely available. Find one that
1889 is closest to pixel_size. */
1890 int sizes[] = {0, 8, 10, 11, 12, 14, 17, 18, 20, 24, 26, 34, 0};
1891 int *smaller, *larger;
1893 for (smaller = sizes; smaller[1]; smaller++)
1894 if (smaller[1] >= pixel_size)
1895 break;
1896 larger = smaller + 1;
1897 if (*larger == pixel_size)
1898 larger++;
1899 while (*smaller || *larger)
1901 int this_size;
1903 if (! *larger)
1904 this_size = *smaller--;
1905 else if (! *smaller)
1906 this_size = *larger++;
1907 else if (pixel_size - *smaller < *larger - pixel_size)
1908 this_size = *smaller--;
1909 else
1910 this_size = *larger++;
1911 sprintf (buf, xlfd_format, this_size);
1912 missing_list = NULL;
1913 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
1914 &missing_list, &missing_count, &def_string);
1915 #ifdef DEBUG_XIC_FONTSET
1916 print_fontset_result (xfs, buf, missing_list, missing_count);
1917 #endif
1918 if (missing_list)
1919 XFreeStringList (missing_list);
1920 if (xfs)
1921 break;
1924 if (! xfs)
1926 const char *last_resort = "-*-*-*-r-normal--*-*-*-*-*-*";
1928 missing_list = NULL;
1929 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), last_resort,
1930 &missing_list, &missing_count, &def_string);
1931 #ifdef DEBUG_XIC_FONTSET
1932 print_fontset_result (xfs, last_resort, missing_list, missing_count);
1933 #endif
1934 if (missing_list)
1935 XFreeStringList (missing_list);
1940 return xfs;
1943 /* Free the X fontset of frame F if it is the last frame using it. */
1945 void
1946 xic_free_xfontset (struct frame *f)
1948 Lisp_Object rest, frame;
1949 bool shared_p = 0;
1951 if (!FRAME_XIC_FONTSET (f))
1952 return;
1954 /* See if there is another frame sharing the same fontset. */
1955 FOR_EACH_FRAME (rest, frame)
1957 struct frame *cf = XFRAME (frame);
1958 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
1959 && FRAME_DISPLAY_INFO (cf) == FRAME_DISPLAY_INFO (f)
1960 && FRAME_XIC_FONTSET (cf) == FRAME_XIC_FONTSET (f))
1962 shared_p = 1;
1963 break;
1967 if (!shared_p)
1968 /* The fontset is not used anymore. It is safe to free it. */
1969 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
1971 FRAME_XIC_FONTSET (f) = NULL;
1975 /* Value is the best input style, given user preferences USER (already
1976 checked to be supported by Emacs), and styles supported by the
1977 input method XIM. */
1979 static XIMStyle
1980 best_xim_style (XIMStyles *xim)
1982 int i, j;
1983 int nr_supported = ARRAYELTS (supported_xim_styles);
1985 for (i = 0; i < nr_supported; ++i)
1986 for (j = 0; j < xim->count_styles; ++j)
1987 if (supported_xim_styles[i] == xim->supported_styles[j])
1988 return supported_xim_styles[i];
1990 /* Return the default style. */
1991 return XIMPreeditNothing | XIMStatusNothing;
1994 /* Create XIC for frame F. */
1996 void
1997 create_frame_xic (struct frame *f)
1999 XIM xim;
2000 XIC xic = NULL;
2001 XFontSet xfs = NULL;
2002 XVaNestedList status_attr = NULL;
2003 XVaNestedList preedit_attr = NULL;
2004 XRectangle s_area;
2005 XPoint spot;
2006 XIMStyle xic_style;
2008 if (FRAME_XIC (f))
2009 goto out;
2011 xim = FRAME_X_XIM (f);
2012 if (!xim)
2013 goto out;
2015 /* Determine XIC style. */
2016 xic_style = best_xim_style (FRAME_X_XIM_STYLES (f));
2018 /* Create X fontset. */
2019 if (xic_style & (XIMPreeditPosition | XIMStatusArea))
2021 xfs = xic_create_xfontset (f);
2022 if (!xfs)
2023 goto out;
2025 FRAME_XIC_FONTSET (f) = xfs;
2028 if (xic_style & XIMPreeditPosition)
2030 spot.x = 0; spot.y = 1;
2031 preedit_attr = XVaCreateNestedList (0,
2032 XNFontSet, xfs,
2033 XNForeground,
2034 FRAME_FOREGROUND_PIXEL (f),
2035 XNBackground,
2036 FRAME_BACKGROUND_PIXEL (f),
2037 (xic_style & XIMPreeditPosition
2038 ? XNSpotLocation
2039 : NULL),
2040 &spot,
2041 NULL);
2043 if (!preedit_attr)
2044 goto out;
2047 if (xic_style & XIMStatusArea)
2049 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
2050 status_attr = XVaCreateNestedList (0,
2051 XNArea,
2052 &s_area,
2053 XNFontSet,
2054 xfs,
2055 XNForeground,
2056 FRAME_FOREGROUND_PIXEL (f),
2057 XNBackground,
2058 FRAME_BACKGROUND_PIXEL (f),
2059 NULL);
2061 if (!status_attr)
2062 goto out;
2065 if (preedit_attr && status_attr)
2066 xic = XCreateIC (xim,
2067 XNInputStyle, xic_style,
2068 XNClientWindow, FRAME_X_WINDOW (f),
2069 XNFocusWindow, FRAME_X_WINDOW (f),
2070 XNStatusAttributes, status_attr,
2071 XNPreeditAttributes, preedit_attr,
2072 NULL);
2073 else if (preedit_attr)
2074 xic = XCreateIC (xim,
2075 XNInputStyle, xic_style,
2076 XNClientWindow, FRAME_X_WINDOW (f),
2077 XNFocusWindow, FRAME_X_WINDOW (f),
2078 XNPreeditAttributes, preedit_attr,
2079 NULL);
2080 else if (status_attr)
2081 xic = XCreateIC (xim,
2082 XNInputStyle, xic_style,
2083 XNClientWindow, FRAME_X_WINDOW (f),
2084 XNFocusWindow, FRAME_X_WINDOW (f),
2085 XNStatusAttributes, status_attr,
2086 NULL);
2087 else
2088 xic = XCreateIC (xim,
2089 XNInputStyle, xic_style,
2090 XNClientWindow, FRAME_X_WINDOW (f),
2091 XNFocusWindow, FRAME_X_WINDOW (f),
2092 NULL);
2094 if (!xic)
2095 goto out;
2097 FRAME_XIC (f) = xic;
2098 FRAME_XIC_STYLE (f) = xic_style;
2099 xfs = NULL; /* Don't free below. */
2101 out:
2103 if (xfs)
2104 free_frame_xic (f);
2106 if (preedit_attr)
2107 XFree (preedit_attr);
2109 if (status_attr)
2110 XFree (status_attr);
2114 /* Destroy XIC and free XIC fontset of frame F, if any. */
2116 void
2117 free_frame_xic (struct frame *f)
2119 if (FRAME_XIC (f) == NULL)
2120 return;
2122 XDestroyIC (FRAME_XIC (f));
2123 xic_free_xfontset (f);
2125 FRAME_XIC (f) = NULL;
2129 /* Place preedit area for XIC of window W's frame to specified
2130 pixel position X/Y. X and Y are relative to window W. */
2132 void
2133 xic_set_preeditarea (struct window *w, int x, int y)
2135 struct frame *f = XFRAME (w->frame);
2136 XVaNestedList attr;
2137 XPoint spot;
2139 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x) + WINDOW_LEFT_FRINGE_WIDTH (w);
2140 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
2141 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
2142 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2143 XFree (attr);
2147 /* Place status area for XIC in bottom right corner of frame F.. */
2149 void
2150 xic_set_statusarea (struct frame *f)
2152 XIC xic = FRAME_XIC (f);
2153 XVaNestedList attr;
2154 XRectangle area;
2155 XRectangle *needed;
2157 /* Negotiate geometry of status area. If input method has existing
2158 status area, use its current size. */
2159 area.x = area.y = area.width = area.height = 0;
2160 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
2161 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2162 XFree (attr);
2164 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
2165 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2166 XFree (attr);
2168 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
2170 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
2171 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2172 XFree (attr);
2175 area.width = needed->width;
2176 area.height = needed->height;
2177 area.x = FRAME_PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
2178 area.y = (FRAME_PIXEL_HEIGHT (f) - area.height
2179 - FRAME_MENUBAR_HEIGHT (f)
2180 - FRAME_TOOLBAR_TOP_HEIGHT (f)
2181 - FRAME_INTERNAL_BORDER_WIDTH (f));
2182 XFree (needed);
2184 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
2185 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2186 XFree (attr);
2190 /* Set X fontset for XIC of frame F, using base font name
2191 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
2193 void
2194 xic_set_xfontset (struct frame *f, const char *base_fontname)
2196 XVaNestedList attr;
2197 XFontSet xfs;
2199 xic_free_xfontset (f);
2201 xfs = xic_create_xfontset (f);
2203 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
2204 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
2205 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2206 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
2207 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
2208 XFree (attr);
2210 FRAME_XIC_FONTSET (f) = xfs;
2213 #endif /* HAVE_X_I18N */
2217 #ifdef USE_X_TOOLKIT
2219 /* Create and set up the X widget for frame F. */
2221 static void
2222 x_window (struct frame *f, long window_prompting, int minibuffer_only)
2224 XClassHint class_hints;
2225 XSetWindowAttributes attributes;
2226 unsigned long attribute_mask;
2227 Widget shell_widget;
2228 Widget pane_widget;
2229 Widget frame_widget;
2230 Arg al [25];
2231 int ac;
2233 block_input ();
2235 /* Use the resource name as the top-level widget name
2236 for looking up resources. Make a non-Lisp copy
2237 for the window manager, so GC relocation won't bother it.
2239 Elsewhere we specify the window name for the window manager. */
2240 f->namebuf = xstrdup (SSDATA (Vx_resource_name));
2242 ac = 0;
2243 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
2244 XtSetArg (al[ac], XtNinput, 1); ac++;
2245 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2246 XtSetArg (al[ac], XtNborderWidth, f->border_width); ac++;
2247 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2248 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2249 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2250 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
2251 applicationShellWidgetClass,
2252 FRAME_X_DISPLAY (f), al, ac);
2254 f->output_data.x->widget = shell_widget;
2255 /* maybe_set_screen_title_format (shell_widget); */
2257 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
2258 NULL, shell_widget, False,
2259 NULL, NULL, NULL, NULL);
2261 ac = 0;
2262 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2263 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2264 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2265 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2266 XtSetValues (pane_widget, al, ac);
2267 f->output_data.x->column_widget = pane_widget;
2269 /* mappedWhenManaged to false tells to the paned window to not map/unmap
2270 the emacs screen when changing menubar. This reduces flickering. */
2272 ac = 0;
2273 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2274 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
2275 XtSetArg (al[ac], XtNallowResize, 1); ac++;
2276 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
2277 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
2278 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2279 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2280 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2281 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2282 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
2283 al, ac);
2285 f->output_data.x->edit_widget = frame_widget;
2287 XtManageChild (frame_widget);
2289 /* Do some needed geometry management. */
2291 Arg gal[3];
2292 int gac = 0;
2293 int extra_borders = 0;
2294 int menubar_size
2295 = (f->output_data.x->menubar_widget
2296 ? (f->output_data.x->menubar_widget->core.height
2297 + f->output_data.x->menubar_widget->core.border_width)
2298 : 0);
2300 #if 0 /* Experimentally, we now get the right results
2301 for -geometry -0-0 without this. 24 Aug 96, rms. */
2302 if (FRAME_EXTERNAL_MENU_BAR (f))
2304 Dimension ibw = 0;
2305 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
2306 menubar_size += ibw;
2308 #endif
2310 FRAME_MENUBAR_HEIGHT (f) = menubar_size;
2312 #ifndef USE_LUCID
2313 /* Motif seems to need this amount added to the sizes
2314 specified for the shell widget. The Athena/Lucid widgets don't.
2315 Both conclusions reached experimentally. -- rms. */
2316 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
2317 &extra_borders, NULL);
2318 extra_borders *= 2;
2319 #endif
2321 f->shell_position = xmalloc (sizeof "=x++" + 4 * INT_STRLEN_BOUND (int));
2323 /* Convert our geometry parameters into a geometry string
2324 and specify it.
2325 Note that we do not specify here whether the position
2326 is a user-specified or program-specified one.
2327 We pass that information later, in x_wm_set_size_hints. */
2329 int left = f->left_pos;
2330 int xneg = window_prompting & XNegative;
2331 int top = f->top_pos;
2332 int yneg = window_prompting & YNegative;
2333 if (xneg)
2334 left = -left;
2335 if (yneg)
2336 top = -top;
2338 if (window_prompting & USPosition)
2339 sprintf (f->shell_position, "=%dx%d%c%d%c%d",
2340 FRAME_PIXEL_WIDTH (f) + extra_borders,
2341 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders,
2342 (xneg ? '-' : '+'), left,
2343 (yneg ? '-' : '+'), top);
2344 else
2346 sprintf (f->shell_position, "=%dx%d",
2347 FRAME_PIXEL_WIDTH (f) + extra_borders,
2348 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders);
2350 /* Setting x and y when the position is not specified in
2351 the geometry string will set program position in the WM hints.
2352 If Emacs had just one program position, we could set it in
2353 fallback resources, but since each make-frame call can specify
2354 different program positions, this is easier. */
2355 XtSetArg (gal[gac], XtNx, left); gac++;
2356 XtSetArg (gal[gac], XtNy, top); gac++;
2360 XtSetArg (gal[gac], XtNgeometry, f->shell_position); gac++;
2361 XtSetValues (shell_widget, gal, gac);
2364 XtManageChild (pane_widget);
2365 XtRealizeWidget (shell_widget);
2367 if (FRAME_X_EMBEDDED_P (f))
2368 XReparentWindow (FRAME_X_DISPLAY (f), XtWindow (shell_widget),
2369 f->output_data.x->parent_desc, 0, 0);
2371 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
2373 validate_x_resource_name ();
2375 class_hints.res_name = SSDATA (Vx_resource_name);
2376 class_hints.res_class = SSDATA (Vx_resource_class);
2377 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
2379 #ifdef HAVE_X_I18N
2380 FRAME_XIC (f) = NULL;
2381 if (use_xim)
2382 create_frame_xic (f);
2383 #endif
2385 f->output_data.x->wm_hints.input = True;
2386 f->output_data.x->wm_hints.flags |= InputHint;
2387 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2388 &f->output_data.x->wm_hints);
2390 hack_wm_protocols (f, shell_widget);
2392 #ifdef HACK_EDITRES
2393 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
2394 #endif
2396 /* Do a stupid property change to force the server to generate a
2397 PropertyNotify event so that the event_stream server timestamp will
2398 be initialized to something relevant to the time we created the window.
2400 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
2401 FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
2402 XA_ATOM, 32, PropModeAppend, NULL, 0);
2404 /* Make all the standard events reach the Emacs frame. */
2405 attributes.event_mask = STANDARD_EVENT_SET;
2407 #ifdef HAVE_X_I18N
2408 if (FRAME_XIC (f))
2410 /* XIM server might require some X events. */
2411 unsigned long fevent = NoEventMask;
2412 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2413 attributes.event_mask |= fevent;
2415 #endif /* HAVE_X_I18N */
2417 attribute_mask = CWEventMask;
2418 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
2419 attribute_mask, &attributes);
2421 XtMapWidget (frame_widget);
2423 /* x_set_name normally ignores requests to set the name if the
2424 requested name is the same as the current name. This is the one
2425 place where that assumption isn't correct; f->name is set, but
2426 the X server hasn't been told. */
2428 Lisp_Object name;
2429 bool explicit = f->explicit_name;
2431 f->explicit_name = 0;
2432 name = f->name;
2433 fset_name (f, Qnil);
2434 x_set_name (f, name, explicit);
2437 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2438 f->output_data.x->current_cursor
2439 = f->output_data.x->text_cursor);
2441 unblock_input ();
2443 /* This is a no-op, except under Motif. Make sure main areas are
2444 set to something reasonable, in case we get an error later. */
2445 lw_set_main_areas (pane_widget, 0, frame_widget);
2448 #else /* not USE_X_TOOLKIT */
2449 #ifdef USE_GTK
2450 static void
2451 x_window (struct frame *f)
2453 if (! xg_create_frame_widgets (f))
2454 error ("Unable to create window");
2456 #ifdef HAVE_X_I18N
2457 FRAME_XIC (f) = NULL;
2458 if (use_xim)
2460 block_input ();
2461 create_frame_xic (f);
2462 if (FRAME_XIC (f))
2464 /* XIM server might require some X events. */
2465 unsigned long fevent = NoEventMask;
2466 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2468 if (fevent != NoEventMask)
2470 XSetWindowAttributes attributes;
2471 XWindowAttributes wattr;
2472 unsigned long attribute_mask;
2474 XGetWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2475 &wattr);
2476 attributes.event_mask = wattr.your_event_mask | fevent;
2477 attribute_mask = CWEventMask;
2478 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2479 attribute_mask, &attributes);
2482 unblock_input ();
2484 #endif
2487 #else /*! USE_GTK */
2488 /* Create and set up the X window for frame F. */
2490 static void
2491 x_window (struct frame *f)
2493 XClassHint class_hints;
2494 XSetWindowAttributes attributes;
2495 unsigned long attribute_mask;
2497 attributes.background_pixel = FRAME_BACKGROUND_PIXEL (f);
2498 attributes.border_pixel = f->output_data.x->border_pixel;
2499 attributes.bit_gravity = StaticGravity;
2500 attributes.backing_store = NotUseful;
2501 attributes.save_under = True;
2502 attributes.event_mask = STANDARD_EVENT_SET;
2503 attributes.colormap = FRAME_X_COLORMAP (f);
2504 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
2505 | CWColormap);
2507 block_input ();
2508 FRAME_X_WINDOW (f)
2509 = XCreateWindow (FRAME_X_DISPLAY (f),
2510 f->output_data.x->parent_desc,
2511 f->left_pos,
2512 f->top_pos,
2513 FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f),
2514 f->border_width,
2515 CopyFromParent, /* depth */
2516 InputOutput, /* class */
2517 FRAME_X_VISUAL (f),
2518 attribute_mask, &attributes);
2520 #ifdef HAVE_X_I18N
2521 if (use_xim)
2523 create_frame_xic (f);
2524 if (FRAME_XIC (f))
2526 /* XIM server might require some X events. */
2527 unsigned long fevent = NoEventMask;
2528 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2529 attributes.event_mask |= fevent;
2530 attribute_mask = CWEventMask;
2531 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2532 attribute_mask, &attributes);
2535 #endif /* HAVE_X_I18N */
2537 validate_x_resource_name ();
2539 class_hints.res_name = SSDATA (Vx_resource_name);
2540 class_hints.res_class = SSDATA (Vx_resource_class);
2541 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
2543 /* This indicates that we use the "Passive Input" input model.
2544 Unless we do this, we don't get the Focus{In,Out} events that we
2545 need to draw the cursor correctly. Accursed bureaucrats.
2546 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
2548 f->output_data.x->wm_hints.input = True;
2549 f->output_data.x->wm_hints.flags |= InputHint;
2550 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2551 &f->output_data.x->wm_hints);
2552 f->output_data.x->wm_hints.icon_pixmap = None;
2554 /* Request "save yourself" and "delete window" commands from wm. */
2556 Atom protocols[2];
2557 protocols[0] = FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window;
2558 protocols[1] = FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
2559 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
2562 /* x_set_name normally ignores requests to set the name if the
2563 requested name is the same as the current name. This is the one
2564 place where that assumption isn't correct; f->name is set, but
2565 the X server hasn't been told. */
2567 Lisp_Object name;
2568 bool explicit = f->explicit_name;
2570 f->explicit_name = 0;
2571 name = f->name;
2572 fset_name (f, Qnil);
2573 x_set_name (f, name, explicit);
2576 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2577 f->output_data.x->current_cursor
2578 = f->output_data.x->text_cursor);
2580 unblock_input ();
2582 if (FRAME_X_WINDOW (f) == 0)
2583 error ("Unable to create window");
2586 #endif /* not USE_GTK */
2587 #endif /* not USE_X_TOOLKIT */
2589 /* Verify that the icon position args for this window are valid. */
2591 static void
2592 x_icon_verify (struct frame *f, Lisp_Object parms)
2594 Lisp_Object icon_x, icon_y;
2596 /* Set the position of the icon. Note that twm groups all
2597 icons in an icon window. */
2598 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2599 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2600 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2602 CHECK_NUMBER (icon_x);
2603 CHECK_NUMBER (icon_y);
2605 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2606 error ("Both left and top icon corners of icon must be specified");
2609 /* Handle the icon stuff for this window. Perhaps later we might
2610 want an x_set_icon_position which can be called interactively as
2611 well. */
2613 static void
2614 x_icon (struct frame *f, Lisp_Object parms)
2616 Lisp_Object icon_x, icon_y;
2617 #if 0
2618 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2619 #endif
2621 /* Set the position of the icon. Note that twm groups all
2622 icons in an icon window. */
2623 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2624 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2625 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2627 CHECK_TYPE_RANGED_INTEGER (int, icon_x);
2628 CHECK_TYPE_RANGED_INTEGER (int, icon_y);
2630 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2631 error ("Both left and top icon corners of icon must be specified");
2633 block_input ();
2635 if (! EQ (icon_x, Qunbound))
2636 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
2638 #if 0 /* x_get_arg removes the visibility parameter as a side effect,
2639 but x_create_frame still needs it. */
2640 /* Start up iconic or window? */
2641 x_wm_set_window_state
2642 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
2643 Qicon)
2644 ? IconicState
2645 : NormalState));
2646 #endif
2648 x_text_icon (f, SSDATA ((!NILP (f->icon_name)
2649 ? f->icon_name
2650 : f->name)));
2652 unblock_input ();
2655 /* Make the GCs needed for this window, setting the
2656 background, border and mouse colors; also create the
2657 mouse cursor and the gray border tile. */
2659 static void
2660 x_make_gc (struct frame *f)
2662 XGCValues gc_values;
2664 block_input ();
2666 /* Create the GCs of this frame.
2667 Note that many default values are used. */
2669 gc_values.foreground = FRAME_FOREGROUND_PIXEL (f);
2670 gc_values.background = FRAME_BACKGROUND_PIXEL (f);
2671 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
2672 f->output_data.x->normal_gc
2673 = XCreateGC (FRAME_X_DISPLAY (f),
2674 FRAME_X_WINDOW (f),
2675 GCLineWidth | GCForeground | GCBackground,
2676 &gc_values);
2678 /* Reverse video style. */
2679 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2680 gc_values.background = FRAME_FOREGROUND_PIXEL (f);
2681 f->output_data.x->reverse_gc
2682 = XCreateGC (FRAME_X_DISPLAY (f),
2683 FRAME_X_WINDOW (f),
2684 GCForeground | GCBackground | GCLineWidth,
2685 &gc_values);
2687 /* Cursor has cursor-color background, background-color foreground. */
2688 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2689 gc_values.background = f->output_data.x->cursor_pixel;
2690 gc_values.fill_style = FillOpaqueStippled;
2691 f->output_data.x->cursor_gc
2692 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2693 (GCForeground | GCBackground
2694 | GCFillStyle | GCLineWidth),
2695 &gc_values);
2697 /* Create the gray border tile used when the pointer is not in
2698 the frame. Since this depends on the frame's pixel values,
2699 this must be done on a per-frame basis. */
2700 f->output_data.x->border_tile
2701 = (XCreatePixmapFromBitmapData
2702 (FRAME_X_DISPLAY (f), FRAME_DISPLAY_INFO (f)->root_window,
2703 gray_bits, gray_width, gray_height,
2704 FRAME_FOREGROUND_PIXEL (f),
2705 FRAME_BACKGROUND_PIXEL (f),
2706 DefaultDepth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f))));
2708 unblock_input ();
2712 /* Free what was allocated in x_make_gc. */
2714 void
2715 x_free_gcs (struct frame *f)
2717 Display *dpy = FRAME_X_DISPLAY (f);
2719 block_input ();
2721 if (f->output_data.x->normal_gc)
2723 XFreeGC (dpy, f->output_data.x->normal_gc);
2724 f->output_data.x->normal_gc = 0;
2727 if (f->output_data.x->reverse_gc)
2729 XFreeGC (dpy, f->output_data.x->reverse_gc);
2730 f->output_data.x->reverse_gc = 0;
2733 if (f->output_data.x->cursor_gc)
2735 XFreeGC (dpy, f->output_data.x->cursor_gc);
2736 f->output_data.x->cursor_gc = 0;
2739 if (f->output_data.x->border_tile)
2741 XFreePixmap (dpy, f->output_data.x->border_tile);
2742 f->output_data.x->border_tile = 0;
2745 unblock_input ();
2749 /* Handler for signals raised during x_create_frame and
2750 x_create_tip_frame. FRAME is the frame which is partially
2751 constructed. */
2753 static Lisp_Object
2754 unwind_create_frame (Lisp_Object frame)
2756 struct frame *f = XFRAME (frame);
2758 /* If frame is already dead, nothing to do. This can happen if the
2759 display is disconnected after the frame has become official, but
2760 before x_create_frame removes the unwind protect. */
2761 if (!FRAME_LIVE_P (f))
2762 return Qnil;
2764 /* If frame is ``official'', nothing to do. */
2765 if (NILP (Fmemq (frame, Vframe_list)))
2767 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2768 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2769 #endif
2771 x_free_frame_resources (f);
2772 free_glyphs (f);
2774 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2775 /* Check that reference counts are indeed correct. */
2776 eassert (dpyinfo->reference_count == dpyinfo_refcount);
2777 eassert (dpyinfo->terminal->image_cache->refcount == image_cache_refcount);
2778 #endif
2779 return Qt;
2782 return Qnil;
2785 static void
2786 do_unwind_create_frame (Lisp_Object frame)
2788 unwind_create_frame (frame);
2791 static void
2792 x_default_font_parameter (struct frame *f, Lisp_Object parms)
2794 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2795 Lisp_Object font_param = x_get_arg (dpyinfo, parms, Qfont, NULL, NULL,
2796 RES_TYPE_STRING);
2797 Lisp_Object font = Qnil;
2798 if (EQ (font_param, Qunbound))
2799 font_param = Qnil;
2801 if (NILP (font_param))
2803 /* System font should take precedence over X resources. We suggest this
2804 regardless of font-use-system-font because .emacs may not have been
2805 read yet. */
2806 const char *system_font = xsettings_get_system_font ();
2807 if (system_font)
2808 font = font_open_by_name (f, build_unibyte_string (system_font));
2811 if (NILP (font))
2812 font = !NILP (font_param) ? font_param
2813 : x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
2815 if (! FONTP (font) && ! STRINGP (font))
2817 const char *names[]
2819 #ifdef HAVE_XFT
2820 /* This will find the normal Xft font. */
2821 "monospace-10",
2822 #endif
2823 "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1",
2824 "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2825 "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2826 /* This was formerly the first thing tried, but it finds
2827 too many fonts and takes too long. */
2828 "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1",
2829 /* If those didn't work, look for something which will
2830 at least work. */
2831 "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1",
2832 "fixed",
2833 NULL };
2834 int i;
2836 for (i = 0; names[i]; i++)
2838 font = font_open_by_name (f, build_unibyte_string (names[i]));
2839 if (! NILP (font))
2840 break;
2842 if (NILP (font))
2843 error ("No suitable font was found");
2845 else if (!NILP (font_param))
2847 /* Remember the explicit font parameter, so we can re-apply it after
2848 we've applied the `default' face settings. */
2849 x_set_frame_parameters (f, list1 (Fcons (Qfont_param, font_param)));
2852 /* This call will make X resources override any system font setting. */
2853 x_default_parameter (f, parms, Qfont, font, "font", "Font", RES_TYPE_STRING);
2857 DEFUN ("x-wm-set-size-hint", Fx_wm_set_size_hint, Sx_wm_set_size_hint,
2858 0, 1, 0,
2859 doc: /* Send the size hints for frame FRAME to the window manager.
2860 If FRAME is omitted or nil, use the selected frame.
2861 Signal error if FRAME is not an X frame. */)
2862 (Lisp_Object frame)
2864 struct frame *f = decode_window_system_frame (frame);
2866 block_input ();
2867 x_wm_set_size_hint (f, 0, 0);
2868 unblock_input ();
2869 return Qnil;
2872 static void
2873 set_machine_and_pid_properties (struct frame *f)
2875 /* This will set WM_CLIENT_MACHINE and WM_LOCALE_NAME. */
2876 XSetWMProperties (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), NULL, NULL,
2877 NULL, 0, NULL, NULL, NULL);
2878 pid_t pid = getpid ();
2879 if (pid <= 0xffffffffu)
2881 unsigned long xpid = pid;
2882 XChangeProperty (FRAME_X_DISPLAY (f),
2883 FRAME_OUTER_WINDOW (f),
2884 XInternAtom (FRAME_X_DISPLAY (f),
2885 "_NET_WM_PID",
2886 False),
2887 XA_CARDINAL, 32, PropModeReplace,
2888 (unsigned char *) &xpid, 1);
2892 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
2893 1, 1, 0,
2894 doc: /* Make a new X window, which is called a "frame" in Emacs terms.
2895 Return an Emacs frame object. PARMS is an alist of frame parameters.
2896 If the parameters specify that the frame should not have a minibuffer,
2897 and do not specify a specific minibuffer window to use, then
2898 `default-minibuffer-frame' must be a frame whose minibuffer can be
2899 shared by the new frame.
2901 This function is an internal primitive--use `make-frame' instead. */)
2902 (Lisp_Object parms)
2904 struct frame *f;
2905 Lisp_Object frame, tem;
2906 Lisp_Object name;
2907 int minibuffer_only = 0;
2908 long window_prompting = 0;
2909 ptrdiff_t count = SPECPDL_INDEX ();
2910 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
2911 Lisp_Object display;
2912 struct x_display_info *dpyinfo = NULL;
2913 Lisp_Object parent;
2914 struct kboard *kb;
2916 parms = Fcopy_alist (parms);
2918 /* Use this general default value to start with
2919 until we know if this frame has a specified name. */
2920 Vx_resource_name = Vinvocation_name;
2922 display = x_get_arg (dpyinfo, parms, Qterminal, 0, 0, RES_TYPE_NUMBER);
2923 if (EQ (display, Qunbound))
2924 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
2925 if (EQ (display, Qunbound))
2926 display = Qnil;
2927 dpyinfo = check_x_display_info (display);
2928 kb = dpyinfo->terminal->kboard;
2930 if (!dpyinfo->terminal->name)
2931 error ("Terminal is not live, can't create new frames on it");
2933 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
2934 if (!STRINGP (name)
2935 && ! EQ (name, Qunbound)
2936 && ! NILP (name))
2937 error ("Invalid frame name--not a string or nil");
2939 if (STRINGP (name))
2940 Vx_resource_name = name;
2942 /* See if parent window is specified. */
2943 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
2944 if (EQ (parent, Qunbound))
2945 parent = Qnil;
2946 if (! NILP (parent))
2947 CHECK_NUMBER (parent);
2949 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
2950 /* No need to protect DISPLAY because that's not used after passing
2951 it to make_frame_without_minibuffer. */
2952 frame = Qnil;
2953 GCPRO4 (parms, parent, name, frame);
2954 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
2955 RES_TYPE_SYMBOL);
2956 if (EQ (tem, Qnone) || NILP (tem))
2957 f = make_frame_without_minibuffer (Qnil, kb, display);
2958 else if (EQ (tem, Qonly))
2960 f = make_minibuffer_frame ();
2961 minibuffer_only = 1;
2963 else if (WINDOWP (tem))
2964 f = make_frame_without_minibuffer (tem, kb, display);
2965 else
2966 f = make_frame (1);
2968 XSETFRAME (frame, f);
2970 f->terminal = dpyinfo->terminal;
2972 f->output_method = output_x_window;
2973 f->output_data.x = xzalloc (sizeof *f->output_data.x);
2974 f->output_data.x->icon_bitmap = -1;
2975 FRAME_FONTSET (f) = -1;
2976 f->output_data.x->scroll_bar_foreground_pixel = -1;
2977 f->output_data.x->scroll_bar_background_pixel = -1;
2978 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
2979 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
2980 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
2981 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
2982 f->output_data.x->white_relief.pixel = -1;
2983 f->output_data.x->black_relief.pixel = -1;
2985 fset_icon_name (f,
2986 x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
2987 RES_TYPE_STRING));
2988 if (! STRINGP (f->icon_name))
2989 fset_icon_name (f, Qnil);
2991 FRAME_DISPLAY_INFO (f) = dpyinfo;
2993 /* With FRAME_DISPLAY_INFO set up, this unwind-protect is safe. */
2994 record_unwind_protect (do_unwind_create_frame, frame);
2996 /* These colors will be set anyway later, but it's important
2997 to get the color reference counts right, so initialize them! */
2999 Lisp_Object black;
3000 struct gcpro gcpro1;
3002 /* Function x_decode_color can signal an error. Make
3003 sure to initialize color slots so that we won't try
3004 to free colors we haven't allocated. */
3005 FRAME_FOREGROUND_PIXEL (f) = -1;
3006 FRAME_BACKGROUND_PIXEL (f) = -1;
3007 f->output_data.x->cursor_pixel = -1;
3008 f->output_data.x->cursor_foreground_pixel = -1;
3009 f->output_data.x->border_pixel = -1;
3010 f->output_data.x->mouse_pixel = -1;
3012 black = build_string ("black");
3013 GCPRO1 (black);
3014 FRAME_FOREGROUND_PIXEL (f)
3015 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3016 FRAME_BACKGROUND_PIXEL (f)
3017 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3018 f->output_data.x->cursor_pixel
3019 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3020 f->output_data.x->cursor_foreground_pixel
3021 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3022 f->output_data.x->border_pixel
3023 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3024 f->output_data.x->mouse_pixel
3025 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3026 UNGCPRO;
3029 /* Specify the parent under which to make this X window. */
3030 if (!NILP (parent))
3032 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
3033 f->output_data.x->explicit_parent = 1;
3035 else
3037 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
3038 f->output_data.x->explicit_parent = 0;
3041 /* Set the name; the functions to which we pass f expect the name to
3042 be set. */
3043 if (EQ (name, Qunbound) || NILP (name))
3045 fset_name (f, build_string (dpyinfo->x_id_name));
3046 f->explicit_name = 0;
3048 else
3050 fset_name (f, name);
3051 f->explicit_name = 1;
3052 /* Use the frame's title when getting resources for this frame. */
3053 specbind (Qx_resource_name, name);
3056 #ifdef HAVE_FREETYPE
3057 #ifdef HAVE_XFT
3058 register_font_driver (&xftfont_driver, f);
3059 #else /* not HAVE_XFT */
3060 register_font_driver (&ftxfont_driver, f);
3061 #endif /* not HAVE_XFT */
3062 #endif /* HAVE_FREETYPE */
3063 register_font_driver (&xfont_driver, f);
3065 x_default_parameter (f, parms, Qfont_backend, Qnil,
3066 "fontBackend", "FontBackend", RES_TYPE_STRING);
3068 /* Extract the window parameters from the supplied values
3069 that are needed to determine window geometry. */
3070 x_default_font_parameter (f, parms);
3071 if (!FRAME_FONT (f))
3073 delete_frame (frame, Qnoelisp);
3074 error ("Invalid frame font");
3077 /* Frame contents get displaced if an embedded X window has a border. */
3078 if (! FRAME_X_EMBEDDED_P (f))
3079 x_default_parameter (f, parms, Qborder_width, make_number (0),
3080 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
3082 /* This defaults to 1 in order to match xterm. We recognize either
3083 internalBorderWidth or internalBorder (which is what xterm calls
3084 it). */
3085 if (NILP (Fassq (Qinternal_border_width, parms)))
3087 Lisp_Object value;
3089 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
3090 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
3091 if (! EQ (value, Qunbound))
3092 parms = Fcons (Fcons (Qinternal_border_width, value),
3093 parms);
3095 x_default_parameter (f, parms, Qinternal_border_width,
3096 #ifdef USE_GTK /* We used to impose 0 in xg_create_frame_widgets. */
3097 make_number (0),
3098 #else
3099 make_number (1),
3100 #endif
3101 "internalBorderWidth", "internalBorderWidth",
3102 RES_TYPE_NUMBER);
3103 x_default_parameter (f, parms, Qright_divider_width, make_number (0),
3104 NULL, NULL, RES_TYPE_NUMBER);
3105 x_default_parameter (f, parms, Qbottom_divider_width, make_number (0),
3106 NULL, NULL, RES_TYPE_NUMBER);
3107 x_default_parameter (f, parms, Qvertical_scroll_bars,
3108 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3109 Qright,
3110 #else
3111 Qleft,
3112 #endif
3113 "verticalScrollBars", "ScrollBars",
3114 RES_TYPE_SYMBOL);
3115 x_default_parameter (f, parms, Qhorizontal_scroll_bars,
3116 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3118 #else
3119 Qnil,
3120 #endif
3121 "horizontalScrollBars", "ScrollBars",
3122 RES_TYPE_SYMBOL);
3124 /* Also do the stuff which must be set before the window exists. */
3125 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
3126 "foreground", "Foreground", RES_TYPE_STRING);
3127 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
3128 "background", "Background", RES_TYPE_STRING);
3129 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
3130 "pointerColor", "Foreground", RES_TYPE_STRING);
3131 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
3132 "borderColor", "BorderColor", RES_TYPE_STRING);
3133 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
3134 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
3135 x_default_parameter (f, parms, Qline_spacing, Qnil,
3136 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
3137 x_default_parameter (f, parms, Qleft_fringe, Qnil,
3138 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
3139 x_default_parameter (f, parms, Qright_fringe, Qnil,
3140 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
3142 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
3143 "scrollBarForeground",
3144 "ScrollBarForeground", 1);
3145 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
3146 "scrollBarBackground",
3147 "ScrollBarBackground", 0);
3149 #ifdef GLYPH_DEBUG
3150 image_cache_refcount =
3151 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
3152 dpyinfo_refcount = dpyinfo->reference_count;
3153 #endif /* GLYPH_DEBUG */
3155 /* Init faces before x_default_parameter is called for the
3156 scroll-bar-width parameter because otherwise we end up in
3157 init_iterator with a null face cache, which should not happen. */
3158 init_frame_faces (f);
3160 /* The following call of change_frame_size is needed since otherwise
3161 x_set_tool_bar_lines will already work with the character sizes
3162 installed by init_frame_faces while the frame's pixel size is
3163 still calculated from a character size of 1 and we subsequently
3164 hit the (height >= 0) assertion in window_box_height.
3166 The non-pixelwise code apparently worked around this because it
3167 had one frame line vs one toolbar line which left us with a zero
3168 root window height which was obviously wrong as well ... */
3169 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
3170 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 5, 1);
3172 /* Set the menu-bar-lines and tool-bar-lines parameters. We don't
3173 look up the X resources controlling the menu-bar and tool-bar
3174 here; they are processed specially at startup, and reflected in
3175 the values of the mode variables. */
3177 x_default_parameter (f, parms, Qmenu_bar_lines,
3178 NILP (Vmenu_bar_mode)
3179 ? make_number (0) : make_number (1),
3180 NULL, NULL, RES_TYPE_NUMBER);
3181 x_default_parameter (f, parms, Qtool_bar_lines,
3182 NILP (Vtool_bar_mode)
3183 ? make_number (0) : make_number (1),
3184 NULL, NULL, RES_TYPE_NUMBER);
3186 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
3187 "bufferPredicate", "BufferPredicate",
3188 RES_TYPE_SYMBOL);
3189 x_default_parameter (f, parms, Qtitle, Qnil,
3190 "title", "Title", RES_TYPE_STRING);
3191 x_default_parameter (f, parms, Qwait_for_wm, Qt,
3192 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN);
3193 x_default_parameter (f, parms, Qfullscreen, Qnil,
3194 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
3195 x_default_parameter (f, parms, Qtool_bar_position,
3196 FRAME_TOOL_BAR_POSITION (f), 0, 0, RES_TYPE_SYMBOL);
3198 /* Compute the size of the X window. */
3199 window_prompting = x_figure_window_size (f, parms, 1);
3201 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
3202 f->no_split = minibuffer_only || EQ (tem, Qt);
3204 x_icon_verify (f, parms);
3206 /* Create the X widget or window. */
3207 #ifdef USE_X_TOOLKIT
3208 x_window (f, window_prompting, minibuffer_only);
3209 #else
3210 x_window (f);
3211 #endif
3213 x_icon (f, parms);
3214 x_make_gc (f);
3216 /* Now consider the frame official. */
3217 f->terminal->reference_count++;
3218 FRAME_DISPLAY_INFO (f)->reference_count++;
3219 Vframe_list = Fcons (frame, Vframe_list);
3221 /* We need to do this after creating the X window, so that the
3222 icon-creation functions can say whose icon they're describing. */
3223 x_default_parameter (f, parms, Qicon_type, Qt,
3224 "bitmapIcon", "BitmapIcon", RES_TYPE_BOOLEAN);
3226 x_default_parameter (f, parms, Qauto_raise, Qnil,
3227 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3228 x_default_parameter (f, parms, Qauto_lower, Qnil,
3229 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3230 x_default_parameter (f, parms, Qcursor_type, Qbox,
3231 "cursorType", "CursorType", RES_TYPE_SYMBOL);
3232 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
3233 "scrollBarWidth", "ScrollBarWidth",
3234 RES_TYPE_NUMBER);
3235 x_default_parameter (f, parms, Qscroll_bar_height, Qnil,
3236 "scrollBarHeight", "ScrollBarHeight",
3237 RES_TYPE_NUMBER);
3238 x_default_parameter (f, parms, Qalpha, Qnil,
3239 "alpha", "Alpha", RES_TYPE_NUMBER);
3241 /* Consider frame official, now. */
3242 f->official = true;
3244 adjust_frame_size (f, FRAME_TEXT_WIDTH (f), FRAME_TEXT_HEIGHT (f), 0, 1);
3246 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3247 /* Create the menu bar. */
3248 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3250 /* If this signals an error, we haven't set size hints for the
3251 frame and we didn't make it visible. */
3252 initialize_frame_menubar (f);
3254 #ifndef USE_GTK
3255 /* This is a no-op, except under Motif where it arranges the
3256 main window for the widgets on it. */
3257 lw_set_main_areas (f->output_data.x->column_widget,
3258 f->output_data.x->menubar_widget,
3259 f->output_data.x->edit_widget);
3260 #endif /* not USE_GTK */
3262 #endif /* USE_X_TOOLKIT || USE_GTK */
3264 /* Tell the server what size and position, etc, we want, and how
3265 badly we want them. This should be done after we have the menu
3266 bar so that its size can be taken into account. */
3267 block_input ();
3268 x_wm_set_size_hint (f, window_prompting, 0);
3269 unblock_input ();
3271 /* Make the window appear on the frame and enable display, unless
3272 the caller says not to. However, with explicit parent, Emacs
3273 cannot control visibility, so don't try. */
3274 if (! f->output_data.x->explicit_parent)
3276 Lisp_Object visibility;
3278 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
3279 RES_TYPE_SYMBOL);
3280 if (EQ (visibility, Qunbound))
3281 visibility = Qt;
3283 if (EQ (visibility, Qicon))
3284 x_iconify_frame (f);
3285 else if (! NILP (visibility))
3286 x_make_frame_visible (f);
3287 else
3289 /* Must have been Qnil. */
3293 block_input ();
3295 /* Set machine name and pid for the purpose of window managers. */
3296 set_machine_and_pid_properties (f);
3298 /* Set the WM leader property. GTK does this itself, so this is not
3299 needed when using GTK. */
3300 if (dpyinfo->client_leader_window != 0)
3302 XChangeProperty (FRAME_X_DISPLAY (f),
3303 FRAME_OUTER_WINDOW (f),
3304 dpyinfo->Xatom_wm_client_leader,
3305 XA_WINDOW, 32, PropModeReplace,
3306 (unsigned char *) &dpyinfo->client_leader_window, 1);
3309 unblock_input ();
3311 /* Initialize `default-minibuffer-frame' in case this is the first
3312 frame on this terminal. */
3313 if (FRAME_HAS_MINIBUF_P (f)
3314 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
3315 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
3316 kset_default_minibuffer_frame (kb, frame);
3318 /* All remaining specified parameters, which have not been "used"
3319 by x_get_arg and friends, now go in the misc. alist of the frame. */
3320 for (tem = parms; CONSP (tem); tem = XCDR (tem))
3321 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
3322 fset_param_alist (f, Fcons (XCAR (tem), f->param_alist));
3324 UNGCPRO;
3326 /* Make sure windows on this frame appear in calls to next-window
3327 and similar functions. */
3328 Vwindow_list = Qnil;
3330 return unbind_to (count, frame);
3334 /* FRAME is used only to get a handle on the X display. We don't pass the
3335 display info directly because we're called from frame.c, which doesn't
3336 know about that structure. */
3338 Lisp_Object
3339 x_get_focus_frame (struct frame *frame)
3341 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (frame);
3342 Lisp_Object xfocus;
3343 if (! dpyinfo->x_focus_frame)
3344 return Qnil;
3346 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
3347 return xfocus;
3351 /* In certain situations, when the window manager follows a
3352 click-to-focus policy, there seems to be no way around calling
3353 XSetInputFocus to give another frame the input focus .
3355 In an ideal world, XSetInputFocus should generally be avoided so
3356 that applications don't interfere with the window manager's focus
3357 policy. But I think it's okay to use when it's clearly done
3358 following a user-command. */
3360 void
3361 x_focus_frame (struct frame *f)
3363 Display *dpy = FRAME_X_DISPLAY (f);
3365 block_input ();
3366 x_catch_errors (dpy);
3368 if (FRAME_X_EMBEDDED_P (f))
3370 /* For Xembedded frames, normally the embedder forwards key
3371 events. See XEmbed Protocol Specification at
3372 http://freedesktop.org/wiki/Specifications/xembed-spec */
3373 xembed_request_focus (f);
3375 else
3377 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3378 RevertToParent, CurrentTime);
3379 x_ewmh_activate_frame (f);
3382 x_uncatch_errors ();
3383 unblock_input ();
3387 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
3388 doc: /* Internal function called by `color-defined-p', which see
3389 .\(Note that the Nextstep version of this function ignores FRAME.) */)
3390 (Lisp_Object color, Lisp_Object frame)
3392 XColor foo;
3393 struct frame *f = decode_window_system_frame (frame);
3395 CHECK_STRING (color);
3397 if (x_defined_color (f, SSDATA (color), &foo, 0))
3398 return Qt;
3399 else
3400 return Qnil;
3403 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
3404 doc: /* Internal function called by `color-values', which see. */)
3405 (Lisp_Object color, Lisp_Object frame)
3407 XColor foo;
3408 struct frame *f = decode_window_system_frame (frame);
3410 CHECK_STRING (color);
3412 if (x_defined_color (f, SSDATA (color), &foo, 0))
3413 return list3i (foo.red, foo.green, foo.blue);
3414 else
3415 return Qnil;
3418 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
3419 doc: /* Internal function called by `display-color-p', which see. */)
3420 (Lisp_Object terminal)
3422 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3424 if (dpyinfo->n_planes <= 2)
3425 return Qnil;
3427 switch (dpyinfo->visual->class)
3429 case StaticColor:
3430 case PseudoColor:
3431 case TrueColor:
3432 case DirectColor:
3433 return Qt;
3435 default:
3436 return Qnil;
3440 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
3441 0, 1, 0,
3442 doc: /* Return t if the X display supports shades of gray.
3443 Note that color displays do support shades of gray.
3444 The optional argument TERMINAL specifies which display to ask about.
3445 TERMINAL should be a terminal object, a frame or a display name (a string).
3446 If omitted or nil, that stands for the selected frame's display. */)
3447 (Lisp_Object terminal)
3449 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3451 if (dpyinfo->n_planes <= 1)
3452 return Qnil;
3454 switch (dpyinfo->visual->class)
3456 case StaticColor:
3457 case PseudoColor:
3458 case TrueColor:
3459 case DirectColor:
3460 case StaticGray:
3461 case GrayScale:
3462 return Qt;
3464 default:
3465 return Qnil;
3469 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
3470 0, 1, 0,
3471 doc: /* Return the width in pixels of the X display TERMINAL.
3472 The optional argument TERMINAL specifies which display to ask about.
3473 TERMINAL should be a terminal object, a frame or a display name (a string).
3474 If omitted or nil, that stands for the selected frame's display.
3476 On \"multi-monitor\" setups this refers to the pixel width for all
3477 physical monitors associated with TERMINAL. To get information for
3478 each physical monitor, use `display-monitor-attributes-list'. */)
3479 (Lisp_Object terminal)
3481 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3483 return make_number (x_display_pixel_width (dpyinfo));
3486 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
3487 Sx_display_pixel_height, 0, 1, 0,
3488 doc: /* Return the height in pixels of the X display TERMINAL.
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.
3493 On \"multi-monitor\" setups this refers to the pixel height for all
3494 physical monitors associated with TERMINAL. To get information for
3495 each physical monitor, use `display-monitor-attributes-list'. */)
3496 (Lisp_Object terminal)
3498 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3500 return make_number (x_display_pixel_height (dpyinfo));
3503 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
3504 0, 1, 0,
3505 doc: /* Return the number of bitplanes of the X display TERMINAL.
3506 The optional argument TERMINAL specifies which display to ask about.
3507 TERMINAL should be a terminal object, a frame or a display name (a string).
3508 If omitted or nil, that stands for the selected frame's display. */)
3509 (Lisp_Object terminal)
3511 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3513 return make_number (dpyinfo->n_planes);
3516 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
3517 0, 1, 0,
3518 doc: /* Return the number of color cells of the X display TERMINAL.
3519 The optional argument TERMINAL specifies which display to ask about.
3520 TERMINAL should be a terminal object, a frame or a display name (a string).
3521 If omitted or nil, that stands for the selected frame's display. */)
3522 (Lisp_Object terminal)
3524 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3526 int nr_planes = DisplayPlanes (dpyinfo->display,
3527 XScreenNumberOfScreen (dpyinfo->screen));
3529 /* Truncate nr_planes to 24 to avoid integer overflow.
3530 Some displays says 32, but only 24 bits are actually significant.
3531 There are only very few and rare video cards that have more than
3532 24 significant bits. Also 24 bits is more than 16 million colors,
3533 it "should be enough for everyone". */
3534 if (nr_planes > 24) nr_planes = 24;
3536 return make_number (1 << nr_planes);
3539 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
3540 Sx_server_max_request_size,
3541 0, 1, 0,
3542 doc: /* Return the maximum request size of the X server of display TERMINAL.
3543 The optional argument TERMINAL specifies which display to ask about.
3544 TERMINAL should be a terminal object, a frame or a display name (a string).
3545 If omitted or nil, that stands for the selected frame's display. */)
3546 (Lisp_Object terminal)
3548 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3550 return make_number (MAXREQUEST (dpyinfo->display));
3553 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
3554 doc: /* Return the "vendor ID" string of the X server of display TERMINAL.
3555 \(Labeling every distributor as a "vendor" embodies the false assumption
3556 that operating systems cannot be developed and distributed noncommercially.)
3557 The optional argument TERMINAL specifies which display to ask about.
3558 TERMINAL should be a terminal object, a frame or a display name (a string).
3559 If omitted or nil, that stands for the selected frame's display. */)
3560 (Lisp_Object terminal)
3562 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3563 const char *vendor = ServerVendor (dpyinfo->display);
3565 if (! vendor) vendor = "";
3566 return build_string (vendor);
3569 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
3570 doc: /* Return the version numbers of the X server of display TERMINAL.
3571 The value is a list of three integers: the major and minor
3572 version numbers of the X Protocol in use, and the distributor-specific release
3573 number. See also the function `x-server-vendor'.
3575 The optional argument TERMINAL specifies which display to ask about.
3576 TERMINAL should be a terminal object, a frame or a display name (a string).
3577 If omitted or nil, that stands for the selected frame's display. */)
3578 (Lisp_Object terminal)
3580 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3581 Display *dpy = dpyinfo->display;
3583 return list3i (ProtocolVersion (dpy), ProtocolRevision (dpy),
3584 VendorRelease (dpy));
3587 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
3588 doc: /* Return the number of screens on the X server of display TERMINAL.
3589 The optional argument TERMINAL specifies which display to ask about.
3590 TERMINAL should be a terminal object, a frame or a display name (a string).
3591 If omitted or nil, that stands for the selected frame's display. */)
3592 (Lisp_Object terminal)
3594 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3596 return make_number (ScreenCount (dpyinfo->display));
3599 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
3600 doc: /* Return the height in millimeters of the X display TERMINAL.
3601 The optional argument TERMINAL specifies which display to ask about.
3602 TERMINAL should be a terminal object, a frame or a display name (a string).
3603 If omitted or nil, that stands for the selected frame's display.
3605 On \"multi-monitor\" setups this refers to the height in millimeters for
3606 all physical monitors associated with TERMINAL. To get information
3607 for each physical monitor, use `display-monitor-attributes-list'. */)
3608 (Lisp_Object terminal)
3610 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3612 return make_number (HeightMMOfScreen (dpyinfo->screen));
3615 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
3616 doc: /* Return the width in millimeters of the X display TERMINAL.
3617 The optional argument TERMINAL specifies which display to ask about.
3618 TERMINAL should be a terminal object, a frame or a display name (a string).
3619 If omitted or nil, that stands for the selected frame's display.
3621 On \"multi-monitor\" setups this refers to the width in millimeters for
3622 all physical monitors associated with TERMINAL. To get information
3623 for each physical monitor, use `display-monitor-attributes-list'. */)
3624 (Lisp_Object terminal)
3626 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3628 return make_number (WidthMMOfScreen (dpyinfo->screen));
3631 DEFUN ("x-display-backing-store", Fx_display_backing_store,
3632 Sx_display_backing_store, 0, 1, 0,
3633 doc: /* Return an indication of whether X display TERMINAL does backing store.
3634 The value may be `always', `when-mapped', or `not-useful'.
3635 The optional argument TERMINAL specifies which display to ask about.
3636 TERMINAL should be a terminal object, a frame or a display name (a string).
3637 If omitted or nil, that stands for the selected frame's display. */)
3638 (Lisp_Object terminal)
3640 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3641 Lisp_Object result;
3643 switch (DoesBackingStore (dpyinfo->screen))
3645 case Always:
3646 result = intern ("always");
3647 break;
3649 case WhenMapped:
3650 result = intern ("when-mapped");
3651 break;
3653 case NotUseful:
3654 result = intern ("not-useful");
3655 break;
3657 default:
3658 error ("Strange value for BackingStore parameter of screen");
3661 return result;
3664 DEFUN ("x-display-visual-class", Fx_display_visual_class,
3665 Sx_display_visual_class, 0, 1, 0,
3666 doc: /* Return the visual class of the X display TERMINAL.
3667 The value is one of the symbols `static-gray', `gray-scale',
3668 `static-color', `pseudo-color', `true-color', or `direct-color'.
3670 The optional argument TERMINAL specifies which display to ask about.
3671 TERMINAL should a terminal object, a frame or a display name (a string).
3672 If omitted or nil, that stands for the selected frame's display. */)
3673 (Lisp_Object terminal)
3675 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3676 Lisp_Object result;
3678 switch (dpyinfo->visual->class)
3680 case StaticGray:
3681 result = intern ("static-gray");
3682 break;
3683 case GrayScale:
3684 result = intern ("gray-scale");
3685 break;
3686 case StaticColor:
3687 result = intern ("static-color");
3688 break;
3689 case PseudoColor:
3690 result = intern ("pseudo-color");
3691 break;
3692 case TrueColor:
3693 result = intern ("true-color");
3694 break;
3695 case DirectColor:
3696 result = intern ("direct-color");
3697 break;
3698 default:
3699 error ("Display has an unknown visual class");
3702 return result;
3705 DEFUN ("x-display-save-under", Fx_display_save_under,
3706 Sx_display_save_under, 0, 1, 0,
3707 doc: /* Return t if the X display TERMINAL supports the save-under feature.
3708 The optional argument TERMINAL specifies which display to ask about.
3709 TERMINAL should be a terminal object, a frame or a display name (a string).
3710 If omitted or nil, that stands for the selected frame's display. */)
3711 (Lisp_Object terminal)
3713 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3715 if (DoesSaveUnders (dpyinfo->screen) == True)
3716 return Qt;
3717 else
3718 return Qnil;
3721 /* Store the geometry of the workarea on display DPYINFO into *RECT.
3722 Return false if and only if the workarea information cannot be
3723 obtained via the _NET_WORKAREA root window property. */
3725 #if ! GTK_CHECK_VERSION (3, 4, 0)
3726 static bool
3727 x_get_net_workarea (struct x_display_info *dpyinfo, XRectangle *rect)
3729 Display *dpy = dpyinfo->display;
3730 long offset, max_len;
3731 Atom target_type, actual_type;
3732 unsigned long actual_size, bytes_remaining;
3733 int rc, actual_format;
3734 unsigned char *tmp_data = NULL;
3735 bool result = false;
3737 x_catch_errors (dpy);
3738 offset = 0;
3739 max_len = 1;
3740 target_type = XA_CARDINAL;
3741 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3742 dpyinfo->Xatom_net_current_desktop,
3743 offset, max_len, False, target_type,
3744 &actual_type, &actual_format, &actual_size,
3745 &bytes_remaining, &tmp_data);
3746 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3747 && actual_format == 32 && actual_size == max_len)
3749 long current_desktop = ((long *) tmp_data)[0];
3751 XFree (tmp_data);
3752 tmp_data = NULL;
3754 offset = 4 * current_desktop;
3755 max_len = 4;
3756 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3757 dpyinfo->Xatom_net_workarea,
3758 offset, max_len, False, target_type,
3759 &actual_type, &actual_format, &actual_size,
3760 &bytes_remaining, &tmp_data);
3761 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3762 && actual_format == 32 && actual_size == max_len)
3764 long *values = (long *) tmp_data;
3766 rect->x = values[0];
3767 rect->y = values[1];
3768 rect->width = values[2];
3769 rect->height = values[3];
3771 XFree (tmp_data);
3772 tmp_data = NULL;
3774 result = true;
3777 if (tmp_data)
3778 XFree (tmp_data);
3779 x_uncatch_errors ();
3781 return result;
3783 #endif
3785 #ifndef USE_GTK
3787 /* Return monitor number where F is "most" or closest to. */
3788 static int
3789 x_get_monitor_for_frame (struct frame *f,
3790 struct MonitorInfo *monitors,
3791 int n_monitors)
3793 XRectangle frect;
3794 int area = 0, dist = -1;
3795 int best_area = -1, best_dist = -1;
3796 int i;
3798 if (n_monitors == 1) return 0;
3799 frect.x = f->left_pos;
3800 frect.y = f->top_pos;
3801 frect.width = FRAME_PIXEL_WIDTH (f);
3802 frect.height = FRAME_PIXEL_HEIGHT (f);
3804 for (i = 0; i < n_monitors; ++i)
3806 struct MonitorInfo *mi = &monitors[i];
3807 XRectangle res;
3808 int a = 0;
3810 if (mi->geom.width == 0) continue;
3812 if (x_intersect_rectangles (&mi->geom, &frect, &res))
3814 a = res.width * res.height;
3815 if (a > area)
3817 area = a;
3818 best_area = i;
3822 if (a == 0 && area == 0)
3824 int dx, dy, d;
3825 if (frect.x + frect.width < mi->geom.x)
3826 dx = mi->geom.x - frect.x + frect.width;
3827 else if (frect.x > mi->geom.x + mi->geom.width)
3828 dx = frect.x - mi->geom.x + mi->geom.width;
3829 else
3830 dx = 0;
3831 if (frect.y + frect.height < mi->geom.y)
3832 dy = mi->geom.y - frect.y + frect.height;
3833 else if (frect.y > mi->geom.y + mi->geom.height)
3834 dy = frect.y - mi->geom.y + mi->geom.height;
3835 else
3836 dy = 0;
3838 d = dx*dx + dy*dy;
3839 if (dist == -1 || dist > d)
3841 dist = d;
3842 best_dist = i;
3847 return best_area != -1 ? best_area : (best_dist != -1 ? best_dist : 0);
3850 static Lisp_Object
3851 x_make_monitor_attribute_list (struct MonitorInfo *monitors,
3852 int n_monitors,
3853 int primary_monitor,
3854 struct x_display_info *dpyinfo,
3855 const char *source)
3857 Lisp_Object monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
3858 Lisp_Object frame, rest;
3860 FOR_EACH_FRAME (rest, frame)
3862 struct frame *f = XFRAME (frame);
3864 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo
3865 && !EQ (frame, tip_frame))
3867 int i = x_get_monitor_for_frame (f, monitors, n_monitors);
3868 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
3872 return make_monitor_attribute_list (monitors, n_monitors, primary_monitor,
3873 monitor_frames, source);
3876 static Lisp_Object
3877 x_get_monitor_attributes_fallback (struct x_display_info *dpyinfo)
3879 struct MonitorInfo monitor;
3880 XRectangle workarea_r;
3882 /* Fallback: treat (possibly) multiple physical monitors as if they
3883 formed a single monitor as a whole. This should provide a
3884 consistent result at least on single monitor environments. */
3885 monitor.geom.x = monitor.geom.y = 0;
3886 monitor.geom.width = x_display_pixel_width (dpyinfo);
3887 monitor.geom.height = x_display_pixel_height (dpyinfo);
3888 monitor.mm_width = WidthMMOfScreen (dpyinfo->screen);
3889 monitor.mm_height = HeightMMOfScreen (dpyinfo->screen);
3890 monitor.name = xstrdup ("combined screen");
3892 if (x_get_net_workarea (dpyinfo, &workarea_r))
3893 monitor.work = workarea_r;
3894 else
3895 monitor.work = monitor.geom;
3896 return x_make_monitor_attribute_list (&monitor, 1, 0, dpyinfo, "fallback");
3900 #ifdef HAVE_XINERAMA
3901 static Lisp_Object
3902 x_get_monitor_attributes_xinerama (struct x_display_info *dpyinfo)
3904 int n_monitors, i;
3905 Lisp_Object attributes_list = Qnil;
3906 Display *dpy = dpyinfo->display;
3907 XineramaScreenInfo *info = XineramaQueryScreens (dpy, &n_monitors);
3908 struct MonitorInfo *monitors;
3909 double mm_width_per_pixel, mm_height_per_pixel;
3911 if (! info || n_monitors == 0)
3913 if (info)
3914 XFree (info);
3915 return attributes_list;
3918 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
3919 / x_display_pixel_width (dpyinfo));
3920 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
3921 / x_display_pixel_height (dpyinfo));
3922 monitors = xzalloc (n_monitors * sizeof *monitors);
3923 for (i = 0; i < n_monitors; ++i)
3925 struct MonitorInfo *mi = &monitors[i];
3926 XRectangle workarea_r;
3928 mi->geom.x = info[i].x_org;
3929 mi->geom.y = info[i].y_org;
3930 mi->geom.width = info[i].width;
3931 mi->geom.height = info[i].height;
3932 mi->mm_width = mi->geom.width * mm_width_per_pixel + 0.5;
3933 mi->mm_height = mi->geom.height * mm_height_per_pixel + 0.5;
3934 mi->name = 0;
3936 /* Xinerama usually have primary monitor first, just use that. */
3937 if (i == 0 && x_get_net_workarea (dpyinfo, &workarea_r))
3939 mi->work = workarea_r;
3940 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
3941 mi->work = mi->geom;
3943 else
3944 mi->work = mi->geom;
3946 XFree (info);
3948 attributes_list = x_make_monitor_attribute_list (monitors,
3949 n_monitors,
3951 dpyinfo,
3952 "Xinerama");
3953 free_monitors (monitors, n_monitors);
3954 return attributes_list;
3956 #endif /* HAVE_XINERAMA */
3959 #ifdef HAVE_XRANDR
3960 static Lisp_Object
3961 x_get_monitor_attributes_xrandr (struct x_display_info *dpyinfo)
3963 Lisp_Object attributes_list = Qnil;
3964 XRRScreenResources *resources;
3965 Display *dpy = dpyinfo->display;
3966 int i, n_monitors, primary = -1;
3967 RROutput pxid = None;
3968 struct MonitorInfo *monitors;
3970 #ifdef HAVE_XRRGETSCREENRESOURCESCURRENT
3971 resources = XRRGetScreenResourcesCurrent (dpy, dpyinfo->root_window);
3972 #else
3973 resources = XRRGetScreenResources (dpy, dpyinfo->root_window);
3974 #endif
3975 if (! resources || resources->noutput == 0)
3977 if (resources)
3978 XRRFreeScreenResources (resources);
3979 return Qnil;
3981 n_monitors = resources->noutput;
3982 monitors = xzalloc (n_monitors * sizeof *monitors);
3984 #ifdef HAVE_XRRGETOUTPUTPRIMARY
3985 pxid = XRRGetOutputPrimary (dpy, dpyinfo->root_window);
3986 #endif
3988 for (i = 0; i < n_monitors; ++i)
3990 XRROutputInfo *info = XRRGetOutputInfo (dpy, resources,
3991 resources->outputs[i]);
3992 Connection conn = info ? info->connection : RR_Disconnected;
3993 RRCrtc id = info ? info->crtc : None;
3995 if (strcmp (info->name, "default") == 0)
3997 /* Non XRandr 1.2 driver, does not give useful data. */
3998 XRRFreeOutputInfo (info);
3999 XRRFreeScreenResources (resources);
4000 free_monitors (monitors, n_monitors);
4001 return Qnil;
4004 if (conn != RR_Disconnected && id != None)
4006 XRRCrtcInfo *crtc = XRRGetCrtcInfo (dpy, resources, id);
4007 struct MonitorInfo *mi = &monitors[i];
4008 XRectangle workarea_r;
4010 if (! crtc)
4012 XRRFreeOutputInfo (info);
4013 continue;
4016 mi->geom.x = crtc->x;
4017 mi->geom.y = crtc->y;
4018 mi->geom.width = crtc->width;
4019 mi->geom.height = crtc->height;
4020 mi->mm_width = info->mm_width;
4021 mi->mm_height = info->mm_height;
4022 mi->name = xstrdup (info->name);
4024 if (pxid != None && pxid == resources->outputs[i])
4025 primary = i;
4026 else if (primary == -1 && strcmp (info->name, "LVDS") == 0)
4027 primary = i;
4029 if (i == primary && x_get_net_workarea (dpyinfo, &workarea_r))
4031 mi->work= workarea_r;
4032 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
4033 mi->work = mi->geom;
4035 else
4036 mi->work = mi->geom;
4038 XRRFreeCrtcInfo (crtc);
4040 XRRFreeOutputInfo (info);
4042 XRRFreeScreenResources (resources);
4044 attributes_list = x_make_monitor_attribute_list (monitors,
4045 n_monitors,
4046 primary,
4047 dpyinfo,
4048 "XRandr");
4049 free_monitors (monitors, n_monitors);
4050 return attributes_list;
4052 #endif /* HAVE_XRANDR */
4054 static Lisp_Object
4055 x_get_monitor_attributes (struct x_display_info *dpyinfo)
4057 Lisp_Object attributes_list = Qnil;
4058 Display *dpy = dpyinfo->display;
4060 (void) dpy; /* Suppress unused variable warning. */
4062 #ifdef HAVE_XRANDR
4063 int xrr_event_base, xrr_error_base;
4064 bool xrr_ok = false;
4065 xrr_ok = XRRQueryExtension (dpy, &xrr_event_base, &xrr_error_base);
4066 if (xrr_ok)
4068 int xrr_major, xrr_minor;
4069 XRRQueryVersion (dpy, &xrr_major, &xrr_minor);
4070 xrr_ok = (xrr_major == 1 && xrr_minor >= 2) || xrr_major > 1;
4073 if (xrr_ok)
4074 attributes_list = x_get_monitor_attributes_xrandr (dpyinfo);
4075 #endif /* HAVE_XRANDR */
4077 #ifdef HAVE_XINERAMA
4078 if (NILP (attributes_list))
4080 int xin_event_base, xin_error_base;
4081 bool xin_ok = false;
4082 xin_ok = XineramaQueryExtension (dpy, &xin_event_base, &xin_error_base);
4083 if (xin_ok && XineramaIsActive (dpy))
4084 attributes_list = x_get_monitor_attributes_xinerama (dpyinfo);
4086 #endif /* HAVE_XINERAMA */
4088 if (NILP (attributes_list))
4089 attributes_list = x_get_monitor_attributes_fallback (dpyinfo);
4091 return attributes_list;
4094 #endif /* !USE_GTK */
4096 DEFUN ("x-display-monitor-attributes-list", Fx_display_monitor_attributes_list,
4097 Sx_display_monitor_attributes_list,
4098 0, 1, 0,
4099 doc: /* Return a list of physical monitor attributes on the X display TERMINAL.
4101 The optional argument TERMINAL specifies which display to ask about.
4102 TERMINAL should be a terminal object, a frame or a display name (a string).
4103 If omitted or nil, that stands for the selected frame's display.
4105 In addition to the standard attribute keys listed in
4106 `display-monitor-attributes-list', the following keys are contained in
4107 the attributes:
4109 source -- String describing the source from which multi-monitor
4110 information is obtained, one of \"Gdk\", \"XRandr\",
4111 \"Xinerama\", or \"fallback\"
4113 Internal use only, use `display-monitor-attributes-list' instead. */)
4114 (Lisp_Object terminal)
4116 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4117 Lisp_Object attributes_list = Qnil;
4119 #ifdef USE_GTK
4120 double mm_width_per_pixel, mm_height_per_pixel;
4121 GdkDisplay *gdpy;
4122 GdkScreen *gscreen;
4123 gint primary_monitor = 0, n_monitors, i;
4124 Lisp_Object monitor_frames, rest, frame;
4125 static const char *source = "Gdk";
4126 struct MonitorInfo *monitors;
4128 block_input ();
4129 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
4130 / x_display_pixel_width (dpyinfo));
4131 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
4132 / x_display_pixel_height (dpyinfo));
4133 gdpy = gdk_x11_lookup_xdisplay (dpyinfo->display);
4134 gscreen = gdk_display_get_default_screen (gdpy);
4135 #if GTK_CHECK_VERSION (2, 20, 0)
4136 primary_monitor = gdk_screen_get_primary_monitor (gscreen);
4137 #endif
4138 n_monitors = gdk_screen_get_n_monitors (gscreen);
4139 monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
4140 monitors = xzalloc (n_monitors * sizeof *monitors);
4142 FOR_EACH_FRAME (rest, frame)
4144 struct frame *f = XFRAME (frame);
4146 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo
4147 && !EQ (frame, tip_frame))
4149 GdkWindow *gwin = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
4151 i = gdk_screen_get_monitor_at_window (gscreen, gwin);
4152 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
4156 for (i = 0; i < n_monitors; ++i)
4158 gint width_mm = -1, height_mm = -1;
4159 GdkRectangle rec, work;
4160 struct MonitorInfo *mi = &monitors[i];
4162 gdk_screen_get_monitor_geometry (gscreen, i, &rec);
4164 #if GTK_CHECK_VERSION (2, 14, 0)
4165 width_mm = gdk_screen_get_monitor_width_mm (gscreen, i);
4166 height_mm = gdk_screen_get_monitor_height_mm (gscreen, i);
4167 #endif
4168 if (width_mm < 0)
4169 width_mm = rec.width * mm_width_per_pixel + 0.5;
4170 if (height_mm < 0)
4171 height_mm = rec.height * mm_height_per_pixel + 0.5;
4173 #if GTK_CHECK_VERSION (3, 4, 0)
4174 gdk_screen_get_monitor_workarea (gscreen, i, &work);
4175 #else
4176 /* Emulate the behavior of GTK+ 3.4. */
4178 XRectangle workarea_r;
4180 if (i == primary_monitor && x_get_net_workarea (dpyinfo, &workarea_r))
4182 work.x = workarea_r.x;
4183 work.y = workarea_r.y;
4184 work.width = workarea_r.width;
4185 work.height = workarea_r.height;
4186 if (! gdk_rectangle_intersect (&rec, &work, &work))
4187 work = rec;
4189 else
4190 work = rec;
4192 #endif
4195 mi->geom.x = rec.x;
4196 mi->geom.y = rec.y;
4197 mi->geom.width = rec.width;
4198 mi->geom.height = rec.height;
4199 mi->work.x = work.x;
4200 mi->work.y = work.y;
4201 mi->work.width = work.width;
4202 mi->work.height = work.height;
4203 mi->mm_width = width_mm;
4204 mi->mm_height = height_mm;
4206 #if GTK_CHECK_VERSION (2, 14, 0)
4207 mi->name = gdk_screen_get_monitor_plug_name (gscreen, i);
4208 #endif
4211 attributes_list = make_monitor_attribute_list (monitors,
4212 n_monitors,
4213 primary_monitor,
4214 monitor_frames,
4215 source);
4216 unblock_input ();
4217 #else /* not USE_GTK */
4219 block_input ();
4220 attributes_list = x_get_monitor_attributes (dpyinfo);
4221 unblock_input ();
4223 #endif /* not USE_GTK */
4225 return attributes_list;
4228 /************************************************************************
4229 X Displays
4230 ************************************************************************/
4233 /* Mapping visual names to visuals. */
4235 static struct visual_class
4237 const char *name;
4238 int class;
4240 visual_classes[] =
4242 {"StaticGray", StaticGray},
4243 {"GrayScale", GrayScale},
4244 {"StaticColor", StaticColor},
4245 {"PseudoColor", PseudoColor},
4246 {"TrueColor", TrueColor},
4247 {"DirectColor", DirectColor},
4248 {NULL, 0}
4252 #ifndef HAVE_XSCREENNUMBEROFSCREEN
4254 /* Value is the screen number of screen SCR. This is a substitute for
4255 the X function with the same name when that doesn't exist. */
4258 XScreenNumberOfScreen (scr)
4259 register Screen *scr;
4261 Display *dpy = scr->display;
4262 int i;
4264 for (i = 0; i < dpy->nscreens; ++i)
4265 if (scr == dpy->screens + i)
4266 break;
4268 return i;
4271 #endif /* not HAVE_XSCREENNUMBEROFSCREEN */
4274 /* Select the visual that should be used on display DPYINFO. Set
4275 members of DPYINFO appropriately. Called from x_term_init. */
4277 void
4278 select_visual (struct x_display_info *dpyinfo)
4280 Display *dpy = dpyinfo->display;
4281 Screen *screen = dpyinfo->screen;
4282 Lisp_Object value;
4284 /* See if a visual is specified. */
4285 value = display_x_get_resource (dpyinfo,
4286 build_string ("visualClass"),
4287 build_string ("VisualClass"),
4288 Qnil, Qnil);
4289 if (STRINGP (value))
4291 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
4292 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
4293 depth, a decimal number. NAME is compared with case ignored. */
4294 char *s = alloca (SBYTES (value) + 1);
4295 char *dash;
4296 int i, class = -1;
4297 XVisualInfo vinfo;
4299 strcpy (s, SSDATA (value));
4300 dash = strchr (s, '-');
4301 if (dash)
4303 dpyinfo->n_planes = atoi (dash + 1);
4304 *dash = '\0';
4306 else
4307 /* We won't find a matching visual with depth 0, so that
4308 an error will be printed below. */
4309 dpyinfo->n_planes = 0;
4311 /* Determine the visual class. */
4312 for (i = 0; visual_classes[i].name; ++i)
4313 if (xstrcasecmp (s, visual_classes[i].name) == 0)
4315 class = visual_classes[i].class;
4316 break;
4319 /* Look up a matching visual for the specified class. */
4320 if (class == -1
4321 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
4322 dpyinfo->n_planes, class, &vinfo))
4323 fatal ("Invalid visual specification `%s'", SDATA (value));
4325 dpyinfo->visual = vinfo.visual;
4327 else
4329 int n_visuals;
4330 XVisualInfo *vinfo, vinfo_template;
4332 dpyinfo->visual = DefaultVisualOfScreen (screen);
4334 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
4335 vinfo_template.screen = XScreenNumberOfScreen (screen);
4336 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
4337 &vinfo_template, &n_visuals);
4338 if (n_visuals <= 0)
4339 fatal ("Can't get proper X visual info");
4341 dpyinfo->n_planes = vinfo->depth;
4342 XFree (vinfo);
4347 /* Return the X display structure for the display named NAME.
4348 Open a new connection if necessary. */
4350 static struct x_display_info *
4351 x_display_info_for_name (Lisp_Object name)
4353 struct x_display_info *dpyinfo;
4355 CHECK_STRING (name);
4357 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
4358 if (!NILP (Fstring_equal (XCAR (dpyinfo->name_list_element), name)))
4359 return dpyinfo;
4361 /* Use this general default value to start with. */
4362 Vx_resource_name = Vinvocation_name;
4364 validate_x_resource_name ();
4366 dpyinfo = x_term_init (name, 0, SSDATA (Vx_resource_name));
4368 if (dpyinfo == 0)
4369 error ("Cannot connect to X server %s", SDATA (name));
4371 XSETFASTINT (Vwindow_system_version, 11);
4373 return dpyinfo;
4377 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
4378 1, 3, 0,
4379 doc: /* Open a connection to a display server.
4380 DISPLAY is the name of the display to connect to.
4381 Optional second arg XRM-STRING is a string of resources in xrdb format.
4382 If the optional third arg MUST-SUCCEED is non-nil,
4383 terminate Emacs if we can't open the connection.
4384 \(In the Nextstep version, the last two arguments are currently ignored.) */)
4385 (Lisp_Object display, Lisp_Object xrm_string, Lisp_Object must_succeed)
4387 char *xrm_option;
4388 struct x_display_info *dpyinfo;
4390 CHECK_STRING (display);
4391 if (! NILP (xrm_string))
4392 CHECK_STRING (xrm_string);
4394 xrm_option = NILP (xrm_string) ? 0 : SSDATA (xrm_string);
4396 validate_x_resource_name ();
4398 /* This is what opens the connection and sets x_current_display.
4399 This also initializes many symbols, such as those used for input. */
4400 dpyinfo = x_term_init (display, xrm_option,
4401 SSDATA (Vx_resource_name));
4403 if (dpyinfo == 0)
4405 if (!NILP (must_succeed))
4406 fatal ("Cannot connect to X server %s.\n\
4407 Check the DISPLAY environment variable or use `-d'.\n\
4408 Also use the `xauth' program to verify that you have the proper\n\
4409 authorization information needed to connect the X server.\n\
4410 An insecure way to solve the problem may be to use `xhost'.\n",
4411 SDATA (display));
4412 else
4413 error ("Cannot connect to X server %s", SDATA (display));
4416 XSETFASTINT (Vwindow_system_version, 11);
4417 return Qnil;
4420 DEFUN ("x-close-connection", Fx_close_connection,
4421 Sx_close_connection, 1, 1, 0,
4422 doc: /* Close the connection to TERMINAL's X server.
4423 For TERMINAL, specify a terminal object, a frame or a display name (a
4424 string). If TERMINAL is nil, that stands for the selected frame's
4425 terminal. */)
4426 (Lisp_Object terminal)
4428 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4430 if (dpyinfo->reference_count > 0)
4431 error ("Display still has frames on it");
4433 x_delete_terminal (dpyinfo->terminal);
4435 return Qnil;
4438 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
4439 doc: /* Return the list of display names that Emacs has connections to. */)
4440 (void)
4442 Lisp_Object result = Qnil;
4443 struct x_display_info *xdi;
4445 for (xdi = x_display_list; xdi; xdi = xdi->next)
4446 result = Fcons (XCAR (xdi->name_list_element), result);
4448 return result;
4451 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
4452 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
4453 This function only has an effect on X Windows. With MS Windows, it is
4454 defined but does nothing.
4456 If ON is nil, allow buffering of requests.
4457 Turning on synchronization prohibits the Xlib routines from buffering
4458 requests and seriously degrades performance, but makes debugging much
4459 easier.
4460 The optional second argument TERMINAL specifies which display to act on.
4461 TERMINAL should be a terminal object, a frame or a display name (a string).
4462 If TERMINAL is omitted or nil, that stands for the selected frame's display. */)
4463 (Lisp_Object on, Lisp_Object terminal)
4465 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4467 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
4469 return Qnil;
4472 /* Wait for responses to all X commands issued so far for frame F. */
4474 void
4475 x_sync (struct frame *f)
4477 block_input ();
4478 XSync (FRAME_X_DISPLAY (f), False);
4479 unblock_input ();
4483 /***********************************************************************
4484 Window properties
4485 ***********************************************************************/
4487 DEFUN ("x-change-window-property", Fx_change_window_property,
4488 Sx_change_window_property, 2, 6, 0,
4489 doc: /* Change window property PROP to VALUE on the X window of FRAME.
4490 PROP must be a string. VALUE may be a string or a list of conses,
4491 numbers and/or strings. If an element in the list is a string, it is
4492 converted to an atom and the value of the atom is used. If an element
4493 is a cons, it is converted to a 32 bit number where the car is the 16
4494 top bits and the cdr is the lower 16 bits.
4496 FRAME nil or omitted means use the selected frame.
4497 If TYPE is given and non-nil, it is the name of the type of VALUE.
4498 If TYPE is not given or nil, the type is STRING.
4499 FORMAT gives the size in bits of each element if VALUE is a list.
4500 It must be one of 8, 16 or 32.
4501 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
4502 If OUTER-P is non-nil, the property is changed for the outer X window of
4503 FRAME. Default is to change on the edit X window. */)
4504 (Lisp_Object prop, Lisp_Object value, Lisp_Object frame,
4505 Lisp_Object type, Lisp_Object format, Lisp_Object outer_p)
4507 struct frame *f = decode_window_system_frame (frame);
4508 Atom prop_atom;
4509 Atom target_type = XA_STRING;
4510 int element_format = 8;
4511 unsigned char *data;
4512 int nelements;
4513 Window w;
4515 CHECK_STRING (prop);
4517 if (! NILP (format))
4519 CHECK_NUMBER (format);
4521 if (XINT (format) != 8 && XINT (format) != 16
4522 && XINT (format) != 32)
4523 error ("FORMAT must be one of 8, 16 or 32");
4524 element_format = XINT (format);
4527 if (CONSP (value))
4529 ptrdiff_t elsize;
4531 nelements = x_check_property_data (value);
4532 if (nelements == -1)
4533 error ("Bad data in VALUE, must be number, string or cons");
4535 /* The man page for XChangeProperty:
4536 "If the specified format is 32, the property data must be a
4537 long array."
4538 This applies even if long is more than 32 bits. The X library
4539 converts to 32 bits before sending to the X server. */
4540 elsize = element_format == 32 ? sizeof (long) : element_format >> 3;
4541 data = xnmalloc (nelements, elsize);
4543 x_fill_property_data (FRAME_X_DISPLAY (f), value, data, element_format);
4545 else
4547 CHECK_STRING (value);
4548 data = SDATA (value);
4549 if (INT_MAX < SBYTES (value))
4550 error ("VALUE too long");
4551 nelements = SBYTES (value);
4554 block_input ();
4555 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4556 if (! NILP (type))
4558 CHECK_STRING (type);
4559 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4562 if (! NILP (outer_p)) w = FRAME_OUTER_WINDOW (f);
4563 else w = FRAME_X_WINDOW (f);
4565 XChangeProperty (FRAME_X_DISPLAY (f), w,
4566 prop_atom, target_type, element_format, PropModeReplace,
4567 data, nelements);
4569 if (CONSP (value)) xfree (data);
4571 /* Make sure the property is set when we return. */
4572 XFlush (FRAME_X_DISPLAY (f));
4573 unblock_input ();
4575 return value;
4579 DEFUN ("x-delete-window-property", Fx_delete_window_property,
4580 Sx_delete_window_property, 1, 2, 0,
4581 doc: /* Remove window property PROP from X window of FRAME.
4582 FRAME nil or omitted means use the selected frame. Value is PROP. */)
4583 (Lisp_Object prop, Lisp_Object frame)
4585 struct frame *f = decode_window_system_frame (frame);
4586 Atom prop_atom;
4588 CHECK_STRING (prop);
4589 block_input ();
4590 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4591 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
4593 /* Make sure the property is removed when we return. */
4594 XFlush (FRAME_X_DISPLAY (f));
4595 unblock_input ();
4597 return prop;
4601 static Lisp_Object
4602 x_window_property_intern (struct frame *f,
4603 Window target_window,
4604 Atom prop_atom,
4605 Atom target_type,
4606 Lisp_Object delete_p,
4607 Lisp_Object vector_ret_p,
4608 bool *found)
4610 unsigned char *tmp_data = NULL;
4611 Lisp_Object prop_value = Qnil;
4612 Atom actual_type;
4613 int actual_format;
4614 unsigned long actual_size, bytes_remaining;
4615 int rc;
4616 struct gcpro gcpro1;
4618 GCPRO1 (prop_value);
4620 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4621 prop_atom, 0, 0, False, target_type,
4622 &actual_type, &actual_format, &actual_size,
4623 &bytes_remaining, &tmp_data);
4625 *found = actual_format != 0;
4627 if (rc == Success && *found)
4629 XFree (tmp_data);
4630 tmp_data = NULL;
4632 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4633 prop_atom, 0, bytes_remaining,
4634 ! NILP (delete_p), target_type,
4635 &actual_type, &actual_format,
4636 &actual_size, &bytes_remaining,
4637 &tmp_data);
4638 if (rc == Success && tmp_data)
4640 /* The man page for XGetWindowProperty says:
4641 "If the returned format is 32, the returned data is represented
4642 as a long array and should be cast to that type to obtain the
4643 elements."
4644 This applies even if long is more than 32 bits, the X library
4645 converts from 32 bit elements received from the X server to long
4646 and passes the long array to us. Thus, for that case memcpy can not
4647 be used. We convert to a 32 bit type here, because so much code
4648 assume on that.
4650 The bytes and offsets passed to XGetWindowProperty refers to the
4651 property and those are indeed in 32 bit quantities if format is
4652 32. */
4654 if (BITS_PER_LONG > 32 && actual_format == 32)
4656 unsigned long i;
4657 int *idata = (int *) tmp_data;
4658 long *ldata = (long *) tmp_data;
4660 for (i = 0; i < actual_size; ++i)
4661 idata[i] = (int) ldata[i];
4664 if (NILP (vector_ret_p))
4665 prop_value = make_string ((char *) tmp_data, actual_size);
4666 else
4667 prop_value = x_property_data_to_lisp (f,
4668 tmp_data,
4669 actual_type,
4670 actual_format,
4671 actual_size);
4674 if (tmp_data) XFree (tmp_data);
4677 UNGCPRO;
4678 return prop_value;
4681 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
4682 1, 6, 0,
4683 doc: /* Value is the value of window property PROP on FRAME.
4684 If FRAME is nil or omitted, use the selected frame.
4686 On X Windows, the following optional arguments are also accepted:
4687 If TYPE is nil or omitted, get the property as a string.
4688 Otherwise TYPE is the name of the atom that denotes the type expected.
4689 If SOURCE is non-nil, get the property on that window instead of from
4690 FRAME. The number 0 denotes the root window.
4691 If DELETE-P is non-nil, delete the property after retrieving it.
4692 If VECTOR-RET-P is non-nil, don't return a string but a vector of values.
4694 On MS Windows, this function accepts but ignores those optional arguments.
4696 Value is nil if FRAME hasn't a property with name PROP or if PROP has
4697 no value of TYPE (always string in the MS Windows case). */)
4698 (Lisp_Object prop, Lisp_Object frame, Lisp_Object type,
4699 Lisp_Object source, Lisp_Object delete_p, Lisp_Object vector_ret_p)
4701 struct frame *f = decode_window_system_frame (frame);
4702 Atom prop_atom;
4703 Lisp_Object prop_value = Qnil;
4704 Atom target_type = XA_STRING;
4705 Window target_window = FRAME_X_WINDOW (f);
4706 struct gcpro gcpro1;
4707 bool found;
4709 GCPRO1 (prop_value);
4710 CHECK_STRING (prop);
4712 if (! NILP (source))
4714 CONS_TO_INTEGER (source, Window, target_window);
4715 if (! target_window)
4716 target_window = FRAME_DISPLAY_INFO (f)->root_window;
4719 block_input ();
4720 if (STRINGP (type))
4722 if (strcmp ("AnyPropertyType", SSDATA (type)) == 0)
4723 target_type = AnyPropertyType;
4724 else
4725 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4728 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4729 prop_value = x_window_property_intern (f,
4730 target_window,
4731 prop_atom,
4732 target_type,
4733 delete_p,
4734 vector_ret_p,
4735 &found);
4736 if (NILP (prop_value)
4737 && ! found
4738 && NILP (source)
4739 && target_window != FRAME_OUTER_WINDOW (f))
4741 prop_value = x_window_property_intern (f,
4742 FRAME_OUTER_WINDOW (f),
4743 prop_atom,
4744 target_type,
4745 delete_p,
4746 vector_ret_p,
4747 &found);
4751 unblock_input ();
4752 UNGCPRO;
4753 return prop_value;
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 #if defined (USE_LUCID) && defined (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_LUCID && 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 the
4983 scroll-bar-width parameter because otherwise we end up in
4984 init_iterator with a null face cache, which should not happen. */
4985 init_frame_faces (f);
4987 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
4989 x_figure_window_size (f, parms, 0);
4992 XSetWindowAttributes attrs;
4993 unsigned long mask;
4994 Atom type = FRAME_DISPLAY_INFO (f)->Xatom_net_window_type_tooltip;
4996 block_input ();
4997 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
4998 if (DoesSaveUnders (dpyinfo->screen))
4999 mask |= CWSaveUnder;
5001 /* Window managers look at the override-redirect flag to determine
5002 whether or net to give windows a decoration (Xlib spec, chapter
5003 3.2.8). */
5004 attrs.override_redirect = True;
5005 attrs.save_under = True;
5006 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
5007 /* Arrange for getting MapNotify and UnmapNotify events. */
5008 attrs.event_mask = StructureNotifyMask;
5009 tip_window
5010 = FRAME_X_WINDOW (f)
5011 = XCreateWindow (FRAME_X_DISPLAY (f),
5012 FRAME_DISPLAY_INFO (f)->root_window,
5013 /* x, y, width, height */
5014 0, 0, 1, 1,
5015 /* Border. */
5016 f->border_width,
5017 CopyFromParent, InputOutput, CopyFromParent,
5018 mask, &attrs);
5019 XChangeProperty (FRAME_X_DISPLAY (f), tip_window,
5020 FRAME_DISPLAY_INFO (f)->Xatom_net_window_type,
5021 XA_ATOM, 32, PropModeReplace,
5022 (unsigned char *)&type, 1);
5023 unblock_input ();
5026 x_make_gc (f);
5028 x_default_parameter (f, parms, Qauto_raise, Qnil,
5029 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5030 x_default_parameter (f, parms, Qauto_lower, Qnil,
5031 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5032 x_default_parameter (f, parms, Qcursor_type, Qbox,
5033 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5035 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
5036 Change will not be effected unless different from the current
5037 FRAME_LINES (f). */
5038 width = FRAME_COLS (f);
5039 height = FRAME_LINES (f);
5040 SET_FRAME_COLS (f, 0);
5041 SET_FRAME_LINES (f, 0);
5042 change_frame_size (f, width, height, 1, 0, 0, 0);
5044 /* Add `tooltip' frame parameter's default value. */
5045 if (NILP (Fframe_parameter (frame, Qtooltip)))
5046 Fmodify_frame_parameters (frame, list1 (Fcons (Qtooltip, Qt)));
5048 /* FIXME - can this be done in a similar way to normal frames?
5049 http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00641.html */
5051 /* Set the `display-type' frame parameter before setting up faces. */
5053 Lisp_Object disptype;
5055 if (FRAME_DISPLAY_INFO (f)->n_planes == 1)
5056 disptype = intern ("mono");
5057 else if (FRAME_DISPLAY_INFO (f)->visual->class == GrayScale
5058 || FRAME_DISPLAY_INFO (f)->visual->class == StaticGray)
5059 disptype = intern ("grayscale");
5060 else
5061 disptype = intern ("color");
5063 if (NILP (Fframe_parameter (frame, Qdisplay_type)))
5064 Fmodify_frame_parameters (frame, list1 (Fcons (Qdisplay_type, disptype)));
5067 /* Set up faces after all frame parameters are known. This call
5068 also merges in face attributes specified for new frames.
5070 Frame parameters may be changed if .Xdefaults contains
5071 specifications for the default font. For example, if there is an
5072 `Emacs.default.attributeBackground: pink', the `background-color'
5073 attribute of the frame get's set, which let's the internal border
5074 of the tooltip frame appear in pink. Prevent this. */
5076 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
5078 /* Set tip_frame here, so that */
5079 tip_frame = frame;
5080 call2 (Qface_set_after_frame_default, frame, Qnil);
5082 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
5083 Fmodify_frame_parameters (frame, list1 (Fcons (Qbackground_color, bg)));
5086 f->no_split = 1;
5088 UNGCPRO;
5090 /* Now that the frame will be official, it counts as a reference to
5091 its display and terminal. */
5092 FRAME_DISPLAY_INFO (f)->reference_count++;
5093 f->terminal->reference_count++;
5095 /* It is now ok to make the frame official even if we get an error
5096 below. And the frame needs to be on Vframe_list or making it
5097 visible won't work. */
5098 Vframe_list = Fcons (frame, Vframe_list);
5099 f->official = true;
5101 /* Setting attributes of faces of the tooltip frame from resources
5102 and similar will increment face_change_count, which leads to the
5103 clearing of all current matrices. Since this isn't necessary
5104 here, avoid it by resetting face_change_count to the value it
5105 had before we created the tip frame. */
5106 face_change_count = face_change_count_before;
5108 /* Discard the unwind_protect. */
5109 return unbind_to (count, frame);
5113 /* Compute where to display tip frame F. PARMS is the list of frame
5114 parameters for F. DX and DY are specified offsets from the current
5115 location of the mouse. WIDTH and HEIGHT are the width and height
5116 of the tooltip. Return coordinates relative to the root window of
5117 the display in *ROOT_X, and *ROOT_Y. */
5119 static void
5120 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)
5122 Lisp_Object left, top;
5123 int win_x, win_y;
5124 Window root, child;
5125 unsigned pmask;
5127 /* User-specified position? */
5128 left = Fcdr (Fassq (Qleft, parms));
5129 top = Fcdr (Fassq (Qtop, parms));
5131 /* Move the tooltip window where the mouse pointer is. Resize and
5132 show it. */
5133 if (!INTEGERP (left) || !INTEGERP (top))
5135 block_input ();
5136 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_DISPLAY_INFO (f)->root_window,
5137 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
5138 unblock_input ();
5141 if (INTEGERP (top))
5142 *root_y = XINT (top);
5143 else if (*root_y + XINT (dy) <= 0)
5144 *root_y = 0; /* Can happen for negative dy */
5145 else if (*root_y + XINT (dy) + height
5146 <= x_display_pixel_height (FRAME_DISPLAY_INFO (f)))
5147 /* It fits below the pointer */
5148 *root_y += XINT (dy);
5149 else if (height + XINT (dy) <= *root_y)
5150 /* It fits above the pointer. */
5151 *root_y -= height + XINT (dy);
5152 else
5153 /* Put it on the top. */
5154 *root_y = 0;
5156 if (INTEGERP (left))
5157 *root_x = XINT (left);
5158 else if (*root_x + XINT (dx) <= 0)
5159 *root_x = 0; /* Can happen for negative dx */
5160 else if (*root_x + XINT (dx) + width
5161 <= x_display_pixel_width (FRAME_DISPLAY_INFO (f)))
5162 /* It fits to the right of the pointer. */
5163 *root_x += XINT (dx);
5164 else if (width + XINT (dx) <= *root_x)
5165 /* It fits to the left of the pointer. */
5166 *root_x -= width + XINT (dx);
5167 else
5168 /* Put it left-justified on the screen--it ought to fit that way. */
5169 *root_x = 0;
5173 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
5174 doc: /* Show STRING in a "tooltip" window on frame FRAME.
5175 A tooltip window is a small X window displaying a string.
5177 This is an internal function; Lisp code should call `tooltip-show'.
5179 FRAME nil or omitted means use the selected frame.
5181 PARMS is an optional list of frame parameters which can be used to
5182 change the tooltip's appearance.
5184 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
5185 means use the default timeout of 5 seconds.
5187 If the list of frame parameters PARMS contains a `left' parameters,
5188 the tooltip is displayed at that x-position. Otherwise it is
5189 displayed at the mouse position, with offset DX added (default is 5 if
5190 DX isn't specified). Likewise for the y-position; if a `top' frame
5191 parameter is specified, it determines the y-position of the tooltip
5192 window, otherwise it is displayed at the mouse position, with offset
5193 DY added (default is -10).
5195 A tooltip's maximum size is specified by `x-max-tooltip-size'.
5196 Text larger than the specified size is clipped. */)
5197 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
5199 struct frame *f;
5200 struct window *w;
5201 int root_x, root_y;
5202 struct buffer *old_buffer;
5203 struct text_pos pos;
5204 int i, width, height, seen_reversed_p;
5205 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5206 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5207 ptrdiff_t count = SPECPDL_INDEX ();
5209 specbind (Qinhibit_redisplay, Qt);
5211 GCPRO4 (string, parms, frame, timeout);
5213 CHECK_STRING (string);
5214 if (SCHARS (string) == 0)
5215 string = make_unibyte_string (" ", 1);
5217 f = decode_window_system_frame (frame);
5218 if (NILP (timeout))
5219 timeout = make_number (5);
5220 else
5221 CHECK_NATNUM (timeout);
5223 if (NILP (dx))
5224 dx = make_number (5);
5225 else
5226 CHECK_NUMBER (dx);
5228 if (NILP (dy))
5229 dy = make_number (-10);
5230 else
5231 CHECK_NUMBER (dy);
5233 #ifdef USE_GTK
5234 if (x_gtk_use_system_tooltips)
5236 bool ok;
5238 /* Hide a previous tip, if any. */
5239 Fx_hide_tip ();
5241 block_input ();
5242 ok = xg_prepare_tooltip (f, string, &width, &height);
5243 if (ok)
5245 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5246 xg_show_tooltip (f, root_x, root_y);
5247 /* This is used in Fx_hide_tip. */
5248 XSETFRAME (tip_frame, f);
5250 unblock_input ();
5251 if (ok) goto start_timer;
5253 #endif /* USE_GTK */
5255 if (NILP (last_show_tip_args))
5256 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5258 if (!NILP (tip_frame))
5260 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5261 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5262 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5264 if (EQ (frame, last_frame)
5265 && !NILP (Fequal (last_string, string))
5266 && !NILP (Fequal (last_parms, parms)))
5268 struct frame *tip_f = XFRAME (tip_frame);
5270 /* Only DX and DY have changed. */
5271 if (!NILP (tip_timer))
5273 Lisp_Object timer = tip_timer;
5274 tip_timer = Qnil;
5275 call1 (Qcancel_timer, timer);
5278 block_input ();
5279 compute_tip_xy (tip_f, parms, dx, dy, FRAME_PIXEL_WIDTH (tip_f),
5280 FRAME_PIXEL_HEIGHT (tip_f), &root_x, &root_y);
5281 XMoveWindow (FRAME_X_DISPLAY (tip_f), FRAME_X_WINDOW (tip_f),
5282 root_x, root_y);
5283 unblock_input ();
5284 goto start_timer;
5288 /* Hide a previous tip, if any. */
5289 Fx_hide_tip ();
5291 ASET (last_show_tip_args, 0, string);
5292 ASET (last_show_tip_args, 1, frame);
5293 ASET (last_show_tip_args, 2, parms);
5295 /* Add default values to frame parameters. */
5296 if (NILP (Fassq (Qname, parms)))
5297 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5298 if (NILP (Fassq (Qinternal_border_width, parms)))
5299 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5300 if (NILP (Fassq (Qborder_width, parms)))
5301 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5302 if (NILP (Fassq (Qbottom_divider_width, parms)))
5303 parms = Fcons (Fcons (Qbottom_divider_width, make_number (0)), parms);
5304 if (NILP (Fassq (Qright_divider_width, parms)))
5305 parms = Fcons (Fcons (Qright_divider_width, make_number (0)), parms);
5306 if (NILP (Fassq (Qborder_color, parms)))
5307 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5308 if (NILP (Fassq (Qbackground_color, parms)))
5309 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5310 parms);
5312 /* Create a frame for the tooltip, and record it in the global
5313 variable tip_frame. */
5314 frame = x_create_tip_frame (FRAME_DISPLAY_INFO (f), parms, string);
5315 f = XFRAME (frame);
5317 /* Set up the frame's root window. */
5318 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5319 w->left_col = 0;
5320 w->top_line = 0;
5321 w->pixel_left = 0;
5322 w->pixel_top = 0;
5324 if (CONSP (Vx_max_tooltip_size)
5325 && RANGED_INTEGERP (1, XCAR (Vx_max_tooltip_size), INT_MAX)
5326 && RANGED_INTEGERP (1, XCDR (Vx_max_tooltip_size), INT_MAX))
5328 w->total_cols = XFASTINT (XCAR (Vx_max_tooltip_size));
5329 w->total_lines = XFASTINT (XCDR (Vx_max_tooltip_size));
5331 else
5333 w->total_cols = 80;
5334 w->total_lines = 40;
5337 w->pixel_width = w->total_cols * FRAME_COLUMN_WIDTH (f);
5338 w->pixel_height = w->total_lines * FRAME_LINE_HEIGHT (f);
5340 FRAME_TOTAL_COLS (f) = w->total_cols;
5341 adjust_frame_glyphs (f);
5342 w->pseudo_window_p = 1;
5344 /* Display the tooltip text in a temporary buffer. */
5345 old_buffer = current_buffer;
5346 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->contents));
5347 bset_truncate_lines (current_buffer, Qnil);
5348 clear_glyph_matrix (w->desired_matrix);
5349 clear_glyph_matrix (w->current_matrix);
5350 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5351 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
5353 /* Compute width and height of the tooltip. */
5354 width = height = seen_reversed_p = 0;
5355 for (i = 0; i < w->desired_matrix->nrows; ++i)
5357 struct glyph_row *row = &w->desired_matrix->rows[i];
5358 struct glyph *last;
5359 int row_width;
5361 /* Stop at the first empty row at the end. */
5362 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5363 break;
5365 /* Let the row go over the full width of the frame. */
5366 row->full_width_p = 1;
5368 row_width = row->pixel_width;
5369 if (row->used[TEXT_AREA])
5371 /* There's a glyph at the end of rows that is used to place
5372 the cursor there. Don't include the width of this glyph. */
5373 if (!row->reversed_p)
5375 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5376 if (INTEGERP (last->object))
5377 row_width -= last->pixel_width;
5379 else
5381 /* There could be a stretch glyph at the beginning of R2L
5382 rows that is produced by extend_face_to_end_of_line.
5383 Don't count that glyph. */
5384 struct glyph *g = row->glyphs[TEXT_AREA];
5386 if (g->type == STRETCH_GLYPH && INTEGERP (g->object))
5388 row_width -= g->pixel_width;
5389 seen_reversed_p = 1;
5394 height += row->height;
5395 width = max (width, row_width);
5398 /* If we've seen partial-length R2L rows, we need to re-adjust the
5399 tool-tip frame width and redisplay it again, to avoid over-wide
5400 tips due to the stretch glyph that extends R2L lines to full
5401 width of the frame. */
5402 if (seen_reversed_p)
5404 /* w->total_cols and FRAME_TOTAL_COLS want the width in columns,
5405 not in pixels. */
5406 w->pixel_width = width;
5407 width /= WINDOW_FRAME_COLUMN_WIDTH (w);
5408 w->total_cols = width;
5409 FRAME_TOTAL_COLS (f) = width;
5410 SET_FRAME_WIDTH (f, width);
5411 adjust_frame_glyphs (f);
5412 clear_glyph_matrix (w->desired_matrix);
5413 clear_glyph_matrix (w->current_matrix);
5414 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
5415 width = height = 0;
5416 /* Recompute width and height of the tooltip. */
5417 for (i = 0; i < w->desired_matrix->nrows; ++i)
5419 struct glyph_row *row = &w->desired_matrix->rows[i];
5420 struct glyph *last;
5421 int row_width;
5423 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5424 break;
5425 row->full_width_p = 1;
5426 row_width = row->pixel_width;
5427 if (row->used[TEXT_AREA] && !row->reversed_p)
5429 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5430 if (INTEGERP (last->object))
5431 row_width -= last->pixel_width;
5434 height += row->height;
5435 width = max (width, row_width);
5439 /* Add the frame's internal border to the width and height the X
5440 window should have. */
5441 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5442 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5444 /* Move the tooltip window where the mouse pointer is. Resize and
5445 show it. */
5446 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5448 block_input ();
5449 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5450 root_x, root_y, width, height);
5451 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5452 unblock_input ();
5454 /* Draw into the window. */
5455 w->must_be_updated_p = 1;
5456 update_single_window (w, 1);
5458 /* Restore original current buffer. */
5459 set_buffer_internal_1 (old_buffer);
5460 windows_or_buffers_changed = old_windows_or_buffers_changed;
5462 start_timer:
5463 /* Let the tip disappear after timeout seconds. */
5464 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5465 intern ("x-hide-tip"));
5467 UNGCPRO;
5468 return unbind_to (count, Qnil);
5472 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5473 doc: /* Hide the current tooltip window, if there is any.
5474 Value is t if tooltip was open, nil otherwise. */)
5475 (void)
5477 ptrdiff_t count;
5478 Lisp_Object deleted, frame, timer;
5479 struct gcpro gcpro1, gcpro2;
5481 /* Return quickly if nothing to do. */
5482 if (NILP (tip_timer) && NILP (tip_frame))
5483 return Qnil;
5485 frame = tip_frame;
5486 timer = tip_timer;
5487 GCPRO2 (frame, timer);
5488 tip_frame = tip_timer = deleted = Qnil;
5490 count = SPECPDL_INDEX ();
5491 specbind (Qinhibit_redisplay, Qt);
5492 specbind (Qinhibit_quit, Qt);
5494 if (!NILP (timer))
5495 call1 (Qcancel_timer, timer);
5497 #ifdef USE_GTK
5499 /* When using system tooltip, tip_frame is the Emacs frame on which
5500 the tip is shown. */
5501 struct frame *f = XFRAME (frame);
5502 if (FRAME_LIVE_P (f) && xg_hide_tooltip (f))
5503 frame = Qnil;
5505 #endif
5507 if (FRAMEP (frame))
5509 delete_frame (frame, Qnil);
5510 deleted = Qt;
5512 #ifdef USE_LUCID
5513 /* Bloodcurdling hack alert: The Lucid menu bar widget's
5514 redisplay procedure is not called when a tip frame over menu
5515 items is unmapped. Redisplay the menu manually... */
5517 Widget w;
5518 struct frame *f = SELECTED_FRAME ();
5519 w = f->output_data.x->menubar_widget;
5521 if (!DoesSaveUnders (FRAME_DISPLAY_INFO (f)->screen)
5522 && w != NULL)
5524 block_input ();
5525 xlwmenu_redisplay (w);
5526 unblock_input ();
5529 #endif /* USE_LUCID */
5532 UNGCPRO;
5533 return unbind_to (count, deleted);
5538 /***********************************************************************
5539 File selection dialog
5540 ***********************************************************************/
5542 DEFUN ("x-uses-old-gtk-dialog", Fx_uses_old_gtk_dialog,
5543 Sx_uses_old_gtk_dialog,
5544 0, 0, 0,
5545 doc: /* Return t if the old Gtk+ file selection dialog is used. */)
5546 (void)
5548 #ifdef USE_GTK
5549 if (use_dialog_box
5550 && use_file_dialog
5551 && window_system_available (SELECTED_FRAME ())
5552 && xg_uses_old_file_dialog ())
5553 return Qt;
5554 #endif
5555 return Qnil;
5559 #ifdef USE_MOTIF
5560 /* Callback for "OK" and "Cancel" on file selection dialog. */
5562 static void
5563 file_dialog_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5565 int *result = client_data;
5566 XmAnyCallbackStruct *cb = call_data;
5567 *result = cb->reason;
5571 /* Callback for unmapping a file selection dialog. This is used to
5572 capture the case where a dialog is closed via a window manager's
5573 closer button, for example. Using a XmNdestroyCallback didn't work
5574 in this case. */
5576 static void
5577 file_dialog_unmap_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5579 int *result = client_data;
5580 *result = XmCR_CANCEL;
5583 static void
5584 clean_up_file_dialog (void *arg)
5586 Widget dialog = arg;
5588 /* Clean up. */
5589 block_input ();
5590 XtUnmanageChild (dialog);
5591 XtDestroyWidget (dialog);
5592 x_menu_set_in_use (0);
5593 unblock_input ();
5597 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5598 doc: /* Read file name, prompting with PROMPT in directory DIR.
5599 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5600 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5601 or directory must exist.
5603 This function is only defined on NS, MS Windows, and X Windows with the
5604 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5605 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories.
5606 On Windows 7 and later, the file selection dialog "remembers" the last
5607 directory where the user selected a file, and will open that directory
5608 instead of DIR on subsequent invocations of this function with the same
5609 value of DIR as in previous invocations; this is standard Windows behavior. */)
5610 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename,
5611 Lisp_Object mustmatch, Lisp_Object only_dir_p)
5613 int result;
5614 struct frame *f = SELECTED_FRAME ();
5615 Lisp_Object file = Qnil;
5616 Lisp_Object decoded_file;
5617 Widget dialog, text, help;
5618 Arg al[10];
5619 int ac = 0;
5620 XmString dir_xmstring, pattern_xmstring;
5621 ptrdiff_t count = SPECPDL_INDEX ();
5622 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5624 check_window_system (f);
5626 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5628 if (popup_activated ())
5629 error ("Trying to use a menu from within a menu-entry");
5631 CHECK_STRING (prompt);
5632 CHECK_STRING (dir);
5634 /* Prevent redisplay. */
5635 specbind (Qinhibit_redisplay, Qt);
5637 block_input ();
5639 /* Create the dialog with PROMPT as title, using DIR as initial
5640 directory and using "*" as pattern. */
5641 dir = Fexpand_file_name (dir, Qnil);
5642 dir_xmstring = XmStringCreateLocalized (SSDATA (dir));
5643 pattern_xmstring = XmStringCreateLocalized ("*");
5645 XtSetArg (al[ac], XmNtitle, SDATA (prompt)); ++ac;
5646 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
5647 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
5648 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
5649 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
5650 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
5651 "fsb", al, ac);
5652 XmStringFree (dir_xmstring);
5653 XmStringFree (pattern_xmstring);
5655 /* Add callbacks for OK and Cancel. */
5656 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
5657 (XtPointer) &result);
5658 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
5659 (XtPointer) &result);
5660 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
5661 (XtPointer) &result);
5663 /* Remove the help button since we can't display help. */
5664 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
5665 XtUnmanageChild (help);
5667 /* Mark OK button as default. */
5668 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
5669 XmNshowAsDefault, True, NULL);
5671 /* If MUSTMATCH is non-nil, disable the file entry field of the
5672 dialog, so that the user must select a file from the files list
5673 box. We can't remove it because we wouldn't have a way to get at
5674 the result file name, then. */
5675 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5676 if (!NILP (mustmatch))
5678 Widget label;
5679 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
5680 XtSetSensitive (text, False);
5681 XtSetSensitive (label, False);
5684 /* Manage the dialog, so that list boxes get filled. */
5685 XtManageChild (dialog);
5687 if (STRINGP (default_filename))
5689 XmString default_xmstring;
5690 Widget wtext = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5691 Widget list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
5693 XmTextPosition last_pos = XmTextFieldGetLastPosition (wtext);
5694 XmTextFieldReplace (wtext, 0, last_pos,
5695 (SSDATA (Ffile_name_nondirectory (default_filename))));
5697 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
5698 must include the path for this to work. */
5700 default_xmstring = XmStringCreateLocalized (SSDATA (default_filename));
5702 if (XmListItemExists (list, default_xmstring))
5704 int item_pos = XmListItemPos (list, default_xmstring);
5705 /* Select the item and scroll it into view. */
5706 XmListSelectPos (list, item_pos, True);
5707 XmListSetPos (list, item_pos);
5710 XmStringFree (default_xmstring);
5713 record_unwind_protect_ptr (clean_up_file_dialog, dialog);
5715 /* Process events until the user presses Cancel or OK. */
5716 x_menu_set_in_use (1);
5717 result = 0;
5718 while (result == 0)
5720 XEvent event;
5721 x_menu_wait_for_event (0);
5722 XtAppNextEvent (Xt_app_con, &event);
5723 if (event.type == KeyPress
5724 && FRAME_X_DISPLAY (f) == event.xkey.display)
5726 KeySym keysym = XLookupKeysym (&event.xkey, 0);
5728 /* Pop down on C-g. */
5729 if (keysym == XK_g && (event.xkey.state & ControlMask) != 0)
5730 XtUnmanageChild (dialog);
5733 (void) x_dispatch_event (&event, FRAME_X_DISPLAY (f));
5736 /* Get the result. */
5737 if (result == XmCR_OK)
5739 XmString text_string;
5740 String data;
5742 XtVaGetValues (dialog, XmNtextString, &text_string, NULL);
5743 XmStringGetLtoR (text_string, XmFONTLIST_DEFAULT_TAG, &data);
5744 XmStringFree (text_string);
5745 file = build_string (data);
5746 XtFree (data);
5748 else
5749 file = Qnil;
5751 unblock_input ();
5752 UNGCPRO;
5754 /* Make "Cancel" equivalent to C-g. */
5755 if (NILP (file))
5756 Fsignal (Qquit, Qnil);
5758 decoded_file = DECODE_FILE (file);
5760 return unbind_to (count, decoded_file);
5763 #endif /* USE_MOTIF */
5765 #ifdef USE_GTK
5767 static void
5768 clean_up_dialog (void)
5770 x_menu_set_in_use (0);
5773 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5774 doc: /* Read file name, prompting with PROMPT in directory DIR.
5775 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5776 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5777 or directory must exist.
5779 This function is only defined on NS, MS Windows, and X Windows with the
5780 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5781 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories.
5782 On Windows 7 and later, the file selection dialog "remembers" the last
5783 directory where the user selected a file, and will open that directory
5784 instead of DIR on subsequent invocations of this function with the same
5785 value of DIR as in previous invocations; this is standard Windows behavior. */)
5786 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
5788 struct frame *f = SELECTED_FRAME ();
5789 char *fn;
5790 Lisp_Object file = Qnil;
5791 Lisp_Object decoded_file;
5792 ptrdiff_t count = SPECPDL_INDEX ();
5793 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5794 char *cdef_file;
5796 check_window_system (f);
5798 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5800 if (popup_activated ())
5801 error ("Trying to use a menu from within a menu-entry");
5803 CHECK_STRING (prompt);
5804 CHECK_STRING (dir);
5806 /* Prevent redisplay. */
5807 specbind (Qinhibit_redisplay, Qt);
5808 record_unwind_protect_void (clean_up_dialog);
5810 block_input ();
5812 if (STRINGP (default_filename))
5813 cdef_file = SSDATA (default_filename);
5814 else
5815 cdef_file = SSDATA (dir);
5817 fn = xg_get_file_name (f, SSDATA (prompt), cdef_file,
5818 ! NILP (mustmatch),
5819 ! NILP (only_dir_p));
5821 if (fn)
5823 file = build_string (fn);
5824 xfree (fn);
5827 unblock_input ();
5828 UNGCPRO;
5830 /* Make "Cancel" equivalent to C-g. */
5831 if (NILP (file))
5832 Fsignal (Qquit, Qnil);
5834 decoded_file = DECODE_FILE (file);
5836 return unbind_to (count, decoded_file);
5840 #ifdef HAVE_FREETYPE
5842 DEFUN ("x-select-font", Fx_select_font, Sx_select_font, 0, 2, 0,
5843 doc: /* Read a font using a GTK dialog.
5844 Return either a font spec (for GTK versions >= 3.2) or a string
5845 containing a GTK-style font name.
5847 FRAME is the frame on which to pop up the font chooser. If omitted or
5848 nil, it defaults to the selected frame. */)
5849 (Lisp_Object frame, Lisp_Object ignored)
5851 struct frame *f = decode_window_system_frame (frame);
5852 Lisp_Object font;
5853 Lisp_Object font_param;
5854 char *default_name = NULL;
5855 struct gcpro gcpro1, gcpro2;
5856 ptrdiff_t count = SPECPDL_INDEX ();
5858 if (popup_activated ())
5859 error ("Trying to use a menu from within a menu-entry");
5861 /* Prevent redisplay. */
5862 specbind (Qinhibit_redisplay, Qt);
5863 record_unwind_protect_void (clean_up_dialog);
5865 block_input ();
5867 GCPRO2 (font_param, font);
5869 XSETFONT (font, FRAME_FONT (f));
5870 font_param = Ffont_get (font, intern (":name"));
5871 if (STRINGP (font_param))
5872 default_name = xstrdup (SSDATA (font_param));
5873 else
5875 font_param = Fframe_parameter (frame, Qfont_param);
5876 if (STRINGP (font_param))
5877 default_name = xstrdup (SSDATA (font_param));
5880 font = xg_get_font (f, default_name);
5881 xfree (default_name);
5883 unblock_input ();
5885 if (NILP (font))
5886 Fsignal (Qquit, Qnil);
5888 return unbind_to (count, font);
5890 #endif /* HAVE_FREETYPE */
5892 #endif /* USE_GTK */
5895 /***********************************************************************
5896 Keyboard
5897 ***********************************************************************/
5899 #ifdef HAVE_XKB
5900 #include <X11/XKBlib.h>
5901 #include <X11/keysym.h>
5902 #endif
5904 DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
5905 Sx_backspace_delete_keys_p, 0, 1, 0,
5906 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
5907 FRAME nil means use the selected frame.
5908 Value is t if we know that both keys are present, and are mapped to the
5909 usual X keysyms. Value is `lambda' if we cannot determine if both keys are
5910 present and mapped to the usual X keysyms. */)
5911 (Lisp_Object frame)
5913 #ifndef HAVE_XKB
5914 return Qlambda;
5915 #else
5916 XkbDescPtr kb;
5917 struct frame *f = decode_window_system_frame (frame);
5918 Display *dpy = FRAME_X_DISPLAY (f);
5919 Lisp_Object have_keys;
5920 int major, minor, op, event, error_code;
5922 block_input ();
5924 /* Check library version in case we're dynamically linked. */
5925 major = XkbMajorVersion;
5926 minor = XkbMinorVersion;
5927 if (!XkbLibraryVersion (&major, &minor))
5929 unblock_input ();
5930 return Qlambda;
5933 /* Check that the server supports XKB. */
5934 major = XkbMajorVersion;
5935 minor = XkbMinorVersion;
5936 if (!XkbQueryExtension (dpy, &op, &event, &error_code, &major, &minor))
5938 unblock_input ();
5939 return Qlambda;
5942 /* In this code we check that the keyboard has physical keys with names
5943 that start with BKSP (Backspace) and DELE (Delete), and that they
5944 generate keysym XK_BackSpace and XK_Delete respectively.
5945 This function is used to test if normal-erase-is-backspace should be
5946 turned on.
5947 An alternative approach would be to just check if XK_BackSpace and
5948 XK_Delete are mapped to any key. But if any of those are mapped to
5949 some non-intuitive key combination (Meta-Shift-Ctrl-whatever) and the
5950 user doesn't know about it, it is better to return false here.
5951 It is more obvious to the user what to do if she/he has two keys
5952 clearly marked with names/symbols and one key does something not
5953 expected (i.e. she/he then tries the other).
5954 The cases where Backspace/Delete is mapped to some other key combination
5955 are rare, and in those cases, normal-erase-is-backspace can be turned on
5956 manually. */
5958 have_keys = Qnil;
5959 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
5960 if (kb)
5962 int delete_keycode = 0, backspace_keycode = 0, i;
5964 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
5966 for (i = kb->min_key_code;
5967 (i < kb->max_key_code
5968 && (delete_keycode == 0 || backspace_keycode == 0));
5969 ++i)
5971 /* The XKB symbolic key names can be seen most easily in
5972 the PS file generated by `xkbprint -label name
5973 $DISPLAY'. */
5974 if (memcmp ("DELE", kb->names->keys[i].name, 4) == 0)
5975 delete_keycode = i;
5976 else if (memcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
5977 backspace_keycode = i;
5980 XkbFreeNames (kb, 0, True);
5983 /* As of libX11-1.6.2, XkbGetMap manual says that you should use
5984 XkbFreeClientMap to free the data returned by XkbGetMap. But
5985 this function just frees the data referenced from KB and not
5986 KB itself. To free KB as well, call XkbFreeKeyboard. */
5987 XkbFreeKeyboard (kb, XkbAllMapComponentsMask, True);
5989 if (delete_keycode
5990 && backspace_keycode
5991 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
5992 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
5993 have_keys = Qt;
5995 unblock_input ();
5996 return have_keys;
5997 #endif
6002 /***********************************************************************
6003 Initialization
6004 ***********************************************************************/
6006 /* Keep this list in the same order as frame_parms in frame.c.
6007 Use 0 for unsupported frame parameters. */
6009 frame_parm_handler x_frame_parm_handlers[] =
6011 x_set_autoraise,
6012 x_set_autolower,
6013 x_set_background_color,
6014 x_set_border_color,
6015 x_set_border_width,
6016 x_set_cursor_color,
6017 x_set_cursor_type,
6018 x_set_font,
6019 x_set_foreground_color,
6020 x_set_icon_name,
6021 x_set_icon_type,
6022 x_set_internal_border_width,
6023 x_set_right_divider_width,
6024 x_set_bottom_divider_width,
6025 x_set_menu_bar_lines,
6026 x_set_mouse_color,
6027 x_explicitly_set_name,
6028 x_set_scroll_bar_width,
6029 x_set_scroll_bar_height,
6030 x_set_title,
6031 x_set_unsplittable,
6032 x_set_vertical_scroll_bars,
6033 x_set_horizontal_scroll_bars,
6034 x_set_visibility,
6035 x_set_tool_bar_lines,
6036 x_set_scroll_bar_foreground,
6037 x_set_scroll_bar_background,
6038 x_set_screen_gamma,
6039 x_set_line_spacing,
6040 x_set_left_fringe,
6041 x_set_right_fringe,
6042 x_set_wait_for_wm,
6043 x_set_fullscreen,
6044 x_set_font_backend,
6045 x_set_alpha,
6046 x_set_sticky,
6047 x_set_tool_bar_position,
6050 void
6051 syms_of_xfns (void)
6053 DEFSYM (Qundefined_color, "undefined-color");
6054 DEFSYM (Qcompound_text, "compound-text");
6055 DEFSYM (Qcancel_timer, "cancel-timer");
6056 DEFSYM (Qfont_param, "font-parameter");
6058 Fput (Qundefined_color, Qerror_conditions,
6059 listn (CONSTYPE_PURE, 2, Qundefined_color, Qerror));
6060 Fput (Qundefined_color, Qerror_message,
6061 build_pure_c_string ("Undefined color"));
6063 DEFVAR_LISP ("x-pointer-shape", Vx_pointer_shape,
6064 doc: /* The shape of the pointer when over text.
6065 Changing the value does not affect existing frames
6066 unless you set the mouse color. */);
6067 Vx_pointer_shape = Qnil;
6069 #if 0 /* This doesn't really do anything. */
6070 DEFVAR_LISP ("x-nontext-pointer-shape", Vx_nontext_pointer_shape,
6071 doc: /* The shape of the pointer when not over text.
6072 This variable takes effect when you create a new frame
6073 or when you set the mouse color. */);
6074 #endif
6075 Vx_nontext_pointer_shape = Qnil;
6077 DEFVAR_LISP ("x-hourglass-pointer-shape", Vx_hourglass_pointer_shape,
6078 doc: /* The shape of the pointer when Emacs is busy.
6079 This variable takes effect when you create a new frame
6080 or when you set the mouse color. */);
6081 Vx_hourglass_pointer_shape = Qnil;
6083 #if 0 /* This doesn't really do anything. */
6084 DEFVAR_LISP ("x-mode-pointer-shape", Vx_mode_pointer_shape,
6085 doc: /* The shape of the pointer when over the mode line.
6086 This variable takes effect when you create a new frame
6087 or when you set the mouse color. */);
6088 #endif
6089 Vx_mode_pointer_shape = Qnil;
6091 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
6092 Vx_sensitive_text_pointer_shape,
6093 doc: /* The shape of the pointer when over mouse-sensitive text.
6094 This variable takes effect when you create a new frame
6095 or when you set the mouse color. */);
6096 Vx_sensitive_text_pointer_shape = Qnil;
6098 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
6099 Vx_window_horizontal_drag_shape,
6100 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
6101 This variable takes effect when you create a new frame
6102 or when you set the mouse color. */);
6103 Vx_window_horizontal_drag_shape = Qnil;
6105 DEFVAR_LISP ("x-window-vertical-drag-cursor",
6106 Vx_window_vertical_drag_shape,
6107 doc: /* Pointer shape to use for indicating a window can be dragged vertically.
6108 This variable takes effect when you create a new frame
6109 or when you set the mouse color. */);
6110 Vx_window_vertical_drag_shape = Qnil;
6112 DEFVAR_LISP ("x-cursor-fore-pixel", Vx_cursor_fore_pixel,
6113 doc: /* A string indicating the foreground color of the cursor box. */);
6114 Vx_cursor_fore_pixel = Qnil;
6116 DEFVAR_LISP ("x-max-tooltip-size", Vx_max_tooltip_size,
6117 doc: /* Maximum size for tooltips.
6118 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
6119 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
6121 DEFVAR_LISP ("x-no-window-manager", Vx_no_window_manager,
6122 doc: /* Non-nil if no X window manager is in use.
6123 Emacs doesn't try to figure this out; this is always nil
6124 unless you set it to something else. */);
6125 /* We don't have any way to find this out, so set it to nil
6126 and maybe the user would like to set it to t. */
6127 Vx_no_window_manager = Qnil;
6129 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
6130 Vx_pixel_size_width_font_regexp,
6131 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
6133 Since Emacs gets width of a font matching with this regexp from
6134 PIXEL_SIZE field of the name, font finding mechanism gets faster for
6135 such a font. This is especially effective for such large fonts as
6136 Chinese, Japanese, and Korean. */);
6137 Vx_pixel_size_width_font_regexp = Qnil;
6139 /* This is not ifdef:ed, so other builds than GTK can customize it. */
6140 DEFVAR_BOOL ("x-gtk-use-old-file-dialog", x_gtk_use_old_file_dialog,
6141 doc: /* Non-nil means prompt with the old GTK file selection dialog.
6142 If nil or if the file selection dialog is not available, the new GTK file
6143 chooser is used instead. To turn off all file dialogs set the
6144 variable `use-file-dialog'. */);
6145 x_gtk_use_old_file_dialog = 0;
6147 DEFVAR_BOOL ("x-gtk-show-hidden-files", x_gtk_show_hidden_files,
6148 doc: /* If non-nil, the GTK file chooser will by default show hidden files.
6149 Note that this is just the default, there is a toggle button on the file
6150 chooser to show or not show hidden files on a case by case basis. */);
6151 x_gtk_show_hidden_files = 0;
6153 DEFVAR_BOOL ("x-gtk-file-dialog-help-text", x_gtk_file_dialog_help_text,
6154 doc: /* If non-nil, the GTK file chooser will show additional help text.
6155 If more space for files in the file chooser dialog is wanted, set this to nil
6156 to turn the additional text off. */);
6157 x_gtk_file_dialog_help_text = 1;
6159 DEFVAR_BOOL ("x-gtk-whole-detached-tool-bar", x_gtk_whole_detached_tool_bar,
6160 doc: /* If non-nil, a detached tool bar is shown in full.
6161 The default is to just show an arrow and pressing on that arrow shows
6162 the tool bar buttons. */);
6163 x_gtk_whole_detached_tool_bar = 0;
6165 DEFVAR_BOOL ("x-gtk-use-system-tooltips", x_gtk_use_system_tooltips,
6166 doc: /* If non-nil with a Gtk+ built Emacs, the Gtk+ tooltip is used.
6167 Otherwise use Emacs own tooltip implementation.
6168 When using Gtk+ tooltips, the tooltip face is not used. */);
6169 x_gtk_use_system_tooltips = 1;
6171 /* Tell Emacs about this window system. */
6172 Fprovide (Qx, Qnil);
6174 #ifdef USE_X_TOOLKIT
6175 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6176 #ifdef USE_MOTIF
6177 Fprovide (intern_c_string ("motif"), Qnil);
6179 DEFVAR_LISP ("motif-version-string", Vmotif_version_string,
6180 doc: /* Version info for LessTif/Motif. */);
6181 Vmotif_version_string = build_string (XmVERSION_STRING);
6182 #endif /* USE_MOTIF */
6183 #endif /* USE_X_TOOLKIT */
6185 #ifdef USE_GTK
6186 /* Provide x-toolkit also for GTK. Internally GTK does not use Xt so it
6187 is not an X toolkit in that sense (USE_X_TOOLKIT is not defined).
6188 But for a user it is a toolkit for X, and indeed, configure
6189 accepts --with-x-toolkit=gtk. */
6190 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6191 Fprovide (intern_c_string ("gtk"), Qnil);
6192 Fprovide (intern_c_string ("move-toolbar"), Qnil);
6194 DEFVAR_LISP ("gtk-version-string", Vgtk_version_string,
6195 doc: /* Version info for GTK+. */);
6197 char gtk_version[sizeof ".." + 3 * INT_STRLEN_BOUND (int)];
6198 int len = sprintf (gtk_version, "%d.%d.%d",
6199 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
6200 Vgtk_version_string = make_pure_string (gtk_version, len, len, 0);
6202 #endif /* USE_GTK */
6204 /* X window properties. */
6205 defsubr (&Sx_change_window_property);
6206 defsubr (&Sx_delete_window_property);
6207 defsubr (&Sx_window_property);
6209 defsubr (&Sxw_display_color_p);
6210 defsubr (&Sx_display_grayscale_p);
6211 defsubr (&Sxw_color_defined_p);
6212 defsubr (&Sxw_color_values);
6213 defsubr (&Sx_server_max_request_size);
6214 defsubr (&Sx_server_vendor);
6215 defsubr (&Sx_server_version);
6216 defsubr (&Sx_display_pixel_width);
6217 defsubr (&Sx_display_pixel_height);
6218 defsubr (&Sx_display_mm_width);
6219 defsubr (&Sx_display_mm_height);
6220 defsubr (&Sx_display_screens);
6221 defsubr (&Sx_display_planes);
6222 defsubr (&Sx_display_color_cells);
6223 defsubr (&Sx_display_visual_class);
6224 defsubr (&Sx_display_backing_store);
6225 defsubr (&Sx_display_save_under);
6226 defsubr (&Sx_display_monitor_attributes_list);
6227 defsubr (&Sx_wm_set_size_hint);
6228 defsubr (&Sx_create_frame);
6229 defsubr (&Sx_open_connection);
6230 defsubr (&Sx_close_connection);
6231 defsubr (&Sx_display_list);
6232 defsubr (&Sx_synchronize);
6233 defsubr (&Sx_backspace_delete_keys_p);
6235 defsubr (&Sx_show_tip);
6236 defsubr (&Sx_hide_tip);
6237 tip_timer = Qnil;
6238 staticpro (&tip_timer);
6239 tip_frame = Qnil;
6240 staticpro (&tip_frame);
6242 last_show_tip_args = Qnil;
6243 staticpro (&last_show_tip_args);
6245 defsubr (&Sx_uses_old_gtk_dialog);
6246 #if defined (USE_MOTIF) || defined (USE_GTK)
6247 defsubr (&Sx_file_dialog);
6248 #endif
6250 #if defined (USE_GTK) && defined (HAVE_FREETYPE)
6251 defsubr (&Sx_select_font);
6252 #endif