]> code.delx.au - gnu-emacs/blob - src/xfns.c
Use bool for boolean in xfns.c
[gnu-emacs] / src / xfns.c
1 /* Functions for the X window system.
2
3 Copyright (C) 1989, 1992-2015 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
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.
11
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.
16
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/>. */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <math.h>
23 #include <unistd.h>
24
25 #include "lisp.h"
26 #include "xterm.h"
27 #include "menu.h"
28 #include "frame.h"
29 #include "window.h"
30 #include "character.h"
31 #include "buffer.h"
32 #include "intervals.h"
33 #include "dispextern.h"
34 #include "keyboard.h"
35 #include "blockinput.h"
36 #include <epaths.h>
37 #include "charset.h"
38 #include "coding.h"
39 #include "fontset.h"
40 #include "systime.h"
41 #include "termhooks.h"
42 #include "atimer.h"
43 #include "termchar.h"
44 #include "font.h"
45
46 #include <sys/types.h>
47 #include <sys/stat.h>
48
49 #include "bitmaps/gray.xbm"
50 #include "xsettings.h"
51
52 #ifdef HAVE_XRANDR
53 #include <X11/extensions/Xrandr.h>
54 #endif
55 #ifdef HAVE_XINERAMA
56 #include <X11/extensions/Xinerama.h>
57 #endif
58
59 #ifdef USE_GTK
60 #include "gtkutil.h"
61 #endif
62
63 #ifdef USE_X_TOOLKIT
64 #include <X11/Shell.h>
65
66 #ifndef USE_MOTIF
67 #ifdef HAVE_XAW3D
68 #include <X11/Xaw3d/Paned.h>
69 #include <X11/Xaw3d/Label.h>
70 #else /* !HAVE_XAW3D */
71 #include <X11/Xaw/Paned.h>
72 #include <X11/Xaw/Label.h>
73 #endif /* HAVE_XAW3D */
74 #endif /* USE_MOTIF */
75
76 #ifdef USG
77 #undef USG /* ####KLUDGE for Solaris 2.2 and up */
78 #include <X11/Xos.h>
79 #define USG
80 #ifdef USG /* Pacify gcc -Wunused-macros. */
81 #endif
82 #else
83 #include <X11/Xos.h>
84 #endif
85
86 #include "widget.h"
87
88 #include "../lwlib/lwlib.h"
89
90 #ifdef USE_MOTIF
91 #include <Xm/Xm.h>
92 #include <Xm/DialogS.h>
93 #include <Xm/FileSB.h>
94 #include <Xm/List.h>
95 #include <Xm/TextF.h>
96 #endif
97
98 #ifdef USE_LUCID
99 #include "../lwlib/xlwmenu.h"
100 #endif
101
102 #if !defined (NO_EDITRES)
103 #define HACK_EDITRES
104 extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
105 #endif /* not defined NO_EDITRES */
106
107 /* Unique id counter for widgets created by the Lucid Widget Library. */
108
109 extern LWLIB_ID widget_id_tick;
110
111 #ifdef USE_MOTIF
112
113 #endif /* USE_MOTIF */
114
115 #endif /* USE_X_TOOLKIT */
116
117 #ifdef USE_GTK
118
119 #endif /* USE_GTK */
120
121 #define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
122
123 #ifdef GLYPH_DEBUG
124 static ptrdiff_t image_cache_refcount;
125 static int dpyinfo_refcount;
126 #endif
127
128 static struct x_display_info *x_display_info_for_name (Lisp_Object);
129
130 /* Let the user specify an X display with a Lisp object.
131 OBJECT may be nil, a frame or a terminal object.
132 nil stands for the selected frame--or, if that is not an X frame,
133 the first X display on the list. */
134
135 struct x_display_info *
136 check_x_display_info (Lisp_Object object)
137 {
138 struct x_display_info *dpyinfo = NULL;
139
140 if (NILP (object))
141 {
142 struct frame *sf = XFRAME (selected_frame);
143
144 if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
145 dpyinfo = FRAME_DISPLAY_INFO (sf);
146 else if (x_display_list != 0)
147 dpyinfo = x_display_list;
148 else
149 error ("X windows are not in use or not initialized");
150 }
151 else if (TERMINALP (object))
152 {
153 struct terminal *t = decode_live_terminal (object);
154
155 if (t->type != output_x_window)
156 error ("Terminal %d is not an X display", t->id);
157
158 dpyinfo = t->display_info.x;
159 }
160 else if (STRINGP (object))
161 dpyinfo = x_display_info_for_name (object);
162 else
163 {
164 struct frame *f = decode_window_system_frame (object);
165 dpyinfo = FRAME_DISPLAY_INFO (f);
166 }
167
168 return dpyinfo;
169 }
170
171 /* Store the screen positions of frame F into XPTR and YPTR.
172 These are the positions of the containing window manager window,
173 not Emacs's own window. */
174
175 void
176 x_real_positions (struct frame *f, int *xptr, int *yptr)
177 {
178 int win_x, win_y, outer_x IF_LINT (= 0), outer_y IF_LINT (= 0);
179 int real_x = 0, real_y = 0;
180 bool had_errors = false;
181 Window win = f->output_data.x->parent_desc;
182 Atom actual_type;
183 unsigned long actual_size, bytes_remaining;
184 int rc, actual_format;
185 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
186 long max_len = 400;
187 Display *dpy = FRAME_X_DISPLAY (f);
188 unsigned char *tmp_data = NULL;
189 Atom target_type = XA_CARDINAL;
190
191 block_input ();
192
193 x_catch_errors (dpy);
194
195 if (win == dpyinfo->root_window)
196 win = FRAME_OUTER_WINDOW (f);
197
198 /* This loop traverses up the containment tree until we hit the root
199 window. Window managers may intersect many windows between our window
200 and the root window. The window we find just before the root window
201 should be the outer WM window. */
202 for (;;)
203 {
204 Window wm_window, rootw;
205 Window *tmp_children;
206 unsigned int tmp_nchildren;
207 int success;
208
209 success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
210 &wm_window, &tmp_children, &tmp_nchildren);
211
212 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
213
214 /* Don't free tmp_children if XQueryTree failed. */
215 if (! success)
216 break;
217
218 XFree (tmp_children);
219
220 if (wm_window == rootw || had_errors)
221 break;
222
223 win = wm_window;
224 }
225
226 if (! had_errors)
227 {
228 unsigned int ign;
229 Window child, rootw;
230
231 /* Get the real coordinates for the WM window upper left corner */
232 XGetGeometry (FRAME_X_DISPLAY (f), win,
233 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
234
235 /* Translate real coordinates to coordinates relative to our
236 window. For our window, the upper left corner is 0, 0.
237 Since the upper left corner of the WM window is outside
238 our window, win_x and win_y will be negative:
239
240 ------------------ ---> x
241 | title |
242 | ----------------- v y
243 | | our window
244 */
245 XTranslateCoordinates (FRAME_X_DISPLAY (f),
246
247 /* From-window, to-window. */
248 FRAME_DISPLAY_INFO (f)->root_window,
249 FRAME_X_WINDOW (f),
250
251 /* From-position, to-position. */
252 real_x, real_y, &win_x, &win_y,
253
254 /* Child of win. */
255 &child);
256
257 if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
258 {
259 outer_x = win_x;
260 outer_y = win_y;
261 }
262 else
263 {
264 XTranslateCoordinates (FRAME_X_DISPLAY (f),
265
266 /* From-window, to-window. */
267 FRAME_DISPLAY_INFO (f)->root_window,
268 FRAME_OUTER_WINDOW (f),
269
270 /* From-position, to-position. */
271 real_x, real_y, &outer_x, &outer_y,
272
273 /* Child of win. */
274 &child);
275 }
276
277 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
278 }
279
280
281 if (dpyinfo->root_window == f->output_data.x->parent_desc)
282 {
283 /* Try _NET_FRAME_EXTENTS if our parent is the root window. */
284 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_frame_extents,
285 0, max_len, False, target_type,
286 &actual_type, &actual_format, &actual_size,
287 &bytes_remaining, &tmp_data);
288
289 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
290 && actual_size == 4 && actual_format == 32)
291 {
292 unsigned int ign;
293 Window rootw;
294 long *fe = (long *)tmp_data;
295
296 XGetGeometry (FRAME_X_DISPLAY (f), win,
297 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
298 outer_x = -fe[0];
299 outer_y = -fe[2];
300 real_x -= fe[0];
301 real_y -= fe[2];
302 }
303 }
304
305 if (tmp_data) XFree (tmp_data);
306
307 x_uncatch_errors ();
308
309 unblock_input ();
310
311 if (had_errors) return;
312
313 f->x_pixels_diff = -win_x;
314 f->y_pixels_diff = -win_y;
315
316 FRAME_X_OUTPUT (f)->x_pixels_outer_diff = -outer_x;
317 FRAME_X_OUTPUT (f)->y_pixels_outer_diff = -outer_y;
318
319 *xptr = real_x;
320 *yptr = real_y;
321 }
322
323 /* Get the mouse position in frame relative coordinates. */
324
325 void
326 x_relative_mouse_position (struct frame *f, int *x, int *y)
327 {
328 Window root, dummy_window;
329 int dummy;
330
331 eassert (FRAME_X_P (f));
332
333 block_input ();
334
335 XQueryPointer (FRAME_X_DISPLAY (f),
336 DefaultRootWindow (FRAME_X_DISPLAY (f)),
337
338 /* The root window which contains the pointer. */
339 &root,
340
341 /* Window pointer is on, not used */
342 &dummy_window,
343
344 /* The position on that root window. */
345 x, y,
346
347 /* x/y in dummy_window coordinates, not used. */
348 &dummy, &dummy,
349
350 /* Modifier keys and pointer buttons, about which
351 we don't care. */
352 (unsigned int *) &dummy);
353
354 unblock_input ();
355
356 /* Translate root window coordinates to window coordinates. */
357 *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
358 *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
359 }
360
361 /* Gamma-correct COLOR on frame F. */
362
363 void
364 gamma_correct (struct frame *f, XColor *color)
365 {
366 if (f->gamma)
367 {
368 color->red = pow (color->red / 65535.0, f->gamma) * 65535.0 + 0.5;
369 color->green = pow (color->green / 65535.0, f->gamma) * 65535.0 + 0.5;
370 color->blue = pow (color->blue / 65535.0, f->gamma) * 65535.0 + 0.5;
371 }
372 }
373
374
375 /* Decide if color named COLOR_NAME is valid for use on frame F. If
376 so, return the RGB values in COLOR. If ALLOC_P,
377 allocate the color. Value is false if COLOR_NAME is invalid, or
378 no color could be allocated. */
379
380 bool
381 x_defined_color (struct frame *f, const char *color_name,
382 XColor *color, bool alloc_p)
383 {
384 bool success_p = false;
385 Display *dpy = FRAME_X_DISPLAY (f);
386 Colormap cmap = FRAME_X_COLORMAP (f);
387
388 block_input ();
389 #ifdef USE_GTK
390 success_p = xg_check_special_colors (f, color_name, color);
391 #endif
392 if (!success_p)
393 success_p = XParseColor (dpy, cmap, color_name, color) != 0;
394 if (success_p && alloc_p)
395 success_p = x_alloc_nearest_color (f, cmap, color);
396 unblock_input ();
397
398 return success_p;
399 }
400
401
402 /* Return the pixel color value for color COLOR_NAME on frame F. If F
403 is a monochrome frame, return MONO_COLOR regardless of what ARG says.
404 Signal an error if color can't be allocated. */
405
406 static int
407 x_decode_color (struct frame *f, Lisp_Object color_name, int mono_color)
408 {
409 XColor cdef;
410
411 CHECK_STRING (color_name);
412
413 #if false /* Don't do this. It's wrong when we're not using the default
414 colormap, it makes freeing difficult, and it's probably not
415 an important optimization. */
416 if (strcmp (SDATA (color_name), "black") == 0)
417 return BLACK_PIX_DEFAULT (f);
418 else if (strcmp (SDATA (color_name), "white") == 0)
419 return WHITE_PIX_DEFAULT (f);
420 #endif
421
422 /* Return MONO_COLOR for monochrome frames. */
423 if (FRAME_DISPLAY_INFO (f)->n_planes == 1)
424 return mono_color;
425
426 /* x_defined_color is responsible for coping with failures
427 by looking for a near-miss. */
428 if (x_defined_color (f, SSDATA (color_name), &cdef, true))
429 return cdef.pixel;
430
431 signal_error ("Undefined color", color_name);
432 }
433
434
435 \f
436 /* Change the `wait-for-wm' frame parameter of frame F. OLD_VALUE is
437 the previous value of that parameter, NEW_VALUE is the new value.
438 See also the comment of wait_for_wm in struct x_output. */
439
440 static void
441 x_set_wait_for_wm (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
442 {
443 f->output_data.x->wait_for_wm = !NILP (new_value);
444 }
445
446 static void
447 x_set_tool_bar_position (struct frame *f,
448 Lisp_Object new_value,
449 Lisp_Object old_value)
450 {
451 Lisp_Object choice = list4 (Qleft, Qright, Qtop, Qbottom);
452
453 if (!NILP (Fmemq (new_value, choice)))
454 {
455 #ifdef USE_GTK
456 if (!EQ (new_value, old_value))
457 {
458 xg_change_toolbar_position (f, new_value);
459 fset_tool_bar_position (f, new_value);
460 }
461 #else
462 if (!EQ (new_value, Qtop))
463 error ("The only supported tool bar position is top");
464 #endif
465 }
466 else
467 wrong_choice (choice, new_value);
468 }
469
470 #ifdef USE_GTK
471
472 /* Set icon from FILE for frame F. By using GTK functions the icon
473 may be any format that GdkPixbuf knows about, i.e. not just bitmaps. */
474
475 bool
476 xg_set_icon (struct frame *f, Lisp_Object file)
477 {
478 bool result = false;
479 Lisp_Object found;
480
481 found = x_find_image_file (file);
482
483 if (! NILP (found))
484 {
485 GdkPixbuf *pixbuf;
486 GError *err = NULL;
487 char *filename = SSDATA (found);
488 block_input ();
489
490 pixbuf = gdk_pixbuf_new_from_file (filename, &err);
491
492 if (pixbuf)
493 {
494 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
495 pixbuf);
496 g_object_unref (pixbuf);
497
498 result = true;
499 }
500 else
501 g_error_free (err);
502
503 unblock_input ();
504 }
505
506 return result;
507 }
508
509 bool
510 xg_set_icon_from_xpm_data (struct frame *f, const char **data)
511 {
512 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data (data);
513
514 if (!pixbuf)
515 return false;
516
517 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
518 g_object_unref (pixbuf);
519 return true;
520 }
521 #endif /* USE_GTK */
522
523
524 /* Functions called only from `x_set_frame_param'
525 to set individual parameters.
526
527 If FRAME_X_WINDOW (f) is 0,
528 the frame is being created and its X-window does not exist yet.
529 In that case, just record the parameter's new value
530 in the standard place; do not attempt to change the window. */
531
532 static void
533 x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
534 {
535 struct x_output *x = f->output_data.x;
536 unsigned long fg, old_fg;
537
538 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
539 old_fg = FRAME_FOREGROUND_PIXEL (f);
540 FRAME_FOREGROUND_PIXEL (f) = fg;
541
542 if (FRAME_X_WINDOW (f) != 0)
543 {
544 Display *dpy = FRAME_X_DISPLAY (f);
545
546 block_input ();
547 XSetForeground (dpy, x->normal_gc, fg);
548 XSetBackground (dpy, x->reverse_gc, fg);
549
550 if (x->cursor_pixel == old_fg)
551 {
552 unload_color (f, x->cursor_pixel);
553 x->cursor_pixel = x_copy_color (f, fg);
554 XSetBackground (dpy, x->cursor_gc, x->cursor_pixel);
555 }
556
557 unblock_input ();
558
559 update_face_from_frame_parameter (f, Qforeground_color, arg);
560
561 if (FRAME_VISIBLE_P (f))
562 redraw_frame (f);
563 }
564
565 unload_color (f, old_fg);
566 }
567
568 static void
569 x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
570 {
571 struct x_output *x = f->output_data.x;
572 unsigned long bg;
573
574 bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
575 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
576 FRAME_BACKGROUND_PIXEL (f) = bg;
577
578 if (FRAME_X_WINDOW (f) != 0)
579 {
580 Display *dpy = FRAME_X_DISPLAY (f);
581
582 block_input ();
583 XSetBackground (dpy, x->normal_gc, bg);
584 XSetForeground (dpy, x->reverse_gc, bg);
585 XSetWindowBackground (dpy, FRAME_X_WINDOW (f), bg);
586 XSetForeground (dpy, x->cursor_gc, bg);
587
588 #ifdef USE_GTK
589 xg_set_background_color (f, bg);
590 #endif
591
592 #ifndef USE_TOOLKIT_SCROLL_BARS /* Turns out to be annoying with
593 toolkit scroll bars. */
594 {
595 Lisp_Object bar;
596 for (bar = FRAME_SCROLL_BARS (f);
597 !NILP (bar);
598 bar = XSCROLL_BAR (bar)->next)
599 {
600 Window window = XSCROLL_BAR (bar)->x_window;
601 XSetWindowBackground (dpy, window, bg);
602 }
603 }
604 #endif /* USE_TOOLKIT_SCROLL_BARS */
605
606 unblock_input ();
607 update_face_from_frame_parameter (f, Qbackground_color, arg);
608
609 if (FRAME_VISIBLE_P (f))
610 redraw_frame (f);
611 }
612 }
613
614 static void
615 x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
616 {
617 struct x_output *x = f->output_data.x;
618 Display *dpy = FRAME_X_DISPLAY (f);
619 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
620 Cursor hourglass_cursor, horizontal_drag_cursor, vertical_drag_cursor;
621 unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
622 unsigned long mask_color = FRAME_BACKGROUND_PIXEL (f);
623
624 /* Don't let pointers be invisible. */
625 if (mask_color == pixel)
626 {
627 x_free_colors (f, &pixel, 1);
628 pixel = x_copy_color (f, FRAME_FOREGROUND_PIXEL (f));
629 }
630
631 unload_color (f, x->mouse_pixel);
632 x->mouse_pixel = pixel;
633
634 block_input ();
635
636 /* It's not okay to crash if the user selects a screwy cursor. */
637 x_catch_errors (dpy);
638
639 if (!NILP (Vx_pointer_shape))
640 {
641 CHECK_NUMBER (Vx_pointer_shape);
642 cursor = XCreateFontCursor (dpy, XINT (Vx_pointer_shape));
643 }
644 else
645 cursor = XCreateFontCursor (dpy, XC_xterm);
646 x_check_errors (dpy, "bad text pointer cursor: %s");
647
648 if (!NILP (Vx_nontext_pointer_shape))
649 {
650 CHECK_NUMBER (Vx_nontext_pointer_shape);
651 nontext_cursor
652 = XCreateFontCursor (dpy, XINT (Vx_nontext_pointer_shape));
653 }
654 else
655 nontext_cursor = XCreateFontCursor (dpy, XC_left_ptr);
656 x_check_errors (dpy, "bad nontext pointer cursor: %s");
657
658 if (!NILP (Vx_hourglass_pointer_shape))
659 {
660 CHECK_NUMBER (Vx_hourglass_pointer_shape);
661 hourglass_cursor
662 = XCreateFontCursor (dpy, XINT (Vx_hourglass_pointer_shape));
663 }
664 else
665 hourglass_cursor = XCreateFontCursor (dpy, XC_watch);
666 x_check_errors (dpy, "bad hourglass pointer cursor: %s");
667
668 if (!NILP (Vx_mode_pointer_shape))
669 {
670 CHECK_NUMBER (Vx_mode_pointer_shape);
671 mode_cursor = XCreateFontCursor (dpy, XINT (Vx_mode_pointer_shape));
672 }
673 else
674 mode_cursor = XCreateFontCursor (dpy, XC_xterm);
675 x_check_errors (dpy, "bad modeline pointer cursor: %s");
676
677 if (!NILP (Vx_sensitive_text_pointer_shape))
678 {
679 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
680 hand_cursor
681 = XCreateFontCursor (dpy, XINT (Vx_sensitive_text_pointer_shape));
682 }
683 else
684 hand_cursor = XCreateFontCursor (dpy, XC_hand2);
685
686 if (!NILP (Vx_window_horizontal_drag_shape))
687 {
688 CHECK_TYPE_RANGED_INTEGER (unsigned, Vx_window_horizontal_drag_shape);
689 horizontal_drag_cursor
690 = XCreateFontCursor (dpy, XINT (Vx_window_horizontal_drag_shape));
691 }
692 else
693 horizontal_drag_cursor
694 = XCreateFontCursor (dpy, XC_sb_h_double_arrow);
695
696 if (!NILP (Vx_window_vertical_drag_shape))
697 {
698 CHECK_NUMBER (Vx_window_vertical_drag_shape);
699 vertical_drag_cursor
700 = XCreateFontCursor (dpy, XINT (Vx_window_vertical_drag_shape));
701 }
702 else
703 vertical_drag_cursor
704 = XCreateFontCursor (dpy, XC_sb_v_double_arrow);
705
706 /* Check and report errors with the above calls. */
707 x_check_errors (dpy, "can't set cursor shape: %s");
708 x_uncatch_errors ();
709
710 {
711 XColor fore_color, back_color;
712
713 fore_color.pixel = x->mouse_pixel;
714 x_query_color (f, &fore_color);
715 back_color.pixel = mask_color;
716 x_query_color (f, &back_color);
717
718 XRecolorCursor (dpy, cursor, &fore_color, &back_color);
719 XRecolorCursor (dpy, nontext_cursor, &fore_color, &back_color);
720 XRecolorCursor (dpy, mode_cursor, &fore_color, &back_color);
721 XRecolorCursor (dpy, hand_cursor, &fore_color, &back_color);
722 XRecolorCursor (dpy, hourglass_cursor, &fore_color, &back_color);
723 XRecolorCursor (dpy, horizontal_drag_cursor, &fore_color, &back_color);
724 XRecolorCursor (dpy, vertical_drag_cursor, &fore_color, &back_color);
725 }
726
727 if (FRAME_X_WINDOW (f) != 0)
728 XDefineCursor (dpy, FRAME_X_WINDOW (f),
729 f->output_data.x->current_cursor = cursor);
730
731 if (cursor != x->text_cursor
732 && x->text_cursor != 0)
733 XFreeCursor (dpy, x->text_cursor);
734 x->text_cursor = cursor;
735
736 if (nontext_cursor != x->nontext_cursor
737 && x->nontext_cursor != 0)
738 XFreeCursor (dpy, x->nontext_cursor);
739 x->nontext_cursor = nontext_cursor;
740
741 if (hourglass_cursor != x->hourglass_cursor
742 && x->hourglass_cursor != 0)
743 XFreeCursor (dpy, x->hourglass_cursor);
744 x->hourglass_cursor = hourglass_cursor;
745
746 if (mode_cursor != x->modeline_cursor
747 && x->modeline_cursor != 0)
748 XFreeCursor (dpy, f->output_data.x->modeline_cursor);
749 x->modeline_cursor = mode_cursor;
750
751 if (hand_cursor != x->hand_cursor
752 && x->hand_cursor != 0)
753 XFreeCursor (dpy, x->hand_cursor);
754 x->hand_cursor = hand_cursor;
755
756 if (horizontal_drag_cursor != x->horizontal_drag_cursor
757 && x->horizontal_drag_cursor != 0)
758 XFreeCursor (dpy, x->horizontal_drag_cursor);
759 x->horizontal_drag_cursor = horizontal_drag_cursor;
760
761 if (vertical_drag_cursor != x->vertical_drag_cursor
762 && x->vertical_drag_cursor != 0)
763 XFreeCursor (dpy, x->vertical_drag_cursor);
764 x->vertical_drag_cursor = vertical_drag_cursor;
765
766 XFlush (dpy);
767 unblock_input ();
768
769 update_face_from_frame_parameter (f, Qmouse_color, arg);
770 }
771
772 static void
773 x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
774 {
775 unsigned long fore_pixel, pixel;
776 bool fore_pixel_allocated_p = false, pixel_allocated_p = false;
777 struct x_output *x = f->output_data.x;
778
779 if (!NILP (Vx_cursor_fore_pixel))
780 {
781 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
782 WHITE_PIX_DEFAULT (f));
783 fore_pixel_allocated_p = true;
784 }
785 else
786 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
787
788 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
789 pixel_allocated_p = true;
790
791 /* Make sure that the cursor color differs from the background color. */
792 if (pixel == FRAME_BACKGROUND_PIXEL (f))
793 {
794 if (pixel_allocated_p)
795 {
796 x_free_colors (f, &pixel, 1);
797 pixel_allocated_p = false;
798 }
799
800 pixel = x->mouse_pixel;
801 if (pixel == fore_pixel)
802 {
803 if (fore_pixel_allocated_p)
804 {
805 x_free_colors (f, &fore_pixel, 1);
806 fore_pixel_allocated_p = false;
807 }
808 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
809 }
810 }
811
812 unload_color (f, x->cursor_foreground_pixel);
813 if (!fore_pixel_allocated_p)
814 fore_pixel = x_copy_color (f, fore_pixel);
815 x->cursor_foreground_pixel = fore_pixel;
816
817 unload_color (f, x->cursor_pixel);
818 if (!pixel_allocated_p)
819 pixel = x_copy_color (f, pixel);
820 x->cursor_pixel = pixel;
821
822 if (FRAME_X_WINDOW (f) != 0)
823 {
824 block_input ();
825 XSetBackground (FRAME_X_DISPLAY (f), x->cursor_gc, x->cursor_pixel);
826 XSetForeground (FRAME_X_DISPLAY (f), x->cursor_gc, fore_pixel);
827 unblock_input ();
828
829 if (FRAME_VISIBLE_P (f))
830 {
831 x_update_cursor (f, false);
832 x_update_cursor (f, true);
833 }
834 }
835
836 update_face_from_frame_parameter (f, Qcursor_color, arg);
837 }
838 \f
839 /* Set the border-color of frame F to pixel value PIX.
840 Note that this does not fully take effect if done before
841 F has an x-window. */
842
843 static void
844 x_set_border_pixel (struct frame *f, int pix)
845 {
846 unload_color (f, f->output_data.x->border_pixel);
847 f->output_data.x->border_pixel = pix;
848
849 if (FRAME_X_WINDOW (f) != 0 && f->border_width > 0)
850 {
851 block_input ();
852 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), pix);
853 unblock_input ();
854
855 if (FRAME_VISIBLE_P (f))
856 redraw_frame (f);
857 }
858 }
859
860 /* Set the border-color of frame F to value described by ARG.
861 ARG can be a string naming a color.
862 The border-color is used for the border that is drawn by the X server.
863 Note that this does not fully take effect if done before
864 F has an x-window; it must be redone when the window is created.
865
866 Note: this is done in two routines because of the way X10 works.
867
868 Note: under X11, this is normally the province of the window manager,
869 and so emacs's border colors may be overridden. */
870
871 static void
872 x_set_border_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
873 {
874 int pix;
875
876 CHECK_STRING (arg);
877 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
878 x_set_border_pixel (f, pix);
879 update_face_from_frame_parameter (f, Qborder_color, arg);
880 }
881
882
883 static void
884 x_set_cursor_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
885 {
886 set_frame_cursor_types (f, arg);
887 }
888
889 static void
890 x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
891 {
892 bool result;
893
894 if (STRINGP (arg))
895 {
896 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
897 return;
898 }
899 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
900 return;
901
902 block_input ();
903 if (NILP (arg))
904 result = x_text_icon (f,
905 SSDATA ((!NILP (f->icon_name)
906 ? f->icon_name
907 : f->name)));
908 else
909 result = x_bitmap_icon (f, arg);
910
911 if (result)
912 {
913 unblock_input ();
914 error ("No icon window available");
915 }
916
917 XFlush (FRAME_X_DISPLAY (f));
918 unblock_input ();
919 }
920
921 static void
922 x_set_icon_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
923 {
924 bool result;
925
926 if (STRINGP (arg))
927 {
928 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
929 return;
930 }
931 else if (!NILP (arg) || NILP (oldval))
932 return;
933
934 fset_icon_name (f, arg);
935
936 if (f->output_data.x->icon_bitmap != 0)
937 return;
938
939 block_input ();
940
941 result = x_text_icon (f,
942 SSDATA ((!NILP (f->icon_name)
943 ? f->icon_name
944 : !NILP (f->title)
945 ? f->title
946 : f->name)));
947
948 if (result)
949 {
950 unblock_input ();
951 error ("No icon window available");
952 }
953
954 XFlush (FRAME_X_DISPLAY (f));
955 unblock_input ();
956 }
957
958 \f
959 static void
960 x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
961 {
962 int nlines;
963 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
964 int olines = FRAME_MENU_BAR_LINES (f);
965 #endif
966
967 /* Right now, menu bars don't work properly in minibuf-only frames;
968 most of the commands try to apply themselves to the minibuffer
969 frame itself, and get an error because you can't switch buffers
970 in or split the minibuffer window. */
971 if (FRAME_MINIBUF_ONLY_P (f))
972 return;
973
974 if (TYPE_RANGED_INTEGERP (int, value))
975 nlines = XINT (value);
976 else
977 nlines = 0;
978
979 /* Make sure we redisplay all windows in this frame. */
980 windows_or_buffers_changed = 59;
981
982 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
983 FRAME_MENU_BAR_LINES (f) = 0;
984 FRAME_MENU_BAR_HEIGHT (f) = 0;
985 if (nlines)
986 {
987 FRAME_EXTERNAL_MENU_BAR (f) = 1;
988 if (FRAME_X_P (f) && f->output_data.x->menubar_widget == 0)
989 /* Make sure next redisplay shows the menu bar. */
990 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = true;
991 }
992 else
993 {
994 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
995 free_frame_menubar (f);
996 FRAME_EXTERNAL_MENU_BAR (f) = 0;
997 if (FRAME_X_P (f))
998 f->output_data.x->menubar_widget = 0;
999 }
1000 #else /* not USE_X_TOOLKIT && not USE_GTK */
1001 FRAME_MENU_BAR_LINES (f) = nlines;
1002 FRAME_MENU_BAR_HEIGHT (f) = nlines * FRAME_LINE_HEIGHT (f);
1003 adjust_frame_size (f, -1, -1, 2, true, Qmenu_bar_lines);
1004 if (FRAME_X_WINDOW (f))
1005 x_clear_under_internal_border (f);
1006
1007 /* If the menu bar height gets changed, the internal border below
1008 the top margin has to be cleared. Also, if the menu bar gets
1009 larger, the area for the added lines has to be cleared except for
1010 the first menu bar line that is to be drawn later. */
1011 if (nlines != olines)
1012 {
1013 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1014 int width = FRAME_PIXEL_WIDTH (f);
1015 int y;
1016
1017 /* height can be zero here. */
1018 if (FRAME_X_WINDOW (f) && height > 0 && width > 0)
1019 {
1020 y = FRAME_TOP_MARGIN_HEIGHT (f);
1021
1022 block_input ();
1023 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1024 0, y, width, height);
1025 unblock_input ();
1026 }
1027
1028 if (nlines > 1 && nlines > olines)
1029 {
1030 y = (olines == 0 ? 1 : olines) * FRAME_LINE_HEIGHT (f);
1031 height = nlines * FRAME_LINE_HEIGHT (f) - y;
1032
1033 block_input ();
1034 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1035 0, y, width, height);
1036 unblock_input ();
1037 }
1038
1039 if (nlines == 0 && WINDOWP (f->menu_bar_window))
1040 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
1041 }
1042 #endif /* not USE_X_TOOLKIT && not USE_GTK */
1043 adjust_frame_glyphs (f);
1044 run_window_configuration_change_hook (f);
1045 }
1046
1047
1048 /* Set the number of lines used for the tool bar of frame F to VALUE.
1049 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1050 is the old number of tool bar lines. This function changes the
1051 height of all windows on frame F to match the new tool bar height.
1052 The frame's height doesn't change. */
1053
1054 static void
1055 x_set_tool_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1056 {
1057 int nlines;
1058
1059 /* Treat tool bars like menu bars. */
1060 if (FRAME_MINIBUF_ONLY_P (f))
1061 return;
1062
1063 /* Use VALUE only if an int >= 0. */
1064 if (RANGED_INTEGERP (0, value, INT_MAX))
1065 nlines = XFASTINT (value);
1066 else
1067 nlines = 0;
1068
1069 x_change_tool_bar_height (f, nlines * FRAME_LINE_HEIGHT (f));
1070 }
1071
1072
1073 /* Set the pixel height of the tool bar of frame F to HEIGHT. */
1074 void
1075 x_change_tool_bar_height (struct frame *f, int height)
1076 {
1077 #ifdef USE_GTK
1078 FRAME_TOOL_BAR_LINES (f) = 0;
1079 FRAME_TOOL_BAR_HEIGHT (f) = 0;
1080 if (height)
1081 {
1082 FRAME_EXTERNAL_TOOL_BAR (f) = true;
1083 if (FRAME_X_P (f) && f->output_data.x->toolbar_widget == 0)
1084 /* Make sure next redisplay shows the tool bar. */
1085 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = true;
1086 update_frame_tool_bar (f);
1087 }
1088 else
1089 {
1090 if (FRAME_EXTERNAL_TOOL_BAR (f))
1091 free_frame_tool_bar (f);
1092 FRAME_EXTERNAL_TOOL_BAR (f) = false;
1093 }
1094 #else /* !USE_GTK */
1095 int unit = FRAME_LINE_HEIGHT (f);
1096 int old_height = FRAME_TOOL_BAR_HEIGHT (f);
1097 int lines = (height + unit - 1) / unit;
1098
1099 /* Make sure we redisplay all windows in this frame. */
1100 windows_or_buffers_changed = 60;
1101
1102
1103 /* Recalculate tool bar and frame text sizes. */
1104 FRAME_TOOL_BAR_HEIGHT (f) = height;
1105 FRAME_TOOL_BAR_LINES (f) = lines;
1106 /* Store the `tool-bar-lines' and `height' frame parameters. */
1107 store_frame_param (f, Qtool_bar_lines, make_number (lines));
1108 store_frame_param (f, Qheight, make_number (FRAME_LINES (f)));
1109
1110 /* We also have to make sure that the internal border at the top of
1111 the frame, below the menu bar or tool bar, is redrawn when the
1112 tool bar disappears. This is so because the internal border is
1113 below the tool bar if one is displayed, but is below the menu bar
1114 if there isn't a tool bar. The tool bar draws into the area
1115 below the menu bar. */
1116 if (FRAME_X_WINDOW (f) && FRAME_TOOL_BAR_HEIGHT (f) == 0)
1117 {
1118 clear_frame (f);
1119 clear_current_matrices (f);
1120 }
1121
1122 if ((height < old_height) && WINDOWP (f->tool_bar_window))
1123 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1124
1125 /* Recalculate toolbar height. */
1126 f->n_tool_bar_rows = 0;
1127
1128 adjust_frame_size (f, -1, -1,
1129 (!f->tool_bar_redisplayed_once ? 1
1130 : (old_height == 0 || height == 0) ? 2
1131 : 4),
1132 false, Qtool_bar_lines);
1133
1134 /* adjust_frame_size might not have done anything, garbage frame
1135 here. */
1136 adjust_frame_glyphs (f);
1137 SET_FRAME_GARBAGED (f);
1138 if (FRAME_X_WINDOW (f))
1139 x_clear_under_internal_border (f);
1140
1141 #endif /* USE_GTK */
1142 }
1143
1144
1145 static void
1146 x_set_internal_border_width (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1147 {
1148 int border;
1149
1150 CHECK_TYPE_RANGED_INTEGER (int, arg);
1151 border = max (XINT (arg), 0);
1152
1153 if (border != FRAME_INTERNAL_BORDER_WIDTH (f))
1154 {
1155 FRAME_INTERNAL_BORDER_WIDTH (f) = border;
1156
1157 #ifdef USE_X_TOOLKIT
1158 if (FRAME_X_OUTPUT (f)->edit_widget)
1159 widget_store_internal_border (FRAME_X_OUTPUT (f)->edit_widget);
1160 #endif
1161
1162 if (FRAME_X_WINDOW (f) != 0)
1163 {
1164 adjust_frame_size (f, -1, -1, 3, false, Qinternal_border_width);
1165
1166 #ifdef USE_GTK
1167 xg_clear_under_internal_border (f);
1168 #else
1169 x_clear_under_internal_border (f);
1170 #endif
1171 }
1172 }
1173
1174 }
1175
1176
1177 /* Set the foreground color for scroll bars on frame F to VALUE.
1178 VALUE should be a string, a color name. If it isn't a string or
1179 isn't a valid color name, do nothing. OLDVAL is the old value of
1180 the frame parameter. */
1181
1182 static void
1183 x_set_scroll_bar_foreground (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1184 {
1185 unsigned long pixel;
1186
1187 if (STRINGP (value))
1188 pixel = x_decode_color (f, value, BLACK_PIX_DEFAULT (f));
1189 else
1190 pixel = -1;
1191
1192 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
1193 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
1194
1195 f->output_data.x->scroll_bar_foreground_pixel = pixel;
1196 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1197 {
1198 /* Remove all scroll bars because they have wrong colors. */
1199 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1200 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1201 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1202 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1203
1204 update_face_from_frame_parameter (f, Qscroll_bar_foreground, value);
1205 redraw_frame (f);
1206 }
1207 }
1208
1209
1210 /* Set the background color for scroll bars on frame F to VALUE VALUE
1211 should be a string, a color name. If it isn't a string or isn't a
1212 valid color name, do nothing. OLDVAL is the old value of the frame
1213 parameter. */
1214
1215 static void
1216 x_set_scroll_bar_background (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1217 {
1218 unsigned long pixel;
1219
1220 if (STRINGP (value))
1221 pixel = x_decode_color (f, value, WHITE_PIX_DEFAULT (f));
1222 else
1223 pixel = -1;
1224
1225 if (f->output_data.x->scroll_bar_background_pixel != -1)
1226 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
1227
1228 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
1229 /* Scrollbar shadow colors. */
1230 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
1231 {
1232 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
1233 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
1234 }
1235 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
1236 {
1237 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
1238 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
1239 }
1240 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
1241
1242 f->output_data.x->scroll_bar_background_pixel = pixel;
1243 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1244 {
1245 /* Remove all scroll bars because they have wrong colors. */
1246 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1247 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1248 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1249 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1250
1251 update_face_from_frame_parameter (f, Qscroll_bar_background, value);
1252 redraw_frame (f);
1253 }
1254 }
1255
1256 \f
1257 /* Encode Lisp string STRING as a text in a format appropriate for
1258 XICCC (X Inter Client Communication Conventions).
1259
1260 This can call Lisp code, so callers must GCPRO.
1261
1262 If STRING contains only ASCII characters, do no conversion and
1263 return the string data of STRING. Otherwise, encode the text by
1264 CODING_SYSTEM, and return a newly allocated memory area which
1265 should be freed by `xfree' by a caller.
1266
1267 Store the byte length of resulting text in *TEXT_BYTES.
1268
1269 If the text contains only ASCII and Latin-1, store true in *STRING_P,
1270 which means that the `encoding' of the result can be `STRING'.
1271 Otherwise store false in *STRINGP, which means that the `encoding' of
1272 the result should be `COMPOUND_TEXT'. */
1273
1274 static unsigned char *
1275 x_encode_text (Lisp_Object string, Lisp_Object coding_system,
1276 ptrdiff_t *text_bytes, bool *stringp, bool *freep)
1277 {
1278 int result = string_xstring_p (string);
1279 struct coding_system coding;
1280
1281 if (result == 0)
1282 {
1283 /* No multibyte character in OBJ. We need not encode it. */
1284 *text_bytes = SBYTES (string);
1285 *stringp = true;
1286 *freep = false;
1287 return SDATA (string);
1288 }
1289
1290 setup_coding_system (coding_system, &coding);
1291 coding.mode |= (CODING_MODE_SAFE_ENCODING | CODING_MODE_LAST_BLOCK);
1292 /* We suppress producing escape sequences for composition. */
1293 coding.common_flags &= ~CODING_ANNOTATION_MASK;
1294 coding.destination = xnmalloc (SCHARS (string), 2);
1295 coding.dst_bytes = SCHARS (string) * 2;
1296 encode_coding_object (&coding, string, 0, 0,
1297 SCHARS (string), SBYTES (string), Qnil);
1298 *text_bytes = coding.produced;
1299 *stringp = (result == 1 || !EQ (coding_system, Qcompound_text));
1300 *freep = true;
1301 return coding.destination;
1302 }
1303
1304 \f
1305 /* Set the WM name to NAME for frame F. Also set the icon name.
1306 If the frame already has an icon name, use that, otherwise set the
1307 icon name to NAME. */
1308
1309 static void
1310 x_set_name_internal (struct frame *f, Lisp_Object name)
1311 {
1312 if (FRAME_X_WINDOW (f))
1313 {
1314 block_input ();
1315 {
1316 XTextProperty text, icon;
1317 ptrdiff_t bytes;
1318 bool stringp;
1319 bool do_free_icon_value = false, do_free_text_value = false;
1320 Lisp_Object coding_system;
1321 Lisp_Object encoded_name;
1322 Lisp_Object encoded_icon_name;
1323 struct gcpro gcpro1;
1324
1325 /* As ENCODE_UTF_8 may cause GC and relocation of string data,
1326 we use it before x_encode_text that may return string data. */
1327 GCPRO1 (name);
1328 encoded_name = ENCODE_UTF_8 (name);
1329 UNGCPRO;
1330
1331 coding_system = Qcompound_text;
1332 /* Note: Encoding strategy
1333
1334 We encode NAME by compound-text and use "COMPOUND-TEXT" in
1335 text.encoding. But, there are non-internationalized window
1336 managers which don't support that encoding. So, if NAME
1337 contains only ASCII and 8859-1 characters, encode it by
1338 iso-latin-1, and use "STRING" in text.encoding hoping that
1339 such window managers at least analyze this format correctly,
1340 i.e. treat 8-bit bytes as 8859-1 characters.
1341
1342 We may also be able to use "UTF8_STRING" in text.encoding
1343 in the future which can encode all Unicode characters.
1344 But, for the moment, there's no way to know that the
1345 current window manager supports it or not.
1346
1347 Either way, we also set the _NET_WM_NAME and _NET_WM_ICON_NAME
1348 properties. Per the EWMH specification, those two properties
1349 are always UTF8_STRING. This matches what gtk_window_set_title()
1350 does in the USE_GTK case. */
1351 text.value = x_encode_text (name, coding_system, &bytes,
1352 &stringp, &do_free_text_value);
1353 text.encoding = (stringp ? XA_STRING
1354 : FRAME_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1355 text.format = 8;
1356 text.nitems = bytes;
1357
1358 if (!STRINGP (f->icon_name))
1359 {
1360 icon = text;
1361 encoded_icon_name = encoded_name;
1362 }
1363 else
1364 {
1365 /* See the above comment "Note: Encoding strategy". */
1366 icon.value = x_encode_text (f->icon_name, coding_system, &bytes,
1367 &stringp, &do_free_icon_value);
1368 icon.encoding = (stringp ? XA_STRING
1369 : FRAME_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1370 icon.format = 8;
1371 icon.nitems = bytes;
1372
1373 encoded_icon_name = ENCODE_UTF_8 (f->icon_name);
1374 }
1375
1376 #ifdef USE_GTK
1377 gtk_window_set_title (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1378 SSDATA (encoded_name));
1379 #else /* not USE_GTK */
1380 XSetWMName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
1381 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1382 FRAME_DISPLAY_INFO (f)->Xatom_net_wm_name,
1383 FRAME_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1384 8, PropModeReplace,
1385 SDATA (encoded_name),
1386 SBYTES (encoded_name));
1387 #endif /* not USE_GTK */
1388
1389 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &icon);
1390 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1391 FRAME_DISPLAY_INFO (f)->Xatom_net_wm_icon_name,
1392 FRAME_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1393 8, PropModeReplace,
1394 SDATA (encoded_icon_name),
1395 SBYTES (encoded_icon_name));
1396
1397 if (do_free_icon_value)
1398 xfree (icon.value);
1399 if (do_free_text_value)
1400 xfree (text.value);
1401 }
1402 unblock_input ();
1403 }
1404 }
1405
1406 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1407 x_id_name.
1408
1409 If EXPLICIT is true, that indicates that lisp code is setting the
1410 name; if NAME is a string, set F's name to NAME and set
1411 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1412
1413 If EXPLICIT is false, that indicates that Emacs redisplay code is
1414 suggesting a new name, which lisp code should override; if
1415 F->explicit_name is set, ignore the new name; otherwise, set it. */
1416
1417 static void
1418 x_set_name (struct frame *f, Lisp_Object name, bool explicit)
1419 {
1420 /* Make sure that requests from lisp code override requests from
1421 Emacs redisplay code. */
1422 if (explicit)
1423 {
1424 /* If we're switching from explicit to implicit, we had better
1425 update the mode lines and thereby update the title. */
1426 if (f->explicit_name && NILP (name))
1427 update_mode_lines = 37;
1428
1429 f->explicit_name = ! NILP (name);
1430 }
1431 else if (f->explicit_name)
1432 return;
1433
1434 /* If NAME is nil, set the name to the x_id_name. */
1435 if (NILP (name))
1436 {
1437 /* Check for no change needed in this very common case
1438 before we do any consing. */
1439 if (!strcmp (FRAME_DISPLAY_INFO (f)->x_id_name,
1440 SSDATA (f->name)))
1441 return;
1442 name = build_string (FRAME_DISPLAY_INFO (f)->x_id_name);
1443 }
1444 else
1445 CHECK_STRING (name);
1446
1447 /* Don't change the name if it's already NAME. */
1448 if (! NILP (Fstring_equal (name, f->name)))
1449 return;
1450
1451 fset_name (f, name);
1452
1453 /* For setting the frame title, the title parameter should override
1454 the name parameter. */
1455 if (! NILP (f->title))
1456 name = f->title;
1457
1458 x_set_name_internal (f, name);
1459 }
1460
1461 /* This function should be called when the user's lisp code has
1462 specified a name for the frame; the name will override any set by the
1463 redisplay code. */
1464 static void
1465 x_explicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1466 {
1467 x_set_name (f, arg, true);
1468 }
1469
1470 /* This function should be called by Emacs redisplay code to set the
1471 name; names set this way will never override names set by the user's
1472 lisp code. */
1473 void
1474 x_implicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1475 {
1476 x_set_name (f, arg, false);
1477 }
1478 \f
1479 /* Change the title of frame F to NAME.
1480 If NAME is nil, use the frame name as the title. */
1481
1482 static void
1483 x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
1484 {
1485 /* Don't change the title if it's already NAME. */
1486 if (EQ (name, f->title))
1487 return;
1488
1489 update_mode_lines = 38;
1490
1491 fset_title (f, name);
1492
1493 if (NILP (name))
1494 name = f->name;
1495 else
1496 CHECK_STRING (name);
1497
1498 x_set_name_internal (f, name);
1499 }
1500
1501 void
1502 x_set_scroll_bar_default_width (struct frame *f)
1503 {
1504 int unit = FRAME_COLUMN_WIDTH (f);
1505 #ifdef USE_TOOLKIT_SCROLL_BARS
1506 #ifdef USE_GTK
1507 int minw = xg_get_default_scrollbar_width ();
1508 #else
1509 int minw = 16;
1510 #endif
1511 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
1512 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (minw + unit - 1) / unit;
1513 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = minw;
1514 #else
1515 /* The width of a non-toolkit scrollbar is 14 pixels. */
1516 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
1517 FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
1518 = FRAME_CONFIG_SCROLL_BAR_COLS (f) * unit;
1519 #endif
1520 }
1521
1522 void
1523 x_set_scroll_bar_default_height (struct frame *f)
1524 {
1525 int height = FRAME_LINE_HEIGHT (f);
1526 #ifdef USE_TOOLKIT_SCROLL_BARS
1527 #ifdef USE_GTK
1528 int min_height = xg_get_default_scrollbar_height ();
1529 #else
1530 int min_height = 16;
1531 #endif
1532 /* A minimum height of 14 doesn't look good for toolkit scroll bars. */
1533 FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) = min_height;
1534 FRAME_CONFIG_SCROLL_BAR_LINES (f) = (min_height + height - 1) / height;
1535 #else
1536 /* The height of a non-toolkit scrollbar is 14 pixels. */
1537 FRAME_CONFIG_SCROLL_BAR_LINES (f) = (14 + height - 1) / height;
1538
1539 /* Use all of that space (aside from required margins) for the
1540 scroll bar. */
1541 FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) = 14;
1542 #endif
1543 }
1544
1545 \f
1546 /* Record in frame F the specified or default value according to ALIST
1547 of the parameter named PROP (a Lisp symbol). If no value is
1548 specified for PROP, look for an X default for XPROP on the frame
1549 named NAME. If that is not found either, use the value DEFLT. */
1550
1551 static Lisp_Object
1552 x_default_scroll_bar_color_parameter (struct frame *f,
1553 Lisp_Object alist, Lisp_Object prop,
1554 const char *xprop, const char *xclass,
1555 bool foreground_p)
1556 {
1557 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1558 Lisp_Object tem;
1559
1560 tem = x_get_arg (dpyinfo, alist, prop, xprop, xclass, RES_TYPE_STRING);
1561 if (EQ (tem, Qunbound))
1562 {
1563 #ifdef USE_TOOLKIT_SCROLL_BARS
1564
1565 /* See if an X resource for the scroll bar color has been
1566 specified. */
1567 AUTO_STRING (foreground, "foreground");
1568 AUTO_STRING (background, "foreground");
1569 AUTO_STRING (verticalScrollBar, "verticalScrollBar");
1570 tem = (display_x_get_resource
1571 (dpyinfo, foreground_p ? foreground : background,
1572 empty_unibyte_string,
1573 verticalScrollBar,
1574 empty_unibyte_string));
1575 if (!STRINGP (tem))
1576 {
1577 /* If nothing has been specified, scroll bars will use a
1578 toolkit-dependent default. Because these defaults are
1579 difficult to get at without actually creating a scroll
1580 bar, use nil to indicate that no color has been
1581 specified. */
1582 tem = Qnil;
1583 }
1584
1585 #else /* not USE_TOOLKIT_SCROLL_BARS */
1586
1587 tem = Qnil;
1588
1589 #endif /* not USE_TOOLKIT_SCROLL_BARS */
1590 }
1591
1592 AUTO_FRAME_ARG (arg, prop, tem);
1593 x_set_frame_parameters (f, arg);
1594 return tem;
1595 }
1596
1597
1598
1599 \f
1600 #ifdef USE_X_TOOLKIT
1601
1602 /* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
1603 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
1604 already be present because of the toolkit (Motif adds some of them,
1605 for example, but Xt doesn't). */
1606
1607 static void
1608 hack_wm_protocols (struct frame *f, Widget widget)
1609 {
1610 Display *dpy = XtDisplay (widget);
1611 Window w = XtWindow (widget);
1612 bool need_delete = true;
1613 bool need_focus = true;
1614 bool need_save = true;
1615
1616 block_input ();
1617 {
1618 Atom type;
1619 unsigned char *catoms;
1620 int format = 0;
1621 unsigned long nitems = 0;
1622 unsigned long bytes_after;
1623
1624 if ((XGetWindowProperty (dpy, w,
1625 FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
1626 0, 100, False, XA_ATOM,
1627 &type, &format, &nitems, &bytes_after,
1628 &catoms)
1629 == Success)
1630 && format == 32 && type == XA_ATOM)
1631 {
1632 Atom *atoms = (Atom *) catoms;
1633 while (nitems > 0)
1634 {
1635 nitems--;
1636 if (atoms[nitems]
1637 == FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window)
1638 need_delete = false;
1639 else if (atoms[nitems]
1640 == FRAME_DISPLAY_INFO (f)->Xatom_wm_take_focus)
1641 need_focus = false;
1642 else if (atoms[nitems]
1643 == FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself)
1644 need_save = false;
1645 }
1646 }
1647 if (catoms)
1648 XFree (catoms);
1649 }
1650 {
1651 Atom props[10];
1652 int count = 0;
1653 if (need_delete)
1654 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window;
1655 if (need_focus)
1656 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_take_focus;
1657 if (need_save)
1658 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
1659 if (count)
1660 XChangeProperty (dpy, w, FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
1661 XA_ATOM, 32, PropModeAppend,
1662 (unsigned char *) props, count);
1663 }
1664 unblock_input ();
1665 }
1666 #endif
1667
1668
1669 \f
1670 /* Support routines for XIC (X Input Context). */
1671
1672 #ifdef HAVE_X_I18N
1673
1674 static XFontSet xic_create_xfontset (struct frame *);
1675 static XIMStyle best_xim_style (XIMStyles *);
1676
1677
1678 /* Supported XIM styles, ordered by preference. */
1679
1680 static const XIMStyle supported_xim_styles[] =
1681 {
1682 XIMPreeditPosition | XIMStatusArea,
1683 XIMPreeditPosition | XIMStatusNothing,
1684 XIMPreeditPosition | XIMStatusNone,
1685 XIMPreeditNothing | XIMStatusArea,
1686 XIMPreeditNothing | XIMStatusNothing,
1687 XIMPreeditNothing | XIMStatusNone,
1688 XIMPreeditNone | XIMStatusArea,
1689 XIMPreeditNone | XIMStatusNothing,
1690 XIMPreeditNone | XIMStatusNone,
1691 0,
1692 };
1693
1694
1695 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
1696 /* Create an X fontset on frame F with base font name BASE_FONTNAME. */
1697
1698 static const char xic_default_fontset[] = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
1699
1700 /* Create an Xt fontset spec from the name of a base font.
1701 If `motif' is True use the Motif syntax. */
1702 char *
1703 xic_create_fontsetname (const char *base_fontname, bool motif)
1704 {
1705 const char *sep = motif ? ";" : ",";
1706 char *fontsetname;
1707 char *z;
1708
1709 /* Make a fontset name from the base font name. */
1710 if (xic_default_fontset == base_fontname)
1711 {
1712 /* There is no base font name, use the default. */
1713 fontsetname = xmalloc (strlen (base_fontname) + 2);
1714 z = stpcpy (fontsetname, base_fontname);
1715 }
1716 else
1717 {
1718 /* Make a fontset name from the base font name.
1719 The font set will be made of the following elements:
1720 - the base font.
1721 - the base font where the charset spec is replaced by -*-*.
1722 - the same but with the family also replaced with -*-*-. */
1723 const char *p = base_fontname;
1724 ptrdiff_t i;
1725
1726 for (i = 0; *p; p++)
1727 if (*p == '-') i++;
1728 if (i != 14)
1729 {
1730 /* As the font name doesn't conform to XLFD, we can't
1731 modify it to generalize it to allcs and allfamilies.
1732 Use the specified font plus the default. */
1733 fontsetname = xmalloc (strlen (base_fontname)
1734 + strlen (xic_default_fontset) + 3);
1735 z = stpcpy (fontsetname, base_fontname);
1736 z = stpcpy (z, sep);
1737 z = stpcpy (z, xic_default_fontset);
1738 }
1739 else
1740 {
1741 ptrdiff_t len;
1742 const char *p1 = NULL, *p2 = NULL, *p3 = NULL;
1743 char *font_allcs = NULL;
1744 char *font_allfamilies = NULL;
1745 char *font_all = NULL;
1746 const char *allcs = "*-*-*-*-*-*-*";
1747 const char *allfamilies = "-*-*-";
1748 const char *all = "*-*-*-*-";
1749 char *base;
1750
1751 for (i = 0, p = base_fontname; i < 8; p++)
1752 {
1753 if (*p == '-')
1754 {
1755 i++;
1756 if (i == 3)
1757 p1 = p + 1;
1758 else if (i == 7)
1759 p2 = p + 1;
1760 else if (i == 6)
1761 p3 = p + 1;
1762 }
1763 }
1764 /* If base_fontname specifies ADSTYLE, make it a
1765 wildcard. */
1766 if (*p3 != '*')
1767 {
1768 ptrdiff_t diff = (p2 - p3) - 2;
1769
1770 base = alloca (strlen (base_fontname) + 1);
1771 memcpy (base, base_fontname, p3 - base_fontname);
1772 base[p3 - base_fontname] = '*';
1773 base[(p3 - base_fontname) + 1] = '-';
1774 strcpy (base + (p3 - base_fontname) + 2, p2);
1775 p = base + (p - base_fontname) - diff;
1776 p1 = base + (p1 - base_fontname);
1777 p2 = base + (p2 - base_fontname) - diff;
1778 base_fontname = base;
1779 }
1780
1781 /* Build the font spec that matches all charsets. */
1782 len = p - base_fontname + strlen (allcs) + 1;
1783 font_allcs = alloca (len);
1784 memcpy (font_allcs, base_fontname, p - base_fontname);
1785 strcpy (font_allcs + (p - base_fontname), allcs);
1786
1787 /* Build the font spec that matches all families and
1788 add-styles. */
1789 len = p - p1 + strlen (allcs) + strlen (allfamilies) + 1;
1790 font_allfamilies = alloca (len);
1791 strcpy (font_allfamilies, allfamilies);
1792 memcpy (font_allfamilies + strlen (allfamilies), p1, p - p1);
1793 strcpy (font_allfamilies + strlen (allfamilies) + (p - p1), allcs);
1794
1795 /* Build the font spec that matches all. */
1796 len = p - p2 + strlen (allcs) + strlen (all) + strlen (allfamilies) + 1;
1797 font_all = alloca (len);
1798 z = stpcpy (font_all, allfamilies);
1799 z = stpcpy (z, all);
1800 memcpy (z, p2, p - p2);
1801 strcpy (z + (p - p2), allcs);
1802
1803 /* Build the actual font set name. */
1804 len = strlen (base_fontname) + strlen (font_allcs)
1805 + strlen (font_allfamilies) + strlen (font_all) + 5;
1806 fontsetname = xmalloc (len);
1807 z = stpcpy (fontsetname, base_fontname);
1808 z = stpcpy (z, sep);
1809 z = stpcpy (z, font_allcs);
1810 z = stpcpy (z, sep);
1811 z = stpcpy (z, font_allfamilies);
1812 z = stpcpy (z, sep);
1813 z = stpcpy (z, font_all);
1814 }
1815 }
1816 if (motif)
1817 strcpy (z, ":");
1818 return fontsetname;
1819 }
1820 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
1821
1822 #ifdef DEBUG_XIC_FONTSET
1823 static void
1824 print_fontset_result (XFontSet xfs, char *name, char **missing_list,
1825 int missing_count)
1826 {
1827 if (xfs)
1828 fprintf (stderr, "XIC Fontset created: %s\n", name);
1829 else
1830 {
1831 fprintf (stderr, "XIC Fontset failed: %s\n", name);
1832 while (missing_count-- > 0)
1833 {
1834 fprintf (stderr, " missing: %s\n", *missing_list);
1835 missing_list++;
1836 }
1837 }
1838
1839 }
1840 #endif
1841
1842 static XFontSet
1843 xic_create_xfontset (struct frame *f)
1844 {
1845 XFontSet xfs = NULL;
1846 struct font *font = FRAME_FONT (f);
1847 int pixel_size = font->pixel_size;
1848 Lisp_Object rest, frame;
1849
1850 /* See if there is another frame already using same fontset. */
1851 FOR_EACH_FRAME (rest, frame)
1852 {
1853 struct frame *cf = XFRAME (frame);
1854
1855 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
1856 && FRAME_DISPLAY_INFO (cf) == FRAME_DISPLAY_INFO (f)
1857 && FRAME_FONT (f)
1858 && FRAME_FONT (f)->pixel_size == pixel_size)
1859 {
1860 xfs = FRAME_XIC_FONTSET (cf);
1861 break;
1862 }
1863 }
1864
1865 if (! xfs)
1866 {
1867 char buf[256];
1868 char **missing_list;
1869 int missing_count;
1870 char *def_string;
1871 const char *xlfd_format = "-*-*-medium-r-normal--%d-*-*-*-*-*";
1872
1873 sprintf (buf, xlfd_format, pixel_size);
1874 missing_list = NULL;
1875 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
1876 &missing_list, &missing_count, &def_string);
1877 #ifdef DEBUG_XIC_FONTSET
1878 print_fontset_result (xfs, buf, missing_list, missing_count);
1879 #endif
1880 if (missing_list)
1881 XFreeStringList (missing_list);
1882 if (! xfs)
1883 {
1884 /* List of pixel sizes most likely available. Find one that
1885 is closest to pixel_size. */
1886 int sizes[] = {0, 8, 10, 11, 12, 14, 17, 18, 20, 24, 26, 34, 0};
1887 int *smaller, *larger;
1888
1889 for (smaller = sizes; smaller[1]; smaller++)
1890 if (smaller[1] >= pixel_size)
1891 break;
1892 larger = smaller + 1;
1893 if (*larger == pixel_size)
1894 larger++;
1895 while (*smaller || *larger)
1896 {
1897 int this_size;
1898
1899 if (! *larger)
1900 this_size = *smaller--;
1901 else if (! *smaller)
1902 this_size = *larger++;
1903 else if (pixel_size - *smaller < *larger - pixel_size)
1904 this_size = *smaller--;
1905 else
1906 this_size = *larger++;
1907 sprintf (buf, xlfd_format, this_size);
1908 missing_list = NULL;
1909 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
1910 &missing_list, &missing_count, &def_string);
1911 #ifdef DEBUG_XIC_FONTSET
1912 print_fontset_result (xfs, buf, missing_list, missing_count);
1913 #endif
1914 if (missing_list)
1915 XFreeStringList (missing_list);
1916 if (xfs)
1917 break;
1918 }
1919 }
1920 if (! xfs)
1921 {
1922 const char *last_resort = "-*-*-*-r-normal--*-*-*-*-*-*";
1923
1924 missing_list = NULL;
1925 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), last_resort,
1926 &missing_list, &missing_count, &def_string);
1927 #ifdef DEBUG_XIC_FONTSET
1928 print_fontset_result (xfs, last_resort, missing_list, missing_count);
1929 #endif
1930 if (missing_list)
1931 XFreeStringList (missing_list);
1932 }
1933
1934 }
1935
1936 return xfs;
1937 }
1938
1939 /* Free the X fontset of frame F if it is the last frame using it. */
1940
1941 void
1942 xic_free_xfontset (struct frame *f)
1943 {
1944 Lisp_Object rest, frame;
1945 bool shared_p = false;
1946
1947 if (!FRAME_XIC_FONTSET (f))
1948 return;
1949
1950 /* See if there is another frame sharing the same fontset. */
1951 FOR_EACH_FRAME (rest, frame)
1952 {
1953 struct frame *cf = XFRAME (frame);
1954 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
1955 && FRAME_DISPLAY_INFO (cf) == FRAME_DISPLAY_INFO (f)
1956 && FRAME_XIC_FONTSET (cf) == FRAME_XIC_FONTSET (f))
1957 {
1958 shared_p = true;
1959 break;
1960 }
1961 }
1962
1963 if (!shared_p)
1964 /* The fontset is not used anymore. It is safe to free it. */
1965 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
1966
1967 FRAME_XIC_FONTSET (f) = NULL;
1968 }
1969
1970
1971 /* Value is the best input style, given user preferences USER (already
1972 checked to be supported by Emacs), and styles supported by the
1973 input method XIM. */
1974
1975 static XIMStyle
1976 best_xim_style (XIMStyles *xim)
1977 {
1978 int i, j;
1979 int nr_supported = ARRAYELTS (supported_xim_styles);
1980
1981 for (i = 0; i < nr_supported; ++i)
1982 for (j = 0; j < xim->count_styles; ++j)
1983 if (supported_xim_styles[i] == xim->supported_styles[j])
1984 return supported_xim_styles[i];
1985
1986 /* Return the default style. */
1987 return XIMPreeditNothing | XIMStatusNothing;
1988 }
1989
1990 /* Create XIC for frame F. */
1991
1992 void
1993 create_frame_xic (struct frame *f)
1994 {
1995 XIM xim;
1996 XIC xic = NULL;
1997 XFontSet xfs = NULL;
1998 XVaNestedList status_attr = NULL;
1999 XVaNestedList preedit_attr = NULL;
2000 XRectangle s_area;
2001 XPoint spot;
2002 XIMStyle xic_style;
2003
2004 if (FRAME_XIC (f))
2005 goto out;
2006
2007 xim = FRAME_X_XIM (f);
2008 if (!xim)
2009 goto out;
2010
2011 /* Determine XIC style. */
2012 xic_style = best_xim_style (FRAME_X_XIM_STYLES (f));
2013
2014 /* Create X fontset. */
2015 if (xic_style & (XIMPreeditPosition | XIMStatusArea))
2016 {
2017 xfs = xic_create_xfontset (f);
2018 if (!xfs)
2019 goto out;
2020
2021 FRAME_XIC_FONTSET (f) = xfs;
2022 }
2023
2024 if (xic_style & XIMPreeditPosition)
2025 {
2026 spot.x = 0; spot.y = 1;
2027 preedit_attr = XVaCreateNestedList (0,
2028 XNFontSet, xfs,
2029 XNForeground,
2030 FRAME_FOREGROUND_PIXEL (f),
2031 XNBackground,
2032 FRAME_BACKGROUND_PIXEL (f),
2033 (xic_style & XIMPreeditPosition
2034 ? XNSpotLocation
2035 : NULL),
2036 &spot,
2037 NULL);
2038
2039 if (!preedit_attr)
2040 goto out;
2041 }
2042
2043 if (xic_style & XIMStatusArea)
2044 {
2045 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
2046 status_attr = XVaCreateNestedList (0,
2047 XNArea,
2048 &s_area,
2049 XNFontSet,
2050 xfs,
2051 XNForeground,
2052 FRAME_FOREGROUND_PIXEL (f),
2053 XNBackground,
2054 FRAME_BACKGROUND_PIXEL (f),
2055 NULL);
2056
2057 if (!status_attr)
2058 goto out;
2059 }
2060
2061 if (preedit_attr && status_attr)
2062 xic = XCreateIC (xim,
2063 XNInputStyle, xic_style,
2064 XNClientWindow, FRAME_X_WINDOW (f),
2065 XNFocusWindow, FRAME_X_WINDOW (f),
2066 XNStatusAttributes, status_attr,
2067 XNPreeditAttributes, preedit_attr,
2068 NULL);
2069 else if (preedit_attr)
2070 xic = XCreateIC (xim,
2071 XNInputStyle, xic_style,
2072 XNClientWindow, FRAME_X_WINDOW (f),
2073 XNFocusWindow, FRAME_X_WINDOW (f),
2074 XNPreeditAttributes, preedit_attr,
2075 NULL);
2076 else if (status_attr)
2077 xic = XCreateIC (xim,
2078 XNInputStyle, xic_style,
2079 XNClientWindow, FRAME_X_WINDOW (f),
2080 XNFocusWindow, FRAME_X_WINDOW (f),
2081 XNStatusAttributes, status_attr,
2082 NULL);
2083 else
2084 xic = XCreateIC (xim,
2085 XNInputStyle, xic_style,
2086 XNClientWindow, FRAME_X_WINDOW (f),
2087 XNFocusWindow, FRAME_X_WINDOW (f),
2088 NULL);
2089
2090 if (!xic)
2091 goto out;
2092
2093 FRAME_XIC (f) = xic;
2094 FRAME_XIC_STYLE (f) = xic_style;
2095 xfs = NULL; /* Don't free below. */
2096
2097 out:
2098
2099 if (xfs)
2100 free_frame_xic (f);
2101
2102 if (preedit_attr)
2103 XFree (preedit_attr);
2104
2105 if (status_attr)
2106 XFree (status_attr);
2107 }
2108
2109
2110 /* Destroy XIC and free XIC fontset of frame F, if any. */
2111
2112 void
2113 free_frame_xic (struct frame *f)
2114 {
2115 if (FRAME_XIC (f) == NULL)
2116 return;
2117
2118 XDestroyIC (FRAME_XIC (f));
2119 xic_free_xfontset (f);
2120
2121 FRAME_XIC (f) = NULL;
2122 }
2123
2124
2125 /* Place preedit area for XIC of window W's frame to specified
2126 pixel position X/Y. X and Y are relative to window W. */
2127
2128 void
2129 xic_set_preeditarea (struct window *w, int x, int y)
2130 {
2131 struct frame *f = XFRAME (w->frame);
2132 XVaNestedList attr;
2133 XPoint spot;
2134
2135 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x) + WINDOW_LEFT_FRINGE_WIDTH (w);
2136 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
2137 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
2138 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2139 XFree (attr);
2140 }
2141
2142
2143 /* Place status area for XIC in bottom right corner of frame F.. */
2144
2145 void
2146 xic_set_statusarea (struct frame *f)
2147 {
2148 XIC xic = FRAME_XIC (f);
2149 XVaNestedList attr;
2150 XRectangle area;
2151 XRectangle *needed;
2152
2153 /* Negotiate geometry of status area. If input method has existing
2154 status area, use its current size. */
2155 area.x = area.y = area.width = area.height = 0;
2156 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
2157 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2158 XFree (attr);
2159
2160 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
2161 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2162 XFree (attr);
2163
2164 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
2165 {
2166 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
2167 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2168 XFree (attr);
2169 }
2170
2171 area.width = needed->width;
2172 area.height = needed->height;
2173 area.x = FRAME_PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
2174 area.y = (FRAME_PIXEL_HEIGHT (f) - area.height
2175 - FRAME_MENUBAR_HEIGHT (f)
2176 - FRAME_TOOLBAR_TOP_HEIGHT (f)
2177 - FRAME_INTERNAL_BORDER_WIDTH (f));
2178 XFree (needed);
2179
2180 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
2181 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2182 XFree (attr);
2183 }
2184
2185
2186 /* Set X fontset for XIC of frame F, using base font name
2187 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
2188
2189 void
2190 xic_set_xfontset (struct frame *f, const char *base_fontname)
2191 {
2192 XVaNestedList attr;
2193 XFontSet xfs;
2194
2195 xic_free_xfontset (f);
2196
2197 xfs = xic_create_xfontset (f);
2198
2199 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
2200 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
2201 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2202 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
2203 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
2204 XFree (attr);
2205
2206 FRAME_XIC_FONTSET (f) = xfs;
2207 }
2208
2209 #endif /* HAVE_X_I18N */
2210
2211
2212 \f
2213 #ifdef USE_X_TOOLKIT
2214
2215 /* Create and set up the X widget for frame F. */
2216
2217 static void
2218 x_window (struct frame *f, long window_prompting)
2219 {
2220 XClassHint class_hints;
2221 XSetWindowAttributes attributes;
2222 unsigned long attribute_mask;
2223 Widget shell_widget;
2224 Widget pane_widget;
2225 Widget frame_widget;
2226 Arg al[25];
2227 int ac;
2228
2229 block_input ();
2230
2231 /* Use the resource name as the top-level widget name
2232 for looking up resources. Make a non-Lisp copy
2233 for the window manager, so GC relocation won't bother it.
2234
2235 Elsewhere we specify the window name for the window manager. */
2236 f->namebuf = xlispstrdup (Vx_resource_name);
2237
2238 ac = 0;
2239 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
2240 XtSetArg (al[ac], XtNinput, 1); ac++;
2241 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2242 XtSetArg (al[ac], XtNborderWidth, f->border_width); ac++;
2243 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2244 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2245 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2246 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
2247 applicationShellWidgetClass,
2248 FRAME_X_DISPLAY (f), al, ac);
2249
2250 f->output_data.x->widget = shell_widget;
2251 /* maybe_set_screen_title_format (shell_widget); */
2252
2253 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
2254 NULL, shell_widget, False,
2255 NULL, NULL, NULL, NULL);
2256
2257 ac = 0;
2258 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2259 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2260 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2261 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2262 XtSetValues (pane_widget, al, ac);
2263 f->output_data.x->column_widget = pane_widget;
2264
2265 /* mappedWhenManaged to false tells to the paned window to not map/unmap
2266 the emacs screen when changing menubar. This reduces flickering. */
2267
2268 ac = 0;
2269 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2270 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
2271 XtSetArg (al[ac], XtNallowResize, 1); ac++;
2272 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
2273 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
2274 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2275 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2276 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2277 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2278 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
2279 al, ac);
2280
2281 f->output_data.x->edit_widget = frame_widget;
2282
2283 XtManageChild (frame_widget);
2284
2285 /* Do some needed geometry management. */
2286 {
2287 Arg gal[3];
2288 int gac = 0;
2289 int extra_borders = 0;
2290 int menubar_size
2291 = (f->output_data.x->menubar_widget
2292 ? (f->output_data.x->menubar_widget->core.height
2293 + f->output_data.x->menubar_widget->core.border_width)
2294 : 0);
2295
2296 #if false /* Experimentally, we now get the right results
2297 for -geometry -0-0 without this. 24 Aug 96, rms. */
2298 if (FRAME_EXTERNAL_MENU_BAR (f))
2299 {
2300 Dimension ibw = 0;
2301 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
2302 menubar_size += ibw;
2303 }
2304 #endif
2305
2306 FRAME_MENUBAR_HEIGHT (f) = menubar_size;
2307
2308 #ifndef USE_LUCID
2309 /* Motif seems to need this amount added to the sizes
2310 specified for the shell widget. The Athena/Lucid widgets don't.
2311 Both conclusions reached experimentally. -- rms. */
2312 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
2313 &extra_borders, NULL);
2314 extra_borders *= 2;
2315 #endif
2316
2317 f->shell_position = xmalloc (sizeof "=x++" + 4 * INT_STRLEN_BOUND (int));
2318
2319 /* Convert our geometry parameters into a geometry string
2320 and specify it.
2321 Note that we do not specify here whether the position
2322 is a user-specified or program-specified one.
2323 We pass that information later, in x_wm_set_size_hints. */
2324 {
2325 int left = f->left_pos;
2326 bool xneg = (window_prompting & XNegative) != 0;
2327 int top = f->top_pos;
2328 bool yneg = (window_prompting & YNegative) != 0;
2329 if (xneg)
2330 left = -left;
2331 if (yneg)
2332 top = -top;
2333
2334 if (window_prompting & USPosition)
2335 sprintf (f->shell_position, "=%dx%d%c%d%c%d",
2336 FRAME_PIXEL_WIDTH (f) + extra_borders,
2337 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders,
2338 (xneg ? '-' : '+'), left,
2339 (yneg ? '-' : '+'), top);
2340 else
2341 {
2342 sprintf (f->shell_position, "=%dx%d",
2343 FRAME_PIXEL_WIDTH (f) + extra_borders,
2344 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders);
2345
2346 /* Setting x and y when the position is not specified in
2347 the geometry string will set program position in the WM hints.
2348 If Emacs had just one program position, we could set it in
2349 fallback resources, but since each make-frame call can specify
2350 different program positions, this is easier. */
2351 XtSetArg (gal[gac], XtNx, left); gac++;
2352 XtSetArg (gal[gac], XtNy, top); gac++;
2353 }
2354 }
2355
2356 XtSetArg (gal[gac], XtNgeometry, f->shell_position); gac++;
2357 XtSetValues (shell_widget, gal, gac);
2358 }
2359
2360 XtManageChild (pane_widget);
2361 XtRealizeWidget (shell_widget);
2362
2363 if (FRAME_X_EMBEDDED_P (f))
2364 XReparentWindow (FRAME_X_DISPLAY (f), XtWindow (shell_widget),
2365 f->output_data.x->parent_desc, 0, 0);
2366
2367 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
2368
2369 validate_x_resource_name ();
2370
2371 class_hints.res_name = SSDATA (Vx_resource_name);
2372 class_hints.res_class = SSDATA (Vx_resource_class);
2373 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
2374
2375 #ifdef HAVE_X_I18N
2376 FRAME_XIC (f) = NULL;
2377 if (use_xim)
2378 create_frame_xic (f);
2379 #endif
2380
2381 f->output_data.x->wm_hints.input = True;
2382 f->output_data.x->wm_hints.flags |= InputHint;
2383 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2384 &f->output_data.x->wm_hints);
2385
2386 hack_wm_protocols (f, shell_widget);
2387
2388 #ifdef HACK_EDITRES
2389 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
2390 #endif
2391
2392 /* Do a stupid property change to force the server to generate a
2393 PropertyNotify event so that the event_stream server timestamp will
2394 be initialized to something relevant to the time we created the window.
2395 */
2396 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
2397 FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
2398 XA_ATOM, 32, PropModeAppend, NULL, 0);
2399
2400 /* Make all the standard events reach the Emacs frame. */
2401 attributes.event_mask = STANDARD_EVENT_SET;
2402
2403 #ifdef HAVE_X_I18N
2404 if (FRAME_XIC (f))
2405 {
2406 /* XIM server might require some X events. */
2407 unsigned long fevent = NoEventMask;
2408 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2409 attributes.event_mask |= fevent;
2410 }
2411 #endif /* HAVE_X_I18N */
2412
2413 attribute_mask = CWEventMask;
2414 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
2415 attribute_mask, &attributes);
2416
2417 XtMapWidget (frame_widget);
2418
2419 /* x_set_name normally ignores requests to set the name if the
2420 requested name is the same as the current name. This is the one
2421 place where that assumption isn't correct; f->name is set, but
2422 the X server hasn't been told. */
2423 {
2424 Lisp_Object name;
2425 bool explicit = f->explicit_name;
2426
2427 f->explicit_name = false;
2428 name = f->name;
2429 fset_name (f, Qnil);
2430 x_set_name (f, name, explicit);
2431 }
2432
2433 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2434 f->output_data.x->current_cursor
2435 = f->output_data.x->text_cursor);
2436
2437 unblock_input ();
2438
2439 /* This is a no-op, except under Motif. Make sure main areas are
2440 set to something reasonable, in case we get an error later. */
2441 lw_set_main_areas (pane_widget, 0, frame_widget);
2442 }
2443
2444 #else /* not USE_X_TOOLKIT */
2445 #ifdef USE_GTK
2446 static void
2447 x_window (struct frame *f)
2448 {
2449 if (! xg_create_frame_widgets (f))
2450 error ("Unable to create window");
2451
2452 #ifdef HAVE_X_I18N
2453 FRAME_XIC (f) = NULL;
2454 if (use_xim)
2455 {
2456 block_input ();
2457 create_frame_xic (f);
2458 if (FRAME_XIC (f))
2459 {
2460 /* XIM server might require some X events. */
2461 unsigned long fevent = NoEventMask;
2462 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2463
2464 if (fevent != NoEventMask)
2465 {
2466 XSetWindowAttributes attributes;
2467 XWindowAttributes wattr;
2468 unsigned long attribute_mask;
2469
2470 XGetWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2471 &wattr);
2472 attributes.event_mask = wattr.your_event_mask | fevent;
2473 attribute_mask = CWEventMask;
2474 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2475 attribute_mask, &attributes);
2476 }
2477 }
2478 unblock_input ();
2479 }
2480 #endif
2481 }
2482
2483 #else /*! USE_GTK */
2484 /* Create and set up the X window for frame F. */
2485
2486 static void
2487 x_window (struct frame *f)
2488 {
2489 XClassHint class_hints;
2490 XSetWindowAttributes attributes;
2491 unsigned long attribute_mask;
2492
2493 attributes.background_pixel = FRAME_BACKGROUND_PIXEL (f);
2494 attributes.border_pixel = f->output_data.x->border_pixel;
2495 attributes.bit_gravity = StaticGravity;
2496 attributes.backing_store = NotUseful;
2497 attributes.save_under = True;
2498 attributes.event_mask = STANDARD_EVENT_SET;
2499 attributes.colormap = FRAME_X_COLORMAP (f);
2500 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
2501 | CWColormap);
2502
2503 block_input ();
2504 FRAME_X_WINDOW (f)
2505 = XCreateWindow (FRAME_X_DISPLAY (f),
2506 f->output_data.x->parent_desc,
2507 f->left_pos,
2508 f->top_pos,
2509 FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f),
2510 f->border_width,
2511 CopyFromParent, /* depth */
2512 InputOutput, /* class */
2513 FRAME_X_VISUAL (f),
2514 attribute_mask, &attributes);
2515
2516 #ifdef HAVE_X_I18N
2517 if (use_xim)
2518 {
2519 create_frame_xic (f);
2520 if (FRAME_XIC (f))
2521 {
2522 /* XIM server might require some X events. */
2523 unsigned long fevent = NoEventMask;
2524 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2525 attributes.event_mask |= fevent;
2526 attribute_mask = CWEventMask;
2527 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2528 attribute_mask, &attributes);
2529 }
2530 }
2531 #endif /* HAVE_X_I18N */
2532
2533 validate_x_resource_name ();
2534
2535 class_hints.res_name = SSDATA (Vx_resource_name);
2536 class_hints.res_class = SSDATA (Vx_resource_class);
2537 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
2538
2539 /* This indicates that we use the "Passive Input" input model.
2540 Unless we do this, we don't get the Focus{In,Out} events that we
2541 need to draw the cursor correctly. Accursed bureaucrats.
2542 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
2543
2544 f->output_data.x->wm_hints.input = True;
2545 f->output_data.x->wm_hints.flags |= InputHint;
2546 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2547 &f->output_data.x->wm_hints);
2548 f->output_data.x->wm_hints.icon_pixmap = None;
2549
2550 /* Request "save yourself" and "delete window" commands from wm. */
2551 {
2552 Atom protocols[2];
2553 protocols[0] = FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window;
2554 protocols[1] = FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
2555 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
2556 }
2557
2558 /* x_set_name normally ignores requests to set the name if the
2559 requested name is the same as the current name. This is the one
2560 place where that assumption isn't correct; f->name is set, but
2561 the X server hasn't been told. */
2562 {
2563 Lisp_Object name;
2564 bool explicit = f->explicit_name;
2565
2566 f->explicit_name = false;
2567 name = f->name;
2568 fset_name (f, Qnil);
2569 x_set_name (f, name, explicit);
2570 }
2571
2572 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2573 f->output_data.x->current_cursor
2574 = f->output_data.x->text_cursor);
2575
2576 unblock_input ();
2577
2578 if (FRAME_X_WINDOW (f) == 0)
2579 error ("Unable to create window");
2580 }
2581
2582 #endif /* not USE_GTK */
2583 #endif /* not USE_X_TOOLKIT */
2584
2585 /* Verify that the icon position args for this window are valid. */
2586
2587 static void
2588 x_icon_verify (struct frame *f, Lisp_Object parms)
2589 {
2590 Lisp_Object icon_x, icon_y;
2591
2592 /* Set the position of the icon. Note that twm groups all
2593 icons in an icon window. */
2594 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2595 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2596 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2597 {
2598 CHECK_NUMBER (icon_x);
2599 CHECK_NUMBER (icon_y);
2600 }
2601 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2602 error ("Both left and top icon corners of icon must be specified");
2603 }
2604
2605 /* Handle the icon stuff for this window. Perhaps later we might
2606 want an x_set_icon_position which can be called interactively as
2607 well. */
2608
2609 static void
2610 x_icon (struct frame *f, Lisp_Object parms)
2611 {
2612 /* Set the position of the icon. Note that twm groups all
2613 icons in an icon window. */
2614 Lisp_Object icon_x
2615 = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2616 Lisp_Object icon_y
2617 = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2618 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2619 {
2620 CHECK_TYPE_RANGED_INTEGER (int, icon_x);
2621 CHECK_TYPE_RANGED_INTEGER (int, icon_y);
2622 }
2623 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2624 error ("Both left and top icon corners of icon must be specified");
2625
2626 block_input ();
2627
2628 if (! EQ (icon_x, Qunbound))
2629 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
2630
2631 #if false /* x_get_arg removes the visibility parameter as a side effect,
2632 but x_create_frame still needs it. */
2633 /* Start up iconic or window? */
2634 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2635 x_wm_set_window_state
2636 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
2637 Qicon)
2638 ? IconicState
2639 : NormalState));
2640 #endif
2641
2642 x_text_icon (f, SSDATA ((!NILP (f->icon_name)
2643 ? f->icon_name
2644 : f->name)));
2645
2646 unblock_input ();
2647 }
2648
2649 /* Make the GCs needed for this window, setting the
2650 background, border and mouse colors; also create the
2651 mouse cursor and the gray border tile. */
2652
2653 static void
2654 x_make_gc (struct frame *f)
2655 {
2656 XGCValues gc_values;
2657
2658 block_input ();
2659
2660 /* Create the GCs of this frame.
2661 Note that many default values are used. */
2662
2663 gc_values.foreground = FRAME_FOREGROUND_PIXEL (f);
2664 gc_values.background = FRAME_BACKGROUND_PIXEL (f);
2665 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
2666 f->output_data.x->normal_gc
2667 = XCreateGC (FRAME_X_DISPLAY (f),
2668 FRAME_X_WINDOW (f),
2669 GCLineWidth | GCForeground | GCBackground,
2670 &gc_values);
2671
2672 /* Reverse video style. */
2673 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2674 gc_values.background = FRAME_FOREGROUND_PIXEL (f);
2675 f->output_data.x->reverse_gc
2676 = XCreateGC (FRAME_X_DISPLAY (f),
2677 FRAME_X_WINDOW (f),
2678 GCForeground | GCBackground | GCLineWidth,
2679 &gc_values);
2680
2681 /* Cursor has cursor-color background, background-color foreground. */
2682 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2683 gc_values.background = f->output_data.x->cursor_pixel;
2684 gc_values.fill_style = FillOpaqueStippled;
2685 f->output_data.x->cursor_gc
2686 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2687 (GCForeground | GCBackground
2688 | GCFillStyle | GCLineWidth),
2689 &gc_values);
2690
2691 /* Create the gray border tile used when the pointer is not in
2692 the frame. Since this depends on the frame's pixel values,
2693 this must be done on a per-frame basis. */
2694 f->output_data.x->border_tile
2695 = (XCreatePixmapFromBitmapData
2696 (FRAME_X_DISPLAY (f), FRAME_DISPLAY_INFO (f)->root_window,
2697 gray_bits, gray_width, gray_height,
2698 FRAME_FOREGROUND_PIXEL (f),
2699 FRAME_BACKGROUND_PIXEL (f),
2700 DefaultDepth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f))));
2701
2702 unblock_input ();
2703 }
2704
2705
2706 /* Free what was allocated in x_make_gc. */
2707
2708 void
2709 x_free_gcs (struct frame *f)
2710 {
2711 Display *dpy = FRAME_X_DISPLAY (f);
2712
2713 block_input ();
2714
2715 if (f->output_data.x->normal_gc)
2716 {
2717 XFreeGC (dpy, f->output_data.x->normal_gc);
2718 f->output_data.x->normal_gc = 0;
2719 }
2720
2721 if (f->output_data.x->reverse_gc)
2722 {
2723 XFreeGC (dpy, f->output_data.x->reverse_gc);
2724 f->output_data.x->reverse_gc = 0;
2725 }
2726
2727 if (f->output_data.x->cursor_gc)
2728 {
2729 XFreeGC (dpy, f->output_data.x->cursor_gc);
2730 f->output_data.x->cursor_gc = 0;
2731 }
2732
2733 if (f->output_data.x->border_tile)
2734 {
2735 XFreePixmap (dpy, f->output_data.x->border_tile);
2736 f->output_data.x->border_tile = 0;
2737 }
2738
2739 unblock_input ();
2740 }
2741
2742
2743 /* Handler for signals raised during x_create_frame and
2744 x_create_tip_frame. FRAME is the frame which is partially
2745 constructed. */
2746
2747 static Lisp_Object
2748 unwind_create_frame (Lisp_Object frame)
2749 {
2750 struct frame *f = XFRAME (frame);
2751
2752 /* If frame is already dead, nothing to do. This can happen if the
2753 display is disconnected after the frame has become official, but
2754 before x_create_frame removes the unwind protect. */
2755 if (!FRAME_LIVE_P (f))
2756 return Qnil;
2757
2758 /* If frame is ``official'', nothing to do. */
2759 if (NILP (Fmemq (frame, Vframe_list)))
2760 {
2761 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2762 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2763 #endif
2764
2765 x_free_frame_resources (f);
2766 free_glyphs (f);
2767
2768 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2769 /* Check that reference counts are indeed correct. */
2770 eassert (dpyinfo->reference_count == dpyinfo_refcount);
2771 eassert (dpyinfo->terminal->image_cache->refcount == image_cache_refcount);
2772 #endif
2773 return Qt;
2774 }
2775
2776 return Qnil;
2777 }
2778
2779 static void
2780 do_unwind_create_frame (Lisp_Object frame)
2781 {
2782 unwind_create_frame (frame);
2783 }
2784
2785 static void
2786 x_default_font_parameter (struct frame *f, Lisp_Object parms)
2787 {
2788 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2789 Lisp_Object font_param = x_get_arg (dpyinfo, parms, Qfont, NULL, NULL,
2790 RES_TYPE_STRING);
2791 Lisp_Object font = Qnil;
2792 if (EQ (font_param, Qunbound))
2793 font_param = Qnil;
2794
2795 if (NILP (font_param))
2796 {
2797 /* System font should take precedence over X resources. We suggest this
2798 regardless of font-use-system-font because .emacs may not have been
2799 read yet. */
2800 const char *system_font = xsettings_get_system_font ();
2801 if (system_font)
2802 font = font_open_by_name (f, build_unibyte_string (system_font));
2803 }
2804
2805 if (NILP (font))
2806 font = !NILP (font_param) ? font_param
2807 : x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
2808
2809 if (! FONTP (font) && ! STRINGP (font))
2810 {
2811 const char *names[]
2812 = {
2813 #ifdef HAVE_XFT
2814 /* This will find the normal Xft font. */
2815 "monospace-10",
2816 #endif
2817 "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1",
2818 "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2819 "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2820 /* This was formerly the first thing tried, but it finds
2821 too many fonts and takes too long. */
2822 "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1",
2823 /* If those didn't work, look for something which will
2824 at least work. */
2825 "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1",
2826 "fixed",
2827 NULL };
2828 int i;
2829
2830 for (i = 0; names[i]; i++)
2831 {
2832 font = font_open_by_name (f, build_unibyte_string (names[i]));
2833 if (! NILP (font))
2834 break;
2835 }
2836 if (NILP (font))
2837 error ("No suitable font was found");
2838 }
2839 else if (!NILP (font_param))
2840 {
2841 /* Remember the explicit font parameter, so we can re-apply it after
2842 we've applied the `default' face settings. */
2843 AUTO_FRAME_ARG (arg, Qfont_param, font_param);
2844 x_set_frame_parameters (f, arg);
2845 }
2846
2847 /* This call will make X resources override any system font setting. */
2848 x_default_parameter (f, parms, Qfont, font, "font", "Font", RES_TYPE_STRING);
2849 }
2850
2851
2852 DEFUN ("x-wm-set-size-hint", Fx_wm_set_size_hint, Sx_wm_set_size_hint,
2853 0, 1, 0,
2854 doc: /* Send the size hints for frame FRAME to the window manager.
2855 If FRAME is omitted or nil, use the selected frame.
2856 Signal error if FRAME is not an X frame. */)
2857 (Lisp_Object frame)
2858 {
2859 struct frame *f = decode_window_system_frame (frame);
2860
2861 block_input ();
2862 x_wm_set_size_hint (f, 0, false);
2863 unblock_input ();
2864 return Qnil;
2865 }
2866
2867 static void
2868 set_machine_and_pid_properties (struct frame *f)
2869 {
2870 /* This will set WM_CLIENT_MACHINE and WM_LOCALE_NAME. */
2871 XSetWMProperties (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), NULL, NULL,
2872 NULL, 0, NULL, NULL, NULL);
2873 pid_t pid = getpid ();
2874 if (pid <= 0xffffffffu)
2875 {
2876 unsigned long xpid = pid;
2877 XChangeProperty (FRAME_X_DISPLAY (f),
2878 FRAME_OUTER_WINDOW (f),
2879 XInternAtom (FRAME_X_DISPLAY (f),
2880 "_NET_WM_PID",
2881 False),
2882 XA_CARDINAL, 32, PropModeReplace,
2883 (unsigned char *) &xpid, 1);
2884 }
2885 }
2886
2887 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
2888 1, 1, 0,
2889 doc: /* Make a new X window, which is called a "frame" in Emacs terms.
2890 Return an Emacs frame object. PARMS is an alist of frame parameters.
2891 If the parameters specify that the frame should not have a minibuffer,
2892 and do not specify a specific minibuffer window to use, then
2893 `default-minibuffer-frame' must be a frame whose minibuffer can be
2894 shared by the new frame.
2895
2896 This function is an internal primitive--use `make-frame' instead. */)
2897 (Lisp_Object parms)
2898 {
2899 struct frame *f;
2900 Lisp_Object frame, tem;
2901 Lisp_Object name;
2902 bool minibuffer_only = false;
2903 long window_prompting = 0;
2904 ptrdiff_t count = SPECPDL_INDEX ();
2905 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
2906 Lisp_Object display;
2907 struct x_display_info *dpyinfo = NULL;
2908 Lisp_Object parent;
2909 struct kboard *kb;
2910
2911 parms = Fcopy_alist (parms);
2912
2913 /* Use this general default value to start with
2914 until we know if this frame has a specified name. */
2915 Vx_resource_name = Vinvocation_name;
2916
2917 display = x_get_arg (dpyinfo, parms, Qterminal, 0, 0, RES_TYPE_NUMBER);
2918 if (EQ (display, Qunbound))
2919 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
2920 if (EQ (display, Qunbound))
2921 display = Qnil;
2922 dpyinfo = check_x_display_info (display);
2923 kb = dpyinfo->terminal->kboard;
2924
2925 if (!dpyinfo->terminal->name)
2926 error ("Terminal is not live, can't create new frames on it");
2927
2928 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
2929 if (!STRINGP (name)
2930 && ! EQ (name, Qunbound)
2931 && ! NILP (name))
2932 error ("Invalid frame name--not a string or nil");
2933
2934 if (STRINGP (name))
2935 Vx_resource_name = name;
2936
2937 /* See if parent window is specified. */
2938 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
2939 if (EQ (parent, Qunbound))
2940 parent = Qnil;
2941 if (! NILP (parent))
2942 CHECK_NUMBER (parent);
2943
2944 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
2945 /* No need to protect DISPLAY because that's not used after passing
2946 it to make_frame_without_minibuffer. */
2947 frame = Qnil;
2948 GCPRO4 (parms, parent, name, frame);
2949 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
2950 RES_TYPE_SYMBOL);
2951 if (EQ (tem, Qnone) || NILP (tem))
2952 f = make_frame_without_minibuffer (Qnil, kb, display);
2953 else if (EQ (tem, Qonly))
2954 {
2955 f = make_minibuffer_frame ();
2956 minibuffer_only = true;
2957 }
2958 else if (WINDOWP (tem))
2959 f = make_frame_without_minibuffer (tem, kb, display);
2960 else
2961 f = make_frame (true);
2962
2963 XSETFRAME (frame, f);
2964
2965 f->terminal = dpyinfo->terminal;
2966
2967 f->output_method = output_x_window;
2968 f->output_data.x = xzalloc (sizeof *f->output_data.x);
2969 f->output_data.x->icon_bitmap = -1;
2970 FRAME_FONTSET (f) = -1;
2971 f->output_data.x->scroll_bar_foreground_pixel = -1;
2972 f->output_data.x->scroll_bar_background_pixel = -1;
2973 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
2974 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
2975 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
2976 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
2977 f->output_data.x->white_relief.pixel = -1;
2978 f->output_data.x->black_relief.pixel = -1;
2979
2980 fset_icon_name (f,
2981 x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
2982 RES_TYPE_STRING));
2983 if (! STRINGP (f->icon_name))
2984 fset_icon_name (f, Qnil);
2985
2986 FRAME_DISPLAY_INFO (f) = dpyinfo;
2987
2988 /* With FRAME_DISPLAY_INFO set up, this unwind-protect is safe. */
2989 record_unwind_protect (do_unwind_create_frame, frame);
2990
2991 /* These colors will be set anyway later, but it's important
2992 to get the color reference counts right, so initialize them! */
2993 {
2994 Lisp_Object black;
2995 struct gcpro gcpro1;
2996
2997 /* Function x_decode_color can signal an error. Make
2998 sure to initialize color slots so that we won't try
2999 to free colors we haven't allocated. */
3000 FRAME_FOREGROUND_PIXEL (f) = -1;
3001 FRAME_BACKGROUND_PIXEL (f) = -1;
3002 f->output_data.x->cursor_pixel = -1;
3003 f->output_data.x->cursor_foreground_pixel = -1;
3004 f->output_data.x->border_pixel = -1;
3005 f->output_data.x->mouse_pixel = -1;
3006
3007 black = build_string ("black");
3008 GCPRO1 (black);
3009 FRAME_FOREGROUND_PIXEL (f)
3010 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3011 FRAME_BACKGROUND_PIXEL (f)
3012 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3013 f->output_data.x->cursor_pixel
3014 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3015 f->output_data.x->cursor_foreground_pixel
3016 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3017 f->output_data.x->border_pixel
3018 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3019 f->output_data.x->mouse_pixel
3020 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3021 UNGCPRO;
3022 }
3023
3024 /* Specify the parent under which to make this X window. */
3025 if (!NILP (parent))
3026 {
3027 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
3028 f->output_data.x->explicit_parent = true;
3029 }
3030 else
3031 {
3032 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
3033 f->output_data.x->explicit_parent = false;
3034 }
3035
3036 /* Set the name; the functions to which we pass f expect the name to
3037 be set. */
3038 if (EQ (name, Qunbound) || NILP (name))
3039 {
3040 fset_name (f, build_string (dpyinfo->x_id_name));
3041 f->explicit_name = false;
3042 }
3043 else
3044 {
3045 fset_name (f, name);
3046 f->explicit_name = true;
3047 /* Use the frame's title when getting resources for this frame. */
3048 specbind (Qx_resource_name, name);
3049 }
3050
3051 #ifdef HAVE_FREETYPE
3052 #ifdef HAVE_XFT
3053 register_font_driver (&xftfont_driver, f);
3054 #else /* not HAVE_XFT */
3055 register_font_driver (&ftxfont_driver, f);
3056 #endif /* not HAVE_XFT */
3057 #endif /* HAVE_FREETYPE */
3058 register_font_driver (&xfont_driver, f);
3059
3060 x_default_parameter (f, parms, Qfont_backend, Qnil,
3061 "fontBackend", "FontBackend", RES_TYPE_STRING);
3062
3063 /* Extract the window parameters from the supplied values
3064 that are needed to determine window geometry. */
3065 x_default_font_parameter (f, parms);
3066 if (!FRAME_FONT (f))
3067 {
3068 delete_frame (frame, Qnoelisp);
3069 error ("Invalid frame font");
3070 }
3071
3072 /* Frame contents get displaced if an embedded X window has a border. */
3073 if (! FRAME_X_EMBEDDED_P (f))
3074 x_default_parameter (f, parms, Qborder_width, make_number (0),
3075 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
3076
3077 /* This defaults to 1 in order to match xterm. We recognize either
3078 internalBorderWidth or internalBorder (which is what xterm calls
3079 it). */
3080 if (NILP (Fassq (Qinternal_border_width, parms)))
3081 {
3082 Lisp_Object value;
3083
3084 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
3085 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
3086 if (! EQ (value, Qunbound))
3087 parms = Fcons (Fcons (Qinternal_border_width, value),
3088 parms);
3089 }
3090 x_default_parameter (f, parms, Qinternal_border_width,
3091 #ifdef USE_GTK /* We used to impose 0 in xg_create_frame_widgets. */
3092 make_number (0),
3093 #else
3094 make_number (1),
3095 #endif
3096 "internalBorderWidth", "internalBorderWidth",
3097 RES_TYPE_NUMBER);
3098 x_default_parameter (f, parms, Qright_divider_width, make_number (0),
3099 NULL, NULL, RES_TYPE_NUMBER);
3100 x_default_parameter (f, parms, Qbottom_divider_width, make_number (0),
3101 NULL, NULL, RES_TYPE_NUMBER);
3102 x_default_parameter (f, parms, Qvertical_scroll_bars,
3103 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3104 Qright,
3105 #else
3106 Qleft,
3107 #endif
3108 "verticalScrollBars", "ScrollBars",
3109 RES_TYPE_SYMBOL);
3110 x_default_parameter (f, parms, Qhorizontal_scroll_bars, Qnil,
3111 "horizontalScrollBars", "ScrollBars",
3112 RES_TYPE_SYMBOL);
3113 /* Also do the stuff which must be set before the window exists. */
3114 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
3115 "foreground", "Foreground", RES_TYPE_STRING);
3116 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
3117 "background", "Background", RES_TYPE_STRING);
3118 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
3119 "pointerColor", "Foreground", RES_TYPE_STRING);
3120 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
3121 "borderColor", "BorderColor", RES_TYPE_STRING);
3122 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
3123 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
3124 x_default_parameter (f, parms, Qline_spacing, Qnil,
3125 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
3126 x_default_parameter (f, parms, Qleft_fringe, Qnil,
3127 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
3128 x_default_parameter (f, parms, Qright_fringe, Qnil,
3129 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
3130
3131 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
3132 "scrollBarForeground",
3133 "ScrollBarForeground", true);
3134 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
3135 "scrollBarBackground",
3136 "ScrollBarBackground", false);
3137
3138 #ifdef GLYPH_DEBUG
3139 image_cache_refcount =
3140 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
3141 dpyinfo_refcount = dpyinfo->reference_count;
3142 #endif /* GLYPH_DEBUG */
3143
3144 /* Init faces before x_default_parameter is called for the
3145 scroll-bar-width parameter because otherwise we end up in
3146 init_iterator with a null face cache, which should not happen. */
3147 init_frame_faces (f);
3148
3149 /* The following call of change_frame_size is needed since otherwise
3150 x_set_tool_bar_lines will already work with the character sizes
3151 installed by init_frame_faces while the frame's pixel size is
3152 still calculated from a character size of 1 and we subsequently
3153 hit the (height >= 0) assertion in window_box_height.
3154
3155 The non-pixelwise code apparently worked around this because it
3156 had one frame line vs one toolbar line which left us with a zero
3157 root window height which was obviously wrong as well ... */
3158 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
3159 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 5, true,
3160 Qx_create_frame_1);
3161
3162 /* Set the menu-bar-lines and tool-bar-lines parameters. We don't
3163 look up the X resources controlling the menu-bar and tool-bar
3164 here; they are processed specially at startup, and reflected in
3165 the values of the mode variables. */
3166
3167 x_default_parameter (f, parms, Qmenu_bar_lines,
3168 NILP (Vmenu_bar_mode)
3169 ? make_number (0) : make_number (1),
3170 NULL, NULL, RES_TYPE_NUMBER);
3171 x_default_parameter (f, parms, Qtool_bar_lines,
3172 NILP (Vtool_bar_mode)
3173 ? make_number (0) : make_number (1),
3174 NULL, NULL, RES_TYPE_NUMBER);
3175
3176 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
3177 "bufferPredicate", "BufferPredicate",
3178 RES_TYPE_SYMBOL);
3179 x_default_parameter (f, parms, Qtitle, Qnil,
3180 "title", "Title", RES_TYPE_STRING);
3181 x_default_parameter (f, parms, Qwait_for_wm, Qt,
3182 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN);
3183 x_default_parameter (f, parms, Qfullscreen, Qnil,
3184 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
3185 x_default_parameter (f, parms, Qtool_bar_position,
3186 FRAME_TOOL_BAR_POSITION (f), 0, 0, RES_TYPE_SYMBOL);
3187
3188 /* Compute the size of the X window. */
3189 window_prompting = x_figure_window_size (f, parms, true);
3190
3191 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
3192 f->no_split = minibuffer_only || EQ (tem, Qt);
3193
3194 x_icon_verify (f, parms);
3195
3196 /* Create the X widget or window. */
3197 #ifdef USE_X_TOOLKIT
3198 x_window (f, window_prompting);
3199 #else
3200 x_window (f);
3201 #endif
3202
3203 x_icon (f, parms);
3204 x_make_gc (f);
3205
3206 /* Now consider the frame official. */
3207 f->terminal->reference_count++;
3208 FRAME_DISPLAY_INFO (f)->reference_count++;
3209 Vframe_list = Fcons (frame, Vframe_list);
3210
3211 /* We need to do this after creating the X window, so that the
3212 icon-creation functions can say whose icon they're describing. */
3213 x_default_parameter (f, parms, Qicon_type, Qt,
3214 "bitmapIcon", "BitmapIcon", RES_TYPE_BOOLEAN);
3215
3216 x_default_parameter (f, parms, Qauto_raise, Qnil,
3217 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3218 x_default_parameter (f, parms, Qauto_lower, Qnil,
3219 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3220 x_default_parameter (f, parms, Qcursor_type, Qbox,
3221 "cursorType", "CursorType", RES_TYPE_SYMBOL);
3222 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
3223 "scrollBarWidth", "ScrollBarWidth",
3224 RES_TYPE_NUMBER);
3225 x_default_parameter (f, parms, Qscroll_bar_height, Qnil,
3226 "scrollBarHeight", "ScrollBarHeight",
3227 RES_TYPE_NUMBER);
3228 x_default_parameter (f, parms, Qalpha, Qnil,
3229 "alpha", "Alpha", RES_TYPE_NUMBER);
3230
3231 /* Consider frame official, now. */
3232 f->can_x_set_window_size = true;
3233
3234 adjust_frame_size (f, FRAME_TEXT_WIDTH (f), FRAME_TEXT_HEIGHT (f), 0, true,
3235 Qx_create_frame_2);
3236
3237 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3238 /* Create the menu bar. */
3239 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3240 {
3241 /* If this signals an error, we haven't set size hints for the
3242 frame and we didn't make it visible. */
3243 initialize_frame_menubar (f);
3244
3245 #ifndef USE_GTK
3246 /* This is a no-op, except under Motif where it arranges the
3247 main window for the widgets on it. */
3248 lw_set_main_areas (f->output_data.x->column_widget,
3249 f->output_data.x->menubar_widget,
3250 f->output_data.x->edit_widget);
3251 #endif /* not USE_GTK */
3252 }
3253 #endif /* USE_X_TOOLKIT || USE_GTK */
3254
3255 /* Tell the server what size and position, etc, we want, and how
3256 badly we want them. This should be done after we have the menu
3257 bar so that its size can be taken into account. */
3258 block_input ();
3259 x_wm_set_size_hint (f, window_prompting, false);
3260 unblock_input ();
3261
3262 /* Make the window appear on the frame and enable display, unless
3263 the caller says not to. However, with explicit parent, Emacs
3264 cannot control visibility, so don't try. */
3265 if (! f->output_data.x->explicit_parent)
3266 {
3267 Lisp_Object visibility;
3268
3269 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
3270 RES_TYPE_SYMBOL);
3271 if (EQ (visibility, Qunbound))
3272 visibility = Qt;
3273
3274 if (EQ (visibility, Qicon))
3275 x_iconify_frame (f);
3276 else if (! NILP (visibility))
3277 x_make_frame_visible (f);
3278 else
3279 {
3280 /* Must have been Qnil. */
3281 }
3282 }
3283
3284 block_input ();
3285
3286 /* Set machine name and pid for the purpose of window managers. */
3287 set_machine_and_pid_properties (f);
3288
3289 /* Set the WM leader property. GTK does this itself, so this is not
3290 needed when using GTK. */
3291 if (dpyinfo->client_leader_window != 0)
3292 {
3293 XChangeProperty (FRAME_X_DISPLAY (f),
3294 FRAME_OUTER_WINDOW (f),
3295 dpyinfo->Xatom_wm_client_leader,
3296 XA_WINDOW, 32, PropModeReplace,
3297 (unsigned char *) &dpyinfo->client_leader_window, 1);
3298 }
3299
3300 unblock_input ();
3301
3302 /* Initialize `default-minibuffer-frame' in case this is the first
3303 frame on this terminal. */
3304 if (FRAME_HAS_MINIBUF_P (f)
3305 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
3306 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
3307 kset_default_minibuffer_frame (kb, frame);
3308
3309 /* All remaining specified parameters, which have not been "used"
3310 by x_get_arg and friends, now go in the misc. alist of the frame. */
3311 for (tem = parms; CONSP (tem); tem = XCDR (tem))
3312 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
3313 fset_param_alist (f, Fcons (XCAR (tem), f->param_alist));
3314
3315 UNGCPRO;
3316
3317 /* Make sure windows on this frame appear in calls to next-window
3318 and similar functions. */
3319 Vwindow_list = Qnil;
3320
3321 return unbind_to (count, frame);
3322 }
3323
3324
3325 /* FRAME is used only to get a handle on the X display. We don't pass the
3326 display info directly because we're called from frame.c, which doesn't
3327 know about that structure. */
3328
3329 Lisp_Object
3330 x_get_focus_frame (struct frame *frame)
3331 {
3332 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (frame);
3333 Lisp_Object xfocus;
3334 if (! dpyinfo->x_focus_frame)
3335 return Qnil;
3336
3337 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
3338 return xfocus;
3339 }
3340
3341
3342 /* In certain situations, when the window manager follows a
3343 click-to-focus policy, there seems to be no way around calling
3344 XSetInputFocus to give another frame the input focus .
3345
3346 In an ideal world, XSetInputFocus should generally be avoided so
3347 that applications don't interfere with the window manager's focus
3348 policy. But I think it's okay to use when it's clearly done
3349 following a user-command. */
3350
3351 void
3352 x_focus_frame (struct frame *f)
3353 {
3354 Display *dpy = FRAME_X_DISPLAY (f);
3355
3356 block_input ();
3357 x_catch_errors (dpy);
3358
3359 if (FRAME_X_EMBEDDED_P (f))
3360 {
3361 /* For Xembedded frames, normally the embedder forwards key
3362 events. See XEmbed Protocol Specification at
3363 http://freedesktop.org/wiki/Specifications/xembed-spec */
3364 xembed_request_focus (f);
3365 }
3366 else
3367 {
3368 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3369 RevertToParent, CurrentTime);
3370 x_ewmh_activate_frame (f);
3371 }
3372
3373 x_uncatch_errors ();
3374 unblock_input ();
3375 }
3376
3377 \f
3378 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
3379 doc: /* Internal function called by `color-defined-p', which see
3380 .\(Note that the Nextstep version of this function ignores FRAME.) */)
3381 (Lisp_Object color, Lisp_Object frame)
3382 {
3383 XColor foo;
3384 struct frame *f = decode_window_system_frame (frame);
3385
3386 CHECK_STRING (color);
3387
3388 if (x_defined_color (f, SSDATA (color), &foo, false))
3389 return Qt;
3390 else
3391 return Qnil;
3392 }
3393
3394 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
3395 doc: /* Internal function called by `color-values', which see. */)
3396 (Lisp_Object color, Lisp_Object frame)
3397 {
3398 XColor foo;
3399 struct frame *f = decode_window_system_frame (frame);
3400
3401 CHECK_STRING (color);
3402
3403 if (x_defined_color (f, SSDATA (color), &foo, false))
3404 return list3i (foo.red, foo.green, foo.blue);
3405 else
3406 return Qnil;
3407 }
3408
3409 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
3410 doc: /* Internal function called by `display-color-p', which see. */)
3411 (Lisp_Object terminal)
3412 {
3413 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3414
3415 if (dpyinfo->n_planes <= 2)
3416 return Qnil;
3417
3418 switch (dpyinfo->visual->class)
3419 {
3420 case StaticColor:
3421 case PseudoColor:
3422 case TrueColor:
3423 case DirectColor:
3424 return Qt;
3425
3426 default:
3427 return Qnil;
3428 }
3429 }
3430
3431 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
3432 0, 1, 0,
3433 doc: /* Return t if the X display supports shades of gray.
3434 Note that color displays do support shades of gray.
3435 The optional argument TERMINAL specifies which display to ask about.
3436 TERMINAL should be a terminal object, a frame or a display name (a string).
3437 If omitted or nil, that stands for the selected frame's display. */)
3438 (Lisp_Object terminal)
3439 {
3440 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3441
3442 if (dpyinfo->n_planes <= 1)
3443 return Qnil;
3444
3445 switch (dpyinfo->visual->class)
3446 {
3447 case StaticColor:
3448 case PseudoColor:
3449 case TrueColor:
3450 case DirectColor:
3451 case StaticGray:
3452 case GrayScale:
3453 return Qt;
3454
3455 default:
3456 return Qnil;
3457 }
3458 }
3459
3460 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
3461 0, 1, 0,
3462 doc: /* Return the width in pixels of the X display TERMINAL.
3463 The optional argument TERMINAL specifies which display to ask about.
3464 TERMINAL should be a terminal object, a frame or a display name (a string).
3465 If omitted or nil, that stands for the selected frame's display.
3466
3467 On \"multi-monitor\" setups this refers to the pixel width for all
3468 physical monitors associated with TERMINAL. To get information for
3469 each physical monitor, use `display-monitor-attributes-list'. */)
3470 (Lisp_Object terminal)
3471 {
3472 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3473
3474 return make_number (x_display_pixel_width (dpyinfo));
3475 }
3476
3477 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
3478 Sx_display_pixel_height, 0, 1, 0,
3479 doc: /* Return the height in pixels of the X display TERMINAL.
3480 The optional argument TERMINAL specifies which display to ask about.
3481 TERMINAL should be a terminal object, a frame or a display name (a string).
3482 If omitted or nil, that stands for the selected frame's display.
3483
3484 On \"multi-monitor\" setups this refers to the pixel height for all
3485 physical monitors associated with TERMINAL. To get information for
3486 each physical monitor, use `display-monitor-attributes-list'. */)
3487 (Lisp_Object terminal)
3488 {
3489 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3490
3491 return make_number (x_display_pixel_height (dpyinfo));
3492 }
3493
3494 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
3495 0, 1, 0,
3496 doc: /* Return the number of bitplanes of the X display TERMINAL.
3497 The optional argument TERMINAL specifies which display to ask about.
3498 TERMINAL should be a terminal object, a frame or a display name (a string).
3499 If omitted or nil, that stands for the selected frame's display. */)
3500 (Lisp_Object terminal)
3501 {
3502 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3503
3504 return make_number (dpyinfo->n_planes);
3505 }
3506
3507 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
3508 0, 1, 0,
3509 doc: /* Return the number of color cells of the X display TERMINAL.
3510 The optional argument TERMINAL specifies which display to ask about.
3511 TERMINAL should be a terminal object, a frame or a display name (a string).
3512 If omitted or nil, that stands for the selected frame's display. */)
3513 (Lisp_Object terminal)
3514 {
3515 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3516
3517 int nr_planes = DisplayPlanes (dpyinfo->display,
3518 XScreenNumberOfScreen (dpyinfo->screen));
3519
3520 /* Truncate nr_planes to 24 to avoid integer overflow.
3521 Some displays says 32, but only 24 bits are actually significant.
3522 There are only very few and rare video cards that have more than
3523 24 significant bits. Also 24 bits is more than 16 million colors,
3524 it "should be enough for everyone". */
3525 if (nr_planes > 24) nr_planes = 24;
3526
3527 return make_number (1 << nr_planes);
3528 }
3529
3530 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
3531 Sx_server_max_request_size,
3532 0, 1, 0,
3533 doc: /* Return the maximum request size of the X server of display TERMINAL.
3534 The optional argument TERMINAL specifies which display to ask about.
3535 TERMINAL should be a terminal object, a frame or a display name (a string).
3536 If omitted or nil, that stands for the selected frame's display. */)
3537 (Lisp_Object terminal)
3538 {
3539 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3540
3541 return make_number (MAXREQUEST (dpyinfo->display));
3542 }
3543
3544 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
3545 doc: /* Return the "vendor ID" string of the X server of display TERMINAL.
3546 \(Labeling every distributor as a "vendor" embodies the false assumption
3547 that operating systems cannot be developed and distributed noncommercially.)
3548 The optional argument TERMINAL specifies which display to ask about.
3549 TERMINAL should be a terminal object, a frame or a display name (a string).
3550 If omitted or nil, that stands for the selected frame's display. */)
3551 (Lisp_Object terminal)
3552 {
3553 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3554 const char *vendor = ServerVendor (dpyinfo->display);
3555
3556 if (! vendor) vendor = "";
3557 return build_string (vendor);
3558 }
3559
3560 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
3561 doc: /* Return the version numbers of the X server of display TERMINAL.
3562 The value is a list of three integers: the major and minor
3563 version numbers of the X Protocol in use, and the distributor-specific release
3564 number. See also the function `x-server-vendor'.
3565
3566 The optional argument TERMINAL specifies which display to ask about.
3567 TERMINAL should be a terminal object, a frame or a display name (a string).
3568 If omitted or nil, that stands for the selected frame's display. */)
3569 (Lisp_Object terminal)
3570 {
3571 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3572 Display *dpy = dpyinfo->display;
3573
3574 return list3i (ProtocolVersion (dpy), ProtocolRevision (dpy),
3575 VendorRelease (dpy));
3576 }
3577
3578 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
3579 doc: /* Return the number of screens on the X server of display TERMINAL.
3580 The optional argument TERMINAL specifies which display to ask about.
3581 TERMINAL should be a terminal object, a frame or a display name (a string).
3582 If omitted or nil, that stands for the selected frame's display. */)
3583 (Lisp_Object terminal)
3584 {
3585 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3586
3587 return make_number (ScreenCount (dpyinfo->display));
3588 }
3589
3590 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
3591 doc: /* Return the height in millimeters of the X display TERMINAL.
3592 The optional argument TERMINAL specifies which display to ask about.
3593 TERMINAL should be a terminal object, a frame or a display name (a string).
3594 If omitted or nil, that stands for the selected frame's display.
3595
3596 On \"multi-monitor\" setups this refers to the height in millimeters for
3597 all physical monitors associated with TERMINAL. To get information
3598 for each physical monitor, use `display-monitor-attributes-list'. */)
3599 (Lisp_Object terminal)
3600 {
3601 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3602
3603 return make_number (HeightMMOfScreen (dpyinfo->screen));
3604 }
3605
3606 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
3607 doc: /* Return the width in millimeters of the X display TERMINAL.
3608 The optional argument TERMINAL specifies which display to ask about.
3609 TERMINAL should be a terminal object, a frame or a display name (a string).
3610 If omitted or nil, that stands for the selected frame's display.
3611
3612 On \"multi-monitor\" setups this refers to the width in millimeters for
3613 all physical monitors associated with TERMINAL. To get information
3614 for each physical monitor, use `display-monitor-attributes-list'. */)
3615 (Lisp_Object terminal)
3616 {
3617 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3618
3619 return make_number (WidthMMOfScreen (dpyinfo->screen));
3620 }
3621
3622 DEFUN ("x-display-backing-store", Fx_display_backing_store,
3623 Sx_display_backing_store, 0, 1, 0,
3624 doc: /* Return an indication of whether X display TERMINAL does backing store.
3625 The value may be `always', `when-mapped', or `not-useful'.
3626 The optional argument TERMINAL specifies which display to ask about.
3627 TERMINAL should be a terminal object, a frame or a display name (a string).
3628 If omitted or nil, that stands for the selected frame's display. */)
3629 (Lisp_Object terminal)
3630 {
3631 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3632 Lisp_Object result;
3633
3634 switch (DoesBackingStore (dpyinfo->screen))
3635 {
3636 case Always:
3637 result = intern ("always");
3638 break;
3639
3640 case WhenMapped:
3641 result = intern ("when-mapped");
3642 break;
3643
3644 case NotUseful:
3645 result = intern ("not-useful");
3646 break;
3647
3648 default:
3649 error ("Strange value for BackingStore parameter of screen");
3650 }
3651
3652 return result;
3653 }
3654
3655 DEFUN ("x-display-visual-class", Fx_display_visual_class,
3656 Sx_display_visual_class, 0, 1, 0,
3657 doc: /* Return the visual class of the X display TERMINAL.
3658 The value is one of the symbols `static-gray', `gray-scale',
3659 `static-color', `pseudo-color', `true-color', or `direct-color'.
3660
3661 The optional argument TERMINAL specifies which display to ask about.
3662 TERMINAL should a terminal object, a frame or a display name (a string).
3663 If omitted or nil, that stands for the selected frame's display. */)
3664 (Lisp_Object terminal)
3665 {
3666 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3667 Lisp_Object result;
3668
3669 switch (dpyinfo->visual->class)
3670 {
3671 case StaticGray:
3672 result = intern ("static-gray");
3673 break;
3674 case GrayScale:
3675 result = intern ("gray-scale");
3676 break;
3677 case StaticColor:
3678 result = intern ("static-color");
3679 break;
3680 case PseudoColor:
3681 result = intern ("pseudo-color");
3682 break;
3683 case TrueColor:
3684 result = intern ("true-color");
3685 break;
3686 case DirectColor:
3687 result = intern ("direct-color");
3688 break;
3689 default:
3690 error ("Display has an unknown visual class");
3691 }
3692
3693 return result;
3694 }
3695
3696 DEFUN ("x-display-save-under", Fx_display_save_under,
3697 Sx_display_save_under, 0, 1, 0,
3698 doc: /* Return t if the X display TERMINAL supports the save-under feature.
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. */)
3702 (Lisp_Object terminal)
3703 {
3704 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3705
3706 if (DoesSaveUnders (dpyinfo->screen) == True)
3707 return Qt;
3708 else
3709 return Qnil;
3710 }
3711
3712 /* Store the geometry of the workarea on display DPYINFO into *RECT.
3713 Return false if and only if the workarea information cannot be
3714 obtained via the _NET_WORKAREA root window property. */
3715
3716 #if ! GTK_CHECK_VERSION (3, 4, 0)
3717 static bool
3718 x_get_net_workarea (struct x_display_info *dpyinfo, XRectangle *rect)
3719 {
3720 Display *dpy = dpyinfo->display;
3721 long offset, max_len;
3722 Atom target_type, actual_type;
3723 unsigned long actual_size, bytes_remaining;
3724 int rc, actual_format;
3725 unsigned char *tmp_data = NULL;
3726 bool result = false;
3727
3728 x_catch_errors (dpy);
3729 offset = 0;
3730 max_len = 1;
3731 target_type = XA_CARDINAL;
3732 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3733 dpyinfo->Xatom_net_current_desktop,
3734 offset, max_len, False, target_type,
3735 &actual_type, &actual_format, &actual_size,
3736 &bytes_remaining, &tmp_data);
3737 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3738 && actual_format == 32 && actual_size == max_len)
3739 {
3740 long current_desktop = ((long *) tmp_data)[0];
3741
3742 XFree (tmp_data);
3743 tmp_data = NULL;
3744
3745 offset = 4 * current_desktop;
3746 max_len = 4;
3747 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3748 dpyinfo->Xatom_net_workarea,
3749 offset, max_len, False, target_type,
3750 &actual_type, &actual_format, &actual_size,
3751 &bytes_remaining, &tmp_data);
3752 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3753 && actual_format == 32 && actual_size == max_len)
3754 {
3755 long *values = (long *) tmp_data;
3756
3757 rect->x = values[0];
3758 rect->y = values[1];
3759 rect->width = values[2];
3760 rect->height = values[3];
3761
3762 XFree (tmp_data);
3763 tmp_data = NULL;
3764
3765 result = true;
3766 }
3767 }
3768 if (tmp_data)
3769 XFree (tmp_data);
3770 x_uncatch_errors ();
3771
3772 return result;
3773 }
3774 #endif
3775
3776 #ifndef USE_GTK
3777
3778 /* Return monitor number where F is "most" or closest to. */
3779 static int
3780 x_get_monitor_for_frame (struct frame *f,
3781 struct MonitorInfo *monitors,
3782 int n_monitors)
3783 {
3784 XRectangle frect;
3785 int area = 0, dist = -1;
3786 int best_area = -1, best_dist = -1;
3787 int i;
3788
3789 if (n_monitors == 1) return 0;
3790 frect.x = f->left_pos;
3791 frect.y = f->top_pos;
3792 frect.width = FRAME_PIXEL_WIDTH (f);
3793 frect.height = FRAME_PIXEL_HEIGHT (f);
3794
3795 for (i = 0; i < n_monitors; ++i)
3796 {
3797 struct MonitorInfo *mi = &monitors[i];
3798 XRectangle res;
3799 int a = 0;
3800
3801 if (mi->geom.width == 0) continue;
3802
3803 if (x_intersect_rectangles (&mi->geom, &frect, &res))
3804 {
3805 a = res.width * res.height;
3806 if (a > area)
3807 {
3808 area = a;
3809 best_area = i;
3810 }
3811 }
3812
3813 if (a == 0 && area == 0)
3814 {
3815 int dx, dy, d;
3816 if (frect.x + frect.width < mi->geom.x)
3817 dx = mi->geom.x - frect.x + frect.width;
3818 else if (frect.x > mi->geom.x + mi->geom.width)
3819 dx = frect.x - mi->geom.x + mi->geom.width;
3820 else
3821 dx = 0;
3822 if (frect.y + frect.height < mi->geom.y)
3823 dy = mi->geom.y - frect.y + frect.height;
3824 else if (frect.y > mi->geom.y + mi->geom.height)
3825 dy = frect.y - mi->geom.y + mi->geom.height;
3826 else
3827 dy = 0;
3828
3829 d = dx*dx + dy*dy;
3830 if (dist == -1 || dist > d)
3831 {
3832 dist = d;
3833 best_dist = i;
3834 }
3835 }
3836 }
3837
3838 return best_area != -1 ? best_area : (best_dist != -1 ? best_dist : 0);
3839 }
3840
3841 static Lisp_Object
3842 x_make_monitor_attribute_list (struct MonitorInfo *monitors,
3843 int n_monitors,
3844 int primary_monitor,
3845 struct x_display_info *dpyinfo,
3846 const char *source)
3847 {
3848 Lisp_Object monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
3849 Lisp_Object frame, rest;
3850
3851 FOR_EACH_FRAME (rest, frame)
3852 {
3853 struct frame *f = XFRAME (frame);
3854
3855 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo
3856 && !EQ (frame, tip_frame))
3857 {
3858 int i = x_get_monitor_for_frame (f, monitors, n_monitors);
3859 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
3860 }
3861 }
3862
3863 return make_monitor_attribute_list (monitors, n_monitors, primary_monitor,
3864 monitor_frames, source);
3865 }
3866
3867 static Lisp_Object
3868 x_get_monitor_attributes_fallback (struct x_display_info *dpyinfo)
3869 {
3870 struct MonitorInfo monitor;
3871 XRectangle workarea_r;
3872
3873 /* Fallback: treat (possibly) multiple physical monitors as if they
3874 formed a single monitor as a whole. This should provide a
3875 consistent result at least on single monitor environments. */
3876 monitor.geom.x = monitor.geom.y = 0;
3877 monitor.geom.width = x_display_pixel_width (dpyinfo);
3878 monitor.geom.height = x_display_pixel_height (dpyinfo);
3879 monitor.mm_width = WidthMMOfScreen (dpyinfo->screen);
3880 monitor.mm_height = HeightMMOfScreen (dpyinfo->screen);
3881 monitor.name = xstrdup ("combined screen");
3882
3883 if (x_get_net_workarea (dpyinfo, &workarea_r))
3884 monitor.work = workarea_r;
3885 else
3886 monitor.work = monitor.geom;
3887 return x_make_monitor_attribute_list (&monitor, 1, 0, dpyinfo, "fallback");
3888 }
3889
3890
3891 #ifdef HAVE_XINERAMA
3892 static Lisp_Object
3893 x_get_monitor_attributes_xinerama (struct x_display_info *dpyinfo)
3894 {
3895 int n_monitors, i;
3896 Lisp_Object attributes_list = Qnil;
3897 Display *dpy = dpyinfo->display;
3898 XineramaScreenInfo *info = XineramaQueryScreens (dpy, &n_monitors);
3899 struct MonitorInfo *monitors;
3900 double mm_width_per_pixel, mm_height_per_pixel;
3901
3902 if (! info || n_monitors == 0)
3903 {
3904 if (info)
3905 XFree (info);
3906 return attributes_list;
3907 }
3908
3909 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
3910 / x_display_pixel_width (dpyinfo));
3911 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
3912 / x_display_pixel_height (dpyinfo));
3913 monitors = xzalloc (n_monitors * sizeof *monitors);
3914 for (i = 0; i < n_monitors; ++i)
3915 {
3916 struct MonitorInfo *mi = &monitors[i];
3917 XRectangle workarea_r;
3918
3919 mi->geom.x = info[i].x_org;
3920 mi->geom.y = info[i].y_org;
3921 mi->geom.width = info[i].width;
3922 mi->geom.height = info[i].height;
3923 mi->mm_width = mi->geom.width * mm_width_per_pixel + 0.5;
3924 mi->mm_height = mi->geom.height * mm_height_per_pixel + 0.5;
3925 mi->name = 0;
3926
3927 /* Xinerama usually have primary monitor first, just use that. */
3928 if (i == 0 && x_get_net_workarea (dpyinfo, &workarea_r))
3929 {
3930 mi->work = workarea_r;
3931 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
3932 mi->work = mi->geom;
3933 }
3934 else
3935 mi->work = mi->geom;
3936 }
3937 XFree (info);
3938
3939 attributes_list = x_make_monitor_attribute_list (monitors,
3940 n_monitors,
3941 0,
3942 dpyinfo,
3943 "Xinerama");
3944 free_monitors (monitors, n_monitors);
3945 return attributes_list;
3946 }
3947 #endif /* HAVE_XINERAMA */
3948
3949
3950 #ifdef HAVE_XRANDR
3951 static Lisp_Object
3952 x_get_monitor_attributes_xrandr (struct x_display_info *dpyinfo)
3953 {
3954 Lisp_Object attributes_list = Qnil;
3955 XRRScreenResources *resources;
3956 Display *dpy = dpyinfo->display;
3957 int i, n_monitors, primary = -1;
3958 RROutput pxid = None;
3959 struct MonitorInfo *monitors;
3960
3961 #ifdef HAVE_XRRGETSCREENRESOURCESCURRENT
3962 resources = XRRGetScreenResourcesCurrent (dpy, dpyinfo->root_window);
3963 #else
3964 resources = XRRGetScreenResources (dpy, dpyinfo->root_window);
3965 #endif
3966 if (! resources || resources->noutput == 0)
3967 {
3968 if (resources)
3969 XRRFreeScreenResources (resources);
3970 return Qnil;
3971 }
3972 n_monitors = resources->noutput;
3973 monitors = xzalloc (n_monitors * sizeof *monitors);
3974
3975 #ifdef HAVE_XRRGETOUTPUTPRIMARY
3976 pxid = XRRGetOutputPrimary (dpy, dpyinfo->root_window);
3977 #endif
3978
3979 for (i = 0; i < n_monitors; ++i)
3980 {
3981 XRROutputInfo *info = XRRGetOutputInfo (dpy, resources,
3982 resources->outputs[i]);
3983 Connection conn = info ? info->connection : RR_Disconnected;
3984 RRCrtc id = info ? info->crtc : None;
3985
3986 if (strcmp (info->name, "default") == 0)
3987 {
3988 /* Non XRandr 1.2 driver, does not give useful data. */
3989 XRRFreeOutputInfo (info);
3990 XRRFreeScreenResources (resources);
3991 free_monitors (monitors, n_monitors);
3992 return Qnil;
3993 }
3994
3995 if (conn != RR_Disconnected && id != None)
3996 {
3997 XRRCrtcInfo *crtc = XRRGetCrtcInfo (dpy, resources, id);
3998 struct MonitorInfo *mi = &monitors[i];
3999 XRectangle workarea_r;
4000
4001 if (! crtc)
4002 {
4003 XRRFreeOutputInfo (info);
4004 continue;
4005 }
4006
4007 mi->geom.x = crtc->x;
4008 mi->geom.y = crtc->y;
4009 mi->geom.width = crtc->width;
4010 mi->geom.height = crtc->height;
4011 mi->mm_width = info->mm_width;
4012 mi->mm_height = info->mm_height;
4013 mi->name = xstrdup (info->name);
4014
4015 if (pxid != None && pxid == resources->outputs[i])
4016 primary = i;
4017 else if (primary == -1 && strcmp (info->name, "LVDS") == 0)
4018 primary = i;
4019
4020 if (i == primary && x_get_net_workarea (dpyinfo, &workarea_r))
4021 {
4022 mi->work= workarea_r;
4023 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
4024 mi->work = mi->geom;
4025 }
4026 else
4027 mi->work = mi->geom;
4028
4029 XRRFreeCrtcInfo (crtc);
4030 }
4031 XRRFreeOutputInfo (info);
4032 }
4033 XRRFreeScreenResources (resources);
4034
4035 attributes_list = x_make_monitor_attribute_list (monitors,
4036 n_monitors,
4037 primary,
4038 dpyinfo,
4039 "XRandr");
4040 free_monitors (monitors, n_monitors);
4041 return attributes_list;
4042 }
4043 #endif /* HAVE_XRANDR */
4044
4045 static Lisp_Object
4046 x_get_monitor_attributes (struct x_display_info *dpyinfo)
4047 {
4048 Lisp_Object attributes_list = Qnil;
4049 Display *dpy = dpyinfo->display;
4050
4051 (void) dpy; /* Suppress unused variable warning. */
4052
4053 #ifdef HAVE_XRANDR
4054 int xrr_event_base, xrr_error_base;
4055 bool xrr_ok = false;
4056 xrr_ok = XRRQueryExtension (dpy, &xrr_event_base, &xrr_error_base);
4057 if (xrr_ok)
4058 {
4059 int xrr_major, xrr_minor;
4060 XRRQueryVersion (dpy, &xrr_major, &xrr_minor);
4061 xrr_ok = (xrr_major == 1 && xrr_minor >= 2) || xrr_major > 1;
4062 }
4063
4064 if (xrr_ok)
4065 attributes_list = x_get_monitor_attributes_xrandr (dpyinfo);
4066 #endif /* HAVE_XRANDR */
4067
4068 #ifdef HAVE_XINERAMA
4069 if (NILP (attributes_list))
4070 {
4071 int xin_event_base, xin_error_base;
4072 bool xin_ok = false;
4073 xin_ok = XineramaQueryExtension (dpy, &xin_event_base, &xin_error_base);
4074 if (xin_ok && XineramaIsActive (dpy))
4075 attributes_list = x_get_monitor_attributes_xinerama (dpyinfo);
4076 }
4077 #endif /* HAVE_XINERAMA */
4078
4079 if (NILP (attributes_list))
4080 attributes_list = x_get_monitor_attributes_fallback (dpyinfo);
4081
4082 return attributes_list;
4083 }
4084
4085 #endif /* !USE_GTK */
4086
4087 DEFUN ("x-display-monitor-attributes-list", Fx_display_monitor_attributes_list,
4088 Sx_display_monitor_attributes_list,
4089 0, 1, 0,
4090 doc: /* Return a list of physical monitor attributes on the X display TERMINAL.
4091
4092 The optional argument TERMINAL specifies which display to ask about.
4093 TERMINAL should be a terminal object, a frame or a display name (a string).
4094 If omitted or nil, that stands for the selected frame's display.
4095
4096 In addition to the standard attribute keys listed in
4097 `display-monitor-attributes-list', the following keys are contained in
4098 the attributes:
4099
4100 source -- String describing the source from which multi-monitor
4101 information is obtained, one of \"Gdk\", \"XRandr\",
4102 \"Xinerama\", or \"fallback\"
4103
4104 Internal use only, use `display-monitor-attributes-list' instead. */)
4105 (Lisp_Object terminal)
4106 {
4107 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4108 Lisp_Object attributes_list = Qnil;
4109
4110 #ifdef USE_GTK
4111 double mm_width_per_pixel, mm_height_per_pixel;
4112 GdkDisplay *gdpy;
4113 GdkScreen *gscreen;
4114 gint primary_monitor = 0, n_monitors, i;
4115 Lisp_Object monitor_frames, rest, frame;
4116 static const char *source = "Gdk";
4117 struct MonitorInfo *monitors;
4118
4119 block_input ();
4120 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
4121 / x_display_pixel_width (dpyinfo));
4122 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
4123 / x_display_pixel_height (dpyinfo));
4124 gdpy = gdk_x11_lookup_xdisplay (dpyinfo->display);
4125 gscreen = gdk_display_get_default_screen (gdpy);
4126 #if GTK_CHECK_VERSION (2, 20, 0)
4127 primary_monitor = gdk_screen_get_primary_monitor (gscreen);
4128 #endif
4129 n_monitors = gdk_screen_get_n_monitors (gscreen);
4130 monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
4131 monitors = xzalloc (n_monitors * sizeof *monitors);
4132
4133 FOR_EACH_FRAME (rest, frame)
4134 {
4135 struct frame *f = XFRAME (frame);
4136
4137 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo
4138 && !EQ (frame, tip_frame))
4139 {
4140 GdkWindow *gwin = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
4141
4142 i = gdk_screen_get_monitor_at_window (gscreen, gwin);
4143 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
4144 }
4145 }
4146
4147 for (i = 0; i < n_monitors; ++i)
4148 {
4149 gint width_mm = -1, height_mm = -1;
4150 GdkRectangle rec, work;
4151 struct MonitorInfo *mi = &monitors[i];
4152
4153 gdk_screen_get_monitor_geometry (gscreen, i, &rec);
4154
4155 #if GTK_CHECK_VERSION (2, 14, 0)
4156 width_mm = gdk_screen_get_monitor_width_mm (gscreen, i);
4157 height_mm = gdk_screen_get_monitor_height_mm (gscreen, i);
4158 #endif
4159 if (width_mm < 0)
4160 width_mm = rec.width * mm_width_per_pixel + 0.5;
4161 if (height_mm < 0)
4162 height_mm = rec.height * mm_height_per_pixel + 0.5;
4163
4164 #if GTK_CHECK_VERSION (3, 4, 0)
4165 gdk_screen_get_monitor_workarea (gscreen, i, &work);
4166 #else
4167 /* Emulate the behavior of GTK+ 3.4. */
4168 {
4169 XRectangle workarea_r;
4170
4171 if (i == primary_monitor && x_get_net_workarea (dpyinfo, &workarea_r))
4172 {
4173 work.x = workarea_r.x;
4174 work.y = workarea_r.y;
4175 work.width = workarea_r.width;
4176 work.height = workarea_r.height;
4177 if (! gdk_rectangle_intersect (&rec, &work, &work))
4178 work = rec;
4179 }
4180 else
4181 work = rec;
4182 }
4183 #endif
4184
4185
4186 mi->geom.x = rec.x;
4187 mi->geom.y = rec.y;
4188 mi->geom.width = rec.width;
4189 mi->geom.height = rec.height;
4190 mi->work.x = work.x;
4191 mi->work.y = work.y;
4192 mi->work.width = work.width;
4193 mi->work.height = work.height;
4194 mi->mm_width = width_mm;
4195 mi->mm_height = height_mm;
4196
4197 #if GTK_CHECK_VERSION (2, 14, 0)
4198 mi->name = gdk_screen_get_monitor_plug_name (gscreen, i);
4199 #endif
4200 }
4201
4202 attributes_list = make_monitor_attribute_list (monitors,
4203 n_monitors,
4204 primary_monitor,
4205 monitor_frames,
4206 source);
4207 unblock_input ();
4208 #else /* not USE_GTK */
4209
4210 block_input ();
4211 attributes_list = x_get_monitor_attributes (dpyinfo);
4212 unblock_input ();
4213
4214 #endif /* not USE_GTK */
4215
4216 return attributes_list;
4217 }
4218
4219 DEFUN ("x-frame-geometry", Fx_frame_geometry, Sx_frame_geometry, 0, 1, 0,
4220 doc: /* Return geometric attributes of frame FRAME.
4221
4222 FRAME must be a live frame and defaults to the selected one.
4223
4224 The return value is an association list containing the following
4225 elements (all size values are in pixels).
4226
4227 - `frame-outer-size' is a cons of the outer width and height of FRAME.
4228 The outer size include the title bar and the external borders as well
4229 as any menu and/or tool bar of frame.
4230
4231 - `border' is a cons of the horizontal and vertical width of FRAME's
4232 external borders.
4233
4234 - `title-bar-height' is the height of the title bar of FRAME.
4235
4236 - `menu-bar-external' if `t' means the menu bar is external (not
4237 included in the inner edges of FRAME).
4238
4239 - `menu-bar-size' is a cons of the width and height of the menu bar of
4240 FRAME.
4241
4242 - `tool-bar-external' if `t' means the tool bar is external (not
4243 included in the inner edges of FRAME).
4244
4245 - `tool-bar-side' tells tells on which side the tool bar on FRAME is and
4246 can be one of `left', `top', `right' or `bottom'.
4247
4248 - `tool-bar-size' is a cons of the width and height of the tool bar of
4249 FRAME.
4250
4251 - `frame-inner-size' is a cons of the inner width and height of FRAME.
4252 This excludes FRAME's title bar and external border as well as any
4253 external menu and/or tool bar. */)
4254 (Lisp_Object frame)
4255 {
4256 struct frame *f = decode_live_frame (frame);
4257 int inner_width = FRAME_PIXEL_WIDTH (f);
4258 int inner_height = FRAME_PIXEL_HEIGHT (f);
4259 int outer_width, outer_height, border, title;
4260 Lisp_Object fullscreen = Fframe_parameter (frame, Qfullscreen);
4261 int menu_bar_height, menu_bar_width, tool_bar_height, tool_bar_width;
4262
4263 border = FRAME_OUTER_TO_INNER_DIFF_X (f);
4264 title = FRAME_X_OUTPUT (f)->y_pixels_outer_diff - border;
4265
4266 outer_width = FRAME_PIXEL_WIDTH (f) + 2 * border;
4267 outer_height = (FRAME_PIXEL_HEIGHT (f)
4268 + FRAME_OUTER_TO_INNER_DIFF_Y (f)
4269 + FRAME_OUTER_TO_INNER_DIFF_X (f));
4270
4271 #if defined (USE_GTK)
4272 {
4273 bool tool_bar_left_right = (EQ (FRAME_TOOL_BAR_POSITION (f), Qleft)
4274 || EQ (FRAME_TOOL_BAR_POSITION (f), Qright));
4275
4276 tool_bar_width = (tool_bar_left_right
4277 ? FRAME_TOOLBAR_WIDTH (f)
4278 : FRAME_PIXEL_WIDTH (f));
4279 tool_bar_height = (tool_bar_left_right
4280 ? FRAME_PIXEL_HEIGHT (f)
4281 : FRAME_TOOLBAR_HEIGHT (f));
4282 if (tool_bar_left_right)
4283 /* For some reason FRAME_OUTER_TO_INNER_DIFF_X does not count the
4284 width of a tool bar. */
4285 outer_width += FRAME_TOOLBAR_WIDTH (f);
4286 }
4287 #else
4288 tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f);
4289 tool_bar_width = ((tool_bar_height > 0)
4290 ? outer_width - 2 * FRAME_INTERNAL_BORDER_WIDTH (f)
4291 : 0);
4292 #endif
4293
4294 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
4295 menu_bar_height = FRAME_MENUBAR_HEIGHT (f);
4296 #else
4297 menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
4298 #endif
4299
4300 menu_bar_width = ((menu_bar_height > 0)
4301 ? outer_width - 2 * border
4302 : 0);
4303
4304 if (!FRAME_EXTERNAL_MENU_BAR (f))
4305 inner_height -= menu_bar_height;
4306 if (!FRAME_EXTERNAL_TOOL_BAR (f))
4307 inner_height -= tool_bar_height;
4308
4309 return
4310 listn (CONSTYPE_PURE, 10,
4311 Fcons (Qframe_position,
4312 Fcons (make_number (f->left_pos), make_number (f->top_pos))),
4313 Fcons (Qframe_outer_size,
4314 Fcons (make_number (outer_width), make_number (outer_height))),
4315 Fcons (Qexternal_border_size,
4316 ((EQ (fullscreen, Qfullboth) || EQ (fullscreen, Qfullscreen))
4317 ? Fcons (make_number (0), make_number (0))
4318 : Fcons (make_number (border), make_number (border)))),
4319 Fcons (Qtitle_height,
4320 ((EQ (fullscreen, Qfullboth) || EQ (fullscreen, Qfullscreen))
4321 ? make_number (0)
4322 : make_number (title))),
4323 Fcons (Qmenu_bar_external, FRAME_EXTERNAL_MENU_BAR (f) ? Qt : Qnil),
4324 Fcons (Qmenu_bar_size,
4325 Fcons (make_number (menu_bar_width),
4326 make_number (menu_bar_height))),
4327 Fcons (Qtool_bar_external, FRAME_EXTERNAL_TOOL_BAR (f) ? Qt : Qnil),
4328 Fcons (Qtool_bar_position, FRAME_TOOL_BAR_POSITION (f)),
4329 Fcons (Qtool_bar_size,
4330 Fcons (make_number (tool_bar_width),
4331 make_number (tool_bar_height))),
4332 Fcons (Qframe_inner_size,
4333 Fcons (make_number (inner_width),
4334 make_number (inner_height))));
4335 }
4336
4337 /************************************************************************
4338 X Displays
4339 ************************************************************************/
4340
4341 \f
4342 /* Mapping visual names to visuals. */
4343
4344 static struct visual_class
4345 {
4346 const char *name;
4347 int class;
4348 }
4349 visual_classes[] =
4350 {
4351 {"StaticGray", StaticGray},
4352 {"GrayScale", GrayScale},
4353 {"StaticColor", StaticColor},
4354 {"PseudoColor", PseudoColor},
4355 {"TrueColor", TrueColor},
4356 {"DirectColor", DirectColor},
4357 {NULL, 0}
4358 };
4359
4360
4361 #ifndef HAVE_XSCREENNUMBEROFSCREEN
4362
4363 /* Value is the screen number of screen SCR. This is a substitute for
4364 the X function with the same name when that doesn't exist. */
4365
4366 int
4367 XScreenNumberOfScreen (scr)
4368 register Screen *scr;
4369 {
4370 Display *dpy = scr->display;
4371 int i;
4372
4373 for (i = 0; i < dpy->nscreens; ++i)
4374 if (scr == dpy->screens + i)
4375 break;
4376
4377 return i;
4378 }
4379
4380 #endif /* not HAVE_XSCREENNUMBEROFSCREEN */
4381
4382
4383 /* Select the visual that should be used on display DPYINFO. Set
4384 members of DPYINFO appropriately. Called from x_term_init. */
4385
4386 void
4387 select_visual (struct x_display_info *dpyinfo)
4388 {
4389 Display *dpy = dpyinfo->display;
4390 Screen *screen = dpyinfo->screen;
4391
4392 /* See if a visual is specified. */
4393 AUTO_STRING (visualClass, "visualClass");
4394 AUTO_STRING (VisualClass, "VisualClass");
4395 Lisp_Object value = display_x_get_resource (dpyinfo, visualClass,
4396 VisualClass, Qnil, Qnil);
4397
4398 if (STRINGP (value))
4399 {
4400 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
4401 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
4402 depth, a decimal number. NAME is compared with case ignored. */
4403 char *s = alloca (SBYTES (value) + 1);
4404 char *dash;
4405 int i, class = -1;
4406 XVisualInfo vinfo;
4407
4408 lispstpcpy (s, value);
4409 dash = strchr (s, '-');
4410 if (dash)
4411 {
4412 dpyinfo->n_planes = atoi (dash + 1);
4413 *dash = '\0';
4414 }
4415 else
4416 /* We won't find a matching visual with depth 0, so that
4417 an error will be printed below. */
4418 dpyinfo->n_planes = 0;
4419
4420 /* Determine the visual class. */
4421 for (i = 0; visual_classes[i].name; ++i)
4422 if (xstrcasecmp (s, visual_classes[i].name) == 0)
4423 {
4424 class = visual_classes[i].class;
4425 break;
4426 }
4427
4428 /* Look up a matching visual for the specified class. */
4429 if (class == -1
4430 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
4431 dpyinfo->n_planes, class, &vinfo))
4432 fatal ("Invalid visual specification `%s'", SDATA (value));
4433
4434 dpyinfo->visual = vinfo.visual;
4435 }
4436 else
4437 {
4438 int n_visuals;
4439 XVisualInfo *vinfo, vinfo_template;
4440
4441 dpyinfo->visual = DefaultVisualOfScreen (screen);
4442
4443 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
4444 vinfo_template.screen = XScreenNumberOfScreen (screen);
4445 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
4446 &vinfo_template, &n_visuals);
4447 if (n_visuals <= 0)
4448 fatal ("Can't get proper X visual info");
4449
4450 dpyinfo->n_planes = vinfo->depth;
4451 XFree (vinfo);
4452 }
4453 }
4454
4455
4456 /* Return the X display structure for the display named NAME.
4457 Open a new connection if necessary. */
4458
4459 static struct x_display_info *
4460 x_display_info_for_name (Lisp_Object name)
4461 {
4462 struct x_display_info *dpyinfo;
4463
4464 CHECK_STRING (name);
4465
4466 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
4467 if (!NILP (Fstring_equal (XCAR (dpyinfo->name_list_element), name)))
4468 return dpyinfo;
4469
4470 /* Use this general default value to start with. */
4471 Vx_resource_name = Vinvocation_name;
4472
4473 validate_x_resource_name ();
4474
4475 dpyinfo = x_term_init (name, 0, SSDATA (Vx_resource_name));
4476
4477 if (dpyinfo == 0)
4478 error ("Cannot connect to X server %s", SDATA (name));
4479
4480 XSETFASTINT (Vwindow_system_version, 11);
4481
4482 return dpyinfo;
4483 }
4484
4485
4486 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
4487 1, 3, 0,
4488 doc: /* Open a connection to a display server.
4489 DISPLAY is the name of the display to connect to.
4490 Optional second arg XRM-STRING is a string of resources in xrdb format.
4491 If the optional third arg MUST-SUCCEED is non-nil,
4492 terminate Emacs if we can't open the connection.
4493 \(In the Nextstep version, the last two arguments are currently ignored.) */)
4494 (Lisp_Object display, Lisp_Object xrm_string, Lisp_Object must_succeed)
4495 {
4496 char *xrm_option;
4497 struct x_display_info *dpyinfo;
4498
4499 CHECK_STRING (display);
4500 if (! NILP (xrm_string))
4501 CHECK_STRING (xrm_string);
4502
4503 xrm_option = NILP (xrm_string) ? 0 : SSDATA (xrm_string);
4504
4505 validate_x_resource_name ();
4506
4507 /* This is what opens the connection and sets x_current_display.
4508 This also initializes many symbols, such as those used for input. */
4509 dpyinfo = x_term_init (display, xrm_option,
4510 SSDATA (Vx_resource_name));
4511
4512 if (dpyinfo == 0)
4513 {
4514 if (!NILP (must_succeed))
4515 fatal ("Cannot connect to X server %s.\n\
4516 Check the DISPLAY environment variable or use `-d'.\n\
4517 Also use the `xauth' program to verify that you have the proper\n\
4518 authorization information needed to connect the X server.\n\
4519 An insecure way to solve the problem may be to use `xhost'.\n",
4520 SDATA (display));
4521 else
4522 error ("Cannot connect to X server %s", SDATA (display));
4523 }
4524
4525 XSETFASTINT (Vwindow_system_version, 11);
4526 return Qnil;
4527 }
4528
4529 DEFUN ("x-close-connection", Fx_close_connection,
4530 Sx_close_connection, 1, 1, 0,
4531 doc: /* Close the connection to TERMINAL's X server.
4532 For TERMINAL, specify a terminal object, a frame or a display name (a
4533 string). If TERMINAL is nil, that stands for the selected frame's
4534 terminal. */)
4535 (Lisp_Object terminal)
4536 {
4537 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4538
4539 if (dpyinfo->reference_count > 0)
4540 error ("Display still has frames on it");
4541
4542 x_delete_terminal (dpyinfo->terminal);
4543
4544 return Qnil;
4545 }
4546
4547 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
4548 doc: /* Return the list of display names that Emacs has connections to. */)
4549 (void)
4550 {
4551 Lisp_Object result = Qnil;
4552 struct x_display_info *xdi;
4553
4554 for (xdi = x_display_list; xdi; xdi = xdi->next)
4555 result = Fcons (XCAR (xdi->name_list_element), result);
4556
4557 return result;
4558 }
4559
4560 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
4561 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
4562 This function only has an effect on X Windows. With MS Windows, it is
4563 defined but does nothing.
4564
4565 If ON is nil, allow buffering of requests.
4566 Turning on synchronization prohibits the Xlib routines from buffering
4567 requests and seriously degrades performance, but makes debugging much
4568 easier.
4569 The optional second argument TERMINAL specifies which display to act on.
4570 TERMINAL should be a terminal object, a frame or a display name (a string).
4571 If TERMINAL is omitted or nil, that stands for the selected frame's display. */)
4572 (Lisp_Object on, Lisp_Object terminal)
4573 {
4574 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4575
4576 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
4577
4578 return Qnil;
4579 }
4580
4581 /* Wait for responses to all X commands issued so far for frame F. */
4582
4583 void
4584 x_sync (struct frame *f)
4585 {
4586 block_input ();
4587 XSync (FRAME_X_DISPLAY (f), False);
4588 unblock_input ();
4589 }
4590
4591 \f
4592 /***********************************************************************
4593 Window properties
4594 ***********************************************************************/
4595
4596 DEFUN ("x-change-window-property", Fx_change_window_property,
4597 Sx_change_window_property, 2, 6, 0,
4598 doc: /* Change window property PROP to VALUE on the X window of FRAME.
4599 PROP must be a string. VALUE may be a string or a list of conses,
4600 numbers and/or strings. If an element in the list is a string, it is
4601 converted to an atom and the value of the atom is used. If an element
4602 is a cons, it is converted to a 32 bit number where the car is the 16
4603 top bits and the cdr is the lower 16 bits.
4604
4605 FRAME nil or omitted means use the selected frame.
4606 If TYPE is given and non-nil, it is the name of the type of VALUE.
4607 If TYPE is not given or nil, the type is STRING.
4608 FORMAT gives the size in bits of each element if VALUE is a list.
4609 It must be one of 8, 16 or 32.
4610 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
4611 If OUTER-P is non-nil, the property is changed for the outer X window of
4612 FRAME. Default is to change on the edit X window. */)
4613 (Lisp_Object prop, Lisp_Object value, Lisp_Object frame,
4614 Lisp_Object type, Lisp_Object format, Lisp_Object outer_p)
4615 {
4616 struct frame *f = decode_window_system_frame (frame);
4617 Atom prop_atom;
4618 Atom target_type = XA_STRING;
4619 int element_format = 8;
4620 unsigned char *data;
4621 int nelements;
4622 Window w;
4623
4624 CHECK_STRING (prop);
4625
4626 if (! NILP (format))
4627 {
4628 CHECK_NUMBER (format);
4629
4630 if (XINT (format) != 8 && XINT (format) != 16
4631 && XINT (format) != 32)
4632 error ("FORMAT must be one of 8, 16 or 32");
4633 element_format = XINT (format);
4634 }
4635
4636 if (CONSP (value))
4637 {
4638 ptrdiff_t elsize;
4639
4640 nelements = x_check_property_data (value);
4641 if (nelements == -1)
4642 error ("Bad data in VALUE, must be number, string or cons");
4643
4644 /* The man page for XChangeProperty:
4645 "If the specified format is 32, the property data must be a
4646 long array."
4647 This applies even if long is more than 32 bits. The X library
4648 converts to 32 bits before sending to the X server. */
4649 elsize = element_format == 32 ? sizeof (long) : element_format >> 3;
4650 data = xnmalloc (nelements, elsize);
4651
4652 x_fill_property_data (FRAME_X_DISPLAY (f), value, data, element_format);
4653 }
4654 else
4655 {
4656 CHECK_STRING (value);
4657 data = SDATA (value);
4658 if (INT_MAX < SBYTES (value))
4659 error ("VALUE too long");
4660 nelements = SBYTES (value);
4661 }
4662
4663 block_input ();
4664 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4665 if (! NILP (type))
4666 {
4667 CHECK_STRING (type);
4668 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4669 }
4670
4671 if (! NILP (outer_p)) w = FRAME_OUTER_WINDOW (f);
4672 else w = FRAME_X_WINDOW (f);
4673
4674 XChangeProperty (FRAME_X_DISPLAY (f), w,
4675 prop_atom, target_type, element_format, PropModeReplace,
4676 data, nelements);
4677
4678 if (CONSP (value)) xfree (data);
4679
4680 /* Make sure the property is set when we return. */
4681 XFlush (FRAME_X_DISPLAY (f));
4682 unblock_input ();
4683
4684 return value;
4685 }
4686
4687
4688 DEFUN ("x-delete-window-property", Fx_delete_window_property,
4689 Sx_delete_window_property, 1, 2, 0,
4690 doc: /* Remove window property PROP from X window of FRAME.
4691 FRAME nil or omitted means use the selected frame. Value is PROP. */)
4692 (Lisp_Object prop, Lisp_Object frame)
4693 {
4694 struct frame *f = decode_window_system_frame (frame);
4695 Atom prop_atom;
4696
4697 CHECK_STRING (prop);
4698 block_input ();
4699 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4700 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
4701
4702 /* Make sure the property is removed when we return. */
4703 XFlush (FRAME_X_DISPLAY (f));
4704 unblock_input ();
4705
4706 return prop;
4707 }
4708
4709
4710 static Lisp_Object
4711 x_window_property_intern (struct frame *f,
4712 Window target_window,
4713 Atom prop_atom,
4714 Atom target_type,
4715 Lisp_Object delete_p,
4716 Lisp_Object vector_ret_p,
4717 bool *found)
4718 {
4719 unsigned char *tmp_data = NULL;
4720 Lisp_Object prop_value = Qnil;
4721 Atom actual_type;
4722 int actual_format;
4723 unsigned long actual_size, bytes_remaining;
4724 int rc;
4725 struct gcpro gcpro1;
4726
4727 GCPRO1 (prop_value);
4728
4729 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4730 prop_atom, 0, 0, False, target_type,
4731 &actual_type, &actual_format, &actual_size,
4732 &bytes_remaining, &tmp_data);
4733
4734 *found = actual_format != 0;
4735
4736 if (rc == Success && *found)
4737 {
4738 XFree (tmp_data);
4739 tmp_data = NULL;
4740
4741 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4742 prop_atom, 0, bytes_remaining,
4743 ! NILP (delete_p), target_type,
4744 &actual_type, &actual_format,
4745 &actual_size, &bytes_remaining,
4746 &tmp_data);
4747 if (rc == Success && tmp_data)
4748 {
4749 /* The man page for XGetWindowProperty says:
4750 "If the returned format is 32, the returned data is represented
4751 as a long array and should be cast to that type to obtain the
4752 elements."
4753 This applies even if long is more than 32 bits, the X library
4754 converts from 32 bit elements received from the X server to long
4755 and passes the long array to us. Thus, for that case memcpy can not
4756 be used. We convert to a 32 bit type here, because so much code
4757 assume on that.
4758
4759 The bytes and offsets passed to XGetWindowProperty refers to the
4760 property and those are indeed in 32 bit quantities if format is
4761 32. */
4762
4763 if (BITS_PER_LONG > 32 && actual_format == 32)
4764 {
4765 unsigned long i;
4766 int *idata = (int *) tmp_data;
4767 long *ldata = (long *) tmp_data;
4768
4769 for (i = 0; i < actual_size; ++i)
4770 idata[i] = (int) ldata[i];
4771 }
4772
4773 if (NILP (vector_ret_p))
4774 prop_value = make_string ((char *) tmp_data, actual_size);
4775 else
4776 prop_value = x_property_data_to_lisp (f,
4777 tmp_data,
4778 actual_type,
4779 actual_format,
4780 actual_size);
4781 }
4782
4783 if (tmp_data) XFree (tmp_data);
4784 }
4785
4786 UNGCPRO;
4787 return prop_value;
4788 }
4789
4790 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
4791 1, 6, 0,
4792 doc: /* Value is the value of window property PROP on FRAME.
4793 If FRAME is nil or omitted, use the selected frame.
4794
4795 On X Windows, the following optional arguments are also accepted:
4796 If TYPE is nil or omitted, get the property as a string.
4797 Otherwise TYPE is the name of the atom that denotes the type expected.
4798 If SOURCE is non-nil, get the property on that window instead of from
4799 FRAME. The number 0 denotes the root window.
4800 If DELETE-P is non-nil, delete the property after retrieving it.
4801 If VECTOR-RET-P is non-nil, don't return a string but a vector of values.
4802
4803 On MS Windows, this function accepts but ignores those optional arguments.
4804
4805 Value is nil if FRAME hasn't a property with name PROP or if PROP has
4806 no value of TYPE (always string in the MS Windows case). */)
4807 (Lisp_Object prop, Lisp_Object frame, Lisp_Object type,
4808 Lisp_Object source, Lisp_Object delete_p, Lisp_Object vector_ret_p)
4809 {
4810 struct frame *f = decode_window_system_frame (frame);
4811 Atom prop_atom;
4812 Lisp_Object prop_value = Qnil;
4813 Atom target_type = XA_STRING;
4814 Window target_window = FRAME_X_WINDOW (f);
4815 struct gcpro gcpro1;
4816 bool found;
4817
4818 GCPRO1 (prop_value);
4819 CHECK_STRING (prop);
4820
4821 if (! NILP (source))
4822 {
4823 CONS_TO_INTEGER (source, Window, target_window);
4824 if (! target_window)
4825 target_window = FRAME_DISPLAY_INFO (f)->root_window;
4826 }
4827
4828 block_input ();
4829 if (STRINGP (type))
4830 {
4831 if (strcmp ("AnyPropertyType", SSDATA (type)) == 0)
4832 target_type = AnyPropertyType;
4833 else
4834 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4835 }
4836
4837 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4838 prop_value = x_window_property_intern (f,
4839 target_window,
4840 prop_atom,
4841 target_type,
4842 delete_p,
4843 vector_ret_p,
4844 &found);
4845 if (NILP (prop_value)
4846 && ! found
4847 && NILP (source)
4848 && target_window != FRAME_OUTER_WINDOW (f))
4849 {
4850 prop_value = x_window_property_intern (f,
4851 FRAME_OUTER_WINDOW (f),
4852 prop_atom,
4853 target_type,
4854 delete_p,
4855 vector_ret_p,
4856 &found);
4857 }
4858
4859
4860 unblock_input ();
4861 UNGCPRO;
4862 return prop_value;
4863 }
4864
4865 /***********************************************************************
4866 Tool tips
4867 ***********************************************************************/
4868
4869 static Lisp_Object x_create_tip_frame (struct x_display_info *,
4870 Lisp_Object, Lisp_Object);
4871 static void compute_tip_xy (struct frame *, Lisp_Object, Lisp_Object,
4872 Lisp_Object, int, int, int *, int *);
4873
4874 /* The frame of a currently visible tooltip. */
4875
4876 Lisp_Object tip_frame;
4877
4878 /* If non-nil, a timer started that hides the last tooltip when it
4879 fires. */
4880
4881 static Lisp_Object tip_timer;
4882 Window tip_window;
4883
4884 /* If non-nil, a vector of 3 elements containing the last args
4885 with which x-show-tip was called. See there. */
4886
4887 static Lisp_Object last_show_tip_args;
4888
4889
4890 static void
4891 unwind_create_tip_frame (Lisp_Object frame)
4892 {
4893 Lisp_Object deleted;
4894
4895 deleted = unwind_create_frame (frame);
4896 if (EQ (deleted, Qt))
4897 {
4898 tip_window = None;
4899 tip_frame = Qnil;
4900 }
4901 }
4902
4903
4904 /* Create a frame for a tooltip on the display described by DPYINFO.
4905 PARMS is a list of frame parameters. TEXT is the string to
4906 display in the tip frame. Value is the frame.
4907
4908 Note that functions called here, esp. x_default_parameter can
4909 signal errors, for instance when a specified color name is
4910 undefined. We have to make sure that we're in a consistent state
4911 when this happens. */
4912
4913 static Lisp_Object
4914 x_create_tip_frame (struct x_display_info *dpyinfo,
4915 Lisp_Object parms,
4916 Lisp_Object text)
4917 {
4918 struct frame *f;
4919 Lisp_Object frame;
4920 Lisp_Object name;
4921 int width, height;
4922 ptrdiff_t count = SPECPDL_INDEX ();
4923 struct gcpro gcpro1, gcpro2, gcpro3;
4924 int face_change_count_before = face_change_count;
4925 Lisp_Object buffer;
4926 struct buffer *old_buffer;
4927
4928 if (!dpyinfo->terminal->name)
4929 error ("Terminal is not live, can't create new frames on it");
4930
4931 parms = Fcopy_alist (parms);
4932
4933 /* Get the name of the frame to use for resource lookup. */
4934 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
4935 if (!STRINGP (name)
4936 && !EQ (name, Qunbound)
4937 && !NILP (name))
4938 error ("Invalid frame name--not a string or nil");
4939
4940 frame = Qnil;
4941 GCPRO3 (parms, name, frame);
4942 f = make_frame (true);
4943 XSETFRAME (frame, f);
4944
4945 AUTO_STRING (tip, " *tip*");
4946 buffer = Fget_buffer_create (tip);
4947 /* Use set_window_buffer instead of Fset_window_buffer (see
4948 discussion of bug#11984, bug#12025, bug#12026). */
4949 set_window_buffer (FRAME_ROOT_WINDOW (f), buffer, false, false);
4950 old_buffer = current_buffer;
4951 set_buffer_internal_1 (XBUFFER (buffer));
4952 bset_truncate_lines (current_buffer, Qnil);
4953 specbind (Qinhibit_read_only, Qt);
4954 specbind (Qinhibit_modification_hooks, Qt);
4955 Ferase_buffer ();
4956 Finsert (1, &text);
4957 set_buffer_internal_1 (old_buffer);
4958
4959 record_unwind_protect (unwind_create_tip_frame, frame);
4960
4961 f->terminal = dpyinfo->terminal;
4962
4963 /* By setting the output method, we're essentially saying that
4964 the frame is live, as per FRAME_LIVE_P. If we get a signal
4965 from this point on, x_destroy_window might screw up reference
4966 counts etc. */
4967 f->output_method = output_x_window;
4968 f->output_data.x = xzalloc (sizeof *f->output_data.x);
4969 f->output_data.x->icon_bitmap = -1;
4970 FRAME_FONTSET (f) = -1;
4971 f->output_data.x->scroll_bar_foreground_pixel = -1;
4972 f->output_data.x->scroll_bar_background_pixel = -1;
4973 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
4974 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
4975 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
4976 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
4977 f->output_data.x->white_relief.pixel = -1;
4978 f->output_data.x->black_relief.pixel = -1;
4979
4980 fset_icon_name (f, Qnil);
4981 FRAME_DISPLAY_INFO (f) = dpyinfo;
4982 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
4983 f->output_data.x->explicit_parent = false;
4984
4985 /* These colors will be set anyway later, but it's important
4986 to get the color reference counts right, so initialize them! */
4987 {
4988 Lisp_Object black;
4989 struct gcpro gcpro1;
4990
4991 /* Function x_decode_color can signal an error. Make
4992 sure to initialize color slots so that we won't try
4993 to free colors we haven't allocated. */
4994 FRAME_FOREGROUND_PIXEL (f) = -1;
4995 FRAME_BACKGROUND_PIXEL (f) = -1;
4996 f->output_data.x->cursor_pixel = -1;
4997 f->output_data.x->cursor_foreground_pixel = -1;
4998 f->output_data.x->border_pixel = -1;
4999 f->output_data.x->mouse_pixel = -1;
5000
5001 black = build_string ("black");
5002 GCPRO1 (black);
5003 FRAME_FOREGROUND_PIXEL (f)
5004 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5005 FRAME_BACKGROUND_PIXEL (f)
5006 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5007 f->output_data.x->cursor_pixel
5008 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5009 f->output_data.x->cursor_foreground_pixel
5010 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5011 f->output_data.x->border_pixel
5012 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5013 f->output_data.x->mouse_pixel
5014 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5015 UNGCPRO;
5016 }
5017
5018 /* Set the name; the functions to which we pass f expect the name to
5019 be set. */
5020 if (EQ (name, Qunbound) || NILP (name))
5021 {
5022 fset_name (f, build_string (dpyinfo->x_id_name));
5023 f->explicit_name = false;
5024 }
5025 else
5026 {
5027 fset_name (f, name);
5028 f->explicit_name = true;
5029 /* use the frame's title when getting resources for this frame. */
5030 specbind (Qx_resource_name, name);
5031 }
5032
5033 register_font_driver (&xfont_driver, f);
5034 #ifdef HAVE_FREETYPE
5035 #ifdef HAVE_XFT
5036 register_font_driver (&xftfont_driver, f);
5037 #else /* not HAVE_XFT */
5038 register_font_driver (&ftxfont_driver, f);
5039 #endif /* not HAVE_XFT */
5040 #endif /* HAVE_FREETYPE */
5041
5042 x_default_parameter (f, parms, Qfont_backend, Qnil,
5043 "fontBackend", "FontBackend", RES_TYPE_STRING);
5044
5045 /* Extract the window parameters from the supplied values that are
5046 needed to determine window geometry. */
5047 x_default_font_parameter (f, parms);
5048
5049 x_default_parameter (f, parms, Qborder_width, make_number (0),
5050 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
5051
5052 /* This defaults to 2 in order to match xterm. We recognize either
5053 internalBorderWidth or internalBorder (which is what xterm calls
5054 it). */
5055 if (NILP (Fassq (Qinternal_border_width, parms)))
5056 {
5057 Lisp_Object value;
5058
5059 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
5060 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
5061 if (! EQ (value, Qunbound))
5062 parms = Fcons (Fcons (Qinternal_border_width, value),
5063 parms);
5064 }
5065
5066 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
5067 "internalBorderWidth", "internalBorderWidth",
5068 RES_TYPE_NUMBER);
5069 x_default_parameter (f, parms, Qright_divider_width, make_number (0),
5070 NULL, NULL, RES_TYPE_NUMBER);
5071 x_default_parameter (f, parms, Qbottom_divider_width, make_number (0),
5072 NULL, NULL, RES_TYPE_NUMBER);
5073
5074 /* Also do the stuff which must be set before the window exists. */
5075 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
5076 "foreground", "Foreground", RES_TYPE_STRING);
5077 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
5078 "background", "Background", RES_TYPE_STRING);
5079 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
5080 "pointerColor", "Foreground", RES_TYPE_STRING);
5081 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
5082 "cursorColor", "Foreground", RES_TYPE_STRING);
5083 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
5084 "borderColor", "BorderColor", RES_TYPE_STRING);
5085
5086 #ifdef GLYPH_DEBUG
5087 image_cache_refcount =
5088 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
5089 dpyinfo_refcount = dpyinfo->reference_count;
5090 #endif /* GLYPH_DEBUG */
5091
5092 /* Init faces before x_default_parameter is called for the
5093 scroll-bar-width parameter because otherwise we end up in
5094 init_iterator with a null face cache, which should not happen. */
5095 init_frame_faces (f);
5096
5097 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
5098
5099 x_figure_window_size (f, parms, false);
5100
5101 {
5102 XSetWindowAttributes attrs;
5103 unsigned long mask;
5104 Atom type = FRAME_DISPLAY_INFO (f)->Xatom_net_window_type_tooltip;
5105
5106 block_input ();
5107 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
5108 if (DoesSaveUnders (dpyinfo->screen))
5109 mask |= CWSaveUnder;
5110
5111 /* Window managers look at the override-redirect flag to determine
5112 whether or net to give windows a decoration (Xlib spec, chapter
5113 3.2.8). */
5114 attrs.override_redirect = True;
5115 attrs.save_under = True;
5116 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
5117 /* Arrange for getting MapNotify and UnmapNotify events. */
5118 attrs.event_mask = StructureNotifyMask;
5119 tip_window
5120 = FRAME_X_WINDOW (f)
5121 = XCreateWindow (FRAME_X_DISPLAY (f),
5122 FRAME_DISPLAY_INFO (f)->root_window,
5123 /* x, y, width, height */
5124 0, 0, 1, 1,
5125 /* Border. */
5126 f->border_width,
5127 CopyFromParent, InputOutput, CopyFromParent,
5128 mask, &attrs);
5129 XChangeProperty (FRAME_X_DISPLAY (f), tip_window,
5130 FRAME_DISPLAY_INFO (f)->Xatom_net_window_type,
5131 XA_ATOM, 32, PropModeReplace,
5132 (unsigned char *)&type, 1);
5133 unblock_input ();
5134 }
5135
5136 x_make_gc (f);
5137
5138 x_default_parameter (f, parms, Qauto_raise, Qnil,
5139 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5140 x_default_parameter (f, parms, Qauto_lower, Qnil,
5141 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5142 x_default_parameter (f, parms, Qcursor_type, Qbox,
5143 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5144
5145 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
5146 Change will not be effected unless different from the current
5147 FRAME_LINES (f). */
5148 width = FRAME_COLS (f);
5149 height = FRAME_LINES (f);
5150 SET_FRAME_COLS (f, 0);
5151 SET_FRAME_LINES (f, 0);
5152 change_frame_size (f, width, height, true, false, false, false);
5153
5154 /* Add `tooltip' frame parameter's default value. */
5155 if (NILP (Fframe_parameter (frame, Qtooltip)))
5156 {
5157 AUTO_FRAME_ARG (arg, Qtooltip, Qt);
5158 Fmodify_frame_parameters (frame, arg);
5159 }
5160
5161 /* FIXME - can this be done in a similar way to normal frames?
5162 http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00641.html */
5163
5164 /* Set the `display-type' frame parameter before setting up faces. */
5165 {
5166 Lisp_Object disptype;
5167
5168 if (FRAME_DISPLAY_INFO (f)->n_planes == 1)
5169 disptype = intern ("mono");
5170 else if (FRAME_DISPLAY_INFO (f)->visual->class == GrayScale
5171 || FRAME_DISPLAY_INFO (f)->visual->class == StaticGray)
5172 disptype = intern ("grayscale");
5173 else
5174 disptype = intern ("color");
5175
5176 if (NILP (Fframe_parameter (frame, Qdisplay_type)))
5177 {
5178 AUTO_FRAME_ARG (arg, Qdisplay_type, disptype);
5179 Fmodify_frame_parameters (frame, arg);
5180 }
5181 }
5182
5183 /* Set up faces after all frame parameters are known. This call
5184 also merges in face attributes specified for new frames.
5185
5186 Frame parameters may be changed if .Xdefaults contains
5187 specifications for the default font. For example, if there is an
5188 `Emacs.default.attributeBackground: pink', the `background-color'
5189 attribute of the frame get's set, which let's the internal border
5190 of the tooltip frame appear in pink. Prevent this. */
5191 {
5192 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
5193
5194 /* Set tip_frame here, so that */
5195 tip_frame = frame;
5196 call2 (Qface_set_after_frame_default, frame, Qnil);
5197
5198 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
5199 {
5200 AUTO_FRAME_ARG (arg, Qbackground_color, bg);
5201 Fmodify_frame_parameters (frame, arg);
5202 }
5203 }
5204
5205 f->no_split = true;
5206
5207 UNGCPRO;
5208
5209 /* Now that the frame will be official, it counts as a reference to
5210 its display and terminal. */
5211 FRAME_DISPLAY_INFO (f)->reference_count++;
5212 f->terminal->reference_count++;
5213
5214 /* It is now ok to make the frame official even if we get an error
5215 below. And the frame needs to be on Vframe_list or making it
5216 visible won't work. */
5217 Vframe_list = Fcons (frame, Vframe_list);
5218 f->can_x_set_window_size = true;
5219
5220 /* Setting attributes of faces of the tooltip frame from resources
5221 and similar will increment face_change_count, which leads to the
5222 clearing of all current matrices. Since this isn't necessary
5223 here, avoid it by resetting face_change_count to the value it
5224 had before we created the tip frame. */
5225 face_change_count = face_change_count_before;
5226
5227 /* Discard the unwind_protect. */
5228 return unbind_to (count, frame);
5229 }
5230
5231
5232 /* Compute where to display tip frame F. PARMS is the list of frame
5233 parameters for F. DX and DY are specified offsets from the current
5234 location of the mouse. WIDTH and HEIGHT are the width and height
5235 of the tooltip. Return coordinates relative to the root window of
5236 the display in *ROOT_X, and *ROOT_Y. */
5237
5238 static void
5239 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)
5240 {
5241 Lisp_Object left, top;
5242 int win_x, win_y;
5243 Window root, child;
5244 unsigned pmask;
5245
5246 /* User-specified position? */
5247 left = Fcdr (Fassq (Qleft, parms));
5248 top = Fcdr (Fassq (Qtop, parms));
5249
5250 /* Move the tooltip window where the mouse pointer is. Resize and
5251 show it. */
5252 if (!INTEGERP (left) || !INTEGERP (top))
5253 {
5254 block_input ();
5255 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_DISPLAY_INFO (f)->root_window,
5256 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
5257 unblock_input ();
5258 }
5259
5260 if (INTEGERP (top))
5261 *root_y = XINT (top);
5262 else if (*root_y + XINT (dy) <= 0)
5263 *root_y = 0; /* Can happen for negative dy */
5264 else if (*root_y + XINT (dy) + height
5265 <= x_display_pixel_height (FRAME_DISPLAY_INFO (f)))
5266 /* It fits below the pointer */
5267 *root_y += XINT (dy);
5268 else if (height + XINT (dy) <= *root_y)
5269 /* It fits above the pointer. */
5270 *root_y -= height + XINT (dy);
5271 else
5272 /* Put it on the top. */
5273 *root_y = 0;
5274
5275 if (INTEGERP (left))
5276 *root_x = XINT (left);
5277 else if (*root_x + XINT (dx) <= 0)
5278 *root_x = 0; /* Can happen for negative dx */
5279 else if (*root_x + XINT (dx) + width
5280 <= x_display_pixel_width (FRAME_DISPLAY_INFO (f)))
5281 /* It fits to the right of the pointer. */
5282 *root_x += XINT (dx);
5283 else if (width + XINT (dx) <= *root_x)
5284 /* It fits to the left of the pointer. */
5285 *root_x -= width + XINT (dx);
5286 else
5287 /* Put it left-justified on the screen--it ought to fit that way. */
5288 *root_x = 0;
5289 }
5290
5291
5292 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
5293 doc: /* Show STRING in a "tooltip" window on frame FRAME.
5294 A tooltip window is a small X window displaying a string.
5295
5296 This is an internal function; Lisp code should call `tooltip-show'.
5297
5298 FRAME nil or omitted means use the selected frame.
5299
5300 PARMS is an optional list of frame parameters which can be used to
5301 change the tooltip's appearance.
5302
5303 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
5304 means use the default timeout of 5 seconds.
5305
5306 If the list of frame parameters PARMS contains a `left' parameters,
5307 the tooltip is displayed at that x-position. Otherwise it is
5308 displayed at the mouse position, with offset DX added (default is 5 if
5309 DX isn't specified). Likewise for the y-position; if a `top' frame
5310 parameter is specified, it determines the y-position of the tooltip
5311 window, otherwise it is displayed at the mouse position, with offset
5312 DY added (default is -10).
5313
5314 A tooltip's maximum size is specified by `x-max-tooltip-size'.
5315 Text larger than the specified size is clipped. */)
5316 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
5317 {
5318 struct frame *f;
5319 struct window *w;
5320 int root_x, root_y;
5321 struct buffer *old_buffer;
5322 struct text_pos pos;
5323 int i, width, height;
5324 bool seen_reversed_p;
5325 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5326 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5327 ptrdiff_t count = SPECPDL_INDEX ();
5328
5329 specbind (Qinhibit_redisplay, Qt);
5330
5331 GCPRO4 (string, parms, frame, timeout);
5332
5333 CHECK_STRING (string);
5334 if (SCHARS (string) == 0)
5335 string = make_unibyte_string (" ", 1);
5336
5337 f = decode_window_system_frame (frame);
5338 if (NILP (timeout))
5339 timeout = make_number (5);
5340 else
5341 CHECK_NATNUM (timeout);
5342
5343 if (NILP (dx))
5344 dx = make_number (5);
5345 else
5346 CHECK_NUMBER (dx);
5347
5348 if (NILP (dy))
5349 dy = make_number (-10);
5350 else
5351 CHECK_NUMBER (dy);
5352
5353 #ifdef USE_GTK
5354 if (x_gtk_use_system_tooltips)
5355 {
5356 bool ok;
5357
5358 /* Hide a previous tip, if any. */
5359 Fx_hide_tip ();
5360
5361 block_input ();
5362 ok = xg_prepare_tooltip (f, string, &width, &height);
5363 if (ok)
5364 {
5365 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5366 xg_show_tooltip (f, root_x, root_y);
5367 /* This is used in Fx_hide_tip. */
5368 XSETFRAME (tip_frame, f);
5369 }
5370 unblock_input ();
5371 if (ok) goto start_timer;
5372 }
5373 #endif /* USE_GTK */
5374
5375 if (NILP (last_show_tip_args))
5376 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5377
5378 if (!NILP (tip_frame))
5379 {
5380 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5381 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5382 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5383
5384 if (EQ (frame, last_frame)
5385 && !NILP (Fequal (last_string, string))
5386 && !NILP (Fequal (last_parms, parms)))
5387 {
5388 struct frame *tip_f = XFRAME (tip_frame);
5389
5390 /* Only DX and DY have changed. */
5391 if (!NILP (tip_timer))
5392 {
5393 Lisp_Object timer = tip_timer;
5394 tip_timer = Qnil;
5395 call1 (Qcancel_timer, timer);
5396 }
5397
5398 block_input ();
5399 compute_tip_xy (tip_f, parms, dx, dy, FRAME_PIXEL_WIDTH (tip_f),
5400 FRAME_PIXEL_HEIGHT (tip_f), &root_x, &root_y);
5401 XMoveWindow (FRAME_X_DISPLAY (tip_f), FRAME_X_WINDOW (tip_f),
5402 root_x, root_y);
5403 unblock_input ();
5404 goto start_timer;
5405 }
5406 }
5407
5408 /* Hide a previous tip, if any. */
5409 Fx_hide_tip ();
5410
5411 ASET (last_show_tip_args, 0, string);
5412 ASET (last_show_tip_args, 1, frame);
5413 ASET (last_show_tip_args, 2, parms);
5414
5415 /* Add default values to frame parameters. */
5416 if (NILP (Fassq (Qname, parms)))
5417 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5418 if (NILP (Fassq (Qinternal_border_width, parms)))
5419 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5420 if (NILP (Fassq (Qborder_width, parms)))
5421 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5422 if (NILP (Fassq (Qbottom_divider_width, parms)))
5423 parms = Fcons (Fcons (Qbottom_divider_width, make_number (0)), parms);
5424 if (NILP (Fassq (Qright_divider_width, parms)))
5425 parms = Fcons (Fcons (Qright_divider_width, make_number (0)), parms);
5426 if (NILP (Fassq (Qborder_color, parms)))
5427 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5428 if (NILP (Fassq (Qbackground_color, parms)))
5429 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5430 parms);
5431
5432 /* Create a frame for the tooltip, and record it in the global
5433 variable tip_frame. */
5434 frame = x_create_tip_frame (FRAME_DISPLAY_INFO (f), parms, string);
5435 f = XFRAME (frame);
5436
5437 /* Set up the frame's root window. */
5438 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5439 w->left_col = 0;
5440 w->top_line = 0;
5441 w->pixel_left = 0;
5442 w->pixel_top = 0;
5443
5444 if (CONSP (Vx_max_tooltip_size)
5445 && RANGED_INTEGERP (1, XCAR (Vx_max_tooltip_size), INT_MAX)
5446 && RANGED_INTEGERP (1, XCDR (Vx_max_tooltip_size), INT_MAX))
5447 {
5448 w->total_cols = XFASTINT (XCAR (Vx_max_tooltip_size));
5449 w->total_lines = XFASTINT (XCDR (Vx_max_tooltip_size));
5450 }
5451 else
5452 {
5453 w->total_cols = 80;
5454 w->total_lines = 40;
5455 }
5456
5457 w->pixel_width = w->total_cols * FRAME_COLUMN_WIDTH (f);
5458 w->pixel_height = w->total_lines * FRAME_LINE_HEIGHT (f);
5459
5460 FRAME_TOTAL_COLS (f) = w->total_cols;
5461 adjust_frame_glyphs (f);
5462 w->pseudo_window_p = true;
5463
5464 /* Display the tooltip text in a temporary buffer. */
5465 old_buffer = current_buffer;
5466 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->contents));
5467 bset_truncate_lines (current_buffer, Qnil);
5468 clear_glyph_matrix (w->desired_matrix);
5469 clear_glyph_matrix (w->current_matrix);
5470 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5471 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
5472
5473 /* Compute width and height of the tooltip. */
5474 width = height = 0;
5475 seen_reversed_p = false;
5476 for (i = 0; i < w->desired_matrix->nrows; ++i)
5477 {
5478 struct glyph_row *row = &w->desired_matrix->rows[i];
5479 struct glyph *last;
5480 int row_width;
5481
5482 /* Stop at the first empty row at the end. */
5483 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5484 break;
5485
5486 /* Let the row go over the full width of the frame. */
5487 row->full_width_p = true;
5488
5489 row_width = row->pixel_width;
5490 if (row->used[TEXT_AREA])
5491 {
5492 /* There's a glyph at the end of rows that is used to place
5493 the cursor there. Don't include the width of this glyph. */
5494 if (!row->reversed_p)
5495 {
5496 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5497 if (NILP (last->object))
5498 row_width -= last->pixel_width;
5499 }
5500 else
5501 {
5502 /* There could be a stretch glyph at the beginning of R2L
5503 rows that is produced by extend_face_to_end_of_line.
5504 Don't count that glyph. */
5505 struct glyph *g = row->glyphs[TEXT_AREA];
5506
5507 if (g->type == STRETCH_GLYPH && NILP (g->object))
5508 {
5509 row_width -= g->pixel_width;
5510 seen_reversed_p = true;
5511 }
5512 }
5513 }
5514
5515 height += row->height;
5516 width = max (width, row_width);
5517 }
5518
5519 /* If we've seen partial-length R2L rows, we need to re-adjust the
5520 tool-tip frame width and redisplay it again, to avoid over-wide
5521 tips due to the stretch glyph that extends R2L lines to full
5522 width of the frame. */
5523 if (seen_reversed_p)
5524 {
5525 /* w->total_cols and FRAME_TOTAL_COLS want the width in columns,
5526 not in pixels. */
5527 w->pixel_width = width;
5528 width /= WINDOW_FRAME_COLUMN_WIDTH (w);
5529 w->total_cols = width;
5530 FRAME_TOTAL_COLS (f) = width;
5531 SET_FRAME_WIDTH (f, width);
5532 adjust_frame_glyphs (f);
5533 clear_glyph_matrix (w->desired_matrix);
5534 clear_glyph_matrix (w->current_matrix);
5535 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
5536 width = height = 0;
5537 /* Recompute width and height of the tooltip. */
5538 for (i = 0; i < w->desired_matrix->nrows; ++i)
5539 {
5540 struct glyph_row *row = &w->desired_matrix->rows[i];
5541 struct glyph *last;
5542 int row_width;
5543
5544 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5545 break;
5546 row->full_width_p = true;
5547 row_width = row->pixel_width;
5548 if (row->used[TEXT_AREA] && !row->reversed_p)
5549 {
5550 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5551 if (NILP (last->object))
5552 row_width -= last->pixel_width;
5553 }
5554
5555 height += row->height;
5556 width = max (width, row_width);
5557 }
5558 }
5559
5560 /* Add the frame's internal border to the width and height the X
5561 window should have. */
5562 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5563 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5564
5565 /* Move the tooltip window where the mouse pointer is. Resize and
5566 show it. */
5567 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5568
5569 block_input ();
5570 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5571 root_x, root_y, width, height);
5572 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5573 unblock_input ();
5574
5575 /* Draw into the window. */
5576 w->must_be_updated_p = true;
5577 update_single_window (w);
5578
5579 /* Restore original current buffer. */
5580 set_buffer_internal_1 (old_buffer);
5581 windows_or_buffers_changed = old_windows_or_buffers_changed;
5582
5583 start_timer:
5584 /* Let the tip disappear after timeout seconds. */
5585 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5586 intern ("x-hide-tip"));
5587
5588 UNGCPRO;
5589 return unbind_to (count, Qnil);
5590 }
5591
5592
5593 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5594 doc: /* Hide the current tooltip window, if there is any.
5595 Value is t if tooltip was open, nil otherwise. */)
5596 (void)
5597 {
5598 ptrdiff_t count;
5599 Lisp_Object deleted, frame, timer;
5600 struct gcpro gcpro1, gcpro2;
5601
5602 /* Return quickly if nothing to do. */
5603 if (NILP (tip_timer) && NILP (tip_frame))
5604 return Qnil;
5605
5606 frame = tip_frame;
5607 timer = tip_timer;
5608 GCPRO2 (frame, timer);
5609 tip_frame = tip_timer = deleted = Qnil;
5610
5611 count = SPECPDL_INDEX ();
5612 specbind (Qinhibit_redisplay, Qt);
5613 specbind (Qinhibit_quit, Qt);
5614
5615 if (!NILP (timer))
5616 call1 (Qcancel_timer, timer);
5617
5618 #ifdef USE_GTK
5619 {
5620 /* When using system tooltip, tip_frame is the Emacs frame on which
5621 the tip is shown. */
5622 struct frame *f = XFRAME (frame);
5623 if (FRAME_LIVE_P (f) && xg_hide_tooltip (f))
5624 frame = Qnil;
5625 }
5626 #endif
5627
5628 if (FRAMEP (frame))
5629 {
5630 delete_frame (frame, Qnil);
5631 deleted = Qt;
5632
5633 #ifdef USE_LUCID
5634 /* Bloodcurdling hack alert: The Lucid menu bar widget's
5635 redisplay procedure is not called when a tip frame over menu
5636 items is unmapped. Redisplay the menu manually... */
5637 {
5638 Widget w;
5639 struct frame *f = SELECTED_FRAME ();
5640 w = f->output_data.x->menubar_widget;
5641
5642 if (!DoesSaveUnders (FRAME_DISPLAY_INFO (f)->screen)
5643 && w != NULL)
5644 {
5645 block_input ();
5646 xlwmenu_redisplay (w);
5647 unblock_input ();
5648 }
5649 }
5650 #endif /* USE_LUCID */
5651 }
5652
5653 UNGCPRO;
5654 return unbind_to (count, deleted);
5655 }
5656
5657
5658 \f
5659 /***********************************************************************
5660 File selection dialog
5661 ***********************************************************************/
5662
5663 DEFUN ("x-uses-old-gtk-dialog", Fx_uses_old_gtk_dialog,
5664 Sx_uses_old_gtk_dialog,
5665 0, 0, 0,
5666 doc: /* Return t if the old Gtk+ file selection dialog is used. */)
5667 (void)
5668 {
5669 #ifdef USE_GTK
5670 if (use_dialog_box
5671 && use_file_dialog
5672 && window_system_available (SELECTED_FRAME ())
5673 && xg_uses_old_file_dialog ())
5674 return Qt;
5675 #endif
5676 return Qnil;
5677 }
5678
5679
5680 #ifdef USE_MOTIF
5681 /* Callback for "OK" and "Cancel" on file selection dialog. */
5682
5683 static void
5684 file_dialog_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5685 {
5686 int *result = client_data;
5687 XmAnyCallbackStruct *cb = call_data;
5688 *result = cb->reason;
5689 }
5690
5691
5692 /* Callback for unmapping a file selection dialog. This is used to
5693 capture the case where a dialog is closed via a window manager's
5694 closer button, for example. Using a XmNdestroyCallback didn't work
5695 in this case. */
5696
5697 static void
5698 file_dialog_unmap_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5699 {
5700 int *result = client_data;
5701 *result = XmCR_CANCEL;
5702 }
5703
5704 static void
5705 clean_up_file_dialog (void *arg)
5706 {
5707 Widget dialog = arg;
5708
5709 /* Clean up. */
5710 block_input ();
5711 XtUnmanageChild (dialog);
5712 XtDestroyWidget (dialog);
5713 x_menu_set_in_use (false);
5714 unblock_input ();
5715 }
5716
5717
5718 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5719 doc: /* Read file name, prompting with PROMPT in directory DIR.
5720 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5721 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5722 or directory must exist.
5723
5724 This function is only defined on NS, MS Windows, and X Windows with the
5725 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5726 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories.
5727 On Windows 7 and later, the file selection dialog "remembers" the last
5728 directory where the user selected a file, and will open that directory
5729 instead of DIR on subsequent invocations of this function with the same
5730 value of DIR as in previous invocations; this is standard Windows behavior. */)
5731 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename,
5732 Lisp_Object mustmatch, Lisp_Object only_dir_p)
5733 {
5734 int result;
5735 struct frame *f = SELECTED_FRAME ();
5736 Lisp_Object file = Qnil;
5737 Lisp_Object decoded_file;
5738 Widget dialog, text, help;
5739 Arg al[10];
5740 int ac = 0;
5741 XmString dir_xmstring, pattern_xmstring;
5742 ptrdiff_t count = SPECPDL_INDEX ();
5743 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5744
5745 check_window_system (f);
5746
5747 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5748
5749 if (popup_activated ())
5750 error ("Trying to use a menu from within a menu-entry");
5751
5752 CHECK_STRING (prompt);
5753 CHECK_STRING (dir);
5754
5755 /* Prevent redisplay. */
5756 specbind (Qinhibit_redisplay, Qt);
5757
5758 block_input ();
5759
5760 /* Create the dialog with PROMPT as title, using DIR as initial
5761 directory and using "*" as pattern. */
5762 dir = Fexpand_file_name (dir, Qnil);
5763 dir_xmstring = XmStringCreateLocalized (SSDATA (dir));
5764 pattern_xmstring = XmStringCreateLocalized ("*");
5765
5766 XtSetArg (al[ac], XmNtitle, SDATA (prompt)); ++ac;
5767 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
5768 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
5769 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
5770 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
5771 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
5772 "fsb", al, ac);
5773 XmStringFree (dir_xmstring);
5774 XmStringFree (pattern_xmstring);
5775
5776 /* Add callbacks for OK and Cancel. */
5777 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
5778 (XtPointer) &result);
5779 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
5780 (XtPointer) &result);
5781 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
5782 (XtPointer) &result);
5783
5784 /* Remove the help button since we can't display help. */
5785 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
5786 XtUnmanageChild (help);
5787
5788 /* Mark OK button as default. */
5789 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
5790 XmNshowAsDefault, True, NULL);
5791
5792 /* If MUSTMATCH is non-nil, disable the file entry field of the
5793 dialog, so that the user must select a file from the files list
5794 box. We can't remove it because we wouldn't have a way to get at
5795 the result file name, then. */
5796 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5797 if (!NILP (mustmatch))
5798 {
5799 Widget label;
5800 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
5801 XtSetSensitive (text, False);
5802 XtSetSensitive (label, False);
5803 }
5804
5805 /* Manage the dialog, so that list boxes get filled. */
5806 XtManageChild (dialog);
5807
5808 if (STRINGP (default_filename))
5809 {
5810 XmString default_xmstring;
5811 Widget wtext = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5812 Widget list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
5813
5814 XmTextPosition last_pos = XmTextFieldGetLastPosition (wtext);
5815 XmTextFieldReplace (wtext, 0, last_pos,
5816 (SSDATA (Ffile_name_nondirectory (default_filename))));
5817
5818 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
5819 must include the path for this to work. */
5820
5821 default_xmstring = XmStringCreateLocalized (SSDATA (default_filename));
5822
5823 if (XmListItemExists (list, default_xmstring))
5824 {
5825 int item_pos = XmListItemPos (list, default_xmstring);
5826 /* Select the item and scroll it into view. */
5827 XmListSelectPos (list, item_pos, True);
5828 XmListSetPos (list, item_pos);
5829 }
5830
5831 XmStringFree (default_xmstring);
5832 }
5833
5834 record_unwind_protect_ptr (clean_up_file_dialog, dialog);
5835
5836 /* Process events until the user presses Cancel or OK. */
5837 x_menu_set_in_use (true);
5838 result = 0;
5839 while (result == 0)
5840 {
5841 XEvent event;
5842 x_menu_wait_for_event (0);
5843 XtAppNextEvent (Xt_app_con, &event);
5844 if (event.type == KeyPress
5845 && FRAME_X_DISPLAY (f) == event.xkey.display)
5846 {
5847 KeySym keysym = XLookupKeysym (&event.xkey, 0);
5848
5849 /* Pop down on C-g. */
5850 if (keysym == XK_g && (event.xkey.state & ControlMask) != 0)
5851 XtUnmanageChild (dialog);
5852 }
5853
5854 (void) x_dispatch_event (&event, FRAME_X_DISPLAY (f));
5855 }
5856
5857 /* Get the result. */
5858 if (result == XmCR_OK)
5859 {
5860 XmString text_string;
5861 String data;
5862
5863 XtVaGetValues (dialog, XmNtextString, &text_string, NULL);
5864 XmStringGetLtoR (text_string, XmFONTLIST_DEFAULT_TAG, &data);
5865 XmStringFree (text_string);
5866 file = build_string (data);
5867 XtFree (data);
5868 }
5869 else
5870 file = Qnil;
5871
5872 unblock_input ();
5873 UNGCPRO;
5874
5875 /* Make "Cancel" equivalent to C-g. */
5876 if (NILP (file))
5877 Fsignal (Qquit, Qnil);
5878
5879 decoded_file = DECODE_FILE (file);
5880
5881 return unbind_to (count, decoded_file);
5882 }
5883
5884 #endif /* USE_MOTIF */
5885
5886 #ifdef USE_GTK
5887
5888 static void
5889 clean_up_dialog (void)
5890 {
5891 x_menu_set_in_use (false);
5892 }
5893
5894 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5895 doc: /* Read file name, prompting with PROMPT in directory DIR.
5896 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5897 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5898 or directory must exist.
5899
5900 This function is only defined on NS, MS Windows, and X Windows with the
5901 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5902 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories.
5903 On Windows 7 and later, the file selection dialog "remembers" the last
5904 directory where the user selected a file, and will open that directory
5905 instead of DIR on subsequent invocations of this function with the same
5906 value of DIR as in previous invocations; this is standard Windows behavior. */)
5907 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
5908 {
5909 struct frame *f = SELECTED_FRAME ();
5910 char *fn;
5911 Lisp_Object file = Qnil;
5912 Lisp_Object decoded_file;
5913 ptrdiff_t count = SPECPDL_INDEX ();
5914 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5915 char *cdef_file;
5916
5917 check_window_system (f);
5918
5919 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5920
5921 if (popup_activated ())
5922 error ("Trying to use a menu from within a menu-entry");
5923
5924 CHECK_STRING (prompt);
5925 CHECK_STRING (dir);
5926
5927 /* Prevent redisplay. */
5928 specbind (Qinhibit_redisplay, Qt);
5929 record_unwind_protect_void (clean_up_dialog);
5930
5931 block_input ();
5932
5933 if (STRINGP (default_filename))
5934 cdef_file = SSDATA (default_filename);
5935 else
5936 cdef_file = SSDATA (dir);
5937
5938 fn = xg_get_file_name (f, SSDATA (prompt), cdef_file,
5939 ! NILP (mustmatch),
5940 ! NILP (only_dir_p));
5941
5942 if (fn)
5943 {
5944 file = build_string (fn);
5945 xfree (fn);
5946 }
5947
5948 unblock_input ();
5949 UNGCPRO;
5950
5951 /* Make "Cancel" equivalent to C-g. */
5952 if (NILP (file))
5953 Fsignal (Qquit, Qnil);
5954
5955 decoded_file = DECODE_FILE (file);
5956
5957 return unbind_to (count, decoded_file);
5958 }
5959
5960
5961 #ifdef HAVE_FREETYPE
5962
5963 DEFUN ("x-select-font", Fx_select_font, Sx_select_font, 0, 2, 0,
5964 doc: /* Read a font using a GTK dialog.
5965 Return either a font spec (for GTK versions >= 3.2) or a string
5966 containing a GTK-style font name.
5967
5968 FRAME is the frame on which to pop up the font chooser. If omitted or
5969 nil, it defaults to the selected frame. */)
5970 (Lisp_Object frame, Lisp_Object ignored)
5971 {
5972 struct frame *f = decode_window_system_frame (frame);
5973 Lisp_Object font;
5974 Lisp_Object font_param;
5975 char *default_name = NULL;
5976 struct gcpro gcpro1, gcpro2;
5977 ptrdiff_t count = SPECPDL_INDEX ();
5978
5979 if (popup_activated ())
5980 error ("Trying to use a menu from within a menu-entry");
5981
5982 /* Prevent redisplay. */
5983 specbind (Qinhibit_redisplay, Qt);
5984 record_unwind_protect_void (clean_up_dialog);
5985
5986 block_input ();
5987
5988 GCPRO2 (font_param, font);
5989
5990 XSETFONT (font, FRAME_FONT (f));
5991 font_param = Ffont_get (font, intern (":name"));
5992 if (STRINGP (font_param))
5993 default_name = xlispstrdup (font_param);
5994 else
5995 {
5996 font_param = Fframe_parameter (frame, Qfont_param);
5997 if (STRINGP (font_param))
5998 default_name = xlispstrdup (font_param);
5999 }
6000
6001 font = xg_get_font (f, default_name);
6002 xfree (default_name);
6003
6004 unblock_input ();
6005
6006 if (NILP (font))
6007 Fsignal (Qquit, Qnil);
6008
6009 return unbind_to (count, font);
6010 }
6011 #endif /* HAVE_FREETYPE */
6012
6013 #endif /* USE_GTK */
6014
6015 \f
6016 /***********************************************************************
6017 Keyboard
6018 ***********************************************************************/
6019
6020 #ifdef HAVE_XKB
6021 #include <X11/XKBlib.h>
6022 #include <X11/keysym.h>
6023 #endif
6024
6025 DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
6026 Sx_backspace_delete_keys_p, 0, 1, 0,
6027 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
6028 FRAME nil means use the selected frame.
6029 Value is t if we know that both keys are present, and are mapped to the
6030 usual X keysyms. Value is `lambda' if we cannot determine if both keys are
6031 present and mapped to the usual X keysyms. */)
6032 (Lisp_Object frame)
6033 {
6034 #ifndef HAVE_XKB
6035 return Qlambda;
6036 #else
6037 XkbDescPtr kb;
6038 struct frame *f = decode_window_system_frame (frame);
6039 Display *dpy = FRAME_X_DISPLAY (f);
6040 Lisp_Object have_keys;
6041 int major, minor, op, event, error_code;
6042
6043 block_input ();
6044
6045 /* Check library version in case we're dynamically linked. */
6046 major = XkbMajorVersion;
6047 minor = XkbMinorVersion;
6048 if (!XkbLibraryVersion (&major, &minor))
6049 {
6050 unblock_input ();
6051 return Qlambda;
6052 }
6053
6054 /* Check that the server supports XKB. */
6055 major = XkbMajorVersion;
6056 minor = XkbMinorVersion;
6057 if (!XkbQueryExtension (dpy, &op, &event, &error_code, &major, &minor))
6058 {
6059 unblock_input ();
6060 return Qlambda;
6061 }
6062
6063 /* In this code we check that the keyboard has physical keys with names
6064 that start with BKSP (Backspace) and DELE (Delete), and that they
6065 generate keysym XK_BackSpace and XK_Delete respectively.
6066 This function is used to test if normal-erase-is-backspace should be
6067 turned on.
6068 An alternative approach would be to just check if XK_BackSpace and
6069 XK_Delete are mapped to any key. But if any of those are mapped to
6070 some non-intuitive key combination (Meta-Shift-Ctrl-whatever) and the
6071 user doesn't know about it, it is better to return false here.
6072 It is more obvious to the user what to do if she/he has two keys
6073 clearly marked with names/symbols and one key does something not
6074 expected (i.e. she/he then tries the other).
6075 The cases where Backspace/Delete is mapped to some other key combination
6076 are rare, and in those cases, normal-erase-is-backspace can be turned on
6077 manually. */
6078
6079 have_keys = Qnil;
6080 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
6081 if (kb)
6082 {
6083 int delete_keycode = 0, backspace_keycode = 0, i;
6084
6085 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
6086 {
6087 for (i = kb->min_key_code;
6088 (i < kb->max_key_code
6089 && (delete_keycode == 0 || backspace_keycode == 0));
6090 ++i)
6091 {
6092 /* The XKB symbolic key names can be seen most easily in
6093 the PS file generated by `xkbprint -label name
6094 $DISPLAY'. */
6095 if (memcmp ("DELE", kb->names->keys[i].name, 4) == 0)
6096 delete_keycode = i;
6097 else if (memcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
6098 backspace_keycode = i;
6099 }
6100
6101 XkbFreeNames (kb, 0, True);
6102 }
6103
6104 /* As of libX11-1.6.2, XkbGetMap manual says that you should use
6105 XkbFreeClientMap to free the data returned by XkbGetMap. But
6106 this function just frees the data referenced from KB and not
6107 KB itself. To free KB as well, call XkbFreeKeyboard. */
6108 XkbFreeKeyboard (kb, XkbAllMapComponentsMask, True);
6109
6110 if (delete_keycode
6111 && backspace_keycode
6112 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
6113 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
6114 have_keys = Qt;
6115 }
6116 unblock_input ();
6117 return have_keys;
6118 #endif
6119 }
6120
6121
6122 \f
6123 /***********************************************************************
6124 Initialization
6125 ***********************************************************************/
6126
6127 /* Keep this list in the same order as frame_parms in frame.c.
6128 Use 0 for unsupported frame parameters. */
6129
6130 frame_parm_handler x_frame_parm_handlers[] =
6131 {
6132 x_set_autoraise,
6133 x_set_autolower,
6134 x_set_background_color,
6135 x_set_border_color,
6136 x_set_border_width,
6137 x_set_cursor_color,
6138 x_set_cursor_type,
6139 x_set_font,
6140 x_set_foreground_color,
6141 x_set_icon_name,
6142 x_set_icon_type,
6143 x_set_internal_border_width,
6144 x_set_right_divider_width,
6145 x_set_bottom_divider_width,
6146 x_set_menu_bar_lines,
6147 x_set_mouse_color,
6148 x_explicitly_set_name,
6149 x_set_scroll_bar_width,
6150 x_set_scroll_bar_height,
6151 x_set_title,
6152 x_set_unsplittable,
6153 x_set_vertical_scroll_bars,
6154 x_set_horizontal_scroll_bars,
6155 x_set_visibility,
6156 x_set_tool_bar_lines,
6157 x_set_scroll_bar_foreground,
6158 x_set_scroll_bar_background,
6159 x_set_screen_gamma,
6160 x_set_line_spacing,
6161 x_set_left_fringe,
6162 x_set_right_fringe,
6163 x_set_wait_for_wm,
6164 x_set_fullscreen,
6165 x_set_font_backend,
6166 x_set_alpha,
6167 x_set_sticky,
6168 x_set_tool_bar_position,
6169 };
6170
6171 void
6172 syms_of_xfns (void)
6173 {
6174 DEFSYM (Qundefined_color, "undefined-color");
6175 DEFSYM (Qcompound_text, "compound-text");
6176 DEFSYM (Qcancel_timer, "cancel-timer");
6177 DEFSYM (Qfont_param, "font-parameter");
6178
6179 Fput (Qundefined_color, Qerror_conditions,
6180 listn (CONSTYPE_PURE, 2, Qundefined_color, Qerror));
6181 Fput (Qundefined_color, Qerror_message,
6182 build_pure_c_string ("Undefined color"));
6183
6184 DEFVAR_LISP ("x-pointer-shape", Vx_pointer_shape,
6185 doc: /* The shape of the pointer when over text.
6186 Changing the value does not affect existing frames
6187 unless you set the mouse color. */);
6188 Vx_pointer_shape = Qnil;
6189
6190 #if false /* This doesn't really do anything. */
6191 DEFVAR_LISP ("x-nontext-pointer-shape", Vx_nontext_pointer_shape,
6192 doc: /* The shape of the pointer when not over text.
6193 This variable takes effect when you create a new frame
6194 or when you set the mouse color. */);
6195 #endif
6196 Vx_nontext_pointer_shape = Qnil;
6197
6198 DEFVAR_LISP ("x-hourglass-pointer-shape", Vx_hourglass_pointer_shape,
6199 doc: /* The shape of the pointer when Emacs is busy.
6200 This variable takes effect when you create a new frame
6201 or when you set the mouse color. */);
6202 Vx_hourglass_pointer_shape = Qnil;
6203
6204 #if false /* This doesn't really do anything. */
6205 DEFVAR_LISP ("x-mode-pointer-shape", Vx_mode_pointer_shape,
6206 doc: /* The shape of the pointer when over the mode line.
6207 This variable takes effect when you create a new frame
6208 or when you set the mouse color. */);
6209 #endif
6210 Vx_mode_pointer_shape = Qnil;
6211
6212 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
6213 Vx_sensitive_text_pointer_shape,
6214 doc: /* The shape of the pointer when over mouse-sensitive text.
6215 This variable takes effect when you create a new frame
6216 or when you set the mouse color. */);
6217 Vx_sensitive_text_pointer_shape = Qnil;
6218
6219 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
6220 Vx_window_horizontal_drag_shape,
6221 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
6222 This variable takes effect when you create a new frame
6223 or when you set the mouse color. */);
6224 Vx_window_horizontal_drag_shape = Qnil;
6225
6226 DEFVAR_LISP ("x-window-vertical-drag-cursor",
6227 Vx_window_vertical_drag_shape,
6228 doc: /* Pointer shape to use for indicating a window can be dragged vertically.
6229 This variable takes effect when you create a new frame
6230 or when you set the mouse color. */);
6231 Vx_window_vertical_drag_shape = Qnil;
6232
6233 DEFVAR_LISP ("x-cursor-fore-pixel", Vx_cursor_fore_pixel,
6234 doc: /* A string indicating the foreground color of the cursor box. */);
6235 Vx_cursor_fore_pixel = Qnil;
6236
6237 DEFVAR_LISP ("x-max-tooltip-size", Vx_max_tooltip_size,
6238 doc: /* Maximum size for tooltips.
6239 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
6240 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
6241
6242 DEFVAR_LISP ("x-no-window-manager", Vx_no_window_manager,
6243 doc: /* Non-nil if no X window manager is in use.
6244 Emacs doesn't try to figure this out; this is always nil
6245 unless you set it to something else. */);
6246 /* We don't have any way to find this out, so set it to nil
6247 and maybe the user would like to set it to t. */
6248 Vx_no_window_manager = Qnil;
6249
6250 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
6251 Vx_pixel_size_width_font_regexp,
6252 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
6253
6254 Since Emacs gets width of a font matching with this regexp from
6255 PIXEL_SIZE field of the name, font finding mechanism gets faster for
6256 such a font. This is especially effective for such large fonts as
6257 Chinese, Japanese, and Korean. */);
6258 Vx_pixel_size_width_font_regexp = Qnil;
6259
6260 /* This is not ifdef:ed, so other builds than GTK can customize it. */
6261 DEFVAR_BOOL ("x-gtk-use-old-file-dialog", x_gtk_use_old_file_dialog,
6262 doc: /* Non-nil means prompt with the old GTK file selection dialog.
6263 If nil or if the file selection dialog is not available, the new GTK file
6264 chooser is used instead. To turn off all file dialogs set the
6265 variable `use-file-dialog'. */);
6266 x_gtk_use_old_file_dialog = false;
6267
6268 DEFVAR_BOOL ("x-gtk-show-hidden-files", x_gtk_show_hidden_files,
6269 doc: /* If non-nil, the GTK file chooser will by default show hidden files.
6270 Note that this is just the default, there is a toggle button on the file
6271 chooser to show or not show hidden files on a case by case basis. */);
6272 x_gtk_show_hidden_files = false;
6273
6274 DEFVAR_BOOL ("x-gtk-file-dialog-help-text", x_gtk_file_dialog_help_text,
6275 doc: /* If non-nil, the GTK file chooser will show additional help text.
6276 If more space for files in the file chooser dialog is wanted, set this to nil
6277 to turn the additional text off. */);
6278 x_gtk_file_dialog_help_text = true;
6279
6280 DEFVAR_BOOL ("x-gtk-use-system-tooltips", x_gtk_use_system_tooltips,
6281 doc: /* If non-nil with a Gtk+ built Emacs, the Gtk+ tooltip is used.
6282 Otherwise use Emacs own tooltip implementation.
6283 When using Gtk+ tooltips, the tooltip face is not used. */);
6284 x_gtk_use_system_tooltips = true;
6285
6286 /* Tell Emacs about this window system. */
6287 Fprovide (Qx, Qnil);
6288
6289 #ifdef USE_X_TOOLKIT
6290 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6291 #ifdef USE_MOTIF
6292 Fprovide (intern_c_string ("motif"), Qnil);
6293
6294 DEFVAR_LISP ("motif-version-string", Vmotif_version_string,
6295 doc: /* Version info for LessTif/Motif. */);
6296 Vmotif_version_string = build_string (XmVERSION_STRING);
6297 #endif /* USE_MOTIF */
6298 #endif /* USE_X_TOOLKIT */
6299
6300 #ifdef USE_GTK
6301 /* Provide x-toolkit also for GTK. Internally GTK does not use Xt so it
6302 is not an X toolkit in that sense (USE_X_TOOLKIT is not defined).
6303 But for a user it is a toolkit for X, and indeed, configure
6304 accepts --with-x-toolkit=gtk. */
6305 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6306 Fprovide (intern_c_string ("gtk"), Qnil);
6307 Fprovide (intern_c_string ("move-toolbar"), Qnil);
6308
6309 DEFVAR_LISP ("gtk-version-string", Vgtk_version_string,
6310 doc: /* Version info for GTK+. */);
6311 {
6312 char gtk_version[sizeof ".." + 3 * INT_STRLEN_BOUND (int)];
6313 int len = sprintf (gtk_version, "%d.%d.%d",
6314 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
6315 Vgtk_version_string = make_pure_string (gtk_version, len, len, false);
6316 }
6317 #endif /* USE_GTK */
6318
6319 /* X window properties. */
6320 defsubr (&Sx_change_window_property);
6321 defsubr (&Sx_delete_window_property);
6322 defsubr (&Sx_window_property);
6323
6324 defsubr (&Sxw_display_color_p);
6325 defsubr (&Sx_display_grayscale_p);
6326 defsubr (&Sxw_color_defined_p);
6327 defsubr (&Sxw_color_values);
6328 defsubr (&Sx_server_max_request_size);
6329 defsubr (&Sx_server_vendor);
6330 defsubr (&Sx_server_version);
6331 defsubr (&Sx_display_pixel_width);
6332 defsubr (&Sx_display_pixel_height);
6333 defsubr (&Sx_display_mm_width);
6334 defsubr (&Sx_display_mm_height);
6335 defsubr (&Sx_display_screens);
6336 defsubr (&Sx_display_planes);
6337 defsubr (&Sx_display_color_cells);
6338 defsubr (&Sx_display_visual_class);
6339 defsubr (&Sx_display_backing_store);
6340 defsubr (&Sx_display_save_under);
6341 defsubr (&Sx_display_monitor_attributes_list);
6342 defsubr (&Sx_frame_geometry);
6343 defsubr (&Sx_wm_set_size_hint);
6344 defsubr (&Sx_create_frame);
6345 defsubr (&Sx_open_connection);
6346 defsubr (&Sx_close_connection);
6347 defsubr (&Sx_display_list);
6348 defsubr (&Sx_synchronize);
6349 defsubr (&Sx_backspace_delete_keys_p);
6350
6351 defsubr (&Sx_show_tip);
6352 defsubr (&Sx_hide_tip);
6353 tip_timer = Qnil;
6354 staticpro (&tip_timer);
6355 tip_frame = Qnil;
6356 staticpro (&tip_frame);
6357
6358 last_show_tip_args = Qnil;
6359 staticpro (&last_show_tip_args);
6360
6361 defsubr (&Sx_uses_old_gtk_dialog);
6362 #if defined (USE_MOTIF) || defined (USE_GTK)
6363 defsubr (&Sx_file_dialog);
6364 #endif
6365
6366 #if defined (USE_GTK) && defined (HAVE_FREETYPE)
6367 defsubr (&Sx_select_font);
6368 #endif
6369 }