Support HiDPI displays for wave style underlines
[emacs.git] / src / xterm.c
blob5c1b061566f56ade7205eba38769362069e9b8f3
1 /* X Communication module for terminals which understand the X protocol.
3 Copyright (C) 1989, 1993-2017 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
21 /* Xt features made by Fred Pierresteguy. */
23 #include <config.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <math.h>
28 #include "lisp.h"
29 #include "blockinput.h"
31 /* This may include sys/types.h, and that somehow loses
32 if this is not done before the other system files. */
33 #include "xterm.h"
34 #include <X11/cursorfont.h>
36 /* If we have Xfixes extension, use it for pointer blanking. */
37 #ifdef HAVE_XFIXES
38 #include <X11/extensions/Xfixes.h>
39 #endif
41 /* Using Xft implies that XRender is available. */
42 #ifdef HAVE_XFT
43 #include <X11/extensions/Xrender.h>
44 #endif
46 #ifdef HAVE_XDBE
47 #include <X11/extensions/Xdbe.h>
48 #endif
50 /* Load sys/types.h if not already loaded.
51 In some systems loading it twice is suicidal. */
52 #ifndef makedev
53 #include <sys/types.h>
54 #endif /* makedev */
56 #include <sys/ioctl.h>
58 #include "systime.h"
60 #include <fcntl.h>
61 #include <errno.h>
62 #include <sys/stat.h>
63 #include "character.h"
64 #include "coding.h"
65 #include "composite.h"
66 #include "frame.h"
67 #include "dispextern.h"
68 #include "xwidget.h"
69 #include "fontset.h"
70 #include "termhooks.h"
71 #include "termopts.h"
72 #include "termchar.h"
73 #include "emacs-icon.h"
74 #include "buffer.h"
75 #include "window.h"
76 #include "keyboard.h"
77 #include "atimer.h"
78 #include "font.h"
79 #include "xsettings.h"
80 #include "sysselect.h"
81 #include "menu.h"
83 #ifdef USE_X_TOOLKIT
84 #include <X11/Shell.h>
85 #endif
87 #include <unistd.h>
89 #ifdef USE_GTK
90 #include "gtkutil.h"
91 #ifdef HAVE_GTK3
92 #include <X11/Xproto.h>
93 #endif
94 #endif
96 #if defined (USE_LUCID) || defined (USE_MOTIF)
97 #include "../lwlib/xlwmenu.h"
98 #endif
100 #ifdef USE_X_TOOLKIT
102 /* Include toolkit specific headers for the scroll bar widget. */
104 #ifdef USE_TOOLKIT_SCROLL_BARS
105 #if defined USE_MOTIF
106 #include <Xm/Xm.h> /* For LESSTIF_VERSION */
107 #include <Xm/ScrollBar.h>
108 #else /* !USE_MOTIF i.e. use Xaw */
110 #ifdef HAVE_XAW3D
111 #include <X11/Xaw3d/Simple.h>
112 #include <X11/Xaw3d/Scrollbar.h>
113 #include <X11/Xaw3d/ThreeD.h>
114 #else /* !HAVE_XAW3D */
115 #include <X11/Xaw/Simple.h>
116 #include <X11/Xaw/Scrollbar.h>
117 #endif /* !HAVE_XAW3D */
118 #ifndef XtNpickTop
119 #define XtNpickTop "pickTop"
120 #endif /* !XtNpickTop */
121 #endif /* !USE_MOTIF */
122 #endif /* USE_TOOLKIT_SCROLL_BARS */
124 #endif /* USE_X_TOOLKIT */
126 #ifdef USE_X_TOOLKIT
127 #include "widget.h"
128 #ifndef XtNinitialState
129 #define XtNinitialState "initialState"
130 #endif
131 #endif
133 #include "bitmaps/gray.xbm"
135 #ifdef HAVE_XKB
136 #include <X11/XKBlib.h>
137 #endif
139 /* Default to using XIM if available. */
140 #ifdef USE_XIM
141 bool use_xim = true;
142 #else
143 bool use_xim = false; /* configure --without-xim */
144 #endif
146 /* Non-zero means that a HELP_EVENT has been generated since Emacs
147 start. */
149 static bool any_help_event_p;
151 /* This is a chain of structures for all the X displays currently in
152 use. */
154 struct x_display_info *x_display_list;
156 #ifdef USE_X_TOOLKIT
158 /* The application context for Xt use. */
159 XtAppContext Xt_app_con;
160 static String Xt_default_resources[] = {0};
162 /* Non-zero means user is interacting with a toolkit scroll bar. */
163 static bool toolkit_scroll_bar_interaction;
165 #endif /* USE_X_TOOLKIT */
167 /* Non-zero timeout value means ignore next mouse click if it arrives
168 before that timeout elapses (i.e. as part of the same sequence of
169 events resulting from clicking on a frame to select it). */
171 static Time ignore_next_mouse_click_timeout;
173 /* Used locally within XTread_socket. */
175 static int x_noop_count;
177 #ifdef USE_GTK
178 /* The name of the Emacs icon file. */
179 static Lisp_Object xg_default_icon_file;
180 #endif
182 /* Some functions take this as char *, not const char *. */
183 static char emacs_class[] = EMACS_CLASS;
185 enum xembed_info
187 XEMBED_MAPPED = 1 << 0
190 enum xembed_message
192 XEMBED_EMBEDDED_NOTIFY = 0,
193 XEMBED_WINDOW_ACTIVATE = 1,
194 XEMBED_WINDOW_DEACTIVATE = 2,
195 XEMBED_REQUEST_FOCUS = 3,
196 XEMBED_FOCUS_IN = 4,
197 XEMBED_FOCUS_OUT = 5,
198 XEMBED_FOCUS_NEXT = 6,
199 XEMBED_FOCUS_PREV = 7,
201 XEMBED_MODALITY_ON = 10,
202 XEMBED_MODALITY_OFF = 11,
203 XEMBED_REGISTER_ACCELERATOR = 12,
204 XEMBED_UNREGISTER_ACCELERATOR = 13,
205 XEMBED_ACTIVATE_ACCELERATOR = 14
208 static void x_free_cr_resources (struct frame *);
209 static bool x_alloc_nearest_color_1 (Display *, Colormap, XColor *);
210 static void x_raise_frame (struct frame *);
211 static void x_lower_frame (struct frame *);
212 static int x_io_error_quitter (Display *);
213 static struct terminal *x_create_terminal (struct x_display_info *);
214 static void x_frame_rehighlight (struct x_display_info *);
216 static void x_clip_to_row (struct window *, struct glyph_row *,
217 enum glyph_row_area, GC);
218 static struct scroll_bar *x_window_to_scroll_bar (Display *, Window, int);
219 static void x_scroll_bar_report_motion (struct frame **, Lisp_Object *,
220 enum scroll_bar_part *,
221 Lisp_Object *, Lisp_Object *,
222 Time *);
223 static void x_horizontal_scroll_bar_report_motion (struct frame **, Lisp_Object *,
224 enum scroll_bar_part *,
225 Lisp_Object *, Lisp_Object *,
226 Time *);
227 static bool x_handle_net_wm_state (struct frame *, const XPropertyEvent *);
228 static void x_check_fullscreen (struct frame *);
229 static void x_check_expected_move (struct frame *, int, int);
230 static void x_sync_with_move (struct frame *, int, int, bool);
231 static int handle_one_xevent (struct x_display_info *,
232 const XEvent *, int *,
233 struct input_event *);
234 #if ! (defined USE_X_TOOLKIT || defined USE_MOTIF)
235 static int x_dispatch_event (XEvent *, Display *);
236 #endif
237 static void x_wm_set_window_state (struct frame *, int);
238 static void x_wm_set_icon_pixmap (struct frame *, ptrdiff_t);
239 static void x_initialize (void);
241 static bool get_current_wm_state (struct frame *, Window, int *, bool *);
243 /* Flush display of frame F. */
245 static void
246 x_flush (struct frame *f)
248 eassert (f && FRAME_X_P (f));
249 /* Don't call XFlush when it is not safe to redisplay; the X
250 connection may be broken. */
251 if (!NILP (Vinhibit_redisplay))
252 return;
254 block_input ();
255 XFlush (FRAME_X_DISPLAY (f));
256 unblock_input ();
260 /* Remove calls to XFlush by defining XFlush to an empty replacement.
261 Calls to XFlush should be unnecessary because the X output buffer
262 is flushed automatically as needed by calls to XPending,
263 XNextEvent, or XWindowEvent according to the XFlush man page.
264 XTread_socket calls XPending. Removing XFlush improves
265 performance. */
267 #define XFlush(DISPLAY) (void) 0
270 /***********************************************************************
271 Debugging
272 ***********************************************************************/
274 #if false
276 /* This is a function useful for recording debugging information about
277 the sequence of occurrences in this file. */
279 struct record
281 char *locus;
282 int type;
285 struct record event_record[100];
287 int event_record_index;
289 void
290 record_event (char *locus, int type)
292 if (event_record_index == ARRAYELTS (event_record))
293 event_record_index = 0;
295 event_record[event_record_index].locus = locus;
296 event_record[event_record_index].type = type;
297 event_record_index++;
300 #endif
302 #ifdef USE_CAIRO
304 #define FRAME_CR_CONTEXT(f) ((f)->output_data.x->cr_context)
305 #define FRAME_CR_SURFACE(f) ((f)->output_data.x->cr_surface)
307 static struct x_gc_ext_data *
308 x_gc_get_ext_data (struct frame *f, GC gc, int create_if_not_found_p)
310 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
311 XEDataObject object;
312 XExtData **head, *ext_data;
314 object.gc = gc;
315 head = XEHeadOfExtensionList (object);
316 ext_data = XFindOnExtensionList (head, dpyinfo->ext_codes->extension);
317 if (ext_data == NULL)
319 if (!create_if_not_found_p)
320 return NULL;
321 else
323 ext_data = xzalloc (sizeof (*ext_data));
324 ext_data->number = dpyinfo->ext_codes->extension;
325 ext_data->private_data = xzalloc (sizeof (struct x_gc_ext_data));
326 XAddToExtensionList (head, ext_data);
329 return (struct x_gc_ext_data *) ext_data->private_data;
332 static void
333 x_extension_initialize (struct x_display_info *dpyinfo)
335 XExtCodes *ext_codes = XAddExtension (dpyinfo->display);
337 dpyinfo->ext_codes = ext_codes;
340 static void
341 x_cr_destroy_surface (struct frame *f)
343 if (FRAME_CR_SURFACE (f))
345 cairo_t *cr = FRAME_CR_CONTEXT (f);
346 cairo_surface_destroy (FRAME_CR_SURFACE (f));
347 FRAME_CR_SURFACE (f) = 0;
348 if (cr) cairo_destroy (cr);
349 FRAME_CR_CONTEXT (f) = NULL;
353 cairo_t *
354 x_begin_cr_clip (struct frame *f, GC gc)
356 cairo_t *cr = FRAME_CR_CONTEXT (f);
358 if (!cr)
361 if (! FRAME_CR_SURFACE (f))
363 cairo_surface_t *surface;
364 surface = cairo_xlib_surface_create (FRAME_X_DISPLAY (f),
365 FRAME_X_DRAWABLE (f),
366 FRAME_DISPLAY_INFO (f)->visual,
367 FRAME_PIXEL_WIDTH (f),
368 FRAME_PIXEL_HEIGHT (f));
369 cr = cairo_create (surface);
370 cairo_surface_destroy (surface);
372 else
373 cr = cairo_create (FRAME_CR_SURFACE (f));
374 FRAME_CR_CONTEXT (f) = cr;
376 cairo_save (cr);
378 if (gc)
380 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 0);
382 if (gc_ext && gc_ext->n_clip_rects)
384 int i;
386 for (i = 0; i < gc_ext->n_clip_rects; i++)
387 cairo_rectangle (cr, gc_ext->clip_rects[i].x,
388 gc_ext->clip_rects[i].y,
389 gc_ext->clip_rects[i].width,
390 gc_ext->clip_rects[i].height);
391 cairo_clip (cr);
395 return cr;
398 void
399 x_end_cr_clip (struct frame *f)
401 cairo_restore (FRAME_CR_CONTEXT (f));
404 void
405 x_set_cr_source_with_gc_foreground (struct frame *f, GC gc)
407 XGCValues xgcv;
408 XColor color;
410 XGetGCValues (FRAME_X_DISPLAY (f), gc, GCForeground, &xgcv);
411 color.pixel = xgcv.foreground;
412 x_query_color (f, &color);
413 cairo_set_source_rgb (FRAME_CR_CONTEXT (f), color.red / 65535.0,
414 color.green / 65535.0, color.blue / 65535.0);
417 void
418 x_set_cr_source_with_gc_background (struct frame *f, GC gc)
420 XGCValues xgcv;
421 XColor color;
423 XGetGCValues (FRAME_X_DISPLAY (f), gc, GCBackground, &xgcv);
424 color.pixel = xgcv.background;
425 x_query_color (f, &color);
426 cairo_set_source_rgb (FRAME_CR_CONTEXT (f), color.red / 65535.0,
427 color.green / 65535.0, color.blue / 65535.0);
430 /* Fringe bitmaps. */
432 static int max_fringe_bmp = 0;
433 static cairo_pattern_t **fringe_bmp = 0;
435 static void
436 x_cr_define_fringe_bitmap (int which, unsigned short *bits, int h, int wd)
438 int i, stride;
439 cairo_surface_t *surface;
440 unsigned char *data;
441 cairo_pattern_t *pattern;
443 if (which >= max_fringe_bmp)
445 i = max_fringe_bmp;
446 max_fringe_bmp = which + 20;
447 fringe_bmp = (cairo_pattern_t **) xrealloc (fringe_bmp, max_fringe_bmp * sizeof (cairo_pattern_t *));
448 while (i < max_fringe_bmp)
449 fringe_bmp[i++] = 0;
452 block_input ();
454 surface = cairo_image_surface_create (CAIRO_FORMAT_A1, wd, h);
455 stride = cairo_image_surface_get_stride (surface);
456 data = cairo_image_surface_get_data (surface);
458 for (i = 0; i < h; i++)
460 *((unsigned short *) data) = bits[i];
461 data += stride;
464 cairo_surface_mark_dirty (surface);
465 pattern = cairo_pattern_create_for_surface (surface);
466 cairo_surface_destroy (surface);
468 unblock_input ();
470 fringe_bmp[which] = pattern;
473 static void
474 x_cr_destroy_fringe_bitmap (int which)
476 if (which >= max_fringe_bmp)
477 return;
479 if (fringe_bmp[which])
481 block_input ();
482 cairo_pattern_destroy (fringe_bmp[which]);
483 unblock_input ();
485 fringe_bmp[which] = 0;
488 static void
489 x_cr_draw_image (struct frame *f, GC gc, cairo_pattern_t *image,
490 int src_x, int src_y, int width, int height,
491 int dest_x, int dest_y, bool overlay_p)
493 cairo_t *cr;
494 cairo_matrix_t matrix;
495 cairo_surface_t *surface;
496 cairo_format_t format;
498 cr = x_begin_cr_clip (f, gc);
499 if (overlay_p)
500 cairo_rectangle (cr, dest_x, dest_y, width, height);
501 else
503 x_set_cr_source_with_gc_background (f, gc);
504 cairo_rectangle (cr, dest_x, dest_y, width, height);
505 cairo_fill_preserve (cr);
507 cairo_clip (cr);
508 cairo_matrix_init_translate (&matrix, src_x - dest_x, src_y - dest_y);
509 cairo_pattern_set_matrix (image, &matrix);
510 cairo_pattern_get_surface (image, &surface);
511 format = cairo_image_surface_get_format (surface);
512 if (format != CAIRO_FORMAT_A8 && format != CAIRO_FORMAT_A1)
514 cairo_set_source (cr, image);
515 cairo_fill (cr);
517 else
519 x_set_cr_source_with_gc_foreground (f, gc);
520 cairo_mask (cr, image);
522 x_end_cr_clip (f);
525 void
526 x_cr_draw_frame (cairo_t *cr, struct frame *f)
528 int width, height;
530 width = FRAME_PIXEL_WIDTH (f);
531 height = FRAME_PIXEL_HEIGHT (f);
533 x_free_cr_resources (f);
534 FRAME_CR_CONTEXT (f) = cr;
535 x_clear_area (f, 0, 0, width, height);
536 expose_frame (f, 0, 0, width, height);
537 FRAME_CR_CONTEXT (f) = NULL;
540 static cairo_status_t
541 x_cr_accumulate_data (void *closure, const unsigned char *data,
542 unsigned int length)
544 Lisp_Object *acc = (Lisp_Object *) closure;
546 *acc = Fcons (make_unibyte_string ((char const *) data, length), *acc);
548 return CAIRO_STATUS_SUCCESS;
551 static void
552 x_cr_destroy (Lisp_Object arg)
554 cairo_t *cr = (cairo_t *) XSAVE_POINTER (arg, 0);
556 block_input ();
557 cairo_destroy (cr);
558 unblock_input ();
561 Lisp_Object
562 x_cr_export_frames (Lisp_Object frames, cairo_surface_type_t surface_type)
564 struct frame *f;
565 cairo_surface_t *surface;
566 cairo_t *cr;
567 int width, height;
568 void (*surface_set_size_func) (cairo_surface_t *, double, double) = NULL;
569 Lisp_Object acc = Qnil;
570 ptrdiff_t count = SPECPDL_INDEX ();
572 specbind (Qredisplay_dont_pause, Qt);
573 redisplay_preserve_echo_area (31);
575 f = XFRAME (XCAR (frames));
576 frames = XCDR (frames);
577 width = FRAME_PIXEL_WIDTH (f);
578 height = FRAME_PIXEL_HEIGHT (f);
580 block_input ();
581 #ifdef CAIRO_HAS_PDF_SURFACE
582 if (surface_type == CAIRO_SURFACE_TYPE_PDF)
584 surface = cairo_pdf_surface_create_for_stream (x_cr_accumulate_data, &acc,
585 width, height);
586 surface_set_size_func = cairo_pdf_surface_set_size;
588 else
589 #endif
590 #ifdef CAIRO_HAS_PNG_FUNCTIONS
591 if (surface_type == CAIRO_SURFACE_TYPE_IMAGE)
592 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
593 else
594 #endif
595 #ifdef CAIRO_HAS_PS_SURFACE
596 if (surface_type == CAIRO_SURFACE_TYPE_PS)
598 surface = cairo_ps_surface_create_for_stream (x_cr_accumulate_data, &acc,
599 width, height);
600 surface_set_size_func = cairo_ps_surface_set_size;
602 else
603 #endif
604 #ifdef CAIRO_HAS_SVG_SURFACE
605 if (surface_type == CAIRO_SURFACE_TYPE_SVG)
606 surface = cairo_svg_surface_create_for_stream (x_cr_accumulate_data, &acc,
607 width, height);
608 else
609 #endif
610 abort ();
612 cr = cairo_create (surface);
613 cairo_surface_destroy (surface);
614 record_unwind_protect (x_cr_destroy, make_save_ptr (cr));
616 while (1)
618 x_free_cr_resources (f);
619 FRAME_CR_CONTEXT (f) = cr;
620 x_clear_area (f, 0, 0, width, height);
621 expose_frame (f, 0, 0, width, height);
622 FRAME_CR_CONTEXT (f) = NULL;
624 if (NILP (frames))
625 break;
627 cairo_surface_show_page (surface);
628 f = XFRAME (XCAR (frames));
629 frames = XCDR (frames);
630 width = FRAME_PIXEL_WIDTH (f);
631 height = FRAME_PIXEL_HEIGHT (f);
632 if (surface_set_size_func)
633 (*surface_set_size_func) (surface, width, height);
635 unblock_input ();
636 maybe_quit ();
637 block_input ();
640 #ifdef CAIRO_HAS_PNG_FUNCTIONS
641 if (surface_type == CAIRO_SURFACE_TYPE_IMAGE)
643 cairo_surface_flush (surface);
644 cairo_surface_write_to_png_stream (surface, x_cr_accumulate_data, &acc);
646 #endif
647 unblock_input ();
649 unbind_to (count, Qnil);
651 return CALLN (Fapply, intern ("concat"), Fnreverse (acc));
654 #endif /* USE_CAIRO */
656 static void
657 x_free_cr_resources (struct frame *f)
659 #ifdef USE_CAIRO
660 if (f == NULL)
662 Lisp_Object rest, frame;
663 FOR_EACH_FRAME (rest, frame)
664 if (FRAME_X_P (XFRAME (frame)))
665 x_free_cr_resources (XFRAME (frame));
667 else
669 cairo_t *cr = FRAME_CR_CONTEXT (f);
671 if (cr)
673 cairo_surface_t *surface = cairo_get_target (cr);
675 if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_XLIB)
677 cairo_destroy (cr);
678 FRAME_CR_CONTEXT (f) = NULL;
682 #endif
685 static void
686 x_set_clip_rectangles (struct frame *f, GC gc, XRectangle *rectangles, int n)
688 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, rectangles, n, Unsorted);
689 #ifdef USE_CAIRO
690 eassert (n >= 0 && n <= MAX_CLIP_RECTS);
693 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 1);
695 gc_ext->n_clip_rects = n;
696 memcpy (gc_ext->clip_rects, rectangles, sizeof (XRectangle) * n);
698 #endif
701 static void
702 x_reset_clip_rectangles (struct frame *f, GC gc)
704 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
705 #ifdef USE_CAIRO
707 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 0);
709 if (gc_ext)
710 gc_ext->n_clip_rects = 0;
712 #endif
715 static void
716 x_fill_rectangle (struct frame *f, GC gc, int x, int y, int width, int height)
718 #ifdef USE_CAIRO
719 cairo_t *cr;
721 cr = x_begin_cr_clip (f, gc);
722 x_set_cr_source_with_gc_foreground (f, gc);
723 cairo_rectangle (cr, x, y, width, height);
724 cairo_fill (cr);
725 x_end_cr_clip (f);
726 #else
727 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
728 gc, x, y, width, height);
729 #endif
732 static void
733 x_draw_rectangle (struct frame *f, GC gc, int x, int y, int width, int height)
735 #ifdef USE_CAIRO
736 cairo_t *cr;
738 cr = x_begin_cr_clip (f, gc);
739 x_set_cr_source_with_gc_foreground (f, gc);
740 cairo_rectangle (cr, x + 0.5, y + 0.5, width, height);
741 cairo_set_line_width (cr, 1);
742 cairo_stroke (cr);
743 x_end_cr_clip (f);
744 #else
745 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
746 gc, x, y, width, height);
747 #endif
750 static void
751 x_clear_window (struct frame *f)
753 #ifdef USE_CAIRO
754 cairo_t *cr;
756 cr = x_begin_cr_clip (f, NULL);
757 x_set_cr_source_with_gc_background (f, f->output_data.x->normal_gc);
758 cairo_paint (cr);
759 x_end_cr_clip (f);
760 #else
761 if (FRAME_X_DOUBLE_BUFFERED_P (f))
762 x_clear_area (f, 0, 0, FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f));
763 else
764 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
765 #endif
768 #ifdef USE_CAIRO
769 static void
770 x_fill_trapezoid_for_relief (struct frame *f, GC gc, int x, int y,
771 int width, int height, int top_p)
773 cairo_t *cr;
775 cr = x_begin_cr_clip (f, gc);
776 x_set_cr_source_with_gc_foreground (f, gc);
777 cairo_move_to (cr, top_p ? x : x + height, y);
778 cairo_line_to (cr, x, y + height);
779 cairo_line_to (cr, top_p ? x + width - height : x + width, y + height);
780 cairo_line_to (cr, x + width, y);
781 cairo_fill (cr);
782 x_end_cr_clip (f);
785 enum corners
787 CORNER_BOTTOM_RIGHT, /* 0 -> pi/2 */
788 CORNER_BOTTOM_LEFT, /* pi/2 -> pi */
789 CORNER_TOP_LEFT, /* pi -> 3pi/2 */
790 CORNER_TOP_RIGHT, /* 3pi/2 -> 2pi */
791 CORNER_LAST
794 static void
795 x_erase_corners_for_relief (struct frame *f, GC gc, int x, int y,
796 int width, int height,
797 double radius, double margin, int corners)
799 cairo_t *cr;
800 int i;
802 cr = x_begin_cr_clip (f, gc);
803 x_set_cr_source_with_gc_background (f, gc);
804 for (i = 0; i < CORNER_LAST; i++)
805 if (corners & (1 << i))
807 double xm, ym, xc, yc;
809 if (i == CORNER_TOP_LEFT || i == CORNER_BOTTOM_LEFT)
810 xm = x - margin, xc = xm + radius;
811 else
812 xm = x + width + margin, xc = xm - radius;
813 if (i == CORNER_TOP_LEFT || i == CORNER_TOP_RIGHT)
814 ym = y - margin, yc = ym + radius;
815 else
816 ym = y + height + margin, yc = ym - radius;
818 cairo_move_to (cr, xm, ym);
819 cairo_arc (cr, xc, yc, radius, i * M_PI_2, (i + 1) * M_PI_2);
821 cairo_clip (cr);
822 cairo_rectangle (cr, x, y, width, height);
823 cairo_fill (cr);
824 x_end_cr_clip (f);
827 static void
828 x_draw_horizontal_wave (struct frame *f, GC gc, int x, int y,
829 int width, int height, int wave_length)
831 cairo_t *cr;
832 double dx = wave_length, dy = height - 1;
833 int xoffset, n;
835 cr = x_begin_cr_clip (f, gc);
836 x_set_cr_source_with_gc_foreground (f, gc);
837 cairo_rectangle (cr, x, y, width, height);
838 cairo_clip (cr);
840 if (x >= 0)
842 xoffset = x % (wave_length * 2);
843 if (xoffset == 0)
844 xoffset = wave_length * 2;
846 else
847 xoffset = x % (wave_length * 2) + wave_length * 2;
848 n = (width + xoffset) / wave_length + 1;
849 if (xoffset > wave_length)
851 xoffset -= wave_length;
852 --n;
853 y += height - 1;
854 dy = -dy;
857 cairo_move_to (cr, x - xoffset + 0.5, y + 0.5);
858 while (--n >= 0)
860 cairo_rel_line_to (cr, dx, dy);
861 dy = -dy;
863 cairo_set_line_width (cr, 1);
864 cairo_stroke (cr);
865 x_end_cr_clip (f);
867 #endif
870 /* Return the struct x_display_info corresponding to DPY. */
872 struct x_display_info *
873 x_display_info_for_display (Display *dpy)
875 struct x_display_info *dpyinfo;
877 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
878 if (dpyinfo->display == dpy)
879 return dpyinfo;
881 return 0;
884 static Window
885 x_find_topmost_parent (struct frame *f)
887 struct x_output *x = f->output_data.x;
888 Window win = None, wi = x->parent_desc;
889 Display *dpy = FRAME_X_DISPLAY (f);
891 while (wi != FRAME_DISPLAY_INFO (f)->root_window)
893 Window root;
894 Window *children;
895 unsigned int nchildren;
897 win = wi;
898 if (XQueryTree (dpy, win, &root, &wi, &children, &nchildren))
899 XFree (children);
900 else
901 break;
904 return win;
907 #define OPAQUE 0xffffffff
909 void
910 x_set_frame_alpha (struct frame *f)
912 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
913 Display *dpy = FRAME_X_DISPLAY (f);
914 Window win = FRAME_OUTER_WINDOW (f);
915 double alpha = 1.0;
916 double alpha_min = 1.0;
917 unsigned long opac;
918 Window parent;
920 if (dpyinfo->x_highlight_frame == f)
921 alpha = f->alpha[0];
922 else
923 alpha = f->alpha[1];
925 if (FLOATP (Vframe_alpha_lower_limit))
926 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
927 else if (INTEGERP (Vframe_alpha_lower_limit))
928 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
930 if (alpha < 0.0)
931 return;
932 else if (alpha > 1.0)
933 alpha = 1.0;
934 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
935 alpha = alpha_min;
937 opac = alpha * OPAQUE;
939 x_catch_errors (dpy);
941 /* If there is a parent from the window manager, put the property there
942 also, to work around broken window managers that fail to do that.
943 Do this unconditionally as this function is called on reparent when
944 alpha has not changed on the frame. */
946 if (!FRAME_PARENT_FRAME (f))
948 parent = x_find_topmost_parent (f);
949 if (parent != None)
950 XChangeProperty (dpy, parent, dpyinfo->Xatom_net_wm_window_opacity,
951 XA_CARDINAL, 32, PropModeReplace,
952 (unsigned char *) &opac, 1);
955 /* return unless necessary */
957 unsigned char *data;
958 Atom actual;
959 int rc, format;
960 unsigned long n, left;
962 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
963 0, 1, False, XA_CARDINAL,
964 &actual, &format, &n, &left,
965 &data);
967 if (rc == Success && actual != None)
969 unsigned long value = *(unsigned long *)data;
970 XFree (data);
971 if (value == opac)
973 x_uncatch_errors ();
974 return;
979 XChangeProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
980 XA_CARDINAL, 32, PropModeReplace,
981 (unsigned char *) &opac, 1);
982 x_uncatch_errors ();
985 /***********************************************************************
986 Starting and ending an update
987 ***********************************************************************/
989 /* Start an update of frame F. This function is installed as a hook
990 for update_begin, i.e. it is called when update_begin is called.
991 This function is called prior to calls to x_update_window_begin for
992 each window being updated. Currently, there is nothing to do here
993 because all interesting stuff is done on a window basis. */
995 static void
996 x_update_begin (struct frame *f)
998 #ifdef USE_CAIRO
999 if (! NILP (tip_frame) && XFRAME (tip_frame) == f
1000 && ! FRAME_VISIBLE_P (f))
1001 return;
1003 if (! FRAME_CR_SURFACE (f))
1005 int width, height;
1006 #ifdef USE_GTK
1007 if (FRAME_GTK_WIDGET (f))
1009 GdkWindow *w = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
1010 width = gdk_window_get_width (w);
1011 height = gdk_window_get_height (w);
1013 else
1014 #endif
1016 width = FRAME_PIXEL_WIDTH (f);
1017 height = FRAME_PIXEL_HEIGHT (f);
1018 if (! FRAME_EXTERNAL_TOOL_BAR (f))
1019 height += FRAME_TOOL_BAR_HEIGHT (f);
1020 if (! FRAME_EXTERNAL_MENU_BAR (f))
1021 height += FRAME_MENU_BAR_HEIGHT (f);
1024 if (width > 0 && height > 0)
1026 block_input();
1027 FRAME_CR_SURFACE (f) = cairo_image_surface_create
1028 (CAIRO_FORMAT_ARGB32, width, height);
1029 unblock_input();
1032 #endif /* USE_CAIRO */
1035 /* Start update of window W. */
1037 static void
1038 x_update_window_begin (struct window *w)
1040 struct frame *f = XFRAME (WINDOW_FRAME (w));
1041 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
1043 w->output_cursor = w->cursor;
1045 block_input ();
1047 if (f == hlinfo->mouse_face_mouse_frame)
1049 /* Don't do highlighting for mouse motion during the update. */
1050 hlinfo->mouse_face_defer = true;
1052 /* If F needs to be redrawn, simply forget about any prior mouse
1053 highlighting. */
1054 if (FRAME_GARBAGED_P (f))
1055 hlinfo->mouse_face_window = Qnil;
1058 unblock_input ();
1062 /* Draw a vertical window border from (x,y0) to (x,y1) */
1064 static void
1065 x_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
1067 struct frame *f = XFRAME (WINDOW_FRAME (w));
1068 struct face *face;
1070 face = FACE_FROM_ID_OR_NULL (f, VERTICAL_BORDER_FACE_ID);
1071 if (face)
1072 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
1073 face->foreground);
1075 #ifdef USE_CAIRO
1076 x_fill_rectangle (f, f->output_data.x->normal_gc, x, y0, 1, y1 - y0);
1077 #else
1078 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
1079 f->output_data.x->normal_gc, x, y0, x, y1);
1080 #endif
1083 /* Draw a window divider from (x0,y0) to (x1,y1) */
1085 static void
1086 x_draw_window_divider (struct window *w, int x0, int x1, int y0, int y1)
1088 struct frame *f = XFRAME (WINDOW_FRAME (w));
1089 struct face *face = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_FACE_ID);
1090 struct face *face_first
1091 = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
1092 struct face *face_last
1093 = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
1094 unsigned long color = face ? face->foreground : FRAME_FOREGROUND_PIXEL (f);
1095 unsigned long color_first = (face_first
1096 ? face_first->foreground
1097 : FRAME_FOREGROUND_PIXEL (f));
1098 unsigned long color_last = (face_last
1099 ? face_last->foreground
1100 : FRAME_FOREGROUND_PIXEL (f));
1101 Display *display = FRAME_X_DISPLAY (f);
1103 if (y1 - y0 > x1 - x0 && x1 - x0 > 2)
1104 /* Vertical. */
1106 XSetForeground (display, f->output_data.x->normal_gc, color_first);
1107 x_fill_rectangle (f, f->output_data.x->normal_gc,
1108 x0, y0, 1, y1 - y0);
1109 XSetForeground (display, f->output_data.x->normal_gc, color);
1110 x_fill_rectangle (f, f->output_data.x->normal_gc,
1111 x0 + 1, y0, x1 - x0 - 2, y1 - y0);
1112 XSetForeground (display, f->output_data.x->normal_gc, color_last);
1113 x_fill_rectangle (f, f->output_data.x->normal_gc,
1114 x1 - 1, y0, 1, y1 - y0);
1116 else if (x1 - x0 > y1 - y0 && y1 - y0 > 3)
1117 /* Horizontal. */
1119 XSetForeground (display, f->output_data.x->normal_gc, color_first);
1120 x_fill_rectangle (f, f->output_data.x->normal_gc,
1121 x0, y0, x1 - x0, 1);
1122 XSetForeground (display, f->output_data.x->normal_gc, color);
1123 x_fill_rectangle (f, f->output_data.x->normal_gc,
1124 x0, y0 + 1, x1 - x0, y1 - y0 - 2);
1125 XSetForeground (display, f->output_data.x->normal_gc, color_last);
1126 x_fill_rectangle (f, f->output_data.x->normal_gc,
1127 x0, y1 - 1, x1 - x0, 1);
1129 else
1131 XSetForeground (display, f->output_data.x->normal_gc, color);
1132 x_fill_rectangle (f, f->output_data.x->normal_gc,
1133 x0, y0, x1 - x0, y1 - y0);
1137 /* End update of window W.
1139 Draw vertical borders between horizontally adjacent windows, and
1140 display W's cursor if CURSOR_ON_P is non-zero.
1142 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
1143 glyphs in mouse-face were overwritten. In that case we have to
1144 make sure that the mouse-highlight is properly redrawn.
1146 W may be a menu bar pseudo-window in case we don't have X toolkit
1147 support. Such windows don't have a cursor, so don't display it
1148 here. */
1150 static void
1151 x_update_window_end (struct window *w, bool cursor_on_p,
1152 bool mouse_face_overwritten_p)
1154 if (!w->pseudo_window_p)
1156 block_input ();
1158 if (cursor_on_p)
1159 display_and_set_cursor (w, true,
1160 w->output_cursor.hpos, w->output_cursor.vpos,
1161 w->output_cursor.x, w->output_cursor.y);
1163 if (draw_window_fringes (w, true))
1165 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
1166 x_draw_right_divider (w);
1167 else
1168 x_draw_vertical_border (w);
1171 unblock_input ();
1174 /* If a row with mouse-face was overwritten, arrange for
1175 XTframe_up_to_date to redisplay the mouse highlight. */
1176 if (mouse_face_overwritten_p)
1178 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
1180 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
1181 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
1182 hlinfo->mouse_face_window = Qnil;
1186 /* Show the frame back buffer. If frame is double-buffered,
1187 atomically publish to the user's screen graphics updates made since
1188 the last call to show_back_buffer. */
1189 static void
1190 show_back_buffer (struct frame *f)
1192 block_input ();
1193 if (FRAME_X_DOUBLE_BUFFERED_P (f))
1195 #ifdef HAVE_XDBE
1196 XdbeSwapInfo swap_info;
1197 memset (&swap_info, 0, sizeof (swap_info));
1198 swap_info.swap_window = FRAME_X_WINDOW (f);
1199 swap_info.swap_action = XdbeCopied;
1200 XdbeSwapBuffers (FRAME_X_DISPLAY (f), &swap_info, 1);
1201 #else
1202 eassert (!"should have back-buffer only with XDBE");
1203 #endif
1205 FRAME_X_NEED_BUFFER_FLIP (f) = false;
1206 unblock_input ();
1209 /* Updates back buffer and flushes changes to display. Called from
1210 minibuf read code. Note that we display the back buffer even if
1211 buffer flipping is blocked. */
1212 static void
1213 x_flip_and_flush (struct frame *f)
1215 block_input ();
1216 if (FRAME_X_NEED_BUFFER_FLIP (f))
1217 show_back_buffer (f);
1218 x_flush (f);
1219 unblock_input ();
1222 /* End update of frame F. This function is installed as a hook in
1223 update_end. */
1225 static void
1226 x_update_end (struct frame *f)
1228 /* Mouse highlight may be displayed again. */
1229 MOUSE_HL_INFO (f)->mouse_face_defer = false;
1231 #ifdef USE_CAIRO
1232 if (FRAME_CR_SURFACE (f))
1234 cairo_t *cr = 0;
1235 block_input();
1236 #if defined (USE_GTK) && defined (HAVE_GTK3)
1237 if (FRAME_GTK_WIDGET (f))
1239 GdkWindow *w = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
1240 cr = gdk_cairo_create (w);
1242 else
1243 #endif
1245 cairo_surface_t *surface;
1246 int width = FRAME_PIXEL_WIDTH (f);
1247 int height = FRAME_PIXEL_HEIGHT (f);
1248 if (! FRAME_EXTERNAL_TOOL_BAR (f))
1249 height += FRAME_TOOL_BAR_HEIGHT (f);
1250 if (! FRAME_EXTERNAL_MENU_BAR (f))
1251 height += FRAME_MENU_BAR_HEIGHT (f);
1252 surface = cairo_xlib_surface_create (FRAME_X_DISPLAY (f),
1253 FRAME_X_DRAWABLE (f),
1254 FRAME_DISPLAY_INFO (f)->visual,
1255 width,
1256 height);
1257 cr = cairo_create (surface);
1258 cairo_surface_destroy (surface);
1261 cairo_set_source_surface (cr, FRAME_CR_SURFACE (f), 0, 0);
1262 cairo_paint (cr);
1263 cairo_destroy (cr);
1264 unblock_input ();
1266 #endif
1268 #ifndef XFlush
1269 block_input ();
1270 XFlush (FRAME_X_DISPLAY (f));
1271 unblock_input ();
1272 #endif
1275 /* This function is called from various places in xdisp.c
1276 whenever a complete update has been performed. */
1278 static void
1279 XTframe_up_to_date (struct frame *f)
1281 eassert (FRAME_X_P (f));
1282 block_input ();
1283 FRAME_MOUSE_UPDATE (f);
1284 if (!buffer_flipping_blocked_p () && FRAME_X_NEED_BUFFER_FLIP (f))
1285 show_back_buffer (f);
1286 unblock_input ();
1289 static void
1290 XTbuffer_flipping_unblocked_hook (struct frame *f)
1292 if (FRAME_X_NEED_BUFFER_FLIP (f))
1293 show_back_buffer (f);
1297 * x_clear_under_internal_border:
1299 * Clear area of frame F's internal border. If the internal border face
1300 * of F has been specified (is not null), fill the area with that face.
1302 void
1303 x_clear_under_internal_border (struct frame *f)
1305 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
1307 int border = FRAME_INTERNAL_BORDER_WIDTH (f);
1308 int width = FRAME_PIXEL_WIDTH (f);
1309 int height = FRAME_PIXEL_HEIGHT (f);
1310 #ifdef USE_GTK
1311 int margin = 0;
1312 #else
1313 int margin = FRAME_TOP_MARGIN_HEIGHT (f);
1314 #endif
1315 struct face *face = FACE_FROM_ID_OR_NULL (f, INTERNAL_BORDER_FACE_ID);
1317 block_input ();
1319 if (face)
1321 unsigned long color = face->background;
1322 Display *display = FRAME_X_DISPLAY (f);
1323 GC gc = f->output_data.x->normal_gc;
1325 XSetForeground (display, gc, color);
1326 x_fill_rectangle (f, gc, 0, margin, width, border);
1327 x_fill_rectangle (f, gc, 0, 0, border, height);
1328 x_fill_rectangle (f, gc, width - border, 0, border, height);
1329 x_fill_rectangle (f, gc, 0, height - border, width, border);
1330 XSetForeground (display, gc, FRAME_FOREGROUND_PIXEL (f));
1332 else
1334 x_clear_area (f, 0, 0, border, height);
1335 x_clear_area (f, 0, margin, width, border);
1336 x_clear_area (f, width - border, 0, border, height);
1337 x_clear_area (f, 0, height - border, width, border);
1340 unblock_input ();
1344 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
1345 arrow bitmaps, or clear the fringes if no bitmaps are required
1346 before DESIRED_ROW is made current. This function is called from
1347 update_window_line only if it is known that there are differences
1348 between bitmaps to be drawn between current row and DESIRED_ROW. */
1350 static void
1351 x_after_update_window_line (struct window *w, struct glyph_row *desired_row)
1353 eassert (w);
1355 if (!desired_row->mode_line_p && !w->pseudo_window_p)
1356 desired_row->redraw_fringe_bitmaps_p = true;
1358 #ifdef USE_X_TOOLKIT
1359 /* When a window has disappeared, make sure that no rest of
1360 full-width rows stays visible in the internal border. Could
1361 check here if updated window is the leftmost/rightmost window,
1362 but I guess it's not worth doing since vertically split windows
1363 are almost never used, internal border is rarely set, and the
1364 overhead is very small. */
1366 struct frame *f;
1367 int width, height;
1369 if (windows_or_buffers_changed
1370 && desired_row->full_width_p
1371 && (f = XFRAME (w->frame),
1372 width = FRAME_INTERNAL_BORDER_WIDTH (f),
1373 width != 0)
1374 && (height = desired_row->visible_height,
1375 height > 0))
1377 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
1378 struct face *face = FACE_FROM_ID_OR_NULL (f, INTERNAL_BORDER_FACE_ID);
1380 block_input ();
1381 if (face)
1383 unsigned long color = face->background;
1384 Display *display = FRAME_X_DISPLAY (f);
1386 XSetForeground (display, f->output_data.x->normal_gc, color);
1387 x_fill_rectangle (f, f->output_data.x->normal_gc,
1388 0, y, width, height);
1389 x_fill_rectangle (f, f->output_data.x->normal_gc,
1390 FRAME_PIXEL_WIDTH (f) - width, y, width, height);
1392 else
1394 x_clear_area (f, 0, y, width, height);
1395 x_clear_area (f, FRAME_PIXEL_WIDTH (f) - width, y, width, height);
1397 unblock_input ();
1400 #endif
1403 static void
1404 x_draw_fringe_bitmap (struct window *w, struct glyph_row *row, struct draw_fringe_bitmap_params *p)
1406 struct frame *f = XFRAME (WINDOW_FRAME (w));
1407 Display *display = FRAME_X_DISPLAY (f);
1408 GC gc = f->output_data.x->normal_gc;
1409 struct face *face = p->face;
1411 /* Must clip because of partially visible lines. */
1412 x_clip_to_row (w, row, ANY_AREA, gc);
1414 if (p->bx >= 0 && !p->overlay_p)
1416 /* In case the same realized face is used for fringes and
1417 for something displayed in the text (e.g. face `region' on
1418 mono-displays, the fill style may have been changed to
1419 FillSolid in x_draw_glyph_string_background. */
1420 if (face->stipple)
1421 XSetFillStyle (display, face->gc, FillOpaqueStippled);
1422 else
1423 XSetForeground (display, face->gc, face->background);
1425 x_fill_rectangle (f, face->gc, p->bx, p->by, p->nx, p->ny);
1427 if (!face->stipple)
1428 XSetForeground (display, face->gc, face->foreground);
1431 #ifdef USE_CAIRO
1432 if (p->which && p->which < max_fringe_bmp)
1434 XGCValues gcv;
1436 XGetGCValues (display, gc, GCForeground | GCBackground, &gcv);
1437 XSetForeground (display, gc, (p->cursor_p
1438 ? (p->overlay_p ? face->background
1439 : f->output_data.x->cursor_pixel)
1440 : face->foreground));
1441 XSetBackground (display, gc, face->background);
1442 x_cr_draw_image (f, gc, fringe_bmp[p->which], 0, p->dh,
1443 p->wd, p->h, p->x, p->y, p->overlay_p);
1444 XSetForeground (display, gc, gcv.foreground);
1445 XSetBackground (display, gc, gcv.background);
1447 #else /* not USE_CAIRO */
1448 if (p->which)
1450 Drawable drawable = FRAME_X_DRAWABLE (f);
1451 char *bits;
1452 Pixmap pixmap, clipmask = (Pixmap) 0;
1453 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
1454 XGCValues gcv;
1456 if (p->wd > 8)
1457 bits = (char *) (p->bits + p->dh);
1458 else
1459 bits = (char *) p->bits + p->dh;
1461 /* Draw the bitmap. I believe these small pixmaps can be cached
1462 by the server. */
1463 pixmap = XCreatePixmapFromBitmapData (display, drawable, bits, p->wd, p->h,
1464 (p->cursor_p
1465 ? (p->overlay_p ? face->background
1466 : f->output_data.x->cursor_pixel)
1467 : face->foreground),
1468 face->background, depth);
1470 if (p->overlay_p)
1472 clipmask = XCreatePixmapFromBitmapData (display,
1473 FRAME_DISPLAY_INFO (f)->root_window,
1474 bits, p->wd, p->h,
1475 1, 0, 1);
1476 gcv.clip_mask = clipmask;
1477 gcv.clip_x_origin = p->x;
1478 gcv.clip_y_origin = p->y;
1479 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
1482 XCopyArea (display, pixmap, drawable, gc, 0, 0,
1483 p->wd, p->h, p->x, p->y);
1484 XFreePixmap (display, pixmap);
1486 if (p->overlay_p)
1488 gcv.clip_mask = (Pixmap) 0;
1489 XChangeGC (display, gc, GCClipMask, &gcv);
1490 XFreePixmap (display, clipmask);
1493 #endif /* not USE_CAIRO */
1495 x_reset_clip_rectangles (f, gc);
1498 /***********************************************************************
1499 Glyph display
1500 ***********************************************************************/
1504 static void x_set_glyph_string_clipping (struct glyph_string *);
1505 static void x_set_glyph_string_gc (struct glyph_string *);
1506 static void x_draw_glyph_string_foreground (struct glyph_string *);
1507 static void x_draw_composite_glyph_string_foreground (struct glyph_string *);
1508 static void x_draw_glyph_string_box (struct glyph_string *);
1509 static void x_draw_glyph_string (struct glyph_string *);
1510 static _Noreturn void x_delete_glyphs (struct frame *, int);
1511 static void x_compute_glyph_string_overhangs (struct glyph_string *);
1512 static void x_set_cursor_gc (struct glyph_string *);
1513 static void x_set_mode_line_face_gc (struct glyph_string *);
1514 static void x_set_mouse_face_gc (struct glyph_string *);
1515 static bool x_alloc_lighter_color (struct frame *, Display *, Colormap,
1516 unsigned long *, double, int);
1517 static void x_setup_relief_color (struct frame *, struct relief *,
1518 double, int, unsigned long);
1519 static void x_setup_relief_colors (struct glyph_string *);
1520 static void x_draw_image_glyph_string (struct glyph_string *);
1521 static void x_draw_image_relief (struct glyph_string *);
1522 static void x_draw_image_foreground (struct glyph_string *);
1523 static void x_draw_image_foreground_1 (struct glyph_string *, Pixmap);
1524 static void x_clear_glyph_string_rect (struct glyph_string *, int,
1525 int, int, int);
1526 static void x_draw_relief_rect (struct frame *, int, int, int, int,
1527 int, bool, bool, bool, bool, bool,
1528 XRectangle *);
1529 static void x_draw_box_rect (struct glyph_string *, int, int, int, int,
1530 int, bool, bool, XRectangle *);
1531 static void x_scroll_bar_clear (struct frame *);
1533 #ifdef GLYPH_DEBUG
1534 static void x_check_font (struct frame *, struct font *);
1535 #endif
1538 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
1539 face. */
1541 static void
1542 x_set_cursor_gc (struct glyph_string *s)
1544 if (s->font == FRAME_FONT (s->f)
1545 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
1546 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
1547 && !s->cmp)
1548 s->gc = s->f->output_data.x->cursor_gc;
1549 else
1551 /* Cursor on non-default face: must merge. */
1552 XGCValues xgcv;
1553 unsigned long mask;
1555 xgcv.background = s->f->output_data.x->cursor_pixel;
1556 xgcv.foreground = s->face->background;
1558 /* If the glyph would be invisible, try a different foreground. */
1559 if (xgcv.foreground == xgcv.background)
1560 xgcv.foreground = s->face->foreground;
1561 if (xgcv.foreground == xgcv.background)
1562 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1563 if (xgcv.foreground == xgcv.background)
1564 xgcv.foreground = s->face->foreground;
1566 /* Make sure the cursor is distinct from text in this face. */
1567 if (xgcv.background == s->face->background
1568 && xgcv.foreground == s->face->foreground)
1570 xgcv.background = s->face->foreground;
1571 xgcv.foreground = s->face->background;
1574 IF_DEBUG (x_check_font (s->f, s->font));
1575 xgcv.graphics_exposures = False;
1576 mask = GCForeground | GCBackground | GCGraphicsExposures;
1578 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1579 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1580 mask, &xgcv);
1581 else
1582 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1583 = XCreateGC (s->display, FRAME_X_DRAWABLE (s->f), mask, &xgcv);
1585 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1590 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1592 static void
1593 x_set_mouse_face_gc (struct glyph_string *s)
1595 int face_id;
1596 struct face *face;
1598 /* What face has to be used last for the mouse face? */
1599 face_id = MOUSE_HL_INFO (s->f)->mouse_face_face_id;
1600 face = FACE_FROM_ID_OR_NULL (s->f, face_id);
1601 if (face == NULL)
1602 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1604 if (s->first_glyph->type == CHAR_GLYPH)
1605 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1606 else
1607 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1608 s->face = FACE_FROM_ID (s->f, face_id);
1609 prepare_face_for_display (s->f, s->face);
1611 if (s->font == s->face->font)
1612 s->gc = s->face->gc;
1613 else
1615 /* Otherwise construct scratch_cursor_gc with values from FACE
1616 except for FONT. */
1617 XGCValues xgcv;
1618 unsigned long mask;
1620 xgcv.background = s->face->background;
1621 xgcv.foreground = s->face->foreground;
1622 xgcv.graphics_exposures = False;
1623 mask = GCForeground | GCBackground | GCGraphicsExposures;
1625 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1626 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1627 mask, &xgcv);
1628 else
1629 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1630 = XCreateGC (s->display, FRAME_X_DRAWABLE (s->f), mask, &xgcv);
1632 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1635 eassert (s->gc != 0);
1639 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1640 Faces to use in the mode line have already been computed when the
1641 matrix was built, so there isn't much to do, here. */
1643 static void
1644 x_set_mode_line_face_gc (struct glyph_string *s)
1646 s->gc = s->face->gc;
1650 /* Set S->gc of glyph string S for drawing that glyph string. Set
1651 S->stippled_p to a non-zero value if the face of S has a stipple
1652 pattern. */
1654 static void
1655 x_set_glyph_string_gc (struct glyph_string *s)
1657 prepare_face_for_display (s->f, s->face);
1659 if (s->hl == DRAW_NORMAL_TEXT)
1661 s->gc = s->face->gc;
1662 s->stippled_p = s->face->stipple != 0;
1664 else if (s->hl == DRAW_INVERSE_VIDEO)
1666 x_set_mode_line_face_gc (s);
1667 s->stippled_p = s->face->stipple != 0;
1669 else if (s->hl == DRAW_CURSOR)
1671 x_set_cursor_gc (s);
1672 s->stippled_p = false;
1674 else if (s->hl == DRAW_MOUSE_FACE)
1676 x_set_mouse_face_gc (s);
1677 s->stippled_p = s->face->stipple != 0;
1679 else if (s->hl == DRAW_IMAGE_RAISED
1680 || s->hl == DRAW_IMAGE_SUNKEN)
1682 s->gc = s->face->gc;
1683 s->stippled_p = s->face->stipple != 0;
1685 else
1686 emacs_abort ();
1688 /* GC must have been set. */
1689 eassert (s->gc != 0);
1693 /* Set clipping for output of glyph string S. S may be part of a mode
1694 line or menu if we don't have X toolkit support. */
1696 static void
1697 x_set_glyph_string_clipping (struct glyph_string *s)
1699 XRectangle *r = s->clip;
1700 int n = get_glyph_string_clip_rects (s, r, 2);
1702 if (n > 0)
1703 x_set_clip_rectangles (s->f, s->gc, r, n);
1704 s->num_clips = n;
1708 /* Set SRC's clipping for output of glyph string DST. This is called
1709 when we are drawing DST's left_overhang or right_overhang only in
1710 the area of SRC. */
1712 static void
1713 x_set_glyph_string_clipping_exactly (struct glyph_string *src, struct glyph_string *dst)
1715 XRectangle r;
1717 r.x = src->x;
1718 r.width = src->width;
1719 r.y = src->y;
1720 r.height = src->height;
1721 dst->clip[0] = r;
1722 dst->num_clips = 1;
1723 x_set_clip_rectangles (dst->f, dst->gc, &r, 1);
1727 /* RIF:
1728 Compute left and right overhang of glyph string S. */
1730 static void
1731 x_compute_glyph_string_overhangs (struct glyph_string *s)
1733 if (s->cmp == NULL
1734 && (s->first_glyph->type == CHAR_GLYPH
1735 || s->first_glyph->type == COMPOSITE_GLYPH))
1737 struct font_metrics metrics;
1739 if (s->first_glyph->type == CHAR_GLYPH)
1741 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1742 struct font *font = s->font;
1743 int i;
1745 for (i = 0; i < s->nchars; i++)
1746 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1747 font->driver->text_extents (font, code, s->nchars, &metrics);
1749 else
1751 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1753 composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
1755 s->right_overhang = (metrics.rbearing > metrics.width
1756 ? metrics.rbearing - metrics.width : 0);
1757 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1759 else if (s->cmp)
1761 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1762 s->left_overhang = - s->cmp->lbearing;
1767 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1769 static void
1770 x_clear_glyph_string_rect (struct glyph_string *s, int x, int y, int w, int h)
1772 XGCValues xgcv;
1773 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1774 XSetForeground (s->display, s->gc, xgcv.background);
1775 x_fill_rectangle (s->f, s->gc, x, y, w, h);
1776 XSetForeground (s->display, s->gc, xgcv.foreground);
1780 /* Draw the background of glyph_string S. If S->background_filled_p
1781 is non-zero don't draw it. FORCE_P non-zero means draw the
1782 background even if it wouldn't be drawn normally. This is used
1783 when a string preceding S draws into the background of S, or S
1784 contains the first component of a composition. */
1786 static void
1787 x_draw_glyph_string_background (struct glyph_string *s, bool force_p)
1789 /* Nothing to do if background has already been drawn or if it
1790 shouldn't be drawn in the first place. */
1791 if (!s->background_filled_p)
1793 int box_line_width = max (s->face->box_line_width, 0);
1795 if (s->stippled_p)
1797 /* Fill background with a stipple pattern. */
1798 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1799 x_fill_rectangle (s->f, s->gc, s->x,
1800 s->y + box_line_width,
1801 s->background_width,
1802 s->height - 2 * box_line_width);
1803 XSetFillStyle (s->display, s->gc, FillSolid);
1804 s->background_filled_p = true;
1806 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1807 /* When xdisp.c ignores FONT_HEIGHT, we cannot trust
1808 font dimensions, since the actual glyphs might be
1809 much smaller. So in that case we always clear the
1810 rectangle with background color. */
1811 || FONT_TOO_HIGH (s->font)
1812 || s->font_not_found_p
1813 || s->extends_to_end_of_line_p
1814 || force_p)
1816 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1817 s->background_width,
1818 s->height - 2 * box_line_width);
1819 s->background_filled_p = true;
1825 /* Draw the foreground of glyph string S. */
1827 static void
1828 x_draw_glyph_string_foreground (struct glyph_string *s)
1830 int i, x;
1832 /* If first glyph of S has a left box line, start drawing the text
1833 of S to the right of that box line. */
1834 if (s->face->box != FACE_NO_BOX
1835 && s->first_glyph->left_box_line_p)
1836 x = s->x + eabs (s->face->box_line_width);
1837 else
1838 x = s->x;
1840 /* Draw characters of S as rectangles if S's font could not be
1841 loaded. */
1842 if (s->font_not_found_p)
1844 for (i = 0; i < s->nchars; ++i)
1846 struct glyph *g = s->first_glyph + i;
1847 x_draw_rectangle (s->f,
1848 s->gc, x, s->y, g->pixel_width - 1,
1849 s->height - 1);
1850 x += g->pixel_width;
1853 else
1855 struct font *font = s->font;
1856 int boff = font->baseline_offset;
1857 int y;
1859 if (font->vertical_centering)
1860 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1862 y = s->ybase - boff;
1863 if (s->for_overlaps
1864 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1865 font->driver->draw (s, 0, s->nchars, x, y, false);
1866 else
1867 font->driver->draw (s, 0, s->nchars, x, y, true);
1868 if (s->face->overstrike)
1869 font->driver->draw (s, 0, s->nchars, x + 1, y, false);
1873 /* Draw the foreground of composite glyph string S. */
1875 static void
1876 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1878 int i, j, x;
1879 struct font *font = s->font;
1881 /* If first glyph of S has a left box line, start drawing the text
1882 of S to the right of that box line. */
1883 if (s->face && s->face->box != FACE_NO_BOX
1884 && s->first_glyph->left_box_line_p)
1885 x = s->x + eabs (s->face->box_line_width);
1886 else
1887 x = s->x;
1889 /* S is a glyph string for a composition. S->cmp_from is the index
1890 of the first character drawn for glyphs of this composition.
1891 S->cmp_from == 0 means we are drawing the very first character of
1892 this composition. */
1894 /* Draw a rectangle for the composition if the font for the very
1895 first character of the composition could not be loaded. */
1896 if (s->font_not_found_p)
1898 if (s->cmp_from == 0)
1899 x_draw_rectangle (s->f, s->gc, x, s->y,
1900 s->width - 1, s->height - 1);
1902 else if (! s->first_glyph->u.cmp.automatic)
1904 int y = s->ybase;
1906 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1907 /* TAB in a composition means display glyphs with padding
1908 space on the left or right. */
1909 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1911 int xx = x + s->cmp->offsets[j * 2];
1912 int yy = y - s->cmp->offsets[j * 2 + 1];
1914 font->driver->draw (s, j, j + 1, xx, yy, false);
1915 if (s->face->overstrike)
1916 font->driver->draw (s, j, j + 1, xx + 1, yy, false);
1919 else
1921 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1922 Lisp_Object glyph;
1923 int y = s->ybase;
1924 int width = 0;
1926 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1928 glyph = LGSTRING_GLYPH (gstring, i);
1929 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1930 width += LGLYPH_WIDTH (glyph);
1931 else
1933 int xoff, yoff, wadjust;
1935 if (j < i)
1937 font->driver->draw (s, j, i, x, y, false);
1938 if (s->face->overstrike)
1939 font->driver->draw (s, j, i, x + 1, y, false);
1940 x += width;
1942 xoff = LGLYPH_XOFF (glyph);
1943 yoff = LGLYPH_YOFF (glyph);
1944 wadjust = LGLYPH_WADJUST (glyph);
1945 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, false);
1946 if (s->face->overstrike)
1947 font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff,
1948 false);
1949 x += wadjust;
1950 j = i + 1;
1951 width = 0;
1954 if (j < i)
1956 font->driver->draw (s, j, i, x, y, false);
1957 if (s->face->overstrike)
1958 font->driver->draw (s, j, i, x + 1, y, false);
1964 /* Draw the foreground of glyph string S for glyphless characters. */
1966 static void
1967 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1969 struct glyph *glyph = s->first_glyph;
1970 XChar2b char2b[8];
1971 int x, i, j;
1973 /* If first glyph of S has a left box line, start drawing the text
1974 of S to the right of that box line. */
1975 if (s->face && s->face->box != FACE_NO_BOX
1976 && s->first_glyph->left_box_line_p)
1977 x = s->x + eabs (s->face->box_line_width);
1978 else
1979 x = s->x;
1981 s->char2b = char2b;
1983 for (i = 0; i < s->nchars; i++, glyph++)
1985 char buf[7], *str = NULL;
1986 int len = glyph->u.glyphless.len;
1988 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1990 if (len > 0
1991 && CHAR_TABLE_P (Vglyphless_char_display)
1992 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1993 >= 1))
1995 Lisp_Object acronym
1996 = (! glyph->u.glyphless.for_no_font
1997 ? CHAR_TABLE_REF (Vglyphless_char_display,
1998 glyph->u.glyphless.ch)
1999 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
2000 if (STRINGP (acronym))
2001 str = SSDATA (acronym);
2004 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
2006 unsigned int ch = glyph->u.glyphless.ch;
2007 eassume (ch <= MAX_CHAR);
2008 sprintf (buf, "%0*X", ch < 0x10000 ? 4 : 6, ch);
2009 str = buf;
2012 if (str)
2014 int upper_len = (len + 1) / 2;
2015 unsigned code;
2017 /* It is assured that all LEN characters in STR is ASCII. */
2018 for (j = 0; j < len; j++)
2020 code = s->font->driver->encode_char (s->font, str[j]);
2021 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
2023 s->font->driver->draw (s, 0, upper_len,
2024 x + glyph->slice.glyphless.upper_xoff,
2025 s->ybase + glyph->slice.glyphless.upper_yoff,
2026 false);
2027 s->font->driver->draw (s, upper_len, len,
2028 x + glyph->slice.glyphless.lower_xoff,
2029 s->ybase + glyph->slice.glyphless.lower_yoff,
2030 false);
2032 if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
2033 x_draw_rectangle (s->f, s->gc,
2034 x, s->ybase - glyph->ascent,
2035 glyph->pixel_width - 1,
2036 glyph->ascent + glyph->descent - 1);
2037 x += glyph->pixel_width;
2041 #ifdef USE_X_TOOLKIT
2043 #ifdef USE_LUCID
2045 /* Return the frame on which widget WIDGET is used.. Abort if frame
2046 cannot be determined. */
2048 static struct frame *
2049 x_frame_of_widget (Widget widget)
2051 struct x_display_info *dpyinfo;
2052 Lisp_Object tail, frame;
2053 struct frame *f;
2055 dpyinfo = x_display_info_for_display (XtDisplay (widget));
2057 /* Find the top-level shell of the widget. Note that this function
2058 can be called when the widget is not yet realized, so XtWindow
2059 (widget) == 0. That's the reason we can't simply use
2060 x_any_window_to_frame. */
2061 while (!XtIsTopLevelShell (widget))
2062 widget = XtParent (widget);
2064 /* Look for a frame with that top-level widget. Allocate the color
2065 on that frame to get the right gamma correction value. */
2066 FOR_EACH_FRAME (tail, frame)
2068 f = XFRAME (frame);
2069 if (FRAME_X_P (f)
2070 && f->output_data.nothing != 1
2071 && FRAME_DISPLAY_INFO (f) == dpyinfo
2072 && f->output_data.x->widget == widget)
2073 return f;
2075 emacs_abort ();
2078 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
2079 or DELTA. Try a color with RGB values multiplied by FACTOR first.
2080 If this produces the same color as PIXEL, try a color where all RGB
2081 values have DELTA added. Return the allocated color in *PIXEL.
2082 DISPLAY is the X display, CMAP is the colormap to operate on.
2083 Value is true if successful. */
2085 bool
2086 x_alloc_lighter_color_for_widget (Widget widget, Display *display, Colormap cmap,
2087 unsigned long *pixel, double factor, int delta)
2089 struct frame *f = x_frame_of_widget (widget);
2090 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
2093 #endif /* USE_LUCID */
2096 /* Structure specifying which arguments should be passed by Xt to
2097 cvt_string_to_pixel. We want the widget's screen and colormap. */
2099 static XtConvertArgRec cvt_string_to_pixel_args[] =
2101 {XtWidgetBaseOffset, (XtPointer) offsetof (WidgetRec, core.screen),
2102 sizeof (Screen *)},
2103 {XtWidgetBaseOffset, (XtPointer) offsetof (WidgetRec, core.colormap),
2104 sizeof (Colormap)}
2108 /* The address of this variable is returned by
2109 cvt_string_to_pixel. */
2111 static Pixel cvt_string_to_pixel_value;
2114 /* Convert a color name to a pixel color.
2116 DPY is the display we are working on.
2118 ARGS is an array of *NARGS XrmValue structures holding additional
2119 information about the widget for which the conversion takes place.
2120 The contents of this array are determined by the specification
2121 in cvt_string_to_pixel_args.
2123 FROM is a pointer to an XrmValue which points to the color name to
2124 convert. TO is an XrmValue in which to return the pixel color.
2126 CLOSURE_RET is a pointer to user-data, in which we record if
2127 we allocated the color or not.
2129 Value is True if successful, False otherwise. */
2131 static Boolean
2132 cvt_string_to_pixel (Display *dpy, XrmValue *args, Cardinal *nargs,
2133 XrmValue *from, XrmValue *to,
2134 XtPointer *closure_ret)
2136 Screen *screen;
2137 Colormap cmap;
2138 Pixel pixel;
2139 String color_name;
2140 XColor color;
2142 if (*nargs != 2)
2144 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
2145 "wrongParameters", "cvt_string_to_pixel",
2146 "XtToolkitError",
2147 "Screen and colormap args required", NULL, NULL);
2148 return False;
2151 screen = *(Screen **) args[0].addr;
2152 cmap = *(Colormap *) args[1].addr;
2153 color_name = (String) from->addr;
2155 if (strcmp (color_name, XtDefaultBackground) == 0)
2157 *closure_ret = (XtPointer) False;
2158 pixel = WhitePixelOfScreen (screen);
2160 else if (strcmp (color_name, XtDefaultForeground) == 0)
2162 *closure_ret = (XtPointer) False;
2163 pixel = BlackPixelOfScreen (screen);
2165 else if (XParseColor (dpy, cmap, color_name, &color)
2166 && x_alloc_nearest_color_1 (dpy, cmap, &color))
2168 pixel = color.pixel;
2169 *closure_ret = (XtPointer) True;
2171 else
2173 String params[1];
2174 Cardinal nparams = 1;
2176 params[0] = color_name;
2177 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
2178 "badValue", "cvt_string_to_pixel",
2179 "XtToolkitError", "Invalid color '%s'",
2180 params, &nparams);
2181 return False;
2184 if (to->addr != NULL)
2186 if (to->size < sizeof (Pixel))
2188 to->size = sizeof (Pixel);
2189 return False;
2192 *(Pixel *) to->addr = pixel;
2194 else
2196 cvt_string_to_pixel_value = pixel;
2197 to->addr = (XtPointer) &cvt_string_to_pixel_value;
2200 to->size = sizeof (Pixel);
2201 return True;
2205 /* Free a pixel color which was previously allocated via
2206 cvt_string_to_pixel. This is registered as the destructor
2207 for this type of resource via XtSetTypeConverter.
2209 APP is the application context in which we work.
2211 TO is a pointer to an XrmValue holding the color to free.
2212 CLOSURE is the value we stored in CLOSURE_RET for this color
2213 in cvt_string_to_pixel.
2215 ARGS and NARGS are like for cvt_string_to_pixel. */
2217 static void
2218 cvt_pixel_dtor (XtAppContext app, XrmValuePtr to, XtPointer closure, XrmValuePtr args,
2219 Cardinal *nargs)
2221 if (*nargs != 2)
2223 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
2224 "XtToolkitError",
2225 "Screen and colormap arguments required",
2226 NULL, NULL);
2228 else if (closure != NULL)
2230 /* We did allocate the pixel, so free it. */
2231 Screen *screen = *(Screen **) args[0].addr;
2232 Colormap cmap = *(Colormap *) args[1].addr;
2233 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
2234 (Pixel *) to->addr, 1);
2239 #endif /* USE_X_TOOLKIT */
2242 /* Value is an array of XColor structures for the contents of the
2243 color map of display DPY. Set *NCELLS to the size of the array.
2244 Note that this probably shouldn't be called for large color maps,
2245 say a 24-bit TrueColor map. */
2247 static const XColor *
2248 x_color_cells (Display *dpy, int *ncells)
2250 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
2251 eassume (dpyinfo);
2253 if (dpyinfo->color_cells == NULL)
2255 Screen *screen = dpyinfo->screen;
2256 int ncolor_cells = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
2257 int i;
2259 dpyinfo->color_cells = xnmalloc (ncolor_cells,
2260 sizeof *dpyinfo->color_cells);
2261 dpyinfo->ncolor_cells = ncolor_cells;
2263 for (i = 0; i < ncolor_cells; ++i)
2264 dpyinfo->color_cells[i].pixel = i;
2266 XQueryColors (dpy, dpyinfo->cmap,
2267 dpyinfo->color_cells, ncolor_cells);
2270 *ncells = dpyinfo->ncolor_cells;
2271 return dpyinfo->color_cells;
2275 /* On frame F, translate pixel colors to RGB values for the NCOLORS
2276 colors in COLORS. Use cached information, if available. */
2278 void
2279 x_query_colors (struct frame *f, XColor *colors, int ncolors)
2281 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2283 if (dpyinfo->red_bits > 0)
2285 /* For TrueColor displays, we can decompose the RGB value
2286 directly. */
2287 int i;
2288 unsigned int rmult, gmult, bmult;
2289 unsigned int rmask, gmask, bmask;
2291 rmask = (1 << dpyinfo->red_bits) - 1;
2292 gmask = (1 << dpyinfo->green_bits) - 1;
2293 bmask = (1 << dpyinfo->blue_bits) - 1;
2294 /* If we're widening, for example, 8 bits in the pixel value to
2295 16 bits for the separate-color representation, we want to
2296 extrapolate the lower bits based on those bits available --
2297 in other words, we'd like 0xff to become 0xffff instead of
2298 the 0xff00 we'd get by just zero-filling the lower bits.
2300 We generate a 32-bit scaled-up value and shift it, in case
2301 the bit count doesn't divide 16 evenly (e.g., when dealing
2302 with a 3-3-2 bit RGB display), to get more of the lower bits
2303 correct.
2305 Should we cache the multipliers in dpyinfo? Maybe
2306 special-case the 8-8-8 common case? */
2307 rmult = 0xffffffff / rmask;
2308 gmult = 0xffffffff / gmask;
2309 bmult = 0xffffffff / bmask;
2311 for (i = 0; i < ncolors; ++i)
2313 unsigned int r, g, b;
2314 unsigned long pixel = colors[i].pixel;
2316 r = (pixel >> dpyinfo->red_offset) & rmask;
2317 g = (pixel >> dpyinfo->green_offset) & gmask;
2318 b = (pixel >> dpyinfo->blue_offset) & bmask;
2320 colors[i].red = (r * rmult) >> 16;
2321 colors[i].green = (g * gmult) >> 16;
2322 colors[i].blue = (b * bmult) >> 16;
2324 return;
2327 if (dpyinfo->color_cells)
2329 int i;
2330 for (i = 0; i < ncolors; ++i)
2332 unsigned long pixel = colors[i].pixel;
2333 eassert (pixel < dpyinfo->ncolor_cells);
2334 eassert (dpyinfo->color_cells[pixel].pixel == pixel);
2335 colors[i] = dpyinfo->color_cells[pixel];
2337 return;
2340 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
2344 /* On frame F, translate pixel color to RGB values for the color in
2345 COLOR. Use cached information, if available. */
2347 void
2348 x_query_color (struct frame *f, XColor *color)
2350 x_query_colors (f, color, 1);
2354 /* On frame F, translate the color name to RGB values. Use cached
2355 information, if possible.
2357 Note that there is currently no way to clean old entries out of the
2358 cache. However, it is limited to names in the server's database,
2359 and names we've actually looked up; list-colors-display is probably
2360 the most color-intensive case we're likely to hit. */
2362 Status x_parse_color (struct frame *f, const char *color_name,
2363 XColor *color)
2365 Display *dpy = FRAME_X_DISPLAY (f);
2366 Colormap cmap = FRAME_X_COLORMAP (f);
2367 struct color_name_cache_entry *cache_entry;
2369 if (color_name[0] == '#')
2371 /* The hex form is parsed directly by XParseColor without
2372 talking to the X server. No need for caching. */
2373 return XParseColor (dpy, cmap, color_name, color);
2376 for (cache_entry = FRAME_DISPLAY_INFO (f)->color_names; cache_entry;
2377 cache_entry = cache_entry->next)
2379 if (!xstrcasecmp(cache_entry->name, color_name))
2381 *color = cache_entry->rgb;
2382 return 1;
2386 if (XParseColor (dpy, cmap, color_name, color) == 0)
2387 /* No caching of negative results, currently. */
2388 return 0;
2390 cache_entry = xzalloc (sizeof *cache_entry);
2391 cache_entry->rgb = *color;
2392 cache_entry->name = xstrdup (color_name);
2393 cache_entry->next = FRAME_DISPLAY_INFO (f)->color_names;
2394 FRAME_DISPLAY_INFO (f)->color_names = cache_entry;
2395 return 1;
2399 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
2400 exact match can't be allocated, try the nearest color available.
2401 Value is true if successful. Set *COLOR to the color
2402 allocated. */
2404 static bool
2405 x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color)
2407 bool rc;
2409 rc = XAllocColor (dpy, cmap, color) != 0;
2410 if (rc == 0)
2412 /* If we got to this point, the colormap is full, so we're going
2413 to try to get the next closest color. The algorithm used is
2414 a least-squares matching, which is what X uses for closest
2415 color matching with StaticColor visuals. */
2416 int nearest, i;
2417 int max_color_delta = 255;
2418 int max_delta = 3 * max_color_delta;
2419 int nearest_delta = max_delta + 1;
2420 int ncells;
2421 const XColor *cells = x_color_cells (dpy, &ncells);
2423 for (nearest = i = 0; i < ncells; ++i)
2425 int dred = (color->red >> 8) - (cells[i].red >> 8);
2426 int dgreen = (color->green >> 8) - (cells[i].green >> 8);
2427 int dblue = (color->blue >> 8) - (cells[i].blue >> 8);
2428 int delta = dred * dred + dgreen * dgreen + dblue * dblue;
2430 if (delta < nearest_delta)
2432 nearest = i;
2433 nearest_delta = delta;
2437 color->red = cells[nearest].red;
2438 color->green = cells[nearest].green;
2439 color->blue = cells[nearest].blue;
2440 rc = XAllocColor (dpy, cmap, color) != 0;
2442 else
2444 /* If allocation succeeded, and the allocated pixel color is not
2445 equal to a cached pixel color recorded earlier, there was a
2446 change in the colormap, so clear the color cache. */
2447 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
2448 eassume (dpyinfo);
2450 if (dpyinfo->color_cells)
2452 XColor *cached_color = &dpyinfo->color_cells[color->pixel];
2453 if (cached_color->red != color->red
2454 || cached_color->blue != color->blue
2455 || cached_color->green != color->green)
2457 xfree (dpyinfo->color_cells);
2458 dpyinfo->color_cells = NULL;
2459 dpyinfo->ncolor_cells = 0;
2464 #ifdef DEBUG_X_COLORS
2465 if (rc)
2466 register_color (color->pixel);
2467 #endif /* DEBUG_X_COLORS */
2469 return rc;
2473 /* Allocate the color COLOR->pixel on frame F, colormap CMAP, after
2474 gamma correction. If an exact match can't be allocated, try the
2475 nearest color available. Value is true if successful. Set *COLOR
2476 to the color allocated. */
2478 bool
2479 x_alloc_nearest_color (struct frame *f, Colormap cmap, XColor *color)
2481 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2483 gamma_correct (f, color);
2485 if (dpyinfo->red_bits > 0)
2487 color->pixel = x_make_truecolor_pixel (dpyinfo,
2488 color->red,
2489 color->green,
2490 color->blue);
2491 return true;
2494 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
2498 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
2499 It's necessary to do this instead of just using PIXEL directly to
2500 get color reference counts right. */
2502 unsigned long
2503 x_copy_color (struct frame *f, unsigned long pixel)
2505 XColor color;
2507 /* If display has an immutable color map, freeing colors is not
2508 necessary and some servers don't allow it. Since we won't free a
2509 color once we've allocated it, we don't need to re-allocate it to
2510 maintain the server's reference count. */
2511 if (!x_mutable_colormap (FRAME_X_VISUAL (f)))
2512 return pixel;
2514 color.pixel = pixel;
2515 block_input ();
2516 /* The color could still be found in the color_cells array. */
2517 x_query_color (f, &color);
2518 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
2519 unblock_input ();
2520 #ifdef DEBUG_X_COLORS
2521 register_color (pixel);
2522 #endif
2523 return color.pixel;
2527 /* Brightness beyond which a color won't have its highlight brightness
2528 boosted.
2530 Nominally, highlight colors for `3d' faces are calculated by
2531 brightening an object's color by a constant scale factor, but this
2532 doesn't yield good results for dark colors, so for colors who's
2533 brightness is less than this value (on a scale of 0-65535) have an
2534 use an additional additive factor.
2536 The value here is set so that the default menu-bar/mode-line color
2537 (grey75) will not have its highlights changed at all. */
2538 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
2541 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
2542 or DELTA. Try a color with RGB values multiplied by FACTOR first.
2543 If this produces the same color as PIXEL, try a color where all RGB
2544 values have DELTA added. Return the allocated color in *PIXEL.
2545 DISPLAY is the X display, CMAP is the colormap to operate on.
2546 Value is non-zero if successful. */
2548 static bool
2549 x_alloc_lighter_color (struct frame *f, Display *display, Colormap cmap,
2550 unsigned long *pixel, double factor, int delta)
2552 XColor color, new;
2553 long bright;
2554 bool success_p;
2556 /* Get RGB color values. */
2557 color.pixel = *pixel;
2558 x_query_color (f, &color);
2560 /* Change RGB values by specified FACTOR. Avoid overflow! */
2561 eassert (factor >= 0);
2562 new.red = min (0xffff, factor * color.red);
2563 new.green = min (0xffff, factor * color.green);
2564 new.blue = min (0xffff, factor * color.blue);
2566 /* Calculate brightness of COLOR. */
2567 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
2569 /* We only boost colors that are darker than
2570 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
2571 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
2572 /* Make an additive adjustment to NEW, because it's dark enough so
2573 that scaling by FACTOR alone isn't enough. */
2575 /* How far below the limit this color is (0 - 1, 1 being darker). */
2576 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
2577 /* The additive adjustment. */
2578 int min_delta = delta * dimness * factor / 2;
2580 if (factor < 1)
2582 new.red = max (0, new.red - min_delta);
2583 new.green = max (0, new.green - min_delta);
2584 new.blue = max (0, new.blue - min_delta);
2586 else
2588 new.red = min (0xffff, min_delta + new.red);
2589 new.green = min (0xffff, min_delta + new.green);
2590 new.blue = min (0xffff, min_delta + new.blue);
2594 /* Try to allocate the color. */
2595 success_p = x_alloc_nearest_color (f, cmap, &new);
2596 if (success_p)
2598 if (new.pixel == *pixel)
2600 /* If we end up with the same color as before, try adding
2601 delta to the RGB values. */
2602 x_free_colors (f, &new.pixel, 1);
2604 new.red = min (0xffff, delta + color.red);
2605 new.green = min (0xffff, delta + color.green);
2606 new.blue = min (0xffff, delta + color.blue);
2607 success_p = x_alloc_nearest_color (f, cmap, &new);
2609 else
2610 success_p = true;
2611 *pixel = new.pixel;
2614 return success_p;
2618 /* Set up the foreground color for drawing relief lines of glyph
2619 string S. RELIEF is a pointer to a struct relief containing the GC
2620 with which lines will be drawn. Use a color that is FACTOR or
2621 DELTA lighter or darker than the relief's background which is found
2622 in S->f->output_data.x->relief_background. If such a color cannot
2623 be allocated, use DEFAULT_PIXEL, instead. */
2625 static void
2626 x_setup_relief_color (struct frame *f, struct relief *relief, double factor,
2627 int delta, unsigned long default_pixel)
2629 XGCValues xgcv;
2630 struct x_output *di = f->output_data.x;
2631 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
2632 unsigned long pixel;
2633 unsigned long background = di->relief_background;
2634 Colormap cmap = FRAME_X_COLORMAP (f);
2635 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2636 Display *dpy = FRAME_X_DISPLAY (f);
2638 xgcv.graphics_exposures = False;
2639 xgcv.line_width = 1;
2641 /* Free previously allocated color. The color cell will be reused
2642 when it has been freed as many times as it was allocated, so this
2643 doesn't affect faces using the same colors. */
2644 if (relief->gc && relief->pixel != -1)
2646 x_free_colors (f, &relief->pixel, 1);
2647 relief->pixel = -1;
2650 /* Allocate new color. */
2651 xgcv.foreground = default_pixel;
2652 pixel = background;
2653 if (dpyinfo->n_planes != 1
2654 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
2655 xgcv.foreground = relief->pixel = pixel;
2657 if (relief->gc == 0)
2659 xgcv.stipple = dpyinfo->gray;
2660 mask |= GCStipple;
2661 relief->gc = XCreateGC (dpy, FRAME_X_DRAWABLE (f), mask, &xgcv);
2663 else
2664 XChangeGC (dpy, relief->gc, mask, &xgcv);
2668 /* Set up colors for the relief lines around glyph string S. */
2670 static void
2671 x_setup_relief_colors (struct glyph_string *s)
2673 struct x_output *di = s->f->output_data.x;
2674 unsigned long color;
2676 if (s->face->use_box_color_for_shadows_p)
2677 color = s->face->box_color;
2678 else if (s->first_glyph->type == IMAGE_GLYPH
2679 && s->img->pixmap
2680 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2681 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2682 else
2684 XGCValues xgcv;
2686 /* Get the background color of the face. */
2687 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2688 color = xgcv.background;
2691 if (di->white_relief.gc == 0
2692 || color != di->relief_background)
2694 di->relief_background = color;
2695 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2696 WHITE_PIX_DEFAULT (s->f));
2697 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2698 BLACK_PIX_DEFAULT (s->f));
2703 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2704 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2705 to draw, it must be >= 0. RAISED_P means draw a raised
2706 relief. LEFT_P means draw a relief on the left side of
2707 the rectangle. RIGHT_P means draw a relief on the right
2708 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2709 when drawing. */
2711 static void
2712 x_draw_relief_rect (struct frame *f,
2713 int left_x, int top_y, int right_x, int bottom_y,
2714 int width, bool raised_p, bool top_p, bool bot_p,
2715 bool left_p, bool right_p,
2716 XRectangle *clip_rect)
2718 #ifdef USE_CAIRO
2719 GC top_left_gc, bottom_right_gc;
2720 int corners = 0;
2722 if (raised_p)
2724 top_left_gc = f->output_data.x->white_relief.gc;
2725 bottom_right_gc = f->output_data.x->black_relief.gc;
2727 else
2729 top_left_gc = f->output_data.x->black_relief.gc;
2730 bottom_right_gc = f->output_data.x->white_relief.gc;
2733 x_set_clip_rectangles (f, top_left_gc, clip_rect, 1);
2734 x_set_clip_rectangles (f, bottom_right_gc, clip_rect, 1);
2736 if (left_p)
2738 x_fill_rectangle (f, top_left_gc, left_x, top_y,
2739 width, bottom_y + 1 - top_y);
2740 if (top_p)
2741 corners |= 1 << CORNER_TOP_LEFT;
2742 if (bot_p)
2743 corners |= 1 << CORNER_BOTTOM_LEFT;
2745 if (right_p)
2747 x_fill_rectangle (f, bottom_right_gc, right_x + 1 - width, top_y,
2748 width, bottom_y + 1 - top_y);
2749 if (top_p)
2750 corners |= 1 << CORNER_TOP_RIGHT;
2751 if (bot_p)
2752 corners |= 1 << CORNER_BOTTOM_RIGHT;
2754 if (top_p)
2756 if (!right_p)
2757 x_fill_rectangle (f, top_left_gc, left_x, top_y,
2758 right_x + 1 - left_x, width);
2759 else
2760 x_fill_trapezoid_for_relief (f, top_left_gc, left_x, top_y,
2761 right_x + 1 - left_x, width, 1);
2763 if (bot_p)
2765 if (!left_p)
2766 x_fill_rectangle (f, bottom_right_gc, left_x, bottom_y + 1 - width,
2767 right_x + 1 - left_x, width);
2768 else
2769 x_fill_trapezoid_for_relief (f, bottom_right_gc,
2770 left_x, bottom_y + 1 - width,
2771 right_x + 1 - left_x, width, 0);
2773 if (left_p && width != 1)
2774 x_fill_rectangle (f, bottom_right_gc, left_x, top_y,
2775 1, bottom_y + 1 - top_y);
2776 if (top_p && width != 1)
2777 x_fill_rectangle (f, bottom_right_gc, left_x, top_y,
2778 right_x + 1 - left_x, 1);
2779 if (corners)
2781 XSetBackground (FRAME_X_DISPLAY (f), top_left_gc,
2782 FRAME_BACKGROUND_PIXEL (f));
2783 x_erase_corners_for_relief (f, top_left_gc, left_x, top_y,
2784 right_x - left_x + 1, bottom_y - top_y + 1,
2785 6, 1, corners);
2788 x_reset_clip_rectangles (f, top_left_gc);
2789 x_reset_clip_rectangles (f, bottom_right_gc);
2790 #else
2791 Display *dpy = FRAME_X_DISPLAY (f);
2792 Drawable drawable = FRAME_X_DRAWABLE (f);
2793 int i;
2794 GC gc;
2796 if (raised_p)
2797 gc = f->output_data.x->white_relief.gc;
2798 else
2799 gc = f->output_data.x->black_relief.gc;
2800 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2802 /* This code is more complicated than it has to be, because of two
2803 minor hacks to make the boxes look nicer: (i) if width > 1, draw
2804 the outermost line using the black relief. (ii) Omit the four
2805 corner pixels. */
2807 /* Top. */
2808 if (top_p)
2810 if (width == 1)
2811 XDrawLine (dpy, drawable, gc,
2812 left_x + left_p, top_y,
2813 right_x + !right_p, top_y);
2815 for (i = 1; i < width; ++i)
2816 XDrawLine (dpy, drawable, gc,
2817 left_x + i * left_p, top_y + i,
2818 right_x + 1 - i * right_p, top_y + i);
2821 /* Left. */
2822 if (left_p)
2824 if (width == 1)
2825 XDrawLine (dpy, drawable, gc, left_x, top_y + 1, left_x, bottom_y);
2827 x_clear_area(f, left_x, top_y, 1, 1);
2828 x_clear_area(f, left_x, bottom_y, 1, 1);
2830 for (i = (width > 1 ? 1 : 0); i < width; ++i)
2831 XDrawLine (dpy, drawable, gc,
2832 left_x + i, top_y + (i + 1) * top_p,
2833 left_x + i, bottom_y + 1 - (i + 1) * bot_p);
2836 XSetClipMask (dpy, gc, None);
2837 if (raised_p)
2838 gc = f->output_data.x->black_relief.gc;
2839 else
2840 gc = f->output_data.x->white_relief.gc;
2841 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2843 if (width > 1)
2845 /* Outermost top line. */
2846 if (top_p)
2847 XDrawLine (dpy, drawable, gc,
2848 left_x + left_p, top_y,
2849 right_x + !right_p, top_y);
2851 /* Outermost left line. */
2852 if (left_p)
2853 XDrawLine (dpy, drawable, gc, left_x, top_y + 1, left_x, bottom_y);
2856 /* Bottom. */
2857 if (bot_p)
2859 XDrawLine (dpy, drawable, gc,
2860 left_x + left_p, bottom_y,
2861 right_x + !right_p, bottom_y);
2862 for (i = 1; i < width; ++i)
2863 XDrawLine (dpy, drawable, gc,
2864 left_x + i * left_p, bottom_y - i,
2865 right_x + 1 - i * right_p, bottom_y - i);
2868 /* Right. */
2869 if (right_p)
2871 x_clear_area(f, right_x, top_y, 1, 1);
2872 x_clear_area(f, right_x, bottom_y, 1, 1);
2873 for (i = 0; i < width; ++i)
2874 XDrawLine (dpy, drawable, gc,
2875 right_x - i, top_y + (i + 1) * top_p,
2876 right_x - i, bottom_y + 1 - (i + 1) * bot_p);
2879 x_reset_clip_rectangles (f, gc);
2881 #endif
2885 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2886 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2887 draw, it must be >= 0. LEFT_P means draw a line on the
2888 left side of the rectangle. RIGHT_P means draw a line
2889 on the right side of the rectangle. CLIP_RECT is the clipping
2890 rectangle to use when drawing. */
2892 static void
2893 x_draw_box_rect (struct glyph_string *s,
2894 int left_x, int top_y, int right_x, int bottom_y, int width,
2895 bool left_p, bool right_p, XRectangle *clip_rect)
2897 XGCValues xgcv;
2899 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2900 XSetForeground (s->display, s->gc, s->face->box_color);
2901 x_set_clip_rectangles (s->f, s->gc, clip_rect, 1);
2903 /* Top. */
2904 x_fill_rectangle (s->f, s->gc,
2905 left_x, top_y, right_x - left_x + 1, width);
2907 /* Left. */
2908 if (left_p)
2909 x_fill_rectangle (s->f, s->gc,
2910 left_x, top_y, width, bottom_y - top_y + 1);
2912 /* Bottom. */
2913 x_fill_rectangle (s->f, s->gc,
2914 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2916 /* Right. */
2917 if (right_p)
2918 x_fill_rectangle (s->f, s->gc,
2919 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2921 XSetForeground (s->display, s->gc, xgcv.foreground);
2922 x_reset_clip_rectangles (s->f, s->gc);
2926 /* Draw a box around glyph string S. */
2928 static void
2929 x_draw_glyph_string_box (struct glyph_string *s)
2931 int width, left_x, right_x, top_y, bottom_y, last_x;
2932 bool raised_p, left_p, right_p;
2933 struct glyph *last_glyph;
2934 XRectangle clip_rect;
2936 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2937 ? WINDOW_RIGHT_EDGE_X (s->w)
2938 : window_box_right (s->w, s->area));
2940 /* The glyph that may have a right box line. */
2941 last_glyph = (s->cmp || s->img
2942 ? s->first_glyph
2943 : s->first_glyph + s->nchars - 1);
2945 width = eabs (s->face->box_line_width);
2946 raised_p = s->face->box == FACE_RAISED_BOX;
2947 left_x = s->x;
2948 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2949 ? last_x - 1
2950 : min (last_x, s->x + s->background_width) - 1);
2951 top_y = s->y;
2952 bottom_y = top_y + s->height - 1;
2954 left_p = (s->first_glyph->left_box_line_p
2955 || (s->hl == DRAW_MOUSE_FACE
2956 && (s->prev == NULL
2957 || s->prev->hl != s->hl)));
2958 right_p = (last_glyph->right_box_line_p
2959 || (s->hl == DRAW_MOUSE_FACE
2960 && (s->next == NULL
2961 || s->next->hl != s->hl)));
2963 get_glyph_string_clip_rect (s, &clip_rect);
2965 if (s->face->box == FACE_SIMPLE_BOX)
2966 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2967 left_p, right_p, &clip_rect);
2968 else
2970 x_setup_relief_colors (s);
2971 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2972 width, raised_p, true, true, left_p, right_p,
2973 &clip_rect);
2978 /* Draw foreground of image glyph string S. */
2980 static void
2981 x_draw_image_foreground (struct glyph_string *s)
2983 int x = s->x;
2984 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2986 /* If first glyph of S has a left box line, start drawing it to the
2987 right of that line. */
2988 if (s->face->box != FACE_NO_BOX
2989 && s->first_glyph->left_box_line_p
2990 && s->slice.x == 0)
2991 x += eabs (s->face->box_line_width);
2993 /* If there is a margin around the image, adjust x- and y-position
2994 by that margin. */
2995 if (s->slice.x == 0)
2996 x += s->img->hmargin;
2997 if (s->slice.y == 0)
2998 y += s->img->vmargin;
3000 if (s->img->pixmap)
3002 if (s->img->mask)
3004 /* We can't set both a clip mask and use XSetClipRectangles
3005 because the latter also sets a clip mask. We also can't
3006 trust on the shape extension to be available
3007 (XShapeCombineRegion). So, compute the rectangle to draw
3008 manually. */
3009 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3010 | GCFunction);
3011 XGCValues xgcv;
3012 XRectangle clip_rect, image_rect, r;
3014 xgcv.clip_mask = s->img->mask;
3015 xgcv.clip_x_origin = x;
3016 xgcv.clip_y_origin = y;
3017 xgcv.function = GXcopy;
3018 XChangeGC (s->display, s->gc, mask, &xgcv);
3020 get_glyph_string_clip_rect (s, &clip_rect);
3021 image_rect.x = x;
3022 image_rect.y = y;
3023 image_rect.width = s->slice.width;
3024 image_rect.height = s->slice.height;
3025 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3026 XCopyArea (s->display, s->img->pixmap,
3027 FRAME_X_DRAWABLE (s->f), s->gc,
3028 s->slice.x + r.x - x, s->slice.y + r.y - y,
3029 r.width, r.height, r.x, r.y);
3031 else
3033 XRectangle clip_rect, image_rect, r;
3035 get_glyph_string_clip_rect (s, &clip_rect);
3036 image_rect.x = x;
3037 image_rect.y = y;
3038 image_rect.width = s->slice.width;
3039 image_rect.height = s->slice.height;
3040 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3041 XCopyArea (s->display, s->img->pixmap,
3042 FRAME_X_DRAWABLE (s->f), s->gc,
3043 s->slice.x + r.x - x, s->slice.y + r.y - y,
3044 r.width, r.height, r.x, r.y);
3046 /* When the image has a mask, we can expect that at
3047 least part of a mouse highlight or a block cursor will
3048 be visible. If the image doesn't have a mask, make
3049 a block cursor visible by drawing a rectangle around
3050 the image. I believe it's looking better if we do
3051 nothing here for mouse-face. */
3052 if (s->hl == DRAW_CURSOR)
3054 int relief = eabs (s->img->relief);
3055 x_draw_rectangle (s->f, s->gc,
3056 x - relief, y - relief,
3057 s->slice.width + relief*2 - 1,
3058 s->slice.height + relief*2 - 1);
3062 else
3063 /* Draw a rectangle if image could not be loaded. */
3064 x_draw_rectangle (s->f, s->gc, x, y,
3065 s->slice.width - 1, s->slice.height - 1);
3069 /* Draw a relief around the image glyph string S. */
3071 static void
3072 x_draw_image_relief (struct glyph_string *s)
3074 int x1, y1, thick;
3075 bool raised_p, top_p, bot_p, left_p, right_p;
3076 int extra_x, extra_y;
3077 XRectangle r;
3078 int x = s->x;
3079 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
3081 /* If first glyph of S has a left box line, start drawing it to the
3082 right of that line. */
3083 if (s->face->box != FACE_NO_BOX
3084 && s->first_glyph->left_box_line_p
3085 && s->slice.x == 0)
3086 x += eabs (s->face->box_line_width);
3088 /* If there is a margin around the image, adjust x- and y-position
3089 by that margin. */
3090 if (s->slice.x == 0)
3091 x += s->img->hmargin;
3092 if (s->slice.y == 0)
3093 y += s->img->vmargin;
3095 if (s->hl == DRAW_IMAGE_SUNKEN
3096 || s->hl == DRAW_IMAGE_RAISED)
3098 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
3099 raised_p = s->hl == DRAW_IMAGE_RAISED;
3101 else
3103 thick = eabs (s->img->relief);
3104 raised_p = s->img->relief > 0;
3107 x1 = x + s->slice.width - 1;
3108 y1 = y + s->slice.height - 1;
3110 extra_x = extra_y = 0;
3111 if (s->face->id == TOOL_BAR_FACE_ID)
3113 if (CONSP (Vtool_bar_button_margin)
3114 && INTEGERP (XCAR (Vtool_bar_button_margin))
3115 && INTEGERP (XCDR (Vtool_bar_button_margin)))
3117 extra_x = XINT (XCAR (Vtool_bar_button_margin));
3118 extra_y = XINT (XCDR (Vtool_bar_button_margin));
3120 else if (INTEGERP (Vtool_bar_button_margin))
3121 extra_x = extra_y = XINT (Vtool_bar_button_margin);
3124 top_p = bot_p = left_p = right_p = false;
3126 if (s->slice.x == 0)
3127 x -= thick + extra_x, left_p = true;
3128 if (s->slice.y == 0)
3129 y -= thick + extra_y, top_p = true;
3130 if (s->slice.x + s->slice.width == s->img->width)
3131 x1 += thick + extra_x, right_p = true;
3132 if (s->slice.y + s->slice.height == s->img->height)
3133 y1 += thick + extra_y, bot_p = true;
3135 x_setup_relief_colors (s);
3136 get_glyph_string_clip_rect (s, &r);
3137 x_draw_relief_rect (s->f, x, y, x1, y1, thick, raised_p,
3138 top_p, bot_p, left_p, right_p, &r);
3142 /* Draw the foreground of image glyph string S to PIXMAP. */
3144 static void
3145 x_draw_image_foreground_1 (struct glyph_string *s, Pixmap pixmap)
3147 int x = 0;
3148 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
3150 /* If first glyph of S has a left box line, start drawing it to the
3151 right of that line. */
3152 if (s->face->box != FACE_NO_BOX
3153 && s->first_glyph->left_box_line_p
3154 && s->slice.x == 0)
3155 x += eabs (s->face->box_line_width);
3157 /* If there is a margin around the image, adjust x- and y-position
3158 by that margin. */
3159 if (s->slice.x == 0)
3160 x += s->img->hmargin;
3161 if (s->slice.y == 0)
3162 y += s->img->vmargin;
3164 if (s->img->pixmap)
3166 if (s->img->mask)
3168 /* We can't set both a clip mask and use XSetClipRectangles
3169 because the latter also sets a clip mask. We also can't
3170 trust on the shape extension to be available
3171 (XShapeCombineRegion). So, compute the rectangle to draw
3172 manually. */
3173 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3174 | GCFunction);
3175 XGCValues xgcv;
3177 xgcv.clip_mask = s->img->mask;
3178 xgcv.clip_x_origin = x - s->slice.x;
3179 xgcv.clip_y_origin = y - s->slice.y;
3180 xgcv.function = GXcopy;
3181 XChangeGC (s->display, s->gc, mask, &xgcv);
3183 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3184 s->slice.x, s->slice.y,
3185 s->slice.width, s->slice.height, x, y);
3186 XSetClipMask (s->display, s->gc, None);
3188 else
3190 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3191 s->slice.x, s->slice.y,
3192 s->slice.width, s->slice.height, x, y);
3194 /* When the image has a mask, we can expect that at
3195 least part of a mouse highlight or a block cursor will
3196 be visible. If the image doesn't have a mask, make
3197 a block cursor visible by drawing a rectangle around
3198 the image. I believe it's looking better if we do
3199 nothing here for mouse-face. */
3200 if (s->hl == DRAW_CURSOR)
3202 int r = eabs (s->img->relief);
3203 x_draw_rectangle (s->f, s->gc, x - r, y - r,
3204 s->slice.width + r*2 - 1,
3205 s->slice.height + r*2 - 1);
3209 else
3210 /* Draw a rectangle if image could not be loaded. */
3211 x_draw_rectangle (s->f, s->gc, x, y,
3212 s->slice.width - 1, s->slice.height - 1);
3216 /* Draw part of the background of glyph string S. X, Y, W, and H
3217 give the rectangle to draw. */
3219 static void
3220 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
3222 if (s->stippled_p)
3224 /* Fill background with a stipple pattern. */
3225 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3226 x_fill_rectangle (s->f, s->gc, x, y, w, h);
3227 XSetFillStyle (s->display, s->gc, FillSolid);
3229 else
3230 x_clear_glyph_string_rect (s, x, y, w, h);
3234 /* Draw image glyph string S.
3236 s->y
3237 s->x +-------------------------
3238 | s->face->box
3240 | +-------------------------
3241 | | s->img->margin
3243 | | +-------------------
3244 | | | the image
3248 static void
3249 x_draw_image_glyph_string (struct glyph_string *s)
3251 int box_line_hwidth = eabs (s->face->box_line_width);
3252 int box_line_vwidth = max (s->face->box_line_width, 0);
3253 int height;
3254 Pixmap pixmap = None;
3256 height = s->height;
3257 if (s->slice.y == 0)
3258 height -= box_line_vwidth;
3259 if (s->slice.y + s->slice.height >= s->img->height)
3260 height -= box_line_vwidth;
3262 /* Fill background with face under the image. Do it only if row is
3263 taller than image or if image has a clip mask to reduce
3264 flickering. */
3265 s->stippled_p = s->face->stipple != 0;
3266 if (height > s->slice.height
3267 || s->img->hmargin
3268 || s->img->vmargin
3269 || s->img->mask
3270 || s->img->pixmap == 0
3271 || s->width != s->background_width)
3273 if (s->img->mask)
3275 /* Create a pixmap as large as the glyph string. Fill it
3276 with the background color. Copy the image to it, using
3277 its mask. Copy the temporary pixmap to the display. */
3278 Screen *screen = FRAME_X_SCREEN (s->f);
3279 int depth = DefaultDepthOfScreen (screen);
3281 /* Create a pixmap as large as the glyph string. */
3282 pixmap = XCreatePixmap (s->display, FRAME_X_DRAWABLE (s->f),
3283 s->background_width,
3284 s->height, depth);
3286 /* Don't clip in the following because we're working on the
3287 pixmap. */
3288 XSetClipMask (s->display, s->gc, None);
3290 /* Fill the pixmap with the background color/stipple. */
3291 if (s->stippled_p)
3293 /* Fill background with a stipple pattern. */
3294 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3295 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
3296 XFillRectangle (s->display, pixmap, s->gc,
3297 0, 0, s->background_width, s->height);
3298 XSetFillStyle (s->display, s->gc, FillSolid);
3299 XSetTSOrigin (s->display, s->gc, 0, 0);
3301 else
3303 XGCValues xgcv;
3304 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3305 &xgcv);
3306 XSetForeground (s->display, s->gc, xgcv.background);
3307 XFillRectangle (s->display, pixmap, s->gc,
3308 0, 0, s->background_width, s->height);
3309 XSetForeground (s->display, s->gc, xgcv.foreground);
3312 else
3314 int x = s->x;
3315 int y = s->y;
3316 int width = s->background_width;
3318 if (s->first_glyph->left_box_line_p
3319 && s->slice.x == 0)
3321 x += box_line_hwidth;
3322 width -= box_line_hwidth;
3325 if (s->slice.y == 0)
3326 y += box_line_vwidth;
3328 x_draw_glyph_string_bg_rect (s, x, y, width, height);
3331 s->background_filled_p = true;
3334 /* Draw the foreground. */
3335 #ifdef USE_CAIRO
3336 if (s->img->cr_data)
3338 cairo_t *cr = x_begin_cr_clip (s->f, s->gc);
3340 int x = s->x + s->img->hmargin;
3341 int y = s->y + s->img->vmargin;
3342 int width = s->background_width;
3344 cairo_set_source_surface (cr, s->img->cr_data,
3345 x - s->slice.x,
3346 y - s->slice.y);
3347 cairo_rectangle (cr, x, y, width, height);
3348 cairo_fill (cr);
3349 x_end_cr_clip (s->f);
3351 else
3352 #endif
3353 if (pixmap != None)
3355 x_draw_image_foreground_1 (s, pixmap);
3356 x_set_glyph_string_clipping (s);
3357 XCopyArea (s->display, pixmap, FRAME_X_DRAWABLE (s->f), s->gc,
3358 0, 0, s->background_width, s->height, s->x, s->y);
3359 XFreePixmap (s->display, pixmap);
3361 else
3362 x_draw_image_foreground (s);
3364 /* If we must draw a relief around the image, do it. */
3365 if (s->img->relief
3366 || s->hl == DRAW_IMAGE_RAISED
3367 || s->hl == DRAW_IMAGE_SUNKEN)
3368 x_draw_image_relief (s);
3372 /* Draw stretch glyph string S. */
3374 static void
3375 x_draw_stretch_glyph_string (struct glyph_string *s)
3377 eassert (s->first_glyph->type == STRETCH_GLYPH);
3379 if (s->hl == DRAW_CURSOR
3380 && !x_stretch_cursor_p)
3382 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
3383 wide as the stretch glyph. */
3384 int width, background_width = s->background_width;
3385 int x = s->x;
3387 if (!s->row->reversed_p)
3389 int left_x = window_box_left_offset (s->w, TEXT_AREA);
3391 if (x < left_x)
3393 background_width -= left_x - x;
3394 x = left_x;
3397 else
3399 /* In R2L rows, draw the cursor on the right edge of the
3400 stretch glyph. */
3401 int right_x = window_box_right (s->w, TEXT_AREA);
3403 if (x + background_width > right_x)
3404 background_width -= x - right_x;
3405 x += background_width;
3407 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
3408 if (s->row->reversed_p)
3409 x -= width;
3411 /* Draw cursor. */
3412 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
3414 /* Clear rest using the GC of the original non-cursor face. */
3415 if (width < background_width)
3417 int y = s->y;
3418 int w = background_width - width, h = s->height;
3419 XRectangle r;
3420 GC gc;
3422 if (!s->row->reversed_p)
3423 x += width;
3424 else
3425 x = s->x;
3426 if (s->row->mouse_face_p
3427 && cursor_in_mouse_face_p (s->w))
3429 x_set_mouse_face_gc (s);
3430 gc = s->gc;
3432 else
3433 gc = s->face->gc;
3435 get_glyph_string_clip_rect (s, &r);
3436 x_set_clip_rectangles (s->f, gc, &r, 1);
3438 if (s->face->stipple)
3440 /* Fill background with a stipple pattern. */
3441 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3442 x_fill_rectangle (s->f, gc, x, y, w, h);
3443 XSetFillStyle (s->display, gc, FillSolid);
3445 else
3447 XGCValues xgcv;
3448 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3449 XSetForeground (s->display, gc, xgcv.background);
3450 x_fill_rectangle (s->f, gc, x, y, w, h);
3451 XSetForeground (s->display, gc, xgcv.foreground);
3454 x_reset_clip_rectangles (s->f, gc);
3457 else if (!s->background_filled_p)
3459 int background_width = s->background_width;
3460 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
3462 /* Don't draw into left margin, fringe or scrollbar area
3463 except for header line and mode line. */
3464 if (x < left_x && !s->row->mode_line_p)
3466 background_width -= left_x - x;
3467 x = left_x;
3469 if (background_width > 0)
3470 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
3473 s->background_filled_p = true;
3476 static void
3477 x_get_scale_factor(Display *disp, int *scale_x, int *scale_y)
3479 const int base_res = 96;
3480 struct x_display_info * dpyinfo = x_display_info_for_display (disp);
3482 *scale_x = *scale_y = 1;
3484 if (dpyinfo)
3486 *scale_x = floor (dpyinfo->resx / base_res);
3487 *scale_y = floor (dpyinfo->resy / base_res);
3492 Draw a wavy line under S. The wave fills wave_height pixels from y0.
3494 x0 wave_length = 2
3496 y0 * * * * *
3497 |* * * * * * * * *
3498 wave_height = 3 | * * * *
3501 static void
3502 x_draw_underwave (struct glyph_string *s)
3504 /* Adjust for scale/HiDPI. */
3505 int scale_x, scale_y;
3507 x_get_scale_factor (s->display, &scale_x, &scale_y);
3509 int wave_height = 3 * scale_y, wave_length = 2 * scale_x, thickness = scale_y;
3511 #ifdef USE_CAIRO
3512 x_draw_horizontal_wave (s->f, s->gc, s->x, s->ybase - wave_height + 3,
3513 s->width, wave_height, wave_length);
3514 #else /* not USE_CAIRO */
3515 int dx, dy, x0, y0, width, x1, y1, x2, y2, xmax;
3516 bool odd;
3517 XRectangle wave_clip, string_clip, final_clip;
3519 dx = wave_length;
3520 dy = wave_height - 1;
3521 x0 = s->x;
3522 y0 = s->ybase + wave_height / 2;
3523 width = s->width;
3524 xmax = x0 + width;
3526 /* Find and set clipping rectangle */
3528 wave_clip.x = x0;
3529 wave_clip.y = y0;
3530 wave_clip.width = width;
3531 wave_clip.height = wave_height;
3532 get_glyph_string_clip_rect (s, &string_clip);
3534 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
3535 return;
3537 XSetClipRectangles (s->display, s->gc, 0, 0, &final_clip, 1, Unsorted);
3539 /* Draw the waves */
3541 x1 = x0 - (x0 % dx);
3542 x2 = x1 + dx;
3543 odd = (x1 / dx) & 1;
3544 y1 = y2 = y0;
3546 if (odd)
3547 y1 += dy;
3548 else
3549 y2 += dy;
3551 if (INT_MAX - dx < xmax)
3552 emacs_abort ();
3554 while (x1 <= xmax)
3556 XSetLineAttributes (s->display, s->gc, thickness, LineSolid, CapButt,
3557 JoinRound);
3558 XDrawLine (s->display, FRAME_X_DRAWABLE (s->f), s->gc, x1, y1, x2, y2);
3559 x1 = x2, y1 = y2;
3560 x2 += dx, y2 = y0 + odd*dy;
3561 odd = !odd;
3564 /* Restore previous clipping rectangle(s) */
3565 XSetClipRectangles (s->display, s->gc, 0, 0, s->clip, s->num_clips, Unsorted);
3566 #endif /* not USE_CAIRO */
3570 /* Draw glyph string S. */
3572 static void
3573 x_draw_glyph_string (struct glyph_string *s)
3575 bool relief_drawn_p = false;
3577 /* If S draws into the background of its successors, draw the
3578 background of the successors first so that S can draw into it.
3579 This makes S->next use XDrawString instead of XDrawImageString. */
3580 if (s->next && s->right_overhang && !s->for_overlaps)
3582 int width;
3583 struct glyph_string *next;
3585 for (width = 0, next = s->next;
3586 next && width < s->right_overhang;
3587 width += next->width, next = next->next)
3588 if (next->first_glyph->type != IMAGE_GLYPH)
3590 x_set_glyph_string_gc (next);
3591 x_set_glyph_string_clipping (next);
3592 if (next->first_glyph->type == STRETCH_GLYPH)
3593 x_draw_stretch_glyph_string (next);
3594 else
3595 x_draw_glyph_string_background (next, true);
3596 next->num_clips = 0;
3600 /* Set up S->gc, set clipping and draw S. */
3601 x_set_glyph_string_gc (s);
3603 /* Draw relief (if any) in advance for char/composition so that the
3604 glyph string can be drawn over it. */
3605 if (!s->for_overlaps
3606 && s->face->box != FACE_NO_BOX
3607 && (s->first_glyph->type == CHAR_GLYPH
3608 || s->first_glyph->type == COMPOSITE_GLYPH))
3611 x_set_glyph_string_clipping (s);
3612 x_draw_glyph_string_background (s, true);
3613 x_draw_glyph_string_box (s);
3614 x_set_glyph_string_clipping (s);
3615 relief_drawn_p = true;
3617 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
3618 && !s->clip_tail
3619 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
3620 || (s->next && s->next->hl != s->hl && s->right_overhang)))
3621 /* We must clip just this glyph. left_overhang part has already
3622 drawn when s->prev was drawn, and right_overhang part will be
3623 drawn later when s->next is drawn. */
3624 x_set_glyph_string_clipping_exactly (s, s);
3625 else
3626 x_set_glyph_string_clipping (s);
3628 switch (s->first_glyph->type)
3630 case IMAGE_GLYPH:
3631 x_draw_image_glyph_string (s);
3632 break;
3634 case XWIDGET_GLYPH:
3635 x_draw_xwidget_glyph_string (s);
3636 break;
3638 case STRETCH_GLYPH:
3639 x_draw_stretch_glyph_string (s);
3640 break;
3642 case CHAR_GLYPH:
3643 if (s->for_overlaps)
3644 s->background_filled_p = true;
3645 else
3646 x_draw_glyph_string_background (s, false);
3647 x_draw_glyph_string_foreground (s);
3648 break;
3650 case COMPOSITE_GLYPH:
3651 if (s->for_overlaps || (s->cmp_from > 0
3652 && ! s->first_glyph->u.cmp.automatic))
3653 s->background_filled_p = true;
3654 else
3655 x_draw_glyph_string_background (s, true);
3656 x_draw_composite_glyph_string_foreground (s);
3657 break;
3659 case GLYPHLESS_GLYPH:
3660 if (s->for_overlaps)
3661 s->background_filled_p = true;
3662 else
3663 x_draw_glyph_string_background (s, true);
3664 x_draw_glyphless_glyph_string_foreground (s);
3665 break;
3667 default:
3668 emacs_abort ();
3671 if (!s->for_overlaps)
3673 /* Draw underline. */
3674 if (s->face->underline_p)
3676 if (s->face->underline_type == FACE_UNDER_WAVE)
3678 if (s->face->underline_defaulted_p)
3679 x_draw_underwave (s);
3680 else
3682 XGCValues xgcv;
3683 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3684 XSetForeground (s->display, s->gc, s->face->underline_color);
3685 x_draw_underwave (s);
3686 XSetForeground (s->display, s->gc, xgcv.foreground);
3689 else if (s->face->underline_type == FACE_UNDER_LINE)
3691 unsigned long thickness, position;
3692 int y;
3694 if (s->prev && s->prev->face->underline_p
3695 && s->prev->face->underline_type == FACE_UNDER_LINE)
3697 /* We use the same underline style as the previous one. */
3698 thickness = s->prev->underline_thickness;
3699 position = s->prev->underline_position;
3701 else
3703 struct font *font = font_for_underline_metrics (s);
3705 /* Get the underline thickness. Default is 1 pixel. */
3706 if (font && font->underline_thickness > 0)
3707 thickness = font->underline_thickness;
3708 else
3709 thickness = 1;
3710 if (x_underline_at_descent_line)
3711 position = (s->height - thickness) - (s->ybase - s->y);
3712 else
3714 /* Get the underline position. This is the recommended
3715 vertical offset in pixels from the baseline to the top of
3716 the underline. This is a signed value according to the
3717 specs, and its default is
3719 ROUND ((maximum descent) / 2), with
3720 ROUND(x) = floor (x + 0.5) */
3722 if (x_use_underline_position_properties
3723 && font && font->underline_position >= 0)
3724 position = font->underline_position;
3725 else if (font)
3726 position = (font->descent + 1) / 2;
3727 else
3728 position = underline_minimum_offset;
3730 position = max (position, underline_minimum_offset);
3732 /* Check the sanity of thickness and position. We should
3733 avoid drawing underline out of the current line area. */
3734 if (s->y + s->height <= s->ybase + position)
3735 position = (s->height - 1) - (s->ybase - s->y);
3736 if (s->y + s->height < s->ybase + position + thickness)
3737 thickness = (s->y + s->height) - (s->ybase + position);
3738 s->underline_thickness = thickness;
3739 s->underline_position = position;
3740 y = s->ybase + position;
3741 if (s->face->underline_defaulted_p)
3742 x_fill_rectangle (s->f, s->gc,
3743 s->x, y, s->width, thickness);
3744 else
3746 XGCValues xgcv;
3747 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3748 XSetForeground (s->display, s->gc, s->face->underline_color);
3749 x_fill_rectangle (s->f, s->gc,
3750 s->x, y, s->width, thickness);
3751 XSetForeground (s->display, s->gc, xgcv.foreground);
3755 /* Draw overline. */
3756 if (s->face->overline_p)
3758 unsigned long dy = 0, h = 1;
3760 if (s->face->overline_color_defaulted_p)
3761 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3762 s->width, h);
3763 else
3765 XGCValues xgcv;
3766 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3767 XSetForeground (s->display, s->gc, s->face->overline_color);
3768 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3769 s->width, h);
3770 XSetForeground (s->display, s->gc, xgcv.foreground);
3774 /* Draw strike-through. */
3775 if (s->face->strike_through_p)
3777 /* Y-coordinate and height of the glyph string's first
3778 glyph. We cannot use s->y and s->height because those
3779 could be larger if there are taller display elements
3780 (e.g., characters displayed with a larger font) in the
3781 same glyph row. */
3782 int glyph_y = s->ybase - s->first_glyph->ascent;
3783 int glyph_height = s->first_glyph->ascent + s->first_glyph->descent;
3784 /* Strike-through width and offset from the glyph string's
3785 top edge. */
3786 unsigned long h = 1;
3787 unsigned long dy = (glyph_height - h) / 2;
3789 if (s->face->strike_through_color_defaulted_p)
3790 x_fill_rectangle (s->f, s->gc, s->x, glyph_y + dy,
3791 s->width, h);
3792 else
3794 XGCValues xgcv;
3795 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3796 XSetForeground (s->display, s->gc, s->face->strike_through_color);
3797 x_fill_rectangle (s->f, s->gc, s->x, glyph_y + dy,
3798 s->width, h);
3799 XSetForeground (s->display, s->gc, xgcv.foreground);
3803 /* Draw relief if not yet drawn. */
3804 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
3805 x_draw_glyph_string_box (s);
3807 if (s->prev)
3809 struct glyph_string *prev;
3811 for (prev = s->prev; prev; prev = prev->prev)
3812 if (prev->hl != s->hl
3813 && prev->x + prev->width + prev->right_overhang > s->x)
3815 /* As prev was drawn while clipped to its own area, we
3816 must draw the right_overhang part using s->hl now. */
3817 enum draw_glyphs_face save = prev->hl;
3819 prev->hl = s->hl;
3820 x_set_glyph_string_gc (prev);
3821 x_set_glyph_string_clipping_exactly (s, prev);
3822 if (prev->first_glyph->type == CHAR_GLYPH)
3823 x_draw_glyph_string_foreground (prev);
3824 else
3825 x_draw_composite_glyph_string_foreground (prev);
3826 x_reset_clip_rectangles (prev->f, prev->gc);
3827 prev->hl = save;
3828 prev->num_clips = 0;
3832 if (s->next)
3834 struct glyph_string *next;
3836 for (next = s->next; next; next = next->next)
3837 if (next->hl != s->hl
3838 && next->x - next->left_overhang < s->x + s->width)
3840 /* As next will be drawn while clipped to its own area,
3841 we must draw the left_overhang part using s->hl now. */
3842 enum draw_glyphs_face save = next->hl;
3844 next->hl = s->hl;
3845 x_set_glyph_string_gc (next);
3846 x_set_glyph_string_clipping_exactly (s, next);
3847 if (next->first_glyph->type == CHAR_GLYPH)
3848 x_draw_glyph_string_foreground (next);
3849 else
3850 x_draw_composite_glyph_string_foreground (next);
3851 x_reset_clip_rectangles (next->f, next->gc);
3852 next->hl = save;
3853 next->num_clips = 0;
3854 next->clip_head = s->next;
3859 /* Reset clipping. */
3860 x_reset_clip_rectangles (s->f, s->gc);
3861 s->num_clips = 0;
3864 /* Shift display to make room for inserted glyphs. */
3866 static void
3867 x_shift_glyphs_for_insert (struct frame *f, int x, int y, int width, int height, int shift_by)
3869 /* Never called on a GUI frame, see
3870 http://lists.gnu.org/archive/html/emacs-devel/2015-05/msg00456.html
3872 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f), FRAME_X_DRAWABLE (f),
3873 f->output_data.x->normal_gc,
3874 x, y, width, height,
3875 x + shift_by, y);
3878 /* Delete N glyphs at the nominal cursor position. Not implemented
3879 for X frames. */
3881 static void
3882 x_delete_glyphs (struct frame *f, register int n)
3884 emacs_abort ();
3888 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
3889 If they are <= 0, this is probably an error. */
3891 static ATTRIBUTE_UNUSED void
3892 x_clear_area1 (Display *dpy, Window window,
3893 int x, int y, int width, int height, int exposures)
3895 eassert (width > 0 && height > 0);
3896 XClearArea (dpy, window, x, y, width, height, exposures);
3899 void
3900 x_clear_area (struct frame *f, int x, int y, int width, int height)
3902 #ifdef USE_CAIRO
3903 cairo_t *cr;
3905 eassert (width > 0 && height > 0);
3907 cr = x_begin_cr_clip (f, NULL);
3908 x_set_cr_source_with_gc_background (f, f->output_data.x->normal_gc);
3909 cairo_rectangle (cr, x, y, width, height);
3910 cairo_fill (cr);
3911 x_end_cr_clip (f);
3912 #else
3913 if (FRAME_X_DOUBLE_BUFFERED_P (f))
3914 XFillRectangle (FRAME_X_DISPLAY (f),
3915 FRAME_X_DRAWABLE (f),
3916 f->output_data.x->reverse_gc,
3917 x, y, width, height);
3918 else
3919 x_clear_area1 (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3920 x, y, width, height, False);
3921 #endif
3925 /* Clear an entire frame. */
3927 static void
3928 x_clear_frame (struct frame *f)
3930 /* Clearing the frame will erase any cursor, so mark them all as no
3931 longer visible. */
3932 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
3934 block_input ();
3936 font_drop_xrender_surfaces (f);
3937 x_clear_window (f);
3939 /* We have to clear the scroll bars. If we have changed colors or
3940 something like that, then they should be notified. */
3941 x_scroll_bar_clear (f);
3943 XFlush (FRAME_X_DISPLAY (f));
3945 unblock_input ();
3948 /* RIF: Show hourglass cursor on frame F. */
3950 static void
3951 x_show_hourglass (struct frame *f)
3953 Display *dpy = FRAME_X_DISPLAY (f);
3955 if (dpy)
3957 struct x_output *x = FRAME_X_OUTPUT (f);
3958 #ifdef USE_X_TOOLKIT
3959 if (x->widget)
3960 #else
3961 if (FRAME_OUTER_WINDOW (f))
3962 #endif
3964 x->hourglass_p = true;
3966 if (!x->hourglass_window)
3968 unsigned long mask = CWCursor;
3969 XSetWindowAttributes attrs;
3970 #ifdef USE_GTK
3971 Window parent = FRAME_X_WINDOW (f);
3972 #else
3973 Window parent = FRAME_OUTER_WINDOW (f);
3974 #endif
3975 attrs.cursor = x->hourglass_cursor;
3977 x->hourglass_window = XCreateWindow
3978 (dpy, parent, 0, 0, 32000, 32000, 0, 0,
3979 InputOnly, CopyFromParent, mask, &attrs);
3982 XMapRaised (dpy, x->hourglass_window);
3983 XFlush (dpy);
3988 /* RIF: Cancel hourglass cursor on frame F. */
3990 static void
3991 x_hide_hourglass (struct frame *f)
3993 struct x_output *x = FRAME_X_OUTPUT (f);
3995 /* Watch out for newly created frames. */
3996 if (x->hourglass_window)
3998 XUnmapWindow (FRAME_X_DISPLAY (f), x->hourglass_window);
3999 /* Sync here because XTread_socket looks at the
4000 hourglass_p flag that is reset to zero below. */
4001 XSync (FRAME_X_DISPLAY (f), False);
4002 x->hourglass_p = false;
4006 /* Invert the middle quarter of the frame for .15 sec. */
4008 static void
4009 XTflash (struct frame *f)
4011 block_input ();
4014 #ifdef USE_GTK
4015 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
4016 when the scroll bars and the edit widget share the same X window. */
4017 GdkWindow *window = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
4018 #ifdef HAVE_GTK3
4019 cairo_t *cr = gdk_cairo_create (window);
4020 cairo_set_source_rgb (cr, 1, 1, 1);
4021 cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
4022 #define XFillRectangle(d, win, gc, x, y, w, h) \
4023 do { \
4024 cairo_rectangle (cr, x, y, w, h); \
4025 cairo_fill (cr); \
4027 while (false)
4028 #else /* ! HAVE_GTK3 */
4029 GdkGCValues vals;
4030 GdkGC *gc;
4031 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
4032 ^ FRAME_BACKGROUND_PIXEL (f));
4033 vals.function = GDK_XOR;
4034 gc = gdk_gc_new_with_values (window,
4035 &vals, GDK_GC_FUNCTION | GDK_GC_FOREGROUND);
4036 #define XFillRectangle(d, win, gc, x, y, w, h) \
4037 gdk_draw_rectangle (window, gc, true, x, y, w, h)
4038 #endif /* ! HAVE_GTK3 */
4039 #else /* ! USE_GTK */
4040 GC gc;
4042 /* Create a GC that will use the GXxor function to flip foreground
4043 pixels into background pixels. */
4045 XGCValues values;
4047 values.function = GXxor;
4048 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
4049 ^ FRAME_BACKGROUND_PIXEL (f));
4051 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4052 GCFunction | GCForeground, &values);
4054 #endif
4056 /* Get the height not including a menu bar widget. */
4057 int height = FRAME_PIXEL_HEIGHT (f);
4058 /* Height of each line to flash. */
4059 int flash_height = FRAME_LINE_HEIGHT (f);
4060 /* These will be the left and right margins of the rectangles. */
4061 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
4062 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
4063 int width = flash_right - flash_left;
4065 /* If window is tall, flash top and bottom line. */
4066 if (height > 3 * FRAME_LINE_HEIGHT (f))
4068 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4069 flash_left,
4070 (FRAME_INTERNAL_BORDER_WIDTH (f)
4071 + FRAME_TOP_MARGIN_HEIGHT (f)),
4072 width, flash_height);
4073 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4074 flash_left,
4075 (height - flash_height
4076 - FRAME_INTERNAL_BORDER_WIDTH (f)),
4077 width, flash_height);
4080 else
4081 /* If it is short, flash it all. */
4082 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4083 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
4084 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
4086 x_flush (f);
4089 struct timespec delay = make_timespec (0, 150 * 1000 * 1000);
4090 struct timespec wakeup = timespec_add (current_timespec (), delay);
4092 /* Keep waiting until past the time wakeup or any input gets
4093 available. */
4094 while (! detect_input_pending ())
4096 struct timespec current = current_timespec ();
4097 struct timespec timeout;
4099 /* Break if result would not be positive. */
4100 if (timespec_cmp (wakeup, current) <= 0)
4101 break;
4103 /* How long `select' should wait. */
4104 timeout = make_timespec (0, 10 * 1000 * 1000);
4106 /* Try to wait that long--but we might wake up sooner. */
4107 pselect (0, NULL, NULL, NULL, &timeout, NULL);
4111 /* If window is tall, flash top and bottom line. */
4112 if (height > 3 * FRAME_LINE_HEIGHT (f))
4114 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4115 flash_left,
4116 (FRAME_INTERNAL_BORDER_WIDTH (f)
4117 + FRAME_TOP_MARGIN_HEIGHT (f)),
4118 width, flash_height);
4119 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4120 flash_left,
4121 (height - flash_height
4122 - FRAME_INTERNAL_BORDER_WIDTH (f)),
4123 width, flash_height);
4125 else
4126 /* If it is short, flash it all. */
4127 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4128 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
4129 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
4131 #ifdef USE_GTK
4132 #ifdef HAVE_GTK3
4133 cairo_destroy (cr);
4134 #else
4135 g_object_unref (G_OBJECT (gc));
4136 #endif
4137 #undef XFillRectangle
4138 #else
4139 XFreeGC (FRAME_X_DISPLAY (f), gc);
4140 #endif
4141 x_flush (f);
4145 unblock_input ();
4149 static void
4150 XTtoggle_invisible_pointer (struct frame *f, bool invisible)
4152 block_input ();
4153 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, invisible);
4154 unblock_input ();
4158 /* Make audible bell. */
4160 static void
4161 XTring_bell (struct frame *f)
4163 if (FRAME_X_DISPLAY (f))
4165 if (visible_bell)
4166 XTflash (f);
4167 else
4169 block_input ();
4170 #ifdef HAVE_XKB
4171 XkbBell (FRAME_X_DISPLAY (f), None, 0, None);
4172 #else
4173 XBell (FRAME_X_DISPLAY (f), 0);
4174 #endif
4175 XFlush (FRAME_X_DISPLAY (f));
4176 unblock_input ();
4181 /***********************************************************************
4182 Line Dance
4183 ***********************************************************************/
4185 /* Perform an insert-lines or delete-lines operation, inserting N
4186 lines or deleting -N lines at vertical position VPOS. */
4188 static void
4189 x_ins_del_lines (struct frame *f, int vpos, int n)
4191 emacs_abort ();
4195 /* Scroll part of the display as described by RUN. */
4197 static void
4198 x_scroll_run (struct window *w, struct run *run)
4200 struct frame *f = XFRAME (w->frame);
4201 int x, y, width, height, from_y, to_y, bottom_y;
4203 /* Get frame-relative bounding box of the text display area of W,
4204 without mode lines. Include in this box the left and right
4205 fringe of W. */
4206 window_box (w, ANY_AREA, &x, &y, &width, &height);
4208 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
4209 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
4210 bottom_y = y + height;
4212 if (to_y < from_y)
4214 /* Scrolling up. Make sure we don't copy part of the mode
4215 line at the bottom. */
4216 if (from_y + run->height > bottom_y)
4217 height = bottom_y - from_y;
4218 else
4219 height = run->height;
4221 else
4223 /* Scrolling down. Make sure we don't copy over the mode line.
4224 at the bottom. */
4225 if (to_y + run->height > bottom_y)
4226 height = bottom_y - to_y;
4227 else
4228 height = run->height;
4231 block_input ();
4233 /* Cursor off. Will be switched on again in x_update_window_end. */
4234 x_clear_cursor (w);
4236 #ifdef USE_CAIRO
4237 SET_FRAME_GARBAGED (f);
4238 #else
4239 XCopyArea (FRAME_X_DISPLAY (f),
4240 FRAME_X_DRAWABLE (f), FRAME_X_DRAWABLE (f),
4241 f->output_data.x->normal_gc,
4242 x, from_y,
4243 width, height,
4244 x, to_y);
4245 #endif
4247 unblock_input ();
4252 /***********************************************************************
4253 Exposure Events
4254 ***********************************************************************/
4257 static void
4258 frame_highlight (struct frame *f)
4260 /* We used to only do this if Vx_no_window_manager was non-nil, but
4261 the ICCCM (section 4.1.6) says that the window's border pixmap
4262 and border pixel are window attributes which are "private to the
4263 client", so we can always change it to whatever we want. */
4264 block_input ();
4265 /* I recently started to get errors in this XSetWindowBorder, depending on
4266 the window-manager in use, tho something more is at play since I've been
4267 using that same window-manager binary for ever. Let's not crash just
4268 because of this (bug#9310). */
4269 x_catch_errors (FRAME_X_DISPLAY (f));
4270 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4271 f->output_data.x->border_pixel);
4272 x_uncatch_errors ();
4273 unblock_input ();
4274 x_update_cursor (f, true);
4275 x_set_frame_alpha (f);
4278 static void
4279 frame_unhighlight (struct frame *f)
4281 /* We used to only do this if Vx_no_window_manager was non-nil, but
4282 the ICCCM (section 4.1.6) says that the window's border pixmap
4283 and border pixel are window attributes which are "private to the
4284 client", so we can always change it to whatever we want. */
4285 block_input ();
4286 /* Same as above for XSetWindowBorder (bug#9310). */
4287 x_catch_errors (FRAME_X_DISPLAY (f));
4288 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4289 f->output_data.x->border_tile);
4290 x_uncatch_errors ();
4291 unblock_input ();
4292 x_update_cursor (f, true);
4293 x_set_frame_alpha (f);
4296 /* The focus has changed. Update the frames as necessary to reflect
4297 the new situation. Note that we can't change the selected frame
4298 here, because the Lisp code we are interrupting might become confused.
4299 Each event gets marked with the frame in which it occurred, so the
4300 Lisp code can tell when the switch took place by examining the events. */
4302 static void
4303 x_new_focus_frame (struct x_display_info *dpyinfo, struct frame *frame)
4305 struct frame *old_focus = dpyinfo->x_focus_frame;
4307 if (frame != dpyinfo->x_focus_frame)
4309 /* Set this before calling other routines, so that they see
4310 the correct value of x_focus_frame. */
4311 dpyinfo->x_focus_frame = frame;
4313 if (old_focus && old_focus->auto_lower)
4314 x_lower_frame (old_focus);
4316 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
4317 dpyinfo->x_pending_autoraise_frame = dpyinfo->x_focus_frame;
4318 else
4319 dpyinfo->x_pending_autoraise_frame = NULL;
4322 x_frame_rehighlight (dpyinfo);
4325 /* Handle FocusIn and FocusOut state changes for FRAME.
4326 If FRAME has focus and there exists more than one frame, puts
4327 a FOCUS_IN_EVENT into *BUFP. */
4329 static void
4330 x_focus_changed (int type, int state, struct x_display_info *dpyinfo, struct frame *frame, struct input_event *bufp)
4332 if (type == FocusIn)
4334 if (dpyinfo->x_focus_event_frame != frame)
4336 x_new_focus_frame (dpyinfo, frame);
4337 dpyinfo->x_focus_event_frame = frame;
4339 /* Don't stop displaying the initial startup message
4340 for a switch-frame event we don't need. */
4341 /* When run as a daemon, Vterminal_frame is always NIL. */
4342 bufp->arg = (((NILP (Vterminal_frame)
4343 || ! FRAME_X_P (XFRAME (Vterminal_frame))
4344 || EQ (Fdaemonp (), Qt))
4345 && CONSP (Vframe_list)
4346 && !NILP (XCDR (Vframe_list)))
4347 ? Qt : Qnil);
4348 bufp->kind = FOCUS_IN_EVENT;
4349 XSETFRAME (bufp->frame_or_window, frame);
4352 frame->output_data.x->focus_state |= state;
4354 #ifdef HAVE_X_I18N
4355 if (FRAME_XIC (frame))
4356 XSetICFocus (FRAME_XIC (frame));
4357 #endif
4359 else if (type == FocusOut)
4361 frame->output_data.x->focus_state &= ~state;
4363 if (dpyinfo->x_focus_event_frame == frame)
4365 dpyinfo->x_focus_event_frame = 0;
4366 x_new_focus_frame (dpyinfo, 0);
4368 bufp->kind = FOCUS_OUT_EVENT;
4369 XSETFRAME (bufp->frame_or_window, frame);
4372 #ifdef HAVE_X_I18N
4373 if (FRAME_XIC (frame))
4374 XUnsetICFocus (FRAME_XIC (frame));
4375 #endif
4376 if (frame->pointer_invisible)
4377 XTtoggle_invisible_pointer (frame, false);
4381 /* Return the Emacs frame-object corresponding to an X window.
4382 It could be the frame's main window or an icon window. */
4384 static struct frame *
4385 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4387 Lisp_Object tail, frame;
4388 struct frame *f;
4390 if (wdesc == None)
4391 return NULL;
4393 FOR_EACH_FRAME (tail, frame)
4395 f = XFRAME (frame);
4396 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4397 continue;
4398 if (f->output_data.x->hourglass_window == wdesc)
4399 return f;
4400 #ifdef USE_X_TOOLKIT
4401 if ((f->output_data.x->edit_widget
4402 && XtWindow (f->output_data.x->edit_widget) == wdesc)
4403 /* A tooltip frame? */
4404 || (!f->output_data.x->edit_widget
4405 && FRAME_X_WINDOW (f) == wdesc)
4406 || f->output_data.x->icon_desc == wdesc)
4407 return f;
4408 #else /* not USE_X_TOOLKIT */
4409 #ifdef USE_GTK
4410 if (f->output_data.x->edit_widget)
4412 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4413 struct x_output *x = f->output_data.x;
4414 if (gwdesc != 0 && gwdesc == x->edit_widget)
4415 return f;
4417 #endif /* USE_GTK */
4418 if (FRAME_X_WINDOW (f) == wdesc
4419 || f->output_data.x->icon_desc == wdesc)
4420 return f;
4421 #endif /* not USE_X_TOOLKIT */
4423 return 0;
4426 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
4428 /* Like x_window_to_frame but also compares the window with the widget's
4429 windows. */
4431 static struct frame *
4432 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4434 Lisp_Object tail, frame;
4435 struct frame *f, *found = NULL;
4436 struct x_output *x;
4438 if (wdesc == None)
4439 return NULL;
4441 FOR_EACH_FRAME (tail, frame)
4443 if (found)
4444 break;
4445 f = XFRAME (frame);
4446 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
4448 /* This frame matches if the window is any of its widgets. */
4449 x = f->output_data.x;
4450 if (x->hourglass_window == wdesc)
4451 found = f;
4452 else if (x->widget)
4454 #ifdef USE_GTK
4455 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4456 if (gwdesc != 0
4457 && gtk_widget_get_toplevel (gwdesc) == x->widget)
4458 found = f;
4459 #else
4460 if (wdesc == XtWindow (x->widget)
4461 || wdesc == XtWindow (x->column_widget)
4462 || wdesc == XtWindow (x->edit_widget))
4463 found = f;
4464 /* Match if the window is this frame's menubar. */
4465 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
4466 found = f;
4467 #endif
4469 else if (FRAME_X_WINDOW (f) == wdesc)
4470 /* A tooltip frame. */
4471 found = f;
4475 return found;
4478 /* Likewise, but consider only the menu bar widget. */
4480 static struct frame *
4481 x_menubar_window_to_frame (struct x_display_info *dpyinfo,
4482 const XEvent *event)
4484 Window wdesc = event->xany.window;
4485 Lisp_Object tail, frame;
4486 struct frame *f;
4487 struct x_output *x;
4489 if (wdesc == None)
4490 return NULL;
4492 FOR_EACH_FRAME (tail, frame)
4494 f = XFRAME (frame);
4495 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4496 continue;
4497 x = f->output_data.x;
4498 #ifdef USE_GTK
4499 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
4500 return f;
4501 #else
4502 /* Match if the window is this frame's menubar. */
4503 if (x->menubar_widget
4504 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
4505 return f;
4506 #endif
4508 return 0;
4511 /* Return the frame whose principal (outermost) window is WDESC.
4512 If WDESC is some other (smaller) window, we return 0. */
4514 struct frame *
4515 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4517 Lisp_Object tail, frame;
4518 struct frame *f;
4519 struct x_output *x;
4521 if (wdesc == None)
4522 return NULL;
4524 FOR_EACH_FRAME (tail, frame)
4526 f = XFRAME (frame);
4527 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4528 continue;
4529 x = f->output_data.x;
4531 if (x->widget)
4533 /* This frame matches if the window is its topmost widget. */
4534 #ifdef USE_GTK
4535 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4536 if (gwdesc == x->widget)
4537 return f;
4538 #else
4539 if (wdesc == XtWindow (x->widget))
4540 return f;
4541 #endif
4543 else if (FRAME_X_WINDOW (f) == wdesc)
4544 /* Tooltip frame. */
4545 return f;
4547 return 0;
4550 #else /* !USE_X_TOOLKIT && !USE_GTK */
4552 #define x_any_window_to_frame(d, i) x_window_to_frame (d, i)
4553 #define x_top_window_to_frame(d, i) x_window_to_frame (d, i)
4555 #endif /* USE_X_TOOLKIT || USE_GTK */
4557 /* The focus may have changed. Figure out if it is a real focus change,
4558 by checking both FocusIn/Out and Enter/LeaveNotify events.
4560 Returns FOCUS_IN_EVENT event in *BUFP. */
4562 static void
4563 x_detect_focus_change (struct x_display_info *dpyinfo, struct frame *frame,
4564 const XEvent *event, struct input_event *bufp)
4566 if (!frame)
4567 return;
4569 switch (event->type)
4571 case EnterNotify:
4572 case LeaveNotify:
4574 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
4575 int focus_state
4576 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
4578 if (event->xcrossing.detail != NotifyInferior
4579 && event->xcrossing.focus
4580 && ! (focus_state & FOCUS_EXPLICIT))
4581 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
4582 FOCUS_IMPLICIT,
4583 dpyinfo, frame, bufp);
4585 break;
4587 case FocusIn:
4588 case FocusOut:
4589 x_focus_changed (event->type,
4590 (event->xfocus.detail == NotifyPointer ?
4591 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
4592 dpyinfo, frame, bufp);
4593 break;
4595 case ClientMessage:
4596 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
4598 enum xembed_message msg = event->xclient.data.l[1];
4599 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
4600 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
4602 break;
4607 #if !defined USE_X_TOOLKIT && !defined USE_GTK
4608 /* Handle an event saying the mouse has moved out of an Emacs frame. */
4610 void
4611 x_mouse_leave (struct x_display_info *dpyinfo)
4613 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
4615 #endif
4617 /* The focus has changed, or we have redirected a frame's focus to
4618 another frame (this happens when a frame uses a surrogate
4619 mini-buffer frame). Shift the highlight as appropriate.
4621 The FRAME argument doesn't necessarily have anything to do with which
4622 frame is being highlighted or un-highlighted; we only use it to find
4623 the appropriate X display info. */
4625 static void
4626 XTframe_rehighlight (struct frame *frame)
4628 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
4631 static void
4632 x_frame_rehighlight (struct x_display_info *dpyinfo)
4634 struct frame *old_highlight = dpyinfo->x_highlight_frame;
4636 if (dpyinfo->x_focus_frame)
4638 dpyinfo->x_highlight_frame
4639 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
4640 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
4641 : dpyinfo->x_focus_frame);
4642 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
4644 fset_focus_frame (dpyinfo->x_focus_frame, Qnil);
4645 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
4648 else
4649 dpyinfo->x_highlight_frame = 0;
4651 if (dpyinfo->x_highlight_frame != old_highlight)
4653 if (old_highlight)
4654 frame_unhighlight (old_highlight);
4655 if (dpyinfo->x_highlight_frame)
4656 frame_highlight (dpyinfo->x_highlight_frame);
4662 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
4664 /* Initialize mode_switch_bit and modifier_meaning. */
4665 static void
4666 x_find_modifier_meanings (struct x_display_info *dpyinfo)
4668 int min_code, max_code;
4669 KeySym *syms;
4670 int syms_per_code;
4671 XModifierKeymap *mods;
4673 dpyinfo->meta_mod_mask = 0;
4674 dpyinfo->shift_lock_mask = 0;
4675 dpyinfo->alt_mod_mask = 0;
4676 dpyinfo->super_mod_mask = 0;
4677 dpyinfo->hyper_mod_mask = 0;
4679 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
4681 syms = XGetKeyboardMapping (dpyinfo->display,
4682 min_code, max_code - min_code + 1,
4683 &syms_per_code);
4684 mods = XGetModifierMapping (dpyinfo->display);
4686 /* Scan the modifier table to see which modifier bits the Meta and
4687 Alt keysyms are on. */
4689 int row, col; /* The row and column in the modifier table. */
4690 bool found_alt_or_meta;
4692 for (row = 3; row < 8; row++)
4694 found_alt_or_meta = false;
4695 for (col = 0; col < mods->max_keypermod; col++)
4697 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
4699 /* Zeroes are used for filler. Skip them. */
4700 if (code == 0)
4701 continue;
4703 /* Are any of this keycode's keysyms a meta key? */
4705 int code_col;
4707 for (code_col = 0; code_col < syms_per_code; code_col++)
4709 int sym = syms[((code - min_code) * syms_per_code) + code_col];
4711 switch (sym)
4713 case XK_Meta_L:
4714 case XK_Meta_R:
4715 found_alt_or_meta = true;
4716 dpyinfo->meta_mod_mask |= (1 << row);
4717 break;
4719 case XK_Alt_L:
4720 case XK_Alt_R:
4721 found_alt_or_meta = true;
4722 dpyinfo->alt_mod_mask |= (1 << row);
4723 break;
4725 case XK_Hyper_L:
4726 case XK_Hyper_R:
4727 if (!found_alt_or_meta)
4728 dpyinfo->hyper_mod_mask |= (1 << row);
4729 code_col = syms_per_code;
4730 col = mods->max_keypermod;
4731 break;
4733 case XK_Super_L:
4734 case XK_Super_R:
4735 if (!found_alt_or_meta)
4736 dpyinfo->super_mod_mask |= (1 << row);
4737 code_col = syms_per_code;
4738 col = mods->max_keypermod;
4739 break;
4741 case XK_Shift_Lock:
4742 /* Ignore this if it's not on the lock modifier. */
4743 if (!found_alt_or_meta && ((1 << row) == LockMask))
4744 dpyinfo->shift_lock_mask = LockMask;
4745 code_col = syms_per_code;
4746 col = mods->max_keypermod;
4747 break;
4755 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
4756 if (! dpyinfo->meta_mod_mask)
4758 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
4759 dpyinfo->alt_mod_mask = 0;
4762 /* If some keys are both alt and meta,
4763 make them just meta, not alt. */
4764 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
4766 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
4769 XFree (syms);
4770 XFreeModifiermap (mods);
4773 /* Convert between the modifier bits X uses and the modifier bits
4774 Emacs uses. */
4777 x_x_to_emacs_modifiers (struct x_display_info *dpyinfo, int state)
4779 int mod_ctrl = ctrl_modifier;
4780 int mod_meta = meta_modifier;
4781 int mod_alt = alt_modifier;
4782 int mod_hyper = hyper_modifier;
4783 int mod_super = super_modifier;
4784 Lisp_Object tem;
4786 tem = Fget (Vx_ctrl_keysym, Qmodifier_value);
4787 if (INTEGERP (tem)) mod_ctrl = XINT (tem) & INT_MAX;
4788 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4789 if (INTEGERP (tem)) mod_alt = XINT (tem) & INT_MAX;
4790 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4791 if (INTEGERP (tem)) mod_meta = XINT (tem) & INT_MAX;
4792 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4793 if (INTEGERP (tem)) mod_hyper = XINT (tem) & INT_MAX;
4794 tem = Fget (Vx_super_keysym, Qmodifier_value);
4795 if (INTEGERP (tem)) mod_super = XINT (tem) & INT_MAX;
4797 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
4798 | ((state & ControlMask) ? mod_ctrl : 0)
4799 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
4800 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
4801 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
4802 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
4805 static int
4806 x_emacs_to_x_modifiers (struct x_display_info *dpyinfo, EMACS_INT state)
4808 EMACS_INT mod_ctrl = ctrl_modifier;
4809 EMACS_INT mod_meta = meta_modifier;
4810 EMACS_INT mod_alt = alt_modifier;
4811 EMACS_INT mod_hyper = hyper_modifier;
4812 EMACS_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);
4818 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4819 if (INTEGERP (tem)) mod_alt = XINT (tem);
4820 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4821 if (INTEGERP (tem)) mod_meta = XINT (tem);
4822 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4823 if (INTEGERP (tem)) mod_hyper = XINT (tem);
4824 tem = Fget (Vx_super_keysym, Qmodifier_value);
4825 if (INTEGERP (tem)) mod_super = XINT (tem);
4828 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
4829 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
4830 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
4831 | ((state & shift_modifier) ? ShiftMask : 0)
4832 | ((state & mod_ctrl) ? ControlMask : 0)
4833 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
4836 /* Convert a keysym to its name. */
4838 char *
4839 x_get_keysym_name (int keysym)
4841 char *value;
4843 block_input ();
4844 value = XKeysymToString (keysym);
4845 unblock_input ();
4847 return value;
4850 /* Mouse clicks and mouse movement. Rah.
4852 Formerly, we used PointerMotionHintMask (in standard_event_mask)
4853 so that we would have to call XQueryPointer after each MotionNotify
4854 event to ask for another such event. However, this made mouse tracking
4855 slow, and there was a bug that made it eventually stop.
4857 Simply asking for MotionNotify all the time seems to work better.
4859 In order to avoid asking for motion events and then throwing most
4860 of them away or busy-polling the server for mouse positions, we ask
4861 the server for pointer motion hints. This means that we get only
4862 one event per group of mouse movements. "Groups" are delimited by
4863 other kinds of events (focus changes and button clicks, for
4864 example), or by XQueryPointer calls; when one of these happens, we
4865 get another MotionNotify event the next time the mouse moves. This
4866 is at least as efficient as getting motion events when mouse
4867 tracking is on, and I suspect only negligibly worse when tracking
4868 is off. */
4870 /* Prepare a mouse-event in *RESULT for placement in the input queue.
4872 If the event is a button press, then note that we have grabbed
4873 the mouse. */
4875 static Lisp_Object
4876 construct_mouse_click (struct input_event *result,
4877 const XButtonEvent *event,
4878 struct frame *f)
4880 /* Make the event type NO_EVENT; we'll change that when we decide
4881 otherwise. */
4882 result->kind = MOUSE_CLICK_EVENT;
4883 result->code = event->button - Button1;
4884 result->timestamp = event->time;
4885 result->modifiers = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f),
4886 event->state)
4887 | (event->type == ButtonRelease
4888 ? up_modifier
4889 : down_modifier));
4891 XSETINT (result->x, event->x);
4892 XSETINT (result->y, event->y);
4893 XSETFRAME (result->frame_or_window, f);
4894 result->arg = Qnil;
4895 return Qnil;
4898 /* Function to report a mouse movement to the mainstream Emacs code.
4899 The input handler calls this.
4901 We have received a mouse movement event, which is given in *event.
4902 If the mouse is over a different glyph than it was last time, tell
4903 the mainstream emacs code by setting mouse_moved. If not, ask for
4904 another motion event, so we can check again the next time it moves. */
4906 static bool
4907 note_mouse_movement (struct frame *frame, const XMotionEvent *event)
4909 XRectangle *r;
4910 struct x_display_info *dpyinfo;
4912 if (!FRAME_X_OUTPUT (frame))
4913 return false;
4915 dpyinfo = FRAME_DISPLAY_INFO (frame);
4916 dpyinfo->last_mouse_movement_time = event->time;
4917 dpyinfo->last_mouse_motion_frame = frame;
4918 dpyinfo->last_mouse_motion_x = event->x;
4919 dpyinfo->last_mouse_motion_y = event->y;
4921 if (event->window != FRAME_X_WINDOW (frame))
4923 frame->mouse_moved = true;
4924 dpyinfo->last_mouse_scroll_bar = NULL;
4925 note_mouse_highlight (frame, -1, -1);
4926 dpyinfo->last_mouse_glyph_frame = NULL;
4927 return true;
4931 /* Has the mouse moved off the glyph it was on at the last sighting? */
4932 r = &dpyinfo->last_mouse_glyph;
4933 if (frame != dpyinfo->last_mouse_glyph_frame
4934 || event->x < r->x || event->x >= r->x + r->width
4935 || event->y < r->y || event->y >= r->y + r->height)
4937 frame->mouse_moved = true;
4938 dpyinfo->last_mouse_scroll_bar = NULL;
4939 note_mouse_highlight (frame, event->x, event->y);
4940 /* Remember which glyph we're now on. */
4941 remember_mouse_glyph (frame, event->x, event->y, r);
4942 dpyinfo->last_mouse_glyph_frame = frame;
4943 return true;
4946 return false;
4949 /* Return the current position of the mouse.
4950 *FP should be a frame which indicates which display to ask about.
4952 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
4953 and *PART to the frame, window, and scroll bar part that the mouse
4954 is over. Set *X and *Y to the portion and whole of the mouse's
4955 position on the scroll bar.
4957 If the mouse movement started elsewhere, set *FP to the frame the
4958 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
4959 the mouse is over.
4961 Set *TIMESTAMP to the server time-stamp for the time at which the mouse
4962 was at this position.
4964 Don't store anything if we don't have a valid set of values to report.
4966 This clears the mouse_moved flag, so we can wait for the next mouse
4967 movement. */
4969 static void
4970 XTmouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
4971 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
4972 Time *timestamp)
4974 struct frame *f1;
4975 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
4977 block_input ();
4979 if (dpyinfo->last_mouse_scroll_bar && insist == 0)
4981 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
4983 if (bar->horizontal)
4984 x_horizontal_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
4985 else
4986 x_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
4988 else
4990 Window root;
4991 int root_x, root_y;
4993 Window dummy_window;
4994 int dummy;
4996 Lisp_Object frame, tail;
4998 /* Clear the mouse-moved flag for every frame on this display. */
4999 FOR_EACH_FRAME (tail, frame)
5000 if (FRAME_X_P (XFRAME (frame))
5001 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
5002 XFRAME (frame)->mouse_moved = false;
5004 dpyinfo->last_mouse_scroll_bar = NULL;
5006 /* Figure out which root window we're on. */
5007 XQueryPointer (FRAME_X_DISPLAY (*fp),
5008 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
5010 /* The root window which contains the pointer. */
5011 &root,
5013 /* Trash which we can't trust if the pointer is on
5014 a different screen. */
5015 &dummy_window,
5017 /* The position on that root window. */
5018 &root_x, &root_y,
5020 /* More trash we can't trust. */
5021 &dummy, &dummy,
5023 /* Modifier keys and pointer buttons, about which
5024 we don't care. */
5025 (unsigned int *) &dummy);
5027 /* Now we have a position on the root; find the innermost window
5028 containing the pointer. */
5030 Window win, child;
5031 #ifdef USE_GTK
5032 Window first_win = 0;
5033 #endif
5034 int win_x, win_y;
5035 int parent_x = 0, parent_y = 0;
5037 win = root;
5039 /* XTranslateCoordinates can get errors if the window
5040 structure is changing at the same time this function
5041 is running. So at least we must not crash from them. */
5043 x_catch_errors (FRAME_X_DISPLAY (*fp));
5045 if (x_mouse_grabbed (dpyinfo))
5047 /* If mouse was grabbed on a frame, give coords for that frame
5048 even if the mouse is now outside it. */
5049 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
5051 /* From-window. */
5052 root,
5054 /* To-window. */
5055 FRAME_X_WINDOW (dpyinfo->last_mouse_frame),
5057 /* From-position, to-position. */
5058 root_x, root_y, &win_x, &win_y,
5060 /* Child of win. */
5061 &child);
5062 f1 = dpyinfo->last_mouse_frame;
5064 else
5066 while (true)
5068 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
5070 /* From-window, to-window. */
5071 root, win,
5073 /* From-position, to-position. */
5074 root_x, root_y, &win_x, &win_y,
5076 /* Child of win. */
5077 &child);
5079 if (child == None || child == win)
5081 #ifdef USE_GTK
5082 /* On GTK we have not inspected WIN yet. If it has
5083 a frame and that frame has a parent, use it. */
5084 struct frame *f = x_window_to_frame (dpyinfo, win);
5086 if (f && FRAME_PARENT_FRAME (f))
5087 first_win = win;
5088 #endif
5089 break;
5091 #ifdef USE_GTK
5092 /* We don't wan't to know the innermost window. We
5093 want the edit window. For non-Gtk+ the innermost
5094 window is the edit window. For Gtk+ it might not
5095 be. It might be the tool bar for example. */
5096 if (x_window_to_frame (dpyinfo, win))
5097 /* But don't hurry. We might find a child frame
5098 beneath. */
5099 first_win = win;
5100 #endif
5101 win = child;
5102 parent_x = win_x;
5103 parent_y = win_y;
5106 #ifdef USE_GTK
5107 if (first_win)
5108 win = first_win;
5109 #endif
5111 /* Now we know that:
5112 win is the innermost window containing the pointer
5113 (XTC says it has no child containing the pointer),
5114 win_x and win_y are the pointer's position in it
5115 (XTC did this the last time through), and
5116 parent_x and parent_y are the pointer's position in win's parent.
5117 (They are what win_x and win_y were when win was child.
5118 If win is the root window, it has no parent, and
5119 parent_{x,y} are invalid, but that's okay, because we'll
5120 never use them in that case.) */
5122 #ifdef USE_GTK
5123 /* We don't wan't to know the innermost window. We
5124 want the edit window. */
5125 f1 = x_window_to_frame (dpyinfo, win);
5126 #else
5127 /* Is win one of our frames? */
5128 f1 = x_any_window_to_frame (dpyinfo, win);
5129 #endif
5131 #ifdef USE_X_TOOLKIT
5132 /* If we end up with the menu bar window, say it's not
5133 on the frame. */
5134 if (f1 != NULL
5135 && f1->output_data.x->menubar_widget
5136 && win == XtWindow (f1->output_data.x->menubar_widget))
5137 f1 = NULL;
5138 #endif /* USE_X_TOOLKIT */
5141 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
5142 f1 = 0;
5144 x_uncatch_errors_after_check ();
5146 /* If not, is it one of our scroll bars? */
5147 if (! f1)
5149 struct scroll_bar *bar;
5151 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win, 2);
5153 if (bar)
5155 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5156 win_x = parent_x;
5157 win_y = parent_y;
5161 if (f1 == 0 && insist > 0)
5162 f1 = SELECTED_FRAME ();
5164 if (f1)
5166 /* Ok, we found a frame. Store all the values.
5167 last_mouse_glyph is a rectangle used to reduce the
5168 generation of mouse events. To not miss any motion
5169 events, we must divide the frame into rectangles of the
5170 size of the smallest character that could be displayed
5171 on it, i.e. into the same rectangles that matrices on
5172 the frame are divided into. */
5174 /* FIXME: what if F1 is not an X frame? */
5175 dpyinfo = FRAME_DISPLAY_INFO (f1);
5176 remember_mouse_glyph (f1, win_x, win_y, &dpyinfo->last_mouse_glyph);
5177 dpyinfo->last_mouse_glyph_frame = f1;
5179 *bar_window = Qnil;
5180 *part = 0;
5181 *fp = f1;
5182 XSETINT (*x, win_x);
5183 XSETINT (*y, win_y);
5184 *timestamp = dpyinfo->last_mouse_movement_time;
5189 unblock_input ();
5194 /***********************************************************************
5195 Scroll bars
5196 ***********************************************************************/
5198 /* Scroll bar support. */
5200 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
5201 manages it.
5202 This can be called in GC, so we have to make sure to strip off mark
5203 bits. */
5205 static struct scroll_bar *
5206 x_window_to_scroll_bar (Display *display, Window window_id, int type)
5208 Lisp_Object tail, frame;
5210 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
5211 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
5212 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
5214 FOR_EACH_FRAME (tail, frame)
5216 Lisp_Object bar, condemned;
5218 if (! FRAME_X_P (XFRAME (frame)))
5219 continue;
5221 /* Scan this frame's scroll bar list for a scroll bar with the
5222 right window ID. */
5223 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
5224 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
5225 /* This trick allows us to search both the ordinary and
5226 condemned scroll bar lists with one loop. */
5227 ! NILP (bar) || (bar = condemned,
5228 condemned = Qnil,
5229 ! NILP (bar));
5230 bar = XSCROLL_BAR (bar)->next)
5231 if (XSCROLL_BAR (bar)->x_window == window_id
5232 && FRAME_X_DISPLAY (XFRAME (frame)) == display
5233 && (type = 2
5234 || (type == 1 && XSCROLL_BAR (bar)->horizontal)
5235 || (type == 0 && !XSCROLL_BAR (bar)->horizontal)))
5236 return XSCROLL_BAR (bar);
5239 return NULL;
5243 #if defined USE_LUCID
5245 /* Return the Lucid menu bar WINDOW is part of. Return null
5246 if WINDOW is not part of a menu bar. */
5248 static Widget
5249 x_window_to_menu_bar (Window window)
5251 Lisp_Object tail, frame;
5253 FOR_EACH_FRAME (tail, frame)
5254 if (FRAME_X_P (XFRAME (frame)))
5256 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
5258 if (menu_bar && xlwmenu_window_p (menu_bar, window))
5259 return menu_bar;
5261 return NULL;
5264 #endif /* USE_LUCID */
5267 /************************************************************************
5268 Toolkit scroll bars
5269 ************************************************************************/
5271 #ifdef USE_TOOLKIT_SCROLL_BARS
5273 static void x_send_scroll_bar_event (Lisp_Object, enum scroll_bar_part,
5274 int, int, bool);
5276 /* Lisp window being scrolled. Set when starting to interact with
5277 a toolkit scroll bar, reset to nil when ending the interaction. */
5279 static Lisp_Object window_being_scrolled;
5281 /* Whether this is an Xaw with arrow-scrollbars. This should imply
5282 that movements of 1/20 of the screen size are mapped to up/down. */
5284 #ifndef USE_GTK
5285 /* Id of action hook installed for scroll bars. */
5287 static XtActionHookId action_hook_id;
5288 static XtActionHookId horizontal_action_hook_id;
5290 static Boolean xaw3d_arrow_scroll;
5292 /* Whether the drag scrolling maintains the mouse at the top of the
5293 thumb. If not, resizing the thumb needs to be done more carefully
5294 to avoid jerkiness. */
5296 static Boolean xaw3d_pick_top;
5298 /* Action hook installed via XtAppAddActionHook when toolkit scroll
5299 bars are used.. The hook is responsible for detecting when
5300 the user ends an interaction with the scroll bar, and generates
5301 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
5303 static void
5304 xt_action_hook (Widget widget, XtPointer client_data, String action_name,
5305 XEvent *event, String *params, Cardinal *num_params)
5307 bool scroll_bar_p;
5308 const char *end_action;
5310 #ifdef USE_MOTIF
5311 scroll_bar_p = XmIsScrollBar (widget);
5312 end_action = "Release";
5313 #else /* !USE_MOTIF i.e. use Xaw */
5314 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
5315 end_action = "EndScroll";
5316 #endif /* USE_MOTIF */
5318 if (scroll_bar_p
5319 && strcmp (action_name, end_action) == 0
5320 && WINDOWP (window_being_scrolled))
5322 struct window *w;
5323 struct scroll_bar *bar;
5325 x_send_scroll_bar_event (window_being_scrolled,
5326 scroll_bar_end_scroll, 0, 0, false);
5327 w = XWINDOW (window_being_scrolled);
5328 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5330 if (bar->dragging != -1)
5332 bar->dragging = -1;
5333 /* The thumb size is incorrect while dragging: fix it. */
5334 set_vertical_scroll_bar (w);
5336 window_being_scrolled = Qnil;
5337 #if defined (USE_LUCID)
5338 bar->last_seen_part = scroll_bar_nowhere;
5339 #endif
5340 /* Xt timeouts no longer needed. */
5341 toolkit_scroll_bar_interaction = false;
5346 static void
5347 xt_horizontal_action_hook (Widget widget, XtPointer client_data, String action_name,
5348 XEvent *event, String *params, Cardinal *num_params)
5350 bool scroll_bar_p;
5351 const char *end_action;
5353 #ifdef USE_MOTIF
5354 scroll_bar_p = XmIsScrollBar (widget);
5355 end_action = "Release";
5356 #else /* !USE_MOTIF i.e. use Xaw */
5357 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
5358 end_action = "EndScroll";
5359 #endif /* USE_MOTIF */
5361 if (scroll_bar_p
5362 && strcmp (action_name, end_action) == 0
5363 && WINDOWP (window_being_scrolled))
5365 struct window *w;
5366 struct scroll_bar *bar;
5368 x_send_scroll_bar_event (window_being_scrolled,
5369 scroll_bar_end_scroll, 0, 0, true);
5370 w = XWINDOW (window_being_scrolled);
5371 if (!NILP (w->horizontal_scroll_bar))
5373 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
5374 if (bar->dragging != -1)
5376 bar->dragging = -1;
5377 /* The thumb size is incorrect while dragging: fix it. */
5378 set_horizontal_scroll_bar (w);
5380 window_being_scrolled = Qnil;
5381 #if defined (USE_LUCID)
5382 bar->last_seen_part = scroll_bar_nowhere;
5383 #endif
5384 /* Xt timeouts no longer needed. */
5385 toolkit_scroll_bar_interaction = false;
5389 #endif /* not USE_GTK */
5391 /* Send a client message with message type Xatom_Scrollbar for a
5392 scroll action to the frame of WINDOW. PART is a value identifying
5393 the part of the scroll bar that was clicked on. PORTION is the
5394 amount to scroll of a whole of WHOLE. */
5396 static void
5397 x_send_scroll_bar_event (Lisp_Object window, enum scroll_bar_part part,
5398 int portion, int whole, bool horizontal)
5400 XEvent event;
5401 XClientMessageEvent *ev = &event.xclient;
5402 struct window *w = XWINDOW (window);
5403 struct frame *f = XFRAME (w->frame);
5404 intptr_t iw = (intptr_t) w;
5405 verify (INTPTR_WIDTH <= 64);
5406 int sign_shift = INTPTR_WIDTH - 32;
5408 block_input ();
5410 /* Construct a ClientMessage event to send to the frame. */
5411 ev->type = ClientMessage;
5412 ev->message_type = (horizontal
5413 ? FRAME_DISPLAY_INFO (f)->Xatom_Horizontal_Scrollbar
5414 : FRAME_DISPLAY_INFO (f)->Xatom_Scrollbar);
5415 ev->display = FRAME_X_DISPLAY (f);
5416 ev->window = FRAME_X_WINDOW (f);
5417 ev->format = 32;
5419 /* A 32-bit X client on a 64-bit X server can pass a window pointer
5420 as-is. A 64-bit client on a 32-bit X server is in trouble
5421 because a pointer does not fit and would be truncated while
5422 passing through the server. So use two slots and hope that X12
5423 will resolve such issues someday. */
5424 ev->data.l[0] = iw >> 31 >> 1;
5425 ev->data.l[1] = sign_shift <= 0 ? iw : iw << sign_shift >> sign_shift;
5426 ev->data.l[2] = part;
5427 ev->data.l[3] = portion;
5428 ev->data.l[4] = whole;
5430 /* Make Xt timeouts work while the scroll bar is active. */
5431 #ifdef USE_X_TOOLKIT
5432 toolkit_scroll_bar_interaction = true;
5433 x_activate_timeout_atimer ();
5434 #endif
5436 /* Setting the event mask to zero means that the message will
5437 be sent to the client that created the window, and if that
5438 window no longer exists, no event will be sent. */
5439 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
5440 unblock_input ();
5444 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
5445 in *IEVENT. */
5447 static void
5448 x_scroll_bar_to_input_event (const XEvent *event,
5449 struct input_event *ievent)
5451 const XClientMessageEvent *ev = &event->xclient;
5452 Lisp_Object window;
5453 struct window *w;
5455 /* See the comment in the function above. */
5456 intptr_t iw0 = ev->data.l[0];
5457 intptr_t iw1 = ev->data.l[1];
5458 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
5459 w = (struct window *) iw;
5461 XSETWINDOW (window, w);
5463 ievent->kind = SCROLL_BAR_CLICK_EVENT;
5464 ievent->frame_or_window = window;
5465 ievent->arg = Qnil;
5466 #ifdef USE_GTK
5467 ievent->timestamp = CurrentTime;
5468 #else
5469 ievent->timestamp =
5470 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
5471 #endif
5472 ievent->code = 0;
5473 ievent->part = ev->data.l[2];
5474 ievent->x = make_number (ev->data.l[3]);
5475 ievent->y = make_number (ev->data.l[4]);
5476 ievent->modifiers = 0;
5479 /* Transform a horizontal scroll bar ClientMessage EVENT to an Emacs
5480 input event in *IEVENT. */
5482 static void
5483 x_horizontal_scroll_bar_to_input_event (const XEvent *event,
5484 struct input_event *ievent)
5486 const XClientMessageEvent *ev = &event->xclient;
5487 Lisp_Object window;
5488 struct window *w;
5490 /* See the comment in the function above. */
5491 intptr_t iw0 = ev->data.l[0];
5492 intptr_t iw1 = ev->data.l[1];
5493 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
5494 w = (struct window *) iw;
5496 XSETWINDOW (window, w);
5498 ievent->kind = HORIZONTAL_SCROLL_BAR_CLICK_EVENT;
5499 ievent->frame_or_window = window;
5500 ievent->arg = Qnil;
5501 #ifdef USE_GTK
5502 ievent->timestamp = CurrentTime;
5503 #else
5504 ievent->timestamp =
5505 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
5506 #endif
5507 ievent->code = 0;
5508 ievent->part = ev->data.l[2];
5509 ievent->x = make_number (ev->data.l[3]);
5510 ievent->y = make_number (ev->data.l[4]);
5511 ievent->modifiers = 0;
5515 #ifdef USE_MOTIF
5517 /* Minimum and maximum values used for Motif scroll bars. */
5519 #define XM_SB_MAX 10000000
5521 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
5522 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
5523 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
5525 static void
5526 xm_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5528 struct scroll_bar *bar = client_data;
5529 XmScrollBarCallbackStruct *cs = call_data;
5530 enum scroll_bar_part part = scroll_bar_nowhere;
5531 bool horizontal = bar->horizontal;
5532 int whole = 0, portion = 0;
5534 switch (cs->reason)
5536 case XmCR_DECREMENT:
5537 bar->dragging = -1;
5538 part = horizontal ? scroll_bar_left_arrow : scroll_bar_up_arrow;
5539 break;
5541 case XmCR_INCREMENT:
5542 bar->dragging = -1;
5543 part = horizontal ? scroll_bar_right_arrow : scroll_bar_down_arrow;
5544 break;
5546 case XmCR_PAGE_DECREMENT:
5547 bar->dragging = -1;
5548 part = horizontal ? scroll_bar_before_handle : scroll_bar_above_handle;
5549 break;
5551 case XmCR_PAGE_INCREMENT:
5552 bar->dragging = -1;
5553 part = horizontal ? scroll_bar_after_handle : scroll_bar_below_handle;
5554 break;
5556 case XmCR_TO_TOP:
5557 bar->dragging = -1;
5558 part = horizontal ? scroll_bar_to_leftmost : scroll_bar_to_top;
5559 break;
5561 case XmCR_TO_BOTTOM:
5562 bar->dragging = -1;
5563 part = horizontal ? scroll_bar_to_rightmost : scroll_bar_to_bottom;
5564 break;
5566 case XmCR_DRAG:
5568 int slider_size;
5570 block_input ();
5571 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
5572 unblock_input ();
5574 if (horizontal)
5576 portion = bar->whole * ((float)cs->value / XM_SB_MAX);
5577 whole = bar->whole * ((float)(XM_SB_MAX - slider_size) / XM_SB_MAX);
5578 portion = min (portion, whole);
5579 part = scroll_bar_horizontal_handle;
5581 else
5583 whole = XM_SB_MAX - slider_size;
5584 portion = min (cs->value, whole);
5585 part = scroll_bar_handle;
5588 bar->dragging = cs->value;
5590 break;
5592 case XmCR_VALUE_CHANGED:
5593 break;
5596 if (part != scroll_bar_nowhere)
5598 window_being_scrolled = bar->window;
5599 x_send_scroll_bar_event (bar->window, part, portion, whole,
5600 bar->horizontal);
5604 #elif defined USE_GTK
5606 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
5607 bar widget. DATA is a pointer to the scroll_bar structure. */
5609 static gboolean
5610 xg_scroll_callback (GtkRange *range,
5611 GtkScrollType scroll,
5612 gdouble value,
5613 gpointer user_data)
5615 int whole = 0, portion = 0;
5616 struct scroll_bar *bar = user_data;
5617 enum scroll_bar_part part = scroll_bar_nowhere;
5618 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
5619 struct frame *f = g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
5621 if (xg_ignore_gtk_scrollbar) return false;
5623 switch (scroll)
5625 case GTK_SCROLL_JUMP:
5626 /* Buttons 1 2 or 3 must be grabbed. */
5627 if (FRAME_DISPLAY_INFO (f)->grabbed != 0
5628 && FRAME_DISPLAY_INFO (f)->grabbed < (1 << 4))
5630 if (bar->horizontal)
5632 part = scroll_bar_horizontal_handle;
5633 whole = (int)(gtk_adjustment_get_upper (adj) -
5634 gtk_adjustment_get_page_size (adj));
5635 portion = min ((int)value, whole);
5636 bar->dragging = portion;
5638 else
5640 part = scroll_bar_handle;
5641 whole = gtk_adjustment_get_upper (adj) -
5642 gtk_adjustment_get_page_size (adj);
5643 portion = min ((int)value, whole);
5644 bar->dragging = portion;
5647 break;
5648 case GTK_SCROLL_STEP_BACKWARD:
5649 part = (bar->horizontal
5650 ? scroll_bar_left_arrow : scroll_bar_up_arrow);
5651 bar->dragging = -1;
5652 break;
5653 case GTK_SCROLL_STEP_FORWARD:
5654 part = (bar->horizontal
5655 ? scroll_bar_right_arrow : scroll_bar_down_arrow);
5656 bar->dragging = -1;
5657 break;
5658 case GTK_SCROLL_PAGE_BACKWARD:
5659 part = (bar->horizontal
5660 ? scroll_bar_before_handle : scroll_bar_above_handle);
5661 bar->dragging = -1;
5662 break;
5663 case GTK_SCROLL_PAGE_FORWARD:
5664 part = (bar->horizontal
5665 ? scroll_bar_after_handle : scroll_bar_below_handle);
5666 bar->dragging = -1;
5667 break;
5668 default:
5669 break;
5672 if (part != scroll_bar_nowhere)
5674 window_being_scrolled = bar->window;
5675 x_send_scroll_bar_event (bar->window, part, portion, whole,
5676 bar->horizontal);
5679 return false;
5682 /* Callback for button release. Sets dragging to -1 when dragging is done. */
5684 static gboolean
5685 xg_end_scroll_callback (GtkWidget *widget,
5686 GdkEventButton *event,
5687 gpointer user_data)
5689 struct scroll_bar *bar = user_data;
5690 bar->dragging = -1;
5691 if (WINDOWP (window_being_scrolled))
5693 x_send_scroll_bar_event (window_being_scrolled,
5694 scroll_bar_end_scroll, 0, 0, bar->horizontal);
5695 window_being_scrolled = Qnil;
5698 return false;
5702 #else /* not USE_GTK and not USE_MOTIF */
5704 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
5705 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
5706 scroll bar struct. CALL_DATA is a pointer to a float saying where
5707 the thumb is. */
5709 static void
5710 xaw_jump_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5712 struct scroll_bar *bar = client_data;
5713 float *top_addr = call_data;
5714 float top = *top_addr;
5715 float shown;
5716 int whole, portion, height, width;
5717 enum scroll_bar_part part;
5718 bool horizontal = bar->horizontal;
5721 if (horizontal)
5723 /* Get the size of the thumb, a value between 0 and 1. */
5724 block_input ();
5725 XtVaGetValues (widget, XtNshown, &shown, XtNwidth, &width, NULL);
5726 unblock_input ();
5728 if (shown < 1)
5730 whole = bar->whole - (shown * bar->whole);
5731 portion = min (top * bar->whole, whole);
5733 else
5735 whole = bar->whole;
5736 portion = 0;
5739 part = scroll_bar_horizontal_handle;
5741 else
5743 /* Get the size of the thumb, a value between 0 and 1. */
5744 block_input ();
5745 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
5746 unblock_input ();
5748 whole = 10000000;
5749 portion = shown < 1 ? top * whole : 0;
5751 if (shown < 1 && (eabs (top + shown - 1) < 1.0f / height))
5752 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
5753 the bottom, so we force the scrolling whenever we see that we're
5754 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
5755 we try to ensure that we always stay two pixels away from the
5756 bottom). */
5757 part = scroll_bar_down_arrow;
5758 else
5759 part = scroll_bar_handle;
5762 window_being_scrolled = bar->window;
5763 bar->dragging = portion;
5764 bar->last_seen_part = part;
5765 x_send_scroll_bar_event (bar->window, part, portion, whole, bar->horizontal);
5769 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
5770 i.e. line or page up or down. WIDGET is the Xaw scroll bar
5771 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
5772 the scroll bar. CALL_DATA is an integer specifying the action that
5773 has taken place. Its magnitude is in the range 0..height of the
5774 scroll bar. Negative values mean scroll towards buffer start.
5775 Values < height of scroll bar mean line-wise movement. */
5777 static void
5778 xaw_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5780 struct scroll_bar *bar = client_data;
5781 /* The position really is stored cast to a pointer. */
5782 int position = (intptr_t) call_data;
5783 Dimension height, width;
5784 enum scroll_bar_part part;
5786 if (bar->horizontal)
5788 /* Get the width of the scroll bar. */
5789 block_input ();
5790 XtVaGetValues (widget, XtNwidth, &width, NULL);
5791 unblock_input ();
5793 if (eabs (position) >= width)
5794 part = (position < 0) ? scroll_bar_before_handle : scroll_bar_after_handle;
5796 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
5797 it maps line-movement to call_data = max(5, height/20). */
5798 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, width / 20))
5799 part = (position < 0) ? scroll_bar_left_arrow : scroll_bar_right_arrow;
5800 else
5801 part = scroll_bar_move_ratio;
5803 window_being_scrolled = bar->window;
5804 bar->dragging = -1;
5805 bar->last_seen_part = part;
5806 x_send_scroll_bar_event (bar->window, part, position, width,
5807 bar->horizontal);
5809 else
5812 /* Get the height of the scroll bar. */
5813 block_input ();
5814 XtVaGetValues (widget, XtNheight, &height, NULL);
5815 unblock_input ();
5817 if (eabs (position) >= height)
5818 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
5820 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
5821 it maps line-movement to call_data = max(5, height/20). */
5822 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
5823 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
5824 else
5825 part = scroll_bar_move_ratio;
5827 window_being_scrolled = bar->window;
5828 bar->dragging = -1;
5829 bar->last_seen_part = part;
5830 x_send_scroll_bar_event (bar->window, part, position, height,
5831 bar->horizontal);
5835 #endif /* not USE_GTK and not USE_MOTIF */
5837 #define SCROLL_BAR_NAME "verticalScrollBar"
5838 #define SCROLL_BAR_HORIZONTAL_NAME "horizontalScrollBar"
5840 /* Create the widget for scroll bar BAR on frame F. Record the widget
5841 and X window of the scroll bar in BAR. */
5843 #ifdef USE_GTK
5844 static void
5845 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5847 const char *scroll_bar_name = SCROLL_BAR_NAME;
5849 block_input ();
5850 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
5851 G_CALLBACK (xg_end_scroll_callback),
5852 scroll_bar_name);
5853 unblock_input ();
5856 static void
5857 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5859 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
5861 block_input ();
5862 xg_create_horizontal_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
5863 G_CALLBACK (xg_end_scroll_callback),
5864 scroll_bar_name);
5865 unblock_input ();
5868 #else /* not USE_GTK */
5870 static void
5871 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5873 Window xwindow;
5874 Widget widget;
5875 Arg av[20];
5876 int ac = 0;
5877 const char *scroll_bar_name = SCROLL_BAR_NAME;
5878 unsigned long pixel;
5880 block_input ();
5882 #ifdef USE_MOTIF
5883 /* Set resources. Create the widget. */
5884 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5885 XtSetArg (av[ac], XmNminimum, 0); ++ac;
5886 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
5887 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
5888 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
5889 XtSetArg (av[ac], XmNincrement, 1); ++ac;
5890 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
5892 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5893 if (pixel != -1)
5895 XtSetArg (av[ac], XmNforeground, pixel);
5896 ++ac;
5899 pixel = f->output_data.x->scroll_bar_background_pixel;
5900 if (pixel != -1)
5902 XtSetArg (av[ac], XmNbackground, pixel);
5903 ++ac;
5906 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
5907 (char *) scroll_bar_name, av, ac);
5909 /* Add one callback for everything that can happen. */
5910 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
5911 (XtPointer) bar);
5912 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
5913 (XtPointer) bar);
5914 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
5915 (XtPointer) bar);
5916 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
5917 (XtPointer) bar);
5918 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
5919 (XtPointer) bar);
5920 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
5921 (XtPointer) bar);
5922 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
5923 (XtPointer) bar);
5925 /* Realize the widget. Only after that is the X window created. */
5926 XtRealizeWidget (widget);
5928 /* Set the cursor to an arrow. I didn't find a resource to do that.
5929 And I'm wondering why it hasn't an arrow cursor by default. */
5930 XDefineCursor (XtDisplay (widget), XtWindow (widget),
5931 f->output_data.x->nontext_cursor);
5933 #else /* !USE_MOTIF i.e. use Xaw */
5935 /* Set resources. Create the widget. The background of the
5936 Xaw3d scroll bar widget is a little bit light for my taste.
5937 We don't alter it here to let users change it according
5938 to their taste with `emacs*verticalScrollBar.background: xxx'. */
5939 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5940 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
5941 /* For smoother scrolling with Xaw3d -sm */
5942 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
5944 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5945 if (pixel != -1)
5947 XtSetArg (av[ac], XtNforeground, pixel);
5948 ++ac;
5951 pixel = f->output_data.x->scroll_bar_background_pixel;
5952 if (pixel != -1)
5954 XtSetArg (av[ac], XtNbackground, pixel);
5955 ++ac;
5958 /* Top/bottom shadow colors. */
5960 /* Allocate them, if necessary. */
5961 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
5963 pixel = f->output_data.x->scroll_bar_background_pixel;
5964 if (pixel != -1)
5966 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5967 FRAME_X_COLORMAP (f),
5968 &pixel, 1.2, 0x8000))
5969 pixel = -1;
5970 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
5973 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5975 pixel = f->output_data.x->scroll_bar_background_pixel;
5976 if (pixel != -1)
5978 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5979 FRAME_X_COLORMAP (f),
5980 &pixel, 0.6, 0x4000))
5981 pixel = -1;
5982 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
5986 #ifdef XtNbeNiceToColormap
5987 /* Tell the toolkit about them. */
5988 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
5989 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5990 /* We tried to allocate a color for the top/bottom shadow, and
5991 failed, so tell Xaw3d to use dithering instead. */
5992 /* But only if we have a small colormap. Xaw3d can allocate nice
5993 colors itself. */
5995 XtSetArg (av[ac], XtNbeNiceToColormap,
5996 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
5997 ++ac;
5999 else
6000 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
6001 be more consistent with other emacs 3d colors, and since Xaw3d is
6002 not good at dealing with allocation failure. */
6004 /* This tells Xaw3d to use real colors instead of dithering for
6005 the shadows. */
6006 XtSetArg (av[ac], XtNbeNiceToColormap, False);
6007 ++ac;
6009 /* Specify the colors. */
6010 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
6011 if (pixel != -1)
6013 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
6014 ++ac;
6016 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
6017 if (pixel != -1)
6019 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
6020 ++ac;
6023 #endif
6025 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
6026 f->output_data.x->edit_widget, av, ac);
6029 char const *initial = "";
6030 char const *val = initial;
6031 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
6032 #ifdef XtNarrowScrollbars
6033 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
6034 #endif
6035 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
6036 if (xaw3d_arrow_scroll || val == initial)
6037 { /* ARROW_SCROLL */
6038 xaw3d_arrow_scroll = True;
6039 /* Isn't that just a personal preference ? --Stef */
6040 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
6044 /* Define callbacks. */
6045 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
6046 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
6047 (XtPointer) bar);
6049 /* Realize the widget. Only after that is the X window created. */
6050 XtRealizeWidget (widget);
6052 #endif /* !USE_MOTIF */
6054 /* Install an action hook that lets us detect when the user
6055 finishes interacting with a scroll bar. */
6056 if (action_hook_id == 0)
6057 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
6059 /* Remember X window and widget in the scroll bar vector. */
6060 SET_SCROLL_BAR_X_WIDGET (bar, widget);
6061 xwindow = XtWindow (widget);
6062 bar->x_window = xwindow;
6063 bar->whole = 1;
6064 bar->horizontal = false;
6066 unblock_input ();
6069 static void
6070 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
6072 Window xwindow;
6073 Widget widget;
6074 Arg av[20];
6075 int ac = 0;
6076 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
6077 unsigned long pixel;
6079 block_input ();
6081 #ifdef USE_MOTIF
6082 /* Set resources. Create the widget. */
6083 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
6084 XtSetArg (av[ac], XmNminimum, 0); ++ac;
6085 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
6086 XtSetArg (av[ac], XmNorientation, XmHORIZONTAL); ++ac;
6087 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_RIGHT), ++ac;
6088 XtSetArg (av[ac], XmNincrement, 1); ++ac;
6089 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
6091 pixel = f->output_data.x->scroll_bar_foreground_pixel;
6092 if (pixel != -1)
6094 XtSetArg (av[ac], XmNforeground, pixel);
6095 ++ac;
6098 pixel = f->output_data.x->scroll_bar_background_pixel;
6099 if (pixel != -1)
6101 XtSetArg (av[ac], XmNbackground, pixel);
6102 ++ac;
6105 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
6106 (char *) scroll_bar_name, av, ac);
6108 /* Add one callback for everything that can happen. */
6109 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
6110 (XtPointer) bar);
6111 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
6112 (XtPointer) bar);
6113 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
6114 (XtPointer) bar);
6115 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
6116 (XtPointer) bar);
6117 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
6118 (XtPointer) bar);
6119 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
6120 (XtPointer) bar);
6121 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
6122 (XtPointer) bar);
6124 /* Realize the widget. Only after that is the X window created. */
6125 XtRealizeWidget (widget);
6127 /* Set the cursor to an arrow. I didn't find a resource to do that.
6128 And I'm wondering why it hasn't an arrow cursor by default. */
6129 XDefineCursor (XtDisplay (widget), XtWindow (widget),
6130 f->output_data.x->nontext_cursor);
6132 #else /* !USE_MOTIF i.e. use Xaw */
6134 /* Set resources. Create the widget. The background of the
6135 Xaw3d scroll bar widget is a little bit light for my taste.
6136 We don't alter it here to let users change it according
6137 to their taste with `emacs*verticalScrollBar.background: xxx'. */
6138 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
6139 XtSetArg (av[ac], XtNorientation, XtorientHorizontal); ++ac;
6140 /* For smoother scrolling with Xaw3d -sm */
6141 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
6143 pixel = f->output_data.x->scroll_bar_foreground_pixel;
6144 if (pixel != -1)
6146 XtSetArg (av[ac], XtNforeground, pixel);
6147 ++ac;
6150 pixel = f->output_data.x->scroll_bar_background_pixel;
6151 if (pixel != -1)
6153 XtSetArg (av[ac], XtNbackground, pixel);
6154 ++ac;
6157 /* Top/bottom shadow colors. */
6159 /* Allocate them, if necessary. */
6160 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
6162 pixel = f->output_data.x->scroll_bar_background_pixel;
6163 if (pixel != -1)
6165 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
6166 FRAME_X_COLORMAP (f),
6167 &pixel, 1.2, 0x8000))
6168 pixel = -1;
6169 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
6172 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
6174 pixel = f->output_data.x->scroll_bar_background_pixel;
6175 if (pixel != -1)
6177 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
6178 FRAME_X_COLORMAP (f),
6179 &pixel, 0.6, 0x4000))
6180 pixel = -1;
6181 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
6185 #ifdef XtNbeNiceToColormap
6186 /* Tell the toolkit about them. */
6187 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
6188 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
6189 /* We tried to allocate a color for the top/bottom shadow, and
6190 failed, so tell Xaw3d to use dithering instead. */
6191 /* But only if we have a small colormap. Xaw3d can allocate nice
6192 colors itself. */
6194 XtSetArg (av[ac], XtNbeNiceToColormap,
6195 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
6196 ++ac;
6198 else
6199 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
6200 be more consistent with other emacs 3d colors, and since Xaw3d is
6201 not good at dealing with allocation failure. */
6203 /* This tells Xaw3d to use real colors instead of dithering for
6204 the shadows. */
6205 XtSetArg (av[ac], XtNbeNiceToColormap, False);
6206 ++ac;
6208 /* Specify the colors. */
6209 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
6210 if (pixel != -1)
6212 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
6213 ++ac;
6215 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
6216 if (pixel != -1)
6218 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
6219 ++ac;
6222 #endif
6224 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
6225 f->output_data.x->edit_widget, av, ac);
6228 char const *initial = "";
6229 char const *val = initial;
6230 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
6231 #ifdef XtNarrowScrollbars
6232 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
6233 #endif
6234 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
6235 if (xaw3d_arrow_scroll || val == initial)
6236 { /* ARROW_SCROLL */
6237 xaw3d_arrow_scroll = True;
6238 /* Isn't that just a personal preference ? --Stef */
6239 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
6243 /* Define callbacks. */
6244 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
6245 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
6246 (XtPointer) bar);
6248 /* Realize the widget. Only after that is the X window created. */
6249 XtRealizeWidget (widget);
6251 #endif /* !USE_MOTIF */
6253 /* Install an action hook that lets us detect when the user
6254 finishes interacting with a scroll bar. */
6255 if (horizontal_action_hook_id == 0)
6256 horizontal_action_hook_id
6257 = XtAppAddActionHook (Xt_app_con, xt_horizontal_action_hook, 0);
6259 /* Remember X window and widget in the scroll bar vector. */
6260 SET_SCROLL_BAR_X_WIDGET (bar, widget);
6261 xwindow = XtWindow (widget);
6262 bar->x_window = xwindow;
6263 bar->whole = 1;
6264 bar->horizontal = true;
6266 unblock_input ();
6268 #endif /* not USE_GTK */
6271 /* Set the thumb size and position of scroll bar BAR. We are currently
6272 displaying PORTION out of a whole WHOLE, and our position POSITION. */
6274 #ifdef USE_GTK
6275 static void
6276 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
6278 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
6281 static void
6282 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
6284 xg_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
6287 #else /* not USE_GTK */
6288 static void
6289 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
6290 int whole)
6292 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6293 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6294 float top, shown;
6296 block_input ();
6298 #ifdef USE_MOTIF
6300 if (scroll_bar_adjust_thumb_portion_p)
6302 /* We use an estimate of 30 chars per line rather than the real
6303 `portion' value. This has the disadvantage that the thumb size
6304 is not very representative, but it makes our life a lot easier.
6305 Otherwise, we have to constantly adjust the thumb size, which
6306 we can't always do quickly enough: while dragging, the size of
6307 the thumb might prevent the user from dragging the thumb all the
6308 way to the end. but Motif and some versions of Xaw3d don't allow
6309 updating the thumb size while dragging. Also, even if we can update
6310 its size, the update will often happen too late.
6311 If you don't believe it, check out revision 1.650 of xterm.c to see
6312 what hoops we were going through and the still poor behavior we got. */
6313 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
6314 /* When the thumb is at the bottom, position == whole.
6315 So we need to increase `whole' to make space for the thumb. */
6316 whole += portion;
6319 if (whole <= 0)
6320 top = 0, shown = 1;
6321 else
6323 top = (float) position / whole;
6324 shown = (float) portion / whole;
6327 if (bar->dragging == -1)
6329 int size, value;
6331 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
6332 is the scroll bar's maximum and MIN is the scroll bar's minimum
6333 value. */
6334 size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
6336 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
6337 value = top * XM_SB_MAX;
6338 value = min (value, XM_SB_MAX - size);
6340 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6342 #else /* !USE_MOTIF i.e. use Xaw */
6344 if (whole == 0)
6345 top = 0, shown = 1;
6346 else
6348 top = (float) position / whole;
6349 shown = (float) portion / whole;
6353 float old_top, old_shown;
6354 Dimension height;
6355 XtVaGetValues (widget,
6356 XtNtopOfThumb, &old_top,
6357 XtNshown, &old_shown,
6358 XtNheight, &height,
6359 NULL);
6361 /* Massage the top+shown values. */
6362 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
6363 top = max (0, min (1, top));
6364 else
6365 top = old_top;
6366 #if ! defined (HAVE_XAW3D)
6367 /* With Xaw, 'top' values too closer to 1.0 may
6368 cause the thumb to disappear. Fix that. */
6369 top = min (top, 0.99f);
6370 #endif
6371 /* Keep two pixels available for moving the thumb down. */
6372 shown = max (0, min (1 - top - (2.0f / height), shown));
6373 #if ! defined (HAVE_XAW3D)
6374 /* Likewise with too small 'shown'. */
6375 shown = max (shown, 0.01f);
6376 #endif
6378 /* If the call to XawScrollbarSetThumb below doesn't seem to
6379 work, check that 'NARROWPROTO' is defined in src/config.h.
6380 If this is not so, most likely you need to fix configure. */
6381 if (top != old_top || shown != old_shown)
6383 if (bar->dragging == -1)
6384 XawScrollbarSetThumb (widget, top, shown);
6385 else
6387 /* Try to make the scrolling a tad smoother. */
6388 if (!xaw3d_pick_top)
6389 shown = min (shown, old_shown);
6391 XawScrollbarSetThumb (widget, top, shown);
6395 #endif /* !USE_MOTIF */
6397 unblock_input ();
6400 static void
6401 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
6402 int whole)
6404 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6405 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6406 float top, shown;
6408 block_input ();
6410 #ifdef USE_MOTIF
6411 bar->whole = whole;
6412 shown = (float) portion / whole;
6413 top = (float) position / (whole - portion);
6415 int size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
6416 int value = clip_to_bounds (0, top * (XM_SB_MAX - size), XM_SB_MAX - size);
6418 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6420 #else /* !USE_MOTIF i.e. use Xaw */
6421 bar->whole = whole;
6422 if (whole == 0)
6423 top = 0, shown = 1;
6424 else
6426 top = (float) position / whole;
6427 shown = (float) portion / whole;
6431 float old_top, old_shown;
6432 Dimension height;
6433 XtVaGetValues (widget,
6434 XtNtopOfThumb, &old_top,
6435 XtNshown, &old_shown,
6436 XtNheight, &height,
6437 NULL);
6439 #if false
6440 /* Massage the top+shown values. */
6441 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
6442 top = max (0, min (1, top));
6443 else
6444 top = old_top;
6445 #if ! defined (HAVE_XAW3D)
6446 /* With Xaw, 'top' values too closer to 1.0 may
6447 cause the thumb to disappear. Fix that. */
6448 top = min (top, 0.99f);
6449 #endif
6450 /* Keep two pixels available for moving the thumb down. */
6451 shown = max (0, min (1 - top - (2.0f / height), shown));
6452 #if ! defined (HAVE_XAW3D)
6453 /* Likewise with too small 'shown'. */
6454 shown = max (shown, 0.01f);
6455 #endif
6456 #endif
6458 /* If the call to XawScrollbarSetThumb below doesn't seem to
6459 work, check that 'NARROWPROTO' is defined in src/config.h.
6460 If this is not so, most likely you need to fix configure. */
6461 XawScrollbarSetThumb (widget, top, shown);
6462 #if false
6463 if (top != old_top || shown != old_shown)
6465 if (bar->dragging == -1)
6466 XawScrollbarSetThumb (widget, top, shown);
6467 else
6469 /* Try to make the scrolling a tad smoother. */
6470 if (!xaw3d_pick_top)
6471 shown = min (shown, old_shown);
6473 XawScrollbarSetThumb (widget, top, shown);
6476 #endif
6478 #endif /* !USE_MOTIF */
6480 unblock_input ();
6482 #endif /* not USE_GTK */
6484 #endif /* USE_TOOLKIT_SCROLL_BARS */
6488 /************************************************************************
6489 Scroll bars, general
6490 ************************************************************************/
6492 /* Create a scroll bar and return the scroll bar vector for it. W is
6493 the Emacs window on which to create the scroll bar. TOP, LEFT,
6494 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
6495 scroll bar. */
6497 static struct scroll_bar *
6498 x_scroll_bar_create (struct window *w, int top, int left,
6499 int width, int height, bool horizontal)
6501 struct frame *f = XFRAME (w->frame);
6502 struct scroll_bar *bar
6503 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
6504 Lisp_Object barobj;
6506 block_input ();
6508 #ifdef USE_TOOLKIT_SCROLL_BARS
6509 if (horizontal)
6510 x_create_horizontal_toolkit_scroll_bar (f, bar);
6511 else
6512 x_create_toolkit_scroll_bar (f, bar);
6513 #else /* not USE_TOOLKIT_SCROLL_BARS */
6515 XSetWindowAttributes a;
6516 unsigned long mask;
6517 Window window;
6519 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
6520 if (a.background_pixel == -1)
6521 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
6523 a.event_mask = (ButtonPressMask | ButtonReleaseMask
6524 | ButtonMotionMask | PointerMotionHintMask
6525 | ExposureMask);
6526 a.cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
6528 mask = (CWBackPixel | CWEventMask | CWCursor);
6530 /* Clear the area of W that will serve as a scroll bar. This is
6531 for the case that a window has been split horizontally. In
6532 this case, no clear_frame is generated to reduce flickering. */
6533 if (width > 0 && window_box_height (w) > 0)
6534 x_clear_area (f, left, top, width, window_box_height (w));
6536 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6537 /* Position and size of scroll bar. */
6538 left, top, width, height,
6539 /* Border width, depth, class, and visual. */
6541 CopyFromParent,
6542 CopyFromParent,
6543 CopyFromParent,
6544 /* Attributes. */
6545 mask, &a);
6546 bar->x_window = window;
6548 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6550 XSETWINDOW (bar->window, w);
6551 bar->top = top;
6552 bar->left = left;
6553 bar->width = width;
6554 bar->height = height;
6555 bar->start = 0;
6556 bar->end = 0;
6557 bar->dragging = -1;
6558 bar->horizontal = horizontal;
6559 #if defined (USE_TOOLKIT_SCROLL_BARS) && defined (USE_LUCID)
6560 bar->last_seen_part = scroll_bar_nowhere;
6561 #endif
6563 /* Add bar to its frame's list of scroll bars. */
6564 bar->next = FRAME_SCROLL_BARS (f);
6565 bar->prev = Qnil;
6566 XSETVECTOR (barobj, bar);
6567 fset_scroll_bars (f, barobj);
6568 if (!NILP (bar->next))
6569 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6571 /* Map the window/widget. */
6572 #ifdef USE_TOOLKIT_SCROLL_BARS
6574 #ifdef USE_GTK
6575 if (horizontal)
6576 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top,
6577 left, width, max (height, 1));
6578 else
6579 xg_update_scrollbar_pos (f, bar->x_window, top,
6580 left, width, max (height, 1));
6581 #else /* not USE_GTK */
6582 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6583 XtConfigureWidget (scroll_bar, left, top, width, max (height, 1), 0);
6584 XtMapWidget (scroll_bar);
6585 /* Don't obscure any child frames. */
6586 XLowerWindow (FRAME_X_DISPLAY (f), bar->x_window);
6587 #endif /* not USE_GTK */
6589 #else /* not USE_TOOLKIT_SCROLL_BARS */
6590 XMapWindow (FRAME_X_DISPLAY (f), bar->x_window);
6591 /* Don't obscure any child frames. */
6592 XLowerWindow (FRAME_X_DISPLAY (f), bar->x_window);
6593 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6595 unblock_input ();
6596 return bar;
6600 #ifndef USE_TOOLKIT_SCROLL_BARS
6602 /* Draw BAR's handle in the proper position.
6604 If the handle is already drawn from START to END, don't bother
6605 redrawing it, unless REBUILD; in that case, always
6606 redraw it. (REBUILD is handy for drawing the handle after expose
6607 events.)
6609 Normally, we want to constrain the start and end of the handle to
6610 fit inside its rectangle, but if the user is dragging the scroll
6611 bar handle, we want to let them drag it down all the way, so that
6612 the bar's top is as far down as it goes; otherwise, there's no way
6613 to move to the very end of the buffer. */
6615 static void
6616 x_scroll_bar_set_handle (struct scroll_bar *bar, int start, int end,
6617 bool rebuild)
6619 bool dragging = bar->dragging != -1;
6620 Window w = bar->x_window;
6621 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6622 GC gc = f->output_data.x->normal_gc;
6624 /* If the display is already accurate, do nothing. */
6625 if (! rebuild
6626 && start == bar->start
6627 && end == bar->end)
6628 return;
6630 block_input ();
6633 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
6634 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
6635 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
6637 /* Make sure the values are reasonable, and try to preserve
6638 the distance between start and end. */
6640 int length = end - start;
6642 if (start < 0)
6643 start = 0;
6644 else if (start > top_range)
6645 start = top_range;
6646 end = start + length;
6648 if (end < start)
6649 end = start;
6650 else if (end > top_range && ! dragging)
6651 end = top_range;
6654 /* Store the adjusted setting in the scroll bar. */
6655 bar->start = start;
6656 bar->end = end;
6658 /* Clip the end position, just for display. */
6659 if (end > top_range)
6660 end = top_range;
6662 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
6663 below top positions, to make sure the handle is always at least
6664 that many pixels tall. */
6665 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
6667 /* Draw the empty space above the handle. Note that we can't clear
6668 zero-height areas; that means "clear to end of window." */
6669 if ((inside_width > 0) && (start > 0))
6670 x_clear_area1 (FRAME_X_DISPLAY (f), w,
6671 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6672 VERTICAL_SCROLL_BAR_TOP_BORDER,
6673 inside_width, start, False);
6675 /* Change to proper foreground color if one is specified. */
6676 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6677 XSetForeground (FRAME_X_DISPLAY (f), gc,
6678 f->output_data.x->scroll_bar_foreground_pixel);
6680 /* Draw the handle itself. */
6681 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
6682 /* x, y, width, height */
6683 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6684 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
6685 inside_width, end - start);
6687 /* Restore the foreground color of the GC if we changed it above. */
6688 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6689 XSetForeground (FRAME_X_DISPLAY (f), gc,
6690 FRAME_FOREGROUND_PIXEL (f));
6692 /* Draw the empty space below the handle. Note that we can't
6693 clear zero-height areas; that means "clear to end of window." */
6694 if ((inside_width > 0) && (end < inside_height))
6695 x_clear_area1 (FRAME_X_DISPLAY (f), w,
6696 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6697 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
6698 inside_width, inside_height - end, False);
6701 unblock_input ();
6704 #endif /* !USE_TOOLKIT_SCROLL_BARS */
6706 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
6707 nil. */
6709 static void
6710 x_scroll_bar_remove (struct scroll_bar *bar)
6712 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6713 block_input ();
6715 #ifdef USE_TOOLKIT_SCROLL_BARS
6716 #ifdef USE_GTK
6717 xg_remove_scroll_bar (f, bar->x_window);
6718 #else /* not USE_GTK */
6719 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
6720 #endif /* not USE_GTK */
6721 #else
6722 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
6723 #endif
6725 /* Dissociate this scroll bar from its window. */
6726 if (bar->horizontal)
6727 wset_horizontal_scroll_bar (XWINDOW (bar->window), Qnil);
6728 else
6729 wset_vertical_scroll_bar (XWINDOW (bar->window), Qnil);
6731 unblock_input ();
6735 /* Set the handle of the vertical scroll bar for WINDOW to indicate
6736 that we are displaying PORTION characters out of a total of WHOLE
6737 characters, starting at POSITION. If WINDOW has no scroll bar,
6738 create one. */
6740 static void
6741 XTset_vertical_scroll_bar (struct window *w, int portion, int whole, int position)
6743 struct frame *f = XFRAME (w->frame);
6744 Lisp_Object barobj;
6745 struct scroll_bar *bar;
6746 int top, height, left, width;
6747 int window_y, window_height;
6749 /* Get window dimensions. */
6750 window_box (w, ANY_AREA, 0, &window_y, 0, &window_height);
6751 top = window_y;
6752 height = window_height;
6753 left = WINDOW_SCROLL_BAR_AREA_X (w);
6754 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
6756 /* Does the scroll bar exist yet? */
6757 if (NILP (w->vertical_scroll_bar))
6759 if (width > 0 && height > 0)
6761 block_input ();
6762 x_clear_area (f, left, top, width, height);
6763 unblock_input ();
6766 bar = x_scroll_bar_create (w, top, left, width, max (height, 1), false);
6768 else
6770 /* It may just need to be moved and resized. */
6771 unsigned int mask = 0;
6773 bar = XSCROLL_BAR (w->vertical_scroll_bar);
6775 block_input ();
6777 if (left != bar->left)
6778 mask |= CWX;
6779 if (top != bar->top)
6780 mask |= CWY;
6781 if (width != bar->width)
6782 mask |= CWWidth;
6783 if (height != bar->height)
6784 mask |= CWHeight;
6786 #ifdef USE_TOOLKIT_SCROLL_BARS
6788 /* Move/size the scroll bar widget. */
6789 if (mask)
6791 /* Since toolkit scroll bars are smaller than the space reserved
6792 for them on the frame, we have to clear "under" them. */
6793 if (width > 0 && height > 0)
6794 x_clear_area (f, left, top, width, height);
6795 #ifdef USE_GTK
6796 xg_update_scrollbar_pos (f, bar->x_window, top,
6797 left, width, max (height, 1));
6798 #else /* not USE_GTK */
6799 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
6800 left, top, width, max (height, 1), 0);
6801 #endif /* not USE_GTK */
6803 #else /* not USE_TOOLKIT_SCROLL_BARS */
6805 /* Move/size the scroll bar window. */
6806 if (mask)
6808 XWindowChanges wc;
6810 wc.x = left;
6811 wc.y = top;
6812 wc.width = width;
6813 wc.height = height;
6814 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
6815 mask, &wc);
6818 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6820 /* Remember new settings. */
6821 bar->left = left;
6822 bar->top = top;
6823 bar->width = width;
6824 bar->height = height;
6826 unblock_input ();
6829 #ifdef USE_TOOLKIT_SCROLL_BARS
6830 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
6831 #else /* not USE_TOOLKIT_SCROLL_BARS */
6832 /* Set the scroll bar's current state, unless we're currently being
6833 dragged. */
6834 if (bar->dragging == -1)
6836 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
6838 if (whole == 0)
6839 x_scroll_bar_set_handle (bar, 0, top_range, false);
6840 else
6842 int start = ((double) position * top_range) / whole;
6843 int end = ((double) (position + portion) * top_range) / whole;
6844 x_scroll_bar_set_handle (bar, start, end, false);
6847 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6849 XSETVECTOR (barobj, bar);
6850 wset_vertical_scroll_bar (w, barobj);
6854 static void
6855 XTset_horizontal_scroll_bar (struct window *w, int portion, int whole, int position)
6857 struct frame *f = XFRAME (w->frame);
6858 Lisp_Object barobj;
6859 struct scroll_bar *bar;
6860 int top, height, left, width;
6861 int window_x, window_width;
6862 int pixel_width = WINDOW_PIXEL_WIDTH (w);
6864 /* Get window dimensions. */
6865 window_box (w, ANY_AREA, &window_x, 0, &window_width, 0);
6866 left = window_x;
6867 width = window_width;
6868 top = WINDOW_SCROLL_BAR_AREA_Y (w);
6869 height = WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
6871 /* Does the scroll bar exist yet? */
6872 if (NILP (w->horizontal_scroll_bar))
6874 if (width > 0 && height > 0)
6876 block_input ();
6878 /* Clear also part between window_width and
6879 WINDOW_PIXEL_WIDTH. */
6880 x_clear_area (f, left, top, pixel_width, height);
6881 unblock_input ();
6884 bar = x_scroll_bar_create (w, top, left, width, height, true);
6886 else
6888 /* It may just need to be moved and resized. */
6889 unsigned int mask = 0;
6891 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
6893 block_input ();
6895 if (left != bar->left)
6896 mask |= CWX;
6897 if (top != bar->top)
6898 mask |= CWY;
6899 if (width != bar->width)
6900 mask |= CWWidth;
6901 if (height != bar->height)
6902 mask |= CWHeight;
6904 #ifdef USE_TOOLKIT_SCROLL_BARS
6905 /* Move/size the scroll bar widget. */
6906 if (mask)
6908 /* Since toolkit scroll bars are smaller than the space reserved
6909 for them on the frame, we have to clear "under" them. */
6910 if (width > 0 && height > 0)
6911 x_clear_area (f,
6912 WINDOW_LEFT_EDGE_X (w), top,
6913 pixel_width - WINDOW_RIGHT_DIVIDER_WIDTH (w), height);
6914 #ifdef USE_GTK
6915 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top, left,
6916 width, height);
6917 #else /* not USE_GTK */
6918 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
6919 left, top, width, height, 0);
6920 #endif /* not USE_GTK */
6922 #else /* not USE_TOOLKIT_SCROLL_BARS */
6924 /* Clear areas not covered by the scroll bar because it's not as
6925 wide as the area reserved for it. This makes sure a
6926 previous mode line display is cleared after C-x 2 C-x 1, for
6927 example. */
6929 int area_height = WINDOW_CONFIG_SCROLL_BAR_HEIGHT (w);
6930 int rest = area_height - height;
6931 if (rest > 0 && width > 0)
6932 x_clear_area (f, left, top, width, rest);
6935 /* Move/size the scroll bar window. */
6936 if (mask)
6938 XWindowChanges wc;
6940 wc.x = left;
6941 wc.y = top;
6942 wc.width = width;
6943 wc.height = height;
6944 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
6945 mask, &wc);
6948 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6950 /* Remember new settings. */
6951 bar->left = left;
6952 bar->top = top;
6953 bar->width = width;
6954 bar->height = height;
6956 unblock_input ();
6959 #ifdef USE_TOOLKIT_SCROLL_BARS
6960 x_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
6961 #else /* not USE_TOOLKIT_SCROLL_BARS */
6962 /* Set the scroll bar's current state, unless we're currently being
6963 dragged. */
6964 if (bar->dragging == -1)
6966 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, width);
6968 if (whole == 0)
6969 x_scroll_bar_set_handle (bar, 0, left_range, false);
6970 else
6972 int start = ((double) position * left_range) / whole;
6973 int end = ((double) (position + portion) * left_range) / whole;
6974 x_scroll_bar_set_handle (bar, start, end, false);
6977 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6979 XSETVECTOR (barobj, bar);
6980 wset_horizontal_scroll_bar (w, barobj);
6984 /* The following three hooks are used when we're doing a thorough
6985 redisplay of the frame. We don't explicitly know which scroll bars
6986 are going to be deleted, because keeping track of when windows go
6987 away is a real pain - "Can you say set-window-configuration, boys
6988 and girls?" Instead, we just assert at the beginning of redisplay
6989 that *all* scroll bars are to be removed, and then save a scroll bar
6990 from the fiery pit when we actually redisplay its window. */
6992 /* Arrange for all scroll bars on FRAME to be removed at the next call
6993 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
6994 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
6996 static void
6997 XTcondemn_scroll_bars (struct frame *frame)
6999 if (!NILP (FRAME_SCROLL_BARS (frame)))
7001 if (!NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
7003 /* Prepend scrollbars to already condemned ones. */
7004 Lisp_Object last = FRAME_SCROLL_BARS (frame);
7006 while (!NILP (XSCROLL_BAR (last)->next))
7007 last = XSCROLL_BAR (last)->next;
7009 XSCROLL_BAR (last)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
7010 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = last;
7013 fset_condemned_scroll_bars (frame, FRAME_SCROLL_BARS (frame));
7014 fset_scroll_bars (frame, Qnil);
7019 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
7020 Note that WINDOW isn't necessarily condemned at all. */
7022 static void
7023 XTredeem_scroll_bar (struct window *w)
7025 struct scroll_bar *bar;
7026 Lisp_Object barobj;
7027 struct frame *f;
7029 /* We can't redeem this window's scroll bar if it doesn't have one. */
7030 if (NILP (w->vertical_scroll_bar) && NILP (w->horizontal_scroll_bar))
7031 emacs_abort ();
7033 if (!NILP (w->vertical_scroll_bar) && WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
7035 bar = XSCROLL_BAR (w->vertical_scroll_bar);
7036 /* Unlink it from the condemned list. */
7037 f = XFRAME (WINDOW_FRAME (w));
7038 if (NILP (bar->prev))
7040 /* If the prev pointer is nil, it must be the first in one of
7041 the lists. */
7042 if (EQ (FRAME_SCROLL_BARS (f), w->vertical_scroll_bar))
7043 /* It's not condemned. Everything's fine. */
7044 goto horizontal;
7045 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
7046 w->vertical_scroll_bar))
7047 fset_condemned_scroll_bars (f, bar->next);
7048 else
7049 /* If its prev pointer is nil, it must be at the front of
7050 one or the other! */
7051 emacs_abort ();
7053 else
7054 XSCROLL_BAR (bar->prev)->next = bar->next;
7056 if (! NILP (bar->next))
7057 XSCROLL_BAR (bar->next)->prev = bar->prev;
7059 bar->next = FRAME_SCROLL_BARS (f);
7060 bar->prev = Qnil;
7061 XSETVECTOR (barobj, bar);
7062 fset_scroll_bars (f, barobj);
7063 if (! NILP (bar->next))
7064 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
7067 horizontal:
7068 if (!NILP (w->horizontal_scroll_bar) && WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
7070 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
7071 /* Unlink it from the condemned list. */
7072 f = XFRAME (WINDOW_FRAME (w));
7073 if (NILP (bar->prev))
7075 /* If the prev pointer is nil, it must be the first in one of
7076 the lists. */
7077 if (EQ (FRAME_SCROLL_BARS (f), w->horizontal_scroll_bar))
7078 /* It's not condemned. Everything's fine. */
7079 return;
7080 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
7081 w->horizontal_scroll_bar))
7082 fset_condemned_scroll_bars (f, bar->next);
7083 else
7084 /* If its prev pointer is nil, it must be at the front of
7085 one or the other! */
7086 emacs_abort ();
7088 else
7089 XSCROLL_BAR (bar->prev)->next = bar->next;
7091 if (! NILP (bar->next))
7092 XSCROLL_BAR (bar->next)->prev = bar->prev;
7094 bar->next = FRAME_SCROLL_BARS (f);
7095 bar->prev = Qnil;
7096 XSETVECTOR (barobj, bar);
7097 fset_scroll_bars (f, barobj);
7098 if (! NILP (bar->next))
7099 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
7103 /* Remove all scroll bars on FRAME that haven't been saved since the
7104 last call to `*condemn_scroll_bars_hook'. */
7106 static void
7107 XTjudge_scroll_bars (struct frame *f)
7109 Lisp_Object bar, next;
7111 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
7113 /* Clear out the condemned list now so we won't try to process any
7114 more events on the hapless scroll bars. */
7115 fset_condemned_scroll_bars (f, Qnil);
7117 for (; ! NILP (bar); bar = next)
7119 struct scroll_bar *b = XSCROLL_BAR (bar);
7121 x_scroll_bar_remove (b);
7123 next = b->next;
7124 b->next = b->prev = Qnil;
7127 /* Now there should be no references to the condemned scroll bars,
7128 and they should get garbage-collected. */
7132 #ifndef USE_TOOLKIT_SCROLL_BARS
7133 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
7134 is a no-op when using toolkit scroll bars.
7136 This may be called from a signal handler, so we have to ignore GC
7137 mark bits. */
7139 static void
7140 x_scroll_bar_expose (struct scroll_bar *bar, const XEvent *event)
7142 Window w = bar->x_window;
7143 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7144 GC gc = f->output_data.x->normal_gc;
7146 block_input ();
7148 x_scroll_bar_set_handle (bar, bar->start, bar->end, true);
7150 /* Switch to scroll bar foreground color. */
7151 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
7152 XSetForeground (FRAME_X_DISPLAY (f), gc,
7153 f->output_data.x->scroll_bar_foreground_pixel);
7155 /* Draw a one-pixel border just inside the edges of the scroll bar. */
7156 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
7157 /* x, y, width, height */
7158 0, 0, bar->width - 1, bar->height - 1);
7160 /* Restore the foreground color of the GC if we changed it above. */
7161 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
7162 XSetForeground (FRAME_X_DISPLAY (f), gc,
7163 FRAME_FOREGROUND_PIXEL (f));
7165 unblock_input ();
7168 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7170 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
7171 is set to something other than NO_EVENT, it is enqueued.
7173 This may be called from a signal handler, so we have to ignore GC
7174 mark bits. */
7177 static void
7178 x_scroll_bar_handle_click (struct scroll_bar *bar,
7179 const XEvent *event,
7180 struct input_event *emacs_event)
7182 if (! WINDOWP (bar->window))
7183 emacs_abort ();
7185 emacs_event->kind = (bar->horizontal
7186 ? HORIZONTAL_SCROLL_BAR_CLICK_EVENT
7187 : SCROLL_BAR_CLICK_EVENT);
7188 emacs_event->code = event->xbutton.button - Button1;
7189 emacs_event->modifiers
7190 = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO
7191 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
7192 event->xbutton.state)
7193 | (event->type == ButtonRelease
7194 ? up_modifier
7195 : down_modifier));
7196 emacs_event->frame_or_window = bar->window;
7197 emacs_event->arg = Qnil;
7198 emacs_event->timestamp = event->xbutton.time;
7199 if (bar->horizontal)
7201 int left_range
7202 = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
7203 int x = event->xbutton.x - HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
7205 if (x < 0) x = 0;
7206 if (x > left_range) x = left_range;
7208 if (x < bar->start)
7209 emacs_event->part = scroll_bar_before_handle;
7210 else if (x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
7211 emacs_event->part = scroll_bar_horizontal_handle;
7212 else
7213 emacs_event->part = scroll_bar_after_handle;
7215 #ifndef USE_TOOLKIT_SCROLL_BARS
7216 /* If the user has released the handle, set it to its final position. */
7217 if (event->type == ButtonRelease && bar->dragging != -1)
7219 int new_start = - bar->dragging;
7220 int new_end = new_start + bar->end - bar->start;
7222 x_scroll_bar_set_handle (bar, new_start, new_end, false);
7223 bar->dragging = -1;
7225 #endif
7227 XSETINT (emacs_event->x, left_range);
7228 XSETINT (emacs_event->y, x);
7230 else
7232 int top_range
7233 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
7234 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
7236 if (y < 0) y = 0;
7237 if (y > top_range) y = top_range;
7239 if (y < bar->start)
7240 emacs_event->part = scroll_bar_above_handle;
7241 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
7242 emacs_event->part = scroll_bar_handle;
7243 else
7244 emacs_event->part = scroll_bar_below_handle;
7246 #ifndef USE_TOOLKIT_SCROLL_BARS
7247 /* If the user has released the handle, set it to its final position. */
7248 if (event->type == ButtonRelease && bar->dragging != -1)
7250 int new_start = y - bar->dragging;
7251 int new_end = new_start + bar->end - bar->start;
7253 x_scroll_bar_set_handle (bar, new_start, new_end, false);
7254 bar->dragging = -1;
7256 #endif
7258 XSETINT (emacs_event->x, y);
7259 XSETINT (emacs_event->y, top_range);
7263 #ifndef USE_TOOLKIT_SCROLL_BARS
7265 /* Handle some mouse motion while someone is dragging the scroll bar.
7267 This may be called from a signal handler, so we have to ignore GC
7268 mark bits. */
7270 static void
7271 x_scroll_bar_note_movement (struct scroll_bar *bar,
7272 const XMotionEvent *event)
7274 struct frame *f = XFRAME (XWINDOW (bar->window)->frame);
7275 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
7277 dpyinfo->last_mouse_movement_time = event->time;
7278 dpyinfo->last_mouse_scroll_bar = bar;
7279 f->mouse_moved = true;
7281 /* If we're dragging the bar, display it. */
7282 if (bar->dragging != -1)
7284 /* Where should the handle be now? */
7285 int new_start = event->y - bar->dragging;
7287 if (new_start != bar->start)
7289 int new_end = new_start + bar->end - bar->start;
7291 x_scroll_bar_set_handle (bar, new_start, new_end, false);
7296 #endif /* !USE_TOOLKIT_SCROLL_BARS */
7298 /* Return information to the user about the current position of the mouse
7299 on the scroll bar. */
7301 static void
7302 x_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
7303 enum scroll_bar_part *part, Lisp_Object *x,
7304 Lisp_Object *y, Time *timestamp)
7306 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
7307 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
7308 Window w = bar->x_window;
7309 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7310 int win_x, win_y;
7311 Window dummy_window;
7312 int dummy_coord;
7313 unsigned int dummy_mask;
7315 block_input ();
7317 /* Get the mouse's position relative to the scroll bar window, and
7318 report that. */
7319 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
7321 /* Root, child, root x and root y. */
7322 &dummy_window, &dummy_window,
7323 &dummy_coord, &dummy_coord,
7325 /* Position relative to scroll bar. */
7326 &win_x, &win_y,
7328 /* Mouse buttons and modifier keys. */
7329 &dummy_mask))
7331 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
7333 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
7335 if (bar->dragging != -1)
7336 win_y -= bar->dragging;
7338 if (win_y < 0)
7339 win_y = 0;
7340 if (win_y > top_range)
7341 win_y = top_range;
7343 *fp = f;
7344 *bar_window = bar->window;
7346 if (bar->dragging != -1)
7347 *part = scroll_bar_handle;
7348 else if (win_y < bar->start)
7349 *part = scroll_bar_above_handle;
7350 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
7351 *part = scroll_bar_handle;
7352 else
7353 *part = scroll_bar_below_handle;
7355 XSETINT (*x, win_y);
7356 XSETINT (*y, top_range);
7358 f->mouse_moved = false;
7359 dpyinfo->last_mouse_scroll_bar = NULL;
7360 *timestamp = dpyinfo->last_mouse_movement_time;
7363 unblock_input ();
7367 /* Return information to the user about the current position of the mouse
7368 on the scroll bar. */
7370 static void
7371 x_horizontal_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
7372 enum scroll_bar_part *part, Lisp_Object *x,
7373 Lisp_Object *y, Time *timestamp)
7375 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
7376 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
7377 Window w = bar->x_window;
7378 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7379 int win_x, win_y;
7380 Window dummy_window;
7381 int dummy_coord;
7382 unsigned int dummy_mask;
7384 block_input ();
7386 /* Get the mouse's position relative to the scroll bar window, and
7387 report that. */
7388 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
7390 /* Root, child, root x and root y. */
7391 &dummy_window, &dummy_window,
7392 &dummy_coord, &dummy_coord,
7394 /* Position relative to scroll bar. */
7395 &win_x, &win_y,
7397 /* Mouse buttons and modifier keys. */
7398 &dummy_mask))
7400 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
7402 win_x -= HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
7404 if (bar->dragging != -1)
7405 win_x -= bar->dragging;
7407 if (win_x < 0)
7408 win_x = 0;
7409 if (win_x > left_range)
7410 win_x = left_range;
7412 *fp = f;
7413 *bar_window = bar->window;
7415 if (bar->dragging != -1)
7416 *part = scroll_bar_horizontal_handle;
7417 else if (win_x < bar->start)
7418 *part = scroll_bar_before_handle;
7419 else if (win_x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
7420 *part = scroll_bar_handle;
7421 else
7422 *part = scroll_bar_after_handle;
7424 XSETINT (*y, win_x);
7425 XSETINT (*x, left_range);
7427 f->mouse_moved = false;
7428 dpyinfo->last_mouse_scroll_bar = NULL;
7429 *timestamp = dpyinfo->last_mouse_movement_time;
7432 unblock_input ();
7436 /* The screen has been cleared so we may have changed foreground or
7437 background colors, and the scroll bars may need to be redrawn.
7438 Clear out the scroll bars, and ask for expose events, so we can
7439 redraw them. */
7441 static void
7442 x_scroll_bar_clear (struct frame *f)
7444 #ifndef USE_TOOLKIT_SCROLL_BARS
7445 Lisp_Object bar;
7447 /* We can have scroll bars even if this is 0,
7448 if we just turned off scroll bar mode.
7449 But in that case we should not clear them. */
7450 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
7451 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
7452 bar = XSCROLL_BAR (bar)->next)
7453 XClearArea (FRAME_X_DISPLAY (f),
7454 XSCROLL_BAR (bar)->x_window,
7455 0, 0, 0, 0, True);
7456 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7459 #ifdef ENABLE_CHECKING
7461 /* Record the last 100 characters stored
7462 to help debug the loss-of-chars-during-GC problem. */
7464 static int temp_index;
7465 static short temp_buffer[100];
7467 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
7468 if (temp_index == ARRAYELTS (temp_buffer)) \
7469 temp_index = 0; \
7470 temp_buffer[temp_index++] = (keysym)
7472 #else /* not ENABLE_CHECKING */
7474 #define STORE_KEYSYM_FOR_DEBUG(keysym) ((void)0)
7476 #endif /* ENABLE_CHECKING */
7478 /* Set this to nonzero to fake an "X I/O error"
7479 on a particular display. */
7481 static struct x_display_info *XTread_socket_fake_io_error;
7483 /* When we find no input here, we occasionally do a no-op command
7484 to verify that the X server is still running and we can still talk with it.
7485 We try all the open displays, one by one.
7486 This variable is used for cycling thru the displays. */
7488 static struct x_display_info *next_noop_dpyinfo;
7490 enum
7492 X_EVENT_NORMAL,
7493 X_EVENT_GOTO_OUT,
7494 X_EVENT_DROP
7497 /* Filter events for the current X input method.
7498 DPYINFO is the display this event is for.
7499 EVENT is the X event to filter.
7501 Returns non-zero if the event was filtered, caller shall not process
7502 this event further.
7503 Returns zero if event is wasn't filtered. */
7505 #ifdef HAVE_X_I18N
7506 static int
7507 x_filter_event (struct x_display_info *dpyinfo, XEvent *event)
7509 /* XFilterEvent returns non-zero if the input method has
7510 consumed the event. We pass the frame's X window to
7511 XFilterEvent because that's the one for which the IC
7512 was created. */
7514 struct frame *f1 = x_any_window_to_frame (dpyinfo,
7515 event->xclient.window);
7517 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
7519 #endif
7521 #ifdef USE_GTK
7522 static int current_count;
7523 static int current_finish;
7524 static struct input_event *current_hold_quit;
7526 /* This is the filter function invoked by the GTK event loop.
7527 It is invoked before the XEvent is translated to a GdkEvent,
7528 so we have a chance to act on the event before GTK. */
7529 static GdkFilterReturn
7530 event_handler_gdk (GdkXEvent *gxev, GdkEvent *ev, gpointer data)
7532 XEvent *xev = (XEvent *) gxev;
7534 block_input ();
7535 if (current_count >= 0)
7537 struct x_display_info *dpyinfo;
7539 dpyinfo = x_display_info_for_display (xev->xany.display);
7541 #ifdef HAVE_X_I18N
7542 /* Filter events for the current X input method.
7543 GTK calls XFilterEvent but not for key press and release,
7544 so we do it here. */
7545 if ((xev->type == KeyPress || xev->type == KeyRelease)
7546 && dpyinfo
7547 && x_filter_event (dpyinfo, xev))
7549 unblock_input ();
7550 return GDK_FILTER_REMOVE;
7552 #endif
7554 if (! dpyinfo)
7555 current_finish = X_EVENT_NORMAL;
7556 else
7557 current_count
7558 += handle_one_xevent (dpyinfo, xev, &current_finish,
7559 current_hold_quit);
7561 else
7562 current_finish = x_dispatch_event (xev, xev->xany.display);
7564 unblock_input ();
7566 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
7567 return GDK_FILTER_REMOVE;
7569 return GDK_FILTER_CONTINUE;
7571 #endif /* USE_GTK */
7574 static void xembed_send_message (struct frame *f, Time,
7575 enum xembed_message,
7576 long detail, long data1, long data2);
7578 static void
7579 x_net_wm_state (struct frame *f, Window window)
7581 int value = FULLSCREEN_NONE;
7582 Lisp_Object lval = Qnil;
7583 bool sticky = false;
7585 get_current_wm_state (f, window, &value, &sticky);
7587 switch (value)
7589 case FULLSCREEN_WIDTH:
7590 lval = Qfullwidth;
7591 break;
7592 case FULLSCREEN_HEIGHT:
7593 lval = Qfullheight;
7594 break;
7595 case FULLSCREEN_BOTH:
7596 lval = Qfullboth;
7597 break;
7598 case FULLSCREEN_MAXIMIZED:
7599 lval = Qmaximized;
7600 break;
7603 frame_size_history_add
7604 (f, Qx_net_wm_state, 0, 0,
7605 list2 (get_frame_param (f, Qfullscreen), lval));
7607 store_frame_param (f, Qfullscreen, lval);
7608 /** store_frame_param (f, Qsticky, sticky ? Qt : Qnil); **/
7611 /* Flip back buffers on any frames with undrawn content. */
7612 static void
7613 flush_dirty_back_buffers (void)
7615 block_input ();
7616 Lisp_Object tail, frame;
7617 FOR_EACH_FRAME (tail, frame)
7619 struct frame *f = XFRAME (frame);
7620 if (FRAME_LIVE_P (f) &&
7621 FRAME_X_P (f) &&
7622 FRAME_X_WINDOW (f) &&
7623 !FRAME_GARBAGED_P (f) &&
7624 !buffer_flipping_blocked_p () &&
7625 FRAME_X_NEED_BUFFER_FLIP (f))
7626 show_back_buffer (f);
7628 unblock_input ();
7631 /* Handles the XEvent EVENT on display DPYINFO.
7633 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
7634 *FINISH is zero if caller should continue reading events.
7635 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
7636 *EVENT is unchanged unless we're processing KeyPress event.
7638 We return the number of characters stored into the buffer. */
7640 static int
7641 handle_one_xevent (struct x_display_info *dpyinfo,
7642 const XEvent *event,
7643 int *finish, struct input_event *hold_quit)
7645 union buffered_input_event inev;
7646 int count = 0;
7647 int do_help = 0;
7648 ptrdiff_t nbytes = 0;
7649 struct frame *any, *f = NULL;
7650 struct coding_system coding;
7651 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
7652 /* This holds the state XLookupString needs to implement dead keys
7653 and other tricks known as "compose processing". _X Window System_
7654 says that a portable program can't use this, but Stephen Gildea assures
7655 me that letting the compiler initialize it to zeros will work okay. */
7656 static XComposeStatus compose_status;
7657 XEvent configureEvent;
7658 XEvent next_event;
7660 USE_SAFE_ALLOCA;
7662 *finish = X_EVENT_NORMAL;
7664 EVENT_INIT (inev.ie);
7665 inev.ie.kind = NO_EVENT;
7666 inev.ie.arg = Qnil;
7668 any = x_any_window_to_frame (dpyinfo, event->xany.window);
7670 if (any && any->wait_event_type == event->type)
7671 any->wait_event_type = 0; /* Indicates we got it. */
7673 switch (event->type)
7675 case ClientMessage:
7677 if (event->xclient.message_type == dpyinfo->Xatom_wm_protocols
7678 && event->xclient.format == 32)
7680 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_take_focus)
7682 /* Use the value returned by x_any_window_to_frame
7683 because this could be the shell widget window
7684 if the frame has no title bar. */
7685 f = any;
7686 #ifdef HAVE_X_I18N
7687 /* Not quite sure this is needed -pd */
7688 if (f && FRAME_XIC (f))
7689 XSetICFocus (FRAME_XIC (f));
7690 #endif
7691 #if false
7692 /* Emacs sets WM hints whose `input' field is `true'. This
7693 instructs the WM to set the input focus automatically for
7694 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
7695 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
7696 it has set the focus. So, XSetInputFocus below is not
7697 needed.
7699 The call to XSetInputFocus below has also caused trouble. In
7700 cases where the XSetInputFocus done by the WM and the one
7701 below are temporally close (on a fast machine), the call
7702 below can generate additional FocusIn events which confuse
7703 Emacs. */
7705 /* Since we set WM_TAKE_FOCUS, we must call
7706 XSetInputFocus explicitly. But not if f is null,
7707 since that might be an event for a deleted frame. */
7708 if (f)
7710 Display *d = event->xclient.display;
7711 /* Catch and ignore errors, in case window has been
7712 iconified by a window manager such as GWM. */
7713 x_catch_errors (d);
7714 XSetInputFocus (d, event->xclient.window,
7715 /* The ICCCM says this is
7716 the only valid choice. */
7717 RevertToParent,
7718 event->xclient.data.l[1]);
7719 x_uncatch_errors ();
7721 /* Not certain about handling scroll bars here */
7722 #endif
7723 goto done;
7726 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_save_yourself)
7728 /* Save state modify the WM_COMMAND property to
7729 something which can reinstate us. This notifies
7730 the session manager, who's looking for such a
7731 PropertyNotify. Can restart processing when
7732 a keyboard or mouse event arrives. */
7733 /* If we have a session manager, don't set this.
7734 KDE will then start two Emacsen, one for the
7735 session manager and one for this. */
7736 #ifdef HAVE_X_SM
7737 if (! x_session_have_connection ())
7738 #endif
7740 f = x_top_window_to_frame (dpyinfo,
7741 event->xclient.window);
7742 /* This is just so we only give real data once
7743 for a single Emacs process. */
7744 if (f == SELECTED_FRAME ())
7745 XSetCommand (FRAME_X_DISPLAY (f),
7746 event->xclient.window,
7747 initial_argv, initial_argc);
7748 else if (f)
7749 XSetCommand (FRAME_X_DISPLAY (f),
7750 event->xclient.window,
7751 0, 0);
7753 goto done;
7756 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_delete_window)
7758 f = any;
7759 if (!f)
7760 goto OTHER; /* May be a dialog that is to be removed */
7762 inev.ie.kind = DELETE_WINDOW_EVENT;
7763 XSETFRAME (inev.ie.frame_or_window, f);
7764 goto done;
7767 goto done;
7770 if (event->xclient.message_type == dpyinfo->Xatom_wm_configure_denied)
7771 goto done;
7773 if (event->xclient.message_type == dpyinfo->Xatom_wm_window_moved)
7775 int new_x, new_y;
7776 f = x_window_to_frame (dpyinfo, event->xclient.window);
7778 new_x = event->xclient.data.s[0];
7779 new_y = event->xclient.data.s[1];
7781 if (f)
7783 f->left_pos = new_x;
7784 f->top_pos = new_y;
7786 goto done;
7789 #ifdef X_TOOLKIT_EDITRES
7790 if (event->xclient.message_type == dpyinfo->Xatom_editres)
7792 f = any;
7793 if (f)
7794 _XEditResCheckMessages (f->output_data.x->widget,
7795 NULL, (XEvent *) event, NULL);
7796 goto done;
7798 #endif /* X_TOOLKIT_EDITRES */
7800 if (event->xclient.message_type == dpyinfo->Xatom_DONE
7801 || event->xclient.message_type == dpyinfo->Xatom_PAGE)
7803 /* Ghostview job completed. Kill it. We could
7804 reply with "Next" if we received "Page", but we
7805 currently never do because we are interested in
7806 images, only, which should have 1 page. */
7807 Pixmap pixmap = (Pixmap) event->xclient.data.l[1];
7808 f = x_window_to_frame (dpyinfo, event->xclient.window);
7809 if (!f)
7810 goto OTHER;
7811 x_kill_gs_process (pixmap, f);
7812 expose_frame (f, 0, 0, 0, 0);
7813 goto done;
7816 #ifdef USE_TOOLKIT_SCROLL_BARS
7817 /* Scroll bar callbacks send a ClientMessage from which
7818 we construct an input_event. */
7819 if (event->xclient.message_type == dpyinfo->Xatom_Scrollbar)
7821 x_scroll_bar_to_input_event (event, &inev.ie);
7822 *finish = X_EVENT_GOTO_OUT;
7823 goto done;
7825 else if (event->xclient.message_type == dpyinfo->Xatom_Horizontal_Scrollbar)
7827 x_horizontal_scroll_bar_to_input_event (event, &inev.ie);
7828 *finish = X_EVENT_GOTO_OUT;
7829 goto done;
7831 #endif /* USE_TOOLKIT_SCROLL_BARS */
7833 /* XEmbed messages from the embedder (if any). */
7834 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
7836 enum xembed_message msg = event->xclient.data.l[1];
7837 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
7838 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7840 *finish = X_EVENT_GOTO_OUT;
7841 goto done;
7844 xft_settings_event (dpyinfo, event);
7846 f = any;
7847 if (!f)
7848 goto OTHER;
7849 if (x_handle_dnd_message (f, &event->xclient, dpyinfo, &inev.ie))
7850 *finish = X_EVENT_DROP;
7852 break;
7854 case SelectionNotify:
7855 x_display_set_last_user_time (dpyinfo, event->xselection.time);
7856 #ifdef USE_X_TOOLKIT
7857 if (! x_window_to_frame (dpyinfo, event->xselection.requestor))
7858 goto OTHER;
7859 #endif /* not USE_X_TOOLKIT */
7860 x_handle_selection_notify (&event->xselection);
7861 break;
7863 case SelectionClear: /* Someone has grabbed ownership. */
7864 x_display_set_last_user_time (dpyinfo, event->xselectionclear.time);
7865 #ifdef USE_X_TOOLKIT
7866 if (! x_window_to_frame (dpyinfo, event->xselectionclear.window))
7867 goto OTHER;
7868 #endif /* USE_X_TOOLKIT */
7870 const XSelectionClearEvent *eventp = &event->xselectionclear;
7872 inev.sie.kind = SELECTION_CLEAR_EVENT;
7873 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
7874 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
7875 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
7877 break;
7879 case SelectionRequest: /* Someone wants our selection. */
7880 x_display_set_last_user_time (dpyinfo, event->xselectionrequest.time);
7881 #ifdef USE_X_TOOLKIT
7882 if (!x_window_to_frame (dpyinfo, event->xselectionrequest.owner))
7883 goto OTHER;
7884 #endif /* USE_X_TOOLKIT */
7886 const XSelectionRequestEvent *eventp = &event->xselectionrequest;
7888 inev.sie.kind = SELECTION_REQUEST_EVENT;
7889 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
7890 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
7891 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
7892 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
7893 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
7894 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
7896 break;
7898 case PropertyNotify:
7899 x_display_set_last_user_time (dpyinfo, event->xproperty.time);
7900 f = x_top_window_to_frame (dpyinfo, event->xproperty.window);
7901 if (f && event->xproperty.atom == dpyinfo->Xatom_net_wm_state)
7903 bool not_hidden = x_handle_net_wm_state (f, &event->xproperty);
7904 if (not_hidden && FRAME_ICONIFIED_P (f))
7906 /* Gnome shell does not iconify us when C-z is pressed.
7907 It hides the frame. So if our state says we aren't
7908 hidden anymore, treat it as deiconified. */
7909 SET_FRAME_VISIBLE (f, 1);
7910 SET_FRAME_ICONIFIED (f, false);
7911 f->output_data.x->has_been_visible = true;
7912 inev.ie.kind = DEICONIFY_EVENT;
7913 XSETFRAME (inev.ie.frame_or_window, f);
7915 else if (! not_hidden && ! FRAME_ICONIFIED_P (f))
7917 SET_FRAME_VISIBLE (f, 0);
7918 SET_FRAME_ICONIFIED (f, true);
7919 inev.ie.kind = ICONIFY_EVENT;
7920 XSETFRAME (inev.ie.frame_or_window, f);
7924 x_handle_property_notify (&event->xproperty);
7925 xft_settings_event (dpyinfo, event);
7926 goto OTHER;
7928 case ReparentNotify:
7929 f = x_top_window_to_frame (dpyinfo, event->xreparent.window);
7930 if (f)
7932 /* Maybe we shouldn't set this for child frames ?? */
7933 f->output_data.x->parent_desc = event->xreparent.parent;
7934 if (!FRAME_PARENT_FRAME (f))
7935 x_real_positions (f, &f->left_pos, &f->top_pos);
7936 else
7938 Window root;
7939 unsigned int dummy_uint;
7941 block_input ();
7942 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
7943 &root, &f->left_pos, &f->top_pos,
7944 &dummy_uint, &dummy_uint, &dummy_uint, &dummy_uint);
7945 unblock_input ();
7948 /* Perhaps reparented due to a WM restart. Reset this. */
7949 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
7950 FRAME_DISPLAY_INFO (f)->net_supported_window = 0;
7952 x_set_frame_alpha (f);
7954 goto OTHER;
7956 case Expose:
7957 f = x_window_to_frame (dpyinfo, event->xexpose.window);
7958 if (f)
7960 if (!FRAME_VISIBLE_P (f))
7962 block_input ();
7963 SET_FRAME_VISIBLE (f, 1);
7964 SET_FRAME_ICONIFIED (f, false);
7965 if (FRAME_X_DOUBLE_BUFFERED_P (f))
7966 font_drop_xrender_surfaces (f);
7967 f->output_data.x->has_been_visible = true;
7968 SET_FRAME_GARBAGED (f);
7969 unblock_input ();
7971 else if (FRAME_GARBAGED_P (f))
7973 #ifdef USE_GTK
7974 /* Go around the back buffer and manually clear the
7975 window the first time we show it. This way, we avoid
7976 showing users the sanity-defying horror of whatever
7977 GtkWindow is rendering beneath us. We've garbaged
7978 the frame, so we'll redraw the whole thing on next
7979 redisplay anyway. Yuck. */
7980 x_clear_area1 (
7981 FRAME_X_DISPLAY (f),
7982 FRAME_X_WINDOW (f),
7983 event->xexpose.x, event->xexpose.y,
7984 event->xexpose.width, event->xexpose.height,
7986 x_clear_under_internal_border (f);
7987 #endif
7991 if (!FRAME_GARBAGED_P (f))
7993 #ifdef USE_GTK
7994 /* This seems to be needed for GTK 2.6 and later, see
7995 http://debbugs.gnu.org/cgi/bugreport.cgi?bug=15398. */
7996 x_clear_area (f,
7997 event->xexpose.x, event->xexpose.y,
7998 event->xexpose.width, event->xexpose.height);
7999 #endif
8000 expose_frame (f, event->xexpose.x, event->xexpose.y,
8001 event->xexpose.width, event->xexpose.height);
8002 #ifdef USE_GTK
8003 x_clear_under_internal_border (f);
8004 #endif
8007 if (!FRAME_GARBAGED_P (f))
8008 show_back_buffer (f);
8010 else
8012 #ifndef USE_TOOLKIT_SCROLL_BARS
8013 struct scroll_bar *bar;
8014 #endif
8015 #if defined USE_LUCID
8016 /* Submenus of the Lucid menu bar aren't widgets
8017 themselves, so there's no way to dispatch events
8018 to them. Recognize this case separately. */
8020 Widget widget = x_window_to_menu_bar (event->xexpose.window);
8021 if (widget)
8022 xlwmenu_redisplay (widget);
8024 #endif /* USE_LUCID */
8026 #ifdef USE_TOOLKIT_SCROLL_BARS
8027 /* Dispatch event to the widget. */
8028 goto OTHER;
8029 #else /* not USE_TOOLKIT_SCROLL_BARS */
8030 bar = x_window_to_scroll_bar (event->xexpose.display,
8031 event->xexpose.window, 2);
8033 if (bar)
8034 x_scroll_bar_expose (bar, event);
8035 #ifdef USE_X_TOOLKIT
8036 else
8037 goto OTHER;
8038 #endif /* USE_X_TOOLKIT */
8039 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8041 break;
8043 case GraphicsExpose: /* This occurs when an XCopyArea's
8044 source area was obscured or not
8045 available. */
8046 f = x_window_to_frame (dpyinfo, event->xgraphicsexpose.drawable);
8047 if (f)
8049 expose_frame (f, event->xgraphicsexpose.x,
8050 event->xgraphicsexpose.y,
8051 event->xgraphicsexpose.width,
8052 event->xgraphicsexpose.height);
8053 #ifdef USE_GTK
8054 x_clear_under_internal_border (f);
8055 #endif
8056 show_back_buffer (f);
8058 #ifdef USE_X_TOOLKIT
8059 else
8060 goto OTHER;
8061 #endif /* USE_X_TOOLKIT */
8062 break;
8064 case NoExpose: /* This occurs when an XCopyArea's
8065 source area was completely
8066 available. */
8067 break;
8069 case UnmapNotify:
8070 /* Redo the mouse-highlight after the tooltip has gone. */
8071 if (event->xunmap.window == tip_window)
8073 tip_window = 0;
8074 x_redo_mouse_highlight (dpyinfo);
8077 f = x_top_window_to_frame (dpyinfo, event->xunmap.window);
8078 if (f) /* F may no longer exist if
8079 the frame was deleted. */
8081 bool visible = FRAME_VISIBLE_P (f);
8082 /* While a frame is unmapped, display generation is
8083 disabled; you don't want to spend time updating a
8084 display that won't ever be seen. */
8085 SET_FRAME_VISIBLE (f, 0);
8086 /* We can't distinguish, from the event, whether the window
8087 has become iconified or invisible. So assume, if it
8088 was previously visible, than now it is iconified.
8089 But x_make_frame_invisible clears both
8090 the visible flag and the iconified flag;
8091 and that way, we know the window is not iconified now. */
8092 if (visible || FRAME_ICONIFIED_P (f))
8094 SET_FRAME_ICONIFIED (f, true);
8095 inev.ie.kind = ICONIFY_EVENT;
8096 XSETFRAME (inev.ie.frame_or_window, f);
8099 goto OTHER;
8101 case MapNotify:
8102 /* We use x_top_window_to_frame because map events can
8103 come for sub-windows and they don't mean that the
8104 frame is visible. */
8105 f = x_top_window_to_frame (dpyinfo, event->xmap.window);
8106 if (f)
8108 bool iconified = FRAME_ICONIFIED_P (f);
8110 /* Check if fullscreen was specified before we where mapped the
8111 first time, i.e. from the command line. */
8112 if (!f->output_data.x->has_been_visible)
8115 x_check_fullscreen (f);
8116 #ifndef USE_GTK
8117 /* For systems that cannot synthesize `skip_taskbar' for
8118 unmapped windows do the following. */
8119 if (FRAME_SKIP_TASKBAR (f))
8120 x_set_skip_taskbar (f, Qt, Qnil);
8121 #endif /* Not USE_GTK */
8124 if (!iconified)
8126 /* The `z-group' is reset every time a frame becomes
8127 invisible. Handle this here. */
8128 if (FRAME_Z_GROUP (f) == z_group_above)
8129 x_set_z_group (f, Qabove, Qnil);
8130 else if (FRAME_Z_GROUP (f) == z_group_below)
8131 x_set_z_group (f, Qbelow, Qnil);
8134 SET_FRAME_VISIBLE (f, 1);
8135 SET_FRAME_ICONIFIED (f, false);
8136 f->output_data.x->has_been_visible = true;
8138 if (iconified)
8140 inev.ie.kind = DEICONIFY_EVENT;
8141 XSETFRAME (inev.ie.frame_or_window, f);
8143 else if (! NILP (Vframe_list) && ! NILP (XCDR (Vframe_list)))
8144 /* Force a redisplay sooner or later to update the
8145 frame titles in case this is the second frame. */
8146 record_asynch_buffer_change ();
8148 goto OTHER;
8150 case KeyPress:
8152 x_display_set_last_user_time (dpyinfo, event->xkey.time);
8153 ignore_next_mouse_click_timeout = 0;
8155 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8156 /* Dispatch KeyPress events when in menu. */
8157 if (popup_activated ())
8158 goto OTHER;
8159 #endif
8161 f = any;
8163 /* If mouse-highlight is an integer, input clears out
8164 mouse highlighting. */
8165 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
8166 #if ! defined (USE_GTK)
8167 && (f == 0
8168 || !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
8169 #endif
8172 clear_mouse_face (hlinfo);
8173 hlinfo->mouse_face_hidden = true;
8176 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
8177 if (f == 0)
8179 /* Scroll bars consume key events, but we want
8180 the keys to go to the scroll bar's frame. */
8181 Widget widget = XtWindowToWidget (dpyinfo->display,
8182 event->xkey.window);
8183 if (widget && XmIsScrollBar (widget))
8185 widget = XtParent (widget);
8186 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
8189 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
8191 if (f != 0)
8193 KeySym keysym, orig_keysym;
8194 /* al%imercury@uunet.uu.net says that making this 81
8195 instead of 80 fixed a bug whereby meta chars made
8196 his Emacs hang.
8198 It seems that some version of XmbLookupString has
8199 a bug of not returning XBufferOverflow in
8200 status_return even if the input is too long to
8201 fit in 81 bytes. So, we must prepare sufficient
8202 bytes for copy_buffer. 513 bytes (256 chars for
8203 two-byte character set) seems to be a fairly good
8204 approximation. -- 2000.8.10 handa@etl.go.jp */
8205 unsigned char copy_buffer[513];
8206 unsigned char *copy_bufptr = copy_buffer;
8207 int copy_bufsiz = sizeof (copy_buffer);
8208 int modifiers;
8209 Lisp_Object coding_system = Qlatin_1;
8210 Lisp_Object c;
8211 /* Event will be modified. */
8212 XKeyEvent xkey = event->xkey;
8214 #ifdef USE_GTK
8215 /* Don't pass keys to GTK. A Tab will shift focus to the
8216 tool bar in GTK 2.4. Keys will still go to menus and
8217 dialogs because in that case popup_activated is nonzero
8218 (see above). */
8219 *finish = X_EVENT_DROP;
8220 #endif
8222 xkey.state |= x_emacs_to_x_modifiers (FRAME_DISPLAY_INFO (f),
8223 extra_keyboard_modifiers);
8224 modifiers = xkey.state;
8226 /* This will have to go some day... */
8228 /* make_lispy_event turns chars into control chars.
8229 Don't do it here because XLookupString is too eager. */
8230 xkey.state &= ~ControlMask;
8231 xkey.state &= ~(dpyinfo->meta_mod_mask
8232 | dpyinfo->super_mod_mask
8233 | dpyinfo->hyper_mod_mask
8234 | dpyinfo->alt_mod_mask);
8236 /* In case Meta is ComposeCharacter,
8237 clear its status. According to Markus Ehrnsperger
8238 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
8239 this enables ComposeCharacter to work whether or
8240 not it is combined with Meta. */
8241 if (modifiers & dpyinfo->meta_mod_mask)
8242 memset (&compose_status, 0, sizeof (compose_status));
8244 #ifdef HAVE_X_I18N
8245 if (FRAME_XIC (f))
8247 Status status_return;
8249 coding_system = Vlocale_coding_system;
8250 nbytes = XmbLookupString (FRAME_XIC (f),
8251 &xkey, (char *) copy_bufptr,
8252 copy_bufsiz, &keysym,
8253 &status_return);
8254 if (status_return == XBufferOverflow)
8256 copy_bufsiz = nbytes + 1;
8257 copy_bufptr = alloca (copy_bufsiz);
8258 nbytes = XmbLookupString (FRAME_XIC (f),
8259 &xkey, (char *) copy_bufptr,
8260 copy_bufsiz, &keysym,
8261 &status_return);
8263 /* Xutf8LookupString is a new but already deprecated interface. -stef */
8264 if (status_return == XLookupNone)
8265 break;
8266 else if (status_return == XLookupChars)
8268 keysym = NoSymbol;
8269 modifiers = 0;
8271 else if (status_return != XLookupKeySym
8272 && status_return != XLookupBoth)
8273 emacs_abort ();
8275 else
8276 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
8277 copy_bufsiz, &keysym,
8278 &compose_status);
8279 #else
8280 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
8281 copy_bufsiz, &keysym,
8282 &compose_status);
8283 #endif
8285 /* If not using XIM/XIC, and a compose sequence is in progress,
8286 we break here. Otherwise, chars_matched is always 0. */
8287 if (compose_status.chars_matched > 0 && nbytes == 0)
8288 break;
8290 memset (&compose_status, 0, sizeof (compose_status));
8291 orig_keysym = keysym;
8293 /* Common for all keysym input events. */
8294 XSETFRAME (inev.ie.frame_or_window, f);
8295 inev.ie.modifiers
8296 = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), modifiers);
8297 inev.ie.timestamp = xkey.time;
8299 /* First deal with keysyms which have defined
8300 translations to characters. */
8301 if (keysym >= 32 && keysym < 128)
8302 /* Avoid explicitly decoding each ASCII character. */
8304 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
8305 inev.ie.code = keysym;
8306 goto done_keysym;
8309 /* Keysyms directly mapped to Unicode characters. */
8310 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
8312 if (keysym < 0x01000080)
8313 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
8314 else
8315 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
8316 inev.ie.code = keysym & 0xFFFFFF;
8317 goto done_keysym;
8320 /* Now non-ASCII. */
8321 if (HASH_TABLE_P (Vx_keysym_table)
8322 && (c = Fgethash (make_number (keysym),
8323 Vx_keysym_table,
8324 Qnil),
8325 NATNUMP (c)))
8327 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
8328 ? ASCII_KEYSTROKE_EVENT
8329 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
8330 inev.ie.code = XFASTINT (c);
8331 goto done_keysym;
8334 /* Random non-modifier sorts of keysyms. */
8335 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
8336 || keysym == XK_Delete
8337 #ifdef XK_ISO_Left_Tab
8338 || (keysym >= XK_ISO_Left_Tab
8339 && keysym <= XK_ISO_Enter)
8340 #endif
8341 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
8342 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
8343 #ifdef HPUX
8344 /* This recognizes the "extended function
8345 keys". It seems there's no cleaner way.
8346 Test IsModifierKey to avoid handling
8347 mode_switch incorrectly. */
8348 || (XK_Select <= keysym && keysym < XK_KP_Space)
8349 #endif
8350 #ifdef XK_dead_circumflex
8351 || orig_keysym == XK_dead_circumflex
8352 #endif
8353 #ifdef XK_dead_grave
8354 || orig_keysym == XK_dead_grave
8355 #endif
8356 #ifdef XK_dead_tilde
8357 || orig_keysym == XK_dead_tilde
8358 #endif
8359 #ifdef XK_dead_diaeresis
8360 || orig_keysym == XK_dead_diaeresis
8361 #endif
8362 #ifdef XK_dead_macron
8363 || orig_keysym == XK_dead_macron
8364 #endif
8365 #ifdef XK_dead_degree
8366 || orig_keysym == XK_dead_degree
8367 #endif
8368 #ifdef XK_dead_acute
8369 || orig_keysym == XK_dead_acute
8370 #endif
8371 #ifdef XK_dead_cedilla
8372 || orig_keysym == XK_dead_cedilla
8373 #endif
8374 #ifdef XK_dead_breve
8375 || orig_keysym == XK_dead_breve
8376 #endif
8377 #ifdef XK_dead_ogonek
8378 || orig_keysym == XK_dead_ogonek
8379 #endif
8380 #ifdef XK_dead_caron
8381 || orig_keysym == XK_dead_caron
8382 #endif
8383 #ifdef XK_dead_doubleacute
8384 || orig_keysym == XK_dead_doubleacute
8385 #endif
8386 #ifdef XK_dead_abovedot
8387 || orig_keysym == XK_dead_abovedot
8388 #endif
8389 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
8390 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
8391 /* Any "vendor-specific" key is ok. */
8392 || (orig_keysym & (1 << 28))
8393 || (keysym != NoSymbol && nbytes == 0))
8394 && ! (IsModifierKey (orig_keysym)
8395 /* The symbols from XK_ISO_Lock
8396 to XK_ISO_Last_Group_Lock
8397 don't have real modifiers but
8398 should be treated similarly to
8399 Mode_switch by Emacs. */
8400 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
8401 || (XK_ISO_Lock <= orig_keysym
8402 && orig_keysym <= XK_ISO_Last_Group_Lock)
8403 #endif
8406 STORE_KEYSYM_FOR_DEBUG (keysym);
8407 /* make_lispy_event will convert this to a symbolic
8408 key. */
8409 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
8410 inev.ie.code = keysym;
8411 goto done_keysym;
8414 { /* Raw bytes, not keysym. */
8415 ptrdiff_t i;
8416 int nchars, len;
8418 for (i = 0, nchars = 0; i < nbytes; i++)
8420 if (ASCII_CHAR_P (copy_bufptr[i]))
8421 nchars++;
8422 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
8425 if (nchars < nbytes)
8427 /* Decode the input data. */
8429 /* The input should be decoded with `coding_system'
8430 which depends on which X*LookupString function
8431 we used just above and the locale. */
8432 setup_coding_system (coding_system, &coding);
8433 coding.src_multibyte = false;
8434 coding.dst_multibyte = true;
8435 /* The input is converted to events, thus we can't
8436 handle composition. Anyway, there's no XIM that
8437 gives us composition information. */
8438 coding.common_flags &= ~CODING_ANNOTATION_MASK;
8440 SAFE_NALLOCA (coding.destination, MAX_MULTIBYTE_LENGTH,
8441 nbytes);
8442 coding.dst_bytes = MAX_MULTIBYTE_LENGTH * nbytes;
8443 coding.mode |= CODING_MODE_LAST_BLOCK;
8444 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
8445 nbytes = coding.produced;
8446 nchars = coding.produced_char;
8447 copy_bufptr = coding.destination;
8450 /* Convert the input data to a sequence of
8451 character events. */
8452 for (i = 0; i < nbytes; i += len)
8454 int ch;
8455 if (nchars == nbytes)
8456 ch = copy_bufptr[i], len = 1;
8457 else
8458 ch = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
8459 inev.ie.kind = (SINGLE_BYTE_CHAR_P (ch)
8460 ? ASCII_KEYSTROKE_EVENT
8461 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
8462 inev.ie.code = ch;
8463 kbd_buffer_store_buffered_event (&inev, hold_quit);
8466 count += nchars;
8468 inev.ie.kind = NO_EVENT; /* Already stored above. */
8470 if (keysym == NoSymbol)
8471 break;
8473 /* FIXME: check side effects and remove this. */
8474 ((XEvent *) event)->xkey = xkey;
8476 done_keysym:
8477 #ifdef HAVE_X_I18N
8478 /* Don't dispatch this event since XtDispatchEvent calls
8479 XFilterEvent, and two calls in a row may freeze the
8480 client. */
8481 break;
8482 #else
8483 goto OTHER;
8484 #endif
8486 case KeyRelease:
8487 x_display_set_last_user_time (dpyinfo, event->xkey.time);
8488 #ifdef HAVE_X_I18N
8489 /* Don't dispatch this event since XtDispatchEvent calls
8490 XFilterEvent, and two calls in a row may freeze the
8491 client. */
8492 break;
8493 #else
8494 goto OTHER;
8495 #endif
8497 case EnterNotify:
8498 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
8499 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8501 f = any;
8503 if (f && x_mouse_click_focus_ignore_position)
8504 ignore_next_mouse_click_timeout = event->xmotion.time + 200;
8506 /* EnterNotify counts as mouse movement,
8507 so update things that depend on mouse position. */
8508 if (f && !f->output_data.x->hourglass_p)
8509 note_mouse_movement (f, &event->xmotion);
8510 #ifdef USE_GTK
8511 /* We may get an EnterNotify on the buttons in the toolbar. In that
8512 case we moved out of any highlighted area and need to note this. */
8513 if (!f && dpyinfo->last_mouse_glyph_frame)
8514 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
8515 #endif
8516 goto OTHER;
8518 case FocusIn:
8519 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8520 goto OTHER;
8522 case LeaveNotify:
8523 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
8524 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8526 f = x_top_window_to_frame (dpyinfo, event->xcrossing.window);
8527 if (f)
8529 if (f == hlinfo->mouse_face_mouse_frame)
8531 /* If we move outside the frame, then we're
8532 certainly no longer on any text in the frame. */
8533 clear_mouse_face (hlinfo);
8534 hlinfo->mouse_face_mouse_frame = 0;
8537 /* Generate a nil HELP_EVENT to cancel a help-echo.
8538 Do it only if there's something to cancel.
8539 Otherwise, the startup message is cleared when
8540 the mouse leaves the frame. */
8541 if (any_help_event_p)
8542 do_help = -1;
8544 #ifdef USE_GTK
8545 /* See comment in EnterNotify above */
8546 else if (dpyinfo->last_mouse_glyph_frame)
8547 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
8548 #endif
8549 goto OTHER;
8551 case FocusOut:
8552 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8553 goto OTHER;
8555 case MotionNotify:
8557 x_display_set_last_user_time (dpyinfo, event->xmotion.time);
8558 previous_help_echo_string = help_echo_string;
8559 help_echo_string = Qnil;
8561 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
8562 : x_window_to_frame (dpyinfo, event->xmotion.window));
8564 if (hlinfo->mouse_face_hidden)
8566 hlinfo->mouse_face_hidden = false;
8567 clear_mouse_face (hlinfo);
8570 #ifdef USE_GTK
8571 if (f && xg_event_is_for_scrollbar (f, event))
8572 f = 0;
8573 #endif
8574 if (f)
8576 /* Maybe generate a SELECT_WINDOW_EVENT for
8577 `mouse-autoselect-window' but don't let popup menus
8578 interfere with this (Bug#1261). */
8579 if (!NILP (Vmouse_autoselect_window)
8580 && !popup_activated ()
8581 /* Don't switch if we're currently in the minibuffer.
8582 This tries to work around problems where the
8583 minibuffer gets unselected unexpectedly, and where
8584 you then have to move your mouse all the way down to
8585 the minibuffer to select it. */
8586 && !MINI_WINDOW_P (XWINDOW (selected_window))
8587 /* With `focus-follows-mouse' non-nil create an event
8588 also when the target window is on another frame. */
8589 && (f == XFRAME (selected_frame)
8590 || !NILP (focus_follows_mouse)))
8592 static Lisp_Object last_mouse_window;
8593 Lisp_Object window = window_from_coordinates
8594 (f, event->xmotion.x, event->xmotion.y, 0, false);
8596 /* A window will be autoselected only when it is not
8597 selected now and the last mouse movement event was
8598 not in it. The remainder of the code is a bit vague
8599 wrt what a "window" is. For immediate autoselection,
8600 the window is usually the entire window but for GTK
8601 where the scroll bars don't count. For delayed
8602 autoselection the window is usually the window's text
8603 area including the margins. */
8604 if (WINDOWP (window)
8605 && !EQ (window, last_mouse_window)
8606 && !EQ (window, selected_window))
8608 inev.ie.kind = SELECT_WINDOW_EVENT;
8609 inev.ie.frame_or_window = window;
8612 /* Remember the last window where we saw the mouse. */
8613 last_mouse_window = window;
8616 if (!note_mouse_movement (f, &event->xmotion))
8617 help_echo_string = previous_help_echo_string;
8619 else
8621 #ifndef USE_TOOLKIT_SCROLL_BARS
8622 struct scroll_bar *bar
8623 = x_window_to_scroll_bar (event->xmotion.display,
8624 event->xmotion.window, 2);
8626 if (bar)
8627 x_scroll_bar_note_movement (bar, &event->xmotion);
8628 #endif /* USE_TOOLKIT_SCROLL_BARS */
8630 /* If we move outside the frame, then we're
8631 certainly no longer on any text in the frame. */
8632 clear_mouse_face (hlinfo);
8635 /* If the contents of the global variable help_echo_string
8636 has changed, generate a HELP_EVENT. */
8637 if (!NILP (help_echo_string)
8638 || !NILP (previous_help_echo_string))
8639 do_help = 1;
8640 goto OTHER;
8643 case ConfigureNotify:
8644 /* An opaque move can generate a stream of events as the window
8645 is dragged around. If the connection round trip time isn't
8646 really short, they may come faster than we can respond to
8647 them, given the multiple queries we can do to check window
8648 manager state, translate coordinates, etc.
8650 So if this ConfigureNotify is immediately followed by another
8651 for the same window, use the info from the latest update, and
8652 consider the events all handled. */
8653 /* Opaque resize may be trickier; ConfigureNotify events are
8654 mixed with Expose events for multiple windows. */
8655 configureEvent = *event;
8656 while (XPending (dpyinfo->display))
8658 XNextEvent (dpyinfo->display, &next_event);
8659 if (next_event.type != ConfigureNotify
8660 || next_event.xconfigure.window != event->xconfigure.window
8661 /* Skipping events with different sizes can lead to a
8662 mispositioned mode line at initial window creation.
8663 Only drop window motion events for now. */
8664 || next_event.xconfigure.width != event->xconfigure.width
8665 || next_event.xconfigure.height != event->xconfigure.height)
8667 XPutBackEvent (dpyinfo->display, &next_event);
8668 break;
8670 else
8671 configureEvent = next_event;
8674 f = x_top_window_to_frame (dpyinfo, configureEvent.xconfigure.window);
8675 /* Unfortunately, we need to call font_drop_xrender_surfaces for
8676 _all_ ConfigureNotify events, otherwise we miss some and
8677 flicker. Don't try to optimize these calls by looking only
8678 for size changes: that's not sufficient. We miss some
8679 surface invalidations and flicker. */
8680 block_input ();
8681 if (f && FRAME_X_DOUBLE_BUFFERED_P (f))
8682 font_drop_xrender_surfaces (f);
8683 unblock_input ();
8684 #ifdef USE_CAIRO
8685 if (f) x_cr_destroy_surface (f);
8686 #endif
8687 #ifdef USE_GTK
8688 if (!f
8689 && (f = any)
8690 && configureEvent.xconfigure.window == FRAME_X_WINDOW (f))
8692 block_input ();
8693 if (FRAME_X_DOUBLE_BUFFERED_P (f))
8694 font_drop_xrender_surfaces (f);
8695 unblock_input ();
8696 xg_frame_resized (f, configureEvent.xconfigure.width,
8697 configureEvent.xconfigure.height);
8698 #ifdef USE_CAIRO
8699 x_cr_destroy_surface (f);
8700 #endif
8701 f = 0;
8703 #endif
8704 if (f)
8706 /* Don't call x_net_wm_state for the scroll bar window.
8707 (Bug#24963, Bug#25887) */
8708 if (configureEvent.xconfigure.window == FRAME_X_WINDOW (f))
8709 x_net_wm_state (f, configureEvent.xconfigure.window);
8711 #ifdef USE_X_TOOLKIT
8712 /* Tip frames are pure X window, set size for them. */
8713 if (! NILP (tip_frame) && XFRAME (tip_frame) == f)
8715 if (FRAME_PIXEL_HEIGHT (f) != configureEvent.xconfigure.height
8716 || FRAME_PIXEL_WIDTH (f) != configureEvent.xconfigure.width)
8718 SET_FRAME_GARBAGED (f);
8720 FRAME_PIXEL_HEIGHT (f) = configureEvent.xconfigure.height;
8721 FRAME_PIXEL_WIDTH (f) = configureEvent.xconfigure.width;
8723 #endif
8725 #ifndef USE_X_TOOLKIT
8726 #ifndef USE_GTK
8727 int width =
8728 FRAME_PIXEL_TO_TEXT_WIDTH (f, configureEvent.xconfigure.width);
8729 int height =
8730 FRAME_PIXEL_TO_TEXT_HEIGHT (f, configureEvent.xconfigure.height);
8732 /* In the toolkit version, change_frame_size
8733 is called by the code that handles resizing
8734 of the EmacsFrame widget. */
8736 /* Even if the number of character rows and columns has
8737 not changed, the font size may have changed, so we need
8738 to check the pixel dimensions as well. */
8739 if (width != FRAME_TEXT_WIDTH (f)
8740 || height != FRAME_TEXT_HEIGHT (f)
8741 || configureEvent.xconfigure.width != FRAME_PIXEL_WIDTH (f)
8742 || configureEvent.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
8744 change_frame_size (f, width, height, false, true, false, true);
8745 x_clear_under_internal_border (f);
8746 SET_FRAME_GARBAGED (f);
8747 cancel_mouse_face (f);
8749 #endif /* not USE_GTK */
8750 #endif
8752 #ifdef USE_GTK
8753 /* GTK creates windows but doesn't map them.
8754 Only get real positions when mapped. */
8755 if (FRAME_GTK_OUTER_WIDGET (f)
8756 && gtk_widget_get_mapped (FRAME_GTK_OUTER_WIDGET (f)))
8757 #endif
8759 int old_left = f->left_pos;
8760 int old_top = f->top_pos;
8761 Lisp_Object frame = Qnil;
8763 XSETFRAME (frame, f);
8765 if (!FRAME_PARENT_FRAME (f))
8766 x_real_positions (f, &f->left_pos, &f->top_pos);
8767 else
8769 Window root;
8770 unsigned int dummy_uint;
8772 block_input ();
8773 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8774 &root, &f->left_pos, &f->top_pos,
8775 &dummy_uint, &dummy_uint, &dummy_uint, &dummy_uint);
8776 unblock_input ();
8779 if (old_left != f->left_pos || old_top != f->top_pos)
8781 inev.ie.kind = MOVE_FRAME_EVENT;
8782 XSETFRAME (inev.ie.frame_or_window, f);
8787 #ifdef HAVE_X_I18N
8788 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
8789 xic_set_statusarea (f);
8790 #endif
8793 goto OTHER;
8795 case ButtonRelease:
8796 case ButtonPress:
8798 /* If we decide we want to generate an event to be seen
8799 by the rest of Emacs, we put it here. */
8800 bool tool_bar_p = false;
8802 memset (&compose_status, 0, sizeof (compose_status));
8803 dpyinfo->last_mouse_glyph_frame = NULL;
8804 x_display_set_last_user_time (dpyinfo, event->xbutton.time);
8806 if (x_mouse_grabbed (dpyinfo))
8807 f = dpyinfo->last_mouse_frame;
8808 else
8810 f = x_window_to_frame (dpyinfo, event->xbutton.window);
8812 if (f && event->xbutton.type == ButtonPress
8813 && !popup_activated ()
8814 && !x_window_to_scroll_bar (event->xbutton.display,
8815 event->xbutton.window, 2)
8816 && !FRAME_NO_ACCEPT_FOCUS (f))
8818 /* When clicking into a child frame or when clicking
8819 into a parent frame with the child frame selected and
8820 `no-accept-focus' is not set, select the clicked
8821 frame. */
8822 struct frame *hf = dpyinfo->x_highlight_frame;
8824 if (FRAME_PARENT_FRAME (f) || (hf && frame_ancestor_p (f, hf)))
8826 block_input ();
8827 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8828 RevertToParent, CurrentTime);
8829 if (FRAME_PARENT_FRAME (f))
8830 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8831 unblock_input ();
8836 #ifdef USE_GTK
8837 if (f && xg_event_is_for_scrollbar (f, event))
8838 f = 0;
8839 #endif
8840 if (f)
8842 #if ! defined (USE_GTK)
8843 /* Is this in the tool-bar? */
8844 if (WINDOWP (f->tool_bar_window)
8845 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
8847 Lisp_Object window;
8848 int x = event->xbutton.x;
8849 int y = event->xbutton.y;
8851 window = window_from_coordinates (f, x, y, 0, true);
8852 tool_bar_p = EQ (window, f->tool_bar_window);
8854 if (tool_bar_p && event->xbutton.button < 4)
8855 handle_tool_bar_click
8856 (f, x, y, event->xbutton.type == ButtonPress,
8857 x_x_to_emacs_modifiers (dpyinfo, event->xbutton.state));
8859 #endif /* !USE_GTK */
8861 if (!tool_bar_p)
8862 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8863 if (! popup_activated ())
8864 #endif
8866 if (ignore_next_mouse_click_timeout)
8868 if (event->type == ButtonPress
8869 && event->xbutton.time > ignore_next_mouse_click_timeout)
8871 ignore_next_mouse_click_timeout = 0;
8872 construct_mouse_click (&inev.ie, &event->xbutton, f);
8874 if (event->type == ButtonRelease)
8875 ignore_next_mouse_click_timeout = 0;
8877 else
8878 construct_mouse_click (&inev.ie, &event->xbutton, f);
8880 if (FRAME_X_EMBEDDED_P (f))
8881 xembed_send_message (f, event->xbutton.time,
8882 XEMBED_REQUEST_FOCUS, 0, 0, 0);
8884 else
8886 struct scroll_bar *bar
8887 = x_window_to_scroll_bar (event->xbutton.display,
8888 event->xbutton.window, 2);
8890 #ifdef USE_TOOLKIT_SCROLL_BARS
8891 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
8892 scroll bars. */
8893 if (bar && event->xbutton.state & ControlMask)
8895 x_scroll_bar_handle_click (bar, event, &inev.ie);
8896 *finish = X_EVENT_DROP;
8898 #else /* not USE_TOOLKIT_SCROLL_BARS */
8899 if (bar)
8900 x_scroll_bar_handle_click (bar, event, &inev.ie);
8901 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8904 if (event->type == ButtonPress)
8906 dpyinfo->grabbed |= (1 << event->xbutton.button);
8907 dpyinfo->last_mouse_frame = f;
8908 #if ! defined (USE_GTK)
8909 if (f && !tool_bar_p)
8910 f->last_tool_bar_item = -1;
8911 #endif /* not USE_GTK */
8913 else
8914 dpyinfo->grabbed &= ~(1 << event->xbutton.button);
8916 /* Ignore any mouse motion that happened before this event;
8917 any subsequent mouse-movement Emacs events should reflect
8918 only motion after the ButtonPress/Release. */
8919 if (f != 0)
8920 f->mouse_moved = false;
8922 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8923 f = x_menubar_window_to_frame (dpyinfo, event);
8924 /* For a down-event in the menu bar,
8925 don't pass it to Xt right now.
8926 Instead, save it away
8927 and we will pass it to Xt from kbd_buffer_get_event.
8928 That way, we can run some Lisp code first. */
8929 if (! popup_activated ()
8930 #ifdef USE_GTK
8931 /* Gtk+ menus only react to the first three buttons. */
8932 && event->xbutton.button < 3
8933 #endif
8934 && f && event->type == ButtonPress
8935 /* Verify the event is really within the menu bar
8936 and not just sent to it due to grabbing. */
8937 && event->xbutton.x >= 0
8938 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
8939 && event->xbutton.y >= 0
8940 && event->xbutton.y < FRAME_MENUBAR_HEIGHT (f)
8941 && event->xbutton.same_screen)
8943 if (!f->output_data.x->saved_menu_event)
8944 f->output_data.x->saved_menu_event = xmalloc (sizeof *event);
8945 *f->output_data.x->saved_menu_event = *event;
8946 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT;
8947 XSETFRAME (inev.ie.frame_or_window, f);
8948 *finish = X_EVENT_DROP;
8950 else
8951 goto OTHER;
8952 #endif /* USE_X_TOOLKIT || USE_GTK */
8954 break;
8956 case CirculateNotify:
8957 goto OTHER;
8959 case CirculateRequest:
8960 goto OTHER;
8962 case VisibilityNotify:
8963 goto OTHER;
8965 case MappingNotify:
8966 /* Someone has changed the keyboard mapping - update the
8967 local cache. */
8968 switch (event->xmapping.request)
8970 case MappingModifier:
8971 x_find_modifier_meanings (dpyinfo);
8972 FALLTHROUGH;
8973 case MappingKeyboard:
8974 XRefreshKeyboardMapping ((XMappingEvent *) &event->xmapping);
8976 goto OTHER;
8978 case DestroyNotify:
8979 xft_settings_event (dpyinfo, event);
8980 break;
8982 default:
8983 OTHER:
8984 #ifdef USE_X_TOOLKIT
8985 block_input ();
8986 if (*finish != X_EVENT_DROP)
8987 XtDispatchEvent ((XEvent *) event);
8988 unblock_input ();
8989 #endif /* USE_X_TOOLKIT */
8990 break;
8993 done:
8994 if (inev.ie.kind != NO_EVENT)
8996 kbd_buffer_store_buffered_event (&inev, hold_quit);
8997 count++;
9000 if (do_help
9001 && !(hold_quit && hold_quit->kind != NO_EVENT))
9003 Lisp_Object frame;
9005 if (f)
9006 XSETFRAME (frame, f);
9007 else
9008 frame = Qnil;
9010 if (do_help > 0)
9012 any_help_event_p = true;
9013 gen_help_event (help_echo_string, frame, help_echo_window,
9014 help_echo_object, help_echo_pos);
9016 else
9018 help_echo_string = Qnil;
9019 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
9021 count++;
9024 /* Sometimes event processing draws to the frame outside redisplay.
9025 To ensure that these changes become visible, draw them here. */
9026 flush_dirty_back_buffers ();
9027 SAFE_FREE ();
9028 return count;
9031 /* Handles the XEvent EVENT on display DISPLAY.
9032 This is used for event loops outside the normal event handling,
9033 i.e. looping while a popup menu or a dialog is posted.
9035 Returns the value handle_one_xevent sets in the finish argument. */
9037 x_dispatch_event (XEvent *event, Display *display)
9039 struct x_display_info *dpyinfo;
9040 int finish = X_EVENT_NORMAL;
9042 dpyinfo = x_display_info_for_display (display);
9044 if (dpyinfo)
9045 handle_one_xevent (dpyinfo, event, &finish, 0);
9047 return finish;
9050 /* Read events coming from the X server.
9051 Return as soon as there are no more events to be read.
9053 Return the number of characters stored into the buffer,
9054 thus pretending to be `read' (except the characters we store
9055 in the keyboard buffer can be multibyte, so are not necessarily
9056 C chars). */
9058 static int
9059 XTread_socket (struct terminal *terminal, struct input_event *hold_quit)
9061 int count = 0;
9062 bool event_found = false;
9063 struct x_display_info *dpyinfo = terminal->display_info.x;
9065 block_input ();
9067 /* For debugging, this gives a way to fake an I/O error. */
9068 if (dpyinfo == XTread_socket_fake_io_error)
9070 XTread_socket_fake_io_error = 0;
9071 x_io_error_quitter (dpyinfo->display);
9074 #ifndef USE_GTK
9075 while (XPending (dpyinfo->display))
9077 int finish;
9078 XEvent event;
9080 XNextEvent (dpyinfo->display, &event);
9082 #ifdef HAVE_X_I18N
9083 /* Filter events for the current X input method. */
9084 if (x_filter_event (dpyinfo, &event))
9085 continue;
9086 #endif
9087 event_found = true;
9089 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
9091 if (finish == X_EVENT_GOTO_OUT)
9092 break;
9095 #else /* USE_GTK */
9097 /* For GTK we must use the GTK event loop. But XEvents gets passed
9098 to our filter function above, and then to the big event switch.
9099 We use a bunch of globals to communicate with our filter function,
9100 that is kind of ugly, but it works.
9102 There is no way to do one display at the time, GTK just does events
9103 from all displays. */
9105 while (gtk_events_pending ())
9107 current_count = count;
9108 current_hold_quit = hold_quit;
9110 gtk_main_iteration ();
9112 count = current_count;
9113 current_count = -1;
9114 current_hold_quit = 0;
9116 if (current_finish == X_EVENT_GOTO_OUT)
9117 break;
9119 #endif /* USE_GTK */
9121 /* On some systems, an X bug causes Emacs to get no more events
9122 when the window is destroyed. Detect that. (1994.) */
9123 if (! event_found)
9125 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
9126 One XNOOP in 100 loops will make Emacs terminate.
9127 B. Bretthauer, 1994 */
9128 x_noop_count++;
9129 if (x_noop_count >= 100)
9131 x_noop_count=0;
9133 if (next_noop_dpyinfo == 0)
9134 next_noop_dpyinfo = x_display_list;
9136 XNoOp (next_noop_dpyinfo->display);
9138 /* Each time we get here, cycle through the displays now open. */
9139 next_noop_dpyinfo = next_noop_dpyinfo->next;
9143 /* If the focus was just given to an auto-raising frame,
9144 raise it now. FIXME: handle more than one such frame. */
9145 if (dpyinfo->x_pending_autoraise_frame)
9147 x_raise_frame (dpyinfo->x_pending_autoraise_frame);
9148 dpyinfo->x_pending_autoraise_frame = NULL;
9151 unblock_input ();
9153 return count;
9159 /***********************************************************************
9160 Text Cursor
9161 ***********************************************************************/
9163 /* Set clipping for output in glyph row ROW. W is the window in which
9164 we operate. GC is the graphics context to set clipping in.
9166 ROW may be a text row or, e.g., a mode line. Text rows must be
9167 clipped to the interior of the window dedicated to text display,
9168 mode lines must be clipped to the whole window. */
9170 static void
9171 x_clip_to_row (struct window *w, struct glyph_row *row,
9172 enum glyph_row_area area, GC gc)
9174 struct frame *f = XFRAME (WINDOW_FRAME (w));
9175 XRectangle clip_rect;
9176 int window_x, window_y, window_width;
9178 window_box (w, area, &window_x, &window_y, &window_width, 0);
9180 clip_rect.x = window_x;
9181 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
9182 clip_rect.y = max (clip_rect.y, window_y);
9183 clip_rect.width = window_width;
9184 clip_rect.height = row->visible_height;
9186 x_set_clip_rectangles (f, gc, &clip_rect, 1);
9190 /* Draw a hollow box cursor on window W in glyph row ROW. */
9192 static void
9193 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
9195 struct frame *f = XFRAME (WINDOW_FRAME (w));
9196 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9197 Display *dpy = FRAME_X_DISPLAY (f);
9198 int x, y, wd, h;
9199 XGCValues xgcv;
9200 struct glyph *cursor_glyph;
9201 GC gc;
9203 /* Get the glyph the cursor is on. If we can't tell because
9204 the current matrix is invalid or such, give up. */
9205 cursor_glyph = get_phys_cursor_glyph (w);
9206 if (cursor_glyph == NULL)
9207 return;
9209 /* Compute frame-relative coordinates for phys cursor. */
9210 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
9211 wd = w->phys_cursor_width - 1;
9213 /* The foreground of cursor_gc is typically the same as the normal
9214 background color, which can cause the cursor box to be invisible. */
9215 xgcv.foreground = f->output_data.x->cursor_pixel;
9216 if (dpyinfo->scratch_cursor_gc)
9217 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
9218 else
9219 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_DRAWABLE (f),
9220 GCForeground, &xgcv);
9221 gc = dpyinfo->scratch_cursor_gc;
9223 /* When on R2L character, show cursor at the right edge of the
9224 glyph, unless the cursor box is as wide as the glyph or wider
9225 (the latter happens when x-stretch-cursor is non-nil). */
9226 if ((cursor_glyph->resolved_level & 1) != 0
9227 && cursor_glyph->pixel_width > wd)
9229 x += cursor_glyph->pixel_width - wd;
9230 if (wd > 0)
9231 wd -= 1;
9233 /* Set clipping, draw the rectangle, and reset clipping again. */
9234 x_clip_to_row (w, row, TEXT_AREA, gc);
9235 x_draw_rectangle (f, gc, x, y, wd, h - 1);
9236 x_reset_clip_rectangles (f, gc);
9240 /* Draw a bar cursor on window W in glyph row ROW.
9242 Implementation note: One would like to draw a bar cursor with an
9243 angle equal to the one given by the font property XA_ITALIC_ANGLE.
9244 Unfortunately, I didn't find a font yet that has this property set.
9245 --gerd. */
9247 static void
9248 x_draw_bar_cursor (struct window *w, struct glyph_row *row, int width, enum text_cursor_kinds kind)
9250 struct frame *f = XFRAME (w->frame);
9251 struct glyph *cursor_glyph;
9253 /* If cursor is out of bounds, don't draw garbage. This can happen
9254 in mini-buffer windows when switching between echo area glyphs
9255 and mini-buffer. */
9256 cursor_glyph = get_phys_cursor_glyph (w);
9257 if (cursor_glyph == NULL)
9258 return;
9260 /* Experimental avoidance of cursor on xwidget. */
9261 if (cursor_glyph->type == XWIDGET_GLYPH)
9262 return;
9264 /* If on an image, draw like a normal cursor. That's usually better
9265 visible than drawing a bar, esp. if the image is large so that
9266 the bar might not be in the window. */
9267 if (cursor_glyph->type == IMAGE_GLYPH)
9269 struct glyph_row *r;
9270 r = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
9271 draw_phys_cursor_glyph (w, r, DRAW_CURSOR);
9273 else
9275 Display *dpy = FRAME_X_DISPLAY (f);
9276 Drawable drawable = FRAME_X_DRAWABLE (f);
9277 GC gc = FRAME_DISPLAY_INFO (f)->scratch_cursor_gc;
9278 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
9279 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
9280 XGCValues xgcv;
9282 /* If the glyph's background equals the color we normally draw
9283 the bars cursor in, the bar cursor in its normal color is
9284 invisible. Use the glyph's foreground color instead in this
9285 case, on the assumption that the glyph's colors are chosen so
9286 that the glyph is legible. */
9287 if (face->background == f->output_data.x->cursor_pixel)
9288 xgcv.background = xgcv.foreground = face->foreground;
9289 else
9290 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
9291 xgcv.graphics_exposures = False;
9293 if (gc)
9294 XChangeGC (dpy, gc, mask, &xgcv);
9295 else
9297 gc = XCreateGC (dpy, drawable, mask, &xgcv);
9298 FRAME_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
9301 x_clip_to_row (w, row, TEXT_AREA, gc);
9303 if (kind == BAR_CURSOR)
9305 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9307 if (width < 0)
9308 width = FRAME_CURSOR_WIDTH (f);
9309 width = min (cursor_glyph->pixel_width, width);
9311 w->phys_cursor_width = width;
9313 /* If the character under cursor is R2L, draw the bar cursor
9314 on the right of its glyph, rather than on the left. */
9315 if ((cursor_glyph->resolved_level & 1) != 0)
9316 x += cursor_glyph->pixel_width - width;
9318 x_fill_rectangle (f, gc, x,
9319 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
9320 width, row->height);
9322 else /* HBAR_CURSOR */
9324 int dummy_x, dummy_y, dummy_h;
9325 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9327 if (width < 0)
9328 width = row->height;
9330 width = min (row->height, width);
9332 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
9333 &dummy_y, &dummy_h);
9335 if ((cursor_glyph->resolved_level & 1) != 0
9336 && cursor_glyph->pixel_width > w->phys_cursor_width - 1)
9337 x += cursor_glyph->pixel_width - w->phys_cursor_width + 1;
9338 x_fill_rectangle (f, gc, x,
9339 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
9340 row->height - width),
9341 w->phys_cursor_width - 1, width);
9344 x_reset_clip_rectangles (f, gc);
9349 /* RIF: Define cursor CURSOR on frame F. */
9351 static void
9352 x_define_frame_cursor (struct frame *f, Cursor cursor)
9354 if (!f->pointer_invisible
9355 && f->output_data.x->current_cursor != cursor)
9356 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
9357 f->output_data.x->current_cursor = cursor;
9361 /* RIF: Clear area on frame F. */
9363 static void
9364 x_clear_frame_area (struct frame *f, int x, int y, int width, int height)
9366 x_clear_area (f, x, y, width, height);
9370 /* RIF: Draw cursor on window W. */
9372 static void
9373 x_draw_window_cursor (struct window *w, struct glyph_row *glyph_row, int x,
9374 int y, enum text_cursor_kinds cursor_type,
9375 int cursor_width, bool on_p, bool active_p)
9377 struct frame *f = XFRAME (WINDOW_FRAME (w));
9379 if (on_p)
9381 w->phys_cursor_type = cursor_type;
9382 w->phys_cursor_on_p = true;
9384 if (glyph_row->exact_window_width_line_p
9385 && (glyph_row->reversed_p
9386 ? (w->phys_cursor.hpos < 0)
9387 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
9389 glyph_row->cursor_in_fringe_p = true;
9390 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
9392 else
9394 switch (cursor_type)
9396 case HOLLOW_BOX_CURSOR:
9397 x_draw_hollow_cursor (w, glyph_row);
9398 break;
9400 case FILLED_BOX_CURSOR:
9401 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
9402 break;
9404 case BAR_CURSOR:
9405 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
9406 break;
9408 case HBAR_CURSOR:
9409 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
9410 break;
9412 case NO_CURSOR:
9413 w->phys_cursor_width = 0;
9414 break;
9416 default:
9417 emacs_abort ();
9421 #ifdef HAVE_X_I18N
9422 if (w == XWINDOW (f->selected_window))
9423 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
9424 xic_set_preeditarea (w, x, y);
9425 #endif
9428 XFlush (FRAME_X_DISPLAY (f));
9432 /* Icons. */
9434 /* Make the x-window of frame F use the gnu icon bitmap. */
9436 bool
9437 x_bitmap_icon (struct frame *f, Lisp_Object file)
9439 ptrdiff_t bitmap_id;
9441 if (FRAME_X_WINDOW (f) == 0)
9442 return true;
9444 /* Free up our existing icon bitmap and mask if any. */
9445 if (f->output_data.x->icon_bitmap > 0)
9446 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
9447 f->output_data.x->icon_bitmap = 0;
9449 if (STRINGP (file))
9451 #ifdef USE_GTK
9452 /* Use gtk_window_set_icon_from_file () if available,
9453 It's not restricted to bitmaps */
9454 if (xg_set_icon (f, file))
9455 return false;
9456 #endif /* USE_GTK */
9457 bitmap_id = x_create_bitmap_from_file (f, file);
9458 x_create_bitmap_mask (f, bitmap_id);
9460 else
9462 /* Create the GNU bitmap and mask if necessary. */
9463 if (FRAME_DISPLAY_INFO (f)->icon_bitmap_id < 0)
9465 ptrdiff_t rc = -1;
9467 #ifdef USE_GTK
9469 if (xg_set_icon (f, xg_default_icon_file)
9470 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
9472 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = -2;
9473 return false;
9476 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
9478 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
9479 if (rc != -1)
9480 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
9482 #endif
9484 /* If all else fails, use the (black and white) xbm image. */
9485 if (rc == -1)
9487 rc = x_create_bitmap_from_data (f, (char *) gnu_xbm_bits,
9488 gnu_xbm_width, gnu_xbm_height);
9489 if (rc == -1)
9490 return true;
9492 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
9493 x_create_bitmap_mask (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
9497 /* The first time we create the GNU bitmap and mask,
9498 this increments the ref-count one extra time.
9499 As a result, the GNU bitmap and mask are never freed.
9500 That way, we don't have to worry about allocating it again. */
9501 x_reference_bitmap (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
9503 bitmap_id = FRAME_DISPLAY_INFO (f)->icon_bitmap_id;
9506 x_wm_set_icon_pixmap (f, bitmap_id);
9507 f->output_data.x->icon_bitmap = bitmap_id;
9509 return false;
9513 /* Make the x-window of frame F use a rectangle with text.
9514 Use ICON_NAME as the text. */
9516 bool
9517 x_text_icon (struct frame *f, const char *icon_name)
9519 if (FRAME_X_WINDOW (f) == 0)
9520 return true;
9523 XTextProperty text;
9524 text.value = (unsigned char *) icon_name;
9525 text.encoding = XA_STRING;
9526 text.format = 8;
9527 text.nitems = strlen (icon_name);
9528 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
9531 if (f->output_data.x->icon_bitmap > 0)
9532 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
9533 f->output_data.x->icon_bitmap = 0;
9534 x_wm_set_icon_pixmap (f, 0);
9536 return false;
9539 #define X_ERROR_MESSAGE_SIZE 200
9541 /* If non-nil, this should be a string.
9542 It means catch X errors and store the error message in this string.
9544 The reason we use a stack is that x_catch_error/x_uncatch_error can
9545 be called from a signal handler.
9548 struct x_error_message_stack {
9549 char string[X_ERROR_MESSAGE_SIZE];
9550 Display *dpy;
9551 x_special_error_handler handler;
9552 void *handler_data;
9553 struct x_error_message_stack *prev;
9555 static struct x_error_message_stack *x_error_message;
9557 /* An X error handler which stores the error message in
9558 *x_error_message. This is called from x_error_handler if
9559 x_catch_errors is in effect. */
9561 static void
9562 x_error_catcher (Display *display, XErrorEvent *event)
9564 XGetErrorText (display, event->error_code,
9565 x_error_message->string,
9566 X_ERROR_MESSAGE_SIZE);
9567 if (x_error_message->handler)
9568 x_error_message->handler (display, event, x_error_message->string,
9569 x_error_message->handler_data);
9572 /* Begin trapping X errors for display DPY. Actually we trap X errors
9573 for all displays, but DPY should be the display you are actually
9574 operating on.
9576 After calling this function, X protocol errors no longer cause
9577 Emacs to exit; instead, they are recorded in the string
9578 stored in *x_error_message.
9580 Calling x_check_errors signals an Emacs error if an X error has
9581 occurred since the last call to x_catch_errors or x_check_errors.
9583 Calling x_uncatch_errors resumes the normal error handling.
9584 Calling x_uncatch_errors_after_check is similar, but skips an XSync
9585 to the server, and should be used only immediately after
9586 x_had_errors_p or x_check_errors. */
9588 void
9589 x_catch_errors_with_handler (Display *dpy, x_special_error_handler handler,
9590 void *handler_data)
9592 struct x_error_message_stack *data = xmalloc (sizeof *data);
9594 /* Make sure any errors from previous requests have been dealt with. */
9595 XSync (dpy, False);
9597 data->dpy = dpy;
9598 data->string[0] = 0;
9599 data->handler = handler;
9600 data->handler_data = handler_data;
9601 data->prev = x_error_message;
9602 x_error_message = data;
9605 void
9606 x_catch_errors (Display *dpy)
9608 x_catch_errors_with_handler (dpy, NULL, NULL);
9611 /* Undo the last x_catch_errors call.
9612 DPY should be the display that was passed to x_catch_errors.
9614 This version should be used only if the immediately preceding
9615 X-protocol-related thing was x_check_errors or x_had_error_p, both
9616 of which issue XSync calls, so we don't need to re-sync here. */
9618 void
9619 x_uncatch_errors_after_check (void)
9621 struct x_error_message_stack *tmp;
9623 block_input ();
9624 tmp = x_error_message;
9625 x_error_message = x_error_message->prev;
9626 xfree (tmp);
9627 unblock_input ();
9630 /* Undo the last x_catch_errors call.
9631 DPY should be the display that was passed to x_catch_errors. */
9633 void
9634 x_uncatch_errors (void)
9636 struct x_error_message_stack *tmp;
9638 block_input ();
9640 /* The display may have been closed before this function is called.
9641 Check if it is still open before calling XSync. */
9642 if (x_display_info_for_display (x_error_message->dpy) != 0)
9643 XSync (x_error_message->dpy, False);
9645 tmp = x_error_message;
9646 x_error_message = x_error_message->prev;
9647 xfree (tmp);
9648 unblock_input ();
9651 /* If any X protocol errors have arrived since the last call to
9652 x_catch_errors or x_check_errors, signal an Emacs error using
9653 sprintf (a buffer, FORMAT, the x error message text) as the text. */
9655 void
9656 x_check_errors (Display *dpy, const char *format)
9658 /* Make sure to catch any errors incurred so far. */
9659 XSync (dpy, False);
9661 if (x_error_message->string[0])
9663 char string[X_ERROR_MESSAGE_SIZE];
9664 memcpy (string, x_error_message->string, X_ERROR_MESSAGE_SIZE);
9665 x_uncatch_errors ();
9666 error (format, string);
9670 /* Nonzero if we had any X protocol errors
9671 since we did x_catch_errors on DPY. */
9673 bool
9674 x_had_errors_p (Display *dpy)
9676 /* Make sure to catch any errors incurred so far. */
9677 XSync (dpy, False);
9679 return x_error_message->string[0] != 0;
9682 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
9684 void
9685 x_clear_errors (Display *dpy)
9687 x_error_message->string[0] = 0;
9690 #if false
9691 /* See comment in unwind_to_catch why calling this is a bad
9692 * idea. --lorentey */
9693 /* Close off all unclosed x_catch_errors calls. */
9695 void
9696 x_fully_uncatch_errors (void)
9698 while (x_error_message)
9699 x_uncatch_errors ();
9701 #endif
9703 #if false
9704 static unsigned int x_wire_count;
9705 x_trace_wire (void)
9707 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
9709 #endif
9712 /************************************************************************
9713 Handling X errors
9714 ************************************************************************/
9716 /* Error message passed to x_connection_closed. */
9718 static char *error_msg;
9720 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
9721 the text of an error message that lead to the connection loss. */
9723 static _Noreturn void
9724 x_connection_closed (Display *dpy, const char *error_message, bool ioerror)
9726 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
9727 Lisp_Object frame, tail;
9728 ptrdiff_t idx = SPECPDL_INDEX ();
9730 error_msg = alloca (strlen (error_message) + 1);
9731 strcpy (error_msg, error_message);
9733 /* Inhibit redisplay while frames are being deleted. */
9734 specbind (Qinhibit_redisplay, Qt);
9736 if (dpyinfo)
9738 /* Protect display from being closed when we delete the last
9739 frame on it. */
9740 dpyinfo->reference_count++;
9741 dpyinfo->terminal->reference_count++;
9743 if (ioerror) dpyinfo->display = 0;
9745 /* First delete frames whose mini-buffers are on frames
9746 that are on the dead display. */
9747 FOR_EACH_FRAME (tail, frame)
9749 Lisp_Object minibuf_frame;
9750 minibuf_frame
9751 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
9752 if (FRAME_X_P (XFRAME (frame))
9753 && FRAME_X_P (XFRAME (minibuf_frame))
9754 && ! EQ (frame, minibuf_frame)
9755 && FRAME_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
9756 delete_frame (frame, Qnoelisp);
9759 /* Now delete all remaining frames on the dead display.
9760 We are now sure none of these is used as the mini-buffer
9761 for another frame that we need to delete. */
9762 FOR_EACH_FRAME (tail, frame)
9763 if (FRAME_X_P (XFRAME (frame))
9764 && FRAME_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
9766 /* Set this to t so that delete_frame won't get confused
9767 trying to find a replacement. */
9768 kset_default_minibuffer_frame (FRAME_KBOARD (XFRAME (frame)), Qt);
9769 delete_frame (frame, Qnoelisp);
9772 /* If DPYINFO is null, this means we didn't open the display in the
9773 first place, so don't try to close it. */
9774 if (dpyinfo)
9776 /* We can not call XtCloseDisplay here because it calls XSync.
9777 XSync inside the error handler apparently hangs Emacs. On
9778 current Xt versions, this isn't needed either. */
9779 #ifdef USE_GTK
9780 /* A long-standing GTK bug prevents proper disconnect handling
9781 (https://bugzilla.gnome.org/show_bug.cgi?id=85715). Once,
9782 the resulting Glib error message loop filled a user's disk.
9783 To avoid this, kill Emacs unconditionally on disconnect. */
9784 shut_down_emacs (0, Qnil);
9785 fprintf (stderr, "%s\n\
9786 When compiled with GTK, Emacs cannot recover from X disconnects.\n\
9787 This is a GTK bug: https://bugzilla.gnome.org/show_bug.cgi?id=85715\n\
9788 For details, see etc/PROBLEMS.\n",
9789 error_msg);
9790 emacs_abort ();
9791 #endif /* USE_GTK */
9793 /* Indicate that this display is dead. */
9794 dpyinfo->display = 0;
9796 dpyinfo->reference_count--;
9797 dpyinfo->terminal->reference_count--;
9798 if (dpyinfo->reference_count != 0)
9799 /* We have just closed all frames on this display. */
9800 emacs_abort ();
9803 Lisp_Object tmp;
9804 XSETTERMINAL (tmp, dpyinfo->terminal);
9805 Fdelete_terminal (tmp, Qnoelisp);
9809 if (terminal_list == 0)
9811 fprintf (stderr, "%s\n", error_msg);
9812 Fkill_emacs (make_number (70));
9813 /* NOTREACHED */
9816 totally_unblock_input ();
9818 unbind_to (idx, Qnil);
9819 clear_waiting_for_input ();
9821 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
9822 longjmp), because returning from this function would get us back into
9823 Xlib's code which will directly call `exit'. */
9824 error ("%s", error_msg);
9827 /* We specifically use it before defining it, so that gcc doesn't inline it,
9828 otherwise gdb doesn't know how to properly put a breakpoint on it. */
9829 static void x_error_quitter (Display *, XErrorEvent *);
9831 /* This is the first-level handler for X protocol errors.
9832 It calls x_error_quitter or x_error_catcher. */
9834 static int
9835 x_error_handler (Display *display, XErrorEvent *event)
9837 #if defined USE_GTK && defined HAVE_GTK3
9838 if ((event->error_code == BadMatch || event->error_code == BadWindow)
9839 && event->request_code == X_SetInputFocus)
9841 return 0;
9843 #endif
9845 if (x_error_message)
9846 x_error_catcher (display, event);
9847 else
9848 x_error_quitter (display, event);
9849 return 0;
9852 /* This is the usual handler for X protocol errors.
9853 It kills all frames on the display that we got the error for.
9854 If that was the only one, it prints an error message and kills Emacs. */
9856 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
9858 /* On older GCC versions, just putting x_error_quitter
9859 after x_error_handler prevents inlining into the former. */
9861 static void NO_INLINE
9862 x_error_quitter (Display *display, XErrorEvent *event)
9864 char buf[256], buf1[356];
9866 /* Ignore BadName errors. They can happen because of fonts
9867 or colors that are not defined. */
9869 if (event->error_code == BadName)
9870 return;
9872 /* Note that there is no real way portable across R3/R4 to get the
9873 original error handler. */
9875 XGetErrorText (display, event->error_code, buf, sizeof (buf));
9876 sprintf (buf1, "X protocol error: %s on protocol request %d",
9877 buf, event->request_code);
9878 x_connection_closed (display, buf1, false);
9882 /* This is the handler for X IO errors, always.
9883 It kills all frames on the display that we lost touch with.
9884 If that was the only one, it prints an error message and kills Emacs. */
9886 static _Noreturn int
9887 x_io_error_quitter (Display *display)
9889 char buf[256];
9891 snprintf (buf, sizeof buf, "Connection lost to X server '%s'",
9892 DisplayString (display));
9893 x_connection_closed (display, buf, true);
9894 assume (false);
9897 /* Changing the font of the frame. */
9899 /* Give frame F the font FONT-OBJECT as its default font. The return
9900 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
9901 frame. If it is negative, generate a new fontset from
9902 FONT-OBJECT. */
9904 Lisp_Object
9905 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
9907 struct font *font = XFONT_OBJECT (font_object);
9908 int unit, font_ascent, font_descent;
9909 #ifndef USE_X_TOOLKIT
9910 int old_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
9911 Lisp_Object fullscreen;
9912 #endif
9914 if (fontset < 0)
9915 fontset = fontset_from_font (font_object);
9916 FRAME_FONTSET (f) = fontset;
9917 if (FRAME_FONT (f) == font)
9918 /* This font is already set in frame F. There's nothing more to
9919 do. */
9920 return font_object;
9922 FRAME_FONT (f) = font;
9923 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
9924 FRAME_COLUMN_WIDTH (f) = font->average_width;
9925 get_font_ascent_descent (font, &font_ascent, &font_descent);
9926 FRAME_LINE_HEIGHT (f) = font_ascent + font_descent;
9928 #ifndef USE_X_TOOLKIT
9929 FRAME_MENU_BAR_HEIGHT (f) = FRAME_MENU_BAR_LINES (f) * FRAME_LINE_HEIGHT (f);
9930 #endif
9932 /* Compute character columns occupied by scrollbar.
9934 Don't do things differently for non-toolkit scrollbars
9935 (Bug#17163). */
9936 unit = FRAME_COLUMN_WIDTH (f);
9937 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
9938 FRAME_CONFIG_SCROLL_BAR_COLS (f)
9939 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + unit - 1) / unit;
9940 else
9941 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
9943 if (FRAME_X_WINDOW (f) != 0)
9945 /* Don't change the size of a tip frame; there's no point in
9946 doing it because it's done in Fx_show_tip, and it leads to
9947 problems because the tip frame has no widget. */
9948 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
9950 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
9951 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 3,
9952 false, Qfont);
9953 #ifndef USE_X_TOOLKIT
9954 if (FRAME_MENU_BAR_HEIGHT (f) != old_menu_bar_height
9955 && !f->after_make_frame
9956 && (EQ (frame_inhibit_implied_resize, Qt)
9957 || (CONSP (frame_inhibit_implied_resize)
9958 && NILP (Fmemq (Qfont, frame_inhibit_implied_resize))))
9959 && (NILP (fullscreen = get_frame_param (f, Qfullscreen))
9960 || EQ (fullscreen, Qfullwidth)))
9961 /* If the menu bar height changes, try to keep text height
9962 constant. */
9963 adjust_frame_size
9964 (f, -1, FRAME_TEXT_HEIGHT (f) + FRAME_MENU_BAR_HEIGHT (f)
9965 - old_menu_bar_height, 1, false, Qfont);
9966 #endif /* USE_X_TOOLKIT */
9970 #ifdef HAVE_X_I18N
9971 if (FRAME_XIC (f)
9972 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
9974 block_input ();
9975 xic_set_xfontset (f, SSDATA (fontset_ascii (fontset)));
9976 unblock_input ();
9978 #endif
9980 return font_object;
9984 /***********************************************************************
9985 X Input Methods
9986 ***********************************************************************/
9988 #ifdef HAVE_X_I18N
9990 #ifdef HAVE_X11R6
9992 /* XIM destroy callback function, which is called whenever the
9993 connection to input method XIM dies. CLIENT_DATA contains a
9994 pointer to the x_display_info structure corresponding to XIM. */
9996 static void
9997 xim_destroy_callback (XIM xim, XPointer client_data, XPointer call_data)
9999 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
10000 Lisp_Object frame, tail;
10002 block_input ();
10004 /* No need to call XDestroyIC.. */
10005 FOR_EACH_FRAME (tail, frame)
10007 struct frame *f = XFRAME (frame);
10008 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
10010 FRAME_XIC (f) = NULL;
10011 xic_free_xfontset (f);
10015 /* No need to call XCloseIM. */
10016 dpyinfo->xim = NULL;
10017 XFree (dpyinfo->xim_styles);
10018 unblock_input ();
10021 #endif /* HAVE_X11R6 */
10023 /* Open the connection to the XIM server on display DPYINFO.
10024 RESOURCE_NAME is the resource name Emacs uses. */
10026 static void
10027 xim_open_dpy (struct x_display_info *dpyinfo, char *resource_name)
10029 XIM xim;
10031 #ifdef HAVE_XIM
10032 if (use_xim)
10034 if (dpyinfo->xim)
10035 XCloseIM (dpyinfo->xim);
10036 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
10037 emacs_class);
10038 dpyinfo->xim = xim;
10040 if (xim)
10042 #ifdef HAVE_X11R6
10043 XIMCallback destroy;
10044 #endif
10046 /* Get supported styles and XIM values. */
10047 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
10049 #ifdef HAVE_X11R6
10050 destroy.callback = xim_destroy_callback;
10051 destroy.client_data = (XPointer)dpyinfo;
10052 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
10053 #endif
10057 else
10058 #endif /* HAVE_XIM */
10059 dpyinfo->xim = NULL;
10063 #ifdef HAVE_X11R6_XIM
10065 /* XIM instantiate callback function, which is called whenever an XIM
10066 server is available. DISPLAY is the display of the XIM.
10067 CLIENT_DATA contains a pointer to an xim_inst_t structure created
10068 when the callback was registered. */
10070 static void
10071 xim_instantiate_callback (Display *display, XPointer client_data, XPointer call_data)
10073 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
10074 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
10076 /* We don't support multiple XIM connections. */
10077 if (dpyinfo->xim)
10078 return;
10080 xim_open_dpy (dpyinfo, xim_inst->resource_name);
10082 /* Create XIC for the existing frames on the same display, as long
10083 as they have no XIC. */
10084 if (dpyinfo->xim && dpyinfo->reference_count > 0)
10086 Lisp_Object tail, frame;
10088 block_input ();
10089 FOR_EACH_FRAME (tail, frame)
10091 struct frame *f = XFRAME (frame);
10093 if (FRAME_X_P (f)
10094 && FRAME_DISPLAY_INFO (f) == xim_inst->dpyinfo)
10095 if (FRAME_XIC (f) == NULL)
10097 create_frame_xic (f);
10098 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
10099 xic_set_statusarea (f);
10100 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
10102 struct window *w = XWINDOW (f->selected_window);
10103 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
10108 unblock_input ();
10112 #endif /* HAVE_X11R6_XIM */
10115 /* Open a connection to the XIM server on display DPYINFO.
10116 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
10117 connection only at the first time. On X11R6, open the connection
10118 in the XIM instantiate callback function. */
10120 static void
10121 xim_initialize (struct x_display_info *dpyinfo, char *resource_name)
10123 dpyinfo->xim = NULL;
10124 #ifdef HAVE_XIM
10125 if (use_xim)
10127 #ifdef HAVE_X11R6_XIM
10128 struct xim_inst_t *xim_inst = xmalloc (sizeof *xim_inst);
10129 Bool ret;
10131 dpyinfo->xim_callback_data = xim_inst;
10132 xim_inst->dpyinfo = dpyinfo;
10133 xim_inst->resource_name = xstrdup (resource_name);
10134 ret = XRegisterIMInstantiateCallback
10135 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
10136 emacs_class, xim_instantiate_callback,
10137 /* This is XPointer in XFree86 but (XPointer *)
10138 on Tru64, at least, hence the configure test. */
10139 (XRegisterIMInstantiateCallback_arg6) xim_inst);
10140 eassert (ret == True);
10141 #else /* not HAVE_X11R6_XIM */
10142 xim_open_dpy (dpyinfo, resource_name);
10143 #endif /* not HAVE_X11R6_XIM */
10145 #endif /* HAVE_XIM */
10149 /* Close the connection to the XIM server on display DPYINFO. */
10151 static void
10152 xim_close_dpy (struct x_display_info *dpyinfo)
10154 #ifdef HAVE_XIM
10155 if (use_xim)
10157 #ifdef HAVE_X11R6_XIM
10158 struct xim_inst_t *xim_inst = dpyinfo->xim_callback_data;
10160 if (dpyinfo->display)
10162 Bool ret = XUnregisterIMInstantiateCallback
10163 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
10164 emacs_class, xim_instantiate_callback,
10165 (XRegisterIMInstantiateCallback_arg6) xim_inst);
10166 eassert (ret == True);
10168 xfree (xim_inst->resource_name);
10169 xfree (xim_inst);
10170 #endif /* HAVE_X11R6_XIM */
10171 if (dpyinfo->display)
10172 XCloseIM (dpyinfo->xim);
10173 dpyinfo->xim = NULL;
10174 XFree (dpyinfo->xim_styles);
10176 #endif /* HAVE_XIM */
10179 #endif /* not HAVE_X11R6_XIM */
10183 /* Calculate the absolute position in frame F
10184 from its current recorded position values and gravity. */
10186 static void
10187 x_calc_absolute_position (struct frame *f)
10189 int flags = f->size_hint_flags;
10190 struct frame *p = FRAME_PARENT_FRAME (f);
10192 /* We have nothing to do if the current position
10193 is already for the top-left corner. */
10194 if (! ((flags & XNegative) || (flags & YNegative)))
10195 return;
10197 /* Treat negative positions as relative to the leftmost bottommost
10198 position that fits on the screen. */
10199 if ((flags & XNegative) && (f->left_pos <= 0))
10201 int width = FRAME_PIXEL_WIDTH (f);
10203 /* A frame that has been visible at least once should have outer
10204 edges. */
10205 if (f->output_data.x->has_been_visible && !p)
10207 Lisp_Object frame;
10208 Lisp_Object edges = Qnil;
10210 XSETFRAME (frame, f);
10211 edges = Fx_frame_edges (frame, Qouter_edges);
10212 if (!NILP (edges))
10213 width = (XINT (Fnth (make_number (2), edges))
10214 - XINT (Fnth (make_number (0), edges)));
10217 if (p)
10218 f->left_pos = (FRAME_PIXEL_WIDTH (p) - width - 2 * f->border_width
10219 + f->left_pos);
10220 else
10221 f->left_pos = (x_display_pixel_width (FRAME_DISPLAY_INFO (f))
10222 - width + f->left_pos);
10226 if ((flags & YNegative) && (f->top_pos <= 0))
10228 int height = FRAME_PIXEL_HEIGHT (f);
10230 #if defined USE_X_TOOLKIT && defined USE_MOTIF
10231 /* Something is fishy here. When using Motif, starting Emacs with
10232 `-g -0-0', the frame appears too low by a few pixels.
10234 This seems to be so because initially, while Emacs is starting,
10235 the column widget's height and the frame's pixel height are
10236 different. The column widget's height is the right one. In
10237 later invocations, when Emacs is up, the frame's pixel height
10238 is right, though.
10240 It's not obvious where the initial small difference comes from.
10241 2000-12-01, gerd. */
10243 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
10244 #endif
10246 if (f->output_data.x->has_been_visible && !p)
10248 Lisp_Object frame;
10249 Lisp_Object edges = Qnil;
10251 XSETFRAME (frame, f);
10252 if (NILP (edges))
10253 edges = Fx_frame_edges (frame, Qouter_edges);
10254 if (!NILP (edges))
10255 height = (XINT (Fnth (make_number (3), edges))
10256 - XINT (Fnth (make_number (1), edges)));
10259 if (p)
10260 f->top_pos = (FRAME_PIXEL_HEIGHT (p) - height - 2 * f->border_width
10261 + f->top_pos);
10262 else
10263 f->top_pos = (x_display_pixel_height (FRAME_DISPLAY_INFO (f))
10264 - height + f->top_pos);
10267 /* The left_pos and top_pos
10268 are now relative to the top and left screen edges,
10269 so the flags should correspond. */
10270 f->size_hint_flags &= ~ (XNegative | YNegative);
10273 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
10274 to really change the position, and 0 when calling from
10275 x_make_frame_visible (in that case, XOFF and YOFF are the current
10276 position values). It is -1 when calling from x_set_frame_parameters,
10277 which means, do adjust for borders but don't change the gravity. */
10279 void
10280 x_set_offset (struct frame *f, register int xoff, register int yoff, int change_gravity)
10282 int modified_top, modified_left;
10284 if (change_gravity > 0)
10286 f->top_pos = yoff;
10287 f->left_pos = xoff;
10288 f->size_hint_flags &= ~ (XNegative | YNegative);
10289 if (xoff < 0)
10290 f->size_hint_flags |= XNegative;
10291 if (yoff < 0)
10292 f->size_hint_flags |= YNegative;
10293 f->win_gravity = NorthWestGravity;
10296 x_calc_absolute_position (f);
10298 block_input ();
10299 x_wm_set_size_hint (f, 0, false);
10301 #ifdef USE_GTK
10302 if (x_gtk_use_window_move)
10304 /* When a position change was requested and the outer GTK widget
10305 has been realized already, leave it to gtk_window_move to DTRT
10306 and return. Used for Bug#25851 and Bug#25943. */
10307 if (change_gravity != 0 && FRAME_GTK_OUTER_WIDGET (f))
10308 gtk_window_move (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
10309 f->left_pos, f->top_pos);
10310 unblock_input ();
10311 return;
10313 #endif /* USE_GTK */
10315 modified_left = f->left_pos;
10316 modified_top = f->top_pos;
10318 if (change_gravity != 0 && FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
10320 /* Some WMs (twm, wmaker at least) has an offset that is smaller
10321 than the WM decorations. So we use the calculated offset instead
10322 of the WM decoration sizes here (x/y_pixels_outer_diff). */
10323 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
10324 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
10327 #ifdef USE_GTK
10328 gtk_window_move (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
10329 modified_left, modified_top);
10330 #else
10331 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10332 modified_left, modified_top);
10333 #endif
10335 x_sync_with_move (f, f->left_pos, f->top_pos,
10336 FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN);
10338 /* change_gravity is non-zero when this function is called from Lisp to
10339 programmatically move a frame. In that case, we call
10340 x_check_expected_move to discover if we have a "Type A" or "Type B"
10341 window manager, and, for a "Type A" window manager, adjust the position
10342 of the frame.
10344 We call x_check_expected_move if a programmatic move occurred, and
10345 either the window manager type (A/B) is unknown or it is Type A but we
10346 need to compute the top/left offset adjustment for this frame. */
10348 if (change_gravity != 0
10349 && !FRAME_PARENT_FRAME (f)
10350 && (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
10351 || (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
10352 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
10353 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
10354 x_check_expected_move (f, modified_left, modified_top);
10356 unblock_input ();
10359 /* Return true if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
10360 on the root window for frame F contains ATOMNAME.
10361 This is how a WM check shall be done according to the Window Manager
10362 Specification/Extended Window Manager Hints at
10363 http://freedesktop.org/wiki/Specifications/wm-spec. */
10365 bool
10366 x_wm_supports (struct frame *f, Atom want_atom)
10368 Atom actual_type;
10369 unsigned long actual_size, bytes_remaining;
10370 int i, rc, actual_format;
10371 bool ret;
10372 Window wmcheck_window;
10373 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10374 Window target_window = dpyinfo->root_window;
10375 int max_len = 65536;
10376 Display *dpy = FRAME_X_DISPLAY (f);
10377 unsigned char *tmp_data = NULL;
10378 Atom target_type = XA_WINDOW;
10380 block_input ();
10382 x_catch_errors (dpy);
10383 rc = XGetWindowProperty (dpy, target_window,
10384 dpyinfo->Xatom_net_supporting_wm_check,
10385 0, max_len, False, target_type,
10386 &actual_type, &actual_format, &actual_size,
10387 &bytes_remaining, &tmp_data);
10389 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
10391 if (tmp_data) XFree (tmp_data);
10392 x_uncatch_errors ();
10393 unblock_input ();
10394 return false;
10397 wmcheck_window = *(Window *) tmp_data;
10398 XFree (tmp_data);
10400 /* Check if window exists. */
10401 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
10402 if (x_had_errors_p (dpy))
10404 x_uncatch_errors_after_check ();
10405 unblock_input ();
10406 return false;
10409 if (dpyinfo->net_supported_window != wmcheck_window)
10411 /* Window changed, reload atoms */
10412 if (dpyinfo->net_supported_atoms != NULL)
10413 XFree (dpyinfo->net_supported_atoms);
10414 dpyinfo->net_supported_atoms = NULL;
10415 dpyinfo->nr_net_supported_atoms = 0;
10416 dpyinfo->net_supported_window = 0;
10418 target_type = XA_ATOM;
10419 tmp_data = NULL;
10420 rc = XGetWindowProperty (dpy, target_window,
10421 dpyinfo->Xatom_net_supported,
10422 0, max_len, False, target_type,
10423 &actual_type, &actual_format, &actual_size,
10424 &bytes_remaining, &tmp_data);
10426 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
10428 if (tmp_data) XFree (tmp_data);
10429 x_uncatch_errors ();
10430 unblock_input ();
10431 return false;
10434 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
10435 dpyinfo->nr_net_supported_atoms = actual_size;
10436 dpyinfo->net_supported_window = wmcheck_window;
10439 ret = false;
10441 for (i = 0; !ret && i < dpyinfo->nr_net_supported_atoms; ++i)
10442 ret = dpyinfo->net_supported_atoms[i] == want_atom;
10444 x_uncatch_errors ();
10445 unblock_input ();
10447 return ret;
10450 static void
10451 set_wm_state (Lisp_Object frame, bool add, Atom atom, Atom value)
10453 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (XFRAME (frame));
10455 x_send_client_event (frame, make_number (0), frame,
10456 dpyinfo->Xatom_net_wm_state,
10457 make_number (32),
10458 /* 1 = add, 0 = remove */
10459 Fcons
10460 (make_number (add),
10461 Fcons
10462 (make_fixnum_or_float (atom),
10463 (value != 0
10464 ? list1 (make_fixnum_or_float (value))
10465 : Qnil))));
10468 void
10469 x_set_sticky (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
10471 Lisp_Object frame;
10472 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10474 XSETFRAME (frame, f);
10476 set_wm_state (frame, !NILP (new_value),
10477 dpyinfo->Xatom_net_wm_state_sticky, None);
10481 * x_set_skip_taskbar:
10483 * Set frame F's `skip-taskbar' parameter. If non-nil, this should
10484 * remove F's icon from the taskbar associated with the display of F's
10485 * window-system window and inhibit switching to F's window via
10486 * <Alt>-<TAB>. If nil, lift these restrictions.
10488 * Some window managers may not honor this parameter.
10490 void
10491 x_set_skip_taskbar (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
10493 if (!EQ (new_value, old_value))
10495 #ifdef USE_GTK
10496 xg_set_skip_taskbar (f, new_value);
10497 #else
10498 Lisp_Object frame;
10499 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10501 XSETFRAME (frame, f);
10502 set_wm_state (frame, !NILP (new_value),
10503 dpyinfo->Xatom_net_wm_state_skip_taskbar, None);
10504 #endif /* USE_GTK */
10505 FRAME_SKIP_TASKBAR (f) = !NILP (new_value);
10510 * x_set_z_group:
10512 * Set frame F's `z-group' parameter. If `above', F's window-system
10513 * window is displayed above all windows that do not have the `above'
10514 * property set. If nil, F's window is shown below all windows that
10515 * have the `above' property set and above all windows that have the
10516 * `below' property set. If `below', F's window is displayed below all
10517 * windows that do not have the `below' property set.
10519 * Some window managers may not honor this parameter.
10521 void
10522 x_set_z_group (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
10524 /* We don't care about old_value. The window manager might have
10525 reset the value without telling us. */
10526 Lisp_Object frame;
10527 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10529 XSETFRAME (frame, f);
10531 if (NILP (new_value))
10533 set_wm_state (frame, false,
10534 dpyinfo->Xatom_net_wm_state_above, None);
10535 set_wm_state (frame, false,
10536 dpyinfo->Xatom_net_wm_state_below, None);
10537 FRAME_Z_GROUP (f) = z_group_none;
10539 else if (EQ (new_value, Qabove))
10541 set_wm_state (frame, true,
10542 dpyinfo->Xatom_net_wm_state_above, None);
10543 set_wm_state (frame, false,
10544 dpyinfo->Xatom_net_wm_state_below, None);
10545 FRAME_Z_GROUP (f) = z_group_above;
10547 else if (EQ (new_value, Qbelow))
10549 set_wm_state (frame, false,
10550 dpyinfo->Xatom_net_wm_state_above, None);
10551 set_wm_state (frame, true,
10552 dpyinfo->Xatom_net_wm_state_below, None);
10553 FRAME_Z_GROUP (f) = z_group_below;
10555 else if (EQ (new_value, Qabove_suspended))
10557 set_wm_state (frame, false,
10558 dpyinfo->Xatom_net_wm_state_above, None);
10559 FRAME_Z_GROUP (f) = z_group_above_suspended;
10561 else
10562 error ("Invalid z-group specification");
10566 /* Return the current _NET_WM_STATE.
10567 SIZE_STATE is set to one of the FULLSCREEN_* values.
10568 Set *STICKY to the sticky state.
10570 Return true iff we are not hidden. */
10572 static bool
10573 get_current_wm_state (struct frame *f,
10574 Window window,
10575 int *size_state,
10576 bool *sticky)
10578 unsigned long actual_size;
10579 int i;
10580 bool is_hidden = false;
10581 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10582 long max_len = 65536;
10583 Atom target_type = XA_ATOM;
10584 /* If XCB is available, we can avoid three XSync calls. */
10585 #ifdef USE_XCB
10586 xcb_get_property_cookie_t prop_cookie;
10587 xcb_get_property_reply_t *prop;
10588 xcb_atom_t *reply_data;
10589 #else
10590 Display *dpy = FRAME_X_DISPLAY (f);
10591 unsigned long bytes_remaining;
10592 int rc, actual_format;
10593 Atom actual_type;
10594 unsigned char *tmp_data = NULL;
10595 Atom *reply_data;
10596 #endif
10598 *sticky = false;
10599 *size_state = FULLSCREEN_NONE;
10601 block_input ();
10603 #ifdef USE_XCB
10604 prop_cookie = xcb_get_property (dpyinfo->xcb_connection, 0, window,
10605 dpyinfo->Xatom_net_wm_state,
10606 target_type, 0, max_len);
10607 prop = xcb_get_property_reply (dpyinfo->xcb_connection, prop_cookie, NULL);
10608 if (prop && prop->type == target_type)
10610 int actual_bytes = xcb_get_property_value_length (prop);
10611 eassume (0 <= actual_bytes);
10612 actual_size = actual_bytes / sizeof *reply_data;
10613 reply_data = xcb_get_property_value (prop);
10615 else
10617 actual_size = 0;
10618 is_hidden = FRAME_ICONIFIED_P (f);
10620 #else
10621 x_catch_errors (dpy);
10622 rc = XGetWindowProperty (dpy, window, dpyinfo->Xatom_net_wm_state,
10623 0, max_len, False, target_type,
10624 &actual_type, &actual_format, &actual_size,
10625 &bytes_remaining, &tmp_data);
10627 if (rc == Success && actual_type == target_type && ! x_had_errors_p (dpy))
10628 reply_data = (Atom *) tmp_data;
10629 else
10631 actual_size = 0;
10632 is_hidden = FRAME_ICONIFIED_P (f);
10635 x_uncatch_errors ();
10636 #endif
10638 for (i = 0; i < actual_size; ++i)
10640 Atom a = reply_data[i];
10641 if (a == dpyinfo->Xatom_net_wm_state_hidden)
10642 is_hidden = true;
10643 else if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
10645 if (*size_state == FULLSCREEN_HEIGHT)
10646 *size_state = FULLSCREEN_MAXIMIZED;
10647 else
10648 *size_state = FULLSCREEN_WIDTH;
10650 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
10652 if (*size_state == FULLSCREEN_WIDTH)
10653 *size_state = FULLSCREEN_MAXIMIZED;
10654 else
10655 *size_state = FULLSCREEN_HEIGHT;
10657 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen)
10658 *size_state = FULLSCREEN_BOTH;
10659 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
10660 *sticky = true;
10663 #ifdef USE_XCB
10664 free (prop);
10665 #else
10666 if (tmp_data) XFree (tmp_data);
10667 #endif
10669 unblock_input ();
10670 return ! is_hidden;
10673 /* Do fullscreen as specified in extended window manager hints */
10675 static bool
10676 do_ewmh_fullscreen (struct frame *f)
10678 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10679 bool have_net_atom = x_wm_supports (f, dpyinfo->Xatom_net_wm_state);
10680 int cur;
10681 bool dummy;
10683 get_current_wm_state (f, FRAME_OUTER_WINDOW (f), &cur, &dummy);
10685 /* Some window managers don't say they support _NET_WM_STATE, but they do say
10686 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
10687 if (!have_net_atom)
10688 have_net_atom = x_wm_supports (f, dpyinfo->Xatom_net_wm_state_fullscreen);
10690 if (have_net_atom && cur != f->want_fullscreen)
10692 Lisp_Object frame;
10694 XSETFRAME (frame, f);
10696 /* Keep number of calls to set_wm_state as low as possible.
10697 Some window managers, or possible Gtk+, hangs when too many
10698 are sent at once. */
10699 switch (f->want_fullscreen)
10701 case FULLSCREEN_BOTH:
10702 if (cur != FULLSCREEN_BOTH)
10703 set_wm_state (frame, true, dpyinfo->Xatom_net_wm_state_fullscreen,
10704 None);
10705 break;
10706 case FULLSCREEN_WIDTH:
10707 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_MAXIMIZED)
10709 set_wm_state (frame, false,
10710 dpyinfo->Xatom_net_wm_state_maximized_horz,
10711 dpyinfo->Xatom_net_wm_state_maximized_vert);
10712 set_wm_state (frame, true,
10713 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10715 else
10717 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_HEIGHT
10718 || cur == FULLSCREEN_MAXIMIZED)
10719 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10720 dpyinfo->Xatom_net_wm_state_maximized_vert);
10721 if (cur != FULLSCREEN_MAXIMIZED || x_frame_normalize_before_maximize)
10722 set_wm_state (frame, true,
10723 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10725 break;
10726 case FULLSCREEN_HEIGHT:
10727 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_MAXIMIZED)
10729 set_wm_state (frame, false,
10730 dpyinfo->Xatom_net_wm_state_maximized_horz,
10731 dpyinfo->Xatom_net_wm_state_maximized_vert);
10732 set_wm_state (frame, true,
10733 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10735 else
10737 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_WIDTH
10738 || cur == FULLSCREEN_MAXIMIZED)
10739 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10740 dpyinfo->Xatom_net_wm_state_maximized_horz);
10741 if (cur != FULLSCREEN_MAXIMIZED || x_frame_normalize_before_maximize)
10742 set_wm_state (frame, true,
10743 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10745 break;
10746 case FULLSCREEN_MAXIMIZED:
10747 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_BOTH)
10749 set_wm_state (frame, false,
10750 dpyinfo->Xatom_net_wm_state_fullscreen, None);
10751 set_wm_state (frame, true,
10752 dpyinfo->Xatom_net_wm_state_maximized_horz,
10753 dpyinfo->Xatom_net_wm_state_maximized_vert);
10755 else if (x_frame_normalize_before_maximize && cur == FULLSCREEN_WIDTH)
10757 set_wm_state (frame, false,
10758 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10759 set_wm_state (frame, true,
10760 dpyinfo->Xatom_net_wm_state_maximized_horz,
10761 dpyinfo->Xatom_net_wm_state_maximized_vert);
10763 else if (x_frame_normalize_before_maximize && cur == FULLSCREEN_HEIGHT)
10765 set_wm_state (frame, false,
10766 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10767 set_wm_state (frame, true,
10768 dpyinfo->Xatom_net_wm_state_maximized_horz,
10769 dpyinfo->Xatom_net_wm_state_maximized_vert);
10771 else
10773 if (cur == FULLSCREEN_BOTH)
10774 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10775 None);
10776 else if (cur == FULLSCREEN_HEIGHT)
10777 set_wm_state (frame, true,
10778 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10779 else if (cur == FULLSCREEN_WIDTH)
10780 set_wm_state (frame, true, None,
10781 dpyinfo->Xatom_net_wm_state_maximized_vert);
10782 else
10783 set_wm_state (frame, true,
10784 dpyinfo->Xatom_net_wm_state_maximized_horz,
10785 dpyinfo->Xatom_net_wm_state_maximized_vert);
10787 break;
10788 case FULLSCREEN_NONE:
10789 if (cur == FULLSCREEN_BOTH)
10790 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10791 None);
10792 else
10793 set_wm_state (frame, false,
10794 dpyinfo->Xatom_net_wm_state_maximized_horz,
10795 dpyinfo->Xatom_net_wm_state_maximized_vert);
10798 f->want_fullscreen = FULLSCREEN_NONE;
10802 return have_net_atom;
10805 static void
10806 XTfullscreen_hook (struct frame *f)
10808 if (FRAME_VISIBLE_P (f))
10810 block_input ();
10811 x_check_fullscreen (f);
10812 x_sync (f);
10813 unblock_input ();
10818 static bool
10819 x_handle_net_wm_state (struct frame *f, const XPropertyEvent *event)
10821 int value = FULLSCREEN_NONE;
10822 Lisp_Object lval;
10823 bool sticky = false;
10824 bool not_hidden = get_current_wm_state (f, event->window, &value, &sticky);
10826 lval = Qnil;
10827 switch (value)
10829 case FULLSCREEN_WIDTH:
10830 lval = Qfullwidth;
10831 break;
10832 case FULLSCREEN_HEIGHT:
10833 lval = Qfullheight;
10834 break;
10835 case FULLSCREEN_BOTH:
10836 lval = Qfullboth;
10837 break;
10838 case FULLSCREEN_MAXIMIZED:
10839 lval = Qmaximized;
10840 break;
10843 frame_size_history_add
10844 (f, Qx_handle_net_wm_state, 0, 0,
10845 list2 (get_frame_param (f, Qfullscreen), lval));
10847 store_frame_param (f, Qfullscreen, lval);
10848 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
10850 return not_hidden;
10853 /* Check if we need to resize the frame due to a fullscreen request.
10854 If so needed, resize the frame. */
10855 static void
10856 x_check_fullscreen (struct frame *f)
10858 Lisp_Object lval = Qnil;
10860 if (do_ewmh_fullscreen (f))
10861 return;
10863 if (f->output_data.x->parent_desc != FRAME_DISPLAY_INFO (f)->root_window)
10864 return; /* Only fullscreen without WM or with EWM hints (above). */
10866 /* Setting fullscreen to nil doesn't do anything. We could save the
10867 last non-fullscreen size and restore it, but it seems like a
10868 lot of work for this unusual case (no window manager running). */
10870 if (f->want_fullscreen != FULLSCREEN_NONE)
10872 int width = FRAME_PIXEL_WIDTH (f), height = FRAME_PIXEL_HEIGHT (f);
10873 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10875 switch (f->want_fullscreen)
10877 /* No difference between these two when there is no WM */
10878 case FULLSCREEN_MAXIMIZED:
10879 lval = Qmaximized;
10880 width = x_display_pixel_width (dpyinfo);
10881 height = x_display_pixel_height (dpyinfo);
10882 break;
10883 case FULLSCREEN_BOTH:
10884 lval = Qfullboth;
10885 width = x_display_pixel_width (dpyinfo);
10886 height = x_display_pixel_height (dpyinfo);
10887 break;
10888 case FULLSCREEN_WIDTH:
10889 lval = Qfullwidth;
10890 width = x_display_pixel_width (dpyinfo);
10891 height = height + FRAME_MENUBAR_HEIGHT (f);
10892 break;
10893 case FULLSCREEN_HEIGHT:
10894 lval = Qfullheight;
10895 height = x_display_pixel_height (dpyinfo);
10896 break;
10897 default:
10898 emacs_abort ();
10901 frame_size_history_add
10902 (f, Qx_check_fullscreen, width, height, Qnil);
10904 x_wm_set_size_hint (f, 0, false);
10906 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10907 width, height);
10909 if (FRAME_VISIBLE_P (f))
10910 x_wait_for_event (f, ConfigureNotify);
10911 else
10913 change_frame_size (f, width, height - FRAME_MENUBAR_HEIGHT (f),
10914 false, true, false, true);
10915 x_sync (f);
10919 /* `x_net_wm_state' might have reset the fullscreen frame parameter,
10920 restore it. */
10921 store_frame_param (f, Qfullscreen, lval);
10924 /* This function is called by x_set_offset to determine whether the window
10925 manager interfered with the positioning of the frame. Type A window
10926 managers position the surrounding window manager decorations a small
10927 amount above and left of the user-supplied position. Type B window
10928 managers position the surrounding window manager decorations at the
10929 user-specified position. If we detect a Type A window manager, we
10930 compensate by moving the window right and down by the proper amount. */
10932 static void
10933 x_check_expected_move (struct frame *f, int expected_left, int expected_top)
10935 int current_left = 0, current_top = 0;
10937 /* x_real_positions returns the left and top offsets of the outermost
10938 window manager window around the frame. */
10940 x_real_positions (f, &current_left, &current_top);
10942 if (current_left != expected_left || current_top != expected_top)
10944 /* It's a "Type A" window manager. */
10946 int adjusted_left;
10947 int adjusted_top;
10949 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
10950 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
10951 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
10953 /* Now fix the mispositioned frame's location. */
10955 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
10956 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
10958 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10959 adjusted_left, adjusted_top);
10961 x_sync_with_move (f, expected_left, expected_top, false);
10963 else
10964 /* It's a "Type B" window manager. We don't have to adjust the
10965 frame's position. */
10967 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
10971 /* Wait for XGetGeometry to return up-to-date position information for a
10972 recently-moved frame. Call this immediately after calling XMoveWindow.
10973 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
10974 frame has been moved to, so we use a fuzzy position comparison instead
10975 of an exact comparison. */
10977 static void
10978 x_sync_with_move (struct frame *f, int left, int top, bool fuzzy)
10980 int count = 0;
10982 while (count++ < 50)
10984 int current_left = 0, current_top = 0;
10986 /* In theory, this call to XSync only needs to happen once, but in
10987 practice, it doesn't seem to work, hence the need for the surrounding
10988 loop. */
10990 XSync (FRAME_X_DISPLAY (f), False);
10991 x_real_positions (f, &current_left, &current_top);
10993 if (fuzzy)
10995 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
10996 pixels. */
10998 if (eabs (current_left - left) <= 10
10999 && eabs (current_top - top) <= 40)
11000 return;
11002 else if (current_left == left && current_top == top)
11003 return;
11006 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
11007 will then return up-to-date position info. */
11009 wait_reading_process_output (0, 500000000, 0, false, Qnil, NULL, 0);
11013 /* Wait for an event on frame F matching EVENTTYPE. */
11014 void
11015 x_wait_for_event (struct frame *f, int eventtype)
11017 int level = interrupt_input_blocked;
11019 fd_set fds;
11020 struct timespec tmo, tmo_at, time_now;
11021 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
11023 f->wait_event_type = eventtype;
11025 /* Set timeout to 0.1 second. Hopefully not noticeable.
11026 Maybe it should be configurable. */
11027 tmo = make_timespec (0, 100 * 1000 * 1000);
11028 tmo_at = timespec_add (current_timespec (), tmo);
11030 while (f->wait_event_type)
11032 pending_signals = true;
11033 totally_unblock_input ();
11034 /* XTread_socket is called after unblock. */
11035 block_input ();
11036 interrupt_input_blocked = level;
11038 FD_ZERO (&fds);
11039 FD_SET (fd, &fds);
11041 time_now = current_timespec ();
11042 if (timespec_cmp (tmo_at, time_now) < 0)
11043 break;
11045 tmo = timespec_sub (tmo_at, time_now);
11046 if (pselect (fd + 1, &fds, NULL, NULL, &tmo, NULL) == 0)
11047 break; /* Timeout */
11050 f->wait_event_type = 0;
11054 /* Change the size of frame F's X window to WIDTH/HEIGHT in the case F
11055 doesn't have a widget. If CHANGE_GRAVITY, change to
11056 top-left-corner window gravity for this size change and subsequent
11057 size changes. Otherwise leave the window gravity unchanged. */
11059 static void
11060 x_set_window_size_1 (struct frame *f, bool change_gravity,
11061 int width, int height)
11063 int pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
11064 int pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
11065 int old_width = FRAME_PIXEL_WIDTH (f);
11066 int old_height = FRAME_PIXEL_HEIGHT (f);
11067 Lisp_Object fullscreen = get_frame_param (f, Qfullscreen);
11069 if (change_gravity)
11070 f->win_gravity = NorthWestGravity;
11071 x_wm_set_size_hint (f, 0, false);
11073 /* When the frame is fullheight and we only want to change the width
11074 or it is fullwidth and we only want to change the height we should
11075 be able to preserve the fullscreen property. However, due to the
11076 fact that we have to send a resize request anyway, the window
11077 manager will abolish it. At least the respective size should
11078 remain unchanged but giving the frame back its normal size will
11079 be broken ... */
11080 if (EQ (fullscreen, Qfullwidth) && width == FRAME_TEXT_WIDTH (f))
11082 frame_size_history_add
11083 (f, Qx_set_window_size_1, width, height,
11084 list2 (make_number (old_height),
11085 make_number (pixelheight + FRAME_MENUBAR_HEIGHT (f))));
11087 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11088 old_width, pixelheight + FRAME_MENUBAR_HEIGHT (f));
11090 else if (EQ (fullscreen, Qfullheight) && height == FRAME_TEXT_HEIGHT (f))
11092 frame_size_history_add
11093 (f, Qx_set_window_size_2, width, height,
11094 list2 (make_number (old_width), make_number (pixelwidth)));
11096 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11097 pixelwidth, old_height);
11100 else
11102 frame_size_history_add
11103 (f, Qx_set_window_size_3, width, height,
11104 list3 (make_number (pixelwidth + FRAME_TOOLBAR_WIDTH (f)),
11105 make_number (pixelheight + FRAME_TOOLBAR_HEIGHT (f)
11106 + FRAME_MENUBAR_HEIGHT (f)),
11107 make_number (FRAME_MENUBAR_HEIGHT (f))));
11109 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11110 pixelwidth, pixelheight + FRAME_MENUBAR_HEIGHT (f));
11111 fullscreen = Qnil;
11116 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11117 receive in the ConfigureNotify event; if we get what we asked
11118 for, then the event won't cause the screen to become garbaged, so
11119 we have to make sure to do it here. */
11120 SET_FRAME_GARBAGED (f);
11122 /* Now, strictly speaking, we can't be sure that this is accurate,
11123 but the window manager will get around to dealing with the size
11124 change request eventually, and we'll hear how it went when the
11125 ConfigureNotify event gets here.
11127 We could just not bother storing any of this information here,
11128 and let the ConfigureNotify event set everything up, but that
11129 might be kind of confusing to the Lisp code, since size changes
11130 wouldn't be reported in the frame parameters until some random
11131 point in the future when the ConfigureNotify event arrives.
11133 Pass true for DELAY since we can't run Lisp code inside of
11134 a BLOCK_INPUT. */
11136 /* But the ConfigureNotify may in fact never arrive, and then this is
11137 not right if the frame is visible. Instead wait (with timeout)
11138 for the ConfigureNotify. */
11139 if (FRAME_VISIBLE_P (f))
11141 x_wait_for_event (f, ConfigureNotify);
11143 if (!NILP (fullscreen))
11144 /* Try to restore fullscreen state. */
11146 store_frame_param (f, Qfullscreen, fullscreen);
11147 x_set_fullscreen (f, fullscreen, fullscreen);
11150 else
11152 change_frame_size (f, width, height, false, true, false, true);
11153 x_sync (f);
11158 /* Call this to change the size of frame F's x-window.
11159 If CHANGE_GRAVITY, change to top-left-corner window gravity
11160 for this size change and subsequent size changes.
11161 Otherwise we leave the window gravity unchanged. */
11163 void
11164 x_set_window_size (struct frame *f, bool change_gravity,
11165 int width, int height, bool pixelwise)
11167 block_input ();
11169 /* The following breaks our calculations. If it's really needed,
11170 think of something else. */
11171 #if false
11172 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
11174 int text_width, text_height;
11176 /* When the frame is maximized/fullscreen or running under for
11177 example Xmonad, x_set_window_size_1 will be a no-op.
11178 In that case, the right thing to do is extend rows/width to
11179 the current frame size. We do that first if x_set_window_size_1
11180 turns out to not be a no-op (there is no way to know).
11181 The size will be adjusted again if the frame gets a
11182 ConfigureNotify event as a result of x_set_window_size. */
11183 int pixelh = FRAME_PIXEL_HEIGHT (f);
11184 #ifdef USE_X_TOOLKIT
11185 /* The menu bar is not part of text lines. The tool bar
11186 is however. */
11187 pixelh -= FRAME_MENUBAR_HEIGHT (f);
11188 #endif
11189 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, FRAME_PIXEL_WIDTH (f));
11190 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelh);
11192 change_frame_size (f, text_width, text_height, false, true, false, true);
11194 #endif
11196 /* Pixelize width and height, if necessary. */
11197 if (! pixelwise)
11199 width = width * FRAME_COLUMN_WIDTH (f);
11200 height = height * FRAME_LINE_HEIGHT (f);
11203 #ifdef USE_GTK
11204 if (FRAME_GTK_WIDGET (f))
11205 xg_frame_set_char_size (f, width, height);
11206 else
11207 x_set_window_size_1 (f, change_gravity, width, height);
11208 #else /* not USE_GTK */
11209 x_set_window_size_1 (f, change_gravity, width, height);
11210 x_clear_under_internal_border (f);
11211 #endif /* not USE_GTK */
11213 /* If cursor was outside the new size, mark it as off. */
11214 mark_window_cursors_off (XWINDOW (f->root_window));
11216 /* Clear out any recollection of where the mouse highlighting was,
11217 since it might be in a place that's outside the new frame size.
11218 Actually checking whether it is outside is a pain in the neck,
11219 so don't try--just let the highlighting be done afresh with new size. */
11220 cancel_mouse_face (f);
11222 unblock_input ();
11224 do_pending_window_change (false);
11227 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11229 void
11230 frame_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
11232 block_input ();
11234 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11235 0, 0, 0, 0, pix_x, pix_y);
11236 unblock_input ();
11239 /* Raise frame F. */
11241 void
11242 x_raise_frame (struct frame *f)
11244 block_input ();
11245 if (FRAME_VISIBLE_P (f))
11246 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
11247 XFlush (FRAME_X_DISPLAY (f));
11248 unblock_input ();
11251 /* Lower frame F. */
11253 static void
11254 x_lower_frame (struct frame *f)
11256 if (FRAME_VISIBLE_P (f))
11258 block_input ();
11259 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
11260 XFlush (FRAME_X_DISPLAY (f));
11261 unblock_input ();
11265 /* Request focus with XEmbed */
11267 void
11268 xembed_request_focus (struct frame *f)
11270 /* See XEmbed Protocol Specification at
11271 http://freedesktop.org/wiki/Specifications/xembed-spec */
11272 if (FRAME_VISIBLE_P (f))
11273 xembed_send_message (f, CurrentTime,
11274 XEMBED_REQUEST_FOCUS, 0, 0, 0);
11277 /* Activate frame with Extended Window Manager Hints */
11279 void
11280 x_ewmh_activate_frame (struct frame *f)
11282 /* See Window Manager Specification/Extended Window Manager Hints at
11283 http://freedesktop.org/wiki/Specifications/wm-spec */
11285 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11287 if (FRAME_VISIBLE_P (f) && x_wm_supports (f, dpyinfo->Xatom_net_active_window))
11289 Lisp_Object frame;
11290 XSETFRAME (frame, f);
11291 x_send_client_event (frame, make_number (0), frame,
11292 dpyinfo->Xatom_net_active_window,
11293 make_number (32),
11294 list2i (1, dpyinfo->last_user_time));
11298 static void
11299 XTframe_raise_lower (struct frame *f, bool raise_flag)
11301 if (raise_flag)
11302 x_raise_frame (f);
11303 else
11304 x_lower_frame (f);
11307 /* XEmbed implementation. */
11309 #if defined USE_X_TOOLKIT || ! defined USE_GTK
11311 /* XEmbed implementation. */
11313 #define XEMBED_VERSION 0
11315 static void
11316 xembed_set_info (struct frame *f, enum xembed_info flags)
11318 unsigned long data[2];
11319 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11321 data[0] = XEMBED_VERSION;
11322 data[1] = flags;
11324 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11325 dpyinfo->Xatom_XEMBED_INFO, dpyinfo->Xatom_XEMBED_INFO,
11326 32, PropModeReplace, (unsigned char *) data, 2);
11328 #endif /* defined USE_X_TOOLKIT || ! defined USE_GTK */
11330 static void
11331 xembed_send_message (struct frame *f, Time t, enum xembed_message msg,
11332 long int detail, long int data1, long int data2)
11334 XEvent event;
11336 event.xclient.type = ClientMessage;
11337 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
11338 event.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_XEMBED;
11339 event.xclient.format = 32;
11340 event.xclient.data.l[0] = t;
11341 event.xclient.data.l[1] = msg;
11342 event.xclient.data.l[2] = detail;
11343 event.xclient.data.l[3] = data1;
11344 event.xclient.data.l[4] = data2;
11346 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
11347 False, NoEventMask, &event);
11348 XSync (FRAME_X_DISPLAY (f), False);
11351 /* Change of visibility. */
11353 /* This function sends the request to make the frame visible, but may
11354 return before it the frame's visibility is changed. */
11356 void
11357 x_make_frame_visible (struct frame *f)
11359 if (FRAME_PARENT_FRAME (f))
11361 if (!FRAME_VISIBLE_P (f))
11363 block_input ();
11364 #ifdef USE_GTK
11365 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
11366 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11367 f->left_pos, f->top_pos);
11368 #else
11369 XMapRaised (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
11370 #endif
11371 unblock_input ();
11373 SET_FRAME_VISIBLE (f, true);
11374 SET_FRAME_ICONIFIED (f, false);
11376 return;
11379 block_input ();
11381 x_set_bitmap_icon (f);
11383 if (! FRAME_VISIBLE_P (f))
11385 /* We test asked_for_visible here to make sure we don't
11386 call x_set_offset a second time
11387 if we get to x_make_frame_visible a second time
11388 before the window gets really visible. */
11389 if (! FRAME_ICONIFIED_P (f)
11390 && ! FRAME_X_EMBEDDED_P (f)
11391 && ! f->output_data.x->asked_for_visible)
11392 x_set_offset (f, f->left_pos, f->top_pos, 0);
11394 f->output_data.x->asked_for_visible = true;
11396 if (! EQ (Vx_no_window_manager, Qt))
11397 x_wm_set_window_state (f, NormalState);
11398 #ifdef USE_X_TOOLKIT
11399 if (FRAME_X_EMBEDDED_P (f))
11400 xembed_set_info (f, XEMBED_MAPPED);
11401 else
11403 /* This was XtPopup, but that did nothing for an iconified frame. */
11404 XtMapWidget (f->output_data.x->widget);
11406 #else /* not USE_X_TOOLKIT */
11407 #ifdef USE_GTK
11408 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
11409 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
11410 #else
11411 if (FRAME_X_EMBEDDED_P (f))
11412 xembed_set_info (f, XEMBED_MAPPED);
11413 else
11414 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11415 #endif /* not USE_GTK */
11416 #endif /* not USE_X_TOOLKIT */
11419 XFlush (FRAME_X_DISPLAY (f));
11421 /* Synchronize to ensure Emacs knows the frame is visible
11422 before we do anything else. We do this loop with input not blocked
11423 so that incoming events are handled. */
11425 /* This must be before UNBLOCK_INPUT
11426 since events that arrive in response to the actions above
11427 will set it when they are handled. */
11428 bool previously_visible = f->output_data.x->has_been_visible;
11430 int original_left = f->left_pos;
11431 int original_top = f->top_pos;
11433 /* This must come after we set COUNT. */
11434 unblock_input ();
11436 /* We unblock here so that arriving X events are processed. */
11438 /* Now move the window back to where it was "supposed to be".
11439 But don't do it if the gravity is negative.
11440 When the gravity is negative, this uses a position
11441 that is 3 pixels too low. Perhaps that's really the border width.
11443 Don't do this if the window has never been visible before,
11444 because the window manager may choose the position
11445 and we don't want to override it. */
11447 if (!FRAME_VISIBLE_P (f)
11448 && !FRAME_ICONIFIED_P (f)
11449 && !FRAME_X_EMBEDDED_P (f)
11450 && !FRAME_PARENT_FRAME (f)
11451 && f->win_gravity == NorthWestGravity
11452 && previously_visible)
11454 Drawable rootw;
11455 int x, y;
11456 unsigned int width, height, border, depth;
11458 block_input ();
11460 /* On some window managers (such as FVWM) moving an existing
11461 window, even to the same place, causes the window manager
11462 to introduce an offset. This can cause the window to move
11463 to an unexpected location. Check the geometry (a little
11464 slow here) and then verify that the window is in the right
11465 place. If the window is not in the right place, move it
11466 there, and take the potential window manager hit. */
11467 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11468 &rootw, &x, &y, &width, &height, &border, &depth);
11470 if (original_left != x || original_top != y)
11471 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11472 original_left, original_top);
11474 unblock_input ();
11479 /* Change from mapped state to withdrawn state. */
11481 /* Make the frame visible (mapped and not iconified). */
11483 void
11484 x_make_frame_invisible (struct frame *f)
11486 Window window;
11488 /* Use the frame's outermost window, not the one we normally draw on. */
11489 window = FRAME_OUTER_WINDOW (f);
11491 /* Don't keep the highlight on an invisible frame. */
11492 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
11493 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
11495 block_input ();
11497 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11498 that the current position of the window is user-specified, rather than
11499 program-specified, so that when the window is mapped again, it will be
11500 placed at the same location, without forcing the user to position it
11501 by hand again (they have already done that once for this window.) */
11502 x_wm_set_size_hint (f, 0, true);
11504 #ifdef USE_GTK
11505 if (FRAME_GTK_OUTER_WIDGET (f))
11506 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
11507 else
11508 #else
11509 if (FRAME_X_EMBEDDED_P (f))
11510 xembed_set_info (f, 0);
11511 else
11512 #endif
11514 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11515 DefaultScreen (FRAME_X_DISPLAY (f))))
11517 unblock_input ();
11518 error ("Can't notify window manager of window withdrawal");
11521 x_sync (f);
11523 /* We can't distinguish this from iconification
11524 just by the event that we get from the server.
11525 So we can't win using the usual strategy of letting
11526 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11527 and synchronize with the server to make sure we agree. */
11528 SET_FRAME_VISIBLE (f, 0);
11529 SET_FRAME_ICONIFIED (f, false);
11531 unblock_input ();
11534 /* Change window state from mapped to iconified. */
11536 void
11537 x_iconify_frame (struct frame *f)
11539 #ifdef USE_X_TOOLKIT
11540 int result;
11541 #endif
11543 /* Don't keep the highlight on an invisible frame. */
11544 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
11545 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
11547 if (FRAME_ICONIFIED_P (f))
11548 return;
11550 block_input ();
11552 x_set_bitmap_icon (f);
11554 #if defined (USE_GTK)
11555 if (FRAME_GTK_OUTER_WIDGET (f))
11557 if (! FRAME_VISIBLE_P (f))
11558 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
11560 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
11561 SET_FRAME_VISIBLE (f, 0);
11562 SET_FRAME_ICONIFIED (f, true);
11563 unblock_input ();
11564 return;
11566 #endif
11568 #ifdef USE_X_TOOLKIT
11570 if (! FRAME_VISIBLE_P (f))
11572 if (! EQ (Vx_no_window_manager, Qt))
11573 x_wm_set_window_state (f, IconicState);
11574 /* This was XtPopup, but that did nothing for an iconified frame. */
11575 XtMapWidget (f->output_data.x->widget);
11576 /* The server won't give us any event to indicate
11577 that an invisible frame was changed to an icon,
11578 so we have to record it here. */
11579 SET_FRAME_VISIBLE (f, 0);
11580 SET_FRAME_ICONIFIED (f, true);
11581 unblock_input ();
11582 return;
11585 result = XIconifyWindow (FRAME_X_DISPLAY (f),
11586 XtWindow (f->output_data.x->widget),
11587 DefaultScreen (FRAME_X_DISPLAY (f)));
11588 unblock_input ();
11590 if (!result)
11591 error ("Can't notify window manager of iconification");
11593 SET_FRAME_ICONIFIED (f, true);
11594 SET_FRAME_VISIBLE (f, 0);
11596 block_input ();
11597 XFlush (FRAME_X_DISPLAY (f));
11598 unblock_input ();
11599 #else /* not USE_X_TOOLKIT */
11601 /* Make sure the X server knows where the window should be positioned,
11602 in case the user deiconifies with the window manager. */
11603 if (! FRAME_VISIBLE_P (f)
11604 && ! FRAME_ICONIFIED_P (f)
11605 && ! FRAME_X_EMBEDDED_P (f))
11606 x_set_offset (f, f->left_pos, f->top_pos, 0);
11608 /* Since we don't know which revision of X we're running, we'll use both
11609 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11611 /* X11R4: send a ClientMessage to the window manager using the
11612 WM_CHANGE_STATE type. */
11614 XEvent msg;
11616 msg.xclient.window = FRAME_X_WINDOW (f);
11617 msg.xclient.type = ClientMessage;
11618 msg.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_wm_change_state;
11619 msg.xclient.format = 32;
11620 msg.xclient.data.l[0] = IconicState;
11622 if (! XSendEvent (FRAME_X_DISPLAY (f),
11623 DefaultRootWindow (FRAME_X_DISPLAY (f)),
11624 False,
11625 SubstructureRedirectMask | SubstructureNotifyMask,
11626 &msg))
11628 unblock_input ();
11629 error ("Can't notify window manager of iconification");
11633 /* X11R3: set the initial_state field of the window manager hints to
11634 IconicState. */
11635 x_wm_set_window_state (f, IconicState);
11637 if (!FRAME_VISIBLE_P (f))
11639 /* If the frame was withdrawn, before, we must map it. */
11640 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11643 SET_FRAME_ICONIFIED (f, true);
11644 SET_FRAME_VISIBLE (f, 0);
11646 XFlush (FRAME_X_DISPLAY (f));
11647 unblock_input ();
11648 #endif /* not USE_X_TOOLKIT */
11652 /* Free X resources of frame F. */
11654 void
11655 x_free_frame_resources (struct frame *f)
11657 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11658 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
11659 #ifdef USE_X_TOOLKIT
11660 Lisp_Object bar;
11661 struct scroll_bar *b;
11662 #endif
11664 block_input ();
11666 /* If a display connection is dead, don't try sending more
11667 commands to the X server. */
11668 if (dpyinfo->display)
11670 /* Always exit with visible pointer to avoid weird issue
11671 with Xfixes (Bug#17609). */
11672 if (f->pointer_invisible)
11673 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, 0);
11675 /* We must free faces before destroying windows because some
11676 font-driver (e.g. xft) access a window while finishing a
11677 face. */
11678 free_frame_faces (f);
11679 tear_down_x_back_buffer (f);
11681 if (f->output_data.x->icon_desc)
11682 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
11684 #ifdef USE_X_TOOLKIT
11685 /* Explicitly destroy the scroll bars of the frame. Without
11686 this, we get "BadDrawable" errors from the toolkit later on,
11687 presumably from expose events generated for the disappearing
11688 toolkit scroll bars. */
11689 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
11691 b = XSCROLL_BAR (bar);
11692 x_scroll_bar_remove (b);
11694 #endif
11696 #ifdef HAVE_X_I18N
11697 if (FRAME_XIC (f))
11698 free_frame_xic (f);
11699 #endif
11701 x_free_cr_resources (f);
11702 #ifdef USE_X_TOOLKIT
11703 if (f->output_data.x->widget)
11705 XtDestroyWidget (f->output_data.x->widget);
11706 f->output_data.x->widget = NULL;
11708 /* Tooltips don't have widgets, only a simple X window, even if
11709 we are using a toolkit. */
11710 else if (FRAME_X_WINDOW (f))
11711 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11713 free_frame_menubar (f);
11715 if (f->shell_position)
11716 xfree (f->shell_position);
11717 #else /* !USE_X_TOOLKIT */
11719 #ifdef USE_GTK
11720 xg_free_frame_widgets (f);
11721 #endif /* USE_GTK */
11723 tear_down_x_back_buffer (f);
11724 if (FRAME_X_WINDOW (f))
11725 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11726 #endif /* !USE_X_TOOLKIT */
11728 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
11729 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
11730 unload_color (f, f->output_data.x->cursor_pixel);
11731 unload_color (f, f->output_data.x->cursor_foreground_pixel);
11732 unload_color (f, f->output_data.x->border_pixel);
11733 unload_color (f, f->output_data.x->mouse_pixel);
11735 if (f->output_data.x->scroll_bar_background_pixel != -1)
11736 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
11737 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
11738 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
11739 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
11740 /* Scrollbar shadow colors. */
11741 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
11742 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
11743 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
11744 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
11745 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
11746 if (f->output_data.x->white_relief.pixel != -1)
11747 unload_color (f, f->output_data.x->white_relief.pixel);
11748 if (f->output_data.x->black_relief.pixel != -1)
11749 unload_color (f, f->output_data.x->black_relief.pixel);
11751 x_free_gcs (f);
11753 /* Free extra GCs allocated by x_setup_relief_colors. */
11754 if (f->output_data.x->white_relief.gc)
11756 XFreeGC (dpyinfo->display, f->output_data.x->white_relief.gc);
11757 f->output_data.x->white_relief.gc = 0;
11759 if (f->output_data.x->black_relief.gc)
11761 XFreeGC (dpyinfo->display, f->output_data.x->black_relief.gc);
11762 f->output_data.x->black_relief.gc = 0;
11765 /* Free cursors. */
11766 if (f->output_data.x->text_cursor != 0)
11767 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->text_cursor);
11768 if (f->output_data.x->nontext_cursor != 0)
11769 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->nontext_cursor);
11770 if (f->output_data.x->modeline_cursor != 0)
11771 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->modeline_cursor);
11772 if (f->output_data.x->hand_cursor != 0)
11773 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hand_cursor);
11774 if (f->output_data.x->hourglass_cursor != 0)
11775 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hourglass_cursor);
11776 if (f->output_data.x->horizontal_drag_cursor != 0)
11777 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->horizontal_drag_cursor);
11778 if (f->output_data.x->vertical_drag_cursor != 0)
11779 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->vertical_drag_cursor);
11780 if (f->output_data.x->left_edge_cursor != 0)
11781 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->left_edge_cursor);
11782 if (f->output_data.x->top_left_corner_cursor != 0)
11783 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->top_left_corner_cursor);
11784 if (f->output_data.x->top_edge_cursor != 0)
11785 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->top_edge_cursor);
11786 if (f->output_data.x->top_right_corner_cursor != 0)
11787 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->top_right_corner_cursor);
11788 if (f->output_data.x->right_edge_cursor != 0)
11789 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->right_edge_cursor);
11790 if (f->output_data.x->bottom_right_corner_cursor != 0)
11791 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->bottom_right_corner_cursor);
11792 if (f->output_data.x->bottom_edge_cursor != 0)
11793 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->bottom_edge_cursor);
11794 if (f->output_data.x->bottom_left_corner_cursor != 0)
11795 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->bottom_left_corner_cursor);
11797 XFlush (FRAME_X_DISPLAY (f));
11800 xfree (f->output_data.x->saved_menu_event);
11801 xfree (f->output_data.x);
11802 f->output_data.x = NULL;
11804 if (f == dpyinfo->x_focus_frame)
11805 dpyinfo->x_focus_frame = 0;
11806 if (f == dpyinfo->x_focus_event_frame)
11807 dpyinfo->x_focus_event_frame = 0;
11808 if (f == dpyinfo->x_highlight_frame)
11809 dpyinfo->x_highlight_frame = 0;
11810 if (f == hlinfo->mouse_face_mouse_frame)
11811 reset_mouse_highlight (hlinfo);
11813 unblock_input ();
11817 /* Destroy the X window of frame F. */
11819 static void
11820 x_destroy_window (struct frame *f)
11822 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11824 /* If a display connection is dead, don't try sending more
11825 commands to the X server. */
11826 if (dpyinfo->display != 0)
11827 x_free_frame_resources (f);
11829 dpyinfo->reference_count--;
11833 /* Setting window manager hints. */
11835 /* Set the normal size hints for the window manager, for frame F.
11836 FLAGS is the flags word to use--or 0 meaning preserve the flags
11837 that the window now has.
11838 If USER_POSITION, set the USPosition
11839 flag (this is useful when FLAGS is 0).
11840 The GTK version is in gtkutils.c. */
11842 #ifndef USE_GTK
11843 void
11844 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
11846 XSizeHints size_hints;
11847 Window window = FRAME_OUTER_WINDOW (f);
11849 if (!window)
11850 return;
11852 #ifdef USE_X_TOOLKIT
11853 if (f->output_data.x->widget)
11855 widget_update_wm_size_hints (f->output_data.x->widget);
11856 return;
11858 #endif
11860 /* Setting PMaxSize caused various problems. */
11861 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
11863 size_hints.x = f->left_pos;
11864 size_hints.y = f->top_pos;
11866 size_hints.width = FRAME_PIXEL_WIDTH (f);
11867 size_hints.height = FRAME_PIXEL_HEIGHT (f);
11869 size_hints.width_inc = frame_resize_pixelwise ? 1 : FRAME_COLUMN_WIDTH (f);
11870 size_hints.height_inc = frame_resize_pixelwise ? 1 : FRAME_LINE_HEIGHT (f);
11872 size_hints.max_width = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
11873 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
11874 size_hints.max_height = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
11875 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
11877 /* Calculate the base and minimum sizes. */
11879 int base_width, base_height;
11881 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
11882 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
11884 /* The window manager uses the base width hints to calculate the
11885 current number of rows and columns in the frame while
11886 resizing; min_width and min_height aren't useful for this
11887 purpose, since they might not give the dimensions for a
11888 zero-row, zero-column frame. */
11890 size_hints.flags |= PBaseSize;
11891 size_hints.base_width = base_width;
11892 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
11893 size_hints.min_width = base_width;
11894 size_hints.min_height = base_height;
11897 /* If we don't need the old flags, we don't need the old hint at all. */
11898 if (flags)
11900 size_hints.flags |= flags;
11901 goto no_read;
11905 XSizeHints hints; /* Sometimes I hate X Windows... */
11906 long supplied_return;
11907 int value;
11909 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
11910 &supplied_return);
11912 if (flags)
11913 size_hints.flags |= flags;
11914 else
11916 if (value == 0)
11917 hints.flags = 0;
11918 if (hints.flags & PSize)
11919 size_hints.flags |= PSize;
11920 if (hints.flags & PPosition)
11921 size_hints.flags |= PPosition;
11922 if (hints.flags & USPosition)
11923 size_hints.flags |= USPosition;
11924 if (hints.flags & USSize)
11925 size_hints.flags |= USSize;
11929 no_read:
11931 #ifdef PWinGravity
11932 size_hints.win_gravity = f->win_gravity;
11933 size_hints.flags |= PWinGravity;
11935 if (user_position)
11937 size_hints.flags &= ~ PPosition;
11938 size_hints.flags |= USPosition;
11940 #endif /* PWinGravity */
11942 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
11944 #endif /* not USE_GTK */
11946 /* Used for IconicState or NormalState */
11948 static void
11949 x_wm_set_window_state (struct frame *f, int state)
11951 #ifdef USE_X_TOOLKIT
11952 Arg al[1];
11954 XtSetArg (al[0], XtNinitialState, state);
11955 XtSetValues (f->output_data.x->widget, al, 1);
11956 #else /* not USE_X_TOOLKIT */
11957 Window window = FRAME_X_WINDOW (f);
11959 f->output_data.x->wm_hints.flags |= StateHint;
11960 f->output_data.x->wm_hints.initial_state = state;
11962 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
11963 #endif /* not USE_X_TOOLKIT */
11966 static void
11967 x_wm_set_icon_pixmap (struct frame *f, ptrdiff_t pixmap_id)
11969 Pixmap icon_pixmap, icon_mask;
11971 #if !defined USE_X_TOOLKIT && !defined USE_GTK
11972 Window window = FRAME_OUTER_WINDOW (f);
11973 #endif
11975 if (pixmap_id > 0)
11977 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
11978 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
11979 icon_mask = x_bitmap_mask (f, pixmap_id);
11980 f->output_data.x->wm_hints.icon_mask = icon_mask;
11982 else
11984 /* It seems there is no way to turn off use of an icon
11985 pixmap. */
11986 return;
11990 #ifdef USE_GTK
11992 xg_set_frame_icon (f, icon_pixmap, icon_mask);
11993 return;
11996 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
11999 Arg al[1];
12000 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12001 XtSetValues (f->output_data.x->widget, al, 1);
12002 XtSetArg (al[0], XtNiconMask, icon_mask);
12003 XtSetValues (f->output_data.x->widget, al, 1);
12006 #else /* not USE_X_TOOLKIT && not USE_GTK */
12008 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
12009 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12011 #endif /* not USE_X_TOOLKIT && not USE_GTK */
12014 void
12015 x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
12017 Window window = FRAME_OUTER_WINDOW (f);
12019 f->output_data.x->wm_hints.flags |= IconPositionHint;
12020 f->output_data.x->wm_hints.icon_x = icon_x;
12021 f->output_data.x->wm_hints.icon_y = icon_y;
12023 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12027 /***********************************************************************
12028 Fonts
12029 ***********************************************************************/
12031 #ifdef GLYPH_DEBUG
12033 /* Check that FONT is valid on frame F. It is if it can be found in F's
12034 font table. */
12036 static void
12037 x_check_font (struct frame *f, struct font *font)
12039 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
12040 if (font->driver->check)
12041 eassert (font->driver->check (f, font) == 0);
12044 #endif /* GLYPH_DEBUG */
12047 /***********************************************************************
12048 Initialization
12049 ***********************************************************************/
12051 #ifdef USE_X_TOOLKIT
12052 static XrmOptionDescRec emacs_options[] = {
12053 {(char *) "-geometry", (char *) ".geometry", XrmoptionSepArg, NULL},
12054 {(char *) "-iconic", (char *) ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
12056 {(char *) "-internal-border-width",
12057 (char *) "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
12058 {(char *) "-ib", (char *) "*EmacsScreen.internalBorderWidth",
12059 XrmoptionSepArg, NULL},
12060 {(char *) "-T", (char *) "*EmacsShell.title", XrmoptionSepArg, NULL},
12061 {(char *) "-wn", (char *) "*EmacsShell.title", XrmoptionSepArg, NULL},
12062 {(char *) "-title", (char *) "*EmacsShell.title", XrmoptionSepArg, NULL},
12063 {(char *) "-iconname", (char *) "*EmacsShell.iconName",
12064 XrmoptionSepArg, NULL},
12065 {(char *) "-in", (char *) "*EmacsShell.iconName", XrmoptionSepArg, NULL},
12066 {(char *) "-mc", (char *) "*pointerColor", XrmoptionSepArg, NULL},
12067 {(char *) "-cr", (char *) "*cursorColor", XrmoptionSepArg, NULL}
12070 /* Whether atimer for Xt timeouts is activated or not. */
12072 static bool x_timeout_atimer_activated_flag;
12074 #endif /* USE_X_TOOLKIT */
12076 static int x_initialized;
12078 /* Test whether two display-name strings agree up to the dot that separates
12079 the screen number from the server number. */
12080 static bool
12081 same_x_server (const char *name1, const char *name2)
12083 bool seen_colon = false;
12084 Lisp_Object sysname = Fsystem_name ();
12085 const char *system_name = SSDATA (sysname);
12086 ptrdiff_t system_name_length = SBYTES (sysname);
12087 ptrdiff_t length_until_period = 0;
12089 while (system_name[length_until_period] != 0
12090 && system_name[length_until_period] != '.')
12091 length_until_period++;
12093 /* Treat `unix' like an empty host name. */
12094 if (! strncmp (name1, "unix:", 5))
12095 name1 += 4;
12096 if (! strncmp (name2, "unix:", 5))
12097 name2 += 4;
12098 /* Treat this host's name like an empty host name. */
12099 if (! strncmp (name1, system_name, system_name_length)
12100 && name1[system_name_length] == ':')
12101 name1 += system_name_length;
12102 if (! strncmp (name2, system_name, system_name_length)
12103 && name2[system_name_length] == ':')
12104 name2 += system_name_length;
12105 /* Treat this host's domainless name like an empty host name. */
12106 if (! strncmp (name1, system_name, length_until_period)
12107 && name1[length_until_period] == ':')
12108 name1 += length_until_period;
12109 if (! strncmp (name2, system_name, length_until_period)
12110 && name2[length_until_period] == ':')
12111 name2 += length_until_period;
12113 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
12115 if (*name1 == ':')
12116 seen_colon = true;
12117 if (seen_colon && *name1 == '.')
12118 return true;
12120 return (seen_colon
12121 && (*name1 == '.' || *name1 == '\0')
12122 && (*name2 == '.' || *name2 == '\0'));
12125 /* Count number of set bits in mask and number of bits to shift to
12126 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
12127 to 5. */
12128 static void
12129 get_bits_and_offset (unsigned long mask, int *bits, int *offset)
12131 int nr = 0;
12132 int off = 0;
12134 while (!(mask & 1))
12136 off++;
12137 mask >>= 1;
12140 while (mask & 1)
12142 nr++;
12143 mask >>= 1;
12146 *offset = off;
12147 *bits = nr;
12150 /* Return true iff display DISPLAY is available for use.
12151 But don't permanently open it, just test its availability. */
12153 bool
12154 x_display_ok (const char *display)
12156 /* XOpenDisplay fails if it gets a signal. Block SIGIO which may arrive. */
12157 unrequest_sigio ();
12158 Display *dpy = XOpenDisplay (display);
12159 request_sigio ();
12160 if (!dpy)
12161 return false;
12162 XCloseDisplay (dpy);
12163 return true;
12166 #ifdef USE_GTK
12167 static void
12168 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
12169 const gchar *msg, gpointer user_data)
12171 if (!strstr (msg, "g_set_prgname"))
12172 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, msg);
12174 #endif
12176 /* Create invisible cursor on X display referred by DPYINFO. */
12178 static Cursor
12179 make_invisible_cursor (struct x_display_info *dpyinfo)
12181 Display *dpy = dpyinfo->display;
12182 static char const no_data[] = { 0 };
12183 Pixmap pix;
12184 XColor col;
12185 Cursor c = 0;
12187 x_catch_errors (dpy);
12188 pix = XCreateBitmapFromData (dpy, dpyinfo->root_window, no_data, 1, 1);
12189 if (! x_had_errors_p (dpy) && pix != None)
12191 Cursor pixc;
12192 col.pixel = 0;
12193 col.red = col.green = col.blue = 0;
12194 col.flags = DoRed | DoGreen | DoBlue;
12195 pixc = XCreatePixmapCursor (dpy, pix, pix, &col, &col, 0, 0);
12196 if (! x_had_errors_p (dpy) && pixc != None)
12197 c = pixc;
12198 XFreePixmap (dpy, pix);
12201 x_uncatch_errors ();
12203 return c;
12206 /* True if DPY supports Xfixes extension >= 4. */
12208 static bool
12209 x_probe_xfixes_extension (Display *dpy)
12211 #ifdef HAVE_XFIXES
12212 int major, minor;
12213 return XFixesQueryVersion (dpy, &major, &minor) && major >= 4;
12214 #else
12215 return false;
12216 #endif /* HAVE_XFIXES */
12219 /* Toggle mouse pointer visibility on frame F by using Xfixes functions. */
12221 static void
12222 xfixes_toggle_visible_pointer (struct frame *f, bool invisible)
12224 #ifdef HAVE_XFIXES
12225 if (invisible)
12226 XFixesHideCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12227 else
12228 XFixesShowCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12229 f->pointer_invisible = invisible;
12230 #else
12231 emacs_abort ();
12232 #endif /* HAVE_XFIXES */
12235 /* Toggle mouse pointer visibility on frame F by using invisible cursor. */
12237 static void
12238 x_toggle_visible_pointer (struct frame *f, bool invisible)
12240 eassert (FRAME_DISPLAY_INFO (f)->invisible_cursor != 0);
12241 if (invisible)
12242 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12243 FRAME_DISPLAY_INFO (f)->invisible_cursor);
12244 else
12245 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12246 f->output_data.x->current_cursor);
12247 f->pointer_invisible = invisible;
12250 /* Setup pointer blanking, prefer Xfixes if available. */
12252 static void
12253 x_setup_pointer_blanking (struct x_display_info *dpyinfo)
12255 /* FIXME: the brave tester should set EMACS_XFIXES because we're suspecting
12256 X server bug, see http://debbugs.gnu.org/cgi/bugreport.cgi?bug=17609. */
12257 if (egetenv ("EMACS_XFIXES") && x_probe_xfixes_extension (dpyinfo->display))
12258 dpyinfo->toggle_visible_pointer = xfixes_toggle_visible_pointer;
12259 else
12261 dpyinfo->toggle_visible_pointer = x_toggle_visible_pointer;
12262 dpyinfo->invisible_cursor = make_invisible_cursor (dpyinfo);
12266 /* Current X display connection identifier. Incremented for each next
12267 connection established. */
12268 static unsigned x_display_id;
12270 /* Open a connection to X display DISPLAY_NAME, and return
12271 the structure that describes the open display.
12272 If we cannot contact the display, return null. */
12274 struct x_display_info *
12275 x_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
12277 Display *dpy;
12278 struct terminal *terminal;
12279 struct x_display_info *dpyinfo;
12280 XrmDatabase xrdb;
12281 #ifdef USE_XCB
12282 xcb_connection_t *xcb_conn;
12283 #endif
12285 block_input ();
12287 if (!x_initialized)
12289 x_initialize ();
12290 ++x_initialized;
12293 if (! x_display_ok (SSDATA (display_name)))
12294 error ("Display %s can't be opened", SSDATA (display_name));
12296 #ifdef USE_GTK
12298 #define NUM_ARGV 10
12299 int argc;
12300 char *argv[NUM_ARGV];
12301 char **argv2 = argv;
12302 guint id;
12304 if (x_initialized++ > 1)
12306 xg_display_open (SSDATA (display_name), &dpy);
12308 else
12310 static char display_opt[] = "--display";
12311 static char name_opt[] = "--name";
12313 for (argc = 0; argc < NUM_ARGV; ++argc)
12314 argv[argc] = 0;
12316 argc = 0;
12317 argv[argc++] = initial_argv[0];
12319 if (! NILP (display_name))
12321 argv[argc++] = display_opt;
12322 argv[argc++] = SSDATA (display_name);
12325 argv[argc++] = name_opt;
12326 argv[argc++] = resource_name;
12328 XSetLocaleModifiers ("");
12330 /* Work around GLib bug that outputs a faulty warning. See
12331 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
12332 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
12333 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
12335 /* NULL window -> events for all windows go to our function.
12336 Call before gtk_init so Gtk+ event filters comes after our. */
12337 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
12339 /* gtk_init does set_locale. Fix locale before and after. */
12340 fixup_locale ();
12341 unrequest_sigio (); /* See comment in x_display_ok. */
12342 gtk_init (&argc, &argv2);
12343 request_sigio ();
12344 fixup_locale ();
12346 g_log_remove_handler ("GLib", id);
12348 xg_initialize ();
12350 dpy = DEFAULT_GDK_DISPLAY ();
12352 #if ! GTK_CHECK_VERSION (2, 90, 0)
12353 /* Load our own gtkrc if it exists. */
12355 const char *file = "~/.emacs.d/gtkrc";
12356 Lisp_Object s, abs_file;
12358 s = build_string (file);
12359 abs_file = Fexpand_file_name (s, Qnil);
12361 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
12362 gtk_rc_parse (SSDATA (abs_file));
12364 #endif
12366 XSetErrorHandler (x_error_handler);
12367 XSetIOErrorHandler (x_io_error_quitter);
12370 #else /* not USE_GTK */
12371 #ifdef USE_X_TOOLKIT
12372 /* weiner@footloose.sps.mot.com reports that this causes
12373 errors with X11R5:
12374 X protocol error: BadAtom (invalid Atom parameter)
12375 on protocol request 18skiloaf.
12376 So let's not use it until R6. */
12377 #ifdef HAVE_X11XTR6
12378 XtSetLanguageProc (NULL, NULL, NULL);
12379 #endif
12382 int argc = 0;
12383 char *argv[3];
12385 argv[0] = (char *) "";
12386 argc = 1;
12387 if (xrm_option)
12389 argv[argc++] = (char *) "-xrm";
12390 argv[argc++] = xrm_option;
12392 turn_on_atimers (false);
12393 unrequest_sigio (); /* See comment in x_display_ok. */
12394 dpy = XtOpenDisplay (Xt_app_con, SSDATA (display_name),
12395 resource_name, EMACS_CLASS,
12396 emacs_options, XtNumber (emacs_options),
12397 &argc, argv);
12398 request_sigio ();
12399 turn_on_atimers (true);
12401 #ifdef HAVE_X11XTR6
12402 /* I think this is to compensate for XtSetLanguageProc. */
12403 fixup_locale ();
12404 #endif
12407 #else /* not USE_X_TOOLKIT */
12408 XSetLocaleModifiers ("");
12409 unrequest_sigio (); /* See comment in x_display_ok. */
12410 dpy = XOpenDisplay (SSDATA (display_name));
12411 request_sigio ();
12412 #endif /* not USE_X_TOOLKIT */
12413 #endif /* not USE_GTK*/
12415 /* Detect failure. */
12416 if (dpy == 0)
12418 unblock_input ();
12419 return 0;
12422 #ifdef USE_XCB
12423 xcb_conn = XGetXCBConnection (dpy);
12424 if (xcb_conn == 0)
12426 #ifdef USE_GTK
12427 xg_display_close (dpy);
12428 #else
12429 #ifdef USE_X_TOOLKIT
12430 XtCloseDisplay (dpy);
12431 #else
12432 XCloseDisplay (dpy);
12433 #endif
12434 #endif /* ! USE_GTK */
12436 unblock_input ();
12437 return 0;
12439 #endif
12441 /* We have definitely succeeded. Record the new connection. */
12443 dpyinfo = xzalloc (sizeof *dpyinfo);
12444 terminal = x_create_terminal (dpyinfo);
12447 struct x_display_info *share;
12449 for (share = x_display_list; share; share = share->next)
12450 if (same_x_server (SSDATA (XCAR (share->name_list_element)),
12451 SSDATA (display_name)))
12452 break;
12453 if (share)
12454 terminal->kboard = share->terminal->kboard;
12455 else
12457 terminal->kboard = allocate_kboard (Qx);
12459 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
12461 char *vendor = ServerVendor (dpy);
12463 /* Temporarily hide the partially initialized terminal. */
12464 terminal_list = terminal->next_terminal;
12465 unblock_input ();
12466 kset_system_key_alist
12467 (terminal->kboard,
12468 call1 (Qvendor_specific_keysyms,
12469 vendor ? build_string (vendor) : empty_unibyte_string));
12470 block_input ();
12471 terminal->next_terminal = terminal_list;
12472 terminal_list = terminal;
12475 /* Don't let the initial kboard remain current longer than necessary.
12476 That would cause problems if a file loaded on startup tries to
12477 prompt in the mini-buffer. */
12478 if (current_kboard == initial_kboard)
12479 current_kboard = terminal->kboard;
12481 terminal->kboard->reference_count++;
12484 /* Put this display on the chain. */
12485 dpyinfo->next = x_display_list;
12486 x_display_list = dpyinfo;
12488 dpyinfo->name_list_element = Fcons (display_name, Qnil);
12489 dpyinfo->display = dpy;
12490 dpyinfo->connection = ConnectionNumber (dpyinfo->display);
12491 #ifdef USE_XCB
12492 dpyinfo->xcb_connection = xcb_conn;
12493 #endif
12495 /* http://lists.gnu.org/archive/html/emacs-devel/2015-11/msg00194.html */
12496 dpyinfo->smallest_font_height = 1;
12497 dpyinfo->smallest_char_width = 1;
12499 /* Set the name of the terminal. */
12500 terminal->name = xlispstrdup (display_name);
12502 #if false
12503 XSetAfterFunction (x_current_display, x_trace_wire);
12504 #endif
12506 Lisp_Object system_name = Fsystem_name ();
12507 ptrdiff_t nbytes;
12508 if (INT_ADD_WRAPV (SBYTES (Vinvocation_name), SBYTES (system_name) + 2,
12509 &nbytes))
12510 memory_full (SIZE_MAX);
12511 dpyinfo->x_id = ++x_display_id;
12512 dpyinfo->x_id_name = xmalloc (nbytes);
12513 char *nametail = lispstpcpy (dpyinfo->x_id_name, Vinvocation_name);
12514 *nametail++ = '@';
12515 lispstpcpy (nametail, system_name);
12517 /* Figure out which modifier bits mean what. */
12518 x_find_modifier_meanings (dpyinfo);
12520 /* Get the scroll bar cursor. */
12521 #ifdef USE_GTK
12522 /* We must create a GTK cursor, it is required for GTK widgets. */
12523 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
12524 #endif /* USE_GTK */
12526 dpyinfo->vertical_scroll_bar_cursor
12527 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
12529 dpyinfo->horizontal_scroll_bar_cursor
12530 = XCreateFontCursor (dpyinfo->display, XC_sb_h_double_arrow);
12532 xrdb = x_load_resources (dpyinfo->display, xrm_option,
12533 resource_name, EMACS_CLASS);
12534 #ifdef HAVE_XRMSETDATABASE
12535 XrmSetDatabase (dpyinfo->display, xrdb);
12536 #else
12537 dpyinfo->display->db = xrdb;
12538 #endif
12539 /* Put the rdb where we can find it in a way that works on
12540 all versions. */
12541 dpyinfo->xrdb = xrdb;
12543 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
12544 DefaultScreen (dpyinfo->display));
12545 select_visual (dpyinfo);
12546 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
12547 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
12548 dpyinfo->icon_bitmap_id = -1;
12549 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
12551 reset_mouse_highlight (&dpyinfo->mouse_highlight);
12553 /* See if we can construct pixel values from RGB values. */
12554 if (dpyinfo->visual->class == TrueColor)
12556 get_bits_and_offset (dpyinfo->visual->red_mask,
12557 &dpyinfo->red_bits, &dpyinfo->red_offset);
12558 get_bits_and_offset (dpyinfo->visual->blue_mask,
12559 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
12560 get_bits_and_offset (dpyinfo->visual->green_mask,
12561 &dpyinfo->green_bits, &dpyinfo->green_offset);
12564 /* See if a private colormap is requested. */
12565 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
12567 if (dpyinfo->visual->class == PseudoColor)
12569 AUTO_STRING (privateColormap, "privateColormap");
12570 AUTO_STRING (PrivateColormap, "PrivateColormap");
12571 Lisp_Object value
12572 = display_x_get_resource (dpyinfo, privateColormap,
12573 PrivateColormap, Qnil, Qnil);
12574 if (STRINGP (value)
12575 && (!strcmp (SSDATA (value), "true")
12576 || !strcmp (SSDATA (value), "on")))
12577 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
12580 else
12581 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
12582 dpyinfo->visual, AllocNone);
12584 #ifdef HAVE_XDBE
12585 dpyinfo->supports_xdbe = false;
12586 int xdbe_major;
12587 int xdbe_minor;
12588 if (XdbeQueryExtension (dpyinfo->display, &xdbe_major, &xdbe_minor))
12589 dpyinfo->supports_xdbe = true;
12590 #endif
12592 #ifdef HAVE_XFT
12594 /* If we are using Xft, the following precautions should be made:
12596 1. Make sure that the Xrender extension is added before the Xft one.
12597 Otherwise, the close-display hook set by Xft is called after the one
12598 for Xrender, and the former tries to re-add the latter. This results
12599 in inconsistency of internal states and leads to X protocol error when
12600 one reconnects to the same X server (Bug#1696).
12602 2. Check dpi value in X resources. It is better we use it as well,
12603 since Xft will use it, as will all Gnome applications. If our real DPI
12604 is smaller or larger than the one Xft uses, our font will look smaller
12605 or larger than other for other applications, even if it is the same
12606 font name (monospace-10 for example). */
12608 int event_base, error_base;
12609 char *v;
12610 double d;
12612 XRenderQueryExtension (dpyinfo->display, &event_base, &error_base);
12614 v = XGetDefault (dpyinfo->display, "Xft", "dpi");
12615 if (v != NULL && sscanf (v, "%lf", &d) == 1)
12616 dpyinfo->resy = dpyinfo->resx = d;
12618 #endif
12620 if (dpyinfo->resy < 1)
12622 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
12623 double pixels = DisplayHeight (dpyinfo->display, screen_number);
12624 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
12625 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
12626 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
12627 pixels = DisplayWidth (dpyinfo->display, screen_number);
12628 mm = DisplayWidthMM (dpyinfo->display, screen_number);
12629 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
12630 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
12634 static const struct
12636 const char *name;
12637 int offset;
12638 } atom_refs[] = {
12639 #define ATOM_REFS_INIT(string, member) \
12640 { string, offsetof (struct x_display_info, member) },
12641 ATOM_REFS_INIT ("WM_PROTOCOLS", Xatom_wm_protocols)
12642 ATOM_REFS_INIT ("WM_TAKE_FOCUS", Xatom_wm_take_focus)
12643 ATOM_REFS_INIT ("WM_SAVE_YOURSELF", Xatom_wm_save_yourself)
12644 ATOM_REFS_INIT ("WM_DELETE_WINDOW", Xatom_wm_delete_window)
12645 ATOM_REFS_INIT ("WM_CHANGE_STATE", Xatom_wm_change_state)
12646 ATOM_REFS_INIT ("WM_CONFIGURE_DENIED", Xatom_wm_configure_denied)
12647 ATOM_REFS_INIT ("WM_MOVED", Xatom_wm_window_moved)
12648 ATOM_REFS_INIT ("WM_CLIENT_LEADER", Xatom_wm_client_leader)
12649 ATOM_REFS_INIT ("Editres", Xatom_editres)
12650 ATOM_REFS_INIT ("CLIPBOARD", Xatom_CLIPBOARD)
12651 ATOM_REFS_INIT ("TIMESTAMP", Xatom_TIMESTAMP)
12652 ATOM_REFS_INIT ("TEXT", Xatom_TEXT)
12653 ATOM_REFS_INIT ("COMPOUND_TEXT", Xatom_COMPOUND_TEXT)
12654 ATOM_REFS_INIT ("UTF8_STRING", Xatom_UTF8_STRING)
12655 ATOM_REFS_INIT ("DELETE", Xatom_DELETE)
12656 ATOM_REFS_INIT ("MULTIPLE", Xatom_MULTIPLE)
12657 ATOM_REFS_INIT ("INCR", Xatom_INCR)
12658 ATOM_REFS_INIT ("_EMACS_TMP_", Xatom_EMACS_TMP)
12659 ATOM_REFS_INIT ("TARGETS", Xatom_TARGETS)
12660 ATOM_REFS_INIT ("NULL", Xatom_NULL)
12661 ATOM_REFS_INIT ("ATOM", Xatom_ATOM)
12662 ATOM_REFS_INIT ("ATOM_PAIR", Xatom_ATOM_PAIR)
12663 ATOM_REFS_INIT ("CLIPBOARD_MANAGER", Xatom_CLIPBOARD_MANAGER)
12664 ATOM_REFS_INIT ("_XEMBED_INFO", Xatom_XEMBED_INFO)
12665 /* For properties of font. */
12666 ATOM_REFS_INIT ("PIXEL_SIZE", Xatom_PIXEL_SIZE)
12667 ATOM_REFS_INIT ("AVERAGE_WIDTH", Xatom_AVERAGE_WIDTH)
12668 ATOM_REFS_INIT ("_MULE_BASELINE_OFFSET", Xatom_MULE_BASELINE_OFFSET)
12669 ATOM_REFS_INIT ("_MULE_RELATIVE_COMPOSE", Xatom_MULE_RELATIVE_COMPOSE)
12670 ATOM_REFS_INIT ("_MULE_DEFAULT_ASCENT", Xatom_MULE_DEFAULT_ASCENT)
12671 /* Ghostscript support. */
12672 ATOM_REFS_INIT ("DONE", Xatom_DONE)
12673 ATOM_REFS_INIT ("PAGE", Xatom_PAGE)
12674 ATOM_REFS_INIT ("SCROLLBAR", Xatom_Scrollbar)
12675 ATOM_REFS_INIT ("HORIZONTAL_SCROLLBAR", Xatom_Horizontal_Scrollbar)
12676 ATOM_REFS_INIT ("_XEMBED", Xatom_XEMBED)
12677 /* EWMH */
12678 ATOM_REFS_INIT ("_NET_WM_STATE", Xatom_net_wm_state)
12679 ATOM_REFS_INIT ("_NET_WM_STATE_FULLSCREEN", Xatom_net_wm_state_fullscreen)
12680 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_HORZ",
12681 Xatom_net_wm_state_maximized_horz)
12682 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_VERT",
12683 Xatom_net_wm_state_maximized_vert)
12684 ATOM_REFS_INIT ("_NET_WM_STATE_STICKY", Xatom_net_wm_state_sticky)
12685 ATOM_REFS_INIT ("_NET_WM_STATE_HIDDEN", Xatom_net_wm_state_hidden)
12686 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE", Xatom_net_window_type)
12687 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE_TOOLTIP",
12688 Xatom_net_window_type_tooltip)
12689 ATOM_REFS_INIT ("_NET_WM_ICON_NAME", Xatom_net_wm_icon_name)
12690 ATOM_REFS_INIT ("_NET_WM_NAME", Xatom_net_wm_name)
12691 ATOM_REFS_INIT ("_NET_SUPPORTED", Xatom_net_supported)
12692 ATOM_REFS_INIT ("_NET_SUPPORTING_WM_CHECK", Xatom_net_supporting_wm_check)
12693 ATOM_REFS_INIT ("_NET_WM_WINDOW_OPACITY", Xatom_net_wm_window_opacity)
12694 ATOM_REFS_INIT ("_NET_ACTIVE_WINDOW", Xatom_net_active_window)
12695 ATOM_REFS_INIT ("_NET_FRAME_EXTENTS", Xatom_net_frame_extents)
12696 ATOM_REFS_INIT ("_NET_CURRENT_DESKTOP", Xatom_net_current_desktop)
12697 ATOM_REFS_INIT ("_NET_WORKAREA", Xatom_net_workarea)
12698 /* Session management */
12699 ATOM_REFS_INIT ("SM_CLIENT_ID", Xatom_SM_CLIENT_ID)
12700 ATOM_REFS_INIT ("_XSETTINGS_SETTINGS", Xatom_xsettings_prop)
12701 ATOM_REFS_INIT ("MANAGER", Xatom_xsettings_mgr)
12702 ATOM_REFS_INIT ("_NET_WM_STATE_SKIP_TASKBAR", Xatom_net_wm_state_skip_taskbar)
12703 ATOM_REFS_INIT ("_NET_WM_STATE_ABOVE", Xatom_net_wm_state_above)
12704 ATOM_REFS_INIT ("_NET_WM_STATE_BELOW", Xatom_net_wm_state_below)
12707 int i;
12708 enum { atom_count = ARRAYELTS (atom_refs) };
12709 /* 1 for _XSETTINGS_SN. */
12710 enum { total_atom_count = 1 + atom_count };
12711 Atom atoms_return[total_atom_count];
12712 char *atom_names[total_atom_count];
12713 static char const xsettings_fmt[] = "_XSETTINGS_S%d";
12714 char xsettings_atom_name[sizeof xsettings_fmt - 2
12715 + INT_STRLEN_BOUND (int)];
12717 for (i = 0; i < atom_count; i++)
12718 atom_names[i] = (char *) atom_refs[i].name;
12720 /* Build _XSETTINGS_SN atom name. */
12721 sprintf (xsettings_atom_name, xsettings_fmt,
12722 XScreenNumberOfScreen (dpyinfo->screen));
12723 atom_names[i] = xsettings_atom_name;
12725 XInternAtoms (dpyinfo->display, atom_names, total_atom_count,
12726 False, atoms_return);
12728 for (i = 0; i < atom_count; i++)
12729 *(Atom *) ((char *) dpyinfo + atom_refs[i].offset) = atoms_return[i];
12731 /* Manually copy last atom. */
12732 dpyinfo->Xatom_xsettings_sel = atoms_return[i];
12735 dpyinfo->x_dnd_atoms_size = 8;
12736 dpyinfo->x_dnd_atoms = xmalloc (sizeof *dpyinfo->x_dnd_atoms
12737 * dpyinfo->x_dnd_atoms_size);
12738 dpyinfo->gray
12739 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12740 gray_bits, gray_width, gray_height,
12741 1, 0, 1);
12743 x_setup_pointer_blanking (dpyinfo);
12745 #ifdef HAVE_X_I18N
12746 xim_initialize (dpyinfo, resource_name);
12747 #endif
12749 xsettings_initialize (dpyinfo);
12751 /* This is only needed for distinguishing keyboard and process input. */
12752 if (dpyinfo->connection != 0)
12753 add_keyboard_wait_descriptor (dpyinfo->connection);
12755 #ifdef F_SETOWN
12756 fcntl (dpyinfo->connection, F_SETOWN, getpid ());
12757 #endif /* ! defined (F_SETOWN) */
12759 if (interrupt_input)
12760 init_sigio (dpyinfo->connection);
12762 #ifdef USE_LUCID
12764 XrmValue d, fr, to;
12765 Font font;
12767 dpy = dpyinfo->display;
12768 d.addr = (XPointer)&dpy;
12769 d.size = sizeof (Display *);
12770 fr.addr = (char *) XtDefaultFont;
12771 fr.size = sizeof (XtDefaultFont);
12772 to.size = sizeof (Font *);
12773 to.addr = (XPointer)&font;
12774 x_catch_errors (dpy);
12775 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
12776 emacs_abort ();
12777 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
12778 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
12779 /* Do not free XFontStruct returned by the above call to XQueryFont.
12780 This leads to X protocol errors at XtCloseDisplay (Bug#18403). */
12781 x_uncatch_errors ();
12783 #endif
12785 /* See if we should run in synchronous mode. This is useful
12786 for debugging X code. */
12788 AUTO_STRING (synchronous, "synchronous");
12789 AUTO_STRING (Synchronous, "Synchronous");
12790 Lisp_Object value = display_x_get_resource (dpyinfo, synchronous,
12791 Synchronous, Qnil, Qnil);
12792 if (STRINGP (value)
12793 && (!strcmp (SSDATA (value), "true")
12794 || !strcmp (SSDATA (value), "on")))
12795 XSynchronize (dpyinfo->display, True);
12799 AUTO_STRING (useXIM, "useXIM");
12800 AUTO_STRING (UseXIM, "UseXIM");
12801 Lisp_Object value = display_x_get_resource (dpyinfo, useXIM, UseXIM,
12802 Qnil, Qnil);
12803 #ifdef USE_XIM
12804 if (STRINGP (value)
12805 && (!strcmp (SSDATA (value), "false")
12806 || !strcmp (SSDATA (value), "off")))
12807 use_xim = false;
12808 #else
12809 if (STRINGP (value)
12810 && (!strcmp (SSDATA (value), "true")
12811 || !strcmp (SSDATA (value), "on")))
12812 use_xim = true;
12813 #endif
12816 #ifdef HAVE_X_SM
12817 /* Only do this for the very first display in the Emacs session.
12818 Ignore X session management when Emacs was first started on a
12819 tty or started as a daemon. */
12820 if (terminal->id == 1 && ! IS_DAEMON)
12821 x_session_initialize (dpyinfo);
12822 #endif
12824 #ifdef USE_CAIRO
12825 x_extension_initialize (dpyinfo);
12826 #endif
12828 unblock_input ();
12830 return dpyinfo;
12833 /* Get rid of display DPYINFO, deleting all frames on it,
12834 and without sending any more commands to the X server. */
12836 static void
12837 x_delete_display (struct x_display_info *dpyinfo)
12839 struct terminal *t;
12840 struct color_name_cache_entry *color_entry, *next_color_entry;
12842 /* Close all frames and delete the generic struct terminal for this
12843 X display. */
12844 for (t = terminal_list; t; t = t->next_terminal)
12845 if (t->type == output_x_window && t->display_info.x == dpyinfo)
12847 #ifdef HAVE_X_SM
12848 /* Close X session management when we close its display. */
12849 if (t->id == 1 && x_session_have_connection ())
12850 x_session_close ();
12851 #endif
12852 delete_terminal (t);
12853 break;
12856 if (next_noop_dpyinfo == dpyinfo)
12857 next_noop_dpyinfo = dpyinfo->next;
12859 if (x_display_list == dpyinfo)
12860 x_display_list = dpyinfo->next;
12861 else
12863 struct x_display_info *tail;
12865 for (tail = x_display_list; tail; tail = tail->next)
12866 if (tail->next == dpyinfo)
12867 tail->next = tail->next->next;
12870 for (color_entry = dpyinfo->color_names;
12871 color_entry;
12872 color_entry = next_color_entry)
12874 next_color_entry = color_entry->next;
12875 xfree (color_entry->name);
12876 xfree (color_entry);
12879 xfree (dpyinfo->x_id_name);
12880 xfree (dpyinfo->x_dnd_atoms);
12881 xfree (dpyinfo->color_cells);
12882 xfree (dpyinfo);
12885 #ifdef USE_X_TOOLKIT
12887 /* Atimer callback function for TIMER. Called every 0.1s to process
12888 Xt timeouts, if needed. We must avoid calling XtAppPending as
12889 much as possible because that function does an implicit XFlush
12890 that slows us down. */
12892 static void
12893 x_process_timeouts (struct atimer *timer)
12895 block_input ();
12896 x_timeout_atimer_activated_flag = false;
12897 if (toolkit_scroll_bar_interaction || popup_activated ())
12899 while (XtAppPending (Xt_app_con) & XtIMTimer)
12900 XtAppProcessEvent (Xt_app_con, XtIMTimer);
12901 /* Reactivate the atimer for next time. */
12902 x_activate_timeout_atimer ();
12904 unblock_input ();
12907 /* Install an asynchronous timer that processes Xt timeout events
12908 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
12909 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
12910 function whenever these variables are set. This is necessary
12911 because some widget sets use timeouts internally, for example the
12912 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
12913 processed, these widgets don't behave normally. */
12915 void
12916 x_activate_timeout_atimer (void)
12918 block_input ();
12919 if (!x_timeout_atimer_activated_flag)
12921 struct timespec interval = make_timespec (0, 100 * 1000 * 1000);
12922 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
12923 x_timeout_atimer_activated_flag = true;
12925 unblock_input ();
12928 #endif /* USE_X_TOOLKIT */
12931 /* Set up use of X before we make the first connection. */
12933 static struct redisplay_interface x_redisplay_interface =
12935 x_frame_parm_handlers,
12936 x_produce_glyphs,
12937 x_write_glyphs,
12938 x_insert_glyphs,
12939 x_clear_end_of_line,
12940 x_scroll_run,
12941 x_after_update_window_line,
12942 x_update_window_begin,
12943 x_update_window_end,
12944 x_flip_and_flush,
12945 x_clear_window_mouse_face,
12946 x_get_glyph_overhangs,
12947 x_fix_overlapping_area,
12948 x_draw_fringe_bitmap,
12949 #ifdef USE_CAIRO
12950 x_cr_define_fringe_bitmap,
12951 x_cr_destroy_fringe_bitmap,
12952 #else
12953 0, /* define_fringe_bitmap */
12954 0, /* destroy_fringe_bitmap */
12955 #endif
12956 x_compute_glyph_string_overhangs,
12957 x_draw_glyph_string,
12958 x_define_frame_cursor,
12959 x_clear_frame_area,
12960 x_draw_window_cursor,
12961 x_draw_vertical_window_border,
12962 x_draw_window_divider,
12963 x_shift_glyphs_for_insert, /* Never called; see comment in function. */
12964 x_show_hourglass,
12965 x_hide_hourglass
12969 /* This function is called when the last frame on a display is deleted. */
12970 void
12971 x_delete_terminal (struct terminal *terminal)
12973 struct x_display_info *dpyinfo = terminal->display_info.x;
12975 /* Protect against recursive calls. delete_frame in
12976 delete_terminal calls us back when it deletes our last frame. */
12977 if (!terminal->name)
12978 return;
12980 block_input ();
12981 #ifdef HAVE_X_I18N
12982 /* We must close our connection to the XIM server before closing the
12983 X display. */
12984 if (dpyinfo->xim)
12985 xim_close_dpy (dpyinfo);
12986 #endif
12988 /* Normally, the display is available... */
12989 if (dpyinfo->display)
12991 x_destroy_all_bitmaps (dpyinfo);
12992 XSetCloseDownMode (dpyinfo->display, DestroyAll);
12994 /* Whether or not XCloseDisplay destroys the associated resource
12995 database depends on the version of libX11. To avoid both
12996 crash and memory leak, we dissociate the database from the
12997 display and then destroy dpyinfo->xrdb ourselves.
12999 Unfortunately, the above strategy does not work in some
13000 situations due to a bug in newer versions of libX11: because
13001 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
13002 dpy->db is NULL, XCloseDisplay destroys the associated
13003 database whereas it has not been created by XGetDefault
13004 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
13005 don't destroy the database here in order to avoid the crash
13006 in the above situations for now, though that may cause memory
13007 leaks in other situations. */
13008 #if false
13009 #ifdef HAVE_XRMSETDATABASE
13010 XrmSetDatabase (dpyinfo->display, NULL);
13011 #else
13012 dpyinfo->display->db = NULL;
13013 #endif
13014 /* We used to call XrmDestroyDatabase from x_delete_display, but
13015 some older versions of libX11 crash if we call it after
13016 closing all the displays. */
13017 XrmDestroyDatabase (dpyinfo->xrdb);
13018 #endif
13020 #ifdef USE_GTK
13021 xg_display_close (dpyinfo->display);
13022 #else
13023 #ifdef USE_X_TOOLKIT
13024 XtCloseDisplay (dpyinfo->display);
13025 #else
13026 XCloseDisplay (dpyinfo->display);
13027 #endif
13028 #endif /* ! USE_GTK */
13029 /* Do not close the connection here because it's already closed
13030 by X(t)CloseDisplay (Bug#18403). */
13031 dpyinfo->display = NULL;
13034 /* ...but if called from x_connection_closed, the display may already
13035 be closed and dpyinfo->display was set to 0 to indicate that. Since
13036 X server is most likely gone, explicit close is the only reliable
13037 way to continue and avoid Bug#19147. */
13038 else if (dpyinfo->connection >= 0)
13039 emacs_close (dpyinfo->connection);
13041 /* No more input on this descriptor. */
13042 delete_keyboard_wait_descriptor (dpyinfo->connection);
13043 /* Mark as dead. */
13044 dpyinfo->connection = -1;
13046 x_delete_display (dpyinfo);
13047 unblock_input ();
13050 /* Create a struct terminal, initialize it with the X11 specific
13051 functions and make DISPLAY->TERMINAL point to it. */
13053 static struct terminal *
13054 x_create_terminal (struct x_display_info *dpyinfo)
13056 struct terminal *terminal;
13058 terminal = create_terminal (output_x_window, &x_redisplay_interface);
13060 terminal->display_info.x = dpyinfo;
13061 dpyinfo->terminal = terminal;
13063 /* kboard is initialized in x_term_init. */
13065 terminal->clear_frame_hook = x_clear_frame;
13066 terminal->ins_del_lines_hook = x_ins_del_lines;
13067 terminal->delete_glyphs_hook = x_delete_glyphs;
13068 terminal->ring_bell_hook = XTring_bell;
13069 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
13070 terminal->update_begin_hook = x_update_begin;
13071 terminal->update_end_hook = x_update_end;
13072 terminal->read_socket_hook = XTread_socket;
13073 terminal->frame_up_to_date_hook = XTframe_up_to_date;
13074 terminal->buffer_flipping_unblocked_hook = XTbuffer_flipping_unblocked_hook;
13075 terminal->mouse_position_hook = XTmouse_position;
13076 terminal->frame_rehighlight_hook = XTframe_rehighlight;
13077 terminal->frame_raise_lower_hook = XTframe_raise_lower;
13078 terminal->fullscreen_hook = XTfullscreen_hook;
13079 terminal->menu_show_hook = x_menu_show;
13080 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
13081 terminal->popup_dialog_hook = xw_popup_dialog;
13082 #endif
13083 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13084 terminal->set_horizontal_scroll_bar_hook = XTset_horizontal_scroll_bar;
13085 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13086 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
13087 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
13088 terminal->delete_frame_hook = x_destroy_window;
13089 terminal->delete_terminal_hook = x_delete_terminal;
13090 /* Other hooks are NULL by default. */
13092 return terminal;
13095 static void
13096 x_initialize (void)
13098 baud_rate = 19200;
13100 x_noop_count = 0;
13101 any_help_event_p = false;
13102 ignore_next_mouse_click_timeout = 0;
13104 #ifdef USE_GTK
13105 current_count = -1;
13106 #endif
13108 /* Try to use interrupt input; if we can't, then start polling. */
13109 Fset_input_interrupt_mode (Qt);
13111 #if THREADS_ENABLED
13112 /* This must be called before any other Xlib routines. */
13113 if (XInitThreads () == 0)
13114 fprintf (stderr,
13115 "Warning: An error occurred initializing X11 thread support!\n");
13116 #endif
13118 #ifdef USE_X_TOOLKIT
13119 XtToolkitInitialize ();
13121 Xt_app_con = XtCreateApplicationContext ();
13123 /* Register a converter from strings to pixels, which uses
13124 Emacs' color allocation infrastructure. */
13125 XtAppSetTypeConverter (Xt_app_con,
13126 XtRString, XtRPixel, cvt_string_to_pixel,
13127 cvt_string_to_pixel_args,
13128 XtNumber (cvt_string_to_pixel_args),
13129 XtCacheByDisplay, cvt_pixel_dtor);
13131 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
13132 #endif
13134 #ifdef USE_TOOLKIT_SCROLL_BARS
13135 #ifndef USE_GTK
13136 xaw3d_arrow_scroll = False;
13137 xaw3d_pick_top = True;
13138 #endif
13139 #endif
13141 #ifdef USE_CAIRO
13142 x_cr_init_fringe (&x_redisplay_interface);
13143 #endif
13145 /* Note that there is no real way portable across R3/R4 to get the
13146 original error handler. */
13147 XSetErrorHandler (x_error_handler);
13148 XSetIOErrorHandler (x_io_error_quitter);
13151 #ifdef USE_GTK
13152 void
13153 init_xterm (void)
13155 /* Emacs can handle only core input events, so make sure
13156 Gtk doesn't use Xinput or Xinput2 extensions. */
13157 xputenv ("GDK_CORE_DEVICE_EVENTS=1");
13159 #endif
13161 void
13162 syms_of_xterm (void)
13164 x_error_message = NULL;
13166 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
13167 DEFSYM (Qlatin_1, "latin-1");
13169 #ifdef USE_GTK
13170 xg_default_icon_file = build_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
13171 staticpro (&xg_default_icon_file);
13173 DEFSYM (Qx_gtk_map_stock, "x-gtk-map-stock");
13174 #endif
13176 DEFVAR_BOOL ("x-use-underline-position-properties",
13177 x_use_underline_position_properties,
13178 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
13179 A value of nil means ignore them. If you encounter fonts with bogus
13180 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
13181 to 4.1, set this to nil. You can also use `underline-minimum-offset'
13182 to override the font's UNDERLINE_POSITION for small font display
13183 sizes. */);
13184 x_use_underline_position_properties = true;
13186 DEFVAR_BOOL ("x-underline-at-descent-line",
13187 x_underline_at_descent_line,
13188 doc: /* Non-nil means to draw the underline at the same place as the descent line.
13189 A value of nil means to draw the underline according to the value of the
13190 variable `x-use-underline-position-properties', which is usually at the
13191 baseline level. The default value is nil. */);
13192 x_underline_at_descent_line = false;
13194 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
13195 x_mouse_click_focus_ignore_position,
13196 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
13197 This variable is only used when the window manager requires that you
13198 click on a frame to select it (give it focus). In that case, a value
13199 of nil, means that the selected window and cursor position changes to
13200 reflect the mouse click position, while a non-nil value means that the
13201 selected window or cursor position is preserved. */);
13202 x_mouse_click_focus_ignore_position = false;
13204 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
13205 doc: /* Which toolkit scroll bars Emacs uses, if any.
13206 A value of nil means Emacs doesn't use toolkit scroll bars.
13207 With the X Window system, the value is a symbol describing the
13208 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
13209 With MS Windows or Nextstep, the value is t. */);
13210 #ifdef USE_TOOLKIT_SCROLL_BARS
13211 #ifdef USE_MOTIF
13212 Vx_toolkit_scroll_bars = intern_c_string ("motif");
13213 #elif defined HAVE_XAW3D
13214 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
13215 #elif USE_GTK
13216 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
13217 #else
13218 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
13219 #endif
13220 #else
13221 Vx_toolkit_scroll_bars = Qnil;
13222 #endif
13224 DEFSYM (Qmodifier_value, "modifier-value");
13225 DEFSYM (Qctrl, "ctrl");
13226 Fput (Qctrl, Qmodifier_value, make_number (ctrl_modifier));
13227 DEFSYM (Qalt, "alt");
13228 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
13229 DEFSYM (Qhyper, "hyper");
13230 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
13231 DEFSYM (Qmeta, "meta");
13232 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
13233 DEFSYM (Qsuper, "super");
13234 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
13236 DEFVAR_LISP ("x-ctrl-keysym", Vx_ctrl_keysym,
13237 doc: /* Which keys Emacs uses for the ctrl modifier.
13238 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
13239 `super'. For example, `ctrl' means use the Ctrl_L and Ctrl_R keysyms.
13240 The default is nil, which is the same as `ctrl'. */);
13241 Vx_ctrl_keysym = Qnil;
13243 DEFVAR_LISP ("x-alt-keysym", Vx_alt_keysym,
13244 doc: /* Which keys Emacs uses for the alt modifier.
13245 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
13246 `super'. For example, `alt' means use the Alt_L and Alt_R keysyms.
13247 The default is nil, which is the same as `alt'. */);
13248 Vx_alt_keysym = Qnil;
13250 DEFVAR_LISP ("x-hyper-keysym", Vx_hyper_keysym,
13251 doc: /* Which keys Emacs uses for the hyper modifier.
13252 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
13253 `super'. For example, `hyper' means use the Hyper_L and Hyper_R
13254 keysyms. The default is nil, which is the same as `hyper'. */);
13255 Vx_hyper_keysym = Qnil;
13257 DEFVAR_LISP ("x-meta-keysym", Vx_meta_keysym,
13258 doc: /* Which keys Emacs uses for the meta modifier.
13259 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
13260 `super'. For example, `meta' means use the Meta_L and Meta_R keysyms.
13261 The default is nil, which is the same as `meta'. */);
13262 Vx_meta_keysym = Qnil;
13264 DEFVAR_LISP ("x-super-keysym", Vx_super_keysym,
13265 doc: /* Which keys Emacs uses for the super modifier.
13266 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
13267 `super'. For example, `super' means use the Super_L and Super_R
13268 keysyms. The default is nil, which is the same as `super'. */);
13269 Vx_super_keysym = Qnil;
13271 DEFVAR_LISP ("x-keysym-table", Vx_keysym_table,
13272 doc: /* Hash table of character codes indexed by X keysym codes. */);
13273 Vx_keysym_table = make_hash_table (hashtest_eql, 900,
13274 DEFAULT_REHASH_SIZE,
13275 DEFAULT_REHASH_THRESHOLD,
13276 Qnil, false);
13278 DEFVAR_BOOL ("x-frame-normalize-before-maximize",
13279 x_frame_normalize_before_maximize,
13280 doc: /* Non-nil means normalize frame before maximizing.
13281 If this variable is t, Emacs first asks the window manager to give the
13282 frame its normal size, and only then the final state, whenever changing
13283 from a full-height, full-width or full-both state to the maximized one
13284 or when changing from the maximized to the full-height or full-width
13285 state.
13287 Set this variable only if your window manager cannot handle the
13288 transition between the various maximization states. */);
13289 x_frame_normalize_before_maximize = false;
13291 DEFVAR_BOOL ("x-gtk-use-window-move", x_gtk_use_window_move,
13292 doc: /* Non-nil means rely on gtk_window_move to set frame positions.
13293 If this variable is t (the default), the GTK build uses the function
13294 gtk_window_move to set or store frame positions and disables some time
13295 consuming frame position adjustments. */);
13296 x_gtk_use_window_move = true;