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