Update copyright year to 2015
[emacs.git] / src / xfns.c
blob2ea5f06e063b5397c7e9378ca46ebc43caef5c1e
1 /* Functions for the X window system.
3 Copyright (C) 1989, 1992-2015 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 = decode_live_terminal (object);
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 static 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, Qmenu_bar_lines);
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 static 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 /* Store the `tool-bar-lines' and `height' frame parameters. */
1116 store_frame_param (f, Qtool_bar_lines, make_number (lines));
1117 store_frame_param (f, Qheight, make_number (FRAME_LINES (f)));
1119 /* We also have to make sure that the internal border at the top of
1120 the frame, below the menu bar or tool bar, is redrawn when the
1121 tool bar disappears. This is so because the internal border is
1122 below the tool bar if one is displayed, but is below the menu bar
1123 if there isn't a tool bar. The tool bar draws into the area
1124 below the menu bar. */
1125 if (FRAME_X_WINDOW (f) && FRAME_TOOL_BAR_HEIGHT (f) == 0)
1127 clear_frame (f);
1128 clear_current_matrices (f);
1131 if ((height < old_height) && WINDOWP (f->tool_bar_window))
1132 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1134 /* Recalculate toolbar height. */
1135 f->n_tool_bar_rows = 0;
1137 adjust_frame_size (f, -1, -1, (old_height == 0 || height == 0) ? 2 : 4, 0,
1138 Qtool_bar_lines);
1140 /* adjust_frame_size might not have done anything, garbage frame
1141 here. */
1142 adjust_frame_glyphs (f);
1143 SET_FRAME_GARBAGED (f);
1144 if (FRAME_X_WINDOW (f))
1145 x_clear_under_internal_border (f);
1147 #endif /* USE_GTK */
1151 static void
1152 x_set_internal_border_width (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1154 int border;
1156 CHECK_TYPE_RANGED_INTEGER (int, arg);
1157 border = max (XINT (arg), 0);
1159 if (border != FRAME_INTERNAL_BORDER_WIDTH (f))
1161 FRAME_INTERNAL_BORDER_WIDTH (f) = border;
1163 #ifdef USE_X_TOOLKIT
1164 if (FRAME_X_OUTPUT (f)->edit_widget)
1165 widget_store_internal_border (FRAME_X_OUTPUT (f)->edit_widget);
1166 #endif
1168 if (FRAME_X_WINDOW (f) != 0)
1170 adjust_frame_size (f, -1, -1, 3, 0, Qinternal_border_width);
1172 #ifdef USE_GTK
1173 xg_clear_under_internal_border (f);
1174 #else
1175 x_clear_under_internal_border (f);
1176 #endif
1183 /* Set the foreground color for scroll bars on frame F to VALUE.
1184 VALUE should be a string, a color name. If it isn't a string or
1185 isn't a valid color name, do nothing. OLDVAL is the old value of
1186 the frame parameter. */
1188 static void
1189 x_set_scroll_bar_foreground (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1191 unsigned long pixel;
1193 if (STRINGP (value))
1194 pixel = x_decode_color (f, value, BLACK_PIX_DEFAULT (f));
1195 else
1196 pixel = -1;
1198 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
1199 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
1201 f->output_data.x->scroll_bar_foreground_pixel = pixel;
1202 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1204 /* Remove all scroll bars because they have wrong colors. */
1205 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1206 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1207 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1208 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1210 update_face_from_frame_parameter (f, Qscroll_bar_foreground, value);
1211 redraw_frame (f);
1216 /* Set the background color for scroll bars on frame F to VALUE VALUE
1217 should be a string, a color name. If it isn't a string or isn't a
1218 valid color name, do nothing. OLDVAL is the old value of the frame
1219 parameter. */
1221 static void
1222 x_set_scroll_bar_background (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1224 unsigned long pixel;
1226 if (STRINGP (value))
1227 pixel = x_decode_color (f, value, WHITE_PIX_DEFAULT (f));
1228 else
1229 pixel = -1;
1231 if (f->output_data.x->scroll_bar_background_pixel != -1)
1232 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
1234 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
1235 /* Scrollbar shadow colors. */
1236 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
1238 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
1239 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
1241 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
1243 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
1244 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
1246 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
1248 f->output_data.x->scroll_bar_background_pixel = pixel;
1249 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1251 /* Remove all scroll bars because they have wrong colors. */
1252 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1253 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1254 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1255 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1257 update_face_from_frame_parameter (f, Qscroll_bar_background, value);
1258 redraw_frame (f);
1263 /* Encode Lisp string STRING as a text in a format appropriate for
1264 XICCC (X Inter Client Communication Conventions).
1266 This can call Lisp code, so callers must GCPRO.
1268 If STRING contains only ASCII characters, do no conversion and
1269 return the string data of STRING. Otherwise, encode the text by
1270 CODING_SYSTEM, and return a newly allocated memory area which
1271 should be freed by `xfree' by a caller.
1273 Store the byte length of resulting text in *TEXT_BYTES.
1275 If the text contains only ASCII and Latin-1, store 1 in *STRING_P,
1276 which means that the `encoding' of the result can be `STRING'.
1277 Otherwise store 0 in *STRINGP, which means that the `encoding' of
1278 the result should be `COMPOUND_TEXT'. */
1280 static unsigned char *
1281 x_encode_text (Lisp_Object string, Lisp_Object coding_system,
1282 ptrdiff_t *text_bytes, int *stringp, bool *freep)
1284 int result = string_xstring_p (string);
1285 struct coding_system coding;
1287 if (result == 0)
1289 /* No multibyte character in OBJ. We need not encode it. */
1290 *text_bytes = SBYTES (string);
1291 *stringp = 1;
1292 *freep = 0;
1293 return SDATA (string);
1296 setup_coding_system (coding_system, &coding);
1297 coding.mode |= (CODING_MODE_SAFE_ENCODING | CODING_MODE_LAST_BLOCK);
1298 /* We suppress producing escape sequences for composition. */
1299 coding.common_flags &= ~CODING_ANNOTATION_MASK;
1300 coding.destination = xnmalloc (SCHARS (string), 2);
1301 coding.dst_bytes = SCHARS (string) * 2;
1302 encode_coding_object (&coding, string, 0, 0,
1303 SCHARS (string), SBYTES (string), Qnil);
1304 *text_bytes = coding.produced;
1305 *stringp = (result == 1 || !EQ (coding_system, Qcompound_text));
1306 *freep = 1;
1307 return coding.destination;
1311 /* Set the WM name to NAME for frame F. Also set the icon name.
1312 If the frame already has an icon name, use that, otherwise set the
1313 icon name to NAME. */
1315 static void
1316 x_set_name_internal (struct frame *f, Lisp_Object name)
1318 if (FRAME_X_WINDOW (f))
1320 block_input ();
1322 XTextProperty text, icon;
1323 ptrdiff_t bytes;
1324 int stringp;
1325 bool do_free_icon_value = 0, do_free_text_value = 0;
1326 Lisp_Object coding_system;
1327 Lisp_Object encoded_name;
1328 Lisp_Object encoded_icon_name;
1329 struct gcpro gcpro1;
1331 /* As ENCODE_UTF_8 may cause GC and relocation of string data,
1332 we use it before x_encode_text that may return string data. */
1333 GCPRO1 (name);
1334 encoded_name = ENCODE_UTF_8 (name);
1335 UNGCPRO;
1337 coding_system = Qcompound_text;
1338 /* Note: Encoding strategy
1340 We encode NAME by compound-text and use "COMPOUND-TEXT" in
1341 text.encoding. But, there are non-internationalized window
1342 managers which don't support that encoding. So, if NAME
1343 contains only ASCII and 8859-1 characters, encode it by
1344 iso-latin-1, and use "STRING" in text.encoding hoping that
1345 such window managers at least analyze this format correctly,
1346 i.e. treat 8-bit bytes as 8859-1 characters.
1348 We may also be able to use "UTF8_STRING" in text.encoding
1349 in the future which can encode all Unicode characters.
1350 But, for the moment, there's no way to know that the
1351 current window manager supports it or not.
1353 Either way, we also set the _NET_WM_NAME and _NET_WM_ICON_NAME
1354 properties. Per the EWMH specification, those two properties
1355 are always UTF8_STRING. This matches what gtk_window_set_title()
1356 does in the USE_GTK case. */
1357 text.value = x_encode_text (name, coding_system, &bytes,
1358 &stringp, &do_free_text_value);
1359 text.encoding = (stringp ? XA_STRING
1360 : FRAME_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1361 text.format = 8;
1362 text.nitems = bytes;
1364 if (!STRINGP (f->icon_name))
1366 icon = text;
1367 encoded_icon_name = encoded_name;
1369 else
1371 /* See the above comment "Note: Encoding strategy". */
1372 icon.value = x_encode_text (f->icon_name, coding_system, &bytes,
1373 &stringp, &do_free_icon_value);
1374 icon.encoding = (stringp ? XA_STRING
1375 : FRAME_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1376 icon.format = 8;
1377 icon.nitems = bytes;
1379 encoded_icon_name = ENCODE_UTF_8 (f->icon_name);
1382 #ifdef USE_GTK
1383 gtk_window_set_title (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1384 SSDATA (encoded_name));
1385 #else /* not USE_GTK */
1386 XSetWMName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
1387 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1388 FRAME_DISPLAY_INFO (f)->Xatom_net_wm_name,
1389 FRAME_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1390 8, PropModeReplace,
1391 SDATA (encoded_name),
1392 SBYTES (encoded_name));
1393 #endif /* not USE_GTK */
1395 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &icon);
1396 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1397 FRAME_DISPLAY_INFO (f)->Xatom_net_wm_icon_name,
1398 FRAME_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1399 8, PropModeReplace,
1400 SDATA (encoded_icon_name),
1401 SBYTES (encoded_icon_name));
1403 if (do_free_icon_value)
1404 xfree (icon.value);
1405 if (do_free_text_value)
1406 xfree (text.value);
1408 unblock_input ();
1412 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1413 x_id_name.
1415 If EXPLICIT is true, that indicates that lisp code is setting the
1416 name; if NAME is a string, set F's name to NAME and set
1417 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1419 If EXPLICIT is false, that indicates that Emacs redisplay code is
1420 suggesting a new name, which lisp code should override; if
1421 F->explicit_name is set, ignore the new name; otherwise, set it. */
1423 static void
1424 x_set_name (struct frame *f, Lisp_Object name, bool explicit)
1426 /* Make sure that requests from lisp code override requests from
1427 Emacs redisplay code. */
1428 if (explicit)
1430 /* If we're switching from explicit to implicit, we had better
1431 update the mode lines and thereby update the title. */
1432 if (f->explicit_name && NILP (name))
1433 update_mode_lines = 37;
1435 f->explicit_name = ! NILP (name);
1437 else if (f->explicit_name)
1438 return;
1440 /* If NAME is nil, set the name to the x_id_name. */
1441 if (NILP (name))
1443 /* Check for no change needed in this very common case
1444 before we do any consing. */
1445 if (!strcmp (FRAME_DISPLAY_INFO (f)->x_id_name,
1446 SSDATA (f->name)))
1447 return;
1448 name = build_string (FRAME_DISPLAY_INFO (f)->x_id_name);
1450 else
1451 CHECK_STRING (name);
1453 /* Don't change the name if it's already NAME. */
1454 if (! NILP (Fstring_equal (name, f->name)))
1455 return;
1457 fset_name (f, name);
1459 /* For setting the frame title, the title parameter should override
1460 the name parameter. */
1461 if (! NILP (f->title))
1462 name = f->title;
1464 x_set_name_internal (f, name);
1467 /* This function should be called when the user's lisp code has
1468 specified a name for the frame; the name will override any set by the
1469 redisplay code. */
1470 static void
1471 x_explicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1473 x_set_name (f, arg, 1);
1476 /* This function should be called by Emacs redisplay code to set the
1477 name; names set this way will never override names set by the user's
1478 lisp code. */
1479 void
1480 x_implicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1482 x_set_name (f, arg, 0);
1485 /* Change the title of frame F to NAME.
1486 If NAME is nil, use the frame name as the title. */
1488 static void
1489 x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
1491 /* Don't change the title if it's already NAME. */
1492 if (EQ (name, f->title))
1493 return;
1495 update_mode_lines = 38;
1497 fset_title (f, name);
1499 if (NILP (name))
1500 name = f->name;
1501 else
1502 CHECK_STRING (name);
1504 x_set_name_internal (f, name);
1507 void
1508 x_set_scroll_bar_default_width (struct frame *f)
1510 int unit = FRAME_COLUMN_WIDTH (f);
1511 #ifdef USE_TOOLKIT_SCROLL_BARS
1512 #ifdef USE_GTK
1513 int minw = xg_get_default_scrollbar_width ();
1514 #else
1515 int minw = 16;
1516 #endif
1517 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
1518 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (minw + unit - 1) / unit;
1519 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = minw;
1520 #else
1521 /* The width of a non-toolkit scrollbar is 14 pixels. */
1522 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
1523 FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
1524 = FRAME_CONFIG_SCROLL_BAR_COLS (f) * unit;
1525 #endif
1528 void
1529 x_set_scroll_bar_default_height (struct frame *f)
1531 int height = FRAME_LINE_HEIGHT (f);
1532 #ifdef USE_TOOLKIT_SCROLL_BARS
1533 #ifdef USE_GTK
1534 int min_height = xg_get_default_scrollbar_height ();
1535 #else
1536 int min_height = 16;
1537 #endif
1538 /* A minimum height of 14 doesn't look good for toolkit scroll bars. */
1539 FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) = min_height;
1540 FRAME_CONFIG_SCROLL_BAR_LINES (f) = (min_height + height - 1) / height;
1541 #else
1542 /* The height of a non-toolkit scrollbar is 14 pixels. */
1543 FRAME_CONFIG_SCROLL_BAR_LINES (f) = (14 + height - 1) / height;
1545 /* Use all of that space (aside from required margins) for the
1546 scroll bar. */
1547 FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) = 14;
1548 #endif
1552 /* Record in frame F the specified or default value according to ALIST
1553 of the parameter named PROP (a Lisp symbol). If no value is
1554 specified for PROP, look for an X default for XPROP on the frame
1555 named NAME. If that is not found either, use the value DEFLT. */
1557 static Lisp_Object
1558 x_default_scroll_bar_color_parameter (struct frame *f,
1559 Lisp_Object alist, Lisp_Object prop,
1560 const char *xprop, const char *xclass,
1561 int foreground_p)
1563 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1564 Lisp_Object tem;
1566 tem = x_get_arg (dpyinfo, alist, prop, xprop, xclass, RES_TYPE_STRING);
1567 if (EQ (tem, Qunbound))
1569 #ifdef USE_TOOLKIT_SCROLL_BARS
1571 /* See if an X resource for the scroll bar color has been
1572 specified. */
1573 AUTO_STRING (foreground, "foreground");
1574 AUTO_STRING (background, "foreground");
1575 AUTO_STRING (verticalScrollBar, "verticalScrollBar");
1576 tem = (display_x_get_resource
1577 (dpyinfo, foreground_p ? foreground : background,
1578 empty_unibyte_string,
1579 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 AUTO_FRAME_ARG (arg, prop, tem);
1599 x_set_frame_parameters (f, arg);
1600 return tem;
1606 #ifdef USE_X_TOOLKIT
1608 /* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
1609 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
1610 already be present because of the toolkit (Motif adds some of them,
1611 for example, but Xt doesn't). */
1613 static void
1614 hack_wm_protocols (struct frame *f, Widget widget)
1616 Display *dpy = XtDisplay (widget);
1617 Window w = XtWindow (widget);
1618 int need_delete = 1;
1619 int need_focus = 1;
1620 int need_save = 1;
1622 block_input ();
1624 Atom type;
1625 unsigned char *catoms;
1626 int format = 0;
1627 unsigned long nitems = 0;
1628 unsigned long bytes_after;
1630 if ((XGetWindowProperty (dpy, w,
1631 FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
1632 0, 100, False, XA_ATOM,
1633 &type, &format, &nitems, &bytes_after,
1634 &catoms)
1635 == Success)
1636 && format == 32 && type == XA_ATOM)
1638 Atom *atoms = (Atom *) catoms;
1639 while (nitems > 0)
1641 nitems--;
1642 if (atoms[nitems]
1643 == FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window)
1644 need_delete = 0;
1645 else if (atoms[nitems]
1646 == FRAME_DISPLAY_INFO (f)->Xatom_wm_take_focus)
1647 need_focus = 0;
1648 else if (atoms[nitems]
1649 == FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself)
1650 need_save = 0;
1653 if (catoms)
1654 XFree (catoms);
1657 Atom props [10];
1658 int count = 0;
1659 if (need_delete)
1660 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window;
1661 if (need_focus)
1662 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_take_focus;
1663 if (need_save)
1664 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
1665 if (count)
1666 XChangeProperty (dpy, w, FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
1667 XA_ATOM, 32, PropModeAppend,
1668 (unsigned char *) props, count);
1670 unblock_input ();
1672 #endif
1676 /* Support routines for XIC (X Input Context). */
1678 #ifdef HAVE_X_I18N
1680 static XFontSet xic_create_xfontset (struct frame *);
1681 static XIMStyle best_xim_style (XIMStyles *);
1684 /* Supported XIM styles, ordered by preference. */
1686 static const XIMStyle supported_xim_styles[] =
1688 XIMPreeditPosition | XIMStatusArea,
1689 XIMPreeditPosition | XIMStatusNothing,
1690 XIMPreeditPosition | XIMStatusNone,
1691 XIMPreeditNothing | XIMStatusArea,
1692 XIMPreeditNothing | XIMStatusNothing,
1693 XIMPreeditNothing | XIMStatusNone,
1694 XIMPreeditNone | XIMStatusArea,
1695 XIMPreeditNone | XIMStatusNothing,
1696 XIMPreeditNone | XIMStatusNone,
1701 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
1702 /* Create an X fontset on frame F with base font name BASE_FONTNAME. */
1704 static const char xic_default_fontset[] = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
1706 /* Create an Xt fontset spec from the name of a base font.
1707 If `motif' is True use the Motif syntax. */
1708 char *
1709 xic_create_fontsetname (const char *base_fontname, int motif)
1711 const char *sep = motif ? ";" : ",";
1712 char *fontsetname;
1713 char *z;
1715 /* Make a fontset name from the base font name. */
1716 if (xic_default_fontset == base_fontname)
1718 /* There is no base font name, use the default. */
1719 fontsetname = xmalloc (strlen (base_fontname) + 2);
1720 z = stpcpy (fontsetname, base_fontname);
1722 else
1724 /* Make a fontset name from the base font name.
1725 The font set will be made of the following elements:
1726 - the base font.
1727 - the base font where the charset spec is replaced by -*-*.
1728 - the same but with the family also replaced with -*-*-. */
1729 const char *p = base_fontname;
1730 ptrdiff_t i;
1732 for (i = 0; *p; p++)
1733 if (*p == '-') i++;
1734 if (i != 14)
1736 /* As the font name doesn't conform to XLFD, we can't
1737 modify it to generalize it to allcs and allfamilies.
1738 Use the specified font plus the default. */
1739 fontsetname = xmalloc (strlen (base_fontname)
1740 + strlen (xic_default_fontset) + 3);
1741 z = stpcpy (fontsetname, base_fontname);
1742 z = stpcpy (z, sep);
1743 z = stpcpy (z, xic_default_fontset);
1745 else
1747 ptrdiff_t len;
1748 const char *p1 = NULL, *p2 = NULL, *p3 = NULL;
1749 char *font_allcs = NULL;
1750 char *font_allfamilies = NULL;
1751 char *font_all = NULL;
1752 const char *allcs = "*-*-*-*-*-*-*";
1753 const char *allfamilies = "-*-*-";
1754 const char *all = "*-*-*-*-";
1755 char *base;
1757 for (i = 0, p = base_fontname; i < 8; p++)
1759 if (*p == '-')
1761 i++;
1762 if (i == 3)
1763 p1 = p + 1;
1764 else if (i == 7)
1765 p2 = p + 1;
1766 else if (i == 6)
1767 p3 = p + 1;
1770 /* If base_fontname specifies ADSTYLE, make it a
1771 wildcard. */
1772 if (*p3 != '*')
1774 ptrdiff_t diff = (p2 - p3) - 2;
1776 base = alloca (strlen (base_fontname) + 1);
1777 memcpy (base, base_fontname, p3 - base_fontname);
1778 base[p3 - base_fontname] = '*';
1779 base[(p3 - base_fontname) + 1] = '-';
1780 strcpy (base + (p3 - base_fontname) + 2, p2);
1781 p = base + (p - base_fontname) - diff;
1782 p1 = base + (p1 - base_fontname);
1783 p2 = base + (p2 - base_fontname) - diff;
1784 base_fontname = base;
1787 /* Build the font spec that matches all charsets. */
1788 len = p - base_fontname + strlen (allcs) + 1;
1789 font_allcs = alloca (len);
1790 memcpy (font_allcs, base_fontname, p - base_fontname);
1791 strcpy (font_allcs + (p - base_fontname), allcs);
1793 /* Build the font spec that matches all families and
1794 add-styles. */
1795 len = p - p1 + strlen (allcs) + strlen (allfamilies) + 1;
1796 font_allfamilies = alloca (len);
1797 strcpy (font_allfamilies, allfamilies);
1798 memcpy (font_allfamilies + strlen (allfamilies), p1, p - p1);
1799 strcpy (font_allfamilies + strlen (allfamilies) + (p - p1), allcs);
1801 /* Build the font spec that matches all. */
1802 len = p - p2 + strlen (allcs) + strlen (all) + strlen (allfamilies) + 1;
1803 font_all = alloca (len);
1804 z = stpcpy (font_all, allfamilies);
1805 z = stpcpy (z, all);
1806 memcpy (z, p2, p - p2);
1807 strcpy (z + (p - p2), allcs);
1809 /* Build the actual font set name. */
1810 len = strlen (base_fontname) + strlen (font_allcs)
1811 + strlen (font_allfamilies) + strlen (font_all) + 5;
1812 fontsetname = xmalloc (len);
1813 z = stpcpy (fontsetname, base_fontname);
1814 z = stpcpy (z, sep);
1815 z = stpcpy (z, font_allcs);
1816 z = stpcpy (z, sep);
1817 z = stpcpy (z, font_allfamilies);
1818 z = stpcpy (z, sep);
1819 z = stpcpy (z, font_all);
1822 if (motif)
1823 strcpy (z, ":");
1824 return fontsetname;
1826 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
1828 #ifdef DEBUG_XIC_FONTSET
1829 static void
1830 print_fontset_result (XFontSet xfs, char *name, char **missing_list,
1831 int missing_count)
1833 if (xfs)
1834 fprintf (stderr, "XIC Fontset created: %s\n", name);
1835 else
1837 fprintf (stderr, "XIC Fontset failed: %s\n", name);
1838 while (missing_count-- > 0)
1840 fprintf (stderr, " missing: %s\n", *missing_list);
1841 missing_list++;
1846 #endif
1848 static XFontSet
1849 xic_create_xfontset (struct frame *f)
1851 XFontSet xfs = NULL;
1852 struct font *font = FRAME_FONT (f);
1853 int pixel_size = font->pixel_size;
1854 Lisp_Object rest, frame;
1856 /* See if there is another frame already using same fontset. */
1857 FOR_EACH_FRAME (rest, frame)
1859 struct frame *cf = XFRAME (frame);
1861 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
1862 && FRAME_DISPLAY_INFO (cf) == FRAME_DISPLAY_INFO (f)
1863 && FRAME_FONT (f)
1864 && FRAME_FONT (f)->pixel_size == pixel_size)
1866 xfs = FRAME_XIC_FONTSET (cf);
1867 break;
1871 if (! xfs)
1873 char buf[256];
1874 char **missing_list;
1875 int missing_count;
1876 char *def_string;
1877 const char *xlfd_format = "-*-*-medium-r-normal--%d-*-*-*-*-*";
1879 sprintf (buf, xlfd_format, pixel_size);
1880 missing_list = NULL;
1881 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
1882 &missing_list, &missing_count, &def_string);
1883 #ifdef DEBUG_XIC_FONTSET
1884 print_fontset_result (xfs, buf, missing_list, missing_count);
1885 #endif
1886 if (missing_list)
1887 XFreeStringList (missing_list);
1888 if (! xfs)
1890 /* List of pixel sizes most likely available. Find one that
1891 is closest to pixel_size. */
1892 int sizes[] = {0, 8, 10, 11, 12, 14, 17, 18, 20, 24, 26, 34, 0};
1893 int *smaller, *larger;
1895 for (smaller = sizes; smaller[1]; smaller++)
1896 if (smaller[1] >= pixel_size)
1897 break;
1898 larger = smaller + 1;
1899 if (*larger == pixel_size)
1900 larger++;
1901 while (*smaller || *larger)
1903 int this_size;
1905 if (! *larger)
1906 this_size = *smaller--;
1907 else if (! *smaller)
1908 this_size = *larger++;
1909 else if (pixel_size - *smaller < *larger - pixel_size)
1910 this_size = *smaller--;
1911 else
1912 this_size = *larger++;
1913 sprintf (buf, xlfd_format, this_size);
1914 missing_list = NULL;
1915 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
1916 &missing_list, &missing_count, &def_string);
1917 #ifdef DEBUG_XIC_FONTSET
1918 print_fontset_result (xfs, buf, missing_list, missing_count);
1919 #endif
1920 if (missing_list)
1921 XFreeStringList (missing_list);
1922 if (xfs)
1923 break;
1926 if (! xfs)
1928 const char *last_resort = "-*-*-*-r-normal--*-*-*-*-*-*";
1930 missing_list = NULL;
1931 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), last_resort,
1932 &missing_list, &missing_count, &def_string);
1933 #ifdef DEBUG_XIC_FONTSET
1934 print_fontset_result (xfs, last_resort, missing_list, missing_count);
1935 #endif
1936 if (missing_list)
1937 XFreeStringList (missing_list);
1942 return xfs;
1945 /* Free the X fontset of frame F if it is the last frame using it. */
1947 void
1948 xic_free_xfontset (struct frame *f)
1950 Lisp_Object rest, frame;
1951 bool shared_p = 0;
1953 if (!FRAME_XIC_FONTSET (f))
1954 return;
1956 /* See if there is another frame sharing the same fontset. */
1957 FOR_EACH_FRAME (rest, frame)
1959 struct frame *cf = XFRAME (frame);
1960 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
1961 && FRAME_DISPLAY_INFO (cf) == FRAME_DISPLAY_INFO (f)
1962 && FRAME_XIC_FONTSET (cf) == FRAME_XIC_FONTSET (f))
1964 shared_p = 1;
1965 break;
1969 if (!shared_p)
1970 /* The fontset is not used anymore. It is safe to free it. */
1971 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
1973 FRAME_XIC_FONTSET (f) = NULL;
1977 /* Value is the best input style, given user preferences USER (already
1978 checked to be supported by Emacs), and styles supported by the
1979 input method XIM. */
1981 static XIMStyle
1982 best_xim_style (XIMStyles *xim)
1984 int i, j;
1985 int nr_supported = ARRAYELTS (supported_xim_styles);
1987 for (i = 0; i < nr_supported; ++i)
1988 for (j = 0; j < xim->count_styles; ++j)
1989 if (supported_xim_styles[i] == xim->supported_styles[j])
1990 return supported_xim_styles[i];
1992 /* Return the default style. */
1993 return XIMPreeditNothing | XIMStatusNothing;
1996 /* Create XIC for frame F. */
1998 void
1999 create_frame_xic (struct frame *f)
2001 XIM xim;
2002 XIC xic = NULL;
2003 XFontSet xfs = NULL;
2004 XVaNestedList status_attr = NULL;
2005 XVaNestedList preedit_attr = NULL;
2006 XRectangle s_area;
2007 XPoint spot;
2008 XIMStyle xic_style;
2010 if (FRAME_XIC (f))
2011 goto out;
2013 xim = FRAME_X_XIM (f);
2014 if (!xim)
2015 goto out;
2017 /* Determine XIC style. */
2018 xic_style = best_xim_style (FRAME_X_XIM_STYLES (f));
2020 /* Create X fontset. */
2021 if (xic_style & (XIMPreeditPosition | XIMStatusArea))
2023 xfs = xic_create_xfontset (f);
2024 if (!xfs)
2025 goto out;
2027 FRAME_XIC_FONTSET (f) = xfs;
2030 if (xic_style & XIMPreeditPosition)
2032 spot.x = 0; spot.y = 1;
2033 preedit_attr = XVaCreateNestedList (0,
2034 XNFontSet, xfs,
2035 XNForeground,
2036 FRAME_FOREGROUND_PIXEL (f),
2037 XNBackground,
2038 FRAME_BACKGROUND_PIXEL (f),
2039 (xic_style & XIMPreeditPosition
2040 ? XNSpotLocation
2041 : NULL),
2042 &spot,
2043 NULL);
2045 if (!preedit_attr)
2046 goto out;
2049 if (xic_style & XIMStatusArea)
2051 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
2052 status_attr = XVaCreateNestedList (0,
2053 XNArea,
2054 &s_area,
2055 XNFontSet,
2056 xfs,
2057 XNForeground,
2058 FRAME_FOREGROUND_PIXEL (f),
2059 XNBackground,
2060 FRAME_BACKGROUND_PIXEL (f),
2061 NULL);
2063 if (!status_attr)
2064 goto out;
2067 if (preedit_attr && status_attr)
2068 xic = XCreateIC (xim,
2069 XNInputStyle, xic_style,
2070 XNClientWindow, FRAME_X_WINDOW (f),
2071 XNFocusWindow, FRAME_X_WINDOW (f),
2072 XNStatusAttributes, status_attr,
2073 XNPreeditAttributes, preedit_attr,
2074 NULL);
2075 else if (preedit_attr)
2076 xic = XCreateIC (xim,
2077 XNInputStyle, xic_style,
2078 XNClientWindow, FRAME_X_WINDOW (f),
2079 XNFocusWindow, FRAME_X_WINDOW (f),
2080 XNPreeditAttributes, preedit_attr,
2081 NULL);
2082 else if (status_attr)
2083 xic = XCreateIC (xim,
2084 XNInputStyle, xic_style,
2085 XNClientWindow, FRAME_X_WINDOW (f),
2086 XNFocusWindow, FRAME_X_WINDOW (f),
2087 XNStatusAttributes, status_attr,
2088 NULL);
2089 else
2090 xic = XCreateIC (xim,
2091 XNInputStyle, xic_style,
2092 XNClientWindow, FRAME_X_WINDOW (f),
2093 XNFocusWindow, FRAME_X_WINDOW (f),
2094 NULL);
2096 if (!xic)
2097 goto out;
2099 FRAME_XIC (f) = xic;
2100 FRAME_XIC_STYLE (f) = xic_style;
2101 xfs = NULL; /* Don't free below. */
2103 out:
2105 if (xfs)
2106 free_frame_xic (f);
2108 if (preedit_attr)
2109 XFree (preedit_attr);
2111 if (status_attr)
2112 XFree (status_attr);
2116 /* Destroy XIC and free XIC fontset of frame F, if any. */
2118 void
2119 free_frame_xic (struct frame *f)
2121 if (FRAME_XIC (f) == NULL)
2122 return;
2124 XDestroyIC (FRAME_XIC (f));
2125 xic_free_xfontset (f);
2127 FRAME_XIC (f) = NULL;
2131 /* Place preedit area for XIC of window W's frame to specified
2132 pixel position X/Y. X and Y are relative to window W. */
2134 void
2135 xic_set_preeditarea (struct window *w, int x, int y)
2137 struct frame *f = XFRAME (w->frame);
2138 XVaNestedList attr;
2139 XPoint spot;
2141 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x) + WINDOW_LEFT_FRINGE_WIDTH (w);
2142 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
2143 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
2144 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2145 XFree (attr);
2149 /* Place status area for XIC in bottom right corner of frame F.. */
2151 void
2152 xic_set_statusarea (struct frame *f)
2154 XIC xic = FRAME_XIC (f);
2155 XVaNestedList attr;
2156 XRectangle area;
2157 XRectangle *needed;
2159 /* Negotiate geometry of status area. If input method has existing
2160 status area, use its current size. */
2161 area.x = area.y = area.width = area.height = 0;
2162 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
2163 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2164 XFree (attr);
2166 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
2167 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2168 XFree (attr);
2170 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
2172 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
2173 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2174 XFree (attr);
2177 area.width = needed->width;
2178 area.height = needed->height;
2179 area.x = FRAME_PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
2180 area.y = (FRAME_PIXEL_HEIGHT (f) - area.height
2181 - FRAME_MENUBAR_HEIGHT (f)
2182 - FRAME_TOOLBAR_TOP_HEIGHT (f)
2183 - FRAME_INTERNAL_BORDER_WIDTH (f));
2184 XFree (needed);
2186 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
2187 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2188 XFree (attr);
2192 /* Set X fontset for XIC of frame F, using base font name
2193 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
2195 void
2196 xic_set_xfontset (struct frame *f, const char *base_fontname)
2198 XVaNestedList attr;
2199 XFontSet xfs;
2201 xic_free_xfontset (f);
2203 xfs = xic_create_xfontset (f);
2205 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
2206 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
2207 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2208 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
2209 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
2210 XFree (attr);
2212 FRAME_XIC_FONTSET (f) = xfs;
2215 #endif /* HAVE_X_I18N */
2219 #ifdef USE_X_TOOLKIT
2221 /* Create and set up the X widget for frame F. */
2223 static void
2224 x_window (struct frame *f, long window_prompting, int minibuffer_only)
2226 XClassHint class_hints;
2227 XSetWindowAttributes attributes;
2228 unsigned long attribute_mask;
2229 Widget shell_widget;
2230 Widget pane_widget;
2231 Widget frame_widget;
2232 Arg al [25];
2233 int ac;
2235 block_input ();
2237 /* Use the resource name as the top-level widget name
2238 for looking up resources. Make a non-Lisp copy
2239 for the window manager, so GC relocation won't bother it.
2241 Elsewhere we specify the window name for the window manager. */
2242 f->namebuf = xstrdup (SSDATA (Vx_resource_name));
2244 ac = 0;
2245 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
2246 XtSetArg (al[ac], XtNinput, 1); ac++;
2247 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2248 XtSetArg (al[ac], XtNborderWidth, f->border_width); ac++;
2249 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2250 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2251 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2252 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
2253 applicationShellWidgetClass,
2254 FRAME_X_DISPLAY (f), al, ac);
2256 f->output_data.x->widget = shell_widget;
2257 /* maybe_set_screen_title_format (shell_widget); */
2259 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
2260 NULL, shell_widget, False,
2261 NULL, NULL, NULL, NULL);
2263 ac = 0;
2264 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2265 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2266 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2267 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2268 XtSetValues (pane_widget, al, ac);
2269 f->output_data.x->column_widget = pane_widget;
2271 /* mappedWhenManaged to false tells to the paned window to not map/unmap
2272 the emacs screen when changing menubar. This reduces flickering. */
2274 ac = 0;
2275 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2276 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
2277 XtSetArg (al[ac], XtNallowResize, 1); ac++;
2278 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
2279 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
2280 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2281 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2282 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2283 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2284 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
2285 al, ac);
2287 f->output_data.x->edit_widget = frame_widget;
2289 XtManageChild (frame_widget);
2291 /* Do some needed geometry management. */
2293 Arg gal[3];
2294 int gac = 0;
2295 int extra_borders = 0;
2296 int menubar_size
2297 = (f->output_data.x->menubar_widget
2298 ? (f->output_data.x->menubar_widget->core.height
2299 + f->output_data.x->menubar_widget->core.border_width)
2300 : 0);
2302 #if 0 /* Experimentally, we now get the right results
2303 for -geometry -0-0 without this. 24 Aug 96, rms. */
2304 if (FRAME_EXTERNAL_MENU_BAR (f))
2306 Dimension ibw = 0;
2307 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
2308 menubar_size += ibw;
2310 #endif
2312 FRAME_MENUBAR_HEIGHT (f) = menubar_size;
2314 #ifndef USE_LUCID
2315 /* Motif seems to need this amount added to the sizes
2316 specified for the shell widget. The Athena/Lucid widgets don't.
2317 Both conclusions reached experimentally. -- rms. */
2318 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
2319 &extra_borders, NULL);
2320 extra_borders *= 2;
2321 #endif
2323 f->shell_position = xmalloc (sizeof "=x++" + 4 * INT_STRLEN_BOUND (int));
2325 /* Convert our geometry parameters into a geometry string
2326 and specify it.
2327 Note that we do not specify here whether the position
2328 is a user-specified or program-specified one.
2329 We pass that information later, in x_wm_set_size_hints. */
2331 int left = f->left_pos;
2332 int xneg = window_prompting & XNegative;
2333 int top = f->top_pos;
2334 int yneg = window_prompting & YNegative;
2335 if (xneg)
2336 left = -left;
2337 if (yneg)
2338 top = -top;
2340 if (window_prompting & USPosition)
2341 sprintf (f->shell_position, "=%dx%d%c%d%c%d",
2342 FRAME_PIXEL_WIDTH (f) + extra_borders,
2343 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders,
2344 (xneg ? '-' : '+'), left,
2345 (yneg ? '-' : '+'), top);
2346 else
2348 sprintf (f->shell_position, "=%dx%d",
2349 FRAME_PIXEL_WIDTH (f) + extra_borders,
2350 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders);
2352 /* Setting x and y when the position is not specified in
2353 the geometry string will set program position in the WM hints.
2354 If Emacs had just one program position, we could set it in
2355 fallback resources, but since each make-frame call can specify
2356 different program positions, this is easier. */
2357 XtSetArg (gal[gac], XtNx, left); gac++;
2358 XtSetArg (gal[gac], XtNy, top); gac++;
2362 XtSetArg (gal[gac], XtNgeometry, f->shell_position); gac++;
2363 XtSetValues (shell_widget, gal, gac);
2366 XtManageChild (pane_widget);
2367 XtRealizeWidget (shell_widget);
2369 if (FRAME_X_EMBEDDED_P (f))
2370 XReparentWindow (FRAME_X_DISPLAY (f), XtWindow (shell_widget),
2371 f->output_data.x->parent_desc, 0, 0);
2373 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
2375 validate_x_resource_name ();
2377 class_hints.res_name = SSDATA (Vx_resource_name);
2378 class_hints.res_class = SSDATA (Vx_resource_class);
2379 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
2381 #ifdef HAVE_X_I18N
2382 FRAME_XIC (f) = NULL;
2383 if (use_xim)
2384 create_frame_xic (f);
2385 #endif
2387 f->output_data.x->wm_hints.input = True;
2388 f->output_data.x->wm_hints.flags |= InputHint;
2389 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2390 &f->output_data.x->wm_hints);
2392 hack_wm_protocols (f, shell_widget);
2394 #ifdef HACK_EDITRES
2395 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
2396 #endif
2398 /* Do a stupid property change to force the server to generate a
2399 PropertyNotify event so that the event_stream server timestamp will
2400 be initialized to something relevant to the time we created the window.
2402 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
2403 FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
2404 XA_ATOM, 32, PropModeAppend, NULL, 0);
2406 /* Make all the standard events reach the Emacs frame. */
2407 attributes.event_mask = STANDARD_EVENT_SET;
2409 #ifdef HAVE_X_I18N
2410 if (FRAME_XIC (f))
2412 /* XIM server might require some X events. */
2413 unsigned long fevent = NoEventMask;
2414 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2415 attributes.event_mask |= fevent;
2417 #endif /* HAVE_X_I18N */
2419 attribute_mask = CWEventMask;
2420 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
2421 attribute_mask, &attributes);
2423 XtMapWidget (frame_widget);
2425 /* x_set_name normally ignores requests to set the name if the
2426 requested name is the same as the current name. This is the one
2427 place where that assumption isn't correct; f->name is set, but
2428 the X server hasn't been told. */
2430 Lisp_Object name;
2431 bool explicit = f->explicit_name;
2433 f->explicit_name = 0;
2434 name = f->name;
2435 fset_name (f, Qnil);
2436 x_set_name (f, name, explicit);
2439 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2440 f->output_data.x->current_cursor
2441 = f->output_data.x->text_cursor);
2443 unblock_input ();
2445 /* This is a no-op, except under Motif. Make sure main areas are
2446 set to something reasonable, in case we get an error later. */
2447 lw_set_main_areas (pane_widget, 0, frame_widget);
2450 #else /* not USE_X_TOOLKIT */
2451 #ifdef USE_GTK
2452 static void
2453 x_window (struct frame *f)
2455 if (! xg_create_frame_widgets (f))
2456 error ("Unable to create window");
2458 #ifdef HAVE_X_I18N
2459 FRAME_XIC (f) = NULL;
2460 if (use_xim)
2462 block_input ();
2463 create_frame_xic (f);
2464 if (FRAME_XIC (f))
2466 /* XIM server might require some X events. */
2467 unsigned long fevent = NoEventMask;
2468 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2470 if (fevent != NoEventMask)
2472 XSetWindowAttributes attributes;
2473 XWindowAttributes wattr;
2474 unsigned long attribute_mask;
2476 XGetWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2477 &wattr);
2478 attributes.event_mask = wattr.your_event_mask | fevent;
2479 attribute_mask = CWEventMask;
2480 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2481 attribute_mask, &attributes);
2484 unblock_input ();
2486 #endif
2489 #else /*! USE_GTK */
2490 /* Create and set up the X window for frame F. */
2492 static void
2493 x_window (struct frame *f)
2495 XClassHint class_hints;
2496 XSetWindowAttributes attributes;
2497 unsigned long attribute_mask;
2499 attributes.background_pixel = FRAME_BACKGROUND_PIXEL (f);
2500 attributes.border_pixel = f->output_data.x->border_pixel;
2501 attributes.bit_gravity = StaticGravity;
2502 attributes.backing_store = NotUseful;
2503 attributes.save_under = True;
2504 attributes.event_mask = STANDARD_EVENT_SET;
2505 attributes.colormap = FRAME_X_COLORMAP (f);
2506 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
2507 | CWColormap);
2509 block_input ();
2510 FRAME_X_WINDOW (f)
2511 = XCreateWindow (FRAME_X_DISPLAY (f),
2512 f->output_data.x->parent_desc,
2513 f->left_pos,
2514 f->top_pos,
2515 FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f),
2516 f->border_width,
2517 CopyFromParent, /* depth */
2518 InputOutput, /* class */
2519 FRAME_X_VISUAL (f),
2520 attribute_mask, &attributes);
2522 #ifdef HAVE_X_I18N
2523 if (use_xim)
2525 create_frame_xic (f);
2526 if (FRAME_XIC (f))
2528 /* XIM server might require some X events. */
2529 unsigned long fevent = NoEventMask;
2530 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2531 attributes.event_mask |= fevent;
2532 attribute_mask = CWEventMask;
2533 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2534 attribute_mask, &attributes);
2537 #endif /* HAVE_X_I18N */
2539 validate_x_resource_name ();
2541 class_hints.res_name = SSDATA (Vx_resource_name);
2542 class_hints.res_class = SSDATA (Vx_resource_class);
2543 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
2545 /* This indicates that we use the "Passive Input" input model.
2546 Unless we do this, we don't get the Focus{In,Out} events that we
2547 need to draw the cursor correctly. Accursed bureaucrats.
2548 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
2550 f->output_data.x->wm_hints.input = True;
2551 f->output_data.x->wm_hints.flags |= InputHint;
2552 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2553 &f->output_data.x->wm_hints);
2554 f->output_data.x->wm_hints.icon_pixmap = None;
2556 /* Request "save yourself" and "delete window" commands from wm. */
2558 Atom protocols[2];
2559 protocols[0] = FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window;
2560 protocols[1] = FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
2561 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
2564 /* x_set_name normally ignores requests to set the name if the
2565 requested name is the same as the current name. This is the one
2566 place where that assumption isn't correct; f->name is set, but
2567 the X server hasn't been told. */
2569 Lisp_Object name;
2570 bool explicit = f->explicit_name;
2572 f->explicit_name = 0;
2573 name = f->name;
2574 fset_name (f, Qnil);
2575 x_set_name (f, name, explicit);
2578 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2579 f->output_data.x->current_cursor
2580 = f->output_data.x->text_cursor);
2582 unblock_input ();
2584 if (FRAME_X_WINDOW (f) == 0)
2585 error ("Unable to create window");
2588 #endif /* not USE_GTK */
2589 #endif /* not USE_X_TOOLKIT */
2591 /* Verify that the icon position args for this window are valid. */
2593 static void
2594 x_icon_verify (struct frame *f, Lisp_Object parms)
2596 Lisp_Object icon_x, icon_y;
2598 /* Set the position of the icon. Note that twm groups all
2599 icons in an icon window. */
2600 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2601 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2602 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2604 CHECK_NUMBER (icon_x);
2605 CHECK_NUMBER (icon_y);
2607 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2608 error ("Both left and top icon corners of icon must be specified");
2611 /* Handle the icon stuff for this window. Perhaps later we might
2612 want an x_set_icon_position which can be called interactively as
2613 well. */
2615 static void
2616 x_icon (struct frame *f, Lisp_Object parms)
2618 Lisp_Object icon_x, icon_y;
2619 #if 0
2620 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2621 #endif
2623 /* Set the position of the icon. Note that twm groups all
2624 icons in an icon window. */
2625 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2626 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2627 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2629 CHECK_TYPE_RANGED_INTEGER (int, icon_x);
2630 CHECK_TYPE_RANGED_INTEGER (int, icon_y);
2632 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2633 error ("Both left and top icon corners of icon must be specified");
2635 block_input ();
2637 if (! EQ (icon_x, Qunbound))
2638 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
2640 #if 0 /* x_get_arg removes the visibility parameter as a side effect,
2641 but x_create_frame still needs it. */
2642 /* Start up iconic or window? */
2643 x_wm_set_window_state
2644 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
2645 Qicon)
2646 ? IconicState
2647 : NormalState));
2648 #endif
2650 x_text_icon (f, SSDATA ((!NILP (f->icon_name)
2651 ? f->icon_name
2652 : f->name)));
2654 unblock_input ();
2657 /* Make the GCs needed for this window, setting the
2658 background, border and mouse colors; also create the
2659 mouse cursor and the gray border tile. */
2661 static void
2662 x_make_gc (struct frame *f)
2664 XGCValues gc_values;
2666 block_input ();
2668 /* Create the GCs of this frame.
2669 Note that many default values are used. */
2671 gc_values.foreground = FRAME_FOREGROUND_PIXEL (f);
2672 gc_values.background = FRAME_BACKGROUND_PIXEL (f);
2673 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
2674 f->output_data.x->normal_gc
2675 = XCreateGC (FRAME_X_DISPLAY (f),
2676 FRAME_X_WINDOW (f),
2677 GCLineWidth | GCForeground | GCBackground,
2678 &gc_values);
2680 /* Reverse video style. */
2681 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2682 gc_values.background = FRAME_FOREGROUND_PIXEL (f);
2683 f->output_data.x->reverse_gc
2684 = XCreateGC (FRAME_X_DISPLAY (f),
2685 FRAME_X_WINDOW (f),
2686 GCForeground | GCBackground | GCLineWidth,
2687 &gc_values);
2689 /* Cursor has cursor-color background, background-color foreground. */
2690 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2691 gc_values.background = f->output_data.x->cursor_pixel;
2692 gc_values.fill_style = FillOpaqueStippled;
2693 f->output_data.x->cursor_gc
2694 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2695 (GCForeground | GCBackground
2696 | GCFillStyle | GCLineWidth),
2697 &gc_values);
2699 /* Create the gray border tile used when the pointer is not in
2700 the frame. Since this depends on the frame's pixel values,
2701 this must be done on a per-frame basis. */
2702 f->output_data.x->border_tile
2703 = (XCreatePixmapFromBitmapData
2704 (FRAME_X_DISPLAY (f), FRAME_DISPLAY_INFO (f)->root_window,
2705 gray_bits, gray_width, gray_height,
2706 FRAME_FOREGROUND_PIXEL (f),
2707 FRAME_BACKGROUND_PIXEL (f),
2708 DefaultDepth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f))));
2710 unblock_input ();
2714 /* Free what was allocated in x_make_gc. */
2716 void
2717 x_free_gcs (struct frame *f)
2719 Display *dpy = FRAME_X_DISPLAY (f);
2721 block_input ();
2723 if (f->output_data.x->normal_gc)
2725 XFreeGC (dpy, f->output_data.x->normal_gc);
2726 f->output_data.x->normal_gc = 0;
2729 if (f->output_data.x->reverse_gc)
2731 XFreeGC (dpy, f->output_data.x->reverse_gc);
2732 f->output_data.x->reverse_gc = 0;
2735 if (f->output_data.x->cursor_gc)
2737 XFreeGC (dpy, f->output_data.x->cursor_gc);
2738 f->output_data.x->cursor_gc = 0;
2741 if (f->output_data.x->border_tile)
2743 XFreePixmap (dpy, f->output_data.x->border_tile);
2744 f->output_data.x->border_tile = 0;
2747 unblock_input ();
2751 /* Handler for signals raised during x_create_frame and
2752 x_create_tip_frame. FRAME is the frame which is partially
2753 constructed. */
2755 static Lisp_Object
2756 unwind_create_frame (Lisp_Object frame)
2758 struct frame *f = XFRAME (frame);
2760 /* If frame is already dead, nothing to do. This can happen if the
2761 display is disconnected after the frame has become official, but
2762 before x_create_frame removes the unwind protect. */
2763 if (!FRAME_LIVE_P (f))
2764 return Qnil;
2766 /* If frame is ``official'', nothing to do. */
2767 if (NILP (Fmemq (frame, Vframe_list)))
2769 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2770 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2771 #endif
2773 x_free_frame_resources (f);
2774 free_glyphs (f);
2776 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2777 /* Check that reference counts are indeed correct. */
2778 eassert (dpyinfo->reference_count == dpyinfo_refcount);
2779 eassert (dpyinfo->terminal->image_cache->refcount == image_cache_refcount);
2780 #endif
2781 return Qt;
2784 return Qnil;
2787 static void
2788 do_unwind_create_frame (Lisp_Object frame)
2790 unwind_create_frame (frame);
2793 static void
2794 x_default_font_parameter (struct frame *f, Lisp_Object parms)
2796 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2797 Lisp_Object font_param = x_get_arg (dpyinfo, parms, Qfont, NULL, NULL,
2798 RES_TYPE_STRING);
2799 Lisp_Object font = Qnil;
2800 if (EQ (font_param, Qunbound))
2801 font_param = Qnil;
2803 if (NILP (font_param))
2805 /* System font should take precedence over X resources. We suggest this
2806 regardless of font-use-system-font because .emacs may not have been
2807 read yet. */
2808 const char *system_font = xsettings_get_system_font ();
2809 if (system_font)
2810 font = font_open_by_name (f, build_unibyte_string (system_font));
2813 if (NILP (font))
2814 font = !NILP (font_param) ? font_param
2815 : x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
2817 if (! FONTP (font) && ! STRINGP (font))
2819 const char *names[]
2821 #ifdef HAVE_XFT
2822 /* This will find the normal Xft font. */
2823 "monospace-10",
2824 #endif
2825 "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1",
2826 "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2827 "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2828 /* This was formerly the first thing tried, but it finds
2829 too many fonts and takes too long. */
2830 "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1",
2831 /* If those didn't work, look for something which will
2832 at least work. */
2833 "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1",
2834 "fixed",
2835 NULL };
2836 int i;
2838 for (i = 0; names[i]; i++)
2840 font = font_open_by_name (f, build_unibyte_string (names[i]));
2841 if (! NILP (font))
2842 break;
2844 if (NILP (font))
2845 error ("No suitable font was found");
2847 else if (!NILP (font_param))
2849 /* Remember the explicit font parameter, so we can re-apply it after
2850 we've applied the `default' face settings. */
2851 AUTO_FRAME_ARG (arg, Qfont_param, font_param);
2852 x_set_frame_parameters (f, arg);
2855 /* This call will make X resources override any system font setting. */
2856 x_default_parameter (f, parms, Qfont, font, "font", "Font", RES_TYPE_STRING);
2860 DEFUN ("x-wm-set-size-hint", Fx_wm_set_size_hint, Sx_wm_set_size_hint,
2861 0, 1, 0,
2862 doc: /* Send the size hints for frame FRAME to the window manager.
2863 If FRAME is omitted or nil, use the selected frame.
2864 Signal error if FRAME is not an X frame. */)
2865 (Lisp_Object frame)
2867 struct frame *f = decode_window_system_frame (frame);
2869 block_input ();
2870 x_wm_set_size_hint (f, 0, 0);
2871 unblock_input ();
2872 return Qnil;
2875 static void
2876 set_machine_and_pid_properties (struct frame *f)
2878 /* This will set WM_CLIENT_MACHINE and WM_LOCALE_NAME. */
2879 XSetWMProperties (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), NULL, NULL,
2880 NULL, 0, NULL, NULL, NULL);
2881 pid_t pid = getpid ();
2882 if (pid <= 0xffffffffu)
2884 unsigned long xpid = pid;
2885 XChangeProperty (FRAME_X_DISPLAY (f),
2886 FRAME_OUTER_WINDOW (f),
2887 XInternAtom (FRAME_X_DISPLAY (f),
2888 "_NET_WM_PID",
2889 False),
2890 XA_CARDINAL, 32, PropModeReplace,
2891 (unsigned char *) &xpid, 1);
2895 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
2896 1, 1, 0,
2897 doc: /* Make a new X window, which is called a "frame" in Emacs terms.
2898 Return an Emacs frame object. PARMS is an alist of frame parameters.
2899 If the parameters specify that the frame should not have a minibuffer,
2900 and do not specify a specific minibuffer window to use, then
2901 `default-minibuffer-frame' must be a frame whose minibuffer can be
2902 shared by the new frame.
2904 This function is an internal primitive--use `make-frame' instead. */)
2905 (Lisp_Object parms)
2907 struct frame *f;
2908 Lisp_Object frame, tem;
2909 Lisp_Object name;
2910 int minibuffer_only = 0;
2911 long window_prompting = 0;
2912 ptrdiff_t count = SPECPDL_INDEX ();
2913 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
2914 Lisp_Object display;
2915 struct x_display_info *dpyinfo = NULL;
2916 Lisp_Object parent;
2917 struct kboard *kb;
2919 parms = Fcopy_alist (parms);
2921 /* Use this general default value to start with
2922 until we know if this frame has a specified name. */
2923 Vx_resource_name = Vinvocation_name;
2925 display = x_get_arg (dpyinfo, parms, Qterminal, 0, 0, RES_TYPE_NUMBER);
2926 if (EQ (display, Qunbound))
2927 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
2928 if (EQ (display, Qunbound))
2929 display = Qnil;
2930 dpyinfo = check_x_display_info (display);
2931 kb = dpyinfo->terminal->kboard;
2933 if (!dpyinfo->terminal->name)
2934 error ("Terminal is not live, can't create new frames on it");
2936 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
2937 if (!STRINGP (name)
2938 && ! EQ (name, Qunbound)
2939 && ! NILP (name))
2940 error ("Invalid frame name--not a string or nil");
2942 if (STRINGP (name))
2943 Vx_resource_name = name;
2945 /* See if parent window is specified. */
2946 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
2947 if (EQ (parent, Qunbound))
2948 parent = Qnil;
2949 if (! NILP (parent))
2950 CHECK_NUMBER (parent);
2952 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
2953 /* No need to protect DISPLAY because that's not used after passing
2954 it to make_frame_without_minibuffer. */
2955 frame = Qnil;
2956 GCPRO4 (parms, parent, name, frame);
2957 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
2958 RES_TYPE_SYMBOL);
2959 if (EQ (tem, Qnone) || NILP (tem))
2960 f = make_frame_without_minibuffer (Qnil, kb, display);
2961 else if (EQ (tem, Qonly))
2963 f = make_minibuffer_frame ();
2964 minibuffer_only = 1;
2966 else if (WINDOWP (tem))
2967 f = make_frame_without_minibuffer (tem, kb, display);
2968 else
2969 f = make_frame (1);
2971 XSETFRAME (frame, f);
2973 f->terminal = dpyinfo->terminal;
2975 f->output_method = output_x_window;
2976 f->output_data.x = xzalloc (sizeof *f->output_data.x);
2977 f->output_data.x->icon_bitmap = -1;
2978 FRAME_FONTSET (f) = -1;
2979 f->output_data.x->scroll_bar_foreground_pixel = -1;
2980 f->output_data.x->scroll_bar_background_pixel = -1;
2981 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
2982 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
2983 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
2984 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
2985 f->output_data.x->white_relief.pixel = -1;
2986 f->output_data.x->black_relief.pixel = -1;
2988 fset_icon_name (f,
2989 x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
2990 RES_TYPE_STRING));
2991 if (! STRINGP (f->icon_name))
2992 fset_icon_name (f, Qnil);
2994 FRAME_DISPLAY_INFO (f) = dpyinfo;
2996 /* With FRAME_DISPLAY_INFO set up, this unwind-protect is safe. */
2997 record_unwind_protect (do_unwind_create_frame, frame);
2999 /* These colors will be set anyway later, but it's important
3000 to get the color reference counts right, so initialize them! */
3002 Lisp_Object black;
3003 struct gcpro gcpro1;
3005 /* Function x_decode_color can signal an error. Make
3006 sure to initialize color slots so that we won't try
3007 to free colors we haven't allocated. */
3008 FRAME_FOREGROUND_PIXEL (f) = -1;
3009 FRAME_BACKGROUND_PIXEL (f) = -1;
3010 f->output_data.x->cursor_pixel = -1;
3011 f->output_data.x->cursor_foreground_pixel = -1;
3012 f->output_data.x->border_pixel = -1;
3013 f->output_data.x->mouse_pixel = -1;
3015 black = build_string ("black");
3016 GCPRO1 (black);
3017 FRAME_FOREGROUND_PIXEL (f)
3018 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3019 FRAME_BACKGROUND_PIXEL (f)
3020 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3021 f->output_data.x->cursor_pixel
3022 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3023 f->output_data.x->cursor_foreground_pixel
3024 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3025 f->output_data.x->border_pixel
3026 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3027 f->output_data.x->mouse_pixel
3028 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3029 UNGCPRO;
3032 /* Specify the parent under which to make this X window. */
3033 if (!NILP (parent))
3035 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
3036 f->output_data.x->explicit_parent = 1;
3038 else
3040 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
3041 f->output_data.x->explicit_parent = 0;
3044 /* Set the name; the functions to which we pass f expect the name to
3045 be set. */
3046 if (EQ (name, Qunbound) || NILP (name))
3048 fset_name (f, build_string (dpyinfo->x_id_name));
3049 f->explicit_name = 0;
3051 else
3053 fset_name (f, name);
3054 f->explicit_name = 1;
3055 /* Use the frame's title when getting resources for this frame. */
3056 specbind (Qx_resource_name, name);
3059 #ifdef HAVE_FREETYPE
3060 #ifdef HAVE_XFT
3061 register_font_driver (&xftfont_driver, f);
3062 #else /* not HAVE_XFT */
3063 register_font_driver (&ftxfont_driver, f);
3064 #endif /* not HAVE_XFT */
3065 #endif /* HAVE_FREETYPE */
3066 register_font_driver (&xfont_driver, f);
3068 x_default_parameter (f, parms, Qfont_backend, Qnil,
3069 "fontBackend", "FontBackend", RES_TYPE_STRING);
3071 /* Extract the window parameters from the supplied values
3072 that are needed to determine window geometry. */
3073 x_default_font_parameter (f, parms);
3074 if (!FRAME_FONT (f))
3076 delete_frame (frame, Qnoelisp);
3077 error ("Invalid frame font");
3080 /* Frame contents get displaced if an embedded X window has a border. */
3081 if (! FRAME_X_EMBEDDED_P (f))
3082 x_default_parameter (f, parms, Qborder_width, make_number (0),
3083 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
3085 /* This defaults to 1 in order to match xterm. We recognize either
3086 internalBorderWidth or internalBorder (which is what xterm calls
3087 it). */
3088 if (NILP (Fassq (Qinternal_border_width, parms)))
3090 Lisp_Object value;
3092 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
3093 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
3094 if (! EQ (value, Qunbound))
3095 parms = Fcons (Fcons (Qinternal_border_width, value),
3096 parms);
3098 x_default_parameter (f, parms, Qinternal_border_width,
3099 #ifdef USE_GTK /* We used to impose 0 in xg_create_frame_widgets. */
3100 make_number (0),
3101 #else
3102 make_number (1),
3103 #endif
3104 "internalBorderWidth", "internalBorderWidth",
3105 RES_TYPE_NUMBER);
3106 x_default_parameter (f, parms, Qright_divider_width, make_number (0),
3107 NULL, NULL, RES_TYPE_NUMBER);
3108 x_default_parameter (f, parms, Qbottom_divider_width, make_number (0),
3109 NULL, NULL, RES_TYPE_NUMBER);
3110 x_default_parameter (f, parms, Qvertical_scroll_bars,
3111 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3112 Qright,
3113 #else
3114 Qleft,
3115 #endif
3116 "verticalScrollBars", "ScrollBars",
3117 RES_TYPE_SYMBOL);
3118 x_default_parameter (f, parms, Qhorizontal_scroll_bars, Qnil,
3119 "horizontalScrollBars", "ScrollBars",
3120 RES_TYPE_SYMBOL);
3121 /* Also do the stuff which must be set before the window exists. */
3122 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
3123 "foreground", "Foreground", RES_TYPE_STRING);
3124 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
3125 "background", "Background", RES_TYPE_STRING);
3126 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
3127 "pointerColor", "Foreground", RES_TYPE_STRING);
3128 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
3129 "borderColor", "BorderColor", RES_TYPE_STRING);
3130 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
3131 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
3132 x_default_parameter (f, parms, Qline_spacing, Qnil,
3133 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
3134 x_default_parameter (f, parms, Qleft_fringe, Qnil,
3135 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
3136 x_default_parameter (f, parms, Qright_fringe, Qnil,
3137 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
3139 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
3140 "scrollBarForeground",
3141 "ScrollBarForeground", 1);
3142 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
3143 "scrollBarBackground",
3144 "ScrollBarBackground", 0);
3146 #ifdef GLYPH_DEBUG
3147 image_cache_refcount =
3148 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
3149 dpyinfo_refcount = dpyinfo->reference_count;
3150 #endif /* GLYPH_DEBUG */
3152 /* Init faces before x_default_parameter is called for the
3153 scroll-bar-width parameter because otherwise we end up in
3154 init_iterator with a null face cache, which should not happen. */
3155 init_frame_faces (f);
3157 /* The following call of change_frame_size is needed since otherwise
3158 x_set_tool_bar_lines will already work with the character sizes
3159 installed by init_frame_faces while the frame's pixel size is
3160 still calculated from a character size of 1 and we subsequently
3161 hit the (height >= 0) assertion in window_box_height.
3163 The non-pixelwise code apparently worked around this because it
3164 had one frame line vs one toolbar line which left us with a zero
3165 root window height which was obviously wrong as well ... */
3166 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
3167 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 5, 1, Qnil);
3169 /* Set the menu-bar-lines and tool-bar-lines parameters. We don't
3170 look up the X resources controlling the menu-bar and tool-bar
3171 here; they are processed specially at startup, and reflected in
3172 the values of the mode variables. */
3174 x_default_parameter (f, parms, Qmenu_bar_lines,
3175 NILP (Vmenu_bar_mode)
3176 ? make_number (0) : make_number (1),
3177 NULL, NULL, RES_TYPE_NUMBER);
3178 x_default_parameter (f, parms, Qtool_bar_lines,
3179 NILP (Vtool_bar_mode)
3180 ? make_number (0) : make_number (1),
3181 NULL, NULL, RES_TYPE_NUMBER);
3183 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
3184 "bufferPredicate", "BufferPredicate",
3185 RES_TYPE_SYMBOL);
3186 x_default_parameter (f, parms, Qtitle, Qnil,
3187 "title", "Title", RES_TYPE_STRING);
3188 x_default_parameter (f, parms, Qwait_for_wm, Qt,
3189 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN);
3190 x_default_parameter (f, parms, Qfullscreen, Qnil,
3191 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
3192 x_default_parameter (f, parms, Qtool_bar_position,
3193 FRAME_TOOL_BAR_POSITION (f), 0, 0, RES_TYPE_SYMBOL);
3195 /* Compute the size of the X window. */
3196 window_prompting = x_figure_window_size (f, parms, 1);
3198 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
3199 f->no_split = minibuffer_only || EQ (tem, Qt);
3201 x_icon_verify (f, parms);
3203 /* Create the X widget or window. */
3204 #ifdef USE_X_TOOLKIT
3205 x_window (f, window_prompting, minibuffer_only);
3206 #else
3207 x_window (f);
3208 #endif
3210 x_icon (f, parms);
3211 x_make_gc (f);
3213 /* Now consider the frame official. */
3214 f->terminal->reference_count++;
3215 FRAME_DISPLAY_INFO (f)->reference_count++;
3216 Vframe_list = Fcons (frame, Vframe_list);
3218 /* We need to do this after creating the X window, so that the
3219 icon-creation functions can say whose icon they're describing. */
3220 x_default_parameter (f, parms, Qicon_type, Qt,
3221 "bitmapIcon", "BitmapIcon", RES_TYPE_BOOLEAN);
3223 x_default_parameter (f, parms, Qauto_raise, Qnil,
3224 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3225 x_default_parameter (f, parms, Qauto_lower, Qnil,
3226 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3227 x_default_parameter (f, parms, Qcursor_type, Qbox,
3228 "cursorType", "CursorType", RES_TYPE_SYMBOL);
3229 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
3230 "scrollBarWidth", "ScrollBarWidth",
3231 RES_TYPE_NUMBER);
3232 x_default_parameter (f, parms, Qscroll_bar_height, Qnil,
3233 "scrollBarHeight", "ScrollBarHeight",
3234 RES_TYPE_NUMBER);
3235 x_default_parameter (f, parms, Qalpha, Qnil,
3236 "alpha", "Alpha", RES_TYPE_NUMBER);
3238 /* Consider frame official, now. */
3239 f->can_x_set_window_size = true;
3241 adjust_frame_size (f, FRAME_TEXT_WIDTH (f), FRAME_TEXT_HEIGHT (f), 0, 1, Qnil);
3243 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3244 /* Create the menu bar. */
3245 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3247 /* If this signals an error, we haven't set size hints for the
3248 frame and we didn't make it visible. */
3249 initialize_frame_menubar (f);
3251 #ifndef USE_GTK
3252 /* This is a no-op, except under Motif where it arranges the
3253 main window for the widgets on it. */
3254 lw_set_main_areas (f->output_data.x->column_widget,
3255 f->output_data.x->menubar_widget,
3256 f->output_data.x->edit_widget);
3257 #endif /* not USE_GTK */
3259 #endif /* USE_X_TOOLKIT || USE_GTK */
3261 /* Tell the server what size and position, etc, we want, and how
3262 badly we want them. This should be done after we have the menu
3263 bar so that its size can be taken into account. */
3264 block_input ();
3265 x_wm_set_size_hint (f, window_prompting, 0);
3266 unblock_input ();
3268 /* Make the window appear on the frame and enable display, unless
3269 the caller says not to. However, with explicit parent, Emacs
3270 cannot control visibility, so don't try. */
3271 if (! f->output_data.x->explicit_parent)
3273 Lisp_Object visibility;
3275 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
3276 RES_TYPE_SYMBOL);
3277 if (EQ (visibility, Qunbound))
3278 visibility = Qt;
3280 if (EQ (visibility, Qicon))
3281 x_iconify_frame (f);
3282 else if (! NILP (visibility))
3283 x_make_frame_visible (f);
3284 else
3286 /* Must have been Qnil. */
3290 block_input ();
3292 /* Set machine name and pid for the purpose of window managers. */
3293 set_machine_and_pid_properties (f);
3295 /* Set the WM leader property. GTK does this itself, so this is not
3296 needed when using GTK. */
3297 if (dpyinfo->client_leader_window != 0)
3299 XChangeProperty (FRAME_X_DISPLAY (f),
3300 FRAME_OUTER_WINDOW (f),
3301 dpyinfo->Xatom_wm_client_leader,
3302 XA_WINDOW, 32, PropModeReplace,
3303 (unsigned char *) &dpyinfo->client_leader_window, 1);
3306 unblock_input ();
3308 /* Initialize `default-minibuffer-frame' in case this is the first
3309 frame on this terminal. */
3310 if (FRAME_HAS_MINIBUF_P (f)
3311 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
3312 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
3313 kset_default_minibuffer_frame (kb, frame);
3315 /* All remaining specified parameters, which have not been "used"
3316 by x_get_arg and friends, now go in the misc. alist of the frame. */
3317 for (tem = parms; CONSP (tem); tem = XCDR (tem))
3318 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
3319 fset_param_alist (f, Fcons (XCAR (tem), f->param_alist));
3321 UNGCPRO;
3323 /* Make sure windows on this frame appear in calls to next-window
3324 and similar functions. */
3325 Vwindow_list = Qnil;
3327 return unbind_to (count, frame);
3331 /* FRAME is used only to get a handle on the X display. We don't pass the
3332 display info directly because we're called from frame.c, which doesn't
3333 know about that structure. */
3335 Lisp_Object
3336 x_get_focus_frame (struct frame *frame)
3338 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (frame);
3339 Lisp_Object xfocus;
3340 if (! dpyinfo->x_focus_frame)
3341 return Qnil;
3343 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
3344 return xfocus;
3348 /* In certain situations, when the window manager follows a
3349 click-to-focus policy, there seems to be no way around calling
3350 XSetInputFocus to give another frame the input focus .
3352 In an ideal world, XSetInputFocus should generally be avoided so
3353 that applications don't interfere with the window manager's focus
3354 policy. But I think it's okay to use when it's clearly done
3355 following a user-command. */
3357 void
3358 x_focus_frame (struct frame *f)
3360 Display *dpy = FRAME_X_DISPLAY (f);
3362 block_input ();
3363 x_catch_errors (dpy);
3365 if (FRAME_X_EMBEDDED_P (f))
3367 /* For Xembedded frames, normally the embedder forwards key
3368 events. See XEmbed Protocol Specification at
3369 http://freedesktop.org/wiki/Specifications/xembed-spec */
3370 xembed_request_focus (f);
3372 else
3374 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3375 RevertToParent, CurrentTime);
3376 x_ewmh_activate_frame (f);
3379 x_uncatch_errors ();
3380 unblock_input ();
3384 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
3385 doc: /* Internal function called by `color-defined-p', which see
3386 .\(Note that the Nextstep version of this function ignores FRAME.) */)
3387 (Lisp_Object color, Lisp_Object frame)
3389 XColor foo;
3390 struct frame *f = decode_window_system_frame (frame);
3392 CHECK_STRING (color);
3394 if (x_defined_color (f, SSDATA (color), &foo, 0))
3395 return Qt;
3396 else
3397 return Qnil;
3400 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
3401 doc: /* Internal function called by `color-values', which see. */)
3402 (Lisp_Object color, Lisp_Object frame)
3404 XColor foo;
3405 struct frame *f = decode_window_system_frame (frame);
3407 CHECK_STRING (color);
3409 if (x_defined_color (f, SSDATA (color), &foo, 0))
3410 return list3i (foo.red, foo.green, foo.blue);
3411 else
3412 return Qnil;
3415 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
3416 doc: /* Internal function called by `display-color-p', which see. */)
3417 (Lisp_Object terminal)
3419 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3421 if (dpyinfo->n_planes <= 2)
3422 return Qnil;
3424 switch (dpyinfo->visual->class)
3426 case StaticColor:
3427 case PseudoColor:
3428 case TrueColor:
3429 case DirectColor:
3430 return Qt;
3432 default:
3433 return Qnil;
3437 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
3438 0, 1, 0,
3439 doc: /* Return t if the X display supports shades of gray.
3440 Note that color displays do support shades of gray.
3441 The optional argument TERMINAL specifies which display to ask about.
3442 TERMINAL should be a terminal object, a frame or a display name (a string).
3443 If omitted or nil, that stands for the selected frame's display. */)
3444 (Lisp_Object terminal)
3446 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3448 if (dpyinfo->n_planes <= 1)
3449 return Qnil;
3451 switch (dpyinfo->visual->class)
3453 case StaticColor:
3454 case PseudoColor:
3455 case TrueColor:
3456 case DirectColor:
3457 case StaticGray:
3458 case GrayScale:
3459 return Qt;
3461 default:
3462 return Qnil;
3466 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
3467 0, 1, 0,
3468 doc: /* Return the width in pixels of the X display TERMINAL.
3469 The optional argument TERMINAL specifies which display to ask about.
3470 TERMINAL should be a terminal object, a frame or a display name (a string).
3471 If omitted or nil, that stands for the selected frame's display.
3473 On \"multi-monitor\" setups this refers to the pixel width for all
3474 physical monitors associated with TERMINAL. To get information for
3475 each physical monitor, use `display-monitor-attributes-list'. */)
3476 (Lisp_Object terminal)
3478 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3480 return make_number (x_display_pixel_width (dpyinfo));
3483 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
3484 Sx_display_pixel_height, 0, 1, 0,
3485 doc: /* Return the height in pixels of the X display TERMINAL.
3486 The optional argument TERMINAL specifies which display to ask about.
3487 TERMINAL should be a terminal object, a frame or a display name (a string).
3488 If omitted or nil, that stands for the selected frame's display.
3490 On \"multi-monitor\" setups this refers to the pixel height for all
3491 physical monitors associated with TERMINAL. To get information for
3492 each physical monitor, use `display-monitor-attributes-list'. */)
3493 (Lisp_Object terminal)
3495 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3497 return make_number (x_display_pixel_height (dpyinfo));
3500 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
3501 0, 1, 0,
3502 doc: /* Return the number of bitplanes of the X display TERMINAL.
3503 The optional argument TERMINAL specifies which display to ask about.
3504 TERMINAL should be a terminal object, a frame or a display name (a string).
3505 If omitted or nil, that stands for the selected frame's display. */)
3506 (Lisp_Object terminal)
3508 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3510 return make_number (dpyinfo->n_planes);
3513 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
3514 0, 1, 0,
3515 doc: /* Return the number of color cells of the X display TERMINAL.
3516 The optional argument TERMINAL specifies which display to ask about.
3517 TERMINAL should be a terminal object, a frame or a display name (a string).
3518 If omitted or nil, that stands for the selected frame's display. */)
3519 (Lisp_Object terminal)
3521 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3523 int nr_planes = DisplayPlanes (dpyinfo->display,
3524 XScreenNumberOfScreen (dpyinfo->screen));
3526 /* Truncate nr_planes to 24 to avoid integer overflow.
3527 Some displays says 32, but only 24 bits are actually significant.
3528 There are only very few and rare video cards that have more than
3529 24 significant bits. Also 24 bits is more than 16 million colors,
3530 it "should be enough for everyone". */
3531 if (nr_planes > 24) nr_planes = 24;
3533 return make_number (1 << nr_planes);
3536 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
3537 Sx_server_max_request_size,
3538 0, 1, 0,
3539 doc: /* Return the maximum request size of the X server of display TERMINAL.
3540 The optional argument TERMINAL specifies which display to ask about.
3541 TERMINAL should be a terminal object, a frame or a display name (a string).
3542 If omitted or nil, that stands for the selected frame's display. */)
3543 (Lisp_Object terminal)
3545 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3547 return make_number (MAXREQUEST (dpyinfo->display));
3550 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
3551 doc: /* Return the "vendor ID" string of the X server of display TERMINAL.
3552 \(Labeling every distributor as a "vendor" embodies the false assumption
3553 that operating systems cannot be developed and distributed noncommercially.)
3554 The optional argument TERMINAL specifies which display to ask about.
3555 TERMINAL should be a terminal object, a frame or a display name (a string).
3556 If omitted or nil, that stands for the selected frame's display. */)
3557 (Lisp_Object terminal)
3559 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3560 const char *vendor = ServerVendor (dpyinfo->display);
3562 if (! vendor) vendor = "";
3563 return build_string (vendor);
3566 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
3567 doc: /* Return the version numbers of the X server of display TERMINAL.
3568 The value is a list of three integers: the major and minor
3569 version numbers of the X Protocol in use, and the distributor-specific release
3570 number. See also the function `x-server-vendor'.
3572 The optional argument TERMINAL specifies which display to ask about.
3573 TERMINAL should be a terminal object, a frame or a display name (a string).
3574 If omitted or nil, that stands for the selected frame's display. */)
3575 (Lisp_Object terminal)
3577 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3578 Display *dpy = dpyinfo->display;
3580 return list3i (ProtocolVersion (dpy), ProtocolRevision (dpy),
3581 VendorRelease (dpy));
3584 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
3585 doc: /* Return the number of screens on the X server of display TERMINAL.
3586 The optional argument TERMINAL specifies which display to ask about.
3587 TERMINAL should be a terminal object, a frame or a display name (a string).
3588 If omitted or nil, that stands for the selected frame's display. */)
3589 (Lisp_Object terminal)
3591 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3593 return make_number (ScreenCount (dpyinfo->display));
3596 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
3597 doc: /* Return the height in millimeters of the X display TERMINAL.
3598 The optional argument TERMINAL specifies which display to ask about.
3599 TERMINAL should be a terminal object, a frame or a display name (a string).
3600 If omitted or nil, that stands for the selected frame's display.
3602 On \"multi-monitor\" setups this refers to the height in millimeters for
3603 all physical monitors associated with TERMINAL. To get information
3604 for each physical monitor, use `display-monitor-attributes-list'. */)
3605 (Lisp_Object terminal)
3607 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3609 return make_number (HeightMMOfScreen (dpyinfo->screen));
3612 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
3613 doc: /* Return the width in millimeters of the X display TERMINAL.
3614 The optional argument TERMINAL specifies which display to ask about.
3615 TERMINAL should be a terminal object, a frame or a display name (a string).
3616 If omitted or nil, that stands for the selected frame's display.
3618 On \"multi-monitor\" setups this refers to the width in millimeters for
3619 all physical monitors associated with TERMINAL. To get information
3620 for each physical monitor, use `display-monitor-attributes-list'. */)
3621 (Lisp_Object terminal)
3623 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3625 return make_number (WidthMMOfScreen (dpyinfo->screen));
3628 DEFUN ("x-display-backing-store", Fx_display_backing_store,
3629 Sx_display_backing_store, 0, 1, 0,
3630 doc: /* Return an indication of whether X display TERMINAL does backing store.
3631 The value may be `always', `when-mapped', or `not-useful'.
3632 The optional argument TERMINAL specifies which display to ask about.
3633 TERMINAL should be a terminal object, a frame or a display name (a string).
3634 If omitted or nil, that stands for the selected frame's display. */)
3635 (Lisp_Object terminal)
3637 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3638 Lisp_Object result;
3640 switch (DoesBackingStore (dpyinfo->screen))
3642 case Always:
3643 result = intern ("always");
3644 break;
3646 case WhenMapped:
3647 result = intern ("when-mapped");
3648 break;
3650 case NotUseful:
3651 result = intern ("not-useful");
3652 break;
3654 default:
3655 error ("Strange value for BackingStore parameter of screen");
3658 return result;
3661 DEFUN ("x-display-visual-class", Fx_display_visual_class,
3662 Sx_display_visual_class, 0, 1, 0,
3663 doc: /* Return the visual class of the X display TERMINAL.
3664 The value is one of the symbols `static-gray', `gray-scale',
3665 `static-color', `pseudo-color', `true-color', or `direct-color'.
3667 The optional argument TERMINAL specifies which display to ask about.
3668 TERMINAL should a terminal object, a frame or a display name (a string).
3669 If omitted or nil, that stands for the selected frame's display. */)
3670 (Lisp_Object terminal)
3672 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3673 Lisp_Object result;
3675 switch (dpyinfo->visual->class)
3677 case StaticGray:
3678 result = intern ("static-gray");
3679 break;
3680 case GrayScale:
3681 result = intern ("gray-scale");
3682 break;
3683 case StaticColor:
3684 result = intern ("static-color");
3685 break;
3686 case PseudoColor:
3687 result = intern ("pseudo-color");
3688 break;
3689 case TrueColor:
3690 result = intern ("true-color");
3691 break;
3692 case DirectColor:
3693 result = intern ("direct-color");
3694 break;
3695 default:
3696 error ("Display has an unknown visual class");
3699 return result;
3702 DEFUN ("x-display-save-under", Fx_display_save_under,
3703 Sx_display_save_under, 0, 1, 0,
3704 doc: /* Return t if the X display TERMINAL supports the save-under feature.
3705 The optional argument TERMINAL specifies which display to ask about.
3706 TERMINAL should be a terminal object, a frame or a display name (a string).
3707 If omitted or nil, that stands for the selected frame's display. */)
3708 (Lisp_Object terminal)
3710 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3712 if (DoesSaveUnders (dpyinfo->screen) == True)
3713 return Qt;
3714 else
3715 return Qnil;
3718 /* Store the geometry of the workarea on display DPYINFO into *RECT.
3719 Return false if and only if the workarea information cannot be
3720 obtained via the _NET_WORKAREA root window property. */
3722 #if ! GTK_CHECK_VERSION (3, 4, 0)
3723 static bool
3724 x_get_net_workarea (struct x_display_info *dpyinfo, XRectangle *rect)
3726 Display *dpy = dpyinfo->display;
3727 long offset, max_len;
3728 Atom target_type, actual_type;
3729 unsigned long actual_size, bytes_remaining;
3730 int rc, actual_format;
3731 unsigned char *tmp_data = NULL;
3732 bool result = false;
3734 x_catch_errors (dpy);
3735 offset = 0;
3736 max_len = 1;
3737 target_type = XA_CARDINAL;
3738 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3739 dpyinfo->Xatom_net_current_desktop,
3740 offset, max_len, False, target_type,
3741 &actual_type, &actual_format, &actual_size,
3742 &bytes_remaining, &tmp_data);
3743 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3744 && actual_format == 32 && actual_size == max_len)
3746 long current_desktop = ((long *) tmp_data)[0];
3748 XFree (tmp_data);
3749 tmp_data = NULL;
3751 offset = 4 * current_desktop;
3752 max_len = 4;
3753 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3754 dpyinfo->Xatom_net_workarea,
3755 offset, max_len, False, target_type,
3756 &actual_type, &actual_format, &actual_size,
3757 &bytes_remaining, &tmp_data);
3758 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3759 && actual_format == 32 && actual_size == max_len)
3761 long *values = (long *) tmp_data;
3763 rect->x = values[0];
3764 rect->y = values[1];
3765 rect->width = values[2];
3766 rect->height = values[3];
3768 XFree (tmp_data);
3769 tmp_data = NULL;
3771 result = true;
3774 if (tmp_data)
3775 XFree (tmp_data);
3776 x_uncatch_errors ();
3778 return result;
3780 #endif
3782 #ifndef USE_GTK
3784 /* Return monitor number where F is "most" or closest to. */
3785 static int
3786 x_get_monitor_for_frame (struct frame *f,
3787 struct MonitorInfo *monitors,
3788 int n_monitors)
3790 XRectangle frect;
3791 int area = 0, dist = -1;
3792 int best_area = -1, best_dist = -1;
3793 int i;
3795 if (n_monitors == 1) return 0;
3796 frect.x = f->left_pos;
3797 frect.y = f->top_pos;
3798 frect.width = FRAME_PIXEL_WIDTH (f);
3799 frect.height = FRAME_PIXEL_HEIGHT (f);
3801 for (i = 0; i < n_monitors; ++i)
3803 struct MonitorInfo *mi = &monitors[i];
3804 XRectangle res;
3805 int a = 0;
3807 if (mi->geom.width == 0) continue;
3809 if (x_intersect_rectangles (&mi->geom, &frect, &res))
3811 a = res.width * res.height;
3812 if (a > area)
3814 area = a;
3815 best_area = i;
3819 if (a == 0 && area == 0)
3821 int dx, dy, d;
3822 if (frect.x + frect.width < mi->geom.x)
3823 dx = mi->geom.x - frect.x + frect.width;
3824 else if (frect.x > mi->geom.x + mi->geom.width)
3825 dx = frect.x - mi->geom.x + mi->geom.width;
3826 else
3827 dx = 0;
3828 if (frect.y + frect.height < mi->geom.y)
3829 dy = mi->geom.y - frect.y + frect.height;
3830 else if (frect.y > mi->geom.y + mi->geom.height)
3831 dy = frect.y - mi->geom.y + mi->geom.height;
3832 else
3833 dy = 0;
3835 d = dx*dx + dy*dy;
3836 if (dist == -1 || dist > d)
3838 dist = d;
3839 best_dist = i;
3844 return best_area != -1 ? best_area : (best_dist != -1 ? best_dist : 0);
3847 static Lisp_Object
3848 x_make_monitor_attribute_list (struct MonitorInfo *monitors,
3849 int n_monitors,
3850 int primary_monitor,
3851 struct x_display_info *dpyinfo,
3852 const char *source)
3854 Lisp_Object monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
3855 Lisp_Object frame, rest;
3857 FOR_EACH_FRAME (rest, frame)
3859 struct frame *f = XFRAME (frame);
3861 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo
3862 && !EQ (frame, tip_frame))
3864 int i = x_get_monitor_for_frame (f, monitors, n_monitors);
3865 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
3869 return make_monitor_attribute_list (monitors, n_monitors, primary_monitor,
3870 monitor_frames, source);
3873 static Lisp_Object
3874 x_get_monitor_attributes_fallback (struct x_display_info *dpyinfo)
3876 struct MonitorInfo monitor;
3877 XRectangle workarea_r;
3879 /* Fallback: treat (possibly) multiple physical monitors as if they
3880 formed a single monitor as a whole. This should provide a
3881 consistent result at least on single monitor environments. */
3882 monitor.geom.x = monitor.geom.y = 0;
3883 monitor.geom.width = x_display_pixel_width (dpyinfo);
3884 monitor.geom.height = x_display_pixel_height (dpyinfo);
3885 monitor.mm_width = WidthMMOfScreen (dpyinfo->screen);
3886 monitor.mm_height = HeightMMOfScreen (dpyinfo->screen);
3887 monitor.name = xstrdup ("combined screen");
3889 if (x_get_net_workarea (dpyinfo, &workarea_r))
3890 monitor.work = workarea_r;
3891 else
3892 monitor.work = monitor.geom;
3893 return x_make_monitor_attribute_list (&monitor, 1, 0, dpyinfo, "fallback");
3897 #ifdef HAVE_XINERAMA
3898 static Lisp_Object
3899 x_get_monitor_attributes_xinerama (struct x_display_info *dpyinfo)
3901 int n_monitors, i;
3902 Lisp_Object attributes_list = Qnil;
3903 Display *dpy = dpyinfo->display;
3904 XineramaScreenInfo *info = XineramaQueryScreens (dpy, &n_monitors);
3905 struct MonitorInfo *monitors;
3906 double mm_width_per_pixel, mm_height_per_pixel;
3908 if (! info || n_monitors == 0)
3910 if (info)
3911 XFree (info);
3912 return attributes_list;
3915 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
3916 / x_display_pixel_width (dpyinfo));
3917 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
3918 / x_display_pixel_height (dpyinfo));
3919 monitors = xzalloc (n_monitors * sizeof *monitors);
3920 for (i = 0; i < n_monitors; ++i)
3922 struct MonitorInfo *mi = &monitors[i];
3923 XRectangle workarea_r;
3925 mi->geom.x = info[i].x_org;
3926 mi->geom.y = info[i].y_org;
3927 mi->geom.width = info[i].width;
3928 mi->geom.height = info[i].height;
3929 mi->mm_width = mi->geom.width * mm_width_per_pixel + 0.5;
3930 mi->mm_height = mi->geom.height * mm_height_per_pixel + 0.5;
3931 mi->name = 0;
3933 /* Xinerama usually have primary monitor first, just use that. */
3934 if (i == 0 && x_get_net_workarea (dpyinfo, &workarea_r))
3936 mi->work = workarea_r;
3937 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
3938 mi->work = mi->geom;
3940 else
3941 mi->work = mi->geom;
3943 XFree (info);
3945 attributes_list = x_make_monitor_attribute_list (monitors,
3946 n_monitors,
3948 dpyinfo,
3949 "Xinerama");
3950 free_monitors (monitors, n_monitors);
3951 return attributes_list;
3953 #endif /* HAVE_XINERAMA */
3956 #ifdef HAVE_XRANDR
3957 static Lisp_Object
3958 x_get_monitor_attributes_xrandr (struct x_display_info *dpyinfo)
3960 Lisp_Object attributes_list = Qnil;
3961 XRRScreenResources *resources;
3962 Display *dpy = dpyinfo->display;
3963 int i, n_monitors, primary = -1;
3964 RROutput pxid = None;
3965 struct MonitorInfo *monitors;
3967 #ifdef HAVE_XRRGETSCREENRESOURCESCURRENT
3968 resources = XRRGetScreenResourcesCurrent (dpy, dpyinfo->root_window);
3969 #else
3970 resources = XRRGetScreenResources (dpy, dpyinfo->root_window);
3971 #endif
3972 if (! resources || resources->noutput == 0)
3974 if (resources)
3975 XRRFreeScreenResources (resources);
3976 return Qnil;
3978 n_monitors = resources->noutput;
3979 monitors = xzalloc (n_monitors * sizeof *monitors);
3981 #ifdef HAVE_XRRGETOUTPUTPRIMARY
3982 pxid = XRRGetOutputPrimary (dpy, dpyinfo->root_window);
3983 #endif
3985 for (i = 0; i < n_monitors; ++i)
3987 XRROutputInfo *info = XRRGetOutputInfo (dpy, resources,
3988 resources->outputs[i]);
3989 Connection conn = info ? info->connection : RR_Disconnected;
3990 RRCrtc id = info ? info->crtc : None;
3992 if (strcmp (info->name, "default") == 0)
3994 /* Non XRandr 1.2 driver, does not give useful data. */
3995 XRRFreeOutputInfo (info);
3996 XRRFreeScreenResources (resources);
3997 free_monitors (monitors, n_monitors);
3998 return Qnil;
4001 if (conn != RR_Disconnected && id != None)
4003 XRRCrtcInfo *crtc = XRRGetCrtcInfo (dpy, resources, id);
4004 struct MonitorInfo *mi = &monitors[i];
4005 XRectangle workarea_r;
4007 if (! crtc)
4009 XRRFreeOutputInfo (info);
4010 continue;
4013 mi->geom.x = crtc->x;
4014 mi->geom.y = crtc->y;
4015 mi->geom.width = crtc->width;
4016 mi->geom.height = crtc->height;
4017 mi->mm_width = info->mm_width;
4018 mi->mm_height = info->mm_height;
4019 mi->name = xstrdup (info->name);
4021 if (pxid != None && pxid == resources->outputs[i])
4022 primary = i;
4023 else if (primary == -1 && strcmp (info->name, "LVDS") == 0)
4024 primary = i;
4026 if (i == primary && x_get_net_workarea (dpyinfo, &workarea_r))
4028 mi->work= workarea_r;
4029 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
4030 mi->work = mi->geom;
4032 else
4033 mi->work = mi->geom;
4035 XRRFreeCrtcInfo (crtc);
4037 XRRFreeOutputInfo (info);
4039 XRRFreeScreenResources (resources);
4041 attributes_list = x_make_monitor_attribute_list (monitors,
4042 n_monitors,
4043 primary,
4044 dpyinfo,
4045 "XRandr");
4046 free_monitors (monitors, n_monitors);
4047 return attributes_list;
4049 #endif /* HAVE_XRANDR */
4051 static Lisp_Object
4052 x_get_monitor_attributes (struct x_display_info *dpyinfo)
4054 Lisp_Object attributes_list = Qnil;
4055 Display *dpy = dpyinfo->display;
4057 (void) dpy; /* Suppress unused variable warning. */
4059 #ifdef HAVE_XRANDR
4060 int xrr_event_base, xrr_error_base;
4061 bool xrr_ok = false;
4062 xrr_ok = XRRQueryExtension (dpy, &xrr_event_base, &xrr_error_base);
4063 if (xrr_ok)
4065 int xrr_major, xrr_minor;
4066 XRRQueryVersion (dpy, &xrr_major, &xrr_minor);
4067 xrr_ok = (xrr_major == 1 && xrr_minor >= 2) || xrr_major > 1;
4070 if (xrr_ok)
4071 attributes_list = x_get_monitor_attributes_xrandr (dpyinfo);
4072 #endif /* HAVE_XRANDR */
4074 #ifdef HAVE_XINERAMA
4075 if (NILP (attributes_list))
4077 int xin_event_base, xin_error_base;
4078 bool xin_ok = false;
4079 xin_ok = XineramaQueryExtension (dpy, &xin_event_base, &xin_error_base);
4080 if (xin_ok && XineramaIsActive (dpy))
4081 attributes_list = x_get_monitor_attributes_xinerama (dpyinfo);
4083 #endif /* HAVE_XINERAMA */
4085 if (NILP (attributes_list))
4086 attributes_list = x_get_monitor_attributes_fallback (dpyinfo);
4088 return attributes_list;
4091 #endif /* !USE_GTK */
4093 DEFUN ("x-display-monitor-attributes-list", Fx_display_monitor_attributes_list,
4094 Sx_display_monitor_attributes_list,
4095 0, 1, 0,
4096 doc: /* Return a list of physical monitor attributes on the X display TERMINAL.
4098 The optional argument TERMINAL specifies which display to ask about.
4099 TERMINAL should be a terminal object, a frame or a display name (a string).
4100 If omitted or nil, that stands for the selected frame's display.
4102 In addition to the standard attribute keys listed in
4103 `display-monitor-attributes-list', the following keys are contained in
4104 the attributes:
4106 source -- String describing the source from which multi-monitor
4107 information is obtained, one of \"Gdk\", \"XRandr\",
4108 \"Xinerama\", or \"fallback\"
4110 Internal use only, use `display-monitor-attributes-list' instead. */)
4111 (Lisp_Object terminal)
4113 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4114 Lisp_Object attributes_list = Qnil;
4116 #ifdef USE_GTK
4117 double mm_width_per_pixel, mm_height_per_pixel;
4118 GdkDisplay *gdpy;
4119 GdkScreen *gscreen;
4120 gint primary_monitor = 0, n_monitors, i;
4121 Lisp_Object monitor_frames, rest, frame;
4122 static const char *source = "Gdk";
4123 struct MonitorInfo *monitors;
4125 block_input ();
4126 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
4127 / x_display_pixel_width (dpyinfo));
4128 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
4129 / x_display_pixel_height (dpyinfo));
4130 gdpy = gdk_x11_lookup_xdisplay (dpyinfo->display);
4131 gscreen = gdk_display_get_default_screen (gdpy);
4132 #if GTK_CHECK_VERSION (2, 20, 0)
4133 primary_monitor = gdk_screen_get_primary_monitor (gscreen);
4134 #endif
4135 n_monitors = gdk_screen_get_n_monitors (gscreen);
4136 monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
4137 monitors = xzalloc (n_monitors * sizeof *monitors);
4139 FOR_EACH_FRAME (rest, frame)
4141 struct frame *f = XFRAME (frame);
4143 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo
4144 && !EQ (frame, tip_frame))
4146 GdkWindow *gwin = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
4148 i = gdk_screen_get_monitor_at_window (gscreen, gwin);
4149 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
4153 for (i = 0; i < n_monitors; ++i)
4155 gint width_mm = -1, height_mm = -1;
4156 GdkRectangle rec, work;
4157 struct MonitorInfo *mi = &monitors[i];
4159 gdk_screen_get_monitor_geometry (gscreen, i, &rec);
4161 #if GTK_CHECK_VERSION (2, 14, 0)
4162 width_mm = gdk_screen_get_monitor_width_mm (gscreen, i);
4163 height_mm = gdk_screen_get_monitor_height_mm (gscreen, i);
4164 #endif
4165 if (width_mm < 0)
4166 width_mm = rec.width * mm_width_per_pixel + 0.5;
4167 if (height_mm < 0)
4168 height_mm = rec.height * mm_height_per_pixel + 0.5;
4170 #if GTK_CHECK_VERSION (3, 4, 0)
4171 gdk_screen_get_monitor_workarea (gscreen, i, &work);
4172 #else
4173 /* Emulate the behavior of GTK+ 3.4. */
4175 XRectangle workarea_r;
4177 if (i == primary_monitor && x_get_net_workarea (dpyinfo, &workarea_r))
4179 work.x = workarea_r.x;
4180 work.y = workarea_r.y;
4181 work.width = workarea_r.width;
4182 work.height = workarea_r.height;
4183 if (! gdk_rectangle_intersect (&rec, &work, &work))
4184 work = rec;
4186 else
4187 work = rec;
4189 #endif
4192 mi->geom.x = rec.x;
4193 mi->geom.y = rec.y;
4194 mi->geom.width = rec.width;
4195 mi->geom.height = rec.height;
4196 mi->work.x = work.x;
4197 mi->work.y = work.y;
4198 mi->work.width = work.width;
4199 mi->work.height = work.height;
4200 mi->mm_width = width_mm;
4201 mi->mm_height = height_mm;
4203 #if GTK_CHECK_VERSION (2, 14, 0)
4204 mi->name = gdk_screen_get_monitor_plug_name (gscreen, i);
4205 #endif
4208 attributes_list = make_monitor_attribute_list (monitors,
4209 n_monitors,
4210 primary_monitor,
4211 monitor_frames,
4212 source);
4213 unblock_input ();
4214 #else /* not USE_GTK */
4216 block_input ();
4217 attributes_list = x_get_monitor_attributes (dpyinfo);
4218 unblock_input ();
4220 #endif /* not USE_GTK */
4222 return attributes_list;
4225 DEFUN ("x-frame-geometry", Fx_frame_geometry, Sx_frame_geometry, 0, 1, 0,
4226 doc: /* Return geometric attributes of frame FRAME.
4228 FRAME must be a live frame and defaults to the selected one.
4230 The return value is an association list containing the following
4231 elements (all size values are in pixels).
4233 - `frame-outer-size' is a cons of the outer width and height of FRAME.
4234 The outer size include the title bar and the external borders as well
4235 as any menu and/or tool bar of frame.
4237 - `border' is a cons of the horizontal and vertical width of FRAME's
4238 external borders.
4240 - `title-bar-height' is the height of the title bar of FRAME.
4242 - `menu-bar-external' if `t' means the menu bar is external (not
4243 included in the inner edges of FRAME).
4245 - `menu-bar-size' is a cons of the width and height of the menu bar of
4246 FRAME.
4248 - `tool-bar-external' if `t' means the tool bar is external (not
4249 included in the inner edges of FRAME).
4251 - `tool-bar-side' tells tells on which side the tool bar on FRAME is and
4252 can be one of `left', `top', `right' or `bottom'.
4254 - `tool-bar-size' is a cons of the width and height of the tool bar of
4255 FRAME.
4257 - `frame-inner-size' is a cons of the inner width and height of FRAME.
4258 This excludes FRAME's title bar and external border as well as any
4259 external menu and/or tool bar. */)
4260 (Lisp_Object frame)
4262 struct frame *f = decode_live_frame (frame);
4263 int inner_width = FRAME_PIXEL_WIDTH (f);
4264 int inner_height = FRAME_PIXEL_HEIGHT (f);
4265 int outer_width, outer_height, border, title;
4266 Lisp_Object fullscreen = Fframe_parameter (frame, Qfullscreen);
4267 int menu_bar_height, menu_bar_width, tool_bar_height, tool_bar_width;
4269 border = FRAME_OUTER_TO_INNER_DIFF_X (f);
4270 title = FRAME_X_OUTPUT (f)->y_pixels_outer_diff - border;
4272 outer_width = FRAME_PIXEL_WIDTH (f) + 2 * border;
4273 outer_height = (FRAME_PIXEL_HEIGHT (f)
4274 + FRAME_OUTER_TO_INNER_DIFF_Y (f)
4275 + FRAME_OUTER_TO_INNER_DIFF_X (f));
4277 #if defined (USE_GTK)
4279 bool tool_bar_left_right = (EQ (FRAME_TOOL_BAR_POSITION (f), Qleft)
4280 || EQ (FRAME_TOOL_BAR_POSITION (f), Qright));
4282 tool_bar_width = (tool_bar_left_right
4283 ? FRAME_TOOLBAR_WIDTH (f)
4284 : FRAME_PIXEL_WIDTH (f));
4285 tool_bar_height = (tool_bar_left_right
4286 ? FRAME_PIXEL_HEIGHT (f)
4287 : FRAME_TOOLBAR_HEIGHT (f));
4288 if (tool_bar_left_right)
4289 /* For some reason FRAME_OUTER_TO_INNER_DIFF_X does not count the
4290 width of a tool bar. */
4291 outer_width += FRAME_TOOLBAR_WIDTH (f);
4293 #else
4294 tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f);
4295 tool_bar_width = ((tool_bar_height > 0)
4296 ? outer_width - 2 * FRAME_INTERNAL_BORDER_WIDTH (f)
4297 : 0);
4298 #endif
4300 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
4301 menu_bar_height = FRAME_MENUBAR_HEIGHT (f);
4302 #else
4303 menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
4304 #endif
4306 menu_bar_width = ((menu_bar_height > 0)
4307 ? outer_width - 2 * border
4308 : 0);
4310 if (!FRAME_EXTERNAL_MENU_BAR (f))
4311 inner_height -= menu_bar_height;
4312 if (!FRAME_EXTERNAL_TOOL_BAR (f))
4313 inner_height -= tool_bar_height;
4315 return
4316 listn (CONSTYPE_PURE, 10,
4317 Fcons (Qframe_position,
4318 Fcons (make_number (f->left_pos), make_number (f->top_pos))),
4319 Fcons (Qframe_outer_size,
4320 Fcons (make_number (outer_width), make_number (outer_height))),
4321 Fcons (Qexternal_border_size,
4322 ((EQ (fullscreen, Qfullboth) || EQ (fullscreen, Qfullscreen))
4323 ? Fcons (make_number (0), make_number (0))
4324 : Fcons (make_number (border), make_number (border)))),
4325 Fcons (Qtitle_height,
4326 ((EQ (fullscreen, Qfullboth) || EQ (fullscreen, Qfullscreen))
4327 ? make_number (0)
4328 : make_number (title))),
4329 Fcons (Qmenu_bar_external, FRAME_EXTERNAL_MENU_BAR (f) ? Qt : Qnil),
4330 Fcons (Qmenu_bar_size,
4331 Fcons (make_number (menu_bar_width),
4332 make_number (menu_bar_height))),
4333 Fcons (Qtool_bar_external, FRAME_EXTERNAL_TOOL_BAR (f) ? Qt : Qnil),
4334 Fcons (Qtool_bar_position, FRAME_TOOL_BAR_POSITION (f)),
4335 Fcons (Qtool_bar_size,
4336 Fcons (make_number (tool_bar_width),
4337 make_number (tool_bar_height))),
4338 Fcons (Qframe_inner_size,
4339 Fcons (make_number (inner_width),
4340 make_number (inner_height))));
4343 /************************************************************************
4344 X Displays
4345 ************************************************************************/
4348 /* Mapping visual names to visuals. */
4350 static struct visual_class
4352 const char *name;
4353 int class;
4355 visual_classes[] =
4357 {"StaticGray", StaticGray},
4358 {"GrayScale", GrayScale},
4359 {"StaticColor", StaticColor},
4360 {"PseudoColor", PseudoColor},
4361 {"TrueColor", TrueColor},
4362 {"DirectColor", DirectColor},
4363 {NULL, 0}
4367 #ifndef HAVE_XSCREENNUMBEROFSCREEN
4369 /* Value is the screen number of screen SCR. This is a substitute for
4370 the X function with the same name when that doesn't exist. */
4373 XScreenNumberOfScreen (scr)
4374 register Screen *scr;
4376 Display *dpy = scr->display;
4377 int i;
4379 for (i = 0; i < dpy->nscreens; ++i)
4380 if (scr == dpy->screens + i)
4381 break;
4383 return i;
4386 #endif /* not HAVE_XSCREENNUMBEROFSCREEN */
4389 /* Select the visual that should be used on display DPYINFO. Set
4390 members of DPYINFO appropriately. Called from x_term_init. */
4392 void
4393 select_visual (struct x_display_info *dpyinfo)
4395 Display *dpy = dpyinfo->display;
4396 Screen *screen = dpyinfo->screen;
4398 /* See if a visual is specified. */
4399 AUTO_STRING (visualClass, "visualClass");
4400 AUTO_STRING (VisualClass, "VisualClass");
4401 Lisp_Object value = display_x_get_resource (dpyinfo, visualClass,
4402 VisualClass, Qnil, Qnil);
4404 if (STRINGP (value))
4406 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
4407 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
4408 depth, a decimal number. NAME is compared with case ignored. */
4409 char *s = alloca (SBYTES (value) + 1);
4410 char *dash;
4411 int i, class = -1;
4412 XVisualInfo vinfo;
4414 lispstpcpy (s, value);
4415 dash = strchr (s, '-');
4416 if (dash)
4418 dpyinfo->n_planes = atoi (dash + 1);
4419 *dash = '\0';
4421 else
4422 /* We won't find a matching visual with depth 0, so that
4423 an error will be printed below. */
4424 dpyinfo->n_planes = 0;
4426 /* Determine the visual class. */
4427 for (i = 0; visual_classes[i].name; ++i)
4428 if (xstrcasecmp (s, visual_classes[i].name) == 0)
4430 class = visual_classes[i].class;
4431 break;
4434 /* Look up a matching visual for the specified class. */
4435 if (class == -1
4436 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
4437 dpyinfo->n_planes, class, &vinfo))
4438 fatal ("Invalid visual specification `%s'", SDATA (value));
4440 dpyinfo->visual = vinfo.visual;
4442 else
4444 int n_visuals;
4445 XVisualInfo *vinfo, vinfo_template;
4447 dpyinfo->visual = DefaultVisualOfScreen (screen);
4449 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
4450 vinfo_template.screen = XScreenNumberOfScreen (screen);
4451 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
4452 &vinfo_template, &n_visuals);
4453 if (n_visuals <= 0)
4454 fatal ("Can't get proper X visual info");
4456 dpyinfo->n_planes = vinfo->depth;
4457 XFree (vinfo);
4462 /* Return the X display structure for the display named NAME.
4463 Open a new connection if necessary. */
4465 static struct x_display_info *
4466 x_display_info_for_name (Lisp_Object name)
4468 struct x_display_info *dpyinfo;
4470 CHECK_STRING (name);
4472 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
4473 if (!NILP (Fstring_equal (XCAR (dpyinfo->name_list_element), name)))
4474 return dpyinfo;
4476 /* Use this general default value to start with. */
4477 Vx_resource_name = Vinvocation_name;
4479 validate_x_resource_name ();
4481 dpyinfo = x_term_init (name, 0, SSDATA (Vx_resource_name));
4483 if (dpyinfo == 0)
4484 error ("Cannot connect to X server %s", SDATA (name));
4486 XSETFASTINT (Vwindow_system_version, 11);
4488 return dpyinfo;
4492 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
4493 1, 3, 0,
4494 doc: /* Open a connection to a display server.
4495 DISPLAY is the name of the display to connect to.
4496 Optional second arg XRM-STRING is a string of resources in xrdb format.
4497 If the optional third arg MUST-SUCCEED is non-nil,
4498 terminate Emacs if we can't open the connection.
4499 \(In the Nextstep version, the last two arguments are currently ignored.) */)
4500 (Lisp_Object display, Lisp_Object xrm_string, Lisp_Object must_succeed)
4502 char *xrm_option;
4503 struct x_display_info *dpyinfo;
4505 CHECK_STRING (display);
4506 if (! NILP (xrm_string))
4507 CHECK_STRING (xrm_string);
4509 xrm_option = NILP (xrm_string) ? 0 : SSDATA (xrm_string);
4511 validate_x_resource_name ();
4513 /* This is what opens the connection and sets x_current_display.
4514 This also initializes many symbols, such as those used for input. */
4515 dpyinfo = x_term_init (display, xrm_option,
4516 SSDATA (Vx_resource_name));
4518 if (dpyinfo == 0)
4520 if (!NILP (must_succeed))
4521 fatal ("Cannot connect to X server %s.\n\
4522 Check the DISPLAY environment variable or use `-d'.\n\
4523 Also use the `xauth' program to verify that you have the proper\n\
4524 authorization information needed to connect the X server.\n\
4525 An insecure way to solve the problem may be to use `xhost'.\n",
4526 SDATA (display));
4527 else
4528 error ("Cannot connect to X server %s", SDATA (display));
4531 XSETFASTINT (Vwindow_system_version, 11);
4532 return Qnil;
4535 DEFUN ("x-close-connection", Fx_close_connection,
4536 Sx_close_connection, 1, 1, 0,
4537 doc: /* Close the connection to TERMINAL's X server.
4538 For TERMINAL, specify a terminal object, a frame or a display name (a
4539 string). If TERMINAL is nil, that stands for the selected frame's
4540 terminal. */)
4541 (Lisp_Object terminal)
4543 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4545 if (dpyinfo->reference_count > 0)
4546 error ("Display still has frames on it");
4548 x_delete_terminal (dpyinfo->terminal);
4550 return Qnil;
4553 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
4554 doc: /* Return the list of display names that Emacs has connections to. */)
4555 (void)
4557 Lisp_Object result = Qnil;
4558 struct x_display_info *xdi;
4560 for (xdi = x_display_list; xdi; xdi = xdi->next)
4561 result = Fcons (XCAR (xdi->name_list_element), result);
4563 return result;
4566 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
4567 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
4568 This function only has an effect on X Windows. With MS Windows, it is
4569 defined but does nothing.
4571 If ON is nil, allow buffering of requests.
4572 Turning on synchronization prohibits the Xlib routines from buffering
4573 requests and seriously degrades performance, but makes debugging much
4574 easier.
4575 The optional second argument TERMINAL specifies which display to act on.
4576 TERMINAL should be a terminal object, a frame or a display name (a string).
4577 If TERMINAL is omitted or nil, that stands for the selected frame's display. */)
4578 (Lisp_Object on, Lisp_Object terminal)
4580 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4582 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
4584 return Qnil;
4587 /* Wait for responses to all X commands issued so far for frame F. */
4589 void
4590 x_sync (struct frame *f)
4592 block_input ();
4593 XSync (FRAME_X_DISPLAY (f), False);
4594 unblock_input ();
4598 /***********************************************************************
4599 Window properties
4600 ***********************************************************************/
4602 DEFUN ("x-change-window-property", Fx_change_window_property,
4603 Sx_change_window_property, 2, 6, 0,
4604 doc: /* Change window property PROP to VALUE on the X window of FRAME.
4605 PROP must be a string. VALUE may be a string or a list of conses,
4606 numbers and/or strings. If an element in the list is a string, it is
4607 converted to an atom and the value of the atom is used. If an element
4608 is a cons, it is converted to a 32 bit number where the car is the 16
4609 top bits and the cdr is the lower 16 bits.
4611 FRAME nil or omitted means use the selected frame.
4612 If TYPE is given and non-nil, it is the name of the type of VALUE.
4613 If TYPE is not given or nil, the type is STRING.
4614 FORMAT gives the size in bits of each element if VALUE is a list.
4615 It must be one of 8, 16 or 32.
4616 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
4617 If OUTER-P is non-nil, the property is changed for the outer X window of
4618 FRAME. Default is to change on the edit X window. */)
4619 (Lisp_Object prop, Lisp_Object value, Lisp_Object frame,
4620 Lisp_Object type, Lisp_Object format, Lisp_Object outer_p)
4622 struct frame *f = decode_window_system_frame (frame);
4623 Atom prop_atom;
4624 Atom target_type = XA_STRING;
4625 int element_format = 8;
4626 unsigned char *data;
4627 int nelements;
4628 Window w;
4630 CHECK_STRING (prop);
4632 if (! NILP (format))
4634 CHECK_NUMBER (format);
4636 if (XINT (format) != 8 && XINT (format) != 16
4637 && XINT (format) != 32)
4638 error ("FORMAT must be one of 8, 16 or 32");
4639 element_format = XINT (format);
4642 if (CONSP (value))
4644 ptrdiff_t elsize;
4646 nelements = x_check_property_data (value);
4647 if (nelements == -1)
4648 error ("Bad data in VALUE, must be number, string or cons");
4650 /* The man page for XChangeProperty:
4651 "If the specified format is 32, the property data must be a
4652 long array."
4653 This applies even if long is more than 32 bits. The X library
4654 converts to 32 bits before sending to the X server. */
4655 elsize = element_format == 32 ? sizeof (long) : element_format >> 3;
4656 data = xnmalloc (nelements, elsize);
4658 x_fill_property_data (FRAME_X_DISPLAY (f), value, data, element_format);
4660 else
4662 CHECK_STRING (value);
4663 data = SDATA (value);
4664 if (INT_MAX < SBYTES (value))
4665 error ("VALUE too long");
4666 nelements = SBYTES (value);
4669 block_input ();
4670 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4671 if (! NILP (type))
4673 CHECK_STRING (type);
4674 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4677 if (! NILP (outer_p)) w = FRAME_OUTER_WINDOW (f);
4678 else w = FRAME_X_WINDOW (f);
4680 XChangeProperty (FRAME_X_DISPLAY (f), w,
4681 prop_atom, target_type, element_format, PropModeReplace,
4682 data, nelements);
4684 if (CONSP (value)) xfree (data);
4686 /* Make sure the property is set when we return. */
4687 XFlush (FRAME_X_DISPLAY (f));
4688 unblock_input ();
4690 return value;
4694 DEFUN ("x-delete-window-property", Fx_delete_window_property,
4695 Sx_delete_window_property, 1, 2, 0,
4696 doc: /* Remove window property PROP from X window of FRAME.
4697 FRAME nil or omitted means use the selected frame. Value is PROP. */)
4698 (Lisp_Object prop, Lisp_Object frame)
4700 struct frame *f = decode_window_system_frame (frame);
4701 Atom prop_atom;
4703 CHECK_STRING (prop);
4704 block_input ();
4705 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4706 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
4708 /* Make sure the property is removed when we return. */
4709 XFlush (FRAME_X_DISPLAY (f));
4710 unblock_input ();
4712 return prop;
4716 static Lisp_Object
4717 x_window_property_intern (struct frame *f,
4718 Window target_window,
4719 Atom prop_atom,
4720 Atom target_type,
4721 Lisp_Object delete_p,
4722 Lisp_Object vector_ret_p,
4723 bool *found)
4725 unsigned char *tmp_data = NULL;
4726 Lisp_Object prop_value = Qnil;
4727 Atom actual_type;
4728 int actual_format;
4729 unsigned long actual_size, bytes_remaining;
4730 int rc;
4731 struct gcpro gcpro1;
4733 GCPRO1 (prop_value);
4735 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4736 prop_atom, 0, 0, False, target_type,
4737 &actual_type, &actual_format, &actual_size,
4738 &bytes_remaining, &tmp_data);
4740 *found = actual_format != 0;
4742 if (rc == Success && *found)
4744 XFree (tmp_data);
4745 tmp_data = NULL;
4747 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4748 prop_atom, 0, bytes_remaining,
4749 ! NILP (delete_p), target_type,
4750 &actual_type, &actual_format,
4751 &actual_size, &bytes_remaining,
4752 &tmp_data);
4753 if (rc == Success && tmp_data)
4755 /* The man page for XGetWindowProperty says:
4756 "If the returned format is 32, the returned data is represented
4757 as a long array and should be cast to that type to obtain the
4758 elements."
4759 This applies even if long is more than 32 bits, the X library
4760 converts from 32 bit elements received from the X server to long
4761 and passes the long array to us. Thus, for that case memcpy can not
4762 be used. We convert to a 32 bit type here, because so much code
4763 assume on that.
4765 The bytes and offsets passed to XGetWindowProperty refers to the
4766 property and those are indeed in 32 bit quantities if format is
4767 32. */
4769 if (BITS_PER_LONG > 32 && actual_format == 32)
4771 unsigned long i;
4772 int *idata = (int *) tmp_data;
4773 long *ldata = (long *) tmp_data;
4775 for (i = 0; i < actual_size; ++i)
4776 idata[i] = (int) ldata[i];
4779 if (NILP (vector_ret_p))
4780 prop_value = make_string ((char *) tmp_data, actual_size);
4781 else
4782 prop_value = x_property_data_to_lisp (f,
4783 tmp_data,
4784 actual_type,
4785 actual_format,
4786 actual_size);
4789 if (tmp_data) XFree (tmp_data);
4792 UNGCPRO;
4793 return prop_value;
4796 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
4797 1, 6, 0,
4798 doc: /* Value is the value of window property PROP on FRAME.
4799 If FRAME is nil or omitted, use the selected frame.
4801 On X Windows, the following optional arguments are also accepted:
4802 If TYPE is nil or omitted, get the property as a string.
4803 Otherwise TYPE is the name of the atom that denotes the type expected.
4804 If SOURCE is non-nil, get the property on that window instead of from
4805 FRAME. The number 0 denotes the root window.
4806 If DELETE-P is non-nil, delete the property after retrieving it.
4807 If VECTOR-RET-P is non-nil, don't return a string but a vector of values.
4809 On MS Windows, this function accepts but ignores those optional arguments.
4811 Value is nil if FRAME hasn't a property with name PROP or if PROP has
4812 no value of TYPE (always string in the MS Windows case). */)
4813 (Lisp_Object prop, Lisp_Object frame, Lisp_Object type,
4814 Lisp_Object source, Lisp_Object delete_p, Lisp_Object vector_ret_p)
4816 struct frame *f = decode_window_system_frame (frame);
4817 Atom prop_atom;
4818 Lisp_Object prop_value = Qnil;
4819 Atom target_type = XA_STRING;
4820 Window target_window = FRAME_X_WINDOW (f);
4821 struct gcpro gcpro1;
4822 bool found;
4824 GCPRO1 (prop_value);
4825 CHECK_STRING (prop);
4827 if (! NILP (source))
4829 CONS_TO_INTEGER (source, Window, target_window);
4830 if (! target_window)
4831 target_window = FRAME_DISPLAY_INFO (f)->root_window;
4834 block_input ();
4835 if (STRINGP (type))
4837 if (strcmp ("AnyPropertyType", SSDATA (type)) == 0)
4838 target_type = AnyPropertyType;
4839 else
4840 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4843 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4844 prop_value = x_window_property_intern (f,
4845 target_window,
4846 prop_atom,
4847 target_type,
4848 delete_p,
4849 vector_ret_p,
4850 &found);
4851 if (NILP (prop_value)
4852 && ! found
4853 && NILP (source)
4854 && target_window != FRAME_OUTER_WINDOW (f))
4856 prop_value = x_window_property_intern (f,
4857 FRAME_OUTER_WINDOW (f),
4858 prop_atom,
4859 target_type,
4860 delete_p,
4861 vector_ret_p,
4862 &found);
4866 unblock_input ();
4867 UNGCPRO;
4868 return prop_value;
4871 /***********************************************************************
4872 Tool tips
4873 ***********************************************************************/
4875 static Lisp_Object x_create_tip_frame (struct x_display_info *,
4876 Lisp_Object, Lisp_Object);
4877 static void compute_tip_xy (struct frame *, Lisp_Object, Lisp_Object,
4878 Lisp_Object, int, int, int *, int *);
4880 /* The frame of a currently visible tooltip. */
4882 Lisp_Object tip_frame;
4884 /* If non-nil, a timer started that hides the last tooltip when it
4885 fires. */
4887 static Lisp_Object tip_timer;
4888 Window tip_window;
4890 /* If non-nil, a vector of 3 elements containing the last args
4891 with which x-show-tip was called. See there. */
4893 static Lisp_Object last_show_tip_args;
4896 static void
4897 unwind_create_tip_frame (Lisp_Object frame)
4899 Lisp_Object deleted;
4901 deleted = unwind_create_frame (frame);
4902 if (EQ (deleted, Qt))
4904 tip_window = None;
4905 tip_frame = Qnil;
4910 /* Create a frame for a tooltip on the display described by DPYINFO.
4911 PARMS is a list of frame parameters. TEXT is the string to
4912 display in the tip frame. Value is the frame.
4914 Note that functions called here, esp. x_default_parameter can
4915 signal errors, for instance when a specified color name is
4916 undefined. We have to make sure that we're in a consistent state
4917 when this happens. */
4919 static Lisp_Object
4920 x_create_tip_frame (struct x_display_info *dpyinfo,
4921 Lisp_Object parms,
4922 Lisp_Object text)
4924 struct frame *f;
4925 Lisp_Object frame;
4926 Lisp_Object name;
4927 int width, height;
4928 ptrdiff_t count = SPECPDL_INDEX ();
4929 struct gcpro gcpro1, gcpro2, gcpro3;
4930 int face_change_count_before = face_change_count;
4931 Lisp_Object buffer;
4932 struct buffer *old_buffer;
4934 if (!dpyinfo->terminal->name)
4935 error ("Terminal is not live, can't create new frames on it");
4937 parms = Fcopy_alist (parms);
4939 /* Get the name of the frame to use for resource lookup. */
4940 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
4941 if (!STRINGP (name)
4942 && !EQ (name, Qunbound)
4943 && !NILP (name))
4944 error ("Invalid frame name--not a string or nil");
4946 frame = Qnil;
4947 GCPRO3 (parms, name, frame);
4948 f = make_frame (1);
4949 XSETFRAME (frame, f);
4951 AUTO_STRING (tip, " *tip*");
4952 buffer = Fget_buffer_create (tip);
4953 /* Use set_window_buffer instead of Fset_window_buffer (see
4954 discussion of bug#11984, bug#12025, bug#12026). */
4955 set_window_buffer (FRAME_ROOT_WINDOW (f), buffer, 0, 0);
4956 old_buffer = current_buffer;
4957 set_buffer_internal_1 (XBUFFER (buffer));
4958 bset_truncate_lines (current_buffer, Qnil);
4959 specbind (Qinhibit_read_only, Qt);
4960 specbind (Qinhibit_modification_hooks, Qt);
4961 Ferase_buffer ();
4962 Finsert (1, &text);
4963 set_buffer_internal_1 (old_buffer);
4965 record_unwind_protect (unwind_create_tip_frame, frame);
4967 f->terminal = dpyinfo->terminal;
4969 /* By setting the output method, we're essentially saying that
4970 the frame is live, as per FRAME_LIVE_P. If we get a signal
4971 from this point on, x_destroy_window might screw up reference
4972 counts etc. */
4973 f->output_method = output_x_window;
4974 f->output_data.x = xzalloc (sizeof *f->output_data.x);
4975 f->output_data.x->icon_bitmap = -1;
4976 FRAME_FONTSET (f) = -1;
4977 f->output_data.x->scroll_bar_foreground_pixel = -1;
4978 f->output_data.x->scroll_bar_background_pixel = -1;
4979 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
4980 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
4981 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
4982 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
4983 f->output_data.x->white_relief.pixel = -1;
4984 f->output_data.x->black_relief.pixel = -1;
4986 fset_icon_name (f, Qnil);
4987 FRAME_DISPLAY_INFO (f) = dpyinfo;
4988 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
4989 f->output_data.x->explicit_parent = 0;
4991 /* These colors will be set anyway later, but it's important
4992 to get the color reference counts right, so initialize them! */
4994 Lisp_Object black;
4995 struct gcpro gcpro1;
4997 /* Function x_decode_color can signal an error. Make
4998 sure to initialize color slots so that we won't try
4999 to free colors we haven't allocated. */
5000 FRAME_FOREGROUND_PIXEL (f) = -1;
5001 FRAME_BACKGROUND_PIXEL (f) = -1;
5002 f->output_data.x->cursor_pixel = -1;
5003 f->output_data.x->cursor_foreground_pixel = -1;
5004 f->output_data.x->border_pixel = -1;
5005 f->output_data.x->mouse_pixel = -1;
5007 black = build_string ("black");
5008 GCPRO1 (black);
5009 FRAME_FOREGROUND_PIXEL (f)
5010 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5011 FRAME_BACKGROUND_PIXEL (f)
5012 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5013 f->output_data.x->cursor_pixel
5014 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5015 f->output_data.x->cursor_foreground_pixel
5016 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5017 f->output_data.x->border_pixel
5018 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5019 f->output_data.x->mouse_pixel
5020 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5021 UNGCPRO;
5024 /* Set the name; the functions to which we pass f expect the name to
5025 be set. */
5026 if (EQ (name, Qunbound) || NILP (name))
5028 fset_name (f, build_string (dpyinfo->x_id_name));
5029 f->explicit_name = 0;
5031 else
5033 fset_name (f, name);
5034 f->explicit_name = 1;
5035 /* use the frame's title when getting resources for this frame. */
5036 specbind (Qx_resource_name, name);
5039 register_font_driver (&xfont_driver, f);
5040 #ifdef HAVE_FREETYPE
5041 #ifdef HAVE_XFT
5042 register_font_driver (&xftfont_driver, f);
5043 #else /* not HAVE_XFT */
5044 register_font_driver (&ftxfont_driver, f);
5045 #endif /* not HAVE_XFT */
5046 #endif /* HAVE_FREETYPE */
5048 x_default_parameter (f, parms, Qfont_backend, Qnil,
5049 "fontBackend", "FontBackend", RES_TYPE_STRING);
5051 /* Extract the window parameters from the supplied values that are
5052 needed to determine window geometry. */
5053 x_default_font_parameter (f, parms);
5055 x_default_parameter (f, parms, Qborder_width, make_number (0),
5056 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
5058 /* This defaults to 2 in order to match xterm. We recognize either
5059 internalBorderWidth or internalBorder (which is what xterm calls
5060 it). */
5061 if (NILP (Fassq (Qinternal_border_width, parms)))
5063 Lisp_Object value;
5065 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
5066 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
5067 if (! EQ (value, Qunbound))
5068 parms = Fcons (Fcons (Qinternal_border_width, value),
5069 parms);
5072 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
5073 "internalBorderWidth", "internalBorderWidth",
5074 RES_TYPE_NUMBER);
5075 x_default_parameter (f, parms, Qright_divider_width, make_number (0),
5076 NULL, NULL, RES_TYPE_NUMBER);
5077 x_default_parameter (f, parms, Qbottom_divider_width, make_number (0),
5078 NULL, NULL, RES_TYPE_NUMBER);
5080 /* Also do the stuff which must be set before the window exists. */
5081 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
5082 "foreground", "Foreground", RES_TYPE_STRING);
5083 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
5084 "background", "Background", RES_TYPE_STRING);
5085 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
5086 "pointerColor", "Foreground", RES_TYPE_STRING);
5087 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
5088 "cursorColor", "Foreground", RES_TYPE_STRING);
5089 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
5090 "borderColor", "BorderColor", RES_TYPE_STRING);
5092 #ifdef GLYPH_DEBUG
5093 image_cache_refcount =
5094 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
5095 dpyinfo_refcount = dpyinfo->reference_count;
5096 #endif /* GLYPH_DEBUG */
5098 /* Init faces before x_default_parameter is called for the
5099 scroll-bar-width parameter because otherwise we end up in
5100 init_iterator with a null face cache, which should not happen. */
5101 init_frame_faces (f);
5103 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
5105 x_figure_window_size (f, parms, 0);
5108 XSetWindowAttributes attrs;
5109 unsigned long mask;
5110 Atom type = FRAME_DISPLAY_INFO (f)->Xatom_net_window_type_tooltip;
5112 block_input ();
5113 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
5114 if (DoesSaveUnders (dpyinfo->screen))
5115 mask |= CWSaveUnder;
5117 /* Window managers look at the override-redirect flag to determine
5118 whether or net to give windows a decoration (Xlib spec, chapter
5119 3.2.8). */
5120 attrs.override_redirect = True;
5121 attrs.save_under = True;
5122 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
5123 /* Arrange for getting MapNotify and UnmapNotify events. */
5124 attrs.event_mask = StructureNotifyMask;
5125 tip_window
5126 = FRAME_X_WINDOW (f)
5127 = XCreateWindow (FRAME_X_DISPLAY (f),
5128 FRAME_DISPLAY_INFO (f)->root_window,
5129 /* x, y, width, height */
5130 0, 0, 1, 1,
5131 /* Border. */
5132 f->border_width,
5133 CopyFromParent, InputOutput, CopyFromParent,
5134 mask, &attrs);
5135 XChangeProperty (FRAME_X_DISPLAY (f), tip_window,
5136 FRAME_DISPLAY_INFO (f)->Xatom_net_window_type,
5137 XA_ATOM, 32, PropModeReplace,
5138 (unsigned char *)&type, 1);
5139 unblock_input ();
5142 x_make_gc (f);
5144 x_default_parameter (f, parms, Qauto_raise, Qnil,
5145 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5146 x_default_parameter (f, parms, Qauto_lower, Qnil,
5147 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5148 x_default_parameter (f, parms, Qcursor_type, Qbox,
5149 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5151 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
5152 Change will not be effected unless different from the current
5153 FRAME_LINES (f). */
5154 width = FRAME_COLS (f);
5155 height = FRAME_LINES (f);
5156 SET_FRAME_COLS (f, 0);
5157 SET_FRAME_LINES (f, 0);
5158 change_frame_size (f, width, height, 1, 0, 0, 0);
5160 /* Add `tooltip' frame parameter's default value. */
5161 if (NILP (Fframe_parameter (frame, Qtooltip)))
5163 AUTO_FRAME_ARG (arg, Qtooltip, Qt);
5164 Fmodify_frame_parameters (frame, arg);
5167 /* FIXME - can this be done in a similar way to normal frames?
5168 http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00641.html */
5170 /* Set the `display-type' frame parameter before setting up faces. */
5172 Lisp_Object disptype;
5174 if (FRAME_DISPLAY_INFO (f)->n_planes == 1)
5175 disptype = intern ("mono");
5176 else if (FRAME_DISPLAY_INFO (f)->visual->class == GrayScale
5177 || FRAME_DISPLAY_INFO (f)->visual->class == StaticGray)
5178 disptype = intern ("grayscale");
5179 else
5180 disptype = intern ("color");
5182 if (NILP (Fframe_parameter (frame, Qdisplay_type)))
5184 AUTO_FRAME_ARG (arg, Qdisplay_type, disptype);
5185 Fmodify_frame_parameters (frame, arg);
5189 /* Set up faces after all frame parameters are known. This call
5190 also merges in face attributes specified for new frames.
5192 Frame parameters may be changed if .Xdefaults contains
5193 specifications for the default font. For example, if there is an
5194 `Emacs.default.attributeBackground: pink', the `background-color'
5195 attribute of the frame get's set, which let's the internal border
5196 of the tooltip frame appear in pink. Prevent this. */
5198 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
5200 /* Set tip_frame here, so that */
5201 tip_frame = frame;
5202 call2 (Qface_set_after_frame_default, frame, Qnil);
5204 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
5206 AUTO_FRAME_ARG (arg, Qbackground_color, bg);
5207 Fmodify_frame_parameters (frame, arg);
5211 f->no_split = 1;
5213 UNGCPRO;
5215 /* Now that the frame will be official, it counts as a reference to
5216 its display and terminal. */
5217 FRAME_DISPLAY_INFO (f)->reference_count++;
5218 f->terminal->reference_count++;
5220 /* It is now ok to make the frame official even if we get an error
5221 below. And the frame needs to be on Vframe_list or making it
5222 visible won't work. */
5223 Vframe_list = Fcons (frame, Vframe_list);
5224 f->can_x_set_window_size = true;
5226 /* Setting attributes of faces of the tooltip frame from resources
5227 and similar will increment face_change_count, which leads to the
5228 clearing of all current matrices. Since this isn't necessary
5229 here, avoid it by resetting face_change_count to the value it
5230 had before we created the tip frame. */
5231 face_change_count = face_change_count_before;
5233 /* Discard the unwind_protect. */
5234 return unbind_to (count, frame);
5238 /* Compute where to display tip frame F. PARMS is the list of frame
5239 parameters for F. DX and DY are specified offsets from the current
5240 location of the mouse. WIDTH and HEIGHT are the width and height
5241 of the tooltip. Return coordinates relative to the root window of
5242 the display in *ROOT_X, and *ROOT_Y. */
5244 static void
5245 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)
5247 Lisp_Object left, top;
5248 int win_x, win_y;
5249 Window root, child;
5250 unsigned pmask;
5252 /* User-specified position? */
5253 left = Fcdr (Fassq (Qleft, parms));
5254 top = Fcdr (Fassq (Qtop, parms));
5256 /* Move the tooltip window where the mouse pointer is. Resize and
5257 show it. */
5258 if (!INTEGERP (left) || !INTEGERP (top))
5260 block_input ();
5261 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_DISPLAY_INFO (f)->root_window,
5262 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
5263 unblock_input ();
5266 if (INTEGERP (top))
5267 *root_y = XINT (top);
5268 else if (*root_y + XINT (dy) <= 0)
5269 *root_y = 0; /* Can happen for negative dy */
5270 else if (*root_y + XINT (dy) + height
5271 <= x_display_pixel_height (FRAME_DISPLAY_INFO (f)))
5272 /* It fits below the pointer */
5273 *root_y += XINT (dy);
5274 else if (height + XINT (dy) <= *root_y)
5275 /* It fits above the pointer. */
5276 *root_y -= height + XINT (dy);
5277 else
5278 /* Put it on the top. */
5279 *root_y = 0;
5281 if (INTEGERP (left))
5282 *root_x = XINT (left);
5283 else if (*root_x + XINT (dx) <= 0)
5284 *root_x = 0; /* Can happen for negative dx */
5285 else if (*root_x + XINT (dx) + width
5286 <= x_display_pixel_width (FRAME_DISPLAY_INFO (f)))
5287 /* It fits to the right of the pointer. */
5288 *root_x += XINT (dx);
5289 else if (width + XINT (dx) <= *root_x)
5290 /* It fits to the left of the pointer. */
5291 *root_x -= width + XINT (dx);
5292 else
5293 /* Put it left-justified on the screen--it ought to fit that way. */
5294 *root_x = 0;
5298 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
5299 doc: /* Show STRING in a "tooltip" window on frame FRAME.
5300 A tooltip window is a small X window displaying a string.
5302 This is an internal function; Lisp code should call `tooltip-show'.
5304 FRAME nil or omitted means use the selected frame.
5306 PARMS is an optional list of frame parameters which can be used to
5307 change the tooltip's appearance.
5309 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
5310 means use the default timeout of 5 seconds.
5312 If the list of frame parameters PARMS contains a `left' parameters,
5313 the tooltip is displayed at that x-position. Otherwise it is
5314 displayed at the mouse position, with offset DX added (default is 5 if
5315 DX isn't specified). Likewise for the y-position; if a `top' frame
5316 parameter is specified, it determines the y-position of the tooltip
5317 window, otherwise it is displayed at the mouse position, with offset
5318 DY added (default is -10).
5320 A tooltip's maximum size is specified by `x-max-tooltip-size'.
5321 Text larger than the specified size is clipped. */)
5322 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
5324 struct frame *f;
5325 struct window *w;
5326 int root_x, root_y;
5327 struct buffer *old_buffer;
5328 struct text_pos pos;
5329 int i, width, height, seen_reversed_p;
5330 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5331 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5332 ptrdiff_t count = SPECPDL_INDEX ();
5334 specbind (Qinhibit_redisplay, Qt);
5336 GCPRO4 (string, parms, frame, timeout);
5338 CHECK_STRING (string);
5339 if (SCHARS (string) == 0)
5340 string = make_unibyte_string (" ", 1);
5342 f = decode_window_system_frame (frame);
5343 if (NILP (timeout))
5344 timeout = make_number (5);
5345 else
5346 CHECK_NATNUM (timeout);
5348 if (NILP (dx))
5349 dx = make_number (5);
5350 else
5351 CHECK_NUMBER (dx);
5353 if (NILP (dy))
5354 dy = make_number (-10);
5355 else
5356 CHECK_NUMBER (dy);
5358 #ifdef USE_GTK
5359 if (x_gtk_use_system_tooltips)
5361 bool ok;
5363 /* Hide a previous tip, if any. */
5364 Fx_hide_tip ();
5366 block_input ();
5367 ok = xg_prepare_tooltip (f, string, &width, &height);
5368 if (ok)
5370 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5371 xg_show_tooltip (f, root_x, root_y);
5372 /* This is used in Fx_hide_tip. */
5373 XSETFRAME (tip_frame, f);
5375 unblock_input ();
5376 if (ok) goto start_timer;
5378 #endif /* USE_GTK */
5380 if (NILP (last_show_tip_args))
5381 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5383 if (!NILP (tip_frame))
5385 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5386 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5387 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5389 if (EQ (frame, last_frame)
5390 && !NILP (Fequal (last_string, string))
5391 && !NILP (Fequal (last_parms, parms)))
5393 struct frame *tip_f = XFRAME (tip_frame);
5395 /* Only DX and DY have changed. */
5396 if (!NILP (tip_timer))
5398 Lisp_Object timer = tip_timer;
5399 tip_timer = Qnil;
5400 call1 (Qcancel_timer, timer);
5403 block_input ();
5404 compute_tip_xy (tip_f, parms, dx, dy, FRAME_PIXEL_WIDTH (tip_f),
5405 FRAME_PIXEL_HEIGHT (tip_f), &root_x, &root_y);
5406 XMoveWindow (FRAME_X_DISPLAY (tip_f), FRAME_X_WINDOW (tip_f),
5407 root_x, root_y);
5408 unblock_input ();
5409 goto start_timer;
5413 /* Hide a previous tip, if any. */
5414 Fx_hide_tip ();
5416 ASET (last_show_tip_args, 0, string);
5417 ASET (last_show_tip_args, 1, frame);
5418 ASET (last_show_tip_args, 2, parms);
5420 /* Add default values to frame parameters. */
5421 if (NILP (Fassq (Qname, parms)))
5422 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5423 if (NILP (Fassq (Qinternal_border_width, parms)))
5424 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5425 if (NILP (Fassq (Qborder_width, parms)))
5426 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5427 if (NILP (Fassq (Qbottom_divider_width, parms)))
5428 parms = Fcons (Fcons (Qbottom_divider_width, make_number (0)), parms);
5429 if (NILP (Fassq (Qright_divider_width, parms)))
5430 parms = Fcons (Fcons (Qright_divider_width, make_number (0)), parms);
5431 if (NILP (Fassq (Qborder_color, parms)))
5432 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5433 if (NILP (Fassq (Qbackground_color, parms)))
5434 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5435 parms);
5437 /* Create a frame for the tooltip, and record it in the global
5438 variable tip_frame. */
5439 frame = x_create_tip_frame (FRAME_DISPLAY_INFO (f), parms, string);
5440 f = XFRAME (frame);
5442 /* Set up the frame's root window. */
5443 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5444 w->left_col = 0;
5445 w->top_line = 0;
5446 w->pixel_left = 0;
5447 w->pixel_top = 0;
5449 if (CONSP (Vx_max_tooltip_size)
5450 && RANGED_INTEGERP (1, XCAR (Vx_max_tooltip_size), INT_MAX)
5451 && RANGED_INTEGERP (1, XCDR (Vx_max_tooltip_size), INT_MAX))
5453 w->total_cols = XFASTINT (XCAR (Vx_max_tooltip_size));
5454 w->total_lines = XFASTINT (XCDR (Vx_max_tooltip_size));
5456 else
5458 w->total_cols = 80;
5459 w->total_lines = 40;
5462 w->pixel_width = w->total_cols * FRAME_COLUMN_WIDTH (f);
5463 w->pixel_height = w->total_lines * FRAME_LINE_HEIGHT (f);
5465 FRAME_TOTAL_COLS (f) = w->total_cols;
5466 adjust_frame_glyphs (f);
5467 w->pseudo_window_p = 1;
5469 /* Display the tooltip text in a temporary buffer. */
5470 old_buffer = current_buffer;
5471 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->contents));
5472 bset_truncate_lines (current_buffer, Qnil);
5473 clear_glyph_matrix (w->desired_matrix);
5474 clear_glyph_matrix (w->current_matrix);
5475 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5476 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
5478 /* Compute width and height of the tooltip. */
5479 width = height = seen_reversed_p = 0;
5480 for (i = 0; i < w->desired_matrix->nrows; ++i)
5482 struct glyph_row *row = &w->desired_matrix->rows[i];
5483 struct glyph *last;
5484 int row_width;
5486 /* Stop at the first empty row at the end. */
5487 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5488 break;
5490 /* Let the row go over the full width of the frame. */
5491 row->full_width_p = 1;
5493 row_width = row->pixel_width;
5494 if (row->used[TEXT_AREA])
5496 /* There's a glyph at the end of rows that is used to place
5497 the cursor there. Don't include the width of this glyph. */
5498 if (!row->reversed_p)
5500 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5501 if (INTEGERP (last->object))
5502 row_width -= last->pixel_width;
5504 else
5506 /* There could be a stretch glyph at the beginning of R2L
5507 rows that is produced by extend_face_to_end_of_line.
5508 Don't count that glyph. */
5509 struct glyph *g = row->glyphs[TEXT_AREA];
5511 if (g->type == STRETCH_GLYPH && INTEGERP (g->object))
5513 row_width -= g->pixel_width;
5514 seen_reversed_p = 1;
5519 height += row->height;
5520 width = max (width, row_width);
5523 /* If we've seen partial-length R2L rows, we need to re-adjust the
5524 tool-tip frame width and redisplay it again, to avoid over-wide
5525 tips due to the stretch glyph that extends R2L lines to full
5526 width of the frame. */
5527 if (seen_reversed_p)
5529 /* w->total_cols and FRAME_TOTAL_COLS want the width in columns,
5530 not in pixels. */
5531 w->pixel_width = width;
5532 width /= WINDOW_FRAME_COLUMN_WIDTH (w);
5533 w->total_cols = width;
5534 FRAME_TOTAL_COLS (f) = width;
5535 SET_FRAME_WIDTH (f, width);
5536 adjust_frame_glyphs (f);
5537 clear_glyph_matrix (w->desired_matrix);
5538 clear_glyph_matrix (w->current_matrix);
5539 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
5540 width = height = 0;
5541 /* Recompute width and height of the tooltip. */
5542 for (i = 0; i < w->desired_matrix->nrows; ++i)
5544 struct glyph_row *row = &w->desired_matrix->rows[i];
5545 struct glyph *last;
5546 int row_width;
5548 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5549 break;
5550 row->full_width_p = 1;
5551 row_width = row->pixel_width;
5552 if (row->used[TEXT_AREA] && !row->reversed_p)
5554 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5555 if (INTEGERP (last->object))
5556 row_width -= last->pixel_width;
5559 height += row->height;
5560 width = max (width, row_width);
5564 /* Add the frame's internal border to the width and height the X
5565 window should have. */
5566 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5567 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5569 /* Move the tooltip window where the mouse pointer is. Resize and
5570 show it. */
5571 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5573 block_input ();
5574 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5575 root_x, root_y, width, height);
5576 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5577 unblock_input ();
5579 /* Draw into the window. */
5580 w->must_be_updated_p = true;
5581 update_single_window (w);
5583 /* Restore original current buffer. */
5584 set_buffer_internal_1 (old_buffer);
5585 windows_or_buffers_changed = old_windows_or_buffers_changed;
5587 start_timer:
5588 /* Let the tip disappear after timeout seconds. */
5589 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5590 intern ("x-hide-tip"));
5592 UNGCPRO;
5593 return unbind_to (count, Qnil);
5597 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5598 doc: /* Hide the current tooltip window, if there is any.
5599 Value is t if tooltip was open, nil otherwise. */)
5600 (void)
5602 ptrdiff_t count;
5603 Lisp_Object deleted, frame, timer;
5604 struct gcpro gcpro1, gcpro2;
5606 /* Return quickly if nothing to do. */
5607 if (NILP (tip_timer) && NILP (tip_frame))
5608 return Qnil;
5610 frame = tip_frame;
5611 timer = tip_timer;
5612 GCPRO2 (frame, timer);
5613 tip_frame = tip_timer = deleted = Qnil;
5615 count = SPECPDL_INDEX ();
5616 specbind (Qinhibit_redisplay, Qt);
5617 specbind (Qinhibit_quit, Qt);
5619 if (!NILP (timer))
5620 call1 (Qcancel_timer, timer);
5622 #ifdef USE_GTK
5624 /* When using system tooltip, tip_frame is the Emacs frame on which
5625 the tip is shown. */
5626 struct frame *f = XFRAME (frame);
5627 if (FRAME_LIVE_P (f) && xg_hide_tooltip (f))
5628 frame = Qnil;
5630 #endif
5632 if (FRAMEP (frame))
5634 delete_frame (frame, Qnil);
5635 deleted = Qt;
5637 #ifdef USE_LUCID
5638 /* Bloodcurdling hack alert: The Lucid menu bar widget's
5639 redisplay procedure is not called when a tip frame over menu
5640 items is unmapped. Redisplay the menu manually... */
5642 Widget w;
5643 struct frame *f = SELECTED_FRAME ();
5644 w = f->output_data.x->menubar_widget;
5646 if (!DoesSaveUnders (FRAME_DISPLAY_INFO (f)->screen)
5647 && w != NULL)
5649 block_input ();
5650 xlwmenu_redisplay (w);
5651 unblock_input ();
5654 #endif /* USE_LUCID */
5657 UNGCPRO;
5658 return unbind_to (count, deleted);
5663 /***********************************************************************
5664 File selection dialog
5665 ***********************************************************************/
5667 DEFUN ("x-uses-old-gtk-dialog", Fx_uses_old_gtk_dialog,
5668 Sx_uses_old_gtk_dialog,
5669 0, 0, 0,
5670 doc: /* Return t if the old Gtk+ file selection dialog is used. */)
5671 (void)
5673 #ifdef USE_GTK
5674 if (use_dialog_box
5675 && use_file_dialog
5676 && window_system_available (SELECTED_FRAME ())
5677 && xg_uses_old_file_dialog ())
5678 return Qt;
5679 #endif
5680 return Qnil;
5684 #ifdef USE_MOTIF
5685 /* Callback for "OK" and "Cancel" on file selection dialog. */
5687 static void
5688 file_dialog_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5690 int *result = client_data;
5691 XmAnyCallbackStruct *cb = call_data;
5692 *result = cb->reason;
5696 /* Callback for unmapping a file selection dialog. This is used to
5697 capture the case where a dialog is closed via a window manager's
5698 closer button, for example. Using a XmNdestroyCallback didn't work
5699 in this case. */
5701 static void
5702 file_dialog_unmap_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5704 int *result = client_data;
5705 *result = XmCR_CANCEL;
5708 static void
5709 clean_up_file_dialog (void *arg)
5711 Widget dialog = arg;
5713 /* Clean up. */
5714 block_input ();
5715 XtUnmanageChild (dialog);
5716 XtDestroyWidget (dialog);
5717 x_menu_set_in_use (0);
5718 unblock_input ();
5722 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5723 doc: /* Read file name, prompting with PROMPT in directory DIR.
5724 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5725 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5726 or directory must exist.
5728 This function is only defined on NS, MS Windows, and X Windows with the
5729 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5730 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories.
5731 On Windows 7 and later, the file selection dialog "remembers" the last
5732 directory where the user selected a file, and will open that directory
5733 instead of DIR on subsequent invocations of this function with the same
5734 value of DIR as in previous invocations; this is standard Windows behavior. */)
5735 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename,
5736 Lisp_Object mustmatch, Lisp_Object only_dir_p)
5738 int result;
5739 struct frame *f = SELECTED_FRAME ();
5740 Lisp_Object file = Qnil;
5741 Lisp_Object decoded_file;
5742 Widget dialog, text, help;
5743 Arg al[10];
5744 int ac = 0;
5745 XmString dir_xmstring, pattern_xmstring;
5746 ptrdiff_t count = SPECPDL_INDEX ();
5747 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5749 check_window_system (f);
5751 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5753 if (popup_activated ())
5754 error ("Trying to use a menu from within a menu-entry");
5756 CHECK_STRING (prompt);
5757 CHECK_STRING (dir);
5759 /* Prevent redisplay. */
5760 specbind (Qinhibit_redisplay, Qt);
5762 block_input ();
5764 /* Create the dialog with PROMPT as title, using DIR as initial
5765 directory and using "*" as pattern. */
5766 dir = Fexpand_file_name (dir, Qnil);
5767 dir_xmstring = XmStringCreateLocalized (SSDATA (dir));
5768 pattern_xmstring = XmStringCreateLocalized ("*");
5770 XtSetArg (al[ac], XmNtitle, SDATA (prompt)); ++ac;
5771 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
5772 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
5773 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
5774 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
5775 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
5776 "fsb", al, ac);
5777 XmStringFree (dir_xmstring);
5778 XmStringFree (pattern_xmstring);
5780 /* Add callbacks for OK and Cancel. */
5781 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
5782 (XtPointer) &result);
5783 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
5784 (XtPointer) &result);
5785 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
5786 (XtPointer) &result);
5788 /* Remove the help button since we can't display help. */
5789 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
5790 XtUnmanageChild (help);
5792 /* Mark OK button as default. */
5793 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
5794 XmNshowAsDefault, True, NULL);
5796 /* If MUSTMATCH is non-nil, disable the file entry field of the
5797 dialog, so that the user must select a file from the files list
5798 box. We can't remove it because we wouldn't have a way to get at
5799 the result file name, then. */
5800 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5801 if (!NILP (mustmatch))
5803 Widget label;
5804 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
5805 XtSetSensitive (text, False);
5806 XtSetSensitive (label, False);
5809 /* Manage the dialog, so that list boxes get filled. */
5810 XtManageChild (dialog);
5812 if (STRINGP (default_filename))
5814 XmString default_xmstring;
5815 Widget wtext = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5816 Widget list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
5818 XmTextPosition last_pos = XmTextFieldGetLastPosition (wtext);
5819 XmTextFieldReplace (wtext, 0, last_pos,
5820 (SSDATA (Ffile_name_nondirectory (default_filename))));
5822 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
5823 must include the path for this to work. */
5825 default_xmstring = XmStringCreateLocalized (SSDATA (default_filename));
5827 if (XmListItemExists (list, default_xmstring))
5829 int item_pos = XmListItemPos (list, default_xmstring);
5830 /* Select the item and scroll it into view. */
5831 XmListSelectPos (list, item_pos, True);
5832 XmListSetPos (list, item_pos);
5835 XmStringFree (default_xmstring);
5838 record_unwind_protect_ptr (clean_up_file_dialog, dialog);
5840 /* Process events until the user presses Cancel or OK. */
5841 x_menu_set_in_use (1);
5842 result = 0;
5843 while (result == 0)
5845 XEvent event;
5846 x_menu_wait_for_event (0);
5847 XtAppNextEvent (Xt_app_con, &event);
5848 if (event.type == KeyPress
5849 && FRAME_X_DISPLAY (f) == event.xkey.display)
5851 KeySym keysym = XLookupKeysym (&event.xkey, 0);
5853 /* Pop down on C-g. */
5854 if (keysym == XK_g && (event.xkey.state & ControlMask) != 0)
5855 XtUnmanageChild (dialog);
5858 (void) x_dispatch_event (&event, FRAME_X_DISPLAY (f));
5861 /* Get the result. */
5862 if (result == XmCR_OK)
5864 XmString text_string;
5865 String data;
5867 XtVaGetValues (dialog, XmNtextString, &text_string, NULL);
5868 XmStringGetLtoR (text_string, XmFONTLIST_DEFAULT_TAG, &data);
5869 XmStringFree (text_string);
5870 file = build_string (data);
5871 XtFree (data);
5873 else
5874 file = Qnil;
5876 unblock_input ();
5877 UNGCPRO;
5879 /* Make "Cancel" equivalent to C-g. */
5880 if (NILP (file))
5881 Fsignal (Qquit, Qnil);
5883 decoded_file = DECODE_FILE (file);
5885 return unbind_to (count, decoded_file);
5888 #endif /* USE_MOTIF */
5890 #ifdef USE_GTK
5892 static void
5893 clean_up_dialog (void)
5895 x_menu_set_in_use (0);
5898 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5899 doc: /* Read file name, prompting with PROMPT in directory DIR.
5900 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5901 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5902 or directory must exist.
5904 This function is only defined on NS, MS Windows, and X Windows with the
5905 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5906 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories.
5907 On Windows 7 and later, the file selection dialog "remembers" the last
5908 directory where the user selected a file, and will open that directory
5909 instead of DIR on subsequent invocations of this function with the same
5910 value of DIR as in previous invocations; this is standard Windows behavior. */)
5911 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
5913 struct frame *f = SELECTED_FRAME ();
5914 char *fn;
5915 Lisp_Object file = Qnil;
5916 Lisp_Object decoded_file;
5917 ptrdiff_t count = SPECPDL_INDEX ();
5918 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5919 char *cdef_file;
5921 check_window_system (f);
5923 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5925 if (popup_activated ())
5926 error ("Trying to use a menu from within a menu-entry");
5928 CHECK_STRING (prompt);
5929 CHECK_STRING (dir);
5931 /* Prevent redisplay. */
5932 specbind (Qinhibit_redisplay, Qt);
5933 record_unwind_protect_void (clean_up_dialog);
5935 block_input ();
5937 if (STRINGP (default_filename))
5938 cdef_file = SSDATA (default_filename);
5939 else
5940 cdef_file = SSDATA (dir);
5942 fn = xg_get_file_name (f, SSDATA (prompt), cdef_file,
5943 ! NILP (mustmatch),
5944 ! NILP (only_dir_p));
5946 if (fn)
5948 file = build_string (fn);
5949 xfree (fn);
5952 unblock_input ();
5953 UNGCPRO;
5955 /* Make "Cancel" equivalent to C-g. */
5956 if (NILP (file))
5957 Fsignal (Qquit, Qnil);
5959 decoded_file = DECODE_FILE (file);
5961 return unbind_to (count, decoded_file);
5965 #ifdef HAVE_FREETYPE
5967 DEFUN ("x-select-font", Fx_select_font, Sx_select_font, 0, 2, 0,
5968 doc: /* Read a font using a GTK dialog.
5969 Return either a font spec (for GTK versions >= 3.2) or a string
5970 containing a GTK-style font name.
5972 FRAME is the frame on which to pop up the font chooser. If omitted or
5973 nil, it defaults to the selected frame. */)
5974 (Lisp_Object frame, Lisp_Object ignored)
5976 struct frame *f = decode_window_system_frame (frame);
5977 Lisp_Object font;
5978 Lisp_Object font_param;
5979 char *default_name = NULL;
5980 struct gcpro gcpro1, gcpro2;
5981 ptrdiff_t count = SPECPDL_INDEX ();
5983 if (popup_activated ())
5984 error ("Trying to use a menu from within a menu-entry");
5986 /* Prevent redisplay. */
5987 specbind (Qinhibit_redisplay, Qt);
5988 record_unwind_protect_void (clean_up_dialog);
5990 block_input ();
5992 GCPRO2 (font_param, font);
5994 XSETFONT (font, FRAME_FONT (f));
5995 font_param = Ffont_get (font, intern (":name"));
5996 if (STRINGP (font_param))
5997 default_name = xstrdup (SSDATA (font_param));
5998 else
6000 font_param = Fframe_parameter (frame, Qfont_param);
6001 if (STRINGP (font_param))
6002 default_name = xstrdup (SSDATA (font_param));
6005 font = xg_get_font (f, default_name);
6006 xfree (default_name);
6008 unblock_input ();
6010 if (NILP (font))
6011 Fsignal (Qquit, Qnil);
6013 return unbind_to (count, font);
6015 #endif /* HAVE_FREETYPE */
6017 #endif /* USE_GTK */
6020 /***********************************************************************
6021 Keyboard
6022 ***********************************************************************/
6024 #ifdef HAVE_XKB
6025 #include <X11/XKBlib.h>
6026 #include <X11/keysym.h>
6027 #endif
6029 DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
6030 Sx_backspace_delete_keys_p, 0, 1, 0,
6031 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
6032 FRAME nil means use the selected frame.
6033 Value is t if we know that both keys are present, and are mapped to the
6034 usual X keysyms. Value is `lambda' if we cannot determine if both keys are
6035 present and mapped to the usual X keysyms. */)
6036 (Lisp_Object frame)
6038 #ifndef HAVE_XKB
6039 return Qlambda;
6040 #else
6041 XkbDescPtr kb;
6042 struct frame *f = decode_window_system_frame (frame);
6043 Display *dpy = FRAME_X_DISPLAY (f);
6044 Lisp_Object have_keys;
6045 int major, minor, op, event, error_code;
6047 block_input ();
6049 /* Check library version in case we're dynamically linked. */
6050 major = XkbMajorVersion;
6051 minor = XkbMinorVersion;
6052 if (!XkbLibraryVersion (&major, &minor))
6054 unblock_input ();
6055 return Qlambda;
6058 /* Check that the server supports XKB. */
6059 major = XkbMajorVersion;
6060 minor = XkbMinorVersion;
6061 if (!XkbQueryExtension (dpy, &op, &event, &error_code, &major, &minor))
6063 unblock_input ();
6064 return Qlambda;
6067 /* In this code we check that the keyboard has physical keys with names
6068 that start with BKSP (Backspace) and DELE (Delete), and that they
6069 generate keysym XK_BackSpace and XK_Delete respectively.
6070 This function is used to test if normal-erase-is-backspace should be
6071 turned on.
6072 An alternative approach would be to just check if XK_BackSpace and
6073 XK_Delete are mapped to any key. But if any of those are mapped to
6074 some non-intuitive key combination (Meta-Shift-Ctrl-whatever) and the
6075 user doesn't know about it, it is better to return false here.
6076 It is more obvious to the user what to do if she/he has two keys
6077 clearly marked with names/symbols and one key does something not
6078 expected (i.e. she/he then tries the other).
6079 The cases where Backspace/Delete is mapped to some other key combination
6080 are rare, and in those cases, normal-erase-is-backspace can be turned on
6081 manually. */
6083 have_keys = Qnil;
6084 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
6085 if (kb)
6087 int delete_keycode = 0, backspace_keycode = 0, i;
6089 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
6091 for (i = kb->min_key_code;
6092 (i < kb->max_key_code
6093 && (delete_keycode == 0 || backspace_keycode == 0));
6094 ++i)
6096 /* The XKB symbolic key names can be seen most easily in
6097 the PS file generated by `xkbprint -label name
6098 $DISPLAY'. */
6099 if (memcmp ("DELE", kb->names->keys[i].name, 4) == 0)
6100 delete_keycode = i;
6101 else if (memcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
6102 backspace_keycode = i;
6105 XkbFreeNames (kb, 0, True);
6108 /* As of libX11-1.6.2, XkbGetMap manual says that you should use
6109 XkbFreeClientMap to free the data returned by XkbGetMap. But
6110 this function just frees the data referenced from KB and not
6111 KB itself. To free KB as well, call XkbFreeKeyboard. */
6112 XkbFreeKeyboard (kb, XkbAllMapComponentsMask, True);
6114 if (delete_keycode
6115 && backspace_keycode
6116 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
6117 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
6118 have_keys = Qt;
6120 unblock_input ();
6121 return have_keys;
6122 #endif
6127 /***********************************************************************
6128 Initialization
6129 ***********************************************************************/
6131 /* Keep this list in the same order as frame_parms in frame.c.
6132 Use 0 for unsupported frame parameters. */
6134 frame_parm_handler x_frame_parm_handlers[] =
6136 x_set_autoraise,
6137 x_set_autolower,
6138 x_set_background_color,
6139 x_set_border_color,
6140 x_set_border_width,
6141 x_set_cursor_color,
6142 x_set_cursor_type,
6143 x_set_font,
6144 x_set_foreground_color,
6145 x_set_icon_name,
6146 x_set_icon_type,
6147 x_set_internal_border_width,
6148 x_set_right_divider_width,
6149 x_set_bottom_divider_width,
6150 x_set_menu_bar_lines,
6151 x_set_mouse_color,
6152 x_explicitly_set_name,
6153 x_set_scroll_bar_width,
6154 x_set_scroll_bar_height,
6155 x_set_title,
6156 x_set_unsplittable,
6157 x_set_vertical_scroll_bars,
6158 x_set_horizontal_scroll_bars,
6159 x_set_visibility,
6160 x_set_tool_bar_lines,
6161 x_set_scroll_bar_foreground,
6162 x_set_scroll_bar_background,
6163 x_set_screen_gamma,
6164 x_set_line_spacing,
6165 x_set_left_fringe,
6166 x_set_right_fringe,
6167 x_set_wait_for_wm,
6168 x_set_fullscreen,
6169 x_set_font_backend,
6170 x_set_alpha,
6171 x_set_sticky,
6172 x_set_tool_bar_position,
6175 void
6176 syms_of_xfns (void)
6178 DEFSYM (Qundefined_color, "undefined-color");
6179 DEFSYM (Qcompound_text, "compound-text");
6180 DEFSYM (Qcancel_timer, "cancel-timer");
6181 DEFSYM (Qfont_param, "font-parameter");
6183 Fput (Qundefined_color, Qerror_conditions,
6184 listn (CONSTYPE_PURE, 2, Qundefined_color, Qerror));
6185 Fput (Qundefined_color, Qerror_message,
6186 build_pure_c_string ("Undefined color"));
6188 DEFVAR_LISP ("x-pointer-shape", Vx_pointer_shape,
6189 doc: /* The shape of the pointer when over text.
6190 Changing the value does not affect existing frames
6191 unless you set the mouse color. */);
6192 Vx_pointer_shape = Qnil;
6194 #if 0 /* This doesn't really do anything. */
6195 DEFVAR_LISP ("x-nontext-pointer-shape", Vx_nontext_pointer_shape,
6196 doc: /* The shape of the pointer when not over text.
6197 This variable takes effect when you create a new frame
6198 or when you set the mouse color. */);
6199 #endif
6200 Vx_nontext_pointer_shape = Qnil;
6202 DEFVAR_LISP ("x-hourglass-pointer-shape", Vx_hourglass_pointer_shape,
6203 doc: /* The shape of the pointer when Emacs is busy.
6204 This variable takes effect when you create a new frame
6205 or when you set the mouse color. */);
6206 Vx_hourglass_pointer_shape = Qnil;
6208 #if 0 /* This doesn't really do anything. */
6209 DEFVAR_LISP ("x-mode-pointer-shape", Vx_mode_pointer_shape,
6210 doc: /* The shape of the pointer when over the mode line.
6211 This variable takes effect when you create a new frame
6212 or when you set the mouse color. */);
6213 #endif
6214 Vx_mode_pointer_shape = Qnil;
6216 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
6217 Vx_sensitive_text_pointer_shape,
6218 doc: /* The shape of the pointer when over mouse-sensitive text.
6219 This variable takes effect when you create a new frame
6220 or when you set the mouse color. */);
6221 Vx_sensitive_text_pointer_shape = Qnil;
6223 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
6224 Vx_window_horizontal_drag_shape,
6225 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
6226 This variable takes effect when you create a new frame
6227 or when you set the mouse color. */);
6228 Vx_window_horizontal_drag_shape = Qnil;
6230 DEFVAR_LISP ("x-window-vertical-drag-cursor",
6231 Vx_window_vertical_drag_shape,
6232 doc: /* Pointer shape to use for indicating a window can be dragged vertically.
6233 This variable takes effect when you create a new frame
6234 or when you set the mouse color. */);
6235 Vx_window_vertical_drag_shape = Qnil;
6237 DEFVAR_LISP ("x-cursor-fore-pixel", Vx_cursor_fore_pixel,
6238 doc: /* A string indicating the foreground color of the cursor box. */);
6239 Vx_cursor_fore_pixel = Qnil;
6241 DEFVAR_LISP ("x-max-tooltip-size", Vx_max_tooltip_size,
6242 doc: /* Maximum size for tooltips.
6243 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
6244 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
6246 DEFVAR_LISP ("x-no-window-manager", Vx_no_window_manager,
6247 doc: /* Non-nil if no X window manager is in use.
6248 Emacs doesn't try to figure this out; this is always nil
6249 unless you set it to something else. */);
6250 /* We don't have any way to find this out, so set it to nil
6251 and maybe the user would like to set it to t. */
6252 Vx_no_window_manager = Qnil;
6254 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
6255 Vx_pixel_size_width_font_regexp,
6256 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
6258 Since Emacs gets width of a font matching with this regexp from
6259 PIXEL_SIZE field of the name, font finding mechanism gets faster for
6260 such a font. This is especially effective for such large fonts as
6261 Chinese, Japanese, and Korean. */);
6262 Vx_pixel_size_width_font_regexp = Qnil;
6264 /* This is not ifdef:ed, so other builds than GTK can customize it. */
6265 DEFVAR_BOOL ("x-gtk-use-old-file-dialog", x_gtk_use_old_file_dialog,
6266 doc: /* Non-nil means prompt with the old GTK file selection dialog.
6267 If nil or if the file selection dialog is not available, the new GTK file
6268 chooser is used instead. To turn off all file dialogs set the
6269 variable `use-file-dialog'. */);
6270 x_gtk_use_old_file_dialog = 0;
6272 DEFVAR_BOOL ("x-gtk-show-hidden-files", x_gtk_show_hidden_files,
6273 doc: /* If non-nil, the GTK file chooser will by default show hidden files.
6274 Note that this is just the default, there is a toggle button on the file
6275 chooser to show or not show hidden files on a case by case basis. */);
6276 x_gtk_show_hidden_files = 0;
6278 DEFVAR_BOOL ("x-gtk-file-dialog-help-text", x_gtk_file_dialog_help_text,
6279 doc: /* If non-nil, the GTK file chooser will show additional help text.
6280 If more space for files in the file chooser dialog is wanted, set this to nil
6281 to turn the additional text off. */);
6282 x_gtk_file_dialog_help_text = 1;
6284 DEFVAR_BOOL ("x-gtk-use-system-tooltips", x_gtk_use_system_tooltips,
6285 doc: /* If non-nil with a Gtk+ built Emacs, the Gtk+ tooltip is used.
6286 Otherwise use Emacs own tooltip implementation.
6287 When using Gtk+ tooltips, the tooltip face is not used. */);
6288 x_gtk_use_system_tooltips = 1;
6290 /* Tell Emacs about this window system. */
6291 Fprovide (Qx, Qnil);
6293 #ifdef USE_X_TOOLKIT
6294 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6295 #ifdef USE_MOTIF
6296 Fprovide (intern_c_string ("motif"), Qnil);
6298 DEFVAR_LISP ("motif-version-string", Vmotif_version_string,
6299 doc: /* Version info for LessTif/Motif. */);
6300 Vmotif_version_string = build_string (XmVERSION_STRING);
6301 #endif /* USE_MOTIF */
6302 #endif /* USE_X_TOOLKIT */
6304 #ifdef USE_GTK
6305 /* Provide x-toolkit also for GTK. Internally GTK does not use Xt so it
6306 is not an X toolkit in that sense (USE_X_TOOLKIT is not defined).
6307 But for a user it is a toolkit for X, and indeed, configure
6308 accepts --with-x-toolkit=gtk. */
6309 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6310 Fprovide (intern_c_string ("gtk"), Qnil);
6311 Fprovide (intern_c_string ("move-toolbar"), Qnil);
6313 DEFVAR_LISP ("gtk-version-string", Vgtk_version_string,
6314 doc: /* Version info for GTK+. */);
6316 char gtk_version[sizeof ".." + 3 * INT_STRLEN_BOUND (int)];
6317 int len = sprintf (gtk_version, "%d.%d.%d",
6318 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
6319 Vgtk_version_string = make_pure_string (gtk_version, len, len, 0);
6321 #endif /* USE_GTK */
6323 /* X window properties. */
6324 defsubr (&Sx_change_window_property);
6325 defsubr (&Sx_delete_window_property);
6326 defsubr (&Sx_window_property);
6328 defsubr (&Sxw_display_color_p);
6329 defsubr (&Sx_display_grayscale_p);
6330 defsubr (&Sxw_color_defined_p);
6331 defsubr (&Sxw_color_values);
6332 defsubr (&Sx_server_max_request_size);
6333 defsubr (&Sx_server_vendor);
6334 defsubr (&Sx_server_version);
6335 defsubr (&Sx_display_pixel_width);
6336 defsubr (&Sx_display_pixel_height);
6337 defsubr (&Sx_display_mm_width);
6338 defsubr (&Sx_display_mm_height);
6339 defsubr (&Sx_display_screens);
6340 defsubr (&Sx_display_planes);
6341 defsubr (&Sx_display_color_cells);
6342 defsubr (&Sx_display_visual_class);
6343 defsubr (&Sx_display_backing_store);
6344 defsubr (&Sx_display_save_under);
6345 defsubr (&Sx_display_monitor_attributes_list);
6346 defsubr (&Sx_frame_geometry);
6347 defsubr (&Sx_wm_set_size_hint);
6348 defsubr (&Sx_create_frame);
6349 defsubr (&Sx_open_connection);
6350 defsubr (&Sx_close_connection);
6351 defsubr (&Sx_display_list);
6352 defsubr (&Sx_synchronize);
6353 defsubr (&Sx_backspace_delete_keys_p);
6355 defsubr (&Sx_show_tip);
6356 defsubr (&Sx_hide_tip);
6357 tip_timer = Qnil;
6358 staticpro (&tip_timer);
6359 tip_frame = Qnil;
6360 staticpro (&tip_frame);
6362 last_show_tip_args = Qnil;
6363 staticpro (&last_show_tip_args);
6365 defsubr (&Sx_uses_old_gtk_dialog);
6366 #if defined (USE_MOTIF) || defined (USE_GTK)
6367 defsubr (&Sx_file_dialog);
6368 #endif
6370 #if defined (USE_GTK) && defined (HAVE_FREETYPE)
6371 defsubr (&Sx_select_font);
6372 #endif