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