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