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