Fix some problems in the Cairo build
[emacs.git] / src / xterm.c
blobf6f2079ec694da89be641c91114132d06651ee30
1 /* X Communication module for terminals which understand the X protocol.
3 Copyright (C) 1989, 1993-2018 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
20 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
21 /* Xt features made by Fred Pierresteguy. */
23 #include <config.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <math.h>
28 #include "lisp.h"
29 #include "blockinput.h"
31 /* This may include sys/types.h, and that somehow loses
32 if this is not done before the other system files. */
33 #include "xterm.h"
34 #include <X11/cursorfont.h>
36 /* If we have Xfixes extension, use it for pointer blanking. */
37 #ifdef HAVE_XFIXES
38 #include <X11/extensions/Xfixes.h>
39 #endif
41 /* Using Xft implies that XRender is available. */
42 #ifdef HAVE_XFT
43 #include <X11/extensions/Xrender.h>
44 #endif
46 #ifdef HAVE_XDBE
47 #include <X11/extensions/Xdbe.h>
48 #endif
50 /* Load sys/types.h if not already loaded.
51 In some systems loading it twice is suicidal. */
52 #ifndef makedev
53 #include <sys/types.h>
54 #endif /* makedev */
56 #include <sys/ioctl.h>
58 #include "systime.h"
60 #include <fcntl.h>
61 #include <errno.h>
62 #include <sys/stat.h>
63 #include "character.h"
64 #include "coding.h"
65 #include "composite.h"
66 #include "frame.h"
67 #include "dispextern.h"
68 #include "xwidget.h"
69 #include "fontset.h"
70 #include "termhooks.h"
71 #include "termopts.h"
72 #include "termchar.h"
73 #include "emacs-icon.h"
74 #include "buffer.h"
75 #include "window.h"
76 #include "keyboard.h"
77 #include "atimer.h"
78 #include "font.h"
79 #include "xsettings.h"
80 #include "sysselect.h"
81 #include "menu.h"
83 #ifdef USE_X_TOOLKIT
84 #include <X11/Shell.h>
85 #endif
87 #include <unistd.h>
89 #ifdef USE_GTK
90 #include "gtkutil.h"
91 #ifdef HAVE_GTK3
92 #include <X11/Xproto.h>
93 #endif
94 #endif
96 #if defined (USE_LUCID) || defined (USE_MOTIF)
97 #include "../lwlib/xlwmenu.h"
98 #endif
100 #ifdef USE_X_TOOLKIT
102 /* Include toolkit specific headers for the scroll bar widget. */
104 #ifdef USE_TOOLKIT_SCROLL_BARS
105 #if defined USE_MOTIF
106 #include <Xm/Xm.h> /* For LESSTIF_VERSION */
107 #include <Xm/ScrollBar.h>
108 #else /* !USE_MOTIF i.e. use Xaw */
110 #ifdef HAVE_XAW3D
111 #include <X11/Xaw3d/Simple.h>
112 #include <X11/Xaw3d/Scrollbar.h>
113 #include <X11/Xaw3d/ThreeD.h>
114 #else /* !HAVE_XAW3D */
115 #include <X11/Xaw/Simple.h>
116 #include <X11/Xaw/Scrollbar.h>
117 #endif /* !HAVE_XAW3D */
118 #ifndef XtNpickTop
119 #define XtNpickTop "pickTop"
120 #endif /* !XtNpickTop */
121 #endif /* !USE_MOTIF */
122 #endif /* USE_TOOLKIT_SCROLL_BARS */
124 #endif /* USE_X_TOOLKIT */
126 #ifdef USE_X_TOOLKIT
127 #include "widget.h"
128 #ifndef XtNinitialState
129 #define XtNinitialState "initialState"
130 #endif
131 #endif
133 #include "bitmaps/gray.xbm"
135 #ifdef HAVE_XKB
136 #include <X11/XKBlib.h>
137 #endif
139 /* Default to using XIM if available. */
140 #ifdef USE_XIM
141 bool use_xim = true;
142 #else
143 bool use_xim = false; /* configure --without-xim */
144 #endif
146 /* Non-zero means that a HELP_EVENT has been generated since Emacs
147 start. */
149 static bool any_help_event_p;
151 /* This is a chain of structures for all the X displays currently in
152 use. */
154 struct x_display_info *x_display_list;
156 #ifdef USE_X_TOOLKIT
158 /* The application context for Xt use. */
159 XtAppContext Xt_app_con;
160 static String Xt_default_resources[] = {0};
162 /* Non-zero means user is interacting with a toolkit scroll bar. */
163 static bool toolkit_scroll_bar_interaction;
165 #endif /* USE_X_TOOLKIT */
167 /* Non-zero timeout value means ignore next mouse click if it arrives
168 before that timeout elapses (i.e. as part of the same sequence of
169 events resulting from clicking on a frame to select it). */
171 static Time ignore_next_mouse_click_timeout;
173 /* Used locally within XTread_socket. */
175 static int x_noop_count;
177 #ifdef USE_GTK
178 /* The name of the Emacs icon file. */
179 static Lisp_Object xg_default_icon_file;
180 #endif
182 /* Some functions take this as char *, not const char *. */
183 static char emacs_class[] = EMACS_CLASS;
185 enum xembed_info
187 XEMBED_MAPPED = 1 << 0
190 enum xembed_message
192 XEMBED_EMBEDDED_NOTIFY = 0,
193 XEMBED_WINDOW_ACTIVATE = 1,
194 XEMBED_WINDOW_DEACTIVATE = 2,
195 XEMBED_REQUEST_FOCUS = 3,
196 XEMBED_FOCUS_IN = 4,
197 XEMBED_FOCUS_OUT = 5,
198 XEMBED_FOCUS_NEXT = 6,
199 XEMBED_FOCUS_PREV = 7,
201 XEMBED_MODALITY_ON = 10,
202 XEMBED_MODALITY_OFF = 11,
203 XEMBED_REGISTER_ACCELERATOR = 12,
204 XEMBED_UNREGISTER_ACCELERATOR = 13,
205 XEMBED_ACTIVATE_ACCELERATOR = 14
208 static void x_free_cr_resources (struct frame *);
209 static bool x_alloc_nearest_color_1 (Display *, Colormap, XColor *);
210 static void x_raise_frame (struct frame *);
211 static void x_lower_frame (struct frame *);
212 static int x_io_error_quitter (Display *);
213 static struct terminal *x_create_terminal (struct x_display_info *);
214 static void x_frame_rehighlight (struct x_display_info *);
216 static void x_clip_to_row (struct window *, struct glyph_row *,
217 enum glyph_row_area, GC);
218 static struct scroll_bar *x_window_to_scroll_bar (Display *, Window, int);
219 static void x_scroll_bar_report_motion (struct frame **, Lisp_Object *,
220 enum scroll_bar_part *,
221 Lisp_Object *, Lisp_Object *,
222 Time *);
223 static void x_horizontal_scroll_bar_report_motion (struct frame **, Lisp_Object *,
224 enum scroll_bar_part *,
225 Lisp_Object *, Lisp_Object *,
226 Time *);
227 static bool x_handle_net_wm_state (struct frame *, const XPropertyEvent *);
228 static void x_check_fullscreen (struct frame *);
229 static void x_check_expected_move (struct frame *, int, int);
230 static void x_sync_with_move (struct frame *, int, int, bool);
231 static int handle_one_xevent (struct x_display_info *,
232 const XEvent *, int *,
233 struct input_event *);
234 #if ! (defined USE_X_TOOLKIT || defined USE_MOTIF) && defined USE_GTK
235 static int x_dispatch_event (XEvent *, Display *);
236 #endif
237 static void x_wm_set_window_state (struct frame *, int);
238 static void x_wm_set_icon_pixmap (struct frame *, ptrdiff_t);
239 static void x_initialize (void);
241 static bool get_current_wm_state (struct frame *, Window, int *, bool *);
243 /* Flush display of frame F. */
245 static void
246 x_flush (struct frame *f)
248 eassert (f && FRAME_X_P (f));
249 /* Don't call XFlush when it is not safe to redisplay; the X
250 connection may be broken. */
251 if (!NILP (Vinhibit_redisplay))
252 return;
254 block_input ();
255 XFlush (FRAME_X_DISPLAY (f));
256 unblock_input ();
260 /* Remove calls to XFlush by defining XFlush to an empty replacement.
261 Calls to XFlush should be unnecessary because the X output buffer
262 is flushed automatically as needed by calls to XPending,
263 XNextEvent, or XWindowEvent according to the XFlush man page.
264 XTread_socket calls XPending. Removing XFlush improves
265 performance. */
267 #define XFlush(DISPLAY) (void) 0
270 /***********************************************************************
271 Debugging
272 ***********************************************************************/
274 #if false
276 /* This is a function useful for recording debugging information about
277 the sequence of occurrences in this file. */
279 struct record
281 char *locus;
282 int type;
285 struct record event_record[100];
287 int event_record_index;
289 void
290 record_event (char *locus, int type)
292 if (event_record_index == ARRAYELTS (event_record))
293 event_record_index = 0;
295 event_record[event_record_index].locus = locus;
296 event_record[event_record_index].type = type;
297 event_record_index++;
300 #endif
302 #ifdef USE_CAIRO
304 #define FRAME_CR_CONTEXT(f) ((f)->output_data.x->cr_context)
305 #define FRAME_CR_SURFACE(f) ((f)->output_data.x->cr_surface)
307 static struct x_gc_ext_data *
308 x_gc_get_ext_data (struct frame *f, GC gc, int create_if_not_found_p)
310 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
311 XEDataObject object;
312 XExtData **head, *ext_data;
314 object.gc = gc;
315 head = XEHeadOfExtensionList (object);
316 ext_data = XFindOnExtensionList (head, dpyinfo->ext_codes->extension);
317 if (ext_data == NULL)
319 if (!create_if_not_found_p)
320 return NULL;
321 else
323 ext_data = xzalloc (sizeof (*ext_data));
324 ext_data->number = dpyinfo->ext_codes->extension;
325 ext_data->private_data = xzalloc (sizeof (struct x_gc_ext_data));
326 XAddToExtensionList (head, ext_data);
329 return (struct x_gc_ext_data *) ext_data->private_data;
332 static void
333 x_extension_initialize (struct x_display_info *dpyinfo)
335 XExtCodes *ext_codes = XAddExtension (dpyinfo->display);
337 dpyinfo->ext_codes = ext_codes;
340 static void
341 x_cr_destroy_surface (struct frame *f)
343 if (FRAME_CR_SURFACE (f))
345 cairo_t *cr = FRAME_CR_CONTEXT (f);
346 cairo_surface_destroy (FRAME_CR_SURFACE (f));
347 FRAME_CR_SURFACE (f) = 0;
348 if (cr) cairo_destroy (cr);
349 FRAME_CR_CONTEXT (f) = NULL;
353 cairo_t *
354 x_begin_cr_clip (struct frame *f, GC gc)
356 cairo_t *cr = FRAME_CR_CONTEXT (f);
358 if (!cr)
361 if (! FRAME_CR_SURFACE (f))
363 FRAME_CR_SURFACE (f) =
364 cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
365 FRAME_PIXEL_WIDTH (f),
366 FRAME_PIXEL_HEIGHT (f));
368 cr = cairo_create (FRAME_CR_SURFACE (f));
369 FRAME_CR_CONTEXT (f) = cr;
371 cairo_save (cr);
373 if (gc)
375 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 0);
377 if (gc_ext && gc_ext->n_clip_rects)
379 int i;
381 for (i = 0; i < gc_ext->n_clip_rects; i++)
382 cairo_rectangle (cr, gc_ext->clip_rects[i].x,
383 gc_ext->clip_rects[i].y,
384 gc_ext->clip_rects[i].width,
385 gc_ext->clip_rects[i].height);
386 cairo_clip (cr);
390 return cr;
393 void
394 x_end_cr_clip (struct frame *f)
396 cairo_restore (FRAME_CR_CONTEXT (f));
399 void
400 x_set_cr_source_with_gc_foreground (struct frame *f, GC gc)
402 XGCValues xgcv;
403 XColor color;
405 XGetGCValues (FRAME_X_DISPLAY (f), gc, GCForeground, &xgcv);
406 color.pixel = xgcv.foreground;
407 x_query_color (f, &color);
408 cairo_set_source_rgb (FRAME_CR_CONTEXT (f), color.red / 65535.0,
409 color.green / 65535.0, color.blue / 65535.0);
412 void
413 x_set_cr_source_with_gc_background (struct frame *f, GC gc)
415 XGCValues xgcv;
416 XColor color;
418 XGetGCValues (FRAME_X_DISPLAY (f), gc, GCBackground, &xgcv);
419 color.pixel = xgcv.background;
420 x_query_color (f, &color);
421 cairo_set_source_rgb (FRAME_CR_CONTEXT (f), color.red / 65535.0,
422 color.green / 65535.0, color.blue / 65535.0);
425 /* Fringe bitmaps. */
427 static int max_fringe_bmp = 0;
428 static cairo_pattern_t **fringe_bmp = 0;
430 static void
431 x_cr_define_fringe_bitmap (int which, unsigned short *bits, int h, int wd)
433 int i, stride;
434 cairo_surface_t *surface;
435 unsigned char *data;
436 cairo_pattern_t *pattern;
438 if (which >= max_fringe_bmp)
440 i = max_fringe_bmp;
441 max_fringe_bmp = which + 20;
442 fringe_bmp = (cairo_pattern_t **) xrealloc (fringe_bmp, max_fringe_bmp * sizeof (cairo_pattern_t *));
443 while (i < max_fringe_bmp)
444 fringe_bmp[i++] = 0;
447 block_input ();
449 surface = cairo_image_surface_create (CAIRO_FORMAT_A1, wd, h);
450 stride = cairo_image_surface_get_stride (surface);
451 data = cairo_image_surface_get_data (surface);
453 for (i = 0; i < h; i++)
455 *((unsigned short *) data) = bits[i];
456 data += stride;
459 cairo_surface_mark_dirty (surface);
460 pattern = cairo_pattern_create_for_surface (surface);
461 cairo_surface_destroy (surface);
463 unblock_input ();
465 fringe_bmp[which] = pattern;
468 static void
469 x_cr_destroy_fringe_bitmap (int which)
471 if (which >= max_fringe_bmp)
472 return;
474 if (fringe_bmp[which])
476 block_input ();
477 cairo_pattern_destroy (fringe_bmp[which]);
478 unblock_input ();
480 fringe_bmp[which] = 0;
483 static void
484 x_cr_draw_image (struct frame *f, GC gc, cairo_pattern_t *image,
485 int src_x, int src_y, int width, int height,
486 int dest_x, int dest_y, bool overlay_p)
488 cairo_t *cr;
489 cairo_matrix_t matrix;
490 cairo_surface_t *surface;
491 cairo_format_t format;
493 cr = x_begin_cr_clip (f, gc);
494 if (overlay_p)
495 cairo_rectangle (cr, dest_x, dest_y, width, height);
496 else
498 x_set_cr_source_with_gc_background (f, gc);
499 cairo_rectangle (cr, dest_x, dest_y, width, height);
500 cairo_fill_preserve (cr);
502 cairo_clip (cr);
503 cairo_matrix_init_translate (&matrix, src_x - dest_x, src_y - dest_y);
504 cairo_pattern_set_matrix (image, &matrix);
505 cairo_pattern_get_surface (image, &surface);
506 format = cairo_image_surface_get_format (surface);
507 if (format != CAIRO_FORMAT_A8 && format != CAIRO_FORMAT_A1)
509 cairo_set_source (cr, image);
510 cairo_fill (cr);
512 else
514 x_set_cr_source_with_gc_foreground (f, gc);
515 cairo_mask (cr, image);
517 x_end_cr_clip (f);
520 void
521 x_cr_draw_frame (cairo_t *cr, struct frame *f)
523 int width, height;
525 width = FRAME_PIXEL_WIDTH (f);
526 height = FRAME_PIXEL_HEIGHT (f);
528 x_free_cr_resources (f);
529 FRAME_CR_CONTEXT (f) = cr;
530 x_clear_area (f, 0, 0, width, height);
531 expose_frame (f, 0, 0, width, height);
532 FRAME_CR_CONTEXT (f) = NULL;
535 static cairo_status_t
536 x_cr_accumulate_data (void *closure, const unsigned char *data,
537 unsigned int length)
539 Lisp_Object *acc = (Lisp_Object *) closure;
541 *acc = Fcons (make_unibyte_string ((char const *) data, length), *acc);
543 return CAIRO_STATUS_SUCCESS;
546 static void
547 x_cr_destroy (Lisp_Object arg)
549 cairo_t *cr = (cairo_t *) XSAVE_POINTER (arg, 0);
551 block_input ();
552 cairo_destroy (cr);
553 unblock_input ();
556 Lisp_Object
557 x_cr_export_frames (Lisp_Object frames, cairo_surface_type_t surface_type)
559 struct frame *f;
560 cairo_surface_t *surface;
561 cairo_t *cr;
562 int width, height;
563 void (*surface_set_size_func) (cairo_surface_t *, double, double) = NULL;
564 Lisp_Object acc = Qnil;
565 ptrdiff_t count = SPECPDL_INDEX ();
567 specbind (Qredisplay_dont_pause, Qt);
568 redisplay_preserve_echo_area (31);
570 f = XFRAME (XCAR (frames));
571 frames = XCDR (frames);
572 width = FRAME_PIXEL_WIDTH (f);
573 height = FRAME_PIXEL_HEIGHT (f);
575 block_input ();
576 #ifdef CAIRO_HAS_PDF_SURFACE
577 if (surface_type == CAIRO_SURFACE_TYPE_PDF)
579 surface = cairo_pdf_surface_create_for_stream (x_cr_accumulate_data, &acc,
580 width, height);
581 surface_set_size_func = cairo_pdf_surface_set_size;
583 else
584 #endif
585 #ifdef CAIRO_HAS_PNG_FUNCTIONS
586 if (surface_type == CAIRO_SURFACE_TYPE_IMAGE)
587 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
588 else
589 #endif
590 #ifdef CAIRO_HAS_PS_SURFACE
591 if (surface_type == CAIRO_SURFACE_TYPE_PS)
593 surface = cairo_ps_surface_create_for_stream (x_cr_accumulate_data, &acc,
594 width, height);
595 surface_set_size_func = cairo_ps_surface_set_size;
597 else
598 #endif
599 #ifdef CAIRO_HAS_SVG_SURFACE
600 if (surface_type == CAIRO_SURFACE_TYPE_SVG)
601 surface = cairo_svg_surface_create_for_stream (x_cr_accumulate_data, &acc,
602 width, height);
603 else
604 #endif
605 abort ();
607 cr = cairo_create (surface);
608 cairo_surface_destroy (surface);
609 record_unwind_protect (x_cr_destroy, make_save_ptr (cr));
611 while (1)
613 x_free_cr_resources (f);
614 FRAME_CR_CONTEXT (f) = cr;
615 x_clear_area (f, 0, 0, width, height);
616 expose_frame (f, 0, 0, width, height);
617 FRAME_CR_CONTEXT (f) = NULL;
619 if (NILP (frames))
620 break;
622 cairo_surface_show_page (surface);
623 f = XFRAME (XCAR (frames));
624 frames = XCDR (frames);
625 width = FRAME_PIXEL_WIDTH (f);
626 height = FRAME_PIXEL_HEIGHT (f);
627 if (surface_set_size_func)
628 (*surface_set_size_func) (surface, width, height);
630 unblock_input ();
631 maybe_quit ();
632 block_input ();
635 #ifdef CAIRO_HAS_PNG_FUNCTIONS
636 if (surface_type == CAIRO_SURFACE_TYPE_IMAGE)
638 cairo_surface_flush (surface);
639 cairo_surface_write_to_png_stream (surface, x_cr_accumulate_data, &acc);
641 #endif
642 unblock_input ();
644 unbind_to (count, Qnil);
646 return CALLN (Fapply, intern ("concat"), Fnreverse (acc));
649 #endif /* USE_CAIRO */
651 static void
652 x_free_cr_resources (struct frame *f)
654 #ifdef USE_CAIRO
655 if (f == NULL)
657 Lisp_Object rest, frame;
658 FOR_EACH_FRAME (rest, frame)
659 if (FRAME_X_P (XFRAME (frame)))
660 x_free_cr_resources (XFRAME (frame));
662 else
664 cairo_t *cr = FRAME_CR_CONTEXT (f);
666 if (cr)
668 cairo_surface_t *surface = cairo_get_target (cr);
670 if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_XLIB)
672 cairo_destroy (cr);
673 FRAME_CR_CONTEXT (f) = NULL;
677 #endif
680 static void
681 x_set_clip_rectangles (struct frame *f, GC gc, XRectangle *rectangles, int n)
683 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, rectangles, n, Unsorted);
684 #ifdef USE_CAIRO
685 eassert (n >= 0 && n <= MAX_CLIP_RECTS);
688 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 1);
690 gc_ext->n_clip_rects = n;
691 memcpy (gc_ext->clip_rects, rectangles, sizeof (XRectangle) * n);
693 #endif
696 static void
697 x_reset_clip_rectangles (struct frame *f, GC gc)
699 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
700 #ifdef USE_CAIRO
702 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 0);
704 if (gc_ext)
705 gc_ext->n_clip_rects = 0;
707 #endif
710 static void
711 x_fill_rectangle (struct frame *f, GC gc, int x, int y, int width, int height)
713 #ifdef USE_CAIRO
714 cairo_t *cr;
716 cr = x_begin_cr_clip (f, gc);
717 x_set_cr_source_with_gc_foreground (f, gc);
718 cairo_rectangle (cr, x, y, width, height);
719 cairo_fill (cr);
720 x_end_cr_clip (f);
721 #else
722 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
723 gc, x, y, width, height);
724 #endif
727 static void
728 x_draw_rectangle (struct frame *f, GC gc, int x, int y, int width, int height)
730 #ifdef USE_CAIRO
731 cairo_t *cr;
733 cr = x_begin_cr_clip (f, gc);
734 x_set_cr_source_with_gc_foreground (f, gc);
735 cairo_rectangle (cr, x + 0.5, y + 0.5, width, height);
736 cairo_set_line_width (cr, 1);
737 cairo_stroke (cr);
738 x_end_cr_clip (f);
739 #else
740 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
741 gc, x, y, width, height);
742 #endif
745 static void
746 x_clear_window (struct frame *f)
748 #ifdef USE_CAIRO
749 cairo_t *cr;
751 cr = x_begin_cr_clip (f, NULL);
752 x_set_cr_source_with_gc_background (f, f->output_data.x->normal_gc);
753 cairo_paint (cr);
754 x_end_cr_clip (f);
755 #else
756 if (FRAME_X_DOUBLE_BUFFERED_P (f))
757 x_clear_area (f, 0, 0, FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f));
758 else
759 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
760 #endif
763 #ifdef USE_CAIRO
764 static void
765 x_fill_trapezoid_for_relief (struct frame *f, GC gc, int x, int y,
766 int width, int height, int top_p)
768 cairo_t *cr;
770 cr = x_begin_cr_clip (f, gc);
771 x_set_cr_source_with_gc_foreground (f, gc);
772 cairo_move_to (cr, top_p ? x : x + height, y);
773 cairo_line_to (cr, x, y + height);
774 cairo_line_to (cr, top_p ? x + width - height : x + width, y + height);
775 cairo_line_to (cr, x + width, y);
776 cairo_fill (cr);
777 x_end_cr_clip (f);
780 enum corners
782 CORNER_BOTTOM_RIGHT, /* 0 -> pi/2 */
783 CORNER_BOTTOM_LEFT, /* pi/2 -> pi */
784 CORNER_TOP_LEFT, /* pi -> 3pi/2 */
785 CORNER_TOP_RIGHT, /* 3pi/2 -> 2pi */
786 CORNER_LAST
789 static void
790 x_erase_corners_for_relief (struct frame *f, GC gc, int x, int y,
791 int width, int height,
792 double radius, double margin, int corners)
794 cairo_t *cr;
795 int i;
797 cr = x_begin_cr_clip (f, gc);
798 x_set_cr_source_with_gc_background (f, gc);
799 for (i = 0; i < CORNER_LAST; i++)
800 if (corners & (1 << i))
802 double xm, ym, xc, yc;
804 if (i == CORNER_TOP_LEFT || i == CORNER_BOTTOM_LEFT)
805 xm = x - margin, xc = xm + radius;
806 else
807 xm = x + width + margin, xc = xm - radius;
808 if (i == CORNER_TOP_LEFT || i == CORNER_TOP_RIGHT)
809 ym = y - margin, yc = ym + radius;
810 else
811 ym = y + height + margin, yc = ym - radius;
813 cairo_move_to (cr, xm, ym);
814 cairo_arc (cr, xc, yc, radius, i * M_PI_2, (i + 1) * M_PI_2);
816 cairo_clip (cr);
817 cairo_rectangle (cr, x, y, width, height);
818 cairo_fill (cr);
819 x_end_cr_clip (f);
822 static void
823 x_draw_horizontal_wave (struct frame *f, GC gc, int x, int y,
824 int width, int height, int wave_length)
826 cairo_t *cr;
827 double dx = wave_length, dy = height - 1;
828 int xoffset, n;
830 cr = x_begin_cr_clip (f, gc);
831 x_set_cr_source_with_gc_foreground (f, gc);
832 cairo_rectangle (cr, x, y, width, height);
833 cairo_clip (cr);
835 if (x >= 0)
837 xoffset = x % (wave_length * 2);
838 if (xoffset == 0)
839 xoffset = wave_length * 2;
841 else
842 xoffset = x % (wave_length * 2) + wave_length * 2;
843 n = (width + xoffset) / wave_length + 1;
844 if (xoffset > wave_length)
846 xoffset -= wave_length;
847 --n;
848 y += height - 1;
849 dy = -dy;
852 cairo_move_to (cr, x - xoffset + 0.5, y + 0.5);
853 while (--n >= 0)
855 cairo_rel_line_to (cr, dx, dy);
856 dy = -dy;
858 cairo_set_line_width (cr, 1);
859 cairo_stroke (cr);
860 x_end_cr_clip (f);
862 #endif
865 /* Return the struct x_display_info corresponding to DPY. */
867 struct x_display_info *
868 x_display_info_for_display (Display *dpy)
870 struct x_display_info *dpyinfo;
872 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
873 if (dpyinfo->display == dpy)
874 return dpyinfo;
876 return 0;
879 static Window
880 x_find_topmost_parent (struct frame *f)
882 struct x_output *x = f->output_data.x;
883 Window win = None, wi = x->parent_desc;
884 Display *dpy = FRAME_X_DISPLAY (f);
886 while (wi != FRAME_DISPLAY_INFO (f)->root_window)
888 Window root;
889 Window *children;
890 unsigned int nchildren;
892 win = wi;
893 if (XQueryTree (dpy, win, &root, &wi, &children, &nchildren))
894 XFree (children);
895 else
896 break;
899 return win;
902 #define OPAQUE 0xffffffff
904 void
905 x_set_frame_alpha (struct frame *f)
907 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
908 Display *dpy = FRAME_X_DISPLAY (f);
909 Window win = FRAME_OUTER_WINDOW (f);
910 double alpha = 1.0;
911 double alpha_min = 1.0;
912 unsigned long opac;
913 Window parent;
915 if (dpyinfo->x_highlight_frame == f)
916 alpha = f->alpha[0];
917 else
918 alpha = f->alpha[1];
920 if (FLOATP (Vframe_alpha_lower_limit))
921 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
922 else if (INTEGERP (Vframe_alpha_lower_limit))
923 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
925 if (alpha < 0.0)
926 return;
927 else if (alpha > 1.0)
928 alpha = 1.0;
929 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
930 alpha = alpha_min;
932 opac = alpha * OPAQUE;
934 x_catch_errors (dpy);
936 /* If there is a parent from the window manager, put the property there
937 also, to work around broken window managers that fail to do that.
938 Do this unconditionally as this function is called on reparent when
939 alpha has not changed on the frame. */
941 if (!FRAME_PARENT_FRAME (f))
943 parent = x_find_topmost_parent (f);
944 if (parent != None)
945 XChangeProperty (dpy, parent, dpyinfo->Xatom_net_wm_window_opacity,
946 XA_CARDINAL, 32, PropModeReplace,
947 (unsigned char *) &opac, 1);
950 /* return unless necessary */
952 unsigned char *data;
953 Atom actual;
954 int rc, format;
955 unsigned long n, left;
957 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
958 0, 1, False, XA_CARDINAL,
959 &actual, &format, &n, &left,
960 &data);
962 if (rc == Success && actual != None)
964 unsigned long value = *(unsigned long *)data;
965 XFree (data);
966 if (value == opac)
968 x_uncatch_errors ();
969 return;
974 XChangeProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
975 XA_CARDINAL, 32, PropModeReplace,
976 (unsigned char *) &opac, 1);
977 x_uncatch_errors ();
980 /***********************************************************************
981 Starting and ending an update
982 ***********************************************************************/
984 /* Start an update of frame F. This function is installed as a hook
985 for update_begin, i.e. it is called when update_begin is called.
986 This function is called prior to calls to x_update_window_begin for
987 each window being updated. Currently, there is nothing to do here
988 because all interesting stuff is done on a window basis. */
990 static void
991 x_update_begin (struct frame *f)
993 #ifdef USE_CAIRO
994 if (! NILP (tip_frame) && XFRAME (tip_frame) == f
995 && ! FRAME_VISIBLE_P (f)
996 #ifdef USE_GTK
997 && !NILP (Fframe_parameter (tip_frame, Qtooltip))
998 #endif
1000 return;
1002 if (! FRAME_CR_SURFACE (f))
1004 int width, height;
1005 #ifdef USE_GTK
1006 if (FRAME_GTK_WIDGET (f))
1008 GdkWindow *w = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
1009 width = gdk_window_get_width (w);
1010 height = gdk_window_get_height (w);
1012 else
1013 #endif
1015 width = FRAME_PIXEL_WIDTH (f);
1016 height = FRAME_PIXEL_HEIGHT (f);
1017 if (! FRAME_EXTERNAL_TOOL_BAR (f))
1018 height += FRAME_TOOL_BAR_HEIGHT (f);
1019 if (! FRAME_EXTERNAL_MENU_BAR (f))
1020 height += FRAME_MENU_BAR_HEIGHT (f);
1023 if (width > 0 && height > 0)
1025 block_input();
1026 FRAME_CR_SURFACE (f) = cairo_image_surface_create
1027 (CAIRO_FORMAT_ARGB32, width, height);
1028 unblock_input();
1031 #endif /* USE_CAIRO */
1034 /* Start update of window W. */
1036 static void
1037 x_update_window_begin (struct window *w)
1039 struct frame *f = XFRAME (WINDOW_FRAME (w));
1040 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
1042 w->output_cursor = w->cursor;
1044 block_input ();
1046 if (f == hlinfo->mouse_face_mouse_frame)
1048 /* Don't do highlighting for mouse motion during the update. */
1049 hlinfo->mouse_face_defer = true;
1051 /* If F needs to be redrawn, simply forget about any prior mouse
1052 highlighting. */
1053 if (FRAME_GARBAGED_P (f))
1054 hlinfo->mouse_face_window = Qnil;
1057 unblock_input ();
1061 /* Draw a vertical window border from (x,y0) to (x,y1) */
1063 static void
1064 x_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
1066 struct frame *f = XFRAME (WINDOW_FRAME (w));
1067 struct face *face;
1069 face = FACE_FROM_ID_OR_NULL (f, VERTICAL_BORDER_FACE_ID);
1070 if (face)
1071 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
1072 face->foreground);
1074 #ifdef USE_CAIRO
1075 x_fill_rectangle (f, f->output_data.x->normal_gc, x, y0, 1, y1 - y0);
1076 #else
1077 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
1078 f->output_data.x->normal_gc, x, y0, x, y1);
1079 #endif
1082 /* Draw a window divider from (x0,y0) to (x1,y1) */
1084 static void
1085 x_draw_window_divider (struct window *w, int x0, int x1, int y0, int y1)
1087 struct frame *f = XFRAME (WINDOW_FRAME (w));
1088 struct face *face = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_FACE_ID);
1089 struct face *face_first
1090 = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
1091 struct face *face_last
1092 = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
1093 unsigned long color = face ? face->foreground : FRAME_FOREGROUND_PIXEL (f);
1094 unsigned long color_first = (face_first
1095 ? face_first->foreground
1096 : FRAME_FOREGROUND_PIXEL (f));
1097 unsigned long color_last = (face_last
1098 ? face_last->foreground
1099 : FRAME_FOREGROUND_PIXEL (f));
1100 Display *display = FRAME_X_DISPLAY (f);
1102 if ((y1 - y0 > x1 - x0) && (x1 - x0 >= 3))
1103 /* A vertical divider, at least three pixels wide: Draw first and
1104 last pixels differently. */
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 /* A horizontal divider, at least three pixels high: Draw first and
1118 last pixels differently. */
1120 XSetForeground (display, f->output_data.x->normal_gc, color_first);
1121 x_fill_rectangle (f, f->output_data.x->normal_gc,
1122 x0, y0, x1 - x0, 1);
1123 XSetForeground (display, f->output_data.x->normal_gc, color);
1124 x_fill_rectangle (f, f->output_data.x->normal_gc,
1125 x0, y0 + 1, x1 - x0, y1 - y0 - 2);
1126 XSetForeground (display, f->output_data.x->normal_gc, color_last);
1127 x_fill_rectangle (f, f->output_data.x->normal_gc,
1128 x0, y1 - 1, x1 - x0, 1);
1130 else
1132 /* In any other case do not draw the first and last pixels
1133 differently. */
1134 XSetForeground (display, f->output_data.x->normal_gc, color);
1135 x_fill_rectangle (f, f->output_data.x->normal_gc,
1136 x0, y0, x1 - x0, y1 - y0);
1140 /* End update of window W.
1142 Draw vertical borders between horizontally adjacent windows, and
1143 display W's cursor if CURSOR_ON_P is non-zero.
1145 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
1146 glyphs in mouse-face were overwritten. In that case we have to
1147 make sure that the mouse-highlight is properly redrawn.
1149 W may be a menu bar pseudo-window in case we don't have X toolkit
1150 support. Such windows don't have a cursor, so don't display it
1151 here. */
1153 static void
1154 x_update_window_end (struct window *w, bool cursor_on_p,
1155 bool mouse_face_overwritten_p)
1157 if (!w->pseudo_window_p)
1159 block_input ();
1161 if (cursor_on_p)
1162 display_and_set_cursor (w, true,
1163 w->output_cursor.hpos, w->output_cursor.vpos,
1164 w->output_cursor.x, w->output_cursor.y);
1166 if (draw_window_fringes (w, true))
1168 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
1169 x_draw_right_divider (w);
1170 else
1171 x_draw_vertical_border (w);
1174 unblock_input ();
1177 /* If a row with mouse-face was overwritten, arrange for
1178 XTframe_up_to_date to redisplay the mouse highlight. */
1179 if (mouse_face_overwritten_p)
1181 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
1183 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
1184 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
1185 hlinfo->mouse_face_window = Qnil;
1189 /* Show the frame back buffer. If frame is double-buffered,
1190 atomically publish to the user's screen graphics updates made since
1191 the last call to show_back_buffer. */
1192 static void
1193 show_back_buffer (struct frame *f)
1195 block_input ();
1196 if (FRAME_X_DOUBLE_BUFFERED_P (f))
1198 #ifdef HAVE_XDBE
1199 XdbeSwapInfo swap_info;
1200 memset (&swap_info, 0, sizeof (swap_info));
1201 swap_info.swap_window = FRAME_X_WINDOW (f);
1202 swap_info.swap_action = XdbeCopied;
1203 XdbeSwapBuffers (FRAME_X_DISPLAY (f), &swap_info, 1);
1204 #else
1205 eassert (!"should have back-buffer only with XDBE");
1206 #endif
1208 FRAME_X_NEED_BUFFER_FLIP (f) = false;
1209 unblock_input ();
1212 /* Updates back buffer and flushes changes to display. Called from
1213 minibuf read code. Note that we display the back buffer even if
1214 buffer flipping is blocked. */
1215 static void
1216 x_flip_and_flush (struct frame *f)
1218 block_input ();
1219 if (FRAME_X_NEED_BUFFER_FLIP (f))
1220 show_back_buffer (f);
1221 x_flush (f);
1222 unblock_input ();
1225 /* End update of frame F. This function is installed as a hook in
1226 update_end. */
1228 static void
1229 x_update_end (struct frame *f)
1231 /* Mouse highlight may be displayed again. */
1232 MOUSE_HL_INFO (f)->mouse_face_defer = false;
1234 #ifdef USE_CAIRO
1235 if (FRAME_CR_SURFACE (f))
1237 cairo_t *cr;
1238 cairo_surface_t *surface;
1239 int width, height;
1241 block_input ();
1242 width = FRAME_PIXEL_WIDTH (f);
1243 height = FRAME_PIXEL_HEIGHT (f);
1244 if (! FRAME_EXTERNAL_TOOL_BAR (f))
1245 height += FRAME_TOOL_BAR_HEIGHT (f);
1246 if (! FRAME_EXTERNAL_MENU_BAR (f))
1247 height += FRAME_MENU_BAR_HEIGHT (f);
1248 surface = cairo_xlib_surface_create (FRAME_X_DISPLAY (f),
1249 FRAME_X_DRAWABLE (f),
1250 FRAME_DISPLAY_INFO (f)->visual,
1251 width,
1252 height);
1253 cr = cairo_create (surface);
1254 cairo_surface_destroy (surface);
1256 cairo_set_source_surface (cr, FRAME_CR_SURFACE (f), 0, 0);
1257 cairo_paint (cr);
1258 cairo_destroy (cr);
1259 unblock_input ();
1261 #endif
1263 #ifndef XFlush
1264 block_input ();
1265 XFlush (FRAME_X_DISPLAY (f));
1266 unblock_input ();
1267 #endif
1270 /* This function is called from various places in xdisp.c
1271 whenever a complete update has been performed. */
1273 static void
1274 XTframe_up_to_date (struct frame *f)
1276 eassert (FRAME_X_P (f));
1277 block_input ();
1278 FRAME_MOUSE_UPDATE (f);
1279 if (!buffer_flipping_blocked_p () && FRAME_X_NEED_BUFFER_FLIP (f))
1280 show_back_buffer (f);
1281 unblock_input ();
1284 static void
1285 XTbuffer_flipping_unblocked_hook (struct frame *f)
1287 if (FRAME_X_NEED_BUFFER_FLIP (f))
1288 show_back_buffer (f);
1292 * x_clear_under_internal_border:
1294 * Clear area of frame F's internal border. If the internal border face
1295 * of F has been specified (is not null), fill the area with that face.
1297 void
1298 x_clear_under_internal_border (struct frame *f)
1300 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
1302 int border = FRAME_INTERNAL_BORDER_WIDTH (f);
1303 int width = FRAME_PIXEL_WIDTH (f);
1304 int height = FRAME_PIXEL_HEIGHT (f);
1305 #ifdef USE_GTK
1306 int margin = 0;
1307 #else
1308 int margin = FRAME_TOP_MARGIN_HEIGHT (f);
1309 #endif
1310 struct face *face = FACE_FROM_ID_OR_NULL (f, INTERNAL_BORDER_FACE_ID);
1312 block_input ();
1314 if (face)
1316 unsigned long color = face->background;
1317 Display *display = FRAME_X_DISPLAY (f);
1318 GC gc = f->output_data.x->normal_gc;
1320 XSetForeground (display, gc, color);
1321 x_fill_rectangle (f, gc, 0, margin, width, border);
1322 x_fill_rectangle (f, gc, 0, 0, border, height);
1323 x_fill_rectangle (f, gc, width - border, 0, border, height);
1324 x_fill_rectangle (f, gc, 0, height - border, width, border);
1325 XSetForeground (display, gc, FRAME_FOREGROUND_PIXEL (f));
1327 else
1329 x_clear_area (f, 0, 0, border, height);
1330 x_clear_area (f, 0, margin, width, border);
1331 x_clear_area (f, width - border, 0, border, height);
1332 x_clear_area (f, 0, height - border, width, border);
1335 unblock_input ();
1339 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
1340 arrow bitmaps, or clear the fringes if no bitmaps are required
1341 before DESIRED_ROW is made current. This function is called from
1342 update_window_line only if it is known that there are differences
1343 between bitmaps to be drawn between current row and DESIRED_ROW. */
1345 static void
1346 x_after_update_window_line (struct window *w, struct glyph_row *desired_row)
1348 eassert (w);
1350 if (!desired_row->mode_line_p && !w->pseudo_window_p)
1351 desired_row->redraw_fringe_bitmaps_p = true;
1353 #ifdef USE_X_TOOLKIT
1354 /* When a window has disappeared, make sure that no rest of
1355 full-width rows stays visible in the internal border. Could
1356 check here if updated window is the leftmost/rightmost window,
1357 but I guess it's not worth doing since vertically split windows
1358 are almost never used, internal border is rarely set, and the
1359 overhead is very small. */
1361 struct frame *f;
1362 int width, height;
1364 if (windows_or_buffers_changed
1365 && desired_row->full_width_p
1366 && (f = XFRAME (w->frame),
1367 width = FRAME_INTERNAL_BORDER_WIDTH (f),
1368 width != 0)
1369 && (height = desired_row->visible_height,
1370 height > 0))
1372 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
1373 struct face *face = FACE_FROM_ID_OR_NULL (f, INTERNAL_BORDER_FACE_ID);
1375 block_input ();
1376 if (face)
1378 unsigned long color = face->background;
1379 Display *display = FRAME_X_DISPLAY (f);
1380 GC gc = f->output_data.x->normal_gc;
1382 XSetForeground (display, gc, color);
1383 x_fill_rectangle (f, gc, 0, y, width, height);
1384 x_fill_rectangle (f, gc, FRAME_PIXEL_WIDTH (f) - width, y,
1385 width, height);
1386 XSetForeground (display, gc, FRAME_FOREGROUND_PIXEL (f));
1388 else
1390 x_clear_area (f, 0, y, width, height);
1391 x_clear_area (f, FRAME_PIXEL_WIDTH (f) - width, y, width, height);
1393 unblock_input ();
1396 #endif
1399 static void
1400 x_draw_fringe_bitmap (struct window *w, struct glyph_row *row, struct draw_fringe_bitmap_params *p)
1402 struct frame *f = XFRAME (WINDOW_FRAME (w));
1403 Display *display = FRAME_X_DISPLAY (f);
1404 GC gc = f->output_data.x->normal_gc;
1405 struct face *face = p->face;
1407 /* Must clip because of partially visible lines. */
1408 x_clip_to_row (w, row, ANY_AREA, gc);
1410 if (p->bx >= 0 && !p->overlay_p)
1412 /* In case the same realized face is used for fringes and
1413 for something displayed in the text (e.g. face `region' on
1414 mono-displays, the fill style may have been changed to
1415 FillSolid in x_draw_glyph_string_background. */
1416 if (face->stipple)
1417 XSetFillStyle (display, face->gc, FillOpaqueStippled);
1418 else
1419 XSetForeground (display, face->gc, face->background);
1421 x_fill_rectangle (f, face->gc, p->bx, p->by, p->nx, p->ny);
1423 if (!face->stipple)
1424 XSetForeground (display, face->gc, face->foreground);
1427 #ifdef USE_CAIRO
1428 if (p->which && p->which < max_fringe_bmp)
1430 XGCValues gcv;
1432 XGetGCValues (display, gc, GCForeground | GCBackground, &gcv);
1433 XSetForeground (display, gc, (p->cursor_p
1434 ? (p->overlay_p ? face->background
1435 : f->output_data.x->cursor_pixel)
1436 : face->foreground));
1437 XSetBackground (display, gc, face->background);
1438 x_cr_draw_image (f, gc, fringe_bmp[p->which], 0, p->dh,
1439 p->wd, p->h, p->x, p->y, p->overlay_p);
1440 XSetForeground (display, gc, gcv.foreground);
1441 XSetBackground (display, gc, gcv.background);
1443 #else /* not USE_CAIRO */
1444 if (p->which)
1446 Drawable drawable = FRAME_X_DRAWABLE (f);
1447 char *bits;
1448 Pixmap pixmap, clipmask = (Pixmap) 0;
1449 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
1450 XGCValues gcv;
1452 if (p->wd > 8)
1453 bits = (char *) (p->bits + p->dh);
1454 else
1455 bits = (char *) p->bits + p->dh;
1457 /* Draw the bitmap. I believe these small pixmaps can be cached
1458 by the server. */
1459 pixmap = XCreatePixmapFromBitmapData (display, drawable, bits, p->wd, p->h,
1460 (p->cursor_p
1461 ? (p->overlay_p ? face->background
1462 : f->output_data.x->cursor_pixel)
1463 : face->foreground),
1464 face->background, depth);
1466 if (p->overlay_p)
1468 clipmask = XCreatePixmapFromBitmapData (display,
1469 FRAME_DISPLAY_INFO (f)->root_window,
1470 bits, p->wd, p->h,
1471 1, 0, 1);
1472 gcv.clip_mask = clipmask;
1473 gcv.clip_x_origin = p->x;
1474 gcv.clip_y_origin = p->y;
1475 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
1478 XCopyArea (display, pixmap, drawable, gc, 0, 0,
1479 p->wd, p->h, p->x, p->y);
1480 XFreePixmap (display, pixmap);
1482 if (p->overlay_p)
1484 gcv.clip_mask = (Pixmap) 0;
1485 XChangeGC (display, gc, GCClipMask, &gcv);
1486 XFreePixmap (display, clipmask);
1489 #endif /* not USE_CAIRO */
1491 x_reset_clip_rectangles (f, gc);
1494 /***********************************************************************
1495 Glyph display
1496 ***********************************************************************/
1500 static void x_set_glyph_string_clipping (struct glyph_string *);
1501 static void x_set_glyph_string_gc (struct glyph_string *);
1502 static void x_draw_glyph_string_foreground (struct glyph_string *);
1503 static void x_draw_composite_glyph_string_foreground (struct glyph_string *);
1504 static void x_draw_glyph_string_box (struct glyph_string *);
1505 static void x_draw_glyph_string (struct glyph_string *);
1506 static _Noreturn void x_delete_glyphs (struct frame *, int);
1507 static void x_compute_glyph_string_overhangs (struct glyph_string *);
1508 static void x_set_cursor_gc (struct glyph_string *);
1509 static void x_set_mode_line_face_gc (struct glyph_string *);
1510 static void x_set_mouse_face_gc (struct glyph_string *);
1511 static bool x_alloc_lighter_color (struct frame *, Display *, Colormap,
1512 unsigned long *, double, int);
1513 static void x_setup_relief_color (struct frame *, struct relief *,
1514 double, int, unsigned long);
1515 static void x_setup_relief_colors (struct glyph_string *);
1516 static void x_draw_image_glyph_string (struct glyph_string *);
1517 static void x_draw_image_relief (struct glyph_string *);
1518 static void x_draw_image_foreground (struct glyph_string *);
1519 static void x_draw_image_foreground_1 (struct glyph_string *, Pixmap);
1520 static void x_clear_glyph_string_rect (struct glyph_string *, int,
1521 int, int, int);
1522 static void x_draw_relief_rect (struct frame *, int, int, int, int,
1523 int, bool, bool, bool, bool, bool,
1524 XRectangle *);
1525 static void x_draw_box_rect (struct glyph_string *, int, int, int, int,
1526 int, bool, bool, XRectangle *);
1527 static void x_scroll_bar_clear (struct frame *);
1529 #ifdef GLYPH_DEBUG
1530 static void x_check_font (struct frame *, struct font *);
1531 #endif
1534 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
1535 face. */
1537 static void
1538 x_set_cursor_gc (struct glyph_string *s)
1540 if (s->font == FRAME_FONT (s->f)
1541 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
1542 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
1543 && !s->cmp)
1544 s->gc = s->f->output_data.x->cursor_gc;
1545 else
1547 /* Cursor on non-default face: must merge. */
1548 XGCValues xgcv;
1549 unsigned long mask;
1551 xgcv.background = s->f->output_data.x->cursor_pixel;
1552 xgcv.foreground = s->face->background;
1554 /* If the glyph would be invisible, try a different foreground. */
1555 if (xgcv.foreground == xgcv.background)
1556 xgcv.foreground = s->face->foreground;
1557 if (xgcv.foreground == xgcv.background)
1558 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1559 if (xgcv.foreground == xgcv.background)
1560 xgcv.foreground = s->face->foreground;
1562 /* Make sure the cursor is distinct from text in this face. */
1563 if (xgcv.background == s->face->background
1564 && xgcv.foreground == s->face->foreground)
1566 xgcv.background = s->face->foreground;
1567 xgcv.foreground = s->face->background;
1570 IF_DEBUG (x_check_font (s->f, s->font));
1571 xgcv.graphics_exposures = False;
1572 mask = GCForeground | GCBackground | GCGraphicsExposures;
1574 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1575 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1576 mask, &xgcv);
1577 else
1578 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1579 = XCreateGC (s->display, FRAME_X_DRAWABLE (s->f), mask, &xgcv);
1581 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1586 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1588 static void
1589 x_set_mouse_face_gc (struct glyph_string *s)
1591 int face_id;
1592 struct face *face;
1594 /* What face has to be used last for the mouse face? */
1595 face_id = MOUSE_HL_INFO (s->f)->mouse_face_face_id;
1596 face = FACE_FROM_ID_OR_NULL (s->f, face_id);
1597 if (face == NULL)
1598 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1600 if (s->first_glyph->type == CHAR_GLYPH)
1601 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1602 else
1603 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1604 s->face = FACE_FROM_ID (s->f, face_id);
1605 prepare_face_for_display (s->f, s->face);
1607 if (s->font == s->face->font)
1608 s->gc = s->face->gc;
1609 else
1611 /* Otherwise construct scratch_cursor_gc with values from FACE
1612 except for FONT. */
1613 XGCValues xgcv;
1614 unsigned long mask;
1616 xgcv.background = s->face->background;
1617 xgcv.foreground = s->face->foreground;
1618 xgcv.graphics_exposures = False;
1619 mask = GCForeground | GCBackground | GCGraphicsExposures;
1621 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1622 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1623 mask, &xgcv);
1624 else
1625 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1626 = XCreateGC (s->display, FRAME_X_DRAWABLE (s->f), mask, &xgcv);
1628 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1631 eassert (s->gc != 0);
1635 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1636 Faces to use in the mode line have already been computed when the
1637 matrix was built, so there isn't much to do, here. */
1639 static void
1640 x_set_mode_line_face_gc (struct glyph_string *s)
1642 s->gc = s->face->gc;
1646 /* Set S->gc of glyph string S for drawing that glyph string. Set
1647 S->stippled_p to a non-zero value if the face of S has a stipple
1648 pattern. */
1650 static void
1651 x_set_glyph_string_gc (struct glyph_string *s)
1653 prepare_face_for_display (s->f, s->face);
1655 if (s->hl == DRAW_NORMAL_TEXT)
1657 s->gc = s->face->gc;
1658 s->stippled_p = s->face->stipple != 0;
1660 else if (s->hl == DRAW_INVERSE_VIDEO)
1662 x_set_mode_line_face_gc (s);
1663 s->stippled_p = s->face->stipple != 0;
1665 else if (s->hl == DRAW_CURSOR)
1667 x_set_cursor_gc (s);
1668 s->stippled_p = false;
1670 else if (s->hl == DRAW_MOUSE_FACE)
1672 x_set_mouse_face_gc (s);
1673 s->stippled_p = s->face->stipple != 0;
1675 else if (s->hl == DRAW_IMAGE_RAISED
1676 || s->hl == DRAW_IMAGE_SUNKEN)
1678 s->gc = s->face->gc;
1679 s->stippled_p = s->face->stipple != 0;
1681 else
1682 emacs_abort ();
1684 /* GC must have been set. */
1685 eassert (s->gc != 0);
1689 /* Set clipping for output of glyph string S. S may be part of a mode
1690 line or menu if we don't have X toolkit support. */
1692 static void
1693 x_set_glyph_string_clipping (struct glyph_string *s)
1695 XRectangle *r = s->clip;
1696 int n = get_glyph_string_clip_rects (s, r, 2);
1698 if (n > 0)
1699 x_set_clip_rectangles (s->f, s->gc, r, n);
1700 s->num_clips = n;
1704 /* Set SRC's clipping for output of glyph string DST. This is called
1705 when we are drawing DST's left_overhang or right_overhang only in
1706 the area of SRC. */
1708 static void
1709 x_set_glyph_string_clipping_exactly (struct glyph_string *src, struct glyph_string *dst)
1711 XRectangle r;
1713 r.x = src->x;
1714 r.width = src->width;
1715 r.y = src->y;
1716 r.height = src->height;
1717 dst->clip[0] = r;
1718 dst->num_clips = 1;
1719 x_set_clip_rectangles (dst->f, dst->gc, &r, 1);
1723 /* RIF:
1724 Compute left and right overhang of glyph string S. */
1726 static void
1727 x_compute_glyph_string_overhangs (struct glyph_string *s)
1729 if (s->cmp == NULL
1730 && (s->first_glyph->type == CHAR_GLYPH
1731 || s->first_glyph->type == COMPOSITE_GLYPH))
1733 struct font_metrics metrics;
1735 if (s->first_glyph->type == CHAR_GLYPH)
1737 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1738 struct font *font = s->font;
1739 int i;
1741 for (i = 0; i < s->nchars; i++)
1742 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1743 font->driver->text_extents (font, code, s->nchars, &metrics);
1745 else
1747 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1749 composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
1751 s->right_overhang = (metrics.rbearing > metrics.width
1752 ? metrics.rbearing - metrics.width : 0);
1753 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1755 else if (s->cmp)
1757 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1758 s->left_overhang = - s->cmp->lbearing;
1763 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1765 static void
1766 x_clear_glyph_string_rect (struct glyph_string *s, int x, int y, int w, int h)
1768 XGCValues xgcv;
1769 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1770 XSetForeground (s->display, s->gc, xgcv.background);
1771 x_fill_rectangle (s->f, s->gc, x, y, w, h);
1772 XSetForeground (s->display, s->gc, xgcv.foreground);
1776 /* Draw the background of glyph_string S. If S->background_filled_p
1777 is non-zero don't draw it. FORCE_P non-zero means draw the
1778 background even if it wouldn't be drawn normally. This is used
1779 when a string preceding S draws into the background of S, or S
1780 contains the first component of a composition. */
1782 static void
1783 x_draw_glyph_string_background (struct glyph_string *s, bool force_p)
1785 /* Nothing to do if background has already been drawn or if it
1786 shouldn't be drawn in the first place. */
1787 if (!s->background_filled_p)
1789 int box_line_width = max (s->face->box_line_width, 0);
1791 if (s->stippled_p)
1793 /* Fill background with a stipple pattern. */
1794 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1795 x_fill_rectangle (s->f, s->gc, s->x,
1796 s->y + box_line_width,
1797 s->background_width,
1798 s->height - 2 * box_line_width);
1799 XSetFillStyle (s->display, s->gc, FillSolid);
1800 s->background_filled_p = true;
1802 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1803 /* When xdisp.c ignores FONT_HEIGHT, we cannot trust
1804 font dimensions, since the actual glyphs might be
1805 much smaller. So in that case we always clear the
1806 rectangle with background color. */
1807 || FONT_TOO_HIGH (s->font)
1808 || s->font_not_found_p
1809 || s->extends_to_end_of_line_p
1810 || force_p)
1812 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1813 s->background_width,
1814 s->height - 2 * box_line_width);
1815 s->background_filled_p = true;
1821 /* Draw the foreground of glyph string S. */
1823 static void
1824 x_draw_glyph_string_foreground (struct glyph_string *s)
1826 int i, x;
1828 /* If first glyph of S has a left box line, start drawing the text
1829 of S to the right of that box line. */
1830 if (s->face->box != FACE_NO_BOX
1831 && s->first_glyph->left_box_line_p)
1832 x = s->x + eabs (s->face->box_line_width);
1833 else
1834 x = s->x;
1836 /* Draw characters of S as rectangles if S's font could not be
1837 loaded. */
1838 if (s->font_not_found_p)
1840 for (i = 0; i < s->nchars; ++i)
1842 struct glyph *g = s->first_glyph + i;
1843 x_draw_rectangle (s->f,
1844 s->gc, x, s->y, g->pixel_width - 1,
1845 s->height - 1);
1846 x += g->pixel_width;
1849 else
1851 struct font *font = s->font;
1852 int boff = font->baseline_offset;
1853 int y;
1855 if (font->vertical_centering)
1856 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1858 y = s->ybase - boff;
1859 if (s->for_overlaps
1860 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1861 font->driver->draw (s, 0, s->nchars, x, y, false);
1862 else
1863 font->driver->draw (s, 0, s->nchars, x, y, true);
1864 if (s->face->overstrike)
1865 font->driver->draw (s, 0, s->nchars, x + 1, y, false);
1869 /* Draw the foreground of composite glyph string S. */
1871 static void
1872 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1874 int i, j, x;
1875 struct font *font = s->font;
1877 /* If first glyph of S has a left box line, start drawing the text
1878 of S to the right of that box line. */
1879 if (s->face && s->face->box != FACE_NO_BOX
1880 && s->first_glyph->left_box_line_p)
1881 x = s->x + eabs (s->face->box_line_width);
1882 else
1883 x = s->x;
1885 /* S is a glyph string for a composition. S->cmp_from is the index
1886 of the first character drawn for glyphs of this composition.
1887 S->cmp_from == 0 means we are drawing the very first character of
1888 this composition. */
1890 /* Draw a rectangle for the composition if the font for the very
1891 first character of the composition could not be loaded. */
1892 if (s->font_not_found_p)
1894 if (s->cmp_from == 0)
1895 x_draw_rectangle (s->f, s->gc, x, s->y,
1896 s->width - 1, s->height - 1);
1898 else if (! s->first_glyph->u.cmp.automatic)
1900 int y = s->ybase;
1902 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1903 /* TAB in a composition means display glyphs with padding
1904 space on the left or right. */
1905 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1907 int xx = x + s->cmp->offsets[j * 2];
1908 int yy = y - s->cmp->offsets[j * 2 + 1];
1910 font->driver->draw (s, j, j + 1, xx, yy, false);
1911 if (s->face->overstrike)
1912 font->driver->draw (s, j, j + 1, xx + 1, yy, false);
1915 else
1917 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1918 Lisp_Object glyph;
1919 int y = s->ybase;
1920 int width = 0;
1922 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1924 glyph = LGSTRING_GLYPH (gstring, i);
1925 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1926 width += LGLYPH_WIDTH (glyph);
1927 else
1929 int xoff, yoff, wadjust;
1931 if (j < i)
1933 font->driver->draw (s, j, i, x, y, false);
1934 if (s->face->overstrike)
1935 font->driver->draw (s, j, i, x + 1, y, false);
1936 x += width;
1938 xoff = LGLYPH_XOFF (glyph);
1939 yoff = LGLYPH_YOFF (glyph);
1940 wadjust = LGLYPH_WADJUST (glyph);
1941 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, false);
1942 if (s->face->overstrike)
1943 font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff,
1944 false);
1945 x += wadjust;
1946 j = i + 1;
1947 width = 0;
1950 if (j < i)
1952 font->driver->draw (s, j, i, x, y, false);
1953 if (s->face->overstrike)
1954 font->driver->draw (s, j, i, x + 1, y, false);
1960 /* Draw the foreground of glyph string S for glyphless characters. */
1962 static void
1963 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1965 struct glyph *glyph = s->first_glyph;
1966 XChar2b char2b[8];
1967 int x, i, j;
1969 /* If first glyph of S has a left box line, start drawing the text
1970 of S to the right of that box line. */
1971 if (s->face && s->face->box != FACE_NO_BOX
1972 && s->first_glyph->left_box_line_p)
1973 x = s->x + eabs (s->face->box_line_width);
1974 else
1975 x = s->x;
1977 s->char2b = char2b;
1979 for (i = 0; i < s->nchars; i++, glyph++)
1981 char buf[7], *str = NULL;
1982 int len = glyph->u.glyphless.len;
1984 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1986 if (len > 0
1987 && CHAR_TABLE_P (Vglyphless_char_display)
1988 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1989 >= 1))
1991 Lisp_Object acronym
1992 = (! glyph->u.glyphless.for_no_font
1993 ? CHAR_TABLE_REF (Vglyphless_char_display,
1994 glyph->u.glyphless.ch)
1995 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
1996 if (STRINGP (acronym))
1997 str = SSDATA (acronym);
2000 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
2002 unsigned int ch = glyph->u.glyphless.ch;
2003 eassume (ch <= MAX_CHAR);
2004 sprintf (buf, "%0*X", ch < 0x10000 ? 4 : 6, ch);
2005 str = buf;
2008 if (str)
2010 int upper_len = (len + 1) / 2;
2011 unsigned code;
2013 /* It is assured that all LEN characters in STR is ASCII. */
2014 for (j = 0; j < len; j++)
2016 code = s->font->driver->encode_char (s->font, str[j]);
2017 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
2019 s->font->driver->draw (s, 0, upper_len,
2020 x + glyph->slice.glyphless.upper_xoff,
2021 s->ybase + glyph->slice.glyphless.upper_yoff,
2022 false);
2023 s->font->driver->draw (s, upper_len, len,
2024 x + glyph->slice.glyphless.lower_xoff,
2025 s->ybase + glyph->slice.glyphless.lower_yoff,
2026 false);
2028 if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
2029 x_draw_rectangle (s->f, s->gc,
2030 x, s->ybase - glyph->ascent,
2031 glyph->pixel_width - 1,
2032 glyph->ascent + glyph->descent - 1);
2033 x += glyph->pixel_width;
2037 #ifdef USE_X_TOOLKIT
2039 #ifdef USE_LUCID
2041 /* Return the frame on which widget WIDGET is used.. Abort if frame
2042 cannot be determined. */
2044 static struct frame *
2045 x_frame_of_widget (Widget widget)
2047 struct x_display_info *dpyinfo;
2048 Lisp_Object tail, frame;
2049 struct frame *f;
2051 dpyinfo = x_display_info_for_display (XtDisplay (widget));
2053 /* Find the top-level shell of the widget. Note that this function
2054 can be called when the widget is not yet realized, so XtWindow
2055 (widget) == 0. That's the reason we can't simply use
2056 x_any_window_to_frame. */
2057 while (!XtIsTopLevelShell (widget))
2058 widget = XtParent (widget);
2060 /* Look for a frame with that top-level widget. Allocate the color
2061 on that frame to get the right gamma correction value. */
2062 FOR_EACH_FRAME (tail, frame)
2064 f = XFRAME (frame);
2065 if (FRAME_X_P (f)
2066 && f->output_data.nothing != 1
2067 && FRAME_DISPLAY_INFO (f) == dpyinfo
2068 && f->output_data.x->widget == widget)
2069 return f;
2071 emacs_abort ();
2074 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
2075 or DELTA. Try a color with RGB values multiplied by FACTOR first.
2076 If this produces the same color as PIXEL, try a color where all RGB
2077 values have DELTA added. Return the allocated color in *PIXEL.
2078 DISPLAY is the X display, CMAP is the colormap to operate on.
2079 Value is true if successful. */
2081 bool
2082 x_alloc_lighter_color_for_widget (Widget widget, Display *display, Colormap cmap,
2083 unsigned long *pixel, double factor, int delta)
2085 struct frame *f = x_frame_of_widget (widget);
2086 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
2089 #endif /* USE_LUCID */
2092 /* Structure specifying which arguments should be passed by Xt to
2093 cvt_string_to_pixel. We want the widget's screen and colormap. */
2095 static XtConvertArgRec cvt_string_to_pixel_args[] =
2097 {XtWidgetBaseOffset, (XtPointer) offsetof (WidgetRec, core.screen),
2098 sizeof (Screen *)},
2099 {XtWidgetBaseOffset, (XtPointer) offsetof (WidgetRec, core.colormap),
2100 sizeof (Colormap)}
2104 /* The address of this variable is returned by
2105 cvt_string_to_pixel. */
2107 static Pixel cvt_string_to_pixel_value;
2110 /* Convert a color name to a pixel color.
2112 DPY is the display we are working on.
2114 ARGS is an array of *NARGS XrmValue structures holding additional
2115 information about the widget for which the conversion takes place.
2116 The contents of this array are determined by the specification
2117 in cvt_string_to_pixel_args.
2119 FROM is a pointer to an XrmValue which points to the color name to
2120 convert. TO is an XrmValue in which to return the pixel color.
2122 CLOSURE_RET is a pointer to user-data, in which we record if
2123 we allocated the color or not.
2125 Value is True if successful, False otherwise. */
2127 static Boolean
2128 cvt_string_to_pixel (Display *dpy, XrmValue *args, Cardinal *nargs,
2129 XrmValue *from, XrmValue *to,
2130 XtPointer *closure_ret)
2132 Screen *screen;
2133 Colormap cmap;
2134 Pixel pixel;
2135 String color_name;
2136 XColor color;
2138 if (*nargs != 2)
2140 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
2141 "wrongParameters", "cvt_string_to_pixel",
2142 "XtToolkitError",
2143 "Screen and colormap args required", NULL, NULL);
2144 return False;
2147 screen = *(Screen **) args[0].addr;
2148 cmap = *(Colormap *) args[1].addr;
2149 color_name = (String) from->addr;
2151 if (strcmp (color_name, XtDefaultBackground) == 0)
2153 *closure_ret = (XtPointer) False;
2154 pixel = WhitePixelOfScreen (screen);
2156 else if (strcmp (color_name, XtDefaultForeground) == 0)
2158 *closure_ret = (XtPointer) False;
2159 pixel = BlackPixelOfScreen (screen);
2161 else if (XParseColor (dpy, cmap, color_name, &color)
2162 && x_alloc_nearest_color_1 (dpy, cmap, &color))
2164 pixel = color.pixel;
2165 *closure_ret = (XtPointer) True;
2167 else
2169 String params[1];
2170 Cardinal nparams = 1;
2172 params[0] = color_name;
2173 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
2174 "badValue", "cvt_string_to_pixel",
2175 "XtToolkitError", "Invalid color '%s'",
2176 params, &nparams);
2177 return False;
2180 if (to->addr != NULL)
2182 if (to->size < sizeof (Pixel))
2184 to->size = sizeof (Pixel);
2185 return False;
2188 *(Pixel *) to->addr = pixel;
2190 else
2192 cvt_string_to_pixel_value = pixel;
2193 to->addr = (XtPointer) &cvt_string_to_pixel_value;
2196 to->size = sizeof (Pixel);
2197 return True;
2201 /* Free a pixel color which was previously allocated via
2202 cvt_string_to_pixel. This is registered as the destructor
2203 for this type of resource via XtSetTypeConverter.
2205 APP is the application context in which we work.
2207 TO is a pointer to an XrmValue holding the color to free.
2208 CLOSURE is the value we stored in CLOSURE_RET for this color
2209 in cvt_string_to_pixel.
2211 ARGS and NARGS are like for cvt_string_to_pixel. */
2213 static void
2214 cvt_pixel_dtor (XtAppContext app, XrmValuePtr to, XtPointer closure, XrmValuePtr args,
2215 Cardinal *nargs)
2217 if (*nargs != 2)
2219 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
2220 "XtToolkitError",
2221 "Screen and colormap arguments required",
2222 NULL, NULL);
2224 else if (closure != NULL)
2226 /* We did allocate the pixel, so free it. */
2227 Screen *screen = *(Screen **) args[0].addr;
2228 Colormap cmap = *(Colormap *) args[1].addr;
2229 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
2230 (Pixel *) to->addr, 1);
2235 #endif /* USE_X_TOOLKIT */
2238 /* Value is an array of XColor structures for the contents of the
2239 color map of display DPY. Set *NCELLS to the size of the array.
2240 Note that this probably shouldn't be called for large color maps,
2241 say a 24-bit TrueColor map. */
2243 static const XColor *
2244 x_color_cells (Display *dpy, int *ncells)
2246 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
2247 eassume (dpyinfo);
2249 if (dpyinfo->color_cells == NULL)
2251 Screen *screen = dpyinfo->screen;
2252 int ncolor_cells = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
2253 int i;
2255 dpyinfo->color_cells = xnmalloc (ncolor_cells,
2256 sizeof *dpyinfo->color_cells);
2257 dpyinfo->ncolor_cells = ncolor_cells;
2259 for (i = 0; i < ncolor_cells; ++i)
2260 dpyinfo->color_cells[i].pixel = i;
2262 XQueryColors (dpy, dpyinfo->cmap,
2263 dpyinfo->color_cells, ncolor_cells);
2266 *ncells = dpyinfo->ncolor_cells;
2267 return dpyinfo->color_cells;
2271 /* On frame F, translate pixel colors to RGB values for the NCOLORS
2272 colors in COLORS. Use cached information, if available. */
2274 void
2275 x_query_colors (struct frame *f, XColor *colors, int ncolors)
2277 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2279 if (dpyinfo->red_bits > 0)
2281 /* For TrueColor displays, we can decompose the RGB value
2282 directly. */
2283 int i;
2284 unsigned int rmult, gmult, bmult;
2285 unsigned int rmask, gmask, bmask;
2287 rmask = (1 << dpyinfo->red_bits) - 1;
2288 gmask = (1 << dpyinfo->green_bits) - 1;
2289 bmask = (1 << dpyinfo->blue_bits) - 1;
2290 /* If we're widening, for example, 8 bits in the pixel value to
2291 16 bits for the separate-color representation, we want to
2292 extrapolate the lower bits based on those bits available --
2293 in other words, we'd like 0xff to become 0xffff instead of
2294 the 0xff00 we'd get by just zero-filling the lower bits.
2296 We generate a 32-bit scaled-up value and shift it, in case
2297 the bit count doesn't divide 16 evenly (e.g., when dealing
2298 with a 3-3-2 bit RGB display), to get more of the lower bits
2299 correct.
2301 Should we cache the multipliers in dpyinfo? Maybe
2302 special-case the 8-8-8 common case? */
2303 rmult = 0xffffffff / rmask;
2304 gmult = 0xffffffff / gmask;
2305 bmult = 0xffffffff / bmask;
2307 for (i = 0; i < ncolors; ++i)
2309 unsigned int r, g, b;
2310 unsigned long pixel = colors[i].pixel;
2312 r = (pixel >> dpyinfo->red_offset) & rmask;
2313 g = (pixel >> dpyinfo->green_offset) & gmask;
2314 b = (pixel >> dpyinfo->blue_offset) & bmask;
2316 colors[i].red = (r * rmult) >> 16;
2317 colors[i].green = (g * gmult) >> 16;
2318 colors[i].blue = (b * bmult) >> 16;
2320 return;
2323 if (dpyinfo->color_cells)
2325 int i;
2326 for (i = 0; i < ncolors; ++i)
2328 unsigned long pixel = colors[i].pixel;
2329 eassert (pixel < dpyinfo->ncolor_cells);
2330 eassert (dpyinfo->color_cells[pixel].pixel == pixel);
2331 colors[i] = dpyinfo->color_cells[pixel];
2333 return;
2336 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
2340 /* On frame F, translate pixel color to RGB values for the color in
2341 COLOR. Use cached information, if available. */
2343 void
2344 x_query_color (struct frame *f, XColor *color)
2346 x_query_colors (f, color, 1);
2350 /* On frame F, translate the color name to RGB values. Use cached
2351 information, if possible.
2353 Note that there is currently no way to clean old entries out of the
2354 cache. However, it is limited to names in the server's database,
2355 and names we've actually looked up; list-colors-display is probably
2356 the most color-intensive case we're likely to hit. */
2358 Status x_parse_color (struct frame *f, const char *color_name,
2359 XColor *color)
2361 Display *dpy = FRAME_X_DISPLAY (f);
2362 Colormap cmap = FRAME_X_COLORMAP (f);
2363 struct color_name_cache_entry *cache_entry;
2365 if (color_name[0] == '#')
2367 /* The hex form is parsed directly by XParseColor without
2368 talking to the X server. No need for caching. */
2369 return XParseColor (dpy, cmap, color_name, color);
2372 for (cache_entry = FRAME_DISPLAY_INFO (f)->color_names; cache_entry;
2373 cache_entry = cache_entry->next)
2375 if (!xstrcasecmp(cache_entry->name, color_name))
2377 *color = cache_entry->rgb;
2378 return 1;
2382 if (XParseColor (dpy, cmap, color_name, color) == 0)
2383 /* No caching of negative results, currently. */
2384 return 0;
2386 cache_entry = xzalloc (sizeof *cache_entry);
2387 cache_entry->rgb = *color;
2388 cache_entry->name = xstrdup (color_name);
2389 cache_entry->next = FRAME_DISPLAY_INFO (f)->color_names;
2390 FRAME_DISPLAY_INFO (f)->color_names = cache_entry;
2391 return 1;
2395 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
2396 exact match can't be allocated, try the nearest color available.
2397 Value is true if successful. Set *COLOR to the color
2398 allocated. */
2400 static bool
2401 x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color)
2403 bool rc;
2405 rc = XAllocColor (dpy, cmap, color) != 0;
2406 if (rc == 0)
2408 /* If we got to this point, the colormap is full, so we're going
2409 to try to get the next closest color. The algorithm used is
2410 a least-squares matching, which is what X uses for closest
2411 color matching with StaticColor visuals. */
2412 int nearest, i;
2413 int max_color_delta = 255;
2414 int max_delta = 3 * max_color_delta;
2415 int nearest_delta = max_delta + 1;
2416 int ncells;
2417 const XColor *cells = x_color_cells (dpy, &ncells);
2419 for (nearest = i = 0; i < ncells; ++i)
2421 int dred = (color->red >> 8) - (cells[i].red >> 8);
2422 int dgreen = (color->green >> 8) - (cells[i].green >> 8);
2423 int dblue = (color->blue >> 8) - (cells[i].blue >> 8);
2424 int delta = dred * dred + dgreen * dgreen + dblue * dblue;
2426 if (delta < nearest_delta)
2428 nearest = i;
2429 nearest_delta = delta;
2433 color->red = cells[nearest].red;
2434 color->green = cells[nearest].green;
2435 color->blue = cells[nearest].blue;
2436 rc = XAllocColor (dpy, cmap, color) != 0;
2438 else
2440 /* If allocation succeeded, and the allocated pixel color is not
2441 equal to a cached pixel color recorded earlier, there was a
2442 change in the colormap, so clear the color cache. */
2443 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
2444 eassume (dpyinfo);
2446 if (dpyinfo->color_cells)
2448 XColor *cached_color = &dpyinfo->color_cells[color->pixel];
2449 if (cached_color->red != color->red
2450 || cached_color->blue != color->blue
2451 || cached_color->green != color->green)
2453 xfree (dpyinfo->color_cells);
2454 dpyinfo->color_cells = NULL;
2455 dpyinfo->ncolor_cells = 0;
2460 #ifdef DEBUG_X_COLORS
2461 if (rc)
2462 register_color (color->pixel);
2463 #endif /* DEBUG_X_COLORS */
2465 return rc;
2469 /* Allocate the color COLOR->pixel on frame F, colormap CMAP, after
2470 gamma correction. If an exact match can't be allocated, try the
2471 nearest color available. Value is true if successful. Set *COLOR
2472 to the color allocated. */
2474 bool
2475 x_alloc_nearest_color (struct frame *f, Colormap cmap, XColor *color)
2477 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2479 gamma_correct (f, color);
2481 if (dpyinfo->red_bits > 0)
2483 color->pixel = x_make_truecolor_pixel (dpyinfo,
2484 color->red,
2485 color->green,
2486 color->blue);
2487 return true;
2490 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
2494 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
2495 It's necessary to do this instead of just using PIXEL directly to
2496 get color reference counts right. */
2498 unsigned long
2499 x_copy_color (struct frame *f, unsigned long pixel)
2501 XColor color;
2503 /* If display has an immutable color map, freeing colors is not
2504 necessary and some servers don't allow it. Since we won't free a
2505 color once we've allocated it, we don't need to re-allocate it to
2506 maintain the server's reference count. */
2507 if (!x_mutable_colormap (FRAME_X_VISUAL (f)))
2508 return pixel;
2510 color.pixel = pixel;
2511 block_input ();
2512 /* The color could still be found in the color_cells array. */
2513 x_query_color (f, &color);
2514 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
2515 unblock_input ();
2516 #ifdef DEBUG_X_COLORS
2517 register_color (pixel);
2518 #endif
2519 return color.pixel;
2523 /* Brightness beyond which a color won't have its highlight brightness
2524 boosted.
2526 Nominally, highlight colors for `3d' faces are calculated by
2527 brightening an object's color by a constant scale factor, but this
2528 doesn't yield good results for dark colors, so for colors who's
2529 brightness is less than this value (on a scale of 0-65535) have an
2530 use an additional additive factor.
2532 The value here is set so that the default menu-bar/mode-line color
2533 (grey75) will not have its highlights changed at all. */
2534 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
2537 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
2538 or DELTA. Try a color with RGB values multiplied by FACTOR first.
2539 If this produces the same color as PIXEL, try a color where all RGB
2540 values have DELTA added. Return the allocated color in *PIXEL.
2541 DISPLAY is the X display, CMAP is the colormap to operate on.
2542 Value is non-zero if successful. */
2544 static bool
2545 x_alloc_lighter_color (struct frame *f, Display *display, Colormap cmap,
2546 unsigned long *pixel, double factor, int delta)
2548 XColor color, new;
2549 long bright;
2550 bool success_p;
2552 /* Get RGB color values. */
2553 color.pixel = *pixel;
2554 x_query_color (f, &color);
2556 /* Change RGB values by specified FACTOR. Avoid overflow! */
2557 eassert (factor >= 0);
2558 new.red = min (0xffff, factor * color.red);
2559 new.green = min (0xffff, factor * color.green);
2560 new.blue = min (0xffff, factor * color.blue);
2562 /* Calculate brightness of COLOR. */
2563 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
2565 /* We only boost colors that are darker than
2566 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
2567 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
2568 /* Make an additive adjustment to NEW, because it's dark enough so
2569 that scaling by FACTOR alone isn't enough. */
2571 /* How far below the limit this color is (0 - 1, 1 being darker). */
2572 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
2573 /* The additive adjustment. */
2574 int min_delta = delta * dimness * factor / 2;
2576 if (factor < 1)
2578 new.red = max (0, new.red - min_delta);
2579 new.green = max (0, new.green - min_delta);
2580 new.blue = max (0, new.blue - min_delta);
2582 else
2584 new.red = min (0xffff, min_delta + new.red);
2585 new.green = min (0xffff, min_delta + new.green);
2586 new.blue = min (0xffff, min_delta + new.blue);
2590 /* Try to allocate the color. */
2591 success_p = x_alloc_nearest_color (f, cmap, &new);
2592 if (success_p)
2594 if (new.pixel == *pixel)
2596 /* If we end up with the same color as before, try adding
2597 delta to the RGB values. */
2598 x_free_colors (f, &new.pixel, 1);
2600 new.red = min (0xffff, delta + color.red);
2601 new.green = min (0xffff, delta + color.green);
2602 new.blue = min (0xffff, delta + color.blue);
2603 success_p = x_alloc_nearest_color (f, cmap, &new);
2605 else
2606 success_p = true;
2607 *pixel = new.pixel;
2610 return success_p;
2614 /* Set up the foreground color for drawing relief lines of glyph
2615 string S. RELIEF is a pointer to a struct relief containing the GC
2616 with which lines will be drawn. Use a color that is FACTOR or
2617 DELTA lighter or darker than the relief's background which is found
2618 in S->f->output_data.x->relief_background. If such a color cannot
2619 be allocated, use DEFAULT_PIXEL, instead. */
2621 static void
2622 x_setup_relief_color (struct frame *f, struct relief *relief, double factor,
2623 int delta, unsigned long default_pixel)
2625 XGCValues xgcv;
2626 struct x_output *di = f->output_data.x;
2627 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
2628 unsigned long pixel;
2629 unsigned long background = di->relief_background;
2630 Colormap cmap = FRAME_X_COLORMAP (f);
2631 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2632 Display *dpy = FRAME_X_DISPLAY (f);
2634 xgcv.graphics_exposures = False;
2635 xgcv.line_width = 1;
2637 /* Free previously allocated color. The color cell will be reused
2638 when it has been freed as many times as it was allocated, so this
2639 doesn't affect faces using the same colors. */
2640 if (relief->gc && relief->pixel != -1)
2642 x_free_colors (f, &relief->pixel, 1);
2643 relief->pixel = -1;
2646 /* Allocate new color. */
2647 xgcv.foreground = default_pixel;
2648 pixel = background;
2649 if (dpyinfo->n_planes != 1
2650 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
2651 xgcv.foreground = relief->pixel = pixel;
2653 if (relief->gc == 0)
2655 xgcv.stipple = dpyinfo->gray;
2656 mask |= GCStipple;
2657 relief->gc = XCreateGC (dpy, FRAME_X_DRAWABLE (f), mask, &xgcv);
2659 else
2660 XChangeGC (dpy, relief->gc, mask, &xgcv);
2664 /* Set up colors for the relief lines around glyph string S. */
2666 static void
2667 x_setup_relief_colors (struct glyph_string *s)
2669 struct x_output *di = s->f->output_data.x;
2670 unsigned long color;
2672 if (s->face->use_box_color_for_shadows_p)
2673 color = s->face->box_color;
2674 else if (s->first_glyph->type == IMAGE_GLYPH
2675 && s->img->pixmap
2676 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2677 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2678 else
2680 XGCValues xgcv;
2682 /* Get the background color of the face. */
2683 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2684 color = xgcv.background;
2687 if (di->white_relief.gc == 0
2688 || color != di->relief_background)
2690 di->relief_background = color;
2691 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2692 WHITE_PIX_DEFAULT (s->f));
2693 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2694 BLACK_PIX_DEFAULT (s->f));
2699 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2700 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2701 to draw, it must be >= 0. RAISED_P means draw a raised
2702 relief. LEFT_P means draw a relief on the left side of
2703 the rectangle. RIGHT_P means draw a relief on the right
2704 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2705 when drawing. */
2707 static void
2708 x_draw_relief_rect (struct frame *f,
2709 int left_x, int top_y, int right_x, int bottom_y,
2710 int width, bool raised_p, bool top_p, bool bot_p,
2711 bool left_p, bool right_p,
2712 XRectangle *clip_rect)
2714 #ifdef USE_CAIRO
2715 GC top_left_gc, bottom_right_gc;
2716 int corners = 0;
2718 if (raised_p)
2720 top_left_gc = f->output_data.x->white_relief.gc;
2721 bottom_right_gc = f->output_data.x->black_relief.gc;
2723 else
2725 top_left_gc = f->output_data.x->black_relief.gc;
2726 bottom_right_gc = f->output_data.x->white_relief.gc;
2729 x_set_clip_rectangles (f, top_left_gc, clip_rect, 1);
2730 x_set_clip_rectangles (f, bottom_right_gc, clip_rect, 1);
2732 if (left_p)
2734 x_fill_rectangle (f, top_left_gc, left_x, top_y,
2735 width, bottom_y + 1 - top_y);
2736 if (top_p)
2737 corners |= 1 << CORNER_TOP_LEFT;
2738 if (bot_p)
2739 corners |= 1 << CORNER_BOTTOM_LEFT;
2741 if (right_p)
2743 x_fill_rectangle (f, bottom_right_gc, right_x + 1 - width, top_y,
2744 width, bottom_y + 1 - top_y);
2745 if (top_p)
2746 corners |= 1 << CORNER_TOP_RIGHT;
2747 if (bot_p)
2748 corners |= 1 << CORNER_BOTTOM_RIGHT;
2750 if (top_p)
2752 if (!right_p)
2753 x_fill_rectangle (f, top_left_gc, left_x, top_y,
2754 right_x + 1 - left_x, width);
2755 else
2756 x_fill_trapezoid_for_relief (f, top_left_gc, left_x, top_y,
2757 right_x + 1 - left_x, width, 1);
2759 if (bot_p)
2761 if (!left_p)
2762 x_fill_rectangle (f, bottom_right_gc, left_x, bottom_y + 1 - width,
2763 right_x + 1 - left_x, width);
2764 else
2765 x_fill_trapezoid_for_relief (f, bottom_right_gc,
2766 left_x, bottom_y + 1 - width,
2767 right_x + 1 - left_x, width, 0);
2769 if (left_p && width != 1)
2770 x_fill_rectangle (f, bottom_right_gc, left_x, top_y,
2771 1, bottom_y + 1 - top_y);
2772 if (top_p && width != 1)
2773 x_fill_rectangle (f, bottom_right_gc, left_x, top_y,
2774 right_x + 1 - left_x, 1);
2775 if (corners)
2777 XSetBackground (FRAME_X_DISPLAY (f), top_left_gc,
2778 FRAME_BACKGROUND_PIXEL (f));
2779 x_erase_corners_for_relief (f, top_left_gc, left_x, top_y,
2780 right_x - left_x + 1, bottom_y - top_y + 1,
2781 6, 1, corners);
2784 x_reset_clip_rectangles (f, top_left_gc);
2785 x_reset_clip_rectangles (f, bottom_right_gc);
2786 #else
2787 Display *dpy = FRAME_X_DISPLAY (f);
2788 Drawable drawable = FRAME_X_DRAWABLE (f);
2789 int i;
2790 GC gc;
2792 if (raised_p)
2793 gc = f->output_data.x->white_relief.gc;
2794 else
2795 gc = f->output_data.x->black_relief.gc;
2796 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2798 /* This code is more complicated than it has to be, because of two
2799 minor hacks to make the boxes look nicer: (i) if width > 1, draw
2800 the outermost line using the black relief. (ii) Omit the four
2801 corner pixels. */
2803 /* Top. */
2804 if (top_p)
2806 if (width == 1)
2807 XDrawLine (dpy, drawable, gc,
2808 left_x + left_p, top_y,
2809 right_x + !right_p, top_y);
2811 for (i = 1; i < width; ++i)
2812 XDrawLine (dpy, drawable, gc,
2813 left_x + i * left_p, top_y + i,
2814 right_x + 1 - i * right_p, top_y + i);
2817 /* Left. */
2818 if (left_p)
2820 if (width == 1)
2821 XDrawLine (dpy, drawable, gc, left_x, top_y + 1, left_x, bottom_y);
2823 x_clear_area(f, left_x, top_y, 1, 1);
2824 x_clear_area(f, left_x, bottom_y, 1, 1);
2826 for (i = (width > 1 ? 1 : 0); i < width; ++i)
2827 XDrawLine (dpy, drawable, gc,
2828 left_x + i, top_y + (i + 1) * top_p,
2829 left_x + i, bottom_y + 1 - (i + 1) * bot_p);
2832 XSetClipMask (dpy, gc, None);
2833 if (raised_p)
2834 gc = f->output_data.x->black_relief.gc;
2835 else
2836 gc = f->output_data.x->white_relief.gc;
2837 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2839 if (width > 1)
2841 /* Outermost top line. */
2842 if (top_p)
2843 XDrawLine (dpy, drawable, gc,
2844 left_x + left_p, top_y,
2845 right_x + !right_p, top_y);
2847 /* Outermost left line. */
2848 if (left_p)
2849 XDrawLine (dpy, drawable, gc, left_x, top_y + 1, left_x, bottom_y);
2852 /* Bottom. */
2853 if (bot_p)
2855 XDrawLine (dpy, drawable, gc,
2856 left_x + left_p, bottom_y,
2857 right_x + !right_p, bottom_y);
2858 for (i = 1; i < width; ++i)
2859 XDrawLine (dpy, drawable, gc,
2860 left_x + i * left_p, bottom_y - i,
2861 right_x + 1 - i * right_p, bottom_y - i);
2864 /* Right. */
2865 if (right_p)
2867 x_clear_area(f, right_x, top_y, 1, 1);
2868 x_clear_area(f, right_x, bottom_y, 1, 1);
2869 for (i = 0; i < width; ++i)
2870 XDrawLine (dpy, drawable, gc,
2871 right_x - i, top_y + (i + 1) * top_p,
2872 right_x - i, bottom_y + 1 - (i + 1) * bot_p);
2875 x_reset_clip_rectangles (f, gc);
2877 #endif
2881 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2882 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2883 draw, it must be >= 0. LEFT_P means draw a line on the
2884 left side of the rectangle. RIGHT_P means draw a line
2885 on the right side of the rectangle. CLIP_RECT is the clipping
2886 rectangle to use when drawing. */
2888 static void
2889 x_draw_box_rect (struct glyph_string *s,
2890 int left_x, int top_y, int right_x, int bottom_y, int width,
2891 bool left_p, bool right_p, XRectangle *clip_rect)
2893 XGCValues xgcv;
2895 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2896 XSetForeground (s->display, s->gc, s->face->box_color);
2897 x_set_clip_rectangles (s->f, s->gc, clip_rect, 1);
2899 /* Top. */
2900 x_fill_rectangle (s->f, s->gc,
2901 left_x, top_y, right_x - left_x + 1, width);
2903 /* Left. */
2904 if (left_p)
2905 x_fill_rectangle (s->f, s->gc,
2906 left_x, top_y, width, bottom_y - top_y + 1);
2908 /* Bottom. */
2909 x_fill_rectangle (s->f, s->gc,
2910 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2912 /* Right. */
2913 if (right_p)
2914 x_fill_rectangle (s->f, s->gc,
2915 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2917 XSetForeground (s->display, s->gc, xgcv.foreground);
2918 x_reset_clip_rectangles (s->f, s->gc);
2922 /* Draw a box around glyph string S. */
2924 static void
2925 x_draw_glyph_string_box (struct glyph_string *s)
2927 int width, left_x, right_x, top_y, bottom_y, last_x;
2928 bool raised_p, left_p, right_p;
2929 struct glyph *last_glyph;
2930 XRectangle clip_rect;
2932 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2933 ? WINDOW_RIGHT_EDGE_X (s->w)
2934 : window_box_right (s->w, s->area));
2936 /* The glyph that may have a right box line. */
2937 last_glyph = (s->cmp || s->img
2938 ? s->first_glyph
2939 : s->first_glyph + s->nchars - 1);
2941 width = eabs (s->face->box_line_width);
2942 raised_p = s->face->box == FACE_RAISED_BOX;
2943 left_x = s->x;
2944 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2945 ? last_x - 1
2946 : min (last_x, s->x + s->background_width) - 1);
2947 top_y = s->y;
2948 bottom_y = top_y + s->height - 1;
2950 left_p = (s->first_glyph->left_box_line_p
2951 || (s->hl == DRAW_MOUSE_FACE
2952 && (s->prev == NULL
2953 || s->prev->hl != s->hl)));
2954 right_p = (last_glyph->right_box_line_p
2955 || (s->hl == DRAW_MOUSE_FACE
2956 && (s->next == NULL
2957 || s->next->hl != s->hl)));
2959 get_glyph_string_clip_rect (s, &clip_rect);
2961 if (s->face->box == FACE_SIMPLE_BOX)
2962 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2963 left_p, right_p, &clip_rect);
2964 else
2966 x_setup_relief_colors (s);
2967 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2968 width, raised_p, true, true, left_p, right_p,
2969 &clip_rect);
2974 /* Draw foreground of image glyph string S. */
2976 static void
2977 x_draw_image_foreground (struct glyph_string *s)
2979 int x = s->x;
2980 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2982 /* If first glyph of S has a left box line, start drawing it to the
2983 right of that line. */
2984 if (s->face->box != FACE_NO_BOX
2985 && s->first_glyph->left_box_line_p
2986 && s->slice.x == 0)
2987 x += eabs (s->face->box_line_width);
2989 /* If there is a margin around the image, adjust x- and y-position
2990 by that margin. */
2991 if (s->slice.x == 0)
2992 x += s->img->hmargin;
2993 if (s->slice.y == 0)
2994 y += s->img->vmargin;
2996 if (s->img->pixmap)
2998 if (s->img->mask)
3000 /* We can't set both a clip mask and use XSetClipRectangles
3001 because the latter also sets a clip mask. We also can't
3002 trust on the shape extension to be available
3003 (XShapeCombineRegion). So, compute the rectangle to draw
3004 manually. */
3005 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3006 | GCFunction);
3007 XGCValues xgcv;
3008 XRectangle clip_rect, image_rect, r;
3010 xgcv.clip_mask = s->img->mask;
3011 xgcv.clip_x_origin = x;
3012 xgcv.clip_y_origin = y;
3013 xgcv.function = GXcopy;
3014 XChangeGC (s->display, s->gc, mask, &xgcv);
3016 get_glyph_string_clip_rect (s, &clip_rect);
3017 image_rect.x = x;
3018 image_rect.y = y;
3019 image_rect.width = s->slice.width;
3020 image_rect.height = s->slice.height;
3021 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3022 XCopyArea (s->display, s->img->pixmap,
3023 FRAME_X_DRAWABLE (s->f), s->gc,
3024 s->slice.x + r.x - x, s->slice.y + r.y - y,
3025 r.width, r.height, r.x, r.y);
3027 else
3029 XRectangle clip_rect, image_rect, r;
3031 get_glyph_string_clip_rect (s, &clip_rect);
3032 image_rect.x = x;
3033 image_rect.y = y;
3034 image_rect.width = s->slice.width;
3035 image_rect.height = s->slice.height;
3036 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3037 XCopyArea (s->display, s->img->pixmap,
3038 FRAME_X_DRAWABLE (s->f), s->gc,
3039 s->slice.x + r.x - x, s->slice.y + r.y - y,
3040 r.width, r.height, r.x, r.y);
3042 /* When the image has a mask, we can expect that at
3043 least part of a mouse highlight or a block cursor will
3044 be visible. If the image doesn't have a mask, make
3045 a block cursor visible by drawing a rectangle around
3046 the image. I believe it's looking better if we do
3047 nothing here for mouse-face. */
3048 if (s->hl == DRAW_CURSOR)
3050 int relief = eabs (s->img->relief);
3051 x_draw_rectangle (s->f, s->gc,
3052 x - relief, y - relief,
3053 s->slice.width + relief*2 - 1,
3054 s->slice.height + relief*2 - 1);
3058 else
3059 /* Draw a rectangle if image could not be loaded. */
3060 x_draw_rectangle (s->f, s->gc, x, y,
3061 s->slice.width - 1, s->slice.height - 1);
3065 /* Draw a relief around the image glyph string S. */
3067 static void
3068 x_draw_image_relief (struct glyph_string *s)
3070 int x1, y1, thick;
3071 bool raised_p, top_p, bot_p, left_p, right_p;
3072 int extra_x, extra_y;
3073 XRectangle r;
3074 int x = s->x;
3075 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
3077 /* If first glyph of S has a left box line, start drawing it to the
3078 right of that line. */
3079 if (s->face->box != FACE_NO_BOX
3080 && s->first_glyph->left_box_line_p
3081 && s->slice.x == 0)
3082 x += eabs (s->face->box_line_width);
3084 /* If there is a margin around the image, adjust x- and y-position
3085 by that margin. */
3086 if (s->slice.x == 0)
3087 x += s->img->hmargin;
3088 if (s->slice.y == 0)
3089 y += s->img->vmargin;
3091 if (s->hl == DRAW_IMAGE_SUNKEN
3092 || s->hl == DRAW_IMAGE_RAISED)
3094 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
3095 raised_p = s->hl == DRAW_IMAGE_RAISED;
3097 else
3099 thick = eabs (s->img->relief);
3100 raised_p = s->img->relief > 0;
3103 x1 = x + s->slice.width - 1;
3104 y1 = y + s->slice.height - 1;
3106 extra_x = extra_y = 0;
3107 if (s->face->id == TOOL_BAR_FACE_ID)
3109 if (CONSP (Vtool_bar_button_margin)
3110 && INTEGERP (XCAR (Vtool_bar_button_margin))
3111 && INTEGERP (XCDR (Vtool_bar_button_margin)))
3113 extra_x = XINT (XCAR (Vtool_bar_button_margin));
3114 extra_y = XINT (XCDR (Vtool_bar_button_margin));
3116 else if (INTEGERP (Vtool_bar_button_margin))
3117 extra_x = extra_y = XINT (Vtool_bar_button_margin);
3120 top_p = bot_p = left_p = right_p = false;
3122 if (s->slice.x == 0)
3123 x -= thick + extra_x, left_p = true;
3124 if (s->slice.y == 0)
3125 y -= thick + extra_y, top_p = true;
3126 if (s->slice.x + s->slice.width == s->img->width)
3127 x1 += thick + extra_x, right_p = true;
3128 if (s->slice.y + s->slice.height == s->img->height)
3129 y1 += thick + extra_y, bot_p = true;
3131 x_setup_relief_colors (s);
3132 get_glyph_string_clip_rect (s, &r);
3133 x_draw_relief_rect (s->f, x, y, x1, y1, thick, raised_p,
3134 top_p, bot_p, left_p, right_p, &r);
3138 /* Draw the foreground of image glyph string S to PIXMAP. */
3140 static void
3141 x_draw_image_foreground_1 (struct glyph_string *s, Pixmap pixmap)
3143 int x = 0;
3144 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
3146 /* If first glyph of S has a left box line, start drawing it to the
3147 right of that line. */
3148 if (s->face->box != FACE_NO_BOX
3149 && s->first_glyph->left_box_line_p
3150 && s->slice.x == 0)
3151 x += eabs (s->face->box_line_width);
3153 /* If there is a margin around the image, adjust x- and y-position
3154 by that margin. */
3155 if (s->slice.x == 0)
3156 x += s->img->hmargin;
3157 if (s->slice.y == 0)
3158 y += s->img->vmargin;
3160 if (s->img->pixmap)
3162 if (s->img->mask)
3164 /* We can't set both a clip mask and use XSetClipRectangles
3165 because the latter also sets a clip mask. We also can't
3166 trust on the shape extension to be available
3167 (XShapeCombineRegion). So, compute the rectangle to draw
3168 manually. */
3169 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3170 | GCFunction);
3171 XGCValues xgcv;
3173 xgcv.clip_mask = s->img->mask;
3174 xgcv.clip_x_origin = x - s->slice.x;
3175 xgcv.clip_y_origin = y - s->slice.y;
3176 xgcv.function = GXcopy;
3177 XChangeGC (s->display, s->gc, mask, &xgcv);
3179 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3180 s->slice.x, s->slice.y,
3181 s->slice.width, s->slice.height, x, y);
3182 XSetClipMask (s->display, s->gc, None);
3184 else
3186 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3187 s->slice.x, s->slice.y,
3188 s->slice.width, s->slice.height, x, y);
3190 /* When the image has a mask, we can expect that at
3191 least part of a mouse highlight or a block cursor will
3192 be visible. If the image doesn't have a mask, make
3193 a block cursor visible by drawing a rectangle around
3194 the image. I believe it's looking better if we do
3195 nothing here for mouse-face. */
3196 if (s->hl == DRAW_CURSOR)
3198 int r = eabs (s->img->relief);
3199 x_draw_rectangle (s->f, s->gc, x - r, y - r,
3200 s->slice.width + r*2 - 1,
3201 s->slice.height + r*2 - 1);
3205 else
3206 /* Draw a rectangle if image could not be loaded. */
3207 x_draw_rectangle (s->f, s->gc, x, y,
3208 s->slice.width - 1, s->slice.height - 1);
3212 /* Draw part of the background of glyph string S. X, Y, W, and H
3213 give the rectangle to draw. */
3215 static void
3216 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
3218 if (s->stippled_p)
3220 /* Fill background with a stipple pattern. */
3221 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3222 x_fill_rectangle (s->f, s->gc, x, y, w, h);
3223 XSetFillStyle (s->display, s->gc, FillSolid);
3225 else
3226 x_clear_glyph_string_rect (s, x, y, w, h);
3230 /* Draw image glyph string S.
3232 s->y
3233 s->x +-------------------------
3234 | s->face->box
3236 | +-------------------------
3237 | | s->img->margin
3239 | | +-------------------
3240 | | | the image
3244 static void
3245 x_draw_image_glyph_string (struct glyph_string *s)
3247 int box_line_hwidth = eabs (s->face->box_line_width);
3248 int box_line_vwidth = max (s->face->box_line_width, 0);
3249 int height;
3250 Pixmap pixmap = None;
3252 height = s->height;
3253 if (s->slice.y == 0)
3254 height -= box_line_vwidth;
3255 if (s->slice.y + s->slice.height >= s->img->height)
3256 height -= box_line_vwidth;
3258 /* Fill background with face under the image. Do it only if row is
3259 taller than image or if image has a clip mask to reduce
3260 flickering. */
3261 s->stippled_p = s->face->stipple != 0;
3262 if (height > s->slice.height
3263 || s->img->hmargin
3264 || s->img->vmargin
3265 || s->img->mask
3266 || s->img->pixmap == 0
3267 || s->width != s->background_width)
3269 if (s->img->mask)
3271 /* Create a pixmap as large as the glyph string. Fill it
3272 with the background color. Copy the image to it, using
3273 its mask. Copy the temporary pixmap to the display. */
3274 Screen *screen = FRAME_X_SCREEN (s->f);
3275 int depth = DefaultDepthOfScreen (screen);
3277 /* Create a pixmap as large as the glyph string. */
3278 pixmap = XCreatePixmap (s->display, FRAME_X_DRAWABLE (s->f),
3279 s->background_width,
3280 s->height, depth);
3282 /* Don't clip in the following because we're working on the
3283 pixmap. */
3284 XSetClipMask (s->display, s->gc, None);
3286 /* Fill the pixmap with the background color/stipple. */
3287 if (s->stippled_p)
3289 /* Fill background with a stipple pattern. */
3290 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3291 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
3292 XFillRectangle (s->display, pixmap, s->gc,
3293 0, 0, s->background_width, s->height);
3294 XSetFillStyle (s->display, s->gc, FillSolid);
3295 XSetTSOrigin (s->display, s->gc, 0, 0);
3297 else
3299 XGCValues xgcv;
3300 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3301 &xgcv);
3302 XSetForeground (s->display, s->gc, xgcv.background);
3303 XFillRectangle (s->display, pixmap, s->gc,
3304 0, 0, s->background_width, s->height);
3305 XSetForeground (s->display, s->gc, xgcv.foreground);
3308 else
3310 int x = s->x;
3311 int y = s->y;
3312 int width = s->background_width;
3314 if (s->first_glyph->left_box_line_p
3315 && s->slice.x == 0)
3317 x += box_line_hwidth;
3318 width -= box_line_hwidth;
3321 if (s->slice.y == 0)
3322 y += box_line_vwidth;
3324 x_draw_glyph_string_bg_rect (s, x, y, width, height);
3327 s->background_filled_p = true;
3330 /* Draw the foreground. */
3331 #ifdef USE_CAIRO
3332 if (s->img->cr_data)
3334 cairo_t *cr = x_begin_cr_clip (s->f, s->gc);
3336 int x = s->x + s->img->hmargin;
3337 int y = s->y + s->img->vmargin;
3338 int width = s->background_width;
3340 cairo_set_source_surface (cr, s->img->cr_data,
3341 x - s->slice.x,
3342 y - s->slice.y);
3343 cairo_rectangle (cr, x, y, width, height);
3344 cairo_fill (cr);
3345 x_end_cr_clip (s->f);
3347 else
3348 #endif
3349 if (pixmap != None)
3351 x_draw_image_foreground_1 (s, pixmap);
3352 x_set_glyph_string_clipping (s);
3353 XCopyArea (s->display, pixmap, FRAME_X_DRAWABLE (s->f), s->gc,
3354 0, 0, s->background_width, s->height, s->x, s->y);
3355 XFreePixmap (s->display, pixmap);
3357 else
3358 x_draw_image_foreground (s);
3360 /* If we must draw a relief around the image, do it. */
3361 if (s->img->relief
3362 || s->hl == DRAW_IMAGE_RAISED
3363 || s->hl == DRAW_IMAGE_SUNKEN)
3364 x_draw_image_relief (s);
3368 /* Draw stretch glyph string S. */
3370 static void
3371 x_draw_stretch_glyph_string (struct glyph_string *s)
3373 eassert (s->first_glyph->type == STRETCH_GLYPH);
3375 if (s->hl == DRAW_CURSOR
3376 && !x_stretch_cursor_p)
3378 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
3379 wide as the stretch glyph. */
3380 int width, background_width = s->background_width;
3381 int x = s->x;
3383 if (!s->row->reversed_p)
3385 int left_x = window_box_left_offset (s->w, TEXT_AREA);
3387 if (x < left_x)
3389 background_width -= left_x - x;
3390 x = left_x;
3393 else
3395 /* In R2L rows, draw the cursor on the right edge of the
3396 stretch glyph. */
3397 int right_x = window_box_right (s->w, TEXT_AREA);
3399 if (x + background_width > right_x)
3400 background_width -= x - right_x;
3401 x += background_width;
3403 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
3404 if (s->row->reversed_p)
3405 x -= width;
3407 /* Draw cursor. */
3408 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
3410 /* Clear rest using the GC of the original non-cursor face. */
3411 if (width < background_width)
3413 int y = s->y;
3414 int w = background_width - width, h = s->height;
3415 XRectangle r;
3416 GC gc;
3418 if (!s->row->reversed_p)
3419 x += width;
3420 else
3421 x = s->x;
3422 if (s->row->mouse_face_p
3423 && cursor_in_mouse_face_p (s->w))
3425 x_set_mouse_face_gc (s);
3426 gc = s->gc;
3428 else
3429 gc = s->face->gc;
3431 get_glyph_string_clip_rect (s, &r);
3432 x_set_clip_rectangles (s->f, gc, &r, 1);
3434 if (s->face->stipple)
3436 /* Fill background with a stipple pattern. */
3437 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3438 x_fill_rectangle (s->f, gc, x, y, w, h);
3439 XSetFillStyle (s->display, gc, FillSolid);
3441 else
3443 XGCValues xgcv;
3444 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3445 XSetForeground (s->display, gc, xgcv.background);
3446 x_fill_rectangle (s->f, gc, x, y, w, h);
3447 XSetForeground (s->display, gc, xgcv.foreground);
3450 x_reset_clip_rectangles (s->f, gc);
3453 else if (!s->background_filled_p)
3455 int background_width = s->background_width;
3456 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
3458 /* Don't draw into left margin, fringe or scrollbar area
3459 except for header line and mode line. */
3460 if (x < left_x && !s->row->mode_line_p)
3462 background_width -= left_x - x;
3463 x = left_x;
3465 if (background_width > 0)
3466 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
3469 s->background_filled_p = true;
3472 static void
3473 x_get_scale_factor(Display *disp, int *scale_x, int *scale_y)
3475 const int base_res = 96;
3476 struct x_display_info * dpyinfo = x_display_info_for_display (disp);
3478 *scale_x = *scale_y = 1;
3480 if (dpyinfo)
3482 if (dpyinfo->resx > base_res)
3483 *scale_x = floor (dpyinfo->resx / base_res);
3484 if (dpyinfo->resy > base_res)
3485 *scale_y = floor (dpyinfo->resy / base_res);
3490 Draw a wavy line under S. The wave fills wave_height pixels from y0.
3492 x0 wave_length = 2
3494 y0 * * * * *
3495 |* * * * * * * * *
3496 wave_height = 3 | * * * *
3499 static void
3500 x_draw_underwave (struct glyph_string *s)
3502 /* Adjust for scale/HiDPI. */
3503 int scale_x, scale_y;
3505 x_get_scale_factor (s->display, &scale_x, &scale_y);
3507 int wave_height = 3 * scale_y, wave_length = 2 * scale_x;
3509 #ifdef USE_CAIRO
3510 x_draw_horizontal_wave (s->f, s->gc, s->x, s->ybase - wave_height + 3,
3511 s->width, wave_height, wave_length);
3512 #else /* not USE_CAIRO */
3513 int dx, dy, x0, y0, width, x1, y1, x2, y2, xmax, thickness = scale_y;;
3514 bool odd;
3515 XRectangle wave_clip, string_clip, final_clip;
3517 dx = wave_length;
3518 dy = wave_height - 1;
3519 x0 = s->x;
3520 y0 = s->ybase + wave_height / 2 - scale_y;
3521 width = s->width;
3522 xmax = x0 + width;
3524 /* Find and set clipping rectangle */
3526 wave_clip.x = x0;
3527 wave_clip.y = y0;
3528 wave_clip.width = width;
3529 wave_clip.height = wave_height;
3530 get_glyph_string_clip_rect (s, &string_clip);
3532 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
3533 return;
3535 XSetClipRectangles (s->display, s->gc, 0, 0, &final_clip, 1, Unsorted);
3537 /* Draw the waves */
3539 x1 = x0 - (x0 % dx);
3540 x2 = x1 + dx;
3541 odd = (x1 / dx) & 1;
3542 y1 = y2 = y0;
3544 if (odd)
3545 y1 += dy;
3546 else
3547 y2 += dy;
3549 if (INT_MAX - dx < xmax)
3550 emacs_abort ();
3552 while (x1 <= xmax)
3554 XSetLineAttributes (s->display, s->gc, thickness, LineSolid, CapButt,
3555 JoinRound);
3556 XDrawLine (s->display, FRAME_X_DRAWABLE (s->f), s->gc, x1, y1, x2, y2);
3557 x1 = x2, y1 = y2;
3558 x2 += dx, y2 = y0 + odd*dy;
3559 odd = !odd;
3562 /* Restore previous clipping rectangle(s) */
3563 XSetClipRectangles (s->display, s->gc, 0, 0, s->clip, s->num_clips, Unsorted);
3564 #endif /* not USE_CAIRO */
3568 /* Draw glyph string S. */
3570 static void
3571 x_draw_glyph_string (struct glyph_string *s)
3573 bool relief_drawn_p = false;
3575 /* If S draws into the background of its successors, draw the
3576 background of the successors first so that S can draw into it.
3577 This makes S->next use XDrawString instead of XDrawImageString. */
3578 if (s->next && s->right_overhang && !s->for_overlaps)
3580 int width;
3581 struct glyph_string *next;
3583 for (width = 0, next = s->next;
3584 next && width < s->right_overhang;
3585 width += next->width, next = next->next)
3586 if (next->first_glyph->type != IMAGE_GLYPH)
3588 x_set_glyph_string_gc (next);
3589 x_set_glyph_string_clipping (next);
3590 if (next->first_glyph->type == STRETCH_GLYPH)
3591 x_draw_stretch_glyph_string (next);
3592 else
3593 x_draw_glyph_string_background (next, true);
3594 next->num_clips = 0;
3598 /* Set up S->gc, set clipping and draw S. */
3599 x_set_glyph_string_gc (s);
3601 /* Draw relief (if any) in advance for char/composition so that the
3602 glyph string can be drawn over it. */
3603 if (!s->for_overlaps
3604 && s->face->box != FACE_NO_BOX
3605 && (s->first_glyph->type == CHAR_GLYPH
3606 || s->first_glyph->type == COMPOSITE_GLYPH))
3609 x_set_glyph_string_clipping (s);
3610 x_draw_glyph_string_background (s, true);
3611 x_draw_glyph_string_box (s);
3612 x_set_glyph_string_clipping (s);
3613 relief_drawn_p = true;
3615 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
3616 && !s->clip_tail
3617 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
3618 || (s->next && s->next->hl != s->hl && s->right_overhang)))
3619 /* We must clip just this glyph. left_overhang part has already
3620 drawn when s->prev was drawn, and right_overhang part will be
3621 drawn later when s->next is drawn. */
3622 x_set_glyph_string_clipping_exactly (s, s);
3623 else
3624 x_set_glyph_string_clipping (s);
3626 switch (s->first_glyph->type)
3628 case IMAGE_GLYPH:
3629 x_draw_image_glyph_string (s);
3630 break;
3632 case XWIDGET_GLYPH:
3633 x_draw_xwidget_glyph_string (s);
3634 break;
3636 case STRETCH_GLYPH:
3637 x_draw_stretch_glyph_string (s);
3638 break;
3640 case CHAR_GLYPH:
3641 if (s->for_overlaps)
3642 s->background_filled_p = true;
3643 else
3644 x_draw_glyph_string_background (s, false);
3645 x_draw_glyph_string_foreground (s);
3646 break;
3648 case COMPOSITE_GLYPH:
3649 if (s->for_overlaps || (s->cmp_from > 0
3650 && ! s->first_glyph->u.cmp.automatic))
3651 s->background_filled_p = true;
3652 else
3653 x_draw_glyph_string_background (s, true);
3654 x_draw_composite_glyph_string_foreground (s);
3655 break;
3657 case GLYPHLESS_GLYPH:
3658 if (s->for_overlaps)
3659 s->background_filled_p = true;
3660 else
3661 x_draw_glyph_string_background (s, true);
3662 x_draw_glyphless_glyph_string_foreground (s);
3663 break;
3665 default:
3666 emacs_abort ();
3669 if (!s->for_overlaps)
3671 /* Draw underline. */
3672 if (s->face->underline_p)
3674 if (s->face->underline_type == FACE_UNDER_WAVE)
3676 if (s->face->underline_defaulted_p)
3677 x_draw_underwave (s);
3678 else
3680 XGCValues xgcv;
3681 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3682 XSetForeground (s->display, s->gc, s->face->underline_color);
3683 x_draw_underwave (s);
3684 XSetForeground (s->display, s->gc, xgcv.foreground);
3687 else if (s->face->underline_type == FACE_UNDER_LINE)
3689 unsigned long thickness, position;
3690 int y;
3692 if (s->prev && s->prev->face->underline_p
3693 && s->prev->face->underline_type == FACE_UNDER_LINE)
3695 /* We use the same underline style as the previous one. */
3696 thickness = s->prev->underline_thickness;
3697 position = s->prev->underline_position;
3699 else
3701 struct font *font = font_for_underline_metrics (s);
3703 /* Get the underline thickness. Default is 1 pixel. */
3704 if (font && font->underline_thickness > 0)
3705 thickness = font->underline_thickness;
3706 else
3707 thickness = 1;
3708 if (x_underline_at_descent_line)
3709 position = (s->height - thickness) - (s->ybase - s->y);
3710 else
3712 /* Get the underline position. This is the recommended
3713 vertical offset in pixels from the baseline to the top of
3714 the underline. This is a signed value according to the
3715 specs, and its default is
3717 ROUND ((maximum descent) / 2), with
3718 ROUND(x) = floor (x + 0.5) */
3720 if (x_use_underline_position_properties
3721 && font && font->underline_position >= 0)
3722 position = font->underline_position;
3723 else if (font)
3724 position = (font->descent + 1) / 2;
3725 else
3726 position = underline_minimum_offset;
3728 position = max (position, underline_minimum_offset);
3730 /* Check the sanity of thickness and position. We should
3731 avoid drawing underline out of the current line area. */
3732 if (s->y + s->height <= s->ybase + position)
3733 position = (s->height - 1) - (s->ybase - s->y);
3734 if (s->y + s->height < s->ybase + position + thickness)
3735 thickness = (s->y + s->height) - (s->ybase + position);
3736 s->underline_thickness = thickness;
3737 s->underline_position = position;
3738 y = s->ybase + position;
3739 if (s->face->underline_defaulted_p)
3740 x_fill_rectangle (s->f, s->gc,
3741 s->x, y, s->width, thickness);
3742 else
3744 XGCValues xgcv;
3745 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3746 XSetForeground (s->display, s->gc, s->face->underline_color);
3747 x_fill_rectangle (s->f, s->gc,
3748 s->x, y, s->width, thickness);
3749 XSetForeground (s->display, s->gc, xgcv.foreground);
3753 /* Draw overline. */
3754 if (s->face->overline_p)
3756 unsigned long dy = 0, h = 1;
3758 if (s->face->overline_color_defaulted_p)
3759 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3760 s->width, h);
3761 else
3763 XGCValues xgcv;
3764 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3765 XSetForeground (s->display, s->gc, s->face->overline_color);
3766 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3767 s->width, h);
3768 XSetForeground (s->display, s->gc, xgcv.foreground);
3772 /* Draw strike-through. */
3773 if (s->face->strike_through_p)
3775 /* Y-coordinate and height of the glyph string's first
3776 glyph. We cannot use s->y and s->height because those
3777 could be larger if there are taller display elements
3778 (e.g., characters displayed with a larger font) in the
3779 same glyph row. */
3780 int glyph_y = s->ybase - s->first_glyph->ascent;
3781 int glyph_height = s->first_glyph->ascent + s->first_glyph->descent;
3782 /* Strike-through width and offset from the glyph string's
3783 top edge. */
3784 unsigned long h = 1;
3785 unsigned long dy = (glyph_height - h) / 2;
3787 if (s->face->strike_through_color_defaulted_p)
3788 x_fill_rectangle (s->f, s->gc, s->x, glyph_y + dy,
3789 s->width, h);
3790 else
3792 XGCValues xgcv;
3793 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3794 XSetForeground (s->display, s->gc, s->face->strike_through_color);
3795 x_fill_rectangle (s->f, s->gc, s->x, glyph_y + dy,
3796 s->width, h);
3797 XSetForeground (s->display, s->gc, xgcv.foreground);
3801 /* Draw relief if not yet drawn. */
3802 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
3803 x_draw_glyph_string_box (s);
3805 if (s->prev)
3807 struct glyph_string *prev;
3809 for (prev = s->prev; prev; prev = prev->prev)
3810 if (prev->hl != s->hl
3811 && prev->x + prev->width + prev->right_overhang > s->x)
3813 /* As prev was drawn while clipped to its own area, we
3814 must draw the right_overhang part using s->hl now. */
3815 enum draw_glyphs_face save = prev->hl;
3817 prev->hl = s->hl;
3818 x_set_glyph_string_gc (prev);
3819 x_set_glyph_string_clipping_exactly (s, prev);
3820 if (prev->first_glyph->type == CHAR_GLYPH)
3821 x_draw_glyph_string_foreground (prev);
3822 else
3823 x_draw_composite_glyph_string_foreground (prev);
3824 x_reset_clip_rectangles (prev->f, prev->gc);
3825 prev->hl = save;
3826 prev->num_clips = 0;
3830 if (s->next)
3832 struct glyph_string *next;
3834 for (next = s->next; next; next = next->next)
3835 if (next->hl != s->hl
3836 && next->x - next->left_overhang < s->x + s->width)
3838 /* As next will be drawn while clipped to its own area,
3839 we must draw the left_overhang part using s->hl now. */
3840 enum draw_glyphs_face save = next->hl;
3842 next->hl = s->hl;
3843 x_set_glyph_string_gc (next);
3844 x_set_glyph_string_clipping_exactly (s, next);
3845 if (next->first_glyph->type == CHAR_GLYPH)
3846 x_draw_glyph_string_foreground (next);
3847 else
3848 x_draw_composite_glyph_string_foreground (next);
3849 x_reset_clip_rectangles (next->f, next->gc);
3850 next->hl = save;
3851 next->num_clips = 0;
3852 next->clip_head = s->next;
3857 /* Reset clipping. */
3858 x_reset_clip_rectangles (s->f, s->gc);
3859 s->num_clips = 0;
3862 /* Shift display to make room for inserted glyphs. */
3864 static void
3865 x_shift_glyphs_for_insert (struct frame *f, int x, int y, int width, int height, int shift_by)
3867 /* Never called on a GUI frame, see
3868 https://lists.gnu.org/r/emacs-devel/2015-05/msg00456.html
3870 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f), FRAME_X_DRAWABLE (f),
3871 f->output_data.x->normal_gc,
3872 x, y, width, height,
3873 x + shift_by, y);
3876 /* Delete N glyphs at the nominal cursor position. Not implemented
3877 for X frames. */
3879 static void
3880 x_delete_glyphs (struct frame *f, register int n)
3882 emacs_abort ();
3886 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
3887 If they are <= 0, this is probably an error. */
3889 static ATTRIBUTE_UNUSED void
3890 x_clear_area1 (Display *dpy, Window window,
3891 int x, int y, int width, int height, int exposures)
3893 eassert (width > 0 && height > 0);
3894 XClearArea (dpy, window, x, y, width, height, exposures);
3897 void
3898 x_clear_area (struct frame *f, int x, int y, int width, int height)
3900 #ifdef USE_CAIRO
3901 cairo_t *cr;
3903 eassert (width > 0 && height > 0);
3905 cr = x_begin_cr_clip (f, NULL);
3906 x_set_cr_source_with_gc_background (f, f->output_data.x->normal_gc);
3907 cairo_rectangle (cr, x, y, width, height);
3908 cairo_fill (cr);
3909 x_end_cr_clip (f);
3910 #else
3911 if (FRAME_X_DOUBLE_BUFFERED_P (f))
3912 XFillRectangle (FRAME_X_DISPLAY (f),
3913 FRAME_X_DRAWABLE (f),
3914 f->output_data.x->reverse_gc,
3915 x, y, width, height);
3916 else
3917 x_clear_area1 (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3918 x, y, width, height, False);
3919 #endif
3923 /* Clear an entire frame. */
3925 static void
3926 x_clear_frame (struct frame *f)
3928 /* Clearing the frame will erase any cursor, so mark them all as no
3929 longer visible. */
3930 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
3932 block_input ();
3934 font_drop_xrender_surfaces (f);
3935 x_clear_window (f);
3937 /* We have to clear the scroll bars. If we have changed colors or
3938 something like that, then they should be notified. */
3939 x_scroll_bar_clear (f);
3941 XFlush (FRAME_X_DISPLAY (f));
3943 unblock_input ();
3946 /* RIF: Show hourglass cursor on frame F. */
3948 static void
3949 x_show_hourglass (struct frame *f)
3951 Display *dpy = FRAME_X_DISPLAY (f);
3953 if (dpy)
3955 struct x_output *x = FRAME_X_OUTPUT (f);
3956 #ifdef USE_X_TOOLKIT
3957 if (x->widget)
3958 #else
3959 if (FRAME_OUTER_WINDOW (f))
3960 #endif
3962 x->hourglass_p = true;
3964 if (!x->hourglass_window)
3966 unsigned long mask = CWCursor;
3967 XSetWindowAttributes attrs;
3968 #ifdef USE_GTK
3969 Window parent = FRAME_X_WINDOW (f);
3970 #else
3971 Window parent = FRAME_OUTER_WINDOW (f);
3972 #endif
3973 attrs.cursor = x->hourglass_cursor;
3975 x->hourglass_window = XCreateWindow
3976 (dpy, parent, 0, 0, 32000, 32000, 0, 0,
3977 InputOnly, CopyFromParent, mask, &attrs);
3980 XMapRaised (dpy, x->hourglass_window);
3981 XFlush (dpy);
3986 /* RIF: Cancel hourglass cursor on frame F. */
3988 static void
3989 x_hide_hourglass (struct frame *f)
3991 struct x_output *x = FRAME_X_OUTPUT (f);
3993 /* Watch out for newly created frames. */
3994 if (x->hourglass_window)
3996 XUnmapWindow (FRAME_X_DISPLAY (f), x->hourglass_window);
3997 /* Sync here because XTread_socket looks at the
3998 hourglass_p flag that is reset to zero below. */
3999 XSync (FRAME_X_DISPLAY (f), False);
4000 x->hourglass_p = false;
4004 /* Invert the middle quarter of the frame for .15 sec. */
4006 static void
4007 XTflash (struct frame *f)
4009 block_input ();
4012 #ifdef USE_GTK
4013 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
4014 when the scroll bars and the edit widget share the same X window. */
4015 GdkWindow *window = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
4016 #ifdef HAVE_GTK3
4017 #if GTK_CHECK_VERSION (3, 22, 0)
4018 cairo_region_t *region = gdk_window_get_visible_region (window);
4019 GdkDrawingContext *context = gdk_window_begin_draw_frame (window, region);
4020 cairo_t *cr = gdk_drawing_context_get_cairo_context (context);
4021 #else
4022 cairo_t *cr = gdk_cairo_create (window);
4023 #endif
4024 cairo_set_source_rgb (cr, 1, 1, 1);
4025 cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
4026 #define XFillRectangle(d, win, gc, x, y, w, h) \
4027 do { \
4028 cairo_rectangle (cr, x, y, w, h); \
4029 cairo_fill (cr); \
4031 while (false)
4032 #else /* ! HAVE_GTK3 */
4033 GdkGCValues vals;
4034 GdkGC *gc;
4035 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
4036 ^ FRAME_BACKGROUND_PIXEL (f));
4037 vals.function = GDK_XOR;
4038 gc = gdk_gc_new_with_values (window,
4039 &vals, GDK_GC_FUNCTION | GDK_GC_FOREGROUND);
4040 #define XFillRectangle(d, win, gc, x, y, w, h) \
4041 gdk_draw_rectangle (window, gc, true, x, y, w, h)
4042 #endif /* ! HAVE_GTK3 */
4043 #else /* ! USE_GTK */
4044 GC gc;
4046 /* Create a GC that will use the GXxor function to flip foreground
4047 pixels into background pixels. */
4049 XGCValues values;
4051 values.function = GXxor;
4052 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
4053 ^ FRAME_BACKGROUND_PIXEL (f));
4055 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4056 GCFunction | GCForeground, &values);
4058 #endif
4060 /* Get the height not including a menu bar widget. */
4061 int height = FRAME_PIXEL_HEIGHT (f);
4062 /* Height of each line to flash. */
4063 int flash_height = FRAME_LINE_HEIGHT (f);
4064 /* These will be the left and right margins of the rectangles. */
4065 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
4066 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
4067 int width = flash_right - flash_left;
4069 /* If window is tall, flash top and bottom line. */
4070 if (height > 3 * FRAME_LINE_HEIGHT (f))
4072 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4073 flash_left,
4074 (FRAME_INTERNAL_BORDER_WIDTH (f)
4075 + FRAME_TOP_MARGIN_HEIGHT (f)),
4076 width, flash_height);
4077 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4078 flash_left,
4079 (height - flash_height
4080 - FRAME_INTERNAL_BORDER_WIDTH (f)),
4081 width, flash_height);
4084 else
4085 /* If it is short, flash it all. */
4086 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4087 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
4088 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
4090 x_flush (f);
4093 struct timespec delay = make_timespec (0, 150 * 1000 * 1000);
4094 struct timespec wakeup = timespec_add (current_timespec (), delay);
4096 /* Keep waiting until past the time wakeup or any input gets
4097 available. */
4098 while (! detect_input_pending ())
4100 struct timespec current = current_timespec ();
4101 struct timespec timeout;
4103 /* Break if result would not be positive. */
4104 if (timespec_cmp (wakeup, current) <= 0)
4105 break;
4107 /* How long `select' should wait. */
4108 timeout = make_timespec (0, 10 * 1000 * 1000);
4110 /* Try to wait that long--but we might wake up sooner. */
4111 pselect (0, NULL, NULL, NULL, &timeout, NULL);
4115 /* If window is tall, flash top and bottom line. */
4116 if (height > 3 * FRAME_LINE_HEIGHT (f))
4118 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4119 flash_left,
4120 (FRAME_INTERNAL_BORDER_WIDTH (f)
4121 + FRAME_TOP_MARGIN_HEIGHT (f)),
4122 width, flash_height);
4123 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4124 flash_left,
4125 (height - flash_height
4126 - FRAME_INTERNAL_BORDER_WIDTH (f)),
4127 width, flash_height);
4129 else
4130 /* If it is short, flash it all. */
4131 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4132 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
4133 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
4135 #ifdef USE_GTK
4136 #ifdef HAVE_GTK3
4137 #if GTK_CHECK_VERSION (3, 22, 0)
4138 gdk_window_end_draw_frame (window, context);
4139 cairo_region_destroy (region);
4140 #else
4141 cairo_destroy (cr);
4142 #endif
4143 #else
4144 g_object_unref (G_OBJECT (gc));
4145 #endif
4146 #undef XFillRectangle
4147 #else
4148 XFreeGC (FRAME_X_DISPLAY (f), gc);
4149 #endif
4150 x_flush (f);
4154 unblock_input ();
4158 static void
4159 XTtoggle_invisible_pointer (struct frame *f, bool invisible)
4161 block_input ();
4162 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, invisible);
4163 unblock_input ();
4167 /* Make audible bell. */
4169 static void
4170 XTring_bell (struct frame *f)
4172 if (FRAME_X_DISPLAY (f))
4174 if (visible_bell)
4175 XTflash (f);
4176 else
4178 block_input ();
4179 #ifdef HAVE_XKB
4180 XkbBell (FRAME_X_DISPLAY (f), None, 0, None);
4181 #else
4182 XBell (FRAME_X_DISPLAY (f), 0);
4183 #endif
4184 XFlush (FRAME_X_DISPLAY (f));
4185 unblock_input ();
4190 /***********************************************************************
4191 Line Dance
4192 ***********************************************************************/
4194 /* Perform an insert-lines or delete-lines operation, inserting N
4195 lines or deleting -N lines at vertical position VPOS. */
4197 static void
4198 x_ins_del_lines (struct frame *f, int vpos, int n)
4200 emacs_abort ();
4204 /* Scroll part of the display as described by RUN. */
4206 static void
4207 x_scroll_run (struct window *w, struct run *run)
4209 struct frame *f = XFRAME (w->frame);
4210 int x, y, width, height, from_y, to_y, bottom_y;
4212 /* Get frame-relative bounding box of the text display area of W,
4213 without mode lines. Include in this box the left and right
4214 fringe of W. */
4215 window_box (w, ANY_AREA, &x, &y, &width, &height);
4217 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
4218 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
4219 bottom_y = y + height;
4221 if (to_y < from_y)
4223 /* Scrolling up. Make sure we don't copy part of the mode
4224 line at the bottom. */
4225 if (from_y + run->height > bottom_y)
4226 height = bottom_y - from_y;
4227 else
4228 height = run->height;
4230 else
4232 /* Scrolling down. Make sure we don't copy over the mode line.
4233 at the bottom. */
4234 if (to_y + run->height > bottom_y)
4235 height = bottom_y - to_y;
4236 else
4237 height = run->height;
4240 block_input ();
4242 /* Cursor off. Will be switched on again in x_update_window_end. */
4243 x_clear_cursor (w);
4245 #ifdef USE_CAIRO
4246 if (FRAME_CR_CONTEXT (f))
4248 cairo_surface_t *s = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
4249 width, height);
4250 cairo_t *cr = cairo_create (s);
4251 cairo_set_source_surface (cr, cairo_get_target (FRAME_CR_CONTEXT (f)),
4252 -x, -from_y);
4253 cairo_paint (cr);
4254 cairo_destroy (cr);
4256 cr = FRAME_CR_CONTEXT (f);
4257 cairo_save (cr);
4258 cairo_set_source_surface (cr, s, 0, to_y);
4259 cairo_rectangle (cr, x, to_y, width, height);
4260 cairo_fill (cr);
4261 cairo_restore (cr);
4262 cairo_surface_destroy (s);
4264 else
4266 SET_FRAME_GARBAGED (f);
4268 #else
4269 XCopyArea (FRAME_X_DISPLAY (f),
4270 FRAME_X_DRAWABLE (f), FRAME_X_DRAWABLE (f),
4271 f->output_data.x->normal_gc,
4272 x, from_y,
4273 width, height,
4274 x, to_y);
4275 #endif
4277 unblock_input ();
4282 /***********************************************************************
4283 Exposure Events
4284 ***********************************************************************/
4287 static void
4288 frame_highlight (struct frame *f)
4290 /* We used to only do this if Vx_no_window_manager was non-nil, but
4291 the ICCCM (section 4.1.6) says that the window's border pixmap
4292 and border pixel are window attributes which are "private to the
4293 client", so we can always change it to whatever we want. */
4294 block_input ();
4295 /* I recently started to get errors in this XSetWindowBorder, depending on
4296 the window-manager in use, tho something more is at play since I've been
4297 using that same window-manager binary for ever. Let's not crash just
4298 because of this (bug#9310). */
4299 x_catch_errors (FRAME_X_DISPLAY (f));
4300 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4301 f->output_data.x->border_pixel);
4302 x_uncatch_errors ();
4303 unblock_input ();
4304 x_update_cursor (f, true);
4305 x_set_frame_alpha (f);
4308 static void
4309 frame_unhighlight (struct frame *f)
4311 /* We used to only do this if Vx_no_window_manager was non-nil, but
4312 the ICCCM (section 4.1.6) says that the window's border pixmap
4313 and border pixel are window attributes which are "private to the
4314 client", so we can always change it to whatever we want. */
4315 block_input ();
4316 /* Same as above for XSetWindowBorder (bug#9310). */
4317 x_catch_errors (FRAME_X_DISPLAY (f));
4318 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4319 f->output_data.x->border_tile);
4320 x_uncatch_errors ();
4321 unblock_input ();
4322 x_update_cursor (f, true);
4323 x_set_frame_alpha (f);
4326 /* The focus has changed. Update the frames as necessary to reflect
4327 the new situation. Note that we can't change the selected frame
4328 here, because the Lisp code we are interrupting might become confused.
4329 Each event gets marked with the frame in which it occurred, so the
4330 Lisp code can tell when the switch took place by examining the events. */
4332 static void
4333 x_new_focus_frame (struct x_display_info *dpyinfo, struct frame *frame)
4335 struct frame *old_focus = dpyinfo->x_focus_frame;
4337 if (frame != dpyinfo->x_focus_frame)
4339 /* Set this before calling other routines, so that they see
4340 the correct value of x_focus_frame. */
4341 dpyinfo->x_focus_frame = frame;
4343 if (old_focus && old_focus->auto_lower)
4344 x_lower_frame (old_focus);
4346 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
4347 dpyinfo->x_pending_autoraise_frame = dpyinfo->x_focus_frame;
4348 else
4349 dpyinfo->x_pending_autoraise_frame = NULL;
4352 x_frame_rehighlight (dpyinfo);
4355 /* Handle FocusIn and FocusOut state changes for FRAME.
4356 If FRAME has focus and there exists more than one frame, puts
4357 a FOCUS_IN_EVENT into *BUFP. */
4359 static void
4360 x_focus_changed (int type, int state, struct x_display_info *dpyinfo, struct frame *frame, struct input_event *bufp)
4362 if (type == FocusIn)
4364 if (dpyinfo->x_focus_event_frame != frame)
4366 x_new_focus_frame (dpyinfo, frame);
4367 dpyinfo->x_focus_event_frame = frame;
4369 /* Don't stop displaying the initial startup message
4370 for a switch-frame event we don't need. */
4371 /* When run as a daemon, Vterminal_frame is always NIL. */
4372 bufp->arg = (((NILP (Vterminal_frame)
4373 || ! FRAME_X_P (XFRAME (Vterminal_frame))
4374 || EQ (Fdaemonp (), Qt))
4375 && CONSP (Vframe_list)
4376 && !NILP (XCDR (Vframe_list)))
4377 ? Qt : Qnil);
4378 bufp->kind = FOCUS_IN_EVENT;
4379 XSETFRAME (bufp->frame_or_window, frame);
4382 frame->output_data.x->focus_state |= state;
4384 #ifdef HAVE_X_I18N
4385 if (FRAME_XIC (frame))
4386 XSetICFocus (FRAME_XIC (frame));
4387 #endif
4389 else if (type == FocusOut)
4391 frame->output_data.x->focus_state &= ~state;
4393 if (dpyinfo->x_focus_event_frame == frame)
4395 dpyinfo->x_focus_event_frame = 0;
4396 x_new_focus_frame (dpyinfo, 0);
4398 bufp->kind = FOCUS_OUT_EVENT;
4399 XSETFRAME (bufp->frame_or_window, frame);
4402 #ifdef HAVE_X_I18N
4403 if (FRAME_XIC (frame))
4404 XUnsetICFocus (FRAME_XIC (frame));
4405 #endif
4406 if (frame->pointer_invisible)
4407 XTtoggle_invisible_pointer (frame, false);
4411 /* Return the Emacs frame-object corresponding to an X window.
4412 It could be the frame's main window or an icon window. */
4414 static struct frame *
4415 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4417 Lisp_Object tail, frame;
4418 struct frame *f;
4420 if (wdesc == None)
4421 return NULL;
4423 FOR_EACH_FRAME (tail, frame)
4425 f = XFRAME (frame);
4426 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4427 continue;
4428 if (f->output_data.x->hourglass_window == wdesc)
4429 return f;
4430 #ifdef USE_X_TOOLKIT
4431 if ((f->output_data.x->edit_widget
4432 && XtWindow (f->output_data.x->edit_widget) == wdesc)
4433 /* A tooltip frame? */
4434 || (!f->output_data.x->edit_widget
4435 && FRAME_X_WINDOW (f) == wdesc)
4436 || f->output_data.x->icon_desc == wdesc)
4437 return f;
4438 #else /* not USE_X_TOOLKIT */
4439 #ifdef USE_GTK
4440 if (f->output_data.x->edit_widget)
4442 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4443 struct x_output *x = f->output_data.x;
4444 if (gwdesc != 0 && gwdesc == x->edit_widget)
4445 return f;
4447 #endif /* USE_GTK */
4448 if (FRAME_X_WINDOW (f) == wdesc
4449 || f->output_data.x->icon_desc == wdesc)
4450 return f;
4451 #endif /* not USE_X_TOOLKIT */
4453 return 0;
4456 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
4458 /* Like x_window_to_frame but also compares the window with the widget's
4459 windows. */
4461 static struct frame *
4462 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4464 Lisp_Object tail, frame;
4465 struct frame *f, *found = NULL;
4466 struct x_output *x;
4468 if (wdesc == None)
4469 return NULL;
4471 FOR_EACH_FRAME (tail, frame)
4473 if (found)
4474 break;
4475 f = XFRAME (frame);
4476 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
4478 /* This frame matches if the window is any of its widgets. */
4479 x = f->output_data.x;
4480 if (x->hourglass_window == wdesc)
4481 found = f;
4482 else if (x->widget)
4484 #ifdef USE_GTK
4485 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4486 if (gwdesc != 0
4487 && gtk_widget_get_toplevel (gwdesc) == x->widget)
4488 found = f;
4489 #else
4490 if (wdesc == XtWindow (x->widget)
4491 || wdesc == XtWindow (x->column_widget)
4492 || wdesc == XtWindow (x->edit_widget))
4493 found = f;
4494 /* Match if the window is this frame's menubar. */
4495 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
4496 found = f;
4497 #endif
4499 else if (FRAME_X_WINDOW (f) == wdesc)
4500 /* A tooltip frame. */
4501 found = f;
4505 return found;
4508 /* Likewise, but consider only the menu bar widget. */
4510 static struct frame *
4511 x_menubar_window_to_frame (struct x_display_info *dpyinfo,
4512 const XEvent *event)
4514 Window wdesc = event->xany.window;
4515 Lisp_Object tail, frame;
4516 struct frame *f;
4517 struct x_output *x;
4519 if (wdesc == None)
4520 return NULL;
4522 FOR_EACH_FRAME (tail, frame)
4524 f = XFRAME (frame);
4525 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4526 continue;
4527 x = f->output_data.x;
4528 #ifdef USE_GTK
4529 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
4530 return f;
4531 #else
4532 /* Match if the window is this frame's menubar. */
4533 if (x->menubar_widget
4534 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
4535 return f;
4536 #endif
4538 return 0;
4541 /* Return the frame whose principal (outermost) window is WDESC.
4542 If WDESC is some other (smaller) window, we return 0. */
4544 struct frame *
4545 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4547 Lisp_Object tail, frame;
4548 struct frame *f;
4549 struct x_output *x;
4551 if (wdesc == None)
4552 return NULL;
4554 FOR_EACH_FRAME (tail, frame)
4556 f = XFRAME (frame);
4557 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4558 continue;
4559 x = f->output_data.x;
4561 if (x->widget)
4563 /* This frame matches if the window is its topmost widget. */
4564 #ifdef USE_GTK
4565 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4566 if (gwdesc == x->widget)
4567 return f;
4568 #else
4569 if (wdesc == XtWindow (x->widget))
4570 return f;
4571 #endif
4573 else if (FRAME_X_WINDOW (f) == wdesc)
4574 /* Tooltip frame. */
4575 return f;
4577 return 0;
4580 #else /* !USE_X_TOOLKIT && !USE_GTK */
4582 #define x_any_window_to_frame(d, i) x_window_to_frame (d, i)
4583 #define x_top_window_to_frame(d, i) x_window_to_frame (d, i)
4585 #endif /* USE_X_TOOLKIT || USE_GTK */
4587 /* The focus may have changed. Figure out if it is a real focus change,
4588 by checking both FocusIn/Out and Enter/LeaveNotify events.
4590 Returns FOCUS_IN_EVENT event in *BUFP. */
4592 static void
4593 x_detect_focus_change (struct x_display_info *dpyinfo, struct frame *frame,
4594 const XEvent *event, struct input_event *bufp)
4596 if (!frame)
4597 return;
4599 switch (event->type)
4601 case EnterNotify:
4602 case LeaveNotify:
4604 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
4605 int focus_state
4606 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
4608 if (event->xcrossing.detail != NotifyInferior
4609 && event->xcrossing.focus
4610 && ! (focus_state & FOCUS_EXPLICIT))
4611 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
4612 FOCUS_IMPLICIT,
4613 dpyinfo, frame, bufp);
4615 break;
4617 case FocusIn:
4618 case FocusOut:
4619 x_focus_changed (event->type,
4620 (event->xfocus.detail == NotifyPointer ?
4621 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
4622 dpyinfo, frame, bufp);
4623 break;
4625 case ClientMessage:
4626 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
4628 enum xembed_message msg = event->xclient.data.l[1];
4629 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
4630 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
4632 break;
4637 #if !defined USE_X_TOOLKIT && !defined USE_GTK
4638 /* Handle an event saying the mouse has moved out of an Emacs frame. */
4640 void
4641 x_mouse_leave (struct x_display_info *dpyinfo)
4643 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
4645 #endif
4647 /* The focus has changed, or we have redirected a frame's focus to
4648 another frame (this happens when a frame uses a surrogate
4649 mini-buffer frame). Shift the highlight as appropriate.
4651 The FRAME argument doesn't necessarily have anything to do with which
4652 frame is being highlighted or un-highlighted; we only use it to find
4653 the appropriate X display info. */
4655 static void
4656 XTframe_rehighlight (struct frame *frame)
4658 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
4661 static void
4662 x_frame_rehighlight (struct x_display_info *dpyinfo)
4664 struct frame *old_highlight = dpyinfo->x_highlight_frame;
4666 if (dpyinfo->x_focus_frame)
4668 dpyinfo->x_highlight_frame
4669 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
4670 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
4671 : dpyinfo->x_focus_frame);
4672 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
4674 fset_focus_frame (dpyinfo->x_focus_frame, Qnil);
4675 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
4678 else
4679 dpyinfo->x_highlight_frame = 0;
4681 if (dpyinfo->x_highlight_frame != old_highlight)
4683 if (old_highlight)
4684 frame_unhighlight (old_highlight);
4685 if (dpyinfo->x_highlight_frame)
4686 frame_highlight (dpyinfo->x_highlight_frame);
4692 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
4694 /* Initialize mode_switch_bit and modifier_meaning. */
4695 static void
4696 x_find_modifier_meanings (struct x_display_info *dpyinfo)
4698 int min_code, max_code;
4699 KeySym *syms;
4700 int syms_per_code;
4701 XModifierKeymap *mods;
4703 dpyinfo->meta_mod_mask = 0;
4704 dpyinfo->shift_lock_mask = 0;
4705 dpyinfo->alt_mod_mask = 0;
4706 dpyinfo->super_mod_mask = 0;
4707 dpyinfo->hyper_mod_mask = 0;
4709 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
4711 syms = XGetKeyboardMapping (dpyinfo->display,
4712 min_code, max_code - min_code + 1,
4713 &syms_per_code);
4714 mods = XGetModifierMapping (dpyinfo->display);
4716 /* Scan the modifier table to see which modifier bits the Meta and
4717 Alt keysyms are on. */
4719 int row, col; /* The row and column in the modifier table. */
4720 bool found_alt_or_meta;
4722 for (row = 3; row < 8; row++)
4724 found_alt_or_meta = false;
4725 for (col = 0; col < mods->max_keypermod; col++)
4727 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
4729 /* Zeroes are used for filler. Skip them. */
4730 if (code == 0)
4731 continue;
4733 /* Are any of this keycode's keysyms a meta key? */
4735 int code_col;
4737 for (code_col = 0; code_col < syms_per_code; code_col++)
4739 int sym = syms[((code - min_code) * syms_per_code) + code_col];
4741 switch (sym)
4743 case XK_Meta_L:
4744 case XK_Meta_R:
4745 found_alt_or_meta = true;
4746 dpyinfo->meta_mod_mask |= (1 << row);
4747 break;
4749 case XK_Alt_L:
4750 case XK_Alt_R:
4751 found_alt_or_meta = true;
4752 dpyinfo->alt_mod_mask |= (1 << row);
4753 break;
4755 case XK_Hyper_L:
4756 case XK_Hyper_R:
4757 if (!found_alt_or_meta)
4758 dpyinfo->hyper_mod_mask |= (1 << row);
4759 code_col = syms_per_code;
4760 col = mods->max_keypermod;
4761 break;
4763 case XK_Super_L:
4764 case XK_Super_R:
4765 if (!found_alt_or_meta)
4766 dpyinfo->super_mod_mask |= (1 << row);
4767 code_col = syms_per_code;
4768 col = mods->max_keypermod;
4769 break;
4771 case XK_Shift_Lock:
4772 /* Ignore this if it's not on the lock modifier. */
4773 if (!found_alt_or_meta && ((1 << row) == LockMask))
4774 dpyinfo->shift_lock_mask = LockMask;
4775 code_col = syms_per_code;
4776 col = mods->max_keypermod;
4777 break;
4785 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
4786 if (! dpyinfo->meta_mod_mask)
4788 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
4789 dpyinfo->alt_mod_mask = 0;
4792 /* If some keys are both alt and meta,
4793 make them just meta, not alt. */
4794 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
4796 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
4799 XFree (syms);
4800 XFreeModifiermap (mods);
4803 /* Convert between the modifier bits X uses and the modifier bits
4804 Emacs uses. */
4807 x_x_to_emacs_modifiers (struct x_display_info *dpyinfo, int state)
4809 int mod_ctrl = ctrl_modifier;
4810 int mod_meta = meta_modifier;
4811 int mod_alt = alt_modifier;
4812 int mod_hyper = hyper_modifier;
4813 int mod_super = super_modifier;
4814 Lisp_Object tem;
4816 tem = Fget (Vx_ctrl_keysym, Qmodifier_value);
4817 if (INTEGERP (tem)) mod_ctrl = XINT (tem) & INT_MAX;
4818 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4819 if (INTEGERP (tem)) mod_alt = XINT (tem) & INT_MAX;
4820 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4821 if (INTEGERP (tem)) mod_meta = XINT (tem) & INT_MAX;
4822 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4823 if (INTEGERP (tem)) mod_hyper = XINT (tem) & INT_MAX;
4824 tem = Fget (Vx_super_keysym, Qmodifier_value);
4825 if (INTEGERP (tem)) mod_super = XINT (tem) & INT_MAX;
4827 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
4828 | ((state & ControlMask) ? mod_ctrl : 0)
4829 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
4830 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
4831 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
4832 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
4835 static int
4836 x_emacs_to_x_modifiers (struct x_display_info *dpyinfo, EMACS_INT state)
4838 EMACS_INT mod_ctrl = ctrl_modifier;
4839 EMACS_INT mod_meta = meta_modifier;
4840 EMACS_INT mod_alt = alt_modifier;
4841 EMACS_INT mod_hyper = hyper_modifier;
4842 EMACS_INT mod_super = super_modifier;
4844 Lisp_Object tem;
4846 tem = Fget (Vx_ctrl_keysym, Qmodifier_value);
4847 if (INTEGERP (tem)) mod_ctrl = XINT (tem);
4848 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4849 if (INTEGERP (tem)) mod_alt = XINT (tem);
4850 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4851 if (INTEGERP (tem)) mod_meta = XINT (tem);
4852 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4853 if (INTEGERP (tem)) mod_hyper = XINT (tem);
4854 tem = Fget (Vx_super_keysym, Qmodifier_value);
4855 if (INTEGERP (tem)) mod_super = XINT (tem);
4858 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
4859 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
4860 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
4861 | ((state & shift_modifier) ? ShiftMask : 0)
4862 | ((state & mod_ctrl) ? ControlMask : 0)
4863 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
4866 /* Convert a keysym to its name. */
4868 char *
4869 x_get_keysym_name (int keysym)
4871 char *value;
4873 block_input ();
4874 value = XKeysymToString (keysym);
4875 unblock_input ();
4877 return value;
4880 /* Mouse clicks and mouse movement. Rah.
4882 Formerly, we used PointerMotionHintMask (in standard_event_mask)
4883 so that we would have to call XQueryPointer after each MotionNotify
4884 event to ask for another such event. However, this made mouse tracking
4885 slow, and there was a bug that made it eventually stop.
4887 Simply asking for MotionNotify all the time seems to work better.
4889 In order to avoid asking for motion events and then throwing most
4890 of them away or busy-polling the server for mouse positions, we ask
4891 the server for pointer motion hints. This means that we get only
4892 one event per group of mouse movements. "Groups" are delimited by
4893 other kinds of events (focus changes and button clicks, for
4894 example), or by XQueryPointer calls; when one of these happens, we
4895 get another MotionNotify event the next time the mouse moves. This
4896 is at least as efficient as getting motion events when mouse
4897 tracking is on, and I suspect only negligibly worse when tracking
4898 is off. */
4900 /* Prepare a mouse-event in *RESULT for placement in the input queue.
4902 If the event is a button press, then note that we have grabbed
4903 the mouse. */
4905 static Lisp_Object
4906 construct_mouse_click (struct input_event *result,
4907 const XButtonEvent *event,
4908 struct frame *f)
4910 /* Make the event type NO_EVENT; we'll change that when we decide
4911 otherwise. */
4912 result->kind = MOUSE_CLICK_EVENT;
4913 result->code = event->button - Button1;
4914 result->timestamp = event->time;
4915 result->modifiers = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f),
4916 event->state)
4917 | (event->type == ButtonRelease
4918 ? up_modifier
4919 : down_modifier));
4921 XSETINT (result->x, event->x);
4922 XSETINT (result->y, event->y);
4923 XSETFRAME (result->frame_or_window, f);
4924 result->arg = Qnil;
4925 return Qnil;
4928 /* Function to report a mouse movement to the mainstream Emacs code.
4929 The input handler calls this.
4931 We have received a mouse movement event, which is given in *event.
4932 If the mouse is over a different glyph than it was last time, tell
4933 the mainstream emacs code by setting mouse_moved. If not, ask for
4934 another motion event, so we can check again the next time it moves. */
4936 static bool
4937 note_mouse_movement (struct frame *frame, const XMotionEvent *event)
4939 XRectangle *r;
4940 struct x_display_info *dpyinfo;
4942 if (!FRAME_X_OUTPUT (frame))
4943 return false;
4945 dpyinfo = FRAME_DISPLAY_INFO (frame);
4946 dpyinfo->last_mouse_movement_time = event->time;
4947 dpyinfo->last_mouse_motion_frame = frame;
4948 dpyinfo->last_mouse_motion_x = event->x;
4949 dpyinfo->last_mouse_motion_y = event->y;
4951 if (event->window != FRAME_X_WINDOW (frame))
4953 frame->mouse_moved = true;
4954 dpyinfo->last_mouse_scroll_bar = NULL;
4955 note_mouse_highlight (frame, -1, -1);
4956 dpyinfo->last_mouse_glyph_frame = NULL;
4957 return true;
4961 /* Has the mouse moved off the glyph it was on at the last sighting? */
4962 r = &dpyinfo->last_mouse_glyph;
4963 if (frame != dpyinfo->last_mouse_glyph_frame
4964 || event->x < r->x || event->x >= r->x + r->width
4965 || event->y < r->y || event->y >= r->y + r->height)
4967 frame->mouse_moved = true;
4968 dpyinfo->last_mouse_scroll_bar = NULL;
4969 note_mouse_highlight (frame, event->x, event->y);
4970 /* Remember which glyph we're now on. */
4971 remember_mouse_glyph (frame, event->x, event->y, r);
4972 dpyinfo->last_mouse_glyph_frame = frame;
4973 return true;
4976 return false;
4979 /* Return the current position of the mouse.
4980 *FP should be a frame which indicates which display to ask about.
4982 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
4983 and *PART to the frame, window, and scroll bar part that the mouse
4984 is over. Set *X and *Y to the portion and whole of the mouse's
4985 position on the scroll bar.
4987 If the mouse movement started elsewhere, set *FP to the frame the
4988 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
4989 the mouse is over.
4991 Set *TIMESTAMP to the server time-stamp for the time at which the mouse
4992 was at this position.
4994 Don't store anything if we don't have a valid set of values to report.
4996 This clears the mouse_moved flag, so we can wait for the next mouse
4997 movement. */
4999 static void
5000 XTmouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
5001 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
5002 Time *timestamp)
5004 struct frame *f1;
5005 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
5007 block_input ();
5009 if (dpyinfo->last_mouse_scroll_bar && insist == 0)
5011 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
5013 if (bar->horizontal)
5014 x_horizontal_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
5015 else
5016 x_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
5018 else
5020 Window root;
5021 int root_x, root_y;
5023 Window dummy_window;
5024 int dummy;
5026 Lisp_Object frame, tail;
5028 /* Clear the mouse-moved flag for every frame on this display. */
5029 FOR_EACH_FRAME (tail, frame)
5030 if (FRAME_X_P (XFRAME (frame))
5031 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
5032 XFRAME (frame)->mouse_moved = false;
5034 dpyinfo->last_mouse_scroll_bar = NULL;
5036 /* Figure out which root window we're on. */
5037 XQueryPointer (FRAME_X_DISPLAY (*fp),
5038 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
5040 /* The root window which contains the pointer. */
5041 &root,
5043 /* Trash which we can't trust if the pointer is on
5044 a different screen. */
5045 &dummy_window,
5047 /* The position on that root window. */
5048 &root_x, &root_y,
5050 /* More trash we can't trust. */
5051 &dummy, &dummy,
5053 /* Modifier keys and pointer buttons, about which
5054 we don't care. */
5055 (unsigned int *) &dummy);
5057 /* Now we have a position on the root; find the innermost window
5058 containing the pointer. */
5060 Window win, child;
5061 #ifdef USE_GTK
5062 Window first_win = 0;
5063 #endif
5064 int win_x, win_y;
5065 int parent_x = 0, parent_y = 0;
5067 win = root;
5069 /* XTranslateCoordinates can get errors if the window
5070 structure is changing at the same time this function
5071 is running. So at least we must not crash from them. */
5073 x_catch_errors (FRAME_X_DISPLAY (*fp));
5075 if (x_mouse_grabbed (dpyinfo))
5077 /* If mouse was grabbed on a frame, give coords for that frame
5078 even if the mouse is now outside it. */
5079 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
5081 /* From-window. */
5082 root,
5084 /* To-window. */
5085 FRAME_X_WINDOW (dpyinfo->last_mouse_frame),
5087 /* From-position, to-position. */
5088 root_x, root_y, &win_x, &win_y,
5090 /* Child of win. */
5091 &child);
5092 f1 = dpyinfo->last_mouse_frame;
5094 else
5096 while (true)
5098 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
5100 /* From-window, to-window. */
5101 root, win,
5103 /* From-position, to-position. */
5104 root_x, root_y, &win_x, &win_y,
5106 /* Child of win. */
5107 &child);
5109 if (child == None || child == win)
5111 #ifdef USE_GTK
5112 /* On GTK we have not inspected WIN yet. If it has
5113 a frame and that frame has a parent, use it. */
5114 struct frame *f = x_window_to_frame (dpyinfo, win);
5116 if (f && FRAME_PARENT_FRAME (f))
5117 first_win = win;
5118 #endif
5119 break;
5121 #ifdef USE_GTK
5122 /* We don't wan't to know the innermost window. We
5123 want the edit window. For non-Gtk+ the innermost
5124 window is the edit window. For Gtk+ it might not
5125 be. It might be the tool bar for example. */
5126 if (x_window_to_frame (dpyinfo, win))
5127 /* But don't hurry. We might find a child frame
5128 beneath. */
5129 first_win = win;
5130 #endif
5131 win = child;
5132 parent_x = win_x;
5133 parent_y = win_y;
5136 #ifdef USE_GTK
5137 if (first_win)
5138 win = first_win;
5139 #endif
5141 /* Now we know that:
5142 win is the innermost window containing the pointer
5143 (XTC says it has no child containing the pointer),
5144 win_x and win_y are the pointer's position in it
5145 (XTC did this the last time through), and
5146 parent_x and parent_y are the pointer's position in win's parent.
5147 (They are what win_x and win_y were when win was child.
5148 If win is the root window, it has no parent, and
5149 parent_{x,y} are invalid, but that's okay, because we'll
5150 never use them in that case.) */
5152 #ifdef USE_GTK
5153 /* We don't wan't to know the innermost window. We
5154 want the edit window. */
5155 f1 = x_window_to_frame (dpyinfo, win);
5156 #else
5157 /* Is win one of our frames? */
5158 f1 = x_any_window_to_frame (dpyinfo, win);
5159 #endif
5161 #ifdef USE_X_TOOLKIT
5162 /* If we end up with the menu bar window, say it's not
5163 on the frame. */
5164 if (f1 != NULL
5165 && f1->output_data.x->menubar_widget
5166 && win == XtWindow (f1->output_data.x->menubar_widget))
5167 f1 = NULL;
5168 #endif /* USE_X_TOOLKIT */
5171 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
5172 f1 = 0;
5174 x_uncatch_errors_after_check ();
5176 /* If not, is it one of our scroll bars? */
5177 if (! f1)
5179 struct scroll_bar *bar;
5181 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win, 2);
5183 if (bar)
5185 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5186 win_x = parent_x;
5187 win_y = parent_y;
5191 if (f1 == 0 && insist > 0)
5192 f1 = SELECTED_FRAME ();
5194 if (f1)
5196 /* Ok, we found a frame. Store all the values.
5197 last_mouse_glyph is a rectangle used to reduce the
5198 generation of mouse events. To not miss any motion
5199 events, we must divide the frame into rectangles of the
5200 size of the smallest character that could be displayed
5201 on it, i.e. into the same rectangles that matrices on
5202 the frame are divided into. */
5204 /* FIXME: what if F1 is not an X frame? */
5205 dpyinfo = FRAME_DISPLAY_INFO (f1);
5206 remember_mouse_glyph (f1, win_x, win_y, &dpyinfo->last_mouse_glyph);
5207 dpyinfo->last_mouse_glyph_frame = f1;
5209 *bar_window = Qnil;
5210 *part = 0;
5211 *fp = f1;
5212 XSETINT (*x, win_x);
5213 XSETINT (*y, win_y);
5214 *timestamp = dpyinfo->last_mouse_movement_time;
5219 unblock_input ();
5224 /***********************************************************************
5225 Scroll bars
5226 ***********************************************************************/
5228 /* Scroll bar support. */
5230 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
5231 manages it.
5232 This can be called in GC, so we have to make sure to strip off mark
5233 bits. */
5235 static struct scroll_bar *
5236 x_window_to_scroll_bar (Display *display, Window window_id, int type)
5238 Lisp_Object tail, frame;
5240 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
5241 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
5242 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
5244 FOR_EACH_FRAME (tail, frame)
5246 Lisp_Object bar, condemned;
5248 if (! FRAME_X_P (XFRAME (frame)))
5249 continue;
5251 /* Scan this frame's scroll bar list for a scroll bar with the
5252 right window ID. */
5253 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
5254 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
5255 /* This trick allows us to search both the ordinary and
5256 condemned scroll bar lists with one loop. */
5257 ! NILP (bar) || (bar = condemned,
5258 condemned = Qnil,
5259 ! NILP (bar));
5260 bar = XSCROLL_BAR (bar)->next)
5261 if (XSCROLL_BAR (bar)->x_window == window_id
5262 && FRAME_X_DISPLAY (XFRAME (frame)) == display
5263 && (type = 2
5264 || (type == 1 && XSCROLL_BAR (bar)->horizontal)
5265 || (type == 0 && !XSCROLL_BAR (bar)->horizontal)))
5266 return XSCROLL_BAR (bar);
5269 return NULL;
5273 #if defined USE_LUCID
5275 /* Return the Lucid menu bar WINDOW is part of. Return null
5276 if WINDOW is not part of a menu bar. */
5278 static Widget
5279 x_window_to_menu_bar (Window window)
5281 Lisp_Object tail, frame;
5283 FOR_EACH_FRAME (tail, frame)
5284 if (FRAME_X_P (XFRAME (frame)))
5286 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
5288 if (menu_bar && xlwmenu_window_p (menu_bar, window))
5289 return menu_bar;
5291 return NULL;
5294 #endif /* USE_LUCID */
5297 /************************************************************************
5298 Toolkit scroll bars
5299 ************************************************************************/
5301 #ifdef USE_TOOLKIT_SCROLL_BARS
5303 static void x_send_scroll_bar_event (Lisp_Object, enum scroll_bar_part,
5304 int, int, bool);
5306 /* Lisp window being scrolled. Set when starting to interact with
5307 a toolkit scroll bar, reset to nil when ending the interaction. */
5309 static Lisp_Object window_being_scrolled;
5311 /* Whether this is an Xaw with arrow-scrollbars. This should imply
5312 that movements of 1/20 of the screen size are mapped to up/down. */
5314 #ifndef USE_GTK
5315 /* Id of action hook installed for scroll bars. */
5317 static XtActionHookId action_hook_id;
5318 static XtActionHookId horizontal_action_hook_id;
5320 static Boolean xaw3d_arrow_scroll;
5322 /* Whether the drag scrolling maintains the mouse at the top of the
5323 thumb. If not, resizing the thumb needs to be done more carefully
5324 to avoid jerkiness. */
5326 static Boolean xaw3d_pick_top;
5328 /* Action hook installed via XtAppAddActionHook when toolkit scroll
5329 bars are used.. The hook is responsible for detecting when
5330 the user ends an interaction with the scroll bar, and generates
5331 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
5333 static void
5334 xt_action_hook (Widget widget, XtPointer client_data, String action_name,
5335 XEvent *event, String *params, Cardinal *num_params)
5337 bool scroll_bar_p;
5338 const char *end_action;
5340 #ifdef USE_MOTIF
5341 scroll_bar_p = XmIsScrollBar (widget);
5342 end_action = "Release";
5343 #else /* !USE_MOTIF i.e. use Xaw */
5344 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
5345 end_action = "EndScroll";
5346 #endif /* USE_MOTIF */
5348 if (scroll_bar_p
5349 && strcmp (action_name, end_action) == 0
5350 && WINDOWP (window_being_scrolled))
5352 struct window *w;
5353 struct scroll_bar *bar;
5355 x_send_scroll_bar_event (window_being_scrolled,
5356 scroll_bar_end_scroll, 0, 0, false);
5357 w = XWINDOW (window_being_scrolled);
5358 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5360 if (bar->dragging != -1)
5362 bar->dragging = -1;
5363 /* The thumb size is incorrect while dragging: fix it. */
5364 set_vertical_scroll_bar (w);
5366 window_being_scrolled = Qnil;
5367 #if defined (USE_LUCID)
5368 bar->last_seen_part = scroll_bar_nowhere;
5369 #endif
5370 /* Xt timeouts no longer needed. */
5371 toolkit_scroll_bar_interaction = false;
5376 static void
5377 xt_horizontal_action_hook (Widget widget, XtPointer client_data, String action_name,
5378 XEvent *event, String *params, Cardinal *num_params)
5380 bool scroll_bar_p;
5381 const char *end_action;
5383 #ifdef USE_MOTIF
5384 scroll_bar_p = XmIsScrollBar (widget);
5385 end_action = "Release";
5386 #else /* !USE_MOTIF i.e. use Xaw */
5387 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
5388 end_action = "EndScroll";
5389 #endif /* USE_MOTIF */
5391 if (scroll_bar_p
5392 && strcmp (action_name, end_action) == 0
5393 && WINDOWP (window_being_scrolled))
5395 struct window *w;
5396 struct scroll_bar *bar;
5398 x_send_scroll_bar_event (window_being_scrolled,
5399 scroll_bar_end_scroll, 0, 0, true);
5400 w = XWINDOW (window_being_scrolled);
5401 if (!NILP (w->horizontal_scroll_bar))
5403 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
5404 if (bar->dragging != -1)
5406 bar->dragging = -1;
5407 /* The thumb size is incorrect while dragging: fix it. */
5408 set_horizontal_scroll_bar (w);
5410 window_being_scrolled = Qnil;
5411 #if defined (USE_LUCID)
5412 bar->last_seen_part = scroll_bar_nowhere;
5413 #endif
5414 /* Xt timeouts no longer needed. */
5415 toolkit_scroll_bar_interaction = false;
5419 #endif /* not USE_GTK */
5421 /* Send a client message with message type Xatom_Scrollbar for a
5422 scroll action to the frame of WINDOW. PART is a value identifying
5423 the part of the scroll bar that was clicked on. PORTION is the
5424 amount to scroll of a whole of WHOLE. */
5426 static void
5427 x_send_scroll_bar_event (Lisp_Object window, enum scroll_bar_part part,
5428 int portion, int whole, bool horizontal)
5430 XEvent event;
5431 XClientMessageEvent *ev = &event.xclient;
5432 struct window *w = XWINDOW (window);
5433 struct frame *f = XFRAME (w->frame);
5434 intptr_t iw = (intptr_t) w;
5435 verify (INTPTR_WIDTH <= 64);
5436 int sign_shift = INTPTR_WIDTH - 32;
5438 block_input ();
5440 /* Construct a ClientMessage event to send to the frame. */
5441 ev->type = ClientMessage;
5442 ev->message_type = (horizontal
5443 ? FRAME_DISPLAY_INFO (f)->Xatom_Horizontal_Scrollbar
5444 : FRAME_DISPLAY_INFO (f)->Xatom_Scrollbar);
5445 ev->display = FRAME_X_DISPLAY (f);
5446 ev->window = FRAME_X_WINDOW (f);
5447 ev->format = 32;
5449 /* A 32-bit X client on a 64-bit X server can pass a window pointer
5450 as-is. A 64-bit client on a 32-bit X server is in trouble
5451 because a pointer does not fit and would be truncated while
5452 passing through the server. So use two slots and hope that X12
5453 will resolve such issues someday. */
5454 ev->data.l[0] = iw >> 31 >> 1;
5455 ev->data.l[1] = sign_shift <= 0 ? iw : iw << sign_shift >> sign_shift;
5456 ev->data.l[2] = part;
5457 ev->data.l[3] = portion;
5458 ev->data.l[4] = whole;
5460 /* Make Xt timeouts work while the scroll bar is active. */
5461 #ifdef USE_X_TOOLKIT
5462 toolkit_scroll_bar_interaction = true;
5463 x_activate_timeout_atimer ();
5464 #endif
5466 /* Setting the event mask to zero means that the message will
5467 be sent to the client that created the window, and if that
5468 window no longer exists, no event will be sent. */
5469 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
5470 unblock_input ();
5474 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
5475 in *IEVENT. */
5477 static void
5478 x_scroll_bar_to_input_event (const XEvent *event,
5479 struct input_event *ievent)
5481 const XClientMessageEvent *ev = &event->xclient;
5482 Lisp_Object window;
5483 struct window *w;
5485 /* See the comment in the function above. */
5486 intptr_t iw0 = ev->data.l[0];
5487 intptr_t iw1 = ev->data.l[1];
5488 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
5489 w = (struct window *) iw;
5491 XSETWINDOW (window, w);
5493 ievent->kind = SCROLL_BAR_CLICK_EVENT;
5494 ievent->frame_or_window = window;
5495 ievent->arg = Qnil;
5496 #ifdef USE_GTK
5497 ievent->timestamp = CurrentTime;
5498 #else
5499 ievent->timestamp =
5500 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
5501 #endif
5502 ievent->code = 0;
5503 ievent->part = ev->data.l[2];
5504 ievent->x = make_number (ev->data.l[3]);
5505 ievent->y = make_number (ev->data.l[4]);
5506 ievent->modifiers = 0;
5509 /* Transform a horizontal scroll bar ClientMessage EVENT to an Emacs
5510 input event in *IEVENT. */
5512 static void
5513 x_horizontal_scroll_bar_to_input_event (const XEvent *event,
5514 struct input_event *ievent)
5516 const XClientMessageEvent *ev = &event->xclient;
5517 Lisp_Object window;
5518 struct window *w;
5520 /* See the comment in the function above. */
5521 intptr_t iw0 = ev->data.l[0];
5522 intptr_t iw1 = ev->data.l[1];
5523 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
5524 w = (struct window *) iw;
5526 XSETWINDOW (window, w);
5528 ievent->kind = HORIZONTAL_SCROLL_BAR_CLICK_EVENT;
5529 ievent->frame_or_window = window;
5530 ievent->arg = Qnil;
5531 #ifdef USE_GTK
5532 ievent->timestamp = CurrentTime;
5533 #else
5534 ievent->timestamp =
5535 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
5536 #endif
5537 ievent->code = 0;
5538 ievent->part = ev->data.l[2];
5539 ievent->x = make_number (ev->data.l[3]);
5540 ievent->y = make_number (ev->data.l[4]);
5541 ievent->modifiers = 0;
5545 #ifdef USE_MOTIF
5547 /* Minimum and maximum values used for Motif scroll bars. */
5549 #define XM_SB_MAX 10000000
5551 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
5552 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
5553 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
5555 static void
5556 xm_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5558 struct scroll_bar *bar = client_data;
5559 XmScrollBarCallbackStruct *cs = call_data;
5560 enum scroll_bar_part part = scroll_bar_nowhere;
5561 bool horizontal = bar->horizontal;
5562 int whole = 0, portion = 0;
5564 switch (cs->reason)
5566 case XmCR_DECREMENT:
5567 bar->dragging = -1;
5568 part = horizontal ? scroll_bar_left_arrow : scroll_bar_up_arrow;
5569 break;
5571 case XmCR_INCREMENT:
5572 bar->dragging = -1;
5573 part = horizontal ? scroll_bar_right_arrow : scroll_bar_down_arrow;
5574 break;
5576 case XmCR_PAGE_DECREMENT:
5577 bar->dragging = -1;
5578 part = horizontal ? scroll_bar_before_handle : scroll_bar_above_handle;
5579 break;
5581 case XmCR_PAGE_INCREMENT:
5582 bar->dragging = -1;
5583 part = horizontal ? scroll_bar_after_handle : scroll_bar_below_handle;
5584 break;
5586 case XmCR_TO_TOP:
5587 bar->dragging = -1;
5588 part = horizontal ? scroll_bar_to_leftmost : scroll_bar_to_top;
5589 break;
5591 case XmCR_TO_BOTTOM:
5592 bar->dragging = -1;
5593 part = horizontal ? scroll_bar_to_rightmost : scroll_bar_to_bottom;
5594 break;
5596 case XmCR_DRAG:
5598 int slider_size;
5600 block_input ();
5601 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
5602 unblock_input ();
5604 if (horizontal)
5606 portion = bar->whole * ((float)cs->value / XM_SB_MAX);
5607 whole = bar->whole * ((float)(XM_SB_MAX - slider_size) / XM_SB_MAX);
5608 portion = min (portion, whole);
5609 part = scroll_bar_horizontal_handle;
5611 else
5613 whole = XM_SB_MAX - slider_size;
5614 portion = min (cs->value, whole);
5615 part = scroll_bar_handle;
5618 bar->dragging = cs->value;
5620 break;
5622 case XmCR_VALUE_CHANGED:
5623 break;
5626 if (part != scroll_bar_nowhere)
5628 window_being_scrolled = bar->window;
5629 x_send_scroll_bar_event (bar->window, part, portion, whole,
5630 bar->horizontal);
5634 #elif defined USE_GTK
5636 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
5637 bar widget. DATA is a pointer to the scroll_bar structure. */
5639 static gboolean
5640 xg_scroll_callback (GtkRange *range,
5641 GtkScrollType scroll,
5642 gdouble value,
5643 gpointer user_data)
5645 int whole = 0, portion = 0;
5646 struct scroll_bar *bar = user_data;
5647 enum scroll_bar_part part = scroll_bar_nowhere;
5648 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
5649 struct frame *f = g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
5651 if (xg_ignore_gtk_scrollbar) return false;
5653 switch (scroll)
5655 case GTK_SCROLL_JUMP:
5656 /* Buttons 1 2 or 3 must be grabbed. */
5657 if (FRAME_DISPLAY_INFO (f)->grabbed != 0
5658 && FRAME_DISPLAY_INFO (f)->grabbed < (1 << 4))
5660 if (bar->horizontal)
5662 part = scroll_bar_horizontal_handle;
5663 whole = (int)(gtk_adjustment_get_upper (adj) -
5664 gtk_adjustment_get_page_size (adj));
5665 portion = min ((int)value, whole);
5666 bar->dragging = portion;
5668 else
5670 part = scroll_bar_handle;
5671 whole = gtk_adjustment_get_upper (adj) -
5672 gtk_adjustment_get_page_size (adj);
5673 portion = min ((int)value, whole);
5674 bar->dragging = portion;
5677 break;
5678 case GTK_SCROLL_STEP_BACKWARD:
5679 part = (bar->horizontal
5680 ? scroll_bar_left_arrow : scroll_bar_up_arrow);
5681 bar->dragging = -1;
5682 break;
5683 case GTK_SCROLL_STEP_FORWARD:
5684 part = (bar->horizontal
5685 ? scroll_bar_right_arrow : scroll_bar_down_arrow);
5686 bar->dragging = -1;
5687 break;
5688 case GTK_SCROLL_PAGE_BACKWARD:
5689 part = (bar->horizontal
5690 ? scroll_bar_before_handle : scroll_bar_above_handle);
5691 bar->dragging = -1;
5692 break;
5693 case GTK_SCROLL_PAGE_FORWARD:
5694 part = (bar->horizontal
5695 ? scroll_bar_after_handle : scroll_bar_below_handle);
5696 bar->dragging = -1;
5697 break;
5698 default:
5699 break;
5702 if (part != scroll_bar_nowhere)
5704 window_being_scrolled = bar->window;
5705 x_send_scroll_bar_event (bar->window, part, portion, whole,
5706 bar->horizontal);
5709 return false;
5712 /* Callback for button release. Sets dragging to -1 when dragging is done. */
5714 static gboolean
5715 xg_end_scroll_callback (GtkWidget *widget,
5716 GdkEventButton *event,
5717 gpointer user_data)
5719 struct scroll_bar *bar = user_data;
5720 bar->dragging = -1;
5721 if (WINDOWP (window_being_scrolled))
5723 x_send_scroll_bar_event (window_being_scrolled,
5724 scroll_bar_end_scroll, 0, 0, bar->horizontal);
5725 window_being_scrolled = Qnil;
5728 return false;
5732 #else /* not USE_GTK and not USE_MOTIF */
5734 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
5735 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
5736 scroll bar struct. CALL_DATA is a pointer to a float saying where
5737 the thumb is. */
5739 static void
5740 xaw_jump_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5742 struct scroll_bar *bar = client_data;
5743 float *top_addr = call_data;
5744 float top = *top_addr;
5745 float shown;
5746 int whole, portion, height, width;
5747 enum scroll_bar_part part;
5748 bool horizontal = bar->horizontal;
5750 if (horizontal)
5752 /* Get the size of the thumb, a value between 0 and 1. */
5753 block_input ();
5754 XtVaGetValues (widget, XtNshown, &shown, XtNwidth, &width, NULL);
5755 unblock_input ();
5757 if (shown < 1)
5759 whole = bar->whole - (shown * bar->whole);
5760 portion = min (top * bar->whole, whole);
5762 else
5764 whole = bar->whole;
5765 portion = 0;
5768 part = scroll_bar_horizontal_handle;
5770 else
5772 /* Get the size of the thumb, a value between 0 and 1. */
5773 block_input ();
5774 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
5775 unblock_input ();
5777 whole = 10000000;
5778 portion = shown < 1 ? top * whole : 0;
5780 if (shown < 1 && (eabs (top + shown - 1) < 1.0f / height))
5781 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
5782 the bottom, so we force the scrolling whenever we see that we're
5783 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
5784 we try to ensure that we always stay two pixels away from the
5785 bottom). */
5786 part = scroll_bar_down_arrow;
5787 else
5788 part = scroll_bar_handle;
5791 window_being_scrolled = bar->window;
5792 bar->dragging = portion;
5793 bar->last_seen_part = part;
5794 x_send_scroll_bar_event (bar->window, part, portion, whole, bar->horizontal);
5798 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
5799 i.e. line or page up or down. WIDGET is the Xaw scroll bar
5800 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
5801 the scroll bar. CALL_DATA is an integer specifying the action that
5802 has taken place. Its magnitude is in the range 0..height of the
5803 scroll bar. Negative values mean scroll towards buffer start.
5804 Values < height of scroll bar mean line-wise movement. */
5806 static void
5807 xaw_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5809 struct scroll_bar *bar = client_data;
5810 /* The position really is stored cast to a pointer. */
5811 int position = (intptr_t) call_data;
5812 Dimension height, width;
5813 enum scroll_bar_part part;
5815 if (bar->horizontal)
5817 /* Get the width of the scroll bar. */
5818 block_input ();
5819 XtVaGetValues (widget, XtNwidth, &width, NULL);
5820 unblock_input ();
5822 if (eabs (position) >= width)
5823 part = (position < 0) ? scroll_bar_before_handle : scroll_bar_after_handle;
5825 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
5826 it maps line-movement to call_data = max(5, height/20). */
5827 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, width / 20))
5828 part = (position < 0) ? scroll_bar_left_arrow : scroll_bar_right_arrow;
5829 else
5830 part = scroll_bar_move_ratio;
5832 window_being_scrolled = bar->window;
5833 bar->dragging = -1;
5834 bar->last_seen_part = part;
5835 x_send_scroll_bar_event (bar->window, part, position, width,
5836 bar->horizontal);
5838 else
5841 /* Get the height of the scroll bar. */
5842 block_input ();
5843 XtVaGetValues (widget, XtNheight, &height, NULL);
5844 unblock_input ();
5846 if (eabs (position) >= height)
5847 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
5849 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
5850 it maps line-movement to call_data = max(5, height/20). */
5851 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
5852 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
5853 else
5854 part = scroll_bar_move_ratio;
5856 window_being_scrolled = bar->window;
5857 bar->dragging = -1;
5858 bar->last_seen_part = part;
5859 x_send_scroll_bar_event (bar->window, part, position, height,
5860 bar->horizontal);
5864 #endif /* not USE_GTK and not USE_MOTIF */
5866 #define SCROLL_BAR_NAME "verticalScrollBar"
5867 #define SCROLL_BAR_HORIZONTAL_NAME "horizontalScrollBar"
5869 /* Create the widget for scroll bar BAR on frame F. Record the widget
5870 and X window of the scroll bar in BAR. */
5872 #ifdef USE_GTK
5873 static void
5874 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5876 const char *scroll_bar_name = SCROLL_BAR_NAME;
5878 block_input ();
5879 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
5880 G_CALLBACK (xg_end_scroll_callback),
5881 scroll_bar_name);
5882 unblock_input ();
5885 static void
5886 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5888 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
5890 block_input ();
5891 xg_create_horizontal_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
5892 G_CALLBACK (xg_end_scroll_callback),
5893 scroll_bar_name);
5894 unblock_input ();
5897 #else /* not USE_GTK */
5899 static void
5900 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5902 Window xwindow;
5903 Widget widget;
5904 Arg av[20];
5905 int ac = 0;
5906 const char *scroll_bar_name = SCROLL_BAR_NAME;
5907 unsigned long pixel;
5909 block_input ();
5911 #ifdef USE_MOTIF
5912 /* Set resources. Create the widget. */
5913 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5914 XtSetArg (av[ac], XmNminimum, 0); ++ac;
5915 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
5916 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
5917 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
5918 XtSetArg (av[ac], XmNincrement, 1); ++ac;
5919 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
5921 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5922 if (pixel != -1)
5924 XtSetArg (av[ac], XmNforeground, pixel);
5925 ++ac;
5928 pixel = f->output_data.x->scroll_bar_background_pixel;
5929 if (pixel != -1)
5931 XtSetArg (av[ac], XmNbackground, pixel);
5932 ++ac;
5935 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
5936 (char *) scroll_bar_name, av, ac);
5938 /* Add one callback for everything that can happen. */
5939 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
5940 (XtPointer) bar);
5941 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
5942 (XtPointer) bar);
5943 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
5944 (XtPointer) bar);
5945 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
5946 (XtPointer) bar);
5947 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
5948 (XtPointer) bar);
5949 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
5950 (XtPointer) bar);
5951 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
5952 (XtPointer) bar);
5954 /* Realize the widget. Only after that is the X window created. */
5955 XtRealizeWidget (widget);
5957 /* Set the cursor to an arrow. I didn't find a resource to do that.
5958 And I'm wondering why it hasn't an arrow cursor by default. */
5959 XDefineCursor (XtDisplay (widget), XtWindow (widget),
5960 f->output_data.x->nontext_cursor);
5962 #else /* !USE_MOTIF i.e. use Xaw */
5964 /* Set resources. Create the widget. The background of the
5965 Xaw3d scroll bar widget is a little bit light for my taste.
5966 We don't alter it here to let users change it according
5967 to their taste with `emacs*verticalScrollBar.background: xxx'. */
5968 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5969 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
5970 /* For smoother scrolling with Xaw3d -sm */
5971 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
5973 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5974 if (pixel != -1)
5976 XtSetArg (av[ac], XtNforeground, pixel);
5977 ++ac;
5980 pixel = f->output_data.x->scroll_bar_background_pixel;
5981 if (pixel != -1)
5983 XtSetArg (av[ac], XtNbackground, pixel);
5984 ++ac;
5987 /* Top/bottom shadow colors. */
5989 /* Allocate them, if necessary. */
5990 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
5992 pixel = f->output_data.x->scroll_bar_background_pixel;
5993 if (pixel != -1)
5995 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5996 FRAME_X_COLORMAP (f),
5997 &pixel, 1.2, 0x8000))
5998 pixel = -1;
5999 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
6002 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
6004 pixel = f->output_data.x->scroll_bar_background_pixel;
6005 if (pixel != -1)
6007 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
6008 FRAME_X_COLORMAP (f),
6009 &pixel, 0.6, 0x4000))
6010 pixel = -1;
6011 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
6015 #ifdef XtNbeNiceToColormap
6016 /* Tell the toolkit about them. */
6017 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
6018 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
6019 /* We tried to allocate a color for the top/bottom shadow, and
6020 failed, so tell Xaw3d to use dithering instead. */
6021 /* But only if we have a small colormap. Xaw3d can allocate nice
6022 colors itself. */
6024 XtSetArg (av[ac], XtNbeNiceToColormap,
6025 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
6026 ++ac;
6028 else
6029 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
6030 be more consistent with other emacs 3d colors, and since Xaw3d is
6031 not good at dealing with allocation failure. */
6033 /* This tells Xaw3d to use real colors instead of dithering for
6034 the shadows. */
6035 XtSetArg (av[ac], XtNbeNiceToColormap, False);
6036 ++ac;
6038 /* Specify the colors. */
6039 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
6040 if (pixel != -1)
6042 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
6043 ++ac;
6045 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
6046 if (pixel != -1)
6048 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
6049 ++ac;
6052 #endif
6054 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
6055 f->output_data.x->edit_widget, av, ac);
6058 char const *initial = "";
6059 char const *val = initial;
6060 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
6061 #ifdef XtNarrowScrollbars
6062 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
6063 #endif
6064 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
6065 if (xaw3d_arrow_scroll || val == initial)
6066 { /* ARROW_SCROLL */
6067 xaw3d_arrow_scroll = True;
6068 /* Isn't that just a personal preference ? --Stef */
6069 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
6073 /* Define callbacks. */
6074 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
6075 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
6076 (XtPointer) bar);
6078 /* Realize the widget. Only after that is the X window created. */
6079 XtRealizeWidget (widget);
6081 #endif /* !USE_MOTIF */
6083 /* Install an action hook that lets us detect when the user
6084 finishes interacting with a scroll bar. */
6085 if (action_hook_id == 0)
6086 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
6088 /* Remember X window and widget in the scroll bar vector. */
6089 SET_SCROLL_BAR_X_WIDGET (bar, widget);
6090 xwindow = XtWindow (widget);
6091 bar->x_window = xwindow;
6092 bar->whole = 1;
6093 bar->horizontal = false;
6095 unblock_input ();
6098 static void
6099 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
6101 Window xwindow;
6102 Widget widget;
6103 Arg av[20];
6104 int ac = 0;
6105 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
6106 unsigned long pixel;
6108 block_input ();
6110 #ifdef USE_MOTIF
6111 /* Set resources. Create the widget. */
6112 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
6113 XtSetArg (av[ac], XmNminimum, 0); ++ac;
6114 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
6115 XtSetArg (av[ac], XmNorientation, XmHORIZONTAL); ++ac;
6116 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_RIGHT), ++ac;
6117 XtSetArg (av[ac], XmNincrement, 1); ++ac;
6118 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
6120 pixel = f->output_data.x->scroll_bar_foreground_pixel;
6121 if (pixel != -1)
6123 XtSetArg (av[ac], XmNforeground, pixel);
6124 ++ac;
6127 pixel = f->output_data.x->scroll_bar_background_pixel;
6128 if (pixel != -1)
6130 XtSetArg (av[ac], XmNbackground, pixel);
6131 ++ac;
6134 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
6135 (char *) scroll_bar_name, av, ac);
6137 /* Add one callback for everything that can happen. */
6138 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
6139 (XtPointer) bar);
6140 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
6141 (XtPointer) bar);
6142 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
6143 (XtPointer) bar);
6144 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
6145 (XtPointer) bar);
6146 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
6147 (XtPointer) bar);
6148 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
6149 (XtPointer) bar);
6150 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
6151 (XtPointer) bar);
6153 /* Realize the widget. Only after that is the X window created. */
6154 XtRealizeWidget (widget);
6156 /* Set the cursor to an arrow. I didn't find a resource to do that.
6157 And I'm wondering why it hasn't an arrow cursor by default. */
6158 XDefineCursor (XtDisplay (widget), XtWindow (widget),
6159 f->output_data.x->nontext_cursor);
6161 #else /* !USE_MOTIF i.e. use Xaw */
6163 /* Set resources. Create the widget. The background of the
6164 Xaw3d scroll bar widget is a little bit light for my taste.
6165 We don't alter it here to let users change it according
6166 to their taste with `emacs*verticalScrollBar.background: xxx'. */
6167 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
6168 XtSetArg (av[ac], XtNorientation, XtorientHorizontal); ++ac;
6169 /* For smoother scrolling with Xaw3d -sm */
6170 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
6172 pixel = f->output_data.x->scroll_bar_foreground_pixel;
6173 if (pixel != -1)
6175 XtSetArg (av[ac], XtNforeground, pixel);
6176 ++ac;
6179 pixel = f->output_data.x->scroll_bar_background_pixel;
6180 if (pixel != -1)
6182 XtSetArg (av[ac], XtNbackground, pixel);
6183 ++ac;
6186 /* Top/bottom shadow colors. */
6188 /* Allocate them, if necessary. */
6189 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
6191 pixel = f->output_data.x->scroll_bar_background_pixel;
6192 if (pixel != -1)
6194 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
6195 FRAME_X_COLORMAP (f),
6196 &pixel, 1.2, 0x8000))
6197 pixel = -1;
6198 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
6201 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
6203 pixel = f->output_data.x->scroll_bar_background_pixel;
6204 if (pixel != -1)
6206 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
6207 FRAME_X_COLORMAP (f),
6208 &pixel, 0.6, 0x4000))
6209 pixel = -1;
6210 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
6214 #ifdef XtNbeNiceToColormap
6215 /* Tell the toolkit about them. */
6216 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
6217 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
6218 /* We tried to allocate a color for the top/bottom shadow, and
6219 failed, so tell Xaw3d to use dithering instead. */
6220 /* But only if we have a small colormap. Xaw3d can allocate nice
6221 colors itself. */
6223 XtSetArg (av[ac], XtNbeNiceToColormap,
6224 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
6225 ++ac;
6227 else
6228 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
6229 be more consistent with other emacs 3d colors, and since Xaw3d is
6230 not good at dealing with allocation failure. */
6232 /* This tells Xaw3d to use real colors instead of dithering for
6233 the shadows. */
6234 XtSetArg (av[ac], XtNbeNiceToColormap, False);
6235 ++ac;
6237 /* Specify the colors. */
6238 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
6239 if (pixel != -1)
6241 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
6242 ++ac;
6244 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
6245 if (pixel != -1)
6247 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
6248 ++ac;
6251 #endif
6253 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
6254 f->output_data.x->edit_widget, av, ac);
6257 char const *initial = "";
6258 char const *val = initial;
6259 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
6260 #ifdef XtNarrowScrollbars
6261 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
6262 #endif
6263 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
6264 if (xaw3d_arrow_scroll || val == initial)
6265 { /* ARROW_SCROLL */
6266 xaw3d_arrow_scroll = True;
6267 /* Isn't that just a personal preference ? --Stef */
6268 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
6272 /* Define callbacks. */
6273 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
6274 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
6275 (XtPointer) bar);
6277 /* Realize the widget. Only after that is the X window created. */
6278 XtRealizeWidget (widget);
6280 #endif /* !USE_MOTIF */
6282 /* Install an action hook that lets us detect when the user
6283 finishes interacting with a scroll bar. */
6284 if (horizontal_action_hook_id == 0)
6285 horizontal_action_hook_id
6286 = XtAppAddActionHook (Xt_app_con, xt_horizontal_action_hook, 0);
6288 /* Remember X window and widget in the scroll bar vector. */
6289 SET_SCROLL_BAR_X_WIDGET (bar, widget);
6290 xwindow = XtWindow (widget);
6291 bar->x_window = xwindow;
6292 bar->whole = 1;
6293 bar->horizontal = true;
6295 unblock_input ();
6297 #endif /* not USE_GTK */
6300 /* Set the thumb size and position of scroll bar BAR. We are currently
6301 displaying PORTION out of a whole WHOLE, and our position POSITION. */
6303 #ifdef USE_GTK
6304 static void
6305 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
6307 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
6310 static void
6311 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
6313 xg_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
6316 #else /* not USE_GTK */
6317 static void
6318 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
6319 int whole)
6321 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6322 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6323 float top, shown;
6325 block_input ();
6327 #ifdef USE_MOTIF
6329 if (scroll_bar_adjust_thumb_portion_p)
6331 /* We use an estimate of 30 chars per line rather than the real
6332 `portion' value. This has the disadvantage that the thumb size
6333 is not very representative, but it makes our life a lot easier.
6334 Otherwise, we have to constantly adjust the thumb size, which
6335 we can't always do quickly enough: while dragging, the size of
6336 the thumb might prevent the user from dragging the thumb all the
6337 way to the end. but Motif and some versions of Xaw3d don't allow
6338 updating the thumb size while dragging. Also, even if we can update
6339 its size, the update will often happen too late.
6340 If you don't believe it, check out revision 1.650 of xterm.c to see
6341 what hoops we were going through and the still poor behavior we got. */
6342 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
6343 /* When the thumb is at the bottom, position == whole.
6344 So we need to increase `whole' to make space for the thumb. */
6345 whole += portion;
6348 if (whole <= 0)
6349 top = 0, shown = 1;
6350 else
6352 top = (float) position / whole;
6353 shown = (float) portion / whole;
6356 if (bar->dragging == -1)
6358 int size, value;
6360 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
6361 is the scroll bar's maximum and MIN is the scroll bar's minimum
6362 value. */
6363 size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
6365 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
6366 value = top * XM_SB_MAX;
6367 value = min (value, XM_SB_MAX - size);
6369 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6371 #else /* !USE_MOTIF i.e. use Xaw */
6373 if (whole == 0)
6374 top = 0, shown = 1;
6375 else
6377 top = (float) position / whole;
6378 shown = (float) portion / whole;
6382 float old_top, old_shown;
6383 Dimension height;
6384 XtVaGetValues (widget,
6385 XtNtopOfThumb, &old_top,
6386 XtNshown, &old_shown,
6387 XtNheight, &height,
6388 NULL);
6390 /* Massage the top+shown values. */
6391 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
6392 top = max (0, min (1, top));
6393 else
6394 top = old_top;
6395 #if ! defined (HAVE_XAW3D)
6396 /* With Xaw, 'top' values too closer to 1.0 may
6397 cause the thumb to disappear. Fix that. */
6398 top = min (top, 0.99f);
6399 #endif
6400 /* Keep two pixels available for moving the thumb down. */
6401 shown = max (0, min (1 - top - (2.0f / height), shown));
6402 #if ! defined (HAVE_XAW3D)
6403 /* Likewise with too small 'shown'. */
6404 shown = max (shown, 0.01f);
6405 #endif
6407 /* If the call to XawScrollbarSetThumb below doesn't seem to
6408 work, check that 'NARROWPROTO' is defined in src/config.h.
6409 If this is not so, most likely you need to fix configure. */
6410 if (top != old_top || shown != old_shown)
6412 if (bar->dragging == -1)
6413 XawScrollbarSetThumb (widget, top, shown);
6414 else
6416 /* Try to make the scrolling a tad smoother. */
6417 if (!xaw3d_pick_top)
6418 shown = min (shown, old_shown);
6420 XawScrollbarSetThumb (widget, top, shown);
6424 #endif /* !USE_MOTIF */
6426 unblock_input ();
6429 static void
6430 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
6431 int whole)
6433 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6434 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6435 float top, shown;
6437 block_input ();
6439 #ifdef USE_MOTIF
6440 bar->whole = whole;
6441 shown = (float) portion / whole;
6442 top = (float) position / (whole - portion);
6444 int size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
6445 int value = clip_to_bounds (0, top * (XM_SB_MAX - size), XM_SB_MAX - size);
6447 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6449 #else /* !USE_MOTIF i.e. use Xaw */
6450 bar->whole = whole;
6451 if (whole == 0)
6452 top = 0, shown = 1;
6453 else
6455 top = (float) position / whole;
6456 shown = (float) portion / whole;
6460 float old_top, old_shown;
6461 Dimension height;
6462 XtVaGetValues (widget,
6463 XtNtopOfThumb, &old_top,
6464 XtNshown, &old_shown,
6465 XtNheight, &height,
6466 NULL);
6468 #if false
6469 /* Massage the top+shown values. */
6470 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
6471 top = max (0, min (1, top));
6472 else
6473 top = old_top;
6474 #if ! defined (HAVE_XAW3D)
6475 /* With Xaw, 'top' values too closer to 1.0 may
6476 cause the thumb to disappear. Fix that. */
6477 top = min (top, 0.99f);
6478 #endif
6479 /* Keep two pixels available for moving the thumb down. */
6480 shown = max (0, min (1 - top - (2.0f / height), shown));
6481 #if ! defined (HAVE_XAW3D)
6482 /* Likewise with too small 'shown'. */
6483 shown = max (shown, 0.01f);
6484 #endif
6485 #endif
6487 /* If the call to XawScrollbarSetThumb below doesn't seem to
6488 work, check that 'NARROWPROTO' is defined in src/config.h.
6489 If this is not so, most likely you need to fix configure. */
6490 XawScrollbarSetThumb (widget, top, shown);
6491 #if false
6492 if (top != old_top || shown != old_shown)
6494 if (bar->dragging == -1)
6495 XawScrollbarSetThumb (widget, top, shown);
6496 else
6498 /* Try to make the scrolling a tad smoother. */
6499 if (!xaw3d_pick_top)
6500 shown = min (shown, old_shown);
6502 XawScrollbarSetThumb (widget, top, shown);
6505 #endif
6507 #endif /* !USE_MOTIF */
6509 unblock_input ();
6511 #endif /* not USE_GTK */
6513 #endif /* USE_TOOLKIT_SCROLL_BARS */
6517 /************************************************************************
6518 Scroll bars, general
6519 ************************************************************************/
6521 /* Create a scroll bar and return the scroll bar vector for it. W is
6522 the Emacs window on which to create the scroll bar. TOP, LEFT,
6523 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
6524 scroll bar. */
6526 static struct scroll_bar *
6527 x_scroll_bar_create (struct window *w, int top, int left,
6528 int width, int height, bool horizontal)
6530 struct frame *f = XFRAME (w->frame);
6531 struct scroll_bar *bar
6532 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
6533 Lisp_Object barobj;
6535 block_input ();
6537 #ifdef USE_TOOLKIT_SCROLL_BARS
6538 if (horizontal)
6539 x_create_horizontal_toolkit_scroll_bar (f, bar);
6540 else
6541 x_create_toolkit_scroll_bar (f, bar);
6542 #else /* not USE_TOOLKIT_SCROLL_BARS */
6544 XSetWindowAttributes a;
6545 unsigned long mask;
6546 Window window;
6548 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
6549 if (a.background_pixel == -1)
6550 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
6552 a.event_mask = (ButtonPressMask | ButtonReleaseMask
6553 | ButtonMotionMask | PointerMotionHintMask
6554 | ExposureMask);
6555 a.cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
6557 mask = (CWBackPixel | CWEventMask | CWCursor);
6559 /* Clear the area of W that will serve as a scroll bar. This is
6560 for the case that a window has been split horizontally. In
6561 this case, no clear_frame is generated to reduce flickering. */
6562 if (width > 0 && window_box_height (w) > 0)
6563 x_clear_area (f, left, top, width, window_box_height (w));
6565 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6566 /* Position and size of scroll bar. */
6567 left, top, width, height,
6568 /* Border width, depth, class, and visual. */
6570 CopyFromParent,
6571 CopyFromParent,
6572 CopyFromParent,
6573 /* Attributes. */
6574 mask, &a);
6575 bar->x_window = window;
6577 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6579 XSETWINDOW (bar->window, w);
6580 bar->top = top;
6581 bar->left = left;
6582 bar->width = width;
6583 bar->height = height;
6584 bar->start = 0;
6585 bar->end = 0;
6586 bar->dragging = -1;
6587 bar->horizontal = horizontal;
6588 #if defined (USE_TOOLKIT_SCROLL_BARS) && defined (USE_LUCID)
6589 bar->last_seen_part = scroll_bar_nowhere;
6590 #endif
6592 /* Add bar to its frame's list of scroll bars. */
6593 bar->next = FRAME_SCROLL_BARS (f);
6594 bar->prev = Qnil;
6595 XSETVECTOR (barobj, bar);
6596 fset_scroll_bars (f, barobj);
6597 if (!NILP (bar->next))
6598 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6600 /* Map the window/widget. */
6601 #ifdef USE_TOOLKIT_SCROLL_BARS
6603 #ifdef USE_GTK
6604 if (horizontal)
6605 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top,
6606 left, width, max (height, 1));
6607 else
6608 xg_update_scrollbar_pos (f, bar->x_window, top,
6609 left, width, max (height, 1));
6610 #else /* not USE_GTK */
6611 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6612 XtConfigureWidget (scroll_bar, left, top, width, max (height, 1), 0);
6613 XtMapWidget (scroll_bar);
6614 /* Don't obscure any child frames. */
6615 XLowerWindow (FRAME_X_DISPLAY (f), bar->x_window);
6616 #endif /* not USE_GTK */
6618 #else /* not USE_TOOLKIT_SCROLL_BARS */
6619 XMapWindow (FRAME_X_DISPLAY (f), bar->x_window);
6620 /* Don't obscure any child frames. */
6621 XLowerWindow (FRAME_X_DISPLAY (f), bar->x_window);
6622 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6624 unblock_input ();
6625 return bar;
6629 #ifndef USE_TOOLKIT_SCROLL_BARS
6631 /* Draw BAR's handle in the proper position.
6633 If the handle is already drawn from START to END, don't bother
6634 redrawing it, unless REBUILD; in that case, always
6635 redraw it. (REBUILD is handy for drawing the handle after expose
6636 events.)
6638 Normally, we want to constrain the start and end of the handle to
6639 fit inside its rectangle, but if the user is dragging the scroll
6640 bar handle, we want to let them drag it down all the way, so that
6641 the bar's top is as far down as it goes; otherwise, there's no way
6642 to move to the very end of the buffer. */
6644 static void
6645 x_scroll_bar_set_handle (struct scroll_bar *bar, int start, int end,
6646 bool rebuild)
6648 bool dragging = bar->dragging != -1;
6649 Window w = bar->x_window;
6650 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6651 GC gc = f->output_data.x->normal_gc;
6653 /* If the display is already accurate, do nothing. */
6654 if (! rebuild
6655 && start == bar->start
6656 && end == bar->end)
6657 return;
6659 block_input ();
6662 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
6663 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
6664 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
6666 /* Make sure the values are reasonable, and try to preserve
6667 the distance between start and end. */
6669 int length = end - start;
6671 if (start < 0)
6672 start = 0;
6673 else if (start > top_range)
6674 start = top_range;
6675 end = start + length;
6677 if (end < start)
6678 end = start;
6679 else if (end > top_range && ! dragging)
6680 end = top_range;
6683 /* Store the adjusted setting in the scroll bar. */
6684 bar->start = start;
6685 bar->end = end;
6687 /* Clip the end position, just for display. */
6688 if (end > top_range)
6689 end = top_range;
6691 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
6692 below top positions, to make sure the handle is always at least
6693 that many pixels tall. */
6694 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
6696 /* Draw the empty space above the handle. Note that we can't clear
6697 zero-height areas; that means "clear to end of window." */
6698 if ((inside_width > 0) && (start > 0))
6699 x_clear_area1 (FRAME_X_DISPLAY (f), w,
6700 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6701 VERTICAL_SCROLL_BAR_TOP_BORDER,
6702 inside_width, start, False);
6704 /* Change to proper foreground color if one is specified. */
6705 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6706 XSetForeground (FRAME_X_DISPLAY (f), gc,
6707 f->output_data.x->scroll_bar_foreground_pixel);
6709 /* Draw the handle itself. */
6710 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
6711 /* x, y, width, height */
6712 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6713 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
6714 inside_width, end - start);
6716 /* Restore the foreground color of the GC if we changed it above. */
6717 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6718 XSetForeground (FRAME_X_DISPLAY (f), gc,
6719 FRAME_FOREGROUND_PIXEL (f));
6721 /* Draw the empty space below the handle. Note that we can't
6722 clear zero-height areas; that means "clear to end of window." */
6723 if ((inside_width > 0) && (end < inside_height))
6724 x_clear_area1 (FRAME_X_DISPLAY (f), w,
6725 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6726 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
6727 inside_width, inside_height - end, False);
6730 unblock_input ();
6733 #endif /* !USE_TOOLKIT_SCROLL_BARS */
6735 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
6736 nil. */
6738 static void
6739 x_scroll_bar_remove (struct scroll_bar *bar)
6741 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6742 block_input ();
6744 #ifdef USE_TOOLKIT_SCROLL_BARS
6745 #ifdef USE_GTK
6746 xg_remove_scroll_bar (f, bar->x_window);
6747 #else /* not USE_GTK */
6748 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
6749 #endif /* not USE_GTK */
6750 #else
6751 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
6752 #endif
6754 /* Dissociate this scroll bar from its window. */
6755 if (bar->horizontal)
6756 wset_horizontal_scroll_bar (XWINDOW (bar->window), Qnil);
6757 else
6758 wset_vertical_scroll_bar (XWINDOW (bar->window), Qnil);
6760 unblock_input ();
6764 /* Set the handle of the vertical scroll bar for WINDOW to indicate
6765 that we are displaying PORTION characters out of a total of WHOLE
6766 characters, starting at POSITION. If WINDOW has no scroll bar,
6767 create one. */
6769 static void
6770 XTset_vertical_scroll_bar (struct window *w, int portion, int whole, int position)
6772 struct frame *f = XFRAME (w->frame);
6773 Lisp_Object barobj;
6774 struct scroll_bar *bar;
6775 int top, height, left, width;
6776 int window_y, window_height;
6778 /* Get window dimensions. */
6779 window_box (w, ANY_AREA, 0, &window_y, 0, &window_height);
6780 top = window_y;
6781 height = window_height;
6782 left = WINDOW_SCROLL_BAR_AREA_X (w);
6783 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
6785 /* Does the scroll bar exist yet? */
6786 if (NILP (w->vertical_scroll_bar))
6788 if (width > 0 && height > 0)
6790 block_input ();
6791 x_clear_area (f, left, top, width, height);
6792 unblock_input ();
6795 bar = x_scroll_bar_create (w, top, left, width, max (height, 1), false);
6797 else
6799 /* It may just need to be moved and resized. */
6800 unsigned int mask = 0;
6802 bar = XSCROLL_BAR (w->vertical_scroll_bar);
6804 block_input ();
6806 if (left != bar->left)
6807 mask |= CWX;
6808 if (top != bar->top)
6809 mask |= CWY;
6810 if (width != bar->width)
6811 mask |= CWWidth;
6812 if (height != bar->height)
6813 mask |= CWHeight;
6815 #ifdef USE_TOOLKIT_SCROLL_BARS
6817 /* Move/size the scroll bar widget. */
6818 if (mask)
6820 /* Since toolkit scroll bars are smaller than the space reserved
6821 for them on the frame, we have to clear "under" them. */
6822 if (width > 0 && height > 0)
6823 x_clear_area (f, left, top, width, height);
6824 #ifdef USE_GTK
6825 xg_update_scrollbar_pos (f, bar->x_window, top,
6826 left, width, max (height, 1));
6827 #else /* not USE_GTK */
6828 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
6829 left, top, width, max (height, 1), 0);
6830 #endif /* not USE_GTK */
6832 #else /* not USE_TOOLKIT_SCROLL_BARS */
6834 /* Move/size the scroll bar window. */
6835 if (mask)
6837 XWindowChanges wc;
6839 wc.x = left;
6840 wc.y = top;
6841 wc.width = width;
6842 wc.height = height;
6843 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
6844 mask, &wc);
6847 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6849 /* Remember new settings. */
6850 bar->left = left;
6851 bar->top = top;
6852 bar->width = width;
6853 bar->height = height;
6855 unblock_input ();
6858 #ifdef USE_TOOLKIT_SCROLL_BARS
6859 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
6860 #else /* not USE_TOOLKIT_SCROLL_BARS */
6861 /* Set the scroll bar's current state, unless we're currently being
6862 dragged. */
6863 if (bar->dragging == -1)
6865 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
6867 if (whole == 0)
6868 x_scroll_bar_set_handle (bar, 0, top_range, false);
6869 else
6871 int start = ((double) position * top_range) / whole;
6872 int end = ((double) (position + portion) * top_range) / whole;
6873 x_scroll_bar_set_handle (bar, start, end, false);
6876 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6878 XSETVECTOR (barobj, bar);
6879 wset_vertical_scroll_bar (w, barobj);
6883 static void
6884 XTset_horizontal_scroll_bar (struct window *w, int portion, int whole, int position)
6886 struct frame *f = XFRAME (w->frame);
6887 Lisp_Object barobj;
6888 struct scroll_bar *bar;
6889 int top, height, left, width;
6890 int window_x, window_width;
6891 int pixel_width = WINDOW_PIXEL_WIDTH (w);
6893 /* Get window dimensions. */
6894 window_box (w, ANY_AREA, &window_x, 0, &window_width, 0);
6895 left = window_x;
6896 width = window_width;
6897 top = WINDOW_SCROLL_BAR_AREA_Y (w);
6898 height = WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
6900 /* Does the scroll bar exist yet? */
6901 if (NILP (w->horizontal_scroll_bar))
6903 if (width > 0 && height > 0)
6905 block_input ();
6907 /* Clear also part between window_width and
6908 WINDOW_PIXEL_WIDTH. */
6909 x_clear_area (f, left, top, pixel_width, height);
6910 unblock_input ();
6913 bar = x_scroll_bar_create (w, top, left, width, height, true);
6915 else
6917 /* It may just need to be moved and resized. */
6918 unsigned int mask = 0;
6920 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
6922 block_input ();
6924 if (left != bar->left)
6925 mask |= CWX;
6926 if (top != bar->top)
6927 mask |= CWY;
6928 if (width != bar->width)
6929 mask |= CWWidth;
6930 if (height != bar->height)
6931 mask |= CWHeight;
6933 #ifdef USE_TOOLKIT_SCROLL_BARS
6934 /* Move/size the scroll bar widget. */
6935 if (mask)
6937 /* Since toolkit scroll bars are smaller than the space reserved
6938 for them on the frame, we have to clear "under" them. */
6939 if (width > 0 && height > 0)
6940 x_clear_area (f,
6941 WINDOW_LEFT_EDGE_X (w), top,
6942 pixel_width - WINDOW_RIGHT_DIVIDER_WIDTH (w), height);
6943 #ifdef USE_GTK
6944 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top, left,
6945 width, height);
6946 #else /* not USE_GTK */
6947 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
6948 left, top, width, height, 0);
6949 #endif /* not USE_GTK */
6951 #else /* not USE_TOOLKIT_SCROLL_BARS */
6953 /* Clear areas not covered by the scroll bar because it's not as
6954 wide as the area reserved for it. This makes sure a
6955 previous mode line display is cleared after C-x 2 C-x 1, for
6956 example. */
6958 int area_height = WINDOW_CONFIG_SCROLL_BAR_HEIGHT (w);
6959 int rest = area_height - height;
6960 if (rest > 0 && width > 0)
6961 x_clear_area (f, left, top, width, rest);
6964 /* Move/size the scroll bar window. */
6965 if (mask)
6967 XWindowChanges wc;
6969 wc.x = left;
6970 wc.y = top;
6971 wc.width = width;
6972 wc.height = height;
6973 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
6974 mask, &wc);
6977 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6979 /* Remember new settings. */
6980 bar->left = left;
6981 bar->top = top;
6982 bar->width = width;
6983 bar->height = height;
6985 unblock_input ();
6988 #ifdef USE_TOOLKIT_SCROLL_BARS
6989 x_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
6990 #else /* not USE_TOOLKIT_SCROLL_BARS */
6991 /* Set the scroll bar's current state, unless we're currently being
6992 dragged. */
6993 if (bar->dragging == -1)
6995 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, width);
6997 if (whole == 0)
6998 x_scroll_bar_set_handle (bar, 0, left_range, false);
6999 else
7001 int start = ((double) position * left_range) / whole;
7002 int end = ((double) (position + portion) * left_range) / whole;
7003 x_scroll_bar_set_handle (bar, start, end, false);
7006 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7008 XSETVECTOR (barobj, bar);
7009 wset_horizontal_scroll_bar (w, barobj);
7013 /* The following three hooks are used when we're doing a thorough
7014 redisplay of the frame. We don't explicitly know which scroll bars
7015 are going to be deleted, because keeping track of when windows go
7016 away is a real pain - "Can you say set-window-configuration, boys
7017 and girls?" Instead, we just assert at the beginning of redisplay
7018 that *all* scroll bars are to be removed, and then save a scroll bar
7019 from the fiery pit when we actually redisplay its window. */
7021 /* Arrange for all scroll bars on FRAME to be removed at the next call
7022 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
7023 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
7025 static void
7026 XTcondemn_scroll_bars (struct frame *frame)
7028 if (!NILP (FRAME_SCROLL_BARS (frame)))
7030 if (!NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
7032 /* Prepend scrollbars to already condemned ones. */
7033 Lisp_Object last = FRAME_SCROLL_BARS (frame);
7035 while (!NILP (XSCROLL_BAR (last)->next))
7036 last = XSCROLL_BAR (last)->next;
7038 XSCROLL_BAR (last)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
7039 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = last;
7042 fset_condemned_scroll_bars (frame, FRAME_SCROLL_BARS (frame));
7043 fset_scroll_bars (frame, Qnil);
7048 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
7049 Note that WINDOW isn't necessarily condemned at all. */
7051 static void
7052 XTredeem_scroll_bar (struct window *w)
7054 struct scroll_bar *bar;
7055 Lisp_Object barobj;
7056 struct frame *f;
7058 /* We can't redeem this window's scroll bar if it doesn't have one. */
7059 if (NILP (w->vertical_scroll_bar) && NILP (w->horizontal_scroll_bar))
7060 emacs_abort ();
7062 if (!NILP (w->vertical_scroll_bar) && WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
7064 bar = XSCROLL_BAR (w->vertical_scroll_bar);
7065 /* Unlink it from the condemned list. */
7066 f = XFRAME (WINDOW_FRAME (w));
7067 if (NILP (bar->prev))
7069 /* If the prev pointer is nil, it must be the first in one of
7070 the lists. */
7071 if (EQ (FRAME_SCROLL_BARS (f), w->vertical_scroll_bar))
7072 /* It's not condemned. Everything's fine. */
7073 goto horizontal;
7074 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
7075 w->vertical_scroll_bar))
7076 fset_condemned_scroll_bars (f, bar->next);
7077 else
7078 /* If its prev pointer is nil, it must be at the front of
7079 one or the other! */
7080 emacs_abort ();
7082 else
7083 XSCROLL_BAR (bar->prev)->next = bar->next;
7085 if (! NILP (bar->next))
7086 XSCROLL_BAR (bar->next)->prev = bar->prev;
7088 bar->next = FRAME_SCROLL_BARS (f);
7089 bar->prev = Qnil;
7090 XSETVECTOR (barobj, bar);
7091 fset_scroll_bars (f, barobj);
7092 if (! NILP (bar->next))
7093 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
7096 horizontal:
7097 if (!NILP (w->horizontal_scroll_bar) && WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
7099 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
7100 /* Unlink it from the condemned list. */
7101 f = XFRAME (WINDOW_FRAME (w));
7102 if (NILP (bar->prev))
7104 /* If the prev pointer is nil, it must be the first in one of
7105 the lists. */
7106 if (EQ (FRAME_SCROLL_BARS (f), w->horizontal_scroll_bar))
7107 /* It's not condemned. Everything's fine. */
7108 return;
7109 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
7110 w->horizontal_scroll_bar))
7111 fset_condemned_scroll_bars (f, bar->next);
7112 else
7113 /* If its prev pointer is nil, it must be at the front of
7114 one or the other! */
7115 emacs_abort ();
7117 else
7118 XSCROLL_BAR (bar->prev)->next = bar->next;
7120 if (! NILP (bar->next))
7121 XSCROLL_BAR (bar->next)->prev = bar->prev;
7123 bar->next = FRAME_SCROLL_BARS (f);
7124 bar->prev = Qnil;
7125 XSETVECTOR (barobj, bar);
7126 fset_scroll_bars (f, barobj);
7127 if (! NILP (bar->next))
7128 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
7132 /* Remove all scroll bars on FRAME that haven't been saved since the
7133 last call to `*condemn_scroll_bars_hook'. */
7135 static void
7136 XTjudge_scroll_bars (struct frame *f)
7138 Lisp_Object bar, next;
7140 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
7142 /* Clear out the condemned list now so we won't try to process any
7143 more events on the hapless scroll bars. */
7144 fset_condemned_scroll_bars (f, Qnil);
7146 for (; ! NILP (bar); bar = next)
7148 struct scroll_bar *b = XSCROLL_BAR (bar);
7150 x_scroll_bar_remove (b);
7152 next = b->next;
7153 b->next = b->prev = Qnil;
7156 /* Now there should be no references to the condemned scroll bars,
7157 and they should get garbage-collected. */
7161 #ifndef USE_TOOLKIT_SCROLL_BARS
7162 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
7163 is a no-op when using toolkit scroll bars.
7165 This may be called from a signal handler, so we have to ignore GC
7166 mark bits. */
7168 static void
7169 x_scroll_bar_expose (struct scroll_bar *bar, const XEvent *event)
7171 Window w = bar->x_window;
7172 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7173 GC gc = f->output_data.x->normal_gc;
7175 block_input ();
7177 x_scroll_bar_set_handle (bar, bar->start, bar->end, true);
7179 /* Switch to scroll bar foreground color. */
7180 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
7181 XSetForeground (FRAME_X_DISPLAY (f), gc,
7182 f->output_data.x->scroll_bar_foreground_pixel);
7184 /* Draw a one-pixel border just inside the edges of the scroll bar. */
7185 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
7186 /* x, y, width, height */
7187 0, 0, bar->width - 1, bar->height - 1);
7189 /* Restore the foreground color of the GC if we changed it above. */
7190 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
7191 XSetForeground (FRAME_X_DISPLAY (f), gc,
7192 FRAME_FOREGROUND_PIXEL (f));
7194 unblock_input ();
7197 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7199 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
7200 is set to something other than NO_EVENT, it is enqueued.
7202 This may be called from a signal handler, so we have to ignore GC
7203 mark bits. */
7206 static void
7207 x_scroll_bar_handle_click (struct scroll_bar *bar,
7208 const XEvent *event,
7209 struct input_event *emacs_event)
7211 if (! WINDOWP (bar->window))
7212 emacs_abort ();
7214 emacs_event->kind = (bar->horizontal
7215 ? HORIZONTAL_SCROLL_BAR_CLICK_EVENT
7216 : SCROLL_BAR_CLICK_EVENT);
7217 emacs_event->code = event->xbutton.button - Button1;
7218 emacs_event->modifiers
7219 = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO
7220 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
7221 event->xbutton.state)
7222 | (event->type == ButtonRelease
7223 ? up_modifier
7224 : down_modifier));
7225 emacs_event->frame_or_window = bar->window;
7226 emacs_event->arg = Qnil;
7227 emacs_event->timestamp = event->xbutton.time;
7228 if (bar->horizontal)
7230 int left_range
7231 = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
7232 int x = event->xbutton.x - HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
7234 if (x < 0) x = 0;
7235 if (x > left_range) x = left_range;
7237 if (x < bar->start)
7238 emacs_event->part = scroll_bar_before_handle;
7239 else if (x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
7240 emacs_event->part = scroll_bar_horizontal_handle;
7241 else
7242 emacs_event->part = scroll_bar_after_handle;
7244 #ifndef USE_TOOLKIT_SCROLL_BARS
7245 /* If the user has released the handle, set it to its final position. */
7246 if (event->type == ButtonRelease && bar->dragging != -1)
7248 int new_start = - bar->dragging;
7249 int new_end = new_start + bar->end - bar->start;
7251 x_scroll_bar_set_handle (bar, new_start, new_end, false);
7252 bar->dragging = -1;
7254 #endif
7256 XSETINT (emacs_event->x, left_range);
7257 XSETINT (emacs_event->y, x);
7259 else
7261 int top_range
7262 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
7263 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
7265 if (y < 0) y = 0;
7266 if (y > top_range) y = top_range;
7268 if (y < bar->start)
7269 emacs_event->part = scroll_bar_above_handle;
7270 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
7271 emacs_event->part = scroll_bar_handle;
7272 else
7273 emacs_event->part = scroll_bar_below_handle;
7275 #ifndef USE_TOOLKIT_SCROLL_BARS
7276 /* If the user has released the handle, set it to its final position. */
7277 if (event->type == ButtonRelease && bar->dragging != -1)
7279 int new_start = y - bar->dragging;
7280 int new_end = new_start + bar->end - bar->start;
7282 x_scroll_bar_set_handle (bar, new_start, new_end, false);
7283 bar->dragging = -1;
7285 #endif
7287 XSETINT (emacs_event->x, y);
7288 XSETINT (emacs_event->y, top_range);
7292 #ifndef USE_TOOLKIT_SCROLL_BARS
7294 /* Handle some mouse motion while someone is dragging the scroll bar.
7296 This may be called from a signal handler, so we have to ignore GC
7297 mark bits. */
7299 static void
7300 x_scroll_bar_note_movement (struct scroll_bar *bar,
7301 const XMotionEvent *event)
7303 struct frame *f = XFRAME (XWINDOW (bar->window)->frame);
7304 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
7306 dpyinfo->last_mouse_movement_time = event->time;
7307 dpyinfo->last_mouse_scroll_bar = bar;
7308 f->mouse_moved = true;
7310 /* If we're dragging the bar, display it. */
7311 if (bar->dragging != -1)
7313 /* Where should the handle be now? */
7314 int new_start = event->y - bar->dragging;
7316 if (new_start != bar->start)
7318 int new_end = new_start + bar->end - bar->start;
7320 x_scroll_bar_set_handle (bar, new_start, new_end, false);
7325 #endif /* !USE_TOOLKIT_SCROLL_BARS */
7327 /* Return information to the user about the current position of the mouse
7328 on the scroll bar. */
7330 static void
7331 x_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
7332 enum scroll_bar_part *part, Lisp_Object *x,
7333 Lisp_Object *y, Time *timestamp)
7335 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
7336 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
7337 Window w = bar->x_window;
7338 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7339 int win_x, win_y;
7340 Window dummy_window;
7341 int dummy_coord;
7342 unsigned int dummy_mask;
7344 block_input ();
7346 /* Get the mouse's position relative to the scroll bar window, and
7347 report that. */
7348 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
7350 /* Root, child, root x and root y. */
7351 &dummy_window, &dummy_window,
7352 &dummy_coord, &dummy_coord,
7354 /* Position relative to scroll bar. */
7355 &win_x, &win_y,
7357 /* Mouse buttons and modifier keys. */
7358 &dummy_mask))
7360 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
7362 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
7364 if (bar->dragging != -1)
7365 win_y -= bar->dragging;
7367 if (win_y < 0)
7368 win_y = 0;
7369 if (win_y > top_range)
7370 win_y = top_range;
7372 *fp = f;
7373 *bar_window = bar->window;
7375 if (bar->dragging != -1)
7376 *part = scroll_bar_handle;
7377 else if (win_y < bar->start)
7378 *part = scroll_bar_above_handle;
7379 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
7380 *part = scroll_bar_handle;
7381 else
7382 *part = scroll_bar_below_handle;
7384 XSETINT (*x, win_y);
7385 XSETINT (*y, top_range);
7387 f->mouse_moved = false;
7388 dpyinfo->last_mouse_scroll_bar = NULL;
7389 *timestamp = dpyinfo->last_mouse_movement_time;
7392 unblock_input ();
7396 /* Return information to the user about the current position of the mouse
7397 on the scroll bar. */
7399 static void
7400 x_horizontal_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
7401 enum scroll_bar_part *part, Lisp_Object *x,
7402 Lisp_Object *y, Time *timestamp)
7404 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
7405 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
7406 Window w = bar->x_window;
7407 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7408 int win_x, win_y;
7409 Window dummy_window;
7410 int dummy_coord;
7411 unsigned int dummy_mask;
7413 block_input ();
7415 /* Get the mouse's position relative to the scroll bar window, and
7416 report that. */
7417 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
7419 /* Root, child, root x and root y. */
7420 &dummy_window, &dummy_window,
7421 &dummy_coord, &dummy_coord,
7423 /* Position relative to scroll bar. */
7424 &win_x, &win_y,
7426 /* Mouse buttons and modifier keys. */
7427 &dummy_mask))
7429 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
7431 win_x -= HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
7433 if (bar->dragging != -1)
7434 win_x -= bar->dragging;
7436 if (win_x < 0)
7437 win_x = 0;
7438 if (win_x > left_range)
7439 win_x = left_range;
7441 *fp = f;
7442 *bar_window = bar->window;
7444 if (bar->dragging != -1)
7445 *part = scroll_bar_horizontal_handle;
7446 else if (win_x < bar->start)
7447 *part = scroll_bar_before_handle;
7448 else if (win_x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
7449 *part = scroll_bar_handle;
7450 else
7451 *part = scroll_bar_after_handle;
7453 XSETINT (*y, win_x);
7454 XSETINT (*x, left_range);
7456 f->mouse_moved = false;
7457 dpyinfo->last_mouse_scroll_bar = NULL;
7458 *timestamp = dpyinfo->last_mouse_movement_time;
7461 unblock_input ();
7465 /* The screen has been cleared so we may have changed foreground or
7466 background colors, and the scroll bars may need to be redrawn.
7467 Clear out the scroll bars, and ask for expose events, so we can
7468 redraw them. */
7470 static void
7471 x_scroll_bar_clear (struct frame *f)
7473 #ifndef USE_TOOLKIT_SCROLL_BARS
7474 Lisp_Object bar;
7476 /* We can have scroll bars even if this is 0,
7477 if we just turned off scroll bar mode.
7478 But in that case we should not clear them. */
7479 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
7480 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
7481 bar = XSCROLL_BAR (bar)->next)
7482 XClearArea (FRAME_X_DISPLAY (f),
7483 XSCROLL_BAR (bar)->x_window,
7484 0, 0, 0, 0, True);
7485 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7488 #ifdef ENABLE_CHECKING
7490 /* Record the last 100 characters stored
7491 to help debug the loss-of-chars-during-GC problem. */
7493 static int temp_index;
7494 static short temp_buffer[100];
7496 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
7497 if (temp_index == ARRAYELTS (temp_buffer)) \
7498 temp_index = 0; \
7499 temp_buffer[temp_index++] = (keysym)
7501 #else /* not ENABLE_CHECKING */
7503 #define STORE_KEYSYM_FOR_DEBUG(keysym) ((void)0)
7505 #endif /* ENABLE_CHECKING */
7507 /* Set this to nonzero to fake an "X I/O error"
7508 on a particular display. */
7510 static struct x_display_info *XTread_socket_fake_io_error;
7512 /* When we find no input here, we occasionally do a no-op command
7513 to verify that the X server is still running and we can still talk with it.
7514 We try all the open displays, one by one.
7515 This variable is used for cycling thru the displays. */
7517 static struct x_display_info *next_noop_dpyinfo;
7519 enum
7521 X_EVENT_NORMAL,
7522 X_EVENT_GOTO_OUT,
7523 X_EVENT_DROP
7526 /* Filter events for the current X input method.
7527 DPYINFO is the display this event is for.
7528 EVENT is the X event to filter.
7530 Returns non-zero if the event was filtered, caller shall not process
7531 this event further.
7532 Returns zero if event is wasn't filtered. */
7534 #ifdef HAVE_X_I18N
7535 static int
7536 x_filter_event (struct x_display_info *dpyinfo, XEvent *event)
7538 /* XFilterEvent returns non-zero if the input method has
7539 consumed the event. We pass the frame's X window to
7540 XFilterEvent because that's the one for which the IC
7541 was created. */
7543 struct frame *f1 = x_any_window_to_frame (dpyinfo,
7544 event->xclient.window);
7546 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
7548 #endif
7550 #ifdef USE_GTK
7551 static int current_count;
7552 static int current_finish;
7553 static struct input_event *current_hold_quit;
7555 /* This is the filter function invoked by the GTK event loop.
7556 It is invoked before the XEvent is translated to a GdkEvent,
7557 so we have a chance to act on the event before GTK. */
7558 static GdkFilterReturn
7559 event_handler_gdk (GdkXEvent *gxev, GdkEvent *ev, gpointer data)
7561 XEvent *xev = (XEvent *) gxev;
7563 block_input ();
7564 if (current_count >= 0)
7566 struct x_display_info *dpyinfo;
7568 dpyinfo = x_display_info_for_display (xev->xany.display);
7570 #ifdef HAVE_X_I18N
7571 /* Filter events for the current X input method.
7572 GTK calls XFilterEvent but not for key press and release,
7573 so we do it here. */
7574 if ((xev->type == KeyPress || xev->type == KeyRelease)
7575 && dpyinfo
7576 && x_filter_event (dpyinfo, xev))
7578 unblock_input ();
7579 return GDK_FILTER_REMOVE;
7581 #endif
7583 if (! dpyinfo)
7584 current_finish = X_EVENT_NORMAL;
7585 else
7586 current_count
7587 += handle_one_xevent (dpyinfo, xev, &current_finish,
7588 current_hold_quit);
7590 else
7591 current_finish = x_dispatch_event (xev, xev->xany.display);
7593 unblock_input ();
7595 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
7596 return GDK_FILTER_REMOVE;
7598 return GDK_FILTER_CONTINUE;
7600 #endif /* USE_GTK */
7603 static void xembed_send_message (struct frame *f, Time,
7604 enum xembed_message,
7605 long detail, long data1, long data2);
7607 static void
7608 x_net_wm_state (struct frame *f, Window window)
7610 int value = FULLSCREEN_NONE;
7611 Lisp_Object lval = Qnil;
7612 bool sticky = false;
7614 get_current_wm_state (f, window, &value, &sticky);
7616 switch (value)
7618 case FULLSCREEN_WIDTH:
7619 lval = Qfullwidth;
7620 break;
7621 case FULLSCREEN_HEIGHT:
7622 lval = Qfullheight;
7623 break;
7624 case FULLSCREEN_BOTH:
7625 lval = Qfullboth;
7626 break;
7627 case FULLSCREEN_MAXIMIZED:
7628 lval = Qmaximized;
7629 break;
7632 frame_size_history_add
7633 (f, Qx_net_wm_state, 0, 0,
7634 list2 (get_frame_param (f, Qfullscreen), lval));
7636 store_frame_param (f, Qfullscreen, lval);
7637 /** store_frame_param (f, Qsticky, sticky ? Qt : Qnil); **/
7640 /* Flip back buffers on any frames with undrawn content. */
7641 static void
7642 flush_dirty_back_buffers (void)
7644 block_input ();
7645 Lisp_Object tail, frame;
7646 FOR_EACH_FRAME (tail, frame)
7648 struct frame *f = XFRAME (frame);
7649 if (FRAME_LIVE_P (f) &&
7650 FRAME_X_P (f) &&
7651 FRAME_X_WINDOW (f) &&
7652 !FRAME_GARBAGED_P (f) &&
7653 !buffer_flipping_blocked_p () &&
7654 FRAME_X_NEED_BUFFER_FLIP (f))
7655 show_back_buffer (f);
7657 unblock_input ();
7660 /* Handles the XEvent EVENT on display DPYINFO.
7662 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
7663 *FINISH is zero if caller should continue reading events.
7664 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
7665 *EVENT is unchanged unless we're processing KeyPress event.
7667 We return the number of characters stored into the buffer. */
7669 static int
7670 handle_one_xevent (struct x_display_info *dpyinfo,
7671 const XEvent *event,
7672 int *finish, struct input_event *hold_quit)
7674 union buffered_input_event inev;
7675 int count = 0;
7676 int do_help = 0;
7677 ptrdiff_t nbytes = 0;
7678 struct frame *any, *f = NULL;
7679 struct coding_system coding;
7680 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
7681 /* This holds the state XLookupString needs to implement dead keys
7682 and other tricks known as "compose processing". _X Window System_
7683 says that a portable program can't use this, but Stephen Gildea assures
7684 me that letting the compiler initialize it to zeros will work okay. */
7685 static XComposeStatus compose_status;
7686 XEvent configureEvent;
7687 XEvent next_event;
7689 USE_SAFE_ALLOCA;
7691 *finish = X_EVENT_NORMAL;
7693 EVENT_INIT (inev.ie);
7694 inev.ie.kind = NO_EVENT;
7695 inev.ie.arg = Qnil;
7697 any = x_any_window_to_frame (dpyinfo, event->xany.window);
7699 if (any && any->wait_event_type == event->type)
7700 any->wait_event_type = 0; /* Indicates we got it. */
7702 switch (event->type)
7704 case ClientMessage:
7706 if (event->xclient.message_type == dpyinfo->Xatom_wm_protocols
7707 && event->xclient.format == 32)
7709 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_take_focus)
7711 /* Use the value returned by x_any_window_to_frame
7712 because this could be the shell widget window
7713 if the frame has no title bar. */
7714 f = any;
7715 #ifdef HAVE_X_I18N
7716 /* Not quite sure this is needed -pd */
7717 if (f && FRAME_XIC (f))
7718 XSetICFocus (FRAME_XIC (f));
7719 #endif
7720 #if false
7721 /* Emacs sets WM hints whose `input' field is `true'. This
7722 instructs the WM to set the input focus automatically for
7723 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
7724 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
7725 it has set the focus. So, XSetInputFocus below is not
7726 needed.
7728 The call to XSetInputFocus below has also caused trouble. In
7729 cases where the XSetInputFocus done by the WM and the one
7730 below are temporally close (on a fast machine), the call
7731 below can generate additional FocusIn events which confuse
7732 Emacs. */
7734 /* Since we set WM_TAKE_FOCUS, we must call
7735 XSetInputFocus explicitly. But not if f is null,
7736 since that might be an event for a deleted frame. */
7737 if (f)
7739 Display *d = event->xclient.display;
7740 /* Catch and ignore errors, in case window has been
7741 iconified by a window manager such as GWM. */
7742 x_catch_errors (d);
7743 XSetInputFocus (d, event->xclient.window,
7744 /* The ICCCM says this is
7745 the only valid choice. */
7746 RevertToParent,
7747 event->xclient.data.l[1]);
7748 x_uncatch_errors ();
7750 /* Not certain about handling scroll bars here */
7751 #endif
7752 goto done;
7755 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_save_yourself)
7757 /* Save state modify the WM_COMMAND property to
7758 something which can reinstate us. This notifies
7759 the session manager, who's looking for such a
7760 PropertyNotify. Can restart processing when
7761 a keyboard or mouse event arrives. */
7762 /* If we have a session manager, don't set this.
7763 KDE will then start two Emacsen, one for the
7764 session manager and one for this. */
7765 #ifdef HAVE_X_SM
7766 if (! x_session_have_connection ())
7767 #endif
7769 f = x_top_window_to_frame (dpyinfo,
7770 event->xclient.window);
7771 /* This is just so we only give real data once
7772 for a single Emacs process. */
7773 if (f == SELECTED_FRAME ())
7774 XSetCommand (FRAME_X_DISPLAY (f),
7775 event->xclient.window,
7776 initial_argv, initial_argc);
7777 else if (f)
7778 XSetCommand (FRAME_X_DISPLAY (f),
7779 event->xclient.window,
7780 0, 0);
7782 goto done;
7785 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_delete_window)
7787 f = any;
7788 if (!f)
7789 goto OTHER; /* May be a dialog that is to be removed */
7791 inev.ie.kind = DELETE_WINDOW_EVENT;
7792 XSETFRAME (inev.ie.frame_or_window, f);
7793 goto done;
7796 goto done;
7799 if (event->xclient.message_type == dpyinfo->Xatom_wm_configure_denied)
7800 goto done;
7802 if (event->xclient.message_type == dpyinfo->Xatom_wm_window_moved)
7804 int new_x, new_y;
7805 f = x_window_to_frame (dpyinfo, event->xclient.window);
7807 new_x = event->xclient.data.s[0];
7808 new_y = event->xclient.data.s[1];
7810 if (f)
7812 f->left_pos = new_x;
7813 f->top_pos = new_y;
7815 goto done;
7818 #ifdef X_TOOLKIT_EDITRES
7819 if (event->xclient.message_type == dpyinfo->Xatom_editres)
7821 f = any;
7822 if (f)
7823 _XEditResCheckMessages (f->output_data.x->widget,
7824 NULL, (XEvent *) event, NULL);
7825 goto done;
7827 #endif /* X_TOOLKIT_EDITRES */
7829 if (event->xclient.message_type == dpyinfo->Xatom_DONE
7830 || event->xclient.message_type == dpyinfo->Xatom_PAGE)
7832 /* Ghostview job completed. Kill it. We could
7833 reply with "Next" if we received "Page", but we
7834 currently never do because we are interested in
7835 images, only, which should have 1 page. */
7836 Pixmap pixmap = (Pixmap) event->xclient.data.l[1];
7837 f = x_window_to_frame (dpyinfo, event->xclient.window);
7838 if (!f)
7839 goto OTHER;
7840 x_kill_gs_process (pixmap, f);
7841 expose_frame (f, 0, 0, 0, 0);
7842 goto done;
7845 #ifdef USE_TOOLKIT_SCROLL_BARS
7846 /* Scroll bar callbacks send a ClientMessage from which
7847 we construct an input_event. */
7848 if (event->xclient.message_type == dpyinfo->Xatom_Scrollbar)
7850 x_scroll_bar_to_input_event (event, &inev.ie);
7851 *finish = X_EVENT_GOTO_OUT;
7852 goto done;
7854 else if (event->xclient.message_type == dpyinfo->Xatom_Horizontal_Scrollbar)
7856 x_horizontal_scroll_bar_to_input_event (event, &inev.ie);
7857 *finish = X_EVENT_GOTO_OUT;
7858 goto done;
7860 #endif /* USE_TOOLKIT_SCROLL_BARS */
7862 /* XEmbed messages from the embedder (if any). */
7863 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
7865 enum xembed_message msg = event->xclient.data.l[1];
7866 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
7867 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7869 *finish = X_EVENT_GOTO_OUT;
7870 goto done;
7873 xft_settings_event (dpyinfo, event);
7875 f = any;
7876 if (!f)
7877 goto OTHER;
7878 if (x_handle_dnd_message (f, &event->xclient, dpyinfo, &inev.ie))
7879 *finish = X_EVENT_DROP;
7881 break;
7883 case SelectionNotify:
7884 x_display_set_last_user_time (dpyinfo, event->xselection.time);
7885 #ifdef USE_X_TOOLKIT
7886 if (! x_window_to_frame (dpyinfo, event->xselection.requestor))
7887 goto OTHER;
7888 #endif /* not USE_X_TOOLKIT */
7889 x_handle_selection_notify (&event->xselection);
7890 break;
7892 case SelectionClear: /* Someone has grabbed ownership. */
7893 x_display_set_last_user_time (dpyinfo, event->xselectionclear.time);
7894 #ifdef USE_X_TOOLKIT
7895 if (! x_window_to_frame (dpyinfo, event->xselectionclear.window))
7896 goto OTHER;
7897 #endif /* USE_X_TOOLKIT */
7899 const XSelectionClearEvent *eventp = &event->xselectionclear;
7901 inev.sie.kind = SELECTION_CLEAR_EVENT;
7902 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
7903 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
7904 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
7906 break;
7908 case SelectionRequest: /* Someone wants our selection. */
7909 x_display_set_last_user_time (dpyinfo, event->xselectionrequest.time);
7910 #ifdef USE_X_TOOLKIT
7911 if (!x_window_to_frame (dpyinfo, event->xselectionrequest.owner))
7912 goto OTHER;
7913 #endif /* USE_X_TOOLKIT */
7915 const XSelectionRequestEvent *eventp = &event->xselectionrequest;
7917 inev.sie.kind = SELECTION_REQUEST_EVENT;
7918 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
7919 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
7920 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
7921 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
7922 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
7923 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
7925 break;
7927 case PropertyNotify:
7928 x_display_set_last_user_time (dpyinfo, event->xproperty.time);
7929 f = x_top_window_to_frame (dpyinfo, event->xproperty.window);
7930 if (f && event->xproperty.atom == dpyinfo->Xatom_net_wm_state)
7932 bool not_hidden = x_handle_net_wm_state (f, &event->xproperty);
7933 if (not_hidden && FRAME_ICONIFIED_P (f))
7935 /* Gnome shell does not iconify us when C-z is pressed.
7936 It hides the frame. So if our state says we aren't
7937 hidden anymore, treat it as deiconified. */
7938 SET_FRAME_VISIBLE (f, 1);
7939 SET_FRAME_ICONIFIED (f, false);
7940 f->output_data.x->has_been_visible = true;
7941 inev.ie.kind = DEICONIFY_EVENT;
7942 XSETFRAME (inev.ie.frame_or_window, f);
7944 else if (! not_hidden && ! FRAME_ICONIFIED_P (f))
7946 SET_FRAME_VISIBLE (f, 0);
7947 SET_FRAME_ICONIFIED (f, true);
7948 inev.ie.kind = ICONIFY_EVENT;
7949 XSETFRAME (inev.ie.frame_or_window, f);
7953 x_handle_property_notify (&event->xproperty);
7954 xft_settings_event (dpyinfo, event);
7955 goto OTHER;
7957 case ReparentNotify:
7958 f = x_top_window_to_frame (dpyinfo, event->xreparent.window);
7959 if (f)
7961 /* Maybe we shouldn't set this for child frames ?? */
7962 f->output_data.x->parent_desc = event->xreparent.parent;
7963 if (!FRAME_PARENT_FRAME (f))
7964 x_real_positions (f, &f->left_pos, &f->top_pos);
7965 else
7967 Window root;
7968 unsigned int dummy_uint;
7970 block_input ();
7971 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
7972 &root, &f->left_pos, &f->top_pos,
7973 &dummy_uint, &dummy_uint, &dummy_uint, &dummy_uint);
7974 unblock_input ();
7977 /* Perhaps reparented due to a WM restart. Reset this. */
7978 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
7979 FRAME_DISPLAY_INFO (f)->net_supported_window = 0;
7981 x_set_frame_alpha (f);
7983 goto OTHER;
7985 case Expose:
7986 f = x_window_to_frame (dpyinfo, event->xexpose.window);
7987 if (f)
7989 if (!FRAME_VISIBLE_P (f))
7991 block_input ();
7992 SET_FRAME_VISIBLE (f, 1);
7993 SET_FRAME_ICONIFIED (f, false);
7994 if (FRAME_X_DOUBLE_BUFFERED_P (f))
7995 font_drop_xrender_surfaces (f);
7996 f->output_data.x->has_been_visible = true;
7997 SET_FRAME_GARBAGED (f);
7998 unblock_input ();
8000 else if (FRAME_GARBAGED_P (f))
8002 #ifdef USE_GTK
8003 /* Go around the back buffer and manually clear the
8004 window the first time we show it. This way, we avoid
8005 showing users the sanity-defying horror of whatever
8006 GtkWindow is rendering beneath us. We've garbaged
8007 the frame, so we'll redraw the whole thing on next
8008 redisplay anyway. Yuck. */
8009 x_clear_area1 (
8010 FRAME_X_DISPLAY (f),
8011 FRAME_X_WINDOW (f),
8012 event->xexpose.x, event->xexpose.y,
8013 event->xexpose.width, event->xexpose.height,
8015 x_clear_under_internal_border (f);
8016 #endif
8020 if (!FRAME_GARBAGED_P (f))
8022 #ifdef USE_GTK
8023 /* This seems to be needed for GTK 2.6 and later, see
8024 https://debbugs.gnu.org/cgi/bugreport.cgi?bug=15398. */
8025 x_clear_area (f,
8026 event->xexpose.x, event->xexpose.y,
8027 event->xexpose.width, event->xexpose.height);
8028 #endif
8029 expose_frame (f, event->xexpose.x, event->xexpose.y,
8030 event->xexpose.width, event->xexpose.height);
8031 #ifdef USE_GTK
8032 x_clear_under_internal_border (f);
8033 #endif
8036 if (!FRAME_GARBAGED_P (f))
8037 show_back_buffer (f);
8039 else
8041 #ifndef USE_TOOLKIT_SCROLL_BARS
8042 struct scroll_bar *bar;
8043 #endif
8044 #if defined USE_LUCID
8045 /* Submenus of the Lucid menu bar aren't widgets
8046 themselves, so there's no way to dispatch events
8047 to them. Recognize this case separately. */
8049 Widget widget = x_window_to_menu_bar (event->xexpose.window);
8050 if (widget)
8051 xlwmenu_redisplay (widget);
8053 #endif /* USE_LUCID */
8055 #ifdef USE_TOOLKIT_SCROLL_BARS
8056 /* Dispatch event to the widget. */
8057 goto OTHER;
8058 #else /* not USE_TOOLKIT_SCROLL_BARS */
8059 bar = x_window_to_scroll_bar (event->xexpose.display,
8060 event->xexpose.window, 2);
8062 if (bar)
8063 x_scroll_bar_expose (bar, event);
8064 #ifdef USE_X_TOOLKIT
8065 else
8066 goto OTHER;
8067 #endif /* USE_X_TOOLKIT */
8068 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8070 break;
8072 case GraphicsExpose: /* This occurs when an XCopyArea's
8073 source area was obscured or not
8074 available. */
8075 f = x_window_to_frame (dpyinfo, event->xgraphicsexpose.drawable);
8076 if (f)
8078 expose_frame (f, event->xgraphicsexpose.x,
8079 event->xgraphicsexpose.y,
8080 event->xgraphicsexpose.width,
8081 event->xgraphicsexpose.height);
8082 #ifdef USE_GTK
8083 x_clear_under_internal_border (f);
8084 #endif
8085 show_back_buffer (f);
8087 #ifdef USE_X_TOOLKIT
8088 else
8089 goto OTHER;
8090 #endif /* USE_X_TOOLKIT */
8091 break;
8093 case NoExpose: /* This occurs when an XCopyArea's
8094 source area was completely
8095 available. */
8096 break;
8098 case UnmapNotify:
8099 /* Redo the mouse-highlight after the tooltip has gone. */
8100 if (event->xunmap.window == tip_window)
8102 tip_window = 0;
8103 x_redo_mouse_highlight (dpyinfo);
8106 f = x_top_window_to_frame (dpyinfo, event->xunmap.window);
8107 if (f) /* F may no longer exist if
8108 the frame was deleted. */
8110 bool visible = FRAME_VISIBLE_P (f);
8111 /* While a frame is unmapped, display generation is
8112 disabled; you don't want to spend time updating a
8113 display that won't ever be seen. */
8114 SET_FRAME_VISIBLE (f, 0);
8115 /* We can't distinguish, from the event, whether the window
8116 has become iconified or invisible. So assume, if it
8117 was previously visible, than now it is iconified.
8118 But x_make_frame_invisible clears both
8119 the visible flag and the iconified flag;
8120 and that way, we know the window is not iconified now. */
8121 if (visible || FRAME_ICONIFIED_P (f))
8123 SET_FRAME_ICONIFIED (f, true);
8124 inev.ie.kind = ICONIFY_EVENT;
8125 XSETFRAME (inev.ie.frame_or_window, f);
8128 goto OTHER;
8130 case MapNotify:
8131 /* We use x_top_window_to_frame because map events can
8132 come for sub-windows and they don't mean that the
8133 frame is visible. */
8134 f = x_top_window_to_frame (dpyinfo, event->xmap.window);
8135 if (f)
8137 bool iconified = FRAME_ICONIFIED_P (f);
8139 /* Check if fullscreen was specified before we where mapped the
8140 first time, i.e. from the command line. */
8141 if (!f->output_data.x->has_been_visible)
8144 x_check_fullscreen (f);
8145 #ifndef USE_GTK
8146 /* For systems that cannot synthesize `skip_taskbar' for
8147 unmapped windows do the following. */
8148 if (FRAME_SKIP_TASKBAR (f))
8149 x_set_skip_taskbar (f, Qt, Qnil);
8150 #endif /* Not USE_GTK */
8153 if (!iconified)
8155 /* The `z-group' is reset every time a frame becomes
8156 invisible. Handle this here. */
8157 if (FRAME_Z_GROUP (f) == z_group_above)
8158 x_set_z_group (f, Qabove, Qnil);
8159 else if (FRAME_Z_GROUP (f) == z_group_below)
8160 x_set_z_group (f, Qbelow, Qnil);
8163 SET_FRAME_VISIBLE (f, 1);
8164 SET_FRAME_ICONIFIED (f, false);
8165 f->output_data.x->has_been_visible = true;
8167 if (iconified)
8169 inev.ie.kind = DEICONIFY_EVENT;
8170 XSETFRAME (inev.ie.frame_or_window, f);
8172 else if (! NILP (Vframe_list) && ! NILP (XCDR (Vframe_list)))
8173 /* Force a redisplay sooner or later to update the
8174 frame titles in case this is the second frame. */
8175 record_asynch_buffer_change ();
8177 goto OTHER;
8179 case KeyPress:
8181 x_display_set_last_user_time (dpyinfo, event->xkey.time);
8182 ignore_next_mouse_click_timeout = 0;
8184 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8185 /* Dispatch KeyPress events when in menu. */
8186 if (popup_activated ())
8187 goto OTHER;
8188 #endif
8190 f = any;
8192 /* If mouse-highlight is an integer, input clears out
8193 mouse highlighting. */
8194 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
8195 #if ! defined (USE_GTK)
8196 && (f == 0
8197 || !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
8198 #endif
8201 clear_mouse_face (hlinfo);
8202 hlinfo->mouse_face_hidden = true;
8205 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
8206 if (f == 0)
8208 /* Scroll bars consume key events, but we want
8209 the keys to go to the scroll bar's frame. */
8210 Widget widget = XtWindowToWidget (dpyinfo->display,
8211 event->xkey.window);
8212 if (widget && XmIsScrollBar (widget))
8214 widget = XtParent (widget);
8215 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
8218 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
8220 if (f != 0)
8222 KeySym keysym, orig_keysym;
8223 /* al%imercury@uunet.uu.net says that making this 81
8224 instead of 80 fixed a bug whereby meta chars made
8225 his Emacs hang.
8227 It seems that some version of XmbLookupString has
8228 a bug of not returning XBufferOverflow in
8229 status_return even if the input is too long to
8230 fit in 81 bytes. So, we must prepare sufficient
8231 bytes for copy_buffer. 513 bytes (256 chars for
8232 two-byte character set) seems to be a fairly good
8233 approximation. -- 2000.8.10 handa@etl.go.jp */
8234 unsigned char copy_buffer[513];
8235 unsigned char *copy_bufptr = copy_buffer;
8236 int copy_bufsiz = sizeof (copy_buffer);
8237 int modifiers;
8238 Lisp_Object coding_system = Qlatin_1;
8239 Lisp_Object c;
8240 /* Event will be modified. */
8241 XKeyEvent xkey = event->xkey;
8243 #ifdef USE_GTK
8244 /* Don't pass keys to GTK. A Tab will shift focus to the
8245 tool bar in GTK 2.4. Keys will still go to menus and
8246 dialogs because in that case popup_activated is nonzero
8247 (see above). */
8248 *finish = X_EVENT_DROP;
8249 #endif
8251 xkey.state |= x_emacs_to_x_modifiers (FRAME_DISPLAY_INFO (f),
8252 extra_keyboard_modifiers);
8253 modifiers = xkey.state;
8255 /* This will have to go some day... */
8257 /* make_lispy_event turns chars into control chars.
8258 Don't do it here because XLookupString is too eager. */
8259 xkey.state &= ~ControlMask;
8260 xkey.state &= ~(dpyinfo->meta_mod_mask
8261 | dpyinfo->super_mod_mask
8262 | dpyinfo->hyper_mod_mask
8263 | dpyinfo->alt_mod_mask);
8265 /* In case Meta is ComposeCharacter,
8266 clear its status. According to Markus Ehrnsperger
8267 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
8268 this enables ComposeCharacter to work whether or
8269 not it is combined with Meta. */
8270 if (modifiers & dpyinfo->meta_mod_mask)
8271 memset (&compose_status, 0, sizeof (compose_status));
8273 #ifdef HAVE_X_I18N
8274 if (FRAME_XIC (f))
8276 Status status_return;
8278 coding_system = Vlocale_coding_system;
8279 nbytes = XmbLookupString (FRAME_XIC (f),
8280 &xkey, (char *) copy_bufptr,
8281 copy_bufsiz, &keysym,
8282 &status_return);
8283 if (status_return == XBufferOverflow)
8285 copy_bufsiz = nbytes + 1;
8286 copy_bufptr = alloca (copy_bufsiz);
8287 nbytes = XmbLookupString (FRAME_XIC (f),
8288 &xkey, (char *) copy_bufptr,
8289 copy_bufsiz, &keysym,
8290 &status_return);
8292 /* Xutf8LookupString is a new but already deprecated interface. -stef */
8293 if (status_return == XLookupNone)
8294 break;
8295 else if (status_return == XLookupChars)
8297 keysym = NoSymbol;
8298 modifiers = 0;
8300 else if (status_return != XLookupKeySym
8301 && status_return != XLookupBoth)
8302 emacs_abort ();
8304 else
8305 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
8306 copy_bufsiz, &keysym,
8307 &compose_status);
8308 #else
8309 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
8310 copy_bufsiz, &keysym,
8311 &compose_status);
8312 #endif
8314 /* If not using XIM/XIC, and a compose sequence is in progress,
8315 we break here. Otherwise, chars_matched is always 0. */
8316 if (compose_status.chars_matched > 0 && nbytes == 0)
8317 break;
8319 memset (&compose_status, 0, sizeof (compose_status));
8320 orig_keysym = keysym;
8322 /* Common for all keysym input events. */
8323 XSETFRAME (inev.ie.frame_or_window, f);
8324 inev.ie.modifiers
8325 = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), modifiers);
8326 inev.ie.timestamp = xkey.time;
8328 /* First deal with keysyms which have defined
8329 translations to characters. */
8330 if (keysym >= 32 && keysym < 128)
8331 /* Avoid explicitly decoding each ASCII character. */
8333 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
8334 inev.ie.code = keysym;
8335 goto done_keysym;
8338 /* Keysyms directly mapped to Unicode characters. */
8339 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
8341 if (keysym < 0x01000080)
8342 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
8343 else
8344 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
8345 inev.ie.code = keysym & 0xFFFFFF;
8346 goto done_keysym;
8349 /* Now non-ASCII. */
8350 if (HASH_TABLE_P (Vx_keysym_table)
8351 && (c = Fgethash (make_number (keysym),
8352 Vx_keysym_table,
8353 Qnil),
8354 NATNUMP (c)))
8356 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
8357 ? ASCII_KEYSTROKE_EVENT
8358 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
8359 inev.ie.code = XFASTINT (c);
8360 goto done_keysym;
8363 /* Random non-modifier sorts of keysyms. */
8364 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
8365 || keysym == XK_Delete
8366 #ifdef XK_ISO_Left_Tab
8367 || (keysym >= XK_ISO_Left_Tab
8368 && keysym <= XK_ISO_Enter)
8369 #endif
8370 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
8371 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
8372 #ifdef HPUX
8373 /* This recognizes the "extended function
8374 keys". It seems there's no cleaner way.
8375 Test IsModifierKey to avoid handling
8376 mode_switch incorrectly. */
8377 || (XK_Select <= keysym && keysym < XK_KP_Space)
8378 #endif
8379 #ifdef XK_dead_circumflex
8380 || orig_keysym == XK_dead_circumflex
8381 #endif
8382 #ifdef XK_dead_grave
8383 || orig_keysym == XK_dead_grave
8384 #endif
8385 #ifdef XK_dead_tilde
8386 || orig_keysym == XK_dead_tilde
8387 #endif
8388 #ifdef XK_dead_diaeresis
8389 || orig_keysym == XK_dead_diaeresis
8390 #endif
8391 #ifdef XK_dead_macron
8392 || orig_keysym == XK_dead_macron
8393 #endif
8394 #ifdef XK_dead_degree
8395 || orig_keysym == XK_dead_degree
8396 #endif
8397 #ifdef XK_dead_acute
8398 || orig_keysym == XK_dead_acute
8399 #endif
8400 #ifdef XK_dead_cedilla
8401 || orig_keysym == XK_dead_cedilla
8402 #endif
8403 #ifdef XK_dead_breve
8404 || orig_keysym == XK_dead_breve
8405 #endif
8406 #ifdef XK_dead_ogonek
8407 || orig_keysym == XK_dead_ogonek
8408 #endif
8409 #ifdef XK_dead_caron
8410 || orig_keysym == XK_dead_caron
8411 #endif
8412 #ifdef XK_dead_doubleacute
8413 || orig_keysym == XK_dead_doubleacute
8414 #endif
8415 #ifdef XK_dead_abovedot
8416 || orig_keysym == XK_dead_abovedot
8417 #endif
8418 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
8419 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
8420 /* Any "vendor-specific" key is ok. */
8421 || (orig_keysym & (1 << 28))
8422 || (keysym != NoSymbol && nbytes == 0))
8423 && ! (IsModifierKey (orig_keysym)
8424 /* The symbols from XK_ISO_Lock
8425 to XK_ISO_Last_Group_Lock
8426 don't have real modifiers but
8427 should be treated similarly to
8428 Mode_switch by Emacs. */
8429 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
8430 || (XK_ISO_Lock <= orig_keysym
8431 && orig_keysym <= XK_ISO_Last_Group_Lock)
8432 #endif
8435 STORE_KEYSYM_FOR_DEBUG (keysym);
8436 /* make_lispy_event will convert this to a symbolic
8437 key. */
8438 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
8439 inev.ie.code = keysym;
8440 goto done_keysym;
8443 { /* Raw bytes, not keysym. */
8444 ptrdiff_t i;
8445 int nchars, len;
8447 for (i = 0, nchars = 0; i < nbytes; i++)
8449 if (ASCII_CHAR_P (copy_bufptr[i]))
8450 nchars++;
8451 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
8454 if (nchars < nbytes)
8456 /* Decode the input data. */
8458 /* The input should be decoded with `coding_system'
8459 which depends on which X*LookupString function
8460 we used just above and the locale. */
8461 setup_coding_system (coding_system, &coding);
8462 coding.src_multibyte = false;
8463 coding.dst_multibyte = true;
8464 /* The input is converted to events, thus we can't
8465 handle composition. Anyway, there's no XIM that
8466 gives us composition information. */
8467 coding.common_flags &= ~CODING_ANNOTATION_MASK;
8469 SAFE_NALLOCA (coding.destination, MAX_MULTIBYTE_LENGTH,
8470 nbytes);
8471 coding.dst_bytes = MAX_MULTIBYTE_LENGTH * nbytes;
8472 coding.mode |= CODING_MODE_LAST_BLOCK;
8473 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
8474 nbytes = coding.produced;
8475 nchars = coding.produced_char;
8476 copy_bufptr = coding.destination;
8479 /* Convert the input data to a sequence of
8480 character events. */
8481 for (i = 0; i < nbytes; i += len)
8483 int ch;
8484 if (nchars == nbytes)
8485 ch = copy_bufptr[i], len = 1;
8486 else
8487 ch = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
8488 inev.ie.kind = (SINGLE_BYTE_CHAR_P (ch)
8489 ? ASCII_KEYSTROKE_EVENT
8490 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
8491 inev.ie.code = ch;
8492 kbd_buffer_store_buffered_event (&inev, hold_quit);
8495 count += nchars;
8497 inev.ie.kind = NO_EVENT; /* Already stored above. */
8499 if (keysym == NoSymbol)
8500 break;
8502 /* FIXME: check side effects and remove this. */
8503 ((XEvent *) event)->xkey = xkey;
8505 done_keysym:
8506 #ifdef HAVE_X_I18N
8507 /* Don't dispatch this event since XtDispatchEvent calls
8508 XFilterEvent, and two calls in a row may freeze the
8509 client. */
8510 break;
8511 #else
8512 goto OTHER;
8513 #endif
8515 case KeyRelease:
8516 x_display_set_last_user_time (dpyinfo, event->xkey.time);
8517 #ifdef HAVE_X_I18N
8518 /* Don't dispatch this event since XtDispatchEvent calls
8519 XFilterEvent, and two calls in a row may freeze the
8520 client. */
8521 break;
8522 #else
8523 goto OTHER;
8524 #endif
8526 case EnterNotify:
8527 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
8528 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8530 f = any;
8532 if (f && x_mouse_click_focus_ignore_position)
8533 ignore_next_mouse_click_timeout = event->xmotion.time + 200;
8535 /* EnterNotify counts as mouse movement,
8536 so update things that depend on mouse position. */
8537 if (f && !f->output_data.x->hourglass_p)
8538 note_mouse_movement (f, &event->xmotion);
8539 #ifdef USE_GTK
8540 /* We may get an EnterNotify on the buttons in the toolbar. In that
8541 case we moved out of any highlighted area and need to note this. */
8542 if (!f && dpyinfo->last_mouse_glyph_frame)
8543 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
8544 #endif
8545 goto OTHER;
8547 case FocusIn:
8548 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8549 goto OTHER;
8551 case LeaveNotify:
8552 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
8553 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8555 f = x_top_window_to_frame (dpyinfo, event->xcrossing.window);
8556 if (f)
8558 if (f == hlinfo->mouse_face_mouse_frame)
8560 /* If we move outside the frame, then we're
8561 certainly no longer on any text in the frame. */
8562 clear_mouse_face (hlinfo);
8563 hlinfo->mouse_face_mouse_frame = 0;
8566 /* Generate a nil HELP_EVENT to cancel a help-echo.
8567 Do it only if there's something to cancel.
8568 Otherwise, the startup message is cleared when
8569 the mouse leaves the frame. */
8570 if (any_help_event_p)
8571 do_help = -1;
8573 #ifdef USE_GTK
8574 /* See comment in EnterNotify above */
8575 else if (dpyinfo->last_mouse_glyph_frame)
8576 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
8577 #endif
8578 goto OTHER;
8580 case FocusOut:
8581 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8582 goto OTHER;
8584 case MotionNotify:
8586 x_display_set_last_user_time (dpyinfo, event->xmotion.time);
8587 previous_help_echo_string = help_echo_string;
8588 help_echo_string = Qnil;
8590 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
8591 : x_window_to_frame (dpyinfo, event->xmotion.window));
8593 if (hlinfo->mouse_face_hidden)
8595 hlinfo->mouse_face_hidden = false;
8596 clear_mouse_face (hlinfo);
8599 #ifdef USE_GTK
8600 if (f && xg_event_is_for_scrollbar (f, event))
8601 f = 0;
8602 #endif
8603 if (f)
8605 /* Maybe generate a SELECT_WINDOW_EVENT for
8606 `mouse-autoselect-window' but don't let popup menus
8607 interfere with this (Bug#1261). */
8608 if (!NILP (Vmouse_autoselect_window)
8609 && !popup_activated ()
8610 /* Don't switch if we're currently in the minibuffer.
8611 This tries to work around problems where the
8612 minibuffer gets unselected unexpectedly, and where
8613 you then have to move your mouse all the way down to
8614 the minibuffer to select it. */
8615 && !MINI_WINDOW_P (XWINDOW (selected_window))
8616 /* With `focus-follows-mouse' non-nil create an event
8617 also when the target window is on another frame. */
8618 && (f == XFRAME (selected_frame)
8619 || !NILP (focus_follows_mouse)))
8621 static Lisp_Object last_mouse_window;
8622 Lisp_Object window = window_from_coordinates
8623 (f, event->xmotion.x, event->xmotion.y, 0, false);
8625 /* A window will be autoselected only when it is not
8626 selected now and the last mouse movement event was
8627 not in it. The remainder of the code is a bit vague
8628 wrt what a "window" is. For immediate autoselection,
8629 the window is usually the entire window but for GTK
8630 where the scroll bars don't count. For delayed
8631 autoselection the window is usually the window's text
8632 area including the margins. */
8633 if (WINDOWP (window)
8634 && !EQ (window, last_mouse_window)
8635 && !EQ (window, selected_window))
8637 inev.ie.kind = SELECT_WINDOW_EVENT;
8638 inev.ie.frame_or_window = window;
8641 /* Remember the last window where we saw the mouse. */
8642 last_mouse_window = window;
8645 if (!note_mouse_movement (f, &event->xmotion))
8646 help_echo_string = previous_help_echo_string;
8648 else
8650 #ifndef USE_TOOLKIT_SCROLL_BARS
8651 struct scroll_bar *bar
8652 = x_window_to_scroll_bar (event->xmotion.display,
8653 event->xmotion.window, 2);
8655 if (bar)
8656 x_scroll_bar_note_movement (bar, &event->xmotion);
8657 #endif /* USE_TOOLKIT_SCROLL_BARS */
8659 /* If we move outside the frame, then we're
8660 certainly no longer on any text in the frame. */
8661 clear_mouse_face (hlinfo);
8664 /* If the contents of the global variable help_echo_string
8665 has changed, generate a HELP_EVENT. */
8666 if (!NILP (help_echo_string)
8667 || !NILP (previous_help_echo_string))
8668 do_help = 1;
8669 goto OTHER;
8672 case ConfigureNotify:
8673 /* An opaque move can generate a stream of events as the window
8674 is dragged around. If the connection round trip time isn't
8675 really short, they may come faster than we can respond to
8676 them, given the multiple queries we can do to check window
8677 manager state, translate coordinates, etc.
8679 So if this ConfigureNotify is immediately followed by another
8680 for the same window, use the info from the latest update, and
8681 consider the events all handled. */
8682 /* Opaque resize may be trickier; ConfigureNotify events are
8683 mixed with Expose events for multiple windows. */
8684 configureEvent = *event;
8685 while (XPending (dpyinfo->display))
8687 XNextEvent (dpyinfo->display, &next_event);
8688 if (next_event.type != ConfigureNotify
8689 || next_event.xconfigure.window != event->xconfigure.window
8690 /* Skipping events with different sizes can lead to a
8691 mispositioned mode line at initial window creation.
8692 Only drop window motion events for now. */
8693 || next_event.xconfigure.width != event->xconfigure.width
8694 || next_event.xconfigure.height != event->xconfigure.height)
8696 XPutBackEvent (dpyinfo->display, &next_event);
8697 break;
8699 else
8700 configureEvent = next_event;
8703 f = x_top_window_to_frame (dpyinfo, configureEvent.xconfigure.window);
8704 /* Unfortunately, we need to call font_drop_xrender_surfaces for
8705 _all_ ConfigureNotify events, otherwise we miss some and
8706 flicker. Don't try to optimize these calls by looking only
8707 for size changes: that's not sufficient. We miss some
8708 surface invalidations and flicker. */
8709 block_input ();
8710 if (f && FRAME_X_DOUBLE_BUFFERED_P (f))
8711 font_drop_xrender_surfaces (f);
8712 unblock_input ();
8713 #ifdef USE_CAIRO
8714 if (f) x_cr_destroy_surface (f);
8715 #endif
8716 #ifdef USE_GTK
8717 if (!f
8718 && (f = any)
8719 && configureEvent.xconfigure.window == FRAME_X_WINDOW (f))
8721 block_input ();
8722 if (FRAME_X_DOUBLE_BUFFERED_P (f))
8723 font_drop_xrender_surfaces (f);
8724 unblock_input ();
8725 xg_frame_resized (f, configureEvent.xconfigure.width,
8726 configureEvent.xconfigure.height);
8727 #ifdef USE_CAIRO
8728 x_cr_destroy_surface (f);
8729 #endif
8730 f = 0;
8732 #endif
8733 if (f)
8735 #ifdef USE_GTK
8736 /* For GTK+ don't call x_net_wm_state for the scroll bar
8737 window. (Bug#24963, Bug#25887) */
8738 if (configureEvent.xconfigure.window == FRAME_X_WINDOW (f))
8739 #endif
8740 x_net_wm_state (f, configureEvent.xconfigure.window);
8742 #ifdef USE_X_TOOLKIT
8743 /* Tip frames are pure X window, set size for them. */
8744 if (! NILP (tip_frame) && XFRAME (tip_frame) == f)
8746 if (FRAME_PIXEL_HEIGHT (f) != configureEvent.xconfigure.height
8747 || FRAME_PIXEL_WIDTH (f) != configureEvent.xconfigure.width)
8749 SET_FRAME_GARBAGED (f);
8751 FRAME_PIXEL_HEIGHT (f) = configureEvent.xconfigure.height;
8752 FRAME_PIXEL_WIDTH (f) = configureEvent.xconfigure.width;
8754 #endif
8756 #ifndef USE_X_TOOLKIT
8757 #ifndef USE_GTK
8758 int width =
8759 FRAME_PIXEL_TO_TEXT_WIDTH (f, configureEvent.xconfigure.width);
8760 int height =
8761 FRAME_PIXEL_TO_TEXT_HEIGHT (f, configureEvent.xconfigure.height);
8763 /* In the toolkit version, change_frame_size
8764 is called by the code that handles resizing
8765 of the EmacsFrame widget. */
8767 /* Even if the number of character rows and columns has
8768 not changed, the font size may have changed, so we need
8769 to check the pixel dimensions as well. */
8770 if (width != FRAME_TEXT_WIDTH (f)
8771 || height != FRAME_TEXT_HEIGHT (f)
8772 || configureEvent.xconfigure.width != FRAME_PIXEL_WIDTH (f)
8773 || configureEvent.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
8775 change_frame_size (f, width, height, false, true, false, true);
8776 x_clear_under_internal_border (f);
8777 SET_FRAME_GARBAGED (f);
8778 cancel_mouse_face (f);
8780 #endif /* not USE_GTK */
8781 #endif
8783 #ifdef USE_GTK
8784 /* GTK creates windows but doesn't map them.
8785 Only get real positions when mapped. */
8786 if (FRAME_GTK_OUTER_WIDGET (f)
8787 && gtk_widget_get_mapped (FRAME_GTK_OUTER_WIDGET (f)))
8788 #endif
8790 int old_left = f->left_pos;
8791 int old_top = f->top_pos;
8792 Lisp_Object frame = Qnil;
8794 XSETFRAME (frame, f);
8796 if (!FRAME_PARENT_FRAME (f))
8797 x_real_positions (f, &f->left_pos, &f->top_pos);
8798 else
8800 Window root;
8801 unsigned int dummy_uint;
8803 block_input ();
8804 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8805 &root, &f->left_pos, &f->top_pos,
8806 &dummy_uint, &dummy_uint, &dummy_uint, &dummy_uint);
8807 unblock_input ();
8810 if (old_left != f->left_pos || old_top != f->top_pos)
8812 inev.ie.kind = MOVE_FRAME_EVENT;
8813 XSETFRAME (inev.ie.frame_or_window, f);
8818 #ifdef HAVE_X_I18N
8819 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
8820 xic_set_statusarea (f);
8821 #endif
8824 goto OTHER;
8826 case ButtonRelease:
8827 case ButtonPress:
8829 /* If we decide we want to generate an event to be seen
8830 by the rest of Emacs, we put it here. */
8831 bool tool_bar_p = false;
8833 memset (&compose_status, 0, sizeof (compose_status));
8834 dpyinfo->last_mouse_glyph_frame = NULL;
8835 x_display_set_last_user_time (dpyinfo, event->xbutton.time);
8837 if (x_mouse_grabbed (dpyinfo))
8838 f = dpyinfo->last_mouse_frame;
8839 else
8841 f = x_window_to_frame (dpyinfo, event->xbutton.window);
8843 if (f && event->xbutton.type == ButtonPress
8844 && !popup_activated ()
8845 && !x_window_to_scroll_bar (event->xbutton.display,
8846 event->xbutton.window, 2)
8847 && !FRAME_NO_ACCEPT_FOCUS (f))
8849 /* When clicking into a child frame or when clicking
8850 into a parent frame with the child frame selected and
8851 `no-accept-focus' is not set, select the clicked
8852 frame. */
8853 struct frame *hf = dpyinfo->x_highlight_frame;
8855 if (FRAME_PARENT_FRAME (f) || (hf && frame_ancestor_p (f, hf)))
8857 block_input ();
8858 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8859 RevertToParent, CurrentTime);
8860 if (FRAME_PARENT_FRAME (f))
8861 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8862 unblock_input ();
8867 #ifdef USE_GTK
8868 if (f && xg_event_is_for_scrollbar (f, event))
8869 f = 0;
8870 #endif
8871 if (f)
8873 #if ! defined (USE_GTK)
8874 /* Is this in the tool-bar? */
8875 if (WINDOWP (f->tool_bar_window)
8876 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
8878 Lisp_Object window;
8879 int x = event->xbutton.x;
8880 int y = event->xbutton.y;
8882 window = window_from_coordinates (f, x, y, 0, true);
8883 tool_bar_p = EQ (window, f->tool_bar_window);
8885 if (tool_bar_p && event->xbutton.button < 4)
8886 handle_tool_bar_click
8887 (f, x, y, event->xbutton.type == ButtonPress,
8888 x_x_to_emacs_modifiers (dpyinfo, event->xbutton.state));
8890 #endif /* !USE_GTK */
8892 if (!tool_bar_p)
8893 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8894 if (! popup_activated ())
8895 #endif
8897 if (ignore_next_mouse_click_timeout)
8899 if (event->type == ButtonPress
8900 && event->xbutton.time > ignore_next_mouse_click_timeout)
8902 ignore_next_mouse_click_timeout = 0;
8903 construct_mouse_click (&inev.ie, &event->xbutton, f);
8905 if (event->type == ButtonRelease)
8906 ignore_next_mouse_click_timeout = 0;
8908 else
8909 construct_mouse_click (&inev.ie, &event->xbutton, f);
8911 if (FRAME_X_EMBEDDED_P (f))
8912 xembed_send_message (f, event->xbutton.time,
8913 XEMBED_REQUEST_FOCUS, 0, 0, 0);
8915 else
8917 struct scroll_bar *bar
8918 = x_window_to_scroll_bar (event->xbutton.display,
8919 event->xbutton.window, 2);
8921 #ifdef USE_TOOLKIT_SCROLL_BARS
8922 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
8923 scroll bars. */
8924 if (bar && event->xbutton.state & ControlMask)
8926 x_scroll_bar_handle_click (bar, event, &inev.ie);
8927 *finish = X_EVENT_DROP;
8929 #else /* not USE_TOOLKIT_SCROLL_BARS */
8930 if (bar)
8931 x_scroll_bar_handle_click (bar, event, &inev.ie);
8932 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8935 if (event->type == ButtonPress)
8937 dpyinfo->grabbed |= (1 << event->xbutton.button);
8938 dpyinfo->last_mouse_frame = f;
8939 #if ! defined (USE_GTK)
8940 if (f && !tool_bar_p)
8941 f->last_tool_bar_item = -1;
8942 #endif /* not USE_GTK */
8944 else
8945 dpyinfo->grabbed &= ~(1 << event->xbutton.button);
8947 /* Ignore any mouse motion that happened before this event;
8948 any subsequent mouse-movement Emacs events should reflect
8949 only motion after the ButtonPress/Release. */
8950 if (f != 0)
8951 f->mouse_moved = false;
8953 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8954 f = x_menubar_window_to_frame (dpyinfo, event);
8955 /* For a down-event in the menu bar,
8956 don't pass it to Xt right now.
8957 Instead, save it away
8958 and we will pass it to Xt from kbd_buffer_get_event.
8959 That way, we can run some Lisp code first. */
8960 if (! popup_activated ()
8961 #ifdef USE_GTK
8962 /* Gtk+ menus only react to the first three buttons. */
8963 && event->xbutton.button < 3
8964 #endif
8965 && f && event->type == ButtonPress
8966 /* Verify the event is really within the menu bar
8967 and not just sent to it due to grabbing. */
8968 && event->xbutton.x >= 0
8969 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
8970 && event->xbutton.y >= 0
8971 && event->xbutton.y < FRAME_MENUBAR_HEIGHT (f)
8972 && event->xbutton.same_screen)
8974 if (!f->output_data.x->saved_menu_event)
8975 f->output_data.x->saved_menu_event = xmalloc (sizeof *event);
8976 *f->output_data.x->saved_menu_event = *event;
8977 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT;
8978 XSETFRAME (inev.ie.frame_or_window, f);
8979 *finish = X_EVENT_DROP;
8981 else
8982 goto OTHER;
8983 #endif /* USE_X_TOOLKIT || USE_GTK */
8985 break;
8987 case CirculateNotify:
8988 goto OTHER;
8990 case CirculateRequest:
8991 goto OTHER;
8993 case VisibilityNotify:
8994 goto OTHER;
8996 case MappingNotify:
8997 /* Someone has changed the keyboard mapping - update the
8998 local cache. */
8999 switch (event->xmapping.request)
9001 case MappingModifier:
9002 x_find_modifier_meanings (dpyinfo);
9003 FALLTHROUGH;
9004 case MappingKeyboard:
9005 XRefreshKeyboardMapping ((XMappingEvent *) &event->xmapping);
9007 goto OTHER;
9009 case DestroyNotify:
9010 xft_settings_event (dpyinfo, event);
9011 break;
9013 default:
9014 OTHER:
9015 #ifdef USE_X_TOOLKIT
9016 block_input ();
9017 if (*finish != X_EVENT_DROP)
9018 XtDispatchEvent ((XEvent *) event);
9019 unblock_input ();
9020 #endif /* USE_X_TOOLKIT */
9021 break;
9024 done:
9025 if (inev.ie.kind != NO_EVENT)
9027 kbd_buffer_store_buffered_event (&inev, hold_quit);
9028 count++;
9031 if (do_help
9032 && !(hold_quit && hold_quit->kind != NO_EVENT))
9034 Lisp_Object frame;
9036 if (f)
9037 XSETFRAME (frame, f);
9038 else
9039 frame = Qnil;
9041 if (do_help > 0)
9043 any_help_event_p = true;
9044 gen_help_event (help_echo_string, frame, help_echo_window,
9045 help_echo_object, help_echo_pos);
9047 else
9049 help_echo_string = Qnil;
9050 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
9052 count++;
9055 /* Sometimes event processing draws to the frame outside redisplay.
9056 To ensure that these changes become visible, draw them here. */
9057 flush_dirty_back_buffers ();
9058 SAFE_FREE ();
9059 return count;
9062 #if defined USE_X_TOOLKIT || defined USE_MOTIF || defined USE_GTK
9064 /* Handles the XEvent EVENT on display DISPLAY.
9065 This is used for event loops outside the normal event handling,
9066 i.e. looping while a popup menu or a dialog is posted.
9068 Returns the value handle_one_xevent sets in the finish argument. */
9070 x_dispatch_event (XEvent *event, Display *display)
9072 struct x_display_info *dpyinfo;
9073 int finish = X_EVENT_NORMAL;
9075 dpyinfo = x_display_info_for_display (display);
9077 if (dpyinfo)
9078 handle_one_xevent (dpyinfo, event, &finish, 0);
9080 return finish;
9082 #endif
9084 /* Read events coming from the X server.
9085 Return as soon as there are no more events to be read.
9087 Return the number of characters stored into the buffer,
9088 thus pretending to be `read' (except the characters we store
9089 in the keyboard buffer can be multibyte, so are not necessarily
9090 C chars). */
9092 static int
9093 XTread_socket (struct terminal *terminal, struct input_event *hold_quit)
9095 int count = 0;
9096 bool event_found = false;
9097 struct x_display_info *dpyinfo = terminal->display_info.x;
9099 block_input ();
9101 /* For debugging, this gives a way to fake an I/O error. */
9102 if (dpyinfo == XTread_socket_fake_io_error)
9104 XTread_socket_fake_io_error = 0;
9105 x_io_error_quitter (dpyinfo->display);
9108 #ifndef USE_GTK
9109 while (XPending (dpyinfo->display))
9111 int finish;
9112 XEvent event;
9114 XNextEvent (dpyinfo->display, &event);
9116 #ifdef HAVE_X_I18N
9117 /* Filter events for the current X input method. */
9118 if (x_filter_event (dpyinfo, &event))
9119 continue;
9120 #endif
9121 event_found = true;
9123 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
9125 if (finish == X_EVENT_GOTO_OUT)
9126 break;
9129 #else /* USE_GTK */
9131 /* For GTK we must use the GTK event loop. But XEvents gets passed
9132 to our filter function above, and then to the big event switch.
9133 We use a bunch of globals to communicate with our filter function,
9134 that is kind of ugly, but it works.
9136 There is no way to do one display at the time, GTK just does events
9137 from all displays. */
9139 while (gtk_events_pending ())
9141 current_count = count;
9142 current_hold_quit = hold_quit;
9144 gtk_main_iteration ();
9146 count = current_count;
9147 current_count = -1;
9148 current_hold_quit = 0;
9150 if (current_finish == X_EVENT_GOTO_OUT)
9151 break;
9153 #endif /* USE_GTK */
9155 /* On some systems, an X bug causes Emacs to get no more events
9156 when the window is destroyed. Detect that. (1994.) */
9157 if (! event_found)
9159 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
9160 One XNOOP in 100 loops will make Emacs terminate.
9161 B. Bretthauer, 1994 */
9162 x_noop_count++;
9163 if (x_noop_count >= 100)
9165 x_noop_count=0;
9167 if (next_noop_dpyinfo == 0)
9168 next_noop_dpyinfo = x_display_list;
9170 XNoOp (next_noop_dpyinfo->display);
9172 /* Each time we get here, cycle through the displays now open. */
9173 next_noop_dpyinfo = next_noop_dpyinfo->next;
9177 /* If the focus was just given to an auto-raising frame,
9178 raise it now. FIXME: handle more than one such frame. */
9179 if (dpyinfo->x_pending_autoraise_frame)
9181 x_raise_frame (dpyinfo->x_pending_autoraise_frame);
9182 dpyinfo->x_pending_autoraise_frame = NULL;
9185 unblock_input ();
9187 return count;
9193 /***********************************************************************
9194 Text Cursor
9195 ***********************************************************************/
9197 /* Set clipping for output in glyph row ROW. W is the window in which
9198 we operate. GC is the graphics context to set clipping in.
9200 ROW may be a text row or, e.g., a mode line. Text rows must be
9201 clipped to the interior of the window dedicated to text display,
9202 mode lines must be clipped to the whole window. */
9204 static void
9205 x_clip_to_row (struct window *w, struct glyph_row *row,
9206 enum glyph_row_area area, GC gc)
9208 struct frame *f = XFRAME (WINDOW_FRAME (w));
9209 XRectangle clip_rect;
9210 int window_x, window_y, window_width;
9212 window_box (w, area, &window_x, &window_y, &window_width, 0);
9214 clip_rect.x = window_x;
9215 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
9216 clip_rect.y = max (clip_rect.y, window_y);
9217 clip_rect.width = window_width;
9218 clip_rect.height = row->visible_height;
9220 x_set_clip_rectangles (f, gc, &clip_rect, 1);
9224 /* Draw a hollow box cursor on window W in glyph row ROW. */
9226 static void
9227 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
9229 struct frame *f = XFRAME (WINDOW_FRAME (w));
9230 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9231 Display *dpy = FRAME_X_DISPLAY (f);
9232 int x, y, wd, h;
9233 XGCValues xgcv;
9234 struct glyph *cursor_glyph;
9235 GC gc;
9237 /* Get the glyph the cursor is on. If we can't tell because
9238 the current matrix is invalid or such, give up. */
9239 cursor_glyph = get_phys_cursor_glyph (w);
9240 if (cursor_glyph == NULL)
9241 return;
9243 /* Compute frame-relative coordinates for phys cursor. */
9244 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
9245 wd = w->phys_cursor_width - 1;
9247 /* The foreground of cursor_gc is typically the same as the normal
9248 background color, which can cause the cursor box to be invisible. */
9249 xgcv.foreground = f->output_data.x->cursor_pixel;
9250 if (dpyinfo->scratch_cursor_gc)
9251 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
9252 else
9253 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_DRAWABLE (f),
9254 GCForeground, &xgcv);
9255 gc = dpyinfo->scratch_cursor_gc;
9257 /* When on R2L character, show cursor at the right edge of the
9258 glyph, unless the cursor box is as wide as the glyph or wider
9259 (the latter happens when x-stretch-cursor is non-nil). */
9260 if ((cursor_glyph->resolved_level & 1) != 0
9261 && cursor_glyph->pixel_width > wd)
9263 x += cursor_glyph->pixel_width - wd;
9264 if (wd > 0)
9265 wd -= 1;
9267 /* Set clipping, draw the rectangle, and reset clipping again. */
9268 x_clip_to_row (w, row, TEXT_AREA, gc);
9269 x_draw_rectangle (f, gc, x, y, wd, h - 1);
9270 x_reset_clip_rectangles (f, gc);
9274 /* Draw a bar cursor on window W in glyph row ROW.
9276 Implementation note: One would like to draw a bar cursor with an
9277 angle equal to the one given by the font property XA_ITALIC_ANGLE.
9278 Unfortunately, I didn't find a font yet that has this property set.
9279 --gerd. */
9281 static void
9282 x_draw_bar_cursor (struct window *w, struct glyph_row *row, int width, enum text_cursor_kinds kind)
9284 struct frame *f = XFRAME (w->frame);
9285 struct glyph *cursor_glyph;
9287 /* If cursor is out of bounds, don't draw garbage. This can happen
9288 in mini-buffer windows when switching between echo area glyphs
9289 and mini-buffer. */
9290 cursor_glyph = get_phys_cursor_glyph (w);
9291 if (cursor_glyph == NULL)
9292 return;
9294 /* Experimental avoidance of cursor on xwidget. */
9295 if (cursor_glyph->type == XWIDGET_GLYPH)
9296 return;
9298 /* If on an image, draw like a normal cursor. That's usually better
9299 visible than drawing a bar, esp. if the image is large so that
9300 the bar might not be in the window. */
9301 if (cursor_glyph->type == IMAGE_GLYPH)
9303 struct glyph_row *r;
9304 r = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
9305 draw_phys_cursor_glyph (w, r, DRAW_CURSOR);
9307 else
9309 Display *dpy = FRAME_X_DISPLAY (f);
9310 Drawable drawable = FRAME_X_DRAWABLE (f);
9311 GC gc = FRAME_DISPLAY_INFO (f)->scratch_cursor_gc;
9312 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
9313 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
9314 XGCValues xgcv;
9316 /* If the glyph's background equals the color we normally draw
9317 the bars cursor in, the bar cursor in its normal color is
9318 invisible. Use the glyph's foreground color instead in this
9319 case, on the assumption that the glyph's colors are chosen so
9320 that the glyph is legible. */
9321 if (face->background == f->output_data.x->cursor_pixel)
9322 xgcv.background = xgcv.foreground = face->foreground;
9323 else
9324 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
9325 xgcv.graphics_exposures = False;
9327 if (gc)
9328 XChangeGC (dpy, gc, mask, &xgcv);
9329 else
9331 gc = XCreateGC (dpy, drawable, mask, &xgcv);
9332 FRAME_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
9335 x_clip_to_row (w, row, TEXT_AREA, gc);
9337 if (kind == BAR_CURSOR)
9339 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9341 if (width < 0)
9342 width = FRAME_CURSOR_WIDTH (f);
9343 width = min (cursor_glyph->pixel_width, width);
9345 w->phys_cursor_width = width;
9347 /* If the character under cursor is R2L, draw the bar cursor
9348 on the right of its glyph, rather than on the left. */
9349 if ((cursor_glyph->resolved_level & 1) != 0)
9350 x += cursor_glyph->pixel_width - width;
9352 x_fill_rectangle (f, gc, x,
9353 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
9354 width, row->height);
9356 else /* HBAR_CURSOR */
9358 int dummy_x, dummy_y, dummy_h;
9359 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9361 if (width < 0)
9362 width = row->height;
9364 width = min (row->height, width);
9366 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
9367 &dummy_y, &dummy_h);
9369 if ((cursor_glyph->resolved_level & 1) != 0
9370 && cursor_glyph->pixel_width > w->phys_cursor_width - 1)
9371 x += cursor_glyph->pixel_width - w->phys_cursor_width + 1;
9372 x_fill_rectangle (f, gc, x,
9373 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
9374 row->height - width),
9375 w->phys_cursor_width - 1, width);
9378 x_reset_clip_rectangles (f, gc);
9383 /* RIF: Define cursor CURSOR on frame F. */
9385 static void
9386 x_define_frame_cursor (struct frame *f, Cursor cursor)
9388 if (!f->pointer_invisible
9389 && f->output_data.x->current_cursor != cursor)
9390 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
9391 f->output_data.x->current_cursor = cursor;
9395 /* RIF: Clear area on frame F. */
9397 static void
9398 x_clear_frame_area (struct frame *f, int x, int y, int width, int height)
9400 x_clear_area (f, x, y, width, height);
9404 /* RIF: Draw cursor on window W. */
9406 static void
9407 x_draw_window_cursor (struct window *w, struct glyph_row *glyph_row, int x,
9408 int y, enum text_cursor_kinds cursor_type,
9409 int cursor_width, bool on_p, bool active_p)
9411 struct frame *f = XFRAME (WINDOW_FRAME (w));
9413 if (on_p)
9415 w->phys_cursor_type = cursor_type;
9416 w->phys_cursor_on_p = true;
9418 if (glyph_row->exact_window_width_line_p
9419 && (glyph_row->reversed_p
9420 ? (w->phys_cursor.hpos < 0)
9421 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
9423 glyph_row->cursor_in_fringe_p = true;
9424 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
9426 else
9428 switch (cursor_type)
9430 case HOLLOW_BOX_CURSOR:
9431 x_draw_hollow_cursor (w, glyph_row);
9432 break;
9434 case FILLED_BOX_CURSOR:
9435 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
9436 break;
9438 case BAR_CURSOR:
9439 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
9440 break;
9442 case HBAR_CURSOR:
9443 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
9444 break;
9446 case NO_CURSOR:
9447 w->phys_cursor_width = 0;
9448 break;
9450 default:
9451 emacs_abort ();
9455 #ifdef HAVE_X_I18N
9456 if (w == XWINDOW (f->selected_window))
9457 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
9458 xic_set_preeditarea (w, x, y);
9459 #endif
9462 XFlush (FRAME_X_DISPLAY (f));
9466 /* Icons. */
9468 /* Make the x-window of frame F use the gnu icon bitmap. */
9470 bool
9471 x_bitmap_icon (struct frame *f, Lisp_Object file)
9473 ptrdiff_t bitmap_id;
9475 if (FRAME_X_WINDOW (f) == 0)
9476 return true;
9478 /* Free up our existing icon bitmap and mask if any. */
9479 if (f->output_data.x->icon_bitmap > 0)
9480 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
9481 f->output_data.x->icon_bitmap = 0;
9483 if (STRINGP (file))
9485 #ifdef USE_GTK
9486 /* Use gtk_window_set_icon_from_file () if available,
9487 It's not restricted to bitmaps */
9488 if (xg_set_icon (f, file))
9489 return false;
9490 #endif /* USE_GTK */
9491 bitmap_id = x_create_bitmap_from_file (f, file);
9492 x_create_bitmap_mask (f, bitmap_id);
9494 else
9496 /* Create the GNU bitmap and mask if necessary. */
9497 if (FRAME_DISPLAY_INFO (f)->icon_bitmap_id < 0)
9499 ptrdiff_t rc = -1;
9501 #ifdef USE_GTK
9503 if (xg_set_icon (f, xg_default_icon_file)
9504 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
9506 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = -2;
9507 return false;
9510 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
9512 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
9513 if (rc != -1)
9514 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
9516 #endif
9518 /* If all else fails, use the (black and white) xbm image. */
9519 if (rc == -1)
9521 rc = x_create_bitmap_from_data (f, (char *) gnu_xbm_bits,
9522 gnu_xbm_width, gnu_xbm_height);
9523 if (rc == -1)
9524 return true;
9526 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
9527 x_create_bitmap_mask (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
9531 /* The first time we create the GNU bitmap and mask,
9532 this increments the ref-count one extra time.
9533 As a result, the GNU bitmap and mask are never freed.
9534 That way, we don't have to worry about allocating it again. */
9535 x_reference_bitmap (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
9537 bitmap_id = FRAME_DISPLAY_INFO (f)->icon_bitmap_id;
9540 x_wm_set_icon_pixmap (f, bitmap_id);
9541 f->output_data.x->icon_bitmap = bitmap_id;
9543 return false;
9547 /* Make the x-window of frame F use a rectangle with text.
9548 Use ICON_NAME as the text. */
9550 bool
9551 x_text_icon (struct frame *f, const char *icon_name)
9553 if (FRAME_X_WINDOW (f) == 0)
9554 return true;
9557 XTextProperty text;
9558 text.value = (unsigned char *) icon_name;
9559 text.encoding = XA_STRING;
9560 text.format = 8;
9561 text.nitems = strlen (icon_name);
9562 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
9565 if (f->output_data.x->icon_bitmap > 0)
9566 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
9567 f->output_data.x->icon_bitmap = 0;
9568 x_wm_set_icon_pixmap (f, 0);
9570 return false;
9573 #define X_ERROR_MESSAGE_SIZE 200
9575 /* If non-nil, this should be a string.
9576 It means catch X errors and store the error message in this string.
9578 The reason we use a stack is that x_catch_error/x_uncatch_error can
9579 be called from a signal handler.
9582 struct x_error_message_stack {
9583 char string[X_ERROR_MESSAGE_SIZE];
9584 Display *dpy;
9585 x_special_error_handler handler;
9586 void *handler_data;
9587 struct x_error_message_stack *prev;
9589 static struct x_error_message_stack *x_error_message;
9591 /* An X error handler which stores the error message in
9592 *x_error_message. This is called from x_error_handler if
9593 x_catch_errors is in effect. */
9595 static void
9596 x_error_catcher (Display *display, XErrorEvent *event)
9598 XGetErrorText (display, event->error_code,
9599 x_error_message->string,
9600 X_ERROR_MESSAGE_SIZE);
9601 if (x_error_message->handler)
9602 x_error_message->handler (display, event, x_error_message->string,
9603 x_error_message->handler_data);
9606 /* Begin trapping X errors for display DPY. Actually we trap X errors
9607 for all displays, but DPY should be the display you are actually
9608 operating on.
9610 After calling this function, X protocol errors no longer cause
9611 Emacs to exit; instead, they are recorded in the string
9612 stored in *x_error_message.
9614 Calling x_check_errors signals an Emacs error if an X error has
9615 occurred since the last call to x_catch_errors or x_check_errors.
9617 Calling x_uncatch_errors resumes the normal error handling.
9618 Calling x_uncatch_errors_after_check is similar, but skips an XSync
9619 to the server, and should be used only immediately after
9620 x_had_errors_p or x_check_errors. */
9622 void
9623 x_catch_errors_with_handler (Display *dpy, x_special_error_handler handler,
9624 void *handler_data)
9626 struct x_error_message_stack *data = xmalloc (sizeof *data);
9628 /* Make sure any errors from previous requests have been dealt with. */
9629 XSync (dpy, False);
9631 data->dpy = dpy;
9632 data->string[0] = 0;
9633 data->handler = handler;
9634 data->handler_data = handler_data;
9635 data->prev = x_error_message;
9636 x_error_message = data;
9639 void
9640 x_catch_errors (Display *dpy)
9642 x_catch_errors_with_handler (dpy, NULL, NULL);
9645 /* Undo the last x_catch_errors call.
9646 DPY should be the display that was passed to x_catch_errors.
9648 This version should be used only if the immediately preceding
9649 X-protocol-related thing was x_check_errors or x_had_error_p, both
9650 of which issue XSync calls, so we don't need to re-sync here. */
9652 void
9653 x_uncatch_errors_after_check (void)
9655 struct x_error_message_stack *tmp;
9657 block_input ();
9658 tmp = x_error_message;
9659 x_error_message = x_error_message->prev;
9660 xfree (tmp);
9661 unblock_input ();
9664 /* Undo the last x_catch_errors call.
9665 DPY should be the display that was passed to x_catch_errors. */
9667 void
9668 x_uncatch_errors (void)
9670 struct x_error_message_stack *tmp;
9672 block_input ();
9674 /* The display may have been closed before this function is called.
9675 Check if it is still open before calling XSync. */
9676 if (x_display_info_for_display (x_error_message->dpy) != 0)
9677 XSync (x_error_message->dpy, False);
9679 tmp = x_error_message;
9680 x_error_message = x_error_message->prev;
9681 xfree (tmp);
9682 unblock_input ();
9685 /* If any X protocol errors have arrived since the last call to
9686 x_catch_errors or x_check_errors, signal an Emacs error using
9687 sprintf (a buffer, FORMAT, the x error message text) as the text. */
9689 void
9690 x_check_errors (Display *dpy, const char *format)
9692 /* Make sure to catch any errors incurred so far. */
9693 XSync (dpy, False);
9695 if (x_error_message->string[0])
9697 char string[X_ERROR_MESSAGE_SIZE];
9698 memcpy (string, x_error_message->string, X_ERROR_MESSAGE_SIZE);
9699 x_uncatch_errors ();
9700 error (format, string);
9704 /* Nonzero if we had any X protocol errors
9705 since we did x_catch_errors on DPY. */
9707 bool
9708 x_had_errors_p (Display *dpy)
9710 /* Make sure to catch any errors incurred so far. */
9711 XSync (dpy, False);
9713 return x_error_message->string[0] != 0;
9716 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
9718 void
9719 x_clear_errors (Display *dpy)
9721 x_error_message->string[0] = 0;
9724 #if false
9725 /* See comment in unwind_to_catch why calling this is a bad
9726 * idea. --lorentey */
9727 /* Close off all unclosed x_catch_errors calls. */
9729 void
9730 x_fully_uncatch_errors (void)
9732 while (x_error_message)
9733 x_uncatch_errors ();
9735 #endif
9737 #if false
9738 static unsigned int x_wire_count;
9739 x_trace_wire (void)
9741 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
9743 #endif
9746 /************************************************************************
9747 Handling X errors
9748 ************************************************************************/
9750 /* Error message passed to x_connection_closed. */
9752 static char *error_msg;
9754 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
9755 the text of an error message that lead to the connection loss. */
9757 static _Noreturn void
9758 x_connection_closed (Display *dpy, const char *error_message, bool ioerror)
9760 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
9761 Lisp_Object frame, tail;
9762 ptrdiff_t idx = SPECPDL_INDEX ();
9764 error_msg = alloca (strlen (error_message) + 1);
9765 strcpy (error_msg, error_message);
9767 /* Inhibit redisplay while frames are being deleted. */
9768 specbind (Qinhibit_redisplay, Qt);
9770 if (dpyinfo)
9772 /* Protect display from being closed when we delete the last
9773 frame on it. */
9774 dpyinfo->reference_count++;
9775 dpyinfo->terminal->reference_count++;
9777 if (ioerror) dpyinfo->display = 0;
9779 /* First delete frames whose mini-buffers are on frames
9780 that are on the dead display. */
9781 FOR_EACH_FRAME (tail, frame)
9783 Lisp_Object minibuf_frame;
9784 minibuf_frame
9785 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
9786 if (FRAME_X_P (XFRAME (frame))
9787 && FRAME_X_P (XFRAME (minibuf_frame))
9788 && ! EQ (frame, minibuf_frame)
9789 && FRAME_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
9790 delete_frame (frame, Qnoelisp);
9793 /* Now delete all remaining frames on the dead display.
9794 We are now sure none of these is used as the mini-buffer
9795 for another frame that we need to delete. */
9796 FOR_EACH_FRAME (tail, frame)
9797 if (FRAME_X_P (XFRAME (frame))
9798 && FRAME_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
9800 /* Set this to t so that delete_frame won't get confused
9801 trying to find a replacement. */
9802 kset_default_minibuffer_frame (FRAME_KBOARD (XFRAME (frame)), Qt);
9803 delete_frame (frame, Qnoelisp);
9806 /* If DPYINFO is null, this means we didn't open the display in the
9807 first place, so don't try to close it. */
9808 if (dpyinfo)
9810 /* We can not call XtCloseDisplay here because it calls XSync.
9811 XSync inside the error handler apparently hangs Emacs. On
9812 current Xt versions, this isn't needed either. */
9813 #ifdef USE_GTK
9814 /* A long-standing GTK bug prevents proper disconnect handling
9815 (https://bugzilla.gnome.org/show_bug.cgi?id=85715). Once,
9816 the resulting Glib error message loop filled a user's disk.
9817 To avoid this, kill Emacs unconditionally on disconnect. */
9818 shut_down_emacs (0, Qnil);
9819 fprintf (stderr, "%s\n\
9820 When compiled with GTK, Emacs cannot recover from X disconnects.\n\
9821 This is a GTK bug: https://bugzilla.gnome.org/show_bug.cgi?id=85715\n\
9822 For details, see etc/PROBLEMS.\n",
9823 error_msg);
9824 emacs_abort ();
9825 #endif /* USE_GTK */
9827 /* Indicate that this display is dead. */
9828 dpyinfo->display = 0;
9830 dpyinfo->reference_count--;
9831 dpyinfo->terminal->reference_count--;
9832 if (dpyinfo->reference_count != 0)
9833 /* We have just closed all frames on this display. */
9834 emacs_abort ();
9837 Lisp_Object tmp;
9838 XSETTERMINAL (tmp, dpyinfo->terminal);
9839 Fdelete_terminal (tmp, Qnoelisp);
9843 if (terminal_list == 0)
9845 fprintf (stderr, "%s\n", error_msg);
9846 Fkill_emacs (make_number (70));
9847 /* NOTREACHED */
9850 totally_unblock_input ();
9852 unbind_to (idx, Qnil);
9853 clear_waiting_for_input ();
9855 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
9856 longjmp), because returning from this function would get us back into
9857 Xlib's code which will directly call `exit'. */
9858 error ("%s", error_msg);
9861 /* We specifically use it before defining it, so that gcc doesn't inline it,
9862 otherwise gdb doesn't know how to properly put a breakpoint on it. */
9863 static void x_error_quitter (Display *, XErrorEvent *);
9865 /* This is the first-level handler for X protocol errors.
9866 It calls x_error_quitter or x_error_catcher. */
9868 static int
9869 x_error_handler (Display *display, XErrorEvent *event)
9871 #if defined USE_GTK && defined HAVE_GTK3
9872 if ((event->error_code == BadMatch || event->error_code == BadWindow)
9873 && event->request_code == X_SetInputFocus)
9875 return 0;
9877 #endif
9879 if (x_error_message)
9880 x_error_catcher (display, event);
9881 else
9882 x_error_quitter (display, event);
9883 return 0;
9886 /* This is the usual handler for X protocol errors.
9887 It kills all frames on the display that we got the error for.
9888 If that was the only one, it prints an error message and kills Emacs. */
9890 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
9892 /* On older GCC versions, just putting x_error_quitter
9893 after x_error_handler prevents inlining into the former. */
9895 static void NO_INLINE
9896 x_error_quitter (Display *display, XErrorEvent *event)
9898 char buf[256], buf1[356];
9900 /* Ignore BadName errors. They can happen because of fonts
9901 or colors that are not defined. */
9903 if (event->error_code == BadName)
9904 return;
9906 /* Note that there is no real way portable across R3/R4 to get the
9907 original error handler. */
9909 XGetErrorText (display, event->error_code, buf, sizeof (buf));
9910 sprintf (buf1, "X protocol error: %s on protocol request %d",
9911 buf, event->request_code);
9912 x_connection_closed (display, buf1, false);
9916 /* This is the handler for X IO errors, always.
9917 It kills all frames on the display that we lost touch with.
9918 If that was the only one, it prints an error message and kills Emacs. */
9920 static _Noreturn int
9921 x_io_error_quitter (Display *display)
9923 char buf[256];
9925 snprintf (buf, sizeof buf, "Connection lost to X server '%s'",
9926 DisplayString (display));
9927 x_connection_closed (display, buf, true);
9928 assume (false);
9931 /* Changing the font of the frame. */
9933 /* Give frame F the font FONT-OBJECT as its default font. The return
9934 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
9935 frame. If it is negative, generate a new fontset from
9936 FONT-OBJECT. */
9938 Lisp_Object
9939 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
9941 struct font *font = XFONT_OBJECT (font_object);
9942 int unit, font_ascent, font_descent;
9943 #ifndef USE_X_TOOLKIT
9944 int old_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
9945 Lisp_Object fullscreen;
9946 #endif
9948 if (fontset < 0)
9949 fontset = fontset_from_font (font_object);
9950 FRAME_FONTSET (f) = fontset;
9951 if (FRAME_FONT (f) == font)
9952 /* This font is already set in frame F. There's nothing more to
9953 do. */
9954 return font_object;
9956 FRAME_FONT (f) = font;
9957 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
9958 FRAME_COLUMN_WIDTH (f) = font->average_width;
9959 get_font_ascent_descent (font, &font_ascent, &font_descent);
9960 FRAME_LINE_HEIGHT (f) = font_ascent + font_descent;
9962 #ifndef USE_X_TOOLKIT
9963 FRAME_MENU_BAR_HEIGHT (f) = FRAME_MENU_BAR_LINES (f) * FRAME_LINE_HEIGHT (f);
9964 #endif
9966 /* Compute character columns occupied by scrollbar.
9968 Don't do things differently for non-toolkit scrollbars
9969 (Bug#17163). */
9970 unit = FRAME_COLUMN_WIDTH (f);
9971 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
9972 FRAME_CONFIG_SCROLL_BAR_COLS (f)
9973 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + unit - 1) / unit;
9974 else
9975 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
9977 if (FRAME_X_WINDOW (f) != 0)
9979 /* Don't change the size of a tip frame; there's no point in
9980 doing it because it's done in Fx_show_tip, and it leads to
9981 problems because the tip frame has no widget. */
9982 if (NILP (tip_frame) || XFRAME (tip_frame) != f
9983 #ifdef USE_GTK
9984 || NILP (Fframe_parameter (tip_frame, Qtooltip))
9985 #endif
9988 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
9989 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 3,
9990 false, Qfont);
9991 #ifndef USE_X_TOOLKIT
9992 if (FRAME_MENU_BAR_HEIGHT (f) != old_menu_bar_height
9993 && !f->after_make_frame
9994 && (EQ (frame_inhibit_implied_resize, Qt)
9995 || (CONSP (frame_inhibit_implied_resize)
9996 && NILP (Fmemq (Qfont, frame_inhibit_implied_resize))))
9997 && (NILP (fullscreen = get_frame_param (f, Qfullscreen))
9998 || EQ (fullscreen, Qfullwidth)))
9999 /* If the menu bar height changes, try to keep text height
10000 constant. */
10001 adjust_frame_size
10002 (f, -1, FRAME_TEXT_HEIGHT (f) + FRAME_MENU_BAR_HEIGHT (f)
10003 - old_menu_bar_height, 1, false, Qfont);
10004 #endif /* USE_X_TOOLKIT */
10008 #ifdef HAVE_X_I18N
10009 if (FRAME_XIC (f)
10010 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
10012 block_input ();
10013 xic_set_xfontset (f, SSDATA (fontset_ascii (fontset)));
10014 unblock_input ();
10016 #endif
10018 return font_object;
10022 /***********************************************************************
10023 X Input Methods
10024 ***********************************************************************/
10026 #ifdef HAVE_X_I18N
10028 #ifdef HAVE_X11R6
10030 /* XIM destroy callback function, which is called whenever the
10031 connection to input method XIM dies. CLIENT_DATA contains a
10032 pointer to the x_display_info structure corresponding to XIM. */
10034 static void
10035 xim_destroy_callback (XIM xim, XPointer client_data, XPointer call_data)
10037 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
10038 Lisp_Object frame, tail;
10040 block_input ();
10042 /* No need to call XDestroyIC.. */
10043 FOR_EACH_FRAME (tail, frame)
10045 struct frame *f = XFRAME (frame);
10046 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
10048 FRAME_XIC (f) = NULL;
10049 xic_free_xfontset (f);
10053 /* No need to call XCloseIM. */
10054 dpyinfo->xim = NULL;
10055 XFree (dpyinfo->xim_styles);
10056 unblock_input ();
10059 #endif /* HAVE_X11R6 */
10061 /* Open the connection to the XIM server on display DPYINFO.
10062 RESOURCE_NAME is the resource name Emacs uses. */
10064 static void
10065 xim_open_dpy (struct x_display_info *dpyinfo, char *resource_name)
10067 XIM xim;
10069 #ifdef HAVE_XIM
10070 if (use_xim)
10072 if (dpyinfo->xim)
10073 XCloseIM (dpyinfo->xim);
10074 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
10075 emacs_class);
10076 dpyinfo->xim = xim;
10078 if (xim)
10080 #ifdef HAVE_X11R6
10081 XIMCallback destroy;
10082 #endif
10084 /* Get supported styles and XIM values. */
10085 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
10087 #ifdef HAVE_X11R6
10088 destroy.callback = xim_destroy_callback;
10089 destroy.client_data = (XPointer)dpyinfo;
10090 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
10091 #endif
10095 else
10096 #endif /* HAVE_XIM */
10097 dpyinfo->xim = NULL;
10101 #ifdef HAVE_X11R6_XIM
10103 /* XIM instantiate callback function, which is called whenever an XIM
10104 server is available. DISPLAY is the display of the XIM.
10105 CLIENT_DATA contains a pointer to an xim_inst_t structure created
10106 when the callback was registered. */
10108 static void
10109 xim_instantiate_callback (Display *display, XPointer client_data, XPointer call_data)
10111 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
10112 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
10114 /* We don't support multiple XIM connections. */
10115 if (dpyinfo->xim)
10116 return;
10118 xim_open_dpy (dpyinfo, xim_inst->resource_name);
10120 /* Create XIC for the existing frames on the same display, as long
10121 as they have no XIC. */
10122 if (dpyinfo->xim && dpyinfo->reference_count > 0)
10124 Lisp_Object tail, frame;
10126 block_input ();
10127 FOR_EACH_FRAME (tail, frame)
10129 struct frame *f = XFRAME (frame);
10131 if (FRAME_X_P (f)
10132 && FRAME_DISPLAY_INFO (f) == xim_inst->dpyinfo)
10133 if (FRAME_XIC (f) == NULL)
10135 create_frame_xic (f);
10136 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
10137 xic_set_statusarea (f);
10138 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
10140 struct window *w = XWINDOW (f->selected_window);
10141 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
10146 unblock_input ();
10150 #endif /* HAVE_X11R6_XIM */
10153 /* Open a connection to the XIM server on display DPYINFO.
10154 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
10155 connection only at the first time. On X11R6, open the connection
10156 in the XIM instantiate callback function. */
10158 static void
10159 xim_initialize (struct x_display_info *dpyinfo, char *resource_name)
10161 dpyinfo->xim = NULL;
10162 #ifdef HAVE_XIM
10163 if (use_xim)
10165 #ifdef HAVE_X11R6_XIM
10166 struct xim_inst_t *xim_inst = xmalloc (sizeof *xim_inst);
10167 Bool ret;
10169 dpyinfo->xim_callback_data = xim_inst;
10170 xim_inst->dpyinfo = dpyinfo;
10171 xim_inst->resource_name = xstrdup (resource_name);
10172 ret = XRegisterIMInstantiateCallback
10173 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
10174 emacs_class, xim_instantiate_callback,
10175 /* This is XPointer in XFree86 but (XPointer *)
10176 on Tru64, at least, hence the configure test. */
10177 (XRegisterIMInstantiateCallback_arg6) xim_inst);
10178 eassert (ret == True);
10179 #else /* not HAVE_X11R6_XIM */
10180 xim_open_dpy (dpyinfo, resource_name);
10181 #endif /* not HAVE_X11R6_XIM */
10183 #endif /* HAVE_XIM */
10187 /* Close the connection to the XIM server on display DPYINFO. */
10189 static void
10190 xim_close_dpy (struct x_display_info *dpyinfo)
10192 #ifdef HAVE_XIM
10193 if (use_xim)
10195 #ifdef HAVE_X11R6_XIM
10196 struct xim_inst_t *xim_inst = dpyinfo->xim_callback_data;
10198 if (dpyinfo->display)
10200 Bool ret = XUnregisterIMInstantiateCallback
10201 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
10202 emacs_class, xim_instantiate_callback,
10203 (XRegisterIMInstantiateCallback_arg6) xim_inst);
10204 eassert (ret == True);
10206 xfree (xim_inst->resource_name);
10207 xfree (xim_inst);
10208 #endif /* HAVE_X11R6_XIM */
10209 if (dpyinfo->display)
10210 XCloseIM (dpyinfo->xim);
10211 dpyinfo->xim = NULL;
10212 XFree (dpyinfo->xim_styles);
10214 #endif /* HAVE_XIM */
10217 #endif /* not HAVE_X11R6_XIM */
10221 /* Calculate the absolute position in frame F
10222 from its current recorded position values and gravity. */
10224 static void
10225 x_calc_absolute_position (struct frame *f)
10227 int flags = f->size_hint_flags;
10228 struct frame *p = FRAME_PARENT_FRAME (f);
10230 /* We have nothing to do if the current position
10231 is already for the top-left corner. */
10232 if (! ((flags & XNegative) || (flags & YNegative)))
10233 return;
10235 /* Treat negative positions as relative to the leftmost bottommost
10236 position that fits on the screen. */
10237 if ((flags & XNegative) && (f->left_pos <= 0))
10239 int width = FRAME_PIXEL_WIDTH (f);
10241 /* A frame that has been visible at least once should have outer
10242 edges. */
10243 if (f->output_data.x->has_been_visible && !p)
10245 Lisp_Object frame;
10246 Lisp_Object edges = Qnil;
10248 XSETFRAME (frame, f);
10249 edges = Fx_frame_edges (frame, Qouter_edges);
10250 if (!NILP (edges))
10251 width = (XINT (Fnth (make_number (2), edges))
10252 - XINT (Fnth (make_number (0), edges)));
10255 if (p)
10256 f->left_pos = (FRAME_PIXEL_WIDTH (p) - width - 2 * f->border_width
10257 + f->left_pos);
10258 else
10259 f->left_pos = (x_display_pixel_width (FRAME_DISPLAY_INFO (f))
10260 - width + f->left_pos);
10264 if ((flags & YNegative) && (f->top_pos <= 0))
10266 int height = FRAME_PIXEL_HEIGHT (f);
10268 #if defined USE_X_TOOLKIT && defined USE_MOTIF
10269 /* Something is fishy here. When using Motif, starting Emacs with
10270 `-g -0-0', the frame appears too low by a few pixels.
10272 This seems to be so because initially, while Emacs is starting,
10273 the column widget's height and the frame's pixel height are
10274 different. The column widget's height is the right one. In
10275 later invocations, when Emacs is up, the frame's pixel height
10276 is right, though.
10278 It's not obvious where the initial small difference comes from.
10279 2000-12-01, gerd. */
10281 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
10282 #endif
10284 if (f->output_data.x->has_been_visible && !p)
10286 Lisp_Object frame;
10287 Lisp_Object edges = Qnil;
10289 XSETFRAME (frame, f);
10290 if (NILP (edges))
10291 edges = Fx_frame_edges (frame, Qouter_edges);
10292 if (!NILP (edges))
10293 height = (XINT (Fnth (make_number (3), edges))
10294 - XINT (Fnth (make_number (1), edges)));
10297 if (p)
10298 f->top_pos = (FRAME_PIXEL_HEIGHT (p) - height - 2 * f->border_width
10299 + f->top_pos);
10300 else
10301 f->top_pos = (x_display_pixel_height (FRAME_DISPLAY_INFO (f))
10302 - height + f->top_pos);
10305 /* The left_pos and top_pos
10306 are now relative to the top and left screen edges,
10307 so the flags should correspond. */
10308 f->size_hint_flags &= ~ (XNegative | YNegative);
10311 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
10312 to really change the position, and 0 when calling from
10313 x_make_frame_visible (in that case, XOFF and YOFF are the current
10314 position values). It is -1 when calling from x_set_frame_parameters,
10315 which means, do adjust for borders but don't change the gravity. */
10317 void
10318 x_set_offset (struct frame *f, register int xoff, register int yoff, int change_gravity)
10320 int modified_top, modified_left;
10321 #ifdef USE_GTK
10322 int scale = xg_get_scale (f);
10323 #endif
10325 if (change_gravity > 0)
10327 f->top_pos = yoff;
10328 f->left_pos = xoff;
10329 f->size_hint_flags &= ~ (XNegative | YNegative);
10330 if (xoff < 0)
10331 f->size_hint_flags |= XNegative;
10332 if (yoff < 0)
10333 f->size_hint_flags |= YNegative;
10334 f->win_gravity = NorthWestGravity;
10337 x_calc_absolute_position (f);
10339 block_input ();
10340 x_wm_set_size_hint (f, 0, false);
10342 #ifdef USE_GTK
10343 if (x_gtk_use_window_move)
10345 /* When a position change was requested and the outer GTK widget
10346 has been realized already, leave it to gtk_window_move to
10347 DTRT and return. Used for Bug#25851 and Bug#25943. Convert
10348 from X pixels to GTK scaled pixels. */
10349 if (change_gravity != 0 && FRAME_GTK_OUTER_WIDGET (f))
10350 gtk_window_move (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
10351 f->left_pos / scale, f->top_pos / scale);
10352 unblock_input ();
10353 return;
10355 #endif /* USE_GTK */
10357 modified_left = f->left_pos;
10358 modified_top = f->top_pos;
10360 if (change_gravity != 0 && FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
10362 /* Some WMs (twm, wmaker at least) has an offset that is smaller
10363 than the WM decorations. So we use the calculated offset instead
10364 of the WM decoration sizes here (x/y_pixels_outer_diff). */
10365 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
10366 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
10369 #ifdef USE_GTK
10370 /* Make sure we adjust for possible scaling. */
10371 gtk_window_move (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
10372 modified_left / scale, modified_top / scale);
10373 #else
10374 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10375 modified_left, modified_top);
10376 #endif
10378 x_sync_with_move (f, f->left_pos, f->top_pos,
10379 FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN);
10381 /* change_gravity is non-zero when this function is called from Lisp to
10382 programmatically move a frame. In that case, we call
10383 x_check_expected_move to discover if we have a "Type A" or "Type B"
10384 window manager, and, for a "Type A" window manager, adjust the position
10385 of the frame.
10387 We call x_check_expected_move if a programmatic move occurred, and
10388 either the window manager type (A/B) is unknown or it is Type A but we
10389 need to compute the top/left offset adjustment for this frame. */
10391 if (change_gravity != 0
10392 && !FRAME_PARENT_FRAME (f)
10393 && (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
10394 || (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
10395 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
10396 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
10397 x_check_expected_move (f, modified_left, modified_top);
10399 unblock_input ();
10402 /* Return true if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
10403 on the root window for frame F contains ATOMNAME.
10404 This is how a WM check shall be done according to the Window Manager
10405 Specification/Extended Window Manager Hints at
10406 http://freedesktop.org/wiki/Specifications/wm-spec. */
10408 bool
10409 x_wm_supports (struct frame *f, Atom want_atom)
10411 Atom actual_type;
10412 unsigned long actual_size, bytes_remaining;
10413 int i, rc, actual_format;
10414 bool ret;
10415 Window wmcheck_window;
10416 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10417 Window target_window = dpyinfo->root_window;
10418 int max_len = 65536;
10419 Display *dpy = FRAME_X_DISPLAY (f);
10420 unsigned char *tmp_data = NULL;
10421 Atom target_type = XA_WINDOW;
10423 block_input ();
10425 x_catch_errors (dpy);
10426 rc = XGetWindowProperty (dpy, target_window,
10427 dpyinfo->Xatom_net_supporting_wm_check,
10428 0, max_len, False, target_type,
10429 &actual_type, &actual_format, &actual_size,
10430 &bytes_remaining, &tmp_data);
10432 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
10434 if (tmp_data) XFree (tmp_data);
10435 x_uncatch_errors ();
10436 unblock_input ();
10437 return false;
10440 wmcheck_window = *(Window *) tmp_data;
10441 XFree (tmp_data);
10443 /* Check if window exists. */
10444 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
10445 if (x_had_errors_p (dpy))
10447 x_uncatch_errors_after_check ();
10448 unblock_input ();
10449 return false;
10452 if (dpyinfo->net_supported_window != wmcheck_window)
10454 /* Window changed, reload atoms */
10455 if (dpyinfo->net_supported_atoms != NULL)
10456 XFree (dpyinfo->net_supported_atoms);
10457 dpyinfo->net_supported_atoms = NULL;
10458 dpyinfo->nr_net_supported_atoms = 0;
10459 dpyinfo->net_supported_window = 0;
10461 target_type = XA_ATOM;
10462 tmp_data = NULL;
10463 rc = XGetWindowProperty (dpy, target_window,
10464 dpyinfo->Xatom_net_supported,
10465 0, max_len, False, target_type,
10466 &actual_type, &actual_format, &actual_size,
10467 &bytes_remaining, &tmp_data);
10469 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
10471 if (tmp_data) XFree (tmp_data);
10472 x_uncatch_errors ();
10473 unblock_input ();
10474 return false;
10477 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
10478 dpyinfo->nr_net_supported_atoms = actual_size;
10479 dpyinfo->net_supported_window = wmcheck_window;
10482 ret = false;
10484 for (i = 0; !ret && i < dpyinfo->nr_net_supported_atoms; ++i)
10485 ret = dpyinfo->net_supported_atoms[i] == want_atom;
10487 x_uncatch_errors ();
10488 unblock_input ();
10490 return ret;
10493 static void
10494 set_wm_state (Lisp_Object frame, bool add, Atom atom, Atom value)
10496 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (XFRAME (frame));
10498 x_send_client_event (frame, make_number (0), frame,
10499 dpyinfo->Xatom_net_wm_state,
10500 make_number (32),
10501 /* 1 = add, 0 = remove */
10502 Fcons
10503 (make_number (add),
10504 Fcons
10505 (make_fixnum_or_float (atom),
10506 (value != 0
10507 ? list1 (make_fixnum_or_float (value))
10508 : Qnil))));
10511 void
10512 x_set_sticky (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
10514 Lisp_Object frame;
10515 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10517 XSETFRAME (frame, f);
10519 set_wm_state (frame, !NILP (new_value),
10520 dpyinfo->Xatom_net_wm_state_sticky, None);
10524 * x_set_skip_taskbar:
10526 * Set frame F's `skip-taskbar' parameter. If non-nil, this should
10527 * remove F's icon from the taskbar associated with the display of F's
10528 * window-system window and inhibit switching to F's window via
10529 * <Alt>-<TAB>. If nil, lift these restrictions.
10531 * Some window managers may not honor this parameter.
10533 void
10534 x_set_skip_taskbar (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
10536 if (!EQ (new_value, old_value))
10538 #ifdef USE_GTK
10539 xg_set_skip_taskbar (f, new_value);
10540 #else
10541 Lisp_Object frame;
10542 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10544 XSETFRAME (frame, f);
10545 set_wm_state (frame, !NILP (new_value),
10546 dpyinfo->Xatom_net_wm_state_skip_taskbar, None);
10547 #endif /* USE_GTK */
10548 FRAME_SKIP_TASKBAR (f) = !NILP (new_value);
10553 * x_set_z_group:
10555 * Set frame F's `z-group' parameter. If `above', F's window-system
10556 * window is displayed above all windows that do not have the `above'
10557 * property set. If nil, F's window is shown below all windows that
10558 * have the `above' property set and above all windows that have the
10559 * `below' property set. If `below', F's window is displayed below all
10560 * windows that do not have the `below' property set.
10562 * Some window managers may not honor this parameter.
10564 void
10565 x_set_z_group (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
10567 /* We don't care about old_value. The window manager might have
10568 reset the value without telling us. */
10569 Lisp_Object frame;
10570 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10572 XSETFRAME (frame, f);
10574 if (NILP (new_value))
10576 set_wm_state (frame, false,
10577 dpyinfo->Xatom_net_wm_state_above, None);
10578 set_wm_state (frame, false,
10579 dpyinfo->Xatom_net_wm_state_below, None);
10580 FRAME_Z_GROUP (f) = z_group_none;
10582 else if (EQ (new_value, Qabove))
10584 set_wm_state (frame, true,
10585 dpyinfo->Xatom_net_wm_state_above, None);
10586 set_wm_state (frame, false,
10587 dpyinfo->Xatom_net_wm_state_below, None);
10588 FRAME_Z_GROUP (f) = z_group_above;
10590 else if (EQ (new_value, Qbelow))
10592 set_wm_state (frame, false,
10593 dpyinfo->Xatom_net_wm_state_above, None);
10594 set_wm_state (frame, true,
10595 dpyinfo->Xatom_net_wm_state_below, None);
10596 FRAME_Z_GROUP (f) = z_group_below;
10598 else if (EQ (new_value, Qabove_suspended))
10600 set_wm_state (frame, false,
10601 dpyinfo->Xatom_net_wm_state_above, None);
10602 FRAME_Z_GROUP (f) = z_group_above_suspended;
10604 else
10605 error ("Invalid z-group specification");
10609 /* Return the current _NET_WM_STATE.
10610 SIZE_STATE is set to one of the FULLSCREEN_* values.
10611 Set *STICKY to the sticky state.
10613 Return true iff we are not hidden. */
10615 static bool
10616 get_current_wm_state (struct frame *f,
10617 Window window,
10618 int *size_state,
10619 bool *sticky)
10621 unsigned long actual_size;
10622 int i;
10623 bool is_hidden = false;
10624 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10625 long max_len = 65536;
10626 Atom target_type = XA_ATOM;
10627 /* If XCB is available, we can avoid three XSync calls. */
10628 #ifdef USE_XCB
10629 xcb_get_property_cookie_t prop_cookie;
10630 xcb_get_property_reply_t *prop;
10631 xcb_atom_t *reply_data;
10632 #else
10633 Display *dpy = FRAME_X_DISPLAY (f);
10634 unsigned long bytes_remaining;
10635 int rc, actual_format;
10636 Atom actual_type;
10637 unsigned char *tmp_data = NULL;
10638 Atom *reply_data;
10639 #endif
10641 *sticky = false;
10642 *size_state = FULLSCREEN_NONE;
10644 block_input ();
10646 #ifdef USE_XCB
10647 prop_cookie = xcb_get_property (dpyinfo->xcb_connection, 0, window,
10648 dpyinfo->Xatom_net_wm_state,
10649 target_type, 0, max_len);
10650 prop = xcb_get_property_reply (dpyinfo->xcb_connection, prop_cookie, NULL);
10651 if (prop && prop->type == target_type)
10653 int actual_bytes = xcb_get_property_value_length (prop);
10654 eassume (0 <= actual_bytes);
10655 actual_size = actual_bytes / sizeof *reply_data;
10656 reply_data = xcb_get_property_value (prop);
10658 else
10660 actual_size = 0;
10661 is_hidden = FRAME_ICONIFIED_P (f);
10663 #else
10664 x_catch_errors (dpy);
10665 rc = XGetWindowProperty (dpy, window, dpyinfo->Xatom_net_wm_state,
10666 0, max_len, False, target_type,
10667 &actual_type, &actual_format, &actual_size,
10668 &bytes_remaining, &tmp_data);
10670 if (rc == Success && actual_type == target_type && ! x_had_errors_p (dpy))
10671 reply_data = (Atom *) tmp_data;
10672 else
10674 actual_size = 0;
10675 is_hidden = FRAME_ICONIFIED_P (f);
10678 x_uncatch_errors ();
10679 #endif
10681 for (i = 0; i < actual_size; ++i)
10683 Atom a = reply_data[i];
10684 if (a == dpyinfo->Xatom_net_wm_state_hidden)
10685 is_hidden = true;
10686 else if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
10688 if (*size_state == FULLSCREEN_HEIGHT)
10689 *size_state = FULLSCREEN_MAXIMIZED;
10690 else
10691 *size_state = FULLSCREEN_WIDTH;
10693 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
10695 if (*size_state == FULLSCREEN_WIDTH)
10696 *size_state = FULLSCREEN_MAXIMIZED;
10697 else
10698 *size_state = FULLSCREEN_HEIGHT;
10700 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen)
10701 *size_state = FULLSCREEN_BOTH;
10702 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
10703 *sticky = true;
10706 #ifdef USE_XCB
10707 free (prop);
10708 #else
10709 if (tmp_data) XFree (tmp_data);
10710 #endif
10712 unblock_input ();
10713 return ! is_hidden;
10716 /* Do fullscreen as specified in extended window manager hints */
10718 static bool
10719 do_ewmh_fullscreen (struct frame *f)
10721 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10722 bool have_net_atom = x_wm_supports (f, dpyinfo->Xatom_net_wm_state);
10723 int cur;
10724 bool dummy;
10726 get_current_wm_state (f, FRAME_OUTER_WINDOW (f), &cur, &dummy);
10728 /* Some window managers don't say they support _NET_WM_STATE, but they do say
10729 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
10730 if (!have_net_atom)
10731 have_net_atom = x_wm_supports (f, dpyinfo->Xatom_net_wm_state_fullscreen);
10733 if (have_net_atom && cur != f->want_fullscreen)
10735 Lisp_Object frame;
10737 XSETFRAME (frame, f);
10739 /* Keep number of calls to set_wm_state as low as possible.
10740 Some window managers, or possible Gtk+, hangs when too many
10741 are sent at once. */
10742 switch (f->want_fullscreen)
10744 case FULLSCREEN_BOTH:
10745 if (cur != FULLSCREEN_BOTH)
10746 set_wm_state (frame, true, dpyinfo->Xatom_net_wm_state_fullscreen,
10747 None);
10748 break;
10749 case FULLSCREEN_WIDTH:
10750 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_MAXIMIZED)
10752 set_wm_state (frame, false,
10753 dpyinfo->Xatom_net_wm_state_maximized_horz,
10754 dpyinfo->Xatom_net_wm_state_maximized_vert);
10755 set_wm_state (frame, true,
10756 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10758 else
10760 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_HEIGHT
10761 || cur == FULLSCREEN_MAXIMIZED)
10762 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10763 dpyinfo->Xatom_net_wm_state_maximized_vert);
10764 if (cur != FULLSCREEN_MAXIMIZED || x_frame_normalize_before_maximize)
10765 set_wm_state (frame, true,
10766 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10768 break;
10769 case FULLSCREEN_HEIGHT:
10770 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_MAXIMIZED)
10772 set_wm_state (frame, false,
10773 dpyinfo->Xatom_net_wm_state_maximized_horz,
10774 dpyinfo->Xatom_net_wm_state_maximized_vert);
10775 set_wm_state (frame, true,
10776 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10778 else
10780 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_WIDTH
10781 || cur == FULLSCREEN_MAXIMIZED)
10782 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10783 dpyinfo->Xatom_net_wm_state_maximized_horz);
10784 if (cur != FULLSCREEN_MAXIMIZED || x_frame_normalize_before_maximize)
10785 set_wm_state (frame, true,
10786 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10788 break;
10789 case FULLSCREEN_MAXIMIZED:
10790 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_BOTH)
10792 set_wm_state (frame, false,
10793 dpyinfo->Xatom_net_wm_state_fullscreen, None);
10794 set_wm_state (frame, true,
10795 dpyinfo->Xatom_net_wm_state_maximized_horz,
10796 dpyinfo->Xatom_net_wm_state_maximized_vert);
10798 else if (x_frame_normalize_before_maximize && cur == FULLSCREEN_WIDTH)
10800 set_wm_state (frame, false,
10801 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10802 set_wm_state (frame, true,
10803 dpyinfo->Xatom_net_wm_state_maximized_horz,
10804 dpyinfo->Xatom_net_wm_state_maximized_vert);
10806 else if (x_frame_normalize_before_maximize && cur == FULLSCREEN_HEIGHT)
10808 set_wm_state (frame, false,
10809 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10810 set_wm_state (frame, true,
10811 dpyinfo->Xatom_net_wm_state_maximized_horz,
10812 dpyinfo->Xatom_net_wm_state_maximized_vert);
10814 else
10816 if (cur == FULLSCREEN_BOTH)
10817 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10818 None);
10819 else if (cur == FULLSCREEN_HEIGHT)
10820 set_wm_state (frame, true,
10821 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10822 else if (cur == FULLSCREEN_WIDTH)
10823 set_wm_state (frame, true, None,
10824 dpyinfo->Xatom_net_wm_state_maximized_vert);
10825 else
10826 set_wm_state (frame, true,
10827 dpyinfo->Xatom_net_wm_state_maximized_horz,
10828 dpyinfo->Xatom_net_wm_state_maximized_vert);
10830 break;
10831 case FULLSCREEN_NONE:
10832 if (cur == FULLSCREEN_BOTH)
10833 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10834 None);
10835 else
10836 set_wm_state (frame, false,
10837 dpyinfo->Xatom_net_wm_state_maximized_horz,
10838 dpyinfo->Xatom_net_wm_state_maximized_vert);
10841 f->want_fullscreen = FULLSCREEN_NONE;
10845 return have_net_atom;
10848 static void
10849 XTfullscreen_hook (struct frame *f)
10851 if (FRAME_VISIBLE_P (f))
10853 block_input ();
10854 x_check_fullscreen (f);
10855 x_sync (f);
10856 unblock_input ();
10861 static bool
10862 x_handle_net_wm_state (struct frame *f, const XPropertyEvent *event)
10864 int value = FULLSCREEN_NONE;
10865 Lisp_Object lval;
10866 bool sticky = false;
10867 bool not_hidden = get_current_wm_state (f, event->window, &value, &sticky);
10869 lval = Qnil;
10870 switch (value)
10872 case FULLSCREEN_WIDTH:
10873 lval = Qfullwidth;
10874 break;
10875 case FULLSCREEN_HEIGHT:
10876 lval = Qfullheight;
10877 break;
10878 case FULLSCREEN_BOTH:
10879 lval = Qfullboth;
10880 break;
10881 case FULLSCREEN_MAXIMIZED:
10882 lval = Qmaximized;
10883 break;
10886 frame_size_history_add
10887 (f, Qx_handle_net_wm_state, 0, 0,
10888 list2 (get_frame_param (f, Qfullscreen), lval));
10890 store_frame_param (f, Qfullscreen, lval);
10891 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
10893 return not_hidden;
10896 /* Check if we need to resize the frame due to a fullscreen request.
10897 If so needed, resize the frame. */
10898 static void
10899 x_check_fullscreen (struct frame *f)
10901 Lisp_Object lval = Qnil;
10903 if (do_ewmh_fullscreen (f))
10904 return;
10906 if (f->output_data.x->parent_desc != FRAME_DISPLAY_INFO (f)->root_window)
10907 return; /* Only fullscreen without WM or with EWM hints (above). */
10909 /* Setting fullscreen to nil doesn't do anything. We could save the
10910 last non-fullscreen size and restore it, but it seems like a
10911 lot of work for this unusual case (no window manager running). */
10913 if (f->want_fullscreen != FULLSCREEN_NONE)
10915 int width = FRAME_PIXEL_WIDTH (f), height = FRAME_PIXEL_HEIGHT (f);
10916 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10918 switch (f->want_fullscreen)
10920 /* No difference between these two when there is no WM */
10921 case FULLSCREEN_MAXIMIZED:
10922 lval = Qmaximized;
10923 width = x_display_pixel_width (dpyinfo);
10924 height = x_display_pixel_height (dpyinfo);
10925 break;
10926 case FULLSCREEN_BOTH:
10927 lval = Qfullboth;
10928 width = x_display_pixel_width (dpyinfo);
10929 height = x_display_pixel_height (dpyinfo);
10930 break;
10931 case FULLSCREEN_WIDTH:
10932 lval = Qfullwidth;
10933 width = x_display_pixel_width (dpyinfo);
10934 height = height + FRAME_MENUBAR_HEIGHT (f);
10935 break;
10936 case FULLSCREEN_HEIGHT:
10937 lval = Qfullheight;
10938 height = x_display_pixel_height (dpyinfo);
10939 break;
10940 default:
10941 emacs_abort ();
10944 frame_size_history_add
10945 (f, Qx_check_fullscreen, width, height, Qnil);
10947 x_wm_set_size_hint (f, 0, false);
10949 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10950 width, height);
10952 if (FRAME_VISIBLE_P (f))
10953 x_wait_for_event (f, ConfigureNotify);
10954 else
10956 change_frame_size (f, width, height - FRAME_MENUBAR_HEIGHT (f),
10957 false, true, false, true);
10958 x_sync (f);
10962 /* `x_net_wm_state' might have reset the fullscreen frame parameter,
10963 restore it. */
10964 store_frame_param (f, Qfullscreen, lval);
10967 /* This function is called by x_set_offset to determine whether the window
10968 manager interfered with the positioning of the frame. Type A window
10969 managers position the surrounding window manager decorations a small
10970 amount above and left of the user-supplied position. Type B window
10971 managers position the surrounding window manager decorations at the
10972 user-specified position. If we detect a Type A window manager, we
10973 compensate by moving the window right and down by the proper amount. */
10975 static void
10976 x_check_expected_move (struct frame *f, int expected_left, int expected_top)
10978 int current_left = 0, current_top = 0;
10980 /* x_real_positions returns the left and top offsets of the outermost
10981 window manager window around the frame. */
10983 x_real_positions (f, &current_left, &current_top);
10985 if (current_left != expected_left || current_top != expected_top)
10987 /* It's a "Type A" window manager. */
10989 int adjusted_left;
10990 int adjusted_top;
10992 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
10993 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
10994 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
10996 /* Now fix the mispositioned frame's location. */
10998 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
10999 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
11001 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11002 adjusted_left, adjusted_top);
11004 x_sync_with_move (f, expected_left, expected_top, false);
11006 else
11007 /* It's a "Type B" window manager. We don't have to adjust the
11008 frame's position. */
11010 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
11014 /* Wait for XGetGeometry to return up-to-date position information for a
11015 recently-moved frame. Call this immediately after calling XMoveWindow.
11016 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
11017 frame has been moved to, so we use a fuzzy position comparison instead
11018 of an exact comparison. */
11020 static void
11021 x_sync_with_move (struct frame *f, int left, int top, bool fuzzy)
11023 int count = 0;
11025 while (count++ < 50)
11027 int current_left = 0, current_top = 0;
11029 /* In theory, this call to XSync only needs to happen once, but in
11030 practice, it doesn't seem to work, hence the need for the surrounding
11031 loop. */
11033 XSync (FRAME_X_DISPLAY (f), False);
11034 x_real_positions (f, &current_left, &current_top);
11036 if (fuzzy)
11038 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
11039 pixels. */
11041 if (eabs (current_left - left) <= 10
11042 && eabs (current_top - top) <= 40)
11043 return;
11045 else if (current_left == left && current_top == top)
11046 return;
11049 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
11050 will then return up-to-date position info. */
11052 wait_reading_process_output (0, 500000000, 0, false, Qnil, NULL, 0);
11056 /* Wait for an event on frame F matching EVENTTYPE. */
11057 void
11058 x_wait_for_event (struct frame *f, int eventtype)
11060 if (!FLOATP (Vx_wait_for_event_timeout))
11061 return;
11063 int level = interrupt_input_blocked;
11064 fd_set fds;
11065 struct timespec tmo, tmo_at, time_now;
11066 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
11068 f->wait_event_type = eventtype;
11070 /* Default timeout is 0.1 second. Hopefully not noticeable. */
11071 double timeout = XFLOAT_DATA (Vx_wait_for_event_timeout);
11072 time_t timeout_seconds = (time_t) timeout;
11073 tmo = make_timespec
11074 (timeout_seconds, (long int) ((timeout - timeout_seconds)
11075 * 1000 * 1000 * 1000));
11076 tmo_at = timespec_add (current_timespec (), tmo);
11078 while (f->wait_event_type)
11080 pending_signals = true;
11081 totally_unblock_input ();
11082 /* XTread_socket is called after unblock. */
11083 block_input ();
11084 interrupt_input_blocked = level;
11086 FD_ZERO (&fds);
11087 FD_SET (fd, &fds);
11089 time_now = current_timespec ();
11090 if (timespec_cmp (tmo_at, time_now) < 0)
11091 break;
11093 tmo = timespec_sub (tmo_at, time_now);
11094 if (pselect (fd + 1, &fds, NULL, NULL, &tmo, NULL) == 0)
11095 break; /* Timeout */
11098 f->wait_event_type = 0;
11102 /* Change the size of frame F's X window to WIDTH/HEIGHT in the case F
11103 doesn't have a widget. If CHANGE_GRAVITY, change to
11104 top-left-corner window gravity for this size change and subsequent
11105 size changes. Otherwise leave the window gravity unchanged. */
11107 static void
11108 x_set_window_size_1 (struct frame *f, bool change_gravity,
11109 int width, int height)
11111 int pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
11112 int pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
11113 int old_width = FRAME_PIXEL_WIDTH (f);
11114 int old_height = FRAME_PIXEL_HEIGHT (f);
11115 Lisp_Object fullscreen = get_frame_param (f, Qfullscreen);
11117 if (change_gravity)
11118 f->win_gravity = NorthWestGravity;
11119 x_wm_set_size_hint (f, 0, false);
11121 /* When the frame is fullheight and we only want to change the width
11122 or it is fullwidth and we only want to change the height we should
11123 be able to preserve the fullscreen property. However, due to the
11124 fact that we have to send a resize request anyway, the window
11125 manager will abolish it. At least the respective size should
11126 remain unchanged but giving the frame back its normal size will
11127 be broken ... */
11128 if (EQ (fullscreen, Qfullwidth) && width == FRAME_TEXT_WIDTH (f))
11130 frame_size_history_add
11131 (f, Qx_set_window_size_1, width, height,
11132 list2 (make_number (old_height),
11133 make_number (pixelheight + FRAME_MENUBAR_HEIGHT (f))));
11135 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11136 old_width, pixelheight + FRAME_MENUBAR_HEIGHT (f));
11138 else if (EQ (fullscreen, Qfullheight) && height == FRAME_TEXT_HEIGHT (f))
11140 frame_size_history_add
11141 (f, Qx_set_window_size_2, width, height,
11142 list2 (make_number (old_width), make_number (pixelwidth)));
11144 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11145 pixelwidth, old_height);
11148 else
11150 frame_size_history_add
11151 (f, Qx_set_window_size_3, width, height,
11152 list3 (make_number (pixelwidth + FRAME_TOOLBAR_WIDTH (f)),
11153 make_number (pixelheight + FRAME_TOOLBAR_HEIGHT (f)
11154 + FRAME_MENUBAR_HEIGHT (f)),
11155 make_number (FRAME_MENUBAR_HEIGHT (f))));
11157 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11158 pixelwidth, pixelheight + FRAME_MENUBAR_HEIGHT (f));
11159 fullscreen = Qnil;
11164 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11165 receive in the ConfigureNotify event; if we get what we asked
11166 for, then the event won't cause the screen to become garbaged, so
11167 we have to make sure to do it here. */
11168 SET_FRAME_GARBAGED (f);
11170 /* Now, strictly speaking, we can't be sure that this is accurate,
11171 but the window manager will get around to dealing with the size
11172 change request eventually, and we'll hear how it went when the
11173 ConfigureNotify event gets here.
11175 We could just not bother storing any of this information here,
11176 and let the ConfigureNotify event set everything up, but that
11177 might be kind of confusing to the Lisp code, since size changes
11178 wouldn't be reported in the frame parameters until some random
11179 point in the future when the ConfigureNotify event arrives.
11181 Pass true for DELAY since we can't run Lisp code inside of
11182 a BLOCK_INPUT. */
11184 /* But the ConfigureNotify may in fact never arrive, and then this is
11185 not right if the frame is visible. Instead wait (with timeout)
11186 for the ConfigureNotify. */
11187 if (FRAME_VISIBLE_P (f))
11189 x_wait_for_event (f, ConfigureNotify);
11191 if (!NILP (fullscreen))
11192 /* Try to restore fullscreen state. */
11194 store_frame_param (f, Qfullscreen, fullscreen);
11195 x_set_fullscreen (f, fullscreen, fullscreen);
11198 else
11200 change_frame_size (f, width, height, false, true, false, true);
11201 x_sync (f);
11206 /* Call this to change the size of frame F's x-window.
11207 If CHANGE_GRAVITY, change to top-left-corner window gravity
11208 for this size change and subsequent size changes.
11209 Otherwise we leave the window gravity unchanged. */
11211 void
11212 x_set_window_size (struct frame *f, bool change_gravity,
11213 int width, int height, bool pixelwise)
11215 block_input ();
11217 /* The following breaks our calculations. If it's really needed,
11218 think of something else. */
11219 #if false
11220 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
11222 int text_width, text_height;
11224 /* When the frame is maximized/fullscreen or running under for
11225 example Xmonad, x_set_window_size_1 will be a no-op.
11226 In that case, the right thing to do is extend rows/width to
11227 the current frame size. We do that first if x_set_window_size_1
11228 turns out to not be a no-op (there is no way to know).
11229 The size will be adjusted again if the frame gets a
11230 ConfigureNotify event as a result of x_set_window_size. */
11231 int pixelh = FRAME_PIXEL_HEIGHT (f);
11232 #ifdef USE_X_TOOLKIT
11233 /* The menu bar is not part of text lines. The tool bar
11234 is however. */
11235 pixelh -= FRAME_MENUBAR_HEIGHT (f);
11236 #endif
11237 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, FRAME_PIXEL_WIDTH (f));
11238 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelh);
11240 change_frame_size (f, text_width, text_height, false, true, false, true);
11242 #endif
11244 /* Pixelize width and height, if necessary. */
11245 if (! pixelwise)
11247 width = width * FRAME_COLUMN_WIDTH (f);
11248 height = height * FRAME_LINE_HEIGHT (f);
11251 #ifdef USE_GTK
11252 if (FRAME_GTK_WIDGET (f))
11253 xg_frame_set_char_size (f, width, height);
11254 else
11255 x_set_window_size_1 (f, change_gravity, width, height);
11256 #else /* not USE_GTK */
11257 x_set_window_size_1 (f, change_gravity, width, height);
11258 x_clear_under_internal_border (f);
11259 #endif /* not USE_GTK */
11261 /* If cursor was outside the new size, mark it as off. */
11262 mark_window_cursors_off (XWINDOW (f->root_window));
11264 /* Clear out any recollection of where the mouse highlighting was,
11265 since it might be in a place that's outside the new frame size.
11266 Actually checking whether it is outside is a pain in the neck,
11267 so don't try--just let the highlighting be done afresh with new size. */
11268 cancel_mouse_face (f);
11270 unblock_input ();
11272 do_pending_window_change (false);
11275 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11277 void
11278 frame_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
11280 block_input ();
11282 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11283 0, 0, 0, 0, pix_x, pix_y);
11284 unblock_input ();
11287 /* Raise frame F. */
11289 void
11290 x_raise_frame (struct frame *f)
11292 block_input ();
11293 if (FRAME_VISIBLE_P (f))
11294 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
11295 XFlush (FRAME_X_DISPLAY (f));
11296 unblock_input ();
11299 /* Lower frame F. */
11301 static void
11302 x_lower_frame (struct frame *f)
11304 if (FRAME_VISIBLE_P (f))
11306 block_input ();
11307 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
11308 XFlush (FRAME_X_DISPLAY (f));
11309 unblock_input ();
11313 /* Request focus with XEmbed */
11315 void
11316 xembed_request_focus (struct frame *f)
11318 /* See XEmbed Protocol Specification at
11319 http://freedesktop.org/wiki/Specifications/xembed-spec */
11320 if (FRAME_VISIBLE_P (f))
11321 xembed_send_message (f, CurrentTime,
11322 XEMBED_REQUEST_FOCUS, 0, 0, 0);
11325 /* Activate frame with Extended Window Manager Hints */
11327 void
11328 x_ewmh_activate_frame (struct frame *f)
11330 /* See Window Manager Specification/Extended Window Manager Hints at
11331 http://freedesktop.org/wiki/Specifications/wm-spec */
11333 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11335 if (FRAME_VISIBLE_P (f) && x_wm_supports (f, dpyinfo->Xatom_net_active_window))
11337 Lisp_Object frame;
11338 XSETFRAME (frame, f);
11339 x_send_client_event (frame, make_number (0), frame,
11340 dpyinfo->Xatom_net_active_window,
11341 make_number (32),
11342 list2i (1, dpyinfo->last_user_time));
11346 static void
11347 XTframe_raise_lower (struct frame *f, bool raise_flag)
11349 if (raise_flag)
11350 x_raise_frame (f);
11351 else
11352 x_lower_frame (f);
11355 /* XEmbed implementation. */
11357 #if defined USE_X_TOOLKIT || ! defined USE_GTK
11359 /* XEmbed implementation. */
11361 #define XEMBED_VERSION 0
11363 static void
11364 xembed_set_info (struct frame *f, enum xembed_info flags)
11366 unsigned long data[2];
11367 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11369 data[0] = XEMBED_VERSION;
11370 data[1] = flags;
11372 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11373 dpyinfo->Xatom_XEMBED_INFO, dpyinfo->Xatom_XEMBED_INFO,
11374 32, PropModeReplace, (unsigned char *) data, 2);
11376 #endif /* defined USE_X_TOOLKIT || ! defined USE_GTK */
11378 static void
11379 xembed_send_message (struct frame *f, Time t, enum xembed_message msg,
11380 long int detail, long int data1, long int data2)
11382 XEvent event;
11384 event.xclient.type = ClientMessage;
11385 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
11386 event.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_XEMBED;
11387 event.xclient.format = 32;
11388 event.xclient.data.l[0] = t;
11389 event.xclient.data.l[1] = msg;
11390 event.xclient.data.l[2] = detail;
11391 event.xclient.data.l[3] = data1;
11392 event.xclient.data.l[4] = data2;
11394 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
11395 False, NoEventMask, &event);
11396 XSync (FRAME_X_DISPLAY (f), False);
11399 /* Change of visibility. */
11401 /* This tries to wait until the frame is really visible, depending on
11402 the value of Vx_wait_for_event_timeout.
11403 However, if the window manager asks the user where to position
11404 the frame, this will return before the user finishes doing that.
11405 The frame will not actually be visible at that time,
11406 but it will become visible later when the window manager
11407 finishes with it. */
11409 void
11410 x_make_frame_visible (struct frame *f)
11412 if (FRAME_PARENT_FRAME (f))
11414 if (!FRAME_VISIBLE_P (f))
11416 block_input ();
11417 #ifdef USE_GTK
11418 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
11419 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11420 f->left_pos, f->top_pos);
11421 #else
11422 XMapRaised (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
11423 #endif
11424 unblock_input ();
11426 SET_FRAME_VISIBLE (f, true);
11427 SET_FRAME_ICONIFIED (f, false);
11429 return;
11432 block_input ();
11434 x_set_bitmap_icon (f);
11436 if (! FRAME_VISIBLE_P (f))
11438 /* We test asked_for_visible here to make sure we don't
11439 call x_set_offset a second time
11440 if we get to x_make_frame_visible a second time
11441 before the window gets really visible. */
11442 if (! FRAME_ICONIFIED_P (f)
11443 && ! FRAME_X_EMBEDDED_P (f)
11444 && ! f->output_data.x->asked_for_visible)
11445 x_set_offset (f, f->left_pos, f->top_pos, 0);
11447 f->output_data.x->asked_for_visible = true;
11449 if (! EQ (Vx_no_window_manager, Qt))
11450 x_wm_set_window_state (f, NormalState);
11451 #ifdef USE_X_TOOLKIT
11452 if (FRAME_X_EMBEDDED_P (f))
11453 xembed_set_info (f, XEMBED_MAPPED);
11454 else
11456 /* This was XtPopup, but that did nothing for an iconified frame. */
11457 XtMapWidget (f->output_data.x->widget);
11459 #else /* not USE_X_TOOLKIT */
11460 #ifdef USE_GTK
11461 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
11462 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
11463 #else
11464 if (FRAME_X_EMBEDDED_P (f))
11465 xembed_set_info (f, XEMBED_MAPPED);
11466 else
11467 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11468 #endif /* not USE_GTK */
11469 #endif /* not USE_X_TOOLKIT */
11472 XFlush (FRAME_X_DISPLAY (f));
11474 /* Synchronize to ensure Emacs knows the frame is visible
11475 before we do anything else. We do this loop with input not blocked
11476 so that incoming events are handled. */
11478 Lisp_Object frame;
11479 /* This must be before UNBLOCK_INPUT
11480 since events that arrive in response to the actions above
11481 will set it when they are handled. */
11482 bool previously_visible = f->output_data.x->has_been_visible;
11484 XSETFRAME (frame, f);
11486 int original_left = f->left_pos;
11487 int original_top = f->top_pos;
11489 /* This must come after we set COUNT. */
11490 unblock_input ();
11492 /* We unblock here so that arriving X events are processed. */
11494 /* Now move the window back to where it was "supposed to be".
11495 But don't do it if the gravity is negative.
11496 When the gravity is negative, this uses a position
11497 that is 3 pixels too low. Perhaps that's really the border width.
11499 Don't do this if the window has never been visible before,
11500 because the window manager may choose the position
11501 and we don't want to override it. */
11503 if (!FRAME_VISIBLE_P (f)
11504 && !FRAME_ICONIFIED_P (f)
11505 && !FRAME_X_EMBEDDED_P (f)
11506 && !FRAME_PARENT_FRAME (f)
11507 && f->win_gravity == NorthWestGravity
11508 && previously_visible)
11510 Drawable rootw;
11511 int x, y;
11512 unsigned int width, height, border, depth;
11514 block_input ();
11516 /* On some window managers (such as FVWM) moving an existing
11517 window, even to the same place, causes the window manager
11518 to introduce an offset. This can cause the window to move
11519 to an unexpected location. Check the geometry (a little
11520 slow here) and then verify that the window is in the right
11521 place. If the window is not in the right place, move it
11522 there, and take the potential window manager hit. */
11523 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11524 &rootw, &x, &y, &width, &height, &border, &depth);
11526 if (original_left != x || original_top != y)
11527 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11528 original_left, original_top);
11530 unblock_input ();
11533 /* Try to wait for a MapNotify event (that is what tells us when a
11534 frame becomes visible). */
11536 #ifdef CYGWIN
11537 /* On Cygwin, which uses input polling, we need to force input to
11538 be read. See
11539 https://lists.gnu.org/r/emacs-devel/2013-12/msg00351.html
11540 and https://debbugs.gnu.org/cgi/bugreport.cgi?bug=24091#131.
11541 Fake an alarm signal to let the handler know that there's
11542 something to be read.
11544 It could be confusing if a real alarm arrives while processing
11545 the fake one. Turn it off and let the handler reset it. */
11546 int old_poll_suppress_count = poll_suppress_count;
11547 poll_suppress_count = 1;
11548 poll_for_input_1 ();
11549 poll_suppress_count = old_poll_suppress_count;
11550 #endif
11551 x_wait_for_event (f, MapNotify);
11555 /* Change from mapped state to withdrawn state. */
11557 /* Make the frame visible (mapped and not iconified). */
11559 void
11560 x_make_frame_invisible (struct frame *f)
11562 Window window;
11564 /* Use the frame's outermost window, not the one we normally draw on. */
11565 window = FRAME_OUTER_WINDOW (f);
11567 /* Don't keep the highlight on an invisible frame. */
11568 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
11569 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
11571 block_input ();
11573 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11574 that the current position of the window is user-specified, rather than
11575 program-specified, so that when the window is mapped again, it will be
11576 placed at the same location, without forcing the user to position it
11577 by hand again (they have already done that once for this window.) */
11578 x_wm_set_size_hint (f, 0, true);
11580 #ifdef USE_GTK
11581 if (FRAME_GTK_OUTER_WIDGET (f))
11582 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
11583 else
11584 #else
11585 if (FRAME_X_EMBEDDED_P (f))
11586 xembed_set_info (f, 0);
11587 else
11588 #endif
11590 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11591 DefaultScreen (FRAME_X_DISPLAY (f))))
11593 unblock_input ();
11594 error ("Can't notify window manager of window withdrawal");
11597 x_sync (f);
11599 /* We can't distinguish this from iconification
11600 just by the event that we get from the server.
11601 So we can't win using the usual strategy of letting
11602 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11603 and synchronize with the server to make sure we agree. */
11604 SET_FRAME_VISIBLE (f, 0);
11605 SET_FRAME_ICONIFIED (f, false);
11607 unblock_input ();
11610 /* Change window state from mapped to iconified. */
11612 void
11613 x_iconify_frame (struct frame *f)
11615 #ifdef USE_X_TOOLKIT
11616 int result;
11617 #endif
11619 /* Don't keep the highlight on an invisible frame. */
11620 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
11621 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
11623 if (FRAME_ICONIFIED_P (f))
11624 return;
11626 block_input ();
11628 x_set_bitmap_icon (f);
11630 #if defined (USE_GTK)
11631 if (FRAME_GTK_OUTER_WIDGET (f))
11633 if (! FRAME_VISIBLE_P (f))
11634 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
11636 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
11637 SET_FRAME_VISIBLE (f, 0);
11638 SET_FRAME_ICONIFIED (f, true);
11639 unblock_input ();
11640 return;
11642 #endif
11644 #ifdef USE_X_TOOLKIT
11646 if (! FRAME_VISIBLE_P (f))
11648 if (! EQ (Vx_no_window_manager, Qt))
11649 x_wm_set_window_state (f, IconicState);
11650 /* This was XtPopup, but that did nothing for an iconified frame. */
11651 XtMapWidget (f->output_data.x->widget);
11652 /* The server won't give us any event to indicate
11653 that an invisible frame was changed to an icon,
11654 so we have to record it here. */
11655 SET_FRAME_VISIBLE (f, 0);
11656 SET_FRAME_ICONIFIED (f, true);
11657 unblock_input ();
11658 return;
11661 result = XIconifyWindow (FRAME_X_DISPLAY (f),
11662 XtWindow (f->output_data.x->widget),
11663 DefaultScreen (FRAME_X_DISPLAY (f)));
11664 unblock_input ();
11666 if (!result)
11667 error ("Can't notify window manager of iconification");
11669 SET_FRAME_ICONIFIED (f, true);
11670 SET_FRAME_VISIBLE (f, 0);
11672 block_input ();
11673 XFlush (FRAME_X_DISPLAY (f));
11674 unblock_input ();
11675 #else /* not USE_X_TOOLKIT */
11677 /* Make sure the X server knows where the window should be positioned,
11678 in case the user deiconifies with the window manager. */
11679 if (! FRAME_VISIBLE_P (f)
11680 && ! FRAME_ICONIFIED_P (f)
11681 && ! FRAME_X_EMBEDDED_P (f))
11682 x_set_offset (f, f->left_pos, f->top_pos, 0);
11684 /* Since we don't know which revision of X we're running, we'll use both
11685 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11687 /* X11R4: send a ClientMessage to the window manager using the
11688 WM_CHANGE_STATE type. */
11690 XEvent msg;
11692 msg.xclient.window = FRAME_X_WINDOW (f);
11693 msg.xclient.type = ClientMessage;
11694 msg.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_wm_change_state;
11695 msg.xclient.format = 32;
11696 msg.xclient.data.l[0] = IconicState;
11698 if (! XSendEvent (FRAME_X_DISPLAY (f),
11699 DefaultRootWindow (FRAME_X_DISPLAY (f)),
11700 False,
11701 SubstructureRedirectMask | SubstructureNotifyMask,
11702 &msg))
11704 unblock_input ();
11705 error ("Can't notify window manager of iconification");
11709 /* X11R3: set the initial_state field of the window manager hints to
11710 IconicState. */
11711 x_wm_set_window_state (f, IconicState);
11713 if (!FRAME_VISIBLE_P (f))
11715 /* If the frame was withdrawn, before, we must map it. */
11716 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11719 SET_FRAME_ICONIFIED (f, true);
11720 SET_FRAME_VISIBLE (f, 0);
11722 XFlush (FRAME_X_DISPLAY (f));
11723 unblock_input ();
11724 #endif /* not USE_X_TOOLKIT */
11728 /* Free X resources of frame F. */
11730 void
11731 x_free_frame_resources (struct frame *f)
11733 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11734 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
11735 #ifdef USE_X_TOOLKIT
11736 Lisp_Object bar;
11737 struct scroll_bar *b;
11738 #endif
11740 block_input ();
11742 /* If a display connection is dead, don't try sending more
11743 commands to the X server. */
11744 if (dpyinfo->display)
11746 /* Always exit with visible pointer to avoid weird issue
11747 with Xfixes (Bug#17609). */
11748 if (f->pointer_invisible)
11749 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, 0);
11751 /* We must free faces before destroying windows because some
11752 font-driver (e.g. xft) access a window while finishing a
11753 face. */
11754 free_frame_faces (f);
11755 tear_down_x_back_buffer (f);
11757 if (f->output_data.x->icon_desc)
11758 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
11760 #ifdef USE_X_TOOLKIT
11761 /* Explicitly destroy the scroll bars of the frame. Without
11762 this, we get "BadDrawable" errors from the toolkit later on,
11763 presumably from expose events generated for the disappearing
11764 toolkit scroll bars. */
11765 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
11767 b = XSCROLL_BAR (bar);
11768 x_scroll_bar_remove (b);
11770 #endif
11772 #ifdef HAVE_X_I18N
11773 if (FRAME_XIC (f))
11774 free_frame_xic (f);
11775 #endif
11777 x_free_cr_resources (f);
11778 #ifdef USE_X_TOOLKIT
11779 if (f->output_data.x->widget)
11781 XtDestroyWidget (f->output_data.x->widget);
11782 f->output_data.x->widget = NULL;
11784 /* Tooltips don't have widgets, only a simple X window, even if
11785 we are using a toolkit. */
11786 else if (FRAME_X_WINDOW (f))
11787 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11789 free_frame_menubar (f);
11791 if (f->shell_position)
11792 xfree (f->shell_position);
11793 #else /* !USE_X_TOOLKIT */
11795 #ifdef USE_GTK
11796 xg_free_frame_widgets (f);
11797 #endif /* USE_GTK */
11799 tear_down_x_back_buffer (f);
11800 if (FRAME_X_WINDOW (f))
11801 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11802 #endif /* !USE_X_TOOLKIT */
11804 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
11805 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
11806 unload_color (f, f->output_data.x->cursor_pixel);
11807 unload_color (f, f->output_data.x->cursor_foreground_pixel);
11808 unload_color (f, f->output_data.x->border_pixel);
11809 unload_color (f, f->output_data.x->mouse_pixel);
11811 if (f->output_data.x->scroll_bar_background_pixel != -1)
11812 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
11813 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
11814 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
11815 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
11816 /* Scrollbar shadow colors. */
11817 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
11818 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
11819 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
11820 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
11821 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
11822 if (f->output_data.x->white_relief.pixel != -1)
11823 unload_color (f, f->output_data.x->white_relief.pixel);
11824 if (f->output_data.x->black_relief.pixel != -1)
11825 unload_color (f, f->output_data.x->black_relief.pixel);
11827 x_free_gcs (f);
11829 /* Free extra GCs allocated by x_setup_relief_colors. */
11830 if (f->output_data.x->white_relief.gc)
11832 XFreeGC (dpyinfo->display, f->output_data.x->white_relief.gc);
11833 f->output_data.x->white_relief.gc = 0;
11835 if (f->output_data.x->black_relief.gc)
11837 XFreeGC (dpyinfo->display, f->output_data.x->black_relief.gc);
11838 f->output_data.x->black_relief.gc = 0;
11841 /* Free cursors. */
11842 if (f->output_data.x->text_cursor != 0)
11843 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->text_cursor);
11844 if (f->output_data.x->nontext_cursor != 0)
11845 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->nontext_cursor);
11846 if (f->output_data.x->modeline_cursor != 0)
11847 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->modeline_cursor);
11848 if (f->output_data.x->hand_cursor != 0)
11849 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hand_cursor);
11850 if (f->output_data.x->hourglass_cursor != 0)
11851 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hourglass_cursor);
11852 if (f->output_data.x->horizontal_drag_cursor != 0)
11853 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->horizontal_drag_cursor);
11854 if (f->output_data.x->vertical_drag_cursor != 0)
11855 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->vertical_drag_cursor);
11856 if (f->output_data.x->left_edge_cursor != 0)
11857 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->left_edge_cursor);
11858 if (f->output_data.x->top_left_corner_cursor != 0)
11859 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->top_left_corner_cursor);
11860 if (f->output_data.x->top_edge_cursor != 0)
11861 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->top_edge_cursor);
11862 if (f->output_data.x->top_right_corner_cursor != 0)
11863 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->top_right_corner_cursor);
11864 if (f->output_data.x->right_edge_cursor != 0)
11865 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->right_edge_cursor);
11866 if (f->output_data.x->bottom_right_corner_cursor != 0)
11867 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->bottom_right_corner_cursor);
11868 if (f->output_data.x->bottom_edge_cursor != 0)
11869 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->bottom_edge_cursor);
11870 if (f->output_data.x->bottom_left_corner_cursor != 0)
11871 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->bottom_left_corner_cursor);
11873 XFlush (FRAME_X_DISPLAY (f));
11876 xfree (f->output_data.x->saved_menu_event);
11877 xfree (f->output_data.x);
11878 f->output_data.x = NULL;
11880 if (f == dpyinfo->x_focus_frame)
11881 dpyinfo->x_focus_frame = 0;
11882 if (f == dpyinfo->x_focus_event_frame)
11883 dpyinfo->x_focus_event_frame = 0;
11884 if (f == dpyinfo->x_highlight_frame)
11885 dpyinfo->x_highlight_frame = 0;
11886 if (f == hlinfo->mouse_face_mouse_frame)
11887 reset_mouse_highlight (hlinfo);
11889 unblock_input ();
11893 /* Destroy the X window of frame F. */
11895 static void
11896 x_destroy_window (struct frame *f)
11898 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11900 /* If a display connection is dead, don't try sending more
11901 commands to the X server. */
11902 if (dpyinfo->display != 0)
11903 x_free_frame_resources (f);
11905 dpyinfo->reference_count--;
11909 /* Setting window manager hints. */
11911 /* Set the normal size hints for the window manager, for frame F.
11912 FLAGS is the flags word to use--or 0 meaning preserve the flags
11913 that the window now has.
11914 If USER_POSITION, set the USPosition
11915 flag (this is useful when FLAGS is 0).
11916 The GTK version is in gtkutils.c. */
11918 #ifndef USE_GTK
11919 void
11920 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
11922 XSizeHints size_hints;
11923 Window window = FRAME_OUTER_WINDOW (f);
11925 if (!window)
11926 return;
11928 #ifdef USE_X_TOOLKIT
11929 if (f->output_data.x->widget)
11931 widget_update_wm_size_hints (f->output_data.x->widget);
11932 return;
11934 #endif
11936 /* Setting PMaxSize caused various problems. */
11937 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
11939 size_hints.x = f->left_pos;
11940 size_hints.y = f->top_pos;
11942 size_hints.width = FRAME_PIXEL_WIDTH (f);
11943 size_hints.height = FRAME_PIXEL_HEIGHT (f);
11945 size_hints.width_inc = frame_resize_pixelwise ? 1 : FRAME_COLUMN_WIDTH (f);
11946 size_hints.height_inc = frame_resize_pixelwise ? 1 : FRAME_LINE_HEIGHT (f);
11948 size_hints.max_width = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
11949 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
11950 size_hints.max_height = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
11951 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
11953 /* Calculate the base and minimum sizes. */
11955 int base_width, base_height;
11957 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
11958 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
11960 /* The window manager uses the base width hints to calculate the
11961 current number of rows and columns in the frame while
11962 resizing; min_width and min_height aren't useful for this
11963 purpose, since they might not give the dimensions for a
11964 zero-row, zero-column frame. */
11966 size_hints.flags |= PBaseSize;
11967 size_hints.base_width = base_width;
11968 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
11969 size_hints.min_width = base_width;
11970 size_hints.min_height = base_height;
11973 /* If we don't need the old flags, we don't need the old hint at all. */
11974 if (flags)
11976 size_hints.flags |= flags;
11977 goto no_read;
11981 XSizeHints hints; /* Sometimes I hate X Windows... */
11982 long supplied_return;
11983 int value;
11985 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
11986 &supplied_return);
11988 if (flags)
11989 size_hints.flags |= flags;
11990 else
11992 if (value == 0)
11993 hints.flags = 0;
11994 if (hints.flags & PSize)
11995 size_hints.flags |= PSize;
11996 if (hints.flags & PPosition)
11997 size_hints.flags |= PPosition;
11998 if (hints.flags & USPosition)
11999 size_hints.flags |= USPosition;
12000 if (hints.flags & USSize)
12001 size_hints.flags |= USSize;
12005 no_read:
12007 #ifdef PWinGravity
12008 size_hints.win_gravity = f->win_gravity;
12009 size_hints.flags |= PWinGravity;
12011 if (user_position)
12013 size_hints.flags &= ~ PPosition;
12014 size_hints.flags |= USPosition;
12016 #endif /* PWinGravity */
12018 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12020 #endif /* not USE_GTK */
12022 /* Used for IconicState or NormalState */
12024 static void
12025 x_wm_set_window_state (struct frame *f, int state)
12027 #ifdef USE_X_TOOLKIT
12028 Arg al[1];
12030 XtSetArg (al[0], XtNinitialState, state);
12031 XtSetValues (f->output_data.x->widget, al, 1);
12032 #else /* not USE_X_TOOLKIT */
12033 Window window = FRAME_X_WINDOW (f);
12035 f->output_data.x->wm_hints.flags |= StateHint;
12036 f->output_data.x->wm_hints.initial_state = state;
12038 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12039 #endif /* not USE_X_TOOLKIT */
12042 static void
12043 x_wm_set_icon_pixmap (struct frame *f, ptrdiff_t pixmap_id)
12045 Pixmap icon_pixmap, icon_mask;
12047 #if !defined USE_X_TOOLKIT && !defined USE_GTK
12048 Window window = FRAME_OUTER_WINDOW (f);
12049 #endif
12051 if (pixmap_id > 0)
12053 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
12054 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
12055 icon_mask = x_bitmap_mask (f, pixmap_id);
12056 f->output_data.x->wm_hints.icon_mask = icon_mask;
12058 else
12060 /* It seems there is no way to turn off use of an icon
12061 pixmap. */
12062 return;
12066 #ifdef USE_GTK
12068 xg_set_frame_icon (f, icon_pixmap, icon_mask);
12069 return;
12072 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
12075 Arg al[1];
12076 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12077 XtSetValues (f->output_data.x->widget, al, 1);
12078 XtSetArg (al[0], XtNiconMask, icon_mask);
12079 XtSetValues (f->output_data.x->widget, al, 1);
12082 #else /* not USE_X_TOOLKIT && not USE_GTK */
12084 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
12085 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12087 #endif /* not USE_X_TOOLKIT && not USE_GTK */
12090 void
12091 x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
12093 Window window = FRAME_OUTER_WINDOW (f);
12095 f->output_data.x->wm_hints.flags |= IconPositionHint;
12096 f->output_data.x->wm_hints.icon_x = icon_x;
12097 f->output_data.x->wm_hints.icon_y = icon_y;
12099 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12103 /***********************************************************************
12104 Fonts
12105 ***********************************************************************/
12107 #ifdef GLYPH_DEBUG
12109 /* Check that FONT is valid on frame F. It is if it can be found in F's
12110 font table. */
12112 static void
12113 x_check_font (struct frame *f, struct font *font)
12115 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
12116 if (font->driver->check)
12117 eassert (font->driver->check (f, font) == 0);
12120 #endif /* GLYPH_DEBUG */
12123 /***********************************************************************
12124 Initialization
12125 ***********************************************************************/
12127 #ifdef USE_X_TOOLKIT
12128 static XrmOptionDescRec emacs_options[] = {
12129 {(char *) "-geometry", (char *) ".geometry", XrmoptionSepArg, NULL},
12130 {(char *) "-iconic", (char *) ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
12132 {(char *) "-internal-border-width",
12133 (char *) "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
12134 {(char *) "-ib", (char *) "*EmacsScreen.internalBorderWidth",
12135 XrmoptionSepArg, NULL},
12136 {(char *) "-T", (char *) "*EmacsShell.title", XrmoptionSepArg, NULL},
12137 {(char *) "-wn", (char *) "*EmacsShell.title", XrmoptionSepArg, NULL},
12138 {(char *) "-title", (char *) "*EmacsShell.title", XrmoptionSepArg, NULL},
12139 {(char *) "-iconname", (char *) "*EmacsShell.iconName",
12140 XrmoptionSepArg, NULL},
12141 {(char *) "-in", (char *) "*EmacsShell.iconName", XrmoptionSepArg, NULL},
12142 {(char *) "-mc", (char *) "*pointerColor", XrmoptionSepArg, NULL},
12143 {(char *) "-cr", (char *) "*cursorColor", XrmoptionSepArg, NULL}
12146 /* Whether atimer for Xt timeouts is activated or not. */
12148 static bool x_timeout_atimer_activated_flag;
12150 #endif /* USE_X_TOOLKIT */
12152 static int x_initialized;
12154 /* Test whether two display-name strings agree up to the dot that separates
12155 the screen number from the server number. */
12156 static bool
12157 same_x_server (const char *name1, const char *name2)
12159 bool seen_colon = false;
12160 Lisp_Object sysname = Fsystem_name ();
12161 if (! STRINGP (sysname))
12162 sysname = empty_unibyte_string;
12163 const char *system_name = SSDATA (sysname);
12164 ptrdiff_t system_name_length = SBYTES (sysname);
12165 ptrdiff_t length_until_period = 0;
12167 while (system_name[length_until_period] != 0
12168 && system_name[length_until_period] != '.')
12169 length_until_period++;
12171 /* Treat `unix' like an empty host name. */
12172 if (! strncmp (name1, "unix:", 5))
12173 name1 += 4;
12174 if (! strncmp (name2, "unix:", 5))
12175 name2 += 4;
12176 /* Treat this host's name like an empty host name. */
12177 if (! strncmp (name1, system_name, system_name_length)
12178 && name1[system_name_length] == ':')
12179 name1 += system_name_length;
12180 if (! strncmp (name2, system_name, system_name_length)
12181 && name2[system_name_length] == ':')
12182 name2 += system_name_length;
12183 /* Treat this host's domainless name like an empty host name. */
12184 if (! strncmp (name1, system_name, length_until_period)
12185 && name1[length_until_period] == ':')
12186 name1 += length_until_period;
12187 if (! strncmp (name2, system_name, length_until_period)
12188 && name2[length_until_period] == ':')
12189 name2 += length_until_period;
12191 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
12193 if (*name1 == ':')
12194 seen_colon = true;
12195 if (seen_colon && *name1 == '.')
12196 return true;
12198 return (seen_colon
12199 && (*name1 == '.' || *name1 == '\0')
12200 && (*name2 == '.' || *name2 == '\0'));
12203 /* Count number of set bits in mask and number of bits to shift to
12204 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
12205 to 5. */
12206 static void
12207 get_bits_and_offset (unsigned long mask, int *bits, int *offset)
12209 int nr = 0;
12210 int off = 0;
12212 while (!(mask & 1))
12214 off++;
12215 mask >>= 1;
12218 while (mask & 1)
12220 nr++;
12221 mask >>= 1;
12224 *offset = off;
12225 *bits = nr;
12228 /* Return true iff display DISPLAY is available for use.
12229 But don't permanently open it, just test its availability. */
12231 bool
12232 x_display_ok (const char *display)
12234 /* XOpenDisplay fails if it gets a signal. Block SIGIO which may arrive. */
12235 unrequest_sigio ();
12236 Display *dpy = XOpenDisplay (display);
12237 request_sigio ();
12238 if (!dpy)
12239 return false;
12240 XCloseDisplay (dpy);
12241 return true;
12244 #ifdef USE_GTK
12245 static void
12246 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
12247 const gchar *msg, gpointer user_data)
12249 if (!strstr (msg, "g_set_prgname"))
12250 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, msg);
12252 #endif
12254 /* Create invisible cursor on X display referred by DPYINFO. */
12256 static Cursor
12257 make_invisible_cursor (struct x_display_info *dpyinfo)
12259 Display *dpy = dpyinfo->display;
12260 static char const no_data[] = { 0 };
12261 Pixmap pix;
12262 XColor col;
12263 Cursor c = 0;
12265 x_catch_errors (dpy);
12266 pix = XCreateBitmapFromData (dpy, dpyinfo->root_window, no_data, 1, 1);
12267 if (! x_had_errors_p (dpy) && pix != None)
12269 Cursor pixc;
12270 col.pixel = 0;
12271 col.red = col.green = col.blue = 0;
12272 col.flags = DoRed | DoGreen | DoBlue;
12273 pixc = XCreatePixmapCursor (dpy, pix, pix, &col, &col, 0, 0);
12274 if (! x_had_errors_p (dpy) && pixc != None)
12275 c = pixc;
12276 XFreePixmap (dpy, pix);
12279 x_uncatch_errors ();
12281 return c;
12284 /* True if DPY supports Xfixes extension >= 4. */
12286 static bool
12287 x_probe_xfixes_extension (Display *dpy)
12289 #ifdef HAVE_XFIXES
12290 int major, minor;
12291 return XFixesQueryVersion (dpy, &major, &minor) && major >= 4;
12292 #else
12293 return false;
12294 #endif /* HAVE_XFIXES */
12297 /* Toggle mouse pointer visibility on frame F by using Xfixes functions. */
12299 static void
12300 xfixes_toggle_visible_pointer (struct frame *f, bool invisible)
12302 #ifdef HAVE_XFIXES
12303 if (invisible)
12304 XFixesHideCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12305 else
12306 XFixesShowCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12307 f->pointer_invisible = invisible;
12308 #else
12309 emacs_abort ();
12310 #endif /* HAVE_XFIXES */
12313 /* Toggle mouse pointer visibility on frame F by using invisible cursor. */
12315 static void
12316 x_toggle_visible_pointer (struct frame *f, bool invisible)
12318 eassert (FRAME_DISPLAY_INFO (f)->invisible_cursor != 0);
12319 if (invisible)
12320 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12321 FRAME_DISPLAY_INFO (f)->invisible_cursor);
12322 else
12323 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12324 f->output_data.x->current_cursor);
12325 f->pointer_invisible = invisible;
12328 /* Setup pointer blanking, prefer Xfixes if available. */
12330 static void
12331 x_setup_pointer_blanking (struct x_display_info *dpyinfo)
12333 /* FIXME: the brave tester should set EMACS_XFIXES because we're suspecting
12334 X server bug, see https://debbugs.gnu.org/cgi/bugreport.cgi?bug=17609. */
12335 if (egetenv ("EMACS_XFIXES") && x_probe_xfixes_extension (dpyinfo->display))
12336 dpyinfo->toggle_visible_pointer = xfixes_toggle_visible_pointer;
12337 else
12339 dpyinfo->toggle_visible_pointer = x_toggle_visible_pointer;
12340 dpyinfo->invisible_cursor = make_invisible_cursor (dpyinfo);
12344 /* Current X display connection identifier. Incremented for each next
12345 connection established. */
12346 static unsigned x_display_id;
12348 /* Open a connection to X display DISPLAY_NAME, and return
12349 the structure that describes the open display.
12350 If we cannot contact the display, return null. */
12352 struct x_display_info *
12353 x_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
12355 Display *dpy;
12356 struct terminal *terminal;
12357 struct x_display_info *dpyinfo;
12358 XrmDatabase xrdb;
12359 #ifdef USE_XCB
12360 xcb_connection_t *xcb_conn;
12361 #endif
12363 block_input ();
12365 if (!x_initialized)
12367 x_initialize ();
12368 ++x_initialized;
12371 if (! x_display_ok (SSDATA (display_name)))
12372 error ("Display %s can't be opened", SSDATA (display_name));
12374 #ifdef USE_GTK
12376 #define NUM_ARGV 10
12377 int argc;
12378 char *argv[NUM_ARGV];
12379 char **argv2 = argv;
12380 guint id;
12382 if (x_initialized++ > 1)
12384 xg_display_open (SSDATA (display_name), &dpy);
12386 else
12388 static char display_opt[] = "--display";
12389 static char name_opt[] = "--name";
12391 for (argc = 0; argc < NUM_ARGV; ++argc)
12392 argv[argc] = 0;
12394 argc = 0;
12395 argv[argc++] = initial_argv[0];
12397 if (! NILP (display_name))
12399 argv[argc++] = display_opt;
12400 argv[argc++] = SSDATA (display_name);
12403 argv[argc++] = name_opt;
12404 argv[argc++] = resource_name;
12406 XSetLocaleModifiers ("");
12408 /* Work around GLib bug that outputs a faulty warning. See
12409 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
12410 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
12411 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
12413 /* NULL window -> events for all windows go to our function.
12414 Call before gtk_init so Gtk+ event filters comes after our. */
12415 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
12417 /* gtk_init does set_locale. Fix locale before and after. */
12418 fixup_locale ();
12419 unrequest_sigio (); /* See comment in x_display_ok. */
12420 gtk_init (&argc, &argv2);
12421 request_sigio ();
12423 g_log_remove_handler ("GLib", id);
12425 xg_initialize ();
12427 /* Do this after the call to xg_initialize, because when
12428 Fontconfig is used, xg_initialize calls its initialization
12429 function which in some versions of Fontconfig calls setlocale. */
12430 fixup_locale ();
12432 dpy = DEFAULT_GDK_DISPLAY ();
12434 #if ! GTK_CHECK_VERSION (2, 90, 0)
12435 /* Load our own gtkrc if it exists. */
12437 const char *file = "~/.emacs.d/gtkrc";
12438 Lisp_Object s, abs_file;
12440 s = build_string (file);
12441 abs_file = Fexpand_file_name (s, Qnil);
12443 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
12444 gtk_rc_parse (SSDATA (abs_file));
12446 #endif
12448 XSetErrorHandler (x_error_handler);
12449 XSetIOErrorHandler (x_io_error_quitter);
12452 #else /* not USE_GTK */
12453 #ifdef USE_X_TOOLKIT
12454 /* weiner@footloose.sps.mot.com reports that this causes
12455 errors with X11R5:
12456 X protocol error: BadAtom (invalid Atom parameter)
12457 on protocol request 18skiloaf.
12458 So let's not use it until R6. */
12459 #ifdef HAVE_X11XTR6
12460 XtSetLanguageProc (NULL, NULL, NULL);
12461 #endif
12464 int argc = 0;
12465 char *argv[3];
12467 argv[0] = (char *) "";
12468 argc = 1;
12469 if (xrm_option)
12471 argv[argc++] = (char *) "-xrm";
12472 argv[argc++] = xrm_option;
12474 turn_on_atimers (false);
12475 unrequest_sigio (); /* See comment in x_display_ok. */
12476 dpy = XtOpenDisplay (Xt_app_con, SSDATA (display_name),
12477 resource_name, EMACS_CLASS,
12478 emacs_options, XtNumber (emacs_options),
12479 &argc, argv);
12480 request_sigio ();
12481 turn_on_atimers (true);
12483 #ifdef HAVE_X11XTR6
12484 /* I think this is to compensate for XtSetLanguageProc. */
12485 fixup_locale ();
12486 #endif
12489 #else /* not USE_X_TOOLKIT */
12490 XSetLocaleModifiers ("");
12491 unrequest_sigio (); /* See comment in x_display_ok. */
12492 dpy = XOpenDisplay (SSDATA (display_name));
12493 request_sigio ();
12494 #endif /* not USE_X_TOOLKIT */
12495 #endif /* not USE_GTK*/
12497 /* Detect failure. */
12498 if (dpy == 0)
12500 unblock_input ();
12501 return 0;
12504 #ifdef USE_XCB
12505 xcb_conn = XGetXCBConnection (dpy);
12506 if (xcb_conn == 0)
12508 #ifdef USE_GTK
12509 xg_display_close (dpy);
12510 #else
12511 #ifdef USE_X_TOOLKIT
12512 XtCloseDisplay (dpy);
12513 #else
12514 XCloseDisplay (dpy);
12515 #endif
12516 #endif /* ! USE_GTK */
12518 unblock_input ();
12519 return 0;
12521 #endif
12523 /* We have definitely succeeded. Record the new connection. */
12525 dpyinfo = xzalloc (sizeof *dpyinfo);
12526 terminal = x_create_terminal (dpyinfo);
12529 struct x_display_info *share;
12531 for (share = x_display_list; share; share = share->next)
12532 if (same_x_server (SSDATA (XCAR (share->name_list_element)),
12533 SSDATA (display_name)))
12534 break;
12535 if (share)
12536 terminal->kboard = share->terminal->kboard;
12537 else
12539 terminal->kboard = allocate_kboard (Qx);
12541 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->u.s.function, Qunbound))
12543 char *vendor = ServerVendor (dpy);
12545 /* Temporarily hide the partially initialized terminal. */
12546 terminal_list = terminal->next_terminal;
12547 unblock_input ();
12548 kset_system_key_alist
12549 (terminal->kboard,
12550 call1 (Qvendor_specific_keysyms,
12551 vendor ? build_string (vendor) : empty_unibyte_string));
12552 block_input ();
12553 terminal->next_terminal = terminal_list;
12554 terminal_list = terminal;
12557 /* Don't let the initial kboard remain current longer than necessary.
12558 That would cause problems if a file loaded on startup tries to
12559 prompt in the mini-buffer. */
12560 if (current_kboard == initial_kboard)
12561 current_kboard = terminal->kboard;
12563 terminal->kboard->reference_count++;
12566 /* Put this display on the chain. */
12567 dpyinfo->next = x_display_list;
12568 x_display_list = dpyinfo;
12570 dpyinfo->name_list_element = Fcons (display_name, Qnil);
12571 dpyinfo->display = dpy;
12572 dpyinfo->connection = ConnectionNumber (dpyinfo->display);
12573 #ifdef USE_XCB
12574 dpyinfo->xcb_connection = xcb_conn;
12575 #endif
12577 /* https://lists.gnu.org/r/emacs-devel/2015-11/msg00194.html */
12578 dpyinfo->smallest_font_height = 1;
12579 dpyinfo->smallest_char_width = 1;
12581 /* Set the name of the terminal. */
12582 terminal->name = xlispstrdup (display_name);
12584 #if false
12585 XSetAfterFunction (x_current_display, x_trace_wire);
12586 #endif
12588 Lisp_Object system_name = Fsystem_name ();
12590 ptrdiff_t nbytes = SBYTES (Vinvocation_name) + 1;
12591 if (STRINGP (system_name)
12592 && INT_ADD_WRAPV (nbytes, SBYTES (system_name) + 1, &nbytes))
12593 memory_full (SIZE_MAX);
12594 dpyinfo->x_id = ++x_display_id;
12595 dpyinfo->x_id_name = xmalloc (nbytes);
12596 char *nametail = lispstpcpy (dpyinfo->x_id_name, Vinvocation_name);
12597 if (STRINGP (system_name))
12599 *nametail++ = '@';
12600 lispstpcpy (nametail, system_name);
12603 /* Figure out which modifier bits mean what. */
12604 x_find_modifier_meanings (dpyinfo);
12606 /* Get the scroll bar cursor. */
12607 #ifdef USE_GTK
12608 /* We must create a GTK cursor, it is required for GTK widgets. */
12609 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
12610 #endif /* USE_GTK */
12612 dpyinfo->vertical_scroll_bar_cursor
12613 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
12615 dpyinfo->horizontal_scroll_bar_cursor
12616 = XCreateFontCursor (dpyinfo->display, XC_sb_h_double_arrow);
12618 xrdb = x_load_resources (dpyinfo->display, xrm_option,
12619 resource_name, EMACS_CLASS);
12620 #ifdef HAVE_XRMSETDATABASE
12621 XrmSetDatabase (dpyinfo->display, xrdb);
12622 #else
12623 dpyinfo->display->db = xrdb;
12624 #endif
12625 /* Put the rdb where we can find it in a way that works on
12626 all versions. */
12627 dpyinfo->xrdb = xrdb;
12629 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
12630 DefaultScreen (dpyinfo->display));
12631 select_visual (dpyinfo);
12632 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
12633 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
12634 dpyinfo->icon_bitmap_id = -1;
12635 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
12637 reset_mouse_highlight (&dpyinfo->mouse_highlight);
12639 /* See if we can construct pixel values from RGB values. */
12640 if (dpyinfo->visual->class == TrueColor)
12642 get_bits_and_offset (dpyinfo->visual->red_mask,
12643 &dpyinfo->red_bits, &dpyinfo->red_offset);
12644 get_bits_and_offset (dpyinfo->visual->blue_mask,
12645 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
12646 get_bits_and_offset (dpyinfo->visual->green_mask,
12647 &dpyinfo->green_bits, &dpyinfo->green_offset);
12650 /* See if a private colormap is requested. */
12651 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
12653 if (dpyinfo->visual->class == PseudoColor)
12655 AUTO_STRING (privateColormap, "privateColormap");
12656 AUTO_STRING (PrivateColormap, "PrivateColormap");
12657 Lisp_Object value
12658 = display_x_get_resource (dpyinfo, privateColormap,
12659 PrivateColormap, Qnil, Qnil);
12660 if (STRINGP (value)
12661 && (!strcmp (SSDATA (value), "true")
12662 || !strcmp (SSDATA (value), "on")))
12663 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
12666 else
12667 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
12668 dpyinfo->visual, AllocNone);
12670 #ifdef HAVE_XDBE
12671 dpyinfo->supports_xdbe = false;
12672 int xdbe_major;
12673 int xdbe_minor;
12674 if (XdbeQueryExtension (dpyinfo->display, &xdbe_major, &xdbe_minor))
12675 dpyinfo->supports_xdbe = true;
12676 #endif
12678 #ifdef HAVE_XFT
12680 /* If we are using Xft, the following precautions should be made:
12682 1. Make sure that the Xrender extension is added before the Xft one.
12683 Otherwise, the close-display hook set by Xft is called after the one
12684 for Xrender, and the former tries to re-add the latter. This results
12685 in inconsistency of internal states and leads to X protocol error when
12686 one reconnects to the same X server (Bug#1696).
12688 2. Check dpi value in X resources. It is better we use it as well,
12689 since Xft will use it, as will all Gnome applications. If our real DPI
12690 is smaller or larger than the one Xft uses, our font will look smaller
12691 or larger than other for other applications, even if it is the same
12692 font name (monospace-10 for example). */
12694 int event_base, error_base;
12695 char *v;
12696 double d;
12698 XRenderQueryExtension (dpyinfo->display, &event_base, &error_base);
12700 v = XGetDefault (dpyinfo->display, "Xft", "dpi");
12701 if (v != NULL && sscanf (v, "%lf", &d) == 1)
12702 dpyinfo->resy = dpyinfo->resx = d;
12704 #endif
12706 if (dpyinfo->resy < 1)
12708 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
12709 double pixels = DisplayHeight (dpyinfo->display, screen_number);
12710 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
12711 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
12712 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
12713 pixels = DisplayWidth (dpyinfo->display, screen_number);
12714 mm = DisplayWidthMM (dpyinfo->display, screen_number);
12715 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
12716 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
12720 static const struct
12722 const char *name;
12723 int offset;
12724 } atom_refs[] = {
12725 #define ATOM_REFS_INIT(string, member) \
12726 { string, offsetof (struct x_display_info, member) },
12727 ATOM_REFS_INIT ("WM_PROTOCOLS", Xatom_wm_protocols)
12728 ATOM_REFS_INIT ("WM_TAKE_FOCUS", Xatom_wm_take_focus)
12729 ATOM_REFS_INIT ("WM_SAVE_YOURSELF", Xatom_wm_save_yourself)
12730 ATOM_REFS_INIT ("WM_DELETE_WINDOW", Xatom_wm_delete_window)
12731 ATOM_REFS_INIT ("WM_CHANGE_STATE", Xatom_wm_change_state)
12732 ATOM_REFS_INIT ("WM_CONFIGURE_DENIED", Xatom_wm_configure_denied)
12733 ATOM_REFS_INIT ("WM_MOVED", Xatom_wm_window_moved)
12734 ATOM_REFS_INIT ("WM_CLIENT_LEADER", Xatom_wm_client_leader)
12735 ATOM_REFS_INIT ("Editres", Xatom_editres)
12736 ATOM_REFS_INIT ("CLIPBOARD", Xatom_CLIPBOARD)
12737 ATOM_REFS_INIT ("TIMESTAMP", Xatom_TIMESTAMP)
12738 ATOM_REFS_INIT ("TEXT", Xatom_TEXT)
12739 ATOM_REFS_INIT ("COMPOUND_TEXT", Xatom_COMPOUND_TEXT)
12740 ATOM_REFS_INIT ("UTF8_STRING", Xatom_UTF8_STRING)
12741 ATOM_REFS_INIT ("DELETE", Xatom_DELETE)
12742 ATOM_REFS_INIT ("MULTIPLE", Xatom_MULTIPLE)
12743 ATOM_REFS_INIT ("INCR", Xatom_INCR)
12744 ATOM_REFS_INIT ("_EMACS_TMP_", Xatom_EMACS_TMP)
12745 ATOM_REFS_INIT ("TARGETS", Xatom_TARGETS)
12746 ATOM_REFS_INIT ("NULL", Xatom_NULL)
12747 ATOM_REFS_INIT ("ATOM", Xatom_ATOM)
12748 ATOM_REFS_INIT ("ATOM_PAIR", Xatom_ATOM_PAIR)
12749 ATOM_REFS_INIT ("CLIPBOARD_MANAGER", Xatom_CLIPBOARD_MANAGER)
12750 ATOM_REFS_INIT ("_XEMBED_INFO", Xatom_XEMBED_INFO)
12751 /* For properties of font. */
12752 ATOM_REFS_INIT ("PIXEL_SIZE", Xatom_PIXEL_SIZE)
12753 ATOM_REFS_INIT ("AVERAGE_WIDTH", Xatom_AVERAGE_WIDTH)
12754 ATOM_REFS_INIT ("_MULE_BASELINE_OFFSET", Xatom_MULE_BASELINE_OFFSET)
12755 ATOM_REFS_INIT ("_MULE_RELATIVE_COMPOSE", Xatom_MULE_RELATIVE_COMPOSE)
12756 ATOM_REFS_INIT ("_MULE_DEFAULT_ASCENT", Xatom_MULE_DEFAULT_ASCENT)
12757 /* Ghostscript support. */
12758 ATOM_REFS_INIT ("DONE", Xatom_DONE)
12759 ATOM_REFS_INIT ("PAGE", Xatom_PAGE)
12760 ATOM_REFS_INIT ("SCROLLBAR", Xatom_Scrollbar)
12761 ATOM_REFS_INIT ("HORIZONTAL_SCROLLBAR", Xatom_Horizontal_Scrollbar)
12762 ATOM_REFS_INIT ("_XEMBED", Xatom_XEMBED)
12763 /* EWMH */
12764 ATOM_REFS_INIT ("_NET_WM_STATE", Xatom_net_wm_state)
12765 ATOM_REFS_INIT ("_NET_WM_STATE_FULLSCREEN", Xatom_net_wm_state_fullscreen)
12766 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_HORZ",
12767 Xatom_net_wm_state_maximized_horz)
12768 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_VERT",
12769 Xatom_net_wm_state_maximized_vert)
12770 ATOM_REFS_INIT ("_NET_WM_STATE_STICKY", Xatom_net_wm_state_sticky)
12771 ATOM_REFS_INIT ("_NET_WM_STATE_HIDDEN", Xatom_net_wm_state_hidden)
12772 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE", Xatom_net_window_type)
12773 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE_TOOLTIP",
12774 Xatom_net_window_type_tooltip)
12775 ATOM_REFS_INIT ("_NET_WM_ICON_NAME", Xatom_net_wm_icon_name)
12776 ATOM_REFS_INIT ("_NET_WM_NAME", Xatom_net_wm_name)
12777 ATOM_REFS_INIT ("_NET_SUPPORTED", Xatom_net_supported)
12778 ATOM_REFS_INIT ("_NET_SUPPORTING_WM_CHECK", Xatom_net_supporting_wm_check)
12779 ATOM_REFS_INIT ("_NET_WM_WINDOW_OPACITY", Xatom_net_wm_window_opacity)
12780 ATOM_REFS_INIT ("_NET_ACTIVE_WINDOW", Xatom_net_active_window)
12781 ATOM_REFS_INIT ("_NET_FRAME_EXTENTS", Xatom_net_frame_extents)
12782 ATOM_REFS_INIT ("_NET_CURRENT_DESKTOP", Xatom_net_current_desktop)
12783 ATOM_REFS_INIT ("_NET_WORKAREA", Xatom_net_workarea)
12784 /* Session management */
12785 ATOM_REFS_INIT ("SM_CLIENT_ID", Xatom_SM_CLIENT_ID)
12786 ATOM_REFS_INIT ("_XSETTINGS_SETTINGS", Xatom_xsettings_prop)
12787 ATOM_REFS_INIT ("MANAGER", Xatom_xsettings_mgr)
12788 ATOM_REFS_INIT ("_NET_WM_STATE_SKIP_TASKBAR", Xatom_net_wm_state_skip_taskbar)
12789 ATOM_REFS_INIT ("_NET_WM_STATE_ABOVE", Xatom_net_wm_state_above)
12790 ATOM_REFS_INIT ("_NET_WM_STATE_BELOW", Xatom_net_wm_state_below)
12793 int i;
12794 enum { atom_count = ARRAYELTS (atom_refs) };
12795 /* 1 for _XSETTINGS_SN. */
12796 enum { total_atom_count = 1 + atom_count };
12797 Atom atoms_return[total_atom_count];
12798 char *atom_names[total_atom_count];
12799 static char const xsettings_fmt[] = "_XSETTINGS_S%d";
12800 char xsettings_atom_name[sizeof xsettings_fmt - 2
12801 + INT_STRLEN_BOUND (int)];
12803 for (i = 0; i < atom_count; i++)
12804 atom_names[i] = (char *) atom_refs[i].name;
12806 /* Build _XSETTINGS_SN atom name. */
12807 sprintf (xsettings_atom_name, xsettings_fmt,
12808 XScreenNumberOfScreen (dpyinfo->screen));
12809 atom_names[i] = xsettings_atom_name;
12811 XInternAtoms (dpyinfo->display, atom_names, total_atom_count,
12812 False, atoms_return);
12814 for (i = 0; i < atom_count; i++)
12815 *(Atom *) ((char *) dpyinfo + atom_refs[i].offset) = atoms_return[i];
12817 /* Manually copy last atom. */
12818 dpyinfo->Xatom_xsettings_sel = atoms_return[i];
12821 dpyinfo->x_dnd_atoms_size = 8;
12822 dpyinfo->x_dnd_atoms = xmalloc (sizeof *dpyinfo->x_dnd_atoms
12823 * dpyinfo->x_dnd_atoms_size);
12824 dpyinfo->gray
12825 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12826 gray_bits, gray_width, gray_height,
12827 1, 0, 1);
12829 x_setup_pointer_blanking (dpyinfo);
12831 #ifdef HAVE_X_I18N
12832 xim_initialize (dpyinfo, resource_name);
12833 #endif
12835 xsettings_initialize (dpyinfo);
12837 /* This is only needed for distinguishing keyboard and process input. */
12838 if (dpyinfo->connection != 0)
12839 add_keyboard_wait_descriptor (dpyinfo->connection);
12841 #ifdef F_SETOWN
12842 fcntl (dpyinfo->connection, F_SETOWN, getpid ());
12843 #endif /* ! defined (F_SETOWN) */
12845 if (interrupt_input)
12846 init_sigio (dpyinfo->connection);
12848 #ifdef USE_LUCID
12850 XrmValue d, fr, to;
12851 Font font;
12853 dpy = dpyinfo->display;
12854 d.addr = (XPointer)&dpy;
12855 d.size = sizeof (Display *);
12856 fr.addr = (char *) XtDefaultFont;
12857 fr.size = sizeof (XtDefaultFont);
12858 to.size = sizeof (Font *);
12859 to.addr = (XPointer)&font;
12860 x_catch_errors (dpy);
12861 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
12862 emacs_abort ();
12863 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
12864 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
12865 /* Do not free XFontStruct returned by the above call to XQueryFont.
12866 This leads to X protocol errors at XtCloseDisplay (Bug#18403). */
12867 x_uncatch_errors ();
12869 #endif
12871 /* See if we should run in synchronous mode. This is useful
12872 for debugging X code. */
12874 AUTO_STRING (synchronous, "synchronous");
12875 AUTO_STRING (Synchronous, "Synchronous");
12876 Lisp_Object value = display_x_get_resource (dpyinfo, synchronous,
12877 Synchronous, Qnil, Qnil);
12878 if (STRINGP (value)
12879 && (!strcmp (SSDATA (value), "true")
12880 || !strcmp (SSDATA (value), "on")))
12881 XSynchronize (dpyinfo->display, True);
12885 AUTO_STRING (useXIM, "useXIM");
12886 AUTO_STRING (UseXIM, "UseXIM");
12887 Lisp_Object value = display_x_get_resource (dpyinfo, useXIM, UseXIM,
12888 Qnil, Qnil);
12889 #ifdef USE_XIM
12890 if (STRINGP (value)
12891 && (!strcmp (SSDATA (value), "false")
12892 || !strcmp (SSDATA (value), "off")))
12893 use_xim = false;
12894 #else
12895 if (STRINGP (value)
12896 && (!strcmp (SSDATA (value), "true")
12897 || !strcmp (SSDATA (value), "on")))
12898 use_xim = true;
12899 #endif
12902 #ifdef HAVE_X_SM
12903 /* Only do this for the very first display in the Emacs session.
12904 Ignore X session management when Emacs was first started on a
12905 tty or started as a daemon. */
12906 if (terminal->id == 1 && ! IS_DAEMON)
12907 x_session_initialize (dpyinfo);
12908 #endif
12910 #ifdef USE_CAIRO
12911 x_extension_initialize (dpyinfo);
12912 #endif
12914 unblock_input ();
12916 return dpyinfo;
12919 /* Get rid of display DPYINFO, deleting all frames on it,
12920 and without sending any more commands to the X server. */
12922 static void
12923 x_delete_display (struct x_display_info *dpyinfo)
12925 struct terminal *t;
12926 struct color_name_cache_entry *color_entry, *next_color_entry;
12928 /* Close all frames and delete the generic struct terminal for this
12929 X display. */
12930 for (t = terminal_list; t; t = t->next_terminal)
12931 if (t->type == output_x_window && t->display_info.x == dpyinfo)
12933 #ifdef HAVE_X_SM
12934 /* Close X session management when we close its display. */
12935 if (t->id == 1 && x_session_have_connection ())
12936 x_session_close ();
12937 #endif
12938 delete_terminal (t);
12939 break;
12942 if (next_noop_dpyinfo == dpyinfo)
12943 next_noop_dpyinfo = dpyinfo->next;
12945 if (x_display_list == dpyinfo)
12946 x_display_list = dpyinfo->next;
12947 else
12949 struct x_display_info *tail;
12951 for (tail = x_display_list; tail; tail = tail->next)
12952 if (tail->next == dpyinfo)
12953 tail->next = tail->next->next;
12956 for (color_entry = dpyinfo->color_names;
12957 color_entry;
12958 color_entry = next_color_entry)
12960 next_color_entry = color_entry->next;
12961 xfree (color_entry->name);
12962 xfree (color_entry);
12965 xfree (dpyinfo->x_id_name);
12966 xfree (dpyinfo->x_dnd_atoms);
12967 xfree (dpyinfo->color_cells);
12968 xfree (dpyinfo);
12971 #ifdef USE_X_TOOLKIT
12973 /* Atimer callback function for TIMER. Called every 0.1s to process
12974 Xt timeouts, if needed. We must avoid calling XtAppPending as
12975 much as possible because that function does an implicit XFlush
12976 that slows us down. */
12978 static void
12979 x_process_timeouts (struct atimer *timer)
12981 block_input ();
12982 x_timeout_atimer_activated_flag = false;
12983 if (toolkit_scroll_bar_interaction || popup_activated ())
12985 while (XtAppPending (Xt_app_con) & XtIMTimer)
12986 XtAppProcessEvent (Xt_app_con, XtIMTimer);
12987 /* Reactivate the atimer for next time. */
12988 x_activate_timeout_atimer ();
12990 unblock_input ();
12993 /* Install an asynchronous timer that processes Xt timeout events
12994 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
12995 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
12996 function whenever these variables are set. This is necessary
12997 because some widget sets use timeouts internally, for example the
12998 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
12999 processed, these widgets don't behave normally. */
13001 void
13002 x_activate_timeout_atimer (void)
13004 block_input ();
13005 if (!x_timeout_atimer_activated_flag)
13007 struct timespec interval = make_timespec (0, 100 * 1000 * 1000);
13008 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
13009 x_timeout_atimer_activated_flag = true;
13011 unblock_input ();
13014 #endif /* USE_X_TOOLKIT */
13017 /* Set up use of X before we make the first connection. */
13019 static struct redisplay_interface x_redisplay_interface =
13021 x_frame_parm_handlers,
13022 x_produce_glyphs,
13023 x_write_glyphs,
13024 x_insert_glyphs,
13025 x_clear_end_of_line,
13026 x_scroll_run,
13027 x_after_update_window_line,
13028 x_update_window_begin,
13029 x_update_window_end,
13030 x_flip_and_flush,
13031 x_clear_window_mouse_face,
13032 x_get_glyph_overhangs,
13033 x_fix_overlapping_area,
13034 x_draw_fringe_bitmap,
13035 #ifdef USE_CAIRO
13036 x_cr_define_fringe_bitmap,
13037 x_cr_destroy_fringe_bitmap,
13038 #else
13039 0, /* define_fringe_bitmap */
13040 0, /* destroy_fringe_bitmap */
13041 #endif
13042 x_compute_glyph_string_overhangs,
13043 x_draw_glyph_string,
13044 x_define_frame_cursor,
13045 x_clear_frame_area,
13046 x_draw_window_cursor,
13047 x_draw_vertical_window_border,
13048 x_draw_window_divider,
13049 x_shift_glyphs_for_insert, /* Never called; see comment in function. */
13050 x_show_hourglass,
13051 x_hide_hourglass
13055 /* This function is called when the last frame on a display is deleted. */
13056 void
13057 x_delete_terminal (struct terminal *terminal)
13059 struct x_display_info *dpyinfo = terminal->display_info.x;
13061 /* Protect against recursive calls. delete_frame in
13062 delete_terminal calls us back when it deletes our last frame. */
13063 if (!terminal->name)
13064 return;
13066 block_input ();
13067 #ifdef HAVE_X_I18N
13068 /* We must close our connection to the XIM server before closing the
13069 X display. */
13070 if (dpyinfo->xim)
13071 xim_close_dpy (dpyinfo);
13072 #endif
13074 /* Normally, the display is available... */
13075 if (dpyinfo->display)
13077 x_destroy_all_bitmaps (dpyinfo);
13078 XSetCloseDownMode (dpyinfo->display, DestroyAll);
13080 /* Whether or not XCloseDisplay destroys the associated resource
13081 database depends on the version of libX11. To avoid both
13082 crash and memory leak, we dissociate the database from the
13083 display and then destroy dpyinfo->xrdb ourselves.
13085 Unfortunately, the above strategy does not work in some
13086 situations due to a bug in newer versions of libX11: because
13087 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
13088 dpy->db is NULL, XCloseDisplay destroys the associated
13089 database whereas it has not been created by XGetDefault
13090 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
13091 don't destroy the database here in order to avoid the crash
13092 in the above situations for now, though that may cause memory
13093 leaks in other situations. */
13094 #if false
13095 #ifdef HAVE_XRMSETDATABASE
13096 XrmSetDatabase (dpyinfo->display, NULL);
13097 #else
13098 dpyinfo->display->db = NULL;
13099 #endif
13100 /* We used to call XrmDestroyDatabase from x_delete_display, but
13101 some older versions of libX11 crash if we call it after
13102 closing all the displays. */
13103 XrmDestroyDatabase (dpyinfo->xrdb);
13104 #endif
13106 #ifdef USE_GTK
13107 xg_display_close (dpyinfo->display);
13108 #else
13109 #ifdef USE_X_TOOLKIT
13110 XtCloseDisplay (dpyinfo->display);
13111 #else
13112 XCloseDisplay (dpyinfo->display);
13113 #endif
13114 #endif /* ! USE_GTK */
13115 /* Do not close the connection here because it's already closed
13116 by X(t)CloseDisplay (Bug#18403). */
13117 dpyinfo->display = NULL;
13120 /* ...but if called from x_connection_closed, the display may already
13121 be closed and dpyinfo->display was set to 0 to indicate that. Since
13122 X server is most likely gone, explicit close is the only reliable
13123 way to continue and avoid Bug#19147. */
13124 else if (dpyinfo->connection >= 0)
13125 emacs_close (dpyinfo->connection);
13127 /* No more input on this descriptor. */
13128 delete_keyboard_wait_descriptor (dpyinfo->connection);
13129 /* Mark as dead. */
13130 dpyinfo->connection = -1;
13132 x_delete_display (dpyinfo);
13133 unblock_input ();
13136 /* Create a struct terminal, initialize it with the X11 specific
13137 functions and make DISPLAY->TERMINAL point to it. */
13139 static struct terminal *
13140 x_create_terminal (struct x_display_info *dpyinfo)
13142 struct terminal *terminal;
13144 terminal = create_terminal (output_x_window, &x_redisplay_interface);
13146 terminal->display_info.x = dpyinfo;
13147 dpyinfo->terminal = terminal;
13149 /* kboard is initialized in x_term_init. */
13151 terminal->clear_frame_hook = x_clear_frame;
13152 terminal->ins_del_lines_hook = x_ins_del_lines;
13153 terminal->delete_glyphs_hook = x_delete_glyphs;
13154 terminal->ring_bell_hook = XTring_bell;
13155 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
13156 terminal->update_begin_hook = x_update_begin;
13157 terminal->update_end_hook = x_update_end;
13158 terminal->read_socket_hook = XTread_socket;
13159 terminal->frame_up_to_date_hook = XTframe_up_to_date;
13160 terminal->buffer_flipping_unblocked_hook = XTbuffer_flipping_unblocked_hook;
13161 terminal->mouse_position_hook = XTmouse_position;
13162 terminal->frame_rehighlight_hook = XTframe_rehighlight;
13163 terminal->frame_raise_lower_hook = XTframe_raise_lower;
13164 terminal->fullscreen_hook = XTfullscreen_hook;
13165 terminal->menu_show_hook = x_menu_show;
13166 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
13167 terminal->popup_dialog_hook = xw_popup_dialog;
13168 #endif
13169 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13170 terminal->set_horizontal_scroll_bar_hook = XTset_horizontal_scroll_bar;
13171 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13172 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
13173 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
13174 terminal->delete_frame_hook = x_destroy_window;
13175 terminal->delete_terminal_hook = x_delete_terminal;
13176 /* Other hooks are NULL by default. */
13178 return terminal;
13181 static void
13182 x_initialize (void)
13184 baud_rate = 19200;
13186 x_noop_count = 0;
13187 any_help_event_p = false;
13188 ignore_next_mouse_click_timeout = 0;
13190 #ifdef USE_GTK
13191 current_count = -1;
13192 #endif
13194 /* Try to use interrupt input; if we can't, then start polling. */
13195 Fset_input_interrupt_mode (Qt);
13197 #if THREADS_ENABLED
13198 /* This must be called before any other Xlib routines. */
13199 if (XInitThreads () == 0)
13200 fprintf (stderr,
13201 "Warning: An error occurred initializing X11 thread support!\n");
13202 #endif
13204 #ifdef USE_X_TOOLKIT
13205 XtToolkitInitialize ();
13207 Xt_app_con = XtCreateApplicationContext ();
13209 /* Register a converter from strings to pixels, which uses
13210 Emacs' color allocation infrastructure. */
13211 XtAppSetTypeConverter (Xt_app_con,
13212 XtRString, XtRPixel, cvt_string_to_pixel,
13213 cvt_string_to_pixel_args,
13214 XtNumber (cvt_string_to_pixel_args),
13215 XtCacheByDisplay, cvt_pixel_dtor);
13217 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
13218 #endif
13220 #ifdef USE_TOOLKIT_SCROLL_BARS
13221 #ifndef USE_GTK
13222 xaw3d_arrow_scroll = False;
13223 xaw3d_pick_top = True;
13224 #endif
13225 #endif
13227 #ifdef USE_CAIRO
13228 x_cr_init_fringe (&x_redisplay_interface);
13229 #endif
13231 /* Note that there is no real way portable across R3/R4 to get the
13232 original error handler. */
13233 XSetErrorHandler (x_error_handler);
13234 XSetIOErrorHandler (x_io_error_quitter);
13237 #ifdef USE_GTK
13238 void
13239 init_xterm (void)
13241 /* Emacs can handle only core input events, so make sure
13242 Gtk doesn't use Xinput or Xinput2 extensions. */
13243 xputenv ("GDK_CORE_DEVICE_EVENTS=1");
13245 #endif
13247 void
13248 syms_of_xterm (void)
13250 x_error_message = NULL;
13252 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
13253 DEFSYM (Qlatin_1, "latin-1");
13255 #ifdef USE_GTK
13256 xg_default_icon_file = build_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
13257 staticpro (&xg_default_icon_file);
13259 DEFSYM (Qx_gtk_map_stock, "x-gtk-map-stock");
13260 #endif
13262 DEFVAR_BOOL ("x-use-underline-position-properties",
13263 x_use_underline_position_properties,
13264 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
13265 A value of nil means ignore them. If you encounter fonts with bogus
13266 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
13267 to 4.1, set this to nil. You can also use `underline-minimum-offset'
13268 to override the font's UNDERLINE_POSITION for small font display
13269 sizes. */);
13270 x_use_underline_position_properties = true;
13272 DEFVAR_BOOL ("x-underline-at-descent-line",
13273 x_underline_at_descent_line,
13274 doc: /* Non-nil means to draw the underline at the same place as the descent line.
13275 (If `line-spacing' is in effect, that moves the underline lower by
13276 that many pixels.)
13277 A value of nil means to draw the underline according to the value of the
13278 variable `x-use-underline-position-properties', which is usually at the
13279 baseline level. The default value is nil. */);
13280 x_underline_at_descent_line = false;
13282 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
13283 x_mouse_click_focus_ignore_position,
13284 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
13285 This variable is only used when the window manager requires that you
13286 click on a frame to select it (give it focus). In that case, a value
13287 of nil, means that the selected window and cursor position changes to
13288 reflect the mouse click position, while a non-nil value means that the
13289 selected window or cursor position is preserved. */);
13290 x_mouse_click_focus_ignore_position = false;
13292 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
13293 doc: /* Which toolkit scroll bars Emacs uses, if any.
13294 A value of nil means Emacs doesn't use toolkit scroll bars.
13295 With the X Window system, the value is a symbol describing the
13296 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
13297 With MS Windows or Nextstep, the value is t. */);
13298 #ifdef USE_TOOLKIT_SCROLL_BARS
13299 #ifdef USE_MOTIF
13300 Vx_toolkit_scroll_bars = intern_c_string ("motif");
13301 #elif defined HAVE_XAW3D
13302 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
13303 #elif USE_GTK
13304 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
13305 #else
13306 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
13307 #endif
13308 #else
13309 Vx_toolkit_scroll_bars = Qnil;
13310 #endif
13312 DEFSYM (Qmodifier_value, "modifier-value");
13313 DEFSYM (Qctrl, "ctrl");
13314 Fput (Qctrl, Qmodifier_value, make_number (ctrl_modifier));
13315 DEFSYM (Qalt, "alt");
13316 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
13317 DEFSYM (Qhyper, "hyper");
13318 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
13319 DEFSYM (Qmeta, "meta");
13320 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
13321 DEFSYM (Qsuper, "super");
13322 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
13324 DEFVAR_LISP ("x-ctrl-keysym", Vx_ctrl_keysym,
13325 doc: /* Which keys Emacs uses for the ctrl modifier.
13326 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
13327 `super'. For example, `ctrl' means use the Ctrl_L and Ctrl_R keysyms.
13328 The default is nil, which is the same as `ctrl'. */);
13329 Vx_ctrl_keysym = Qnil;
13331 DEFVAR_LISP ("x-alt-keysym", Vx_alt_keysym,
13332 doc: /* Which keys Emacs uses for the alt modifier.
13333 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
13334 `super'. For example, `alt' means use the Alt_L and Alt_R keysyms.
13335 The default is nil, which is the same as `alt'. */);
13336 Vx_alt_keysym = Qnil;
13338 DEFVAR_LISP ("x-hyper-keysym", Vx_hyper_keysym,
13339 doc: /* Which keys Emacs uses for the hyper modifier.
13340 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
13341 `super'. For example, `hyper' means use the Hyper_L and Hyper_R
13342 keysyms. The default is nil, which is the same as `hyper'. */);
13343 Vx_hyper_keysym = Qnil;
13345 DEFVAR_LISP ("x-meta-keysym", Vx_meta_keysym,
13346 doc: /* Which keys Emacs uses for the meta modifier.
13347 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
13348 `super'. For example, `meta' means use the Meta_L and Meta_R keysyms.
13349 The default is nil, which is the same as `meta'. */);
13350 Vx_meta_keysym = Qnil;
13352 DEFVAR_LISP ("x-super-keysym", Vx_super_keysym,
13353 doc: /* Which keys Emacs uses for the super modifier.
13354 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
13355 `super'. For example, `super' means use the Super_L and Super_R
13356 keysyms. The default is nil, which is the same as `super'. */);
13357 Vx_super_keysym = Qnil;
13359 DEFVAR_LISP ("x-wait-for-event-timeout", Vx_wait_for_event_timeout,
13360 doc: /* How long to wait for X events.
13362 Emacs will wait up to this many seconds to receive X events after
13363 making changes which affect the state of the graphical interface.
13364 Under some window managers this can take an indefinite amount of time,
13365 so it is important to limit the wait.
13367 If set to a non-float value, there will be no wait at all. */);
13368 Vx_wait_for_event_timeout = make_float (0.1);
13370 DEFVAR_LISP ("x-keysym-table", Vx_keysym_table,
13371 doc: /* Hash table of character codes indexed by X keysym codes. */);
13372 Vx_keysym_table = make_hash_table (hashtest_eql, 900,
13373 DEFAULT_REHASH_SIZE,
13374 DEFAULT_REHASH_THRESHOLD,
13375 Qnil, false);
13377 DEFVAR_BOOL ("x-frame-normalize-before-maximize",
13378 x_frame_normalize_before_maximize,
13379 doc: /* Non-nil means normalize frame before maximizing.
13380 If this variable is t, Emacs first asks the window manager to give the
13381 frame its normal size, and only then the final state, whenever changing
13382 from a full-height, full-width or full-both state to the maximized one
13383 or when changing from the maximized to the full-height or full-width
13384 state.
13386 Set this variable only if your window manager cannot handle the
13387 transition between the various maximization states. */);
13388 x_frame_normalize_before_maximize = false;
13390 DEFVAR_BOOL ("x-gtk-use-window-move", x_gtk_use_window_move,
13391 doc: /* Non-nil means rely on gtk_window_move to set frame positions.
13392 If this variable is t (the default), the GTK build uses the function
13393 gtk_window_move to set or store frame positions and disables some time
13394 consuming frame position adjustments. In newer versions of GTK, Emacs
13395 always uses gtk_window_move and ignores the value of this variable. */);
13396 x_gtk_use_window_move = true;