(eww): Don't interpret "org/foo" as an URL.
[emacs.git] / src / xfns.c
blob4a417526dcdc9d81cf6b670450dba86618571026
1 /* Functions for the X window system.
3 Copyright (C) 1989, 1992-2015 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 #include <config.h>
21 #include <stdio.h>
22 #include <math.h>
23 #include <unistd.h>
25 #include "lisp.h"
26 #include "xterm.h"
27 #include "menu.h"
28 #include "frame.h"
29 #include "window.h"
30 #include "character.h"
31 #include "buffer.h"
32 #include "intervals.h"
33 #include "dispextern.h"
34 #include "keyboard.h"
35 #include "blockinput.h"
36 #include <epaths.h>
37 #include "charset.h"
38 #include "coding.h"
39 #include "fontset.h"
40 #include "systime.h"
41 #include "termhooks.h"
42 #include "atimer.h"
43 #include "termchar.h"
44 #include "font.h"
46 #include <sys/types.h>
47 #include <sys/stat.h>
49 #if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work. */
50 #include "bitmaps/gray.xbm"
51 #else
52 #include <X11/bitmaps/gray>
53 #endif
55 #include "xsettings.h"
57 #ifdef HAVE_XRANDR
58 #include <X11/extensions/Xrandr.h>
59 #endif
60 #ifdef HAVE_XINERAMA
61 #include <X11/extensions/Xinerama.h>
62 #endif
64 #ifdef USE_GTK
65 #include "gtkutil.h"
66 #endif
68 #ifdef USE_X_TOOLKIT
69 #include <X11/Shell.h>
71 #ifndef USE_MOTIF
72 #ifdef HAVE_XAW3D
73 #include <X11/Xaw3d/Paned.h>
74 #include <X11/Xaw3d/Label.h>
75 #else /* !HAVE_XAW3D */
76 #include <X11/Xaw/Paned.h>
77 #include <X11/Xaw/Label.h>
78 #endif /* HAVE_XAW3D */
79 #endif /* USE_MOTIF */
81 #ifdef USG
82 #undef USG /* ####KLUDGE for Solaris 2.2 and up */
83 #include <X11/Xos.h>
84 #define USG
85 #ifdef USG /* Pacify gcc -Wunused-macros. */
86 #endif
87 #else
88 #include <X11/Xos.h>
89 #endif
91 #include "widget.h"
93 #include "../lwlib/lwlib.h"
95 #ifdef USE_MOTIF
96 #include <Xm/Xm.h>
97 #include <Xm/DialogS.h>
98 #include <Xm/FileSB.h>
99 #include <Xm/List.h>
100 #include <Xm/TextF.h>
101 #endif
103 #ifdef USE_LUCID
104 #include "../lwlib/xlwmenu.h"
105 #endif
107 #if !defined (NO_EDITRES)
108 #define HACK_EDITRES
109 extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
110 #endif /* not defined NO_EDITRES */
112 /* Unique id counter for widgets created by the Lucid Widget Library. */
114 extern LWLIB_ID widget_id_tick;
116 #ifdef USE_MOTIF
118 #endif /* USE_MOTIF */
120 #endif /* USE_X_TOOLKIT */
122 #ifdef USE_GTK
124 #endif /* USE_GTK */
126 #define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
128 #ifdef GLYPH_DEBUG
129 static ptrdiff_t image_cache_refcount;
130 static int dpyinfo_refcount;
131 #endif
133 static struct x_display_info *x_display_info_for_name (Lisp_Object);
135 /* Let the user specify an X display with a Lisp object.
136 OBJECT may be nil, a frame or a terminal object.
137 nil stands for the selected frame--or, if that is not an X frame,
138 the first X display on the list. */
140 struct x_display_info *
141 check_x_display_info (Lisp_Object object)
143 struct x_display_info *dpyinfo = NULL;
145 if (NILP (object))
147 struct frame *sf = XFRAME (selected_frame);
149 if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
150 dpyinfo = FRAME_DISPLAY_INFO (sf);
151 else if (x_display_list != 0)
152 dpyinfo = x_display_list;
153 else
154 error ("X windows are not in use or not initialized");
156 else if (TERMINALP (object))
158 struct terminal *t = decode_live_terminal (object);
160 if (t->type != output_x_window)
161 error ("Terminal %d is not an X display", t->id);
163 dpyinfo = t->display_info.x;
165 else if (STRINGP (object))
166 dpyinfo = x_display_info_for_name (object);
167 else
169 struct frame *f = decode_window_system_frame (object);
170 dpyinfo = FRAME_DISPLAY_INFO (f);
173 return dpyinfo;
176 /* Store the screen positions of frame F into XPTR and YPTR.
177 These are the positions of the containing window manager window,
178 not Emacs's own window. */
180 void
181 x_real_positions (struct frame *f, int *xptr, int *yptr)
183 int win_x, win_y, outer_x IF_LINT (= 0), outer_y IF_LINT (= 0);
184 int real_x = 0, real_y = 0;
185 int had_errors = 0;
186 Window win = f->output_data.x->parent_desc;
187 Atom actual_type;
188 unsigned long actual_size, bytes_remaining;
189 int rc, actual_format;
190 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
191 long max_len = 400;
192 Display *dpy = FRAME_X_DISPLAY (f);
193 unsigned char *tmp_data = NULL;
194 Atom target_type = XA_CARDINAL;
196 block_input ();
198 x_catch_errors (dpy);
200 if (win == dpyinfo->root_window)
201 win = FRAME_OUTER_WINDOW (f);
203 /* This loop traverses up the containment tree until we hit the root
204 window. Window managers may intersect many windows between our window
205 and the root window. The window we find just before the root window
206 should be the outer WM window. */
207 for (;;)
209 Window wm_window, rootw;
210 Window *tmp_children;
211 unsigned int tmp_nchildren;
212 int success;
214 success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
215 &wm_window, &tmp_children, &tmp_nchildren);
217 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
219 /* Don't free tmp_children if XQueryTree failed. */
220 if (! success)
221 break;
223 XFree (tmp_children);
225 if (wm_window == rootw || had_errors)
226 break;
228 win = wm_window;
231 if (! had_errors)
233 unsigned int ign;
234 Window child, rootw;
236 /* Get the real coordinates for the WM window upper left corner */
237 XGetGeometry (FRAME_X_DISPLAY (f), win,
238 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
240 /* Translate real coordinates to coordinates relative to our
241 window. For our window, the upper left corner is 0, 0.
242 Since the upper left corner of the WM window is outside
243 our window, win_x and win_y will be negative:
245 ------------------ ---> x
246 | title |
247 | ----------------- v y
248 | | our window
250 XTranslateCoordinates (FRAME_X_DISPLAY (f),
252 /* From-window, to-window. */
253 FRAME_DISPLAY_INFO (f)->root_window,
254 FRAME_X_WINDOW (f),
256 /* From-position, to-position. */
257 real_x, real_y, &win_x, &win_y,
259 /* Child of win. */
260 &child);
262 if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
264 outer_x = win_x;
265 outer_y = win_y;
267 else
269 XTranslateCoordinates (FRAME_X_DISPLAY (f),
271 /* From-window, to-window. */
272 FRAME_DISPLAY_INFO (f)->root_window,
273 FRAME_OUTER_WINDOW (f),
275 /* From-position, to-position. */
276 real_x, real_y, &outer_x, &outer_y,
278 /* Child of win. */
279 &child);
282 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
286 if (dpyinfo->root_window == f->output_data.x->parent_desc)
288 /* Try _NET_FRAME_EXTENTS if our parent is the root window. */
289 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_frame_extents,
290 0, max_len, False, target_type,
291 &actual_type, &actual_format, &actual_size,
292 &bytes_remaining, &tmp_data);
294 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
295 && actual_size == 4 && actual_format == 32)
297 unsigned int ign;
298 Window rootw;
299 long *fe = (long *)tmp_data;
301 XGetGeometry (FRAME_X_DISPLAY (f), win,
302 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
303 outer_x = -fe[0];
304 outer_y = -fe[2];
305 real_x -= fe[0];
306 real_y -= fe[2];
310 if (tmp_data) XFree (tmp_data);
312 x_uncatch_errors ();
314 unblock_input ();
316 if (had_errors) return;
318 f->x_pixels_diff = -win_x;
319 f->y_pixels_diff = -win_y;
321 FRAME_X_OUTPUT (f)->x_pixels_outer_diff = -outer_x;
322 FRAME_X_OUTPUT (f)->y_pixels_outer_diff = -outer_y;
324 *xptr = real_x;
325 *yptr = real_y;
328 /* Get the mouse position in frame relative coordinates. */
330 void
331 x_relative_mouse_position (struct frame *f, int *x, int *y)
333 Window root, dummy_window;
334 int dummy;
336 eassert (FRAME_X_P (f));
338 block_input ();
340 XQueryPointer (FRAME_X_DISPLAY (f),
341 DefaultRootWindow (FRAME_X_DISPLAY (f)),
343 /* The root window which contains the pointer. */
344 &root,
346 /* Window pointer is on, not used */
347 &dummy_window,
349 /* The position on that root window. */
350 x, y,
352 /* x/y in dummy_window coordinates, not used. */
353 &dummy, &dummy,
355 /* Modifier keys and pointer buttons, about which
356 we don't care. */
357 (unsigned int *) &dummy);
359 unblock_input ();
361 /* Translate root window coordinates to window coordinates. */
362 *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
363 *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
366 /* Gamma-correct COLOR on frame F. */
368 void
369 gamma_correct (struct frame *f, XColor *color)
371 if (f->gamma)
373 color->red = pow (color->red / 65535.0, f->gamma) * 65535.0 + 0.5;
374 color->green = pow (color->green / 65535.0, f->gamma) * 65535.0 + 0.5;
375 color->blue = pow (color->blue / 65535.0, f->gamma) * 65535.0 + 0.5;
380 /* Decide if color named COLOR_NAME is valid for use on frame F. If
381 so, return the RGB values in COLOR. If ALLOC_P,
382 allocate the color. Value is false if COLOR_NAME is invalid, or
383 no color could be allocated. */
385 bool
386 x_defined_color (struct frame *f, const char *color_name,
387 XColor *color, bool alloc_p)
389 bool success_p = 0;
390 Display *dpy = FRAME_X_DISPLAY (f);
391 Colormap cmap = FRAME_X_COLORMAP (f);
393 block_input ();
394 #ifdef USE_GTK
395 success_p = xg_check_special_colors (f, color_name, color);
396 #endif
397 if (!success_p)
398 success_p = XParseColor (dpy, cmap, color_name, color) != 0;
399 if (success_p && alloc_p)
400 success_p = x_alloc_nearest_color (f, cmap, color);
401 unblock_input ();
403 return success_p;
407 /* Return the pixel color value for color COLOR_NAME on frame F. If F
408 is a monochrome frame, return MONO_COLOR regardless of what ARG says.
409 Signal an error if color can't be allocated. */
411 static int
412 x_decode_color (struct frame *f, Lisp_Object color_name, int mono_color)
414 XColor cdef;
416 CHECK_STRING (color_name);
418 #if 0 /* Don't do this. It's wrong when we're not using the default
419 colormap, it makes freeing difficult, and it's probably not
420 an important optimization. */
421 if (strcmp (SDATA (color_name), "black") == 0)
422 return BLACK_PIX_DEFAULT (f);
423 else if (strcmp (SDATA (color_name), "white") == 0)
424 return WHITE_PIX_DEFAULT (f);
425 #endif
427 /* Return MONO_COLOR for monochrome frames. */
428 if (FRAME_DISPLAY_INFO (f)->n_planes == 1)
429 return mono_color;
431 /* x_defined_color is responsible for coping with failures
432 by looking for a near-miss. */
433 if (x_defined_color (f, SSDATA (color_name), &cdef, 1))
434 return cdef.pixel;
436 signal_error ("Undefined color", color_name);
441 /* Change the `wait-for-wm' frame parameter of frame F. OLD_VALUE is
442 the previous value of that parameter, NEW_VALUE is the new value.
443 See also the comment of wait_for_wm in struct x_output. */
445 static void
446 x_set_wait_for_wm (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
448 f->output_data.x->wait_for_wm = !NILP (new_value);
451 static void
452 x_set_tool_bar_position (struct frame *f,
453 Lisp_Object new_value,
454 Lisp_Object old_value)
456 Lisp_Object choice = list4 (Qleft, Qright, Qtop, Qbottom);
458 if (!NILP (Fmemq (new_value, choice)))
460 #ifdef USE_GTK
461 if (!EQ (new_value, old_value))
463 xg_change_toolbar_position (f, new_value);
464 fset_tool_bar_position (f, new_value);
466 #else
467 if (!EQ (new_value, Qtop))
468 error ("The only supported tool bar position is top");
469 #endif
471 else
472 wrong_choice (choice, new_value);
475 #ifdef USE_GTK
477 /* Set icon from FILE for frame F. By using GTK functions the icon
478 may be any format that GdkPixbuf knows about, i.e. not just bitmaps. */
481 xg_set_icon (struct frame *f, Lisp_Object file)
483 int result = 0;
484 Lisp_Object found;
486 found = x_find_image_file (file);
488 if (! NILP (found))
490 GdkPixbuf *pixbuf;
491 GError *err = NULL;
492 char *filename = SSDATA (found);
493 block_input ();
495 pixbuf = gdk_pixbuf_new_from_file (filename, &err);
497 if (pixbuf)
499 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
500 pixbuf);
501 g_object_unref (pixbuf);
503 result = 1;
505 else
506 g_error_free (err);
508 unblock_input ();
511 return result;
515 xg_set_icon_from_xpm_data (struct frame *f, const char **data)
517 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data (data);
519 if (!pixbuf)
520 return 0;
522 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
523 g_object_unref (pixbuf);
524 return 1;
526 #endif /* USE_GTK */
529 /* Functions called only from `x_set_frame_param'
530 to set individual parameters.
532 If FRAME_X_WINDOW (f) is 0,
533 the frame is being created and its X-window does not exist yet.
534 In that case, just record the parameter's new value
535 in the standard place; do not attempt to change the window. */
537 static void
538 x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
540 struct x_output *x = f->output_data.x;
541 unsigned long fg, old_fg;
543 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
544 old_fg = FRAME_FOREGROUND_PIXEL (f);
545 FRAME_FOREGROUND_PIXEL (f) = fg;
547 if (FRAME_X_WINDOW (f) != 0)
549 Display *dpy = FRAME_X_DISPLAY (f);
551 block_input ();
552 XSetForeground (dpy, x->normal_gc, fg);
553 XSetBackground (dpy, x->reverse_gc, fg);
555 if (x->cursor_pixel == old_fg)
557 unload_color (f, x->cursor_pixel);
558 x->cursor_pixel = x_copy_color (f, fg);
559 XSetBackground (dpy, x->cursor_gc, x->cursor_pixel);
562 unblock_input ();
564 update_face_from_frame_parameter (f, Qforeground_color, arg);
566 if (FRAME_VISIBLE_P (f))
567 redraw_frame (f);
570 unload_color (f, old_fg);
573 static void
574 x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
576 struct x_output *x = f->output_data.x;
577 unsigned long bg;
579 bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
580 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
581 FRAME_BACKGROUND_PIXEL (f) = bg;
583 if (FRAME_X_WINDOW (f) != 0)
585 Display *dpy = FRAME_X_DISPLAY (f);
587 block_input ();
588 XSetBackground (dpy, x->normal_gc, bg);
589 XSetForeground (dpy, x->reverse_gc, bg);
590 XSetWindowBackground (dpy, FRAME_X_WINDOW (f), bg);
591 XSetForeground (dpy, x->cursor_gc, bg);
593 #ifdef USE_GTK
594 xg_set_background_color (f, bg);
595 #endif
597 #ifndef USE_TOOLKIT_SCROLL_BARS /* Turns out to be annoying with
598 toolkit scroll bars. */
600 Lisp_Object bar;
601 for (bar = FRAME_SCROLL_BARS (f);
602 !NILP (bar);
603 bar = XSCROLL_BAR (bar)->next)
605 Window window = XSCROLL_BAR (bar)->x_window;
606 XSetWindowBackground (dpy, window, bg);
609 #endif /* USE_TOOLKIT_SCROLL_BARS */
611 unblock_input ();
612 update_face_from_frame_parameter (f, Qbackground_color, arg);
614 if (FRAME_VISIBLE_P (f))
615 redraw_frame (f);
619 static void
620 x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
622 struct x_output *x = f->output_data.x;
623 Display *dpy = FRAME_X_DISPLAY (f);
624 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
625 Cursor hourglass_cursor, horizontal_drag_cursor, vertical_drag_cursor;
626 unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
627 unsigned long mask_color = FRAME_BACKGROUND_PIXEL (f);
629 /* Don't let pointers be invisible. */
630 if (mask_color == pixel)
632 x_free_colors (f, &pixel, 1);
633 pixel = x_copy_color (f, FRAME_FOREGROUND_PIXEL (f));
636 unload_color (f, x->mouse_pixel);
637 x->mouse_pixel = pixel;
639 block_input ();
641 /* It's not okay to crash if the user selects a screwy cursor. */
642 x_catch_errors (dpy);
644 if (!NILP (Vx_pointer_shape))
646 CHECK_NUMBER (Vx_pointer_shape);
647 cursor = XCreateFontCursor (dpy, XINT (Vx_pointer_shape));
649 else
650 cursor = XCreateFontCursor (dpy, XC_xterm);
651 x_check_errors (dpy, "bad text pointer cursor: %s");
653 if (!NILP (Vx_nontext_pointer_shape))
655 CHECK_NUMBER (Vx_nontext_pointer_shape);
656 nontext_cursor
657 = XCreateFontCursor (dpy, XINT (Vx_nontext_pointer_shape));
659 else
660 nontext_cursor = XCreateFontCursor (dpy, XC_left_ptr);
661 x_check_errors (dpy, "bad nontext pointer cursor: %s");
663 if (!NILP (Vx_hourglass_pointer_shape))
665 CHECK_NUMBER (Vx_hourglass_pointer_shape);
666 hourglass_cursor
667 = XCreateFontCursor (dpy, XINT (Vx_hourglass_pointer_shape));
669 else
670 hourglass_cursor = XCreateFontCursor (dpy, XC_watch);
671 x_check_errors (dpy, "bad hourglass pointer cursor: %s");
673 if (!NILP (Vx_mode_pointer_shape))
675 CHECK_NUMBER (Vx_mode_pointer_shape);
676 mode_cursor = XCreateFontCursor (dpy, XINT (Vx_mode_pointer_shape));
678 else
679 mode_cursor = XCreateFontCursor (dpy, XC_xterm);
680 x_check_errors (dpy, "bad modeline pointer cursor: %s");
682 if (!NILP (Vx_sensitive_text_pointer_shape))
684 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
685 hand_cursor
686 = XCreateFontCursor (dpy, XINT (Vx_sensitive_text_pointer_shape));
688 else
689 hand_cursor = XCreateFontCursor (dpy, XC_hand2);
691 if (!NILP (Vx_window_horizontal_drag_shape))
693 CHECK_TYPE_RANGED_INTEGER (unsigned, Vx_window_horizontal_drag_shape);
694 horizontal_drag_cursor
695 = XCreateFontCursor (dpy, XINT (Vx_window_horizontal_drag_shape));
697 else
698 horizontal_drag_cursor
699 = XCreateFontCursor (dpy, XC_sb_h_double_arrow);
701 if (!NILP (Vx_window_vertical_drag_shape))
703 CHECK_NUMBER (Vx_window_vertical_drag_shape);
704 vertical_drag_cursor
705 = XCreateFontCursor (dpy, XINT (Vx_window_vertical_drag_shape));
707 else
708 vertical_drag_cursor
709 = XCreateFontCursor (dpy, XC_sb_v_double_arrow);
711 /* Check and report errors with the above calls. */
712 x_check_errors (dpy, "can't set cursor shape: %s");
713 x_uncatch_errors ();
716 XColor fore_color, back_color;
718 fore_color.pixel = x->mouse_pixel;
719 x_query_color (f, &fore_color);
720 back_color.pixel = mask_color;
721 x_query_color (f, &back_color);
723 XRecolorCursor (dpy, cursor, &fore_color, &back_color);
724 XRecolorCursor (dpy, nontext_cursor, &fore_color, &back_color);
725 XRecolorCursor (dpy, mode_cursor, &fore_color, &back_color);
726 XRecolorCursor (dpy, hand_cursor, &fore_color, &back_color);
727 XRecolorCursor (dpy, hourglass_cursor, &fore_color, &back_color);
728 XRecolorCursor (dpy, horizontal_drag_cursor, &fore_color, &back_color);
729 XRecolorCursor (dpy, vertical_drag_cursor, &fore_color, &back_color);
732 if (FRAME_X_WINDOW (f) != 0)
733 XDefineCursor (dpy, FRAME_X_WINDOW (f),
734 f->output_data.x->current_cursor = cursor);
736 if (cursor != x->text_cursor
737 && x->text_cursor != 0)
738 XFreeCursor (dpy, x->text_cursor);
739 x->text_cursor = cursor;
741 if (nontext_cursor != x->nontext_cursor
742 && x->nontext_cursor != 0)
743 XFreeCursor (dpy, x->nontext_cursor);
744 x->nontext_cursor = nontext_cursor;
746 if (hourglass_cursor != x->hourglass_cursor
747 && x->hourglass_cursor != 0)
748 XFreeCursor (dpy, x->hourglass_cursor);
749 x->hourglass_cursor = hourglass_cursor;
751 if (mode_cursor != x->modeline_cursor
752 && x->modeline_cursor != 0)
753 XFreeCursor (dpy, f->output_data.x->modeline_cursor);
754 x->modeline_cursor = mode_cursor;
756 if (hand_cursor != x->hand_cursor
757 && x->hand_cursor != 0)
758 XFreeCursor (dpy, x->hand_cursor);
759 x->hand_cursor = hand_cursor;
761 if (horizontal_drag_cursor != x->horizontal_drag_cursor
762 && x->horizontal_drag_cursor != 0)
763 XFreeCursor (dpy, x->horizontal_drag_cursor);
764 x->horizontal_drag_cursor = horizontal_drag_cursor;
766 if (vertical_drag_cursor != x->vertical_drag_cursor
767 && x->vertical_drag_cursor != 0)
768 XFreeCursor (dpy, x->vertical_drag_cursor);
769 x->vertical_drag_cursor = vertical_drag_cursor;
771 XFlush (dpy);
772 unblock_input ();
774 update_face_from_frame_parameter (f, Qmouse_color, arg);
777 static void
778 x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
780 unsigned long fore_pixel, pixel;
781 bool fore_pixel_allocated_p = 0, pixel_allocated_p = 0;
782 struct x_output *x = f->output_data.x;
784 if (!NILP (Vx_cursor_fore_pixel))
786 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
787 WHITE_PIX_DEFAULT (f));
788 fore_pixel_allocated_p = 1;
790 else
791 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
793 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
794 pixel_allocated_p = 1;
796 /* Make sure that the cursor color differs from the background color. */
797 if (pixel == FRAME_BACKGROUND_PIXEL (f))
799 if (pixel_allocated_p)
801 x_free_colors (f, &pixel, 1);
802 pixel_allocated_p = 0;
805 pixel = x->mouse_pixel;
806 if (pixel == fore_pixel)
808 if (fore_pixel_allocated_p)
810 x_free_colors (f, &fore_pixel, 1);
811 fore_pixel_allocated_p = 0;
813 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
817 unload_color (f, x->cursor_foreground_pixel);
818 if (!fore_pixel_allocated_p)
819 fore_pixel = x_copy_color (f, fore_pixel);
820 x->cursor_foreground_pixel = fore_pixel;
822 unload_color (f, x->cursor_pixel);
823 if (!pixel_allocated_p)
824 pixel = x_copy_color (f, pixel);
825 x->cursor_pixel = pixel;
827 if (FRAME_X_WINDOW (f) != 0)
829 block_input ();
830 XSetBackground (FRAME_X_DISPLAY (f), x->cursor_gc, x->cursor_pixel);
831 XSetForeground (FRAME_X_DISPLAY (f), x->cursor_gc, fore_pixel);
832 unblock_input ();
834 if (FRAME_VISIBLE_P (f))
836 x_update_cursor (f, 0);
837 x_update_cursor (f, 1);
841 update_face_from_frame_parameter (f, Qcursor_color, arg);
844 /* Set the border-color of frame F to pixel value PIX.
845 Note that this does not fully take effect if done before
846 F has an x-window. */
848 static void
849 x_set_border_pixel (struct frame *f, int pix)
851 unload_color (f, f->output_data.x->border_pixel);
852 f->output_data.x->border_pixel = pix;
854 if (FRAME_X_WINDOW (f) != 0 && f->border_width > 0)
856 block_input ();
857 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), pix);
858 unblock_input ();
860 if (FRAME_VISIBLE_P (f))
861 redraw_frame (f);
865 /* Set the border-color of frame F to value described by ARG.
866 ARG can be a string naming a color.
867 The border-color is used for the border that is drawn by the X server.
868 Note that this does not fully take effect if done before
869 F has an x-window; it must be redone when the window is created.
871 Note: this is done in two routines because of the way X10 works.
873 Note: under X11, this is normally the province of the window manager,
874 and so emacs's border colors may be overridden. */
876 static void
877 x_set_border_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
879 int pix;
881 CHECK_STRING (arg);
882 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
883 x_set_border_pixel (f, pix);
884 update_face_from_frame_parameter (f, Qborder_color, arg);
888 static void
889 x_set_cursor_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
891 set_frame_cursor_types (f, arg);
894 static void
895 x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
897 int result;
899 if (STRINGP (arg))
901 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
902 return;
904 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
905 return;
907 block_input ();
908 if (NILP (arg))
909 result = x_text_icon (f,
910 SSDATA ((!NILP (f->icon_name)
911 ? f->icon_name
912 : f->name)));
913 else
914 result = x_bitmap_icon (f, arg);
916 if (result)
918 unblock_input ();
919 error ("No icon window available");
922 XFlush (FRAME_X_DISPLAY (f));
923 unblock_input ();
926 static void
927 x_set_icon_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
929 int result;
931 if (STRINGP (arg))
933 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
934 return;
936 else if (!NILP (arg) || NILP (oldval))
937 return;
939 fset_icon_name (f, arg);
941 if (f->output_data.x->icon_bitmap != 0)
942 return;
944 block_input ();
946 result = x_text_icon (f,
947 SSDATA ((!NILP (f->icon_name)
948 ? f->icon_name
949 : !NILP (f->title)
950 ? f->title
951 : f->name)));
953 if (result)
955 unblock_input ();
956 error ("No icon window available");
959 XFlush (FRAME_X_DISPLAY (f));
960 unblock_input ();
964 static void
965 x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
967 int nlines;
968 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
969 int olines = FRAME_MENU_BAR_LINES (f);
970 #endif
972 /* Right now, menu bars don't work properly in minibuf-only frames;
973 most of the commands try to apply themselves to the minibuffer
974 frame itself, and get an error because you can't switch buffers
975 in or split the minibuffer window. */
976 if (FRAME_MINIBUF_ONLY_P (f))
977 return;
979 if (TYPE_RANGED_INTEGERP (int, value))
980 nlines = XINT (value);
981 else
982 nlines = 0;
984 /* Make sure we redisplay all windows in this frame. */
985 windows_or_buffers_changed = 59;
987 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
988 FRAME_MENU_BAR_LINES (f) = 0;
989 FRAME_MENU_BAR_HEIGHT (f) = 0;
990 if (nlines)
992 FRAME_EXTERNAL_MENU_BAR (f) = 1;
993 if (FRAME_X_P (f) && f->output_data.x->menubar_widget == 0)
994 /* Make sure next redisplay shows the menu bar. */
995 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = 1;
997 else
999 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1000 free_frame_menubar (f);
1001 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1002 if (FRAME_X_P (f))
1003 f->output_data.x->menubar_widget = 0;
1005 #else /* not USE_X_TOOLKIT && not USE_GTK */
1006 FRAME_MENU_BAR_LINES (f) = nlines;
1007 FRAME_MENU_BAR_HEIGHT (f) = nlines * FRAME_LINE_HEIGHT (f);
1008 adjust_frame_size (f, -1, -1, 2, 1, Qmenu_bar_lines);
1009 if (FRAME_X_WINDOW (f))
1010 x_clear_under_internal_border (f);
1012 /* If the menu bar height gets changed, the internal border below
1013 the top margin has to be cleared. Also, if the menu bar gets
1014 larger, the area for the added lines has to be cleared except for
1015 the first menu bar line that is to be drawn later. */
1016 if (nlines != olines)
1018 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1019 int width = FRAME_PIXEL_WIDTH (f);
1020 int y;
1022 /* height can be zero here. */
1023 if (FRAME_X_WINDOW (f) && height > 0 && width > 0)
1025 y = FRAME_TOP_MARGIN_HEIGHT (f);
1027 block_input ();
1028 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1029 0, y, width, height);
1030 unblock_input ();
1033 if (nlines > 1 && nlines > olines)
1035 y = (olines == 0 ? 1 : olines) * FRAME_LINE_HEIGHT (f);
1036 height = nlines * FRAME_LINE_HEIGHT (f) - y;
1038 block_input ();
1039 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1040 0, y, width, height);
1041 unblock_input ();
1044 if (nlines == 0 && WINDOWP (f->menu_bar_window))
1045 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
1047 #endif /* not USE_X_TOOLKIT && not USE_GTK */
1048 adjust_frame_glyphs (f);
1049 run_window_configuration_change_hook (f);
1053 /* Set the number of lines used for the tool bar of frame F to VALUE.
1054 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1055 is the old number of tool bar lines. This function changes the
1056 height of all windows on frame F to match the new tool bar height.
1057 The frame's height doesn't change. */
1059 static void
1060 x_set_tool_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1062 int nlines;
1064 /* Treat tool bars like menu bars. */
1065 if (FRAME_MINIBUF_ONLY_P (f))
1066 return;
1068 /* Use VALUE only if an int >= 0. */
1069 if (RANGED_INTEGERP (0, value, INT_MAX))
1070 nlines = XFASTINT (value);
1071 else
1072 nlines = 0;
1074 x_change_tool_bar_height (f, nlines * FRAME_LINE_HEIGHT (f));
1078 /* Set the pixel height of the tool bar of frame F to HEIGHT. */
1079 void
1080 x_change_tool_bar_height (struct frame *f, int height)
1082 #ifdef USE_GTK
1083 FRAME_TOOL_BAR_LINES (f) = 0;
1084 FRAME_TOOL_BAR_HEIGHT (f) = 0;
1085 if (height)
1087 FRAME_EXTERNAL_TOOL_BAR (f) = 1;
1088 if (FRAME_X_P (f) && f->output_data.x->toolbar_widget == 0)
1089 /* Make sure next redisplay shows the tool bar. */
1090 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = 1;
1091 update_frame_tool_bar (f);
1093 else
1095 if (FRAME_EXTERNAL_TOOL_BAR (f))
1096 free_frame_tool_bar (f);
1097 FRAME_EXTERNAL_TOOL_BAR (f) = 0;
1099 #else /* !USE_GTK */
1100 int unit = FRAME_LINE_HEIGHT (f);
1101 int old_height = FRAME_TOOL_BAR_HEIGHT (f);
1102 int lines = (height + unit - 1) / unit;
1104 /* Make sure we redisplay all windows in this frame. */
1105 windows_or_buffers_changed = 60;
1108 /* Recalculate tool bar and frame text sizes. */
1109 FRAME_TOOL_BAR_HEIGHT (f) = height;
1110 FRAME_TOOL_BAR_LINES (f) = lines;
1111 /* Store the `tool-bar-lines' and `height' frame parameters. */
1112 store_frame_param (f, Qtool_bar_lines, make_number (lines));
1113 store_frame_param (f, Qheight, make_number (FRAME_LINES (f)));
1115 /* We also have to make sure that the internal border at the top of
1116 the frame, below the menu bar or tool bar, is redrawn when the
1117 tool bar disappears. This is so because the internal border is
1118 below the tool bar if one is displayed, but is below the menu bar
1119 if there isn't a tool bar. The tool bar draws into the area
1120 below the menu bar. */
1121 if (FRAME_X_WINDOW (f) && FRAME_TOOL_BAR_HEIGHT (f) == 0)
1123 clear_frame (f);
1124 clear_current_matrices (f);
1127 if ((height < old_height) && WINDOWP (f->tool_bar_window))
1128 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1130 /* Recalculate toolbar height. */
1131 f->n_tool_bar_rows = 0;
1133 adjust_frame_size (f, -1, -1, (old_height == 0 || height == 0) ? 2 : 4, 0,
1134 Qtool_bar_lines);
1136 /* adjust_frame_size might not have done anything, garbage frame
1137 here. */
1138 adjust_frame_glyphs (f);
1139 SET_FRAME_GARBAGED (f);
1140 if (FRAME_X_WINDOW (f))
1141 x_clear_under_internal_border (f);
1143 #endif /* USE_GTK */
1147 static void
1148 x_set_internal_border_width (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1150 int border;
1152 CHECK_TYPE_RANGED_INTEGER (int, arg);
1153 border = max (XINT (arg), 0);
1155 if (border != FRAME_INTERNAL_BORDER_WIDTH (f))
1157 FRAME_INTERNAL_BORDER_WIDTH (f) = border;
1159 #ifdef USE_X_TOOLKIT
1160 if (FRAME_X_OUTPUT (f)->edit_widget)
1161 widget_store_internal_border (FRAME_X_OUTPUT (f)->edit_widget);
1162 #endif
1164 if (FRAME_X_WINDOW (f) != 0)
1166 adjust_frame_size (f, -1, -1, 3, 0, Qinternal_border_width);
1168 #ifdef USE_GTK
1169 xg_clear_under_internal_border (f);
1170 #else
1171 x_clear_under_internal_border (f);
1172 #endif
1179 /* Set the foreground color for scroll bars on frame F to VALUE.
1180 VALUE should be a string, a color name. If it isn't a string or
1181 isn't a valid color name, do nothing. OLDVAL is the old value of
1182 the frame parameter. */
1184 static void
1185 x_set_scroll_bar_foreground (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1187 unsigned long pixel;
1189 if (STRINGP (value))
1190 pixel = x_decode_color (f, value, BLACK_PIX_DEFAULT (f));
1191 else
1192 pixel = -1;
1194 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
1195 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
1197 f->output_data.x->scroll_bar_foreground_pixel = pixel;
1198 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1200 /* Remove all scroll bars because they have wrong colors. */
1201 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1202 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1203 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1204 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1206 update_face_from_frame_parameter (f, Qscroll_bar_foreground, value);
1207 redraw_frame (f);
1212 /* Set the background color for scroll bars on frame F to VALUE VALUE
1213 should be a string, a color name. If it isn't a string or isn't a
1214 valid color name, do nothing. OLDVAL is the old value of the frame
1215 parameter. */
1217 static void
1218 x_set_scroll_bar_background (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1220 unsigned long pixel;
1222 if (STRINGP (value))
1223 pixel = x_decode_color (f, value, WHITE_PIX_DEFAULT (f));
1224 else
1225 pixel = -1;
1227 if (f->output_data.x->scroll_bar_background_pixel != -1)
1228 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
1230 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
1231 /* Scrollbar shadow colors. */
1232 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
1234 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
1235 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
1237 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
1239 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
1240 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
1242 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
1244 f->output_data.x->scroll_bar_background_pixel = pixel;
1245 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1247 /* Remove all scroll bars because they have wrong colors. */
1248 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1249 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1250 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1251 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1253 update_face_from_frame_parameter (f, Qscroll_bar_background, value);
1254 redraw_frame (f);
1259 /* Encode Lisp string STRING as a text in a format appropriate for
1260 XICCC (X Inter Client Communication Conventions).
1262 This can call Lisp code, so callers must GCPRO.
1264 If STRING contains only ASCII characters, do no conversion and
1265 return the string data of STRING. Otherwise, encode the text by
1266 CODING_SYSTEM, and return a newly allocated memory area which
1267 should be freed by `xfree' by a caller.
1269 Store the byte length of resulting text in *TEXT_BYTES.
1271 If the text contains only ASCII and Latin-1, store 1 in *STRING_P,
1272 which means that the `encoding' of the result can be `STRING'.
1273 Otherwise store 0 in *STRINGP, which means that the `encoding' of
1274 the result should be `COMPOUND_TEXT'. */
1276 static unsigned char *
1277 x_encode_text (Lisp_Object string, Lisp_Object coding_system,
1278 ptrdiff_t *text_bytes, int *stringp, bool *freep)
1280 int result = string_xstring_p (string);
1281 struct coding_system coding;
1283 if (result == 0)
1285 /* No multibyte character in OBJ. We need not encode it. */
1286 *text_bytes = SBYTES (string);
1287 *stringp = 1;
1288 *freep = 0;
1289 return SDATA (string);
1292 setup_coding_system (coding_system, &coding);
1293 coding.mode |= (CODING_MODE_SAFE_ENCODING | CODING_MODE_LAST_BLOCK);
1294 /* We suppress producing escape sequences for composition. */
1295 coding.common_flags &= ~CODING_ANNOTATION_MASK;
1296 coding.destination = xnmalloc (SCHARS (string), 2);
1297 coding.dst_bytes = SCHARS (string) * 2;
1298 encode_coding_object (&coding, string, 0, 0,
1299 SCHARS (string), SBYTES (string), Qnil);
1300 *text_bytes = coding.produced;
1301 *stringp = (result == 1 || !EQ (coding_system, Qcompound_text));
1302 *freep = 1;
1303 return coding.destination;
1307 /* Set the WM name to NAME for frame F. Also set the icon name.
1308 If the frame already has an icon name, use that, otherwise set the
1309 icon name to NAME. */
1311 static void
1312 x_set_name_internal (struct frame *f, Lisp_Object name)
1314 if (FRAME_X_WINDOW (f))
1316 block_input ();
1318 XTextProperty text, icon;
1319 ptrdiff_t bytes;
1320 int stringp;
1321 bool do_free_icon_value = 0, do_free_text_value = 0;
1322 Lisp_Object coding_system;
1323 Lisp_Object encoded_name;
1324 Lisp_Object encoded_icon_name;
1325 struct gcpro gcpro1;
1327 /* As ENCODE_UTF_8 may cause GC and relocation of string data,
1328 we use it before x_encode_text that may return string data. */
1329 GCPRO1 (name);
1330 encoded_name = ENCODE_UTF_8 (name);
1331 UNGCPRO;
1333 coding_system = Qcompound_text;
1334 /* Note: Encoding strategy
1336 We encode NAME by compound-text and use "COMPOUND-TEXT" in
1337 text.encoding. But, there are non-internationalized window
1338 managers which don't support that encoding. So, if NAME
1339 contains only ASCII and 8859-1 characters, encode it by
1340 iso-latin-1, and use "STRING" in text.encoding hoping that
1341 such window managers at least analyze this format correctly,
1342 i.e. treat 8-bit bytes as 8859-1 characters.
1344 We may also be able to use "UTF8_STRING" in text.encoding
1345 in the future which can encode all Unicode characters.
1346 But, for the moment, there's no way to know that the
1347 current window manager supports it or not.
1349 Either way, we also set the _NET_WM_NAME and _NET_WM_ICON_NAME
1350 properties. Per the EWMH specification, those two properties
1351 are always UTF8_STRING. This matches what gtk_window_set_title()
1352 does in the USE_GTK case. */
1353 text.value = x_encode_text (name, coding_system, &bytes,
1354 &stringp, &do_free_text_value);
1355 text.encoding = (stringp ? XA_STRING
1356 : FRAME_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1357 text.format = 8;
1358 text.nitems = bytes;
1360 if (!STRINGP (f->icon_name))
1362 icon = text;
1363 encoded_icon_name = encoded_name;
1365 else
1367 /* See the above comment "Note: Encoding strategy". */
1368 icon.value = x_encode_text (f->icon_name, coding_system, &bytes,
1369 &stringp, &do_free_icon_value);
1370 icon.encoding = (stringp ? XA_STRING
1371 : FRAME_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1372 icon.format = 8;
1373 icon.nitems = bytes;
1375 encoded_icon_name = ENCODE_UTF_8 (f->icon_name);
1378 #ifdef USE_GTK
1379 gtk_window_set_title (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1380 SSDATA (encoded_name));
1381 #else /* not USE_GTK */
1382 XSetWMName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
1383 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1384 FRAME_DISPLAY_INFO (f)->Xatom_net_wm_name,
1385 FRAME_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1386 8, PropModeReplace,
1387 SDATA (encoded_name),
1388 SBYTES (encoded_name));
1389 #endif /* not USE_GTK */
1391 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &icon);
1392 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1393 FRAME_DISPLAY_INFO (f)->Xatom_net_wm_icon_name,
1394 FRAME_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1395 8, PropModeReplace,
1396 SDATA (encoded_icon_name),
1397 SBYTES (encoded_icon_name));
1399 if (do_free_icon_value)
1400 xfree (icon.value);
1401 if (do_free_text_value)
1402 xfree (text.value);
1404 unblock_input ();
1408 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1409 x_id_name.
1411 If EXPLICIT is true, that indicates that lisp code is setting the
1412 name; if NAME is a string, set F's name to NAME and set
1413 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1415 If EXPLICIT is false, that indicates that Emacs redisplay code is
1416 suggesting a new name, which lisp code should override; if
1417 F->explicit_name is set, ignore the new name; otherwise, set it. */
1419 static void
1420 x_set_name (struct frame *f, Lisp_Object name, bool explicit)
1422 /* Make sure that requests from lisp code override requests from
1423 Emacs redisplay code. */
1424 if (explicit)
1426 /* If we're switching from explicit to implicit, we had better
1427 update the mode lines and thereby update the title. */
1428 if (f->explicit_name && NILP (name))
1429 update_mode_lines = 37;
1431 f->explicit_name = ! NILP (name);
1433 else if (f->explicit_name)
1434 return;
1436 /* If NAME is nil, set the name to the x_id_name. */
1437 if (NILP (name))
1439 /* Check for no change needed in this very common case
1440 before we do any consing. */
1441 if (!strcmp (FRAME_DISPLAY_INFO (f)->x_id_name,
1442 SSDATA (f->name)))
1443 return;
1444 name = build_string (FRAME_DISPLAY_INFO (f)->x_id_name);
1446 else
1447 CHECK_STRING (name);
1449 /* Don't change the name if it's already NAME. */
1450 if (! NILP (Fstring_equal (name, f->name)))
1451 return;
1453 fset_name (f, name);
1455 /* For setting the frame title, the title parameter should override
1456 the name parameter. */
1457 if (! NILP (f->title))
1458 name = f->title;
1460 x_set_name_internal (f, name);
1463 /* This function should be called when the user's lisp code has
1464 specified a name for the frame; the name will override any set by the
1465 redisplay code. */
1466 static void
1467 x_explicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1469 x_set_name (f, arg, 1);
1472 /* This function should be called by Emacs redisplay code to set the
1473 name; names set this way will never override names set by the user's
1474 lisp code. */
1475 void
1476 x_implicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1478 x_set_name (f, arg, 0);
1481 /* Change the title of frame F to NAME.
1482 If NAME is nil, use the frame name as the title. */
1484 static void
1485 x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
1487 /* Don't change the title if it's already NAME. */
1488 if (EQ (name, f->title))
1489 return;
1491 update_mode_lines = 38;
1493 fset_title (f, name);
1495 if (NILP (name))
1496 name = f->name;
1497 else
1498 CHECK_STRING (name);
1500 x_set_name_internal (f, name);
1503 void
1504 x_set_scroll_bar_default_width (struct frame *f)
1506 int unit = FRAME_COLUMN_WIDTH (f);
1507 #ifdef USE_TOOLKIT_SCROLL_BARS
1508 #ifdef USE_GTK
1509 int minw = xg_get_default_scrollbar_width ();
1510 #else
1511 int minw = 16;
1512 #endif
1513 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
1514 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (minw + unit - 1) / unit;
1515 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = minw;
1516 #else
1517 /* The width of a non-toolkit scrollbar is 14 pixels. */
1518 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
1519 FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
1520 = FRAME_CONFIG_SCROLL_BAR_COLS (f) * unit;
1521 #endif
1524 void
1525 x_set_scroll_bar_default_height (struct frame *f)
1527 int height = FRAME_LINE_HEIGHT (f);
1528 #ifdef USE_TOOLKIT_SCROLL_BARS
1529 #ifdef USE_GTK
1530 int min_height = xg_get_default_scrollbar_height ();
1531 #else
1532 int min_height = 16;
1533 #endif
1534 /* A minimum height of 14 doesn't look good for toolkit scroll bars. */
1535 FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) = min_height;
1536 FRAME_CONFIG_SCROLL_BAR_LINES (f) = (min_height + height - 1) / height;
1537 #else
1538 /* The height of a non-toolkit scrollbar is 14 pixels. */
1539 FRAME_CONFIG_SCROLL_BAR_LINES (f) = (14 + height - 1) / height;
1541 /* Use all of that space (aside from required margins) for the
1542 scroll bar. */
1543 FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) = 14;
1544 #endif
1548 /* Record in frame F the specified or default value according to ALIST
1549 of the parameter named PROP (a Lisp symbol). If no value is
1550 specified for PROP, look for an X default for XPROP on the frame
1551 named NAME. If that is not found either, use the value DEFLT. */
1553 static Lisp_Object
1554 x_default_scroll_bar_color_parameter (struct frame *f,
1555 Lisp_Object alist, Lisp_Object prop,
1556 const char *xprop, const char *xclass,
1557 int foreground_p)
1559 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1560 Lisp_Object tem;
1562 tem = x_get_arg (dpyinfo, alist, prop, xprop, xclass, RES_TYPE_STRING);
1563 if (EQ (tem, Qunbound))
1565 #ifdef USE_TOOLKIT_SCROLL_BARS
1567 /* See if an X resource for the scroll bar color has been
1568 specified. */
1569 AUTO_STRING (foreground, "foreground");
1570 AUTO_STRING (background, "foreground");
1571 AUTO_STRING (verticalScrollBar, "verticalScrollBar");
1572 tem = (display_x_get_resource
1573 (dpyinfo, foreground_p ? foreground : background,
1574 empty_unibyte_string,
1575 verticalScrollBar,
1576 empty_unibyte_string));
1577 if (!STRINGP (tem))
1579 /* If nothing has been specified, scroll bars will use a
1580 toolkit-dependent default. Because these defaults are
1581 difficult to get at without actually creating a scroll
1582 bar, use nil to indicate that no color has been
1583 specified. */
1584 tem = Qnil;
1587 #else /* not USE_TOOLKIT_SCROLL_BARS */
1589 tem = Qnil;
1591 #endif /* not USE_TOOLKIT_SCROLL_BARS */
1594 AUTO_FRAME_ARG (arg, prop, tem);
1595 x_set_frame_parameters (f, arg);
1596 return tem;
1602 #ifdef USE_X_TOOLKIT
1604 /* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
1605 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
1606 already be present because of the toolkit (Motif adds some of them,
1607 for example, but Xt doesn't). */
1609 static void
1610 hack_wm_protocols (struct frame *f, Widget widget)
1612 Display *dpy = XtDisplay (widget);
1613 Window w = XtWindow (widget);
1614 int need_delete = 1;
1615 int need_focus = 1;
1616 int need_save = 1;
1618 block_input ();
1620 Atom type;
1621 unsigned char *catoms;
1622 int format = 0;
1623 unsigned long nitems = 0;
1624 unsigned long bytes_after;
1626 if ((XGetWindowProperty (dpy, w,
1627 FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
1628 0, 100, False, XA_ATOM,
1629 &type, &format, &nitems, &bytes_after,
1630 &catoms)
1631 == Success)
1632 && format == 32 && type == XA_ATOM)
1634 Atom *atoms = (Atom *) catoms;
1635 while (nitems > 0)
1637 nitems--;
1638 if (atoms[nitems]
1639 == FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window)
1640 need_delete = 0;
1641 else if (atoms[nitems]
1642 == FRAME_DISPLAY_INFO (f)->Xatom_wm_take_focus)
1643 need_focus = 0;
1644 else if (atoms[nitems]
1645 == FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself)
1646 need_save = 0;
1649 if (catoms)
1650 XFree (catoms);
1653 Atom props [10];
1654 int count = 0;
1655 if (need_delete)
1656 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window;
1657 if (need_focus)
1658 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_take_focus;
1659 if (need_save)
1660 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
1661 if (count)
1662 XChangeProperty (dpy, w, FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
1663 XA_ATOM, 32, PropModeAppend,
1664 (unsigned char *) props, count);
1666 unblock_input ();
1668 #endif
1672 /* Support routines for XIC (X Input Context). */
1674 #ifdef HAVE_X_I18N
1676 static XFontSet xic_create_xfontset (struct frame *);
1677 static XIMStyle best_xim_style (XIMStyles *);
1680 /* Supported XIM styles, ordered by preference. */
1682 static const XIMStyle supported_xim_styles[] =
1684 XIMPreeditPosition | XIMStatusArea,
1685 XIMPreeditPosition | XIMStatusNothing,
1686 XIMPreeditPosition | XIMStatusNone,
1687 XIMPreeditNothing | XIMStatusArea,
1688 XIMPreeditNothing | XIMStatusNothing,
1689 XIMPreeditNothing | XIMStatusNone,
1690 XIMPreeditNone | XIMStatusArea,
1691 XIMPreeditNone | XIMStatusNothing,
1692 XIMPreeditNone | XIMStatusNone,
1697 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
1698 /* Create an X fontset on frame F with base font name BASE_FONTNAME. */
1700 static const char xic_default_fontset[] = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
1702 /* Create an Xt fontset spec from the name of a base font.
1703 If `motif' is True use the Motif syntax. */
1704 char *
1705 xic_create_fontsetname (const char *base_fontname, int motif)
1707 const char *sep = motif ? ";" : ",";
1708 char *fontsetname;
1709 char *z;
1711 /* Make a fontset name from the base font name. */
1712 if (xic_default_fontset == base_fontname)
1714 /* There is no base font name, use the default. */
1715 fontsetname = xmalloc (strlen (base_fontname) + 2);
1716 z = stpcpy (fontsetname, base_fontname);
1718 else
1720 /* Make a fontset name from the base font name.
1721 The font set will be made of the following elements:
1722 - the base font.
1723 - the base font where the charset spec is replaced by -*-*.
1724 - the same but with the family also replaced with -*-*-. */
1725 const char *p = base_fontname;
1726 ptrdiff_t i;
1728 for (i = 0; *p; p++)
1729 if (*p == '-') i++;
1730 if (i != 14)
1732 /* As the font name doesn't conform to XLFD, we can't
1733 modify it to generalize it to allcs and allfamilies.
1734 Use the specified font plus the default. */
1735 fontsetname = xmalloc (strlen (base_fontname)
1736 + strlen (xic_default_fontset) + 3);
1737 z = stpcpy (fontsetname, base_fontname);
1738 z = stpcpy (z, sep);
1739 z = stpcpy (z, xic_default_fontset);
1741 else
1743 ptrdiff_t len;
1744 const char *p1 = NULL, *p2 = NULL, *p3 = NULL;
1745 char *font_allcs = NULL;
1746 char *font_allfamilies = NULL;
1747 char *font_all = NULL;
1748 const char *allcs = "*-*-*-*-*-*-*";
1749 const char *allfamilies = "-*-*-";
1750 const char *all = "*-*-*-*-";
1751 char *base;
1753 for (i = 0, p = base_fontname; i < 8; p++)
1755 if (*p == '-')
1757 i++;
1758 if (i == 3)
1759 p1 = p + 1;
1760 else if (i == 7)
1761 p2 = p + 1;
1762 else if (i == 6)
1763 p3 = p + 1;
1766 /* If base_fontname specifies ADSTYLE, make it a
1767 wildcard. */
1768 if (*p3 != '*')
1770 ptrdiff_t diff = (p2 - p3) - 2;
1772 base = alloca (strlen (base_fontname) + 1);
1773 memcpy (base, base_fontname, p3 - base_fontname);
1774 base[p3 - base_fontname] = '*';
1775 base[(p3 - base_fontname) + 1] = '-';
1776 strcpy (base + (p3 - base_fontname) + 2, p2);
1777 p = base + (p - base_fontname) - diff;
1778 p1 = base + (p1 - base_fontname);
1779 p2 = base + (p2 - base_fontname) - diff;
1780 base_fontname = base;
1783 /* Build the font spec that matches all charsets. */
1784 len = p - base_fontname + strlen (allcs) + 1;
1785 font_allcs = alloca (len);
1786 memcpy (font_allcs, base_fontname, p - base_fontname);
1787 strcpy (font_allcs + (p - base_fontname), allcs);
1789 /* Build the font spec that matches all families and
1790 add-styles. */
1791 len = p - p1 + strlen (allcs) + strlen (allfamilies) + 1;
1792 font_allfamilies = alloca (len);
1793 strcpy (font_allfamilies, allfamilies);
1794 memcpy (font_allfamilies + strlen (allfamilies), p1, p - p1);
1795 strcpy (font_allfamilies + strlen (allfamilies) + (p - p1), allcs);
1797 /* Build the font spec that matches all. */
1798 len = p - p2 + strlen (allcs) + strlen (all) + strlen (allfamilies) + 1;
1799 font_all = alloca (len);
1800 z = stpcpy (font_all, allfamilies);
1801 z = stpcpy (z, all);
1802 memcpy (z, p2, p - p2);
1803 strcpy (z + (p - p2), allcs);
1805 /* Build the actual font set name. */
1806 len = strlen (base_fontname) + strlen (font_allcs)
1807 + strlen (font_allfamilies) + strlen (font_all) + 5;
1808 fontsetname = xmalloc (len);
1809 z = stpcpy (fontsetname, base_fontname);
1810 z = stpcpy (z, sep);
1811 z = stpcpy (z, font_allcs);
1812 z = stpcpy (z, sep);
1813 z = stpcpy (z, font_allfamilies);
1814 z = stpcpy (z, sep);
1815 z = stpcpy (z, font_all);
1818 if (motif)
1819 strcpy (z, ":");
1820 return fontsetname;
1822 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
1824 #ifdef DEBUG_XIC_FONTSET
1825 static void
1826 print_fontset_result (XFontSet xfs, char *name, char **missing_list,
1827 int missing_count)
1829 if (xfs)
1830 fprintf (stderr, "XIC Fontset created: %s\n", name);
1831 else
1833 fprintf (stderr, "XIC Fontset failed: %s\n", name);
1834 while (missing_count-- > 0)
1836 fprintf (stderr, " missing: %s\n", *missing_list);
1837 missing_list++;
1842 #endif
1844 static XFontSet
1845 xic_create_xfontset (struct frame *f)
1847 XFontSet xfs = NULL;
1848 struct font *font = FRAME_FONT (f);
1849 int pixel_size = font->pixel_size;
1850 Lisp_Object rest, frame;
1852 /* See if there is another frame already using same fontset. */
1853 FOR_EACH_FRAME (rest, frame)
1855 struct frame *cf = XFRAME (frame);
1857 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
1858 && FRAME_DISPLAY_INFO (cf) == FRAME_DISPLAY_INFO (f)
1859 && FRAME_FONT (f)
1860 && FRAME_FONT (f)->pixel_size == pixel_size)
1862 xfs = FRAME_XIC_FONTSET (cf);
1863 break;
1867 if (! xfs)
1869 char buf[256];
1870 char **missing_list;
1871 int missing_count;
1872 char *def_string;
1873 const char *xlfd_format = "-*-*-medium-r-normal--%d-*-*-*-*-*";
1875 sprintf (buf, xlfd_format, pixel_size);
1876 missing_list = NULL;
1877 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
1878 &missing_list, &missing_count, &def_string);
1879 #ifdef DEBUG_XIC_FONTSET
1880 print_fontset_result (xfs, buf, missing_list, missing_count);
1881 #endif
1882 if (missing_list)
1883 XFreeStringList (missing_list);
1884 if (! xfs)
1886 /* List of pixel sizes most likely available. Find one that
1887 is closest to pixel_size. */
1888 int sizes[] = {0, 8, 10, 11, 12, 14, 17, 18, 20, 24, 26, 34, 0};
1889 int *smaller, *larger;
1891 for (smaller = sizes; smaller[1]; smaller++)
1892 if (smaller[1] >= pixel_size)
1893 break;
1894 larger = smaller + 1;
1895 if (*larger == pixel_size)
1896 larger++;
1897 while (*smaller || *larger)
1899 int this_size;
1901 if (! *larger)
1902 this_size = *smaller--;
1903 else if (! *smaller)
1904 this_size = *larger++;
1905 else if (pixel_size - *smaller < *larger - pixel_size)
1906 this_size = *smaller--;
1907 else
1908 this_size = *larger++;
1909 sprintf (buf, xlfd_format, this_size);
1910 missing_list = NULL;
1911 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
1912 &missing_list, &missing_count, &def_string);
1913 #ifdef DEBUG_XIC_FONTSET
1914 print_fontset_result (xfs, buf, missing_list, missing_count);
1915 #endif
1916 if (missing_list)
1917 XFreeStringList (missing_list);
1918 if (xfs)
1919 break;
1922 if (! xfs)
1924 const char *last_resort = "-*-*-*-r-normal--*-*-*-*-*-*";
1926 missing_list = NULL;
1927 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), last_resort,
1928 &missing_list, &missing_count, &def_string);
1929 #ifdef DEBUG_XIC_FONTSET
1930 print_fontset_result (xfs, last_resort, missing_list, missing_count);
1931 #endif
1932 if (missing_list)
1933 XFreeStringList (missing_list);
1938 return xfs;
1941 /* Free the X fontset of frame F if it is the last frame using it. */
1943 void
1944 xic_free_xfontset (struct frame *f)
1946 Lisp_Object rest, frame;
1947 bool shared_p = 0;
1949 if (!FRAME_XIC_FONTSET (f))
1950 return;
1952 /* See if there is another frame sharing the same fontset. */
1953 FOR_EACH_FRAME (rest, frame)
1955 struct frame *cf = XFRAME (frame);
1956 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
1957 && FRAME_DISPLAY_INFO (cf) == FRAME_DISPLAY_INFO (f)
1958 && FRAME_XIC_FONTSET (cf) == FRAME_XIC_FONTSET (f))
1960 shared_p = 1;
1961 break;
1965 if (!shared_p)
1966 /* The fontset is not used anymore. It is safe to free it. */
1967 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
1969 FRAME_XIC_FONTSET (f) = NULL;
1973 /* Value is the best input style, given user preferences USER (already
1974 checked to be supported by Emacs), and styles supported by the
1975 input method XIM. */
1977 static XIMStyle
1978 best_xim_style (XIMStyles *xim)
1980 int i, j;
1981 int nr_supported = ARRAYELTS (supported_xim_styles);
1983 for (i = 0; i < nr_supported; ++i)
1984 for (j = 0; j < xim->count_styles; ++j)
1985 if (supported_xim_styles[i] == xim->supported_styles[j])
1986 return supported_xim_styles[i];
1988 /* Return the default style. */
1989 return XIMPreeditNothing | XIMStatusNothing;
1992 /* Create XIC for frame F. */
1994 void
1995 create_frame_xic (struct frame *f)
1997 XIM xim;
1998 XIC xic = NULL;
1999 XFontSet xfs = NULL;
2000 XVaNestedList status_attr = NULL;
2001 XVaNestedList preedit_attr = NULL;
2002 XRectangle s_area;
2003 XPoint spot;
2004 XIMStyle xic_style;
2006 if (FRAME_XIC (f))
2007 goto out;
2009 xim = FRAME_X_XIM (f);
2010 if (!xim)
2011 goto out;
2013 /* Determine XIC style. */
2014 xic_style = best_xim_style (FRAME_X_XIM_STYLES (f));
2016 /* Create X fontset. */
2017 if (xic_style & (XIMPreeditPosition | XIMStatusArea))
2019 xfs = xic_create_xfontset (f);
2020 if (!xfs)
2021 goto out;
2023 FRAME_XIC_FONTSET (f) = xfs;
2026 if (xic_style & XIMPreeditPosition)
2028 spot.x = 0; spot.y = 1;
2029 preedit_attr = XVaCreateNestedList (0,
2030 XNFontSet, xfs,
2031 XNForeground,
2032 FRAME_FOREGROUND_PIXEL (f),
2033 XNBackground,
2034 FRAME_BACKGROUND_PIXEL (f),
2035 (xic_style & XIMPreeditPosition
2036 ? XNSpotLocation
2037 : NULL),
2038 &spot,
2039 NULL);
2041 if (!preedit_attr)
2042 goto out;
2045 if (xic_style & XIMStatusArea)
2047 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
2048 status_attr = XVaCreateNestedList (0,
2049 XNArea,
2050 &s_area,
2051 XNFontSet,
2052 xfs,
2053 XNForeground,
2054 FRAME_FOREGROUND_PIXEL (f),
2055 XNBackground,
2056 FRAME_BACKGROUND_PIXEL (f),
2057 NULL);
2059 if (!status_attr)
2060 goto out;
2063 if (preedit_attr && status_attr)
2064 xic = XCreateIC (xim,
2065 XNInputStyle, xic_style,
2066 XNClientWindow, FRAME_X_WINDOW (f),
2067 XNFocusWindow, FRAME_X_WINDOW (f),
2068 XNStatusAttributes, status_attr,
2069 XNPreeditAttributes, preedit_attr,
2070 NULL);
2071 else if (preedit_attr)
2072 xic = XCreateIC (xim,
2073 XNInputStyle, xic_style,
2074 XNClientWindow, FRAME_X_WINDOW (f),
2075 XNFocusWindow, FRAME_X_WINDOW (f),
2076 XNPreeditAttributes, preedit_attr,
2077 NULL);
2078 else if (status_attr)
2079 xic = XCreateIC (xim,
2080 XNInputStyle, xic_style,
2081 XNClientWindow, FRAME_X_WINDOW (f),
2082 XNFocusWindow, FRAME_X_WINDOW (f),
2083 XNStatusAttributes, status_attr,
2084 NULL);
2085 else
2086 xic = XCreateIC (xim,
2087 XNInputStyle, xic_style,
2088 XNClientWindow, FRAME_X_WINDOW (f),
2089 XNFocusWindow, FRAME_X_WINDOW (f),
2090 NULL);
2092 if (!xic)
2093 goto out;
2095 FRAME_XIC (f) = xic;
2096 FRAME_XIC_STYLE (f) = xic_style;
2097 xfs = NULL; /* Don't free below. */
2099 out:
2101 if (xfs)
2102 free_frame_xic (f);
2104 if (preedit_attr)
2105 XFree (preedit_attr);
2107 if (status_attr)
2108 XFree (status_attr);
2112 /* Destroy XIC and free XIC fontset of frame F, if any. */
2114 void
2115 free_frame_xic (struct frame *f)
2117 if (FRAME_XIC (f) == NULL)
2118 return;
2120 XDestroyIC (FRAME_XIC (f));
2121 xic_free_xfontset (f);
2123 FRAME_XIC (f) = NULL;
2127 /* Place preedit area for XIC of window W's frame to specified
2128 pixel position X/Y. X and Y are relative to window W. */
2130 void
2131 xic_set_preeditarea (struct window *w, int x, int y)
2133 struct frame *f = XFRAME (w->frame);
2134 XVaNestedList attr;
2135 XPoint spot;
2137 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x) + WINDOW_LEFT_FRINGE_WIDTH (w);
2138 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
2139 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
2140 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2141 XFree (attr);
2145 /* Place status area for XIC in bottom right corner of frame F.. */
2147 void
2148 xic_set_statusarea (struct frame *f)
2150 XIC xic = FRAME_XIC (f);
2151 XVaNestedList attr;
2152 XRectangle area;
2153 XRectangle *needed;
2155 /* Negotiate geometry of status area. If input method has existing
2156 status area, use its current size. */
2157 area.x = area.y = area.width = area.height = 0;
2158 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
2159 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2160 XFree (attr);
2162 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
2163 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2164 XFree (attr);
2166 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
2168 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
2169 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2170 XFree (attr);
2173 area.width = needed->width;
2174 area.height = needed->height;
2175 area.x = FRAME_PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
2176 area.y = (FRAME_PIXEL_HEIGHT (f) - area.height
2177 - FRAME_MENUBAR_HEIGHT (f)
2178 - FRAME_TOOLBAR_TOP_HEIGHT (f)
2179 - FRAME_INTERNAL_BORDER_WIDTH (f));
2180 XFree (needed);
2182 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
2183 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2184 XFree (attr);
2188 /* Set X fontset for XIC of frame F, using base font name
2189 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
2191 void
2192 xic_set_xfontset (struct frame *f, const char *base_fontname)
2194 XVaNestedList attr;
2195 XFontSet xfs;
2197 xic_free_xfontset (f);
2199 xfs = xic_create_xfontset (f);
2201 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
2202 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
2203 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2204 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
2205 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
2206 XFree (attr);
2208 FRAME_XIC_FONTSET (f) = xfs;
2211 #endif /* HAVE_X_I18N */
2215 #ifdef USE_X_TOOLKIT
2217 /* Create and set up the X widget for frame F. */
2219 static void
2220 x_window (struct frame *f, long window_prompting, int minibuffer_only)
2222 XClassHint class_hints;
2223 XSetWindowAttributes attributes;
2224 unsigned long attribute_mask;
2225 Widget shell_widget;
2226 Widget pane_widget;
2227 Widget frame_widget;
2228 Arg al [25];
2229 int ac;
2231 block_input ();
2233 /* Use the resource name as the top-level widget name
2234 for looking up resources. Make a non-Lisp copy
2235 for the window manager, so GC relocation won't bother it.
2237 Elsewhere we specify the window name for the window manager. */
2238 f->namebuf = xstrdup (SSDATA (Vx_resource_name));
2240 ac = 0;
2241 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
2242 XtSetArg (al[ac], XtNinput, 1); ac++;
2243 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2244 XtSetArg (al[ac], XtNborderWidth, f->border_width); ac++;
2245 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2246 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2247 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2248 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
2249 applicationShellWidgetClass,
2250 FRAME_X_DISPLAY (f), al, ac);
2252 f->output_data.x->widget = shell_widget;
2253 /* maybe_set_screen_title_format (shell_widget); */
2255 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
2256 NULL, shell_widget, False,
2257 NULL, NULL, NULL, NULL);
2259 ac = 0;
2260 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2261 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2262 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2263 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2264 XtSetValues (pane_widget, al, ac);
2265 f->output_data.x->column_widget = pane_widget;
2267 /* mappedWhenManaged to false tells to the paned window to not map/unmap
2268 the emacs screen when changing menubar. This reduces flickering. */
2270 ac = 0;
2271 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2272 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
2273 XtSetArg (al[ac], XtNallowResize, 1); ac++;
2274 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
2275 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
2276 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2277 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2278 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2279 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2280 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
2281 al, ac);
2283 f->output_data.x->edit_widget = frame_widget;
2285 XtManageChild (frame_widget);
2287 /* Do some needed geometry management. */
2289 Arg gal[3];
2290 int gac = 0;
2291 int extra_borders = 0;
2292 int menubar_size
2293 = (f->output_data.x->menubar_widget
2294 ? (f->output_data.x->menubar_widget->core.height
2295 + f->output_data.x->menubar_widget->core.border_width)
2296 : 0);
2298 #if 0 /* Experimentally, we now get the right results
2299 for -geometry -0-0 without this. 24 Aug 96, rms. */
2300 if (FRAME_EXTERNAL_MENU_BAR (f))
2302 Dimension ibw = 0;
2303 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
2304 menubar_size += ibw;
2306 #endif
2308 FRAME_MENUBAR_HEIGHT (f) = menubar_size;
2310 #ifndef USE_LUCID
2311 /* Motif seems to need this amount added to the sizes
2312 specified for the shell widget. The Athena/Lucid widgets don't.
2313 Both conclusions reached experimentally. -- rms. */
2314 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
2315 &extra_borders, NULL);
2316 extra_borders *= 2;
2317 #endif
2319 f->shell_position = xmalloc (sizeof "=x++" + 4 * INT_STRLEN_BOUND (int));
2321 /* Convert our geometry parameters into a geometry string
2322 and specify it.
2323 Note that we do not specify here whether the position
2324 is a user-specified or program-specified one.
2325 We pass that information later, in x_wm_set_size_hints. */
2327 int left = f->left_pos;
2328 int xneg = window_prompting & XNegative;
2329 int top = f->top_pos;
2330 int yneg = window_prompting & YNegative;
2331 if (xneg)
2332 left = -left;
2333 if (yneg)
2334 top = -top;
2336 if (window_prompting & USPosition)
2337 sprintf (f->shell_position, "=%dx%d%c%d%c%d",
2338 FRAME_PIXEL_WIDTH (f) + extra_borders,
2339 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders,
2340 (xneg ? '-' : '+'), left,
2341 (yneg ? '-' : '+'), top);
2342 else
2344 sprintf (f->shell_position, "=%dx%d",
2345 FRAME_PIXEL_WIDTH (f) + extra_borders,
2346 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders);
2348 /* Setting x and y when the position is not specified in
2349 the geometry string will set program position in the WM hints.
2350 If Emacs had just one program position, we could set it in
2351 fallback resources, but since each make-frame call can specify
2352 different program positions, this is easier. */
2353 XtSetArg (gal[gac], XtNx, left); gac++;
2354 XtSetArg (gal[gac], XtNy, top); gac++;
2358 XtSetArg (gal[gac], XtNgeometry, f->shell_position); gac++;
2359 XtSetValues (shell_widget, gal, gac);
2362 XtManageChild (pane_widget);
2363 XtRealizeWidget (shell_widget);
2365 if (FRAME_X_EMBEDDED_P (f))
2366 XReparentWindow (FRAME_X_DISPLAY (f), XtWindow (shell_widget),
2367 f->output_data.x->parent_desc, 0, 0);
2369 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
2371 validate_x_resource_name ();
2373 class_hints.res_name = SSDATA (Vx_resource_name);
2374 class_hints.res_class = SSDATA (Vx_resource_class);
2375 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
2377 #ifdef HAVE_X_I18N
2378 FRAME_XIC (f) = NULL;
2379 if (use_xim)
2380 create_frame_xic (f);
2381 #endif
2383 f->output_data.x->wm_hints.input = True;
2384 f->output_data.x->wm_hints.flags |= InputHint;
2385 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2386 &f->output_data.x->wm_hints);
2388 hack_wm_protocols (f, shell_widget);
2390 #ifdef HACK_EDITRES
2391 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
2392 #endif
2394 /* Do a stupid property change to force the server to generate a
2395 PropertyNotify event so that the event_stream server timestamp will
2396 be initialized to something relevant to the time we created the window.
2398 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
2399 FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
2400 XA_ATOM, 32, PropModeAppend, NULL, 0);
2402 /* Make all the standard events reach the Emacs frame. */
2403 attributes.event_mask = STANDARD_EVENT_SET;
2405 #ifdef HAVE_X_I18N
2406 if (FRAME_XIC (f))
2408 /* XIM server might require some X events. */
2409 unsigned long fevent = NoEventMask;
2410 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2411 attributes.event_mask |= fevent;
2413 #endif /* HAVE_X_I18N */
2415 attribute_mask = CWEventMask;
2416 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
2417 attribute_mask, &attributes);
2419 XtMapWidget (frame_widget);
2421 /* x_set_name normally ignores requests to set the name if the
2422 requested name is the same as the current name. This is the one
2423 place where that assumption isn't correct; f->name is set, but
2424 the X server hasn't been told. */
2426 Lisp_Object name;
2427 bool explicit = f->explicit_name;
2429 f->explicit_name = 0;
2430 name = f->name;
2431 fset_name (f, Qnil);
2432 x_set_name (f, name, explicit);
2435 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2436 f->output_data.x->current_cursor
2437 = f->output_data.x->text_cursor);
2439 unblock_input ();
2441 /* This is a no-op, except under Motif. Make sure main areas are
2442 set to something reasonable, in case we get an error later. */
2443 lw_set_main_areas (pane_widget, 0, frame_widget);
2446 #else /* not USE_X_TOOLKIT */
2447 #ifdef USE_GTK
2448 static void
2449 x_window (struct frame *f)
2451 if (! xg_create_frame_widgets (f))
2452 error ("Unable to create window");
2454 #ifdef HAVE_X_I18N
2455 FRAME_XIC (f) = NULL;
2456 if (use_xim)
2458 block_input ();
2459 create_frame_xic (f);
2460 if (FRAME_XIC (f))
2462 /* XIM server might require some X events. */
2463 unsigned long fevent = NoEventMask;
2464 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2466 if (fevent != NoEventMask)
2468 XSetWindowAttributes attributes;
2469 XWindowAttributes wattr;
2470 unsigned long attribute_mask;
2472 XGetWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2473 &wattr);
2474 attributes.event_mask = wattr.your_event_mask | fevent;
2475 attribute_mask = CWEventMask;
2476 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2477 attribute_mask, &attributes);
2480 unblock_input ();
2482 #endif
2485 #else /*! USE_GTK */
2486 /* Create and set up the X window for frame F. */
2488 static void
2489 x_window (struct frame *f)
2491 XClassHint class_hints;
2492 XSetWindowAttributes attributes;
2493 unsigned long attribute_mask;
2495 attributes.background_pixel = FRAME_BACKGROUND_PIXEL (f);
2496 attributes.border_pixel = f->output_data.x->border_pixel;
2497 attributes.bit_gravity = StaticGravity;
2498 attributes.backing_store = NotUseful;
2499 attributes.save_under = True;
2500 attributes.event_mask = STANDARD_EVENT_SET;
2501 attributes.colormap = FRAME_X_COLORMAP (f);
2502 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
2503 | CWColormap);
2505 block_input ();
2506 FRAME_X_WINDOW (f)
2507 = XCreateWindow (FRAME_X_DISPLAY (f),
2508 f->output_data.x->parent_desc,
2509 f->left_pos,
2510 f->top_pos,
2511 FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f),
2512 f->border_width,
2513 CopyFromParent, /* depth */
2514 InputOutput, /* class */
2515 FRAME_X_VISUAL (f),
2516 attribute_mask, &attributes);
2518 #ifdef HAVE_X_I18N
2519 if (use_xim)
2521 create_frame_xic (f);
2522 if (FRAME_XIC (f))
2524 /* XIM server might require some X events. */
2525 unsigned long fevent = NoEventMask;
2526 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2527 attributes.event_mask |= fevent;
2528 attribute_mask = CWEventMask;
2529 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2530 attribute_mask, &attributes);
2533 #endif /* HAVE_X_I18N */
2535 validate_x_resource_name ();
2537 class_hints.res_name = SSDATA (Vx_resource_name);
2538 class_hints.res_class = SSDATA (Vx_resource_class);
2539 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
2541 /* This indicates that we use the "Passive Input" input model.
2542 Unless we do this, we don't get the Focus{In,Out} events that we
2543 need to draw the cursor correctly. Accursed bureaucrats.
2544 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
2546 f->output_data.x->wm_hints.input = True;
2547 f->output_data.x->wm_hints.flags |= InputHint;
2548 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2549 &f->output_data.x->wm_hints);
2550 f->output_data.x->wm_hints.icon_pixmap = None;
2552 /* Request "save yourself" and "delete window" commands from wm. */
2554 Atom protocols[2];
2555 protocols[0] = FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window;
2556 protocols[1] = FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
2557 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
2560 /* x_set_name normally ignores requests to set the name if the
2561 requested name is the same as the current name. This is the one
2562 place where that assumption isn't correct; f->name is set, but
2563 the X server hasn't been told. */
2565 Lisp_Object name;
2566 bool explicit = f->explicit_name;
2568 f->explicit_name = 0;
2569 name = f->name;
2570 fset_name (f, Qnil);
2571 x_set_name (f, name, explicit);
2574 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2575 f->output_data.x->current_cursor
2576 = f->output_data.x->text_cursor);
2578 unblock_input ();
2580 if (FRAME_X_WINDOW (f) == 0)
2581 error ("Unable to create window");
2584 #endif /* not USE_GTK */
2585 #endif /* not USE_X_TOOLKIT */
2587 /* Verify that the icon position args for this window are valid. */
2589 static void
2590 x_icon_verify (struct frame *f, Lisp_Object parms)
2592 Lisp_Object icon_x, icon_y;
2594 /* Set the position of the icon. Note that twm groups all
2595 icons in an icon window. */
2596 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2597 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2598 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2600 CHECK_NUMBER (icon_x);
2601 CHECK_NUMBER (icon_y);
2603 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2604 error ("Both left and top icon corners of icon must be specified");
2607 /* Handle the icon stuff for this window. Perhaps later we might
2608 want an x_set_icon_position which can be called interactively as
2609 well. */
2611 static void
2612 x_icon (struct frame *f, Lisp_Object parms)
2614 Lisp_Object icon_x, icon_y;
2615 #if 0
2616 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2617 #endif
2619 /* Set the position of the icon. Note that twm groups all
2620 icons in an icon window. */
2621 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2622 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2623 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2625 CHECK_TYPE_RANGED_INTEGER (int, icon_x);
2626 CHECK_TYPE_RANGED_INTEGER (int, icon_y);
2628 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2629 error ("Both left and top icon corners of icon must be specified");
2631 block_input ();
2633 if (! EQ (icon_x, Qunbound))
2634 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
2636 #if 0 /* x_get_arg removes the visibility parameter as a side effect,
2637 but x_create_frame still needs it. */
2638 /* Start up iconic or window? */
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, 0);
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 int minibuffer_only = 0;
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 = 1;
2962 else if (WINDOWP (tem))
2963 f = make_frame_without_minibuffer (tem, kb, display);
2964 else
2965 f = make_frame (1);
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 = 1;
3034 else
3036 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
3037 f->output_data.x->explicit_parent = 0;
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 = 0;
3047 else
3049 fset_name (f, name);
3050 f->explicit_name = 1;
3051 /* Use the frame's title when getting resources for this frame. */
3052 specbind (Qx_resource_name, name);
3055 #ifdef HAVE_FREETYPE
3056 #ifdef HAVE_XFT
3057 register_font_driver (&xftfont_driver, f);
3058 #else /* not HAVE_XFT */
3059 register_font_driver (&ftxfont_driver, f);
3060 #endif /* not HAVE_XFT */
3061 #endif /* HAVE_FREETYPE */
3062 register_font_driver (&xfont_driver, f);
3064 x_default_parameter (f, parms, Qfont_backend, Qnil,
3065 "fontBackend", "FontBackend", RES_TYPE_STRING);
3067 /* Extract the window parameters from the supplied values
3068 that are needed to determine window geometry. */
3069 x_default_font_parameter (f, parms);
3070 if (!FRAME_FONT (f))
3072 delete_frame (frame, Qnoelisp);
3073 error ("Invalid frame font");
3076 /* Frame contents get displaced if an embedded X window has a border. */
3077 if (! FRAME_X_EMBEDDED_P (f))
3078 x_default_parameter (f, parms, Qborder_width, make_number (0),
3079 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
3081 /* This defaults to 1 in order to match xterm. We recognize either
3082 internalBorderWidth or internalBorder (which is what xterm calls
3083 it). */
3084 if (NILP (Fassq (Qinternal_border_width, parms)))
3086 Lisp_Object value;
3088 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
3089 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
3090 if (! EQ (value, Qunbound))
3091 parms = Fcons (Fcons (Qinternal_border_width, value),
3092 parms);
3094 x_default_parameter (f, parms, Qinternal_border_width,
3095 #ifdef USE_GTK /* We used to impose 0 in xg_create_frame_widgets. */
3096 make_number (0),
3097 #else
3098 make_number (1),
3099 #endif
3100 "internalBorderWidth", "internalBorderWidth",
3101 RES_TYPE_NUMBER);
3102 x_default_parameter (f, parms, Qright_divider_width, make_number (0),
3103 NULL, NULL, RES_TYPE_NUMBER);
3104 x_default_parameter (f, parms, Qbottom_divider_width, make_number (0),
3105 NULL, NULL, RES_TYPE_NUMBER);
3106 x_default_parameter (f, parms, Qvertical_scroll_bars,
3107 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3108 Qright,
3109 #else
3110 Qleft,
3111 #endif
3112 "verticalScrollBars", "ScrollBars",
3113 RES_TYPE_SYMBOL);
3114 x_default_parameter (f, parms, Qhorizontal_scroll_bars, Qnil,
3115 "horizontalScrollBars", "ScrollBars",
3116 RES_TYPE_SYMBOL);
3117 /* Also do the stuff which must be set before the window exists. */
3118 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
3119 "foreground", "Foreground", RES_TYPE_STRING);
3120 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
3121 "background", "Background", RES_TYPE_STRING);
3122 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
3123 "pointerColor", "Foreground", RES_TYPE_STRING);
3124 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
3125 "borderColor", "BorderColor", RES_TYPE_STRING);
3126 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
3127 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
3128 x_default_parameter (f, parms, Qline_spacing, Qnil,
3129 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
3130 x_default_parameter (f, parms, Qleft_fringe, Qnil,
3131 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
3132 x_default_parameter (f, parms, Qright_fringe, Qnil,
3133 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
3135 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
3136 "scrollBarForeground",
3137 "ScrollBarForeground", 1);
3138 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
3139 "scrollBarBackground",
3140 "ScrollBarBackground", 0);
3142 #ifdef GLYPH_DEBUG
3143 image_cache_refcount =
3144 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
3145 dpyinfo_refcount = dpyinfo->reference_count;
3146 #endif /* GLYPH_DEBUG */
3148 /* Init faces before x_default_parameter is called for the
3149 scroll-bar-width parameter because otherwise we end up in
3150 init_iterator with a null face cache, which should not happen. */
3151 init_frame_faces (f);
3153 /* The following call of change_frame_size is needed since otherwise
3154 x_set_tool_bar_lines will already work with the character sizes
3155 installed by init_frame_faces while the frame's pixel size is
3156 still calculated from a character size of 1 and we subsequently
3157 hit the (height >= 0) assertion in window_box_height.
3159 The non-pixelwise code apparently worked around this because it
3160 had one frame line vs one toolbar line which left us with a zero
3161 root window height which was obviously wrong as well ... */
3162 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
3163 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 5, 1, Qnil);
3165 /* Set the menu-bar-lines and tool-bar-lines parameters. We don't
3166 look up the X resources controlling the menu-bar and tool-bar
3167 here; they are processed specially at startup, and reflected in
3168 the values of the mode variables. */
3170 x_default_parameter (f, parms, Qmenu_bar_lines,
3171 NILP (Vmenu_bar_mode)
3172 ? make_number (0) : make_number (1),
3173 NULL, NULL, RES_TYPE_NUMBER);
3174 x_default_parameter (f, parms, Qtool_bar_lines,
3175 NILP (Vtool_bar_mode)
3176 ? make_number (0) : make_number (1),
3177 NULL, NULL, RES_TYPE_NUMBER);
3179 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
3180 "bufferPredicate", "BufferPredicate",
3181 RES_TYPE_SYMBOL);
3182 x_default_parameter (f, parms, Qtitle, Qnil,
3183 "title", "Title", RES_TYPE_STRING);
3184 x_default_parameter (f, parms, Qwait_for_wm, Qt,
3185 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN);
3186 x_default_parameter (f, parms, Qfullscreen, Qnil,
3187 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
3188 x_default_parameter (f, parms, Qtool_bar_position,
3189 FRAME_TOOL_BAR_POSITION (f), 0, 0, RES_TYPE_SYMBOL);
3191 /* Compute the size of the X window. */
3192 window_prompting = x_figure_window_size (f, parms, 1);
3194 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
3195 f->no_split = minibuffer_only || EQ (tem, Qt);
3197 x_icon_verify (f, parms);
3199 /* Create the X widget or window. */
3200 #ifdef USE_X_TOOLKIT
3201 x_window (f, window_prompting, minibuffer_only);
3202 #else
3203 x_window (f);
3204 #endif
3206 x_icon (f, parms);
3207 x_make_gc (f);
3209 /* Now consider the frame official. */
3210 f->terminal->reference_count++;
3211 FRAME_DISPLAY_INFO (f)->reference_count++;
3212 Vframe_list = Fcons (frame, Vframe_list);
3214 /* We need to do this after creating the X window, so that the
3215 icon-creation functions can say whose icon they're describing. */
3216 x_default_parameter (f, parms, Qicon_type, Qt,
3217 "bitmapIcon", "BitmapIcon", RES_TYPE_BOOLEAN);
3219 x_default_parameter (f, parms, Qauto_raise, Qnil,
3220 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3221 x_default_parameter (f, parms, Qauto_lower, Qnil,
3222 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3223 x_default_parameter (f, parms, Qcursor_type, Qbox,
3224 "cursorType", "CursorType", RES_TYPE_SYMBOL);
3225 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
3226 "scrollBarWidth", "ScrollBarWidth",
3227 RES_TYPE_NUMBER);
3228 x_default_parameter (f, parms, Qscroll_bar_height, Qnil,
3229 "scrollBarHeight", "ScrollBarHeight",
3230 RES_TYPE_NUMBER);
3231 x_default_parameter (f, parms, Qalpha, Qnil,
3232 "alpha", "Alpha", RES_TYPE_NUMBER);
3234 /* Consider frame official, now. */
3235 f->can_x_set_window_size = true;
3237 adjust_frame_size (f, FRAME_TEXT_WIDTH (f), FRAME_TEXT_HEIGHT (f), 0, 1, Qnil);
3239 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3240 /* Create the menu bar. */
3241 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3243 /* If this signals an error, we haven't set size hints for the
3244 frame and we didn't make it visible. */
3245 initialize_frame_menubar (f);
3247 #ifndef USE_GTK
3248 /* This is a no-op, except under Motif where it arranges the
3249 main window for the widgets on it. */
3250 lw_set_main_areas (f->output_data.x->column_widget,
3251 f->output_data.x->menubar_widget,
3252 f->output_data.x->edit_widget);
3253 #endif /* not USE_GTK */
3255 #endif /* USE_X_TOOLKIT || USE_GTK */
3257 /* Tell the server what size and position, etc, we want, and how
3258 badly we want them. This should be done after we have the menu
3259 bar so that its size can be taken into account. */
3260 block_input ();
3261 x_wm_set_size_hint (f, window_prompting, 0);
3262 unblock_input ();
3264 /* Make the window appear on the frame and enable display, unless
3265 the caller says not to. However, with explicit parent, Emacs
3266 cannot control visibility, so don't try. */
3267 if (! f->output_data.x->explicit_parent)
3269 Lisp_Object visibility;
3271 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
3272 RES_TYPE_SYMBOL);
3273 if (EQ (visibility, Qunbound))
3274 visibility = Qt;
3276 if (EQ (visibility, Qicon))
3277 x_iconify_frame (f);
3278 else if (! NILP (visibility))
3279 x_make_frame_visible (f);
3280 else
3282 /* Must have been Qnil. */
3286 block_input ();
3288 /* Set machine name and pid for the purpose of window managers. */
3289 set_machine_and_pid_properties (f);
3291 /* Set the WM leader property. GTK does this itself, so this is not
3292 needed when using GTK. */
3293 if (dpyinfo->client_leader_window != 0)
3295 XChangeProperty (FRAME_X_DISPLAY (f),
3296 FRAME_OUTER_WINDOW (f),
3297 dpyinfo->Xatom_wm_client_leader,
3298 XA_WINDOW, 32, PropModeReplace,
3299 (unsigned char *) &dpyinfo->client_leader_window, 1);
3302 unblock_input ();
3304 /* Initialize `default-minibuffer-frame' in case this is the first
3305 frame on this terminal. */
3306 if (FRAME_HAS_MINIBUF_P (f)
3307 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
3308 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
3309 kset_default_minibuffer_frame (kb, frame);
3311 /* All remaining specified parameters, which have not been "used"
3312 by x_get_arg and friends, now go in the misc. alist of the frame. */
3313 for (tem = parms; CONSP (tem); tem = XCDR (tem))
3314 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
3315 fset_param_alist (f, Fcons (XCAR (tem), f->param_alist));
3317 UNGCPRO;
3319 /* Make sure windows on this frame appear in calls to next-window
3320 and similar functions. */
3321 Vwindow_list = Qnil;
3323 return unbind_to (count, frame);
3327 /* FRAME is used only to get a handle on the X display. We don't pass the
3328 display info directly because we're called from frame.c, which doesn't
3329 know about that structure. */
3331 Lisp_Object
3332 x_get_focus_frame (struct frame *frame)
3334 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (frame);
3335 Lisp_Object xfocus;
3336 if (! dpyinfo->x_focus_frame)
3337 return Qnil;
3339 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
3340 return xfocus;
3344 /* In certain situations, when the window manager follows a
3345 click-to-focus policy, there seems to be no way around calling
3346 XSetInputFocus to give another frame the input focus .
3348 In an ideal world, XSetInputFocus should generally be avoided so
3349 that applications don't interfere with the window manager's focus
3350 policy. But I think it's okay to use when it's clearly done
3351 following a user-command. */
3353 void
3354 x_focus_frame (struct frame *f)
3356 Display *dpy = FRAME_X_DISPLAY (f);
3358 block_input ();
3359 x_catch_errors (dpy);
3361 if (FRAME_X_EMBEDDED_P (f))
3363 /* For Xembedded frames, normally the embedder forwards key
3364 events. See XEmbed Protocol Specification at
3365 http://freedesktop.org/wiki/Specifications/xembed-spec */
3366 xembed_request_focus (f);
3368 else
3370 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3371 RevertToParent, CurrentTime);
3372 x_ewmh_activate_frame (f);
3375 x_uncatch_errors ();
3376 unblock_input ();
3380 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
3381 doc: /* Internal function called by `color-defined-p', which see
3382 .\(Note that the Nextstep version of this function ignores FRAME.) */)
3383 (Lisp_Object color, Lisp_Object frame)
3385 XColor foo;
3386 struct frame *f = decode_window_system_frame (frame);
3388 CHECK_STRING (color);
3390 if (x_defined_color (f, SSDATA (color), &foo, 0))
3391 return Qt;
3392 else
3393 return Qnil;
3396 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
3397 doc: /* Internal function called by `color-values', which see. */)
3398 (Lisp_Object color, Lisp_Object frame)
3400 XColor foo;
3401 struct frame *f = decode_window_system_frame (frame);
3403 CHECK_STRING (color);
3405 if (x_defined_color (f, SSDATA (color), &foo, 0))
3406 return list3i (foo.red, foo.green, foo.blue);
3407 else
3408 return Qnil;
3411 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
3412 doc: /* Internal function called by `display-color-p', which see. */)
3413 (Lisp_Object terminal)
3415 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3417 if (dpyinfo->n_planes <= 2)
3418 return Qnil;
3420 switch (dpyinfo->visual->class)
3422 case StaticColor:
3423 case PseudoColor:
3424 case TrueColor:
3425 case DirectColor:
3426 return Qt;
3428 default:
3429 return Qnil;
3433 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
3434 0, 1, 0,
3435 doc: /* Return t if the X display supports shades of gray.
3436 Note that color displays do support shades of gray.
3437 The optional argument TERMINAL specifies which display to ask about.
3438 TERMINAL should be a terminal object, a frame or a display name (a string).
3439 If omitted or nil, that stands for the selected frame's display. */)
3440 (Lisp_Object terminal)
3442 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3444 if (dpyinfo->n_planes <= 1)
3445 return Qnil;
3447 switch (dpyinfo->visual->class)
3449 case StaticColor:
3450 case PseudoColor:
3451 case TrueColor:
3452 case DirectColor:
3453 case StaticGray:
3454 case GrayScale:
3455 return Qt;
3457 default:
3458 return Qnil;
3462 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
3463 0, 1, 0,
3464 doc: /* Return the width in pixels of the X display TERMINAL.
3465 The optional argument TERMINAL specifies which display to ask about.
3466 TERMINAL should be a terminal object, a frame or a display name (a string).
3467 If omitted or nil, that stands for the selected frame's display.
3469 On \"multi-monitor\" setups this refers to the pixel width for all
3470 physical monitors associated with TERMINAL. To get information for
3471 each physical monitor, use `display-monitor-attributes-list'. */)
3472 (Lisp_Object terminal)
3474 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3476 return make_number (x_display_pixel_width (dpyinfo));
3479 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
3480 Sx_display_pixel_height, 0, 1, 0,
3481 doc: /* Return the height in pixels of the X display TERMINAL.
3482 The optional argument TERMINAL specifies which display to ask about.
3483 TERMINAL should be a terminal object, a frame or a display name (a string).
3484 If omitted or nil, that stands for the selected frame's display.
3486 On \"multi-monitor\" setups this refers to the pixel height for all
3487 physical monitors associated with TERMINAL. To get information for
3488 each physical monitor, use `display-monitor-attributes-list'. */)
3489 (Lisp_Object terminal)
3491 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3493 return make_number (x_display_pixel_height (dpyinfo));
3496 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
3497 0, 1, 0,
3498 doc: /* Return the number of bitplanes of the X display TERMINAL.
3499 The optional argument TERMINAL specifies which display to ask about.
3500 TERMINAL should be a terminal object, a frame or a display name (a string).
3501 If omitted or nil, that stands for the selected frame's display. */)
3502 (Lisp_Object terminal)
3504 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3506 return make_number (dpyinfo->n_planes);
3509 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
3510 0, 1, 0,
3511 doc: /* Return the number of color cells of the X display TERMINAL.
3512 The optional argument TERMINAL specifies which display to ask about.
3513 TERMINAL should be a terminal object, a frame or a display name (a string).
3514 If omitted or nil, that stands for the selected frame's display. */)
3515 (Lisp_Object terminal)
3517 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3519 int nr_planes = DisplayPlanes (dpyinfo->display,
3520 XScreenNumberOfScreen (dpyinfo->screen));
3522 /* Truncate nr_planes to 24 to avoid integer overflow.
3523 Some displays says 32, but only 24 bits are actually significant.
3524 There are only very few and rare video cards that have more than
3525 24 significant bits. Also 24 bits is more than 16 million colors,
3526 it "should be enough for everyone". */
3527 if (nr_planes > 24) nr_planes = 24;
3529 return make_number (1 << nr_planes);
3532 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
3533 Sx_server_max_request_size,
3534 0, 1, 0,
3535 doc: /* Return the maximum request size of the X server of display TERMINAL.
3536 The optional argument TERMINAL specifies which display to ask about.
3537 TERMINAL should be a terminal object, a frame or a display name (a string).
3538 If omitted or nil, that stands for the selected frame's display. */)
3539 (Lisp_Object terminal)
3541 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3543 return make_number (MAXREQUEST (dpyinfo->display));
3546 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
3547 doc: /* Return the "vendor ID" string of the X server of display TERMINAL.
3548 \(Labeling every distributor as a "vendor" embodies the false assumption
3549 that operating systems cannot be developed and distributed noncommercially.)
3550 The optional argument TERMINAL specifies which display to ask about.
3551 TERMINAL should be a terminal object, a frame or a display name (a string).
3552 If omitted or nil, that stands for the selected frame's display. */)
3553 (Lisp_Object terminal)
3555 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3556 const char *vendor = ServerVendor (dpyinfo->display);
3558 if (! vendor) vendor = "";
3559 return build_string (vendor);
3562 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
3563 doc: /* Return the version numbers of the X server of display TERMINAL.
3564 The value is a list of three integers: the major and minor
3565 version numbers of the X Protocol in use, and the distributor-specific release
3566 number. See also the function `x-server-vendor'.
3568 The optional argument TERMINAL specifies which display to ask about.
3569 TERMINAL should be a terminal object, a frame or a display name (a string).
3570 If omitted or nil, that stands for the selected frame's display. */)
3571 (Lisp_Object terminal)
3573 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3574 Display *dpy = dpyinfo->display;
3576 return list3i (ProtocolVersion (dpy), ProtocolRevision (dpy),
3577 VendorRelease (dpy));
3580 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
3581 doc: /* Return the number of screens on the X server of display TERMINAL.
3582 The optional argument TERMINAL specifies which display to ask about.
3583 TERMINAL should be a terminal object, a frame or a display name (a string).
3584 If omitted or nil, that stands for the selected frame's display. */)
3585 (Lisp_Object terminal)
3587 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3589 return make_number (ScreenCount (dpyinfo->display));
3592 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
3593 doc: /* Return the height in millimeters of the X display TERMINAL.
3594 The optional argument TERMINAL specifies which display to ask about.
3595 TERMINAL should be a terminal object, a frame or a display name (a string).
3596 If omitted or nil, that stands for the selected frame's display.
3598 On \"multi-monitor\" setups this refers to the height in millimeters for
3599 all physical monitors associated with TERMINAL. To get information
3600 for each physical monitor, use `display-monitor-attributes-list'. */)
3601 (Lisp_Object terminal)
3603 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3605 return make_number (HeightMMOfScreen (dpyinfo->screen));
3608 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
3609 doc: /* Return the width in millimeters of the X display TERMINAL.
3610 The optional argument TERMINAL specifies which display to ask about.
3611 TERMINAL should be a terminal object, a frame or a display name (a string).
3612 If omitted or nil, that stands for the selected frame's display.
3614 On \"multi-monitor\" setups this refers to the width in millimeters for
3615 all physical monitors associated with TERMINAL. To get information
3616 for each physical monitor, use `display-monitor-attributes-list'. */)
3617 (Lisp_Object terminal)
3619 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3621 return make_number (WidthMMOfScreen (dpyinfo->screen));
3624 DEFUN ("x-display-backing-store", Fx_display_backing_store,
3625 Sx_display_backing_store, 0, 1, 0,
3626 doc: /* Return an indication of whether X display TERMINAL does backing store.
3627 The value may be `always', `when-mapped', or `not-useful'.
3628 The optional argument TERMINAL specifies which display to ask about.
3629 TERMINAL should be a terminal object, a frame or a display name (a string).
3630 If omitted or nil, that stands for the selected frame's display. */)
3631 (Lisp_Object terminal)
3633 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3634 Lisp_Object result;
3636 switch (DoesBackingStore (dpyinfo->screen))
3638 case Always:
3639 result = intern ("always");
3640 break;
3642 case WhenMapped:
3643 result = intern ("when-mapped");
3644 break;
3646 case NotUseful:
3647 result = intern ("not-useful");
3648 break;
3650 default:
3651 error ("Strange value for BackingStore parameter of screen");
3654 return result;
3657 DEFUN ("x-display-visual-class", Fx_display_visual_class,
3658 Sx_display_visual_class, 0, 1, 0,
3659 doc: /* Return the visual class of the X display TERMINAL.
3660 The value is one of the symbols `static-gray', `gray-scale',
3661 `static-color', `pseudo-color', `true-color', or `direct-color'.
3663 The optional argument TERMINAL specifies which display to ask about.
3664 TERMINAL should a terminal object, a frame or a display name (a string).
3665 If omitted or nil, that stands for the selected frame's display. */)
3666 (Lisp_Object terminal)
3668 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3669 Lisp_Object result;
3671 switch (dpyinfo->visual->class)
3673 case StaticGray:
3674 result = intern ("static-gray");
3675 break;
3676 case GrayScale:
3677 result = intern ("gray-scale");
3678 break;
3679 case StaticColor:
3680 result = intern ("static-color");
3681 break;
3682 case PseudoColor:
3683 result = intern ("pseudo-color");
3684 break;
3685 case TrueColor:
3686 result = intern ("true-color");
3687 break;
3688 case DirectColor:
3689 result = intern ("direct-color");
3690 break;
3691 default:
3692 error ("Display has an unknown visual class");
3695 return result;
3698 DEFUN ("x-display-save-under", Fx_display_save_under,
3699 Sx_display_save_under, 0, 1, 0,
3700 doc: /* Return t if the X display TERMINAL supports the save-under feature.
3701 The optional argument TERMINAL specifies which display to ask about.
3702 TERMINAL should be a terminal object, a frame or a display name (a string).
3703 If omitted or nil, that stands for the selected frame's display. */)
3704 (Lisp_Object terminal)
3706 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3708 if (DoesSaveUnders (dpyinfo->screen) == True)
3709 return Qt;
3710 else
3711 return Qnil;
3714 /* Store the geometry of the workarea on display DPYINFO into *RECT.
3715 Return false if and only if the workarea information cannot be
3716 obtained via the _NET_WORKAREA root window property. */
3718 #if ! GTK_CHECK_VERSION (3, 4, 0)
3719 static bool
3720 x_get_net_workarea (struct x_display_info *dpyinfo, XRectangle *rect)
3722 Display *dpy = dpyinfo->display;
3723 long offset, max_len;
3724 Atom target_type, actual_type;
3725 unsigned long actual_size, bytes_remaining;
3726 int rc, actual_format;
3727 unsigned char *tmp_data = NULL;
3728 bool result = false;
3730 x_catch_errors (dpy);
3731 offset = 0;
3732 max_len = 1;
3733 target_type = XA_CARDINAL;
3734 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3735 dpyinfo->Xatom_net_current_desktop,
3736 offset, max_len, False, target_type,
3737 &actual_type, &actual_format, &actual_size,
3738 &bytes_remaining, &tmp_data);
3739 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3740 && actual_format == 32 && actual_size == max_len)
3742 long current_desktop = ((long *) tmp_data)[0];
3744 XFree (tmp_data);
3745 tmp_data = NULL;
3747 offset = 4 * current_desktop;
3748 max_len = 4;
3749 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3750 dpyinfo->Xatom_net_workarea,
3751 offset, max_len, False, target_type,
3752 &actual_type, &actual_format, &actual_size,
3753 &bytes_remaining, &tmp_data);
3754 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3755 && actual_format == 32 && actual_size == max_len)
3757 long *values = (long *) tmp_data;
3759 rect->x = values[0];
3760 rect->y = values[1];
3761 rect->width = values[2];
3762 rect->height = values[3];
3764 XFree (tmp_data);
3765 tmp_data = NULL;
3767 result = true;
3770 if (tmp_data)
3771 XFree (tmp_data);
3772 x_uncatch_errors ();
3774 return result;
3776 #endif
3778 #ifndef USE_GTK
3780 /* Return monitor number where F is "most" or closest to. */
3781 static int
3782 x_get_monitor_for_frame (struct frame *f,
3783 struct MonitorInfo *monitors,
3784 int n_monitors)
3786 XRectangle frect;
3787 int area = 0, dist = -1;
3788 int best_area = -1, best_dist = -1;
3789 int i;
3791 if (n_monitors == 1) return 0;
3792 frect.x = f->left_pos;
3793 frect.y = f->top_pos;
3794 frect.width = FRAME_PIXEL_WIDTH (f);
3795 frect.height = FRAME_PIXEL_HEIGHT (f);
3797 for (i = 0; i < n_monitors; ++i)
3799 struct MonitorInfo *mi = &monitors[i];
3800 XRectangle res;
3801 int a = 0;
3803 if (mi->geom.width == 0) continue;
3805 if (x_intersect_rectangles (&mi->geom, &frect, &res))
3807 a = res.width * res.height;
3808 if (a > area)
3810 area = a;
3811 best_area = i;
3815 if (a == 0 && area == 0)
3817 int dx, dy, d;
3818 if (frect.x + frect.width < mi->geom.x)
3819 dx = mi->geom.x - frect.x + frect.width;
3820 else if (frect.x > mi->geom.x + mi->geom.width)
3821 dx = frect.x - mi->geom.x + mi->geom.width;
3822 else
3823 dx = 0;
3824 if (frect.y + frect.height < mi->geom.y)
3825 dy = mi->geom.y - frect.y + frect.height;
3826 else if (frect.y > mi->geom.y + mi->geom.height)
3827 dy = frect.y - mi->geom.y + mi->geom.height;
3828 else
3829 dy = 0;
3831 d = dx*dx + dy*dy;
3832 if (dist == -1 || dist > d)
3834 dist = d;
3835 best_dist = i;
3840 return best_area != -1 ? best_area : (best_dist != -1 ? best_dist : 0);
3843 static Lisp_Object
3844 x_make_monitor_attribute_list (struct MonitorInfo *monitors,
3845 int n_monitors,
3846 int primary_monitor,
3847 struct x_display_info *dpyinfo,
3848 const char *source)
3850 Lisp_Object monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
3851 Lisp_Object frame, rest;
3853 FOR_EACH_FRAME (rest, frame)
3855 struct frame *f = XFRAME (frame);
3857 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo
3858 && !EQ (frame, tip_frame))
3860 int i = x_get_monitor_for_frame (f, monitors, n_monitors);
3861 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
3865 return make_monitor_attribute_list (monitors, n_monitors, primary_monitor,
3866 monitor_frames, source);
3869 static Lisp_Object
3870 x_get_monitor_attributes_fallback (struct x_display_info *dpyinfo)
3872 struct MonitorInfo monitor;
3873 XRectangle workarea_r;
3875 /* Fallback: treat (possibly) multiple physical monitors as if they
3876 formed a single monitor as a whole. This should provide a
3877 consistent result at least on single monitor environments. */
3878 monitor.geom.x = monitor.geom.y = 0;
3879 monitor.geom.width = x_display_pixel_width (dpyinfo);
3880 monitor.geom.height = x_display_pixel_height (dpyinfo);
3881 monitor.mm_width = WidthMMOfScreen (dpyinfo->screen);
3882 monitor.mm_height = HeightMMOfScreen (dpyinfo->screen);
3883 monitor.name = xstrdup ("combined screen");
3885 if (x_get_net_workarea (dpyinfo, &workarea_r))
3886 monitor.work = workarea_r;
3887 else
3888 monitor.work = monitor.geom;
3889 return x_make_monitor_attribute_list (&monitor, 1, 0, dpyinfo, "fallback");
3893 #ifdef HAVE_XINERAMA
3894 static Lisp_Object
3895 x_get_monitor_attributes_xinerama (struct x_display_info *dpyinfo)
3897 int n_monitors, i;
3898 Lisp_Object attributes_list = Qnil;
3899 Display *dpy = dpyinfo->display;
3900 XineramaScreenInfo *info = XineramaQueryScreens (dpy, &n_monitors);
3901 struct MonitorInfo *monitors;
3902 double mm_width_per_pixel, mm_height_per_pixel;
3904 if (! info || n_monitors == 0)
3906 if (info)
3907 XFree (info);
3908 return attributes_list;
3911 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
3912 / x_display_pixel_width (dpyinfo));
3913 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
3914 / x_display_pixel_height (dpyinfo));
3915 monitors = xzalloc (n_monitors * sizeof *monitors);
3916 for (i = 0; i < n_monitors; ++i)
3918 struct MonitorInfo *mi = &monitors[i];
3919 XRectangle workarea_r;
3921 mi->geom.x = info[i].x_org;
3922 mi->geom.y = info[i].y_org;
3923 mi->geom.width = info[i].width;
3924 mi->geom.height = info[i].height;
3925 mi->mm_width = mi->geom.width * mm_width_per_pixel + 0.5;
3926 mi->mm_height = mi->geom.height * mm_height_per_pixel + 0.5;
3927 mi->name = 0;
3929 /* Xinerama usually have primary monitor first, just use that. */
3930 if (i == 0 && x_get_net_workarea (dpyinfo, &workarea_r))
3932 mi->work = workarea_r;
3933 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
3934 mi->work = mi->geom;
3936 else
3937 mi->work = mi->geom;
3939 XFree (info);
3941 attributes_list = x_make_monitor_attribute_list (monitors,
3942 n_monitors,
3944 dpyinfo,
3945 "Xinerama");
3946 free_monitors (monitors, n_monitors);
3947 return attributes_list;
3949 #endif /* HAVE_XINERAMA */
3952 #ifdef HAVE_XRANDR
3953 static Lisp_Object
3954 x_get_monitor_attributes_xrandr (struct x_display_info *dpyinfo)
3956 Lisp_Object attributes_list = Qnil;
3957 XRRScreenResources *resources;
3958 Display *dpy = dpyinfo->display;
3959 int i, n_monitors, primary = -1;
3960 RROutput pxid = None;
3961 struct MonitorInfo *monitors;
3963 #ifdef HAVE_XRRGETSCREENRESOURCESCURRENT
3964 resources = XRRGetScreenResourcesCurrent (dpy, dpyinfo->root_window);
3965 #else
3966 resources = XRRGetScreenResources (dpy, dpyinfo->root_window);
3967 #endif
3968 if (! resources || resources->noutput == 0)
3970 if (resources)
3971 XRRFreeScreenResources (resources);
3972 return Qnil;
3974 n_monitors = resources->noutput;
3975 monitors = xzalloc (n_monitors * sizeof *monitors);
3977 #ifdef HAVE_XRRGETOUTPUTPRIMARY
3978 pxid = XRRGetOutputPrimary (dpy, dpyinfo->root_window);
3979 #endif
3981 for (i = 0; i < n_monitors; ++i)
3983 XRROutputInfo *info = XRRGetOutputInfo (dpy, resources,
3984 resources->outputs[i]);
3985 Connection conn = info ? info->connection : RR_Disconnected;
3986 RRCrtc id = info ? info->crtc : None;
3988 if (strcmp (info->name, "default") == 0)
3990 /* Non XRandr 1.2 driver, does not give useful data. */
3991 XRRFreeOutputInfo (info);
3992 XRRFreeScreenResources (resources);
3993 free_monitors (monitors, n_monitors);
3994 return Qnil;
3997 if (conn != RR_Disconnected && id != None)
3999 XRRCrtcInfo *crtc = XRRGetCrtcInfo (dpy, resources, id);
4000 struct MonitorInfo *mi = &monitors[i];
4001 XRectangle workarea_r;
4003 if (! crtc)
4005 XRRFreeOutputInfo (info);
4006 continue;
4009 mi->geom.x = crtc->x;
4010 mi->geom.y = crtc->y;
4011 mi->geom.width = crtc->width;
4012 mi->geom.height = crtc->height;
4013 mi->mm_width = info->mm_width;
4014 mi->mm_height = info->mm_height;
4015 mi->name = xstrdup (info->name);
4017 if (pxid != None && pxid == resources->outputs[i])
4018 primary = i;
4019 else if (primary == -1 && strcmp (info->name, "LVDS") == 0)
4020 primary = i;
4022 if (i == primary && x_get_net_workarea (dpyinfo, &workarea_r))
4024 mi->work= workarea_r;
4025 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
4026 mi->work = mi->geom;
4028 else
4029 mi->work = mi->geom;
4031 XRRFreeCrtcInfo (crtc);
4033 XRRFreeOutputInfo (info);
4035 XRRFreeScreenResources (resources);
4037 attributes_list = x_make_monitor_attribute_list (monitors,
4038 n_monitors,
4039 primary,
4040 dpyinfo,
4041 "XRandr");
4042 free_monitors (monitors, n_monitors);
4043 return attributes_list;
4045 #endif /* HAVE_XRANDR */
4047 static Lisp_Object
4048 x_get_monitor_attributes (struct x_display_info *dpyinfo)
4050 Lisp_Object attributes_list = Qnil;
4051 Display *dpy = dpyinfo->display;
4053 (void) dpy; /* Suppress unused variable warning. */
4055 #ifdef HAVE_XRANDR
4056 int xrr_event_base, xrr_error_base;
4057 bool xrr_ok = false;
4058 xrr_ok = XRRQueryExtension (dpy, &xrr_event_base, &xrr_error_base);
4059 if (xrr_ok)
4061 int xrr_major, xrr_minor;
4062 XRRQueryVersion (dpy, &xrr_major, &xrr_minor);
4063 xrr_ok = (xrr_major == 1 && xrr_minor >= 2) || xrr_major > 1;
4066 if (xrr_ok)
4067 attributes_list = x_get_monitor_attributes_xrandr (dpyinfo);
4068 #endif /* HAVE_XRANDR */
4070 #ifdef HAVE_XINERAMA
4071 if (NILP (attributes_list))
4073 int xin_event_base, xin_error_base;
4074 bool xin_ok = false;
4075 xin_ok = XineramaQueryExtension (dpy, &xin_event_base, &xin_error_base);
4076 if (xin_ok && XineramaIsActive (dpy))
4077 attributes_list = x_get_monitor_attributes_xinerama (dpyinfo);
4079 #endif /* HAVE_XINERAMA */
4081 if (NILP (attributes_list))
4082 attributes_list = x_get_monitor_attributes_fallback (dpyinfo);
4084 return attributes_list;
4087 #endif /* !USE_GTK */
4089 DEFUN ("x-display-monitor-attributes-list", Fx_display_monitor_attributes_list,
4090 Sx_display_monitor_attributes_list,
4091 0, 1, 0,
4092 doc: /* Return a list of physical monitor attributes on the X display TERMINAL.
4094 The optional argument TERMINAL specifies which display to ask about.
4095 TERMINAL should be a terminal object, a frame or a display name (a string).
4096 If omitted or nil, that stands for the selected frame's display.
4098 In addition to the standard attribute keys listed in
4099 `display-monitor-attributes-list', the following keys are contained in
4100 the attributes:
4102 source -- String describing the source from which multi-monitor
4103 information is obtained, one of \"Gdk\", \"XRandr\",
4104 \"Xinerama\", or \"fallback\"
4106 Internal use only, use `display-monitor-attributes-list' instead. */)
4107 (Lisp_Object terminal)
4109 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4110 Lisp_Object attributes_list = Qnil;
4112 #ifdef USE_GTK
4113 double mm_width_per_pixel, mm_height_per_pixel;
4114 GdkDisplay *gdpy;
4115 GdkScreen *gscreen;
4116 gint primary_monitor = 0, n_monitors, i;
4117 Lisp_Object monitor_frames, rest, frame;
4118 static const char *source = "Gdk";
4119 struct MonitorInfo *monitors;
4121 block_input ();
4122 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
4123 / x_display_pixel_width (dpyinfo));
4124 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
4125 / x_display_pixel_height (dpyinfo));
4126 gdpy = gdk_x11_lookup_xdisplay (dpyinfo->display);
4127 gscreen = gdk_display_get_default_screen (gdpy);
4128 #if GTK_CHECK_VERSION (2, 20, 0)
4129 primary_monitor = gdk_screen_get_primary_monitor (gscreen);
4130 #endif
4131 n_monitors = gdk_screen_get_n_monitors (gscreen);
4132 monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
4133 monitors = xzalloc (n_monitors * sizeof *monitors);
4135 FOR_EACH_FRAME (rest, frame)
4137 struct frame *f = XFRAME (frame);
4139 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo
4140 && !EQ (frame, tip_frame))
4142 GdkWindow *gwin = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
4144 i = gdk_screen_get_monitor_at_window (gscreen, gwin);
4145 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
4149 for (i = 0; i < n_monitors; ++i)
4151 gint width_mm = -1, height_mm = -1;
4152 GdkRectangle rec, work;
4153 struct MonitorInfo *mi = &monitors[i];
4155 gdk_screen_get_monitor_geometry (gscreen, i, &rec);
4157 #if GTK_CHECK_VERSION (2, 14, 0)
4158 width_mm = gdk_screen_get_monitor_width_mm (gscreen, i);
4159 height_mm = gdk_screen_get_monitor_height_mm (gscreen, i);
4160 #endif
4161 if (width_mm < 0)
4162 width_mm = rec.width * mm_width_per_pixel + 0.5;
4163 if (height_mm < 0)
4164 height_mm = rec.height * mm_height_per_pixel + 0.5;
4166 #if GTK_CHECK_VERSION (3, 4, 0)
4167 gdk_screen_get_monitor_workarea (gscreen, i, &work);
4168 #else
4169 /* Emulate the behavior of GTK+ 3.4. */
4171 XRectangle workarea_r;
4173 if (i == primary_monitor && x_get_net_workarea (dpyinfo, &workarea_r))
4175 work.x = workarea_r.x;
4176 work.y = workarea_r.y;
4177 work.width = workarea_r.width;
4178 work.height = workarea_r.height;
4179 if (! gdk_rectangle_intersect (&rec, &work, &work))
4180 work = rec;
4182 else
4183 work = rec;
4185 #endif
4188 mi->geom.x = rec.x;
4189 mi->geom.y = rec.y;
4190 mi->geom.width = rec.width;
4191 mi->geom.height = rec.height;
4192 mi->work.x = work.x;
4193 mi->work.y = work.y;
4194 mi->work.width = work.width;
4195 mi->work.height = work.height;
4196 mi->mm_width = width_mm;
4197 mi->mm_height = height_mm;
4199 #if GTK_CHECK_VERSION (2, 14, 0)
4200 mi->name = gdk_screen_get_monitor_plug_name (gscreen, i);
4201 #endif
4204 attributes_list = make_monitor_attribute_list (monitors,
4205 n_monitors,
4206 primary_monitor,
4207 monitor_frames,
4208 source);
4209 unblock_input ();
4210 #else /* not USE_GTK */
4212 block_input ();
4213 attributes_list = x_get_monitor_attributes (dpyinfo);
4214 unblock_input ();
4216 #endif /* not USE_GTK */
4218 return attributes_list;
4221 DEFUN ("x-frame-geometry", Fx_frame_geometry, Sx_frame_geometry, 0, 1, 0,
4222 doc: /* Return geometric attributes of frame FRAME.
4224 FRAME must be a live frame and defaults to the selected one.
4226 The return value is an association list containing the following
4227 elements (all size values are in pixels).
4229 - `frame-outer-size' is a cons of the outer width and height of FRAME.
4230 The outer size include the title bar and the external borders as well
4231 as any menu and/or tool bar of frame.
4233 - `border' is a cons of the horizontal and vertical width of FRAME's
4234 external borders.
4236 - `title-bar-height' is the height of the title bar of FRAME.
4238 - `menu-bar-external' if `t' means the menu bar is external (not
4239 included in the inner edges of FRAME).
4241 - `menu-bar-size' is a cons of the width and height of the menu bar of
4242 FRAME.
4244 - `tool-bar-external' if `t' means the tool bar is external (not
4245 included in the inner edges of FRAME).
4247 - `tool-bar-side' tells tells on which side the tool bar on FRAME is and
4248 can be one of `left', `top', `right' or `bottom'.
4250 - `tool-bar-size' is a cons of the width and height of the tool bar of
4251 FRAME.
4253 - `frame-inner-size' is a cons of the inner width and height of FRAME.
4254 This excludes FRAME's title bar and external border as well as any
4255 external menu and/or tool bar. */)
4256 (Lisp_Object frame)
4258 struct frame *f = decode_live_frame (frame);
4259 int inner_width = FRAME_PIXEL_WIDTH (f);
4260 int inner_height = FRAME_PIXEL_HEIGHT (f);
4261 int outer_width, outer_height, border, title;
4262 Lisp_Object fullscreen = Fframe_parameter (frame, Qfullscreen);
4263 int menu_bar_height, menu_bar_width, tool_bar_height, tool_bar_width;
4265 border = FRAME_OUTER_TO_INNER_DIFF_X (f);
4266 title = FRAME_X_OUTPUT (f)->y_pixels_outer_diff - border;
4268 outer_width = FRAME_PIXEL_WIDTH (f) + 2 * border;
4269 outer_height = (FRAME_PIXEL_HEIGHT (f)
4270 + FRAME_OUTER_TO_INNER_DIFF_Y (f)
4271 + FRAME_OUTER_TO_INNER_DIFF_X (f));
4273 #if defined (USE_GTK)
4275 bool tool_bar_left_right = (EQ (FRAME_TOOL_BAR_POSITION (f), Qleft)
4276 || EQ (FRAME_TOOL_BAR_POSITION (f), Qright));
4278 tool_bar_width = (tool_bar_left_right
4279 ? FRAME_TOOLBAR_WIDTH (f)
4280 : FRAME_PIXEL_WIDTH (f));
4281 tool_bar_height = (tool_bar_left_right
4282 ? FRAME_PIXEL_HEIGHT (f)
4283 : FRAME_TOOLBAR_HEIGHT (f));
4284 if (tool_bar_left_right)
4285 /* For some reason FRAME_OUTER_TO_INNER_DIFF_X does not count the
4286 width of a tool bar. */
4287 outer_width += FRAME_TOOLBAR_WIDTH (f);
4289 #else
4290 tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f);
4291 tool_bar_width = ((tool_bar_height > 0)
4292 ? outer_width - 2 * FRAME_INTERNAL_BORDER_WIDTH (f)
4293 : 0);
4294 #endif
4296 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
4297 menu_bar_height = FRAME_MENUBAR_HEIGHT (f);
4298 #else
4299 menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
4300 #endif
4302 menu_bar_width = ((menu_bar_height > 0)
4303 ? outer_width - 2 * border
4304 : 0);
4306 if (!FRAME_EXTERNAL_MENU_BAR (f))
4307 inner_height -= menu_bar_height;
4308 if (!FRAME_EXTERNAL_TOOL_BAR (f))
4309 inner_height -= tool_bar_height;
4311 return
4312 listn (CONSTYPE_PURE, 10,
4313 Fcons (Qframe_position,
4314 Fcons (make_number (f->left_pos), make_number (f->top_pos))),
4315 Fcons (Qframe_outer_size,
4316 Fcons (make_number (outer_width), make_number (outer_height))),
4317 Fcons (Qexternal_border_size,
4318 ((EQ (fullscreen, Qfullboth) || EQ (fullscreen, Qfullscreen))
4319 ? Fcons (make_number (0), make_number (0))
4320 : Fcons (make_number (border), make_number (border)))),
4321 Fcons (Qtitle_height,
4322 ((EQ (fullscreen, Qfullboth) || EQ (fullscreen, Qfullscreen))
4323 ? make_number (0)
4324 : make_number (title))),
4325 Fcons (Qmenu_bar_external, FRAME_EXTERNAL_MENU_BAR (f) ? Qt : Qnil),
4326 Fcons (Qmenu_bar_size,
4327 Fcons (make_number (menu_bar_width),
4328 make_number (menu_bar_height))),
4329 Fcons (Qtool_bar_external, FRAME_EXTERNAL_TOOL_BAR (f) ? Qt : Qnil),
4330 Fcons (Qtool_bar_position, FRAME_TOOL_BAR_POSITION (f)),
4331 Fcons (Qtool_bar_size,
4332 Fcons (make_number (tool_bar_width),
4333 make_number (tool_bar_height))),
4334 Fcons (Qframe_inner_size,
4335 Fcons (make_number (inner_width),
4336 make_number (inner_height))));
4339 /************************************************************************
4340 X Displays
4341 ************************************************************************/
4344 /* Mapping visual names to visuals. */
4346 static struct visual_class
4348 const char *name;
4349 int class;
4351 visual_classes[] =
4353 {"StaticGray", StaticGray},
4354 {"GrayScale", GrayScale},
4355 {"StaticColor", StaticColor},
4356 {"PseudoColor", PseudoColor},
4357 {"TrueColor", TrueColor},
4358 {"DirectColor", DirectColor},
4359 {NULL, 0}
4363 #ifndef HAVE_XSCREENNUMBEROFSCREEN
4365 /* Value is the screen number of screen SCR. This is a substitute for
4366 the X function with the same name when that doesn't exist. */
4369 XScreenNumberOfScreen (scr)
4370 register Screen *scr;
4372 Display *dpy = scr->display;
4373 int i;
4375 for (i = 0; i < dpy->nscreens; ++i)
4376 if (scr == dpy->screens + i)
4377 break;
4379 return i;
4382 #endif /* not HAVE_XSCREENNUMBEROFSCREEN */
4385 /* Select the visual that should be used on display DPYINFO. Set
4386 members of DPYINFO appropriately. Called from x_term_init. */
4388 void
4389 select_visual (struct x_display_info *dpyinfo)
4391 Display *dpy = dpyinfo->display;
4392 Screen *screen = dpyinfo->screen;
4394 /* See if a visual is specified. */
4395 AUTO_STRING (visualClass, "visualClass");
4396 AUTO_STRING (VisualClass, "VisualClass");
4397 Lisp_Object value = display_x_get_resource (dpyinfo, visualClass,
4398 VisualClass, Qnil, Qnil);
4400 if (STRINGP (value))
4402 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
4403 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
4404 depth, a decimal number. NAME is compared with case ignored. */
4405 char *s = alloca (SBYTES (value) + 1);
4406 char *dash;
4407 int i, class = -1;
4408 XVisualInfo vinfo;
4410 lispstpcpy (s, value);
4411 dash = strchr (s, '-');
4412 if (dash)
4414 dpyinfo->n_planes = atoi (dash + 1);
4415 *dash = '\0';
4417 else
4418 /* We won't find a matching visual with depth 0, so that
4419 an error will be printed below. */
4420 dpyinfo->n_planes = 0;
4422 /* Determine the visual class. */
4423 for (i = 0; visual_classes[i].name; ++i)
4424 if (xstrcasecmp (s, visual_classes[i].name) == 0)
4426 class = visual_classes[i].class;
4427 break;
4430 /* Look up a matching visual for the specified class. */
4431 if (class == -1
4432 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
4433 dpyinfo->n_planes, class, &vinfo))
4434 fatal ("Invalid visual specification `%s'", SDATA (value));
4436 dpyinfo->visual = vinfo.visual;
4438 else
4440 int n_visuals;
4441 XVisualInfo *vinfo, vinfo_template;
4443 dpyinfo->visual = DefaultVisualOfScreen (screen);
4445 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
4446 vinfo_template.screen = XScreenNumberOfScreen (screen);
4447 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
4448 &vinfo_template, &n_visuals);
4449 if (n_visuals <= 0)
4450 fatal ("Can't get proper X visual info");
4452 dpyinfo->n_planes = vinfo->depth;
4453 XFree (vinfo);
4458 /* Return the X display structure for the display named NAME.
4459 Open a new connection if necessary. */
4461 static struct x_display_info *
4462 x_display_info_for_name (Lisp_Object name)
4464 struct x_display_info *dpyinfo;
4466 CHECK_STRING (name);
4468 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
4469 if (!NILP (Fstring_equal (XCAR (dpyinfo->name_list_element), name)))
4470 return dpyinfo;
4472 /* Use this general default value to start with. */
4473 Vx_resource_name = Vinvocation_name;
4475 validate_x_resource_name ();
4477 dpyinfo = x_term_init (name, 0, SSDATA (Vx_resource_name));
4479 if (dpyinfo == 0)
4480 error ("Cannot connect to X server %s", SDATA (name));
4482 XSETFASTINT (Vwindow_system_version, 11);
4484 return dpyinfo;
4488 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
4489 1, 3, 0,
4490 doc: /* Open a connection to a display server.
4491 DISPLAY is the name of the display to connect to.
4492 Optional second arg XRM-STRING is a string of resources in xrdb format.
4493 If the optional third arg MUST-SUCCEED is non-nil,
4494 terminate Emacs if we can't open the connection.
4495 \(In the Nextstep version, the last two arguments are currently ignored.) */)
4496 (Lisp_Object display, Lisp_Object xrm_string, Lisp_Object must_succeed)
4498 char *xrm_option;
4499 struct x_display_info *dpyinfo;
4501 CHECK_STRING (display);
4502 if (! NILP (xrm_string))
4503 CHECK_STRING (xrm_string);
4505 xrm_option = NILP (xrm_string) ? 0 : SSDATA (xrm_string);
4507 validate_x_resource_name ();
4509 /* This is what opens the connection and sets x_current_display.
4510 This also initializes many symbols, such as those used for input. */
4511 dpyinfo = x_term_init (display, xrm_option,
4512 SSDATA (Vx_resource_name));
4514 if (dpyinfo == 0)
4516 if (!NILP (must_succeed))
4517 fatal ("Cannot connect to X server %s.\n\
4518 Check the DISPLAY environment variable or use `-d'.\n\
4519 Also use the `xauth' program to verify that you have the proper\n\
4520 authorization information needed to connect the X server.\n\
4521 An insecure way to solve the problem may be to use `xhost'.\n",
4522 SDATA (display));
4523 else
4524 error ("Cannot connect to X server %s", SDATA (display));
4527 XSETFASTINT (Vwindow_system_version, 11);
4528 return Qnil;
4531 DEFUN ("x-close-connection", Fx_close_connection,
4532 Sx_close_connection, 1, 1, 0,
4533 doc: /* Close the connection to TERMINAL's X server.
4534 For TERMINAL, specify a terminal object, a frame or a display name (a
4535 string). If TERMINAL is nil, that stands for the selected frame's
4536 terminal. */)
4537 (Lisp_Object terminal)
4539 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4541 if (dpyinfo->reference_count > 0)
4542 error ("Display still has frames on it");
4544 x_delete_terminal (dpyinfo->terminal);
4546 return Qnil;
4549 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
4550 doc: /* Return the list of display names that Emacs has connections to. */)
4551 (void)
4553 Lisp_Object result = Qnil;
4554 struct x_display_info *xdi;
4556 for (xdi = x_display_list; xdi; xdi = xdi->next)
4557 result = Fcons (XCAR (xdi->name_list_element), result);
4559 return result;
4562 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
4563 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
4564 This function only has an effect on X Windows. With MS Windows, it is
4565 defined but does nothing.
4567 If ON is nil, allow buffering of requests.
4568 Turning on synchronization prohibits the Xlib routines from buffering
4569 requests and seriously degrades performance, but makes debugging much
4570 easier.
4571 The optional second argument TERMINAL specifies which display to act on.
4572 TERMINAL should be a terminal object, a frame or a display name (a string).
4573 If TERMINAL is omitted or nil, that stands for the selected frame's display. */)
4574 (Lisp_Object on, Lisp_Object terminal)
4576 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4578 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
4580 return Qnil;
4583 /* Wait for responses to all X commands issued so far for frame F. */
4585 void
4586 x_sync (struct frame *f)
4588 block_input ();
4589 XSync (FRAME_X_DISPLAY (f), False);
4590 unblock_input ();
4594 /***********************************************************************
4595 Window properties
4596 ***********************************************************************/
4598 DEFUN ("x-change-window-property", Fx_change_window_property,
4599 Sx_change_window_property, 2, 6, 0,
4600 doc: /* Change window property PROP to VALUE on the X window of FRAME.
4601 PROP must be a string. VALUE may be a string or a list of conses,
4602 numbers and/or strings. If an element in the list is a string, it is
4603 converted to an atom and the value of the atom is used. If an element
4604 is a cons, it is converted to a 32 bit number where the car is the 16
4605 top bits and the cdr is the lower 16 bits.
4607 FRAME nil or omitted means use the selected frame.
4608 If TYPE is given and non-nil, it is the name of the type of VALUE.
4609 If TYPE is not given or nil, the type is STRING.
4610 FORMAT gives the size in bits of each element if VALUE is a list.
4611 It must be one of 8, 16 or 32.
4612 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
4613 If OUTER-P is non-nil, the property is changed for the outer X window of
4614 FRAME. Default is to change on the edit X window. */)
4615 (Lisp_Object prop, Lisp_Object value, Lisp_Object frame,
4616 Lisp_Object type, Lisp_Object format, Lisp_Object outer_p)
4618 struct frame *f = decode_window_system_frame (frame);
4619 Atom prop_atom;
4620 Atom target_type = XA_STRING;
4621 int element_format = 8;
4622 unsigned char *data;
4623 int nelements;
4624 Window w;
4626 CHECK_STRING (prop);
4628 if (! NILP (format))
4630 CHECK_NUMBER (format);
4632 if (XINT (format) != 8 && XINT (format) != 16
4633 && XINT (format) != 32)
4634 error ("FORMAT must be one of 8, 16 or 32");
4635 element_format = XINT (format);
4638 if (CONSP (value))
4640 ptrdiff_t elsize;
4642 nelements = x_check_property_data (value);
4643 if (nelements == -1)
4644 error ("Bad data in VALUE, must be number, string or cons");
4646 /* The man page for XChangeProperty:
4647 "If the specified format is 32, the property data must be a
4648 long array."
4649 This applies even if long is more than 32 bits. The X library
4650 converts to 32 bits before sending to the X server. */
4651 elsize = element_format == 32 ? sizeof (long) : element_format >> 3;
4652 data = xnmalloc (nelements, elsize);
4654 x_fill_property_data (FRAME_X_DISPLAY (f), value, data, element_format);
4656 else
4658 CHECK_STRING (value);
4659 data = SDATA (value);
4660 if (INT_MAX < SBYTES (value))
4661 error ("VALUE too long");
4662 nelements = SBYTES (value);
4665 block_input ();
4666 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4667 if (! NILP (type))
4669 CHECK_STRING (type);
4670 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4673 if (! NILP (outer_p)) w = FRAME_OUTER_WINDOW (f);
4674 else w = FRAME_X_WINDOW (f);
4676 XChangeProperty (FRAME_X_DISPLAY (f), w,
4677 prop_atom, target_type, element_format, PropModeReplace,
4678 data, nelements);
4680 if (CONSP (value)) xfree (data);
4682 /* Make sure the property is set when we return. */
4683 XFlush (FRAME_X_DISPLAY (f));
4684 unblock_input ();
4686 return value;
4690 DEFUN ("x-delete-window-property", Fx_delete_window_property,
4691 Sx_delete_window_property, 1, 2, 0,
4692 doc: /* Remove window property PROP from X window of FRAME.
4693 FRAME nil or omitted means use the selected frame. Value is PROP. */)
4694 (Lisp_Object prop, Lisp_Object frame)
4696 struct frame *f = decode_window_system_frame (frame);
4697 Atom prop_atom;
4699 CHECK_STRING (prop);
4700 block_input ();
4701 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4702 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
4704 /* Make sure the property is removed when we return. */
4705 XFlush (FRAME_X_DISPLAY (f));
4706 unblock_input ();
4708 return prop;
4712 static Lisp_Object
4713 x_window_property_intern (struct frame *f,
4714 Window target_window,
4715 Atom prop_atom,
4716 Atom target_type,
4717 Lisp_Object delete_p,
4718 Lisp_Object vector_ret_p,
4719 bool *found)
4721 unsigned char *tmp_data = NULL;
4722 Lisp_Object prop_value = Qnil;
4723 Atom actual_type;
4724 int actual_format;
4725 unsigned long actual_size, bytes_remaining;
4726 int rc;
4727 struct gcpro gcpro1;
4729 GCPRO1 (prop_value);
4731 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4732 prop_atom, 0, 0, False, target_type,
4733 &actual_type, &actual_format, &actual_size,
4734 &bytes_remaining, &tmp_data);
4736 *found = actual_format != 0;
4738 if (rc == Success && *found)
4740 XFree (tmp_data);
4741 tmp_data = NULL;
4743 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4744 prop_atom, 0, bytes_remaining,
4745 ! NILP (delete_p), target_type,
4746 &actual_type, &actual_format,
4747 &actual_size, &bytes_remaining,
4748 &tmp_data);
4749 if (rc == Success && tmp_data)
4751 /* The man page for XGetWindowProperty says:
4752 "If the returned format is 32, the returned data is represented
4753 as a long array and should be cast to that type to obtain the
4754 elements."
4755 This applies even if long is more than 32 bits, the X library
4756 converts from 32 bit elements received from the X server to long
4757 and passes the long array to us. Thus, for that case memcpy can not
4758 be used. We convert to a 32 bit type here, because so much code
4759 assume on that.
4761 The bytes and offsets passed to XGetWindowProperty refers to the
4762 property and those are indeed in 32 bit quantities if format is
4763 32. */
4765 if (BITS_PER_LONG > 32 && actual_format == 32)
4767 unsigned long i;
4768 int *idata = (int *) tmp_data;
4769 long *ldata = (long *) tmp_data;
4771 for (i = 0; i < actual_size; ++i)
4772 idata[i] = (int) ldata[i];
4775 if (NILP (vector_ret_p))
4776 prop_value = make_string ((char *) tmp_data, actual_size);
4777 else
4778 prop_value = x_property_data_to_lisp (f,
4779 tmp_data,
4780 actual_type,
4781 actual_format,
4782 actual_size);
4785 if (tmp_data) XFree (tmp_data);
4788 UNGCPRO;
4789 return prop_value;
4792 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
4793 1, 6, 0,
4794 doc: /* Value is the value of window property PROP on FRAME.
4795 If FRAME is nil or omitted, use the selected frame.
4797 On X Windows, the following optional arguments are also accepted:
4798 If TYPE is nil or omitted, get the property as a string.
4799 Otherwise TYPE is the name of the atom that denotes the type expected.
4800 If SOURCE is non-nil, get the property on that window instead of from
4801 FRAME. The number 0 denotes the root window.
4802 If DELETE-P is non-nil, delete the property after retrieving it.
4803 If VECTOR-RET-P is non-nil, don't return a string but a vector of values.
4805 On MS Windows, this function accepts but ignores those optional arguments.
4807 Value is nil if FRAME hasn't a property with name PROP or if PROP has
4808 no value of TYPE (always string in the MS Windows case). */)
4809 (Lisp_Object prop, Lisp_Object frame, Lisp_Object type,
4810 Lisp_Object source, Lisp_Object delete_p, Lisp_Object vector_ret_p)
4812 struct frame *f = decode_window_system_frame (frame);
4813 Atom prop_atom;
4814 Lisp_Object prop_value = Qnil;
4815 Atom target_type = XA_STRING;
4816 Window target_window = FRAME_X_WINDOW (f);
4817 struct gcpro gcpro1;
4818 bool found;
4820 GCPRO1 (prop_value);
4821 CHECK_STRING (prop);
4823 if (! NILP (source))
4825 CONS_TO_INTEGER (source, Window, target_window);
4826 if (! target_window)
4827 target_window = FRAME_DISPLAY_INFO (f)->root_window;
4830 block_input ();
4831 if (STRINGP (type))
4833 if (strcmp ("AnyPropertyType", SSDATA (type)) == 0)
4834 target_type = AnyPropertyType;
4835 else
4836 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4839 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4840 prop_value = x_window_property_intern (f,
4841 target_window,
4842 prop_atom,
4843 target_type,
4844 delete_p,
4845 vector_ret_p,
4846 &found);
4847 if (NILP (prop_value)
4848 && ! found
4849 && NILP (source)
4850 && target_window != FRAME_OUTER_WINDOW (f))
4852 prop_value = x_window_property_intern (f,
4853 FRAME_OUTER_WINDOW (f),
4854 prop_atom,
4855 target_type,
4856 delete_p,
4857 vector_ret_p,
4858 &found);
4862 unblock_input ();
4863 UNGCPRO;
4864 return prop_value;
4867 /***********************************************************************
4868 Tool tips
4869 ***********************************************************************/
4871 static Lisp_Object x_create_tip_frame (struct x_display_info *,
4872 Lisp_Object, Lisp_Object);
4873 static void compute_tip_xy (struct frame *, Lisp_Object, Lisp_Object,
4874 Lisp_Object, int, int, int *, int *);
4876 /* The frame of a currently visible tooltip. */
4878 Lisp_Object tip_frame;
4880 /* If non-nil, a timer started that hides the last tooltip when it
4881 fires. */
4883 static Lisp_Object tip_timer;
4884 Window tip_window;
4886 /* If non-nil, a vector of 3 elements containing the last args
4887 with which x-show-tip was called. See there. */
4889 static Lisp_Object last_show_tip_args;
4892 static void
4893 unwind_create_tip_frame (Lisp_Object frame)
4895 Lisp_Object deleted;
4897 deleted = unwind_create_frame (frame);
4898 if (EQ (deleted, Qt))
4900 tip_window = None;
4901 tip_frame = Qnil;
4906 /* Create a frame for a tooltip on the display described by DPYINFO.
4907 PARMS is a list of frame parameters. TEXT is the string to
4908 display in the tip frame. Value is the frame.
4910 Note that functions called here, esp. x_default_parameter can
4911 signal errors, for instance when a specified color name is
4912 undefined. We have to make sure that we're in a consistent state
4913 when this happens. */
4915 static Lisp_Object
4916 x_create_tip_frame (struct x_display_info *dpyinfo,
4917 Lisp_Object parms,
4918 Lisp_Object text)
4920 struct frame *f;
4921 Lisp_Object frame;
4922 Lisp_Object name;
4923 int width, height;
4924 ptrdiff_t count = SPECPDL_INDEX ();
4925 struct gcpro gcpro1, gcpro2, gcpro3;
4926 int face_change_count_before = face_change_count;
4927 Lisp_Object buffer;
4928 struct buffer *old_buffer;
4930 if (!dpyinfo->terminal->name)
4931 error ("Terminal is not live, can't create new frames on it");
4933 parms = Fcopy_alist (parms);
4935 /* Get the name of the frame to use for resource lookup. */
4936 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
4937 if (!STRINGP (name)
4938 && !EQ (name, Qunbound)
4939 && !NILP (name))
4940 error ("Invalid frame name--not a string or nil");
4942 frame = Qnil;
4943 GCPRO3 (parms, name, frame);
4944 f = make_frame (1);
4945 XSETFRAME (frame, f);
4947 AUTO_STRING (tip, " *tip*");
4948 buffer = Fget_buffer_create (tip);
4949 /* Use set_window_buffer instead of Fset_window_buffer (see
4950 discussion of bug#11984, bug#12025, bug#12026). */
4951 set_window_buffer (FRAME_ROOT_WINDOW (f), buffer, 0, 0);
4952 old_buffer = current_buffer;
4953 set_buffer_internal_1 (XBUFFER (buffer));
4954 bset_truncate_lines (current_buffer, Qnil);
4955 specbind (Qinhibit_read_only, Qt);
4956 specbind (Qinhibit_modification_hooks, Qt);
4957 Ferase_buffer ();
4958 Finsert (1, &text);
4959 set_buffer_internal_1 (old_buffer);
4961 record_unwind_protect (unwind_create_tip_frame, frame);
4963 f->terminal = dpyinfo->terminal;
4965 /* By setting the output method, we're essentially saying that
4966 the frame is live, as per FRAME_LIVE_P. If we get a signal
4967 from this point on, x_destroy_window might screw up reference
4968 counts etc. */
4969 f->output_method = output_x_window;
4970 f->output_data.x = xzalloc (sizeof *f->output_data.x);
4971 f->output_data.x->icon_bitmap = -1;
4972 FRAME_FONTSET (f) = -1;
4973 f->output_data.x->scroll_bar_foreground_pixel = -1;
4974 f->output_data.x->scroll_bar_background_pixel = -1;
4975 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
4976 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
4977 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
4978 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
4979 f->output_data.x->white_relief.pixel = -1;
4980 f->output_data.x->black_relief.pixel = -1;
4982 fset_icon_name (f, Qnil);
4983 FRAME_DISPLAY_INFO (f) = dpyinfo;
4984 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
4985 f->output_data.x->explicit_parent = 0;
4987 /* These colors will be set anyway later, but it's important
4988 to get the color reference counts right, so initialize them! */
4990 Lisp_Object black;
4991 struct gcpro gcpro1;
4993 /* Function x_decode_color can signal an error. Make
4994 sure to initialize color slots so that we won't try
4995 to free colors we haven't allocated. */
4996 FRAME_FOREGROUND_PIXEL (f) = -1;
4997 FRAME_BACKGROUND_PIXEL (f) = -1;
4998 f->output_data.x->cursor_pixel = -1;
4999 f->output_data.x->cursor_foreground_pixel = -1;
5000 f->output_data.x->border_pixel = -1;
5001 f->output_data.x->mouse_pixel = -1;
5003 black = build_string ("black");
5004 GCPRO1 (black);
5005 FRAME_FOREGROUND_PIXEL (f)
5006 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5007 FRAME_BACKGROUND_PIXEL (f)
5008 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5009 f->output_data.x->cursor_pixel
5010 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5011 f->output_data.x->cursor_foreground_pixel
5012 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5013 f->output_data.x->border_pixel
5014 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5015 f->output_data.x->mouse_pixel
5016 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5017 UNGCPRO;
5020 /* Set the name; the functions to which we pass f expect the name to
5021 be set. */
5022 if (EQ (name, Qunbound) || NILP (name))
5024 fset_name (f, build_string (dpyinfo->x_id_name));
5025 f->explicit_name = 0;
5027 else
5029 fset_name (f, name);
5030 f->explicit_name = 1;
5031 /* use the frame's title when getting resources for this frame. */
5032 specbind (Qx_resource_name, name);
5035 register_font_driver (&xfont_driver, f);
5036 #ifdef HAVE_FREETYPE
5037 #ifdef HAVE_XFT
5038 register_font_driver (&xftfont_driver, f);
5039 #else /* not HAVE_XFT */
5040 register_font_driver (&ftxfont_driver, f);
5041 #endif /* not HAVE_XFT */
5042 #endif /* HAVE_FREETYPE */
5044 x_default_parameter (f, parms, Qfont_backend, Qnil,
5045 "fontBackend", "FontBackend", RES_TYPE_STRING);
5047 /* Extract the window parameters from the supplied values that are
5048 needed to determine window geometry. */
5049 x_default_font_parameter (f, parms);
5051 x_default_parameter (f, parms, Qborder_width, make_number (0),
5052 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
5054 /* This defaults to 2 in order to match xterm. We recognize either
5055 internalBorderWidth or internalBorder (which is what xterm calls
5056 it). */
5057 if (NILP (Fassq (Qinternal_border_width, parms)))
5059 Lisp_Object value;
5061 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
5062 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
5063 if (! EQ (value, Qunbound))
5064 parms = Fcons (Fcons (Qinternal_border_width, value),
5065 parms);
5068 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
5069 "internalBorderWidth", "internalBorderWidth",
5070 RES_TYPE_NUMBER);
5071 x_default_parameter (f, parms, Qright_divider_width, make_number (0),
5072 NULL, NULL, RES_TYPE_NUMBER);
5073 x_default_parameter (f, parms, Qbottom_divider_width, make_number (0),
5074 NULL, NULL, RES_TYPE_NUMBER);
5076 /* Also do the stuff which must be set before the window exists. */
5077 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
5078 "foreground", "Foreground", RES_TYPE_STRING);
5079 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
5080 "background", "Background", RES_TYPE_STRING);
5081 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
5082 "pointerColor", "Foreground", RES_TYPE_STRING);
5083 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
5084 "cursorColor", "Foreground", RES_TYPE_STRING);
5085 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
5086 "borderColor", "BorderColor", RES_TYPE_STRING);
5088 #ifdef GLYPH_DEBUG
5089 image_cache_refcount =
5090 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
5091 dpyinfo_refcount = dpyinfo->reference_count;
5092 #endif /* GLYPH_DEBUG */
5094 /* Init faces before x_default_parameter is called for the
5095 scroll-bar-width parameter because otherwise we end up in
5096 init_iterator with a null face cache, which should not happen. */
5097 init_frame_faces (f);
5099 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
5101 x_figure_window_size (f, parms, 0);
5104 XSetWindowAttributes attrs;
5105 unsigned long mask;
5106 Atom type = FRAME_DISPLAY_INFO (f)->Xatom_net_window_type_tooltip;
5108 block_input ();
5109 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
5110 if (DoesSaveUnders (dpyinfo->screen))
5111 mask |= CWSaveUnder;
5113 /* Window managers look at the override-redirect flag to determine
5114 whether or net to give windows a decoration (Xlib spec, chapter
5115 3.2.8). */
5116 attrs.override_redirect = True;
5117 attrs.save_under = True;
5118 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
5119 /* Arrange for getting MapNotify and UnmapNotify events. */
5120 attrs.event_mask = StructureNotifyMask;
5121 tip_window
5122 = FRAME_X_WINDOW (f)
5123 = XCreateWindow (FRAME_X_DISPLAY (f),
5124 FRAME_DISPLAY_INFO (f)->root_window,
5125 /* x, y, width, height */
5126 0, 0, 1, 1,
5127 /* Border. */
5128 f->border_width,
5129 CopyFromParent, InputOutput, CopyFromParent,
5130 mask, &attrs);
5131 XChangeProperty (FRAME_X_DISPLAY (f), tip_window,
5132 FRAME_DISPLAY_INFO (f)->Xatom_net_window_type,
5133 XA_ATOM, 32, PropModeReplace,
5134 (unsigned char *)&type, 1);
5135 unblock_input ();
5138 x_make_gc (f);
5140 x_default_parameter (f, parms, Qauto_raise, Qnil,
5141 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5142 x_default_parameter (f, parms, Qauto_lower, Qnil,
5143 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5144 x_default_parameter (f, parms, Qcursor_type, Qbox,
5145 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5147 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
5148 Change will not be effected unless different from the current
5149 FRAME_LINES (f). */
5150 width = FRAME_COLS (f);
5151 height = FRAME_LINES (f);
5152 SET_FRAME_COLS (f, 0);
5153 SET_FRAME_LINES (f, 0);
5154 change_frame_size (f, width, height, 1, 0, 0, 0);
5156 /* Add `tooltip' frame parameter's default value. */
5157 if (NILP (Fframe_parameter (frame, Qtooltip)))
5159 AUTO_FRAME_ARG (arg, Qtooltip, Qt);
5160 Fmodify_frame_parameters (frame, arg);
5163 /* FIXME - can this be done in a similar way to normal frames?
5164 http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00641.html */
5166 /* Set the `display-type' frame parameter before setting up faces. */
5168 Lisp_Object disptype;
5170 if (FRAME_DISPLAY_INFO (f)->n_planes == 1)
5171 disptype = intern ("mono");
5172 else if (FRAME_DISPLAY_INFO (f)->visual->class == GrayScale
5173 || FRAME_DISPLAY_INFO (f)->visual->class == StaticGray)
5174 disptype = intern ("grayscale");
5175 else
5176 disptype = intern ("color");
5178 if (NILP (Fframe_parameter (frame, Qdisplay_type)))
5180 AUTO_FRAME_ARG (arg, Qdisplay_type, disptype);
5181 Fmodify_frame_parameters (frame, arg);
5185 /* Set up faces after all frame parameters are known. This call
5186 also merges in face attributes specified for new frames.
5188 Frame parameters may be changed if .Xdefaults contains
5189 specifications for the default font. For example, if there is an
5190 `Emacs.default.attributeBackground: pink', the `background-color'
5191 attribute of the frame get's set, which let's the internal border
5192 of the tooltip frame appear in pink. Prevent this. */
5194 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
5196 /* Set tip_frame here, so that */
5197 tip_frame = frame;
5198 call2 (Qface_set_after_frame_default, frame, Qnil);
5200 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
5202 AUTO_FRAME_ARG (arg, Qbackground_color, bg);
5203 Fmodify_frame_parameters (frame, arg);
5207 f->no_split = 1;
5209 UNGCPRO;
5211 /* Now that the frame will be official, it counts as a reference to
5212 its display and terminal. */
5213 FRAME_DISPLAY_INFO (f)->reference_count++;
5214 f->terminal->reference_count++;
5216 /* It is now ok to make the frame official even if we get an error
5217 below. And the frame needs to be on Vframe_list or making it
5218 visible won't work. */
5219 Vframe_list = Fcons (frame, Vframe_list);
5220 f->can_x_set_window_size = true;
5222 /* Setting attributes of faces of the tooltip frame from resources
5223 and similar will increment face_change_count, which leads to the
5224 clearing of all current matrices. Since this isn't necessary
5225 here, avoid it by resetting face_change_count to the value it
5226 had before we created the tip frame. */
5227 face_change_count = face_change_count_before;
5229 /* Discard the unwind_protect. */
5230 return unbind_to (count, frame);
5234 /* Compute where to display tip frame F. PARMS is the list of frame
5235 parameters for F. DX and DY are specified offsets from the current
5236 location of the mouse. WIDTH and HEIGHT are the width and height
5237 of the tooltip. Return coordinates relative to the root window of
5238 the display in *ROOT_X, and *ROOT_Y. */
5240 static void
5241 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)
5243 Lisp_Object left, top;
5244 int win_x, win_y;
5245 Window root, child;
5246 unsigned pmask;
5248 /* User-specified position? */
5249 left = Fcdr (Fassq (Qleft, parms));
5250 top = Fcdr (Fassq (Qtop, parms));
5252 /* Move the tooltip window where the mouse pointer is. Resize and
5253 show it. */
5254 if (!INTEGERP (left) || !INTEGERP (top))
5256 block_input ();
5257 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_DISPLAY_INFO (f)->root_window,
5258 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
5259 unblock_input ();
5262 if (INTEGERP (top))
5263 *root_y = XINT (top);
5264 else if (*root_y + XINT (dy) <= 0)
5265 *root_y = 0; /* Can happen for negative dy */
5266 else if (*root_y + XINT (dy) + height
5267 <= x_display_pixel_height (FRAME_DISPLAY_INFO (f)))
5268 /* It fits below the pointer */
5269 *root_y += XINT (dy);
5270 else if (height + XINT (dy) <= *root_y)
5271 /* It fits above the pointer. */
5272 *root_y -= height + XINT (dy);
5273 else
5274 /* Put it on the top. */
5275 *root_y = 0;
5277 if (INTEGERP (left))
5278 *root_x = XINT (left);
5279 else if (*root_x + XINT (dx) <= 0)
5280 *root_x = 0; /* Can happen for negative dx */
5281 else if (*root_x + XINT (dx) + width
5282 <= x_display_pixel_width (FRAME_DISPLAY_INFO (f)))
5283 /* It fits to the right of the pointer. */
5284 *root_x += XINT (dx);
5285 else if (width + XINT (dx) <= *root_x)
5286 /* It fits to the left of the pointer. */
5287 *root_x -= width + XINT (dx);
5288 else
5289 /* Put it left-justified on the screen--it ought to fit that way. */
5290 *root_x = 0;
5294 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
5295 doc: /* Show STRING in a "tooltip" window on frame FRAME.
5296 A tooltip window is a small X window displaying a string.
5298 This is an internal function; Lisp code should call `tooltip-show'.
5300 FRAME nil or omitted means use the selected frame.
5302 PARMS is an optional list of frame parameters which can be used to
5303 change the tooltip's appearance.
5305 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
5306 means use the default timeout of 5 seconds.
5308 If the list of frame parameters PARMS contains a `left' parameters,
5309 the tooltip is displayed at that x-position. Otherwise it is
5310 displayed at the mouse position, with offset DX added (default is 5 if
5311 DX isn't specified). Likewise for the y-position; if a `top' frame
5312 parameter is specified, it determines the y-position of the tooltip
5313 window, otherwise it is displayed at the mouse position, with offset
5314 DY added (default is -10).
5316 A tooltip's maximum size is specified by `x-max-tooltip-size'.
5317 Text larger than the specified size is clipped. */)
5318 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
5320 struct frame *f;
5321 struct window *w;
5322 int root_x, root_y;
5323 struct buffer *old_buffer;
5324 struct text_pos pos;
5325 int i, width, height, seen_reversed_p;
5326 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5327 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5328 ptrdiff_t count = SPECPDL_INDEX ();
5330 specbind (Qinhibit_redisplay, Qt);
5332 GCPRO4 (string, parms, frame, timeout);
5334 CHECK_STRING (string);
5335 if (SCHARS (string) == 0)
5336 string = make_unibyte_string (" ", 1);
5338 f = decode_window_system_frame (frame);
5339 if (NILP (timeout))
5340 timeout = make_number (5);
5341 else
5342 CHECK_NATNUM (timeout);
5344 if (NILP (dx))
5345 dx = make_number (5);
5346 else
5347 CHECK_NUMBER (dx);
5349 if (NILP (dy))
5350 dy = make_number (-10);
5351 else
5352 CHECK_NUMBER (dy);
5354 #ifdef USE_GTK
5355 if (x_gtk_use_system_tooltips)
5357 bool ok;
5359 /* Hide a previous tip, if any. */
5360 Fx_hide_tip ();
5362 block_input ();
5363 ok = xg_prepare_tooltip (f, string, &width, &height);
5364 if (ok)
5366 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5367 xg_show_tooltip (f, root_x, root_y);
5368 /* This is used in Fx_hide_tip. */
5369 XSETFRAME (tip_frame, f);
5371 unblock_input ();
5372 if (ok) goto start_timer;
5374 #endif /* USE_GTK */
5376 if (NILP (last_show_tip_args))
5377 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5379 if (!NILP (tip_frame))
5381 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5382 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5383 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5385 if (EQ (frame, last_frame)
5386 && !NILP (Fequal (last_string, string))
5387 && !NILP (Fequal (last_parms, parms)))
5389 struct frame *tip_f = XFRAME (tip_frame);
5391 /* Only DX and DY have changed. */
5392 if (!NILP (tip_timer))
5394 Lisp_Object timer = tip_timer;
5395 tip_timer = Qnil;
5396 call1 (Qcancel_timer, timer);
5399 block_input ();
5400 compute_tip_xy (tip_f, parms, dx, dy, FRAME_PIXEL_WIDTH (tip_f),
5401 FRAME_PIXEL_HEIGHT (tip_f), &root_x, &root_y);
5402 XMoveWindow (FRAME_X_DISPLAY (tip_f), FRAME_X_WINDOW (tip_f),
5403 root_x, root_y);
5404 unblock_input ();
5405 goto start_timer;
5409 /* Hide a previous tip, if any. */
5410 Fx_hide_tip ();
5412 ASET (last_show_tip_args, 0, string);
5413 ASET (last_show_tip_args, 1, frame);
5414 ASET (last_show_tip_args, 2, parms);
5416 /* Add default values to frame parameters. */
5417 if (NILP (Fassq (Qname, parms)))
5418 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5419 if (NILP (Fassq (Qinternal_border_width, parms)))
5420 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5421 if (NILP (Fassq (Qborder_width, parms)))
5422 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5423 if (NILP (Fassq (Qbottom_divider_width, parms)))
5424 parms = Fcons (Fcons (Qbottom_divider_width, make_number (0)), parms);
5425 if (NILP (Fassq (Qright_divider_width, parms)))
5426 parms = Fcons (Fcons (Qright_divider_width, make_number (0)), parms);
5427 if (NILP (Fassq (Qborder_color, parms)))
5428 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5429 if (NILP (Fassq (Qbackground_color, parms)))
5430 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5431 parms);
5433 /* Create a frame for the tooltip, and record it in the global
5434 variable tip_frame. */
5435 frame = x_create_tip_frame (FRAME_DISPLAY_INFO (f), parms, string);
5436 f = XFRAME (frame);
5438 /* Set up the frame's root window. */
5439 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5440 w->left_col = 0;
5441 w->top_line = 0;
5442 w->pixel_left = 0;
5443 w->pixel_top = 0;
5445 if (CONSP (Vx_max_tooltip_size)
5446 && RANGED_INTEGERP (1, XCAR (Vx_max_tooltip_size), INT_MAX)
5447 && RANGED_INTEGERP (1, XCDR (Vx_max_tooltip_size), INT_MAX))
5449 w->total_cols = XFASTINT (XCAR (Vx_max_tooltip_size));
5450 w->total_lines = XFASTINT (XCDR (Vx_max_tooltip_size));
5452 else
5454 w->total_cols = 80;
5455 w->total_lines = 40;
5458 w->pixel_width = w->total_cols * FRAME_COLUMN_WIDTH (f);
5459 w->pixel_height = w->total_lines * FRAME_LINE_HEIGHT (f);
5461 FRAME_TOTAL_COLS (f) = w->total_cols;
5462 adjust_frame_glyphs (f);
5463 w->pseudo_window_p = 1;
5465 /* Display the tooltip text in a temporary buffer. */
5466 old_buffer = current_buffer;
5467 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->contents));
5468 bset_truncate_lines (current_buffer, Qnil);
5469 clear_glyph_matrix (w->desired_matrix);
5470 clear_glyph_matrix (w->current_matrix);
5471 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5472 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
5474 /* Compute width and height of the tooltip. */
5475 width = height = seen_reversed_p = 0;
5476 for (i = 0; i < w->desired_matrix->nrows; ++i)
5478 struct glyph_row *row = &w->desired_matrix->rows[i];
5479 struct glyph *last;
5480 int row_width;
5482 /* Stop at the first empty row at the end. */
5483 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5484 break;
5486 /* Let the row go over the full width of the frame. */
5487 row->full_width_p = 1;
5489 row_width = row->pixel_width;
5490 if (row->used[TEXT_AREA])
5492 /* There's a glyph at the end of rows that is used to place
5493 the cursor there. Don't include the width of this glyph. */
5494 if (!row->reversed_p)
5496 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5497 if (NILP (last->object))
5498 row_width -= last->pixel_width;
5500 else
5502 /* There could be a stretch glyph at the beginning of R2L
5503 rows that is produced by extend_face_to_end_of_line.
5504 Don't count that glyph. */
5505 struct glyph *g = row->glyphs[TEXT_AREA];
5507 if (g->type == STRETCH_GLYPH && NILP (g->object))
5509 row_width -= g->pixel_width;
5510 seen_reversed_p = 1;
5515 height += row->height;
5516 width = max (width, row_width);
5519 /* If we've seen partial-length R2L rows, we need to re-adjust the
5520 tool-tip frame width and redisplay it again, to avoid over-wide
5521 tips due to the stretch glyph that extends R2L lines to full
5522 width of the frame. */
5523 if (seen_reversed_p)
5525 /* w->total_cols and FRAME_TOTAL_COLS want the width in columns,
5526 not in pixels. */
5527 w->pixel_width = width;
5528 width /= WINDOW_FRAME_COLUMN_WIDTH (w);
5529 w->total_cols = width;
5530 FRAME_TOTAL_COLS (f) = width;
5531 SET_FRAME_WIDTH (f, width);
5532 adjust_frame_glyphs (f);
5533 clear_glyph_matrix (w->desired_matrix);
5534 clear_glyph_matrix (w->current_matrix);
5535 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
5536 width = height = 0;
5537 /* Recompute width and height of the tooltip. */
5538 for (i = 0; i < w->desired_matrix->nrows; ++i)
5540 struct glyph_row *row = &w->desired_matrix->rows[i];
5541 struct glyph *last;
5542 int row_width;
5544 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5545 break;
5546 row->full_width_p = 1;
5547 row_width = row->pixel_width;
5548 if (row->used[TEXT_AREA] && !row->reversed_p)
5550 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5551 if (NILP (last->object))
5552 row_width -= last->pixel_width;
5555 height += row->height;
5556 width = max (width, row_width);
5560 /* Add the frame's internal border to the width and height the X
5561 window should have. */
5562 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5563 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5565 /* Move the tooltip window where the mouse pointer is. Resize and
5566 show it. */
5567 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5569 block_input ();
5570 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5571 root_x, root_y, width, height);
5572 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5573 unblock_input ();
5575 /* Draw into the window. */
5576 w->must_be_updated_p = true;
5577 update_single_window (w);
5579 /* Restore original current buffer. */
5580 set_buffer_internal_1 (old_buffer);
5581 windows_or_buffers_changed = old_windows_or_buffers_changed;
5583 start_timer:
5584 /* Let the tip disappear after timeout seconds. */
5585 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5586 intern ("x-hide-tip"));
5588 UNGCPRO;
5589 return unbind_to (count, Qnil);
5593 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5594 doc: /* Hide the current tooltip window, if there is any.
5595 Value is t if tooltip was open, nil otherwise. */)
5596 (void)
5598 ptrdiff_t count;
5599 Lisp_Object deleted, frame, timer;
5600 struct gcpro gcpro1, gcpro2;
5602 /* Return quickly if nothing to do. */
5603 if (NILP (tip_timer) && NILP (tip_frame))
5604 return Qnil;
5606 frame = tip_frame;
5607 timer = tip_timer;
5608 GCPRO2 (frame, timer);
5609 tip_frame = tip_timer = deleted = Qnil;
5611 count = SPECPDL_INDEX ();
5612 specbind (Qinhibit_redisplay, Qt);
5613 specbind (Qinhibit_quit, Qt);
5615 if (!NILP (timer))
5616 call1 (Qcancel_timer, timer);
5618 #ifdef USE_GTK
5620 /* When using system tooltip, tip_frame is the Emacs frame on which
5621 the tip is shown. */
5622 struct frame *f = XFRAME (frame);
5623 if (FRAME_LIVE_P (f) && xg_hide_tooltip (f))
5624 frame = Qnil;
5626 #endif
5628 if (FRAMEP (frame))
5630 delete_frame (frame, Qnil);
5631 deleted = Qt;
5633 #ifdef USE_LUCID
5634 /* Bloodcurdling hack alert: The Lucid menu bar widget's
5635 redisplay procedure is not called when a tip frame over menu
5636 items is unmapped. Redisplay the menu manually... */
5638 Widget w;
5639 struct frame *f = SELECTED_FRAME ();
5640 w = f->output_data.x->menubar_widget;
5642 if (!DoesSaveUnders (FRAME_DISPLAY_INFO (f)->screen)
5643 && w != NULL)
5645 block_input ();
5646 xlwmenu_redisplay (w);
5647 unblock_input ();
5650 #endif /* USE_LUCID */
5653 UNGCPRO;
5654 return unbind_to (count, deleted);
5659 /***********************************************************************
5660 File selection dialog
5661 ***********************************************************************/
5663 DEFUN ("x-uses-old-gtk-dialog", Fx_uses_old_gtk_dialog,
5664 Sx_uses_old_gtk_dialog,
5665 0, 0, 0,
5666 doc: /* Return t if the old Gtk+ file selection dialog is used. */)
5667 (void)
5669 #ifdef USE_GTK
5670 if (use_dialog_box
5671 && use_file_dialog
5672 && window_system_available (SELECTED_FRAME ())
5673 && xg_uses_old_file_dialog ())
5674 return Qt;
5675 #endif
5676 return Qnil;
5680 #ifdef USE_MOTIF
5681 /* Callback for "OK" and "Cancel" on file selection dialog. */
5683 static void
5684 file_dialog_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5686 int *result = client_data;
5687 XmAnyCallbackStruct *cb = call_data;
5688 *result = cb->reason;
5692 /* Callback for unmapping a file selection dialog. This is used to
5693 capture the case where a dialog is closed via a window manager's
5694 closer button, for example. Using a XmNdestroyCallback didn't work
5695 in this case. */
5697 static void
5698 file_dialog_unmap_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5700 int *result = client_data;
5701 *result = XmCR_CANCEL;
5704 static void
5705 clean_up_file_dialog (void *arg)
5707 Widget dialog = arg;
5709 /* Clean up. */
5710 block_input ();
5711 XtUnmanageChild (dialog);
5712 XtDestroyWidget (dialog);
5713 x_menu_set_in_use (0);
5714 unblock_input ();
5718 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5719 doc: /* Read file name, prompting with PROMPT in directory DIR.
5720 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5721 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5722 or directory must exist.
5724 This function is only defined on NS, MS Windows, and X Windows with the
5725 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5726 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories.
5727 On Windows 7 and later, the file selection dialog "remembers" the last
5728 directory where the user selected a file, and will open that directory
5729 instead of DIR on subsequent invocations of this function with the same
5730 value of DIR as in previous invocations; this is standard Windows behavior. */)
5731 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename,
5732 Lisp_Object mustmatch, Lisp_Object only_dir_p)
5734 int result;
5735 struct frame *f = SELECTED_FRAME ();
5736 Lisp_Object file = Qnil;
5737 Lisp_Object decoded_file;
5738 Widget dialog, text, help;
5739 Arg al[10];
5740 int ac = 0;
5741 XmString dir_xmstring, pattern_xmstring;
5742 ptrdiff_t count = SPECPDL_INDEX ();
5743 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5745 check_window_system (f);
5747 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5749 if (popup_activated ())
5750 error ("Trying to use a menu from within a menu-entry");
5752 CHECK_STRING (prompt);
5753 CHECK_STRING (dir);
5755 /* Prevent redisplay. */
5756 specbind (Qinhibit_redisplay, Qt);
5758 block_input ();
5760 /* Create the dialog with PROMPT as title, using DIR as initial
5761 directory and using "*" as pattern. */
5762 dir = Fexpand_file_name (dir, Qnil);
5763 dir_xmstring = XmStringCreateLocalized (SSDATA (dir));
5764 pattern_xmstring = XmStringCreateLocalized ("*");
5766 XtSetArg (al[ac], XmNtitle, SDATA (prompt)); ++ac;
5767 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
5768 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
5769 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
5770 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
5771 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
5772 "fsb", al, ac);
5773 XmStringFree (dir_xmstring);
5774 XmStringFree (pattern_xmstring);
5776 /* Add callbacks for OK and Cancel. */
5777 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
5778 (XtPointer) &result);
5779 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
5780 (XtPointer) &result);
5781 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
5782 (XtPointer) &result);
5784 /* Remove the help button since we can't display help. */
5785 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
5786 XtUnmanageChild (help);
5788 /* Mark OK button as default. */
5789 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
5790 XmNshowAsDefault, True, NULL);
5792 /* If MUSTMATCH is non-nil, disable the file entry field of the
5793 dialog, so that the user must select a file from the files list
5794 box. We can't remove it because we wouldn't have a way to get at
5795 the result file name, then. */
5796 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5797 if (!NILP (mustmatch))
5799 Widget label;
5800 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
5801 XtSetSensitive (text, False);
5802 XtSetSensitive (label, False);
5805 /* Manage the dialog, so that list boxes get filled. */
5806 XtManageChild (dialog);
5808 if (STRINGP (default_filename))
5810 XmString default_xmstring;
5811 Widget wtext = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5812 Widget list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
5814 XmTextPosition last_pos = XmTextFieldGetLastPosition (wtext);
5815 XmTextFieldReplace (wtext, 0, last_pos,
5816 (SSDATA (Ffile_name_nondirectory (default_filename))));
5818 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
5819 must include the path for this to work. */
5821 default_xmstring = XmStringCreateLocalized (SSDATA (default_filename));
5823 if (XmListItemExists (list, default_xmstring))
5825 int item_pos = XmListItemPos (list, default_xmstring);
5826 /* Select the item and scroll it into view. */
5827 XmListSelectPos (list, item_pos, True);
5828 XmListSetPos (list, item_pos);
5831 XmStringFree (default_xmstring);
5834 record_unwind_protect_ptr (clean_up_file_dialog, dialog);
5836 /* Process events until the user presses Cancel or OK. */
5837 x_menu_set_in_use (1);
5838 result = 0;
5839 while (result == 0)
5841 XEvent event;
5842 x_menu_wait_for_event (0);
5843 XtAppNextEvent (Xt_app_con, &event);
5844 if (event.type == KeyPress
5845 && FRAME_X_DISPLAY (f) == event.xkey.display)
5847 KeySym keysym = XLookupKeysym (&event.xkey, 0);
5849 /* Pop down on C-g. */
5850 if (keysym == XK_g && (event.xkey.state & ControlMask) != 0)
5851 XtUnmanageChild (dialog);
5854 (void) x_dispatch_event (&event, FRAME_X_DISPLAY (f));
5857 /* Get the result. */
5858 if (result == XmCR_OK)
5860 XmString text_string;
5861 String data;
5863 XtVaGetValues (dialog, XmNtextString, &text_string, NULL);
5864 XmStringGetLtoR (text_string, XmFONTLIST_DEFAULT_TAG, &data);
5865 XmStringFree (text_string);
5866 file = build_string (data);
5867 XtFree (data);
5869 else
5870 file = Qnil;
5872 unblock_input ();
5873 UNGCPRO;
5875 /* Make "Cancel" equivalent to C-g. */
5876 if (NILP (file))
5877 Fsignal (Qquit, Qnil);
5879 decoded_file = DECODE_FILE (file);
5881 return unbind_to (count, decoded_file);
5884 #endif /* USE_MOTIF */
5886 #ifdef USE_GTK
5888 static void
5889 clean_up_dialog (void)
5891 x_menu_set_in_use (0);
5894 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5895 doc: /* Read file name, prompting with PROMPT in directory DIR.
5896 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5897 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5898 or directory must exist.
5900 This function is only defined on NS, MS Windows, and X Windows with the
5901 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5902 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories.
5903 On Windows 7 and later, the file selection dialog "remembers" the last
5904 directory where the user selected a file, and will open that directory
5905 instead of DIR on subsequent invocations of this function with the same
5906 value of DIR as in previous invocations; this is standard Windows behavior. */)
5907 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
5909 struct frame *f = SELECTED_FRAME ();
5910 char *fn;
5911 Lisp_Object file = Qnil;
5912 Lisp_Object decoded_file;
5913 ptrdiff_t count = SPECPDL_INDEX ();
5914 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5915 char *cdef_file;
5917 check_window_system (f);
5919 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5921 if (popup_activated ())
5922 error ("Trying to use a menu from within a menu-entry");
5924 CHECK_STRING (prompt);
5925 CHECK_STRING (dir);
5927 /* Prevent redisplay. */
5928 specbind (Qinhibit_redisplay, Qt);
5929 record_unwind_protect_void (clean_up_dialog);
5931 block_input ();
5933 if (STRINGP (default_filename))
5934 cdef_file = SSDATA (default_filename);
5935 else
5936 cdef_file = SSDATA (dir);
5938 fn = xg_get_file_name (f, SSDATA (prompt), cdef_file,
5939 ! NILP (mustmatch),
5940 ! NILP (only_dir_p));
5942 if (fn)
5944 file = build_string (fn);
5945 xfree (fn);
5948 unblock_input ();
5949 UNGCPRO;
5951 /* Make "Cancel" equivalent to C-g. */
5952 if (NILP (file))
5953 Fsignal (Qquit, Qnil);
5955 decoded_file = DECODE_FILE (file);
5957 return unbind_to (count, decoded_file);
5961 #ifdef HAVE_FREETYPE
5963 DEFUN ("x-select-font", Fx_select_font, Sx_select_font, 0, 2, 0,
5964 doc: /* Read a font using a GTK dialog.
5965 Return either a font spec (for GTK versions >= 3.2) or a string
5966 containing a GTK-style font name.
5968 FRAME is the frame on which to pop up the font chooser. If omitted or
5969 nil, it defaults to the selected frame. */)
5970 (Lisp_Object frame, Lisp_Object ignored)
5972 struct frame *f = decode_window_system_frame (frame);
5973 Lisp_Object font;
5974 Lisp_Object font_param;
5975 char *default_name = NULL;
5976 struct gcpro gcpro1, gcpro2;
5977 ptrdiff_t count = SPECPDL_INDEX ();
5979 if (popup_activated ())
5980 error ("Trying to use a menu from within a menu-entry");
5982 /* Prevent redisplay. */
5983 specbind (Qinhibit_redisplay, Qt);
5984 record_unwind_protect_void (clean_up_dialog);
5986 block_input ();
5988 GCPRO2 (font_param, font);
5990 XSETFONT (font, FRAME_FONT (f));
5991 font_param = Ffont_get (font, intern (":name"));
5992 if (STRINGP (font_param))
5993 default_name = xstrdup (SSDATA (font_param));
5994 else
5996 font_param = Fframe_parameter (frame, Qfont_param);
5997 if (STRINGP (font_param))
5998 default_name = xstrdup (SSDATA (font_param));
6001 font = xg_get_font (f, default_name);
6002 xfree (default_name);
6004 unblock_input ();
6006 if (NILP (font))
6007 Fsignal (Qquit, Qnil);
6009 return unbind_to (count, font);
6011 #endif /* HAVE_FREETYPE */
6013 #endif /* USE_GTK */
6016 /***********************************************************************
6017 Keyboard
6018 ***********************************************************************/
6020 #ifdef HAVE_XKB
6021 #include <X11/XKBlib.h>
6022 #include <X11/keysym.h>
6023 #endif
6025 DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
6026 Sx_backspace_delete_keys_p, 0, 1, 0,
6027 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
6028 FRAME nil means use the selected frame.
6029 Value is t if we know that both keys are present, and are mapped to the
6030 usual X keysyms. Value is `lambda' if we cannot determine if both keys are
6031 present and mapped to the usual X keysyms. */)
6032 (Lisp_Object frame)
6034 #ifndef HAVE_XKB
6035 return Qlambda;
6036 #else
6037 XkbDescPtr kb;
6038 struct frame *f = decode_window_system_frame (frame);
6039 Display *dpy = FRAME_X_DISPLAY (f);
6040 Lisp_Object have_keys;
6041 int major, minor, op, event, error_code;
6043 block_input ();
6045 /* Check library version in case we're dynamically linked. */
6046 major = XkbMajorVersion;
6047 minor = XkbMinorVersion;
6048 if (!XkbLibraryVersion (&major, &minor))
6050 unblock_input ();
6051 return Qlambda;
6054 /* Check that the server supports XKB. */
6055 major = XkbMajorVersion;
6056 minor = XkbMinorVersion;
6057 if (!XkbQueryExtension (dpy, &op, &event, &error_code, &major, &minor))
6059 unblock_input ();
6060 return Qlambda;
6063 /* In this code we check that the keyboard has physical keys with names
6064 that start with BKSP (Backspace) and DELE (Delete), and that they
6065 generate keysym XK_BackSpace and XK_Delete respectively.
6066 This function is used to test if normal-erase-is-backspace should be
6067 turned on.
6068 An alternative approach would be to just check if XK_BackSpace and
6069 XK_Delete are mapped to any key. But if any of those are mapped to
6070 some non-intuitive key combination (Meta-Shift-Ctrl-whatever) and the
6071 user doesn't know about it, it is better to return false here.
6072 It is more obvious to the user what to do if she/he has two keys
6073 clearly marked with names/symbols and one key does something not
6074 expected (i.e. she/he then tries the other).
6075 The cases where Backspace/Delete is mapped to some other key combination
6076 are rare, and in those cases, normal-erase-is-backspace can be turned on
6077 manually. */
6079 have_keys = Qnil;
6080 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
6081 if (kb)
6083 int delete_keycode = 0, backspace_keycode = 0, i;
6085 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
6087 for (i = kb->min_key_code;
6088 (i < kb->max_key_code
6089 && (delete_keycode == 0 || backspace_keycode == 0));
6090 ++i)
6092 /* The XKB symbolic key names can be seen most easily in
6093 the PS file generated by `xkbprint -label name
6094 $DISPLAY'. */
6095 if (memcmp ("DELE", kb->names->keys[i].name, 4) == 0)
6096 delete_keycode = i;
6097 else if (memcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
6098 backspace_keycode = i;
6101 XkbFreeNames (kb, 0, True);
6104 /* As of libX11-1.6.2, XkbGetMap manual says that you should use
6105 XkbFreeClientMap to free the data returned by XkbGetMap. But
6106 this function just frees the data referenced from KB and not
6107 KB itself. To free KB as well, call XkbFreeKeyboard. */
6108 XkbFreeKeyboard (kb, XkbAllMapComponentsMask, True);
6110 if (delete_keycode
6111 && backspace_keycode
6112 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
6113 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
6114 have_keys = Qt;
6116 unblock_input ();
6117 return have_keys;
6118 #endif
6123 /***********************************************************************
6124 Initialization
6125 ***********************************************************************/
6127 /* Keep this list in the same order as frame_parms in frame.c.
6128 Use 0 for unsupported frame parameters. */
6130 frame_parm_handler x_frame_parm_handlers[] =
6132 x_set_autoraise,
6133 x_set_autolower,
6134 x_set_background_color,
6135 x_set_border_color,
6136 x_set_border_width,
6137 x_set_cursor_color,
6138 x_set_cursor_type,
6139 x_set_font,
6140 x_set_foreground_color,
6141 x_set_icon_name,
6142 x_set_icon_type,
6143 x_set_internal_border_width,
6144 x_set_right_divider_width,
6145 x_set_bottom_divider_width,
6146 x_set_menu_bar_lines,
6147 x_set_mouse_color,
6148 x_explicitly_set_name,
6149 x_set_scroll_bar_width,
6150 x_set_scroll_bar_height,
6151 x_set_title,
6152 x_set_unsplittable,
6153 x_set_vertical_scroll_bars,
6154 x_set_horizontal_scroll_bars,
6155 x_set_visibility,
6156 x_set_tool_bar_lines,
6157 x_set_scroll_bar_foreground,
6158 x_set_scroll_bar_background,
6159 x_set_screen_gamma,
6160 x_set_line_spacing,
6161 x_set_left_fringe,
6162 x_set_right_fringe,
6163 x_set_wait_for_wm,
6164 x_set_fullscreen,
6165 x_set_font_backend,
6166 x_set_alpha,
6167 x_set_sticky,
6168 x_set_tool_bar_position,
6171 void
6172 syms_of_xfns (void)
6174 DEFSYM (Qundefined_color, "undefined-color");
6175 DEFSYM (Qcompound_text, "compound-text");
6176 DEFSYM (Qcancel_timer, "cancel-timer");
6177 DEFSYM (Qfont_param, "font-parameter");
6179 Fput (Qundefined_color, Qerror_conditions,
6180 listn (CONSTYPE_PURE, 2, Qundefined_color, Qerror));
6181 Fput (Qundefined_color, Qerror_message,
6182 build_pure_c_string ("Undefined color"));
6184 DEFVAR_LISP ("x-pointer-shape", Vx_pointer_shape,
6185 doc: /* The shape of the pointer when over text.
6186 Changing the value does not affect existing frames
6187 unless you set the mouse color. */);
6188 Vx_pointer_shape = Qnil;
6190 #if 0 /* This doesn't really do anything. */
6191 DEFVAR_LISP ("x-nontext-pointer-shape", Vx_nontext_pointer_shape,
6192 doc: /* The shape of the pointer when not over text.
6193 This variable takes effect when you create a new frame
6194 or when you set the mouse color. */);
6195 #endif
6196 Vx_nontext_pointer_shape = Qnil;
6198 DEFVAR_LISP ("x-hourglass-pointer-shape", Vx_hourglass_pointer_shape,
6199 doc: /* The shape of the pointer when Emacs is busy.
6200 This variable takes effect when you create a new frame
6201 or when you set the mouse color. */);
6202 Vx_hourglass_pointer_shape = Qnil;
6204 #if 0 /* This doesn't really do anything. */
6205 DEFVAR_LISP ("x-mode-pointer-shape", Vx_mode_pointer_shape,
6206 doc: /* The shape of the pointer when over the mode line.
6207 This variable takes effect when you create a new frame
6208 or when you set the mouse color. */);
6209 #endif
6210 Vx_mode_pointer_shape = Qnil;
6212 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
6213 Vx_sensitive_text_pointer_shape,
6214 doc: /* The shape of the pointer when over mouse-sensitive text.
6215 This variable takes effect when you create a new frame
6216 or when you set the mouse color. */);
6217 Vx_sensitive_text_pointer_shape = Qnil;
6219 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
6220 Vx_window_horizontal_drag_shape,
6221 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
6222 This variable takes effect when you create a new frame
6223 or when you set the mouse color. */);
6224 Vx_window_horizontal_drag_shape = Qnil;
6226 DEFVAR_LISP ("x-window-vertical-drag-cursor",
6227 Vx_window_vertical_drag_shape,
6228 doc: /* Pointer shape to use for indicating a window can be dragged vertically.
6229 This variable takes effect when you create a new frame
6230 or when you set the mouse color. */);
6231 Vx_window_vertical_drag_shape = Qnil;
6233 DEFVAR_LISP ("x-cursor-fore-pixel", Vx_cursor_fore_pixel,
6234 doc: /* A string indicating the foreground color of the cursor box. */);
6235 Vx_cursor_fore_pixel = Qnil;
6237 DEFVAR_LISP ("x-max-tooltip-size", Vx_max_tooltip_size,
6238 doc: /* Maximum size for tooltips.
6239 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
6240 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
6242 DEFVAR_LISP ("x-no-window-manager", Vx_no_window_manager,
6243 doc: /* Non-nil if no X window manager is in use.
6244 Emacs doesn't try to figure this out; this is always nil
6245 unless you set it to something else. */);
6246 /* We don't have any way to find this out, so set it to nil
6247 and maybe the user would like to set it to t. */
6248 Vx_no_window_manager = Qnil;
6250 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
6251 Vx_pixel_size_width_font_regexp,
6252 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
6254 Since Emacs gets width of a font matching with this regexp from
6255 PIXEL_SIZE field of the name, font finding mechanism gets faster for
6256 such a font. This is especially effective for such large fonts as
6257 Chinese, Japanese, and Korean. */);
6258 Vx_pixel_size_width_font_regexp = Qnil;
6260 /* This is not ifdef:ed, so other builds than GTK can customize it. */
6261 DEFVAR_BOOL ("x-gtk-use-old-file-dialog", x_gtk_use_old_file_dialog,
6262 doc: /* Non-nil means prompt with the old GTK file selection dialog.
6263 If nil or if the file selection dialog is not available, the new GTK file
6264 chooser is used instead. To turn off all file dialogs set the
6265 variable `use-file-dialog'. */);
6266 x_gtk_use_old_file_dialog = 0;
6268 DEFVAR_BOOL ("x-gtk-show-hidden-files", x_gtk_show_hidden_files,
6269 doc: /* If non-nil, the GTK file chooser will by default show hidden files.
6270 Note that this is just the default, there is a toggle button on the file
6271 chooser to show or not show hidden files on a case by case basis. */);
6272 x_gtk_show_hidden_files = 0;
6274 DEFVAR_BOOL ("x-gtk-file-dialog-help-text", x_gtk_file_dialog_help_text,
6275 doc: /* If non-nil, the GTK file chooser will show additional help text.
6276 If more space for files in the file chooser dialog is wanted, set this to nil
6277 to turn the additional text off. */);
6278 x_gtk_file_dialog_help_text = 1;
6280 DEFVAR_BOOL ("x-gtk-use-system-tooltips", x_gtk_use_system_tooltips,
6281 doc: /* If non-nil with a Gtk+ built Emacs, the Gtk+ tooltip is used.
6282 Otherwise use Emacs own tooltip implementation.
6283 When using Gtk+ tooltips, the tooltip face is not used. */);
6284 x_gtk_use_system_tooltips = 1;
6286 /* Tell Emacs about this window system. */
6287 Fprovide (Qx, Qnil);
6289 #ifdef USE_X_TOOLKIT
6290 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6291 #ifdef USE_MOTIF
6292 Fprovide (intern_c_string ("motif"), Qnil);
6294 DEFVAR_LISP ("motif-version-string", Vmotif_version_string,
6295 doc: /* Version info for LessTif/Motif. */);
6296 Vmotif_version_string = build_string (XmVERSION_STRING);
6297 #endif /* USE_MOTIF */
6298 #endif /* USE_X_TOOLKIT */
6300 #ifdef USE_GTK
6301 /* Provide x-toolkit also for GTK. Internally GTK does not use Xt so it
6302 is not an X toolkit in that sense (USE_X_TOOLKIT is not defined).
6303 But for a user it is a toolkit for X, and indeed, configure
6304 accepts --with-x-toolkit=gtk. */
6305 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6306 Fprovide (intern_c_string ("gtk"), Qnil);
6307 Fprovide (intern_c_string ("move-toolbar"), Qnil);
6309 DEFVAR_LISP ("gtk-version-string", Vgtk_version_string,
6310 doc: /* Version info for GTK+. */);
6312 char gtk_version[sizeof ".." + 3 * INT_STRLEN_BOUND (int)];
6313 int len = sprintf (gtk_version, "%d.%d.%d",
6314 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
6315 Vgtk_version_string = make_pure_string (gtk_version, len, len, 0);
6317 #endif /* USE_GTK */
6319 /* X window properties. */
6320 defsubr (&Sx_change_window_property);
6321 defsubr (&Sx_delete_window_property);
6322 defsubr (&Sx_window_property);
6324 defsubr (&Sxw_display_color_p);
6325 defsubr (&Sx_display_grayscale_p);
6326 defsubr (&Sxw_color_defined_p);
6327 defsubr (&Sxw_color_values);
6328 defsubr (&Sx_server_max_request_size);
6329 defsubr (&Sx_server_vendor);
6330 defsubr (&Sx_server_version);
6331 defsubr (&Sx_display_pixel_width);
6332 defsubr (&Sx_display_pixel_height);
6333 defsubr (&Sx_display_mm_width);
6334 defsubr (&Sx_display_mm_height);
6335 defsubr (&Sx_display_screens);
6336 defsubr (&Sx_display_planes);
6337 defsubr (&Sx_display_color_cells);
6338 defsubr (&Sx_display_visual_class);
6339 defsubr (&Sx_display_backing_store);
6340 defsubr (&Sx_display_save_under);
6341 defsubr (&Sx_display_monitor_attributes_list);
6342 defsubr (&Sx_frame_geometry);
6343 defsubr (&Sx_wm_set_size_hint);
6344 defsubr (&Sx_create_frame);
6345 defsubr (&Sx_open_connection);
6346 defsubr (&Sx_close_connection);
6347 defsubr (&Sx_display_list);
6348 defsubr (&Sx_synchronize);
6349 defsubr (&Sx_backspace_delete_keys_p);
6351 defsubr (&Sx_show_tip);
6352 defsubr (&Sx_hide_tip);
6353 tip_timer = Qnil;
6354 staticpro (&tip_timer);
6355 tip_frame = Qnil;
6356 staticpro (&tip_frame);
6358 last_show_tip_args = Qnil;
6359 staticpro (&last_show_tip_args);
6361 defsubr (&Sx_uses_old_gtk_dialog);
6362 #if defined (USE_MOTIF) || defined (USE_GTK)
6363 defsubr (&Sx_file_dialog);
6364 #endif
6366 #if defined (USE_GTK) && defined (HAVE_FREETYPE)
6367 defsubr (&Sx_select_font);
6368 #endif