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