Draw outermost line using black relief and erase corners also for cairo.
[emacs.git] / src / xfns.c
blob5b591d317697f39903b2235a3c5e6ff6c188404c
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 #include "bitmaps/gray.xbm"
50 #include "xsettings.h"
52 #ifdef HAVE_XRANDR
53 #include <X11/extensions/Xrandr.h>
54 #endif
55 #ifdef HAVE_XINERAMA
56 #include <X11/extensions/Xinerama.h>
57 #endif
59 #ifdef USE_GTK
60 #include "gtkutil.h"
61 #endif
63 #ifdef USE_X_TOOLKIT
64 #include <X11/Shell.h>
66 #ifndef USE_MOTIF
67 #ifdef HAVE_XAW3D
68 #include <X11/Xaw3d/Paned.h>
69 #include <X11/Xaw3d/Label.h>
70 #else /* !HAVE_XAW3D */
71 #include <X11/Xaw/Paned.h>
72 #include <X11/Xaw/Label.h>
73 #endif /* HAVE_XAW3D */
74 #endif /* USE_MOTIF */
76 #ifdef USG
77 #undef USG /* ####KLUDGE for Solaris 2.2 and up */
78 #include <X11/Xos.h>
79 #define USG
80 #ifdef USG /* Pacify gcc -Wunused-macros. */
81 #endif
82 #else
83 #include <X11/Xos.h>
84 #endif
86 #include "widget.h"
88 #include "../lwlib/lwlib.h"
90 #ifdef USE_MOTIF
91 #include <Xm/Xm.h>
92 #include <Xm/DialogS.h>
93 #include <Xm/FileSB.h>
94 #include <Xm/List.h>
95 #include <Xm/TextF.h>
96 #endif
98 #ifdef USE_LUCID
99 #include "../lwlib/xlwmenu.h"
100 #endif
102 #if !defined (NO_EDITRES)
103 #define HACK_EDITRES
104 extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
105 #endif /* not defined NO_EDITRES */
107 /* Unique id counter for widgets created by the Lucid Widget Library. */
109 extern LWLIB_ID widget_id_tick;
111 #ifdef USE_MOTIF
113 #endif /* USE_MOTIF */
115 #endif /* USE_X_TOOLKIT */
117 #ifdef USE_GTK
119 #endif /* USE_GTK */
121 #define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
123 #ifdef GLYPH_DEBUG
124 static ptrdiff_t image_cache_refcount;
125 static int dpyinfo_refcount;
126 #endif
128 static struct x_display_info *x_display_info_for_name (Lisp_Object);
130 /* Let the user specify an X display with a Lisp object.
131 OBJECT may be nil, a frame or a terminal object.
132 nil stands for the selected frame--or, if that is not an X frame,
133 the first X display on the list. */
135 struct x_display_info *
136 check_x_display_info (Lisp_Object object)
138 struct x_display_info *dpyinfo = NULL;
140 if (NILP (object))
142 struct frame *sf = XFRAME (selected_frame);
144 if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
145 dpyinfo = FRAME_DISPLAY_INFO (sf);
146 else if (x_display_list != 0)
147 dpyinfo = x_display_list;
148 else
149 error ("X windows are not in use or not initialized");
151 else if (TERMINALP (object))
153 struct terminal *t = decode_live_terminal (object);
155 if (t->type != output_x_window)
156 error ("Terminal %d is not an X display", t->id);
158 dpyinfo = t->display_info.x;
160 else if (STRINGP (object))
161 dpyinfo = x_display_info_for_name (object);
162 else
164 struct frame *f = decode_window_system_frame (object);
165 dpyinfo = FRAME_DISPLAY_INFO (f);
168 return dpyinfo;
171 /* Store the screen positions of frame F into XPTR and YPTR.
172 These are the positions of the containing window manager window,
173 not Emacs's own window. */
175 void
176 x_real_positions (struct frame *f, int *xptr, int *yptr)
178 int win_x, win_y, outer_x IF_LINT (= 0), outer_y IF_LINT (= 0);
179 int real_x = 0, real_y = 0;
180 bool had_errors = false;
181 Window win = f->output_data.x->parent_desc;
182 Atom actual_type;
183 unsigned long actual_size, bytes_remaining;
184 int rc, actual_format;
185 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
186 long max_len = 400;
187 Display *dpy = FRAME_X_DISPLAY (f);
188 unsigned char *tmp_data = NULL;
189 Atom target_type = XA_CARDINAL;
191 block_input ();
193 x_catch_errors (dpy);
195 if (win == dpyinfo->root_window)
196 win = FRAME_OUTER_WINDOW (f);
198 /* This loop traverses up the containment tree until we hit the root
199 window. Window managers may intersect many windows between our window
200 and the root window. The window we find just before the root window
201 should be the outer WM window. */
202 for (;;)
204 Window wm_window, rootw;
205 Window *tmp_children;
206 unsigned int tmp_nchildren;
207 int success;
209 success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
210 &wm_window, &tmp_children, &tmp_nchildren);
212 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
214 /* Don't free tmp_children if XQueryTree failed. */
215 if (! success)
216 break;
218 XFree (tmp_children);
220 if (wm_window == rootw || had_errors)
221 break;
223 win = wm_window;
226 if (! had_errors)
228 unsigned int ign;
229 Window child, rootw;
231 /* Get the real coordinates for the WM window upper left corner */
232 XGetGeometry (FRAME_X_DISPLAY (f), win,
233 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
235 /* Translate real coordinates to coordinates relative to our
236 window. For our window, the upper left corner is 0, 0.
237 Since the upper left corner of the WM window is outside
238 our window, win_x and win_y will be negative:
240 ------------------ ---> x
241 | title |
242 | ----------------- v y
243 | | our window
245 XTranslateCoordinates (FRAME_X_DISPLAY (f),
247 /* From-window, to-window. */
248 FRAME_DISPLAY_INFO (f)->root_window,
249 FRAME_X_WINDOW (f),
251 /* From-position, to-position. */
252 real_x, real_y, &win_x, &win_y,
254 /* Child of win. */
255 &child);
257 if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
259 outer_x = win_x;
260 outer_y = win_y;
262 else
264 XTranslateCoordinates (FRAME_X_DISPLAY (f),
266 /* From-window, to-window. */
267 FRAME_DISPLAY_INFO (f)->root_window,
268 FRAME_OUTER_WINDOW (f),
270 /* From-position, to-position. */
271 real_x, real_y, &outer_x, &outer_y,
273 /* Child of win. */
274 &child);
277 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
281 if (dpyinfo->root_window == f->output_data.x->parent_desc)
283 /* Try _NET_FRAME_EXTENTS if our parent is the root window. */
284 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_frame_extents,
285 0, max_len, False, target_type,
286 &actual_type, &actual_format, &actual_size,
287 &bytes_remaining, &tmp_data);
289 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
290 && actual_size == 4 && actual_format == 32)
292 unsigned int ign;
293 Window rootw;
294 long *fe = (long *)tmp_data;
296 XGetGeometry (FRAME_X_DISPLAY (f), win,
297 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
298 outer_x = -fe[0];
299 outer_y = -fe[2];
300 real_x -= fe[0];
301 real_y -= fe[2];
305 if (tmp_data) XFree (tmp_data);
307 x_uncatch_errors ();
309 unblock_input ();
311 if (had_errors) return;
313 f->x_pixels_diff = -win_x;
314 f->y_pixels_diff = -win_y;
316 FRAME_X_OUTPUT (f)->x_pixels_outer_diff = -outer_x;
317 FRAME_X_OUTPUT (f)->y_pixels_outer_diff = -outer_y;
319 *xptr = real_x;
320 *yptr = real_y;
323 /* Get the mouse position in frame relative coordinates. */
325 void
326 x_relative_mouse_position (struct frame *f, int *x, int *y)
328 Window root, dummy_window;
329 int dummy;
331 eassert (FRAME_X_P (f));
333 block_input ();
335 XQueryPointer (FRAME_X_DISPLAY (f),
336 DefaultRootWindow (FRAME_X_DISPLAY (f)),
338 /* The root window which contains the pointer. */
339 &root,
341 /* Window pointer is on, not used */
342 &dummy_window,
344 /* The position on that root window. */
345 x, y,
347 /* x/y in dummy_window coordinates, not used. */
348 &dummy, &dummy,
350 /* Modifier keys and pointer buttons, about which
351 we don't care. */
352 (unsigned int *) &dummy);
354 unblock_input ();
356 /* Translate root window coordinates to window coordinates. */
357 *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
358 *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
361 /* Gamma-correct COLOR on frame F. */
363 void
364 gamma_correct (struct frame *f, XColor *color)
366 if (f->gamma)
368 color->red = pow (color->red / 65535.0, f->gamma) * 65535.0 + 0.5;
369 color->green = pow (color->green / 65535.0, f->gamma) * 65535.0 + 0.5;
370 color->blue = pow (color->blue / 65535.0, f->gamma) * 65535.0 + 0.5;
375 /* Decide if color named COLOR_NAME is valid for use on frame F. If
376 so, return the RGB values in COLOR. If ALLOC_P,
377 allocate the color. Value is false if COLOR_NAME is invalid, or
378 no color could be allocated. */
380 bool
381 x_defined_color (struct frame *f, const char *color_name,
382 XColor *color, bool alloc_p)
384 bool success_p = false;
385 Display *dpy = FRAME_X_DISPLAY (f);
386 Colormap cmap = FRAME_X_COLORMAP (f);
388 block_input ();
389 #ifdef USE_GTK
390 success_p = xg_check_special_colors (f, color_name, color);
391 #endif
392 if (!success_p)
393 success_p = XParseColor (dpy, cmap, color_name, color) != 0;
394 if (success_p && alloc_p)
395 success_p = x_alloc_nearest_color (f, cmap, color);
396 unblock_input ();
398 return success_p;
402 /* Return the pixel color value for color COLOR_NAME on frame F. If F
403 is a monochrome frame, return MONO_COLOR regardless of what ARG says.
404 Signal an error if color can't be allocated. */
406 static int
407 x_decode_color (struct frame *f, Lisp_Object color_name, int mono_color)
409 XColor cdef;
411 CHECK_STRING (color_name);
413 #if false /* Don't do this. It's wrong when we're not using the default
414 colormap, it makes freeing difficult, and it's probably not
415 an important optimization. */
416 if (strcmp (SDATA (color_name), "black") == 0)
417 return BLACK_PIX_DEFAULT (f);
418 else if (strcmp (SDATA (color_name), "white") == 0)
419 return WHITE_PIX_DEFAULT (f);
420 #endif
422 /* Return MONO_COLOR for monochrome frames. */
423 if (FRAME_DISPLAY_INFO (f)->n_planes == 1)
424 return mono_color;
426 /* x_defined_color is responsible for coping with failures
427 by looking for a near-miss. */
428 if (x_defined_color (f, SSDATA (color_name), &cdef, true))
429 return cdef.pixel;
431 signal_error ("Undefined color", color_name);
436 /* Change the `wait-for-wm' frame parameter of frame F. OLD_VALUE is
437 the previous value of that parameter, NEW_VALUE is the new value.
438 See also the comment of wait_for_wm in struct x_output. */
440 static void
441 x_set_wait_for_wm (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
443 f->output_data.x->wait_for_wm = !NILP (new_value);
446 static void
447 x_set_tool_bar_position (struct frame *f,
448 Lisp_Object new_value,
449 Lisp_Object old_value)
451 Lisp_Object choice = list4 (Qleft, Qright, Qtop, Qbottom);
453 if (!NILP (Fmemq (new_value, choice)))
455 #ifdef USE_GTK
456 if (!EQ (new_value, old_value))
458 xg_change_toolbar_position (f, new_value);
459 fset_tool_bar_position (f, new_value);
461 #else
462 if (!EQ (new_value, Qtop))
463 error ("The only supported tool bar position is top");
464 #endif
466 else
467 wrong_choice (choice, new_value);
470 #ifdef USE_GTK
472 /* Set icon from FILE for frame F. By using GTK functions the icon
473 may be any format that GdkPixbuf knows about, i.e. not just bitmaps. */
475 bool
476 xg_set_icon (struct frame *f, Lisp_Object file)
478 bool result = false;
479 Lisp_Object found;
481 found = x_find_image_file (file);
483 if (! NILP (found))
485 GdkPixbuf *pixbuf;
486 GError *err = NULL;
487 char *filename = SSDATA (found);
488 block_input ();
490 pixbuf = gdk_pixbuf_new_from_file (filename, &err);
492 if (pixbuf)
494 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
495 pixbuf);
496 g_object_unref (pixbuf);
498 result = true;
500 else
501 g_error_free (err);
503 unblock_input ();
506 return result;
509 bool
510 xg_set_icon_from_xpm_data (struct frame *f, const char **data)
512 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data (data);
514 if (!pixbuf)
515 return false;
517 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
518 g_object_unref (pixbuf);
519 return true;
521 #endif /* USE_GTK */
524 /* Functions called only from `x_set_frame_param'
525 to set individual parameters.
527 If FRAME_X_WINDOW (f) is 0,
528 the frame is being created and its X-window does not exist yet.
529 In that case, just record the parameter's new value
530 in the standard place; do not attempt to change the window. */
532 static void
533 x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
535 struct x_output *x = f->output_data.x;
536 unsigned long fg, old_fg;
538 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
539 old_fg = FRAME_FOREGROUND_PIXEL (f);
540 FRAME_FOREGROUND_PIXEL (f) = fg;
542 if (FRAME_X_WINDOW (f) != 0)
544 Display *dpy = FRAME_X_DISPLAY (f);
546 block_input ();
547 XSetForeground (dpy, x->normal_gc, fg);
548 XSetBackground (dpy, x->reverse_gc, fg);
550 if (x->cursor_pixel == old_fg)
552 unload_color (f, x->cursor_pixel);
553 x->cursor_pixel = x_copy_color (f, fg);
554 XSetBackground (dpy, x->cursor_gc, x->cursor_pixel);
557 unblock_input ();
559 update_face_from_frame_parameter (f, Qforeground_color, arg);
561 if (FRAME_VISIBLE_P (f))
562 redraw_frame (f);
565 unload_color (f, old_fg);
568 static void
569 x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
571 struct x_output *x = f->output_data.x;
572 unsigned long bg;
574 bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
575 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
576 FRAME_BACKGROUND_PIXEL (f) = bg;
578 if (FRAME_X_WINDOW (f) != 0)
580 Display *dpy = FRAME_X_DISPLAY (f);
582 block_input ();
583 XSetBackground (dpy, x->normal_gc, bg);
584 XSetForeground (dpy, x->reverse_gc, bg);
585 XSetWindowBackground (dpy, FRAME_X_WINDOW (f), bg);
586 XSetForeground (dpy, x->cursor_gc, bg);
588 #ifdef USE_GTK
589 xg_set_background_color (f, bg);
590 #endif
592 #ifndef USE_TOOLKIT_SCROLL_BARS /* Turns out to be annoying with
593 toolkit scroll bars. */
595 Lisp_Object bar;
596 for (bar = FRAME_SCROLL_BARS (f);
597 !NILP (bar);
598 bar = XSCROLL_BAR (bar)->next)
600 Window window = XSCROLL_BAR (bar)->x_window;
601 XSetWindowBackground (dpy, window, bg);
604 #endif /* USE_TOOLKIT_SCROLL_BARS */
606 unblock_input ();
607 update_face_from_frame_parameter (f, Qbackground_color, arg);
609 if (FRAME_VISIBLE_P (f))
610 redraw_frame (f);
614 static void
615 x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
617 struct x_output *x = f->output_data.x;
618 Display *dpy = FRAME_X_DISPLAY (f);
619 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
620 Cursor hourglass_cursor, horizontal_drag_cursor, vertical_drag_cursor;
621 unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
622 unsigned long mask_color = FRAME_BACKGROUND_PIXEL (f);
624 /* Don't let pointers be invisible. */
625 if (mask_color == pixel)
627 x_free_colors (f, &pixel, 1);
628 pixel = x_copy_color (f, FRAME_FOREGROUND_PIXEL (f));
631 unload_color (f, x->mouse_pixel);
632 x->mouse_pixel = pixel;
634 block_input ();
636 /* It's not okay to crash if the user selects a screwy cursor. */
637 x_catch_errors (dpy);
639 if (!NILP (Vx_pointer_shape))
641 CHECK_NUMBER (Vx_pointer_shape);
642 cursor = XCreateFontCursor (dpy, XINT (Vx_pointer_shape));
644 else
645 cursor = XCreateFontCursor (dpy, XC_xterm);
646 x_check_errors (dpy, "bad text pointer cursor: %s");
648 if (!NILP (Vx_nontext_pointer_shape))
650 CHECK_NUMBER (Vx_nontext_pointer_shape);
651 nontext_cursor
652 = XCreateFontCursor (dpy, XINT (Vx_nontext_pointer_shape));
654 else
655 nontext_cursor = XCreateFontCursor (dpy, XC_left_ptr);
656 x_check_errors (dpy, "bad nontext pointer cursor: %s");
658 if (!NILP (Vx_hourglass_pointer_shape))
660 CHECK_NUMBER (Vx_hourglass_pointer_shape);
661 hourglass_cursor
662 = XCreateFontCursor (dpy, XINT (Vx_hourglass_pointer_shape));
664 else
665 hourglass_cursor = XCreateFontCursor (dpy, XC_watch);
666 x_check_errors (dpy, "bad hourglass pointer cursor: %s");
668 if (!NILP (Vx_mode_pointer_shape))
670 CHECK_NUMBER (Vx_mode_pointer_shape);
671 mode_cursor = XCreateFontCursor (dpy, XINT (Vx_mode_pointer_shape));
673 else
674 mode_cursor = XCreateFontCursor (dpy, XC_xterm);
675 x_check_errors (dpy, "bad modeline pointer cursor: %s");
677 if (!NILP (Vx_sensitive_text_pointer_shape))
679 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
680 hand_cursor
681 = XCreateFontCursor (dpy, XINT (Vx_sensitive_text_pointer_shape));
683 else
684 hand_cursor = XCreateFontCursor (dpy, XC_hand2);
686 if (!NILP (Vx_window_horizontal_drag_shape))
688 CHECK_TYPE_RANGED_INTEGER (unsigned, Vx_window_horizontal_drag_shape);
689 horizontal_drag_cursor
690 = XCreateFontCursor (dpy, XINT (Vx_window_horizontal_drag_shape));
692 else
693 horizontal_drag_cursor
694 = XCreateFontCursor (dpy, XC_sb_h_double_arrow);
696 if (!NILP (Vx_window_vertical_drag_shape))
698 CHECK_NUMBER (Vx_window_vertical_drag_shape);
699 vertical_drag_cursor
700 = XCreateFontCursor (dpy, XINT (Vx_window_vertical_drag_shape));
702 else
703 vertical_drag_cursor
704 = XCreateFontCursor (dpy, XC_sb_v_double_arrow);
706 /* Check and report errors with the above calls. */
707 x_check_errors (dpy, "can't set cursor shape: %s");
708 x_uncatch_errors ();
711 XColor fore_color, back_color;
713 fore_color.pixel = x->mouse_pixel;
714 x_query_color (f, &fore_color);
715 back_color.pixel = mask_color;
716 x_query_color (f, &back_color);
718 XRecolorCursor (dpy, cursor, &fore_color, &back_color);
719 XRecolorCursor (dpy, nontext_cursor, &fore_color, &back_color);
720 XRecolorCursor (dpy, mode_cursor, &fore_color, &back_color);
721 XRecolorCursor (dpy, hand_cursor, &fore_color, &back_color);
722 XRecolorCursor (dpy, hourglass_cursor, &fore_color, &back_color);
723 XRecolorCursor (dpy, horizontal_drag_cursor, &fore_color, &back_color);
724 XRecolorCursor (dpy, vertical_drag_cursor, &fore_color, &back_color);
727 if (FRAME_X_WINDOW (f) != 0)
728 XDefineCursor (dpy, FRAME_X_WINDOW (f),
729 f->output_data.x->current_cursor = cursor);
731 if (cursor != x->text_cursor
732 && x->text_cursor != 0)
733 XFreeCursor (dpy, x->text_cursor);
734 x->text_cursor = cursor;
736 if (nontext_cursor != x->nontext_cursor
737 && x->nontext_cursor != 0)
738 XFreeCursor (dpy, x->nontext_cursor);
739 x->nontext_cursor = nontext_cursor;
741 if (hourglass_cursor != x->hourglass_cursor
742 && x->hourglass_cursor != 0)
743 XFreeCursor (dpy, x->hourglass_cursor);
744 x->hourglass_cursor = hourglass_cursor;
746 if (mode_cursor != x->modeline_cursor
747 && x->modeline_cursor != 0)
748 XFreeCursor (dpy, f->output_data.x->modeline_cursor);
749 x->modeline_cursor = mode_cursor;
751 if (hand_cursor != x->hand_cursor
752 && x->hand_cursor != 0)
753 XFreeCursor (dpy, x->hand_cursor);
754 x->hand_cursor = hand_cursor;
756 if (horizontal_drag_cursor != x->horizontal_drag_cursor
757 && x->horizontal_drag_cursor != 0)
758 XFreeCursor (dpy, x->horizontal_drag_cursor);
759 x->horizontal_drag_cursor = horizontal_drag_cursor;
761 if (vertical_drag_cursor != x->vertical_drag_cursor
762 && x->vertical_drag_cursor != 0)
763 XFreeCursor (dpy, x->vertical_drag_cursor);
764 x->vertical_drag_cursor = vertical_drag_cursor;
766 XFlush (dpy);
767 unblock_input ();
769 update_face_from_frame_parameter (f, Qmouse_color, arg);
772 static void
773 x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
775 unsigned long fore_pixel, pixel;
776 bool fore_pixel_allocated_p = false, pixel_allocated_p = false;
777 struct x_output *x = f->output_data.x;
779 if (!NILP (Vx_cursor_fore_pixel))
781 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
782 WHITE_PIX_DEFAULT (f));
783 fore_pixel_allocated_p = true;
785 else
786 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
788 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
789 pixel_allocated_p = true;
791 /* Make sure that the cursor color differs from the background color. */
792 if (pixel == FRAME_BACKGROUND_PIXEL (f))
794 if (pixel_allocated_p)
796 x_free_colors (f, &pixel, 1);
797 pixel_allocated_p = false;
800 pixel = x->mouse_pixel;
801 if (pixel == fore_pixel)
803 if (fore_pixel_allocated_p)
805 x_free_colors (f, &fore_pixel, 1);
806 fore_pixel_allocated_p = false;
808 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
812 unload_color (f, x->cursor_foreground_pixel);
813 if (!fore_pixel_allocated_p)
814 fore_pixel = x_copy_color (f, fore_pixel);
815 x->cursor_foreground_pixel = fore_pixel;
817 unload_color (f, x->cursor_pixel);
818 if (!pixel_allocated_p)
819 pixel = x_copy_color (f, pixel);
820 x->cursor_pixel = pixel;
822 if (FRAME_X_WINDOW (f) != 0)
824 block_input ();
825 XSetBackground (FRAME_X_DISPLAY (f), x->cursor_gc, x->cursor_pixel);
826 XSetForeground (FRAME_X_DISPLAY (f), x->cursor_gc, fore_pixel);
827 unblock_input ();
829 if (FRAME_VISIBLE_P (f))
831 x_update_cursor (f, false);
832 x_update_cursor (f, true);
836 update_face_from_frame_parameter (f, Qcursor_color, arg);
839 /* Set the border-color of frame F to pixel value PIX.
840 Note that this does not fully take effect if done before
841 F has an x-window. */
843 static void
844 x_set_border_pixel (struct frame *f, int pix)
846 unload_color (f, f->output_data.x->border_pixel);
847 f->output_data.x->border_pixel = pix;
849 if (FRAME_X_WINDOW (f) != 0 && f->border_width > 0)
851 block_input ();
852 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), pix);
853 unblock_input ();
855 if (FRAME_VISIBLE_P (f))
856 redraw_frame (f);
860 /* Set the border-color of frame F to value described by ARG.
861 ARG can be a string naming a color.
862 The border-color is used for the border that is drawn by the X server.
863 Note that this does not fully take effect if done before
864 F has an x-window; it must be redone when the window is created.
866 Note: this is done in two routines because of the way X10 works.
868 Note: under X11, this is normally the province of the window manager,
869 and so emacs's border colors may be overridden. */
871 static void
872 x_set_border_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
874 int pix;
876 CHECK_STRING (arg);
877 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
878 x_set_border_pixel (f, pix);
879 update_face_from_frame_parameter (f, Qborder_color, arg);
883 static void
884 x_set_cursor_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
886 set_frame_cursor_types (f, arg);
889 static void
890 x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
892 bool result;
894 if (STRINGP (arg))
896 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
897 return;
899 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
900 return;
902 block_input ();
903 if (NILP (arg))
904 result = x_text_icon (f,
905 SSDATA ((!NILP (f->icon_name)
906 ? f->icon_name
907 : f->name)));
908 else
909 result = x_bitmap_icon (f, arg);
911 if (result)
913 unblock_input ();
914 error ("No icon window available");
917 XFlush (FRAME_X_DISPLAY (f));
918 unblock_input ();
921 static void
922 x_set_icon_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
924 bool result;
926 if (STRINGP (arg))
928 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
929 return;
931 else if (!NILP (arg) || NILP (oldval))
932 return;
934 fset_icon_name (f, arg);
936 if (f->output_data.x->icon_bitmap != 0)
937 return;
939 block_input ();
941 result = x_text_icon (f,
942 SSDATA ((!NILP (f->icon_name)
943 ? f->icon_name
944 : !NILP (f->title)
945 ? f->title
946 : f->name)));
948 if (result)
950 unblock_input ();
951 error ("No icon window available");
954 XFlush (FRAME_X_DISPLAY (f));
955 unblock_input ();
959 static void
960 x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
962 int nlines;
963 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
964 int olines = FRAME_MENU_BAR_LINES (f);
965 #endif
967 /* Right now, menu bars don't work properly in minibuf-only frames;
968 most of the commands try to apply themselves to the minibuffer
969 frame itself, and get an error because you can't switch buffers
970 in or split the minibuffer window. */
971 if (FRAME_MINIBUF_ONLY_P (f))
972 return;
974 if (TYPE_RANGED_INTEGERP (int, value))
975 nlines = XINT (value);
976 else
977 nlines = 0;
979 /* Make sure we redisplay all windows in this frame. */
980 windows_or_buffers_changed = 59;
982 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
983 FRAME_MENU_BAR_LINES (f) = 0;
984 FRAME_MENU_BAR_HEIGHT (f) = 0;
985 if (nlines)
987 FRAME_EXTERNAL_MENU_BAR (f) = 1;
988 if (FRAME_X_P (f) && f->output_data.x->menubar_widget == 0)
989 /* Make sure next redisplay shows the menu bar. */
990 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = true;
992 else
994 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
995 free_frame_menubar (f);
996 FRAME_EXTERNAL_MENU_BAR (f) = 0;
997 if (FRAME_X_P (f))
998 f->output_data.x->menubar_widget = 0;
1000 #else /* not USE_X_TOOLKIT && not USE_GTK */
1001 FRAME_MENU_BAR_LINES (f) = nlines;
1002 FRAME_MENU_BAR_HEIGHT (f) = nlines * FRAME_LINE_HEIGHT (f);
1003 adjust_frame_size (f, -1, -1, 2, true, Qmenu_bar_lines);
1004 if (FRAME_X_WINDOW (f))
1005 x_clear_under_internal_border (f);
1007 /* If the menu bar height gets changed, the internal border below
1008 the top margin has to be cleared. Also, if the menu bar gets
1009 larger, the area for the added lines has to be cleared except for
1010 the first menu bar line that is to be drawn later. */
1011 if (nlines != olines)
1013 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1014 int width = FRAME_PIXEL_WIDTH (f);
1015 int y;
1017 /* height can be zero here. */
1018 if (FRAME_X_WINDOW (f) && height > 0 && width > 0)
1020 y = FRAME_TOP_MARGIN_HEIGHT (f);
1022 block_input ();
1023 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1024 0, y, width, height);
1025 unblock_input ();
1028 if (nlines > 1 && nlines > olines)
1030 y = (olines == 0 ? 1 : olines) * FRAME_LINE_HEIGHT (f);
1031 height = nlines * FRAME_LINE_HEIGHT (f) - y;
1033 block_input ();
1034 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1035 0, y, width, height);
1036 unblock_input ();
1039 if (nlines == 0 && WINDOWP (f->menu_bar_window))
1040 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
1042 #endif /* not USE_X_TOOLKIT && not USE_GTK */
1043 adjust_frame_glyphs (f);
1044 run_window_configuration_change_hook (f);
1048 /* Set the number of lines used for the tool bar of frame F to VALUE.
1049 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1050 is the old number of tool bar lines. This function changes the
1051 height of all windows on frame F to match the new tool bar height.
1052 The frame's height doesn't change. */
1054 static void
1055 x_set_tool_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1057 int nlines;
1059 /* Treat tool bars like menu bars. */
1060 if (FRAME_MINIBUF_ONLY_P (f))
1061 return;
1063 /* Use VALUE only if an int >= 0. */
1064 if (RANGED_INTEGERP (0, value, INT_MAX))
1065 nlines = XFASTINT (value);
1066 else
1067 nlines = 0;
1069 x_change_tool_bar_height (f, nlines * FRAME_LINE_HEIGHT (f));
1073 /* Set the pixel height of the tool bar of frame F to HEIGHT. */
1074 void
1075 x_change_tool_bar_height (struct frame *f, int height)
1077 #ifdef USE_GTK
1078 FRAME_TOOL_BAR_LINES (f) = 0;
1079 FRAME_TOOL_BAR_HEIGHT (f) = 0;
1080 if (height)
1082 FRAME_EXTERNAL_TOOL_BAR (f) = true;
1083 if (FRAME_X_P (f) && f->output_data.x->toolbar_widget == 0)
1084 /* Make sure next redisplay shows the tool bar. */
1085 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = true;
1086 update_frame_tool_bar (f);
1088 else
1090 if (FRAME_EXTERNAL_TOOL_BAR (f))
1091 free_frame_tool_bar (f);
1092 FRAME_EXTERNAL_TOOL_BAR (f) = false;
1094 #else /* !USE_GTK */
1095 int unit = FRAME_LINE_HEIGHT (f);
1096 int old_height = FRAME_TOOL_BAR_HEIGHT (f);
1097 int lines = (height + unit - 1) / unit;
1098 Lisp_Object fullscreen;
1100 /* Make sure we redisplay all windows in this frame. */
1101 windows_or_buffers_changed = 60;
1104 /* Recalculate tool bar and frame text sizes. */
1105 FRAME_TOOL_BAR_HEIGHT (f) = height;
1106 FRAME_TOOL_BAR_LINES (f) = lines;
1107 /* Store the `tool-bar-lines' and `height' frame parameters. */
1108 store_frame_param (f, Qtool_bar_lines, make_number (lines));
1109 store_frame_param (f, Qheight, make_number (FRAME_LINES (f)));
1111 /* We also have to make sure that the internal border at the top of
1112 the frame, below the menu bar or tool bar, is redrawn when the
1113 tool bar disappears. This is so because the internal border is
1114 below the tool bar if one is displayed, but is below the menu bar
1115 if there isn't a tool bar. The tool bar draws into the area
1116 below the menu bar. */
1117 if (FRAME_X_WINDOW (f) && FRAME_TOOL_BAR_HEIGHT (f) == 0)
1119 clear_frame (f);
1120 clear_current_matrices (f);
1123 if ((height < old_height) && WINDOWP (f->tool_bar_window))
1124 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1126 /* Recalculate toolbar height. */
1127 f->n_tool_bar_rows = 0;
1129 adjust_frame_size (f, -1, -1,
1130 ((!f->tool_bar_redisplayed_once
1131 && (NILP (fullscreen =
1132 get_frame_param (f, Qfullscreen))
1133 || EQ (fullscreen, Qfullwidth))) ? 1
1134 : (old_height == 0 || height == 0) ? 2
1135 : 4),
1136 false, Qtool_bar_lines);
1138 /* adjust_frame_size might not have done anything, garbage frame
1139 here. */
1140 adjust_frame_glyphs (f);
1141 SET_FRAME_GARBAGED (f);
1142 if (FRAME_X_WINDOW (f))
1143 x_clear_under_internal_border (f);
1145 #endif /* USE_GTK */
1149 static void
1150 x_set_internal_border_width (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1152 int border;
1154 CHECK_TYPE_RANGED_INTEGER (int, arg);
1155 border = max (XINT (arg), 0);
1157 if (border != FRAME_INTERNAL_BORDER_WIDTH (f))
1159 FRAME_INTERNAL_BORDER_WIDTH (f) = border;
1161 #ifdef USE_X_TOOLKIT
1162 if (FRAME_X_OUTPUT (f)->edit_widget)
1163 widget_store_internal_border (FRAME_X_OUTPUT (f)->edit_widget);
1164 #endif
1166 if (FRAME_X_WINDOW (f) != 0)
1168 adjust_frame_size (f, -1, -1, 3, false, Qinternal_border_width);
1170 #ifdef USE_GTK
1171 xg_clear_under_internal_border (f);
1172 #else
1173 x_clear_under_internal_border (f);
1174 #endif
1181 /* Set the foreground color for scroll bars on frame F to VALUE.
1182 VALUE should be a string, a color name. If it isn't a string or
1183 isn't a valid color name, do nothing. OLDVAL is the old value of
1184 the frame parameter. */
1186 static void
1187 x_set_scroll_bar_foreground (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1189 unsigned long pixel;
1191 if (STRINGP (value))
1192 pixel = x_decode_color (f, value, BLACK_PIX_DEFAULT (f));
1193 else
1194 pixel = -1;
1196 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
1197 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
1199 f->output_data.x->scroll_bar_foreground_pixel = pixel;
1200 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1202 /* Remove all scroll bars because they have wrong colors. */
1203 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1204 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1205 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1206 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1208 update_face_from_frame_parameter (f, Qscroll_bar_foreground, value);
1209 redraw_frame (f);
1214 /* Set the background color for scroll bars on frame F to VALUE VALUE
1215 should be a string, a color name. If it isn't a string or isn't a
1216 valid color name, do nothing. OLDVAL is the old value of the frame
1217 parameter. */
1219 static void
1220 x_set_scroll_bar_background (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1222 unsigned long pixel;
1224 if (STRINGP (value))
1225 pixel = x_decode_color (f, value, WHITE_PIX_DEFAULT (f));
1226 else
1227 pixel = -1;
1229 if (f->output_data.x->scroll_bar_background_pixel != -1)
1230 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
1232 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
1233 /* Scrollbar shadow colors. */
1234 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
1236 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
1237 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
1239 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
1241 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
1242 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
1244 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
1246 f->output_data.x->scroll_bar_background_pixel = pixel;
1247 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1249 /* Remove all scroll bars because they have wrong colors. */
1250 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1251 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1252 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1253 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1255 update_face_from_frame_parameter (f, Qscroll_bar_background, value);
1256 redraw_frame (f);
1261 /* Encode Lisp string STRING as a text in a format appropriate for
1262 XICCC (X Inter Client Communication Conventions).
1264 This can call Lisp code, so callers must GCPRO.
1266 If STRING contains only ASCII characters, do no conversion and
1267 return the string data of STRING. Otherwise, encode the text by
1268 CODING_SYSTEM, and return a newly allocated memory area which
1269 should be freed by `xfree' by a caller.
1271 Store the byte length of resulting text in *TEXT_BYTES.
1273 If the text contains only ASCII and Latin-1, store true in *STRING_P,
1274 which means that the `encoding' of the result can be `STRING'.
1275 Otherwise store false in *STRINGP, which means that the `encoding' of
1276 the result should be `COMPOUND_TEXT'. */
1278 static unsigned char *
1279 x_encode_text (Lisp_Object string, Lisp_Object coding_system,
1280 ptrdiff_t *text_bytes, bool *stringp, bool *freep)
1282 int result = string_xstring_p (string);
1283 struct coding_system coding;
1285 if (result == 0)
1287 /* No multibyte character in OBJ. We need not encode it. */
1288 *text_bytes = SBYTES (string);
1289 *stringp = true;
1290 *freep = false;
1291 return SDATA (string);
1294 setup_coding_system (coding_system, &coding);
1295 coding.mode |= (CODING_MODE_SAFE_ENCODING | CODING_MODE_LAST_BLOCK);
1296 /* We suppress producing escape sequences for composition. */
1297 coding.common_flags &= ~CODING_ANNOTATION_MASK;
1298 coding.destination = xnmalloc (SCHARS (string), 2);
1299 coding.dst_bytes = SCHARS (string) * 2;
1300 encode_coding_object (&coding, string, 0, 0,
1301 SCHARS (string), SBYTES (string), Qnil);
1302 *text_bytes = coding.produced;
1303 *stringp = (result == 1 || !EQ (coding_system, Qcompound_text));
1304 *freep = true;
1305 return coding.destination;
1309 /* Set the WM name to NAME for frame F. Also set the icon name.
1310 If the frame already has an icon name, use that, otherwise set the
1311 icon name to NAME. */
1313 static void
1314 x_set_name_internal (struct frame *f, Lisp_Object name)
1316 if (FRAME_X_WINDOW (f))
1318 block_input ();
1320 XTextProperty text, icon;
1321 ptrdiff_t bytes;
1322 bool stringp;
1323 bool do_free_icon_value = false, do_free_text_value = false;
1324 Lisp_Object coding_system;
1325 Lisp_Object encoded_name;
1326 Lisp_Object encoded_icon_name;
1327 struct gcpro gcpro1;
1329 /* As ENCODE_UTF_8 may cause GC and relocation of string data,
1330 we use it before x_encode_text that may return string data. */
1331 GCPRO1 (name);
1332 encoded_name = ENCODE_UTF_8 (name);
1333 UNGCPRO;
1335 coding_system = Qcompound_text;
1336 /* Note: Encoding strategy
1338 We encode NAME by compound-text and use "COMPOUND-TEXT" in
1339 text.encoding. But, there are non-internationalized window
1340 managers which don't support that encoding. So, if NAME
1341 contains only ASCII and 8859-1 characters, encode it by
1342 iso-latin-1, and use "STRING" in text.encoding hoping that
1343 such window managers at least analyze this format correctly,
1344 i.e. treat 8-bit bytes as 8859-1 characters.
1346 We may also be able to use "UTF8_STRING" in text.encoding
1347 in the future which can encode all Unicode characters.
1348 But, for the moment, there's no way to know that the
1349 current window manager supports it or not.
1351 Either way, we also set the _NET_WM_NAME and _NET_WM_ICON_NAME
1352 properties. Per the EWMH specification, those two properties
1353 are always UTF8_STRING. This matches what gtk_window_set_title()
1354 does in the USE_GTK case. */
1355 text.value = x_encode_text (name, coding_system, &bytes,
1356 &stringp, &do_free_text_value);
1357 text.encoding = (stringp ? XA_STRING
1358 : FRAME_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1359 text.format = 8;
1360 text.nitems = bytes;
1362 if (!STRINGP (f->icon_name))
1364 icon = text;
1365 encoded_icon_name = encoded_name;
1367 else
1369 /* See the above comment "Note: Encoding strategy". */
1370 icon.value = x_encode_text (f->icon_name, coding_system, &bytes,
1371 &stringp, &do_free_icon_value);
1372 icon.encoding = (stringp ? XA_STRING
1373 : FRAME_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1374 icon.format = 8;
1375 icon.nitems = bytes;
1377 encoded_icon_name = ENCODE_UTF_8 (f->icon_name);
1380 #ifdef USE_GTK
1381 gtk_window_set_title (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1382 SSDATA (encoded_name));
1383 #else /* not USE_GTK */
1384 XSetWMName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
1385 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1386 FRAME_DISPLAY_INFO (f)->Xatom_net_wm_name,
1387 FRAME_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1388 8, PropModeReplace,
1389 SDATA (encoded_name),
1390 SBYTES (encoded_name));
1391 #endif /* not USE_GTK */
1393 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &icon);
1394 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1395 FRAME_DISPLAY_INFO (f)->Xatom_net_wm_icon_name,
1396 FRAME_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1397 8, PropModeReplace,
1398 SDATA (encoded_icon_name),
1399 SBYTES (encoded_icon_name));
1401 if (do_free_icon_value)
1402 xfree (icon.value);
1403 if (do_free_text_value)
1404 xfree (text.value);
1406 unblock_input ();
1410 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1411 x_id_name.
1413 If EXPLICIT is true, that indicates that lisp code is setting the
1414 name; if NAME is a string, set F's name to NAME and set
1415 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1417 If EXPLICIT is false, that indicates that Emacs redisplay code is
1418 suggesting a new name, which lisp code should override; if
1419 F->explicit_name is set, ignore the new name; otherwise, set it. */
1421 static void
1422 x_set_name (struct frame *f, Lisp_Object name, bool explicit)
1424 /* Make sure that requests from lisp code override requests from
1425 Emacs redisplay code. */
1426 if (explicit)
1428 /* If we're switching from explicit to implicit, we had better
1429 update the mode lines and thereby update the title. */
1430 if (f->explicit_name && NILP (name))
1431 update_mode_lines = 37;
1433 f->explicit_name = ! NILP (name);
1435 else if (f->explicit_name)
1436 return;
1438 /* If NAME is nil, set the name to the x_id_name. */
1439 if (NILP (name))
1441 /* Check for no change needed in this very common case
1442 before we do any consing. */
1443 if (!strcmp (FRAME_DISPLAY_INFO (f)->x_id_name,
1444 SSDATA (f->name)))
1445 return;
1446 name = build_string (FRAME_DISPLAY_INFO (f)->x_id_name);
1448 else
1449 CHECK_STRING (name);
1451 /* Don't change the name if it's already NAME. */
1452 if (! NILP (Fstring_equal (name, f->name)))
1453 return;
1455 fset_name (f, name);
1457 /* For setting the frame title, the title parameter should override
1458 the name parameter. */
1459 if (! NILP (f->title))
1460 name = f->title;
1462 x_set_name_internal (f, name);
1465 /* This function should be called when the user's lisp code has
1466 specified a name for the frame; the name will override any set by the
1467 redisplay code. */
1468 static void
1469 x_explicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1471 x_set_name (f, arg, true);
1474 /* This function should be called by Emacs redisplay code to set the
1475 name; names set this way will never override names set by the user's
1476 lisp code. */
1477 void
1478 x_implicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1480 x_set_name (f, arg, false);
1483 /* Change the title of frame F to NAME.
1484 If NAME is nil, use the frame name as the title. */
1486 static void
1487 x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
1489 /* Don't change the title if it's already NAME. */
1490 if (EQ (name, f->title))
1491 return;
1493 update_mode_lines = 38;
1495 fset_title (f, name);
1497 if (NILP (name))
1498 name = f->name;
1499 else
1500 CHECK_STRING (name);
1502 x_set_name_internal (f, name);
1505 void
1506 x_set_scroll_bar_default_width (struct frame *f)
1508 int unit = FRAME_COLUMN_WIDTH (f);
1509 #ifdef USE_TOOLKIT_SCROLL_BARS
1510 #ifdef USE_GTK
1511 int minw = xg_get_default_scrollbar_width ();
1512 #else
1513 int minw = 16;
1514 #endif
1515 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
1516 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (minw + unit - 1) / unit;
1517 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = minw;
1518 #else
1519 /* The width of a non-toolkit scrollbar is 14 pixels. */
1520 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
1521 FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
1522 = FRAME_CONFIG_SCROLL_BAR_COLS (f) * unit;
1523 #endif
1526 void
1527 x_set_scroll_bar_default_height (struct frame *f)
1529 int height = FRAME_LINE_HEIGHT (f);
1530 #ifdef USE_TOOLKIT_SCROLL_BARS
1531 #ifdef USE_GTK
1532 int min_height = xg_get_default_scrollbar_height ();
1533 #else
1534 int min_height = 16;
1535 #endif
1536 /* A minimum height of 14 doesn't look good for toolkit scroll bars. */
1537 FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) = min_height;
1538 FRAME_CONFIG_SCROLL_BAR_LINES (f) = (min_height + height - 1) / height;
1539 #else
1540 /* The height of a non-toolkit scrollbar is 14 pixels. */
1541 FRAME_CONFIG_SCROLL_BAR_LINES (f) = (14 + height - 1) / height;
1543 /* Use all of that space (aside from required margins) for the
1544 scroll bar. */
1545 FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) = 14;
1546 #endif
1550 /* Record in frame F the specified or default value according to ALIST
1551 of the parameter named PROP (a Lisp symbol). If no value is
1552 specified for PROP, look for an X default for XPROP on the frame
1553 named NAME. If that is not found either, use the value DEFLT. */
1555 static Lisp_Object
1556 x_default_scroll_bar_color_parameter (struct frame *f,
1557 Lisp_Object alist, Lisp_Object prop,
1558 const char *xprop, const char *xclass,
1559 bool foreground_p)
1561 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1562 Lisp_Object tem;
1564 tem = x_get_arg (dpyinfo, alist, prop, xprop, xclass, RES_TYPE_STRING);
1565 if (EQ (tem, Qunbound))
1567 #ifdef USE_TOOLKIT_SCROLL_BARS
1569 /* See if an X resource for the scroll bar color has been
1570 specified. */
1571 AUTO_STRING (foreground, "foreground");
1572 AUTO_STRING (background, "foreground");
1573 AUTO_STRING (verticalScrollBar, "verticalScrollBar");
1574 tem = (display_x_get_resource
1575 (dpyinfo, foreground_p ? foreground : background,
1576 empty_unibyte_string,
1577 verticalScrollBar,
1578 empty_unibyte_string));
1579 if (!STRINGP (tem))
1581 /* If nothing has been specified, scroll bars will use a
1582 toolkit-dependent default. Because these defaults are
1583 difficult to get at without actually creating a scroll
1584 bar, use nil to indicate that no color has been
1585 specified. */
1586 tem = Qnil;
1589 #else /* not USE_TOOLKIT_SCROLL_BARS */
1591 tem = Qnil;
1593 #endif /* not USE_TOOLKIT_SCROLL_BARS */
1596 AUTO_FRAME_ARG (arg, prop, tem);
1597 x_set_frame_parameters (f, arg);
1598 return tem;
1604 #ifdef USE_X_TOOLKIT
1606 /* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
1607 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
1608 already be present because of the toolkit (Motif adds some of them,
1609 for example, but Xt doesn't). */
1611 static void
1612 hack_wm_protocols (struct frame *f, Widget widget)
1614 Display *dpy = XtDisplay (widget);
1615 Window w = XtWindow (widget);
1616 bool need_delete = true;
1617 bool need_focus = true;
1618 bool need_save = true;
1620 block_input ();
1622 Atom type;
1623 unsigned char *catoms;
1624 int format = 0;
1625 unsigned long nitems = 0;
1626 unsigned long bytes_after;
1628 if ((XGetWindowProperty (dpy, w,
1629 FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
1630 0, 100, False, XA_ATOM,
1631 &type, &format, &nitems, &bytes_after,
1632 &catoms)
1633 == Success)
1634 && format == 32 && type == XA_ATOM)
1636 Atom *atoms = (Atom *) catoms;
1637 while (nitems > 0)
1639 nitems--;
1640 if (atoms[nitems]
1641 == FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window)
1642 need_delete = false;
1643 else if (atoms[nitems]
1644 == FRAME_DISPLAY_INFO (f)->Xatom_wm_take_focus)
1645 need_focus = false;
1646 else if (atoms[nitems]
1647 == FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself)
1648 need_save = false;
1651 if (catoms)
1652 XFree (catoms);
1655 Atom props[10];
1656 int count = 0;
1657 if (need_delete)
1658 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window;
1659 if (need_focus)
1660 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_take_focus;
1661 if (need_save)
1662 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
1663 if (count)
1664 XChangeProperty (dpy, w, FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
1665 XA_ATOM, 32, PropModeAppend,
1666 (unsigned char *) props, count);
1668 unblock_input ();
1670 #endif
1674 /* Support routines for XIC (X Input Context). */
1676 #ifdef HAVE_X_I18N
1678 static XFontSet xic_create_xfontset (struct frame *);
1679 static XIMStyle best_xim_style (XIMStyles *);
1682 /* Supported XIM styles, ordered by preference. */
1684 static const XIMStyle supported_xim_styles[] =
1686 XIMPreeditPosition | XIMStatusArea,
1687 XIMPreeditPosition | XIMStatusNothing,
1688 XIMPreeditPosition | XIMStatusNone,
1689 XIMPreeditNothing | XIMStatusArea,
1690 XIMPreeditNothing | XIMStatusNothing,
1691 XIMPreeditNothing | XIMStatusNone,
1692 XIMPreeditNone | XIMStatusArea,
1693 XIMPreeditNone | XIMStatusNothing,
1694 XIMPreeditNone | XIMStatusNone,
1699 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
1700 /* Create an X fontset on frame F with base font name BASE_FONTNAME. */
1702 static const char xic_default_fontset[] = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
1704 /* Create an Xt fontset spec from the name of a base font.
1705 If `motif' is True use the Motif syntax. */
1706 char *
1707 xic_create_fontsetname (const char *base_fontname, bool motif)
1709 const char *sep = motif ? ";" : ",";
1710 char *fontsetname;
1711 char *z;
1713 /* Make a fontset name from the base font name. */
1714 if (xic_default_fontset == base_fontname)
1716 /* There is no base font name, use the default. */
1717 fontsetname = xmalloc (strlen (base_fontname) + 2);
1718 z = stpcpy (fontsetname, base_fontname);
1720 else
1722 /* Make a fontset name from the base font name.
1723 The font set will be made of the following elements:
1724 - the base font.
1725 - the base font where the charset spec is replaced by -*-*.
1726 - the same but with the family also replaced with -*-*-. */
1727 const char *p = base_fontname;
1728 ptrdiff_t i;
1730 for (i = 0; *p; p++)
1731 if (*p == '-') i++;
1732 if (i != 14)
1734 /* As the font name doesn't conform to XLFD, we can't
1735 modify it to generalize it to allcs and allfamilies.
1736 Use the specified font plus the default. */
1737 fontsetname = xmalloc (strlen (base_fontname)
1738 + strlen (xic_default_fontset) + 3);
1739 z = stpcpy (fontsetname, base_fontname);
1740 z = stpcpy (z, sep);
1741 z = stpcpy (z, xic_default_fontset);
1743 else
1745 ptrdiff_t len;
1746 const char *p1 = NULL, *p2 = NULL, *p3 = NULL;
1747 char *font_allcs = NULL;
1748 char *font_allfamilies = NULL;
1749 char *font_all = NULL;
1750 const char *allcs = "*-*-*-*-*-*-*";
1751 const char *allfamilies = "-*-*-";
1752 const char *all = "*-*-*-*-";
1753 char *base;
1755 for (i = 0, p = base_fontname; i < 8; p++)
1757 if (*p == '-')
1759 i++;
1760 if (i == 3)
1761 p1 = p + 1;
1762 else if (i == 7)
1763 p2 = p + 1;
1764 else if (i == 6)
1765 p3 = p + 1;
1768 /* If base_fontname specifies ADSTYLE, make it a
1769 wildcard. */
1770 if (*p3 != '*')
1772 ptrdiff_t diff = (p2 - p3) - 2;
1774 base = alloca (strlen (base_fontname) + 1);
1775 memcpy (base, base_fontname, p3 - base_fontname);
1776 base[p3 - base_fontname] = '*';
1777 base[(p3 - base_fontname) + 1] = '-';
1778 strcpy (base + (p3 - base_fontname) + 2, p2);
1779 p = base + (p - base_fontname) - diff;
1780 p1 = base + (p1 - base_fontname);
1781 p2 = base + (p2 - base_fontname) - diff;
1782 base_fontname = base;
1785 /* Build the font spec that matches all charsets. */
1786 len = p - base_fontname + strlen (allcs) + 1;
1787 font_allcs = alloca (len);
1788 memcpy (font_allcs, base_fontname, p - base_fontname);
1789 strcpy (font_allcs + (p - base_fontname), allcs);
1791 /* Build the font spec that matches all families and
1792 add-styles. */
1793 len = p - p1 + strlen (allcs) + strlen (allfamilies) + 1;
1794 font_allfamilies = alloca (len);
1795 strcpy (font_allfamilies, allfamilies);
1796 memcpy (font_allfamilies + strlen (allfamilies), p1, p - p1);
1797 strcpy (font_allfamilies + strlen (allfamilies) + (p - p1), allcs);
1799 /* Build the font spec that matches all. */
1800 len = p - p2 + strlen (allcs) + strlen (all) + strlen (allfamilies) + 1;
1801 font_all = alloca (len);
1802 z = stpcpy (font_all, allfamilies);
1803 z = stpcpy (z, all);
1804 memcpy (z, p2, p - p2);
1805 strcpy (z + (p - p2), allcs);
1807 /* Build the actual font set name. */
1808 len = strlen (base_fontname) + strlen (font_allcs)
1809 + strlen (font_allfamilies) + strlen (font_all) + 5;
1810 fontsetname = xmalloc (len);
1811 z = stpcpy (fontsetname, base_fontname);
1812 z = stpcpy (z, sep);
1813 z = stpcpy (z, font_allcs);
1814 z = stpcpy (z, sep);
1815 z = stpcpy (z, font_allfamilies);
1816 z = stpcpy (z, sep);
1817 z = stpcpy (z, font_all);
1820 if (motif)
1821 strcpy (z, ":");
1822 return fontsetname;
1824 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
1826 #ifdef DEBUG_XIC_FONTSET
1827 static void
1828 print_fontset_result (XFontSet xfs, char *name, char **missing_list,
1829 int missing_count)
1831 if (xfs)
1832 fprintf (stderr, "XIC Fontset created: %s\n", name);
1833 else
1835 fprintf (stderr, "XIC Fontset failed: %s\n", name);
1836 while (missing_count-- > 0)
1838 fprintf (stderr, " missing: %s\n", *missing_list);
1839 missing_list++;
1844 #endif
1846 static XFontSet
1847 xic_create_xfontset (struct frame *f)
1849 XFontSet xfs = NULL;
1850 struct font *font = FRAME_FONT (f);
1851 int pixel_size = font->pixel_size;
1852 Lisp_Object rest, frame;
1854 /* See if there is another frame already using same fontset. */
1855 FOR_EACH_FRAME (rest, frame)
1857 struct frame *cf = XFRAME (frame);
1859 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
1860 && FRAME_DISPLAY_INFO (cf) == FRAME_DISPLAY_INFO (f)
1861 && FRAME_FONT (f)
1862 && FRAME_FONT (f)->pixel_size == pixel_size)
1864 xfs = FRAME_XIC_FONTSET (cf);
1865 break;
1869 if (! xfs)
1871 char buf[256];
1872 char **missing_list;
1873 int missing_count;
1874 char *def_string;
1875 const char *xlfd_format = "-*-*-medium-r-normal--%d-*-*-*-*-*";
1877 sprintf (buf, xlfd_format, pixel_size);
1878 missing_list = NULL;
1879 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
1880 &missing_list, &missing_count, &def_string);
1881 #ifdef DEBUG_XIC_FONTSET
1882 print_fontset_result (xfs, buf, missing_list, missing_count);
1883 #endif
1884 if (missing_list)
1885 XFreeStringList (missing_list);
1886 if (! xfs)
1888 /* List of pixel sizes most likely available. Find one that
1889 is closest to pixel_size. */
1890 int sizes[] = {0, 8, 10, 11, 12, 14, 17, 18, 20, 24, 26, 34, 0};
1891 int *smaller, *larger;
1893 for (smaller = sizes; smaller[1]; smaller++)
1894 if (smaller[1] >= pixel_size)
1895 break;
1896 larger = smaller + 1;
1897 if (*larger == pixel_size)
1898 larger++;
1899 while (*smaller || *larger)
1901 int this_size;
1903 if (! *larger)
1904 this_size = *smaller--;
1905 else if (! *smaller)
1906 this_size = *larger++;
1907 else if (pixel_size - *smaller < *larger - pixel_size)
1908 this_size = *smaller--;
1909 else
1910 this_size = *larger++;
1911 sprintf (buf, xlfd_format, this_size);
1912 missing_list = NULL;
1913 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
1914 &missing_list, &missing_count, &def_string);
1915 #ifdef DEBUG_XIC_FONTSET
1916 print_fontset_result (xfs, buf, missing_list, missing_count);
1917 #endif
1918 if (missing_list)
1919 XFreeStringList (missing_list);
1920 if (xfs)
1921 break;
1924 if (! xfs)
1926 const char *last_resort = "-*-*-*-r-normal--*-*-*-*-*-*";
1928 missing_list = NULL;
1929 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), last_resort,
1930 &missing_list, &missing_count, &def_string);
1931 #ifdef DEBUG_XIC_FONTSET
1932 print_fontset_result (xfs, last_resort, missing_list, missing_count);
1933 #endif
1934 if (missing_list)
1935 XFreeStringList (missing_list);
1940 return xfs;
1943 /* Free the X fontset of frame F if it is the last frame using it. */
1945 void
1946 xic_free_xfontset (struct frame *f)
1948 Lisp_Object rest, frame;
1949 bool shared_p = false;
1951 if (!FRAME_XIC_FONTSET (f))
1952 return;
1954 /* See if there is another frame sharing the same fontset. */
1955 FOR_EACH_FRAME (rest, frame)
1957 struct frame *cf = XFRAME (frame);
1958 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
1959 && FRAME_DISPLAY_INFO (cf) == FRAME_DISPLAY_INFO (f)
1960 && FRAME_XIC_FONTSET (cf) == FRAME_XIC_FONTSET (f))
1962 shared_p = true;
1963 break;
1967 if (!shared_p)
1968 /* The fontset is not used anymore. It is safe to free it. */
1969 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
1971 FRAME_XIC_FONTSET (f) = NULL;
1975 /* Value is the best input style, given user preferences USER (already
1976 checked to be supported by Emacs), and styles supported by the
1977 input method XIM. */
1979 static XIMStyle
1980 best_xim_style (XIMStyles *xim)
1982 int i, j;
1983 int nr_supported = ARRAYELTS (supported_xim_styles);
1985 for (i = 0; i < nr_supported; ++i)
1986 for (j = 0; j < xim->count_styles; ++j)
1987 if (supported_xim_styles[i] == xim->supported_styles[j])
1988 return supported_xim_styles[i];
1990 /* Return the default style. */
1991 return XIMPreeditNothing | XIMStatusNothing;
1994 /* Create XIC for frame F. */
1996 void
1997 create_frame_xic (struct frame *f)
1999 XIM xim;
2000 XIC xic = NULL;
2001 XFontSet xfs = NULL;
2002 XVaNestedList status_attr = NULL;
2003 XVaNestedList preedit_attr = NULL;
2004 XRectangle s_area;
2005 XPoint spot;
2006 XIMStyle xic_style;
2008 if (FRAME_XIC (f))
2009 goto out;
2011 xim = FRAME_X_XIM (f);
2012 if (!xim)
2013 goto out;
2015 /* Determine XIC style. */
2016 xic_style = best_xim_style (FRAME_X_XIM_STYLES (f));
2018 /* Create X fontset. */
2019 if (xic_style & (XIMPreeditPosition | XIMStatusArea))
2021 xfs = xic_create_xfontset (f);
2022 if (!xfs)
2023 goto out;
2025 FRAME_XIC_FONTSET (f) = xfs;
2028 if (xic_style & XIMPreeditPosition)
2030 spot.x = 0; spot.y = 1;
2031 preedit_attr = XVaCreateNestedList (0,
2032 XNFontSet, xfs,
2033 XNForeground,
2034 FRAME_FOREGROUND_PIXEL (f),
2035 XNBackground,
2036 FRAME_BACKGROUND_PIXEL (f),
2037 (xic_style & XIMPreeditPosition
2038 ? XNSpotLocation
2039 : NULL),
2040 &spot,
2041 NULL);
2043 if (!preedit_attr)
2044 goto out;
2047 if (xic_style & XIMStatusArea)
2049 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
2050 status_attr = XVaCreateNestedList (0,
2051 XNArea,
2052 &s_area,
2053 XNFontSet,
2054 xfs,
2055 XNForeground,
2056 FRAME_FOREGROUND_PIXEL (f),
2057 XNBackground,
2058 FRAME_BACKGROUND_PIXEL (f),
2059 NULL);
2061 if (!status_attr)
2062 goto out;
2065 if (preedit_attr && status_attr)
2066 xic = XCreateIC (xim,
2067 XNInputStyle, xic_style,
2068 XNClientWindow, FRAME_X_WINDOW (f),
2069 XNFocusWindow, FRAME_X_WINDOW (f),
2070 XNStatusAttributes, status_attr,
2071 XNPreeditAttributes, preedit_attr,
2072 NULL);
2073 else if (preedit_attr)
2074 xic = XCreateIC (xim,
2075 XNInputStyle, xic_style,
2076 XNClientWindow, FRAME_X_WINDOW (f),
2077 XNFocusWindow, FRAME_X_WINDOW (f),
2078 XNPreeditAttributes, preedit_attr,
2079 NULL);
2080 else if (status_attr)
2081 xic = XCreateIC (xim,
2082 XNInputStyle, xic_style,
2083 XNClientWindow, FRAME_X_WINDOW (f),
2084 XNFocusWindow, FRAME_X_WINDOW (f),
2085 XNStatusAttributes, status_attr,
2086 NULL);
2087 else
2088 xic = XCreateIC (xim,
2089 XNInputStyle, xic_style,
2090 XNClientWindow, FRAME_X_WINDOW (f),
2091 XNFocusWindow, FRAME_X_WINDOW (f),
2092 NULL);
2094 if (!xic)
2095 goto out;
2097 FRAME_XIC (f) = xic;
2098 FRAME_XIC_STYLE (f) = xic_style;
2099 xfs = NULL; /* Don't free below. */
2101 out:
2103 if (xfs)
2104 free_frame_xic (f);
2106 if (preedit_attr)
2107 XFree (preedit_attr);
2109 if (status_attr)
2110 XFree (status_attr);
2114 /* Destroy XIC and free XIC fontset of frame F, if any. */
2116 void
2117 free_frame_xic (struct frame *f)
2119 if (FRAME_XIC (f) == NULL)
2120 return;
2122 XDestroyIC (FRAME_XIC (f));
2123 xic_free_xfontset (f);
2125 FRAME_XIC (f) = NULL;
2129 /* Place preedit area for XIC of window W's frame to specified
2130 pixel position X/Y. X and Y are relative to window W. */
2132 void
2133 xic_set_preeditarea (struct window *w, int x, int y)
2135 struct frame *f = XFRAME (w->frame);
2136 XVaNestedList attr;
2137 XPoint spot;
2139 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x) + WINDOW_LEFT_FRINGE_WIDTH (w);
2140 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
2141 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
2142 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2143 XFree (attr);
2147 /* Place status area for XIC in bottom right corner of frame F.. */
2149 void
2150 xic_set_statusarea (struct frame *f)
2152 XIC xic = FRAME_XIC (f);
2153 XVaNestedList attr;
2154 XRectangle area;
2155 XRectangle *needed;
2157 /* Negotiate geometry of status area. If input method has existing
2158 status area, use its current size. */
2159 area.x = area.y = area.width = area.height = 0;
2160 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
2161 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2162 XFree (attr);
2164 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
2165 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2166 XFree (attr);
2168 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
2170 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
2171 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2172 XFree (attr);
2175 area.width = needed->width;
2176 area.height = needed->height;
2177 area.x = FRAME_PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
2178 area.y = (FRAME_PIXEL_HEIGHT (f) - area.height
2179 - FRAME_MENUBAR_HEIGHT (f)
2180 - FRAME_TOOLBAR_TOP_HEIGHT (f)
2181 - FRAME_INTERNAL_BORDER_WIDTH (f));
2182 XFree (needed);
2184 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
2185 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2186 XFree (attr);
2190 /* Set X fontset for XIC of frame F, using base font name
2191 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
2193 void
2194 xic_set_xfontset (struct frame *f, const char *base_fontname)
2196 XVaNestedList attr;
2197 XFontSet xfs;
2199 xic_free_xfontset (f);
2201 xfs = xic_create_xfontset (f);
2203 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
2204 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
2205 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2206 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
2207 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
2208 XFree (attr);
2210 FRAME_XIC_FONTSET (f) = xfs;
2213 #endif /* HAVE_X_I18N */
2217 #ifdef USE_X_TOOLKIT
2219 /* Create and set up the X widget for frame F. */
2221 static void
2222 x_window (struct frame *f, long window_prompting)
2224 XClassHint class_hints;
2225 XSetWindowAttributes attributes;
2226 unsigned long attribute_mask;
2227 Widget shell_widget;
2228 Widget pane_widget;
2229 Widget frame_widget;
2230 Arg al[25];
2231 int ac;
2233 block_input ();
2235 /* Use the resource name as the top-level widget name
2236 for looking up resources. Make a non-Lisp copy
2237 for the window manager, so GC relocation won't bother it.
2239 Elsewhere we specify the window name for the window manager. */
2240 f->namebuf = xlispstrdup (Vx_resource_name);
2242 ac = 0;
2243 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
2244 XtSetArg (al[ac], XtNinput, 1); ac++;
2245 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2246 XtSetArg (al[ac], XtNborderWidth, f->border_width); ac++;
2247 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2248 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2249 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2250 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
2251 applicationShellWidgetClass,
2252 FRAME_X_DISPLAY (f), al, ac);
2254 f->output_data.x->widget = shell_widget;
2255 /* maybe_set_screen_title_format (shell_widget); */
2257 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
2258 NULL, shell_widget, False,
2259 NULL, NULL, NULL, NULL);
2261 ac = 0;
2262 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2263 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2264 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2265 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2266 XtSetValues (pane_widget, al, ac);
2267 f->output_data.x->column_widget = pane_widget;
2269 /* mappedWhenManaged to false tells to the paned window to not map/unmap
2270 the emacs screen when changing menubar. This reduces flickering. */
2272 ac = 0;
2273 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2274 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
2275 XtSetArg (al[ac], XtNallowResize, 1); ac++;
2276 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
2277 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
2278 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2279 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2280 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2281 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2282 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
2283 al, ac);
2285 f->output_data.x->edit_widget = frame_widget;
2287 XtManageChild (frame_widget);
2289 /* Do some needed geometry management. */
2291 Arg gal[3];
2292 int gac = 0;
2293 int extra_borders = 0;
2294 int menubar_size
2295 = (f->output_data.x->menubar_widget
2296 ? (f->output_data.x->menubar_widget->core.height
2297 + f->output_data.x->menubar_widget->core.border_width)
2298 : 0);
2300 #if false /* Experimentally, we now get the right results
2301 for -geometry -0-0 without this. 24 Aug 96, rms. */
2302 if (FRAME_EXTERNAL_MENU_BAR (f))
2304 Dimension ibw = 0;
2305 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
2306 menubar_size += ibw;
2308 #endif
2310 FRAME_MENUBAR_HEIGHT (f) = menubar_size;
2312 #ifndef USE_LUCID
2313 /* Motif seems to need this amount added to the sizes
2314 specified for the shell widget. The Athena/Lucid widgets don't.
2315 Both conclusions reached experimentally. -- rms. */
2316 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
2317 &extra_borders, NULL);
2318 extra_borders *= 2;
2319 #endif
2321 f->shell_position = xmalloc (sizeof "=x++" + 4 * INT_STRLEN_BOUND (int));
2323 /* Convert our geometry parameters into a geometry string
2324 and specify it.
2325 Note that we do not specify here whether the position
2326 is a user-specified or program-specified one.
2327 We pass that information later, in x_wm_set_size_hints. */
2329 int left = f->left_pos;
2330 bool xneg = (window_prompting & XNegative) != 0;
2331 int top = f->top_pos;
2332 bool yneg = (window_prompting & YNegative) != 0;
2333 if (xneg)
2334 left = -left;
2335 if (yneg)
2336 top = -top;
2338 if (window_prompting & USPosition)
2339 sprintf (f->shell_position, "=%dx%d%c%d%c%d",
2340 FRAME_PIXEL_WIDTH (f) + extra_borders,
2341 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders,
2342 (xneg ? '-' : '+'), left,
2343 (yneg ? '-' : '+'), top);
2344 else
2346 sprintf (f->shell_position, "=%dx%d",
2347 FRAME_PIXEL_WIDTH (f) + extra_borders,
2348 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders);
2350 /* Setting x and y when the position is not specified in
2351 the geometry string will set program position in the WM hints.
2352 If Emacs had just one program position, we could set it in
2353 fallback resources, but since each make-frame call can specify
2354 different program positions, this is easier. */
2355 XtSetArg (gal[gac], XtNx, left); gac++;
2356 XtSetArg (gal[gac], XtNy, top); gac++;
2360 XtSetArg (gal[gac], XtNgeometry, f->shell_position); gac++;
2361 XtSetValues (shell_widget, gal, gac);
2364 XtManageChild (pane_widget);
2365 XtRealizeWidget (shell_widget);
2367 if (FRAME_X_EMBEDDED_P (f))
2368 XReparentWindow (FRAME_X_DISPLAY (f), XtWindow (shell_widget),
2369 f->output_data.x->parent_desc, 0, 0);
2371 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
2373 validate_x_resource_name ();
2375 class_hints.res_name = SSDATA (Vx_resource_name);
2376 class_hints.res_class = SSDATA (Vx_resource_class);
2377 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
2379 #ifdef HAVE_X_I18N
2380 FRAME_XIC (f) = NULL;
2381 if (use_xim)
2382 create_frame_xic (f);
2383 #endif
2385 f->output_data.x->wm_hints.input = True;
2386 f->output_data.x->wm_hints.flags |= InputHint;
2387 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2388 &f->output_data.x->wm_hints);
2390 hack_wm_protocols (f, shell_widget);
2392 #ifdef HACK_EDITRES
2393 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
2394 #endif
2396 /* Do a stupid property change to force the server to generate a
2397 PropertyNotify event so that the event_stream server timestamp will
2398 be initialized to something relevant to the time we created the window.
2400 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
2401 FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
2402 XA_ATOM, 32, PropModeAppend, NULL, 0);
2404 /* Make all the standard events reach the Emacs frame. */
2405 attributes.event_mask = STANDARD_EVENT_SET;
2407 #ifdef HAVE_X_I18N
2408 if (FRAME_XIC (f))
2410 /* XIM server might require some X events. */
2411 unsigned long fevent = NoEventMask;
2412 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2413 attributes.event_mask |= fevent;
2415 #endif /* HAVE_X_I18N */
2417 attribute_mask = CWEventMask;
2418 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
2419 attribute_mask, &attributes);
2421 XtMapWidget (frame_widget);
2423 /* x_set_name normally ignores requests to set the name if the
2424 requested name is the same as the current name. This is the one
2425 place where that assumption isn't correct; f->name is set, but
2426 the X server hasn't been told. */
2428 Lisp_Object name;
2429 bool explicit = f->explicit_name;
2431 f->explicit_name = false;
2432 name = f->name;
2433 fset_name (f, Qnil);
2434 x_set_name (f, name, explicit);
2437 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2438 f->output_data.x->current_cursor
2439 = f->output_data.x->text_cursor);
2441 unblock_input ();
2443 /* This is a no-op, except under Motif. Make sure main areas are
2444 set to something reasonable, in case we get an error later. */
2445 lw_set_main_areas (pane_widget, 0, frame_widget);
2448 #else /* not USE_X_TOOLKIT */
2449 #ifdef USE_GTK
2450 static void
2451 x_window (struct frame *f)
2453 if (! xg_create_frame_widgets (f))
2454 error ("Unable to create window");
2456 #ifdef HAVE_X_I18N
2457 FRAME_XIC (f) = NULL;
2458 if (use_xim)
2460 block_input ();
2461 create_frame_xic (f);
2462 if (FRAME_XIC (f))
2464 /* XIM server might require some X events. */
2465 unsigned long fevent = NoEventMask;
2466 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2468 if (fevent != NoEventMask)
2470 XSetWindowAttributes attributes;
2471 XWindowAttributes wattr;
2472 unsigned long attribute_mask;
2474 XGetWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2475 &wattr);
2476 attributes.event_mask = wattr.your_event_mask | fevent;
2477 attribute_mask = CWEventMask;
2478 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2479 attribute_mask, &attributes);
2482 unblock_input ();
2484 #endif
2487 #else /*! USE_GTK */
2488 /* Create and set up the X window for frame F. */
2490 static void
2491 x_window (struct frame *f)
2493 XClassHint class_hints;
2494 XSetWindowAttributes attributes;
2495 unsigned long attribute_mask;
2497 attributes.background_pixel = FRAME_BACKGROUND_PIXEL (f);
2498 attributes.border_pixel = f->output_data.x->border_pixel;
2499 attributes.bit_gravity = StaticGravity;
2500 attributes.backing_store = NotUseful;
2501 attributes.save_under = True;
2502 attributes.event_mask = STANDARD_EVENT_SET;
2503 attributes.colormap = FRAME_X_COLORMAP (f);
2504 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
2505 | CWColormap);
2507 block_input ();
2508 FRAME_X_WINDOW (f)
2509 = XCreateWindow (FRAME_X_DISPLAY (f),
2510 f->output_data.x->parent_desc,
2511 f->left_pos,
2512 f->top_pos,
2513 FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f),
2514 f->border_width,
2515 CopyFromParent, /* depth */
2516 InputOutput, /* class */
2517 FRAME_X_VISUAL (f),
2518 attribute_mask, &attributes);
2520 #ifdef HAVE_X_I18N
2521 if (use_xim)
2523 create_frame_xic (f);
2524 if (FRAME_XIC (f))
2526 /* XIM server might require some X events. */
2527 unsigned long fevent = NoEventMask;
2528 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2529 attributes.event_mask |= fevent;
2530 attribute_mask = CWEventMask;
2531 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2532 attribute_mask, &attributes);
2535 #endif /* HAVE_X_I18N */
2537 validate_x_resource_name ();
2539 class_hints.res_name = SSDATA (Vx_resource_name);
2540 class_hints.res_class = SSDATA (Vx_resource_class);
2541 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
2543 /* This indicates that we use the "Passive Input" input model.
2544 Unless we do this, we don't get the Focus{In,Out} events that we
2545 need to draw the cursor correctly. Accursed bureaucrats.
2546 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
2548 f->output_data.x->wm_hints.input = True;
2549 f->output_data.x->wm_hints.flags |= InputHint;
2550 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2551 &f->output_data.x->wm_hints);
2552 f->output_data.x->wm_hints.icon_pixmap = None;
2554 /* Request "save yourself" and "delete window" commands from wm. */
2556 Atom protocols[2];
2557 protocols[0] = FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window;
2558 protocols[1] = FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
2559 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
2562 /* x_set_name normally ignores requests to set the name if the
2563 requested name is the same as the current name. This is the one
2564 place where that assumption isn't correct; f->name is set, but
2565 the X server hasn't been told. */
2567 Lisp_Object name;
2568 bool explicit = f->explicit_name;
2570 f->explicit_name = false;
2571 name = f->name;
2572 fset_name (f, Qnil);
2573 x_set_name (f, name, explicit);
2576 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2577 f->output_data.x->current_cursor
2578 = f->output_data.x->text_cursor);
2580 unblock_input ();
2582 if (FRAME_X_WINDOW (f) == 0)
2583 error ("Unable to create window");
2586 #endif /* not USE_GTK */
2587 #endif /* not USE_X_TOOLKIT */
2589 /* Verify that the icon position args for this window are valid. */
2591 static void
2592 x_icon_verify (struct frame *f, Lisp_Object parms)
2594 Lisp_Object icon_x, icon_y;
2596 /* Set the position of the icon. Note that twm groups all
2597 icons in an icon window. */
2598 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2599 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2600 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2602 CHECK_NUMBER (icon_x);
2603 CHECK_NUMBER (icon_y);
2605 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2606 error ("Both left and top icon corners of icon must be specified");
2609 /* Handle the icon stuff for this window. Perhaps later we might
2610 want an x_set_icon_position which can be called interactively as
2611 well. */
2613 static void
2614 x_icon (struct frame *f, Lisp_Object parms)
2616 /* Set the position of the icon. Note that twm groups all
2617 icons in an icon window. */
2618 Lisp_Object icon_x
2619 = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2620 Lisp_Object icon_y
2621 = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2622 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2624 CHECK_TYPE_RANGED_INTEGER (int, icon_x);
2625 CHECK_TYPE_RANGED_INTEGER (int, icon_y);
2627 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2628 error ("Both left and top icon corners of icon must be specified");
2630 block_input ();
2632 if (! EQ (icon_x, Qunbound))
2633 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
2635 #if false /* x_get_arg removes the visibility parameter as a side effect,
2636 but x_create_frame still needs it. */
2637 /* Start up iconic or window? */
2638 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2639 x_wm_set_window_state
2640 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
2641 Qicon)
2642 ? IconicState
2643 : NormalState));
2644 #endif
2646 x_text_icon (f, SSDATA ((!NILP (f->icon_name)
2647 ? f->icon_name
2648 : f->name)));
2650 unblock_input ();
2653 /* Make the GCs needed for this window, setting the
2654 background, border and mouse colors; also create the
2655 mouse cursor and the gray border tile. */
2657 static void
2658 x_make_gc (struct frame *f)
2660 XGCValues gc_values;
2662 block_input ();
2664 /* Create the GCs of this frame.
2665 Note that many default values are used. */
2667 gc_values.foreground = FRAME_FOREGROUND_PIXEL (f);
2668 gc_values.background = FRAME_BACKGROUND_PIXEL (f);
2669 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
2670 f->output_data.x->normal_gc
2671 = XCreateGC (FRAME_X_DISPLAY (f),
2672 FRAME_X_WINDOW (f),
2673 GCLineWidth | GCForeground | GCBackground,
2674 &gc_values);
2676 /* Reverse video style. */
2677 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2678 gc_values.background = FRAME_FOREGROUND_PIXEL (f);
2679 f->output_data.x->reverse_gc
2680 = XCreateGC (FRAME_X_DISPLAY (f),
2681 FRAME_X_WINDOW (f),
2682 GCForeground | GCBackground | GCLineWidth,
2683 &gc_values);
2685 /* Cursor has cursor-color background, background-color foreground. */
2686 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2687 gc_values.background = f->output_data.x->cursor_pixel;
2688 gc_values.fill_style = FillOpaqueStippled;
2689 f->output_data.x->cursor_gc
2690 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2691 (GCForeground | GCBackground
2692 | GCFillStyle | GCLineWidth),
2693 &gc_values);
2695 /* Create the gray border tile used when the pointer is not in
2696 the frame. Since this depends on the frame's pixel values,
2697 this must be done on a per-frame basis. */
2698 f->output_data.x->border_tile
2699 = (XCreatePixmapFromBitmapData
2700 (FRAME_X_DISPLAY (f), FRAME_DISPLAY_INFO (f)->root_window,
2701 gray_bits, gray_width, gray_height,
2702 FRAME_FOREGROUND_PIXEL (f),
2703 FRAME_BACKGROUND_PIXEL (f),
2704 DefaultDepth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f))));
2706 unblock_input ();
2710 /* Free what was allocated in x_make_gc. */
2712 void
2713 x_free_gcs (struct frame *f)
2715 Display *dpy = FRAME_X_DISPLAY (f);
2717 block_input ();
2719 if (f->output_data.x->normal_gc)
2721 XFreeGC (dpy, f->output_data.x->normal_gc);
2722 f->output_data.x->normal_gc = 0;
2725 if (f->output_data.x->reverse_gc)
2727 XFreeGC (dpy, f->output_data.x->reverse_gc);
2728 f->output_data.x->reverse_gc = 0;
2731 if (f->output_data.x->cursor_gc)
2733 XFreeGC (dpy, f->output_data.x->cursor_gc);
2734 f->output_data.x->cursor_gc = 0;
2737 if (f->output_data.x->border_tile)
2739 XFreePixmap (dpy, f->output_data.x->border_tile);
2740 f->output_data.x->border_tile = 0;
2743 unblock_input ();
2747 /* Handler for signals raised during x_create_frame and
2748 x_create_tip_frame. FRAME is the frame which is partially
2749 constructed. */
2751 static Lisp_Object
2752 unwind_create_frame (Lisp_Object frame)
2754 struct frame *f = XFRAME (frame);
2756 /* If frame is already dead, nothing to do. This can happen if the
2757 display is disconnected after the frame has become official, but
2758 before x_create_frame removes the unwind protect. */
2759 if (!FRAME_LIVE_P (f))
2760 return Qnil;
2762 /* If frame is ``official'', nothing to do. */
2763 if (NILP (Fmemq (frame, Vframe_list)))
2765 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2766 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2767 #endif
2769 x_free_frame_resources (f);
2770 free_glyphs (f);
2772 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2773 /* Check that reference counts are indeed correct. */
2774 eassert (dpyinfo->reference_count == dpyinfo_refcount);
2775 eassert (dpyinfo->terminal->image_cache->refcount == image_cache_refcount);
2776 #endif
2777 return Qt;
2780 return Qnil;
2783 static void
2784 do_unwind_create_frame (Lisp_Object frame)
2786 unwind_create_frame (frame);
2789 static void
2790 x_default_font_parameter (struct frame *f, Lisp_Object parms)
2792 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2793 Lisp_Object font_param = x_get_arg (dpyinfo, parms, Qfont, NULL, NULL,
2794 RES_TYPE_STRING);
2795 Lisp_Object font = Qnil;
2796 if (EQ (font_param, Qunbound))
2797 font_param = Qnil;
2799 if (NILP (font_param))
2801 /* System font should take precedence over X resources. We suggest this
2802 regardless of font-use-system-font because .emacs may not have been
2803 read yet. */
2804 const char *system_font = xsettings_get_system_font ();
2805 if (system_font)
2806 font = font_open_by_name (f, build_unibyte_string (system_font));
2809 if (NILP (font))
2810 font = !NILP (font_param) ? font_param
2811 : x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
2813 if (! FONTP (font) && ! STRINGP (font))
2815 const char *names[]
2817 #ifdef HAVE_XFT
2818 /* This will find the normal Xft font. */
2819 "monospace-10",
2820 #endif
2821 "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1",
2822 "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2823 "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2824 /* This was formerly the first thing tried, but it finds
2825 too many fonts and takes too long. */
2826 "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1",
2827 /* If those didn't work, look for something which will
2828 at least work. */
2829 "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1",
2830 "fixed",
2831 NULL };
2832 int i;
2834 for (i = 0; names[i]; i++)
2836 font = font_open_by_name (f, build_unibyte_string (names[i]));
2837 if (! NILP (font))
2838 break;
2840 if (NILP (font))
2841 error ("No suitable font was found");
2843 else if (!NILP (font_param))
2845 /* Remember the explicit font parameter, so we can re-apply it after
2846 we've applied the `default' face settings. */
2847 AUTO_FRAME_ARG (arg, Qfont_param, font_param);
2848 x_set_frame_parameters (f, arg);
2851 /* This call will make X resources override any system font setting. */
2852 x_default_parameter (f, parms, Qfont, font, "font", "Font", RES_TYPE_STRING);
2856 DEFUN ("x-wm-set-size-hint", Fx_wm_set_size_hint, Sx_wm_set_size_hint,
2857 0, 1, 0,
2858 doc: /* Send the size hints for frame FRAME to the window manager.
2859 If FRAME is omitted or nil, use the selected frame.
2860 Signal error if FRAME is not an X frame. */)
2861 (Lisp_Object frame)
2863 struct frame *f = decode_window_system_frame (frame);
2865 block_input ();
2866 x_wm_set_size_hint (f, 0, false);
2867 unblock_input ();
2868 return Qnil;
2871 static void
2872 set_machine_and_pid_properties (struct frame *f)
2874 /* This will set WM_CLIENT_MACHINE and WM_LOCALE_NAME. */
2875 XSetWMProperties (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), NULL, NULL,
2876 NULL, 0, NULL, NULL, NULL);
2877 pid_t pid = getpid ();
2878 if (pid <= 0xffffffffu)
2880 unsigned long xpid = pid;
2881 XChangeProperty (FRAME_X_DISPLAY (f),
2882 FRAME_OUTER_WINDOW (f),
2883 XInternAtom (FRAME_X_DISPLAY (f),
2884 "_NET_WM_PID",
2885 False),
2886 XA_CARDINAL, 32, PropModeReplace,
2887 (unsigned char *) &xpid, 1);
2891 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
2892 1, 1, 0,
2893 doc: /* Make a new X window, which is called a "frame" in Emacs terms.
2894 Return an Emacs frame object. PARMS is an alist of frame parameters.
2895 If the parameters specify that the frame should not have a minibuffer,
2896 and do not specify a specific minibuffer window to use, then
2897 `default-minibuffer-frame' must be a frame whose minibuffer can be
2898 shared by the new frame.
2900 This function is an internal primitive--use `make-frame' instead. */)
2901 (Lisp_Object parms)
2903 struct frame *f;
2904 Lisp_Object frame, tem;
2905 Lisp_Object name;
2906 bool minibuffer_only = false;
2907 long window_prompting = 0;
2908 ptrdiff_t count = SPECPDL_INDEX ();
2909 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
2910 Lisp_Object display;
2911 struct x_display_info *dpyinfo = NULL;
2912 Lisp_Object parent;
2913 struct kboard *kb;
2915 parms = Fcopy_alist (parms);
2917 /* Use this general default value to start with
2918 until we know if this frame has a specified name. */
2919 Vx_resource_name = Vinvocation_name;
2921 display = x_get_arg (dpyinfo, parms, Qterminal, 0, 0, RES_TYPE_NUMBER);
2922 if (EQ (display, Qunbound))
2923 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
2924 if (EQ (display, Qunbound))
2925 display = Qnil;
2926 dpyinfo = check_x_display_info (display);
2927 kb = dpyinfo->terminal->kboard;
2929 if (!dpyinfo->terminal->name)
2930 error ("Terminal is not live, can't create new frames on it");
2932 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
2933 if (!STRINGP (name)
2934 && ! EQ (name, Qunbound)
2935 && ! NILP (name))
2936 error ("Invalid frame name--not a string or nil");
2938 if (STRINGP (name))
2939 Vx_resource_name = name;
2941 /* See if parent window is specified. */
2942 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
2943 if (EQ (parent, Qunbound))
2944 parent = Qnil;
2945 if (! NILP (parent))
2946 CHECK_NUMBER (parent);
2948 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
2949 /* No need to protect DISPLAY because that's not used after passing
2950 it to make_frame_without_minibuffer. */
2951 frame = Qnil;
2952 GCPRO4 (parms, parent, name, frame);
2953 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
2954 RES_TYPE_SYMBOL);
2955 if (EQ (tem, Qnone) || NILP (tem))
2956 f = make_frame_without_minibuffer (Qnil, kb, display);
2957 else if (EQ (tem, Qonly))
2959 f = make_minibuffer_frame ();
2960 minibuffer_only = true;
2962 else if (WINDOWP (tem))
2963 f = make_frame_without_minibuffer (tem, kb, display);
2964 else
2965 f = make_frame (true);
2967 XSETFRAME (frame, f);
2969 f->terminal = dpyinfo->terminal;
2971 f->output_method = output_x_window;
2972 f->output_data.x = xzalloc (sizeof *f->output_data.x);
2973 f->output_data.x->icon_bitmap = -1;
2974 FRAME_FONTSET (f) = -1;
2975 f->output_data.x->scroll_bar_foreground_pixel = -1;
2976 f->output_data.x->scroll_bar_background_pixel = -1;
2977 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
2978 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
2979 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
2980 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
2981 f->output_data.x->white_relief.pixel = -1;
2982 f->output_data.x->black_relief.pixel = -1;
2984 fset_icon_name (f,
2985 x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
2986 RES_TYPE_STRING));
2987 if (! STRINGP (f->icon_name))
2988 fset_icon_name (f, Qnil);
2990 FRAME_DISPLAY_INFO (f) = dpyinfo;
2992 /* With FRAME_DISPLAY_INFO set up, this unwind-protect is safe. */
2993 record_unwind_protect (do_unwind_create_frame, frame);
2995 /* These colors will be set anyway later, but it's important
2996 to get the color reference counts right, so initialize them! */
2998 Lisp_Object black;
2999 struct gcpro gcpro1;
3001 /* Function x_decode_color can signal an error. Make
3002 sure to initialize color slots so that we won't try
3003 to free colors we haven't allocated. */
3004 FRAME_FOREGROUND_PIXEL (f) = -1;
3005 FRAME_BACKGROUND_PIXEL (f) = -1;
3006 f->output_data.x->cursor_pixel = -1;
3007 f->output_data.x->cursor_foreground_pixel = -1;
3008 f->output_data.x->border_pixel = -1;
3009 f->output_data.x->mouse_pixel = -1;
3011 black = build_string ("black");
3012 GCPRO1 (black);
3013 FRAME_FOREGROUND_PIXEL (f)
3014 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3015 FRAME_BACKGROUND_PIXEL (f)
3016 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3017 f->output_data.x->cursor_pixel
3018 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3019 f->output_data.x->cursor_foreground_pixel
3020 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3021 f->output_data.x->border_pixel
3022 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3023 f->output_data.x->mouse_pixel
3024 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3025 UNGCPRO;
3028 /* Specify the parent under which to make this X window. */
3029 if (!NILP (parent))
3031 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
3032 f->output_data.x->explicit_parent = true;
3034 else
3036 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
3037 f->output_data.x->explicit_parent = false;
3040 /* Set the name; the functions to which we pass f expect the name to
3041 be set. */
3042 if (EQ (name, Qunbound) || NILP (name))
3044 fset_name (f, build_string (dpyinfo->x_id_name));
3045 f->explicit_name = false;
3047 else
3049 fset_name (f, name);
3050 f->explicit_name = true;
3051 /* Use the frame's title when getting resources for this frame. */
3052 specbind (Qx_resource_name, name);
3055 #ifdef USE_CAIRO
3056 register_font_driver (&ftcrfont_driver, f);
3057 #else
3058 #ifdef HAVE_FREETYPE
3059 #ifdef HAVE_XFT
3060 register_font_driver (&xftfont_driver, f);
3061 #else /* not HAVE_XFT */
3062 register_font_driver (&ftxfont_driver, f);
3063 #endif /* not HAVE_XFT */
3064 #endif /* HAVE_FREETYPE */
3065 register_font_driver (&xfont_driver, f);
3066 #endif /* not USE_CAIRO */
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", true);
3142 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
3143 "scrollBarBackground",
3144 "ScrollBarBackground", false);
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, true,
3168 Qx_create_frame_1);
3170 /* Set the menu-bar-lines and tool-bar-lines parameters. We don't
3171 look up the X resources controlling the menu-bar and tool-bar
3172 here; they are processed specially at startup, and reflected in
3173 the values of the mode variables. */
3175 x_default_parameter (f, parms, Qmenu_bar_lines,
3176 NILP (Vmenu_bar_mode)
3177 ? make_number (0) : make_number (1),
3178 NULL, NULL, RES_TYPE_NUMBER);
3179 x_default_parameter (f, parms, Qtool_bar_lines,
3180 NILP (Vtool_bar_mode)
3181 ? make_number (0) : make_number (1),
3182 NULL, NULL, RES_TYPE_NUMBER);
3184 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
3185 "bufferPredicate", "BufferPredicate",
3186 RES_TYPE_SYMBOL);
3187 x_default_parameter (f, parms, Qtitle, Qnil,
3188 "title", "Title", RES_TYPE_STRING);
3189 x_default_parameter (f, parms, Qwait_for_wm, Qt,
3190 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN);
3191 x_default_parameter (f, parms, Qtool_bar_position,
3192 FRAME_TOOL_BAR_POSITION (f), 0, 0, RES_TYPE_SYMBOL);
3194 /* Compute the size of the X window. */
3195 window_prompting = x_figure_window_size (f, parms, true);
3197 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
3198 f->no_split = minibuffer_only || EQ (tem, Qt);
3200 x_icon_verify (f, parms);
3202 /* Create the X widget or window. */
3203 #ifdef USE_X_TOOLKIT
3204 x_window (f, window_prompting);
3205 #else
3206 x_window (f);
3207 #endif
3209 x_icon (f, parms);
3210 x_make_gc (f);
3212 /* Now consider the frame official. */
3213 f->terminal->reference_count++;
3214 FRAME_DISPLAY_INFO (f)->reference_count++;
3215 Vframe_list = Fcons (frame, Vframe_list);
3217 /* We need to do this after creating the X window, so that the
3218 icon-creation functions can say whose icon they're describing. */
3219 x_default_parameter (f, parms, Qicon_type, Qt,
3220 "bitmapIcon", "BitmapIcon", RES_TYPE_BOOLEAN);
3222 x_default_parameter (f, parms, Qauto_raise, Qnil,
3223 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3224 x_default_parameter (f, parms, Qauto_lower, Qnil,
3225 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3226 x_default_parameter (f, parms, Qcursor_type, Qbox,
3227 "cursorType", "CursorType", RES_TYPE_SYMBOL);
3228 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
3229 "scrollBarWidth", "ScrollBarWidth",
3230 RES_TYPE_NUMBER);
3231 x_default_parameter (f, parms, Qscroll_bar_height, Qnil,
3232 "scrollBarHeight", "ScrollBarHeight",
3233 RES_TYPE_NUMBER);
3234 x_default_parameter (f, parms, Qalpha, Qnil,
3235 "alpha", "Alpha", RES_TYPE_NUMBER);
3237 /* Consider frame official, now. */
3238 f->can_x_set_window_size = true;
3240 adjust_frame_size (f, FRAME_TEXT_WIDTH (f), FRAME_TEXT_HEIGHT (f), 0, true,
3241 Qx_create_frame_2);
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, false);
3266 unblock_input ();
3268 /* Process fullscreen parameter here in the hope that normalizing a
3269 fullheight/fullwidth frame will produce the size set by the last
3270 adjust_frame_size call. */
3271 x_default_parameter (f, parms, Qfullscreen, Qnil,
3272 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
3274 /* Make the window appear on the frame and enable display, unless
3275 the caller says not to. However, with explicit parent, Emacs
3276 cannot control visibility, so don't try. */
3277 if (! f->output_data.x->explicit_parent)
3279 Lisp_Object visibility;
3281 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
3282 RES_TYPE_SYMBOL);
3283 if (EQ (visibility, Qunbound))
3284 visibility = Qt;
3286 if (EQ (visibility, Qicon))
3287 x_iconify_frame (f);
3288 else if (! NILP (visibility))
3289 x_make_frame_visible (f);
3290 else
3292 /* Must have been Qnil. */
3296 block_input ();
3298 /* Set machine name and pid for the purpose of window managers. */
3299 set_machine_and_pid_properties (f);
3301 /* Set the WM leader property. GTK does this itself, so this is not
3302 needed when using GTK. */
3303 if (dpyinfo->client_leader_window != 0)
3305 XChangeProperty (FRAME_X_DISPLAY (f),
3306 FRAME_OUTER_WINDOW (f),
3307 dpyinfo->Xatom_wm_client_leader,
3308 XA_WINDOW, 32, PropModeReplace,
3309 (unsigned char *) &dpyinfo->client_leader_window, 1);
3312 unblock_input ();
3314 /* Initialize `default-minibuffer-frame' in case this is the first
3315 frame on this terminal. */
3316 if (FRAME_HAS_MINIBUF_P (f)
3317 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
3318 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
3319 kset_default_minibuffer_frame (kb, frame);
3321 /* All remaining specified parameters, which have not been "used"
3322 by x_get_arg and friends, now go in the misc. alist of the frame. */
3323 for (tem = parms; CONSP (tem); tem = XCDR (tem))
3324 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
3325 fset_param_alist (f, Fcons (XCAR (tem), f->param_alist));
3327 UNGCPRO;
3329 /* Make sure windows on this frame appear in calls to next-window
3330 and similar functions. */
3331 Vwindow_list = Qnil;
3333 return unbind_to (count, frame);
3337 /* FRAME is used only to get a handle on the X display. We don't pass the
3338 display info directly because we're called from frame.c, which doesn't
3339 know about that structure. */
3341 Lisp_Object
3342 x_get_focus_frame (struct frame *frame)
3344 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (frame);
3345 Lisp_Object xfocus;
3346 if (! dpyinfo->x_focus_frame)
3347 return Qnil;
3349 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
3350 return xfocus;
3354 /* In certain situations, when the window manager follows a
3355 click-to-focus policy, there seems to be no way around calling
3356 XSetInputFocus to give another frame the input focus .
3358 In an ideal world, XSetInputFocus should generally be avoided so
3359 that applications don't interfere with the window manager's focus
3360 policy. But I think it's okay to use when it's clearly done
3361 following a user-command. */
3363 void
3364 x_focus_frame (struct frame *f)
3366 Display *dpy = FRAME_X_DISPLAY (f);
3368 block_input ();
3369 x_catch_errors (dpy);
3371 if (FRAME_X_EMBEDDED_P (f))
3373 /* For Xembedded frames, normally the embedder forwards key
3374 events. See XEmbed Protocol Specification at
3375 http://freedesktop.org/wiki/Specifications/xembed-spec */
3376 xembed_request_focus (f);
3378 else
3380 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3381 RevertToParent, CurrentTime);
3382 x_ewmh_activate_frame (f);
3385 x_uncatch_errors ();
3386 unblock_input ();
3390 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
3391 doc: /* Internal function called by `color-defined-p', which see
3392 .\(Note that the Nextstep version of this function ignores FRAME.) */)
3393 (Lisp_Object color, Lisp_Object frame)
3395 XColor foo;
3396 struct frame *f = decode_window_system_frame (frame);
3398 CHECK_STRING (color);
3400 if (x_defined_color (f, SSDATA (color), &foo, false))
3401 return Qt;
3402 else
3403 return Qnil;
3406 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
3407 doc: /* Internal function called by `color-values', which see. */)
3408 (Lisp_Object color, Lisp_Object frame)
3410 XColor foo;
3411 struct frame *f = decode_window_system_frame (frame);
3413 CHECK_STRING (color);
3415 if (x_defined_color (f, SSDATA (color), &foo, false))
3416 return list3i (foo.red, foo.green, foo.blue);
3417 else
3418 return Qnil;
3421 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
3422 doc: /* Internal function called by `display-color-p', which see. */)
3423 (Lisp_Object terminal)
3425 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3427 if (dpyinfo->n_planes <= 2)
3428 return Qnil;
3430 switch (dpyinfo->visual->class)
3432 case StaticColor:
3433 case PseudoColor:
3434 case TrueColor:
3435 case DirectColor:
3436 return Qt;
3438 default:
3439 return Qnil;
3443 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
3444 0, 1, 0,
3445 doc: /* Return t if the X display supports shades of gray.
3446 Note that color displays do support shades of gray.
3447 The optional argument TERMINAL specifies which display to ask about.
3448 TERMINAL should be a terminal object, a frame or a display name (a string).
3449 If omitted or nil, that stands for the selected frame's display. */)
3450 (Lisp_Object terminal)
3452 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3454 if (dpyinfo->n_planes <= 1)
3455 return Qnil;
3457 switch (dpyinfo->visual->class)
3459 case StaticColor:
3460 case PseudoColor:
3461 case TrueColor:
3462 case DirectColor:
3463 case StaticGray:
3464 case GrayScale:
3465 return Qt;
3467 default:
3468 return Qnil;
3472 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
3473 0, 1, 0,
3474 doc: /* Return the width in pixels of the X display TERMINAL.
3475 The optional argument TERMINAL specifies which display to ask about.
3476 TERMINAL should be a terminal object, a frame or a display name (a string).
3477 If omitted or nil, that stands for the selected frame's display.
3479 On \"multi-monitor\" setups this refers to the pixel width for all
3480 physical monitors associated with TERMINAL. To get information for
3481 each physical monitor, use `display-monitor-attributes-list'. */)
3482 (Lisp_Object terminal)
3484 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3486 return make_number (x_display_pixel_width (dpyinfo));
3489 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
3490 Sx_display_pixel_height, 0, 1, 0,
3491 doc: /* Return the height in pixels of the X display TERMINAL.
3492 The optional argument TERMINAL specifies which display to ask about.
3493 TERMINAL should be a terminal object, a frame or a display name (a string).
3494 If omitted or nil, that stands for the selected frame's display.
3496 On \"multi-monitor\" setups this refers to the pixel height for all
3497 physical monitors associated with TERMINAL. To get information for
3498 each physical monitor, use `display-monitor-attributes-list'. */)
3499 (Lisp_Object terminal)
3501 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3503 return make_number (x_display_pixel_height (dpyinfo));
3506 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
3507 0, 1, 0,
3508 doc: /* Return the number of bitplanes of the X display TERMINAL.
3509 The optional argument TERMINAL specifies which display to ask about.
3510 TERMINAL should be a terminal object, a frame or a display name (a string).
3511 If omitted or nil, that stands for the selected frame's display. */)
3512 (Lisp_Object terminal)
3514 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3516 return make_number (dpyinfo->n_planes);
3519 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
3520 0, 1, 0,
3521 doc: /* Return the number of color cells of the X display TERMINAL.
3522 The optional argument TERMINAL specifies which display to ask about.
3523 TERMINAL should be a terminal object, a frame or a display name (a string).
3524 If omitted or nil, that stands for the selected frame's display. */)
3525 (Lisp_Object terminal)
3527 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3529 int nr_planes = DisplayPlanes (dpyinfo->display,
3530 XScreenNumberOfScreen (dpyinfo->screen));
3532 /* Truncate nr_planes to 24 to avoid integer overflow.
3533 Some displays says 32, but only 24 bits are actually significant.
3534 There are only very few and rare video cards that have more than
3535 24 significant bits. Also 24 bits is more than 16 million colors,
3536 it "should be enough for everyone". */
3537 if (nr_planes > 24) nr_planes = 24;
3539 return make_number (1 << nr_planes);
3542 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
3543 Sx_server_max_request_size,
3544 0, 1, 0,
3545 doc: /* Return the maximum request size of the X server of display TERMINAL.
3546 The optional argument TERMINAL specifies which display to ask about.
3547 TERMINAL should be a terminal object, a frame or a display name (a string).
3548 If omitted or nil, that stands for the selected frame's display. */)
3549 (Lisp_Object terminal)
3551 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3553 return make_number (MAXREQUEST (dpyinfo->display));
3556 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
3557 doc: /* Return the "vendor ID" string of the GUI software on TERMINAL.
3559 \(Labeling every distributor as a "vendor" embodies the false assumption
3560 that operating systems cannot be developed and distributed noncommercially.)
3561 The optional argument TERMINAL specifies which display to ask about.
3563 For GNU and Unix systems, this queries the X server software; for
3564 MS-Windows, this queries the OS.
3566 TERMINAL should be a terminal object, a frame or a display name (a string).
3567 If omitted or nil, that stands for the selected frame's display. */)
3568 (Lisp_Object terminal)
3570 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3571 const char *vendor = ServerVendor (dpyinfo->display);
3573 if (! vendor) vendor = "";
3574 return build_string (vendor);
3577 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
3578 doc: /* Return the version numbers of the GUI software on TERMINAL.
3579 The value is a list of three integers specifying the version of the GUI
3580 software in use.
3582 For GNU and Unix system, the first 2 numbers are the version of the X
3583 Protocol used on TERMINAL and the 3rd number is the distributor-specific
3584 release number. For MS-Windows, the 3 numbers report the version and
3585 the build number of the OS.
3587 See also the function `x-server-vendor'.
3589 The optional argument TERMINAL specifies which display to ask about.
3590 TERMINAL should be a terminal object, a frame or a display name (a string).
3591 If omitted or nil, that stands for the selected frame's display. */)
3592 (Lisp_Object terminal)
3594 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3595 Display *dpy = dpyinfo->display;
3597 return list3i (ProtocolVersion (dpy), ProtocolRevision (dpy),
3598 VendorRelease (dpy));
3601 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
3602 doc: /* Return the number of screens on the X server of display TERMINAL.
3603 The optional argument TERMINAL specifies which display to ask about.
3604 TERMINAL should be a terminal object, a frame or a display name (a string).
3605 If omitted or nil, that stands for the selected frame's display. */)
3606 (Lisp_Object terminal)
3608 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3610 return make_number (ScreenCount (dpyinfo->display));
3613 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
3614 doc: /* Return the height in millimeters of the X display TERMINAL.
3615 The optional argument TERMINAL specifies which display to ask about.
3616 TERMINAL should be a terminal object, a frame or a display name (a string).
3617 If omitted or nil, that stands for the selected frame's display.
3619 On \"multi-monitor\" setups this refers to the height in millimeters for
3620 all physical monitors associated with TERMINAL. To get information
3621 for each physical monitor, use `display-monitor-attributes-list'. */)
3622 (Lisp_Object terminal)
3624 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3626 return make_number (HeightMMOfScreen (dpyinfo->screen));
3629 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
3630 doc: /* Return the width in millimeters of the X display TERMINAL.
3631 The optional argument TERMINAL specifies which display to ask about.
3632 TERMINAL should be a terminal object, a frame or a display name (a string).
3633 If omitted or nil, that stands for the selected frame's display.
3635 On \"multi-monitor\" setups this refers to the width in millimeters for
3636 all physical monitors associated with TERMINAL. To get information
3637 for each physical monitor, use `display-monitor-attributes-list'. */)
3638 (Lisp_Object terminal)
3640 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3642 return make_number (WidthMMOfScreen (dpyinfo->screen));
3645 DEFUN ("x-display-backing-store", Fx_display_backing_store,
3646 Sx_display_backing_store, 0, 1, 0,
3647 doc: /* Return an indication of whether X display TERMINAL does backing store.
3648 The value may be `always', `when-mapped', or `not-useful'.
3649 The optional argument TERMINAL specifies which display to ask about.
3650 TERMINAL should be a terminal object, a frame or a display name (a string).
3651 If omitted or nil, that stands for the selected frame's display. */)
3652 (Lisp_Object terminal)
3654 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3655 Lisp_Object result;
3657 switch (DoesBackingStore (dpyinfo->screen))
3659 case Always:
3660 result = intern ("always");
3661 break;
3663 case WhenMapped:
3664 result = intern ("when-mapped");
3665 break;
3667 case NotUseful:
3668 result = intern ("not-useful");
3669 break;
3671 default:
3672 error ("Strange value for BackingStore parameter of screen");
3675 return result;
3678 DEFUN ("x-display-visual-class", Fx_display_visual_class,
3679 Sx_display_visual_class, 0, 1, 0,
3680 doc: /* Return the visual class of the X display TERMINAL.
3681 The value is one of the symbols `static-gray', `gray-scale',
3682 `static-color', `pseudo-color', `true-color', or `direct-color'.
3684 The optional argument TERMINAL specifies which display to ask about.
3685 TERMINAL should a terminal object, a frame or a display name (a string).
3686 If omitted or nil, that stands for the selected frame's display. */)
3687 (Lisp_Object terminal)
3689 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3690 Lisp_Object result;
3692 switch (dpyinfo->visual->class)
3694 case StaticGray:
3695 result = intern ("static-gray");
3696 break;
3697 case GrayScale:
3698 result = intern ("gray-scale");
3699 break;
3700 case StaticColor:
3701 result = intern ("static-color");
3702 break;
3703 case PseudoColor:
3704 result = intern ("pseudo-color");
3705 break;
3706 case TrueColor:
3707 result = intern ("true-color");
3708 break;
3709 case DirectColor:
3710 result = intern ("direct-color");
3711 break;
3712 default:
3713 error ("Display has an unknown visual class");
3716 return result;
3719 DEFUN ("x-display-save-under", Fx_display_save_under,
3720 Sx_display_save_under, 0, 1, 0,
3721 doc: /* Return t if the X display TERMINAL supports the save-under feature.
3722 The optional argument TERMINAL specifies which display to ask about.
3723 TERMINAL should be a terminal object, a frame or a display name (a string).
3724 If omitted or nil, that stands for the selected frame's display. */)
3725 (Lisp_Object terminal)
3727 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3729 if (DoesSaveUnders (dpyinfo->screen) == True)
3730 return Qt;
3731 else
3732 return Qnil;
3735 /* Store the geometry of the workarea on display DPYINFO into *RECT.
3736 Return false if and only if the workarea information cannot be
3737 obtained via the _NET_WORKAREA root window property. */
3739 #if ! GTK_CHECK_VERSION (3, 4, 0)
3740 static bool
3741 x_get_net_workarea (struct x_display_info *dpyinfo, XRectangle *rect)
3743 Display *dpy = dpyinfo->display;
3744 long offset, max_len;
3745 Atom target_type, actual_type;
3746 unsigned long actual_size, bytes_remaining;
3747 int rc, actual_format;
3748 unsigned char *tmp_data = NULL;
3749 bool result = false;
3751 x_catch_errors (dpy);
3752 offset = 0;
3753 max_len = 1;
3754 target_type = XA_CARDINAL;
3755 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3756 dpyinfo->Xatom_net_current_desktop,
3757 offset, max_len, False, target_type,
3758 &actual_type, &actual_format, &actual_size,
3759 &bytes_remaining, &tmp_data);
3760 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3761 && actual_format == 32 && actual_size == max_len)
3763 long current_desktop = ((long *) tmp_data)[0];
3765 XFree (tmp_data);
3766 tmp_data = NULL;
3768 offset = 4 * current_desktop;
3769 max_len = 4;
3770 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3771 dpyinfo->Xatom_net_workarea,
3772 offset, max_len, False, target_type,
3773 &actual_type, &actual_format, &actual_size,
3774 &bytes_remaining, &tmp_data);
3775 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3776 && actual_format == 32 && actual_size == max_len)
3778 long *values = (long *) tmp_data;
3780 rect->x = values[0];
3781 rect->y = values[1];
3782 rect->width = values[2];
3783 rect->height = values[3];
3785 XFree (tmp_data);
3786 tmp_data = NULL;
3788 result = true;
3791 if (tmp_data)
3792 XFree (tmp_data);
3793 x_uncatch_errors ();
3795 return result;
3797 #endif
3799 #ifndef USE_GTK
3801 /* Return monitor number where F is "most" or closest to. */
3802 static int
3803 x_get_monitor_for_frame (struct frame *f,
3804 struct MonitorInfo *monitors,
3805 int n_monitors)
3807 XRectangle frect;
3808 int area = 0, dist = -1;
3809 int best_area = -1, best_dist = -1;
3810 int i;
3812 if (n_monitors == 1) return 0;
3813 frect.x = f->left_pos;
3814 frect.y = f->top_pos;
3815 frect.width = FRAME_PIXEL_WIDTH (f);
3816 frect.height = FRAME_PIXEL_HEIGHT (f);
3818 for (i = 0; i < n_monitors; ++i)
3820 struct MonitorInfo *mi = &monitors[i];
3821 XRectangle res;
3822 int a = 0;
3824 if (mi->geom.width == 0) continue;
3826 if (x_intersect_rectangles (&mi->geom, &frect, &res))
3828 a = res.width * res.height;
3829 if (a > area)
3831 area = a;
3832 best_area = i;
3836 if (a == 0 && area == 0)
3838 int dx, dy, d;
3839 if (frect.x + frect.width < mi->geom.x)
3840 dx = mi->geom.x - frect.x + frect.width;
3841 else if (frect.x > mi->geom.x + mi->geom.width)
3842 dx = frect.x - mi->geom.x + mi->geom.width;
3843 else
3844 dx = 0;
3845 if (frect.y + frect.height < mi->geom.y)
3846 dy = mi->geom.y - frect.y + frect.height;
3847 else if (frect.y > mi->geom.y + mi->geom.height)
3848 dy = frect.y - mi->geom.y + mi->geom.height;
3849 else
3850 dy = 0;
3852 d = dx*dx + dy*dy;
3853 if (dist == -1 || dist > d)
3855 dist = d;
3856 best_dist = i;
3861 return best_area != -1 ? best_area : (best_dist != -1 ? best_dist : 0);
3864 static Lisp_Object
3865 x_make_monitor_attribute_list (struct MonitorInfo *monitors,
3866 int n_monitors,
3867 int primary_monitor,
3868 struct x_display_info *dpyinfo,
3869 const char *source)
3871 Lisp_Object monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
3872 Lisp_Object frame, rest;
3874 FOR_EACH_FRAME (rest, frame)
3876 struct frame *f = XFRAME (frame);
3878 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo
3879 && !EQ (frame, tip_frame))
3881 int i = x_get_monitor_for_frame (f, monitors, n_monitors);
3882 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
3886 return make_monitor_attribute_list (monitors, n_monitors, primary_monitor,
3887 monitor_frames, source);
3890 static Lisp_Object
3891 x_get_monitor_attributes_fallback (struct x_display_info *dpyinfo)
3893 struct MonitorInfo monitor;
3894 XRectangle workarea_r;
3896 /* Fallback: treat (possibly) multiple physical monitors as if they
3897 formed a single monitor as a whole. This should provide a
3898 consistent result at least on single monitor environments. */
3899 monitor.geom.x = monitor.geom.y = 0;
3900 monitor.geom.width = x_display_pixel_width (dpyinfo);
3901 monitor.geom.height = x_display_pixel_height (dpyinfo);
3902 monitor.mm_width = WidthMMOfScreen (dpyinfo->screen);
3903 monitor.mm_height = HeightMMOfScreen (dpyinfo->screen);
3904 monitor.name = xstrdup ("combined screen");
3906 if (x_get_net_workarea (dpyinfo, &workarea_r))
3907 monitor.work = workarea_r;
3908 else
3909 monitor.work = monitor.geom;
3910 return x_make_monitor_attribute_list (&monitor, 1, 0, dpyinfo, "fallback");
3914 #ifdef HAVE_XINERAMA
3915 static Lisp_Object
3916 x_get_monitor_attributes_xinerama (struct x_display_info *dpyinfo)
3918 int n_monitors, i;
3919 Lisp_Object attributes_list = Qnil;
3920 Display *dpy = dpyinfo->display;
3921 XineramaScreenInfo *info = XineramaQueryScreens (dpy, &n_monitors);
3922 struct MonitorInfo *monitors;
3923 double mm_width_per_pixel, mm_height_per_pixel;
3925 if (! info || n_monitors == 0)
3927 if (info)
3928 XFree (info);
3929 return attributes_list;
3932 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
3933 / x_display_pixel_width (dpyinfo));
3934 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
3935 / x_display_pixel_height (dpyinfo));
3936 monitors = xzalloc (n_monitors * sizeof *monitors);
3937 for (i = 0; i < n_monitors; ++i)
3939 struct MonitorInfo *mi = &monitors[i];
3940 XRectangle workarea_r;
3942 mi->geom.x = info[i].x_org;
3943 mi->geom.y = info[i].y_org;
3944 mi->geom.width = info[i].width;
3945 mi->geom.height = info[i].height;
3946 mi->mm_width = mi->geom.width * mm_width_per_pixel + 0.5;
3947 mi->mm_height = mi->geom.height * mm_height_per_pixel + 0.5;
3948 mi->name = 0;
3950 /* Xinerama usually have primary monitor first, just use that. */
3951 if (i == 0 && x_get_net_workarea (dpyinfo, &workarea_r))
3953 mi->work = workarea_r;
3954 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
3955 mi->work = mi->geom;
3957 else
3958 mi->work = mi->geom;
3960 XFree (info);
3962 attributes_list = x_make_monitor_attribute_list (monitors,
3963 n_monitors,
3965 dpyinfo,
3966 "Xinerama");
3967 free_monitors (monitors, n_monitors);
3968 return attributes_list;
3970 #endif /* HAVE_XINERAMA */
3973 #ifdef HAVE_XRANDR
3974 static Lisp_Object
3975 x_get_monitor_attributes_xrandr (struct x_display_info *dpyinfo)
3977 Lisp_Object attributes_list = Qnil;
3978 XRRScreenResources *resources;
3979 Display *dpy = dpyinfo->display;
3980 int i, n_monitors, primary = -1;
3981 RROutput pxid = None;
3982 struct MonitorInfo *monitors;
3984 #ifdef HAVE_XRRGETSCREENRESOURCESCURRENT
3985 resources = XRRGetScreenResourcesCurrent (dpy, dpyinfo->root_window);
3986 #else
3987 resources = XRRGetScreenResources (dpy, dpyinfo->root_window);
3988 #endif
3989 if (! resources || resources->noutput == 0)
3991 if (resources)
3992 XRRFreeScreenResources (resources);
3993 return Qnil;
3995 n_monitors = resources->noutput;
3996 monitors = xzalloc (n_monitors * sizeof *monitors);
3998 #ifdef HAVE_XRRGETOUTPUTPRIMARY
3999 pxid = XRRGetOutputPrimary (dpy, dpyinfo->root_window);
4000 #endif
4002 for (i = 0; i < n_monitors; ++i)
4004 XRROutputInfo *info = XRRGetOutputInfo (dpy, resources,
4005 resources->outputs[i]);
4006 Connection conn = info ? info->connection : RR_Disconnected;
4007 RRCrtc id = info ? info->crtc : None;
4009 if (strcmp (info->name, "default") == 0)
4011 /* Non XRandr 1.2 driver, does not give useful data. */
4012 XRRFreeOutputInfo (info);
4013 XRRFreeScreenResources (resources);
4014 free_monitors (monitors, n_monitors);
4015 return Qnil;
4018 if (conn != RR_Disconnected && id != None)
4020 XRRCrtcInfo *crtc = XRRGetCrtcInfo (dpy, resources, id);
4021 struct MonitorInfo *mi = &monitors[i];
4022 XRectangle workarea_r;
4024 if (! crtc)
4026 XRRFreeOutputInfo (info);
4027 continue;
4030 mi->geom.x = crtc->x;
4031 mi->geom.y = crtc->y;
4032 mi->geom.width = crtc->width;
4033 mi->geom.height = crtc->height;
4034 mi->mm_width = info->mm_width;
4035 mi->mm_height = info->mm_height;
4036 mi->name = xstrdup (info->name);
4038 if (pxid != None && pxid == resources->outputs[i])
4039 primary = i;
4040 else if (primary == -1 && strcmp (info->name, "LVDS") == 0)
4041 primary = i;
4043 if (i == primary && x_get_net_workarea (dpyinfo, &workarea_r))
4045 mi->work= workarea_r;
4046 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
4047 mi->work = mi->geom;
4049 else
4050 mi->work = mi->geom;
4052 XRRFreeCrtcInfo (crtc);
4054 XRRFreeOutputInfo (info);
4056 XRRFreeScreenResources (resources);
4058 attributes_list = x_make_monitor_attribute_list (monitors,
4059 n_monitors,
4060 primary,
4061 dpyinfo,
4062 "XRandr");
4063 free_monitors (monitors, n_monitors);
4064 return attributes_list;
4066 #endif /* HAVE_XRANDR */
4068 static Lisp_Object
4069 x_get_monitor_attributes (struct x_display_info *dpyinfo)
4071 Lisp_Object attributes_list = Qnil;
4072 Display *dpy = dpyinfo->display;
4074 (void) dpy; /* Suppress unused variable warning. */
4076 #ifdef HAVE_XRANDR
4077 int xrr_event_base, xrr_error_base;
4078 bool xrr_ok = false;
4079 xrr_ok = XRRQueryExtension (dpy, &xrr_event_base, &xrr_error_base);
4080 if (xrr_ok)
4082 int xrr_major, xrr_minor;
4083 XRRQueryVersion (dpy, &xrr_major, &xrr_minor);
4084 xrr_ok = (xrr_major == 1 && xrr_minor >= 2) || xrr_major > 1;
4087 if (xrr_ok)
4088 attributes_list = x_get_monitor_attributes_xrandr (dpyinfo);
4089 #endif /* HAVE_XRANDR */
4091 #ifdef HAVE_XINERAMA
4092 if (NILP (attributes_list))
4094 int xin_event_base, xin_error_base;
4095 bool xin_ok = false;
4096 xin_ok = XineramaQueryExtension (dpy, &xin_event_base, &xin_error_base);
4097 if (xin_ok && XineramaIsActive (dpy))
4098 attributes_list = x_get_monitor_attributes_xinerama (dpyinfo);
4100 #endif /* HAVE_XINERAMA */
4102 if (NILP (attributes_list))
4103 attributes_list = x_get_monitor_attributes_fallback (dpyinfo);
4105 return attributes_list;
4108 #endif /* !USE_GTK */
4110 DEFUN ("x-display-monitor-attributes-list", Fx_display_monitor_attributes_list,
4111 Sx_display_monitor_attributes_list,
4112 0, 1, 0,
4113 doc: /* Return a list of physical monitor attributes on the X display TERMINAL.
4115 The optional argument TERMINAL specifies which display to ask about.
4116 TERMINAL should be a terminal object, a frame or a display name (a string).
4117 If omitted or nil, that stands for the selected frame's display.
4119 In addition to the standard attribute keys listed in
4120 `display-monitor-attributes-list', the following keys are contained in
4121 the attributes:
4123 source -- String describing the source from which multi-monitor
4124 information is obtained, one of \"Gdk\", \"XRandr\",
4125 \"Xinerama\", or \"fallback\"
4127 Internal use only, use `display-monitor-attributes-list' instead. */)
4128 (Lisp_Object terminal)
4130 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4131 Lisp_Object attributes_list = Qnil;
4133 #ifdef USE_GTK
4134 double mm_width_per_pixel, mm_height_per_pixel;
4135 GdkDisplay *gdpy;
4136 GdkScreen *gscreen;
4137 gint primary_monitor = 0, n_monitors, i;
4138 Lisp_Object monitor_frames, rest, frame;
4139 static const char *source = "Gdk";
4140 struct MonitorInfo *monitors;
4142 block_input ();
4143 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
4144 / x_display_pixel_width (dpyinfo));
4145 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
4146 / x_display_pixel_height (dpyinfo));
4147 gdpy = gdk_x11_lookup_xdisplay (dpyinfo->display);
4148 gscreen = gdk_display_get_default_screen (gdpy);
4149 #if GTK_CHECK_VERSION (2, 20, 0)
4150 primary_monitor = gdk_screen_get_primary_monitor (gscreen);
4151 #endif
4152 n_monitors = gdk_screen_get_n_monitors (gscreen);
4153 monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
4154 monitors = xzalloc (n_monitors * sizeof *monitors);
4156 FOR_EACH_FRAME (rest, frame)
4158 struct frame *f = XFRAME (frame);
4160 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo
4161 && !EQ (frame, tip_frame))
4163 GdkWindow *gwin = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
4165 i = gdk_screen_get_monitor_at_window (gscreen, gwin);
4166 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
4170 for (i = 0; i < n_monitors; ++i)
4172 gint width_mm = -1, height_mm = -1;
4173 GdkRectangle rec, work;
4174 struct MonitorInfo *mi = &monitors[i];
4176 gdk_screen_get_monitor_geometry (gscreen, i, &rec);
4178 #if GTK_CHECK_VERSION (2, 14, 0)
4179 width_mm = gdk_screen_get_monitor_width_mm (gscreen, i);
4180 height_mm = gdk_screen_get_monitor_height_mm (gscreen, i);
4181 #endif
4182 if (width_mm < 0)
4183 width_mm = rec.width * mm_width_per_pixel + 0.5;
4184 if (height_mm < 0)
4185 height_mm = rec.height * mm_height_per_pixel + 0.5;
4187 #if GTK_CHECK_VERSION (3, 4, 0)
4188 gdk_screen_get_monitor_workarea (gscreen, i, &work);
4189 #else
4190 /* Emulate the behavior of GTK+ 3.4. */
4192 XRectangle workarea_r;
4194 if (i == primary_monitor && x_get_net_workarea (dpyinfo, &workarea_r))
4196 work.x = workarea_r.x;
4197 work.y = workarea_r.y;
4198 work.width = workarea_r.width;
4199 work.height = workarea_r.height;
4200 if (! gdk_rectangle_intersect (&rec, &work, &work))
4201 work = rec;
4203 else
4204 work = rec;
4206 #endif
4209 mi->geom.x = rec.x;
4210 mi->geom.y = rec.y;
4211 mi->geom.width = rec.width;
4212 mi->geom.height = rec.height;
4213 mi->work.x = work.x;
4214 mi->work.y = work.y;
4215 mi->work.width = work.width;
4216 mi->work.height = work.height;
4217 mi->mm_width = width_mm;
4218 mi->mm_height = height_mm;
4220 #if GTK_CHECK_VERSION (2, 14, 0)
4221 mi->name = gdk_screen_get_monitor_plug_name (gscreen, i);
4222 #endif
4225 attributes_list = make_monitor_attribute_list (monitors,
4226 n_monitors,
4227 primary_monitor,
4228 monitor_frames,
4229 source);
4230 unblock_input ();
4231 #else /* not USE_GTK */
4233 block_input ();
4234 attributes_list = x_get_monitor_attributes (dpyinfo);
4235 unblock_input ();
4237 #endif /* not USE_GTK */
4239 return attributes_list;
4242 DEFUN ("x-frame-geometry", Fx_frame_geometry, Sx_frame_geometry, 0, 1, 0,
4243 doc: /* Return geometric attributes of frame FRAME.
4245 FRAME must be a live frame and defaults to the selected one.
4247 The return value is an association list containing the following
4248 elements (all size values are in pixels).
4250 - `frame-outer-size' is a cons of the outer width and height of FRAME.
4251 The outer size include the title bar and the external borders as well
4252 as any menu and/or tool bar of frame.
4254 - `border' is a cons of the horizontal and vertical width of FRAME's
4255 external borders.
4257 - `title-bar-height' is the height of the title bar of FRAME.
4259 - `menu-bar-external' if `t' means the menu bar is external (not
4260 included in the inner edges of FRAME).
4262 - `menu-bar-size' is a cons of the width and height of the menu bar of
4263 FRAME.
4265 - `tool-bar-external' if `t' means the tool bar is external (not
4266 included in the inner edges of FRAME).
4268 - `tool-bar-side' tells tells on which side the tool bar on FRAME is and
4269 can be one of `left', `top', `right' or `bottom'.
4271 - `tool-bar-size' is a cons of the width and height of the tool bar of
4272 FRAME.
4274 - `frame-inner-size' is a cons of the inner width and height of FRAME.
4275 This excludes FRAME's title bar and external border as well as any
4276 external menu and/or tool bar. */)
4277 (Lisp_Object frame)
4279 struct frame *f = decode_live_frame (frame);
4280 int inner_width = FRAME_PIXEL_WIDTH (f);
4281 int inner_height = FRAME_PIXEL_HEIGHT (f);
4282 int outer_width, outer_height, border, title;
4283 Lisp_Object fullscreen = Fframe_parameter (frame, Qfullscreen);
4284 int menu_bar_height, menu_bar_width, tool_bar_height, tool_bar_width;
4286 border = FRAME_OUTER_TO_INNER_DIFF_X (f);
4287 title = FRAME_X_OUTPUT (f)->y_pixels_outer_diff - border;
4289 outer_width = FRAME_PIXEL_WIDTH (f) + 2 * border;
4290 outer_height = (FRAME_PIXEL_HEIGHT (f)
4291 + FRAME_OUTER_TO_INNER_DIFF_Y (f)
4292 + FRAME_OUTER_TO_INNER_DIFF_X (f));
4294 #if defined (USE_GTK)
4296 bool tool_bar_left_right = (EQ (FRAME_TOOL_BAR_POSITION (f), Qleft)
4297 || EQ (FRAME_TOOL_BAR_POSITION (f), Qright));
4299 tool_bar_width = (tool_bar_left_right
4300 ? FRAME_TOOLBAR_WIDTH (f)
4301 : FRAME_PIXEL_WIDTH (f));
4302 tool_bar_height = (tool_bar_left_right
4303 ? FRAME_PIXEL_HEIGHT (f)
4304 : FRAME_TOOLBAR_HEIGHT (f));
4305 if (tool_bar_left_right)
4306 /* For some reason FRAME_OUTER_TO_INNER_DIFF_X does not count the
4307 width of a tool bar. */
4308 outer_width += FRAME_TOOLBAR_WIDTH (f);
4310 #else
4311 tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f);
4312 tool_bar_width = ((tool_bar_height > 0)
4313 ? outer_width - 2 * FRAME_INTERNAL_BORDER_WIDTH (f)
4314 : 0);
4315 #endif
4317 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
4318 menu_bar_height = FRAME_MENUBAR_HEIGHT (f);
4319 #else
4320 menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
4321 #endif
4323 menu_bar_width = ((menu_bar_height > 0)
4324 ? outer_width - 2 * border
4325 : 0);
4327 if (!FRAME_EXTERNAL_MENU_BAR (f))
4328 inner_height -= menu_bar_height;
4329 if (!FRAME_EXTERNAL_TOOL_BAR (f))
4330 inner_height -= tool_bar_height;
4332 return
4333 listn (CONSTYPE_HEAP, 10,
4334 Fcons (Qframe_position,
4335 Fcons (make_number (f->left_pos), make_number (f->top_pos))),
4336 Fcons (Qframe_outer_size,
4337 Fcons (make_number (outer_width), make_number (outer_height))),
4338 Fcons (Qexternal_border_size,
4339 ((EQ (fullscreen, Qfullboth) || EQ (fullscreen, Qfullscreen))
4340 ? Fcons (make_number (0), make_number (0))
4341 : Fcons (make_number (border), make_number (border)))),
4342 Fcons (Qtitle_height,
4343 ((EQ (fullscreen, Qfullboth) || EQ (fullscreen, Qfullscreen))
4344 ? make_number (0)
4345 : make_number (title))),
4346 Fcons (Qmenu_bar_external, FRAME_EXTERNAL_MENU_BAR (f) ? Qt : Qnil),
4347 Fcons (Qmenu_bar_size,
4348 Fcons (make_number (menu_bar_width),
4349 make_number (menu_bar_height))),
4350 Fcons (Qtool_bar_external, FRAME_EXTERNAL_TOOL_BAR (f) ? Qt : Qnil),
4351 Fcons (Qtool_bar_position, FRAME_TOOL_BAR_POSITION (f)),
4352 Fcons (Qtool_bar_size,
4353 Fcons (make_number (tool_bar_width),
4354 make_number (tool_bar_height))),
4355 Fcons (Qframe_inner_size,
4356 Fcons (make_number (inner_width),
4357 make_number (inner_height))));
4360 /************************************************************************
4361 X Displays
4362 ************************************************************************/
4365 /* Mapping visual names to visuals. */
4367 static struct visual_class
4369 const char *name;
4370 int class;
4372 visual_classes[] =
4374 {"StaticGray", StaticGray},
4375 {"GrayScale", GrayScale},
4376 {"StaticColor", StaticColor},
4377 {"PseudoColor", PseudoColor},
4378 {"TrueColor", TrueColor},
4379 {"DirectColor", DirectColor},
4380 {NULL, 0}
4384 #ifndef HAVE_XSCREENNUMBEROFSCREEN
4386 /* Value is the screen number of screen SCR. This is a substitute for
4387 the X function with the same name when that doesn't exist. */
4390 XScreenNumberOfScreen (scr)
4391 register Screen *scr;
4393 Display *dpy = scr->display;
4394 int i;
4396 for (i = 0; i < dpy->nscreens; ++i)
4397 if (scr == dpy->screens + i)
4398 break;
4400 return i;
4403 #endif /* not HAVE_XSCREENNUMBEROFSCREEN */
4406 /* Select the visual that should be used on display DPYINFO. Set
4407 members of DPYINFO appropriately. Called from x_term_init. */
4409 void
4410 select_visual (struct x_display_info *dpyinfo)
4412 Display *dpy = dpyinfo->display;
4413 Screen *screen = dpyinfo->screen;
4415 /* See if a visual is specified. */
4416 AUTO_STRING (visualClass, "visualClass");
4417 AUTO_STRING (VisualClass, "VisualClass");
4418 Lisp_Object value = display_x_get_resource (dpyinfo, visualClass,
4419 VisualClass, Qnil, Qnil);
4421 if (STRINGP (value))
4423 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
4424 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
4425 depth, a decimal number. NAME is compared with case ignored. */
4426 char *s = alloca (SBYTES (value) + 1);
4427 char *dash;
4428 int i, class = -1;
4429 XVisualInfo vinfo;
4431 lispstpcpy (s, value);
4432 dash = strchr (s, '-');
4433 if (dash)
4435 dpyinfo->n_planes = atoi (dash + 1);
4436 *dash = '\0';
4438 else
4439 /* We won't find a matching visual with depth 0, so that
4440 an error will be printed below. */
4441 dpyinfo->n_planes = 0;
4443 /* Determine the visual class. */
4444 for (i = 0; visual_classes[i].name; ++i)
4445 if (xstrcasecmp (s, visual_classes[i].name) == 0)
4447 class = visual_classes[i].class;
4448 break;
4451 /* Look up a matching visual for the specified class. */
4452 if (class == -1
4453 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
4454 dpyinfo->n_planes, class, &vinfo))
4455 fatal ("Invalid visual specification `%s'", SDATA (value));
4457 dpyinfo->visual = vinfo.visual;
4459 else
4461 int n_visuals;
4462 XVisualInfo *vinfo, vinfo_template;
4464 dpyinfo->visual = DefaultVisualOfScreen (screen);
4466 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
4467 vinfo_template.screen = XScreenNumberOfScreen (screen);
4468 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
4469 &vinfo_template, &n_visuals);
4470 if (n_visuals <= 0)
4471 fatal ("Can't get proper X visual info");
4473 dpyinfo->n_planes = vinfo->depth;
4474 XFree (vinfo);
4479 /* Return the X display structure for the display named NAME.
4480 Open a new connection if necessary. */
4482 static struct x_display_info *
4483 x_display_info_for_name (Lisp_Object name)
4485 struct x_display_info *dpyinfo;
4487 CHECK_STRING (name);
4489 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
4490 if (!NILP (Fstring_equal (XCAR (dpyinfo->name_list_element), name)))
4491 return dpyinfo;
4493 /* Use this general default value to start with. */
4494 Vx_resource_name = Vinvocation_name;
4496 validate_x_resource_name ();
4498 dpyinfo = x_term_init (name, 0, SSDATA (Vx_resource_name));
4500 if (dpyinfo == 0)
4501 error ("Cannot connect to X server %s", SDATA (name));
4503 XSETFASTINT (Vwindow_system_version, 11);
4505 return dpyinfo;
4509 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
4510 1, 3, 0,
4511 doc: /* Open a connection to a display server.
4512 DISPLAY is the name of the display to connect to.
4513 Optional second arg XRM-STRING is a string of resources in xrdb format.
4514 If the optional third arg MUST-SUCCEED is non-nil,
4515 terminate Emacs if we can't open the connection.
4516 \(In the Nextstep version, the last two arguments are currently ignored.) */)
4517 (Lisp_Object display, Lisp_Object xrm_string, Lisp_Object must_succeed)
4519 char *xrm_option;
4520 struct x_display_info *dpyinfo;
4522 CHECK_STRING (display);
4523 if (! NILP (xrm_string))
4524 CHECK_STRING (xrm_string);
4526 xrm_option = NILP (xrm_string) ? 0 : SSDATA (xrm_string);
4528 validate_x_resource_name ();
4530 /* This is what opens the connection and sets x_current_display.
4531 This also initializes many symbols, such as those used for input. */
4532 dpyinfo = x_term_init (display, xrm_option,
4533 SSDATA (Vx_resource_name));
4535 if (dpyinfo == 0)
4537 if (!NILP (must_succeed))
4538 fatal ("Cannot connect to X server %s.\n\
4539 Check the DISPLAY environment variable or use `-d'.\n\
4540 Also use the `xauth' program to verify that you have the proper\n\
4541 authorization information needed to connect the X server.\n\
4542 An insecure way to solve the problem may be to use `xhost'.\n",
4543 SDATA (display));
4544 else
4545 error ("Cannot connect to X server %s", SDATA (display));
4548 XSETFASTINT (Vwindow_system_version, 11);
4549 return Qnil;
4552 DEFUN ("x-close-connection", Fx_close_connection,
4553 Sx_close_connection, 1, 1, 0,
4554 doc: /* Close the connection to TERMINAL's X server.
4555 For TERMINAL, specify a terminal object, a frame or a display name (a
4556 string). If TERMINAL is nil, that stands for the selected frame's
4557 terminal. */)
4558 (Lisp_Object terminal)
4560 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4562 if (dpyinfo->reference_count > 0)
4563 error ("Display still has frames on it");
4565 x_delete_terminal (dpyinfo->terminal);
4567 return Qnil;
4570 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
4571 doc: /* Return the list of display names that Emacs has connections to. */)
4572 (void)
4574 Lisp_Object result = Qnil;
4575 struct x_display_info *xdi;
4577 for (xdi = x_display_list; xdi; xdi = xdi->next)
4578 result = Fcons (XCAR (xdi->name_list_element), result);
4580 return result;
4583 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
4584 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
4585 This function only has an effect on X Windows. With MS Windows, it is
4586 defined but does nothing.
4588 If ON is nil, allow buffering of requests.
4589 Turning on synchronization prohibits the Xlib routines from buffering
4590 requests and seriously degrades performance, but makes debugging much
4591 easier.
4592 The optional second argument TERMINAL specifies which display to act on.
4593 TERMINAL should be a terminal object, a frame or a display name (a string).
4594 If TERMINAL is omitted or nil, that stands for the selected frame's display. */)
4595 (Lisp_Object on, Lisp_Object terminal)
4597 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4599 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
4601 return Qnil;
4604 /* Wait for responses to all X commands issued so far for frame F. */
4606 void
4607 x_sync (struct frame *f)
4609 block_input ();
4610 XSync (FRAME_X_DISPLAY (f), False);
4611 unblock_input ();
4615 /***********************************************************************
4616 Window properties
4617 ***********************************************************************/
4619 DEFUN ("x-change-window-property", Fx_change_window_property,
4620 Sx_change_window_property, 2, 6, 0,
4621 doc: /* Change window property PROP to VALUE on the X window of FRAME.
4622 PROP must be a string. VALUE may be a string or a list of conses,
4623 numbers and/or strings. If an element in the list is a string, it is
4624 converted to an atom and the value of the atom is used. If an element
4625 is a cons, it is converted to a 32 bit number where the car is the 16
4626 top bits and the cdr is the lower 16 bits.
4628 FRAME nil or omitted means use the selected frame.
4629 If TYPE is given and non-nil, it is the name of the type of VALUE.
4630 If TYPE is not given or nil, the type is STRING.
4631 FORMAT gives the size in bits of each element if VALUE is a list.
4632 It must be one of 8, 16 or 32.
4633 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
4634 If OUTER-P is non-nil, the property is changed for the outer X window of
4635 FRAME. Default is to change on the edit X window. */)
4636 (Lisp_Object prop, Lisp_Object value, Lisp_Object frame,
4637 Lisp_Object type, Lisp_Object format, Lisp_Object outer_p)
4639 struct frame *f = decode_window_system_frame (frame);
4640 Atom prop_atom;
4641 Atom target_type = XA_STRING;
4642 int element_format = 8;
4643 unsigned char *data;
4644 int nelements;
4645 Window w;
4647 CHECK_STRING (prop);
4649 if (! NILP (format))
4651 CHECK_NUMBER (format);
4653 if (XINT (format) != 8 && XINT (format) != 16
4654 && XINT (format) != 32)
4655 error ("FORMAT must be one of 8, 16 or 32");
4656 element_format = XINT (format);
4659 if (CONSP (value))
4661 ptrdiff_t elsize;
4663 nelements = x_check_property_data (value);
4664 if (nelements == -1)
4665 error ("Bad data in VALUE, must be number, string or cons");
4667 /* The man page for XChangeProperty:
4668 "If the specified format is 32, the property data must be a
4669 long array."
4670 This applies even if long is more than 32 bits. The X library
4671 converts to 32 bits before sending to the X server. */
4672 elsize = element_format == 32 ? sizeof (long) : element_format >> 3;
4673 data = xnmalloc (nelements, elsize);
4675 x_fill_property_data (FRAME_X_DISPLAY (f), value, data, element_format);
4677 else
4679 CHECK_STRING (value);
4680 data = SDATA (value);
4681 if (INT_MAX < SBYTES (value))
4682 error ("VALUE too long");
4683 nelements = SBYTES (value);
4686 block_input ();
4687 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4688 if (! NILP (type))
4690 CHECK_STRING (type);
4691 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4694 if (! NILP (outer_p)) w = FRAME_OUTER_WINDOW (f);
4695 else w = FRAME_X_WINDOW (f);
4697 XChangeProperty (FRAME_X_DISPLAY (f), w,
4698 prop_atom, target_type, element_format, PropModeReplace,
4699 data, nelements);
4701 if (CONSP (value)) xfree (data);
4703 /* Make sure the property is set when we return. */
4704 XFlush (FRAME_X_DISPLAY (f));
4705 unblock_input ();
4707 return value;
4711 DEFUN ("x-delete-window-property", Fx_delete_window_property,
4712 Sx_delete_window_property, 1, 2, 0,
4713 doc: /* Remove window property PROP from X window of FRAME.
4714 FRAME nil or omitted means use the selected frame. Value is PROP. */)
4715 (Lisp_Object prop, Lisp_Object frame)
4717 struct frame *f = decode_window_system_frame (frame);
4718 Atom prop_atom;
4720 CHECK_STRING (prop);
4721 block_input ();
4722 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4723 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
4725 /* Make sure the property is removed when we return. */
4726 XFlush (FRAME_X_DISPLAY (f));
4727 unblock_input ();
4729 return prop;
4733 static Lisp_Object
4734 x_window_property_intern (struct frame *f,
4735 Window target_window,
4736 Atom prop_atom,
4737 Atom target_type,
4738 Lisp_Object delete_p,
4739 Lisp_Object vector_ret_p,
4740 bool *found)
4742 unsigned char *tmp_data = NULL;
4743 Lisp_Object prop_value = Qnil;
4744 Atom actual_type;
4745 int actual_format;
4746 unsigned long actual_size, bytes_remaining;
4747 int rc;
4748 struct gcpro gcpro1;
4750 GCPRO1 (prop_value);
4752 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4753 prop_atom, 0, 0, False, target_type,
4754 &actual_type, &actual_format, &actual_size,
4755 &bytes_remaining, &tmp_data);
4757 *found = actual_format != 0;
4759 if (rc == Success && *found)
4761 XFree (tmp_data);
4762 tmp_data = NULL;
4764 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4765 prop_atom, 0, bytes_remaining,
4766 ! NILP (delete_p), target_type,
4767 &actual_type, &actual_format,
4768 &actual_size, &bytes_remaining,
4769 &tmp_data);
4770 if (rc == Success && tmp_data)
4772 /* The man page for XGetWindowProperty says:
4773 "If the returned format is 32, the returned data is represented
4774 as a long array and should be cast to that type to obtain the
4775 elements."
4776 This applies even if long is more than 32 bits, the X library
4777 converts from 32 bit elements received from the X server to long
4778 and passes the long array to us. Thus, for that case memcpy can not
4779 be used. We convert to a 32 bit type here, because so much code
4780 assume on that.
4782 The bytes and offsets passed to XGetWindowProperty refers to the
4783 property and those are indeed in 32 bit quantities if format is
4784 32. */
4786 if (BITS_PER_LONG > 32 && actual_format == 32)
4788 unsigned long i;
4789 int *idata = (int *) tmp_data;
4790 long *ldata = (long *) tmp_data;
4792 for (i = 0; i < actual_size; ++i)
4793 idata[i] = (int) ldata[i];
4796 if (NILP (vector_ret_p))
4797 prop_value = make_string ((char *) tmp_data, actual_size);
4798 else
4799 prop_value = x_property_data_to_lisp (f,
4800 tmp_data,
4801 actual_type,
4802 actual_format,
4803 actual_size);
4806 if (tmp_data) XFree (tmp_data);
4809 UNGCPRO;
4810 return prop_value;
4813 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
4814 1, 6, 0,
4815 doc: /* Value is the value of window property PROP on FRAME.
4816 If FRAME is nil or omitted, use the selected frame.
4818 On X Windows, the following optional arguments are also accepted:
4819 If TYPE is nil or omitted, get the property as a string.
4820 Otherwise TYPE is the name of the atom that denotes the type expected.
4821 If SOURCE is non-nil, get the property on that window instead of from
4822 FRAME. The number 0 denotes the root window.
4823 If DELETE-P is non-nil, delete the property after retrieving it.
4824 If VECTOR-RET-P is non-nil, don't return a string but a vector of values.
4826 On MS Windows, this function accepts but ignores those optional arguments.
4828 Value is nil if FRAME hasn't a property with name PROP or if PROP has
4829 no value of TYPE (always string in the MS Windows case). */)
4830 (Lisp_Object prop, Lisp_Object frame, Lisp_Object type,
4831 Lisp_Object source, Lisp_Object delete_p, Lisp_Object vector_ret_p)
4833 struct frame *f = decode_window_system_frame (frame);
4834 Atom prop_atom;
4835 Lisp_Object prop_value = Qnil;
4836 Atom target_type = XA_STRING;
4837 Window target_window = FRAME_X_WINDOW (f);
4838 struct gcpro gcpro1;
4839 bool found;
4841 GCPRO1 (prop_value);
4842 CHECK_STRING (prop);
4844 if (! NILP (source))
4846 CONS_TO_INTEGER (source, Window, target_window);
4847 if (! target_window)
4848 target_window = FRAME_DISPLAY_INFO (f)->root_window;
4851 block_input ();
4852 if (STRINGP (type))
4854 if (strcmp ("AnyPropertyType", SSDATA (type)) == 0)
4855 target_type = AnyPropertyType;
4856 else
4857 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4860 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4861 prop_value = x_window_property_intern (f,
4862 target_window,
4863 prop_atom,
4864 target_type,
4865 delete_p,
4866 vector_ret_p,
4867 &found);
4868 if (NILP (prop_value)
4869 && ! found
4870 && NILP (source)
4871 && target_window != FRAME_OUTER_WINDOW (f))
4873 prop_value = x_window_property_intern (f,
4874 FRAME_OUTER_WINDOW (f),
4875 prop_atom,
4876 target_type,
4877 delete_p,
4878 vector_ret_p,
4879 &found);
4883 unblock_input ();
4884 UNGCPRO;
4885 return prop_value;
4888 /***********************************************************************
4889 Tool tips
4890 ***********************************************************************/
4892 static Lisp_Object x_create_tip_frame (struct x_display_info *,
4893 Lisp_Object, Lisp_Object);
4894 static void compute_tip_xy (struct frame *, Lisp_Object, Lisp_Object,
4895 Lisp_Object, int, int, int *, int *);
4897 /* The frame of a currently visible tooltip. */
4899 Lisp_Object tip_frame;
4901 /* If non-nil, a timer started that hides the last tooltip when it
4902 fires. */
4904 static Lisp_Object tip_timer;
4905 Window tip_window;
4907 /* If non-nil, a vector of 3 elements containing the last args
4908 with which x-show-tip was called. See there. */
4910 static Lisp_Object last_show_tip_args;
4913 static void
4914 unwind_create_tip_frame (Lisp_Object frame)
4916 Lisp_Object deleted;
4918 deleted = unwind_create_frame (frame);
4919 if (EQ (deleted, Qt))
4921 tip_window = None;
4922 tip_frame = Qnil;
4927 /* Create a frame for a tooltip on the display described by DPYINFO.
4928 PARMS is a list of frame parameters. TEXT is the string to
4929 display in the tip frame. Value is the frame.
4931 Note that functions called here, esp. x_default_parameter can
4932 signal errors, for instance when a specified color name is
4933 undefined. We have to make sure that we're in a consistent state
4934 when this happens. */
4936 static Lisp_Object
4937 x_create_tip_frame (struct x_display_info *dpyinfo,
4938 Lisp_Object parms,
4939 Lisp_Object text)
4941 struct frame *f;
4942 Lisp_Object frame;
4943 Lisp_Object name;
4944 int width, height;
4945 ptrdiff_t count = SPECPDL_INDEX ();
4946 struct gcpro gcpro1, gcpro2, gcpro3;
4947 bool face_change_before = face_change;
4948 Lisp_Object buffer;
4949 struct buffer *old_buffer;
4951 if (!dpyinfo->terminal->name)
4952 error ("Terminal is not live, can't create new frames on it");
4954 parms = Fcopy_alist (parms);
4956 /* Get the name of the frame to use for resource lookup. */
4957 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
4958 if (!STRINGP (name)
4959 && !EQ (name, Qunbound)
4960 && !NILP (name))
4961 error ("Invalid frame name--not a string or nil");
4963 frame = Qnil;
4964 GCPRO3 (parms, name, frame);
4965 f = make_frame (true);
4966 XSETFRAME (frame, f);
4968 AUTO_STRING (tip, " *tip*");
4969 buffer = Fget_buffer_create (tip);
4970 /* Use set_window_buffer instead of Fset_window_buffer (see
4971 discussion of bug#11984, bug#12025, bug#12026). */
4972 set_window_buffer (FRAME_ROOT_WINDOW (f), buffer, false, false);
4973 old_buffer = current_buffer;
4974 set_buffer_internal_1 (XBUFFER (buffer));
4975 bset_truncate_lines (current_buffer, Qnil);
4976 specbind (Qinhibit_read_only, Qt);
4977 specbind (Qinhibit_modification_hooks, Qt);
4978 Ferase_buffer ();
4979 Finsert (1, &text);
4980 set_buffer_internal_1 (old_buffer);
4982 record_unwind_protect (unwind_create_tip_frame, frame);
4984 f->terminal = dpyinfo->terminal;
4986 /* By setting the output method, we're essentially saying that
4987 the frame is live, as per FRAME_LIVE_P. If we get a signal
4988 from this point on, x_destroy_window might screw up reference
4989 counts etc. */
4990 f->output_method = output_x_window;
4991 f->output_data.x = xzalloc (sizeof *f->output_data.x);
4992 f->output_data.x->icon_bitmap = -1;
4993 FRAME_FONTSET (f) = -1;
4994 f->output_data.x->scroll_bar_foreground_pixel = -1;
4995 f->output_data.x->scroll_bar_background_pixel = -1;
4996 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
4997 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
4998 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
4999 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
5000 f->output_data.x->white_relief.pixel = -1;
5001 f->output_data.x->black_relief.pixel = -1;
5003 fset_icon_name (f, Qnil);
5004 FRAME_DISPLAY_INFO (f) = dpyinfo;
5005 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
5006 f->output_data.x->explicit_parent = false;
5008 /* These colors will be set anyway later, but it's important
5009 to get the color reference counts right, so initialize them! */
5011 Lisp_Object black;
5012 struct gcpro gcpro1;
5014 /* Function x_decode_color can signal an error. Make
5015 sure to initialize color slots so that we won't try
5016 to free colors we haven't allocated. */
5017 FRAME_FOREGROUND_PIXEL (f) = -1;
5018 FRAME_BACKGROUND_PIXEL (f) = -1;
5019 f->output_data.x->cursor_pixel = -1;
5020 f->output_data.x->cursor_foreground_pixel = -1;
5021 f->output_data.x->border_pixel = -1;
5022 f->output_data.x->mouse_pixel = -1;
5024 black = build_string ("black");
5025 GCPRO1 (black);
5026 FRAME_FOREGROUND_PIXEL (f)
5027 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5028 FRAME_BACKGROUND_PIXEL (f)
5029 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5030 f->output_data.x->cursor_pixel
5031 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5032 f->output_data.x->cursor_foreground_pixel
5033 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5034 f->output_data.x->border_pixel
5035 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5036 f->output_data.x->mouse_pixel
5037 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5038 UNGCPRO;
5041 /* Set the name; the functions to which we pass f expect the name to
5042 be set. */
5043 if (EQ (name, Qunbound) || NILP (name))
5045 fset_name (f, build_string (dpyinfo->x_id_name));
5046 f->explicit_name = false;
5048 else
5050 fset_name (f, name);
5051 f->explicit_name = true;
5052 /* use the frame's title when getting resources for this frame. */
5053 specbind (Qx_resource_name, name);
5056 #ifdef USE_CAIRO
5057 register_font_driver (&ftcrfont_driver, f);
5058 #else
5059 register_font_driver (&xfont_driver, f);
5060 #ifdef HAVE_FREETYPE
5061 #ifdef HAVE_XFT
5062 register_font_driver (&xftfont_driver, f);
5063 #else /* not HAVE_XFT */
5064 register_font_driver (&ftxfont_driver, f);
5065 #endif /* not HAVE_XFT */
5066 #endif /* HAVE_FREETYPE */
5067 #endif /* not USE_CAIRO */
5069 x_default_parameter (f, parms, Qfont_backend, Qnil,
5070 "fontBackend", "FontBackend", RES_TYPE_STRING);
5072 /* Extract the window parameters from the supplied values that are
5073 needed to determine window geometry. */
5074 x_default_font_parameter (f, parms);
5076 x_default_parameter (f, parms, Qborder_width, make_number (0),
5077 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
5079 /* This defaults to 2 in order to match xterm. We recognize either
5080 internalBorderWidth or internalBorder (which is what xterm calls
5081 it). */
5082 if (NILP (Fassq (Qinternal_border_width, parms)))
5084 Lisp_Object value;
5086 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
5087 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
5088 if (! EQ (value, Qunbound))
5089 parms = Fcons (Fcons (Qinternal_border_width, value),
5090 parms);
5093 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
5094 "internalBorderWidth", "internalBorderWidth",
5095 RES_TYPE_NUMBER);
5096 x_default_parameter (f, parms, Qright_divider_width, make_number (0),
5097 NULL, NULL, RES_TYPE_NUMBER);
5098 x_default_parameter (f, parms, Qbottom_divider_width, make_number (0),
5099 NULL, NULL, RES_TYPE_NUMBER);
5101 /* Also do the stuff which must be set before the window exists. */
5102 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
5103 "foreground", "Foreground", RES_TYPE_STRING);
5104 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
5105 "background", "Background", RES_TYPE_STRING);
5106 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
5107 "pointerColor", "Foreground", RES_TYPE_STRING);
5108 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
5109 "cursorColor", "Foreground", RES_TYPE_STRING);
5110 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
5111 "borderColor", "BorderColor", RES_TYPE_STRING);
5113 #ifdef GLYPH_DEBUG
5114 image_cache_refcount =
5115 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
5116 dpyinfo_refcount = dpyinfo->reference_count;
5117 #endif /* GLYPH_DEBUG */
5119 /* Init faces before x_default_parameter is called for the
5120 scroll-bar-width parameter because otherwise we end up in
5121 init_iterator with a null face cache, which should not happen. */
5122 init_frame_faces (f);
5124 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
5126 x_figure_window_size (f, parms, false);
5129 XSetWindowAttributes attrs;
5130 unsigned long mask;
5131 Atom type = FRAME_DISPLAY_INFO (f)->Xatom_net_window_type_tooltip;
5133 block_input ();
5134 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
5135 if (DoesSaveUnders (dpyinfo->screen))
5136 mask |= CWSaveUnder;
5138 /* Window managers look at the override-redirect flag to determine
5139 whether or net to give windows a decoration (Xlib spec, chapter
5140 3.2.8). */
5141 attrs.override_redirect = True;
5142 attrs.save_under = True;
5143 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
5144 /* Arrange for getting MapNotify and UnmapNotify events. */
5145 attrs.event_mask = StructureNotifyMask;
5146 tip_window
5147 = FRAME_X_WINDOW (f)
5148 = XCreateWindow (FRAME_X_DISPLAY (f),
5149 FRAME_DISPLAY_INFO (f)->root_window,
5150 /* x, y, width, height */
5151 0, 0, 1, 1,
5152 /* Border. */
5153 f->border_width,
5154 CopyFromParent, InputOutput, CopyFromParent,
5155 mask, &attrs);
5156 XChangeProperty (FRAME_X_DISPLAY (f), tip_window,
5157 FRAME_DISPLAY_INFO (f)->Xatom_net_window_type,
5158 XA_ATOM, 32, PropModeReplace,
5159 (unsigned char *)&type, 1);
5160 unblock_input ();
5163 x_make_gc (f);
5165 x_default_parameter (f, parms, Qauto_raise, Qnil,
5166 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5167 x_default_parameter (f, parms, Qauto_lower, Qnil,
5168 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5169 x_default_parameter (f, parms, Qcursor_type, Qbox,
5170 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5172 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
5173 Change will not be effected unless different from the current
5174 FRAME_LINES (f). */
5175 width = FRAME_COLS (f);
5176 height = FRAME_LINES (f);
5177 SET_FRAME_COLS (f, 0);
5178 SET_FRAME_LINES (f, 0);
5179 change_frame_size (f, width, height, true, false, false, false);
5181 /* Add `tooltip' frame parameter's default value. */
5182 if (NILP (Fframe_parameter (frame, Qtooltip)))
5184 AUTO_FRAME_ARG (arg, Qtooltip, Qt);
5185 Fmodify_frame_parameters (frame, arg);
5188 /* FIXME - can this be done in a similar way to normal frames?
5189 http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00641.html */
5191 /* Set the `display-type' frame parameter before setting up faces. */
5193 Lisp_Object disptype;
5195 if (FRAME_DISPLAY_INFO (f)->n_planes == 1)
5196 disptype = intern ("mono");
5197 else if (FRAME_DISPLAY_INFO (f)->visual->class == GrayScale
5198 || FRAME_DISPLAY_INFO (f)->visual->class == StaticGray)
5199 disptype = intern ("grayscale");
5200 else
5201 disptype = intern ("color");
5203 if (NILP (Fframe_parameter (frame, Qdisplay_type)))
5205 AUTO_FRAME_ARG (arg, Qdisplay_type, disptype);
5206 Fmodify_frame_parameters (frame, arg);
5210 /* Set up faces after all frame parameters are known. This call
5211 also merges in face attributes specified for new frames.
5213 Frame parameters may be changed if .Xdefaults contains
5214 specifications for the default font. For example, if there is an
5215 `Emacs.default.attributeBackground: pink', the `background-color'
5216 attribute of the frame get's set, which let's the internal border
5217 of the tooltip frame appear in pink. Prevent this. */
5219 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
5221 /* Set tip_frame here, so that */
5222 tip_frame = frame;
5223 call2 (Qface_set_after_frame_default, frame, Qnil);
5225 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
5227 AUTO_FRAME_ARG (arg, Qbackground_color, bg);
5228 Fmodify_frame_parameters (frame, arg);
5232 f->no_split = true;
5234 UNGCPRO;
5236 /* Now that the frame will be official, it counts as a reference to
5237 its display and terminal. */
5238 FRAME_DISPLAY_INFO (f)->reference_count++;
5239 f->terminal->reference_count++;
5241 /* It is now ok to make the frame official even if we get an error
5242 below. And the frame needs to be on Vframe_list or making it
5243 visible won't work. */
5244 Vframe_list = Fcons (frame, Vframe_list);
5245 f->can_x_set_window_size = true;
5247 /* Setting attributes of faces of the tooltip frame from resources
5248 and similar will set face_change, which leads to the clearing of
5249 all current matrices. Since this isn't necessary here, avoid it
5250 by resetting face_change to the value it had before we created
5251 the tip frame. */
5252 face_change = face_change_before;
5254 /* Discard the unwind_protect. */
5255 return unbind_to (count, frame);
5259 /* Compute where to display tip frame F. PARMS is the list of frame
5260 parameters for F. DX and DY are specified offsets from the current
5261 location of the mouse. WIDTH and HEIGHT are the width and height
5262 of the tooltip. Return coordinates relative to the root window of
5263 the display in *ROOT_X, and *ROOT_Y. */
5265 static void
5266 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)
5268 Lisp_Object left, top;
5269 int win_x, win_y;
5270 Window root, child;
5271 unsigned pmask;
5273 /* User-specified position? */
5274 left = Fcdr (Fassq (Qleft, parms));
5275 top = Fcdr (Fassq (Qtop, parms));
5277 /* Move the tooltip window where the mouse pointer is. Resize and
5278 show it. */
5279 if (!INTEGERP (left) || !INTEGERP (top))
5281 block_input ();
5282 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_DISPLAY_INFO (f)->root_window,
5283 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
5284 unblock_input ();
5287 if (INTEGERP (top))
5288 *root_y = XINT (top);
5289 else if (*root_y + XINT (dy) <= 0)
5290 *root_y = 0; /* Can happen for negative dy */
5291 else if (*root_y + XINT (dy) + height
5292 <= x_display_pixel_height (FRAME_DISPLAY_INFO (f)))
5293 /* It fits below the pointer */
5294 *root_y += XINT (dy);
5295 else if (height + XINT (dy) <= *root_y)
5296 /* It fits above the pointer. */
5297 *root_y -= height + XINT (dy);
5298 else
5299 /* Put it on the top. */
5300 *root_y = 0;
5302 if (INTEGERP (left))
5303 *root_x = XINT (left);
5304 else if (*root_x + XINT (dx) <= 0)
5305 *root_x = 0; /* Can happen for negative dx */
5306 else if (*root_x + XINT (dx) + width
5307 <= x_display_pixel_width (FRAME_DISPLAY_INFO (f)))
5308 /* It fits to the right of the pointer. */
5309 *root_x += XINT (dx);
5310 else if (width + XINT (dx) <= *root_x)
5311 /* It fits to the left of the pointer. */
5312 *root_x -= width + XINT (dx);
5313 else
5314 /* Put it left-justified on the screen--it ought to fit that way. */
5315 *root_x = 0;
5319 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
5320 doc: /* Show STRING in a "tooltip" window on frame FRAME.
5321 A tooltip window is a small X window displaying a string.
5323 This is an internal function; Lisp code should call `tooltip-show'.
5325 FRAME nil or omitted means use the selected frame.
5327 PARMS is an optional list of frame parameters which can be used to
5328 change the tooltip's appearance.
5330 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
5331 means use the default timeout of 5 seconds.
5333 If the list of frame parameters PARMS contains a `left' parameters,
5334 the tooltip is displayed at that x-position. Otherwise it is
5335 displayed at the mouse position, with offset DX added (default is 5 if
5336 DX isn't specified). Likewise for the y-position; if a `top' frame
5337 parameter is specified, it determines the y-position of the tooltip
5338 window, otherwise it is displayed at the mouse position, with offset
5339 DY added (default is -10).
5341 A tooltip's maximum size is specified by `x-max-tooltip-size'.
5342 Text larger than the specified size is clipped. */)
5343 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
5345 struct frame *f;
5346 struct window *w;
5347 int root_x, root_y;
5348 struct buffer *old_buffer;
5349 struct text_pos pos;
5350 int i, width, height;
5351 bool seen_reversed_p;
5352 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5353 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5354 ptrdiff_t count = SPECPDL_INDEX ();
5356 specbind (Qinhibit_redisplay, Qt);
5358 GCPRO4 (string, parms, frame, timeout);
5360 CHECK_STRING (string);
5361 if (SCHARS (string) == 0)
5362 string = make_unibyte_string (" ", 1);
5364 f = decode_window_system_frame (frame);
5365 if (NILP (timeout))
5366 timeout = make_number (5);
5367 else
5368 CHECK_NATNUM (timeout);
5370 if (NILP (dx))
5371 dx = make_number (5);
5372 else
5373 CHECK_NUMBER (dx);
5375 if (NILP (dy))
5376 dy = make_number (-10);
5377 else
5378 CHECK_NUMBER (dy);
5380 #ifdef USE_GTK
5381 if (x_gtk_use_system_tooltips)
5383 bool ok;
5385 /* Hide a previous tip, if any. */
5386 Fx_hide_tip ();
5388 block_input ();
5389 ok = xg_prepare_tooltip (f, string, &width, &height);
5390 if (ok)
5392 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5393 xg_show_tooltip (f, root_x, root_y);
5394 /* This is used in Fx_hide_tip. */
5395 XSETFRAME (tip_frame, f);
5397 unblock_input ();
5398 if (ok) goto start_timer;
5400 #endif /* USE_GTK */
5402 if (NILP (last_show_tip_args))
5403 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5405 if (!NILP (tip_frame))
5407 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5408 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5409 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5411 if (EQ (frame, last_frame)
5412 && !NILP (Fequal (last_string, string))
5413 && !NILP (Fequal (last_parms, parms)))
5415 struct frame *tip_f = XFRAME (tip_frame);
5417 /* Only DX and DY have changed. */
5418 if (!NILP (tip_timer))
5420 Lisp_Object timer = tip_timer;
5421 tip_timer = Qnil;
5422 call1 (Qcancel_timer, timer);
5425 block_input ();
5426 compute_tip_xy (tip_f, parms, dx, dy, FRAME_PIXEL_WIDTH (tip_f),
5427 FRAME_PIXEL_HEIGHT (tip_f), &root_x, &root_y);
5428 XMoveWindow (FRAME_X_DISPLAY (tip_f), FRAME_X_WINDOW (tip_f),
5429 root_x, root_y);
5430 unblock_input ();
5431 goto start_timer;
5435 /* Hide a previous tip, if any. */
5436 Fx_hide_tip ();
5438 ASET (last_show_tip_args, 0, string);
5439 ASET (last_show_tip_args, 1, frame);
5440 ASET (last_show_tip_args, 2, parms);
5442 /* Add default values to frame parameters. */
5443 if (NILP (Fassq (Qname, parms)))
5444 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5445 if (NILP (Fassq (Qinternal_border_width, parms)))
5446 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5447 if (NILP (Fassq (Qborder_width, parms)))
5448 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5449 if (NILP (Fassq (Qbottom_divider_width, parms)))
5450 parms = Fcons (Fcons (Qbottom_divider_width, make_number (0)), parms);
5451 if (NILP (Fassq (Qright_divider_width, parms)))
5452 parms = Fcons (Fcons (Qright_divider_width, make_number (0)), parms);
5453 if (NILP (Fassq (Qborder_color, parms)))
5454 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5455 if (NILP (Fassq (Qbackground_color, parms)))
5456 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5457 parms);
5459 /* Create a frame for the tooltip, and record it in the global
5460 variable tip_frame. */
5461 frame = x_create_tip_frame (FRAME_DISPLAY_INFO (f), parms, string);
5462 f = XFRAME (frame);
5464 /* Set up the frame's root window. */
5465 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5466 w->left_col = 0;
5467 w->top_line = 0;
5468 w->pixel_left = 0;
5469 w->pixel_top = 0;
5471 if (CONSP (Vx_max_tooltip_size)
5472 && RANGED_INTEGERP (1, XCAR (Vx_max_tooltip_size), INT_MAX)
5473 && RANGED_INTEGERP (1, XCDR (Vx_max_tooltip_size), INT_MAX))
5475 w->total_cols = XFASTINT (XCAR (Vx_max_tooltip_size));
5476 w->total_lines = XFASTINT (XCDR (Vx_max_tooltip_size));
5478 else
5480 w->total_cols = 80;
5481 w->total_lines = 40;
5484 w->pixel_width = w->total_cols * FRAME_COLUMN_WIDTH (f);
5485 w->pixel_height = w->total_lines * FRAME_LINE_HEIGHT (f);
5487 FRAME_TOTAL_COLS (f) = w->total_cols;
5488 adjust_frame_glyphs (f);
5489 w->pseudo_window_p = true;
5491 /* Display the tooltip text in a temporary buffer. */
5492 old_buffer = current_buffer;
5493 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->contents));
5494 bset_truncate_lines (current_buffer, Qnil);
5495 clear_glyph_matrix (w->desired_matrix);
5496 clear_glyph_matrix (w->current_matrix);
5497 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5498 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
5500 /* Compute width and height of the tooltip. */
5501 width = height = 0;
5502 seen_reversed_p = false;
5503 for (i = 0; i < w->desired_matrix->nrows; ++i)
5505 struct glyph_row *row = &w->desired_matrix->rows[i];
5506 struct glyph *last;
5507 int row_width;
5509 /* Stop at the first empty row at the end. */
5510 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5511 break;
5513 /* Let the row go over the full width of the frame. */
5514 row->full_width_p = true;
5516 row_width = row->pixel_width;
5517 if (row->used[TEXT_AREA])
5519 /* There's a glyph at the end of rows that is used to place
5520 the cursor there. Don't include the width of this glyph. */
5521 if (!row->reversed_p)
5523 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5524 if (NILP (last->object))
5525 row_width -= last->pixel_width;
5527 else
5529 /* There could be a stretch glyph at the beginning of R2L
5530 rows that is produced by extend_face_to_end_of_line.
5531 Don't count that glyph. */
5532 struct glyph *g = row->glyphs[TEXT_AREA];
5534 if (g->type == STRETCH_GLYPH && NILP (g->object))
5536 row_width -= g->pixel_width;
5537 seen_reversed_p = true;
5542 height += row->height;
5543 width = max (width, row_width);
5546 /* If we've seen partial-length R2L rows, we need to re-adjust the
5547 tool-tip frame width and redisplay it again, to avoid over-wide
5548 tips due to the stretch glyph that extends R2L lines to full
5549 width of the frame. */
5550 if (seen_reversed_p)
5552 /* w->total_cols and FRAME_TOTAL_COLS want the width in columns,
5553 not in pixels. */
5554 w->pixel_width = width;
5555 width /= WINDOW_FRAME_COLUMN_WIDTH (w);
5556 w->total_cols = width;
5557 FRAME_TOTAL_COLS (f) = width;
5558 SET_FRAME_WIDTH (f, width);
5559 adjust_frame_glyphs (f);
5560 clear_glyph_matrix (w->desired_matrix);
5561 clear_glyph_matrix (w->current_matrix);
5562 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
5563 width = height = 0;
5564 /* Recompute width and height of the tooltip. */
5565 for (i = 0; i < w->desired_matrix->nrows; ++i)
5567 struct glyph_row *row = &w->desired_matrix->rows[i];
5568 struct glyph *last;
5569 int row_width;
5571 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5572 break;
5573 row->full_width_p = true;
5574 row_width = row->pixel_width;
5575 if (row->used[TEXT_AREA] && !row->reversed_p)
5577 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5578 if (NILP (last->object))
5579 row_width -= last->pixel_width;
5582 height += row->height;
5583 width = max (width, row_width);
5587 /* Add the frame's internal border to the width and height the X
5588 window should have. */
5589 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5590 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5592 /* Move the tooltip window where the mouse pointer is. Resize and
5593 show it. */
5594 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5596 block_input ();
5597 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5598 root_x, root_y, width, height);
5599 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5600 unblock_input ();
5602 /* Draw into the window. */
5603 w->must_be_updated_p = true;
5604 update_single_window (w);
5606 /* Restore original current buffer. */
5607 set_buffer_internal_1 (old_buffer);
5608 windows_or_buffers_changed = old_windows_or_buffers_changed;
5610 start_timer:
5611 /* Let the tip disappear after timeout seconds. */
5612 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5613 intern ("x-hide-tip"));
5615 UNGCPRO;
5616 return unbind_to (count, Qnil);
5620 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5621 doc: /* Hide the current tooltip window, if there is any.
5622 Value is t if tooltip was open, nil otherwise. */)
5623 (void)
5625 ptrdiff_t count;
5626 Lisp_Object deleted, frame, timer;
5627 struct gcpro gcpro1, gcpro2;
5629 /* Return quickly if nothing to do. */
5630 if (NILP (tip_timer) && NILP (tip_frame))
5631 return Qnil;
5633 frame = tip_frame;
5634 timer = tip_timer;
5635 GCPRO2 (frame, timer);
5636 tip_frame = tip_timer = deleted = Qnil;
5638 count = SPECPDL_INDEX ();
5639 specbind (Qinhibit_redisplay, Qt);
5640 specbind (Qinhibit_quit, Qt);
5642 if (!NILP (timer))
5643 call1 (Qcancel_timer, timer);
5645 #ifdef USE_GTK
5647 /* When using system tooltip, tip_frame is the Emacs frame on which
5648 the tip is shown. */
5649 struct frame *f = XFRAME (frame);
5650 if (FRAME_LIVE_P (f) && xg_hide_tooltip (f))
5651 frame = Qnil;
5653 #endif
5655 if (FRAMEP (frame))
5657 delete_frame (frame, Qnil);
5658 deleted = Qt;
5660 #ifdef USE_LUCID
5661 /* Bloodcurdling hack alert: The Lucid menu bar widget's
5662 redisplay procedure is not called when a tip frame over menu
5663 items is unmapped. Redisplay the menu manually... */
5665 Widget w;
5666 struct frame *f = SELECTED_FRAME ();
5667 w = f->output_data.x->menubar_widget;
5669 if (!DoesSaveUnders (FRAME_DISPLAY_INFO (f)->screen)
5670 && w != NULL)
5672 block_input ();
5673 xlwmenu_redisplay (w);
5674 unblock_input ();
5677 #endif /* USE_LUCID */
5680 UNGCPRO;
5681 return unbind_to (count, deleted);
5686 /***********************************************************************
5687 File selection dialog
5688 ***********************************************************************/
5690 DEFUN ("x-uses-old-gtk-dialog", Fx_uses_old_gtk_dialog,
5691 Sx_uses_old_gtk_dialog,
5692 0, 0, 0,
5693 doc: /* Return t if the old Gtk+ file selection dialog is used. */)
5694 (void)
5696 #ifdef USE_GTK
5697 if (use_dialog_box
5698 && use_file_dialog
5699 && window_system_available (SELECTED_FRAME ())
5700 && xg_uses_old_file_dialog ())
5701 return Qt;
5702 #endif
5703 return Qnil;
5707 #ifdef USE_MOTIF
5708 /* Callback for "OK" and "Cancel" on file selection dialog. */
5710 static void
5711 file_dialog_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5713 int *result = client_data;
5714 XmAnyCallbackStruct *cb = call_data;
5715 *result = cb->reason;
5719 /* Callback for unmapping a file selection dialog. This is used to
5720 capture the case where a dialog is closed via a window manager's
5721 closer button, for example. Using a XmNdestroyCallback didn't work
5722 in this case. */
5724 static void
5725 file_dialog_unmap_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5727 int *result = client_data;
5728 *result = XmCR_CANCEL;
5731 static void
5732 clean_up_file_dialog (void *arg)
5734 Widget dialog = arg;
5736 /* Clean up. */
5737 block_input ();
5738 XtUnmanageChild (dialog);
5739 XtDestroyWidget (dialog);
5740 x_menu_set_in_use (false);
5741 unblock_input ();
5745 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5746 doc: /* Read file name, prompting with PROMPT in directory DIR.
5747 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5748 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5749 or directory must exist.
5751 This function is only defined on NS, MS Windows, and X Windows with the
5752 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5753 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories.
5754 On Windows 7 and later, the file selection dialog "remembers" the last
5755 directory where the user selected a file, and will open that directory
5756 instead of DIR on subsequent invocations of this function with the same
5757 value of DIR as in previous invocations; this is standard Windows behavior. */)
5758 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename,
5759 Lisp_Object mustmatch, Lisp_Object only_dir_p)
5761 int result;
5762 struct frame *f = SELECTED_FRAME ();
5763 Lisp_Object file = Qnil;
5764 Lisp_Object decoded_file;
5765 Widget dialog, text, help;
5766 Arg al[10];
5767 int ac = 0;
5768 XmString dir_xmstring, pattern_xmstring;
5769 ptrdiff_t count = SPECPDL_INDEX ();
5770 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5772 check_window_system (f);
5774 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5776 if (popup_activated ())
5777 error ("Trying to use a menu from within a menu-entry");
5779 CHECK_STRING (prompt);
5780 CHECK_STRING (dir);
5782 /* Prevent redisplay. */
5783 specbind (Qinhibit_redisplay, Qt);
5785 block_input ();
5787 /* Create the dialog with PROMPT as title, using DIR as initial
5788 directory and using "*" as pattern. */
5789 dir = Fexpand_file_name (dir, Qnil);
5790 dir_xmstring = XmStringCreateLocalized (SSDATA (dir));
5791 pattern_xmstring = XmStringCreateLocalized ("*");
5793 XtSetArg (al[ac], XmNtitle, SDATA (prompt)); ++ac;
5794 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
5795 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
5796 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
5797 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
5798 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
5799 "fsb", al, ac);
5800 XmStringFree (dir_xmstring);
5801 XmStringFree (pattern_xmstring);
5803 /* Add callbacks for OK and Cancel. */
5804 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
5805 (XtPointer) &result);
5806 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
5807 (XtPointer) &result);
5808 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
5809 (XtPointer) &result);
5811 /* Remove the help button since we can't display help. */
5812 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
5813 XtUnmanageChild (help);
5815 /* Mark OK button as default. */
5816 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
5817 XmNshowAsDefault, True, NULL);
5819 /* If MUSTMATCH is non-nil, disable the file entry field of the
5820 dialog, so that the user must select a file from the files list
5821 box. We can't remove it because we wouldn't have a way to get at
5822 the result file name, then. */
5823 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5824 if (!NILP (mustmatch))
5826 Widget label;
5827 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
5828 XtSetSensitive (text, False);
5829 XtSetSensitive (label, False);
5832 /* Manage the dialog, so that list boxes get filled. */
5833 XtManageChild (dialog);
5835 if (STRINGP (default_filename))
5837 XmString default_xmstring;
5838 Widget wtext = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5839 Widget list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
5841 XmTextPosition last_pos = XmTextFieldGetLastPosition (wtext);
5842 XmTextFieldReplace (wtext, 0, last_pos,
5843 (SSDATA (Ffile_name_nondirectory (default_filename))));
5845 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
5846 must include the path for this to work. */
5848 default_xmstring = XmStringCreateLocalized (SSDATA (default_filename));
5850 if (XmListItemExists (list, default_xmstring))
5852 int item_pos = XmListItemPos (list, default_xmstring);
5853 /* Select the item and scroll it into view. */
5854 XmListSelectPos (list, item_pos, True);
5855 XmListSetPos (list, item_pos);
5858 XmStringFree (default_xmstring);
5861 record_unwind_protect_ptr (clean_up_file_dialog, dialog);
5863 /* Process events until the user presses Cancel or OK. */
5864 x_menu_set_in_use (true);
5865 result = 0;
5866 while (result == 0)
5868 XEvent event;
5869 x_menu_wait_for_event (0);
5870 XtAppNextEvent (Xt_app_con, &event);
5871 if (event.type == KeyPress
5872 && FRAME_X_DISPLAY (f) == event.xkey.display)
5874 KeySym keysym = XLookupKeysym (&event.xkey, 0);
5876 /* Pop down on C-g. */
5877 if (keysym == XK_g && (event.xkey.state & ControlMask) != 0)
5878 XtUnmanageChild (dialog);
5881 (void) x_dispatch_event (&event, FRAME_X_DISPLAY (f));
5884 /* Get the result. */
5885 if (result == XmCR_OK)
5887 XmString text_string;
5888 String data;
5890 XtVaGetValues (dialog, XmNtextString, &text_string, NULL);
5891 XmStringGetLtoR (text_string, XmFONTLIST_DEFAULT_TAG, &data);
5892 XmStringFree (text_string);
5893 file = build_string (data);
5894 XtFree (data);
5896 else
5897 file = Qnil;
5899 unblock_input ();
5900 UNGCPRO;
5902 /* Make "Cancel" equivalent to C-g. */
5903 if (NILP (file))
5904 Fsignal (Qquit, Qnil);
5906 decoded_file = DECODE_FILE (file);
5908 return unbind_to (count, decoded_file);
5911 #endif /* USE_MOTIF */
5913 #ifdef USE_GTK
5915 static void
5916 clean_up_dialog (void)
5918 x_menu_set_in_use (false);
5921 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5922 doc: /* Read file name, prompting with PROMPT in directory DIR.
5923 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5924 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5925 or directory must exist.
5927 This function is only defined on NS, MS Windows, and X Windows with the
5928 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5929 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories.
5930 On Windows 7 and later, the file selection dialog "remembers" the last
5931 directory where the user selected a file, and will open that directory
5932 instead of DIR on subsequent invocations of this function with the same
5933 value of DIR as in previous invocations; this is standard Windows behavior. */)
5934 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
5936 struct frame *f = SELECTED_FRAME ();
5937 char *fn;
5938 Lisp_Object file = Qnil;
5939 Lisp_Object decoded_file;
5940 ptrdiff_t count = SPECPDL_INDEX ();
5941 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5942 char *cdef_file;
5944 check_window_system (f);
5946 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5948 if (popup_activated ())
5949 error ("Trying to use a menu from within a menu-entry");
5951 CHECK_STRING (prompt);
5952 CHECK_STRING (dir);
5954 /* Prevent redisplay. */
5955 specbind (Qinhibit_redisplay, Qt);
5956 record_unwind_protect_void (clean_up_dialog);
5958 block_input ();
5960 if (STRINGP (default_filename))
5961 cdef_file = SSDATA (default_filename);
5962 else
5963 cdef_file = SSDATA (dir);
5965 fn = xg_get_file_name (f, SSDATA (prompt), cdef_file,
5966 ! NILP (mustmatch),
5967 ! NILP (only_dir_p));
5969 if (fn)
5971 file = build_string (fn);
5972 xfree (fn);
5975 unblock_input ();
5976 UNGCPRO;
5978 /* Make "Cancel" equivalent to C-g. */
5979 if (NILP (file))
5980 Fsignal (Qquit, Qnil);
5982 decoded_file = DECODE_FILE (file);
5984 return unbind_to (count, decoded_file);
5988 #ifdef HAVE_FREETYPE
5990 DEFUN ("x-select-font", Fx_select_font, Sx_select_font, 0, 2, 0,
5991 doc: /* Read a font using a GTK dialog.
5992 Return either a font spec (for GTK versions >= 3.2) or a string
5993 containing a GTK-style font name.
5995 FRAME is the frame on which to pop up the font chooser. If omitted or
5996 nil, it defaults to the selected frame. */)
5997 (Lisp_Object frame, Lisp_Object ignored)
5999 struct frame *f = decode_window_system_frame (frame);
6000 Lisp_Object font;
6001 Lisp_Object font_param;
6002 char *default_name = NULL;
6003 struct gcpro gcpro1, gcpro2;
6004 ptrdiff_t count = SPECPDL_INDEX ();
6006 if (popup_activated ())
6007 error ("Trying to use a menu from within a menu-entry");
6009 /* Prevent redisplay. */
6010 specbind (Qinhibit_redisplay, Qt);
6011 record_unwind_protect_void (clean_up_dialog);
6013 block_input ();
6015 GCPRO2 (font_param, font);
6017 XSETFONT (font, FRAME_FONT (f));
6018 font_param = Ffont_get (font, intern (":name"));
6019 if (STRINGP (font_param))
6020 default_name = xlispstrdup (font_param);
6021 else
6023 font_param = Fframe_parameter (frame, Qfont_param);
6024 if (STRINGP (font_param))
6025 default_name = xlispstrdup (font_param);
6028 font = xg_get_font (f, default_name);
6029 xfree (default_name);
6031 unblock_input ();
6033 if (NILP (font))
6034 Fsignal (Qquit, Qnil);
6036 return unbind_to (count, font);
6038 #endif /* HAVE_FREETYPE */
6040 #endif /* USE_GTK */
6043 /***********************************************************************
6044 Keyboard
6045 ***********************************************************************/
6047 #ifdef HAVE_XKB
6048 #include <X11/XKBlib.h>
6049 #include <X11/keysym.h>
6050 #endif
6052 DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
6053 Sx_backspace_delete_keys_p, 0, 1, 0,
6054 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
6055 FRAME nil means use the selected frame.
6056 Value is t if we know that both keys are present, and are mapped to the
6057 usual X keysyms. Value is `lambda' if we cannot determine if both keys are
6058 present and mapped to the usual X keysyms. */)
6059 (Lisp_Object frame)
6061 #ifndef HAVE_XKB
6062 return Qlambda;
6063 #else
6064 XkbDescPtr kb;
6065 struct frame *f = decode_window_system_frame (frame);
6066 Display *dpy = FRAME_X_DISPLAY (f);
6067 Lisp_Object have_keys;
6068 int major, minor, op, event, error_code;
6070 block_input ();
6072 /* Check library version in case we're dynamically linked. */
6073 major = XkbMajorVersion;
6074 minor = XkbMinorVersion;
6075 if (!XkbLibraryVersion (&major, &minor))
6077 unblock_input ();
6078 return Qlambda;
6081 /* Check that the server supports XKB. */
6082 major = XkbMajorVersion;
6083 minor = XkbMinorVersion;
6084 if (!XkbQueryExtension (dpy, &op, &event, &error_code, &major, &minor))
6086 unblock_input ();
6087 return Qlambda;
6090 /* In this code we check that the keyboard has physical keys with names
6091 that start with BKSP (Backspace) and DELE (Delete), and that they
6092 generate keysym XK_BackSpace and XK_Delete respectively.
6093 This function is used to test if normal-erase-is-backspace should be
6094 turned on.
6095 An alternative approach would be to just check if XK_BackSpace and
6096 XK_Delete are mapped to any key. But if any of those are mapped to
6097 some non-intuitive key combination (Meta-Shift-Ctrl-whatever) and the
6098 user doesn't know about it, it is better to return false here.
6099 It is more obvious to the user what to do if she/he has two keys
6100 clearly marked with names/symbols and one key does something not
6101 expected (i.e. she/he then tries the other).
6102 The cases where Backspace/Delete is mapped to some other key combination
6103 are rare, and in those cases, normal-erase-is-backspace can be turned on
6104 manually. */
6106 have_keys = Qnil;
6107 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
6108 if (kb)
6110 int delete_keycode = 0, backspace_keycode = 0, i;
6112 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
6114 for (i = kb->min_key_code;
6115 (i < kb->max_key_code
6116 && (delete_keycode == 0 || backspace_keycode == 0));
6117 ++i)
6119 /* The XKB symbolic key names can be seen most easily in
6120 the PS file generated by `xkbprint -label name
6121 $DISPLAY'. */
6122 if (memcmp ("DELE", kb->names->keys[i].name, 4) == 0)
6123 delete_keycode = i;
6124 else if (memcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
6125 backspace_keycode = i;
6128 XkbFreeNames (kb, 0, True);
6131 /* As of libX11-1.6.2, XkbGetMap manual says that you should use
6132 XkbFreeClientMap to free the data returned by XkbGetMap. But
6133 this function just frees the data referenced from KB and not
6134 KB itself. To free KB as well, call XkbFreeKeyboard. */
6135 XkbFreeKeyboard (kb, XkbAllMapComponentsMask, True);
6137 if (delete_keycode
6138 && backspace_keycode
6139 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
6140 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
6141 have_keys = Qt;
6143 unblock_input ();
6144 return have_keys;
6145 #endif
6150 /***********************************************************************
6151 Printing
6152 ***********************************************************************/
6154 #ifdef USE_CAIRO
6155 DEFUN ("x-export-frames", Fx_export_frames, Sx_export_frames, 0, 2, 0,
6156 doc: /* XXX Experimental. Return image data of FRAMES in TYPE format.
6157 FRAMES should be nil (the selected frame), a frame, or a list of
6158 frames (each of which corresponds to one page). Optional arg TYPE
6159 should be either `pdf' (default), `png', `ps', or `svg'. Supported
6160 types are determined by the compile-time configuration of cairo. */)
6161 (Lisp_Object frames, Lisp_Object type)
6163 Lisp_Object result, rest, tmp;
6164 cairo_surface_type_t surface_type;
6166 if (NILP (frames))
6167 frames = selected_frame;
6168 if (!CONSP (frames))
6169 frames = list1 (frames);
6171 tmp = Qnil;
6172 for (rest = frames; CONSP (rest); rest = XCDR (rest))
6174 struct frame *f = XFRAME (XCAR (rest));
6176 if (! FRAME_LIVE_P (f) || ! FRAME_X_P (f) || ! FRAME_LIVE_P (f))
6177 error ("Invalid frame");
6179 Lisp_Object frame;
6181 XSETFRAME (frame, f);
6182 tmp = Fcons (frame, tmp);
6184 frames = Fnreverse (tmp);
6186 #ifdef CAIRO_HAS_PDF_SURFACE
6187 if (NILP (type) || EQ (type, intern ("pdf"))) /* XXX: Qpdf */
6188 surface_type = CAIRO_SURFACE_TYPE_PDF;
6189 else
6190 #endif
6191 #ifdef CAIRO_HAS_PNG_FUNCTIONS
6192 if (EQ (type, intern ("png")))
6194 if (!NILP (XCDR (frames)))
6195 error ("PNG export cannot handle multiple frames.");
6196 surface_type = CAIRO_SURFACE_TYPE_IMAGE;
6198 else
6199 #endif
6200 #ifdef CAIRO_HAS_PS_SURFACE
6201 if (EQ (type, intern ("ps")))
6202 surface_type = CAIRO_SURFACE_TYPE_PS;
6203 else
6204 #endif
6205 #ifdef CAIRO_HAS_SVG_SURFACE
6206 if (EQ (type, intern ("svg")))
6208 /* For now, we stick to SVG 1.1. */
6209 if (!NILP (XCDR (frames)))
6210 error ("SVG export cannot handle multiple frames.");
6211 surface_type = CAIRO_SURFACE_TYPE_SVG;
6213 else
6214 #endif
6215 error ("Unsupported export type");
6217 result = x_cr_export_frames (frames, surface_type);
6219 return result;
6222 #ifdef USE_GTK
6223 DEFUN ("x-page-setup-dialog", Fx_page_setup_dialog, Sx_page_setup_dialog, 0, 0, 0,
6224 doc: /* Pop up a page setup dialog.
6225 The current page setup can be obtained using `x-get-page-setup'. */)
6226 (void)
6228 block_input ();
6229 xg_page_setup_dialog ();
6230 unblock_input ();
6232 return Qnil;
6235 DEFUN ("x-get-page-setup", Fx_get_page_setup, Sx_get_page_setup, 0, 0, 0,
6236 doc: /* Return the value of the current page setup.
6237 The return value is an alist containing the following keys:
6239 orientation: page orientation (symbol `portrait', `landscape',
6240 `reverse-portrait', or `reverse-landscape').
6241 width, height: page width/height in points not including margins.
6242 left-margin, right-margin, top-margin, bottom-margin: print margins,
6243 which is the parts of the page that the printer cannot print
6244 on, in points.
6246 The paper width can be obtained as the sum of width, left-margin, and
6247 right-margin values. Likewise, the paper height is the sum of height,
6248 top-margin, and bottom-margin values. */)
6249 (void)
6251 Lisp_Object result;
6253 block_input ();
6254 result = xg_get_page_setup ();
6255 unblock_input ();
6257 return result;
6260 DEFUN ("x-print-frames-dialog", Fx_print_frames_dialog, Sx_print_frames_dialog, 0, 1, "",
6261 doc: /* Pop up a print dialog to print the current contents of FRAMES.
6262 FRAMES should be nil (the selected frame), a frame, or a list of
6263 frames (each of which corresponds to one page). Each frame should be
6264 visible. */)
6265 (Lisp_Object frames)
6267 Lisp_Object rest, tmp;
6269 if (NILP (frames))
6270 frames = selected_frame;
6271 if (!CONSP (frames))
6272 frames = list1 (frames);
6274 tmp = Qnil;
6275 for (rest = frames; CONSP (rest); rest = XCDR (rest))
6277 struct frame *f = XFRAME (XCAR (rest));
6278 if (! FRAME_LIVE_P (f) || ! FRAME_X_P (f) || ! FRAME_LIVE_P (f))
6279 error ("Invalid frame");
6280 Lisp_Object frame;
6282 XSETFRAME (frame, f);
6283 if (!EQ (Fframe_visible_p (frame), Qt))
6284 error ("Frames to be printed must be visible.");
6285 tmp = Fcons (frame, tmp);
6287 frames = Fnreverse (tmp);
6289 /* Make sure the current matrices are up-to-date. */
6290 Fredisplay (Qt);
6292 block_input ();
6293 xg_print_frames_dialog (frames);
6294 unblock_input ();
6296 return Qnil;
6298 #endif /* USE_GTK */
6299 #endif /* USE_CAIRO */
6302 /***********************************************************************
6303 Initialization
6304 ***********************************************************************/
6306 /* Keep this list in the same order as frame_parms in frame.c.
6307 Use 0 for unsupported frame parameters. */
6309 frame_parm_handler x_frame_parm_handlers[] =
6311 x_set_autoraise,
6312 x_set_autolower,
6313 x_set_background_color,
6314 x_set_border_color,
6315 x_set_border_width,
6316 x_set_cursor_color,
6317 x_set_cursor_type,
6318 x_set_font,
6319 x_set_foreground_color,
6320 x_set_icon_name,
6321 x_set_icon_type,
6322 x_set_internal_border_width,
6323 x_set_right_divider_width,
6324 x_set_bottom_divider_width,
6325 x_set_menu_bar_lines,
6326 x_set_mouse_color,
6327 x_explicitly_set_name,
6328 x_set_scroll_bar_width,
6329 x_set_scroll_bar_height,
6330 x_set_title,
6331 x_set_unsplittable,
6332 x_set_vertical_scroll_bars,
6333 x_set_horizontal_scroll_bars,
6334 x_set_visibility,
6335 x_set_tool_bar_lines,
6336 x_set_scroll_bar_foreground,
6337 x_set_scroll_bar_background,
6338 x_set_screen_gamma,
6339 x_set_line_spacing,
6340 x_set_left_fringe,
6341 x_set_right_fringe,
6342 x_set_wait_for_wm,
6343 x_set_fullscreen,
6344 x_set_font_backend,
6345 x_set_alpha,
6346 x_set_sticky,
6347 x_set_tool_bar_position,
6350 void
6351 syms_of_xfns (void)
6353 DEFSYM (Qundefined_color, "undefined-color");
6354 DEFSYM (Qcompound_text, "compound-text");
6355 DEFSYM (Qcancel_timer, "cancel-timer");
6356 DEFSYM (Qfont_param, "font-parameter");
6358 #ifdef USE_CAIRO
6359 DEFSYM (Qorientation, "orientation");
6360 DEFSYM (Qtop_margin, "top-margin");
6361 DEFSYM (Qbottom_margin, "bottom-margin");
6362 DEFSYM (Qportrait, "portrait");
6363 DEFSYM (Qlandscape, "landscape");
6364 DEFSYM (Qreverse_portrait, "reverse-portrait");
6365 DEFSYM (Qreverse_landscape, "reverse-landscape");
6366 #endif
6368 Fput (Qundefined_color, Qerror_conditions,
6369 listn (CONSTYPE_PURE, 2, Qundefined_color, Qerror));
6370 Fput (Qundefined_color, Qerror_message,
6371 build_pure_c_string ("Undefined color"));
6373 DEFVAR_LISP ("x-pointer-shape", Vx_pointer_shape,
6374 doc: /* The shape of the pointer when over text.
6375 Changing the value does not affect existing frames
6376 unless you set the mouse color. */);
6377 Vx_pointer_shape = Qnil;
6379 #if false /* This doesn't really do anything. */
6380 DEFVAR_LISP ("x-nontext-pointer-shape", Vx_nontext_pointer_shape,
6381 doc: /* The shape of the pointer when not over text.
6382 This variable takes effect when you create a new frame
6383 or when you set the mouse color. */);
6384 #endif
6385 Vx_nontext_pointer_shape = Qnil;
6387 DEFVAR_LISP ("x-hourglass-pointer-shape", Vx_hourglass_pointer_shape,
6388 doc: /* The shape of the pointer when Emacs is busy.
6389 This variable takes effect when you create a new frame
6390 or when you set the mouse color. */);
6391 Vx_hourglass_pointer_shape = Qnil;
6393 #if false /* This doesn't really do anything. */
6394 DEFVAR_LISP ("x-mode-pointer-shape", Vx_mode_pointer_shape,
6395 doc: /* The shape of the pointer when over the mode line.
6396 This variable takes effect when you create a new frame
6397 or when you set the mouse color. */);
6398 #endif
6399 Vx_mode_pointer_shape = Qnil;
6401 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
6402 Vx_sensitive_text_pointer_shape,
6403 doc: /* The shape of the pointer when over mouse-sensitive text.
6404 This variable takes effect when you create a new frame
6405 or when you set the mouse color. */);
6406 Vx_sensitive_text_pointer_shape = Qnil;
6408 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
6409 Vx_window_horizontal_drag_shape,
6410 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
6411 This variable takes effect when you create a new frame
6412 or when you set the mouse color. */);
6413 Vx_window_horizontal_drag_shape = Qnil;
6415 DEFVAR_LISP ("x-window-vertical-drag-cursor",
6416 Vx_window_vertical_drag_shape,
6417 doc: /* Pointer shape to use for indicating a window can be dragged vertically.
6418 This variable takes effect when you create a new frame
6419 or when you set the mouse color. */);
6420 Vx_window_vertical_drag_shape = Qnil;
6422 DEFVAR_LISP ("x-cursor-fore-pixel", Vx_cursor_fore_pixel,
6423 doc: /* A string indicating the foreground color of the cursor box. */);
6424 Vx_cursor_fore_pixel = Qnil;
6426 DEFVAR_LISP ("x-max-tooltip-size", Vx_max_tooltip_size,
6427 doc: /* Maximum size for tooltips.
6428 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
6429 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
6431 DEFVAR_LISP ("x-no-window-manager", Vx_no_window_manager,
6432 doc: /* Non-nil if no X window manager is in use.
6433 Emacs doesn't try to figure this out; this is always nil
6434 unless you set it to something else. */);
6435 /* We don't have any way to find this out, so set it to nil
6436 and maybe the user would like to set it to t. */
6437 Vx_no_window_manager = Qnil;
6439 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
6440 Vx_pixel_size_width_font_regexp,
6441 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
6443 Since Emacs gets width of a font matching with this regexp from
6444 PIXEL_SIZE field of the name, font finding mechanism gets faster for
6445 such a font. This is especially effective for such large fonts as
6446 Chinese, Japanese, and Korean. */);
6447 Vx_pixel_size_width_font_regexp = Qnil;
6449 /* This is not ifdef:ed, so other builds than GTK can customize it. */
6450 DEFVAR_BOOL ("x-gtk-use-old-file-dialog", x_gtk_use_old_file_dialog,
6451 doc: /* Non-nil means prompt with the old GTK file selection dialog.
6452 If nil or if the file selection dialog is not available, the new GTK file
6453 chooser is used instead. To turn off all file dialogs set the
6454 variable `use-file-dialog'. */);
6455 x_gtk_use_old_file_dialog = false;
6457 DEFVAR_BOOL ("x-gtk-show-hidden-files", x_gtk_show_hidden_files,
6458 doc: /* If non-nil, the GTK file chooser will by default show hidden files.
6459 Note that this is just the default, there is a toggle button on the file
6460 chooser to show or not show hidden files on a case by case basis. */);
6461 x_gtk_show_hidden_files = false;
6463 DEFVAR_BOOL ("x-gtk-file-dialog-help-text", x_gtk_file_dialog_help_text,
6464 doc: /* If non-nil, the GTK file chooser will show additional help text.
6465 If more space for files in the file chooser dialog is wanted, set this to nil
6466 to turn the additional text off. */);
6467 x_gtk_file_dialog_help_text = true;
6469 DEFVAR_BOOL ("x-gtk-use-system-tooltips", x_gtk_use_system_tooltips,
6470 doc: /* If non-nil with a Gtk+ built Emacs, the Gtk+ tooltip is used.
6471 Otherwise use Emacs own tooltip implementation.
6472 When using Gtk+ tooltips, the tooltip face is not used. */);
6473 x_gtk_use_system_tooltips = true;
6475 /* Tell Emacs about this window system. */
6476 Fprovide (Qx, Qnil);
6478 #ifdef USE_X_TOOLKIT
6479 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6480 #ifdef USE_MOTIF
6481 Fprovide (intern_c_string ("motif"), Qnil);
6483 DEFVAR_LISP ("motif-version-string", Vmotif_version_string,
6484 doc: /* Version info for LessTif/Motif. */);
6485 Vmotif_version_string = build_string (XmVERSION_STRING);
6486 #endif /* USE_MOTIF */
6487 #endif /* USE_X_TOOLKIT */
6489 #ifdef USE_GTK
6490 /* Provide x-toolkit also for GTK. Internally GTK does not use Xt so it
6491 is not an X toolkit in that sense (USE_X_TOOLKIT is not defined).
6492 But for a user it is a toolkit for X, and indeed, configure
6493 accepts --with-x-toolkit=gtk. */
6494 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6495 Fprovide (intern_c_string ("gtk"), Qnil);
6496 Fprovide (intern_c_string ("move-toolbar"), Qnil);
6498 DEFVAR_LISP ("gtk-version-string", Vgtk_version_string,
6499 doc: /* Version info for GTK+. */);
6501 char gtk_version[sizeof ".." + 3 * INT_STRLEN_BOUND (int)];
6502 int len = sprintf (gtk_version, "%d.%d.%d",
6503 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
6504 Vgtk_version_string = make_pure_string (gtk_version, len, len, false);
6506 #endif /* USE_GTK */
6508 #ifdef USE_CAIRO
6509 Fprovide (intern_c_string ("cairo"), Qnil);
6511 DEFVAR_LISP ("cairo-version-string", Vcairo_version_string,
6512 doc: /* Version info for cairo. */);
6514 char cairo_version[sizeof ".." + 3 * INT_STRLEN_BOUND (int)];
6515 int len = sprintf (cairo_version, "%d.%d.%d",
6516 CAIRO_VERSION_MAJOR, CAIRO_VERSION_MINOR,
6517 CAIRO_VERSION_MICRO);
6518 Vcairo_version_string = make_pure_string (cairo_version, len, len, false);
6520 #endif
6522 /* X window properties. */
6523 defsubr (&Sx_change_window_property);
6524 defsubr (&Sx_delete_window_property);
6525 defsubr (&Sx_window_property);
6527 defsubr (&Sxw_display_color_p);
6528 defsubr (&Sx_display_grayscale_p);
6529 defsubr (&Sxw_color_defined_p);
6530 defsubr (&Sxw_color_values);
6531 defsubr (&Sx_server_max_request_size);
6532 defsubr (&Sx_server_vendor);
6533 defsubr (&Sx_server_version);
6534 defsubr (&Sx_display_pixel_width);
6535 defsubr (&Sx_display_pixel_height);
6536 defsubr (&Sx_display_mm_width);
6537 defsubr (&Sx_display_mm_height);
6538 defsubr (&Sx_display_screens);
6539 defsubr (&Sx_display_planes);
6540 defsubr (&Sx_display_color_cells);
6541 defsubr (&Sx_display_visual_class);
6542 defsubr (&Sx_display_backing_store);
6543 defsubr (&Sx_display_save_under);
6544 defsubr (&Sx_display_monitor_attributes_list);
6545 defsubr (&Sx_frame_geometry);
6546 defsubr (&Sx_wm_set_size_hint);
6547 defsubr (&Sx_create_frame);
6548 defsubr (&Sx_open_connection);
6549 defsubr (&Sx_close_connection);
6550 defsubr (&Sx_display_list);
6551 defsubr (&Sx_synchronize);
6552 defsubr (&Sx_backspace_delete_keys_p);
6554 defsubr (&Sx_show_tip);
6555 defsubr (&Sx_hide_tip);
6556 tip_timer = Qnil;
6557 staticpro (&tip_timer);
6558 tip_frame = Qnil;
6559 staticpro (&tip_frame);
6561 last_show_tip_args = Qnil;
6562 staticpro (&last_show_tip_args);
6564 defsubr (&Sx_uses_old_gtk_dialog);
6565 #if defined (USE_MOTIF) || defined (USE_GTK)
6566 defsubr (&Sx_file_dialog);
6567 #endif
6569 #if defined (USE_GTK) && defined (HAVE_FREETYPE)
6570 defsubr (&Sx_select_font);
6571 #endif
6573 #ifdef USE_CAIRO
6574 defsubr (&Sx_export_frames);
6575 #ifdef USE_GTK
6576 defsubr (&Sx_page_setup_dialog);
6577 defsubr (&Sx_get_page_setup);
6578 defsubr (&Sx_print_frames_dialog);
6579 #endif
6580 #endif