Make defvar affect the default binding outside of any let.
[emacs.git] / src / xfns.c
blobafae67d6327fa3d94e1bc1a74c8de647d1f728fe
1 /* Functions for the X window system.
3 Copyright (C) 1989, 1992-2013 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 /* This makes the fields of a Display accessible, in Xlib header files. */
27 #define XLIB_ILLEGAL_ACCESS
29 #include "lisp.h"
30 #include "xterm.h"
31 #include "frame.h"
32 #include "window.h"
33 #include "character.h"
34 #include "buffer.h"
35 #include "intervals.h"
36 #include "dispextern.h"
37 #include "keyboard.h"
38 #include "blockinput.h"
39 #include <epaths.h>
40 #include "charset.h"
41 #include "coding.h"
42 #include "fontset.h"
43 #include "systime.h"
44 #include "termhooks.h"
45 #include "atimer.h"
46 #include "termchar.h"
47 #include "font.h"
49 #ifdef HAVE_X_WINDOWS
51 #include <sys/types.h>
52 #include <sys/stat.h>
54 #if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work. */
55 #include "bitmaps/gray.xbm"
56 #else
57 #include <X11/bitmaps/gray>
58 #endif
60 #include "xsettings.h"
62 #ifdef HAVE_XRANDR
63 #include <X11/extensions/Xrandr.h>
64 #endif
65 #ifdef HAVE_XINERAMA
66 #include <X11/extensions/Xinerama.h>
67 #endif
69 #ifdef USE_GTK
70 #include "gtkutil.h"
71 #endif
73 #ifdef USE_X_TOOLKIT
74 #include <X11/Shell.h>
76 #ifndef USE_MOTIF
77 #ifdef HAVE_XAW3D
78 #include <X11/Xaw3d/Paned.h>
79 #include <X11/Xaw3d/Label.h>
80 #else /* !HAVE_XAW3D */
81 #include <X11/Xaw/Paned.h>
82 #include <X11/Xaw/Label.h>
83 #endif /* HAVE_XAW3D */
84 #endif /* USE_MOTIF */
86 #ifdef USG
87 #undef USG /* ####KLUDGE for Solaris 2.2 and up */
88 #include <X11/Xos.h>
89 #define USG
90 #ifdef USG /* Pacify gcc -Wunused-macros. */
91 #endif
92 #else
93 #include <X11/Xos.h>
94 #endif
96 #include "widget.h"
98 #include "../lwlib/lwlib.h"
100 #ifdef USE_MOTIF
101 #include <Xm/Xm.h>
102 #include <Xm/DialogS.h>
103 #include <Xm/FileSB.h>
104 #include <Xm/List.h>
105 #include <Xm/TextF.h>
106 #endif
108 #ifdef USE_LUCID
109 #include "../lwlib/xlwmenu.h"
110 #endif
112 #if !defined (NO_EDITRES)
113 #define HACK_EDITRES
114 extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
115 #endif /* not defined NO_EDITRES */
117 /* Unique id counter for widgets created by the Lucid Widget Library. */
119 extern LWLIB_ID widget_id_tick;
121 #ifdef USE_MOTIF
123 #endif /* USE_MOTIF */
125 #endif /* USE_X_TOOLKIT */
127 #ifdef USE_GTK
129 #endif /* USE_GTK */
131 #define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
133 static Lisp_Object Qsuppress_icon;
134 static Lisp_Object Qundefined_color;
135 static Lisp_Object Qcompound_text, Qcancel_timer;
136 Lisp_Object Qfont_param;
138 #ifdef GLYPH_DEBUG
139 static ptrdiff_t image_cache_refcount;
140 static int dpyinfo_refcount;
141 #endif
143 static struct x_display_info *x_display_info_for_name (Lisp_Object);
145 /* Let the user specify an X display with a Lisp object.
146 OBJECT may be nil, a frame or a terminal object.
147 nil stands for the selected frame--or, if that is not an X frame,
148 the first X display on the list. */
150 struct x_display_info *
151 check_x_display_info (Lisp_Object object)
153 struct x_display_info *dpyinfo = NULL;
155 if (NILP (object))
157 struct frame *sf = XFRAME (selected_frame);
159 if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
160 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
161 else if (x_display_list != 0)
162 dpyinfo = x_display_list;
163 else
164 error ("X windows are not in use or not initialized");
166 else if (TERMINALP (object))
168 struct terminal *t = get_terminal (object, 1);
170 if (t->type != output_x_window)
171 error ("Terminal %d is not an X display", t->id);
173 dpyinfo = t->display_info.x;
175 else if (STRINGP (object))
176 dpyinfo = x_display_info_for_name (object);
177 else
179 FRAME_PTR f = decode_window_system_frame (object);
180 dpyinfo = FRAME_X_DISPLAY_INFO (f);
183 return dpyinfo;
187 /* Return the Emacs frame-object corresponding to an X window.
188 It could be the frame's main window or an icon window. */
190 /* This function can be called during GC, so use GC_xxx type test macros. */
192 struct frame *
193 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
195 Lisp_Object tail, frame;
196 struct frame *f;
198 if (wdesc == None)
199 return NULL;
201 FOR_EACH_FRAME (tail, frame)
203 f = XFRAME (frame);
204 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
205 continue;
206 if (f->output_data.x->hourglass_window == wdesc)
207 return f;
208 #ifdef USE_X_TOOLKIT
209 if ((f->output_data.x->edit_widget
210 && XtWindow (f->output_data.x->edit_widget) == wdesc)
211 /* A tooltip frame? */
212 || (!f->output_data.x->edit_widget
213 && FRAME_X_WINDOW (f) == wdesc)
214 || f->output_data.x->icon_desc == wdesc)
215 return f;
216 #else /* not USE_X_TOOLKIT */
217 #ifdef USE_GTK
218 if (f->output_data.x->edit_widget)
220 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
221 struct x_output *x = f->output_data.x;
222 if (gwdesc != 0 && gwdesc == x->edit_widget)
223 return f;
225 #endif /* USE_GTK */
226 if (FRAME_X_WINDOW (f) == wdesc
227 || f->output_data.x->icon_desc == wdesc)
228 return f;
229 #endif /* not USE_X_TOOLKIT */
231 return 0;
234 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
235 /* Like x_window_to_frame but also compares the window with the widget's
236 windows. */
238 struct frame *
239 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
241 Lisp_Object tail, frame;
242 struct frame *f, *found = NULL;
243 struct x_output *x;
245 if (wdesc == None)
246 return NULL;
248 FOR_EACH_FRAME (tail, frame)
250 if (found)
251 break;
252 f = XFRAME (frame);
253 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
255 /* This frame matches if the window is any of its widgets. */
256 x = f->output_data.x;
257 if (x->hourglass_window == wdesc)
258 found = f;
259 else if (x->widget)
261 #ifdef USE_GTK
262 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
263 if (gwdesc != 0
264 && gtk_widget_get_toplevel (gwdesc) == x->widget)
265 found = f;
266 #else
267 if (wdesc == XtWindow (x->widget)
268 || wdesc == XtWindow (x->column_widget)
269 || wdesc == XtWindow (x->edit_widget))
270 found = f;
271 /* Match if the window is this frame's menubar. */
272 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
273 found = f;
274 #endif
276 else if (FRAME_X_WINDOW (f) == wdesc)
277 /* A tooltip frame. */
278 found = f;
282 return found;
285 /* Likewise, but consider only the menu bar widget. */
287 struct frame *
288 x_menubar_window_to_frame (struct x_display_info *dpyinfo, XEvent *event)
290 Window wdesc = event->xany.window;
291 Lisp_Object tail, frame;
292 struct frame *f;
293 struct x_output *x;
295 if (wdesc == None)
296 return NULL;
298 FOR_EACH_FRAME (tail, frame)
300 f = XFRAME (frame);
301 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
302 continue;
303 x = f->output_data.x;
304 #ifdef USE_GTK
305 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
306 return f;
307 #else
308 /* Match if the window is this frame's menubar. */
309 if (x->menubar_widget
310 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
311 return f;
312 #endif
314 return 0;
317 /* Return the frame whose principal (outermost) window is WDESC.
318 If WDESC is some other (smaller) window, we return 0. */
320 struct frame *
321 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
323 Lisp_Object tail, frame;
324 struct frame *f;
325 struct x_output *x;
327 if (wdesc == None)
328 return NULL;
330 FOR_EACH_FRAME (tail, frame)
332 f = XFRAME (frame);
333 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
334 continue;
335 x = f->output_data.x;
337 if (x->widget)
339 /* This frame matches if the window is its topmost widget. */
340 #ifdef USE_GTK
341 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
342 if (gwdesc == x->widget)
343 return f;
344 #else
345 if (wdesc == XtWindow (x->widget))
346 return f;
347 #if 0 /* I don't know why it did this,
348 but it seems logically wrong,
349 and it causes trouble for MapNotify events. */
350 /* Match if the window is this frame's menubar. */
351 if (x->menubar_widget
352 && wdesc == XtWindow (x->menubar_widget))
353 return f;
354 #endif
355 #endif
357 else if (FRAME_X_WINDOW (f) == wdesc)
358 /* Tooltip frame. */
359 return f;
361 return 0;
363 #endif /* USE_X_TOOLKIT || USE_GTK */
367 /* Store the screen positions of frame F into XPTR and YPTR.
368 These are the positions of the containing window manager window,
369 not Emacs's own window. */
371 void
372 x_real_positions (FRAME_PTR f, int *xptr, int *yptr)
374 int win_x, win_y, outer_x IF_LINT (= 0), outer_y IF_LINT (= 0);
375 int real_x = 0, real_y = 0;
376 int had_errors = 0;
377 Window win = f->output_data.x->parent_desc;
378 Atom actual_type;
379 unsigned long actual_size, bytes_remaining;
380 int rc, actual_format;
381 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
382 long max_len = 400;
383 Display *dpy = FRAME_X_DISPLAY (f);
384 unsigned char *tmp_data = NULL;
385 Atom target_type = XA_CARDINAL;
387 block_input ();
389 x_catch_errors (dpy);
391 if (win == dpyinfo->root_window)
392 win = FRAME_OUTER_WINDOW (f);
394 /* This loop traverses up the containment tree until we hit the root
395 window. Window managers may intersect many windows between our window
396 and the root window. The window we find just before the root window
397 should be the outer WM window. */
398 for (;;)
400 Window wm_window, rootw;
401 Window *tmp_children;
402 unsigned int tmp_nchildren;
403 int success;
405 success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
406 &wm_window, &tmp_children, &tmp_nchildren);
408 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
410 /* Don't free tmp_children if XQueryTree failed. */
411 if (! success)
412 break;
414 XFree (tmp_children);
416 if (wm_window == rootw || had_errors)
417 break;
419 win = wm_window;
422 if (! had_errors)
424 unsigned int ign;
425 Window child, rootw;
427 /* Get the real coordinates for the WM window upper left corner */
428 XGetGeometry (FRAME_X_DISPLAY (f), win,
429 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
431 /* Translate real coordinates to coordinates relative to our
432 window. For our window, the upper left corner is 0, 0.
433 Since the upper left corner of the WM window is outside
434 our window, win_x and win_y will be negative:
436 ------------------ ---> x
437 | title |
438 | ----------------- v y
439 | | our window
441 XTranslateCoordinates (FRAME_X_DISPLAY (f),
443 /* From-window, to-window. */
444 FRAME_X_DISPLAY_INFO (f)->root_window,
445 FRAME_X_WINDOW (f),
447 /* From-position, to-position. */
448 real_x, real_y, &win_x, &win_y,
450 /* Child of win. */
451 &child);
453 if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
455 outer_x = win_x;
456 outer_y = win_y;
458 else
460 XTranslateCoordinates (FRAME_X_DISPLAY (f),
462 /* From-window, to-window. */
463 FRAME_X_DISPLAY_INFO (f)->root_window,
464 FRAME_OUTER_WINDOW (f),
466 /* From-position, to-position. */
467 real_x, real_y, &outer_x, &outer_y,
469 /* Child of win. */
470 &child);
473 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
477 if (dpyinfo->root_window == f->output_data.x->parent_desc)
479 /* Try _NET_FRAME_EXTENTS if our parent is the root window. */
480 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_frame_extents,
481 0, max_len, False, target_type,
482 &actual_type, &actual_format, &actual_size,
483 &bytes_remaining, &tmp_data);
485 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
486 && actual_size == 4 && actual_format == 32)
488 unsigned int ign;
489 Window rootw;
490 long *fe = (long *)tmp_data;
492 XGetGeometry (FRAME_X_DISPLAY (f), win,
493 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
494 outer_x = -fe[0];
495 outer_y = -fe[2];
496 real_x -= fe[0];
497 real_y -= fe[2];
501 if (tmp_data) XFree (tmp_data);
503 x_uncatch_errors ();
505 unblock_input ();
507 if (had_errors) return;
509 f->x_pixels_diff = -win_x;
510 f->y_pixels_diff = -win_y;
512 FRAME_X_OUTPUT (f)->x_pixels_outer_diff = -outer_x;
513 FRAME_X_OUTPUT (f)->y_pixels_outer_diff = -outer_y;
515 *xptr = real_x;
516 *yptr = real_y;
522 /* Gamma-correct COLOR on frame F. */
524 void
525 gamma_correct (struct frame *f, XColor *color)
527 if (f->gamma)
529 color->red = pow (color->red / 65535.0, f->gamma) * 65535.0 + 0.5;
530 color->green = pow (color->green / 65535.0, f->gamma) * 65535.0 + 0.5;
531 color->blue = pow (color->blue / 65535.0, f->gamma) * 65535.0 + 0.5;
536 /* Decide if color named COLOR_NAME is valid for use on frame F. If
537 so, return the RGB values in COLOR. If ALLOC_P,
538 allocate the color. Value is false if COLOR_NAME is invalid, or
539 no color could be allocated. */
541 bool
542 x_defined_color (struct frame *f, const char *color_name,
543 XColor *color, bool alloc_p)
545 bool success_p = 0;
546 Display *dpy = FRAME_X_DISPLAY (f);
547 Colormap cmap = FRAME_X_COLORMAP (f);
549 block_input ();
550 #ifdef USE_GTK
551 success_p = xg_check_special_colors (f, color_name, color);
552 #endif
553 if (!success_p)
554 success_p = XParseColor (dpy, cmap, color_name, color) != 0;
555 if (success_p && alloc_p)
556 success_p = x_alloc_nearest_color (f, cmap, color);
557 unblock_input ();
559 return success_p;
563 /* Return the pixel color value for color COLOR_NAME on frame F. If F
564 is a monochrome frame, return MONO_COLOR regardless of what ARG says.
565 Signal an error if color can't be allocated. */
567 static int
568 x_decode_color (FRAME_PTR f, Lisp_Object color_name, int mono_color)
570 XColor cdef;
572 CHECK_STRING (color_name);
574 #if 0 /* Don't do this. It's wrong when we're not using the default
575 colormap, it makes freeing difficult, and it's probably not
576 an important optimization. */
577 if (strcmp (SDATA (color_name), "black") == 0)
578 return BLACK_PIX_DEFAULT (f);
579 else if (strcmp (SDATA (color_name), "white") == 0)
580 return WHITE_PIX_DEFAULT (f);
581 #endif
583 /* Return MONO_COLOR for monochrome frames. */
584 if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
585 return mono_color;
587 /* x_defined_color is responsible for coping with failures
588 by looking for a near-miss. */
589 if (x_defined_color (f, SSDATA (color_name), &cdef, 1))
590 return cdef.pixel;
592 signal_error ("Undefined color", color_name);
597 /* Change the `wait-for-wm' frame parameter of frame F. OLD_VALUE is
598 the previous value of that parameter, NEW_VALUE is the new value.
599 See also the comment of wait_for_wm in struct x_output. */
601 static void
602 x_set_wait_for_wm (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
604 f->output_data.x->wait_for_wm = !NILP (new_value);
607 static void
608 x_set_tool_bar_position (struct frame *f,
609 Lisp_Object new_value,
610 Lisp_Object old_value)
612 if (! EQ (new_value, Qleft) && ! EQ (new_value, Qright)
613 && ! EQ (new_value, Qbottom) && ! EQ (new_value, Qtop))
614 return;
615 if (EQ (new_value, old_value)) return;
617 #ifdef USE_GTK
618 xg_change_toolbar_position (f, new_value);
619 fset_tool_bar_position (f, new_value);
620 #endif
623 #ifdef USE_GTK
625 /* Set icon from FILE for frame F. By using GTK functions the icon
626 may be any format that GdkPixbuf knows about, i.e. not just bitmaps. */
629 xg_set_icon (FRAME_PTR f, Lisp_Object file)
631 int result = 0;
632 Lisp_Object found;
634 found = x_find_image_file (file);
636 if (! NILP (found))
638 GdkPixbuf *pixbuf;
639 GError *err = NULL;
640 char *filename = SSDATA (found);
641 block_input ();
643 pixbuf = gdk_pixbuf_new_from_file (filename, &err);
645 if (pixbuf)
647 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
648 pixbuf);
649 g_object_unref (pixbuf);
651 result = 1;
653 else
654 g_error_free (err);
656 unblock_input ();
659 return result;
663 xg_set_icon_from_xpm_data (FRAME_PTR f, const char **data)
665 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data (data);
667 if (!pixbuf)
668 return 0;
670 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
671 g_object_unref (pixbuf);
672 return 1;
674 #endif /* USE_GTK */
677 /* Functions called only from `x_set_frame_param'
678 to set individual parameters.
680 If FRAME_X_WINDOW (f) is 0,
681 the frame is being created and its X-window does not exist yet.
682 In that case, just record the parameter's new value
683 in the standard place; do not attempt to change the window. */
685 static void
686 x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
688 struct x_output *x = f->output_data.x;
689 unsigned long fg, old_fg;
691 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
692 old_fg = FRAME_FOREGROUND_PIXEL (f);
693 FRAME_FOREGROUND_PIXEL (f) = fg;
695 if (FRAME_X_WINDOW (f) != 0)
697 Display *dpy = FRAME_X_DISPLAY (f);
699 block_input ();
700 XSetForeground (dpy, x->normal_gc, fg);
701 XSetBackground (dpy, x->reverse_gc, fg);
703 if (x->cursor_pixel == old_fg)
705 unload_color (f, x->cursor_pixel);
706 x->cursor_pixel = x_copy_color (f, fg);
707 XSetBackground (dpy, x->cursor_gc, x->cursor_pixel);
710 unblock_input ();
712 update_face_from_frame_parameter (f, Qforeground_color, arg);
714 if (FRAME_VISIBLE_P (f))
715 redraw_frame (f);
718 unload_color (f, old_fg);
721 static void
722 x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
724 struct x_output *x = f->output_data.x;
725 unsigned long bg;
727 bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
728 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
729 FRAME_BACKGROUND_PIXEL (f) = bg;
731 if (FRAME_X_WINDOW (f) != 0)
733 Display *dpy = FRAME_X_DISPLAY (f);
735 block_input ();
736 XSetBackground (dpy, x->normal_gc, bg);
737 XSetForeground (dpy, x->reverse_gc, bg);
738 XSetWindowBackground (dpy, FRAME_X_WINDOW (f), bg);
739 XSetForeground (dpy, x->cursor_gc, bg);
741 #ifdef USE_GTK
742 xg_set_background_color (f, bg);
743 #endif
745 #ifndef USE_TOOLKIT_SCROLL_BARS /* Turns out to be annoying with
746 toolkit scroll bars. */
748 Lisp_Object bar;
749 for (bar = FRAME_SCROLL_BARS (f);
750 !NILP (bar);
751 bar = XSCROLL_BAR (bar)->next)
753 Window window = XSCROLL_BAR (bar)->x_window;
754 XSetWindowBackground (dpy, window, bg);
757 #endif /* USE_TOOLKIT_SCROLL_BARS */
759 unblock_input ();
760 update_face_from_frame_parameter (f, Qbackground_color, arg);
762 if (FRAME_VISIBLE_P (f))
763 redraw_frame (f);
767 static Cursor
768 make_invisible_cursor (struct frame *f)
770 Display *dpy = FRAME_X_DISPLAY (f);
771 static char const no_data[] = { 0 };
772 Pixmap pix;
773 XColor col;
774 Cursor c = 0;
776 x_catch_errors (dpy);
777 pix = XCreateBitmapFromData (dpy, FRAME_X_DISPLAY_INFO (f)->root_window,
778 no_data, 1, 1);
779 if (! x_had_errors_p (dpy) && pix != None)
781 Cursor pixc;
782 col.pixel = 0;
783 col.red = col.green = col.blue = 0;
784 col.flags = DoRed | DoGreen | DoBlue;
785 pixc = XCreatePixmapCursor (dpy, pix, pix, &col, &col, 0, 0);
786 if (! x_had_errors_p (dpy) && pixc != None)
787 c = pixc;
788 XFreePixmap (dpy, pix);
791 x_uncatch_errors ();
793 return c;
796 static void
797 x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
799 struct x_output *x = f->output_data.x;
800 Display *dpy = FRAME_X_DISPLAY (f);
801 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
802 Cursor hourglass_cursor, horizontal_drag_cursor;
803 unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
804 unsigned long mask_color = FRAME_BACKGROUND_PIXEL (f);
806 /* Don't let pointers be invisible. */
807 if (mask_color == pixel)
809 x_free_colors (f, &pixel, 1);
810 pixel = x_copy_color (f, FRAME_FOREGROUND_PIXEL (f));
813 unload_color (f, x->mouse_pixel);
814 x->mouse_pixel = pixel;
816 block_input ();
818 /* It's not okay to crash if the user selects a screwy cursor. */
819 x_catch_errors (dpy);
821 if (!NILP (Vx_pointer_shape))
823 CHECK_NUMBER (Vx_pointer_shape);
824 cursor = XCreateFontCursor (dpy, XINT (Vx_pointer_shape));
826 else
827 cursor = XCreateFontCursor (dpy, XC_xterm);
828 x_check_errors (dpy, "bad text pointer cursor: %s");
830 if (!NILP (Vx_nontext_pointer_shape))
832 CHECK_NUMBER (Vx_nontext_pointer_shape);
833 nontext_cursor
834 = XCreateFontCursor (dpy, XINT (Vx_nontext_pointer_shape));
836 else
837 nontext_cursor = XCreateFontCursor (dpy, XC_left_ptr);
838 x_check_errors (dpy, "bad nontext pointer cursor: %s");
840 if (!NILP (Vx_hourglass_pointer_shape))
842 CHECK_NUMBER (Vx_hourglass_pointer_shape);
843 hourglass_cursor
844 = XCreateFontCursor (dpy, XINT (Vx_hourglass_pointer_shape));
846 else
847 hourglass_cursor = XCreateFontCursor (dpy, XC_watch);
848 x_check_errors (dpy, "bad hourglass pointer cursor: %s");
850 if (!NILP (Vx_mode_pointer_shape))
852 CHECK_NUMBER (Vx_mode_pointer_shape);
853 mode_cursor = XCreateFontCursor (dpy, XINT (Vx_mode_pointer_shape));
855 else
856 mode_cursor = XCreateFontCursor (dpy, XC_xterm);
857 x_check_errors (dpy, "bad modeline pointer cursor: %s");
859 if (!NILP (Vx_sensitive_text_pointer_shape))
861 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
862 hand_cursor
863 = XCreateFontCursor (dpy, XINT (Vx_sensitive_text_pointer_shape));
865 else
866 hand_cursor = XCreateFontCursor (dpy, XC_hand2);
868 if (!NILP (Vx_window_horizontal_drag_shape))
870 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
871 horizontal_drag_cursor
872 = XCreateFontCursor (dpy, XINT (Vx_window_horizontal_drag_shape));
874 else
875 horizontal_drag_cursor
876 = XCreateFontCursor (dpy, XC_sb_h_double_arrow);
878 /* Check and report errors with the above calls. */
879 x_check_errors (dpy, "can't set cursor shape: %s");
880 x_uncatch_errors ();
883 XColor fore_color, back_color;
885 fore_color.pixel = x->mouse_pixel;
886 x_query_color (f, &fore_color);
887 back_color.pixel = mask_color;
888 x_query_color (f, &back_color);
890 XRecolorCursor (dpy, cursor, &fore_color, &back_color);
891 XRecolorCursor (dpy, nontext_cursor, &fore_color, &back_color);
892 XRecolorCursor (dpy, mode_cursor, &fore_color, &back_color);
893 XRecolorCursor (dpy, hand_cursor, &fore_color, &back_color);
894 XRecolorCursor (dpy, hourglass_cursor, &fore_color, &back_color);
895 XRecolorCursor (dpy, horizontal_drag_cursor, &fore_color, &back_color);
898 if (FRAME_X_WINDOW (f) != 0)
899 XDefineCursor (dpy, FRAME_X_WINDOW (f),
900 f->output_data.x->current_cursor = cursor);
902 if (FRAME_X_DISPLAY_INFO (f)->invisible_cursor == 0)
903 FRAME_X_DISPLAY_INFO (f)->invisible_cursor = make_invisible_cursor (f);
905 if (cursor != x->text_cursor
906 && x->text_cursor != 0)
907 XFreeCursor (dpy, x->text_cursor);
908 x->text_cursor = cursor;
910 if (nontext_cursor != x->nontext_cursor
911 && x->nontext_cursor != 0)
912 XFreeCursor (dpy, x->nontext_cursor);
913 x->nontext_cursor = nontext_cursor;
915 if (hourglass_cursor != x->hourglass_cursor
916 && x->hourglass_cursor != 0)
917 XFreeCursor (dpy, x->hourglass_cursor);
918 x->hourglass_cursor = hourglass_cursor;
920 if (mode_cursor != x->modeline_cursor
921 && x->modeline_cursor != 0)
922 XFreeCursor (dpy, f->output_data.x->modeline_cursor);
923 x->modeline_cursor = mode_cursor;
925 if (hand_cursor != x->hand_cursor
926 && x->hand_cursor != 0)
927 XFreeCursor (dpy, x->hand_cursor);
928 x->hand_cursor = hand_cursor;
930 if (horizontal_drag_cursor != x->horizontal_drag_cursor
931 && x->horizontal_drag_cursor != 0)
932 XFreeCursor (dpy, x->horizontal_drag_cursor);
933 x->horizontal_drag_cursor = horizontal_drag_cursor;
935 XFlush (dpy);
936 unblock_input ();
938 update_face_from_frame_parameter (f, Qmouse_color, arg);
941 static void
942 x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
944 unsigned long fore_pixel, pixel;
945 bool fore_pixel_allocated_p = 0, pixel_allocated_p = 0;
946 struct x_output *x = f->output_data.x;
948 if (!NILP (Vx_cursor_fore_pixel))
950 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
951 WHITE_PIX_DEFAULT (f));
952 fore_pixel_allocated_p = 1;
954 else
955 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
957 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
958 pixel_allocated_p = 1;
960 /* Make sure that the cursor color differs from the background color. */
961 if (pixel == FRAME_BACKGROUND_PIXEL (f))
963 if (pixel_allocated_p)
965 x_free_colors (f, &pixel, 1);
966 pixel_allocated_p = 0;
969 pixel = x->mouse_pixel;
970 if (pixel == fore_pixel)
972 if (fore_pixel_allocated_p)
974 x_free_colors (f, &fore_pixel, 1);
975 fore_pixel_allocated_p = 0;
977 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
981 unload_color (f, x->cursor_foreground_pixel);
982 if (!fore_pixel_allocated_p)
983 fore_pixel = x_copy_color (f, fore_pixel);
984 x->cursor_foreground_pixel = fore_pixel;
986 unload_color (f, x->cursor_pixel);
987 if (!pixel_allocated_p)
988 pixel = x_copy_color (f, pixel);
989 x->cursor_pixel = pixel;
991 if (FRAME_X_WINDOW (f) != 0)
993 block_input ();
994 XSetBackground (FRAME_X_DISPLAY (f), x->cursor_gc, x->cursor_pixel);
995 XSetForeground (FRAME_X_DISPLAY (f), x->cursor_gc, fore_pixel);
996 unblock_input ();
998 if (FRAME_VISIBLE_P (f))
1000 x_update_cursor (f, 0);
1001 x_update_cursor (f, 1);
1005 update_face_from_frame_parameter (f, Qcursor_color, arg);
1008 /* Set the border-color of frame F to pixel value PIX.
1009 Note that this does not fully take effect if done before
1010 F has an x-window. */
1012 static void
1013 x_set_border_pixel (struct frame *f, int pix)
1015 unload_color (f, f->output_data.x->border_pixel);
1016 f->output_data.x->border_pixel = pix;
1018 if (FRAME_X_WINDOW (f) != 0 && f->border_width > 0)
1020 block_input ();
1021 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), pix);
1022 unblock_input ();
1024 if (FRAME_VISIBLE_P (f))
1025 redraw_frame (f);
1029 /* Set the border-color of frame F to value described by ARG.
1030 ARG can be a string naming a color.
1031 The border-color is used for the border that is drawn by the X server.
1032 Note that this does not fully take effect if done before
1033 F has an x-window; it must be redone when the window is created.
1035 Note: this is done in two routines because of the way X10 works.
1037 Note: under X11, this is normally the province of the window manager,
1038 and so emacs' border colors may be overridden. */
1040 static void
1041 x_set_border_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1043 int pix;
1045 CHECK_STRING (arg);
1046 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1047 x_set_border_pixel (f, pix);
1048 update_face_from_frame_parameter (f, Qborder_color, arg);
1052 static void
1053 x_set_cursor_type (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1055 set_frame_cursor_types (f, arg);
1057 /* Make sure the cursor gets redrawn. */
1058 cursor_type_changed = 1;
1061 static void
1062 x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1064 int result;
1066 if (STRINGP (arg))
1068 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1069 return;
1071 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
1072 return;
1074 block_input ();
1075 if (NILP (arg))
1076 result = x_text_icon (f,
1077 SSDATA ((!NILP (f->icon_name)
1078 ? f->icon_name
1079 : f->name)));
1080 else
1081 result = x_bitmap_icon (f, arg);
1083 if (result)
1085 unblock_input ();
1086 error ("No icon window available");
1089 XFlush (FRAME_X_DISPLAY (f));
1090 unblock_input ();
1093 static void
1094 x_set_icon_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1096 int result;
1098 if (STRINGP (arg))
1100 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1101 return;
1103 else if (!NILP (arg) || NILP (oldval))
1104 return;
1106 fset_icon_name (f, arg);
1108 if (f->output_data.x->icon_bitmap != 0)
1109 return;
1111 block_input ();
1113 result = x_text_icon (f,
1114 SSDATA ((!NILP (f->icon_name)
1115 ? f->icon_name
1116 : !NILP (f->title)
1117 ? f->title
1118 : f->name)));
1120 if (result)
1122 unblock_input ();
1123 error ("No icon window available");
1126 XFlush (FRAME_X_DISPLAY (f));
1127 unblock_input ();
1131 void
1132 x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1134 int nlines;
1135 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
1136 int olines = FRAME_MENU_BAR_LINES (f);
1137 #endif
1139 /* Right now, menu bars don't work properly in minibuf-only frames;
1140 most of the commands try to apply themselves to the minibuffer
1141 frame itself, and get an error because you can't switch buffers
1142 in or split the minibuffer window. */
1143 if (FRAME_MINIBUF_ONLY_P (f))
1144 return;
1146 if (TYPE_RANGED_INTEGERP (int, value))
1147 nlines = XINT (value);
1148 else
1149 nlines = 0;
1151 /* Make sure we redisplay all windows in this frame. */
1152 windows_or_buffers_changed++;
1154 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1155 FRAME_MENU_BAR_LINES (f) = 0;
1156 if (nlines)
1158 FRAME_EXTERNAL_MENU_BAR (f) = 1;
1159 if (FRAME_X_P (f) && f->output_data.x->menubar_widget == 0)
1160 /* Make sure next redisplay shows the menu bar. */
1161 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = 1;
1163 else
1165 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1166 free_frame_menubar (f);
1167 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1168 if (FRAME_X_P (f))
1169 f->output_data.x->menubar_widget = 0;
1171 #else /* not USE_X_TOOLKIT && not USE_GTK */
1172 FRAME_MENU_BAR_LINES (f) = nlines;
1173 resize_frame_windows (f, FRAME_LINES (f), 0);
1175 /* If the menu bar height gets changed, the internal border below
1176 the top margin has to be cleared. Also, if the menu bar gets
1177 larger, the area for the added lines has to be cleared except for
1178 the first menu bar line that is to be drawn later. */
1179 if (nlines != olines)
1181 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1182 int width = FRAME_PIXEL_WIDTH (f);
1183 int y;
1185 /* height can be zero here. */
1186 if (height > 0 && width > 0)
1188 y = FRAME_TOP_MARGIN_HEIGHT (f);
1190 block_input ();
1191 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1192 0, y, width, height, False);
1193 unblock_input ();
1196 if (nlines > 1 && nlines > olines)
1198 y = (olines == 0 ? 1 : olines) * FRAME_LINE_HEIGHT (f);
1199 height = nlines * FRAME_LINE_HEIGHT (f) - y;
1201 block_input ();
1202 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1203 0, y, width, height, False);
1204 unblock_input ();
1207 if (nlines == 0 && WINDOWP (f->menu_bar_window))
1208 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
1210 #endif /* not USE_X_TOOLKIT && not USE_GTK */
1211 adjust_glyphs (f);
1212 run_window_configuration_change_hook (f);
1216 /* Set the number of lines used for the tool bar of frame F to VALUE.
1217 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1218 is the old number of tool bar lines. This function changes the
1219 height of all windows on frame F to match the new tool bar height.
1220 The frame's height doesn't change. */
1222 void
1223 x_set_tool_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1225 int delta, nlines, root_height;
1226 Lisp_Object root_window;
1228 /* Treat tool bars like menu bars. */
1229 if (FRAME_MINIBUF_ONLY_P (f))
1230 return;
1232 /* Use VALUE only if an int >= 0. */
1233 if (RANGED_INTEGERP (0, value, INT_MAX))
1234 nlines = XFASTINT (value);
1235 else
1236 nlines = 0;
1238 #ifdef USE_GTK
1239 FRAME_TOOL_BAR_LINES (f) = 0;
1240 if (nlines)
1242 FRAME_EXTERNAL_TOOL_BAR (f) = 1;
1243 if (FRAME_X_P (f) && f->output_data.x->toolbar_widget == 0)
1244 /* Make sure next redisplay shows the tool bar. */
1245 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = 1;
1246 update_frame_tool_bar (f);
1248 else
1250 if (FRAME_EXTERNAL_TOOL_BAR (f))
1251 free_frame_tool_bar (f);
1252 FRAME_EXTERNAL_TOOL_BAR (f) = 0;
1255 return;
1256 #endif
1258 /* Make sure we redisplay all windows in this frame. */
1259 ++windows_or_buffers_changed;
1261 delta = nlines - FRAME_TOOL_BAR_LINES (f);
1263 /* Don't resize the tool-bar to more than we have room for. */
1264 root_window = FRAME_ROOT_WINDOW (f);
1265 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
1266 if (root_height - delta < 1)
1268 delta = root_height - 1;
1269 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
1272 FRAME_TOOL_BAR_LINES (f) = nlines;
1273 resize_frame_windows (f, FRAME_LINES (f), 0);
1274 adjust_glyphs (f);
1276 /* We also have to make sure that the internal border at the top of
1277 the frame, below the menu bar or tool bar, is redrawn when the
1278 tool bar disappears. This is so because the internal border is
1279 below the tool bar if one is displayed, but is below the menu bar
1280 if there isn't a tool bar. The tool bar draws into the area
1281 below the menu bar. */
1282 if (FRAME_X_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
1284 clear_frame (f);
1285 clear_current_matrices (f);
1288 /* If the tool bar gets smaller, the internal border below it
1289 has to be cleared. It was formerly part of the display
1290 of the larger tool bar, and updating windows won't clear it. */
1291 if (delta < 0)
1293 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1294 int width = FRAME_PIXEL_WIDTH (f);
1295 int y = (FRAME_MENU_BAR_LINES (f) + nlines) * FRAME_LINE_HEIGHT (f);
1297 /* height can be zero here. */
1298 if (height > 0 && width > 0)
1300 block_input ();
1301 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1302 0, y, width, height, False);
1303 unblock_input ();
1306 if (WINDOWP (f->tool_bar_window))
1307 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1310 run_window_configuration_change_hook (f);
1315 /* Set the foreground color for scroll bars on frame F to VALUE.
1316 VALUE should be a string, a color name. If it isn't a string or
1317 isn't a valid color name, do nothing. OLDVAL is the old value of
1318 the frame parameter. */
1320 static void
1321 x_set_scroll_bar_foreground (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1323 unsigned long pixel;
1325 if (STRINGP (value))
1326 pixel = x_decode_color (f, value, BLACK_PIX_DEFAULT (f));
1327 else
1328 pixel = -1;
1330 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
1331 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
1333 f->output_data.x->scroll_bar_foreground_pixel = pixel;
1334 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1336 /* Remove all scroll bars because they have wrong colors. */
1337 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1338 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1339 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1340 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1342 update_face_from_frame_parameter (f, Qscroll_bar_foreground, value);
1343 redraw_frame (f);
1348 /* Set the background color for scroll bars on frame F to VALUE VALUE
1349 should be a string, a color name. If it isn't a string or isn't a
1350 valid color name, do nothing. OLDVAL is the old value of the frame
1351 parameter. */
1353 static void
1354 x_set_scroll_bar_background (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1356 unsigned long pixel;
1358 if (STRINGP (value))
1359 pixel = x_decode_color (f, value, WHITE_PIX_DEFAULT (f));
1360 else
1361 pixel = -1;
1363 if (f->output_data.x->scroll_bar_background_pixel != -1)
1364 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
1366 #ifdef USE_TOOLKIT_SCROLL_BARS
1367 /* Scrollbar shadow colors. */
1368 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
1370 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
1371 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
1373 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
1375 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
1376 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
1378 #endif /* USE_TOOLKIT_SCROLL_BARS */
1380 f->output_data.x->scroll_bar_background_pixel = pixel;
1381 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1383 /* Remove all scroll bars because they have wrong colors. */
1384 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1385 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1386 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1387 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1389 update_face_from_frame_parameter (f, Qscroll_bar_background, value);
1390 redraw_frame (f);
1395 /* Encode Lisp string STRING as a text in a format appropriate for
1396 XICCC (X Inter Client Communication Conventions).
1398 This can call Lisp code, so callers must GCPRO.
1400 If STRING contains only ASCII characters, do no conversion and
1401 return the string data of STRING. Otherwise, encode the text by
1402 CODING_SYSTEM, and return a newly allocated memory area which
1403 should be freed by `xfree' by a caller.
1405 SELECTIONP non-zero means the string is being encoded for an X
1406 selection, so it is safe to run pre-write conversions (which
1407 may run Lisp code).
1409 Store the byte length of resulting text in *TEXT_BYTES.
1411 If the text contains only ASCII and Latin-1, store 1 in *STRING_P,
1412 which means that the `encoding' of the result can be `STRING'.
1413 Otherwise store 0 in *STRINGP, which means that the `encoding' of
1414 the result should be `COMPOUND_TEXT'. */
1416 static unsigned char *
1417 x_encode_text (Lisp_Object string, Lisp_Object coding_system, int selectionp,
1418 ptrdiff_t *text_bytes, int *stringp, int *freep)
1420 int result = string_xstring_p (string);
1421 struct coding_system coding;
1423 if (result == 0)
1425 /* No multibyte character in OBJ. We need not encode it. */
1426 *text_bytes = SBYTES (string);
1427 *stringp = 1;
1428 *freep = 0;
1429 return SDATA (string);
1432 setup_coding_system (coding_system, &coding);
1433 coding.mode |= (CODING_MODE_SAFE_ENCODING | CODING_MODE_LAST_BLOCK);
1434 /* We suppress producing escape sequences for composition. */
1435 coding.common_flags &= ~CODING_ANNOTATION_MASK;
1436 coding.destination = xnmalloc (SCHARS (string), 2);
1437 coding.dst_bytes = SCHARS (string) * 2;
1438 encode_coding_object (&coding, string, 0, 0,
1439 SCHARS (string), SBYTES (string), Qnil);
1440 *text_bytes = coding.produced;
1441 *stringp = (result == 1 || !EQ (coding_system, Qcompound_text));
1442 *freep = 1;
1443 return coding.destination;
1447 /* Set the WM name to NAME for frame F. Also set the icon name.
1448 If the frame already has an icon name, use that, otherwise set the
1449 icon name to NAME. */
1451 static void
1452 x_set_name_internal (FRAME_PTR f, Lisp_Object name)
1454 if (FRAME_X_WINDOW (f))
1456 block_input ();
1458 XTextProperty text, icon;
1459 ptrdiff_t bytes;
1460 int stringp;
1461 int do_free_icon_value = 0, do_free_text_value = 0;
1462 Lisp_Object coding_system;
1463 Lisp_Object encoded_name;
1464 Lisp_Object encoded_icon_name;
1465 struct gcpro gcpro1;
1467 /* As ENCODE_UTF_8 may cause GC and relocation of string data,
1468 we use it before x_encode_text that may return string data. */
1469 GCPRO1 (name);
1470 encoded_name = ENCODE_UTF_8 (name);
1471 UNGCPRO;
1473 coding_system = Qcompound_text;
1474 /* Note: Encoding strategy
1476 We encode NAME by compound-text and use "COMPOUND-TEXT" in
1477 text.encoding. But, there are non-internationalized window
1478 managers which don't support that encoding. So, if NAME
1479 contains only ASCII and 8859-1 characters, encode it by
1480 iso-latin-1, and use "STRING" in text.encoding hoping that
1481 such window managers at least analyze this format correctly,
1482 i.e. treat 8-bit bytes as 8859-1 characters.
1484 We may also be able to use "UTF8_STRING" in text.encoding
1485 in the future which can encode all Unicode characters.
1486 But, for the moment, there's no way to know that the
1487 current window manager supports it or not.
1489 Either way, we also set the _NET_WM_NAME and _NET_WM_ICON_NAME
1490 properties. Per the EWMH specification, those two properties
1491 are always UTF8_STRING. This matches what gtk_window_set_title()
1492 does in the USE_GTK case. */
1493 text.value = x_encode_text (name, coding_system, 0, &bytes, &stringp,
1494 &do_free_text_value);
1495 text.encoding = (stringp ? XA_STRING
1496 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1497 text.format = 8;
1498 text.nitems = bytes;
1499 if (text.nitems != bytes)
1500 error ("Window name too large");
1502 if (!STRINGP (f->icon_name))
1504 icon = text;
1505 encoded_icon_name = encoded_name;
1507 else
1509 /* See the above comment "Note: Encoding strategy". */
1510 icon.value = x_encode_text (f->icon_name, coding_system, 0,
1511 &bytes, &stringp, &do_free_icon_value);
1512 icon.encoding = (stringp ? XA_STRING
1513 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1514 icon.format = 8;
1515 icon.nitems = bytes;
1516 if (icon.nitems != bytes)
1517 error ("Icon name too large");
1519 encoded_icon_name = ENCODE_UTF_8 (f->icon_name);
1522 #ifdef USE_GTK
1523 gtk_window_set_title (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1524 SSDATA (encoded_name));
1525 #else /* not USE_GTK */
1526 XSetWMName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
1527 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1528 FRAME_X_DISPLAY_INFO (f)->Xatom_net_wm_name,
1529 FRAME_X_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1530 8, PropModeReplace,
1531 SDATA (encoded_name),
1532 SBYTES (encoded_name));
1533 #endif /* not USE_GTK */
1535 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &icon);
1536 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1537 FRAME_X_DISPLAY_INFO (f)->Xatom_net_wm_icon_name,
1538 FRAME_X_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1539 8, PropModeReplace,
1540 SDATA (encoded_icon_name),
1541 SBYTES (encoded_icon_name));
1543 if (do_free_icon_value)
1544 xfree (icon.value);
1545 if (do_free_text_value)
1546 xfree (text.value);
1548 unblock_input ();
1552 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1553 x_id_name.
1555 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1556 name; if NAME is a string, set F's name to NAME and set
1557 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1559 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1560 suggesting a new name, which lisp code should override; if
1561 F->explicit_name is set, ignore the new name; otherwise, set it. */
1563 static void
1564 x_set_name (struct frame *f, Lisp_Object name, int explicit)
1566 /* Make sure that requests from lisp code override requests from
1567 Emacs redisplay code. */
1568 if (explicit)
1570 /* If we're switching from explicit to implicit, we had better
1571 update the mode lines and thereby update the title. */
1572 if (f->explicit_name && NILP (name))
1573 update_mode_lines = 1;
1575 f->explicit_name = ! NILP (name);
1577 else if (f->explicit_name)
1578 return;
1580 /* If NAME is nil, set the name to the x_id_name. */
1581 if (NILP (name))
1583 /* Check for no change needed in this very common case
1584 before we do any consing. */
1585 if (!strcmp (FRAME_X_DISPLAY_INFO (f)->x_id_name,
1586 SSDATA (f->name)))
1587 return;
1588 name = build_string (FRAME_X_DISPLAY_INFO (f)->x_id_name);
1590 else
1591 CHECK_STRING (name);
1593 /* Don't change the name if it's already NAME. */
1594 if (! NILP (Fstring_equal (name, f->name)))
1595 return;
1597 fset_name (f, name);
1599 /* For setting the frame title, the title parameter should override
1600 the name parameter. */
1601 if (! NILP (f->title))
1602 name = f->title;
1604 x_set_name_internal (f, name);
1607 /* This function should be called when the user's lisp code has
1608 specified a name for the frame; the name will override any set by the
1609 redisplay code. */
1610 static void
1611 x_explicitly_set_name (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1613 x_set_name (f, arg, 1);
1616 /* This function should be called by Emacs redisplay code to set the
1617 name; names set this way will never override names set by the user's
1618 lisp code. */
1619 void
1620 x_implicitly_set_name (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1622 x_set_name (f, arg, 0);
1625 /* Change the title of frame F to NAME.
1626 If NAME is nil, use the frame name as the title. */
1628 static void
1629 x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
1631 /* Don't change the title if it's already NAME. */
1632 if (EQ (name, f->title))
1633 return;
1635 update_mode_lines = 1;
1637 fset_title (f, name);
1639 if (NILP (name))
1640 name = f->name;
1641 else
1642 CHECK_STRING (name);
1644 x_set_name_internal (f, name);
1647 void
1648 x_set_scroll_bar_default_width (struct frame *f)
1650 int wid = FRAME_COLUMN_WIDTH (f);
1651 #ifdef USE_TOOLKIT_SCROLL_BARS
1652 #ifdef USE_GTK
1653 int minw = xg_get_default_scrollbar_width ();
1654 #else
1655 int minw = 16;
1656 #endif
1657 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
1658 int width = minw + 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM;
1659 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (width + wid - 1) / wid;
1660 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = width;
1661 #else
1662 /* Make the actual width at least 14 pixels and a multiple of a
1663 character width. */
1664 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
1666 /* Use all of that space (aside from required margins) for the
1667 scroll bar. */
1668 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = 0;
1669 #endif
1673 /* Record in frame F the specified or default value according to ALIST
1674 of the parameter named PROP (a Lisp symbol). If no value is
1675 specified for PROP, look for an X default for XPROP on the frame
1676 named NAME. If that is not found either, use the value DEFLT. */
1678 static Lisp_Object
1679 x_default_scroll_bar_color_parameter (struct frame *f,
1680 Lisp_Object alist, Lisp_Object prop,
1681 const char *xprop, const char *xclass,
1682 int foreground_p)
1684 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1685 Lisp_Object tem;
1687 tem = x_get_arg (dpyinfo, alist, prop, xprop, xclass, RES_TYPE_STRING);
1688 if (EQ (tem, Qunbound))
1690 #ifdef USE_TOOLKIT_SCROLL_BARS
1692 /* See if an X resource for the scroll bar color has been
1693 specified. */
1694 tem = display_x_get_resource (dpyinfo,
1695 build_string (foreground_p
1696 ? "foreground"
1697 : "background"),
1698 empty_unibyte_string,
1699 build_string ("verticalScrollBar"),
1700 empty_unibyte_string);
1701 if (!STRINGP (tem))
1703 /* If nothing has been specified, scroll bars will use a
1704 toolkit-dependent default. Because these defaults are
1705 difficult to get at without actually creating a scroll
1706 bar, use nil to indicate that no color has been
1707 specified. */
1708 tem = Qnil;
1711 #else /* not USE_TOOLKIT_SCROLL_BARS */
1713 tem = Qnil;
1715 #endif /* not USE_TOOLKIT_SCROLL_BARS */
1718 x_set_frame_parameters (f, list1 (Fcons (prop, tem)));
1719 return tem;
1725 #ifdef USE_X_TOOLKIT
1727 /* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
1728 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
1729 already be present because of the toolkit (Motif adds some of them,
1730 for example, but Xt doesn't). */
1732 static void
1733 hack_wm_protocols (FRAME_PTR f, Widget widget)
1735 Display *dpy = XtDisplay (widget);
1736 Window w = XtWindow (widget);
1737 int need_delete = 1;
1738 int need_focus = 1;
1739 int need_save = 1;
1741 block_input ();
1743 Atom type;
1744 unsigned char *catoms;
1745 int format = 0;
1746 unsigned long nitems = 0;
1747 unsigned long bytes_after;
1749 if ((XGetWindowProperty (dpy, w,
1750 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
1751 (long)0, (long)100, False, XA_ATOM,
1752 &type, &format, &nitems, &bytes_after,
1753 &catoms)
1754 == Success)
1755 && format == 32 && type == XA_ATOM)
1757 Atom *atoms = (Atom *) catoms;
1758 while (nitems > 0)
1760 nitems--;
1761 if (atoms[nitems]
1762 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window)
1763 need_delete = 0;
1764 else if (atoms[nitems]
1765 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus)
1766 need_focus = 0;
1767 else if (atoms[nitems]
1768 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself)
1769 need_save = 0;
1772 if (catoms)
1773 XFree (catoms);
1776 Atom props [10];
1777 int count = 0;
1778 if (need_delete)
1779 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
1780 if (need_focus)
1781 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus;
1782 if (need_save)
1783 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
1784 if (count)
1785 XChangeProperty (dpy, w, FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
1786 XA_ATOM, 32, PropModeAppend,
1787 (unsigned char *) props, count);
1789 unblock_input ();
1791 #endif
1795 /* Support routines for XIC (X Input Context). */
1797 #ifdef HAVE_X_I18N
1799 static XFontSet xic_create_xfontset (struct frame *);
1800 static XIMStyle best_xim_style (XIMStyles *, XIMStyles *);
1803 /* Supported XIM styles, ordered by preference. */
1805 static XIMStyle supported_xim_styles[] =
1807 XIMPreeditPosition | XIMStatusArea,
1808 XIMPreeditPosition | XIMStatusNothing,
1809 XIMPreeditPosition | XIMStatusNone,
1810 XIMPreeditNothing | XIMStatusArea,
1811 XIMPreeditNothing | XIMStatusNothing,
1812 XIMPreeditNothing | XIMStatusNone,
1813 XIMPreeditNone | XIMStatusArea,
1814 XIMPreeditNone | XIMStatusNothing,
1815 XIMPreeditNone | XIMStatusNone,
1820 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
1821 /* Create an X fontset on frame F with base font name BASE_FONTNAME. */
1823 static const char xic_default_fontset[] = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
1825 /* Create an Xt fontset spec from the name of a base font.
1826 If `motif' is True use the Motif syntax. */
1827 char *
1828 xic_create_fontsetname (const char *base_fontname, int motif)
1830 const char *sep = motif ? ";" : ",";
1831 char *fontsetname;
1833 /* Make a fontset name from the base font name. */
1834 if (xic_default_fontset == base_fontname)
1836 /* There is no base font name, use the default. */
1837 fontsetname = xmalloc (strlen (base_fontname) + 2);
1838 strcpy (fontsetname, base_fontname);
1840 else
1842 /* Make a fontset name from the base font name.
1843 The font set will be made of the following elements:
1844 - the base font.
1845 - the base font where the charset spec is replaced by -*-*.
1846 - the same but with the family also replaced with -*-*-. */
1847 const char *p = base_fontname;
1848 ptrdiff_t i;
1850 for (i = 0; *p; p++)
1851 if (*p == '-') i++;
1852 if (i != 14)
1854 /* As the font name doesn't conform to XLFD, we can't
1855 modify it to generalize it to allcs and allfamilies.
1856 Use the specified font plus the default. */
1857 fontsetname = xmalloc (strlen (base_fontname)
1858 + strlen (xic_default_fontset) + 3);
1859 strcpy (fontsetname, base_fontname);
1860 strcat (fontsetname, sep);
1861 strcat (fontsetname, xic_default_fontset);
1863 else
1865 ptrdiff_t len;
1866 const char *p1 = NULL, *p2 = NULL, *p3 = NULL;
1867 char *font_allcs = NULL;
1868 char *font_allfamilies = NULL;
1869 char *font_all = NULL;
1870 const char *allcs = "*-*-*-*-*-*-*";
1871 const char *allfamilies = "-*-*-";
1872 const char *all = "*-*-*-*-";
1873 char *base;
1875 for (i = 0, p = base_fontname; i < 8; p++)
1877 if (*p == '-')
1879 i++;
1880 if (i == 3)
1881 p1 = p + 1;
1882 else if (i == 7)
1883 p2 = p + 1;
1884 else if (i == 6)
1885 p3 = p + 1;
1888 /* If base_fontname specifies ADSTYLE, make it a
1889 wildcard. */
1890 if (*p3 != '*')
1892 ptrdiff_t diff = (p2 - p3) - 2;
1894 base = alloca (strlen (base_fontname) + 1);
1895 memcpy (base, base_fontname, p3 - base_fontname);
1896 base[p3 - base_fontname] = '*';
1897 base[(p3 - base_fontname) + 1] = '-';
1898 strcpy (base + (p3 - base_fontname) + 2, p2);
1899 p = base + (p - base_fontname) - diff;
1900 p1 = base + (p1 - base_fontname);
1901 p2 = base + (p2 - base_fontname) - diff;
1902 base_fontname = base;
1905 /* Build the font spec that matches all charsets. */
1906 len = p - base_fontname + strlen (allcs) + 1;
1907 font_allcs = alloca (len);
1908 memcpy (font_allcs, base_fontname, p - base_fontname);
1909 strcat (font_allcs, allcs);
1911 /* Build the font spec that matches all families and
1912 add-styles. */
1913 len = p - p1 + strlen (allcs) + strlen (allfamilies) + 1;
1914 font_allfamilies = alloca (len);
1915 strcpy (font_allfamilies, allfamilies);
1916 memcpy (font_allfamilies + strlen (allfamilies), p1, p - p1);
1917 strcat (font_allfamilies, allcs);
1919 /* Build the font spec that matches all. */
1920 len = p - p2 + strlen (allcs) + strlen (all) + strlen (allfamilies) + 1;
1921 font_all = alloca (len);
1922 strcpy (font_all, allfamilies);
1923 strcat (font_all, all);
1924 memcpy (font_all + strlen (all) + strlen (allfamilies), p2, p - p2);
1925 strcat (font_all, allcs);
1927 /* Build the actual font set name. */
1928 len = strlen (base_fontname) + strlen (font_allcs)
1929 + strlen (font_allfamilies) + strlen (font_all) + 5;
1930 fontsetname = xmalloc (len);
1931 strcpy (fontsetname, base_fontname);
1932 strcat (fontsetname, sep);
1933 strcat (fontsetname, font_allcs);
1934 strcat (fontsetname, sep);
1935 strcat (fontsetname, font_allfamilies);
1936 strcat (fontsetname, sep);
1937 strcat (fontsetname, font_all);
1940 if (motif)
1941 strcat (fontsetname, ":");
1942 return fontsetname;
1944 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
1946 #ifdef DEBUG_XIC_FONTSET
1947 static void
1948 print_fontset_result (XFontSet xfs, char *name, char **missing_list,
1949 int missing_count)
1951 if (xfs)
1952 fprintf (stderr, "XIC Fontset created: %s\n", name);
1953 else
1955 fprintf (stderr, "XIC Fontset failed: %s\n", name);
1956 while (missing_count-- > 0)
1958 fprintf (stderr, " missing: %s\n", *missing_list);
1959 missing_list++;
1964 #endif
1966 static XFontSet
1967 xic_create_xfontset (struct frame *f)
1969 XFontSet xfs = NULL;
1970 struct font *font = FRAME_FONT (f);
1971 int pixel_size = font->pixel_size;
1972 Lisp_Object rest, frame;
1974 /* See if there is another frame already using same fontset. */
1975 FOR_EACH_FRAME (rest, frame)
1977 struct frame *cf = XFRAME (frame);
1979 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
1980 && FRAME_X_DISPLAY_INFO (cf) == FRAME_X_DISPLAY_INFO (f)
1981 && FRAME_FONT (f)
1982 && FRAME_FONT (f)->pixel_size == pixel_size)
1984 xfs = FRAME_XIC_FONTSET (cf);
1985 break;
1989 if (! xfs)
1991 char buf[256];
1992 char **missing_list;
1993 int missing_count;
1994 char *def_string;
1995 const char *xlfd_format = "-*-*-medium-r-normal--%d-*-*-*-*-*";
1997 sprintf (buf, xlfd_format, pixel_size);
1998 missing_list = NULL;
1999 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
2000 &missing_list, &missing_count, &def_string);
2001 #ifdef DEBUG_XIC_FONTSET
2002 print_fontset_result (xfs, buf, missing_list, missing_count);
2003 #endif
2004 if (missing_list)
2005 XFreeStringList (missing_list);
2006 if (! xfs)
2008 /* List of pixel sizes most likely available. Find one that
2009 is closest to pixel_size. */
2010 int sizes[] = {0, 8, 10, 11, 12, 14, 17, 18, 20, 24, 26, 34, 0};
2011 int *smaller, *larger;
2013 for (smaller = sizes; smaller[1]; smaller++)
2014 if (smaller[1] >= pixel_size)
2015 break;
2016 larger = smaller + 1;
2017 if (*larger == pixel_size)
2018 larger++;
2019 while (*smaller || *larger)
2021 int this_size;
2023 if (! *larger)
2024 this_size = *smaller--;
2025 else if (! *smaller)
2026 this_size = *larger++;
2027 else if (pixel_size - *smaller < *larger - pixel_size)
2028 this_size = *smaller--;
2029 else
2030 this_size = *larger++;
2031 sprintf (buf, xlfd_format, this_size);
2032 missing_list = NULL;
2033 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
2034 &missing_list, &missing_count, &def_string);
2035 #ifdef DEBUG_XIC_FONTSET
2036 print_fontset_result (xfs, buf, missing_list, missing_count);
2037 #endif
2038 if (missing_list)
2039 XFreeStringList (missing_list);
2040 if (xfs)
2041 break;
2044 if (! xfs)
2046 const char *last_resort = "-*-*-*-r-normal--*-*-*-*-*-*";
2048 missing_list = NULL;
2049 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), last_resort,
2050 &missing_list, &missing_count, &def_string);
2051 #ifdef DEBUG_XIC_FONTSET
2052 print_fontset_result (xfs, last_resort, missing_list, missing_count);
2053 #endif
2054 if (missing_list)
2055 XFreeStringList (missing_list);
2060 return xfs;
2063 /* Free the X fontset of frame F if it is the last frame using it. */
2065 void
2066 xic_free_xfontset (struct frame *f)
2068 Lisp_Object rest, frame;
2069 bool shared_p = 0;
2071 if (!FRAME_XIC_FONTSET (f))
2072 return;
2074 /* See if there is another frame sharing the same fontset. */
2075 FOR_EACH_FRAME (rest, frame)
2077 struct frame *cf = XFRAME (frame);
2078 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
2079 && FRAME_X_DISPLAY_INFO (cf) == FRAME_X_DISPLAY_INFO (f)
2080 && FRAME_XIC_FONTSET (cf) == FRAME_XIC_FONTSET (f))
2082 shared_p = 1;
2083 break;
2087 if (!shared_p)
2088 /* The fontset is not used anymore. It is safe to free it. */
2089 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
2091 FRAME_XIC_FONTSET (f) = NULL;
2095 /* Value is the best input style, given user preferences USER (already
2096 checked to be supported by Emacs), and styles supported by the
2097 input method XIM. */
2099 static XIMStyle
2100 best_xim_style (XIMStyles *user, XIMStyles *xim)
2102 int i, j;
2104 for (i = 0; i < user->count_styles; ++i)
2105 for (j = 0; j < xim->count_styles; ++j)
2106 if (user->supported_styles[i] == xim->supported_styles[j])
2107 return user->supported_styles[i];
2109 /* Return the default style. */
2110 return XIMPreeditNothing | XIMStatusNothing;
2113 /* Create XIC for frame F. */
2115 static XIMStyle xic_style;
2117 void
2118 create_frame_xic (struct frame *f)
2120 XIM xim;
2121 XIC xic = NULL;
2122 XFontSet xfs = NULL;
2124 if (FRAME_XIC (f))
2125 return;
2127 /* Create X fontset. */
2128 xfs = xic_create_xfontset (f);
2129 xim = FRAME_X_XIM (f);
2130 if (xim)
2132 XRectangle s_area;
2133 XPoint spot;
2134 XVaNestedList preedit_attr;
2135 XVaNestedList status_attr;
2137 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
2138 spot.x = 0; spot.y = 1;
2140 /* Determine XIC style. */
2141 if (xic_style == 0)
2143 XIMStyles supported_list;
2144 supported_list.count_styles = (sizeof supported_xim_styles
2145 / sizeof supported_xim_styles[0]);
2146 supported_list.supported_styles = supported_xim_styles;
2147 xic_style = best_xim_style (&supported_list,
2148 FRAME_X_XIM_STYLES (f));
2151 preedit_attr = XVaCreateNestedList (0,
2152 XNFontSet, xfs,
2153 XNForeground,
2154 FRAME_FOREGROUND_PIXEL (f),
2155 XNBackground,
2156 FRAME_BACKGROUND_PIXEL (f),
2157 (xic_style & XIMPreeditPosition
2158 ? XNSpotLocation
2159 : NULL),
2160 &spot,
2161 NULL);
2162 status_attr = XVaCreateNestedList (0,
2163 XNArea,
2164 &s_area,
2165 XNFontSet,
2166 xfs,
2167 XNForeground,
2168 FRAME_FOREGROUND_PIXEL (f),
2169 XNBackground,
2170 FRAME_BACKGROUND_PIXEL (f),
2171 NULL);
2173 xic = XCreateIC (xim,
2174 XNInputStyle, xic_style,
2175 XNClientWindow, FRAME_X_WINDOW (f),
2176 XNFocusWindow, FRAME_X_WINDOW (f),
2177 XNStatusAttributes, status_attr,
2178 XNPreeditAttributes, preedit_attr,
2179 NULL);
2180 XFree (preedit_attr);
2181 XFree (status_attr);
2184 FRAME_XIC (f) = xic;
2185 FRAME_XIC_STYLE (f) = xic_style;
2186 FRAME_XIC_FONTSET (f) = xfs;
2190 /* Destroy XIC and free XIC fontset of frame F, if any. */
2192 void
2193 free_frame_xic (struct frame *f)
2195 if (FRAME_XIC (f) == NULL)
2196 return;
2198 XDestroyIC (FRAME_XIC (f));
2199 xic_free_xfontset (f);
2201 FRAME_XIC (f) = NULL;
2205 /* Place preedit area for XIC of window W's frame to specified
2206 pixel position X/Y. X and Y are relative to window W. */
2208 void
2209 xic_set_preeditarea (struct window *w, int x, int y)
2211 struct frame *f = XFRAME (w->frame);
2212 XVaNestedList attr;
2213 XPoint spot;
2215 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x) + WINDOW_LEFT_FRINGE_WIDTH (w);
2216 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
2217 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
2218 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2219 XFree (attr);
2223 /* Place status area for XIC in bottom right corner of frame F.. */
2225 void
2226 xic_set_statusarea (struct frame *f)
2228 XIC xic = FRAME_XIC (f);
2229 XVaNestedList attr;
2230 XRectangle area;
2231 XRectangle *needed;
2233 /* Negotiate geometry of status area. If input method has existing
2234 status area, use its current size. */
2235 area.x = area.y = area.width = area.height = 0;
2236 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
2237 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2238 XFree (attr);
2240 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
2241 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2242 XFree (attr);
2244 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
2246 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
2247 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2248 XFree (attr);
2251 area.width = needed->width;
2252 area.height = needed->height;
2253 area.x = FRAME_PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
2254 area.y = (FRAME_PIXEL_HEIGHT (f) - area.height
2255 - FRAME_MENUBAR_HEIGHT (f)
2256 - FRAME_TOOLBAR_TOP_HEIGHT (f)
2257 - FRAME_INTERNAL_BORDER_WIDTH (f));
2258 XFree (needed);
2260 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
2261 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2262 XFree (attr);
2266 /* Set X fontset for XIC of frame F, using base font name
2267 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
2269 void
2270 xic_set_xfontset (struct frame *f, const char *base_fontname)
2272 XVaNestedList attr;
2273 XFontSet xfs;
2275 xic_free_xfontset (f);
2277 xfs = xic_create_xfontset (f);
2279 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
2280 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
2281 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2282 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
2283 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
2284 XFree (attr);
2286 FRAME_XIC_FONTSET (f) = xfs;
2289 #endif /* HAVE_X_I18N */
2293 #ifdef USE_X_TOOLKIT
2295 /* Create and set up the X widget for frame F. */
2297 static void
2298 x_window (struct frame *f, long window_prompting, int minibuffer_only)
2300 XClassHint class_hints;
2301 XSetWindowAttributes attributes;
2302 unsigned long attribute_mask;
2303 Widget shell_widget;
2304 Widget pane_widget;
2305 Widget frame_widget;
2306 Arg al [25];
2307 int ac;
2309 block_input ();
2311 /* Use the resource name as the top-level widget name
2312 for looking up resources. Make a non-Lisp copy
2313 for the window manager, so GC relocation won't bother it.
2315 Elsewhere we specify the window name for the window manager. */
2318 char *str = SSDATA (Vx_resource_name);
2319 f->namebuf = xmalloc (strlen (str) + 1);
2320 strcpy (f->namebuf, str);
2323 ac = 0;
2324 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
2325 XtSetArg (al[ac], XtNinput, 1); ac++;
2326 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2327 XtSetArg (al[ac], XtNborderWidth, f->border_width); ac++;
2328 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2329 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2330 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2331 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
2332 applicationShellWidgetClass,
2333 FRAME_X_DISPLAY (f), al, ac);
2335 f->output_data.x->widget = shell_widget;
2336 /* maybe_set_screen_title_format (shell_widget); */
2338 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
2339 (widget_value *) NULL,
2340 shell_widget, False,
2341 (lw_callback) NULL,
2342 (lw_callback) NULL,
2343 (lw_callback) NULL,
2344 (lw_callback) NULL);
2346 ac = 0;
2347 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2348 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2349 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2350 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2351 XtSetValues (pane_widget, al, ac);
2352 f->output_data.x->column_widget = pane_widget;
2354 /* mappedWhenManaged to false tells to the paned window to not map/unmap
2355 the emacs screen when changing menubar. This reduces flickering. */
2357 ac = 0;
2358 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2359 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
2360 XtSetArg (al[ac], XtNallowResize, 1); ac++;
2361 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
2362 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
2363 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2364 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2365 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2366 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2367 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
2368 al, ac);
2370 f->output_data.x->edit_widget = frame_widget;
2372 XtManageChild (frame_widget);
2374 /* Do some needed geometry management. */
2376 char *tem, shell_position[sizeof "=x++" + 4 * INT_STRLEN_BOUND (int)];
2377 Arg gal[10];
2378 int gac = 0;
2379 int extra_borders = 0;
2380 int menubar_size
2381 = (f->output_data.x->menubar_widget
2382 ? (f->output_data.x->menubar_widget->core.height
2383 + f->output_data.x->menubar_widget->core.border_width)
2384 : 0);
2386 #if 0 /* Experimentally, we now get the right results
2387 for -geometry -0-0 without this. 24 Aug 96, rms. */
2388 if (FRAME_EXTERNAL_MENU_BAR (f))
2390 Dimension ibw = 0;
2391 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
2392 menubar_size += ibw;
2394 #endif
2396 f->output_data.x->menubar_height = menubar_size;
2398 #ifndef USE_LUCID
2399 /* Motif seems to need this amount added to the sizes
2400 specified for the shell widget. The Athena/Lucid widgets don't.
2401 Both conclusions reached experimentally. -- rms. */
2402 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
2403 &extra_borders, NULL);
2404 extra_borders *= 2;
2405 #endif
2407 /* Convert our geometry parameters into a geometry string
2408 and specify it.
2409 Note that we do not specify here whether the position
2410 is a user-specified or program-specified one.
2411 We pass that information later, in x_wm_set_size_hints. */
2413 int left = f->left_pos;
2414 int xneg = window_prompting & XNegative;
2415 int top = f->top_pos;
2416 int yneg = window_prompting & YNegative;
2417 if (xneg)
2418 left = -left;
2419 if (yneg)
2420 top = -top;
2422 if (window_prompting & USPosition)
2423 sprintf (shell_position, "=%dx%d%c%d%c%d",
2424 FRAME_PIXEL_WIDTH (f) + extra_borders,
2425 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders,
2426 (xneg ? '-' : '+'), left,
2427 (yneg ? '-' : '+'), top);
2428 else
2430 sprintf (shell_position, "=%dx%d",
2431 FRAME_PIXEL_WIDTH (f) + extra_borders,
2432 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders);
2434 /* Setting x and y when the position is not specified in
2435 the geometry string will set program position in the WM hints.
2436 If Emacs had just one program position, we could set it in
2437 fallback resources, but since each make-frame call can specify
2438 different program positions, this is easier. */
2439 XtSetArg (gal[gac], XtNx, left); gac++;
2440 XtSetArg (gal[gac], XtNy, top); gac++;
2444 /* We don't free this because we don't know whether
2445 it is safe to free it while the frame exists.
2446 It isn't worth the trouble of arranging to free it
2447 when the frame is deleted. */
2448 tem = xstrdup (shell_position);
2449 XtSetArg (gal[gac], XtNgeometry, tem); gac++;
2450 XtSetValues (shell_widget, gal, gac);
2453 XtManageChild (pane_widget);
2454 XtRealizeWidget (shell_widget);
2456 if (FRAME_X_EMBEDDED_P (f))
2457 XReparentWindow (FRAME_X_DISPLAY (f), XtWindow (shell_widget),
2458 f->output_data.x->parent_desc, 0, 0);
2460 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
2462 validate_x_resource_name ();
2464 class_hints.res_name = SSDATA (Vx_resource_name);
2465 class_hints.res_class = SSDATA (Vx_resource_class);
2466 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
2468 #ifdef HAVE_X_I18N
2469 FRAME_XIC (f) = NULL;
2470 if (use_xim)
2471 create_frame_xic (f);
2472 #endif
2474 f->output_data.x->wm_hints.input = True;
2475 f->output_data.x->wm_hints.flags |= InputHint;
2476 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2477 &f->output_data.x->wm_hints);
2479 hack_wm_protocols (f, shell_widget);
2481 #ifdef HACK_EDITRES
2482 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
2483 #endif
2485 /* Do a stupid property change to force the server to generate a
2486 PropertyNotify event so that the event_stream server timestamp will
2487 be initialized to something relevant to the time we created the window.
2489 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
2490 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
2491 XA_ATOM, 32, PropModeAppend,
2492 (unsigned char*) NULL, 0);
2494 /* Make all the standard events reach the Emacs frame. */
2495 attributes.event_mask = STANDARD_EVENT_SET;
2497 #ifdef HAVE_X_I18N
2498 if (FRAME_XIC (f))
2500 /* XIM server might require some X events. */
2501 unsigned long fevent = NoEventMask;
2502 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2503 attributes.event_mask |= fevent;
2505 #endif /* HAVE_X_I18N */
2507 attribute_mask = CWEventMask;
2508 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
2509 attribute_mask, &attributes);
2511 XtMapWidget (frame_widget);
2513 /* x_set_name normally ignores requests to set the name if the
2514 requested name is the same as the current name. This is the one
2515 place where that assumption isn't correct; f->name is set, but
2516 the X server hasn't been told. */
2518 Lisp_Object name;
2519 int explicit = f->explicit_name;
2521 f->explicit_name = 0;
2522 name = f->name;
2523 fset_name (f, Qnil);
2524 x_set_name (f, name, explicit);
2527 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2528 f->output_data.x->current_cursor
2529 = f->output_data.x->text_cursor);
2531 unblock_input ();
2533 /* This is a no-op, except under Motif. Make sure main areas are
2534 set to something reasonable, in case we get an error later. */
2535 lw_set_main_areas (pane_widget, 0, frame_widget);
2538 #else /* not USE_X_TOOLKIT */
2539 #ifdef USE_GTK
2540 static void
2541 x_window (FRAME_PTR f)
2543 if (! xg_create_frame_widgets (f))
2544 error ("Unable to create window");
2546 #ifdef HAVE_X_I18N
2547 FRAME_XIC (f) = NULL;
2548 if (use_xim)
2550 block_input ();
2551 create_frame_xic (f);
2552 if (FRAME_XIC (f))
2554 /* XIM server might require some X events. */
2555 unsigned long fevent = NoEventMask;
2556 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2558 if (fevent != NoEventMask)
2560 XSetWindowAttributes attributes;
2561 XWindowAttributes wattr;
2562 unsigned long attribute_mask;
2564 XGetWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2565 &wattr);
2566 attributes.event_mask = wattr.your_event_mask | fevent;
2567 attribute_mask = CWEventMask;
2568 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2569 attribute_mask, &attributes);
2572 unblock_input ();
2574 #endif
2577 #else /*! USE_GTK */
2578 /* Create and set up the X window for frame F. */
2580 static void
2581 x_window (struct frame *f)
2583 XClassHint class_hints;
2584 XSetWindowAttributes attributes;
2585 unsigned long attribute_mask;
2587 attributes.background_pixel = FRAME_BACKGROUND_PIXEL (f);
2588 attributes.border_pixel = f->output_data.x->border_pixel;
2589 attributes.bit_gravity = StaticGravity;
2590 attributes.backing_store = NotUseful;
2591 attributes.save_under = True;
2592 attributes.event_mask = STANDARD_EVENT_SET;
2593 attributes.colormap = FRAME_X_COLORMAP (f);
2594 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
2595 | CWColormap);
2597 block_input ();
2598 FRAME_X_WINDOW (f)
2599 = XCreateWindow (FRAME_X_DISPLAY (f),
2600 f->output_data.x->parent_desc,
2601 f->left_pos,
2602 f->top_pos,
2603 FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f),
2604 f->border_width,
2605 CopyFromParent, /* depth */
2606 InputOutput, /* class */
2607 FRAME_X_VISUAL (f),
2608 attribute_mask, &attributes);
2610 #ifdef HAVE_X_I18N
2611 if (use_xim)
2613 create_frame_xic (f);
2614 if (FRAME_XIC (f))
2616 /* XIM server might require some X events. */
2617 unsigned long fevent = NoEventMask;
2618 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2619 attributes.event_mask |= fevent;
2620 attribute_mask = CWEventMask;
2621 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2622 attribute_mask, &attributes);
2625 #endif /* HAVE_X_I18N */
2627 validate_x_resource_name ();
2629 class_hints.res_name = SSDATA (Vx_resource_name);
2630 class_hints.res_class = SSDATA (Vx_resource_class);
2631 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
2633 /* The menubar is part of the ordinary display;
2634 it does not count in addition to the height of the window. */
2635 f->output_data.x->menubar_height = 0;
2637 /* This indicates that we use the "Passive Input" input model.
2638 Unless we do this, we don't get the Focus{In,Out} events that we
2639 need to draw the cursor correctly. Accursed bureaucrats.
2640 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
2642 f->output_data.x->wm_hints.input = True;
2643 f->output_data.x->wm_hints.flags |= InputHint;
2644 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2645 &f->output_data.x->wm_hints);
2646 f->output_data.x->wm_hints.icon_pixmap = None;
2648 /* Request "save yourself" and "delete window" commands from wm. */
2650 Atom protocols[2];
2651 protocols[0] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
2652 protocols[1] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
2653 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
2656 /* x_set_name normally ignores requests to set the name if the
2657 requested name is the same as the current name. This is the one
2658 place where that assumption isn't correct; f->name is set, but
2659 the X server hasn't been told. */
2661 Lisp_Object name;
2662 int explicit = f->explicit_name;
2664 f->explicit_name = 0;
2665 name = f->name;
2666 fset_name (f, Qnil);
2667 x_set_name (f, name, explicit);
2670 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2671 f->output_data.x->current_cursor
2672 = f->output_data.x->text_cursor);
2674 unblock_input ();
2676 if (FRAME_X_WINDOW (f) == 0)
2677 error ("Unable to create window");
2680 #endif /* not USE_GTK */
2681 #endif /* not USE_X_TOOLKIT */
2683 /* Verify that the icon position args for this window are valid. */
2685 static void
2686 x_icon_verify (struct frame *f, Lisp_Object parms)
2688 Lisp_Object icon_x, icon_y;
2690 /* Set the position of the icon. Note that twm groups all
2691 icons in an icon window. */
2692 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2693 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2694 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2696 CHECK_NUMBER (icon_x);
2697 CHECK_NUMBER (icon_y);
2699 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2700 error ("Both left and top icon corners of icon must be specified");
2703 /* Handle the icon stuff for this window. Perhaps later we might
2704 want an x_set_icon_position which can be called interactively as
2705 well. */
2707 static void
2708 x_icon (struct frame *f, Lisp_Object parms)
2710 Lisp_Object icon_x, icon_y;
2711 #if 0
2712 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2713 #endif
2715 /* Set the position of the icon. Note that twm groups all
2716 icons in an icon window. */
2717 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2718 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2719 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2721 CHECK_TYPE_RANGED_INTEGER (int, icon_x);
2722 CHECK_TYPE_RANGED_INTEGER (int, icon_y);
2724 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2725 error ("Both left and top icon corners of icon must be specified");
2727 block_input ();
2729 if (! EQ (icon_x, Qunbound))
2730 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
2732 #if 0 /* x_get_arg removes the visibility parameter as a side effect,
2733 but x_create_frame still needs it. */
2734 /* Start up iconic or window? */
2735 x_wm_set_window_state
2736 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
2737 Qicon)
2738 ? IconicState
2739 : NormalState));
2740 #endif
2742 x_text_icon (f, SSDATA ((!NILP (f->icon_name)
2743 ? f->icon_name
2744 : f->name)));
2746 unblock_input ();
2749 /* Make the GCs needed for this window, setting the
2750 background, border and mouse colors; also create the
2751 mouse cursor and the gray border tile. */
2753 static void
2754 x_make_gc (struct frame *f)
2756 XGCValues gc_values;
2758 block_input ();
2760 /* Create the GCs of this frame.
2761 Note that many default values are used. */
2763 gc_values.foreground = FRAME_FOREGROUND_PIXEL (f);
2764 gc_values.background = FRAME_BACKGROUND_PIXEL (f);
2765 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
2766 f->output_data.x->normal_gc
2767 = XCreateGC (FRAME_X_DISPLAY (f),
2768 FRAME_X_WINDOW (f),
2769 GCLineWidth | GCForeground | GCBackground,
2770 &gc_values);
2772 /* Reverse video style. */
2773 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2774 gc_values.background = FRAME_FOREGROUND_PIXEL (f);
2775 f->output_data.x->reverse_gc
2776 = XCreateGC (FRAME_X_DISPLAY (f),
2777 FRAME_X_WINDOW (f),
2778 GCForeground | GCBackground | GCLineWidth,
2779 &gc_values);
2781 /* Cursor has cursor-color background, background-color foreground. */
2782 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2783 gc_values.background = f->output_data.x->cursor_pixel;
2784 gc_values.fill_style = FillOpaqueStippled;
2785 f->output_data.x->cursor_gc
2786 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2787 (GCForeground | GCBackground
2788 | GCFillStyle | GCLineWidth),
2789 &gc_values);
2791 /* Create the gray border tile used when the pointer is not in
2792 the frame. Since this depends on the frame's pixel values,
2793 this must be done on a per-frame basis. */
2794 f->output_data.x->border_tile
2795 = (XCreatePixmapFromBitmapData
2796 (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
2797 gray_bits, gray_width, gray_height,
2798 FRAME_FOREGROUND_PIXEL (f),
2799 FRAME_BACKGROUND_PIXEL (f),
2800 DefaultDepth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f))));
2802 unblock_input ();
2806 /* Free what was allocated in x_make_gc. */
2808 void
2809 x_free_gcs (struct frame *f)
2811 Display *dpy = FRAME_X_DISPLAY (f);
2813 block_input ();
2815 if (f->output_data.x->normal_gc)
2817 XFreeGC (dpy, f->output_data.x->normal_gc);
2818 f->output_data.x->normal_gc = 0;
2821 if (f->output_data.x->reverse_gc)
2823 XFreeGC (dpy, f->output_data.x->reverse_gc);
2824 f->output_data.x->reverse_gc = 0;
2827 if (f->output_data.x->cursor_gc)
2829 XFreeGC (dpy, f->output_data.x->cursor_gc);
2830 f->output_data.x->cursor_gc = 0;
2833 if (f->output_data.x->border_tile)
2835 XFreePixmap (dpy, f->output_data.x->border_tile);
2836 f->output_data.x->border_tile = 0;
2839 unblock_input ();
2843 /* Handler for signals raised during x_create_frame and
2844 x_create_tip_frame. FRAME is the frame which is partially
2845 constructed. */
2847 static Lisp_Object
2848 unwind_create_frame (Lisp_Object frame)
2850 struct frame *f = XFRAME (frame);
2852 /* If frame is already dead, nothing to do. This can happen if the
2853 display is disconnected after the frame has become official, but
2854 before x_create_frame removes the unwind protect. */
2855 if (!FRAME_LIVE_P (f))
2856 return Qnil;
2858 /* If frame is ``official'', nothing to do. */
2859 if (NILP (Fmemq (frame, Vframe_list)))
2861 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2862 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2863 #endif
2865 x_free_frame_resources (f);
2866 free_glyphs (f);
2868 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2869 /* Check that reference counts are indeed correct. */
2870 eassert (dpyinfo->reference_count == dpyinfo_refcount);
2871 eassert (dpyinfo->terminal->image_cache->refcount == image_cache_refcount);
2872 #endif
2873 return Qt;
2876 return Qnil;
2879 static void
2880 do_unwind_create_frame (Lisp_Object frame)
2882 unwind_create_frame (frame);
2885 static void
2886 unwind_create_frame_1 (Lisp_Object val)
2888 inhibit_lisp_code = val;
2891 static void
2892 x_default_font_parameter (struct frame *f, Lisp_Object parms)
2894 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2895 Lisp_Object font_param = x_get_arg (dpyinfo, parms, Qfont, NULL, NULL,
2896 RES_TYPE_STRING);
2897 Lisp_Object font = Qnil;
2898 if (EQ (font_param, Qunbound))
2899 font_param = Qnil;
2901 if (NILP (font_param))
2903 /* System font should take precedence over X resources. We suggest this
2904 regardless of font-use-system-font because .emacs may not have been
2905 read yet. */
2906 const char *system_font = xsettings_get_system_font ();
2907 if (system_font)
2908 font = font_open_by_name (f, build_unibyte_string (system_font));
2911 if (NILP (font))
2912 font = !NILP (font_param) ? font_param
2913 : x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
2915 if (! FONTP (font) && ! STRINGP (font))
2917 const char *names[]
2919 #ifdef HAVE_XFT
2920 /* This will find the normal Xft font. */
2921 "monospace-10",
2922 #endif
2923 "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1",
2924 "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2925 "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2926 /* This was formerly the first thing tried, but it finds
2927 too many fonts and takes too long. */
2928 "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1",
2929 /* If those didn't work, look for something which will
2930 at least work. */
2931 "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1",
2932 "fixed",
2933 NULL };
2934 int i;
2936 for (i = 0; names[i]; i++)
2938 font = font_open_by_name (f, build_unibyte_string (names[i]));
2939 if (! NILP (font))
2940 break;
2942 if (NILP (font))
2943 error ("No suitable font was found");
2945 else if (!NILP (font_param))
2947 /* Remember the explicit font parameter, so we can re-apply it after
2948 we've applied the `default' face settings. */
2949 x_set_frame_parameters (f, list1 (Fcons (Qfont_param, font_param)));
2952 /* This call will make X resources override any system font setting. */
2953 x_default_parameter (f, parms, Qfont, font, "font", "Font", RES_TYPE_STRING);
2957 DEFUN ("x-wm-set-size-hint", Fx_wm_set_size_hint, Sx_wm_set_size_hint,
2958 0, 1, 0,
2959 doc: /* Send the size hints for frame FRAME to the window manager.
2960 If FRAME is omitted or nil, use the selected frame.
2961 Signal error if FRAME is not an X frame. */)
2962 (Lisp_Object frame)
2964 struct frame *f = decode_window_system_frame (frame);
2966 block_input ();
2967 x_wm_set_size_hint (f, 0, 0);
2968 unblock_input ();
2969 return Qnil;
2972 static void
2973 set_machine_and_pid_properties (struct frame *f)
2975 long pid = (long) getpid ();
2977 /* This will set WM_CLIENT_MACHINE and WM_LOCALE_NAME. */
2978 XSetWMProperties (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), NULL, NULL,
2979 NULL, 0, NULL, NULL, NULL);
2980 XChangeProperty (FRAME_X_DISPLAY (f),
2981 FRAME_OUTER_WINDOW (f),
2982 XInternAtom (FRAME_X_DISPLAY (f),
2983 "_NET_WM_PID",
2984 False),
2985 XA_CARDINAL, 32, PropModeReplace,
2986 (unsigned char *) &pid, 1);
2989 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
2990 1, 1, 0,
2991 doc: /* Make a new X window, which is called a "frame" in Emacs terms.
2992 Return an Emacs frame object.
2993 PARMS is an alist of frame parameters.
2994 If the parameters specify that the frame should not have a minibuffer,
2995 and do not specify a specific minibuffer window to use,
2996 then `default-minibuffer-frame' must be a frame whose minibuffer can
2997 be shared by the new frame.
2999 This function is an internal primitive--use `make-frame' instead. */)
3000 (Lisp_Object parms)
3002 struct frame *f;
3003 Lisp_Object frame, tem;
3004 Lisp_Object name;
3005 int minibuffer_only = 0;
3006 long window_prompting = 0;
3007 int width, height;
3008 ptrdiff_t count = SPECPDL_INDEX ();
3009 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
3010 Lisp_Object display;
3011 struct x_display_info *dpyinfo = NULL;
3012 Lisp_Object parent;
3013 struct kboard *kb;
3015 parms = Fcopy_alist (parms);
3017 /* Use this general default value to start with
3018 until we know if this frame has a specified name. */
3019 Vx_resource_name = Vinvocation_name;
3021 display = x_get_arg (dpyinfo, parms, Qterminal, 0, 0, RES_TYPE_NUMBER);
3022 if (EQ (display, Qunbound))
3023 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
3024 if (EQ (display, Qunbound))
3025 display = Qnil;
3026 dpyinfo = check_x_display_info (display);
3027 kb = dpyinfo->terminal->kboard;
3029 if (!dpyinfo->terminal->name)
3030 error ("Terminal is not live, can't create new frames on it");
3032 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
3033 if (!STRINGP (name)
3034 && ! EQ (name, Qunbound)
3035 && ! NILP (name))
3036 error ("Invalid frame name--not a string or nil");
3038 if (STRINGP (name))
3039 Vx_resource_name = name;
3041 /* See if parent window is specified. */
3042 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
3043 if (EQ (parent, Qunbound))
3044 parent = Qnil;
3045 if (! NILP (parent))
3046 CHECK_NUMBER (parent);
3048 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
3049 /* No need to protect DISPLAY because that's not used after passing
3050 it to make_frame_without_minibuffer. */
3051 frame = Qnil;
3052 GCPRO4 (parms, parent, name, frame);
3053 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
3054 RES_TYPE_SYMBOL);
3055 if (EQ (tem, Qnone) || NILP (tem))
3056 f = make_frame_without_minibuffer (Qnil, kb, display);
3057 else if (EQ (tem, Qonly))
3059 f = make_minibuffer_frame ();
3060 minibuffer_only = 1;
3062 else if (WINDOWP (tem))
3063 f = make_frame_without_minibuffer (tem, kb, display);
3064 else
3065 f = make_frame (1);
3067 XSETFRAME (frame, f);
3069 f->terminal = dpyinfo->terminal;
3071 f->output_method = output_x_window;
3072 f->output_data.x = xzalloc (sizeof *f->output_data.x);
3073 f->output_data.x->icon_bitmap = -1;
3074 FRAME_FONTSET (f) = -1;
3075 f->output_data.x->scroll_bar_foreground_pixel = -1;
3076 f->output_data.x->scroll_bar_background_pixel = -1;
3077 #ifdef USE_TOOLKIT_SCROLL_BARS
3078 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
3079 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
3080 #endif /* USE_TOOLKIT_SCROLL_BARS */
3082 fset_icon_name (f,
3083 x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
3084 RES_TYPE_STRING));
3085 if (! STRINGP (f->icon_name))
3086 fset_icon_name (f, Qnil);
3088 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
3090 /* With FRAME_X_DISPLAY_INFO set up, this unwind-protect is safe. */
3091 record_unwind_protect (do_unwind_create_frame, frame);
3093 /* These colors will be set anyway later, but it's important
3094 to get the color reference counts right, so initialize them! */
3096 Lisp_Object black;
3097 struct gcpro gcpro1;
3099 /* Function x_decode_color can signal an error. Make
3100 sure to initialize color slots so that we won't try
3101 to free colors we haven't allocated. */
3102 FRAME_FOREGROUND_PIXEL (f) = -1;
3103 FRAME_BACKGROUND_PIXEL (f) = -1;
3104 f->output_data.x->cursor_pixel = -1;
3105 f->output_data.x->cursor_foreground_pixel = -1;
3106 f->output_data.x->border_pixel = -1;
3107 f->output_data.x->mouse_pixel = -1;
3109 black = build_string ("black");
3110 GCPRO1 (black);
3111 FRAME_FOREGROUND_PIXEL (f)
3112 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3113 FRAME_BACKGROUND_PIXEL (f)
3114 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3115 f->output_data.x->cursor_pixel
3116 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3117 f->output_data.x->cursor_foreground_pixel
3118 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3119 f->output_data.x->border_pixel
3120 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3121 f->output_data.x->mouse_pixel
3122 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3123 UNGCPRO;
3126 /* Specify the parent under which to make this X window. */
3128 if (!NILP (parent))
3130 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
3131 f->output_data.x->explicit_parent = 1;
3133 else
3135 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
3136 f->output_data.x->explicit_parent = 0;
3139 /* Set the name; the functions to which we pass f expect the name to
3140 be set. */
3141 if (EQ (name, Qunbound) || NILP (name))
3143 fset_name (f, build_string (dpyinfo->x_id_name));
3144 f->explicit_name = 0;
3146 else
3148 fset_name (f, name);
3149 f->explicit_name = 1;
3150 /* use the frame's title when getting resources for this frame. */
3151 specbind (Qx_resource_name, name);
3154 #ifdef HAVE_FREETYPE
3155 #ifdef HAVE_XFT
3156 register_font_driver (&xftfont_driver, f);
3157 #else /* not HAVE_XFT */
3158 register_font_driver (&ftxfont_driver, f);
3159 #endif /* not HAVE_XFT */
3160 #endif /* HAVE_FREETYPE */
3161 register_font_driver (&xfont_driver, f);
3163 x_default_parameter (f, parms, Qfont_backend, Qnil,
3164 "fontBackend", "FontBackend", RES_TYPE_STRING);
3166 /* Extract the window parameters from the supplied values
3167 that are needed to determine window geometry. */
3168 x_default_font_parameter (f, parms);
3169 if (!FRAME_FONT (f))
3171 delete_frame (frame, Qnoelisp);
3172 error ("Invalid frame font");
3175 /* Frame contents get displaced if an embedded X window has a border. */
3176 if (! FRAME_X_EMBEDDED_P (f))
3177 x_default_parameter (f, parms, Qborder_width, make_number (0),
3178 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
3180 /* This defaults to 1 in order to match xterm. We recognize either
3181 internalBorderWidth or internalBorder (which is what xterm calls
3182 it). */
3183 if (NILP (Fassq (Qinternal_border_width, parms)))
3185 Lisp_Object value;
3187 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
3188 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
3189 if (! EQ (value, Qunbound))
3190 parms = Fcons (Fcons (Qinternal_border_width, value),
3191 parms);
3193 x_default_parameter (f, parms, Qinternal_border_width,
3194 #ifdef USE_GTK /* We used to impose 0 in xg_create_frame_widgets. */
3195 make_number (0),
3196 #else
3197 make_number (1),
3198 #endif
3199 "internalBorderWidth", "internalBorderWidth",
3200 RES_TYPE_NUMBER);
3201 x_default_parameter (f, parms, Qvertical_scroll_bars,
3202 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3203 Qright,
3204 #else
3205 Qleft,
3206 #endif
3207 "verticalScrollBars", "ScrollBars",
3208 RES_TYPE_SYMBOL);
3210 /* Also do the stuff which must be set before the window exists. */
3211 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
3212 "foreground", "Foreground", RES_TYPE_STRING);
3213 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
3214 "background", "Background", RES_TYPE_STRING);
3215 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
3216 "pointerColor", "Foreground", RES_TYPE_STRING);
3217 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
3218 "borderColor", "BorderColor", RES_TYPE_STRING);
3219 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
3220 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
3221 x_default_parameter (f, parms, Qline_spacing, Qnil,
3222 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
3223 x_default_parameter (f, parms, Qleft_fringe, Qnil,
3224 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
3225 x_default_parameter (f, parms, Qright_fringe, Qnil,
3226 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
3228 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
3229 "scrollBarForeground",
3230 "ScrollBarForeground", 1);
3231 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
3232 "scrollBarBackground",
3233 "ScrollBarBackground", 0);
3235 #ifdef GLYPH_DEBUG
3236 image_cache_refcount =
3237 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
3238 dpyinfo_refcount = dpyinfo->reference_count;
3239 #endif /* GLYPH_DEBUG */
3241 /* Init faces before x_default_parameter is called for scroll-bar
3242 parameters because that function calls x_set_scroll_bar_width,
3243 which calls change_frame_size, which calls Fset_window_buffer,
3244 which runs hooks, which call Fvertical_motion. At the end, we
3245 end up in init_iterator with a null face cache, which should not
3246 happen. */
3247 init_frame_faces (f);
3249 /* Set the menu-bar-lines and tool-bar-lines parameters. We don't
3250 look up the X resources controlling the menu-bar and tool-bar
3251 here; they are processed specially at startup, and reflected in
3252 the values of the mode variables.
3254 Avoid calling window-configuration-change-hook; otherwise we
3255 could get an infloop in next_frame since the frame is not yet in
3256 Vframe_list. */
3258 ptrdiff_t count2 = SPECPDL_INDEX ();
3259 record_unwind_protect (unwind_create_frame_1, inhibit_lisp_code);
3260 inhibit_lisp_code = Qt;
3262 x_default_parameter (f, parms, Qmenu_bar_lines,
3263 NILP (Vmenu_bar_mode)
3264 ? make_number (0) : make_number (1),
3265 NULL, NULL, RES_TYPE_NUMBER);
3266 x_default_parameter (f, parms, Qtool_bar_lines,
3267 NILP (Vtool_bar_mode)
3268 ? make_number (0) : make_number (1),
3269 NULL, NULL, RES_TYPE_NUMBER);
3271 unbind_to (count2, Qnil);
3274 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
3275 "bufferPredicate", "BufferPredicate",
3276 RES_TYPE_SYMBOL);
3277 x_default_parameter (f, parms, Qtitle, Qnil,
3278 "title", "Title", RES_TYPE_STRING);
3279 x_default_parameter (f, parms, Qwait_for_wm, Qt,
3280 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN);
3281 x_default_parameter (f, parms, Qfullscreen, Qnil,
3282 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
3283 x_default_parameter (f, parms, Qtool_bar_position,
3284 f->tool_bar_position, 0, 0, RES_TYPE_SYMBOL);
3286 /* Compute the size of the X window. */
3287 window_prompting = x_figure_window_size (f, parms, 1);
3289 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
3290 f->no_split = minibuffer_only || EQ (tem, Qt);
3292 x_icon_verify (f, parms);
3294 /* Create the X widget or window. */
3295 #ifdef USE_X_TOOLKIT
3296 x_window (f, window_prompting, minibuffer_only);
3297 #else
3298 x_window (f);
3299 #endif
3301 x_icon (f, parms);
3302 x_make_gc (f);
3304 /* Now consider the frame official. */
3305 f->terminal->reference_count++;
3306 FRAME_X_DISPLAY_INFO (f)->reference_count++;
3307 Vframe_list = Fcons (frame, Vframe_list);
3309 /* We need to do this after creating the X window, so that the
3310 icon-creation functions can say whose icon they're describing. */
3311 x_default_parameter (f, parms, Qicon_type, Qt,
3312 "bitmapIcon", "BitmapIcon", RES_TYPE_BOOLEAN);
3314 x_default_parameter (f, parms, Qauto_raise, Qnil,
3315 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3316 x_default_parameter (f, parms, Qauto_lower, Qnil,
3317 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3318 x_default_parameter (f, parms, Qcursor_type, Qbox,
3319 "cursorType", "CursorType", RES_TYPE_SYMBOL);
3320 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
3321 "scrollBarWidth", "ScrollBarWidth",
3322 RES_TYPE_NUMBER);
3323 x_default_parameter (f, parms, Qalpha, Qnil,
3324 "alpha", "Alpha", RES_TYPE_NUMBER);
3326 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
3327 Change will not be effected unless different from the current
3328 FRAME_LINES (f). */
3329 width = FRAME_COLS (f);
3330 height = FRAME_LINES (f);
3332 SET_FRAME_COLS (f, 0);
3333 FRAME_LINES (f) = 0;
3334 change_frame_size (f, height, width, 1, 0, 0);
3336 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3337 /* Create the menu bar. */
3338 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3340 /* If this signals an error, we haven't set size hints for the
3341 frame and we didn't make it visible. */
3342 initialize_frame_menubar (f);
3344 #ifndef USE_GTK
3345 /* This is a no-op, except under Motif where it arranges the
3346 main window for the widgets on it. */
3347 lw_set_main_areas (f->output_data.x->column_widget,
3348 f->output_data.x->menubar_widget,
3349 f->output_data.x->edit_widget);
3350 #endif /* not USE_GTK */
3352 #endif /* USE_X_TOOLKIT || USE_GTK */
3354 /* Tell the server what size and position, etc, we want, and how
3355 badly we want them. This should be done after we have the menu
3356 bar so that its size can be taken into account. */
3357 block_input ();
3358 x_wm_set_size_hint (f, window_prompting, 0);
3359 unblock_input ();
3361 /* Make the window appear on the frame and enable display, unless
3362 the caller says not to. However, with explicit parent, Emacs
3363 cannot control visibility, so don't try. */
3364 if (! f->output_data.x->explicit_parent)
3366 Lisp_Object visibility;
3368 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
3369 RES_TYPE_SYMBOL);
3370 if (EQ (visibility, Qunbound))
3371 visibility = Qt;
3373 if (EQ (visibility, Qicon))
3374 x_iconify_frame (f);
3375 else if (! NILP (visibility))
3376 x_make_frame_visible (f);
3377 else
3379 /* Must have been Qnil. */
3383 block_input ();
3385 /* Set machine name and pid for the purpose of window managers. */
3386 set_machine_and_pid_properties (f);
3388 /* Set the WM leader property. GTK does this itself, so this is not
3389 needed when using GTK. */
3390 if (dpyinfo->client_leader_window != 0)
3392 XChangeProperty (FRAME_X_DISPLAY (f),
3393 FRAME_OUTER_WINDOW (f),
3394 dpyinfo->Xatom_wm_client_leader,
3395 XA_WINDOW, 32, PropModeReplace,
3396 (unsigned char *) &dpyinfo->client_leader_window, 1);
3399 unblock_input ();
3401 /* Initialize `default-minibuffer-frame' in case this is the first
3402 frame on this terminal. */
3403 if (FRAME_HAS_MINIBUF_P (f)
3404 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
3405 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
3406 kset_default_minibuffer_frame (kb, frame);
3408 /* All remaining specified parameters, which have not been "used"
3409 by x_get_arg and friends, now go in the misc. alist of the frame. */
3410 for (tem = parms; CONSP (tem); tem = XCDR (tem))
3411 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
3412 fset_param_alist (f, Fcons (XCAR (tem), f->param_alist));
3414 UNGCPRO;
3416 /* Make sure windows on this frame appear in calls to next-window
3417 and similar functions. */
3418 Vwindow_list = Qnil;
3420 return unbind_to (count, frame);
3424 /* FRAME is used only to get a handle on the X display. We don't pass the
3425 display info directly because we're called from frame.c, which doesn't
3426 know about that structure. */
3428 Lisp_Object
3429 x_get_focus_frame (struct frame *frame)
3431 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (frame);
3432 Lisp_Object xfocus;
3433 if (! dpyinfo->x_focus_frame)
3434 return Qnil;
3436 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
3437 return xfocus;
3441 /* In certain situations, when the window manager follows a
3442 click-to-focus policy, there seems to be no way around calling
3443 XSetInputFocus to give another frame the input focus .
3445 In an ideal world, XSetInputFocus should generally be avoided so
3446 that applications don't interfere with the window manager's focus
3447 policy. But I think it's okay to use when it's clearly done
3448 following a user-command. */
3450 DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
3451 doc: /* Set the input focus to FRAME.
3452 FRAME nil means use the selected frame. */)
3453 (Lisp_Object frame)
3455 struct frame *f = decode_window_system_frame (frame);
3456 Display *dpy = FRAME_X_DISPLAY (f);
3458 block_input ();
3459 x_catch_errors (dpy);
3461 if (FRAME_X_EMBEDDED_P (f))
3463 /* For Xembedded frames, normally the embedder forwards key
3464 events. See XEmbed Protocol Specification at
3465 http://freedesktop.org/wiki/Specifications/xembed-spec */
3466 xembed_request_focus (f);
3468 else
3470 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3471 RevertToParent, CurrentTime);
3472 x_ewmh_activate_frame (f);
3475 x_uncatch_errors ();
3476 unblock_input ();
3478 return Qnil;
3482 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
3483 doc: /* Internal function called by `color-defined-p', which see
3484 .\(Note that the Nextstep version of this function ignores FRAME.) */)
3485 (Lisp_Object color, Lisp_Object frame)
3487 XColor foo;
3488 FRAME_PTR f = decode_window_system_frame (frame);
3490 CHECK_STRING (color);
3492 if (x_defined_color (f, SSDATA (color), &foo, 0))
3493 return Qt;
3494 else
3495 return Qnil;
3498 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
3499 doc: /* Internal function called by `color-values', which see. */)
3500 (Lisp_Object color, Lisp_Object frame)
3502 XColor foo;
3503 FRAME_PTR f = decode_window_system_frame (frame);
3505 CHECK_STRING (color);
3507 if (x_defined_color (f, SSDATA (color), &foo, 0))
3508 return list3i (foo.red, foo.green, foo.blue);
3509 else
3510 return Qnil;
3513 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
3514 doc: /* Internal function called by `display-color-p', which see. */)
3515 (Lisp_Object terminal)
3517 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3519 if (dpyinfo->n_planes <= 2)
3520 return Qnil;
3522 switch (dpyinfo->visual->class)
3524 case StaticColor:
3525 case PseudoColor:
3526 case TrueColor:
3527 case DirectColor:
3528 return Qt;
3530 default:
3531 return Qnil;
3535 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
3536 0, 1, 0,
3537 doc: /* Return t if the X display supports shades of gray.
3538 Note that color displays do support shades of gray.
3539 The optional argument TERMINAL specifies which display to ask about.
3540 TERMINAL should be a terminal object, a frame or a display name (a string).
3541 If omitted or nil, that stands for the selected frame's display. */)
3542 (Lisp_Object terminal)
3544 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3546 if (dpyinfo->n_planes <= 1)
3547 return Qnil;
3549 switch (dpyinfo->visual->class)
3551 case StaticColor:
3552 case PseudoColor:
3553 case TrueColor:
3554 case DirectColor:
3555 case StaticGray:
3556 case GrayScale:
3557 return Qt;
3559 default:
3560 return Qnil;
3564 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
3565 0, 1, 0,
3566 doc: /* Return the width in pixels of the X display TERMINAL.
3567 The optional argument TERMINAL specifies which display to ask about.
3568 TERMINAL should be a terminal object, a frame or a display name (a string).
3569 If omitted or nil, that stands for the selected frame's display.
3571 On \"multi-monitor\" setups this refers to the pixel width for all
3572 physical monitors associated with TERMINAL. To get information for
3573 each physical monitor, use `display-monitor-attributes-list'. */)
3574 (Lisp_Object terminal)
3576 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3578 return make_number (x_display_pixel_width (dpyinfo));
3581 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
3582 Sx_display_pixel_height, 0, 1, 0,
3583 doc: /* Return the height in pixels of the X display TERMINAL.
3584 The optional argument TERMINAL specifies which display to ask about.
3585 TERMINAL should be a terminal object, a frame or a display name (a string).
3586 If omitted or nil, that stands for the selected frame's display.
3588 On \"multi-monitor\" setups this refers to the pixel height for all
3589 physical monitors associated with TERMINAL. To get information for
3590 each physical monitor, use `display-monitor-attributes-list'. */)
3591 (Lisp_Object terminal)
3593 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3595 return make_number (x_display_pixel_height (dpyinfo));
3598 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
3599 0, 1, 0,
3600 doc: /* Return the number of bitplanes of the X display TERMINAL.
3601 The optional argument TERMINAL specifies which display to ask about.
3602 TERMINAL should be a terminal object, a frame or a display name (a string).
3603 If omitted or nil, that stands for the selected frame's display. */)
3604 (Lisp_Object terminal)
3606 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3608 return make_number (dpyinfo->n_planes);
3611 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
3612 0, 1, 0,
3613 doc: /* Return the number of color cells of the X display TERMINAL.
3614 The optional argument TERMINAL specifies which display to ask about.
3615 TERMINAL should be a terminal object, a frame or a display name (a string).
3616 If omitted or nil, that stands for the selected frame's display. */)
3617 (Lisp_Object terminal)
3619 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3621 int nr_planes = DisplayPlanes (dpyinfo->display,
3622 XScreenNumberOfScreen (dpyinfo->screen));
3624 /* Truncate nr_planes to 24 to avoid integer overflow.
3625 Some displays says 32, but only 24 bits are actually significant.
3626 There are only very few and rare video cards that have more than
3627 24 significant bits. Also 24 bits is more than 16 million colors,
3628 it "should be enough for everyone". */
3629 if (nr_planes > 24) nr_planes = 24;
3631 return make_number (1 << nr_planes);
3634 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
3635 Sx_server_max_request_size,
3636 0, 1, 0,
3637 doc: /* Return the maximum request size of the X server of display TERMINAL.
3638 The optional argument TERMINAL specifies which display to ask about.
3639 TERMINAL should be a terminal object, a frame or a display name (a string).
3640 If omitted or nil, that stands for the selected frame's display. */)
3641 (Lisp_Object terminal)
3643 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3645 return make_number (MAXREQUEST (dpyinfo->display));
3648 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
3649 doc: /* Return the "vendor ID" string of the X server of display TERMINAL.
3650 \(Labeling every distributor as a "vendor" embodies the false assumption
3651 that operating systems cannot be developed and distributed noncommercially.)
3652 The optional argument TERMINAL specifies which display to ask about.
3653 TERMINAL should be a terminal object, a frame or a display name (a string).
3654 If omitted or nil, that stands for the selected frame's display. */)
3655 (Lisp_Object terminal)
3657 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3658 const char *vendor = ServerVendor (dpyinfo->display);
3660 if (! vendor) vendor = "";
3661 return build_string (vendor);
3664 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
3665 doc: /* Return the version numbers of the X server of display TERMINAL.
3666 The value is a list of three integers: the major and minor
3667 version numbers of the X Protocol in use, and the distributor-specific release
3668 number. See also the function `x-server-vendor'.
3670 The optional argument TERMINAL specifies which display to ask about.
3671 TERMINAL should be a terminal object, a frame or a display name (a string).
3672 If omitted or nil, that stands for the selected frame's display. */)
3673 (Lisp_Object terminal)
3675 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3676 Display *dpy = dpyinfo->display;
3678 return list3i (ProtocolVersion (dpy), ProtocolRevision (dpy),
3679 VendorRelease (dpy));
3682 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
3683 doc: /* Return the number of screens on the X server of display TERMINAL.
3684 The optional argument TERMINAL specifies which display to ask about.
3685 TERMINAL should be a terminal object, a frame or a display name (a string).
3686 If omitted or nil, that stands for the selected frame's display. */)
3687 (Lisp_Object terminal)
3689 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3691 return make_number (ScreenCount (dpyinfo->display));
3694 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
3695 doc: /* Return the height in millimeters of the X display TERMINAL.
3696 The optional argument TERMINAL specifies which display to ask about.
3697 TERMINAL should be a terminal object, a frame or a display name (a string).
3698 If omitted or nil, that stands for the selected frame's display.
3700 On \"multi-monitor\" setups this refers to the height in millimeters for
3701 all physical monitors associated with TERMINAL. To get information
3702 for each physical monitor, use `display-monitor-attributes-list'. */)
3703 (Lisp_Object terminal)
3705 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3707 return make_number (HeightMMOfScreen (dpyinfo->screen));
3710 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
3711 doc: /* Return the width in millimeters of the X display TERMINAL.
3712 The optional argument TERMINAL specifies which display to ask about.
3713 TERMINAL should be a terminal object, a frame or a display name (a string).
3714 If omitted or nil, that stands for the selected frame's display.
3716 On \"multi-monitor\" setups this refers to the width in millimeters for
3717 all physical monitors associated with TERMINAL. To get information
3718 for each physical monitor, use `display-monitor-attributes-list'. */)
3719 (Lisp_Object terminal)
3721 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3723 return make_number (WidthMMOfScreen (dpyinfo->screen));
3726 DEFUN ("x-display-backing-store", Fx_display_backing_store,
3727 Sx_display_backing_store, 0, 1, 0,
3728 doc: /* Return an indication of whether X display TERMINAL does backing store.
3729 The value may be `always', `when-mapped', or `not-useful'.
3730 The optional argument TERMINAL specifies which display to ask about.
3731 TERMINAL should be a terminal object, a frame or a display name (a string).
3732 If omitted or nil, that stands for the selected frame's display. */)
3733 (Lisp_Object terminal)
3735 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3736 Lisp_Object result;
3738 switch (DoesBackingStore (dpyinfo->screen))
3740 case Always:
3741 result = intern ("always");
3742 break;
3744 case WhenMapped:
3745 result = intern ("when-mapped");
3746 break;
3748 case NotUseful:
3749 result = intern ("not-useful");
3750 break;
3752 default:
3753 error ("Strange value for BackingStore parameter of screen");
3756 return result;
3759 DEFUN ("x-display-visual-class", Fx_display_visual_class,
3760 Sx_display_visual_class, 0, 1, 0,
3761 doc: /* Return the visual class of the X display TERMINAL.
3762 The value is one of the symbols `static-gray', `gray-scale',
3763 `static-color', `pseudo-color', `true-color', or `direct-color'.
3765 The optional argument TERMINAL specifies which display to ask about.
3766 TERMINAL should a terminal object, a frame or a display name (a string).
3767 If omitted or nil, that stands for the selected frame's display. */)
3768 (Lisp_Object terminal)
3770 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3771 Lisp_Object result;
3773 switch (dpyinfo->visual->class)
3775 case StaticGray:
3776 result = intern ("static-gray");
3777 break;
3778 case GrayScale:
3779 result = intern ("gray-scale");
3780 break;
3781 case StaticColor:
3782 result = intern ("static-color");
3783 break;
3784 case PseudoColor:
3785 result = intern ("pseudo-color");
3786 break;
3787 case TrueColor:
3788 result = intern ("true-color");
3789 break;
3790 case DirectColor:
3791 result = intern ("direct-color");
3792 break;
3793 default:
3794 error ("Display has an unknown visual class");
3797 return result;
3800 DEFUN ("x-display-save-under", Fx_display_save_under,
3801 Sx_display_save_under, 0, 1, 0,
3802 doc: /* Return t if the X display TERMINAL supports the save-under feature.
3803 The optional argument TERMINAL specifies which display to ask about.
3804 TERMINAL should be a terminal object, a frame or a display name (a string).
3805 If omitted or nil, that stands for the selected frame's display. */)
3806 (Lisp_Object terminal)
3808 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3810 if (DoesSaveUnders (dpyinfo->screen) == True)
3811 return Qt;
3812 else
3813 return Qnil;
3816 /* Store the geometry of the workarea on display DPYINFO into *RECT.
3817 Return false if and only if the workarea information cannot be
3818 obtained via the _NET_WORKAREA root window property. */
3820 #if ! GTK_CHECK_VERSION (3, 4, 0)
3821 static bool
3822 x_get_net_workarea (struct x_display_info *dpyinfo, XRectangle *rect)
3824 Display *dpy = dpyinfo->display;
3825 long offset, max_len;
3826 Atom target_type, actual_type;
3827 unsigned long actual_size, bytes_remaining;
3828 int rc, actual_format;
3829 unsigned char *tmp_data = NULL;
3830 bool result = false;
3832 x_catch_errors (dpy);
3833 offset = 0;
3834 max_len = 1;
3835 target_type = XA_CARDINAL;
3836 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3837 dpyinfo->Xatom_net_current_desktop,
3838 offset, max_len, False, target_type,
3839 &actual_type, &actual_format, &actual_size,
3840 &bytes_remaining, &tmp_data);
3841 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3842 && actual_format == 32 && actual_size == max_len)
3844 long current_desktop = ((long *) tmp_data)[0];
3846 XFree (tmp_data);
3847 tmp_data = NULL;
3849 offset = 4 * current_desktop;
3850 max_len = 4;
3851 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3852 dpyinfo->Xatom_net_workarea,
3853 offset, max_len, False, target_type,
3854 &actual_type, &actual_format, &actual_size,
3855 &bytes_remaining, &tmp_data);
3856 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3857 && actual_format == 32 && actual_size == max_len)
3859 long *values = (long *) tmp_data;
3861 rect->x = values[0];
3862 rect->y = values[1];
3863 rect->width = values[2];
3864 rect->height = values[3];
3866 XFree (tmp_data);
3867 tmp_data = NULL;
3869 result = true;
3872 if (tmp_data)
3873 XFree (tmp_data);
3874 x_uncatch_errors ();
3876 return result;
3878 #endif
3880 #ifndef USE_GTK
3882 /* Return monitor number where F is "most" or closest to. */
3883 static int
3884 x_get_monitor_for_frame (struct frame *f,
3885 struct MonitorInfo *monitors,
3886 int n_monitors)
3888 XRectangle frect;
3889 int area = 0, dist = -1;
3890 int best_area = -1, best_dist = -1;
3891 int i;
3893 if (n_monitors == 1) return 0;
3894 frect.x = f->left_pos;
3895 frect.y = f->top_pos;
3896 frect.width = FRAME_PIXEL_WIDTH (f);
3897 frect.height = FRAME_PIXEL_HEIGHT (f);
3899 for (i = 0; i < n_monitors; ++i)
3901 struct MonitorInfo *mi = &monitors[i];
3902 XRectangle res;
3903 int a = 0;
3905 if (mi->geom.width == 0) continue;
3907 if (x_intersect_rectangles (&mi->geom, &frect, &res))
3909 a = res.width * res.height;
3910 if (a > area)
3912 area = a;
3913 best_area = i;
3917 if (a == 0 && area == 0)
3919 int dx, dy, d;
3920 if (frect.x + frect.width < mi->geom.x)
3921 dx = mi->geom.x - frect.x + frect.width;
3922 else if (frect.x > mi->geom.x + mi->geom.width)
3923 dx = frect.x - mi->geom.x + mi->geom.width;
3924 else
3925 dx = 0;
3926 if (frect.y + frect.height < mi->geom.y)
3927 dy = mi->geom.y - frect.y + frect.height;
3928 else if (frect.y > mi->geom.y + mi->geom.height)
3929 dy = frect.y - mi->geom.y + mi->geom.height;
3930 else
3931 dy = 0;
3933 d = dx*dx + dy*dy;
3934 if (dist == -1 || dist > d)
3936 dist = d;
3937 best_dist = i;
3942 return best_area != -1 ? best_area : (best_dist != -1 ? best_dist : 0);
3945 static Lisp_Object
3946 x_make_monitor_attribute_list (struct MonitorInfo *monitors,
3947 int n_monitors,
3948 int primary_monitor,
3949 struct x_display_info *dpyinfo,
3950 const char *source)
3952 Lisp_Object monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
3953 Lisp_Object frame, rest;
3955 FOR_EACH_FRAME (rest, frame)
3957 struct frame *f = XFRAME (frame);
3959 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo
3960 && !EQ (frame, tip_frame))
3962 int i = x_get_monitor_for_frame (f, monitors, n_monitors);
3963 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
3967 return make_monitor_attribute_list (monitors, n_monitors, primary_monitor,
3968 monitor_frames, source);
3971 static Lisp_Object
3972 x_get_monitor_attributes_fallback (struct x_display_info *dpyinfo)
3974 struct MonitorInfo monitor;
3975 XRectangle workarea_r;
3977 /* Fallback: treat (possibly) multiple physical monitors as if they
3978 formed a single monitor as a whole. This should provide a
3979 consistent result at least on single monitor environments. */
3980 monitor.geom.x = monitor.geom.y = 0;
3981 monitor.geom.width = x_display_pixel_width (dpyinfo);
3982 monitor.geom.height = x_display_pixel_height (dpyinfo);
3983 monitor.mm_width = WidthMMOfScreen (dpyinfo->screen);
3984 monitor.mm_height = HeightMMOfScreen (dpyinfo->screen);
3985 monitor.name = xstrdup ("combined screen");
3987 if (x_get_net_workarea (dpyinfo, &workarea_r))
3988 monitor.work = workarea_r;
3989 else
3990 monitor.work = monitor.geom;
3991 return x_make_monitor_attribute_list (&monitor, 1, 0, dpyinfo, "fallback");
3995 #ifdef HAVE_XINERAMA
3996 static Lisp_Object
3997 x_get_monitor_attributes_xinerama (struct x_display_info *dpyinfo)
3999 int n_monitors, i;
4000 Lisp_Object attributes_list = Qnil;
4001 Display *dpy = dpyinfo->display;
4002 XineramaScreenInfo *info = XineramaQueryScreens (dpy, &n_monitors);
4003 struct MonitorInfo *monitors;
4004 double mm_width_per_pixel, mm_height_per_pixel;
4006 if (! info || n_monitors == 0)
4008 if (info)
4009 XFree (info);
4010 return attributes_list;
4013 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
4014 / x_display_pixel_width (dpyinfo));
4015 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
4016 / x_display_pixel_height (dpyinfo));
4017 monitors = (struct MonitorInfo *) xzalloc (n_monitors * sizeof (*monitors));
4018 for (i = 0; i < n_monitors; ++i)
4020 struct MonitorInfo *mi = &monitors[i];
4021 XRectangle workarea_r;
4023 mi->geom.x = info[i].x_org;
4024 mi->geom.y = info[i].y_org;
4025 mi->geom.width = info[i].width;
4026 mi->geom.height = info[i].height;
4027 mi->mm_width = mi->geom.width * mm_width_per_pixel + 0.5;
4028 mi->mm_height = mi->geom.height * mm_height_per_pixel + 0.5;
4029 mi->name = 0;
4031 /* Xinerama usually have primary monitor first, just use that. */
4032 if (i == 0 && x_get_net_workarea (dpyinfo, &workarea_r))
4034 mi->work = workarea_r;
4035 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
4036 mi->work = mi->geom;
4038 else
4039 mi->work = mi->geom;
4041 XFree (info);
4043 attributes_list = x_make_monitor_attribute_list (monitors,
4044 n_monitors,
4046 dpyinfo,
4047 "Xinerama");
4048 free_monitors (monitors, n_monitors);
4049 return attributes_list;
4051 #endif /* HAVE_XINERAMA */
4054 #ifdef HAVE_XRANDR
4055 static Lisp_Object
4056 x_get_monitor_attributes_xrandr (struct x_display_info *dpyinfo)
4058 Lisp_Object attributes_list = Qnil;
4059 XRRScreenResources *resources;
4060 Display *dpy = dpyinfo->display;
4061 int i, n_monitors, primary = -1;
4062 RROutput pxid = None;
4063 struct MonitorInfo *monitors;
4065 #ifdef HAVE_XRRGETSCREENRESOURCESCURRENT
4066 resources = XRRGetScreenResourcesCurrent (dpy, dpyinfo->root_window);
4067 #else
4068 resources = XRRGetScreenResources (dpy, dpyinfo->root_window);
4069 #endif
4070 if (! resources || resources->noutput == 0)
4072 if (resources)
4073 XRRFreeScreenResources (resources);
4074 return Qnil;
4076 n_monitors = resources->noutput;
4077 monitors = (struct MonitorInfo *) xzalloc (n_monitors * sizeof (*monitors));
4079 #ifdef HAVE_XRRGETOUTPUTPRIMARY
4080 pxid = XRRGetOutputPrimary (dpy, dpyinfo->root_window);
4081 #endif
4083 for (i = 0; i < n_monitors; ++i)
4085 XRROutputInfo *info = XRRGetOutputInfo (dpy, resources,
4086 resources->outputs[i]);
4087 Connection conn = info ? info->connection : RR_Disconnected;
4088 RRCrtc id = info ? info->crtc : None;
4090 if (strcmp (info->name, "default") == 0)
4092 /* Non XRandr 1.2 driver, does not give useful data. */
4093 XRRFreeOutputInfo (info);
4094 XRRFreeScreenResources (resources);
4095 free_monitors (monitors, n_monitors);
4096 return Qnil;
4099 if (conn != RR_Disconnected && id != None)
4101 XRRCrtcInfo *crtc = XRRGetCrtcInfo (dpy, resources, id);
4102 struct MonitorInfo *mi = &monitors[i];
4103 XRectangle workarea_r;
4105 if (! crtc)
4107 XRRFreeOutputInfo (info);
4108 continue;
4111 mi->geom.x = crtc->x;
4112 mi->geom.y = crtc->y;
4113 mi->geom.width = crtc->width;
4114 mi->geom.height = crtc->height;
4115 mi->mm_width = info->mm_width;
4116 mi->mm_height = info->mm_height;
4117 mi->name = xstrdup (info->name);
4119 if (pxid != None && pxid == resources->outputs[i])
4120 primary = i;
4121 else if (primary == -1 && strcmp (info->name, "LVDS") == 0)
4122 primary = i;
4124 if (i == primary && x_get_net_workarea (dpyinfo, &workarea_r))
4126 mi->work= workarea_r;
4127 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
4128 mi->work = mi->geom;
4130 else
4131 mi->work = mi->geom;
4133 XRRFreeCrtcInfo (crtc);
4135 XRRFreeOutputInfo (info);
4137 XRRFreeScreenResources (resources);
4139 attributes_list = x_make_monitor_attribute_list (monitors,
4140 n_monitors,
4141 primary,
4142 dpyinfo,
4143 "XRandr");
4144 free_monitors (monitors, n_monitors);
4145 return attributes_list;
4147 #endif /* HAVE_XRANDR */
4149 static Lisp_Object
4150 x_get_monitor_attributes (struct x_display_info *dpyinfo)
4152 Lisp_Object attributes_list = Qnil;
4153 Display *dpy = dpyinfo->display;
4155 #ifdef HAVE_XRANDR
4156 int xrr_event_base, xrr_error_base;
4157 bool xrr_ok = false;
4158 xrr_ok = XRRQueryExtension (dpy, &xrr_event_base, &xrr_error_base);
4159 if (xrr_ok)
4161 int xrr_major, xrr_minor;
4162 XRRQueryVersion (dpy, &xrr_major, &xrr_minor);
4163 xrr_ok = (xrr_major == 1 && xrr_minor >= 2) || xrr_major > 1;
4166 if (xrr_ok)
4167 attributes_list = x_get_monitor_attributes_xrandr (dpyinfo);
4168 #endif /* HAVE_XRANDR */
4170 #ifdef HAVE_XINERAMA
4171 if (NILP (attributes_list))
4173 int xin_event_base, xin_error_base;
4174 bool xin_ok = false;
4175 xin_ok = XineramaQueryExtension (dpy, &xin_event_base, &xin_error_base);
4176 if (xin_ok && XineramaIsActive (dpy))
4177 attributes_list = x_get_monitor_attributes_xinerama (dpyinfo);
4179 #endif /* HAVE_XINERAMA */
4181 if (NILP (attributes_list))
4182 attributes_list = x_get_monitor_attributes_fallback (dpyinfo);
4184 return attributes_list;
4187 #endif /* !USE_GTK */
4189 DEFUN ("x-display-monitor-attributes-list", Fx_display_monitor_attributes_list,
4190 Sx_display_monitor_attributes_list,
4191 0, 1, 0,
4192 doc: /* Return a list of physical monitor attributes on the X display TERMINAL.
4194 The optional argument TERMINAL specifies which display to ask about.
4195 TERMINAL should be a terminal object, a frame or a display name (a string).
4196 If omitted or nil, that stands for the selected frame's display.
4198 In addition to the standard attribute keys listed in
4199 `display-monitor-attributes-list', the following keys are contained in
4200 the attributes:
4202 source -- String describing the source from which multi-monitor
4203 information is obtained, one of \"Gdk\", \"XRandr\",
4204 \"Xinerama\", or \"fallback\"
4206 Internal use only, use `display-monitor-attributes-list' instead. */)
4207 (Lisp_Object terminal)
4209 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4210 Lisp_Object attributes_list = Qnil;
4212 #ifdef USE_GTK
4213 double mm_width_per_pixel, mm_height_per_pixel;
4214 GdkDisplay *gdpy;
4215 GdkScreen *gscreen;
4216 gint primary_monitor = 0, n_monitors, i;
4217 Lisp_Object monitor_frames, rest, frame;
4218 static const char *source = "Gdk";
4219 struct MonitorInfo *monitors;
4221 block_input ();
4222 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
4223 / x_display_pixel_width (dpyinfo));
4224 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
4225 / x_display_pixel_height (dpyinfo));
4226 gdpy = gdk_x11_lookup_xdisplay (dpyinfo->display);
4227 gscreen = gdk_display_get_default_screen (gdpy);
4228 #if GTK_CHECK_VERSION (2, 20, 0)
4229 primary_monitor = gdk_screen_get_primary_monitor (gscreen);
4230 #endif
4231 n_monitors = gdk_screen_get_n_monitors (gscreen);
4232 monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
4233 monitors = (struct MonitorInfo *) xzalloc (n_monitors * sizeof (*monitors));
4235 FOR_EACH_FRAME (rest, frame)
4237 struct frame *f = XFRAME (frame);
4239 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo
4240 && !EQ (frame, tip_frame))
4242 GdkWindow *gwin = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
4244 i = gdk_screen_get_monitor_at_window (gscreen, gwin);
4245 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
4249 for (i = 0; i < n_monitors; ++i)
4251 gint width_mm = -1, height_mm = -1;
4252 GdkRectangle rec, work;
4253 struct MonitorInfo *mi = &monitors[i];
4255 gdk_screen_get_monitor_geometry (gscreen, i, &rec);
4257 #if GTK_CHECK_VERSION (2, 14, 0)
4258 width_mm = gdk_screen_get_monitor_width_mm (gscreen, i);
4259 height_mm = gdk_screen_get_monitor_height_mm (gscreen, i);
4260 #endif
4261 if (width_mm < 0)
4262 width_mm = rec.width * mm_width_per_pixel + 0.5;
4263 if (height_mm < 0)
4264 height_mm = rec.height * mm_height_per_pixel + 0.5;
4266 #if GTK_CHECK_VERSION (3, 4, 0)
4267 gdk_screen_get_monitor_workarea (gscreen, i, &work);
4268 #else
4269 /* Emulate the behavior of GTK+ 3.4. */
4271 XRectangle workarea_r;
4273 if (i == primary_monitor && x_get_net_workarea (dpyinfo, &workarea_r))
4275 work.x = workarea_r.x;
4276 work.y = workarea_r.y;
4277 work.width = workarea_r.width;
4278 work.height = workarea_r.height;
4279 if (! gdk_rectangle_intersect (&rec, &work, &work))
4280 work = rec;
4282 else
4283 work = rec;
4285 #endif
4288 mi->geom.x = rec.x;
4289 mi->geom.y = rec.y;
4290 mi->geom.width = rec.width;
4291 mi->geom.height = rec.height;
4292 mi->work.x = work.x;
4293 mi->work.y = work.y;
4294 mi->work.width = work.width;
4295 mi->work.height = work.height;
4296 mi->mm_width = width_mm;
4297 mi->mm_height = height_mm;
4299 #if GTK_CHECK_VERSION (2, 14, 0)
4300 mi->name = gdk_screen_get_monitor_plug_name (gscreen, i);
4301 #endif
4304 attributes_list = make_monitor_attribute_list (monitors,
4305 n_monitors,
4306 primary_monitor,
4307 monitor_frames,
4308 source);
4309 unblock_input ();
4310 #else /* not USE_GTK */
4312 block_input ();
4313 attributes_list = x_get_monitor_attributes (dpyinfo);
4314 unblock_input ();
4316 #endif /* not USE_GTK */
4318 return attributes_list;
4321 /************************************************************************
4322 X Displays
4323 ************************************************************************/
4326 /* Mapping visual names to visuals. */
4328 static struct visual_class
4330 const char *name;
4331 int class;
4333 visual_classes[] =
4335 {"StaticGray", StaticGray},
4336 {"GrayScale", GrayScale},
4337 {"StaticColor", StaticColor},
4338 {"PseudoColor", PseudoColor},
4339 {"TrueColor", TrueColor},
4340 {"DirectColor", DirectColor},
4341 {NULL, 0}
4345 #ifndef HAVE_XSCREENNUMBEROFSCREEN
4347 /* Value is the screen number of screen SCR. This is a substitute for
4348 the X function with the same name when that doesn't exist. */
4351 XScreenNumberOfScreen (scr)
4352 register Screen *scr;
4354 Display *dpy = scr->display;
4355 int i;
4357 for (i = 0; i < dpy->nscreens; ++i)
4358 if (scr == dpy->screens + i)
4359 break;
4361 return i;
4364 #endif /* not HAVE_XSCREENNUMBEROFSCREEN */
4367 /* Select the visual that should be used on display DPYINFO. Set
4368 members of DPYINFO appropriately. Called from x_term_init. */
4370 void
4371 select_visual (struct x_display_info *dpyinfo)
4373 Display *dpy = dpyinfo->display;
4374 Screen *screen = dpyinfo->screen;
4375 Lisp_Object value;
4377 /* See if a visual is specified. */
4378 value = display_x_get_resource (dpyinfo,
4379 build_string ("visualClass"),
4380 build_string ("VisualClass"),
4381 Qnil, Qnil);
4382 if (STRINGP (value))
4384 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
4385 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
4386 depth, a decimal number. NAME is compared with case ignored. */
4387 char *s = alloca (SBYTES (value) + 1);
4388 char *dash;
4389 int i, class = -1;
4390 XVisualInfo vinfo;
4392 strcpy (s, SSDATA (value));
4393 dash = strchr (s, '-');
4394 if (dash)
4396 dpyinfo->n_planes = atoi (dash + 1);
4397 *dash = '\0';
4399 else
4400 /* We won't find a matching visual with depth 0, so that
4401 an error will be printed below. */
4402 dpyinfo->n_planes = 0;
4404 /* Determine the visual class. */
4405 for (i = 0; visual_classes[i].name; ++i)
4406 if (xstrcasecmp (s, visual_classes[i].name) == 0)
4408 class = visual_classes[i].class;
4409 break;
4412 /* Look up a matching visual for the specified class. */
4413 if (class == -1
4414 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
4415 dpyinfo->n_planes, class, &vinfo))
4416 fatal ("Invalid visual specification `%s'", SDATA (value));
4418 dpyinfo->visual = vinfo.visual;
4420 else
4422 int n_visuals;
4423 XVisualInfo *vinfo, vinfo_template;
4425 dpyinfo->visual = DefaultVisualOfScreen (screen);
4427 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
4428 vinfo_template.screen = XScreenNumberOfScreen (screen);
4429 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
4430 &vinfo_template, &n_visuals);
4431 if (n_visuals <= 0)
4432 fatal ("Can't get proper X visual info");
4434 dpyinfo->n_planes = vinfo->depth;
4435 XFree (vinfo);
4440 /* Return the X display structure for the display named NAME.
4441 Open a new connection if necessary. */
4443 static struct x_display_info *
4444 x_display_info_for_name (Lisp_Object name)
4446 Lisp_Object names;
4447 struct x_display_info *dpyinfo;
4449 CHECK_STRING (name);
4451 #if 0
4452 if (! EQ (Vinitial_window_system, intern ("x")))
4453 error ("Not using X Windows"); /* That doesn't stop us anymore. */
4454 #endif
4456 for (dpyinfo = x_display_list, names = x_display_name_list;
4457 dpyinfo;
4458 dpyinfo = dpyinfo->next, names = XCDR (names))
4460 Lisp_Object tem;
4461 tem = Fstring_equal (XCAR (XCAR (names)), name);
4462 if (!NILP (tem))
4463 return dpyinfo;
4466 /* Use this general default value to start with. */
4467 Vx_resource_name = Vinvocation_name;
4469 validate_x_resource_name ();
4471 dpyinfo = x_term_init (name, (char *)0,
4472 SSDATA (Vx_resource_name));
4474 if (dpyinfo == 0)
4475 error ("Cannot connect to X server %s", SDATA (name));
4477 XSETFASTINT (Vwindow_system_version, 11);
4479 return dpyinfo;
4483 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
4484 1, 3, 0,
4485 doc: /* Open a connection to a display server.
4486 DISPLAY is the name of the display to connect to.
4487 Optional second arg XRM-STRING is a string of resources in xrdb format.
4488 If the optional third arg MUST-SUCCEED is non-nil,
4489 terminate Emacs if we can't open the connection.
4490 \(In the Nextstep version, the last two arguments are currently ignored.) */)
4491 (Lisp_Object display, Lisp_Object xrm_string, Lisp_Object must_succeed)
4493 char *xrm_option;
4494 struct x_display_info *dpyinfo;
4496 CHECK_STRING (display);
4497 if (! NILP (xrm_string))
4498 CHECK_STRING (xrm_string);
4500 #if 0
4501 if (! EQ (Vinitial_window_system, intern ("x")))
4502 error ("Not using X Windows"); /* That doesn't stop us anymore. */
4503 #endif
4505 if (! NILP (xrm_string))
4506 xrm_option = SSDATA (xrm_string);
4507 else
4508 xrm_option = (char *) 0;
4510 validate_x_resource_name ();
4512 /* This is what opens the connection and sets x_current_display.
4513 This also initializes many symbols, such as those used for input. */
4514 dpyinfo = x_term_init (display, xrm_option,
4515 SSDATA (Vx_resource_name));
4517 if (dpyinfo == 0)
4519 if (!NILP (must_succeed))
4520 fatal ("Cannot connect to X server %s.\n\
4521 Check the DISPLAY environment variable or use `-d'.\n\
4522 Also use the `xauth' program to verify that you have the proper\n\
4523 authorization information needed to connect the X server.\n\
4524 An insecure way to solve the problem may be to use `xhost'.\n",
4525 SDATA (display));
4526 else
4527 error ("Cannot connect to X server %s", SDATA (display));
4530 XSETFASTINT (Vwindow_system_version, 11);
4531 return Qnil;
4534 DEFUN ("x-close-connection", Fx_close_connection,
4535 Sx_close_connection, 1, 1, 0,
4536 doc: /* Close the connection to TERMINAL's X server.
4537 For TERMINAL, specify a terminal object, a frame or a display name (a
4538 string). If TERMINAL is nil, that stands for the selected frame's
4539 terminal. */)
4540 (Lisp_Object terminal)
4542 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4544 if (dpyinfo->reference_count > 0)
4545 error ("Display still has frames on it");
4547 x_delete_terminal (dpyinfo->terminal);
4549 return Qnil;
4552 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
4553 doc: /* Return the list of display names that Emacs has connections to. */)
4554 (void)
4556 Lisp_Object tail, result;
4558 result = Qnil;
4559 for (tail = x_display_name_list; CONSP (tail); tail = XCDR (tail))
4560 result = Fcons (XCAR (XCAR (tail)), result);
4562 return result;
4565 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
4566 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
4567 This function only has an effect on X Windows. With MS Windows, it is
4568 defined but does nothing.
4570 If ON is nil, allow buffering of requests.
4571 Turning on synchronization prohibits the Xlib routines from buffering
4572 requests and seriously degrades performance, but makes debugging much
4573 easier.
4574 The optional second argument TERMINAL specifies which display to act on.
4575 TERMINAL should be a terminal object, a frame or a display name (a string).
4576 If TERMINAL is omitted or nil, that stands for the selected frame's display. */)
4577 (Lisp_Object on, Lisp_Object terminal)
4579 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4581 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
4583 return Qnil;
4586 /* Wait for responses to all X commands issued so far for frame F. */
4588 void
4589 x_sync (FRAME_PTR f)
4591 block_input ();
4592 XSync (FRAME_X_DISPLAY (f), False);
4593 unblock_input ();
4597 /***********************************************************************
4598 Window properties
4599 ***********************************************************************/
4601 DEFUN ("x-change-window-property", Fx_change_window_property,
4602 Sx_change_window_property, 2, 6, 0,
4603 doc: /* Change window property PROP to VALUE on the X window of FRAME.
4604 PROP must be a string. VALUE may be a string or a list of conses,
4605 numbers and/or strings. If an element in the list is a string, it is
4606 converted to an atom and the value of the atom is used. If an element
4607 is a cons, it is converted to a 32 bit number where the car is the 16
4608 top bits and the cdr is the lower 16 bits.
4610 FRAME nil or omitted means use the selected frame.
4611 If TYPE is given and non-nil, it is the name of the type of VALUE.
4612 If TYPE is not given or nil, the type is STRING.
4613 FORMAT gives the size in bits of each element if VALUE is a list.
4614 It must be one of 8, 16 or 32.
4615 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
4616 If OUTER-P is non-nil, the property is changed for the outer X window of
4617 FRAME. Default is to change on the edit X window. */)
4618 (Lisp_Object prop, Lisp_Object value, Lisp_Object frame,
4619 Lisp_Object type, Lisp_Object format, Lisp_Object outer_p)
4621 struct frame *f = decode_window_system_frame (frame);
4622 Atom prop_atom;
4623 Atom target_type = XA_STRING;
4624 int element_format = 8;
4625 unsigned char *data;
4626 int nelements;
4627 Window w;
4629 CHECK_STRING (prop);
4631 if (! NILP (format))
4633 CHECK_NUMBER (format);
4635 if (XINT (format) != 8 && XINT (format) != 16
4636 && XINT (format) != 32)
4637 error ("FORMAT must be one of 8, 16 or 32");
4638 element_format = XINT (format);
4641 if (CONSP (value))
4643 ptrdiff_t elsize;
4645 nelements = x_check_property_data (value);
4646 if (nelements == -1)
4647 error ("Bad data in VALUE, must be number, string or cons");
4649 /* The man page for XChangeProperty:
4650 "If the specified format is 32, the property data must be a
4651 long array."
4652 This applies even if long is more than 32 bits. The X library
4653 converts to 32 bits before sending to the X server. */
4654 elsize = element_format == 32 ? sizeof (long) : element_format >> 3;
4655 data = xnmalloc (nelements, elsize);
4657 x_fill_property_data (FRAME_X_DISPLAY (f), value, data, element_format);
4659 else
4661 CHECK_STRING (value);
4662 data = SDATA (value);
4663 if (INT_MAX < SBYTES (value))
4664 error ("VALUE too long");
4665 nelements = SBYTES (value);
4668 block_input ();
4669 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4670 if (! NILP (type))
4672 CHECK_STRING (type);
4673 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4676 if (! NILP (outer_p)) w = FRAME_OUTER_WINDOW (f);
4677 else w = FRAME_X_WINDOW (f);
4679 XChangeProperty (FRAME_X_DISPLAY (f), w,
4680 prop_atom, target_type, element_format, PropModeReplace,
4681 data, nelements);
4683 if (CONSP (value)) xfree (data);
4685 /* Make sure the property is set when we return. */
4686 XFlush (FRAME_X_DISPLAY (f));
4687 unblock_input ();
4689 return value;
4693 DEFUN ("x-delete-window-property", Fx_delete_window_property,
4694 Sx_delete_window_property, 1, 2, 0,
4695 doc: /* Remove window property PROP from X window of FRAME.
4696 FRAME nil or omitted means use the selected frame. Value is PROP. */)
4697 (Lisp_Object prop, Lisp_Object frame)
4699 struct frame *f = decode_window_system_frame (frame);
4700 Atom prop_atom;
4702 CHECK_STRING (prop);
4703 block_input ();
4704 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4705 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
4707 /* Make sure the property is removed when we return. */
4708 XFlush (FRAME_X_DISPLAY (f));
4709 unblock_input ();
4711 return prop;
4715 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
4716 1, 6, 0,
4717 doc: /* Value is the value of window property PROP on FRAME.
4718 If FRAME is nil or omitted, use the selected frame.
4720 On X Windows, the following optional arguments are also accepted:
4721 If TYPE is nil or omitted, get the property as a string.
4722 Otherwise TYPE is the name of the atom that denotes the type expected.
4723 If SOURCE is non-nil, get the property on that window instead of from
4724 FRAME. The number 0 denotes the root window.
4725 If DELETE-P is non-nil, delete the property after retrieving it.
4726 If VECTOR-RET-P is non-nil, don't return a string but a vector of values.
4728 On MS Windows, this function accepts but ignores those optional arguments.
4730 Value is nil if FRAME hasn't a property with name PROP or if PROP has
4731 no value of TYPE (always string in the MS Windows case). */)
4732 (Lisp_Object prop, Lisp_Object frame, Lisp_Object type,
4733 Lisp_Object source, Lisp_Object delete_p, Lisp_Object vector_ret_p)
4735 struct frame *f = decode_window_system_frame (frame);
4736 Atom prop_atom;
4737 int rc;
4738 Lisp_Object prop_value = Qnil;
4739 unsigned char *tmp_data = NULL;
4740 Atom actual_type;
4741 Atom target_type = XA_STRING;
4742 int actual_format;
4743 unsigned long actual_size, bytes_remaining;
4744 Window target_window = FRAME_X_WINDOW (f);
4745 struct gcpro gcpro1;
4747 GCPRO1 (prop_value);
4748 CHECK_STRING (prop);
4750 if (! NILP (source))
4752 CONS_TO_INTEGER (source, Window, target_window);
4753 if (! target_window)
4754 target_window = FRAME_X_DISPLAY_INFO (f)->root_window;
4757 block_input ();
4758 if (STRINGP (type))
4760 if (strcmp ("AnyPropertyType", SSDATA (type)) == 0)
4761 target_type = AnyPropertyType;
4762 else
4763 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4766 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4767 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4768 prop_atom, 0, 0, False, target_type,
4769 &actual_type, &actual_format, &actual_size,
4770 &bytes_remaining, &tmp_data);
4771 if (rc == Success)
4773 int size = bytes_remaining;
4775 XFree (tmp_data);
4776 tmp_data = NULL;
4778 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4779 prop_atom, 0, bytes_remaining,
4780 ! NILP (delete_p), target_type,
4781 &actual_type, &actual_format,
4782 &actual_size, &bytes_remaining,
4783 &tmp_data);
4784 if (rc == Success && tmp_data)
4786 /* The man page for XGetWindowProperty says:
4787 "If the returned format is 32, the returned data is represented
4788 as a long array and should be cast to that type to obtain the
4789 elements."
4790 This applies even if long is more than 32 bits, the X library
4791 converts from 32 bit elements received from the X server to long
4792 and passes the long array to us. Thus, for that case memcpy can not
4793 be used. We convert to a 32 bit type here, because so much code
4794 assume on that.
4796 The bytes and offsets passed to XGetWindowProperty refers to the
4797 property and those are indeed in 32 bit quantities if format is
4798 32. */
4800 if (BITS_PER_LONG > 32 && actual_format == 32)
4802 unsigned long i;
4803 int *idata = (int *) tmp_data;
4804 long *ldata = (long *) tmp_data;
4806 for (i = 0; i < actual_size; ++i)
4807 idata[i] = (int) ldata[i];
4810 if (NILP (vector_ret_p))
4811 prop_value = make_string ((char *) tmp_data, size);
4812 else
4813 prop_value = x_property_data_to_lisp (f,
4814 tmp_data,
4815 actual_type,
4816 actual_format,
4817 actual_size);
4820 if (tmp_data) XFree (tmp_data);
4823 unblock_input ();
4824 UNGCPRO;
4825 return prop_value;
4830 /***********************************************************************
4831 Busy cursor
4832 ***********************************************************************/
4834 /* Timer function of hourglass_atimer. TIMER is equal to
4835 hourglass_atimer.
4837 Display an hourglass pointer on all frames by mapping the frames'
4838 hourglass_window. Set the hourglass_p flag in the frames'
4839 output_data.x structure to indicate that an hourglass cursor is
4840 shown on the frames. */
4842 void
4843 show_hourglass (struct atimer *timer)
4845 /* The timer implementation will cancel this timer automatically
4846 after this function has run. Set hourglass_atimer to null
4847 so that we know the timer doesn't have to be canceled. */
4848 hourglass_atimer = NULL;
4850 if (!hourglass_shown_p)
4852 Lisp_Object rest, frame;
4854 block_input ();
4856 FOR_EACH_FRAME (rest, frame)
4858 struct frame *f = XFRAME (frame);
4860 if (FRAME_LIVE_P (f) && FRAME_X_P (f) && FRAME_X_DISPLAY (f))
4862 Display *dpy = FRAME_X_DISPLAY (f);
4864 #ifdef USE_X_TOOLKIT
4865 if (f->output_data.x->widget)
4866 #else
4867 if (FRAME_OUTER_WINDOW (f))
4868 #endif
4870 f->output_data.x->hourglass_p = 1;
4872 if (!f->output_data.x->hourglass_window)
4874 unsigned long mask = CWCursor;
4875 XSetWindowAttributes attrs;
4876 #ifdef USE_GTK
4877 Window parent = FRAME_X_WINDOW (f);
4878 #else
4879 Window parent = FRAME_OUTER_WINDOW (f);
4880 #endif
4881 attrs.cursor = f->output_data.x->hourglass_cursor;
4883 f->output_data.x->hourglass_window
4884 = XCreateWindow (dpy, parent,
4885 0, 0, 32000, 32000, 0, 0,
4886 InputOnly,
4887 CopyFromParent,
4888 mask, &attrs);
4891 XMapRaised (dpy, f->output_data.x->hourglass_window);
4892 XFlush (dpy);
4897 hourglass_shown_p = 1;
4898 unblock_input ();
4903 /* Hide the hourglass pointer on all frames, if it is currently
4904 shown. */
4906 void
4907 hide_hourglass (void)
4909 if (hourglass_shown_p)
4911 Lisp_Object rest, frame;
4913 block_input ();
4914 FOR_EACH_FRAME (rest, frame)
4916 struct frame *f = XFRAME (frame);
4918 if (FRAME_X_P (f)
4919 /* Watch out for newly created frames. */
4920 && f->output_data.x->hourglass_window)
4922 XUnmapWindow (FRAME_X_DISPLAY (f),
4923 f->output_data.x->hourglass_window);
4924 /* Sync here because XTread_socket looks at the
4925 hourglass_p flag that is reset to zero below. */
4926 XSync (FRAME_X_DISPLAY (f), False);
4927 f->output_data.x->hourglass_p = 0;
4931 hourglass_shown_p = 0;
4932 unblock_input ();
4938 /***********************************************************************
4939 Tool tips
4940 ***********************************************************************/
4942 static Lisp_Object x_create_tip_frame (struct x_display_info *,
4943 Lisp_Object, Lisp_Object);
4944 static void compute_tip_xy (struct frame *, Lisp_Object, Lisp_Object,
4945 Lisp_Object, int, int, int *, int *);
4947 /* The frame of a currently visible tooltip. */
4949 Lisp_Object tip_frame;
4951 /* If non-nil, a timer started that hides the last tooltip when it
4952 fires. */
4954 static Lisp_Object tip_timer;
4955 Window tip_window;
4957 /* If non-nil, a vector of 3 elements containing the last args
4958 with which x-show-tip was called. See there. */
4960 static Lisp_Object last_show_tip_args;
4963 static void
4964 unwind_create_tip_frame (Lisp_Object frame)
4966 Lisp_Object deleted;
4968 deleted = unwind_create_frame (frame);
4969 if (EQ (deleted, Qt))
4971 tip_window = None;
4972 tip_frame = Qnil;
4977 /* Create a frame for a tooltip on the display described by DPYINFO.
4978 PARMS is a list of frame parameters. TEXT is the string to
4979 display in the tip frame. Value is the frame.
4981 Note that functions called here, esp. x_default_parameter can
4982 signal errors, for instance when a specified color name is
4983 undefined. We have to make sure that we're in a consistent state
4984 when this happens. */
4986 static Lisp_Object
4987 x_create_tip_frame (struct x_display_info *dpyinfo,
4988 Lisp_Object parms,
4989 Lisp_Object text)
4991 struct frame *f;
4992 Lisp_Object frame;
4993 Lisp_Object name;
4994 int width, height;
4995 ptrdiff_t count = SPECPDL_INDEX ();
4996 struct gcpro gcpro1, gcpro2, gcpro3;
4997 int face_change_count_before = face_change_count;
4998 Lisp_Object buffer;
4999 struct buffer *old_buffer;
5001 if (!dpyinfo->terminal->name)
5002 error ("Terminal is not live, can't create new frames on it");
5004 parms = Fcopy_alist (parms);
5006 /* Get the name of the frame to use for resource lookup. */
5007 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
5008 if (!STRINGP (name)
5009 && !EQ (name, Qunbound)
5010 && !NILP (name))
5011 error ("Invalid frame name--not a string or nil");
5013 frame = Qnil;
5014 GCPRO3 (parms, name, frame);
5015 f = make_frame (1);
5016 XSETFRAME (frame, f);
5018 buffer = Fget_buffer_create (build_string (" *tip*"));
5019 /* Use set_window_buffer instead of Fset_window_buffer (see
5020 discussion of bug#11984, bug#12025, bug#12026). */
5021 set_window_buffer (FRAME_ROOT_WINDOW (f), buffer, 0, 0);
5022 old_buffer = current_buffer;
5023 set_buffer_internal_1 (XBUFFER (buffer));
5024 bset_truncate_lines (current_buffer, Qnil);
5025 specbind (Qinhibit_read_only, Qt);
5026 specbind (Qinhibit_modification_hooks, Qt);
5027 Ferase_buffer ();
5028 Finsert (1, &text);
5029 set_buffer_internal_1 (old_buffer);
5031 record_unwind_protect (unwind_create_tip_frame, frame);
5033 f->terminal = dpyinfo->terminal;
5035 /* By setting the output method, we're essentially saying that
5036 the frame is live, as per FRAME_LIVE_P. If we get a signal
5037 from this point on, x_destroy_window might screw up reference
5038 counts etc. */
5039 f->output_method = output_x_window;
5040 f->output_data.x = xzalloc (sizeof *f->output_data.x);
5041 f->output_data.x->icon_bitmap = -1;
5042 FRAME_FONTSET (f) = -1;
5043 f->output_data.x->scroll_bar_foreground_pixel = -1;
5044 f->output_data.x->scroll_bar_background_pixel = -1;
5045 #ifdef USE_TOOLKIT_SCROLL_BARS
5046 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
5047 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
5048 #endif /* USE_TOOLKIT_SCROLL_BARS */
5049 fset_icon_name (f, Qnil);
5050 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
5051 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
5052 f->output_data.x->explicit_parent = 0;
5054 /* These colors will be set anyway later, but it's important
5055 to get the color reference counts right, so initialize them! */
5057 Lisp_Object black;
5058 struct gcpro gcpro1;
5060 /* Function x_decode_color can signal an error. Make
5061 sure to initialize color slots so that we won't try
5062 to free colors we haven't allocated. */
5063 FRAME_FOREGROUND_PIXEL (f) = -1;
5064 FRAME_BACKGROUND_PIXEL (f) = -1;
5065 f->output_data.x->cursor_pixel = -1;
5066 f->output_data.x->cursor_foreground_pixel = -1;
5067 f->output_data.x->border_pixel = -1;
5068 f->output_data.x->mouse_pixel = -1;
5070 black = build_string ("black");
5071 GCPRO1 (black);
5072 FRAME_FOREGROUND_PIXEL (f)
5073 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5074 FRAME_BACKGROUND_PIXEL (f)
5075 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5076 f->output_data.x->cursor_pixel
5077 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5078 f->output_data.x->cursor_foreground_pixel
5079 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5080 f->output_data.x->border_pixel
5081 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5082 f->output_data.x->mouse_pixel
5083 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5084 UNGCPRO;
5087 /* Set the name; the functions to which we pass f expect the name to
5088 be set. */
5089 if (EQ (name, Qunbound) || NILP (name))
5091 fset_name (f, build_string (dpyinfo->x_id_name));
5092 f->explicit_name = 0;
5094 else
5096 fset_name (f, name);
5097 f->explicit_name = 1;
5098 /* use the frame's title when getting resources for this frame. */
5099 specbind (Qx_resource_name, name);
5102 register_font_driver (&xfont_driver, f);
5103 #ifdef HAVE_FREETYPE
5104 #ifdef HAVE_XFT
5105 register_font_driver (&xftfont_driver, f);
5106 #else /* not HAVE_XFT */
5107 register_font_driver (&ftxfont_driver, f);
5108 #endif /* not HAVE_XFT */
5109 #endif /* HAVE_FREETYPE */
5111 x_default_parameter (f, parms, Qfont_backend, Qnil,
5112 "fontBackend", "FontBackend", RES_TYPE_STRING);
5114 /* Extract the window parameters from the supplied values that are
5115 needed to determine window geometry. */
5116 x_default_font_parameter (f, parms);
5118 x_default_parameter (f, parms, Qborder_width, make_number (0),
5119 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
5121 /* This defaults to 2 in order to match xterm. We recognize either
5122 internalBorderWidth or internalBorder (which is what xterm calls
5123 it). */
5124 if (NILP (Fassq (Qinternal_border_width, parms)))
5126 Lisp_Object value;
5128 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
5129 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
5130 if (! EQ (value, Qunbound))
5131 parms = Fcons (Fcons (Qinternal_border_width, value),
5132 parms);
5135 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
5136 "internalBorderWidth", "internalBorderWidth",
5137 RES_TYPE_NUMBER);
5139 /* Also do the stuff which must be set before the window exists. */
5140 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
5141 "foreground", "Foreground", RES_TYPE_STRING);
5142 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
5143 "background", "Background", RES_TYPE_STRING);
5144 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
5145 "pointerColor", "Foreground", RES_TYPE_STRING);
5146 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
5147 "cursorColor", "Foreground", RES_TYPE_STRING);
5148 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
5149 "borderColor", "BorderColor", RES_TYPE_STRING);
5151 #ifdef GLYPH_DEBUG
5152 image_cache_refcount =
5153 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
5154 dpyinfo_refcount = dpyinfo->reference_count;
5155 #endif /* GLYPH_DEBUG */
5157 /* Init faces before x_default_parameter is called for scroll-bar
5158 parameters because that function calls x_set_scroll_bar_width,
5159 which calls change_frame_size, which calls Fset_window_buffer,
5160 which runs hooks, which call Fvertical_motion. At the end, we
5161 end up in init_iterator with a null face cache, which should not
5162 happen. */
5163 init_frame_faces (f);
5165 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
5167 x_figure_window_size (f, parms, 0);
5170 XSetWindowAttributes attrs;
5171 unsigned long mask;
5172 Atom type = FRAME_X_DISPLAY_INFO (f)->Xatom_net_window_type_tooltip;
5174 block_input ();
5175 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
5176 if (DoesSaveUnders (dpyinfo->screen))
5177 mask |= CWSaveUnder;
5179 /* Window managers look at the override-redirect flag to determine
5180 whether or net to give windows a decoration (Xlib spec, chapter
5181 3.2.8). */
5182 attrs.override_redirect = True;
5183 attrs.save_under = True;
5184 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
5185 /* Arrange for getting MapNotify and UnmapNotify events. */
5186 attrs.event_mask = StructureNotifyMask;
5187 tip_window
5188 = FRAME_X_WINDOW (f)
5189 = XCreateWindow (FRAME_X_DISPLAY (f),
5190 FRAME_X_DISPLAY_INFO (f)->root_window,
5191 /* x, y, width, height */
5192 0, 0, 1, 1,
5193 /* Border. */
5194 f->border_width,
5195 CopyFromParent, InputOutput, CopyFromParent,
5196 mask, &attrs);
5197 XChangeProperty (FRAME_X_DISPLAY (f), tip_window,
5198 FRAME_X_DISPLAY_INFO (f)->Xatom_net_window_type,
5199 XA_ATOM, 32, PropModeReplace,
5200 (unsigned char *)&type, 1);
5201 unblock_input ();
5204 x_make_gc (f);
5206 x_default_parameter (f, parms, Qauto_raise, Qnil,
5207 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5208 x_default_parameter (f, parms, Qauto_lower, Qnil,
5209 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5210 x_default_parameter (f, parms, Qcursor_type, Qbox,
5211 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5213 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
5214 Change will not be effected unless different from the current
5215 FRAME_LINES (f). */
5216 width = FRAME_COLS (f);
5217 height = FRAME_LINES (f);
5218 SET_FRAME_COLS (f, 0);
5219 FRAME_LINES (f) = 0;
5220 change_frame_size (f, height, width, 1, 0, 0);
5222 /* Add `tooltip' frame parameter's default value. */
5223 if (NILP (Fframe_parameter (frame, Qtooltip)))
5224 Fmodify_frame_parameters (frame, list1 (Fcons (Qtooltip, Qt)));
5226 /* FIXME - can this be done in a similar way to normal frames?
5227 http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00641.html */
5229 /* Set the `display-type' frame parameter before setting up faces. */
5231 Lisp_Object disptype;
5233 if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
5234 disptype = intern ("mono");
5235 else if (FRAME_X_DISPLAY_INFO (f)->visual->class == GrayScale
5236 || FRAME_X_DISPLAY_INFO (f)->visual->class == StaticGray)
5237 disptype = intern ("grayscale");
5238 else
5239 disptype = intern ("color");
5241 if (NILP (Fframe_parameter (frame, Qdisplay_type)))
5242 Fmodify_frame_parameters (frame, list1 (Fcons (Qdisplay_type, disptype)));
5245 /* Set up faces after all frame parameters are known. This call
5246 also merges in face attributes specified for new frames.
5248 Frame parameters may be changed if .Xdefaults contains
5249 specifications for the default font. For example, if there is an
5250 `Emacs.default.attributeBackground: pink', the `background-color'
5251 attribute of the frame get's set, which let's the internal border
5252 of the tooltip frame appear in pink. Prevent this. */
5254 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
5256 /* Set tip_frame here, so that */
5257 tip_frame = frame;
5258 call2 (Qface_set_after_frame_default, frame, Qnil);
5260 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
5261 Fmodify_frame_parameters (frame, list1 (Fcons (Qbackground_color, bg)));
5264 f->no_split = 1;
5266 UNGCPRO;
5268 /* Now that the frame will be official, it counts as a reference to
5269 its display and terminal. */
5270 FRAME_X_DISPLAY_INFO (f)->reference_count++;
5271 f->terminal->reference_count++;
5273 /* It is now ok to make the frame official even if we get an error
5274 below. And the frame needs to be on Vframe_list or making it
5275 visible won't work. */
5276 Vframe_list = Fcons (frame, Vframe_list);
5279 /* Setting attributes of faces of the tooltip frame from resources
5280 and similar will increment face_change_count, which leads to the
5281 clearing of all current matrices. Since this isn't necessary
5282 here, avoid it by resetting face_change_count to the value it
5283 had before we created the tip frame. */
5284 face_change_count = face_change_count_before;
5286 /* Discard the unwind_protect. */
5287 return unbind_to (count, frame);
5291 /* Compute where to display tip frame F. PARMS is the list of frame
5292 parameters for F. DX and DY are specified offsets from the current
5293 location of the mouse. WIDTH and HEIGHT are the width and height
5294 of the tooltip. Return coordinates relative to the root window of
5295 the display in *ROOT_X, and *ROOT_Y. */
5297 static void
5298 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)
5300 Lisp_Object left, top;
5301 int win_x, win_y;
5302 Window root, child;
5303 unsigned pmask;
5305 /* User-specified position? */
5306 left = Fcdr (Fassq (Qleft, parms));
5307 top = Fcdr (Fassq (Qtop, parms));
5309 /* Move the tooltip window where the mouse pointer is. Resize and
5310 show it. */
5311 if (!INTEGERP (left) || !INTEGERP (top))
5313 block_input ();
5314 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
5315 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
5316 unblock_input ();
5319 if (INTEGERP (top))
5320 *root_y = XINT (top);
5321 else if (*root_y + XINT (dy) <= 0)
5322 *root_y = 0; /* Can happen for negative dy */
5323 else if (*root_y + XINT (dy) + height
5324 <= x_display_pixel_height (FRAME_X_DISPLAY_INFO (f)))
5325 /* It fits below the pointer */
5326 *root_y += XINT (dy);
5327 else if (height + XINT (dy) <= *root_y)
5328 /* It fits above the pointer. */
5329 *root_y -= height + XINT (dy);
5330 else
5331 /* Put it on the top. */
5332 *root_y = 0;
5334 if (INTEGERP (left))
5335 *root_x = XINT (left);
5336 else if (*root_x + XINT (dx) <= 0)
5337 *root_x = 0; /* Can happen for negative dx */
5338 else if (*root_x + XINT (dx) + width
5339 <= x_display_pixel_width (FRAME_X_DISPLAY_INFO (f)))
5340 /* It fits to the right of the pointer. */
5341 *root_x += XINT (dx);
5342 else if (width + XINT (dx) <= *root_x)
5343 /* It fits to the left of the pointer. */
5344 *root_x -= width + XINT (dx);
5345 else
5346 /* Put it left-justified on the screen--it ought to fit that way. */
5347 *root_x = 0;
5351 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
5352 doc: /* Show STRING in a "tooltip" window on frame FRAME.
5353 A tooltip window is a small X window displaying a string.
5355 This is an internal function; Lisp code should call `tooltip-show'.
5357 FRAME nil or omitted means use the selected frame.
5359 PARMS is an optional list of frame parameters which can be used to
5360 change the tooltip's appearance.
5362 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
5363 means use the default timeout of 5 seconds.
5365 If the list of frame parameters PARMS contains a `left' parameters,
5366 the tooltip is displayed at that x-position. Otherwise it is
5367 displayed at the mouse position, with offset DX added (default is 5 if
5368 DX isn't specified). Likewise for the y-position; if a `top' frame
5369 parameter is specified, it determines the y-position of the tooltip
5370 window, otherwise it is displayed at the mouse position, with offset
5371 DY added (default is -10).
5373 A tooltip's maximum size is specified by `x-max-tooltip-size'.
5374 Text larger than the specified size is clipped. */)
5375 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
5377 struct frame *f;
5378 struct window *w;
5379 int root_x, root_y;
5380 struct buffer *old_buffer;
5381 struct text_pos pos;
5382 int i, width, height, seen_reversed_p;
5383 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5384 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5385 ptrdiff_t count = SPECPDL_INDEX ();
5387 specbind (Qinhibit_redisplay, Qt);
5389 GCPRO4 (string, parms, frame, timeout);
5391 CHECK_STRING (string);
5392 if (SCHARS (string) == 0)
5393 string = make_unibyte_string (" ", 1);
5395 f = decode_window_system_frame (frame);
5396 if (NILP (timeout))
5397 timeout = make_number (5);
5398 else
5399 CHECK_NATNUM (timeout);
5401 if (NILP (dx))
5402 dx = make_number (5);
5403 else
5404 CHECK_NUMBER (dx);
5406 if (NILP (dy))
5407 dy = make_number (-10);
5408 else
5409 CHECK_NUMBER (dy);
5411 #ifdef USE_GTK
5412 if (x_gtk_use_system_tooltips)
5414 bool ok;
5416 /* Hide a previous tip, if any. */
5417 Fx_hide_tip ();
5419 block_input ();
5420 ok = xg_prepare_tooltip (f, string, &width, &height);
5421 if (ok)
5423 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5424 xg_show_tooltip (f, root_x, root_y);
5425 /* This is used in Fx_hide_tip. */
5426 XSETFRAME (tip_frame, f);
5428 unblock_input ();
5429 if (ok) goto start_timer;
5431 #endif /* USE_GTK */
5433 if (NILP (last_show_tip_args))
5434 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5436 if (!NILP (tip_frame))
5438 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5439 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5440 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5442 if (EQ (frame, last_frame)
5443 && !NILP (Fequal (last_string, string))
5444 && !NILP (Fequal (last_parms, parms)))
5446 struct frame *tip_f = XFRAME (tip_frame);
5448 /* Only DX and DY have changed. */
5449 if (!NILP (tip_timer))
5451 Lisp_Object timer = tip_timer;
5452 tip_timer = Qnil;
5453 call1 (Qcancel_timer, timer);
5456 block_input ();
5457 compute_tip_xy (tip_f, parms, dx, dy, FRAME_PIXEL_WIDTH (tip_f),
5458 FRAME_PIXEL_HEIGHT (tip_f), &root_x, &root_y);
5459 XMoveWindow (FRAME_X_DISPLAY (tip_f), FRAME_X_WINDOW (tip_f),
5460 root_x, root_y);
5461 unblock_input ();
5462 goto start_timer;
5466 /* Hide a previous tip, if any. */
5467 Fx_hide_tip ();
5469 ASET (last_show_tip_args, 0, string);
5470 ASET (last_show_tip_args, 1, frame);
5471 ASET (last_show_tip_args, 2, parms);
5473 /* Add default values to frame parameters. */
5474 if (NILP (Fassq (Qname, parms)))
5475 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5476 if (NILP (Fassq (Qinternal_border_width, parms)))
5477 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5478 if (NILP (Fassq (Qborder_width, parms)))
5479 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5480 if (NILP (Fassq (Qborder_color, parms)))
5481 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5482 if (NILP (Fassq (Qbackground_color, parms)))
5483 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5484 parms);
5486 /* Create a frame for the tooltip, and record it in the global
5487 variable tip_frame. */
5488 frame = x_create_tip_frame (FRAME_X_DISPLAY_INFO (f), parms, string);
5489 f = XFRAME (frame);
5491 /* Set up the frame's root window. */
5492 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5493 w->left_col = 0;
5494 w->top_line = 0;
5496 if (CONSP (Vx_max_tooltip_size)
5497 && RANGED_INTEGERP (1, XCAR (Vx_max_tooltip_size), INT_MAX)
5498 && RANGED_INTEGERP (1, XCDR (Vx_max_tooltip_size), INT_MAX))
5500 w->total_cols = XFASTINT (XCAR (Vx_max_tooltip_size));
5501 w->total_lines = XFASTINT (XCDR (Vx_max_tooltip_size));
5503 else
5505 w->total_cols = 80;
5506 w->total_lines = 40;
5509 FRAME_TOTAL_COLS (f) = w->total_cols;
5510 adjust_glyphs (f);
5511 w->pseudo_window_p = 1;
5513 /* Display the tooltip text in a temporary buffer. */
5514 old_buffer = current_buffer;
5515 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->contents));
5516 bset_truncate_lines (current_buffer, Qnil);
5517 clear_glyph_matrix (w->desired_matrix);
5518 clear_glyph_matrix (w->current_matrix);
5519 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5520 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
5522 /* Compute width and height of the tooltip. */
5523 width = height = seen_reversed_p = 0;
5524 for (i = 0; i < w->desired_matrix->nrows; ++i)
5526 struct glyph_row *row = &w->desired_matrix->rows[i];
5527 struct glyph *last;
5528 int row_width;
5530 /* Stop at the first empty row at the end. */
5531 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5532 break;
5534 /* Let the row go over the full width of the frame. */
5535 row->full_width_p = 1;
5537 row_width = row->pixel_width;
5538 if (row->used[TEXT_AREA])
5540 /* There's a glyph at the end of rows that is used to place
5541 the cursor there. Don't include the width of this glyph. */
5542 if (!row->reversed_p)
5544 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5545 if (INTEGERP (last->object))
5546 row_width -= last->pixel_width;
5548 else
5550 /* There could be a stretch glyph at the beginning of R2L
5551 rows that is produced by extend_face_to_end_of_line.
5552 Don't count that glyph. */
5553 struct glyph *g = row->glyphs[TEXT_AREA];
5555 if (g->type == STRETCH_GLYPH && INTEGERP (g->object))
5557 row_width -= g->pixel_width;
5558 seen_reversed_p = 1;
5563 height += row->height;
5564 width = max (width, row_width);
5567 /* If we've seen partial-length R2L rows, we need to re-adjust the
5568 tool-tip frame width and redisplay it again, to avoid over-wide
5569 tips due to the stretch glyph that extends R2L lines to full
5570 width of the frame. */
5571 if (seen_reversed_p)
5573 /* w->total_cols and FRAME_TOTAL_COLS want the width in columns,
5574 not in pixels. */
5575 width /= WINDOW_FRAME_COLUMN_WIDTH (w);
5576 w->total_cols = width;
5577 FRAME_TOTAL_COLS (f) = width;
5578 adjust_glyphs (f);
5579 clear_glyph_matrix (w->desired_matrix);
5580 clear_glyph_matrix (w->current_matrix);
5581 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
5582 width = height = 0;
5583 /* Recompute width and height of the tooltip. */
5584 for (i = 0; i < w->desired_matrix->nrows; ++i)
5586 struct glyph_row *row = &w->desired_matrix->rows[i];
5587 struct glyph *last;
5588 int row_width;
5590 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5591 break;
5592 row->full_width_p = 1;
5593 row_width = row->pixel_width;
5594 if (row->used[TEXT_AREA] && !row->reversed_p)
5596 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5597 if (INTEGERP (last->object))
5598 row_width -= last->pixel_width;
5601 height += row->height;
5602 width = max (width, row_width);
5606 /* Add the frame's internal border to the width and height the X
5607 window should have. */
5608 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5609 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5611 /* Move the tooltip window where the mouse pointer is. Resize and
5612 show it. */
5613 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5615 block_input ();
5616 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5617 root_x, root_y, width, height);
5618 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5619 unblock_input ();
5621 /* Draw into the window. */
5622 w->must_be_updated_p = 1;
5623 update_single_window (w, 1);
5625 /* Restore original current buffer. */
5626 set_buffer_internal_1 (old_buffer);
5627 windows_or_buffers_changed = old_windows_or_buffers_changed;
5629 start_timer:
5630 /* Let the tip disappear after timeout seconds. */
5631 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5632 intern ("x-hide-tip"));
5634 UNGCPRO;
5635 return unbind_to (count, Qnil);
5639 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5640 doc: /* Hide the current tooltip window, if there is any.
5641 Value is t if tooltip was open, nil otherwise. */)
5642 (void)
5644 ptrdiff_t count;
5645 Lisp_Object deleted, frame, timer;
5646 struct gcpro gcpro1, gcpro2;
5648 /* Return quickly if nothing to do. */
5649 if (NILP (tip_timer) && NILP (tip_frame))
5650 return Qnil;
5652 frame = tip_frame;
5653 timer = tip_timer;
5654 GCPRO2 (frame, timer);
5655 tip_frame = tip_timer = deleted = Qnil;
5657 count = SPECPDL_INDEX ();
5658 specbind (Qinhibit_redisplay, Qt);
5659 specbind (Qinhibit_quit, Qt);
5661 if (!NILP (timer))
5662 call1 (Qcancel_timer, timer);
5664 #ifdef USE_GTK
5666 /* When using system tooltip, tip_frame is the Emacs frame on which
5667 the tip is shown. */
5668 struct frame *f = XFRAME (frame);
5669 if (FRAME_LIVE_P (f) && xg_hide_tooltip (f))
5670 frame = Qnil;
5672 #endif
5674 if (FRAMEP (frame))
5676 delete_frame (frame, Qnil);
5677 deleted = Qt;
5679 #ifdef USE_LUCID
5680 /* Bloodcurdling hack alert: The Lucid menu bar widget's
5681 redisplay procedure is not called when a tip frame over menu
5682 items is unmapped. Redisplay the menu manually... */
5684 Widget w;
5685 struct frame *f = SELECTED_FRAME ();
5686 w = f->output_data.x->menubar_widget;
5688 if (!DoesSaveUnders (FRAME_X_DISPLAY_INFO (f)->screen)
5689 && w != NULL)
5691 block_input ();
5692 xlwmenu_redisplay (w);
5693 unblock_input ();
5696 #endif /* USE_LUCID */
5699 UNGCPRO;
5700 return unbind_to (count, deleted);
5705 /***********************************************************************
5706 File selection dialog
5707 ***********************************************************************/
5709 DEFUN ("x-uses-old-gtk-dialog", Fx_uses_old_gtk_dialog,
5710 Sx_uses_old_gtk_dialog,
5711 0, 0, 0,
5712 doc: /* Return t if the old Gtk+ file selection dialog is used. */)
5713 (void)
5715 #ifdef USE_GTK
5716 if (use_dialog_box
5717 && use_file_dialog
5718 && window_system_available (SELECTED_FRAME ())
5719 && xg_uses_old_file_dialog ())
5720 return Qt;
5721 #endif
5722 return Qnil;
5726 #ifdef USE_MOTIF
5727 /* Callback for "OK" and "Cancel" on file selection dialog. */
5729 static void
5730 file_dialog_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5732 int *result = (int *) client_data;
5733 XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call_data;
5734 *result = cb->reason;
5738 /* Callback for unmapping a file selection dialog. This is used to
5739 capture the case where a dialog is closed via a window manager's
5740 closer button, for example. Using a XmNdestroyCallback didn't work
5741 in this case. */
5743 static void
5744 file_dialog_unmap_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5746 int *result = (int *) client_data;
5747 *result = XmCR_CANCEL;
5750 static void
5751 clean_up_file_dialog (void *arg)
5753 Widget dialog = arg;
5755 /* Clean up. */
5756 block_input ();
5757 XtUnmanageChild (dialog);
5758 XtDestroyWidget (dialog);
5759 x_menu_set_in_use (0);
5760 unblock_input ();
5764 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5765 doc: /* Read file name, prompting with PROMPT in directory DIR.
5766 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5767 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5768 or directory must exist.
5770 This function is only defined on NS, MS Windows, and X Windows with the
5771 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5772 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories. */)
5773 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
5775 int result;
5776 struct frame *f = SELECTED_FRAME ();
5777 Lisp_Object file = Qnil;
5778 Lisp_Object decoded_file;
5779 Widget dialog, text, help;
5780 Arg al[10];
5781 int ac = 0;
5782 XmString dir_xmstring, pattern_xmstring;
5783 ptrdiff_t count = SPECPDL_INDEX ();
5784 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5786 check_window_system (f);
5788 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5790 if (popup_activated ())
5791 error ("Trying to use a menu from within a menu-entry");
5793 CHECK_STRING (prompt);
5794 CHECK_STRING (dir);
5796 /* Prevent redisplay. */
5797 specbind (Qinhibit_redisplay, Qt);
5799 block_input ();
5801 /* Create the dialog with PROMPT as title, using DIR as initial
5802 directory and using "*" as pattern. */
5803 dir = Fexpand_file_name (dir, Qnil);
5804 dir_xmstring = XmStringCreateLocalized (SSDATA (dir));
5805 pattern_xmstring = XmStringCreateLocalized ("*");
5807 XtSetArg (al[ac], XmNtitle, SDATA (prompt)); ++ac;
5808 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
5809 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
5810 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
5811 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
5812 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
5813 "fsb", al, ac);
5814 XmStringFree (dir_xmstring);
5815 XmStringFree (pattern_xmstring);
5817 /* Add callbacks for OK and Cancel. */
5818 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
5819 (XtPointer) &result);
5820 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
5821 (XtPointer) &result);
5822 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
5823 (XtPointer) &result);
5825 /* Remove the help button since we can't display help. */
5826 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
5827 XtUnmanageChild (help);
5829 /* Mark OK button as default. */
5830 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
5831 XmNshowAsDefault, True, NULL);
5833 /* If MUSTMATCH is non-nil, disable the file entry field of the
5834 dialog, so that the user must select a file from the files list
5835 box. We can't remove it because we wouldn't have a way to get at
5836 the result file name, then. */
5837 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5838 if (!NILP (mustmatch))
5840 Widget label;
5841 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
5842 XtSetSensitive (text, False);
5843 XtSetSensitive (label, False);
5846 /* Manage the dialog, so that list boxes get filled. */
5847 XtManageChild (dialog);
5849 if (STRINGP (default_filename))
5851 XmString default_xmstring;
5852 Widget wtext = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5853 Widget list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
5855 XmTextPosition last_pos = XmTextFieldGetLastPosition (wtext);
5856 XmTextFieldReplace (wtext, 0, last_pos,
5857 (SSDATA (Ffile_name_nondirectory (default_filename))));
5859 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
5860 must include the path for this to work. */
5862 default_xmstring = XmStringCreateLocalized (SSDATA (default_filename));
5864 if (XmListItemExists (list, default_xmstring))
5866 int item_pos = XmListItemPos (list, default_xmstring);
5867 /* Select the item and scroll it into view. */
5868 XmListSelectPos (list, item_pos, True);
5869 XmListSetPos (list, item_pos);
5872 XmStringFree (default_xmstring);
5875 record_unwind_protect_ptr (clean_up_file_dialog, dialog);
5877 /* Process events until the user presses Cancel or OK. */
5878 x_menu_set_in_use (1);
5879 result = 0;
5880 while (result == 0)
5882 XEvent event;
5883 x_menu_wait_for_event (0);
5884 XtAppNextEvent (Xt_app_con, &event);
5885 if (event.type == KeyPress
5886 && FRAME_X_DISPLAY (f) == event.xkey.display)
5888 KeySym keysym = XLookupKeysym (&event.xkey, 0);
5890 /* Pop down on C-g. */
5891 if (keysym == XK_g && (event.xkey.state & ControlMask) != 0)
5892 XtUnmanageChild (dialog);
5895 (void) x_dispatch_event (&event, FRAME_X_DISPLAY (f));
5898 /* Get the result. */
5899 if (result == XmCR_OK)
5901 XmString text_string;
5902 String data;
5904 XtVaGetValues (dialog, XmNtextString, &text_string, NULL);
5905 XmStringGetLtoR (text_string, XmFONTLIST_DEFAULT_TAG, &data);
5906 XmStringFree (text_string);
5907 file = build_string (data);
5908 XtFree (data);
5910 else
5911 file = Qnil;
5913 unblock_input ();
5914 UNGCPRO;
5916 /* Make "Cancel" equivalent to C-g. */
5917 if (NILP (file))
5918 Fsignal (Qquit, Qnil);
5920 decoded_file = DECODE_FILE (file);
5922 return unbind_to (count, decoded_file);
5925 #endif /* USE_MOTIF */
5927 #ifdef USE_GTK
5929 static void
5930 clean_up_dialog (void)
5932 x_menu_set_in_use (0);
5935 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5936 doc: /* Read file name, prompting with PROMPT in directory DIR.
5937 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5938 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5939 or directory must exist.
5941 This function is only defined on NS, MS Windows, and X Windows with the
5942 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5943 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories. */)
5944 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
5946 FRAME_PTR f = SELECTED_FRAME ();
5947 char *fn;
5948 Lisp_Object file = Qnil;
5949 Lisp_Object decoded_file;
5950 ptrdiff_t count = SPECPDL_INDEX ();
5951 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5952 char *cdef_file;
5954 check_window_system (f);
5956 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5958 if (popup_activated ())
5959 error ("Trying to use a menu from within a menu-entry");
5961 CHECK_STRING (prompt);
5962 CHECK_STRING (dir);
5964 /* Prevent redisplay. */
5965 specbind (Qinhibit_redisplay, Qt);
5966 record_unwind_protect_void (clean_up_dialog);
5968 block_input ();
5970 if (STRINGP (default_filename))
5971 cdef_file = SSDATA (default_filename);
5972 else
5973 cdef_file = SSDATA (dir);
5975 fn = xg_get_file_name (f, SSDATA (prompt), cdef_file,
5976 ! NILP (mustmatch),
5977 ! NILP (only_dir_p));
5979 if (fn)
5981 file = build_string (fn);
5982 xfree (fn);
5985 unblock_input ();
5986 UNGCPRO;
5988 /* Make "Cancel" equivalent to C-g. */
5989 if (NILP (file))
5990 Fsignal (Qquit, Qnil);
5992 decoded_file = DECODE_FILE (file);
5994 return unbind_to (count, decoded_file);
5998 #ifdef HAVE_FREETYPE
6000 DEFUN ("x-select-font", Fx_select_font, Sx_select_font, 0, 2, 0,
6001 doc: /* Read a font using a GTK dialog.
6002 Return either a font spec (for GTK versions >= 3.2) or a string
6003 containing a GTK-style font name.
6005 FRAME is the frame on which to pop up the font chooser. If omitted or
6006 nil, it defaults to the selected frame. */)
6007 (Lisp_Object frame, Lisp_Object ignored)
6009 FRAME_PTR f = decode_window_system_frame (frame);
6010 Lisp_Object font;
6011 Lisp_Object font_param;
6012 char *default_name = NULL;
6013 struct gcpro gcpro1, gcpro2;
6014 ptrdiff_t count = SPECPDL_INDEX ();
6016 if (popup_activated ())
6017 error ("Trying to use a menu from within a menu-entry");
6019 /* Prevent redisplay. */
6020 specbind (Qinhibit_redisplay, Qt);
6021 record_unwind_protect_void (clean_up_dialog);
6023 block_input ();
6025 GCPRO2 (font_param, font);
6027 XSETFONT (font, FRAME_FONT (f));
6028 font_param = Ffont_get (font, intern (":name"));
6029 if (STRINGP (font_param))
6030 default_name = xstrdup (SSDATA (font_param));
6031 else
6033 font_param = Fframe_parameter (frame, Qfont_param);
6034 if (STRINGP (font_param))
6035 default_name = xstrdup (SSDATA (font_param));
6038 font = xg_get_font (f, default_name);
6039 xfree (default_name);
6041 unblock_input ();
6043 if (NILP (font))
6044 Fsignal (Qquit, Qnil);
6046 return unbind_to (count, font);
6048 #endif /* HAVE_FREETYPE */
6050 #endif /* USE_GTK */
6053 /***********************************************************************
6054 Keyboard
6055 ***********************************************************************/
6057 #ifdef HAVE_XKB
6058 #include <X11/XKBlib.h>
6059 #include <X11/keysym.h>
6060 #endif
6062 DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
6063 Sx_backspace_delete_keys_p, 0, 1, 0,
6064 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
6065 FRAME nil means use the selected frame.
6066 Value is t if we know that both keys are present, and are mapped to the
6067 usual X keysyms. Value is `lambda' if we cannot determine if both keys are
6068 present and mapped to the usual X keysyms. */)
6069 (Lisp_Object frame)
6071 #ifndef HAVE_XKB
6072 return Qlambda;
6073 #else
6074 XkbDescPtr kb;
6075 struct frame *f = decode_window_system_frame (frame);
6076 Display *dpy = FRAME_X_DISPLAY (f);
6077 Lisp_Object have_keys;
6078 int major, minor, op, event, error_code;
6080 block_input ();
6082 /* Check library version in case we're dynamically linked. */
6083 major = XkbMajorVersion;
6084 minor = XkbMinorVersion;
6085 if (!XkbLibraryVersion (&major, &minor))
6087 unblock_input ();
6088 return Qlambda;
6091 /* Check that the server supports XKB. */
6092 major = XkbMajorVersion;
6093 minor = XkbMinorVersion;
6094 if (!XkbQueryExtension (dpy, &op, &event, &error_code, &major, &minor))
6096 unblock_input ();
6097 return Qlambda;
6100 /* In this code we check that the keyboard has physical keys with names
6101 that start with BKSP (Backspace) and DELE (Delete), and that they
6102 generate keysym XK_BackSpace and XK_Delete respectively.
6103 This function is used to test if normal-erase-is-backspace should be
6104 turned on.
6105 An alternative approach would be to just check if XK_BackSpace and
6106 XK_Delete are mapped to any key. But if any of those are mapped to
6107 some non-intuitive key combination (Meta-Shift-Ctrl-whatever) and the
6108 user doesn't know about it, it is better to return false here.
6109 It is more obvious to the user what to do if she/he has two keys
6110 clearly marked with names/symbols and one key does something not
6111 expected (i.e. she/he then tries the other).
6112 The cases where Backspace/Delete is mapped to some other key combination
6113 are rare, and in those cases, normal-erase-is-backspace can be turned on
6114 manually. */
6116 have_keys = Qnil;
6117 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
6118 if (kb)
6120 int delete_keycode = 0, backspace_keycode = 0, i;
6122 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
6124 for (i = kb->min_key_code;
6125 (i < kb->max_key_code
6126 && (delete_keycode == 0 || backspace_keycode == 0));
6127 ++i)
6129 /* The XKB symbolic key names can be seen most easily in
6130 the PS file generated by `xkbprint -label name
6131 $DISPLAY'. */
6132 if (memcmp ("DELE", kb->names->keys[i].name, 4) == 0)
6133 delete_keycode = i;
6134 else if (memcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
6135 backspace_keycode = i;
6138 XkbFreeNames (kb, 0, True);
6141 XkbFreeClientMap (kb, 0, True);
6143 if (delete_keycode
6144 && backspace_keycode
6145 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
6146 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
6147 have_keys = Qt;
6149 unblock_input ();
6150 return have_keys;
6151 #endif
6156 /***********************************************************************
6157 Initialization
6158 ***********************************************************************/
6160 /* Keep this list in the same order as frame_parms in frame.c.
6161 Use 0 for unsupported frame parameters. */
6163 frame_parm_handler x_frame_parm_handlers[] =
6165 x_set_autoraise,
6166 x_set_autolower,
6167 x_set_background_color,
6168 x_set_border_color,
6169 x_set_border_width,
6170 x_set_cursor_color,
6171 x_set_cursor_type,
6172 x_set_font,
6173 x_set_foreground_color,
6174 x_set_icon_name,
6175 x_set_icon_type,
6176 x_set_internal_border_width,
6177 x_set_menu_bar_lines,
6178 x_set_mouse_color,
6179 x_explicitly_set_name,
6180 x_set_scroll_bar_width,
6181 x_set_title,
6182 x_set_unsplittable,
6183 x_set_vertical_scroll_bars,
6184 x_set_visibility,
6185 x_set_tool_bar_lines,
6186 x_set_scroll_bar_foreground,
6187 x_set_scroll_bar_background,
6188 x_set_screen_gamma,
6189 x_set_line_spacing,
6190 x_set_fringe_width,
6191 x_set_fringe_width,
6192 x_set_wait_for_wm,
6193 x_set_fullscreen,
6194 x_set_font_backend,
6195 x_set_alpha,
6196 x_set_sticky,
6197 x_set_tool_bar_position,
6200 void
6201 syms_of_xfns (void)
6203 /* The section below is built by the lisp expression at the top of the file,
6204 just above where these variables are declared. */
6205 /*&&& init symbols here &&&*/
6206 DEFSYM (Qsuppress_icon, "suppress-icon");
6207 DEFSYM (Qundefined_color, "undefined-color");
6208 DEFSYM (Qcompound_text, "compound-text");
6209 DEFSYM (Qcancel_timer, "cancel-timer");
6210 DEFSYM (Qfont_param, "font-parameter");
6211 /* This is the end of symbol initialization. */
6213 Fput (Qundefined_color, Qerror_conditions,
6214 listn (CONSTYPE_PURE, 2, Qundefined_color, Qerror));
6215 Fput (Qundefined_color, Qerror_message,
6216 build_pure_c_string ("Undefined color"));
6218 DEFVAR_LISP ("x-pointer-shape", Vx_pointer_shape,
6219 doc: /* The shape of the pointer when over text.
6220 Changing the value does not affect existing frames
6221 unless you set the mouse color. */);
6222 Vx_pointer_shape = Qnil;
6224 #if 0 /* This doesn't really do anything. */
6225 DEFVAR_LISP ("x-nontext-pointer-shape", Vx_nontext_pointer_shape,
6226 doc: /* The shape of the pointer when not over text.
6227 This variable takes effect when you create a new frame
6228 or when you set the mouse color. */);
6229 #endif
6230 Vx_nontext_pointer_shape = Qnil;
6232 DEFVAR_LISP ("x-hourglass-pointer-shape", Vx_hourglass_pointer_shape,
6233 doc: /* The shape of the pointer when Emacs is busy.
6234 This variable takes effect when you create a new frame
6235 or when you set the mouse color. */);
6236 Vx_hourglass_pointer_shape = Qnil;
6238 #if 0 /* This doesn't really do anything. */
6239 DEFVAR_LISP ("x-mode-pointer-shape", Vx_mode_pointer_shape,
6240 doc: /* The shape of the pointer when over the mode line.
6241 This variable takes effect when you create a new frame
6242 or when you set the mouse color. */);
6243 #endif
6244 Vx_mode_pointer_shape = Qnil;
6246 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
6247 Vx_sensitive_text_pointer_shape,
6248 doc: /* The shape of the pointer when over mouse-sensitive text.
6249 This variable takes effect when you create a new frame
6250 or when you set the mouse color. */);
6251 Vx_sensitive_text_pointer_shape = Qnil;
6253 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
6254 Vx_window_horizontal_drag_shape,
6255 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
6256 This variable takes effect when you create a new frame
6257 or when you set the mouse color. */);
6258 Vx_window_horizontal_drag_shape = Qnil;
6260 DEFVAR_LISP ("x-cursor-fore-pixel", Vx_cursor_fore_pixel,
6261 doc: /* A string indicating the foreground color of the cursor box. */);
6262 Vx_cursor_fore_pixel = Qnil;
6264 DEFVAR_LISP ("x-max-tooltip-size", Vx_max_tooltip_size,
6265 doc: /* Maximum size for tooltips.
6266 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
6267 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
6269 DEFVAR_LISP ("x-no-window-manager", Vx_no_window_manager,
6270 doc: /* Non-nil if no X window manager is in use.
6271 Emacs doesn't try to figure this out; this is always nil
6272 unless you set it to something else. */);
6273 /* We don't have any way to find this out, so set it to nil
6274 and maybe the user would like to set it to t. */
6275 Vx_no_window_manager = Qnil;
6277 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
6278 Vx_pixel_size_width_font_regexp,
6279 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
6281 Since Emacs gets width of a font matching with this regexp from
6282 PIXEL_SIZE field of the name, font finding mechanism gets faster for
6283 such a font. This is especially effective for such large fonts as
6284 Chinese, Japanese, and Korean. */);
6285 Vx_pixel_size_width_font_regexp = Qnil;
6287 /* This is not ifdef:ed, so other builds than GTK can customize it. */
6288 DEFVAR_BOOL ("x-gtk-use-old-file-dialog", x_gtk_use_old_file_dialog,
6289 doc: /* Non-nil means prompt with the old GTK file selection dialog.
6290 If nil or if the file selection dialog is not available, the new GTK file
6291 chooser is used instead. To turn off all file dialogs set the
6292 variable `use-file-dialog'. */);
6293 x_gtk_use_old_file_dialog = 0;
6295 DEFVAR_BOOL ("x-gtk-show-hidden-files", x_gtk_show_hidden_files,
6296 doc: /* If non-nil, the GTK file chooser will by default show hidden files.
6297 Note that this is just the default, there is a toggle button on the file
6298 chooser to show or not show hidden files on a case by case basis. */);
6299 x_gtk_show_hidden_files = 0;
6301 DEFVAR_BOOL ("x-gtk-file-dialog-help-text", x_gtk_file_dialog_help_text,
6302 doc: /* If non-nil, the GTK file chooser will show additional help text.
6303 If more space for files in the file chooser dialog is wanted, set this to nil
6304 to turn the additional text off. */);
6305 x_gtk_file_dialog_help_text = 1;
6307 DEFVAR_BOOL ("x-gtk-whole-detached-tool-bar", x_gtk_whole_detached_tool_bar,
6308 doc: /* If non-nil, a detached tool bar is shown in full.
6309 The default is to just show an arrow and pressing on that arrow shows
6310 the tool bar buttons. */);
6311 x_gtk_whole_detached_tool_bar = 0;
6313 DEFVAR_BOOL ("x-gtk-use-system-tooltips", x_gtk_use_system_tooltips,
6314 doc: /* If non-nil with a Gtk+ built Emacs, the Gtk+ tooltip is used.
6315 Otherwise use Emacs own tooltip implementation.
6316 When using Gtk+ tooltips, the tooltip face is not used. */);
6317 x_gtk_use_system_tooltips = 1;
6319 Fprovide (intern_c_string ("x"), Qnil);
6321 #ifdef USE_X_TOOLKIT
6322 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6323 #ifdef USE_MOTIF
6324 Fprovide (intern_c_string ("motif"), Qnil);
6326 DEFVAR_LISP ("motif-version-string", Vmotif_version_string,
6327 doc: /* Version info for LessTif/Motif. */);
6328 Vmotif_version_string = build_string (XmVERSION_STRING);
6329 #endif /* USE_MOTIF */
6330 #endif /* USE_X_TOOLKIT */
6332 #ifdef USE_GTK
6333 /* Provide x-toolkit also for GTK. Internally GTK does not use Xt so it
6334 is not an X toolkit in that sense (USE_X_TOOLKIT is not defined).
6335 But for a user it is a toolkit for X, and indeed, configure
6336 accepts --with-x-toolkit=gtk. */
6337 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6338 Fprovide (intern_c_string ("gtk"), Qnil);
6339 Fprovide (intern_c_string ("move-toolbar"), Qnil);
6341 DEFVAR_LISP ("gtk-version-string", Vgtk_version_string,
6342 doc: /* Version info for GTK+. */);
6344 char gtk_version[sizeof ".." + 3 * INT_STRLEN_BOUND (int)];
6345 int len = sprintf (gtk_version, "%d.%d.%d",
6346 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
6347 Vgtk_version_string = make_pure_string (gtk_version, len, len, 0);
6349 #endif /* USE_GTK */
6351 /* X window properties. */
6352 defsubr (&Sx_change_window_property);
6353 defsubr (&Sx_delete_window_property);
6354 defsubr (&Sx_window_property);
6356 defsubr (&Sxw_display_color_p);
6357 defsubr (&Sx_display_grayscale_p);
6358 defsubr (&Sxw_color_defined_p);
6359 defsubr (&Sxw_color_values);
6360 defsubr (&Sx_server_max_request_size);
6361 defsubr (&Sx_server_vendor);
6362 defsubr (&Sx_server_version);
6363 defsubr (&Sx_display_pixel_width);
6364 defsubr (&Sx_display_pixel_height);
6365 defsubr (&Sx_display_mm_width);
6366 defsubr (&Sx_display_mm_height);
6367 defsubr (&Sx_display_screens);
6368 defsubr (&Sx_display_planes);
6369 defsubr (&Sx_display_color_cells);
6370 defsubr (&Sx_display_visual_class);
6371 defsubr (&Sx_display_backing_store);
6372 defsubr (&Sx_display_save_under);
6373 defsubr (&Sx_display_monitor_attributes_list);
6374 defsubr (&Sx_wm_set_size_hint);
6375 defsubr (&Sx_create_frame);
6376 defsubr (&Sx_open_connection);
6377 defsubr (&Sx_close_connection);
6378 defsubr (&Sx_display_list);
6379 defsubr (&Sx_synchronize);
6380 defsubr (&Sx_focus_frame);
6381 defsubr (&Sx_backspace_delete_keys_p);
6383 defsubr (&Sx_show_tip);
6384 defsubr (&Sx_hide_tip);
6385 tip_timer = Qnil;
6386 staticpro (&tip_timer);
6387 tip_frame = Qnil;
6388 staticpro (&tip_frame);
6390 last_show_tip_args = Qnil;
6391 staticpro (&last_show_tip_args);
6393 defsubr (&Sx_uses_old_gtk_dialog);
6394 #if defined (USE_MOTIF) || defined (USE_GTK)
6395 defsubr (&Sx_file_dialog);
6396 #endif
6398 #if defined (USE_GTK) && defined (HAVE_FREETYPE)
6399 defsubr (&Sx_select_font);
6400 #endif
6403 #endif /* HAVE_X_WINDOWS */