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