* process.c (allocate_pty) [PTY_OPEN]: Set fd's FD_CLOEXEC flag.
[emacs.git] / src / xfns.c
blob0c91e298ec8068fa188a5135a22256bf8eb35c09
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 struct frame *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 (struct frame *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 (struct frame *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 (struct frame *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 (struct frame *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 (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1055 set_frame_cursor_types (f, arg);
1058 static void
1059 x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1061 int result;
1063 if (STRINGP (arg))
1065 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1066 return;
1068 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
1069 return;
1071 block_input ();
1072 if (NILP (arg))
1073 result = x_text_icon (f,
1074 SSDATA ((!NILP (f->icon_name)
1075 ? f->icon_name
1076 : f->name)));
1077 else
1078 result = x_bitmap_icon (f, arg);
1080 if (result)
1082 unblock_input ();
1083 error ("No icon window available");
1086 XFlush (FRAME_X_DISPLAY (f));
1087 unblock_input ();
1090 static void
1091 x_set_icon_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1093 int result;
1095 if (STRINGP (arg))
1097 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1098 return;
1100 else if (!NILP (arg) || NILP (oldval))
1101 return;
1103 fset_icon_name (f, arg);
1105 if (f->output_data.x->icon_bitmap != 0)
1106 return;
1108 block_input ();
1110 result = x_text_icon (f,
1111 SSDATA ((!NILP (f->icon_name)
1112 ? f->icon_name
1113 : !NILP (f->title)
1114 ? f->title
1115 : f->name)));
1117 if (result)
1119 unblock_input ();
1120 error ("No icon window available");
1123 XFlush (FRAME_X_DISPLAY (f));
1124 unblock_input ();
1128 void
1129 x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1131 int nlines;
1132 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
1133 int olines = FRAME_MENU_BAR_LINES (f);
1134 #endif
1136 /* Right now, menu bars don't work properly in minibuf-only frames;
1137 most of the commands try to apply themselves to the minibuffer
1138 frame itself, and get an error because you can't switch buffers
1139 in or split the minibuffer window. */
1140 if (FRAME_MINIBUF_ONLY_P (f))
1141 return;
1143 if (TYPE_RANGED_INTEGERP (int, value))
1144 nlines = XINT (value);
1145 else
1146 nlines = 0;
1148 /* Make sure we redisplay all windows in this frame. */
1149 windows_or_buffers_changed++;
1151 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1152 FRAME_MENU_BAR_LINES (f) = 0;
1153 if (nlines)
1155 FRAME_EXTERNAL_MENU_BAR (f) = 1;
1156 if (FRAME_X_P (f) && f->output_data.x->menubar_widget == 0)
1157 /* Make sure next redisplay shows the menu bar. */
1158 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = 1;
1160 else
1162 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1163 free_frame_menubar (f);
1164 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1165 if (FRAME_X_P (f))
1166 f->output_data.x->menubar_widget = 0;
1168 #else /* not USE_X_TOOLKIT && not USE_GTK */
1169 FRAME_MENU_BAR_LINES (f) = nlines;
1170 resize_frame_windows (f, FRAME_LINES (f), 0);
1172 /* If the menu bar height gets changed, the internal border below
1173 the top margin has to be cleared. Also, if the menu bar gets
1174 larger, the area for the added lines has to be cleared except for
1175 the first menu bar line that is to be drawn later. */
1176 if (nlines != olines)
1178 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1179 int width = FRAME_PIXEL_WIDTH (f);
1180 int y;
1182 /* height can be zero here. */
1183 if (height > 0 && width > 0)
1185 y = FRAME_TOP_MARGIN_HEIGHT (f);
1187 block_input ();
1188 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1189 0, y, width, height, False);
1190 unblock_input ();
1193 if (nlines > 1 && nlines > olines)
1195 y = (olines == 0 ? 1 : olines) * FRAME_LINE_HEIGHT (f);
1196 height = nlines * FRAME_LINE_HEIGHT (f) - y;
1198 block_input ();
1199 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1200 0, y, width, height, False);
1201 unblock_input ();
1204 if (nlines == 0 && WINDOWP (f->menu_bar_window))
1205 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
1207 #endif /* not USE_X_TOOLKIT && not USE_GTK */
1208 adjust_glyphs (f);
1209 run_window_configuration_change_hook (f);
1213 /* Set the number of lines used for the tool bar of frame F to VALUE.
1214 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1215 is the old number of tool bar lines. This function changes the
1216 height of all windows on frame F to match the new tool bar height.
1217 The frame's height doesn't change. */
1219 void
1220 x_set_tool_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1222 int delta, nlines, root_height;
1223 Lisp_Object root_window;
1225 /* Treat tool bars like menu bars. */
1226 if (FRAME_MINIBUF_ONLY_P (f))
1227 return;
1229 /* Use VALUE only if an int >= 0. */
1230 if (RANGED_INTEGERP (0, value, INT_MAX))
1231 nlines = XFASTINT (value);
1232 else
1233 nlines = 0;
1235 #ifdef USE_GTK
1236 FRAME_TOOL_BAR_LINES (f) = 0;
1237 if (nlines)
1239 FRAME_EXTERNAL_TOOL_BAR (f) = 1;
1240 if (FRAME_X_P (f) && f->output_data.x->toolbar_widget == 0)
1241 /* Make sure next redisplay shows the tool bar. */
1242 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = 1;
1243 update_frame_tool_bar (f);
1245 else
1247 if (FRAME_EXTERNAL_TOOL_BAR (f))
1248 free_frame_tool_bar (f);
1249 FRAME_EXTERNAL_TOOL_BAR (f) = 0;
1252 return;
1253 #endif
1255 /* Make sure we redisplay all windows in this frame. */
1256 ++windows_or_buffers_changed;
1258 delta = nlines - FRAME_TOOL_BAR_LINES (f);
1260 /* Don't resize the tool-bar to more than we have room for. */
1261 root_window = FRAME_ROOT_WINDOW (f);
1262 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
1263 if (root_height - delta < 1)
1265 delta = root_height - 1;
1266 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
1269 FRAME_TOOL_BAR_LINES (f) = nlines;
1270 resize_frame_windows (f, FRAME_LINES (f), 0);
1271 adjust_glyphs (f);
1273 /* We also have to make sure that the internal border at the top of
1274 the frame, below the menu bar or tool bar, is redrawn when the
1275 tool bar disappears. This is so because the internal border is
1276 below the tool bar if one is displayed, but is below the menu bar
1277 if there isn't a tool bar. The tool bar draws into the area
1278 below the menu bar. */
1279 if (FRAME_X_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
1281 clear_frame (f);
1282 clear_current_matrices (f);
1285 /* If the tool bar gets smaller, the internal border below it
1286 has to be cleared. It was formerly part of the display
1287 of the larger tool bar, and updating windows won't clear it. */
1288 if (delta < 0)
1290 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1291 int width = FRAME_PIXEL_WIDTH (f);
1292 int y = (FRAME_MENU_BAR_LINES (f) + nlines) * FRAME_LINE_HEIGHT (f);
1294 /* height can be zero here. */
1295 if (height > 0 && width > 0)
1297 block_input ();
1298 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1299 0, y, width, height, False);
1300 unblock_input ();
1303 if (WINDOWP (f->tool_bar_window))
1304 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1307 run_window_configuration_change_hook (f);
1312 /* Set the foreground color for scroll bars on frame F to VALUE.
1313 VALUE should be a string, a color name. If it isn't a string or
1314 isn't a valid color name, do nothing. OLDVAL is the old value of
1315 the frame parameter. */
1317 static void
1318 x_set_scroll_bar_foreground (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1320 unsigned long pixel;
1322 if (STRINGP (value))
1323 pixel = x_decode_color (f, value, BLACK_PIX_DEFAULT (f));
1324 else
1325 pixel = -1;
1327 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
1328 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
1330 f->output_data.x->scroll_bar_foreground_pixel = pixel;
1331 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1333 /* Remove all scroll bars because they have wrong colors. */
1334 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1335 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1336 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1337 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1339 update_face_from_frame_parameter (f, Qscroll_bar_foreground, value);
1340 redraw_frame (f);
1345 /* Set the background color for scroll bars on frame F to VALUE VALUE
1346 should be a string, a color name. If it isn't a string or isn't a
1347 valid color name, do nothing. OLDVAL is the old value of the frame
1348 parameter. */
1350 static void
1351 x_set_scroll_bar_background (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1353 unsigned long pixel;
1355 if (STRINGP (value))
1356 pixel = x_decode_color (f, value, WHITE_PIX_DEFAULT (f));
1357 else
1358 pixel = -1;
1360 if (f->output_data.x->scroll_bar_background_pixel != -1)
1361 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
1363 #ifdef USE_TOOLKIT_SCROLL_BARS
1364 /* Scrollbar shadow colors. */
1365 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
1367 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
1368 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
1370 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
1372 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
1373 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
1375 #endif /* USE_TOOLKIT_SCROLL_BARS */
1377 f->output_data.x->scroll_bar_background_pixel = pixel;
1378 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1380 /* Remove all scroll bars because they have wrong colors. */
1381 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1382 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1383 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1384 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1386 update_face_from_frame_parameter (f, Qscroll_bar_background, value);
1387 redraw_frame (f);
1392 /* Encode Lisp string STRING as a text in a format appropriate for
1393 XICCC (X Inter Client Communication Conventions).
1395 This can call Lisp code, so callers must GCPRO.
1397 If STRING contains only ASCII characters, do no conversion and
1398 return the string data of STRING. Otherwise, encode the text by
1399 CODING_SYSTEM, and return a newly allocated memory area which
1400 should be freed by `xfree' by a caller.
1402 SELECTIONP non-zero means the string is being encoded for an X
1403 selection, so it is safe to run pre-write conversions (which
1404 may run Lisp code).
1406 Store the byte length of resulting text in *TEXT_BYTES.
1408 If the text contains only ASCII and Latin-1, store 1 in *STRING_P,
1409 which means that the `encoding' of the result can be `STRING'.
1410 Otherwise store 0 in *STRINGP, which means that the `encoding' of
1411 the result should be `COMPOUND_TEXT'. */
1413 static unsigned char *
1414 x_encode_text (Lisp_Object string, Lisp_Object coding_system, int selectionp,
1415 ptrdiff_t *text_bytes, int *stringp, int *freep)
1417 int result = string_xstring_p (string);
1418 struct coding_system coding;
1420 if (result == 0)
1422 /* No multibyte character in OBJ. We need not encode it. */
1423 *text_bytes = SBYTES (string);
1424 *stringp = 1;
1425 *freep = 0;
1426 return SDATA (string);
1429 setup_coding_system (coding_system, &coding);
1430 coding.mode |= (CODING_MODE_SAFE_ENCODING | CODING_MODE_LAST_BLOCK);
1431 /* We suppress producing escape sequences for composition. */
1432 coding.common_flags &= ~CODING_ANNOTATION_MASK;
1433 coding.destination = xnmalloc (SCHARS (string), 2);
1434 coding.dst_bytes = SCHARS (string) * 2;
1435 encode_coding_object (&coding, string, 0, 0,
1436 SCHARS (string), SBYTES (string), Qnil);
1437 *text_bytes = coding.produced;
1438 *stringp = (result == 1 || !EQ (coding_system, Qcompound_text));
1439 *freep = 1;
1440 return coding.destination;
1444 /* Set the WM name to NAME for frame F. Also set the icon name.
1445 If the frame already has an icon name, use that, otherwise set the
1446 icon name to NAME. */
1448 static void
1449 x_set_name_internal (struct frame *f, Lisp_Object name)
1451 if (FRAME_X_WINDOW (f))
1453 block_input ();
1455 XTextProperty text, icon;
1456 ptrdiff_t bytes;
1457 int stringp;
1458 int do_free_icon_value = 0, do_free_text_value = 0;
1459 Lisp_Object coding_system;
1460 Lisp_Object encoded_name;
1461 Lisp_Object encoded_icon_name;
1462 struct gcpro gcpro1;
1464 /* As ENCODE_UTF_8 may cause GC and relocation of string data,
1465 we use it before x_encode_text that may return string data. */
1466 GCPRO1 (name);
1467 encoded_name = ENCODE_UTF_8 (name);
1468 UNGCPRO;
1470 coding_system = Qcompound_text;
1471 /* Note: Encoding strategy
1473 We encode NAME by compound-text and use "COMPOUND-TEXT" in
1474 text.encoding. But, there are non-internationalized window
1475 managers which don't support that encoding. So, if NAME
1476 contains only ASCII and 8859-1 characters, encode it by
1477 iso-latin-1, and use "STRING" in text.encoding hoping that
1478 such window managers at least analyze this format correctly,
1479 i.e. treat 8-bit bytes as 8859-1 characters.
1481 We may also be able to use "UTF8_STRING" in text.encoding
1482 in the future which can encode all Unicode characters.
1483 But, for the moment, there's no way to know that the
1484 current window manager supports it or not.
1486 Either way, we also set the _NET_WM_NAME and _NET_WM_ICON_NAME
1487 properties. Per the EWMH specification, those two properties
1488 are always UTF8_STRING. This matches what gtk_window_set_title()
1489 does in the USE_GTK case. */
1490 text.value = x_encode_text (name, coding_system, 0, &bytes, &stringp,
1491 &do_free_text_value);
1492 text.encoding = (stringp ? XA_STRING
1493 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1494 text.format = 8;
1495 text.nitems = bytes;
1496 if (text.nitems != bytes)
1497 error ("Window name too large");
1499 if (!STRINGP (f->icon_name))
1501 icon = text;
1502 encoded_icon_name = encoded_name;
1504 else
1506 /* See the above comment "Note: Encoding strategy". */
1507 icon.value = x_encode_text (f->icon_name, coding_system, 0,
1508 &bytes, &stringp, &do_free_icon_value);
1509 icon.encoding = (stringp ? XA_STRING
1510 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1511 icon.format = 8;
1512 icon.nitems = bytes;
1513 if (icon.nitems != bytes)
1514 error ("Icon name too large");
1516 encoded_icon_name = ENCODE_UTF_8 (f->icon_name);
1519 #ifdef USE_GTK
1520 gtk_window_set_title (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1521 SSDATA (encoded_name));
1522 #else /* not USE_GTK */
1523 XSetWMName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
1524 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1525 FRAME_X_DISPLAY_INFO (f)->Xatom_net_wm_name,
1526 FRAME_X_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1527 8, PropModeReplace,
1528 SDATA (encoded_name),
1529 SBYTES (encoded_name));
1530 #endif /* not USE_GTK */
1532 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &icon);
1533 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1534 FRAME_X_DISPLAY_INFO (f)->Xatom_net_wm_icon_name,
1535 FRAME_X_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1536 8, PropModeReplace,
1537 SDATA (encoded_icon_name),
1538 SBYTES (encoded_icon_name));
1540 if (do_free_icon_value)
1541 xfree (icon.value);
1542 if (do_free_text_value)
1543 xfree (text.value);
1545 unblock_input ();
1549 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1550 x_id_name.
1552 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1553 name; if NAME is a string, set F's name to NAME and set
1554 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1556 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1557 suggesting a new name, which lisp code should override; if
1558 F->explicit_name is set, ignore the new name; otherwise, set it. */
1560 static void
1561 x_set_name (struct frame *f, Lisp_Object name, int explicit)
1563 /* Make sure that requests from lisp code override requests from
1564 Emacs redisplay code. */
1565 if (explicit)
1567 /* If we're switching from explicit to implicit, we had better
1568 update the mode lines and thereby update the title. */
1569 if (f->explicit_name && NILP (name))
1570 update_mode_lines = 1;
1572 f->explicit_name = ! NILP (name);
1574 else if (f->explicit_name)
1575 return;
1577 /* If NAME is nil, set the name to the x_id_name. */
1578 if (NILP (name))
1580 /* Check for no change needed in this very common case
1581 before we do any consing. */
1582 if (!strcmp (FRAME_X_DISPLAY_INFO (f)->x_id_name,
1583 SSDATA (f->name)))
1584 return;
1585 name = build_string (FRAME_X_DISPLAY_INFO (f)->x_id_name);
1587 else
1588 CHECK_STRING (name);
1590 /* Don't change the name if it's already NAME. */
1591 if (! NILP (Fstring_equal (name, f->name)))
1592 return;
1594 fset_name (f, name);
1596 /* For setting the frame title, the title parameter should override
1597 the name parameter. */
1598 if (! NILP (f->title))
1599 name = f->title;
1601 x_set_name_internal (f, name);
1604 /* This function should be called when the user's lisp code has
1605 specified a name for the frame; the name will override any set by the
1606 redisplay code. */
1607 static void
1608 x_explicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1610 x_set_name (f, arg, 1);
1613 /* This function should be called by Emacs redisplay code to set the
1614 name; names set this way will never override names set by the user's
1615 lisp code. */
1616 void
1617 x_implicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1619 x_set_name (f, arg, 0);
1622 /* Change the title of frame F to NAME.
1623 If NAME is nil, use the frame name as the title. */
1625 static void
1626 x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
1628 /* Don't change the title if it's already NAME. */
1629 if (EQ (name, f->title))
1630 return;
1632 update_mode_lines = 1;
1634 fset_title (f, name);
1636 if (NILP (name))
1637 name = f->name;
1638 else
1639 CHECK_STRING (name);
1641 x_set_name_internal (f, name);
1644 void
1645 x_set_scroll_bar_default_width (struct frame *f)
1647 int wid = FRAME_COLUMN_WIDTH (f);
1648 #ifdef USE_TOOLKIT_SCROLL_BARS
1649 #ifdef USE_GTK
1650 int minw = xg_get_default_scrollbar_width ();
1651 #else
1652 int minw = 16;
1653 #endif
1654 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
1655 int width = minw + 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM;
1656 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (width + wid - 1) / wid;
1657 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = width;
1658 #else
1659 /* Make the actual width at least 14 pixels and a multiple of a
1660 character width. */
1661 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
1663 /* Use all of that space (aside from required margins) for the
1664 scroll bar. */
1665 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = 0;
1666 #endif
1670 /* Record in frame F the specified or default value according to ALIST
1671 of the parameter named PROP (a Lisp symbol). If no value is
1672 specified for PROP, look for an X default for XPROP on the frame
1673 named NAME. If that is not found either, use the value DEFLT. */
1675 static Lisp_Object
1676 x_default_scroll_bar_color_parameter (struct frame *f,
1677 Lisp_Object alist, Lisp_Object prop,
1678 const char *xprop, const char *xclass,
1679 int foreground_p)
1681 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1682 Lisp_Object tem;
1684 tem = x_get_arg (dpyinfo, alist, prop, xprop, xclass, RES_TYPE_STRING);
1685 if (EQ (tem, Qunbound))
1687 #ifdef USE_TOOLKIT_SCROLL_BARS
1689 /* See if an X resource for the scroll bar color has been
1690 specified. */
1691 tem = display_x_get_resource (dpyinfo,
1692 build_string (foreground_p
1693 ? "foreground"
1694 : "background"),
1695 empty_unibyte_string,
1696 build_string ("verticalScrollBar"),
1697 empty_unibyte_string);
1698 if (!STRINGP (tem))
1700 /* If nothing has been specified, scroll bars will use a
1701 toolkit-dependent default. Because these defaults are
1702 difficult to get at without actually creating a scroll
1703 bar, use nil to indicate that no color has been
1704 specified. */
1705 tem = Qnil;
1708 #else /* not USE_TOOLKIT_SCROLL_BARS */
1710 tem = Qnil;
1712 #endif /* not USE_TOOLKIT_SCROLL_BARS */
1715 x_set_frame_parameters (f, list1 (Fcons (prop, tem)));
1716 return tem;
1722 #ifdef USE_X_TOOLKIT
1724 /* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
1725 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
1726 already be present because of the toolkit (Motif adds some of them,
1727 for example, but Xt doesn't). */
1729 static void
1730 hack_wm_protocols (struct frame *f, Widget widget)
1732 Display *dpy = XtDisplay (widget);
1733 Window w = XtWindow (widget);
1734 int need_delete = 1;
1735 int need_focus = 1;
1736 int need_save = 1;
1738 block_input ();
1740 Atom type;
1741 unsigned char *catoms;
1742 int format = 0;
1743 unsigned long nitems = 0;
1744 unsigned long bytes_after;
1746 if ((XGetWindowProperty (dpy, w,
1747 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
1748 (long)0, (long)100, False, XA_ATOM,
1749 &type, &format, &nitems, &bytes_after,
1750 &catoms)
1751 == Success)
1752 && format == 32 && type == XA_ATOM)
1754 Atom *atoms = (Atom *) catoms;
1755 while (nitems > 0)
1757 nitems--;
1758 if (atoms[nitems]
1759 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window)
1760 need_delete = 0;
1761 else if (atoms[nitems]
1762 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus)
1763 need_focus = 0;
1764 else if (atoms[nitems]
1765 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself)
1766 need_save = 0;
1769 if (catoms)
1770 XFree (catoms);
1773 Atom props [10];
1774 int count = 0;
1775 if (need_delete)
1776 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
1777 if (need_focus)
1778 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus;
1779 if (need_save)
1780 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
1781 if (count)
1782 XChangeProperty (dpy, w, FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
1783 XA_ATOM, 32, PropModeAppend,
1784 (unsigned char *) props, count);
1786 unblock_input ();
1788 #endif
1792 /* Support routines for XIC (X Input Context). */
1794 #ifdef HAVE_X_I18N
1796 static XFontSet xic_create_xfontset (struct frame *);
1797 static XIMStyle best_xim_style (XIMStyles *, XIMStyles *);
1800 /* Supported XIM styles, ordered by preference. */
1802 static XIMStyle supported_xim_styles[] =
1804 XIMPreeditPosition | XIMStatusArea,
1805 XIMPreeditPosition | XIMStatusNothing,
1806 XIMPreeditPosition | XIMStatusNone,
1807 XIMPreeditNothing | XIMStatusArea,
1808 XIMPreeditNothing | XIMStatusNothing,
1809 XIMPreeditNothing | XIMStatusNone,
1810 XIMPreeditNone | XIMStatusArea,
1811 XIMPreeditNone | XIMStatusNothing,
1812 XIMPreeditNone | XIMStatusNone,
1817 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
1818 /* Create an X fontset on frame F with base font name BASE_FONTNAME. */
1820 static const char xic_default_fontset[] = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
1822 /* Create an Xt fontset spec from the name of a base font.
1823 If `motif' is True use the Motif syntax. */
1824 char *
1825 xic_create_fontsetname (const char *base_fontname, int motif)
1827 const char *sep = motif ? ";" : ",";
1828 char *fontsetname;
1830 /* Make a fontset name from the base font name. */
1831 if (xic_default_fontset == base_fontname)
1833 /* There is no base font name, use the default. */
1834 fontsetname = xmalloc (strlen (base_fontname) + 2);
1835 strcpy (fontsetname, base_fontname);
1837 else
1839 /* Make a fontset name from the base font name.
1840 The font set will be made of the following elements:
1841 - the base font.
1842 - the base font where the charset spec is replaced by -*-*.
1843 - the same but with the family also replaced with -*-*-. */
1844 const char *p = base_fontname;
1845 ptrdiff_t i;
1847 for (i = 0; *p; p++)
1848 if (*p == '-') i++;
1849 if (i != 14)
1851 /* As the font name doesn't conform to XLFD, we can't
1852 modify it to generalize it to allcs and allfamilies.
1853 Use the specified font plus the default. */
1854 fontsetname = xmalloc (strlen (base_fontname)
1855 + strlen (xic_default_fontset) + 3);
1856 strcpy (fontsetname, base_fontname);
1857 strcat (fontsetname, sep);
1858 strcat (fontsetname, xic_default_fontset);
1860 else
1862 ptrdiff_t len;
1863 const char *p1 = NULL, *p2 = NULL, *p3 = NULL;
1864 char *font_allcs = NULL;
1865 char *font_allfamilies = NULL;
1866 char *font_all = NULL;
1867 const char *allcs = "*-*-*-*-*-*-*";
1868 const char *allfamilies = "-*-*-";
1869 const char *all = "*-*-*-*-";
1870 char *base;
1872 for (i = 0, p = base_fontname; i < 8; p++)
1874 if (*p == '-')
1876 i++;
1877 if (i == 3)
1878 p1 = p + 1;
1879 else if (i == 7)
1880 p2 = p + 1;
1881 else if (i == 6)
1882 p3 = p + 1;
1885 /* If base_fontname specifies ADSTYLE, make it a
1886 wildcard. */
1887 if (*p3 != '*')
1889 ptrdiff_t diff = (p2 - p3) - 2;
1891 base = alloca (strlen (base_fontname) + 1);
1892 memcpy (base, base_fontname, p3 - base_fontname);
1893 base[p3 - base_fontname] = '*';
1894 base[(p3 - base_fontname) + 1] = '-';
1895 strcpy (base + (p3 - base_fontname) + 2, p2);
1896 p = base + (p - base_fontname) - diff;
1897 p1 = base + (p1 - base_fontname);
1898 p2 = base + (p2 - base_fontname) - diff;
1899 base_fontname = base;
1902 /* Build the font spec that matches all charsets. */
1903 len = p - base_fontname + strlen (allcs) + 1;
1904 font_allcs = alloca (len);
1905 memcpy (font_allcs, base_fontname, p - base_fontname);
1906 strcat (font_allcs, allcs);
1908 /* Build the font spec that matches all families and
1909 add-styles. */
1910 len = p - p1 + strlen (allcs) + strlen (allfamilies) + 1;
1911 font_allfamilies = alloca (len);
1912 strcpy (font_allfamilies, allfamilies);
1913 memcpy (font_allfamilies + strlen (allfamilies), p1, p - p1);
1914 strcat (font_allfamilies, allcs);
1916 /* Build the font spec that matches all. */
1917 len = p - p2 + strlen (allcs) + strlen (all) + strlen (allfamilies) + 1;
1918 font_all = alloca (len);
1919 strcpy (font_all, allfamilies);
1920 strcat (font_all, all);
1921 memcpy (font_all + strlen (all) + strlen (allfamilies), p2, p - p2);
1922 strcat (font_all, allcs);
1924 /* Build the actual font set name. */
1925 len = strlen (base_fontname) + strlen (font_allcs)
1926 + strlen (font_allfamilies) + strlen (font_all) + 5;
1927 fontsetname = xmalloc (len);
1928 strcpy (fontsetname, base_fontname);
1929 strcat (fontsetname, sep);
1930 strcat (fontsetname, font_allcs);
1931 strcat (fontsetname, sep);
1932 strcat (fontsetname, font_allfamilies);
1933 strcat (fontsetname, sep);
1934 strcat (fontsetname, font_all);
1937 if (motif)
1938 strcat (fontsetname, ":");
1939 return fontsetname;
1941 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
1943 #ifdef DEBUG_XIC_FONTSET
1944 static void
1945 print_fontset_result (XFontSet xfs, char *name, char **missing_list,
1946 int missing_count)
1948 if (xfs)
1949 fprintf (stderr, "XIC Fontset created: %s\n", name);
1950 else
1952 fprintf (stderr, "XIC Fontset failed: %s\n", name);
1953 while (missing_count-- > 0)
1955 fprintf (stderr, " missing: %s\n", *missing_list);
1956 missing_list++;
1961 #endif
1963 static XFontSet
1964 xic_create_xfontset (struct frame *f)
1966 XFontSet xfs = NULL;
1967 struct font *font = FRAME_FONT (f);
1968 int pixel_size = font->pixel_size;
1969 Lisp_Object rest, frame;
1971 /* See if there is another frame already using same fontset. */
1972 FOR_EACH_FRAME (rest, frame)
1974 struct frame *cf = XFRAME (frame);
1976 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
1977 && FRAME_X_DISPLAY_INFO (cf) == FRAME_X_DISPLAY_INFO (f)
1978 && FRAME_FONT (f)
1979 && FRAME_FONT (f)->pixel_size == pixel_size)
1981 xfs = FRAME_XIC_FONTSET (cf);
1982 break;
1986 if (! xfs)
1988 char buf[256];
1989 char **missing_list;
1990 int missing_count;
1991 char *def_string;
1992 const char *xlfd_format = "-*-*-medium-r-normal--%d-*-*-*-*-*";
1994 sprintf (buf, xlfd_format, pixel_size);
1995 missing_list = NULL;
1996 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
1997 &missing_list, &missing_count, &def_string);
1998 #ifdef DEBUG_XIC_FONTSET
1999 print_fontset_result (xfs, buf, missing_list, missing_count);
2000 #endif
2001 if (missing_list)
2002 XFreeStringList (missing_list);
2003 if (! xfs)
2005 /* List of pixel sizes most likely available. Find one that
2006 is closest to pixel_size. */
2007 int sizes[] = {0, 8, 10, 11, 12, 14, 17, 18, 20, 24, 26, 34, 0};
2008 int *smaller, *larger;
2010 for (smaller = sizes; smaller[1]; smaller++)
2011 if (smaller[1] >= pixel_size)
2012 break;
2013 larger = smaller + 1;
2014 if (*larger == pixel_size)
2015 larger++;
2016 while (*smaller || *larger)
2018 int this_size;
2020 if (! *larger)
2021 this_size = *smaller--;
2022 else if (! *smaller)
2023 this_size = *larger++;
2024 else if (pixel_size - *smaller < *larger - pixel_size)
2025 this_size = *smaller--;
2026 else
2027 this_size = *larger++;
2028 sprintf (buf, xlfd_format, this_size);
2029 missing_list = NULL;
2030 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
2031 &missing_list, &missing_count, &def_string);
2032 #ifdef DEBUG_XIC_FONTSET
2033 print_fontset_result (xfs, buf, missing_list, missing_count);
2034 #endif
2035 if (missing_list)
2036 XFreeStringList (missing_list);
2037 if (xfs)
2038 break;
2041 if (! xfs)
2043 const char *last_resort = "-*-*-*-r-normal--*-*-*-*-*-*";
2045 missing_list = NULL;
2046 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), last_resort,
2047 &missing_list, &missing_count, &def_string);
2048 #ifdef DEBUG_XIC_FONTSET
2049 print_fontset_result (xfs, last_resort, missing_list, missing_count);
2050 #endif
2051 if (missing_list)
2052 XFreeStringList (missing_list);
2057 return xfs;
2060 /* Free the X fontset of frame F if it is the last frame using it. */
2062 void
2063 xic_free_xfontset (struct frame *f)
2065 Lisp_Object rest, frame;
2066 bool shared_p = 0;
2068 if (!FRAME_XIC_FONTSET (f))
2069 return;
2071 /* See if there is another frame sharing the same fontset. */
2072 FOR_EACH_FRAME (rest, frame)
2074 struct frame *cf = XFRAME (frame);
2075 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
2076 && FRAME_X_DISPLAY_INFO (cf) == FRAME_X_DISPLAY_INFO (f)
2077 && FRAME_XIC_FONTSET (cf) == FRAME_XIC_FONTSET (f))
2079 shared_p = 1;
2080 break;
2084 if (!shared_p)
2085 /* The fontset is not used anymore. It is safe to free it. */
2086 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
2088 FRAME_XIC_FONTSET (f) = NULL;
2092 /* Value is the best input style, given user preferences USER (already
2093 checked to be supported by Emacs), and styles supported by the
2094 input method XIM. */
2096 static XIMStyle
2097 best_xim_style (XIMStyles *user, XIMStyles *xim)
2099 int i, j;
2101 for (i = 0; i < user->count_styles; ++i)
2102 for (j = 0; j < xim->count_styles; ++j)
2103 if (user->supported_styles[i] == xim->supported_styles[j])
2104 return user->supported_styles[i];
2106 /* Return the default style. */
2107 return XIMPreeditNothing | XIMStatusNothing;
2110 /* Create XIC for frame F. */
2112 static XIMStyle xic_style;
2114 void
2115 create_frame_xic (struct frame *f)
2117 XIM xim;
2118 XIC xic = NULL;
2119 XFontSet xfs = NULL;
2121 if (FRAME_XIC (f))
2122 return;
2124 /* Create X fontset. */
2125 xfs = xic_create_xfontset (f);
2126 xim = FRAME_X_XIM (f);
2127 if (xim)
2129 XRectangle s_area;
2130 XPoint spot;
2131 XVaNestedList preedit_attr;
2132 XVaNestedList status_attr;
2134 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
2135 spot.x = 0; spot.y = 1;
2137 /* Determine XIC style. */
2138 if (xic_style == 0)
2140 XIMStyles supported_list;
2141 supported_list.count_styles = (sizeof supported_xim_styles
2142 / sizeof supported_xim_styles[0]);
2143 supported_list.supported_styles = supported_xim_styles;
2144 xic_style = best_xim_style (&supported_list,
2145 FRAME_X_XIM_STYLES (f));
2148 preedit_attr = XVaCreateNestedList (0,
2149 XNFontSet, xfs,
2150 XNForeground,
2151 FRAME_FOREGROUND_PIXEL (f),
2152 XNBackground,
2153 FRAME_BACKGROUND_PIXEL (f),
2154 (xic_style & XIMPreeditPosition
2155 ? XNSpotLocation
2156 : NULL),
2157 &spot,
2158 NULL);
2159 status_attr = XVaCreateNestedList (0,
2160 XNArea,
2161 &s_area,
2162 XNFontSet,
2163 xfs,
2164 XNForeground,
2165 FRAME_FOREGROUND_PIXEL (f),
2166 XNBackground,
2167 FRAME_BACKGROUND_PIXEL (f),
2168 NULL);
2170 xic = XCreateIC (xim,
2171 XNInputStyle, xic_style,
2172 XNClientWindow, FRAME_X_WINDOW (f),
2173 XNFocusWindow, FRAME_X_WINDOW (f),
2174 XNStatusAttributes, status_attr,
2175 XNPreeditAttributes, preedit_attr,
2176 NULL);
2177 XFree (preedit_attr);
2178 XFree (status_attr);
2181 FRAME_XIC (f) = xic;
2182 FRAME_XIC_STYLE (f) = xic_style;
2183 FRAME_XIC_FONTSET (f) = xfs;
2187 /* Destroy XIC and free XIC fontset of frame F, if any. */
2189 void
2190 free_frame_xic (struct frame *f)
2192 if (FRAME_XIC (f) == NULL)
2193 return;
2195 XDestroyIC (FRAME_XIC (f));
2196 xic_free_xfontset (f);
2198 FRAME_XIC (f) = NULL;
2202 /* Place preedit area for XIC of window W's frame to specified
2203 pixel position X/Y. X and Y are relative to window W. */
2205 void
2206 xic_set_preeditarea (struct window *w, int x, int y)
2208 struct frame *f = XFRAME (w->frame);
2209 XVaNestedList attr;
2210 XPoint spot;
2212 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x) + WINDOW_LEFT_FRINGE_WIDTH (w);
2213 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
2214 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
2215 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2216 XFree (attr);
2220 /* Place status area for XIC in bottom right corner of frame F.. */
2222 void
2223 xic_set_statusarea (struct frame *f)
2225 XIC xic = FRAME_XIC (f);
2226 XVaNestedList attr;
2227 XRectangle area;
2228 XRectangle *needed;
2230 /* Negotiate geometry of status area. If input method has existing
2231 status area, use its current size. */
2232 area.x = area.y = area.width = area.height = 0;
2233 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
2234 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2235 XFree (attr);
2237 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
2238 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2239 XFree (attr);
2241 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
2243 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
2244 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2245 XFree (attr);
2248 area.width = needed->width;
2249 area.height = needed->height;
2250 area.x = FRAME_PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
2251 area.y = (FRAME_PIXEL_HEIGHT (f) - area.height
2252 - FRAME_MENUBAR_HEIGHT (f)
2253 - FRAME_TOOLBAR_TOP_HEIGHT (f)
2254 - FRAME_INTERNAL_BORDER_WIDTH (f));
2255 XFree (needed);
2257 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
2258 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2259 XFree (attr);
2263 /* Set X fontset for XIC of frame F, using base font name
2264 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
2266 void
2267 xic_set_xfontset (struct frame *f, const char *base_fontname)
2269 XVaNestedList attr;
2270 XFontSet xfs;
2272 xic_free_xfontset (f);
2274 xfs = xic_create_xfontset (f);
2276 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
2277 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
2278 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2279 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
2280 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
2281 XFree (attr);
2283 FRAME_XIC_FONTSET (f) = xfs;
2286 #endif /* HAVE_X_I18N */
2290 #ifdef USE_X_TOOLKIT
2292 /* Create and set up the X widget for frame F. */
2294 static void
2295 x_window (struct frame *f, long window_prompting, int minibuffer_only)
2297 XClassHint class_hints;
2298 XSetWindowAttributes attributes;
2299 unsigned long attribute_mask;
2300 Widget shell_widget;
2301 Widget pane_widget;
2302 Widget frame_widget;
2303 Arg al [25];
2304 int ac;
2306 block_input ();
2308 /* Use the resource name as the top-level widget name
2309 for looking up resources. Make a non-Lisp copy
2310 for the window manager, so GC relocation won't bother it.
2312 Elsewhere we specify the window name for the window manager. */
2313 f->namebuf = xstrdup (SSDATA (Vx_resource_name));
2315 ac = 0;
2316 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
2317 XtSetArg (al[ac], XtNinput, 1); ac++;
2318 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2319 XtSetArg (al[ac], XtNborderWidth, f->border_width); ac++;
2320 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2321 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2322 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2323 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
2324 applicationShellWidgetClass,
2325 FRAME_X_DISPLAY (f), al, ac);
2327 f->output_data.x->widget = shell_widget;
2328 /* maybe_set_screen_title_format (shell_widget); */
2330 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
2331 NULL, shell_widget, False,
2332 NULL, NULL, NULL, NULL);
2334 ac = 0;
2335 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2336 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2337 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2338 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2339 XtSetValues (pane_widget, al, ac);
2340 f->output_data.x->column_widget = pane_widget;
2342 /* mappedWhenManaged to false tells to the paned window to not map/unmap
2343 the emacs screen when changing menubar. This reduces flickering. */
2345 ac = 0;
2346 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2347 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
2348 XtSetArg (al[ac], XtNallowResize, 1); ac++;
2349 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
2350 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
2351 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2352 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2353 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2354 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2355 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
2356 al, ac);
2358 f->output_data.x->edit_widget = frame_widget;
2360 XtManageChild (frame_widget);
2362 /* Do some needed geometry management. */
2364 char *tem, shell_position[sizeof "=x++" + 4 * INT_STRLEN_BOUND (int)];
2365 Arg gal[10];
2366 int gac = 0;
2367 int extra_borders = 0;
2368 int menubar_size
2369 = (f->output_data.x->menubar_widget
2370 ? (f->output_data.x->menubar_widget->core.height
2371 + f->output_data.x->menubar_widget->core.border_width)
2372 : 0);
2374 #if 0 /* Experimentally, we now get the right results
2375 for -geometry -0-0 without this. 24 Aug 96, rms. */
2376 if (FRAME_EXTERNAL_MENU_BAR (f))
2378 Dimension ibw = 0;
2379 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
2380 menubar_size += ibw;
2382 #endif
2384 f->output_data.x->menubar_height = menubar_size;
2386 #ifndef USE_LUCID
2387 /* Motif seems to need this amount added to the sizes
2388 specified for the shell widget. The Athena/Lucid widgets don't.
2389 Both conclusions reached experimentally. -- rms. */
2390 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
2391 &extra_borders, NULL);
2392 extra_borders *= 2;
2393 #endif
2395 /* Convert our geometry parameters into a geometry string
2396 and specify it.
2397 Note that we do not specify here whether the position
2398 is a user-specified or program-specified one.
2399 We pass that information later, in x_wm_set_size_hints. */
2401 int left = f->left_pos;
2402 int xneg = window_prompting & XNegative;
2403 int top = f->top_pos;
2404 int yneg = window_prompting & YNegative;
2405 if (xneg)
2406 left = -left;
2407 if (yneg)
2408 top = -top;
2410 if (window_prompting & USPosition)
2411 sprintf (shell_position, "=%dx%d%c%d%c%d",
2412 FRAME_PIXEL_WIDTH (f) + extra_borders,
2413 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders,
2414 (xneg ? '-' : '+'), left,
2415 (yneg ? '-' : '+'), top);
2416 else
2418 sprintf (shell_position, "=%dx%d",
2419 FRAME_PIXEL_WIDTH (f) + extra_borders,
2420 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders);
2422 /* Setting x and y when the position is not specified in
2423 the geometry string will set program position in the WM hints.
2424 If Emacs had just one program position, we could set it in
2425 fallback resources, but since each make-frame call can specify
2426 different program positions, this is easier. */
2427 XtSetArg (gal[gac], XtNx, left); gac++;
2428 XtSetArg (gal[gac], XtNy, top); gac++;
2432 /* We don't free this because we don't know whether
2433 it is safe to free it while the frame exists.
2434 It isn't worth the trouble of arranging to free it
2435 when the frame is deleted. */
2436 tem = xstrdup (shell_position);
2437 XtSetArg (gal[gac], XtNgeometry, tem); gac++;
2438 XtSetValues (shell_widget, gal, gac);
2441 XtManageChild (pane_widget);
2442 XtRealizeWidget (shell_widget);
2444 if (FRAME_X_EMBEDDED_P (f))
2445 XReparentWindow (FRAME_X_DISPLAY (f), XtWindow (shell_widget),
2446 f->output_data.x->parent_desc, 0, 0);
2448 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
2450 validate_x_resource_name ();
2452 class_hints.res_name = SSDATA (Vx_resource_name);
2453 class_hints.res_class = SSDATA (Vx_resource_class);
2454 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
2456 #ifdef HAVE_X_I18N
2457 FRAME_XIC (f) = NULL;
2458 if (use_xim)
2459 create_frame_xic (f);
2460 #endif
2462 f->output_data.x->wm_hints.input = True;
2463 f->output_data.x->wm_hints.flags |= InputHint;
2464 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2465 &f->output_data.x->wm_hints);
2467 hack_wm_protocols (f, shell_widget);
2469 #ifdef HACK_EDITRES
2470 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
2471 #endif
2473 /* Do a stupid property change to force the server to generate a
2474 PropertyNotify event so that the event_stream server timestamp will
2475 be initialized to something relevant to the time we created the window.
2477 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
2478 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
2479 XA_ATOM, 32, PropModeAppend, NULL, 0);
2481 /* Make all the standard events reach the Emacs frame. */
2482 attributes.event_mask = STANDARD_EVENT_SET;
2484 #ifdef HAVE_X_I18N
2485 if (FRAME_XIC (f))
2487 /* XIM server might require some X events. */
2488 unsigned long fevent = NoEventMask;
2489 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2490 attributes.event_mask |= fevent;
2492 #endif /* HAVE_X_I18N */
2494 attribute_mask = CWEventMask;
2495 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
2496 attribute_mask, &attributes);
2498 XtMapWidget (frame_widget);
2500 /* x_set_name normally ignores requests to set the name if the
2501 requested name is the same as the current name. This is the one
2502 place where that assumption isn't correct; f->name is set, but
2503 the X server hasn't been told. */
2505 Lisp_Object name;
2506 int explicit = f->explicit_name;
2508 f->explicit_name = 0;
2509 name = f->name;
2510 fset_name (f, Qnil);
2511 x_set_name (f, name, explicit);
2514 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2515 f->output_data.x->current_cursor
2516 = f->output_data.x->text_cursor);
2518 unblock_input ();
2520 /* This is a no-op, except under Motif. Make sure main areas are
2521 set to something reasonable, in case we get an error later. */
2522 lw_set_main_areas (pane_widget, 0, frame_widget);
2525 #else /* not USE_X_TOOLKIT */
2526 #ifdef USE_GTK
2527 static void
2528 x_window (struct frame *f)
2530 if (! xg_create_frame_widgets (f))
2531 error ("Unable to create window");
2533 #ifdef HAVE_X_I18N
2534 FRAME_XIC (f) = NULL;
2535 if (use_xim)
2537 block_input ();
2538 create_frame_xic (f);
2539 if (FRAME_XIC (f))
2541 /* XIM server might require some X events. */
2542 unsigned long fevent = NoEventMask;
2543 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2545 if (fevent != NoEventMask)
2547 XSetWindowAttributes attributes;
2548 XWindowAttributes wattr;
2549 unsigned long attribute_mask;
2551 XGetWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2552 &wattr);
2553 attributes.event_mask = wattr.your_event_mask | fevent;
2554 attribute_mask = CWEventMask;
2555 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2556 attribute_mask, &attributes);
2559 unblock_input ();
2561 #endif
2564 #else /*! USE_GTK */
2565 /* Create and set up the X window for frame F. */
2567 static void
2568 x_window (struct frame *f)
2570 XClassHint class_hints;
2571 XSetWindowAttributes attributes;
2572 unsigned long attribute_mask;
2574 attributes.background_pixel = FRAME_BACKGROUND_PIXEL (f);
2575 attributes.border_pixel = f->output_data.x->border_pixel;
2576 attributes.bit_gravity = StaticGravity;
2577 attributes.backing_store = NotUseful;
2578 attributes.save_under = True;
2579 attributes.event_mask = STANDARD_EVENT_SET;
2580 attributes.colormap = FRAME_X_COLORMAP (f);
2581 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
2582 | CWColormap);
2584 block_input ();
2585 FRAME_X_WINDOW (f)
2586 = XCreateWindow (FRAME_X_DISPLAY (f),
2587 f->output_data.x->parent_desc,
2588 f->left_pos,
2589 f->top_pos,
2590 FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f),
2591 f->border_width,
2592 CopyFromParent, /* depth */
2593 InputOutput, /* class */
2594 FRAME_X_VISUAL (f),
2595 attribute_mask, &attributes);
2597 #ifdef HAVE_X_I18N
2598 if (use_xim)
2600 create_frame_xic (f);
2601 if (FRAME_XIC (f))
2603 /* XIM server might require some X events. */
2604 unsigned long fevent = NoEventMask;
2605 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2606 attributes.event_mask |= fevent;
2607 attribute_mask = CWEventMask;
2608 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2609 attribute_mask, &attributes);
2612 #endif /* HAVE_X_I18N */
2614 validate_x_resource_name ();
2616 class_hints.res_name = SSDATA (Vx_resource_name);
2617 class_hints.res_class = SSDATA (Vx_resource_class);
2618 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
2620 /* The menubar is part of the ordinary display;
2621 it does not count in addition to the height of the window. */
2622 f->output_data.x->menubar_height = 0;
2624 /* This indicates that we use the "Passive Input" input model.
2625 Unless we do this, we don't get the Focus{In,Out} events that we
2626 need to draw the cursor correctly. Accursed bureaucrats.
2627 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
2629 f->output_data.x->wm_hints.input = True;
2630 f->output_data.x->wm_hints.flags |= InputHint;
2631 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2632 &f->output_data.x->wm_hints);
2633 f->output_data.x->wm_hints.icon_pixmap = None;
2635 /* Request "save yourself" and "delete window" commands from wm. */
2637 Atom protocols[2];
2638 protocols[0] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
2639 protocols[1] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
2640 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
2643 /* x_set_name normally ignores requests to set the name if the
2644 requested name is the same as the current name. This is the one
2645 place where that assumption isn't correct; f->name is set, but
2646 the X server hasn't been told. */
2648 Lisp_Object name;
2649 int explicit = f->explicit_name;
2651 f->explicit_name = 0;
2652 name = f->name;
2653 fset_name (f, Qnil);
2654 x_set_name (f, name, explicit);
2657 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2658 f->output_data.x->current_cursor
2659 = f->output_data.x->text_cursor);
2661 unblock_input ();
2663 if (FRAME_X_WINDOW (f) == 0)
2664 error ("Unable to create window");
2667 #endif /* not USE_GTK */
2668 #endif /* not USE_X_TOOLKIT */
2670 /* Verify that the icon position args for this window are valid. */
2672 static void
2673 x_icon_verify (struct frame *f, Lisp_Object parms)
2675 Lisp_Object icon_x, icon_y;
2677 /* Set the position of the icon. Note that twm groups all
2678 icons in an icon window. */
2679 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2680 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2681 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2683 CHECK_NUMBER (icon_x);
2684 CHECK_NUMBER (icon_y);
2686 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2687 error ("Both left and top icon corners of icon must be specified");
2690 /* Handle the icon stuff for this window. Perhaps later we might
2691 want an x_set_icon_position which can be called interactively as
2692 well. */
2694 static void
2695 x_icon (struct frame *f, Lisp_Object parms)
2697 Lisp_Object icon_x, icon_y;
2698 #if 0
2699 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2700 #endif
2702 /* Set the position of the icon. Note that twm groups all
2703 icons in an icon window. */
2704 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2705 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2706 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2708 CHECK_TYPE_RANGED_INTEGER (int, icon_x);
2709 CHECK_TYPE_RANGED_INTEGER (int, icon_y);
2711 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2712 error ("Both left and top icon corners of icon must be specified");
2714 block_input ();
2716 if (! EQ (icon_x, Qunbound))
2717 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
2719 #if 0 /* x_get_arg removes the visibility parameter as a side effect,
2720 but x_create_frame still needs it. */
2721 /* Start up iconic or window? */
2722 x_wm_set_window_state
2723 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
2724 Qicon)
2725 ? IconicState
2726 : NormalState));
2727 #endif
2729 x_text_icon (f, SSDATA ((!NILP (f->icon_name)
2730 ? f->icon_name
2731 : f->name)));
2733 unblock_input ();
2736 /* Make the GCs needed for this window, setting the
2737 background, border and mouse colors; also create the
2738 mouse cursor and the gray border tile. */
2740 static void
2741 x_make_gc (struct frame *f)
2743 XGCValues gc_values;
2745 block_input ();
2747 /* Create the GCs of this frame.
2748 Note that many default values are used. */
2750 gc_values.foreground = FRAME_FOREGROUND_PIXEL (f);
2751 gc_values.background = FRAME_BACKGROUND_PIXEL (f);
2752 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
2753 f->output_data.x->normal_gc
2754 = XCreateGC (FRAME_X_DISPLAY (f),
2755 FRAME_X_WINDOW (f),
2756 GCLineWidth | GCForeground | GCBackground,
2757 &gc_values);
2759 /* Reverse video style. */
2760 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2761 gc_values.background = FRAME_FOREGROUND_PIXEL (f);
2762 f->output_data.x->reverse_gc
2763 = XCreateGC (FRAME_X_DISPLAY (f),
2764 FRAME_X_WINDOW (f),
2765 GCForeground | GCBackground | GCLineWidth,
2766 &gc_values);
2768 /* Cursor has cursor-color background, background-color foreground. */
2769 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2770 gc_values.background = f->output_data.x->cursor_pixel;
2771 gc_values.fill_style = FillOpaqueStippled;
2772 f->output_data.x->cursor_gc
2773 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2774 (GCForeground | GCBackground
2775 | GCFillStyle | GCLineWidth),
2776 &gc_values);
2778 /* Create the gray border tile used when the pointer is not in
2779 the frame. Since this depends on the frame's pixel values,
2780 this must be done on a per-frame basis. */
2781 f->output_data.x->border_tile
2782 = (XCreatePixmapFromBitmapData
2783 (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
2784 gray_bits, gray_width, gray_height,
2785 FRAME_FOREGROUND_PIXEL (f),
2786 FRAME_BACKGROUND_PIXEL (f),
2787 DefaultDepth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f))));
2789 unblock_input ();
2793 /* Free what was allocated in x_make_gc. */
2795 void
2796 x_free_gcs (struct frame *f)
2798 Display *dpy = FRAME_X_DISPLAY (f);
2800 block_input ();
2802 if (f->output_data.x->normal_gc)
2804 XFreeGC (dpy, f->output_data.x->normal_gc);
2805 f->output_data.x->normal_gc = 0;
2808 if (f->output_data.x->reverse_gc)
2810 XFreeGC (dpy, f->output_data.x->reverse_gc);
2811 f->output_data.x->reverse_gc = 0;
2814 if (f->output_data.x->cursor_gc)
2816 XFreeGC (dpy, f->output_data.x->cursor_gc);
2817 f->output_data.x->cursor_gc = 0;
2820 if (f->output_data.x->border_tile)
2822 XFreePixmap (dpy, f->output_data.x->border_tile);
2823 f->output_data.x->border_tile = 0;
2826 unblock_input ();
2830 /* Handler for signals raised during x_create_frame and
2831 x_create_tip_frame. FRAME is the frame which is partially
2832 constructed. */
2834 static Lisp_Object
2835 unwind_create_frame (Lisp_Object frame)
2837 struct frame *f = XFRAME (frame);
2839 /* If frame is already dead, nothing to do. This can happen if the
2840 display is disconnected after the frame has become official, but
2841 before x_create_frame removes the unwind protect. */
2842 if (!FRAME_LIVE_P (f))
2843 return Qnil;
2845 /* If frame is ``official'', nothing to do. */
2846 if (NILP (Fmemq (frame, Vframe_list)))
2848 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2849 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2850 #endif
2852 x_free_frame_resources (f);
2853 free_glyphs (f);
2855 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2856 /* Check that reference counts are indeed correct. */
2857 eassert (dpyinfo->reference_count == dpyinfo_refcount);
2858 eassert (dpyinfo->terminal->image_cache->refcount == image_cache_refcount);
2859 #endif
2860 return Qt;
2863 return Qnil;
2866 static void
2867 do_unwind_create_frame (Lisp_Object frame)
2869 unwind_create_frame (frame);
2872 static void
2873 unwind_create_frame_1 (Lisp_Object val)
2875 inhibit_lisp_code = val;
2878 static void
2879 x_default_font_parameter (struct frame *f, Lisp_Object parms)
2881 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2882 Lisp_Object font_param = x_get_arg (dpyinfo, parms, Qfont, NULL, NULL,
2883 RES_TYPE_STRING);
2884 Lisp_Object font = Qnil;
2885 if (EQ (font_param, Qunbound))
2886 font_param = Qnil;
2888 if (NILP (font_param))
2890 /* System font should take precedence over X resources. We suggest this
2891 regardless of font-use-system-font because .emacs may not have been
2892 read yet. */
2893 const char *system_font = xsettings_get_system_font ();
2894 if (system_font)
2895 font = font_open_by_name (f, build_unibyte_string (system_font));
2898 if (NILP (font))
2899 font = !NILP (font_param) ? font_param
2900 : x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
2902 if (! FONTP (font) && ! STRINGP (font))
2904 const char *names[]
2906 #ifdef HAVE_XFT
2907 /* This will find the normal Xft font. */
2908 "monospace-10",
2909 #endif
2910 "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1",
2911 "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2912 "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2913 /* This was formerly the first thing tried, but it finds
2914 too many fonts and takes too long. */
2915 "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1",
2916 /* If those didn't work, look for something which will
2917 at least work. */
2918 "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1",
2919 "fixed",
2920 NULL };
2921 int i;
2923 for (i = 0; names[i]; i++)
2925 font = font_open_by_name (f, build_unibyte_string (names[i]));
2926 if (! NILP (font))
2927 break;
2929 if (NILP (font))
2930 error ("No suitable font was found");
2932 else if (!NILP (font_param))
2934 /* Remember the explicit font parameter, so we can re-apply it after
2935 we've applied the `default' face settings. */
2936 x_set_frame_parameters (f, list1 (Fcons (Qfont_param, font_param)));
2939 /* This call will make X resources override any system font setting. */
2940 x_default_parameter (f, parms, Qfont, font, "font", "Font", RES_TYPE_STRING);
2944 DEFUN ("x-wm-set-size-hint", Fx_wm_set_size_hint, Sx_wm_set_size_hint,
2945 0, 1, 0,
2946 doc: /* Send the size hints for frame FRAME to the window manager.
2947 If FRAME is omitted or nil, use the selected frame.
2948 Signal error if FRAME is not an X frame. */)
2949 (Lisp_Object frame)
2951 struct frame *f = decode_window_system_frame (frame);
2953 block_input ();
2954 x_wm_set_size_hint (f, 0, 0);
2955 unblock_input ();
2956 return Qnil;
2959 static void
2960 set_machine_and_pid_properties (struct frame *f)
2962 long pid = (long) getpid ();
2964 /* This will set WM_CLIENT_MACHINE and WM_LOCALE_NAME. */
2965 XSetWMProperties (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), NULL, NULL,
2966 NULL, 0, NULL, NULL, NULL);
2967 XChangeProperty (FRAME_X_DISPLAY (f),
2968 FRAME_OUTER_WINDOW (f),
2969 XInternAtom (FRAME_X_DISPLAY (f),
2970 "_NET_WM_PID",
2971 False),
2972 XA_CARDINAL, 32, PropModeReplace,
2973 (unsigned char *) &pid, 1);
2976 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
2977 1, 1, 0,
2978 doc: /* Make a new X window, which is called a "frame" in Emacs terms.
2979 Return an Emacs frame object.
2980 PARMS is an alist of frame parameters.
2981 If the parameters specify that the frame should not have a minibuffer,
2982 and do not specify a specific minibuffer window to use,
2983 then `default-minibuffer-frame' must be a frame whose minibuffer can
2984 be shared by the new frame.
2986 This function is an internal primitive--use `make-frame' instead. */)
2987 (Lisp_Object parms)
2989 struct frame *f;
2990 Lisp_Object frame, tem;
2991 Lisp_Object name;
2992 int minibuffer_only = 0;
2993 long window_prompting = 0;
2994 int width, height;
2995 ptrdiff_t count = SPECPDL_INDEX ();
2996 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
2997 Lisp_Object display;
2998 struct x_display_info *dpyinfo = NULL;
2999 Lisp_Object parent;
3000 struct kboard *kb;
3002 parms = Fcopy_alist (parms);
3004 /* Use this general default value to start with
3005 until we know if this frame has a specified name. */
3006 Vx_resource_name = Vinvocation_name;
3008 display = x_get_arg (dpyinfo, parms, Qterminal, 0, 0, RES_TYPE_NUMBER);
3009 if (EQ (display, Qunbound))
3010 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
3011 if (EQ (display, Qunbound))
3012 display = Qnil;
3013 dpyinfo = check_x_display_info (display);
3014 kb = dpyinfo->terminal->kboard;
3016 if (!dpyinfo->terminal->name)
3017 error ("Terminal is not live, can't create new frames on it");
3019 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
3020 if (!STRINGP (name)
3021 && ! EQ (name, Qunbound)
3022 && ! NILP (name))
3023 error ("Invalid frame name--not a string or nil");
3025 if (STRINGP (name))
3026 Vx_resource_name = name;
3028 /* See if parent window is specified. */
3029 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
3030 if (EQ (parent, Qunbound))
3031 parent = Qnil;
3032 if (! NILP (parent))
3033 CHECK_NUMBER (parent);
3035 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
3036 /* No need to protect DISPLAY because that's not used after passing
3037 it to make_frame_without_minibuffer. */
3038 frame = Qnil;
3039 GCPRO4 (parms, parent, name, frame);
3040 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
3041 RES_TYPE_SYMBOL);
3042 if (EQ (tem, Qnone) || NILP (tem))
3043 f = make_frame_without_minibuffer (Qnil, kb, display);
3044 else if (EQ (tem, Qonly))
3046 f = make_minibuffer_frame ();
3047 minibuffer_only = 1;
3049 else if (WINDOWP (tem))
3050 f = make_frame_without_minibuffer (tem, kb, display);
3051 else
3052 f = make_frame (1);
3054 XSETFRAME (frame, f);
3056 f->terminal = dpyinfo->terminal;
3058 f->output_method = output_x_window;
3059 f->output_data.x = xzalloc (sizeof *f->output_data.x);
3060 f->output_data.x->icon_bitmap = -1;
3061 FRAME_FONTSET (f) = -1;
3062 f->output_data.x->scroll_bar_foreground_pixel = -1;
3063 f->output_data.x->scroll_bar_background_pixel = -1;
3064 #ifdef USE_TOOLKIT_SCROLL_BARS
3065 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
3066 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
3067 #endif /* USE_TOOLKIT_SCROLL_BARS */
3069 fset_icon_name (f,
3070 x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
3071 RES_TYPE_STRING));
3072 if (! STRINGP (f->icon_name))
3073 fset_icon_name (f, Qnil);
3075 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
3077 /* With FRAME_X_DISPLAY_INFO set up, this unwind-protect is safe. */
3078 record_unwind_protect (do_unwind_create_frame, frame);
3080 /* These colors will be set anyway later, but it's important
3081 to get the color reference counts right, so initialize them! */
3083 Lisp_Object black;
3084 struct gcpro gcpro1;
3086 /* Function x_decode_color can signal an error. Make
3087 sure to initialize color slots so that we won't try
3088 to free colors we haven't allocated. */
3089 FRAME_FOREGROUND_PIXEL (f) = -1;
3090 FRAME_BACKGROUND_PIXEL (f) = -1;
3091 f->output_data.x->cursor_pixel = -1;
3092 f->output_data.x->cursor_foreground_pixel = -1;
3093 f->output_data.x->border_pixel = -1;
3094 f->output_data.x->mouse_pixel = -1;
3096 black = build_string ("black");
3097 GCPRO1 (black);
3098 FRAME_FOREGROUND_PIXEL (f)
3099 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3100 FRAME_BACKGROUND_PIXEL (f)
3101 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3102 f->output_data.x->cursor_pixel
3103 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3104 f->output_data.x->cursor_foreground_pixel
3105 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3106 f->output_data.x->border_pixel
3107 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3108 f->output_data.x->mouse_pixel
3109 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3110 UNGCPRO;
3113 /* Specify the parent under which to make this X window. */
3115 if (!NILP (parent))
3117 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
3118 f->output_data.x->explicit_parent = 1;
3120 else
3122 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
3123 f->output_data.x->explicit_parent = 0;
3126 /* Set the name; the functions to which we pass f expect the name to
3127 be set. */
3128 if (EQ (name, Qunbound) || NILP (name))
3130 fset_name (f, build_string (dpyinfo->x_id_name));
3131 f->explicit_name = 0;
3133 else
3135 fset_name (f, name);
3136 f->explicit_name = 1;
3137 /* use the frame's title when getting resources for this frame. */
3138 specbind (Qx_resource_name, name);
3141 #ifdef HAVE_FREETYPE
3142 #ifdef HAVE_XFT
3143 register_font_driver (&xftfont_driver, f);
3144 #else /* not HAVE_XFT */
3145 register_font_driver (&ftxfont_driver, f);
3146 #endif /* not HAVE_XFT */
3147 #endif /* HAVE_FREETYPE */
3148 register_font_driver (&xfont_driver, f);
3150 x_default_parameter (f, parms, Qfont_backend, Qnil,
3151 "fontBackend", "FontBackend", RES_TYPE_STRING);
3153 /* Extract the window parameters from the supplied values
3154 that are needed to determine window geometry. */
3155 x_default_font_parameter (f, parms);
3156 if (!FRAME_FONT (f))
3158 delete_frame (frame, Qnoelisp);
3159 error ("Invalid frame font");
3162 /* Frame contents get displaced if an embedded X window has a border. */
3163 if (! FRAME_X_EMBEDDED_P (f))
3164 x_default_parameter (f, parms, Qborder_width, make_number (0),
3165 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
3167 /* This defaults to 1 in order to match xterm. We recognize either
3168 internalBorderWidth or internalBorder (which is what xterm calls
3169 it). */
3170 if (NILP (Fassq (Qinternal_border_width, parms)))
3172 Lisp_Object value;
3174 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
3175 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
3176 if (! EQ (value, Qunbound))
3177 parms = Fcons (Fcons (Qinternal_border_width, value),
3178 parms);
3180 x_default_parameter (f, parms, Qinternal_border_width,
3181 #ifdef USE_GTK /* We used to impose 0 in xg_create_frame_widgets. */
3182 make_number (0),
3183 #else
3184 make_number (1),
3185 #endif
3186 "internalBorderWidth", "internalBorderWidth",
3187 RES_TYPE_NUMBER);
3188 x_default_parameter (f, parms, Qvertical_scroll_bars,
3189 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3190 Qright,
3191 #else
3192 Qleft,
3193 #endif
3194 "verticalScrollBars", "ScrollBars",
3195 RES_TYPE_SYMBOL);
3197 /* Also do the stuff which must be set before the window exists. */
3198 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
3199 "foreground", "Foreground", RES_TYPE_STRING);
3200 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
3201 "background", "Background", RES_TYPE_STRING);
3202 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
3203 "pointerColor", "Foreground", RES_TYPE_STRING);
3204 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
3205 "borderColor", "BorderColor", RES_TYPE_STRING);
3206 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
3207 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
3208 x_default_parameter (f, parms, Qline_spacing, Qnil,
3209 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
3210 x_default_parameter (f, parms, Qleft_fringe, Qnil,
3211 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
3212 x_default_parameter (f, parms, Qright_fringe, Qnil,
3213 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
3215 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
3216 "scrollBarForeground",
3217 "ScrollBarForeground", 1);
3218 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
3219 "scrollBarBackground",
3220 "ScrollBarBackground", 0);
3222 #ifdef GLYPH_DEBUG
3223 image_cache_refcount =
3224 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
3225 dpyinfo_refcount = dpyinfo->reference_count;
3226 #endif /* GLYPH_DEBUG */
3228 /* Init faces before x_default_parameter is called for scroll-bar
3229 parameters because that function calls x_set_scroll_bar_width,
3230 which calls change_frame_size, which calls Fset_window_buffer,
3231 which runs hooks, which call Fvertical_motion. At the end, we
3232 end up in init_iterator with a null face cache, which should not
3233 happen. */
3234 init_frame_faces (f);
3236 /* Set the menu-bar-lines and tool-bar-lines parameters. We don't
3237 look up the X resources controlling the menu-bar and tool-bar
3238 here; they are processed specially at startup, and reflected in
3239 the values of the mode variables.
3241 Avoid calling window-configuration-change-hook; otherwise we
3242 could get an infloop in next_frame since the frame is not yet in
3243 Vframe_list. */
3245 ptrdiff_t count2 = SPECPDL_INDEX ();
3246 record_unwind_protect (unwind_create_frame_1, inhibit_lisp_code);
3247 inhibit_lisp_code = Qt;
3249 x_default_parameter (f, parms, Qmenu_bar_lines,
3250 NILP (Vmenu_bar_mode)
3251 ? make_number (0) : make_number (1),
3252 NULL, NULL, RES_TYPE_NUMBER);
3253 x_default_parameter (f, parms, Qtool_bar_lines,
3254 NILP (Vtool_bar_mode)
3255 ? make_number (0) : make_number (1),
3256 NULL, NULL, RES_TYPE_NUMBER);
3258 unbind_to (count2, Qnil);
3261 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
3262 "bufferPredicate", "BufferPredicate",
3263 RES_TYPE_SYMBOL);
3264 x_default_parameter (f, parms, Qtitle, Qnil,
3265 "title", "Title", RES_TYPE_STRING);
3266 x_default_parameter (f, parms, Qwait_for_wm, Qt,
3267 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN);
3268 x_default_parameter (f, parms, Qfullscreen, Qnil,
3269 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
3270 x_default_parameter (f, parms, Qtool_bar_position,
3271 f->tool_bar_position, 0, 0, RES_TYPE_SYMBOL);
3273 /* Compute the size of the X window. */
3274 window_prompting = x_figure_window_size (f, parms, 1);
3276 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
3277 f->no_split = minibuffer_only || EQ (tem, Qt);
3279 x_icon_verify (f, parms);
3281 /* Create the X widget or window. */
3282 #ifdef USE_X_TOOLKIT
3283 x_window (f, window_prompting, minibuffer_only);
3284 #else
3285 x_window (f);
3286 #endif
3288 x_icon (f, parms);
3289 x_make_gc (f);
3291 /* Now consider the frame official. */
3292 f->terminal->reference_count++;
3293 FRAME_X_DISPLAY_INFO (f)->reference_count++;
3294 Vframe_list = Fcons (frame, Vframe_list);
3296 /* We need to do this after creating the X window, so that the
3297 icon-creation functions can say whose icon they're describing. */
3298 x_default_parameter (f, parms, Qicon_type, Qt,
3299 "bitmapIcon", "BitmapIcon", RES_TYPE_BOOLEAN);
3301 x_default_parameter (f, parms, Qauto_raise, Qnil,
3302 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3303 x_default_parameter (f, parms, Qauto_lower, Qnil,
3304 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3305 x_default_parameter (f, parms, Qcursor_type, Qbox,
3306 "cursorType", "CursorType", RES_TYPE_SYMBOL);
3307 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
3308 "scrollBarWidth", "ScrollBarWidth",
3309 RES_TYPE_NUMBER);
3310 x_default_parameter (f, parms, Qalpha, Qnil,
3311 "alpha", "Alpha", RES_TYPE_NUMBER);
3313 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
3314 Change will not be effected unless different from the current
3315 FRAME_LINES (f). */
3316 width = FRAME_COLS (f);
3317 height = FRAME_LINES (f);
3319 SET_FRAME_COLS (f, 0);
3320 FRAME_LINES (f) = 0;
3321 change_frame_size (f, height, width, 1, 0, 0);
3323 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3324 /* Create the menu bar. */
3325 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3327 /* If this signals an error, we haven't set size hints for the
3328 frame and we didn't make it visible. */
3329 initialize_frame_menubar (f);
3331 #ifndef USE_GTK
3332 /* This is a no-op, except under Motif where it arranges the
3333 main window for the widgets on it. */
3334 lw_set_main_areas (f->output_data.x->column_widget,
3335 f->output_data.x->menubar_widget,
3336 f->output_data.x->edit_widget);
3337 #endif /* not USE_GTK */
3339 #endif /* USE_X_TOOLKIT || USE_GTK */
3341 /* Tell the server what size and position, etc, we want, and how
3342 badly we want them. This should be done after we have the menu
3343 bar so that its size can be taken into account. */
3344 block_input ();
3345 x_wm_set_size_hint (f, window_prompting, 0);
3346 unblock_input ();
3348 /* Make the window appear on the frame and enable display, unless
3349 the caller says not to. However, with explicit parent, Emacs
3350 cannot control visibility, so don't try. */
3351 if (! f->output_data.x->explicit_parent)
3353 Lisp_Object visibility;
3355 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
3356 RES_TYPE_SYMBOL);
3357 if (EQ (visibility, Qunbound))
3358 visibility = Qt;
3360 if (EQ (visibility, Qicon))
3361 x_iconify_frame (f);
3362 else if (! NILP (visibility))
3363 x_make_frame_visible (f);
3364 else
3366 /* Must have been Qnil. */
3370 block_input ();
3372 /* Set machine name and pid for the purpose of window managers. */
3373 set_machine_and_pid_properties (f);
3375 /* Set the WM leader property. GTK does this itself, so this is not
3376 needed when using GTK. */
3377 if (dpyinfo->client_leader_window != 0)
3379 XChangeProperty (FRAME_X_DISPLAY (f),
3380 FRAME_OUTER_WINDOW (f),
3381 dpyinfo->Xatom_wm_client_leader,
3382 XA_WINDOW, 32, PropModeReplace,
3383 (unsigned char *) &dpyinfo->client_leader_window, 1);
3386 unblock_input ();
3388 /* Initialize `default-minibuffer-frame' in case this is the first
3389 frame on this terminal. */
3390 if (FRAME_HAS_MINIBUF_P (f)
3391 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
3392 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
3393 kset_default_minibuffer_frame (kb, frame);
3395 /* All remaining specified parameters, which have not been "used"
3396 by x_get_arg and friends, now go in the misc. alist of the frame. */
3397 for (tem = parms; CONSP (tem); tem = XCDR (tem))
3398 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
3399 fset_param_alist (f, Fcons (XCAR (tem), f->param_alist));
3401 UNGCPRO;
3403 /* Make sure windows on this frame appear in calls to next-window
3404 and similar functions. */
3405 Vwindow_list = Qnil;
3407 return unbind_to (count, frame);
3411 /* FRAME is used only to get a handle on the X display. We don't pass the
3412 display info directly because we're called from frame.c, which doesn't
3413 know about that structure. */
3415 Lisp_Object
3416 x_get_focus_frame (struct frame *frame)
3418 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (frame);
3419 Lisp_Object xfocus;
3420 if (! dpyinfo->x_focus_frame)
3421 return Qnil;
3423 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
3424 return xfocus;
3428 /* In certain situations, when the window manager follows a
3429 click-to-focus policy, there seems to be no way around calling
3430 XSetInputFocus to give another frame the input focus .
3432 In an ideal world, XSetInputFocus should generally be avoided so
3433 that applications don't interfere with the window manager's focus
3434 policy. But I think it's okay to use when it's clearly done
3435 following a user-command. */
3437 DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
3438 doc: /* Set the input focus to FRAME.
3439 FRAME nil means use the selected frame. */)
3440 (Lisp_Object frame)
3442 struct frame *f = decode_window_system_frame (frame);
3443 Display *dpy = FRAME_X_DISPLAY (f);
3445 block_input ();
3446 x_catch_errors (dpy);
3448 if (FRAME_X_EMBEDDED_P (f))
3450 /* For Xembedded frames, normally the embedder forwards key
3451 events. See XEmbed Protocol Specification at
3452 http://freedesktop.org/wiki/Specifications/xembed-spec */
3453 xembed_request_focus (f);
3455 else
3457 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3458 RevertToParent, CurrentTime);
3459 x_ewmh_activate_frame (f);
3462 x_uncatch_errors ();
3463 unblock_input ();
3465 return Qnil;
3469 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
3470 doc: /* Internal function called by `color-defined-p', which see
3471 .\(Note that the Nextstep version of this function ignores FRAME.) */)
3472 (Lisp_Object color, Lisp_Object frame)
3474 XColor foo;
3475 struct frame *f = decode_window_system_frame (frame);
3477 CHECK_STRING (color);
3479 if (x_defined_color (f, SSDATA (color), &foo, 0))
3480 return Qt;
3481 else
3482 return Qnil;
3485 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
3486 doc: /* Internal function called by `color-values', which see. */)
3487 (Lisp_Object color, Lisp_Object frame)
3489 XColor foo;
3490 struct frame *f = decode_window_system_frame (frame);
3492 CHECK_STRING (color);
3494 if (x_defined_color (f, SSDATA (color), &foo, 0))
3495 return list3i (foo.red, foo.green, foo.blue);
3496 else
3497 return Qnil;
3500 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
3501 doc: /* Internal function called by `display-color-p', which see. */)
3502 (Lisp_Object terminal)
3504 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3506 if (dpyinfo->n_planes <= 2)
3507 return Qnil;
3509 switch (dpyinfo->visual->class)
3511 case StaticColor:
3512 case PseudoColor:
3513 case TrueColor:
3514 case DirectColor:
3515 return Qt;
3517 default:
3518 return Qnil;
3522 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
3523 0, 1, 0,
3524 doc: /* Return t if the X display supports shades of gray.
3525 Note that color displays do support shades of gray.
3526 The optional argument TERMINAL specifies which display to ask about.
3527 TERMINAL should be a terminal object, a frame or a display name (a string).
3528 If omitted or nil, that stands for the selected frame's display. */)
3529 (Lisp_Object terminal)
3531 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3533 if (dpyinfo->n_planes <= 1)
3534 return Qnil;
3536 switch (dpyinfo->visual->class)
3538 case StaticColor:
3539 case PseudoColor:
3540 case TrueColor:
3541 case DirectColor:
3542 case StaticGray:
3543 case GrayScale:
3544 return Qt;
3546 default:
3547 return Qnil;
3551 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
3552 0, 1, 0,
3553 doc: /* Return the width in pixels of the X display TERMINAL.
3554 The optional argument TERMINAL specifies which display to ask about.
3555 TERMINAL should be a terminal object, a frame or a display name (a string).
3556 If omitted or nil, that stands for the selected frame's display.
3558 On \"multi-monitor\" setups this refers to the pixel width for all
3559 physical monitors associated with TERMINAL. To get information for
3560 each physical monitor, use `display-monitor-attributes-list'. */)
3561 (Lisp_Object terminal)
3563 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3565 return make_number (x_display_pixel_width (dpyinfo));
3568 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
3569 Sx_display_pixel_height, 0, 1, 0,
3570 doc: /* Return the height in pixels of the X display TERMINAL.
3571 The optional argument TERMINAL specifies which display to ask about.
3572 TERMINAL should be a terminal object, a frame or a display name (a string).
3573 If omitted or nil, that stands for the selected frame's display.
3575 On \"multi-monitor\" setups this refers to the pixel height for all
3576 physical monitors associated with TERMINAL. To get information for
3577 each physical monitor, use `display-monitor-attributes-list'. */)
3578 (Lisp_Object terminal)
3580 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3582 return make_number (x_display_pixel_height (dpyinfo));
3585 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
3586 0, 1, 0,
3587 doc: /* Return the number of bitplanes of the X display TERMINAL.
3588 The optional argument TERMINAL specifies which display to ask about.
3589 TERMINAL should be a terminal object, a frame or a display name (a string).
3590 If omitted or nil, that stands for the selected frame's display. */)
3591 (Lisp_Object terminal)
3593 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3595 return make_number (dpyinfo->n_planes);
3598 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
3599 0, 1, 0,
3600 doc: /* Return the number of color cells 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 int nr_planes = DisplayPlanes (dpyinfo->display,
3609 XScreenNumberOfScreen (dpyinfo->screen));
3611 /* Truncate nr_planes to 24 to avoid integer overflow.
3612 Some displays says 32, but only 24 bits are actually significant.
3613 There are only very few and rare video cards that have more than
3614 24 significant bits. Also 24 bits is more than 16 million colors,
3615 it "should be enough for everyone". */
3616 if (nr_planes > 24) nr_planes = 24;
3618 return make_number (1 << nr_planes);
3621 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
3622 Sx_server_max_request_size,
3623 0, 1, 0,
3624 doc: /* Return the maximum request size of the X server of display TERMINAL.
3625 The optional argument TERMINAL specifies which display to ask about.
3626 TERMINAL should be a terminal object, a frame or a display name (a string).
3627 If omitted or nil, that stands for the selected frame's display. */)
3628 (Lisp_Object terminal)
3630 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3632 return make_number (MAXREQUEST (dpyinfo->display));
3635 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
3636 doc: /* Return the "vendor ID" string of the X server of display TERMINAL.
3637 \(Labeling every distributor as a "vendor" embodies the false assumption
3638 that operating systems cannot be developed and distributed noncommercially.)
3639 The optional argument TERMINAL specifies which display to ask about.
3640 TERMINAL should be a terminal object, a frame or a display name (a string).
3641 If omitted or nil, that stands for the selected frame's display. */)
3642 (Lisp_Object terminal)
3644 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3645 const char *vendor = ServerVendor (dpyinfo->display);
3647 if (! vendor) vendor = "";
3648 return build_string (vendor);
3651 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
3652 doc: /* Return the version numbers of the X server of display TERMINAL.
3653 The value is a list of three integers: the major and minor
3654 version numbers of the X Protocol in use, and the distributor-specific release
3655 number. See also the function `x-server-vendor'.
3657 The optional argument TERMINAL specifies which display to ask about.
3658 TERMINAL should be a terminal object, a frame or a display name (a string).
3659 If omitted or nil, that stands for the selected frame's display. */)
3660 (Lisp_Object terminal)
3662 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3663 Display *dpy = dpyinfo->display;
3665 return list3i (ProtocolVersion (dpy), ProtocolRevision (dpy),
3666 VendorRelease (dpy));
3669 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
3670 doc: /* Return the number of screens on the X server of display TERMINAL.
3671 The optional argument TERMINAL specifies which display to ask about.
3672 TERMINAL should be a terminal object, a frame or a display name (a string).
3673 If omitted or nil, that stands for the selected frame's display. */)
3674 (Lisp_Object terminal)
3676 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3678 return make_number (ScreenCount (dpyinfo->display));
3681 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
3682 doc: /* Return the height in millimeters of the X display TERMINAL.
3683 The optional argument TERMINAL specifies which display to ask about.
3684 TERMINAL should be a terminal object, a frame or a display name (a string).
3685 If omitted or nil, that stands for the selected frame's display.
3687 On \"multi-monitor\" setups this refers to the height in millimeters for
3688 all physical monitors associated with TERMINAL. To get information
3689 for each physical monitor, use `display-monitor-attributes-list'. */)
3690 (Lisp_Object terminal)
3692 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3694 return make_number (HeightMMOfScreen (dpyinfo->screen));
3697 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
3698 doc: /* Return the width in millimeters of the X display TERMINAL.
3699 The optional argument TERMINAL specifies which display to ask about.
3700 TERMINAL should be a terminal object, a frame or a display name (a string).
3701 If omitted or nil, that stands for the selected frame's display.
3703 On \"multi-monitor\" setups this refers to the width in millimeters for
3704 all physical monitors associated with TERMINAL. To get information
3705 for each physical monitor, use `display-monitor-attributes-list'. */)
3706 (Lisp_Object terminal)
3708 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3710 return make_number (WidthMMOfScreen (dpyinfo->screen));
3713 DEFUN ("x-display-backing-store", Fx_display_backing_store,
3714 Sx_display_backing_store, 0, 1, 0,
3715 doc: /* Return an indication of whether X display TERMINAL does backing store.
3716 The value may be `always', `when-mapped', or `not-useful'.
3717 The optional argument TERMINAL specifies which display to ask about.
3718 TERMINAL should be a terminal object, a frame or a display name (a string).
3719 If omitted or nil, that stands for the selected frame's display. */)
3720 (Lisp_Object terminal)
3722 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3723 Lisp_Object result;
3725 switch (DoesBackingStore (dpyinfo->screen))
3727 case Always:
3728 result = intern ("always");
3729 break;
3731 case WhenMapped:
3732 result = intern ("when-mapped");
3733 break;
3735 case NotUseful:
3736 result = intern ("not-useful");
3737 break;
3739 default:
3740 error ("Strange value for BackingStore parameter of screen");
3743 return result;
3746 DEFUN ("x-display-visual-class", Fx_display_visual_class,
3747 Sx_display_visual_class, 0, 1, 0,
3748 doc: /* Return the visual class of the X display TERMINAL.
3749 The value is one of the symbols `static-gray', `gray-scale',
3750 `static-color', `pseudo-color', `true-color', or `direct-color'.
3752 The optional argument TERMINAL specifies which display to ask about.
3753 TERMINAL should a terminal object, a frame or a display name (a string).
3754 If omitted or nil, that stands for the selected frame's display. */)
3755 (Lisp_Object terminal)
3757 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3758 Lisp_Object result;
3760 switch (dpyinfo->visual->class)
3762 case StaticGray:
3763 result = intern ("static-gray");
3764 break;
3765 case GrayScale:
3766 result = intern ("gray-scale");
3767 break;
3768 case StaticColor:
3769 result = intern ("static-color");
3770 break;
3771 case PseudoColor:
3772 result = intern ("pseudo-color");
3773 break;
3774 case TrueColor:
3775 result = intern ("true-color");
3776 break;
3777 case DirectColor:
3778 result = intern ("direct-color");
3779 break;
3780 default:
3781 error ("Display has an unknown visual class");
3784 return result;
3787 DEFUN ("x-display-save-under", Fx_display_save_under,
3788 Sx_display_save_under, 0, 1, 0,
3789 doc: /* Return t if the X display TERMINAL supports the save-under feature.
3790 The optional argument TERMINAL specifies which display to ask about.
3791 TERMINAL should be a terminal object, a frame or a display name (a string).
3792 If omitted or nil, that stands for the selected frame's display. */)
3793 (Lisp_Object terminal)
3795 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3797 if (DoesSaveUnders (dpyinfo->screen) == True)
3798 return Qt;
3799 else
3800 return Qnil;
3803 /* Store the geometry of the workarea on display DPYINFO into *RECT.
3804 Return false if and only if the workarea information cannot be
3805 obtained via the _NET_WORKAREA root window property. */
3807 #if ! GTK_CHECK_VERSION (3, 4, 0)
3808 static bool
3809 x_get_net_workarea (struct x_display_info *dpyinfo, XRectangle *rect)
3811 Display *dpy = dpyinfo->display;
3812 long offset, max_len;
3813 Atom target_type, actual_type;
3814 unsigned long actual_size, bytes_remaining;
3815 int rc, actual_format;
3816 unsigned char *tmp_data = NULL;
3817 bool result = false;
3819 x_catch_errors (dpy);
3820 offset = 0;
3821 max_len = 1;
3822 target_type = XA_CARDINAL;
3823 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3824 dpyinfo->Xatom_net_current_desktop,
3825 offset, max_len, False, target_type,
3826 &actual_type, &actual_format, &actual_size,
3827 &bytes_remaining, &tmp_data);
3828 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3829 && actual_format == 32 && actual_size == max_len)
3831 long current_desktop = ((long *) tmp_data)[0];
3833 XFree (tmp_data);
3834 tmp_data = NULL;
3836 offset = 4 * current_desktop;
3837 max_len = 4;
3838 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3839 dpyinfo->Xatom_net_workarea,
3840 offset, max_len, False, target_type,
3841 &actual_type, &actual_format, &actual_size,
3842 &bytes_remaining, &tmp_data);
3843 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3844 && actual_format == 32 && actual_size == max_len)
3846 long *values = (long *) tmp_data;
3848 rect->x = values[0];
3849 rect->y = values[1];
3850 rect->width = values[2];
3851 rect->height = values[3];
3853 XFree (tmp_data);
3854 tmp_data = NULL;
3856 result = true;
3859 if (tmp_data)
3860 XFree (tmp_data);
3861 x_uncatch_errors ();
3863 return result;
3865 #endif
3867 #ifndef USE_GTK
3869 /* Return monitor number where F is "most" or closest to. */
3870 static int
3871 x_get_monitor_for_frame (struct frame *f,
3872 struct MonitorInfo *monitors,
3873 int n_monitors)
3875 XRectangle frect;
3876 int area = 0, dist = -1;
3877 int best_area = -1, best_dist = -1;
3878 int i;
3880 if (n_monitors == 1) return 0;
3881 frect.x = f->left_pos;
3882 frect.y = f->top_pos;
3883 frect.width = FRAME_PIXEL_WIDTH (f);
3884 frect.height = FRAME_PIXEL_HEIGHT (f);
3886 for (i = 0; i < n_monitors; ++i)
3888 struct MonitorInfo *mi = &monitors[i];
3889 XRectangle res;
3890 int a = 0;
3892 if (mi->geom.width == 0) continue;
3894 if (x_intersect_rectangles (&mi->geom, &frect, &res))
3896 a = res.width * res.height;
3897 if (a > area)
3899 area = a;
3900 best_area = i;
3904 if (a == 0 && area == 0)
3906 int dx, dy, d;
3907 if (frect.x + frect.width < mi->geom.x)
3908 dx = mi->geom.x - frect.x + frect.width;
3909 else if (frect.x > mi->geom.x + mi->geom.width)
3910 dx = frect.x - mi->geom.x + mi->geom.width;
3911 else
3912 dx = 0;
3913 if (frect.y + frect.height < mi->geom.y)
3914 dy = mi->geom.y - frect.y + frect.height;
3915 else if (frect.y > mi->geom.y + mi->geom.height)
3916 dy = frect.y - mi->geom.y + mi->geom.height;
3917 else
3918 dy = 0;
3920 d = dx*dx + dy*dy;
3921 if (dist == -1 || dist > d)
3923 dist = d;
3924 best_dist = i;
3929 return best_area != -1 ? best_area : (best_dist != -1 ? best_dist : 0);
3932 static Lisp_Object
3933 x_make_monitor_attribute_list (struct MonitorInfo *monitors,
3934 int n_monitors,
3935 int primary_monitor,
3936 struct x_display_info *dpyinfo,
3937 const char *source)
3939 Lisp_Object monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
3940 Lisp_Object frame, rest;
3942 FOR_EACH_FRAME (rest, frame)
3944 struct frame *f = XFRAME (frame);
3946 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo
3947 && !EQ (frame, tip_frame))
3949 int i = x_get_monitor_for_frame (f, monitors, n_monitors);
3950 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
3954 return make_monitor_attribute_list (monitors, n_monitors, primary_monitor,
3955 monitor_frames, source);
3958 static Lisp_Object
3959 x_get_monitor_attributes_fallback (struct x_display_info *dpyinfo)
3961 struct MonitorInfo monitor;
3962 XRectangle workarea_r;
3964 /* Fallback: treat (possibly) multiple physical monitors as if they
3965 formed a single monitor as a whole. This should provide a
3966 consistent result at least on single monitor environments. */
3967 monitor.geom.x = monitor.geom.y = 0;
3968 monitor.geom.width = x_display_pixel_width (dpyinfo);
3969 monitor.geom.height = x_display_pixel_height (dpyinfo);
3970 monitor.mm_width = WidthMMOfScreen (dpyinfo->screen);
3971 monitor.mm_height = HeightMMOfScreen (dpyinfo->screen);
3972 monitor.name = xstrdup ("combined screen");
3974 if (x_get_net_workarea (dpyinfo, &workarea_r))
3975 monitor.work = workarea_r;
3976 else
3977 monitor.work = monitor.geom;
3978 return x_make_monitor_attribute_list (&monitor, 1, 0, dpyinfo, "fallback");
3982 #ifdef HAVE_XINERAMA
3983 static Lisp_Object
3984 x_get_monitor_attributes_xinerama (struct x_display_info *dpyinfo)
3986 int n_monitors, i;
3987 Lisp_Object attributes_list = Qnil;
3988 Display *dpy = dpyinfo->display;
3989 XineramaScreenInfo *info = XineramaQueryScreens (dpy, &n_monitors);
3990 struct MonitorInfo *monitors;
3991 double mm_width_per_pixel, mm_height_per_pixel;
3993 if (! info || n_monitors == 0)
3995 if (info)
3996 XFree (info);
3997 return attributes_list;
4000 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
4001 / x_display_pixel_width (dpyinfo));
4002 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
4003 / x_display_pixel_height (dpyinfo));
4004 monitors = xzalloc (n_monitors * sizeof *monitors);
4005 for (i = 0; i < n_monitors; ++i)
4007 struct MonitorInfo *mi = &monitors[i];
4008 XRectangle workarea_r;
4010 mi->geom.x = info[i].x_org;
4011 mi->geom.y = info[i].y_org;
4012 mi->geom.width = info[i].width;
4013 mi->geom.height = info[i].height;
4014 mi->mm_width = mi->geom.width * mm_width_per_pixel + 0.5;
4015 mi->mm_height = mi->geom.height * mm_height_per_pixel + 0.5;
4016 mi->name = 0;
4018 /* Xinerama usually have primary monitor first, just use that. */
4019 if (i == 0 && x_get_net_workarea (dpyinfo, &workarea_r))
4021 mi->work = workarea_r;
4022 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
4023 mi->work = mi->geom;
4025 else
4026 mi->work = mi->geom;
4028 XFree (info);
4030 attributes_list = x_make_monitor_attribute_list (monitors,
4031 n_monitors,
4033 dpyinfo,
4034 "Xinerama");
4035 free_monitors (monitors, n_monitors);
4036 return attributes_list;
4038 #endif /* HAVE_XINERAMA */
4041 #ifdef HAVE_XRANDR
4042 static Lisp_Object
4043 x_get_monitor_attributes_xrandr (struct x_display_info *dpyinfo)
4045 Lisp_Object attributes_list = Qnil;
4046 XRRScreenResources *resources;
4047 Display *dpy = dpyinfo->display;
4048 int i, n_monitors, primary = -1;
4049 RROutput pxid = None;
4050 struct MonitorInfo *monitors;
4052 #ifdef HAVE_XRRGETSCREENRESOURCESCURRENT
4053 resources = XRRGetScreenResourcesCurrent (dpy, dpyinfo->root_window);
4054 #else
4055 resources = XRRGetScreenResources (dpy, dpyinfo->root_window);
4056 #endif
4057 if (! resources || resources->noutput == 0)
4059 if (resources)
4060 XRRFreeScreenResources (resources);
4061 return Qnil;
4063 n_monitors = resources->noutput;
4064 monitors = xzalloc (n_monitors * sizeof *monitors);
4066 #ifdef HAVE_XRRGETOUTPUTPRIMARY
4067 pxid = XRRGetOutputPrimary (dpy, dpyinfo->root_window);
4068 #endif
4070 for (i = 0; i < n_monitors; ++i)
4072 XRROutputInfo *info = XRRGetOutputInfo (dpy, resources,
4073 resources->outputs[i]);
4074 Connection conn = info ? info->connection : RR_Disconnected;
4075 RRCrtc id = info ? info->crtc : None;
4077 if (strcmp (info->name, "default") == 0)
4079 /* Non XRandr 1.2 driver, does not give useful data. */
4080 XRRFreeOutputInfo (info);
4081 XRRFreeScreenResources (resources);
4082 free_monitors (monitors, n_monitors);
4083 return Qnil;
4086 if (conn != RR_Disconnected && id != None)
4088 XRRCrtcInfo *crtc = XRRGetCrtcInfo (dpy, resources, id);
4089 struct MonitorInfo *mi = &monitors[i];
4090 XRectangle workarea_r;
4092 if (! crtc)
4094 XRRFreeOutputInfo (info);
4095 continue;
4098 mi->geom.x = crtc->x;
4099 mi->geom.y = crtc->y;
4100 mi->geom.width = crtc->width;
4101 mi->geom.height = crtc->height;
4102 mi->mm_width = info->mm_width;
4103 mi->mm_height = info->mm_height;
4104 mi->name = xstrdup (info->name);
4106 if (pxid != None && pxid == resources->outputs[i])
4107 primary = i;
4108 else if (primary == -1 && strcmp (info->name, "LVDS") == 0)
4109 primary = i;
4111 if (i == primary && x_get_net_workarea (dpyinfo, &workarea_r))
4113 mi->work= workarea_r;
4114 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
4115 mi->work = mi->geom;
4117 else
4118 mi->work = mi->geom;
4120 XRRFreeCrtcInfo (crtc);
4122 XRRFreeOutputInfo (info);
4124 XRRFreeScreenResources (resources);
4126 attributes_list = x_make_monitor_attribute_list (monitors,
4127 n_monitors,
4128 primary,
4129 dpyinfo,
4130 "XRandr");
4131 free_monitors (monitors, n_monitors);
4132 return attributes_list;
4134 #endif /* HAVE_XRANDR */
4136 static Lisp_Object
4137 x_get_monitor_attributes (struct x_display_info *dpyinfo)
4139 Lisp_Object attributes_list = Qnil;
4140 Display *dpy = dpyinfo->display;
4142 #ifdef HAVE_XRANDR
4143 int xrr_event_base, xrr_error_base;
4144 bool xrr_ok = false;
4145 xrr_ok = XRRQueryExtension (dpy, &xrr_event_base, &xrr_error_base);
4146 if (xrr_ok)
4148 int xrr_major, xrr_minor;
4149 XRRQueryVersion (dpy, &xrr_major, &xrr_minor);
4150 xrr_ok = (xrr_major == 1 && xrr_minor >= 2) || xrr_major > 1;
4153 if (xrr_ok)
4154 attributes_list = x_get_monitor_attributes_xrandr (dpyinfo);
4155 #endif /* HAVE_XRANDR */
4157 #ifdef HAVE_XINERAMA
4158 if (NILP (attributes_list))
4160 int xin_event_base, xin_error_base;
4161 bool xin_ok = false;
4162 xin_ok = XineramaQueryExtension (dpy, &xin_event_base, &xin_error_base);
4163 if (xin_ok && XineramaIsActive (dpy))
4164 attributes_list = x_get_monitor_attributes_xinerama (dpyinfo);
4166 #endif /* HAVE_XINERAMA */
4168 if (NILP (attributes_list))
4169 attributes_list = x_get_monitor_attributes_fallback (dpyinfo);
4171 return attributes_list;
4174 #endif /* !USE_GTK */
4176 DEFUN ("x-display-monitor-attributes-list", Fx_display_monitor_attributes_list,
4177 Sx_display_monitor_attributes_list,
4178 0, 1, 0,
4179 doc: /* Return a list of physical monitor attributes on the X display TERMINAL.
4181 The optional argument TERMINAL specifies which display to ask about.
4182 TERMINAL should be a terminal object, a frame or a display name (a string).
4183 If omitted or nil, that stands for the selected frame's display.
4185 In addition to the standard attribute keys listed in
4186 `display-monitor-attributes-list', the following keys are contained in
4187 the attributes:
4189 source -- String describing the source from which multi-monitor
4190 information is obtained, one of \"Gdk\", \"XRandr\",
4191 \"Xinerama\", or \"fallback\"
4193 Internal use only, use `display-monitor-attributes-list' instead. */)
4194 (Lisp_Object terminal)
4196 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4197 Lisp_Object attributes_list = Qnil;
4199 #ifdef USE_GTK
4200 double mm_width_per_pixel, mm_height_per_pixel;
4201 GdkDisplay *gdpy;
4202 GdkScreen *gscreen;
4203 gint primary_monitor = 0, n_monitors, i;
4204 Lisp_Object monitor_frames, rest, frame;
4205 static const char *source = "Gdk";
4206 struct MonitorInfo *monitors;
4208 block_input ();
4209 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
4210 / x_display_pixel_width (dpyinfo));
4211 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
4212 / x_display_pixel_height (dpyinfo));
4213 gdpy = gdk_x11_lookup_xdisplay (dpyinfo->display);
4214 gscreen = gdk_display_get_default_screen (gdpy);
4215 #if GTK_CHECK_VERSION (2, 20, 0)
4216 primary_monitor = gdk_screen_get_primary_monitor (gscreen);
4217 #endif
4218 n_monitors = gdk_screen_get_n_monitors (gscreen);
4219 monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
4220 monitors = xzalloc (n_monitors * sizeof *monitors);
4222 FOR_EACH_FRAME (rest, frame)
4224 struct frame *f = XFRAME (frame);
4226 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo
4227 && !EQ (frame, tip_frame))
4229 GdkWindow *gwin = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
4231 i = gdk_screen_get_monitor_at_window (gscreen, gwin);
4232 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
4236 for (i = 0; i < n_monitors; ++i)
4238 gint width_mm = -1, height_mm = -1;
4239 GdkRectangle rec, work;
4240 struct MonitorInfo *mi = &monitors[i];
4242 gdk_screen_get_monitor_geometry (gscreen, i, &rec);
4244 #if GTK_CHECK_VERSION (2, 14, 0)
4245 width_mm = gdk_screen_get_monitor_width_mm (gscreen, i);
4246 height_mm = gdk_screen_get_monitor_height_mm (gscreen, i);
4247 #endif
4248 if (width_mm < 0)
4249 width_mm = rec.width * mm_width_per_pixel + 0.5;
4250 if (height_mm < 0)
4251 height_mm = rec.height * mm_height_per_pixel + 0.5;
4253 #if GTK_CHECK_VERSION (3, 4, 0)
4254 gdk_screen_get_monitor_workarea (gscreen, i, &work);
4255 #else
4256 /* Emulate the behavior of GTK+ 3.4. */
4258 XRectangle workarea_r;
4260 if (i == primary_monitor && x_get_net_workarea (dpyinfo, &workarea_r))
4262 work.x = workarea_r.x;
4263 work.y = workarea_r.y;
4264 work.width = workarea_r.width;
4265 work.height = workarea_r.height;
4266 if (! gdk_rectangle_intersect (&rec, &work, &work))
4267 work = rec;
4269 else
4270 work = rec;
4272 #endif
4275 mi->geom.x = rec.x;
4276 mi->geom.y = rec.y;
4277 mi->geom.width = rec.width;
4278 mi->geom.height = rec.height;
4279 mi->work.x = work.x;
4280 mi->work.y = work.y;
4281 mi->work.width = work.width;
4282 mi->work.height = work.height;
4283 mi->mm_width = width_mm;
4284 mi->mm_height = height_mm;
4286 #if GTK_CHECK_VERSION (2, 14, 0)
4287 mi->name = gdk_screen_get_monitor_plug_name (gscreen, i);
4288 #endif
4291 attributes_list = make_monitor_attribute_list (monitors,
4292 n_monitors,
4293 primary_monitor,
4294 monitor_frames,
4295 source);
4296 unblock_input ();
4297 #else /* not USE_GTK */
4299 block_input ();
4300 attributes_list = x_get_monitor_attributes (dpyinfo);
4301 unblock_input ();
4303 #endif /* not USE_GTK */
4305 return attributes_list;
4308 /************************************************************************
4309 X Displays
4310 ************************************************************************/
4313 /* Mapping visual names to visuals. */
4315 static struct visual_class
4317 const char *name;
4318 int class;
4320 visual_classes[] =
4322 {"StaticGray", StaticGray},
4323 {"GrayScale", GrayScale},
4324 {"StaticColor", StaticColor},
4325 {"PseudoColor", PseudoColor},
4326 {"TrueColor", TrueColor},
4327 {"DirectColor", DirectColor},
4328 {NULL, 0}
4332 #ifndef HAVE_XSCREENNUMBEROFSCREEN
4334 /* Value is the screen number of screen SCR. This is a substitute for
4335 the X function with the same name when that doesn't exist. */
4338 XScreenNumberOfScreen (scr)
4339 register Screen *scr;
4341 Display *dpy = scr->display;
4342 int i;
4344 for (i = 0; i < dpy->nscreens; ++i)
4345 if (scr == dpy->screens + i)
4346 break;
4348 return i;
4351 #endif /* not HAVE_XSCREENNUMBEROFSCREEN */
4354 /* Select the visual that should be used on display DPYINFO. Set
4355 members of DPYINFO appropriately. Called from x_term_init. */
4357 void
4358 select_visual (struct x_display_info *dpyinfo)
4360 Display *dpy = dpyinfo->display;
4361 Screen *screen = dpyinfo->screen;
4362 Lisp_Object value;
4364 /* See if a visual is specified. */
4365 value = display_x_get_resource (dpyinfo,
4366 build_string ("visualClass"),
4367 build_string ("VisualClass"),
4368 Qnil, Qnil);
4369 if (STRINGP (value))
4371 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
4372 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
4373 depth, a decimal number. NAME is compared with case ignored. */
4374 char *s = alloca (SBYTES (value) + 1);
4375 char *dash;
4376 int i, class = -1;
4377 XVisualInfo vinfo;
4379 strcpy (s, SSDATA (value));
4380 dash = strchr (s, '-');
4381 if (dash)
4383 dpyinfo->n_planes = atoi (dash + 1);
4384 *dash = '\0';
4386 else
4387 /* We won't find a matching visual with depth 0, so that
4388 an error will be printed below. */
4389 dpyinfo->n_planes = 0;
4391 /* Determine the visual class. */
4392 for (i = 0; visual_classes[i].name; ++i)
4393 if (xstrcasecmp (s, visual_classes[i].name) == 0)
4395 class = visual_classes[i].class;
4396 break;
4399 /* Look up a matching visual for the specified class. */
4400 if (class == -1
4401 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
4402 dpyinfo->n_planes, class, &vinfo))
4403 fatal ("Invalid visual specification `%s'", SDATA (value));
4405 dpyinfo->visual = vinfo.visual;
4407 else
4409 int n_visuals;
4410 XVisualInfo *vinfo, vinfo_template;
4412 dpyinfo->visual = DefaultVisualOfScreen (screen);
4414 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
4415 vinfo_template.screen = XScreenNumberOfScreen (screen);
4416 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
4417 &vinfo_template, &n_visuals);
4418 if (n_visuals <= 0)
4419 fatal ("Can't get proper X visual info");
4421 dpyinfo->n_planes = vinfo->depth;
4422 XFree (vinfo);
4427 /* Return the X display structure for the display named NAME.
4428 Open a new connection if necessary. */
4430 static struct x_display_info *
4431 x_display_info_for_name (Lisp_Object name)
4433 Lisp_Object names;
4434 struct x_display_info *dpyinfo;
4436 CHECK_STRING (name);
4438 #if 0
4439 if (! EQ (Vinitial_window_system, intern ("x")))
4440 error ("Not using X Windows"); /* That doesn't stop us anymore. */
4441 #endif
4443 for (dpyinfo = x_display_list, names = x_display_name_list;
4444 dpyinfo;
4445 dpyinfo = dpyinfo->next, names = XCDR (names))
4447 Lisp_Object tem;
4448 tem = Fstring_equal (XCAR (XCAR (names)), name);
4449 if (!NILP (tem))
4450 return dpyinfo;
4453 /* Use this general default value to start with. */
4454 Vx_resource_name = Vinvocation_name;
4456 validate_x_resource_name ();
4458 dpyinfo = x_term_init (name, 0, SSDATA (Vx_resource_name));
4460 if (dpyinfo == 0)
4461 error ("Cannot connect to X server %s", SDATA (name));
4463 XSETFASTINT (Vwindow_system_version, 11);
4465 return dpyinfo;
4469 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
4470 1, 3, 0,
4471 doc: /* Open a connection to a display server.
4472 DISPLAY is the name of the display to connect to.
4473 Optional second arg XRM-STRING is a string of resources in xrdb format.
4474 If the optional third arg MUST-SUCCEED is non-nil,
4475 terminate Emacs if we can't open the connection.
4476 \(In the Nextstep version, the last two arguments are currently ignored.) */)
4477 (Lisp_Object display, Lisp_Object xrm_string, Lisp_Object must_succeed)
4479 char *xrm_option;
4480 struct x_display_info *dpyinfo;
4482 CHECK_STRING (display);
4483 if (! NILP (xrm_string))
4484 CHECK_STRING (xrm_string);
4486 #if 0
4487 if (! EQ (Vinitial_window_system, intern ("x")))
4488 error ("Not using X Windows"); /* That doesn't stop us anymore. */
4489 #endif
4491 xrm_option = NILP (xrm_string) ? 0 : SSDATA (xrm_string);
4493 validate_x_resource_name ();
4495 /* This is what opens the connection and sets x_current_display.
4496 This also initializes many symbols, such as those used for input. */
4497 dpyinfo = x_term_init (display, xrm_option,
4498 SSDATA (Vx_resource_name));
4500 if (dpyinfo == 0)
4502 if (!NILP (must_succeed))
4503 fatal ("Cannot connect to X server %s.\n\
4504 Check the DISPLAY environment variable or use `-d'.\n\
4505 Also use the `xauth' program to verify that you have the proper\n\
4506 authorization information needed to connect the X server.\n\
4507 An insecure way to solve the problem may be to use `xhost'.\n",
4508 SDATA (display));
4509 else
4510 error ("Cannot connect to X server %s", SDATA (display));
4513 XSETFASTINT (Vwindow_system_version, 11);
4514 return Qnil;
4517 DEFUN ("x-close-connection", Fx_close_connection,
4518 Sx_close_connection, 1, 1, 0,
4519 doc: /* Close the connection to TERMINAL's X server.
4520 For TERMINAL, specify a terminal object, a frame or a display name (a
4521 string). If TERMINAL is nil, that stands for the selected frame's
4522 terminal. */)
4523 (Lisp_Object terminal)
4525 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4527 if (dpyinfo->reference_count > 0)
4528 error ("Display still has frames on it");
4530 x_delete_terminal (dpyinfo->terminal);
4532 return Qnil;
4535 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
4536 doc: /* Return the list of display names that Emacs has connections to. */)
4537 (void)
4539 Lisp_Object tail, result;
4541 result = Qnil;
4542 for (tail = x_display_name_list; CONSP (tail); tail = XCDR (tail))
4543 result = Fcons (XCAR (XCAR (tail)), result);
4545 return result;
4548 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
4549 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
4550 This function only has an effect on X Windows. With MS Windows, it is
4551 defined but does nothing.
4553 If ON is nil, allow buffering of requests.
4554 Turning on synchronization prohibits the Xlib routines from buffering
4555 requests and seriously degrades performance, but makes debugging much
4556 easier.
4557 The optional second argument TERMINAL specifies which display to act on.
4558 TERMINAL should be a terminal object, a frame or a display name (a string).
4559 If TERMINAL is omitted or nil, that stands for the selected frame's display. */)
4560 (Lisp_Object on, Lisp_Object terminal)
4562 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4564 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
4566 return Qnil;
4569 /* Wait for responses to all X commands issued so far for frame F. */
4571 void
4572 x_sync (struct frame *f)
4574 block_input ();
4575 XSync (FRAME_X_DISPLAY (f), False);
4576 unblock_input ();
4580 /***********************************************************************
4581 Window properties
4582 ***********************************************************************/
4584 DEFUN ("x-change-window-property", Fx_change_window_property,
4585 Sx_change_window_property, 2, 6, 0,
4586 doc: /* Change window property PROP to VALUE on the X window of FRAME.
4587 PROP must be a string. VALUE may be a string or a list of conses,
4588 numbers and/or strings. If an element in the list is a string, it is
4589 converted to an atom and the value of the atom is used. If an element
4590 is a cons, it is converted to a 32 bit number where the car is the 16
4591 top bits and the cdr is the lower 16 bits.
4593 FRAME nil or omitted means use the selected frame.
4594 If TYPE is given and non-nil, it is the name of the type of VALUE.
4595 If TYPE is not given or nil, the type is STRING.
4596 FORMAT gives the size in bits of each element if VALUE is a list.
4597 It must be one of 8, 16 or 32.
4598 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
4599 If OUTER-P is non-nil, the property is changed for the outer X window of
4600 FRAME. Default is to change on the edit X window. */)
4601 (Lisp_Object prop, Lisp_Object value, Lisp_Object frame,
4602 Lisp_Object type, Lisp_Object format, Lisp_Object outer_p)
4604 struct frame *f = decode_window_system_frame (frame);
4605 Atom prop_atom;
4606 Atom target_type = XA_STRING;
4607 int element_format = 8;
4608 unsigned char *data;
4609 int nelements;
4610 Window w;
4612 CHECK_STRING (prop);
4614 if (! NILP (format))
4616 CHECK_NUMBER (format);
4618 if (XINT (format) != 8 && XINT (format) != 16
4619 && XINT (format) != 32)
4620 error ("FORMAT must be one of 8, 16 or 32");
4621 element_format = XINT (format);
4624 if (CONSP (value))
4626 ptrdiff_t elsize;
4628 nelements = x_check_property_data (value);
4629 if (nelements == -1)
4630 error ("Bad data in VALUE, must be number, string or cons");
4632 /* The man page for XChangeProperty:
4633 "If the specified format is 32, the property data must be a
4634 long array."
4635 This applies even if long is more than 32 bits. The X library
4636 converts to 32 bits before sending to the X server. */
4637 elsize = element_format == 32 ? sizeof (long) : element_format >> 3;
4638 data = xnmalloc (nelements, elsize);
4640 x_fill_property_data (FRAME_X_DISPLAY (f), value, data, element_format);
4642 else
4644 CHECK_STRING (value);
4645 data = SDATA (value);
4646 if (INT_MAX < SBYTES (value))
4647 error ("VALUE too long");
4648 nelements = SBYTES (value);
4651 block_input ();
4652 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4653 if (! NILP (type))
4655 CHECK_STRING (type);
4656 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4659 if (! NILP (outer_p)) w = FRAME_OUTER_WINDOW (f);
4660 else w = FRAME_X_WINDOW (f);
4662 XChangeProperty (FRAME_X_DISPLAY (f), w,
4663 prop_atom, target_type, element_format, PropModeReplace,
4664 data, nelements);
4666 if (CONSP (value)) xfree (data);
4668 /* Make sure the property is set when we return. */
4669 XFlush (FRAME_X_DISPLAY (f));
4670 unblock_input ();
4672 return value;
4676 DEFUN ("x-delete-window-property", Fx_delete_window_property,
4677 Sx_delete_window_property, 1, 2, 0,
4678 doc: /* Remove window property PROP from X window of FRAME.
4679 FRAME nil or omitted means use the selected frame. Value is PROP. */)
4680 (Lisp_Object prop, Lisp_Object frame)
4682 struct frame *f = decode_window_system_frame (frame);
4683 Atom prop_atom;
4685 CHECK_STRING (prop);
4686 block_input ();
4687 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4688 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
4690 /* Make sure the property is removed when we return. */
4691 XFlush (FRAME_X_DISPLAY (f));
4692 unblock_input ();
4694 return prop;
4698 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
4699 1, 6, 0,
4700 doc: /* Value is the value of window property PROP on FRAME.
4701 If FRAME is nil or omitted, use the selected frame.
4703 On X Windows, the following optional arguments are also accepted:
4704 If TYPE is nil or omitted, get the property as a string.
4705 Otherwise TYPE is the name of the atom that denotes the type expected.
4706 If SOURCE is non-nil, get the property on that window instead of from
4707 FRAME. The number 0 denotes the root window.
4708 If DELETE-P is non-nil, delete the property after retrieving it.
4709 If VECTOR-RET-P is non-nil, don't return a string but a vector of values.
4711 On MS Windows, this function accepts but ignores those optional arguments.
4713 Value is nil if FRAME hasn't a property with name PROP or if PROP has
4714 no value of TYPE (always string in the MS Windows case). */)
4715 (Lisp_Object prop, Lisp_Object frame, Lisp_Object type,
4716 Lisp_Object source, Lisp_Object delete_p, Lisp_Object vector_ret_p)
4718 struct frame *f = decode_window_system_frame (frame);
4719 Atom prop_atom;
4720 int rc;
4721 Lisp_Object prop_value = Qnil;
4722 unsigned char *tmp_data = NULL;
4723 Atom actual_type;
4724 Atom target_type = XA_STRING;
4725 int actual_format;
4726 unsigned long actual_size, bytes_remaining;
4727 Window target_window = FRAME_X_WINDOW (f);
4728 struct gcpro gcpro1;
4730 GCPRO1 (prop_value);
4731 CHECK_STRING (prop);
4733 if (! NILP (source))
4735 CONS_TO_INTEGER (source, Window, target_window);
4736 if (! target_window)
4737 target_window = FRAME_X_DISPLAY_INFO (f)->root_window;
4740 block_input ();
4741 if (STRINGP (type))
4743 if (strcmp ("AnyPropertyType", SSDATA (type)) == 0)
4744 target_type = AnyPropertyType;
4745 else
4746 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4749 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4750 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4751 prop_atom, 0, 0, False, target_type,
4752 &actual_type, &actual_format, &actual_size,
4753 &bytes_remaining, &tmp_data);
4754 if (rc == Success)
4756 int size = bytes_remaining;
4758 XFree (tmp_data);
4759 tmp_data = NULL;
4761 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4762 prop_atom, 0, bytes_remaining,
4763 ! NILP (delete_p), target_type,
4764 &actual_type, &actual_format,
4765 &actual_size, &bytes_remaining,
4766 &tmp_data);
4767 if (rc == Success && tmp_data)
4769 /* The man page for XGetWindowProperty says:
4770 "If the returned format is 32, the returned data is represented
4771 as a long array and should be cast to that type to obtain the
4772 elements."
4773 This applies even if long is more than 32 bits, the X library
4774 converts from 32 bit elements received from the X server to long
4775 and passes the long array to us. Thus, for that case memcpy can not
4776 be used. We convert to a 32 bit type here, because so much code
4777 assume on that.
4779 The bytes and offsets passed to XGetWindowProperty refers to the
4780 property and those are indeed in 32 bit quantities if format is
4781 32. */
4783 if (BITS_PER_LONG > 32 && actual_format == 32)
4785 unsigned long i;
4786 int *idata = (int *) tmp_data;
4787 long *ldata = (long *) tmp_data;
4789 for (i = 0; i < actual_size; ++i)
4790 idata[i] = (int) ldata[i];
4793 if (NILP (vector_ret_p))
4794 prop_value = make_string ((char *) tmp_data, size);
4795 else
4796 prop_value = x_property_data_to_lisp (f,
4797 tmp_data,
4798 actual_type,
4799 actual_format,
4800 actual_size);
4803 if (tmp_data) XFree (tmp_data);
4806 unblock_input ();
4807 UNGCPRO;
4808 return prop_value;
4813 /***********************************************************************
4814 Busy cursor
4815 ***********************************************************************/
4817 /* Timer function of hourglass_atimer. TIMER is equal to
4818 hourglass_atimer.
4820 Display an hourglass pointer on all frames by mapping the frames'
4821 hourglass_window. Set the hourglass_p flag in the frames'
4822 output_data.x structure to indicate that an hourglass cursor is
4823 shown on the frames. */
4825 void
4826 show_hourglass (struct atimer *timer)
4828 /* The timer implementation will cancel this timer automatically
4829 after this function has run. Set hourglass_atimer to null
4830 so that we know the timer doesn't have to be canceled. */
4831 hourglass_atimer = NULL;
4833 if (!hourglass_shown_p)
4835 Lisp_Object rest, frame;
4837 block_input ();
4839 FOR_EACH_FRAME (rest, frame)
4841 struct frame *f = XFRAME (frame);
4843 if (FRAME_LIVE_P (f) && FRAME_X_P (f) && FRAME_X_DISPLAY (f))
4845 Display *dpy = FRAME_X_DISPLAY (f);
4847 #ifdef USE_X_TOOLKIT
4848 if (f->output_data.x->widget)
4849 #else
4850 if (FRAME_OUTER_WINDOW (f))
4851 #endif
4853 f->output_data.x->hourglass_p = 1;
4855 if (!f->output_data.x->hourglass_window)
4857 unsigned long mask = CWCursor;
4858 XSetWindowAttributes attrs;
4859 #ifdef USE_GTK
4860 Window parent = FRAME_X_WINDOW (f);
4861 #else
4862 Window parent = FRAME_OUTER_WINDOW (f);
4863 #endif
4864 attrs.cursor = f->output_data.x->hourglass_cursor;
4866 f->output_data.x->hourglass_window
4867 = XCreateWindow (dpy, parent,
4868 0, 0, 32000, 32000, 0, 0,
4869 InputOnly,
4870 CopyFromParent,
4871 mask, &attrs);
4874 XMapRaised (dpy, f->output_data.x->hourglass_window);
4875 XFlush (dpy);
4880 hourglass_shown_p = 1;
4881 unblock_input ();
4886 /* Hide the hourglass pointer on all frames, if it is currently
4887 shown. */
4889 void
4890 hide_hourglass (void)
4892 if (hourglass_shown_p)
4894 Lisp_Object rest, frame;
4896 block_input ();
4897 FOR_EACH_FRAME (rest, frame)
4899 struct frame *f = XFRAME (frame);
4901 if (FRAME_X_P (f)
4902 /* Watch out for newly created frames. */
4903 && f->output_data.x->hourglass_window)
4905 XUnmapWindow (FRAME_X_DISPLAY (f),
4906 f->output_data.x->hourglass_window);
4907 /* Sync here because XTread_socket looks at the
4908 hourglass_p flag that is reset to zero below. */
4909 XSync (FRAME_X_DISPLAY (f), False);
4910 f->output_data.x->hourglass_p = 0;
4914 hourglass_shown_p = 0;
4915 unblock_input ();
4921 /***********************************************************************
4922 Tool tips
4923 ***********************************************************************/
4925 static Lisp_Object x_create_tip_frame (struct x_display_info *,
4926 Lisp_Object, Lisp_Object);
4927 static void compute_tip_xy (struct frame *, Lisp_Object, Lisp_Object,
4928 Lisp_Object, int, int, int *, int *);
4930 /* The frame of a currently visible tooltip. */
4932 Lisp_Object tip_frame;
4934 /* If non-nil, a timer started that hides the last tooltip when it
4935 fires. */
4937 static Lisp_Object tip_timer;
4938 Window tip_window;
4940 /* If non-nil, a vector of 3 elements containing the last args
4941 with which x-show-tip was called. See there. */
4943 static Lisp_Object last_show_tip_args;
4946 static void
4947 unwind_create_tip_frame (Lisp_Object frame)
4949 Lisp_Object deleted;
4951 deleted = unwind_create_frame (frame);
4952 if (EQ (deleted, Qt))
4954 tip_window = None;
4955 tip_frame = Qnil;
4960 /* Create a frame for a tooltip on the display described by DPYINFO.
4961 PARMS is a list of frame parameters. TEXT is the string to
4962 display in the tip frame. Value is the frame.
4964 Note that functions called here, esp. x_default_parameter can
4965 signal errors, for instance when a specified color name is
4966 undefined. We have to make sure that we're in a consistent state
4967 when this happens. */
4969 static Lisp_Object
4970 x_create_tip_frame (struct x_display_info *dpyinfo,
4971 Lisp_Object parms,
4972 Lisp_Object text)
4974 struct frame *f;
4975 Lisp_Object frame;
4976 Lisp_Object name;
4977 int width, height;
4978 ptrdiff_t count = SPECPDL_INDEX ();
4979 struct gcpro gcpro1, gcpro2, gcpro3;
4980 int face_change_count_before = face_change_count;
4981 Lisp_Object buffer;
4982 struct buffer *old_buffer;
4984 if (!dpyinfo->terminal->name)
4985 error ("Terminal is not live, can't create new frames on it");
4987 parms = Fcopy_alist (parms);
4989 /* Get the name of the frame to use for resource lookup. */
4990 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
4991 if (!STRINGP (name)
4992 && !EQ (name, Qunbound)
4993 && !NILP (name))
4994 error ("Invalid frame name--not a string or nil");
4996 frame = Qnil;
4997 GCPRO3 (parms, name, frame);
4998 f = make_frame (1);
4999 XSETFRAME (frame, f);
5001 buffer = Fget_buffer_create (build_string (" *tip*"));
5002 /* Use set_window_buffer instead of Fset_window_buffer (see
5003 discussion of bug#11984, bug#12025, bug#12026). */
5004 set_window_buffer (FRAME_ROOT_WINDOW (f), buffer, 0, 0);
5005 old_buffer = current_buffer;
5006 set_buffer_internal_1 (XBUFFER (buffer));
5007 bset_truncate_lines (current_buffer, Qnil);
5008 specbind (Qinhibit_read_only, Qt);
5009 specbind (Qinhibit_modification_hooks, Qt);
5010 Ferase_buffer ();
5011 Finsert (1, &text);
5012 set_buffer_internal_1 (old_buffer);
5014 record_unwind_protect (unwind_create_tip_frame, frame);
5016 f->terminal = dpyinfo->terminal;
5018 /* By setting the output method, we're essentially saying that
5019 the frame is live, as per FRAME_LIVE_P. If we get a signal
5020 from this point on, x_destroy_window might screw up reference
5021 counts etc. */
5022 f->output_method = output_x_window;
5023 f->output_data.x = xzalloc (sizeof *f->output_data.x);
5024 f->output_data.x->icon_bitmap = -1;
5025 FRAME_FONTSET (f) = -1;
5026 f->output_data.x->scroll_bar_foreground_pixel = -1;
5027 f->output_data.x->scroll_bar_background_pixel = -1;
5028 #ifdef USE_TOOLKIT_SCROLL_BARS
5029 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
5030 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
5031 #endif /* USE_TOOLKIT_SCROLL_BARS */
5032 fset_icon_name (f, Qnil);
5033 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
5034 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
5035 f->output_data.x->explicit_parent = 0;
5037 /* These colors will be set anyway later, but it's important
5038 to get the color reference counts right, so initialize them! */
5040 Lisp_Object black;
5041 struct gcpro gcpro1;
5043 /* Function x_decode_color can signal an error. Make
5044 sure to initialize color slots so that we won't try
5045 to free colors we haven't allocated. */
5046 FRAME_FOREGROUND_PIXEL (f) = -1;
5047 FRAME_BACKGROUND_PIXEL (f) = -1;
5048 f->output_data.x->cursor_pixel = -1;
5049 f->output_data.x->cursor_foreground_pixel = -1;
5050 f->output_data.x->border_pixel = -1;
5051 f->output_data.x->mouse_pixel = -1;
5053 black = build_string ("black");
5054 GCPRO1 (black);
5055 FRAME_FOREGROUND_PIXEL (f)
5056 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5057 FRAME_BACKGROUND_PIXEL (f)
5058 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5059 f->output_data.x->cursor_pixel
5060 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5061 f->output_data.x->cursor_foreground_pixel
5062 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5063 f->output_data.x->border_pixel
5064 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5065 f->output_data.x->mouse_pixel
5066 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5067 UNGCPRO;
5070 /* Set the name; the functions to which we pass f expect the name to
5071 be set. */
5072 if (EQ (name, Qunbound) || NILP (name))
5074 fset_name (f, build_string (dpyinfo->x_id_name));
5075 f->explicit_name = 0;
5077 else
5079 fset_name (f, name);
5080 f->explicit_name = 1;
5081 /* use the frame's title when getting resources for this frame. */
5082 specbind (Qx_resource_name, name);
5085 register_font_driver (&xfont_driver, f);
5086 #ifdef HAVE_FREETYPE
5087 #ifdef HAVE_XFT
5088 register_font_driver (&xftfont_driver, f);
5089 #else /* not HAVE_XFT */
5090 register_font_driver (&ftxfont_driver, f);
5091 #endif /* not HAVE_XFT */
5092 #endif /* HAVE_FREETYPE */
5094 x_default_parameter (f, parms, Qfont_backend, Qnil,
5095 "fontBackend", "FontBackend", RES_TYPE_STRING);
5097 /* Extract the window parameters from the supplied values that are
5098 needed to determine window geometry. */
5099 x_default_font_parameter (f, parms);
5101 x_default_parameter (f, parms, Qborder_width, make_number (0),
5102 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
5104 /* This defaults to 2 in order to match xterm. We recognize either
5105 internalBorderWidth or internalBorder (which is what xterm calls
5106 it). */
5107 if (NILP (Fassq (Qinternal_border_width, parms)))
5109 Lisp_Object value;
5111 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
5112 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
5113 if (! EQ (value, Qunbound))
5114 parms = Fcons (Fcons (Qinternal_border_width, value),
5115 parms);
5118 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
5119 "internalBorderWidth", "internalBorderWidth",
5120 RES_TYPE_NUMBER);
5122 /* Also do the stuff which must be set before the window exists. */
5123 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
5124 "foreground", "Foreground", RES_TYPE_STRING);
5125 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
5126 "background", "Background", RES_TYPE_STRING);
5127 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
5128 "pointerColor", "Foreground", RES_TYPE_STRING);
5129 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
5130 "cursorColor", "Foreground", RES_TYPE_STRING);
5131 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
5132 "borderColor", "BorderColor", RES_TYPE_STRING);
5134 #ifdef GLYPH_DEBUG
5135 image_cache_refcount =
5136 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
5137 dpyinfo_refcount = dpyinfo->reference_count;
5138 #endif /* GLYPH_DEBUG */
5140 /* Init faces before x_default_parameter is called for scroll-bar
5141 parameters because that function calls x_set_scroll_bar_width,
5142 which calls change_frame_size, which calls Fset_window_buffer,
5143 which runs hooks, which call Fvertical_motion. At the end, we
5144 end up in init_iterator with a null face cache, which should not
5145 happen. */
5146 init_frame_faces (f);
5148 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
5150 x_figure_window_size (f, parms, 0);
5153 XSetWindowAttributes attrs;
5154 unsigned long mask;
5155 Atom type = FRAME_X_DISPLAY_INFO (f)->Xatom_net_window_type_tooltip;
5157 block_input ();
5158 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
5159 if (DoesSaveUnders (dpyinfo->screen))
5160 mask |= CWSaveUnder;
5162 /* Window managers look at the override-redirect flag to determine
5163 whether or net to give windows a decoration (Xlib spec, chapter
5164 3.2.8). */
5165 attrs.override_redirect = True;
5166 attrs.save_under = True;
5167 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
5168 /* Arrange for getting MapNotify and UnmapNotify events. */
5169 attrs.event_mask = StructureNotifyMask;
5170 tip_window
5171 = FRAME_X_WINDOW (f)
5172 = XCreateWindow (FRAME_X_DISPLAY (f),
5173 FRAME_X_DISPLAY_INFO (f)->root_window,
5174 /* x, y, width, height */
5175 0, 0, 1, 1,
5176 /* Border. */
5177 f->border_width,
5178 CopyFromParent, InputOutput, CopyFromParent,
5179 mask, &attrs);
5180 XChangeProperty (FRAME_X_DISPLAY (f), tip_window,
5181 FRAME_X_DISPLAY_INFO (f)->Xatom_net_window_type,
5182 XA_ATOM, 32, PropModeReplace,
5183 (unsigned char *)&type, 1);
5184 unblock_input ();
5187 x_make_gc (f);
5189 x_default_parameter (f, parms, Qauto_raise, Qnil,
5190 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5191 x_default_parameter (f, parms, Qauto_lower, Qnil,
5192 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5193 x_default_parameter (f, parms, Qcursor_type, Qbox,
5194 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5196 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
5197 Change will not be effected unless different from the current
5198 FRAME_LINES (f). */
5199 width = FRAME_COLS (f);
5200 height = FRAME_LINES (f);
5201 SET_FRAME_COLS (f, 0);
5202 FRAME_LINES (f) = 0;
5203 change_frame_size (f, height, width, 1, 0, 0);
5205 /* Add `tooltip' frame parameter's default value. */
5206 if (NILP (Fframe_parameter (frame, Qtooltip)))
5207 Fmodify_frame_parameters (frame, list1 (Fcons (Qtooltip, Qt)));
5209 /* FIXME - can this be done in a similar way to normal frames?
5210 http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00641.html */
5212 /* Set the `display-type' frame parameter before setting up faces. */
5214 Lisp_Object disptype;
5216 if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
5217 disptype = intern ("mono");
5218 else if (FRAME_X_DISPLAY_INFO (f)->visual->class == GrayScale
5219 || FRAME_X_DISPLAY_INFO (f)->visual->class == StaticGray)
5220 disptype = intern ("grayscale");
5221 else
5222 disptype = intern ("color");
5224 if (NILP (Fframe_parameter (frame, Qdisplay_type)))
5225 Fmodify_frame_parameters (frame, list1 (Fcons (Qdisplay_type, disptype)));
5228 /* Set up faces after all frame parameters are known. This call
5229 also merges in face attributes specified for new frames.
5231 Frame parameters may be changed if .Xdefaults contains
5232 specifications for the default font. For example, if there is an
5233 `Emacs.default.attributeBackground: pink', the `background-color'
5234 attribute of the frame get's set, which let's the internal border
5235 of the tooltip frame appear in pink. Prevent this. */
5237 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
5239 /* Set tip_frame here, so that */
5240 tip_frame = frame;
5241 call2 (Qface_set_after_frame_default, frame, Qnil);
5243 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
5244 Fmodify_frame_parameters (frame, list1 (Fcons (Qbackground_color, bg)));
5247 f->no_split = 1;
5249 UNGCPRO;
5251 /* Now that the frame will be official, it counts as a reference to
5252 its display and terminal. */
5253 FRAME_X_DISPLAY_INFO (f)->reference_count++;
5254 f->terminal->reference_count++;
5256 /* It is now ok to make the frame official even if we get an error
5257 below. And the frame needs to be on Vframe_list or making it
5258 visible won't work. */
5259 Vframe_list = Fcons (frame, Vframe_list);
5262 /* Setting attributes of faces of the tooltip frame from resources
5263 and similar will increment face_change_count, which leads to the
5264 clearing of all current matrices. Since this isn't necessary
5265 here, avoid it by resetting face_change_count to the value it
5266 had before we created the tip frame. */
5267 face_change_count = face_change_count_before;
5269 /* Discard the unwind_protect. */
5270 return unbind_to (count, frame);
5274 /* Compute where to display tip frame F. PARMS is the list of frame
5275 parameters for F. DX and DY are specified offsets from the current
5276 location of the mouse. WIDTH and HEIGHT are the width and height
5277 of the tooltip. Return coordinates relative to the root window of
5278 the display in *ROOT_X, and *ROOT_Y. */
5280 static void
5281 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)
5283 Lisp_Object left, top;
5284 int win_x, win_y;
5285 Window root, child;
5286 unsigned pmask;
5288 /* User-specified position? */
5289 left = Fcdr (Fassq (Qleft, parms));
5290 top = Fcdr (Fassq (Qtop, parms));
5292 /* Move the tooltip window where the mouse pointer is. Resize and
5293 show it. */
5294 if (!INTEGERP (left) || !INTEGERP (top))
5296 block_input ();
5297 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
5298 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
5299 unblock_input ();
5302 if (INTEGERP (top))
5303 *root_y = XINT (top);
5304 else if (*root_y + XINT (dy) <= 0)
5305 *root_y = 0; /* Can happen for negative dy */
5306 else if (*root_y + XINT (dy) + height
5307 <= x_display_pixel_height (FRAME_X_DISPLAY_INFO (f)))
5308 /* It fits below the pointer */
5309 *root_y += XINT (dy);
5310 else if (height + XINT (dy) <= *root_y)
5311 /* It fits above the pointer. */
5312 *root_y -= height + XINT (dy);
5313 else
5314 /* Put it on the top. */
5315 *root_y = 0;
5317 if (INTEGERP (left))
5318 *root_x = XINT (left);
5319 else if (*root_x + XINT (dx) <= 0)
5320 *root_x = 0; /* Can happen for negative dx */
5321 else if (*root_x + XINT (dx) + width
5322 <= x_display_pixel_width (FRAME_X_DISPLAY_INFO (f)))
5323 /* It fits to the right of the pointer. */
5324 *root_x += XINT (dx);
5325 else if (width + XINT (dx) <= *root_x)
5326 /* It fits to the left of the pointer. */
5327 *root_x -= width + XINT (dx);
5328 else
5329 /* Put it left-justified on the screen--it ought to fit that way. */
5330 *root_x = 0;
5334 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
5335 doc: /* Show STRING in a "tooltip" window on frame FRAME.
5336 A tooltip window is a small X window displaying a string.
5338 This is an internal function; Lisp code should call `tooltip-show'.
5340 FRAME nil or omitted means use the selected frame.
5342 PARMS is an optional list of frame parameters which can be used to
5343 change the tooltip's appearance.
5345 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
5346 means use the default timeout of 5 seconds.
5348 If the list of frame parameters PARMS contains a `left' parameters,
5349 the tooltip is displayed at that x-position. Otherwise it is
5350 displayed at the mouse position, with offset DX added (default is 5 if
5351 DX isn't specified). Likewise for the y-position; if a `top' frame
5352 parameter is specified, it determines the y-position of the tooltip
5353 window, otherwise it is displayed at the mouse position, with offset
5354 DY added (default is -10).
5356 A tooltip's maximum size is specified by `x-max-tooltip-size'.
5357 Text larger than the specified size is clipped. */)
5358 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
5360 struct frame *f;
5361 struct window *w;
5362 int root_x, root_y;
5363 struct buffer *old_buffer;
5364 struct text_pos pos;
5365 int i, width, height, seen_reversed_p;
5366 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5367 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5368 ptrdiff_t count = SPECPDL_INDEX ();
5370 specbind (Qinhibit_redisplay, Qt);
5372 GCPRO4 (string, parms, frame, timeout);
5374 CHECK_STRING (string);
5375 if (SCHARS (string) == 0)
5376 string = make_unibyte_string (" ", 1);
5378 f = decode_window_system_frame (frame);
5379 if (NILP (timeout))
5380 timeout = make_number (5);
5381 else
5382 CHECK_NATNUM (timeout);
5384 if (NILP (dx))
5385 dx = make_number (5);
5386 else
5387 CHECK_NUMBER (dx);
5389 if (NILP (dy))
5390 dy = make_number (-10);
5391 else
5392 CHECK_NUMBER (dy);
5394 #ifdef USE_GTK
5395 if (x_gtk_use_system_tooltips)
5397 bool ok;
5399 /* Hide a previous tip, if any. */
5400 Fx_hide_tip ();
5402 block_input ();
5403 ok = xg_prepare_tooltip (f, string, &width, &height);
5404 if (ok)
5406 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5407 xg_show_tooltip (f, root_x, root_y);
5408 /* This is used in Fx_hide_tip. */
5409 XSETFRAME (tip_frame, f);
5411 unblock_input ();
5412 if (ok) goto start_timer;
5414 #endif /* USE_GTK */
5416 if (NILP (last_show_tip_args))
5417 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5419 if (!NILP (tip_frame))
5421 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5422 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5423 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5425 if (EQ (frame, last_frame)
5426 && !NILP (Fequal (last_string, string))
5427 && !NILP (Fequal (last_parms, parms)))
5429 struct frame *tip_f = XFRAME (tip_frame);
5431 /* Only DX and DY have changed. */
5432 if (!NILP (tip_timer))
5434 Lisp_Object timer = tip_timer;
5435 tip_timer = Qnil;
5436 call1 (Qcancel_timer, timer);
5439 block_input ();
5440 compute_tip_xy (tip_f, parms, dx, dy, FRAME_PIXEL_WIDTH (tip_f),
5441 FRAME_PIXEL_HEIGHT (tip_f), &root_x, &root_y);
5442 XMoveWindow (FRAME_X_DISPLAY (tip_f), FRAME_X_WINDOW (tip_f),
5443 root_x, root_y);
5444 unblock_input ();
5445 goto start_timer;
5449 /* Hide a previous tip, if any. */
5450 Fx_hide_tip ();
5452 ASET (last_show_tip_args, 0, string);
5453 ASET (last_show_tip_args, 1, frame);
5454 ASET (last_show_tip_args, 2, parms);
5456 /* Add default values to frame parameters. */
5457 if (NILP (Fassq (Qname, parms)))
5458 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5459 if (NILP (Fassq (Qinternal_border_width, parms)))
5460 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5461 if (NILP (Fassq (Qborder_width, parms)))
5462 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5463 if (NILP (Fassq (Qborder_color, parms)))
5464 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5465 if (NILP (Fassq (Qbackground_color, parms)))
5466 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5467 parms);
5469 /* Create a frame for the tooltip, and record it in the global
5470 variable tip_frame. */
5471 frame = x_create_tip_frame (FRAME_X_DISPLAY_INFO (f), parms, string);
5472 f = XFRAME (frame);
5474 /* Set up the frame's root window. */
5475 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5476 w->left_col = 0;
5477 w->top_line = 0;
5479 if (CONSP (Vx_max_tooltip_size)
5480 && RANGED_INTEGERP (1, XCAR (Vx_max_tooltip_size), INT_MAX)
5481 && RANGED_INTEGERP (1, XCDR (Vx_max_tooltip_size), INT_MAX))
5483 w->total_cols = XFASTINT (XCAR (Vx_max_tooltip_size));
5484 w->total_lines = XFASTINT (XCDR (Vx_max_tooltip_size));
5486 else
5488 w->total_cols = 80;
5489 w->total_lines = 40;
5492 FRAME_TOTAL_COLS (f) = w->total_cols;
5493 adjust_glyphs (f);
5494 w->pseudo_window_p = 1;
5496 /* Display the tooltip text in a temporary buffer. */
5497 old_buffer = current_buffer;
5498 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->contents));
5499 bset_truncate_lines (current_buffer, Qnil);
5500 clear_glyph_matrix (w->desired_matrix);
5501 clear_glyph_matrix (w->current_matrix);
5502 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5503 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
5505 /* Compute width and height of the tooltip. */
5506 width = height = seen_reversed_p = 0;
5507 for (i = 0; i < w->desired_matrix->nrows; ++i)
5509 struct glyph_row *row = &w->desired_matrix->rows[i];
5510 struct glyph *last;
5511 int row_width;
5513 /* Stop at the first empty row at the end. */
5514 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5515 break;
5517 /* Let the row go over the full width of the frame. */
5518 row->full_width_p = 1;
5520 row_width = row->pixel_width;
5521 if (row->used[TEXT_AREA])
5523 /* There's a glyph at the end of rows that is used to place
5524 the cursor there. Don't include the width of this glyph. */
5525 if (!row->reversed_p)
5527 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5528 if (INTEGERP (last->object))
5529 row_width -= last->pixel_width;
5531 else
5533 /* There could be a stretch glyph at the beginning of R2L
5534 rows that is produced by extend_face_to_end_of_line.
5535 Don't count that glyph. */
5536 struct glyph *g = row->glyphs[TEXT_AREA];
5538 if (g->type == STRETCH_GLYPH && INTEGERP (g->object))
5540 row_width -= g->pixel_width;
5541 seen_reversed_p = 1;
5546 height += row->height;
5547 width = max (width, row_width);
5550 /* If we've seen partial-length R2L rows, we need to re-adjust the
5551 tool-tip frame width and redisplay it again, to avoid over-wide
5552 tips due to the stretch glyph that extends R2L lines to full
5553 width of the frame. */
5554 if (seen_reversed_p)
5556 /* w->total_cols and FRAME_TOTAL_COLS want the width in columns,
5557 not in pixels. */
5558 width /= WINDOW_FRAME_COLUMN_WIDTH (w);
5559 w->total_cols = width;
5560 FRAME_TOTAL_COLS (f) = width;
5561 adjust_glyphs (f);
5562 clear_glyph_matrix (w->desired_matrix);
5563 clear_glyph_matrix (w->current_matrix);
5564 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
5565 width = height = 0;
5566 /* Recompute width and height of the tooltip. */
5567 for (i = 0; i < w->desired_matrix->nrows; ++i)
5569 struct glyph_row *row = &w->desired_matrix->rows[i];
5570 struct glyph *last;
5571 int row_width;
5573 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5574 break;
5575 row->full_width_p = 1;
5576 row_width = row->pixel_width;
5577 if (row->used[TEXT_AREA] && !row->reversed_p)
5579 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5580 if (INTEGERP (last->object))
5581 row_width -= last->pixel_width;
5584 height += row->height;
5585 width = max (width, row_width);
5589 /* Add the frame's internal border to the width and height the X
5590 window should have. */
5591 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5592 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5594 /* Move the tooltip window where the mouse pointer is. Resize and
5595 show it. */
5596 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5598 block_input ();
5599 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5600 root_x, root_y, width, height);
5601 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5602 unblock_input ();
5604 /* Draw into the window. */
5605 w->must_be_updated_p = 1;
5606 update_single_window (w, 1);
5608 /* Restore original current buffer. */
5609 set_buffer_internal_1 (old_buffer);
5610 windows_or_buffers_changed = old_windows_or_buffers_changed;
5612 start_timer:
5613 /* Let the tip disappear after timeout seconds. */
5614 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5615 intern ("x-hide-tip"));
5617 UNGCPRO;
5618 return unbind_to (count, Qnil);
5622 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5623 doc: /* Hide the current tooltip window, if there is any.
5624 Value is t if tooltip was open, nil otherwise. */)
5625 (void)
5627 ptrdiff_t count;
5628 Lisp_Object deleted, frame, timer;
5629 struct gcpro gcpro1, gcpro2;
5631 /* Return quickly if nothing to do. */
5632 if (NILP (tip_timer) && NILP (tip_frame))
5633 return Qnil;
5635 frame = tip_frame;
5636 timer = tip_timer;
5637 GCPRO2 (frame, timer);
5638 tip_frame = tip_timer = deleted = Qnil;
5640 count = SPECPDL_INDEX ();
5641 specbind (Qinhibit_redisplay, Qt);
5642 specbind (Qinhibit_quit, Qt);
5644 if (!NILP (timer))
5645 call1 (Qcancel_timer, timer);
5647 #ifdef USE_GTK
5649 /* When using system tooltip, tip_frame is the Emacs frame on which
5650 the tip is shown. */
5651 struct frame *f = XFRAME (frame);
5652 if (FRAME_LIVE_P (f) && xg_hide_tooltip (f))
5653 frame = Qnil;
5655 #endif
5657 if (FRAMEP (frame))
5659 delete_frame (frame, Qnil);
5660 deleted = Qt;
5662 #ifdef USE_LUCID
5663 /* Bloodcurdling hack alert: The Lucid menu bar widget's
5664 redisplay procedure is not called when a tip frame over menu
5665 items is unmapped. Redisplay the menu manually... */
5667 Widget w;
5668 struct frame *f = SELECTED_FRAME ();
5669 w = f->output_data.x->menubar_widget;
5671 if (!DoesSaveUnders (FRAME_X_DISPLAY_INFO (f)->screen)
5672 && w != NULL)
5674 block_input ();
5675 xlwmenu_redisplay (w);
5676 unblock_input ();
5679 #endif /* USE_LUCID */
5682 UNGCPRO;
5683 return unbind_to (count, deleted);
5688 /***********************************************************************
5689 File selection dialog
5690 ***********************************************************************/
5692 DEFUN ("x-uses-old-gtk-dialog", Fx_uses_old_gtk_dialog,
5693 Sx_uses_old_gtk_dialog,
5694 0, 0, 0,
5695 doc: /* Return t if the old Gtk+ file selection dialog is used. */)
5696 (void)
5698 #ifdef USE_GTK
5699 if (use_dialog_box
5700 && use_file_dialog
5701 && window_system_available (SELECTED_FRAME ())
5702 && xg_uses_old_file_dialog ())
5703 return Qt;
5704 #endif
5705 return Qnil;
5709 #ifdef USE_MOTIF
5710 /* Callback for "OK" and "Cancel" on file selection dialog. */
5712 static void
5713 file_dialog_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5715 int *result = client_data;
5716 XmAnyCallbackStruct *cb = call_data;
5717 *result = cb->reason;
5721 /* Callback for unmapping a file selection dialog. This is used to
5722 capture the case where a dialog is closed via a window manager's
5723 closer button, for example. Using a XmNdestroyCallback didn't work
5724 in this case. */
5726 static void
5727 file_dialog_unmap_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5729 int *result = client_data;
5730 *result = XmCR_CANCEL;
5733 static void
5734 clean_up_file_dialog (void *arg)
5736 Widget dialog = arg;
5738 /* Clean up. */
5739 block_input ();
5740 XtUnmanageChild (dialog);
5741 XtDestroyWidget (dialog);
5742 x_menu_set_in_use (0);
5743 unblock_input ();
5747 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5748 doc: /* Read file name, prompting with PROMPT in directory DIR.
5749 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5750 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5751 or directory must exist.
5753 This function is only defined on NS, MS Windows, and X Windows with the
5754 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5755 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories. */)
5756 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename,
5757 Lisp_Object mustmatch, Lisp_Object only_dir_p)
5759 int result;
5760 struct frame *f = SELECTED_FRAME ();
5761 Lisp_Object file = Qnil;
5762 Lisp_Object decoded_file;
5763 Widget dialog, text, help;
5764 Arg al[10];
5765 int ac = 0;
5766 XmString dir_xmstring, pattern_xmstring;
5767 ptrdiff_t count = SPECPDL_INDEX ();
5768 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5770 check_window_system (f);
5772 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5774 if (popup_activated ())
5775 error ("Trying to use a menu from within a menu-entry");
5777 CHECK_STRING (prompt);
5778 CHECK_STRING (dir);
5780 /* Prevent redisplay. */
5781 specbind (Qinhibit_redisplay, Qt);
5783 block_input ();
5785 /* Create the dialog with PROMPT as title, using DIR as initial
5786 directory and using "*" as pattern. */
5787 dir = Fexpand_file_name (dir, Qnil);
5788 dir_xmstring = XmStringCreateLocalized (SSDATA (dir));
5789 pattern_xmstring = XmStringCreateLocalized ("*");
5791 XtSetArg (al[ac], XmNtitle, SDATA (prompt)); ++ac;
5792 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
5793 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
5794 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
5795 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
5796 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
5797 "fsb", al, ac);
5798 XmStringFree (dir_xmstring);
5799 XmStringFree (pattern_xmstring);
5801 /* Add callbacks for OK and Cancel. */
5802 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
5803 (XtPointer) &result);
5804 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
5805 (XtPointer) &result);
5806 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
5807 (XtPointer) &result);
5809 /* Remove the help button since we can't display help. */
5810 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
5811 XtUnmanageChild (help);
5813 /* Mark OK button as default. */
5814 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
5815 XmNshowAsDefault, True, NULL);
5817 /* If MUSTMATCH is non-nil, disable the file entry field of the
5818 dialog, so that the user must select a file from the files list
5819 box. We can't remove it because we wouldn't have a way to get at
5820 the result file name, then. */
5821 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5822 if (!NILP (mustmatch))
5824 Widget label;
5825 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
5826 XtSetSensitive (text, False);
5827 XtSetSensitive (label, False);
5830 /* Manage the dialog, so that list boxes get filled. */
5831 XtManageChild (dialog);
5833 if (STRINGP (default_filename))
5835 XmString default_xmstring;
5836 Widget wtext = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5837 Widget list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
5839 XmTextPosition last_pos = XmTextFieldGetLastPosition (wtext);
5840 XmTextFieldReplace (wtext, 0, last_pos,
5841 (SSDATA (Ffile_name_nondirectory (default_filename))));
5843 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
5844 must include the path for this to work. */
5846 default_xmstring = XmStringCreateLocalized (SSDATA (default_filename));
5848 if (XmListItemExists (list, default_xmstring))
5850 int item_pos = XmListItemPos (list, default_xmstring);
5851 /* Select the item and scroll it into view. */
5852 XmListSelectPos (list, item_pos, True);
5853 XmListSetPos (list, item_pos);
5856 XmStringFree (default_xmstring);
5859 record_unwind_protect_ptr (clean_up_file_dialog, dialog);
5861 /* Process events until the user presses Cancel or OK. */
5862 x_menu_set_in_use (1);
5863 result = 0;
5864 while (result == 0)
5866 XEvent event;
5867 x_menu_wait_for_event (0);
5868 XtAppNextEvent (Xt_app_con, &event);
5869 if (event.type == KeyPress
5870 && FRAME_X_DISPLAY (f) == event.xkey.display)
5872 KeySym keysym = XLookupKeysym (&event.xkey, 0);
5874 /* Pop down on C-g. */
5875 if (keysym == XK_g && (event.xkey.state & ControlMask) != 0)
5876 XtUnmanageChild (dialog);
5879 (void) x_dispatch_event (&event, FRAME_X_DISPLAY (f));
5882 /* Get the result. */
5883 if (result == XmCR_OK)
5885 XmString text_string;
5886 String data;
5888 XtVaGetValues (dialog, XmNtextString, &text_string, NULL);
5889 XmStringGetLtoR (text_string, XmFONTLIST_DEFAULT_TAG, &data);
5890 XmStringFree (text_string);
5891 file = build_string (data);
5892 XtFree (data);
5894 else
5895 file = Qnil;
5897 unblock_input ();
5898 UNGCPRO;
5900 /* Make "Cancel" equivalent to C-g. */
5901 if (NILP (file))
5902 Fsignal (Qquit, Qnil);
5904 decoded_file = DECODE_FILE (file);
5906 return unbind_to (count, decoded_file);
5909 #endif /* USE_MOTIF */
5911 #ifdef USE_GTK
5913 static void
5914 clean_up_dialog (void)
5916 x_menu_set_in_use (0);
5919 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5920 doc: /* Read file name, prompting with PROMPT in directory DIR.
5921 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5922 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5923 or directory must exist.
5925 This function is only defined on NS, MS Windows, and X Windows with the
5926 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5927 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories. */)
5928 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
5930 struct frame *f = SELECTED_FRAME ();
5931 char *fn;
5932 Lisp_Object file = Qnil;
5933 Lisp_Object decoded_file;
5934 ptrdiff_t count = SPECPDL_INDEX ();
5935 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5936 char *cdef_file;
5938 check_window_system (f);
5940 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5942 if (popup_activated ())
5943 error ("Trying to use a menu from within a menu-entry");
5945 CHECK_STRING (prompt);
5946 CHECK_STRING (dir);
5948 /* Prevent redisplay. */
5949 specbind (Qinhibit_redisplay, Qt);
5950 record_unwind_protect_void (clean_up_dialog);
5952 block_input ();
5954 if (STRINGP (default_filename))
5955 cdef_file = SSDATA (default_filename);
5956 else
5957 cdef_file = SSDATA (dir);
5959 fn = xg_get_file_name (f, SSDATA (prompt), cdef_file,
5960 ! NILP (mustmatch),
5961 ! NILP (only_dir_p));
5963 if (fn)
5965 file = build_string (fn);
5966 xfree (fn);
5969 unblock_input ();
5970 UNGCPRO;
5972 /* Make "Cancel" equivalent to C-g. */
5973 if (NILP (file))
5974 Fsignal (Qquit, Qnil);
5976 decoded_file = DECODE_FILE (file);
5978 return unbind_to (count, decoded_file);
5982 #ifdef HAVE_FREETYPE
5984 DEFUN ("x-select-font", Fx_select_font, Sx_select_font, 0, 2, 0,
5985 doc: /* Read a font using a GTK dialog.
5986 Return either a font spec (for GTK versions >= 3.2) or a string
5987 containing a GTK-style font name.
5989 FRAME is the frame on which to pop up the font chooser. If omitted or
5990 nil, it defaults to the selected frame. */)
5991 (Lisp_Object frame, Lisp_Object ignored)
5993 struct frame *f = decode_window_system_frame (frame);
5994 Lisp_Object font;
5995 Lisp_Object font_param;
5996 char *default_name = NULL;
5997 struct gcpro gcpro1, gcpro2;
5998 ptrdiff_t count = SPECPDL_INDEX ();
6000 if (popup_activated ())
6001 error ("Trying to use a menu from within a menu-entry");
6003 /* Prevent redisplay. */
6004 specbind (Qinhibit_redisplay, Qt);
6005 record_unwind_protect_void (clean_up_dialog);
6007 block_input ();
6009 GCPRO2 (font_param, font);
6011 XSETFONT (font, FRAME_FONT (f));
6012 font_param = Ffont_get (font, intern (":name"));
6013 if (STRINGP (font_param))
6014 default_name = xstrdup (SSDATA (font_param));
6015 else
6017 font_param = Fframe_parameter (frame, Qfont_param);
6018 if (STRINGP (font_param))
6019 default_name = xstrdup (SSDATA (font_param));
6022 font = xg_get_font (f, default_name);
6023 xfree (default_name);
6025 unblock_input ();
6027 if (NILP (font))
6028 Fsignal (Qquit, Qnil);
6030 return unbind_to (count, font);
6032 #endif /* HAVE_FREETYPE */
6034 #endif /* USE_GTK */
6037 /***********************************************************************
6038 Keyboard
6039 ***********************************************************************/
6041 #ifdef HAVE_XKB
6042 #include <X11/XKBlib.h>
6043 #include <X11/keysym.h>
6044 #endif
6046 DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
6047 Sx_backspace_delete_keys_p, 0, 1, 0,
6048 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
6049 FRAME nil means use the selected frame.
6050 Value is t if we know that both keys are present, and are mapped to the
6051 usual X keysyms. Value is `lambda' if we cannot determine if both keys are
6052 present and mapped to the usual X keysyms. */)
6053 (Lisp_Object frame)
6055 #ifndef HAVE_XKB
6056 return Qlambda;
6057 #else
6058 XkbDescPtr kb;
6059 struct frame *f = decode_window_system_frame (frame);
6060 Display *dpy = FRAME_X_DISPLAY (f);
6061 Lisp_Object have_keys;
6062 int major, minor, op, event, error_code;
6064 block_input ();
6066 /* Check library version in case we're dynamically linked. */
6067 major = XkbMajorVersion;
6068 minor = XkbMinorVersion;
6069 if (!XkbLibraryVersion (&major, &minor))
6071 unblock_input ();
6072 return Qlambda;
6075 /* Check that the server supports XKB. */
6076 major = XkbMajorVersion;
6077 minor = XkbMinorVersion;
6078 if (!XkbQueryExtension (dpy, &op, &event, &error_code, &major, &minor))
6080 unblock_input ();
6081 return Qlambda;
6084 /* In this code we check that the keyboard has physical keys with names
6085 that start with BKSP (Backspace) and DELE (Delete), and that they
6086 generate keysym XK_BackSpace and XK_Delete respectively.
6087 This function is used to test if normal-erase-is-backspace should be
6088 turned on.
6089 An alternative approach would be to just check if XK_BackSpace and
6090 XK_Delete are mapped to any key. But if any of those are mapped to
6091 some non-intuitive key combination (Meta-Shift-Ctrl-whatever) and the
6092 user doesn't know about it, it is better to return false here.
6093 It is more obvious to the user what to do if she/he has two keys
6094 clearly marked with names/symbols and one key does something not
6095 expected (i.e. she/he then tries the other).
6096 The cases where Backspace/Delete is mapped to some other key combination
6097 are rare, and in those cases, normal-erase-is-backspace can be turned on
6098 manually. */
6100 have_keys = Qnil;
6101 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
6102 if (kb)
6104 int delete_keycode = 0, backspace_keycode = 0, i;
6106 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
6108 for (i = kb->min_key_code;
6109 (i < kb->max_key_code
6110 && (delete_keycode == 0 || backspace_keycode == 0));
6111 ++i)
6113 /* The XKB symbolic key names can be seen most easily in
6114 the PS file generated by `xkbprint -label name
6115 $DISPLAY'. */
6116 if (memcmp ("DELE", kb->names->keys[i].name, 4) == 0)
6117 delete_keycode = i;
6118 else if (memcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
6119 backspace_keycode = i;
6122 XkbFreeNames (kb, 0, True);
6125 XkbFreeClientMap (kb, 0, True);
6127 if (delete_keycode
6128 && backspace_keycode
6129 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
6130 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
6131 have_keys = Qt;
6133 unblock_input ();
6134 return have_keys;
6135 #endif
6140 /***********************************************************************
6141 Initialization
6142 ***********************************************************************/
6144 /* Keep this list in the same order as frame_parms in frame.c.
6145 Use 0 for unsupported frame parameters. */
6147 frame_parm_handler x_frame_parm_handlers[] =
6149 x_set_autoraise,
6150 x_set_autolower,
6151 x_set_background_color,
6152 x_set_border_color,
6153 x_set_border_width,
6154 x_set_cursor_color,
6155 x_set_cursor_type,
6156 x_set_font,
6157 x_set_foreground_color,
6158 x_set_icon_name,
6159 x_set_icon_type,
6160 x_set_internal_border_width,
6161 x_set_menu_bar_lines,
6162 x_set_mouse_color,
6163 x_explicitly_set_name,
6164 x_set_scroll_bar_width,
6165 x_set_title,
6166 x_set_unsplittable,
6167 x_set_vertical_scroll_bars,
6168 x_set_visibility,
6169 x_set_tool_bar_lines,
6170 x_set_scroll_bar_foreground,
6171 x_set_scroll_bar_background,
6172 x_set_screen_gamma,
6173 x_set_line_spacing,
6174 x_set_fringe_width,
6175 x_set_fringe_width,
6176 x_set_wait_for_wm,
6177 x_set_fullscreen,
6178 x_set_font_backend,
6179 x_set_alpha,
6180 x_set_sticky,
6181 x_set_tool_bar_position,
6184 void
6185 syms_of_xfns (void)
6187 /* The section below is built by the lisp expression at the top of the file,
6188 just above where these variables are declared. */
6189 /*&&& init symbols here &&&*/
6190 DEFSYM (Qsuppress_icon, "suppress-icon");
6191 DEFSYM (Qundefined_color, "undefined-color");
6192 DEFSYM (Qcompound_text, "compound-text");
6193 DEFSYM (Qcancel_timer, "cancel-timer");
6194 DEFSYM (Qfont_param, "font-parameter");
6195 /* This is the end of symbol initialization. */
6197 Fput (Qundefined_color, Qerror_conditions,
6198 listn (CONSTYPE_PURE, 2, Qundefined_color, Qerror));
6199 Fput (Qundefined_color, Qerror_message,
6200 build_pure_c_string ("Undefined color"));
6202 DEFVAR_LISP ("x-pointer-shape", Vx_pointer_shape,
6203 doc: /* The shape of the pointer when over text.
6204 Changing the value does not affect existing frames
6205 unless you set the mouse color. */);
6206 Vx_pointer_shape = Qnil;
6208 #if 0 /* This doesn't really do anything. */
6209 DEFVAR_LISP ("x-nontext-pointer-shape", Vx_nontext_pointer_shape,
6210 doc: /* The shape of the pointer when not over text.
6211 This variable takes effect when you create a new frame
6212 or when you set the mouse color. */);
6213 #endif
6214 Vx_nontext_pointer_shape = Qnil;
6216 DEFVAR_LISP ("x-hourglass-pointer-shape", Vx_hourglass_pointer_shape,
6217 doc: /* The shape of the pointer when Emacs is busy.
6218 This variable takes effect when you create a new frame
6219 or when you set the mouse color. */);
6220 Vx_hourglass_pointer_shape = Qnil;
6222 #if 0 /* This doesn't really do anything. */
6223 DEFVAR_LISP ("x-mode-pointer-shape", Vx_mode_pointer_shape,
6224 doc: /* The shape of the pointer when over the mode line.
6225 This variable takes effect when you create a new frame
6226 or when you set the mouse color. */);
6227 #endif
6228 Vx_mode_pointer_shape = Qnil;
6230 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
6231 Vx_sensitive_text_pointer_shape,
6232 doc: /* The shape of the pointer when over mouse-sensitive text.
6233 This variable takes effect when you create a new frame
6234 or when you set the mouse color. */);
6235 Vx_sensitive_text_pointer_shape = Qnil;
6237 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
6238 Vx_window_horizontal_drag_shape,
6239 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
6240 This variable takes effect when you create a new frame
6241 or when you set the mouse color. */);
6242 Vx_window_horizontal_drag_shape = Qnil;
6244 DEFVAR_LISP ("x-cursor-fore-pixel", Vx_cursor_fore_pixel,
6245 doc: /* A string indicating the foreground color of the cursor box. */);
6246 Vx_cursor_fore_pixel = Qnil;
6248 DEFVAR_LISP ("x-max-tooltip-size", Vx_max_tooltip_size,
6249 doc: /* Maximum size for tooltips.
6250 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
6251 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
6253 DEFVAR_LISP ("x-no-window-manager", Vx_no_window_manager,
6254 doc: /* Non-nil if no X window manager is in use.
6255 Emacs doesn't try to figure this out; this is always nil
6256 unless you set it to something else. */);
6257 /* We don't have any way to find this out, so set it to nil
6258 and maybe the user would like to set it to t. */
6259 Vx_no_window_manager = Qnil;
6261 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
6262 Vx_pixel_size_width_font_regexp,
6263 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
6265 Since Emacs gets width of a font matching with this regexp from
6266 PIXEL_SIZE field of the name, font finding mechanism gets faster for
6267 such a font. This is especially effective for such large fonts as
6268 Chinese, Japanese, and Korean. */);
6269 Vx_pixel_size_width_font_regexp = Qnil;
6271 /* This is not ifdef:ed, so other builds than GTK can customize it. */
6272 DEFVAR_BOOL ("x-gtk-use-old-file-dialog", x_gtk_use_old_file_dialog,
6273 doc: /* Non-nil means prompt with the old GTK file selection dialog.
6274 If nil or if the file selection dialog is not available, the new GTK file
6275 chooser is used instead. To turn off all file dialogs set the
6276 variable `use-file-dialog'. */);
6277 x_gtk_use_old_file_dialog = 0;
6279 DEFVAR_BOOL ("x-gtk-show-hidden-files", x_gtk_show_hidden_files,
6280 doc: /* If non-nil, the GTK file chooser will by default show hidden files.
6281 Note that this is just the default, there is a toggle button on the file
6282 chooser to show or not show hidden files on a case by case basis. */);
6283 x_gtk_show_hidden_files = 0;
6285 DEFVAR_BOOL ("x-gtk-file-dialog-help-text", x_gtk_file_dialog_help_text,
6286 doc: /* If non-nil, the GTK file chooser will show additional help text.
6287 If more space for files in the file chooser dialog is wanted, set this to nil
6288 to turn the additional text off. */);
6289 x_gtk_file_dialog_help_text = 1;
6291 DEFVAR_BOOL ("x-gtk-whole-detached-tool-bar", x_gtk_whole_detached_tool_bar,
6292 doc: /* If non-nil, a detached tool bar is shown in full.
6293 The default is to just show an arrow and pressing on that arrow shows
6294 the tool bar buttons. */);
6295 x_gtk_whole_detached_tool_bar = 0;
6297 DEFVAR_BOOL ("x-gtk-use-system-tooltips", x_gtk_use_system_tooltips,
6298 doc: /* If non-nil with a Gtk+ built Emacs, the Gtk+ tooltip is used.
6299 Otherwise use Emacs own tooltip implementation.
6300 When using Gtk+ tooltips, the tooltip face is not used. */);
6301 x_gtk_use_system_tooltips = 1;
6303 Fprovide (intern_c_string ("x"), Qnil);
6305 #ifdef USE_X_TOOLKIT
6306 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6307 #ifdef USE_MOTIF
6308 Fprovide (intern_c_string ("motif"), Qnil);
6310 DEFVAR_LISP ("motif-version-string", Vmotif_version_string,
6311 doc: /* Version info for LessTif/Motif. */);
6312 Vmotif_version_string = build_string (XmVERSION_STRING);
6313 #endif /* USE_MOTIF */
6314 #endif /* USE_X_TOOLKIT */
6316 #ifdef USE_GTK
6317 /* Provide x-toolkit also for GTK. Internally GTK does not use Xt so it
6318 is not an X toolkit in that sense (USE_X_TOOLKIT is not defined).
6319 But for a user it is a toolkit for X, and indeed, configure
6320 accepts --with-x-toolkit=gtk. */
6321 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6322 Fprovide (intern_c_string ("gtk"), Qnil);
6323 Fprovide (intern_c_string ("move-toolbar"), Qnil);
6325 DEFVAR_LISP ("gtk-version-string", Vgtk_version_string,
6326 doc: /* Version info for GTK+. */);
6328 char gtk_version[sizeof ".." + 3 * INT_STRLEN_BOUND (int)];
6329 int len = sprintf (gtk_version, "%d.%d.%d",
6330 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
6331 Vgtk_version_string = make_pure_string (gtk_version, len, len, 0);
6333 #endif /* USE_GTK */
6335 /* X window properties. */
6336 defsubr (&Sx_change_window_property);
6337 defsubr (&Sx_delete_window_property);
6338 defsubr (&Sx_window_property);
6340 defsubr (&Sxw_display_color_p);
6341 defsubr (&Sx_display_grayscale_p);
6342 defsubr (&Sxw_color_defined_p);
6343 defsubr (&Sxw_color_values);
6344 defsubr (&Sx_server_max_request_size);
6345 defsubr (&Sx_server_vendor);
6346 defsubr (&Sx_server_version);
6347 defsubr (&Sx_display_pixel_width);
6348 defsubr (&Sx_display_pixel_height);
6349 defsubr (&Sx_display_mm_width);
6350 defsubr (&Sx_display_mm_height);
6351 defsubr (&Sx_display_screens);
6352 defsubr (&Sx_display_planes);
6353 defsubr (&Sx_display_color_cells);
6354 defsubr (&Sx_display_visual_class);
6355 defsubr (&Sx_display_backing_store);
6356 defsubr (&Sx_display_save_under);
6357 defsubr (&Sx_display_monitor_attributes_list);
6358 defsubr (&Sx_wm_set_size_hint);
6359 defsubr (&Sx_create_frame);
6360 defsubr (&Sx_open_connection);
6361 defsubr (&Sx_close_connection);
6362 defsubr (&Sx_display_list);
6363 defsubr (&Sx_synchronize);
6364 defsubr (&Sx_focus_frame);
6365 defsubr (&Sx_backspace_delete_keys_p);
6367 defsubr (&Sx_show_tip);
6368 defsubr (&Sx_hide_tip);
6369 tip_timer = Qnil;
6370 staticpro (&tip_timer);
6371 tip_frame = Qnil;
6372 staticpro (&tip_frame);
6374 last_show_tip_args = Qnil;
6375 staticpro (&last_show_tip_args);
6377 defsubr (&Sx_uses_old_gtk_dialog);
6378 #if defined (USE_MOTIF) || defined (USE_GTK)
6379 defsubr (&Sx_file_dialog);
6380 #endif
6382 #if defined (USE_GTK) && defined (HAVE_FREETYPE)
6383 defsubr (&Sx_select_font);
6384 #endif
6387 #endif /* HAVE_X_WINDOWS */