; Typo fixes, mostly repeated words
[emacs.git] / src / xterm.c
blobd90654b101d740892f365f793e95e5dd4c2de2e8
1 /* X Communication module for terminals which understand the X protocol.
3 Copyright (C) 1989, 1993-2017 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)
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 cairo_surface_t *surface;
364 surface = cairo_xlib_surface_create (FRAME_X_DISPLAY (f),
365 FRAME_X_DRAWABLE (f),
366 FRAME_DISPLAY_INFO (f)->visual,
367 FRAME_PIXEL_WIDTH (f),
368 FRAME_PIXEL_HEIGHT (f));
369 cr = cairo_create (surface);
370 cairo_surface_destroy (surface);
372 else
373 cr = cairo_create (FRAME_CR_SURFACE (f));
374 FRAME_CR_CONTEXT (f) = cr;
376 cairo_save (cr);
378 if (gc)
380 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 0);
382 if (gc_ext && gc_ext->n_clip_rects)
384 int i;
386 for (i = 0; i < gc_ext->n_clip_rects; i++)
387 cairo_rectangle (cr, gc_ext->clip_rects[i].x,
388 gc_ext->clip_rects[i].y,
389 gc_ext->clip_rects[i].width,
390 gc_ext->clip_rects[i].height);
391 cairo_clip (cr);
395 return cr;
398 void
399 x_end_cr_clip (struct frame *f)
401 cairo_restore (FRAME_CR_CONTEXT (f));
404 void
405 x_set_cr_source_with_gc_foreground (struct frame *f, GC gc)
407 XGCValues xgcv;
408 XColor color;
410 XGetGCValues (FRAME_X_DISPLAY (f), gc, GCForeground, &xgcv);
411 color.pixel = xgcv.foreground;
412 x_query_color (f, &color);
413 cairo_set_source_rgb (FRAME_CR_CONTEXT (f), color.red / 65535.0,
414 color.green / 65535.0, color.blue / 65535.0);
417 void
418 x_set_cr_source_with_gc_background (struct frame *f, GC gc)
420 XGCValues xgcv;
421 XColor color;
423 XGetGCValues (FRAME_X_DISPLAY (f), gc, GCBackground, &xgcv);
424 color.pixel = xgcv.background;
425 x_query_color (f, &color);
426 cairo_set_source_rgb (FRAME_CR_CONTEXT (f), color.red / 65535.0,
427 color.green / 65535.0, color.blue / 65535.0);
430 /* Fringe bitmaps. */
432 static int max_fringe_bmp = 0;
433 static cairo_pattern_t **fringe_bmp = 0;
435 static void
436 x_cr_define_fringe_bitmap (int which, unsigned short *bits, int h, int wd)
438 int i, stride;
439 cairo_surface_t *surface;
440 unsigned char *data;
441 cairo_pattern_t *pattern;
443 if (which >= max_fringe_bmp)
445 i = max_fringe_bmp;
446 max_fringe_bmp = which + 20;
447 fringe_bmp = (cairo_pattern_t **) xrealloc (fringe_bmp, max_fringe_bmp * sizeof (cairo_pattern_t *));
448 while (i < max_fringe_bmp)
449 fringe_bmp[i++] = 0;
452 block_input ();
454 surface = cairo_image_surface_create (CAIRO_FORMAT_A1, wd, h);
455 stride = cairo_image_surface_get_stride (surface);
456 data = cairo_image_surface_get_data (surface);
458 for (i = 0; i < h; i++)
460 *((unsigned short *) data) = bits[i];
461 data += stride;
464 cairo_surface_mark_dirty (surface);
465 pattern = cairo_pattern_create_for_surface (surface);
466 cairo_surface_destroy (surface);
468 unblock_input ();
470 fringe_bmp[which] = pattern;
473 static void
474 x_cr_destroy_fringe_bitmap (int which)
476 if (which >= max_fringe_bmp)
477 return;
479 if (fringe_bmp[which])
481 block_input ();
482 cairo_pattern_destroy (fringe_bmp[which]);
483 unblock_input ();
485 fringe_bmp[which] = 0;
488 static void
489 x_cr_draw_image (struct frame *f, GC gc, cairo_pattern_t *image,
490 int src_x, int src_y, int width, int height,
491 int dest_x, int dest_y, bool overlay_p)
493 cairo_t *cr;
494 cairo_matrix_t matrix;
495 cairo_surface_t *surface;
496 cairo_format_t format;
498 cr = x_begin_cr_clip (f, gc);
499 if (overlay_p)
500 cairo_rectangle (cr, dest_x, dest_y, width, height);
501 else
503 x_set_cr_source_with_gc_background (f, gc);
504 cairo_rectangle (cr, dest_x, dest_y, width, height);
505 cairo_fill_preserve (cr);
507 cairo_clip (cr);
508 cairo_matrix_init_translate (&matrix, src_x - dest_x, src_y - dest_y);
509 cairo_pattern_set_matrix (image, &matrix);
510 cairo_pattern_get_surface (image, &surface);
511 format = cairo_image_surface_get_format (surface);
512 if (format != CAIRO_FORMAT_A8 && format != CAIRO_FORMAT_A1)
514 cairo_set_source (cr, image);
515 cairo_fill (cr);
517 else
519 x_set_cr_source_with_gc_foreground (f, gc);
520 cairo_mask (cr, image);
522 x_end_cr_clip (f);
525 void
526 x_cr_draw_frame (cairo_t *cr, struct frame *f)
528 int width, height;
530 width = FRAME_PIXEL_WIDTH (f);
531 height = FRAME_PIXEL_HEIGHT (f);
533 x_free_cr_resources (f);
534 FRAME_CR_CONTEXT (f) = cr;
535 x_clear_area (f, 0, 0, width, height);
536 expose_frame (f, 0, 0, width, height);
537 FRAME_CR_CONTEXT (f) = NULL;
540 static cairo_status_t
541 x_cr_accumulate_data (void *closure, const unsigned char *data,
542 unsigned int length)
544 Lisp_Object *acc = (Lisp_Object *) closure;
546 *acc = Fcons (make_unibyte_string ((char const *) data, length), *acc);
548 return CAIRO_STATUS_SUCCESS;
551 static void
552 x_cr_destroy (Lisp_Object arg)
554 cairo_t *cr = (cairo_t *) XSAVE_POINTER (arg, 0);
556 block_input ();
557 cairo_destroy (cr);
558 unblock_input ();
561 Lisp_Object
562 x_cr_export_frames (Lisp_Object frames, cairo_surface_type_t surface_type)
564 struct frame *f;
565 cairo_surface_t *surface;
566 cairo_t *cr;
567 int width, height;
568 void (*surface_set_size_func) (cairo_surface_t *, double, double) = NULL;
569 Lisp_Object acc = Qnil;
570 ptrdiff_t count = SPECPDL_INDEX ();
572 specbind (Qredisplay_dont_pause, Qt);
573 redisplay_preserve_echo_area (31);
575 f = XFRAME (XCAR (frames));
576 frames = XCDR (frames);
577 width = FRAME_PIXEL_WIDTH (f);
578 height = FRAME_PIXEL_HEIGHT (f);
580 block_input ();
581 #ifdef CAIRO_HAS_PDF_SURFACE
582 if (surface_type == CAIRO_SURFACE_TYPE_PDF)
584 surface = cairo_pdf_surface_create_for_stream (x_cr_accumulate_data, &acc,
585 width, height);
586 surface_set_size_func = cairo_pdf_surface_set_size;
588 else
589 #endif
590 #ifdef CAIRO_HAS_PNG_FUNCTIONS
591 if (surface_type == CAIRO_SURFACE_TYPE_IMAGE)
592 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
593 else
594 #endif
595 #ifdef CAIRO_HAS_PS_SURFACE
596 if (surface_type == CAIRO_SURFACE_TYPE_PS)
598 surface = cairo_ps_surface_create_for_stream (x_cr_accumulate_data, &acc,
599 width, height);
600 surface_set_size_func = cairo_ps_surface_set_size;
602 else
603 #endif
604 #ifdef CAIRO_HAS_SVG_SURFACE
605 if (surface_type == CAIRO_SURFACE_TYPE_SVG)
606 surface = cairo_svg_surface_create_for_stream (x_cr_accumulate_data, &acc,
607 width, height);
608 else
609 #endif
610 abort ();
612 cr = cairo_create (surface);
613 cairo_surface_destroy (surface);
614 record_unwind_protect (x_cr_destroy, make_save_ptr (cr));
616 while (1)
618 x_free_cr_resources (f);
619 FRAME_CR_CONTEXT (f) = cr;
620 x_clear_area (f, 0, 0, width, height);
621 expose_frame (f, 0, 0, width, height);
622 FRAME_CR_CONTEXT (f) = NULL;
624 if (NILP (frames))
625 break;
627 cairo_surface_show_page (surface);
628 f = XFRAME (XCAR (frames));
629 frames = XCDR (frames);
630 width = FRAME_PIXEL_WIDTH (f);
631 height = FRAME_PIXEL_HEIGHT (f);
632 if (surface_set_size_func)
633 (*surface_set_size_func) (surface, width, height);
635 unblock_input ();
636 maybe_quit ();
637 block_input ();
640 #ifdef CAIRO_HAS_PNG_FUNCTIONS
641 if (surface_type == CAIRO_SURFACE_TYPE_IMAGE)
643 cairo_surface_flush (surface);
644 cairo_surface_write_to_png_stream (surface, x_cr_accumulate_data, &acc);
646 #endif
647 unblock_input ();
649 unbind_to (count, Qnil);
651 return CALLN (Fapply, intern ("concat"), Fnreverse (acc));
654 #endif /* USE_CAIRO */
656 static void
657 x_free_cr_resources (struct frame *f)
659 #ifdef USE_CAIRO
660 if (f == NULL)
662 Lisp_Object rest, frame;
663 FOR_EACH_FRAME (rest, frame)
664 if (FRAME_X_P (XFRAME (frame)))
665 x_free_cr_resources (XFRAME (frame));
667 else
669 cairo_t *cr = FRAME_CR_CONTEXT (f);
671 if (cr)
673 cairo_surface_t *surface = cairo_get_target (cr);
675 if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_XLIB)
677 cairo_destroy (cr);
678 FRAME_CR_CONTEXT (f) = NULL;
682 #endif
685 static void
686 x_set_clip_rectangles (struct frame *f, GC gc, XRectangle *rectangles, int n)
688 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, rectangles, n, Unsorted);
689 #ifdef USE_CAIRO
690 eassert (n >= 0 && n <= MAX_CLIP_RECTS);
693 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 1);
695 gc_ext->n_clip_rects = n;
696 memcpy (gc_ext->clip_rects, rectangles, sizeof (XRectangle) * n);
698 #endif
701 static void
702 x_reset_clip_rectangles (struct frame *f, GC gc)
704 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
705 #ifdef USE_CAIRO
707 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 0);
709 if (gc_ext)
710 gc_ext->n_clip_rects = 0;
712 #endif
715 static void
716 x_fill_rectangle (struct frame *f, GC gc, int x, int y, int width, int height)
718 #ifdef USE_CAIRO
719 cairo_t *cr;
721 cr = x_begin_cr_clip (f, gc);
722 x_set_cr_source_with_gc_foreground (f, gc);
723 cairo_rectangle (cr, x, y, width, height);
724 cairo_fill (cr);
725 x_end_cr_clip (f);
726 #else
727 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
728 gc, x, y, width, height);
729 #endif
732 static void
733 x_draw_rectangle (struct frame *f, GC gc, int x, int y, int width, int height)
735 #ifdef USE_CAIRO
736 cairo_t *cr;
738 cr = x_begin_cr_clip (f, gc);
739 x_set_cr_source_with_gc_foreground (f, gc);
740 cairo_rectangle (cr, x + 0.5, y + 0.5, width, height);
741 cairo_set_line_width (cr, 1);
742 cairo_stroke (cr);
743 x_end_cr_clip (f);
744 #else
745 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
746 gc, x, y, width, height);
747 #endif
750 static void
751 x_clear_window (struct frame *f)
753 #ifdef USE_CAIRO
754 cairo_t *cr;
756 cr = x_begin_cr_clip (f, NULL);
757 x_set_cr_source_with_gc_background (f, f->output_data.x->normal_gc);
758 cairo_paint (cr);
759 x_end_cr_clip (f);
760 #else
761 if (FRAME_X_DOUBLE_BUFFERED_P (f))
762 x_clear_area (f, 0, 0, FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f));
763 else
764 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
765 #endif
768 #ifdef USE_CAIRO
769 static void
770 x_fill_trapezoid_for_relief (struct frame *f, GC gc, int x, int y,
771 int width, int height, int top_p)
773 cairo_t *cr;
775 cr = x_begin_cr_clip (f, gc);
776 x_set_cr_source_with_gc_foreground (f, gc);
777 cairo_move_to (cr, top_p ? x : x + height, y);
778 cairo_line_to (cr, x, y + height);
779 cairo_line_to (cr, top_p ? x + width - height : x + width, y + height);
780 cairo_line_to (cr, x + width, y);
781 cairo_fill (cr);
782 x_end_cr_clip (f);
785 enum corners
787 CORNER_BOTTOM_RIGHT, /* 0 -> pi/2 */
788 CORNER_BOTTOM_LEFT, /* pi/2 -> pi */
789 CORNER_TOP_LEFT, /* pi -> 3pi/2 */
790 CORNER_TOP_RIGHT, /* 3pi/2 -> 2pi */
791 CORNER_LAST
794 static void
795 x_erase_corners_for_relief (struct frame *f, GC gc, int x, int y,
796 int width, int height,
797 double radius, double margin, int corners)
799 cairo_t *cr;
800 int i;
802 cr = x_begin_cr_clip (f, gc);
803 x_set_cr_source_with_gc_background (f, gc);
804 for (i = 0; i < CORNER_LAST; i++)
805 if (corners & (1 << i))
807 double xm, ym, xc, yc;
809 if (i == CORNER_TOP_LEFT || i == CORNER_BOTTOM_LEFT)
810 xm = x - margin, xc = xm + radius;
811 else
812 xm = x + width + margin, xc = xm - radius;
813 if (i == CORNER_TOP_LEFT || i == CORNER_TOP_RIGHT)
814 ym = y - margin, yc = ym + radius;
815 else
816 ym = y + height + margin, yc = ym - radius;
818 cairo_move_to (cr, xm, ym);
819 cairo_arc (cr, xc, yc, radius, i * M_PI_2, (i + 1) * M_PI_2);
821 cairo_clip (cr);
822 cairo_rectangle (cr, x, y, width, height);
823 cairo_fill (cr);
824 x_end_cr_clip (f);
827 static void
828 x_draw_horizontal_wave (struct frame *f, GC gc, int x, int y,
829 int width, int height, int wave_length)
831 cairo_t *cr;
832 double dx = wave_length, dy = height - 1;
833 int xoffset, n;
835 cr = x_begin_cr_clip (f, gc);
836 x_set_cr_source_with_gc_foreground (f, gc);
837 cairo_rectangle (cr, x, y, width, height);
838 cairo_clip (cr);
840 if (x >= 0)
842 xoffset = x % (wave_length * 2);
843 if (xoffset == 0)
844 xoffset = wave_length * 2;
846 else
847 xoffset = x % (wave_length * 2) + wave_length * 2;
848 n = (width + xoffset) / wave_length + 1;
849 if (xoffset > wave_length)
851 xoffset -= wave_length;
852 --n;
853 y += height - 1;
854 dy = -dy;
857 cairo_move_to (cr, x - xoffset + 0.5, y + 0.5);
858 while (--n >= 0)
860 cairo_rel_line_to (cr, dx, dy);
861 dy = -dy;
863 cairo_set_line_width (cr, 1);
864 cairo_stroke (cr);
865 x_end_cr_clip (f);
867 #endif
870 /* Return the struct x_display_info corresponding to DPY. */
872 struct x_display_info *
873 x_display_info_for_display (Display *dpy)
875 struct x_display_info *dpyinfo;
877 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
878 if (dpyinfo->display == dpy)
879 return dpyinfo;
881 return 0;
884 static Window
885 x_find_topmost_parent (struct frame *f)
887 struct x_output *x = f->output_data.x;
888 Window win = None, wi = x->parent_desc;
889 Display *dpy = FRAME_X_DISPLAY (f);
891 while (wi != FRAME_DISPLAY_INFO (f)->root_window)
893 Window root;
894 Window *children;
895 unsigned int nchildren;
897 win = wi;
898 if (XQueryTree (dpy, win, &root, &wi, &children, &nchildren))
899 XFree (children);
900 else
901 break;
904 return win;
907 #define OPAQUE 0xffffffff
909 void
910 x_set_frame_alpha (struct frame *f)
912 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
913 Display *dpy = FRAME_X_DISPLAY (f);
914 Window win = FRAME_OUTER_WINDOW (f);
915 double alpha = 1.0;
916 double alpha_min = 1.0;
917 unsigned long opac;
918 Window parent;
920 if (dpyinfo->x_highlight_frame == f)
921 alpha = f->alpha[0];
922 else
923 alpha = f->alpha[1];
925 if (FLOATP (Vframe_alpha_lower_limit))
926 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
927 else if (INTEGERP (Vframe_alpha_lower_limit))
928 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
930 if (alpha < 0.0)
931 return;
932 else if (alpha > 1.0)
933 alpha = 1.0;
934 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
935 alpha = alpha_min;
937 opac = alpha * OPAQUE;
939 x_catch_errors (dpy);
941 /* If there is a parent from the window manager, put the property there
942 also, to work around broken window managers that fail to do that.
943 Do this unconditionally as this function is called on reparent when
944 alpha has not changed on the frame. */
946 if (!FRAME_PARENT_FRAME (f))
948 parent = x_find_topmost_parent (f);
949 if (parent != None)
950 XChangeProperty (dpy, parent, dpyinfo->Xatom_net_wm_window_opacity,
951 XA_CARDINAL, 32, PropModeReplace,
952 (unsigned char *) &opac, 1);
955 /* return unless necessary */
957 unsigned char *data;
958 Atom actual;
959 int rc, format;
960 unsigned long n, left;
962 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
963 0, 1, False, XA_CARDINAL,
964 &actual, &format, &n, &left,
965 &data);
967 if (rc == Success && actual != None)
969 unsigned long value = *(unsigned long *)data;
970 XFree (data);
971 if (value == opac)
973 x_uncatch_errors ();
974 return;
979 XChangeProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
980 XA_CARDINAL, 32, PropModeReplace,
981 (unsigned char *) &opac, 1);
982 x_uncatch_errors ();
985 /***********************************************************************
986 Starting and ending an update
987 ***********************************************************************/
989 /* Start an update of frame F. This function is installed as a hook
990 for update_begin, i.e. it is called when update_begin is called.
991 This function is called prior to calls to x_update_window_begin for
992 each window being updated. Currently, there is nothing to do here
993 because all interesting stuff is done on a window basis. */
995 static void
996 x_update_begin (struct frame *f)
998 #ifdef USE_CAIRO
999 if (! NILP (tip_frame) && XFRAME (tip_frame) == f
1000 && ! FRAME_VISIBLE_P (f))
1001 return;
1003 if (! FRAME_CR_SURFACE (f))
1005 int width, height;
1006 #ifdef USE_GTK
1007 if (FRAME_GTK_WIDGET (f))
1009 GdkWindow *w = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
1010 width = gdk_window_get_width (w);
1011 height = gdk_window_get_height (w);
1013 else
1014 #endif
1016 width = FRAME_PIXEL_WIDTH (f);
1017 height = FRAME_PIXEL_HEIGHT (f);
1018 if (! FRAME_EXTERNAL_TOOL_BAR (f))
1019 height += FRAME_TOOL_BAR_HEIGHT (f);
1020 if (! FRAME_EXTERNAL_MENU_BAR (f))
1021 height += FRAME_MENU_BAR_HEIGHT (f);
1024 if (width > 0 && height > 0)
1026 block_input();
1027 FRAME_CR_SURFACE (f) = cairo_image_surface_create
1028 (CAIRO_FORMAT_ARGB32, width, height);
1029 unblock_input();
1032 #endif /* USE_CAIRO */
1035 /* Start update of window W. */
1037 static void
1038 x_update_window_begin (struct window *w)
1040 struct frame *f = XFRAME (WINDOW_FRAME (w));
1041 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
1043 w->output_cursor = w->cursor;
1045 block_input ();
1047 if (f == hlinfo->mouse_face_mouse_frame)
1049 /* Don't do highlighting for mouse motion during the update. */
1050 hlinfo->mouse_face_defer = true;
1052 /* If F needs to be redrawn, simply forget about any prior mouse
1053 highlighting. */
1054 if (FRAME_GARBAGED_P (f))
1055 hlinfo->mouse_face_window = Qnil;
1058 unblock_input ();
1062 /* Draw a vertical window border from (x,y0) to (x,y1) */
1064 static void
1065 x_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
1067 struct frame *f = XFRAME (WINDOW_FRAME (w));
1068 struct face *face;
1070 face = FACE_FROM_ID_OR_NULL (f, VERTICAL_BORDER_FACE_ID);
1071 if (face)
1072 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
1073 face->foreground);
1075 #ifdef USE_CAIRO
1076 x_fill_rectangle (f, f->output_data.x->normal_gc, x, y0, 1, y1 - y0);
1077 #else
1078 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
1079 f->output_data.x->normal_gc, x, y0, x, y1);
1080 #endif
1083 /* Draw a window divider from (x0,y0) to (x1,y1) */
1085 static void
1086 x_draw_window_divider (struct window *w, int x0, int x1, int y0, int y1)
1088 struct frame *f = XFRAME (WINDOW_FRAME (w));
1089 struct face *face = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_FACE_ID);
1090 struct face *face_first
1091 = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
1092 struct face *face_last
1093 = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
1094 unsigned long color = face ? face->foreground : FRAME_FOREGROUND_PIXEL (f);
1095 unsigned long color_first = (face_first
1096 ? face_first->foreground
1097 : FRAME_FOREGROUND_PIXEL (f));
1098 unsigned long color_last = (face_last
1099 ? face_last->foreground
1100 : FRAME_FOREGROUND_PIXEL (f));
1101 Display *display = FRAME_X_DISPLAY (f);
1103 if (y1 - y0 > x1 - x0 && x1 - x0 > 2)
1104 /* Vertical. */
1106 XSetForeground (display, f->output_data.x->normal_gc, color_first);
1107 x_fill_rectangle (f, f->output_data.x->normal_gc,
1108 x0, y0, 1, y1 - y0);
1109 XSetForeground (display, f->output_data.x->normal_gc, color);
1110 x_fill_rectangle (f, f->output_data.x->normal_gc,
1111 x0 + 1, y0, x1 - x0 - 2, y1 - y0);
1112 XSetForeground (display, f->output_data.x->normal_gc, color_last);
1113 x_fill_rectangle (f, f->output_data.x->normal_gc,
1114 x1 - 1, y0, 1, y1 - y0);
1116 else if (x1 - x0 > y1 - y0 && y1 - y0 > 3)
1117 /* Horizontal. */
1119 XSetForeground (display, f->output_data.x->normal_gc, color_first);
1120 x_fill_rectangle (f, f->output_data.x->normal_gc,
1121 x0, y0, x1 - x0, 1);
1122 XSetForeground (display, f->output_data.x->normal_gc, color);
1123 x_fill_rectangle (f, f->output_data.x->normal_gc,
1124 x0, y0 + 1, x1 - x0, y1 - y0 - 2);
1125 XSetForeground (display, f->output_data.x->normal_gc, color_last);
1126 x_fill_rectangle (f, f->output_data.x->normal_gc,
1127 x0, y1 - 1, x1 - x0, 1);
1129 else
1131 XSetForeground (display, f->output_data.x->normal_gc, color);
1132 x_fill_rectangle (f, f->output_data.x->normal_gc,
1133 x0, y0, x1 - x0, y1 - y0);
1137 /* End update of window W.
1139 Draw vertical borders between horizontally adjacent windows, and
1140 display W's cursor if CURSOR_ON_P is non-zero.
1142 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
1143 glyphs in mouse-face were overwritten. In that case we have to
1144 make sure that the mouse-highlight is properly redrawn.
1146 W may be a menu bar pseudo-window in case we don't have X toolkit
1147 support. Such windows don't have a cursor, so don't display it
1148 here. */
1150 static void
1151 x_update_window_end (struct window *w, bool cursor_on_p,
1152 bool mouse_face_overwritten_p)
1154 if (!w->pseudo_window_p)
1156 block_input ();
1158 if (cursor_on_p)
1159 display_and_set_cursor (w, true,
1160 w->output_cursor.hpos, w->output_cursor.vpos,
1161 w->output_cursor.x, w->output_cursor.y);
1163 if (draw_window_fringes (w, true))
1165 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
1166 x_draw_right_divider (w);
1167 else
1168 x_draw_vertical_border (w);
1171 unblock_input ();
1174 /* If a row with mouse-face was overwritten, arrange for
1175 XTframe_up_to_date to redisplay the mouse highlight. */
1176 if (mouse_face_overwritten_p)
1178 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
1180 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
1181 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
1182 hlinfo->mouse_face_window = Qnil;
1186 /* Show the frame back buffer. If frame is double-buffered,
1187 atomically publish to the user's screen graphics updates made since
1188 the last call to show_back_buffer. */
1189 static void
1190 show_back_buffer (struct frame *f)
1192 block_input ();
1193 if (FRAME_X_DOUBLE_BUFFERED_P (f))
1195 #ifdef HAVE_XDBE
1196 XdbeSwapInfo swap_info;
1197 memset (&swap_info, 0, sizeof (swap_info));
1198 swap_info.swap_window = FRAME_X_WINDOW (f);
1199 swap_info.swap_action = XdbeCopied;
1200 XdbeSwapBuffers (FRAME_X_DISPLAY (f), &swap_info, 1);
1201 #else
1202 eassert (!"should have back-buffer only with XDBE");
1203 #endif
1205 FRAME_X_NEED_BUFFER_FLIP (f) = false;
1206 unblock_input ();
1209 /* Updates back buffer and flushes changes to display. Called from
1210 minibuf read code. Note that we display the back buffer even if
1211 buffer flipping is blocked. */
1212 static void
1213 x_flip_and_flush (struct frame *f)
1215 block_input ();
1216 if (FRAME_X_NEED_BUFFER_FLIP (f))
1217 show_back_buffer (f);
1218 x_flush (f);
1219 unblock_input ();
1222 /* End update of frame F. This function is installed as a hook in
1223 update_end. */
1225 static void
1226 x_update_end (struct frame *f)
1228 /* Mouse highlight may be displayed again. */
1229 MOUSE_HL_INFO (f)->mouse_face_defer = false;
1231 #ifdef USE_CAIRO
1232 if (FRAME_CR_SURFACE (f))
1234 cairo_t *cr = 0;
1235 block_input();
1236 #if defined (USE_GTK) && defined (HAVE_GTK3)
1237 if (FRAME_GTK_WIDGET (f))
1239 GdkWindow *w = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
1240 cr = gdk_cairo_create (w);
1242 else
1243 #endif
1245 cairo_surface_t *surface;
1246 int width = FRAME_PIXEL_WIDTH (f);
1247 int height = FRAME_PIXEL_HEIGHT (f);
1248 if (! FRAME_EXTERNAL_TOOL_BAR (f))
1249 height += FRAME_TOOL_BAR_HEIGHT (f);
1250 if (! FRAME_EXTERNAL_MENU_BAR (f))
1251 height += FRAME_MENU_BAR_HEIGHT (f);
1252 surface = cairo_xlib_surface_create (FRAME_X_DISPLAY (f),
1253 FRAME_X_DRAWABLE (f),
1254 FRAME_DISPLAY_INFO (f)->visual,
1255 width,
1256 height);
1257 cr = cairo_create (surface);
1258 cairo_surface_destroy (surface);
1261 cairo_set_source_surface (cr, FRAME_CR_SURFACE (f), 0, 0);
1262 cairo_paint (cr);
1263 cairo_destroy (cr);
1264 unblock_input ();
1266 #endif
1268 #ifndef XFlush
1269 block_input ();
1270 XFlush (FRAME_X_DISPLAY (f));
1271 unblock_input ();
1272 #endif
1275 /* This function is called from various places in xdisp.c
1276 whenever a complete update has been performed. */
1278 static void
1279 XTframe_up_to_date (struct frame *f)
1281 eassert (FRAME_X_P (f));
1282 block_input ();
1283 FRAME_MOUSE_UPDATE (f);
1284 if (!buffer_flipping_blocked_p () && FRAME_X_NEED_BUFFER_FLIP (f))
1285 show_back_buffer (f);
1286 unblock_input ();
1289 static void
1290 XTbuffer_flipping_unblocked_hook (struct frame *f)
1292 if (FRAME_X_NEED_BUFFER_FLIP (f))
1293 show_back_buffer (f);
1297 * x_clear_under_internal_border:
1299 * Clear area of frame F's internal border. If the internal border face
1300 * of F has been specified (is not null), fill the area with that face.
1302 void
1303 x_clear_under_internal_border (struct frame *f)
1305 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
1307 int border = FRAME_INTERNAL_BORDER_WIDTH (f);
1308 int width = FRAME_PIXEL_WIDTH (f);
1309 int height = FRAME_PIXEL_HEIGHT (f);
1310 #ifdef USE_GTK
1311 int margin = 0;
1312 #else
1313 int margin = FRAME_TOP_MARGIN_HEIGHT (f);
1314 #endif
1315 struct face *face = FACE_FROM_ID_OR_NULL (f, INTERNAL_BORDER_FACE_ID);
1317 block_input ();
1319 if (face)
1321 unsigned long color = face->background;
1322 Display *display = FRAME_X_DISPLAY (f);
1323 GC gc = f->output_data.x->normal_gc;
1325 XSetForeground (display, gc, color);
1326 x_fill_rectangle (f, gc, 0, margin, width, border);
1327 x_fill_rectangle (f, gc, 0, 0, border, height);
1328 x_fill_rectangle (f, gc, width - border, 0, border, height);
1329 x_fill_rectangle (f, gc, 0, height - border, width, border);
1330 XSetForeground (display, gc, FRAME_FOREGROUND_PIXEL (f));
1332 else
1334 x_clear_area (f, 0, 0, border, height);
1335 x_clear_area (f, 0, margin, width, border);
1336 x_clear_area (f, width - border, 0, border, height);
1337 x_clear_area (f, 0, height - border, width, border);
1340 unblock_input ();
1344 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
1345 arrow bitmaps, or clear the fringes if no bitmaps are required
1346 before DESIRED_ROW is made current. This function is called from
1347 update_window_line only if it is known that there are differences
1348 between bitmaps to be drawn between current row and DESIRED_ROW. */
1350 static void
1351 x_after_update_window_line (struct window *w, struct glyph_row *desired_row)
1353 eassert (w);
1355 if (!desired_row->mode_line_p && !w->pseudo_window_p)
1356 desired_row->redraw_fringe_bitmaps_p = true;
1358 #ifdef USE_X_TOOLKIT
1359 /* When a window has disappeared, make sure that no rest of
1360 full-width rows stays visible in the internal border. Could
1361 check here if updated window is the leftmost/rightmost window,
1362 but I guess it's not worth doing since vertically split windows
1363 are almost never used, internal border is rarely set, and the
1364 overhead is very small. */
1366 struct frame *f;
1367 int width, height;
1369 if (windows_or_buffers_changed
1370 && desired_row->full_width_p
1371 && (f = XFRAME (w->frame),
1372 width = FRAME_INTERNAL_BORDER_WIDTH (f),
1373 width != 0)
1374 && (height = desired_row->visible_height,
1375 height > 0))
1377 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
1378 struct face *face = FACE_FROM_ID_OR_NULL (f, INTERNAL_BORDER_FACE_ID);
1380 block_input ();
1381 if (face)
1383 unsigned long color = face->background;
1384 Display *display = FRAME_X_DISPLAY (f);
1385 GC gc = f->output_data.x->normal_gc;
1387 XSetForeground (display, gc, color);
1388 x_fill_rectangle (f, gc, 0, y, width, height);
1389 x_fill_rectangle (f, gc, FRAME_PIXEL_WIDTH (f) - width, y,
1390 width, height);
1391 XSetForeground (display, gc, FRAME_FOREGROUND_PIXEL (f));
1393 else
1395 x_clear_area (f, 0, y, width, height);
1396 x_clear_area (f, FRAME_PIXEL_WIDTH (f) - width, y, width, height);
1398 unblock_input ();
1401 #endif
1404 static void
1405 x_draw_fringe_bitmap (struct window *w, struct glyph_row *row, struct draw_fringe_bitmap_params *p)
1407 struct frame *f = XFRAME (WINDOW_FRAME (w));
1408 Display *display = FRAME_X_DISPLAY (f);
1409 GC gc = f->output_data.x->normal_gc;
1410 struct face *face = p->face;
1412 /* Must clip because of partially visible lines. */
1413 x_clip_to_row (w, row, ANY_AREA, gc);
1415 if (p->bx >= 0 && !p->overlay_p)
1417 /* In case the same realized face is used for fringes and
1418 for something displayed in the text (e.g. face `region' on
1419 mono-displays, the fill style may have been changed to
1420 FillSolid in x_draw_glyph_string_background. */
1421 if (face->stipple)
1422 XSetFillStyle (display, face->gc, FillOpaqueStippled);
1423 else
1424 XSetForeground (display, face->gc, face->background);
1426 x_fill_rectangle (f, face->gc, p->bx, p->by, p->nx, p->ny);
1428 if (!face->stipple)
1429 XSetForeground (display, face->gc, face->foreground);
1432 #ifdef USE_CAIRO
1433 if (p->which && p->which < max_fringe_bmp)
1435 XGCValues gcv;
1437 XGetGCValues (display, gc, GCForeground | GCBackground, &gcv);
1438 XSetForeground (display, gc, (p->cursor_p
1439 ? (p->overlay_p ? face->background
1440 : f->output_data.x->cursor_pixel)
1441 : face->foreground));
1442 XSetBackground (display, gc, face->background);
1443 x_cr_draw_image (f, gc, fringe_bmp[p->which], 0, p->dh,
1444 p->wd, p->h, p->x, p->y, p->overlay_p);
1445 XSetForeground (display, gc, gcv.foreground);
1446 XSetBackground (display, gc, gcv.background);
1448 #else /* not USE_CAIRO */
1449 if (p->which)
1451 Drawable drawable = FRAME_X_DRAWABLE (f);
1452 char *bits;
1453 Pixmap pixmap, clipmask = (Pixmap) 0;
1454 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
1455 XGCValues gcv;
1457 if (p->wd > 8)
1458 bits = (char *) (p->bits + p->dh);
1459 else
1460 bits = (char *) p->bits + p->dh;
1462 /* Draw the bitmap. I believe these small pixmaps can be cached
1463 by the server. */
1464 pixmap = XCreatePixmapFromBitmapData (display, drawable, bits, p->wd, p->h,
1465 (p->cursor_p
1466 ? (p->overlay_p ? face->background
1467 : f->output_data.x->cursor_pixel)
1468 : face->foreground),
1469 face->background, depth);
1471 if (p->overlay_p)
1473 clipmask = XCreatePixmapFromBitmapData (display,
1474 FRAME_DISPLAY_INFO (f)->root_window,
1475 bits, p->wd, p->h,
1476 1, 0, 1);
1477 gcv.clip_mask = clipmask;
1478 gcv.clip_x_origin = p->x;
1479 gcv.clip_y_origin = p->y;
1480 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
1483 XCopyArea (display, pixmap, drawable, gc, 0, 0,
1484 p->wd, p->h, p->x, p->y);
1485 XFreePixmap (display, pixmap);
1487 if (p->overlay_p)
1489 gcv.clip_mask = (Pixmap) 0;
1490 XChangeGC (display, gc, GCClipMask, &gcv);
1491 XFreePixmap (display, clipmask);
1494 #endif /* not USE_CAIRO */
1496 x_reset_clip_rectangles (f, gc);
1499 /***********************************************************************
1500 Glyph display
1501 ***********************************************************************/
1505 static void x_set_glyph_string_clipping (struct glyph_string *);
1506 static void x_set_glyph_string_gc (struct glyph_string *);
1507 static void x_draw_glyph_string_foreground (struct glyph_string *);
1508 static void x_draw_composite_glyph_string_foreground (struct glyph_string *);
1509 static void x_draw_glyph_string_box (struct glyph_string *);
1510 static void x_draw_glyph_string (struct glyph_string *);
1511 static _Noreturn void x_delete_glyphs (struct frame *, int);
1512 static void x_compute_glyph_string_overhangs (struct glyph_string *);
1513 static void x_set_cursor_gc (struct glyph_string *);
1514 static void x_set_mode_line_face_gc (struct glyph_string *);
1515 static void x_set_mouse_face_gc (struct glyph_string *);
1516 static bool x_alloc_lighter_color (struct frame *, Display *, Colormap,
1517 unsigned long *, double, int);
1518 static void x_setup_relief_color (struct frame *, struct relief *,
1519 double, int, unsigned long);
1520 static void x_setup_relief_colors (struct glyph_string *);
1521 static void x_draw_image_glyph_string (struct glyph_string *);
1522 static void x_draw_image_relief (struct glyph_string *);
1523 static void x_draw_image_foreground (struct glyph_string *);
1524 static void x_draw_image_foreground_1 (struct glyph_string *, Pixmap);
1525 static void x_clear_glyph_string_rect (struct glyph_string *, int,
1526 int, int, int);
1527 static void x_draw_relief_rect (struct frame *, int, int, int, int,
1528 int, bool, bool, bool, bool, bool,
1529 XRectangle *);
1530 static void x_draw_box_rect (struct glyph_string *, int, int, int, int,
1531 int, bool, bool, XRectangle *);
1532 static void x_scroll_bar_clear (struct frame *);
1534 #ifdef GLYPH_DEBUG
1535 static void x_check_font (struct frame *, struct font *);
1536 #endif
1539 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
1540 face. */
1542 static void
1543 x_set_cursor_gc (struct glyph_string *s)
1545 if (s->font == FRAME_FONT (s->f)
1546 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
1547 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
1548 && !s->cmp)
1549 s->gc = s->f->output_data.x->cursor_gc;
1550 else
1552 /* Cursor on non-default face: must merge. */
1553 XGCValues xgcv;
1554 unsigned long mask;
1556 xgcv.background = s->f->output_data.x->cursor_pixel;
1557 xgcv.foreground = s->face->background;
1559 /* If the glyph would be invisible, try a different foreground. */
1560 if (xgcv.foreground == xgcv.background)
1561 xgcv.foreground = s->face->foreground;
1562 if (xgcv.foreground == xgcv.background)
1563 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1564 if (xgcv.foreground == xgcv.background)
1565 xgcv.foreground = s->face->foreground;
1567 /* Make sure the cursor is distinct from text in this face. */
1568 if (xgcv.background == s->face->background
1569 && xgcv.foreground == s->face->foreground)
1571 xgcv.background = s->face->foreground;
1572 xgcv.foreground = s->face->background;
1575 IF_DEBUG (x_check_font (s->f, s->font));
1576 xgcv.graphics_exposures = False;
1577 mask = GCForeground | GCBackground | GCGraphicsExposures;
1579 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1580 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1581 mask, &xgcv);
1582 else
1583 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1584 = XCreateGC (s->display, FRAME_X_DRAWABLE (s->f), mask, &xgcv);
1586 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1591 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1593 static void
1594 x_set_mouse_face_gc (struct glyph_string *s)
1596 int face_id;
1597 struct face *face;
1599 /* What face has to be used last for the mouse face? */
1600 face_id = MOUSE_HL_INFO (s->f)->mouse_face_face_id;
1601 face = FACE_FROM_ID_OR_NULL (s->f, face_id);
1602 if (face == NULL)
1603 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1605 if (s->first_glyph->type == CHAR_GLYPH)
1606 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1607 else
1608 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1609 s->face = FACE_FROM_ID (s->f, face_id);
1610 prepare_face_for_display (s->f, s->face);
1612 if (s->font == s->face->font)
1613 s->gc = s->face->gc;
1614 else
1616 /* Otherwise construct scratch_cursor_gc with values from FACE
1617 except for FONT. */
1618 XGCValues xgcv;
1619 unsigned long mask;
1621 xgcv.background = s->face->background;
1622 xgcv.foreground = s->face->foreground;
1623 xgcv.graphics_exposures = False;
1624 mask = GCForeground | GCBackground | GCGraphicsExposures;
1626 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1627 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1628 mask, &xgcv);
1629 else
1630 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1631 = XCreateGC (s->display, FRAME_X_DRAWABLE (s->f), mask, &xgcv);
1633 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1636 eassert (s->gc != 0);
1640 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1641 Faces to use in the mode line have already been computed when the
1642 matrix was built, so there isn't much to do, here. */
1644 static void
1645 x_set_mode_line_face_gc (struct glyph_string *s)
1647 s->gc = s->face->gc;
1651 /* Set S->gc of glyph string S for drawing that glyph string. Set
1652 S->stippled_p to a non-zero value if the face of S has a stipple
1653 pattern. */
1655 static void
1656 x_set_glyph_string_gc (struct glyph_string *s)
1658 prepare_face_for_display (s->f, s->face);
1660 if (s->hl == DRAW_NORMAL_TEXT)
1662 s->gc = s->face->gc;
1663 s->stippled_p = s->face->stipple != 0;
1665 else if (s->hl == DRAW_INVERSE_VIDEO)
1667 x_set_mode_line_face_gc (s);
1668 s->stippled_p = s->face->stipple != 0;
1670 else if (s->hl == DRAW_CURSOR)
1672 x_set_cursor_gc (s);
1673 s->stippled_p = false;
1675 else if (s->hl == DRAW_MOUSE_FACE)
1677 x_set_mouse_face_gc (s);
1678 s->stippled_p = s->face->stipple != 0;
1680 else if (s->hl == DRAW_IMAGE_RAISED
1681 || s->hl == DRAW_IMAGE_SUNKEN)
1683 s->gc = s->face->gc;
1684 s->stippled_p = s->face->stipple != 0;
1686 else
1687 emacs_abort ();
1689 /* GC must have been set. */
1690 eassert (s->gc != 0);
1694 /* Set clipping for output of glyph string S. S may be part of a mode
1695 line or menu if we don't have X toolkit support. */
1697 static void
1698 x_set_glyph_string_clipping (struct glyph_string *s)
1700 XRectangle *r = s->clip;
1701 int n = get_glyph_string_clip_rects (s, r, 2);
1703 if (n > 0)
1704 x_set_clip_rectangles (s->f, s->gc, r, n);
1705 s->num_clips = n;
1709 /* Set SRC's clipping for output of glyph string DST. This is called
1710 when we are drawing DST's left_overhang or right_overhang only in
1711 the area of SRC. */
1713 static void
1714 x_set_glyph_string_clipping_exactly (struct glyph_string *src, struct glyph_string *dst)
1716 XRectangle r;
1718 r.x = src->x;
1719 r.width = src->width;
1720 r.y = src->y;
1721 r.height = src->height;
1722 dst->clip[0] = r;
1723 dst->num_clips = 1;
1724 x_set_clip_rectangles (dst->f, dst->gc, &r, 1);
1728 /* RIF:
1729 Compute left and right overhang of glyph string S. */
1731 static void
1732 x_compute_glyph_string_overhangs (struct glyph_string *s)
1734 if (s->cmp == NULL
1735 && (s->first_glyph->type == CHAR_GLYPH
1736 || s->first_glyph->type == COMPOSITE_GLYPH))
1738 struct font_metrics metrics;
1740 if (s->first_glyph->type == CHAR_GLYPH)
1742 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1743 struct font *font = s->font;
1744 int i;
1746 for (i = 0; i < s->nchars; i++)
1747 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1748 font->driver->text_extents (font, code, s->nchars, &metrics);
1750 else
1752 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1754 composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
1756 s->right_overhang = (metrics.rbearing > metrics.width
1757 ? metrics.rbearing - metrics.width : 0);
1758 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1760 else if (s->cmp)
1762 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1763 s->left_overhang = - s->cmp->lbearing;
1768 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1770 static void
1771 x_clear_glyph_string_rect (struct glyph_string *s, int x, int y, int w, int h)
1773 XGCValues xgcv;
1774 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1775 XSetForeground (s->display, s->gc, xgcv.background);
1776 x_fill_rectangle (s->f, s->gc, x, y, w, h);
1777 XSetForeground (s->display, s->gc, xgcv.foreground);
1781 /* Draw the background of glyph_string S. If S->background_filled_p
1782 is non-zero don't draw it. FORCE_P non-zero means draw the
1783 background even if it wouldn't be drawn normally. This is used
1784 when a string preceding S draws into the background of S, or S
1785 contains the first component of a composition. */
1787 static void
1788 x_draw_glyph_string_background (struct glyph_string *s, bool force_p)
1790 /* Nothing to do if background has already been drawn or if it
1791 shouldn't be drawn in the first place. */
1792 if (!s->background_filled_p)
1794 int box_line_width = max (s->face->box_line_width, 0);
1796 if (s->stippled_p)
1798 /* Fill background with a stipple pattern. */
1799 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1800 x_fill_rectangle (s->f, s->gc, s->x,
1801 s->y + box_line_width,
1802 s->background_width,
1803 s->height - 2 * box_line_width);
1804 XSetFillStyle (s->display, s->gc, FillSolid);
1805 s->background_filled_p = true;
1807 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1808 /* When xdisp.c ignores FONT_HEIGHT, we cannot trust
1809 font dimensions, since the actual glyphs might be
1810 much smaller. So in that case we always clear the
1811 rectangle with background color. */
1812 || FONT_TOO_HIGH (s->font)
1813 || s->font_not_found_p
1814 || s->extends_to_end_of_line_p
1815 || force_p)
1817 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1818 s->background_width,
1819 s->height - 2 * box_line_width);
1820 s->background_filled_p = true;
1826 /* Draw the foreground of glyph string S. */
1828 static void
1829 x_draw_glyph_string_foreground (struct glyph_string *s)
1831 int i, x;
1833 /* If first glyph of S has a left box line, start drawing the text
1834 of S to the right of that box line. */
1835 if (s->face->box != FACE_NO_BOX
1836 && s->first_glyph->left_box_line_p)
1837 x = s->x + eabs (s->face->box_line_width);
1838 else
1839 x = s->x;
1841 /* Draw characters of S as rectangles if S's font could not be
1842 loaded. */
1843 if (s->font_not_found_p)
1845 for (i = 0; i < s->nchars; ++i)
1847 struct glyph *g = s->first_glyph + i;
1848 x_draw_rectangle (s->f,
1849 s->gc, x, s->y, g->pixel_width - 1,
1850 s->height - 1);
1851 x += g->pixel_width;
1854 else
1856 struct font *font = s->font;
1857 int boff = font->baseline_offset;
1858 int y;
1860 if (font->vertical_centering)
1861 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1863 y = s->ybase - boff;
1864 if (s->for_overlaps
1865 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1866 font->driver->draw (s, 0, s->nchars, x, y, false);
1867 else
1868 font->driver->draw (s, 0, s->nchars, x, y, true);
1869 if (s->face->overstrike)
1870 font->driver->draw (s, 0, s->nchars, x + 1, y, false);
1874 /* Draw the foreground of composite glyph string S. */
1876 static void
1877 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1879 int i, j, x;
1880 struct font *font = s->font;
1882 /* If first glyph of S has a left box line, start drawing the text
1883 of S to the right of that box line. */
1884 if (s->face && s->face->box != FACE_NO_BOX
1885 && s->first_glyph->left_box_line_p)
1886 x = s->x + eabs (s->face->box_line_width);
1887 else
1888 x = s->x;
1890 /* S is a glyph string for a composition. S->cmp_from is the index
1891 of the first character drawn for glyphs of this composition.
1892 S->cmp_from == 0 means we are drawing the very first character of
1893 this composition. */
1895 /* Draw a rectangle for the composition if the font for the very
1896 first character of the composition could not be loaded. */
1897 if (s->font_not_found_p)
1899 if (s->cmp_from == 0)
1900 x_draw_rectangle (s->f, s->gc, x, s->y,
1901 s->width - 1, s->height - 1);
1903 else if (! s->first_glyph->u.cmp.automatic)
1905 int y = s->ybase;
1907 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1908 /* TAB in a composition means display glyphs with padding
1909 space on the left or right. */
1910 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1912 int xx = x + s->cmp->offsets[j * 2];
1913 int yy = y - s->cmp->offsets[j * 2 + 1];
1915 font->driver->draw (s, j, j + 1, xx, yy, false);
1916 if (s->face->overstrike)
1917 font->driver->draw (s, j, j + 1, xx + 1, yy, false);
1920 else
1922 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1923 Lisp_Object glyph;
1924 int y = s->ybase;
1925 int width = 0;
1927 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1929 glyph = LGSTRING_GLYPH (gstring, i);
1930 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1931 width += LGLYPH_WIDTH (glyph);
1932 else
1934 int xoff, yoff, wadjust;
1936 if (j < i)
1938 font->driver->draw (s, j, i, x, y, false);
1939 if (s->face->overstrike)
1940 font->driver->draw (s, j, i, x + 1, y, false);
1941 x += width;
1943 xoff = LGLYPH_XOFF (glyph);
1944 yoff = LGLYPH_YOFF (glyph);
1945 wadjust = LGLYPH_WADJUST (glyph);
1946 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, false);
1947 if (s->face->overstrike)
1948 font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff,
1949 false);
1950 x += wadjust;
1951 j = i + 1;
1952 width = 0;
1955 if (j < i)
1957 font->driver->draw (s, j, i, x, y, false);
1958 if (s->face->overstrike)
1959 font->driver->draw (s, j, i, x + 1, y, false);
1965 /* Draw the foreground of glyph string S for glyphless characters. */
1967 static void
1968 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1970 struct glyph *glyph = s->first_glyph;
1971 XChar2b char2b[8];
1972 int x, i, j;
1974 /* If first glyph of S has a left box line, start drawing the text
1975 of S to the right of that box line. */
1976 if (s->face && s->face->box != FACE_NO_BOX
1977 && s->first_glyph->left_box_line_p)
1978 x = s->x + eabs (s->face->box_line_width);
1979 else
1980 x = s->x;
1982 s->char2b = char2b;
1984 for (i = 0; i < s->nchars; i++, glyph++)
1986 char buf[7], *str = NULL;
1987 int len = glyph->u.glyphless.len;
1989 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1991 if (len > 0
1992 && CHAR_TABLE_P (Vglyphless_char_display)
1993 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1994 >= 1))
1996 Lisp_Object acronym
1997 = (! glyph->u.glyphless.for_no_font
1998 ? CHAR_TABLE_REF (Vglyphless_char_display,
1999 glyph->u.glyphless.ch)
2000 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
2001 if (STRINGP (acronym))
2002 str = SSDATA (acronym);
2005 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
2007 unsigned int ch = glyph->u.glyphless.ch;
2008 eassume (ch <= MAX_CHAR);
2009 sprintf (buf, "%0*X", ch < 0x10000 ? 4 : 6, ch);
2010 str = buf;
2013 if (str)
2015 int upper_len = (len + 1) / 2;
2016 unsigned code;
2018 /* It is assured that all LEN characters in STR is ASCII. */
2019 for (j = 0; j < len; j++)
2021 code = s->font->driver->encode_char (s->font, str[j]);
2022 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
2024 s->font->driver->draw (s, 0, upper_len,
2025 x + glyph->slice.glyphless.upper_xoff,
2026 s->ybase + glyph->slice.glyphless.upper_yoff,
2027 false);
2028 s->font->driver->draw (s, upper_len, len,
2029 x + glyph->slice.glyphless.lower_xoff,
2030 s->ybase + glyph->slice.glyphless.lower_yoff,
2031 false);
2033 if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
2034 x_draw_rectangle (s->f, s->gc,
2035 x, s->ybase - glyph->ascent,
2036 glyph->pixel_width - 1,
2037 glyph->ascent + glyph->descent - 1);
2038 x += glyph->pixel_width;
2042 #ifdef USE_X_TOOLKIT
2044 #ifdef USE_LUCID
2046 /* Return the frame on which widget WIDGET is used.. Abort if frame
2047 cannot be determined. */
2049 static struct frame *
2050 x_frame_of_widget (Widget widget)
2052 struct x_display_info *dpyinfo;
2053 Lisp_Object tail, frame;
2054 struct frame *f;
2056 dpyinfo = x_display_info_for_display (XtDisplay (widget));
2058 /* Find the top-level shell of the widget. Note that this function
2059 can be called when the widget is not yet realized, so XtWindow
2060 (widget) == 0. That's the reason we can't simply use
2061 x_any_window_to_frame. */
2062 while (!XtIsTopLevelShell (widget))
2063 widget = XtParent (widget);
2065 /* Look for a frame with that top-level widget. Allocate the color
2066 on that frame to get the right gamma correction value. */
2067 FOR_EACH_FRAME (tail, frame)
2069 f = XFRAME (frame);
2070 if (FRAME_X_P (f)
2071 && f->output_data.nothing != 1
2072 && FRAME_DISPLAY_INFO (f) == dpyinfo
2073 && f->output_data.x->widget == widget)
2074 return f;
2076 emacs_abort ();
2079 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
2080 or DELTA. Try a color with RGB values multiplied by FACTOR first.
2081 If this produces the same color as PIXEL, try a color where all RGB
2082 values have DELTA added. Return the allocated color in *PIXEL.
2083 DISPLAY is the X display, CMAP is the colormap to operate on.
2084 Value is true if successful. */
2086 bool
2087 x_alloc_lighter_color_for_widget (Widget widget, Display *display, Colormap cmap,
2088 unsigned long *pixel, double factor, int delta)
2090 struct frame *f = x_frame_of_widget (widget);
2091 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
2094 #endif /* USE_LUCID */
2097 /* Structure specifying which arguments should be passed by Xt to
2098 cvt_string_to_pixel. We want the widget's screen and colormap. */
2100 static XtConvertArgRec cvt_string_to_pixel_args[] =
2102 {XtWidgetBaseOffset, (XtPointer) offsetof (WidgetRec, core.screen),
2103 sizeof (Screen *)},
2104 {XtWidgetBaseOffset, (XtPointer) offsetof (WidgetRec, core.colormap),
2105 sizeof (Colormap)}
2109 /* The address of this variable is returned by
2110 cvt_string_to_pixel. */
2112 static Pixel cvt_string_to_pixel_value;
2115 /* Convert a color name to a pixel color.
2117 DPY is the display we are working on.
2119 ARGS is an array of *NARGS XrmValue structures holding additional
2120 information about the widget for which the conversion takes place.
2121 The contents of this array are determined by the specification
2122 in cvt_string_to_pixel_args.
2124 FROM is a pointer to an XrmValue which points to the color name to
2125 convert. TO is an XrmValue in which to return the pixel color.
2127 CLOSURE_RET is a pointer to user-data, in which we record if
2128 we allocated the color or not.
2130 Value is True if successful, False otherwise. */
2132 static Boolean
2133 cvt_string_to_pixel (Display *dpy, XrmValue *args, Cardinal *nargs,
2134 XrmValue *from, XrmValue *to,
2135 XtPointer *closure_ret)
2137 Screen *screen;
2138 Colormap cmap;
2139 Pixel pixel;
2140 String color_name;
2141 XColor color;
2143 if (*nargs != 2)
2145 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
2146 "wrongParameters", "cvt_string_to_pixel",
2147 "XtToolkitError",
2148 "Screen and colormap args required", NULL, NULL);
2149 return False;
2152 screen = *(Screen **) args[0].addr;
2153 cmap = *(Colormap *) args[1].addr;
2154 color_name = (String) from->addr;
2156 if (strcmp (color_name, XtDefaultBackground) == 0)
2158 *closure_ret = (XtPointer) False;
2159 pixel = WhitePixelOfScreen (screen);
2161 else if (strcmp (color_name, XtDefaultForeground) == 0)
2163 *closure_ret = (XtPointer) False;
2164 pixel = BlackPixelOfScreen (screen);
2166 else if (XParseColor (dpy, cmap, color_name, &color)
2167 && x_alloc_nearest_color_1 (dpy, cmap, &color))
2169 pixel = color.pixel;
2170 *closure_ret = (XtPointer) True;
2172 else
2174 String params[1];
2175 Cardinal nparams = 1;
2177 params[0] = color_name;
2178 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
2179 "badValue", "cvt_string_to_pixel",
2180 "XtToolkitError", "Invalid color '%s'",
2181 params, &nparams);
2182 return False;
2185 if (to->addr != NULL)
2187 if (to->size < sizeof (Pixel))
2189 to->size = sizeof (Pixel);
2190 return False;
2193 *(Pixel *) to->addr = pixel;
2195 else
2197 cvt_string_to_pixel_value = pixel;
2198 to->addr = (XtPointer) &cvt_string_to_pixel_value;
2201 to->size = sizeof (Pixel);
2202 return True;
2206 /* Free a pixel color which was previously allocated via
2207 cvt_string_to_pixel. This is registered as the destructor
2208 for this type of resource via XtSetTypeConverter.
2210 APP is the application context in which we work.
2212 TO is a pointer to an XrmValue holding the color to free.
2213 CLOSURE is the value we stored in CLOSURE_RET for this color
2214 in cvt_string_to_pixel.
2216 ARGS and NARGS are like for cvt_string_to_pixel. */
2218 static void
2219 cvt_pixel_dtor (XtAppContext app, XrmValuePtr to, XtPointer closure, XrmValuePtr args,
2220 Cardinal *nargs)
2222 if (*nargs != 2)
2224 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
2225 "XtToolkitError",
2226 "Screen and colormap arguments required",
2227 NULL, NULL);
2229 else if (closure != NULL)
2231 /* We did allocate the pixel, so free it. */
2232 Screen *screen = *(Screen **) args[0].addr;
2233 Colormap cmap = *(Colormap *) args[1].addr;
2234 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
2235 (Pixel *) to->addr, 1);
2240 #endif /* USE_X_TOOLKIT */
2243 /* Value is an array of XColor structures for the contents of the
2244 color map of display DPY. Set *NCELLS to the size of the array.
2245 Note that this probably shouldn't be called for large color maps,
2246 say a 24-bit TrueColor map. */
2248 static const XColor *
2249 x_color_cells (Display *dpy, int *ncells)
2251 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
2252 eassume (dpyinfo);
2254 if (dpyinfo->color_cells == NULL)
2256 Screen *screen = dpyinfo->screen;
2257 int ncolor_cells = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
2258 int i;
2260 dpyinfo->color_cells = xnmalloc (ncolor_cells,
2261 sizeof *dpyinfo->color_cells);
2262 dpyinfo->ncolor_cells = ncolor_cells;
2264 for (i = 0; i < ncolor_cells; ++i)
2265 dpyinfo->color_cells[i].pixel = i;
2267 XQueryColors (dpy, dpyinfo->cmap,
2268 dpyinfo->color_cells, ncolor_cells);
2271 *ncells = dpyinfo->ncolor_cells;
2272 return dpyinfo->color_cells;
2276 /* On frame F, translate pixel colors to RGB values for the NCOLORS
2277 colors in COLORS. Use cached information, if available. */
2279 void
2280 x_query_colors (struct frame *f, XColor *colors, int ncolors)
2282 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2284 if (dpyinfo->red_bits > 0)
2286 /* For TrueColor displays, we can decompose the RGB value
2287 directly. */
2288 int i;
2289 unsigned int rmult, gmult, bmult;
2290 unsigned int rmask, gmask, bmask;
2292 rmask = (1 << dpyinfo->red_bits) - 1;
2293 gmask = (1 << dpyinfo->green_bits) - 1;
2294 bmask = (1 << dpyinfo->blue_bits) - 1;
2295 /* If we're widening, for example, 8 bits in the pixel value to
2296 16 bits for the separate-color representation, we want to
2297 extrapolate the lower bits based on those bits available --
2298 in other words, we'd like 0xff to become 0xffff instead of
2299 the 0xff00 we'd get by just zero-filling the lower bits.
2301 We generate a 32-bit scaled-up value and shift it, in case
2302 the bit count doesn't divide 16 evenly (e.g., when dealing
2303 with a 3-3-2 bit RGB display), to get more of the lower bits
2304 correct.
2306 Should we cache the multipliers in dpyinfo? Maybe
2307 special-case the 8-8-8 common case? */
2308 rmult = 0xffffffff / rmask;
2309 gmult = 0xffffffff / gmask;
2310 bmult = 0xffffffff / bmask;
2312 for (i = 0; i < ncolors; ++i)
2314 unsigned int r, g, b;
2315 unsigned long pixel = colors[i].pixel;
2317 r = (pixel >> dpyinfo->red_offset) & rmask;
2318 g = (pixel >> dpyinfo->green_offset) & gmask;
2319 b = (pixel >> dpyinfo->blue_offset) & bmask;
2321 colors[i].red = (r * rmult) >> 16;
2322 colors[i].green = (g * gmult) >> 16;
2323 colors[i].blue = (b * bmult) >> 16;
2325 return;
2328 if (dpyinfo->color_cells)
2330 int i;
2331 for (i = 0; i < ncolors; ++i)
2333 unsigned long pixel = colors[i].pixel;
2334 eassert (pixel < dpyinfo->ncolor_cells);
2335 eassert (dpyinfo->color_cells[pixel].pixel == pixel);
2336 colors[i] = dpyinfo->color_cells[pixel];
2338 return;
2341 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
2345 /* On frame F, translate pixel color to RGB values for the color in
2346 COLOR. Use cached information, if available. */
2348 void
2349 x_query_color (struct frame *f, XColor *color)
2351 x_query_colors (f, color, 1);
2355 /* On frame F, translate the color name to RGB values. Use cached
2356 information, if possible.
2358 Note that there is currently no way to clean old entries out of the
2359 cache. However, it is limited to names in the server's database,
2360 and names we've actually looked up; list-colors-display is probably
2361 the most color-intensive case we're likely to hit. */
2363 Status x_parse_color (struct frame *f, const char *color_name,
2364 XColor *color)
2366 Display *dpy = FRAME_X_DISPLAY (f);
2367 Colormap cmap = FRAME_X_COLORMAP (f);
2368 struct color_name_cache_entry *cache_entry;
2370 if (color_name[0] == '#')
2372 /* The hex form is parsed directly by XParseColor without
2373 talking to the X server. No need for caching. */
2374 return XParseColor (dpy, cmap, color_name, color);
2377 for (cache_entry = FRAME_DISPLAY_INFO (f)->color_names; cache_entry;
2378 cache_entry = cache_entry->next)
2380 if (!xstrcasecmp(cache_entry->name, color_name))
2382 *color = cache_entry->rgb;
2383 return 1;
2387 if (XParseColor (dpy, cmap, color_name, color) == 0)
2388 /* No caching of negative results, currently. */
2389 return 0;
2391 cache_entry = xzalloc (sizeof *cache_entry);
2392 cache_entry->rgb = *color;
2393 cache_entry->name = xstrdup (color_name);
2394 cache_entry->next = FRAME_DISPLAY_INFO (f)->color_names;
2395 FRAME_DISPLAY_INFO (f)->color_names = cache_entry;
2396 return 1;
2400 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
2401 exact match can't be allocated, try the nearest color available.
2402 Value is true if successful. Set *COLOR to the color
2403 allocated. */
2405 static bool
2406 x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color)
2408 bool rc;
2410 rc = XAllocColor (dpy, cmap, color) != 0;
2411 if (rc == 0)
2413 /* If we got to this point, the colormap is full, so we're going
2414 to try to get the next closest color. The algorithm used is
2415 a least-squares matching, which is what X uses for closest
2416 color matching with StaticColor visuals. */
2417 int nearest, i;
2418 int max_color_delta = 255;
2419 int max_delta = 3 * max_color_delta;
2420 int nearest_delta = max_delta + 1;
2421 int ncells;
2422 const XColor *cells = x_color_cells (dpy, &ncells);
2424 for (nearest = i = 0; i < ncells; ++i)
2426 int dred = (color->red >> 8) - (cells[i].red >> 8);
2427 int dgreen = (color->green >> 8) - (cells[i].green >> 8);
2428 int dblue = (color->blue >> 8) - (cells[i].blue >> 8);
2429 int delta = dred * dred + dgreen * dgreen + dblue * dblue;
2431 if (delta < nearest_delta)
2433 nearest = i;
2434 nearest_delta = delta;
2438 color->red = cells[nearest].red;
2439 color->green = cells[nearest].green;
2440 color->blue = cells[nearest].blue;
2441 rc = XAllocColor (dpy, cmap, color) != 0;
2443 else
2445 /* If allocation succeeded, and the allocated pixel color is not
2446 equal to a cached pixel color recorded earlier, there was a
2447 change in the colormap, so clear the color cache. */
2448 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
2449 eassume (dpyinfo);
2451 if (dpyinfo->color_cells)
2453 XColor *cached_color = &dpyinfo->color_cells[color->pixel];
2454 if (cached_color->red != color->red
2455 || cached_color->blue != color->blue
2456 || cached_color->green != color->green)
2458 xfree (dpyinfo->color_cells);
2459 dpyinfo->color_cells = NULL;
2460 dpyinfo->ncolor_cells = 0;
2465 #ifdef DEBUG_X_COLORS
2466 if (rc)
2467 register_color (color->pixel);
2468 #endif /* DEBUG_X_COLORS */
2470 return rc;
2474 /* Allocate the color COLOR->pixel on frame F, colormap CMAP, after
2475 gamma correction. If an exact match can't be allocated, try the
2476 nearest color available. Value is true if successful. Set *COLOR
2477 to the color allocated. */
2479 bool
2480 x_alloc_nearest_color (struct frame *f, Colormap cmap, XColor *color)
2482 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2484 gamma_correct (f, color);
2486 if (dpyinfo->red_bits > 0)
2488 color->pixel = x_make_truecolor_pixel (dpyinfo,
2489 color->red,
2490 color->green,
2491 color->blue);
2492 return true;
2495 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
2499 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
2500 It's necessary to do this instead of just using PIXEL directly to
2501 get color reference counts right. */
2503 unsigned long
2504 x_copy_color (struct frame *f, unsigned long pixel)
2506 XColor color;
2508 /* If display has an immutable color map, freeing colors is not
2509 necessary and some servers don't allow it. Since we won't free a
2510 color once we've allocated it, we don't need to re-allocate it to
2511 maintain the server's reference count. */
2512 if (!x_mutable_colormap (FRAME_X_VISUAL (f)))
2513 return pixel;
2515 color.pixel = pixel;
2516 block_input ();
2517 /* The color could still be found in the color_cells array. */
2518 x_query_color (f, &color);
2519 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
2520 unblock_input ();
2521 #ifdef DEBUG_X_COLORS
2522 register_color (pixel);
2523 #endif
2524 return color.pixel;
2528 /* Brightness beyond which a color won't have its highlight brightness
2529 boosted.
2531 Nominally, highlight colors for `3d' faces are calculated by
2532 brightening an object's color by a constant scale factor, but this
2533 doesn't yield good results for dark colors, so for colors who's
2534 brightness is less than this value (on a scale of 0-65535) have an
2535 use an additional additive factor.
2537 The value here is set so that the default menu-bar/mode-line color
2538 (grey75) will not have its highlights changed at all. */
2539 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
2542 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
2543 or DELTA. Try a color with RGB values multiplied by FACTOR first.
2544 If this produces the same color as PIXEL, try a color where all RGB
2545 values have DELTA added. Return the allocated color in *PIXEL.
2546 DISPLAY is the X display, CMAP is the colormap to operate on.
2547 Value is non-zero if successful. */
2549 static bool
2550 x_alloc_lighter_color (struct frame *f, Display *display, Colormap cmap,
2551 unsigned long *pixel, double factor, int delta)
2553 XColor color, new;
2554 long bright;
2555 bool success_p;
2557 /* Get RGB color values. */
2558 color.pixel = *pixel;
2559 x_query_color (f, &color);
2561 /* Change RGB values by specified FACTOR. Avoid overflow! */
2562 eassert (factor >= 0);
2563 new.red = min (0xffff, factor * color.red);
2564 new.green = min (0xffff, factor * color.green);
2565 new.blue = min (0xffff, factor * color.blue);
2567 /* Calculate brightness of COLOR. */
2568 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
2570 /* We only boost colors that are darker than
2571 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
2572 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
2573 /* Make an additive adjustment to NEW, because it's dark enough so
2574 that scaling by FACTOR alone isn't enough. */
2576 /* How far below the limit this color is (0 - 1, 1 being darker). */
2577 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
2578 /* The additive adjustment. */
2579 int min_delta = delta * dimness * factor / 2;
2581 if (factor < 1)
2583 new.red = max (0, new.red - min_delta);
2584 new.green = max (0, new.green - min_delta);
2585 new.blue = max (0, new.blue - min_delta);
2587 else
2589 new.red = min (0xffff, min_delta + new.red);
2590 new.green = min (0xffff, min_delta + new.green);
2591 new.blue = min (0xffff, min_delta + new.blue);
2595 /* Try to allocate the color. */
2596 success_p = x_alloc_nearest_color (f, cmap, &new);
2597 if (success_p)
2599 if (new.pixel == *pixel)
2601 /* If we end up with the same color as before, try adding
2602 delta to the RGB values. */
2603 x_free_colors (f, &new.pixel, 1);
2605 new.red = min (0xffff, delta + color.red);
2606 new.green = min (0xffff, delta + color.green);
2607 new.blue = min (0xffff, delta + color.blue);
2608 success_p = x_alloc_nearest_color (f, cmap, &new);
2610 else
2611 success_p = true;
2612 *pixel = new.pixel;
2615 return success_p;
2619 /* Set up the foreground color for drawing relief lines of glyph
2620 string S. RELIEF is a pointer to a struct relief containing the GC
2621 with which lines will be drawn. Use a color that is FACTOR or
2622 DELTA lighter or darker than the relief's background which is found
2623 in S->f->output_data.x->relief_background. If such a color cannot
2624 be allocated, use DEFAULT_PIXEL, instead. */
2626 static void
2627 x_setup_relief_color (struct frame *f, struct relief *relief, double factor,
2628 int delta, unsigned long default_pixel)
2630 XGCValues xgcv;
2631 struct x_output *di = f->output_data.x;
2632 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
2633 unsigned long pixel;
2634 unsigned long background = di->relief_background;
2635 Colormap cmap = FRAME_X_COLORMAP (f);
2636 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2637 Display *dpy = FRAME_X_DISPLAY (f);
2639 xgcv.graphics_exposures = False;
2640 xgcv.line_width = 1;
2642 /* Free previously allocated color. The color cell will be reused
2643 when it has been freed as many times as it was allocated, so this
2644 doesn't affect faces using the same colors. */
2645 if (relief->gc && relief->pixel != -1)
2647 x_free_colors (f, &relief->pixel, 1);
2648 relief->pixel = -1;
2651 /* Allocate new color. */
2652 xgcv.foreground = default_pixel;
2653 pixel = background;
2654 if (dpyinfo->n_planes != 1
2655 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
2656 xgcv.foreground = relief->pixel = pixel;
2658 if (relief->gc == 0)
2660 xgcv.stipple = dpyinfo->gray;
2661 mask |= GCStipple;
2662 relief->gc = XCreateGC (dpy, FRAME_X_DRAWABLE (f), mask, &xgcv);
2664 else
2665 XChangeGC (dpy, relief->gc, mask, &xgcv);
2669 /* Set up colors for the relief lines around glyph string S. */
2671 static void
2672 x_setup_relief_colors (struct glyph_string *s)
2674 struct x_output *di = s->f->output_data.x;
2675 unsigned long color;
2677 if (s->face->use_box_color_for_shadows_p)
2678 color = s->face->box_color;
2679 else if (s->first_glyph->type == IMAGE_GLYPH
2680 && s->img->pixmap
2681 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2682 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2683 else
2685 XGCValues xgcv;
2687 /* Get the background color of the face. */
2688 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2689 color = xgcv.background;
2692 if (di->white_relief.gc == 0
2693 || color != di->relief_background)
2695 di->relief_background = color;
2696 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2697 WHITE_PIX_DEFAULT (s->f));
2698 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2699 BLACK_PIX_DEFAULT (s->f));
2704 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2705 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2706 to draw, it must be >= 0. RAISED_P means draw a raised
2707 relief. LEFT_P means draw a relief on the left side of
2708 the rectangle. RIGHT_P means draw a relief on the right
2709 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2710 when drawing. */
2712 static void
2713 x_draw_relief_rect (struct frame *f,
2714 int left_x, int top_y, int right_x, int bottom_y,
2715 int width, bool raised_p, bool top_p, bool bot_p,
2716 bool left_p, bool right_p,
2717 XRectangle *clip_rect)
2719 #ifdef USE_CAIRO
2720 GC top_left_gc, bottom_right_gc;
2721 int corners = 0;
2723 if (raised_p)
2725 top_left_gc = f->output_data.x->white_relief.gc;
2726 bottom_right_gc = f->output_data.x->black_relief.gc;
2728 else
2730 top_left_gc = f->output_data.x->black_relief.gc;
2731 bottom_right_gc = f->output_data.x->white_relief.gc;
2734 x_set_clip_rectangles (f, top_left_gc, clip_rect, 1);
2735 x_set_clip_rectangles (f, bottom_right_gc, clip_rect, 1);
2737 if (left_p)
2739 x_fill_rectangle (f, top_left_gc, left_x, top_y,
2740 width, bottom_y + 1 - top_y);
2741 if (top_p)
2742 corners |= 1 << CORNER_TOP_LEFT;
2743 if (bot_p)
2744 corners |= 1 << CORNER_BOTTOM_LEFT;
2746 if (right_p)
2748 x_fill_rectangle (f, bottom_right_gc, right_x + 1 - width, top_y,
2749 width, bottom_y + 1 - top_y);
2750 if (top_p)
2751 corners |= 1 << CORNER_TOP_RIGHT;
2752 if (bot_p)
2753 corners |= 1 << CORNER_BOTTOM_RIGHT;
2755 if (top_p)
2757 if (!right_p)
2758 x_fill_rectangle (f, top_left_gc, left_x, top_y,
2759 right_x + 1 - left_x, width);
2760 else
2761 x_fill_trapezoid_for_relief (f, top_left_gc, left_x, top_y,
2762 right_x + 1 - left_x, width, 1);
2764 if (bot_p)
2766 if (!left_p)
2767 x_fill_rectangle (f, bottom_right_gc, left_x, bottom_y + 1 - width,
2768 right_x + 1 - left_x, width);
2769 else
2770 x_fill_trapezoid_for_relief (f, bottom_right_gc,
2771 left_x, bottom_y + 1 - width,
2772 right_x + 1 - left_x, width, 0);
2774 if (left_p && width != 1)
2775 x_fill_rectangle (f, bottom_right_gc, left_x, top_y,
2776 1, bottom_y + 1 - top_y);
2777 if (top_p && width != 1)
2778 x_fill_rectangle (f, bottom_right_gc, left_x, top_y,
2779 right_x + 1 - left_x, 1);
2780 if (corners)
2782 XSetBackground (FRAME_X_DISPLAY (f), top_left_gc,
2783 FRAME_BACKGROUND_PIXEL (f));
2784 x_erase_corners_for_relief (f, top_left_gc, left_x, top_y,
2785 right_x - left_x + 1, bottom_y - top_y + 1,
2786 6, 1, corners);
2789 x_reset_clip_rectangles (f, top_left_gc);
2790 x_reset_clip_rectangles (f, bottom_right_gc);
2791 #else
2792 Display *dpy = FRAME_X_DISPLAY (f);
2793 Drawable drawable = FRAME_X_DRAWABLE (f);
2794 int i;
2795 GC gc;
2797 if (raised_p)
2798 gc = f->output_data.x->white_relief.gc;
2799 else
2800 gc = f->output_data.x->black_relief.gc;
2801 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2803 /* This code is more complicated than it has to be, because of two
2804 minor hacks to make the boxes look nicer: (i) if width > 1, draw
2805 the outermost line using the black relief. (ii) Omit the four
2806 corner pixels. */
2808 /* Top. */
2809 if (top_p)
2811 if (width == 1)
2812 XDrawLine (dpy, drawable, gc,
2813 left_x + left_p, top_y,
2814 right_x + !right_p, top_y);
2816 for (i = 1; i < width; ++i)
2817 XDrawLine (dpy, drawable, gc,
2818 left_x + i * left_p, top_y + i,
2819 right_x + 1 - i * right_p, top_y + i);
2822 /* Left. */
2823 if (left_p)
2825 if (width == 1)
2826 XDrawLine (dpy, drawable, gc, left_x, top_y + 1, left_x, bottom_y);
2828 x_clear_area(f, left_x, top_y, 1, 1);
2829 x_clear_area(f, left_x, bottom_y, 1, 1);
2831 for (i = (width > 1 ? 1 : 0); i < width; ++i)
2832 XDrawLine (dpy, drawable, gc,
2833 left_x + i, top_y + (i + 1) * top_p,
2834 left_x + i, bottom_y + 1 - (i + 1) * bot_p);
2837 XSetClipMask (dpy, gc, None);
2838 if (raised_p)
2839 gc = f->output_data.x->black_relief.gc;
2840 else
2841 gc = f->output_data.x->white_relief.gc;
2842 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2844 if (width > 1)
2846 /* Outermost top line. */
2847 if (top_p)
2848 XDrawLine (dpy, drawable, gc,
2849 left_x + left_p, top_y,
2850 right_x + !right_p, top_y);
2852 /* Outermost left line. */
2853 if (left_p)
2854 XDrawLine (dpy, drawable, gc, left_x, top_y + 1, left_x, bottom_y);
2857 /* Bottom. */
2858 if (bot_p)
2860 XDrawLine (dpy, drawable, gc,
2861 left_x + left_p, bottom_y,
2862 right_x + !right_p, bottom_y);
2863 for (i = 1; i < width; ++i)
2864 XDrawLine (dpy, drawable, gc,
2865 left_x + i * left_p, bottom_y - i,
2866 right_x + 1 - i * right_p, bottom_y - i);
2869 /* Right. */
2870 if (right_p)
2872 x_clear_area(f, right_x, top_y, 1, 1);
2873 x_clear_area(f, right_x, bottom_y, 1, 1);
2874 for (i = 0; i < width; ++i)
2875 XDrawLine (dpy, drawable, gc,
2876 right_x - i, top_y + (i + 1) * top_p,
2877 right_x - i, bottom_y + 1 - (i + 1) * bot_p);
2880 x_reset_clip_rectangles (f, gc);
2882 #endif
2886 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2887 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2888 draw, it must be >= 0. LEFT_P means draw a line on the
2889 left side of the rectangle. RIGHT_P means draw a line
2890 on the right side of the rectangle. CLIP_RECT is the clipping
2891 rectangle to use when drawing. */
2893 static void
2894 x_draw_box_rect (struct glyph_string *s,
2895 int left_x, int top_y, int right_x, int bottom_y, int width,
2896 bool left_p, bool right_p, XRectangle *clip_rect)
2898 XGCValues xgcv;
2900 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2901 XSetForeground (s->display, s->gc, s->face->box_color);
2902 x_set_clip_rectangles (s->f, s->gc, clip_rect, 1);
2904 /* Top. */
2905 x_fill_rectangle (s->f, s->gc,
2906 left_x, top_y, right_x - left_x + 1, width);
2908 /* Left. */
2909 if (left_p)
2910 x_fill_rectangle (s->f, s->gc,
2911 left_x, top_y, width, bottom_y - top_y + 1);
2913 /* Bottom. */
2914 x_fill_rectangle (s->f, s->gc,
2915 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2917 /* Right. */
2918 if (right_p)
2919 x_fill_rectangle (s->f, s->gc,
2920 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2922 XSetForeground (s->display, s->gc, xgcv.foreground);
2923 x_reset_clip_rectangles (s->f, s->gc);
2927 /* Draw a box around glyph string S. */
2929 static void
2930 x_draw_glyph_string_box (struct glyph_string *s)
2932 int width, left_x, right_x, top_y, bottom_y, last_x;
2933 bool raised_p, left_p, right_p;
2934 struct glyph *last_glyph;
2935 XRectangle clip_rect;
2937 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2938 ? WINDOW_RIGHT_EDGE_X (s->w)
2939 : window_box_right (s->w, s->area));
2941 /* The glyph that may have a right box line. */
2942 last_glyph = (s->cmp || s->img
2943 ? s->first_glyph
2944 : s->first_glyph + s->nchars - 1);
2946 width = eabs (s->face->box_line_width);
2947 raised_p = s->face->box == FACE_RAISED_BOX;
2948 left_x = s->x;
2949 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2950 ? last_x - 1
2951 : min (last_x, s->x + s->background_width) - 1);
2952 top_y = s->y;
2953 bottom_y = top_y + s->height - 1;
2955 left_p = (s->first_glyph->left_box_line_p
2956 || (s->hl == DRAW_MOUSE_FACE
2957 && (s->prev == NULL
2958 || s->prev->hl != s->hl)));
2959 right_p = (last_glyph->right_box_line_p
2960 || (s->hl == DRAW_MOUSE_FACE
2961 && (s->next == NULL
2962 || s->next->hl != s->hl)));
2964 get_glyph_string_clip_rect (s, &clip_rect);
2966 if (s->face->box == FACE_SIMPLE_BOX)
2967 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2968 left_p, right_p, &clip_rect);
2969 else
2971 x_setup_relief_colors (s);
2972 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2973 width, raised_p, true, true, left_p, right_p,
2974 &clip_rect);
2979 /* Draw foreground of image glyph string S. */
2981 static void
2982 x_draw_image_foreground (struct glyph_string *s)
2984 int x = s->x;
2985 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2987 /* If first glyph of S has a left box line, start drawing it to the
2988 right of that line. */
2989 if (s->face->box != FACE_NO_BOX
2990 && s->first_glyph->left_box_line_p
2991 && s->slice.x == 0)
2992 x += eabs (s->face->box_line_width);
2994 /* If there is a margin around the image, adjust x- and y-position
2995 by that margin. */
2996 if (s->slice.x == 0)
2997 x += s->img->hmargin;
2998 if (s->slice.y == 0)
2999 y += s->img->vmargin;
3001 if (s->img->pixmap)
3003 if (s->img->mask)
3005 /* We can't set both a clip mask and use XSetClipRectangles
3006 because the latter also sets a clip mask. We also can't
3007 trust on the shape extension to be available
3008 (XShapeCombineRegion). So, compute the rectangle to draw
3009 manually. */
3010 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3011 | GCFunction);
3012 XGCValues xgcv;
3013 XRectangle clip_rect, image_rect, r;
3015 xgcv.clip_mask = s->img->mask;
3016 xgcv.clip_x_origin = x;
3017 xgcv.clip_y_origin = y;
3018 xgcv.function = GXcopy;
3019 XChangeGC (s->display, s->gc, mask, &xgcv);
3021 get_glyph_string_clip_rect (s, &clip_rect);
3022 image_rect.x = x;
3023 image_rect.y = y;
3024 image_rect.width = s->slice.width;
3025 image_rect.height = s->slice.height;
3026 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3027 XCopyArea (s->display, s->img->pixmap,
3028 FRAME_X_DRAWABLE (s->f), s->gc,
3029 s->slice.x + r.x - x, s->slice.y + r.y - y,
3030 r.width, r.height, r.x, r.y);
3032 else
3034 XRectangle clip_rect, image_rect, r;
3036 get_glyph_string_clip_rect (s, &clip_rect);
3037 image_rect.x = x;
3038 image_rect.y = y;
3039 image_rect.width = s->slice.width;
3040 image_rect.height = s->slice.height;
3041 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3042 XCopyArea (s->display, s->img->pixmap,
3043 FRAME_X_DRAWABLE (s->f), s->gc,
3044 s->slice.x + r.x - x, s->slice.y + r.y - y,
3045 r.width, r.height, r.x, r.y);
3047 /* When the image has a mask, we can expect that at
3048 least part of a mouse highlight or a block cursor will
3049 be visible. If the image doesn't have a mask, make
3050 a block cursor visible by drawing a rectangle around
3051 the image. I believe it's looking better if we do
3052 nothing here for mouse-face. */
3053 if (s->hl == DRAW_CURSOR)
3055 int relief = eabs (s->img->relief);
3056 x_draw_rectangle (s->f, s->gc,
3057 x - relief, y - relief,
3058 s->slice.width + relief*2 - 1,
3059 s->slice.height + relief*2 - 1);
3063 else
3064 /* Draw a rectangle if image could not be loaded. */
3065 x_draw_rectangle (s->f, s->gc, x, y,
3066 s->slice.width - 1, s->slice.height - 1);
3070 /* Draw a relief around the image glyph string S. */
3072 static void
3073 x_draw_image_relief (struct glyph_string *s)
3075 int x1, y1, thick;
3076 bool raised_p, top_p, bot_p, left_p, right_p;
3077 int extra_x, extra_y;
3078 XRectangle r;
3079 int x = s->x;
3080 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
3082 /* If first glyph of S has a left box line, start drawing it to the
3083 right of that line. */
3084 if (s->face->box != FACE_NO_BOX
3085 && s->first_glyph->left_box_line_p
3086 && s->slice.x == 0)
3087 x += eabs (s->face->box_line_width);
3089 /* If there is a margin around the image, adjust x- and y-position
3090 by that margin. */
3091 if (s->slice.x == 0)
3092 x += s->img->hmargin;
3093 if (s->slice.y == 0)
3094 y += s->img->vmargin;
3096 if (s->hl == DRAW_IMAGE_SUNKEN
3097 || s->hl == DRAW_IMAGE_RAISED)
3099 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
3100 raised_p = s->hl == DRAW_IMAGE_RAISED;
3102 else
3104 thick = eabs (s->img->relief);
3105 raised_p = s->img->relief > 0;
3108 x1 = x + s->slice.width - 1;
3109 y1 = y + s->slice.height - 1;
3111 extra_x = extra_y = 0;
3112 if (s->face->id == TOOL_BAR_FACE_ID)
3114 if (CONSP (Vtool_bar_button_margin)
3115 && INTEGERP (XCAR (Vtool_bar_button_margin))
3116 && INTEGERP (XCDR (Vtool_bar_button_margin)))
3118 extra_x = XINT (XCAR (Vtool_bar_button_margin));
3119 extra_y = XINT (XCDR (Vtool_bar_button_margin));
3121 else if (INTEGERP (Vtool_bar_button_margin))
3122 extra_x = extra_y = XINT (Vtool_bar_button_margin);
3125 top_p = bot_p = left_p = right_p = false;
3127 if (s->slice.x == 0)
3128 x -= thick + extra_x, left_p = true;
3129 if (s->slice.y == 0)
3130 y -= thick + extra_y, top_p = true;
3131 if (s->slice.x + s->slice.width == s->img->width)
3132 x1 += thick + extra_x, right_p = true;
3133 if (s->slice.y + s->slice.height == s->img->height)
3134 y1 += thick + extra_y, bot_p = true;
3136 x_setup_relief_colors (s);
3137 get_glyph_string_clip_rect (s, &r);
3138 x_draw_relief_rect (s->f, x, y, x1, y1, thick, raised_p,
3139 top_p, bot_p, left_p, right_p, &r);
3143 /* Draw the foreground of image glyph string S to PIXMAP. */
3145 static void
3146 x_draw_image_foreground_1 (struct glyph_string *s, Pixmap pixmap)
3148 int x = 0;
3149 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
3151 /* If first glyph of S has a left box line, start drawing it to the
3152 right of that line. */
3153 if (s->face->box != FACE_NO_BOX
3154 && s->first_glyph->left_box_line_p
3155 && s->slice.x == 0)
3156 x += eabs (s->face->box_line_width);
3158 /* If there is a margin around the image, adjust x- and y-position
3159 by that margin. */
3160 if (s->slice.x == 0)
3161 x += s->img->hmargin;
3162 if (s->slice.y == 0)
3163 y += s->img->vmargin;
3165 if (s->img->pixmap)
3167 if (s->img->mask)
3169 /* We can't set both a clip mask and use XSetClipRectangles
3170 because the latter also sets a clip mask. We also can't
3171 trust on the shape extension to be available
3172 (XShapeCombineRegion). So, compute the rectangle to draw
3173 manually. */
3174 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3175 | GCFunction);
3176 XGCValues xgcv;
3178 xgcv.clip_mask = s->img->mask;
3179 xgcv.clip_x_origin = x - s->slice.x;
3180 xgcv.clip_y_origin = y - s->slice.y;
3181 xgcv.function = GXcopy;
3182 XChangeGC (s->display, s->gc, mask, &xgcv);
3184 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3185 s->slice.x, s->slice.y,
3186 s->slice.width, s->slice.height, x, y);
3187 XSetClipMask (s->display, s->gc, None);
3189 else
3191 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3192 s->slice.x, s->slice.y,
3193 s->slice.width, s->slice.height, x, y);
3195 /* When the image has a mask, we can expect that at
3196 least part of a mouse highlight or a block cursor will
3197 be visible. If the image doesn't have a mask, make
3198 a block cursor visible by drawing a rectangle around
3199 the image. I believe it's looking better if we do
3200 nothing here for mouse-face. */
3201 if (s->hl == DRAW_CURSOR)
3203 int r = eabs (s->img->relief);
3204 x_draw_rectangle (s->f, s->gc, x - r, y - r,
3205 s->slice.width + r*2 - 1,
3206 s->slice.height + r*2 - 1);
3210 else
3211 /* Draw a rectangle if image could not be loaded. */
3212 x_draw_rectangle (s->f, s->gc, x, y,
3213 s->slice.width - 1, s->slice.height - 1);
3217 /* Draw part of the background of glyph string S. X, Y, W, and H
3218 give the rectangle to draw. */
3220 static void
3221 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
3223 if (s->stippled_p)
3225 /* Fill background with a stipple pattern. */
3226 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3227 x_fill_rectangle (s->f, s->gc, x, y, w, h);
3228 XSetFillStyle (s->display, s->gc, FillSolid);
3230 else
3231 x_clear_glyph_string_rect (s, x, y, w, h);
3235 /* Draw image glyph string S.
3237 s->y
3238 s->x +-------------------------
3239 | s->face->box
3241 | +-------------------------
3242 | | s->img->margin
3244 | | +-------------------
3245 | | | the image
3249 static void
3250 x_draw_image_glyph_string (struct glyph_string *s)
3252 int box_line_hwidth = eabs (s->face->box_line_width);
3253 int box_line_vwidth = max (s->face->box_line_width, 0);
3254 int height;
3255 Pixmap pixmap = None;
3257 height = s->height;
3258 if (s->slice.y == 0)
3259 height -= box_line_vwidth;
3260 if (s->slice.y + s->slice.height >= s->img->height)
3261 height -= box_line_vwidth;
3263 /* Fill background with face under the image. Do it only if row is
3264 taller than image or if image has a clip mask to reduce
3265 flickering. */
3266 s->stippled_p = s->face->stipple != 0;
3267 if (height > s->slice.height
3268 || s->img->hmargin
3269 || s->img->vmargin
3270 || s->img->mask
3271 || s->img->pixmap == 0
3272 || s->width != s->background_width)
3274 if (s->img->mask)
3276 /* Create a pixmap as large as the glyph string. Fill it
3277 with the background color. Copy the image to it, using
3278 its mask. Copy the temporary pixmap to the display. */
3279 Screen *screen = FRAME_X_SCREEN (s->f);
3280 int depth = DefaultDepthOfScreen (screen);
3282 /* Create a pixmap as large as the glyph string. */
3283 pixmap = XCreatePixmap (s->display, FRAME_X_DRAWABLE (s->f),
3284 s->background_width,
3285 s->height, depth);
3287 /* Don't clip in the following because we're working on the
3288 pixmap. */
3289 XSetClipMask (s->display, s->gc, None);
3291 /* Fill the pixmap with the background color/stipple. */
3292 if (s->stippled_p)
3294 /* Fill background with a stipple pattern. */
3295 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3296 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
3297 XFillRectangle (s->display, pixmap, s->gc,
3298 0, 0, s->background_width, s->height);
3299 XSetFillStyle (s->display, s->gc, FillSolid);
3300 XSetTSOrigin (s->display, s->gc, 0, 0);
3302 else
3304 XGCValues xgcv;
3305 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3306 &xgcv);
3307 XSetForeground (s->display, s->gc, xgcv.background);
3308 XFillRectangle (s->display, pixmap, s->gc,
3309 0, 0, s->background_width, s->height);
3310 XSetForeground (s->display, s->gc, xgcv.foreground);
3313 else
3315 int x = s->x;
3316 int y = s->y;
3317 int width = s->background_width;
3319 if (s->first_glyph->left_box_line_p
3320 && s->slice.x == 0)
3322 x += box_line_hwidth;
3323 width -= box_line_hwidth;
3326 if (s->slice.y == 0)
3327 y += box_line_vwidth;
3329 x_draw_glyph_string_bg_rect (s, x, y, width, height);
3332 s->background_filled_p = true;
3335 /* Draw the foreground. */
3336 #ifdef USE_CAIRO
3337 if (s->img->cr_data)
3339 cairo_t *cr = x_begin_cr_clip (s->f, s->gc);
3341 int x = s->x + s->img->hmargin;
3342 int y = s->y + s->img->vmargin;
3343 int width = s->background_width;
3345 cairo_set_source_surface (cr, s->img->cr_data,
3346 x - s->slice.x,
3347 y - s->slice.y);
3348 cairo_rectangle (cr, x, y, width, height);
3349 cairo_fill (cr);
3350 x_end_cr_clip (s->f);
3352 else
3353 #endif
3354 if (pixmap != None)
3356 x_draw_image_foreground_1 (s, pixmap);
3357 x_set_glyph_string_clipping (s);
3358 XCopyArea (s->display, pixmap, FRAME_X_DRAWABLE (s->f), s->gc,
3359 0, 0, s->background_width, s->height, s->x, s->y);
3360 XFreePixmap (s->display, pixmap);
3362 else
3363 x_draw_image_foreground (s);
3365 /* If we must draw a relief around the image, do it. */
3366 if (s->img->relief
3367 || s->hl == DRAW_IMAGE_RAISED
3368 || s->hl == DRAW_IMAGE_SUNKEN)
3369 x_draw_image_relief (s);
3373 /* Draw stretch glyph string S. */
3375 static void
3376 x_draw_stretch_glyph_string (struct glyph_string *s)
3378 eassert (s->first_glyph->type == STRETCH_GLYPH);
3380 if (s->hl == DRAW_CURSOR
3381 && !x_stretch_cursor_p)
3383 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
3384 wide as the stretch glyph. */
3385 int width, background_width = s->background_width;
3386 int x = s->x;
3388 if (!s->row->reversed_p)
3390 int left_x = window_box_left_offset (s->w, TEXT_AREA);
3392 if (x < left_x)
3394 background_width -= left_x - x;
3395 x = left_x;
3398 else
3400 /* In R2L rows, draw the cursor on the right edge of the
3401 stretch glyph. */
3402 int right_x = window_box_right (s->w, TEXT_AREA);
3404 if (x + background_width > right_x)
3405 background_width -= x - right_x;
3406 x += background_width;
3408 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
3409 if (s->row->reversed_p)
3410 x -= width;
3412 /* Draw cursor. */
3413 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
3415 /* Clear rest using the GC of the original non-cursor face. */
3416 if (width < background_width)
3418 int y = s->y;
3419 int w = background_width - width, h = s->height;
3420 XRectangle r;
3421 GC gc;
3423 if (!s->row->reversed_p)
3424 x += width;
3425 else
3426 x = s->x;
3427 if (s->row->mouse_face_p
3428 && cursor_in_mouse_face_p (s->w))
3430 x_set_mouse_face_gc (s);
3431 gc = s->gc;
3433 else
3434 gc = s->face->gc;
3436 get_glyph_string_clip_rect (s, &r);
3437 x_set_clip_rectangles (s->f, gc, &r, 1);
3439 if (s->face->stipple)
3441 /* Fill background with a stipple pattern. */
3442 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3443 x_fill_rectangle (s->f, gc, x, y, w, h);
3444 XSetFillStyle (s->display, gc, FillSolid);
3446 else
3448 XGCValues xgcv;
3449 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3450 XSetForeground (s->display, gc, xgcv.background);
3451 x_fill_rectangle (s->f, gc, x, y, w, h);
3452 XSetForeground (s->display, gc, xgcv.foreground);
3455 x_reset_clip_rectangles (s->f, gc);
3458 else if (!s->background_filled_p)
3460 int background_width = s->background_width;
3461 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
3463 /* Don't draw into left margin, fringe or scrollbar area
3464 except for header line and mode line. */
3465 if (x < left_x && !s->row->mode_line_p)
3467 background_width -= left_x - x;
3468 x = left_x;
3470 if (background_width > 0)
3471 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
3474 s->background_filled_p = true;
3477 static void
3478 x_get_scale_factor(Display *disp, int *scale_x, int *scale_y)
3480 const int base_res = 96;
3481 struct x_display_info * dpyinfo = x_display_info_for_display (disp);
3483 *scale_x = *scale_y = 1;
3485 if (dpyinfo)
3487 if (dpyinfo->resx > base_res)
3488 *scale_x = floor (dpyinfo->resx / base_res);
3489 if (dpyinfo->resy > base_res)
3490 *scale_y = floor (dpyinfo->resy / base_res);
3495 Draw a wavy line under S. The wave fills wave_height pixels from y0.
3497 x0 wave_length = 2
3499 y0 * * * * *
3500 |* * * * * * * * *
3501 wave_height = 3 | * * * *
3504 static void
3505 x_draw_underwave (struct glyph_string *s)
3507 /* Adjust for scale/HiDPI. */
3508 int scale_x, scale_y;
3510 x_get_scale_factor (s->display, &scale_x, &scale_y);
3512 int wave_height = 3 * scale_y, wave_length = 2 * scale_x, thickness = scale_y;
3514 #ifdef USE_CAIRO
3515 x_draw_horizontal_wave (s->f, s->gc, s->x, s->ybase - wave_height + 3,
3516 s->width, wave_height, wave_length);
3517 #else /* not USE_CAIRO */
3518 int dx, dy, x0, y0, width, x1, y1, x2, y2, xmax;
3519 bool odd;
3520 XRectangle wave_clip, string_clip, final_clip;
3522 dx = wave_length;
3523 dy = wave_height - 1;
3524 x0 = s->x;
3525 y0 = s->ybase + wave_height / 2 - scale_y;
3526 width = s->width;
3527 xmax = x0 + width;
3529 /* Find and set clipping rectangle */
3531 wave_clip.x = x0;
3532 wave_clip.y = y0;
3533 wave_clip.width = width;
3534 wave_clip.height = wave_height;
3535 get_glyph_string_clip_rect (s, &string_clip);
3537 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
3538 return;
3540 XSetClipRectangles (s->display, s->gc, 0, 0, &final_clip, 1, Unsorted);
3542 /* Draw the waves */
3544 x1 = x0 - (x0 % dx);
3545 x2 = x1 + dx;
3546 odd = (x1 / dx) & 1;
3547 y1 = y2 = y0;
3549 if (odd)
3550 y1 += dy;
3551 else
3552 y2 += dy;
3554 if (INT_MAX - dx < xmax)
3555 emacs_abort ();
3557 while (x1 <= xmax)
3559 XSetLineAttributes (s->display, s->gc, thickness, LineSolid, CapButt,
3560 JoinRound);
3561 XDrawLine (s->display, FRAME_X_DRAWABLE (s->f), s->gc, x1, y1, x2, y2);
3562 x1 = x2, y1 = y2;
3563 x2 += dx, y2 = y0 + odd*dy;
3564 odd = !odd;
3567 /* Restore previous clipping rectangle(s) */
3568 XSetClipRectangles (s->display, s->gc, 0, 0, s->clip, s->num_clips, Unsorted);
3569 #endif /* not USE_CAIRO */
3573 /* Draw glyph string S. */
3575 static void
3576 x_draw_glyph_string (struct glyph_string *s)
3578 bool relief_drawn_p = false;
3580 /* If S draws into the background of its successors, draw the
3581 background of the successors first so that S can draw into it.
3582 This makes S->next use XDrawString instead of XDrawImageString. */
3583 if (s->next && s->right_overhang && !s->for_overlaps)
3585 int width;
3586 struct glyph_string *next;
3588 for (width = 0, next = s->next;
3589 next && width < s->right_overhang;
3590 width += next->width, next = next->next)
3591 if (next->first_glyph->type != IMAGE_GLYPH)
3593 x_set_glyph_string_gc (next);
3594 x_set_glyph_string_clipping (next);
3595 if (next->first_glyph->type == STRETCH_GLYPH)
3596 x_draw_stretch_glyph_string (next);
3597 else
3598 x_draw_glyph_string_background (next, true);
3599 next->num_clips = 0;
3603 /* Set up S->gc, set clipping and draw S. */
3604 x_set_glyph_string_gc (s);
3606 /* Draw relief (if any) in advance for char/composition so that the
3607 glyph string can be drawn over it. */
3608 if (!s->for_overlaps
3609 && s->face->box != FACE_NO_BOX
3610 && (s->first_glyph->type == CHAR_GLYPH
3611 || s->first_glyph->type == COMPOSITE_GLYPH))
3614 x_set_glyph_string_clipping (s);
3615 x_draw_glyph_string_background (s, true);
3616 x_draw_glyph_string_box (s);
3617 x_set_glyph_string_clipping (s);
3618 relief_drawn_p = true;
3620 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
3621 && !s->clip_tail
3622 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
3623 || (s->next && s->next->hl != s->hl && s->right_overhang)))
3624 /* We must clip just this glyph. left_overhang part has already
3625 drawn when s->prev was drawn, and right_overhang part will be
3626 drawn later when s->next is drawn. */
3627 x_set_glyph_string_clipping_exactly (s, s);
3628 else
3629 x_set_glyph_string_clipping (s);
3631 switch (s->first_glyph->type)
3633 case IMAGE_GLYPH:
3634 x_draw_image_glyph_string (s);
3635 break;
3637 case XWIDGET_GLYPH:
3638 x_draw_xwidget_glyph_string (s);
3639 break;
3641 case STRETCH_GLYPH:
3642 x_draw_stretch_glyph_string (s);
3643 break;
3645 case CHAR_GLYPH:
3646 if (s->for_overlaps)
3647 s->background_filled_p = true;
3648 else
3649 x_draw_glyph_string_background (s, false);
3650 x_draw_glyph_string_foreground (s);
3651 break;
3653 case COMPOSITE_GLYPH:
3654 if (s->for_overlaps || (s->cmp_from > 0
3655 && ! s->first_glyph->u.cmp.automatic))
3656 s->background_filled_p = true;
3657 else
3658 x_draw_glyph_string_background (s, true);
3659 x_draw_composite_glyph_string_foreground (s);
3660 break;
3662 case GLYPHLESS_GLYPH:
3663 if (s->for_overlaps)
3664 s->background_filled_p = true;
3665 else
3666 x_draw_glyph_string_background (s, true);
3667 x_draw_glyphless_glyph_string_foreground (s);
3668 break;
3670 default:
3671 emacs_abort ();
3674 if (!s->for_overlaps)
3676 /* Draw underline. */
3677 if (s->face->underline_p)
3679 if (s->face->underline_type == FACE_UNDER_WAVE)
3681 if (s->face->underline_defaulted_p)
3682 x_draw_underwave (s);
3683 else
3685 XGCValues xgcv;
3686 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3687 XSetForeground (s->display, s->gc, s->face->underline_color);
3688 x_draw_underwave (s);
3689 XSetForeground (s->display, s->gc, xgcv.foreground);
3692 else if (s->face->underline_type == FACE_UNDER_LINE)
3694 unsigned long thickness, position;
3695 int y;
3697 if (s->prev && s->prev->face->underline_p
3698 && s->prev->face->underline_type == FACE_UNDER_LINE)
3700 /* We use the same underline style as the previous one. */
3701 thickness = s->prev->underline_thickness;
3702 position = s->prev->underline_position;
3704 else
3706 struct font *font = font_for_underline_metrics (s);
3708 /* Get the underline thickness. Default is 1 pixel. */
3709 if (font && font->underline_thickness > 0)
3710 thickness = font->underline_thickness;
3711 else
3712 thickness = 1;
3713 if (x_underline_at_descent_line)
3714 position = (s->height - thickness) - (s->ybase - s->y);
3715 else
3717 /* Get the underline position. This is the recommended
3718 vertical offset in pixels from the baseline to the top of
3719 the underline. This is a signed value according to the
3720 specs, and its default is
3722 ROUND ((maximum descent) / 2), with
3723 ROUND(x) = floor (x + 0.5) */
3725 if (x_use_underline_position_properties
3726 && font && font->underline_position >= 0)
3727 position = font->underline_position;
3728 else if (font)
3729 position = (font->descent + 1) / 2;
3730 else
3731 position = underline_minimum_offset;
3733 position = max (position, underline_minimum_offset);
3735 /* Check the sanity of thickness and position. We should
3736 avoid drawing underline out of the current line area. */
3737 if (s->y + s->height <= s->ybase + position)
3738 position = (s->height - 1) - (s->ybase - s->y);
3739 if (s->y + s->height < s->ybase + position + thickness)
3740 thickness = (s->y + s->height) - (s->ybase + position);
3741 s->underline_thickness = thickness;
3742 s->underline_position = position;
3743 y = s->ybase + position;
3744 if (s->face->underline_defaulted_p)
3745 x_fill_rectangle (s->f, s->gc,
3746 s->x, y, s->width, thickness);
3747 else
3749 XGCValues xgcv;
3750 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3751 XSetForeground (s->display, s->gc, s->face->underline_color);
3752 x_fill_rectangle (s->f, s->gc,
3753 s->x, y, s->width, thickness);
3754 XSetForeground (s->display, s->gc, xgcv.foreground);
3758 /* Draw overline. */
3759 if (s->face->overline_p)
3761 unsigned long dy = 0, h = 1;
3763 if (s->face->overline_color_defaulted_p)
3764 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3765 s->width, h);
3766 else
3768 XGCValues xgcv;
3769 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3770 XSetForeground (s->display, s->gc, s->face->overline_color);
3771 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3772 s->width, h);
3773 XSetForeground (s->display, s->gc, xgcv.foreground);
3777 /* Draw strike-through. */
3778 if (s->face->strike_through_p)
3780 /* Y-coordinate and height of the glyph string's first
3781 glyph. We cannot use s->y and s->height because those
3782 could be larger if there are taller display elements
3783 (e.g., characters displayed with a larger font) in the
3784 same glyph row. */
3785 int glyph_y = s->ybase - s->first_glyph->ascent;
3786 int glyph_height = s->first_glyph->ascent + s->first_glyph->descent;
3787 /* Strike-through width and offset from the glyph string's
3788 top edge. */
3789 unsigned long h = 1;
3790 unsigned long dy = (glyph_height - h) / 2;
3792 if (s->face->strike_through_color_defaulted_p)
3793 x_fill_rectangle (s->f, s->gc, s->x, glyph_y + dy,
3794 s->width, h);
3795 else
3797 XGCValues xgcv;
3798 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3799 XSetForeground (s->display, s->gc, s->face->strike_through_color);
3800 x_fill_rectangle (s->f, s->gc, s->x, glyph_y + dy,
3801 s->width, h);
3802 XSetForeground (s->display, s->gc, xgcv.foreground);
3806 /* Draw relief if not yet drawn. */
3807 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
3808 x_draw_glyph_string_box (s);
3810 if (s->prev)
3812 struct glyph_string *prev;
3814 for (prev = s->prev; prev; prev = prev->prev)
3815 if (prev->hl != s->hl
3816 && prev->x + prev->width + prev->right_overhang > s->x)
3818 /* As prev was drawn while clipped to its own area, we
3819 must draw the right_overhang part using s->hl now. */
3820 enum draw_glyphs_face save = prev->hl;
3822 prev->hl = s->hl;
3823 x_set_glyph_string_gc (prev);
3824 x_set_glyph_string_clipping_exactly (s, prev);
3825 if (prev->first_glyph->type == CHAR_GLYPH)
3826 x_draw_glyph_string_foreground (prev);
3827 else
3828 x_draw_composite_glyph_string_foreground (prev);
3829 x_reset_clip_rectangles (prev->f, prev->gc);
3830 prev->hl = save;
3831 prev->num_clips = 0;
3835 if (s->next)
3837 struct glyph_string *next;
3839 for (next = s->next; next; next = next->next)
3840 if (next->hl != s->hl
3841 && next->x - next->left_overhang < s->x + s->width)
3843 /* As next will be drawn while clipped to its own area,
3844 we must draw the left_overhang part using s->hl now. */
3845 enum draw_glyphs_face save = next->hl;
3847 next->hl = s->hl;
3848 x_set_glyph_string_gc (next);
3849 x_set_glyph_string_clipping_exactly (s, next);
3850 if (next->first_glyph->type == CHAR_GLYPH)
3851 x_draw_glyph_string_foreground (next);
3852 else
3853 x_draw_composite_glyph_string_foreground (next);
3854 x_reset_clip_rectangles (next->f, next->gc);
3855 next->hl = save;
3856 next->num_clips = 0;
3857 next->clip_head = s->next;
3862 /* Reset clipping. */
3863 x_reset_clip_rectangles (s->f, s->gc);
3864 s->num_clips = 0;
3867 /* Shift display to make room for inserted glyphs. */
3869 static void
3870 x_shift_glyphs_for_insert (struct frame *f, int x, int y, int width, int height, int shift_by)
3872 /* Never called on a GUI frame, see
3873 https://lists.gnu.org/archive/html/emacs-devel/2015-05/msg00456.html
3875 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f), FRAME_X_DRAWABLE (f),
3876 f->output_data.x->normal_gc,
3877 x, y, width, height,
3878 x + shift_by, y);
3881 /* Delete N glyphs at the nominal cursor position. Not implemented
3882 for X frames. */
3884 static void
3885 x_delete_glyphs (struct frame *f, register int n)
3887 emacs_abort ();
3891 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
3892 If they are <= 0, this is probably an error. */
3894 static ATTRIBUTE_UNUSED void
3895 x_clear_area1 (Display *dpy, Window window,
3896 int x, int y, int width, int height, int exposures)
3898 eassert (width > 0 && height > 0);
3899 XClearArea (dpy, window, x, y, width, height, exposures);
3902 void
3903 x_clear_area (struct frame *f, int x, int y, int width, int height)
3905 #ifdef USE_CAIRO
3906 cairo_t *cr;
3908 eassert (width > 0 && height > 0);
3910 cr = x_begin_cr_clip (f, NULL);
3911 x_set_cr_source_with_gc_background (f, f->output_data.x->normal_gc);
3912 cairo_rectangle (cr, x, y, width, height);
3913 cairo_fill (cr);
3914 x_end_cr_clip (f);
3915 #else
3916 if (FRAME_X_DOUBLE_BUFFERED_P (f))
3917 XFillRectangle (FRAME_X_DISPLAY (f),
3918 FRAME_X_DRAWABLE (f),
3919 f->output_data.x->reverse_gc,
3920 x, y, width, height);
3921 else
3922 x_clear_area1 (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3923 x, y, width, height, False);
3924 #endif
3928 /* Clear an entire frame. */
3930 static void
3931 x_clear_frame (struct frame *f)
3933 /* Clearing the frame will erase any cursor, so mark them all as no
3934 longer visible. */
3935 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
3937 block_input ();
3939 font_drop_xrender_surfaces (f);
3940 x_clear_window (f);
3942 /* We have to clear the scroll bars. If we have changed colors or
3943 something like that, then they should be notified. */
3944 x_scroll_bar_clear (f);
3946 XFlush (FRAME_X_DISPLAY (f));
3948 unblock_input ();
3951 /* RIF: Show hourglass cursor on frame F. */
3953 static void
3954 x_show_hourglass (struct frame *f)
3956 Display *dpy = FRAME_X_DISPLAY (f);
3958 if (dpy)
3960 struct x_output *x = FRAME_X_OUTPUT (f);
3961 #ifdef USE_X_TOOLKIT
3962 if (x->widget)
3963 #else
3964 if (FRAME_OUTER_WINDOW (f))
3965 #endif
3967 x->hourglass_p = true;
3969 if (!x->hourglass_window)
3971 unsigned long mask = CWCursor;
3972 XSetWindowAttributes attrs;
3973 #ifdef USE_GTK
3974 Window parent = FRAME_X_WINDOW (f);
3975 #else
3976 Window parent = FRAME_OUTER_WINDOW (f);
3977 #endif
3978 attrs.cursor = x->hourglass_cursor;
3980 x->hourglass_window = XCreateWindow
3981 (dpy, parent, 0, 0, 32000, 32000, 0, 0,
3982 InputOnly, CopyFromParent, mask, &attrs);
3985 XMapRaised (dpy, x->hourglass_window);
3986 XFlush (dpy);
3991 /* RIF: Cancel hourglass cursor on frame F. */
3993 static void
3994 x_hide_hourglass (struct frame *f)
3996 struct x_output *x = FRAME_X_OUTPUT (f);
3998 /* Watch out for newly created frames. */
3999 if (x->hourglass_window)
4001 XUnmapWindow (FRAME_X_DISPLAY (f), x->hourglass_window);
4002 /* Sync here because XTread_socket looks at the
4003 hourglass_p flag that is reset to zero below. */
4004 XSync (FRAME_X_DISPLAY (f), False);
4005 x->hourglass_p = false;
4009 /* Invert the middle quarter of the frame for .15 sec. */
4011 static void
4012 XTflash (struct frame *f)
4014 block_input ();
4017 #ifdef USE_GTK
4018 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
4019 when the scroll bars and the edit widget share the same X window. */
4020 GdkWindow *window = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
4021 #ifdef HAVE_GTK3
4022 #if GTK_CHECK_VERSION (3, 22, 0)
4023 cairo_region_t *region = gdk_window_get_visible_region (window);
4024 GdkDrawingContext *context = gdk_window_begin_draw_frame (window, region);
4025 cairo_t *cr = gdk_drawing_context_get_cairo_context (context);
4026 #else
4027 cairo_t *cr = gdk_cairo_create (window);
4028 #endif
4029 cairo_set_source_rgb (cr, 1, 1, 1);
4030 cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
4031 #define XFillRectangle(d, win, gc, x, y, w, h) \
4032 do { \
4033 cairo_rectangle (cr, x, y, w, h); \
4034 cairo_fill (cr); \
4036 while (false)
4037 #else /* ! HAVE_GTK3 */
4038 GdkGCValues vals;
4039 GdkGC *gc;
4040 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
4041 ^ FRAME_BACKGROUND_PIXEL (f));
4042 vals.function = GDK_XOR;
4043 gc = gdk_gc_new_with_values (window,
4044 &vals, GDK_GC_FUNCTION | GDK_GC_FOREGROUND);
4045 #define XFillRectangle(d, win, gc, x, y, w, h) \
4046 gdk_draw_rectangle (window, gc, true, x, y, w, h)
4047 #endif /* ! HAVE_GTK3 */
4048 #else /* ! USE_GTK */
4049 GC gc;
4051 /* Create a GC that will use the GXxor function to flip foreground
4052 pixels into background pixels. */
4054 XGCValues values;
4056 values.function = GXxor;
4057 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
4058 ^ FRAME_BACKGROUND_PIXEL (f));
4060 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4061 GCFunction | GCForeground, &values);
4063 #endif
4065 /* Get the height not including a menu bar widget. */
4066 int height = FRAME_PIXEL_HEIGHT (f);
4067 /* Height of each line to flash. */
4068 int flash_height = FRAME_LINE_HEIGHT (f);
4069 /* These will be the left and right margins of the rectangles. */
4070 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
4071 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
4072 int width = flash_right - flash_left;
4074 /* If window is tall, flash top and bottom line. */
4075 if (height > 3 * FRAME_LINE_HEIGHT (f))
4077 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4078 flash_left,
4079 (FRAME_INTERNAL_BORDER_WIDTH (f)
4080 + FRAME_TOP_MARGIN_HEIGHT (f)),
4081 width, flash_height);
4082 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4083 flash_left,
4084 (height - flash_height
4085 - FRAME_INTERNAL_BORDER_WIDTH (f)),
4086 width, flash_height);
4089 else
4090 /* If it is short, flash it all. */
4091 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4092 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
4093 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
4095 x_flush (f);
4098 struct timespec delay = make_timespec (0, 150 * 1000 * 1000);
4099 struct timespec wakeup = timespec_add (current_timespec (), delay);
4101 /* Keep waiting until past the time wakeup or any input gets
4102 available. */
4103 while (! detect_input_pending ())
4105 struct timespec current = current_timespec ();
4106 struct timespec timeout;
4108 /* Break if result would not be positive. */
4109 if (timespec_cmp (wakeup, current) <= 0)
4110 break;
4112 /* How long `select' should wait. */
4113 timeout = make_timespec (0, 10 * 1000 * 1000);
4115 /* Try to wait that long--but we might wake up sooner. */
4116 pselect (0, NULL, NULL, NULL, &timeout, NULL);
4120 /* If window is tall, flash top and bottom line. */
4121 if (height > 3 * FRAME_LINE_HEIGHT (f))
4123 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4124 flash_left,
4125 (FRAME_INTERNAL_BORDER_WIDTH (f)
4126 + FRAME_TOP_MARGIN_HEIGHT (f)),
4127 width, flash_height);
4128 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4129 flash_left,
4130 (height - flash_height
4131 - FRAME_INTERNAL_BORDER_WIDTH (f)),
4132 width, flash_height);
4134 else
4135 /* If it is short, flash it all. */
4136 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4137 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
4138 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
4140 #ifdef USE_GTK
4141 #ifdef HAVE_GTK3
4142 #if GTK_CHECK_VERSION (3, 22, 0)
4143 gdk_window_end_draw_frame (window, context);
4144 cairo_region_destroy (region);
4145 #else
4146 cairo_destroy (cr);
4147 #endif
4148 #else
4149 g_object_unref (G_OBJECT (gc));
4150 #endif
4151 #undef XFillRectangle
4152 #else
4153 XFreeGC (FRAME_X_DISPLAY (f), gc);
4154 #endif
4155 x_flush (f);
4159 unblock_input ();
4163 static void
4164 XTtoggle_invisible_pointer (struct frame *f, bool invisible)
4166 block_input ();
4167 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, invisible);
4168 unblock_input ();
4172 /* Make audible bell. */
4174 static void
4175 XTring_bell (struct frame *f)
4177 if (FRAME_X_DISPLAY (f))
4179 if (visible_bell)
4180 XTflash (f);
4181 else
4183 block_input ();
4184 #ifdef HAVE_XKB
4185 XkbBell (FRAME_X_DISPLAY (f), None, 0, None);
4186 #else
4187 XBell (FRAME_X_DISPLAY (f), 0);
4188 #endif
4189 XFlush (FRAME_X_DISPLAY (f));
4190 unblock_input ();
4195 /***********************************************************************
4196 Line Dance
4197 ***********************************************************************/
4199 /* Perform an insert-lines or delete-lines operation, inserting N
4200 lines or deleting -N lines at vertical position VPOS. */
4202 static void
4203 x_ins_del_lines (struct frame *f, int vpos, int n)
4205 emacs_abort ();
4209 /* Scroll part of the display as described by RUN. */
4211 static void
4212 x_scroll_run (struct window *w, struct run *run)
4214 struct frame *f = XFRAME (w->frame);
4215 int x, y, width, height, from_y, to_y, bottom_y;
4217 /* Get frame-relative bounding box of the text display area of W,
4218 without mode lines. Include in this box the left and right
4219 fringe of W. */
4220 window_box (w, ANY_AREA, &x, &y, &width, &height);
4222 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
4223 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
4224 bottom_y = y + height;
4226 if (to_y < from_y)
4228 /* Scrolling up. Make sure we don't copy part of the mode
4229 line at the bottom. */
4230 if (from_y + run->height > bottom_y)
4231 height = bottom_y - from_y;
4232 else
4233 height = run->height;
4235 else
4237 /* Scrolling down. Make sure we don't copy over the mode line.
4238 at the bottom. */
4239 if (to_y + run->height > bottom_y)
4240 height = bottom_y - to_y;
4241 else
4242 height = run->height;
4245 block_input ();
4247 /* Cursor off. Will be switched on again in x_update_window_end. */
4248 x_clear_cursor (w);
4250 #ifdef USE_CAIRO
4251 SET_FRAME_GARBAGED (f);
4252 #else
4253 XCopyArea (FRAME_X_DISPLAY (f),
4254 FRAME_X_DRAWABLE (f), FRAME_X_DRAWABLE (f),
4255 f->output_data.x->normal_gc,
4256 x, from_y,
4257 width, height,
4258 x, to_y);
4259 #endif
4261 unblock_input ();
4266 /***********************************************************************
4267 Exposure Events
4268 ***********************************************************************/
4271 static void
4272 frame_highlight (struct frame *f)
4274 /* We used to only do this if Vx_no_window_manager was non-nil, but
4275 the ICCCM (section 4.1.6) says that the window's border pixmap
4276 and border pixel are window attributes which are "private to the
4277 client", so we can always change it to whatever we want. */
4278 block_input ();
4279 /* I recently started to get errors in this XSetWindowBorder, depending on
4280 the window-manager in use, tho something more is at play since I've been
4281 using that same window-manager binary for ever. Let's not crash just
4282 because of this (bug#9310). */
4283 x_catch_errors (FRAME_X_DISPLAY (f));
4284 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4285 f->output_data.x->border_pixel);
4286 x_uncatch_errors ();
4287 unblock_input ();
4288 x_update_cursor (f, true);
4289 x_set_frame_alpha (f);
4292 static void
4293 frame_unhighlight (struct frame *f)
4295 /* We used to only do this if Vx_no_window_manager was non-nil, but
4296 the ICCCM (section 4.1.6) says that the window's border pixmap
4297 and border pixel are window attributes which are "private to the
4298 client", so we can always change it to whatever we want. */
4299 block_input ();
4300 /* Same as above for XSetWindowBorder (bug#9310). */
4301 x_catch_errors (FRAME_X_DISPLAY (f));
4302 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4303 f->output_data.x->border_tile);
4304 x_uncatch_errors ();
4305 unblock_input ();
4306 x_update_cursor (f, true);
4307 x_set_frame_alpha (f);
4310 /* The focus has changed. Update the frames as necessary to reflect
4311 the new situation. Note that we can't change the selected frame
4312 here, because the Lisp code we are interrupting might become confused.
4313 Each event gets marked with the frame in which it occurred, so the
4314 Lisp code can tell when the switch took place by examining the events. */
4316 static void
4317 x_new_focus_frame (struct x_display_info *dpyinfo, struct frame *frame)
4319 struct frame *old_focus = dpyinfo->x_focus_frame;
4321 if (frame != dpyinfo->x_focus_frame)
4323 /* Set this before calling other routines, so that they see
4324 the correct value of x_focus_frame. */
4325 dpyinfo->x_focus_frame = frame;
4327 if (old_focus && old_focus->auto_lower)
4328 x_lower_frame (old_focus);
4330 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
4331 dpyinfo->x_pending_autoraise_frame = dpyinfo->x_focus_frame;
4332 else
4333 dpyinfo->x_pending_autoraise_frame = NULL;
4336 x_frame_rehighlight (dpyinfo);
4339 /* Handle FocusIn and FocusOut state changes for FRAME.
4340 If FRAME has focus and there exists more than one frame, puts
4341 a FOCUS_IN_EVENT into *BUFP. */
4343 static void
4344 x_focus_changed (int type, int state, struct x_display_info *dpyinfo, struct frame *frame, struct input_event *bufp)
4346 if (type == FocusIn)
4348 if (dpyinfo->x_focus_event_frame != frame)
4350 x_new_focus_frame (dpyinfo, frame);
4351 dpyinfo->x_focus_event_frame = frame;
4353 /* Don't stop displaying the initial startup message
4354 for a switch-frame event we don't need. */
4355 /* When run as a daemon, Vterminal_frame is always NIL. */
4356 bufp->arg = (((NILP (Vterminal_frame)
4357 || ! FRAME_X_P (XFRAME (Vterminal_frame))
4358 || EQ (Fdaemonp (), Qt))
4359 && CONSP (Vframe_list)
4360 && !NILP (XCDR (Vframe_list)))
4361 ? Qt : Qnil);
4362 bufp->kind = FOCUS_IN_EVENT;
4363 XSETFRAME (bufp->frame_or_window, frame);
4366 frame->output_data.x->focus_state |= state;
4368 #ifdef HAVE_X_I18N
4369 if (FRAME_XIC (frame))
4370 XSetICFocus (FRAME_XIC (frame));
4371 #endif
4373 else if (type == FocusOut)
4375 frame->output_data.x->focus_state &= ~state;
4377 if (dpyinfo->x_focus_event_frame == frame)
4379 dpyinfo->x_focus_event_frame = 0;
4380 x_new_focus_frame (dpyinfo, 0);
4382 bufp->kind = FOCUS_OUT_EVENT;
4383 XSETFRAME (bufp->frame_or_window, frame);
4386 #ifdef HAVE_X_I18N
4387 if (FRAME_XIC (frame))
4388 XUnsetICFocus (FRAME_XIC (frame));
4389 #endif
4390 if (frame->pointer_invisible)
4391 XTtoggle_invisible_pointer (frame, false);
4395 /* Return the Emacs frame-object corresponding to an X window.
4396 It could be the frame's main window or an icon window. */
4398 static struct frame *
4399 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4401 Lisp_Object tail, frame;
4402 struct frame *f;
4404 if (wdesc == None)
4405 return NULL;
4407 FOR_EACH_FRAME (tail, frame)
4409 f = XFRAME (frame);
4410 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4411 continue;
4412 if (f->output_data.x->hourglass_window == wdesc)
4413 return f;
4414 #ifdef USE_X_TOOLKIT
4415 if ((f->output_data.x->edit_widget
4416 && XtWindow (f->output_data.x->edit_widget) == wdesc)
4417 /* A tooltip frame? */
4418 || (!f->output_data.x->edit_widget
4419 && FRAME_X_WINDOW (f) == wdesc)
4420 || f->output_data.x->icon_desc == wdesc)
4421 return f;
4422 #else /* not USE_X_TOOLKIT */
4423 #ifdef USE_GTK
4424 if (f->output_data.x->edit_widget)
4426 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4427 struct x_output *x = f->output_data.x;
4428 if (gwdesc != 0 && gwdesc == x->edit_widget)
4429 return f;
4431 #endif /* USE_GTK */
4432 if (FRAME_X_WINDOW (f) == wdesc
4433 || f->output_data.x->icon_desc == wdesc)
4434 return f;
4435 #endif /* not USE_X_TOOLKIT */
4437 return 0;
4440 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
4442 /* Like x_window_to_frame but also compares the window with the widget's
4443 windows. */
4445 static struct frame *
4446 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4448 Lisp_Object tail, frame;
4449 struct frame *f, *found = NULL;
4450 struct x_output *x;
4452 if (wdesc == None)
4453 return NULL;
4455 FOR_EACH_FRAME (tail, frame)
4457 if (found)
4458 break;
4459 f = XFRAME (frame);
4460 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
4462 /* This frame matches if the window is any of its widgets. */
4463 x = f->output_data.x;
4464 if (x->hourglass_window == wdesc)
4465 found = f;
4466 else if (x->widget)
4468 #ifdef USE_GTK
4469 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4470 if (gwdesc != 0
4471 && gtk_widget_get_toplevel (gwdesc) == x->widget)
4472 found = f;
4473 #else
4474 if (wdesc == XtWindow (x->widget)
4475 || wdesc == XtWindow (x->column_widget)
4476 || wdesc == XtWindow (x->edit_widget))
4477 found = f;
4478 /* Match if the window is this frame's menubar. */
4479 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
4480 found = f;
4481 #endif
4483 else if (FRAME_X_WINDOW (f) == wdesc)
4484 /* A tooltip frame. */
4485 found = f;
4489 return found;
4492 /* Likewise, but consider only the menu bar widget. */
4494 static struct frame *
4495 x_menubar_window_to_frame (struct x_display_info *dpyinfo,
4496 const XEvent *event)
4498 Window wdesc = event->xany.window;
4499 Lisp_Object tail, frame;
4500 struct frame *f;
4501 struct x_output *x;
4503 if (wdesc == None)
4504 return NULL;
4506 FOR_EACH_FRAME (tail, frame)
4508 f = XFRAME (frame);
4509 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4510 continue;
4511 x = f->output_data.x;
4512 #ifdef USE_GTK
4513 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
4514 return f;
4515 #else
4516 /* Match if the window is this frame's menubar. */
4517 if (x->menubar_widget
4518 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
4519 return f;
4520 #endif
4522 return 0;
4525 /* Return the frame whose principal (outermost) window is WDESC.
4526 If WDESC is some other (smaller) window, we return 0. */
4528 struct frame *
4529 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
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;
4545 if (x->widget)
4547 /* This frame matches if the window is its topmost widget. */
4548 #ifdef USE_GTK
4549 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4550 if (gwdesc == x->widget)
4551 return f;
4552 #else
4553 if (wdesc == XtWindow (x->widget))
4554 return f;
4555 #endif
4557 else if (FRAME_X_WINDOW (f) == wdesc)
4558 /* Tooltip frame. */
4559 return f;
4561 return 0;
4564 #else /* !USE_X_TOOLKIT && !USE_GTK */
4566 #define x_any_window_to_frame(d, i) x_window_to_frame (d, i)
4567 #define x_top_window_to_frame(d, i) x_window_to_frame (d, i)
4569 #endif /* USE_X_TOOLKIT || USE_GTK */
4571 /* The focus may have changed. Figure out if it is a real focus change,
4572 by checking both FocusIn/Out and Enter/LeaveNotify events.
4574 Returns FOCUS_IN_EVENT event in *BUFP. */
4576 static void
4577 x_detect_focus_change (struct x_display_info *dpyinfo, struct frame *frame,
4578 const XEvent *event, struct input_event *bufp)
4580 if (!frame)
4581 return;
4583 switch (event->type)
4585 case EnterNotify:
4586 case LeaveNotify:
4588 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
4589 int focus_state
4590 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
4592 if (event->xcrossing.detail != NotifyInferior
4593 && event->xcrossing.focus
4594 && ! (focus_state & FOCUS_EXPLICIT))
4595 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
4596 FOCUS_IMPLICIT,
4597 dpyinfo, frame, bufp);
4599 break;
4601 case FocusIn:
4602 case FocusOut:
4603 x_focus_changed (event->type,
4604 (event->xfocus.detail == NotifyPointer ?
4605 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
4606 dpyinfo, frame, bufp);
4607 break;
4609 case ClientMessage:
4610 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
4612 enum xembed_message msg = event->xclient.data.l[1];
4613 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
4614 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
4616 break;
4621 #if !defined USE_X_TOOLKIT && !defined USE_GTK
4622 /* Handle an event saying the mouse has moved out of an Emacs frame. */
4624 void
4625 x_mouse_leave (struct x_display_info *dpyinfo)
4627 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
4629 #endif
4631 /* The focus has changed, or we have redirected a frame's focus to
4632 another frame (this happens when a frame uses a surrogate
4633 mini-buffer frame). Shift the highlight as appropriate.
4635 The FRAME argument doesn't necessarily have anything to do with which
4636 frame is being highlighted or un-highlighted; we only use it to find
4637 the appropriate X display info. */
4639 static void
4640 XTframe_rehighlight (struct frame *frame)
4642 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
4645 static void
4646 x_frame_rehighlight (struct x_display_info *dpyinfo)
4648 struct frame *old_highlight = dpyinfo->x_highlight_frame;
4650 if (dpyinfo->x_focus_frame)
4652 dpyinfo->x_highlight_frame
4653 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
4654 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
4655 : dpyinfo->x_focus_frame);
4656 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
4658 fset_focus_frame (dpyinfo->x_focus_frame, Qnil);
4659 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
4662 else
4663 dpyinfo->x_highlight_frame = 0;
4665 if (dpyinfo->x_highlight_frame != old_highlight)
4667 if (old_highlight)
4668 frame_unhighlight (old_highlight);
4669 if (dpyinfo->x_highlight_frame)
4670 frame_highlight (dpyinfo->x_highlight_frame);
4676 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
4678 /* Initialize mode_switch_bit and modifier_meaning. */
4679 static void
4680 x_find_modifier_meanings (struct x_display_info *dpyinfo)
4682 int min_code, max_code;
4683 KeySym *syms;
4684 int syms_per_code;
4685 XModifierKeymap *mods;
4687 dpyinfo->meta_mod_mask = 0;
4688 dpyinfo->shift_lock_mask = 0;
4689 dpyinfo->alt_mod_mask = 0;
4690 dpyinfo->super_mod_mask = 0;
4691 dpyinfo->hyper_mod_mask = 0;
4693 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
4695 syms = XGetKeyboardMapping (dpyinfo->display,
4696 min_code, max_code - min_code + 1,
4697 &syms_per_code);
4698 mods = XGetModifierMapping (dpyinfo->display);
4700 /* Scan the modifier table to see which modifier bits the Meta and
4701 Alt keysyms are on. */
4703 int row, col; /* The row and column in the modifier table. */
4704 bool found_alt_or_meta;
4706 for (row = 3; row < 8; row++)
4708 found_alt_or_meta = false;
4709 for (col = 0; col < mods->max_keypermod; col++)
4711 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
4713 /* Zeroes are used for filler. Skip them. */
4714 if (code == 0)
4715 continue;
4717 /* Are any of this keycode's keysyms a meta key? */
4719 int code_col;
4721 for (code_col = 0; code_col < syms_per_code; code_col++)
4723 int sym = syms[((code - min_code) * syms_per_code) + code_col];
4725 switch (sym)
4727 case XK_Meta_L:
4728 case XK_Meta_R:
4729 found_alt_or_meta = true;
4730 dpyinfo->meta_mod_mask |= (1 << row);
4731 break;
4733 case XK_Alt_L:
4734 case XK_Alt_R:
4735 found_alt_or_meta = true;
4736 dpyinfo->alt_mod_mask |= (1 << row);
4737 break;
4739 case XK_Hyper_L:
4740 case XK_Hyper_R:
4741 if (!found_alt_or_meta)
4742 dpyinfo->hyper_mod_mask |= (1 << row);
4743 code_col = syms_per_code;
4744 col = mods->max_keypermod;
4745 break;
4747 case XK_Super_L:
4748 case XK_Super_R:
4749 if (!found_alt_or_meta)
4750 dpyinfo->super_mod_mask |= (1 << row);
4751 code_col = syms_per_code;
4752 col = mods->max_keypermod;
4753 break;
4755 case XK_Shift_Lock:
4756 /* Ignore this if it's not on the lock modifier. */
4757 if (!found_alt_or_meta && ((1 << row) == LockMask))
4758 dpyinfo->shift_lock_mask = LockMask;
4759 code_col = syms_per_code;
4760 col = mods->max_keypermod;
4761 break;
4769 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
4770 if (! dpyinfo->meta_mod_mask)
4772 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
4773 dpyinfo->alt_mod_mask = 0;
4776 /* If some keys are both alt and meta,
4777 make them just meta, not alt. */
4778 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
4780 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
4783 XFree (syms);
4784 XFreeModifiermap (mods);
4787 /* Convert between the modifier bits X uses and the modifier bits
4788 Emacs uses. */
4791 x_x_to_emacs_modifiers (struct x_display_info *dpyinfo, int state)
4793 int mod_ctrl = ctrl_modifier;
4794 int mod_meta = meta_modifier;
4795 int mod_alt = alt_modifier;
4796 int mod_hyper = hyper_modifier;
4797 int mod_super = super_modifier;
4798 Lisp_Object tem;
4800 tem = Fget (Vx_ctrl_keysym, Qmodifier_value);
4801 if (INTEGERP (tem)) mod_ctrl = XINT (tem) & INT_MAX;
4802 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4803 if (INTEGERP (tem)) mod_alt = XINT (tem) & INT_MAX;
4804 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4805 if (INTEGERP (tem)) mod_meta = XINT (tem) & INT_MAX;
4806 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4807 if (INTEGERP (tem)) mod_hyper = XINT (tem) & INT_MAX;
4808 tem = Fget (Vx_super_keysym, Qmodifier_value);
4809 if (INTEGERP (tem)) mod_super = XINT (tem) & INT_MAX;
4811 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
4812 | ((state & ControlMask) ? mod_ctrl : 0)
4813 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
4814 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
4815 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
4816 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
4819 static int
4820 x_emacs_to_x_modifiers (struct x_display_info *dpyinfo, EMACS_INT state)
4822 EMACS_INT mod_ctrl = ctrl_modifier;
4823 EMACS_INT mod_meta = meta_modifier;
4824 EMACS_INT mod_alt = alt_modifier;
4825 EMACS_INT mod_hyper = hyper_modifier;
4826 EMACS_INT mod_super = super_modifier;
4828 Lisp_Object tem;
4830 tem = Fget (Vx_ctrl_keysym, Qmodifier_value);
4831 if (INTEGERP (tem)) mod_ctrl = XINT (tem);
4832 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4833 if (INTEGERP (tem)) mod_alt = XINT (tem);
4834 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4835 if (INTEGERP (tem)) mod_meta = XINT (tem);
4836 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4837 if (INTEGERP (tem)) mod_hyper = XINT (tem);
4838 tem = Fget (Vx_super_keysym, Qmodifier_value);
4839 if (INTEGERP (tem)) mod_super = XINT (tem);
4842 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
4843 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
4844 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
4845 | ((state & shift_modifier) ? ShiftMask : 0)
4846 | ((state & mod_ctrl) ? ControlMask : 0)
4847 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
4850 /* Convert a keysym to its name. */
4852 char *
4853 x_get_keysym_name (int keysym)
4855 char *value;
4857 block_input ();
4858 value = XKeysymToString (keysym);
4859 unblock_input ();
4861 return value;
4864 /* Mouse clicks and mouse movement. Rah.
4866 Formerly, we used PointerMotionHintMask (in standard_event_mask)
4867 so that we would have to call XQueryPointer after each MotionNotify
4868 event to ask for another such event. However, this made mouse tracking
4869 slow, and there was a bug that made it eventually stop.
4871 Simply asking for MotionNotify all the time seems to work better.
4873 In order to avoid asking for motion events and then throwing most
4874 of them away or busy-polling the server for mouse positions, we ask
4875 the server for pointer motion hints. This means that we get only
4876 one event per group of mouse movements. "Groups" are delimited by
4877 other kinds of events (focus changes and button clicks, for
4878 example), or by XQueryPointer calls; when one of these happens, we
4879 get another MotionNotify event the next time the mouse moves. This
4880 is at least as efficient as getting motion events when mouse
4881 tracking is on, and I suspect only negligibly worse when tracking
4882 is off. */
4884 /* Prepare a mouse-event in *RESULT for placement in the input queue.
4886 If the event is a button press, then note that we have grabbed
4887 the mouse. */
4889 static Lisp_Object
4890 construct_mouse_click (struct input_event *result,
4891 const XButtonEvent *event,
4892 struct frame *f)
4894 /* Make the event type NO_EVENT; we'll change that when we decide
4895 otherwise. */
4896 result->kind = MOUSE_CLICK_EVENT;
4897 result->code = event->button - Button1;
4898 result->timestamp = event->time;
4899 result->modifiers = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f),
4900 event->state)
4901 | (event->type == ButtonRelease
4902 ? up_modifier
4903 : down_modifier));
4905 XSETINT (result->x, event->x);
4906 XSETINT (result->y, event->y);
4907 XSETFRAME (result->frame_or_window, f);
4908 result->arg = Qnil;
4909 return Qnil;
4912 /* Function to report a mouse movement to the mainstream Emacs code.
4913 The input handler calls this.
4915 We have received a mouse movement event, which is given in *event.
4916 If the mouse is over a different glyph than it was last time, tell
4917 the mainstream emacs code by setting mouse_moved. If not, ask for
4918 another motion event, so we can check again the next time it moves. */
4920 static bool
4921 note_mouse_movement (struct frame *frame, const XMotionEvent *event)
4923 XRectangle *r;
4924 struct x_display_info *dpyinfo;
4926 if (!FRAME_X_OUTPUT (frame))
4927 return false;
4929 dpyinfo = FRAME_DISPLAY_INFO (frame);
4930 dpyinfo->last_mouse_movement_time = event->time;
4931 dpyinfo->last_mouse_motion_frame = frame;
4932 dpyinfo->last_mouse_motion_x = event->x;
4933 dpyinfo->last_mouse_motion_y = event->y;
4935 if (event->window != FRAME_X_WINDOW (frame))
4937 frame->mouse_moved = true;
4938 dpyinfo->last_mouse_scroll_bar = NULL;
4939 note_mouse_highlight (frame, -1, -1);
4940 dpyinfo->last_mouse_glyph_frame = NULL;
4941 return true;
4945 /* Has the mouse moved off the glyph it was on at the last sighting? */
4946 r = &dpyinfo->last_mouse_glyph;
4947 if (frame != dpyinfo->last_mouse_glyph_frame
4948 || event->x < r->x || event->x >= r->x + r->width
4949 || event->y < r->y || event->y >= r->y + r->height)
4951 frame->mouse_moved = true;
4952 dpyinfo->last_mouse_scroll_bar = NULL;
4953 note_mouse_highlight (frame, event->x, event->y);
4954 /* Remember which glyph we're now on. */
4955 remember_mouse_glyph (frame, event->x, event->y, r);
4956 dpyinfo->last_mouse_glyph_frame = frame;
4957 return true;
4960 return false;
4963 /* Return the current position of the mouse.
4964 *FP should be a frame which indicates which display to ask about.
4966 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
4967 and *PART to the frame, window, and scroll bar part that the mouse
4968 is over. Set *X and *Y to the portion and whole of the mouse's
4969 position on the scroll bar.
4971 If the mouse movement started elsewhere, set *FP to the frame the
4972 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
4973 the mouse is over.
4975 Set *TIMESTAMP to the server time-stamp for the time at which the mouse
4976 was at this position.
4978 Don't store anything if we don't have a valid set of values to report.
4980 This clears the mouse_moved flag, so we can wait for the next mouse
4981 movement. */
4983 static void
4984 XTmouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
4985 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
4986 Time *timestamp)
4988 struct frame *f1;
4989 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
4991 block_input ();
4993 if (dpyinfo->last_mouse_scroll_bar && insist == 0)
4995 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
4997 if (bar->horizontal)
4998 x_horizontal_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
4999 else
5000 x_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
5002 else
5004 Window root;
5005 int root_x, root_y;
5007 Window dummy_window;
5008 int dummy;
5010 Lisp_Object frame, tail;
5012 /* Clear the mouse-moved flag for every frame on this display. */
5013 FOR_EACH_FRAME (tail, frame)
5014 if (FRAME_X_P (XFRAME (frame))
5015 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
5016 XFRAME (frame)->mouse_moved = false;
5018 dpyinfo->last_mouse_scroll_bar = NULL;
5020 /* Figure out which root window we're on. */
5021 XQueryPointer (FRAME_X_DISPLAY (*fp),
5022 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
5024 /* The root window which contains the pointer. */
5025 &root,
5027 /* Trash which we can't trust if the pointer is on
5028 a different screen. */
5029 &dummy_window,
5031 /* The position on that root window. */
5032 &root_x, &root_y,
5034 /* More trash we can't trust. */
5035 &dummy, &dummy,
5037 /* Modifier keys and pointer buttons, about which
5038 we don't care. */
5039 (unsigned int *) &dummy);
5041 /* Now we have a position on the root; find the innermost window
5042 containing the pointer. */
5044 Window win, child;
5045 #ifdef USE_GTK
5046 Window first_win = 0;
5047 #endif
5048 int win_x, win_y;
5049 int parent_x = 0, parent_y = 0;
5051 win = root;
5053 /* XTranslateCoordinates can get errors if the window
5054 structure is changing at the same time this function
5055 is running. So at least we must not crash from them. */
5057 x_catch_errors (FRAME_X_DISPLAY (*fp));
5059 if (x_mouse_grabbed (dpyinfo))
5061 /* If mouse was grabbed on a frame, give coords for that frame
5062 even if the mouse is now outside it. */
5063 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
5065 /* From-window. */
5066 root,
5068 /* To-window. */
5069 FRAME_X_WINDOW (dpyinfo->last_mouse_frame),
5071 /* From-position, to-position. */
5072 root_x, root_y, &win_x, &win_y,
5074 /* Child of win. */
5075 &child);
5076 f1 = dpyinfo->last_mouse_frame;
5078 else
5080 while (true)
5082 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
5084 /* From-window, to-window. */
5085 root, win,
5087 /* From-position, to-position. */
5088 root_x, root_y, &win_x, &win_y,
5090 /* Child of win. */
5091 &child);
5093 if (child == None || child == win)
5095 #ifdef USE_GTK
5096 /* On GTK we have not inspected WIN yet. If it has
5097 a frame and that frame has a parent, use it. */
5098 struct frame *f = x_window_to_frame (dpyinfo, win);
5100 if (f && FRAME_PARENT_FRAME (f))
5101 first_win = win;
5102 #endif
5103 break;
5105 #ifdef USE_GTK
5106 /* We don't wan't to know the innermost window. We
5107 want the edit window. For non-Gtk+ the innermost
5108 window is the edit window. For Gtk+ it might not
5109 be. It might be the tool bar for example. */
5110 if (x_window_to_frame (dpyinfo, win))
5111 /* But don't hurry. We might find a child frame
5112 beneath. */
5113 first_win = win;
5114 #endif
5115 win = child;
5116 parent_x = win_x;
5117 parent_y = win_y;
5120 #ifdef USE_GTK
5121 if (first_win)
5122 win = first_win;
5123 #endif
5125 /* Now we know that:
5126 win is the innermost window containing the pointer
5127 (XTC says it has no child containing the pointer),
5128 win_x and win_y are the pointer's position in it
5129 (XTC did this the last time through), and
5130 parent_x and parent_y are the pointer's position in win's parent.
5131 (They are what win_x and win_y were when win was child.
5132 If win is the root window, it has no parent, and
5133 parent_{x,y} are invalid, but that's okay, because we'll
5134 never use them in that case.) */
5136 #ifdef USE_GTK
5137 /* We don't wan't to know the innermost window. We
5138 want the edit window. */
5139 f1 = x_window_to_frame (dpyinfo, win);
5140 #else
5141 /* Is win one of our frames? */
5142 f1 = x_any_window_to_frame (dpyinfo, win);
5143 #endif
5145 #ifdef USE_X_TOOLKIT
5146 /* If we end up with the menu bar window, say it's not
5147 on the frame. */
5148 if (f1 != NULL
5149 && f1->output_data.x->menubar_widget
5150 && win == XtWindow (f1->output_data.x->menubar_widget))
5151 f1 = NULL;
5152 #endif /* USE_X_TOOLKIT */
5155 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
5156 f1 = 0;
5158 x_uncatch_errors_after_check ();
5160 /* If not, is it one of our scroll bars? */
5161 if (! f1)
5163 struct scroll_bar *bar;
5165 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win, 2);
5167 if (bar)
5169 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5170 win_x = parent_x;
5171 win_y = parent_y;
5175 if (f1 == 0 && insist > 0)
5176 f1 = SELECTED_FRAME ();
5178 if (f1)
5180 /* Ok, we found a frame. Store all the values.
5181 last_mouse_glyph is a rectangle used to reduce the
5182 generation of mouse events. To not miss any motion
5183 events, we must divide the frame into rectangles of the
5184 size of the smallest character that could be displayed
5185 on it, i.e. into the same rectangles that matrices on
5186 the frame are divided into. */
5188 /* FIXME: what if F1 is not an X frame? */
5189 dpyinfo = FRAME_DISPLAY_INFO (f1);
5190 remember_mouse_glyph (f1, win_x, win_y, &dpyinfo->last_mouse_glyph);
5191 dpyinfo->last_mouse_glyph_frame = f1;
5193 *bar_window = Qnil;
5194 *part = 0;
5195 *fp = f1;
5196 XSETINT (*x, win_x);
5197 XSETINT (*y, win_y);
5198 *timestamp = dpyinfo->last_mouse_movement_time;
5203 unblock_input ();
5208 /***********************************************************************
5209 Scroll bars
5210 ***********************************************************************/
5212 /* Scroll bar support. */
5214 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
5215 manages it.
5216 This can be called in GC, so we have to make sure to strip off mark
5217 bits. */
5219 static struct scroll_bar *
5220 x_window_to_scroll_bar (Display *display, Window window_id, int type)
5222 Lisp_Object tail, frame;
5224 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
5225 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
5226 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
5228 FOR_EACH_FRAME (tail, frame)
5230 Lisp_Object bar, condemned;
5232 if (! FRAME_X_P (XFRAME (frame)))
5233 continue;
5235 /* Scan this frame's scroll bar list for a scroll bar with the
5236 right window ID. */
5237 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
5238 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
5239 /* This trick allows us to search both the ordinary and
5240 condemned scroll bar lists with one loop. */
5241 ! NILP (bar) || (bar = condemned,
5242 condemned = Qnil,
5243 ! NILP (bar));
5244 bar = XSCROLL_BAR (bar)->next)
5245 if (XSCROLL_BAR (bar)->x_window == window_id
5246 && FRAME_X_DISPLAY (XFRAME (frame)) == display
5247 && (type = 2
5248 || (type == 1 && XSCROLL_BAR (bar)->horizontal)
5249 || (type == 0 && !XSCROLL_BAR (bar)->horizontal)))
5250 return XSCROLL_BAR (bar);
5253 return NULL;
5257 #if defined USE_LUCID
5259 /* Return the Lucid menu bar WINDOW is part of. Return null
5260 if WINDOW is not part of a menu bar. */
5262 static Widget
5263 x_window_to_menu_bar (Window window)
5265 Lisp_Object tail, frame;
5267 FOR_EACH_FRAME (tail, frame)
5268 if (FRAME_X_P (XFRAME (frame)))
5270 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
5272 if (menu_bar && xlwmenu_window_p (menu_bar, window))
5273 return menu_bar;
5275 return NULL;
5278 #endif /* USE_LUCID */
5281 /************************************************************************
5282 Toolkit scroll bars
5283 ************************************************************************/
5285 #ifdef USE_TOOLKIT_SCROLL_BARS
5287 static void x_send_scroll_bar_event (Lisp_Object, enum scroll_bar_part,
5288 int, int, bool);
5290 /* Lisp window being scrolled. Set when starting to interact with
5291 a toolkit scroll bar, reset to nil when ending the interaction. */
5293 static Lisp_Object window_being_scrolled;
5295 /* Whether this is an Xaw with arrow-scrollbars. This should imply
5296 that movements of 1/20 of the screen size are mapped to up/down. */
5298 #ifndef USE_GTK
5299 /* Id of action hook installed for scroll bars. */
5301 static XtActionHookId action_hook_id;
5302 static XtActionHookId horizontal_action_hook_id;
5304 static Boolean xaw3d_arrow_scroll;
5306 /* Whether the drag scrolling maintains the mouse at the top of the
5307 thumb. If not, resizing the thumb needs to be done more carefully
5308 to avoid jerkiness. */
5310 static Boolean xaw3d_pick_top;
5312 /* Action hook installed via XtAppAddActionHook when toolkit scroll
5313 bars are used.. The hook is responsible for detecting when
5314 the user ends an interaction with the scroll bar, and generates
5315 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
5317 static void
5318 xt_action_hook (Widget widget, XtPointer client_data, String action_name,
5319 XEvent *event, String *params, Cardinal *num_params)
5321 bool scroll_bar_p;
5322 const char *end_action;
5324 #ifdef USE_MOTIF
5325 scroll_bar_p = XmIsScrollBar (widget);
5326 end_action = "Release";
5327 #else /* !USE_MOTIF i.e. use Xaw */
5328 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
5329 end_action = "EndScroll";
5330 #endif /* USE_MOTIF */
5332 if (scroll_bar_p
5333 && strcmp (action_name, end_action) == 0
5334 && WINDOWP (window_being_scrolled))
5336 struct window *w;
5337 struct scroll_bar *bar;
5339 x_send_scroll_bar_event (window_being_scrolled,
5340 scroll_bar_end_scroll, 0, 0, false);
5341 w = XWINDOW (window_being_scrolled);
5342 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5344 if (bar->dragging != -1)
5346 bar->dragging = -1;
5347 /* The thumb size is incorrect while dragging: fix it. */
5348 set_vertical_scroll_bar (w);
5350 window_being_scrolled = Qnil;
5351 #if defined (USE_LUCID)
5352 bar->last_seen_part = scroll_bar_nowhere;
5353 #endif
5354 /* Xt timeouts no longer needed. */
5355 toolkit_scroll_bar_interaction = false;
5360 static void
5361 xt_horizontal_action_hook (Widget widget, XtPointer client_data, String action_name,
5362 XEvent *event, String *params, Cardinal *num_params)
5364 bool scroll_bar_p;
5365 const char *end_action;
5367 #ifdef USE_MOTIF
5368 scroll_bar_p = XmIsScrollBar (widget);
5369 end_action = "Release";
5370 #else /* !USE_MOTIF i.e. use Xaw */
5371 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
5372 end_action = "EndScroll";
5373 #endif /* USE_MOTIF */
5375 if (scroll_bar_p
5376 && strcmp (action_name, end_action) == 0
5377 && WINDOWP (window_being_scrolled))
5379 struct window *w;
5380 struct scroll_bar *bar;
5382 x_send_scroll_bar_event (window_being_scrolled,
5383 scroll_bar_end_scroll, 0, 0, true);
5384 w = XWINDOW (window_being_scrolled);
5385 if (!NILP (w->horizontal_scroll_bar))
5387 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
5388 if (bar->dragging != -1)
5390 bar->dragging = -1;
5391 /* The thumb size is incorrect while dragging: fix it. */
5392 set_horizontal_scroll_bar (w);
5394 window_being_scrolled = Qnil;
5395 #if defined (USE_LUCID)
5396 bar->last_seen_part = scroll_bar_nowhere;
5397 #endif
5398 /* Xt timeouts no longer needed. */
5399 toolkit_scroll_bar_interaction = false;
5403 #endif /* not USE_GTK */
5405 /* Send a client message with message type Xatom_Scrollbar for a
5406 scroll action to the frame of WINDOW. PART is a value identifying
5407 the part of the scroll bar that was clicked on. PORTION is the
5408 amount to scroll of a whole of WHOLE. */
5410 static void
5411 x_send_scroll_bar_event (Lisp_Object window, enum scroll_bar_part part,
5412 int portion, int whole, bool horizontal)
5414 XEvent event;
5415 XClientMessageEvent *ev = &event.xclient;
5416 struct window *w = XWINDOW (window);
5417 struct frame *f = XFRAME (w->frame);
5418 intptr_t iw = (intptr_t) w;
5419 verify (INTPTR_WIDTH <= 64);
5420 int sign_shift = INTPTR_WIDTH - 32;
5422 block_input ();
5424 /* Construct a ClientMessage event to send to the frame. */
5425 ev->type = ClientMessage;
5426 ev->message_type = (horizontal
5427 ? FRAME_DISPLAY_INFO (f)->Xatom_Horizontal_Scrollbar
5428 : FRAME_DISPLAY_INFO (f)->Xatom_Scrollbar);
5429 ev->display = FRAME_X_DISPLAY (f);
5430 ev->window = FRAME_X_WINDOW (f);
5431 ev->format = 32;
5433 /* A 32-bit X client on a 64-bit X server can pass a window pointer
5434 as-is. A 64-bit client on a 32-bit X server is in trouble
5435 because a pointer does not fit and would be truncated while
5436 passing through the server. So use two slots and hope that X12
5437 will resolve such issues someday. */
5438 ev->data.l[0] = iw >> 31 >> 1;
5439 ev->data.l[1] = sign_shift <= 0 ? iw : iw << sign_shift >> sign_shift;
5440 ev->data.l[2] = part;
5441 ev->data.l[3] = portion;
5442 ev->data.l[4] = whole;
5444 /* Make Xt timeouts work while the scroll bar is active. */
5445 #ifdef USE_X_TOOLKIT
5446 toolkit_scroll_bar_interaction = true;
5447 x_activate_timeout_atimer ();
5448 #endif
5450 /* Setting the event mask to zero means that the message will
5451 be sent to the client that created the window, and if that
5452 window no longer exists, no event will be sent. */
5453 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
5454 unblock_input ();
5458 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
5459 in *IEVENT. */
5461 static void
5462 x_scroll_bar_to_input_event (const XEvent *event,
5463 struct input_event *ievent)
5465 const XClientMessageEvent *ev = &event->xclient;
5466 Lisp_Object window;
5467 struct window *w;
5469 /* See the comment in the function above. */
5470 intptr_t iw0 = ev->data.l[0];
5471 intptr_t iw1 = ev->data.l[1];
5472 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
5473 w = (struct window *) iw;
5475 XSETWINDOW (window, w);
5477 ievent->kind = SCROLL_BAR_CLICK_EVENT;
5478 ievent->frame_or_window = window;
5479 ievent->arg = Qnil;
5480 #ifdef USE_GTK
5481 ievent->timestamp = CurrentTime;
5482 #else
5483 ievent->timestamp =
5484 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
5485 #endif
5486 ievent->code = 0;
5487 ievent->part = ev->data.l[2];
5488 ievent->x = make_number (ev->data.l[3]);
5489 ievent->y = make_number (ev->data.l[4]);
5490 ievent->modifiers = 0;
5493 /* Transform a horizontal scroll bar ClientMessage EVENT to an Emacs
5494 input event in *IEVENT. */
5496 static void
5497 x_horizontal_scroll_bar_to_input_event (const XEvent *event,
5498 struct input_event *ievent)
5500 const XClientMessageEvent *ev = &event->xclient;
5501 Lisp_Object window;
5502 struct window *w;
5504 /* See the comment in the function above. */
5505 intptr_t iw0 = ev->data.l[0];
5506 intptr_t iw1 = ev->data.l[1];
5507 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
5508 w = (struct window *) iw;
5510 XSETWINDOW (window, w);
5512 ievent->kind = HORIZONTAL_SCROLL_BAR_CLICK_EVENT;
5513 ievent->frame_or_window = window;
5514 ievent->arg = Qnil;
5515 #ifdef USE_GTK
5516 ievent->timestamp = CurrentTime;
5517 #else
5518 ievent->timestamp =
5519 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
5520 #endif
5521 ievent->code = 0;
5522 ievent->part = ev->data.l[2];
5523 ievent->x = make_number (ev->data.l[3]);
5524 ievent->y = make_number (ev->data.l[4]);
5525 ievent->modifiers = 0;
5529 #ifdef USE_MOTIF
5531 /* Minimum and maximum values used for Motif scroll bars. */
5533 #define XM_SB_MAX 10000000
5535 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
5536 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
5537 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
5539 static void
5540 xm_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5542 struct scroll_bar *bar = client_data;
5543 XmScrollBarCallbackStruct *cs = call_data;
5544 enum scroll_bar_part part = scroll_bar_nowhere;
5545 bool horizontal = bar->horizontal;
5546 int whole = 0, portion = 0;
5548 switch (cs->reason)
5550 case XmCR_DECREMENT:
5551 bar->dragging = -1;
5552 part = horizontal ? scroll_bar_left_arrow : scroll_bar_up_arrow;
5553 break;
5555 case XmCR_INCREMENT:
5556 bar->dragging = -1;
5557 part = horizontal ? scroll_bar_right_arrow : scroll_bar_down_arrow;
5558 break;
5560 case XmCR_PAGE_DECREMENT:
5561 bar->dragging = -1;
5562 part = horizontal ? scroll_bar_before_handle : scroll_bar_above_handle;
5563 break;
5565 case XmCR_PAGE_INCREMENT:
5566 bar->dragging = -1;
5567 part = horizontal ? scroll_bar_after_handle : scroll_bar_below_handle;
5568 break;
5570 case XmCR_TO_TOP:
5571 bar->dragging = -1;
5572 part = horizontal ? scroll_bar_to_leftmost : scroll_bar_to_top;
5573 break;
5575 case XmCR_TO_BOTTOM:
5576 bar->dragging = -1;
5577 part = horizontal ? scroll_bar_to_rightmost : scroll_bar_to_bottom;
5578 break;
5580 case XmCR_DRAG:
5582 int slider_size;
5584 block_input ();
5585 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
5586 unblock_input ();
5588 if (horizontal)
5590 portion = bar->whole * ((float)cs->value / XM_SB_MAX);
5591 whole = bar->whole * ((float)(XM_SB_MAX - slider_size) / XM_SB_MAX);
5592 portion = min (portion, whole);
5593 part = scroll_bar_horizontal_handle;
5595 else
5597 whole = XM_SB_MAX - slider_size;
5598 portion = min (cs->value, whole);
5599 part = scroll_bar_handle;
5602 bar->dragging = cs->value;
5604 break;
5606 case XmCR_VALUE_CHANGED:
5607 break;
5610 if (part != scroll_bar_nowhere)
5612 window_being_scrolled = bar->window;
5613 x_send_scroll_bar_event (bar->window, part, portion, whole,
5614 bar->horizontal);
5618 #elif defined USE_GTK
5620 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
5621 bar widget. DATA is a pointer to the scroll_bar structure. */
5623 static gboolean
5624 xg_scroll_callback (GtkRange *range,
5625 GtkScrollType scroll,
5626 gdouble value,
5627 gpointer user_data)
5629 int whole = 0, portion = 0;
5630 struct scroll_bar *bar = user_data;
5631 enum scroll_bar_part part = scroll_bar_nowhere;
5632 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
5633 struct frame *f = g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
5635 if (xg_ignore_gtk_scrollbar) return false;
5637 switch (scroll)
5639 case GTK_SCROLL_JUMP:
5640 /* Buttons 1 2 or 3 must be grabbed. */
5641 if (FRAME_DISPLAY_INFO (f)->grabbed != 0
5642 && FRAME_DISPLAY_INFO (f)->grabbed < (1 << 4))
5644 if (bar->horizontal)
5646 part = scroll_bar_horizontal_handle;
5647 whole = (int)(gtk_adjustment_get_upper (adj) -
5648 gtk_adjustment_get_page_size (adj));
5649 portion = min ((int)value, whole);
5650 bar->dragging = portion;
5652 else
5654 part = scroll_bar_handle;
5655 whole = gtk_adjustment_get_upper (adj) -
5656 gtk_adjustment_get_page_size (adj);
5657 portion = min ((int)value, whole);
5658 bar->dragging = portion;
5661 break;
5662 case GTK_SCROLL_STEP_BACKWARD:
5663 part = (bar->horizontal
5664 ? scroll_bar_left_arrow : scroll_bar_up_arrow);
5665 bar->dragging = -1;
5666 break;
5667 case GTK_SCROLL_STEP_FORWARD:
5668 part = (bar->horizontal
5669 ? scroll_bar_right_arrow : scroll_bar_down_arrow);
5670 bar->dragging = -1;
5671 break;
5672 case GTK_SCROLL_PAGE_BACKWARD:
5673 part = (bar->horizontal
5674 ? scroll_bar_before_handle : scroll_bar_above_handle);
5675 bar->dragging = -1;
5676 break;
5677 case GTK_SCROLL_PAGE_FORWARD:
5678 part = (bar->horizontal
5679 ? scroll_bar_after_handle : scroll_bar_below_handle);
5680 bar->dragging = -1;
5681 break;
5682 default:
5683 break;
5686 if (part != scroll_bar_nowhere)
5688 window_being_scrolled = bar->window;
5689 x_send_scroll_bar_event (bar->window, part, portion, whole,
5690 bar->horizontal);
5693 return false;
5696 /* Callback for button release. Sets dragging to -1 when dragging is done. */
5698 static gboolean
5699 xg_end_scroll_callback (GtkWidget *widget,
5700 GdkEventButton *event,
5701 gpointer user_data)
5703 struct scroll_bar *bar = user_data;
5704 bar->dragging = -1;
5705 if (WINDOWP (window_being_scrolled))
5707 x_send_scroll_bar_event (window_being_scrolled,
5708 scroll_bar_end_scroll, 0, 0, bar->horizontal);
5709 window_being_scrolled = Qnil;
5712 return false;
5716 #else /* not USE_GTK and not USE_MOTIF */
5718 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
5719 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
5720 scroll bar struct. CALL_DATA is a pointer to a float saying where
5721 the thumb is. */
5723 static void
5724 xaw_jump_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5726 struct scroll_bar *bar = client_data;
5727 float *top_addr = call_data;
5728 float top = *top_addr;
5729 float shown;
5730 int whole, portion, height, width;
5731 enum scroll_bar_part part;
5732 bool horizontal = bar->horizontal;
5734 if (horizontal)
5736 /* Get the size of the thumb, a value between 0 and 1. */
5737 block_input ();
5738 XtVaGetValues (widget, XtNshown, &shown, XtNwidth, &width, NULL);
5739 unblock_input ();
5741 if (shown < 1)
5743 whole = bar->whole - (shown * bar->whole);
5744 portion = min (top * bar->whole, whole);
5746 else
5748 whole = bar->whole;
5749 portion = 0;
5752 part = scroll_bar_horizontal_handle;
5754 else
5756 /* Get the size of the thumb, a value between 0 and 1. */
5757 block_input ();
5758 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
5759 unblock_input ();
5761 whole = 10000000;
5762 portion = shown < 1 ? top * whole : 0;
5764 if (shown < 1 && (eabs (top + shown - 1) < 1.0f / height))
5765 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
5766 the bottom, so we force the scrolling whenever we see that we're
5767 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
5768 we try to ensure that we always stay two pixels away from the
5769 bottom). */
5770 part = scroll_bar_down_arrow;
5771 else
5772 part = scroll_bar_handle;
5775 window_being_scrolled = bar->window;
5776 bar->dragging = portion;
5777 bar->last_seen_part = part;
5778 x_send_scroll_bar_event (bar->window, part, portion, whole, bar->horizontal);
5782 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
5783 i.e. line or page up or down. WIDGET is the Xaw scroll bar
5784 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
5785 the scroll bar. CALL_DATA is an integer specifying the action that
5786 has taken place. Its magnitude is in the range 0..height of the
5787 scroll bar. Negative values mean scroll towards buffer start.
5788 Values < height of scroll bar mean line-wise movement. */
5790 static void
5791 xaw_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5793 struct scroll_bar *bar = client_data;
5794 /* The position really is stored cast to a pointer. */
5795 int position = (intptr_t) call_data;
5796 Dimension height, width;
5797 enum scroll_bar_part part;
5799 if (bar->horizontal)
5801 /* Get the width of the scroll bar. */
5802 block_input ();
5803 XtVaGetValues (widget, XtNwidth, &width, NULL);
5804 unblock_input ();
5806 if (eabs (position) >= width)
5807 part = (position < 0) ? scroll_bar_before_handle : scroll_bar_after_handle;
5809 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
5810 it maps line-movement to call_data = max(5, height/20). */
5811 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, width / 20))
5812 part = (position < 0) ? scroll_bar_left_arrow : scroll_bar_right_arrow;
5813 else
5814 part = scroll_bar_move_ratio;
5816 window_being_scrolled = bar->window;
5817 bar->dragging = -1;
5818 bar->last_seen_part = part;
5819 x_send_scroll_bar_event (bar->window, part, position, width,
5820 bar->horizontal);
5822 else
5825 /* Get the height of the scroll bar. */
5826 block_input ();
5827 XtVaGetValues (widget, XtNheight, &height, NULL);
5828 unblock_input ();
5830 if (eabs (position) >= height)
5831 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
5833 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
5834 it maps line-movement to call_data = max(5, height/20). */
5835 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
5836 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
5837 else
5838 part = scroll_bar_move_ratio;
5840 window_being_scrolled = bar->window;
5841 bar->dragging = -1;
5842 bar->last_seen_part = part;
5843 x_send_scroll_bar_event (bar->window, part, position, height,
5844 bar->horizontal);
5848 #endif /* not USE_GTK and not USE_MOTIF */
5850 #define SCROLL_BAR_NAME "verticalScrollBar"
5851 #define SCROLL_BAR_HORIZONTAL_NAME "horizontalScrollBar"
5853 /* Create the widget for scroll bar BAR on frame F. Record the widget
5854 and X window of the scroll bar in BAR. */
5856 #ifdef USE_GTK
5857 static void
5858 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5860 const char *scroll_bar_name = SCROLL_BAR_NAME;
5862 block_input ();
5863 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
5864 G_CALLBACK (xg_end_scroll_callback),
5865 scroll_bar_name);
5866 unblock_input ();
5869 static void
5870 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5872 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
5874 block_input ();
5875 xg_create_horizontal_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
5876 G_CALLBACK (xg_end_scroll_callback),
5877 scroll_bar_name);
5878 unblock_input ();
5881 #else /* not USE_GTK */
5883 static void
5884 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5886 Window xwindow;
5887 Widget widget;
5888 Arg av[20];
5889 int ac = 0;
5890 const char *scroll_bar_name = SCROLL_BAR_NAME;
5891 unsigned long pixel;
5893 block_input ();
5895 #ifdef USE_MOTIF
5896 /* Set resources. Create the widget. */
5897 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5898 XtSetArg (av[ac], XmNminimum, 0); ++ac;
5899 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
5900 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
5901 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
5902 XtSetArg (av[ac], XmNincrement, 1); ++ac;
5903 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
5905 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5906 if (pixel != -1)
5908 XtSetArg (av[ac], XmNforeground, pixel);
5909 ++ac;
5912 pixel = f->output_data.x->scroll_bar_background_pixel;
5913 if (pixel != -1)
5915 XtSetArg (av[ac], XmNbackground, pixel);
5916 ++ac;
5919 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
5920 (char *) scroll_bar_name, av, ac);
5922 /* Add one callback for everything that can happen. */
5923 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
5924 (XtPointer) bar);
5925 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
5926 (XtPointer) bar);
5927 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
5928 (XtPointer) bar);
5929 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
5930 (XtPointer) bar);
5931 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
5932 (XtPointer) bar);
5933 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
5934 (XtPointer) bar);
5935 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
5936 (XtPointer) bar);
5938 /* Realize the widget. Only after that is the X window created. */
5939 XtRealizeWidget (widget);
5941 /* Set the cursor to an arrow. I didn't find a resource to do that.
5942 And I'm wondering why it hasn't an arrow cursor by default. */
5943 XDefineCursor (XtDisplay (widget), XtWindow (widget),
5944 f->output_data.x->nontext_cursor);
5946 #else /* !USE_MOTIF i.e. use Xaw */
5948 /* Set resources. Create the widget. The background of the
5949 Xaw3d scroll bar widget is a little bit light for my taste.
5950 We don't alter it here to let users change it according
5951 to their taste with `emacs*verticalScrollBar.background: xxx'. */
5952 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5953 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
5954 /* For smoother scrolling with Xaw3d -sm */
5955 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
5957 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5958 if (pixel != -1)
5960 XtSetArg (av[ac], XtNforeground, pixel);
5961 ++ac;
5964 pixel = f->output_data.x->scroll_bar_background_pixel;
5965 if (pixel != -1)
5967 XtSetArg (av[ac], XtNbackground, pixel);
5968 ++ac;
5971 /* Top/bottom shadow colors. */
5973 /* Allocate them, if necessary. */
5974 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
5976 pixel = f->output_data.x->scroll_bar_background_pixel;
5977 if (pixel != -1)
5979 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5980 FRAME_X_COLORMAP (f),
5981 &pixel, 1.2, 0x8000))
5982 pixel = -1;
5983 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
5986 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5988 pixel = f->output_data.x->scroll_bar_background_pixel;
5989 if (pixel != -1)
5991 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5992 FRAME_X_COLORMAP (f),
5993 &pixel, 0.6, 0x4000))
5994 pixel = -1;
5995 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
5999 #ifdef XtNbeNiceToColormap
6000 /* Tell the toolkit about them. */
6001 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
6002 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
6003 /* We tried to allocate a color for the top/bottom shadow, and
6004 failed, so tell Xaw3d to use dithering instead. */
6005 /* But only if we have a small colormap. Xaw3d can allocate nice
6006 colors itself. */
6008 XtSetArg (av[ac], XtNbeNiceToColormap,
6009 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
6010 ++ac;
6012 else
6013 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
6014 be more consistent with other emacs 3d colors, and since Xaw3d is
6015 not good at dealing with allocation failure. */
6017 /* This tells Xaw3d to use real colors instead of dithering for
6018 the shadows. */
6019 XtSetArg (av[ac], XtNbeNiceToColormap, False);
6020 ++ac;
6022 /* Specify the colors. */
6023 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
6024 if (pixel != -1)
6026 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
6027 ++ac;
6029 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
6030 if (pixel != -1)
6032 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
6033 ++ac;
6036 #endif
6038 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
6039 f->output_data.x->edit_widget, av, ac);
6042 char const *initial = "";
6043 char const *val = initial;
6044 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
6045 #ifdef XtNarrowScrollbars
6046 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
6047 #endif
6048 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
6049 if (xaw3d_arrow_scroll || val == initial)
6050 { /* ARROW_SCROLL */
6051 xaw3d_arrow_scroll = True;
6052 /* Isn't that just a personal preference ? --Stef */
6053 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
6057 /* Define callbacks. */
6058 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
6059 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
6060 (XtPointer) bar);
6062 /* Realize the widget. Only after that is the X window created. */
6063 XtRealizeWidget (widget);
6065 #endif /* !USE_MOTIF */
6067 /* Install an action hook that lets us detect when the user
6068 finishes interacting with a scroll bar. */
6069 if (action_hook_id == 0)
6070 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
6072 /* Remember X window and widget in the scroll bar vector. */
6073 SET_SCROLL_BAR_X_WIDGET (bar, widget);
6074 xwindow = XtWindow (widget);
6075 bar->x_window = xwindow;
6076 bar->whole = 1;
6077 bar->horizontal = false;
6079 unblock_input ();
6082 static void
6083 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
6085 Window xwindow;
6086 Widget widget;
6087 Arg av[20];
6088 int ac = 0;
6089 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
6090 unsigned long pixel;
6092 block_input ();
6094 #ifdef USE_MOTIF
6095 /* Set resources. Create the widget. */
6096 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
6097 XtSetArg (av[ac], XmNminimum, 0); ++ac;
6098 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
6099 XtSetArg (av[ac], XmNorientation, XmHORIZONTAL); ++ac;
6100 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_RIGHT), ++ac;
6101 XtSetArg (av[ac], XmNincrement, 1); ++ac;
6102 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
6104 pixel = f->output_data.x->scroll_bar_foreground_pixel;
6105 if (pixel != -1)
6107 XtSetArg (av[ac], XmNforeground, pixel);
6108 ++ac;
6111 pixel = f->output_data.x->scroll_bar_background_pixel;
6112 if (pixel != -1)
6114 XtSetArg (av[ac], XmNbackground, pixel);
6115 ++ac;
6118 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
6119 (char *) scroll_bar_name, av, ac);
6121 /* Add one callback for everything that can happen. */
6122 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
6123 (XtPointer) bar);
6124 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
6125 (XtPointer) bar);
6126 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
6127 (XtPointer) bar);
6128 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
6129 (XtPointer) bar);
6130 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
6131 (XtPointer) bar);
6132 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
6133 (XtPointer) bar);
6134 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
6135 (XtPointer) bar);
6137 /* Realize the widget. Only after that is the X window created. */
6138 XtRealizeWidget (widget);
6140 /* Set the cursor to an arrow. I didn't find a resource to do that.
6141 And I'm wondering why it hasn't an arrow cursor by default. */
6142 XDefineCursor (XtDisplay (widget), XtWindow (widget),
6143 f->output_data.x->nontext_cursor);
6145 #else /* !USE_MOTIF i.e. use Xaw */
6147 /* Set resources. Create the widget. The background of the
6148 Xaw3d scroll bar widget is a little bit light for my taste.
6149 We don't alter it here to let users change it according
6150 to their taste with `emacs*verticalScrollBar.background: xxx'. */
6151 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
6152 XtSetArg (av[ac], XtNorientation, XtorientHorizontal); ++ac;
6153 /* For smoother scrolling with Xaw3d -sm */
6154 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
6156 pixel = f->output_data.x->scroll_bar_foreground_pixel;
6157 if (pixel != -1)
6159 XtSetArg (av[ac], XtNforeground, pixel);
6160 ++ac;
6163 pixel = f->output_data.x->scroll_bar_background_pixel;
6164 if (pixel != -1)
6166 XtSetArg (av[ac], XtNbackground, pixel);
6167 ++ac;
6170 /* Top/bottom shadow colors. */
6172 /* Allocate them, if necessary. */
6173 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
6175 pixel = f->output_data.x->scroll_bar_background_pixel;
6176 if (pixel != -1)
6178 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
6179 FRAME_X_COLORMAP (f),
6180 &pixel, 1.2, 0x8000))
6181 pixel = -1;
6182 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
6185 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
6187 pixel = f->output_data.x->scroll_bar_background_pixel;
6188 if (pixel != -1)
6190 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
6191 FRAME_X_COLORMAP (f),
6192 &pixel, 0.6, 0x4000))
6193 pixel = -1;
6194 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
6198 #ifdef XtNbeNiceToColormap
6199 /* Tell the toolkit about them. */
6200 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
6201 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
6202 /* We tried to allocate a color for the top/bottom shadow, and
6203 failed, so tell Xaw3d to use dithering instead. */
6204 /* But only if we have a small colormap. Xaw3d can allocate nice
6205 colors itself. */
6207 XtSetArg (av[ac], XtNbeNiceToColormap,
6208 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
6209 ++ac;
6211 else
6212 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
6213 be more consistent with other emacs 3d colors, and since Xaw3d is
6214 not good at dealing with allocation failure. */
6216 /* This tells Xaw3d to use real colors instead of dithering for
6217 the shadows. */
6218 XtSetArg (av[ac], XtNbeNiceToColormap, False);
6219 ++ac;
6221 /* Specify the colors. */
6222 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
6223 if (pixel != -1)
6225 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
6226 ++ac;
6228 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
6229 if (pixel != -1)
6231 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
6232 ++ac;
6235 #endif
6237 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
6238 f->output_data.x->edit_widget, av, ac);
6241 char const *initial = "";
6242 char const *val = initial;
6243 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
6244 #ifdef XtNarrowScrollbars
6245 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
6246 #endif
6247 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
6248 if (xaw3d_arrow_scroll || val == initial)
6249 { /* ARROW_SCROLL */
6250 xaw3d_arrow_scroll = True;
6251 /* Isn't that just a personal preference ? --Stef */
6252 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
6256 /* Define callbacks. */
6257 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
6258 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
6259 (XtPointer) bar);
6261 /* Realize the widget. Only after that is the X window created. */
6262 XtRealizeWidget (widget);
6264 #endif /* !USE_MOTIF */
6266 /* Install an action hook that lets us detect when the user
6267 finishes interacting with a scroll bar. */
6268 if (horizontal_action_hook_id == 0)
6269 horizontal_action_hook_id
6270 = XtAppAddActionHook (Xt_app_con, xt_horizontal_action_hook, 0);
6272 /* Remember X window and widget in the scroll bar vector. */
6273 SET_SCROLL_BAR_X_WIDGET (bar, widget);
6274 xwindow = XtWindow (widget);
6275 bar->x_window = xwindow;
6276 bar->whole = 1;
6277 bar->horizontal = true;
6279 unblock_input ();
6281 #endif /* not USE_GTK */
6284 /* Set the thumb size and position of scroll bar BAR. We are currently
6285 displaying PORTION out of a whole WHOLE, and our position POSITION. */
6287 #ifdef USE_GTK
6288 static void
6289 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
6291 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
6294 static void
6295 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
6297 xg_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
6300 #else /* not USE_GTK */
6301 static void
6302 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
6303 int whole)
6305 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6306 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6307 float top, shown;
6309 block_input ();
6311 #ifdef USE_MOTIF
6313 if (scroll_bar_adjust_thumb_portion_p)
6315 /* We use an estimate of 30 chars per line rather than the real
6316 `portion' value. This has the disadvantage that the thumb size
6317 is not very representative, but it makes our life a lot easier.
6318 Otherwise, we have to constantly adjust the thumb size, which
6319 we can't always do quickly enough: while dragging, the size of
6320 the thumb might prevent the user from dragging the thumb all the
6321 way to the end. but Motif and some versions of Xaw3d don't allow
6322 updating the thumb size while dragging. Also, even if we can update
6323 its size, the update will often happen too late.
6324 If you don't believe it, check out revision 1.650 of xterm.c to see
6325 what hoops we were going through and the still poor behavior we got. */
6326 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
6327 /* When the thumb is at the bottom, position == whole.
6328 So we need to increase `whole' to make space for the thumb. */
6329 whole += portion;
6332 if (whole <= 0)
6333 top = 0, shown = 1;
6334 else
6336 top = (float) position / whole;
6337 shown = (float) portion / whole;
6340 if (bar->dragging == -1)
6342 int size, value;
6344 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
6345 is the scroll bar's maximum and MIN is the scroll bar's minimum
6346 value. */
6347 size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
6349 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
6350 value = top * XM_SB_MAX;
6351 value = min (value, XM_SB_MAX - size);
6353 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6355 #else /* !USE_MOTIF i.e. use Xaw */
6357 if (whole == 0)
6358 top = 0, shown = 1;
6359 else
6361 top = (float) position / whole;
6362 shown = (float) portion / whole;
6366 float old_top, old_shown;
6367 Dimension height;
6368 XtVaGetValues (widget,
6369 XtNtopOfThumb, &old_top,
6370 XtNshown, &old_shown,
6371 XtNheight, &height,
6372 NULL);
6374 /* Massage the top+shown values. */
6375 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
6376 top = max (0, min (1, top));
6377 else
6378 top = old_top;
6379 #if ! defined (HAVE_XAW3D)
6380 /* With Xaw, 'top' values too closer to 1.0 may
6381 cause the thumb to disappear. Fix that. */
6382 top = min (top, 0.99f);
6383 #endif
6384 /* Keep two pixels available for moving the thumb down. */
6385 shown = max (0, min (1 - top - (2.0f / height), shown));
6386 #if ! defined (HAVE_XAW3D)
6387 /* Likewise with too small 'shown'. */
6388 shown = max (shown, 0.01f);
6389 #endif
6391 /* If the call to XawScrollbarSetThumb below doesn't seem to
6392 work, check that 'NARROWPROTO' is defined in src/config.h.
6393 If this is not so, most likely you need to fix configure. */
6394 if (top != old_top || shown != old_shown)
6396 if (bar->dragging == -1)
6397 XawScrollbarSetThumb (widget, top, shown);
6398 else
6400 /* Try to make the scrolling a tad smoother. */
6401 if (!xaw3d_pick_top)
6402 shown = min (shown, old_shown);
6404 XawScrollbarSetThumb (widget, top, shown);
6408 #endif /* !USE_MOTIF */
6410 unblock_input ();
6413 static void
6414 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
6415 int whole)
6417 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6418 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6419 float top, shown;
6421 block_input ();
6423 #ifdef USE_MOTIF
6424 bar->whole = whole;
6425 shown = (float) portion / whole;
6426 top = (float) position / (whole - portion);
6428 int size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
6429 int value = clip_to_bounds (0, top * (XM_SB_MAX - size), XM_SB_MAX - size);
6431 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6433 #else /* !USE_MOTIF i.e. use Xaw */
6434 bar->whole = whole;
6435 if (whole == 0)
6436 top = 0, shown = 1;
6437 else
6439 top = (float) position / whole;
6440 shown = (float) portion / whole;
6444 float old_top, old_shown;
6445 Dimension height;
6446 XtVaGetValues (widget,
6447 XtNtopOfThumb, &old_top,
6448 XtNshown, &old_shown,
6449 XtNheight, &height,
6450 NULL);
6452 #if false
6453 /* Massage the top+shown values. */
6454 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
6455 top = max (0, min (1, top));
6456 else
6457 top = old_top;
6458 #if ! defined (HAVE_XAW3D)
6459 /* With Xaw, 'top' values too closer to 1.0 may
6460 cause the thumb to disappear. Fix that. */
6461 top = min (top, 0.99f);
6462 #endif
6463 /* Keep two pixels available for moving the thumb down. */
6464 shown = max (0, min (1 - top - (2.0f / height), shown));
6465 #if ! defined (HAVE_XAW3D)
6466 /* Likewise with too small 'shown'. */
6467 shown = max (shown, 0.01f);
6468 #endif
6469 #endif
6471 /* If the call to XawScrollbarSetThumb below doesn't seem to
6472 work, check that 'NARROWPROTO' is defined in src/config.h.
6473 If this is not so, most likely you need to fix configure. */
6474 XawScrollbarSetThumb (widget, top, shown);
6475 #if false
6476 if (top != old_top || shown != old_shown)
6478 if (bar->dragging == -1)
6479 XawScrollbarSetThumb (widget, top, shown);
6480 else
6482 /* Try to make the scrolling a tad smoother. */
6483 if (!xaw3d_pick_top)
6484 shown = min (shown, old_shown);
6486 XawScrollbarSetThumb (widget, top, shown);
6489 #endif
6491 #endif /* !USE_MOTIF */
6493 unblock_input ();
6495 #endif /* not USE_GTK */
6497 #endif /* USE_TOOLKIT_SCROLL_BARS */
6501 /************************************************************************
6502 Scroll bars, general
6503 ************************************************************************/
6505 /* Create a scroll bar and return the scroll bar vector for it. W is
6506 the Emacs window on which to create the scroll bar. TOP, LEFT,
6507 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
6508 scroll bar. */
6510 static struct scroll_bar *
6511 x_scroll_bar_create (struct window *w, int top, int left,
6512 int width, int height, bool horizontal)
6514 struct frame *f = XFRAME (w->frame);
6515 struct scroll_bar *bar
6516 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
6517 Lisp_Object barobj;
6519 block_input ();
6521 #ifdef USE_TOOLKIT_SCROLL_BARS
6522 if (horizontal)
6523 x_create_horizontal_toolkit_scroll_bar (f, bar);
6524 else
6525 x_create_toolkit_scroll_bar (f, bar);
6526 #else /* not USE_TOOLKIT_SCROLL_BARS */
6528 XSetWindowAttributes a;
6529 unsigned long mask;
6530 Window window;
6532 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
6533 if (a.background_pixel == -1)
6534 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
6536 a.event_mask = (ButtonPressMask | ButtonReleaseMask
6537 | ButtonMotionMask | PointerMotionHintMask
6538 | ExposureMask);
6539 a.cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
6541 mask = (CWBackPixel | CWEventMask | CWCursor);
6543 /* Clear the area of W that will serve as a scroll bar. This is
6544 for the case that a window has been split horizontally. In
6545 this case, no clear_frame is generated to reduce flickering. */
6546 if (width > 0 && window_box_height (w) > 0)
6547 x_clear_area (f, left, top, width, window_box_height (w));
6549 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6550 /* Position and size of scroll bar. */
6551 left, top, width, height,
6552 /* Border width, depth, class, and visual. */
6554 CopyFromParent,
6555 CopyFromParent,
6556 CopyFromParent,
6557 /* Attributes. */
6558 mask, &a);
6559 bar->x_window = window;
6561 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6563 XSETWINDOW (bar->window, w);
6564 bar->top = top;
6565 bar->left = left;
6566 bar->width = width;
6567 bar->height = height;
6568 bar->start = 0;
6569 bar->end = 0;
6570 bar->dragging = -1;
6571 bar->horizontal = horizontal;
6572 #if defined (USE_TOOLKIT_SCROLL_BARS) && defined (USE_LUCID)
6573 bar->last_seen_part = scroll_bar_nowhere;
6574 #endif
6576 /* Add bar to its frame's list of scroll bars. */
6577 bar->next = FRAME_SCROLL_BARS (f);
6578 bar->prev = Qnil;
6579 XSETVECTOR (barobj, bar);
6580 fset_scroll_bars (f, barobj);
6581 if (!NILP (bar->next))
6582 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6584 /* Map the window/widget. */
6585 #ifdef USE_TOOLKIT_SCROLL_BARS
6587 #ifdef USE_GTK
6588 if (horizontal)
6589 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top,
6590 left, width, max (height, 1));
6591 else
6592 xg_update_scrollbar_pos (f, bar->x_window, top,
6593 left, width, max (height, 1));
6594 #else /* not USE_GTK */
6595 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6596 XtConfigureWidget (scroll_bar, left, top, width, max (height, 1), 0);
6597 XtMapWidget (scroll_bar);
6598 /* Don't obscure any child frames. */
6599 XLowerWindow (FRAME_X_DISPLAY (f), bar->x_window);
6600 #endif /* not USE_GTK */
6602 #else /* not USE_TOOLKIT_SCROLL_BARS */
6603 XMapWindow (FRAME_X_DISPLAY (f), bar->x_window);
6604 /* Don't obscure any child frames. */
6605 XLowerWindow (FRAME_X_DISPLAY (f), bar->x_window);
6606 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6608 unblock_input ();
6609 return bar;
6613 #ifndef USE_TOOLKIT_SCROLL_BARS
6615 /* Draw BAR's handle in the proper position.
6617 If the handle is already drawn from START to END, don't bother
6618 redrawing it, unless REBUILD; in that case, always
6619 redraw it. (REBUILD is handy for drawing the handle after expose
6620 events.)
6622 Normally, we want to constrain the start and end of the handle to
6623 fit inside its rectangle, but if the user is dragging the scroll
6624 bar handle, we want to let them drag it down all the way, so that
6625 the bar's top is as far down as it goes; otherwise, there's no way
6626 to move to the very end of the buffer. */
6628 static void
6629 x_scroll_bar_set_handle (struct scroll_bar *bar, int start, int end,
6630 bool rebuild)
6632 bool dragging = bar->dragging != -1;
6633 Window w = bar->x_window;
6634 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6635 GC gc = f->output_data.x->normal_gc;
6637 /* If the display is already accurate, do nothing. */
6638 if (! rebuild
6639 && start == bar->start
6640 && end == bar->end)
6641 return;
6643 block_input ();
6646 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
6647 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
6648 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
6650 /* Make sure the values are reasonable, and try to preserve
6651 the distance between start and end. */
6653 int length = end - start;
6655 if (start < 0)
6656 start = 0;
6657 else if (start > top_range)
6658 start = top_range;
6659 end = start + length;
6661 if (end < start)
6662 end = start;
6663 else if (end > top_range && ! dragging)
6664 end = top_range;
6667 /* Store the adjusted setting in the scroll bar. */
6668 bar->start = start;
6669 bar->end = end;
6671 /* Clip the end position, just for display. */
6672 if (end > top_range)
6673 end = top_range;
6675 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
6676 below top positions, to make sure the handle is always at least
6677 that many pixels tall. */
6678 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
6680 /* Draw the empty space above the handle. Note that we can't clear
6681 zero-height areas; that means "clear to end of window." */
6682 if ((inside_width > 0) && (start > 0))
6683 x_clear_area1 (FRAME_X_DISPLAY (f), w,
6684 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6685 VERTICAL_SCROLL_BAR_TOP_BORDER,
6686 inside_width, start, False);
6688 /* Change to proper foreground color if one is specified. */
6689 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6690 XSetForeground (FRAME_X_DISPLAY (f), gc,
6691 f->output_data.x->scroll_bar_foreground_pixel);
6693 /* Draw the handle itself. */
6694 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
6695 /* x, y, width, height */
6696 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6697 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
6698 inside_width, end - start);
6700 /* Restore the foreground color of the GC if we changed it above. */
6701 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6702 XSetForeground (FRAME_X_DISPLAY (f), gc,
6703 FRAME_FOREGROUND_PIXEL (f));
6705 /* Draw the empty space below the handle. Note that we can't
6706 clear zero-height areas; that means "clear to end of window." */
6707 if ((inside_width > 0) && (end < inside_height))
6708 x_clear_area1 (FRAME_X_DISPLAY (f), w,
6709 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6710 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
6711 inside_width, inside_height - end, False);
6714 unblock_input ();
6717 #endif /* !USE_TOOLKIT_SCROLL_BARS */
6719 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
6720 nil. */
6722 static void
6723 x_scroll_bar_remove (struct scroll_bar *bar)
6725 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6726 block_input ();
6728 #ifdef USE_TOOLKIT_SCROLL_BARS
6729 #ifdef USE_GTK
6730 xg_remove_scroll_bar (f, bar->x_window);
6731 #else /* not USE_GTK */
6732 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
6733 #endif /* not USE_GTK */
6734 #else
6735 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
6736 #endif
6738 /* Dissociate this scroll bar from its window. */
6739 if (bar->horizontal)
6740 wset_horizontal_scroll_bar (XWINDOW (bar->window), Qnil);
6741 else
6742 wset_vertical_scroll_bar (XWINDOW (bar->window), Qnil);
6744 unblock_input ();
6748 /* Set the handle of the vertical scroll bar for WINDOW to indicate
6749 that we are displaying PORTION characters out of a total of WHOLE
6750 characters, starting at POSITION. If WINDOW has no scroll bar,
6751 create one. */
6753 static void
6754 XTset_vertical_scroll_bar (struct window *w, int portion, int whole, int position)
6756 struct frame *f = XFRAME (w->frame);
6757 Lisp_Object barobj;
6758 struct scroll_bar *bar;
6759 int top, height, left, width;
6760 int window_y, window_height;
6762 /* Get window dimensions. */
6763 window_box (w, ANY_AREA, 0, &window_y, 0, &window_height);
6764 top = window_y;
6765 height = window_height;
6766 left = WINDOW_SCROLL_BAR_AREA_X (w);
6767 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
6769 /* Does the scroll bar exist yet? */
6770 if (NILP (w->vertical_scroll_bar))
6772 if (width > 0 && height > 0)
6774 block_input ();
6775 x_clear_area (f, left, top, width, height);
6776 unblock_input ();
6779 bar = x_scroll_bar_create (w, top, left, width, max (height, 1), false);
6781 else
6783 /* It may just need to be moved and resized. */
6784 unsigned int mask = 0;
6786 bar = XSCROLL_BAR (w->vertical_scroll_bar);
6788 block_input ();
6790 if (left != bar->left)
6791 mask |= CWX;
6792 if (top != bar->top)
6793 mask |= CWY;
6794 if (width != bar->width)
6795 mask |= CWWidth;
6796 if (height != bar->height)
6797 mask |= CWHeight;
6799 #ifdef USE_TOOLKIT_SCROLL_BARS
6801 /* Move/size the scroll bar widget. */
6802 if (mask)
6804 /* Since toolkit scroll bars are smaller than the space reserved
6805 for them on the frame, we have to clear "under" them. */
6806 if (width > 0 && height > 0)
6807 x_clear_area (f, left, top, width, height);
6808 #ifdef USE_GTK
6809 xg_update_scrollbar_pos (f, bar->x_window, top,
6810 left, width, max (height, 1));
6811 #else /* not USE_GTK */
6812 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
6813 left, top, width, max (height, 1), 0);
6814 #endif /* not USE_GTK */
6816 #else /* not USE_TOOLKIT_SCROLL_BARS */
6818 /* Move/size the scroll bar window. */
6819 if (mask)
6821 XWindowChanges wc;
6823 wc.x = left;
6824 wc.y = top;
6825 wc.width = width;
6826 wc.height = height;
6827 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
6828 mask, &wc);
6831 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6833 /* Remember new settings. */
6834 bar->left = left;
6835 bar->top = top;
6836 bar->width = width;
6837 bar->height = height;
6839 unblock_input ();
6842 #ifdef USE_TOOLKIT_SCROLL_BARS
6843 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
6844 #else /* not USE_TOOLKIT_SCROLL_BARS */
6845 /* Set the scroll bar's current state, unless we're currently being
6846 dragged. */
6847 if (bar->dragging == -1)
6849 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
6851 if (whole == 0)
6852 x_scroll_bar_set_handle (bar, 0, top_range, false);
6853 else
6855 int start = ((double) position * top_range) / whole;
6856 int end = ((double) (position + portion) * top_range) / whole;
6857 x_scroll_bar_set_handle (bar, start, end, false);
6860 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6862 XSETVECTOR (barobj, bar);
6863 wset_vertical_scroll_bar (w, barobj);
6867 static void
6868 XTset_horizontal_scroll_bar (struct window *w, int portion, int whole, int position)
6870 struct frame *f = XFRAME (w->frame);
6871 Lisp_Object barobj;
6872 struct scroll_bar *bar;
6873 int top, height, left, width;
6874 int window_x, window_width;
6875 int pixel_width = WINDOW_PIXEL_WIDTH (w);
6877 /* Get window dimensions. */
6878 window_box (w, ANY_AREA, &window_x, 0, &window_width, 0);
6879 left = window_x;
6880 width = window_width;
6881 top = WINDOW_SCROLL_BAR_AREA_Y (w);
6882 height = WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
6884 /* Does the scroll bar exist yet? */
6885 if (NILP (w->horizontal_scroll_bar))
6887 if (width > 0 && height > 0)
6889 block_input ();
6891 /* Clear also part between window_width and
6892 WINDOW_PIXEL_WIDTH. */
6893 x_clear_area (f, left, top, pixel_width, height);
6894 unblock_input ();
6897 bar = x_scroll_bar_create (w, top, left, width, height, true);
6899 else
6901 /* It may just need to be moved and resized. */
6902 unsigned int mask = 0;
6904 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
6906 block_input ();
6908 if (left != bar->left)
6909 mask |= CWX;
6910 if (top != bar->top)
6911 mask |= CWY;
6912 if (width != bar->width)
6913 mask |= CWWidth;
6914 if (height != bar->height)
6915 mask |= CWHeight;
6917 #ifdef USE_TOOLKIT_SCROLL_BARS
6918 /* Move/size the scroll bar widget. */
6919 if (mask)
6921 /* Since toolkit scroll bars are smaller than the space reserved
6922 for them on the frame, we have to clear "under" them. */
6923 if (width > 0 && height > 0)
6924 x_clear_area (f,
6925 WINDOW_LEFT_EDGE_X (w), top,
6926 pixel_width - WINDOW_RIGHT_DIVIDER_WIDTH (w), height);
6927 #ifdef USE_GTK
6928 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top, left,
6929 width, height);
6930 #else /* not USE_GTK */
6931 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
6932 left, top, width, height, 0);
6933 #endif /* not USE_GTK */
6935 #else /* not USE_TOOLKIT_SCROLL_BARS */
6937 /* Clear areas not covered by the scroll bar because it's not as
6938 wide as the area reserved for it. This makes sure a
6939 previous mode line display is cleared after C-x 2 C-x 1, for
6940 example. */
6942 int area_height = WINDOW_CONFIG_SCROLL_BAR_HEIGHT (w);
6943 int rest = area_height - height;
6944 if (rest > 0 && width > 0)
6945 x_clear_area (f, left, top, width, rest);
6948 /* Move/size the scroll bar window. */
6949 if (mask)
6951 XWindowChanges wc;
6953 wc.x = left;
6954 wc.y = top;
6955 wc.width = width;
6956 wc.height = height;
6957 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
6958 mask, &wc);
6961 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6963 /* Remember new settings. */
6964 bar->left = left;
6965 bar->top = top;
6966 bar->width = width;
6967 bar->height = height;
6969 unblock_input ();
6972 #ifdef USE_TOOLKIT_SCROLL_BARS
6973 x_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
6974 #else /* not USE_TOOLKIT_SCROLL_BARS */
6975 /* Set the scroll bar's current state, unless we're currently being
6976 dragged. */
6977 if (bar->dragging == -1)
6979 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, width);
6981 if (whole == 0)
6982 x_scroll_bar_set_handle (bar, 0, left_range, false);
6983 else
6985 int start = ((double) position * left_range) / whole;
6986 int end = ((double) (position + portion) * left_range) / whole;
6987 x_scroll_bar_set_handle (bar, start, end, false);
6990 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6992 XSETVECTOR (barobj, bar);
6993 wset_horizontal_scroll_bar (w, barobj);
6997 /* The following three hooks are used when we're doing a thorough
6998 redisplay of the frame. We don't explicitly know which scroll bars
6999 are going to be deleted, because keeping track of when windows go
7000 away is a real pain - "Can you say set-window-configuration, boys
7001 and girls?" Instead, we just assert at the beginning of redisplay
7002 that *all* scroll bars are to be removed, and then save a scroll bar
7003 from the fiery pit when we actually redisplay its window. */
7005 /* Arrange for all scroll bars on FRAME to be removed at the next call
7006 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
7007 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
7009 static void
7010 XTcondemn_scroll_bars (struct frame *frame)
7012 if (!NILP (FRAME_SCROLL_BARS (frame)))
7014 if (!NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
7016 /* Prepend scrollbars to already condemned ones. */
7017 Lisp_Object last = FRAME_SCROLL_BARS (frame);
7019 while (!NILP (XSCROLL_BAR (last)->next))
7020 last = XSCROLL_BAR (last)->next;
7022 XSCROLL_BAR (last)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
7023 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = last;
7026 fset_condemned_scroll_bars (frame, FRAME_SCROLL_BARS (frame));
7027 fset_scroll_bars (frame, Qnil);
7032 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
7033 Note that WINDOW isn't necessarily condemned at all. */
7035 static void
7036 XTredeem_scroll_bar (struct window *w)
7038 struct scroll_bar *bar;
7039 Lisp_Object barobj;
7040 struct frame *f;
7042 /* We can't redeem this window's scroll bar if it doesn't have one. */
7043 if (NILP (w->vertical_scroll_bar) && NILP (w->horizontal_scroll_bar))
7044 emacs_abort ();
7046 if (!NILP (w->vertical_scroll_bar) && WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
7048 bar = XSCROLL_BAR (w->vertical_scroll_bar);
7049 /* Unlink it from the condemned list. */
7050 f = XFRAME (WINDOW_FRAME (w));
7051 if (NILP (bar->prev))
7053 /* If the prev pointer is nil, it must be the first in one of
7054 the lists. */
7055 if (EQ (FRAME_SCROLL_BARS (f), w->vertical_scroll_bar))
7056 /* It's not condemned. Everything's fine. */
7057 goto horizontal;
7058 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
7059 w->vertical_scroll_bar))
7060 fset_condemned_scroll_bars (f, bar->next);
7061 else
7062 /* If its prev pointer is nil, it must be at the front of
7063 one or the other! */
7064 emacs_abort ();
7066 else
7067 XSCROLL_BAR (bar->prev)->next = bar->next;
7069 if (! NILP (bar->next))
7070 XSCROLL_BAR (bar->next)->prev = bar->prev;
7072 bar->next = FRAME_SCROLL_BARS (f);
7073 bar->prev = Qnil;
7074 XSETVECTOR (barobj, bar);
7075 fset_scroll_bars (f, barobj);
7076 if (! NILP (bar->next))
7077 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
7080 horizontal:
7081 if (!NILP (w->horizontal_scroll_bar) && WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
7083 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
7084 /* Unlink it from the condemned list. */
7085 f = XFRAME (WINDOW_FRAME (w));
7086 if (NILP (bar->prev))
7088 /* If the prev pointer is nil, it must be the first in one of
7089 the lists. */
7090 if (EQ (FRAME_SCROLL_BARS (f), w->horizontal_scroll_bar))
7091 /* It's not condemned. Everything's fine. */
7092 return;
7093 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
7094 w->horizontal_scroll_bar))
7095 fset_condemned_scroll_bars (f, bar->next);
7096 else
7097 /* If its prev pointer is nil, it must be at the front of
7098 one or the other! */
7099 emacs_abort ();
7101 else
7102 XSCROLL_BAR (bar->prev)->next = bar->next;
7104 if (! NILP (bar->next))
7105 XSCROLL_BAR (bar->next)->prev = bar->prev;
7107 bar->next = FRAME_SCROLL_BARS (f);
7108 bar->prev = Qnil;
7109 XSETVECTOR (barobj, bar);
7110 fset_scroll_bars (f, barobj);
7111 if (! NILP (bar->next))
7112 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
7116 /* Remove all scroll bars on FRAME that haven't been saved since the
7117 last call to `*condemn_scroll_bars_hook'. */
7119 static void
7120 XTjudge_scroll_bars (struct frame *f)
7122 Lisp_Object bar, next;
7124 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
7126 /* Clear out the condemned list now so we won't try to process any
7127 more events on the hapless scroll bars. */
7128 fset_condemned_scroll_bars (f, Qnil);
7130 for (; ! NILP (bar); bar = next)
7132 struct scroll_bar *b = XSCROLL_BAR (bar);
7134 x_scroll_bar_remove (b);
7136 next = b->next;
7137 b->next = b->prev = Qnil;
7140 /* Now there should be no references to the condemned scroll bars,
7141 and they should get garbage-collected. */
7145 #ifndef USE_TOOLKIT_SCROLL_BARS
7146 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
7147 is a no-op when using toolkit scroll bars.
7149 This may be called from a signal handler, so we have to ignore GC
7150 mark bits. */
7152 static void
7153 x_scroll_bar_expose (struct scroll_bar *bar, const XEvent *event)
7155 Window w = bar->x_window;
7156 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7157 GC gc = f->output_data.x->normal_gc;
7159 block_input ();
7161 x_scroll_bar_set_handle (bar, bar->start, bar->end, true);
7163 /* Switch to scroll bar foreground color. */
7164 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
7165 XSetForeground (FRAME_X_DISPLAY (f), gc,
7166 f->output_data.x->scroll_bar_foreground_pixel);
7168 /* Draw a one-pixel border just inside the edges of the scroll bar. */
7169 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
7170 /* x, y, width, height */
7171 0, 0, bar->width - 1, bar->height - 1);
7173 /* Restore the foreground color of the GC if we changed it above. */
7174 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
7175 XSetForeground (FRAME_X_DISPLAY (f), gc,
7176 FRAME_FOREGROUND_PIXEL (f));
7178 unblock_input ();
7181 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7183 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
7184 is set to something other than NO_EVENT, it is enqueued.
7186 This may be called from a signal handler, so we have to ignore GC
7187 mark bits. */
7190 static void
7191 x_scroll_bar_handle_click (struct scroll_bar *bar,
7192 const XEvent *event,
7193 struct input_event *emacs_event)
7195 if (! WINDOWP (bar->window))
7196 emacs_abort ();
7198 emacs_event->kind = (bar->horizontal
7199 ? HORIZONTAL_SCROLL_BAR_CLICK_EVENT
7200 : SCROLL_BAR_CLICK_EVENT);
7201 emacs_event->code = event->xbutton.button - Button1;
7202 emacs_event->modifiers
7203 = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO
7204 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
7205 event->xbutton.state)
7206 | (event->type == ButtonRelease
7207 ? up_modifier
7208 : down_modifier));
7209 emacs_event->frame_or_window = bar->window;
7210 emacs_event->arg = Qnil;
7211 emacs_event->timestamp = event->xbutton.time;
7212 if (bar->horizontal)
7214 int left_range
7215 = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
7216 int x = event->xbutton.x - HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
7218 if (x < 0) x = 0;
7219 if (x > left_range) x = left_range;
7221 if (x < bar->start)
7222 emacs_event->part = scroll_bar_before_handle;
7223 else if (x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
7224 emacs_event->part = scroll_bar_horizontal_handle;
7225 else
7226 emacs_event->part = scroll_bar_after_handle;
7228 #ifndef USE_TOOLKIT_SCROLL_BARS
7229 /* If the user has released the handle, set it to its final position. */
7230 if (event->type == ButtonRelease && bar->dragging != -1)
7232 int new_start = - bar->dragging;
7233 int new_end = new_start + bar->end - bar->start;
7235 x_scroll_bar_set_handle (bar, new_start, new_end, false);
7236 bar->dragging = -1;
7238 #endif
7240 XSETINT (emacs_event->x, left_range);
7241 XSETINT (emacs_event->y, x);
7243 else
7245 int top_range
7246 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
7247 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
7249 if (y < 0) y = 0;
7250 if (y > top_range) y = top_range;
7252 if (y < bar->start)
7253 emacs_event->part = scroll_bar_above_handle;
7254 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
7255 emacs_event->part = scroll_bar_handle;
7256 else
7257 emacs_event->part = scroll_bar_below_handle;
7259 #ifndef USE_TOOLKIT_SCROLL_BARS
7260 /* If the user has released the handle, set it to its final position. */
7261 if (event->type == ButtonRelease && bar->dragging != -1)
7263 int new_start = y - bar->dragging;
7264 int new_end = new_start + bar->end - bar->start;
7266 x_scroll_bar_set_handle (bar, new_start, new_end, false);
7267 bar->dragging = -1;
7269 #endif
7271 XSETINT (emacs_event->x, y);
7272 XSETINT (emacs_event->y, top_range);
7276 #ifndef USE_TOOLKIT_SCROLL_BARS
7278 /* Handle some mouse motion while someone is dragging the scroll bar.
7280 This may be called from a signal handler, so we have to ignore GC
7281 mark bits. */
7283 static void
7284 x_scroll_bar_note_movement (struct scroll_bar *bar,
7285 const XMotionEvent *event)
7287 struct frame *f = XFRAME (XWINDOW (bar->window)->frame);
7288 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
7290 dpyinfo->last_mouse_movement_time = event->time;
7291 dpyinfo->last_mouse_scroll_bar = bar;
7292 f->mouse_moved = true;
7294 /* If we're dragging the bar, display it. */
7295 if (bar->dragging != -1)
7297 /* Where should the handle be now? */
7298 int new_start = event->y - bar->dragging;
7300 if (new_start != bar->start)
7302 int new_end = new_start + bar->end - bar->start;
7304 x_scroll_bar_set_handle (bar, new_start, new_end, false);
7309 #endif /* !USE_TOOLKIT_SCROLL_BARS */
7311 /* Return information to the user about the current position of the mouse
7312 on the scroll bar. */
7314 static void
7315 x_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
7316 enum scroll_bar_part *part, Lisp_Object *x,
7317 Lisp_Object *y, Time *timestamp)
7319 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
7320 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
7321 Window w = bar->x_window;
7322 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7323 int win_x, win_y;
7324 Window dummy_window;
7325 int dummy_coord;
7326 unsigned int dummy_mask;
7328 block_input ();
7330 /* Get the mouse's position relative to the scroll bar window, and
7331 report that. */
7332 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
7334 /* Root, child, root x and root y. */
7335 &dummy_window, &dummy_window,
7336 &dummy_coord, &dummy_coord,
7338 /* Position relative to scroll bar. */
7339 &win_x, &win_y,
7341 /* Mouse buttons and modifier keys. */
7342 &dummy_mask))
7344 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
7346 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
7348 if (bar->dragging != -1)
7349 win_y -= bar->dragging;
7351 if (win_y < 0)
7352 win_y = 0;
7353 if (win_y > top_range)
7354 win_y = top_range;
7356 *fp = f;
7357 *bar_window = bar->window;
7359 if (bar->dragging != -1)
7360 *part = scroll_bar_handle;
7361 else if (win_y < bar->start)
7362 *part = scroll_bar_above_handle;
7363 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
7364 *part = scroll_bar_handle;
7365 else
7366 *part = scroll_bar_below_handle;
7368 XSETINT (*x, win_y);
7369 XSETINT (*y, top_range);
7371 f->mouse_moved = false;
7372 dpyinfo->last_mouse_scroll_bar = NULL;
7373 *timestamp = dpyinfo->last_mouse_movement_time;
7376 unblock_input ();
7380 /* Return information to the user about the current position of the mouse
7381 on the scroll bar. */
7383 static void
7384 x_horizontal_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
7385 enum scroll_bar_part *part, Lisp_Object *x,
7386 Lisp_Object *y, Time *timestamp)
7388 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
7389 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
7390 Window w = bar->x_window;
7391 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7392 int win_x, win_y;
7393 Window dummy_window;
7394 int dummy_coord;
7395 unsigned int dummy_mask;
7397 block_input ();
7399 /* Get the mouse's position relative to the scroll bar window, and
7400 report that. */
7401 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
7403 /* Root, child, root x and root y. */
7404 &dummy_window, &dummy_window,
7405 &dummy_coord, &dummy_coord,
7407 /* Position relative to scroll bar. */
7408 &win_x, &win_y,
7410 /* Mouse buttons and modifier keys. */
7411 &dummy_mask))
7413 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
7415 win_x -= HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
7417 if (bar->dragging != -1)
7418 win_x -= bar->dragging;
7420 if (win_x < 0)
7421 win_x = 0;
7422 if (win_x > left_range)
7423 win_x = left_range;
7425 *fp = f;
7426 *bar_window = bar->window;
7428 if (bar->dragging != -1)
7429 *part = scroll_bar_horizontal_handle;
7430 else if (win_x < bar->start)
7431 *part = scroll_bar_before_handle;
7432 else if (win_x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
7433 *part = scroll_bar_handle;
7434 else
7435 *part = scroll_bar_after_handle;
7437 XSETINT (*y, win_x);
7438 XSETINT (*x, left_range);
7440 f->mouse_moved = false;
7441 dpyinfo->last_mouse_scroll_bar = NULL;
7442 *timestamp = dpyinfo->last_mouse_movement_time;
7445 unblock_input ();
7449 /* The screen has been cleared so we may have changed foreground or
7450 background colors, and the scroll bars may need to be redrawn.
7451 Clear out the scroll bars, and ask for expose events, so we can
7452 redraw them. */
7454 static void
7455 x_scroll_bar_clear (struct frame *f)
7457 #ifndef USE_TOOLKIT_SCROLL_BARS
7458 Lisp_Object bar;
7460 /* We can have scroll bars even if this is 0,
7461 if we just turned off scroll bar mode.
7462 But in that case we should not clear them. */
7463 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
7464 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
7465 bar = XSCROLL_BAR (bar)->next)
7466 XClearArea (FRAME_X_DISPLAY (f),
7467 XSCROLL_BAR (bar)->x_window,
7468 0, 0, 0, 0, True);
7469 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7472 #ifdef ENABLE_CHECKING
7474 /* Record the last 100 characters stored
7475 to help debug the loss-of-chars-during-GC problem. */
7477 static int temp_index;
7478 static short temp_buffer[100];
7480 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
7481 if (temp_index == ARRAYELTS (temp_buffer)) \
7482 temp_index = 0; \
7483 temp_buffer[temp_index++] = (keysym)
7485 #else /* not ENABLE_CHECKING */
7487 #define STORE_KEYSYM_FOR_DEBUG(keysym) ((void)0)
7489 #endif /* ENABLE_CHECKING */
7491 /* Set this to nonzero to fake an "X I/O error"
7492 on a particular display. */
7494 static struct x_display_info *XTread_socket_fake_io_error;
7496 /* When we find no input here, we occasionally do a no-op command
7497 to verify that the X server is still running and we can still talk with it.
7498 We try all the open displays, one by one.
7499 This variable is used for cycling thru the displays. */
7501 static struct x_display_info *next_noop_dpyinfo;
7503 enum
7505 X_EVENT_NORMAL,
7506 X_EVENT_GOTO_OUT,
7507 X_EVENT_DROP
7510 /* Filter events for the current X input method.
7511 DPYINFO is the display this event is for.
7512 EVENT is the X event to filter.
7514 Returns non-zero if the event was filtered, caller shall not process
7515 this event further.
7516 Returns zero if event is wasn't filtered. */
7518 #ifdef HAVE_X_I18N
7519 static int
7520 x_filter_event (struct x_display_info *dpyinfo, XEvent *event)
7522 /* XFilterEvent returns non-zero if the input method has
7523 consumed the event. We pass the frame's X window to
7524 XFilterEvent because that's the one for which the IC
7525 was created. */
7527 struct frame *f1 = x_any_window_to_frame (dpyinfo,
7528 event->xclient.window);
7530 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
7532 #endif
7534 #ifdef USE_GTK
7535 static int current_count;
7536 static int current_finish;
7537 static struct input_event *current_hold_quit;
7539 /* This is the filter function invoked by the GTK event loop.
7540 It is invoked before the XEvent is translated to a GdkEvent,
7541 so we have a chance to act on the event before GTK. */
7542 static GdkFilterReturn
7543 event_handler_gdk (GdkXEvent *gxev, GdkEvent *ev, gpointer data)
7545 XEvent *xev = (XEvent *) gxev;
7547 block_input ();
7548 if (current_count >= 0)
7550 struct x_display_info *dpyinfo;
7552 dpyinfo = x_display_info_for_display (xev->xany.display);
7554 #ifdef HAVE_X_I18N
7555 /* Filter events for the current X input method.
7556 GTK calls XFilterEvent but not for key press and release,
7557 so we do it here. */
7558 if ((xev->type == KeyPress || xev->type == KeyRelease)
7559 && dpyinfo
7560 && x_filter_event (dpyinfo, xev))
7562 unblock_input ();
7563 return GDK_FILTER_REMOVE;
7565 #endif
7567 if (! dpyinfo)
7568 current_finish = X_EVENT_NORMAL;
7569 else
7570 current_count
7571 += handle_one_xevent (dpyinfo, xev, &current_finish,
7572 current_hold_quit);
7574 else
7575 current_finish = x_dispatch_event (xev, xev->xany.display);
7577 unblock_input ();
7579 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
7580 return GDK_FILTER_REMOVE;
7582 return GDK_FILTER_CONTINUE;
7584 #endif /* USE_GTK */
7587 static void xembed_send_message (struct frame *f, Time,
7588 enum xembed_message,
7589 long detail, long data1, long data2);
7591 static void
7592 x_net_wm_state (struct frame *f, Window window)
7594 int value = FULLSCREEN_NONE;
7595 Lisp_Object lval = Qnil;
7596 bool sticky = false;
7598 get_current_wm_state (f, window, &value, &sticky);
7600 switch (value)
7602 case FULLSCREEN_WIDTH:
7603 lval = Qfullwidth;
7604 break;
7605 case FULLSCREEN_HEIGHT:
7606 lval = Qfullheight;
7607 break;
7608 case FULLSCREEN_BOTH:
7609 lval = Qfullboth;
7610 break;
7611 case FULLSCREEN_MAXIMIZED:
7612 lval = Qmaximized;
7613 break;
7616 frame_size_history_add
7617 (f, Qx_net_wm_state, 0, 0,
7618 list2 (get_frame_param (f, Qfullscreen), lval));
7620 store_frame_param (f, Qfullscreen, lval);
7621 /** store_frame_param (f, Qsticky, sticky ? Qt : Qnil); **/
7624 /* Flip back buffers on any frames with undrawn content. */
7625 static void
7626 flush_dirty_back_buffers (void)
7628 block_input ();
7629 Lisp_Object tail, frame;
7630 FOR_EACH_FRAME (tail, frame)
7632 struct frame *f = XFRAME (frame);
7633 if (FRAME_LIVE_P (f) &&
7634 FRAME_X_P (f) &&
7635 FRAME_X_WINDOW (f) &&
7636 !FRAME_GARBAGED_P (f) &&
7637 !buffer_flipping_blocked_p () &&
7638 FRAME_X_NEED_BUFFER_FLIP (f))
7639 show_back_buffer (f);
7641 unblock_input ();
7644 /* Handles the XEvent EVENT on display DPYINFO.
7646 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
7647 *FINISH is zero if caller should continue reading events.
7648 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
7649 *EVENT is unchanged unless we're processing KeyPress event.
7651 We return the number of characters stored into the buffer. */
7653 static int
7654 handle_one_xevent (struct x_display_info *dpyinfo,
7655 const XEvent *event,
7656 int *finish, struct input_event *hold_quit)
7658 union buffered_input_event inev;
7659 int count = 0;
7660 int do_help = 0;
7661 ptrdiff_t nbytes = 0;
7662 struct frame *any, *f = NULL;
7663 struct coding_system coding;
7664 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
7665 /* This holds the state XLookupString needs to implement dead keys
7666 and other tricks known as "compose processing". _X Window System_
7667 says that a portable program can't use this, but Stephen Gildea assures
7668 me that letting the compiler initialize it to zeros will work okay. */
7669 static XComposeStatus compose_status;
7670 XEvent configureEvent;
7671 XEvent next_event;
7673 USE_SAFE_ALLOCA;
7675 *finish = X_EVENT_NORMAL;
7677 EVENT_INIT (inev.ie);
7678 inev.ie.kind = NO_EVENT;
7679 inev.ie.arg = Qnil;
7681 any = x_any_window_to_frame (dpyinfo, event->xany.window);
7683 if (any && any->wait_event_type == event->type)
7684 any->wait_event_type = 0; /* Indicates we got it. */
7686 switch (event->type)
7688 case ClientMessage:
7690 if (event->xclient.message_type == dpyinfo->Xatom_wm_protocols
7691 && event->xclient.format == 32)
7693 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_take_focus)
7695 /* Use the value returned by x_any_window_to_frame
7696 because this could be the shell widget window
7697 if the frame has no title bar. */
7698 f = any;
7699 #ifdef HAVE_X_I18N
7700 /* Not quite sure this is needed -pd */
7701 if (f && FRAME_XIC (f))
7702 XSetICFocus (FRAME_XIC (f));
7703 #endif
7704 #if false
7705 /* Emacs sets WM hints whose `input' field is `true'. This
7706 instructs the WM to set the input focus automatically for
7707 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
7708 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
7709 it has set the focus. So, XSetInputFocus below is not
7710 needed.
7712 The call to XSetInputFocus below has also caused trouble. In
7713 cases where the XSetInputFocus done by the WM and the one
7714 below are temporally close (on a fast machine), the call
7715 below can generate additional FocusIn events which confuse
7716 Emacs. */
7718 /* Since we set WM_TAKE_FOCUS, we must call
7719 XSetInputFocus explicitly. But not if f is null,
7720 since that might be an event for a deleted frame. */
7721 if (f)
7723 Display *d = event->xclient.display;
7724 /* Catch and ignore errors, in case window has been
7725 iconified by a window manager such as GWM. */
7726 x_catch_errors (d);
7727 XSetInputFocus (d, event->xclient.window,
7728 /* The ICCCM says this is
7729 the only valid choice. */
7730 RevertToParent,
7731 event->xclient.data.l[1]);
7732 x_uncatch_errors ();
7734 /* Not certain about handling scroll bars here */
7735 #endif
7736 goto done;
7739 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_save_yourself)
7741 /* Save state modify the WM_COMMAND property to
7742 something which can reinstate us. This notifies
7743 the session manager, who's looking for such a
7744 PropertyNotify. Can restart processing when
7745 a keyboard or mouse event arrives. */
7746 /* If we have a session manager, don't set this.
7747 KDE will then start two Emacsen, one for the
7748 session manager and one for this. */
7749 #ifdef HAVE_X_SM
7750 if (! x_session_have_connection ())
7751 #endif
7753 f = x_top_window_to_frame (dpyinfo,
7754 event->xclient.window);
7755 /* This is just so we only give real data once
7756 for a single Emacs process. */
7757 if (f == SELECTED_FRAME ())
7758 XSetCommand (FRAME_X_DISPLAY (f),
7759 event->xclient.window,
7760 initial_argv, initial_argc);
7761 else if (f)
7762 XSetCommand (FRAME_X_DISPLAY (f),
7763 event->xclient.window,
7764 0, 0);
7766 goto done;
7769 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_delete_window)
7771 f = any;
7772 if (!f)
7773 goto OTHER; /* May be a dialog that is to be removed */
7775 inev.ie.kind = DELETE_WINDOW_EVENT;
7776 XSETFRAME (inev.ie.frame_or_window, f);
7777 goto done;
7780 goto done;
7783 if (event->xclient.message_type == dpyinfo->Xatom_wm_configure_denied)
7784 goto done;
7786 if (event->xclient.message_type == dpyinfo->Xatom_wm_window_moved)
7788 int new_x, new_y;
7789 f = x_window_to_frame (dpyinfo, event->xclient.window);
7791 new_x = event->xclient.data.s[0];
7792 new_y = event->xclient.data.s[1];
7794 if (f)
7796 f->left_pos = new_x;
7797 f->top_pos = new_y;
7799 goto done;
7802 #ifdef X_TOOLKIT_EDITRES
7803 if (event->xclient.message_type == dpyinfo->Xatom_editres)
7805 f = any;
7806 if (f)
7807 _XEditResCheckMessages (f->output_data.x->widget,
7808 NULL, (XEvent *) event, NULL);
7809 goto done;
7811 #endif /* X_TOOLKIT_EDITRES */
7813 if (event->xclient.message_type == dpyinfo->Xatom_DONE
7814 || event->xclient.message_type == dpyinfo->Xatom_PAGE)
7816 /* Ghostview job completed. Kill it. We could
7817 reply with "Next" if we received "Page", but we
7818 currently never do because we are interested in
7819 images, only, which should have 1 page. */
7820 Pixmap pixmap = (Pixmap) event->xclient.data.l[1];
7821 f = x_window_to_frame (dpyinfo, event->xclient.window);
7822 if (!f)
7823 goto OTHER;
7824 x_kill_gs_process (pixmap, f);
7825 expose_frame (f, 0, 0, 0, 0);
7826 goto done;
7829 #ifdef USE_TOOLKIT_SCROLL_BARS
7830 /* Scroll bar callbacks send a ClientMessage from which
7831 we construct an input_event. */
7832 if (event->xclient.message_type == dpyinfo->Xatom_Scrollbar)
7834 x_scroll_bar_to_input_event (event, &inev.ie);
7835 *finish = X_EVENT_GOTO_OUT;
7836 goto done;
7838 else if (event->xclient.message_type == dpyinfo->Xatom_Horizontal_Scrollbar)
7840 x_horizontal_scroll_bar_to_input_event (event, &inev.ie);
7841 *finish = X_EVENT_GOTO_OUT;
7842 goto done;
7844 #endif /* USE_TOOLKIT_SCROLL_BARS */
7846 /* XEmbed messages from the embedder (if any). */
7847 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
7849 enum xembed_message msg = event->xclient.data.l[1];
7850 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
7851 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7853 *finish = X_EVENT_GOTO_OUT;
7854 goto done;
7857 xft_settings_event (dpyinfo, event);
7859 f = any;
7860 if (!f)
7861 goto OTHER;
7862 if (x_handle_dnd_message (f, &event->xclient, dpyinfo, &inev.ie))
7863 *finish = X_EVENT_DROP;
7865 break;
7867 case SelectionNotify:
7868 x_display_set_last_user_time (dpyinfo, event->xselection.time);
7869 #ifdef USE_X_TOOLKIT
7870 if (! x_window_to_frame (dpyinfo, event->xselection.requestor))
7871 goto OTHER;
7872 #endif /* not USE_X_TOOLKIT */
7873 x_handle_selection_notify (&event->xselection);
7874 break;
7876 case SelectionClear: /* Someone has grabbed ownership. */
7877 x_display_set_last_user_time (dpyinfo, event->xselectionclear.time);
7878 #ifdef USE_X_TOOLKIT
7879 if (! x_window_to_frame (dpyinfo, event->xselectionclear.window))
7880 goto OTHER;
7881 #endif /* USE_X_TOOLKIT */
7883 const XSelectionClearEvent *eventp = &event->xselectionclear;
7885 inev.sie.kind = SELECTION_CLEAR_EVENT;
7886 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
7887 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
7888 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
7890 break;
7892 case SelectionRequest: /* Someone wants our selection. */
7893 x_display_set_last_user_time (dpyinfo, event->xselectionrequest.time);
7894 #ifdef USE_X_TOOLKIT
7895 if (!x_window_to_frame (dpyinfo, event->xselectionrequest.owner))
7896 goto OTHER;
7897 #endif /* USE_X_TOOLKIT */
7899 const XSelectionRequestEvent *eventp = &event->xselectionrequest;
7901 inev.sie.kind = SELECTION_REQUEST_EVENT;
7902 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
7903 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
7904 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
7905 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
7906 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
7907 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
7909 break;
7911 case PropertyNotify:
7912 x_display_set_last_user_time (dpyinfo, event->xproperty.time);
7913 f = x_top_window_to_frame (dpyinfo, event->xproperty.window);
7914 if (f && event->xproperty.atom == dpyinfo->Xatom_net_wm_state)
7916 bool not_hidden = x_handle_net_wm_state (f, &event->xproperty);
7917 if (not_hidden && FRAME_ICONIFIED_P (f))
7919 /* Gnome shell does not iconify us when C-z is pressed.
7920 It hides the frame. So if our state says we aren't
7921 hidden anymore, treat it as deiconified. */
7922 SET_FRAME_VISIBLE (f, 1);
7923 SET_FRAME_ICONIFIED (f, false);
7924 f->output_data.x->has_been_visible = true;
7925 inev.ie.kind = DEICONIFY_EVENT;
7926 XSETFRAME (inev.ie.frame_or_window, f);
7928 else if (! not_hidden && ! FRAME_ICONIFIED_P (f))
7930 SET_FRAME_VISIBLE (f, 0);
7931 SET_FRAME_ICONIFIED (f, true);
7932 inev.ie.kind = ICONIFY_EVENT;
7933 XSETFRAME (inev.ie.frame_or_window, f);
7937 x_handle_property_notify (&event->xproperty);
7938 xft_settings_event (dpyinfo, event);
7939 goto OTHER;
7941 case ReparentNotify:
7942 f = x_top_window_to_frame (dpyinfo, event->xreparent.window);
7943 if (f)
7945 /* Maybe we shouldn't set this for child frames ?? */
7946 f->output_data.x->parent_desc = event->xreparent.parent;
7947 if (!FRAME_PARENT_FRAME (f))
7948 x_real_positions (f, &f->left_pos, &f->top_pos);
7949 else
7951 Window root;
7952 unsigned int dummy_uint;
7954 block_input ();
7955 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
7956 &root, &f->left_pos, &f->top_pos,
7957 &dummy_uint, &dummy_uint, &dummy_uint, &dummy_uint);
7958 unblock_input ();
7961 /* Perhaps reparented due to a WM restart. Reset this. */
7962 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
7963 FRAME_DISPLAY_INFO (f)->net_supported_window = 0;
7965 x_set_frame_alpha (f);
7967 goto OTHER;
7969 case Expose:
7970 f = x_window_to_frame (dpyinfo, event->xexpose.window);
7971 if (f)
7973 if (!FRAME_VISIBLE_P (f))
7975 block_input ();
7976 SET_FRAME_VISIBLE (f, 1);
7977 SET_FRAME_ICONIFIED (f, false);
7978 if (FRAME_X_DOUBLE_BUFFERED_P (f))
7979 font_drop_xrender_surfaces (f);
7980 f->output_data.x->has_been_visible = true;
7981 SET_FRAME_GARBAGED (f);
7982 unblock_input ();
7984 else if (FRAME_GARBAGED_P (f))
7986 #ifdef USE_GTK
7987 /* Go around the back buffer and manually clear the
7988 window the first time we show it. This way, we avoid
7989 showing users the sanity-defying horror of whatever
7990 GtkWindow is rendering beneath us. We've garbaged
7991 the frame, so we'll redraw the whole thing on next
7992 redisplay anyway. Yuck. */
7993 x_clear_area1 (
7994 FRAME_X_DISPLAY (f),
7995 FRAME_X_WINDOW (f),
7996 event->xexpose.x, event->xexpose.y,
7997 event->xexpose.width, event->xexpose.height,
7999 x_clear_under_internal_border (f);
8000 #endif
8004 if (!FRAME_GARBAGED_P (f))
8006 #ifdef USE_GTK
8007 /* This seems to be needed for GTK 2.6 and later, see
8008 https://debbugs.gnu.org/cgi/bugreport.cgi?bug=15398. */
8009 x_clear_area (f,
8010 event->xexpose.x, event->xexpose.y,
8011 event->xexpose.width, event->xexpose.height);
8012 #endif
8013 expose_frame (f, event->xexpose.x, event->xexpose.y,
8014 event->xexpose.width, event->xexpose.height);
8015 #ifdef USE_GTK
8016 x_clear_under_internal_border (f);
8017 #endif
8020 if (!FRAME_GARBAGED_P (f))
8021 show_back_buffer (f);
8023 else
8025 #ifndef USE_TOOLKIT_SCROLL_BARS
8026 struct scroll_bar *bar;
8027 #endif
8028 #if defined USE_LUCID
8029 /* Submenus of the Lucid menu bar aren't widgets
8030 themselves, so there's no way to dispatch events
8031 to them. Recognize this case separately. */
8033 Widget widget = x_window_to_menu_bar (event->xexpose.window);
8034 if (widget)
8035 xlwmenu_redisplay (widget);
8037 #endif /* USE_LUCID */
8039 #ifdef USE_TOOLKIT_SCROLL_BARS
8040 /* Dispatch event to the widget. */
8041 goto OTHER;
8042 #else /* not USE_TOOLKIT_SCROLL_BARS */
8043 bar = x_window_to_scroll_bar (event->xexpose.display,
8044 event->xexpose.window, 2);
8046 if (bar)
8047 x_scroll_bar_expose (bar, event);
8048 #ifdef USE_X_TOOLKIT
8049 else
8050 goto OTHER;
8051 #endif /* USE_X_TOOLKIT */
8052 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8054 break;
8056 case GraphicsExpose: /* This occurs when an XCopyArea's
8057 source area was obscured or not
8058 available. */
8059 f = x_window_to_frame (dpyinfo, event->xgraphicsexpose.drawable);
8060 if (f)
8062 expose_frame (f, event->xgraphicsexpose.x,
8063 event->xgraphicsexpose.y,
8064 event->xgraphicsexpose.width,
8065 event->xgraphicsexpose.height);
8066 #ifdef USE_GTK
8067 x_clear_under_internal_border (f);
8068 #endif
8069 show_back_buffer (f);
8071 #ifdef USE_X_TOOLKIT
8072 else
8073 goto OTHER;
8074 #endif /* USE_X_TOOLKIT */
8075 break;
8077 case NoExpose: /* This occurs when an XCopyArea's
8078 source area was completely
8079 available. */
8080 break;
8082 case UnmapNotify:
8083 /* Redo the mouse-highlight after the tooltip has gone. */
8084 if (event->xunmap.window == tip_window)
8086 tip_window = 0;
8087 x_redo_mouse_highlight (dpyinfo);
8090 f = x_top_window_to_frame (dpyinfo, event->xunmap.window);
8091 if (f) /* F may no longer exist if
8092 the frame was deleted. */
8094 bool visible = FRAME_VISIBLE_P (f);
8095 /* While a frame is unmapped, display generation is
8096 disabled; you don't want to spend time updating a
8097 display that won't ever be seen. */
8098 SET_FRAME_VISIBLE (f, 0);
8099 /* We can't distinguish, from the event, whether the window
8100 has become iconified or invisible. So assume, if it
8101 was previously visible, than now it is iconified.
8102 But x_make_frame_invisible clears both
8103 the visible flag and the iconified flag;
8104 and that way, we know the window is not iconified now. */
8105 if (visible || FRAME_ICONIFIED_P (f))
8107 SET_FRAME_ICONIFIED (f, true);
8108 inev.ie.kind = ICONIFY_EVENT;
8109 XSETFRAME (inev.ie.frame_or_window, f);
8112 goto OTHER;
8114 case MapNotify:
8115 /* We use x_top_window_to_frame because map events can
8116 come for sub-windows and they don't mean that the
8117 frame is visible. */
8118 f = x_top_window_to_frame (dpyinfo, event->xmap.window);
8119 if (f)
8121 bool iconified = FRAME_ICONIFIED_P (f);
8123 /* Check if fullscreen was specified before we where mapped the
8124 first time, i.e. from the command line. */
8125 if (!f->output_data.x->has_been_visible)
8128 x_check_fullscreen (f);
8129 #ifndef USE_GTK
8130 /* For systems that cannot synthesize `skip_taskbar' for
8131 unmapped windows do the following. */
8132 if (FRAME_SKIP_TASKBAR (f))
8133 x_set_skip_taskbar (f, Qt, Qnil);
8134 #endif /* Not USE_GTK */
8137 if (!iconified)
8139 /* The `z-group' is reset every time a frame becomes
8140 invisible. Handle this here. */
8141 if (FRAME_Z_GROUP (f) == z_group_above)
8142 x_set_z_group (f, Qabove, Qnil);
8143 else if (FRAME_Z_GROUP (f) == z_group_below)
8144 x_set_z_group (f, Qbelow, Qnil);
8147 SET_FRAME_VISIBLE (f, 1);
8148 SET_FRAME_ICONIFIED (f, false);
8149 f->output_data.x->has_been_visible = true;
8151 if (iconified)
8153 inev.ie.kind = DEICONIFY_EVENT;
8154 XSETFRAME (inev.ie.frame_or_window, f);
8156 else if (! NILP (Vframe_list) && ! NILP (XCDR (Vframe_list)))
8157 /* Force a redisplay sooner or later to update the
8158 frame titles in case this is the second frame. */
8159 record_asynch_buffer_change ();
8161 goto OTHER;
8163 case KeyPress:
8165 x_display_set_last_user_time (dpyinfo, event->xkey.time);
8166 ignore_next_mouse_click_timeout = 0;
8168 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8169 /* Dispatch KeyPress events when in menu. */
8170 if (popup_activated ())
8171 goto OTHER;
8172 #endif
8174 f = any;
8176 /* If mouse-highlight is an integer, input clears out
8177 mouse highlighting. */
8178 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
8179 #if ! defined (USE_GTK)
8180 && (f == 0
8181 || !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
8182 #endif
8185 clear_mouse_face (hlinfo);
8186 hlinfo->mouse_face_hidden = true;
8189 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
8190 if (f == 0)
8192 /* Scroll bars consume key events, but we want
8193 the keys to go to the scroll bar's frame. */
8194 Widget widget = XtWindowToWidget (dpyinfo->display,
8195 event->xkey.window);
8196 if (widget && XmIsScrollBar (widget))
8198 widget = XtParent (widget);
8199 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
8202 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
8204 if (f != 0)
8206 KeySym keysym, orig_keysym;
8207 /* al%imercury@uunet.uu.net says that making this 81
8208 instead of 80 fixed a bug whereby meta chars made
8209 his Emacs hang.
8211 It seems that some version of XmbLookupString has
8212 a bug of not returning XBufferOverflow in
8213 status_return even if the input is too long to
8214 fit in 81 bytes. So, we must prepare sufficient
8215 bytes for copy_buffer. 513 bytes (256 chars for
8216 two-byte character set) seems to be a fairly good
8217 approximation. -- 2000.8.10 handa@etl.go.jp */
8218 unsigned char copy_buffer[513];
8219 unsigned char *copy_bufptr = copy_buffer;
8220 int copy_bufsiz = sizeof (copy_buffer);
8221 int modifiers;
8222 Lisp_Object coding_system = Qlatin_1;
8223 Lisp_Object c;
8224 /* Event will be modified. */
8225 XKeyEvent xkey = event->xkey;
8227 #ifdef USE_GTK
8228 /* Don't pass keys to GTK. A Tab will shift focus to the
8229 tool bar in GTK 2.4. Keys will still go to menus and
8230 dialogs because in that case popup_activated is nonzero
8231 (see above). */
8232 *finish = X_EVENT_DROP;
8233 #endif
8235 xkey.state |= x_emacs_to_x_modifiers (FRAME_DISPLAY_INFO (f),
8236 extra_keyboard_modifiers);
8237 modifiers = xkey.state;
8239 /* This will have to go some day... */
8241 /* make_lispy_event turns chars into control chars.
8242 Don't do it here because XLookupString is too eager. */
8243 xkey.state &= ~ControlMask;
8244 xkey.state &= ~(dpyinfo->meta_mod_mask
8245 | dpyinfo->super_mod_mask
8246 | dpyinfo->hyper_mod_mask
8247 | dpyinfo->alt_mod_mask);
8249 /* In case Meta is ComposeCharacter,
8250 clear its status. According to Markus Ehrnsperger
8251 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
8252 this enables ComposeCharacter to work whether or
8253 not it is combined with Meta. */
8254 if (modifiers & dpyinfo->meta_mod_mask)
8255 memset (&compose_status, 0, sizeof (compose_status));
8257 #ifdef HAVE_X_I18N
8258 if (FRAME_XIC (f))
8260 Status status_return;
8262 coding_system = Vlocale_coding_system;
8263 nbytes = XmbLookupString (FRAME_XIC (f),
8264 &xkey, (char *) copy_bufptr,
8265 copy_bufsiz, &keysym,
8266 &status_return);
8267 if (status_return == XBufferOverflow)
8269 copy_bufsiz = nbytes + 1;
8270 copy_bufptr = alloca (copy_bufsiz);
8271 nbytes = XmbLookupString (FRAME_XIC (f),
8272 &xkey, (char *) copy_bufptr,
8273 copy_bufsiz, &keysym,
8274 &status_return);
8276 /* Xutf8LookupString is a new but already deprecated interface. -stef */
8277 if (status_return == XLookupNone)
8278 break;
8279 else if (status_return == XLookupChars)
8281 keysym = NoSymbol;
8282 modifiers = 0;
8284 else if (status_return != XLookupKeySym
8285 && status_return != XLookupBoth)
8286 emacs_abort ();
8288 else
8289 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
8290 copy_bufsiz, &keysym,
8291 &compose_status);
8292 #else
8293 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
8294 copy_bufsiz, &keysym,
8295 &compose_status);
8296 #endif
8298 /* If not using XIM/XIC, and a compose sequence is in progress,
8299 we break here. Otherwise, chars_matched is always 0. */
8300 if (compose_status.chars_matched > 0 && nbytes == 0)
8301 break;
8303 memset (&compose_status, 0, sizeof (compose_status));
8304 orig_keysym = keysym;
8306 /* Common for all keysym input events. */
8307 XSETFRAME (inev.ie.frame_or_window, f);
8308 inev.ie.modifiers
8309 = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), modifiers);
8310 inev.ie.timestamp = xkey.time;
8312 /* First deal with keysyms which have defined
8313 translations to characters. */
8314 if (keysym >= 32 && keysym < 128)
8315 /* Avoid explicitly decoding each ASCII character. */
8317 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
8318 inev.ie.code = keysym;
8319 goto done_keysym;
8322 /* Keysyms directly mapped to Unicode characters. */
8323 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
8325 if (keysym < 0x01000080)
8326 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
8327 else
8328 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
8329 inev.ie.code = keysym & 0xFFFFFF;
8330 goto done_keysym;
8333 /* Now non-ASCII. */
8334 if (HASH_TABLE_P (Vx_keysym_table)
8335 && (c = Fgethash (make_number (keysym),
8336 Vx_keysym_table,
8337 Qnil),
8338 NATNUMP (c)))
8340 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
8341 ? ASCII_KEYSTROKE_EVENT
8342 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
8343 inev.ie.code = XFASTINT (c);
8344 goto done_keysym;
8347 /* Random non-modifier sorts of keysyms. */
8348 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
8349 || keysym == XK_Delete
8350 #ifdef XK_ISO_Left_Tab
8351 || (keysym >= XK_ISO_Left_Tab
8352 && keysym <= XK_ISO_Enter)
8353 #endif
8354 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
8355 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
8356 #ifdef HPUX
8357 /* This recognizes the "extended function
8358 keys". It seems there's no cleaner way.
8359 Test IsModifierKey to avoid handling
8360 mode_switch incorrectly. */
8361 || (XK_Select <= keysym && keysym < XK_KP_Space)
8362 #endif
8363 #ifdef XK_dead_circumflex
8364 || orig_keysym == XK_dead_circumflex
8365 #endif
8366 #ifdef XK_dead_grave
8367 || orig_keysym == XK_dead_grave
8368 #endif
8369 #ifdef XK_dead_tilde
8370 || orig_keysym == XK_dead_tilde
8371 #endif
8372 #ifdef XK_dead_diaeresis
8373 || orig_keysym == XK_dead_diaeresis
8374 #endif
8375 #ifdef XK_dead_macron
8376 || orig_keysym == XK_dead_macron
8377 #endif
8378 #ifdef XK_dead_degree
8379 || orig_keysym == XK_dead_degree
8380 #endif
8381 #ifdef XK_dead_acute
8382 || orig_keysym == XK_dead_acute
8383 #endif
8384 #ifdef XK_dead_cedilla
8385 || orig_keysym == XK_dead_cedilla
8386 #endif
8387 #ifdef XK_dead_breve
8388 || orig_keysym == XK_dead_breve
8389 #endif
8390 #ifdef XK_dead_ogonek
8391 || orig_keysym == XK_dead_ogonek
8392 #endif
8393 #ifdef XK_dead_caron
8394 || orig_keysym == XK_dead_caron
8395 #endif
8396 #ifdef XK_dead_doubleacute
8397 || orig_keysym == XK_dead_doubleacute
8398 #endif
8399 #ifdef XK_dead_abovedot
8400 || orig_keysym == XK_dead_abovedot
8401 #endif
8402 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
8403 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
8404 /* Any "vendor-specific" key is ok. */
8405 || (orig_keysym & (1 << 28))
8406 || (keysym != NoSymbol && nbytes == 0))
8407 && ! (IsModifierKey (orig_keysym)
8408 /* The symbols from XK_ISO_Lock
8409 to XK_ISO_Last_Group_Lock
8410 don't have real modifiers but
8411 should be treated similarly to
8412 Mode_switch by Emacs. */
8413 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
8414 || (XK_ISO_Lock <= orig_keysym
8415 && orig_keysym <= XK_ISO_Last_Group_Lock)
8416 #endif
8419 STORE_KEYSYM_FOR_DEBUG (keysym);
8420 /* make_lispy_event will convert this to a symbolic
8421 key. */
8422 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
8423 inev.ie.code = keysym;
8424 goto done_keysym;
8427 { /* Raw bytes, not keysym. */
8428 ptrdiff_t i;
8429 int nchars, len;
8431 for (i = 0, nchars = 0; i < nbytes; i++)
8433 if (ASCII_CHAR_P (copy_bufptr[i]))
8434 nchars++;
8435 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
8438 if (nchars < nbytes)
8440 /* Decode the input data. */
8442 /* The input should be decoded with `coding_system'
8443 which depends on which X*LookupString function
8444 we used just above and the locale. */
8445 setup_coding_system (coding_system, &coding);
8446 coding.src_multibyte = false;
8447 coding.dst_multibyte = true;
8448 /* The input is converted to events, thus we can't
8449 handle composition. Anyway, there's no XIM that
8450 gives us composition information. */
8451 coding.common_flags &= ~CODING_ANNOTATION_MASK;
8453 SAFE_NALLOCA (coding.destination, MAX_MULTIBYTE_LENGTH,
8454 nbytes);
8455 coding.dst_bytes = MAX_MULTIBYTE_LENGTH * nbytes;
8456 coding.mode |= CODING_MODE_LAST_BLOCK;
8457 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
8458 nbytes = coding.produced;
8459 nchars = coding.produced_char;
8460 copy_bufptr = coding.destination;
8463 /* Convert the input data to a sequence of
8464 character events. */
8465 for (i = 0; i < nbytes; i += len)
8467 int ch;
8468 if (nchars == nbytes)
8469 ch = copy_bufptr[i], len = 1;
8470 else
8471 ch = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
8472 inev.ie.kind = (SINGLE_BYTE_CHAR_P (ch)
8473 ? ASCII_KEYSTROKE_EVENT
8474 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
8475 inev.ie.code = ch;
8476 kbd_buffer_store_buffered_event (&inev, hold_quit);
8479 count += nchars;
8481 inev.ie.kind = NO_EVENT; /* Already stored above. */
8483 if (keysym == NoSymbol)
8484 break;
8486 /* FIXME: check side effects and remove this. */
8487 ((XEvent *) event)->xkey = xkey;
8489 done_keysym:
8490 #ifdef HAVE_X_I18N
8491 /* Don't dispatch this event since XtDispatchEvent calls
8492 XFilterEvent, and two calls in a row may freeze the
8493 client. */
8494 break;
8495 #else
8496 goto OTHER;
8497 #endif
8499 case KeyRelease:
8500 x_display_set_last_user_time (dpyinfo, event->xkey.time);
8501 #ifdef HAVE_X_I18N
8502 /* Don't dispatch this event since XtDispatchEvent calls
8503 XFilterEvent, and two calls in a row may freeze the
8504 client. */
8505 break;
8506 #else
8507 goto OTHER;
8508 #endif
8510 case EnterNotify:
8511 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
8512 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8514 f = any;
8516 if (f && x_mouse_click_focus_ignore_position)
8517 ignore_next_mouse_click_timeout = event->xmotion.time + 200;
8519 /* EnterNotify counts as mouse movement,
8520 so update things that depend on mouse position. */
8521 if (f && !f->output_data.x->hourglass_p)
8522 note_mouse_movement (f, &event->xmotion);
8523 #ifdef USE_GTK
8524 /* We may get an EnterNotify on the buttons in the toolbar. In that
8525 case we moved out of any highlighted area and need to note this. */
8526 if (!f && dpyinfo->last_mouse_glyph_frame)
8527 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
8528 #endif
8529 goto OTHER;
8531 case FocusIn:
8532 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8533 goto OTHER;
8535 case LeaveNotify:
8536 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
8537 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8539 f = x_top_window_to_frame (dpyinfo, event->xcrossing.window);
8540 if (f)
8542 if (f == hlinfo->mouse_face_mouse_frame)
8544 /* If we move outside the frame, then we're
8545 certainly no longer on any text in the frame. */
8546 clear_mouse_face (hlinfo);
8547 hlinfo->mouse_face_mouse_frame = 0;
8550 /* Generate a nil HELP_EVENT to cancel a help-echo.
8551 Do it only if there's something to cancel.
8552 Otherwise, the startup message is cleared when
8553 the mouse leaves the frame. */
8554 if (any_help_event_p)
8555 do_help = -1;
8557 #ifdef USE_GTK
8558 /* See comment in EnterNotify above */
8559 else if (dpyinfo->last_mouse_glyph_frame)
8560 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
8561 #endif
8562 goto OTHER;
8564 case FocusOut:
8565 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8566 goto OTHER;
8568 case MotionNotify:
8570 x_display_set_last_user_time (dpyinfo, event->xmotion.time);
8571 previous_help_echo_string = help_echo_string;
8572 help_echo_string = Qnil;
8574 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
8575 : x_window_to_frame (dpyinfo, event->xmotion.window));
8577 if (hlinfo->mouse_face_hidden)
8579 hlinfo->mouse_face_hidden = false;
8580 clear_mouse_face (hlinfo);
8583 #ifdef USE_GTK
8584 if (f && xg_event_is_for_scrollbar (f, event))
8585 f = 0;
8586 #endif
8587 if (f)
8589 /* Maybe generate a SELECT_WINDOW_EVENT for
8590 `mouse-autoselect-window' but don't let popup menus
8591 interfere with this (Bug#1261). */
8592 if (!NILP (Vmouse_autoselect_window)
8593 && !popup_activated ()
8594 /* Don't switch if we're currently in the minibuffer.
8595 This tries to work around problems where the
8596 minibuffer gets unselected unexpectedly, and where
8597 you then have to move your mouse all the way down to
8598 the minibuffer to select it. */
8599 && !MINI_WINDOW_P (XWINDOW (selected_window))
8600 /* With `focus-follows-mouse' non-nil create an event
8601 also when the target window is on another frame. */
8602 && (f == XFRAME (selected_frame)
8603 || !NILP (focus_follows_mouse)))
8605 static Lisp_Object last_mouse_window;
8606 Lisp_Object window = window_from_coordinates
8607 (f, event->xmotion.x, event->xmotion.y, 0, false);
8609 /* A window will be autoselected only when it is not
8610 selected now and the last mouse movement event was
8611 not in it. The remainder of the code is a bit vague
8612 wrt what a "window" is. For immediate autoselection,
8613 the window is usually the entire window but for GTK
8614 where the scroll bars don't count. For delayed
8615 autoselection the window is usually the window's text
8616 area including the margins. */
8617 if (WINDOWP (window)
8618 && !EQ (window, last_mouse_window)
8619 && !EQ (window, selected_window))
8621 inev.ie.kind = SELECT_WINDOW_EVENT;
8622 inev.ie.frame_or_window = window;
8625 /* Remember the last window where we saw the mouse. */
8626 last_mouse_window = window;
8629 if (!note_mouse_movement (f, &event->xmotion))
8630 help_echo_string = previous_help_echo_string;
8632 else
8634 #ifndef USE_TOOLKIT_SCROLL_BARS
8635 struct scroll_bar *bar
8636 = x_window_to_scroll_bar (event->xmotion.display,
8637 event->xmotion.window, 2);
8639 if (bar)
8640 x_scroll_bar_note_movement (bar, &event->xmotion);
8641 #endif /* USE_TOOLKIT_SCROLL_BARS */
8643 /* If we move outside the frame, then we're
8644 certainly no longer on any text in the frame. */
8645 clear_mouse_face (hlinfo);
8648 /* If the contents of the global variable help_echo_string
8649 has changed, generate a HELP_EVENT. */
8650 if (!NILP (help_echo_string)
8651 || !NILP (previous_help_echo_string))
8652 do_help = 1;
8653 goto OTHER;
8656 case ConfigureNotify:
8657 /* An opaque move can generate a stream of events as the window
8658 is dragged around. If the connection round trip time isn't
8659 really short, they may come faster than we can respond to
8660 them, given the multiple queries we can do to check window
8661 manager state, translate coordinates, etc.
8663 So if this ConfigureNotify is immediately followed by another
8664 for the same window, use the info from the latest update, and
8665 consider the events all handled. */
8666 /* Opaque resize may be trickier; ConfigureNotify events are
8667 mixed with Expose events for multiple windows. */
8668 configureEvent = *event;
8669 while (XPending (dpyinfo->display))
8671 XNextEvent (dpyinfo->display, &next_event);
8672 if (next_event.type != ConfigureNotify
8673 || next_event.xconfigure.window != event->xconfigure.window
8674 /* Skipping events with different sizes can lead to a
8675 mispositioned mode line at initial window creation.
8676 Only drop window motion events for now. */
8677 || next_event.xconfigure.width != event->xconfigure.width
8678 || next_event.xconfigure.height != event->xconfigure.height)
8680 XPutBackEvent (dpyinfo->display, &next_event);
8681 break;
8683 else
8684 configureEvent = next_event;
8687 f = x_top_window_to_frame (dpyinfo, configureEvent.xconfigure.window);
8688 /* Unfortunately, we need to call font_drop_xrender_surfaces for
8689 _all_ ConfigureNotify events, otherwise we miss some and
8690 flicker. Don't try to optimize these calls by looking only
8691 for size changes: that's not sufficient. We miss some
8692 surface invalidations and flicker. */
8693 block_input ();
8694 if (f && FRAME_X_DOUBLE_BUFFERED_P (f))
8695 font_drop_xrender_surfaces (f);
8696 unblock_input ();
8697 #ifdef USE_CAIRO
8698 if (f) x_cr_destroy_surface (f);
8699 #endif
8700 #ifdef USE_GTK
8701 if (!f
8702 && (f = any)
8703 && configureEvent.xconfigure.window == FRAME_X_WINDOW (f))
8705 block_input ();
8706 if (FRAME_X_DOUBLE_BUFFERED_P (f))
8707 font_drop_xrender_surfaces (f);
8708 unblock_input ();
8709 xg_frame_resized (f, configureEvent.xconfigure.width,
8710 configureEvent.xconfigure.height);
8711 #ifdef USE_CAIRO
8712 x_cr_destroy_surface (f);
8713 #endif
8714 f = 0;
8716 #endif
8717 if (f)
8719 #ifdef USE_GTK
8720 /* For GTK+ don't call x_net_wm_state for the scroll bar
8721 window. (Bug#24963, Bug#25887) */
8722 if (configureEvent.xconfigure.window == FRAME_X_WINDOW (f))
8723 #endif
8724 x_net_wm_state (f, configureEvent.xconfigure.window);
8726 #ifdef USE_X_TOOLKIT
8727 /* Tip frames are pure X window, set size for them. */
8728 if (! NILP (tip_frame) && XFRAME (tip_frame) == f)
8730 if (FRAME_PIXEL_HEIGHT (f) != configureEvent.xconfigure.height
8731 || FRAME_PIXEL_WIDTH (f) != configureEvent.xconfigure.width)
8733 SET_FRAME_GARBAGED (f);
8735 FRAME_PIXEL_HEIGHT (f) = configureEvent.xconfigure.height;
8736 FRAME_PIXEL_WIDTH (f) = configureEvent.xconfigure.width;
8738 #endif
8740 #ifndef USE_X_TOOLKIT
8741 #ifndef USE_GTK
8742 int width =
8743 FRAME_PIXEL_TO_TEXT_WIDTH (f, configureEvent.xconfigure.width);
8744 int height =
8745 FRAME_PIXEL_TO_TEXT_HEIGHT (f, configureEvent.xconfigure.height);
8747 /* In the toolkit version, change_frame_size
8748 is called by the code that handles resizing
8749 of the EmacsFrame widget. */
8751 /* Even if the number of character rows and columns has
8752 not changed, the font size may have changed, so we need
8753 to check the pixel dimensions as well. */
8754 if (width != FRAME_TEXT_WIDTH (f)
8755 || height != FRAME_TEXT_HEIGHT (f)
8756 || configureEvent.xconfigure.width != FRAME_PIXEL_WIDTH (f)
8757 || configureEvent.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
8759 change_frame_size (f, width, height, false, true, false, true);
8760 x_clear_under_internal_border (f);
8761 SET_FRAME_GARBAGED (f);
8762 cancel_mouse_face (f);
8764 #endif /* not USE_GTK */
8765 #endif
8767 #ifdef USE_GTK
8768 /* GTK creates windows but doesn't map them.
8769 Only get real positions when mapped. */
8770 if (FRAME_GTK_OUTER_WIDGET (f)
8771 && gtk_widget_get_mapped (FRAME_GTK_OUTER_WIDGET (f)))
8772 #endif
8774 int old_left = f->left_pos;
8775 int old_top = f->top_pos;
8776 Lisp_Object frame = Qnil;
8778 XSETFRAME (frame, f);
8780 if (!FRAME_PARENT_FRAME (f))
8781 x_real_positions (f, &f->left_pos, &f->top_pos);
8782 else
8784 Window root;
8785 unsigned int dummy_uint;
8787 block_input ();
8788 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8789 &root, &f->left_pos, &f->top_pos,
8790 &dummy_uint, &dummy_uint, &dummy_uint, &dummy_uint);
8791 unblock_input ();
8794 if (old_left != f->left_pos || old_top != f->top_pos)
8796 inev.ie.kind = MOVE_FRAME_EVENT;
8797 XSETFRAME (inev.ie.frame_or_window, f);
8802 #ifdef HAVE_X_I18N
8803 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
8804 xic_set_statusarea (f);
8805 #endif
8808 goto OTHER;
8810 case ButtonRelease:
8811 case ButtonPress:
8813 /* If we decide we want to generate an event to be seen
8814 by the rest of Emacs, we put it here. */
8815 bool tool_bar_p = false;
8817 memset (&compose_status, 0, sizeof (compose_status));
8818 dpyinfo->last_mouse_glyph_frame = NULL;
8819 x_display_set_last_user_time (dpyinfo, event->xbutton.time);
8821 if (x_mouse_grabbed (dpyinfo))
8822 f = dpyinfo->last_mouse_frame;
8823 else
8825 f = x_window_to_frame (dpyinfo, event->xbutton.window);
8827 if (f && event->xbutton.type == ButtonPress
8828 && !popup_activated ()
8829 && !x_window_to_scroll_bar (event->xbutton.display,
8830 event->xbutton.window, 2)
8831 && !FRAME_NO_ACCEPT_FOCUS (f))
8833 /* When clicking into a child frame or when clicking
8834 into a parent frame with the child frame selected and
8835 `no-accept-focus' is not set, select the clicked
8836 frame. */
8837 struct frame *hf = dpyinfo->x_highlight_frame;
8839 if (FRAME_PARENT_FRAME (f) || (hf && frame_ancestor_p (f, hf)))
8841 block_input ();
8842 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8843 RevertToParent, CurrentTime);
8844 if (FRAME_PARENT_FRAME (f))
8845 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8846 unblock_input ();
8851 #ifdef USE_GTK
8852 if (f && xg_event_is_for_scrollbar (f, event))
8853 f = 0;
8854 #endif
8855 if (f)
8857 #if ! defined (USE_GTK)
8858 /* Is this in the tool-bar? */
8859 if (WINDOWP (f->tool_bar_window)
8860 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
8862 Lisp_Object window;
8863 int x = event->xbutton.x;
8864 int y = event->xbutton.y;
8866 window = window_from_coordinates (f, x, y, 0, true);
8867 tool_bar_p = EQ (window, f->tool_bar_window);
8869 if (tool_bar_p && event->xbutton.button < 4)
8870 handle_tool_bar_click
8871 (f, x, y, event->xbutton.type == ButtonPress,
8872 x_x_to_emacs_modifiers (dpyinfo, event->xbutton.state));
8874 #endif /* !USE_GTK */
8876 if (!tool_bar_p)
8877 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8878 if (! popup_activated ())
8879 #endif
8881 if (ignore_next_mouse_click_timeout)
8883 if (event->type == ButtonPress
8884 && event->xbutton.time > ignore_next_mouse_click_timeout)
8886 ignore_next_mouse_click_timeout = 0;
8887 construct_mouse_click (&inev.ie, &event->xbutton, f);
8889 if (event->type == ButtonRelease)
8890 ignore_next_mouse_click_timeout = 0;
8892 else
8893 construct_mouse_click (&inev.ie, &event->xbutton, f);
8895 if (FRAME_X_EMBEDDED_P (f))
8896 xembed_send_message (f, event->xbutton.time,
8897 XEMBED_REQUEST_FOCUS, 0, 0, 0);
8899 else
8901 struct scroll_bar *bar
8902 = x_window_to_scroll_bar (event->xbutton.display,
8903 event->xbutton.window, 2);
8905 #ifdef USE_TOOLKIT_SCROLL_BARS
8906 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
8907 scroll bars. */
8908 if (bar && event->xbutton.state & ControlMask)
8910 x_scroll_bar_handle_click (bar, event, &inev.ie);
8911 *finish = X_EVENT_DROP;
8913 #else /* not USE_TOOLKIT_SCROLL_BARS */
8914 if (bar)
8915 x_scroll_bar_handle_click (bar, event, &inev.ie);
8916 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8919 if (event->type == ButtonPress)
8921 dpyinfo->grabbed |= (1 << event->xbutton.button);
8922 dpyinfo->last_mouse_frame = f;
8923 #if ! defined (USE_GTK)
8924 if (f && !tool_bar_p)
8925 f->last_tool_bar_item = -1;
8926 #endif /* not USE_GTK */
8928 else
8929 dpyinfo->grabbed &= ~(1 << event->xbutton.button);
8931 /* Ignore any mouse motion that happened before this event;
8932 any subsequent mouse-movement Emacs events should reflect
8933 only motion after the ButtonPress/Release. */
8934 if (f != 0)
8935 f->mouse_moved = false;
8937 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8938 f = x_menubar_window_to_frame (dpyinfo, event);
8939 /* For a down-event in the menu bar,
8940 don't pass it to Xt right now.
8941 Instead, save it away
8942 and we will pass it to Xt from kbd_buffer_get_event.
8943 That way, we can run some Lisp code first. */
8944 if (! popup_activated ()
8945 #ifdef USE_GTK
8946 /* Gtk+ menus only react to the first three buttons. */
8947 && event->xbutton.button < 3
8948 #endif
8949 && f && event->type == ButtonPress
8950 /* Verify the event is really within the menu bar
8951 and not just sent to it due to grabbing. */
8952 && event->xbutton.x >= 0
8953 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
8954 && event->xbutton.y >= 0
8955 && event->xbutton.y < FRAME_MENUBAR_HEIGHT (f)
8956 && event->xbutton.same_screen)
8958 if (!f->output_data.x->saved_menu_event)
8959 f->output_data.x->saved_menu_event = xmalloc (sizeof *event);
8960 *f->output_data.x->saved_menu_event = *event;
8961 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT;
8962 XSETFRAME (inev.ie.frame_or_window, f);
8963 *finish = X_EVENT_DROP;
8965 else
8966 goto OTHER;
8967 #endif /* USE_X_TOOLKIT || USE_GTK */
8969 break;
8971 case CirculateNotify:
8972 goto OTHER;
8974 case CirculateRequest:
8975 goto OTHER;
8977 case VisibilityNotify:
8978 goto OTHER;
8980 case MappingNotify:
8981 /* Someone has changed the keyboard mapping - update the
8982 local cache. */
8983 switch (event->xmapping.request)
8985 case MappingModifier:
8986 x_find_modifier_meanings (dpyinfo);
8987 FALLTHROUGH;
8988 case MappingKeyboard:
8989 XRefreshKeyboardMapping ((XMappingEvent *) &event->xmapping);
8991 goto OTHER;
8993 case DestroyNotify:
8994 xft_settings_event (dpyinfo, event);
8995 break;
8997 default:
8998 OTHER:
8999 #ifdef USE_X_TOOLKIT
9000 block_input ();
9001 if (*finish != X_EVENT_DROP)
9002 XtDispatchEvent ((XEvent *) event);
9003 unblock_input ();
9004 #endif /* USE_X_TOOLKIT */
9005 break;
9008 done:
9009 if (inev.ie.kind != NO_EVENT)
9011 kbd_buffer_store_buffered_event (&inev, hold_quit);
9012 count++;
9015 if (do_help
9016 && !(hold_quit && hold_quit->kind != NO_EVENT))
9018 Lisp_Object frame;
9020 if (f)
9021 XSETFRAME (frame, f);
9022 else
9023 frame = Qnil;
9025 if (do_help > 0)
9027 any_help_event_p = true;
9028 gen_help_event (help_echo_string, frame, help_echo_window,
9029 help_echo_object, help_echo_pos);
9031 else
9033 help_echo_string = Qnil;
9034 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
9036 count++;
9039 /* Sometimes event processing draws to the frame outside redisplay.
9040 To ensure that these changes become visible, draw them here. */
9041 flush_dirty_back_buffers ();
9042 SAFE_FREE ();
9043 return count;
9046 /* Handles the XEvent EVENT on display DISPLAY.
9047 This is used for event loops outside the normal event handling,
9048 i.e. looping while a popup menu or a dialog is posted.
9050 Returns the value handle_one_xevent sets in the finish argument. */
9052 x_dispatch_event (XEvent *event, Display *display)
9054 struct x_display_info *dpyinfo;
9055 int finish = X_EVENT_NORMAL;
9057 dpyinfo = x_display_info_for_display (display);
9059 if (dpyinfo)
9060 handle_one_xevent (dpyinfo, event, &finish, 0);
9062 return finish;
9065 /* Read events coming from the X server.
9066 Return as soon as there are no more events to be read.
9068 Return the number of characters stored into the buffer,
9069 thus pretending to be `read' (except the characters we store
9070 in the keyboard buffer can be multibyte, so are not necessarily
9071 C chars). */
9073 static int
9074 XTread_socket (struct terminal *terminal, struct input_event *hold_quit)
9076 int count = 0;
9077 bool event_found = false;
9078 struct x_display_info *dpyinfo = terminal->display_info.x;
9080 block_input ();
9082 /* For debugging, this gives a way to fake an I/O error. */
9083 if (dpyinfo == XTread_socket_fake_io_error)
9085 XTread_socket_fake_io_error = 0;
9086 x_io_error_quitter (dpyinfo->display);
9089 #ifndef USE_GTK
9090 while (XPending (dpyinfo->display))
9092 int finish;
9093 XEvent event;
9095 XNextEvent (dpyinfo->display, &event);
9097 #ifdef HAVE_X_I18N
9098 /* Filter events for the current X input method. */
9099 if (x_filter_event (dpyinfo, &event))
9100 continue;
9101 #endif
9102 event_found = true;
9104 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
9106 if (finish == X_EVENT_GOTO_OUT)
9107 break;
9110 #else /* USE_GTK */
9112 /* For GTK we must use the GTK event loop. But XEvents gets passed
9113 to our filter function above, and then to the big event switch.
9114 We use a bunch of globals to communicate with our filter function,
9115 that is kind of ugly, but it works.
9117 There is no way to do one display at the time, GTK just does events
9118 from all displays. */
9120 while (gtk_events_pending ())
9122 current_count = count;
9123 current_hold_quit = hold_quit;
9125 gtk_main_iteration ();
9127 count = current_count;
9128 current_count = -1;
9129 current_hold_quit = 0;
9131 if (current_finish == X_EVENT_GOTO_OUT)
9132 break;
9134 #endif /* USE_GTK */
9136 /* On some systems, an X bug causes Emacs to get no more events
9137 when the window is destroyed. Detect that. (1994.) */
9138 if (! event_found)
9140 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
9141 One XNOOP in 100 loops will make Emacs terminate.
9142 B. Bretthauer, 1994 */
9143 x_noop_count++;
9144 if (x_noop_count >= 100)
9146 x_noop_count=0;
9148 if (next_noop_dpyinfo == 0)
9149 next_noop_dpyinfo = x_display_list;
9151 XNoOp (next_noop_dpyinfo->display);
9153 /* Each time we get here, cycle through the displays now open. */
9154 next_noop_dpyinfo = next_noop_dpyinfo->next;
9158 /* If the focus was just given to an auto-raising frame,
9159 raise it now. FIXME: handle more than one such frame. */
9160 if (dpyinfo->x_pending_autoraise_frame)
9162 x_raise_frame (dpyinfo->x_pending_autoraise_frame);
9163 dpyinfo->x_pending_autoraise_frame = NULL;
9166 unblock_input ();
9168 return count;
9174 /***********************************************************************
9175 Text Cursor
9176 ***********************************************************************/
9178 /* Set clipping for output in glyph row ROW. W is the window in which
9179 we operate. GC is the graphics context to set clipping in.
9181 ROW may be a text row or, e.g., a mode line. Text rows must be
9182 clipped to the interior of the window dedicated to text display,
9183 mode lines must be clipped to the whole window. */
9185 static void
9186 x_clip_to_row (struct window *w, struct glyph_row *row,
9187 enum glyph_row_area area, GC gc)
9189 struct frame *f = XFRAME (WINDOW_FRAME (w));
9190 XRectangle clip_rect;
9191 int window_x, window_y, window_width;
9193 window_box (w, area, &window_x, &window_y, &window_width, 0);
9195 clip_rect.x = window_x;
9196 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
9197 clip_rect.y = max (clip_rect.y, window_y);
9198 clip_rect.width = window_width;
9199 clip_rect.height = row->visible_height;
9201 x_set_clip_rectangles (f, gc, &clip_rect, 1);
9205 /* Draw a hollow box cursor on window W in glyph row ROW. */
9207 static void
9208 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
9210 struct frame *f = XFRAME (WINDOW_FRAME (w));
9211 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9212 Display *dpy = FRAME_X_DISPLAY (f);
9213 int x, y, wd, h;
9214 XGCValues xgcv;
9215 struct glyph *cursor_glyph;
9216 GC gc;
9218 /* Get the glyph the cursor is on. If we can't tell because
9219 the current matrix is invalid or such, give up. */
9220 cursor_glyph = get_phys_cursor_glyph (w);
9221 if (cursor_glyph == NULL)
9222 return;
9224 /* Compute frame-relative coordinates for phys cursor. */
9225 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
9226 wd = w->phys_cursor_width - 1;
9228 /* The foreground of cursor_gc is typically the same as the normal
9229 background color, which can cause the cursor box to be invisible. */
9230 xgcv.foreground = f->output_data.x->cursor_pixel;
9231 if (dpyinfo->scratch_cursor_gc)
9232 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
9233 else
9234 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_DRAWABLE (f),
9235 GCForeground, &xgcv);
9236 gc = dpyinfo->scratch_cursor_gc;
9238 /* When on R2L character, show cursor at the right edge of the
9239 glyph, unless the cursor box is as wide as the glyph or wider
9240 (the latter happens when x-stretch-cursor is non-nil). */
9241 if ((cursor_glyph->resolved_level & 1) != 0
9242 && cursor_glyph->pixel_width > wd)
9244 x += cursor_glyph->pixel_width - wd;
9245 if (wd > 0)
9246 wd -= 1;
9248 /* Set clipping, draw the rectangle, and reset clipping again. */
9249 x_clip_to_row (w, row, TEXT_AREA, gc);
9250 x_draw_rectangle (f, gc, x, y, wd, h - 1);
9251 x_reset_clip_rectangles (f, gc);
9255 /* Draw a bar cursor on window W in glyph row ROW.
9257 Implementation note: One would like to draw a bar cursor with an
9258 angle equal to the one given by the font property XA_ITALIC_ANGLE.
9259 Unfortunately, I didn't find a font yet that has this property set.
9260 --gerd. */
9262 static void
9263 x_draw_bar_cursor (struct window *w, struct glyph_row *row, int width, enum text_cursor_kinds kind)
9265 struct frame *f = XFRAME (w->frame);
9266 struct glyph *cursor_glyph;
9268 /* If cursor is out of bounds, don't draw garbage. This can happen
9269 in mini-buffer windows when switching between echo area glyphs
9270 and mini-buffer. */
9271 cursor_glyph = get_phys_cursor_glyph (w);
9272 if (cursor_glyph == NULL)
9273 return;
9275 /* Experimental avoidance of cursor on xwidget. */
9276 if (cursor_glyph->type == XWIDGET_GLYPH)
9277 return;
9279 /* If on an image, draw like a normal cursor. That's usually better
9280 visible than drawing a bar, esp. if the image is large so that
9281 the bar might not be in the window. */
9282 if (cursor_glyph->type == IMAGE_GLYPH)
9284 struct glyph_row *r;
9285 r = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
9286 draw_phys_cursor_glyph (w, r, DRAW_CURSOR);
9288 else
9290 Display *dpy = FRAME_X_DISPLAY (f);
9291 Drawable drawable = FRAME_X_DRAWABLE (f);
9292 GC gc = FRAME_DISPLAY_INFO (f)->scratch_cursor_gc;
9293 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
9294 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
9295 XGCValues xgcv;
9297 /* If the glyph's background equals the color we normally draw
9298 the bars cursor in, the bar cursor in its normal color is
9299 invisible. Use the glyph's foreground color instead in this
9300 case, on the assumption that the glyph's colors are chosen so
9301 that the glyph is legible. */
9302 if (face->background == f->output_data.x->cursor_pixel)
9303 xgcv.background = xgcv.foreground = face->foreground;
9304 else
9305 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
9306 xgcv.graphics_exposures = False;
9308 if (gc)
9309 XChangeGC (dpy, gc, mask, &xgcv);
9310 else
9312 gc = XCreateGC (dpy, drawable, mask, &xgcv);
9313 FRAME_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
9316 x_clip_to_row (w, row, TEXT_AREA, gc);
9318 if (kind == BAR_CURSOR)
9320 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9322 if (width < 0)
9323 width = FRAME_CURSOR_WIDTH (f);
9324 width = min (cursor_glyph->pixel_width, width);
9326 w->phys_cursor_width = width;
9328 /* If the character under cursor is R2L, draw the bar cursor
9329 on the right of its glyph, rather than on the left. */
9330 if ((cursor_glyph->resolved_level & 1) != 0)
9331 x += cursor_glyph->pixel_width - width;
9333 x_fill_rectangle (f, gc, x,
9334 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
9335 width, row->height);
9337 else /* HBAR_CURSOR */
9339 int dummy_x, dummy_y, dummy_h;
9340 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9342 if (width < 0)
9343 width = row->height;
9345 width = min (row->height, width);
9347 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
9348 &dummy_y, &dummy_h);
9350 if ((cursor_glyph->resolved_level & 1) != 0
9351 && cursor_glyph->pixel_width > w->phys_cursor_width - 1)
9352 x += cursor_glyph->pixel_width - w->phys_cursor_width + 1;
9353 x_fill_rectangle (f, gc, x,
9354 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
9355 row->height - width),
9356 w->phys_cursor_width - 1, width);
9359 x_reset_clip_rectangles (f, gc);
9364 /* RIF: Define cursor CURSOR on frame F. */
9366 static void
9367 x_define_frame_cursor (struct frame *f, Cursor cursor)
9369 if (!f->pointer_invisible
9370 && f->output_data.x->current_cursor != cursor)
9371 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
9372 f->output_data.x->current_cursor = cursor;
9376 /* RIF: Clear area on frame F. */
9378 static void
9379 x_clear_frame_area (struct frame *f, int x, int y, int width, int height)
9381 x_clear_area (f, x, y, width, height);
9385 /* RIF: Draw cursor on window W. */
9387 static void
9388 x_draw_window_cursor (struct window *w, struct glyph_row *glyph_row, int x,
9389 int y, enum text_cursor_kinds cursor_type,
9390 int cursor_width, bool on_p, bool active_p)
9392 struct frame *f = XFRAME (WINDOW_FRAME (w));
9394 if (on_p)
9396 w->phys_cursor_type = cursor_type;
9397 w->phys_cursor_on_p = true;
9399 if (glyph_row->exact_window_width_line_p
9400 && (glyph_row->reversed_p
9401 ? (w->phys_cursor.hpos < 0)
9402 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
9404 glyph_row->cursor_in_fringe_p = true;
9405 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
9407 else
9409 switch (cursor_type)
9411 case HOLLOW_BOX_CURSOR:
9412 x_draw_hollow_cursor (w, glyph_row);
9413 break;
9415 case FILLED_BOX_CURSOR:
9416 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
9417 break;
9419 case BAR_CURSOR:
9420 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
9421 break;
9423 case HBAR_CURSOR:
9424 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
9425 break;
9427 case NO_CURSOR:
9428 w->phys_cursor_width = 0;
9429 break;
9431 default:
9432 emacs_abort ();
9436 #ifdef HAVE_X_I18N
9437 if (w == XWINDOW (f->selected_window))
9438 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
9439 xic_set_preeditarea (w, x, y);
9440 #endif
9443 XFlush (FRAME_X_DISPLAY (f));
9447 /* Icons. */
9449 /* Make the x-window of frame F use the gnu icon bitmap. */
9451 bool
9452 x_bitmap_icon (struct frame *f, Lisp_Object file)
9454 ptrdiff_t bitmap_id;
9456 if (FRAME_X_WINDOW (f) == 0)
9457 return true;
9459 /* Free up our existing icon bitmap and mask if any. */
9460 if (f->output_data.x->icon_bitmap > 0)
9461 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
9462 f->output_data.x->icon_bitmap = 0;
9464 if (STRINGP (file))
9466 #ifdef USE_GTK
9467 /* Use gtk_window_set_icon_from_file () if available,
9468 It's not restricted to bitmaps */
9469 if (xg_set_icon (f, file))
9470 return false;
9471 #endif /* USE_GTK */
9472 bitmap_id = x_create_bitmap_from_file (f, file);
9473 x_create_bitmap_mask (f, bitmap_id);
9475 else
9477 /* Create the GNU bitmap and mask if necessary. */
9478 if (FRAME_DISPLAY_INFO (f)->icon_bitmap_id < 0)
9480 ptrdiff_t rc = -1;
9482 #ifdef USE_GTK
9484 if (xg_set_icon (f, xg_default_icon_file)
9485 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
9487 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = -2;
9488 return false;
9491 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
9493 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
9494 if (rc != -1)
9495 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
9497 #endif
9499 /* If all else fails, use the (black and white) xbm image. */
9500 if (rc == -1)
9502 rc = x_create_bitmap_from_data (f, (char *) gnu_xbm_bits,
9503 gnu_xbm_width, gnu_xbm_height);
9504 if (rc == -1)
9505 return true;
9507 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
9508 x_create_bitmap_mask (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
9512 /* The first time we create the GNU bitmap and mask,
9513 this increments the ref-count one extra time.
9514 As a result, the GNU bitmap and mask are never freed.
9515 That way, we don't have to worry about allocating it again. */
9516 x_reference_bitmap (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
9518 bitmap_id = FRAME_DISPLAY_INFO (f)->icon_bitmap_id;
9521 x_wm_set_icon_pixmap (f, bitmap_id);
9522 f->output_data.x->icon_bitmap = bitmap_id;
9524 return false;
9528 /* Make the x-window of frame F use a rectangle with text.
9529 Use ICON_NAME as the text. */
9531 bool
9532 x_text_icon (struct frame *f, const char *icon_name)
9534 if (FRAME_X_WINDOW (f) == 0)
9535 return true;
9538 XTextProperty text;
9539 text.value = (unsigned char *) icon_name;
9540 text.encoding = XA_STRING;
9541 text.format = 8;
9542 text.nitems = strlen (icon_name);
9543 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
9546 if (f->output_data.x->icon_bitmap > 0)
9547 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
9548 f->output_data.x->icon_bitmap = 0;
9549 x_wm_set_icon_pixmap (f, 0);
9551 return false;
9554 #define X_ERROR_MESSAGE_SIZE 200
9556 /* If non-nil, this should be a string.
9557 It means catch X errors and store the error message in this string.
9559 The reason we use a stack is that x_catch_error/x_uncatch_error can
9560 be called from a signal handler.
9563 struct x_error_message_stack {
9564 char string[X_ERROR_MESSAGE_SIZE];
9565 Display *dpy;
9566 x_special_error_handler handler;
9567 void *handler_data;
9568 struct x_error_message_stack *prev;
9570 static struct x_error_message_stack *x_error_message;
9572 /* An X error handler which stores the error message in
9573 *x_error_message. This is called from x_error_handler if
9574 x_catch_errors is in effect. */
9576 static void
9577 x_error_catcher (Display *display, XErrorEvent *event)
9579 XGetErrorText (display, event->error_code,
9580 x_error_message->string,
9581 X_ERROR_MESSAGE_SIZE);
9582 if (x_error_message->handler)
9583 x_error_message->handler (display, event, x_error_message->string,
9584 x_error_message->handler_data);
9587 /* Begin trapping X errors for display DPY. Actually we trap X errors
9588 for all displays, but DPY should be the display you are actually
9589 operating on.
9591 After calling this function, X protocol errors no longer cause
9592 Emacs to exit; instead, they are recorded in the string
9593 stored in *x_error_message.
9595 Calling x_check_errors signals an Emacs error if an X error has
9596 occurred since the last call to x_catch_errors or x_check_errors.
9598 Calling x_uncatch_errors resumes the normal error handling.
9599 Calling x_uncatch_errors_after_check is similar, but skips an XSync
9600 to the server, and should be used only immediately after
9601 x_had_errors_p or x_check_errors. */
9603 void
9604 x_catch_errors_with_handler (Display *dpy, x_special_error_handler handler,
9605 void *handler_data)
9607 struct x_error_message_stack *data = xmalloc (sizeof *data);
9609 /* Make sure any errors from previous requests have been dealt with. */
9610 XSync (dpy, False);
9612 data->dpy = dpy;
9613 data->string[0] = 0;
9614 data->handler = handler;
9615 data->handler_data = handler_data;
9616 data->prev = x_error_message;
9617 x_error_message = data;
9620 void
9621 x_catch_errors (Display *dpy)
9623 x_catch_errors_with_handler (dpy, NULL, NULL);
9626 /* Undo the last x_catch_errors call.
9627 DPY should be the display that was passed to x_catch_errors.
9629 This version should be used only if the immediately preceding
9630 X-protocol-related thing was x_check_errors or x_had_error_p, both
9631 of which issue XSync calls, so we don't need to re-sync here. */
9633 void
9634 x_uncatch_errors_after_check (void)
9636 struct x_error_message_stack *tmp;
9638 block_input ();
9639 tmp = x_error_message;
9640 x_error_message = x_error_message->prev;
9641 xfree (tmp);
9642 unblock_input ();
9645 /* Undo the last x_catch_errors call.
9646 DPY should be the display that was passed to x_catch_errors. */
9648 void
9649 x_uncatch_errors (void)
9651 struct x_error_message_stack *tmp;
9653 block_input ();
9655 /* The display may have been closed before this function is called.
9656 Check if it is still open before calling XSync. */
9657 if (x_display_info_for_display (x_error_message->dpy) != 0)
9658 XSync (x_error_message->dpy, False);
9660 tmp = x_error_message;
9661 x_error_message = x_error_message->prev;
9662 xfree (tmp);
9663 unblock_input ();
9666 /* If any X protocol errors have arrived since the last call to
9667 x_catch_errors or x_check_errors, signal an Emacs error using
9668 sprintf (a buffer, FORMAT, the x error message text) as the text. */
9670 void
9671 x_check_errors (Display *dpy, const char *format)
9673 /* Make sure to catch any errors incurred so far. */
9674 XSync (dpy, False);
9676 if (x_error_message->string[0])
9678 char string[X_ERROR_MESSAGE_SIZE];
9679 memcpy (string, x_error_message->string, X_ERROR_MESSAGE_SIZE);
9680 x_uncatch_errors ();
9681 error (format, string);
9685 /* Nonzero if we had any X protocol errors
9686 since we did x_catch_errors on DPY. */
9688 bool
9689 x_had_errors_p (Display *dpy)
9691 /* Make sure to catch any errors incurred so far. */
9692 XSync (dpy, False);
9694 return x_error_message->string[0] != 0;
9697 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
9699 void
9700 x_clear_errors (Display *dpy)
9702 x_error_message->string[0] = 0;
9705 #if false
9706 /* See comment in unwind_to_catch why calling this is a bad
9707 * idea. --lorentey */
9708 /* Close off all unclosed x_catch_errors calls. */
9710 void
9711 x_fully_uncatch_errors (void)
9713 while (x_error_message)
9714 x_uncatch_errors ();
9716 #endif
9718 #if false
9719 static unsigned int x_wire_count;
9720 x_trace_wire (void)
9722 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
9724 #endif
9727 /************************************************************************
9728 Handling X errors
9729 ************************************************************************/
9731 /* Error message passed to x_connection_closed. */
9733 static char *error_msg;
9735 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
9736 the text of an error message that lead to the connection loss. */
9738 static _Noreturn void
9739 x_connection_closed (Display *dpy, const char *error_message, bool ioerror)
9741 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
9742 Lisp_Object frame, tail;
9743 ptrdiff_t idx = SPECPDL_INDEX ();
9745 error_msg = alloca (strlen (error_message) + 1);
9746 strcpy (error_msg, error_message);
9748 /* Inhibit redisplay while frames are being deleted. */
9749 specbind (Qinhibit_redisplay, Qt);
9751 if (dpyinfo)
9753 /* Protect display from being closed when we delete the last
9754 frame on it. */
9755 dpyinfo->reference_count++;
9756 dpyinfo->terminal->reference_count++;
9758 if (ioerror) dpyinfo->display = 0;
9760 /* First delete frames whose mini-buffers are on frames
9761 that are on the dead display. */
9762 FOR_EACH_FRAME (tail, frame)
9764 Lisp_Object minibuf_frame;
9765 minibuf_frame
9766 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
9767 if (FRAME_X_P (XFRAME (frame))
9768 && FRAME_X_P (XFRAME (minibuf_frame))
9769 && ! EQ (frame, minibuf_frame)
9770 && FRAME_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
9771 delete_frame (frame, Qnoelisp);
9774 /* Now delete all remaining frames on the dead display.
9775 We are now sure none of these is used as the mini-buffer
9776 for another frame that we need to delete. */
9777 FOR_EACH_FRAME (tail, frame)
9778 if (FRAME_X_P (XFRAME (frame))
9779 && FRAME_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
9781 /* Set this to t so that delete_frame won't get confused
9782 trying to find a replacement. */
9783 kset_default_minibuffer_frame (FRAME_KBOARD (XFRAME (frame)), Qt);
9784 delete_frame (frame, Qnoelisp);
9787 /* If DPYINFO is null, this means we didn't open the display in the
9788 first place, so don't try to close it. */
9789 if (dpyinfo)
9791 /* We can not call XtCloseDisplay here because it calls XSync.
9792 XSync inside the error handler apparently hangs Emacs. On
9793 current Xt versions, this isn't needed either. */
9794 #ifdef USE_GTK
9795 /* A long-standing GTK bug prevents proper disconnect handling
9796 (https://bugzilla.gnome.org/show_bug.cgi?id=85715). Once,
9797 the resulting Glib error message loop filled a user's disk.
9798 To avoid this, kill Emacs unconditionally on disconnect. */
9799 shut_down_emacs (0, Qnil);
9800 fprintf (stderr, "%s\n\
9801 When compiled with GTK, Emacs cannot recover from X disconnects.\n\
9802 This is a GTK bug: https://bugzilla.gnome.org/show_bug.cgi?id=85715\n\
9803 For details, see etc/PROBLEMS.\n",
9804 error_msg);
9805 emacs_abort ();
9806 #endif /* USE_GTK */
9808 /* Indicate that this display is dead. */
9809 dpyinfo->display = 0;
9811 dpyinfo->reference_count--;
9812 dpyinfo->terminal->reference_count--;
9813 if (dpyinfo->reference_count != 0)
9814 /* We have just closed all frames on this display. */
9815 emacs_abort ();
9818 Lisp_Object tmp;
9819 XSETTERMINAL (tmp, dpyinfo->terminal);
9820 Fdelete_terminal (tmp, Qnoelisp);
9824 if (terminal_list == 0)
9826 fprintf (stderr, "%s\n", error_msg);
9827 Fkill_emacs (make_number (70));
9828 /* NOTREACHED */
9831 totally_unblock_input ();
9833 unbind_to (idx, Qnil);
9834 clear_waiting_for_input ();
9836 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
9837 longjmp), because returning from this function would get us back into
9838 Xlib's code which will directly call `exit'. */
9839 error ("%s", error_msg);
9842 /* We specifically use it before defining it, so that gcc doesn't inline it,
9843 otherwise gdb doesn't know how to properly put a breakpoint on it. */
9844 static void x_error_quitter (Display *, XErrorEvent *);
9846 /* This is the first-level handler for X protocol errors.
9847 It calls x_error_quitter or x_error_catcher. */
9849 static int
9850 x_error_handler (Display *display, XErrorEvent *event)
9852 #if defined USE_GTK && defined HAVE_GTK3
9853 if ((event->error_code == BadMatch || event->error_code == BadWindow)
9854 && event->request_code == X_SetInputFocus)
9856 return 0;
9858 #endif
9860 if (x_error_message)
9861 x_error_catcher (display, event);
9862 else
9863 x_error_quitter (display, event);
9864 return 0;
9867 /* This is the usual handler for X protocol errors.
9868 It kills all frames on the display that we got the error for.
9869 If that was the only one, it prints an error message and kills Emacs. */
9871 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
9873 /* On older GCC versions, just putting x_error_quitter
9874 after x_error_handler prevents inlining into the former. */
9876 static void NO_INLINE
9877 x_error_quitter (Display *display, XErrorEvent *event)
9879 char buf[256], buf1[356];
9881 /* Ignore BadName errors. They can happen because of fonts
9882 or colors that are not defined. */
9884 if (event->error_code == BadName)
9885 return;
9887 /* Note that there is no real way portable across R3/R4 to get the
9888 original error handler. */
9890 XGetErrorText (display, event->error_code, buf, sizeof (buf));
9891 sprintf (buf1, "X protocol error: %s on protocol request %d",
9892 buf, event->request_code);
9893 x_connection_closed (display, buf1, false);
9897 /* This is the handler for X IO errors, always.
9898 It kills all frames on the display that we lost touch with.
9899 If that was the only one, it prints an error message and kills Emacs. */
9901 static _Noreturn int
9902 x_io_error_quitter (Display *display)
9904 char buf[256];
9906 snprintf (buf, sizeof buf, "Connection lost to X server '%s'",
9907 DisplayString (display));
9908 x_connection_closed (display, buf, true);
9909 assume (false);
9912 /* Changing the font of the frame. */
9914 /* Give frame F the font FONT-OBJECT as its default font. The return
9915 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
9916 frame. If it is negative, generate a new fontset from
9917 FONT-OBJECT. */
9919 Lisp_Object
9920 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
9922 struct font *font = XFONT_OBJECT (font_object);
9923 int unit, font_ascent, font_descent;
9924 #ifndef USE_X_TOOLKIT
9925 int old_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
9926 Lisp_Object fullscreen;
9927 #endif
9929 if (fontset < 0)
9930 fontset = fontset_from_font (font_object);
9931 FRAME_FONTSET (f) = fontset;
9932 if (FRAME_FONT (f) == font)
9933 /* This font is already set in frame F. There's nothing more to
9934 do. */
9935 return font_object;
9937 FRAME_FONT (f) = font;
9938 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
9939 FRAME_COLUMN_WIDTH (f) = font->average_width;
9940 get_font_ascent_descent (font, &font_ascent, &font_descent);
9941 FRAME_LINE_HEIGHT (f) = font_ascent + font_descent;
9943 #ifndef USE_X_TOOLKIT
9944 FRAME_MENU_BAR_HEIGHT (f) = FRAME_MENU_BAR_LINES (f) * FRAME_LINE_HEIGHT (f);
9945 #endif
9947 /* Compute character columns occupied by scrollbar.
9949 Don't do things differently for non-toolkit scrollbars
9950 (Bug#17163). */
9951 unit = FRAME_COLUMN_WIDTH (f);
9952 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
9953 FRAME_CONFIG_SCROLL_BAR_COLS (f)
9954 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + unit - 1) / unit;
9955 else
9956 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
9958 if (FRAME_X_WINDOW (f) != 0)
9960 /* Don't change the size of a tip frame; there's no point in
9961 doing it because it's done in Fx_show_tip, and it leads to
9962 problems because the tip frame has no widget. */
9963 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
9965 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
9966 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 3,
9967 false, Qfont);
9968 #ifndef USE_X_TOOLKIT
9969 if (FRAME_MENU_BAR_HEIGHT (f) != old_menu_bar_height
9970 && !f->after_make_frame
9971 && (EQ (frame_inhibit_implied_resize, Qt)
9972 || (CONSP (frame_inhibit_implied_resize)
9973 && NILP (Fmemq (Qfont, frame_inhibit_implied_resize))))
9974 && (NILP (fullscreen = get_frame_param (f, Qfullscreen))
9975 || EQ (fullscreen, Qfullwidth)))
9976 /* If the menu bar height changes, try to keep text height
9977 constant. */
9978 adjust_frame_size
9979 (f, -1, FRAME_TEXT_HEIGHT (f) + FRAME_MENU_BAR_HEIGHT (f)
9980 - old_menu_bar_height, 1, false, Qfont);
9981 #endif /* USE_X_TOOLKIT */
9985 #ifdef HAVE_X_I18N
9986 if (FRAME_XIC (f)
9987 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
9989 block_input ();
9990 xic_set_xfontset (f, SSDATA (fontset_ascii (fontset)));
9991 unblock_input ();
9993 #endif
9995 return font_object;
9999 /***********************************************************************
10000 X Input Methods
10001 ***********************************************************************/
10003 #ifdef HAVE_X_I18N
10005 #ifdef HAVE_X11R6
10007 /* XIM destroy callback function, which is called whenever the
10008 connection to input method XIM dies. CLIENT_DATA contains a
10009 pointer to the x_display_info structure corresponding to XIM. */
10011 static void
10012 xim_destroy_callback (XIM xim, XPointer client_data, XPointer call_data)
10014 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
10015 Lisp_Object frame, tail;
10017 block_input ();
10019 /* No need to call XDestroyIC.. */
10020 FOR_EACH_FRAME (tail, frame)
10022 struct frame *f = XFRAME (frame);
10023 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
10025 FRAME_XIC (f) = NULL;
10026 xic_free_xfontset (f);
10030 /* No need to call XCloseIM. */
10031 dpyinfo->xim = NULL;
10032 XFree (dpyinfo->xim_styles);
10033 unblock_input ();
10036 #endif /* HAVE_X11R6 */
10038 /* Open the connection to the XIM server on display DPYINFO.
10039 RESOURCE_NAME is the resource name Emacs uses. */
10041 static void
10042 xim_open_dpy (struct x_display_info *dpyinfo, char *resource_name)
10044 XIM xim;
10046 #ifdef HAVE_XIM
10047 if (use_xim)
10049 if (dpyinfo->xim)
10050 XCloseIM (dpyinfo->xim);
10051 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
10052 emacs_class);
10053 dpyinfo->xim = xim;
10055 if (xim)
10057 #ifdef HAVE_X11R6
10058 XIMCallback destroy;
10059 #endif
10061 /* Get supported styles and XIM values. */
10062 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
10064 #ifdef HAVE_X11R6
10065 destroy.callback = xim_destroy_callback;
10066 destroy.client_data = (XPointer)dpyinfo;
10067 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
10068 #endif
10072 else
10073 #endif /* HAVE_XIM */
10074 dpyinfo->xim = NULL;
10078 #ifdef HAVE_X11R6_XIM
10080 /* XIM instantiate callback function, which is called whenever an XIM
10081 server is available. DISPLAY is the display of the XIM.
10082 CLIENT_DATA contains a pointer to an xim_inst_t structure created
10083 when the callback was registered. */
10085 static void
10086 xim_instantiate_callback (Display *display, XPointer client_data, XPointer call_data)
10088 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
10089 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
10091 /* We don't support multiple XIM connections. */
10092 if (dpyinfo->xim)
10093 return;
10095 xim_open_dpy (dpyinfo, xim_inst->resource_name);
10097 /* Create XIC for the existing frames on the same display, as long
10098 as they have no XIC. */
10099 if (dpyinfo->xim && dpyinfo->reference_count > 0)
10101 Lisp_Object tail, frame;
10103 block_input ();
10104 FOR_EACH_FRAME (tail, frame)
10106 struct frame *f = XFRAME (frame);
10108 if (FRAME_X_P (f)
10109 && FRAME_DISPLAY_INFO (f) == xim_inst->dpyinfo)
10110 if (FRAME_XIC (f) == NULL)
10112 create_frame_xic (f);
10113 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
10114 xic_set_statusarea (f);
10115 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
10117 struct window *w = XWINDOW (f->selected_window);
10118 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
10123 unblock_input ();
10127 #endif /* HAVE_X11R6_XIM */
10130 /* Open a connection to the XIM server on display DPYINFO.
10131 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
10132 connection only at the first time. On X11R6, open the connection
10133 in the XIM instantiate callback function. */
10135 static void
10136 xim_initialize (struct x_display_info *dpyinfo, char *resource_name)
10138 dpyinfo->xim = NULL;
10139 #ifdef HAVE_XIM
10140 if (use_xim)
10142 #ifdef HAVE_X11R6_XIM
10143 struct xim_inst_t *xim_inst = xmalloc (sizeof *xim_inst);
10144 Bool ret;
10146 dpyinfo->xim_callback_data = xim_inst;
10147 xim_inst->dpyinfo = dpyinfo;
10148 xim_inst->resource_name = xstrdup (resource_name);
10149 ret = XRegisterIMInstantiateCallback
10150 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
10151 emacs_class, xim_instantiate_callback,
10152 /* This is XPointer in XFree86 but (XPointer *)
10153 on Tru64, at least, hence the configure test. */
10154 (XRegisterIMInstantiateCallback_arg6) xim_inst);
10155 eassert (ret == True);
10156 #else /* not HAVE_X11R6_XIM */
10157 xim_open_dpy (dpyinfo, resource_name);
10158 #endif /* not HAVE_X11R6_XIM */
10160 #endif /* HAVE_XIM */
10164 /* Close the connection to the XIM server on display DPYINFO. */
10166 static void
10167 xim_close_dpy (struct x_display_info *dpyinfo)
10169 #ifdef HAVE_XIM
10170 if (use_xim)
10172 #ifdef HAVE_X11R6_XIM
10173 struct xim_inst_t *xim_inst = dpyinfo->xim_callback_data;
10175 if (dpyinfo->display)
10177 Bool ret = XUnregisterIMInstantiateCallback
10178 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
10179 emacs_class, xim_instantiate_callback,
10180 (XRegisterIMInstantiateCallback_arg6) xim_inst);
10181 eassert (ret == True);
10183 xfree (xim_inst->resource_name);
10184 xfree (xim_inst);
10185 #endif /* HAVE_X11R6_XIM */
10186 if (dpyinfo->display)
10187 XCloseIM (dpyinfo->xim);
10188 dpyinfo->xim = NULL;
10189 XFree (dpyinfo->xim_styles);
10191 #endif /* HAVE_XIM */
10194 #endif /* not HAVE_X11R6_XIM */
10198 /* Calculate the absolute position in frame F
10199 from its current recorded position values and gravity. */
10201 static void
10202 x_calc_absolute_position (struct frame *f)
10204 int flags = f->size_hint_flags;
10205 struct frame *p = FRAME_PARENT_FRAME (f);
10207 /* We have nothing to do if the current position
10208 is already for the top-left corner. */
10209 if (! ((flags & XNegative) || (flags & YNegative)))
10210 return;
10212 /* Treat negative positions as relative to the leftmost bottommost
10213 position that fits on the screen. */
10214 if ((flags & XNegative) && (f->left_pos <= 0))
10216 int width = FRAME_PIXEL_WIDTH (f);
10218 /* A frame that has been visible at least once should have outer
10219 edges. */
10220 if (f->output_data.x->has_been_visible && !p)
10222 Lisp_Object frame;
10223 Lisp_Object edges = Qnil;
10225 XSETFRAME (frame, f);
10226 edges = Fx_frame_edges (frame, Qouter_edges);
10227 if (!NILP (edges))
10228 width = (XINT (Fnth (make_number (2), edges))
10229 - XINT (Fnth (make_number (0), edges)));
10232 if (p)
10233 f->left_pos = (FRAME_PIXEL_WIDTH (p) - width - 2 * f->border_width
10234 + f->left_pos);
10235 else
10236 f->left_pos = (x_display_pixel_width (FRAME_DISPLAY_INFO (f))
10237 - width + f->left_pos);
10241 if ((flags & YNegative) && (f->top_pos <= 0))
10243 int height = FRAME_PIXEL_HEIGHT (f);
10245 #if defined USE_X_TOOLKIT && defined USE_MOTIF
10246 /* Something is fishy here. When using Motif, starting Emacs with
10247 `-g -0-0', the frame appears too low by a few pixels.
10249 This seems to be so because initially, while Emacs is starting,
10250 the column widget's height and the frame's pixel height are
10251 different. The column widget's height is the right one. In
10252 later invocations, when Emacs is up, the frame's pixel height
10253 is right, though.
10255 It's not obvious where the initial small difference comes from.
10256 2000-12-01, gerd. */
10258 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
10259 #endif
10261 if (f->output_data.x->has_been_visible && !p)
10263 Lisp_Object frame;
10264 Lisp_Object edges = Qnil;
10266 XSETFRAME (frame, f);
10267 if (NILP (edges))
10268 edges = Fx_frame_edges (frame, Qouter_edges);
10269 if (!NILP (edges))
10270 height = (XINT (Fnth (make_number (3), edges))
10271 - XINT (Fnth (make_number (1), edges)));
10274 if (p)
10275 f->top_pos = (FRAME_PIXEL_HEIGHT (p) - height - 2 * f->border_width
10276 + f->top_pos);
10277 else
10278 f->top_pos = (x_display_pixel_height (FRAME_DISPLAY_INFO (f))
10279 - height + f->top_pos);
10282 /* The left_pos and top_pos
10283 are now relative to the top and left screen edges,
10284 so the flags should correspond. */
10285 f->size_hint_flags &= ~ (XNegative | YNegative);
10288 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
10289 to really change the position, and 0 when calling from
10290 x_make_frame_visible (in that case, XOFF and YOFF are the current
10291 position values). It is -1 when calling from x_set_frame_parameters,
10292 which means, do adjust for borders but don't change the gravity. */
10294 void
10295 x_set_offset (struct frame *f, register int xoff, register int yoff, int change_gravity)
10297 int modified_top, modified_left;
10299 if (change_gravity > 0)
10301 f->top_pos = yoff;
10302 f->left_pos = xoff;
10303 f->size_hint_flags &= ~ (XNegative | YNegative);
10304 if (xoff < 0)
10305 f->size_hint_flags |= XNegative;
10306 if (yoff < 0)
10307 f->size_hint_flags |= YNegative;
10308 f->win_gravity = NorthWestGravity;
10311 x_calc_absolute_position (f);
10313 block_input ();
10314 x_wm_set_size_hint (f, 0, false);
10316 #ifdef USE_GTK
10317 if (x_gtk_use_window_move)
10319 /* When a position change was requested and the outer GTK widget
10320 has been realized already, leave it to gtk_window_move to DTRT
10321 and return. Used for Bug#25851 and Bug#25943. */
10322 if (change_gravity != 0 && FRAME_GTK_OUTER_WIDGET (f))
10323 gtk_window_move (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
10324 f->left_pos, f->top_pos);
10325 unblock_input ();
10326 return;
10328 #endif /* USE_GTK */
10330 modified_left = f->left_pos;
10331 modified_top = f->top_pos;
10333 if (change_gravity != 0 && FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
10335 /* Some WMs (twm, wmaker at least) has an offset that is smaller
10336 than the WM decorations. So we use the calculated offset instead
10337 of the WM decoration sizes here (x/y_pixels_outer_diff). */
10338 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
10339 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
10342 #ifdef USE_GTK
10343 gtk_window_move (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
10344 modified_left, modified_top);
10345 #else
10346 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10347 modified_left, modified_top);
10348 #endif
10350 x_sync_with_move (f, f->left_pos, f->top_pos,
10351 FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN);
10353 /* change_gravity is non-zero when this function is called from Lisp to
10354 programmatically move a frame. In that case, we call
10355 x_check_expected_move to discover if we have a "Type A" or "Type B"
10356 window manager, and, for a "Type A" window manager, adjust the position
10357 of the frame.
10359 We call x_check_expected_move if a programmatic move occurred, and
10360 either the window manager type (A/B) is unknown or it is Type A but we
10361 need to compute the top/left offset adjustment for this frame. */
10363 if (change_gravity != 0
10364 && !FRAME_PARENT_FRAME (f)
10365 && (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
10366 || (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
10367 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
10368 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
10369 x_check_expected_move (f, modified_left, modified_top);
10371 unblock_input ();
10374 /* Return true if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
10375 on the root window for frame F contains ATOMNAME.
10376 This is how a WM check shall be done according to the Window Manager
10377 Specification/Extended Window Manager Hints at
10378 http://freedesktop.org/wiki/Specifications/wm-spec. */
10380 bool
10381 x_wm_supports (struct frame *f, Atom want_atom)
10383 Atom actual_type;
10384 unsigned long actual_size, bytes_remaining;
10385 int i, rc, actual_format;
10386 bool ret;
10387 Window wmcheck_window;
10388 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10389 Window target_window = dpyinfo->root_window;
10390 int max_len = 65536;
10391 Display *dpy = FRAME_X_DISPLAY (f);
10392 unsigned char *tmp_data = NULL;
10393 Atom target_type = XA_WINDOW;
10395 block_input ();
10397 x_catch_errors (dpy);
10398 rc = XGetWindowProperty (dpy, target_window,
10399 dpyinfo->Xatom_net_supporting_wm_check,
10400 0, max_len, False, target_type,
10401 &actual_type, &actual_format, &actual_size,
10402 &bytes_remaining, &tmp_data);
10404 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
10406 if (tmp_data) XFree (tmp_data);
10407 x_uncatch_errors ();
10408 unblock_input ();
10409 return false;
10412 wmcheck_window = *(Window *) tmp_data;
10413 XFree (tmp_data);
10415 /* Check if window exists. */
10416 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
10417 if (x_had_errors_p (dpy))
10419 x_uncatch_errors_after_check ();
10420 unblock_input ();
10421 return false;
10424 if (dpyinfo->net_supported_window != wmcheck_window)
10426 /* Window changed, reload atoms */
10427 if (dpyinfo->net_supported_atoms != NULL)
10428 XFree (dpyinfo->net_supported_atoms);
10429 dpyinfo->net_supported_atoms = NULL;
10430 dpyinfo->nr_net_supported_atoms = 0;
10431 dpyinfo->net_supported_window = 0;
10433 target_type = XA_ATOM;
10434 tmp_data = NULL;
10435 rc = XGetWindowProperty (dpy, target_window,
10436 dpyinfo->Xatom_net_supported,
10437 0, max_len, False, target_type,
10438 &actual_type, &actual_format, &actual_size,
10439 &bytes_remaining, &tmp_data);
10441 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
10443 if (tmp_data) XFree (tmp_data);
10444 x_uncatch_errors ();
10445 unblock_input ();
10446 return false;
10449 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
10450 dpyinfo->nr_net_supported_atoms = actual_size;
10451 dpyinfo->net_supported_window = wmcheck_window;
10454 ret = false;
10456 for (i = 0; !ret && i < dpyinfo->nr_net_supported_atoms; ++i)
10457 ret = dpyinfo->net_supported_atoms[i] == want_atom;
10459 x_uncatch_errors ();
10460 unblock_input ();
10462 return ret;
10465 static void
10466 set_wm_state (Lisp_Object frame, bool add, Atom atom, Atom value)
10468 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (XFRAME (frame));
10470 x_send_client_event (frame, make_number (0), frame,
10471 dpyinfo->Xatom_net_wm_state,
10472 make_number (32),
10473 /* 1 = add, 0 = remove */
10474 Fcons
10475 (make_number (add),
10476 Fcons
10477 (make_fixnum_or_float (atom),
10478 (value != 0
10479 ? list1 (make_fixnum_or_float (value))
10480 : Qnil))));
10483 void
10484 x_set_sticky (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
10486 Lisp_Object frame;
10487 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10489 XSETFRAME (frame, f);
10491 set_wm_state (frame, !NILP (new_value),
10492 dpyinfo->Xatom_net_wm_state_sticky, None);
10496 * x_set_skip_taskbar:
10498 * Set frame F's `skip-taskbar' parameter. If non-nil, this should
10499 * remove F's icon from the taskbar associated with the display of F's
10500 * window-system window and inhibit switching to F's window via
10501 * <Alt>-<TAB>. If nil, lift these restrictions.
10503 * Some window managers may not honor this parameter.
10505 void
10506 x_set_skip_taskbar (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
10508 if (!EQ (new_value, old_value))
10510 #ifdef USE_GTK
10511 xg_set_skip_taskbar (f, new_value);
10512 #else
10513 Lisp_Object frame;
10514 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10516 XSETFRAME (frame, f);
10517 set_wm_state (frame, !NILP (new_value),
10518 dpyinfo->Xatom_net_wm_state_skip_taskbar, None);
10519 #endif /* USE_GTK */
10520 FRAME_SKIP_TASKBAR (f) = !NILP (new_value);
10525 * x_set_z_group:
10527 * Set frame F's `z-group' parameter. If `above', F's window-system
10528 * window is displayed above all windows that do not have the `above'
10529 * property set. If nil, F's window is shown below all windows that
10530 * have the `above' property set and above all windows that have the
10531 * `below' property set. If `below', F's window is displayed below all
10532 * windows that do not have the `below' property set.
10534 * Some window managers may not honor this parameter.
10536 void
10537 x_set_z_group (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
10539 /* We don't care about old_value. The window manager might have
10540 reset the value without telling us. */
10541 Lisp_Object frame;
10542 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10544 XSETFRAME (frame, f);
10546 if (NILP (new_value))
10548 set_wm_state (frame, false,
10549 dpyinfo->Xatom_net_wm_state_above, None);
10550 set_wm_state (frame, false,
10551 dpyinfo->Xatom_net_wm_state_below, None);
10552 FRAME_Z_GROUP (f) = z_group_none;
10554 else if (EQ (new_value, Qabove))
10556 set_wm_state (frame, true,
10557 dpyinfo->Xatom_net_wm_state_above, None);
10558 set_wm_state (frame, false,
10559 dpyinfo->Xatom_net_wm_state_below, None);
10560 FRAME_Z_GROUP (f) = z_group_above;
10562 else if (EQ (new_value, Qbelow))
10564 set_wm_state (frame, false,
10565 dpyinfo->Xatom_net_wm_state_above, None);
10566 set_wm_state (frame, true,
10567 dpyinfo->Xatom_net_wm_state_below, None);
10568 FRAME_Z_GROUP (f) = z_group_below;
10570 else if (EQ (new_value, Qabove_suspended))
10572 set_wm_state (frame, false,
10573 dpyinfo->Xatom_net_wm_state_above, None);
10574 FRAME_Z_GROUP (f) = z_group_above_suspended;
10576 else
10577 error ("Invalid z-group specification");
10581 /* Return the current _NET_WM_STATE.
10582 SIZE_STATE is set to one of the FULLSCREEN_* values.
10583 Set *STICKY to the sticky state.
10585 Return true iff we are not hidden. */
10587 static bool
10588 get_current_wm_state (struct frame *f,
10589 Window window,
10590 int *size_state,
10591 bool *sticky)
10593 unsigned long actual_size;
10594 int i;
10595 bool is_hidden = false;
10596 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10597 long max_len = 65536;
10598 Atom target_type = XA_ATOM;
10599 /* If XCB is available, we can avoid three XSync calls. */
10600 #ifdef USE_XCB
10601 xcb_get_property_cookie_t prop_cookie;
10602 xcb_get_property_reply_t *prop;
10603 xcb_atom_t *reply_data;
10604 #else
10605 Display *dpy = FRAME_X_DISPLAY (f);
10606 unsigned long bytes_remaining;
10607 int rc, actual_format;
10608 Atom actual_type;
10609 unsigned char *tmp_data = NULL;
10610 Atom *reply_data;
10611 #endif
10613 *sticky = false;
10614 *size_state = FULLSCREEN_NONE;
10616 block_input ();
10618 #ifdef USE_XCB
10619 prop_cookie = xcb_get_property (dpyinfo->xcb_connection, 0, window,
10620 dpyinfo->Xatom_net_wm_state,
10621 target_type, 0, max_len);
10622 prop = xcb_get_property_reply (dpyinfo->xcb_connection, prop_cookie, NULL);
10623 if (prop && prop->type == target_type)
10625 int actual_bytes = xcb_get_property_value_length (prop);
10626 eassume (0 <= actual_bytes);
10627 actual_size = actual_bytes / sizeof *reply_data;
10628 reply_data = xcb_get_property_value (prop);
10630 else
10632 actual_size = 0;
10633 is_hidden = FRAME_ICONIFIED_P (f);
10635 #else
10636 x_catch_errors (dpy);
10637 rc = XGetWindowProperty (dpy, window, dpyinfo->Xatom_net_wm_state,
10638 0, max_len, False, target_type,
10639 &actual_type, &actual_format, &actual_size,
10640 &bytes_remaining, &tmp_data);
10642 if (rc == Success && actual_type == target_type && ! x_had_errors_p (dpy))
10643 reply_data = (Atom *) tmp_data;
10644 else
10646 actual_size = 0;
10647 is_hidden = FRAME_ICONIFIED_P (f);
10650 x_uncatch_errors ();
10651 #endif
10653 for (i = 0; i < actual_size; ++i)
10655 Atom a = reply_data[i];
10656 if (a == dpyinfo->Xatom_net_wm_state_hidden)
10657 is_hidden = true;
10658 else if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
10660 if (*size_state == FULLSCREEN_HEIGHT)
10661 *size_state = FULLSCREEN_MAXIMIZED;
10662 else
10663 *size_state = FULLSCREEN_WIDTH;
10665 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
10667 if (*size_state == FULLSCREEN_WIDTH)
10668 *size_state = FULLSCREEN_MAXIMIZED;
10669 else
10670 *size_state = FULLSCREEN_HEIGHT;
10672 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen)
10673 *size_state = FULLSCREEN_BOTH;
10674 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
10675 *sticky = true;
10678 #ifdef USE_XCB
10679 free (prop);
10680 #else
10681 if (tmp_data) XFree (tmp_data);
10682 #endif
10684 unblock_input ();
10685 return ! is_hidden;
10688 /* Do fullscreen as specified in extended window manager hints */
10690 static bool
10691 do_ewmh_fullscreen (struct frame *f)
10693 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10694 bool have_net_atom = x_wm_supports (f, dpyinfo->Xatom_net_wm_state);
10695 int cur;
10696 bool dummy;
10698 get_current_wm_state (f, FRAME_OUTER_WINDOW (f), &cur, &dummy);
10700 /* Some window managers don't say they support _NET_WM_STATE, but they do say
10701 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
10702 if (!have_net_atom)
10703 have_net_atom = x_wm_supports (f, dpyinfo->Xatom_net_wm_state_fullscreen);
10705 if (have_net_atom && cur != f->want_fullscreen)
10707 Lisp_Object frame;
10709 XSETFRAME (frame, f);
10711 /* Keep number of calls to set_wm_state as low as possible.
10712 Some window managers, or possible Gtk+, hangs when too many
10713 are sent at once. */
10714 switch (f->want_fullscreen)
10716 case FULLSCREEN_BOTH:
10717 if (cur != FULLSCREEN_BOTH)
10718 set_wm_state (frame, true, dpyinfo->Xatom_net_wm_state_fullscreen,
10719 None);
10720 break;
10721 case FULLSCREEN_WIDTH:
10722 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_MAXIMIZED)
10724 set_wm_state (frame, false,
10725 dpyinfo->Xatom_net_wm_state_maximized_horz,
10726 dpyinfo->Xatom_net_wm_state_maximized_vert);
10727 set_wm_state (frame, true,
10728 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10730 else
10732 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_HEIGHT
10733 || cur == FULLSCREEN_MAXIMIZED)
10734 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10735 dpyinfo->Xatom_net_wm_state_maximized_vert);
10736 if (cur != FULLSCREEN_MAXIMIZED || x_frame_normalize_before_maximize)
10737 set_wm_state (frame, true,
10738 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10740 break;
10741 case FULLSCREEN_HEIGHT:
10742 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_MAXIMIZED)
10744 set_wm_state (frame, false,
10745 dpyinfo->Xatom_net_wm_state_maximized_horz,
10746 dpyinfo->Xatom_net_wm_state_maximized_vert);
10747 set_wm_state (frame, true,
10748 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10750 else
10752 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_WIDTH
10753 || cur == FULLSCREEN_MAXIMIZED)
10754 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10755 dpyinfo->Xatom_net_wm_state_maximized_horz);
10756 if (cur != FULLSCREEN_MAXIMIZED || x_frame_normalize_before_maximize)
10757 set_wm_state (frame, true,
10758 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10760 break;
10761 case FULLSCREEN_MAXIMIZED:
10762 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_BOTH)
10764 set_wm_state (frame, false,
10765 dpyinfo->Xatom_net_wm_state_fullscreen, None);
10766 set_wm_state (frame, true,
10767 dpyinfo->Xatom_net_wm_state_maximized_horz,
10768 dpyinfo->Xatom_net_wm_state_maximized_vert);
10770 else if (x_frame_normalize_before_maximize && cur == FULLSCREEN_WIDTH)
10772 set_wm_state (frame, false,
10773 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10774 set_wm_state (frame, true,
10775 dpyinfo->Xatom_net_wm_state_maximized_horz,
10776 dpyinfo->Xatom_net_wm_state_maximized_vert);
10778 else if (x_frame_normalize_before_maximize && cur == FULLSCREEN_HEIGHT)
10780 set_wm_state (frame, false,
10781 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10782 set_wm_state (frame, true,
10783 dpyinfo->Xatom_net_wm_state_maximized_horz,
10784 dpyinfo->Xatom_net_wm_state_maximized_vert);
10786 else
10788 if (cur == FULLSCREEN_BOTH)
10789 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10790 None);
10791 else if (cur == FULLSCREEN_HEIGHT)
10792 set_wm_state (frame, true,
10793 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10794 else if (cur == FULLSCREEN_WIDTH)
10795 set_wm_state (frame, true, None,
10796 dpyinfo->Xatom_net_wm_state_maximized_vert);
10797 else
10798 set_wm_state (frame, true,
10799 dpyinfo->Xatom_net_wm_state_maximized_horz,
10800 dpyinfo->Xatom_net_wm_state_maximized_vert);
10802 break;
10803 case FULLSCREEN_NONE:
10804 if (cur == FULLSCREEN_BOTH)
10805 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10806 None);
10807 else
10808 set_wm_state (frame, false,
10809 dpyinfo->Xatom_net_wm_state_maximized_horz,
10810 dpyinfo->Xatom_net_wm_state_maximized_vert);
10813 f->want_fullscreen = FULLSCREEN_NONE;
10817 return have_net_atom;
10820 static void
10821 XTfullscreen_hook (struct frame *f)
10823 if (FRAME_VISIBLE_P (f))
10825 block_input ();
10826 x_check_fullscreen (f);
10827 x_sync (f);
10828 unblock_input ();
10833 static bool
10834 x_handle_net_wm_state (struct frame *f, const XPropertyEvent *event)
10836 int value = FULLSCREEN_NONE;
10837 Lisp_Object lval;
10838 bool sticky = false;
10839 bool not_hidden = get_current_wm_state (f, event->window, &value, &sticky);
10841 lval = Qnil;
10842 switch (value)
10844 case FULLSCREEN_WIDTH:
10845 lval = Qfullwidth;
10846 break;
10847 case FULLSCREEN_HEIGHT:
10848 lval = Qfullheight;
10849 break;
10850 case FULLSCREEN_BOTH:
10851 lval = Qfullboth;
10852 break;
10853 case FULLSCREEN_MAXIMIZED:
10854 lval = Qmaximized;
10855 break;
10858 frame_size_history_add
10859 (f, Qx_handle_net_wm_state, 0, 0,
10860 list2 (get_frame_param (f, Qfullscreen), lval));
10862 store_frame_param (f, Qfullscreen, lval);
10863 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
10865 return not_hidden;
10868 /* Check if we need to resize the frame due to a fullscreen request.
10869 If so needed, resize the frame. */
10870 static void
10871 x_check_fullscreen (struct frame *f)
10873 Lisp_Object lval = Qnil;
10875 if (do_ewmh_fullscreen (f))
10876 return;
10878 if (f->output_data.x->parent_desc != FRAME_DISPLAY_INFO (f)->root_window)
10879 return; /* Only fullscreen without WM or with EWM hints (above). */
10881 /* Setting fullscreen to nil doesn't do anything. We could save the
10882 last non-fullscreen size and restore it, but it seems like a
10883 lot of work for this unusual case (no window manager running). */
10885 if (f->want_fullscreen != FULLSCREEN_NONE)
10887 int width = FRAME_PIXEL_WIDTH (f), height = FRAME_PIXEL_HEIGHT (f);
10888 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10890 switch (f->want_fullscreen)
10892 /* No difference between these two when there is no WM */
10893 case FULLSCREEN_MAXIMIZED:
10894 lval = Qmaximized;
10895 width = x_display_pixel_width (dpyinfo);
10896 height = x_display_pixel_height (dpyinfo);
10897 break;
10898 case FULLSCREEN_BOTH:
10899 lval = Qfullboth;
10900 width = x_display_pixel_width (dpyinfo);
10901 height = x_display_pixel_height (dpyinfo);
10902 break;
10903 case FULLSCREEN_WIDTH:
10904 lval = Qfullwidth;
10905 width = x_display_pixel_width (dpyinfo);
10906 height = height + FRAME_MENUBAR_HEIGHT (f);
10907 break;
10908 case FULLSCREEN_HEIGHT:
10909 lval = Qfullheight;
10910 height = x_display_pixel_height (dpyinfo);
10911 break;
10912 default:
10913 emacs_abort ();
10916 frame_size_history_add
10917 (f, Qx_check_fullscreen, width, height, Qnil);
10919 x_wm_set_size_hint (f, 0, false);
10921 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10922 width, height);
10924 if (FRAME_VISIBLE_P (f))
10925 x_wait_for_event (f, ConfigureNotify);
10926 else
10928 change_frame_size (f, width, height - FRAME_MENUBAR_HEIGHT (f),
10929 false, true, false, true);
10930 x_sync (f);
10934 /* `x_net_wm_state' might have reset the fullscreen frame parameter,
10935 restore it. */
10936 store_frame_param (f, Qfullscreen, lval);
10939 /* This function is called by x_set_offset to determine whether the window
10940 manager interfered with the positioning of the frame. Type A window
10941 managers position the surrounding window manager decorations a small
10942 amount above and left of the user-supplied position. Type B window
10943 managers position the surrounding window manager decorations at the
10944 user-specified position. If we detect a Type A window manager, we
10945 compensate by moving the window right and down by the proper amount. */
10947 static void
10948 x_check_expected_move (struct frame *f, int expected_left, int expected_top)
10950 int current_left = 0, current_top = 0;
10952 /* x_real_positions returns the left and top offsets of the outermost
10953 window manager window around the frame. */
10955 x_real_positions (f, &current_left, &current_top);
10957 if (current_left != expected_left || current_top != expected_top)
10959 /* It's a "Type A" window manager. */
10961 int adjusted_left;
10962 int adjusted_top;
10964 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
10965 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
10966 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
10968 /* Now fix the mispositioned frame's location. */
10970 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
10971 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
10973 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10974 adjusted_left, adjusted_top);
10976 x_sync_with_move (f, expected_left, expected_top, false);
10978 else
10979 /* It's a "Type B" window manager. We don't have to adjust the
10980 frame's position. */
10982 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
10986 /* Wait for XGetGeometry to return up-to-date position information for a
10987 recently-moved frame. Call this immediately after calling XMoveWindow.
10988 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
10989 frame has been moved to, so we use a fuzzy position comparison instead
10990 of an exact comparison. */
10992 static void
10993 x_sync_with_move (struct frame *f, int left, int top, bool fuzzy)
10995 int count = 0;
10997 while (count++ < 50)
10999 int current_left = 0, current_top = 0;
11001 /* In theory, this call to XSync only needs to happen once, but in
11002 practice, it doesn't seem to work, hence the need for the surrounding
11003 loop. */
11005 XSync (FRAME_X_DISPLAY (f), False);
11006 x_real_positions (f, &current_left, &current_top);
11008 if (fuzzy)
11010 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
11011 pixels. */
11013 if (eabs (current_left - left) <= 10
11014 && eabs (current_top - top) <= 40)
11015 return;
11017 else if (current_left == left && current_top == top)
11018 return;
11021 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
11022 will then return up-to-date position info. */
11024 wait_reading_process_output (0, 500000000, 0, false, Qnil, NULL, 0);
11028 /* Wait for an event on frame F matching EVENTTYPE. */
11029 void
11030 x_wait_for_event (struct frame *f, int eventtype)
11032 if (!FLOATP (Vx_wait_for_event_timeout))
11033 return;
11035 int level = interrupt_input_blocked;
11036 fd_set fds;
11037 struct timespec tmo, tmo_at, time_now;
11038 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
11040 f->wait_event_type = eventtype;
11042 /* Default timeout is 0.1 second. Hopefully not noticeable. */
11043 double timeout = XFLOAT_DATA (Vx_wait_for_event_timeout);
11044 time_t timeout_seconds = (time_t) timeout;
11045 tmo = make_timespec
11046 (timeout_seconds, (long int) ((timeout - timeout_seconds)
11047 * 1000 * 1000 * 1000));
11048 tmo_at = timespec_add (current_timespec (), tmo);
11050 while (f->wait_event_type)
11052 pending_signals = true;
11053 totally_unblock_input ();
11054 /* XTread_socket is called after unblock. */
11055 block_input ();
11056 interrupt_input_blocked = level;
11058 FD_ZERO (&fds);
11059 FD_SET (fd, &fds);
11061 time_now = current_timespec ();
11062 if (timespec_cmp (tmo_at, time_now) < 0)
11063 break;
11065 tmo = timespec_sub (tmo_at, time_now);
11066 if (pselect (fd + 1, &fds, NULL, NULL, &tmo, NULL) == 0)
11067 break; /* Timeout */
11070 f->wait_event_type = 0;
11074 /* Change the size of frame F's X window to WIDTH/HEIGHT in the case F
11075 doesn't have a widget. If CHANGE_GRAVITY, change to
11076 top-left-corner window gravity for this size change and subsequent
11077 size changes. Otherwise leave the window gravity unchanged. */
11079 static void
11080 x_set_window_size_1 (struct frame *f, bool change_gravity,
11081 int width, int height)
11083 int pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
11084 int pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
11085 int old_width = FRAME_PIXEL_WIDTH (f);
11086 int old_height = FRAME_PIXEL_HEIGHT (f);
11087 Lisp_Object fullscreen = get_frame_param (f, Qfullscreen);
11089 if (change_gravity)
11090 f->win_gravity = NorthWestGravity;
11091 x_wm_set_size_hint (f, 0, false);
11093 /* When the frame is fullheight and we only want to change the width
11094 or it is fullwidth and we only want to change the height we should
11095 be able to preserve the fullscreen property. However, due to the
11096 fact that we have to send a resize request anyway, the window
11097 manager will abolish it. At least the respective size should
11098 remain unchanged but giving the frame back its normal size will
11099 be broken ... */
11100 if (EQ (fullscreen, Qfullwidth) && width == FRAME_TEXT_WIDTH (f))
11102 frame_size_history_add
11103 (f, Qx_set_window_size_1, width, height,
11104 list2 (make_number (old_height),
11105 make_number (pixelheight + FRAME_MENUBAR_HEIGHT (f))));
11107 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11108 old_width, pixelheight + FRAME_MENUBAR_HEIGHT (f));
11110 else if (EQ (fullscreen, Qfullheight) && height == FRAME_TEXT_HEIGHT (f))
11112 frame_size_history_add
11113 (f, Qx_set_window_size_2, width, height,
11114 list2 (make_number (old_width), make_number (pixelwidth)));
11116 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11117 pixelwidth, old_height);
11120 else
11122 frame_size_history_add
11123 (f, Qx_set_window_size_3, width, height,
11124 list3 (make_number (pixelwidth + FRAME_TOOLBAR_WIDTH (f)),
11125 make_number (pixelheight + FRAME_TOOLBAR_HEIGHT (f)
11126 + FRAME_MENUBAR_HEIGHT (f)),
11127 make_number (FRAME_MENUBAR_HEIGHT (f))));
11129 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11130 pixelwidth, pixelheight + FRAME_MENUBAR_HEIGHT (f));
11131 fullscreen = Qnil;
11136 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11137 receive in the ConfigureNotify event; if we get what we asked
11138 for, then the event won't cause the screen to become garbaged, so
11139 we have to make sure to do it here. */
11140 SET_FRAME_GARBAGED (f);
11142 /* Now, strictly speaking, we can't be sure that this is accurate,
11143 but the window manager will get around to dealing with the size
11144 change request eventually, and we'll hear how it went when the
11145 ConfigureNotify event gets here.
11147 We could just not bother storing any of this information here,
11148 and let the ConfigureNotify event set everything up, but that
11149 might be kind of confusing to the Lisp code, since size changes
11150 wouldn't be reported in the frame parameters until some random
11151 point in the future when the ConfigureNotify event arrives.
11153 Pass true for DELAY since we can't run Lisp code inside of
11154 a BLOCK_INPUT. */
11156 /* But the ConfigureNotify may in fact never arrive, and then this is
11157 not right if the frame is visible. Instead wait (with timeout)
11158 for the ConfigureNotify. */
11159 if (FRAME_VISIBLE_P (f))
11161 x_wait_for_event (f, ConfigureNotify);
11163 if (!NILP (fullscreen))
11164 /* Try to restore fullscreen state. */
11166 store_frame_param (f, Qfullscreen, fullscreen);
11167 x_set_fullscreen (f, fullscreen, fullscreen);
11170 else
11172 change_frame_size (f, width, height, false, true, false, true);
11173 x_sync (f);
11178 /* Call this to change the size of frame F's x-window.
11179 If CHANGE_GRAVITY, change to top-left-corner window gravity
11180 for this size change and subsequent size changes.
11181 Otherwise we leave the window gravity unchanged. */
11183 void
11184 x_set_window_size (struct frame *f, bool change_gravity,
11185 int width, int height, bool pixelwise)
11187 block_input ();
11189 /* The following breaks our calculations. If it's really needed,
11190 think of something else. */
11191 #if false
11192 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
11194 int text_width, text_height;
11196 /* When the frame is maximized/fullscreen or running under for
11197 example Xmonad, x_set_window_size_1 will be a no-op.
11198 In that case, the right thing to do is extend rows/width to
11199 the current frame size. We do that first if x_set_window_size_1
11200 turns out to not be a no-op (there is no way to know).
11201 The size will be adjusted again if the frame gets a
11202 ConfigureNotify event as a result of x_set_window_size. */
11203 int pixelh = FRAME_PIXEL_HEIGHT (f);
11204 #ifdef USE_X_TOOLKIT
11205 /* The menu bar is not part of text lines. The tool bar
11206 is however. */
11207 pixelh -= FRAME_MENUBAR_HEIGHT (f);
11208 #endif
11209 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, FRAME_PIXEL_WIDTH (f));
11210 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelh);
11212 change_frame_size (f, text_width, text_height, false, true, false, true);
11214 #endif
11216 /* Pixelize width and height, if necessary. */
11217 if (! pixelwise)
11219 width = width * FRAME_COLUMN_WIDTH (f);
11220 height = height * FRAME_LINE_HEIGHT (f);
11223 #ifdef USE_GTK
11224 if (FRAME_GTK_WIDGET (f))
11225 xg_frame_set_char_size (f, width, height);
11226 else
11227 x_set_window_size_1 (f, change_gravity, width, height);
11228 #else /* not USE_GTK */
11229 x_set_window_size_1 (f, change_gravity, width, height);
11230 x_clear_under_internal_border (f);
11231 #endif /* not USE_GTK */
11233 /* If cursor was outside the new size, mark it as off. */
11234 mark_window_cursors_off (XWINDOW (f->root_window));
11236 /* Clear out any recollection of where the mouse highlighting was,
11237 since it might be in a place that's outside the new frame size.
11238 Actually checking whether it is outside is a pain in the neck,
11239 so don't try--just let the highlighting be done afresh with new size. */
11240 cancel_mouse_face (f);
11242 unblock_input ();
11244 do_pending_window_change (false);
11247 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11249 void
11250 frame_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
11252 block_input ();
11254 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11255 0, 0, 0, 0, pix_x, pix_y);
11256 unblock_input ();
11259 /* Raise frame F. */
11261 void
11262 x_raise_frame (struct frame *f)
11264 block_input ();
11265 if (FRAME_VISIBLE_P (f))
11266 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
11267 XFlush (FRAME_X_DISPLAY (f));
11268 unblock_input ();
11271 /* Lower frame F. */
11273 static void
11274 x_lower_frame (struct frame *f)
11276 if (FRAME_VISIBLE_P (f))
11278 block_input ();
11279 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
11280 XFlush (FRAME_X_DISPLAY (f));
11281 unblock_input ();
11285 /* Request focus with XEmbed */
11287 void
11288 xembed_request_focus (struct frame *f)
11290 /* See XEmbed Protocol Specification at
11291 http://freedesktop.org/wiki/Specifications/xembed-spec */
11292 if (FRAME_VISIBLE_P (f))
11293 xembed_send_message (f, CurrentTime,
11294 XEMBED_REQUEST_FOCUS, 0, 0, 0);
11297 /* Activate frame with Extended Window Manager Hints */
11299 void
11300 x_ewmh_activate_frame (struct frame *f)
11302 /* See Window Manager Specification/Extended Window Manager Hints at
11303 http://freedesktop.org/wiki/Specifications/wm-spec */
11305 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11307 if (FRAME_VISIBLE_P (f) && x_wm_supports (f, dpyinfo->Xatom_net_active_window))
11309 Lisp_Object frame;
11310 XSETFRAME (frame, f);
11311 x_send_client_event (frame, make_number (0), frame,
11312 dpyinfo->Xatom_net_active_window,
11313 make_number (32),
11314 list2i (1, dpyinfo->last_user_time));
11318 static void
11319 XTframe_raise_lower (struct frame *f, bool raise_flag)
11321 if (raise_flag)
11322 x_raise_frame (f);
11323 else
11324 x_lower_frame (f);
11327 /* XEmbed implementation. */
11329 #if defined USE_X_TOOLKIT || ! defined USE_GTK
11331 /* XEmbed implementation. */
11333 #define XEMBED_VERSION 0
11335 static void
11336 xembed_set_info (struct frame *f, enum xembed_info flags)
11338 unsigned long data[2];
11339 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11341 data[0] = XEMBED_VERSION;
11342 data[1] = flags;
11344 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11345 dpyinfo->Xatom_XEMBED_INFO, dpyinfo->Xatom_XEMBED_INFO,
11346 32, PropModeReplace, (unsigned char *) data, 2);
11348 #endif /* defined USE_X_TOOLKIT || ! defined USE_GTK */
11350 static void
11351 xembed_send_message (struct frame *f, Time t, enum xembed_message msg,
11352 long int detail, long int data1, long int data2)
11354 XEvent event;
11356 event.xclient.type = ClientMessage;
11357 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
11358 event.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_XEMBED;
11359 event.xclient.format = 32;
11360 event.xclient.data.l[0] = t;
11361 event.xclient.data.l[1] = msg;
11362 event.xclient.data.l[2] = detail;
11363 event.xclient.data.l[3] = data1;
11364 event.xclient.data.l[4] = data2;
11366 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
11367 False, NoEventMask, &event);
11368 XSync (FRAME_X_DISPLAY (f), False);
11371 /* Change of visibility. */
11373 /* This tries to wait until the frame is really visible, depending on
11374 the value of Vx_wait_for_event_timeout.
11375 However, if the window manager asks the user where to position
11376 the frame, this will return before the user finishes doing that.
11377 The frame will not actually be visible at that time,
11378 but it will become visible later when the window manager
11379 finishes with it. */
11381 void
11382 x_make_frame_visible (struct frame *f)
11384 if (FRAME_PARENT_FRAME (f))
11386 if (!FRAME_VISIBLE_P (f))
11388 block_input ();
11389 #ifdef USE_GTK
11390 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
11391 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11392 f->left_pos, f->top_pos);
11393 #else
11394 XMapRaised (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
11395 #endif
11396 unblock_input ();
11398 SET_FRAME_VISIBLE (f, true);
11399 SET_FRAME_ICONIFIED (f, false);
11401 return;
11404 block_input ();
11406 x_set_bitmap_icon (f);
11408 if (! FRAME_VISIBLE_P (f))
11410 /* We test asked_for_visible here to make sure we don't
11411 call x_set_offset a second time
11412 if we get to x_make_frame_visible a second time
11413 before the window gets really visible. */
11414 if (! FRAME_ICONIFIED_P (f)
11415 && ! FRAME_X_EMBEDDED_P (f)
11416 && ! f->output_data.x->asked_for_visible)
11417 x_set_offset (f, f->left_pos, f->top_pos, 0);
11419 f->output_data.x->asked_for_visible = true;
11421 if (! EQ (Vx_no_window_manager, Qt))
11422 x_wm_set_window_state (f, NormalState);
11423 #ifdef USE_X_TOOLKIT
11424 if (FRAME_X_EMBEDDED_P (f))
11425 xembed_set_info (f, XEMBED_MAPPED);
11426 else
11428 /* This was XtPopup, but that did nothing for an iconified frame. */
11429 XtMapWidget (f->output_data.x->widget);
11431 #else /* not USE_X_TOOLKIT */
11432 #ifdef USE_GTK
11433 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
11434 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
11435 #else
11436 if (FRAME_X_EMBEDDED_P (f))
11437 xembed_set_info (f, XEMBED_MAPPED);
11438 else
11439 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11440 #endif /* not USE_GTK */
11441 #endif /* not USE_X_TOOLKIT */
11444 XFlush (FRAME_X_DISPLAY (f));
11446 /* Synchronize to ensure Emacs knows the frame is visible
11447 before we do anything else. We do this loop with input not blocked
11448 so that incoming events are handled. */
11450 Lisp_Object frame;
11451 /* This must be before UNBLOCK_INPUT
11452 since events that arrive in response to the actions above
11453 will set it when they are handled. */
11454 bool previously_visible = f->output_data.x->has_been_visible;
11456 XSETFRAME (frame, f);
11458 int original_left = f->left_pos;
11459 int original_top = f->top_pos;
11461 /* This must come after we set COUNT. */
11462 unblock_input ();
11464 /* We unblock here so that arriving X events are processed. */
11466 /* Now move the window back to where it was "supposed to be".
11467 But don't do it if the gravity is negative.
11468 When the gravity is negative, this uses a position
11469 that is 3 pixels too low. Perhaps that's really the border width.
11471 Don't do this if the window has never been visible before,
11472 because the window manager may choose the position
11473 and we don't want to override it. */
11475 if (!FRAME_VISIBLE_P (f)
11476 && !FRAME_ICONIFIED_P (f)
11477 && !FRAME_X_EMBEDDED_P (f)
11478 && !FRAME_PARENT_FRAME (f)
11479 && f->win_gravity == NorthWestGravity
11480 && previously_visible)
11482 Drawable rootw;
11483 int x, y;
11484 unsigned int width, height, border, depth;
11486 block_input ();
11488 /* On some window managers (such as FVWM) moving an existing
11489 window, even to the same place, causes the window manager
11490 to introduce an offset. This can cause the window to move
11491 to an unexpected location. Check the geometry (a little
11492 slow here) and then verify that the window is in the right
11493 place. If the window is not in the right place, move it
11494 there, and take the potential window manager hit. */
11495 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11496 &rootw, &x, &y, &width, &height, &border, &depth);
11498 if (original_left != x || original_top != y)
11499 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11500 original_left, original_top);
11502 unblock_input ();
11505 /* Try to wait for a MapNotify event (that is what tells us when a
11506 frame becomes visible). */
11507 x_wait_for_event (f, MapNotify);
11511 /* Change from mapped state to withdrawn state. */
11513 /* Make the frame visible (mapped and not iconified). */
11515 void
11516 x_make_frame_invisible (struct frame *f)
11518 Window window;
11520 /* Use the frame's outermost window, not the one we normally draw on. */
11521 window = FRAME_OUTER_WINDOW (f);
11523 /* Don't keep the highlight on an invisible frame. */
11524 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
11525 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
11527 block_input ();
11529 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11530 that the current position of the window is user-specified, rather than
11531 program-specified, so that when the window is mapped again, it will be
11532 placed at the same location, without forcing the user to position it
11533 by hand again (they have already done that once for this window.) */
11534 x_wm_set_size_hint (f, 0, true);
11536 #ifdef USE_GTK
11537 if (FRAME_GTK_OUTER_WIDGET (f))
11538 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
11539 else
11540 #else
11541 if (FRAME_X_EMBEDDED_P (f))
11542 xembed_set_info (f, 0);
11543 else
11544 #endif
11546 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11547 DefaultScreen (FRAME_X_DISPLAY (f))))
11549 unblock_input ();
11550 error ("Can't notify window manager of window withdrawal");
11553 x_sync (f);
11555 /* We can't distinguish this from iconification
11556 just by the event that we get from the server.
11557 So we can't win using the usual strategy of letting
11558 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11559 and synchronize with the server to make sure we agree. */
11560 SET_FRAME_VISIBLE (f, 0);
11561 SET_FRAME_ICONIFIED (f, false);
11563 unblock_input ();
11566 /* Change window state from mapped to iconified. */
11568 void
11569 x_iconify_frame (struct frame *f)
11571 #ifdef USE_X_TOOLKIT
11572 int result;
11573 #endif
11575 /* Don't keep the highlight on an invisible frame. */
11576 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
11577 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
11579 if (FRAME_ICONIFIED_P (f))
11580 return;
11582 block_input ();
11584 x_set_bitmap_icon (f);
11586 #if defined (USE_GTK)
11587 if (FRAME_GTK_OUTER_WIDGET (f))
11589 if (! FRAME_VISIBLE_P (f))
11590 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
11592 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
11593 SET_FRAME_VISIBLE (f, 0);
11594 SET_FRAME_ICONIFIED (f, true);
11595 unblock_input ();
11596 return;
11598 #endif
11600 #ifdef USE_X_TOOLKIT
11602 if (! FRAME_VISIBLE_P (f))
11604 if (! EQ (Vx_no_window_manager, Qt))
11605 x_wm_set_window_state (f, IconicState);
11606 /* This was XtPopup, but that did nothing for an iconified frame. */
11607 XtMapWidget (f->output_data.x->widget);
11608 /* The server won't give us any event to indicate
11609 that an invisible frame was changed to an icon,
11610 so we have to record it here. */
11611 SET_FRAME_VISIBLE (f, 0);
11612 SET_FRAME_ICONIFIED (f, true);
11613 unblock_input ();
11614 return;
11617 result = XIconifyWindow (FRAME_X_DISPLAY (f),
11618 XtWindow (f->output_data.x->widget),
11619 DefaultScreen (FRAME_X_DISPLAY (f)));
11620 unblock_input ();
11622 if (!result)
11623 error ("Can't notify window manager of iconification");
11625 SET_FRAME_ICONIFIED (f, true);
11626 SET_FRAME_VISIBLE (f, 0);
11628 block_input ();
11629 XFlush (FRAME_X_DISPLAY (f));
11630 unblock_input ();
11631 #else /* not USE_X_TOOLKIT */
11633 /* Make sure the X server knows where the window should be positioned,
11634 in case the user deiconifies with the window manager. */
11635 if (! FRAME_VISIBLE_P (f)
11636 && ! FRAME_ICONIFIED_P (f)
11637 && ! FRAME_X_EMBEDDED_P (f))
11638 x_set_offset (f, f->left_pos, f->top_pos, 0);
11640 /* Since we don't know which revision of X we're running, we'll use both
11641 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11643 /* X11R4: send a ClientMessage to the window manager using the
11644 WM_CHANGE_STATE type. */
11646 XEvent msg;
11648 msg.xclient.window = FRAME_X_WINDOW (f);
11649 msg.xclient.type = ClientMessage;
11650 msg.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_wm_change_state;
11651 msg.xclient.format = 32;
11652 msg.xclient.data.l[0] = IconicState;
11654 if (! XSendEvent (FRAME_X_DISPLAY (f),
11655 DefaultRootWindow (FRAME_X_DISPLAY (f)),
11656 False,
11657 SubstructureRedirectMask | SubstructureNotifyMask,
11658 &msg))
11660 unblock_input ();
11661 error ("Can't notify window manager of iconification");
11665 /* X11R3: set the initial_state field of the window manager hints to
11666 IconicState. */
11667 x_wm_set_window_state (f, IconicState);
11669 if (!FRAME_VISIBLE_P (f))
11671 /* If the frame was withdrawn, before, we must map it. */
11672 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11675 SET_FRAME_ICONIFIED (f, true);
11676 SET_FRAME_VISIBLE (f, 0);
11678 XFlush (FRAME_X_DISPLAY (f));
11679 unblock_input ();
11680 #endif /* not USE_X_TOOLKIT */
11684 /* Free X resources of frame F. */
11686 void
11687 x_free_frame_resources (struct frame *f)
11689 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11690 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
11691 #ifdef USE_X_TOOLKIT
11692 Lisp_Object bar;
11693 struct scroll_bar *b;
11694 #endif
11696 block_input ();
11698 /* If a display connection is dead, don't try sending more
11699 commands to the X server. */
11700 if (dpyinfo->display)
11702 /* Always exit with visible pointer to avoid weird issue
11703 with Xfixes (Bug#17609). */
11704 if (f->pointer_invisible)
11705 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, 0);
11707 /* We must free faces before destroying windows because some
11708 font-driver (e.g. xft) access a window while finishing a
11709 face. */
11710 free_frame_faces (f);
11711 tear_down_x_back_buffer (f);
11713 if (f->output_data.x->icon_desc)
11714 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
11716 #ifdef USE_X_TOOLKIT
11717 /* Explicitly destroy the scroll bars of the frame. Without
11718 this, we get "BadDrawable" errors from the toolkit later on,
11719 presumably from expose events generated for the disappearing
11720 toolkit scroll bars. */
11721 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
11723 b = XSCROLL_BAR (bar);
11724 x_scroll_bar_remove (b);
11726 #endif
11728 #ifdef HAVE_X_I18N
11729 if (FRAME_XIC (f))
11730 free_frame_xic (f);
11731 #endif
11733 x_free_cr_resources (f);
11734 #ifdef USE_X_TOOLKIT
11735 if (f->output_data.x->widget)
11737 XtDestroyWidget (f->output_data.x->widget);
11738 f->output_data.x->widget = NULL;
11740 /* Tooltips don't have widgets, only a simple X window, even if
11741 we are using a toolkit. */
11742 else if (FRAME_X_WINDOW (f))
11743 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11745 free_frame_menubar (f);
11747 if (f->shell_position)
11748 xfree (f->shell_position);
11749 #else /* !USE_X_TOOLKIT */
11751 #ifdef USE_GTK
11752 xg_free_frame_widgets (f);
11753 #endif /* USE_GTK */
11755 tear_down_x_back_buffer (f);
11756 if (FRAME_X_WINDOW (f))
11757 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11758 #endif /* !USE_X_TOOLKIT */
11760 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
11761 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
11762 unload_color (f, f->output_data.x->cursor_pixel);
11763 unload_color (f, f->output_data.x->cursor_foreground_pixel);
11764 unload_color (f, f->output_data.x->border_pixel);
11765 unload_color (f, f->output_data.x->mouse_pixel);
11767 if (f->output_data.x->scroll_bar_background_pixel != -1)
11768 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
11769 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
11770 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
11771 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
11772 /* Scrollbar shadow colors. */
11773 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
11774 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
11775 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
11776 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
11777 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
11778 if (f->output_data.x->white_relief.pixel != -1)
11779 unload_color (f, f->output_data.x->white_relief.pixel);
11780 if (f->output_data.x->black_relief.pixel != -1)
11781 unload_color (f, f->output_data.x->black_relief.pixel);
11783 x_free_gcs (f);
11785 /* Free extra GCs allocated by x_setup_relief_colors. */
11786 if (f->output_data.x->white_relief.gc)
11788 XFreeGC (dpyinfo->display, f->output_data.x->white_relief.gc);
11789 f->output_data.x->white_relief.gc = 0;
11791 if (f->output_data.x->black_relief.gc)
11793 XFreeGC (dpyinfo->display, f->output_data.x->black_relief.gc);
11794 f->output_data.x->black_relief.gc = 0;
11797 /* Free cursors. */
11798 if (f->output_data.x->text_cursor != 0)
11799 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->text_cursor);
11800 if (f->output_data.x->nontext_cursor != 0)
11801 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->nontext_cursor);
11802 if (f->output_data.x->modeline_cursor != 0)
11803 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->modeline_cursor);
11804 if (f->output_data.x->hand_cursor != 0)
11805 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hand_cursor);
11806 if (f->output_data.x->hourglass_cursor != 0)
11807 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hourglass_cursor);
11808 if (f->output_data.x->horizontal_drag_cursor != 0)
11809 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->horizontal_drag_cursor);
11810 if (f->output_data.x->vertical_drag_cursor != 0)
11811 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->vertical_drag_cursor);
11812 if (f->output_data.x->left_edge_cursor != 0)
11813 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->left_edge_cursor);
11814 if (f->output_data.x->top_left_corner_cursor != 0)
11815 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->top_left_corner_cursor);
11816 if (f->output_data.x->top_edge_cursor != 0)
11817 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->top_edge_cursor);
11818 if (f->output_data.x->top_right_corner_cursor != 0)
11819 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->top_right_corner_cursor);
11820 if (f->output_data.x->right_edge_cursor != 0)
11821 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->right_edge_cursor);
11822 if (f->output_data.x->bottom_right_corner_cursor != 0)
11823 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->bottom_right_corner_cursor);
11824 if (f->output_data.x->bottom_edge_cursor != 0)
11825 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->bottom_edge_cursor);
11826 if (f->output_data.x->bottom_left_corner_cursor != 0)
11827 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->bottom_left_corner_cursor);
11829 XFlush (FRAME_X_DISPLAY (f));
11832 xfree (f->output_data.x->saved_menu_event);
11833 xfree (f->output_data.x);
11834 f->output_data.x = NULL;
11836 if (f == dpyinfo->x_focus_frame)
11837 dpyinfo->x_focus_frame = 0;
11838 if (f == dpyinfo->x_focus_event_frame)
11839 dpyinfo->x_focus_event_frame = 0;
11840 if (f == dpyinfo->x_highlight_frame)
11841 dpyinfo->x_highlight_frame = 0;
11842 if (f == hlinfo->mouse_face_mouse_frame)
11843 reset_mouse_highlight (hlinfo);
11845 unblock_input ();
11849 /* Destroy the X window of frame F. */
11851 static void
11852 x_destroy_window (struct frame *f)
11854 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11856 /* If a display connection is dead, don't try sending more
11857 commands to the X server. */
11858 if (dpyinfo->display != 0)
11859 x_free_frame_resources (f);
11861 dpyinfo->reference_count--;
11865 /* Setting window manager hints. */
11867 /* Set the normal size hints for the window manager, for frame F.
11868 FLAGS is the flags word to use--or 0 meaning preserve the flags
11869 that the window now has.
11870 If USER_POSITION, set the USPosition
11871 flag (this is useful when FLAGS is 0).
11872 The GTK version is in gtkutils.c. */
11874 #ifndef USE_GTK
11875 void
11876 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
11878 XSizeHints size_hints;
11879 Window window = FRAME_OUTER_WINDOW (f);
11881 if (!window)
11882 return;
11884 #ifdef USE_X_TOOLKIT
11885 if (f->output_data.x->widget)
11887 widget_update_wm_size_hints (f->output_data.x->widget);
11888 return;
11890 #endif
11892 /* Setting PMaxSize caused various problems. */
11893 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
11895 size_hints.x = f->left_pos;
11896 size_hints.y = f->top_pos;
11898 size_hints.width = FRAME_PIXEL_WIDTH (f);
11899 size_hints.height = FRAME_PIXEL_HEIGHT (f);
11901 size_hints.width_inc = frame_resize_pixelwise ? 1 : FRAME_COLUMN_WIDTH (f);
11902 size_hints.height_inc = frame_resize_pixelwise ? 1 : FRAME_LINE_HEIGHT (f);
11904 size_hints.max_width = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
11905 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
11906 size_hints.max_height = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
11907 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
11909 /* Calculate the base and minimum sizes. */
11911 int base_width, base_height;
11913 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
11914 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
11916 /* The window manager uses the base width hints to calculate the
11917 current number of rows and columns in the frame while
11918 resizing; min_width and min_height aren't useful for this
11919 purpose, since they might not give the dimensions for a
11920 zero-row, zero-column frame. */
11922 size_hints.flags |= PBaseSize;
11923 size_hints.base_width = base_width;
11924 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
11925 size_hints.min_width = base_width;
11926 size_hints.min_height = base_height;
11929 /* If we don't need the old flags, we don't need the old hint at all. */
11930 if (flags)
11932 size_hints.flags |= flags;
11933 goto no_read;
11937 XSizeHints hints; /* Sometimes I hate X Windows... */
11938 long supplied_return;
11939 int value;
11941 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
11942 &supplied_return);
11944 if (flags)
11945 size_hints.flags |= flags;
11946 else
11948 if (value == 0)
11949 hints.flags = 0;
11950 if (hints.flags & PSize)
11951 size_hints.flags |= PSize;
11952 if (hints.flags & PPosition)
11953 size_hints.flags |= PPosition;
11954 if (hints.flags & USPosition)
11955 size_hints.flags |= USPosition;
11956 if (hints.flags & USSize)
11957 size_hints.flags |= USSize;
11961 no_read:
11963 #ifdef PWinGravity
11964 size_hints.win_gravity = f->win_gravity;
11965 size_hints.flags |= PWinGravity;
11967 if (user_position)
11969 size_hints.flags &= ~ PPosition;
11970 size_hints.flags |= USPosition;
11972 #endif /* PWinGravity */
11974 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
11976 #endif /* not USE_GTK */
11978 /* Used for IconicState or NormalState */
11980 static void
11981 x_wm_set_window_state (struct frame *f, int state)
11983 #ifdef USE_X_TOOLKIT
11984 Arg al[1];
11986 XtSetArg (al[0], XtNinitialState, state);
11987 XtSetValues (f->output_data.x->widget, al, 1);
11988 #else /* not USE_X_TOOLKIT */
11989 Window window = FRAME_X_WINDOW (f);
11991 f->output_data.x->wm_hints.flags |= StateHint;
11992 f->output_data.x->wm_hints.initial_state = state;
11994 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
11995 #endif /* not USE_X_TOOLKIT */
11998 static void
11999 x_wm_set_icon_pixmap (struct frame *f, ptrdiff_t pixmap_id)
12001 Pixmap icon_pixmap, icon_mask;
12003 #if !defined USE_X_TOOLKIT && !defined USE_GTK
12004 Window window = FRAME_OUTER_WINDOW (f);
12005 #endif
12007 if (pixmap_id > 0)
12009 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
12010 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
12011 icon_mask = x_bitmap_mask (f, pixmap_id);
12012 f->output_data.x->wm_hints.icon_mask = icon_mask;
12014 else
12016 /* It seems there is no way to turn off use of an icon
12017 pixmap. */
12018 return;
12022 #ifdef USE_GTK
12024 xg_set_frame_icon (f, icon_pixmap, icon_mask);
12025 return;
12028 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
12031 Arg al[1];
12032 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12033 XtSetValues (f->output_data.x->widget, al, 1);
12034 XtSetArg (al[0], XtNiconMask, icon_mask);
12035 XtSetValues (f->output_data.x->widget, al, 1);
12038 #else /* not USE_X_TOOLKIT && not USE_GTK */
12040 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
12041 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12043 #endif /* not USE_X_TOOLKIT && not USE_GTK */
12046 void
12047 x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
12049 Window window = FRAME_OUTER_WINDOW (f);
12051 f->output_data.x->wm_hints.flags |= IconPositionHint;
12052 f->output_data.x->wm_hints.icon_x = icon_x;
12053 f->output_data.x->wm_hints.icon_y = icon_y;
12055 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12059 /***********************************************************************
12060 Fonts
12061 ***********************************************************************/
12063 #ifdef GLYPH_DEBUG
12065 /* Check that FONT is valid on frame F. It is if it can be found in F's
12066 font table. */
12068 static void
12069 x_check_font (struct frame *f, struct font *font)
12071 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
12072 if (font->driver->check)
12073 eassert (font->driver->check (f, font) == 0);
12076 #endif /* GLYPH_DEBUG */
12079 /***********************************************************************
12080 Initialization
12081 ***********************************************************************/
12083 #ifdef USE_X_TOOLKIT
12084 static XrmOptionDescRec emacs_options[] = {
12085 {(char *) "-geometry", (char *) ".geometry", XrmoptionSepArg, NULL},
12086 {(char *) "-iconic", (char *) ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
12088 {(char *) "-internal-border-width",
12089 (char *) "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
12090 {(char *) "-ib", (char *) "*EmacsScreen.internalBorderWidth",
12091 XrmoptionSepArg, NULL},
12092 {(char *) "-T", (char *) "*EmacsShell.title", XrmoptionSepArg, NULL},
12093 {(char *) "-wn", (char *) "*EmacsShell.title", XrmoptionSepArg, NULL},
12094 {(char *) "-title", (char *) "*EmacsShell.title", XrmoptionSepArg, NULL},
12095 {(char *) "-iconname", (char *) "*EmacsShell.iconName",
12096 XrmoptionSepArg, NULL},
12097 {(char *) "-in", (char *) "*EmacsShell.iconName", XrmoptionSepArg, NULL},
12098 {(char *) "-mc", (char *) "*pointerColor", XrmoptionSepArg, NULL},
12099 {(char *) "-cr", (char *) "*cursorColor", XrmoptionSepArg, NULL}
12102 /* Whether atimer for Xt timeouts is activated or not. */
12104 static bool x_timeout_atimer_activated_flag;
12106 #endif /* USE_X_TOOLKIT */
12108 static int x_initialized;
12110 /* Test whether two display-name strings agree up to the dot that separates
12111 the screen number from the server number. */
12112 static bool
12113 same_x_server (const char *name1, const char *name2)
12115 bool seen_colon = false;
12116 Lisp_Object sysname = Fsystem_name ();
12117 const char *system_name = SSDATA (sysname);
12118 ptrdiff_t system_name_length = SBYTES (sysname);
12119 ptrdiff_t length_until_period = 0;
12121 while (system_name[length_until_period] != 0
12122 && system_name[length_until_period] != '.')
12123 length_until_period++;
12125 /* Treat `unix' like an empty host name. */
12126 if (! strncmp (name1, "unix:", 5))
12127 name1 += 4;
12128 if (! strncmp (name2, "unix:", 5))
12129 name2 += 4;
12130 /* Treat this host's name like an empty host name. */
12131 if (! strncmp (name1, system_name, system_name_length)
12132 && name1[system_name_length] == ':')
12133 name1 += system_name_length;
12134 if (! strncmp (name2, system_name, system_name_length)
12135 && name2[system_name_length] == ':')
12136 name2 += system_name_length;
12137 /* Treat this host's domainless name like an empty host name. */
12138 if (! strncmp (name1, system_name, length_until_period)
12139 && name1[length_until_period] == ':')
12140 name1 += length_until_period;
12141 if (! strncmp (name2, system_name, length_until_period)
12142 && name2[length_until_period] == ':')
12143 name2 += length_until_period;
12145 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
12147 if (*name1 == ':')
12148 seen_colon = true;
12149 if (seen_colon && *name1 == '.')
12150 return true;
12152 return (seen_colon
12153 && (*name1 == '.' || *name1 == '\0')
12154 && (*name2 == '.' || *name2 == '\0'));
12157 /* Count number of set bits in mask and number of bits to shift to
12158 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
12159 to 5. */
12160 static void
12161 get_bits_and_offset (unsigned long mask, int *bits, int *offset)
12163 int nr = 0;
12164 int off = 0;
12166 while (!(mask & 1))
12168 off++;
12169 mask >>= 1;
12172 while (mask & 1)
12174 nr++;
12175 mask >>= 1;
12178 *offset = off;
12179 *bits = nr;
12182 /* Return true iff display DISPLAY is available for use.
12183 But don't permanently open it, just test its availability. */
12185 bool
12186 x_display_ok (const char *display)
12188 /* XOpenDisplay fails if it gets a signal. Block SIGIO which may arrive. */
12189 unrequest_sigio ();
12190 Display *dpy = XOpenDisplay (display);
12191 request_sigio ();
12192 if (!dpy)
12193 return false;
12194 XCloseDisplay (dpy);
12195 return true;
12198 #ifdef USE_GTK
12199 static void
12200 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
12201 const gchar *msg, gpointer user_data)
12203 if (!strstr (msg, "g_set_prgname"))
12204 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, msg);
12206 #endif
12208 /* Create invisible cursor on X display referred by DPYINFO. */
12210 static Cursor
12211 make_invisible_cursor (struct x_display_info *dpyinfo)
12213 Display *dpy = dpyinfo->display;
12214 static char const no_data[] = { 0 };
12215 Pixmap pix;
12216 XColor col;
12217 Cursor c = 0;
12219 x_catch_errors (dpy);
12220 pix = XCreateBitmapFromData (dpy, dpyinfo->root_window, no_data, 1, 1);
12221 if (! x_had_errors_p (dpy) && pix != None)
12223 Cursor pixc;
12224 col.pixel = 0;
12225 col.red = col.green = col.blue = 0;
12226 col.flags = DoRed | DoGreen | DoBlue;
12227 pixc = XCreatePixmapCursor (dpy, pix, pix, &col, &col, 0, 0);
12228 if (! x_had_errors_p (dpy) && pixc != None)
12229 c = pixc;
12230 XFreePixmap (dpy, pix);
12233 x_uncatch_errors ();
12235 return c;
12238 /* True if DPY supports Xfixes extension >= 4. */
12240 static bool
12241 x_probe_xfixes_extension (Display *dpy)
12243 #ifdef HAVE_XFIXES
12244 int major, minor;
12245 return XFixesQueryVersion (dpy, &major, &minor) && major >= 4;
12246 #else
12247 return false;
12248 #endif /* HAVE_XFIXES */
12251 /* Toggle mouse pointer visibility on frame F by using Xfixes functions. */
12253 static void
12254 xfixes_toggle_visible_pointer (struct frame *f, bool invisible)
12256 #ifdef HAVE_XFIXES
12257 if (invisible)
12258 XFixesHideCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12259 else
12260 XFixesShowCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12261 f->pointer_invisible = invisible;
12262 #else
12263 emacs_abort ();
12264 #endif /* HAVE_XFIXES */
12267 /* Toggle mouse pointer visibility on frame F by using invisible cursor. */
12269 static void
12270 x_toggle_visible_pointer (struct frame *f, bool invisible)
12272 eassert (FRAME_DISPLAY_INFO (f)->invisible_cursor != 0);
12273 if (invisible)
12274 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12275 FRAME_DISPLAY_INFO (f)->invisible_cursor);
12276 else
12277 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12278 f->output_data.x->current_cursor);
12279 f->pointer_invisible = invisible;
12282 /* Setup pointer blanking, prefer Xfixes if available. */
12284 static void
12285 x_setup_pointer_blanking (struct x_display_info *dpyinfo)
12287 /* FIXME: the brave tester should set EMACS_XFIXES because we're suspecting
12288 X server bug, see https://debbugs.gnu.org/cgi/bugreport.cgi?bug=17609. */
12289 if (egetenv ("EMACS_XFIXES") && x_probe_xfixes_extension (dpyinfo->display))
12290 dpyinfo->toggle_visible_pointer = xfixes_toggle_visible_pointer;
12291 else
12293 dpyinfo->toggle_visible_pointer = x_toggle_visible_pointer;
12294 dpyinfo->invisible_cursor = make_invisible_cursor (dpyinfo);
12298 /* Current X display connection identifier. Incremented for each next
12299 connection established. */
12300 static unsigned x_display_id;
12302 /* Open a connection to X display DISPLAY_NAME, and return
12303 the structure that describes the open display.
12304 If we cannot contact the display, return null. */
12306 struct x_display_info *
12307 x_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
12309 Display *dpy;
12310 struct terminal *terminal;
12311 struct x_display_info *dpyinfo;
12312 XrmDatabase xrdb;
12313 #ifdef USE_XCB
12314 xcb_connection_t *xcb_conn;
12315 #endif
12317 block_input ();
12319 if (!x_initialized)
12321 x_initialize ();
12322 ++x_initialized;
12325 if (! x_display_ok (SSDATA (display_name)))
12326 error ("Display %s can't be opened", SSDATA (display_name));
12328 #ifdef USE_GTK
12330 #define NUM_ARGV 10
12331 int argc;
12332 char *argv[NUM_ARGV];
12333 char **argv2 = argv;
12334 guint id;
12336 if (x_initialized++ > 1)
12338 xg_display_open (SSDATA (display_name), &dpy);
12340 else
12342 static char display_opt[] = "--display";
12343 static char name_opt[] = "--name";
12345 for (argc = 0; argc < NUM_ARGV; ++argc)
12346 argv[argc] = 0;
12348 argc = 0;
12349 argv[argc++] = initial_argv[0];
12351 if (! NILP (display_name))
12353 argv[argc++] = display_opt;
12354 argv[argc++] = SSDATA (display_name);
12357 argv[argc++] = name_opt;
12358 argv[argc++] = resource_name;
12360 XSetLocaleModifiers ("");
12362 /* Work around GLib bug that outputs a faulty warning. See
12363 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
12364 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
12365 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
12367 /* NULL window -> events for all windows go to our function.
12368 Call before gtk_init so Gtk+ event filters comes after our. */
12369 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
12371 /* gtk_init does set_locale. Fix locale before and after. */
12372 fixup_locale ();
12373 unrequest_sigio (); /* See comment in x_display_ok. */
12374 gtk_init (&argc, &argv2);
12375 request_sigio ();
12376 fixup_locale ();
12378 g_log_remove_handler ("GLib", id);
12380 xg_initialize ();
12382 dpy = DEFAULT_GDK_DISPLAY ();
12384 #if ! GTK_CHECK_VERSION (2, 90, 0)
12385 /* Load our own gtkrc if it exists. */
12387 const char *file = "~/.emacs.d/gtkrc";
12388 Lisp_Object s, abs_file;
12390 s = build_string (file);
12391 abs_file = Fexpand_file_name (s, Qnil);
12393 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
12394 gtk_rc_parse (SSDATA (abs_file));
12396 #endif
12398 XSetErrorHandler (x_error_handler);
12399 XSetIOErrorHandler (x_io_error_quitter);
12402 #else /* not USE_GTK */
12403 #ifdef USE_X_TOOLKIT
12404 /* weiner@footloose.sps.mot.com reports that this causes
12405 errors with X11R5:
12406 X protocol error: BadAtom (invalid Atom parameter)
12407 on protocol request 18skiloaf.
12408 So let's not use it until R6. */
12409 #ifdef HAVE_X11XTR6
12410 XtSetLanguageProc (NULL, NULL, NULL);
12411 #endif
12414 int argc = 0;
12415 char *argv[3];
12417 argv[0] = (char *) "";
12418 argc = 1;
12419 if (xrm_option)
12421 argv[argc++] = (char *) "-xrm";
12422 argv[argc++] = xrm_option;
12424 turn_on_atimers (false);
12425 unrequest_sigio (); /* See comment in x_display_ok. */
12426 dpy = XtOpenDisplay (Xt_app_con, SSDATA (display_name),
12427 resource_name, EMACS_CLASS,
12428 emacs_options, XtNumber (emacs_options),
12429 &argc, argv);
12430 request_sigio ();
12431 turn_on_atimers (true);
12433 #ifdef HAVE_X11XTR6
12434 /* I think this is to compensate for XtSetLanguageProc. */
12435 fixup_locale ();
12436 #endif
12439 #else /* not USE_X_TOOLKIT */
12440 XSetLocaleModifiers ("");
12441 unrequest_sigio (); /* See comment in x_display_ok. */
12442 dpy = XOpenDisplay (SSDATA (display_name));
12443 request_sigio ();
12444 #endif /* not USE_X_TOOLKIT */
12445 #endif /* not USE_GTK*/
12447 /* Detect failure. */
12448 if (dpy == 0)
12450 unblock_input ();
12451 return 0;
12454 #ifdef USE_XCB
12455 xcb_conn = XGetXCBConnection (dpy);
12456 if (xcb_conn == 0)
12458 #ifdef USE_GTK
12459 xg_display_close (dpy);
12460 #else
12461 #ifdef USE_X_TOOLKIT
12462 XtCloseDisplay (dpy);
12463 #else
12464 XCloseDisplay (dpy);
12465 #endif
12466 #endif /* ! USE_GTK */
12468 unblock_input ();
12469 return 0;
12471 #endif
12473 /* We have definitely succeeded. Record the new connection. */
12475 dpyinfo = xzalloc (sizeof *dpyinfo);
12476 terminal = x_create_terminal (dpyinfo);
12479 struct x_display_info *share;
12481 for (share = x_display_list; share; share = share->next)
12482 if (same_x_server (SSDATA (XCAR (share->name_list_element)),
12483 SSDATA (display_name)))
12484 break;
12485 if (share)
12486 terminal->kboard = share->terminal->kboard;
12487 else
12489 terminal->kboard = allocate_kboard (Qx);
12491 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
12493 char *vendor = ServerVendor (dpy);
12495 /* Temporarily hide the partially initialized terminal. */
12496 terminal_list = terminal->next_terminal;
12497 unblock_input ();
12498 kset_system_key_alist
12499 (terminal->kboard,
12500 call1 (Qvendor_specific_keysyms,
12501 vendor ? build_string (vendor) : empty_unibyte_string));
12502 block_input ();
12503 terminal->next_terminal = terminal_list;
12504 terminal_list = terminal;
12507 /* Don't let the initial kboard remain current longer than necessary.
12508 That would cause problems if a file loaded on startup tries to
12509 prompt in the mini-buffer. */
12510 if (current_kboard == initial_kboard)
12511 current_kboard = terminal->kboard;
12513 terminal->kboard->reference_count++;
12516 /* Put this display on the chain. */
12517 dpyinfo->next = x_display_list;
12518 x_display_list = dpyinfo;
12520 dpyinfo->name_list_element = Fcons (display_name, Qnil);
12521 dpyinfo->display = dpy;
12522 dpyinfo->connection = ConnectionNumber (dpyinfo->display);
12523 #ifdef USE_XCB
12524 dpyinfo->xcb_connection = xcb_conn;
12525 #endif
12527 /* https://lists.gnu.org/archive/html/emacs-devel/2015-11/msg00194.html */
12528 dpyinfo->smallest_font_height = 1;
12529 dpyinfo->smallest_char_width = 1;
12531 /* Set the name of the terminal. */
12532 terminal->name = xlispstrdup (display_name);
12534 #if false
12535 XSetAfterFunction (x_current_display, x_trace_wire);
12536 #endif
12538 Lisp_Object system_name = Fsystem_name ();
12539 ptrdiff_t nbytes;
12540 if (INT_ADD_WRAPV (SBYTES (Vinvocation_name), SBYTES (system_name) + 2,
12541 &nbytes))
12542 memory_full (SIZE_MAX);
12543 dpyinfo->x_id = ++x_display_id;
12544 dpyinfo->x_id_name = xmalloc (nbytes);
12545 char *nametail = lispstpcpy (dpyinfo->x_id_name, Vinvocation_name);
12546 *nametail++ = '@';
12547 lispstpcpy (nametail, system_name);
12549 /* Figure out which modifier bits mean what. */
12550 x_find_modifier_meanings (dpyinfo);
12552 /* Get the scroll bar cursor. */
12553 #ifdef USE_GTK
12554 /* We must create a GTK cursor, it is required for GTK widgets. */
12555 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
12556 #endif /* USE_GTK */
12558 dpyinfo->vertical_scroll_bar_cursor
12559 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
12561 dpyinfo->horizontal_scroll_bar_cursor
12562 = XCreateFontCursor (dpyinfo->display, XC_sb_h_double_arrow);
12564 xrdb = x_load_resources (dpyinfo->display, xrm_option,
12565 resource_name, EMACS_CLASS);
12566 #ifdef HAVE_XRMSETDATABASE
12567 XrmSetDatabase (dpyinfo->display, xrdb);
12568 #else
12569 dpyinfo->display->db = xrdb;
12570 #endif
12571 /* Put the rdb where we can find it in a way that works on
12572 all versions. */
12573 dpyinfo->xrdb = xrdb;
12575 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
12576 DefaultScreen (dpyinfo->display));
12577 select_visual (dpyinfo);
12578 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
12579 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
12580 dpyinfo->icon_bitmap_id = -1;
12581 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
12583 reset_mouse_highlight (&dpyinfo->mouse_highlight);
12585 /* See if we can construct pixel values from RGB values. */
12586 if (dpyinfo->visual->class == TrueColor)
12588 get_bits_and_offset (dpyinfo->visual->red_mask,
12589 &dpyinfo->red_bits, &dpyinfo->red_offset);
12590 get_bits_and_offset (dpyinfo->visual->blue_mask,
12591 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
12592 get_bits_and_offset (dpyinfo->visual->green_mask,
12593 &dpyinfo->green_bits, &dpyinfo->green_offset);
12596 /* See if a private colormap is requested. */
12597 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
12599 if (dpyinfo->visual->class == PseudoColor)
12601 AUTO_STRING (privateColormap, "privateColormap");
12602 AUTO_STRING (PrivateColormap, "PrivateColormap");
12603 Lisp_Object value
12604 = display_x_get_resource (dpyinfo, privateColormap,
12605 PrivateColormap, Qnil, Qnil);
12606 if (STRINGP (value)
12607 && (!strcmp (SSDATA (value), "true")
12608 || !strcmp (SSDATA (value), "on")))
12609 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
12612 else
12613 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
12614 dpyinfo->visual, AllocNone);
12616 #ifdef HAVE_XDBE
12617 dpyinfo->supports_xdbe = false;
12618 int xdbe_major;
12619 int xdbe_minor;
12620 if (XdbeQueryExtension (dpyinfo->display, &xdbe_major, &xdbe_minor))
12621 dpyinfo->supports_xdbe = true;
12622 #endif
12624 #ifdef HAVE_XFT
12626 /* If we are using Xft, the following precautions should be made:
12628 1. Make sure that the Xrender extension is added before the Xft one.
12629 Otherwise, the close-display hook set by Xft is called after the one
12630 for Xrender, and the former tries to re-add the latter. This results
12631 in inconsistency of internal states and leads to X protocol error when
12632 one reconnects to the same X server (Bug#1696).
12634 2. Check dpi value in X resources. It is better we use it as well,
12635 since Xft will use it, as will all Gnome applications. If our real DPI
12636 is smaller or larger than the one Xft uses, our font will look smaller
12637 or larger than other for other applications, even if it is the same
12638 font name (monospace-10 for example). */
12640 int event_base, error_base;
12641 char *v;
12642 double d;
12644 XRenderQueryExtension (dpyinfo->display, &event_base, &error_base);
12646 v = XGetDefault (dpyinfo->display, "Xft", "dpi");
12647 if (v != NULL && sscanf (v, "%lf", &d) == 1)
12648 dpyinfo->resy = dpyinfo->resx = d;
12650 #endif
12652 if (dpyinfo->resy < 1)
12654 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
12655 double pixels = DisplayHeight (dpyinfo->display, screen_number);
12656 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
12657 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
12658 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
12659 pixels = DisplayWidth (dpyinfo->display, screen_number);
12660 mm = DisplayWidthMM (dpyinfo->display, screen_number);
12661 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
12662 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
12666 static const struct
12668 const char *name;
12669 int offset;
12670 } atom_refs[] = {
12671 #define ATOM_REFS_INIT(string, member) \
12672 { string, offsetof (struct x_display_info, member) },
12673 ATOM_REFS_INIT ("WM_PROTOCOLS", Xatom_wm_protocols)
12674 ATOM_REFS_INIT ("WM_TAKE_FOCUS", Xatom_wm_take_focus)
12675 ATOM_REFS_INIT ("WM_SAVE_YOURSELF", Xatom_wm_save_yourself)
12676 ATOM_REFS_INIT ("WM_DELETE_WINDOW", Xatom_wm_delete_window)
12677 ATOM_REFS_INIT ("WM_CHANGE_STATE", Xatom_wm_change_state)
12678 ATOM_REFS_INIT ("WM_CONFIGURE_DENIED", Xatom_wm_configure_denied)
12679 ATOM_REFS_INIT ("WM_MOVED", Xatom_wm_window_moved)
12680 ATOM_REFS_INIT ("WM_CLIENT_LEADER", Xatom_wm_client_leader)
12681 ATOM_REFS_INIT ("Editres", Xatom_editres)
12682 ATOM_REFS_INIT ("CLIPBOARD", Xatom_CLIPBOARD)
12683 ATOM_REFS_INIT ("TIMESTAMP", Xatom_TIMESTAMP)
12684 ATOM_REFS_INIT ("TEXT", Xatom_TEXT)
12685 ATOM_REFS_INIT ("COMPOUND_TEXT", Xatom_COMPOUND_TEXT)
12686 ATOM_REFS_INIT ("UTF8_STRING", Xatom_UTF8_STRING)
12687 ATOM_REFS_INIT ("DELETE", Xatom_DELETE)
12688 ATOM_REFS_INIT ("MULTIPLE", Xatom_MULTIPLE)
12689 ATOM_REFS_INIT ("INCR", Xatom_INCR)
12690 ATOM_REFS_INIT ("_EMACS_TMP_", Xatom_EMACS_TMP)
12691 ATOM_REFS_INIT ("TARGETS", Xatom_TARGETS)
12692 ATOM_REFS_INIT ("NULL", Xatom_NULL)
12693 ATOM_REFS_INIT ("ATOM", Xatom_ATOM)
12694 ATOM_REFS_INIT ("ATOM_PAIR", Xatom_ATOM_PAIR)
12695 ATOM_REFS_INIT ("CLIPBOARD_MANAGER", Xatom_CLIPBOARD_MANAGER)
12696 ATOM_REFS_INIT ("_XEMBED_INFO", Xatom_XEMBED_INFO)
12697 /* For properties of font. */
12698 ATOM_REFS_INIT ("PIXEL_SIZE", Xatom_PIXEL_SIZE)
12699 ATOM_REFS_INIT ("AVERAGE_WIDTH", Xatom_AVERAGE_WIDTH)
12700 ATOM_REFS_INIT ("_MULE_BASELINE_OFFSET", Xatom_MULE_BASELINE_OFFSET)
12701 ATOM_REFS_INIT ("_MULE_RELATIVE_COMPOSE", Xatom_MULE_RELATIVE_COMPOSE)
12702 ATOM_REFS_INIT ("_MULE_DEFAULT_ASCENT", Xatom_MULE_DEFAULT_ASCENT)
12703 /* Ghostscript support. */
12704 ATOM_REFS_INIT ("DONE", Xatom_DONE)
12705 ATOM_REFS_INIT ("PAGE", Xatom_PAGE)
12706 ATOM_REFS_INIT ("SCROLLBAR", Xatom_Scrollbar)
12707 ATOM_REFS_INIT ("HORIZONTAL_SCROLLBAR", Xatom_Horizontal_Scrollbar)
12708 ATOM_REFS_INIT ("_XEMBED", Xatom_XEMBED)
12709 /* EWMH */
12710 ATOM_REFS_INIT ("_NET_WM_STATE", Xatom_net_wm_state)
12711 ATOM_REFS_INIT ("_NET_WM_STATE_FULLSCREEN", Xatom_net_wm_state_fullscreen)
12712 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_HORZ",
12713 Xatom_net_wm_state_maximized_horz)
12714 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_VERT",
12715 Xatom_net_wm_state_maximized_vert)
12716 ATOM_REFS_INIT ("_NET_WM_STATE_STICKY", Xatom_net_wm_state_sticky)
12717 ATOM_REFS_INIT ("_NET_WM_STATE_HIDDEN", Xatom_net_wm_state_hidden)
12718 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE", Xatom_net_window_type)
12719 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE_TOOLTIP",
12720 Xatom_net_window_type_tooltip)
12721 ATOM_REFS_INIT ("_NET_WM_ICON_NAME", Xatom_net_wm_icon_name)
12722 ATOM_REFS_INIT ("_NET_WM_NAME", Xatom_net_wm_name)
12723 ATOM_REFS_INIT ("_NET_SUPPORTED", Xatom_net_supported)
12724 ATOM_REFS_INIT ("_NET_SUPPORTING_WM_CHECK", Xatom_net_supporting_wm_check)
12725 ATOM_REFS_INIT ("_NET_WM_WINDOW_OPACITY", Xatom_net_wm_window_opacity)
12726 ATOM_REFS_INIT ("_NET_ACTIVE_WINDOW", Xatom_net_active_window)
12727 ATOM_REFS_INIT ("_NET_FRAME_EXTENTS", Xatom_net_frame_extents)
12728 ATOM_REFS_INIT ("_NET_CURRENT_DESKTOP", Xatom_net_current_desktop)
12729 ATOM_REFS_INIT ("_NET_WORKAREA", Xatom_net_workarea)
12730 /* Session management */
12731 ATOM_REFS_INIT ("SM_CLIENT_ID", Xatom_SM_CLIENT_ID)
12732 ATOM_REFS_INIT ("_XSETTINGS_SETTINGS", Xatom_xsettings_prop)
12733 ATOM_REFS_INIT ("MANAGER", Xatom_xsettings_mgr)
12734 ATOM_REFS_INIT ("_NET_WM_STATE_SKIP_TASKBAR", Xatom_net_wm_state_skip_taskbar)
12735 ATOM_REFS_INIT ("_NET_WM_STATE_ABOVE", Xatom_net_wm_state_above)
12736 ATOM_REFS_INIT ("_NET_WM_STATE_BELOW", Xatom_net_wm_state_below)
12739 int i;
12740 enum { atom_count = ARRAYELTS (atom_refs) };
12741 /* 1 for _XSETTINGS_SN. */
12742 enum { total_atom_count = 1 + atom_count };
12743 Atom atoms_return[total_atom_count];
12744 char *atom_names[total_atom_count];
12745 static char const xsettings_fmt[] = "_XSETTINGS_S%d";
12746 char xsettings_atom_name[sizeof xsettings_fmt - 2
12747 + INT_STRLEN_BOUND (int)];
12749 for (i = 0; i < atom_count; i++)
12750 atom_names[i] = (char *) atom_refs[i].name;
12752 /* Build _XSETTINGS_SN atom name. */
12753 sprintf (xsettings_atom_name, xsettings_fmt,
12754 XScreenNumberOfScreen (dpyinfo->screen));
12755 atom_names[i] = xsettings_atom_name;
12757 XInternAtoms (dpyinfo->display, atom_names, total_atom_count,
12758 False, atoms_return);
12760 for (i = 0; i < atom_count; i++)
12761 *(Atom *) ((char *) dpyinfo + atom_refs[i].offset) = atoms_return[i];
12763 /* Manually copy last atom. */
12764 dpyinfo->Xatom_xsettings_sel = atoms_return[i];
12767 dpyinfo->x_dnd_atoms_size = 8;
12768 dpyinfo->x_dnd_atoms = xmalloc (sizeof *dpyinfo->x_dnd_atoms
12769 * dpyinfo->x_dnd_atoms_size);
12770 dpyinfo->gray
12771 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12772 gray_bits, gray_width, gray_height,
12773 1, 0, 1);
12775 x_setup_pointer_blanking (dpyinfo);
12777 #ifdef HAVE_X_I18N
12778 xim_initialize (dpyinfo, resource_name);
12779 #endif
12781 xsettings_initialize (dpyinfo);
12783 /* This is only needed for distinguishing keyboard and process input. */
12784 if (dpyinfo->connection != 0)
12785 add_keyboard_wait_descriptor (dpyinfo->connection);
12787 #ifdef F_SETOWN
12788 fcntl (dpyinfo->connection, F_SETOWN, getpid ());
12789 #endif /* ! defined (F_SETOWN) */
12791 if (interrupt_input)
12792 init_sigio (dpyinfo->connection);
12794 #ifdef USE_LUCID
12796 XrmValue d, fr, to;
12797 Font font;
12799 dpy = dpyinfo->display;
12800 d.addr = (XPointer)&dpy;
12801 d.size = sizeof (Display *);
12802 fr.addr = (char *) XtDefaultFont;
12803 fr.size = sizeof (XtDefaultFont);
12804 to.size = sizeof (Font *);
12805 to.addr = (XPointer)&font;
12806 x_catch_errors (dpy);
12807 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
12808 emacs_abort ();
12809 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
12810 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
12811 /* Do not free XFontStruct returned by the above call to XQueryFont.
12812 This leads to X protocol errors at XtCloseDisplay (Bug#18403). */
12813 x_uncatch_errors ();
12815 #endif
12817 /* See if we should run in synchronous mode. This is useful
12818 for debugging X code. */
12820 AUTO_STRING (synchronous, "synchronous");
12821 AUTO_STRING (Synchronous, "Synchronous");
12822 Lisp_Object value = display_x_get_resource (dpyinfo, synchronous,
12823 Synchronous, Qnil, Qnil);
12824 if (STRINGP (value)
12825 && (!strcmp (SSDATA (value), "true")
12826 || !strcmp (SSDATA (value), "on")))
12827 XSynchronize (dpyinfo->display, True);
12831 AUTO_STRING (useXIM, "useXIM");
12832 AUTO_STRING (UseXIM, "UseXIM");
12833 Lisp_Object value = display_x_get_resource (dpyinfo, useXIM, UseXIM,
12834 Qnil, Qnil);
12835 #ifdef USE_XIM
12836 if (STRINGP (value)
12837 && (!strcmp (SSDATA (value), "false")
12838 || !strcmp (SSDATA (value), "off")))
12839 use_xim = false;
12840 #else
12841 if (STRINGP (value)
12842 && (!strcmp (SSDATA (value), "true")
12843 || !strcmp (SSDATA (value), "on")))
12844 use_xim = true;
12845 #endif
12848 #ifdef HAVE_X_SM
12849 /* Only do this for the very first display in the Emacs session.
12850 Ignore X session management when Emacs was first started on a
12851 tty or started as a daemon. */
12852 if (terminal->id == 1 && ! IS_DAEMON)
12853 x_session_initialize (dpyinfo);
12854 #endif
12856 #ifdef USE_CAIRO
12857 x_extension_initialize (dpyinfo);
12858 #endif
12860 unblock_input ();
12862 return dpyinfo;
12865 /* Get rid of display DPYINFO, deleting all frames on it,
12866 and without sending any more commands to the X server. */
12868 static void
12869 x_delete_display (struct x_display_info *dpyinfo)
12871 struct terminal *t;
12872 struct color_name_cache_entry *color_entry, *next_color_entry;
12874 /* Close all frames and delete the generic struct terminal for this
12875 X display. */
12876 for (t = terminal_list; t; t = t->next_terminal)
12877 if (t->type == output_x_window && t->display_info.x == dpyinfo)
12879 #ifdef HAVE_X_SM
12880 /* Close X session management when we close its display. */
12881 if (t->id == 1 && x_session_have_connection ())
12882 x_session_close ();
12883 #endif
12884 delete_terminal (t);
12885 break;
12888 if (next_noop_dpyinfo == dpyinfo)
12889 next_noop_dpyinfo = dpyinfo->next;
12891 if (x_display_list == dpyinfo)
12892 x_display_list = dpyinfo->next;
12893 else
12895 struct x_display_info *tail;
12897 for (tail = x_display_list; tail; tail = tail->next)
12898 if (tail->next == dpyinfo)
12899 tail->next = tail->next->next;
12902 for (color_entry = dpyinfo->color_names;
12903 color_entry;
12904 color_entry = next_color_entry)
12906 next_color_entry = color_entry->next;
12907 xfree (color_entry->name);
12908 xfree (color_entry);
12911 xfree (dpyinfo->x_id_name);
12912 xfree (dpyinfo->x_dnd_atoms);
12913 xfree (dpyinfo->color_cells);
12914 xfree (dpyinfo);
12917 #ifdef USE_X_TOOLKIT
12919 /* Atimer callback function for TIMER. Called every 0.1s to process
12920 Xt timeouts, if needed. We must avoid calling XtAppPending as
12921 much as possible because that function does an implicit XFlush
12922 that slows us down. */
12924 static void
12925 x_process_timeouts (struct atimer *timer)
12927 block_input ();
12928 x_timeout_atimer_activated_flag = false;
12929 if (toolkit_scroll_bar_interaction || popup_activated ())
12931 while (XtAppPending (Xt_app_con) & XtIMTimer)
12932 XtAppProcessEvent (Xt_app_con, XtIMTimer);
12933 /* Reactivate the atimer for next time. */
12934 x_activate_timeout_atimer ();
12936 unblock_input ();
12939 /* Install an asynchronous timer that processes Xt timeout events
12940 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
12941 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
12942 function whenever these variables are set. This is necessary
12943 because some widget sets use timeouts internally, for example the
12944 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
12945 processed, these widgets don't behave normally. */
12947 void
12948 x_activate_timeout_atimer (void)
12950 block_input ();
12951 if (!x_timeout_atimer_activated_flag)
12953 struct timespec interval = make_timespec (0, 100 * 1000 * 1000);
12954 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
12955 x_timeout_atimer_activated_flag = true;
12957 unblock_input ();
12960 #endif /* USE_X_TOOLKIT */
12963 /* Set up use of X before we make the first connection. */
12965 static struct redisplay_interface x_redisplay_interface =
12967 x_frame_parm_handlers,
12968 x_produce_glyphs,
12969 x_write_glyphs,
12970 x_insert_glyphs,
12971 x_clear_end_of_line,
12972 x_scroll_run,
12973 x_after_update_window_line,
12974 x_update_window_begin,
12975 x_update_window_end,
12976 x_flip_and_flush,
12977 x_clear_window_mouse_face,
12978 x_get_glyph_overhangs,
12979 x_fix_overlapping_area,
12980 x_draw_fringe_bitmap,
12981 #ifdef USE_CAIRO
12982 x_cr_define_fringe_bitmap,
12983 x_cr_destroy_fringe_bitmap,
12984 #else
12985 0, /* define_fringe_bitmap */
12986 0, /* destroy_fringe_bitmap */
12987 #endif
12988 x_compute_glyph_string_overhangs,
12989 x_draw_glyph_string,
12990 x_define_frame_cursor,
12991 x_clear_frame_area,
12992 x_draw_window_cursor,
12993 x_draw_vertical_window_border,
12994 x_draw_window_divider,
12995 x_shift_glyphs_for_insert, /* Never called; see comment in function. */
12996 x_show_hourglass,
12997 x_hide_hourglass
13001 /* This function is called when the last frame on a display is deleted. */
13002 void
13003 x_delete_terminal (struct terminal *terminal)
13005 struct x_display_info *dpyinfo = terminal->display_info.x;
13007 /* Protect against recursive calls. delete_frame in
13008 delete_terminal calls us back when it deletes our last frame. */
13009 if (!terminal->name)
13010 return;
13012 block_input ();
13013 #ifdef HAVE_X_I18N
13014 /* We must close our connection to the XIM server before closing the
13015 X display. */
13016 if (dpyinfo->xim)
13017 xim_close_dpy (dpyinfo);
13018 #endif
13020 /* Normally, the display is available... */
13021 if (dpyinfo->display)
13023 x_destroy_all_bitmaps (dpyinfo);
13024 XSetCloseDownMode (dpyinfo->display, DestroyAll);
13026 /* Whether or not XCloseDisplay destroys the associated resource
13027 database depends on the version of libX11. To avoid both
13028 crash and memory leak, we dissociate the database from the
13029 display and then destroy dpyinfo->xrdb ourselves.
13031 Unfortunately, the above strategy does not work in some
13032 situations due to a bug in newer versions of libX11: because
13033 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
13034 dpy->db is NULL, XCloseDisplay destroys the associated
13035 database whereas it has not been created by XGetDefault
13036 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
13037 don't destroy the database here in order to avoid the crash
13038 in the above situations for now, though that may cause memory
13039 leaks in other situations. */
13040 #if false
13041 #ifdef HAVE_XRMSETDATABASE
13042 XrmSetDatabase (dpyinfo->display, NULL);
13043 #else
13044 dpyinfo->display->db = NULL;
13045 #endif
13046 /* We used to call XrmDestroyDatabase from x_delete_display, but
13047 some older versions of libX11 crash if we call it after
13048 closing all the displays. */
13049 XrmDestroyDatabase (dpyinfo->xrdb);
13050 #endif
13052 #ifdef USE_GTK
13053 xg_display_close (dpyinfo->display);
13054 #else
13055 #ifdef USE_X_TOOLKIT
13056 XtCloseDisplay (dpyinfo->display);
13057 #else
13058 XCloseDisplay (dpyinfo->display);
13059 #endif
13060 #endif /* ! USE_GTK */
13061 /* Do not close the connection here because it's already closed
13062 by X(t)CloseDisplay (Bug#18403). */
13063 dpyinfo->display = NULL;
13066 /* ...but if called from x_connection_closed, the display may already
13067 be closed and dpyinfo->display was set to 0 to indicate that. Since
13068 X server is most likely gone, explicit close is the only reliable
13069 way to continue and avoid Bug#19147. */
13070 else if (dpyinfo->connection >= 0)
13071 emacs_close (dpyinfo->connection);
13073 /* No more input on this descriptor. */
13074 delete_keyboard_wait_descriptor (dpyinfo->connection);
13075 /* Mark as dead. */
13076 dpyinfo->connection = -1;
13078 x_delete_display (dpyinfo);
13079 unblock_input ();
13082 /* Create a struct terminal, initialize it with the X11 specific
13083 functions and make DISPLAY->TERMINAL point to it. */
13085 static struct terminal *
13086 x_create_terminal (struct x_display_info *dpyinfo)
13088 struct terminal *terminal;
13090 terminal = create_terminal (output_x_window, &x_redisplay_interface);
13092 terminal->display_info.x = dpyinfo;
13093 dpyinfo->terminal = terminal;
13095 /* kboard is initialized in x_term_init. */
13097 terminal->clear_frame_hook = x_clear_frame;
13098 terminal->ins_del_lines_hook = x_ins_del_lines;
13099 terminal->delete_glyphs_hook = x_delete_glyphs;
13100 terminal->ring_bell_hook = XTring_bell;
13101 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
13102 terminal->update_begin_hook = x_update_begin;
13103 terminal->update_end_hook = x_update_end;
13104 terminal->read_socket_hook = XTread_socket;
13105 terminal->frame_up_to_date_hook = XTframe_up_to_date;
13106 terminal->buffer_flipping_unblocked_hook = XTbuffer_flipping_unblocked_hook;
13107 terminal->mouse_position_hook = XTmouse_position;
13108 terminal->frame_rehighlight_hook = XTframe_rehighlight;
13109 terminal->frame_raise_lower_hook = XTframe_raise_lower;
13110 terminal->fullscreen_hook = XTfullscreen_hook;
13111 terminal->menu_show_hook = x_menu_show;
13112 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
13113 terminal->popup_dialog_hook = xw_popup_dialog;
13114 #endif
13115 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13116 terminal->set_horizontal_scroll_bar_hook = XTset_horizontal_scroll_bar;
13117 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13118 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
13119 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
13120 terminal->delete_frame_hook = x_destroy_window;
13121 terminal->delete_terminal_hook = x_delete_terminal;
13122 /* Other hooks are NULL by default. */
13124 return terminal;
13127 static void
13128 x_initialize (void)
13130 baud_rate = 19200;
13132 x_noop_count = 0;
13133 any_help_event_p = false;
13134 ignore_next_mouse_click_timeout = 0;
13136 #ifdef USE_GTK
13137 current_count = -1;
13138 #endif
13140 /* Try to use interrupt input; if we can't, then start polling. */
13141 Fset_input_interrupt_mode (Qt);
13143 #if THREADS_ENABLED
13144 /* This must be called before any other Xlib routines. */
13145 if (XInitThreads () == 0)
13146 fprintf (stderr,
13147 "Warning: An error occurred initializing X11 thread support!\n");
13148 #endif
13150 #ifdef USE_X_TOOLKIT
13151 XtToolkitInitialize ();
13153 Xt_app_con = XtCreateApplicationContext ();
13155 /* Register a converter from strings to pixels, which uses
13156 Emacs' color allocation infrastructure. */
13157 XtAppSetTypeConverter (Xt_app_con,
13158 XtRString, XtRPixel, cvt_string_to_pixel,
13159 cvt_string_to_pixel_args,
13160 XtNumber (cvt_string_to_pixel_args),
13161 XtCacheByDisplay, cvt_pixel_dtor);
13163 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
13164 #endif
13166 #ifdef USE_TOOLKIT_SCROLL_BARS
13167 #ifndef USE_GTK
13168 xaw3d_arrow_scroll = False;
13169 xaw3d_pick_top = True;
13170 #endif
13171 #endif
13173 #ifdef USE_CAIRO
13174 x_cr_init_fringe (&x_redisplay_interface);
13175 #endif
13177 /* Note that there is no real way portable across R3/R4 to get the
13178 original error handler. */
13179 XSetErrorHandler (x_error_handler);
13180 XSetIOErrorHandler (x_io_error_quitter);
13183 #ifdef USE_GTK
13184 void
13185 init_xterm (void)
13187 /* Emacs can handle only core input events, so make sure
13188 Gtk doesn't use Xinput or Xinput2 extensions. */
13189 xputenv ("GDK_CORE_DEVICE_EVENTS=1");
13191 #endif
13193 void
13194 syms_of_xterm (void)
13196 x_error_message = NULL;
13198 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
13199 DEFSYM (Qlatin_1, "latin-1");
13201 #ifdef USE_GTK
13202 xg_default_icon_file = build_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
13203 staticpro (&xg_default_icon_file);
13205 DEFSYM (Qx_gtk_map_stock, "x-gtk-map-stock");
13206 #endif
13208 DEFVAR_BOOL ("x-use-underline-position-properties",
13209 x_use_underline_position_properties,
13210 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
13211 A value of nil means ignore them. If you encounter fonts with bogus
13212 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
13213 to 4.1, set this to nil. You can also use `underline-minimum-offset'
13214 to override the font's UNDERLINE_POSITION for small font display
13215 sizes. */);
13216 x_use_underline_position_properties = true;
13218 DEFVAR_BOOL ("x-underline-at-descent-line",
13219 x_underline_at_descent_line,
13220 doc: /* Non-nil means to draw the underline at the same place as the descent line.
13221 A value of nil means to draw the underline according to the value of the
13222 variable `x-use-underline-position-properties', which is usually at the
13223 baseline level. The default value is nil. */);
13224 x_underline_at_descent_line = false;
13226 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
13227 x_mouse_click_focus_ignore_position,
13228 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
13229 This variable is only used when the window manager requires that you
13230 click on a frame to select it (give it focus). In that case, a value
13231 of nil, means that the selected window and cursor position changes to
13232 reflect the mouse click position, while a non-nil value means that the
13233 selected window or cursor position is preserved. */);
13234 x_mouse_click_focus_ignore_position = false;
13236 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
13237 doc: /* Which toolkit scroll bars Emacs uses, if any.
13238 A value of nil means Emacs doesn't use toolkit scroll bars.
13239 With the X Window system, the value is a symbol describing the
13240 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
13241 With MS Windows or Nextstep, the value is t. */);
13242 #ifdef USE_TOOLKIT_SCROLL_BARS
13243 #ifdef USE_MOTIF
13244 Vx_toolkit_scroll_bars = intern_c_string ("motif");
13245 #elif defined HAVE_XAW3D
13246 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
13247 #elif USE_GTK
13248 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
13249 #else
13250 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
13251 #endif
13252 #else
13253 Vx_toolkit_scroll_bars = Qnil;
13254 #endif
13256 DEFSYM (Qmodifier_value, "modifier-value");
13257 DEFSYM (Qctrl, "ctrl");
13258 Fput (Qctrl, Qmodifier_value, make_number (ctrl_modifier));
13259 DEFSYM (Qalt, "alt");
13260 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
13261 DEFSYM (Qhyper, "hyper");
13262 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
13263 DEFSYM (Qmeta, "meta");
13264 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
13265 DEFSYM (Qsuper, "super");
13266 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
13268 DEFVAR_LISP ("x-ctrl-keysym", Vx_ctrl_keysym,
13269 doc: /* Which keys Emacs uses for the ctrl modifier.
13270 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
13271 `super'. For example, `ctrl' means use the Ctrl_L and Ctrl_R keysyms.
13272 The default is nil, which is the same as `ctrl'. */);
13273 Vx_ctrl_keysym = Qnil;
13275 DEFVAR_LISP ("x-alt-keysym", Vx_alt_keysym,
13276 doc: /* Which keys Emacs uses for the alt modifier.
13277 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
13278 `super'. For example, `alt' means use the Alt_L and Alt_R keysyms.
13279 The default is nil, which is the same as `alt'. */);
13280 Vx_alt_keysym = Qnil;
13282 DEFVAR_LISP ("x-hyper-keysym", Vx_hyper_keysym,
13283 doc: /* Which keys Emacs uses for the hyper modifier.
13284 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
13285 `super'. For example, `hyper' means use the Hyper_L and Hyper_R
13286 keysyms. The default is nil, which is the same as `hyper'. */);
13287 Vx_hyper_keysym = Qnil;
13289 DEFVAR_LISP ("x-meta-keysym", Vx_meta_keysym,
13290 doc: /* Which keys Emacs uses for the meta modifier.
13291 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
13292 `super'. For example, `meta' means use the Meta_L and Meta_R keysyms.
13293 The default is nil, which is the same as `meta'. */);
13294 Vx_meta_keysym = Qnil;
13296 DEFVAR_LISP ("x-super-keysym", Vx_super_keysym,
13297 doc: /* Which keys Emacs uses for the super modifier.
13298 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
13299 `super'. For example, `super' means use the Super_L and Super_R
13300 keysyms. The default is nil, which is the same as `super'. */);
13301 Vx_super_keysym = Qnil;
13303 DEFVAR_LISP ("x-wait-for-event-timeout", Vx_wait_for_event_timeout,
13304 doc: /* How long to wait for X events.
13306 Emacs will wait up to this many seconds to receive X events after
13307 making changes which affect the state of the graphical interface.
13308 Under some window managers this can take an indefinite amount of time,
13309 so it is important to limit the wait.
13311 If set to a non-float value, there will be no wait at all. */);
13312 Vx_wait_for_event_timeout = make_float (0.1);
13314 DEFVAR_LISP ("x-keysym-table", Vx_keysym_table,
13315 doc: /* Hash table of character codes indexed by X keysym codes. */);
13316 Vx_keysym_table = make_hash_table (hashtest_eql, 900,
13317 DEFAULT_REHASH_SIZE,
13318 DEFAULT_REHASH_THRESHOLD,
13319 Qnil, false);
13321 DEFVAR_BOOL ("x-frame-normalize-before-maximize",
13322 x_frame_normalize_before_maximize,
13323 doc: /* Non-nil means normalize frame before maximizing.
13324 If this variable is t, Emacs first asks the window manager to give the
13325 frame its normal size, and only then the final state, whenever changing
13326 from a full-height, full-width or full-both state to the maximized one
13327 or when changing from the maximized to the full-height or full-width
13328 state.
13330 Set this variable only if your window manager cannot handle the
13331 transition between the various maximization states. */);
13332 x_frame_normalize_before_maximize = false;
13334 DEFVAR_BOOL ("x-gtk-use-window-move", x_gtk_use_window_move,
13335 doc: /* Non-nil means rely on gtk_window_move to set frame positions.
13336 If this variable is t (the default), the GTK build uses the function
13337 gtk_window_move to set or store frame positions and disables some time
13338 consuming frame position adjustments. In newer versions of GTK, Emacs
13339 always uses gtk_window_move and ignores the value of this variable. */);
13340 x_gtk_use_window_move = true;