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