]> code.delx.au - gnu-emacs/blob - src/xfns.c
Use bool, not int, to track face changes
[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 GUI software on TERMINAL.
3546
3547 \(Labeling every distributor as a "vendor" embodies the false assumption
3548 that operating systems cannot be developed and distributed noncommercially.)
3549 The optional argument TERMINAL specifies which display to ask about.
3550
3551 For GNU and Unix systems, this queries the X server software; for
3552 MS-Windows, this queries the OS.
3553
3554 TERMINAL should be a terminal object, a frame or a display name (a string).
3555 If omitted or nil, that stands for the selected frame's display. */)
3556 (Lisp_Object terminal)
3557 {
3558 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3559 const char *vendor = ServerVendor (dpyinfo->display);
3560
3561 if (! vendor) vendor = "";
3562 return build_string (vendor);
3563 }
3564
3565 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
3566 doc: /* Return the version numbers of the GUI software on TERMINAL.
3567 The value is a list of three integers specifying the version of the GUI
3568 software in use.
3569
3570 For GNU and Unix system, the first 2 numbers are the version of the X
3571 Protocol used on TERMINAL and the 3rd number is the distributor-specific
3572 release number. For MS-Windows, the 3 numbers report the version and
3573 the build number of the OS.
3574
3575 See also the function `x-server-vendor'.
3576
3577 The optional argument TERMINAL specifies which display to ask about.
3578 TERMINAL should be a terminal object, a frame or a display name (a string).
3579 If omitted or nil, that stands for the selected frame's display. */)
3580 (Lisp_Object terminal)
3581 {
3582 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3583 Display *dpy = dpyinfo->display;
3584
3585 return list3i (ProtocolVersion (dpy), ProtocolRevision (dpy),
3586 VendorRelease (dpy));
3587 }
3588
3589 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
3590 doc: /* Return the number of screens on the X server of display TERMINAL.
3591 The optional argument TERMINAL specifies which display to ask about.
3592 TERMINAL should be a terminal object, a frame or a display name (a string).
3593 If omitted or nil, that stands for the selected frame's display. */)
3594 (Lisp_Object terminal)
3595 {
3596 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3597
3598 return make_number (ScreenCount (dpyinfo->display));
3599 }
3600
3601 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
3602 doc: /* Return the height in millimeters of the X display TERMINAL.
3603 The optional argument TERMINAL specifies which display to ask about.
3604 TERMINAL should be a terminal object, a frame or a display name (a string).
3605 If omitted or nil, that stands for the selected frame's display.
3606
3607 On \"multi-monitor\" setups this refers to the height in millimeters for
3608 all physical monitors associated with TERMINAL. To get information
3609 for each physical monitor, use `display-monitor-attributes-list'. */)
3610 (Lisp_Object terminal)
3611 {
3612 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3613
3614 return make_number (HeightMMOfScreen (dpyinfo->screen));
3615 }
3616
3617 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
3618 doc: /* Return the width in millimeters of the X display TERMINAL.
3619 The optional argument TERMINAL specifies which display to ask about.
3620 TERMINAL should be a terminal object, a frame or a display name (a string).
3621 If omitted or nil, that stands for the selected frame's display.
3622
3623 On \"multi-monitor\" setups this refers to the width in millimeters for
3624 all physical monitors associated with TERMINAL. To get information
3625 for each physical monitor, use `display-monitor-attributes-list'. */)
3626 (Lisp_Object terminal)
3627 {
3628 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3629
3630 return make_number (WidthMMOfScreen (dpyinfo->screen));
3631 }
3632
3633 DEFUN ("x-display-backing-store", Fx_display_backing_store,
3634 Sx_display_backing_store, 0, 1, 0,
3635 doc: /* Return an indication of whether X display TERMINAL does backing store.
3636 The value may be `always', `when-mapped', or `not-useful'.
3637 The optional argument TERMINAL specifies which display to ask about.
3638 TERMINAL should be a terminal object, a frame or a display name (a string).
3639 If omitted or nil, that stands for the selected frame's display. */)
3640 (Lisp_Object terminal)
3641 {
3642 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3643 Lisp_Object result;
3644
3645 switch (DoesBackingStore (dpyinfo->screen))
3646 {
3647 case Always:
3648 result = intern ("always");
3649 break;
3650
3651 case WhenMapped:
3652 result = intern ("when-mapped");
3653 break;
3654
3655 case NotUseful:
3656 result = intern ("not-useful");
3657 break;
3658
3659 default:
3660 error ("Strange value for BackingStore parameter of screen");
3661 }
3662
3663 return result;
3664 }
3665
3666 DEFUN ("x-display-visual-class", Fx_display_visual_class,
3667 Sx_display_visual_class, 0, 1, 0,
3668 doc: /* Return the visual class of the X display TERMINAL.
3669 The value is one of the symbols `static-gray', `gray-scale',
3670 `static-color', `pseudo-color', `true-color', or `direct-color'.
3671
3672 The optional argument TERMINAL specifies which display to ask about.
3673 TERMINAL should a terminal object, a frame or a display name (a string).
3674 If omitted or nil, that stands for the selected frame's display. */)
3675 (Lisp_Object terminal)
3676 {
3677 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3678 Lisp_Object result;
3679
3680 switch (dpyinfo->visual->class)
3681 {
3682 case StaticGray:
3683 result = intern ("static-gray");
3684 break;
3685 case GrayScale:
3686 result = intern ("gray-scale");
3687 break;
3688 case StaticColor:
3689 result = intern ("static-color");
3690 break;
3691 case PseudoColor:
3692 result = intern ("pseudo-color");
3693 break;
3694 case TrueColor:
3695 result = intern ("true-color");
3696 break;
3697 case DirectColor:
3698 result = intern ("direct-color");
3699 break;
3700 default:
3701 error ("Display has an unknown visual class");
3702 }
3703
3704 return result;
3705 }
3706
3707 DEFUN ("x-display-save-under", Fx_display_save_under,
3708 Sx_display_save_under, 0, 1, 0,
3709 doc: /* Return t if the X display TERMINAL supports the save-under feature.
3710 The optional argument TERMINAL specifies which display to ask about.
3711 TERMINAL should be a terminal object, a frame or a display name (a string).
3712 If omitted or nil, that stands for the selected frame's display. */)
3713 (Lisp_Object terminal)
3714 {
3715 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3716
3717 if (DoesSaveUnders (dpyinfo->screen) == True)
3718 return Qt;
3719 else
3720 return Qnil;
3721 }
3722
3723 /* Store the geometry of the workarea on display DPYINFO into *RECT.
3724 Return false if and only if the workarea information cannot be
3725 obtained via the _NET_WORKAREA root window property. */
3726
3727 #if ! GTK_CHECK_VERSION (3, 4, 0)
3728 static bool
3729 x_get_net_workarea (struct x_display_info *dpyinfo, XRectangle *rect)
3730 {
3731 Display *dpy = dpyinfo->display;
3732 long offset, max_len;
3733 Atom target_type, actual_type;
3734 unsigned long actual_size, bytes_remaining;
3735 int rc, actual_format;
3736 unsigned char *tmp_data = NULL;
3737 bool result = false;
3738
3739 x_catch_errors (dpy);
3740 offset = 0;
3741 max_len = 1;
3742 target_type = XA_CARDINAL;
3743 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3744 dpyinfo->Xatom_net_current_desktop,
3745 offset, max_len, False, target_type,
3746 &actual_type, &actual_format, &actual_size,
3747 &bytes_remaining, &tmp_data);
3748 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3749 && actual_format == 32 && actual_size == max_len)
3750 {
3751 long current_desktop = ((long *) tmp_data)[0];
3752
3753 XFree (tmp_data);
3754 tmp_data = NULL;
3755
3756 offset = 4 * current_desktop;
3757 max_len = 4;
3758 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3759 dpyinfo->Xatom_net_workarea,
3760 offset, max_len, False, target_type,
3761 &actual_type, &actual_format, &actual_size,
3762 &bytes_remaining, &tmp_data);
3763 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3764 && actual_format == 32 && actual_size == max_len)
3765 {
3766 long *values = (long *) tmp_data;
3767
3768 rect->x = values[0];
3769 rect->y = values[1];
3770 rect->width = values[2];
3771 rect->height = values[3];
3772
3773 XFree (tmp_data);
3774 tmp_data = NULL;
3775
3776 result = true;
3777 }
3778 }
3779 if (tmp_data)
3780 XFree (tmp_data);
3781 x_uncatch_errors ();
3782
3783 return result;
3784 }
3785 #endif
3786
3787 #ifndef USE_GTK
3788
3789 /* Return monitor number where F is "most" or closest to. */
3790 static int
3791 x_get_monitor_for_frame (struct frame *f,
3792 struct MonitorInfo *monitors,
3793 int n_monitors)
3794 {
3795 XRectangle frect;
3796 int area = 0, dist = -1;
3797 int best_area = -1, best_dist = -1;
3798 int i;
3799
3800 if (n_monitors == 1) return 0;
3801 frect.x = f->left_pos;
3802 frect.y = f->top_pos;
3803 frect.width = FRAME_PIXEL_WIDTH (f);
3804 frect.height = FRAME_PIXEL_HEIGHT (f);
3805
3806 for (i = 0; i < n_monitors; ++i)
3807 {
3808 struct MonitorInfo *mi = &monitors[i];
3809 XRectangle res;
3810 int a = 0;
3811
3812 if (mi->geom.width == 0) continue;
3813
3814 if (x_intersect_rectangles (&mi->geom, &frect, &res))
3815 {
3816 a = res.width * res.height;
3817 if (a > area)
3818 {
3819 area = a;
3820 best_area = i;
3821 }
3822 }
3823
3824 if (a == 0 && area == 0)
3825 {
3826 int dx, dy, d;
3827 if (frect.x + frect.width < mi->geom.x)
3828 dx = mi->geom.x - frect.x + frect.width;
3829 else if (frect.x > mi->geom.x + mi->geom.width)
3830 dx = frect.x - mi->geom.x + mi->geom.width;
3831 else
3832 dx = 0;
3833 if (frect.y + frect.height < mi->geom.y)
3834 dy = mi->geom.y - frect.y + frect.height;
3835 else if (frect.y > mi->geom.y + mi->geom.height)
3836 dy = frect.y - mi->geom.y + mi->geom.height;
3837 else
3838 dy = 0;
3839
3840 d = dx*dx + dy*dy;
3841 if (dist == -1 || dist > d)
3842 {
3843 dist = d;
3844 best_dist = i;
3845 }
3846 }
3847 }
3848
3849 return best_area != -1 ? best_area : (best_dist != -1 ? best_dist : 0);
3850 }
3851
3852 static Lisp_Object
3853 x_make_monitor_attribute_list (struct MonitorInfo *monitors,
3854 int n_monitors,
3855 int primary_monitor,
3856 struct x_display_info *dpyinfo,
3857 const char *source)
3858 {
3859 Lisp_Object monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
3860 Lisp_Object frame, rest;
3861
3862 FOR_EACH_FRAME (rest, frame)
3863 {
3864 struct frame *f = XFRAME (frame);
3865
3866 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo
3867 && !EQ (frame, tip_frame))
3868 {
3869 int i = x_get_monitor_for_frame (f, monitors, n_monitors);
3870 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
3871 }
3872 }
3873
3874 return make_monitor_attribute_list (monitors, n_monitors, primary_monitor,
3875 monitor_frames, source);
3876 }
3877
3878 static Lisp_Object
3879 x_get_monitor_attributes_fallback (struct x_display_info *dpyinfo)
3880 {
3881 struct MonitorInfo monitor;
3882 XRectangle workarea_r;
3883
3884 /* Fallback: treat (possibly) multiple physical monitors as if they
3885 formed a single monitor as a whole. This should provide a
3886 consistent result at least on single monitor environments. */
3887 monitor.geom.x = monitor.geom.y = 0;
3888 monitor.geom.width = x_display_pixel_width (dpyinfo);
3889 monitor.geom.height = x_display_pixel_height (dpyinfo);
3890 monitor.mm_width = WidthMMOfScreen (dpyinfo->screen);
3891 monitor.mm_height = HeightMMOfScreen (dpyinfo->screen);
3892 monitor.name = xstrdup ("combined screen");
3893
3894 if (x_get_net_workarea (dpyinfo, &workarea_r))
3895 monitor.work = workarea_r;
3896 else
3897 monitor.work = monitor.geom;
3898 return x_make_monitor_attribute_list (&monitor, 1, 0, dpyinfo, "fallback");
3899 }
3900
3901
3902 #ifdef HAVE_XINERAMA
3903 static Lisp_Object
3904 x_get_monitor_attributes_xinerama (struct x_display_info *dpyinfo)
3905 {
3906 int n_monitors, i;
3907 Lisp_Object attributes_list = Qnil;
3908 Display *dpy = dpyinfo->display;
3909 XineramaScreenInfo *info = XineramaQueryScreens (dpy, &n_monitors);
3910 struct MonitorInfo *monitors;
3911 double mm_width_per_pixel, mm_height_per_pixel;
3912
3913 if (! info || n_monitors == 0)
3914 {
3915 if (info)
3916 XFree (info);
3917 return attributes_list;
3918 }
3919
3920 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
3921 / x_display_pixel_width (dpyinfo));
3922 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
3923 / x_display_pixel_height (dpyinfo));
3924 monitors = xzalloc (n_monitors * sizeof *monitors);
3925 for (i = 0; i < n_monitors; ++i)
3926 {
3927 struct MonitorInfo *mi = &monitors[i];
3928 XRectangle workarea_r;
3929
3930 mi->geom.x = info[i].x_org;
3931 mi->geom.y = info[i].y_org;
3932 mi->geom.width = info[i].width;
3933 mi->geom.height = info[i].height;
3934 mi->mm_width = mi->geom.width * mm_width_per_pixel + 0.5;
3935 mi->mm_height = mi->geom.height * mm_height_per_pixel + 0.5;
3936 mi->name = 0;
3937
3938 /* Xinerama usually have primary monitor first, just use that. */
3939 if (i == 0 && x_get_net_workarea (dpyinfo, &workarea_r))
3940 {
3941 mi->work = workarea_r;
3942 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
3943 mi->work = mi->geom;
3944 }
3945 else
3946 mi->work = mi->geom;
3947 }
3948 XFree (info);
3949
3950 attributes_list = x_make_monitor_attribute_list (monitors,
3951 n_monitors,
3952 0,
3953 dpyinfo,
3954 "Xinerama");
3955 free_monitors (monitors, n_monitors);
3956 return attributes_list;
3957 }
3958 #endif /* HAVE_XINERAMA */
3959
3960
3961 #ifdef HAVE_XRANDR
3962 static Lisp_Object
3963 x_get_monitor_attributes_xrandr (struct x_display_info *dpyinfo)
3964 {
3965 Lisp_Object attributes_list = Qnil;
3966 XRRScreenResources *resources;
3967 Display *dpy = dpyinfo->display;
3968 int i, n_monitors, primary = -1;
3969 RROutput pxid = None;
3970 struct MonitorInfo *monitors;
3971
3972 #ifdef HAVE_XRRGETSCREENRESOURCESCURRENT
3973 resources = XRRGetScreenResourcesCurrent (dpy, dpyinfo->root_window);
3974 #else
3975 resources = XRRGetScreenResources (dpy, dpyinfo->root_window);
3976 #endif
3977 if (! resources || resources->noutput == 0)
3978 {
3979 if (resources)
3980 XRRFreeScreenResources (resources);
3981 return Qnil;
3982 }
3983 n_monitors = resources->noutput;
3984 monitors = xzalloc (n_monitors * sizeof *monitors);
3985
3986 #ifdef HAVE_XRRGETOUTPUTPRIMARY
3987 pxid = XRRGetOutputPrimary (dpy, dpyinfo->root_window);
3988 #endif
3989
3990 for (i = 0; i < n_monitors; ++i)
3991 {
3992 XRROutputInfo *info = XRRGetOutputInfo (dpy, resources,
3993 resources->outputs[i]);
3994 Connection conn = info ? info->connection : RR_Disconnected;
3995 RRCrtc id = info ? info->crtc : None;
3996
3997 if (strcmp (info->name, "default") == 0)
3998 {
3999 /* Non XRandr 1.2 driver, does not give useful data. */
4000 XRRFreeOutputInfo (info);
4001 XRRFreeScreenResources (resources);
4002 free_monitors (monitors, n_monitors);
4003 return Qnil;
4004 }
4005
4006 if (conn != RR_Disconnected && id != None)
4007 {
4008 XRRCrtcInfo *crtc = XRRGetCrtcInfo (dpy, resources, id);
4009 struct MonitorInfo *mi = &monitors[i];
4010 XRectangle workarea_r;
4011
4012 if (! crtc)
4013 {
4014 XRRFreeOutputInfo (info);
4015 continue;
4016 }
4017
4018 mi->geom.x = crtc->x;
4019 mi->geom.y = crtc->y;
4020 mi->geom.width = crtc->width;
4021 mi->geom.height = crtc->height;
4022 mi->mm_width = info->mm_width;
4023 mi->mm_height = info->mm_height;
4024 mi->name = xstrdup (info->name);
4025
4026 if (pxid != None && pxid == resources->outputs[i])
4027 primary = i;
4028 else if (primary == -1 && strcmp (info->name, "LVDS") == 0)
4029 primary = i;
4030
4031 if (i == primary && x_get_net_workarea (dpyinfo, &workarea_r))
4032 {
4033 mi->work= workarea_r;
4034 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
4035 mi->work = mi->geom;
4036 }
4037 else
4038 mi->work = mi->geom;
4039
4040 XRRFreeCrtcInfo (crtc);
4041 }
4042 XRRFreeOutputInfo (info);
4043 }
4044 XRRFreeScreenResources (resources);
4045
4046 attributes_list = x_make_monitor_attribute_list (monitors,
4047 n_monitors,
4048 primary,
4049 dpyinfo,
4050 "XRandr");
4051 free_monitors (monitors, n_monitors);
4052 return attributes_list;
4053 }
4054 #endif /* HAVE_XRANDR */
4055
4056 static Lisp_Object
4057 x_get_monitor_attributes (struct x_display_info *dpyinfo)
4058 {
4059 Lisp_Object attributes_list = Qnil;
4060 Display *dpy = dpyinfo->display;
4061
4062 (void) dpy; /* Suppress unused variable warning. */
4063
4064 #ifdef HAVE_XRANDR
4065 int xrr_event_base, xrr_error_base;
4066 bool xrr_ok = false;
4067 xrr_ok = XRRQueryExtension (dpy, &xrr_event_base, &xrr_error_base);
4068 if (xrr_ok)
4069 {
4070 int xrr_major, xrr_minor;
4071 XRRQueryVersion (dpy, &xrr_major, &xrr_minor);
4072 xrr_ok = (xrr_major == 1 && xrr_minor >= 2) || xrr_major > 1;
4073 }
4074
4075 if (xrr_ok)
4076 attributes_list = x_get_monitor_attributes_xrandr (dpyinfo);
4077 #endif /* HAVE_XRANDR */
4078
4079 #ifdef HAVE_XINERAMA
4080 if (NILP (attributes_list))
4081 {
4082 int xin_event_base, xin_error_base;
4083 bool xin_ok = false;
4084 xin_ok = XineramaQueryExtension (dpy, &xin_event_base, &xin_error_base);
4085 if (xin_ok && XineramaIsActive (dpy))
4086 attributes_list = x_get_monitor_attributes_xinerama (dpyinfo);
4087 }
4088 #endif /* HAVE_XINERAMA */
4089
4090 if (NILP (attributes_list))
4091 attributes_list = x_get_monitor_attributes_fallback (dpyinfo);
4092
4093 return attributes_list;
4094 }
4095
4096 #endif /* !USE_GTK */
4097
4098 DEFUN ("x-display-monitor-attributes-list", Fx_display_monitor_attributes_list,
4099 Sx_display_monitor_attributes_list,
4100 0, 1, 0,
4101 doc: /* Return a list of physical monitor attributes on the X display TERMINAL.
4102
4103 The optional argument TERMINAL specifies which display to ask about.
4104 TERMINAL should be a terminal object, a frame or a display name (a string).
4105 If omitted or nil, that stands for the selected frame's display.
4106
4107 In addition to the standard attribute keys listed in
4108 `display-monitor-attributes-list', the following keys are contained in
4109 the attributes:
4110
4111 source -- String describing the source from which multi-monitor
4112 information is obtained, one of \"Gdk\", \"XRandr\",
4113 \"Xinerama\", or \"fallback\"
4114
4115 Internal use only, use `display-monitor-attributes-list' instead. */)
4116 (Lisp_Object terminal)
4117 {
4118 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4119 Lisp_Object attributes_list = Qnil;
4120
4121 #ifdef USE_GTK
4122 double mm_width_per_pixel, mm_height_per_pixel;
4123 GdkDisplay *gdpy;
4124 GdkScreen *gscreen;
4125 gint primary_monitor = 0, n_monitors, i;
4126 Lisp_Object monitor_frames, rest, frame;
4127 static const char *source = "Gdk";
4128 struct MonitorInfo *monitors;
4129
4130 block_input ();
4131 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
4132 / x_display_pixel_width (dpyinfo));
4133 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
4134 / x_display_pixel_height (dpyinfo));
4135 gdpy = gdk_x11_lookup_xdisplay (dpyinfo->display);
4136 gscreen = gdk_display_get_default_screen (gdpy);
4137 #if GTK_CHECK_VERSION (2, 20, 0)
4138 primary_monitor = gdk_screen_get_primary_monitor (gscreen);
4139 #endif
4140 n_monitors = gdk_screen_get_n_monitors (gscreen);
4141 monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
4142 monitors = xzalloc (n_monitors * sizeof *monitors);
4143
4144 FOR_EACH_FRAME (rest, frame)
4145 {
4146 struct frame *f = XFRAME (frame);
4147
4148 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo
4149 && !EQ (frame, tip_frame))
4150 {
4151 GdkWindow *gwin = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
4152
4153 i = gdk_screen_get_monitor_at_window (gscreen, gwin);
4154 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
4155 }
4156 }
4157
4158 for (i = 0; i < n_monitors; ++i)
4159 {
4160 gint width_mm = -1, height_mm = -1;
4161 GdkRectangle rec, work;
4162 struct MonitorInfo *mi = &monitors[i];
4163
4164 gdk_screen_get_monitor_geometry (gscreen, i, &rec);
4165
4166 #if GTK_CHECK_VERSION (2, 14, 0)
4167 width_mm = gdk_screen_get_monitor_width_mm (gscreen, i);
4168 height_mm = gdk_screen_get_monitor_height_mm (gscreen, i);
4169 #endif
4170 if (width_mm < 0)
4171 width_mm = rec.width * mm_width_per_pixel + 0.5;
4172 if (height_mm < 0)
4173 height_mm = rec.height * mm_height_per_pixel + 0.5;
4174
4175 #if GTK_CHECK_VERSION (3, 4, 0)
4176 gdk_screen_get_monitor_workarea (gscreen, i, &work);
4177 #else
4178 /* Emulate the behavior of GTK+ 3.4. */
4179 {
4180 XRectangle workarea_r;
4181
4182 if (i == primary_monitor && x_get_net_workarea (dpyinfo, &workarea_r))
4183 {
4184 work.x = workarea_r.x;
4185 work.y = workarea_r.y;
4186 work.width = workarea_r.width;
4187 work.height = workarea_r.height;
4188 if (! gdk_rectangle_intersect (&rec, &work, &work))
4189 work = rec;
4190 }
4191 else
4192 work = rec;
4193 }
4194 #endif
4195
4196
4197 mi->geom.x = rec.x;
4198 mi->geom.y = rec.y;
4199 mi->geom.width = rec.width;
4200 mi->geom.height = rec.height;
4201 mi->work.x = work.x;
4202 mi->work.y = work.y;
4203 mi->work.width = work.width;
4204 mi->work.height = work.height;
4205 mi->mm_width = width_mm;
4206 mi->mm_height = height_mm;
4207
4208 #if GTK_CHECK_VERSION (2, 14, 0)
4209 mi->name = gdk_screen_get_monitor_plug_name (gscreen, i);
4210 #endif
4211 }
4212
4213 attributes_list = make_monitor_attribute_list (monitors,
4214 n_monitors,
4215 primary_monitor,
4216 monitor_frames,
4217 source);
4218 unblock_input ();
4219 #else /* not USE_GTK */
4220
4221 block_input ();
4222 attributes_list = x_get_monitor_attributes (dpyinfo);
4223 unblock_input ();
4224
4225 #endif /* not USE_GTK */
4226
4227 return attributes_list;
4228 }
4229
4230 DEFUN ("x-frame-geometry", Fx_frame_geometry, Sx_frame_geometry, 0, 1, 0,
4231 doc: /* Return geometric attributes of frame FRAME.
4232
4233 FRAME must be a live frame and defaults to the selected one.
4234
4235 The return value is an association list containing the following
4236 elements (all size values are in pixels).
4237
4238 - `frame-outer-size' is a cons of the outer width and height of FRAME.
4239 The outer size include the title bar and the external borders as well
4240 as any menu and/or tool bar of frame.
4241
4242 - `border' is a cons of the horizontal and vertical width of FRAME's
4243 external borders.
4244
4245 - `title-bar-height' is the height of the title bar of FRAME.
4246
4247 - `menu-bar-external' if `t' means the menu bar is external (not
4248 included in the inner edges of FRAME).
4249
4250 - `menu-bar-size' is a cons of the width and height of the menu bar of
4251 FRAME.
4252
4253 - `tool-bar-external' if `t' means the tool bar is external (not
4254 included in the inner edges of FRAME).
4255
4256 - `tool-bar-side' tells tells on which side the tool bar on FRAME is and
4257 can be one of `left', `top', `right' or `bottom'.
4258
4259 - `tool-bar-size' is a cons of the width and height of the tool bar of
4260 FRAME.
4261
4262 - `frame-inner-size' is a cons of the inner width and height of FRAME.
4263 This excludes FRAME's title bar and external border as well as any
4264 external menu and/or tool bar. */)
4265 (Lisp_Object frame)
4266 {
4267 struct frame *f = decode_live_frame (frame);
4268 int inner_width = FRAME_PIXEL_WIDTH (f);
4269 int inner_height = FRAME_PIXEL_HEIGHT (f);
4270 int outer_width, outer_height, border, title;
4271 Lisp_Object fullscreen = Fframe_parameter (frame, Qfullscreen);
4272 int menu_bar_height, menu_bar_width, tool_bar_height, tool_bar_width;
4273
4274 border = FRAME_OUTER_TO_INNER_DIFF_X (f);
4275 title = FRAME_X_OUTPUT (f)->y_pixels_outer_diff - border;
4276
4277 outer_width = FRAME_PIXEL_WIDTH (f) + 2 * border;
4278 outer_height = (FRAME_PIXEL_HEIGHT (f)
4279 + FRAME_OUTER_TO_INNER_DIFF_Y (f)
4280 + FRAME_OUTER_TO_INNER_DIFF_X (f));
4281
4282 #if defined (USE_GTK)
4283 {
4284 bool tool_bar_left_right = (EQ (FRAME_TOOL_BAR_POSITION (f), Qleft)
4285 || EQ (FRAME_TOOL_BAR_POSITION (f), Qright));
4286
4287 tool_bar_width = (tool_bar_left_right
4288 ? FRAME_TOOLBAR_WIDTH (f)
4289 : FRAME_PIXEL_WIDTH (f));
4290 tool_bar_height = (tool_bar_left_right
4291 ? FRAME_PIXEL_HEIGHT (f)
4292 : FRAME_TOOLBAR_HEIGHT (f));
4293 if (tool_bar_left_right)
4294 /* For some reason FRAME_OUTER_TO_INNER_DIFF_X does not count the
4295 width of a tool bar. */
4296 outer_width += FRAME_TOOLBAR_WIDTH (f);
4297 }
4298 #else
4299 tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f);
4300 tool_bar_width = ((tool_bar_height > 0)
4301 ? outer_width - 2 * FRAME_INTERNAL_BORDER_WIDTH (f)
4302 : 0);
4303 #endif
4304
4305 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
4306 menu_bar_height = FRAME_MENUBAR_HEIGHT (f);
4307 #else
4308 menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
4309 #endif
4310
4311 menu_bar_width = ((menu_bar_height > 0)
4312 ? outer_width - 2 * border
4313 : 0);
4314
4315 if (!FRAME_EXTERNAL_MENU_BAR (f))
4316 inner_height -= menu_bar_height;
4317 if (!FRAME_EXTERNAL_TOOL_BAR (f))
4318 inner_height -= tool_bar_height;
4319
4320 return
4321 listn (CONSTYPE_PURE, 10,
4322 Fcons (Qframe_position,
4323 Fcons (make_number (f->left_pos), make_number (f->top_pos))),
4324 Fcons (Qframe_outer_size,
4325 Fcons (make_number (outer_width), make_number (outer_height))),
4326 Fcons (Qexternal_border_size,
4327 ((EQ (fullscreen, Qfullboth) || EQ (fullscreen, Qfullscreen))
4328 ? Fcons (make_number (0), make_number (0))
4329 : Fcons (make_number (border), make_number (border)))),
4330 Fcons (Qtitle_height,
4331 ((EQ (fullscreen, Qfullboth) || EQ (fullscreen, Qfullscreen))
4332 ? make_number (0)
4333 : make_number (title))),
4334 Fcons (Qmenu_bar_external, FRAME_EXTERNAL_MENU_BAR (f) ? Qt : Qnil),
4335 Fcons (Qmenu_bar_size,
4336 Fcons (make_number (menu_bar_width),
4337 make_number (menu_bar_height))),
4338 Fcons (Qtool_bar_external, FRAME_EXTERNAL_TOOL_BAR (f) ? Qt : Qnil),
4339 Fcons (Qtool_bar_position, FRAME_TOOL_BAR_POSITION (f)),
4340 Fcons (Qtool_bar_size,
4341 Fcons (make_number (tool_bar_width),
4342 make_number (tool_bar_height))),
4343 Fcons (Qframe_inner_size,
4344 Fcons (make_number (inner_width),
4345 make_number (inner_height))));
4346 }
4347
4348 /************************************************************************
4349 X Displays
4350 ************************************************************************/
4351
4352 \f
4353 /* Mapping visual names to visuals. */
4354
4355 static struct visual_class
4356 {
4357 const char *name;
4358 int class;
4359 }
4360 visual_classes[] =
4361 {
4362 {"StaticGray", StaticGray},
4363 {"GrayScale", GrayScale},
4364 {"StaticColor", StaticColor},
4365 {"PseudoColor", PseudoColor},
4366 {"TrueColor", TrueColor},
4367 {"DirectColor", DirectColor},
4368 {NULL, 0}
4369 };
4370
4371
4372 #ifndef HAVE_XSCREENNUMBEROFSCREEN
4373
4374 /* Value is the screen number of screen SCR. This is a substitute for
4375 the X function with the same name when that doesn't exist. */
4376
4377 int
4378 XScreenNumberOfScreen (scr)
4379 register Screen *scr;
4380 {
4381 Display *dpy = scr->display;
4382 int i;
4383
4384 for (i = 0; i < dpy->nscreens; ++i)
4385 if (scr == dpy->screens + i)
4386 break;
4387
4388 return i;
4389 }
4390
4391 #endif /* not HAVE_XSCREENNUMBEROFSCREEN */
4392
4393
4394 /* Select the visual that should be used on display DPYINFO. Set
4395 members of DPYINFO appropriately. Called from x_term_init. */
4396
4397 void
4398 select_visual (struct x_display_info *dpyinfo)
4399 {
4400 Display *dpy = dpyinfo->display;
4401 Screen *screen = dpyinfo->screen;
4402
4403 /* See if a visual is specified. */
4404 AUTO_STRING (visualClass, "visualClass");
4405 AUTO_STRING (VisualClass, "VisualClass");
4406 Lisp_Object value = display_x_get_resource (dpyinfo, visualClass,
4407 VisualClass, Qnil, Qnil);
4408
4409 if (STRINGP (value))
4410 {
4411 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
4412 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
4413 depth, a decimal number. NAME is compared with case ignored. */
4414 char *s = alloca (SBYTES (value) + 1);
4415 char *dash;
4416 int i, class = -1;
4417 XVisualInfo vinfo;
4418
4419 lispstpcpy (s, value);
4420 dash = strchr (s, '-');
4421 if (dash)
4422 {
4423 dpyinfo->n_planes = atoi (dash + 1);
4424 *dash = '\0';
4425 }
4426 else
4427 /* We won't find a matching visual with depth 0, so that
4428 an error will be printed below. */
4429 dpyinfo->n_planes = 0;
4430
4431 /* Determine the visual class. */
4432 for (i = 0; visual_classes[i].name; ++i)
4433 if (xstrcasecmp (s, visual_classes[i].name) == 0)
4434 {
4435 class = visual_classes[i].class;
4436 break;
4437 }
4438
4439 /* Look up a matching visual for the specified class. */
4440 if (class == -1
4441 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
4442 dpyinfo->n_planes, class, &vinfo))
4443 fatal ("Invalid visual specification `%s'", SDATA (value));
4444
4445 dpyinfo->visual = vinfo.visual;
4446 }
4447 else
4448 {
4449 int n_visuals;
4450 XVisualInfo *vinfo, vinfo_template;
4451
4452 dpyinfo->visual = DefaultVisualOfScreen (screen);
4453
4454 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
4455 vinfo_template.screen = XScreenNumberOfScreen (screen);
4456 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
4457 &vinfo_template, &n_visuals);
4458 if (n_visuals <= 0)
4459 fatal ("Can't get proper X visual info");
4460
4461 dpyinfo->n_planes = vinfo->depth;
4462 XFree (vinfo);
4463 }
4464 }
4465
4466
4467 /* Return the X display structure for the display named NAME.
4468 Open a new connection if necessary. */
4469
4470 static struct x_display_info *
4471 x_display_info_for_name (Lisp_Object name)
4472 {
4473 struct x_display_info *dpyinfo;
4474
4475 CHECK_STRING (name);
4476
4477 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
4478 if (!NILP (Fstring_equal (XCAR (dpyinfo->name_list_element), name)))
4479 return dpyinfo;
4480
4481 /* Use this general default value to start with. */
4482 Vx_resource_name = Vinvocation_name;
4483
4484 validate_x_resource_name ();
4485
4486 dpyinfo = x_term_init (name, 0, SSDATA (Vx_resource_name));
4487
4488 if (dpyinfo == 0)
4489 error ("Cannot connect to X server %s", SDATA (name));
4490
4491 XSETFASTINT (Vwindow_system_version, 11);
4492
4493 return dpyinfo;
4494 }
4495
4496
4497 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
4498 1, 3, 0,
4499 doc: /* Open a connection to a display server.
4500 DISPLAY is the name of the display to connect to.
4501 Optional second arg XRM-STRING is a string of resources in xrdb format.
4502 If the optional third arg MUST-SUCCEED is non-nil,
4503 terminate Emacs if we can't open the connection.
4504 \(In the Nextstep version, the last two arguments are currently ignored.) */)
4505 (Lisp_Object display, Lisp_Object xrm_string, Lisp_Object must_succeed)
4506 {
4507 char *xrm_option;
4508 struct x_display_info *dpyinfo;
4509
4510 CHECK_STRING (display);
4511 if (! NILP (xrm_string))
4512 CHECK_STRING (xrm_string);
4513
4514 xrm_option = NILP (xrm_string) ? 0 : SSDATA (xrm_string);
4515
4516 validate_x_resource_name ();
4517
4518 /* This is what opens the connection and sets x_current_display.
4519 This also initializes many symbols, such as those used for input. */
4520 dpyinfo = x_term_init (display, xrm_option,
4521 SSDATA (Vx_resource_name));
4522
4523 if (dpyinfo == 0)
4524 {
4525 if (!NILP (must_succeed))
4526 fatal ("Cannot connect to X server %s.\n\
4527 Check the DISPLAY environment variable or use `-d'.\n\
4528 Also use the `xauth' program to verify that you have the proper\n\
4529 authorization information needed to connect the X server.\n\
4530 An insecure way to solve the problem may be to use `xhost'.\n",
4531 SDATA (display));
4532 else
4533 error ("Cannot connect to X server %s", SDATA (display));
4534 }
4535
4536 XSETFASTINT (Vwindow_system_version, 11);
4537 return Qnil;
4538 }
4539
4540 DEFUN ("x-close-connection", Fx_close_connection,
4541 Sx_close_connection, 1, 1, 0,
4542 doc: /* Close the connection to TERMINAL's X server.
4543 For TERMINAL, specify a terminal object, a frame or a display name (a
4544 string). If TERMINAL is nil, that stands for the selected frame's
4545 terminal. */)
4546 (Lisp_Object terminal)
4547 {
4548 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4549
4550 if (dpyinfo->reference_count > 0)
4551 error ("Display still has frames on it");
4552
4553 x_delete_terminal (dpyinfo->terminal);
4554
4555 return Qnil;
4556 }
4557
4558 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
4559 doc: /* Return the list of display names that Emacs has connections to. */)
4560 (void)
4561 {
4562 Lisp_Object result = Qnil;
4563 struct x_display_info *xdi;
4564
4565 for (xdi = x_display_list; xdi; xdi = xdi->next)
4566 result = Fcons (XCAR (xdi->name_list_element), result);
4567
4568 return result;
4569 }
4570
4571 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
4572 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
4573 This function only has an effect on X Windows. With MS Windows, it is
4574 defined but does nothing.
4575
4576 If ON is nil, allow buffering of requests.
4577 Turning on synchronization prohibits the Xlib routines from buffering
4578 requests and seriously degrades performance, but makes debugging much
4579 easier.
4580 The optional second argument TERMINAL specifies which display to act on.
4581 TERMINAL should be a terminal object, a frame or a display name (a string).
4582 If TERMINAL is omitted or nil, that stands for the selected frame's display. */)
4583 (Lisp_Object on, Lisp_Object terminal)
4584 {
4585 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4586
4587 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
4588
4589 return Qnil;
4590 }
4591
4592 /* Wait for responses to all X commands issued so far for frame F. */
4593
4594 void
4595 x_sync (struct frame *f)
4596 {
4597 block_input ();
4598 XSync (FRAME_X_DISPLAY (f), False);
4599 unblock_input ();
4600 }
4601
4602 \f
4603 /***********************************************************************
4604 Window properties
4605 ***********************************************************************/
4606
4607 DEFUN ("x-change-window-property", Fx_change_window_property,
4608 Sx_change_window_property, 2, 6, 0,
4609 doc: /* Change window property PROP to VALUE on the X window of FRAME.
4610 PROP must be a string. VALUE may be a string or a list of conses,
4611 numbers and/or strings. If an element in the list is a string, it is
4612 converted to an atom and the value of the atom is used. If an element
4613 is a cons, it is converted to a 32 bit number where the car is the 16
4614 top bits and the cdr is the lower 16 bits.
4615
4616 FRAME nil or omitted means use the selected frame.
4617 If TYPE is given and non-nil, it is the name of the type of VALUE.
4618 If TYPE is not given or nil, the type is STRING.
4619 FORMAT gives the size in bits of each element if VALUE is a list.
4620 It must be one of 8, 16 or 32.
4621 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
4622 If OUTER-P is non-nil, the property is changed for the outer X window of
4623 FRAME. Default is to change on the edit X window. */)
4624 (Lisp_Object prop, Lisp_Object value, Lisp_Object frame,
4625 Lisp_Object type, Lisp_Object format, Lisp_Object outer_p)
4626 {
4627 struct frame *f = decode_window_system_frame (frame);
4628 Atom prop_atom;
4629 Atom target_type = XA_STRING;
4630 int element_format = 8;
4631 unsigned char *data;
4632 int nelements;
4633 Window w;
4634
4635 CHECK_STRING (prop);
4636
4637 if (! NILP (format))
4638 {
4639 CHECK_NUMBER (format);
4640
4641 if (XINT (format) != 8 && XINT (format) != 16
4642 && XINT (format) != 32)
4643 error ("FORMAT must be one of 8, 16 or 32");
4644 element_format = XINT (format);
4645 }
4646
4647 if (CONSP (value))
4648 {
4649 ptrdiff_t elsize;
4650
4651 nelements = x_check_property_data (value);
4652 if (nelements == -1)
4653 error ("Bad data in VALUE, must be number, string or cons");
4654
4655 /* The man page for XChangeProperty:
4656 "If the specified format is 32, the property data must be a
4657 long array."
4658 This applies even if long is more than 32 bits. The X library
4659 converts to 32 bits before sending to the X server. */
4660 elsize = element_format == 32 ? sizeof (long) : element_format >> 3;
4661 data = xnmalloc (nelements, elsize);
4662
4663 x_fill_property_data (FRAME_X_DISPLAY (f), value, data, element_format);
4664 }
4665 else
4666 {
4667 CHECK_STRING (value);
4668 data = SDATA (value);
4669 if (INT_MAX < SBYTES (value))
4670 error ("VALUE too long");
4671 nelements = SBYTES (value);
4672 }
4673
4674 block_input ();
4675 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4676 if (! NILP (type))
4677 {
4678 CHECK_STRING (type);
4679 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4680 }
4681
4682 if (! NILP (outer_p)) w = FRAME_OUTER_WINDOW (f);
4683 else w = FRAME_X_WINDOW (f);
4684
4685 XChangeProperty (FRAME_X_DISPLAY (f), w,
4686 prop_atom, target_type, element_format, PropModeReplace,
4687 data, nelements);
4688
4689 if (CONSP (value)) xfree (data);
4690
4691 /* Make sure the property is set when we return. */
4692 XFlush (FRAME_X_DISPLAY (f));
4693 unblock_input ();
4694
4695 return value;
4696 }
4697
4698
4699 DEFUN ("x-delete-window-property", Fx_delete_window_property,
4700 Sx_delete_window_property, 1, 2, 0,
4701 doc: /* Remove window property PROP from X window of FRAME.
4702 FRAME nil or omitted means use the selected frame. Value is PROP. */)
4703 (Lisp_Object prop, Lisp_Object frame)
4704 {
4705 struct frame *f = decode_window_system_frame (frame);
4706 Atom prop_atom;
4707
4708 CHECK_STRING (prop);
4709 block_input ();
4710 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4711 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
4712
4713 /* Make sure the property is removed when we return. */
4714 XFlush (FRAME_X_DISPLAY (f));
4715 unblock_input ();
4716
4717 return prop;
4718 }
4719
4720
4721 static Lisp_Object
4722 x_window_property_intern (struct frame *f,
4723 Window target_window,
4724 Atom prop_atom,
4725 Atom target_type,
4726 Lisp_Object delete_p,
4727 Lisp_Object vector_ret_p,
4728 bool *found)
4729 {
4730 unsigned char *tmp_data = NULL;
4731 Lisp_Object prop_value = Qnil;
4732 Atom actual_type;
4733 int actual_format;
4734 unsigned long actual_size, bytes_remaining;
4735 int rc;
4736 struct gcpro gcpro1;
4737
4738 GCPRO1 (prop_value);
4739
4740 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4741 prop_atom, 0, 0, False, target_type,
4742 &actual_type, &actual_format, &actual_size,
4743 &bytes_remaining, &tmp_data);
4744
4745 *found = actual_format != 0;
4746
4747 if (rc == Success && *found)
4748 {
4749 XFree (tmp_data);
4750 tmp_data = NULL;
4751
4752 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4753 prop_atom, 0, bytes_remaining,
4754 ! NILP (delete_p), target_type,
4755 &actual_type, &actual_format,
4756 &actual_size, &bytes_remaining,
4757 &tmp_data);
4758 if (rc == Success && tmp_data)
4759 {
4760 /* The man page for XGetWindowProperty says:
4761 "If the returned format is 32, the returned data is represented
4762 as a long array and should be cast to that type to obtain the
4763 elements."
4764 This applies even if long is more than 32 bits, the X library
4765 converts from 32 bit elements received from the X server to long
4766 and passes the long array to us. Thus, for that case memcpy can not
4767 be used. We convert to a 32 bit type here, because so much code
4768 assume on that.
4769
4770 The bytes and offsets passed to XGetWindowProperty refers to the
4771 property and those are indeed in 32 bit quantities if format is
4772 32. */
4773
4774 if (BITS_PER_LONG > 32 && actual_format == 32)
4775 {
4776 unsigned long i;
4777 int *idata = (int *) tmp_data;
4778 long *ldata = (long *) tmp_data;
4779
4780 for (i = 0; i < actual_size; ++i)
4781 idata[i] = (int) ldata[i];
4782 }
4783
4784 if (NILP (vector_ret_p))
4785 prop_value = make_string ((char *) tmp_data, actual_size);
4786 else
4787 prop_value = x_property_data_to_lisp (f,
4788 tmp_data,
4789 actual_type,
4790 actual_format,
4791 actual_size);
4792 }
4793
4794 if (tmp_data) XFree (tmp_data);
4795 }
4796
4797 UNGCPRO;
4798 return prop_value;
4799 }
4800
4801 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
4802 1, 6, 0,
4803 doc: /* Value is the value of window property PROP on FRAME.
4804 If FRAME is nil or omitted, use the selected frame.
4805
4806 On X Windows, the following optional arguments are also accepted:
4807 If TYPE is nil or omitted, get the property as a string.
4808 Otherwise TYPE is the name of the atom that denotes the type expected.
4809 If SOURCE is non-nil, get the property on that window instead of from
4810 FRAME. The number 0 denotes the root window.
4811 If DELETE-P is non-nil, delete the property after retrieving it.
4812 If VECTOR-RET-P is non-nil, don't return a string but a vector of values.
4813
4814 On MS Windows, this function accepts but ignores those optional arguments.
4815
4816 Value is nil if FRAME hasn't a property with name PROP or if PROP has
4817 no value of TYPE (always string in the MS Windows case). */)
4818 (Lisp_Object prop, Lisp_Object frame, Lisp_Object type,
4819 Lisp_Object source, Lisp_Object delete_p, Lisp_Object vector_ret_p)
4820 {
4821 struct frame *f = decode_window_system_frame (frame);
4822 Atom prop_atom;
4823 Lisp_Object prop_value = Qnil;
4824 Atom target_type = XA_STRING;
4825 Window target_window = FRAME_X_WINDOW (f);
4826 struct gcpro gcpro1;
4827 bool found;
4828
4829 GCPRO1 (prop_value);
4830 CHECK_STRING (prop);
4831
4832 if (! NILP (source))
4833 {
4834 CONS_TO_INTEGER (source, Window, target_window);
4835 if (! target_window)
4836 target_window = FRAME_DISPLAY_INFO (f)->root_window;
4837 }
4838
4839 block_input ();
4840 if (STRINGP (type))
4841 {
4842 if (strcmp ("AnyPropertyType", SSDATA (type)) == 0)
4843 target_type = AnyPropertyType;
4844 else
4845 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4846 }
4847
4848 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4849 prop_value = x_window_property_intern (f,
4850 target_window,
4851 prop_atom,
4852 target_type,
4853 delete_p,
4854 vector_ret_p,
4855 &found);
4856 if (NILP (prop_value)
4857 && ! found
4858 && NILP (source)
4859 && target_window != FRAME_OUTER_WINDOW (f))
4860 {
4861 prop_value = x_window_property_intern (f,
4862 FRAME_OUTER_WINDOW (f),
4863 prop_atom,
4864 target_type,
4865 delete_p,
4866 vector_ret_p,
4867 &found);
4868 }
4869
4870
4871 unblock_input ();
4872 UNGCPRO;
4873 return prop_value;
4874 }
4875
4876 /***********************************************************************
4877 Tool tips
4878 ***********************************************************************/
4879
4880 static Lisp_Object x_create_tip_frame (struct x_display_info *,
4881 Lisp_Object, Lisp_Object);
4882 static void compute_tip_xy (struct frame *, Lisp_Object, Lisp_Object,
4883 Lisp_Object, int, int, int *, int *);
4884
4885 /* The frame of a currently visible tooltip. */
4886
4887 Lisp_Object tip_frame;
4888
4889 /* If non-nil, a timer started that hides the last tooltip when it
4890 fires. */
4891
4892 static Lisp_Object tip_timer;
4893 Window tip_window;
4894
4895 /* If non-nil, a vector of 3 elements containing the last args
4896 with which x-show-tip was called. See there. */
4897
4898 static Lisp_Object last_show_tip_args;
4899
4900
4901 static void
4902 unwind_create_tip_frame (Lisp_Object frame)
4903 {
4904 Lisp_Object deleted;
4905
4906 deleted = unwind_create_frame (frame);
4907 if (EQ (deleted, Qt))
4908 {
4909 tip_window = None;
4910 tip_frame = Qnil;
4911 }
4912 }
4913
4914
4915 /* Create a frame for a tooltip on the display described by DPYINFO.
4916 PARMS is a list of frame parameters. TEXT is the string to
4917 display in the tip frame. Value is the frame.
4918
4919 Note that functions called here, esp. x_default_parameter can
4920 signal errors, for instance when a specified color name is
4921 undefined. We have to make sure that we're in a consistent state
4922 when this happens. */
4923
4924 static Lisp_Object
4925 x_create_tip_frame (struct x_display_info *dpyinfo,
4926 Lisp_Object parms,
4927 Lisp_Object text)
4928 {
4929 struct frame *f;
4930 Lisp_Object frame;
4931 Lisp_Object name;
4932 int width, height;
4933 ptrdiff_t count = SPECPDL_INDEX ();
4934 struct gcpro gcpro1, gcpro2, gcpro3;
4935 bool face_change_before = face_change;
4936 Lisp_Object buffer;
4937 struct buffer *old_buffer;
4938
4939 if (!dpyinfo->terminal->name)
4940 error ("Terminal is not live, can't create new frames on it");
4941
4942 parms = Fcopy_alist (parms);
4943
4944 /* Get the name of the frame to use for resource lookup. */
4945 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
4946 if (!STRINGP (name)
4947 && !EQ (name, Qunbound)
4948 && !NILP (name))
4949 error ("Invalid frame name--not a string or nil");
4950
4951 frame = Qnil;
4952 GCPRO3 (parms, name, frame);
4953 f = make_frame (true);
4954 XSETFRAME (frame, f);
4955
4956 AUTO_STRING (tip, " *tip*");
4957 buffer = Fget_buffer_create (tip);
4958 /* Use set_window_buffer instead of Fset_window_buffer (see
4959 discussion of bug#11984, bug#12025, bug#12026). */
4960 set_window_buffer (FRAME_ROOT_WINDOW (f), buffer, false, false);
4961 old_buffer = current_buffer;
4962 set_buffer_internal_1 (XBUFFER (buffer));
4963 bset_truncate_lines (current_buffer, Qnil);
4964 specbind (Qinhibit_read_only, Qt);
4965 specbind (Qinhibit_modification_hooks, Qt);
4966 Ferase_buffer ();
4967 Finsert (1, &text);
4968 set_buffer_internal_1 (old_buffer);
4969
4970 record_unwind_protect (unwind_create_tip_frame, frame);
4971
4972 f->terminal = dpyinfo->terminal;
4973
4974 /* By setting the output method, we're essentially saying that
4975 the frame is live, as per FRAME_LIVE_P. If we get a signal
4976 from this point on, x_destroy_window might screw up reference
4977 counts etc. */
4978 f->output_method = output_x_window;
4979 f->output_data.x = xzalloc (sizeof *f->output_data.x);
4980 f->output_data.x->icon_bitmap = -1;
4981 FRAME_FONTSET (f) = -1;
4982 f->output_data.x->scroll_bar_foreground_pixel = -1;
4983 f->output_data.x->scroll_bar_background_pixel = -1;
4984 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
4985 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
4986 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
4987 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
4988 f->output_data.x->white_relief.pixel = -1;
4989 f->output_data.x->black_relief.pixel = -1;
4990
4991 fset_icon_name (f, Qnil);
4992 FRAME_DISPLAY_INFO (f) = dpyinfo;
4993 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
4994 f->output_data.x->explicit_parent = false;
4995
4996 /* These colors will be set anyway later, but it's important
4997 to get the color reference counts right, so initialize them! */
4998 {
4999 Lisp_Object black;
5000 struct gcpro gcpro1;
5001
5002 /* Function x_decode_color can signal an error. Make
5003 sure to initialize color slots so that we won't try
5004 to free colors we haven't allocated. */
5005 FRAME_FOREGROUND_PIXEL (f) = -1;
5006 FRAME_BACKGROUND_PIXEL (f) = -1;
5007 f->output_data.x->cursor_pixel = -1;
5008 f->output_data.x->cursor_foreground_pixel = -1;
5009 f->output_data.x->border_pixel = -1;
5010 f->output_data.x->mouse_pixel = -1;
5011
5012 black = build_string ("black");
5013 GCPRO1 (black);
5014 FRAME_FOREGROUND_PIXEL (f)
5015 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5016 FRAME_BACKGROUND_PIXEL (f)
5017 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5018 f->output_data.x->cursor_pixel
5019 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5020 f->output_data.x->cursor_foreground_pixel
5021 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5022 f->output_data.x->border_pixel
5023 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5024 f->output_data.x->mouse_pixel
5025 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
5026 UNGCPRO;
5027 }
5028
5029 /* Set the name; the functions to which we pass f expect the name to
5030 be set. */
5031 if (EQ (name, Qunbound) || NILP (name))
5032 {
5033 fset_name (f, build_string (dpyinfo->x_id_name));
5034 f->explicit_name = false;
5035 }
5036 else
5037 {
5038 fset_name (f, name);
5039 f->explicit_name = true;
5040 /* use the frame's title when getting resources for this frame. */
5041 specbind (Qx_resource_name, name);
5042 }
5043
5044 register_font_driver (&xfont_driver, f);
5045 #ifdef HAVE_FREETYPE
5046 #ifdef HAVE_XFT
5047 register_font_driver (&xftfont_driver, f);
5048 #else /* not HAVE_XFT */
5049 register_font_driver (&ftxfont_driver, f);
5050 #endif /* not HAVE_XFT */
5051 #endif /* HAVE_FREETYPE */
5052
5053 x_default_parameter (f, parms, Qfont_backend, Qnil,
5054 "fontBackend", "FontBackend", RES_TYPE_STRING);
5055
5056 /* Extract the window parameters from the supplied values that are
5057 needed to determine window geometry. */
5058 x_default_font_parameter (f, parms);
5059
5060 x_default_parameter (f, parms, Qborder_width, make_number (0),
5061 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
5062
5063 /* This defaults to 2 in order to match xterm. We recognize either
5064 internalBorderWidth or internalBorder (which is what xterm calls
5065 it). */
5066 if (NILP (Fassq (Qinternal_border_width, parms)))
5067 {
5068 Lisp_Object value;
5069
5070 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
5071 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
5072 if (! EQ (value, Qunbound))
5073 parms = Fcons (Fcons (Qinternal_border_width, value),
5074 parms);
5075 }
5076
5077 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
5078 "internalBorderWidth", "internalBorderWidth",
5079 RES_TYPE_NUMBER);
5080 x_default_parameter (f, parms, Qright_divider_width, make_number (0),
5081 NULL, NULL, RES_TYPE_NUMBER);
5082 x_default_parameter (f, parms, Qbottom_divider_width, make_number (0),
5083 NULL, NULL, RES_TYPE_NUMBER);
5084
5085 /* Also do the stuff which must be set before the window exists. */
5086 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
5087 "foreground", "Foreground", RES_TYPE_STRING);
5088 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
5089 "background", "Background", RES_TYPE_STRING);
5090 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
5091 "pointerColor", "Foreground", RES_TYPE_STRING);
5092 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
5093 "cursorColor", "Foreground", RES_TYPE_STRING);
5094 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
5095 "borderColor", "BorderColor", RES_TYPE_STRING);
5096
5097 #ifdef GLYPH_DEBUG
5098 image_cache_refcount =
5099 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
5100 dpyinfo_refcount = dpyinfo->reference_count;
5101 #endif /* GLYPH_DEBUG */
5102
5103 /* Init faces before x_default_parameter is called for the
5104 scroll-bar-width parameter because otherwise we end up in
5105 init_iterator with a null face cache, which should not happen. */
5106 init_frame_faces (f);
5107
5108 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
5109
5110 x_figure_window_size (f, parms, false);
5111
5112 {
5113 XSetWindowAttributes attrs;
5114 unsigned long mask;
5115 Atom type = FRAME_DISPLAY_INFO (f)->Xatom_net_window_type_tooltip;
5116
5117 block_input ();
5118 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
5119 if (DoesSaveUnders (dpyinfo->screen))
5120 mask |= CWSaveUnder;
5121
5122 /* Window managers look at the override-redirect flag to determine
5123 whether or net to give windows a decoration (Xlib spec, chapter
5124 3.2.8). */
5125 attrs.override_redirect = True;
5126 attrs.save_under = True;
5127 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
5128 /* Arrange for getting MapNotify and UnmapNotify events. */
5129 attrs.event_mask = StructureNotifyMask;
5130 tip_window
5131 = FRAME_X_WINDOW (f)
5132 = XCreateWindow (FRAME_X_DISPLAY (f),
5133 FRAME_DISPLAY_INFO (f)->root_window,
5134 /* x, y, width, height */
5135 0, 0, 1, 1,
5136 /* Border. */
5137 f->border_width,
5138 CopyFromParent, InputOutput, CopyFromParent,
5139 mask, &attrs);
5140 XChangeProperty (FRAME_X_DISPLAY (f), tip_window,
5141 FRAME_DISPLAY_INFO (f)->Xatom_net_window_type,
5142 XA_ATOM, 32, PropModeReplace,
5143 (unsigned char *)&type, 1);
5144 unblock_input ();
5145 }
5146
5147 x_make_gc (f);
5148
5149 x_default_parameter (f, parms, Qauto_raise, Qnil,
5150 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5151 x_default_parameter (f, parms, Qauto_lower, Qnil,
5152 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5153 x_default_parameter (f, parms, Qcursor_type, Qbox,
5154 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5155
5156 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
5157 Change will not be effected unless different from the current
5158 FRAME_LINES (f). */
5159 width = FRAME_COLS (f);
5160 height = FRAME_LINES (f);
5161 SET_FRAME_COLS (f, 0);
5162 SET_FRAME_LINES (f, 0);
5163 change_frame_size (f, width, height, true, false, false, false);
5164
5165 /* Add `tooltip' frame parameter's default value. */
5166 if (NILP (Fframe_parameter (frame, Qtooltip)))
5167 {
5168 AUTO_FRAME_ARG (arg, Qtooltip, Qt);
5169 Fmodify_frame_parameters (frame, arg);
5170 }
5171
5172 /* FIXME - can this be done in a similar way to normal frames?
5173 http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00641.html */
5174
5175 /* Set the `display-type' frame parameter before setting up faces. */
5176 {
5177 Lisp_Object disptype;
5178
5179 if (FRAME_DISPLAY_INFO (f)->n_planes == 1)
5180 disptype = intern ("mono");
5181 else if (FRAME_DISPLAY_INFO (f)->visual->class == GrayScale
5182 || FRAME_DISPLAY_INFO (f)->visual->class == StaticGray)
5183 disptype = intern ("grayscale");
5184 else
5185 disptype = intern ("color");
5186
5187 if (NILP (Fframe_parameter (frame, Qdisplay_type)))
5188 {
5189 AUTO_FRAME_ARG (arg, Qdisplay_type, disptype);
5190 Fmodify_frame_parameters (frame, arg);
5191 }
5192 }
5193
5194 /* Set up faces after all frame parameters are known. This call
5195 also merges in face attributes specified for new frames.
5196
5197 Frame parameters may be changed if .Xdefaults contains
5198 specifications for the default font. For example, if there is an
5199 `Emacs.default.attributeBackground: pink', the `background-color'
5200 attribute of the frame get's set, which let's the internal border
5201 of the tooltip frame appear in pink. Prevent this. */
5202 {
5203 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
5204
5205 /* Set tip_frame here, so that */
5206 tip_frame = frame;
5207 call2 (Qface_set_after_frame_default, frame, Qnil);
5208
5209 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
5210 {
5211 AUTO_FRAME_ARG (arg, Qbackground_color, bg);
5212 Fmodify_frame_parameters (frame, arg);
5213 }
5214 }
5215
5216 f->no_split = true;
5217
5218 UNGCPRO;
5219
5220 /* Now that the frame will be official, it counts as a reference to
5221 its display and terminal. */
5222 FRAME_DISPLAY_INFO (f)->reference_count++;
5223 f->terminal->reference_count++;
5224
5225 /* It is now ok to make the frame official even if we get an error
5226 below. And the frame needs to be on Vframe_list or making it
5227 visible won't work. */
5228 Vframe_list = Fcons (frame, Vframe_list);
5229 f->can_x_set_window_size = true;
5230
5231 /* Setting attributes of faces of the tooltip frame from resources
5232 and similar will set face_change, which leads to the clearing of
5233 all current matrices. Since this isn't necessary here, avoid it
5234 by resetting face_change to the value it had before we created
5235 the tip frame. */
5236 face_change = face_change_before;
5237
5238 /* Discard the unwind_protect. */
5239 return unbind_to (count, frame);
5240 }
5241
5242
5243 /* Compute where to display tip frame F. PARMS is the list of frame
5244 parameters for F. DX and DY are specified offsets from the current
5245 location of the mouse. WIDTH and HEIGHT are the width and height
5246 of the tooltip. Return coordinates relative to the root window of
5247 the display in *ROOT_X, and *ROOT_Y. */
5248
5249 static void
5250 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)
5251 {
5252 Lisp_Object left, top;
5253 int win_x, win_y;
5254 Window root, child;
5255 unsigned pmask;
5256
5257 /* User-specified position? */
5258 left = Fcdr (Fassq (Qleft, parms));
5259 top = Fcdr (Fassq (Qtop, parms));
5260
5261 /* Move the tooltip window where the mouse pointer is. Resize and
5262 show it. */
5263 if (!INTEGERP (left) || !INTEGERP (top))
5264 {
5265 block_input ();
5266 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_DISPLAY_INFO (f)->root_window,
5267 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
5268 unblock_input ();
5269 }
5270
5271 if (INTEGERP (top))
5272 *root_y = XINT (top);
5273 else if (*root_y + XINT (dy) <= 0)
5274 *root_y = 0; /* Can happen for negative dy */
5275 else if (*root_y + XINT (dy) + height
5276 <= x_display_pixel_height (FRAME_DISPLAY_INFO (f)))
5277 /* It fits below the pointer */
5278 *root_y += XINT (dy);
5279 else if (height + XINT (dy) <= *root_y)
5280 /* It fits above the pointer. */
5281 *root_y -= height + XINT (dy);
5282 else
5283 /* Put it on the top. */
5284 *root_y = 0;
5285
5286 if (INTEGERP (left))
5287 *root_x = XINT (left);
5288 else if (*root_x + XINT (dx) <= 0)
5289 *root_x = 0; /* Can happen for negative dx */
5290 else if (*root_x + XINT (dx) + width
5291 <= x_display_pixel_width (FRAME_DISPLAY_INFO (f)))
5292 /* It fits to the right of the pointer. */
5293 *root_x += XINT (dx);
5294 else if (width + XINT (dx) <= *root_x)
5295 /* It fits to the left of the pointer. */
5296 *root_x -= width + XINT (dx);
5297 else
5298 /* Put it left-justified on the screen--it ought to fit that way. */
5299 *root_x = 0;
5300 }
5301
5302
5303 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
5304 doc: /* Show STRING in a "tooltip" window on frame FRAME.
5305 A tooltip window is a small X window displaying a string.
5306
5307 This is an internal function; Lisp code should call `tooltip-show'.
5308
5309 FRAME nil or omitted means use the selected frame.
5310
5311 PARMS is an optional list of frame parameters which can be used to
5312 change the tooltip's appearance.
5313
5314 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
5315 means use the default timeout of 5 seconds.
5316
5317 If the list of frame parameters PARMS contains a `left' parameters,
5318 the tooltip is displayed at that x-position. Otherwise it is
5319 displayed at the mouse position, with offset DX added (default is 5 if
5320 DX isn't specified). Likewise for the y-position; if a `top' frame
5321 parameter is specified, it determines the y-position of the tooltip
5322 window, otherwise it is displayed at the mouse position, with offset
5323 DY added (default is -10).
5324
5325 A tooltip's maximum size is specified by `x-max-tooltip-size'.
5326 Text larger than the specified size is clipped. */)
5327 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
5328 {
5329 struct frame *f;
5330 struct window *w;
5331 int root_x, root_y;
5332 struct buffer *old_buffer;
5333 struct text_pos pos;
5334 int i, width, height;
5335 bool seen_reversed_p;
5336 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5337 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5338 ptrdiff_t count = SPECPDL_INDEX ();
5339
5340 specbind (Qinhibit_redisplay, Qt);
5341
5342 GCPRO4 (string, parms, frame, timeout);
5343
5344 CHECK_STRING (string);
5345 if (SCHARS (string) == 0)
5346 string = make_unibyte_string (" ", 1);
5347
5348 f = decode_window_system_frame (frame);
5349 if (NILP (timeout))
5350 timeout = make_number (5);
5351 else
5352 CHECK_NATNUM (timeout);
5353
5354 if (NILP (dx))
5355 dx = make_number (5);
5356 else
5357 CHECK_NUMBER (dx);
5358
5359 if (NILP (dy))
5360 dy = make_number (-10);
5361 else
5362 CHECK_NUMBER (dy);
5363
5364 #ifdef USE_GTK
5365 if (x_gtk_use_system_tooltips)
5366 {
5367 bool ok;
5368
5369 /* Hide a previous tip, if any. */
5370 Fx_hide_tip ();
5371
5372 block_input ();
5373 ok = xg_prepare_tooltip (f, string, &width, &height);
5374 if (ok)
5375 {
5376 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5377 xg_show_tooltip (f, root_x, root_y);
5378 /* This is used in Fx_hide_tip. */
5379 XSETFRAME (tip_frame, f);
5380 }
5381 unblock_input ();
5382 if (ok) goto start_timer;
5383 }
5384 #endif /* USE_GTK */
5385
5386 if (NILP (last_show_tip_args))
5387 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5388
5389 if (!NILP (tip_frame))
5390 {
5391 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5392 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5393 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5394
5395 if (EQ (frame, last_frame)
5396 && !NILP (Fequal (last_string, string))
5397 && !NILP (Fequal (last_parms, parms)))
5398 {
5399 struct frame *tip_f = XFRAME (tip_frame);
5400
5401 /* Only DX and DY have changed. */
5402 if (!NILP (tip_timer))
5403 {
5404 Lisp_Object timer = tip_timer;
5405 tip_timer = Qnil;
5406 call1 (Qcancel_timer, timer);
5407 }
5408
5409 block_input ();
5410 compute_tip_xy (tip_f, parms, dx, dy, FRAME_PIXEL_WIDTH (tip_f),
5411 FRAME_PIXEL_HEIGHT (tip_f), &root_x, &root_y);
5412 XMoveWindow (FRAME_X_DISPLAY (tip_f), FRAME_X_WINDOW (tip_f),
5413 root_x, root_y);
5414 unblock_input ();
5415 goto start_timer;
5416 }
5417 }
5418
5419 /* Hide a previous tip, if any. */
5420 Fx_hide_tip ();
5421
5422 ASET (last_show_tip_args, 0, string);
5423 ASET (last_show_tip_args, 1, frame);
5424 ASET (last_show_tip_args, 2, parms);
5425
5426 /* Add default values to frame parameters. */
5427 if (NILP (Fassq (Qname, parms)))
5428 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5429 if (NILP (Fassq (Qinternal_border_width, parms)))
5430 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5431 if (NILP (Fassq (Qborder_width, parms)))
5432 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5433 if (NILP (Fassq (Qbottom_divider_width, parms)))
5434 parms = Fcons (Fcons (Qbottom_divider_width, make_number (0)), parms);
5435 if (NILP (Fassq (Qright_divider_width, parms)))
5436 parms = Fcons (Fcons (Qright_divider_width, make_number (0)), parms);
5437 if (NILP (Fassq (Qborder_color, parms)))
5438 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5439 if (NILP (Fassq (Qbackground_color, parms)))
5440 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5441 parms);
5442
5443 /* Create a frame for the tooltip, and record it in the global
5444 variable tip_frame. */
5445 frame = x_create_tip_frame (FRAME_DISPLAY_INFO (f), parms, string);
5446 f = XFRAME (frame);
5447
5448 /* Set up the frame's root window. */
5449 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5450 w->left_col = 0;
5451 w->top_line = 0;
5452 w->pixel_left = 0;
5453 w->pixel_top = 0;
5454
5455 if (CONSP (Vx_max_tooltip_size)
5456 && RANGED_INTEGERP (1, XCAR (Vx_max_tooltip_size), INT_MAX)
5457 && RANGED_INTEGERP (1, XCDR (Vx_max_tooltip_size), INT_MAX))
5458 {
5459 w->total_cols = XFASTINT (XCAR (Vx_max_tooltip_size));
5460 w->total_lines = XFASTINT (XCDR (Vx_max_tooltip_size));
5461 }
5462 else
5463 {
5464 w->total_cols = 80;
5465 w->total_lines = 40;
5466 }
5467
5468 w->pixel_width = w->total_cols * FRAME_COLUMN_WIDTH (f);
5469 w->pixel_height = w->total_lines * FRAME_LINE_HEIGHT (f);
5470
5471 FRAME_TOTAL_COLS (f) = w->total_cols;
5472 adjust_frame_glyphs (f);
5473 w->pseudo_window_p = true;
5474
5475 /* Display the tooltip text in a temporary buffer. */
5476 old_buffer = current_buffer;
5477 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->contents));
5478 bset_truncate_lines (current_buffer, Qnil);
5479 clear_glyph_matrix (w->desired_matrix);
5480 clear_glyph_matrix (w->current_matrix);
5481 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5482 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
5483
5484 /* Compute width and height of the tooltip. */
5485 width = height = 0;
5486 seen_reversed_p = false;
5487 for (i = 0; i < w->desired_matrix->nrows; ++i)
5488 {
5489 struct glyph_row *row = &w->desired_matrix->rows[i];
5490 struct glyph *last;
5491 int row_width;
5492
5493 /* Stop at the first empty row at the end. */
5494 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5495 break;
5496
5497 /* Let the row go over the full width of the frame. */
5498 row->full_width_p = true;
5499
5500 row_width = row->pixel_width;
5501 if (row->used[TEXT_AREA])
5502 {
5503 /* There's a glyph at the end of rows that is used to place
5504 the cursor there. Don't include the width of this glyph. */
5505 if (!row->reversed_p)
5506 {
5507 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5508 if (NILP (last->object))
5509 row_width -= last->pixel_width;
5510 }
5511 else
5512 {
5513 /* There could be a stretch glyph at the beginning of R2L
5514 rows that is produced by extend_face_to_end_of_line.
5515 Don't count that glyph. */
5516 struct glyph *g = row->glyphs[TEXT_AREA];
5517
5518 if (g->type == STRETCH_GLYPH && NILP (g->object))
5519 {
5520 row_width -= g->pixel_width;
5521 seen_reversed_p = true;
5522 }
5523 }
5524 }
5525
5526 height += row->height;
5527 width = max (width, row_width);
5528 }
5529
5530 /* If we've seen partial-length R2L rows, we need to re-adjust the
5531 tool-tip frame width and redisplay it again, to avoid over-wide
5532 tips due to the stretch glyph that extends R2L lines to full
5533 width of the frame. */
5534 if (seen_reversed_p)
5535 {
5536 /* w->total_cols and FRAME_TOTAL_COLS want the width in columns,
5537 not in pixels. */
5538 w->pixel_width = width;
5539 width /= WINDOW_FRAME_COLUMN_WIDTH (w);
5540 w->total_cols = width;
5541 FRAME_TOTAL_COLS (f) = width;
5542 SET_FRAME_WIDTH (f, width);
5543 adjust_frame_glyphs (f);
5544 clear_glyph_matrix (w->desired_matrix);
5545 clear_glyph_matrix (w->current_matrix);
5546 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
5547 width = height = 0;
5548 /* Recompute width and height of the tooltip. */
5549 for (i = 0; i < w->desired_matrix->nrows; ++i)
5550 {
5551 struct glyph_row *row = &w->desired_matrix->rows[i];
5552 struct glyph *last;
5553 int row_width;
5554
5555 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5556 break;
5557 row->full_width_p = true;
5558 row_width = row->pixel_width;
5559 if (row->used[TEXT_AREA] && !row->reversed_p)
5560 {
5561 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5562 if (NILP (last->object))
5563 row_width -= last->pixel_width;
5564 }
5565
5566 height += row->height;
5567 width = max (width, row_width);
5568 }
5569 }
5570
5571 /* Add the frame's internal border to the width and height the X
5572 window should have. */
5573 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5574 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5575
5576 /* Move the tooltip window where the mouse pointer is. Resize and
5577 show it. */
5578 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5579
5580 block_input ();
5581 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5582 root_x, root_y, width, height);
5583 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5584 unblock_input ();
5585
5586 /* Draw into the window. */
5587 w->must_be_updated_p = true;
5588 update_single_window (w);
5589
5590 /* Restore original current buffer. */
5591 set_buffer_internal_1 (old_buffer);
5592 windows_or_buffers_changed = old_windows_or_buffers_changed;
5593
5594 start_timer:
5595 /* Let the tip disappear after timeout seconds. */
5596 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5597 intern ("x-hide-tip"));
5598
5599 UNGCPRO;
5600 return unbind_to (count, Qnil);
5601 }
5602
5603
5604 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5605 doc: /* Hide the current tooltip window, if there is any.
5606 Value is t if tooltip was open, nil otherwise. */)
5607 (void)
5608 {
5609 ptrdiff_t count;
5610 Lisp_Object deleted, frame, timer;
5611 struct gcpro gcpro1, gcpro2;
5612
5613 /* Return quickly if nothing to do. */
5614 if (NILP (tip_timer) && NILP (tip_frame))
5615 return Qnil;
5616
5617 frame = tip_frame;
5618 timer = tip_timer;
5619 GCPRO2 (frame, timer);
5620 tip_frame = tip_timer = deleted = Qnil;
5621
5622 count = SPECPDL_INDEX ();
5623 specbind (Qinhibit_redisplay, Qt);
5624 specbind (Qinhibit_quit, Qt);
5625
5626 if (!NILP (timer))
5627 call1 (Qcancel_timer, timer);
5628
5629 #ifdef USE_GTK
5630 {
5631 /* When using system tooltip, tip_frame is the Emacs frame on which
5632 the tip is shown. */
5633 struct frame *f = XFRAME (frame);
5634 if (FRAME_LIVE_P (f) && xg_hide_tooltip (f))
5635 frame = Qnil;
5636 }
5637 #endif
5638
5639 if (FRAMEP (frame))
5640 {
5641 delete_frame (frame, Qnil);
5642 deleted = Qt;
5643
5644 #ifdef USE_LUCID
5645 /* Bloodcurdling hack alert: The Lucid menu bar widget's
5646 redisplay procedure is not called when a tip frame over menu
5647 items is unmapped. Redisplay the menu manually... */
5648 {
5649 Widget w;
5650 struct frame *f = SELECTED_FRAME ();
5651 w = f->output_data.x->menubar_widget;
5652
5653 if (!DoesSaveUnders (FRAME_DISPLAY_INFO (f)->screen)
5654 && w != NULL)
5655 {
5656 block_input ();
5657 xlwmenu_redisplay (w);
5658 unblock_input ();
5659 }
5660 }
5661 #endif /* USE_LUCID */
5662 }
5663
5664 UNGCPRO;
5665 return unbind_to (count, deleted);
5666 }
5667
5668
5669 \f
5670 /***********************************************************************
5671 File selection dialog
5672 ***********************************************************************/
5673
5674 DEFUN ("x-uses-old-gtk-dialog", Fx_uses_old_gtk_dialog,
5675 Sx_uses_old_gtk_dialog,
5676 0, 0, 0,
5677 doc: /* Return t if the old Gtk+ file selection dialog is used. */)
5678 (void)
5679 {
5680 #ifdef USE_GTK
5681 if (use_dialog_box
5682 && use_file_dialog
5683 && window_system_available (SELECTED_FRAME ())
5684 && xg_uses_old_file_dialog ())
5685 return Qt;
5686 #endif
5687 return Qnil;
5688 }
5689
5690
5691 #ifdef USE_MOTIF
5692 /* Callback for "OK" and "Cancel" on file selection dialog. */
5693
5694 static void
5695 file_dialog_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5696 {
5697 int *result = client_data;
5698 XmAnyCallbackStruct *cb = call_data;
5699 *result = cb->reason;
5700 }
5701
5702
5703 /* Callback for unmapping a file selection dialog. This is used to
5704 capture the case where a dialog is closed via a window manager's
5705 closer button, for example. Using a XmNdestroyCallback didn't work
5706 in this case. */
5707
5708 static void
5709 file_dialog_unmap_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5710 {
5711 int *result = client_data;
5712 *result = XmCR_CANCEL;
5713 }
5714
5715 static void
5716 clean_up_file_dialog (void *arg)
5717 {
5718 Widget dialog = arg;
5719
5720 /* Clean up. */
5721 block_input ();
5722 XtUnmanageChild (dialog);
5723 XtDestroyWidget (dialog);
5724 x_menu_set_in_use (false);
5725 unblock_input ();
5726 }
5727
5728
5729 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5730 doc: /* Read file name, prompting with PROMPT in directory DIR.
5731 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5732 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5733 or directory must exist.
5734
5735 This function is only defined on NS, MS Windows, and X Windows with the
5736 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5737 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories.
5738 On Windows 7 and later, the file selection dialog "remembers" the last
5739 directory where the user selected a file, and will open that directory
5740 instead of DIR on subsequent invocations of this function with the same
5741 value of DIR as in previous invocations; this is standard Windows behavior. */)
5742 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename,
5743 Lisp_Object mustmatch, Lisp_Object only_dir_p)
5744 {
5745 int result;
5746 struct frame *f = SELECTED_FRAME ();
5747 Lisp_Object file = Qnil;
5748 Lisp_Object decoded_file;
5749 Widget dialog, text, help;
5750 Arg al[10];
5751 int ac = 0;
5752 XmString dir_xmstring, pattern_xmstring;
5753 ptrdiff_t count = SPECPDL_INDEX ();
5754 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5755
5756 check_window_system (f);
5757
5758 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5759
5760 if (popup_activated ())
5761 error ("Trying to use a menu from within a menu-entry");
5762
5763 CHECK_STRING (prompt);
5764 CHECK_STRING (dir);
5765
5766 /* Prevent redisplay. */
5767 specbind (Qinhibit_redisplay, Qt);
5768
5769 block_input ();
5770
5771 /* Create the dialog with PROMPT as title, using DIR as initial
5772 directory and using "*" as pattern. */
5773 dir = Fexpand_file_name (dir, Qnil);
5774 dir_xmstring = XmStringCreateLocalized (SSDATA (dir));
5775 pattern_xmstring = XmStringCreateLocalized ("*");
5776
5777 XtSetArg (al[ac], XmNtitle, SDATA (prompt)); ++ac;
5778 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
5779 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
5780 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
5781 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
5782 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
5783 "fsb", al, ac);
5784 XmStringFree (dir_xmstring);
5785 XmStringFree (pattern_xmstring);
5786
5787 /* Add callbacks for OK and Cancel. */
5788 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
5789 (XtPointer) &result);
5790 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
5791 (XtPointer) &result);
5792 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
5793 (XtPointer) &result);
5794
5795 /* Remove the help button since we can't display help. */
5796 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
5797 XtUnmanageChild (help);
5798
5799 /* Mark OK button as default. */
5800 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
5801 XmNshowAsDefault, True, NULL);
5802
5803 /* If MUSTMATCH is non-nil, disable the file entry field of the
5804 dialog, so that the user must select a file from the files list
5805 box. We can't remove it because we wouldn't have a way to get at
5806 the result file name, then. */
5807 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5808 if (!NILP (mustmatch))
5809 {
5810 Widget label;
5811 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
5812 XtSetSensitive (text, False);
5813 XtSetSensitive (label, False);
5814 }
5815
5816 /* Manage the dialog, so that list boxes get filled. */
5817 XtManageChild (dialog);
5818
5819 if (STRINGP (default_filename))
5820 {
5821 XmString default_xmstring;
5822 Widget wtext = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5823 Widget list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
5824
5825 XmTextPosition last_pos = XmTextFieldGetLastPosition (wtext);
5826 XmTextFieldReplace (wtext, 0, last_pos,
5827 (SSDATA (Ffile_name_nondirectory (default_filename))));
5828
5829 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
5830 must include the path for this to work. */
5831
5832 default_xmstring = XmStringCreateLocalized (SSDATA (default_filename));
5833
5834 if (XmListItemExists (list, default_xmstring))
5835 {
5836 int item_pos = XmListItemPos (list, default_xmstring);
5837 /* Select the item and scroll it into view. */
5838 XmListSelectPos (list, item_pos, True);
5839 XmListSetPos (list, item_pos);
5840 }
5841
5842 XmStringFree (default_xmstring);
5843 }
5844
5845 record_unwind_protect_ptr (clean_up_file_dialog, dialog);
5846
5847 /* Process events until the user presses Cancel or OK. */
5848 x_menu_set_in_use (true);
5849 result = 0;
5850 while (result == 0)
5851 {
5852 XEvent event;
5853 x_menu_wait_for_event (0);
5854 XtAppNextEvent (Xt_app_con, &event);
5855 if (event.type == KeyPress
5856 && FRAME_X_DISPLAY (f) == event.xkey.display)
5857 {
5858 KeySym keysym = XLookupKeysym (&event.xkey, 0);
5859
5860 /* Pop down on C-g. */
5861 if (keysym == XK_g && (event.xkey.state & ControlMask) != 0)
5862 XtUnmanageChild (dialog);
5863 }
5864
5865 (void) x_dispatch_event (&event, FRAME_X_DISPLAY (f));
5866 }
5867
5868 /* Get the result. */
5869 if (result == XmCR_OK)
5870 {
5871 XmString text_string;
5872 String data;
5873
5874 XtVaGetValues (dialog, XmNtextString, &text_string, NULL);
5875 XmStringGetLtoR (text_string, XmFONTLIST_DEFAULT_TAG, &data);
5876 XmStringFree (text_string);
5877 file = build_string (data);
5878 XtFree (data);
5879 }
5880 else
5881 file = Qnil;
5882
5883 unblock_input ();
5884 UNGCPRO;
5885
5886 /* Make "Cancel" equivalent to C-g. */
5887 if (NILP (file))
5888 Fsignal (Qquit, Qnil);
5889
5890 decoded_file = DECODE_FILE (file);
5891
5892 return unbind_to (count, decoded_file);
5893 }
5894
5895 #endif /* USE_MOTIF */
5896
5897 #ifdef USE_GTK
5898
5899 static void
5900 clean_up_dialog (void)
5901 {
5902 x_menu_set_in_use (false);
5903 }
5904
5905 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5906 doc: /* Read file name, prompting with PROMPT in directory DIR.
5907 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5908 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5909 or directory must exist.
5910
5911 This function is only defined on NS, MS Windows, and X Windows with the
5912 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5913 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories.
5914 On Windows 7 and later, the file selection dialog "remembers" the last
5915 directory where the user selected a file, and will open that directory
5916 instead of DIR on subsequent invocations of this function with the same
5917 value of DIR as in previous invocations; this is standard Windows behavior. */)
5918 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
5919 {
5920 struct frame *f = SELECTED_FRAME ();
5921 char *fn;
5922 Lisp_Object file = Qnil;
5923 Lisp_Object decoded_file;
5924 ptrdiff_t count = SPECPDL_INDEX ();
5925 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5926 char *cdef_file;
5927
5928 check_window_system (f);
5929
5930 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5931
5932 if (popup_activated ())
5933 error ("Trying to use a menu from within a menu-entry");
5934
5935 CHECK_STRING (prompt);
5936 CHECK_STRING (dir);
5937
5938 /* Prevent redisplay. */
5939 specbind (Qinhibit_redisplay, Qt);
5940 record_unwind_protect_void (clean_up_dialog);
5941
5942 block_input ();
5943
5944 if (STRINGP (default_filename))
5945 cdef_file = SSDATA (default_filename);
5946 else
5947 cdef_file = SSDATA (dir);
5948
5949 fn = xg_get_file_name (f, SSDATA (prompt), cdef_file,
5950 ! NILP (mustmatch),
5951 ! NILP (only_dir_p));
5952
5953 if (fn)
5954 {
5955 file = build_string (fn);
5956 xfree (fn);
5957 }
5958
5959 unblock_input ();
5960 UNGCPRO;
5961
5962 /* Make "Cancel" equivalent to C-g. */
5963 if (NILP (file))
5964 Fsignal (Qquit, Qnil);
5965
5966 decoded_file = DECODE_FILE (file);
5967
5968 return unbind_to (count, decoded_file);
5969 }
5970
5971
5972 #ifdef HAVE_FREETYPE
5973
5974 DEFUN ("x-select-font", Fx_select_font, Sx_select_font, 0, 2, 0,
5975 doc: /* Read a font using a GTK dialog.
5976 Return either a font spec (for GTK versions >= 3.2) or a string
5977 containing a GTK-style font name.
5978
5979 FRAME is the frame on which to pop up the font chooser. If omitted or
5980 nil, it defaults to the selected frame. */)
5981 (Lisp_Object frame, Lisp_Object ignored)
5982 {
5983 struct frame *f = decode_window_system_frame (frame);
5984 Lisp_Object font;
5985 Lisp_Object font_param;
5986 char *default_name = NULL;
5987 struct gcpro gcpro1, gcpro2;
5988 ptrdiff_t count = SPECPDL_INDEX ();
5989
5990 if (popup_activated ())
5991 error ("Trying to use a menu from within a menu-entry");
5992
5993 /* Prevent redisplay. */
5994 specbind (Qinhibit_redisplay, Qt);
5995 record_unwind_protect_void (clean_up_dialog);
5996
5997 block_input ();
5998
5999 GCPRO2 (font_param, font);
6000
6001 XSETFONT (font, FRAME_FONT (f));
6002 font_param = Ffont_get (font, intern (":name"));
6003 if (STRINGP (font_param))
6004 default_name = xlispstrdup (font_param);
6005 else
6006 {
6007 font_param = Fframe_parameter (frame, Qfont_param);
6008 if (STRINGP (font_param))
6009 default_name = xlispstrdup (font_param);
6010 }
6011
6012 font = xg_get_font (f, default_name);
6013 xfree (default_name);
6014
6015 unblock_input ();
6016
6017 if (NILP (font))
6018 Fsignal (Qquit, Qnil);
6019
6020 return unbind_to (count, font);
6021 }
6022 #endif /* HAVE_FREETYPE */
6023
6024 #endif /* USE_GTK */
6025
6026 \f
6027 /***********************************************************************
6028 Keyboard
6029 ***********************************************************************/
6030
6031 #ifdef HAVE_XKB
6032 #include <X11/XKBlib.h>
6033 #include <X11/keysym.h>
6034 #endif
6035
6036 DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
6037 Sx_backspace_delete_keys_p, 0, 1, 0,
6038 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
6039 FRAME nil means use the selected frame.
6040 Value is t if we know that both keys are present, and are mapped to the
6041 usual X keysyms. Value is `lambda' if we cannot determine if both keys are
6042 present and mapped to the usual X keysyms. */)
6043 (Lisp_Object frame)
6044 {
6045 #ifndef HAVE_XKB
6046 return Qlambda;
6047 #else
6048 XkbDescPtr kb;
6049 struct frame *f = decode_window_system_frame (frame);
6050 Display *dpy = FRAME_X_DISPLAY (f);
6051 Lisp_Object have_keys;
6052 int major, minor, op, event, error_code;
6053
6054 block_input ();
6055
6056 /* Check library version in case we're dynamically linked. */
6057 major = XkbMajorVersion;
6058 minor = XkbMinorVersion;
6059 if (!XkbLibraryVersion (&major, &minor))
6060 {
6061 unblock_input ();
6062 return Qlambda;
6063 }
6064
6065 /* Check that the server supports XKB. */
6066 major = XkbMajorVersion;
6067 minor = XkbMinorVersion;
6068 if (!XkbQueryExtension (dpy, &op, &event, &error_code, &major, &minor))
6069 {
6070 unblock_input ();
6071 return Qlambda;
6072 }
6073
6074 /* In this code we check that the keyboard has physical keys with names
6075 that start with BKSP (Backspace) and DELE (Delete), and that they
6076 generate keysym XK_BackSpace and XK_Delete respectively.
6077 This function is used to test if normal-erase-is-backspace should be
6078 turned on.
6079 An alternative approach would be to just check if XK_BackSpace and
6080 XK_Delete are mapped to any key. But if any of those are mapped to
6081 some non-intuitive key combination (Meta-Shift-Ctrl-whatever) and the
6082 user doesn't know about it, it is better to return false here.
6083 It is more obvious to the user what to do if she/he has two keys
6084 clearly marked with names/symbols and one key does something not
6085 expected (i.e. she/he then tries the other).
6086 The cases where Backspace/Delete is mapped to some other key combination
6087 are rare, and in those cases, normal-erase-is-backspace can be turned on
6088 manually. */
6089
6090 have_keys = Qnil;
6091 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
6092 if (kb)
6093 {
6094 int delete_keycode = 0, backspace_keycode = 0, i;
6095
6096 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
6097 {
6098 for (i = kb->min_key_code;
6099 (i < kb->max_key_code
6100 && (delete_keycode == 0 || backspace_keycode == 0));
6101 ++i)
6102 {
6103 /* The XKB symbolic key names can be seen most easily in
6104 the PS file generated by `xkbprint -label name
6105 $DISPLAY'. */
6106 if (memcmp ("DELE", kb->names->keys[i].name, 4) == 0)
6107 delete_keycode = i;
6108 else if (memcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
6109 backspace_keycode = i;
6110 }
6111
6112 XkbFreeNames (kb, 0, True);
6113 }
6114
6115 /* As of libX11-1.6.2, XkbGetMap manual says that you should use
6116 XkbFreeClientMap to free the data returned by XkbGetMap. But
6117 this function just frees the data referenced from KB and not
6118 KB itself. To free KB as well, call XkbFreeKeyboard. */
6119 XkbFreeKeyboard (kb, XkbAllMapComponentsMask, True);
6120
6121 if (delete_keycode
6122 && backspace_keycode
6123 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
6124 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
6125 have_keys = Qt;
6126 }
6127 unblock_input ();
6128 return have_keys;
6129 #endif
6130 }
6131
6132
6133 \f
6134 /***********************************************************************
6135 Initialization
6136 ***********************************************************************/
6137
6138 /* Keep this list in the same order as frame_parms in frame.c.
6139 Use 0 for unsupported frame parameters. */
6140
6141 frame_parm_handler x_frame_parm_handlers[] =
6142 {
6143 x_set_autoraise,
6144 x_set_autolower,
6145 x_set_background_color,
6146 x_set_border_color,
6147 x_set_border_width,
6148 x_set_cursor_color,
6149 x_set_cursor_type,
6150 x_set_font,
6151 x_set_foreground_color,
6152 x_set_icon_name,
6153 x_set_icon_type,
6154 x_set_internal_border_width,
6155 x_set_right_divider_width,
6156 x_set_bottom_divider_width,
6157 x_set_menu_bar_lines,
6158 x_set_mouse_color,
6159 x_explicitly_set_name,
6160 x_set_scroll_bar_width,
6161 x_set_scroll_bar_height,
6162 x_set_title,
6163 x_set_unsplittable,
6164 x_set_vertical_scroll_bars,
6165 x_set_horizontal_scroll_bars,
6166 x_set_visibility,
6167 x_set_tool_bar_lines,
6168 x_set_scroll_bar_foreground,
6169 x_set_scroll_bar_background,
6170 x_set_screen_gamma,
6171 x_set_line_spacing,
6172 x_set_left_fringe,
6173 x_set_right_fringe,
6174 x_set_wait_for_wm,
6175 x_set_fullscreen,
6176 x_set_font_backend,
6177 x_set_alpha,
6178 x_set_sticky,
6179 x_set_tool_bar_position,
6180 };
6181
6182 void
6183 syms_of_xfns (void)
6184 {
6185 DEFSYM (Qundefined_color, "undefined-color");
6186 DEFSYM (Qcompound_text, "compound-text");
6187 DEFSYM (Qcancel_timer, "cancel-timer");
6188 DEFSYM (Qfont_param, "font-parameter");
6189
6190 Fput (Qundefined_color, Qerror_conditions,
6191 listn (CONSTYPE_PURE, 2, Qundefined_color, Qerror));
6192 Fput (Qundefined_color, Qerror_message,
6193 build_pure_c_string ("Undefined color"));
6194
6195 DEFVAR_LISP ("x-pointer-shape", Vx_pointer_shape,
6196 doc: /* The shape of the pointer when over text.
6197 Changing the value does not affect existing frames
6198 unless you set the mouse color. */);
6199 Vx_pointer_shape = Qnil;
6200
6201 #if false /* This doesn't really do anything. */
6202 DEFVAR_LISP ("x-nontext-pointer-shape", Vx_nontext_pointer_shape,
6203 doc: /* The shape of the pointer when not over text.
6204 This variable takes effect when you create a new frame
6205 or when you set the mouse color. */);
6206 #endif
6207 Vx_nontext_pointer_shape = Qnil;
6208
6209 DEFVAR_LISP ("x-hourglass-pointer-shape", Vx_hourglass_pointer_shape,
6210 doc: /* The shape of the pointer when Emacs is busy.
6211 This variable takes effect when you create a new frame
6212 or when you set the mouse color. */);
6213 Vx_hourglass_pointer_shape = Qnil;
6214
6215 #if false /* This doesn't really do anything. */
6216 DEFVAR_LISP ("x-mode-pointer-shape", Vx_mode_pointer_shape,
6217 doc: /* The shape of the pointer when over the mode line.
6218 This variable takes effect when you create a new frame
6219 or when you set the mouse color. */);
6220 #endif
6221 Vx_mode_pointer_shape = Qnil;
6222
6223 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
6224 Vx_sensitive_text_pointer_shape,
6225 doc: /* The shape of the pointer when over mouse-sensitive text.
6226 This variable takes effect when you create a new frame
6227 or when you set the mouse color. */);
6228 Vx_sensitive_text_pointer_shape = Qnil;
6229
6230 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
6231 Vx_window_horizontal_drag_shape,
6232 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
6233 This variable takes effect when you create a new frame
6234 or when you set the mouse color. */);
6235 Vx_window_horizontal_drag_shape = Qnil;
6236
6237 DEFVAR_LISP ("x-window-vertical-drag-cursor",
6238 Vx_window_vertical_drag_shape,
6239 doc: /* Pointer shape to use for indicating a window can be dragged vertically.
6240 This variable takes effect when you create a new frame
6241 or when you set the mouse color. */);
6242 Vx_window_vertical_drag_shape = Qnil;
6243
6244 DEFVAR_LISP ("x-cursor-fore-pixel", Vx_cursor_fore_pixel,
6245 doc: /* A string indicating the foreground color of the cursor box. */);
6246 Vx_cursor_fore_pixel = Qnil;
6247
6248 DEFVAR_LISP ("x-max-tooltip-size", Vx_max_tooltip_size,
6249 doc: /* Maximum size for tooltips.
6250 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
6251 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
6252
6253 DEFVAR_LISP ("x-no-window-manager", Vx_no_window_manager,
6254 doc: /* Non-nil if no X window manager is in use.
6255 Emacs doesn't try to figure this out; this is always nil
6256 unless you set it to something else. */);
6257 /* We don't have any way to find this out, so set it to nil
6258 and maybe the user would like to set it to t. */
6259 Vx_no_window_manager = Qnil;
6260
6261 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
6262 Vx_pixel_size_width_font_regexp,
6263 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
6264
6265 Since Emacs gets width of a font matching with this regexp from
6266 PIXEL_SIZE field of the name, font finding mechanism gets faster for
6267 such a font. This is especially effective for such large fonts as
6268 Chinese, Japanese, and Korean. */);
6269 Vx_pixel_size_width_font_regexp = Qnil;
6270
6271 /* This is not ifdef:ed, so other builds than GTK can customize it. */
6272 DEFVAR_BOOL ("x-gtk-use-old-file-dialog", x_gtk_use_old_file_dialog,
6273 doc: /* Non-nil means prompt with the old GTK file selection dialog.
6274 If nil or if the file selection dialog is not available, the new GTK file
6275 chooser is used instead. To turn off all file dialogs set the
6276 variable `use-file-dialog'. */);
6277 x_gtk_use_old_file_dialog = false;
6278
6279 DEFVAR_BOOL ("x-gtk-show-hidden-files", x_gtk_show_hidden_files,
6280 doc: /* If non-nil, the GTK file chooser will by default show hidden files.
6281 Note that this is just the default, there is a toggle button on the file
6282 chooser to show or not show hidden files on a case by case basis. */);
6283 x_gtk_show_hidden_files = false;
6284
6285 DEFVAR_BOOL ("x-gtk-file-dialog-help-text", x_gtk_file_dialog_help_text,
6286 doc: /* If non-nil, the GTK file chooser will show additional help text.
6287 If more space for files in the file chooser dialog is wanted, set this to nil
6288 to turn the additional text off. */);
6289 x_gtk_file_dialog_help_text = true;
6290
6291 DEFVAR_BOOL ("x-gtk-use-system-tooltips", x_gtk_use_system_tooltips,
6292 doc: /* If non-nil with a Gtk+ built Emacs, the Gtk+ tooltip is used.
6293 Otherwise use Emacs own tooltip implementation.
6294 When using Gtk+ tooltips, the tooltip face is not used. */);
6295 x_gtk_use_system_tooltips = true;
6296
6297 /* Tell Emacs about this window system. */
6298 Fprovide (Qx, Qnil);
6299
6300 #ifdef USE_X_TOOLKIT
6301 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6302 #ifdef USE_MOTIF
6303 Fprovide (intern_c_string ("motif"), Qnil);
6304
6305 DEFVAR_LISP ("motif-version-string", Vmotif_version_string,
6306 doc: /* Version info for LessTif/Motif. */);
6307 Vmotif_version_string = build_string (XmVERSION_STRING);
6308 #endif /* USE_MOTIF */
6309 #endif /* USE_X_TOOLKIT */
6310
6311 #ifdef USE_GTK
6312 /* Provide x-toolkit also for GTK. Internally GTK does not use Xt so it
6313 is not an X toolkit in that sense (USE_X_TOOLKIT is not defined).
6314 But for a user it is a toolkit for X, and indeed, configure
6315 accepts --with-x-toolkit=gtk. */
6316 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6317 Fprovide (intern_c_string ("gtk"), Qnil);
6318 Fprovide (intern_c_string ("move-toolbar"), Qnil);
6319
6320 DEFVAR_LISP ("gtk-version-string", Vgtk_version_string,
6321 doc: /* Version info for GTK+. */);
6322 {
6323 char gtk_version[sizeof ".." + 3 * INT_STRLEN_BOUND (int)];
6324 int len = sprintf (gtk_version, "%d.%d.%d",
6325 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
6326 Vgtk_version_string = make_pure_string (gtk_version, len, len, false);
6327 }
6328 #endif /* USE_GTK */
6329
6330 /* X window properties. */
6331 defsubr (&Sx_change_window_property);
6332 defsubr (&Sx_delete_window_property);
6333 defsubr (&Sx_window_property);
6334
6335 defsubr (&Sxw_display_color_p);
6336 defsubr (&Sx_display_grayscale_p);
6337 defsubr (&Sxw_color_defined_p);
6338 defsubr (&Sxw_color_values);
6339 defsubr (&Sx_server_max_request_size);
6340 defsubr (&Sx_server_vendor);
6341 defsubr (&Sx_server_version);
6342 defsubr (&Sx_display_pixel_width);
6343 defsubr (&Sx_display_pixel_height);
6344 defsubr (&Sx_display_mm_width);
6345 defsubr (&Sx_display_mm_height);
6346 defsubr (&Sx_display_screens);
6347 defsubr (&Sx_display_planes);
6348 defsubr (&Sx_display_color_cells);
6349 defsubr (&Sx_display_visual_class);
6350 defsubr (&Sx_display_backing_store);
6351 defsubr (&Sx_display_save_under);
6352 defsubr (&Sx_display_monitor_attributes_list);
6353 defsubr (&Sx_frame_geometry);
6354 defsubr (&Sx_wm_set_size_hint);
6355 defsubr (&Sx_create_frame);
6356 defsubr (&Sx_open_connection);
6357 defsubr (&Sx_close_connection);
6358 defsubr (&Sx_display_list);
6359 defsubr (&Sx_synchronize);
6360 defsubr (&Sx_backspace_delete_keys_p);
6361
6362 defsubr (&Sx_show_tip);
6363 defsubr (&Sx_hide_tip);
6364 tip_timer = Qnil;
6365 staticpro (&tip_timer);
6366 tip_frame = Qnil;
6367 staticpro (&tip_frame);
6368
6369 last_show_tip_args = Qnil;
6370 staticpro (&last_show_tip_args);
6371
6372 defsubr (&Sx_uses_old_gtk_dialog);
6373 #if defined (USE_MOTIF) || defined (USE_GTK)
6374 defsubr (&Sx_file_dialog);
6375 #endif
6376
6377 #if defined (USE_GTK) && defined (HAVE_FREETYPE)
6378 defsubr (&Sx_select_font);
6379 #endif
6380 }