(gnus-blocked-images): Clarify privacy implications
[emacs.git] / src / xterm.c
blob3f956d950d87392b630c5276fa23b9d751dded3e
1 /* X Communication module for terminals which understand the X protocol.
3 Copyright (C) 1989, 1993-2018 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
20 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
21 /* Xt features made by Fred Pierresteguy. */
23 #include <config.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <math.h>
28 #include "lisp.h"
29 #include "blockinput.h"
31 /* This may include sys/types.h, and that somehow loses
32 if this is not done before the other system files. */
33 #include "xterm.h"
34 #include <X11/cursorfont.h>
36 /* If we have Xfixes extension, use it for pointer blanking. */
37 #ifdef HAVE_XFIXES
38 #include <X11/extensions/Xfixes.h>
39 #endif
41 /* Using Xft implies that XRender is available. */
42 #ifdef HAVE_XFT
43 #include <X11/extensions/Xrender.h>
44 #endif
46 #ifdef HAVE_XDBE
47 #include <X11/extensions/Xdbe.h>
48 #endif
50 /* Load sys/types.h if not already loaded.
51 In some systems loading it twice is suicidal. */
52 #ifndef makedev
53 #include <sys/types.h>
54 #endif /* makedev */
56 #include <sys/ioctl.h>
58 #include "systime.h"
60 #include <fcntl.h>
61 #include <errno.h>
62 #include <sys/stat.h>
63 #include "character.h"
64 #include "coding.h"
65 #include "composite.h"
66 #include "frame.h"
67 #include "dispextern.h"
68 #include "xwidget.h"
69 #include "fontset.h"
70 #include "termhooks.h"
71 #include "termopts.h"
72 #include "termchar.h"
73 #include "emacs-icon.h"
74 #include "buffer.h"
75 #include "window.h"
76 #include "keyboard.h"
77 #include "atimer.h"
78 #include "font.h"
79 #include "xsettings.h"
80 #include "sysselect.h"
81 #include "menu.h"
83 #ifdef USE_X_TOOLKIT
84 #include <X11/Shell.h>
85 #endif
87 #include <unistd.h>
89 #ifdef USE_GTK
90 #include "gtkutil.h"
91 #ifdef HAVE_GTK3
92 #include <X11/Xproto.h>
93 #endif
94 #endif
96 #if defined (USE_LUCID) || defined (USE_MOTIF)
97 #include "../lwlib/xlwmenu.h"
98 #endif
100 #ifdef USE_X_TOOLKIT
102 /* Include toolkit specific headers for the scroll bar widget. */
104 #ifdef USE_TOOLKIT_SCROLL_BARS
105 #if defined USE_MOTIF
106 #include <Xm/Xm.h> /* For LESSTIF_VERSION */
107 #include <Xm/ScrollBar.h>
108 #else /* !USE_MOTIF i.e. use Xaw */
110 #ifdef HAVE_XAW3D
111 #include <X11/Xaw3d/Simple.h>
112 #include <X11/Xaw3d/Scrollbar.h>
113 #include <X11/Xaw3d/ThreeD.h>
114 #else /* !HAVE_XAW3D */
115 #include <X11/Xaw/Simple.h>
116 #include <X11/Xaw/Scrollbar.h>
117 #endif /* !HAVE_XAW3D */
118 #ifndef XtNpickTop
119 #define XtNpickTop "pickTop"
120 #endif /* !XtNpickTop */
121 #endif /* !USE_MOTIF */
122 #endif /* USE_TOOLKIT_SCROLL_BARS */
124 #endif /* USE_X_TOOLKIT */
126 #ifdef USE_X_TOOLKIT
127 #include "widget.h"
128 #ifndef XtNinitialState
129 #define XtNinitialState "initialState"
130 #endif
131 #endif
133 #include "bitmaps/gray.xbm"
135 #ifdef HAVE_XKB
136 #include <X11/XKBlib.h>
137 #endif
139 /* Default to using XIM if available. */
140 #ifdef USE_XIM
141 bool use_xim = true;
142 #else
143 bool use_xim = false; /* configure --without-xim */
144 #endif
146 /* Non-zero means that a HELP_EVENT has been generated since Emacs
147 start. */
149 static bool any_help_event_p;
151 /* This is a chain of structures for all the X displays currently in
152 use. */
154 struct x_display_info *x_display_list;
156 #ifdef USE_X_TOOLKIT
158 /* The application context for Xt use. */
159 XtAppContext Xt_app_con;
160 static String Xt_default_resources[] = {0};
162 /* Non-zero means user is interacting with a toolkit scroll bar. */
163 static bool toolkit_scroll_bar_interaction;
165 #endif /* USE_X_TOOLKIT */
167 /* Non-zero timeout value means ignore next mouse click if it arrives
168 before that timeout elapses (i.e. as part of the same sequence of
169 events resulting from clicking on a frame to select it). */
171 static Time ignore_next_mouse_click_timeout;
173 /* Used locally within XTread_socket. */
175 static int x_noop_count;
177 #ifdef USE_GTK
178 /* The name of the Emacs icon file. */
179 static Lisp_Object xg_default_icon_file;
180 #endif
182 /* Some functions take this as char *, not const char *. */
183 static char emacs_class[] = EMACS_CLASS;
185 enum xembed_info
187 XEMBED_MAPPED = 1 << 0
190 enum xembed_message
192 XEMBED_EMBEDDED_NOTIFY = 0,
193 XEMBED_WINDOW_ACTIVATE = 1,
194 XEMBED_WINDOW_DEACTIVATE = 2,
195 XEMBED_REQUEST_FOCUS = 3,
196 XEMBED_FOCUS_IN = 4,
197 XEMBED_FOCUS_OUT = 5,
198 XEMBED_FOCUS_NEXT = 6,
199 XEMBED_FOCUS_PREV = 7,
201 XEMBED_MODALITY_ON = 10,
202 XEMBED_MODALITY_OFF = 11,
203 XEMBED_REGISTER_ACCELERATOR = 12,
204 XEMBED_UNREGISTER_ACCELERATOR = 13,
205 XEMBED_ACTIVATE_ACCELERATOR = 14
208 static void x_free_cr_resources (struct frame *);
209 static bool x_alloc_nearest_color_1 (Display *, Colormap, XColor *);
210 static void x_raise_frame (struct frame *);
211 static void x_lower_frame (struct frame *);
212 static int x_io_error_quitter (Display *);
213 static struct terminal *x_create_terminal (struct x_display_info *);
214 static void x_frame_rehighlight (struct x_display_info *);
216 static void x_clip_to_row (struct window *, struct glyph_row *,
217 enum glyph_row_area, GC);
218 static struct scroll_bar *x_window_to_scroll_bar (Display *, Window, int);
219 static void x_scroll_bar_report_motion (struct frame **, Lisp_Object *,
220 enum scroll_bar_part *,
221 Lisp_Object *, Lisp_Object *,
222 Time *);
223 static void x_horizontal_scroll_bar_report_motion (struct frame **, Lisp_Object *,
224 enum scroll_bar_part *,
225 Lisp_Object *, Lisp_Object *,
226 Time *);
227 static bool x_handle_net_wm_state (struct frame *, const XPropertyEvent *);
228 static void x_check_fullscreen (struct frame *);
229 static void x_check_expected_move (struct frame *, int, int);
230 static void x_sync_with_move (struct frame *, int, int, bool);
231 static int handle_one_xevent (struct x_display_info *,
232 const XEvent *, int *,
233 struct input_event *);
234 #if ! (defined USE_X_TOOLKIT || defined USE_MOTIF) && defined USE_GTK
235 static int x_dispatch_event (XEvent *, Display *);
236 #endif
237 static void x_wm_set_window_state (struct frame *, int);
238 static void x_wm_set_icon_pixmap (struct frame *, ptrdiff_t);
239 static void x_initialize (void);
241 static bool get_current_wm_state (struct frame *, Window, int *, bool *);
243 /* Flush display of frame F. */
245 static void
246 x_flush (struct frame *f)
248 eassert (f && FRAME_X_P (f));
249 /* Don't call XFlush when it is not safe to redisplay; the X
250 connection may be broken. */
251 if (!NILP (Vinhibit_redisplay))
252 return;
254 block_input ();
255 XFlush (FRAME_X_DISPLAY (f));
256 unblock_input ();
260 /* Remove calls to XFlush by defining XFlush to an empty replacement.
261 Calls to XFlush should be unnecessary because the X output buffer
262 is flushed automatically as needed by calls to XPending,
263 XNextEvent, or XWindowEvent according to the XFlush man page.
264 XTread_socket calls XPending. Removing XFlush improves
265 performance. */
267 #define XFlush(DISPLAY) (void) 0
270 /***********************************************************************
271 Debugging
272 ***********************************************************************/
274 #if false
276 /* This is a function useful for recording debugging information about
277 the sequence of occurrences in this file. */
279 struct record
281 char *locus;
282 int type;
285 struct record event_record[100];
287 int event_record_index;
289 void
290 record_event (char *locus, int type)
292 if (event_record_index == ARRAYELTS (event_record))
293 event_record_index = 0;
295 event_record[event_record_index].locus = locus;
296 event_record[event_record_index].type = type;
297 event_record_index++;
300 #endif
302 #ifdef USE_CAIRO
304 #define FRAME_CR_CONTEXT(f) ((f)->output_data.x->cr_context)
305 #define FRAME_CR_SURFACE(f) ((f)->output_data.x->cr_surface)
307 static struct x_gc_ext_data *
308 x_gc_get_ext_data (struct frame *f, GC gc, int create_if_not_found_p)
310 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
311 XEDataObject object;
312 XExtData **head, *ext_data;
314 object.gc = gc;
315 head = XEHeadOfExtensionList (object);
316 ext_data = XFindOnExtensionList (head, dpyinfo->ext_codes->extension);
317 if (ext_data == NULL)
319 if (!create_if_not_found_p)
320 return NULL;
321 else
323 ext_data = xzalloc (sizeof (*ext_data));
324 ext_data->number = dpyinfo->ext_codes->extension;
325 ext_data->private_data = xzalloc (sizeof (struct x_gc_ext_data));
326 XAddToExtensionList (head, ext_data);
329 return (struct x_gc_ext_data *) ext_data->private_data;
332 static void
333 x_extension_initialize (struct x_display_info *dpyinfo)
335 XExtCodes *ext_codes = XAddExtension (dpyinfo->display);
337 dpyinfo->ext_codes = ext_codes;
340 static void
341 x_cr_destroy_surface (struct frame *f)
343 if (FRAME_CR_SURFACE (f))
345 cairo_t *cr = FRAME_CR_CONTEXT (f);
346 cairo_surface_destroy (FRAME_CR_SURFACE (f));
347 FRAME_CR_SURFACE (f) = 0;
348 if (cr) cairo_destroy (cr);
349 FRAME_CR_CONTEXT (f) = NULL;
353 cairo_t *
354 x_begin_cr_clip (struct frame *f, GC gc)
356 cairo_t *cr = FRAME_CR_CONTEXT (f);
358 if (!cr)
361 if (! FRAME_CR_SURFACE (f))
363 FRAME_CR_SURFACE (f) =
364 cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
365 FRAME_PIXEL_WIDTH (f),
366 FRAME_PIXEL_HEIGHT (f));
368 cr = cairo_create (FRAME_CR_SURFACE (f));
369 FRAME_CR_CONTEXT (f) = cr;
371 cairo_save (cr);
373 if (gc)
375 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 0);
377 if (gc_ext && gc_ext->n_clip_rects)
379 int i;
381 for (i = 0; i < gc_ext->n_clip_rects; i++)
382 cairo_rectangle (cr, gc_ext->clip_rects[i].x,
383 gc_ext->clip_rects[i].y,
384 gc_ext->clip_rects[i].width,
385 gc_ext->clip_rects[i].height);
386 cairo_clip (cr);
390 return cr;
393 void
394 x_end_cr_clip (struct frame *f)
396 cairo_restore (FRAME_CR_CONTEXT (f));
399 void
400 x_set_cr_source_with_gc_foreground (struct frame *f, GC gc)
402 XGCValues xgcv;
403 XColor color;
405 XGetGCValues (FRAME_X_DISPLAY (f), gc, GCForeground, &xgcv);
406 color.pixel = xgcv.foreground;
407 x_query_color (f, &color);
408 cairo_set_source_rgb (FRAME_CR_CONTEXT (f), color.red / 65535.0,
409 color.green / 65535.0, color.blue / 65535.0);
412 void
413 x_set_cr_source_with_gc_background (struct frame *f, GC gc)
415 XGCValues xgcv;
416 XColor color;
418 XGetGCValues (FRAME_X_DISPLAY (f), gc, GCBackground, &xgcv);
419 color.pixel = xgcv.background;
420 x_query_color (f, &color);
421 cairo_set_source_rgb (FRAME_CR_CONTEXT (f), color.red / 65535.0,
422 color.green / 65535.0, color.blue / 65535.0);
425 /* Fringe bitmaps. */
427 static int max_fringe_bmp = 0;
428 static cairo_pattern_t **fringe_bmp = 0;
430 static void
431 x_cr_define_fringe_bitmap (int which, unsigned short *bits, int h, int wd)
433 int i, stride;
434 cairo_surface_t *surface;
435 unsigned char *data;
436 cairo_pattern_t *pattern;
438 if (which >= max_fringe_bmp)
440 i = max_fringe_bmp;
441 max_fringe_bmp = which + 20;
442 fringe_bmp = (cairo_pattern_t **) xrealloc (fringe_bmp, max_fringe_bmp * sizeof (cairo_pattern_t *));
443 while (i < max_fringe_bmp)
444 fringe_bmp[i++] = 0;
447 block_input ();
449 surface = cairo_image_surface_create (CAIRO_FORMAT_A1, wd, h);
450 stride = cairo_image_surface_get_stride (surface);
451 data = cairo_image_surface_get_data (surface);
453 for (i = 0; i < h; i++)
455 *((unsigned short *) data) = bits[i];
456 data += stride;
459 cairo_surface_mark_dirty (surface);
460 pattern = cairo_pattern_create_for_surface (surface);
461 cairo_surface_destroy (surface);
463 unblock_input ();
465 fringe_bmp[which] = pattern;
468 static void
469 x_cr_destroy_fringe_bitmap (int which)
471 if (which >= max_fringe_bmp)
472 return;
474 if (fringe_bmp[which])
476 block_input ();
477 cairo_pattern_destroy (fringe_bmp[which]);
478 unblock_input ();
480 fringe_bmp[which] = 0;
483 static void
484 x_cr_draw_image (struct frame *f, GC gc, cairo_pattern_t *image,
485 int src_x, int src_y, int width, int height,
486 int dest_x, int dest_y, bool overlay_p)
488 cairo_t *cr;
489 cairo_matrix_t matrix;
490 cairo_surface_t *surface;
491 cairo_format_t format;
493 cr = x_begin_cr_clip (f, gc);
494 if (overlay_p)
495 cairo_rectangle (cr, dest_x, dest_y, width, height);
496 else
498 x_set_cr_source_with_gc_background (f, gc);
499 cairo_rectangle (cr, dest_x, dest_y, width, height);
500 cairo_fill_preserve (cr);
502 cairo_clip (cr);
503 cairo_matrix_init_translate (&matrix, src_x - dest_x, src_y - dest_y);
504 cairo_pattern_set_matrix (image, &matrix);
505 cairo_pattern_get_surface (image, &surface);
506 format = cairo_image_surface_get_format (surface);
507 if (format != CAIRO_FORMAT_A8 && format != CAIRO_FORMAT_A1)
509 cairo_set_source (cr, image);
510 cairo_fill (cr);
512 else
514 x_set_cr_source_with_gc_foreground (f, gc);
515 cairo_mask (cr, image);
517 x_end_cr_clip (f);
520 void
521 x_cr_draw_frame (cairo_t *cr, struct frame *f)
523 int width, height;
525 width = FRAME_PIXEL_WIDTH (f);
526 height = FRAME_PIXEL_HEIGHT (f);
528 x_free_cr_resources (f);
529 FRAME_CR_CONTEXT (f) = cr;
530 x_clear_area (f, 0, 0, width, height);
531 expose_frame (f, 0, 0, width, height);
532 FRAME_CR_CONTEXT (f) = NULL;
535 static cairo_status_t
536 x_cr_accumulate_data (void *closure, const unsigned char *data,
537 unsigned int length)
539 Lisp_Object *acc = (Lisp_Object *) closure;
541 *acc = Fcons (make_unibyte_string ((char const *) data, length), *acc);
543 return CAIRO_STATUS_SUCCESS;
546 static void
547 x_cr_destroy (Lisp_Object arg)
549 cairo_t *cr = (cairo_t *) XSAVE_POINTER (arg, 0);
551 block_input ();
552 cairo_destroy (cr);
553 unblock_input ();
556 Lisp_Object
557 x_cr_export_frames (Lisp_Object frames, cairo_surface_type_t surface_type)
559 struct frame *f;
560 cairo_surface_t *surface;
561 cairo_t *cr;
562 int width, height;
563 void (*surface_set_size_func) (cairo_surface_t *, double, double) = NULL;
564 Lisp_Object acc = Qnil;
565 ptrdiff_t count = SPECPDL_INDEX ();
567 specbind (Qredisplay_dont_pause, Qt);
568 redisplay_preserve_echo_area (31);
570 f = XFRAME (XCAR (frames));
571 frames = XCDR (frames);
572 width = FRAME_PIXEL_WIDTH (f);
573 height = FRAME_PIXEL_HEIGHT (f);
575 block_input ();
576 #ifdef CAIRO_HAS_PDF_SURFACE
577 if (surface_type == CAIRO_SURFACE_TYPE_PDF)
579 surface = cairo_pdf_surface_create_for_stream (x_cr_accumulate_data, &acc,
580 width, height);
581 surface_set_size_func = cairo_pdf_surface_set_size;
583 else
584 #endif
585 #ifdef CAIRO_HAS_PNG_FUNCTIONS
586 if (surface_type == CAIRO_SURFACE_TYPE_IMAGE)
587 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
588 else
589 #endif
590 #ifdef CAIRO_HAS_PS_SURFACE
591 if (surface_type == CAIRO_SURFACE_TYPE_PS)
593 surface = cairo_ps_surface_create_for_stream (x_cr_accumulate_data, &acc,
594 width, height);
595 surface_set_size_func = cairo_ps_surface_set_size;
597 else
598 #endif
599 #ifdef CAIRO_HAS_SVG_SURFACE
600 if (surface_type == CAIRO_SURFACE_TYPE_SVG)
601 surface = cairo_svg_surface_create_for_stream (x_cr_accumulate_data, &acc,
602 width, height);
603 else
604 #endif
605 abort ();
607 cr = cairo_create (surface);
608 cairo_surface_destroy (surface);
609 record_unwind_protect (x_cr_destroy, make_save_ptr (cr));
611 while (1)
613 x_free_cr_resources (f);
614 FRAME_CR_CONTEXT (f) = cr;
615 x_clear_area (f, 0, 0, width, height);
616 expose_frame (f, 0, 0, width, height);
617 FRAME_CR_CONTEXT (f) = NULL;
619 if (NILP (frames))
620 break;
622 cairo_surface_show_page (surface);
623 f = XFRAME (XCAR (frames));
624 frames = XCDR (frames);
625 width = FRAME_PIXEL_WIDTH (f);
626 height = FRAME_PIXEL_HEIGHT (f);
627 if (surface_set_size_func)
628 (*surface_set_size_func) (surface, width, height);
630 unblock_input ();
631 maybe_quit ();
632 block_input ();
635 #ifdef CAIRO_HAS_PNG_FUNCTIONS
636 if (surface_type == CAIRO_SURFACE_TYPE_IMAGE)
638 cairo_surface_flush (surface);
639 cairo_surface_write_to_png_stream (surface, x_cr_accumulate_data, &acc);
641 #endif
642 unblock_input ();
644 unbind_to (count, Qnil);
646 return CALLN (Fapply, intern ("concat"), Fnreverse (acc));
649 #endif /* USE_CAIRO */
651 static void
652 x_free_cr_resources (struct frame *f)
654 #ifdef USE_CAIRO
655 if (f == NULL)
657 Lisp_Object rest, frame;
658 FOR_EACH_FRAME (rest, frame)
659 if (FRAME_X_P (XFRAME (frame)))
660 x_free_cr_resources (XFRAME (frame));
662 else
664 cairo_t *cr = FRAME_CR_CONTEXT (f);
666 if (cr)
668 cairo_surface_t *surface = cairo_get_target (cr);
670 if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_XLIB)
672 cairo_destroy (cr);
673 FRAME_CR_CONTEXT (f) = NULL;
677 #endif
680 static void
681 x_set_clip_rectangles (struct frame *f, GC gc, XRectangle *rectangles, int n)
683 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, rectangles, n, Unsorted);
684 #ifdef USE_CAIRO
685 eassert (n >= 0 && n <= MAX_CLIP_RECTS);
688 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 1);
690 gc_ext->n_clip_rects = n;
691 memcpy (gc_ext->clip_rects, rectangles, sizeof (XRectangle) * n);
693 #endif
696 static void
697 x_reset_clip_rectangles (struct frame *f, GC gc)
699 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
700 #ifdef USE_CAIRO
702 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 0);
704 if (gc_ext)
705 gc_ext->n_clip_rects = 0;
707 #endif
710 static void
711 x_fill_rectangle (struct frame *f, GC gc, int x, int y, int width, int height)
713 #ifdef USE_CAIRO
714 cairo_t *cr;
716 cr = x_begin_cr_clip (f, gc);
717 x_set_cr_source_with_gc_foreground (f, gc);
718 cairo_rectangle (cr, x, y, width, height);
719 cairo_fill (cr);
720 x_end_cr_clip (f);
721 #else
722 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
723 gc, x, y, width, height);
724 #endif
727 static void
728 x_draw_rectangle (struct frame *f, GC gc, int x, int y, int width, int height)
730 #ifdef USE_CAIRO
731 cairo_t *cr;
733 cr = x_begin_cr_clip (f, gc);
734 x_set_cr_source_with_gc_foreground (f, gc);
735 cairo_rectangle (cr, x + 0.5, y + 0.5, width, height);
736 cairo_set_line_width (cr, 1);
737 cairo_stroke (cr);
738 x_end_cr_clip (f);
739 #else
740 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
741 gc, x, y, width, height);
742 #endif
745 static void
746 x_clear_window (struct frame *f)
748 #ifdef USE_CAIRO
749 cairo_t *cr;
751 cr = x_begin_cr_clip (f, NULL);
752 x_set_cr_source_with_gc_background (f, f->output_data.x->normal_gc);
753 cairo_paint (cr);
754 x_end_cr_clip (f);
755 #else
756 if (FRAME_X_DOUBLE_BUFFERED_P (f))
757 x_clear_area (f, 0, 0, FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f));
758 else
759 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
760 #endif
763 #ifdef USE_CAIRO
764 static void
765 x_fill_trapezoid_for_relief (struct frame *f, GC gc, int x, int y,
766 int width, int height, int top_p)
768 cairo_t *cr;
770 cr = x_begin_cr_clip (f, gc);
771 x_set_cr_source_with_gc_foreground (f, gc);
772 cairo_move_to (cr, top_p ? x : x + height, y);
773 cairo_line_to (cr, x, y + height);
774 cairo_line_to (cr, top_p ? x + width - height : x + width, y + height);
775 cairo_line_to (cr, x + width, y);
776 cairo_fill (cr);
777 x_end_cr_clip (f);
780 enum corners
782 CORNER_BOTTOM_RIGHT, /* 0 -> pi/2 */
783 CORNER_BOTTOM_LEFT, /* pi/2 -> pi */
784 CORNER_TOP_LEFT, /* pi -> 3pi/2 */
785 CORNER_TOP_RIGHT, /* 3pi/2 -> 2pi */
786 CORNER_LAST
789 static void
790 x_erase_corners_for_relief (struct frame *f, GC gc, int x, int y,
791 int width, int height,
792 double radius, double margin, int corners)
794 cairo_t *cr;
795 int i;
797 cr = x_begin_cr_clip (f, gc);
798 x_set_cr_source_with_gc_background (f, gc);
799 for (i = 0; i < CORNER_LAST; i++)
800 if (corners & (1 << i))
802 double xm, ym, xc, yc;
804 if (i == CORNER_TOP_LEFT || i == CORNER_BOTTOM_LEFT)
805 xm = x - margin, xc = xm + radius;
806 else
807 xm = x + width + margin, xc = xm - radius;
808 if (i == CORNER_TOP_LEFT || i == CORNER_TOP_RIGHT)
809 ym = y - margin, yc = ym + radius;
810 else
811 ym = y + height + margin, yc = ym - radius;
813 cairo_move_to (cr, xm, ym);
814 cairo_arc (cr, xc, yc, radius, i * M_PI_2, (i + 1) * M_PI_2);
816 cairo_clip (cr);
817 cairo_rectangle (cr, x, y, width, height);
818 cairo_fill (cr);
819 x_end_cr_clip (f);
822 static void
823 x_draw_horizontal_wave (struct frame *f, GC gc, int x, int y,
824 int width, int height, int wave_length)
826 cairo_t *cr;
827 double dx = wave_length, dy = height - 1;
828 int xoffset, n;
830 cr = x_begin_cr_clip (f, gc);
831 x_set_cr_source_with_gc_foreground (f, gc);
832 cairo_rectangle (cr, x, y, width, height);
833 cairo_clip (cr);
835 if (x >= 0)
837 xoffset = x % (wave_length * 2);
838 if (xoffset == 0)
839 xoffset = wave_length * 2;
841 else
842 xoffset = x % (wave_length * 2) + wave_length * 2;
843 n = (width + xoffset) / wave_length + 1;
844 if (xoffset > wave_length)
846 xoffset -= wave_length;
847 --n;
848 y += height - 1;
849 dy = -dy;
852 cairo_move_to (cr, x - xoffset + 0.5, y + 0.5);
853 while (--n >= 0)
855 cairo_rel_line_to (cr, dx, dy);
856 dy = -dy;
858 cairo_set_line_width (cr, 1);
859 cairo_stroke (cr);
860 x_end_cr_clip (f);
862 #endif
865 /* Return the struct x_display_info corresponding to DPY. */
867 struct x_display_info *
868 x_display_info_for_display (Display *dpy)
870 struct x_display_info *dpyinfo;
872 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
873 if (dpyinfo->display == dpy)
874 return dpyinfo;
876 return 0;
879 static Window
880 x_find_topmost_parent (struct frame *f)
882 struct x_output *x = f->output_data.x;
883 Window win = None, wi = x->parent_desc;
884 Display *dpy = FRAME_X_DISPLAY (f);
886 while (wi != FRAME_DISPLAY_INFO (f)->root_window)
888 Window root;
889 Window *children;
890 unsigned int nchildren;
892 win = wi;
893 if (XQueryTree (dpy, win, &root, &wi, &children, &nchildren))
894 XFree (children);
895 else
896 break;
899 return win;
902 #define OPAQUE 0xffffffff
904 void
905 x_set_frame_alpha (struct frame *f)
907 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
908 Display *dpy = FRAME_X_DISPLAY (f);
909 Window win = FRAME_OUTER_WINDOW (f);
910 double alpha = 1.0;
911 double alpha_min = 1.0;
912 unsigned long opac;
913 Window parent;
915 if (dpyinfo->x_highlight_frame == f)
916 alpha = f->alpha[0];
917 else
918 alpha = f->alpha[1];
920 if (FLOATP (Vframe_alpha_lower_limit))
921 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
922 else if (INTEGERP (Vframe_alpha_lower_limit))
923 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
925 if (alpha < 0.0)
926 return;
927 else if (alpha > 1.0)
928 alpha = 1.0;
929 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
930 alpha = alpha_min;
932 opac = alpha * OPAQUE;
934 x_catch_errors (dpy);
936 /* If there is a parent from the window manager, put the property there
937 also, to work around broken window managers that fail to do that.
938 Do this unconditionally as this function is called on reparent when
939 alpha has not changed on the frame. */
941 if (!FRAME_PARENT_FRAME (f))
943 parent = x_find_topmost_parent (f);
944 if (parent != None)
945 XChangeProperty (dpy, parent, dpyinfo->Xatom_net_wm_window_opacity,
946 XA_CARDINAL, 32, PropModeReplace,
947 (unsigned char *) &opac, 1);
950 /* return unless necessary */
952 unsigned char *data;
953 Atom actual;
954 int rc, format;
955 unsigned long n, left;
957 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
958 0, 1, False, XA_CARDINAL,
959 &actual, &format, &n, &left,
960 &data);
962 if (rc == Success && actual != None)
964 unsigned long value = *(unsigned long *)data;
965 XFree (data);
966 if (value == opac)
968 x_uncatch_errors ();
969 return;
974 XChangeProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
975 XA_CARDINAL, 32, PropModeReplace,
976 (unsigned char *) &opac, 1);
977 x_uncatch_errors ();
980 /***********************************************************************
981 Starting and ending an update
982 ***********************************************************************/
984 /* Start an update of frame F. This function is installed as a hook
985 for update_begin, i.e. it is called when update_begin is called.
986 This function is called prior to calls to x_update_window_begin for
987 each window being updated. Currently, there is nothing to do here
988 because all interesting stuff is done on a window basis. */
990 static void
991 x_update_begin (struct frame *f)
993 #ifdef USE_CAIRO
994 if (FRAME_TOOLTIP_P (f) && !FRAME_VISIBLE_P (f))
995 return;
997 if (! FRAME_CR_SURFACE (f))
999 int width, height;
1000 #ifdef USE_GTK
1001 if (FRAME_GTK_WIDGET (f))
1003 GdkWindow *w = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
1004 width = gdk_window_get_width (w);
1005 height = gdk_window_get_height (w);
1007 else
1008 #endif
1010 width = FRAME_PIXEL_WIDTH (f);
1011 height = FRAME_PIXEL_HEIGHT (f);
1012 if (! FRAME_EXTERNAL_TOOL_BAR (f))
1013 height += FRAME_TOOL_BAR_HEIGHT (f);
1014 if (! FRAME_EXTERNAL_MENU_BAR (f))
1015 height += FRAME_MENU_BAR_HEIGHT (f);
1018 if (width > 0 && height > 0)
1020 block_input();
1021 FRAME_CR_SURFACE (f) = cairo_image_surface_create
1022 (CAIRO_FORMAT_ARGB32, width, height);
1023 unblock_input();
1026 #endif /* USE_CAIRO */
1029 /* Start update of window W. */
1031 static void
1032 x_update_window_begin (struct window *w)
1034 struct frame *f = XFRAME (WINDOW_FRAME (w));
1035 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
1037 w->output_cursor = w->cursor;
1039 block_input ();
1041 if (f == hlinfo->mouse_face_mouse_frame)
1043 /* Don't do highlighting for mouse motion during the update. */
1044 hlinfo->mouse_face_defer = true;
1046 /* If F needs to be redrawn, simply forget about any prior mouse
1047 highlighting. */
1048 if (FRAME_GARBAGED_P (f))
1049 hlinfo->mouse_face_window = Qnil;
1052 unblock_input ();
1056 /* Draw a vertical window border from (x,y0) to (x,y1) */
1058 static void
1059 x_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
1061 struct frame *f = XFRAME (WINDOW_FRAME (w));
1062 struct face *face;
1064 face = FACE_FROM_ID_OR_NULL (f, VERTICAL_BORDER_FACE_ID);
1065 if (face)
1066 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
1067 face->foreground);
1069 #ifdef USE_CAIRO
1070 x_fill_rectangle (f, f->output_data.x->normal_gc, x, y0, 1, y1 - y0);
1071 #else
1072 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
1073 f->output_data.x->normal_gc, x, y0, x, y1);
1074 #endif
1077 /* Draw a window divider from (x0,y0) to (x1,y1) */
1079 static void
1080 x_draw_window_divider (struct window *w, int x0, int x1, int y0, int y1)
1082 struct frame *f = XFRAME (WINDOW_FRAME (w));
1083 struct face *face = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_FACE_ID);
1084 struct face *face_first
1085 = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
1086 struct face *face_last
1087 = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
1088 unsigned long color = face ? face->foreground : FRAME_FOREGROUND_PIXEL (f);
1089 unsigned long color_first = (face_first
1090 ? face_first->foreground
1091 : FRAME_FOREGROUND_PIXEL (f));
1092 unsigned long color_last = (face_last
1093 ? face_last->foreground
1094 : FRAME_FOREGROUND_PIXEL (f));
1095 Display *display = FRAME_X_DISPLAY (f);
1097 if ((y1 - y0 > x1 - x0) && (x1 - x0 >= 3))
1098 /* A vertical divider, at least three pixels wide: Draw first and
1099 last pixels differently. */
1101 XSetForeground (display, f->output_data.x->normal_gc, color_first);
1102 x_fill_rectangle (f, f->output_data.x->normal_gc,
1103 x0, y0, 1, y1 - y0);
1104 XSetForeground (display, f->output_data.x->normal_gc, color);
1105 x_fill_rectangle (f, f->output_data.x->normal_gc,
1106 x0 + 1, y0, x1 - x0 - 2, y1 - y0);
1107 XSetForeground (display, f->output_data.x->normal_gc, color_last);
1108 x_fill_rectangle (f, f->output_data.x->normal_gc,
1109 x1 - 1, y0, 1, y1 - y0);
1111 else if ((x1 - x0 > y1 - y0) && (y1 - y0 >= 3))
1112 /* A horizontal divider, at least three pixels high: Draw first and
1113 last pixels differently. */
1115 XSetForeground (display, f->output_data.x->normal_gc, color_first);
1116 x_fill_rectangle (f, f->output_data.x->normal_gc,
1117 x0, y0, x1 - x0, 1);
1118 XSetForeground (display, f->output_data.x->normal_gc, color);
1119 x_fill_rectangle (f, f->output_data.x->normal_gc,
1120 x0, y0 + 1, x1 - x0, y1 - y0 - 2);
1121 XSetForeground (display, f->output_data.x->normal_gc, color_last);
1122 x_fill_rectangle (f, f->output_data.x->normal_gc,
1123 x0, y1 - 1, x1 - x0, 1);
1125 else
1127 /* In any other case do not draw the first and last pixels
1128 differently. */
1129 XSetForeground (display, f->output_data.x->normal_gc, color);
1130 x_fill_rectangle (f, f->output_data.x->normal_gc,
1131 x0, y0, x1 - x0, y1 - y0);
1135 /* End update of window W.
1137 Draw vertical borders between horizontally adjacent windows, and
1138 display W's cursor if CURSOR_ON_P is non-zero.
1140 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
1141 glyphs in mouse-face were overwritten. In that case we have to
1142 make sure that the mouse-highlight is properly redrawn.
1144 W may be a menu bar pseudo-window in case we don't have X toolkit
1145 support. Such windows don't have a cursor, so don't display it
1146 here. */
1148 static void
1149 x_update_window_end (struct window *w, bool cursor_on_p,
1150 bool mouse_face_overwritten_p)
1152 if (!w->pseudo_window_p)
1154 block_input ();
1156 if (cursor_on_p)
1157 display_and_set_cursor (w, true,
1158 w->output_cursor.hpos, w->output_cursor.vpos,
1159 w->output_cursor.x, w->output_cursor.y);
1161 if (draw_window_fringes (w, true))
1163 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
1164 x_draw_right_divider (w);
1165 else
1166 x_draw_vertical_border (w);
1169 unblock_input ();
1172 /* If a row with mouse-face was overwritten, arrange for
1173 XTframe_up_to_date to redisplay the mouse highlight. */
1174 if (mouse_face_overwritten_p)
1176 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
1178 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
1179 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
1180 hlinfo->mouse_face_window = Qnil;
1184 /* Show the frame back buffer. If frame is double-buffered,
1185 atomically publish to the user's screen graphics updates made since
1186 the last call to show_back_buffer. */
1187 static void
1188 show_back_buffer (struct frame *f)
1190 block_input ();
1191 if (FRAME_X_DOUBLE_BUFFERED_P (f))
1193 #ifdef HAVE_XDBE
1194 XdbeSwapInfo swap_info;
1195 memset (&swap_info, 0, sizeof (swap_info));
1196 swap_info.swap_window = FRAME_X_WINDOW (f);
1197 swap_info.swap_action = XdbeCopied;
1198 XdbeSwapBuffers (FRAME_X_DISPLAY (f), &swap_info, 1);
1199 #else
1200 eassert (!"should have back-buffer only with XDBE");
1201 #endif
1203 FRAME_X_NEED_BUFFER_FLIP (f) = false;
1204 unblock_input ();
1207 /* Updates back buffer and flushes changes to display. Called from
1208 minibuf read code. Note that we display the back buffer even if
1209 buffer flipping is blocked. */
1210 static void
1211 x_flip_and_flush (struct frame *f)
1213 block_input ();
1214 if (FRAME_X_NEED_BUFFER_FLIP (f))
1215 show_back_buffer (f);
1216 x_flush (f);
1217 unblock_input ();
1220 /* End update of frame F. This function is installed as a hook in
1221 update_end. */
1223 static void
1224 x_update_end (struct frame *f)
1226 /* Mouse highlight may be displayed again. */
1227 MOUSE_HL_INFO (f)->mouse_face_defer = false;
1229 #ifdef USE_CAIRO
1230 if (FRAME_CR_SURFACE (f))
1232 cairo_t *cr;
1233 cairo_surface_t *surface;
1234 int width, height;
1236 block_input ();
1237 width = FRAME_PIXEL_WIDTH (f);
1238 height = FRAME_PIXEL_HEIGHT (f);
1239 if (! FRAME_EXTERNAL_TOOL_BAR (f))
1240 height += FRAME_TOOL_BAR_HEIGHT (f);
1241 if (! FRAME_EXTERNAL_MENU_BAR (f))
1242 height += FRAME_MENU_BAR_HEIGHT (f);
1243 surface = cairo_xlib_surface_create (FRAME_X_DISPLAY (f),
1244 FRAME_X_DRAWABLE (f),
1245 FRAME_DISPLAY_INFO (f)->visual,
1246 width,
1247 height);
1248 cr = cairo_create (surface);
1249 cairo_surface_destroy (surface);
1251 cairo_set_source_surface (cr, FRAME_CR_SURFACE (f), 0, 0);
1252 cairo_paint (cr);
1253 cairo_destroy (cr);
1254 unblock_input ();
1256 #endif
1258 #ifndef XFlush
1259 block_input ();
1260 XFlush (FRAME_X_DISPLAY (f));
1261 unblock_input ();
1262 #endif
1265 /* This function is called from various places in xdisp.c
1266 whenever a complete update has been performed. */
1268 static void
1269 XTframe_up_to_date (struct frame *f)
1271 eassert (FRAME_X_P (f));
1272 block_input ();
1273 FRAME_MOUSE_UPDATE (f);
1274 if (!buffer_flipping_blocked_p () && FRAME_X_NEED_BUFFER_FLIP (f))
1275 show_back_buffer (f);
1276 unblock_input ();
1279 static void
1280 XTbuffer_flipping_unblocked_hook (struct frame *f)
1282 if (FRAME_X_NEED_BUFFER_FLIP (f))
1283 show_back_buffer (f);
1287 * x_clear_under_internal_border:
1289 * Clear area of frame F's internal border. If the internal border face
1290 * of F has been specified (is not null), fill the area with that face.
1292 void
1293 x_clear_under_internal_border (struct frame *f)
1295 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
1297 int border = FRAME_INTERNAL_BORDER_WIDTH (f);
1298 int width = FRAME_PIXEL_WIDTH (f);
1299 int height = FRAME_PIXEL_HEIGHT (f);
1300 #ifdef USE_GTK
1301 int margin = 0;
1302 #else
1303 int margin = FRAME_TOP_MARGIN_HEIGHT (f);
1304 #endif
1305 struct face *face = FACE_FROM_ID_OR_NULL (f, INTERNAL_BORDER_FACE_ID);
1307 block_input ();
1309 if (face)
1311 unsigned long color = face->background;
1312 Display *display = FRAME_X_DISPLAY (f);
1313 GC gc = f->output_data.x->normal_gc;
1315 XSetForeground (display, gc, color);
1316 x_fill_rectangle (f, gc, 0, margin, width, border);
1317 x_fill_rectangle (f, gc, 0, 0, border, height);
1318 x_fill_rectangle (f, gc, width - border, 0, border, height);
1319 x_fill_rectangle (f, gc, 0, height - border, width, border);
1320 XSetForeground (display, gc, FRAME_FOREGROUND_PIXEL (f));
1322 else
1324 x_clear_area (f, 0, 0, border, height);
1325 x_clear_area (f, 0, margin, width, border);
1326 x_clear_area (f, width - border, 0, border, height);
1327 x_clear_area (f, 0, height - border, width, border);
1330 unblock_input ();
1334 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
1335 arrow bitmaps, or clear the fringes if no bitmaps are required
1336 before DESIRED_ROW is made current. This function is called from
1337 update_window_line only if it is known that there are differences
1338 between bitmaps to be drawn between current row and DESIRED_ROW. */
1340 static void
1341 x_after_update_window_line (struct window *w, struct glyph_row *desired_row)
1343 eassert (w);
1345 if (!desired_row->mode_line_p && !w->pseudo_window_p)
1346 desired_row->redraw_fringe_bitmaps_p = true;
1348 #ifdef USE_X_TOOLKIT
1349 /* When a window has disappeared, make sure that no rest of
1350 full-width rows stays visible in the internal border. Could
1351 check here if updated window is the leftmost/rightmost window,
1352 but I guess it's not worth doing since vertically split windows
1353 are almost never used, internal border is rarely set, and the
1354 overhead is very small. */
1356 struct frame *f;
1357 int width, height;
1359 if (windows_or_buffers_changed
1360 && desired_row->full_width_p
1361 && (f = XFRAME (w->frame),
1362 width = FRAME_INTERNAL_BORDER_WIDTH (f),
1363 width != 0)
1364 && (height = desired_row->visible_height,
1365 height > 0))
1367 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
1368 struct face *face = FACE_FROM_ID_OR_NULL (f, INTERNAL_BORDER_FACE_ID);
1370 block_input ();
1371 if (face)
1373 unsigned long color = face->background;
1374 Display *display = FRAME_X_DISPLAY (f);
1375 GC gc = f->output_data.x->normal_gc;
1377 XSetForeground (display, gc, color);
1378 x_fill_rectangle (f, gc, 0, y, width, height);
1379 x_fill_rectangle (f, gc, FRAME_PIXEL_WIDTH (f) - width, y,
1380 width, height);
1381 XSetForeground (display, gc, FRAME_FOREGROUND_PIXEL (f));
1383 else
1385 x_clear_area (f, 0, y, width, height);
1386 x_clear_area (f, FRAME_PIXEL_WIDTH (f) - width, y, width, height);
1388 unblock_input ();
1391 #endif
1394 static void
1395 x_draw_fringe_bitmap (struct window *w, struct glyph_row *row, struct draw_fringe_bitmap_params *p)
1397 struct frame *f = XFRAME (WINDOW_FRAME (w));
1398 Display *display = FRAME_X_DISPLAY (f);
1399 GC gc = f->output_data.x->normal_gc;
1400 struct face *face = p->face;
1402 /* Must clip because of partially visible lines. */
1403 x_clip_to_row (w, row, ANY_AREA, gc);
1405 if (p->bx >= 0 && !p->overlay_p)
1407 /* In case the same realized face is used for fringes and
1408 for something displayed in the text (e.g. face `region' on
1409 mono-displays, the fill style may have been changed to
1410 FillSolid in x_draw_glyph_string_background. */
1411 if (face->stipple)
1412 XSetFillStyle (display, face->gc, FillOpaqueStippled);
1413 else
1414 XSetForeground (display, face->gc, face->background);
1416 x_fill_rectangle (f, face->gc, p->bx, p->by, p->nx, p->ny);
1418 if (!face->stipple)
1419 XSetForeground (display, face->gc, face->foreground);
1422 #ifdef USE_CAIRO
1423 if (p->which && p->which < max_fringe_bmp)
1425 XGCValues gcv;
1427 XGetGCValues (display, gc, GCForeground | GCBackground, &gcv);
1428 XSetForeground (display, gc, (p->cursor_p
1429 ? (p->overlay_p ? face->background
1430 : f->output_data.x->cursor_pixel)
1431 : face->foreground));
1432 XSetBackground (display, gc, face->background);
1433 x_cr_draw_image (f, gc, fringe_bmp[p->which], 0, p->dh,
1434 p->wd, p->h, p->x, p->y, p->overlay_p);
1435 XSetForeground (display, gc, gcv.foreground);
1436 XSetBackground (display, gc, gcv.background);
1438 #else /* not USE_CAIRO */
1439 if (p->which)
1441 Drawable drawable = FRAME_X_DRAWABLE (f);
1442 char *bits;
1443 Pixmap pixmap, clipmask = (Pixmap) 0;
1444 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
1445 XGCValues gcv;
1447 if (p->wd > 8)
1448 bits = (char *) (p->bits + p->dh);
1449 else
1450 bits = (char *) p->bits + p->dh;
1452 /* Draw the bitmap. I believe these small pixmaps can be cached
1453 by the server. */
1454 pixmap = XCreatePixmapFromBitmapData (display, drawable, bits, p->wd, p->h,
1455 (p->cursor_p
1456 ? (p->overlay_p ? face->background
1457 : f->output_data.x->cursor_pixel)
1458 : face->foreground),
1459 face->background, depth);
1461 if (p->overlay_p)
1463 clipmask = XCreatePixmapFromBitmapData (display,
1464 FRAME_DISPLAY_INFO (f)->root_window,
1465 bits, p->wd, p->h,
1466 1, 0, 1);
1467 gcv.clip_mask = clipmask;
1468 gcv.clip_x_origin = p->x;
1469 gcv.clip_y_origin = p->y;
1470 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
1473 XCopyArea (display, pixmap, drawable, gc, 0, 0,
1474 p->wd, p->h, p->x, p->y);
1475 XFreePixmap (display, pixmap);
1477 if (p->overlay_p)
1479 gcv.clip_mask = (Pixmap) 0;
1480 XChangeGC (display, gc, GCClipMask, &gcv);
1481 XFreePixmap (display, clipmask);
1484 #endif /* not USE_CAIRO */
1486 x_reset_clip_rectangles (f, gc);
1489 /***********************************************************************
1490 Glyph display
1491 ***********************************************************************/
1495 static void x_set_glyph_string_clipping (struct glyph_string *);
1496 static void x_set_glyph_string_gc (struct glyph_string *);
1497 static void x_draw_glyph_string_foreground (struct glyph_string *);
1498 static void x_draw_composite_glyph_string_foreground (struct glyph_string *);
1499 static void x_draw_glyph_string_box (struct glyph_string *);
1500 static void x_draw_glyph_string (struct glyph_string *);
1501 static _Noreturn void x_delete_glyphs (struct frame *, int);
1502 static void x_compute_glyph_string_overhangs (struct glyph_string *);
1503 static void x_set_cursor_gc (struct glyph_string *);
1504 static void x_set_mode_line_face_gc (struct glyph_string *);
1505 static void x_set_mouse_face_gc (struct glyph_string *);
1506 static bool x_alloc_lighter_color (struct frame *, Display *, Colormap,
1507 unsigned long *, double, int);
1508 static void x_setup_relief_color (struct frame *, struct relief *,
1509 double, int, unsigned long);
1510 static void x_setup_relief_colors (struct glyph_string *);
1511 static void x_draw_image_glyph_string (struct glyph_string *);
1512 static void x_draw_image_relief (struct glyph_string *);
1513 static void x_draw_image_foreground (struct glyph_string *);
1514 static void x_draw_image_foreground_1 (struct glyph_string *, Pixmap);
1515 static void x_clear_glyph_string_rect (struct glyph_string *, int,
1516 int, int, int);
1517 static void x_draw_relief_rect (struct frame *, int, int, int, int,
1518 int, bool, bool, bool, bool, bool,
1519 XRectangle *);
1520 static void x_draw_box_rect (struct glyph_string *, int, int, int, int,
1521 int, bool, bool, XRectangle *);
1522 static void x_scroll_bar_clear (struct frame *);
1524 #ifdef GLYPH_DEBUG
1525 static void x_check_font (struct frame *, struct font *);
1526 #endif
1529 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
1530 face. */
1532 static void
1533 x_set_cursor_gc (struct glyph_string *s)
1535 if (s->font == FRAME_FONT (s->f)
1536 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
1537 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
1538 && !s->cmp)
1539 s->gc = s->f->output_data.x->cursor_gc;
1540 else
1542 /* Cursor on non-default face: must merge. */
1543 XGCValues xgcv;
1544 unsigned long mask;
1546 xgcv.background = s->f->output_data.x->cursor_pixel;
1547 xgcv.foreground = s->face->background;
1549 /* If the glyph would be invisible, try a different foreground. */
1550 if (xgcv.foreground == xgcv.background)
1551 xgcv.foreground = s->face->foreground;
1552 if (xgcv.foreground == xgcv.background)
1553 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1554 if (xgcv.foreground == xgcv.background)
1555 xgcv.foreground = s->face->foreground;
1557 /* Make sure the cursor is distinct from text in this face. */
1558 if (xgcv.background == s->face->background
1559 && xgcv.foreground == s->face->foreground)
1561 xgcv.background = s->face->foreground;
1562 xgcv.foreground = s->face->background;
1565 IF_DEBUG (x_check_font (s->f, s->font));
1566 xgcv.graphics_exposures = False;
1567 mask = GCForeground | GCBackground | GCGraphicsExposures;
1569 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1570 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1571 mask, &xgcv);
1572 else
1573 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1574 = XCreateGC (s->display, FRAME_X_DRAWABLE (s->f), mask, &xgcv);
1576 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1581 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1583 static void
1584 x_set_mouse_face_gc (struct glyph_string *s)
1586 int face_id;
1587 struct face *face;
1589 /* What face has to be used last for the mouse face? */
1590 face_id = MOUSE_HL_INFO (s->f)->mouse_face_face_id;
1591 face = FACE_FROM_ID_OR_NULL (s->f, face_id);
1592 if (face == NULL)
1593 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1595 if (s->first_glyph->type == CHAR_GLYPH)
1596 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1597 else
1598 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1599 s->face = FACE_FROM_ID (s->f, face_id);
1600 prepare_face_for_display (s->f, s->face);
1602 if (s->font == s->face->font)
1603 s->gc = s->face->gc;
1604 else
1606 /* Otherwise construct scratch_cursor_gc with values from FACE
1607 except for FONT. */
1608 XGCValues xgcv;
1609 unsigned long mask;
1611 xgcv.background = s->face->background;
1612 xgcv.foreground = s->face->foreground;
1613 xgcv.graphics_exposures = False;
1614 mask = GCForeground | GCBackground | GCGraphicsExposures;
1616 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1617 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1618 mask, &xgcv);
1619 else
1620 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1621 = XCreateGC (s->display, FRAME_X_DRAWABLE (s->f), mask, &xgcv);
1623 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1626 eassert (s->gc != 0);
1630 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1631 Faces to use in the mode line have already been computed when the
1632 matrix was built, so there isn't much to do, here. */
1634 static void
1635 x_set_mode_line_face_gc (struct glyph_string *s)
1637 s->gc = s->face->gc;
1641 /* Set S->gc of glyph string S for drawing that glyph string. Set
1642 S->stippled_p to a non-zero value if the face of S has a stipple
1643 pattern. */
1645 static void
1646 x_set_glyph_string_gc (struct glyph_string *s)
1648 prepare_face_for_display (s->f, s->face);
1650 if (s->hl == DRAW_NORMAL_TEXT)
1652 s->gc = s->face->gc;
1653 s->stippled_p = s->face->stipple != 0;
1655 else if (s->hl == DRAW_INVERSE_VIDEO)
1657 x_set_mode_line_face_gc (s);
1658 s->stippled_p = s->face->stipple != 0;
1660 else if (s->hl == DRAW_CURSOR)
1662 x_set_cursor_gc (s);
1663 s->stippled_p = false;
1665 else if (s->hl == DRAW_MOUSE_FACE)
1667 x_set_mouse_face_gc (s);
1668 s->stippled_p = s->face->stipple != 0;
1670 else if (s->hl == DRAW_IMAGE_RAISED
1671 || s->hl == DRAW_IMAGE_SUNKEN)
1673 s->gc = s->face->gc;
1674 s->stippled_p = s->face->stipple != 0;
1676 else
1677 emacs_abort ();
1679 /* GC must have been set. */
1680 eassert (s->gc != 0);
1684 /* Set clipping for output of glyph string S. S may be part of a mode
1685 line or menu if we don't have X toolkit support. */
1687 static void
1688 x_set_glyph_string_clipping (struct glyph_string *s)
1690 XRectangle *r = s->clip;
1691 int n = get_glyph_string_clip_rects (s, r, 2);
1693 if (n > 0)
1694 x_set_clip_rectangles (s->f, s->gc, r, n);
1695 s->num_clips = n;
1699 /* Set SRC's clipping for output of glyph string DST. This is called
1700 when we are drawing DST's left_overhang or right_overhang only in
1701 the area of SRC. */
1703 static void
1704 x_set_glyph_string_clipping_exactly (struct glyph_string *src, struct glyph_string *dst)
1706 XRectangle r;
1708 r.x = src->x;
1709 r.width = src->width;
1710 r.y = src->y;
1711 r.height = src->height;
1712 dst->clip[0] = r;
1713 dst->num_clips = 1;
1714 x_set_clip_rectangles (dst->f, dst->gc, &r, 1);
1718 /* RIF:
1719 Compute left and right overhang of glyph string S. */
1721 static void
1722 x_compute_glyph_string_overhangs (struct glyph_string *s)
1724 if (s->cmp == NULL
1725 && (s->first_glyph->type == CHAR_GLYPH
1726 || s->first_glyph->type == COMPOSITE_GLYPH))
1728 struct font_metrics metrics;
1730 if (s->first_glyph->type == CHAR_GLYPH)
1732 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1733 struct font *font = s->font;
1734 int i;
1736 for (i = 0; i < s->nchars; i++)
1737 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1738 font->driver->text_extents (font, code, s->nchars, &metrics);
1740 else
1742 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1744 composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
1746 s->right_overhang = (metrics.rbearing > metrics.width
1747 ? metrics.rbearing - metrics.width : 0);
1748 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1750 else if (s->cmp)
1752 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1753 s->left_overhang = - s->cmp->lbearing;
1758 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1760 static void
1761 x_clear_glyph_string_rect (struct glyph_string *s, int x, int y, int w, int h)
1763 XGCValues xgcv;
1764 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1765 XSetForeground (s->display, s->gc, xgcv.background);
1766 x_fill_rectangle (s->f, s->gc, x, y, w, h);
1767 XSetForeground (s->display, s->gc, xgcv.foreground);
1771 /* Draw the background of glyph_string S. If S->background_filled_p
1772 is non-zero don't draw it. FORCE_P non-zero means draw the
1773 background even if it wouldn't be drawn normally. This is used
1774 when a string preceding S draws into the background of S, or S
1775 contains the first component of a composition. */
1777 static void
1778 x_draw_glyph_string_background (struct glyph_string *s, bool force_p)
1780 /* Nothing to do if background has already been drawn or if it
1781 shouldn't be drawn in the first place. */
1782 if (!s->background_filled_p)
1784 int box_line_width = max (s->face->box_line_width, 0);
1786 if (s->stippled_p)
1788 /* Fill background with a stipple pattern. */
1789 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1790 x_fill_rectangle (s->f, s->gc, s->x,
1791 s->y + box_line_width,
1792 s->background_width,
1793 s->height - 2 * box_line_width);
1794 XSetFillStyle (s->display, s->gc, FillSolid);
1795 s->background_filled_p = true;
1797 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1798 /* When xdisp.c ignores FONT_HEIGHT, we cannot trust
1799 font dimensions, since the actual glyphs might be
1800 much smaller. So in that case we always clear the
1801 rectangle with background color. */
1802 || FONT_TOO_HIGH (s->font)
1803 || s->font_not_found_p
1804 || s->extends_to_end_of_line_p
1805 || force_p)
1807 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1808 s->background_width,
1809 s->height - 2 * box_line_width);
1810 s->background_filled_p = true;
1816 /* Draw the foreground of glyph string S. */
1818 static void
1819 x_draw_glyph_string_foreground (struct glyph_string *s)
1821 int i, x;
1823 /* If first glyph of S has a left box line, start drawing the text
1824 of S to the right of that box line. */
1825 if (s->face->box != FACE_NO_BOX
1826 && s->first_glyph->left_box_line_p)
1827 x = s->x + eabs (s->face->box_line_width);
1828 else
1829 x = s->x;
1831 /* Draw characters of S as rectangles if S's font could not be
1832 loaded. */
1833 if (s->font_not_found_p)
1835 for (i = 0; i < s->nchars; ++i)
1837 struct glyph *g = s->first_glyph + i;
1838 x_draw_rectangle (s->f,
1839 s->gc, x, s->y, g->pixel_width - 1,
1840 s->height - 1);
1841 x += g->pixel_width;
1844 else
1846 struct font *font = s->font;
1847 int boff = font->baseline_offset;
1848 int y;
1850 if (font->vertical_centering)
1851 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1853 y = s->ybase - boff;
1854 if (s->for_overlaps
1855 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1856 font->driver->draw (s, 0, s->nchars, x, y, false);
1857 else
1858 font->driver->draw (s, 0, s->nchars, x, y, true);
1859 if (s->face->overstrike)
1860 font->driver->draw (s, 0, s->nchars, x + 1, y, false);
1864 /* Draw the foreground of composite glyph string S. */
1866 static void
1867 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1869 int i, j, x;
1870 struct font *font = s->font;
1872 /* If first glyph of S has a left box line, start drawing the text
1873 of S to the right of that box line. */
1874 if (s->face && s->face->box != FACE_NO_BOX
1875 && s->first_glyph->left_box_line_p)
1876 x = s->x + eabs (s->face->box_line_width);
1877 else
1878 x = s->x;
1880 /* S is a glyph string for a composition. S->cmp_from is the index
1881 of the first character drawn for glyphs of this composition.
1882 S->cmp_from == 0 means we are drawing the very first character of
1883 this composition. */
1885 /* Draw a rectangle for the composition if the font for the very
1886 first character of the composition could not be loaded. */
1887 if (s->font_not_found_p)
1889 if (s->cmp_from == 0)
1890 x_draw_rectangle (s->f, s->gc, x, s->y,
1891 s->width - 1, s->height - 1);
1893 else if (! s->first_glyph->u.cmp.automatic)
1895 int y = s->ybase;
1897 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1898 /* TAB in a composition means display glyphs with padding
1899 space on the left or right. */
1900 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1902 int xx = x + s->cmp->offsets[j * 2];
1903 int yy = y - s->cmp->offsets[j * 2 + 1];
1905 font->driver->draw (s, j, j + 1, xx, yy, false);
1906 if (s->face->overstrike)
1907 font->driver->draw (s, j, j + 1, xx + 1, yy, false);
1910 else
1912 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1913 Lisp_Object glyph;
1914 int y = s->ybase;
1915 int width = 0;
1917 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1919 glyph = LGSTRING_GLYPH (gstring, i);
1920 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1921 width += LGLYPH_WIDTH (glyph);
1922 else
1924 int xoff, yoff, wadjust;
1926 if (j < i)
1928 font->driver->draw (s, j, i, x, y, false);
1929 if (s->face->overstrike)
1930 font->driver->draw (s, j, i, x + 1, y, false);
1931 x += width;
1933 xoff = LGLYPH_XOFF (glyph);
1934 yoff = LGLYPH_YOFF (glyph);
1935 wadjust = LGLYPH_WADJUST (glyph);
1936 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, false);
1937 if (s->face->overstrike)
1938 font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff,
1939 false);
1940 x += wadjust;
1941 j = i + 1;
1942 width = 0;
1945 if (j < i)
1947 font->driver->draw (s, j, i, x, y, false);
1948 if (s->face->overstrike)
1949 font->driver->draw (s, j, i, x + 1, y, false);
1955 /* Draw the foreground of glyph string S for glyphless characters. */
1957 static void
1958 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1960 struct glyph *glyph = s->first_glyph;
1961 XChar2b char2b[8];
1962 int x, i, j;
1964 /* If first glyph of S has a left box line, start drawing the text
1965 of S to the right of that box line. */
1966 if (s->face && s->face->box != FACE_NO_BOX
1967 && s->first_glyph->left_box_line_p)
1968 x = s->x + eabs (s->face->box_line_width);
1969 else
1970 x = s->x;
1972 s->char2b = char2b;
1974 for (i = 0; i < s->nchars; i++, glyph++)
1976 char buf[7], *str = NULL;
1977 int len = glyph->u.glyphless.len;
1979 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1981 if (len > 0
1982 && CHAR_TABLE_P (Vglyphless_char_display)
1983 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1984 >= 1))
1986 Lisp_Object acronym
1987 = (! glyph->u.glyphless.for_no_font
1988 ? CHAR_TABLE_REF (Vglyphless_char_display,
1989 glyph->u.glyphless.ch)
1990 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
1991 if (STRINGP (acronym))
1992 str = SSDATA (acronym);
1995 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
1997 unsigned int ch = glyph->u.glyphless.ch;
1998 eassume (ch <= MAX_CHAR);
1999 sprintf (buf, "%0*X", ch < 0x10000 ? 4 : 6, ch);
2000 str = buf;
2003 if (str)
2005 int upper_len = (len + 1) / 2;
2006 unsigned code;
2008 /* It is assured that all LEN characters in STR is ASCII. */
2009 for (j = 0; j < len; j++)
2011 code = s->font->driver->encode_char (s->font, str[j]);
2012 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
2014 s->font->driver->draw (s, 0, upper_len,
2015 x + glyph->slice.glyphless.upper_xoff,
2016 s->ybase + glyph->slice.glyphless.upper_yoff,
2017 false);
2018 s->font->driver->draw (s, upper_len, len,
2019 x + glyph->slice.glyphless.lower_xoff,
2020 s->ybase + glyph->slice.glyphless.lower_yoff,
2021 false);
2023 if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
2024 x_draw_rectangle (s->f, s->gc,
2025 x, s->ybase - glyph->ascent,
2026 glyph->pixel_width - 1,
2027 glyph->ascent + glyph->descent - 1);
2028 x += glyph->pixel_width;
2032 #ifdef USE_X_TOOLKIT
2034 #ifdef USE_LUCID
2036 /* Return the frame on which widget WIDGET is used.. Abort if frame
2037 cannot be determined. */
2039 static struct frame *
2040 x_frame_of_widget (Widget widget)
2042 struct x_display_info *dpyinfo;
2043 Lisp_Object tail, frame;
2044 struct frame *f;
2046 dpyinfo = x_display_info_for_display (XtDisplay (widget));
2048 /* Find the top-level shell of the widget. Note that this function
2049 can be called when the widget is not yet realized, so XtWindow
2050 (widget) == 0. That's the reason we can't simply use
2051 x_any_window_to_frame. */
2052 while (!XtIsTopLevelShell (widget))
2053 widget = XtParent (widget);
2055 /* Look for a frame with that top-level widget. Allocate the color
2056 on that frame to get the right gamma correction value. */
2057 FOR_EACH_FRAME (tail, frame)
2059 f = XFRAME (frame);
2060 if (FRAME_X_P (f)
2061 && f->output_data.nothing != 1
2062 && FRAME_DISPLAY_INFO (f) == dpyinfo
2063 && f->output_data.x->widget == widget)
2064 return f;
2066 emacs_abort ();
2069 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
2070 or DELTA. Try a color with RGB values multiplied by FACTOR first.
2071 If this produces the same color as PIXEL, try a color where all RGB
2072 values have DELTA added. Return the allocated color in *PIXEL.
2073 DISPLAY is the X display, CMAP is the colormap to operate on.
2074 Value is true if successful. */
2076 bool
2077 x_alloc_lighter_color_for_widget (Widget widget, Display *display, Colormap cmap,
2078 unsigned long *pixel, double factor, int delta)
2080 struct frame *f = x_frame_of_widget (widget);
2081 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
2084 #endif /* USE_LUCID */
2087 /* Structure specifying which arguments should be passed by Xt to
2088 cvt_string_to_pixel. We want the widget's screen and colormap. */
2090 static XtConvertArgRec cvt_string_to_pixel_args[] =
2092 {XtWidgetBaseOffset, (XtPointer) offsetof (WidgetRec, core.screen),
2093 sizeof (Screen *)},
2094 {XtWidgetBaseOffset, (XtPointer) offsetof (WidgetRec, core.colormap),
2095 sizeof (Colormap)}
2099 /* The address of this variable is returned by
2100 cvt_string_to_pixel. */
2102 static Pixel cvt_string_to_pixel_value;
2105 /* Convert a color name to a pixel color.
2107 DPY is the display we are working on.
2109 ARGS is an array of *NARGS XrmValue structures holding additional
2110 information about the widget for which the conversion takes place.
2111 The contents of this array are determined by the specification
2112 in cvt_string_to_pixel_args.
2114 FROM is a pointer to an XrmValue which points to the color name to
2115 convert. TO is an XrmValue in which to return the pixel color.
2117 CLOSURE_RET is a pointer to user-data, in which we record if
2118 we allocated the color or not.
2120 Value is True if successful, False otherwise. */
2122 static Boolean
2123 cvt_string_to_pixel (Display *dpy, XrmValue *args, Cardinal *nargs,
2124 XrmValue *from, XrmValue *to,
2125 XtPointer *closure_ret)
2127 Screen *screen;
2128 Colormap cmap;
2129 Pixel pixel;
2130 String color_name;
2131 XColor color;
2133 if (*nargs != 2)
2135 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
2136 "wrongParameters", "cvt_string_to_pixel",
2137 "XtToolkitError",
2138 "Screen and colormap args required", NULL, NULL);
2139 return False;
2142 screen = *(Screen **) args[0].addr;
2143 cmap = *(Colormap *) args[1].addr;
2144 color_name = (String) from->addr;
2146 if (strcmp (color_name, XtDefaultBackground) == 0)
2148 *closure_ret = (XtPointer) False;
2149 pixel = WhitePixelOfScreen (screen);
2151 else if (strcmp (color_name, XtDefaultForeground) == 0)
2153 *closure_ret = (XtPointer) False;
2154 pixel = BlackPixelOfScreen (screen);
2156 else if (XParseColor (dpy, cmap, color_name, &color)
2157 && x_alloc_nearest_color_1 (dpy, cmap, &color))
2159 pixel = color.pixel;
2160 *closure_ret = (XtPointer) True;
2162 else
2164 String params[1];
2165 Cardinal nparams = 1;
2167 params[0] = color_name;
2168 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
2169 "badValue", "cvt_string_to_pixel",
2170 "XtToolkitError", "Invalid color '%s'",
2171 params, &nparams);
2172 return False;
2175 if (to->addr != NULL)
2177 if (to->size < sizeof (Pixel))
2179 to->size = sizeof (Pixel);
2180 return False;
2183 *(Pixel *) to->addr = pixel;
2185 else
2187 cvt_string_to_pixel_value = pixel;
2188 to->addr = (XtPointer) &cvt_string_to_pixel_value;
2191 to->size = sizeof (Pixel);
2192 return True;
2196 /* Free a pixel color which was previously allocated via
2197 cvt_string_to_pixel. This is registered as the destructor
2198 for this type of resource via XtSetTypeConverter.
2200 APP is the application context in which we work.
2202 TO is a pointer to an XrmValue holding the color to free.
2203 CLOSURE is the value we stored in CLOSURE_RET for this color
2204 in cvt_string_to_pixel.
2206 ARGS and NARGS are like for cvt_string_to_pixel. */
2208 static void
2209 cvt_pixel_dtor (XtAppContext app, XrmValuePtr to, XtPointer closure, XrmValuePtr args,
2210 Cardinal *nargs)
2212 if (*nargs != 2)
2214 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
2215 "XtToolkitError",
2216 "Screen and colormap arguments required",
2217 NULL, NULL);
2219 else if (closure != NULL)
2221 /* We did allocate the pixel, so free it. */
2222 Screen *screen = *(Screen **) args[0].addr;
2223 Colormap cmap = *(Colormap *) args[1].addr;
2224 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
2225 (Pixel *) to->addr, 1);
2230 #endif /* USE_X_TOOLKIT */
2233 /* Value is an array of XColor structures for the contents of the
2234 color map of display DPY. Set *NCELLS to the size of the array.
2235 Note that this probably shouldn't be called for large color maps,
2236 say a 24-bit TrueColor map. */
2238 static const XColor *
2239 x_color_cells (Display *dpy, int *ncells)
2241 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
2242 eassume (dpyinfo);
2244 if (dpyinfo->color_cells == NULL)
2246 Screen *screen = dpyinfo->screen;
2247 int ncolor_cells = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
2248 int i;
2250 dpyinfo->color_cells = xnmalloc (ncolor_cells,
2251 sizeof *dpyinfo->color_cells);
2252 dpyinfo->ncolor_cells = ncolor_cells;
2254 for (i = 0; i < ncolor_cells; ++i)
2255 dpyinfo->color_cells[i].pixel = i;
2257 XQueryColors (dpy, dpyinfo->cmap,
2258 dpyinfo->color_cells, ncolor_cells);
2261 *ncells = dpyinfo->ncolor_cells;
2262 return dpyinfo->color_cells;
2266 /* On frame F, translate pixel colors to RGB values for the NCOLORS
2267 colors in COLORS. Use cached information, if available. */
2269 void
2270 x_query_colors (struct frame *f, XColor *colors, int ncolors)
2272 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2274 if (dpyinfo->red_bits > 0)
2276 /* For TrueColor displays, we can decompose the RGB value
2277 directly. */
2278 int i;
2279 unsigned int rmult, gmult, bmult;
2280 unsigned int rmask, gmask, bmask;
2282 rmask = (1 << dpyinfo->red_bits) - 1;
2283 gmask = (1 << dpyinfo->green_bits) - 1;
2284 bmask = (1 << dpyinfo->blue_bits) - 1;
2285 /* If we're widening, for example, 8 bits in the pixel value to
2286 16 bits for the separate-color representation, we want to
2287 extrapolate the lower bits based on those bits available --
2288 in other words, we'd like 0xff to become 0xffff instead of
2289 the 0xff00 we'd get by just zero-filling the lower bits.
2291 We generate a 32-bit scaled-up value and shift it, in case
2292 the bit count doesn't divide 16 evenly (e.g., when dealing
2293 with a 3-3-2 bit RGB display), to get more of the lower bits
2294 correct.
2296 Should we cache the multipliers in dpyinfo? Maybe
2297 special-case the 8-8-8 common case? */
2298 rmult = 0xffffffff / rmask;
2299 gmult = 0xffffffff / gmask;
2300 bmult = 0xffffffff / bmask;
2302 for (i = 0; i < ncolors; ++i)
2304 unsigned int r, g, b;
2305 unsigned long pixel = colors[i].pixel;
2307 r = (pixel >> dpyinfo->red_offset) & rmask;
2308 g = (pixel >> dpyinfo->green_offset) & gmask;
2309 b = (pixel >> dpyinfo->blue_offset) & bmask;
2311 colors[i].red = (r * rmult) >> 16;
2312 colors[i].green = (g * gmult) >> 16;
2313 colors[i].blue = (b * bmult) >> 16;
2315 return;
2318 if (dpyinfo->color_cells)
2320 int i;
2321 for (i = 0; i < ncolors; ++i)
2323 unsigned long pixel = colors[i].pixel;
2324 eassert (pixel < dpyinfo->ncolor_cells);
2325 eassert (dpyinfo->color_cells[pixel].pixel == pixel);
2326 colors[i] = dpyinfo->color_cells[pixel];
2328 return;
2331 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
2335 /* On frame F, translate pixel color to RGB values for the color in
2336 COLOR. Use cached information, if available. */
2338 void
2339 x_query_color (struct frame *f, XColor *color)
2341 x_query_colors (f, color, 1);
2345 /* On frame F, translate the color name to RGB values. Use cached
2346 information, if possible.
2348 Note that there is currently no way to clean old entries out of the
2349 cache. However, it is limited to names in the server's database,
2350 and names we've actually looked up; list-colors-display is probably
2351 the most color-intensive case we're likely to hit. */
2353 Status x_parse_color (struct frame *f, const char *color_name,
2354 XColor *color)
2356 Display *dpy = FRAME_X_DISPLAY (f);
2357 Colormap cmap = FRAME_X_COLORMAP (f);
2358 struct color_name_cache_entry *cache_entry;
2360 if (color_name[0] == '#')
2362 /* The hex form is parsed directly by XParseColor without
2363 talking to the X server. No need for caching. */
2364 return XParseColor (dpy, cmap, color_name, color);
2367 for (cache_entry = FRAME_DISPLAY_INFO (f)->color_names; cache_entry;
2368 cache_entry = cache_entry->next)
2370 if (!xstrcasecmp(cache_entry->name, color_name))
2372 *color = cache_entry->rgb;
2373 return 1;
2377 if (XParseColor (dpy, cmap, color_name, color) == 0)
2378 /* No caching of negative results, currently. */
2379 return 0;
2381 cache_entry = xzalloc (sizeof *cache_entry);
2382 cache_entry->rgb = *color;
2383 cache_entry->name = xstrdup (color_name);
2384 cache_entry->next = FRAME_DISPLAY_INFO (f)->color_names;
2385 FRAME_DISPLAY_INFO (f)->color_names = cache_entry;
2386 return 1;
2390 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
2391 exact match can't be allocated, try the nearest color available.
2392 Value is true if successful. Set *COLOR to the color
2393 allocated. */
2395 static bool
2396 x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color)
2398 bool rc;
2400 rc = XAllocColor (dpy, cmap, color) != 0;
2401 if (rc == 0)
2403 /* If we got to this point, the colormap is full, so we're going
2404 to try to get the next closest color. The algorithm used is
2405 a least-squares matching, which is what X uses for closest
2406 color matching with StaticColor visuals. */
2407 int nearest, i;
2408 int max_color_delta = 255;
2409 int max_delta = 3 * max_color_delta;
2410 int nearest_delta = max_delta + 1;
2411 int ncells;
2412 const XColor *cells = x_color_cells (dpy, &ncells);
2414 for (nearest = i = 0; i < ncells; ++i)
2416 int dred = (color->red >> 8) - (cells[i].red >> 8);
2417 int dgreen = (color->green >> 8) - (cells[i].green >> 8);
2418 int dblue = (color->blue >> 8) - (cells[i].blue >> 8);
2419 int delta = dred * dred + dgreen * dgreen + dblue * dblue;
2421 if (delta < nearest_delta)
2423 nearest = i;
2424 nearest_delta = delta;
2428 color->red = cells[nearest].red;
2429 color->green = cells[nearest].green;
2430 color->blue = cells[nearest].blue;
2431 rc = XAllocColor (dpy, cmap, color) != 0;
2433 else
2435 /* If allocation succeeded, and the allocated pixel color is not
2436 equal to a cached pixel color recorded earlier, there was a
2437 change in the colormap, so clear the color cache. */
2438 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
2439 eassume (dpyinfo);
2441 if (dpyinfo->color_cells)
2443 XColor *cached_color = &dpyinfo->color_cells[color->pixel];
2444 if (cached_color->red != color->red
2445 || cached_color->blue != color->blue
2446 || cached_color->green != color->green)
2448 xfree (dpyinfo->color_cells);
2449 dpyinfo->color_cells = NULL;
2450 dpyinfo->ncolor_cells = 0;
2455 #ifdef DEBUG_X_COLORS
2456 if (rc)
2457 register_color (color->pixel);
2458 #endif /* DEBUG_X_COLORS */
2460 return rc;
2464 /* Allocate the color COLOR->pixel on frame F, colormap CMAP, after
2465 gamma correction. If an exact match can't be allocated, try the
2466 nearest color available. Value is true if successful. Set *COLOR
2467 to the color allocated. */
2469 bool
2470 x_alloc_nearest_color (struct frame *f, Colormap cmap, XColor *color)
2472 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2474 gamma_correct (f, color);
2476 if (dpyinfo->red_bits > 0)
2478 color->pixel = x_make_truecolor_pixel (dpyinfo,
2479 color->red,
2480 color->green,
2481 color->blue);
2482 return true;
2485 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
2489 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
2490 It's necessary to do this instead of just using PIXEL directly to
2491 get color reference counts right. */
2493 unsigned long
2494 x_copy_color (struct frame *f, unsigned long pixel)
2496 XColor color;
2498 /* If display has an immutable color map, freeing colors is not
2499 necessary and some servers don't allow it. Since we won't free a
2500 color once we've allocated it, we don't need to re-allocate it to
2501 maintain the server's reference count. */
2502 if (!x_mutable_colormap (FRAME_X_VISUAL (f)))
2503 return pixel;
2505 color.pixel = pixel;
2506 block_input ();
2507 /* The color could still be found in the color_cells array. */
2508 x_query_color (f, &color);
2509 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
2510 unblock_input ();
2511 #ifdef DEBUG_X_COLORS
2512 register_color (pixel);
2513 #endif
2514 return color.pixel;
2518 /* Brightness beyond which a color won't have its highlight brightness
2519 boosted.
2521 Nominally, highlight colors for `3d' faces are calculated by
2522 brightening an object's color by a constant scale factor, but this
2523 doesn't yield good results for dark colors, so for colors who's
2524 brightness is less than this value (on a scale of 0-65535) have an
2525 use an additional additive factor.
2527 The value here is set so that the default menu-bar/mode-line color
2528 (grey75) will not have its highlights changed at all. */
2529 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
2532 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
2533 or DELTA. Try a color with RGB values multiplied by FACTOR first.
2534 If this produces the same color as PIXEL, try a color where all RGB
2535 values have DELTA added. Return the allocated color in *PIXEL.
2536 DISPLAY is the X display, CMAP is the colormap to operate on.
2537 Value is non-zero if successful. */
2539 static bool
2540 x_alloc_lighter_color (struct frame *f, Display *display, Colormap cmap,
2541 unsigned long *pixel, double factor, int delta)
2543 XColor color, new;
2544 long bright;
2545 bool success_p;
2547 /* Get RGB color values. */
2548 color.pixel = *pixel;
2549 x_query_color (f, &color);
2551 /* Change RGB values by specified FACTOR. Avoid overflow! */
2552 eassert (factor >= 0);
2553 new.red = min (0xffff, factor * color.red);
2554 new.green = min (0xffff, factor * color.green);
2555 new.blue = min (0xffff, factor * color.blue);
2557 /* Calculate brightness of COLOR. */
2558 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
2560 /* We only boost colors that are darker than
2561 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
2562 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
2563 /* Make an additive adjustment to NEW, because it's dark enough so
2564 that scaling by FACTOR alone isn't enough. */
2566 /* How far below the limit this color is (0 - 1, 1 being darker). */
2567 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
2568 /* The additive adjustment. */
2569 int min_delta = delta * dimness * factor / 2;
2571 if (factor < 1)
2573 new.red = max (0, new.red - min_delta);
2574 new.green = max (0, new.green - min_delta);
2575 new.blue = max (0, new.blue - min_delta);
2577 else
2579 new.red = min (0xffff, min_delta + new.red);
2580 new.green = min (0xffff, min_delta + new.green);
2581 new.blue = min (0xffff, min_delta + new.blue);
2585 /* Try to allocate the color. */
2586 success_p = x_alloc_nearest_color (f, cmap, &new);
2587 if (success_p)
2589 if (new.pixel == *pixel)
2591 /* If we end up with the same color as before, try adding
2592 delta to the RGB values. */
2593 x_free_colors (f, &new.pixel, 1);
2595 new.red = min (0xffff, delta + color.red);
2596 new.green = min (0xffff, delta + color.green);
2597 new.blue = min (0xffff, delta + color.blue);
2598 success_p = x_alloc_nearest_color (f, cmap, &new);
2600 else
2601 success_p = true;
2602 *pixel = new.pixel;
2605 return success_p;
2609 /* Set up the foreground color for drawing relief lines of glyph
2610 string S. RELIEF is a pointer to a struct relief containing the GC
2611 with which lines will be drawn. Use a color that is FACTOR or
2612 DELTA lighter or darker than the relief's background which is found
2613 in S->f->output_data.x->relief_background. If such a color cannot
2614 be allocated, use DEFAULT_PIXEL, instead. */
2616 static void
2617 x_setup_relief_color (struct frame *f, struct relief *relief, double factor,
2618 int delta, unsigned long default_pixel)
2620 XGCValues xgcv;
2621 struct x_output *di = f->output_data.x;
2622 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
2623 unsigned long pixel;
2624 unsigned long background = di->relief_background;
2625 Colormap cmap = FRAME_X_COLORMAP (f);
2626 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2627 Display *dpy = FRAME_X_DISPLAY (f);
2629 xgcv.graphics_exposures = False;
2630 xgcv.line_width = 1;
2632 /* Free previously allocated color. The color cell will be reused
2633 when it has been freed as many times as it was allocated, so this
2634 doesn't affect faces using the same colors. */
2635 if (relief->gc && relief->pixel != -1)
2637 x_free_colors (f, &relief->pixel, 1);
2638 relief->pixel = -1;
2641 /* Allocate new color. */
2642 xgcv.foreground = default_pixel;
2643 pixel = background;
2644 if (dpyinfo->n_planes != 1
2645 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
2646 xgcv.foreground = relief->pixel = pixel;
2648 if (relief->gc == 0)
2650 xgcv.stipple = dpyinfo->gray;
2651 mask |= GCStipple;
2652 relief->gc = XCreateGC (dpy, FRAME_X_DRAWABLE (f), mask, &xgcv);
2654 else
2655 XChangeGC (dpy, relief->gc, mask, &xgcv);
2659 /* Set up colors for the relief lines around glyph string S. */
2661 static void
2662 x_setup_relief_colors (struct glyph_string *s)
2664 struct x_output *di = s->f->output_data.x;
2665 unsigned long color;
2667 if (s->face->use_box_color_for_shadows_p)
2668 color = s->face->box_color;
2669 else if (s->first_glyph->type == IMAGE_GLYPH
2670 && s->img->pixmap
2671 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2672 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2673 else
2675 XGCValues xgcv;
2677 /* Get the background color of the face. */
2678 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2679 color = xgcv.background;
2682 if (di->white_relief.gc == 0
2683 || color != di->relief_background)
2685 di->relief_background = color;
2686 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2687 WHITE_PIX_DEFAULT (s->f));
2688 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2689 BLACK_PIX_DEFAULT (s->f));
2694 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2695 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2696 to draw, it must be >= 0. RAISED_P means draw a raised
2697 relief. LEFT_P means draw a relief on the left side of
2698 the rectangle. RIGHT_P means draw a relief on the right
2699 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2700 when drawing. */
2702 static void
2703 x_draw_relief_rect (struct frame *f,
2704 int left_x, int top_y, int right_x, int bottom_y,
2705 int width, bool raised_p, bool top_p, bool bot_p,
2706 bool left_p, bool right_p,
2707 XRectangle *clip_rect)
2709 #ifdef USE_CAIRO
2710 GC top_left_gc, bottom_right_gc;
2711 int corners = 0;
2713 if (raised_p)
2715 top_left_gc = f->output_data.x->white_relief.gc;
2716 bottom_right_gc = f->output_data.x->black_relief.gc;
2718 else
2720 top_left_gc = f->output_data.x->black_relief.gc;
2721 bottom_right_gc = f->output_data.x->white_relief.gc;
2724 x_set_clip_rectangles (f, top_left_gc, clip_rect, 1);
2725 x_set_clip_rectangles (f, bottom_right_gc, clip_rect, 1);
2727 if (left_p)
2729 x_fill_rectangle (f, top_left_gc, left_x, top_y,
2730 width, bottom_y + 1 - top_y);
2731 if (top_p)
2732 corners |= 1 << CORNER_TOP_LEFT;
2733 if (bot_p)
2734 corners |= 1 << CORNER_BOTTOM_LEFT;
2736 if (right_p)
2738 x_fill_rectangle (f, bottom_right_gc, right_x + 1 - width, top_y,
2739 width, bottom_y + 1 - top_y);
2740 if (top_p)
2741 corners |= 1 << CORNER_TOP_RIGHT;
2742 if (bot_p)
2743 corners |= 1 << CORNER_BOTTOM_RIGHT;
2745 if (top_p)
2747 if (!right_p)
2748 x_fill_rectangle (f, top_left_gc, left_x, top_y,
2749 right_x + 1 - left_x, width);
2750 else
2751 x_fill_trapezoid_for_relief (f, top_left_gc, left_x, top_y,
2752 right_x + 1 - left_x, width, 1);
2754 if (bot_p)
2756 if (!left_p)
2757 x_fill_rectangle (f, bottom_right_gc, left_x, bottom_y + 1 - width,
2758 right_x + 1 - left_x, width);
2759 else
2760 x_fill_trapezoid_for_relief (f, bottom_right_gc,
2761 left_x, bottom_y + 1 - width,
2762 right_x + 1 - left_x, width, 0);
2764 if (left_p && width != 1)
2765 x_fill_rectangle (f, bottom_right_gc, left_x, top_y,
2766 1, bottom_y + 1 - top_y);
2767 if (top_p && width != 1)
2768 x_fill_rectangle (f, bottom_right_gc, left_x, top_y,
2769 right_x + 1 - left_x, 1);
2770 if (corners)
2772 XSetBackground (FRAME_X_DISPLAY (f), top_left_gc,
2773 FRAME_BACKGROUND_PIXEL (f));
2774 x_erase_corners_for_relief (f, top_left_gc, left_x, top_y,
2775 right_x - left_x + 1, bottom_y - top_y + 1,
2776 6, 1, corners);
2779 x_reset_clip_rectangles (f, top_left_gc);
2780 x_reset_clip_rectangles (f, bottom_right_gc);
2781 #else
2782 Display *dpy = FRAME_X_DISPLAY (f);
2783 Drawable drawable = FRAME_X_DRAWABLE (f);
2784 int i;
2785 GC gc;
2787 if (raised_p)
2788 gc = f->output_data.x->white_relief.gc;
2789 else
2790 gc = f->output_data.x->black_relief.gc;
2791 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2793 /* This code is more complicated than it has to be, because of two
2794 minor hacks to make the boxes look nicer: (i) if width > 1, draw
2795 the outermost line using the black relief. (ii) Omit the four
2796 corner pixels. */
2798 /* Top. */
2799 if (top_p)
2801 if (width == 1)
2802 XDrawLine (dpy, drawable, gc,
2803 left_x + left_p, top_y,
2804 right_x + !right_p, top_y);
2806 for (i = 1; i < width; ++i)
2807 XDrawLine (dpy, drawable, gc,
2808 left_x + i * left_p, top_y + i,
2809 right_x + 1 - i * right_p, top_y + i);
2812 /* Left. */
2813 if (left_p)
2815 if (width == 1)
2816 XDrawLine (dpy, drawable, gc, left_x, top_y + 1, left_x, bottom_y);
2818 x_clear_area(f, left_x, top_y, 1, 1);
2819 x_clear_area(f, left_x, bottom_y, 1, 1);
2821 for (i = (width > 1 ? 1 : 0); i < width; ++i)
2822 XDrawLine (dpy, drawable, gc,
2823 left_x + i, top_y + (i + 1) * top_p,
2824 left_x + i, bottom_y + 1 - (i + 1) * bot_p);
2827 XSetClipMask (dpy, gc, None);
2828 if (raised_p)
2829 gc = f->output_data.x->black_relief.gc;
2830 else
2831 gc = f->output_data.x->white_relief.gc;
2832 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2834 if (width > 1)
2836 /* Outermost top line. */
2837 if (top_p)
2838 XDrawLine (dpy, drawable, gc,
2839 left_x + left_p, top_y,
2840 right_x + !right_p, top_y);
2842 /* Outermost left line. */
2843 if (left_p)
2844 XDrawLine (dpy, drawable, gc, left_x, top_y + 1, left_x, bottom_y);
2847 /* Bottom. */
2848 if (bot_p)
2850 XDrawLine (dpy, drawable, gc,
2851 left_x + left_p, bottom_y,
2852 right_x + !right_p, bottom_y);
2853 for (i = 1; i < width; ++i)
2854 XDrawLine (dpy, drawable, gc,
2855 left_x + i * left_p, bottom_y - i,
2856 right_x + 1 - i * right_p, bottom_y - i);
2859 /* Right. */
2860 if (right_p)
2862 x_clear_area(f, right_x, top_y, 1, 1);
2863 x_clear_area(f, right_x, bottom_y, 1, 1);
2864 for (i = 0; i < width; ++i)
2865 XDrawLine (dpy, drawable, gc,
2866 right_x - i, top_y + (i + 1) * top_p,
2867 right_x - i, bottom_y + 1 - (i + 1) * bot_p);
2870 x_reset_clip_rectangles (f, gc);
2872 #endif
2876 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2877 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2878 draw, it must be >= 0. LEFT_P means draw a line on the
2879 left side of the rectangle. RIGHT_P means draw a line
2880 on the right side of the rectangle. CLIP_RECT is the clipping
2881 rectangle to use when drawing. */
2883 static void
2884 x_draw_box_rect (struct glyph_string *s,
2885 int left_x, int top_y, int right_x, int bottom_y, int width,
2886 bool left_p, bool right_p, XRectangle *clip_rect)
2888 XGCValues xgcv;
2890 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2891 XSetForeground (s->display, s->gc, s->face->box_color);
2892 x_set_clip_rectangles (s->f, s->gc, clip_rect, 1);
2894 /* Top. */
2895 x_fill_rectangle (s->f, s->gc,
2896 left_x, top_y, right_x - left_x + 1, width);
2898 /* Left. */
2899 if (left_p)
2900 x_fill_rectangle (s->f, s->gc,
2901 left_x, top_y, width, bottom_y - top_y + 1);
2903 /* Bottom. */
2904 x_fill_rectangle (s->f, s->gc,
2905 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2907 /* Right. */
2908 if (right_p)
2909 x_fill_rectangle (s->f, s->gc,
2910 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2912 XSetForeground (s->display, s->gc, xgcv.foreground);
2913 x_reset_clip_rectangles (s->f, s->gc);
2917 /* Draw a box around glyph string S. */
2919 static void
2920 x_draw_glyph_string_box (struct glyph_string *s)
2922 int width, left_x, right_x, top_y, bottom_y, last_x;
2923 bool raised_p, left_p, right_p;
2924 struct glyph *last_glyph;
2925 XRectangle clip_rect;
2927 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2928 ? WINDOW_RIGHT_EDGE_X (s->w)
2929 : window_box_right (s->w, s->area));
2931 /* The glyph that may have a right box line. */
2932 last_glyph = (s->cmp || s->img
2933 ? s->first_glyph
2934 : s->first_glyph + s->nchars - 1);
2936 width = eabs (s->face->box_line_width);
2937 raised_p = s->face->box == FACE_RAISED_BOX;
2938 left_x = s->x;
2939 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2940 ? last_x - 1
2941 : min (last_x, s->x + s->background_width) - 1);
2942 top_y = s->y;
2943 bottom_y = top_y + s->height - 1;
2945 left_p = (s->first_glyph->left_box_line_p
2946 || (s->hl == DRAW_MOUSE_FACE
2947 && (s->prev == NULL
2948 || s->prev->hl != s->hl)));
2949 right_p = (last_glyph->right_box_line_p
2950 || (s->hl == DRAW_MOUSE_FACE
2951 && (s->next == NULL
2952 || s->next->hl != s->hl)));
2954 get_glyph_string_clip_rect (s, &clip_rect);
2956 if (s->face->box == FACE_SIMPLE_BOX)
2957 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2958 left_p, right_p, &clip_rect);
2959 else
2961 x_setup_relief_colors (s);
2962 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2963 width, raised_p, true, true, left_p, right_p,
2964 &clip_rect);
2969 /* Draw foreground of image glyph string S. */
2971 static void
2972 x_draw_image_foreground (struct glyph_string *s)
2974 int x = s->x;
2975 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2977 /* If first glyph of S has a left box line, start drawing it to the
2978 right of that line. */
2979 if (s->face->box != FACE_NO_BOX
2980 && s->first_glyph->left_box_line_p
2981 && s->slice.x == 0)
2982 x += eabs (s->face->box_line_width);
2984 /* If there is a margin around the image, adjust x- and y-position
2985 by that margin. */
2986 if (s->slice.x == 0)
2987 x += s->img->hmargin;
2988 if (s->slice.y == 0)
2989 y += s->img->vmargin;
2991 if (s->img->pixmap)
2993 if (s->img->mask)
2995 /* We can't set both a clip mask and use XSetClipRectangles
2996 because the latter also sets a clip mask. We also can't
2997 trust on the shape extension to be available
2998 (XShapeCombineRegion). So, compute the rectangle to draw
2999 manually. */
3000 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3001 | GCFunction);
3002 XGCValues xgcv;
3003 XRectangle clip_rect, image_rect, r;
3005 xgcv.clip_mask = s->img->mask;
3006 xgcv.clip_x_origin = x;
3007 xgcv.clip_y_origin = y;
3008 xgcv.function = GXcopy;
3009 XChangeGC (s->display, s->gc, mask, &xgcv);
3011 get_glyph_string_clip_rect (s, &clip_rect);
3012 image_rect.x = x;
3013 image_rect.y = y;
3014 image_rect.width = s->slice.width;
3015 image_rect.height = s->slice.height;
3016 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3017 XCopyArea (s->display, s->img->pixmap,
3018 FRAME_X_DRAWABLE (s->f), s->gc,
3019 s->slice.x + r.x - x, s->slice.y + r.y - y,
3020 r.width, r.height, r.x, r.y);
3022 else
3024 XRectangle clip_rect, image_rect, r;
3026 get_glyph_string_clip_rect (s, &clip_rect);
3027 image_rect.x = x;
3028 image_rect.y = y;
3029 image_rect.width = s->slice.width;
3030 image_rect.height = s->slice.height;
3031 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3032 XCopyArea (s->display, s->img->pixmap,
3033 FRAME_X_DRAWABLE (s->f), s->gc,
3034 s->slice.x + r.x - x, s->slice.y + r.y - y,
3035 r.width, r.height, r.x, r.y);
3037 /* When the image has a mask, we can expect that at
3038 least part of a mouse highlight or a block cursor will
3039 be visible. If the image doesn't have a mask, make
3040 a block cursor visible by drawing a rectangle around
3041 the image. I believe it's looking better if we do
3042 nothing here for mouse-face. */
3043 if (s->hl == DRAW_CURSOR)
3045 int relief = eabs (s->img->relief);
3046 x_draw_rectangle (s->f, s->gc,
3047 x - relief, y - relief,
3048 s->slice.width + relief*2 - 1,
3049 s->slice.height + relief*2 - 1);
3053 else
3054 /* Draw a rectangle if image could not be loaded. */
3055 x_draw_rectangle (s->f, s->gc, x, y,
3056 s->slice.width - 1, s->slice.height - 1);
3060 /* Draw a relief around the image glyph string S. */
3062 static void
3063 x_draw_image_relief (struct glyph_string *s)
3065 int x1, y1, thick;
3066 bool raised_p, top_p, bot_p, left_p, right_p;
3067 int extra_x, extra_y;
3068 XRectangle r;
3069 int x = s->x;
3070 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
3072 /* If first glyph of S has a left box line, start drawing it to the
3073 right of that line. */
3074 if (s->face->box != FACE_NO_BOX
3075 && s->first_glyph->left_box_line_p
3076 && s->slice.x == 0)
3077 x += eabs (s->face->box_line_width);
3079 /* If there is a margin around the image, adjust x- and y-position
3080 by that margin. */
3081 if (s->slice.x == 0)
3082 x += s->img->hmargin;
3083 if (s->slice.y == 0)
3084 y += s->img->vmargin;
3086 if (s->hl == DRAW_IMAGE_SUNKEN
3087 || s->hl == DRAW_IMAGE_RAISED)
3089 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
3090 raised_p = s->hl == DRAW_IMAGE_RAISED;
3092 else
3094 thick = eabs (s->img->relief);
3095 raised_p = s->img->relief > 0;
3098 x1 = x + s->slice.width - 1;
3099 y1 = y + s->slice.height - 1;
3101 extra_x = extra_y = 0;
3102 if (s->face->id == TOOL_BAR_FACE_ID)
3104 if (CONSP (Vtool_bar_button_margin)
3105 && INTEGERP (XCAR (Vtool_bar_button_margin))
3106 && INTEGERP (XCDR (Vtool_bar_button_margin)))
3108 extra_x = XINT (XCAR (Vtool_bar_button_margin));
3109 extra_y = XINT (XCDR (Vtool_bar_button_margin));
3111 else if (INTEGERP (Vtool_bar_button_margin))
3112 extra_x = extra_y = XINT (Vtool_bar_button_margin);
3115 top_p = bot_p = left_p = right_p = false;
3117 if (s->slice.x == 0)
3118 x -= thick + extra_x, left_p = true;
3119 if (s->slice.y == 0)
3120 y -= thick + extra_y, top_p = true;
3121 if (s->slice.x + s->slice.width == s->img->width)
3122 x1 += thick + extra_x, right_p = true;
3123 if (s->slice.y + s->slice.height == s->img->height)
3124 y1 += thick + extra_y, bot_p = true;
3126 x_setup_relief_colors (s);
3127 get_glyph_string_clip_rect (s, &r);
3128 x_draw_relief_rect (s->f, x, y, x1, y1, thick, raised_p,
3129 top_p, bot_p, left_p, right_p, &r);
3133 /* Draw the foreground of image glyph string S to PIXMAP. */
3135 static void
3136 x_draw_image_foreground_1 (struct glyph_string *s, Pixmap pixmap)
3138 int x = 0;
3139 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
3141 /* If first glyph of S has a left box line, start drawing it to the
3142 right of that line. */
3143 if (s->face->box != FACE_NO_BOX
3144 && s->first_glyph->left_box_line_p
3145 && s->slice.x == 0)
3146 x += eabs (s->face->box_line_width);
3148 /* If there is a margin around the image, adjust x- and y-position
3149 by that margin. */
3150 if (s->slice.x == 0)
3151 x += s->img->hmargin;
3152 if (s->slice.y == 0)
3153 y += s->img->vmargin;
3155 if (s->img->pixmap)
3157 if (s->img->mask)
3159 /* We can't set both a clip mask and use XSetClipRectangles
3160 because the latter also sets a clip mask. We also can't
3161 trust on the shape extension to be available
3162 (XShapeCombineRegion). So, compute the rectangle to draw
3163 manually. */
3164 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3165 | GCFunction);
3166 XGCValues xgcv;
3168 xgcv.clip_mask = s->img->mask;
3169 xgcv.clip_x_origin = x - s->slice.x;
3170 xgcv.clip_y_origin = y - s->slice.y;
3171 xgcv.function = GXcopy;
3172 XChangeGC (s->display, s->gc, mask, &xgcv);
3174 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3175 s->slice.x, s->slice.y,
3176 s->slice.width, s->slice.height, x, y);
3177 XSetClipMask (s->display, s->gc, None);
3179 else
3181 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3182 s->slice.x, s->slice.y,
3183 s->slice.width, s->slice.height, x, y);
3185 /* When the image has a mask, we can expect that at
3186 least part of a mouse highlight or a block cursor will
3187 be visible. If the image doesn't have a mask, make
3188 a block cursor visible by drawing a rectangle around
3189 the image. I believe it's looking better if we do
3190 nothing here for mouse-face. */
3191 if (s->hl == DRAW_CURSOR)
3193 int r = eabs (s->img->relief);
3194 x_draw_rectangle (s->f, s->gc, x - r, y - r,
3195 s->slice.width + r*2 - 1,
3196 s->slice.height + r*2 - 1);
3200 else
3201 /* Draw a rectangle if image could not be loaded. */
3202 x_draw_rectangle (s->f, s->gc, x, y,
3203 s->slice.width - 1, s->slice.height - 1);
3207 /* Draw part of the background of glyph string S. X, Y, W, and H
3208 give the rectangle to draw. */
3210 static void
3211 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
3213 if (s->stippled_p)
3215 /* Fill background with a stipple pattern. */
3216 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3217 x_fill_rectangle (s->f, s->gc, x, y, w, h);
3218 XSetFillStyle (s->display, s->gc, FillSolid);
3220 else
3221 x_clear_glyph_string_rect (s, x, y, w, h);
3225 /* Draw image glyph string S.
3227 s->y
3228 s->x +-------------------------
3229 | s->face->box
3231 | +-------------------------
3232 | | s->img->margin
3234 | | +-------------------
3235 | | | the image
3239 static void
3240 x_draw_image_glyph_string (struct glyph_string *s)
3242 int box_line_hwidth = eabs (s->face->box_line_width);
3243 int box_line_vwidth = max (s->face->box_line_width, 0);
3244 int height;
3245 Pixmap pixmap = None;
3247 height = s->height;
3248 if (s->slice.y == 0)
3249 height -= box_line_vwidth;
3250 if (s->slice.y + s->slice.height >= s->img->height)
3251 height -= box_line_vwidth;
3253 /* Fill background with face under the image. Do it only if row is
3254 taller than image or if image has a clip mask to reduce
3255 flickering. */
3256 s->stippled_p = s->face->stipple != 0;
3257 if (height > s->slice.height
3258 || s->img->hmargin
3259 || s->img->vmargin
3260 || s->img->mask
3261 || s->img->pixmap == 0
3262 || s->width != s->background_width)
3264 if (s->img->mask)
3266 /* Create a pixmap as large as the glyph string. Fill it
3267 with the background color. Copy the image to it, using
3268 its mask. Copy the temporary pixmap to the display. */
3269 Screen *screen = FRAME_X_SCREEN (s->f);
3270 int depth = DefaultDepthOfScreen (screen);
3272 /* Create a pixmap as large as the glyph string. */
3273 pixmap = XCreatePixmap (s->display, FRAME_X_DRAWABLE (s->f),
3274 s->background_width,
3275 s->height, depth);
3277 /* Don't clip in the following because we're working on the
3278 pixmap. */
3279 XSetClipMask (s->display, s->gc, None);
3281 /* Fill the pixmap with the background color/stipple. */
3282 if (s->stippled_p)
3284 /* Fill background with a stipple pattern. */
3285 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3286 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
3287 XFillRectangle (s->display, pixmap, s->gc,
3288 0, 0, s->background_width, s->height);
3289 XSetFillStyle (s->display, s->gc, FillSolid);
3290 XSetTSOrigin (s->display, s->gc, 0, 0);
3292 else
3294 XGCValues xgcv;
3295 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3296 &xgcv);
3297 XSetForeground (s->display, s->gc, xgcv.background);
3298 XFillRectangle (s->display, pixmap, s->gc,
3299 0, 0, s->background_width, s->height);
3300 XSetForeground (s->display, s->gc, xgcv.foreground);
3303 else
3305 int x = s->x;
3306 int y = s->y;
3307 int width = s->background_width;
3309 if (s->first_glyph->left_box_line_p
3310 && s->slice.x == 0)
3312 x += box_line_hwidth;
3313 width -= box_line_hwidth;
3316 if (s->slice.y == 0)
3317 y += box_line_vwidth;
3319 x_draw_glyph_string_bg_rect (s, x, y, width, height);
3322 s->background_filled_p = true;
3325 /* Draw the foreground. */
3326 #ifdef USE_CAIRO
3327 if (s->img->cr_data)
3329 cairo_t *cr = x_begin_cr_clip (s->f, s->gc);
3331 int x = s->x + s->img->hmargin;
3332 int y = s->y + s->img->vmargin;
3333 int width = s->background_width;
3335 cairo_set_source_surface (cr, s->img->cr_data,
3336 x - s->slice.x,
3337 y - s->slice.y);
3338 cairo_rectangle (cr, x, y, width, height);
3339 cairo_fill (cr);
3340 x_end_cr_clip (s->f);
3342 else
3343 #endif
3344 if (pixmap != None)
3346 x_draw_image_foreground_1 (s, pixmap);
3347 x_set_glyph_string_clipping (s);
3348 XCopyArea (s->display, pixmap, FRAME_X_DRAWABLE (s->f), s->gc,
3349 0, 0, s->background_width, s->height, s->x, s->y);
3350 XFreePixmap (s->display, pixmap);
3352 else
3353 x_draw_image_foreground (s);
3355 /* If we must draw a relief around the image, do it. */
3356 if (s->img->relief
3357 || s->hl == DRAW_IMAGE_RAISED
3358 || s->hl == DRAW_IMAGE_SUNKEN)
3359 x_draw_image_relief (s);
3363 /* Draw stretch glyph string S. */
3365 static void
3366 x_draw_stretch_glyph_string (struct glyph_string *s)
3368 eassert (s->first_glyph->type == STRETCH_GLYPH);
3370 if (s->hl == DRAW_CURSOR
3371 && !x_stretch_cursor_p)
3373 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
3374 wide as the stretch glyph. */
3375 int width, background_width = s->background_width;
3376 int x = s->x;
3378 if (!s->row->reversed_p)
3380 int left_x = window_box_left_offset (s->w, TEXT_AREA);
3382 if (x < left_x)
3384 background_width -= left_x - x;
3385 x = left_x;
3388 else
3390 /* In R2L rows, draw the cursor on the right edge of the
3391 stretch glyph. */
3392 int right_x = window_box_right (s->w, TEXT_AREA);
3394 if (x + background_width > right_x)
3395 background_width -= x - right_x;
3396 x += background_width;
3398 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
3399 if (s->row->reversed_p)
3400 x -= width;
3402 /* Draw cursor. */
3403 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
3405 /* Clear rest using the GC of the original non-cursor face. */
3406 if (width < background_width)
3408 int y = s->y;
3409 int w = background_width - width, h = s->height;
3410 XRectangle r;
3411 GC gc;
3413 if (!s->row->reversed_p)
3414 x += width;
3415 else
3416 x = s->x;
3417 if (s->row->mouse_face_p
3418 && cursor_in_mouse_face_p (s->w))
3420 x_set_mouse_face_gc (s);
3421 gc = s->gc;
3423 else
3424 gc = s->face->gc;
3426 get_glyph_string_clip_rect (s, &r);
3427 x_set_clip_rectangles (s->f, gc, &r, 1);
3429 if (s->face->stipple)
3431 /* Fill background with a stipple pattern. */
3432 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3433 x_fill_rectangle (s->f, gc, x, y, w, h);
3434 XSetFillStyle (s->display, gc, FillSolid);
3436 else
3438 XGCValues xgcv;
3439 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3440 XSetForeground (s->display, gc, xgcv.background);
3441 x_fill_rectangle (s->f, gc, x, y, w, h);
3442 XSetForeground (s->display, gc, xgcv.foreground);
3445 x_reset_clip_rectangles (s->f, gc);
3448 else if (!s->background_filled_p)
3450 int background_width = s->background_width;
3451 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
3453 /* Don't draw into left margin, fringe or scrollbar area
3454 except for header line and mode line. */
3455 if (x < left_x && !s->row->mode_line_p)
3457 background_width -= left_x - x;
3458 x = left_x;
3460 if (background_width > 0)
3461 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
3464 s->background_filled_p = true;
3467 static void
3468 x_get_scale_factor(Display *disp, int *scale_x, int *scale_y)
3470 const int base_res = 96;
3471 struct x_display_info * dpyinfo = x_display_info_for_display (disp);
3473 *scale_x = *scale_y = 1;
3475 if (dpyinfo)
3477 if (dpyinfo->resx > base_res)
3478 *scale_x = floor (dpyinfo->resx / base_res);
3479 if (dpyinfo->resy > base_res)
3480 *scale_y = floor (dpyinfo->resy / base_res);
3485 Draw a wavy line under S. The wave fills wave_height pixels from y0.
3487 x0 wave_length = 2
3489 y0 * * * * *
3490 |* * * * * * * * *
3491 wave_height = 3 | * * * *
3494 static void
3495 x_draw_underwave (struct glyph_string *s)
3497 /* Adjust for scale/HiDPI. */
3498 int scale_x, scale_y;
3500 x_get_scale_factor (s->display, &scale_x, &scale_y);
3502 int wave_height = 3 * scale_y, wave_length = 2 * scale_x;
3504 #ifdef USE_CAIRO
3505 x_draw_horizontal_wave (s->f, s->gc, s->x, s->ybase - wave_height + 3,
3506 s->width, wave_height, wave_length);
3507 #else /* not USE_CAIRO */
3508 int dx, dy, x0, y0, width, x1, y1, x2, y2, xmax, thickness = scale_y;;
3509 bool odd;
3510 XRectangle wave_clip, string_clip, final_clip;
3512 dx = wave_length;
3513 dy = wave_height - 1;
3514 x0 = s->x;
3515 y0 = s->ybase + wave_height / 2 - scale_y;
3516 width = s->width;
3517 xmax = x0 + width;
3519 /* Find and set clipping rectangle */
3521 wave_clip.x = x0;
3522 wave_clip.y = y0;
3523 wave_clip.width = width;
3524 wave_clip.height = wave_height;
3525 get_glyph_string_clip_rect (s, &string_clip);
3527 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
3528 return;
3530 XSetClipRectangles (s->display, s->gc, 0, 0, &final_clip, 1, Unsorted);
3532 /* Draw the waves */
3534 x1 = x0 - (x0 % dx);
3535 x2 = x1 + dx;
3536 odd = (x1 / dx) & 1;
3537 y1 = y2 = y0;
3539 if (odd)
3540 y1 += dy;
3541 else
3542 y2 += dy;
3544 if (INT_MAX - dx < xmax)
3545 emacs_abort ();
3547 while (x1 <= xmax)
3549 XSetLineAttributes (s->display, s->gc, thickness, LineSolid, CapButt,
3550 JoinRound);
3551 XDrawLine (s->display, FRAME_X_DRAWABLE (s->f), s->gc, x1, y1, x2, y2);
3552 x1 = x2, y1 = y2;
3553 x2 += dx, y2 = y0 + odd*dy;
3554 odd = !odd;
3557 /* Restore previous clipping rectangle(s) */
3558 XSetClipRectangles (s->display, s->gc, 0, 0, s->clip, s->num_clips, Unsorted);
3559 #endif /* not USE_CAIRO */
3563 /* Draw glyph string S. */
3565 static void
3566 x_draw_glyph_string (struct glyph_string *s)
3568 bool relief_drawn_p = false;
3570 /* If S draws into the background of its successors, draw the
3571 background of the successors first so that S can draw into it.
3572 This makes S->next use XDrawString instead of XDrawImageString. */
3573 if (s->next && s->right_overhang && !s->for_overlaps)
3575 int width;
3576 struct glyph_string *next;
3578 for (width = 0, next = s->next;
3579 next && width < s->right_overhang;
3580 width += next->width, next = next->next)
3581 if (next->first_glyph->type != IMAGE_GLYPH)
3583 x_set_glyph_string_gc (next);
3584 x_set_glyph_string_clipping (next);
3585 if (next->first_glyph->type == STRETCH_GLYPH)
3586 x_draw_stretch_glyph_string (next);
3587 else
3588 x_draw_glyph_string_background (next, true);
3589 next->num_clips = 0;
3593 /* Set up S->gc, set clipping and draw S. */
3594 x_set_glyph_string_gc (s);
3596 /* Draw relief (if any) in advance for char/composition so that the
3597 glyph string can be drawn over it. */
3598 if (!s->for_overlaps
3599 && s->face->box != FACE_NO_BOX
3600 && (s->first_glyph->type == CHAR_GLYPH
3601 || s->first_glyph->type == COMPOSITE_GLYPH))
3604 x_set_glyph_string_clipping (s);
3605 x_draw_glyph_string_background (s, true);
3606 x_draw_glyph_string_box (s);
3607 x_set_glyph_string_clipping (s);
3608 relief_drawn_p = true;
3610 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
3611 && !s->clip_tail
3612 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
3613 || (s->next && s->next->hl != s->hl && s->right_overhang)))
3614 /* We must clip just this glyph. left_overhang part has already
3615 drawn when s->prev was drawn, and right_overhang part will be
3616 drawn later when s->next is drawn. */
3617 x_set_glyph_string_clipping_exactly (s, s);
3618 else
3619 x_set_glyph_string_clipping (s);
3621 switch (s->first_glyph->type)
3623 case IMAGE_GLYPH:
3624 x_draw_image_glyph_string (s);
3625 break;
3627 case XWIDGET_GLYPH:
3628 x_draw_xwidget_glyph_string (s);
3629 break;
3631 case STRETCH_GLYPH:
3632 x_draw_stretch_glyph_string (s);
3633 break;
3635 case CHAR_GLYPH:
3636 if (s->for_overlaps)
3637 s->background_filled_p = true;
3638 else
3639 x_draw_glyph_string_background (s, false);
3640 x_draw_glyph_string_foreground (s);
3641 break;
3643 case COMPOSITE_GLYPH:
3644 if (s->for_overlaps || (s->cmp_from > 0
3645 && ! s->first_glyph->u.cmp.automatic))
3646 s->background_filled_p = true;
3647 else
3648 x_draw_glyph_string_background (s, true);
3649 x_draw_composite_glyph_string_foreground (s);
3650 break;
3652 case GLYPHLESS_GLYPH:
3653 if (s->for_overlaps)
3654 s->background_filled_p = true;
3655 else
3656 x_draw_glyph_string_background (s, true);
3657 x_draw_glyphless_glyph_string_foreground (s);
3658 break;
3660 default:
3661 emacs_abort ();
3664 if (!s->for_overlaps)
3666 /* Draw underline. */
3667 if (s->face->underline_p)
3669 if (s->face->underline_type == FACE_UNDER_WAVE)
3671 if (s->face->underline_defaulted_p)
3672 x_draw_underwave (s);
3673 else
3675 XGCValues xgcv;
3676 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3677 XSetForeground (s->display, s->gc, s->face->underline_color);
3678 x_draw_underwave (s);
3679 XSetForeground (s->display, s->gc, xgcv.foreground);
3682 else if (s->face->underline_type == FACE_UNDER_LINE)
3684 unsigned long thickness, position;
3685 int y;
3687 if (s->prev && s->prev->face->underline_p
3688 && s->prev->face->underline_type == FACE_UNDER_LINE)
3690 /* We use the same underline style as the previous one. */
3691 thickness = s->prev->underline_thickness;
3692 position = s->prev->underline_position;
3694 else
3696 struct font *font = font_for_underline_metrics (s);
3697 unsigned long minimum_offset;
3698 bool underline_at_descent_line;
3699 bool use_underline_position_properties;
3700 Lisp_Object val
3701 = buffer_local_value (Qunderline_minimum_offset,
3702 s->w->contents);
3703 if (INTEGERP (val))
3704 minimum_offset = XFASTINT (val);
3705 else
3706 minimum_offset = 1;
3707 val = buffer_local_value (Qx_underline_at_descent_line,
3708 s->w->contents);
3709 underline_at_descent_line
3710 = !(NILP (val) || EQ (val, Qunbound));
3712 = buffer_local_value (Qx_use_underline_position_properties,
3713 s->w->contents);
3714 use_underline_position_properties
3715 = !(NILP (val) || EQ (val, Qunbound));
3717 /* Get the underline thickness. Default is 1 pixel. */
3718 if (font && font->underline_thickness > 0)
3719 thickness = font->underline_thickness;
3720 else
3721 thickness = 1;
3722 if (underline_at_descent_line)
3723 position = (s->height - thickness) - (s->ybase - s->y);
3724 else
3726 /* Get the underline position. This is the
3727 recommended vertical offset in pixels from
3728 the baseline to the top of the underline.
3729 This is a signed value according to the
3730 specs, and its default is
3732 ROUND ((maximum descent) / 2), with
3733 ROUND(x) = floor (x + 0.5) */
3735 if (use_underline_position_properties
3736 && font && font->underline_position >= 0)
3737 position = font->underline_position;
3738 else if (font)
3739 position = (font->descent + 1) / 2;
3740 else
3741 position = minimum_offset;
3743 position = max (position, minimum_offset);
3745 /* Check the sanity of thickness and position. We should
3746 avoid drawing underline out of the current line area. */
3747 if (s->y + s->height <= s->ybase + position)
3748 position = (s->height - 1) - (s->ybase - s->y);
3749 if (s->y + s->height < s->ybase + position + thickness)
3750 thickness = (s->y + s->height) - (s->ybase + position);
3751 s->underline_thickness = thickness;
3752 s->underline_position = position;
3753 y = s->ybase + position;
3754 if (s->face->underline_defaulted_p)
3755 x_fill_rectangle (s->f, s->gc,
3756 s->x, y, s->width, thickness);
3757 else
3759 XGCValues xgcv;
3760 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3761 XSetForeground (s->display, s->gc, s->face->underline_color);
3762 x_fill_rectangle (s->f, s->gc,
3763 s->x, y, s->width, thickness);
3764 XSetForeground (s->display, s->gc, xgcv.foreground);
3768 /* Draw overline. */
3769 if (s->face->overline_p)
3771 unsigned long dy = 0, h = 1;
3773 if (s->face->overline_color_defaulted_p)
3774 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3775 s->width, h);
3776 else
3778 XGCValues xgcv;
3779 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3780 XSetForeground (s->display, s->gc, s->face->overline_color);
3781 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3782 s->width, h);
3783 XSetForeground (s->display, s->gc, xgcv.foreground);
3787 /* Draw strike-through. */
3788 if (s->face->strike_through_p)
3790 /* Y-coordinate and height of the glyph string's first
3791 glyph. We cannot use s->y and s->height because those
3792 could be larger if there are taller display elements
3793 (e.g., characters displayed with a larger font) in the
3794 same glyph row. */
3795 int glyph_y = s->ybase - s->first_glyph->ascent;
3796 int glyph_height = s->first_glyph->ascent + s->first_glyph->descent;
3797 /* Strike-through width and offset from the glyph string's
3798 top edge. */
3799 unsigned long h = 1;
3800 unsigned long dy = (glyph_height - h) / 2;
3802 if (s->face->strike_through_color_defaulted_p)
3803 x_fill_rectangle (s->f, s->gc, s->x, glyph_y + dy,
3804 s->width, h);
3805 else
3807 XGCValues xgcv;
3808 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3809 XSetForeground (s->display, s->gc, s->face->strike_through_color);
3810 x_fill_rectangle (s->f, s->gc, s->x, glyph_y + dy,
3811 s->width, h);
3812 XSetForeground (s->display, s->gc, xgcv.foreground);
3816 /* Draw relief if not yet drawn. */
3817 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
3818 x_draw_glyph_string_box (s);
3820 if (s->prev)
3822 struct glyph_string *prev;
3824 for (prev = s->prev; prev; prev = prev->prev)
3825 if (prev->hl != s->hl
3826 && prev->x + prev->width + prev->right_overhang > s->x)
3828 /* As prev was drawn while clipped to its own area, we
3829 must draw the right_overhang part using s->hl now. */
3830 enum draw_glyphs_face save = prev->hl;
3832 prev->hl = s->hl;
3833 x_set_glyph_string_gc (prev);
3834 x_set_glyph_string_clipping_exactly (s, prev);
3835 if (prev->first_glyph->type == CHAR_GLYPH)
3836 x_draw_glyph_string_foreground (prev);
3837 else
3838 x_draw_composite_glyph_string_foreground (prev);
3839 x_reset_clip_rectangles (prev->f, prev->gc);
3840 prev->hl = save;
3841 prev->num_clips = 0;
3845 if (s->next)
3847 struct glyph_string *next;
3849 for (next = s->next; next; next = next->next)
3850 if (next->hl != s->hl
3851 && next->x - next->left_overhang < s->x + s->width)
3853 /* As next will be drawn while clipped to its own area,
3854 we must draw the left_overhang part using s->hl now. */
3855 enum draw_glyphs_face save = next->hl;
3857 next->hl = s->hl;
3858 x_set_glyph_string_gc (next);
3859 x_set_glyph_string_clipping_exactly (s, next);
3860 if (next->first_glyph->type == CHAR_GLYPH)
3861 x_draw_glyph_string_foreground (next);
3862 else
3863 x_draw_composite_glyph_string_foreground (next);
3864 x_reset_clip_rectangles (next->f, next->gc);
3865 next->hl = save;
3866 next->num_clips = 0;
3867 next->clip_head = s->next;
3872 /* Reset clipping. */
3873 x_reset_clip_rectangles (s->f, s->gc);
3874 s->num_clips = 0;
3877 /* Shift display to make room for inserted glyphs. */
3879 static void
3880 x_shift_glyphs_for_insert (struct frame *f, int x, int y, int width, int height, int shift_by)
3882 /* Never called on a GUI frame, see
3883 https://lists.gnu.org/r/emacs-devel/2015-05/msg00456.html
3885 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f), FRAME_X_DRAWABLE (f),
3886 f->output_data.x->normal_gc,
3887 x, y, width, height,
3888 x + shift_by, y);
3891 /* Delete N glyphs at the nominal cursor position. Not implemented
3892 for X frames. */
3894 static void
3895 x_delete_glyphs (struct frame *f, register int n)
3897 emacs_abort ();
3901 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
3902 If they are <= 0, this is probably an error. */
3904 static ATTRIBUTE_UNUSED void
3905 x_clear_area1 (Display *dpy, Window window,
3906 int x, int y, int width, int height, int exposures)
3908 eassert (width > 0 && height > 0);
3909 XClearArea (dpy, window, x, y, width, height, exposures);
3912 void
3913 x_clear_area (struct frame *f, int x, int y, int width, int height)
3915 #ifdef USE_CAIRO
3916 cairo_t *cr;
3918 eassert (width > 0 && height > 0);
3920 cr = x_begin_cr_clip (f, NULL);
3921 x_set_cr_source_with_gc_background (f, f->output_data.x->normal_gc);
3922 cairo_rectangle (cr, x, y, width, height);
3923 cairo_fill (cr);
3924 x_end_cr_clip (f);
3925 #else
3926 if (FRAME_X_DOUBLE_BUFFERED_P (f))
3927 XFillRectangle (FRAME_X_DISPLAY (f),
3928 FRAME_X_DRAWABLE (f),
3929 f->output_data.x->reverse_gc,
3930 x, y, width, height);
3931 else
3932 x_clear_area1 (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3933 x, y, width, height, False);
3934 #endif
3938 /* Clear an entire frame. */
3940 static void
3941 x_clear_frame (struct frame *f)
3943 /* Clearing the frame will erase any cursor, so mark them all as no
3944 longer visible. */
3945 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
3947 block_input ();
3949 font_drop_xrender_surfaces (f);
3950 x_clear_window (f);
3952 /* We have to clear the scroll bars. If we have changed colors or
3953 something like that, then they should be notified. */
3954 x_scroll_bar_clear (f);
3956 XFlush (FRAME_X_DISPLAY (f));
3958 unblock_input ();
3961 /* RIF: Show hourglass cursor on frame F. */
3963 static void
3964 x_show_hourglass (struct frame *f)
3966 Display *dpy = FRAME_X_DISPLAY (f);
3968 if (dpy)
3970 struct x_output *x = FRAME_X_OUTPUT (f);
3971 #ifdef USE_X_TOOLKIT
3972 if (x->widget)
3973 #else
3974 if (FRAME_OUTER_WINDOW (f))
3975 #endif
3977 x->hourglass_p = true;
3979 if (!x->hourglass_window)
3981 unsigned long mask = CWCursor;
3982 XSetWindowAttributes attrs;
3983 #ifdef USE_GTK
3984 Window parent = FRAME_X_WINDOW (f);
3985 #else
3986 Window parent = FRAME_OUTER_WINDOW (f);
3987 #endif
3988 attrs.cursor = x->hourglass_cursor;
3990 x->hourglass_window = XCreateWindow
3991 (dpy, parent, 0, 0, 32000, 32000, 0, 0,
3992 InputOnly, CopyFromParent, mask, &attrs);
3995 XMapRaised (dpy, x->hourglass_window);
3996 XFlush (dpy);
4001 /* RIF: Cancel hourglass cursor on frame F. */
4003 static void
4004 x_hide_hourglass (struct frame *f)
4006 struct x_output *x = FRAME_X_OUTPUT (f);
4008 /* Watch out for newly created frames. */
4009 if (x->hourglass_window)
4011 XUnmapWindow (FRAME_X_DISPLAY (f), x->hourglass_window);
4012 /* Sync here because XTread_socket looks at the
4013 hourglass_p flag that is reset to zero below. */
4014 XSync (FRAME_X_DISPLAY (f), False);
4015 x->hourglass_p = false;
4019 /* Invert the middle quarter of the frame for .15 sec. */
4021 static void
4022 XTflash (struct frame *f)
4024 block_input ();
4027 #ifdef USE_GTK
4028 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
4029 when the scroll bars and the edit widget share the same X window. */
4030 GdkWindow *window = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
4031 #ifdef HAVE_GTK3
4032 #if GTK_CHECK_VERSION (3, 22, 0)
4033 cairo_region_t *region = gdk_window_get_visible_region (window);
4034 GdkDrawingContext *context = gdk_window_begin_draw_frame (window, region);
4035 cairo_t *cr = gdk_drawing_context_get_cairo_context (context);
4036 #else
4037 cairo_t *cr = gdk_cairo_create (window);
4038 #endif
4039 cairo_set_source_rgb (cr, 1, 1, 1);
4040 cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
4041 #define XFillRectangle(d, win, gc, x, y, w, h) \
4042 do { \
4043 cairo_rectangle (cr, x, y, w, h); \
4044 cairo_fill (cr); \
4046 while (false)
4047 #else /* ! HAVE_GTK3 */
4048 GdkGCValues vals;
4049 GdkGC *gc;
4050 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
4051 ^ FRAME_BACKGROUND_PIXEL (f));
4052 vals.function = GDK_XOR;
4053 gc = gdk_gc_new_with_values (window,
4054 &vals, GDK_GC_FUNCTION | GDK_GC_FOREGROUND);
4055 #define XFillRectangle(d, win, gc, x, y, w, h) \
4056 gdk_draw_rectangle (window, gc, true, x, y, w, h)
4057 #endif /* ! HAVE_GTK3 */
4058 #else /* ! USE_GTK */
4059 GC gc;
4061 /* Create a GC that will use the GXxor function to flip foreground
4062 pixels into background pixels. */
4064 XGCValues values;
4066 values.function = GXxor;
4067 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
4068 ^ FRAME_BACKGROUND_PIXEL (f));
4070 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4071 GCFunction | GCForeground, &values);
4073 #endif
4075 /* Get the height not including a menu bar widget. */
4076 int height = FRAME_PIXEL_HEIGHT (f);
4077 /* Height of each line to flash. */
4078 int flash_height = FRAME_LINE_HEIGHT (f);
4079 /* These will be the left and right margins of the rectangles. */
4080 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
4081 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
4082 int width = flash_right - flash_left;
4084 /* If window is tall, flash top and bottom line. */
4085 if (height > 3 * FRAME_LINE_HEIGHT (f))
4087 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4088 flash_left,
4089 (FRAME_INTERNAL_BORDER_WIDTH (f)
4090 + FRAME_TOP_MARGIN_HEIGHT (f)),
4091 width, flash_height);
4092 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4093 flash_left,
4094 (height - flash_height
4095 - FRAME_INTERNAL_BORDER_WIDTH (f)),
4096 width, flash_height);
4099 else
4100 /* If it is short, flash it all. */
4101 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4102 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
4103 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
4105 x_flush (f);
4108 struct timespec delay = make_timespec (0, 150 * 1000 * 1000);
4109 struct timespec wakeup = timespec_add (current_timespec (), delay);
4111 /* Keep waiting until past the time wakeup or any input gets
4112 available. */
4113 while (! detect_input_pending ())
4115 struct timespec current = current_timespec ();
4116 struct timespec timeout;
4118 /* Break if result would not be positive. */
4119 if (timespec_cmp (wakeup, current) <= 0)
4120 break;
4122 /* How long `select' should wait. */
4123 timeout = make_timespec (0, 10 * 1000 * 1000);
4125 /* Try to wait that long--but we might wake up sooner. */
4126 pselect (0, NULL, NULL, NULL, &timeout, NULL);
4130 /* If window is tall, flash top and bottom line. */
4131 if (height > 3 * FRAME_LINE_HEIGHT (f))
4133 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4134 flash_left,
4135 (FRAME_INTERNAL_BORDER_WIDTH (f)
4136 + FRAME_TOP_MARGIN_HEIGHT (f)),
4137 width, flash_height);
4138 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4139 flash_left,
4140 (height - flash_height
4141 - FRAME_INTERNAL_BORDER_WIDTH (f)),
4142 width, flash_height);
4144 else
4145 /* If it is short, flash it all. */
4146 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4147 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
4148 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
4150 #ifdef USE_GTK
4151 #ifdef HAVE_GTK3
4152 #if GTK_CHECK_VERSION (3, 22, 0)
4153 gdk_window_end_draw_frame (window, context);
4154 cairo_region_destroy (region);
4155 #else
4156 cairo_destroy (cr);
4157 #endif
4158 #else
4159 g_object_unref (G_OBJECT (gc));
4160 #endif
4161 #undef XFillRectangle
4162 #else
4163 XFreeGC (FRAME_X_DISPLAY (f), gc);
4164 #endif
4165 x_flush (f);
4169 unblock_input ();
4173 static void
4174 XTtoggle_invisible_pointer (struct frame *f, bool invisible)
4176 block_input ();
4177 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, invisible);
4178 unblock_input ();
4182 /* Make audible bell. */
4184 static void
4185 XTring_bell (struct frame *f)
4187 if (FRAME_X_DISPLAY (f))
4189 if (visible_bell)
4190 XTflash (f);
4191 else
4193 block_input ();
4194 #ifdef HAVE_XKB
4195 XkbBell (FRAME_X_DISPLAY (f), None, 0, None);
4196 #else
4197 XBell (FRAME_X_DISPLAY (f), 0);
4198 #endif
4199 XFlush (FRAME_X_DISPLAY (f));
4200 unblock_input ();
4205 /***********************************************************************
4206 Line Dance
4207 ***********************************************************************/
4209 /* Perform an insert-lines or delete-lines operation, inserting N
4210 lines or deleting -N lines at vertical position VPOS. */
4212 static void
4213 x_ins_del_lines (struct frame *f, int vpos, int n)
4215 emacs_abort ();
4219 /* Scroll part of the display as described by RUN. */
4221 static void
4222 x_scroll_run (struct window *w, struct run *run)
4224 struct frame *f = XFRAME (w->frame);
4225 int x, y, width, height, from_y, to_y, bottom_y;
4227 /* Get frame-relative bounding box of the text display area of W,
4228 without mode lines. Include in this box the left and right
4229 fringe of W. */
4230 window_box (w, ANY_AREA, &x, &y, &width, &height);
4232 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
4233 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
4234 bottom_y = y + height;
4236 if (to_y < from_y)
4238 /* Scrolling up. Make sure we don't copy part of the mode
4239 line at the bottom. */
4240 if (from_y + run->height > bottom_y)
4241 height = bottom_y - from_y;
4242 else
4243 height = run->height;
4245 else
4247 /* Scrolling down. Make sure we don't copy over the mode line.
4248 at the bottom. */
4249 if (to_y + run->height > bottom_y)
4250 height = bottom_y - to_y;
4251 else
4252 height = run->height;
4255 block_input ();
4257 /* Cursor off. Will be switched on again in x_update_window_end. */
4258 x_clear_cursor (w);
4260 #ifdef USE_CAIRO
4261 if (FRAME_CR_CONTEXT (f))
4263 int wx = WINDOW_LEFT_EDGE_X (w);
4264 cairo_surface_t *s = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
4265 width, height);
4266 cairo_t *cr = cairo_create (s);
4267 cairo_set_source_surface (cr, cairo_get_target (FRAME_CR_CONTEXT (f)),
4268 -x, -from_y);
4269 cairo_paint (cr);
4270 cairo_destroy (cr);
4272 cr = FRAME_CR_CONTEXT (f);
4273 cairo_save (cr);
4274 cairo_set_source_surface (cr, s, wx, to_y);
4275 cairo_rectangle (cr, wx, to_y, width, height);
4276 cairo_fill (cr);
4277 cairo_restore (cr);
4278 cairo_surface_destroy (s);
4280 else
4282 SET_FRAME_GARBAGED (f);
4284 #else
4285 XCopyArea (FRAME_X_DISPLAY (f),
4286 FRAME_X_DRAWABLE (f), FRAME_X_DRAWABLE (f),
4287 f->output_data.x->normal_gc,
4288 x, from_y,
4289 width, height,
4290 x, to_y);
4291 #endif
4293 unblock_input ();
4298 /***********************************************************************
4299 Exposure Events
4300 ***********************************************************************/
4303 static void
4304 frame_highlight (struct frame *f)
4306 /* We used to only do this if Vx_no_window_manager was non-nil, but
4307 the ICCCM (section 4.1.6) says that the window's border pixmap
4308 and border pixel are window attributes which are "private to the
4309 client", so we can always change it to whatever we want. */
4310 block_input ();
4311 /* I recently started to get errors in this XSetWindowBorder, depending on
4312 the window-manager in use, tho something more is at play since I've been
4313 using that same window-manager binary for ever. Let's not crash just
4314 because of this (bug#9310). */
4315 x_catch_errors (FRAME_X_DISPLAY (f));
4316 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4317 f->output_data.x->border_pixel);
4318 x_uncatch_errors ();
4319 unblock_input ();
4320 x_update_cursor (f, true);
4321 x_set_frame_alpha (f);
4324 static void
4325 frame_unhighlight (struct frame *f)
4327 /* We used to only do this if Vx_no_window_manager was non-nil, but
4328 the ICCCM (section 4.1.6) says that the window's border pixmap
4329 and border pixel are window attributes which are "private to the
4330 client", so we can always change it to whatever we want. */
4331 block_input ();
4332 /* Same as above for XSetWindowBorder (bug#9310). */
4333 x_catch_errors (FRAME_X_DISPLAY (f));
4334 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4335 f->output_data.x->border_tile);
4336 x_uncatch_errors ();
4337 unblock_input ();
4338 x_update_cursor (f, true);
4339 x_set_frame_alpha (f);
4342 /* The focus has changed. Update the frames as necessary to reflect
4343 the new situation. Note that we can't change the selected frame
4344 here, because the Lisp code we are interrupting might become confused.
4345 Each event gets marked with the frame in which it occurred, so the
4346 Lisp code can tell when the switch took place by examining the events. */
4348 static void
4349 x_new_focus_frame (struct x_display_info *dpyinfo, struct frame *frame)
4351 struct frame *old_focus = dpyinfo->x_focus_frame;
4353 if (frame != dpyinfo->x_focus_frame)
4355 /* Set this before calling other routines, so that they see
4356 the correct value of x_focus_frame. */
4357 dpyinfo->x_focus_frame = frame;
4359 if (old_focus && old_focus->auto_lower)
4360 x_lower_frame (old_focus);
4362 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
4363 dpyinfo->x_pending_autoraise_frame = dpyinfo->x_focus_frame;
4364 else
4365 dpyinfo->x_pending_autoraise_frame = NULL;
4368 x_frame_rehighlight (dpyinfo);
4371 /* Handle FocusIn and FocusOut state changes for FRAME.
4372 If FRAME has focus and there exists more than one frame, puts
4373 a FOCUS_IN_EVENT into *BUFP. */
4375 static void
4376 x_focus_changed (int type, int state, struct x_display_info *dpyinfo, struct frame *frame, struct input_event *bufp)
4378 if (type == FocusIn)
4380 if (dpyinfo->x_focus_event_frame != frame)
4382 x_new_focus_frame (dpyinfo, frame);
4383 dpyinfo->x_focus_event_frame = frame;
4385 /* Don't stop displaying the initial startup message
4386 for a switch-frame event we don't need. */
4387 /* When run as a daemon, Vterminal_frame is always NIL. */
4388 bufp->arg = (((NILP (Vterminal_frame)
4389 || ! FRAME_X_P (XFRAME (Vterminal_frame))
4390 || EQ (Fdaemonp (), Qt))
4391 && CONSP (Vframe_list)
4392 && !NILP (XCDR (Vframe_list)))
4393 ? Qt : Qnil);
4394 bufp->kind = FOCUS_IN_EVENT;
4395 XSETFRAME (bufp->frame_or_window, frame);
4398 frame->output_data.x->focus_state |= state;
4400 #ifdef HAVE_X_I18N
4401 if (FRAME_XIC (frame))
4402 XSetICFocus (FRAME_XIC (frame));
4403 #endif
4405 else if (type == FocusOut)
4407 frame->output_data.x->focus_state &= ~state;
4409 if (dpyinfo->x_focus_event_frame == frame)
4411 dpyinfo->x_focus_event_frame = 0;
4412 x_new_focus_frame (dpyinfo, 0);
4414 bufp->kind = FOCUS_OUT_EVENT;
4415 XSETFRAME (bufp->frame_or_window, frame);
4418 #ifdef HAVE_X_I18N
4419 if (FRAME_XIC (frame))
4420 XUnsetICFocus (FRAME_XIC (frame));
4421 #endif
4422 if (frame->pointer_invisible)
4423 XTtoggle_invisible_pointer (frame, false);
4427 /* Return the Emacs frame-object corresponding to an X window.
4428 It could be the frame's main window or an icon window. */
4430 static struct frame *
4431 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4433 Lisp_Object tail, frame;
4434 struct frame *f;
4436 if (wdesc == None)
4437 return NULL;
4439 FOR_EACH_FRAME (tail, frame)
4441 f = XFRAME (frame);
4442 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4443 continue;
4444 if (f->output_data.x->hourglass_window == wdesc)
4445 return f;
4446 #ifdef USE_X_TOOLKIT
4447 if ((f->output_data.x->edit_widget
4448 && XtWindow (f->output_data.x->edit_widget) == wdesc)
4449 /* A tooltip frame? */
4450 || (!f->output_data.x->edit_widget
4451 && FRAME_X_WINDOW (f) == wdesc)
4452 || f->output_data.x->icon_desc == wdesc)
4453 return f;
4454 #else /* not USE_X_TOOLKIT */
4455 #ifdef USE_GTK
4456 if (f->output_data.x->edit_widget)
4458 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4459 struct x_output *x = f->output_data.x;
4460 if (gwdesc != 0 && gwdesc == x->edit_widget)
4461 return f;
4463 #endif /* USE_GTK */
4464 if (FRAME_X_WINDOW (f) == wdesc
4465 || f->output_data.x->icon_desc == wdesc)
4466 return f;
4467 #endif /* not USE_X_TOOLKIT */
4469 return 0;
4472 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
4474 /* Like x_window_to_frame but also compares the window with the widget's
4475 windows. */
4477 static struct frame *
4478 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4480 Lisp_Object tail, frame;
4481 struct frame *f, *found = NULL;
4482 struct x_output *x;
4484 if (wdesc == None)
4485 return NULL;
4487 FOR_EACH_FRAME (tail, frame)
4489 if (found)
4490 break;
4491 f = XFRAME (frame);
4492 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
4494 /* This frame matches if the window is any of its widgets. */
4495 x = f->output_data.x;
4496 if (x->hourglass_window == wdesc)
4497 found = f;
4498 else if (x->widget)
4500 #ifdef USE_GTK
4501 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4502 if (gwdesc != 0
4503 && gtk_widget_get_toplevel (gwdesc) == x->widget)
4504 found = f;
4505 #else
4506 if (wdesc == XtWindow (x->widget)
4507 || wdesc == XtWindow (x->column_widget)
4508 || wdesc == XtWindow (x->edit_widget))
4509 found = f;
4510 /* Match if the window is this frame's menubar. */
4511 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
4512 found = f;
4513 #endif
4515 else if (FRAME_X_WINDOW (f) == wdesc)
4516 /* A tooltip frame. */
4517 found = f;
4521 return found;
4524 /* Likewise, but consider only the menu bar widget. */
4526 static struct frame *
4527 x_menubar_window_to_frame (struct x_display_info *dpyinfo,
4528 const XEvent *event)
4530 Window wdesc = event->xany.window;
4531 Lisp_Object tail, frame;
4532 struct frame *f;
4533 struct x_output *x;
4535 if (wdesc == None)
4536 return NULL;
4538 FOR_EACH_FRAME (tail, frame)
4540 f = XFRAME (frame);
4541 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4542 continue;
4543 x = f->output_data.x;
4544 #ifdef USE_GTK
4545 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
4546 return f;
4547 #else
4548 /* Match if the window is this frame's menubar. */
4549 if (x->menubar_widget
4550 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
4551 return f;
4552 #endif
4554 return 0;
4557 /* Return the frame whose principal (outermost) window is WDESC.
4558 If WDESC is some other (smaller) window, we return 0. */
4560 struct frame *
4561 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4563 Lisp_Object tail, frame;
4564 struct frame *f;
4565 struct x_output *x;
4567 if (wdesc == None)
4568 return NULL;
4570 FOR_EACH_FRAME (tail, frame)
4572 f = XFRAME (frame);
4573 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4574 continue;
4575 x = f->output_data.x;
4577 if (x->widget)
4579 /* This frame matches if the window is its topmost widget. */
4580 #ifdef USE_GTK
4581 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4582 if (gwdesc == x->widget)
4583 return f;
4584 #else
4585 if (wdesc == XtWindow (x->widget))
4586 return f;
4587 #endif
4589 else if (FRAME_X_WINDOW (f) == wdesc)
4590 /* Tooltip frame. */
4591 return f;
4593 return 0;
4596 #else /* !USE_X_TOOLKIT && !USE_GTK */
4598 #define x_any_window_to_frame(d, i) x_window_to_frame (d, i)
4599 #define x_top_window_to_frame(d, i) x_window_to_frame (d, i)
4601 #endif /* USE_X_TOOLKIT || USE_GTK */
4603 /* The focus may have changed. Figure out if it is a real focus change,
4604 by checking both FocusIn/Out and Enter/LeaveNotify events.
4606 Returns FOCUS_IN_EVENT event in *BUFP. */
4608 static void
4609 x_detect_focus_change (struct x_display_info *dpyinfo, struct frame *frame,
4610 const XEvent *event, struct input_event *bufp)
4612 if (!frame)
4613 return;
4615 switch (event->type)
4617 case EnterNotify:
4618 case LeaveNotify:
4620 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
4621 int focus_state
4622 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
4624 if (event->xcrossing.detail != NotifyInferior
4625 && event->xcrossing.focus
4626 && ! (focus_state & FOCUS_EXPLICIT))
4627 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
4628 FOCUS_IMPLICIT,
4629 dpyinfo, frame, bufp);
4631 break;
4633 case FocusIn:
4634 case FocusOut:
4635 x_focus_changed (event->type,
4636 (event->xfocus.detail == NotifyPointer ?
4637 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
4638 dpyinfo, frame, bufp);
4639 break;
4641 case ClientMessage:
4642 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
4644 enum xembed_message msg = event->xclient.data.l[1];
4645 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
4646 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
4648 break;
4653 #if !defined USE_X_TOOLKIT && !defined USE_GTK
4654 /* Handle an event saying the mouse has moved out of an Emacs frame. */
4656 void
4657 x_mouse_leave (struct x_display_info *dpyinfo)
4659 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
4661 #endif
4663 /* The focus has changed, or we have redirected a frame's focus to
4664 another frame (this happens when a frame uses a surrogate
4665 mini-buffer frame). Shift the highlight as appropriate.
4667 The FRAME argument doesn't necessarily have anything to do with which
4668 frame is being highlighted or un-highlighted; we only use it to find
4669 the appropriate X display info. */
4671 static void
4672 XTframe_rehighlight (struct frame *frame)
4674 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
4677 static void
4678 x_frame_rehighlight (struct x_display_info *dpyinfo)
4680 struct frame *old_highlight = dpyinfo->x_highlight_frame;
4682 if (dpyinfo->x_focus_frame)
4684 dpyinfo->x_highlight_frame
4685 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
4686 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
4687 : dpyinfo->x_focus_frame);
4688 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
4690 fset_focus_frame (dpyinfo->x_focus_frame, Qnil);
4691 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
4694 else
4695 dpyinfo->x_highlight_frame = 0;
4697 if (dpyinfo->x_highlight_frame != old_highlight)
4699 if (old_highlight)
4700 frame_unhighlight (old_highlight);
4701 if (dpyinfo->x_highlight_frame)
4702 frame_highlight (dpyinfo->x_highlight_frame);
4708 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
4710 /* Initialize mode_switch_bit and modifier_meaning. */
4711 static void
4712 x_find_modifier_meanings (struct x_display_info *dpyinfo)
4714 int min_code, max_code;
4715 KeySym *syms;
4716 int syms_per_code;
4717 XModifierKeymap *mods;
4719 dpyinfo->meta_mod_mask = 0;
4720 dpyinfo->shift_lock_mask = 0;
4721 dpyinfo->alt_mod_mask = 0;
4722 dpyinfo->super_mod_mask = 0;
4723 dpyinfo->hyper_mod_mask = 0;
4725 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
4727 syms = XGetKeyboardMapping (dpyinfo->display,
4728 min_code, max_code - min_code + 1,
4729 &syms_per_code);
4730 mods = XGetModifierMapping (dpyinfo->display);
4732 /* Scan the modifier table to see which modifier bits the Meta and
4733 Alt keysyms are on. */
4735 int row, col; /* The row and column in the modifier table. */
4736 bool found_alt_or_meta;
4738 for (row = 3; row < 8; row++)
4740 found_alt_or_meta = false;
4741 for (col = 0; col < mods->max_keypermod; col++)
4743 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
4745 /* Zeroes are used for filler. Skip them. */
4746 if (code == 0)
4747 continue;
4749 /* Are any of this keycode's keysyms a meta key? */
4751 int code_col;
4753 for (code_col = 0; code_col < syms_per_code; code_col++)
4755 int sym = syms[((code - min_code) * syms_per_code) + code_col];
4757 switch (sym)
4759 case XK_Meta_L:
4760 case XK_Meta_R:
4761 found_alt_or_meta = true;
4762 dpyinfo->meta_mod_mask |= (1 << row);
4763 break;
4765 case XK_Alt_L:
4766 case XK_Alt_R:
4767 found_alt_or_meta = true;
4768 dpyinfo->alt_mod_mask |= (1 << row);
4769 break;
4771 case XK_Hyper_L:
4772 case XK_Hyper_R:
4773 if (!found_alt_or_meta)
4774 dpyinfo->hyper_mod_mask |= (1 << row);
4775 code_col = syms_per_code;
4776 col = mods->max_keypermod;
4777 break;
4779 case XK_Super_L:
4780 case XK_Super_R:
4781 if (!found_alt_or_meta)
4782 dpyinfo->super_mod_mask |= (1 << row);
4783 code_col = syms_per_code;
4784 col = mods->max_keypermod;
4785 break;
4787 case XK_Shift_Lock:
4788 /* Ignore this if it's not on the lock modifier. */
4789 if (!found_alt_or_meta && ((1 << row) == LockMask))
4790 dpyinfo->shift_lock_mask = LockMask;
4791 code_col = syms_per_code;
4792 col = mods->max_keypermod;
4793 break;
4801 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
4802 if (! dpyinfo->meta_mod_mask)
4804 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
4805 dpyinfo->alt_mod_mask = 0;
4808 /* If some keys are both alt and meta,
4809 make them just meta, not alt. */
4810 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
4812 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
4815 XFree (syms);
4816 XFreeModifiermap (mods);
4819 /* Convert between the modifier bits X uses and the modifier bits
4820 Emacs uses. */
4823 x_x_to_emacs_modifiers (struct x_display_info *dpyinfo, int state)
4825 int mod_ctrl = ctrl_modifier;
4826 int mod_meta = meta_modifier;
4827 int mod_alt = alt_modifier;
4828 int mod_hyper = hyper_modifier;
4829 int mod_super = super_modifier;
4830 Lisp_Object tem;
4832 tem = Fget (Vx_ctrl_keysym, Qmodifier_value);
4833 if (INTEGERP (tem)) mod_ctrl = XINT (tem) & INT_MAX;
4834 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4835 if (INTEGERP (tem)) mod_alt = XINT (tem) & INT_MAX;
4836 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4837 if (INTEGERP (tem)) mod_meta = XINT (tem) & INT_MAX;
4838 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4839 if (INTEGERP (tem)) mod_hyper = XINT (tem) & INT_MAX;
4840 tem = Fget (Vx_super_keysym, Qmodifier_value);
4841 if (INTEGERP (tem)) mod_super = XINT (tem) & INT_MAX;
4843 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
4844 | ((state & ControlMask) ? mod_ctrl : 0)
4845 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
4846 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
4847 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
4848 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
4851 static int
4852 x_emacs_to_x_modifiers (struct x_display_info *dpyinfo, EMACS_INT state)
4854 EMACS_INT mod_ctrl = ctrl_modifier;
4855 EMACS_INT mod_meta = meta_modifier;
4856 EMACS_INT mod_alt = alt_modifier;
4857 EMACS_INT mod_hyper = hyper_modifier;
4858 EMACS_INT mod_super = super_modifier;
4860 Lisp_Object tem;
4862 tem = Fget (Vx_ctrl_keysym, Qmodifier_value);
4863 if (INTEGERP (tem)) mod_ctrl = XINT (tem);
4864 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4865 if (INTEGERP (tem)) mod_alt = XINT (tem);
4866 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4867 if (INTEGERP (tem)) mod_meta = XINT (tem);
4868 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4869 if (INTEGERP (tem)) mod_hyper = XINT (tem);
4870 tem = Fget (Vx_super_keysym, Qmodifier_value);
4871 if (INTEGERP (tem)) mod_super = XINT (tem);
4874 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
4875 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
4876 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
4877 | ((state & shift_modifier) ? ShiftMask : 0)
4878 | ((state & mod_ctrl) ? ControlMask : 0)
4879 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
4882 /* Convert a keysym to its name. */
4884 char *
4885 x_get_keysym_name (int keysym)
4887 char *value;
4889 block_input ();
4890 value = XKeysymToString (keysym);
4891 unblock_input ();
4893 return value;
4896 /* Mouse clicks and mouse movement. Rah.
4898 Formerly, we used PointerMotionHintMask (in standard_event_mask)
4899 so that we would have to call XQueryPointer after each MotionNotify
4900 event to ask for another such event. However, this made mouse tracking
4901 slow, and there was a bug that made it eventually stop.
4903 Simply asking for MotionNotify all the time seems to work better.
4905 In order to avoid asking for motion events and then throwing most
4906 of them away or busy-polling the server for mouse positions, we ask
4907 the server for pointer motion hints. This means that we get only
4908 one event per group of mouse movements. "Groups" are delimited by
4909 other kinds of events (focus changes and button clicks, for
4910 example), or by XQueryPointer calls; when one of these happens, we
4911 get another MotionNotify event the next time the mouse moves. This
4912 is at least as efficient as getting motion events when mouse
4913 tracking is on, and I suspect only negligibly worse when tracking
4914 is off. */
4916 /* Prepare a mouse-event in *RESULT for placement in the input queue.
4918 If the event is a button press, then note that we have grabbed
4919 the mouse. */
4921 static Lisp_Object
4922 construct_mouse_click (struct input_event *result,
4923 const XButtonEvent *event,
4924 struct frame *f)
4926 /* Make the event type NO_EVENT; we'll change that when we decide
4927 otherwise. */
4928 result->kind = MOUSE_CLICK_EVENT;
4929 result->code = event->button - Button1;
4930 result->timestamp = event->time;
4931 result->modifiers = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f),
4932 event->state)
4933 | (event->type == ButtonRelease
4934 ? up_modifier
4935 : down_modifier));
4937 XSETINT (result->x, event->x);
4938 XSETINT (result->y, event->y);
4939 XSETFRAME (result->frame_or_window, f);
4940 result->arg = Qnil;
4941 return Qnil;
4944 /* Function to report a mouse movement to the mainstream Emacs code.
4945 The input handler calls this.
4947 We have received a mouse movement event, which is given in *event.
4948 If the mouse is over a different glyph than it was last time, tell
4949 the mainstream emacs code by setting mouse_moved. If not, ask for
4950 another motion event, so we can check again the next time it moves. */
4952 static bool
4953 note_mouse_movement (struct frame *frame, const XMotionEvent *event)
4955 XRectangle *r;
4956 struct x_display_info *dpyinfo;
4958 if (!FRAME_X_OUTPUT (frame))
4959 return false;
4961 dpyinfo = FRAME_DISPLAY_INFO (frame);
4962 dpyinfo->last_mouse_movement_time = event->time;
4963 dpyinfo->last_mouse_motion_frame = frame;
4964 dpyinfo->last_mouse_motion_x = event->x;
4965 dpyinfo->last_mouse_motion_y = event->y;
4967 if (event->window != FRAME_X_WINDOW (frame))
4969 frame->mouse_moved = true;
4970 dpyinfo->last_mouse_scroll_bar = NULL;
4971 note_mouse_highlight (frame, -1, -1);
4972 dpyinfo->last_mouse_glyph_frame = NULL;
4973 return true;
4977 /* Has the mouse moved off the glyph it was on at the last sighting? */
4978 r = &dpyinfo->last_mouse_glyph;
4979 if (frame != dpyinfo->last_mouse_glyph_frame
4980 || event->x < r->x || event->x >= r->x + r->width
4981 || event->y < r->y || event->y >= r->y + r->height)
4983 frame->mouse_moved = true;
4984 dpyinfo->last_mouse_scroll_bar = NULL;
4985 note_mouse_highlight (frame, event->x, event->y);
4986 /* Remember which glyph we're now on. */
4987 remember_mouse_glyph (frame, event->x, event->y, r);
4988 dpyinfo->last_mouse_glyph_frame = frame;
4989 return true;
4992 return false;
4995 /* Return the current position of the mouse.
4996 *FP should be a frame which indicates which display to ask about.
4998 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
4999 and *PART to the frame, window, and scroll bar part that the mouse
5000 is over. Set *X and *Y to the portion and whole of the mouse's
5001 position on the scroll bar.
5003 If the mouse movement started elsewhere, set *FP to the frame the
5004 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
5005 the mouse is over.
5007 Set *TIMESTAMP to the server time-stamp for the time at which the mouse
5008 was at this position.
5010 Don't store anything if we don't have a valid set of values to report.
5012 This clears the mouse_moved flag, so we can wait for the next mouse
5013 movement. */
5015 static void
5016 XTmouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
5017 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
5018 Time *timestamp)
5020 struct frame *f1;
5021 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
5023 block_input ();
5025 if (dpyinfo->last_mouse_scroll_bar && insist == 0)
5027 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
5029 if (bar->horizontal)
5030 x_horizontal_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
5031 else
5032 x_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
5034 else
5036 Window root;
5037 int root_x, root_y;
5039 Window dummy_window;
5040 int dummy;
5042 Lisp_Object frame, tail;
5044 /* Clear the mouse-moved flag for every frame on this display. */
5045 FOR_EACH_FRAME (tail, frame)
5046 if (FRAME_X_P (XFRAME (frame))
5047 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
5048 XFRAME (frame)->mouse_moved = false;
5050 dpyinfo->last_mouse_scroll_bar = NULL;
5052 /* Figure out which root window we're on. */
5053 XQueryPointer (FRAME_X_DISPLAY (*fp),
5054 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
5056 /* The root window which contains the pointer. */
5057 &root,
5059 /* Trash which we can't trust if the pointer is on
5060 a different screen. */
5061 &dummy_window,
5063 /* The position on that root window. */
5064 &root_x, &root_y,
5066 /* More trash we can't trust. */
5067 &dummy, &dummy,
5069 /* Modifier keys and pointer buttons, about which
5070 we don't care. */
5071 (unsigned int *) &dummy);
5073 /* Now we have a position on the root; find the innermost window
5074 containing the pointer. */
5076 Window win, child;
5077 #ifdef USE_GTK
5078 Window first_win = 0;
5079 #endif
5080 int win_x, win_y;
5081 int parent_x = 0, parent_y = 0;
5083 win = root;
5085 /* XTranslateCoordinates can get errors if the window
5086 structure is changing at the same time this function
5087 is running. So at least we must not crash from them. */
5089 x_catch_errors (FRAME_X_DISPLAY (*fp));
5091 if (x_mouse_grabbed (dpyinfo))
5093 /* If mouse was grabbed on a frame, give coords for that frame
5094 even if the mouse is now outside it. */
5095 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
5097 /* From-window. */
5098 root,
5100 /* To-window. */
5101 FRAME_X_WINDOW (dpyinfo->last_mouse_frame),
5103 /* From-position, to-position. */
5104 root_x, root_y, &win_x, &win_y,
5106 /* Child of win. */
5107 &child);
5108 f1 = dpyinfo->last_mouse_frame;
5110 else
5112 while (true)
5114 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
5116 /* From-window, to-window. */
5117 root, win,
5119 /* From-position, to-position. */
5120 root_x, root_y, &win_x, &win_y,
5122 /* Child of win. */
5123 &child);
5125 if (child == None || child == win)
5127 #ifdef USE_GTK
5128 /* On GTK we have not inspected WIN yet. If it has
5129 a frame and that frame has a parent, use it. */
5130 struct frame *f = x_window_to_frame (dpyinfo, win);
5132 if (f && FRAME_PARENT_FRAME (f))
5133 first_win = win;
5134 #endif
5135 break;
5137 #ifdef USE_GTK
5138 /* We don't wan't to know the innermost window. We
5139 want the edit window. For non-Gtk+ the innermost
5140 window is the edit window. For Gtk+ it might not
5141 be. It might be the tool bar for example. */
5142 if (x_window_to_frame (dpyinfo, win))
5143 /* But don't hurry. We might find a child frame
5144 beneath. */
5145 first_win = win;
5146 #endif
5147 win = child;
5148 parent_x = win_x;
5149 parent_y = win_y;
5152 #ifdef USE_GTK
5153 if (first_win)
5154 win = first_win;
5155 #endif
5157 /* Now we know that:
5158 win is the innermost window containing the pointer
5159 (XTC says it has no child containing the pointer),
5160 win_x and win_y are the pointer's position in it
5161 (XTC did this the last time through), and
5162 parent_x and parent_y are the pointer's position in win's parent.
5163 (They are what win_x and win_y were when win was child.
5164 If win is the root window, it has no parent, and
5165 parent_{x,y} are invalid, but that's okay, because we'll
5166 never use them in that case.) */
5168 #ifdef USE_GTK
5169 /* We don't wan't to know the innermost window. We
5170 want the edit window. */
5171 f1 = x_window_to_frame (dpyinfo, win);
5172 #else
5173 /* Is win one of our frames? */
5174 f1 = x_any_window_to_frame (dpyinfo, win);
5175 #endif
5177 #ifdef USE_X_TOOLKIT
5178 /* If we end up with the menu bar window, say it's not
5179 on the frame. */
5180 if (f1 != NULL
5181 && f1->output_data.x->menubar_widget
5182 && win == XtWindow (f1->output_data.x->menubar_widget))
5183 f1 = NULL;
5184 #endif /* USE_X_TOOLKIT */
5187 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
5188 f1 = 0;
5190 x_uncatch_errors_after_check ();
5192 /* If not, is it one of our scroll bars? */
5193 if (! f1)
5195 struct scroll_bar *bar;
5197 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win, 2);
5199 if (bar)
5201 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5202 win_x = parent_x;
5203 win_y = parent_y;
5207 if (f1 == 0 && insist > 0)
5208 f1 = SELECTED_FRAME ();
5210 if (f1)
5212 /* Ok, we found a frame. Store all the values.
5213 last_mouse_glyph is a rectangle used to reduce the
5214 generation of mouse events. To not miss any motion
5215 events, we must divide the frame into rectangles of the
5216 size of the smallest character that could be displayed
5217 on it, i.e. into the same rectangles that matrices on
5218 the frame are divided into. */
5220 /* FIXME: what if F1 is not an X frame? */
5221 dpyinfo = FRAME_DISPLAY_INFO (f1);
5222 remember_mouse_glyph (f1, win_x, win_y, &dpyinfo->last_mouse_glyph);
5223 dpyinfo->last_mouse_glyph_frame = f1;
5225 *bar_window = Qnil;
5226 *part = 0;
5227 *fp = f1;
5228 XSETINT (*x, win_x);
5229 XSETINT (*y, win_y);
5230 *timestamp = dpyinfo->last_mouse_movement_time;
5235 unblock_input ();
5240 /***********************************************************************
5241 Scroll bars
5242 ***********************************************************************/
5244 /* Scroll bar support. */
5246 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
5247 manages it.
5248 This can be called in GC, so we have to make sure to strip off mark
5249 bits. */
5251 static struct scroll_bar *
5252 x_window_to_scroll_bar (Display *display, Window window_id, int type)
5254 Lisp_Object tail, frame;
5256 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
5257 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
5258 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
5260 FOR_EACH_FRAME (tail, frame)
5262 Lisp_Object bar, condemned;
5264 if (! FRAME_X_P (XFRAME (frame)))
5265 continue;
5267 /* Scan this frame's scroll bar list for a scroll bar with the
5268 right window ID. */
5269 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
5270 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
5271 /* This trick allows us to search both the ordinary and
5272 condemned scroll bar lists with one loop. */
5273 ! NILP (bar) || (bar = condemned,
5274 condemned = Qnil,
5275 ! NILP (bar));
5276 bar = XSCROLL_BAR (bar)->next)
5277 if (XSCROLL_BAR (bar)->x_window == window_id
5278 && FRAME_X_DISPLAY (XFRAME (frame)) == display
5279 && (type = 2
5280 || (type == 1 && XSCROLL_BAR (bar)->horizontal)
5281 || (type == 0 && !XSCROLL_BAR (bar)->horizontal)))
5282 return XSCROLL_BAR (bar);
5285 return NULL;
5289 #if defined USE_LUCID
5291 /* Return the Lucid menu bar WINDOW is part of. Return null
5292 if WINDOW is not part of a menu bar. */
5294 static Widget
5295 x_window_to_menu_bar (Window window)
5297 Lisp_Object tail, frame;
5299 FOR_EACH_FRAME (tail, frame)
5300 if (FRAME_X_P (XFRAME (frame)))
5302 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
5304 if (menu_bar && xlwmenu_window_p (menu_bar, window))
5305 return menu_bar;
5307 return NULL;
5310 #endif /* USE_LUCID */
5313 /************************************************************************
5314 Toolkit scroll bars
5315 ************************************************************************/
5317 #ifdef USE_TOOLKIT_SCROLL_BARS
5319 static void x_send_scroll_bar_event (Lisp_Object, enum scroll_bar_part,
5320 int, int, bool);
5322 /* Lisp window being scrolled. Set when starting to interact with
5323 a toolkit scroll bar, reset to nil when ending the interaction. */
5325 static Lisp_Object window_being_scrolled;
5327 /* Whether this is an Xaw with arrow-scrollbars. This should imply
5328 that movements of 1/20 of the screen size are mapped to up/down. */
5330 #ifndef USE_GTK
5331 /* Id of action hook installed for scroll bars. */
5333 static XtActionHookId action_hook_id;
5334 static XtActionHookId horizontal_action_hook_id;
5336 static Boolean xaw3d_arrow_scroll;
5338 /* Whether the drag scrolling maintains the mouse at the top of the
5339 thumb. If not, resizing the thumb needs to be done more carefully
5340 to avoid jerkiness. */
5342 static Boolean xaw3d_pick_top;
5344 /* Action hook installed via XtAppAddActionHook when toolkit scroll
5345 bars are used.. The hook is responsible for detecting when
5346 the user ends an interaction with the scroll bar, and generates
5347 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
5349 static void
5350 xt_action_hook (Widget widget, XtPointer client_data, String action_name,
5351 XEvent *event, String *params, Cardinal *num_params)
5353 bool scroll_bar_p;
5354 const char *end_action;
5356 #ifdef USE_MOTIF
5357 scroll_bar_p = XmIsScrollBar (widget);
5358 end_action = "Release";
5359 #else /* !USE_MOTIF i.e. use Xaw */
5360 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
5361 end_action = "EndScroll";
5362 #endif /* USE_MOTIF */
5364 if (scroll_bar_p
5365 && strcmp (action_name, end_action) == 0
5366 && WINDOWP (window_being_scrolled))
5368 struct window *w;
5369 struct scroll_bar *bar;
5371 x_send_scroll_bar_event (window_being_scrolled,
5372 scroll_bar_end_scroll, 0, 0, false);
5373 w = XWINDOW (window_being_scrolled);
5374 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5376 if (bar->dragging != -1)
5378 bar->dragging = -1;
5379 /* The thumb size is incorrect while dragging: fix it. */
5380 set_vertical_scroll_bar (w);
5382 window_being_scrolled = Qnil;
5383 #if defined (USE_LUCID)
5384 bar->last_seen_part = scroll_bar_nowhere;
5385 #endif
5386 /* Xt timeouts no longer needed. */
5387 toolkit_scroll_bar_interaction = false;
5392 static void
5393 xt_horizontal_action_hook (Widget widget, XtPointer client_data, String action_name,
5394 XEvent *event, String *params, Cardinal *num_params)
5396 bool scroll_bar_p;
5397 const char *end_action;
5399 #ifdef USE_MOTIF
5400 scroll_bar_p = XmIsScrollBar (widget);
5401 end_action = "Release";
5402 #else /* !USE_MOTIF i.e. use Xaw */
5403 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
5404 end_action = "EndScroll";
5405 #endif /* USE_MOTIF */
5407 if (scroll_bar_p
5408 && strcmp (action_name, end_action) == 0
5409 && WINDOWP (window_being_scrolled))
5411 struct window *w;
5412 struct scroll_bar *bar;
5414 x_send_scroll_bar_event (window_being_scrolled,
5415 scroll_bar_end_scroll, 0, 0, true);
5416 w = XWINDOW (window_being_scrolled);
5417 if (!NILP (w->horizontal_scroll_bar))
5419 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
5420 if (bar->dragging != -1)
5422 bar->dragging = -1;
5423 /* The thumb size is incorrect while dragging: fix it. */
5424 set_horizontal_scroll_bar (w);
5426 window_being_scrolled = Qnil;
5427 #if defined (USE_LUCID)
5428 bar->last_seen_part = scroll_bar_nowhere;
5429 #endif
5430 /* Xt timeouts no longer needed. */
5431 toolkit_scroll_bar_interaction = false;
5435 #endif /* not USE_GTK */
5437 /* Send a client message with message type Xatom_Scrollbar for a
5438 scroll action to the frame of WINDOW. PART is a value identifying
5439 the part of the scroll bar that was clicked on. PORTION is the
5440 amount to scroll of a whole of WHOLE. */
5442 static void
5443 x_send_scroll_bar_event (Lisp_Object window, enum scroll_bar_part part,
5444 int portion, int whole, bool horizontal)
5446 XEvent event;
5447 XClientMessageEvent *ev = &event.xclient;
5448 struct window *w = XWINDOW (window);
5449 struct frame *f = XFRAME (w->frame);
5450 intptr_t iw = (intptr_t) w;
5451 verify (INTPTR_WIDTH <= 64);
5452 int sign_shift = INTPTR_WIDTH - 32;
5454 block_input ();
5456 /* Construct a ClientMessage event to send to the frame. */
5457 ev->type = ClientMessage;
5458 ev->message_type = (horizontal
5459 ? FRAME_DISPLAY_INFO (f)->Xatom_Horizontal_Scrollbar
5460 : FRAME_DISPLAY_INFO (f)->Xatom_Scrollbar);
5461 ev->display = FRAME_X_DISPLAY (f);
5462 ev->window = FRAME_X_WINDOW (f);
5463 ev->format = 32;
5465 /* A 32-bit X client on a 64-bit X server can pass a window pointer
5466 as-is. A 64-bit client on a 32-bit X server is in trouble
5467 because a pointer does not fit and would be truncated while
5468 passing through the server. So use two slots and hope that X12
5469 will resolve such issues someday. */
5470 ev->data.l[0] = iw >> 31 >> 1;
5471 ev->data.l[1] = sign_shift <= 0 ? iw : iw << sign_shift >> sign_shift;
5472 ev->data.l[2] = part;
5473 ev->data.l[3] = portion;
5474 ev->data.l[4] = whole;
5476 /* Make Xt timeouts work while the scroll bar is active. */
5477 #ifdef USE_X_TOOLKIT
5478 toolkit_scroll_bar_interaction = true;
5479 x_activate_timeout_atimer ();
5480 #endif
5482 /* Setting the event mask to zero means that the message will
5483 be sent to the client that created the window, and if that
5484 window no longer exists, no event will be sent. */
5485 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
5486 unblock_input ();
5490 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
5491 in *IEVENT. */
5493 static void
5494 x_scroll_bar_to_input_event (const XEvent *event,
5495 struct input_event *ievent)
5497 const XClientMessageEvent *ev = &event->xclient;
5498 Lisp_Object window;
5499 struct window *w;
5501 /* See the comment in the function above. */
5502 intptr_t iw0 = ev->data.l[0];
5503 intptr_t iw1 = ev->data.l[1];
5504 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
5505 w = (struct window *) iw;
5507 XSETWINDOW (window, w);
5509 ievent->kind = SCROLL_BAR_CLICK_EVENT;
5510 ievent->frame_or_window = window;
5511 ievent->arg = Qnil;
5512 #ifdef USE_GTK
5513 ievent->timestamp = CurrentTime;
5514 #else
5515 ievent->timestamp =
5516 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
5517 #endif
5518 ievent->code = 0;
5519 ievent->part = ev->data.l[2];
5520 ievent->x = make_number (ev->data.l[3]);
5521 ievent->y = make_number (ev->data.l[4]);
5522 ievent->modifiers = 0;
5525 /* Transform a horizontal scroll bar ClientMessage EVENT to an Emacs
5526 input event in *IEVENT. */
5528 static void
5529 x_horizontal_scroll_bar_to_input_event (const XEvent *event,
5530 struct input_event *ievent)
5532 const XClientMessageEvent *ev = &event->xclient;
5533 Lisp_Object window;
5534 struct window *w;
5536 /* See the comment in the function above. */
5537 intptr_t iw0 = ev->data.l[0];
5538 intptr_t iw1 = ev->data.l[1];
5539 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
5540 w = (struct window *) iw;
5542 XSETWINDOW (window, w);
5544 ievent->kind = HORIZONTAL_SCROLL_BAR_CLICK_EVENT;
5545 ievent->frame_or_window = window;
5546 ievent->arg = Qnil;
5547 #ifdef USE_GTK
5548 ievent->timestamp = CurrentTime;
5549 #else
5550 ievent->timestamp =
5551 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
5552 #endif
5553 ievent->code = 0;
5554 ievent->part = ev->data.l[2];
5555 ievent->x = make_number (ev->data.l[3]);
5556 ievent->y = make_number (ev->data.l[4]);
5557 ievent->modifiers = 0;
5561 #ifdef USE_MOTIF
5563 /* Minimum and maximum values used for Motif scroll bars. */
5565 #define XM_SB_MAX 10000000
5567 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
5568 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
5569 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
5571 static void
5572 xm_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5574 struct scroll_bar *bar = client_data;
5575 XmScrollBarCallbackStruct *cs = call_data;
5576 enum scroll_bar_part part = scroll_bar_nowhere;
5577 bool horizontal = bar->horizontal;
5578 int whole = 0, portion = 0;
5580 switch (cs->reason)
5582 case XmCR_DECREMENT:
5583 bar->dragging = -1;
5584 part = horizontal ? scroll_bar_left_arrow : scroll_bar_up_arrow;
5585 break;
5587 case XmCR_INCREMENT:
5588 bar->dragging = -1;
5589 part = horizontal ? scroll_bar_right_arrow : scroll_bar_down_arrow;
5590 break;
5592 case XmCR_PAGE_DECREMENT:
5593 bar->dragging = -1;
5594 part = horizontal ? scroll_bar_before_handle : scroll_bar_above_handle;
5595 break;
5597 case XmCR_PAGE_INCREMENT:
5598 bar->dragging = -1;
5599 part = horizontal ? scroll_bar_after_handle : scroll_bar_below_handle;
5600 break;
5602 case XmCR_TO_TOP:
5603 bar->dragging = -1;
5604 part = horizontal ? scroll_bar_to_leftmost : scroll_bar_to_top;
5605 break;
5607 case XmCR_TO_BOTTOM:
5608 bar->dragging = -1;
5609 part = horizontal ? scroll_bar_to_rightmost : scroll_bar_to_bottom;
5610 break;
5612 case XmCR_DRAG:
5614 int slider_size;
5616 block_input ();
5617 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
5618 unblock_input ();
5620 if (horizontal)
5622 portion = bar->whole * ((float)cs->value / XM_SB_MAX);
5623 whole = bar->whole * ((float)(XM_SB_MAX - slider_size) / XM_SB_MAX);
5624 portion = min (portion, whole);
5625 part = scroll_bar_horizontal_handle;
5627 else
5629 whole = XM_SB_MAX - slider_size;
5630 portion = min (cs->value, whole);
5631 part = scroll_bar_handle;
5634 bar->dragging = cs->value;
5636 break;
5638 case XmCR_VALUE_CHANGED:
5639 break;
5642 if (part != scroll_bar_nowhere)
5644 window_being_scrolled = bar->window;
5645 x_send_scroll_bar_event (bar->window, part, portion, whole,
5646 bar->horizontal);
5650 #elif defined USE_GTK
5652 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
5653 bar widget. DATA is a pointer to the scroll_bar structure. */
5655 static gboolean
5656 xg_scroll_callback (GtkRange *range,
5657 GtkScrollType scroll,
5658 gdouble value,
5659 gpointer user_data)
5661 int whole = 0, portion = 0;
5662 struct scroll_bar *bar = user_data;
5663 enum scroll_bar_part part = scroll_bar_nowhere;
5664 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
5665 struct frame *f = g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
5667 if (xg_ignore_gtk_scrollbar) return false;
5669 switch (scroll)
5671 case GTK_SCROLL_JUMP:
5672 /* Buttons 1 2 or 3 must be grabbed. */
5673 if (FRAME_DISPLAY_INFO (f)->grabbed != 0
5674 && FRAME_DISPLAY_INFO (f)->grabbed < (1 << 4))
5676 if (bar->horizontal)
5678 part = scroll_bar_horizontal_handle;
5679 whole = (int)(gtk_adjustment_get_upper (adj) -
5680 gtk_adjustment_get_page_size (adj));
5681 portion = min ((int)value, whole);
5682 bar->dragging = portion;
5684 else
5686 part = scroll_bar_handle;
5687 whole = gtk_adjustment_get_upper (adj) -
5688 gtk_adjustment_get_page_size (adj);
5689 portion = min ((int)value, whole);
5690 bar->dragging = portion;
5693 break;
5694 case GTK_SCROLL_STEP_BACKWARD:
5695 part = (bar->horizontal
5696 ? scroll_bar_left_arrow : scroll_bar_up_arrow);
5697 bar->dragging = -1;
5698 break;
5699 case GTK_SCROLL_STEP_FORWARD:
5700 part = (bar->horizontal
5701 ? scroll_bar_right_arrow : scroll_bar_down_arrow);
5702 bar->dragging = -1;
5703 break;
5704 case GTK_SCROLL_PAGE_BACKWARD:
5705 part = (bar->horizontal
5706 ? scroll_bar_before_handle : scroll_bar_above_handle);
5707 bar->dragging = -1;
5708 break;
5709 case GTK_SCROLL_PAGE_FORWARD:
5710 part = (bar->horizontal
5711 ? scroll_bar_after_handle : scroll_bar_below_handle);
5712 bar->dragging = -1;
5713 break;
5714 default:
5715 break;
5718 if (part != scroll_bar_nowhere)
5720 window_being_scrolled = bar->window;
5721 x_send_scroll_bar_event (bar->window, part, portion, whole,
5722 bar->horizontal);
5725 return false;
5728 /* Callback for button release. Sets dragging to -1 when dragging is done. */
5730 static gboolean
5731 xg_end_scroll_callback (GtkWidget *widget,
5732 GdkEventButton *event,
5733 gpointer user_data)
5735 struct scroll_bar *bar = user_data;
5736 bar->dragging = -1;
5737 if (WINDOWP (window_being_scrolled))
5739 x_send_scroll_bar_event (window_being_scrolled,
5740 scroll_bar_end_scroll, 0, 0, bar->horizontal);
5741 window_being_scrolled = Qnil;
5744 return false;
5748 #else /* not USE_GTK and not USE_MOTIF */
5750 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
5751 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
5752 scroll bar struct. CALL_DATA is a pointer to a float saying where
5753 the thumb is. */
5755 static void
5756 xaw_jump_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5758 struct scroll_bar *bar = client_data;
5759 float *top_addr = call_data;
5760 float top = *top_addr;
5761 float shown;
5762 int whole, portion, height, width;
5763 enum scroll_bar_part part;
5764 bool horizontal = bar->horizontal;
5766 if (horizontal)
5768 /* Get the size of the thumb, a value between 0 and 1. */
5769 block_input ();
5770 XtVaGetValues (widget, XtNshown, &shown, XtNwidth, &width, NULL);
5771 unblock_input ();
5773 if (shown < 1)
5775 whole = bar->whole - (shown * bar->whole);
5776 portion = min (top * bar->whole, whole);
5778 else
5780 whole = bar->whole;
5781 portion = 0;
5784 part = scroll_bar_horizontal_handle;
5786 else
5788 /* Get the size of the thumb, a value between 0 and 1. */
5789 block_input ();
5790 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
5791 unblock_input ();
5793 whole = 10000000;
5794 portion = shown < 1 ? top * whole : 0;
5796 if (shown < 1 && (eabs (top + shown - 1) < 1.0f / height))
5797 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
5798 the bottom, so we force the scrolling whenever we see that we're
5799 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
5800 we try to ensure that we always stay two pixels away from the
5801 bottom). */
5802 part = scroll_bar_down_arrow;
5803 else
5804 part = scroll_bar_handle;
5807 window_being_scrolled = bar->window;
5808 bar->dragging = portion;
5809 bar->last_seen_part = part;
5810 x_send_scroll_bar_event (bar->window, part, portion, whole, bar->horizontal);
5814 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
5815 i.e. line or page up or down. WIDGET is the Xaw scroll bar
5816 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
5817 the scroll bar. CALL_DATA is an integer specifying the action that
5818 has taken place. Its magnitude is in the range 0..height of the
5819 scroll bar. Negative values mean scroll towards buffer start.
5820 Values < height of scroll bar mean line-wise movement. */
5822 static void
5823 xaw_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5825 struct scroll_bar *bar = client_data;
5826 /* The position really is stored cast to a pointer. */
5827 int position = (intptr_t) call_data;
5828 Dimension height, width;
5829 enum scroll_bar_part part;
5831 if (bar->horizontal)
5833 /* Get the width of the scroll bar. */
5834 block_input ();
5835 XtVaGetValues (widget, XtNwidth, &width, NULL);
5836 unblock_input ();
5838 if (eabs (position) >= width)
5839 part = (position < 0) ? scroll_bar_before_handle : scroll_bar_after_handle;
5841 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
5842 it maps line-movement to call_data = max(5, height/20). */
5843 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, width / 20))
5844 part = (position < 0) ? scroll_bar_left_arrow : scroll_bar_right_arrow;
5845 else
5846 part = scroll_bar_move_ratio;
5848 window_being_scrolled = bar->window;
5849 bar->dragging = -1;
5850 bar->last_seen_part = part;
5851 x_send_scroll_bar_event (bar->window, part, position, width,
5852 bar->horizontal);
5854 else
5857 /* Get the height of the scroll bar. */
5858 block_input ();
5859 XtVaGetValues (widget, XtNheight, &height, NULL);
5860 unblock_input ();
5862 if (eabs (position) >= height)
5863 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
5865 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
5866 it maps line-movement to call_data = max(5, height/20). */
5867 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
5868 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
5869 else
5870 part = scroll_bar_move_ratio;
5872 window_being_scrolled = bar->window;
5873 bar->dragging = -1;
5874 bar->last_seen_part = part;
5875 x_send_scroll_bar_event (bar->window, part, position, height,
5876 bar->horizontal);
5880 #endif /* not USE_GTK and not USE_MOTIF */
5882 #define SCROLL_BAR_NAME "verticalScrollBar"
5883 #define SCROLL_BAR_HORIZONTAL_NAME "horizontalScrollBar"
5885 /* Create the widget for scroll bar BAR on frame F. Record the widget
5886 and X window of the scroll bar in BAR. */
5888 #ifdef USE_GTK
5889 static void
5890 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5892 const char *scroll_bar_name = SCROLL_BAR_NAME;
5894 block_input ();
5895 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
5896 G_CALLBACK (xg_end_scroll_callback),
5897 scroll_bar_name);
5898 unblock_input ();
5901 static void
5902 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5904 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
5906 block_input ();
5907 xg_create_horizontal_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
5908 G_CALLBACK (xg_end_scroll_callback),
5909 scroll_bar_name);
5910 unblock_input ();
5913 #else /* not USE_GTK */
5915 static void
5916 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5918 Window xwindow;
5919 Widget widget;
5920 Arg av[20];
5921 int ac = 0;
5922 const char *scroll_bar_name = SCROLL_BAR_NAME;
5923 unsigned long pixel;
5925 block_input ();
5927 #ifdef USE_MOTIF
5928 /* Set resources. Create the widget. */
5929 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5930 XtSetArg (av[ac], XmNminimum, 0); ++ac;
5931 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
5932 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
5933 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
5934 XtSetArg (av[ac], XmNincrement, 1); ++ac;
5935 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
5937 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5938 if (pixel != -1)
5940 XtSetArg (av[ac], XmNforeground, pixel);
5941 ++ac;
5944 pixel = f->output_data.x->scroll_bar_background_pixel;
5945 if (pixel != -1)
5947 XtSetArg (av[ac], XmNbackground, pixel);
5948 ++ac;
5951 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
5952 (char *) scroll_bar_name, av, ac);
5954 /* Add one callback for everything that can happen. */
5955 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
5956 (XtPointer) bar);
5957 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
5958 (XtPointer) bar);
5959 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
5960 (XtPointer) bar);
5961 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
5962 (XtPointer) bar);
5963 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
5964 (XtPointer) bar);
5965 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
5966 (XtPointer) bar);
5967 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
5968 (XtPointer) bar);
5970 /* Realize the widget. Only after that is the X window created. */
5971 XtRealizeWidget (widget);
5973 /* Set the cursor to an arrow. I didn't find a resource to do that.
5974 And I'm wondering why it hasn't an arrow cursor by default. */
5975 XDefineCursor (XtDisplay (widget), XtWindow (widget),
5976 f->output_data.x->nontext_cursor);
5978 #else /* !USE_MOTIF i.e. use Xaw */
5980 /* Set resources. Create the widget. The background of the
5981 Xaw3d scroll bar widget is a little bit light for my taste.
5982 We don't alter it here to let users change it according
5983 to their taste with `emacs*verticalScrollBar.background: xxx'. */
5984 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5985 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
5986 /* For smoother scrolling with Xaw3d -sm */
5987 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
5989 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5990 if (pixel != -1)
5992 XtSetArg (av[ac], XtNforeground, pixel);
5993 ++ac;
5996 pixel = f->output_data.x->scroll_bar_background_pixel;
5997 if (pixel != -1)
5999 XtSetArg (av[ac], XtNbackground, pixel);
6000 ++ac;
6003 /* Top/bottom shadow colors. */
6005 /* Allocate them, if necessary. */
6006 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
6008 pixel = f->output_data.x->scroll_bar_background_pixel;
6009 if (pixel != -1)
6011 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
6012 FRAME_X_COLORMAP (f),
6013 &pixel, 1.2, 0x8000))
6014 pixel = -1;
6015 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
6018 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
6020 pixel = f->output_data.x->scroll_bar_background_pixel;
6021 if (pixel != -1)
6023 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
6024 FRAME_X_COLORMAP (f),
6025 &pixel, 0.6, 0x4000))
6026 pixel = -1;
6027 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
6031 #ifdef XtNbeNiceToColormap
6032 /* Tell the toolkit about them. */
6033 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
6034 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
6035 /* We tried to allocate a color for the top/bottom shadow, and
6036 failed, so tell Xaw3d to use dithering instead. */
6037 /* But only if we have a small colormap. Xaw3d can allocate nice
6038 colors itself. */
6040 XtSetArg (av[ac], XtNbeNiceToColormap,
6041 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
6042 ++ac;
6044 else
6045 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
6046 be more consistent with other emacs 3d colors, and since Xaw3d is
6047 not good at dealing with allocation failure. */
6049 /* This tells Xaw3d to use real colors instead of dithering for
6050 the shadows. */
6051 XtSetArg (av[ac], XtNbeNiceToColormap, False);
6052 ++ac;
6054 /* Specify the colors. */
6055 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
6056 if (pixel != -1)
6058 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
6059 ++ac;
6061 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
6062 if (pixel != -1)
6064 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
6065 ++ac;
6068 #endif
6070 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
6071 f->output_data.x->edit_widget, av, ac);
6074 char const *initial = "";
6075 char const *val = initial;
6076 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
6077 #ifdef XtNarrowScrollbars
6078 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
6079 #endif
6080 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
6081 if (xaw3d_arrow_scroll || val == initial)
6082 { /* ARROW_SCROLL */
6083 xaw3d_arrow_scroll = True;
6084 /* Isn't that just a personal preference ? --Stef */
6085 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
6089 /* Define callbacks. */
6090 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
6091 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
6092 (XtPointer) bar);
6094 /* Realize the widget. Only after that is the X window created. */
6095 XtRealizeWidget (widget);
6097 #endif /* !USE_MOTIF */
6099 /* Install an action hook that lets us detect when the user
6100 finishes interacting with a scroll bar. */
6101 if (action_hook_id == 0)
6102 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
6104 /* Remember X window and widget in the scroll bar vector. */
6105 SET_SCROLL_BAR_X_WIDGET (bar, widget);
6106 xwindow = XtWindow (widget);
6107 bar->x_window = xwindow;
6108 bar->whole = 1;
6109 bar->horizontal = false;
6111 unblock_input ();
6114 static void
6115 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
6117 Window xwindow;
6118 Widget widget;
6119 Arg av[20];
6120 int ac = 0;
6121 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
6122 unsigned long pixel;
6124 block_input ();
6126 #ifdef USE_MOTIF
6127 /* Set resources. Create the widget. */
6128 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
6129 XtSetArg (av[ac], XmNminimum, 0); ++ac;
6130 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
6131 XtSetArg (av[ac], XmNorientation, XmHORIZONTAL); ++ac;
6132 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_RIGHT), ++ac;
6133 XtSetArg (av[ac], XmNincrement, 1); ++ac;
6134 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
6136 pixel = f->output_data.x->scroll_bar_foreground_pixel;
6137 if (pixel != -1)
6139 XtSetArg (av[ac], XmNforeground, pixel);
6140 ++ac;
6143 pixel = f->output_data.x->scroll_bar_background_pixel;
6144 if (pixel != -1)
6146 XtSetArg (av[ac], XmNbackground, pixel);
6147 ++ac;
6150 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
6151 (char *) scroll_bar_name, av, ac);
6153 /* Add one callback for everything that can happen. */
6154 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
6155 (XtPointer) bar);
6156 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
6157 (XtPointer) bar);
6158 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
6159 (XtPointer) bar);
6160 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
6161 (XtPointer) bar);
6162 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
6163 (XtPointer) bar);
6164 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
6165 (XtPointer) bar);
6166 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
6167 (XtPointer) bar);
6169 /* Realize the widget. Only after that is the X window created. */
6170 XtRealizeWidget (widget);
6172 /* Set the cursor to an arrow. I didn't find a resource to do that.
6173 And I'm wondering why it hasn't an arrow cursor by default. */
6174 XDefineCursor (XtDisplay (widget), XtWindow (widget),
6175 f->output_data.x->nontext_cursor);
6177 #else /* !USE_MOTIF i.e. use Xaw */
6179 /* Set resources. Create the widget. The background of the
6180 Xaw3d scroll bar widget is a little bit light for my taste.
6181 We don't alter it here to let users change it according
6182 to their taste with `emacs*verticalScrollBar.background: xxx'. */
6183 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
6184 XtSetArg (av[ac], XtNorientation, XtorientHorizontal); ++ac;
6185 /* For smoother scrolling with Xaw3d -sm */
6186 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
6188 pixel = f->output_data.x->scroll_bar_foreground_pixel;
6189 if (pixel != -1)
6191 XtSetArg (av[ac], XtNforeground, pixel);
6192 ++ac;
6195 pixel = f->output_data.x->scroll_bar_background_pixel;
6196 if (pixel != -1)
6198 XtSetArg (av[ac], XtNbackground, pixel);
6199 ++ac;
6202 /* Top/bottom shadow colors. */
6204 /* Allocate them, if necessary. */
6205 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
6207 pixel = f->output_data.x->scroll_bar_background_pixel;
6208 if (pixel != -1)
6210 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
6211 FRAME_X_COLORMAP (f),
6212 &pixel, 1.2, 0x8000))
6213 pixel = -1;
6214 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
6217 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
6219 pixel = f->output_data.x->scroll_bar_background_pixel;
6220 if (pixel != -1)
6222 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
6223 FRAME_X_COLORMAP (f),
6224 &pixel, 0.6, 0x4000))
6225 pixel = -1;
6226 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
6230 #ifdef XtNbeNiceToColormap
6231 /* Tell the toolkit about them. */
6232 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
6233 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
6234 /* We tried to allocate a color for the top/bottom shadow, and
6235 failed, so tell Xaw3d to use dithering instead. */
6236 /* But only if we have a small colormap. Xaw3d can allocate nice
6237 colors itself. */
6239 XtSetArg (av[ac], XtNbeNiceToColormap,
6240 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
6241 ++ac;
6243 else
6244 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
6245 be more consistent with other emacs 3d colors, and since Xaw3d is
6246 not good at dealing with allocation failure. */
6248 /* This tells Xaw3d to use real colors instead of dithering for
6249 the shadows. */
6250 XtSetArg (av[ac], XtNbeNiceToColormap, False);
6251 ++ac;
6253 /* Specify the colors. */
6254 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
6255 if (pixel != -1)
6257 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
6258 ++ac;
6260 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
6261 if (pixel != -1)
6263 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
6264 ++ac;
6267 #endif
6269 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
6270 f->output_data.x->edit_widget, av, ac);
6273 char const *initial = "";
6274 char const *val = initial;
6275 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
6276 #ifdef XtNarrowScrollbars
6277 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
6278 #endif
6279 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
6280 if (xaw3d_arrow_scroll || val == initial)
6281 { /* ARROW_SCROLL */
6282 xaw3d_arrow_scroll = True;
6283 /* Isn't that just a personal preference ? --Stef */
6284 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
6288 /* Define callbacks. */
6289 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
6290 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
6291 (XtPointer) bar);
6293 /* Realize the widget. Only after that is the X window created. */
6294 XtRealizeWidget (widget);
6296 #endif /* !USE_MOTIF */
6298 /* Install an action hook that lets us detect when the user
6299 finishes interacting with a scroll bar. */
6300 if (horizontal_action_hook_id == 0)
6301 horizontal_action_hook_id
6302 = XtAppAddActionHook (Xt_app_con, xt_horizontal_action_hook, 0);
6304 /* Remember X window and widget in the scroll bar vector. */
6305 SET_SCROLL_BAR_X_WIDGET (bar, widget);
6306 xwindow = XtWindow (widget);
6307 bar->x_window = xwindow;
6308 bar->whole = 1;
6309 bar->horizontal = true;
6311 unblock_input ();
6313 #endif /* not USE_GTK */
6316 /* Set the thumb size and position of scroll bar BAR. We are currently
6317 displaying PORTION out of a whole WHOLE, and our position POSITION. */
6319 #ifdef USE_GTK
6320 static void
6321 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
6323 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
6326 static void
6327 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
6329 xg_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
6332 #else /* not USE_GTK */
6333 static void
6334 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
6335 int whole)
6337 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6338 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6339 float top, shown;
6341 block_input ();
6343 #ifdef USE_MOTIF
6345 if (scroll_bar_adjust_thumb_portion_p)
6347 /* We use an estimate of 30 chars per line rather than the real
6348 `portion' value. This has the disadvantage that the thumb size
6349 is not very representative, but it makes our life a lot easier.
6350 Otherwise, we have to constantly adjust the thumb size, which
6351 we can't always do quickly enough: while dragging, the size of
6352 the thumb might prevent the user from dragging the thumb all the
6353 way to the end. but Motif and some versions of Xaw3d don't allow
6354 updating the thumb size while dragging. Also, even if we can update
6355 its size, the update will often happen too late.
6356 If you don't believe it, check out revision 1.650 of xterm.c to see
6357 what hoops we were going through and the still poor behavior we got. */
6358 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
6359 /* When the thumb is at the bottom, position == whole.
6360 So we need to increase `whole' to make space for the thumb. */
6361 whole += portion;
6364 if (whole <= 0)
6365 top = 0, shown = 1;
6366 else
6368 top = (float) position / whole;
6369 shown = (float) portion / whole;
6372 if (bar->dragging == -1)
6374 int size, value;
6376 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
6377 is the scroll bar's maximum and MIN is the scroll bar's minimum
6378 value. */
6379 size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
6381 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
6382 value = top * XM_SB_MAX;
6383 value = min (value, XM_SB_MAX - size);
6385 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6387 #else /* !USE_MOTIF i.e. use Xaw */
6389 if (whole == 0)
6390 top = 0, shown = 1;
6391 else
6393 top = (float) position / whole;
6394 shown = (float) portion / whole;
6398 float old_top, old_shown;
6399 Dimension height;
6400 XtVaGetValues (widget,
6401 XtNtopOfThumb, &old_top,
6402 XtNshown, &old_shown,
6403 XtNheight, &height,
6404 NULL);
6406 /* Massage the top+shown values. */
6407 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
6408 top = max (0, min (1, top));
6409 else
6410 top = old_top;
6411 #if ! defined (HAVE_XAW3D)
6412 /* With Xaw, 'top' values too closer to 1.0 may
6413 cause the thumb to disappear. Fix that. */
6414 top = min (top, 0.99f);
6415 #endif
6416 /* Keep two pixels available for moving the thumb down. */
6417 shown = max (0, min (1 - top - (2.0f / height), shown));
6418 #if ! defined (HAVE_XAW3D)
6419 /* Likewise with too small 'shown'. */
6420 shown = max (shown, 0.01f);
6421 #endif
6423 /* If the call to XawScrollbarSetThumb below doesn't seem to
6424 work, check that 'NARROWPROTO' is defined in src/config.h.
6425 If this is not so, most likely you need to fix configure. */
6426 if (top != old_top || shown != old_shown)
6428 if (bar->dragging == -1)
6429 XawScrollbarSetThumb (widget, top, shown);
6430 else
6432 /* Try to make the scrolling a tad smoother. */
6433 if (!xaw3d_pick_top)
6434 shown = min (shown, old_shown);
6436 XawScrollbarSetThumb (widget, top, shown);
6440 #endif /* !USE_MOTIF */
6442 unblock_input ();
6445 static void
6446 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
6447 int whole)
6449 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6450 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6451 float top, shown;
6453 block_input ();
6455 #ifdef USE_MOTIF
6456 bar->whole = whole;
6457 shown = (float) portion / whole;
6458 top = (float) position / (whole - portion);
6460 int size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
6461 int value = clip_to_bounds (0, top * (XM_SB_MAX - size), XM_SB_MAX - size);
6463 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6465 #else /* !USE_MOTIF i.e. use Xaw */
6466 bar->whole = whole;
6467 if (whole == 0)
6468 top = 0, shown = 1;
6469 else
6471 top = (float) position / whole;
6472 shown = (float) portion / whole;
6476 float old_top, old_shown;
6477 Dimension height;
6478 XtVaGetValues (widget,
6479 XtNtopOfThumb, &old_top,
6480 XtNshown, &old_shown,
6481 XtNheight, &height,
6482 NULL);
6484 #if false
6485 /* Massage the top+shown values. */
6486 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
6487 top = max (0, min (1, top));
6488 else
6489 top = old_top;
6490 #if ! defined (HAVE_XAW3D)
6491 /* With Xaw, 'top' values too closer to 1.0 may
6492 cause the thumb to disappear. Fix that. */
6493 top = min (top, 0.99f);
6494 #endif
6495 /* Keep two pixels available for moving the thumb down. */
6496 shown = max (0, min (1 - top - (2.0f / height), shown));
6497 #if ! defined (HAVE_XAW3D)
6498 /* Likewise with too small 'shown'. */
6499 shown = max (shown, 0.01f);
6500 #endif
6501 #endif
6503 /* If the call to XawScrollbarSetThumb below doesn't seem to
6504 work, check that 'NARROWPROTO' is defined in src/config.h.
6505 If this is not so, most likely you need to fix configure. */
6506 XawScrollbarSetThumb (widget, top, shown);
6507 #if false
6508 if (top != old_top || shown != old_shown)
6510 if (bar->dragging == -1)
6511 XawScrollbarSetThumb (widget, top, shown);
6512 else
6514 /* Try to make the scrolling a tad smoother. */
6515 if (!xaw3d_pick_top)
6516 shown = min (shown, old_shown);
6518 XawScrollbarSetThumb (widget, top, shown);
6521 #endif
6523 #endif /* !USE_MOTIF */
6525 unblock_input ();
6527 #endif /* not USE_GTK */
6529 #endif /* USE_TOOLKIT_SCROLL_BARS */
6533 /************************************************************************
6534 Scroll bars, general
6535 ************************************************************************/
6537 /* Create a scroll bar and return the scroll bar vector for it. W is
6538 the Emacs window on which to create the scroll bar. TOP, LEFT,
6539 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
6540 scroll bar. */
6542 static struct scroll_bar *
6543 x_scroll_bar_create (struct window *w, int top, int left,
6544 int width, int height, bool horizontal)
6546 struct frame *f = XFRAME (w->frame);
6547 struct scroll_bar *bar
6548 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
6549 Lisp_Object barobj;
6551 block_input ();
6553 #ifdef USE_TOOLKIT_SCROLL_BARS
6554 if (horizontal)
6555 x_create_horizontal_toolkit_scroll_bar (f, bar);
6556 else
6557 x_create_toolkit_scroll_bar (f, bar);
6558 #else /* not USE_TOOLKIT_SCROLL_BARS */
6560 XSetWindowAttributes a;
6561 unsigned long mask;
6562 Window window;
6564 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
6565 if (a.background_pixel == -1)
6566 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
6568 a.event_mask = (ButtonPressMask | ButtonReleaseMask
6569 | ButtonMotionMask | PointerMotionHintMask
6570 | ExposureMask);
6571 a.cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
6573 mask = (CWBackPixel | CWEventMask | CWCursor);
6575 /* Clear the area of W that will serve as a scroll bar. This is
6576 for the case that a window has been split horizontally. In
6577 this case, no clear_frame is generated to reduce flickering. */
6578 if (width > 0 && window_box_height (w) > 0)
6579 x_clear_area (f, left, top, width, window_box_height (w));
6581 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6582 /* Position and size of scroll bar. */
6583 left, top, width, height,
6584 /* Border width, depth, class, and visual. */
6586 CopyFromParent,
6587 CopyFromParent,
6588 CopyFromParent,
6589 /* Attributes. */
6590 mask, &a);
6591 bar->x_window = window;
6593 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6595 XSETWINDOW (bar->window, w);
6596 bar->top = top;
6597 bar->left = left;
6598 bar->width = width;
6599 bar->height = height;
6600 bar->start = 0;
6601 bar->end = 0;
6602 bar->dragging = -1;
6603 bar->horizontal = horizontal;
6604 #if defined (USE_TOOLKIT_SCROLL_BARS) && defined (USE_LUCID)
6605 bar->last_seen_part = scroll_bar_nowhere;
6606 #endif
6608 /* Add bar to its frame's list of scroll bars. */
6609 bar->next = FRAME_SCROLL_BARS (f);
6610 bar->prev = Qnil;
6611 XSETVECTOR (barobj, bar);
6612 fset_scroll_bars (f, barobj);
6613 if (!NILP (bar->next))
6614 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6616 /* Map the window/widget. */
6617 #ifdef USE_TOOLKIT_SCROLL_BARS
6619 #ifdef USE_GTK
6620 if (horizontal)
6621 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top,
6622 left, width, max (height, 1));
6623 else
6624 xg_update_scrollbar_pos (f, bar->x_window, top,
6625 left, width, max (height, 1));
6626 #else /* not USE_GTK */
6627 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6628 XtConfigureWidget (scroll_bar, left, top, width, max (height, 1), 0);
6629 XtMapWidget (scroll_bar);
6630 /* Don't obscure any child frames. */
6631 XLowerWindow (FRAME_X_DISPLAY (f), bar->x_window);
6632 #endif /* not USE_GTK */
6634 #else /* not USE_TOOLKIT_SCROLL_BARS */
6635 XMapWindow (FRAME_X_DISPLAY (f), bar->x_window);
6636 /* Don't obscure any child frames. */
6637 XLowerWindow (FRAME_X_DISPLAY (f), bar->x_window);
6638 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6640 unblock_input ();
6641 return bar;
6645 #ifndef USE_TOOLKIT_SCROLL_BARS
6647 /* Draw BAR's handle in the proper position.
6649 If the handle is already drawn from START to END, don't bother
6650 redrawing it, unless REBUILD; in that case, always
6651 redraw it. (REBUILD is handy for drawing the handle after expose
6652 events.)
6654 Normally, we want to constrain the start and end of the handle to
6655 fit inside its rectangle, but if the user is dragging the scroll
6656 bar handle, we want to let them drag it down all the way, so that
6657 the bar's top is as far down as it goes; otherwise, there's no way
6658 to move to the very end of the buffer. */
6660 static void
6661 x_scroll_bar_set_handle (struct scroll_bar *bar, int start, int end,
6662 bool rebuild)
6664 bool dragging = bar->dragging != -1;
6665 Window w = bar->x_window;
6666 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6667 GC gc = f->output_data.x->normal_gc;
6669 /* If the display is already accurate, do nothing. */
6670 if (! rebuild
6671 && start == bar->start
6672 && end == bar->end)
6673 return;
6675 block_input ();
6678 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
6679 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
6680 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
6682 /* Make sure the values are reasonable, and try to preserve
6683 the distance between start and end. */
6685 int length = end - start;
6687 if (start < 0)
6688 start = 0;
6689 else if (start > top_range)
6690 start = top_range;
6691 end = start + length;
6693 if (end < start)
6694 end = start;
6695 else if (end > top_range && ! dragging)
6696 end = top_range;
6699 /* Store the adjusted setting in the scroll bar. */
6700 bar->start = start;
6701 bar->end = end;
6703 /* Clip the end position, just for display. */
6704 if (end > top_range)
6705 end = top_range;
6707 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
6708 below top positions, to make sure the handle is always at least
6709 that many pixels tall. */
6710 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
6712 /* Draw the empty space above the handle. Note that we can't clear
6713 zero-height areas; that means "clear to end of window." */
6714 if ((inside_width > 0) && (start > 0))
6715 x_clear_area1 (FRAME_X_DISPLAY (f), w,
6716 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6717 VERTICAL_SCROLL_BAR_TOP_BORDER,
6718 inside_width, start, False);
6720 /* Change to proper foreground color if one is specified. */
6721 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6722 XSetForeground (FRAME_X_DISPLAY (f), gc,
6723 f->output_data.x->scroll_bar_foreground_pixel);
6725 /* Draw the handle itself. */
6726 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
6727 /* x, y, width, height */
6728 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6729 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
6730 inside_width, end - start);
6732 /* Restore the foreground color of the GC if we changed it above. */
6733 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6734 XSetForeground (FRAME_X_DISPLAY (f), gc,
6735 FRAME_FOREGROUND_PIXEL (f));
6737 /* Draw the empty space below the handle. Note that we can't
6738 clear zero-height areas; that means "clear to end of window." */
6739 if ((inside_width > 0) && (end < inside_height))
6740 x_clear_area1 (FRAME_X_DISPLAY (f), w,
6741 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6742 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
6743 inside_width, inside_height - end, False);
6746 unblock_input ();
6749 #endif /* !USE_TOOLKIT_SCROLL_BARS */
6751 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
6752 nil. */
6754 static void
6755 x_scroll_bar_remove (struct scroll_bar *bar)
6757 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6758 block_input ();
6760 #ifdef USE_TOOLKIT_SCROLL_BARS
6761 #ifdef USE_GTK
6762 xg_remove_scroll_bar (f, bar->x_window);
6763 #else /* not USE_GTK */
6764 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
6765 #endif /* not USE_GTK */
6766 #else
6767 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
6768 #endif
6770 /* Dissociate this scroll bar from its window. */
6771 if (bar->horizontal)
6772 wset_horizontal_scroll_bar (XWINDOW (bar->window), Qnil);
6773 else
6774 wset_vertical_scroll_bar (XWINDOW (bar->window), Qnil);
6776 unblock_input ();
6780 /* Set the handle of the vertical scroll bar for WINDOW to indicate
6781 that we are displaying PORTION characters out of a total of WHOLE
6782 characters, starting at POSITION. If WINDOW has no scroll bar,
6783 create one. */
6785 static void
6786 XTset_vertical_scroll_bar (struct window *w, int portion, int whole, int position)
6788 struct frame *f = XFRAME (w->frame);
6789 Lisp_Object barobj;
6790 struct scroll_bar *bar;
6791 int top, height, left, width;
6792 int window_y, window_height;
6794 /* Get window dimensions. */
6795 window_box (w, ANY_AREA, 0, &window_y, 0, &window_height);
6796 top = window_y;
6797 height = window_height;
6798 left = WINDOW_SCROLL_BAR_AREA_X (w);
6799 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
6801 /* Does the scroll bar exist yet? */
6802 if (NILP (w->vertical_scroll_bar))
6804 if (width > 0 && height > 0)
6806 block_input ();
6807 x_clear_area (f, left, top, width, height);
6808 unblock_input ();
6811 bar = x_scroll_bar_create (w, top, left, width, max (height, 1), false);
6813 else
6815 /* It may just need to be moved and resized. */
6816 unsigned int mask = 0;
6818 bar = XSCROLL_BAR (w->vertical_scroll_bar);
6820 block_input ();
6822 if (left != bar->left)
6823 mask |= CWX;
6824 if (top != bar->top)
6825 mask |= CWY;
6826 if (width != bar->width)
6827 mask |= CWWidth;
6828 if (height != bar->height)
6829 mask |= CWHeight;
6831 #ifdef USE_TOOLKIT_SCROLL_BARS
6833 /* Move/size the scroll bar widget. */
6834 if (mask)
6836 /* Since toolkit scroll bars are smaller than the space reserved
6837 for them on the frame, we have to clear "under" them. */
6838 if (width > 0 && height > 0)
6839 x_clear_area (f, left, top, width, height);
6840 #ifdef USE_GTK
6841 xg_update_scrollbar_pos (f, bar->x_window, top,
6842 left, width, max (height, 1));
6843 #else /* not USE_GTK */
6844 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
6845 left, top, width, max (height, 1), 0);
6846 #endif /* not USE_GTK */
6848 #else /* not USE_TOOLKIT_SCROLL_BARS */
6850 /* Move/size the scroll bar window. */
6851 if (mask)
6853 XWindowChanges wc;
6855 wc.x = left;
6856 wc.y = top;
6857 wc.width = width;
6858 wc.height = height;
6859 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
6860 mask, &wc);
6863 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6865 /* Remember new settings. */
6866 bar->left = left;
6867 bar->top = top;
6868 bar->width = width;
6869 bar->height = height;
6871 unblock_input ();
6874 #ifdef USE_TOOLKIT_SCROLL_BARS
6875 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
6876 #else /* not USE_TOOLKIT_SCROLL_BARS */
6877 /* Set the scroll bar's current state, unless we're currently being
6878 dragged. */
6879 if (bar->dragging == -1)
6881 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
6883 if (whole == 0)
6884 x_scroll_bar_set_handle (bar, 0, top_range, false);
6885 else
6887 int start = ((double) position * top_range) / whole;
6888 int end = ((double) (position + portion) * top_range) / whole;
6889 x_scroll_bar_set_handle (bar, start, end, false);
6892 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6894 XSETVECTOR (barobj, bar);
6895 wset_vertical_scroll_bar (w, barobj);
6899 static void
6900 XTset_horizontal_scroll_bar (struct window *w, int portion, int whole, int position)
6902 struct frame *f = XFRAME (w->frame);
6903 Lisp_Object barobj;
6904 struct scroll_bar *bar;
6905 int top, height, left, width;
6906 int window_x, window_width;
6907 int pixel_width = WINDOW_PIXEL_WIDTH (w);
6909 /* Get window dimensions. */
6910 window_box (w, ANY_AREA, &window_x, 0, &window_width, 0);
6911 left = window_x;
6912 width = window_width;
6913 top = WINDOW_SCROLL_BAR_AREA_Y (w);
6914 height = WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
6916 /* Does the scroll bar exist yet? */
6917 if (NILP (w->horizontal_scroll_bar))
6919 if (width > 0 && height > 0)
6921 block_input ();
6923 /* Clear also part between window_width and
6924 WINDOW_PIXEL_WIDTH. */
6925 x_clear_area (f, left, top, pixel_width, height);
6926 unblock_input ();
6929 bar = x_scroll_bar_create (w, top, left, width, height, true);
6931 else
6933 /* It may just need to be moved and resized. */
6934 unsigned int mask = 0;
6936 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
6938 block_input ();
6940 if (left != bar->left)
6941 mask |= CWX;
6942 if (top != bar->top)
6943 mask |= CWY;
6944 if (width != bar->width)
6945 mask |= CWWidth;
6946 if (height != bar->height)
6947 mask |= CWHeight;
6949 #ifdef USE_TOOLKIT_SCROLL_BARS
6950 /* Move/size the scroll bar widget. */
6951 if (mask)
6953 /* Since toolkit scroll bars are smaller than the space reserved
6954 for them on the frame, we have to clear "under" them. */
6955 if (width > 0 && height > 0)
6956 x_clear_area (f,
6957 WINDOW_LEFT_EDGE_X (w), top,
6958 pixel_width - WINDOW_RIGHT_DIVIDER_WIDTH (w), height);
6959 #ifdef USE_GTK
6960 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top, left,
6961 width, height);
6962 #else /* not USE_GTK */
6963 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
6964 left, top, width, height, 0);
6965 #endif /* not USE_GTK */
6967 #else /* not USE_TOOLKIT_SCROLL_BARS */
6969 /* Clear areas not covered by the scroll bar because it's not as
6970 wide as the area reserved for it. This makes sure a
6971 previous mode line display is cleared after C-x 2 C-x 1, for
6972 example. */
6974 int area_height = WINDOW_CONFIG_SCROLL_BAR_HEIGHT (w);
6975 int rest = area_height - height;
6976 if (rest > 0 && width > 0)
6977 x_clear_area (f, left, top, width, rest);
6980 /* Move/size the scroll bar window. */
6981 if (mask)
6983 XWindowChanges wc;
6985 wc.x = left;
6986 wc.y = top;
6987 wc.width = width;
6988 wc.height = height;
6989 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
6990 mask, &wc);
6993 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6995 /* Remember new settings. */
6996 bar->left = left;
6997 bar->top = top;
6998 bar->width = width;
6999 bar->height = height;
7001 unblock_input ();
7004 #ifdef USE_TOOLKIT_SCROLL_BARS
7005 x_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
7006 #else /* not USE_TOOLKIT_SCROLL_BARS */
7007 /* Set the scroll bar's current state, unless we're currently being
7008 dragged. */
7009 if (bar->dragging == -1)
7011 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, width);
7013 if (whole == 0)
7014 x_scroll_bar_set_handle (bar, 0, left_range, false);
7015 else
7017 int start = ((double) position * left_range) / whole;
7018 int end = ((double) (position + portion) * left_range) / whole;
7019 x_scroll_bar_set_handle (bar, start, end, false);
7022 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7024 XSETVECTOR (barobj, bar);
7025 wset_horizontal_scroll_bar (w, barobj);
7029 /* The following three hooks are used when we're doing a thorough
7030 redisplay of the frame. We don't explicitly know which scroll bars
7031 are going to be deleted, because keeping track of when windows go
7032 away is a real pain - "Can you say set-window-configuration, boys
7033 and girls?" Instead, we just assert at the beginning of redisplay
7034 that *all* scroll bars are to be removed, and then save a scroll bar
7035 from the fiery pit when we actually redisplay its window. */
7037 /* Arrange for all scroll bars on FRAME to be removed at the next call
7038 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
7039 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
7041 static void
7042 XTcondemn_scroll_bars (struct frame *frame)
7044 if (!NILP (FRAME_SCROLL_BARS (frame)))
7046 if (!NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
7048 /* Prepend scrollbars to already condemned ones. */
7049 Lisp_Object last = FRAME_SCROLL_BARS (frame);
7051 while (!NILP (XSCROLL_BAR (last)->next))
7052 last = XSCROLL_BAR (last)->next;
7054 XSCROLL_BAR (last)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
7055 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = last;
7058 fset_condemned_scroll_bars (frame, FRAME_SCROLL_BARS (frame));
7059 fset_scroll_bars (frame, Qnil);
7064 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
7065 Note that WINDOW isn't necessarily condemned at all. */
7067 static void
7068 XTredeem_scroll_bar (struct window *w)
7070 struct scroll_bar *bar;
7071 Lisp_Object barobj;
7072 struct frame *f;
7074 /* We can't redeem this window's scroll bar if it doesn't have one. */
7075 if (NILP (w->vertical_scroll_bar) && NILP (w->horizontal_scroll_bar))
7076 emacs_abort ();
7078 if (!NILP (w->vertical_scroll_bar) && WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
7080 bar = XSCROLL_BAR (w->vertical_scroll_bar);
7081 /* Unlink it from the condemned list. */
7082 f = XFRAME (WINDOW_FRAME (w));
7083 if (NILP (bar->prev))
7085 /* If the prev pointer is nil, it must be the first in one of
7086 the lists. */
7087 if (EQ (FRAME_SCROLL_BARS (f), w->vertical_scroll_bar))
7088 /* It's not condemned. Everything's fine. */
7089 goto horizontal;
7090 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
7091 w->vertical_scroll_bar))
7092 fset_condemned_scroll_bars (f, bar->next);
7093 else
7094 /* If its prev pointer is nil, it must be at the front of
7095 one or the other! */
7096 emacs_abort ();
7098 else
7099 XSCROLL_BAR (bar->prev)->next = bar->next;
7101 if (! NILP (bar->next))
7102 XSCROLL_BAR (bar->next)->prev = bar->prev;
7104 bar->next = FRAME_SCROLL_BARS (f);
7105 bar->prev = Qnil;
7106 XSETVECTOR (barobj, bar);
7107 fset_scroll_bars (f, barobj);
7108 if (! NILP (bar->next))
7109 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
7112 horizontal:
7113 if (!NILP (w->horizontal_scroll_bar) && WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
7115 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
7116 /* Unlink it from the condemned list. */
7117 f = XFRAME (WINDOW_FRAME (w));
7118 if (NILP (bar->prev))
7120 /* If the prev pointer is nil, it must be the first in one of
7121 the lists. */
7122 if (EQ (FRAME_SCROLL_BARS (f), w->horizontal_scroll_bar))
7123 /* It's not condemned. Everything's fine. */
7124 return;
7125 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
7126 w->horizontal_scroll_bar))
7127 fset_condemned_scroll_bars (f, bar->next);
7128 else
7129 /* If its prev pointer is nil, it must be at the front of
7130 one or the other! */
7131 emacs_abort ();
7133 else
7134 XSCROLL_BAR (bar->prev)->next = bar->next;
7136 if (! NILP (bar->next))
7137 XSCROLL_BAR (bar->next)->prev = bar->prev;
7139 bar->next = FRAME_SCROLL_BARS (f);
7140 bar->prev = Qnil;
7141 XSETVECTOR (barobj, bar);
7142 fset_scroll_bars (f, barobj);
7143 if (! NILP (bar->next))
7144 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
7148 /* Remove all scroll bars on FRAME that haven't been saved since the
7149 last call to `*condemn_scroll_bars_hook'. */
7151 static void
7152 XTjudge_scroll_bars (struct frame *f)
7154 Lisp_Object bar, next;
7156 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
7158 /* Clear out the condemned list now so we won't try to process any
7159 more events on the hapless scroll bars. */
7160 fset_condemned_scroll_bars (f, Qnil);
7162 for (; ! NILP (bar); bar = next)
7164 struct scroll_bar *b = XSCROLL_BAR (bar);
7166 x_scroll_bar_remove (b);
7168 next = b->next;
7169 b->next = b->prev = Qnil;
7172 /* Now there should be no references to the condemned scroll bars,
7173 and they should get garbage-collected. */
7177 #ifndef USE_TOOLKIT_SCROLL_BARS
7178 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
7179 is a no-op when using toolkit scroll bars.
7181 This may be called from a signal handler, so we have to ignore GC
7182 mark bits. */
7184 static void
7185 x_scroll_bar_expose (struct scroll_bar *bar, const XEvent *event)
7187 Window w = bar->x_window;
7188 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7189 GC gc = f->output_data.x->normal_gc;
7191 block_input ();
7193 x_scroll_bar_set_handle (bar, bar->start, bar->end, true);
7195 /* Switch to scroll bar foreground color. */
7196 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
7197 XSetForeground (FRAME_X_DISPLAY (f), gc,
7198 f->output_data.x->scroll_bar_foreground_pixel);
7200 /* Draw a one-pixel border just inside the edges of the scroll bar. */
7201 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
7202 /* x, y, width, height */
7203 0, 0, bar->width - 1, bar->height - 1);
7205 /* Restore the foreground color of the GC if we changed it above. */
7206 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
7207 XSetForeground (FRAME_X_DISPLAY (f), gc,
7208 FRAME_FOREGROUND_PIXEL (f));
7210 unblock_input ();
7213 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7215 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
7216 is set to something other than NO_EVENT, it is enqueued.
7218 This may be called from a signal handler, so we have to ignore GC
7219 mark bits. */
7222 static void
7223 x_scroll_bar_handle_click (struct scroll_bar *bar,
7224 const XEvent *event,
7225 struct input_event *emacs_event)
7227 if (! WINDOWP (bar->window))
7228 emacs_abort ();
7230 emacs_event->kind = (bar->horizontal
7231 ? HORIZONTAL_SCROLL_BAR_CLICK_EVENT
7232 : SCROLL_BAR_CLICK_EVENT);
7233 emacs_event->code = event->xbutton.button - Button1;
7234 emacs_event->modifiers
7235 = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO
7236 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
7237 event->xbutton.state)
7238 | (event->type == ButtonRelease
7239 ? up_modifier
7240 : down_modifier));
7241 emacs_event->frame_or_window = bar->window;
7242 emacs_event->arg = Qnil;
7243 emacs_event->timestamp = event->xbutton.time;
7244 if (bar->horizontal)
7246 int left_range
7247 = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
7248 int x = event->xbutton.x - HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
7250 if (x < 0) x = 0;
7251 if (x > left_range) x = left_range;
7253 if (x < bar->start)
7254 emacs_event->part = scroll_bar_before_handle;
7255 else if (x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
7256 emacs_event->part = scroll_bar_horizontal_handle;
7257 else
7258 emacs_event->part = scroll_bar_after_handle;
7260 #ifndef USE_TOOLKIT_SCROLL_BARS
7261 /* If the user has released the handle, set it to its final position. */
7262 if (event->type == ButtonRelease && bar->dragging != -1)
7264 int new_start = - bar->dragging;
7265 int new_end = new_start + bar->end - bar->start;
7267 x_scroll_bar_set_handle (bar, new_start, new_end, false);
7268 bar->dragging = -1;
7270 #endif
7272 XSETINT (emacs_event->x, left_range);
7273 XSETINT (emacs_event->y, x);
7275 else
7277 int top_range
7278 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
7279 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
7281 if (y < 0) y = 0;
7282 if (y > top_range) y = top_range;
7284 if (y < bar->start)
7285 emacs_event->part = scroll_bar_above_handle;
7286 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
7287 emacs_event->part = scroll_bar_handle;
7288 else
7289 emacs_event->part = scroll_bar_below_handle;
7291 #ifndef USE_TOOLKIT_SCROLL_BARS
7292 /* If the user has released the handle, set it to its final position. */
7293 if (event->type == ButtonRelease && bar->dragging != -1)
7295 int new_start = y - bar->dragging;
7296 int new_end = new_start + bar->end - bar->start;
7298 x_scroll_bar_set_handle (bar, new_start, new_end, false);
7299 bar->dragging = -1;
7301 #endif
7303 XSETINT (emacs_event->x, y);
7304 XSETINT (emacs_event->y, top_range);
7308 #ifndef USE_TOOLKIT_SCROLL_BARS
7310 /* Handle some mouse motion while someone is dragging the scroll bar.
7312 This may be called from a signal handler, so we have to ignore GC
7313 mark bits. */
7315 static void
7316 x_scroll_bar_note_movement (struct scroll_bar *bar,
7317 const XMotionEvent *event)
7319 struct frame *f = XFRAME (XWINDOW (bar->window)->frame);
7320 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
7322 dpyinfo->last_mouse_movement_time = event->time;
7323 dpyinfo->last_mouse_scroll_bar = bar;
7324 f->mouse_moved = true;
7326 /* If we're dragging the bar, display it. */
7327 if (bar->dragging != -1)
7329 /* Where should the handle be now? */
7330 int new_start = event->y - bar->dragging;
7332 if (new_start != bar->start)
7334 int new_end = new_start + bar->end - bar->start;
7336 x_scroll_bar_set_handle (bar, new_start, new_end, false);
7341 #endif /* !USE_TOOLKIT_SCROLL_BARS */
7343 /* Return information to the user about the current position of the mouse
7344 on the scroll bar. */
7346 static void
7347 x_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
7348 enum scroll_bar_part *part, Lisp_Object *x,
7349 Lisp_Object *y, Time *timestamp)
7351 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
7352 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
7353 Window w = bar->x_window;
7354 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7355 int win_x, win_y;
7356 Window dummy_window;
7357 int dummy_coord;
7358 unsigned int dummy_mask;
7360 block_input ();
7362 /* Get the mouse's position relative to the scroll bar window, and
7363 report that. */
7364 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
7366 /* Root, child, root x and root y. */
7367 &dummy_window, &dummy_window,
7368 &dummy_coord, &dummy_coord,
7370 /* Position relative to scroll bar. */
7371 &win_x, &win_y,
7373 /* Mouse buttons and modifier keys. */
7374 &dummy_mask))
7376 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
7378 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
7380 if (bar->dragging != -1)
7381 win_y -= bar->dragging;
7383 if (win_y < 0)
7384 win_y = 0;
7385 if (win_y > top_range)
7386 win_y = top_range;
7388 *fp = f;
7389 *bar_window = bar->window;
7391 if (bar->dragging != -1)
7392 *part = scroll_bar_handle;
7393 else if (win_y < bar->start)
7394 *part = scroll_bar_above_handle;
7395 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
7396 *part = scroll_bar_handle;
7397 else
7398 *part = scroll_bar_below_handle;
7400 XSETINT (*x, win_y);
7401 XSETINT (*y, top_range);
7403 f->mouse_moved = false;
7404 dpyinfo->last_mouse_scroll_bar = NULL;
7405 *timestamp = dpyinfo->last_mouse_movement_time;
7408 unblock_input ();
7412 /* Return information to the user about the current position of the mouse
7413 on the scroll bar. */
7415 static void
7416 x_horizontal_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
7417 enum scroll_bar_part *part, Lisp_Object *x,
7418 Lisp_Object *y, Time *timestamp)
7420 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
7421 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
7422 Window w = bar->x_window;
7423 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7424 int win_x, win_y;
7425 Window dummy_window;
7426 int dummy_coord;
7427 unsigned int dummy_mask;
7429 block_input ();
7431 /* Get the mouse's position relative to the scroll bar window, and
7432 report that. */
7433 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
7435 /* Root, child, root x and root y. */
7436 &dummy_window, &dummy_window,
7437 &dummy_coord, &dummy_coord,
7439 /* Position relative to scroll bar. */
7440 &win_x, &win_y,
7442 /* Mouse buttons and modifier keys. */
7443 &dummy_mask))
7445 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
7447 win_x -= HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
7449 if (bar->dragging != -1)
7450 win_x -= bar->dragging;
7452 if (win_x < 0)
7453 win_x = 0;
7454 if (win_x > left_range)
7455 win_x = left_range;
7457 *fp = f;
7458 *bar_window = bar->window;
7460 if (bar->dragging != -1)
7461 *part = scroll_bar_horizontal_handle;
7462 else if (win_x < bar->start)
7463 *part = scroll_bar_before_handle;
7464 else if (win_x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
7465 *part = scroll_bar_handle;
7466 else
7467 *part = scroll_bar_after_handle;
7469 XSETINT (*y, win_x);
7470 XSETINT (*x, left_range);
7472 f->mouse_moved = false;
7473 dpyinfo->last_mouse_scroll_bar = NULL;
7474 *timestamp = dpyinfo->last_mouse_movement_time;
7477 unblock_input ();
7481 /* The screen has been cleared so we may have changed foreground or
7482 background colors, and the scroll bars may need to be redrawn.
7483 Clear out the scroll bars, and ask for expose events, so we can
7484 redraw them. */
7486 static void
7487 x_scroll_bar_clear (struct frame *f)
7489 #ifndef USE_TOOLKIT_SCROLL_BARS
7490 Lisp_Object bar;
7492 /* We can have scroll bars even if this is 0,
7493 if we just turned off scroll bar mode.
7494 But in that case we should not clear them. */
7495 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
7496 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
7497 bar = XSCROLL_BAR (bar)->next)
7498 XClearArea (FRAME_X_DISPLAY (f),
7499 XSCROLL_BAR (bar)->x_window,
7500 0, 0, 0, 0, True);
7501 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7504 #ifdef ENABLE_CHECKING
7506 /* Record the last 100 characters stored
7507 to help debug the loss-of-chars-during-GC problem. */
7509 static int temp_index;
7510 static short temp_buffer[100];
7512 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
7513 if (temp_index == ARRAYELTS (temp_buffer)) \
7514 temp_index = 0; \
7515 temp_buffer[temp_index++] = (keysym)
7517 #else /* not ENABLE_CHECKING */
7519 #define STORE_KEYSYM_FOR_DEBUG(keysym) ((void)0)
7521 #endif /* ENABLE_CHECKING */
7523 /* Set this to nonzero to fake an "X I/O error"
7524 on a particular display. */
7526 static struct x_display_info *XTread_socket_fake_io_error;
7528 /* When we find no input here, we occasionally do a no-op command
7529 to verify that the X server is still running and we can still talk with it.
7530 We try all the open displays, one by one.
7531 This variable is used for cycling thru the displays. */
7533 static struct x_display_info *next_noop_dpyinfo;
7535 enum
7537 X_EVENT_NORMAL,
7538 X_EVENT_GOTO_OUT,
7539 X_EVENT_DROP
7542 /* Filter events for the current X input method.
7543 DPYINFO is the display this event is for.
7544 EVENT is the X event to filter.
7546 Returns non-zero if the event was filtered, caller shall not process
7547 this event further.
7548 Returns zero if event is wasn't filtered. */
7550 #ifdef HAVE_X_I18N
7551 static int
7552 x_filter_event (struct x_display_info *dpyinfo, XEvent *event)
7554 /* XFilterEvent returns non-zero if the input method has
7555 consumed the event. We pass the frame's X window to
7556 XFilterEvent because that's the one for which the IC
7557 was created. */
7559 struct frame *f1 = x_any_window_to_frame (dpyinfo,
7560 event->xclient.window);
7562 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
7564 #endif
7566 #ifdef USE_GTK
7567 static int current_count;
7568 static int current_finish;
7569 static struct input_event *current_hold_quit;
7571 /* This is the filter function invoked by the GTK event loop.
7572 It is invoked before the XEvent is translated to a GdkEvent,
7573 so we have a chance to act on the event before GTK. */
7574 static GdkFilterReturn
7575 event_handler_gdk (GdkXEvent *gxev, GdkEvent *ev, gpointer data)
7577 XEvent *xev = (XEvent *) gxev;
7579 block_input ();
7580 if (current_count >= 0)
7582 struct x_display_info *dpyinfo;
7584 dpyinfo = x_display_info_for_display (xev->xany.display);
7586 #ifdef HAVE_X_I18N
7587 /* Filter events for the current X input method.
7588 GTK calls XFilterEvent but not for key press and release,
7589 so we do it here. */
7590 if ((xev->type == KeyPress || xev->type == KeyRelease)
7591 && dpyinfo
7592 && x_filter_event (dpyinfo, xev))
7594 unblock_input ();
7595 return GDK_FILTER_REMOVE;
7597 #endif
7599 if (! dpyinfo)
7600 current_finish = X_EVENT_NORMAL;
7601 else
7602 current_count
7603 += handle_one_xevent (dpyinfo, xev, &current_finish,
7604 current_hold_quit);
7606 else
7607 current_finish = x_dispatch_event (xev, xev->xany.display);
7609 unblock_input ();
7611 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
7612 return GDK_FILTER_REMOVE;
7614 return GDK_FILTER_CONTINUE;
7616 #endif /* USE_GTK */
7619 static void xembed_send_message (struct frame *f, Time,
7620 enum xembed_message,
7621 long detail, long data1, long data2);
7623 static void
7624 x_net_wm_state (struct frame *f, Window window)
7626 int value = FULLSCREEN_NONE;
7627 Lisp_Object lval = Qnil;
7628 bool sticky = false;
7630 get_current_wm_state (f, window, &value, &sticky);
7632 switch (value)
7634 case FULLSCREEN_WIDTH:
7635 lval = Qfullwidth;
7636 break;
7637 case FULLSCREEN_HEIGHT:
7638 lval = Qfullheight;
7639 break;
7640 case FULLSCREEN_BOTH:
7641 lval = Qfullboth;
7642 break;
7643 case FULLSCREEN_MAXIMIZED:
7644 lval = Qmaximized;
7645 break;
7648 frame_size_history_add
7649 (f, Qx_net_wm_state, 0, 0,
7650 list2 (get_frame_param (f, Qfullscreen), lval));
7652 store_frame_param (f, Qfullscreen, lval);
7653 /** store_frame_param (f, Qsticky, sticky ? Qt : Qnil); **/
7656 /* Flip back buffers on any frames with undrawn content. */
7657 static void
7658 flush_dirty_back_buffers (void)
7660 block_input ();
7661 Lisp_Object tail, frame;
7662 FOR_EACH_FRAME (tail, frame)
7664 struct frame *f = XFRAME (frame);
7665 if (FRAME_LIVE_P (f) &&
7666 FRAME_X_P (f) &&
7667 FRAME_X_WINDOW (f) &&
7668 !FRAME_GARBAGED_P (f) &&
7669 !buffer_flipping_blocked_p () &&
7670 FRAME_X_NEED_BUFFER_FLIP (f))
7671 show_back_buffer (f);
7673 unblock_input ();
7676 /* Handles the XEvent EVENT on display DPYINFO.
7678 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
7679 *FINISH is zero if caller should continue reading events.
7680 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
7681 *EVENT is unchanged unless we're processing KeyPress event.
7683 We return the number of characters stored into the buffer. */
7685 static int
7686 handle_one_xevent (struct x_display_info *dpyinfo,
7687 const XEvent *event,
7688 int *finish, struct input_event *hold_quit)
7690 union buffered_input_event inev;
7691 int count = 0;
7692 int do_help = 0;
7693 ptrdiff_t nbytes = 0;
7694 struct frame *any, *f = NULL;
7695 struct coding_system coding;
7696 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
7697 /* This holds the state XLookupString needs to implement dead keys
7698 and other tricks known as "compose processing". _X Window System_
7699 says that a portable program can't use this, but Stephen Gildea assures
7700 me that letting the compiler initialize it to zeros will work okay. */
7701 static XComposeStatus compose_status;
7702 XEvent configureEvent;
7703 XEvent next_event;
7705 USE_SAFE_ALLOCA;
7707 *finish = X_EVENT_NORMAL;
7709 EVENT_INIT (inev.ie);
7710 inev.ie.kind = NO_EVENT;
7711 inev.ie.arg = Qnil;
7713 any = x_any_window_to_frame (dpyinfo, event->xany.window);
7715 if (any && any->wait_event_type == event->type)
7716 any->wait_event_type = 0; /* Indicates we got it. */
7718 switch (event->type)
7720 case ClientMessage:
7722 if (event->xclient.message_type == dpyinfo->Xatom_wm_protocols
7723 && event->xclient.format == 32)
7725 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_take_focus)
7727 /* Use the value returned by x_any_window_to_frame
7728 because this could be the shell widget window
7729 if the frame has no title bar. */
7730 f = any;
7731 #ifdef HAVE_X_I18N
7732 /* Not quite sure this is needed -pd */
7733 if (f && FRAME_XIC (f))
7734 XSetICFocus (FRAME_XIC (f));
7735 #endif
7736 #if false
7737 /* Emacs sets WM hints whose `input' field is `true'. This
7738 instructs the WM to set the input focus automatically for
7739 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
7740 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
7741 it has set the focus. So, XSetInputFocus below is not
7742 needed.
7744 The call to XSetInputFocus below has also caused trouble. In
7745 cases where the XSetInputFocus done by the WM and the one
7746 below are temporally close (on a fast machine), the call
7747 below can generate additional FocusIn events which confuse
7748 Emacs. */
7750 /* Since we set WM_TAKE_FOCUS, we must call
7751 XSetInputFocus explicitly. But not if f is null,
7752 since that might be an event for a deleted frame. */
7753 if (f)
7755 Display *d = event->xclient.display;
7756 /* Catch and ignore errors, in case window has been
7757 iconified by a window manager such as GWM. */
7758 x_catch_errors (d);
7759 XSetInputFocus (d, event->xclient.window,
7760 /* The ICCCM says this is
7761 the only valid choice. */
7762 RevertToParent,
7763 event->xclient.data.l[1]);
7764 x_uncatch_errors ();
7766 /* Not certain about handling scroll bars here */
7767 #endif
7768 goto done;
7771 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_save_yourself)
7773 /* Save state modify the WM_COMMAND property to
7774 something which can reinstate us. This notifies
7775 the session manager, who's looking for such a
7776 PropertyNotify. Can restart processing when
7777 a keyboard or mouse event arrives. */
7778 /* If we have a session manager, don't set this.
7779 KDE will then start two Emacsen, one for the
7780 session manager and one for this. */
7781 #ifdef HAVE_X_SM
7782 if (! x_session_have_connection ())
7783 #endif
7785 f = x_top_window_to_frame (dpyinfo,
7786 event->xclient.window);
7787 /* This is just so we only give real data once
7788 for a single Emacs process. */
7789 if (f == SELECTED_FRAME ())
7790 XSetCommand (FRAME_X_DISPLAY (f),
7791 event->xclient.window,
7792 initial_argv, initial_argc);
7793 else if (f)
7794 XSetCommand (FRAME_X_DISPLAY (f),
7795 event->xclient.window,
7796 0, 0);
7798 goto done;
7801 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_delete_window)
7803 f = any;
7804 if (!f)
7805 goto OTHER; /* May be a dialog that is to be removed */
7807 inev.ie.kind = DELETE_WINDOW_EVENT;
7808 XSETFRAME (inev.ie.frame_or_window, f);
7809 goto done;
7812 goto done;
7815 if (event->xclient.message_type == dpyinfo->Xatom_wm_configure_denied)
7816 goto done;
7818 if (event->xclient.message_type == dpyinfo->Xatom_wm_window_moved)
7820 int new_x, new_y;
7821 f = x_window_to_frame (dpyinfo, event->xclient.window);
7823 new_x = event->xclient.data.s[0];
7824 new_y = event->xclient.data.s[1];
7826 if (f)
7828 f->left_pos = new_x;
7829 f->top_pos = new_y;
7831 goto done;
7834 #ifdef X_TOOLKIT_EDITRES
7835 if (event->xclient.message_type == dpyinfo->Xatom_editres)
7837 f = any;
7838 if (f)
7839 _XEditResCheckMessages (f->output_data.x->widget,
7840 NULL, (XEvent *) event, NULL);
7841 goto done;
7843 #endif /* X_TOOLKIT_EDITRES */
7845 if (event->xclient.message_type == dpyinfo->Xatom_DONE
7846 || event->xclient.message_type == dpyinfo->Xatom_PAGE)
7848 /* Ghostview job completed. Kill it. We could
7849 reply with "Next" if we received "Page", but we
7850 currently never do because we are interested in
7851 images, only, which should have 1 page. */
7852 Pixmap pixmap = (Pixmap) event->xclient.data.l[1];
7853 f = x_window_to_frame (dpyinfo, event->xclient.window);
7854 if (!f)
7855 goto OTHER;
7856 x_kill_gs_process (pixmap, f);
7857 expose_frame (f, 0, 0, 0, 0);
7858 goto done;
7861 #ifdef USE_TOOLKIT_SCROLL_BARS
7862 /* Scroll bar callbacks send a ClientMessage from which
7863 we construct an input_event. */
7864 if (event->xclient.message_type == dpyinfo->Xatom_Scrollbar)
7866 x_scroll_bar_to_input_event (event, &inev.ie);
7867 *finish = X_EVENT_GOTO_OUT;
7868 goto done;
7870 else if (event->xclient.message_type == dpyinfo->Xatom_Horizontal_Scrollbar)
7872 x_horizontal_scroll_bar_to_input_event (event, &inev.ie);
7873 *finish = X_EVENT_GOTO_OUT;
7874 goto done;
7876 #endif /* USE_TOOLKIT_SCROLL_BARS */
7878 /* XEmbed messages from the embedder (if any). */
7879 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
7881 enum xembed_message msg = event->xclient.data.l[1];
7882 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
7883 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7885 *finish = X_EVENT_GOTO_OUT;
7886 goto done;
7889 xft_settings_event (dpyinfo, event);
7891 f = any;
7892 if (!f)
7893 goto OTHER;
7894 if (x_handle_dnd_message (f, &event->xclient, dpyinfo, &inev.ie))
7895 *finish = X_EVENT_DROP;
7897 break;
7899 case SelectionNotify:
7900 x_display_set_last_user_time (dpyinfo, event->xselection.time);
7901 #ifdef USE_X_TOOLKIT
7902 if (! x_window_to_frame (dpyinfo, event->xselection.requestor))
7903 goto OTHER;
7904 #endif /* not USE_X_TOOLKIT */
7905 x_handle_selection_notify (&event->xselection);
7906 break;
7908 case SelectionClear: /* Someone has grabbed ownership. */
7909 x_display_set_last_user_time (dpyinfo, event->xselectionclear.time);
7910 #ifdef USE_X_TOOLKIT
7911 if (! x_window_to_frame (dpyinfo, event->xselectionclear.window))
7912 goto OTHER;
7913 #endif /* USE_X_TOOLKIT */
7915 const XSelectionClearEvent *eventp = &event->xselectionclear;
7917 inev.sie.kind = SELECTION_CLEAR_EVENT;
7918 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
7919 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
7920 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
7922 break;
7924 case SelectionRequest: /* Someone wants our selection. */
7925 x_display_set_last_user_time (dpyinfo, event->xselectionrequest.time);
7926 #ifdef USE_X_TOOLKIT
7927 if (!x_window_to_frame (dpyinfo, event->xselectionrequest.owner))
7928 goto OTHER;
7929 #endif /* USE_X_TOOLKIT */
7931 const XSelectionRequestEvent *eventp = &event->xselectionrequest;
7933 inev.sie.kind = SELECTION_REQUEST_EVENT;
7934 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
7935 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
7936 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
7937 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
7938 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
7939 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
7941 break;
7943 case PropertyNotify:
7944 x_display_set_last_user_time (dpyinfo, event->xproperty.time);
7945 f = x_top_window_to_frame (dpyinfo, event->xproperty.window);
7946 if (f && event->xproperty.atom == dpyinfo->Xatom_net_wm_state)
7948 bool not_hidden = x_handle_net_wm_state (f, &event->xproperty);
7949 if (not_hidden && FRAME_ICONIFIED_P (f))
7951 /* Gnome shell does not iconify us when C-z is pressed.
7952 It hides the frame. So if our state says we aren't
7953 hidden anymore, treat it as deiconified. */
7954 SET_FRAME_VISIBLE (f, 1);
7955 SET_FRAME_ICONIFIED (f, false);
7956 f->output_data.x->has_been_visible = true;
7957 inev.ie.kind = DEICONIFY_EVENT;
7958 XSETFRAME (inev.ie.frame_or_window, f);
7960 else if (! not_hidden && ! FRAME_ICONIFIED_P (f))
7962 SET_FRAME_VISIBLE (f, 0);
7963 SET_FRAME_ICONIFIED (f, true);
7964 inev.ie.kind = ICONIFY_EVENT;
7965 XSETFRAME (inev.ie.frame_or_window, f);
7969 x_handle_property_notify (&event->xproperty);
7970 xft_settings_event (dpyinfo, event);
7971 goto OTHER;
7973 case ReparentNotify:
7974 f = x_top_window_to_frame (dpyinfo, event->xreparent.window);
7975 if (f)
7977 /* Maybe we shouldn't set this for child frames ?? */
7978 f->output_data.x->parent_desc = event->xreparent.parent;
7979 if (!FRAME_PARENT_FRAME (f))
7980 x_real_positions (f, &f->left_pos, &f->top_pos);
7981 else
7983 Window root;
7984 unsigned int dummy_uint;
7986 block_input ();
7987 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
7988 &root, &f->left_pos, &f->top_pos,
7989 &dummy_uint, &dummy_uint, &dummy_uint, &dummy_uint);
7990 unblock_input ();
7993 /* Perhaps reparented due to a WM restart. Reset this. */
7994 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
7995 FRAME_DISPLAY_INFO (f)->net_supported_window = 0;
7997 x_set_frame_alpha (f);
7999 goto OTHER;
8001 case Expose:
8002 f = x_window_to_frame (dpyinfo, event->xexpose.window);
8003 if (f)
8005 if (!FRAME_VISIBLE_P (f))
8007 block_input ();
8008 SET_FRAME_VISIBLE (f, 1);
8009 SET_FRAME_ICONIFIED (f, false);
8010 if (FRAME_X_DOUBLE_BUFFERED_P (f))
8011 font_drop_xrender_surfaces (f);
8012 f->output_data.x->has_been_visible = true;
8013 SET_FRAME_GARBAGED (f);
8014 unblock_input ();
8016 else if (FRAME_GARBAGED_P (f))
8018 #ifdef USE_GTK
8019 /* Go around the back buffer and manually clear the
8020 window the first time we show it. This way, we avoid
8021 showing users the sanity-defying horror of whatever
8022 GtkWindow is rendering beneath us. We've garbaged
8023 the frame, so we'll redraw the whole thing on next
8024 redisplay anyway. Yuck. */
8025 x_clear_area1 (
8026 FRAME_X_DISPLAY (f),
8027 FRAME_X_WINDOW (f),
8028 event->xexpose.x, event->xexpose.y,
8029 event->xexpose.width, event->xexpose.height,
8031 x_clear_under_internal_border (f);
8032 #endif
8036 if (!FRAME_GARBAGED_P (f))
8038 #ifdef USE_GTK
8039 /* This seems to be needed for GTK 2.6 and later, see
8040 https://debbugs.gnu.org/cgi/bugreport.cgi?bug=15398. */
8041 x_clear_area (f,
8042 event->xexpose.x, event->xexpose.y,
8043 event->xexpose.width, event->xexpose.height);
8044 #endif
8045 expose_frame (f, event->xexpose.x, event->xexpose.y,
8046 event->xexpose.width, event->xexpose.height);
8047 #ifdef USE_GTK
8048 x_clear_under_internal_border (f);
8049 #endif
8052 if (!FRAME_GARBAGED_P (f))
8053 show_back_buffer (f);
8055 else
8057 #ifndef USE_TOOLKIT_SCROLL_BARS
8058 struct scroll_bar *bar;
8059 #endif
8060 #if defined USE_LUCID
8061 /* Submenus of the Lucid menu bar aren't widgets
8062 themselves, so there's no way to dispatch events
8063 to them. Recognize this case separately. */
8065 Widget widget = x_window_to_menu_bar (event->xexpose.window);
8066 if (widget)
8067 xlwmenu_redisplay (widget);
8069 #endif /* USE_LUCID */
8071 #ifdef USE_TOOLKIT_SCROLL_BARS
8072 /* Dispatch event to the widget. */
8073 goto OTHER;
8074 #else /* not USE_TOOLKIT_SCROLL_BARS */
8075 bar = x_window_to_scroll_bar (event->xexpose.display,
8076 event->xexpose.window, 2);
8078 if (bar)
8079 x_scroll_bar_expose (bar, event);
8080 #ifdef USE_X_TOOLKIT
8081 else
8082 goto OTHER;
8083 #endif /* USE_X_TOOLKIT */
8084 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8086 break;
8088 case GraphicsExpose: /* This occurs when an XCopyArea's
8089 source area was obscured or not
8090 available. */
8091 f = x_window_to_frame (dpyinfo, event->xgraphicsexpose.drawable);
8092 if (f)
8094 expose_frame (f, event->xgraphicsexpose.x,
8095 event->xgraphicsexpose.y,
8096 event->xgraphicsexpose.width,
8097 event->xgraphicsexpose.height);
8098 #ifdef USE_GTK
8099 x_clear_under_internal_border (f);
8100 #endif
8101 show_back_buffer (f);
8103 #ifdef USE_X_TOOLKIT
8104 else
8105 goto OTHER;
8106 #endif /* USE_X_TOOLKIT */
8107 break;
8109 case NoExpose: /* This occurs when an XCopyArea's
8110 source area was completely
8111 available. */
8112 break;
8114 case UnmapNotify:
8115 /* Redo the mouse-highlight after the tooltip has gone. */
8116 if (event->xunmap.window == tip_window)
8118 tip_window = None;
8119 x_redo_mouse_highlight (dpyinfo);
8122 f = x_top_window_to_frame (dpyinfo, event->xunmap.window);
8123 if (f) /* F may no longer exist if
8124 the frame was deleted. */
8126 bool visible = FRAME_VISIBLE_P (f);
8127 /* While a frame is unmapped, display generation is
8128 disabled; you don't want to spend time updating a
8129 display that won't ever be seen. */
8130 SET_FRAME_VISIBLE (f, 0);
8131 /* We can't distinguish, from the event, whether the window
8132 has become iconified or invisible. So assume, if it
8133 was previously visible, than now it is iconified.
8134 But x_make_frame_invisible clears both
8135 the visible flag and the iconified flag;
8136 and that way, we know the window is not iconified now. */
8137 if (visible || FRAME_ICONIFIED_P (f))
8139 SET_FRAME_ICONIFIED (f, true);
8140 inev.ie.kind = ICONIFY_EVENT;
8141 XSETFRAME (inev.ie.frame_or_window, f);
8144 goto OTHER;
8146 case MapNotify:
8147 /* We use x_top_window_to_frame because map events can
8148 come for sub-windows and they don't mean that the
8149 frame is visible. */
8150 f = x_top_window_to_frame (dpyinfo, event->xmap.window);
8151 if (f)
8153 bool iconified = FRAME_ICONIFIED_P (f);
8155 /* Check if fullscreen was specified before we where mapped the
8156 first time, i.e. from the command line. */
8157 if (!f->output_data.x->has_been_visible)
8160 x_check_fullscreen (f);
8161 #ifndef USE_GTK
8162 /* For systems that cannot synthesize `skip_taskbar' for
8163 unmapped windows do the following. */
8164 if (FRAME_SKIP_TASKBAR (f))
8165 x_set_skip_taskbar (f, Qt, Qnil);
8166 #endif /* Not USE_GTK */
8169 if (!iconified)
8171 /* The `z-group' is reset every time a frame becomes
8172 invisible. Handle this here. */
8173 if (FRAME_Z_GROUP (f) == z_group_above)
8174 x_set_z_group (f, Qabove, Qnil);
8175 else if (FRAME_Z_GROUP (f) == z_group_below)
8176 x_set_z_group (f, Qbelow, Qnil);
8179 SET_FRAME_VISIBLE (f, 1);
8180 SET_FRAME_ICONIFIED (f, false);
8181 f->output_data.x->has_been_visible = true;
8183 if (iconified)
8185 inev.ie.kind = DEICONIFY_EVENT;
8186 XSETFRAME (inev.ie.frame_or_window, f);
8188 else if (! NILP (Vframe_list) && ! NILP (XCDR (Vframe_list)))
8189 /* Force a redisplay sooner or later to update the
8190 frame titles in case this is the second frame. */
8191 record_asynch_buffer_change ();
8193 goto OTHER;
8195 case KeyPress:
8197 x_display_set_last_user_time (dpyinfo, event->xkey.time);
8198 ignore_next_mouse_click_timeout = 0;
8200 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8201 /* Dispatch KeyPress events when in menu. */
8202 if (popup_activated ())
8203 goto OTHER;
8204 #endif
8206 f = any;
8208 /* If mouse-highlight is an integer, input clears out
8209 mouse highlighting. */
8210 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
8211 #if ! defined (USE_GTK)
8212 && (f == 0
8213 || !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
8214 #endif
8217 clear_mouse_face (hlinfo);
8218 hlinfo->mouse_face_hidden = true;
8221 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
8222 if (f == 0)
8224 /* Scroll bars consume key events, but we want
8225 the keys to go to the scroll bar's frame. */
8226 Widget widget = XtWindowToWidget (dpyinfo->display,
8227 event->xkey.window);
8228 if (widget && XmIsScrollBar (widget))
8230 widget = XtParent (widget);
8231 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
8234 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
8236 if (f != 0)
8238 KeySym keysym, orig_keysym;
8239 /* al%imercury@uunet.uu.net says that making this 81
8240 instead of 80 fixed a bug whereby meta chars made
8241 his Emacs hang.
8243 It seems that some version of XmbLookupString has
8244 a bug of not returning XBufferOverflow in
8245 status_return even if the input is too long to
8246 fit in 81 bytes. So, we must prepare sufficient
8247 bytes for copy_buffer. 513 bytes (256 chars for
8248 two-byte character set) seems to be a fairly good
8249 approximation. -- 2000.8.10 handa@etl.go.jp */
8250 unsigned char copy_buffer[513];
8251 unsigned char *copy_bufptr = copy_buffer;
8252 int copy_bufsiz = sizeof (copy_buffer);
8253 int modifiers;
8254 Lisp_Object coding_system = Qlatin_1;
8255 Lisp_Object c;
8256 /* Event will be modified. */
8257 XKeyEvent xkey = event->xkey;
8259 #ifdef USE_GTK
8260 /* Don't pass keys to GTK. A Tab will shift focus to the
8261 tool bar in GTK 2.4. Keys will still go to menus and
8262 dialogs because in that case popup_activated is nonzero
8263 (see above). */
8264 *finish = X_EVENT_DROP;
8265 #endif
8267 xkey.state |= x_emacs_to_x_modifiers (FRAME_DISPLAY_INFO (f),
8268 extra_keyboard_modifiers);
8269 modifiers = xkey.state;
8271 /* This will have to go some day... */
8273 /* make_lispy_event turns chars into control chars.
8274 Don't do it here because XLookupString is too eager. */
8275 xkey.state &= ~ControlMask;
8276 xkey.state &= ~(dpyinfo->meta_mod_mask
8277 | dpyinfo->super_mod_mask
8278 | dpyinfo->hyper_mod_mask
8279 | dpyinfo->alt_mod_mask);
8281 /* In case Meta is ComposeCharacter,
8282 clear its status. According to Markus Ehrnsperger
8283 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
8284 this enables ComposeCharacter to work whether or
8285 not it is combined with Meta. */
8286 if (modifiers & dpyinfo->meta_mod_mask)
8287 memset (&compose_status, 0, sizeof (compose_status));
8289 #ifdef HAVE_X_I18N
8290 if (FRAME_XIC (f))
8292 Status status_return;
8294 coding_system = Vlocale_coding_system;
8295 nbytes = XmbLookupString (FRAME_XIC (f),
8296 &xkey, (char *) copy_bufptr,
8297 copy_bufsiz, &keysym,
8298 &status_return);
8299 if (status_return == XBufferOverflow)
8301 copy_bufsiz = nbytes + 1;
8302 copy_bufptr = alloca (copy_bufsiz);
8303 nbytes = XmbLookupString (FRAME_XIC (f),
8304 &xkey, (char *) copy_bufptr,
8305 copy_bufsiz, &keysym,
8306 &status_return);
8308 /* Xutf8LookupString is a new but already deprecated interface. -stef */
8309 if (status_return == XLookupNone)
8310 break;
8311 else if (status_return == XLookupChars)
8313 keysym = NoSymbol;
8314 modifiers = 0;
8316 else if (status_return != XLookupKeySym
8317 && status_return != XLookupBoth)
8318 emacs_abort ();
8320 else
8321 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
8322 copy_bufsiz, &keysym,
8323 &compose_status);
8324 #else
8325 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
8326 copy_bufsiz, &keysym,
8327 &compose_status);
8328 #endif
8330 /* If not using XIM/XIC, and a compose sequence is in progress,
8331 we break here. Otherwise, chars_matched is always 0. */
8332 if (compose_status.chars_matched > 0 && nbytes == 0)
8333 break;
8335 memset (&compose_status, 0, sizeof (compose_status));
8336 orig_keysym = keysym;
8338 /* Common for all keysym input events. */
8339 XSETFRAME (inev.ie.frame_or_window, f);
8340 inev.ie.modifiers
8341 = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), modifiers);
8342 inev.ie.timestamp = xkey.time;
8344 /* First deal with keysyms which have defined
8345 translations to characters. */
8346 if (keysym >= 32 && keysym < 128)
8347 /* Avoid explicitly decoding each ASCII character. */
8349 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
8350 inev.ie.code = keysym;
8351 goto done_keysym;
8354 /* Keysyms directly mapped to Unicode characters. */
8355 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
8357 if (keysym < 0x01000080)
8358 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
8359 else
8360 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
8361 inev.ie.code = keysym & 0xFFFFFF;
8362 goto done_keysym;
8365 /* Now non-ASCII. */
8366 if (HASH_TABLE_P (Vx_keysym_table)
8367 && (c = Fgethash (make_number (keysym),
8368 Vx_keysym_table,
8369 Qnil),
8370 NATNUMP (c)))
8372 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
8373 ? ASCII_KEYSTROKE_EVENT
8374 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
8375 inev.ie.code = XFASTINT (c);
8376 goto done_keysym;
8379 /* Random non-modifier sorts of keysyms. */
8380 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
8381 || keysym == XK_Delete
8382 #ifdef XK_ISO_Left_Tab
8383 || (keysym >= XK_ISO_Left_Tab
8384 && keysym <= XK_ISO_Enter)
8385 #endif
8386 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
8387 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
8388 #ifdef HPUX
8389 /* This recognizes the "extended function
8390 keys". It seems there's no cleaner way.
8391 Test IsModifierKey to avoid handling
8392 mode_switch incorrectly. */
8393 || (XK_Select <= keysym && keysym < XK_KP_Space)
8394 #endif
8395 #ifdef XK_dead_circumflex
8396 || orig_keysym == XK_dead_circumflex
8397 #endif
8398 #ifdef XK_dead_grave
8399 || orig_keysym == XK_dead_grave
8400 #endif
8401 #ifdef XK_dead_tilde
8402 || orig_keysym == XK_dead_tilde
8403 #endif
8404 #ifdef XK_dead_diaeresis
8405 || orig_keysym == XK_dead_diaeresis
8406 #endif
8407 #ifdef XK_dead_macron
8408 || orig_keysym == XK_dead_macron
8409 #endif
8410 #ifdef XK_dead_degree
8411 || orig_keysym == XK_dead_degree
8412 #endif
8413 #ifdef XK_dead_acute
8414 || orig_keysym == XK_dead_acute
8415 #endif
8416 #ifdef XK_dead_cedilla
8417 || orig_keysym == XK_dead_cedilla
8418 #endif
8419 #ifdef XK_dead_breve
8420 || orig_keysym == XK_dead_breve
8421 #endif
8422 #ifdef XK_dead_ogonek
8423 || orig_keysym == XK_dead_ogonek
8424 #endif
8425 #ifdef XK_dead_caron
8426 || orig_keysym == XK_dead_caron
8427 #endif
8428 #ifdef XK_dead_doubleacute
8429 || orig_keysym == XK_dead_doubleacute
8430 #endif
8431 #ifdef XK_dead_abovedot
8432 || orig_keysym == XK_dead_abovedot
8433 #endif
8434 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
8435 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
8436 /* Any "vendor-specific" key is ok. */
8437 || (orig_keysym & (1 << 28))
8438 || (keysym != NoSymbol && nbytes == 0))
8439 && ! (IsModifierKey (orig_keysym)
8440 /* The symbols from XK_ISO_Lock
8441 to XK_ISO_Last_Group_Lock
8442 don't have real modifiers but
8443 should be treated similarly to
8444 Mode_switch by Emacs. */
8445 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
8446 || (XK_ISO_Lock <= orig_keysym
8447 && orig_keysym <= XK_ISO_Last_Group_Lock)
8448 #endif
8451 STORE_KEYSYM_FOR_DEBUG (keysym);
8452 /* make_lispy_event will convert this to a symbolic
8453 key. */
8454 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
8455 inev.ie.code = keysym;
8456 goto done_keysym;
8459 { /* Raw bytes, not keysym. */
8460 ptrdiff_t i;
8461 int nchars, len;
8463 for (i = 0, nchars = 0; i < nbytes; i++)
8465 if (ASCII_CHAR_P (copy_bufptr[i]))
8466 nchars++;
8467 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
8470 if (nchars < nbytes)
8472 /* Decode the input data. */
8474 /* The input should be decoded with `coding_system'
8475 which depends on which X*LookupString function
8476 we used just above and the locale. */
8477 setup_coding_system (coding_system, &coding);
8478 coding.src_multibyte = false;
8479 coding.dst_multibyte = true;
8480 /* The input is converted to events, thus we can't
8481 handle composition. Anyway, there's no XIM that
8482 gives us composition information. */
8483 coding.common_flags &= ~CODING_ANNOTATION_MASK;
8485 SAFE_NALLOCA (coding.destination, MAX_MULTIBYTE_LENGTH,
8486 nbytes);
8487 coding.dst_bytes = MAX_MULTIBYTE_LENGTH * nbytes;
8488 coding.mode |= CODING_MODE_LAST_BLOCK;
8489 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
8490 nbytes = coding.produced;
8491 nchars = coding.produced_char;
8492 copy_bufptr = coding.destination;
8495 /* Convert the input data to a sequence of
8496 character events. */
8497 for (i = 0; i < nbytes; i += len)
8499 int ch;
8500 if (nchars == nbytes)
8501 ch = copy_bufptr[i], len = 1;
8502 else
8503 ch = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
8504 inev.ie.kind = (SINGLE_BYTE_CHAR_P (ch)
8505 ? ASCII_KEYSTROKE_EVENT
8506 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
8507 inev.ie.code = ch;
8508 kbd_buffer_store_buffered_event (&inev, hold_quit);
8511 count += nchars;
8513 inev.ie.kind = NO_EVENT; /* Already stored above. */
8515 if (keysym == NoSymbol)
8516 break;
8518 /* FIXME: check side effects and remove this. */
8519 ((XEvent *) event)->xkey = xkey;
8521 done_keysym:
8522 #ifdef HAVE_X_I18N
8523 /* Don't dispatch this event since XtDispatchEvent calls
8524 XFilterEvent, and two calls in a row may freeze the
8525 client. */
8526 break;
8527 #else
8528 goto OTHER;
8529 #endif
8531 case KeyRelease:
8532 x_display_set_last_user_time (dpyinfo, event->xkey.time);
8533 #ifdef HAVE_X_I18N
8534 /* Don't dispatch this event since XtDispatchEvent calls
8535 XFilterEvent, and two calls in a row may freeze the
8536 client. */
8537 break;
8538 #else
8539 goto OTHER;
8540 #endif
8542 case EnterNotify:
8543 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
8544 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8546 f = any;
8548 if (f && x_mouse_click_focus_ignore_position)
8549 ignore_next_mouse_click_timeout = event->xmotion.time + 200;
8551 /* EnterNotify counts as mouse movement,
8552 so update things that depend on mouse position. */
8553 if (f && !f->output_data.x->hourglass_p)
8554 note_mouse_movement (f, &event->xmotion);
8555 #ifdef USE_GTK
8556 /* We may get an EnterNotify on the buttons in the toolbar. In that
8557 case we moved out of any highlighted area and need to note this. */
8558 if (!f && dpyinfo->last_mouse_glyph_frame)
8559 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
8560 #endif
8561 goto OTHER;
8563 case FocusIn:
8564 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8565 goto OTHER;
8567 case LeaveNotify:
8568 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
8569 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8571 f = x_top_window_to_frame (dpyinfo, event->xcrossing.window);
8572 if (f)
8574 if (f == hlinfo->mouse_face_mouse_frame)
8576 /* If we move outside the frame, then we're
8577 certainly no longer on any text in the frame. */
8578 clear_mouse_face (hlinfo);
8579 hlinfo->mouse_face_mouse_frame = 0;
8582 /* Generate a nil HELP_EVENT to cancel a help-echo.
8583 Do it only if there's something to cancel.
8584 Otherwise, the startup message is cleared when
8585 the mouse leaves the frame. */
8586 if (any_help_event_p)
8587 do_help = -1;
8589 #ifdef USE_GTK
8590 /* See comment in EnterNotify above */
8591 else if (dpyinfo->last_mouse_glyph_frame)
8592 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
8593 #endif
8594 goto OTHER;
8596 case FocusOut:
8597 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8598 goto OTHER;
8600 case MotionNotify:
8602 x_display_set_last_user_time (dpyinfo, event->xmotion.time);
8603 previous_help_echo_string = help_echo_string;
8604 help_echo_string = Qnil;
8606 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
8607 : x_window_to_frame (dpyinfo, event->xmotion.window));
8609 if (hlinfo->mouse_face_hidden)
8611 hlinfo->mouse_face_hidden = false;
8612 clear_mouse_face (hlinfo);
8615 #ifdef USE_GTK
8616 if (f && xg_event_is_for_scrollbar (f, event))
8617 f = 0;
8618 #endif
8619 if (f)
8621 /* Maybe generate a SELECT_WINDOW_EVENT for
8622 `mouse-autoselect-window' but don't let popup menus
8623 interfere with this (Bug#1261). */
8624 if (!NILP (Vmouse_autoselect_window)
8625 && !popup_activated ()
8626 /* Don't switch if we're currently in the minibuffer.
8627 This tries to work around problems where the
8628 minibuffer gets unselected unexpectedly, and where
8629 you then have to move your mouse all the way down to
8630 the minibuffer to select it. */
8631 && !MINI_WINDOW_P (XWINDOW (selected_window))
8632 /* With `focus-follows-mouse' non-nil create an event
8633 also when the target window is on another frame. */
8634 && (f == XFRAME (selected_frame)
8635 || !NILP (focus_follows_mouse)))
8637 static Lisp_Object last_mouse_window;
8638 Lisp_Object window = window_from_coordinates
8639 (f, event->xmotion.x, event->xmotion.y, 0, false);
8641 /* A window will be autoselected only when it is not
8642 selected now and the last mouse movement event was
8643 not in it. The remainder of the code is a bit vague
8644 wrt what a "window" is. For immediate autoselection,
8645 the window is usually the entire window but for GTK
8646 where the scroll bars don't count. For delayed
8647 autoselection the window is usually the window's text
8648 area including the margins. */
8649 if (WINDOWP (window)
8650 && !EQ (window, last_mouse_window)
8651 && !EQ (window, selected_window))
8653 inev.ie.kind = SELECT_WINDOW_EVENT;
8654 inev.ie.frame_or_window = window;
8657 /* Remember the last window where we saw the mouse. */
8658 last_mouse_window = window;
8661 if (!note_mouse_movement (f, &event->xmotion))
8662 help_echo_string = previous_help_echo_string;
8664 else
8666 #ifndef USE_TOOLKIT_SCROLL_BARS
8667 struct scroll_bar *bar
8668 = x_window_to_scroll_bar (event->xmotion.display,
8669 event->xmotion.window, 2);
8671 if (bar)
8672 x_scroll_bar_note_movement (bar, &event->xmotion);
8673 #endif /* USE_TOOLKIT_SCROLL_BARS */
8675 /* If we move outside the frame, then we're
8676 certainly no longer on any text in the frame. */
8677 clear_mouse_face (hlinfo);
8680 /* If the contents of the global variable help_echo_string
8681 has changed, generate a HELP_EVENT. */
8682 if (!NILP (help_echo_string)
8683 || !NILP (previous_help_echo_string))
8684 do_help = 1;
8685 goto OTHER;
8688 case ConfigureNotify:
8689 /* An opaque move can generate a stream of events as the window
8690 is dragged around. If the connection round trip time isn't
8691 really short, they may come faster than we can respond to
8692 them, given the multiple queries we can do to check window
8693 manager state, translate coordinates, etc.
8695 So if this ConfigureNotify is immediately followed by another
8696 for the same window, use the info from the latest update, and
8697 consider the events all handled. */
8698 /* Opaque resize may be trickier; ConfigureNotify events are
8699 mixed with Expose events for multiple windows. */
8700 configureEvent = *event;
8701 while (XPending (dpyinfo->display))
8703 XNextEvent (dpyinfo->display, &next_event);
8704 if (next_event.type != ConfigureNotify
8705 || next_event.xconfigure.window != event->xconfigure.window
8706 /* Skipping events with different sizes can lead to a
8707 mispositioned mode line at initial window creation.
8708 Only drop window motion events for now. */
8709 || next_event.xconfigure.width != event->xconfigure.width
8710 || next_event.xconfigure.height != event->xconfigure.height)
8712 XPutBackEvent (dpyinfo->display, &next_event);
8713 break;
8715 else
8716 configureEvent = next_event;
8719 f = x_top_window_to_frame (dpyinfo, configureEvent.xconfigure.window);
8720 /* Unfortunately, we need to call font_drop_xrender_surfaces for
8721 _all_ ConfigureNotify events, otherwise we miss some and
8722 flicker. Don't try to optimize these calls by looking only
8723 for size changes: that's not sufficient. We miss some
8724 surface invalidations and flicker. */
8725 block_input ();
8726 if (f && FRAME_X_DOUBLE_BUFFERED_P (f))
8727 font_drop_xrender_surfaces (f);
8728 unblock_input ();
8729 #ifdef USE_CAIRO
8730 if (f) x_cr_destroy_surface (f);
8731 #endif
8732 #ifdef USE_GTK
8733 if (!f
8734 && (f = any)
8735 && configureEvent.xconfigure.window == FRAME_X_WINDOW (f))
8737 block_input ();
8738 if (FRAME_X_DOUBLE_BUFFERED_P (f))
8739 font_drop_xrender_surfaces (f);
8740 unblock_input ();
8741 xg_frame_resized (f, configureEvent.xconfigure.width,
8742 configureEvent.xconfigure.height);
8743 #ifdef USE_CAIRO
8744 x_cr_destroy_surface (f);
8745 #endif
8746 f = 0;
8748 #endif
8749 if (f)
8751 #ifdef USE_GTK
8752 /* For GTK+ don't call x_net_wm_state for the scroll bar
8753 window. (Bug#24963, Bug#25887) */
8754 if (configureEvent.xconfigure.window == FRAME_X_WINDOW (f))
8755 #endif
8756 x_net_wm_state (f, configureEvent.xconfigure.window);
8758 #ifdef USE_X_TOOLKIT
8759 /* Tip frames are pure X window, set size for them. */
8760 if (FRAME_TOOLTIP_P (f))
8762 if (FRAME_PIXEL_HEIGHT (f) != configureEvent.xconfigure.height
8763 || FRAME_PIXEL_WIDTH (f) != configureEvent.xconfigure.width)
8765 SET_FRAME_GARBAGED (f);
8767 FRAME_PIXEL_HEIGHT (f) = configureEvent.xconfigure.height;
8768 FRAME_PIXEL_WIDTH (f) = configureEvent.xconfigure.width;
8770 #endif
8772 #ifndef USE_X_TOOLKIT
8773 #ifndef USE_GTK
8774 int width =
8775 FRAME_PIXEL_TO_TEXT_WIDTH (f, configureEvent.xconfigure.width);
8776 int height =
8777 FRAME_PIXEL_TO_TEXT_HEIGHT (f, configureEvent.xconfigure.height);
8779 /* In the toolkit version, change_frame_size
8780 is called by the code that handles resizing
8781 of the EmacsFrame widget. */
8783 /* Even if the number of character rows and columns has
8784 not changed, the font size may have changed, so we need
8785 to check the pixel dimensions as well. */
8786 if (width != FRAME_TEXT_WIDTH (f)
8787 || height != FRAME_TEXT_HEIGHT (f)
8788 || configureEvent.xconfigure.width != FRAME_PIXEL_WIDTH (f)
8789 || configureEvent.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
8791 change_frame_size (f, width, height, false, true, false, true);
8792 x_clear_under_internal_border (f);
8793 SET_FRAME_GARBAGED (f);
8794 cancel_mouse_face (f);
8796 #endif /* not USE_GTK */
8797 #endif
8799 #ifdef USE_GTK
8800 /* GTK creates windows but doesn't map them.
8801 Only get real positions when mapped. */
8802 if (FRAME_GTK_OUTER_WIDGET (f)
8803 && gtk_widget_get_mapped (FRAME_GTK_OUTER_WIDGET (f)))
8804 #endif
8806 int old_left = f->left_pos;
8807 int old_top = f->top_pos;
8808 Lisp_Object frame = Qnil;
8810 XSETFRAME (frame, f);
8812 if (!FRAME_PARENT_FRAME (f))
8813 x_real_positions (f, &f->left_pos, &f->top_pos);
8814 else
8816 Window root;
8817 unsigned int dummy_uint;
8819 block_input ();
8820 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8821 &root, &f->left_pos, &f->top_pos,
8822 &dummy_uint, &dummy_uint, &dummy_uint, &dummy_uint);
8823 unblock_input ();
8826 if (old_left != f->left_pos || old_top != f->top_pos)
8828 inev.ie.kind = MOVE_FRAME_EVENT;
8829 XSETFRAME (inev.ie.frame_or_window, f);
8834 #ifdef HAVE_X_I18N
8835 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
8836 xic_set_statusarea (f);
8837 #endif
8840 goto OTHER;
8842 case ButtonRelease:
8843 case ButtonPress:
8845 /* If we decide we want to generate an event to be seen
8846 by the rest of Emacs, we put it here. */
8847 bool tool_bar_p = false;
8849 memset (&compose_status, 0, sizeof (compose_status));
8850 dpyinfo->last_mouse_glyph_frame = NULL;
8851 x_display_set_last_user_time (dpyinfo, event->xbutton.time);
8853 if (x_mouse_grabbed (dpyinfo))
8854 f = dpyinfo->last_mouse_frame;
8855 else
8857 f = x_window_to_frame (dpyinfo, event->xbutton.window);
8859 if (f && event->xbutton.type == ButtonPress
8860 && !popup_activated ()
8861 && !x_window_to_scroll_bar (event->xbutton.display,
8862 event->xbutton.window, 2)
8863 && !FRAME_NO_ACCEPT_FOCUS (f))
8865 /* When clicking into a child frame or when clicking
8866 into a parent frame with the child frame selected and
8867 `no-accept-focus' is not set, select the clicked
8868 frame. */
8869 struct frame *hf = dpyinfo->x_highlight_frame;
8871 if (FRAME_PARENT_FRAME (f) || (hf && frame_ancestor_p (f, hf)))
8873 block_input ();
8874 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8875 RevertToParent, CurrentTime);
8876 if (FRAME_PARENT_FRAME (f))
8877 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8878 unblock_input ();
8883 #ifdef USE_GTK
8884 if (f && xg_event_is_for_scrollbar (f, event))
8885 f = 0;
8886 #endif
8887 if (f)
8889 #if ! defined (USE_GTK)
8890 /* Is this in the tool-bar? */
8891 if (WINDOWP (f->tool_bar_window)
8892 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
8894 Lisp_Object window;
8895 int x = event->xbutton.x;
8896 int y = event->xbutton.y;
8898 window = window_from_coordinates (f, x, y, 0, true);
8899 tool_bar_p = EQ (window, f->tool_bar_window);
8901 if (tool_bar_p && event->xbutton.button < 4)
8902 handle_tool_bar_click
8903 (f, x, y, event->xbutton.type == ButtonPress,
8904 x_x_to_emacs_modifiers (dpyinfo, event->xbutton.state));
8906 #endif /* !USE_GTK */
8908 if (!tool_bar_p)
8909 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8910 if (! popup_activated ())
8911 #endif
8913 if (ignore_next_mouse_click_timeout)
8915 if (event->type == ButtonPress
8916 && event->xbutton.time > ignore_next_mouse_click_timeout)
8918 ignore_next_mouse_click_timeout = 0;
8919 construct_mouse_click (&inev.ie, &event->xbutton, f);
8921 if (event->type == ButtonRelease)
8922 ignore_next_mouse_click_timeout = 0;
8924 else
8925 construct_mouse_click (&inev.ie, &event->xbutton, f);
8927 if (FRAME_X_EMBEDDED_P (f))
8928 xembed_send_message (f, event->xbutton.time,
8929 XEMBED_REQUEST_FOCUS, 0, 0, 0);
8931 else
8933 struct scroll_bar *bar
8934 = x_window_to_scroll_bar (event->xbutton.display,
8935 event->xbutton.window, 2);
8937 #ifdef USE_TOOLKIT_SCROLL_BARS
8938 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
8939 scroll bars. */
8940 if (bar && event->xbutton.state & ControlMask)
8942 x_scroll_bar_handle_click (bar, event, &inev.ie);
8943 *finish = X_EVENT_DROP;
8945 #else /* not USE_TOOLKIT_SCROLL_BARS */
8946 if (bar)
8947 x_scroll_bar_handle_click (bar, event, &inev.ie);
8948 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8951 if (event->type == ButtonPress)
8953 dpyinfo->grabbed |= (1 << event->xbutton.button);
8954 dpyinfo->last_mouse_frame = f;
8955 #if ! defined (USE_GTK)
8956 if (f && !tool_bar_p)
8957 f->last_tool_bar_item = -1;
8958 #endif /* not USE_GTK */
8960 else
8961 dpyinfo->grabbed &= ~(1 << event->xbutton.button);
8963 /* Ignore any mouse motion that happened before this event;
8964 any subsequent mouse-movement Emacs events should reflect
8965 only motion after the ButtonPress/Release. */
8966 if (f != 0)
8967 f->mouse_moved = false;
8969 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8970 f = x_menubar_window_to_frame (dpyinfo, event);
8971 /* For a down-event in the menu bar,
8972 don't pass it to Xt right now.
8973 Instead, save it away
8974 and we will pass it to Xt from kbd_buffer_get_event.
8975 That way, we can run some Lisp code first. */
8976 if (! popup_activated ()
8977 #ifdef USE_GTK
8978 /* Gtk+ menus only react to the first three buttons. */
8979 && event->xbutton.button < 3
8980 #endif
8981 && f && event->type == ButtonPress
8982 /* Verify the event is really within the menu bar
8983 and not just sent to it due to grabbing. */
8984 && event->xbutton.x >= 0
8985 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
8986 && event->xbutton.y >= 0
8987 && event->xbutton.y < FRAME_MENUBAR_HEIGHT (f)
8988 && event->xbutton.same_screen)
8990 if (!f->output_data.x->saved_menu_event)
8991 f->output_data.x->saved_menu_event = xmalloc (sizeof *event);
8992 *f->output_data.x->saved_menu_event = *event;
8993 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT;
8994 XSETFRAME (inev.ie.frame_or_window, f);
8995 *finish = X_EVENT_DROP;
8997 else
8998 goto OTHER;
8999 #endif /* USE_X_TOOLKIT || USE_GTK */
9001 break;
9003 case CirculateNotify:
9004 goto OTHER;
9006 case CirculateRequest:
9007 goto OTHER;
9009 case VisibilityNotify:
9010 goto OTHER;
9012 case MappingNotify:
9013 /* Someone has changed the keyboard mapping - update the
9014 local cache. */
9015 switch (event->xmapping.request)
9017 case MappingModifier:
9018 x_find_modifier_meanings (dpyinfo);
9019 FALLTHROUGH;
9020 case MappingKeyboard:
9021 XRefreshKeyboardMapping ((XMappingEvent *) &event->xmapping);
9023 goto OTHER;
9025 case DestroyNotify:
9026 xft_settings_event (dpyinfo, event);
9027 break;
9029 default:
9030 OTHER:
9031 #ifdef USE_X_TOOLKIT
9032 block_input ();
9033 if (*finish != X_EVENT_DROP)
9034 XtDispatchEvent ((XEvent *) event);
9035 unblock_input ();
9036 #endif /* USE_X_TOOLKIT */
9037 break;
9040 done:
9041 if (inev.ie.kind != NO_EVENT)
9043 kbd_buffer_store_buffered_event (&inev, hold_quit);
9044 count++;
9047 if (do_help
9048 && !(hold_quit && hold_quit->kind != NO_EVENT))
9050 Lisp_Object frame;
9052 if (f)
9053 XSETFRAME (frame, f);
9054 else
9055 frame = Qnil;
9057 if (do_help > 0)
9059 any_help_event_p = true;
9060 gen_help_event (help_echo_string, frame, help_echo_window,
9061 help_echo_object, help_echo_pos);
9063 else
9065 help_echo_string = Qnil;
9066 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
9068 count++;
9071 /* Sometimes event processing draws to the frame outside redisplay.
9072 To ensure that these changes become visible, draw them here. */
9073 flush_dirty_back_buffers ();
9074 SAFE_FREE ();
9075 return count;
9078 #if defined USE_X_TOOLKIT || defined USE_MOTIF || defined USE_GTK
9080 /* Handles the XEvent EVENT on display DISPLAY.
9081 This is used for event loops outside the normal event handling,
9082 i.e. looping while a popup menu or a dialog is posted.
9084 Returns the value handle_one_xevent sets in the finish argument. */
9086 x_dispatch_event (XEvent *event, Display *display)
9088 struct x_display_info *dpyinfo;
9089 int finish = X_EVENT_NORMAL;
9091 dpyinfo = x_display_info_for_display (display);
9093 if (dpyinfo)
9094 handle_one_xevent (dpyinfo, event, &finish, 0);
9096 return finish;
9098 #endif
9100 /* Read events coming from the X server.
9101 Return as soon as there are no more events to be read.
9103 Return the number of characters stored into the buffer,
9104 thus pretending to be `read' (except the characters we store
9105 in the keyboard buffer can be multibyte, so are not necessarily
9106 C chars). */
9108 static int
9109 XTread_socket (struct terminal *terminal, struct input_event *hold_quit)
9111 int count = 0;
9112 bool event_found = false;
9113 struct x_display_info *dpyinfo = terminal->display_info.x;
9115 block_input ();
9117 /* For debugging, this gives a way to fake an I/O error. */
9118 if (dpyinfo == XTread_socket_fake_io_error)
9120 XTread_socket_fake_io_error = 0;
9121 x_io_error_quitter (dpyinfo->display);
9124 #ifndef USE_GTK
9125 while (XPending (dpyinfo->display))
9127 int finish;
9128 XEvent event;
9130 XNextEvent (dpyinfo->display, &event);
9132 #ifdef HAVE_X_I18N
9133 /* Filter events for the current X input method. */
9134 if (x_filter_event (dpyinfo, &event))
9135 continue;
9136 #endif
9137 event_found = true;
9139 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
9141 if (finish == X_EVENT_GOTO_OUT)
9142 break;
9145 #else /* USE_GTK */
9147 /* For GTK we must use the GTK event loop. But XEvents gets passed
9148 to our filter function above, and then to the big event switch.
9149 We use a bunch of globals to communicate with our filter function,
9150 that is kind of ugly, but it works.
9152 There is no way to do one display at the time, GTK just does events
9153 from all displays. */
9155 while (gtk_events_pending ())
9157 current_count = count;
9158 current_hold_quit = hold_quit;
9160 gtk_main_iteration ();
9162 count = current_count;
9163 current_count = -1;
9164 current_hold_quit = 0;
9166 if (current_finish == X_EVENT_GOTO_OUT)
9167 break;
9169 #endif /* USE_GTK */
9171 /* On some systems, an X bug causes Emacs to get no more events
9172 when the window is destroyed. Detect that. (1994.) */
9173 if (! event_found)
9175 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
9176 One XNOOP in 100 loops will make Emacs terminate.
9177 B. Bretthauer, 1994 */
9178 x_noop_count++;
9179 if (x_noop_count >= 100)
9181 x_noop_count=0;
9183 if (next_noop_dpyinfo == 0)
9184 next_noop_dpyinfo = x_display_list;
9186 XNoOp (next_noop_dpyinfo->display);
9188 /* Each time we get here, cycle through the displays now open. */
9189 next_noop_dpyinfo = next_noop_dpyinfo->next;
9193 /* If the focus was just given to an auto-raising frame,
9194 raise it now. FIXME: handle more than one such frame. */
9195 if (dpyinfo->x_pending_autoraise_frame)
9197 x_raise_frame (dpyinfo->x_pending_autoraise_frame);
9198 dpyinfo->x_pending_autoraise_frame = NULL;
9201 unblock_input ();
9203 return count;
9209 /***********************************************************************
9210 Text Cursor
9211 ***********************************************************************/
9213 /* Set clipping for output in glyph row ROW. W is the window in which
9214 we operate. GC is the graphics context to set clipping in.
9216 ROW may be a text row or, e.g., a mode line. Text rows must be
9217 clipped to the interior of the window dedicated to text display,
9218 mode lines must be clipped to the whole window. */
9220 static void
9221 x_clip_to_row (struct window *w, struct glyph_row *row,
9222 enum glyph_row_area area, GC gc)
9224 struct frame *f = XFRAME (WINDOW_FRAME (w));
9225 XRectangle clip_rect;
9226 int window_x, window_y, window_width;
9228 window_box (w, area, &window_x, &window_y, &window_width, 0);
9230 clip_rect.x = window_x;
9231 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
9232 clip_rect.y = max (clip_rect.y, window_y);
9233 clip_rect.width = window_width;
9234 clip_rect.height = row->visible_height;
9236 x_set_clip_rectangles (f, gc, &clip_rect, 1);
9240 /* Draw a hollow box cursor on window W in glyph row ROW. */
9242 static void
9243 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
9245 struct frame *f = XFRAME (WINDOW_FRAME (w));
9246 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9247 Display *dpy = FRAME_X_DISPLAY (f);
9248 int x, y, wd, h;
9249 XGCValues xgcv;
9250 struct glyph *cursor_glyph;
9251 GC gc;
9253 /* Get the glyph the cursor is on. If we can't tell because
9254 the current matrix is invalid or such, give up. */
9255 cursor_glyph = get_phys_cursor_glyph (w);
9256 if (cursor_glyph == NULL)
9257 return;
9259 /* Compute frame-relative coordinates for phys cursor. */
9260 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
9261 wd = w->phys_cursor_width - 1;
9263 /* The foreground of cursor_gc is typically the same as the normal
9264 background color, which can cause the cursor box to be invisible. */
9265 xgcv.foreground = f->output_data.x->cursor_pixel;
9266 if (dpyinfo->scratch_cursor_gc)
9267 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
9268 else
9269 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_DRAWABLE (f),
9270 GCForeground, &xgcv);
9271 gc = dpyinfo->scratch_cursor_gc;
9273 /* When on R2L character, show cursor at the right edge of the
9274 glyph, unless the cursor box is as wide as the glyph or wider
9275 (the latter happens when x-stretch-cursor is non-nil). */
9276 if ((cursor_glyph->resolved_level & 1) != 0
9277 && cursor_glyph->pixel_width > wd)
9279 x += cursor_glyph->pixel_width - wd;
9280 if (wd > 0)
9281 wd -= 1;
9283 /* Set clipping, draw the rectangle, and reset clipping again. */
9284 x_clip_to_row (w, row, TEXT_AREA, gc);
9285 x_draw_rectangle (f, gc, x, y, wd, h - 1);
9286 x_reset_clip_rectangles (f, gc);
9290 /* Draw a bar cursor on window W in glyph row ROW.
9292 Implementation note: One would like to draw a bar cursor with an
9293 angle equal to the one given by the font property XA_ITALIC_ANGLE.
9294 Unfortunately, I didn't find a font yet that has this property set.
9295 --gerd. */
9297 static void
9298 x_draw_bar_cursor (struct window *w, struct glyph_row *row, int width, enum text_cursor_kinds kind)
9300 struct frame *f = XFRAME (w->frame);
9301 struct glyph *cursor_glyph;
9303 /* If cursor is out of bounds, don't draw garbage. This can happen
9304 in mini-buffer windows when switching between echo area glyphs
9305 and mini-buffer. */
9306 cursor_glyph = get_phys_cursor_glyph (w);
9307 if (cursor_glyph == NULL)
9308 return;
9310 /* Experimental avoidance of cursor on xwidget. */
9311 if (cursor_glyph->type == XWIDGET_GLYPH)
9312 return;
9314 /* If on an image, draw like a normal cursor. That's usually better
9315 visible than drawing a bar, esp. if the image is large so that
9316 the bar might not be in the window. */
9317 if (cursor_glyph->type == IMAGE_GLYPH)
9319 struct glyph_row *r;
9320 r = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
9321 draw_phys_cursor_glyph (w, r, DRAW_CURSOR);
9323 else
9325 Display *dpy = FRAME_X_DISPLAY (f);
9326 Drawable drawable = FRAME_X_DRAWABLE (f);
9327 GC gc = FRAME_DISPLAY_INFO (f)->scratch_cursor_gc;
9328 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
9329 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
9330 XGCValues xgcv;
9332 /* If the glyph's background equals the color we normally draw
9333 the bars cursor in, the bar cursor in its normal color is
9334 invisible. Use the glyph's foreground color instead in this
9335 case, on the assumption that the glyph's colors are chosen so
9336 that the glyph is legible. */
9337 if (face->background == f->output_data.x->cursor_pixel)
9338 xgcv.background = xgcv.foreground = face->foreground;
9339 else
9340 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
9341 xgcv.graphics_exposures = False;
9343 if (gc)
9344 XChangeGC (dpy, gc, mask, &xgcv);
9345 else
9347 gc = XCreateGC (dpy, drawable, mask, &xgcv);
9348 FRAME_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
9351 x_clip_to_row (w, row, TEXT_AREA, gc);
9353 if (kind == BAR_CURSOR)
9355 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9357 if (width < 0)
9358 width = FRAME_CURSOR_WIDTH (f);
9359 width = min (cursor_glyph->pixel_width, width);
9361 w->phys_cursor_width = width;
9363 /* If the character under cursor is R2L, draw the bar cursor
9364 on the right of its glyph, rather than on the left. */
9365 if ((cursor_glyph->resolved_level & 1) != 0)
9366 x += cursor_glyph->pixel_width - width;
9368 x_fill_rectangle (f, gc, x,
9369 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
9370 width, row->height);
9372 else /* HBAR_CURSOR */
9374 int dummy_x, dummy_y, dummy_h;
9375 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9377 if (width < 0)
9378 width = row->height;
9380 width = min (row->height, width);
9382 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
9383 &dummy_y, &dummy_h);
9385 if ((cursor_glyph->resolved_level & 1) != 0
9386 && cursor_glyph->pixel_width > w->phys_cursor_width - 1)
9387 x += cursor_glyph->pixel_width - w->phys_cursor_width + 1;
9388 x_fill_rectangle (f, gc, x,
9389 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
9390 row->height - width),
9391 w->phys_cursor_width - 1, width);
9394 x_reset_clip_rectangles (f, gc);
9399 /* RIF: Define cursor CURSOR on frame F. */
9401 static void
9402 x_define_frame_cursor (struct frame *f, Cursor cursor)
9404 if (!f->pointer_invisible
9405 && f->output_data.x->current_cursor != cursor)
9406 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
9407 f->output_data.x->current_cursor = cursor;
9411 /* RIF: Clear area on frame F. */
9413 static void
9414 x_clear_frame_area (struct frame *f, int x, int y, int width, int height)
9416 x_clear_area (f, x, y, width, height);
9420 /* RIF: Draw cursor on window W. */
9422 static void
9423 x_draw_window_cursor (struct window *w, struct glyph_row *glyph_row, int x,
9424 int y, enum text_cursor_kinds cursor_type,
9425 int cursor_width, bool on_p, bool active_p)
9427 struct frame *f = XFRAME (WINDOW_FRAME (w));
9429 if (on_p)
9431 w->phys_cursor_type = cursor_type;
9432 w->phys_cursor_on_p = true;
9434 if (glyph_row->exact_window_width_line_p
9435 && (glyph_row->reversed_p
9436 ? (w->phys_cursor.hpos < 0)
9437 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
9439 glyph_row->cursor_in_fringe_p = true;
9440 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
9442 else
9444 switch (cursor_type)
9446 case HOLLOW_BOX_CURSOR:
9447 x_draw_hollow_cursor (w, glyph_row);
9448 break;
9450 case FILLED_BOX_CURSOR:
9451 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
9452 break;
9454 case BAR_CURSOR:
9455 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
9456 break;
9458 case HBAR_CURSOR:
9459 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
9460 break;
9462 case NO_CURSOR:
9463 w->phys_cursor_width = 0;
9464 break;
9466 default:
9467 emacs_abort ();
9471 #ifdef HAVE_X_I18N
9472 if (w == XWINDOW (f->selected_window))
9473 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
9474 xic_set_preeditarea (w, x, y);
9475 #endif
9478 XFlush (FRAME_X_DISPLAY (f));
9482 /* Icons. */
9484 /* Make the x-window of frame F use the gnu icon bitmap. */
9486 bool
9487 x_bitmap_icon (struct frame *f, Lisp_Object file)
9489 ptrdiff_t bitmap_id;
9491 if (FRAME_X_WINDOW (f) == 0)
9492 return true;
9494 /* Free up our existing icon bitmap and mask if any. */
9495 if (f->output_data.x->icon_bitmap > 0)
9496 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
9497 f->output_data.x->icon_bitmap = 0;
9499 if (STRINGP (file))
9501 #ifdef USE_GTK
9502 /* Use gtk_window_set_icon_from_file () if available,
9503 It's not restricted to bitmaps */
9504 if (xg_set_icon (f, file))
9505 return false;
9506 #endif /* USE_GTK */
9507 bitmap_id = x_create_bitmap_from_file (f, file);
9508 x_create_bitmap_mask (f, bitmap_id);
9510 else
9512 /* Create the GNU bitmap and mask if necessary. */
9513 if (FRAME_DISPLAY_INFO (f)->icon_bitmap_id < 0)
9515 ptrdiff_t rc = -1;
9517 #ifdef USE_GTK
9519 if (xg_set_icon (f, xg_default_icon_file)
9520 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
9522 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = -2;
9523 return false;
9526 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
9528 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
9529 if (rc != -1)
9530 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
9532 #endif
9534 /* If all else fails, use the (black and white) xbm image. */
9535 if (rc == -1)
9537 rc = x_create_bitmap_from_data (f, (char *) gnu_xbm_bits,
9538 gnu_xbm_width, gnu_xbm_height);
9539 if (rc == -1)
9540 return true;
9542 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
9543 x_create_bitmap_mask (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
9547 /* The first time we create the GNU bitmap and mask,
9548 this increments the ref-count one extra time.
9549 As a result, the GNU bitmap and mask are never freed.
9550 That way, we don't have to worry about allocating it again. */
9551 x_reference_bitmap (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
9553 bitmap_id = FRAME_DISPLAY_INFO (f)->icon_bitmap_id;
9556 x_wm_set_icon_pixmap (f, bitmap_id);
9557 f->output_data.x->icon_bitmap = bitmap_id;
9559 return false;
9563 /* Make the x-window of frame F use a rectangle with text.
9564 Use ICON_NAME as the text. */
9566 bool
9567 x_text_icon (struct frame *f, const char *icon_name)
9569 if (FRAME_X_WINDOW (f) == 0)
9570 return true;
9573 XTextProperty text;
9574 text.value = (unsigned char *) icon_name;
9575 text.encoding = XA_STRING;
9576 text.format = 8;
9577 text.nitems = strlen (icon_name);
9578 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
9581 if (f->output_data.x->icon_bitmap > 0)
9582 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
9583 f->output_data.x->icon_bitmap = 0;
9584 x_wm_set_icon_pixmap (f, 0);
9586 return false;
9589 #define X_ERROR_MESSAGE_SIZE 200
9591 /* If non-nil, this should be a string.
9592 It means catch X errors and store the error message in this string.
9594 The reason we use a stack is that x_catch_error/x_uncatch_error can
9595 be called from a signal handler.
9598 struct x_error_message_stack {
9599 char string[X_ERROR_MESSAGE_SIZE];
9600 Display *dpy;
9601 x_special_error_handler handler;
9602 void *handler_data;
9603 struct x_error_message_stack *prev;
9605 static struct x_error_message_stack *x_error_message;
9607 /* An X error handler which stores the error message in
9608 *x_error_message. This is called from x_error_handler if
9609 x_catch_errors is in effect. */
9611 static void
9612 x_error_catcher (Display *display, XErrorEvent *event)
9614 XGetErrorText (display, event->error_code,
9615 x_error_message->string,
9616 X_ERROR_MESSAGE_SIZE);
9617 if (x_error_message->handler)
9618 x_error_message->handler (display, event, x_error_message->string,
9619 x_error_message->handler_data);
9622 /* Begin trapping X errors for display DPY. Actually we trap X errors
9623 for all displays, but DPY should be the display you are actually
9624 operating on.
9626 After calling this function, X protocol errors no longer cause
9627 Emacs to exit; instead, they are recorded in the string
9628 stored in *x_error_message.
9630 Calling x_check_errors signals an Emacs error if an X error has
9631 occurred since the last call to x_catch_errors or x_check_errors.
9633 Calling x_uncatch_errors resumes the normal error handling.
9634 Calling x_uncatch_errors_after_check is similar, but skips an XSync
9635 to the server, and should be used only immediately after
9636 x_had_errors_p or x_check_errors. */
9638 void
9639 x_catch_errors_with_handler (Display *dpy, x_special_error_handler handler,
9640 void *handler_data)
9642 struct x_error_message_stack *data = xmalloc (sizeof *data);
9644 /* Make sure any errors from previous requests have been dealt with. */
9645 XSync (dpy, False);
9647 data->dpy = dpy;
9648 data->string[0] = 0;
9649 data->handler = handler;
9650 data->handler_data = handler_data;
9651 data->prev = x_error_message;
9652 x_error_message = data;
9655 void
9656 x_catch_errors (Display *dpy)
9658 x_catch_errors_with_handler (dpy, NULL, NULL);
9661 /* Undo the last x_catch_errors call.
9662 DPY should be the display that was passed to x_catch_errors.
9664 This version should be used only if the immediately preceding
9665 X-protocol-related thing was x_check_errors or x_had_error_p, both
9666 of which issue XSync calls, so we don't need to re-sync here. */
9668 void
9669 x_uncatch_errors_after_check (void)
9671 struct x_error_message_stack *tmp;
9673 block_input ();
9674 tmp = x_error_message;
9675 x_error_message = x_error_message->prev;
9676 xfree (tmp);
9677 unblock_input ();
9680 /* Undo the last x_catch_errors call.
9681 DPY should be the display that was passed to x_catch_errors. */
9683 void
9684 x_uncatch_errors (void)
9686 struct x_error_message_stack *tmp;
9688 block_input ();
9690 /* The display may have been closed before this function is called.
9691 Check if it is still open before calling XSync. */
9692 if (x_display_info_for_display (x_error_message->dpy) != 0)
9693 XSync (x_error_message->dpy, False);
9695 tmp = x_error_message;
9696 x_error_message = x_error_message->prev;
9697 xfree (tmp);
9698 unblock_input ();
9701 /* If any X protocol errors have arrived since the last call to
9702 x_catch_errors or x_check_errors, signal an Emacs error using
9703 sprintf (a buffer, FORMAT, the x error message text) as the text. */
9705 void
9706 x_check_errors (Display *dpy, const char *format)
9708 /* Make sure to catch any errors incurred so far. */
9709 XSync (dpy, False);
9711 if (x_error_message->string[0])
9713 char string[X_ERROR_MESSAGE_SIZE];
9714 memcpy (string, x_error_message->string, X_ERROR_MESSAGE_SIZE);
9715 x_uncatch_errors ();
9716 error (format, string);
9720 /* Nonzero if we had any X protocol errors
9721 since we did x_catch_errors on DPY. */
9723 bool
9724 x_had_errors_p (Display *dpy)
9726 /* Make sure to catch any errors incurred so far. */
9727 XSync (dpy, False);
9729 return x_error_message->string[0] != 0;
9732 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
9734 void
9735 x_clear_errors (Display *dpy)
9737 x_error_message->string[0] = 0;
9740 #if false
9741 /* See comment in unwind_to_catch why calling this is a bad
9742 * idea. --lorentey */
9743 /* Close off all unclosed x_catch_errors calls. */
9745 void
9746 x_fully_uncatch_errors (void)
9748 while (x_error_message)
9749 x_uncatch_errors ();
9751 #endif
9753 #if false
9754 static unsigned int x_wire_count;
9755 x_trace_wire (void)
9757 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
9759 #endif
9762 /************************************************************************
9763 Handling X errors
9764 ************************************************************************/
9766 /* Error message passed to x_connection_closed. */
9768 static char *error_msg;
9770 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
9771 the text of an error message that lead to the connection loss. */
9773 static _Noreturn void
9774 x_connection_closed (Display *dpy, const char *error_message, bool ioerror)
9776 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
9777 Lisp_Object frame, tail;
9778 ptrdiff_t idx = SPECPDL_INDEX ();
9780 error_msg = alloca (strlen (error_message) + 1);
9781 strcpy (error_msg, error_message);
9783 /* Inhibit redisplay while frames are being deleted. */
9784 specbind (Qinhibit_redisplay, Qt);
9786 if (dpyinfo)
9788 /* Protect display from being closed when we delete the last
9789 frame on it. */
9790 dpyinfo->reference_count++;
9791 dpyinfo->terminal->reference_count++;
9793 if (ioerror) dpyinfo->display = 0;
9795 /* First delete frames whose mini-buffers are on frames
9796 that are on the dead display. */
9797 FOR_EACH_FRAME (tail, frame)
9799 Lisp_Object minibuf_frame;
9800 minibuf_frame
9801 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
9802 if (FRAME_X_P (XFRAME (frame))
9803 && FRAME_X_P (XFRAME (minibuf_frame))
9804 && ! EQ (frame, minibuf_frame)
9805 && FRAME_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
9806 delete_frame (frame, Qnoelisp);
9809 /* Now delete all remaining frames on the dead display.
9810 We are now sure none of these is used as the mini-buffer
9811 for another frame that we need to delete. */
9812 FOR_EACH_FRAME (tail, frame)
9813 if (FRAME_X_P (XFRAME (frame))
9814 && FRAME_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
9816 /* Set this to t so that delete_frame won't get confused
9817 trying to find a replacement. */
9818 kset_default_minibuffer_frame (FRAME_KBOARD (XFRAME (frame)), Qt);
9819 delete_frame (frame, Qnoelisp);
9822 /* If DPYINFO is null, this means we didn't open the display in the
9823 first place, so don't try to close it. */
9824 if (dpyinfo)
9826 /* We can not call XtCloseDisplay here because it calls XSync.
9827 XSync inside the error handler apparently hangs Emacs. On
9828 current Xt versions, this isn't needed either. */
9829 #ifdef USE_GTK
9830 /* A long-standing GTK bug prevents proper disconnect handling
9831 (https://bugzilla.gnome.org/show_bug.cgi?id=85715). Once,
9832 the resulting Glib error message loop filled a user's disk.
9833 To avoid this, kill Emacs unconditionally on disconnect. */
9834 shut_down_emacs (0, Qnil);
9835 fprintf (stderr, "%s\n\
9836 When compiled with GTK, Emacs cannot recover from X disconnects.\n\
9837 This is a GTK bug: https://bugzilla.gnome.org/show_bug.cgi?id=85715\n\
9838 For details, see etc/PROBLEMS.\n",
9839 error_msg);
9840 emacs_abort ();
9841 #endif /* USE_GTK */
9843 /* Indicate that this display is dead. */
9844 dpyinfo->display = 0;
9846 dpyinfo->reference_count--;
9847 dpyinfo->terminal->reference_count--;
9848 if (dpyinfo->reference_count != 0)
9849 /* We have just closed all frames on this display. */
9850 emacs_abort ();
9853 Lisp_Object tmp;
9854 XSETTERMINAL (tmp, dpyinfo->terminal);
9855 Fdelete_terminal (tmp, Qnoelisp);
9859 if (terminal_list == 0)
9861 fprintf (stderr, "%s\n", error_msg);
9862 Fkill_emacs (make_number (70));
9863 /* NOTREACHED */
9866 totally_unblock_input ();
9868 unbind_to (idx, Qnil);
9869 clear_waiting_for_input ();
9871 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
9872 longjmp), because returning from this function would get us back into
9873 Xlib's code which will directly call `exit'. */
9874 error ("%s", error_msg);
9877 /* We specifically use it before defining it, so that gcc doesn't inline it,
9878 otherwise gdb doesn't know how to properly put a breakpoint on it. */
9879 static void x_error_quitter (Display *, XErrorEvent *);
9881 /* This is the first-level handler for X protocol errors.
9882 It calls x_error_quitter or x_error_catcher. */
9884 static int
9885 x_error_handler (Display *display, XErrorEvent *event)
9887 #if defined USE_GTK && defined HAVE_GTK3
9888 if ((event->error_code == BadMatch || event->error_code == BadWindow)
9889 && event->request_code == X_SetInputFocus)
9891 return 0;
9893 #endif
9895 if (x_error_message)
9896 x_error_catcher (display, event);
9897 else
9898 x_error_quitter (display, event);
9899 return 0;
9902 /* This is the usual handler for X protocol errors.
9903 It kills all frames on the display that we got the error for.
9904 If that was the only one, it prints an error message and kills Emacs. */
9906 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
9908 /* On older GCC versions, just putting x_error_quitter
9909 after x_error_handler prevents inlining into the former. */
9911 static void NO_INLINE
9912 x_error_quitter (Display *display, XErrorEvent *event)
9914 char buf[256], buf1[356];
9916 /* Ignore BadName errors. They can happen because of fonts
9917 or colors that are not defined. */
9919 if (event->error_code == BadName)
9920 return;
9922 /* Note that there is no real way portable across R3/R4 to get the
9923 original error handler. */
9925 XGetErrorText (display, event->error_code, buf, sizeof (buf));
9926 sprintf (buf1, "X protocol error: %s on protocol request %d",
9927 buf, event->request_code);
9928 x_connection_closed (display, buf1, false);
9932 /* This is the handler for X IO errors, always.
9933 It kills all frames on the display that we lost touch with.
9934 If that was the only one, it prints an error message and kills Emacs. */
9936 static _Noreturn int
9937 x_io_error_quitter (Display *display)
9939 char buf[256];
9941 snprintf (buf, sizeof buf, "Connection lost to X server '%s'",
9942 DisplayString (display));
9943 x_connection_closed (display, buf, true);
9944 assume (false);
9947 /* Changing the font of the frame. */
9949 /* Give frame F the font FONT-OBJECT as its default font. The return
9950 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
9951 frame. If it is negative, generate a new fontset from
9952 FONT-OBJECT. */
9954 Lisp_Object
9955 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
9957 struct font *font = XFONT_OBJECT (font_object);
9958 int unit, font_ascent, font_descent;
9959 #ifndef USE_X_TOOLKIT
9960 int old_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
9961 Lisp_Object fullscreen;
9962 #endif
9964 if (fontset < 0)
9965 fontset = fontset_from_font (font_object);
9966 FRAME_FONTSET (f) = fontset;
9967 if (FRAME_FONT (f) == font)
9968 /* This font is already set in frame F. There's nothing more to
9969 do. */
9970 return font_object;
9972 FRAME_FONT (f) = font;
9973 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
9974 FRAME_COLUMN_WIDTH (f) = font->average_width;
9975 get_font_ascent_descent (font, &font_ascent, &font_descent);
9976 FRAME_LINE_HEIGHT (f) = font_ascent + font_descent;
9978 #ifndef USE_X_TOOLKIT
9979 FRAME_MENU_BAR_HEIGHT (f) = FRAME_MENU_BAR_LINES (f) * FRAME_LINE_HEIGHT (f);
9980 #endif
9982 /* Compute character columns occupied by scrollbar.
9984 Don't do things differently for non-toolkit scrollbars
9985 (Bug#17163). */
9986 unit = FRAME_COLUMN_WIDTH (f);
9987 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
9988 FRAME_CONFIG_SCROLL_BAR_COLS (f)
9989 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + unit - 1) / unit;
9990 else
9991 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
9993 if (FRAME_X_WINDOW (f) != 0)
9995 /* Don't change the size of a tip frame; there's no point in
9996 doing it because it's done in Fx_show_tip, and it leads to
9997 problems because the tip frame has no widget. */
9998 if (!FRAME_TOOLTIP_P (f))
10000 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
10001 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 3,
10002 false, Qfont);
10003 #ifndef USE_X_TOOLKIT
10004 if (FRAME_MENU_BAR_HEIGHT (f) != old_menu_bar_height
10005 && !f->after_make_frame
10006 && (EQ (frame_inhibit_implied_resize, Qt)
10007 || (CONSP (frame_inhibit_implied_resize)
10008 && NILP (Fmemq (Qfont, frame_inhibit_implied_resize))))
10009 && (NILP (fullscreen = get_frame_param (f, Qfullscreen))
10010 || EQ (fullscreen, Qfullwidth)))
10011 /* If the menu bar height changes, try to keep text height
10012 constant. */
10013 adjust_frame_size
10014 (f, -1, FRAME_TEXT_HEIGHT (f) + FRAME_MENU_BAR_HEIGHT (f)
10015 - old_menu_bar_height, 1, false, Qfont);
10016 #endif /* USE_X_TOOLKIT */
10020 #ifdef HAVE_X_I18N
10021 if (FRAME_XIC (f)
10022 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
10024 block_input ();
10025 xic_set_xfontset (f, SSDATA (fontset_ascii (fontset)));
10026 unblock_input ();
10028 #endif
10030 return font_object;
10034 /***********************************************************************
10035 X Input Methods
10036 ***********************************************************************/
10038 #ifdef HAVE_X_I18N
10040 #ifdef HAVE_X11R6
10042 /* XIM destroy callback function, which is called whenever the
10043 connection to input method XIM dies. CLIENT_DATA contains a
10044 pointer to the x_display_info structure corresponding to XIM. */
10046 static void
10047 xim_destroy_callback (XIM xim, XPointer client_data, XPointer call_data)
10049 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
10050 Lisp_Object frame, tail;
10052 block_input ();
10054 /* No need to call XDestroyIC.. */
10055 FOR_EACH_FRAME (tail, frame)
10057 struct frame *f = XFRAME (frame);
10058 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
10060 FRAME_XIC (f) = NULL;
10061 xic_free_xfontset (f);
10065 /* No need to call XCloseIM. */
10066 dpyinfo->xim = NULL;
10067 XFree (dpyinfo->xim_styles);
10068 unblock_input ();
10071 #endif /* HAVE_X11R6 */
10073 /* Open the connection to the XIM server on display DPYINFO.
10074 RESOURCE_NAME is the resource name Emacs uses. */
10076 static void
10077 xim_open_dpy (struct x_display_info *dpyinfo, char *resource_name)
10079 XIM xim;
10081 #ifdef HAVE_XIM
10082 if (use_xim)
10084 if (dpyinfo->xim)
10085 XCloseIM (dpyinfo->xim);
10086 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
10087 emacs_class);
10088 dpyinfo->xim = xim;
10090 if (xim)
10092 #ifdef HAVE_X11R6
10093 XIMCallback destroy;
10094 #endif
10096 /* Get supported styles and XIM values. */
10097 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
10099 #ifdef HAVE_X11R6
10100 destroy.callback = xim_destroy_callback;
10101 destroy.client_data = (XPointer)dpyinfo;
10102 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
10103 #endif
10107 else
10108 #endif /* HAVE_XIM */
10109 dpyinfo->xim = NULL;
10113 #ifdef HAVE_X11R6_XIM
10115 /* XIM instantiate callback function, which is called whenever an XIM
10116 server is available. DISPLAY is the display of the XIM.
10117 CLIENT_DATA contains a pointer to an xim_inst_t structure created
10118 when the callback was registered. */
10120 static void
10121 xim_instantiate_callback (Display *display, XPointer client_data, XPointer call_data)
10123 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
10124 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
10126 /* We don't support multiple XIM connections. */
10127 if (dpyinfo->xim)
10128 return;
10130 xim_open_dpy (dpyinfo, xim_inst->resource_name);
10132 /* Create XIC for the existing frames on the same display, as long
10133 as they have no XIC. */
10134 if (dpyinfo->xim && dpyinfo->reference_count > 0)
10136 Lisp_Object tail, frame;
10138 block_input ();
10139 FOR_EACH_FRAME (tail, frame)
10141 struct frame *f = XFRAME (frame);
10143 if (FRAME_X_P (f)
10144 && FRAME_DISPLAY_INFO (f) == xim_inst->dpyinfo)
10145 if (FRAME_XIC (f) == NULL)
10147 create_frame_xic (f);
10148 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
10149 xic_set_statusarea (f);
10150 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
10152 struct window *w = XWINDOW (f->selected_window);
10153 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
10158 unblock_input ();
10162 #endif /* HAVE_X11R6_XIM */
10165 /* Open a connection to the XIM server on display DPYINFO.
10166 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
10167 connection only at the first time. On X11R6, open the connection
10168 in the XIM instantiate callback function. */
10170 static void
10171 xim_initialize (struct x_display_info *dpyinfo, char *resource_name)
10173 dpyinfo->xim = NULL;
10174 #ifdef HAVE_XIM
10175 if (use_xim)
10177 #ifdef HAVE_X11R6_XIM
10178 struct xim_inst_t *xim_inst = xmalloc (sizeof *xim_inst);
10179 Bool ret;
10181 dpyinfo->xim_callback_data = xim_inst;
10182 xim_inst->dpyinfo = dpyinfo;
10183 xim_inst->resource_name = xstrdup (resource_name);
10184 ret = XRegisterIMInstantiateCallback
10185 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
10186 emacs_class, xim_instantiate_callback,
10187 /* This is XPointer in XFree86 but (XPointer *)
10188 on Tru64, at least, hence the configure test. */
10189 (XRegisterIMInstantiateCallback_arg6) xim_inst);
10190 eassert (ret == True);
10191 #else /* not HAVE_X11R6_XIM */
10192 xim_open_dpy (dpyinfo, resource_name);
10193 #endif /* not HAVE_X11R6_XIM */
10195 #endif /* HAVE_XIM */
10199 /* Close the connection to the XIM server on display DPYINFO. */
10201 static void
10202 xim_close_dpy (struct x_display_info *dpyinfo)
10204 #ifdef HAVE_XIM
10205 if (use_xim)
10207 #ifdef HAVE_X11R6_XIM
10208 struct xim_inst_t *xim_inst = dpyinfo->xim_callback_data;
10210 if (dpyinfo->display)
10212 Bool ret = XUnregisterIMInstantiateCallback
10213 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
10214 emacs_class, xim_instantiate_callback,
10215 (XRegisterIMInstantiateCallback_arg6) xim_inst);
10216 eassert (ret == True);
10218 xfree (xim_inst->resource_name);
10219 xfree (xim_inst);
10220 #endif /* HAVE_X11R6_XIM */
10221 if (dpyinfo->display)
10222 XCloseIM (dpyinfo->xim);
10223 dpyinfo->xim = NULL;
10224 XFree (dpyinfo->xim_styles);
10226 #endif /* HAVE_XIM */
10229 #endif /* not HAVE_X11R6_XIM */
10233 /* Calculate the absolute position in frame F
10234 from its current recorded position values and gravity. */
10236 static void
10237 x_calc_absolute_position (struct frame *f)
10239 int flags = f->size_hint_flags;
10240 struct frame *p = FRAME_PARENT_FRAME (f);
10242 /* We have nothing to do if the current position
10243 is already for the top-left corner. */
10244 if (! ((flags & XNegative) || (flags & YNegative)))
10245 return;
10247 /* Treat negative positions as relative to the leftmost bottommost
10248 position that fits on the screen. */
10249 if ((flags & XNegative) && (f->left_pos <= 0))
10251 int width = FRAME_PIXEL_WIDTH (f);
10253 /* A frame that has been visible at least once should have outer
10254 edges. */
10255 if (f->output_data.x->has_been_visible && !p)
10257 Lisp_Object frame;
10258 Lisp_Object edges = Qnil;
10260 XSETFRAME (frame, f);
10261 edges = Fx_frame_edges (frame, Qouter_edges);
10262 if (!NILP (edges))
10263 width = (XINT (Fnth (make_number (2), edges))
10264 - XINT (Fnth (make_number (0), edges)));
10267 if (p)
10268 f->left_pos = (FRAME_PIXEL_WIDTH (p) - width - 2 * f->border_width
10269 + f->left_pos);
10270 else
10271 f->left_pos = (x_display_pixel_width (FRAME_DISPLAY_INFO (f))
10272 - width + f->left_pos);
10276 if ((flags & YNegative) && (f->top_pos <= 0))
10278 int height = FRAME_PIXEL_HEIGHT (f);
10280 #if defined USE_X_TOOLKIT && defined USE_MOTIF
10281 /* Something is fishy here. When using Motif, starting Emacs with
10282 `-g -0-0', the frame appears too low by a few pixels.
10284 This seems to be so because initially, while Emacs is starting,
10285 the column widget's height and the frame's pixel height are
10286 different. The column widget's height is the right one. In
10287 later invocations, when Emacs is up, the frame's pixel height
10288 is right, though.
10290 It's not obvious where the initial small difference comes from.
10291 2000-12-01, gerd. */
10293 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
10294 #endif
10296 if (f->output_data.x->has_been_visible && !p)
10298 Lisp_Object frame;
10299 Lisp_Object edges = Qnil;
10301 XSETFRAME (frame, f);
10302 if (NILP (edges))
10303 edges = Fx_frame_edges (frame, Qouter_edges);
10304 if (!NILP (edges))
10305 height = (XINT (Fnth (make_number (3), edges))
10306 - XINT (Fnth (make_number (1), edges)));
10309 if (p)
10310 f->top_pos = (FRAME_PIXEL_HEIGHT (p) - height - 2 * f->border_width
10311 + f->top_pos);
10312 else
10313 f->top_pos = (x_display_pixel_height (FRAME_DISPLAY_INFO (f))
10314 - height + f->top_pos);
10317 /* The left_pos and top_pos
10318 are now relative to the top and left screen edges,
10319 so the flags should correspond. */
10320 f->size_hint_flags &= ~ (XNegative | YNegative);
10323 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
10324 to really change the position, and 0 when calling from
10325 x_make_frame_visible (in that case, XOFF and YOFF are the current
10326 position values). It is -1 when calling from x_set_frame_parameters,
10327 which means, do adjust for borders but don't change the gravity. */
10329 void
10330 x_set_offset (struct frame *f, register int xoff, register int yoff, int change_gravity)
10332 int modified_top, modified_left;
10333 #ifdef USE_GTK
10334 int scale = xg_get_scale (f);
10335 #endif
10337 if (change_gravity > 0)
10339 f->top_pos = yoff;
10340 f->left_pos = xoff;
10341 f->size_hint_flags &= ~ (XNegative | YNegative);
10342 if (xoff < 0)
10343 f->size_hint_flags |= XNegative;
10344 if (yoff < 0)
10345 f->size_hint_flags |= YNegative;
10346 f->win_gravity = NorthWestGravity;
10349 x_calc_absolute_position (f);
10351 block_input ();
10352 x_wm_set_size_hint (f, 0, false);
10354 #ifdef USE_GTK
10355 if (x_gtk_use_window_move)
10357 /* When a position change was requested and the outer GTK widget
10358 has been realized already, leave it to gtk_window_move to
10359 DTRT and return. Used for Bug#25851 and Bug#25943. Convert
10360 from X pixels to GTK scaled pixels. */
10361 if (change_gravity != 0 && FRAME_GTK_OUTER_WIDGET (f))
10362 gtk_window_move (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
10363 f->left_pos / scale, f->top_pos / scale);
10364 unblock_input ();
10365 return;
10367 #endif /* USE_GTK */
10369 modified_left = f->left_pos;
10370 modified_top = f->top_pos;
10372 if (change_gravity != 0 && FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
10374 /* Some WMs (twm, wmaker at least) has an offset that is smaller
10375 than the WM decorations. So we use the calculated offset instead
10376 of the WM decoration sizes here (x/y_pixels_outer_diff). */
10377 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
10378 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
10381 #ifdef USE_GTK
10382 /* Make sure we adjust for possible scaling. */
10383 gtk_window_move (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
10384 modified_left / scale, modified_top / scale);
10385 #else
10386 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10387 modified_left, modified_top);
10388 #endif
10390 x_sync_with_move (f, f->left_pos, f->top_pos,
10391 FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN);
10393 /* change_gravity is non-zero when this function is called from Lisp to
10394 programmatically move a frame. In that case, we call
10395 x_check_expected_move to discover if we have a "Type A" or "Type B"
10396 window manager, and, for a "Type A" window manager, adjust the position
10397 of the frame.
10399 We call x_check_expected_move if a programmatic move occurred, and
10400 either the window manager type (A/B) is unknown or it is Type A but we
10401 need to compute the top/left offset adjustment for this frame. */
10403 if (change_gravity != 0
10404 && !FRAME_PARENT_FRAME (f)
10405 && (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
10406 || (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
10407 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
10408 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
10409 x_check_expected_move (f, modified_left, modified_top);
10411 unblock_input ();
10414 /* Return true if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
10415 on the root window for frame F contains ATOMNAME.
10416 This is how a WM check shall be done according to the Window Manager
10417 Specification/Extended Window Manager Hints at
10418 http://freedesktop.org/wiki/Specifications/wm-spec. */
10420 bool
10421 x_wm_supports (struct frame *f, Atom want_atom)
10423 Atom actual_type;
10424 unsigned long actual_size, bytes_remaining;
10425 int i, rc, actual_format;
10426 bool ret;
10427 Window wmcheck_window;
10428 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10429 Window target_window = dpyinfo->root_window;
10430 int max_len = 65536;
10431 Display *dpy = FRAME_X_DISPLAY (f);
10432 unsigned char *tmp_data = NULL;
10433 Atom target_type = XA_WINDOW;
10435 block_input ();
10437 x_catch_errors (dpy);
10438 rc = XGetWindowProperty (dpy, target_window,
10439 dpyinfo->Xatom_net_supporting_wm_check,
10440 0, max_len, False, target_type,
10441 &actual_type, &actual_format, &actual_size,
10442 &bytes_remaining, &tmp_data);
10444 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
10446 if (tmp_data) XFree (tmp_data);
10447 x_uncatch_errors ();
10448 unblock_input ();
10449 return false;
10452 wmcheck_window = *(Window *) tmp_data;
10453 XFree (tmp_data);
10455 /* Check if window exists. */
10456 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
10457 if (x_had_errors_p (dpy))
10459 x_uncatch_errors_after_check ();
10460 unblock_input ();
10461 return false;
10464 if (dpyinfo->net_supported_window != wmcheck_window)
10466 /* Window changed, reload atoms */
10467 if (dpyinfo->net_supported_atoms != NULL)
10468 XFree (dpyinfo->net_supported_atoms);
10469 dpyinfo->net_supported_atoms = NULL;
10470 dpyinfo->nr_net_supported_atoms = 0;
10471 dpyinfo->net_supported_window = 0;
10473 target_type = XA_ATOM;
10474 tmp_data = NULL;
10475 rc = XGetWindowProperty (dpy, target_window,
10476 dpyinfo->Xatom_net_supported,
10477 0, max_len, False, target_type,
10478 &actual_type, &actual_format, &actual_size,
10479 &bytes_remaining, &tmp_data);
10481 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
10483 if (tmp_data) XFree (tmp_data);
10484 x_uncatch_errors ();
10485 unblock_input ();
10486 return false;
10489 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
10490 dpyinfo->nr_net_supported_atoms = actual_size;
10491 dpyinfo->net_supported_window = wmcheck_window;
10494 ret = false;
10496 for (i = 0; !ret && i < dpyinfo->nr_net_supported_atoms; ++i)
10497 ret = dpyinfo->net_supported_atoms[i] == want_atom;
10499 x_uncatch_errors ();
10500 unblock_input ();
10502 return ret;
10505 static void
10506 set_wm_state (Lisp_Object frame, bool add, Atom atom, Atom value)
10508 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (XFRAME (frame));
10510 x_send_client_event (frame, make_number (0), frame,
10511 dpyinfo->Xatom_net_wm_state,
10512 make_number (32),
10513 /* 1 = add, 0 = remove */
10514 Fcons
10515 (make_number (add),
10516 Fcons
10517 (make_fixnum_or_float (atom),
10518 (value != 0
10519 ? list1 (make_fixnum_or_float (value))
10520 : Qnil))));
10523 void
10524 x_set_sticky (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
10526 Lisp_Object frame;
10527 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10529 XSETFRAME (frame, f);
10531 set_wm_state (frame, !NILP (new_value),
10532 dpyinfo->Xatom_net_wm_state_sticky, None);
10536 * x_set_skip_taskbar:
10538 * Set frame F's `skip-taskbar' parameter. If non-nil, this should
10539 * remove F's icon from the taskbar associated with the display of F's
10540 * window-system window and inhibit switching to F's window via
10541 * <Alt>-<TAB>. If nil, lift these restrictions.
10543 * Some window managers may not honor this parameter.
10545 void
10546 x_set_skip_taskbar (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
10548 if (!EQ (new_value, old_value))
10550 #ifdef USE_GTK
10551 xg_set_skip_taskbar (f, new_value);
10552 #else
10553 Lisp_Object frame;
10554 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10556 XSETFRAME (frame, f);
10557 set_wm_state (frame, !NILP (new_value),
10558 dpyinfo->Xatom_net_wm_state_skip_taskbar, None);
10559 #endif /* USE_GTK */
10560 FRAME_SKIP_TASKBAR (f) = !NILP (new_value);
10565 * x_set_z_group:
10567 * Set frame F's `z-group' parameter. If `above', F's window-system
10568 * window is displayed above all windows that do not have the `above'
10569 * property set. If nil, F's window is shown below all windows that
10570 * have the `above' property set and above all windows that have the
10571 * `below' property set. If `below', F's window is displayed below all
10572 * windows that do not have the `below' property set.
10574 * Some window managers may not honor this parameter.
10576 void
10577 x_set_z_group (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
10579 /* We don't care about old_value. The window manager might have
10580 reset the value without telling us. */
10581 Lisp_Object frame;
10582 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10584 XSETFRAME (frame, f);
10586 if (NILP (new_value))
10588 set_wm_state (frame, false,
10589 dpyinfo->Xatom_net_wm_state_above, None);
10590 set_wm_state (frame, false,
10591 dpyinfo->Xatom_net_wm_state_below, None);
10592 FRAME_Z_GROUP (f) = z_group_none;
10594 else if (EQ (new_value, Qabove))
10596 set_wm_state (frame, true,
10597 dpyinfo->Xatom_net_wm_state_above, None);
10598 set_wm_state (frame, false,
10599 dpyinfo->Xatom_net_wm_state_below, None);
10600 FRAME_Z_GROUP (f) = z_group_above;
10602 else if (EQ (new_value, Qbelow))
10604 set_wm_state (frame, false,
10605 dpyinfo->Xatom_net_wm_state_above, None);
10606 set_wm_state (frame, true,
10607 dpyinfo->Xatom_net_wm_state_below, None);
10608 FRAME_Z_GROUP (f) = z_group_below;
10610 else if (EQ (new_value, Qabove_suspended))
10612 set_wm_state (frame, false,
10613 dpyinfo->Xatom_net_wm_state_above, None);
10614 FRAME_Z_GROUP (f) = z_group_above_suspended;
10616 else
10617 error ("Invalid z-group specification");
10621 /* Return the current _NET_WM_STATE.
10622 SIZE_STATE is set to one of the FULLSCREEN_* values.
10623 Set *STICKY to the sticky state.
10625 Return true iff we are not hidden. */
10627 static bool
10628 get_current_wm_state (struct frame *f,
10629 Window window,
10630 int *size_state,
10631 bool *sticky)
10633 unsigned long actual_size;
10634 int i;
10635 bool is_hidden = false;
10636 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10637 long max_len = 65536;
10638 Atom target_type = XA_ATOM;
10639 /* If XCB is available, we can avoid three XSync calls. */
10640 #ifdef USE_XCB
10641 xcb_get_property_cookie_t prop_cookie;
10642 xcb_get_property_reply_t *prop;
10643 xcb_atom_t *reply_data;
10644 #else
10645 Display *dpy = FRAME_X_DISPLAY (f);
10646 unsigned long bytes_remaining;
10647 int rc, actual_format;
10648 Atom actual_type;
10649 unsigned char *tmp_data = NULL;
10650 Atom *reply_data;
10651 #endif
10653 *sticky = false;
10654 *size_state = FULLSCREEN_NONE;
10656 block_input ();
10658 #ifdef USE_XCB
10659 prop_cookie = xcb_get_property (dpyinfo->xcb_connection, 0, window,
10660 dpyinfo->Xatom_net_wm_state,
10661 target_type, 0, max_len);
10662 prop = xcb_get_property_reply (dpyinfo->xcb_connection, prop_cookie, NULL);
10663 if (prop && prop->type == target_type)
10665 int actual_bytes = xcb_get_property_value_length (prop);
10666 eassume (0 <= actual_bytes);
10667 actual_size = actual_bytes / sizeof *reply_data;
10668 reply_data = xcb_get_property_value (prop);
10670 else
10672 actual_size = 0;
10673 is_hidden = FRAME_ICONIFIED_P (f);
10675 #else
10676 x_catch_errors (dpy);
10677 rc = XGetWindowProperty (dpy, window, dpyinfo->Xatom_net_wm_state,
10678 0, max_len, False, target_type,
10679 &actual_type, &actual_format, &actual_size,
10680 &bytes_remaining, &tmp_data);
10682 if (rc == Success && actual_type == target_type && ! x_had_errors_p (dpy))
10683 reply_data = (Atom *) tmp_data;
10684 else
10686 actual_size = 0;
10687 is_hidden = FRAME_ICONIFIED_P (f);
10690 x_uncatch_errors ();
10691 #endif
10693 for (i = 0; i < actual_size; ++i)
10695 Atom a = reply_data[i];
10696 if (a == dpyinfo->Xatom_net_wm_state_hidden)
10697 is_hidden = true;
10698 else if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
10700 if (*size_state == FULLSCREEN_HEIGHT)
10701 *size_state = FULLSCREEN_MAXIMIZED;
10702 else
10703 *size_state = FULLSCREEN_WIDTH;
10705 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
10707 if (*size_state == FULLSCREEN_WIDTH)
10708 *size_state = FULLSCREEN_MAXIMIZED;
10709 else
10710 *size_state = FULLSCREEN_HEIGHT;
10712 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen)
10713 *size_state = FULLSCREEN_BOTH;
10714 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
10715 *sticky = true;
10718 #ifdef USE_XCB
10719 free (prop);
10720 #else
10721 if (tmp_data) XFree (tmp_data);
10722 #endif
10724 unblock_input ();
10725 return ! is_hidden;
10728 /* Do fullscreen as specified in extended window manager hints */
10730 static bool
10731 do_ewmh_fullscreen (struct frame *f)
10733 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10734 bool have_net_atom = x_wm_supports (f, dpyinfo->Xatom_net_wm_state);
10735 int cur;
10736 bool dummy;
10738 get_current_wm_state (f, FRAME_OUTER_WINDOW (f), &cur, &dummy);
10740 /* Some window managers don't say they support _NET_WM_STATE, but they do say
10741 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
10742 if (!have_net_atom)
10743 have_net_atom = x_wm_supports (f, dpyinfo->Xatom_net_wm_state_fullscreen);
10745 if (have_net_atom && cur != f->want_fullscreen)
10747 Lisp_Object frame;
10749 XSETFRAME (frame, f);
10751 /* Keep number of calls to set_wm_state as low as possible.
10752 Some window managers, or possible Gtk+, hangs when too many
10753 are sent at once. */
10754 switch (f->want_fullscreen)
10756 case FULLSCREEN_BOTH:
10757 if (cur != FULLSCREEN_BOTH)
10758 set_wm_state (frame, true, dpyinfo->Xatom_net_wm_state_fullscreen,
10759 None);
10760 break;
10761 case FULLSCREEN_WIDTH:
10762 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_MAXIMIZED)
10764 set_wm_state (frame, false,
10765 dpyinfo->Xatom_net_wm_state_maximized_horz,
10766 dpyinfo->Xatom_net_wm_state_maximized_vert);
10767 set_wm_state (frame, true,
10768 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10770 else
10772 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_HEIGHT
10773 || cur == FULLSCREEN_MAXIMIZED)
10774 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10775 dpyinfo->Xatom_net_wm_state_maximized_vert);
10776 if (cur != FULLSCREEN_MAXIMIZED || x_frame_normalize_before_maximize)
10777 set_wm_state (frame, true,
10778 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10780 break;
10781 case FULLSCREEN_HEIGHT:
10782 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_MAXIMIZED)
10784 set_wm_state (frame, false,
10785 dpyinfo->Xatom_net_wm_state_maximized_horz,
10786 dpyinfo->Xatom_net_wm_state_maximized_vert);
10787 set_wm_state (frame, true,
10788 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10790 else
10792 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_WIDTH
10793 || cur == FULLSCREEN_MAXIMIZED)
10794 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10795 dpyinfo->Xatom_net_wm_state_maximized_horz);
10796 if (cur != FULLSCREEN_MAXIMIZED || x_frame_normalize_before_maximize)
10797 set_wm_state (frame, true,
10798 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10800 break;
10801 case FULLSCREEN_MAXIMIZED:
10802 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_BOTH)
10804 set_wm_state (frame, false,
10805 dpyinfo->Xatom_net_wm_state_fullscreen, None);
10806 set_wm_state (frame, true,
10807 dpyinfo->Xatom_net_wm_state_maximized_horz,
10808 dpyinfo->Xatom_net_wm_state_maximized_vert);
10810 else if (x_frame_normalize_before_maximize && cur == FULLSCREEN_WIDTH)
10812 set_wm_state (frame, false,
10813 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10814 set_wm_state (frame, true,
10815 dpyinfo->Xatom_net_wm_state_maximized_horz,
10816 dpyinfo->Xatom_net_wm_state_maximized_vert);
10818 else if (x_frame_normalize_before_maximize && cur == FULLSCREEN_HEIGHT)
10820 set_wm_state (frame, false,
10821 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10822 set_wm_state (frame, true,
10823 dpyinfo->Xatom_net_wm_state_maximized_horz,
10824 dpyinfo->Xatom_net_wm_state_maximized_vert);
10826 else
10828 if (cur == FULLSCREEN_BOTH)
10829 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10830 None);
10831 else if (cur == FULLSCREEN_HEIGHT)
10832 set_wm_state (frame, true,
10833 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10834 else if (cur == FULLSCREEN_WIDTH)
10835 set_wm_state (frame, true, None,
10836 dpyinfo->Xatom_net_wm_state_maximized_vert);
10837 else
10838 set_wm_state (frame, true,
10839 dpyinfo->Xatom_net_wm_state_maximized_horz,
10840 dpyinfo->Xatom_net_wm_state_maximized_vert);
10842 break;
10843 case FULLSCREEN_NONE:
10844 if (cur == FULLSCREEN_BOTH)
10845 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10846 None);
10847 else
10848 set_wm_state (frame, false,
10849 dpyinfo->Xatom_net_wm_state_maximized_horz,
10850 dpyinfo->Xatom_net_wm_state_maximized_vert);
10853 f->want_fullscreen = FULLSCREEN_NONE;
10857 return have_net_atom;
10860 static void
10861 XTfullscreen_hook (struct frame *f)
10863 if (FRAME_VISIBLE_P (f))
10865 block_input ();
10866 x_check_fullscreen (f);
10867 x_sync (f);
10868 unblock_input ();
10873 static bool
10874 x_handle_net_wm_state (struct frame *f, const XPropertyEvent *event)
10876 int value = FULLSCREEN_NONE;
10877 Lisp_Object lval;
10878 bool sticky = false;
10879 bool not_hidden = get_current_wm_state (f, event->window, &value, &sticky);
10881 lval = Qnil;
10882 switch (value)
10884 case FULLSCREEN_WIDTH:
10885 lval = Qfullwidth;
10886 break;
10887 case FULLSCREEN_HEIGHT:
10888 lval = Qfullheight;
10889 break;
10890 case FULLSCREEN_BOTH:
10891 lval = Qfullboth;
10892 break;
10893 case FULLSCREEN_MAXIMIZED:
10894 lval = Qmaximized;
10895 break;
10898 frame_size_history_add
10899 (f, Qx_handle_net_wm_state, 0, 0,
10900 list2 (get_frame_param (f, Qfullscreen), lval));
10902 store_frame_param (f, Qfullscreen, lval);
10903 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
10905 return not_hidden;
10908 /* Check if we need to resize the frame due to a fullscreen request.
10909 If so needed, resize the frame. */
10910 static void
10911 x_check_fullscreen (struct frame *f)
10913 Lisp_Object lval = Qnil;
10915 if (do_ewmh_fullscreen (f))
10916 return;
10918 if (f->output_data.x->parent_desc != FRAME_DISPLAY_INFO (f)->root_window)
10919 return; /* Only fullscreen without WM or with EWM hints (above). */
10921 /* Setting fullscreen to nil doesn't do anything. We could save the
10922 last non-fullscreen size and restore it, but it seems like a
10923 lot of work for this unusual case (no window manager running). */
10925 if (f->want_fullscreen != FULLSCREEN_NONE)
10927 int width = FRAME_PIXEL_WIDTH (f), height = FRAME_PIXEL_HEIGHT (f);
10928 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10930 switch (f->want_fullscreen)
10932 /* No difference between these two when there is no WM */
10933 case FULLSCREEN_MAXIMIZED:
10934 lval = Qmaximized;
10935 width = x_display_pixel_width (dpyinfo);
10936 height = x_display_pixel_height (dpyinfo);
10937 break;
10938 case FULLSCREEN_BOTH:
10939 lval = Qfullboth;
10940 width = x_display_pixel_width (dpyinfo);
10941 height = x_display_pixel_height (dpyinfo);
10942 break;
10943 case FULLSCREEN_WIDTH:
10944 lval = Qfullwidth;
10945 width = x_display_pixel_width (dpyinfo);
10946 height = height + FRAME_MENUBAR_HEIGHT (f);
10947 break;
10948 case FULLSCREEN_HEIGHT:
10949 lval = Qfullheight;
10950 height = x_display_pixel_height (dpyinfo);
10951 break;
10952 default:
10953 emacs_abort ();
10956 frame_size_history_add
10957 (f, Qx_check_fullscreen, width, height, Qnil);
10959 x_wm_set_size_hint (f, 0, false);
10961 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10962 width, height);
10964 if (FRAME_VISIBLE_P (f))
10965 x_wait_for_event (f, ConfigureNotify);
10966 else
10968 change_frame_size (f, width, height - FRAME_MENUBAR_HEIGHT (f),
10969 false, true, false, true);
10970 x_sync (f);
10974 /* `x_net_wm_state' might have reset the fullscreen frame parameter,
10975 restore it. */
10976 store_frame_param (f, Qfullscreen, lval);
10979 /* This function is called by x_set_offset to determine whether the window
10980 manager interfered with the positioning of the frame. Type A window
10981 managers position the surrounding window manager decorations a small
10982 amount above and left of the user-supplied position. Type B window
10983 managers position the surrounding window manager decorations at the
10984 user-specified position. If we detect a Type A window manager, we
10985 compensate by moving the window right and down by the proper amount. */
10987 static void
10988 x_check_expected_move (struct frame *f, int expected_left, int expected_top)
10990 int current_left = 0, current_top = 0;
10992 /* x_real_positions returns the left and top offsets of the outermost
10993 window manager window around the frame. */
10995 x_real_positions (f, &current_left, &current_top);
10997 if (current_left != expected_left || current_top != expected_top)
10999 /* It's a "Type A" window manager. */
11001 int adjusted_left;
11002 int adjusted_top;
11004 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
11005 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
11006 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
11008 /* Now fix the mispositioned frame's location. */
11010 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
11011 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
11013 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11014 adjusted_left, adjusted_top);
11016 x_sync_with_move (f, expected_left, expected_top, false);
11018 else
11019 /* It's a "Type B" window manager. We don't have to adjust the
11020 frame's position. */
11022 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
11026 /* Wait for XGetGeometry to return up-to-date position information for a
11027 recently-moved frame. Call this immediately after calling XMoveWindow.
11028 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
11029 frame has been moved to, so we use a fuzzy position comparison instead
11030 of an exact comparison. */
11032 static void
11033 x_sync_with_move (struct frame *f, int left, int top, bool fuzzy)
11035 int count = 0;
11037 while (count++ < 50)
11039 int current_left = 0, current_top = 0;
11041 /* In theory, this call to XSync only needs to happen once, but in
11042 practice, it doesn't seem to work, hence the need for the surrounding
11043 loop. */
11045 XSync (FRAME_X_DISPLAY (f), False);
11046 x_real_positions (f, &current_left, &current_top);
11048 if (fuzzy)
11050 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
11051 pixels. */
11053 if (eabs (current_left - left) <= 10
11054 && eabs (current_top - top) <= 40)
11055 return;
11057 else if (current_left == left && current_top == top)
11058 return;
11061 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
11062 will then return up-to-date position info. */
11064 wait_reading_process_output (0, 500000000, 0, false, Qnil, NULL, 0);
11068 /* Wait for an event on frame F matching EVENTTYPE. */
11069 void
11070 x_wait_for_event (struct frame *f, int eventtype)
11072 if (!FLOATP (Vx_wait_for_event_timeout))
11073 return;
11075 int level = interrupt_input_blocked;
11076 fd_set fds;
11077 struct timespec tmo, tmo_at, time_now;
11078 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
11080 f->wait_event_type = eventtype;
11082 /* Default timeout is 0.1 second. Hopefully not noticeable. */
11083 double timeout = XFLOAT_DATA (Vx_wait_for_event_timeout);
11084 time_t timeout_seconds = (time_t) timeout;
11085 tmo = make_timespec
11086 (timeout_seconds, (long int) ((timeout - timeout_seconds)
11087 * 1000 * 1000 * 1000));
11088 tmo_at = timespec_add (current_timespec (), tmo);
11090 while (f->wait_event_type)
11092 pending_signals = true;
11093 totally_unblock_input ();
11094 /* XTread_socket is called after unblock. */
11095 block_input ();
11096 interrupt_input_blocked = level;
11098 FD_ZERO (&fds);
11099 FD_SET (fd, &fds);
11101 time_now = current_timespec ();
11102 if (timespec_cmp (tmo_at, time_now) < 0)
11103 break;
11105 tmo = timespec_sub (tmo_at, time_now);
11106 if (pselect (fd + 1, &fds, NULL, NULL, &tmo, NULL) == 0)
11107 break; /* Timeout */
11110 f->wait_event_type = 0;
11114 /* Change the size of frame F's X window to WIDTH/HEIGHT in the case F
11115 doesn't have a widget. If CHANGE_GRAVITY, change to
11116 top-left-corner window gravity for this size change and subsequent
11117 size changes. Otherwise leave the window gravity unchanged. */
11119 static void
11120 x_set_window_size_1 (struct frame *f, bool change_gravity,
11121 int width, int height)
11123 int pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
11124 int pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
11125 int old_width = FRAME_PIXEL_WIDTH (f);
11126 int old_height = FRAME_PIXEL_HEIGHT (f);
11127 Lisp_Object fullscreen = get_frame_param (f, Qfullscreen);
11129 if (change_gravity)
11130 f->win_gravity = NorthWestGravity;
11131 x_wm_set_size_hint (f, 0, false);
11133 /* When the frame is fullheight and we only want to change the width
11134 or it is fullwidth and we only want to change the height we should
11135 be able to preserve the fullscreen property. However, due to the
11136 fact that we have to send a resize request anyway, the window
11137 manager will abolish it. At least the respective size should
11138 remain unchanged but giving the frame back its normal size will
11139 be broken ... */
11140 if (EQ (fullscreen, Qfullwidth) && width == FRAME_TEXT_WIDTH (f))
11142 frame_size_history_add
11143 (f, Qx_set_window_size_1, width, height,
11144 list2 (make_number (old_height),
11145 make_number (pixelheight + FRAME_MENUBAR_HEIGHT (f))));
11147 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11148 old_width, pixelheight + FRAME_MENUBAR_HEIGHT (f));
11150 else if (EQ (fullscreen, Qfullheight) && height == FRAME_TEXT_HEIGHT (f))
11152 frame_size_history_add
11153 (f, Qx_set_window_size_2, width, height,
11154 list2 (make_number (old_width), make_number (pixelwidth)));
11156 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11157 pixelwidth, old_height);
11160 else
11162 frame_size_history_add
11163 (f, Qx_set_window_size_3, width, height,
11164 list3 (make_number (pixelwidth + FRAME_TOOLBAR_WIDTH (f)),
11165 make_number (pixelheight + FRAME_TOOLBAR_HEIGHT (f)
11166 + FRAME_MENUBAR_HEIGHT (f)),
11167 make_number (FRAME_MENUBAR_HEIGHT (f))));
11169 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11170 pixelwidth, pixelheight + FRAME_MENUBAR_HEIGHT (f));
11171 fullscreen = Qnil;
11176 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11177 receive in the ConfigureNotify event; if we get what we asked
11178 for, then the event won't cause the screen to become garbaged, so
11179 we have to make sure to do it here. */
11180 SET_FRAME_GARBAGED (f);
11182 /* Now, strictly speaking, we can't be sure that this is accurate,
11183 but the window manager will get around to dealing with the size
11184 change request eventually, and we'll hear how it went when the
11185 ConfigureNotify event gets here.
11187 We could just not bother storing any of this information here,
11188 and let the ConfigureNotify event set everything up, but that
11189 might be kind of confusing to the Lisp code, since size changes
11190 wouldn't be reported in the frame parameters until some random
11191 point in the future when the ConfigureNotify event arrives.
11193 Pass true for DELAY since we can't run Lisp code inside of
11194 a BLOCK_INPUT. */
11196 /* But the ConfigureNotify may in fact never arrive, and then this is
11197 not right if the frame is visible. Instead wait (with timeout)
11198 for the ConfigureNotify. */
11199 if (FRAME_VISIBLE_P (f))
11201 x_wait_for_event (f, ConfigureNotify);
11203 if (!NILP (fullscreen))
11204 /* Try to restore fullscreen state. */
11206 store_frame_param (f, Qfullscreen, fullscreen);
11207 x_set_fullscreen (f, fullscreen, fullscreen);
11210 else
11212 change_frame_size (f, width, height, false, true, false, true);
11213 x_sync (f);
11218 /* Call this to change the size of frame F's x-window.
11219 If CHANGE_GRAVITY, change to top-left-corner window gravity
11220 for this size change and subsequent size changes.
11221 Otherwise we leave the window gravity unchanged. */
11223 void
11224 x_set_window_size (struct frame *f, bool change_gravity,
11225 int width, int height, bool pixelwise)
11227 block_input ();
11229 /* The following breaks our calculations. If it's really needed,
11230 think of something else. */
11231 #if false
11232 if (!FRAME_TOOLTIP_P (f))
11234 int text_width, text_height;
11236 /* When the frame is maximized/fullscreen or running under for
11237 example Xmonad, x_set_window_size_1 will be a no-op.
11238 In that case, the right thing to do is extend rows/width to
11239 the current frame size. We do that first if x_set_window_size_1
11240 turns out to not be a no-op (there is no way to know).
11241 The size will be adjusted again if the frame gets a
11242 ConfigureNotify event as a result of x_set_window_size. */
11243 int pixelh = FRAME_PIXEL_HEIGHT (f);
11244 #ifdef USE_X_TOOLKIT
11245 /* The menu bar is not part of text lines. The tool bar
11246 is however. */
11247 pixelh -= FRAME_MENUBAR_HEIGHT (f);
11248 #endif
11249 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, FRAME_PIXEL_WIDTH (f));
11250 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelh);
11252 change_frame_size (f, text_width, text_height, false, true, false, true);
11254 #endif
11256 /* Pixelize width and height, if necessary. */
11257 if (! pixelwise)
11259 width = width * FRAME_COLUMN_WIDTH (f);
11260 height = height * FRAME_LINE_HEIGHT (f);
11263 #ifdef USE_GTK
11264 if (FRAME_GTK_WIDGET (f))
11265 xg_frame_set_char_size (f, width, height);
11266 else
11267 x_set_window_size_1 (f, change_gravity, width, height);
11268 #else /* not USE_GTK */
11269 x_set_window_size_1 (f, change_gravity, width, height);
11270 x_clear_under_internal_border (f);
11271 #endif /* not USE_GTK */
11273 /* If cursor was outside the new size, mark it as off. */
11274 mark_window_cursors_off (XWINDOW (f->root_window));
11276 /* Clear out any recollection of where the mouse highlighting was,
11277 since it might be in a place that's outside the new frame size.
11278 Actually checking whether it is outside is a pain in the neck,
11279 so don't try--just let the highlighting be done afresh with new size. */
11280 cancel_mouse_face (f);
11282 unblock_input ();
11284 do_pending_window_change (false);
11287 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11289 void
11290 frame_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
11292 block_input ();
11294 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11295 0, 0, 0, 0, pix_x, pix_y);
11296 unblock_input ();
11299 /* Raise frame F. */
11301 void
11302 x_raise_frame (struct frame *f)
11304 block_input ();
11305 if (FRAME_VISIBLE_P (f))
11306 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
11307 XFlush (FRAME_X_DISPLAY (f));
11308 unblock_input ();
11311 /* Lower frame F. */
11313 static void
11314 x_lower_frame (struct frame *f)
11316 if (FRAME_VISIBLE_P (f))
11318 block_input ();
11319 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
11320 XFlush (FRAME_X_DISPLAY (f));
11321 unblock_input ();
11325 /* Request focus with XEmbed */
11327 void
11328 xembed_request_focus (struct frame *f)
11330 /* See XEmbed Protocol Specification at
11331 http://freedesktop.org/wiki/Specifications/xembed-spec */
11332 if (FRAME_VISIBLE_P (f))
11333 xembed_send_message (f, CurrentTime,
11334 XEMBED_REQUEST_FOCUS, 0, 0, 0);
11337 /* Activate frame with Extended Window Manager Hints */
11339 void
11340 x_ewmh_activate_frame (struct frame *f)
11342 /* See Window Manager Specification/Extended Window Manager Hints at
11343 http://freedesktop.org/wiki/Specifications/wm-spec */
11345 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11347 if (FRAME_VISIBLE_P (f) && x_wm_supports (f, dpyinfo->Xatom_net_active_window))
11349 Lisp_Object frame;
11350 XSETFRAME (frame, f);
11351 x_send_client_event (frame, make_number (0), frame,
11352 dpyinfo->Xatom_net_active_window,
11353 make_number (32),
11354 list2i (1, dpyinfo->last_user_time));
11358 static void
11359 XTframe_raise_lower (struct frame *f, bool raise_flag)
11361 if (raise_flag)
11362 x_raise_frame (f);
11363 else
11364 x_lower_frame (f);
11367 /* XEmbed implementation. */
11369 #if defined USE_X_TOOLKIT || ! defined USE_GTK
11371 /* XEmbed implementation. */
11373 #define XEMBED_VERSION 0
11375 static void
11376 xembed_set_info (struct frame *f, enum xembed_info flags)
11378 unsigned long data[2];
11379 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11381 data[0] = XEMBED_VERSION;
11382 data[1] = flags;
11384 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11385 dpyinfo->Xatom_XEMBED_INFO, dpyinfo->Xatom_XEMBED_INFO,
11386 32, PropModeReplace, (unsigned char *) data, 2);
11388 #endif /* defined USE_X_TOOLKIT || ! defined USE_GTK */
11390 static void
11391 xembed_send_message (struct frame *f, Time t, enum xembed_message msg,
11392 long int detail, long int data1, long int data2)
11394 XEvent event;
11396 event.xclient.type = ClientMessage;
11397 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
11398 event.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_XEMBED;
11399 event.xclient.format = 32;
11400 event.xclient.data.l[0] = t;
11401 event.xclient.data.l[1] = msg;
11402 event.xclient.data.l[2] = detail;
11403 event.xclient.data.l[3] = data1;
11404 event.xclient.data.l[4] = data2;
11406 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
11407 False, NoEventMask, &event);
11408 XSync (FRAME_X_DISPLAY (f), False);
11411 /* Change of visibility. */
11413 /* This tries to wait until the frame is really visible, depending on
11414 the value of Vx_wait_for_event_timeout.
11415 However, if the window manager asks the user where to position
11416 the frame, this will return before the user finishes doing that.
11417 The frame will not actually be visible at that time,
11418 but it will become visible later when the window manager
11419 finishes with it. */
11421 void
11422 x_make_frame_visible (struct frame *f)
11424 if (FRAME_PARENT_FRAME (f))
11426 if (!FRAME_VISIBLE_P (f))
11428 block_input ();
11429 #ifdef USE_GTK
11430 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
11431 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11432 f->left_pos, f->top_pos);
11433 #else
11434 XMapRaised (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
11435 #endif
11436 unblock_input ();
11438 SET_FRAME_VISIBLE (f, true);
11439 SET_FRAME_ICONIFIED (f, false);
11441 return;
11444 block_input ();
11446 x_set_bitmap_icon (f);
11448 if (! FRAME_VISIBLE_P (f))
11450 /* We test asked_for_visible here to make sure we don't
11451 call x_set_offset a second time
11452 if we get to x_make_frame_visible a second time
11453 before the window gets really visible. */
11454 if (! FRAME_ICONIFIED_P (f)
11455 && ! FRAME_X_EMBEDDED_P (f)
11456 && ! f->output_data.x->asked_for_visible)
11457 x_set_offset (f, f->left_pos, f->top_pos, 0);
11459 f->output_data.x->asked_for_visible = true;
11461 if (! EQ (Vx_no_window_manager, Qt))
11462 x_wm_set_window_state (f, NormalState);
11463 #ifdef USE_X_TOOLKIT
11464 if (FRAME_X_EMBEDDED_P (f))
11465 xembed_set_info (f, XEMBED_MAPPED);
11466 else
11468 /* This was XtPopup, but that did nothing for an iconified frame. */
11469 XtMapWidget (f->output_data.x->widget);
11471 #else /* not USE_X_TOOLKIT */
11472 #ifdef USE_GTK
11473 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
11474 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
11475 #else
11476 if (FRAME_X_EMBEDDED_P (f))
11477 xembed_set_info (f, XEMBED_MAPPED);
11478 else
11479 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11480 #endif /* not USE_GTK */
11481 #endif /* not USE_X_TOOLKIT */
11484 XFlush (FRAME_X_DISPLAY (f));
11486 /* Synchronize to ensure Emacs knows the frame is visible
11487 before we do anything else. We do this loop with input not blocked
11488 so that incoming events are handled. */
11490 Lisp_Object frame;
11491 /* This must be before UNBLOCK_INPUT
11492 since events that arrive in response to the actions above
11493 will set it when they are handled. */
11494 bool previously_visible = f->output_data.x->has_been_visible;
11496 XSETFRAME (frame, f);
11498 int original_left = f->left_pos;
11499 int original_top = f->top_pos;
11501 /* This must come after we set COUNT. */
11502 unblock_input ();
11504 /* We unblock here so that arriving X events are processed. */
11506 /* Now move the window back to where it was "supposed to be".
11507 But don't do it if the gravity is negative.
11508 When the gravity is negative, this uses a position
11509 that is 3 pixels too low. Perhaps that's really the border width.
11511 Don't do this if the window has never been visible before,
11512 because the window manager may choose the position
11513 and we don't want to override it. */
11515 if (!FRAME_VISIBLE_P (f)
11516 && !FRAME_ICONIFIED_P (f)
11517 && !FRAME_X_EMBEDDED_P (f)
11518 && !FRAME_PARENT_FRAME (f)
11519 && f->win_gravity == NorthWestGravity
11520 && previously_visible)
11522 Drawable rootw;
11523 int x, y;
11524 unsigned int width, height, border, depth;
11526 block_input ();
11528 /* On some window managers (such as FVWM) moving an existing
11529 window, even to the same place, causes the window manager
11530 to introduce an offset. This can cause the window to move
11531 to an unexpected location. Check the geometry (a little
11532 slow here) and then verify that the window is in the right
11533 place. If the window is not in the right place, move it
11534 there, and take the potential window manager hit. */
11535 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11536 &rootw, &x, &y, &width, &height, &border, &depth);
11538 if (original_left != x || original_top != y)
11539 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11540 original_left, original_top);
11542 unblock_input ();
11545 /* Try to wait for a MapNotify event (that is what tells us when a
11546 frame becomes visible). */
11548 #ifdef CYGWIN
11549 /* On Cygwin, which uses input polling, we need to force input to
11550 be read. See
11551 https://lists.gnu.org/r/emacs-devel/2013-12/msg00351.html
11552 and https://debbugs.gnu.org/cgi/bugreport.cgi?bug=24091#131.
11553 Fake an alarm signal to let the handler know that there's
11554 something to be read.
11556 It could be confusing if a real alarm arrives while processing
11557 the fake one. Turn it off and let the handler reset it. */
11558 int old_poll_suppress_count = poll_suppress_count;
11559 poll_suppress_count = 1;
11560 poll_for_input_1 ();
11561 poll_suppress_count = old_poll_suppress_count;
11562 #endif
11563 if (! FRAME_VISIBLE_P (f))
11564 x_wait_for_event (f, MapNotify);
11568 /* Change from mapped state to withdrawn state. */
11570 /* Make the frame visible (mapped and not iconified). */
11572 void
11573 x_make_frame_invisible (struct frame *f)
11575 Window window;
11577 /* Use the frame's outermost window, not the one we normally draw on. */
11578 window = FRAME_OUTER_WINDOW (f);
11580 /* Don't keep the highlight on an invisible frame. */
11581 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
11582 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
11584 block_input ();
11586 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11587 that the current position of the window is user-specified, rather than
11588 program-specified, so that when the window is mapped again, it will be
11589 placed at the same location, without forcing the user to position it
11590 by hand again (they have already done that once for this window.) */
11591 x_wm_set_size_hint (f, 0, true);
11593 #ifdef USE_GTK
11594 if (FRAME_GTK_OUTER_WIDGET (f))
11595 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
11596 else
11597 #else
11598 if (FRAME_X_EMBEDDED_P (f))
11599 xembed_set_info (f, 0);
11600 else
11601 #endif
11603 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11604 DefaultScreen (FRAME_X_DISPLAY (f))))
11606 unblock_input ();
11607 error ("Can't notify window manager of window withdrawal");
11610 x_sync (f);
11612 /* We can't distinguish this from iconification
11613 just by the event that we get from the server.
11614 So we can't win using the usual strategy of letting
11615 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11616 and synchronize with the server to make sure we agree. */
11617 SET_FRAME_VISIBLE (f, 0);
11618 SET_FRAME_ICONIFIED (f, false);
11620 unblock_input ();
11623 /* Change window state from mapped to iconified. */
11625 void
11626 x_iconify_frame (struct frame *f)
11628 #ifdef USE_X_TOOLKIT
11629 int result;
11630 #endif
11632 /* Don't keep the highlight on an invisible frame. */
11633 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
11634 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
11636 if (FRAME_ICONIFIED_P (f))
11637 return;
11639 block_input ();
11641 x_set_bitmap_icon (f);
11643 #if defined (USE_GTK)
11644 if (FRAME_GTK_OUTER_WIDGET (f))
11646 if (! FRAME_VISIBLE_P (f))
11647 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
11649 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
11650 SET_FRAME_VISIBLE (f, 0);
11651 SET_FRAME_ICONIFIED (f, true);
11652 unblock_input ();
11653 return;
11655 #endif
11657 #ifdef USE_X_TOOLKIT
11659 if (! FRAME_VISIBLE_P (f))
11661 if (! EQ (Vx_no_window_manager, Qt))
11662 x_wm_set_window_state (f, IconicState);
11663 /* This was XtPopup, but that did nothing for an iconified frame. */
11664 XtMapWidget (f->output_data.x->widget);
11665 /* The server won't give us any event to indicate
11666 that an invisible frame was changed to an icon,
11667 so we have to record it here. */
11668 SET_FRAME_VISIBLE (f, 0);
11669 SET_FRAME_ICONIFIED (f, true);
11670 unblock_input ();
11671 return;
11674 result = XIconifyWindow (FRAME_X_DISPLAY (f),
11675 XtWindow (f->output_data.x->widget),
11676 DefaultScreen (FRAME_X_DISPLAY (f)));
11677 unblock_input ();
11679 if (!result)
11680 error ("Can't notify window manager of iconification");
11682 SET_FRAME_ICONIFIED (f, true);
11683 SET_FRAME_VISIBLE (f, 0);
11685 block_input ();
11686 XFlush (FRAME_X_DISPLAY (f));
11687 unblock_input ();
11688 #else /* not USE_X_TOOLKIT */
11690 /* Make sure the X server knows where the window should be positioned,
11691 in case the user deiconifies with the window manager. */
11692 if (! FRAME_VISIBLE_P (f)
11693 && ! FRAME_ICONIFIED_P (f)
11694 && ! FRAME_X_EMBEDDED_P (f))
11695 x_set_offset (f, f->left_pos, f->top_pos, 0);
11697 /* Since we don't know which revision of X we're running, we'll use both
11698 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11700 /* X11R4: send a ClientMessage to the window manager using the
11701 WM_CHANGE_STATE type. */
11703 XEvent msg;
11705 msg.xclient.window = FRAME_X_WINDOW (f);
11706 msg.xclient.type = ClientMessage;
11707 msg.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_wm_change_state;
11708 msg.xclient.format = 32;
11709 msg.xclient.data.l[0] = IconicState;
11711 if (! XSendEvent (FRAME_X_DISPLAY (f),
11712 DefaultRootWindow (FRAME_X_DISPLAY (f)),
11713 False,
11714 SubstructureRedirectMask | SubstructureNotifyMask,
11715 &msg))
11717 unblock_input ();
11718 error ("Can't notify window manager of iconification");
11722 /* X11R3: set the initial_state field of the window manager hints to
11723 IconicState. */
11724 x_wm_set_window_state (f, IconicState);
11726 if (!FRAME_VISIBLE_P (f))
11728 /* If the frame was withdrawn, before, we must map it. */
11729 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11732 SET_FRAME_ICONIFIED (f, true);
11733 SET_FRAME_VISIBLE (f, 0);
11735 XFlush (FRAME_X_DISPLAY (f));
11736 unblock_input ();
11737 #endif /* not USE_X_TOOLKIT */
11741 /* Free X resources of frame F. */
11743 void
11744 x_free_frame_resources (struct frame *f)
11746 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11747 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
11748 #ifdef USE_X_TOOLKIT
11749 Lisp_Object bar;
11750 struct scroll_bar *b;
11751 #endif
11753 block_input ();
11755 /* If a display connection is dead, don't try sending more
11756 commands to the X server. */
11757 if (dpyinfo->display)
11759 /* Always exit with visible pointer to avoid weird issue
11760 with Xfixes (Bug#17609). */
11761 if (f->pointer_invisible)
11762 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, 0);
11764 /* We must free faces before destroying windows because some
11765 font-driver (e.g. xft) access a window while finishing a
11766 face. */
11767 free_frame_faces (f);
11768 tear_down_x_back_buffer (f);
11770 if (f->output_data.x->icon_desc)
11771 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
11773 #ifdef USE_X_TOOLKIT
11774 /* Explicitly destroy the scroll bars of the frame. Without
11775 this, we get "BadDrawable" errors from the toolkit later on,
11776 presumably from expose events generated for the disappearing
11777 toolkit scroll bars. */
11778 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
11780 b = XSCROLL_BAR (bar);
11781 x_scroll_bar_remove (b);
11783 #endif
11785 #ifdef HAVE_X_I18N
11786 if (FRAME_XIC (f))
11787 free_frame_xic (f);
11788 #endif
11790 x_free_cr_resources (f);
11791 #ifdef USE_X_TOOLKIT
11792 if (f->output_data.x->widget)
11794 XtDestroyWidget (f->output_data.x->widget);
11795 f->output_data.x->widget = NULL;
11797 /* Tooltips don't have widgets, only a simple X window, even if
11798 we are using a toolkit. */
11799 else if (FRAME_X_WINDOW (f))
11800 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11802 free_frame_menubar (f);
11804 if (f->shell_position)
11805 xfree (f->shell_position);
11806 #else /* !USE_X_TOOLKIT */
11808 #ifdef USE_GTK
11809 xg_free_frame_widgets (f);
11810 #endif /* USE_GTK */
11812 tear_down_x_back_buffer (f);
11813 if (FRAME_X_WINDOW (f))
11814 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11815 #endif /* !USE_X_TOOLKIT */
11817 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
11818 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
11819 unload_color (f, f->output_data.x->cursor_pixel);
11820 unload_color (f, f->output_data.x->cursor_foreground_pixel);
11821 unload_color (f, f->output_data.x->border_pixel);
11822 unload_color (f, f->output_data.x->mouse_pixel);
11824 if (f->output_data.x->scroll_bar_background_pixel != -1)
11825 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
11826 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
11827 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
11828 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
11829 /* Scrollbar shadow colors. */
11830 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
11831 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
11832 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
11833 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
11834 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
11835 if (f->output_data.x->white_relief.pixel != -1)
11836 unload_color (f, f->output_data.x->white_relief.pixel);
11837 if (f->output_data.x->black_relief.pixel != -1)
11838 unload_color (f, f->output_data.x->black_relief.pixel);
11840 x_free_gcs (f);
11842 /* Free extra GCs allocated by x_setup_relief_colors. */
11843 if (f->output_data.x->white_relief.gc)
11845 XFreeGC (dpyinfo->display, f->output_data.x->white_relief.gc);
11846 f->output_data.x->white_relief.gc = 0;
11848 if (f->output_data.x->black_relief.gc)
11850 XFreeGC (dpyinfo->display, f->output_data.x->black_relief.gc);
11851 f->output_data.x->black_relief.gc = 0;
11854 /* Free cursors. */
11855 if (f->output_data.x->text_cursor != 0)
11856 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->text_cursor);
11857 if (f->output_data.x->nontext_cursor != 0)
11858 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->nontext_cursor);
11859 if (f->output_data.x->modeline_cursor != 0)
11860 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->modeline_cursor);
11861 if (f->output_data.x->hand_cursor != 0)
11862 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hand_cursor);
11863 if (f->output_data.x->hourglass_cursor != 0)
11864 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hourglass_cursor);
11865 if (f->output_data.x->horizontal_drag_cursor != 0)
11866 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->horizontal_drag_cursor);
11867 if (f->output_data.x->vertical_drag_cursor != 0)
11868 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->vertical_drag_cursor);
11869 if (f->output_data.x->left_edge_cursor != 0)
11870 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->left_edge_cursor);
11871 if (f->output_data.x->top_left_corner_cursor != 0)
11872 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->top_left_corner_cursor);
11873 if (f->output_data.x->top_edge_cursor != 0)
11874 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->top_edge_cursor);
11875 if (f->output_data.x->top_right_corner_cursor != 0)
11876 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->top_right_corner_cursor);
11877 if (f->output_data.x->right_edge_cursor != 0)
11878 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->right_edge_cursor);
11879 if (f->output_data.x->bottom_right_corner_cursor != 0)
11880 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->bottom_right_corner_cursor);
11881 if (f->output_data.x->bottom_edge_cursor != 0)
11882 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->bottom_edge_cursor);
11883 if (f->output_data.x->bottom_left_corner_cursor != 0)
11884 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->bottom_left_corner_cursor);
11886 XFlush (FRAME_X_DISPLAY (f));
11889 xfree (f->output_data.x->saved_menu_event);
11890 xfree (f->output_data.x);
11891 f->output_data.x = NULL;
11893 if (f == dpyinfo->x_focus_frame)
11894 dpyinfo->x_focus_frame = 0;
11895 if (f == dpyinfo->x_focus_event_frame)
11896 dpyinfo->x_focus_event_frame = 0;
11897 if (f == dpyinfo->x_highlight_frame)
11898 dpyinfo->x_highlight_frame = 0;
11899 if (f == hlinfo->mouse_face_mouse_frame)
11900 reset_mouse_highlight (hlinfo);
11902 unblock_input ();
11906 /* Destroy the X window of frame F. */
11908 static void
11909 x_destroy_window (struct frame *f)
11911 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11913 /* If a display connection is dead, don't try sending more
11914 commands to the X server. */
11915 if (dpyinfo->display != 0)
11916 x_free_frame_resources (f);
11918 dpyinfo->reference_count--;
11922 /* Setting window manager hints. */
11924 /* Set the normal size hints for the window manager, for frame F.
11925 FLAGS is the flags word to use--or 0 meaning preserve the flags
11926 that the window now has.
11927 If USER_POSITION, set the USPosition
11928 flag (this is useful when FLAGS is 0).
11929 The GTK version is in gtkutils.c. */
11931 #ifndef USE_GTK
11932 void
11933 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
11935 XSizeHints size_hints;
11936 Window window = FRAME_OUTER_WINDOW (f);
11938 if (!window)
11939 return;
11941 #ifdef USE_X_TOOLKIT
11942 if (f->output_data.x->widget)
11944 widget_update_wm_size_hints (f->output_data.x->widget);
11945 return;
11947 #endif
11949 /* Setting PMaxSize caused various problems. */
11950 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
11952 size_hints.x = f->left_pos;
11953 size_hints.y = f->top_pos;
11955 size_hints.width = FRAME_PIXEL_WIDTH (f);
11956 size_hints.height = FRAME_PIXEL_HEIGHT (f);
11958 size_hints.width_inc = frame_resize_pixelwise ? 1 : FRAME_COLUMN_WIDTH (f);
11959 size_hints.height_inc = frame_resize_pixelwise ? 1 : FRAME_LINE_HEIGHT (f);
11961 size_hints.max_width = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
11962 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
11963 size_hints.max_height = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
11964 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
11966 /* Calculate the base and minimum sizes. */
11968 int base_width, base_height;
11970 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
11971 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
11973 /* The window manager uses the base width hints to calculate the
11974 current number of rows and columns in the frame while
11975 resizing; min_width and min_height aren't useful for this
11976 purpose, since they might not give the dimensions for a
11977 zero-row, zero-column frame. */
11979 size_hints.flags |= PBaseSize;
11980 size_hints.base_width = base_width;
11981 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
11982 size_hints.min_width = base_width;
11983 size_hints.min_height = base_height;
11986 /* If we don't need the old flags, we don't need the old hint at all. */
11987 if (flags)
11989 size_hints.flags |= flags;
11990 goto no_read;
11994 XSizeHints hints; /* Sometimes I hate X Windows... */
11995 long supplied_return;
11996 int value;
11998 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
11999 &supplied_return);
12001 if (flags)
12002 size_hints.flags |= flags;
12003 else
12005 if (value == 0)
12006 hints.flags = 0;
12007 if (hints.flags & PSize)
12008 size_hints.flags |= PSize;
12009 if (hints.flags & PPosition)
12010 size_hints.flags |= PPosition;
12011 if (hints.flags & USPosition)
12012 size_hints.flags |= USPosition;
12013 if (hints.flags & USSize)
12014 size_hints.flags |= USSize;
12018 no_read:
12020 #ifdef PWinGravity
12021 size_hints.win_gravity = f->win_gravity;
12022 size_hints.flags |= PWinGravity;
12024 if (user_position)
12026 size_hints.flags &= ~ PPosition;
12027 size_hints.flags |= USPosition;
12029 #endif /* PWinGravity */
12031 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12033 #endif /* not USE_GTK */
12035 /* Used for IconicState or NormalState */
12037 static void
12038 x_wm_set_window_state (struct frame *f, int state)
12040 #ifdef USE_X_TOOLKIT
12041 Arg al[1];
12043 XtSetArg (al[0], XtNinitialState, state);
12044 XtSetValues (f->output_data.x->widget, al, 1);
12045 #else /* not USE_X_TOOLKIT */
12046 Window window = FRAME_X_WINDOW (f);
12048 f->output_data.x->wm_hints.flags |= StateHint;
12049 f->output_data.x->wm_hints.initial_state = state;
12051 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12052 #endif /* not USE_X_TOOLKIT */
12055 static void
12056 x_wm_set_icon_pixmap (struct frame *f, ptrdiff_t pixmap_id)
12058 Pixmap icon_pixmap, icon_mask;
12060 #if !defined USE_X_TOOLKIT && !defined USE_GTK
12061 Window window = FRAME_OUTER_WINDOW (f);
12062 #endif
12064 if (pixmap_id > 0)
12066 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
12067 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
12068 icon_mask = x_bitmap_mask (f, pixmap_id);
12069 f->output_data.x->wm_hints.icon_mask = icon_mask;
12071 else
12073 /* It seems there is no way to turn off use of an icon
12074 pixmap. */
12075 return;
12079 #ifdef USE_GTK
12081 xg_set_frame_icon (f, icon_pixmap, icon_mask);
12082 return;
12085 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
12088 Arg al[1];
12089 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12090 XtSetValues (f->output_data.x->widget, al, 1);
12091 XtSetArg (al[0], XtNiconMask, icon_mask);
12092 XtSetValues (f->output_data.x->widget, al, 1);
12095 #else /* not USE_X_TOOLKIT && not USE_GTK */
12097 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
12098 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12100 #endif /* not USE_X_TOOLKIT && not USE_GTK */
12103 void
12104 x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
12106 Window window = FRAME_OUTER_WINDOW (f);
12108 f->output_data.x->wm_hints.flags |= IconPositionHint;
12109 f->output_data.x->wm_hints.icon_x = icon_x;
12110 f->output_data.x->wm_hints.icon_y = icon_y;
12112 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12116 /***********************************************************************
12117 Fonts
12118 ***********************************************************************/
12120 #ifdef GLYPH_DEBUG
12122 /* Check that FONT is valid on frame F. It is if it can be found in F's
12123 font table. */
12125 static void
12126 x_check_font (struct frame *f, struct font *font)
12128 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
12129 if (font->driver->check)
12130 eassert (font->driver->check (f, font) == 0);
12133 #endif /* GLYPH_DEBUG */
12136 /***********************************************************************
12137 Initialization
12138 ***********************************************************************/
12140 #ifdef USE_X_TOOLKIT
12141 static XrmOptionDescRec emacs_options[] = {
12142 {(char *) "-geometry", (char *) ".geometry", XrmoptionSepArg, NULL},
12143 {(char *) "-iconic", (char *) ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
12145 {(char *) "-internal-border-width",
12146 (char *) "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
12147 {(char *) "-ib", (char *) "*EmacsScreen.internalBorderWidth",
12148 XrmoptionSepArg, NULL},
12149 {(char *) "-T", (char *) "*EmacsShell.title", XrmoptionSepArg, NULL},
12150 {(char *) "-wn", (char *) "*EmacsShell.title", XrmoptionSepArg, NULL},
12151 {(char *) "-title", (char *) "*EmacsShell.title", XrmoptionSepArg, NULL},
12152 {(char *) "-iconname", (char *) "*EmacsShell.iconName",
12153 XrmoptionSepArg, NULL},
12154 {(char *) "-in", (char *) "*EmacsShell.iconName", XrmoptionSepArg, NULL},
12155 {(char *) "-mc", (char *) "*pointerColor", XrmoptionSepArg, NULL},
12156 {(char *) "-cr", (char *) "*cursorColor", XrmoptionSepArg, NULL}
12159 /* Whether atimer for Xt timeouts is activated or not. */
12161 static bool x_timeout_atimer_activated_flag;
12163 #endif /* USE_X_TOOLKIT */
12165 static int x_initialized;
12167 /* Test whether two display-name strings agree up to the dot that separates
12168 the screen number from the server number. */
12169 static bool
12170 same_x_server (const char *name1, const char *name2)
12172 bool seen_colon = false;
12173 Lisp_Object sysname = Fsystem_name ();
12174 if (! STRINGP (sysname))
12175 sysname = empty_unibyte_string;
12176 const char *system_name = SSDATA (sysname);
12177 ptrdiff_t system_name_length = SBYTES (sysname);
12178 ptrdiff_t length_until_period = 0;
12180 while (system_name[length_until_period] != 0
12181 && system_name[length_until_period] != '.')
12182 length_until_period++;
12184 /* Treat `unix' like an empty host name. */
12185 if (! strncmp (name1, "unix:", 5))
12186 name1 += 4;
12187 if (! strncmp (name2, "unix:", 5))
12188 name2 += 4;
12189 /* Treat this host's name like an empty host name. */
12190 if (! strncmp (name1, system_name, system_name_length)
12191 && name1[system_name_length] == ':')
12192 name1 += system_name_length;
12193 if (! strncmp (name2, system_name, system_name_length)
12194 && name2[system_name_length] == ':')
12195 name2 += system_name_length;
12196 /* Treat this host's domainless name like an empty host name. */
12197 if (! strncmp (name1, system_name, length_until_period)
12198 && name1[length_until_period] == ':')
12199 name1 += length_until_period;
12200 if (! strncmp (name2, system_name, length_until_period)
12201 && name2[length_until_period] == ':')
12202 name2 += length_until_period;
12204 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
12206 if (*name1 == ':')
12207 seen_colon = true;
12208 if (seen_colon && *name1 == '.')
12209 return true;
12211 return (seen_colon
12212 && (*name1 == '.' || *name1 == '\0')
12213 && (*name2 == '.' || *name2 == '\0'));
12216 /* Count number of set bits in mask and number of bits to shift to
12217 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
12218 to 5. */
12219 static void
12220 get_bits_and_offset (unsigned long mask, int *bits, int *offset)
12222 int nr = 0;
12223 int off = 0;
12225 while (!(mask & 1))
12227 off++;
12228 mask >>= 1;
12231 while (mask & 1)
12233 nr++;
12234 mask >>= 1;
12237 *offset = off;
12238 *bits = nr;
12241 /* Return true iff display DISPLAY is available for use.
12242 But don't permanently open it, just test its availability. */
12244 bool
12245 x_display_ok (const char *display)
12247 /* XOpenDisplay fails if it gets a signal. Block SIGIO which may arrive. */
12248 unrequest_sigio ();
12249 Display *dpy = XOpenDisplay (display);
12250 request_sigio ();
12251 if (!dpy)
12252 return false;
12253 XCloseDisplay (dpy);
12254 return true;
12257 #ifdef USE_GTK
12258 static void
12259 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
12260 const gchar *msg, gpointer user_data)
12262 if (!strstr (msg, "g_set_prgname"))
12263 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, msg);
12265 #endif
12267 /* Create invisible cursor on X display referred by DPYINFO. */
12269 static Cursor
12270 make_invisible_cursor (struct x_display_info *dpyinfo)
12272 Display *dpy = dpyinfo->display;
12273 static char const no_data[] = { 0 };
12274 Pixmap pix;
12275 XColor col;
12276 Cursor c = 0;
12278 x_catch_errors (dpy);
12279 pix = XCreateBitmapFromData (dpy, dpyinfo->root_window, no_data, 1, 1);
12280 if (! x_had_errors_p (dpy) && pix != None)
12282 Cursor pixc;
12283 col.pixel = 0;
12284 col.red = col.green = col.blue = 0;
12285 col.flags = DoRed | DoGreen | DoBlue;
12286 pixc = XCreatePixmapCursor (dpy, pix, pix, &col, &col, 0, 0);
12287 if (! x_had_errors_p (dpy) && pixc != None)
12288 c = pixc;
12289 XFreePixmap (dpy, pix);
12292 x_uncatch_errors ();
12294 return c;
12297 /* True if DPY supports Xfixes extension >= 4. */
12299 static bool
12300 x_probe_xfixes_extension (Display *dpy)
12302 #ifdef HAVE_XFIXES
12303 int major, minor;
12304 return XFixesQueryVersion (dpy, &major, &minor) && major >= 4;
12305 #else
12306 return false;
12307 #endif /* HAVE_XFIXES */
12310 /* Toggle mouse pointer visibility on frame F by using Xfixes functions. */
12312 static void
12313 xfixes_toggle_visible_pointer (struct frame *f, bool invisible)
12315 #ifdef HAVE_XFIXES
12316 if (invisible)
12317 XFixesHideCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12318 else
12319 XFixesShowCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12320 f->pointer_invisible = invisible;
12321 #else
12322 emacs_abort ();
12323 #endif /* HAVE_XFIXES */
12326 /* Toggle mouse pointer visibility on frame F by using invisible cursor. */
12328 static void
12329 x_toggle_visible_pointer (struct frame *f, bool invisible)
12331 eassert (FRAME_DISPLAY_INFO (f)->invisible_cursor != 0);
12332 if (invisible)
12333 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12334 FRAME_DISPLAY_INFO (f)->invisible_cursor);
12335 else
12336 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12337 f->output_data.x->current_cursor);
12338 f->pointer_invisible = invisible;
12341 /* Setup pointer blanking, prefer Xfixes if available. */
12343 static void
12344 x_setup_pointer_blanking (struct x_display_info *dpyinfo)
12346 /* FIXME: the brave tester should set EMACS_XFIXES because we're suspecting
12347 X server bug, see https://debbugs.gnu.org/cgi/bugreport.cgi?bug=17609. */
12348 if (egetenv ("EMACS_XFIXES") && x_probe_xfixes_extension (dpyinfo->display))
12349 dpyinfo->toggle_visible_pointer = xfixes_toggle_visible_pointer;
12350 else
12352 dpyinfo->toggle_visible_pointer = x_toggle_visible_pointer;
12353 dpyinfo->invisible_cursor = make_invisible_cursor (dpyinfo);
12357 /* Current X display connection identifier. Incremented for each next
12358 connection established. */
12359 static unsigned x_display_id;
12361 /* Open a connection to X display DISPLAY_NAME, and return
12362 the structure that describes the open display.
12363 If we cannot contact the display, return null. */
12365 struct x_display_info *
12366 x_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
12368 Display *dpy;
12369 struct terminal *terminal;
12370 struct x_display_info *dpyinfo;
12371 XrmDatabase xrdb;
12372 #ifdef USE_XCB
12373 xcb_connection_t *xcb_conn;
12374 #endif
12376 block_input ();
12378 if (!x_initialized)
12380 x_initialize ();
12381 ++x_initialized;
12384 if (! x_display_ok (SSDATA (display_name)))
12385 error ("Display %s can't be opened", SSDATA (display_name));
12387 #ifdef USE_GTK
12389 #define NUM_ARGV 10
12390 int argc;
12391 char *argv[NUM_ARGV];
12392 char **argv2 = argv;
12393 guint id;
12395 if (x_initialized++ > 1)
12397 xg_display_open (SSDATA (display_name), &dpy);
12399 else
12401 static char display_opt[] = "--display";
12402 static char name_opt[] = "--name";
12404 for (argc = 0; argc < NUM_ARGV; ++argc)
12405 argv[argc] = 0;
12407 argc = 0;
12408 argv[argc++] = initial_argv[0];
12410 if (! NILP (display_name))
12412 argv[argc++] = display_opt;
12413 argv[argc++] = SSDATA (display_name);
12416 argv[argc++] = name_opt;
12417 argv[argc++] = resource_name;
12419 XSetLocaleModifiers ("");
12421 /* Work around GLib bug that outputs a faulty warning. See
12422 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
12423 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
12424 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
12426 /* NULL window -> events for all windows go to our function.
12427 Call before gtk_init so Gtk+ event filters comes after our. */
12428 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
12430 /* gtk_init does set_locale. Fix locale before and after. */
12431 fixup_locale ();
12432 unrequest_sigio (); /* See comment in x_display_ok. */
12433 gtk_init (&argc, &argv2);
12434 request_sigio ();
12436 g_log_remove_handler ("GLib", id);
12438 xg_initialize ();
12440 /* Do this after the call to xg_initialize, because when
12441 Fontconfig is used, xg_initialize calls its initialization
12442 function which in some versions of Fontconfig calls setlocale. */
12443 fixup_locale ();
12445 dpy = DEFAULT_GDK_DISPLAY ();
12447 #if ! GTK_CHECK_VERSION (2, 90, 0)
12448 /* Load our own gtkrc if it exists. */
12450 const char *file = "~/.emacs.d/gtkrc";
12451 Lisp_Object s, abs_file;
12453 s = build_string (file);
12454 abs_file = Fexpand_file_name (s, Qnil);
12456 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
12457 gtk_rc_parse (SSDATA (abs_file));
12459 #endif
12461 XSetErrorHandler (x_error_handler);
12462 XSetIOErrorHandler (x_io_error_quitter);
12465 #else /* not USE_GTK */
12466 #ifdef USE_X_TOOLKIT
12467 /* weiner@footloose.sps.mot.com reports that this causes
12468 errors with X11R5:
12469 X protocol error: BadAtom (invalid Atom parameter)
12470 on protocol request 18skiloaf.
12471 So let's not use it until R6. */
12472 #ifdef HAVE_X11XTR6
12473 XtSetLanguageProc (NULL, NULL, NULL);
12474 #endif
12477 int argc = 0;
12478 char *argv[3];
12480 argv[0] = (char *) "";
12481 argc = 1;
12482 if (xrm_option)
12484 argv[argc++] = (char *) "-xrm";
12485 argv[argc++] = xrm_option;
12487 turn_on_atimers (false);
12488 unrequest_sigio (); /* See comment in x_display_ok. */
12489 dpy = XtOpenDisplay (Xt_app_con, SSDATA (display_name),
12490 resource_name, EMACS_CLASS,
12491 emacs_options, XtNumber (emacs_options),
12492 &argc, argv);
12493 request_sigio ();
12494 turn_on_atimers (true);
12496 #ifdef HAVE_X11XTR6
12497 /* I think this is to compensate for XtSetLanguageProc. */
12498 fixup_locale ();
12499 #endif
12502 #else /* not USE_X_TOOLKIT */
12503 XSetLocaleModifiers ("");
12504 unrequest_sigio (); /* See comment in x_display_ok. */
12505 dpy = XOpenDisplay (SSDATA (display_name));
12506 request_sigio ();
12507 #endif /* not USE_X_TOOLKIT */
12508 #endif /* not USE_GTK*/
12510 /* Detect failure. */
12511 if (dpy == 0)
12513 unblock_input ();
12514 return 0;
12517 #ifdef USE_XCB
12518 xcb_conn = XGetXCBConnection (dpy);
12519 if (xcb_conn == 0)
12521 #ifdef USE_GTK
12522 xg_display_close (dpy);
12523 #else
12524 #ifdef USE_X_TOOLKIT
12525 XtCloseDisplay (dpy);
12526 #else
12527 XCloseDisplay (dpy);
12528 #endif
12529 #endif /* ! USE_GTK */
12531 unblock_input ();
12532 return 0;
12534 #endif
12536 /* We have definitely succeeded. Record the new connection. */
12538 dpyinfo = xzalloc (sizeof *dpyinfo);
12539 terminal = x_create_terminal (dpyinfo);
12542 struct x_display_info *share;
12544 for (share = x_display_list; share; share = share->next)
12545 if (same_x_server (SSDATA (XCAR (share->name_list_element)),
12546 SSDATA (display_name)))
12547 break;
12548 if (share)
12549 terminal->kboard = share->terminal->kboard;
12550 else
12552 terminal->kboard = allocate_kboard (Qx);
12554 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->u.s.function, Qunbound))
12556 char *vendor = ServerVendor (dpy);
12558 /* Temporarily hide the partially initialized terminal. */
12559 terminal_list = terminal->next_terminal;
12560 unblock_input ();
12561 kset_system_key_alist
12562 (terminal->kboard,
12563 call1 (Qvendor_specific_keysyms,
12564 vendor ? build_string (vendor) : empty_unibyte_string));
12565 block_input ();
12566 terminal->next_terminal = terminal_list;
12567 terminal_list = terminal;
12570 /* Don't let the initial kboard remain current longer than necessary.
12571 That would cause problems if a file loaded on startup tries to
12572 prompt in the mini-buffer. */
12573 if (current_kboard == initial_kboard)
12574 current_kboard = terminal->kboard;
12576 terminal->kboard->reference_count++;
12579 /* Put this display on the chain. */
12580 dpyinfo->next = x_display_list;
12581 x_display_list = dpyinfo;
12583 dpyinfo->name_list_element = Fcons (display_name, Qnil);
12584 dpyinfo->display = dpy;
12585 dpyinfo->connection = ConnectionNumber (dpyinfo->display);
12586 #ifdef USE_XCB
12587 dpyinfo->xcb_connection = xcb_conn;
12588 #endif
12590 /* https://lists.gnu.org/r/emacs-devel/2015-11/msg00194.html */
12591 dpyinfo->smallest_font_height = 1;
12592 dpyinfo->smallest_char_width = 1;
12594 /* Set the name of the terminal. */
12595 terminal->name = xlispstrdup (display_name);
12597 #if false
12598 XSetAfterFunction (x_current_display, x_trace_wire);
12599 #endif
12601 Lisp_Object system_name = Fsystem_name ();
12603 ptrdiff_t nbytes = SBYTES (Vinvocation_name) + 1;
12604 if (STRINGP (system_name)
12605 && INT_ADD_WRAPV (nbytes, SBYTES (system_name) + 1, &nbytes))
12606 memory_full (SIZE_MAX);
12607 dpyinfo->x_id = ++x_display_id;
12608 dpyinfo->x_id_name = xmalloc (nbytes);
12609 char *nametail = lispstpcpy (dpyinfo->x_id_name, Vinvocation_name);
12610 if (STRINGP (system_name))
12612 *nametail++ = '@';
12613 lispstpcpy (nametail, system_name);
12616 /* Figure out which modifier bits mean what. */
12617 x_find_modifier_meanings (dpyinfo);
12619 /* Get the scroll bar cursor. */
12620 #ifdef USE_GTK
12621 /* We must create a GTK cursor, it is required for GTK widgets. */
12622 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
12623 #endif /* USE_GTK */
12625 dpyinfo->vertical_scroll_bar_cursor
12626 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
12628 dpyinfo->horizontal_scroll_bar_cursor
12629 = XCreateFontCursor (dpyinfo->display, XC_sb_h_double_arrow);
12631 xrdb = x_load_resources (dpyinfo->display, xrm_option,
12632 resource_name, EMACS_CLASS);
12633 #ifdef HAVE_XRMSETDATABASE
12634 XrmSetDatabase (dpyinfo->display, xrdb);
12635 #else
12636 dpyinfo->display->db = xrdb;
12637 #endif
12638 /* Put the rdb where we can find it in a way that works on
12639 all versions. */
12640 dpyinfo->xrdb = xrdb;
12642 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
12643 DefaultScreen (dpyinfo->display));
12644 select_visual (dpyinfo);
12645 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
12646 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
12647 dpyinfo->icon_bitmap_id = -1;
12648 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
12650 reset_mouse_highlight (&dpyinfo->mouse_highlight);
12652 /* See if we can construct pixel values from RGB values. */
12653 if (dpyinfo->visual->class == TrueColor)
12655 get_bits_and_offset (dpyinfo->visual->red_mask,
12656 &dpyinfo->red_bits, &dpyinfo->red_offset);
12657 get_bits_and_offset (dpyinfo->visual->blue_mask,
12658 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
12659 get_bits_and_offset (dpyinfo->visual->green_mask,
12660 &dpyinfo->green_bits, &dpyinfo->green_offset);
12663 /* See if a private colormap is requested. */
12664 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
12666 if (dpyinfo->visual->class == PseudoColor)
12668 AUTO_STRING (privateColormap, "privateColormap");
12669 AUTO_STRING (PrivateColormap, "PrivateColormap");
12670 Lisp_Object value
12671 = display_x_get_resource (dpyinfo, privateColormap,
12672 PrivateColormap, Qnil, Qnil);
12673 if (STRINGP (value)
12674 && (!strcmp (SSDATA (value), "true")
12675 || !strcmp (SSDATA (value), "on")))
12676 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
12679 else
12680 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
12681 dpyinfo->visual, AllocNone);
12683 #ifdef HAVE_XDBE
12684 dpyinfo->supports_xdbe = false;
12685 int xdbe_major;
12686 int xdbe_minor;
12687 if (XdbeQueryExtension (dpyinfo->display, &xdbe_major, &xdbe_minor))
12688 dpyinfo->supports_xdbe = true;
12689 #endif
12691 #ifdef HAVE_XFT
12693 /* If we are using Xft, the following precautions should be made:
12695 1. Make sure that the Xrender extension is added before the Xft one.
12696 Otherwise, the close-display hook set by Xft is called after the one
12697 for Xrender, and the former tries to re-add the latter. This results
12698 in inconsistency of internal states and leads to X protocol error when
12699 one reconnects to the same X server (Bug#1696).
12701 2. Check dpi value in X resources. It is better we use it as well,
12702 since Xft will use it, as will all Gnome applications. If our real DPI
12703 is smaller or larger than the one Xft uses, our font will look smaller
12704 or larger than other for other applications, even if it is the same
12705 font name (monospace-10 for example). */
12707 int event_base, error_base;
12708 char *v;
12709 double d;
12711 XRenderQueryExtension (dpyinfo->display, &event_base, &error_base);
12713 v = XGetDefault (dpyinfo->display, "Xft", "dpi");
12714 if (v != NULL && sscanf (v, "%lf", &d) == 1)
12715 dpyinfo->resy = dpyinfo->resx = d;
12717 #endif
12719 if (dpyinfo->resy < 1)
12721 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
12722 double pixels = DisplayHeight (dpyinfo->display, screen_number);
12723 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
12724 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
12725 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
12726 pixels = DisplayWidth (dpyinfo->display, screen_number);
12727 mm = DisplayWidthMM (dpyinfo->display, screen_number);
12728 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
12729 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
12733 static const struct
12735 const char *name;
12736 int offset;
12737 } atom_refs[] = {
12738 #define ATOM_REFS_INIT(string, member) \
12739 { string, offsetof (struct x_display_info, member) },
12740 ATOM_REFS_INIT ("WM_PROTOCOLS", Xatom_wm_protocols)
12741 ATOM_REFS_INIT ("WM_TAKE_FOCUS", Xatom_wm_take_focus)
12742 ATOM_REFS_INIT ("WM_SAVE_YOURSELF", Xatom_wm_save_yourself)
12743 ATOM_REFS_INIT ("WM_DELETE_WINDOW", Xatom_wm_delete_window)
12744 ATOM_REFS_INIT ("WM_CHANGE_STATE", Xatom_wm_change_state)
12745 ATOM_REFS_INIT ("WM_CONFIGURE_DENIED", Xatom_wm_configure_denied)
12746 ATOM_REFS_INIT ("WM_MOVED", Xatom_wm_window_moved)
12747 ATOM_REFS_INIT ("WM_CLIENT_LEADER", Xatom_wm_client_leader)
12748 ATOM_REFS_INIT ("Editres", Xatom_editres)
12749 ATOM_REFS_INIT ("CLIPBOARD", Xatom_CLIPBOARD)
12750 ATOM_REFS_INIT ("TIMESTAMP", Xatom_TIMESTAMP)
12751 ATOM_REFS_INIT ("TEXT", Xatom_TEXT)
12752 ATOM_REFS_INIT ("COMPOUND_TEXT", Xatom_COMPOUND_TEXT)
12753 ATOM_REFS_INIT ("UTF8_STRING", Xatom_UTF8_STRING)
12754 ATOM_REFS_INIT ("DELETE", Xatom_DELETE)
12755 ATOM_REFS_INIT ("MULTIPLE", Xatom_MULTIPLE)
12756 ATOM_REFS_INIT ("INCR", Xatom_INCR)
12757 ATOM_REFS_INIT ("_EMACS_TMP_", Xatom_EMACS_TMP)
12758 ATOM_REFS_INIT ("TARGETS", Xatom_TARGETS)
12759 ATOM_REFS_INIT ("NULL", Xatom_NULL)
12760 ATOM_REFS_INIT ("ATOM", Xatom_ATOM)
12761 ATOM_REFS_INIT ("ATOM_PAIR", Xatom_ATOM_PAIR)
12762 ATOM_REFS_INIT ("CLIPBOARD_MANAGER", Xatom_CLIPBOARD_MANAGER)
12763 ATOM_REFS_INIT ("_XEMBED_INFO", Xatom_XEMBED_INFO)
12764 /* For properties of font. */
12765 ATOM_REFS_INIT ("PIXEL_SIZE", Xatom_PIXEL_SIZE)
12766 ATOM_REFS_INIT ("AVERAGE_WIDTH", Xatom_AVERAGE_WIDTH)
12767 ATOM_REFS_INIT ("_MULE_BASELINE_OFFSET", Xatom_MULE_BASELINE_OFFSET)
12768 ATOM_REFS_INIT ("_MULE_RELATIVE_COMPOSE", Xatom_MULE_RELATIVE_COMPOSE)
12769 ATOM_REFS_INIT ("_MULE_DEFAULT_ASCENT", Xatom_MULE_DEFAULT_ASCENT)
12770 /* Ghostscript support. */
12771 ATOM_REFS_INIT ("DONE", Xatom_DONE)
12772 ATOM_REFS_INIT ("PAGE", Xatom_PAGE)
12773 ATOM_REFS_INIT ("SCROLLBAR", Xatom_Scrollbar)
12774 ATOM_REFS_INIT ("HORIZONTAL_SCROLLBAR", Xatom_Horizontal_Scrollbar)
12775 ATOM_REFS_INIT ("_XEMBED", Xatom_XEMBED)
12776 /* EWMH */
12777 ATOM_REFS_INIT ("_NET_WM_STATE", Xatom_net_wm_state)
12778 ATOM_REFS_INIT ("_NET_WM_STATE_FULLSCREEN", Xatom_net_wm_state_fullscreen)
12779 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_HORZ",
12780 Xatom_net_wm_state_maximized_horz)
12781 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_VERT",
12782 Xatom_net_wm_state_maximized_vert)
12783 ATOM_REFS_INIT ("_NET_WM_STATE_STICKY", Xatom_net_wm_state_sticky)
12784 ATOM_REFS_INIT ("_NET_WM_STATE_HIDDEN", Xatom_net_wm_state_hidden)
12785 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE", Xatom_net_window_type)
12786 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE_TOOLTIP",
12787 Xatom_net_window_type_tooltip)
12788 ATOM_REFS_INIT ("_NET_WM_ICON_NAME", Xatom_net_wm_icon_name)
12789 ATOM_REFS_INIT ("_NET_WM_NAME", Xatom_net_wm_name)
12790 ATOM_REFS_INIT ("_NET_SUPPORTED", Xatom_net_supported)
12791 ATOM_REFS_INIT ("_NET_SUPPORTING_WM_CHECK", Xatom_net_supporting_wm_check)
12792 ATOM_REFS_INIT ("_NET_WM_WINDOW_OPACITY", Xatom_net_wm_window_opacity)
12793 ATOM_REFS_INIT ("_NET_ACTIVE_WINDOW", Xatom_net_active_window)
12794 ATOM_REFS_INIT ("_NET_FRAME_EXTENTS", Xatom_net_frame_extents)
12795 ATOM_REFS_INIT ("_NET_CURRENT_DESKTOP", Xatom_net_current_desktop)
12796 ATOM_REFS_INIT ("_NET_WORKAREA", Xatom_net_workarea)
12797 /* Session management */
12798 ATOM_REFS_INIT ("SM_CLIENT_ID", Xatom_SM_CLIENT_ID)
12799 ATOM_REFS_INIT ("_XSETTINGS_SETTINGS", Xatom_xsettings_prop)
12800 ATOM_REFS_INIT ("MANAGER", Xatom_xsettings_mgr)
12801 ATOM_REFS_INIT ("_NET_WM_STATE_SKIP_TASKBAR", Xatom_net_wm_state_skip_taskbar)
12802 ATOM_REFS_INIT ("_NET_WM_STATE_ABOVE", Xatom_net_wm_state_above)
12803 ATOM_REFS_INIT ("_NET_WM_STATE_BELOW", Xatom_net_wm_state_below)
12806 int i;
12807 enum { atom_count = ARRAYELTS (atom_refs) };
12808 /* 1 for _XSETTINGS_SN. */
12809 enum { total_atom_count = 1 + atom_count };
12810 Atom atoms_return[total_atom_count];
12811 char *atom_names[total_atom_count];
12812 static char const xsettings_fmt[] = "_XSETTINGS_S%d";
12813 char xsettings_atom_name[sizeof xsettings_fmt - 2
12814 + INT_STRLEN_BOUND (int)];
12816 for (i = 0; i < atom_count; i++)
12817 atom_names[i] = (char *) atom_refs[i].name;
12819 /* Build _XSETTINGS_SN atom name. */
12820 sprintf (xsettings_atom_name, xsettings_fmt,
12821 XScreenNumberOfScreen (dpyinfo->screen));
12822 atom_names[i] = xsettings_atom_name;
12824 XInternAtoms (dpyinfo->display, atom_names, total_atom_count,
12825 False, atoms_return);
12827 for (i = 0; i < atom_count; i++)
12828 *(Atom *) ((char *) dpyinfo + atom_refs[i].offset) = atoms_return[i];
12830 /* Manually copy last atom. */
12831 dpyinfo->Xatom_xsettings_sel = atoms_return[i];
12834 dpyinfo->x_dnd_atoms_size = 8;
12835 dpyinfo->x_dnd_atoms = xmalloc (sizeof *dpyinfo->x_dnd_atoms
12836 * dpyinfo->x_dnd_atoms_size);
12837 dpyinfo->gray
12838 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12839 gray_bits, gray_width, gray_height,
12840 1, 0, 1);
12842 x_setup_pointer_blanking (dpyinfo);
12844 #ifdef HAVE_X_I18N
12845 xim_initialize (dpyinfo, resource_name);
12846 #endif
12848 xsettings_initialize (dpyinfo);
12850 /* This is only needed for distinguishing keyboard and process input. */
12851 if (dpyinfo->connection != 0)
12852 add_keyboard_wait_descriptor (dpyinfo->connection);
12854 #ifdef F_SETOWN
12855 fcntl (dpyinfo->connection, F_SETOWN, getpid ());
12856 #endif /* ! defined (F_SETOWN) */
12858 if (interrupt_input)
12859 init_sigio (dpyinfo->connection);
12861 #ifdef USE_LUCID
12863 XrmValue d, fr, to;
12864 Font font;
12866 dpy = dpyinfo->display;
12867 d.addr = (XPointer)&dpy;
12868 d.size = sizeof (Display *);
12869 fr.addr = (char *) XtDefaultFont;
12870 fr.size = sizeof (XtDefaultFont);
12871 to.size = sizeof (Font *);
12872 to.addr = (XPointer)&font;
12873 x_catch_errors (dpy);
12874 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
12875 emacs_abort ();
12876 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
12877 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
12878 /* Do not free XFontStruct returned by the above call to XQueryFont.
12879 This leads to X protocol errors at XtCloseDisplay (Bug#18403). */
12880 x_uncatch_errors ();
12882 #endif
12884 /* See if we should run in synchronous mode. This is useful
12885 for debugging X code. */
12887 AUTO_STRING (synchronous, "synchronous");
12888 AUTO_STRING (Synchronous, "Synchronous");
12889 Lisp_Object value = display_x_get_resource (dpyinfo, synchronous,
12890 Synchronous, Qnil, Qnil);
12891 if (STRINGP (value)
12892 && (!strcmp (SSDATA (value), "true")
12893 || !strcmp (SSDATA (value), "on")))
12894 XSynchronize (dpyinfo->display, True);
12898 AUTO_STRING (useXIM, "useXIM");
12899 AUTO_STRING (UseXIM, "UseXIM");
12900 Lisp_Object value = display_x_get_resource (dpyinfo, useXIM, UseXIM,
12901 Qnil, Qnil);
12902 #ifdef USE_XIM
12903 if (STRINGP (value)
12904 && (!strcmp (SSDATA (value), "false")
12905 || !strcmp (SSDATA (value), "off")))
12906 use_xim = false;
12907 #else
12908 if (STRINGP (value)
12909 && (!strcmp (SSDATA (value), "true")
12910 || !strcmp (SSDATA (value), "on")))
12911 use_xim = true;
12912 #endif
12915 #ifdef HAVE_X_SM
12916 /* Only do this for the very first display in the Emacs session.
12917 Ignore X session management when Emacs was first started on a
12918 tty or started as a daemon. */
12919 if (terminal->id == 1 && ! IS_DAEMON)
12920 x_session_initialize (dpyinfo);
12921 #endif
12923 #ifdef USE_CAIRO
12924 x_extension_initialize (dpyinfo);
12925 #endif
12927 unblock_input ();
12929 return dpyinfo;
12932 /* Get rid of display DPYINFO, deleting all frames on it,
12933 and without sending any more commands to the X server. */
12935 static void
12936 x_delete_display (struct x_display_info *dpyinfo)
12938 struct terminal *t;
12939 struct color_name_cache_entry *color_entry, *next_color_entry;
12941 /* Close all frames and delete the generic struct terminal for this
12942 X display. */
12943 for (t = terminal_list; t; t = t->next_terminal)
12944 if (t->type == output_x_window && t->display_info.x == dpyinfo)
12946 #ifdef HAVE_X_SM
12947 /* Close X session management when we close its display. */
12948 if (t->id == 1 && x_session_have_connection ())
12949 x_session_close ();
12950 #endif
12951 delete_terminal (t);
12952 break;
12955 if (next_noop_dpyinfo == dpyinfo)
12956 next_noop_dpyinfo = dpyinfo->next;
12958 if (x_display_list == dpyinfo)
12959 x_display_list = dpyinfo->next;
12960 else
12962 struct x_display_info *tail;
12964 for (tail = x_display_list; tail; tail = tail->next)
12965 if (tail->next == dpyinfo)
12966 tail->next = tail->next->next;
12969 for (color_entry = dpyinfo->color_names;
12970 color_entry;
12971 color_entry = next_color_entry)
12973 next_color_entry = color_entry->next;
12974 xfree (color_entry->name);
12975 xfree (color_entry);
12978 xfree (dpyinfo->x_id_name);
12979 xfree (dpyinfo->x_dnd_atoms);
12980 xfree (dpyinfo->color_cells);
12981 xfree (dpyinfo);
12984 #ifdef USE_X_TOOLKIT
12986 /* Atimer callback function for TIMER. Called every 0.1s to process
12987 Xt timeouts, if needed. We must avoid calling XtAppPending as
12988 much as possible because that function does an implicit XFlush
12989 that slows us down. */
12991 static void
12992 x_process_timeouts (struct atimer *timer)
12994 block_input ();
12995 x_timeout_atimer_activated_flag = false;
12996 if (toolkit_scroll_bar_interaction || popup_activated ())
12998 while (XtAppPending (Xt_app_con) & XtIMTimer)
12999 XtAppProcessEvent (Xt_app_con, XtIMTimer);
13000 /* Reactivate the atimer for next time. */
13001 x_activate_timeout_atimer ();
13003 unblock_input ();
13006 /* Install an asynchronous timer that processes Xt timeout events
13007 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
13008 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
13009 function whenever these variables are set. This is necessary
13010 because some widget sets use timeouts internally, for example the
13011 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
13012 processed, these widgets don't behave normally. */
13014 void
13015 x_activate_timeout_atimer (void)
13017 block_input ();
13018 if (!x_timeout_atimer_activated_flag)
13020 struct timespec interval = make_timespec (0, 100 * 1000 * 1000);
13021 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
13022 x_timeout_atimer_activated_flag = true;
13024 unblock_input ();
13027 #endif /* USE_X_TOOLKIT */
13030 /* Set up use of X before we make the first connection. */
13032 static struct redisplay_interface x_redisplay_interface =
13034 x_frame_parm_handlers,
13035 x_produce_glyphs,
13036 x_write_glyphs,
13037 x_insert_glyphs,
13038 x_clear_end_of_line,
13039 x_scroll_run,
13040 x_after_update_window_line,
13041 x_update_window_begin,
13042 x_update_window_end,
13043 x_flip_and_flush,
13044 x_clear_window_mouse_face,
13045 x_get_glyph_overhangs,
13046 x_fix_overlapping_area,
13047 x_draw_fringe_bitmap,
13048 #ifdef USE_CAIRO
13049 x_cr_define_fringe_bitmap,
13050 x_cr_destroy_fringe_bitmap,
13051 #else
13052 0, /* define_fringe_bitmap */
13053 0, /* destroy_fringe_bitmap */
13054 #endif
13055 x_compute_glyph_string_overhangs,
13056 x_draw_glyph_string,
13057 x_define_frame_cursor,
13058 x_clear_frame_area,
13059 x_draw_window_cursor,
13060 x_draw_vertical_window_border,
13061 x_draw_window_divider,
13062 x_shift_glyphs_for_insert, /* Never called; see comment in function. */
13063 x_show_hourglass,
13064 x_hide_hourglass
13068 /* This function is called when the last frame on a display is deleted. */
13069 void
13070 x_delete_terminal (struct terminal *terminal)
13072 struct x_display_info *dpyinfo = terminal->display_info.x;
13074 /* Protect against recursive calls. delete_frame in
13075 delete_terminal calls us back when it deletes our last frame. */
13076 if (!terminal->name)
13077 return;
13079 block_input ();
13080 #ifdef HAVE_X_I18N
13081 /* We must close our connection to the XIM server before closing the
13082 X display. */
13083 if (dpyinfo->xim)
13084 xim_close_dpy (dpyinfo);
13085 #endif
13087 /* Normally, the display is available... */
13088 if (dpyinfo->display)
13090 x_destroy_all_bitmaps (dpyinfo);
13091 XSetCloseDownMode (dpyinfo->display, DestroyAll);
13093 /* Whether or not XCloseDisplay destroys the associated resource
13094 database depends on the version of libX11. To avoid both
13095 crash and memory leak, we dissociate the database from the
13096 display and then destroy dpyinfo->xrdb ourselves.
13098 Unfortunately, the above strategy does not work in some
13099 situations due to a bug in newer versions of libX11: because
13100 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
13101 dpy->db is NULL, XCloseDisplay destroys the associated
13102 database whereas it has not been created by XGetDefault
13103 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
13104 don't destroy the database here in order to avoid the crash
13105 in the above situations for now, though that may cause memory
13106 leaks in other situations. */
13107 #if false
13108 #ifdef HAVE_XRMSETDATABASE
13109 XrmSetDatabase (dpyinfo->display, NULL);
13110 #else
13111 dpyinfo->display->db = NULL;
13112 #endif
13113 /* We used to call XrmDestroyDatabase from x_delete_display, but
13114 some older versions of libX11 crash if we call it after
13115 closing all the displays. */
13116 XrmDestroyDatabase (dpyinfo->xrdb);
13117 #endif
13119 #ifdef USE_GTK
13120 xg_display_close (dpyinfo->display);
13121 #else
13122 #ifdef USE_X_TOOLKIT
13123 XtCloseDisplay (dpyinfo->display);
13124 #else
13125 XCloseDisplay (dpyinfo->display);
13126 #endif
13127 #endif /* ! USE_GTK */
13128 /* Do not close the connection here because it's already closed
13129 by X(t)CloseDisplay (Bug#18403). */
13130 dpyinfo->display = NULL;
13133 /* ...but if called from x_connection_closed, the display may already
13134 be closed and dpyinfo->display was set to 0 to indicate that. Since
13135 X server is most likely gone, explicit close is the only reliable
13136 way to continue and avoid Bug#19147. */
13137 else if (dpyinfo->connection >= 0)
13138 emacs_close (dpyinfo->connection);
13140 /* No more input on this descriptor. */
13141 delete_keyboard_wait_descriptor (dpyinfo->connection);
13142 /* Mark as dead. */
13143 dpyinfo->connection = -1;
13145 x_delete_display (dpyinfo);
13146 unblock_input ();
13149 /* Create a struct terminal, initialize it with the X11 specific
13150 functions and make DISPLAY->TERMINAL point to it. */
13152 static struct terminal *
13153 x_create_terminal (struct x_display_info *dpyinfo)
13155 struct terminal *terminal;
13157 terminal = create_terminal (output_x_window, &x_redisplay_interface);
13159 terminal->display_info.x = dpyinfo;
13160 dpyinfo->terminal = terminal;
13162 /* kboard is initialized in x_term_init. */
13164 terminal->clear_frame_hook = x_clear_frame;
13165 terminal->ins_del_lines_hook = x_ins_del_lines;
13166 terminal->delete_glyphs_hook = x_delete_glyphs;
13167 terminal->ring_bell_hook = XTring_bell;
13168 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
13169 terminal->update_begin_hook = x_update_begin;
13170 terminal->update_end_hook = x_update_end;
13171 terminal->read_socket_hook = XTread_socket;
13172 terminal->frame_up_to_date_hook = XTframe_up_to_date;
13173 terminal->buffer_flipping_unblocked_hook = XTbuffer_flipping_unblocked_hook;
13174 terminal->mouse_position_hook = XTmouse_position;
13175 terminal->frame_rehighlight_hook = XTframe_rehighlight;
13176 terminal->frame_raise_lower_hook = XTframe_raise_lower;
13177 terminal->fullscreen_hook = XTfullscreen_hook;
13178 terminal->menu_show_hook = x_menu_show;
13179 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
13180 terminal->popup_dialog_hook = xw_popup_dialog;
13181 #endif
13182 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13183 terminal->set_horizontal_scroll_bar_hook = XTset_horizontal_scroll_bar;
13184 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13185 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
13186 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
13187 terminal->delete_frame_hook = x_destroy_window;
13188 terminal->delete_terminal_hook = x_delete_terminal;
13189 /* Other hooks are NULL by default. */
13191 return terminal;
13194 static void
13195 x_initialize (void)
13197 baud_rate = 19200;
13199 x_noop_count = 0;
13200 any_help_event_p = false;
13201 ignore_next_mouse_click_timeout = 0;
13203 #ifdef USE_GTK
13204 current_count = -1;
13205 #endif
13207 /* Try to use interrupt input; if we can't, then start polling. */
13208 Fset_input_interrupt_mode (Qt);
13210 #if THREADS_ENABLED
13211 /* This must be called before any other Xlib routines. */
13212 if (XInitThreads () == 0)
13213 fprintf (stderr,
13214 "Warning: An error occurred initializing X11 thread support!\n");
13215 #endif
13217 #ifdef USE_X_TOOLKIT
13218 XtToolkitInitialize ();
13220 Xt_app_con = XtCreateApplicationContext ();
13222 /* Register a converter from strings to pixels, which uses
13223 Emacs' color allocation infrastructure. */
13224 XtAppSetTypeConverter (Xt_app_con,
13225 XtRString, XtRPixel, cvt_string_to_pixel,
13226 cvt_string_to_pixel_args,
13227 XtNumber (cvt_string_to_pixel_args),
13228 XtCacheByDisplay, cvt_pixel_dtor);
13230 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
13231 #endif
13233 #ifdef USE_TOOLKIT_SCROLL_BARS
13234 #ifndef USE_GTK
13235 xaw3d_arrow_scroll = False;
13236 xaw3d_pick_top = True;
13237 #endif
13238 #endif
13240 #ifdef USE_CAIRO
13241 x_cr_init_fringe (&x_redisplay_interface);
13242 #endif
13244 /* Note that there is no real way portable across R3/R4 to get the
13245 original error handler. */
13246 XSetErrorHandler (x_error_handler);
13247 XSetIOErrorHandler (x_io_error_quitter);
13250 #ifdef USE_GTK
13251 void
13252 init_xterm (void)
13254 /* Emacs can handle only core input events, so make sure
13255 Gtk doesn't use Xinput or Xinput2 extensions. */
13256 xputenv ("GDK_CORE_DEVICE_EVENTS=1");
13258 #endif
13260 void
13261 syms_of_xterm (void)
13263 x_error_message = NULL;
13265 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
13266 DEFSYM (Qlatin_1, "latin-1");
13268 #ifdef USE_GTK
13269 xg_default_icon_file = build_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
13270 staticpro (&xg_default_icon_file);
13272 DEFSYM (Qx_gtk_map_stock, "x-gtk-map-stock");
13273 #endif
13275 DEFVAR_BOOL ("x-use-underline-position-properties",
13276 x_use_underline_position_properties,
13277 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
13278 A value of nil means ignore them. If you encounter fonts with bogus
13279 UNDERLINE_POSITION font properties, set this to nil. You can also use
13280 `underline-minimum-offset' to override the font's UNDERLINE_POSITION for
13281 small font display sizes. */);
13282 x_use_underline_position_properties = true;
13283 DEFSYM (Qx_use_underline_position_properties,
13284 "x-use-underline-position-properties");
13286 DEFVAR_BOOL ("x-underline-at-descent-line",
13287 x_underline_at_descent_line,
13288 doc: /* Non-nil means to draw the underline at the same place as the descent line.
13289 (If `line-spacing' is in effect, that moves the underline lower by
13290 that many pixels.)
13291 A value of nil means to draw the underline according to the value of the
13292 variable `x-use-underline-position-properties', which is usually at the
13293 baseline level. The default value is nil. */);
13294 x_underline_at_descent_line = false;
13295 DEFSYM (Qx_underline_at_descent_line, "x-underline-at-descent-line");
13297 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
13298 x_mouse_click_focus_ignore_position,
13299 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
13300 This variable is only used when the window manager requires that you
13301 click on a frame to select it (give it focus). In that case, a value
13302 of nil, means that the selected window and cursor position changes to
13303 reflect the mouse click position, while a non-nil value means that the
13304 selected window or cursor position is preserved. */);
13305 x_mouse_click_focus_ignore_position = false;
13307 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
13308 doc: /* Which toolkit scroll bars Emacs uses, if any.
13309 A value of nil means Emacs doesn't use toolkit scroll bars.
13310 With the X Window system, the value is a symbol describing the
13311 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
13312 With MS Windows or Nextstep, the value is t. */);
13313 #ifdef USE_TOOLKIT_SCROLL_BARS
13314 #ifdef USE_MOTIF
13315 Vx_toolkit_scroll_bars = intern_c_string ("motif");
13316 #elif defined HAVE_XAW3D
13317 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
13318 #elif USE_GTK
13319 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
13320 #else
13321 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
13322 #endif
13323 #else
13324 Vx_toolkit_scroll_bars = Qnil;
13325 #endif
13327 DEFSYM (Qmodifier_value, "modifier-value");
13328 DEFSYM (Qctrl, "ctrl");
13329 Fput (Qctrl, Qmodifier_value, make_number (ctrl_modifier));
13330 DEFSYM (Qalt, "alt");
13331 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
13332 DEFSYM (Qhyper, "hyper");
13333 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
13334 DEFSYM (Qmeta, "meta");
13335 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
13336 DEFSYM (Qsuper, "super");
13337 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
13339 DEFVAR_LISP ("x-ctrl-keysym", Vx_ctrl_keysym,
13340 doc: /* Which keys Emacs uses for the ctrl modifier.
13341 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
13342 `super'. For example, `ctrl' means use the Ctrl_L and Ctrl_R keysyms.
13343 The default is nil, which is the same as `ctrl'. */);
13344 Vx_ctrl_keysym = Qnil;
13346 DEFVAR_LISP ("x-alt-keysym", Vx_alt_keysym,
13347 doc: /* Which keys Emacs uses for the alt modifier.
13348 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
13349 `super'. For example, `alt' means use the Alt_L and Alt_R keysyms.
13350 The default is nil, which is the same as `alt'. */);
13351 Vx_alt_keysym = Qnil;
13353 DEFVAR_LISP ("x-hyper-keysym", Vx_hyper_keysym,
13354 doc: /* Which keys Emacs uses for the hyper modifier.
13355 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
13356 `super'. For example, `hyper' means use the Hyper_L and Hyper_R
13357 keysyms. The default is nil, which is the same as `hyper'. */);
13358 Vx_hyper_keysym = Qnil;
13360 DEFVAR_LISP ("x-meta-keysym", Vx_meta_keysym,
13361 doc: /* Which keys Emacs uses for the meta modifier.
13362 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
13363 `super'. For example, `meta' means use the Meta_L and Meta_R keysyms.
13364 The default is nil, which is the same as `meta'. */);
13365 Vx_meta_keysym = Qnil;
13367 DEFVAR_LISP ("x-super-keysym", Vx_super_keysym,
13368 doc: /* Which keys Emacs uses for the super modifier.
13369 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
13370 `super'. For example, `super' means use the Super_L and Super_R
13371 keysyms. The default is nil, which is the same as `super'. */);
13372 Vx_super_keysym = Qnil;
13374 DEFVAR_LISP ("x-wait-for-event-timeout", Vx_wait_for_event_timeout,
13375 doc: /* How long to wait for X events.
13377 Emacs will wait up to this many seconds to receive X events after
13378 making changes which affect the state of the graphical interface.
13379 Under some window managers this can take an indefinite amount of time,
13380 so it is important to limit the wait.
13382 If set to a non-float value, there will be no wait at all. */);
13383 Vx_wait_for_event_timeout = make_float (0.1);
13385 DEFVAR_LISP ("x-keysym-table", Vx_keysym_table,
13386 doc: /* Hash table of character codes indexed by X keysym codes. */);
13387 Vx_keysym_table = make_hash_table (hashtest_eql, 900,
13388 DEFAULT_REHASH_SIZE,
13389 DEFAULT_REHASH_THRESHOLD,
13390 Qnil, false);
13392 DEFVAR_BOOL ("x-frame-normalize-before-maximize",
13393 x_frame_normalize_before_maximize,
13394 doc: /* Non-nil means normalize frame before maximizing.
13395 If this variable is t, Emacs first asks the window manager to give the
13396 frame its normal size, and only then the final state, whenever changing
13397 from a full-height, full-width or full-both state to the maximized one
13398 or when changing from the maximized to the full-height or full-width
13399 state.
13401 Set this variable only if your window manager cannot handle the
13402 transition between the various maximization states. */);
13403 x_frame_normalize_before_maximize = false;
13405 DEFVAR_BOOL ("x-gtk-use-window-move", x_gtk_use_window_move,
13406 doc: /* Non-nil means rely on gtk_window_move to set frame positions.
13407 If this variable is t (the default), the GTK build uses the function
13408 gtk_window_move to set or store frame positions and disables some time
13409 consuming frame position adjustments. In newer versions of GTK, Emacs
13410 always uses gtk_window_move and ignores the value of this variable. */);
13411 x_gtk_use_window_move = true;