Fix ‘!NILP (Vpurify_flag)’ assertion failure during temacs bootstrap
[emacs.git] / src / xterm.c
blob4f9eff6c5e6a0e02118d1aad5c1177b08c662c83
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 #ifdef USE_CAIRO
27 #include <math.h>
28 #endif
30 #include "lisp.h"
31 #include "blockinput.h"
33 /* This may include sys/types.h, and that somehow loses
34 if this is not done before the other system files. */
35 #include "xterm.h"
36 #include <X11/cursorfont.h>
38 /* If we have Xfixes extension, use it for pointer blanking. */
39 #ifdef HAVE_XFIXES
40 #include <X11/extensions/Xfixes.h>
41 #endif
43 /* Using Xft implies that XRender is available. */
44 #ifdef HAVE_XFT
45 #include <X11/extensions/Xrender.h>
46 #endif
48 #ifdef HAVE_XDBE
49 #include <X11/extensions/Xdbe.h>
50 #endif
52 /* Load sys/types.h if not already loaded.
53 In some systems loading it twice is suicidal. */
54 #ifndef makedev
55 #include <sys/types.h>
56 #endif /* makedev */
58 #include <sys/ioctl.h>
60 #include "systime.h"
62 #include <fcntl.h>
63 #include <errno.h>
64 #include <sys/stat.h>
65 #include "character.h"
66 #include "coding.h"
67 #include "composite.h"
68 #include "frame.h"
69 #include "dispextern.h"
70 #include "xwidget.h"
71 #include "fontset.h"
72 #include "termhooks.h"
73 #include "termopts.h"
74 #include "termchar.h"
75 #include "emacs-icon.h"
76 #include "buffer.h"
77 #include "window.h"
78 #include "keyboard.h"
79 #include "atimer.h"
80 #include "font.h"
81 #include "xsettings.h"
82 #include "sysselect.h"
83 #include "menu.h"
85 #ifdef USE_X_TOOLKIT
86 #include <X11/Shell.h>
87 #endif
89 #include <unistd.h>
91 #ifdef USE_GTK
92 #include "gtkutil.h"
93 #ifdef HAVE_GTK3
94 #include <X11/Xproto.h>
95 #endif
96 #endif
98 #if defined (USE_LUCID) || defined (USE_MOTIF)
99 #include "../lwlib/xlwmenu.h"
100 #endif
102 #ifdef USE_X_TOOLKIT
104 /* Include toolkit specific headers for the scroll bar widget. */
106 #ifdef USE_TOOLKIT_SCROLL_BARS
107 #if defined USE_MOTIF
108 #include <Xm/Xm.h> /* For LESSTIF_VERSION */
109 #include <Xm/ScrollBar.h>
110 #else /* !USE_MOTIF i.e. use Xaw */
112 #ifdef HAVE_XAW3D
113 #include <X11/Xaw3d/Simple.h>
114 #include <X11/Xaw3d/Scrollbar.h>
115 #include <X11/Xaw3d/ThreeD.h>
116 #else /* !HAVE_XAW3D */
117 #include <X11/Xaw/Simple.h>
118 #include <X11/Xaw/Scrollbar.h>
119 #endif /* !HAVE_XAW3D */
120 #ifndef XtNpickTop
121 #define XtNpickTop "pickTop"
122 #endif /* !XtNpickTop */
123 #endif /* !USE_MOTIF */
124 #endif /* USE_TOOLKIT_SCROLL_BARS */
126 #endif /* USE_X_TOOLKIT */
128 #ifdef USE_X_TOOLKIT
129 #include "widget.h"
130 #ifndef XtNinitialState
131 #define XtNinitialState "initialState"
132 #endif
133 #endif
135 #include "bitmaps/gray.xbm"
137 #ifdef HAVE_XKB
138 #include <X11/XKBlib.h>
139 #endif
141 /* Default to using XIM if available. */
142 #ifdef USE_XIM
143 bool use_xim = true;
144 #else
145 bool use_xim = false; /* configure --without-xim */
146 #endif
148 /* Non-zero means that a HELP_EVENT has been generated since Emacs
149 start. */
151 static bool any_help_event_p;
153 /* This is a chain of structures for all the X displays currently in
154 use. */
156 struct x_display_info *x_display_list;
158 #ifdef USE_X_TOOLKIT
160 /* The application context for Xt use. */
161 XtAppContext Xt_app_con;
162 static String Xt_default_resources[] = {0};
164 /* Non-zero means user is interacting with a toolkit scroll bar. */
165 static bool toolkit_scroll_bar_interaction;
167 #endif /* USE_X_TOOLKIT */
169 /* Non-zero timeout value means ignore next mouse click if it arrives
170 before that timeout elapses (i.e. as part of the same sequence of
171 events resulting from clicking on a frame to select it). */
173 static Time ignore_next_mouse_click_timeout;
175 /* Used locally within XTread_socket. */
177 static int x_noop_count;
179 #ifdef USE_GTK
180 /* The name of the Emacs icon file. */
181 static Lisp_Object xg_default_icon_file;
182 #endif
184 /* Some functions take this as char *, not const char *. */
185 static char emacs_class[] = EMACS_CLASS;
187 enum xembed_info
189 XEMBED_MAPPED = 1 << 0
192 enum xembed_message
194 XEMBED_EMBEDDED_NOTIFY = 0,
195 XEMBED_WINDOW_ACTIVATE = 1,
196 XEMBED_WINDOW_DEACTIVATE = 2,
197 XEMBED_REQUEST_FOCUS = 3,
198 XEMBED_FOCUS_IN = 4,
199 XEMBED_FOCUS_OUT = 5,
200 XEMBED_FOCUS_NEXT = 6,
201 XEMBED_FOCUS_PREV = 7,
203 XEMBED_MODALITY_ON = 10,
204 XEMBED_MODALITY_OFF = 11,
205 XEMBED_REGISTER_ACCELERATOR = 12,
206 XEMBED_UNREGISTER_ACCELERATOR = 13,
207 XEMBED_ACTIVATE_ACCELERATOR = 14
210 static void x_free_cr_resources (struct frame *);
211 static bool x_alloc_nearest_color_1 (Display *, Colormap, XColor *);
212 static void x_raise_frame (struct frame *);
213 static void x_lower_frame (struct frame *);
214 static int x_io_error_quitter (Display *);
215 static struct terminal *x_create_terminal (struct x_display_info *);
216 static void x_frame_rehighlight (struct x_display_info *);
218 static void x_clip_to_row (struct window *, struct glyph_row *,
219 enum glyph_row_area, GC);
220 static struct scroll_bar *x_window_to_scroll_bar (Display *, Window, int);
221 static void x_scroll_bar_report_motion (struct frame **, Lisp_Object *,
222 enum scroll_bar_part *,
223 Lisp_Object *, Lisp_Object *,
224 Time *);
225 static void x_horizontal_scroll_bar_report_motion (struct frame **, Lisp_Object *,
226 enum scroll_bar_part *,
227 Lisp_Object *, Lisp_Object *,
228 Time *);
229 static bool x_handle_net_wm_state (struct frame *, const XPropertyEvent *);
230 static void x_check_fullscreen (struct frame *);
231 static void x_check_expected_move (struct frame *, int, int);
232 static void x_sync_with_move (struct frame *, int, int, bool);
233 static int handle_one_xevent (struct x_display_info *,
234 const XEvent *, int *,
235 struct input_event *);
236 #if ! (defined USE_X_TOOLKIT || defined USE_MOTIF)
237 static int x_dispatch_event (XEvent *, Display *);
238 #endif
239 static void x_wm_set_window_state (struct frame *, int);
240 static void x_wm_set_icon_pixmap (struct frame *, ptrdiff_t);
241 static void x_initialize (void);
243 static bool get_current_wm_state (struct frame *, Window, int *, bool *);
245 /* Flush display of frame F. */
247 static void
248 x_flush (struct frame *f)
250 eassert (f && FRAME_X_P (f));
251 /* Don't call XFlush when it is not safe to redisplay; the X
252 connection may be broken. */
253 if (!NILP (Vinhibit_redisplay))
254 return;
256 block_input ();
257 XFlush (FRAME_X_DISPLAY (f));
258 unblock_input ();
262 /* Remove calls to XFlush by defining XFlush to an empty replacement.
263 Calls to XFlush should be unnecessary because the X output buffer
264 is flushed automatically as needed by calls to XPending,
265 XNextEvent, or XWindowEvent according to the XFlush man page.
266 XTread_socket calls XPending. Removing XFlush improves
267 performance. */
269 #define XFlush(DISPLAY) (void) 0
272 /***********************************************************************
273 Debugging
274 ***********************************************************************/
276 #if false
278 /* This is a function useful for recording debugging information about
279 the sequence of occurrences in this file. */
281 struct record
283 char *locus;
284 int type;
287 struct record event_record[100];
289 int event_record_index;
291 void
292 record_event (char *locus, int type)
294 if (event_record_index == ARRAYELTS (event_record))
295 event_record_index = 0;
297 event_record[event_record_index].locus = locus;
298 event_record[event_record_index].type = type;
299 event_record_index++;
302 #endif
304 #ifdef USE_CAIRO
306 #define FRAME_CR_CONTEXT(f) ((f)->output_data.x->cr_context)
307 #define FRAME_CR_SURFACE(f) ((f)->output_data.x->cr_surface)
309 static struct x_gc_ext_data *
310 x_gc_get_ext_data (struct frame *f, GC gc, int create_if_not_found_p)
312 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
313 XEDataObject object;
314 XExtData **head, *ext_data;
316 object.gc = gc;
317 head = XEHeadOfExtensionList (object);
318 ext_data = XFindOnExtensionList (head, dpyinfo->ext_codes->extension);
319 if (ext_data == NULL)
321 if (!create_if_not_found_p)
322 return NULL;
323 else
325 ext_data = xzalloc (sizeof (*ext_data));
326 ext_data->number = dpyinfo->ext_codes->extension;
327 ext_data->private_data = xzalloc (sizeof (struct x_gc_ext_data));
328 XAddToExtensionList (head, ext_data);
331 return (struct x_gc_ext_data *) ext_data->private_data;
334 static void
335 x_extension_initialize (struct x_display_info *dpyinfo)
337 XExtCodes *ext_codes = XAddExtension (dpyinfo->display);
339 dpyinfo->ext_codes = ext_codes;
342 static void
343 x_cr_destroy_surface (struct frame *f)
345 if (FRAME_CR_SURFACE (f))
347 cairo_t *cr = FRAME_CR_CONTEXT (f);
348 cairo_surface_destroy (FRAME_CR_SURFACE (f));
349 FRAME_CR_SURFACE (f) = 0;
350 if (cr) cairo_destroy (cr);
351 FRAME_CR_CONTEXT (f) = NULL;
355 cairo_t *
356 x_begin_cr_clip (struct frame *f, GC gc)
358 cairo_t *cr = FRAME_CR_CONTEXT (f);
360 if (!cr)
363 if (! FRAME_CR_SURFACE (f))
365 cairo_surface_t *surface;
366 surface = cairo_xlib_surface_create (FRAME_X_DISPLAY (f),
367 FRAME_X_DRAWABLE (f),
368 FRAME_DISPLAY_INFO (f)->visual,
369 FRAME_PIXEL_WIDTH (f),
370 FRAME_PIXEL_HEIGHT (f));
371 cr = cairo_create (surface);
372 cairo_surface_destroy (surface);
374 else
375 cr = cairo_create (FRAME_CR_SURFACE (f));
376 FRAME_CR_CONTEXT (f) = cr;
378 cairo_save (cr);
380 if (gc)
382 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 0);
384 if (gc_ext && gc_ext->n_clip_rects)
386 int i;
388 for (i = 0; i < gc_ext->n_clip_rects; i++)
389 cairo_rectangle (cr, gc_ext->clip_rects[i].x,
390 gc_ext->clip_rects[i].y,
391 gc_ext->clip_rects[i].width,
392 gc_ext->clip_rects[i].height);
393 cairo_clip (cr);
397 return cr;
400 void
401 x_end_cr_clip (struct frame *f)
403 cairo_restore (FRAME_CR_CONTEXT (f));
406 void
407 x_set_cr_source_with_gc_foreground (struct frame *f, GC gc)
409 XGCValues xgcv;
410 XColor color;
412 XGetGCValues (FRAME_X_DISPLAY (f), gc, GCForeground, &xgcv);
413 color.pixel = xgcv.foreground;
414 x_query_color (f, &color);
415 cairo_set_source_rgb (FRAME_CR_CONTEXT (f), color.red / 65535.0,
416 color.green / 65535.0, color.blue / 65535.0);
419 void
420 x_set_cr_source_with_gc_background (struct frame *f, GC gc)
422 XGCValues xgcv;
423 XColor color;
425 XGetGCValues (FRAME_X_DISPLAY (f), gc, GCBackground, &xgcv);
426 color.pixel = xgcv.background;
427 x_query_color (f, &color);
428 cairo_set_source_rgb (FRAME_CR_CONTEXT (f), color.red / 65535.0,
429 color.green / 65535.0, color.blue / 65535.0);
432 /* Fringe bitmaps. */
434 static int max_fringe_bmp = 0;
435 static cairo_pattern_t **fringe_bmp = 0;
437 static void
438 x_cr_define_fringe_bitmap (int which, unsigned short *bits, int h, int wd)
440 int i, stride;
441 cairo_surface_t *surface;
442 unsigned char *data;
443 cairo_pattern_t *pattern;
445 if (which >= max_fringe_bmp)
447 i = max_fringe_bmp;
448 max_fringe_bmp = which + 20;
449 fringe_bmp = (cairo_pattern_t **) xrealloc (fringe_bmp, max_fringe_bmp * sizeof (cairo_pattern_t *));
450 while (i < max_fringe_bmp)
451 fringe_bmp[i++] = 0;
454 block_input ();
456 surface = cairo_image_surface_create (CAIRO_FORMAT_A1, wd, h);
457 stride = cairo_image_surface_get_stride (surface);
458 data = cairo_image_surface_get_data (surface);
460 for (i = 0; i < h; i++)
462 *((unsigned short *) data) = bits[i];
463 data += stride;
466 cairo_surface_mark_dirty (surface);
467 pattern = cairo_pattern_create_for_surface (surface);
468 cairo_surface_destroy (surface);
470 unblock_input ();
472 fringe_bmp[which] = pattern;
475 static void
476 x_cr_destroy_fringe_bitmap (int which)
478 if (which >= max_fringe_bmp)
479 return;
481 if (fringe_bmp[which])
483 block_input ();
484 cairo_pattern_destroy (fringe_bmp[which]);
485 unblock_input ();
487 fringe_bmp[which] = 0;
490 static void
491 x_cr_draw_image (struct frame *f, GC gc, cairo_pattern_t *image,
492 int src_x, int src_y, int width, int height,
493 int dest_x, int dest_y, bool overlay_p)
495 cairo_t *cr;
496 cairo_matrix_t matrix;
497 cairo_surface_t *surface;
498 cairo_format_t format;
500 cr = x_begin_cr_clip (f, gc);
501 if (overlay_p)
502 cairo_rectangle (cr, dest_x, dest_y, width, height);
503 else
505 x_set_cr_source_with_gc_background (f, gc);
506 cairo_rectangle (cr, dest_x, dest_y, width, height);
507 cairo_fill_preserve (cr);
509 cairo_clip (cr);
510 cairo_matrix_init_translate (&matrix, src_x - dest_x, src_y - dest_y);
511 cairo_pattern_set_matrix (image, &matrix);
512 cairo_pattern_get_surface (image, &surface);
513 format = cairo_image_surface_get_format (surface);
514 if (format != CAIRO_FORMAT_A8 && format != CAIRO_FORMAT_A1)
516 cairo_set_source (cr, image);
517 cairo_fill (cr);
519 else
521 x_set_cr_source_with_gc_foreground (f, gc);
522 cairo_mask (cr, image);
524 x_end_cr_clip (f);
527 void
528 x_cr_draw_frame (cairo_t *cr, struct frame *f)
530 int width, height;
532 width = FRAME_PIXEL_WIDTH (f);
533 height = FRAME_PIXEL_HEIGHT (f);
535 x_free_cr_resources (f);
536 FRAME_CR_CONTEXT (f) = cr;
537 x_clear_area (f, 0, 0, width, height);
538 expose_frame (f, 0, 0, width, height);
539 FRAME_CR_CONTEXT (f) = NULL;
542 static cairo_status_t
543 x_cr_accumulate_data (void *closure, const unsigned char *data,
544 unsigned int length)
546 Lisp_Object *acc = (Lisp_Object *) closure;
548 *acc = Fcons (make_unibyte_string ((char const *) data, length), *acc);
550 return CAIRO_STATUS_SUCCESS;
553 static void
554 x_cr_destroy (Lisp_Object arg)
556 cairo_t *cr = (cairo_t *) XSAVE_POINTER (arg, 0);
558 block_input ();
559 cairo_destroy (cr);
560 unblock_input ();
563 Lisp_Object
564 x_cr_export_frames (Lisp_Object frames, cairo_surface_type_t surface_type)
566 struct frame *f;
567 cairo_surface_t *surface;
568 cairo_t *cr;
569 int width, height;
570 void (*surface_set_size_func) (cairo_surface_t *, double, double) = NULL;
571 Lisp_Object acc = Qnil;
572 int count = SPECPDL_INDEX ();
574 specbind (Qredisplay_dont_pause, Qt);
575 redisplay_preserve_echo_area (31);
577 f = XFRAME (XCAR (frames));
578 frames = XCDR (frames);
579 width = FRAME_PIXEL_WIDTH (f);
580 height = FRAME_PIXEL_HEIGHT (f);
582 block_input ();
583 #ifdef CAIRO_HAS_PDF_SURFACE
584 if (surface_type == CAIRO_SURFACE_TYPE_PDF)
586 surface = cairo_pdf_surface_create_for_stream (x_cr_accumulate_data, &acc,
587 width, height);
588 surface_set_size_func = cairo_pdf_surface_set_size;
590 else
591 #endif
592 #ifdef CAIRO_HAS_PNG_FUNCTIONS
593 if (surface_type == CAIRO_SURFACE_TYPE_IMAGE)
594 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
595 else
596 #endif
597 #ifdef CAIRO_HAS_PS_SURFACE
598 if (surface_type == CAIRO_SURFACE_TYPE_PS)
600 surface = cairo_ps_surface_create_for_stream (x_cr_accumulate_data, &acc,
601 width, height);
602 surface_set_size_func = cairo_ps_surface_set_size;
604 else
605 #endif
606 #ifdef CAIRO_HAS_SVG_SURFACE
607 if (surface_type == CAIRO_SURFACE_TYPE_SVG)
608 surface = cairo_svg_surface_create_for_stream (x_cr_accumulate_data, &acc,
609 width, height);
610 else
611 #endif
612 abort ();
614 cr = cairo_create (surface);
615 cairo_surface_destroy (surface);
616 record_unwind_protect (x_cr_destroy, make_save_ptr (cr));
618 while (1)
620 x_free_cr_resources (f);
621 FRAME_CR_CONTEXT (f) = cr;
622 x_clear_area (f, 0, 0, width, height);
623 expose_frame (f, 0, 0, width, height);
624 FRAME_CR_CONTEXT (f) = NULL;
626 if (NILP (frames))
627 break;
629 cairo_surface_show_page (surface);
630 f = XFRAME (XCAR (frames));
631 frames = XCDR (frames);
632 width = FRAME_PIXEL_WIDTH (f);
633 height = FRAME_PIXEL_HEIGHT (f);
634 if (surface_set_size_func)
635 (*surface_set_size_func) (surface, width, height);
637 unblock_input ();
638 maybe_quit ();
639 block_input ();
642 #ifdef CAIRO_HAS_PNG_FUNCTIONS
643 if (surface_type == CAIRO_SURFACE_TYPE_IMAGE)
645 cairo_surface_flush (surface);
646 cairo_surface_write_to_png_stream (surface, x_cr_accumulate_data, &acc);
648 #endif
649 unblock_input ();
651 unbind_to (count, Qnil);
653 return CALLN (Fapply, intern ("concat"), Fnreverse (acc));
656 #endif /* USE_CAIRO */
658 static void
659 x_free_cr_resources (struct frame *f)
661 #ifdef USE_CAIRO
662 if (f == NULL)
664 Lisp_Object rest, frame;
665 FOR_EACH_FRAME (rest, frame)
666 if (FRAME_X_P (XFRAME (frame)))
667 x_free_cr_resources (XFRAME (frame));
669 else
671 cairo_t *cr = FRAME_CR_CONTEXT (f);
673 if (cr)
675 cairo_surface_t *surface = cairo_get_target (cr);
677 if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_XLIB)
679 cairo_destroy (cr);
680 FRAME_CR_CONTEXT (f) = NULL;
684 #endif
687 static void
688 x_set_clip_rectangles (struct frame *f, GC gc, XRectangle *rectangles, int n)
690 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, rectangles, n, Unsorted);
691 #ifdef USE_CAIRO
692 eassert (n >= 0 && n <= MAX_CLIP_RECTS);
695 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 1);
697 gc_ext->n_clip_rects = n;
698 memcpy (gc_ext->clip_rects, rectangles, sizeof (XRectangle) * n);
700 #endif
703 static void
704 x_reset_clip_rectangles (struct frame *f, GC gc)
706 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
707 #ifdef USE_CAIRO
709 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 0);
711 if (gc_ext)
712 gc_ext->n_clip_rects = 0;
714 #endif
717 static void
718 x_fill_rectangle (struct frame *f, GC gc, int x, int y, int width, int height)
720 #ifdef USE_CAIRO
721 cairo_t *cr;
723 cr = x_begin_cr_clip (f, gc);
724 x_set_cr_source_with_gc_foreground (f, gc);
725 cairo_rectangle (cr, x, y, width, height);
726 cairo_fill (cr);
727 x_end_cr_clip (f);
728 #else
729 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
730 gc, x, y, width, height);
731 #endif
734 static void
735 x_draw_rectangle (struct frame *f, GC gc, int x, int y, int width, int height)
737 #ifdef USE_CAIRO
738 cairo_t *cr;
740 cr = x_begin_cr_clip (f, gc);
741 x_set_cr_source_with_gc_foreground (f, gc);
742 cairo_rectangle (cr, x + 0.5, y + 0.5, width, height);
743 cairo_set_line_width (cr, 1);
744 cairo_stroke (cr);
745 x_end_cr_clip (f);
746 #else
747 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
748 gc, x, y, width, height);
749 #endif
752 static void
753 x_clear_window (struct frame *f)
755 #ifdef USE_CAIRO
756 cairo_t *cr;
758 cr = x_begin_cr_clip (f, NULL);
759 x_set_cr_source_with_gc_background (f, f->output_data.x->normal_gc);
760 cairo_paint (cr);
761 x_end_cr_clip (f);
762 #else
763 if (FRAME_X_DOUBLE_BUFFERED_P (f))
764 x_clear_area (f, 0, 0, FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f));
765 else
766 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
767 #endif
770 #ifdef USE_CAIRO
771 static void
772 x_fill_trapezoid_for_relief (struct frame *f, GC gc, int x, int y,
773 int width, int height, int top_p)
775 cairo_t *cr;
777 cr = x_begin_cr_clip (f, gc);
778 x_set_cr_source_with_gc_foreground (f, gc);
779 cairo_move_to (cr, top_p ? x : x + height, y);
780 cairo_line_to (cr, x, y + height);
781 cairo_line_to (cr, top_p ? x + width - height : x + width, y + height);
782 cairo_line_to (cr, x + width, y);
783 cairo_fill (cr);
784 x_end_cr_clip (f);
787 enum corners
789 CORNER_BOTTOM_RIGHT, /* 0 -> pi/2 */
790 CORNER_BOTTOM_LEFT, /* pi/2 -> pi */
791 CORNER_TOP_LEFT, /* pi -> 3pi/2 */
792 CORNER_TOP_RIGHT, /* 3pi/2 -> 2pi */
793 CORNER_LAST
796 static void
797 x_erase_corners_for_relief (struct frame *f, GC gc, int x, int y,
798 int width, int height,
799 double radius, double margin, int corners)
801 cairo_t *cr;
802 int i;
804 cr = x_begin_cr_clip (f, gc);
805 x_set_cr_source_with_gc_background (f, gc);
806 for (i = 0; i < CORNER_LAST; i++)
807 if (corners & (1 << i))
809 double xm, ym, xc, yc;
811 if (i == CORNER_TOP_LEFT || i == CORNER_BOTTOM_LEFT)
812 xm = x - margin, xc = xm + radius;
813 else
814 xm = x + width + margin, xc = xm - radius;
815 if (i == CORNER_TOP_LEFT || i == CORNER_TOP_RIGHT)
816 ym = y - margin, yc = ym + radius;
817 else
818 ym = y + height + margin, yc = ym - radius;
820 cairo_move_to (cr, xm, ym);
821 cairo_arc (cr, xc, yc, radius, i * M_PI_2, (i + 1) * M_PI_2);
823 cairo_clip (cr);
824 cairo_rectangle (cr, x, y, width, height);
825 cairo_fill (cr);
826 x_end_cr_clip (f);
829 static void
830 x_draw_horizontal_wave (struct frame *f, GC gc, int x, int y,
831 int width, int height, int wave_length)
833 cairo_t *cr;
834 double dx = wave_length, dy = height - 1;
835 int xoffset, n;
837 cr = x_begin_cr_clip (f, gc);
838 x_set_cr_source_with_gc_foreground (f, gc);
839 cairo_rectangle (cr, x, y, width, height);
840 cairo_clip (cr);
842 if (x >= 0)
844 xoffset = x % (wave_length * 2);
845 if (xoffset == 0)
846 xoffset = wave_length * 2;
848 else
849 xoffset = x % (wave_length * 2) + wave_length * 2;
850 n = (width + xoffset) / wave_length + 1;
851 if (xoffset > wave_length)
853 xoffset -= wave_length;
854 --n;
855 y += height - 1;
856 dy = -dy;
859 cairo_move_to (cr, x - xoffset + 0.5, y + 0.5);
860 while (--n >= 0)
862 cairo_rel_line_to (cr, dx, dy);
863 dy = -dy;
865 cairo_set_line_width (cr, 1);
866 cairo_stroke (cr);
867 x_end_cr_clip (f);
869 #endif
872 /* Return the struct x_display_info corresponding to DPY. */
874 struct x_display_info *
875 x_display_info_for_display (Display *dpy)
877 struct x_display_info *dpyinfo;
879 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
880 if (dpyinfo->display == dpy)
881 return dpyinfo;
883 return 0;
886 static Window
887 x_find_topmost_parent (struct frame *f)
889 struct x_output *x = f->output_data.x;
890 Window win = None, wi = x->parent_desc;
891 Display *dpy = FRAME_X_DISPLAY (f);
893 while (wi != FRAME_DISPLAY_INFO (f)->root_window)
895 Window root;
896 Window *children;
897 unsigned int nchildren;
899 win = wi;
900 if (XQueryTree (dpy, win, &root, &wi, &children, &nchildren))
901 XFree (children);
902 else
903 break;
906 return win;
909 #define OPAQUE 0xffffffff
911 void
912 x_set_frame_alpha (struct frame *f)
914 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
915 Display *dpy = FRAME_X_DISPLAY (f);
916 Window win = FRAME_OUTER_WINDOW (f);
917 double alpha = 1.0;
918 double alpha_min = 1.0;
919 unsigned long opac;
920 Window parent;
922 if (dpyinfo->x_highlight_frame == f)
923 alpha = f->alpha[0];
924 else
925 alpha = f->alpha[1];
927 if (FLOATP (Vframe_alpha_lower_limit))
928 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
929 else if (INTEGERP (Vframe_alpha_lower_limit))
930 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
932 if (alpha < 0.0)
933 return;
934 else if (alpha > 1.0)
935 alpha = 1.0;
936 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
937 alpha = alpha_min;
939 opac = alpha * OPAQUE;
941 x_catch_errors (dpy);
943 /* If there is a parent from the window manager, put the property there
944 also, to work around broken window managers that fail to do that.
945 Do this unconditionally as this function is called on reparent when
946 alpha has not changed on the frame. */
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);
954 /* return unless necessary */
956 unsigned char *data;
957 Atom actual;
958 int rc, format;
959 unsigned long n, left;
961 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
962 0, 1, False, XA_CARDINAL,
963 &actual, &format, &n, &left,
964 &data);
966 if (rc == Success && actual != None)
968 unsigned long value = *(unsigned long *)data;
969 XFree (data);
970 if (value == opac)
972 x_uncatch_errors ();
973 return;
978 XChangeProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
979 XA_CARDINAL, 32, PropModeReplace,
980 (unsigned char *) &opac, 1);
981 x_uncatch_errors ();
984 /***********************************************************************
985 Starting and ending an update
986 ***********************************************************************/
988 /* Start an update of frame F. This function is installed as a hook
989 for update_begin, i.e. it is called when update_begin is called.
990 This function is called prior to calls to x_update_window_begin for
991 each window being updated. Currently, there is nothing to do here
992 because all interesting stuff is done on a window basis. */
994 static void
995 x_update_begin (struct frame *f)
997 #ifdef USE_CAIRO
998 if (! NILP (tip_frame) && XFRAME (tip_frame) == f
999 && ! FRAME_VISIBLE_P (f))
1000 return;
1002 if (! FRAME_CR_SURFACE (f))
1004 int width, height;
1005 #ifdef USE_GTK
1006 if (FRAME_GTK_WIDGET (f))
1008 GdkWindow *w = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
1009 width = gdk_window_get_width (w);
1010 height = gdk_window_get_height (w);
1012 else
1013 #endif
1015 width = FRAME_PIXEL_WIDTH (f);
1016 height = FRAME_PIXEL_HEIGHT (f);
1017 if (! FRAME_EXTERNAL_TOOL_BAR (f))
1018 height += FRAME_TOOL_BAR_HEIGHT (f);
1019 if (! FRAME_EXTERNAL_MENU_BAR (f))
1020 height += FRAME_MENU_BAR_HEIGHT (f);
1023 if (width > 0 && height > 0)
1025 block_input();
1026 FRAME_CR_SURFACE (f) = cairo_image_surface_create
1027 (CAIRO_FORMAT_ARGB32, width, height);
1028 unblock_input();
1031 #endif /* USE_CAIRO */
1034 /* Start update of window W. */
1036 static void
1037 x_update_window_begin (struct window *w)
1039 struct frame *f = XFRAME (WINDOW_FRAME (w));
1040 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
1042 w->output_cursor = w->cursor;
1044 block_input ();
1046 if (f == hlinfo->mouse_face_mouse_frame)
1048 /* Don't do highlighting for mouse motion during the update. */
1049 hlinfo->mouse_face_defer = true;
1051 /* If F needs to be redrawn, simply forget about any prior mouse
1052 highlighting. */
1053 if (FRAME_GARBAGED_P (f))
1054 hlinfo->mouse_face_window = Qnil;
1057 unblock_input ();
1061 /* Draw a vertical window border from (x,y0) to (x,y1) */
1063 static void
1064 x_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
1066 struct frame *f = XFRAME (WINDOW_FRAME (w));
1067 struct face *face;
1069 face = FACE_FROM_ID_OR_NULL (f, VERTICAL_BORDER_FACE_ID);
1070 if (face)
1071 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
1072 face->foreground);
1074 #ifdef USE_CAIRO
1075 x_fill_rectangle (f, f->output_data.x->normal_gc, x, y0, 1, y1 - y0);
1076 #else
1077 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
1078 f->output_data.x->normal_gc, x, y0, x, y1);
1079 #endif
1082 /* Draw a window divider from (x0,y0) to (x1,y1) */
1084 static void
1085 x_draw_window_divider (struct window *w, int x0, int x1, int y0, int y1)
1087 struct frame *f = XFRAME (WINDOW_FRAME (w));
1088 struct face *face = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_FACE_ID);
1089 struct face *face_first
1090 = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
1091 struct face *face_last
1092 = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
1093 unsigned long color = face ? face->foreground : FRAME_FOREGROUND_PIXEL (f);
1094 unsigned long color_first = (face_first
1095 ? face_first->foreground
1096 : FRAME_FOREGROUND_PIXEL (f));
1097 unsigned long color_last = (face_last
1098 ? face_last->foreground
1099 : FRAME_FOREGROUND_PIXEL (f));
1100 Display *display = FRAME_X_DISPLAY (f);
1102 if (y1 - y0 > x1 - x0 && x1 - x0 > 2)
1103 /* Vertical. */
1105 XSetForeground (display, f->output_data.x->normal_gc, color_first);
1106 x_fill_rectangle (f, f->output_data.x->normal_gc,
1107 x0, y0, 1, y1 - y0);
1108 XSetForeground (display, f->output_data.x->normal_gc, color);
1109 x_fill_rectangle (f, f->output_data.x->normal_gc,
1110 x0 + 1, y0, x1 - x0 - 2, y1 - y0);
1111 XSetForeground (display, f->output_data.x->normal_gc, color_last);
1112 x_fill_rectangle (f, f->output_data.x->normal_gc,
1113 x1 - 1, y0, 1, y1 - y0);
1115 else if (x1 - x0 > y1 - y0 && y1 - y0 > 3)
1116 /* Horizontal. */
1118 XSetForeground (display, f->output_data.x->normal_gc, color_first);
1119 x_fill_rectangle (f, f->output_data.x->normal_gc,
1120 x0, y0, x1 - x0, 1);
1121 XSetForeground (display, f->output_data.x->normal_gc, color);
1122 x_fill_rectangle (f, f->output_data.x->normal_gc,
1123 x0, y0 + 1, x1 - x0, y1 - y0 - 2);
1124 XSetForeground (display, f->output_data.x->normal_gc, color_last);
1125 x_fill_rectangle (f, f->output_data.x->normal_gc,
1126 x0, y1 - 1, x1 - x0, 1);
1128 else
1130 XSetForeground (display, f->output_data.x->normal_gc, color);
1131 x_fill_rectangle (f, f->output_data.x->normal_gc,
1132 x0, y0, x1 - x0, y1 - y0);
1136 /* End update of window W.
1138 Draw vertical borders between horizontally adjacent windows, and
1139 display W's cursor if CURSOR_ON_P is non-zero.
1141 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
1142 glyphs in mouse-face were overwritten. In that case we have to
1143 make sure that the mouse-highlight is properly redrawn.
1145 W may be a menu bar pseudo-window in case we don't have X toolkit
1146 support. Such windows don't have a cursor, so don't display it
1147 here. */
1149 static void
1150 x_update_window_end (struct window *w, bool cursor_on_p,
1151 bool mouse_face_overwritten_p)
1153 if (!w->pseudo_window_p)
1155 block_input ();
1157 if (cursor_on_p)
1158 display_and_set_cursor (w, true,
1159 w->output_cursor.hpos, w->output_cursor.vpos,
1160 w->output_cursor.x, w->output_cursor.y);
1162 if (draw_window_fringes (w, true))
1164 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
1165 x_draw_right_divider (w);
1166 else
1167 x_draw_vertical_border (w);
1170 unblock_input ();
1173 /* If a row with mouse-face was overwritten, arrange for
1174 XTframe_up_to_date to redisplay the mouse highlight. */
1175 if (mouse_face_overwritten_p)
1177 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
1179 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
1180 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
1181 hlinfo->mouse_face_window = Qnil;
1185 /* Show the frame back buffer. If frame is double-buffered,
1186 atomically publish to the user's screen graphics updates made since
1187 the last call to show_back_buffer. */
1188 static void
1189 show_back_buffer (struct frame *f)
1191 block_input ();
1192 if (FRAME_X_DOUBLE_BUFFERED_P (f))
1194 #ifdef HAVE_XDBE
1195 XdbeSwapInfo swap_info;
1196 memset (&swap_info, 0, sizeof (swap_info));
1197 swap_info.swap_window = FRAME_X_WINDOW (f);
1198 swap_info.swap_action = XdbeCopied;
1199 XdbeSwapBuffers (FRAME_X_DISPLAY (f), &swap_info, 1);
1200 #else
1201 eassert (!"should have back-buffer only with XDBE");
1202 #endif
1204 FRAME_X_NEED_BUFFER_FLIP (f) = false;
1205 unblock_input ();
1208 /* Updates back buffer and flushes changes to display. Called from
1209 minibuf read code. Note that we display the back buffer even if
1210 buffer flipping is blocked. */
1211 static void
1212 x_flip_and_flush (struct frame *f)
1214 block_input ();
1215 if (FRAME_X_NEED_BUFFER_FLIP (f))
1216 show_back_buffer (f);
1217 x_flush (f);
1218 unblock_input ();
1221 /* End update of frame F. This function is installed as a hook in
1222 update_end. */
1224 static void
1225 x_update_end (struct frame *f)
1227 /* Mouse highlight may be displayed again. */
1228 MOUSE_HL_INFO (f)->mouse_face_defer = false;
1230 #ifdef USE_CAIRO
1231 if (FRAME_CR_SURFACE (f))
1233 cairo_t *cr = 0;
1234 block_input();
1235 #if defined (USE_GTK) && defined (HAVE_GTK3)
1236 if (FRAME_GTK_WIDGET (f))
1238 GdkWindow *w = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
1239 cr = gdk_cairo_create (w);
1241 else
1242 #endif
1244 cairo_surface_t *surface;
1245 int width = FRAME_PIXEL_WIDTH (f);
1246 int height = FRAME_PIXEL_HEIGHT (f);
1247 if (! FRAME_EXTERNAL_TOOL_BAR (f))
1248 height += FRAME_TOOL_BAR_HEIGHT (f);
1249 if (! FRAME_EXTERNAL_MENU_BAR (f))
1250 height += FRAME_MENU_BAR_HEIGHT (f);
1251 surface = cairo_xlib_surface_create (FRAME_X_DISPLAY (f),
1252 FRAME_X_DRAWABLE (f),
1253 FRAME_DISPLAY_INFO (f)->visual,
1254 width,
1255 height);
1256 cr = cairo_create (surface);
1257 cairo_surface_destroy (surface);
1260 cairo_set_source_surface (cr, FRAME_CR_SURFACE (f), 0, 0);
1261 cairo_paint (cr);
1262 cairo_destroy (cr);
1263 unblock_input ();
1265 #endif
1267 #ifndef XFlush
1268 block_input ();
1269 XFlush (FRAME_X_DISPLAY (f));
1270 unblock_input ();
1271 #endif
1274 /* This function is called from various places in xdisp.c
1275 whenever a complete update has been performed. */
1277 static void
1278 XTframe_up_to_date (struct frame *f)
1280 eassert (FRAME_X_P (f));
1281 block_input ();
1282 FRAME_MOUSE_UPDATE (f);
1283 if (!buffer_flipping_blocked_p () && FRAME_X_NEED_BUFFER_FLIP (f))
1284 show_back_buffer (f);
1285 unblock_input ();
1288 static void
1289 XTbuffer_flipping_unblocked_hook (struct frame *f)
1291 if (FRAME_X_NEED_BUFFER_FLIP (f))
1292 show_back_buffer (f);
1295 /* Clear under internal border if any (GTK has its own version). */
1296 #ifndef USE_GTK
1297 void
1298 x_clear_under_internal_border (struct frame *f)
1300 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
1302 int border = FRAME_INTERNAL_BORDER_WIDTH (f);
1303 int width = FRAME_PIXEL_WIDTH (f);
1304 int height = FRAME_PIXEL_HEIGHT (f);
1305 int margin = FRAME_TOP_MARGIN_HEIGHT (f);
1307 block_input ();
1308 x_clear_area (f, 0, 0, border, height);
1309 x_clear_area (f, 0, margin, width, border);
1310 x_clear_area (f, width - border, 0, border, height);
1311 x_clear_area (f, 0, height - border, width, border);
1312 unblock_input ();
1315 #endif
1317 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
1318 arrow bitmaps, or clear the fringes if no bitmaps are required
1319 before DESIRED_ROW is made current. This function is called from
1320 update_window_line only if it is known that there are differences
1321 between bitmaps to be drawn between current row and DESIRED_ROW. */
1323 static void
1324 x_after_update_window_line (struct window *w, struct glyph_row *desired_row)
1326 eassert (w);
1328 if (!desired_row->mode_line_p && !w->pseudo_window_p)
1329 desired_row->redraw_fringe_bitmaps_p = true;
1331 #ifdef USE_X_TOOLKIT
1332 /* When a window has disappeared, make sure that no rest of
1333 full-width rows stays visible in the internal border. Could
1334 check here if updated window is the leftmost/rightmost window,
1335 but I guess it's not worth doing since vertically split windows
1336 are almost never used, internal border is rarely set, and the
1337 overhead is very small. */
1339 struct frame *f;
1340 int width, height;
1342 if (windows_or_buffers_changed
1343 && desired_row->full_width_p
1344 && (f = XFRAME (w->frame),
1345 width = FRAME_INTERNAL_BORDER_WIDTH (f),
1346 width != 0)
1347 && (height = desired_row->visible_height,
1348 height > 0))
1350 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
1352 block_input ();
1353 x_clear_area (f, 0, y, width, height);
1354 x_clear_area (f, FRAME_PIXEL_WIDTH (f) - width, y, width, height);
1355 unblock_input ();
1358 #endif
1361 static void
1362 x_draw_fringe_bitmap (struct window *w, struct glyph_row *row, struct draw_fringe_bitmap_params *p)
1364 struct frame *f = XFRAME (WINDOW_FRAME (w));
1365 Display *display = FRAME_X_DISPLAY (f);
1366 GC gc = f->output_data.x->normal_gc;
1367 struct face *face = p->face;
1369 /* Must clip because of partially visible lines. */
1370 x_clip_to_row (w, row, ANY_AREA, gc);
1372 if (p->bx >= 0 && !p->overlay_p)
1374 /* In case the same realized face is used for fringes and
1375 for something displayed in the text (e.g. face `region' on
1376 mono-displays, the fill style may have been changed to
1377 FillSolid in x_draw_glyph_string_background. */
1378 if (face->stipple)
1379 XSetFillStyle (display, face->gc, FillOpaqueStippled);
1380 else
1381 XSetForeground (display, face->gc, face->background);
1383 x_fill_rectangle (f, face->gc, p->bx, p->by, p->nx, p->ny);
1385 if (!face->stipple)
1386 XSetForeground (display, face->gc, face->foreground);
1389 #ifdef USE_CAIRO
1390 if (p->which && p->which < max_fringe_bmp)
1392 XGCValues gcv;
1394 XGetGCValues (display, gc, GCForeground | GCBackground, &gcv);
1395 XSetForeground (display, gc, (p->cursor_p
1396 ? (p->overlay_p ? face->background
1397 : f->output_data.x->cursor_pixel)
1398 : face->foreground));
1399 XSetBackground (display, gc, face->background);
1400 x_cr_draw_image (f, gc, fringe_bmp[p->which], 0, p->dh,
1401 p->wd, p->h, p->x, p->y, p->overlay_p);
1402 XSetForeground (display, gc, gcv.foreground);
1403 XSetBackground (display, gc, gcv.background);
1405 #else /* not USE_CAIRO */
1406 if (p->which)
1408 Drawable drawable = FRAME_X_DRAWABLE (f);
1409 char *bits;
1410 Pixmap pixmap, clipmask = (Pixmap) 0;
1411 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
1412 XGCValues gcv;
1414 if (p->wd > 8)
1415 bits = (char *) (p->bits + p->dh);
1416 else
1417 bits = (char *) p->bits + p->dh;
1419 /* Draw the bitmap. I believe these small pixmaps can be cached
1420 by the server. */
1421 pixmap = XCreatePixmapFromBitmapData (display, drawable, bits, p->wd, p->h,
1422 (p->cursor_p
1423 ? (p->overlay_p ? face->background
1424 : f->output_data.x->cursor_pixel)
1425 : face->foreground),
1426 face->background, depth);
1428 if (p->overlay_p)
1430 clipmask = XCreatePixmapFromBitmapData (display,
1431 FRAME_DISPLAY_INFO (f)->root_window,
1432 bits, p->wd, p->h,
1433 1, 0, 1);
1434 gcv.clip_mask = clipmask;
1435 gcv.clip_x_origin = p->x;
1436 gcv.clip_y_origin = p->y;
1437 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
1440 XCopyArea (display, pixmap, drawable, gc, 0, 0,
1441 p->wd, p->h, p->x, p->y);
1442 XFreePixmap (display, pixmap);
1444 if (p->overlay_p)
1446 gcv.clip_mask = (Pixmap) 0;
1447 XChangeGC (display, gc, GCClipMask, &gcv);
1448 XFreePixmap (display, clipmask);
1451 #endif /* not USE_CAIRO */
1453 x_reset_clip_rectangles (f, gc);
1456 /***********************************************************************
1457 Glyph display
1458 ***********************************************************************/
1462 static void x_set_glyph_string_clipping (struct glyph_string *);
1463 static void x_set_glyph_string_gc (struct glyph_string *);
1464 static void x_draw_glyph_string_foreground (struct glyph_string *);
1465 static void x_draw_composite_glyph_string_foreground (struct glyph_string *);
1466 static void x_draw_glyph_string_box (struct glyph_string *);
1467 static void x_draw_glyph_string (struct glyph_string *);
1468 static _Noreturn void x_delete_glyphs (struct frame *, int);
1469 static void x_compute_glyph_string_overhangs (struct glyph_string *);
1470 static void x_set_cursor_gc (struct glyph_string *);
1471 static void x_set_mode_line_face_gc (struct glyph_string *);
1472 static void x_set_mouse_face_gc (struct glyph_string *);
1473 static bool x_alloc_lighter_color (struct frame *, Display *, Colormap,
1474 unsigned long *, double, int);
1475 static void x_setup_relief_color (struct frame *, struct relief *,
1476 double, int, unsigned long);
1477 static void x_setup_relief_colors (struct glyph_string *);
1478 static void x_draw_image_glyph_string (struct glyph_string *);
1479 static void x_draw_image_relief (struct glyph_string *);
1480 static void x_draw_image_foreground (struct glyph_string *);
1481 static void x_draw_image_foreground_1 (struct glyph_string *, Pixmap);
1482 static void x_clear_glyph_string_rect (struct glyph_string *, int,
1483 int, int, int);
1484 static void x_draw_relief_rect (struct frame *, int, int, int, int,
1485 int, bool, bool, bool, bool, bool,
1486 XRectangle *);
1487 static void x_draw_box_rect (struct glyph_string *, int, int, int, int,
1488 int, bool, bool, XRectangle *);
1489 static void x_scroll_bar_clear (struct frame *);
1491 #ifdef GLYPH_DEBUG
1492 static void x_check_font (struct frame *, struct font *);
1493 #endif
1496 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
1497 face. */
1499 static void
1500 x_set_cursor_gc (struct glyph_string *s)
1502 if (s->font == FRAME_FONT (s->f)
1503 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
1504 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
1505 && !s->cmp)
1506 s->gc = s->f->output_data.x->cursor_gc;
1507 else
1509 /* Cursor on non-default face: must merge. */
1510 XGCValues xgcv;
1511 unsigned long mask;
1513 xgcv.background = s->f->output_data.x->cursor_pixel;
1514 xgcv.foreground = s->face->background;
1516 /* If the glyph would be invisible, try a different foreground. */
1517 if (xgcv.foreground == xgcv.background)
1518 xgcv.foreground = s->face->foreground;
1519 if (xgcv.foreground == xgcv.background)
1520 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1521 if (xgcv.foreground == xgcv.background)
1522 xgcv.foreground = s->face->foreground;
1524 /* Make sure the cursor is distinct from text in this face. */
1525 if (xgcv.background == s->face->background
1526 && xgcv.foreground == s->face->foreground)
1528 xgcv.background = s->face->foreground;
1529 xgcv.foreground = s->face->background;
1532 IF_DEBUG (x_check_font (s->f, s->font));
1533 xgcv.graphics_exposures = False;
1534 mask = GCForeground | GCBackground | GCGraphicsExposures;
1536 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1537 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1538 mask, &xgcv);
1539 else
1540 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1541 = XCreateGC (s->display, FRAME_X_DRAWABLE (s->f), mask, &xgcv);
1543 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1548 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1550 static void
1551 x_set_mouse_face_gc (struct glyph_string *s)
1553 int face_id;
1554 struct face *face;
1556 /* What face has to be used last for the mouse face? */
1557 face_id = MOUSE_HL_INFO (s->f)->mouse_face_face_id;
1558 face = FACE_FROM_ID_OR_NULL (s->f, face_id);
1559 if (face == NULL)
1560 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1562 if (s->first_glyph->type == CHAR_GLYPH)
1563 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1564 else
1565 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1566 s->face = FACE_FROM_ID (s->f, face_id);
1567 prepare_face_for_display (s->f, s->face);
1569 if (s->font == s->face->font)
1570 s->gc = s->face->gc;
1571 else
1573 /* Otherwise construct scratch_cursor_gc with values from FACE
1574 except for FONT. */
1575 XGCValues xgcv;
1576 unsigned long mask;
1578 xgcv.background = s->face->background;
1579 xgcv.foreground = s->face->foreground;
1580 xgcv.graphics_exposures = False;
1581 mask = GCForeground | GCBackground | GCGraphicsExposures;
1583 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1584 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1585 mask, &xgcv);
1586 else
1587 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1588 = XCreateGC (s->display, FRAME_X_DRAWABLE (s->f), mask, &xgcv);
1590 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1593 eassert (s->gc != 0);
1597 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1598 Faces to use in the mode line have already been computed when the
1599 matrix was built, so there isn't much to do, here. */
1601 static void
1602 x_set_mode_line_face_gc (struct glyph_string *s)
1604 s->gc = s->face->gc;
1608 /* Set S->gc of glyph string S for drawing that glyph string. Set
1609 S->stippled_p to a non-zero value if the face of S has a stipple
1610 pattern. */
1612 static void
1613 x_set_glyph_string_gc (struct glyph_string *s)
1615 prepare_face_for_display (s->f, s->face);
1617 if (s->hl == DRAW_NORMAL_TEXT)
1619 s->gc = s->face->gc;
1620 s->stippled_p = s->face->stipple != 0;
1622 else if (s->hl == DRAW_INVERSE_VIDEO)
1624 x_set_mode_line_face_gc (s);
1625 s->stippled_p = s->face->stipple != 0;
1627 else if (s->hl == DRAW_CURSOR)
1629 x_set_cursor_gc (s);
1630 s->stippled_p = false;
1632 else if (s->hl == DRAW_MOUSE_FACE)
1634 x_set_mouse_face_gc (s);
1635 s->stippled_p = s->face->stipple != 0;
1637 else if (s->hl == DRAW_IMAGE_RAISED
1638 || s->hl == DRAW_IMAGE_SUNKEN)
1640 s->gc = s->face->gc;
1641 s->stippled_p = s->face->stipple != 0;
1643 else
1644 emacs_abort ();
1646 /* GC must have been set. */
1647 eassert (s->gc != 0);
1651 /* Set clipping for output of glyph string S. S may be part of a mode
1652 line or menu if we don't have X toolkit support. */
1654 static void
1655 x_set_glyph_string_clipping (struct glyph_string *s)
1657 XRectangle *r = s->clip;
1658 int n = get_glyph_string_clip_rects (s, r, 2);
1660 if (n > 0)
1661 x_set_clip_rectangles (s->f, s->gc, r, n);
1662 s->num_clips = n;
1666 /* Set SRC's clipping for output of glyph string DST. This is called
1667 when we are drawing DST's left_overhang or right_overhang only in
1668 the area of SRC. */
1670 static void
1671 x_set_glyph_string_clipping_exactly (struct glyph_string *src, struct glyph_string *dst)
1673 XRectangle r;
1675 r.x = src->x;
1676 r.width = src->width;
1677 r.y = src->y;
1678 r.height = src->height;
1679 dst->clip[0] = r;
1680 dst->num_clips = 1;
1681 x_set_clip_rectangles (dst->f, dst->gc, &r, 1);
1685 /* RIF:
1686 Compute left and right overhang of glyph string S. */
1688 static void
1689 x_compute_glyph_string_overhangs (struct glyph_string *s)
1691 if (s->cmp == NULL
1692 && (s->first_glyph->type == CHAR_GLYPH
1693 || s->first_glyph->type == COMPOSITE_GLYPH))
1695 struct font_metrics metrics;
1697 if (s->first_glyph->type == CHAR_GLYPH)
1699 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1700 struct font *font = s->font;
1701 int i;
1703 for (i = 0; i < s->nchars; i++)
1704 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1705 font->driver->text_extents (font, code, s->nchars, &metrics);
1707 else
1709 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1711 composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
1713 s->right_overhang = (metrics.rbearing > metrics.width
1714 ? metrics.rbearing - metrics.width : 0);
1715 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1717 else if (s->cmp)
1719 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1720 s->left_overhang = - s->cmp->lbearing;
1725 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1727 static void
1728 x_clear_glyph_string_rect (struct glyph_string *s, int x, int y, int w, int h)
1730 XGCValues xgcv;
1731 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1732 XSetForeground (s->display, s->gc, xgcv.background);
1733 x_fill_rectangle (s->f, s->gc, x, y, w, h);
1734 XSetForeground (s->display, s->gc, xgcv.foreground);
1738 /* Draw the background of glyph_string S. If S->background_filled_p
1739 is non-zero don't draw it. FORCE_P non-zero means draw the
1740 background even if it wouldn't be drawn normally. This is used
1741 when a string preceding S draws into the background of S, or S
1742 contains the first component of a composition. */
1744 static void
1745 x_draw_glyph_string_background (struct glyph_string *s, bool force_p)
1747 /* Nothing to do if background has already been drawn or if it
1748 shouldn't be drawn in the first place. */
1749 if (!s->background_filled_p)
1751 int box_line_width = max (s->face->box_line_width, 0);
1753 if (s->stippled_p)
1755 /* Fill background with a stipple pattern. */
1756 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1757 x_fill_rectangle (s->f, s->gc, s->x,
1758 s->y + box_line_width,
1759 s->background_width,
1760 s->height - 2 * box_line_width);
1761 XSetFillStyle (s->display, s->gc, FillSolid);
1762 s->background_filled_p = true;
1764 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1765 /* When xdisp.c ignores FONT_HEIGHT, we cannot trust
1766 font dimensions, since the actual glyphs might be
1767 much smaller. So in that case we always clear the
1768 rectangle with background color. */
1769 || FONT_TOO_HIGH (s->font)
1770 || s->font_not_found_p
1771 || s->extends_to_end_of_line_p
1772 || force_p)
1774 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1775 s->background_width,
1776 s->height - 2 * box_line_width);
1777 s->background_filled_p = true;
1783 /* Draw the foreground of glyph string S. */
1785 static void
1786 x_draw_glyph_string_foreground (struct glyph_string *s)
1788 int i, x;
1790 /* If first glyph of S has a left box line, start drawing the text
1791 of S to the right of that box line. */
1792 if (s->face->box != FACE_NO_BOX
1793 && s->first_glyph->left_box_line_p)
1794 x = s->x + eabs (s->face->box_line_width);
1795 else
1796 x = s->x;
1798 /* Draw characters of S as rectangles if S's font could not be
1799 loaded. */
1800 if (s->font_not_found_p)
1802 for (i = 0; i < s->nchars; ++i)
1804 struct glyph *g = s->first_glyph + i;
1805 x_draw_rectangle (s->f,
1806 s->gc, x, s->y, g->pixel_width - 1,
1807 s->height - 1);
1808 x += g->pixel_width;
1811 else
1813 struct font *font = s->font;
1814 int boff = font->baseline_offset;
1815 int y;
1817 if (font->vertical_centering)
1818 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1820 y = s->ybase - boff;
1821 if (s->for_overlaps
1822 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1823 font->driver->draw (s, 0, s->nchars, x, y, false);
1824 else
1825 font->driver->draw (s, 0, s->nchars, x, y, true);
1826 if (s->face->overstrike)
1827 font->driver->draw (s, 0, s->nchars, x + 1, y, false);
1831 /* Draw the foreground of composite glyph string S. */
1833 static void
1834 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1836 int i, j, x;
1837 struct font *font = s->font;
1839 /* If first glyph of S has a left box line, start drawing the text
1840 of S to the right of that box line. */
1841 if (s->face && s->face->box != FACE_NO_BOX
1842 && s->first_glyph->left_box_line_p)
1843 x = s->x + eabs (s->face->box_line_width);
1844 else
1845 x = s->x;
1847 /* S is a glyph string for a composition. S->cmp_from is the index
1848 of the first character drawn for glyphs of this composition.
1849 S->cmp_from == 0 means we are drawing the very first character of
1850 this composition. */
1852 /* Draw a rectangle for the composition if the font for the very
1853 first character of the composition could not be loaded. */
1854 if (s->font_not_found_p)
1856 if (s->cmp_from == 0)
1857 x_draw_rectangle (s->f, s->gc, x, s->y,
1858 s->width - 1, s->height - 1);
1860 else if (! s->first_glyph->u.cmp.automatic)
1862 int y = s->ybase;
1864 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1865 /* TAB in a composition means display glyphs with padding
1866 space on the left or right. */
1867 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1869 int xx = x + s->cmp->offsets[j * 2];
1870 int yy = y - s->cmp->offsets[j * 2 + 1];
1872 font->driver->draw (s, j, j + 1, xx, yy, false);
1873 if (s->face->overstrike)
1874 font->driver->draw (s, j, j + 1, xx + 1, yy, false);
1877 else
1879 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1880 Lisp_Object glyph;
1881 int y = s->ybase;
1882 int width = 0;
1884 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1886 glyph = LGSTRING_GLYPH (gstring, i);
1887 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1888 width += LGLYPH_WIDTH (glyph);
1889 else
1891 int xoff, yoff, wadjust;
1893 if (j < i)
1895 font->driver->draw (s, j, i, x, y, false);
1896 if (s->face->overstrike)
1897 font->driver->draw (s, j, i, x + 1, y, false);
1898 x += width;
1900 xoff = LGLYPH_XOFF (glyph);
1901 yoff = LGLYPH_YOFF (glyph);
1902 wadjust = LGLYPH_WADJUST (glyph);
1903 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, false);
1904 if (s->face->overstrike)
1905 font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff,
1906 false);
1907 x += wadjust;
1908 j = i + 1;
1909 width = 0;
1912 if (j < i)
1914 font->driver->draw (s, j, i, x, y, false);
1915 if (s->face->overstrike)
1916 font->driver->draw (s, j, i, x + 1, y, false);
1922 /* Draw the foreground of glyph string S for glyphless characters. */
1924 static void
1925 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1927 struct glyph *glyph = s->first_glyph;
1928 XChar2b char2b[8];
1929 int x, i, j;
1931 /* If first glyph of S has a left box line, start drawing the text
1932 of S to the right of that box line. */
1933 if (s->face && s->face->box != FACE_NO_BOX
1934 && s->first_glyph->left_box_line_p)
1935 x = s->x + eabs (s->face->box_line_width);
1936 else
1937 x = s->x;
1939 s->char2b = char2b;
1941 for (i = 0; i < s->nchars; i++, glyph++)
1943 char buf[7], *str = NULL;
1944 int len = glyph->u.glyphless.len;
1946 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1948 if (len > 0
1949 && CHAR_TABLE_P (Vglyphless_char_display)
1950 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1951 >= 1))
1953 Lisp_Object acronym
1954 = (! glyph->u.glyphless.for_no_font
1955 ? CHAR_TABLE_REF (Vglyphless_char_display,
1956 glyph->u.glyphless.ch)
1957 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
1958 if (STRINGP (acronym))
1959 str = SSDATA (acronym);
1962 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
1964 sprintf (buf, "%0*X",
1965 glyph->u.glyphless.ch < 0x10000 ? 4 : 6,
1966 glyph->u.glyphless.ch + 0u);
1967 str = buf;
1970 if (str)
1972 int upper_len = (len + 1) / 2;
1973 unsigned code;
1975 /* It is assured that all LEN characters in STR is ASCII. */
1976 for (j = 0; j < len; j++)
1978 code = s->font->driver->encode_char (s->font, str[j]);
1979 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
1981 s->font->driver->draw (s, 0, upper_len,
1982 x + glyph->slice.glyphless.upper_xoff,
1983 s->ybase + glyph->slice.glyphless.upper_yoff,
1984 false);
1985 s->font->driver->draw (s, upper_len, len,
1986 x + glyph->slice.glyphless.lower_xoff,
1987 s->ybase + glyph->slice.glyphless.lower_yoff,
1988 false);
1990 if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
1991 x_draw_rectangle (s->f, s->gc,
1992 x, s->ybase - glyph->ascent,
1993 glyph->pixel_width - 1,
1994 glyph->ascent + glyph->descent - 1);
1995 x += glyph->pixel_width;
1999 #ifdef USE_X_TOOLKIT
2001 #ifdef USE_LUCID
2003 /* Return the frame on which widget WIDGET is used.. Abort if frame
2004 cannot be determined. */
2006 static struct frame *
2007 x_frame_of_widget (Widget widget)
2009 struct x_display_info *dpyinfo;
2010 Lisp_Object tail, frame;
2011 struct frame *f;
2013 dpyinfo = x_display_info_for_display (XtDisplay (widget));
2015 /* Find the top-level shell of the widget. Note that this function
2016 can be called when the widget is not yet realized, so XtWindow
2017 (widget) == 0. That's the reason we can't simply use
2018 x_any_window_to_frame. */
2019 while (!XtIsTopLevelShell (widget))
2020 widget = XtParent (widget);
2022 /* Look for a frame with that top-level widget. Allocate the color
2023 on that frame to get the right gamma correction value. */
2024 FOR_EACH_FRAME (tail, frame)
2026 f = XFRAME (frame);
2027 if (FRAME_X_P (f)
2028 && f->output_data.nothing != 1
2029 && FRAME_DISPLAY_INFO (f) == dpyinfo
2030 && f->output_data.x->widget == widget)
2031 return f;
2033 emacs_abort ();
2036 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
2037 or DELTA. Try a color with RGB values multiplied by FACTOR first.
2038 If this produces the same color as PIXEL, try a color where all RGB
2039 values have DELTA added. Return the allocated color in *PIXEL.
2040 DISPLAY is the X display, CMAP is the colormap to operate on.
2041 Value is true if successful. */
2043 bool
2044 x_alloc_lighter_color_for_widget (Widget widget, Display *display, Colormap cmap,
2045 unsigned long *pixel, double factor, int delta)
2047 struct frame *f = x_frame_of_widget (widget);
2048 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
2051 #endif /* USE_LUCID */
2054 /* Structure specifying which arguments should be passed by Xt to
2055 cvt_string_to_pixel. We want the widget's screen and colormap. */
2057 static XtConvertArgRec cvt_string_to_pixel_args[] =
2059 {XtWidgetBaseOffset, (XtPointer) offsetof (WidgetRec, core.screen),
2060 sizeof (Screen *)},
2061 {XtWidgetBaseOffset, (XtPointer) offsetof (WidgetRec, core.colormap),
2062 sizeof (Colormap)}
2066 /* The address of this variable is returned by
2067 cvt_string_to_pixel. */
2069 static Pixel cvt_string_to_pixel_value;
2072 /* Convert a color name to a pixel color.
2074 DPY is the display we are working on.
2076 ARGS is an array of *NARGS XrmValue structures holding additional
2077 information about the widget for which the conversion takes place.
2078 The contents of this array are determined by the specification
2079 in cvt_string_to_pixel_args.
2081 FROM is a pointer to an XrmValue which points to the color name to
2082 convert. TO is an XrmValue in which to return the pixel color.
2084 CLOSURE_RET is a pointer to user-data, in which we record if
2085 we allocated the color or not.
2087 Value is True if successful, False otherwise. */
2089 static Boolean
2090 cvt_string_to_pixel (Display *dpy, XrmValue *args, Cardinal *nargs,
2091 XrmValue *from, XrmValue *to,
2092 XtPointer *closure_ret)
2094 Screen *screen;
2095 Colormap cmap;
2096 Pixel pixel;
2097 String color_name;
2098 XColor color;
2100 if (*nargs != 2)
2102 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
2103 "wrongParameters", "cvt_string_to_pixel",
2104 "XtToolkitError",
2105 "Screen and colormap args required", NULL, NULL);
2106 return False;
2109 screen = *(Screen **) args[0].addr;
2110 cmap = *(Colormap *) args[1].addr;
2111 color_name = (String) from->addr;
2113 if (strcmp (color_name, XtDefaultBackground) == 0)
2115 *closure_ret = (XtPointer) False;
2116 pixel = WhitePixelOfScreen (screen);
2118 else if (strcmp (color_name, XtDefaultForeground) == 0)
2120 *closure_ret = (XtPointer) False;
2121 pixel = BlackPixelOfScreen (screen);
2123 else if (XParseColor (dpy, cmap, color_name, &color)
2124 && x_alloc_nearest_color_1 (dpy, cmap, &color))
2126 pixel = color.pixel;
2127 *closure_ret = (XtPointer) True;
2129 else
2131 String params[1];
2132 Cardinal nparams = 1;
2134 params[0] = color_name;
2135 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
2136 "badValue", "cvt_string_to_pixel",
2137 "XtToolkitError", "Invalid color '%s'",
2138 params, &nparams);
2139 return False;
2142 if (to->addr != NULL)
2144 if (to->size < sizeof (Pixel))
2146 to->size = sizeof (Pixel);
2147 return False;
2150 *(Pixel *) to->addr = pixel;
2152 else
2154 cvt_string_to_pixel_value = pixel;
2155 to->addr = (XtPointer) &cvt_string_to_pixel_value;
2158 to->size = sizeof (Pixel);
2159 return True;
2163 /* Free a pixel color which was previously allocated via
2164 cvt_string_to_pixel. This is registered as the destructor
2165 for this type of resource via XtSetTypeConverter.
2167 APP is the application context in which we work.
2169 TO is a pointer to an XrmValue holding the color to free.
2170 CLOSURE is the value we stored in CLOSURE_RET for this color
2171 in cvt_string_to_pixel.
2173 ARGS and NARGS are like for cvt_string_to_pixel. */
2175 static void
2176 cvt_pixel_dtor (XtAppContext app, XrmValuePtr to, XtPointer closure, XrmValuePtr args,
2177 Cardinal *nargs)
2179 if (*nargs != 2)
2181 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
2182 "XtToolkitError",
2183 "Screen and colormap arguments required",
2184 NULL, NULL);
2186 else if (closure != NULL)
2188 /* We did allocate the pixel, so free it. */
2189 Screen *screen = *(Screen **) args[0].addr;
2190 Colormap cmap = *(Colormap *) args[1].addr;
2191 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
2192 (Pixel *) to->addr, 1);
2197 #endif /* USE_X_TOOLKIT */
2200 /* Value is an array of XColor structures for the contents of the
2201 color map of display DPY. Set *NCELLS to the size of the array.
2202 Note that this probably shouldn't be called for large color maps,
2203 say a 24-bit TrueColor map. */
2205 static const XColor *
2206 x_color_cells (Display *dpy, int *ncells)
2208 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
2209 eassume (dpyinfo);
2211 if (dpyinfo->color_cells == NULL)
2213 Screen *screen = dpyinfo->screen;
2214 int ncolor_cells = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
2215 int i;
2217 dpyinfo->color_cells = xnmalloc (ncolor_cells,
2218 sizeof *dpyinfo->color_cells);
2219 dpyinfo->ncolor_cells = ncolor_cells;
2221 for (i = 0; i < ncolor_cells; ++i)
2222 dpyinfo->color_cells[i].pixel = i;
2224 XQueryColors (dpy, dpyinfo->cmap,
2225 dpyinfo->color_cells, ncolor_cells);
2228 *ncells = dpyinfo->ncolor_cells;
2229 return dpyinfo->color_cells;
2233 /* On frame F, translate pixel colors to RGB values for the NCOLORS
2234 colors in COLORS. Use cached information, if available. */
2236 void
2237 x_query_colors (struct frame *f, XColor *colors, int ncolors)
2239 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2241 if (dpyinfo->red_bits > 0)
2243 /* For TrueColor displays, we can decompose the RGB value
2244 directly. */
2245 int i;
2246 unsigned int rmult, gmult, bmult;
2247 unsigned int rmask, gmask, bmask;
2249 rmask = (1 << dpyinfo->red_bits) - 1;
2250 gmask = (1 << dpyinfo->green_bits) - 1;
2251 bmask = (1 << dpyinfo->blue_bits) - 1;
2252 /* If we're widening, for example, 8 bits in the pixel value to
2253 16 bits for the separate-color representation, we want to
2254 extrapolate the lower bits based on those bits available --
2255 in other words, we'd like 0xff to become 0xffff instead of
2256 the 0xff00 we'd get by just zero-filling the lower bits.
2258 We generate a 32-bit scaled-up value and shift it, in case
2259 the bit count doesn't divide 16 evenly (e.g., when dealing
2260 with a 3-3-2 bit RGB display), to get more of the lower bits
2261 correct.
2263 Should we cache the multipliers in dpyinfo? Maybe
2264 special-case the 8-8-8 common case? */
2265 rmult = 0xffffffff / rmask;
2266 gmult = 0xffffffff / gmask;
2267 bmult = 0xffffffff / bmask;
2269 for (i = 0; i < ncolors; ++i)
2271 unsigned int r, g, b;
2272 unsigned long pixel = colors[i].pixel;
2274 r = (pixel >> dpyinfo->red_offset) & rmask;
2275 g = (pixel >> dpyinfo->green_offset) & gmask;
2276 b = (pixel >> dpyinfo->blue_offset) & bmask;
2278 colors[i].red = (r * rmult) >> 16;
2279 colors[i].green = (g * gmult) >> 16;
2280 colors[i].blue = (b * bmult) >> 16;
2282 return;
2285 if (dpyinfo->color_cells)
2287 int i;
2288 for (i = 0; i < ncolors; ++i)
2290 unsigned long pixel = colors[i].pixel;
2291 eassert (pixel < dpyinfo->ncolor_cells);
2292 eassert (dpyinfo->color_cells[pixel].pixel == pixel);
2293 colors[i] = dpyinfo->color_cells[pixel];
2295 return;
2298 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
2302 /* On frame F, translate pixel color to RGB values for the color in
2303 COLOR. Use cached information, if available. */
2305 void
2306 x_query_color (struct frame *f, XColor *color)
2308 x_query_colors (f, color, 1);
2312 /* On frame F, translate the color name to RGB values. Use cached
2313 information, if possible.
2315 Note that there is currently no way to clean old entries out of the
2316 cache. However, it is limited to names in the server's database,
2317 and names we've actually looked up; list-colors-display is probably
2318 the most color-intensive case we're likely to hit. */
2320 Status x_parse_color (struct frame *f, const char *color_name,
2321 XColor *color)
2323 Display *dpy = FRAME_X_DISPLAY (f);
2324 Colormap cmap = FRAME_X_COLORMAP (f);
2325 struct color_name_cache_entry *cache_entry;
2327 if (color_name[0] == '#')
2329 /* The hex form is parsed directly by XParseColor without
2330 talking to the X server. No need for caching. */
2331 return XParseColor (dpy, cmap, color_name, color);
2334 for (cache_entry = FRAME_DISPLAY_INFO (f)->color_names; cache_entry;
2335 cache_entry = cache_entry->next)
2337 if (!xstrcasecmp(cache_entry->name, color_name))
2339 *color = cache_entry->rgb;
2340 return 1;
2344 if (XParseColor (dpy, cmap, color_name, color) == 0)
2345 /* No caching of negative results, currently. */
2346 return 0;
2348 cache_entry = xzalloc (sizeof *cache_entry);
2349 cache_entry->rgb = *color;
2350 cache_entry->name = xstrdup (color_name);
2351 cache_entry->next = FRAME_DISPLAY_INFO (f)->color_names;
2352 FRAME_DISPLAY_INFO (f)->color_names = cache_entry;
2353 return 1;
2357 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
2358 exact match can't be allocated, try the nearest color available.
2359 Value is true if successful. Set *COLOR to the color
2360 allocated. */
2362 static bool
2363 x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color)
2365 bool rc;
2367 rc = XAllocColor (dpy, cmap, color) != 0;
2368 if (rc == 0)
2370 /* If we got to this point, the colormap is full, so we're going
2371 to try to get the next closest color. The algorithm used is
2372 a least-squares matching, which is what X uses for closest
2373 color matching with StaticColor visuals. */
2374 int nearest, i;
2375 int max_color_delta = 255;
2376 int max_delta = 3 * max_color_delta;
2377 int nearest_delta = max_delta + 1;
2378 int ncells;
2379 const XColor *cells = x_color_cells (dpy, &ncells);
2381 for (nearest = i = 0; i < ncells; ++i)
2383 int dred = (color->red >> 8) - (cells[i].red >> 8);
2384 int dgreen = (color->green >> 8) - (cells[i].green >> 8);
2385 int dblue = (color->blue >> 8) - (cells[i].blue >> 8);
2386 int delta = dred * dred + dgreen * dgreen + dblue * dblue;
2388 if (delta < nearest_delta)
2390 nearest = i;
2391 nearest_delta = delta;
2395 color->red = cells[nearest].red;
2396 color->green = cells[nearest].green;
2397 color->blue = cells[nearest].blue;
2398 rc = XAllocColor (dpy, cmap, color) != 0;
2400 else
2402 /* If allocation succeeded, and the allocated pixel color is not
2403 equal to a cached pixel color recorded earlier, there was a
2404 change in the colormap, so clear the color cache. */
2405 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
2406 eassume (dpyinfo);
2408 if (dpyinfo->color_cells)
2410 XColor *cached_color = &dpyinfo->color_cells[color->pixel];
2411 if (cached_color->red != color->red
2412 || cached_color->blue != color->blue
2413 || cached_color->green != color->green)
2415 xfree (dpyinfo->color_cells);
2416 dpyinfo->color_cells = NULL;
2417 dpyinfo->ncolor_cells = 0;
2422 #ifdef DEBUG_X_COLORS
2423 if (rc)
2424 register_color (color->pixel);
2425 #endif /* DEBUG_X_COLORS */
2427 return rc;
2431 /* Allocate the color COLOR->pixel on frame F, colormap CMAP, after
2432 gamma correction. If an exact match can't be allocated, try the
2433 nearest color available. Value is true if successful. Set *COLOR
2434 to the color allocated. */
2436 bool
2437 x_alloc_nearest_color (struct frame *f, Colormap cmap, XColor *color)
2439 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2441 gamma_correct (f, color);
2443 if (dpyinfo->red_bits > 0)
2445 color->pixel = x_make_truecolor_pixel (dpyinfo,
2446 color->red,
2447 color->green,
2448 color->blue);
2449 return true;
2452 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
2456 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
2457 It's necessary to do this instead of just using PIXEL directly to
2458 get color reference counts right. */
2460 unsigned long
2461 x_copy_color (struct frame *f, unsigned long pixel)
2463 XColor color;
2465 /* If display has an immutable color map, freeing colors is not
2466 necessary and some servers don't allow it. Since we won't free a
2467 color once we've allocated it, we don't need to re-allocate it to
2468 maintain the server's reference count. */
2469 if (!x_mutable_colormap (FRAME_X_VISUAL (f)))
2470 return pixel;
2472 color.pixel = pixel;
2473 block_input ();
2474 /* The color could still be found in the color_cells array. */
2475 x_query_color (f, &color);
2476 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
2477 unblock_input ();
2478 #ifdef DEBUG_X_COLORS
2479 register_color (pixel);
2480 #endif
2481 return color.pixel;
2485 /* Brightness beyond which a color won't have its highlight brightness
2486 boosted.
2488 Nominally, highlight colors for `3d' faces are calculated by
2489 brightening an object's color by a constant scale factor, but this
2490 doesn't yield good results for dark colors, so for colors who's
2491 brightness is less than this value (on a scale of 0-65535) have an
2492 use an additional additive factor.
2494 The value here is set so that the default menu-bar/mode-line color
2495 (grey75) will not have its highlights changed at all. */
2496 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
2499 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
2500 or DELTA. Try a color with RGB values multiplied by FACTOR first.
2501 If this produces the same color as PIXEL, try a color where all RGB
2502 values have DELTA added. Return the allocated color in *PIXEL.
2503 DISPLAY is the X display, CMAP is the colormap to operate on.
2504 Value is non-zero if successful. */
2506 static bool
2507 x_alloc_lighter_color (struct frame *f, Display *display, Colormap cmap,
2508 unsigned long *pixel, double factor, int delta)
2510 XColor color, new;
2511 long bright;
2512 bool success_p;
2514 /* Get RGB color values. */
2515 color.pixel = *pixel;
2516 x_query_color (f, &color);
2518 /* Change RGB values by specified FACTOR. Avoid overflow! */
2519 eassert (factor >= 0);
2520 new.red = min (0xffff, factor * color.red);
2521 new.green = min (0xffff, factor * color.green);
2522 new.blue = min (0xffff, factor * color.blue);
2524 /* Calculate brightness of COLOR. */
2525 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
2527 /* We only boost colors that are darker than
2528 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
2529 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
2530 /* Make an additive adjustment to NEW, because it's dark enough so
2531 that scaling by FACTOR alone isn't enough. */
2533 /* How far below the limit this color is (0 - 1, 1 being darker). */
2534 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
2535 /* The additive adjustment. */
2536 int min_delta = delta * dimness * factor / 2;
2538 if (factor < 1)
2540 new.red = max (0, new.red - min_delta);
2541 new.green = max (0, new.green - min_delta);
2542 new.blue = max (0, new.blue - min_delta);
2544 else
2546 new.red = min (0xffff, min_delta + new.red);
2547 new.green = min (0xffff, min_delta + new.green);
2548 new.blue = min (0xffff, min_delta + new.blue);
2552 /* Try to allocate the color. */
2553 success_p = x_alloc_nearest_color (f, cmap, &new);
2554 if (success_p)
2556 if (new.pixel == *pixel)
2558 /* If we end up with the same color as before, try adding
2559 delta to the RGB values. */
2560 x_free_colors (f, &new.pixel, 1);
2562 new.red = min (0xffff, delta + color.red);
2563 new.green = min (0xffff, delta + color.green);
2564 new.blue = min (0xffff, delta + color.blue);
2565 success_p = x_alloc_nearest_color (f, cmap, &new);
2567 else
2568 success_p = true;
2569 *pixel = new.pixel;
2572 return success_p;
2576 /* Set up the foreground color for drawing relief lines of glyph
2577 string S. RELIEF is a pointer to a struct relief containing the GC
2578 with which lines will be drawn. Use a color that is FACTOR or
2579 DELTA lighter or darker than the relief's background which is found
2580 in S->f->output_data.x->relief_background. If such a color cannot
2581 be allocated, use DEFAULT_PIXEL, instead. */
2583 static void
2584 x_setup_relief_color (struct frame *f, struct relief *relief, double factor,
2585 int delta, unsigned long default_pixel)
2587 XGCValues xgcv;
2588 struct x_output *di = f->output_data.x;
2589 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
2590 unsigned long pixel;
2591 unsigned long background = di->relief_background;
2592 Colormap cmap = FRAME_X_COLORMAP (f);
2593 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2594 Display *dpy = FRAME_X_DISPLAY (f);
2596 xgcv.graphics_exposures = False;
2597 xgcv.line_width = 1;
2599 /* Free previously allocated color. The color cell will be reused
2600 when it has been freed as many times as it was allocated, so this
2601 doesn't affect faces using the same colors. */
2602 if (relief->gc && relief->pixel != -1)
2604 x_free_colors (f, &relief->pixel, 1);
2605 relief->pixel = -1;
2608 /* Allocate new color. */
2609 xgcv.foreground = default_pixel;
2610 pixel = background;
2611 if (dpyinfo->n_planes != 1
2612 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
2613 xgcv.foreground = relief->pixel = pixel;
2615 if (relief->gc == 0)
2617 xgcv.stipple = dpyinfo->gray;
2618 mask |= GCStipple;
2619 relief->gc = XCreateGC (dpy, FRAME_X_DRAWABLE (f), mask, &xgcv);
2621 else
2622 XChangeGC (dpy, relief->gc, mask, &xgcv);
2626 /* Set up colors for the relief lines around glyph string S. */
2628 static void
2629 x_setup_relief_colors (struct glyph_string *s)
2631 struct x_output *di = s->f->output_data.x;
2632 unsigned long color;
2634 if (s->face->use_box_color_for_shadows_p)
2635 color = s->face->box_color;
2636 else if (s->first_glyph->type == IMAGE_GLYPH
2637 && s->img->pixmap
2638 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2639 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2640 else
2642 XGCValues xgcv;
2644 /* Get the background color of the face. */
2645 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2646 color = xgcv.background;
2649 if (di->white_relief.gc == 0
2650 || color != di->relief_background)
2652 di->relief_background = color;
2653 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2654 WHITE_PIX_DEFAULT (s->f));
2655 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2656 BLACK_PIX_DEFAULT (s->f));
2661 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2662 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2663 to draw, it must be >= 0. RAISED_P means draw a raised
2664 relief. LEFT_P means draw a relief on the left side of
2665 the rectangle. RIGHT_P means draw a relief on the right
2666 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2667 when drawing. */
2669 static void
2670 x_draw_relief_rect (struct frame *f,
2671 int left_x, int top_y, int right_x, int bottom_y,
2672 int width, bool raised_p, bool top_p, bool bot_p,
2673 bool left_p, bool right_p,
2674 XRectangle *clip_rect)
2676 #ifdef USE_CAIRO
2677 GC top_left_gc, bottom_right_gc;
2678 int corners = 0;
2680 if (raised_p)
2682 top_left_gc = f->output_data.x->white_relief.gc;
2683 bottom_right_gc = f->output_data.x->black_relief.gc;
2685 else
2687 top_left_gc = f->output_data.x->black_relief.gc;
2688 bottom_right_gc = f->output_data.x->white_relief.gc;
2691 x_set_clip_rectangles (f, top_left_gc, clip_rect, 1);
2692 x_set_clip_rectangles (f, bottom_right_gc, clip_rect, 1);
2694 if (left_p)
2696 x_fill_rectangle (f, top_left_gc, left_x, top_y,
2697 width, bottom_y + 1 - top_y);
2698 if (top_p)
2699 corners |= 1 << CORNER_TOP_LEFT;
2700 if (bot_p)
2701 corners |= 1 << CORNER_BOTTOM_LEFT;
2703 if (right_p)
2705 x_fill_rectangle (f, bottom_right_gc, right_x + 1 - width, top_y,
2706 width, bottom_y + 1 - top_y);
2707 if (top_p)
2708 corners |= 1 << CORNER_TOP_RIGHT;
2709 if (bot_p)
2710 corners |= 1 << CORNER_BOTTOM_RIGHT;
2712 if (top_p)
2714 if (!right_p)
2715 x_fill_rectangle (f, top_left_gc, left_x, top_y,
2716 right_x + 1 - left_x, width);
2717 else
2718 x_fill_trapezoid_for_relief (f, top_left_gc, left_x, top_y,
2719 right_x + 1 - left_x, width, 1);
2721 if (bot_p)
2723 if (!left_p)
2724 x_fill_rectangle (f, bottom_right_gc, left_x, bottom_y + 1 - width,
2725 right_x + 1 - left_x, width);
2726 else
2727 x_fill_trapezoid_for_relief (f, bottom_right_gc,
2728 left_x, bottom_y + 1 - width,
2729 right_x + 1 - left_x, width, 0);
2731 if (left_p && width != 1)
2732 x_fill_rectangle (f, bottom_right_gc, left_x, top_y,
2733 1, bottom_y + 1 - top_y);
2734 if (top_p && width != 1)
2735 x_fill_rectangle (f, bottom_right_gc, left_x, top_y,
2736 right_x + 1 - left_x, 1);
2737 if (corners)
2739 XSetBackground (FRAME_X_DISPLAY (f), top_left_gc,
2740 FRAME_BACKGROUND_PIXEL (f));
2741 x_erase_corners_for_relief (f, top_left_gc, left_x, top_y,
2742 right_x - left_x + 1, bottom_y - top_y + 1,
2743 6, 1, corners);
2746 x_reset_clip_rectangles (f, top_left_gc);
2747 x_reset_clip_rectangles (f, bottom_right_gc);
2748 #else
2749 Display *dpy = FRAME_X_DISPLAY (f);
2750 Drawable drawable = FRAME_X_DRAWABLE (f);
2751 int i;
2752 GC gc;
2754 if (raised_p)
2755 gc = f->output_data.x->white_relief.gc;
2756 else
2757 gc = f->output_data.x->black_relief.gc;
2758 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2760 /* This code is more complicated than it has to be, because of two
2761 minor hacks to make the boxes look nicer: (i) if width > 1, draw
2762 the outermost line using the black relief. (ii) Omit the four
2763 corner pixels. */
2765 /* Top. */
2766 if (top_p)
2768 if (width == 1)
2769 XDrawLine (dpy, drawable, gc,
2770 left_x + left_p, top_y,
2771 right_x + !right_p, top_y);
2773 for (i = 1; i < width; ++i)
2774 XDrawLine (dpy, drawable, gc,
2775 left_x + i * left_p, top_y + i,
2776 right_x + 1 - i * right_p, top_y + i);
2779 /* Left. */
2780 if (left_p)
2782 if (width == 1)
2783 XDrawLine (dpy, drawable, gc, left_x, top_y + 1, left_x, bottom_y);
2785 x_clear_area(f, left_x, top_y, 1, 1);
2786 x_clear_area(f, left_x, bottom_y, 1, 1);
2788 for (i = (width > 1 ? 1 : 0); i < width; ++i)
2789 XDrawLine (dpy, drawable, gc,
2790 left_x + i, top_y + (i + 1) * top_p,
2791 left_x + i, bottom_y + 1 - (i + 1) * bot_p);
2794 XSetClipMask (dpy, gc, None);
2795 if (raised_p)
2796 gc = f->output_data.x->black_relief.gc;
2797 else
2798 gc = f->output_data.x->white_relief.gc;
2799 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2801 if (width > 1)
2803 /* Outermost top line. */
2804 if (top_p)
2805 XDrawLine (dpy, drawable, gc,
2806 left_x + left_p, top_y,
2807 right_x + !right_p, top_y);
2809 /* Outermost left line. */
2810 if (left_p)
2811 XDrawLine (dpy, drawable, gc, left_x, top_y + 1, left_x, bottom_y);
2814 /* Bottom. */
2815 if (bot_p)
2817 XDrawLine (dpy, drawable, gc,
2818 left_x + left_p, bottom_y,
2819 right_x + !right_p, bottom_y);
2820 for (i = 1; i < width; ++i)
2821 XDrawLine (dpy, drawable, gc,
2822 left_x + i * left_p, bottom_y - i,
2823 right_x + 1 - i * right_p, bottom_y - i);
2826 /* Right. */
2827 if (right_p)
2829 x_clear_area(f, right_x, top_y, 1, 1);
2830 x_clear_area(f, right_x, bottom_y, 1, 1);
2831 for (i = 0; i < width; ++i)
2832 XDrawLine (dpy, drawable, gc,
2833 right_x - i, top_y + (i + 1) * top_p,
2834 right_x - i, bottom_y + 1 - (i + 1) * bot_p);
2837 x_reset_clip_rectangles (f, gc);
2839 #endif
2843 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2844 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2845 draw, it must be >= 0. LEFT_P means draw a line on the
2846 left side of the rectangle. RIGHT_P means draw a line
2847 on the right side of the rectangle. CLIP_RECT is the clipping
2848 rectangle to use when drawing. */
2850 static void
2851 x_draw_box_rect (struct glyph_string *s,
2852 int left_x, int top_y, int right_x, int bottom_y, int width,
2853 bool left_p, bool right_p, XRectangle *clip_rect)
2855 XGCValues xgcv;
2857 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2858 XSetForeground (s->display, s->gc, s->face->box_color);
2859 x_set_clip_rectangles (s->f, s->gc, clip_rect, 1);
2861 /* Top. */
2862 x_fill_rectangle (s->f, s->gc,
2863 left_x, top_y, right_x - left_x + 1, width);
2865 /* Left. */
2866 if (left_p)
2867 x_fill_rectangle (s->f, s->gc,
2868 left_x, top_y, width, bottom_y - top_y + 1);
2870 /* Bottom. */
2871 x_fill_rectangle (s->f, s->gc,
2872 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2874 /* Right. */
2875 if (right_p)
2876 x_fill_rectangle (s->f, s->gc,
2877 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2879 XSetForeground (s->display, s->gc, xgcv.foreground);
2880 x_reset_clip_rectangles (s->f, s->gc);
2884 /* Draw a box around glyph string S. */
2886 static void
2887 x_draw_glyph_string_box (struct glyph_string *s)
2889 int width, left_x, right_x, top_y, bottom_y, last_x;
2890 bool raised_p, left_p, right_p;
2891 struct glyph *last_glyph;
2892 XRectangle clip_rect;
2894 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2895 ? WINDOW_RIGHT_EDGE_X (s->w)
2896 : window_box_right (s->w, s->area));
2898 /* The glyph that may have a right box line. */
2899 last_glyph = (s->cmp || s->img
2900 ? s->first_glyph
2901 : s->first_glyph + s->nchars - 1);
2903 width = eabs (s->face->box_line_width);
2904 raised_p = s->face->box == FACE_RAISED_BOX;
2905 left_x = s->x;
2906 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2907 ? last_x - 1
2908 : min (last_x, s->x + s->background_width) - 1);
2909 top_y = s->y;
2910 bottom_y = top_y + s->height - 1;
2912 left_p = (s->first_glyph->left_box_line_p
2913 || (s->hl == DRAW_MOUSE_FACE
2914 && (s->prev == NULL
2915 || s->prev->hl != s->hl)));
2916 right_p = (last_glyph->right_box_line_p
2917 || (s->hl == DRAW_MOUSE_FACE
2918 && (s->next == NULL
2919 || s->next->hl != s->hl)));
2921 get_glyph_string_clip_rect (s, &clip_rect);
2923 if (s->face->box == FACE_SIMPLE_BOX)
2924 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2925 left_p, right_p, &clip_rect);
2926 else
2928 x_setup_relief_colors (s);
2929 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2930 width, raised_p, true, true, left_p, right_p,
2931 &clip_rect);
2936 /* Draw foreground of image glyph string S. */
2938 static void
2939 x_draw_image_foreground (struct glyph_string *s)
2941 int x = s->x;
2942 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2944 /* If first glyph of S has a left box line, start drawing it to the
2945 right of that line. */
2946 if (s->face->box != FACE_NO_BOX
2947 && s->first_glyph->left_box_line_p
2948 && s->slice.x == 0)
2949 x += eabs (s->face->box_line_width);
2951 /* If there is a margin around the image, adjust x- and y-position
2952 by that margin. */
2953 if (s->slice.x == 0)
2954 x += s->img->hmargin;
2955 if (s->slice.y == 0)
2956 y += s->img->vmargin;
2958 if (s->img->pixmap)
2960 if (s->img->mask)
2962 /* We can't set both a clip mask and use XSetClipRectangles
2963 because the latter also sets a clip mask. We also can't
2964 trust on the shape extension to be available
2965 (XShapeCombineRegion). So, compute the rectangle to draw
2966 manually. */
2967 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2968 | GCFunction);
2969 XGCValues xgcv;
2970 XRectangle clip_rect, image_rect, r;
2972 xgcv.clip_mask = s->img->mask;
2973 xgcv.clip_x_origin = x;
2974 xgcv.clip_y_origin = y;
2975 xgcv.function = GXcopy;
2976 XChangeGC (s->display, s->gc, mask, &xgcv);
2978 get_glyph_string_clip_rect (s, &clip_rect);
2979 image_rect.x = x;
2980 image_rect.y = y;
2981 image_rect.width = s->slice.width;
2982 image_rect.height = s->slice.height;
2983 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2984 XCopyArea (s->display, s->img->pixmap,
2985 FRAME_X_DRAWABLE (s->f), s->gc,
2986 s->slice.x + r.x - x, s->slice.y + r.y - y,
2987 r.width, r.height, r.x, r.y);
2989 else
2991 XRectangle clip_rect, image_rect, r;
2993 get_glyph_string_clip_rect (s, &clip_rect);
2994 image_rect.x = x;
2995 image_rect.y = y;
2996 image_rect.width = s->slice.width;
2997 image_rect.height = s->slice.height;
2998 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2999 XCopyArea (s->display, s->img->pixmap,
3000 FRAME_X_DRAWABLE (s->f), s->gc,
3001 s->slice.x + r.x - x, s->slice.y + r.y - y,
3002 r.width, r.height, r.x, r.y);
3004 /* When the image has a mask, we can expect that at
3005 least part of a mouse highlight or a block cursor will
3006 be visible. If the image doesn't have a mask, make
3007 a block cursor visible by drawing a rectangle around
3008 the image. I believe it's looking better if we do
3009 nothing here for mouse-face. */
3010 if (s->hl == DRAW_CURSOR)
3012 int relief = eabs (s->img->relief);
3013 x_draw_rectangle (s->f, s->gc,
3014 x - relief, y - relief,
3015 s->slice.width + relief*2 - 1,
3016 s->slice.height + relief*2 - 1);
3020 else
3021 /* Draw a rectangle if image could not be loaded. */
3022 x_draw_rectangle (s->f, s->gc, x, y,
3023 s->slice.width - 1, s->slice.height - 1);
3027 /* Draw a relief around the image glyph string S. */
3029 static void
3030 x_draw_image_relief (struct glyph_string *s)
3032 int x1, y1, thick;
3033 bool raised_p, top_p, bot_p, left_p, right_p;
3034 int extra_x, extra_y;
3035 XRectangle r;
3036 int x = s->x;
3037 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
3039 /* If first glyph of S has a left box line, start drawing it to the
3040 right of that line. */
3041 if (s->face->box != FACE_NO_BOX
3042 && s->first_glyph->left_box_line_p
3043 && s->slice.x == 0)
3044 x += eabs (s->face->box_line_width);
3046 /* If there is a margin around the image, adjust x- and y-position
3047 by that margin. */
3048 if (s->slice.x == 0)
3049 x += s->img->hmargin;
3050 if (s->slice.y == 0)
3051 y += s->img->vmargin;
3053 if (s->hl == DRAW_IMAGE_SUNKEN
3054 || s->hl == DRAW_IMAGE_RAISED)
3056 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
3057 raised_p = s->hl == DRAW_IMAGE_RAISED;
3059 else
3061 thick = eabs (s->img->relief);
3062 raised_p = s->img->relief > 0;
3065 x1 = x + s->slice.width - 1;
3066 y1 = y + s->slice.height - 1;
3068 extra_x = extra_y = 0;
3069 if (s->face->id == TOOL_BAR_FACE_ID)
3071 if (CONSP (Vtool_bar_button_margin)
3072 && INTEGERP (XCAR (Vtool_bar_button_margin))
3073 && INTEGERP (XCDR (Vtool_bar_button_margin)))
3075 extra_x = XINT (XCAR (Vtool_bar_button_margin));
3076 extra_y = XINT (XCDR (Vtool_bar_button_margin));
3078 else if (INTEGERP (Vtool_bar_button_margin))
3079 extra_x = extra_y = XINT (Vtool_bar_button_margin);
3082 top_p = bot_p = left_p = right_p = false;
3084 if (s->slice.x == 0)
3085 x -= thick + extra_x, left_p = true;
3086 if (s->slice.y == 0)
3087 y -= thick + extra_y, top_p = true;
3088 if (s->slice.x + s->slice.width == s->img->width)
3089 x1 += thick + extra_x, right_p = true;
3090 if (s->slice.y + s->slice.height == s->img->height)
3091 y1 += thick + extra_y, bot_p = true;
3093 x_setup_relief_colors (s);
3094 get_glyph_string_clip_rect (s, &r);
3095 x_draw_relief_rect (s->f, x, y, x1, y1, thick, raised_p,
3096 top_p, bot_p, left_p, right_p, &r);
3100 /* Draw the foreground of image glyph string S to PIXMAP. */
3102 static void
3103 x_draw_image_foreground_1 (struct glyph_string *s, Pixmap pixmap)
3105 int x = 0;
3106 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
3108 /* If first glyph of S has a left box line, start drawing it to the
3109 right of that line. */
3110 if (s->face->box != FACE_NO_BOX
3111 && s->first_glyph->left_box_line_p
3112 && s->slice.x == 0)
3113 x += eabs (s->face->box_line_width);
3115 /* If there is a margin around the image, adjust x- and y-position
3116 by that margin. */
3117 if (s->slice.x == 0)
3118 x += s->img->hmargin;
3119 if (s->slice.y == 0)
3120 y += s->img->vmargin;
3122 if (s->img->pixmap)
3124 if (s->img->mask)
3126 /* We can't set both a clip mask and use XSetClipRectangles
3127 because the latter also sets a clip mask. We also can't
3128 trust on the shape extension to be available
3129 (XShapeCombineRegion). So, compute the rectangle to draw
3130 manually. */
3131 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3132 | GCFunction);
3133 XGCValues xgcv;
3135 xgcv.clip_mask = s->img->mask;
3136 xgcv.clip_x_origin = x - s->slice.x;
3137 xgcv.clip_y_origin = y - s->slice.y;
3138 xgcv.function = GXcopy;
3139 XChangeGC (s->display, s->gc, mask, &xgcv);
3141 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3142 s->slice.x, s->slice.y,
3143 s->slice.width, s->slice.height, x, y);
3144 XSetClipMask (s->display, s->gc, None);
3146 else
3148 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3149 s->slice.x, s->slice.y,
3150 s->slice.width, s->slice.height, x, y);
3152 /* When the image has a mask, we can expect that at
3153 least part of a mouse highlight or a block cursor will
3154 be visible. If the image doesn't have a mask, make
3155 a block cursor visible by drawing a rectangle around
3156 the image. I believe it's looking better if we do
3157 nothing here for mouse-face. */
3158 if (s->hl == DRAW_CURSOR)
3160 int r = eabs (s->img->relief);
3161 x_draw_rectangle (s->f, s->gc, x - r, y - r,
3162 s->slice.width + r*2 - 1,
3163 s->slice.height + r*2 - 1);
3167 else
3168 /* Draw a rectangle if image could not be loaded. */
3169 x_draw_rectangle (s->f, s->gc, x, y,
3170 s->slice.width - 1, s->slice.height - 1);
3174 /* Draw part of the background of glyph string S. X, Y, W, and H
3175 give the rectangle to draw. */
3177 static void
3178 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
3180 if (s->stippled_p)
3182 /* Fill background with a stipple pattern. */
3183 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3184 x_fill_rectangle (s->f, s->gc, x, y, w, h);
3185 XSetFillStyle (s->display, s->gc, FillSolid);
3187 else
3188 x_clear_glyph_string_rect (s, x, y, w, h);
3192 /* Draw image glyph string S.
3194 s->y
3195 s->x +-------------------------
3196 | s->face->box
3198 | +-------------------------
3199 | | s->img->margin
3201 | | +-------------------
3202 | | | the image
3206 static void
3207 x_draw_image_glyph_string (struct glyph_string *s)
3209 int box_line_hwidth = eabs (s->face->box_line_width);
3210 int box_line_vwidth = max (s->face->box_line_width, 0);
3211 int height;
3212 Pixmap pixmap = None;
3214 height = s->height;
3215 if (s->slice.y == 0)
3216 height -= box_line_vwidth;
3217 if (s->slice.y + s->slice.height >= s->img->height)
3218 height -= box_line_vwidth;
3220 /* Fill background with face under the image. Do it only if row is
3221 taller than image or if image has a clip mask to reduce
3222 flickering. */
3223 s->stippled_p = s->face->stipple != 0;
3224 if (height > s->slice.height
3225 || s->img->hmargin
3226 || s->img->vmargin
3227 || s->img->mask
3228 || s->img->pixmap == 0
3229 || s->width != s->background_width)
3231 if (s->img->mask)
3233 /* Create a pixmap as large as the glyph string. Fill it
3234 with the background color. Copy the image to it, using
3235 its mask. Copy the temporary pixmap to the display. */
3236 Screen *screen = FRAME_X_SCREEN (s->f);
3237 int depth = DefaultDepthOfScreen (screen);
3239 /* Create a pixmap as large as the glyph string. */
3240 pixmap = XCreatePixmap (s->display, FRAME_X_DRAWABLE (s->f),
3241 s->background_width,
3242 s->height, depth);
3244 /* Don't clip in the following because we're working on the
3245 pixmap. */
3246 XSetClipMask (s->display, s->gc, None);
3248 /* Fill the pixmap with the background color/stipple. */
3249 if (s->stippled_p)
3251 /* Fill background with a stipple pattern. */
3252 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3253 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
3254 XFillRectangle (s->display, pixmap, s->gc,
3255 0, 0, s->background_width, s->height);
3256 XSetFillStyle (s->display, s->gc, FillSolid);
3257 XSetTSOrigin (s->display, s->gc, 0, 0);
3259 else
3261 XGCValues xgcv;
3262 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3263 &xgcv);
3264 XSetForeground (s->display, s->gc, xgcv.background);
3265 XFillRectangle (s->display, pixmap, s->gc,
3266 0, 0, s->background_width, s->height);
3267 XSetForeground (s->display, s->gc, xgcv.foreground);
3270 else
3272 int x = s->x;
3273 int y = s->y;
3274 int width = s->background_width;
3276 if (s->first_glyph->left_box_line_p
3277 && s->slice.x == 0)
3279 x += box_line_hwidth;
3280 width -= box_line_hwidth;
3283 if (s->slice.y == 0)
3284 y += box_line_vwidth;
3286 x_draw_glyph_string_bg_rect (s, x, y, width, height);
3289 s->background_filled_p = true;
3292 /* Draw the foreground. */
3293 #ifdef USE_CAIRO
3294 if (s->img->cr_data)
3296 cairo_t *cr = x_begin_cr_clip (s->f, s->gc);
3298 int x = s->x + s->img->hmargin;
3299 int y = s->y + s->img->vmargin;
3300 int width = s->background_width;
3302 cairo_set_source_surface (cr, s->img->cr_data,
3303 x - s->slice.x,
3304 y - s->slice.y);
3305 cairo_rectangle (cr, x, y, width, height);
3306 cairo_fill (cr);
3307 x_end_cr_clip (s->f);
3309 else
3310 #endif
3311 if (pixmap != None)
3313 x_draw_image_foreground_1 (s, pixmap);
3314 x_set_glyph_string_clipping (s);
3315 XCopyArea (s->display, pixmap, FRAME_X_DRAWABLE (s->f), s->gc,
3316 0, 0, s->background_width, s->height, s->x, s->y);
3317 XFreePixmap (s->display, pixmap);
3319 else
3320 x_draw_image_foreground (s);
3322 /* If we must draw a relief around the image, do it. */
3323 if (s->img->relief
3324 || s->hl == DRAW_IMAGE_RAISED
3325 || s->hl == DRAW_IMAGE_SUNKEN)
3326 x_draw_image_relief (s);
3330 /* Draw stretch glyph string S. */
3332 static void
3333 x_draw_stretch_glyph_string (struct glyph_string *s)
3335 eassert (s->first_glyph->type == STRETCH_GLYPH);
3337 if (s->hl == DRAW_CURSOR
3338 && !x_stretch_cursor_p)
3340 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
3341 wide as the stretch glyph. */
3342 int width, background_width = s->background_width;
3343 int x = s->x;
3345 if (!s->row->reversed_p)
3347 int left_x = window_box_left_offset (s->w, TEXT_AREA);
3349 if (x < left_x)
3351 background_width -= left_x - x;
3352 x = left_x;
3355 else
3357 /* In R2L rows, draw the cursor on the right edge of the
3358 stretch glyph. */
3359 int right_x = window_box_right (s->w, TEXT_AREA);
3361 if (x + background_width > right_x)
3362 background_width -= x - right_x;
3363 x += background_width;
3365 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
3366 if (s->row->reversed_p)
3367 x -= width;
3369 /* Draw cursor. */
3370 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
3372 /* Clear rest using the GC of the original non-cursor face. */
3373 if (width < background_width)
3375 int y = s->y;
3376 int w = background_width - width, h = s->height;
3377 XRectangle r;
3378 GC gc;
3380 if (!s->row->reversed_p)
3381 x += width;
3382 else
3383 x = s->x;
3384 if (s->row->mouse_face_p
3385 && cursor_in_mouse_face_p (s->w))
3387 x_set_mouse_face_gc (s);
3388 gc = s->gc;
3390 else
3391 gc = s->face->gc;
3393 get_glyph_string_clip_rect (s, &r);
3394 x_set_clip_rectangles (s->f, gc, &r, 1);
3396 if (s->face->stipple)
3398 /* Fill background with a stipple pattern. */
3399 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3400 x_fill_rectangle (s->f, gc, x, y, w, h);
3401 XSetFillStyle (s->display, gc, FillSolid);
3403 else
3405 XGCValues xgcv;
3406 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3407 XSetForeground (s->display, gc, xgcv.background);
3408 x_fill_rectangle (s->f, gc, x, y, w, h);
3409 XSetForeground (s->display, gc, xgcv.foreground);
3412 x_reset_clip_rectangles (s->f, gc);
3415 else if (!s->background_filled_p)
3417 int background_width = s->background_width;
3418 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
3420 /* Don't draw into left margin, fringe or scrollbar area
3421 except for header line and mode line. */
3422 if (x < left_x && !s->row->mode_line_p)
3424 background_width -= left_x - x;
3425 x = left_x;
3427 if (background_width > 0)
3428 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
3431 s->background_filled_p = true;
3435 Draw a wavy line under S. The wave fills wave_height pixels from y0.
3437 x0 wave_length = 2
3439 y0 * * * * *
3440 |* * * * * * * * *
3441 wave_height = 3 | * * * *
3445 static void
3446 x_draw_underwave (struct glyph_string *s)
3448 int wave_height = 3, wave_length = 2;
3449 #ifdef USE_CAIRO
3450 x_draw_horizontal_wave (s->f, s->gc, s->x, s->ybase - wave_height + 3,
3451 s->width, wave_height, wave_length);
3452 #else /* not USE_CAIRO */
3453 int dx, dy, x0, y0, width, x1, y1, x2, y2, xmax;
3454 bool odd;
3455 XRectangle wave_clip, string_clip, final_clip;
3457 dx = wave_length;
3458 dy = wave_height - 1;
3459 x0 = s->x;
3460 y0 = s->ybase - wave_height + 3;
3461 width = s->width;
3462 xmax = x0 + width;
3464 /* Find and set clipping rectangle */
3466 wave_clip.x = x0;
3467 wave_clip.y = y0;
3468 wave_clip.width = width;
3469 wave_clip.height = wave_height;
3470 get_glyph_string_clip_rect (s, &string_clip);
3472 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
3473 return;
3475 XSetClipRectangles (s->display, s->gc, 0, 0, &final_clip, 1, Unsorted);
3477 /* Draw the waves */
3479 x1 = x0 - (x0 % dx);
3480 x2 = x1 + dx;
3481 odd = (x1 / dx) & 1;
3482 y1 = y2 = y0;
3484 if (odd)
3485 y1 += dy;
3486 else
3487 y2 += dy;
3489 if (INT_MAX - dx < xmax)
3490 emacs_abort ();
3492 while (x1 <= xmax)
3494 XDrawLine (s->display, FRAME_X_DRAWABLE (s->f), s->gc, x1, y1, x2, y2);
3495 x1 = x2, y1 = y2;
3496 x2 += dx, y2 = y0 + odd*dy;
3497 odd = !odd;
3500 /* Restore previous clipping rectangle(s) */
3501 XSetClipRectangles (s->display, s->gc, 0, 0, s->clip, s->num_clips, Unsorted);
3502 #endif /* not USE_CAIRO */
3506 /* Draw glyph string S. */
3508 static void
3509 x_draw_glyph_string (struct glyph_string *s)
3511 bool relief_drawn_p = false;
3513 /* If S draws into the background of its successors, draw the
3514 background of the successors first so that S can draw into it.
3515 This makes S->next use XDrawString instead of XDrawImageString. */
3516 if (s->next && s->right_overhang && !s->for_overlaps)
3518 int width;
3519 struct glyph_string *next;
3521 for (width = 0, next = s->next;
3522 next && width < s->right_overhang;
3523 width += next->width, next = next->next)
3524 if (next->first_glyph->type != IMAGE_GLYPH)
3526 x_set_glyph_string_gc (next);
3527 x_set_glyph_string_clipping (next);
3528 if (next->first_glyph->type == STRETCH_GLYPH)
3529 x_draw_stretch_glyph_string (next);
3530 else
3531 x_draw_glyph_string_background (next, true);
3532 next->num_clips = 0;
3536 /* Set up S->gc, set clipping and draw S. */
3537 x_set_glyph_string_gc (s);
3539 /* Draw relief (if any) in advance for char/composition so that the
3540 glyph string can be drawn over it. */
3541 if (!s->for_overlaps
3542 && s->face->box != FACE_NO_BOX
3543 && (s->first_glyph->type == CHAR_GLYPH
3544 || s->first_glyph->type == COMPOSITE_GLYPH))
3547 x_set_glyph_string_clipping (s);
3548 x_draw_glyph_string_background (s, true);
3549 x_draw_glyph_string_box (s);
3550 x_set_glyph_string_clipping (s);
3551 relief_drawn_p = true;
3553 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
3554 && !s->clip_tail
3555 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
3556 || (s->next && s->next->hl != s->hl && s->right_overhang)))
3557 /* We must clip just this glyph. left_overhang part has already
3558 drawn when s->prev was drawn, and right_overhang part will be
3559 drawn later when s->next is drawn. */
3560 x_set_glyph_string_clipping_exactly (s, s);
3561 else
3562 x_set_glyph_string_clipping (s);
3564 switch (s->first_glyph->type)
3566 case IMAGE_GLYPH:
3567 x_draw_image_glyph_string (s);
3568 break;
3570 case XWIDGET_GLYPH:
3571 x_draw_xwidget_glyph_string (s);
3572 break;
3574 case STRETCH_GLYPH:
3575 x_draw_stretch_glyph_string (s);
3576 break;
3578 case CHAR_GLYPH:
3579 if (s->for_overlaps)
3580 s->background_filled_p = true;
3581 else
3582 x_draw_glyph_string_background (s, false);
3583 x_draw_glyph_string_foreground (s);
3584 break;
3586 case COMPOSITE_GLYPH:
3587 if (s->for_overlaps || (s->cmp_from > 0
3588 && ! s->first_glyph->u.cmp.automatic))
3589 s->background_filled_p = true;
3590 else
3591 x_draw_glyph_string_background (s, true);
3592 x_draw_composite_glyph_string_foreground (s);
3593 break;
3595 case GLYPHLESS_GLYPH:
3596 if (s->for_overlaps)
3597 s->background_filled_p = true;
3598 else
3599 x_draw_glyph_string_background (s, true);
3600 x_draw_glyphless_glyph_string_foreground (s);
3601 break;
3603 default:
3604 emacs_abort ();
3607 if (!s->for_overlaps)
3609 /* Draw underline. */
3610 if (s->face->underline_p)
3612 if (s->face->underline_type == FACE_UNDER_WAVE)
3614 if (s->face->underline_defaulted_p)
3615 x_draw_underwave (s);
3616 else
3618 XGCValues xgcv;
3619 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3620 XSetForeground (s->display, s->gc, s->face->underline_color);
3621 x_draw_underwave (s);
3622 XSetForeground (s->display, s->gc, xgcv.foreground);
3625 else if (s->face->underline_type == FACE_UNDER_LINE)
3627 unsigned long thickness, position;
3628 int y;
3630 if (s->prev && s->prev->face->underline_p
3631 && s->prev->face->underline_type == FACE_UNDER_LINE)
3633 /* We use the same underline style as the previous one. */
3634 thickness = s->prev->underline_thickness;
3635 position = s->prev->underline_position;
3637 else
3639 struct font *font = font_for_underline_metrics (s);
3641 /* Get the underline thickness. Default is 1 pixel. */
3642 if (font && font->underline_thickness > 0)
3643 thickness = font->underline_thickness;
3644 else
3645 thickness = 1;
3646 if (x_underline_at_descent_line)
3647 position = (s->height - thickness) - (s->ybase - s->y);
3648 else
3650 /* Get the underline position. This is the recommended
3651 vertical offset in pixels from the baseline to the top of
3652 the underline. This is a signed value according to the
3653 specs, and its default is
3655 ROUND ((maximum descent) / 2), with
3656 ROUND(x) = floor (x + 0.5) */
3658 if (x_use_underline_position_properties
3659 && font && font->underline_position >= 0)
3660 position = font->underline_position;
3661 else if (font)
3662 position = (font->descent + 1) / 2;
3663 else
3664 position = underline_minimum_offset;
3666 position = max (position, underline_minimum_offset);
3668 /* Check the sanity of thickness and position. We should
3669 avoid drawing underline out of the current line area. */
3670 if (s->y + s->height <= s->ybase + position)
3671 position = (s->height - 1) - (s->ybase - s->y);
3672 if (s->y + s->height < s->ybase + position + thickness)
3673 thickness = (s->y + s->height) - (s->ybase + position);
3674 s->underline_thickness = thickness;
3675 s->underline_position = position;
3676 y = s->ybase + position;
3677 if (s->face->underline_defaulted_p)
3678 x_fill_rectangle (s->f, s->gc,
3679 s->x, y, s->width, thickness);
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_fill_rectangle (s->f, s->gc,
3686 s->x, y, s->width, thickness);
3687 XSetForeground (s->display, s->gc, xgcv.foreground);
3691 /* Draw overline. */
3692 if (s->face->overline_p)
3694 unsigned long dy = 0, h = 1;
3696 if (s->face->overline_color_defaulted_p)
3697 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3698 s->width, h);
3699 else
3701 XGCValues xgcv;
3702 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3703 XSetForeground (s->display, s->gc, s->face->overline_color);
3704 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3705 s->width, h);
3706 XSetForeground (s->display, s->gc, xgcv.foreground);
3710 /* Draw strike-through. */
3711 if (s->face->strike_through_p)
3713 /* Y-coordinate and height of the glyph string's first
3714 glyph. We cannot use s->y and s->height because those
3715 could be larger if there are taller display elements
3716 (e.g., characters displayed with a larger font) in the
3717 same glyph row. */
3718 int glyph_y = s->ybase - s->first_glyph->ascent;
3719 int glyph_height = s->first_glyph->ascent + s->first_glyph->descent;
3720 /* Strike-through width and offset from the glyph string's
3721 top edge. */
3722 unsigned long h = 1;
3723 unsigned long dy = (glyph_height - h) / 2;
3725 if (s->face->strike_through_color_defaulted_p)
3726 x_fill_rectangle (s->f, s->gc, s->x, glyph_y + dy,
3727 s->width, h);
3728 else
3730 XGCValues xgcv;
3731 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3732 XSetForeground (s->display, s->gc, s->face->strike_through_color);
3733 x_fill_rectangle (s->f, s->gc, s->x, glyph_y + dy,
3734 s->width, h);
3735 XSetForeground (s->display, s->gc, xgcv.foreground);
3739 /* Draw relief if not yet drawn. */
3740 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
3741 x_draw_glyph_string_box (s);
3743 if (s->prev)
3745 struct glyph_string *prev;
3747 for (prev = s->prev; prev; prev = prev->prev)
3748 if (prev->hl != s->hl
3749 && prev->x + prev->width + prev->right_overhang > s->x)
3751 /* As prev was drawn while clipped to its own area, we
3752 must draw the right_overhang part using s->hl now. */
3753 enum draw_glyphs_face save = prev->hl;
3755 prev->hl = s->hl;
3756 x_set_glyph_string_gc (prev);
3757 x_set_glyph_string_clipping_exactly (s, prev);
3758 if (prev->first_glyph->type == CHAR_GLYPH)
3759 x_draw_glyph_string_foreground (prev);
3760 else
3761 x_draw_composite_glyph_string_foreground (prev);
3762 x_reset_clip_rectangles (prev->f, prev->gc);
3763 prev->hl = save;
3764 prev->num_clips = 0;
3768 if (s->next)
3770 struct glyph_string *next;
3772 for (next = s->next; next; next = next->next)
3773 if (next->hl != s->hl
3774 && next->x - next->left_overhang < s->x + s->width)
3776 /* As next will be drawn while clipped to its own area,
3777 we must draw the left_overhang part using s->hl now. */
3778 enum draw_glyphs_face save = next->hl;
3780 next->hl = s->hl;
3781 x_set_glyph_string_gc (next);
3782 x_set_glyph_string_clipping_exactly (s, next);
3783 if (next->first_glyph->type == CHAR_GLYPH)
3784 x_draw_glyph_string_foreground (next);
3785 else
3786 x_draw_composite_glyph_string_foreground (next);
3787 x_reset_clip_rectangles (next->f, next->gc);
3788 next->hl = save;
3789 next->num_clips = 0;
3790 next->clip_head = s->next;
3795 /* Reset clipping. */
3796 x_reset_clip_rectangles (s->f, s->gc);
3797 s->num_clips = 0;
3800 /* Shift display to make room for inserted glyphs. */
3802 static void
3803 x_shift_glyphs_for_insert (struct frame *f, int x, int y, int width, int height, int shift_by)
3805 /* Never called on a GUI frame, see
3806 http://lists.gnu.org/archive/html/emacs-devel/2015-05/msg00456.html
3808 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f), FRAME_X_DRAWABLE (f),
3809 f->output_data.x->normal_gc,
3810 x, y, width, height,
3811 x + shift_by, y);
3814 /* Delete N glyphs at the nominal cursor position. Not implemented
3815 for X frames. */
3817 static void
3818 x_delete_glyphs (struct frame *f, register int n)
3820 emacs_abort ();
3824 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
3825 If they are <= 0, this is probably an error. */
3827 static ATTRIBUTE_UNUSED void
3828 x_clear_area1 (Display *dpy, Window window,
3829 int x, int y, int width, int height, int exposures)
3831 eassert (width > 0 && height > 0);
3832 XClearArea (dpy, window, x, y, width, height, exposures);
3835 void
3836 x_clear_area (struct frame *f, int x, int y, int width, int height)
3838 #ifdef USE_CAIRO
3839 cairo_t *cr;
3841 eassert (width > 0 && height > 0);
3843 cr = x_begin_cr_clip (f, NULL);
3844 x_set_cr_source_with_gc_background (f, f->output_data.x->normal_gc);
3845 cairo_rectangle (cr, x, y, width, height);
3846 cairo_fill (cr);
3847 x_end_cr_clip (f);
3848 #else
3849 if (FRAME_X_DOUBLE_BUFFERED_P (f))
3850 XFillRectangle (FRAME_X_DISPLAY (f),
3851 FRAME_X_DRAWABLE (f),
3852 f->output_data.x->reverse_gc,
3853 x, y, width, height);
3854 else
3855 x_clear_area1 (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3856 x, y, width, height, False);
3857 #endif
3861 /* Clear an entire frame. */
3863 static void
3864 x_clear_frame (struct frame *f)
3866 /* Clearing the frame will erase any cursor, so mark them all as no
3867 longer visible. */
3868 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
3870 block_input ();
3872 font_drop_xrender_surfaces (f);
3873 x_clear_window (f);
3875 /* We have to clear the scroll bars. If we have changed colors or
3876 something like that, then they should be notified. */
3877 x_scroll_bar_clear (f);
3879 XFlush (FRAME_X_DISPLAY (f));
3881 unblock_input ();
3884 /* RIF: Show hourglass cursor on frame F. */
3886 static void
3887 x_show_hourglass (struct frame *f)
3889 Display *dpy = FRAME_X_DISPLAY (f);
3891 if (dpy)
3893 struct x_output *x = FRAME_X_OUTPUT (f);
3894 #ifdef USE_X_TOOLKIT
3895 if (x->widget)
3896 #else
3897 if (FRAME_OUTER_WINDOW (f))
3898 #endif
3900 x->hourglass_p = true;
3902 if (!x->hourglass_window)
3904 unsigned long mask = CWCursor;
3905 XSetWindowAttributes attrs;
3906 #ifdef USE_GTK
3907 Window parent = FRAME_X_WINDOW (f);
3908 #else
3909 Window parent = FRAME_OUTER_WINDOW (f);
3910 #endif
3911 attrs.cursor = x->hourglass_cursor;
3913 x->hourglass_window = XCreateWindow
3914 (dpy, parent, 0, 0, 32000, 32000, 0, 0,
3915 InputOnly, CopyFromParent, mask, &attrs);
3918 XMapRaised (dpy, x->hourglass_window);
3919 XFlush (dpy);
3924 /* RIF: Cancel hourglass cursor on frame F. */
3926 static void
3927 x_hide_hourglass (struct frame *f)
3929 struct x_output *x = FRAME_X_OUTPUT (f);
3931 /* Watch out for newly created frames. */
3932 if (x->hourglass_window)
3934 XUnmapWindow (FRAME_X_DISPLAY (f), x->hourglass_window);
3935 /* Sync here because XTread_socket looks at the
3936 hourglass_p flag that is reset to zero below. */
3937 XSync (FRAME_X_DISPLAY (f), False);
3938 x->hourglass_p = false;
3942 /* Invert the middle quarter of the frame for .15 sec. */
3944 static void
3945 XTflash (struct frame *f)
3947 block_input ();
3950 #ifdef USE_GTK
3951 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
3952 when the scroll bars and the edit widget share the same X window. */
3953 GdkWindow *window = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
3954 #ifdef HAVE_GTK3
3955 cairo_t *cr = gdk_cairo_create (window);
3956 cairo_set_source_rgb (cr, 1, 1, 1);
3957 cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
3958 #define XFillRectangle(d, win, gc, x, y, w, h) \
3959 do { \
3960 cairo_rectangle (cr, x, y, w, h); \
3961 cairo_fill (cr); \
3963 while (false)
3964 #else /* ! HAVE_GTK3 */
3965 GdkGCValues vals;
3966 GdkGC *gc;
3967 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
3968 ^ FRAME_BACKGROUND_PIXEL (f));
3969 vals.function = GDK_XOR;
3970 gc = gdk_gc_new_with_values (window,
3971 &vals, GDK_GC_FUNCTION | GDK_GC_FOREGROUND);
3972 #define XFillRectangle(d, win, gc, x, y, w, h) \
3973 gdk_draw_rectangle (window, gc, true, x, y, w, h)
3974 #endif /* ! HAVE_GTK3 */
3975 #else /* ! USE_GTK */
3976 GC gc;
3978 /* Create a GC that will use the GXxor function to flip foreground
3979 pixels into background pixels. */
3981 XGCValues values;
3983 values.function = GXxor;
3984 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3985 ^ FRAME_BACKGROUND_PIXEL (f));
3987 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3988 GCFunction | GCForeground, &values);
3990 #endif
3992 /* Get the height not including a menu bar widget. */
3993 int height = FRAME_PIXEL_HEIGHT (f);
3994 /* Height of each line to flash. */
3995 int flash_height = FRAME_LINE_HEIGHT (f);
3996 /* These will be the left and right margins of the rectangles. */
3997 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3998 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3999 int width = flash_right - flash_left;
4001 /* If window is tall, flash top and bottom line. */
4002 if (height > 3 * FRAME_LINE_HEIGHT (f))
4004 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4005 flash_left,
4006 (FRAME_INTERNAL_BORDER_WIDTH (f)
4007 + FRAME_TOP_MARGIN_HEIGHT (f)),
4008 width, flash_height);
4009 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4010 flash_left,
4011 (height - flash_height
4012 - FRAME_INTERNAL_BORDER_WIDTH (f)),
4013 width, flash_height);
4016 else
4017 /* If it is short, flash it all. */
4018 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4019 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
4020 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
4022 x_flush (f);
4025 struct timespec delay = make_timespec (0, 150 * 1000 * 1000);
4026 struct timespec wakeup = timespec_add (current_timespec (), delay);
4028 /* Keep waiting until past the time wakeup or any input gets
4029 available. */
4030 while (! detect_input_pending ())
4032 struct timespec current = current_timespec ();
4033 struct timespec timeout;
4035 /* Break if result would not be positive. */
4036 if (timespec_cmp (wakeup, current) <= 0)
4037 break;
4039 /* How long `select' should wait. */
4040 timeout = make_timespec (0, 10 * 1000 * 1000);
4042 /* Try to wait that long--but we might wake up sooner. */
4043 pselect (0, NULL, NULL, NULL, &timeout, NULL);
4047 /* If window is tall, flash top and bottom line. */
4048 if (height > 3 * FRAME_LINE_HEIGHT (f))
4050 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4051 flash_left,
4052 (FRAME_INTERNAL_BORDER_WIDTH (f)
4053 + FRAME_TOP_MARGIN_HEIGHT (f)),
4054 width, flash_height);
4055 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4056 flash_left,
4057 (height - flash_height
4058 - FRAME_INTERNAL_BORDER_WIDTH (f)),
4059 width, flash_height);
4061 else
4062 /* If it is short, flash it all. */
4063 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4064 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
4065 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
4067 #ifdef USE_GTK
4068 #ifdef HAVE_GTK3
4069 cairo_destroy (cr);
4070 #else
4071 g_object_unref (G_OBJECT (gc));
4072 #endif
4073 #undef XFillRectangle
4074 #else
4075 XFreeGC (FRAME_X_DISPLAY (f), gc);
4076 #endif
4077 x_flush (f);
4081 unblock_input ();
4085 static void
4086 XTtoggle_invisible_pointer (struct frame *f, bool invisible)
4088 block_input ();
4089 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, invisible);
4090 unblock_input ();
4094 /* Make audible bell. */
4096 static void
4097 XTring_bell (struct frame *f)
4099 if (FRAME_X_DISPLAY (f))
4101 if (visible_bell)
4102 XTflash (f);
4103 else
4105 block_input ();
4106 #ifdef HAVE_XKB
4107 XkbBell (FRAME_X_DISPLAY (f), None, 0, None);
4108 #else
4109 XBell (FRAME_X_DISPLAY (f), 0);
4110 #endif
4111 XFlush (FRAME_X_DISPLAY (f));
4112 unblock_input ();
4117 /***********************************************************************
4118 Line Dance
4119 ***********************************************************************/
4121 /* Perform an insert-lines or delete-lines operation, inserting N
4122 lines or deleting -N lines at vertical position VPOS. */
4124 static void
4125 x_ins_del_lines (struct frame *f, int vpos, int n)
4127 emacs_abort ();
4131 /* Scroll part of the display as described by RUN. */
4133 static void
4134 x_scroll_run (struct window *w, struct run *run)
4136 struct frame *f = XFRAME (w->frame);
4137 int x, y, width, height, from_y, to_y, bottom_y;
4139 /* Get frame-relative bounding box of the text display area of W,
4140 without mode lines. Include in this box the left and right
4141 fringe of W. */
4142 window_box (w, ANY_AREA, &x, &y, &width, &height);
4144 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
4145 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
4146 bottom_y = y + height;
4148 if (to_y < from_y)
4150 /* Scrolling up. Make sure we don't copy part of the mode
4151 line at the bottom. */
4152 if (from_y + run->height > bottom_y)
4153 height = bottom_y - from_y;
4154 else
4155 height = run->height;
4157 else
4159 /* Scrolling down. Make sure we don't copy over the mode line.
4160 at the bottom. */
4161 if (to_y + run->height > bottom_y)
4162 height = bottom_y - to_y;
4163 else
4164 height = run->height;
4167 block_input ();
4169 /* Cursor off. Will be switched on again in x_update_window_end. */
4170 x_clear_cursor (w);
4172 #ifdef USE_CAIRO
4173 SET_FRAME_GARBAGED (f);
4174 #else
4175 XCopyArea (FRAME_X_DISPLAY (f),
4176 FRAME_X_DRAWABLE (f), FRAME_X_DRAWABLE (f),
4177 f->output_data.x->normal_gc,
4178 x, from_y,
4179 width, height,
4180 x, to_y);
4181 #endif
4183 unblock_input ();
4188 /***********************************************************************
4189 Exposure Events
4190 ***********************************************************************/
4193 static void
4194 frame_highlight (struct frame *f)
4196 /* We used to only do this if Vx_no_window_manager was non-nil, but
4197 the ICCCM (section 4.1.6) says that the window's border pixmap
4198 and border pixel are window attributes which are "private to the
4199 client", so we can always change it to whatever we want. */
4200 block_input ();
4201 /* I recently started to get errors in this XSetWindowBorder, depending on
4202 the window-manager in use, tho something more is at play since I've been
4203 using that same window-manager binary for ever. Let's not crash just
4204 because of this (bug#9310). */
4205 x_catch_errors (FRAME_X_DISPLAY (f));
4206 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4207 f->output_data.x->border_pixel);
4208 x_uncatch_errors ();
4209 unblock_input ();
4210 x_update_cursor (f, true);
4211 x_set_frame_alpha (f);
4214 static void
4215 frame_unhighlight (struct frame *f)
4217 /* We used to only do this if Vx_no_window_manager was non-nil, but
4218 the ICCCM (section 4.1.6) says that the window's border pixmap
4219 and border pixel are window attributes which are "private to the
4220 client", so we can always change it to whatever we want. */
4221 block_input ();
4222 /* Same as above for XSetWindowBorder (bug#9310). */
4223 x_catch_errors (FRAME_X_DISPLAY (f));
4224 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4225 f->output_data.x->border_tile);
4226 x_uncatch_errors ();
4227 unblock_input ();
4228 x_update_cursor (f, true);
4229 x_set_frame_alpha (f);
4232 /* The focus has changed. Update the frames as necessary to reflect
4233 the new situation. Note that we can't change the selected frame
4234 here, because the Lisp code we are interrupting might become confused.
4235 Each event gets marked with the frame in which it occurred, so the
4236 Lisp code can tell when the switch took place by examining the events. */
4238 static void
4239 x_new_focus_frame (struct x_display_info *dpyinfo, struct frame *frame)
4241 struct frame *old_focus = dpyinfo->x_focus_frame;
4243 if (frame != dpyinfo->x_focus_frame)
4245 /* Set this before calling other routines, so that they see
4246 the correct value of x_focus_frame. */
4247 dpyinfo->x_focus_frame = frame;
4249 if (old_focus && old_focus->auto_lower)
4250 x_lower_frame (old_focus);
4252 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
4253 dpyinfo->x_pending_autoraise_frame = dpyinfo->x_focus_frame;
4254 else
4255 dpyinfo->x_pending_autoraise_frame = NULL;
4258 x_frame_rehighlight (dpyinfo);
4261 /* Handle FocusIn and FocusOut state changes for FRAME.
4262 If FRAME has focus and there exists more than one frame, puts
4263 a FOCUS_IN_EVENT into *BUFP. */
4265 static void
4266 x_focus_changed (int type, int state, struct x_display_info *dpyinfo, struct frame *frame, struct input_event *bufp)
4268 if (type == FocusIn)
4270 if (dpyinfo->x_focus_event_frame != frame)
4272 x_new_focus_frame (dpyinfo, frame);
4273 dpyinfo->x_focus_event_frame = frame;
4275 /* Don't stop displaying the initial startup message
4276 for a switch-frame event we don't need. */
4277 /* When run as a daemon, Vterminal_frame is always NIL. */
4278 bufp->arg = (((NILP (Vterminal_frame)
4279 || ! FRAME_X_P (XFRAME (Vterminal_frame))
4280 || EQ (Fdaemonp (), Qt))
4281 && CONSP (Vframe_list)
4282 && !NILP (XCDR (Vframe_list)))
4283 ? Qt : Qnil);
4284 bufp->kind = FOCUS_IN_EVENT;
4285 XSETFRAME (bufp->frame_or_window, frame);
4288 frame->output_data.x->focus_state |= state;
4290 #ifdef HAVE_X_I18N
4291 if (FRAME_XIC (frame))
4292 XSetICFocus (FRAME_XIC (frame));
4293 #endif
4295 else if (type == FocusOut)
4297 frame->output_data.x->focus_state &= ~state;
4299 if (dpyinfo->x_focus_event_frame == frame)
4301 dpyinfo->x_focus_event_frame = 0;
4302 x_new_focus_frame (dpyinfo, 0);
4304 bufp->kind = FOCUS_OUT_EVENT;
4305 XSETFRAME (bufp->frame_or_window, frame);
4308 #ifdef HAVE_X_I18N
4309 if (FRAME_XIC (frame))
4310 XUnsetICFocus (FRAME_XIC (frame));
4311 #endif
4312 if (frame->pointer_invisible)
4313 XTtoggle_invisible_pointer (frame, false);
4317 /* Return the Emacs frame-object corresponding to an X window.
4318 It could be the frame's main window or an icon window. */
4320 static struct frame *
4321 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4323 Lisp_Object tail, frame;
4324 struct frame *f;
4326 if (wdesc == None)
4327 return NULL;
4329 FOR_EACH_FRAME (tail, frame)
4331 f = XFRAME (frame);
4332 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4333 continue;
4334 if (f->output_data.x->hourglass_window == wdesc)
4335 return f;
4336 #ifdef USE_X_TOOLKIT
4337 if ((f->output_data.x->edit_widget
4338 && XtWindow (f->output_data.x->edit_widget) == wdesc)
4339 /* A tooltip frame? */
4340 || (!f->output_data.x->edit_widget
4341 && FRAME_X_WINDOW (f) == wdesc)
4342 || f->output_data.x->icon_desc == wdesc)
4343 return f;
4344 #else /* not USE_X_TOOLKIT */
4345 #ifdef USE_GTK
4346 if (f->output_data.x->edit_widget)
4348 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4349 struct x_output *x = f->output_data.x;
4350 if (gwdesc != 0 && gwdesc == x->edit_widget)
4351 return f;
4353 #endif /* USE_GTK */
4354 if (FRAME_X_WINDOW (f) == wdesc
4355 || f->output_data.x->icon_desc == wdesc)
4356 return f;
4357 #endif /* not USE_X_TOOLKIT */
4359 return 0;
4362 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
4364 /* Like x_window_to_frame but also compares the window with the widget's
4365 windows. */
4367 static struct frame *
4368 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4370 Lisp_Object tail, frame;
4371 struct frame *f, *found = NULL;
4372 struct x_output *x;
4374 if (wdesc == None)
4375 return NULL;
4377 FOR_EACH_FRAME (tail, frame)
4379 if (found)
4380 break;
4381 f = XFRAME (frame);
4382 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
4384 /* This frame matches if the window is any of its widgets. */
4385 x = f->output_data.x;
4386 if (x->hourglass_window == wdesc)
4387 found = f;
4388 else if (x->widget)
4390 #ifdef USE_GTK
4391 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4392 if (gwdesc != 0
4393 && gtk_widget_get_toplevel (gwdesc) == x->widget)
4394 found = f;
4395 #else
4396 if (wdesc == XtWindow (x->widget)
4397 || wdesc == XtWindow (x->column_widget)
4398 || wdesc == XtWindow (x->edit_widget))
4399 found = f;
4400 /* Match if the window is this frame's menubar. */
4401 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
4402 found = f;
4403 #endif
4405 else if (FRAME_X_WINDOW (f) == wdesc)
4406 /* A tooltip frame. */
4407 found = f;
4411 return found;
4414 /* Likewise, but consider only the menu bar widget. */
4416 static struct frame *
4417 x_menubar_window_to_frame (struct x_display_info *dpyinfo,
4418 const XEvent *event)
4420 Window wdesc = event->xany.window;
4421 Lisp_Object tail, frame;
4422 struct frame *f;
4423 struct x_output *x;
4425 if (wdesc == None)
4426 return NULL;
4428 FOR_EACH_FRAME (tail, frame)
4430 f = XFRAME (frame);
4431 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4432 continue;
4433 x = f->output_data.x;
4434 #ifdef USE_GTK
4435 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
4436 return f;
4437 #else
4438 /* Match if the window is this frame's menubar. */
4439 if (x->menubar_widget
4440 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
4441 return f;
4442 #endif
4444 return 0;
4447 /* Return the frame whose principal (outermost) window is WDESC.
4448 If WDESC is some other (smaller) window, we return 0. */
4450 struct frame *
4451 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4453 Lisp_Object tail, frame;
4454 struct frame *f;
4455 struct x_output *x;
4457 if (wdesc == None)
4458 return NULL;
4460 FOR_EACH_FRAME (tail, frame)
4462 f = XFRAME (frame);
4463 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4464 continue;
4465 x = f->output_data.x;
4467 if (x->widget)
4469 /* This frame matches if the window is its topmost widget. */
4470 #ifdef USE_GTK
4471 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4472 if (gwdesc == x->widget)
4473 return f;
4474 #else
4475 if (wdesc == XtWindow (x->widget))
4476 return f;
4477 #endif
4479 else if (FRAME_X_WINDOW (f) == wdesc)
4480 /* Tooltip frame. */
4481 return f;
4483 return 0;
4486 #else /* !USE_X_TOOLKIT && !USE_GTK */
4488 #define x_any_window_to_frame(d, i) x_window_to_frame (d, i)
4489 #define x_top_window_to_frame(d, i) x_window_to_frame (d, i)
4491 #endif /* USE_X_TOOLKIT || USE_GTK */
4493 /* The focus may have changed. Figure out if it is a real focus change,
4494 by checking both FocusIn/Out and Enter/LeaveNotify events.
4496 Returns FOCUS_IN_EVENT event in *BUFP. */
4498 static void
4499 x_detect_focus_change (struct x_display_info *dpyinfo, struct frame *frame,
4500 const XEvent *event, struct input_event *bufp)
4502 if (!frame)
4503 return;
4505 switch (event->type)
4507 case EnterNotify:
4508 case LeaveNotify:
4510 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
4511 int focus_state
4512 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
4514 if (event->xcrossing.detail != NotifyInferior
4515 && event->xcrossing.focus
4516 && ! (focus_state & FOCUS_EXPLICIT))
4517 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
4518 FOCUS_IMPLICIT,
4519 dpyinfo, frame, bufp);
4521 break;
4523 case FocusIn:
4524 case FocusOut:
4525 x_focus_changed (event->type,
4526 (event->xfocus.detail == NotifyPointer ?
4527 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
4528 dpyinfo, frame, bufp);
4529 break;
4531 case ClientMessage:
4532 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
4534 enum xembed_message msg = event->xclient.data.l[1];
4535 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
4536 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
4538 break;
4543 #if !defined USE_X_TOOLKIT && !defined USE_GTK
4544 /* Handle an event saying the mouse has moved out of an Emacs frame. */
4546 void
4547 x_mouse_leave (struct x_display_info *dpyinfo)
4549 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
4551 #endif
4553 /* The focus has changed, or we have redirected a frame's focus to
4554 another frame (this happens when a frame uses a surrogate
4555 mini-buffer frame). Shift the highlight as appropriate.
4557 The FRAME argument doesn't necessarily have anything to do with which
4558 frame is being highlighted or un-highlighted; we only use it to find
4559 the appropriate X display info. */
4561 static void
4562 XTframe_rehighlight (struct frame *frame)
4564 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
4567 static void
4568 x_frame_rehighlight (struct x_display_info *dpyinfo)
4570 struct frame *old_highlight = dpyinfo->x_highlight_frame;
4572 if (dpyinfo->x_focus_frame)
4574 dpyinfo->x_highlight_frame
4575 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
4576 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
4577 : dpyinfo->x_focus_frame);
4578 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
4580 fset_focus_frame (dpyinfo->x_focus_frame, Qnil);
4581 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
4584 else
4585 dpyinfo->x_highlight_frame = 0;
4587 if (dpyinfo->x_highlight_frame != old_highlight)
4589 if (old_highlight)
4590 frame_unhighlight (old_highlight);
4591 if (dpyinfo->x_highlight_frame)
4592 frame_highlight (dpyinfo->x_highlight_frame);
4598 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
4600 /* Initialize mode_switch_bit and modifier_meaning. */
4601 static void
4602 x_find_modifier_meanings (struct x_display_info *dpyinfo)
4604 int min_code, max_code;
4605 KeySym *syms;
4606 int syms_per_code;
4607 XModifierKeymap *mods;
4609 dpyinfo->meta_mod_mask = 0;
4610 dpyinfo->shift_lock_mask = 0;
4611 dpyinfo->alt_mod_mask = 0;
4612 dpyinfo->super_mod_mask = 0;
4613 dpyinfo->hyper_mod_mask = 0;
4615 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
4617 syms = XGetKeyboardMapping (dpyinfo->display,
4618 min_code, max_code - min_code + 1,
4619 &syms_per_code);
4620 mods = XGetModifierMapping (dpyinfo->display);
4622 /* Scan the modifier table to see which modifier bits the Meta and
4623 Alt keysyms are on. */
4625 int row, col; /* The row and column in the modifier table. */
4626 bool found_alt_or_meta;
4628 for (row = 3; row < 8; row++)
4630 found_alt_or_meta = false;
4631 for (col = 0; col < mods->max_keypermod; col++)
4633 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
4635 /* Zeroes are used for filler. Skip them. */
4636 if (code == 0)
4637 continue;
4639 /* Are any of this keycode's keysyms a meta key? */
4641 int code_col;
4643 for (code_col = 0; code_col < syms_per_code; code_col++)
4645 int sym = syms[((code - min_code) * syms_per_code) + code_col];
4647 switch (sym)
4649 case XK_Meta_L:
4650 case XK_Meta_R:
4651 found_alt_or_meta = true;
4652 dpyinfo->meta_mod_mask |= (1 << row);
4653 break;
4655 case XK_Alt_L:
4656 case XK_Alt_R:
4657 found_alt_or_meta = true;
4658 dpyinfo->alt_mod_mask |= (1 << row);
4659 break;
4661 case XK_Hyper_L:
4662 case XK_Hyper_R:
4663 if (!found_alt_or_meta)
4664 dpyinfo->hyper_mod_mask |= (1 << row);
4665 code_col = syms_per_code;
4666 col = mods->max_keypermod;
4667 break;
4669 case XK_Super_L:
4670 case XK_Super_R:
4671 if (!found_alt_or_meta)
4672 dpyinfo->super_mod_mask |= (1 << row);
4673 code_col = syms_per_code;
4674 col = mods->max_keypermod;
4675 break;
4677 case XK_Shift_Lock:
4678 /* Ignore this if it's not on the lock modifier. */
4679 if (!found_alt_or_meta && ((1 << row) == LockMask))
4680 dpyinfo->shift_lock_mask = LockMask;
4681 code_col = syms_per_code;
4682 col = mods->max_keypermod;
4683 break;
4691 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
4692 if (! dpyinfo->meta_mod_mask)
4694 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
4695 dpyinfo->alt_mod_mask = 0;
4698 /* If some keys are both alt and meta,
4699 make them just meta, not alt. */
4700 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
4702 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
4705 XFree (syms);
4706 XFreeModifiermap (mods);
4709 /* Convert between the modifier bits X uses and the modifier bits
4710 Emacs uses. */
4713 x_x_to_emacs_modifiers (struct x_display_info *dpyinfo, int state)
4715 int mod_ctrl = ctrl_modifier;
4716 int mod_meta = meta_modifier;
4717 int mod_alt = alt_modifier;
4718 int mod_hyper = hyper_modifier;
4719 int mod_super = super_modifier;
4720 Lisp_Object tem;
4722 tem = Fget (Vx_ctrl_keysym, Qmodifier_value);
4723 if (INTEGERP (tem)) mod_ctrl = XINT (tem) & INT_MAX;
4724 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4725 if (INTEGERP (tem)) mod_alt = XINT (tem) & INT_MAX;
4726 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4727 if (INTEGERP (tem)) mod_meta = XINT (tem) & INT_MAX;
4728 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4729 if (INTEGERP (tem)) mod_hyper = XINT (tem) & INT_MAX;
4730 tem = Fget (Vx_super_keysym, Qmodifier_value);
4731 if (INTEGERP (tem)) mod_super = XINT (tem) & INT_MAX;
4733 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
4734 | ((state & ControlMask) ? mod_ctrl : 0)
4735 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
4736 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
4737 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
4738 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
4741 static int
4742 x_emacs_to_x_modifiers (struct x_display_info *dpyinfo, EMACS_INT state)
4744 EMACS_INT mod_ctrl = ctrl_modifier;
4745 EMACS_INT mod_meta = meta_modifier;
4746 EMACS_INT mod_alt = alt_modifier;
4747 EMACS_INT mod_hyper = hyper_modifier;
4748 EMACS_INT mod_super = super_modifier;
4750 Lisp_Object tem;
4752 tem = Fget (Vx_ctrl_keysym, Qmodifier_value);
4753 if (INTEGERP (tem)) mod_ctrl = XINT (tem);
4754 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4755 if (INTEGERP (tem)) mod_alt = XINT (tem);
4756 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4757 if (INTEGERP (tem)) mod_meta = XINT (tem);
4758 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4759 if (INTEGERP (tem)) mod_hyper = XINT (tem);
4760 tem = Fget (Vx_super_keysym, Qmodifier_value);
4761 if (INTEGERP (tem)) mod_super = XINT (tem);
4764 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
4765 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
4766 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
4767 | ((state & shift_modifier) ? ShiftMask : 0)
4768 | ((state & mod_ctrl) ? ControlMask : 0)
4769 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
4772 /* Convert a keysym to its name. */
4774 char *
4775 x_get_keysym_name (int keysym)
4777 char *value;
4779 block_input ();
4780 value = XKeysymToString (keysym);
4781 unblock_input ();
4783 return value;
4786 /* Mouse clicks and mouse movement. Rah.
4788 Formerly, we used PointerMotionHintMask (in standard_event_mask)
4789 so that we would have to call XQueryPointer after each MotionNotify
4790 event to ask for another such event. However, this made mouse tracking
4791 slow, and there was a bug that made it eventually stop.
4793 Simply asking for MotionNotify all the time seems to work better.
4795 In order to avoid asking for motion events and then throwing most
4796 of them away or busy-polling the server for mouse positions, we ask
4797 the server for pointer motion hints. This means that we get only
4798 one event per group of mouse movements. "Groups" are delimited by
4799 other kinds of events (focus changes and button clicks, for
4800 example), or by XQueryPointer calls; when one of these happens, we
4801 get another MotionNotify event the next time the mouse moves. This
4802 is at least as efficient as getting motion events when mouse
4803 tracking is on, and I suspect only negligibly worse when tracking
4804 is off. */
4806 /* Prepare a mouse-event in *RESULT for placement in the input queue.
4808 If the event is a button press, then note that we have grabbed
4809 the mouse. */
4811 static Lisp_Object
4812 construct_mouse_click (struct input_event *result,
4813 const XButtonEvent *event,
4814 struct frame *f)
4816 /* Make the event type NO_EVENT; we'll change that when we decide
4817 otherwise. */
4818 result->kind = MOUSE_CLICK_EVENT;
4819 result->code = event->button - Button1;
4820 result->timestamp = event->time;
4821 result->modifiers = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f),
4822 event->state)
4823 | (event->type == ButtonRelease
4824 ? up_modifier
4825 : down_modifier));
4827 XSETINT (result->x, event->x);
4828 XSETINT (result->y, event->y);
4829 XSETFRAME (result->frame_or_window, f);
4830 result->arg = Qnil;
4831 return Qnil;
4834 /* Function to report a mouse movement to the mainstream Emacs code.
4835 The input handler calls this.
4837 We have received a mouse movement event, which is given in *event.
4838 If the mouse is over a different glyph than it was last time, tell
4839 the mainstream emacs code by setting mouse_moved. If not, ask for
4840 another motion event, so we can check again the next time it moves. */
4842 static bool
4843 note_mouse_movement (struct frame *frame, const XMotionEvent *event)
4845 XRectangle *r;
4846 struct x_display_info *dpyinfo;
4848 if (!FRAME_X_OUTPUT (frame))
4849 return false;
4851 dpyinfo = FRAME_DISPLAY_INFO (frame);
4852 dpyinfo->last_mouse_movement_time = event->time;
4853 dpyinfo->last_mouse_motion_frame = frame;
4854 dpyinfo->last_mouse_motion_x = event->x;
4855 dpyinfo->last_mouse_motion_y = event->y;
4857 if (event->window != FRAME_X_WINDOW (frame))
4859 frame->mouse_moved = true;
4860 dpyinfo->last_mouse_scroll_bar = NULL;
4861 note_mouse_highlight (frame, -1, -1);
4862 dpyinfo->last_mouse_glyph_frame = NULL;
4863 return true;
4867 /* Has the mouse moved off the glyph it was on at the last sighting? */
4868 r = &dpyinfo->last_mouse_glyph;
4869 if (frame != dpyinfo->last_mouse_glyph_frame
4870 || event->x < r->x || event->x >= r->x + r->width
4871 || event->y < r->y || event->y >= r->y + r->height)
4873 frame->mouse_moved = true;
4874 dpyinfo->last_mouse_scroll_bar = NULL;
4875 note_mouse_highlight (frame, event->x, event->y);
4876 /* Remember which glyph we're now on. */
4877 remember_mouse_glyph (frame, event->x, event->y, r);
4878 dpyinfo->last_mouse_glyph_frame = frame;
4879 return true;
4882 return false;
4885 /* Return the current position of the mouse.
4886 *FP should be a frame which indicates which display to ask about.
4888 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
4889 and *PART to the frame, window, and scroll bar part that the mouse
4890 is over. Set *X and *Y to the portion and whole of the mouse's
4891 position on the scroll bar.
4893 If the mouse movement started elsewhere, set *FP to the frame the
4894 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
4895 the mouse is over.
4897 Set *TIMESTAMP to the server time-stamp for the time at which the mouse
4898 was at this position.
4900 Don't store anything if we don't have a valid set of values to report.
4902 This clears the mouse_moved flag, so we can wait for the next mouse
4903 movement. */
4905 static void
4906 XTmouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
4907 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
4908 Time *timestamp)
4910 struct frame *f1;
4911 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
4913 block_input ();
4915 if (dpyinfo->last_mouse_scroll_bar && insist == 0)
4917 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
4919 if (bar->horizontal)
4920 x_horizontal_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
4921 else
4922 x_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
4924 else
4926 Window root;
4927 int root_x, root_y;
4929 Window dummy_window;
4930 int dummy;
4932 Lisp_Object frame, tail;
4934 /* Clear the mouse-moved flag for every frame on this display. */
4935 FOR_EACH_FRAME (tail, frame)
4936 if (FRAME_X_P (XFRAME (frame))
4937 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
4938 XFRAME (frame)->mouse_moved = false;
4940 dpyinfo->last_mouse_scroll_bar = NULL;
4942 /* Figure out which root window we're on. */
4943 XQueryPointer (FRAME_X_DISPLAY (*fp),
4944 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
4946 /* The root window which contains the pointer. */
4947 &root,
4949 /* Trash which we can't trust if the pointer is on
4950 a different screen. */
4951 &dummy_window,
4953 /* The position on that root window. */
4954 &root_x, &root_y,
4956 /* More trash we can't trust. */
4957 &dummy, &dummy,
4959 /* Modifier keys and pointer buttons, about which
4960 we don't care. */
4961 (unsigned int *) &dummy);
4963 /* Now we have a position on the root; find the innermost window
4964 containing the pointer. */
4966 Window win, child;
4967 int win_x, win_y;
4968 int parent_x = 0, parent_y = 0;
4970 win = root;
4972 /* XTranslateCoordinates can get errors if the window
4973 structure is changing at the same time this function
4974 is running. So at least we must not crash from them. */
4976 x_catch_errors (FRAME_X_DISPLAY (*fp));
4978 if (x_mouse_grabbed (dpyinfo))
4980 /* If mouse was grabbed on a frame, give coords for that frame
4981 even if the mouse is now outside it. */
4982 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
4984 /* From-window. */
4985 root,
4987 /* To-window. */
4988 FRAME_X_WINDOW (dpyinfo->last_mouse_frame),
4990 /* From-position, to-position. */
4991 root_x, root_y, &win_x, &win_y,
4993 /* Child of win. */
4994 &child);
4995 f1 = dpyinfo->last_mouse_frame;
4997 else
4999 while (true)
5001 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
5003 /* From-window, to-window. */
5004 root, win,
5006 /* From-position, to-position. */
5007 root_x, root_y, &win_x, &win_y,
5009 /* Child of win. */
5010 &child);
5012 if (child == None || child == win)
5013 break;
5014 #ifdef USE_GTK
5015 /* We don't wan't to know the innermost window. We
5016 want the edit window. For non-Gtk+ the innermost
5017 window is the edit window. For Gtk+ it might not
5018 be. It might be the tool bar for example. */
5019 if (x_window_to_frame (dpyinfo, win))
5020 break;
5021 #endif
5022 win = child;
5023 parent_x = win_x;
5024 parent_y = win_y;
5027 /* Now we know that:
5028 win is the innermost window containing the pointer
5029 (XTC says it has no child containing the pointer),
5030 win_x and win_y are the pointer's position in it
5031 (XTC did this the last time through), and
5032 parent_x and parent_y are the pointer's position in win's parent.
5033 (They are what win_x and win_y were when win was child.
5034 If win is the root window, it has no parent, and
5035 parent_{x,y} are invalid, but that's okay, because we'll
5036 never use them in that case.) */
5038 #ifdef USE_GTK
5039 /* We don't wan't to know the innermost window. We
5040 want the edit window. */
5041 f1 = x_window_to_frame (dpyinfo, win);
5042 #else
5043 /* Is win one of our frames? */
5044 f1 = x_any_window_to_frame (dpyinfo, win);
5045 #endif
5047 #ifdef USE_X_TOOLKIT
5048 /* If we end up with the menu bar window, say it's not
5049 on the frame. */
5050 if (f1 != NULL
5051 && f1->output_data.x->menubar_widget
5052 && win == XtWindow (f1->output_data.x->menubar_widget))
5053 f1 = NULL;
5054 #endif /* USE_X_TOOLKIT */
5057 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
5058 f1 = 0;
5060 x_uncatch_errors_after_check ();
5062 /* If not, is it one of our scroll bars? */
5063 if (! f1)
5065 struct scroll_bar *bar;
5067 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win, 2);
5069 if (bar)
5071 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5072 win_x = parent_x;
5073 win_y = parent_y;
5077 if (f1 == 0 && insist > 0)
5078 f1 = SELECTED_FRAME ();
5080 if (f1)
5082 /* Ok, we found a frame. Store all the values.
5083 last_mouse_glyph is a rectangle used to reduce the
5084 generation of mouse events. To not miss any motion
5085 events, we must divide the frame into rectangles of the
5086 size of the smallest character that could be displayed
5087 on it, i.e. into the same rectangles that matrices on
5088 the frame are divided into. */
5090 /* FIXME: what if F1 is not an X frame? */
5091 dpyinfo = FRAME_DISPLAY_INFO (f1);
5092 remember_mouse_glyph (f1, win_x, win_y, &dpyinfo->last_mouse_glyph);
5093 dpyinfo->last_mouse_glyph_frame = f1;
5095 *bar_window = Qnil;
5096 *part = 0;
5097 *fp = f1;
5098 XSETINT (*x, win_x);
5099 XSETINT (*y, win_y);
5100 *timestamp = dpyinfo->last_mouse_movement_time;
5105 unblock_input ();
5110 /***********************************************************************
5111 Scroll bars
5112 ***********************************************************************/
5114 /* Scroll bar support. */
5116 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
5117 manages it.
5118 This can be called in GC, so we have to make sure to strip off mark
5119 bits. */
5121 static struct scroll_bar *
5122 x_window_to_scroll_bar (Display *display, Window window_id, int type)
5124 Lisp_Object tail, frame;
5126 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
5127 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
5128 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
5130 FOR_EACH_FRAME (tail, frame)
5132 Lisp_Object bar, condemned;
5134 if (! FRAME_X_P (XFRAME (frame)))
5135 continue;
5137 /* Scan this frame's scroll bar list for a scroll bar with the
5138 right window ID. */
5139 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
5140 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
5141 /* This trick allows us to search both the ordinary and
5142 condemned scroll bar lists with one loop. */
5143 ! NILP (bar) || (bar = condemned,
5144 condemned = Qnil,
5145 ! NILP (bar));
5146 bar = XSCROLL_BAR (bar)->next)
5147 if (XSCROLL_BAR (bar)->x_window == window_id
5148 && FRAME_X_DISPLAY (XFRAME (frame)) == display
5149 && (type = 2
5150 || (type == 1 && XSCROLL_BAR (bar)->horizontal)
5151 || (type == 0 && !XSCROLL_BAR (bar)->horizontal)))
5152 return XSCROLL_BAR (bar);
5155 return NULL;
5159 #if defined USE_LUCID
5161 /* Return the Lucid menu bar WINDOW is part of. Return null
5162 if WINDOW is not part of a menu bar. */
5164 static Widget
5165 x_window_to_menu_bar (Window window)
5167 Lisp_Object tail, frame;
5169 FOR_EACH_FRAME (tail, frame)
5170 if (FRAME_X_P (XFRAME (frame)))
5172 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
5174 if (menu_bar && xlwmenu_window_p (menu_bar, window))
5175 return menu_bar;
5177 return NULL;
5180 #endif /* USE_LUCID */
5183 /************************************************************************
5184 Toolkit scroll bars
5185 ************************************************************************/
5187 #ifdef USE_TOOLKIT_SCROLL_BARS
5189 static void x_send_scroll_bar_event (Lisp_Object, enum scroll_bar_part,
5190 int, int, bool);
5192 /* Lisp window being scrolled. Set when starting to interact with
5193 a toolkit scroll bar, reset to nil when ending the interaction. */
5195 static Lisp_Object window_being_scrolled;
5197 /* Whether this is an Xaw with arrow-scrollbars. This should imply
5198 that movements of 1/20 of the screen size are mapped to up/down. */
5200 #ifndef USE_GTK
5201 /* Id of action hook installed for scroll bars. */
5203 static XtActionHookId action_hook_id;
5204 static XtActionHookId horizontal_action_hook_id;
5206 static Boolean xaw3d_arrow_scroll;
5208 /* Whether the drag scrolling maintains the mouse at the top of the
5209 thumb. If not, resizing the thumb needs to be done more carefully
5210 to avoid jerkiness. */
5212 static Boolean xaw3d_pick_top;
5214 /* Action hook installed via XtAppAddActionHook when toolkit scroll
5215 bars are used.. The hook is responsible for detecting when
5216 the user ends an interaction with the scroll bar, and generates
5217 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
5219 static void
5220 xt_action_hook (Widget widget, XtPointer client_data, String action_name,
5221 XEvent *event, String *params, Cardinal *num_params)
5223 bool scroll_bar_p;
5224 const char *end_action;
5226 #ifdef USE_MOTIF
5227 scroll_bar_p = XmIsScrollBar (widget);
5228 end_action = "Release";
5229 #else /* !USE_MOTIF i.e. use Xaw */
5230 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
5231 end_action = "EndScroll";
5232 #endif /* USE_MOTIF */
5234 if (scroll_bar_p
5235 && strcmp (action_name, end_action) == 0
5236 && WINDOWP (window_being_scrolled))
5238 struct window *w;
5239 struct scroll_bar *bar;
5241 x_send_scroll_bar_event (window_being_scrolled,
5242 scroll_bar_end_scroll, 0, 0, false);
5243 w = XWINDOW (window_being_scrolled);
5244 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5246 if (bar->dragging != -1)
5248 bar->dragging = -1;
5249 /* The thumb size is incorrect while dragging: fix it. */
5250 set_vertical_scroll_bar (w);
5252 window_being_scrolled = Qnil;
5253 #if defined (USE_LUCID)
5254 bar->last_seen_part = scroll_bar_nowhere;
5255 #endif
5256 /* Xt timeouts no longer needed. */
5257 toolkit_scroll_bar_interaction = false;
5262 static void
5263 xt_horizontal_action_hook (Widget widget, XtPointer client_data, String action_name,
5264 XEvent *event, String *params, Cardinal *num_params)
5266 bool scroll_bar_p;
5267 const char *end_action;
5269 #ifdef USE_MOTIF
5270 scroll_bar_p = XmIsScrollBar (widget);
5271 end_action = "Release";
5272 #else /* !USE_MOTIF i.e. use Xaw */
5273 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
5274 end_action = "EndScroll";
5275 #endif /* USE_MOTIF */
5277 if (scroll_bar_p
5278 && strcmp (action_name, end_action) == 0
5279 && WINDOWP (window_being_scrolled))
5281 struct window *w;
5282 struct scroll_bar *bar;
5284 x_send_scroll_bar_event (window_being_scrolled,
5285 scroll_bar_end_scroll, 0, 0, true);
5286 w = XWINDOW (window_being_scrolled);
5287 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
5289 if (bar->dragging != -1)
5291 bar->dragging = -1;
5292 /* The thumb size is incorrect while dragging: fix it. */
5293 set_horizontal_scroll_bar (w);
5295 window_being_scrolled = Qnil;
5296 #if defined (USE_LUCID)
5297 bar->last_seen_part = scroll_bar_nowhere;
5298 #endif
5299 /* Xt timeouts no longer needed. */
5300 toolkit_scroll_bar_interaction = false;
5303 #endif /* not USE_GTK */
5305 /* Send a client message with message type Xatom_Scrollbar for a
5306 scroll action to the frame of WINDOW. PART is a value identifying
5307 the part of the scroll bar that was clicked on. PORTION is the
5308 amount to scroll of a whole of WHOLE. */
5310 static void
5311 x_send_scroll_bar_event (Lisp_Object window, enum scroll_bar_part part,
5312 int portion, int whole, bool horizontal)
5314 XEvent event;
5315 XClientMessageEvent *ev = &event.xclient;
5316 struct window *w = XWINDOW (window);
5317 struct frame *f = XFRAME (w->frame);
5318 intptr_t iw = (intptr_t) w;
5319 verify (INTPTR_WIDTH <= 64);
5320 int sign_shift = INTPTR_WIDTH - 32;
5322 block_input ();
5324 /* Construct a ClientMessage event to send to the frame. */
5325 ev->type = ClientMessage;
5326 ev->message_type = (horizontal
5327 ? FRAME_DISPLAY_INFO (f)->Xatom_Horizontal_Scrollbar
5328 : FRAME_DISPLAY_INFO (f)->Xatom_Scrollbar);
5329 ev->display = FRAME_X_DISPLAY (f);
5330 ev->window = FRAME_X_WINDOW (f);
5331 ev->format = 32;
5333 /* A 32-bit X client on a 64-bit X server can pass a window pointer
5334 as-is. A 64-bit client on a 32-bit X server is in trouble
5335 because a pointer does not fit and would be truncated while
5336 passing through the server. So use two slots and hope that X12
5337 will resolve such issues someday. */
5338 ev->data.l[0] = iw >> 31 >> 1;
5339 ev->data.l[1] = sign_shift <= 0 ? iw : iw << sign_shift >> sign_shift;
5340 ev->data.l[2] = part;
5341 ev->data.l[3] = portion;
5342 ev->data.l[4] = whole;
5344 /* Make Xt timeouts work while the scroll bar is active. */
5345 #ifdef USE_X_TOOLKIT
5346 toolkit_scroll_bar_interaction = true;
5347 x_activate_timeout_atimer ();
5348 #endif
5350 /* Setting the event mask to zero means that the message will
5351 be sent to the client that created the window, and if that
5352 window no longer exists, no event will be sent. */
5353 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
5354 unblock_input ();
5358 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
5359 in *IEVENT. */
5361 static void
5362 x_scroll_bar_to_input_event (const XEvent *event,
5363 struct input_event *ievent)
5365 const XClientMessageEvent *ev = &event->xclient;
5366 Lisp_Object window;
5367 struct window *w;
5369 /* See the comment in the function above. */
5370 intptr_t iw0 = ev->data.l[0];
5371 intptr_t iw1 = ev->data.l[1];
5372 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
5373 w = (struct window *) iw;
5375 XSETWINDOW (window, w);
5377 ievent->kind = SCROLL_BAR_CLICK_EVENT;
5378 ievent->frame_or_window = window;
5379 ievent->arg = Qnil;
5380 #ifdef USE_GTK
5381 ievent->timestamp = CurrentTime;
5382 #else
5383 ievent->timestamp =
5384 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
5385 #endif
5386 ievent->code = 0;
5387 ievent->part = ev->data.l[2];
5388 ievent->x = make_number (ev->data.l[3]);
5389 ievent->y = make_number (ev->data.l[4]);
5390 ievent->modifiers = 0;
5393 /* Transform a horizontal scroll bar ClientMessage EVENT to an Emacs
5394 input event in *IEVENT. */
5396 static void
5397 x_horizontal_scroll_bar_to_input_event (const XEvent *event,
5398 struct input_event *ievent)
5400 const XClientMessageEvent *ev = &event->xclient;
5401 Lisp_Object window;
5402 struct window *w;
5404 /* See the comment in the function above. */
5405 intptr_t iw0 = ev->data.l[0];
5406 intptr_t iw1 = ev->data.l[1];
5407 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
5408 w = (struct window *) iw;
5410 XSETWINDOW (window, w);
5412 ievent->kind = HORIZONTAL_SCROLL_BAR_CLICK_EVENT;
5413 ievent->frame_or_window = window;
5414 ievent->arg = Qnil;
5415 #ifdef USE_GTK
5416 ievent->timestamp = CurrentTime;
5417 #else
5418 ievent->timestamp =
5419 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
5420 #endif
5421 ievent->code = 0;
5422 ievent->part = ev->data.l[2];
5423 ievent->x = make_number (ev->data.l[3]);
5424 ievent->y = make_number (ev->data.l[4]);
5425 ievent->modifiers = 0;
5429 #ifdef USE_MOTIF
5431 /* Minimum and maximum values used for Motif scroll bars. */
5433 #define XM_SB_MAX 10000000
5435 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
5436 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
5437 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
5439 static void
5440 xm_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5442 struct scroll_bar *bar = client_data;
5443 XmScrollBarCallbackStruct *cs = call_data;
5444 enum scroll_bar_part part = scroll_bar_nowhere;
5445 bool horizontal = bar->horizontal;
5446 int whole = 0, portion = 0;
5448 switch (cs->reason)
5450 case XmCR_DECREMENT:
5451 bar->dragging = -1;
5452 part = horizontal ? scroll_bar_left_arrow : scroll_bar_up_arrow;
5453 break;
5455 case XmCR_INCREMENT:
5456 bar->dragging = -1;
5457 part = horizontal ? scroll_bar_right_arrow : scroll_bar_down_arrow;
5458 break;
5460 case XmCR_PAGE_DECREMENT:
5461 bar->dragging = -1;
5462 part = horizontal ? scroll_bar_before_handle : scroll_bar_above_handle;
5463 break;
5465 case XmCR_PAGE_INCREMENT:
5466 bar->dragging = -1;
5467 part = horizontal ? scroll_bar_after_handle : scroll_bar_below_handle;
5468 break;
5470 case XmCR_TO_TOP:
5471 bar->dragging = -1;
5472 part = horizontal ? scroll_bar_to_leftmost : scroll_bar_to_top;
5473 break;
5475 case XmCR_TO_BOTTOM:
5476 bar->dragging = -1;
5477 part = horizontal ? scroll_bar_to_rightmost : scroll_bar_to_bottom;
5478 break;
5480 case XmCR_DRAG:
5482 int slider_size;
5484 block_input ();
5485 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
5486 unblock_input ();
5488 if (horizontal)
5490 portion = bar->whole * ((float)cs->value / XM_SB_MAX);
5491 whole = bar->whole * ((float)(XM_SB_MAX - slider_size) / XM_SB_MAX);
5492 portion = min (portion, whole);
5493 part = scroll_bar_horizontal_handle;
5495 else
5497 whole = XM_SB_MAX - slider_size;
5498 portion = min (cs->value, whole);
5499 part = scroll_bar_handle;
5502 bar->dragging = cs->value;
5504 break;
5506 case XmCR_VALUE_CHANGED:
5507 break;
5510 if (part != scroll_bar_nowhere)
5512 window_being_scrolled = bar->window;
5513 x_send_scroll_bar_event (bar->window, part, portion, whole,
5514 bar->horizontal);
5518 #elif defined USE_GTK
5520 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
5521 bar widget. DATA is a pointer to the scroll_bar structure. */
5523 static gboolean
5524 xg_scroll_callback (GtkRange *range,
5525 GtkScrollType scroll,
5526 gdouble value,
5527 gpointer user_data)
5529 int whole = 0, portion = 0;
5530 struct scroll_bar *bar = user_data;
5531 enum scroll_bar_part part = scroll_bar_nowhere;
5532 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
5533 struct frame *f = g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
5535 if (xg_ignore_gtk_scrollbar) return false;
5537 switch (scroll)
5539 case GTK_SCROLL_JUMP:
5540 /* Buttons 1 2 or 3 must be grabbed. */
5541 if (FRAME_DISPLAY_INFO (f)->grabbed != 0
5542 && FRAME_DISPLAY_INFO (f)->grabbed < (1 << 4))
5544 if (bar->horizontal)
5546 part = scroll_bar_horizontal_handle;
5547 whole = (int)(gtk_adjustment_get_upper (adj) -
5548 gtk_adjustment_get_page_size (adj));
5549 portion = min ((int)value, whole);
5550 bar->dragging = portion;
5552 else
5554 part = scroll_bar_handle;
5555 whole = gtk_adjustment_get_upper (adj) -
5556 gtk_adjustment_get_page_size (adj);
5557 portion = min ((int)value, whole);
5558 bar->dragging = portion;
5561 break;
5562 case GTK_SCROLL_STEP_BACKWARD:
5563 part = (bar->horizontal
5564 ? scroll_bar_left_arrow : scroll_bar_up_arrow);
5565 bar->dragging = -1;
5566 break;
5567 case GTK_SCROLL_STEP_FORWARD:
5568 part = (bar->horizontal
5569 ? scroll_bar_right_arrow : scroll_bar_down_arrow);
5570 bar->dragging = -1;
5571 break;
5572 case GTK_SCROLL_PAGE_BACKWARD:
5573 part = (bar->horizontal
5574 ? scroll_bar_before_handle : scroll_bar_above_handle);
5575 bar->dragging = -1;
5576 break;
5577 case GTK_SCROLL_PAGE_FORWARD:
5578 part = (bar->horizontal
5579 ? scroll_bar_after_handle : scroll_bar_below_handle);
5580 bar->dragging = -1;
5581 break;
5582 default:
5583 break;
5586 if (part != scroll_bar_nowhere)
5588 window_being_scrolled = bar->window;
5589 x_send_scroll_bar_event (bar->window, part, portion, whole,
5590 bar->horizontal);
5593 return false;
5596 /* Callback for button release. Sets dragging to -1 when dragging is done. */
5598 static gboolean
5599 xg_end_scroll_callback (GtkWidget *widget,
5600 GdkEventButton *event,
5601 gpointer user_data)
5603 struct scroll_bar *bar = user_data;
5604 bar->dragging = -1;
5605 if (WINDOWP (window_being_scrolled))
5607 x_send_scroll_bar_event (window_being_scrolled,
5608 scroll_bar_end_scroll, 0, 0, bar->horizontal);
5609 window_being_scrolled = Qnil;
5612 return false;
5616 #else /* not USE_GTK and not USE_MOTIF */
5618 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
5619 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
5620 scroll bar struct. CALL_DATA is a pointer to a float saying where
5621 the thumb is. */
5623 static void
5624 xaw_jump_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5626 struct scroll_bar *bar = client_data;
5627 float *top_addr = call_data;
5628 float top = *top_addr;
5629 float shown;
5630 int whole, portion, height, width;
5631 enum scroll_bar_part part;
5632 bool horizontal = bar->horizontal;
5635 if (horizontal)
5637 /* Get the size of the thumb, a value between 0 and 1. */
5638 block_input ();
5639 XtVaGetValues (widget, XtNshown, &shown, XtNwidth, &width, NULL);
5640 unblock_input ();
5642 if (shown < 1)
5644 whole = bar->whole - (shown * bar->whole);
5645 portion = min (top * bar->whole, whole);
5647 else
5649 whole = bar->whole;
5650 portion = 0;
5653 part = scroll_bar_horizontal_handle;
5655 else
5657 /* Get the size of the thumb, a value between 0 and 1. */
5658 block_input ();
5659 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
5660 unblock_input ();
5662 whole = 10000000;
5663 portion = shown < 1 ? top * whole : 0;
5665 if (shown < 1 && (eabs (top + shown - 1) < 1.0f / height))
5666 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
5667 the bottom, so we force the scrolling whenever we see that we're
5668 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
5669 we try to ensure that we always stay two pixels away from the
5670 bottom). */
5671 part = scroll_bar_down_arrow;
5672 else
5673 part = scroll_bar_handle;
5676 window_being_scrolled = bar->window;
5677 bar->dragging = portion;
5678 bar->last_seen_part = part;
5679 x_send_scroll_bar_event (bar->window, part, portion, whole, bar->horizontal);
5683 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
5684 i.e. line or page up or down. WIDGET is the Xaw scroll bar
5685 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
5686 the scroll bar. CALL_DATA is an integer specifying the action that
5687 has taken place. Its magnitude is in the range 0..height of the
5688 scroll bar. Negative values mean scroll towards buffer start.
5689 Values < height of scroll bar mean line-wise movement. */
5691 static void
5692 xaw_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5694 struct scroll_bar *bar = client_data;
5695 /* The position really is stored cast to a pointer. */
5696 int position = (intptr_t) call_data;
5697 Dimension height, width;
5698 enum scroll_bar_part part;
5700 if (bar->horizontal)
5702 /* Get the width of the scroll bar. */
5703 block_input ();
5704 XtVaGetValues (widget, XtNwidth, &width, NULL);
5705 unblock_input ();
5707 if (eabs (position) >= width)
5708 part = (position < 0) ? scroll_bar_before_handle : scroll_bar_after_handle;
5710 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
5711 it maps line-movement to call_data = max(5, height/20). */
5712 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, width / 20))
5713 part = (position < 0) ? scroll_bar_left_arrow : scroll_bar_right_arrow;
5714 else
5715 part = scroll_bar_move_ratio;
5717 window_being_scrolled = bar->window;
5718 bar->dragging = -1;
5719 bar->last_seen_part = part;
5720 x_send_scroll_bar_event (bar->window, part, position, width,
5721 bar->horizontal);
5723 else
5726 /* Get the height of the scroll bar. */
5727 block_input ();
5728 XtVaGetValues (widget, XtNheight, &height, NULL);
5729 unblock_input ();
5731 if (eabs (position) >= height)
5732 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
5734 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
5735 it maps line-movement to call_data = max(5, height/20). */
5736 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
5737 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
5738 else
5739 part = scroll_bar_move_ratio;
5741 window_being_scrolled = bar->window;
5742 bar->dragging = -1;
5743 bar->last_seen_part = part;
5744 x_send_scroll_bar_event (bar->window, part, position, height,
5745 bar->horizontal);
5749 #endif /* not USE_GTK and not USE_MOTIF */
5751 #define SCROLL_BAR_NAME "verticalScrollBar"
5752 #define SCROLL_BAR_HORIZONTAL_NAME "horizontalScrollBar"
5754 /* Create the widget for scroll bar BAR on frame F. Record the widget
5755 and X window of the scroll bar in BAR. */
5757 #ifdef USE_GTK
5758 static void
5759 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5761 const char *scroll_bar_name = SCROLL_BAR_NAME;
5763 block_input ();
5764 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
5765 G_CALLBACK (xg_end_scroll_callback),
5766 scroll_bar_name);
5767 unblock_input ();
5770 static void
5771 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5773 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
5775 block_input ();
5776 xg_create_horizontal_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
5777 G_CALLBACK (xg_end_scroll_callback),
5778 scroll_bar_name);
5779 unblock_input ();
5782 #else /* not USE_GTK */
5784 static void
5785 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5787 Window xwindow;
5788 Widget widget;
5789 Arg av[20];
5790 int ac = 0;
5791 const char *scroll_bar_name = SCROLL_BAR_NAME;
5792 unsigned long pixel;
5794 block_input ();
5796 #ifdef USE_MOTIF
5797 /* Set resources. Create the widget. */
5798 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5799 XtSetArg (av[ac], XmNminimum, 0); ++ac;
5800 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
5801 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
5802 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
5803 XtSetArg (av[ac], XmNincrement, 1); ++ac;
5804 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
5806 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5807 if (pixel != -1)
5809 XtSetArg (av[ac], XmNforeground, pixel);
5810 ++ac;
5813 pixel = f->output_data.x->scroll_bar_background_pixel;
5814 if (pixel != -1)
5816 XtSetArg (av[ac], XmNbackground, pixel);
5817 ++ac;
5820 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
5821 (char *) scroll_bar_name, av, ac);
5823 /* Add one callback for everything that can happen. */
5824 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
5825 (XtPointer) bar);
5826 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
5827 (XtPointer) bar);
5828 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
5829 (XtPointer) bar);
5830 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
5831 (XtPointer) bar);
5832 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
5833 (XtPointer) bar);
5834 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
5835 (XtPointer) bar);
5836 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
5837 (XtPointer) bar);
5839 /* Realize the widget. Only after that is the X window created. */
5840 XtRealizeWidget (widget);
5842 /* Set the cursor to an arrow. I didn't find a resource to do that.
5843 And I'm wondering why it hasn't an arrow cursor by default. */
5844 XDefineCursor (XtDisplay (widget), XtWindow (widget),
5845 f->output_data.x->nontext_cursor);
5847 #else /* !USE_MOTIF i.e. use Xaw */
5849 /* Set resources. Create the widget. The background of the
5850 Xaw3d scroll bar widget is a little bit light for my taste.
5851 We don't alter it here to let users change it according
5852 to their taste with `emacs*verticalScrollBar.background: xxx'. */
5853 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5854 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
5855 /* For smoother scrolling with Xaw3d -sm */
5856 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
5858 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5859 if (pixel != -1)
5861 XtSetArg (av[ac], XtNforeground, pixel);
5862 ++ac;
5865 pixel = f->output_data.x->scroll_bar_background_pixel;
5866 if (pixel != -1)
5868 XtSetArg (av[ac], XtNbackground, pixel);
5869 ++ac;
5872 /* Top/bottom shadow colors. */
5874 /* Allocate them, if necessary. */
5875 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
5877 pixel = f->output_data.x->scroll_bar_background_pixel;
5878 if (pixel != -1)
5880 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5881 FRAME_X_COLORMAP (f),
5882 &pixel, 1.2, 0x8000))
5883 pixel = -1;
5884 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
5887 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5889 pixel = f->output_data.x->scroll_bar_background_pixel;
5890 if (pixel != -1)
5892 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5893 FRAME_X_COLORMAP (f),
5894 &pixel, 0.6, 0x4000))
5895 pixel = -1;
5896 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
5900 #ifdef XtNbeNiceToColormap
5901 /* Tell the toolkit about them. */
5902 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
5903 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5904 /* We tried to allocate a color for the top/bottom shadow, and
5905 failed, so tell Xaw3d to use dithering instead. */
5906 /* But only if we have a small colormap. Xaw3d can allocate nice
5907 colors itself. */
5909 XtSetArg (av[ac], XtNbeNiceToColormap,
5910 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
5911 ++ac;
5913 else
5914 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
5915 be more consistent with other emacs 3d colors, and since Xaw3d is
5916 not good at dealing with allocation failure. */
5918 /* This tells Xaw3d to use real colors instead of dithering for
5919 the shadows. */
5920 XtSetArg (av[ac], XtNbeNiceToColormap, False);
5921 ++ac;
5923 /* Specify the colors. */
5924 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
5925 if (pixel != -1)
5927 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
5928 ++ac;
5930 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
5931 if (pixel != -1)
5933 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
5934 ++ac;
5937 #endif
5939 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
5940 f->output_data.x->edit_widget, av, ac);
5943 char const *initial = "";
5944 char const *val = initial;
5945 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
5946 #ifdef XtNarrowScrollbars
5947 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
5948 #endif
5949 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
5950 if (xaw3d_arrow_scroll || val == initial)
5951 { /* ARROW_SCROLL */
5952 xaw3d_arrow_scroll = True;
5953 /* Isn't that just a personal preference ? --Stef */
5954 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
5958 /* Define callbacks. */
5959 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
5960 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
5961 (XtPointer) bar);
5963 /* Realize the widget. Only after that is the X window created. */
5964 XtRealizeWidget (widget);
5966 #endif /* !USE_MOTIF */
5968 /* Install an action hook that lets us detect when the user
5969 finishes interacting with a scroll bar. */
5970 if (action_hook_id == 0)
5971 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
5973 /* Remember X window and widget in the scroll bar vector. */
5974 SET_SCROLL_BAR_X_WIDGET (bar, widget);
5975 xwindow = XtWindow (widget);
5976 bar->x_window = xwindow;
5977 bar->whole = 1;
5978 bar->horizontal = false;
5980 unblock_input ();
5983 static void
5984 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5986 Window xwindow;
5987 Widget widget;
5988 Arg av[20];
5989 int ac = 0;
5990 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
5991 unsigned long pixel;
5993 block_input ();
5995 #ifdef USE_MOTIF
5996 /* Set resources. Create the widget. */
5997 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5998 XtSetArg (av[ac], XmNminimum, 0); ++ac;
5999 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
6000 XtSetArg (av[ac], XmNorientation, XmHORIZONTAL); ++ac;
6001 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_RIGHT), ++ac;
6002 XtSetArg (av[ac], XmNincrement, 1); ++ac;
6003 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
6005 pixel = f->output_data.x->scroll_bar_foreground_pixel;
6006 if (pixel != -1)
6008 XtSetArg (av[ac], XmNforeground, pixel);
6009 ++ac;
6012 pixel = f->output_data.x->scroll_bar_background_pixel;
6013 if (pixel != -1)
6015 XtSetArg (av[ac], XmNbackground, pixel);
6016 ++ac;
6019 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
6020 (char *) scroll_bar_name, av, ac);
6022 /* Add one callback for everything that can happen. */
6023 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
6024 (XtPointer) bar);
6025 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
6026 (XtPointer) bar);
6027 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
6028 (XtPointer) bar);
6029 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
6030 (XtPointer) bar);
6031 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
6032 (XtPointer) bar);
6033 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
6034 (XtPointer) bar);
6035 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
6036 (XtPointer) bar);
6038 /* Realize the widget. Only after that is the X window created. */
6039 XtRealizeWidget (widget);
6041 /* Set the cursor to an arrow. I didn't find a resource to do that.
6042 And I'm wondering why it hasn't an arrow cursor by default. */
6043 XDefineCursor (XtDisplay (widget), XtWindow (widget),
6044 f->output_data.x->nontext_cursor);
6046 #else /* !USE_MOTIF i.e. use Xaw */
6048 /* Set resources. Create the widget. The background of the
6049 Xaw3d scroll bar widget is a little bit light for my taste.
6050 We don't alter it here to let users change it according
6051 to their taste with `emacs*verticalScrollBar.background: xxx'. */
6052 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
6053 XtSetArg (av[ac], XtNorientation, XtorientHorizontal); ++ac;
6054 /* For smoother scrolling with Xaw3d -sm */
6055 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
6057 pixel = f->output_data.x->scroll_bar_foreground_pixel;
6058 if (pixel != -1)
6060 XtSetArg (av[ac], XtNforeground, pixel);
6061 ++ac;
6064 pixel = f->output_data.x->scroll_bar_background_pixel;
6065 if (pixel != -1)
6067 XtSetArg (av[ac], XtNbackground, pixel);
6068 ++ac;
6071 /* Top/bottom shadow colors. */
6073 /* Allocate them, if necessary. */
6074 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
6076 pixel = f->output_data.x->scroll_bar_background_pixel;
6077 if (pixel != -1)
6079 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
6080 FRAME_X_COLORMAP (f),
6081 &pixel, 1.2, 0x8000))
6082 pixel = -1;
6083 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
6086 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
6088 pixel = f->output_data.x->scroll_bar_background_pixel;
6089 if (pixel != -1)
6091 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
6092 FRAME_X_COLORMAP (f),
6093 &pixel, 0.6, 0x4000))
6094 pixel = -1;
6095 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
6099 #ifdef XtNbeNiceToColormap
6100 /* Tell the toolkit about them. */
6101 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
6102 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
6103 /* We tried to allocate a color for the top/bottom shadow, and
6104 failed, so tell Xaw3d to use dithering instead. */
6105 /* But only if we have a small colormap. Xaw3d can allocate nice
6106 colors itself. */
6108 XtSetArg (av[ac], XtNbeNiceToColormap,
6109 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
6110 ++ac;
6112 else
6113 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
6114 be more consistent with other emacs 3d colors, and since Xaw3d is
6115 not good at dealing with allocation failure. */
6117 /* This tells Xaw3d to use real colors instead of dithering for
6118 the shadows. */
6119 XtSetArg (av[ac], XtNbeNiceToColormap, False);
6120 ++ac;
6122 /* Specify the colors. */
6123 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
6124 if (pixel != -1)
6126 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
6127 ++ac;
6129 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
6130 if (pixel != -1)
6132 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
6133 ++ac;
6136 #endif
6138 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
6139 f->output_data.x->edit_widget, av, ac);
6142 char const *initial = "";
6143 char const *val = initial;
6144 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
6145 #ifdef XtNarrowScrollbars
6146 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
6147 #endif
6148 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
6149 if (xaw3d_arrow_scroll || val == initial)
6150 { /* ARROW_SCROLL */
6151 xaw3d_arrow_scroll = True;
6152 /* Isn't that just a personal preference ? --Stef */
6153 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
6157 /* Define callbacks. */
6158 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
6159 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
6160 (XtPointer) bar);
6162 /* Realize the widget. Only after that is the X window created. */
6163 XtRealizeWidget (widget);
6165 #endif /* !USE_MOTIF */
6167 /* Install an action hook that lets us detect when the user
6168 finishes interacting with a scroll bar. */
6169 if (horizontal_action_hook_id == 0)
6170 horizontal_action_hook_id
6171 = XtAppAddActionHook (Xt_app_con, xt_horizontal_action_hook, 0);
6173 /* Remember X window and widget in the scroll bar vector. */
6174 SET_SCROLL_BAR_X_WIDGET (bar, widget);
6175 xwindow = XtWindow (widget);
6176 bar->x_window = xwindow;
6177 bar->whole = 1;
6178 bar->horizontal = true;
6180 unblock_input ();
6182 #endif /* not USE_GTK */
6185 /* Set the thumb size and position of scroll bar BAR. We are currently
6186 displaying PORTION out of a whole WHOLE, and our position POSITION. */
6188 #ifdef USE_GTK
6189 static void
6190 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
6192 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
6195 static void
6196 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
6198 xg_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
6201 #else /* not USE_GTK */
6202 static void
6203 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
6204 int whole)
6206 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6207 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6208 float top, shown;
6210 block_input ();
6212 #ifdef USE_MOTIF
6214 if (scroll_bar_adjust_thumb_portion_p)
6216 /* We use an estimate of 30 chars per line rather than the real
6217 `portion' value. This has the disadvantage that the thumb size
6218 is not very representative, but it makes our life a lot easier.
6219 Otherwise, we have to constantly adjust the thumb size, which
6220 we can't always do quickly enough: while dragging, the size of
6221 the thumb might prevent the user from dragging the thumb all the
6222 way to the end. but Motif and some versions of Xaw3d don't allow
6223 updating the thumb size while dragging. Also, even if we can update
6224 its size, the update will often happen too late.
6225 If you don't believe it, check out revision 1.650 of xterm.c to see
6226 what hoops we were going through and the still poor behavior we got. */
6227 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
6228 /* When the thumb is at the bottom, position == whole.
6229 So we need to increase `whole' to make space for the thumb. */
6230 whole += portion;
6233 if (whole <= 0)
6234 top = 0, shown = 1;
6235 else
6237 top = (float) position / whole;
6238 shown = (float) portion / whole;
6241 if (bar->dragging == -1)
6243 int size, value;
6245 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
6246 is the scroll bar's maximum and MIN is the scroll bar's minimum
6247 value. */
6248 size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
6250 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
6251 value = top * XM_SB_MAX;
6252 value = min (value, XM_SB_MAX - size);
6254 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6256 #else /* !USE_MOTIF i.e. use Xaw */
6258 if (whole == 0)
6259 top = 0, shown = 1;
6260 else
6262 top = (float) position / whole;
6263 shown = (float) portion / whole;
6267 float old_top, old_shown;
6268 Dimension height;
6269 XtVaGetValues (widget,
6270 XtNtopOfThumb, &old_top,
6271 XtNshown, &old_shown,
6272 XtNheight, &height,
6273 NULL);
6275 /* Massage the top+shown values. */
6276 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
6277 top = max (0, min (1, top));
6278 else
6279 top = old_top;
6280 #if ! defined (HAVE_XAW3D)
6281 /* With Xaw, 'top' values too closer to 1.0 may
6282 cause the thumb to disappear. Fix that. */
6283 top = min (top, 0.99f);
6284 #endif
6285 /* Keep two pixels available for moving the thumb down. */
6286 shown = max (0, min (1 - top - (2.0f / height), shown));
6287 #if ! defined (HAVE_XAW3D)
6288 /* Likewise with too small 'shown'. */
6289 shown = max (shown, 0.01f);
6290 #endif
6292 /* If the call to XawScrollbarSetThumb below doesn't seem to
6293 work, check that 'NARROWPROTO' is defined in src/config.h.
6294 If this is not so, most likely you need to fix configure. */
6295 if (top != old_top || shown != old_shown)
6297 if (bar->dragging == -1)
6298 XawScrollbarSetThumb (widget, top, shown);
6299 else
6301 /* Try to make the scrolling a tad smoother. */
6302 if (!xaw3d_pick_top)
6303 shown = min (shown, old_shown);
6305 XawScrollbarSetThumb (widget, top, shown);
6309 #endif /* !USE_MOTIF */
6311 unblock_input ();
6314 static void
6315 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
6316 int whole)
6318 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6319 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6320 float top, shown;
6322 block_input ();
6324 #ifdef USE_MOTIF
6325 bar->whole = whole;
6326 shown = (float) portion / whole;
6327 top = (float) position / (whole - portion);
6329 int size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
6330 int value = clip_to_bounds (0, top * (XM_SB_MAX - size), XM_SB_MAX - size);
6332 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6334 #else /* !USE_MOTIF i.e. use Xaw */
6335 bar->whole = whole;
6336 if (whole == 0)
6337 top = 0, shown = 1;
6338 else
6340 top = (float) position / whole;
6341 shown = (float) portion / whole;
6345 float old_top, old_shown;
6346 Dimension height;
6347 XtVaGetValues (widget,
6348 XtNtopOfThumb, &old_top,
6349 XtNshown, &old_shown,
6350 XtNheight, &height,
6351 NULL);
6353 #if false
6354 /* Massage the top+shown values. */
6355 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
6356 top = max (0, min (1, top));
6357 else
6358 top = old_top;
6359 #if ! defined (HAVE_XAW3D)
6360 /* With Xaw, 'top' values too closer to 1.0 may
6361 cause the thumb to disappear. Fix that. */
6362 top = min (top, 0.99f);
6363 #endif
6364 /* Keep two pixels available for moving the thumb down. */
6365 shown = max (0, min (1 - top - (2.0f / height), shown));
6366 #if ! defined (HAVE_XAW3D)
6367 /* Likewise with too small 'shown'. */
6368 shown = max (shown, 0.01f);
6369 #endif
6370 #endif
6372 /* If the call to XawScrollbarSetThumb below doesn't seem to
6373 work, check that 'NARROWPROTO' is defined in src/config.h.
6374 If this is not so, most likely you need to fix configure. */
6375 XawScrollbarSetThumb (widget, top, shown);
6376 #if false
6377 if (top != old_top || shown != old_shown)
6379 if (bar->dragging == -1)
6380 XawScrollbarSetThumb (widget, top, shown);
6381 else
6383 /* Try to make the scrolling a tad smoother. */
6384 if (!xaw3d_pick_top)
6385 shown = min (shown, old_shown);
6387 XawScrollbarSetThumb (widget, top, shown);
6390 #endif
6392 #endif /* !USE_MOTIF */
6394 unblock_input ();
6396 #endif /* not USE_GTK */
6398 #endif /* USE_TOOLKIT_SCROLL_BARS */
6402 /************************************************************************
6403 Scroll bars, general
6404 ************************************************************************/
6406 /* Create a scroll bar and return the scroll bar vector for it. W is
6407 the Emacs window on which to create the scroll bar. TOP, LEFT,
6408 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
6409 scroll bar. */
6411 static struct scroll_bar *
6412 x_scroll_bar_create (struct window *w, int top, int left,
6413 int width, int height, bool horizontal)
6415 struct frame *f = XFRAME (w->frame);
6416 struct scroll_bar *bar
6417 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
6418 Lisp_Object barobj;
6420 block_input ();
6422 #ifdef USE_TOOLKIT_SCROLL_BARS
6423 if (horizontal)
6424 x_create_horizontal_toolkit_scroll_bar (f, bar);
6425 else
6426 x_create_toolkit_scroll_bar (f, bar);
6427 #else /* not USE_TOOLKIT_SCROLL_BARS */
6429 XSetWindowAttributes a;
6430 unsigned long mask;
6431 Window window;
6433 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
6434 if (a.background_pixel == -1)
6435 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
6437 a.event_mask = (ButtonPressMask | ButtonReleaseMask
6438 | ButtonMotionMask | PointerMotionHintMask
6439 | ExposureMask);
6440 a.cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
6442 mask = (CWBackPixel | CWEventMask | CWCursor);
6444 /* Clear the area of W that will serve as a scroll bar. This is
6445 for the case that a window has been split horizontally. In
6446 this case, no clear_frame is generated to reduce flickering. */
6447 if (width > 0 && window_box_height (w) > 0)
6448 x_clear_area (f, left, top, width, window_box_height (w));
6450 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6451 /* Position and size of scroll bar. */
6452 left, top, width, height,
6453 /* Border width, depth, class, and visual. */
6455 CopyFromParent,
6456 CopyFromParent,
6457 CopyFromParent,
6458 /* Attributes. */
6459 mask, &a);
6460 bar->x_window = window;
6462 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6464 XSETWINDOW (bar->window, w);
6465 bar->top = top;
6466 bar->left = left;
6467 bar->width = width;
6468 bar->height = height;
6469 bar->start = 0;
6470 bar->end = 0;
6471 bar->dragging = -1;
6472 bar->horizontal = horizontal;
6473 #if defined (USE_TOOLKIT_SCROLL_BARS) && defined (USE_LUCID)
6474 bar->last_seen_part = scroll_bar_nowhere;
6475 #endif
6477 /* Add bar to its frame's list of scroll bars. */
6478 bar->next = FRAME_SCROLL_BARS (f);
6479 bar->prev = Qnil;
6480 XSETVECTOR (barobj, bar);
6481 fset_scroll_bars (f, barobj);
6482 if (!NILP (bar->next))
6483 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6485 /* Map the window/widget. */
6486 #ifdef USE_TOOLKIT_SCROLL_BARS
6488 #ifdef USE_GTK
6489 if (horizontal)
6490 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top,
6491 left, width, max (height, 1));
6492 else
6493 xg_update_scrollbar_pos (f, bar->x_window, top,
6494 left, width, max (height, 1));
6495 #else /* not USE_GTK */
6496 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6497 XtConfigureWidget (scroll_bar, left, top, width, max (height, 1), 0);
6498 XtMapWidget (scroll_bar);
6499 #endif /* not USE_GTK */
6501 #else /* not USE_TOOLKIT_SCROLL_BARS */
6502 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
6503 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6505 unblock_input ();
6506 return bar;
6510 #ifndef USE_TOOLKIT_SCROLL_BARS
6512 /* Draw BAR's handle in the proper position.
6514 If the handle is already drawn from START to END, don't bother
6515 redrawing it, unless REBUILD; in that case, always
6516 redraw it. (REBUILD is handy for drawing the handle after expose
6517 events.)
6519 Normally, we want to constrain the start and end of the handle to
6520 fit inside its rectangle, but if the user is dragging the scroll
6521 bar handle, we want to let them drag it down all the way, so that
6522 the bar's top is as far down as it goes; otherwise, there's no way
6523 to move to the very end of the buffer. */
6525 static void
6526 x_scroll_bar_set_handle (struct scroll_bar *bar, int start, int end,
6527 bool rebuild)
6529 bool dragging = bar->dragging != -1;
6530 Window w = bar->x_window;
6531 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6532 GC gc = f->output_data.x->normal_gc;
6534 /* If the display is already accurate, do nothing. */
6535 if (! rebuild
6536 && start == bar->start
6537 && end == bar->end)
6538 return;
6540 block_input ();
6543 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
6544 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
6545 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
6547 /* Make sure the values are reasonable, and try to preserve
6548 the distance between start and end. */
6550 int length = end - start;
6552 if (start < 0)
6553 start = 0;
6554 else if (start > top_range)
6555 start = top_range;
6556 end = start + length;
6558 if (end < start)
6559 end = start;
6560 else if (end > top_range && ! dragging)
6561 end = top_range;
6564 /* Store the adjusted setting in the scroll bar. */
6565 bar->start = start;
6566 bar->end = end;
6568 /* Clip the end position, just for display. */
6569 if (end > top_range)
6570 end = top_range;
6572 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
6573 below top positions, to make sure the handle is always at least
6574 that many pixels tall. */
6575 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
6577 /* Draw the empty space above the handle. Note that we can't clear
6578 zero-height areas; that means "clear to end of window." */
6579 if ((inside_width > 0) && (start > 0))
6580 x_clear_area1 (FRAME_X_DISPLAY (f), w,
6581 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6582 VERTICAL_SCROLL_BAR_TOP_BORDER,
6583 inside_width, start, False);
6585 /* Change to proper foreground color if one is specified. */
6586 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6587 XSetForeground (FRAME_X_DISPLAY (f), gc,
6588 f->output_data.x->scroll_bar_foreground_pixel);
6590 /* Draw the handle itself. */
6591 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
6592 /* x, y, width, height */
6593 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6594 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
6595 inside_width, end - start);
6597 /* Restore the foreground color of the GC if we changed it above. */
6598 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6599 XSetForeground (FRAME_X_DISPLAY (f), gc,
6600 FRAME_FOREGROUND_PIXEL (f));
6602 /* Draw the empty space below the handle. Note that we can't
6603 clear zero-height areas; that means "clear to end of window." */
6604 if ((inside_width > 0) && (end < inside_height))
6605 x_clear_area1 (FRAME_X_DISPLAY (f), w,
6606 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6607 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
6608 inside_width, inside_height - end, False);
6611 unblock_input ();
6614 #endif /* !USE_TOOLKIT_SCROLL_BARS */
6616 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
6617 nil. */
6619 static void
6620 x_scroll_bar_remove (struct scroll_bar *bar)
6622 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6623 block_input ();
6625 #ifdef USE_TOOLKIT_SCROLL_BARS
6626 #ifdef USE_GTK
6627 xg_remove_scroll_bar (f, bar->x_window);
6628 #else /* not USE_GTK */
6629 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
6630 #endif /* not USE_GTK */
6631 #else
6632 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
6633 #endif
6635 /* Dissociate this scroll bar from its window. */
6636 if (bar->horizontal)
6637 wset_horizontal_scroll_bar (XWINDOW (bar->window), Qnil);
6638 else
6639 wset_vertical_scroll_bar (XWINDOW (bar->window), Qnil);
6641 unblock_input ();
6645 /* Set the handle of the vertical scroll bar for WINDOW to indicate
6646 that we are displaying PORTION characters out of a total of WHOLE
6647 characters, starting at POSITION. If WINDOW has no scroll bar,
6648 create one. */
6650 static void
6651 XTset_vertical_scroll_bar (struct window *w, int portion, int whole, int position)
6653 struct frame *f = XFRAME (w->frame);
6654 Lisp_Object barobj;
6655 struct scroll_bar *bar;
6656 int top, height, left, width;
6657 int window_y, window_height;
6659 /* Get window dimensions. */
6660 window_box (w, ANY_AREA, 0, &window_y, 0, &window_height);
6661 top = window_y;
6662 height = window_height;
6663 left = WINDOW_SCROLL_BAR_AREA_X (w);
6664 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
6666 /* Does the scroll bar exist yet? */
6667 if (NILP (w->vertical_scroll_bar))
6669 if (width > 0 && height > 0)
6671 block_input ();
6672 x_clear_area (f, left, top, width, height);
6673 unblock_input ();
6676 bar = x_scroll_bar_create (w, top, left, width, max (height, 1), false);
6678 else
6680 /* It may just need to be moved and resized. */
6681 unsigned int mask = 0;
6683 bar = XSCROLL_BAR (w->vertical_scroll_bar);
6685 block_input ();
6687 if (left != bar->left)
6688 mask |= CWX;
6689 if (top != bar->top)
6690 mask |= CWY;
6691 if (width != bar->width)
6692 mask |= CWWidth;
6693 if (height != bar->height)
6694 mask |= CWHeight;
6696 #ifdef USE_TOOLKIT_SCROLL_BARS
6698 /* Move/size the scroll bar widget. */
6699 if (mask)
6701 /* Since toolkit scroll bars are smaller than the space reserved
6702 for them on the frame, we have to clear "under" them. */
6703 if (width > 0 && height > 0)
6704 x_clear_area (f, left, top, width, height);
6705 #ifdef USE_GTK
6706 xg_update_scrollbar_pos (f, bar->x_window, top,
6707 left, width, max (height, 1));
6708 #else /* not USE_GTK */
6709 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
6710 left, top, width, max (height, 1), 0);
6711 #endif /* not USE_GTK */
6713 #else /* not USE_TOOLKIT_SCROLL_BARS */
6715 /* Move/size the scroll bar window. */
6716 if (mask)
6718 XWindowChanges wc;
6720 wc.x = left;
6721 wc.y = top;
6722 wc.width = width;
6723 wc.height = height;
6724 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
6725 mask, &wc);
6728 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6730 /* Remember new settings. */
6731 bar->left = left;
6732 bar->top = top;
6733 bar->width = width;
6734 bar->height = height;
6736 unblock_input ();
6739 #ifdef USE_TOOLKIT_SCROLL_BARS
6740 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
6741 #else /* not USE_TOOLKIT_SCROLL_BARS */
6742 /* Set the scroll bar's current state, unless we're currently being
6743 dragged. */
6744 if (bar->dragging == -1)
6746 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
6748 if (whole == 0)
6749 x_scroll_bar_set_handle (bar, 0, top_range, false);
6750 else
6752 int start = ((double) position * top_range) / whole;
6753 int end = ((double) (position + portion) * top_range) / whole;
6754 x_scroll_bar_set_handle (bar, start, end, false);
6757 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6759 XSETVECTOR (barobj, bar);
6760 wset_vertical_scroll_bar (w, barobj);
6764 static void
6765 XTset_horizontal_scroll_bar (struct window *w, int portion, int whole, int position)
6767 struct frame *f = XFRAME (w->frame);
6768 Lisp_Object barobj;
6769 struct scroll_bar *bar;
6770 int top, height, left, width;
6771 int window_x, window_width;
6772 int pixel_width = WINDOW_PIXEL_WIDTH (w);
6774 /* Get window dimensions. */
6775 window_box (w, ANY_AREA, &window_x, 0, &window_width, 0);
6776 left = window_x;
6777 width = window_width;
6778 top = WINDOW_SCROLL_BAR_AREA_Y (w);
6779 height = WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
6781 /* Does the scroll bar exist yet? */
6782 if (NILP (w->horizontal_scroll_bar))
6784 if (width > 0 && height > 0)
6786 block_input ();
6788 /* Clear also part between window_width and
6789 WINDOW_PIXEL_WIDTH. */
6790 x_clear_area (f, left, top, pixel_width, height);
6791 unblock_input ();
6794 bar = x_scroll_bar_create (w, top, left, width, height, true);
6796 else
6798 /* It may just need to be moved and resized. */
6799 unsigned int mask = 0;
6801 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
6803 block_input ();
6805 if (left != bar->left)
6806 mask |= CWX;
6807 if (top != bar->top)
6808 mask |= CWY;
6809 if (width != bar->width)
6810 mask |= CWWidth;
6811 if (height != bar->height)
6812 mask |= CWHeight;
6814 #ifdef USE_TOOLKIT_SCROLL_BARS
6815 /* Move/size the scroll bar widget. */
6816 if (mask)
6818 /* Since toolkit scroll bars are smaller than the space reserved
6819 for them on the frame, we have to clear "under" them. */
6820 if (width > 0 && height > 0)
6821 x_clear_area (f,
6822 WINDOW_LEFT_EDGE_X (w), top,
6823 pixel_width - WINDOW_RIGHT_DIVIDER_WIDTH (w), height);
6824 #ifdef USE_GTK
6825 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top, left,
6826 width, height);
6827 #else /* not USE_GTK */
6828 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
6829 left, top, width, height, 0);
6830 #endif /* not USE_GTK */
6832 #else /* not USE_TOOLKIT_SCROLL_BARS */
6834 /* Clear areas not covered by the scroll bar because it's not as
6835 wide as the area reserved for it. This makes sure a
6836 previous mode line display is cleared after C-x 2 C-x 1, for
6837 example. */
6839 int area_height = WINDOW_CONFIG_SCROLL_BAR_HEIGHT (w);
6840 int rest = area_height - height;
6841 if (rest > 0 && width > 0)
6842 x_clear_area (f, left, top, width, rest);
6845 /* Move/size the scroll bar window. */
6846 if (mask)
6848 XWindowChanges wc;
6850 wc.x = left;
6851 wc.y = top;
6852 wc.width = width;
6853 wc.height = height;
6854 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
6855 mask, &wc);
6858 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6860 /* Remember new settings. */
6861 bar->left = left;
6862 bar->top = top;
6863 bar->width = width;
6864 bar->height = height;
6866 unblock_input ();
6869 #ifdef USE_TOOLKIT_SCROLL_BARS
6870 x_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
6871 #else /* not USE_TOOLKIT_SCROLL_BARS */
6872 /* Set the scroll bar's current state, unless we're currently being
6873 dragged. */
6874 if (bar->dragging == -1)
6876 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, width);
6878 if (whole == 0)
6879 x_scroll_bar_set_handle (bar, 0, left_range, false);
6880 else
6882 int start = ((double) position * left_range) / whole;
6883 int end = ((double) (position + portion) * left_range) / whole;
6884 x_scroll_bar_set_handle (bar, start, end, false);
6887 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6889 XSETVECTOR (barobj, bar);
6890 wset_horizontal_scroll_bar (w, barobj);
6894 /* The following three hooks are used when we're doing a thorough
6895 redisplay of the frame. We don't explicitly know which scroll bars
6896 are going to be deleted, because keeping track of when windows go
6897 away is a real pain - "Can you say set-window-configuration, boys
6898 and girls?" Instead, we just assert at the beginning of redisplay
6899 that *all* scroll bars are to be removed, and then save a scroll bar
6900 from the fiery pit when we actually redisplay its window. */
6902 /* Arrange for all scroll bars on FRAME to be removed at the next call
6903 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
6904 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
6906 static void
6907 XTcondemn_scroll_bars (struct frame *frame)
6909 if (!NILP (FRAME_SCROLL_BARS (frame)))
6911 if (!NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
6913 /* Prepend scrollbars to already condemned ones. */
6914 Lisp_Object last = FRAME_SCROLL_BARS (frame);
6916 while (!NILP (XSCROLL_BAR (last)->next))
6917 last = XSCROLL_BAR (last)->next;
6919 XSCROLL_BAR (last)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
6920 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = last;
6923 fset_condemned_scroll_bars (frame, FRAME_SCROLL_BARS (frame));
6924 fset_scroll_bars (frame, Qnil);
6929 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
6930 Note that WINDOW isn't necessarily condemned at all. */
6932 static void
6933 XTredeem_scroll_bar (struct window *w)
6935 struct scroll_bar *bar;
6936 Lisp_Object barobj;
6937 struct frame *f;
6939 /* We can't redeem this window's scroll bar if it doesn't have one. */
6940 if (NILP (w->vertical_scroll_bar) && NILP (w->horizontal_scroll_bar))
6941 emacs_abort ();
6943 if (!NILP (w->vertical_scroll_bar) && WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
6945 bar = XSCROLL_BAR (w->vertical_scroll_bar);
6946 /* Unlink it from the condemned list. */
6947 f = XFRAME (WINDOW_FRAME (w));
6948 if (NILP (bar->prev))
6950 /* If the prev pointer is nil, it must be the first in one of
6951 the lists. */
6952 if (EQ (FRAME_SCROLL_BARS (f), w->vertical_scroll_bar))
6953 /* It's not condemned. Everything's fine. */
6954 goto horizontal;
6955 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
6956 w->vertical_scroll_bar))
6957 fset_condemned_scroll_bars (f, bar->next);
6958 else
6959 /* If its prev pointer is nil, it must be at the front of
6960 one or the other! */
6961 emacs_abort ();
6963 else
6964 XSCROLL_BAR (bar->prev)->next = bar->next;
6966 if (! NILP (bar->next))
6967 XSCROLL_BAR (bar->next)->prev = bar->prev;
6969 bar->next = FRAME_SCROLL_BARS (f);
6970 bar->prev = Qnil;
6971 XSETVECTOR (barobj, bar);
6972 fset_scroll_bars (f, barobj);
6973 if (! NILP (bar->next))
6974 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6977 horizontal:
6978 if (!NILP (w->horizontal_scroll_bar) && WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
6980 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
6981 /* Unlink it from the condemned list. */
6982 f = XFRAME (WINDOW_FRAME (w));
6983 if (NILP (bar->prev))
6985 /* If the prev pointer is nil, it must be the first in one of
6986 the lists. */
6987 if (EQ (FRAME_SCROLL_BARS (f), w->horizontal_scroll_bar))
6988 /* It's not condemned. Everything's fine. */
6989 return;
6990 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
6991 w->horizontal_scroll_bar))
6992 fset_condemned_scroll_bars (f, bar->next);
6993 else
6994 /* If its prev pointer is nil, it must be at the front of
6995 one or the other! */
6996 emacs_abort ();
6998 else
6999 XSCROLL_BAR (bar->prev)->next = bar->next;
7001 if (! NILP (bar->next))
7002 XSCROLL_BAR (bar->next)->prev = bar->prev;
7004 bar->next = FRAME_SCROLL_BARS (f);
7005 bar->prev = Qnil;
7006 XSETVECTOR (barobj, bar);
7007 fset_scroll_bars (f, barobj);
7008 if (! NILP (bar->next))
7009 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
7013 /* Remove all scroll bars on FRAME that haven't been saved since the
7014 last call to `*condemn_scroll_bars_hook'. */
7016 static void
7017 XTjudge_scroll_bars (struct frame *f)
7019 Lisp_Object bar, next;
7021 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
7023 /* Clear out the condemned list now so we won't try to process any
7024 more events on the hapless scroll bars. */
7025 fset_condemned_scroll_bars (f, Qnil);
7027 for (; ! NILP (bar); bar = next)
7029 struct scroll_bar *b = XSCROLL_BAR (bar);
7031 x_scroll_bar_remove (b);
7033 next = b->next;
7034 b->next = b->prev = Qnil;
7037 /* Now there should be no references to the condemned scroll bars,
7038 and they should get garbage-collected. */
7042 #ifndef USE_TOOLKIT_SCROLL_BARS
7043 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
7044 is a no-op when using toolkit scroll bars.
7046 This may be called from a signal handler, so we have to ignore GC
7047 mark bits. */
7049 static void
7050 x_scroll_bar_expose (struct scroll_bar *bar, const XEvent *event)
7052 Window w = bar->x_window;
7053 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7054 GC gc = f->output_data.x->normal_gc;
7056 block_input ();
7058 x_scroll_bar_set_handle (bar, bar->start, bar->end, true);
7060 /* Switch to scroll bar foreground color. */
7061 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
7062 XSetForeground (FRAME_X_DISPLAY (f), gc,
7063 f->output_data.x->scroll_bar_foreground_pixel);
7065 /* Draw a one-pixel border just inside the edges of the scroll bar. */
7066 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
7067 /* x, y, width, height */
7068 0, 0, bar->width - 1, bar->height - 1);
7070 /* Restore the foreground color of the GC if we changed it above. */
7071 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
7072 XSetForeground (FRAME_X_DISPLAY (f), gc,
7073 FRAME_FOREGROUND_PIXEL (f));
7075 unblock_input ();
7078 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7080 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
7081 is set to something other than NO_EVENT, it is enqueued.
7083 This may be called from a signal handler, so we have to ignore GC
7084 mark bits. */
7087 static void
7088 x_scroll_bar_handle_click (struct scroll_bar *bar,
7089 const XEvent *event,
7090 struct input_event *emacs_event)
7092 if (! WINDOWP (bar->window))
7093 emacs_abort ();
7095 emacs_event->kind = (bar->horizontal
7096 ? HORIZONTAL_SCROLL_BAR_CLICK_EVENT
7097 : SCROLL_BAR_CLICK_EVENT);
7098 emacs_event->code = event->xbutton.button - Button1;
7099 emacs_event->modifiers
7100 = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO
7101 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
7102 event->xbutton.state)
7103 | (event->type == ButtonRelease
7104 ? up_modifier
7105 : down_modifier));
7106 emacs_event->frame_or_window = bar->window;
7107 emacs_event->arg = Qnil;
7108 emacs_event->timestamp = event->xbutton.time;
7109 if (bar->horizontal)
7111 int left_range
7112 = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
7113 int x = event->xbutton.x - HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
7115 if (x < 0) x = 0;
7116 if (x > left_range) x = left_range;
7118 if (x < bar->start)
7119 emacs_event->part = scroll_bar_before_handle;
7120 else if (x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
7121 emacs_event->part = scroll_bar_horizontal_handle;
7122 else
7123 emacs_event->part = scroll_bar_after_handle;
7125 #ifndef USE_TOOLKIT_SCROLL_BARS
7126 /* If the user has released the handle, set it to its final position. */
7127 if (event->type == ButtonRelease && bar->dragging != -1)
7129 int new_start = - bar->dragging;
7130 int new_end = new_start + bar->end - bar->start;
7132 x_scroll_bar_set_handle (bar, new_start, new_end, false);
7133 bar->dragging = -1;
7135 #endif
7137 XSETINT (emacs_event->x, left_range);
7138 XSETINT (emacs_event->y, x);
7140 else
7142 int top_range
7143 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
7144 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
7146 if (y < 0) y = 0;
7147 if (y > top_range) y = top_range;
7149 if (y < bar->start)
7150 emacs_event->part = scroll_bar_above_handle;
7151 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
7152 emacs_event->part = scroll_bar_handle;
7153 else
7154 emacs_event->part = scroll_bar_below_handle;
7156 #ifndef USE_TOOLKIT_SCROLL_BARS
7157 /* If the user has released the handle, set it to its final position. */
7158 if (event->type == ButtonRelease && bar->dragging != -1)
7160 int new_start = y - bar->dragging;
7161 int new_end = new_start + bar->end - bar->start;
7163 x_scroll_bar_set_handle (bar, new_start, new_end, false);
7164 bar->dragging = -1;
7166 #endif
7168 XSETINT (emacs_event->x, y);
7169 XSETINT (emacs_event->y, top_range);
7173 #ifndef USE_TOOLKIT_SCROLL_BARS
7175 /* Handle some mouse motion while someone is dragging the scroll bar.
7177 This may be called from a signal handler, so we have to ignore GC
7178 mark bits. */
7180 static void
7181 x_scroll_bar_note_movement (struct scroll_bar *bar,
7182 const XMotionEvent *event)
7184 struct frame *f = XFRAME (XWINDOW (bar->window)->frame);
7185 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
7187 dpyinfo->last_mouse_movement_time = event->time;
7188 dpyinfo->last_mouse_scroll_bar = bar;
7189 f->mouse_moved = true;
7191 /* If we're dragging the bar, display it. */
7192 if (bar->dragging != -1)
7194 /* Where should the handle be now? */
7195 int new_start = event->y - bar->dragging;
7197 if (new_start != bar->start)
7199 int new_end = new_start + bar->end - bar->start;
7201 x_scroll_bar_set_handle (bar, new_start, new_end, false);
7206 #endif /* !USE_TOOLKIT_SCROLL_BARS */
7208 /* Return information to the user about the current position of the mouse
7209 on the scroll bar. */
7211 static void
7212 x_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
7213 enum scroll_bar_part *part, Lisp_Object *x,
7214 Lisp_Object *y, Time *timestamp)
7216 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
7217 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
7218 Window w = bar->x_window;
7219 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7220 int win_x, win_y;
7221 Window dummy_window;
7222 int dummy_coord;
7223 unsigned int dummy_mask;
7225 block_input ();
7227 /* Get the mouse's position relative to the scroll bar window, and
7228 report that. */
7229 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
7231 /* Root, child, root x and root y. */
7232 &dummy_window, &dummy_window,
7233 &dummy_coord, &dummy_coord,
7235 /* Position relative to scroll bar. */
7236 &win_x, &win_y,
7238 /* Mouse buttons and modifier keys. */
7239 &dummy_mask))
7241 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
7243 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
7245 if (bar->dragging != -1)
7246 win_y -= bar->dragging;
7248 if (win_y < 0)
7249 win_y = 0;
7250 if (win_y > top_range)
7251 win_y = top_range;
7253 *fp = f;
7254 *bar_window = bar->window;
7256 if (bar->dragging != -1)
7257 *part = scroll_bar_handle;
7258 else if (win_y < bar->start)
7259 *part = scroll_bar_above_handle;
7260 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
7261 *part = scroll_bar_handle;
7262 else
7263 *part = scroll_bar_below_handle;
7265 XSETINT (*x, win_y);
7266 XSETINT (*y, top_range);
7268 f->mouse_moved = false;
7269 dpyinfo->last_mouse_scroll_bar = NULL;
7270 *timestamp = dpyinfo->last_mouse_movement_time;
7273 unblock_input ();
7277 /* Return information to the user about the current position of the mouse
7278 on the scroll bar. */
7280 static void
7281 x_horizontal_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
7282 enum scroll_bar_part *part, Lisp_Object *x,
7283 Lisp_Object *y, Time *timestamp)
7285 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
7286 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
7287 Window w = bar->x_window;
7288 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7289 int win_x, win_y;
7290 Window dummy_window;
7291 int dummy_coord;
7292 unsigned int dummy_mask;
7294 block_input ();
7296 /* Get the mouse's position relative to the scroll bar window, and
7297 report that. */
7298 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
7300 /* Root, child, root x and root y. */
7301 &dummy_window, &dummy_window,
7302 &dummy_coord, &dummy_coord,
7304 /* Position relative to scroll bar. */
7305 &win_x, &win_y,
7307 /* Mouse buttons and modifier keys. */
7308 &dummy_mask))
7310 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
7312 win_x -= HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
7314 if (bar->dragging != -1)
7315 win_x -= bar->dragging;
7317 if (win_x < 0)
7318 win_x = 0;
7319 if (win_x > left_range)
7320 win_x = left_range;
7322 *fp = f;
7323 *bar_window = bar->window;
7325 if (bar->dragging != -1)
7326 *part = scroll_bar_horizontal_handle;
7327 else if (win_x < bar->start)
7328 *part = scroll_bar_before_handle;
7329 else if (win_x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
7330 *part = scroll_bar_handle;
7331 else
7332 *part = scroll_bar_after_handle;
7334 XSETINT (*y, win_x);
7335 XSETINT (*x, left_range);
7337 f->mouse_moved = false;
7338 dpyinfo->last_mouse_scroll_bar = NULL;
7339 *timestamp = dpyinfo->last_mouse_movement_time;
7342 unblock_input ();
7346 /* The screen has been cleared so we may have changed foreground or
7347 background colors, and the scroll bars may need to be redrawn.
7348 Clear out the scroll bars, and ask for expose events, so we can
7349 redraw them. */
7351 static void
7352 x_scroll_bar_clear (struct frame *f)
7354 #ifndef USE_TOOLKIT_SCROLL_BARS
7355 Lisp_Object bar;
7357 /* We can have scroll bars even if this is 0,
7358 if we just turned off scroll bar mode.
7359 But in that case we should not clear them. */
7360 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
7361 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
7362 bar = XSCROLL_BAR (bar)->next)
7363 XClearArea (FRAME_X_DISPLAY (f),
7364 XSCROLL_BAR (bar)->x_window,
7365 0, 0, 0, 0, True);
7366 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7369 #ifdef ENABLE_CHECKING
7371 /* Record the last 100 characters stored
7372 to help debug the loss-of-chars-during-GC problem. */
7374 static int temp_index;
7375 static short temp_buffer[100];
7377 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
7378 if (temp_index == ARRAYELTS (temp_buffer)) \
7379 temp_index = 0; \
7380 temp_buffer[temp_index++] = (keysym)
7382 #else /* not ENABLE_CHECKING */
7384 #define STORE_KEYSYM_FOR_DEBUG(keysym) ((void)0)
7386 #endif /* ENABLE_CHECKING */
7388 /* Set this to nonzero to fake an "X I/O error"
7389 on a particular display. */
7391 static struct x_display_info *XTread_socket_fake_io_error;
7393 /* When we find no input here, we occasionally do a no-op command
7394 to verify that the X server is still running and we can still talk with it.
7395 We try all the open displays, one by one.
7396 This variable is used for cycling thru the displays. */
7398 static struct x_display_info *next_noop_dpyinfo;
7400 enum
7402 X_EVENT_NORMAL,
7403 X_EVENT_GOTO_OUT,
7404 X_EVENT_DROP
7407 /* Filter events for the current X input method.
7408 DPYINFO is the display this event is for.
7409 EVENT is the X event to filter.
7411 Returns non-zero if the event was filtered, caller shall not process
7412 this event further.
7413 Returns zero if event is wasn't filtered. */
7415 #ifdef HAVE_X_I18N
7416 static int
7417 x_filter_event (struct x_display_info *dpyinfo, XEvent *event)
7419 /* XFilterEvent returns non-zero if the input method has
7420 consumed the event. We pass the frame's X window to
7421 XFilterEvent because that's the one for which the IC
7422 was created. */
7424 struct frame *f1 = x_any_window_to_frame (dpyinfo,
7425 event->xclient.window);
7427 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
7429 #endif
7431 #ifdef USE_GTK
7432 static int current_count;
7433 static int current_finish;
7434 static struct input_event *current_hold_quit;
7436 /* This is the filter function invoked by the GTK event loop.
7437 It is invoked before the XEvent is translated to a GdkEvent,
7438 so we have a chance to act on the event before GTK. */
7439 static GdkFilterReturn
7440 event_handler_gdk (GdkXEvent *gxev, GdkEvent *ev, gpointer data)
7442 XEvent *xev = (XEvent *) gxev;
7444 block_input ();
7445 if (current_count >= 0)
7447 struct x_display_info *dpyinfo;
7449 dpyinfo = x_display_info_for_display (xev->xany.display);
7451 #ifdef HAVE_X_I18N
7452 /* Filter events for the current X input method.
7453 GTK calls XFilterEvent but not for key press and release,
7454 so we do it here. */
7455 if ((xev->type == KeyPress || xev->type == KeyRelease)
7456 && dpyinfo
7457 && x_filter_event (dpyinfo, xev))
7459 unblock_input ();
7460 return GDK_FILTER_REMOVE;
7462 #endif
7464 if (! dpyinfo)
7465 current_finish = X_EVENT_NORMAL;
7466 else
7467 current_count
7468 += handle_one_xevent (dpyinfo, xev, &current_finish,
7469 current_hold_quit);
7471 else
7472 current_finish = x_dispatch_event (xev, xev->xany.display);
7474 unblock_input ();
7476 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
7477 return GDK_FILTER_REMOVE;
7479 return GDK_FILTER_CONTINUE;
7481 #endif /* USE_GTK */
7484 static void xembed_send_message (struct frame *f, Time,
7485 enum xembed_message,
7486 long detail, long data1, long data2);
7488 static void
7489 x_net_wm_state (struct frame *f, Window window)
7491 int value = FULLSCREEN_NONE;
7492 Lisp_Object lval = Qnil;
7493 bool sticky = false;
7495 get_current_wm_state (f, window, &value, &sticky);
7497 switch (value)
7499 case FULLSCREEN_WIDTH:
7500 lval = Qfullwidth;
7501 break;
7502 case FULLSCREEN_HEIGHT:
7503 lval = Qfullheight;
7504 break;
7505 case FULLSCREEN_BOTH:
7506 lval = Qfullboth;
7507 break;
7508 case FULLSCREEN_MAXIMIZED:
7509 lval = Qmaximized;
7510 break;
7513 frame_size_history_add
7514 (f, Qx_net_wm_state, 0, 0,
7515 list2 (get_frame_param (f, Qfullscreen), lval));
7517 store_frame_param (f, Qfullscreen, lval);
7518 /** store_frame_param (f, Qsticky, sticky ? Qt : Qnil); **/
7521 /* Flip back buffers on any frames with undrawn content. */
7522 static void
7523 flush_dirty_back_buffers (void)
7525 block_input ();
7526 Lisp_Object tail, frame;
7527 FOR_EACH_FRAME (tail, frame)
7529 struct frame *f = XFRAME (frame);
7530 if (FRAME_LIVE_P (f) &&
7531 FRAME_X_P (f) &&
7532 FRAME_X_WINDOW (f) &&
7533 !FRAME_GARBAGED_P (f) &&
7534 !buffer_flipping_blocked_p () &&
7535 FRAME_X_NEED_BUFFER_FLIP (f))
7536 show_back_buffer (f);
7538 unblock_input ();
7541 /* Handles the XEvent EVENT on display DPYINFO.
7543 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
7544 *FINISH is zero if caller should continue reading events.
7545 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
7546 *EVENT is unchanged unless we're processing KeyPress event.
7548 We return the number of characters stored into the buffer. */
7550 static int
7551 handle_one_xevent (struct x_display_info *dpyinfo,
7552 const XEvent *event,
7553 int *finish, struct input_event *hold_quit)
7555 union buffered_input_event inev;
7556 int count = 0;
7557 int do_help = 0;
7558 ptrdiff_t nbytes = 0;
7559 struct frame *any, *f = NULL;
7560 struct coding_system coding;
7561 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
7562 /* This holds the state XLookupString needs to implement dead keys
7563 and other tricks known as "compose processing". _X Window System_
7564 says that a portable program can't use this, but Stephen Gildea assures
7565 me that letting the compiler initialize it to zeros will work okay. */
7566 static XComposeStatus compose_status;
7567 XEvent configureEvent;
7568 XEvent next_event;
7570 USE_SAFE_ALLOCA;
7572 *finish = X_EVENT_NORMAL;
7574 EVENT_INIT (inev.ie);
7575 inev.ie.kind = NO_EVENT;
7576 inev.ie.arg = Qnil;
7578 any = x_any_window_to_frame (dpyinfo, event->xany.window);
7580 if (any && any->wait_event_type == event->type)
7581 any->wait_event_type = 0; /* Indicates we got it. */
7583 switch (event->type)
7585 case ClientMessage:
7587 if (event->xclient.message_type == dpyinfo->Xatom_wm_protocols
7588 && event->xclient.format == 32)
7590 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_take_focus)
7592 /* Use the value returned by x_any_window_to_frame
7593 because this could be the shell widget window
7594 if the frame has no title bar. */
7595 f = any;
7596 #ifdef HAVE_X_I18N
7597 /* Not quite sure this is needed -pd */
7598 if (f && FRAME_XIC (f))
7599 XSetICFocus (FRAME_XIC (f));
7600 #endif
7601 #if false
7602 /* Emacs sets WM hints whose `input' field is `true'. This
7603 instructs the WM to set the input focus automatically for
7604 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
7605 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
7606 it has set the focus. So, XSetInputFocus below is not
7607 needed.
7609 The call to XSetInputFocus below has also caused trouble. In
7610 cases where the XSetInputFocus done by the WM and the one
7611 below are temporally close (on a fast machine), the call
7612 below can generate additional FocusIn events which confuse
7613 Emacs. */
7615 /* Since we set WM_TAKE_FOCUS, we must call
7616 XSetInputFocus explicitly. But not if f is null,
7617 since that might be an event for a deleted frame. */
7618 if (f)
7620 Display *d = event->xclient.display;
7621 /* Catch and ignore errors, in case window has been
7622 iconified by a window manager such as GWM. */
7623 x_catch_errors (d);
7624 XSetInputFocus (d, event->xclient.window,
7625 /* The ICCCM says this is
7626 the only valid choice. */
7627 RevertToParent,
7628 event->xclient.data.l[1]);
7629 x_uncatch_errors ();
7631 /* Not certain about handling scroll bars here */
7632 #endif
7633 goto done;
7636 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_save_yourself)
7638 /* Save state modify the WM_COMMAND property to
7639 something which can reinstate us. This notifies
7640 the session manager, who's looking for such a
7641 PropertyNotify. Can restart processing when
7642 a keyboard or mouse event arrives. */
7643 /* If we have a session manager, don't set this.
7644 KDE will then start two Emacsen, one for the
7645 session manager and one for this. */
7646 #ifdef HAVE_X_SM
7647 if (! x_session_have_connection ())
7648 #endif
7650 f = x_top_window_to_frame (dpyinfo,
7651 event->xclient.window);
7652 /* This is just so we only give real data once
7653 for a single Emacs process. */
7654 if (f == SELECTED_FRAME ())
7655 XSetCommand (FRAME_X_DISPLAY (f),
7656 event->xclient.window,
7657 initial_argv, initial_argc);
7658 else if (f)
7659 XSetCommand (FRAME_X_DISPLAY (f),
7660 event->xclient.window,
7661 0, 0);
7663 goto done;
7666 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_delete_window)
7668 f = any;
7669 if (!f)
7670 goto OTHER; /* May be a dialog that is to be removed */
7672 inev.ie.kind = DELETE_WINDOW_EVENT;
7673 XSETFRAME (inev.ie.frame_or_window, f);
7674 goto done;
7677 goto done;
7680 if (event->xclient.message_type == dpyinfo->Xatom_wm_configure_denied)
7681 goto done;
7683 if (event->xclient.message_type == dpyinfo->Xatom_wm_window_moved)
7685 int new_x, new_y;
7686 f = x_window_to_frame (dpyinfo, event->xclient.window);
7688 new_x = event->xclient.data.s[0];
7689 new_y = event->xclient.data.s[1];
7691 if (f)
7693 f->left_pos = new_x;
7694 f->top_pos = new_y;
7696 goto done;
7699 #ifdef X_TOOLKIT_EDITRES
7700 if (event->xclient.message_type == dpyinfo->Xatom_editres)
7702 f = any;
7703 if (f)
7704 _XEditResCheckMessages (f->output_data.x->widget,
7705 NULL, (XEvent *) event, NULL);
7706 goto done;
7708 #endif /* X_TOOLKIT_EDITRES */
7710 if (event->xclient.message_type == dpyinfo->Xatom_DONE
7711 || event->xclient.message_type == dpyinfo->Xatom_PAGE)
7713 /* Ghostview job completed. Kill it. We could
7714 reply with "Next" if we received "Page", but we
7715 currently never do because we are interested in
7716 images, only, which should have 1 page. */
7717 Pixmap pixmap = (Pixmap) event->xclient.data.l[1];
7718 f = x_window_to_frame (dpyinfo, event->xclient.window);
7719 if (!f)
7720 goto OTHER;
7721 x_kill_gs_process (pixmap, f);
7722 expose_frame (f, 0, 0, 0, 0);
7723 goto done;
7726 #ifdef USE_TOOLKIT_SCROLL_BARS
7727 /* Scroll bar callbacks send a ClientMessage from which
7728 we construct an input_event. */
7729 if (event->xclient.message_type == dpyinfo->Xatom_Scrollbar)
7731 x_scroll_bar_to_input_event (event, &inev.ie);
7732 *finish = X_EVENT_GOTO_OUT;
7733 goto done;
7735 else if (event->xclient.message_type == dpyinfo->Xatom_Horizontal_Scrollbar)
7737 x_horizontal_scroll_bar_to_input_event (event, &inev.ie);
7738 *finish = X_EVENT_GOTO_OUT;
7739 goto done;
7741 #endif /* USE_TOOLKIT_SCROLL_BARS */
7743 /* XEmbed messages from the embedder (if any). */
7744 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
7746 enum xembed_message msg = event->xclient.data.l[1];
7747 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
7748 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7750 *finish = X_EVENT_GOTO_OUT;
7751 goto done;
7754 xft_settings_event (dpyinfo, event);
7756 f = any;
7757 if (!f)
7758 goto OTHER;
7759 if (x_handle_dnd_message (f, &event->xclient, dpyinfo, &inev.ie))
7760 *finish = X_EVENT_DROP;
7762 break;
7764 case SelectionNotify:
7765 x_display_set_last_user_time (dpyinfo, event->xselection.time);
7766 #ifdef USE_X_TOOLKIT
7767 if (! x_window_to_frame (dpyinfo, event->xselection.requestor))
7768 goto OTHER;
7769 #endif /* not USE_X_TOOLKIT */
7770 x_handle_selection_notify (&event->xselection);
7771 break;
7773 case SelectionClear: /* Someone has grabbed ownership. */
7774 x_display_set_last_user_time (dpyinfo, event->xselectionclear.time);
7775 #ifdef USE_X_TOOLKIT
7776 if (! x_window_to_frame (dpyinfo, event->xselectionclear.window))
7777 goto OTHER;
7778 #endif /* USE_X_TOOLKIT */
7780 const XSelectionClearEvent *eventp = &event->xselectionclear;
7782 inev.sie.kind = SELECTION_CLEAR_EVENT;
7783 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
7784 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
7785 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
7787 break;
7789 case SelectionRequest: /* Someone wants our selection. */
7790 x_display_set_last_user_time (dpyinfo, event->xselectionrequest.time);
7791 #ifdef USE_X_TOOLKIT
7792 if (!x_window_to_frame (dpyinfo, event->xselectionrequest.owner))
7793 goto OTHER;
7794 #endif /* USE_X_TOOLKIT */
7796 const XSelectionRequestEvent *eventp = &event->xselectionrequest;
7798 inev.sie.kind = SELECTION_REQUEST_EVENT;
7799 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
7800 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
7801 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
7802 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
7803 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
7804 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
7806 break;
7808 case PropertyNotify:
7809 x_display_set_last_user_time (dpyinfo, event->xproperty.time);
7810 f = x_top_window_to_frame (dpyinfo, event->xproperty.window);
7811 if (f && event->xproperty.atom == dpyinfo->Xatom_net_wm_state)
7813 bool not_hidden = x_handle_net_wm_state (f, &event->xproperty);
7814 if (not_hidden && FRAME_ICONIFIED_P (f))
7816 /* Gnome shell does not iconify us when C-z is pressed.
7817 It hides the frame. So if our state says we aren't
7818 hidden anymore, treat it as deiconified. */
7819 SET_FRAME_VISIBLE (f, 1);
7820 SET_FRAME_ICONIFIED (f, false);
7821 f->output_data.x->has_been_visible = true;
7822 inev.ie.kind = DEICONIFY_EVENT;
7823 XSETFRAME (inev.ie.frame_or_window, f);
7825 else if (! not_hidden && ! FRAME_ICONIFIED_P (f))
7827 SET_FRAME_VISIBLE (f, 0);
7828 SET_FRAME_ICONIFIED (f, true);
7829 inev.ie.kind = ICONIFY_EVENT;
7830 XSETFRAME (inev.ie.frame_or_window, f);
7834 x_handle_property_notify (&event->xproperty);
7835 xft_settings_event (dpyinfo, event);
7836 goto OTHER;
7838 case ReparentNotify:
7839 f = x_top_window_to_frame (dpyinfo, event->xreparent.window);
7840 if (f)
7842 f->output_data.x->parent_desc = event->xreparent.parent;
7843 x_real_positions (f, &f->left_pos, &f->top_pos);
7845 /* Perhaps reparented due to a WM restart. Reset this. */
7846 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
7847 FRAME_DISPLAY_INFO (f)->net_supported_window = 0;
7849 x_set_frame_alpha (f);
7851 goto OTHER;
7853 case Expose:
7854 f = x_window_to_frame (dpyinfo, event->xexpose.window);
7855 if (f)
7857 if (!FRAME_VISIBLE_P (f))
7859 block_input ();
7860 SET_FRAME_VISIBLE (f, 1);
7861 SET_FRAME_ICONIFIED (f, false);
7862 if (FRAME_X_DOUBLE_BUFFERED_P (f))
7863 font_drop_xrender_surfaces (f);
7864 f->output_data.x->has_been_visible = true;
7865 SET_FRAME_GARBAGED (f);
7866 unblock_input ();
7868 else if (FRAME_GARBAGED_P (f))
7870 #ifdef USE_GTK
7871 /* Go around the back buffer and manually clear the
7872 window the first time we show it. This way, we avoid
7873 showing users the sanity-defying horror of whatever
7874 GtkWindow is rendering beneath us. We've garbaged
7875 the frame, so we'll redraw the whole thing on next
7876 redisplay anyway. Yuck. */
7877 x_clear_area1 (
7878 FRAME_X_DISPLAY (f),
7879 FRAME_X_WINDOW (f),
7880 event->xexpose.x, event->xexpose.y,
7881 event->xexpose.width, event->xexpose.height,
7883 #endif
7887 if (!FRAME_GARBAGED_P (f))
7889 #ifdef USE_GTK
7890 /* This seems to be needed for GTK 2.6 and later, see
7891 http://debbugs.gnu.org/cgi/bugreport.cgi?bug=15398. */
7892 x_clear_area (f,
7893 event->xexpose.x, event->xexpose.y,
7894 event->xexpose.width, event->xexpose.height);
7895 #endif
7896 expose_frame (f, event->xexpose.x, event->xexpose.y,
7897 event->xexpose.width, event->xexpose.height);
7900 if (!FRAME_GARBAGED_P (f))
7901 show_back_buffer (f);
7903 else
7905 #ifndef USE_TOOLKIT_SCROLL_BARS
7906 struct scroll_bar *bar;
7907 #endif
7908 #if defined USE_LUCID
7909 /* Submenus of the Lucid menu bar aren't widgets
7910 themselves, so there's no way to dispatch events
7911 to them. Recognize this case separately. */
7913 Widget widget = x_window_to_menu_bar (event->xexpose.window);
7914 if (widget)
7915 xlwmenu_redisplay (widget);
7917 #endif /* USE_LUCID */
7919 #ifdef USE_TOOLKIT_SCROLL_BARS
7920 /* Dispatch event to the widget. */
7921 goto OTHER;
7922 #else /* not USE_TOOLKIT_SCROLL_BARS */
7923 bar = x_window_to_scroll_bar (event->xexpose.display,
7924 event->xexpose.window, 2);
7926 if (bar)
7927 x_scroll_bar_expose (bar, event);
7928 #ifdef USE_X_TOOLKIT
7929 else
7930 goto OTHER;
7931 #endif /* USE_X_TOOLKIT */
7932 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7934 break;
7936 case GraphicsExpose: /* This occurs when an XCopyArea's
7937 source area was obscured or not
7938 available. */
7939 f = x_window_to_frame (dpyinfo, event->xgraphicsexpose.drawable);
7940 if (f)
7942 expose_frame (f, event->xgraphicsexpose.x,
7943 event->xgraphicsexpose.y,
7944 event->xgraphicsexpose.width,
7945 event->xgraphicsexpose.height);
7946 show_back_buffer (f);
7948 #ifdef USE_X_TOOLKIT
7949 else
7950 goto OTHER;
7951 #endif /* USE_X_TOOLKIT */
7952 break;
7954 case NoExpose: /* This occurs when an XCopyArea's
7955 source area was completely
7956 available. */
7957 break;
7959 case UnmapNotify:
7960 /* Redo the mouse-highlight after the tooltip has gone. */
7961 if (event->xunmap.window == tip_window)
7963 tip_window = 0;
7964 x_redo_mouse_highlight (dpyinfo);
7967 f = x_top_window_to_frame (dpyinfo, event->xunmap.window);
7968 if (f) /* F may no longer exist if
7969 the frame was deleted. */
7971 bool visible = FRAME_VISIBLE_P (f);
7972 /* While a frame is unmapped, display generation is
7973 disabled; you don't want to spend time updating a
7974 display that won't ever be seen. */
7975 SET_FRAME_VISIBLE (f, 0);
7976 /* We can't distinguish, from the event, whether the window
7977 has become iconified or invisible. So assume, if it
7978 was previously visible, than now it is iconified.
7979 But x_make_frame_invisible clears both
7980 the visible flag and the iconified flag;
7981 and that way, we know the window is not iconified now. */
7982 if (visible || FRAME_ICONIFIED_P (f))
7984 SET_FRAME_ICONIFIED (f, true);
7985 inev.ie.kind = ICONIFY_EVENT;
7986 XSETFRAME (inev.ie.frame_or_window, f);
7989 goto OTHER;
7991 case MapNotify:
7992 /* We use x_top_window_to_frame because map events can
7993 come for sub-windows and they don't mean that the
7994 frame is visible. */
7995 f = x_top_window_to_frame (dpyinfo, event->xmap.window);
7996 if (f)
7998 bool iconified = FRAME_ICONIFIED_P (f);
8000 /* Check if fullscreen was specified before we where mapped the
8001 first time, i.e. from the command line. */
8002 if (!f->output_data.x->has_been_visible)
8003 x_check_fullscreen (f);
8005 SET_FRAME_VISIBLE (f, 1);
8006 SET_FRAME_ICONIFIED (f, false);
8007 f->output_data.x->has_been_visible = true;
8009 if (iconified)
8011 inev.ie.kind = DEICONIFY_EVENT;
8012 XSETFRAME (inev.ie.frame_or_window, f);
8014 else if (! NILP (Vframe_list) && ! NILP (XCDR (Vframe_list)))
8015 /* Force a redisplay sooner or later to update the
8016 frame titles in case this is the second frame. */
8017 record_asynch_buffer_change ();
8019 goto OTHER;
8021 case KeyPress:
8023 x_display_set_last_user_time (dpyinfo, event->xkey.time);
8024 ignore_next_mouse_click_timeout = 0;
8026 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8027 /* Dispatch KeyPress events when in menu. */
8028 if (popup_activated ())
8029 goto OTHER;
8030 #endif
8032 f = any;
8034 /* If mouse-highlight is an integer, input clears out
8035 mouse highlighting. */
8036 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
8037 #if ! defined (USE_GTK)
8038 && (f == 0
8039 || !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
8040 #endif
8043 clear_mouse_face (hlinfo);
8044 hlinfo->mouse_face_hidden = true;
8047 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
8048 if (f == 0)
8050 /* Scroll bars consume key events, but we want
8051 the keys to go to the scroll bar's frame. */
8052 Widget widget = XtWindowToWidget (dpyinfo->display,
8053 event->xkey.window);
8054 if (widget && XmIsScrollBar (widget))
8056 widget = XtParent (widget);
8057 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
8060 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
8062 if (f != 0)
8064 KeySym keysym, orig_keysym;
8065 /* al%imercury@uunet.uu.net says that making this 81
8066 instead of 80 fixed a bug whereby meta chars made
8067 his Emacs hang.
8069 It seems that some version of XmbLookupString has
8070 a bug of not returning XBufferOverflow in
8071 status_return even if the input is too long to
8072 fit in 81 bytes. So, we must prepare sufficient
8073 bytes for copy_buffer. 513 bytes (256 chars for
8074 two-byte character set) seems to be a fairly good
8075 approximation. -- 2000.8.10 handa@etl.go.jp */
8076 unsigned char copy_buffer[513];
8077 unsigned char *copy_bufptr = copy_buffer;
8078 int copy_bufsiz = sizeof (copy_buffer);
8079 int modifiers;
8080 Lisp_Object coding_system = Qlatin_1;
8081 Lisp_Object c;
8082 /* Event will be modified. */
8083 XKeyEvent xkey = event->xkey;
8085 #ifdef USE_GTK
8086 /* Don't pass keys to GTK. A Tab will shift focus to the
8087 tool bar in GTK 2.4. Keys will still go to menus and
8088 dialogs because in that case popup_activated is nonzero
8089 (see above). */
8090 *finish = X_EVENT_DROP;
8091 #endif
8093 xkey.state |= x_emacs_to_x_modifiers (FRAME_DISPLAY_INFO (f),
8094 extra_keyboard_modifiers);
8095 modifiers = xkey.state;
8097 /* This will have to go some day... */
8099 /* make_lispy_event turns chars into control chars.
8100 Don't do it here because XLookupString is too eager. */
8101 xkey.state &= ~ControlMask;
8102 xkey.state &= ~(dpyinfo->meta_mod_mask
8103 | dpyinfo->super_mod_mask
8104 | dpyinfo->hyper_mod_mask
8105 | dpyinfo->alt_mod_mask);
8107 /* In case Meta is ComposeCharacter,
8108 clear its status. According to Markus Ehrnsperger
8109 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
8110 this enables ComposeCharacter to work whether or
8111 not it is combined with Meta. */
8112 if (modifiers & dpyinfo->meta_mod_mask)
8113 memset (&compose_status, 0, sizeof (compose_status));
8115 #ifdef HAVE_X_I18N
8116 if (FRAME_XIC (f))
8118 Status status_return;
8120 coding_system = Vlocale_coding_system;
8121 nbytes = XmbLookupString (FRAME_XIC (f),
8122 &xkey, (char *) copy_bufptr,
8123 copy_bufsiz, &keysym,
8124 &status_return);
8125 if (status_return == XBufferOverflow)
8127 copy_bufsiz = nbytes + 1;
8128 copy_bufptr = alloca (copy_bufsiz);
8129 nbytes = XmbLookupString (FRAME_XIC (f),
8130 &xkey, (char *) copy_bufptr,
8131 copy_bufsiz, &keysym,
8132 &status_return);
8134 /* Xutf8LookupString is a new but already deprecated interface. -stef */
8135 if (status_return == XLookupNone)
8136 break;
8137 else if (status_return == XLookupChars)
8139 keysym = NoSymbol;
8140 modifiers = 0;
8142 else if (status_return != XLookupKeySym
8143 && status_return != XLookupBoth)
8144 emacs_abort ();
8146 else
8147 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
8148 copy_bufsiz, &keysym,
8149 &compose_status);
8150 #else
8151 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
8152 copy_bufsiz, &keysym,
8153 &compose_status);
8154 #endif
8156 /* If not using XIM/XIC, and a compose sequence is in progress,
8157 we break here. Otherwise, chars_matched is always 0. */
8158 if (compose_status.chars_matched > 0 && nbytes == 0)
8159 break;
8161 memset (&compose_status, 0, sizeof (compose_status));
8162 orig_keysym = keysym;
8164 /* Common for all keysym input events. */
8165 XSETFRAME (inev.ie.frame_or_window, f);
8166 inev.ie.modifiers
8167 = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), modifiers);
8168 inev.ie.timestamp = xkey.time;
8170 /* First deal with keysyms which have defined
8171 translations to characters. */
8172 if (keysym >= 32 && keysym < 128)
8173 /* Avoid explicitly decoding each ASCII character. */
8175 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
8176 inev.ie.code = keysym;
8177 goto done_keysym;
8180 /* Keysyms directly mapped to Unicode characters. */
8181 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
8183 if (keysym < 0x01000080)
8184 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
8185 else
8186 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
8187 inev.ie.code = keysym & 0xFFFFFF;
8188 goto done_keysym;
8191 /* Now non-ASCII. */
8192 if (HASH_TABLE_P (Vx_keysym_table)
8193 && (c = Fgethash (make_number (keysym),
8194 Vx_keysym_table,
8195 Qnil),
8196 NATNUMP (c)))
8198 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
8199 ? ASCII_KEYSTROKE_EVENT
8200 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
8201 inev.ie.code = XFASTINT (c);
8202 goto done_keysym;
8205 /* Random non-modifier sorts of keysyms. */
8206 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
8207 || keysym == XK_Delete
8208 #ifdef XK_ISO_Left_Tab
8209 || (keysym >= XK_ISO_Left_Tab
8210 && keysym <= XK_ISO_Enter)
8211 #endif
8212 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
8213 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
8214 #ifdef HPUX
8215 /* This recognizes the "extended function
8216 keys". It seems there's no cleaner way.
8217 Test IsModifierKey to avoid handling
8218 mode_switch incorrectly. */
8219 || (XK_Select <= keysym && keysym < XK_KP_Space)
8220 #endif
8221 #ifdef XK_dead_circumflex
8222 || orig_keysym == XK_dead_circumflex
8223 #endif
8224 #ifdef XK_dead_grave
8225 || orig_keysym == XK_dead_grave
8226 #endif
8227 #ifdef XK_dead_tilde
8228 || orig_keysym == XK_dead_tilde
8229 #endif
8230 #ifdef XK_dead_diaeresis
8231 || orig_keysym == XK_dead_diaeresis
8232 #endif
8233 #ifdef XK_dead_macron
8234 || orig_keysym == XK_dead_macron
8235 #endif
8236 #ifdef XK_dead_degree
8237 || orig_keysym == XK_dead_degree
8238 #endif
8239 #ifdef XK_dead_acute
8240 || orig_keysym == XK_dead_acute
8241 #endif
8242 #ifdef XK_dead_cedilla
8243 || orig_keysym == XK_dead_cedilla
8244 #endif
8245 #ifdef XK_dead_breve
8246 || orig_keysym == XK_dead_breve
8247 #endif
8248 #ifdef XK_dead_ogonek
8249 || orig_keysym == XK_dead_ogonek
8250 #endif
8251 #ifdef XK_dead_caron
8252 || orig_keysym == XK_dead_caron
8253 #endif
8254 #ifdef XK_dead_doubleacute
8255 || orig_keysym == XK_dead_doubleacute
8256 #endif
8257 #ifdef XK_dead_abovedot
8258 || orig_keysym == XK_dead_abovedot
8259 #endif
8260 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
8261 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
8262 /* Any "vendor-specific" key is ok. */
8263 || (orig_keysym & (1 << 28))
8264 || (keysym != NoSymbol && nbytes == 0))
8265 && ! (IsModifierKey (orig_keysym)
8266 /* The symbols from XK_ISO_Lock
8267 to XK_ISO_Last_Group_Lock
8268 don't have real modifiers but
8269 should be treated similarly to
8270 Mode_switch by Emacs. */
8271 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
8272 || (XK_ISO_Lock <= orig_keysym
8273 && orig_keysym <= XK_ISO_Last_Group_Lock)
8274 #endif
8277 STORE_KEYSYM_FOR_DEBUG (keysym);
8278 /* make_lispy_event will convert this to a symbolic
8279 key. */
8280 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
8281 inev.ie.code = keysym;
8282 goto done_keysym;
8285 { /* Raw bytes, not keysym. */
8286 ptrdiff_t i;
8287 int nchars, len;
8289 for (i = 0, nchars = 0; i < nbytes; i++)
8291 if (ASCII_CHAR_P (copy_bufptr[i]))
8292 nchars++;
8293 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
8296 if (nchars < nbytes)
8298 /* Decode the input data. */
8300 /* The input should be decoded with `coding_system'
8301 which depends on which X*LookupString function
8302 we used just above and the locale. */
8303 setup_coding_system (coding_system, &coding);
8304 coding.src_multibyte = false;
8305 coding.dst_multibyte = true;
8306 /* The input is converted to events, thus we can't
8307 handle composition. Anyway, there's no XIM that
8308 gives us composition information. */
8309 coding.common_flags &= ~CODING_ANNOTATION_MASK;
8311 SAFE_NALLOCA (coding.destination, MAX_MULTIBYTE_LENGTH,
8312 nbytes);
8313 coding.dst_bytes = MAX_MULTIBYTE_LENGTH * nbytes;
8314 coding.mode |= CODING_MODE_LAST_BLOCK;
8315 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
8316 nbytes = coding.produced;
8317 nchars = coding.produced_char;
8318 copy_bufptr = coding.destination;
8321 /* Convert the input data to a sequence of
8322 character events. */
8323 for (i = 0; i < nbytes; i += len)
8325 int ch;
8326 if (nchars == nbytes)
8327 ch = copy_bufptr[i], len = 1;
8328 else
8329 ch = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
8330 inev.ie.kind = (SINGLE_BYTE_CHAR_P (ch)
8331 ? ASCII_KEYSTROKE_EVENT
8332 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
8333 inev.ie.code = ch;
8334 kbd_buffer_store_buffered_event (&inev, hold_quit);
8337 count += nchars;
8339 inev.ie.kind = NO_EVENT; /* Already stored above. */
8341 if (keysym == NoSymbol)
8342 break;
8344 /* FIXME: check side effects and remove this. */
8345 ((XEvent *) event)->xkey = xkey;
8347 done_keysym:
8348 #ifdef HAVE_X_I18N
8349 /* Don't dispatch this event since XtDispatchEvent calls
8350 XFilterEvent, and two calls in a row may freeze the
8351 client. */
8352 break;
8353 #else
8354 goto OTHER;
8355 #endif
8357 case KeyRelease:
8358 x_display_set_last_user_time (dpyinfo, event->xkey.time);
8359 #ifdef HAVE_X_I18N
8360 /* Don't dispatch this event since XtDispatchEvent calls
8361 XFilterEvent, and two calls in a row may freeze the
8362 client. */
8363 break;
8364 #else
8365 goto OTHER;
8366 #endif
8368 case EnterNotify:
8369 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
8370 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8372 f = any;
8374 if (f && x_mouse_click_focus_ignore_position)
8375 ignore_next_mouse_click_timeout = event->xmotion.time + 200;
8377 /* EnterNotify counts as mouse movement,
8378 so update things that depend on mouse position. */
8379 if (f && !f->output_data.x->hourglass_p)
8380 note_mouse_movement (f, &event->xmotion);
8381 #ifdef USE_GTK
8382 /* We may get an EnterNotify on the buttons in the toolbar. In that
8383 case we moved out of any highlighted area and need to note this. */
8384 if (!f && dpyinfo->last_mouse_glyph_frame)
8385 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
8386 #endif
8387 goto OTHER;
8389 case FocusIn:
8390 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8391 goto OTHER;
8393 case LeaveNotify:
8394 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
8395 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8397 f = x_top_window_to_frame (dpyinfo, event->xcrossing.window);
8398 if (f)
8400 if (f == hlinfo->mouse_face_mouse_frame)
8402 /* If we move outside the frame, then we're
8403 certainly no longer on any text in the frame. */
8404 clear_mouse_face (hlinfo);
8405 hlinfo->mouse_face_mouse_frame = 0;
8408 /* Generate a nil HELP_EVENT to cancel a help-echo.
8409 Do it only if there's something to cancel.
8410 Otherwise, the startup message is cleared when
8411 the mouse leaves the frame. */
8412 if (any_help_event_p)
8413 do_help = -1;
8415 #ifdef USE_GTK
8416 /* See comment in EnterNotify above */
8417 else if (dpyinfo->last_mouse_glyph_frame)
8418 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
8419 #endif
8420 goto OTHER;
8422 case FocusOut:
8423 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8424 goto OTHER;
8426 case MotionNotify:
8428 x_display_set_last_user_time (dpyinfo, event->xmotion.time);
8429 previous_help_echo_string = help_echo_string;
8430 help_echo_string = Qnil;
8432 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
8433 : x_window_to_frame (dpyinfo, event->xmotion.window));
8435 if (hlinfo->mouse_face_hidden)
8437 hlinfo->mouse_face_hidden = false;
8438 clear_mouse_face (hlinfo);
8441 #ifdef USE_GTK
8442 if (f && xg_event_is_for_scrollbar (f, event))
8443 f = 0;
8444 #endif
8445 if (f)
8448 /* Generate SELECT_WINDOW_EVENTs when needed.
8449 Don't let popup menus influence things (bug#1261). */
8450 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
8452 static Lisp_Object last_mouse_window;
8453 Lisp_Object window = window_from_coordinates
8454 (f, event->xmotion.x, event->xmotion.y, 0, false);
8456 /* Window will be selected only when it is not selected now and
8457 last mouse movement event was not in it. Minibuffer window
8458 will be selected only when it is active. */
8459 if (WINDOWP (window)
8460 && !EQ (window, last_mouse_window)
8461 && !EQ (window, selected_window)
8462 /* For click-to-focus window managers
8463 create event iff we don't leave the
8464 selected frame. */
8465 && (focus_follows_mouse
8466 || (EQ (XWINDOW (window)->frame,
8467 XWINDOW (selected_window)->frame))))
8469 inev.ie.kind = SELECT_WINDOW_EVENT;
8470 inev.ie.frame_or_window = window;
8472 /* Remember the last window where we saw the mouse. */
8473 last_mouse_window = window;
8475 if (!note_mouse_movement (f, &event->xmotion))
8476 help_echo_string = previous_help_echo_string;
8478 else
8480 #ifndef USE_TOOLKIT_SCROLL_BARS
8481 struct scroll_bar *bar
8482 = x_window_to_scroll_bar (event->xmotion.display,
8483 event->xmotion.window, 2);
8485 if (bar)
8486 x_scroll_bar_note_movement (bar, &event->xmotion);
8487 #endif /* USE_TOOLKIT_SCROLL_BARS */
8489 /* If we move outside the frame, then we're
8490 certainly no longer on any text in the frame. */
8491 clear_mouse_face (hlinfo);
8494 /* If the contents of the global variable help_echo_string
8495 has changed, generate a HELP_EVENT. */
8496 if (!NILP (help_echo_string)
8497 || !NILP (previous_help_echo_string))
8498 do_help = 1;
8499 goto OTHER;
8502 case ConfigureNotify:
8503 /* An opaque move can generate a stream of events as the window
8504 is dragged around. If the connection round trip time isn't
8505 really short, they may come faster than we can respond to
8506 them, given the multiple queries we can do to check window
8507 manager state, translate coordinates, etc.
8509 So if this ConfigureNotify is immediately followed by another
8510 for the same window, use the info from the latest update, and
8511 consider the events all handled. */
8512 /* Opaque resize may be trickier; ConfigureNotify events are
8513 mixed with Expose events for multiple windows. */
8514 configureEvent = *event;
8515 while (XPending (dpyinfo->display))
8517 XNextEvent (dpyinfo->display, &next_event);
8518 if (next_event.type != ConfigureNotify
8519 || next_event.xconfigure.window != event->xconfigure.window
8520 /* Skipping events with different sizes can lead to a
8521 mispositioned mode line at initial window creation.
8522 Only drop window motion events for now. */
8523 || next_event.xconfigure.width != event->xconfigure.width
8524 || next_event.xconfigure.height != event->xconfigure.height)
8526 XPutBackEvent (dpyinfo->display, &next_event);
8527 break;
8529 else
8530 configureEvent = next_event;
8533 f = x_top_window_to_frame (dpyinfo, configureEvent.xconfigure.window);
8534 /* Unfortunately, we need to call font_drop_xrender_surfaces for
8535 _all_ ConfigureNotify events, otherwise we miss some and
8536 flicker. Don't try to optimize these calls by looking only
8537 for size changes: that's not sufficient. We miss some
8538 surface invalidations and flicker. */
8539 block_input ();
8540 if (f && FRAME_X_DOUBLE_BUFFERED_P (f))
8541 font_drop_xrender_surfaces (f);
8542 unblock_input ();
8543 #ifdef USE_CAIRO
8544 if (f) x_cr_destroy_surface (f);
8545 #endif
8546 #ifdef USE_GTK
8547 if (!f
8548 && (f = any)
8549 && configureEvent.xconfigure.window == FRAME_X_WINDOW (f))
8551 block_input ();
8552 if (FRAME_X_DOUBLE_BUFFERED_P (f))
8553 font_drop_xrender_surfaces (f);
8554 unblock_input ();
8555 xg_frame_resized (f, configureEvent.xconfigure.width,
8556 configureEvent.xconfigure.height);
8557 #ifdef USE_CAIRO
8558 x_cr_destroy_surface (f);
8559 #endif
8560 f = 0;
8562 #endif
8563 if (f)
8565 /* Don't call x_net_wm_state for the scroll bar window.
8566 (Bug#24963, Bug#25887) */
8567 if (configureEvent.xconfigure.window == FRAME_X_WINDOW (f))
8568 x_net_wm_state (f, configureEvent.xconfigure.window);
8570 #ifdef USE_X_TOOLKIT
8571 /* Tip frames are pure X window, set size for them. */
8572 if (! NILP (tip_frame) && XFRAME (tip_frame) == f)
8574 if (FRAME_PIXEL_HEIGHT (f) != configureEvent.xconfigure.height
8575 || FRAME_PIXEL_WIDTH (f) != configureEvent.xconfigure.width)
8577 SET_FRAME_GARBAGED (f);
8579 FRAME_PIXEL_HEIGHT (f) = configureEvent.xconfigure.height;
8580 FRAME_PIXEL_WIDTH (f) = configureEvent.xconfigure.width;
8582 #endif
8584 #ifndef USE_X_TOOLKIT
8585 #ifndef USE_GTK
8586 int width =
8587 FRAME_PIXEL_TO_TEXT_WIDTH (f, configureEvent.xconfigure.width);
8588 int height =
8589 FRAME_PIXEL_TO_TEXT_HEIGHT (f, configureEvent.xconfigure.height);
8591 /* In the toolkit version, change_frame_size
8592 is called by the code that handles resizing
8593 of the EmacsFrame widget. */
8595 /* Even if the number of character rows and columns has
8596 not changed, the font size may have changed, so we need
8597 to check the pixel dimensions as well. */
8598 if (width != FRAME_TEXT_WIDTH (f)
8599 || height != FRAME_TEXT_HEIGHT (f)
8600 || configureEvent.xconfigure.width != FRAME_PIXEL_WIDTH (f)
8601 || configureEvent.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
8603 change_frame_size (f, width, height, false, true, false, true);
8604 x_clear_under_internal_border (f);
8605 SET_FRAME_GARBAGED (f);
8606 cancel_mouse_face (f);
8608 #endif /* not USE_GTK */
8609 #endif
8611 #ifdef USE_GTK
8612 /* GTK creates windows but doesn't map them.
8613 Only get real positions when mapped. */
8614 if (FRAME_GTK_OUTER_WIDGET (f)
8615 && gtk_widget_get_mapped (FRAME_GTK_OUTER_WIDGET (f)))
8616 #endif
8617 x_real_positions (f, &f->left_pos, &f->top_pos);
8619 #ifdef HAVE_X_I18N
8620 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
8621 xic_set_statusarea (f);
8622 #endif
8625 goto OTHER;
8627 case ButtonRelease:
8628 case ButtonPress:
8630 /* If we decide we want to generate an event to be seen
8631 by the rest of Emacs, we put it here. */
8632 bool tool_bar_p = false;
8634 memset (&compose_status, 0, sizeof (compose_status));
8635 dpyinfo->last_mouse_glyph_frame = NULL;
8636 x_display_set_last_user_time (dpyinfo, event->xbutton.time);
8638 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
8639 : x_window_to_frame (dpyinfo, event->xbutton.window));
8641 #ifdef USE_GTK
8642 if (f && xg_event_is_for_scrollbar (f, event))
8643 f = 0;
8644 #endif
8645 if (f)
8647 #if ! defined (USE_GTK)
8648 /* Is this in the tool-bar? */
8649 if (WINDOWP (f->tool_bar_window)
8650 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
8652 Lisp_Object window;
8653 int x = event->xbutton.x;
8654 int y = event->xbutton.y;
8656 window = window_from_coordinates (f, x, y, 0, true);
8657 tool_bar_p = EQ (window, f->tool_bar_window);
8659 if (tool_bar_p && event->xbutton.button < 4)
8660 handle_tool_bar_click
8661 (f, x, y, event->xbutton.type == ButtonPress,
8662 x_x_to_emacs_modifiers (dpyinfo, event->xbutton.state));
8664 #endif /* !USE_GTK */
8666 if (!tool_bar_p)
8667 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8668 if (! popup_activated ())
8669 #endif
8671 if (ignore_next_mouse_click_timeout)
8673 if (event->type == ButtonPress
8674 && event->xbutton.time > ignore_next_mouse_click_timeout)
8676 ignore_next_mouse_click_timeout = 0;
8677 construct_mouse_click (&inev.ie, &event->xbutton, f);
8679 if (event->type == ButtonRelease)
8680 ignore_next_mouse_click_timeout = 0;
8682 else
8683 construct_mouse_click (&inev.ie, &event->xbutton, f);
8685 if (FRAME_X_EMBEDDED_P (f))
8686 xembed_send_message (f, event->xbutton.time,
8687 XEMBED_REQUEST_FOCUS, 0, 0, 0);
8689 else
8691 struct scroll_bar *bar
8692 = x_window_to_scroll_bar (event->xbutton.display,
8693 event->xbutton.window, 2);
8695 #ifdef USE_TOOLKIT_SCROLL_BARS
8696 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
8697 scroll bars. */
8698 if (bar && event->xbutton.state & ControlMask)
8700 x_scroll_bar_handle_click (bar, event, &inev.ie);
8701 *finish = X_EVENT_DROP;
8703 #else /* not USE_TOOLKIT_SCROLL_BARS */
8704 if (bar)
8705 x_scroll_bar_handle_click (bar, event, &inev.ie);
8706 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8709 if (event->type == ButtonPress)
8711 dpyinfo->grabbed |= (1 << event->xbutton.button);
8712 dpyinfo->last_mouse_frame = f;
8713 #if ! defined (USE_GTK)
8714 if (f && !tool_bar_p)
8715 f->last_tool_bar_item = -1;
8716 #endif /* not USE_GTK */
8718 else
8719 dpyinfo->grabbed &= ~(1 << event->xbutton.button);
8721 /* Ignore any mouse motion that happened before this event;
8722 any subsequent mouse-movement Emacs events should reflect
8723 only motion after the ButtonPress/Release. */
8724 if (f != 0)
8725 f->mouse_moved = false;
8727 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8728 f = x_menubar_window_to_frame (dpyinfo, event);
8729 /* For a down-event in the menu bar,
8730 don't pass it to Xt right now.
8731 Instead, save it away
8732 and we will pass it to Xt from kbd_buffer_get_event.
8733 That way, we can run some Lisp code first. */
8734 if (! popup_activated ()
8735 #ifdef USE_GTK
8736 /* Gtk+ menus only react to the first three buttons. */
8737 && event->xbutton.button < 3
8738 #endif
8739 && f && event->type == ButtonPress
8740 /* Verify the event is really within the menu bar
8741 and not just sent to it due to grabbing. */
8742 && event->xbutton.x >= 0
8743 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
8744 && event->xbutton.y >= 0
8745 && event->xbutton.y < FRAME_MENUBAR_HEIGHT (f)
8746 && event->xbutton.same_screen)
8748 if (!f->output_data.x->saved_menu_event)
8749 f->output_data.x->saved_menu_event = xmalloc (sizeof *event);
8750 *f->output_data.x->saved_menu_event = *event;
8751 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT;
8752 XSETFRAME (inev.ie.frame_or_window, f);
8753 *finish = X_EVENT_DROP;
8755 else
8756 goto OTHER;
8757 #endif /* USE_X_TOOLKIT || USE_GTK */
8759 break;
8761 case CirculateNotify:
8762 goto OTHER;
8764 case CirculateRequest:
8765 goto OTHER;
8767 case VisibilityNotify:
8768 goto OTHER;
8770 case MappingNotify:
8771 /* Someone has changed the keyboard mapping - update the
8772 local cache. */
8773 switch (event->xmapping.request)
8775 case MappingModifier:
8776 x_find_modifier_meanings (dpyinfo);
8777 /* This is meant to fall through. */
8778 case MappingKeyboard:
8779 XRefreshKeyboardMapping ((XMappingEvent *) &event->xmapping);
8781 goto OTHER;
8783 case DestroyNotify:
8784 xft_settings_event (dpyinfo, event);
8785 break;
8787 default:
8788 OTHER:
8789 #ifdef USE_X_TOOLKIT
8790 block_input ();
8791 if (*finish != X_EVENT_DROP)
8792 XtDispatchEvent ((XEvent *) event);
8793 unblock_input ();
8794 #endif /* USE_X_TOOLKIT */
8795 break;
8798 done:
8799 if (inev.ie.kind != NO_EVENT)
8801 kbd_buffer_store_buffered_event (&inev, hold_quit);
8802 count++;
8805 if (do_help
8806 && !(hold_quit && hold_quit->kind != NO_EVENT))
8808 Lisp_Object frame;
8810 if (f)
8811 XSETFRAME (frame, f);
8812 else
8813 frame = Qnil;
8815 if (do_help > 0)
8817 any_help_event_p = true;
8818 gen_help_event (help_echo_string, frame, help_echo_window,
8819 help_echo_object, help_echo_pos);
8821 else
8823 help_echo_string = Qnil;
8824 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
8826 count++;
8829 /* Sometimes event processing draws to the frame outside redisplay.
8830 To ensure that these changes become visible, draw them here. */
8831 flush_dirty_back_buffers ();
8832 SAFE_FREE ();
8833 return count;
8836 /* Handles the XEvent EVENT on display DISPLAY.
8837 This is used for event loops outside the normal event handling,
8838 i.e. looping while a popup menu or a dialog is posted.
8840 Returns the value handle_one_xevent sets in the finish argument. */
8842 x_dispatch_event (XEvent *event, Display *display)
8844 struct x_display_info *dpyinfo;
8845 int finish = X_EVENT_NORMAL;
8847 dpyinfo = x_display_info_for_display (display);
8849 if (dpyinfo)
8850 handle_one_xevent (dpyinfo, event, &finish, 0);
8852 return finish;
8855 /* Read events coming from the X server.
8856 Return as soon as there are no more events to be read.
8858 Return the number of characters stored into the buffer,
8859 thus pretending to be `read' (except the characters we store
8860 in the keyboard buffer can be multibyte, so are not necessarily
8861 C chars). */
8863 static int
8864 XTread_socket (struct terminal *terminal, struct input_event *hold_quit)
8866 int count = 0;
8867 bool event_found = false;
8868 struct x_display_info *dpyinfo = terminal->display_info.x;
8870 block_input ();
8872 /* For debugging, this gives a way to fake an I/O error. */
8873 if (dpyinfo == XTread_socket_fake_io_error)
8875 XTread_socket_fake_io_error = 0;
8876 x_io_error_quitter (dpyinfo->display);
8879 #ifndef USE_GTK
8880 while (XPending (dpyinfo->display))
8882 int finish;
8883 XEvent event;
8885 XNextEvent (dpyinfo->display, &event);
8887 #ifdef HAVE_X_I18N
8888 /* Filter events for the current X input method. */
8889 if (x_filter_event (dpyinfo, &event))
8890 continue;
8891 #endif
8892 event_found = true;
8894 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
8896 if (finish == X_EVENT_GOTO_OUT)
8897 break;
8900 #else /* USE_GTK */
8902 /* For GTK we must use the GTK event loop. But XEvents gets passed
8903 to our filter function above, and then to the big event switch.
8904 We use a bunch of globals to communicate with our filter function,
8905 that is kind of ugly, but it works.
8907 There is no way to do one display at the time, GTK just does events
8908 from all displays. */
8910 while (gtk_events_pending ())
8912 current_count = count;
8913 current_hold_quit = hold_quit;
8915 gtk_main_iteration ();
8917 count = current_count;
8918 current_count = -1;
8919 current_hold_quit = 0;
8921 if (current_finish == X_EVENT_GOTO_OUT)
8922 break;
8924 #endif /* USE_GTK */
8926 /* On some systems, an X bug causes Emacs to get no more events
8927 when the window is destroyed. Detect that. (1994.) */
8928 if (! event_found)
8930 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
8931 One XNOOP in 100 loops will make Emacs terminate.
8932 B. Bretthauer, 1994 */
8933 x_noop_count++;
8934 if (x_noop_count >= 100)
8936 x_noop_count=0;
8938 if (next_noop_dpyinfo == 0)
8939 next_noop_dpyinfo = x_display_list;
8941 XNoOp (next_noop_dpyinfo->display);
8943 /* Each time we get here, cycle through the displays now open. */
8944 next_noop_dpyinfo = next_noop_dpyinfo->next;
8948 /* If the focus was just given to an auto-raising frame,
8949 raise it now. FIXME: handle more than one such frame. */
8950 if (dpyinfo->x_pending_autoraise_frame)
8952 x_raise_frame (dpyinfo->x_pending_autoraise_frame);
8953 dpyinfo->x_pending_autoraise_frame = NULL;
8956 unblock_input ();
8958 return count;
8964 /***********************************************************************
8965 Text Cursor
8966 ***********************************************************************/
8968 /* Set clipping for output in glyph row ROW. W is the window in which
8969 we operate. GC is the graphics context to set clipping in.
8971 ROW may be a text row or, e.g., a mode line. Text rows must be
8972 clipped to the interior of the window dedicated to text display,
8973 mode lines must be clipped to the whole window. */
8975 static void
8976 x_clip_to_row (struct window *w, struct glyph_row *row,
8977 enum glyph_row_area area, GC gc)
8979 struct frame *f = XFRAME (WINDOW_FRAME (w));
8980 XRectangle clip_rect;
8981 int window_x, window_y, window_width;
8983 window_box (w, area, &window_x, &window_y, &window_width, 0);
8985 clip_rect.x = window_x;
8986 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
8987 clip_rect.y = max (clip_rect.y, window_y);
8988 clip_rect.width = window_width;
8989 clip_rect.height = row->visible_height;
8991 x_set_clip_rectangles (f, gc, &clip_rect, 1);
8995 /* Draw a hollow box cursor on window W in glyph row ROW. */
8997 static void
8998 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
9000 struct frame *f = XFRAME (WINDOW_FRAME (w));
9001 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9002 Display *dpy = FRAME_X_DISPLAY (f);
9003 int x, y, wd, h;
9004 XGCValues xgcv;
9005 struct glyph *cursor_glyph;
9006 GC gc;
9008 /* Get the glyph the cursor is on. If we can't tell because
9009 the current matrix is invalid or such, give up. */
9010 cursor_glyph = get_phys_cursor_glyph (w);
9011 if (cursor_glyph == NULL)
9012 return;
9014 /* Compute frame-relative coordinates for phys cursor. */
9015 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
9016 wd = w->phys_cursor_width - 1;
9018 /* The foreground of cursor_gc is typically the same as the normal
9019 background color, which can cause the cursor box to be invisible. */
9020 xgcv.foreground = f->output_data.x->cursor_pixel;
9021 if (dpyinfo->scratch_cursor_gc)
9022 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
9023 else
9024 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_DRAWABLE (f),
9025 GCForeground, &xgcv);
9026 gc = dpyinfo->scratch_cursor_gc;
9028 /* When on R2L character, show cursor at the right edge of the
9029 glyph, unless the cursor box is as wide as the glyph or wider
9030 (the latter happens when x-stretch-cursor is non-nil). */
9031 if ((cursor_glyph->resolved_level & 1) != 0
9032 && cursor_glyph->pixel_width > wd)
9034 x += cursor_glyph->pixel_width - wd;
9035 if (wd > 0)
9036 wd -= 1;
9038 /* Set clipping, draw the rectangle, and reset clipping again. */
9039 x_clip_to_row (w, row, TEXT_AREA, gc);
9040 x_draw_rectangle (f, gc, x, y, wd, h - 1);
9041 x_reset_clip_rectangles (f, gc);
9045 /* Draw a bar cursor on window W in glyph row ROW.
9047 Implementation note: One would like to draw a bar cursor with an
9048 angle equal to the one given by the font property XA_ITALIC_ANGLE.
9049 Unfortunately, I didn't find a font yet that has this property set.
9050 --gerd. */
9052 static void
9053 x_draw_bar_cursor (struct window *w, struct glyph_row *row, int width, enum text_cursor_kinds kind)
9055 struct frame *f = XFRAME (w->frame);
9056 struct glyph *cursor_glyph;
9058 /* If cursor is out of bounds, don't draw garbage. This can happen
9059 in mini-buffer windows when switching between echo area glyphs
9060 and mini-buffer. */
9061 cursor_glyph = get_phys_cursor_glyph (w);
9062 if (cursor_glyph == NULL)
9063 return;
9065 /* Experimental avoidance of cursor on xwidget. */
9066 if (cursor_glyph->type == XWIDGET_GLYPH)
9067 return;
9069 /* If on an image, draw like a normal cursor. That's usually better
9070 visible than drawing a bar, esp. if the image is large so that
9071 the bar might not be in the window. */
9072 if (cursor_glyph->type == IMAGE_GLYPH)
9074 struct glyph_row *r;
9075 r = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
9076 draw_phys_cursor_glyph (w, r, DRAW_CURSOR);
9078 else
9080 Display *dpy = FRAME_X_DISPLAY (f);
9081 Drawable drawable = FRAME_X_DRAWABLE (f);
9082 GC gc = FRAME_DISPLAY_INFO (f)->scratch_cursor_gc;
9083 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
9084 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
9085 XGCValues xgcv;
9087 /* If the glyph's background equals the color we normally draw
9088 the bars cursor in, the bar cursor in its normal color is
9089 invisible. Use the glyph's foreground color instead in this
9090 case, on the assumption that the glyph's colors are chosen so
9091 that the glyph is legible. */
9092 if (face->background == f->output_data.x->cursor_pixel)
9093 xgcv.background = xgcv.foreground = face->foreground;
9094 else
9095 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
9096 xgcv.graphics_exposures = False;
9098 if (gc)
9099 XChangeGC (dpy, gc, mask, &xgcv);
9100 else
9102 gc = XCreateGC (dpy, drawable, mask, &xgcv);
9103 FRAME_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
9106 x_clip_to_row (w, row, TEXT_AREA, gc);
9108 if (kind == BAR_CURSOR)
9110 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9112 if (width < 0)
9113 width = FRAME_CURSOR_WIDTH (f);
9114 width = min (cursor_glyph->pixel_width, width);
9116 w->phys_cursor_width = width;
9118 /* If the character under cursor is R2L, draw the bar cursor
9119 on the right of its glyph, rather than on the left. */
9120 if ((cursor_glyph->resolved_level & 1) != 0)
9121 x += cursor_glyph->pixel_width - width;
9123 x_fill_rectangle (f, gc, x,
9124 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
9125 width, row->height);
9127 else /* HBAR_CURSOR */
9129 int dummy_x, dummy_y, dummy_h;
9130 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9132 if (width < 0)
9133 width = row->height;
9135 width = min (row->height, width);
9137 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
9138 &dummy_y, &dummy_h);
9140 if ((cursor_glyph->resolved_level & 1) != 0
9141 && cursor_glyph->pixel_width > w->phys_cursor_width - 1)
9142 x += cursor_glyph->pixel_width - w->phys_cursor_width + 1;
9143 x_fill_rectangle (f, gc, x,
9144 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
9145 row->height - width),
9146 w->phys_cursor_width - 1, width);
9149 x_reset_clip_rectangles (f, gc);
9154 /* RIF: Define cursor CURSOR on frame F. */
9156 static void
9157 x_define_frame_cursor (struct frame *f, Cursor cursor)
9159 if (!f->pointer_invisible
9160 && f->output_data.x->current_cursor != cursor)
9161 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
9162 f->output_data.x->current_cursor = cursor;
9166 /* RIF: Clear area on frame F. */
9168 static void
9169 x_clear_frame_area (struct frame *f, int x, int y, int width, int height)
9171 x_clear_area (f, x, y, width, height);
9175 /* RIF: Draw cursor on window W. */
9177 static void
9178 x_draw_window_cursor (struct window *w, struct glyph_row *glyph_row, int x,
9179 int y, enum text_cursor_kinds cursor_type,
9180 int cursor_width, bool on_p, bool active_p)
9182 struct frame *f = XFRAME (WINDOW_FRAME (w));
9184 if (on_p)
9186 w->phys_cursor_type = cursor_type;
9187 w->phys_cursor_on_p = true;
9189 if (glyph_row->exact_window_width_line_p
9190 && (glyph_row->reversed_p
9191 ? (w->phys_cursor.hpos < 0)
9192 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
9194 glyph_row->cursor_in_fringe_p = true;
9195 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
9197 else
9199 switch (cursor_type)
9201 case HOLLOW_BOX_CURSOR:
9202 x_draw_hollow_cursor (w, glyph_row);
9203 break;
9205 case FILLED_BOX_CURSOR:
9206 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
9207 break;
9209 case BAR_CURSOR:
9210 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
9211 break;
9213 case HBAR_CURSOR:
9214 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
9215 break;
9217 case NO_CURSOR:
9218 w->phys_cursor_width = 0;
9219 break;
9221 default:
9222 emacs_abort ();
9226 #ifdef HAVE_X_I18N
9227 if (w == XWINDOW (f->selected_window))
9228 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
9229 xic_set_preeditarea (w, x, y);
9230 #endif
9233 XFlush (FRAME_X_DISPLAY (f));
9237 /* Icons. */
9239 /* Make the x-window of frame F use the gnu icon bitmap. */
9241 bool
9242 x_bitmap_icon (struct frame *f, Lisp_Object file)
9244 ptrdiff_t bitmap_id;
9246 if (FRAME_X_WINDOW (f) == 0)
9247 return true;
9249 /* Free up our existing icon bitmap and mask if any. */
9250 if (f->output_data.x->icon_bitmap > 0)
9251 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
9252 f->output_data.x->icon_bitmap = 0;
9254 if (STRINGP (file))
9256 #ifdef USE_GTK
9257 /* Use gtk_window_set_icon_from_file () if available,
9258 It's not restricted to bitmaps */
9259 if (xg_set_icon (f, file))
9260 return false;
9261 #endif /* USE_GTK */
9262 bitmap_id = x_create_bitmap_from_file (f, file);
9263 x_create_bitmap_mask (f, bitmap_id);
9265 else
9267 /* Create the GNU bitmap and mask if necessary. */
9268 if (FRAME_DISPLAY_INFO (f)->icon_bitmap_id < 0)
9270 ptrdiff_t rc = -1;
9272 #ifdef USE_GTK
9274 if (xg_set_icon (f, xg_default_icon_file)
9275 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
9277 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = -2;
9278 return false;
9281 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
9283 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
9284 if (rc != -1)
9285 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
9287 #endif
9289 /* If all else fails, use the (black and white) xbm image. */
9290 if (rc == -1)
9292 rc = x_create_bitmap_from_data (f, (char *) gnu_xbm_bits,
9293 gnu_xbm_width, gnu_xbm_height);
9294 if (rc == -1)
9295 return true;
9297 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
9298 x_create_bitmap_mask (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
9302 /* The first time we create the GNU bitmap and mask,
9303 this increments the ref-count one extra time.
9304 As a result, the GNU bitmap and mask are never freed.
9305 That way, we don't have to worry about allocating it again. */
9306 x_reference_bitmap (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
9308 bitmap_id = FRAME_DISPLAY_INFO (f)->icon_bitmap_id;
9311 x_wm_set_icon_pixmap (f, bitmap_id);
9312 f->output_data.x->icon_bitmap = bitmap_id;
9314 return false;
9318 /* Make the x-window of frame F use a rectangle with text.
9319 Use ICON_NAME as the text. */
9321 bool
9322 x_text_icon (struct frame *f, const char *icon_name)
9324 if (FRAME_X_WINDOW (f) == 0)
9325 return true;
9328 XTextProperty text;
9329 text.value = (unsigned char *) icon_name;
9330 text.encoding = XA_STRING;
9331 text.format = 8;
9332 text.nitems = strlen (icon_name);
9333 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
9336 if (f->output_data.x->icon_bitmap > 0)
9337 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
9338 f->output_data.x->icon_bitmap = 0;
9339 x_wm_set_icon_pixmap (f, 0);
9341 return false;
9344 #define X_ERROR_MESSAGE_SIZE 200
9346 /* If non-nil, this should be a string.
9347 It means catch X errors and store the error message in this string.
9349 The reason we use a stack is that x_catch_error/x_uncatch_error can
9350 be called from a signal handler.
9353 struct x_error_message_stack {
9354 char string[X_ERROR_MESSAGE_SIZE];
9355 Display *dpy;
9356 x_special_error_handler handler;
9357 void *handler_data;
9358 struct x_error_message_stack *prev;
9360 static struct x_error_message_stack *x_error_message;
9362 /* An X error handler which stores the error message in
9363 *x_error_message. This is called from x_error_handler if
9364 x_catch_errors is in effect. */
9366 static void
9367 x_error_catcher (Display *display, XErrorEvent *event)
9369 XGetErrorText (display, event->error_code,
9370 x_error_message->string,
9371 X_ERROR_MESSAGE_SIZE);
9372 if (x_error_message->handler)
9373 x_error_message->handler (display, event, x_error_message->string,
9374 x_error_message->handler_data);
9377 /* Begin trapping X errors for display DPY. Actually we trap X errors
9378 for all displays, but DPY should be the display you are actually
9379 operating on.
9381 After calling this function, X protocol errors no longer cause
9382 Emacs to exit; instead, they are recorded in the string
9383 stored in *x_error_message.
9385 Calling x_check_errors signals an Emacs error if an X error has
9386 occurred since the last call to x_catch_errors or x_check_errors.
9388 Calling x_uncatch_errors resumes the normal error handling.
9389 Calling x_uncatch_errors_after_check is similar, but skips an XSync
9390 to the server, and should be used only immediately after
9391 x_had_errors_p or x_check_errors. */
9393 void
9394 x_catch_errors_with_handler (Display *dpy, x_special_error_handler handler,
9395 void *handler_data)
9397 struct x_error_message_stack *data = xmalloc (sizeof *data);
9399 /* Make sure any errors from previous requests have been dealt with. */
9400 XSync (dpy, False);
9402 data->dpy = dpy;
9403 data->string[0] = 0;
9404 data->handler = handler;
9405 data->handler_data = handler_data;
9406 data->prev = x_error_message;
9407 x_error_message = data;
9410 void
9411 x_catch_errors (Display *dpy)
9413 x_catch_errors_with_handler (dpy, NULL, NULL);
9416 /* Undo the last x_catch_errors call.
9417 DPY should be the display that was passed to x_catch_errors.
9419 This version should be used only if the immediately preceding
9420 X-protocol-related thing was x_check_errors or x_had_error_p, both
9421 of which issue XSync calls, so we don't need to re-sync here. */
9423 void
9424 x_uncatch_errors_after_check (void)
9426 struct x_error_message_stack *tmp;
9428 block_input ();
9429 tmp = x_error_message;
9430 x_error_message = x_error_message->prev;
9431 xfree (tmp);
9432 unblock_input ();
9435 /* Undo the last x_catch_errors call.
9436 DPY should be the display that was passed to x_catch_errors. */
9438 void
9439 x_uncatch_errors (void)
9441 struct x_error_message_stack *tmp;
9443 block_input ();
9445 /* The display may have been closed before this function is called.
9446 Check if it is still open before calling XSync. */
9447 if (x_display_info_for_display (x_error_message->dpy) != 0)
9448 XSync (x_error_message->dpy, False);
9450 tmp = x_error_message;
9451 x_error_message = x_error_message->prev;
9452 xfree (tmp);
9453 unblock_input ();
9456 /* If any X protocol errors have arrived since the last call to
9457 x_catch_errors or x_check_errors, signal an Emacs error using
9458 sprintf (a buffer, FORMAT, the x error message text) as the text. */
9460 void
9461 x_check_errors (Display *dpy, const char *format)
9463 /* Make sure to catch any errors incurred so far. */
9464 XSync (dpy, False);
9466 if (x_error_message->string[0])
9468 char string[X_ERROR_MESSAGE_SIZE];
9469 memcpy (string, x_error_message->string, X_ERROR_MESSAGE_SIZE);
9470 x_uncatch_errors ();
9471 error (format, string);
9475 /* Nonzero if we had any X protocol errors
9476 since we did x_catch_errors on DPY. */
9478 bool
9479 x_had_errors_p (Display *dpy)
9481 /* Make sure to catch any errors incurred so far. */
9482 XSync (dpy, False);
9484 return x_error_message->string[0] != 0;
9487 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
9489 void
9490 x_clear_errors (Display *dpy)
9492 x_error_message->string[0] = 0;
9495 #if false
9496 /* See comment in unwind_to_catch why calling this is a bad
9497 * idea. --lorentey */
9498 /* Close off all unclosed x_catch_errors calls. */
9500 void
9501 x_fully_uncatch_errors (void)
9503 while (x_error_message)
9504 x_uncatch_errors ();
9506 #endif
9508 #if false
9509 static unsigned int x_wire_count;
9510 x_trace_wire (void)
9512 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
9514 #endif
9517 /************************************************************************
9518 Handling X errors
9519 ************************************************************************/
9521 /* Error message passed to x_connection_closed. */
9523 static char *error_msg;
9525 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
9526 the text of an error message that lead to the connection loss. */
9528 static _Noreturn void
9529 x_connection_closed (Display *dpy, const char *error_message, bool ioerror)
9531 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
9532 Lisp_Object frame, tail;
9533 ptrdiff_t idx = SPECPDL_INDEX ();
9535 error_msg = alloca (strlen (error_message) + 1);
9536 strcpy (error_msg, error_message);
9538 /* Inhibit redisplay while frames are being deleted. */
9539 specbind (Qinhibit_redisplay, Qt);
9541 if (dpyinfo)
9543 /* Protect display from being closed when we delete the last
9544 frame on it. */
9545 dpyinfo->reference_count++;
9546 dpyinfo->terminal->reference_count++;
9548 if (ioerror) dpyinfo->display = 0;
9550 /* First delete frames whose mini-buffers are on frames
9551 that are on the dead display. */
9552 FOR_EACH_FRAME (tail, frame)
9554 Lisp_Object minibuf_frame;
9555 minibuf_frame
9556 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
9557 if (FRAME_X_P (XFRAME (frame))
9558 && FRAME_X_P (XFRAME (minibuf_frame))
9559 && ! EQ (frame, minibuf_frame)
9560 && FRAME_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
9561 delete_frame (frame, Qnoelisp);
9564 /* Now delete all remaining frames on the dead display.
9565 We are now sure none of these is used as the mini-buffer
9566 for another frame that we need to delete. */
9567 FOR_EACH_FRAME (tail, frame)
9568 if (FRAME_X_P (XFRAME (frame))
9569 && FRAME_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
9571 /* Set this to t so that delete_frame won't get confused
9572 trying to find a replacement. */
9573 kset_default_minibuffer_frame (FRAME_KBOARD (XFRAME (frame)), Qt);
9574 delete_frame (frame, Qnoelisp);
9577 /* If DPYINFO is null, this means we didn't open the display in the
9578 first place, so don't try to close it. */
9579 if (dpyinfo)
9581 /* We can not call XtCloseDisplay here because it calls XSync.
9582 XSync inside the error handler apparently hangs Emacs. On
9583 current Xt versions, this isn't needed either. */
9584 #ifdef USE_GTK
9585 /* A long-standing GTK bug prevents proper disconnect handling
9586 (https://bugzilla.gnome.org/show_bug.cgi?id=85715). Once,
9587 the resulting Glib error message loop filled a user's disk.
9588 To avoid this, kill Emacs unconditionally on disconnect. */
9589 shut_down_emacs (0, Qnil);
9590 fprintf (stderr, "%s\n\
9591 When compiled with GTK, Emacs cannot recover from X disconnects.\n\
9592 This is a GTK bug: https://bugzilla.gnome.org/show_bug.cgi?id=85715\n\
9593 For details, see etc/PROBLEMS.\n",
9594 error_msg);
9595 emacs_abort ();
9596 #endif /* USE_GTK */
9598 /* Indicate that this display is dead. */
9599 dpyinfo->display = 0;
9601 dpyinfo->reference_count--;
9602 dpyinfo->terminal->reference_count--;
9603 if (dpyinfo->reference_count != 0)
9604 /* We have just closed all frames on this display. */
9605 emacs_abort ();
9608 Lisp_Object tmp;
9609 XSETTERMINAL (tmp, dpyinfo->terminal);
9610 Fdelete_terminal (tmp, Qnoelisp);
9614 if (terminal_list == 0)
9616 fprintf (stderr, "%s\n", error_msg);
9617 Fkill_emacs (make_number (70));
9618 /* NOTREACHED */
9621 totally_unblock_input ();
9623 unbind_to (idx, Qnil);
9624 clear_waiting_for_input ();
9626 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
9627 longjmp), because returning from this function would get us back into
9628 Xlib's code which will directly call `exit'. */
9629 error ("%s", error_msg);
9632 /* We specifically use it before defining it, so that gcc doesn't inline it,
9633 otherwise gdb doesn't know how to properly put a breakpoint on it. */
9634 static void x_error_quitter (Display *, XErrorEvent *);
9636 /* This is the first-level handler for X protocol errors.
9637 It calls x_error_quitter or x_error_catcher. */
9639 static int
9640 x_error_handler (Display *display, XErrorEvent *event)
9642 #if defined USE_GTK && defined HAVE_GTK3
9643 if ((event->error_code == BadMatch || event->error_code == BadWindow)
9644 && event->request_code == X_SetInputFocus)
9646 return 0;
9648 #endif
9650 if (x_error_message)
9651 x_error_catcher (display, event);
9652 else
9653 x_error_quitter (display, event);
9654 return 0;
9657 /* This is the usual handler for X protocol errors.
9658 It kills all frames on the display that we got the error for.
9659 If that was the only one, it prints an error message and kills Emacs. */
9661 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
9663 /* On older GCC versions, just putting x_error_quitter
9664 after x_error_handler prevents inlining into the former. */
9666 static void NO_INLINE
9667 x_error_quitter (Display *display, XErrorEvent *event)
9669 char buf[256], buf1[356];
9671 /* Ignore BadName errors. They can happen because of fonts
9672 or colors that are not defined. */
9674 if (event->error_code == BadName)
9675 return;
9677 /* Note that there is no real way portable across R3/R4 to get the
9678 original error handler. */
9680 XGetErrorText (display, event->error_code, buf, sizeof (buf));
9681 sprintf (buf1, "X protocol error: %s on protocol request %d",
9682 buf, event->request_code);
9683 x_connection_closed (display, buf1, false);
9687 /* This is the handler for X IO errors, always.
9688 It kills all frames on the display that we lost touch with.
9689 If that was the only one, it prints an error message and kills Emacs. */
9691 static _Noreturn int
9692 x_io_error_quitter (Display *display)
9694 char buf[256];
9696 snprintf (buf, sizeof buf, "Connection lost to X server '%s'",
9697 DisplayString (display));
9698 x_connection_closed (display, buf, true);
9699 assume (false);
9702 /* Changing the font of the frame. */
9704 /* Give frame F the font FONT-OBJECT as its default font. The return
9705 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
9706 frame. If it is negative, generate a new fontset from
9707 FONT-OBJECT. */
9709 Lisp_Object
9710 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
9712 struct font *font = XFONT_OBJECT (font_object);
9713 int unit, font_ascent, font_descent;
9714 #ifndef USE_X_TOOLKIT
9715 int old_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
9716 Lisp_Object fullscreen;
9717 #endif
9719 if (fontset < 0)
9720 fontset = fontset_from_font (font_object);
9721 FRAME_FONTSET (f) = fontset;
9722 if (FRAME_FONT (f) == font)
9723 /* This font is already set in frame F. There's nothing more to
9724 do. */
9725 return font_object;
9727 FRAME_FONT (f) = font;
9728 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
9729 FRAME_COLUMN_WIDTH (f) = font->average_width;
9730 get_font_ascent_descent (font, &font_ascent, &font_descent);
9731 FRAME_LINE_HEIGHT (f) = font_ascent + font_descent;
9733 #ifndef USE_X_TOOLKIT
9734 FRAME_MENU_BAR_HEIGHT (f) = FRAME_MENU_BAR_LINES (f) * FRAME_LINE_HEIGHT (f);
9735 #endif
9737 /* Compute character columns occupied by scrollbar.
9739 Don't do things differently for non-toolkit scrollbars
9740 (Bug#17163). */
9741 unit = FRAME_COLUMN_WIDTH (f);
9742 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
9743 FRAME_CONFIG_SCROLL_BAR_COLS (f)
9744 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + unit - 1) / unit;
9745 else
9746 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
9748 if (FRAME_X_WINDOW (f) != 0)
9750 /* Don't change the size of a tip frame; there's no point in
9751 doing it because it's done in Fx_show_tip, and it leads to
9752 problems because the tip frame has no widget. */
9753 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
9755 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
9756 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 3,
9757 false, Qfont);
9758 #ifndef USE_X_TOOLKIT
9759 if (FRAME_MENU_BAR_HEIGHT (f) != old_menu_bar_height
9760 && !f->after_make_frame
9761 && (EQ (frame_inhibit_implied_resize, Qt)
9762 || (CONSP (frame_inhibit_implied_resize)
9763 && NILP (Fmemq (Qfont, frame_inhibit_implied_resize))))
9764 && (NILP (fullscreen = get_frame_param (f, Qfullscreen))
9765 || EQ (fullscreen, Qfullwidth)))
9766 /* If the menu bar height changes, try to keep text height
9767 constant. */
9768 adjust_frame_size
9769 (f, -1, FRAME_TEXT_HEIGHT (f) + FRAME_MENU_BAR_HEIGHT (f)
9770 - old_menu_bar_height, 1, false, Qfont);
9771 #endif /* USE_X_TOOLKIT */
9775 #ifdef HAVE_X_I18N
9776 if (FRAME_XIC (f)
9777 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
9779 block_input ();
9780 xic_set_xfontset (f, SSDATA (fontset_ascii (fontset)));
9781 unblock_input ();
9783 #endif
9785 return font_object;
9789 /***********************************************************************
9790 X Input Methods
9791 ***********************************************************************/
9793 #ifdef HAVE_X_I18N
9795 #ifdef HAVE_X11R6
9797 /* XIM destroy callback function, which is called whenever the
9798 connection to input method XIM dies. CLIENT_DATA contains a
9799 pointer to the x_display_info structure corresponding to XIM. */
9801 static void
9802 xim_destroy_callback (XIM xim, XPointer client_data, XPointer call_data)
9804 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
9805 Lisp_Object frame, tail;
9807 block_input ();
9809 /* No need to call XDestroyIC.. */
9810 FOR_EACH_FRAME (tail, frame)
9812 struct frame *f = XFRAME (frame);
9813 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
9815 FRAME_XIC (f) = NULL;
9816 xic_free_xfontset (f);
9820 /* No need to call XCloseIM. */
9821 dpyinfo->xim = NULL;
9822 XFree (dpyinfo->xim_styles);
9823 unblock_input ();
9826 #endif /* HAVE_X11R6 */
9828 /* Open the connection to the XIM server on display DPYINFO.
9829 RESOURCE_NAME is the resource name Emacs uses. */
9831 static void
9832 xim_open_dpy (struct x_display_info *dpyinfo, char *resource_name)
9834 XIM xim;
9836 #ifdef HAVE_XIM
9837 if (use_xim)
9839 if (dpyinfo->xim)
9840 XCloseIM (dpyinfo->xim);
9841 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
9842 emacs_class);
9843 dpyinfo->xim = xim;
9845 if (xim)
9847 #ifdef HAVE_X11R6
9848 XIMCallback destroy;
9849 #endif
9851 /* Get supported styles and XIM values. */
9852 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
9854 #ifdef HAVE_X11R6
9855 destroy.callback = xim_destroy_callback;
9856 destroy.client_data = (XPointer)dpyinfo;
9857 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
9858 #endif
9862 else
9863 #endif /* HAVE_XIM */
9864 dpyinfo->xim = NULL;
9868 #ifdef HAVE_X11R6_XIM
9870 /* XIM instantiate callback function, which is called whenever an XIM
9871 server is available. DISPLAY is the display of the XIM.
9872 CLIENT_DATA contains a pointer to an xim_inst_t structure created
9873 when the callback was registered. */
9875 static void
9876 xim_instantiate_callback (Display *display, XPointer client_data, XPointer call_data)
9878 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
9879 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
9881 /* We don't support multiple XIM connections. */
9882 if (dpyinfo->xim)
9883 return;
9885 xim_open_dpy (dpyinfo, xim_inst->resource_name);
9887 /* Create XIC for the existing frames on the same display, as long
9888 as they have no XIC. */
9889 if (dpyinfo->xim && dpyinfo->reference_count > 0)
9891 Lisp_Object tail, frame;
9893 block_input ();
9894 FOR_EACH_FRAME (tail, frame)
9896 struct frame *f = XFRAME (frame);
9898 if (FRAME_X_P (f)
9899 && FRAME_DISPLAY_INFO (f) == xim_inst->dpyinfo)
9900 if (FRAME_XIC (f) == NULL)
9902 create_frame_xic (f);
9903 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
9904 xic_set_statusarea (f);
9905 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
9907 struct window *w = XWINDOW (f->selected_window);
9908 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
9913 unblock_input ();
9917 #endif /* HAVE_X11R6_XIM */
9920 /* Open a connection to the XIM server on display DPYINFO.
9921 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
9922 connection only at the first time. On X11R6, open the connection
9923 in the XIM instantiate callback function. */
9925 static void
9926 xim_initialize (struct x_display_info *dpyinfo, char *resource_name)
9928 dpyinfo->xim = NULL;
9929 #ifdef HAVE_XIM
9930 if (use_xim)
9932 #ifdef HAVE_X11R6_XIM
9933 struct xim_inst_t *xim_inst = xmalloc (sizeof *xim_inst);
9934 Bool ret;
9936 dpyinfo->xim_callback_data = xim_inst;
9937 xim_inst->dpyinfo = dpyinfo;
9938 xim_inst->resource_name = xstrdup (resource_name);
9939 ret = XRegisterIMInstantiateCallback
9940 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
9941 emacs_class, xim_instantiate_callback,
9942 /* This is XPointer in XFree86 but (XPointer *)
9943 on Tru64, at least, hence the configure test. */
9944 (XRegisterIMInstantiateCallback_arg6) xim_inst);
9945 eassert (ret == True);
9946 #else /* not HAVE_X11R6_XIM */
9947 xim_open_dpy (dpyinfo, resource_name);
9948 #endif /* not HAVE_X11R6_XIM */
9950 #endif /* HAVE_XIM */
9954 /* Close the connection to the XIM server on display DPYINFO. */
9956 static void
9957 xim_close_dpy (struct x_display_info *dpyinfo)
9959 #ifdef HAVE_XIM
9960 if (use_xim)
9962 #ifdef HAVE_X11R6_XIM
9963 struct xim_inst_t *xim_inst = dpyinfo->xim_callback_data;
9965 if (dpyinfo->display)
9967 Bool ret = XUnregisterIMInstantiateCallback
9968 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
9969 emacs_class, xim_instantiate_callback,
9970 (XRegisterIMInstantiateCallback_arg6) xim_inst);
9971 eassert (ret == True);
9973 xfree (xim_inst->resource_name);
9974 xfree (xim_inst);
9975 #endif /* HAVE_X11R6_XIM */
9976 if (dpyinfo->display)
9977 XCloseIM (dpyinfo->xim);
9978 dpyinfo->xim = NULL;
9979 XFree (dpyinfo->xim_styles);
9981 #endif /* HAVE_XIM */
9984 #endif /* not HAVE_X11R6_XIM */
9988 /* Calculate the absolute position in frame F
9989 from its current recorded position values and gravity. */
9991 static void
9992 x_calc_absolute_position (struct frame *f)
9994 int flags = f->size_hint_flags;
9996 /* We have nothing to do if the current position
9997 is already for the top-left corner. */
9998 if (! ((flags & XNegative) || (flags & YNegative)))
9999 return;
10001 /* Treat negative positions as relative to the leftmost bottommost
10002 position that fits on the screen. */
10003 if (flags & XNegative)
10004 f->left_pos = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
10005 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
10008 int height = FRAME_PIXEL_HEIGHT (f);
10010 #if defined USE_X_TOOLKIT && defined USE_MOTIF
10011 /* Something is fishy here. When using Motif, starting Emacs with
10012 `-g -0-0', the frame appears too low by a few pixels.
10014 This seems to be so because initially, while Emacs is starting,
10015 the column widget's height and the frame's pixel height are
10016 different. The column widget's height is the right one. In
10017 later invocations, when Emacs is up, the frame's pixel height
10018 is right, though.
10020 It's not obvious where the initial small difference comes from.
10021 2000-12-01, gerd. */
10023 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
10024 #endif
10026 if (flags & YNegative)
10027 f->top_pos = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
10028 - height + f->top_pos;
10031 /* The left_pos and top_pos
10032 are now relative to the top and left screen edges,
10033 so the flags should correspond. */
10034 f->size_hint_flags &= ~ (XNegative | YNegative);
10037 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
10038 to really change the position, and 0 when calling from
10039 x_make_frame_visible (in that case, XOFF and YOFF are the current
10040 position values). It is -1 when calling from x_set_frame_parameters,
10041 which means, do adjust for borders but don't change the gravity. */
10043 void
10044 x_set_offset (struct frame *f, register int xoff, register int yoff, int change_gravity)
10046 int modified_top, modified_left;
10048 if (change_gravity > 0)
10050 f->top_pos = yoff;
10051 f->left_pos = xoff;
10052 f->size_hint_flags &= ~ (XNegative | YNegative);
10053 if (xoff < 0)
10054 f->size_hint_flags |= XNegative;
10055 if (yoff < 0)
10056 f->size_hint_flags |= YNegative;
10057 f->win_gravity = NorthWestGravity;
10060 x_calc_absolute_position (f);
10062 block_input ();
10063 x_wm_set_size_hint (f, 0, false);
10065 #ifdef USE_GTK
10066 if (x_gtk_use_window_move)
10068 /* When a position change was requested and the outer GTK widget
10069 has been realized already, leave it to gtk_window_move to DTRT
10070 and return. Used for Bug#25851 and Bug#25943. */
10071 if (change_gravity != 0 && FRAME_GTK_OUTER_WIDGET (f))
10072 gtk_window_move (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
10073 f->left_pos, f->top_pos);
10074 unblock_input ();
10075 return;
10077 #endif /* USE_GTK */
10079 modified_left = f->left_pos;
10080 modified_top = f->top_pos;
10082 if (change_gravity != 0 && FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
10084 /* Some WMs (twm, wmaker at least) has an offset that is smaller
10085 than the WM decorations. So we use the calculated offset instead
10086 of the WM decoration sizes here (x/y_pixels_outer_diff). */
10087 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
10088 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
10091 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10092 modified_left, modified_top);
10094 x_sync_with_move (f, f->left_pos, f->top_pos,
10095 FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN);
10097 /* change_gravity is non-zero when this function is called from Lisp to
10098 programmatically move a frame. In that case, we call
10099 x_check_expected_move to discover if we have a "Type A" or "Type B"
10100 window manager, and, for a "Type A" window manager, adjust the position
10101 of the frame.
10103 We call x_check_expected_move if a programmatic move occurred, and
10104 either the window manager type (A/B) is unknown or it is Type A but we
10105 need to compute the top/left offset adjustment for this frame. */
10107 if (change_gravity != 0
10108 && (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
10109 || (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
10110 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
10111 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
10112 x_check_expected_move (f, modified_left, modified_top);
10114 unblock_input ();
10117 /* Return true if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
10118 on the root window for frame F contains ATOMNAME.
10119 This is how a WM check shall be done according to the Window Manager
10120 Specification/Extended Window Manager Hints at
10121 http://freedesktop.org/wiki/Specifications/wm-spec. */
10123 bool
10124 x_wm_supports (struct frame *f, Atom want_atom)
10126 Atom actual_type;
10127 unsigned long actual_size, bytes_remaining;
10128 int i, rc, actual_format;
10129 bool ret;
10130 Window wmcheck_window;
10131 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10132 Window target_window = dpyinfo->root_window;
10133 int max_len = 65536;
10134 Display *dpy = FRAME_X_DISPLAY (f);
10135 unsigned char *tmp_data = NULL;
10136 Atom target_type = XA_WINDOW;
10138 block_input ();
10140 x_catch_errors (dpy);
10141 rc = XGetWindowProperty (dpy, target_window,
10142 dpyinfo->Xatom_net_supporting_wm_check,
10143 0, max_len, False, target_type,
10144 &actual_type, &actual_format, &actual_size,
10145 &bytes_remaining, &tmp_data);
10147 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
10149 if (tmp_data) XFree (tmp_data);
10150 x_uncatch_errors ();
10151 unblock_input ();
10152 return false;
10155 wmcheck_window = *(Window *) tmp_data;
10156 XFree (tmp_data);
10158 /* Check if window exists. */
10159 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
10160 if (x_had_errors_p (dpy))
10162 x_uncatch_errors_after_check ();
10163 unblock_input ();
10164 return false;
10167 if (dpyinfo->net_supported_window != wmcheck_window)
10169 /* Window changed, reload atoms */
10170 if (dpyinfo->net_supported_atoms != NULL)
10171 XFree (dpyinfo->net_supported_atoms);
10172 dpyinfo->net_supported_atoms = NULL;
10173 dpyinfo->nr_net_supported_atoms = 0;
10174 dpyinfo->net_supported_window = 0;
10176 target_type = XA_ATOM;
10177 tmp_data = NULL;
10178 rc = XGetWindowProperty (dpy, target_window,
10179 dpyinfo->Xatom_net_supported,
10180 0, max_len, False, target_type,
10181 &actual_type, &actual_format, &actual_size,
10182 &bytes_remaining, &tmp_data);
10184 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
10186 if (tmp_data) XFree (tmp_data);
10187 x_uncatch_errors ();
10188 unblock_input ();
10189 return false;
10192 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
10193 dpyinfo->nr_net_supported_atoms = actual_size;
10194 dpyinfo->net_supported_window = wmcheck_window;
10197 ret = false;
10199 for (i = 0; !ret && i < dpyinfo->nr_net_supported_atoms; ++i)
10200 ret = dpyinfo->net_supported_atoms[i] == want_atom;
10202 x_uncatch_errors ();
10203 unblock_input ();
10205 return ret;
10208 static void
10209 set_wm_state (Lisp_Object frame, bool add, Atom atom, Atom value)
10211 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (XFRAME (frame));
10213 x_send_client_event (frame, make_number (0), frame,
10214 dpyinfo->Xatom_net_wm_state,
10215 make_number (32),
10216 /* 1 = add, 0 = remove */
10217 Fcons
10218 (make_number (add),
10219 Fcons
10220 (make_fixnum_or_float (atom),
10221 (value != 0
10222 ? list1 (make_fixnum_or_float (value))
10223 : Qnil))));
10226 void
10227 x_set_sticky (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
10229 Lisp_Object frame;
10230 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10232 XSETFRAME (frame, f);
10234 set_wm_state (frame, !NILP (new_value),
10235 dpyinfo->Xatom_net_wm_state_sticky, None);
10238 /* Return the current _NET_WM_STATE.
10239 SIZE_STATE is set to one of the FULLSCREEN_* values.
10240 Set *STICKY to the sticky state.
10242 Return true iff we are not hidden. */
10244 static bool
10245 get_current_wm_state (struct frame *f,
10246 Window window,
10247 int *size_state,
10248 bool *sticky)
10250 unsigned long actual_size;
10251 int i;
10252 bool is_hidden = false;
10253 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10254 long max_len = 65536;
10255 Atom target_type = XA_ATOM;
10256 /* If XCB is available, we can avoid three XSync calls. */
10257 #ifdef USE_XCB
10258 xcb_get_property_cookie_t prop_cookie;
10259 xcb_get_property_reply_t *prop;
10260 xcb_atom_t *reply_data;
10261 #else
10262 Display *dpy = FRAME_X_DISPLAY (f);
10263 unsigned long bytes_remaining;
10264 int rc, actual_format;
10265 Atom actual_type;
10266 unsigned char *tmp_data = NULL;
10267 Atom *reply_data;
10268 #endif
10270 *sticky = false;
10271 *size_state = FULLSCREEN_NONE;
10273 block_input ();
10275 #ifdef USE_XCB
10276 prop_cookie = xcb_get_property (dpyinfo->xcb_connection, 0, window,
10277 dpyinfo->Xatom_net_wm_state,
10278 target_type, 0, max_len);
10279 prop = xcb_get_property_reply (dpyinfo->xcb_connection, prop_cookie, NULL);
10280 if (prop && prop->type == target_type)
10282 int actual_bytes = xcb_get_property_value_length (prop);
10283 eassume (0 <= actual_bytes);
10284 actual_size = actual_bytes / sizeof *reply_data;
10285 reply_data = xcb_get_property_value (prop);
10287 else
10289 actual_size = 0;
10290 is_hidden = FRAME_ICONIFIED_P (f);
10292 #else
10293 x_catch_errors (dpy);
10294 rc = XGetWindowProperty (dpy, window, dpyinfo->Xatom_net_wm_state,
10295 0, max_len, False, target_type,
10296 &actual_type, &actual_format, &actual_size,
10297 &bytes_remaining, &tmp_data);
10299 if (rc == Success && actual_type == target_type && ! x_had_errors_p (dpy))
10300 reply_data = (Atom *) tmp_data;
10301 else
10303 actual_size = 0;
10304 is_hidden = FRAME_ICONIFIED_P (f);
10307 x_uncatch_errors ();
10308 #endif
10310 for (i = 0; i < actual_size; ++i)
10312 Atom a = reply_data[i];
10313 if (a == dpyinfo->Xatom_net_wm_state_hidden)
10314 is_hidden = true;
10315 else if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
10317 if (*size_state == FULLSCREEN_HEIGHT)
10318 *size_state = FULLSCREEN_MAXIMIZED;
10319 else
10320 *size_state = FULLSCREEN_WIDTH;
10322 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
10324 if (*size_state == FULLSCREEN_WIDTH)
10325 *size_state = FULLSCREEN_MAXIMIZED;
10326 else
10327 *size_state = FULLSCREEN_HEIGHT;
10329 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen)
10330 *size_state = FULLSCREEN_BOTH;
10331 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
10332 *sticky = true;
10335 #ifdef USE_XCB
10336 free (prop);
10337 #else
10338 if (tmp_data) XFree (tmp_data);
10339 #endif
10341 unblock_input ();
10342 return ! is_hidden;
10345 /* Do fullscreen as specified in extended window manager hints */
10347 static bool
10348 do_ewmh_fullscreen (struct frame *f)
10350 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10351 bool have_net_atom = x_wm_supports (f, dpyinfo->Xatom_net_wm_state);
10352 int cur;
10353 bool dummy;
10355 get_current_wm_state (f, FRAME_OUTER_WINDOW (f), &cur, &dummy);
10357 /* Some window managers don't say they support _NET_WM_STATE, but they do say
10358 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
10359 if (!have_net_atom)
10360 have_net_atom = x_wm_supports (f, dpyinfo->Xatom_net_wm_state_fullscreen);
10362 if (have_net_atom && cur != f->want_fullscreen)
10364 Lisp_Object frame;
10366 XSETFRAME (frame, f);
10368 /* Keep number of calls to set_wm_state as low as possible.
10369 Some window managers, or possible Gtk+, hangs when too many
10370 are sent at once. */
10371 switch (f->want_fullscreen)
10373 case FULLSCREEN_BOTH:
10374 if (cur != FULLSCREEN_BOTH)
10375 set_wm_state (frame, true, dpyinfo->Xatom_net_wm_state_fullscreen,
10376 None);
10377 break;
10378 case FULLSCREEN_WIDTH:
10379 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_MAXIMIZED)
10381 set_wm_state (frame, false,
10382 dpyinfo->Xatom_net_wm_state_maximized_horz,
10383 dpyinfo->Xatom_net_wm_state_maximized_vert);
10384 set_wm_state (frame, true,
10385 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10387 else
10389 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_HEIGHT
10390 || cur == FULLSCREEN_MAXIMIZED)
10391 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10392 dpyinfo->Xatom_net_wm_state_maximized_vert);
10393 if (cur != FULLSCREEN_MAXIMIZED || x_frame_normalize_before_maximize)
10394 set_wm_state (frame, true,
10395 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10397 break;
10398 case FULLSCREEN_HEIGHT:
10399 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_MAXIMIZED)
10401 set_wm_state (frame, false,
10402 dpyinfo->Xatom_net_wm_state_maximized_horz,
10403 dpyinfo->Xatom_net_wm_state_maximized_vert);
10404 set_wm_state (frame, true,
10405 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10407 else
10409 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_WIDTH
10410 || cur == FULLSCREEN_MAXIMIZED)
10411 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10412 dpyinfo->Xatom_net_wm_state_maximized_horz);
10413 if (cur != FULLSCREEN_MAXIMIZED || x_frame_normalize_before_maximize)
10414 set_wm_state (frame, true,
10415 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10417 break;
10418 case FULLSCREEN_MAXIMIZED:
10419 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_BOTH)
10421 set_wm_state (frame, false,
10422 dpyinfo->Xatom_net_wm_state_fullscreen, None);
10423 set_wm_state (frame, true,
10424 dpyinfo->Xatom_net_wm_state_maximized_horz,
10425 dpyinfo->Xatom_net_wm_state_maximized_vert);
10427 else if (x_frame_normalize_before_maximize && cur == FULLSCREEN_WIDTH)
10429 set_wm_state (frame, false,
10430 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10431 set_wm_state (frame, true,
10432 dpyinfo->Xatom_net_wm_state_maximized_horz,
10433 dpyinfo->Xatom_net_wm_state_maximized_vert);
10435 else if (x_frame_normalize_before_maximize && cur == FULLSCREEN_HEIGHT)
10437 set_wm_state (frame, false,
10438 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10439 set_wm_state (frame, true,
10440 dpyinfo->Xatom_net_wm_state_maximized_horz,
10441 dpyinfo->Xatom_net_wm_state_maximized_vert);
10443 else
10445 if (cur == FULLSCREEN_BOTH)
10446 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10447 None);
10448 else if (cur == FULLSCREEN_HEIGHT)
10449 set_wm_state (frame, true,
10450 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10451 else if (cur == FULLSCREEN_WIDTH)
10452 set_wm_state (frame, true, None,
10453 dpyinfo->Xatom_net_wm_state_maximized_vert);
10454 else
10455 set_wm_state (frame, true,
10456 dpyinfo->Xatom_net_wm_state_maximized_horz,
10457 dpyinfo->Xatom_net_wm_state_maximized_vert);
10459 break;
10460 case FULLSCREEN_NONE:
10461 if (cur == FULLSCREEN_BOTH)
10462 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10463 None);
10464 else
10465 set_wm_state (frame, false,
10466 dpyinfo->Xatom_net_wm_state_maximized_horz,
10467 dpyinfo->Xatom_net_wm_state_maximized_vert);
10470 f->want_fullscreen = FULLSCREEN_NONE;
10474 return have_net_atom;
10477 static void
10478 XTfullscreen_hook (struct frame *f)
10480 if (FRAME_VISIBLE_P (f))
10482 block_input ();
10483 x_check_fullscreen (f);
10484 x_sync (f);
10485 unblock_input ();
10490 static bool
10491 x_handle_net_wm_state (struct frame *f, const XPropertyEvent *event)
10493 int value = FULLSCREEN_NONE;
10494 Lisp_Object lval;
10495 bool sticky = false;
10496 bool not_hidden = get_current_wm_state (f, event->window, &value, &sticky);
10498 lval = Qnil;
10499 switch (value)
10501 case FULLSCREEN_WIDTH:
10502 lval = Qfullwidth;
10503 break;
10504 case FULLSCREEN_HEIGHT:
10505 lval = Qfullheight;
10506 break;
10507 case FULLSCREEN_BOTH:
10508 lval = Qfullboth;
10509 break;
10510 case FULLSCREEN_MAXIMIZED:
10511 lval = Qmaximized;
10512 break;
10515 frame_size_history_add
10516 (f, Qx_handle_net_wm_state, 0, 0,
10517 list2 (get_frame_param (f, Qfullscreen), lval));
10519 store_frame_param (f, Qfullscreen, lval);
10520 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
10522 return not_hidden;
10525 /* Check if we need to resize the frame due to a fullscreen request.
10526 If so needed, resize the frame. */
10527 static void
10528 x_check_fullscreen (struct frame *f)
10530 Lisp_Object lval = Qnil;
10532 if (do_ewmh_fullscreen (f))
10533 return;
10535 if (f->output_data.x->parent_desc != FRAME_DISPLAY_INFO (f)->root_window)
10536 return; /* Only fullscreen without WM or with EWM hints (above). */
10538 /* Setting fullscreen to nil doesn't do anything. We could save the
10539 last non-fullscreen size and restore it, but it seems like a
10540 lot of work for this unusual case (no window manager running). */
10542 if (f->want_fullscreen != FULLSCREEN_NONE)
10544 int width = FRAME_PIXEL_WIDTH (f), height = FRAME_PIXEL_HEIGHT (f);
10545 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10547 switch (f->want_fullscreen)
10549 /* No difference between these two when there is no WM */
10550 case FULLSCREEN_MAXIMIZED:
10551 lval = Qmaximized;
10552 width = x_display_pixel_width (dpyinfo);
10553 height = x_display_pixel_height (dpyinfo);
10554 break;
10555 case FULLSCREEN_BOTH:
10556 lval = Qfullboth;
10557 width = x_display_pixel_width (dpyinfo);
10558 height = x_display_pixel_height (dpyinfo);
10559 break;
10560 case FULLSCREEN_WIDTH:
10561 lval = Qfullwidth;
10562 width = x_display_pixel_width (dpyinfo);
10563 height = height + FRAME_MENUBAR_HEIGHT (f);
10564 break;
10565 case FULLSCREEN_HEIGHT:
10566 lval = Qfullheight;
10567 height = x_display_pixel_height (dpyinfo);
10568 break;
10569 default:
10570 emacs_abort ();
10573 frame_size_history_add
10574 (f, Qx_check_fullscreen, width, height, Qnil);
10576 x_wm_set_size_hint (f, 0, false);
10578 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10579 width, height);
10581 if (FRAME_VISIBLE_P (f))
10582 x_wait_for_event (f, ConfigureNotify);
10583 else
10585 change_frame_size (f, width, height - FRAME_MENUBAR_HEIGHT (f),
10586 false, true, false, true);
10587 x_sync (f);
10591 /* `x_net_wm_state' might have reset the fullscreen frame parameter,
10592 restore it. */
10593 store_frame_param (f, Qfullscreen, lval);
10596 /* This function is called by x_set_offset to determine whether the window
10597 manager interfered with the positioning of the frame. Type A window
10598 managers position the surrounding window manager decorations a small
10599 amount above and left of the user-supplied position. Type B window
10600 managers position the surrounding window manager decorations at the
10601 user-specified position. If we detect a Type A window manager, we
10602 compensate by moving the window right and down by the proper amount. */
10604 static void
10605 x_check_expected_move (struct frame *f, int expected_left, int expected_top)
10607 int current_left = 0, current_top = 0;
10609 /* x_real_positions returns the left and top offsets of the outermost
10610 window manager window around the frame. */
10612 x_real_positions (f, &current_left, &current_top);
10614 if (current_left != expected_left || current_top != expected_top)
10616 /* It's a "Type A" window manager. */
10618 int adjusted_left;
10619 int adjusted_top;
10621 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
10622 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
10623 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
10625 /* Now fix the mispositioned frame's location. */
10627 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
10628 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
10630 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10631 adjusted_left, adjusted_top);
10633 x_sync_with_move (f, expected_left, expected_top, false);
10635 else
10636 /* It's a "Type B" window manager. We don't have to adjust the
10637 frame's position. */
10639 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
10643 /* Wait for XGetGeometry to return up-to-date position information for a
10644 recently-moved frame. Call this immediately after calling XMoveWindow.
10645 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
10646 frame has been moved to, so we use a fuzzy position comparison instead
10647 of an exact comparison. */
10649 static void
10650 x_sync_with_move (struct frame *f, int left, int top, bool fuzzy)
10652 int count = 0;
10654 while (count++ < 50)
10656 int current_left = 0, current_top = 0;
10658 /* In theory, this call to XSync only needs to happen once, but in
10659 practice, it doesn't seem to work, hence the need for the surrounding
10660 loop. */
10662 XSync (FRAME_X_DISPLAY (f), False);
10663 x_real_positions (f, &current_left, &current_top);
10665 if (fuzzy)
10667 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
10668 pixels. */
10670 if (eabs (current_left - left) <= 10
10671 && eabs (current_top - top) <= 40)
10672 return;
10674 else if (current_left == left && current_top == top)
10675 return;
10678 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
10679 will then return up-to-date position info. */
10681 wait_reading_process_output (0, 500000000, 0, false, Qnil, NULL, 0);
10685 /* Wait for an event on frame F matching EVENTTYPE. */
10686 void
10687 x_wait_for_event (struct frame *f, int eventtype)
10689 int level = interrupt_input_blocked;
10691 fd_set fds;
10692 struct timespec tmo, tmo_at, time_now;
10693 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
10695 f->wait_event_type = eventtype;
10697 /* Set timeout to 0.1 second. Hopefully not noticeable.
10698 Maybe it should be configurable. */
10699 tmo = make_timespec (0, 100 * 1000 * 1000);
10700 tmo_at = timespec_add (current_timespec (), tmo);
10702 while (f->wait_event_type)
10704 pending_signals = true;
10705 totally_unblock_input ();
10706 /* XTread_socket is called after unblock. */
10707 block_input ();
10708 interrupt_input_blocked = level;
10710 FD_ZERO (&fds);
10711 FD_SET (fd, &fds);
10713 time_now = current_timespec ();
10714 if (timespec_cmp (tmo_at, time_now) < 0)
10715 break;
10717 tmo = timespec_sub (tmo_at, time_now);
10718 if (pselect (fd + 1, &fds, NULL, NULL, &tmo, NULL) == 0)
10719 break; /* Timeout */
10722 f->wait_event_type = 0;
10726 /* Change the size of frame F's X window to WIDTH/HEIGHT in the case F
10727 doesn't have a widget. If CHANGE_GRAVITY, change to
10728 top-left-corner window gravity for this size change and subsequent
10729 size changes. Otherwise leave the window gravity unchanged. */
10731 static void
10732 x_set_window_size_1 (struct frame *f, bool change_gravity,
10733 int width, int height)
10735 int pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
10736 int pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
10737 int old_width = FRAME_PIXEL_WIDTH (f);
10738 int old_height = FRAME_PIXEL_HEIGHT (f);
10739 Lisp_Object fullscreen = get_frame_param (f, Qfullscreen);
10741 if (change_gravity) f->win_gravity = NorthWestGravity;
10742 x_wm_set_size_hint (f, 0, false);
10744 /* When the frame is fullheight and we only want to change the width
10745 or it is fullwidth and we only want to change the height we should
10746 be able to preserve the fullscreen property. However, due to the
10747 fact that we have to send a resize request anyway, the window
10748 manager will abolish it. At least the respective size should
10749 remain unchanged but giving the frame back its normal size will
10750 be broken ... */
10751 if (EQ (fullscreen, Qfullwidth) && width == FRAME_TEXT_WIDTH (f))
10753 frame_size_history_add
10754 (f, Qx_set_window_size_1, width, height,
10755 list2 (make_number (old_height),
10756 make_number (pixelheight + FRAME_MENUBAR_HEIGHT (f))));
10758 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10759 old_width, pixelheight + FRAME_MENUBAR_HEIGHT (f));
10761 else if (EQ (fullscreen, Qfullheight) && height == FRAME_TEXT_HEIGHT (f))
10763 frame_size_history_add
10764 (f, Qx_set_window_size_2, width, height,
10765 list2 (make_number (old_width), make_number (pixelwidth)));
10767 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10768 pixelwidth, old_height);
10771 else
10773 frame_size_history_add
10774 (f, Qx_set_window_size_3, width, height,
10775 list3 (make_number (pixelwidth + FRAME_TOOLBAR_WIDTH (f)),
10776 make_number (pixelheight + FRAME_TOOLBAR_HEIGHT (f)
10777 + FRAME_MENUBAR_HEIGHT (f)),
10778 make_number (FRAME_MENUBAR_HEIGHT (f))));
10780 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10781 pixelwidth, pixelheight + FRAME_MENUBAR_HEIGHT (f));
10782 fullscreen = Qnil;
10787 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
10788 receive in the ConfigureNotify event; if we get what we asked
10789 for, then the event won't cause the screen to become garbaged, so
10790 we have to make sure to do it here. */
10791 SET_FRAME_GARBAGED (f);
10793 /* Now, strictly speaking, we can't be sure that this is accurate,
10794 but the window manager will get around to dealing with the size
10795 change request eventually, and we'll hear how it went when the
10796 ConfigureNotify event gets here.
10798 We could just not bother storing any of this information here,
10799 and let the ConfigureNotify event set everything up, but that
10800 might be kind of confusing to the Lisp code, since size changes
10801 wouldn't be reported in the frame parameters until some random
10802 point in the future when the ConfigureNotify event arrives.
10804 Pass true for DELAY since we can't run Lisp code inside of
10805 a BLOCK_INPUT. */
10807 /* But the ConfigureNotify may in fact never arrive, and then this is
10808 not right if the frame is visible. Instead wait (with timeout)
10809 for the ConfigureNotify. */
10810 if (FRAME_VISIBLE_P (f))
10812 x_wait_for_event (f, ConfigureNotify);
10814 if (!NILP (fullscreen))
10815 /* Try to restore fullscreen state. */
10817 store_frame_param (f, Qfullscreen, fullscreen);
10818 x_set_fullscreen (f, fullscreen, fullscreen);
10821 else
10823 change_frame_size (f, width, height, false, true, false, true);
10824 x_sync (f);
10829 /* Call this to change the size of frame F's x-window.
10830 If CHANGE_GRAVITY, change to top-left-corner window gravity
10831 for this size change and subsequent size changes.
10832 Otherwise we leave the window gravity unchanged. */
10834 void
10835 x_set_window_size (struct frame *f, bool change_gravity,
10836 int width, int height, bool pixelwise)
10838 block_input ();
10840 /* The following breaks our calculations. If it's really needed,
10841 think of something else. */
10842 #if false
10843 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
10845 int text_width, text_height;
10847 /* When the frame is maximized/fullscreen or running under for
10848 example Xmonad, x_set_window_size_1 will be a no-op.
10849 In that case, the right thing to do is extend rows/width to
10850 the current frame size. We do that first if x_set_window_size_1
10851 turns out to not be a no-op (there is no way to know).
10852 The size will be adjusted again if the frame gets a
10853 ConfigureNotify event as a result of x_set_window_size. */
10854 int pixelh = FRAME_PIXEL_HEIGHT (f);
10855 #ifdef USE_X_TOOLKIT
10856 /* The menu bar is not part of text lines. The tool bar
10857 is however. */
10858 pixelh -= FRAME_MENUBAR_HEIGHT (f);
10859 #endif
10860 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, FRAME_PIXEL_WIDTH (f));
10861 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelh);
10863 change_frame_size (f, text_width, text_height, false, true, false, true);
10865 #endif
10867 /* Pixelize width and height, if necessary. */
10868 if (! pixelwise)
10870 width = width * FRAME_COLUMN_WIDTH (f);
10871 height = height * FRAME_LINE_HEIGHT (f);
10874 #ifdef USE_GTK
10875 if (FRAME_GTK_WIDGET (f))
10876 xg_frame_set_char_size (f, width, height);
10877 else
10878 x_set_window_size_1 (f, change_gravity, width, height);
10879 #else /* not USE_GTK */
10880 x_set_window_size_1 (f, change_gravity, width, height);
10881 x_clear_under_internal_border (f);
10882 #endif /* not USE_GTK */
10884 /* If cursor was outside the new size, mark it as off. */
10885 mark_window_cursors_off (XWINDOW (f->root_window));
10887 /* Clear out any recollection of where the mouse highlighting was,
10888 since it might be in a place that's outside the new frame size.
10889 Actually checking whether it is outside is a pain in the neck,
10890 so don't try--just let the highlighting be done afresh with new size. */
10891 cancel_mouse_face (f);
10893 unblock_input ();
10895 do_pending_window_change (false);
10898 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
10900 void
10901 frame_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
10903 block_input ();
10905 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
10906 0, 0, 0, 0, pix_x, pix_y);
10907 unblock_input ();
10910 /* Raise frame F. */
10912 void
10913 x_raise_frame (struct frame *f)
10915 block_input ();
10916 if (FRAME_VISIBLE_P (f))
10917 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
10918 XFlush (FRAME_X_DISPLAY (f));
10919 unblock_input ();
10922 /* Lower frame F. */
10924 static void
10925 x_lower_frame (struct frame *f)
10927 if (FRAME_VISIBLE_P (f))
10929 block_input ();
10930 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
10931 XFlush (FRAME_X_DISPLAY (f));
10932 unblock_input ();
10936 /* Request focus with XEmbed */
10938 void
10939 xembed_request_focus (struct frame *f)
10941 /* See XEmbed Protocol Specification at
10942 http://freedesktop.org/wiki/Specifications/xembed-spec */
10943 if (FRAME_VISIBLE_P (f))
10944 xembed_send_message (f, CurrentTime,
10945 XEMBED_REQUEST_FOCUS, 0, 0, 0);
10948 /* Activate frame with Extended Window Manager Hints */
10950 void
10951 x_ewmh_activate_frame (struct frame *f)
10953 /* See Window Manager Specification/Extended Window Manager Hints at
10954 http://freedesktop.org/wiki/Specifications/wm-spec */
10956 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10958 if (FRAME_VISIBLE_P (f) && x_wm_supports (f, dpyinfo->Xatom_net_active_window))
10960 Lisp_Object frame;
10961 XSETFRAME (frame, f);
10962 x_send_client_event (frame, make_number (0), frame,
10963 dpyinfo->Xatom_net_active_window,
10964 make_number (32),
10965 list2i (1, dpyinfo->last_user_time));
10969 static void
10970 XTframe_raise_lower (struct frame *f, bool raise_flag)
10972 if (raise_flag)
10973 x_raise_frame (f);
10974 else
10975 x_lower_frame (f);
10978 /* XEmbed implementation. */
10980 #if defined USE_X_TOOLKIT || ! defined USE_GTK
10982 /* XEmbed implementation. */
10984 #define XEMBED_VERSION 0
10986 static void
10987 xembed_set_info (struct frame *f, enum xembed_info flags)
10989 unsigned long data[2];
10990 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10992 data[0] = XEMBED_VERSION;
10993 data[1] = flags;
10995 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10996 dpyinfo->Xatom_XEMBED_INFO, dpyinfo->Xatom_XEMBED_INFO,
10997 32, PropModeReplace, (unsigned char *) data, 2);
10999 #endif /* defined USE_X_TOOLKIT || ! defined USE_GTK */
11001 static void
11002 xembed_send_message (struct frame *f, Time t, enum xembed_message msg,
11003 long int detail, long int data1, long int data2)
11005 XEvent event;
11007 event.xclient.type = ClientMessage;
11008 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
11009 event.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_XEMBED;
11010 event.xclient.format = 32;
11011 event.xclient.data.l[0] = t;
11012 event.xclient.data.l[1] = msg;
11013 event.xclient.data.l[2] = detail;
11014 event.xclient.data.l[3] = data1;
11015 event.xclient.data.l[4] = data2;
11017 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
11018 False, NoEventMask, &event);
11019 XSync (FRAME_X_DISPLAY (f), False);
11022 /* Change of visibility. */
11024 /* This function sends the request to make the frame visible, but may
11025 return before it the frame's visibility is changed. */
11027 void
11028 x_make_frame_visible (struct frame *f)
11030 block_input ();
11032 x_set_bitmap_icon (f);
11034 if (! FRAME_VISIBLE_P (f))
11036 /* We test asked_for_visible here to make sure we don't
11037 call x_set_offset a second time
11038 if we get to x_make_frame_visible a second time
11039 before the window gets really visible. */
11040 if (! FRAME_ICONIFIED_P (f)
11041 && ! FRAME_X_EMBEDDED_P (f)
11042 && ! f->output_data.x->asked_for_visible)
11043 x_set_offset (f, f->left_pos, f->top_pos, 0);
11045 f->output_data.x->asked_for_visible = true;
11047 if (! EQ (Vx_no_window_manager, Qt))
11048 x_wm_set_window_state (f, NormalState);
11049 #ifdef USE_X_TOOLKIT
11050 if (FRAME_X_EMBEDDED_P (f))
11051 xembed_set_info (f, XEMBED_MAPPED);
11052 else
11054 /* This was XtPopup, but that did nothing for an iconified frame. */
11055 XtMapWidget (f->output_data.x->widget);
11057 #else /* not USE_X_TOOLKIT */
11058 #ifdef USE_GTK
11059 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
11060 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
11061 #else
11062 if (FRAME_X_EMBEDDED_P (f))
11063 xembed_set_info (f, XEMBED_MAPPED);
11064 else
11065 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11066 #endif /* not USE_GTK */
11067 #endif /* not USE_X_TOOLKIT */
11070 XFlush (FRAME_X_DISPLAY (f));
11072 /* Synchronize to ensure Emacs knows the frame is visible
11073 before we do anything else. We do this loop with input not blocked
11074 so that incoming events are handled. */
11076 /* This must be before UNBLOCK_INPUT
11077 since events that arrive in response to the actions above
11078 will set it when they are handled. */
11079 bool previously_visible = f->output_data.x->has_been_visible;
11081 int original_left = f->left_pos;
11082 int original_top = f->top_pos;
11084 /* This must come after we set COUNT. */
11085 unblock_input ();
11087 /* We unblock here so that arriving X events are processed. */
11089 /* Now move the window back to where it was "supposed to be".
11090 But don't do it if the gravity is negative.
11091 When the gravity is negative, this uses a position
11092 that is 3 pixels too low. Perhaps that's really the border width.
11094 Don't do this if the window has never been visible before,
11095 because the window manager may choose the position
11096 and we don't want to override it. */
11098 if (! FRAME_VISIBLE_P (f)
11099 && ! FRAME_ICONIFIED_P (f)
11100 && ! FRAME_X_EMBEDDED_P (f)
11101 && f->win_gravity == NorthWestGravity
11102 && previously_visible)
11104 Drawable rootw;
11105 int x, y;
11106 unsigned int width, height, border, depth;
11108 block_input ();
11110 /* On some window managers (such as FVWM) moving an existing
11111 window, even to the same place, causes the window manager
11112 to introduce an offset. This can cause the window to move
11113 to an unexpected location. Check the geometry (a little
11114 slow here) and then verify that the window is in the right
11115 place. If the window is not in the right place, move it
11116 there, and take the potential window manager hit. */
11117 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11118 &rootw, &x, &y, &width, &height, &border, &depth);
11120 if (original_left != x || original_top != y)
11121 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11122 original_left, original_top);
11124 unblock_input ();
11129 /* Change from mapped state to withdrawn state. */
11131 /* Make the frame visible (mapped and not iconified). */
11133 void
11134 x_make_frame_invisible (struct frame *f)
11136 Window window;
11138 /* Use the frame's outermost window, not the one we normally draw on. */
11139 window = FRAME_OUTER_WINDOW (f);
11141 /* Don't keep the highlight on an invisible frame. */
11142 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
11143 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
11145 block_input ();
11147 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11148 that the current position of the window is user-specified, rather than
11149 program-specified, so that when the window is mapped again, it will be
11150 placed at the same location, without forcing the user to position it
11151 by hand again (they have already done that once for this window.) */
11152 x_wm_set_size_hint (f, 0, true);
11154 #ifdef USE_GTK
11155 if (FRAME_GTK_OUTER_WIDGET (f))
11156 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
11157 else
11158 #else
11159 if (FRAME_X_EMBEDDED_P (f))
11160 xembed_set_info (f, 0);
11161 else
11162 #endif
11165 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11166 DefaultScreen (FRAME_X_DISPLAY (f))))
11168 unblock_input ();
11169 error ("Can't notify window manager of window withdrawal");
11173 /* We can't distinguish this from iconification
11174 just by the event that we get from the server.
11175 So we can't win using the usual strategy of letting
11176 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11177 and synchronize with the server to make sure we agree. */
11178 SET_FRAME_VISIBLE (f, 0);
11179 SET_FRAME_ICONIFIED (f, false);
11181 x_sync (f);
11183 unblock_input ();
11186 /* Change window state from mapped to iconified. */
11188 void
11189 x_iconify_frame (struct frame *f)
11191 #ifdef USE_X_TOOLKIT
11192 int result;
11193 #endif
11195 /* Don't keep the highlight on an invisible frame. */
11196 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
11197 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
11199 if (FRAME_ICONIFIED_P (f))
11200 return;
11202 block_input ();
11204 x_set_bitmap_icon (f);
11206 #if defined (USE_GTK)
11207 if (FRAME_GTK_OUTER_WIDGET (f))
11209 if (! FRAME_VISIBLE_P (f))
11210 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
11212 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
11213 SET_FRAME_VISIBLE (f, 0);
11214 SET_FRAME_ICONIFIED (f, true);
11215 unblock_input ();
11216 return;
11218 #endif
11220 #ifdef USE_X_TOOLKIT
11222 if (! FRAME_VISIBLE_P (f))
11224 if (! EQ (Vx_no_window_manager, Qt))
11225 x_wm_set_window_state (f, IconicState);
11226 /* This was XtPopup, but that did nothing for an iconified frame. */
11227 XtMapWidget (f->output_data.x->widget);
11228 /* The server won't give us any event to indicate
11229 that an invisible frame was changed to an icon,
11230 so we have to record it here. */
11231 SET_FRAME_VISIBLE (f, 0);
11232 SET_FRAME_ICONIFIED (f, true);
11233 unblock_input ();
11234 return;
11237 result = XIconifyWindow (FRAME_X_DISPLAY (f),
11238 XtWindow (f->output_data.x->widget),
11239 DefaultScreen (FRAME_X_DISPLAY (f)));
11240 unblock_input ();
11242 if (!result)
11243 error ("Can't notify window manager of iconification");
11245 SET_FRAME_ICONIFIED (f, true);
11246 SET_FRAME_VISIBLE (f, 0);
11248 block_input ();
11249 XFlush (FRAME_X_DISPLAY (f));
11250 unblock_input ();
11251 #else /* not USE_X_TOOLKIT */
11253 /* Make sure the X server knows where the window should be positioned,
11254 in case the user deiconifies with the window manager. */
11255 if (! FRAME_VISIBLE_P (f)
11256 && ! FRAME_ICONIFIED_P (f)
11257 && ! FRAME_X_EMBEDDED_P (f))
11258 x_set_offset (f, f->left_pos, f->top_pos, 0);
11260 /* Since we don't know which revision of X we're running, we'll use both
11261 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11263 /* X11R4: send a ClientMessage to the window manager using the
11264 WM_CHANGE_STATE type. */
11266 XEvent msg;
11268 msg.xclient.window = FRAME_X_WINDOW (f);
11269 msg.xclient.type = ClientMessage;
11270 msg.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_wm_change_state;
11271 msg.xclient.format = 32;
11272 msg.xclient.data.l[0] = IconicState;
11274 if (! XSendEvent (FRAME_X_DISPLAY (f),
11275 DefaultRootWindow (FRAME_X_DISPLAY (f)),
11276 False,
11277 SubstructureRedirectMask | SubstructureNotifyMask,
11278 &msg))
11280 unblock_input ();
11281 error ("Can't notify window manager of iconification");
11285 /* X11R3: set the initial_state field of the window manager hints to
11286 IconicState. */
11287 x_wm_set_window_state (f, IconicState);
11289 if (!FRAME_VISIBLE_P (f))
11291 /* If the frame was withdrawn, before, we must map it. */
11292 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11295 SET_FRAME_ICONIFIED (f, true);
11296 SET_FRAME_VISIBLE (f, 0);
11298 XFlush (FRAME_X_DISPLAY (f));
11299 unblock_input ();
11300 #endif /* not USE_X_TOOLKIT */
11304 /* Free X resources of frame F. */
11306 void
11307 x_free_frame_resources (struct frame *f)
11309 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11310 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
11311 #ifdef USE_X_TOOLKIT
11312 Lisp_Object bar;
11313 struct scroll_bar *b;
11314 #endif
11316 block_input ();
11318 /* If a display connection is dead, don't try sending more
11319 commands to the X server. */
11320 if (dpyinfo->display)
11322 /* Always exit with visible pointer to avoid weird issue
11323 with Xfixes (Bug#17609). */
11324 if (f->pointer_invisible)
11325 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, 0);
11327 /* We must free faces before destroying windows because some
11328 font-driver (e.g. xft) access a window while finishing a
11329 face. */
11330 free_frame_faces (f);
11331 tear_down_x_back_buffer (f);
11333 if (f->output_data.x->icon_desc)
11334 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
11336 #ifdef USE_X_TOOLKIT
11337 /* Explicitly destroy the scroll bars of the frame. Without
11338 this, we get "BadDrawable" errors from the toolkit later on,
11339 presumably from expose events generated for the disappearing
11340 toolkit scroll bars. */
11341 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
11343 b = XSCROLL_BAR (bar);
11344 x_scroll_bar_remove (b);
11346 #endif
11348 #ifdef HAVE_X_I18N
11349 if (FRAME_XIC (f))
11350 free_frame_xic (f);
11351 #endif
11353 x_free_cr_resources (f);
11354 #ifdef USE_X_TOOLKIT
11355 if (f->output_data.x->widget)
11357 XtDestroyWidget (f->output_data.x->widget);
11358 f->output_data.x->widget = NULL;
11360 /* Tooltips don't have widgets, only a simple X window, even if
11361 we are using a toolkit. */
11362 else if (FRAME_X_WINDOW (f))
11363 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11365 free_frame_menubar (f);
11367 if (f->shell_position)
11368 xfree (f->shell_position);
11369 #else /* !USE_X_TOOLKIT */
11371 #ifdef USE_GTK
11372 xg_free_frame_widgets (f);
11373 #endif /* USE_GTK */
11375 tear_down_x_back_buffer (f);
11376 if (FRAME_X_WINDOW (f))
11377 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11378 #endif /* !USE_X_TOOLKIT */
11380 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
11381 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
11382 unload_color (f, f->output_data.x->cursor_pixel);
11383 unload_color (f, f->output_data.x->cursor_foreground_pixel);
11384 unload_color (f, f->output_data.x->border_pixel);
11385 unload_color (f, f->output_data.x->mouse_pixel);
11387 if (f->output_data.x->scroll_bar_background_pixel != -1)
11388 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
11389 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
11390 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
11391 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
11392 /* Scrollbar shadow colors. */
11393 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
11394 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
11395 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
11396 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
11397 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
11398 if (f->output_data.x->white_relief.pixel != -1)
11399 unload_color (f, f->output_data.x->white_relief.pixel);
11400 if (f->output_data.x->black_relief.pixel != -1)
11401 unload_color (f, f->output_data.x->black_relief.pixel);
11403 x_free_gcs (f);
11405 /* Free extra GCs allocated by x_setup_relief_colors. */
11406 if (f->output_data.x->white_relief.gc)
11408 XFreeGC (dpyinfo->display, f->output_data.x->white_relief.gc);
11409 f->output_data.x->white_relief.gc = 0;
11411 if (f->output_data.x->black_relief.gc)
11413 XFreeGC (dpyinfo->display, f->output_data.x->black_relief.gc);
11414 f->output_data.x->black_relief.gc = 0;
11417 /* Free cursors. */
11418 if (f->output_data.x->text_cursor != 0)
11419 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->text_cursor);
11420 if (f->output_data.x->nontext_cursor != 0)
11421 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->nontext_cursor);
11422 if (f->output_data.x->modeline_cursor != 0)
11423 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->modeline_cursor);
11424 if (f->output_data.x->hand_cursor != 0)
11425 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hand_cursor);
11426 if (f->output_data.x->hourglass_cursor != 0)
11427 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hourglass_cursor);
11428 if (f->output_data.x->horizontal_drag_cursor != 0)
11429 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->horizontal_drag_cursor);
11430 if (f->output_data.x->vertical_drag_cursor != 0)
11431 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->vertical_drag_cursor);
11433 XFlush (FRAME_X_DISPLAY (f));
11436 xfree (f->output_data.x->saved_menu_event);
11437 xfree (f->output_data.x);
11438 f->output_data.x = NULL;
11440 if (f == dpyinfo->x_focus_frame)
11441 dpyinfo->x_focus_frame = 0;
11442 if (f == dpyinfo->x_focus_event_frame)
11443 dpyinfo->x_focus_event_frame = 0;
11444 if (f == dpyinfo->x_highlight_frame)
11445 dpyinfo->x_highlight_frame = 0;
11446 if (f == hlinfo->mouse_face_mouse_frame)
11447 reset_mouse_highlight (hlinfo);
11449 unblock_input ();
11453 /* Destroy the X window of frame F. */
11455 static void
11456 x_destroy_window (struct frame *f)
11458 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11460 /* If a display connection is dead, don't try sending more
11461 commands to the X server. */
11462 if (dpyinfo->display != 0)
11463 x_free_frame_resources (f);
11465 dpyinfo->reference_count--;
11469 /* Setting window manager hints. */
11471 /* Set the normal size hints for the window manager, for frame F.
11472 FLAGS is the flags word to use--or 0 meaning preserve the flags
11473 that the window now has.
11474 If USER_POSITION, set the USPosition
11475 flag (this is useful when FLAGS is 0).
11476 The GTK version is in gtkutils.c. */
11478 #ifndef USE_GTK
11479 void
11480 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
11482 XSizeHints size_hints;
11483 Window window = FRAME_OUTER_WINDOW (f);
11485 if (!window)
11486 return;
11488 #ifdef USE_X_TOOLKIT
11489 if (f->output_data.x->widget)
11491 widget_update_wm_size_hints (f->output_data.x->widget);
11492 return;
11494 #endif
11496 /* Setting PMaxSize caused various problems. */
11497 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
11499 size_hints.x = f->left_pos;
11500 size_hints.y = f->top_pos;
11502 size_hints.width = FRAME_PIXEL_WIDTH (f);
11503 size_hints.height = FRAME_PIXEL_HEIGHT (f);
11505 size_hints.width_inc = frame_resize_pixelwise ? 1 : FRAME_COLUMN_WIDTH (f);
11506 size_hints.height_inc = frame_resize_pixelwise ? 1 : FRAME_LINE_HEIGHT (f);
11508 size_hints.max_width = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
11509 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
11510 size_hints.max_height = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
11511 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
11513 /* Calculate the base and minimum sizes. */
11515 int base_width, base_height;
11517 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
11518 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
11520 /* The window manager uses the base width hints to calculate the
11521 current number of rows and columns in the frame while
11522 resizing; min_width and min_height aren't useful for this
11523 purpose, since they might not give the dimensions for a
11524 zero-row, zero-column frame. */
11526 size_hints.flags |= PBaseSize;
11527 size_hints.base_width = base_width;
11528 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
11529 size_hints.min_width = base_width;
11530 size_hints.min_height = base_height;
11533 /* If we don't need the old flags, we don't need the old hint at all. */
11534 if (flags)
11536 size_hints.flags |= flags;
11537 goto no_read;
11541 XSizeHints hints; /* Sometimes I hate X Windows... */
11542 long supplied_return;
11543 int value;
11545 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
11546 &supplied_return);
11548 if (flags)
11549 size_hints.flags |= flags;
11550 else
11552 if (value == 0)
11553 hints.flags = 0;
11554 if (hints.flags & PSize)
11555 size_hints.flags |= PSize;
11556 if (hints.flags & PPosition)
11557 size_hints.flags |= PPosition;
11558 if (hints.flags & USPosition)
11559 size_hints.flags |= USPosition;
11560 if (hints.flags & USSize)
11561 size_hints.flags |= USSize;
11565 no_read:
11567 #ifdef PWinGravity
11568 size_hints.win_gravity = f->win_gravity;
11569 size_hints.flags |= PWinGravity;
11571 if (user_position)
11573 size_hints.flags &= ~ PPosition;
11574 size_hints.flags |= USPosition;
11576 #endif /* PWinGravity */
11578 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
11580 #endif /* not USE_GTK */
11582 /* Used for IconicState or NormalState */
11584 static void
11585 x_wm_set_window_state (struct frame *f, int state)
11587 #ifdef USE_X_TOOLKIT
11588 Arg al[1];
11590 XtSetArg (al[0], XtNinitialState, state);
11591 XtSetValues (f->output_data.x->widget, al, 1);
11592 #else /* not USE_X_TOOLKIT */
11593 Window window = FRAME_X_WINDOW (f);
11595 f->output_data.x->wm_hints.flags |= StateHint;
11596 f->output_data.x->wm_hints.initial_state = state;
11598 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
11599 #endif /* not USE_X_TOOLKIT */
11602 static void
11603 x_wm_set_icon_pixmap (struct frame *f, ptrdiff_t pixmap_id)
11605 Pixmap icon_pixmap, icon_mask;
11607 #if !defined USE_X_TOOLKIT && !defined USE_GTK
11608 Window window = FRAME_OUTER_WINDOW (f);
11609 #endif
11611 if (pixmap_id > 0)
11613 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
11614 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
11615 icon_mask = x_bitmap_mask (f, pixmap_id);
11616 f->output_data.x->wm_hints.icon_mask = icon_mask;
11618 else
11620 /* It seems there is no way to turn off use of an icon
11621 pixmap. */
11622 return;
11626 #ifdef USE_GTK
11628 xg_set_frame_icon (f, icon_pixmap, icon_mask);
11629 return;
11632 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
11635 Arg al[1];
11636 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
11637 XtSetValues (f->output_data.x->widget, al, 1);
11638 XtSetArg (al[0], XtNiconMask, icon_mask);
11639 XtSetValues (f->output_data.x->widget, al, 1);
11642 #else /* not USE_X_TOOLKIT && not USE_GTK */
11644 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
11645 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
11647 #endif /* not USE_X_TOOLKIT && not USE_GTK */
11650 void
11651 x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
11653 Window window = FRAME_OUTER_WINDOW (f);
11655 f->output_data.x->wm_hints.flags |= IconPositionHint;
11656 f->output_data.x->wm_hints.icon_x = icon_x;
11657 f->output_data.x->wm_hints.icon_y = icon_y;
11659 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
11663 /***********************************************************************
11664 Fonts
11665 ***********************************************************************/
11667 #ifdef GLYPH_DEBUG
11669 /* Check that FONT is valid on frame F. It is if it can be found in F's
11670 font table. */
11672 static void
11673 x_check_font (struct frame *f, struct font *font)
11675 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
11676 if (font->driver->check)
11677 eassert (font->driver->check (f, font) == 0);
11680 #endif /* GLYPH_DEBUG */
11683 /***********************************************************************
11684 Initialization
11685 ***********************************************************************/
11687 #ifdef USE_X_TOOLKIT
11688 static XrmOptionDescRec emacs_options[] = {
11689 {(char *) "-geometry", (char *) ".geometry", XrmoptionSepArg, NULL},
11690 {(char *) "-iconic", (char *) ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
11692 {(char *) "-internal-border-width",
11693 (char *) "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
11694 {(char *) "-ib", (char *) "*EmacsScreen.internalBorderWidth",
11695 XrmoptionSepArg, NULL},
11696 {(char *) "-T", (char *) "*EmacsShell.title", XrmoptionSepArg, NULL},
11697 {(char *) "-wn", (char *) "*EmacsShell.title", XrmoptionSepArg, NULL},
11698 {(char *) "-title", (char *) "*EmacsShell.title", XrmoptionSepArg, NULL},
11699 {(char *) "-iconname", (char *) "*EmacsShell.iconName",
11700 XrmoptionSepArg, NULL},
11701 {(char *) "-in", (char *) "*EmacsShell.iconName", XrmoptionSepArg, NULL},
11702 {(char *) "-mc", (char *) "*pointerColor", XrmoptionSepArg, NULL},
11703 {(char *) "-cr", (char *) "*cursorColor", XrmoptionSepArg, NULL}
11706 /* Whether atimer for Xt timeouts is activated or not. */
11708 static bool x_timeout_atimer_activated_flag;
11710 #endif /* USE_X_TOOLKIT */
11712 static int x_initialized;
11714 /* Test whether two display-name strings agree up to the dot that separates
11715 the screen number from the server number. */
11716 static bool
11717 same_x_server (const char *name1, const char *name2)
11719 bool seen_colon = false;
11720 Lisp_Object sysname = Fsystem_name ();
11721 const char *system_name = SSDATA (sysname);
11722 ptrdiff_t system_name_length = SBYTES (sysname);
11723 ptrdiff_t length_until_period = 0;
11725 while (system_name[length_until_period] != 0
11726 && system_name[length_until_period] != '.')
11727 length_until_period++;
11729 /* Treat `unix' like an empty host name. */
11730 if (! strncmp (name1, "unix:", 5))
11731 name1 += 4;
11732 if (! strncmp (name2, "unix:", 5))
11733 name2 += 4;
11734 /* Treat this host's name like an empty host name. */
11735 if (! strncmp (name1, system_name, system_name_length)
11736 && name1[system_name_length] == ':')
11737 name1 += system_name_length;
11738 if (! strncmp (name2, system_name, system_name_length)
11739 && name2[system_name_length] == ':')
11740 name2 += system_name_length;
11741 /* Treat this host's domainless name like an empty host name. */
11742 if (! strncmp (name1, system_name, length_until_period)
11743 && name1[length_until_period] == ':')
11744 name1 += length_until_period;
11745 if (! strncmp (name2, system_name, length_until_period)
11746 && name2[length_until_period] == ':')
11747 name2 += length_until_period;
11749 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
11751 if (*name1 == ':')
11752 seen_colon = true;
11753 if (seen_colon && *name1 == '.')
11754 return true;
11756 return (seen_colon
11757 && (*name1 == '.' || *name1 == '\0')
11758 && (*name2 == '.' || *name2 == '\0'));
11761 /* Count number of set bits in mask and number of bits to shift to
11762 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
11763 to 5. */
11764 static void
11765 get_bits_and_offset (unsigned long mask, int *bits, int *offset)
11767 int nr = 0;
11768 int off = 0;
11770 while (!(mask & 1))
11772 off++;
11773 mask >>= 1;
11776 while (mask & 1)
11778 nr++;
11779 mask >>= 1;
11782 *offset = off;
11783 *bits = nr;
11786 /* Return true iff display DISPLAY is available for use.
11787 But don't permanently open it, just test its availability. */
11789 bool
11790 x_display_ok (const char *display)
11792 /* XOpenDisplay fails if it gets a signal. Block SIGIO which may arrive. */
11793 unrequest_sigio ();
11794 Display *dpy = XOpenDisplay (display);
11795 request_sigio ();
11796 if (!dpy)
11797 return false;
11798 XCloseDisplay (dpy);
11799 return true;
11802 #ifdef USE_GTK
11803 static void
11804 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
11805 const gchar *msg, gpointer user_data)
11807 if (!strstr (msg, "g_set_prgname"))
11808 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, msg);
11810 #endif
11812 /* Create invisible cursor on X display referred by DPYINFO. */
11814 static Cursor
11815 make_invisible_cursor (struct x_display_info *dpyinfo)
11817 Display *dpy = dpyinfo->display;
11818 static char const no_data[] = { 0 };
11819 Pixmap pix;
11820 XColor col;
11821 Cursor c = 0;
11823 x_catch_errors (dpy);
11824 pix = XCreateBitmapFromData (dpy, dpyinfo->root_window, no_data, 1, 1);
11825 if (! x_had_errors_p (dpy) && pix != None)
11827 Cursor pixc;
11828 col.pixel = 0;
11829 col.red = col.green = col.blue = 0;
11830 col.flags = DoRed | DoGreen | DoBlue;
11831 pixc = XCreatePixmapCursor (dpy, pix, pix, &col, &col, 0, 0);
11832 if (! x_had_errors_p (dpy) && pixc != None)
11833 c = pixc;
11834 XFreePixmap (dpy, pix);
11837 x_uncatch_errors ();
11839 return c;
11842 /* True if DPY supports Xfixes extension >= 4. */
11844 static bool
11845 x_probe_xfixes_extension (Display *dpy)
11847 #ifdef HAVE_XFIXES
11848 int major, minor;
11849 return XFixesQueryVersion (dpy, &major, &minor) && major >= 4;
11850 #else
11851 return false;
11852 #endif /* HAVE_XFIXES */
11855 /* Toggle mouse pointer visibility on frame F by using Xfixes functions. */
11857 static void
11858 xfixes_toggle_visible_pointer (struct frame *f, bool invisible)
11860 #ifdef HAVE_XFIXES
11861 if (invisible)
11862 XFixesHideCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11863 else
11864 XFixesShowCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11865 f->pointer_invisible = invisible;
11866 #else
11867 emacs_abort ();
11868 #endif /* HAVE_XFIXES */
11871 /* Toggle mouse pointer visibility on frame F by using invisible cursor. */
11873 static void
11874 x_toggle_visible_pointer (struct frame *f, bool invisible)
11876 eassert (FRAME_DISPLAY_INFO (f)->invisible_cursor != 0);
11877 if (invisible)
11878 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11879 FRAME_DISPLAY_INFO (f)->invisible_cursor);
11880 else
11881 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11882 f->output_data.x->current_cursor);
11883 f->pointer_invisible = invisible;
11886 /* Setup pointer blanking, prefer Xfixes if available. */
11888 static void
11889 x_setup_pointer_blanking (struct x_display_info *dpyinfo)
11891 /* FIXME: the brave tester should set EMACS_XFIXES because we're suspecting
11892 X server bug, see http://debbugs.gnu.org/cgi/bugreport.cgi?bug=17609. */
11893 if (egetenv ("EMACS_XFIXES") && x_probe_xfixes_extension (dpyinfo->display))
11894 dpyinfo->toggle_visible_pointer = xfixes_toggle_visible_pointer;
11895 else
11897 dpyinfo->toggle_visible_pointer = x_toggle_visible_pointer;
11898 dpyinfo->invisible_cursor = make_invisible_cursor (dpyinfo);
11902 /* Current X display connection identifier. Incremented for each next
11903 connection established. */
11904 static unsigned x_display_id;
11906 /* Open a connection to X display DISPLAY_NAME, and return
11907 the structure that describes the open display.
11908 If we cannot contact the display, return null. */
11910 struct x_display_info *
11911 x_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
11913 Display *dpy;
11914 struct terminal *terminal;
11915 struct x_display_info *dpyinfo;
11916 XrmDatabase xrdb;
11917 #ifdef USE_XCB
11918 xcb_connection_t *xcb_conn;
11919 #endif
11921 block_input ();
11923 if (!x_initialized)
11925 x_initialize ();
11926 ++x_initialized;
11929 if (! x_display_ok (SSDATA (display_name)))
11930 error ("Display %s can't be opened", SSDATA (display_name));
11932 #ifdef USE_GTK
11934 #define NUM_ARGV 10
11935 int argc;
11936 char *argv[NUM_ARGV];
11937 char **argv2 = argv;
11938 guint id;
11940 if (x_initialized++ > 1)
11942 xg_display_open (SSDATA (display_name), &dpy);
11944 else
11946 static char display_opt[] = "--display";
11947 static char name_opt[] = "--name";
11949 for (argc = 0; argc < NUM_ARGV; ++argc)
11950 argv[argc] = 0;
11952 argc = 0;
11953 argv[argc++] = initial_argv[0];
11955 if (! NILP (display_name))
11957 argv[argc++] = display_opt;
11958 argv[argc++] = SSDATA (display_name);
11961 argv[argc++] = name_opt;
11962 argv[argc++] = resource_name;
11964 XSetLocaleModifiers ("");
11966 /* Work around GLib bug that outputs a faulty warning. See
11967 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
11968 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
11969 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
11971 /* NULL window -> events for all windows go to our function.
11972 Call before gtk_init so Gtk+ event filters comes after our. */
11973 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
11975 /* gtk_init does set_locale. Fix locale before and after. */
11976 fixup_locale ();
11977 unrequest_sigio (); /* See comment in x_display_ok. */
11978 gtk_init (&argc, &argv2);
11979 request_sigio ();
11980 fixup_locale ();
11982 g_log_remove_handler ("GLib", id);
11984 xg_initialize ();
11986 dpy = DEFAULT_GDK_DISPLAY ();
11988 #if ! GTK_CHECK_VERSION (2, 90, 0)
11989 /* Load our own gtkrc if it exists. */
11991 const char *file = "~/.emacs.d/gtkrc";
11992 Lisp_Object s, abs_file;
11994 s = build_string (file);
11995 abs_file = Fexpand_file_name (s, Qnil);
11997 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
11998 gtk_rc_parse (SSDATA (abs_file));
12000 #endif
12002 XSetErrorHandler (x_error_handler);
12003 XSetIOErrorHandler (x_io_error_quitter);
12006 #else /* not USE_GTK */
12007 #ifdef USE_X_TOOLKIT
12008 /* weiner@footloose.sps.mot.com reports that this causes
12009 errors with X11R5:
12010 X protocol error: BadAtom (invalid Atom parameter)
12011 on protocol request 18skiloaf.
12012 So let's not use it until R6. */
12013 #ifdef HAVE_X11XTR6
12014 XtSetLanguageProc (NULL, NULL, NULL);
12015 #endif
12018 int argc = 0;
12019 char *argv[3];
12021 argv[0] = (char *) "";
12022 argc = 1;
12023 if (xrm_option)
12025 argv[argc++] = (char *) "-xrm";
12026 argv[argc++] = xrm_option;
12028 turn_on_atimers (false);
12029 unrequest_sigio (); /* See comment in x_display_ok. */
12030 dpy = XtOpenDisplay (Xt_app_con, SSDATA (display_name),
12031 resource_name, EMACS_CLASS,
12032 emacs_options, XtNumber (emacs_options),
12033 &argc, argv);
12034 request_sigio ();
12035 turn_on_atimers (true);
12037 #ifdef HAVE_X11XTR6
12038 /* I think this is to compensate for XtSetLanguageProc. */
12039 fixup_locale ();
12040 #endif
12043 #else /* not USE_X_TOOLKIT */
12044 XSetLocaleModifiers ("");
12045 unrequest_sigio (); /* See comment in x_display_ok. */
12046 dpy = XOpenDisplay (SSDATA (display_name));
12047 request_sigio ();
12048 #endif /* not USE_X_TOOLKIT */
12049 #endif /* not USE_GTK*/
12051 /* Detect failure. */
12052 if (dpy == 0)
12054 unblock_input ();
12055 return 0;
12058 #ifdef USE_XCB
12059 xcb_conn = XGetXCBConnection (dpy);
12060 if (xcb_conn == 0)
12062 #ifdef USE_GTK
12063 xg_display_close (dpy);
12064 #else
12065 #ifdef USE_X_TOOLKIT
12066 XtCloseDisplay (dpy);
12067 #else
12068 XCloseDisplay (dpy);
12069 #endif
12070 #endif /* ! USE_GTK */
12072 unblock_input ();
12073 return 0;
12075 #endif
12077 /* We have definitely succeeded. Record the new connection. */
12079 dpyinfo = xzalloc (sizeof *dpyinfo);
12080 terminal = x_create_terminal (dpyinfo);
12083 struct x_display_info *share;
12085 for (share = x_display_list; share; share = share->next)
12086 if (same_x_server (SSDATA (XCAR (share->name_list_element)),
12087 SSDATA (display_name)))
12088 break;
12089 if (share)
12090 terminal->kboard = share->terminal->kboard;
12091 else
12093 terminal->kboard = allocate_kboard (Qx);
12095 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
12097 char *vendor = ServerVendor (dpy);
12099 /* Temporarily hide the partially initialized terminal. */
12100 terminal_list = terminal->next_terminal;
12101 unblock_input ();
12102 kset_system_key_alist
12103 (terminal->kboard,
12104 call1 (Qvendor_specific_keysyms,
12105 vendor ? build_string (vendor) : empty_unibyte_string));
12106 block_input ();
12107 terminal->next_terminal = terminal_list;
12108 terminal_list = terminal;
12111 /* Don't let the initial kboard remain current longer than necessary.
12112 That would cause problems if a file loaded on startup tries to
12113 prompt in the mini-buffer. */
12114 if (current_kboard == initial_kboard)
12115 current_kboard = terminal->kboard;
12117 terminal->kboard->reference_count++;
12120 /* Put this display on the chain. */
12121 dpyinfo->next = x_display_list;
12122 x_display_list = dpyinfo;
12124 dpyinfo->name_list_element = Fcons (display_name, Qnil);
12125 dpyinfo->display = dpy;
12126 dpyinfo->connection = ConnectionNumber (dpyinfo->display);
12127 #ifdef USE_XCB
12128 dpyinfo->xcb_connection = xcb_conn;
12129 #endif
12131 /* http://lists.gnu.org/archive/html/emacs-devel/2015-11/msg00194.html */
12132 dpyinfo->smallest_font_height = 1;
12133 dpyinfo->smallest_char_width = 1;
12135 /* Set the name of the terminal. */
12136 terminal->name = xlispstrdup (display_name);
12138 #if false
12139 XSetAfterFunction (x_current_display, x_trace_wire);
12140 #endif
12142 Lisp_Object system_name = Fsystem_name ();
12143 ptrdiff_t nbytes;
12144 if (INT_ADD_WRAPV (SBYTES (Vinvocation_name), SBYTES (system_name) + 2,
12145 &nbytes))
12146 memory_full (SIZE_MAX);
12147 dpyinfo->x_id = ++x_display_id;
12148 dpyinfo->x_id_name = xmalloc (nbytes);
12149 char *nametail = lispstpcpy (dpyinfo->x_id_name, Vinvocation_name);
12150 *nametail++ = '@';
12151 lispstpcpy (nametail, system_name);
12153 /* Figure out which modifier bits mean what. */
12154 x_find_modifier_meanings (dpyinfo);
12156 /* Get the scroll bar cursor. */
12157 #ifdef USE_GTK
12158 /* We must create a GTK cursor, it is required for GTK widgets. */
12159 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
12160 #endif /* USE_GTK */
12162 dpyinfo->vertical_scroll_bar_cursor
12163 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
12165 dpyinfo->horizontal_scroll_bar_cursor
12166 = XCreateFontCursor (dpyinfo->display, XC_sb_h_double_arrow);
12168 xrdb = x_load_resources (dpyinfo->display, xrm_option,
12169 resource_name, EMACS_CLASS);
12170 #ifdef HAVE_XRMSETDATABASE
12171 XrmSetDatabase (dpyinfo->display, xrdb);
12172 #else
12173 dpyinfo->display->db = xrdb;
12174 #endif
12175 /* Put the rdb where we can find it in a way that works on
12176 all versions. */
12177 dpyinfo->xrdb = xrdb;
12179 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
12180 DefaultScreen (dpyinfo->display));
12181 select_visual (dpyinfo);
12182 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
12183 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
12184 dpyinfo->icon_bitmap_id = -1;
12185 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
12187 reset_mouse_highlight (&dpyinfo->mouse_highlight);
12189 /* See if we can construct pixel values from RGB values. */
12190 if (dpyinfo->visual->class == TrueColor)
12192 get_bits_and_offset (dpyinfo->visual->red_mask,
12193 &dpyinfo->red_bits, &dpyinfo->red_offset);
12194 get_bits_and_offset (dpyinfo->visual->blue_mask,
12195 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
12196 get_bits_and_offset (dpyinfo->visual->green_mask,
12197 &dpyinfo->green_bits, &dpyinfo->green_offset);
12200 /* See if a private colormap is requested. */
12201 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
12203 if (dpyinfo->visual->class == PseudoColor)
12205 AUTO_STRING (privateColormap, "privateColormap");
12206 AUTO_STRING (PrivateColormap, "PrivateColormap");
12207 Lisp_Object value
12208 = display_x_get_resource (dpyinfo, privateColormap,
12209 PrivateColormap, Qnil, Qnil);
12210 if (STRINGP (value)
12211 && (!strcmp (SSDATA (value), "true")
12212 || !strcmp (SSDATA (value), "on")))
12213 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
12216 else
12217 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
12218 dpyinfo->visual, AllocNone);
12220 #ifdef HAVE_XDBE
12221 dpyinfo->supports_xdbe = false;
12222 int xdbe_major;
12223 int xdbe_minor;
12224 if (XdbeQueryExtension (dpyinfo->display, &xdbe_major, &xdbe_minor))
12225 dpyinfo->supports_xdbe = true;
12226 #endif
12228 #ifdef HAVE_XFT
12230 /* If we are using Xft, the following precautions should be made:
12232 1. Make sure that the Xrender extension is added before the Xft one.
12233 Otherwise, the close-display hook set by Xft is called after the one
12234 for Xrender, and the former tries to re-add the latter. This results
12235 in inconsistency of internal states and leads to X protocol error when
12236 one reconnects to the same X server (Bug#1696).
12238 2. Check dpi value in X resources. It is better we use it as well,
12239 since Xft will use it, as will all Gnome applications. If our real DPI
12240 is smaller or larger than the one Xft uses, our font will look smaller
12241 or larger than other for other applications, even if it is the same
12242 font name (monospace-10 for example). */
12244 int event_base, error_base;
12245 char *v;
12246 double d;
12248 XRenderQueryExtension (dpyinfo->display, &event_base, &error_base);
12250 v = XGetDefault (dpyinfo->display, "Xft", "dpi");
12251 if (v != NULL && sscanf (v, "%lf", &d) == 1)
12252 dpyinfo->resy = dpyinfo->resx = d;
12254 #endif
12256 if (dpyinfo->resy < 1)
12258 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
12259 double pixels = DisplayHeight (dpyinfo->display, screen_number);
12260 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
12261 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
12262 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
12263 pixels = DisplayWidth (dpyinfo->display, screen_number);
12264 mm = DisplayWidthMM (dpyinfo->display, screen_number);
12265 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
12266 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
12270 static const struct
12272 const char *name;
12273 int offset;
12274 } atom_refs[] = {
12275 #define ATOM_REFS_INIT(string, member) \
12276 { string, offsetof (struct x_display_info, member) },
12277 ATOM_REFS_INIT ("WM_PROTOCOLS", Xatom_wm_protocols)
12278 ATOM_REFS_INIT ("WM_TAKE_FOCUS", Xatom_wm_take_focus)
12279 ATOM_REFS_INIT ("WM_SAVE_YOURSELF", Xatom_wm_save_yourself)
12280 ATOM_REFS_INIT ("WM_DELETE_WINDOW", Xatom_wm_delete_window)
12281 ATOM_REFS_INIT ("WM_CHANGE_STATE", Xatom_wm_change_state)
12282 ATOM_REFS_INIT ("WM_CONFIGURE_DENIED", Xatom_wm_configure_denied)
12283 ATOM_REFS_INIT ("WM_MOVED", Xatom_wm_window_moved)
12284 ATOM_REFS_INIT ("WM_CLIENT_LEADER", Xatom_wm_client_leader)
12285 ATOM_REFS_INIT ("Editres", Xatom_editres)
12286 ATOM_REFS_INIT ("CLIPBOARD", Xatom_CLIPBOARD)
12287 ATOM_REFS_INIT ("TIMESTAMP", Xatom_TIMESTAMP)
12288 ATOM_REFS_INIT ("TEXT", Xatom_TEXT)
12289 ATOM_REFS_INIT ("COMPOUND_TEXT", Xatom_COMPOUND_TEXT)
12290 ATOM_REFS_INIT ("UTF8_STRING", Xatom_UTF8_STRING)
12291 ATOM_REFS_INIT ("DELETE", Xatom_DELETE)
12292 ATOM_REFS_INIT ("MULTIPLE", Xatom_MULTIPLE)
12293 ATOM_REFS_INIT ("INCR", Xatom_INCR)
12294 ATOM_REFS_INIT ("_EMACS_TMP_", Xatom_EMACS_TMP)
12295 ATOM_REFS_INIT ("TARGETS", Xatom_TARGETS)
12296 ATOM_REFS_INIT ("NULL", Xatom_NULL)
12297 ATOM_REFS_INIT ("ATOM", Xatom_ATOM)
12298 ATOM_REFS_INIT ("ATOM_PAIR", Xatom_ATOM_PAIR)
12299 ATOM_REFS_INIT ("CLIPBOARD_MANAGER", Xatom_CLIPBOARD_MANAGER)
12300 ATOM_REFS_INIT ("_XEMBED_INFO", Xatom_XEMBED_INFO)
12301 /* For properties of font. */
12302 ATOM_REFS_INIT ("PIXEL_SIZE", Xatom_PIXEL_SIZE)
12303 ATOM_REFS_INIT ("AVERAGE_WIDTH", Xatom_AVERAGE_WIDTH)
12304 ATOM_REFS_INIT ("_MULE_BASELINE_OFFSET", Xatom_MULE_BASELINE_OFFSET)
12305 ATOM_REFS_INIT ("_MULE_RELATIVE_COMPOSE", Xatom_MULE_RELATIVE_COMPOSE)
12306 ATOM_REFS_INIT ("_MULE_DEFAULT_ASCENT", Xatom_MULE_DEFAULT_ASCENT)
12307 /* Ghostscript support. */
12308 ATOM_REFS_INIT ("DONE", Xatom_DONE)
12309 ATOM_REFS_INIT ("PAGE", Xatom_PAGE)
12310 ATOM_REFS_INIT ("SCROLLBAR", Xatom_Scrollbar)
12311 ATOM_REFS_INIT ("HORIZONTAL_SCROLLBAR", Xatom_Horizontal_Scrollbar)
12312 ATOM_REFS_INIT ("_XEMBED", Xatom_XEMBED)
12313 /* EWMH */
12314 ATOM_REFS_INIT ("_NET_WM_STATE", Xatom_net_wm_state)
12315 ATOM_REFS_INIT ("_NET_WM_STATE_FULLSCREEN", Xatom_net_wm_state_fullscreen)
12316 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_HORZ",
12317 Xatom_net_wm_state_maximized_horz)
12318 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_VERT",
12319 Xatom_net_wm_state_maximized_vert)
12320 ATOM_REFS_INIT ("_NET_WM_STATE_STICKY", Xatom_net_wm_state_sticky)
12321 ATOM_REFS_INIT ("_NET_WM_STATE_HIDDEN", Xatom_net_wm_state_hidden)
12322 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE", Xatom_net_window_type)
12323 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE_TOOLTIP",
12324 Xatom_net_window_type_tooltip)
12325 ATOM_REFS_INIT ("_NET_WM_ICON_NAME", Xatom_net_wm_icon_name)
12326 ATOM_REFS_INIT ("_NET_WM_NAME", Xatom_net_wm_name)
12327 ATOM_REFS_INIT ("_NET_SUPPORTED", Xatom_net_supported)
12328 ATOM_REFS_INIT ("_NET_SUPPORTING_WM_CHECK", Xatom_net_supporting_wm_check)
12329 ATOM_REFS_INIT ("_NET_WM_WINDOW_OPACITY", Xatom_net_wm_window_opacity)
12330 ATOM_REFS_INIT ("_NET_ACTIVE_WINDOW", Xatom_net_active_window)
12331 ATOM_REFS_INIT ("_NET_FRAME_EXTENTS", Xatom_net_frame_extents)
12332 ATOM_REFS_INIT ("_NET_CURRENT_DESKTOP", Xatom_net_current_desktop)
12333 ATOM_REFS_INIT ("_NET_WORKAREA", Xatom_net_workarea)
12334 /* Session management */
12335 ATOM_REFS_INIT ("SM_CLIENT_ID", Xatom_SM_CLIENT_ID)
12336 ATOM_REFS_INIT ("_XSETTINGS_SETTINGS", Xatom_xsettings_prop)
12337 ATOM_REFS_INIT ("MANAGER", Xatom_xsettings_mgr)
12340 int i;
12341 enum { atom_count = ARRAYELTS (atom_refs) };
12342 /* 1 for _XSETTINGS_SN. */
12343 enum { total_atom_count = 1 + atom_count };
12344 Atom atoms_return[total_atom_count];
12345 char *atom_names[total_atom_count];
12346 static char const xsettings_fmt[] = "_XSETTINGS_S%d";
12347 char xsettings_atom_name[sizeof xsettings_fmt - 2
12348 + INT_STRLEN_BOUND (int)];
12350 for (i = 0; i < atom_count; i++)
12351 atom_names[i] = (char *) atom_refs[i].name;
12353 /* Build _XSETTINGS_SN atom name. */
12354 sprintf (xsettings_atom_name, xsettings_fmt,
12355 XScreenNumberOfScreen (dpyinfo->screen));
12356 atom_names[i] = xsettings_atom_name;
12358 XInternAtoms (dpyinfo->display, atom_names, total_atom_count,
12359 False, atoms_return);
12361 for (i = 0; i < atom_count; i++)
12362 *(Atom *) ((char *) dpyinfo + atom_refs[i].offset) = atoms_return[i];
12364 /* Manually copy last atom. */
12365 dpyinfo->Xatom_xsettings_sel = atoms_return[i];
12368 dpyinfo->x_dnd_atoms_size = 8;
12369 dpyinfo->x_dnd_atoms = xmalloc (sizeof *dpyinfo->x_dnd_atoms
12370 * dpyinfo->x_dnd_atoms_size);
12371 dpyinfo->gray
12372 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12373 gray_bits, gray_width, gray_height,
12374 1, 0, 1);
12376 x_setup_pointer_blanking (dpyinfo);
12378 #ifdef HAVE_X_I18N
12379 xim_initialize (dpyinfo, resource_name);
12380 #endif
12382 xsettings_initialize (dpyinfo);
12384 /* This is only needed for distinguishing keyboard and process input. */
12385 if (dpyinfo->connection != 0)
12386 add_keyboard_wait_descriptor (dpyinfo->connection);
12388 #ifdef F_SETOWN
12389 fcntl (dpyinfo->connection, F_SETOWN, getpid ());
12390 #endif /* ! defined (F_SETOWN) */
12392 if (interrupt_input)
12393 init_sigio (dpyinfo->connection);
12395 #ifdef USE_LUCID
12397 XrmValue d, fr, to;
12398 Font font;
12400 dpy = dpyinfo->display;
12401 d.addr = (XPointer)&dpy;
12402 d.size = sizeof (Display *);
12403 fr.addr = (char *) XtDefaultFont;
12404 fr.size = sizeof (XtDefaultFont);
12405 to.size = sizeof (Font *);
12406 to.addr = (XPointer)&font;
12407 x_catch_errors (dpy);
12408 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
12409 emacs_abort ();
12410 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
12411 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
12412 /* Do not free XFontStruct returned by the above call to XQueryFont.
12413 This leads to X protocol errors at XtCloseDisplay (Bug#18403). */
12414 x_uncatch_errors ();
12416 #endif
12418 /* See if we should run in synchronous mode. This is useful
12419 for debugging X code. */
12421 AUTO_STRING (synchronous, "synchronous");
12422 AUTO_STRING (Synchronous, "Synchronous");
12423 Lisp_Object value = display_x_get_resource (dpyinfo, synchronous,
12424 Synchronous, Qnil, Qnil);
12425 if (STRINGP (value)
12426 && (!strcmp (SSDATA (value), "true")
12427 || !strcmp (SSDATA (value), "on")))
12428 XSynchronize (dpyinfo->display, True);
12432 AUTO_STRING (useXIM, "useXIM");
12433 AUTO_STRING (UseXIM, "UseXIM");
12434 Lisp_Object value = display_x_get_resource (dpyinfo, useXIM, UseXIM,
12435 Qnil, Qnil);
12436 #ifdef USE_XIM
12437 if (STRINGP (value)
12438 && (!strcmp (SSDATA (value), "false")
12439 || !strcmp (SSDATA (value), "off")))
12440 use_xim = false;
12441 #else
12442 if (STRINGP (value)
12443 && (!strcmp (SSDATA (value), "true")
12444 || !strcmp (SSDATA (value), "on")))
12445 use_xim = true;
12446 #endif
12449 #ifdef HAVE_X_SM
12450 /* Only do this for the very first display in the Emacs session.
12451 Ignore X session management when Emacs was first started on a
12452 tty or started as a daemon. */
12453 if (terminal->id == 1 && ! IS_DAEMON)
12454 x_session_initialize (dpyinfo);
12455 #endif
12457 #ifdef USE_CAIRO
12458 x_extension_initialize (dpyinfo);
12459 #endif
12461 unblock_input ();
12463 return dpyinfo;
12466 /* Get rid of display DPYINFO, deleting all frames on it,
12467 and without sending any more commands to the X server. */
12469 static void
12470 x_delete_display (struct x_display_info *dpyinfo)
12472 struct terminal *t;
12473 struct color_name_cache_entry *color_entry, *next_color_entry;
12475 /* Close all frames and delete the generic struct terminal for this
12476 X display. */
12477 for (t = terminal_list; t; t = t->next_terminal)
12478 if (t->type == output_x_window && t->display_info.x == dpyinfo)
12480 #ifdef HAVE_X_SM
12481 /* Close X session management when we close its display. */
12482 if (t->id == 1 && x_session_have_connection ())
12483 x_session_close ();
12484 #endif
12485 delete_terminal (t);
12486 break;
12489 if (next_noop_dpyinfo == dpyinfo)
12490 next_noop_dpyinfo = dpyinfo->next;
12492 if (x_display_list == dpyinfo)
12493 x_display_list = dpyinfo->next;
12494 else
12496 struct x_display_info *tail;
12498 for (tail = x_display_list; tail; tail = tail->next)
12499 if (tail->next == dpyinfo)
12500 tail->next = tail->next->next;
12503 for (color_entry = dpyinfo->color_names;
12504 color_entry;
12505 color_entry = next_color_entry)
12507 next_color_entry = color_entry->next;
12508 xfree (color_entry->name);
12509 xfree (color_entry);
12512 xfree (dpyinfo->x_id_name);
12513 xfree (dpyinfo->x_dnd_atoms);
12514 xfree (dpyinfo->color_cells);
12515 xfree (dpyinfo);
12518 #ifdef USE_X_TOOLKIT
12520 /* Atimer callback function for TIMER. Called every 0.1s to process
12521 Xt timeouts, if needed. We must avoid calling XtAppPending as
12522 much as possible because that function does an implicit XFlush
12523 that slows us down. */
12525 static void
12526 x_process_timeouts (struct atimer *timer)
12528 block_input ();
12529 x_timeout_atimer_activated_flag = false;
12530 if (toolkit_scroll_bar_interaction || popup_activated ())
12532 while (XtAppPending (Xt_app_con) & XtIMTimer)
12533 XtAppProcessEvent (Xt_app_con, XtIMTimer);
12534 /* Reactivate the atimer for next time. */
12535 x_activate_timeout_atimer ();
12537 unblock_input ();
12540 /* Install an asynchronous timer that processes Xt timeout events
12541 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
12542 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
12543 function whenever these variables are set. This is necessary
12544 because some widget sets use timeouts internally, for example the
12545 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
12546 processed, these widgets don't behave normally. */
12548 void
12549 x_activate_timeout_atimer (void)
12551 block_input ();
12552 if (!x_timeout_atimer_activated_flag)
12554 struct timespec interval = make_timespec (0, 100 * 1000 * 1000);
12555 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
12556 x_timeout_atimer_activated_flag = true;
12558 unblock_input ();
12561 #endif /* USE_X_TOOLKIT */
12564 /* Set up use of X before we make the first connection. */
12566 static struct redisplay_interface x_redisplay_interface =
12568 x_frame_parm_handlers,
12569 x_produce_glyphs,
12570 x_write_glyphs,
12571 x_insert_glyphs,
12572 x_clear_end_of_line,
12573 x_scroll_run,
12574 x_after_update_window_line,
12575 x_update_window_begin,
12576 x_update_window_end,
12577 x_flip_and_flush,
12578 x_clear_window_mouse_face,
12579 x_get_glyph_overhangs,
12580 x_fix_overlapping_area,
12581 x_draw_fringe_bitmap,
12582 #ifdef USE_CAIRO
12583 x_cr_define_fringe_bitmap,
12584 x_cr_destroy_fringe_bitmap,
12585 #else
12586 0, /* define_fringe_bitmap */
12587 0, /* destroy_fringe_bitmap */
12588 #endif
12589 x_compute_glyph_string_overhangs,
12590 x_draw_glyph_string,
12591 x_define_frame_cursor,
12592 x_clear_frame_area,
12593 x_draw_window_cursor,
12594 x_draw_vertical_window_border,
12595 x_draw_window_divider,
12596 x_shift_glyphs_for_insert, /* Never called; see comment in function. */
12597 x_show_hourglass,
12598 x_hide_hourglass
12602 /* This function is called when the last frame on a display is deleted. */
12603 void
12604 x_delete_terminal (struct terminal *terminal)
12606 struct x_display_info *dpyinfo = terminal->display_info.x;
12608 /* Protect against recursive calls. delete_frame in
12609 delete_terminal calls us back when it deletes our last frame. */
12610 if (!terminal->name)
12611 return;
12613 block_input ();
12614 #ifdef HAVE_X_I18N
12615 /* We must close our connection to the XIM server before closing the
12616 X display. */
12617 if (dpyinfo->xim)
12618 xim_close_dpy (dpyinfo);
12619 #endif
12621 /* Normally, the display is available... */
12622 if (dpyinfo->display)
12624 x_destroy_all_bitmaps (dpyinfo);
12625 XSetCloseDownMode (dpyinfo->display, DestroyAll);
12627 /* Whether or not XCloseDisplay destroys the associated resource
12628 database depends on the version of libX11. To avoid both
12629 crash and memory leak, we dissociate the database from the
12630 display and then destroy dpyinfo->xrdb ourselves.
12632 Unfortunately, the above strategy does not work in some
12633 situations due to a bug in newer versions of libX11: because
12634 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
12635 dpy->db is NULL, XCloseDisplay destroys the associated
12636 database whereas it has not been created by XGetDefault
12637 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
12638 don't destroy the database here in order to avoid the crash
12639 in the above situations for now, though that may cause memory
12640 leaks in other situations. */
12641 #if false
12642 #ifdef HAVE_XRMSETDATABASE
12643 XrmSetDatabase (dpyinfo->display, NULL);
12644 #else
12645 dpyinfo->display->db = NULL;
12646 #endif
12647 /* We used to call XrmDestroyDatabase from x_delete_display, but
12648 some older versions of libX11 crash if we call it after
12649 closing all the displays. */
12650 XrmDestroyDatabase (dpyinfo->xrdb);
12651 #endif
12653 #ifdef USE_GTK
12654 xg_display_close (dpyinfo->display);
12655 #else
12656 #ifdef USE_X_TOOLKIT
12657 XtCloseDisplay (dpyinfo->display);
12658 #else
12659 XCloseDisplay (dpyinfo->display);
12660 #endif
12661 #endif /* ! USE_GTK */
12662 /* Do not close the connection here because it's already closed
12663 by X(t)CloseDisplay (Bug#18403). */
12664 dpyinfo->display = NULL;
12667 /* ...but if called from x_connection_closed, the display may already
12668 be closed and dpyinfo->display was set to 0 to indicate that. Since
12669 X server is most likely gone, explicit close is the only reliable
12670 way to continue and avoid Bug#19147. */
12671 else if (dpyinfo->connection >= 0)
12672 emacs_close (dpyinfo->connection);
12674 /* No more input on this descriptor. */
12675 delete_keyboard_wait_descriptor (dpyinfo->connection);
12676 /* Mark as dead. */
12677 dpyinfo->connection = -1;
12679 x_delete_display (dpyinfo);
12680 unblock_input ();
12683 /* Create a struct terminal, initialize it with the X11 specific
12684 functions and make DISPLAY->TERMINAL point to it. */
12686 static struct terminal *
12687 x_create_terminal (struct x_display_info *dpyinfo)
12689 struct terminal *terminal;
12691 terminal = create_terminal (output_x_window, &x_redisplay_interface);
12693 terminal->display_info.x = dpyinfo;
12694 dpyinfo->terminal = terminal;
12696 /* kboard is initialized in x_term_init. */
12698 terminal->clear_frame_hook = x_clear_frame;
12699 terminal->ins_del_lines_hook = x_ins_del_lines;
12700 terminal->delete_glyphs_hook = x_delete_glyphs;
12701 terminal->ring_bell_hook = XTring_bell;
12702 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
12703 terminal->update_begin_hook = x_update_begin;
12704 terminal->update_end_hook = x_update_end;
12705 terminal->read_socket_hook = XTread_socket;
12706 terminal->frame_up_to_date_hook = XTframe_up_to_date;
12707 terminal->buffer_flipping_unblocked_hook = XTbuffer_flipping_unblocked_hook;
12708 terminal->mouse_position_hook = XTmouse_position;
12709 terminal->frame_rehighlight_hook = XTframe_rehighlight;
12710 terminal->frame_raise_lower_hook = XTframe_raise_lower;
12711 terminal->fullscreen_hook = XTfullscreen_hook;
12712 terminal->menu_show_hook = x_menu_show;
12713 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
12714 terminal->popup_dialog_hook = xw_popup_dialog;
12715 #endif
12716 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
12717 terminal->set_horizontal_scroll_bar_hook = XTset_horizontal_scroll_bar;
12718 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
12719 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
12720 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
12721 terminal->delete_frame_hook = x_destroy_window;
12722 terminal->delete_terminal_hook = x_delete_terminal;
12723 /* Other hooks are NULL by default. */
12725 return terminal;
12728 static void
12729 x_initialize (void)
12731 baud_rate = 19200;
12733 x_noop_count = 0;
12734 any_help_event_p = false;
12735 ignore_next_mouse_click_timeout = 0;
12737 #ifdef USE_GTK
12738 current_count = -1;
12739 #endif
12741 /* Try to use interrupt input; if we can't, then start polling. */
12742 Fset_input_interrupt_mode (Qt);
12744 #if THREADS_ENABLED
12745 /* This must be called before any other Xlib routines. */
12746 if (XInitThreads () == 0)
12747 fprintf (stderr,
12748 "Warning: An error occurred initializing X11 thread support!\n");
12749 #endif
12751 #ifdef USE_X_TOOLKIT
12752 XtToolkitInitialize ();
12754 Xt_app_con = XtCreateApplicationContext ();
12756 /* Register a converter from strings to pixels, which uses
12757 Emacs' color allocation infrastructure. */
12758 XtAppSetTypeConverter (Xt_app_con,
12759 XtRString, XtRPixel, cvt_string_to_pixel,
12760 cvt_string_to_pixel_args,
12761 XtNumber (cvt_string_to_pixel_args),
12762 XtCacheByDisplay, cvt_pixel_dtor);
12764 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
12765 #endif
12767 #ifdef USE_TOOLKIT_SCROLL_BARS
12768 #ifndef USE_GTK
12769 xaw3d_arrow_scroll = False;
12770 xaw3d_pick_top = True;
12771 #endif
12772 #endif
12774 #ifdef USE_CAIRO
12775 x_cr_init_fringe (&x_redisplay_interface);
12776 #endif
12778 /* Note that there is no real way portable across R3/R4 to get the
12779 original error handler. */
12780 XSetErrorHandler (x_error_handler);
12781 XSetIOErrorHandler (x_io_error_quitter);
12784 #ifdef USE_GTK
12785 void
12786 init_xterm (void)
12788 /* Emacs can handle only core input events, so make sure
12789 Gtk doesn't use Xinput or Xinput2 extensions. */
12790 xputenv ("GDK_CORE_DEVICE_EVENTS=1");
12792 #endif
12794 void
12795 syms_of_xterm (void)
12797 x_error_message = NULL;
12799 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
12800 DEFSYM (Qlatin_1, "latin-1");
12802 #ifdef USE_GTK
12803 xg_default_icon_file = build_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
12804 staticpro (&xg_default_icon_file);
12806 DEFSYM (Qx_gtk_map_stock, "x-gtk-map-stock");
12807 #endif
12809 DEFVAR_BOOL ("x-use-underline-position-properties",
12810 x_use_underline_position_properties,
12811 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
12812 A value of nil means ignore them. If you encounter fonts with bogus
12813 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
12814 to 4.1, set this to nil. You can also use `underline-minimum-offset'
12815 to override the font's UNDERLINE_POSITION for small font display
12816 sizes. */);
12817 x_use_underline_position_properties = true;
12819 DEFVAR_BOOL ("x-underline-at-descent-line",
12820 x_underline_at_descent_line,
12821 doc: /* Non-nil means to draw the underline at the same place as the descent line.
12822 A value of nil means to draw the underline according to the value of the
12823 variable `x-use-underline-position-properties', which is usually at the
12824 baseline level. The default value is nil. */);
12825 x_underline_at_descent_line = false;
12827 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
12828 x_mouse_click_focus_ignore_position,
12829 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
12830 This variable is only used when the window manager requires that you
12831 click on a frame to select it (give it focus). In that case, a value
12832 of nil, means that the selected window and cursor position changes to
12833 reflect the mouse click position, while a non-nil value means that the
12834 selected window or cursor position is preserved. */);
12835 x_mouse_click_focus_ignore_position = false;
12837 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
12838 doc: /* Which toolkit scroll bars Emacs uses, if any.
12839 A value of nil means Emacs doesn't use toolkit scroll bars.
12840 With the X Window system, the value is a symbol describing the
12841 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
12842 With MS Windows or Nextstep, the value is t. */);
12843 #ifdef USE_TOOLKIT_SCROLL_BARS
12844 #ifdef USE_MOTIF
12845 Vx_toolkit_scroll_bars = intern_c_string ("motif");
12846 #elif defined HAVE_XAW3D
12847 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
12848 #elif USE_GTK
12849 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
12850 #else
12851 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
12852 #endif
12853 #else
12854 Vx_toolkit_scroll_bars = Qnil;
12855 #endif
12857 DEFSYM (Qmodifier_value, "modifier-value");
12858 DEFSYM (Qctrl, "ctrl");
12859 Fput (Qctrl, Qmodifier_value, make_number (ctrl_modifier));
12860 DEFSYM (Qalt, "alt");
12861 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
12862 DEFSYM (Qhyper, "hyper");
12863 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
12864 DEFSYM (Qmeta, "meta");
12865 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
12866 DEFSYM (Qsuper, "super");
12867 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
12869 DEFVAR_LISP ("x-ctrl-keysym", Vx_ctrl_keysym,
12870 doc: /* Which keys Emacs uses for the ctrl modifier.
12871 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
12872 `super'. For example, `ctrl' means use the Ctrl_L and Ctrl_R keysyms.
12873 The default is nil, which is the same as `ctrl'. */);
12874 Vx_ctrl_keysym = Qnil;
12876 DEFVAR_LISP ("x-alt-keysym", Vx_alt_keysym,
12877 doc: /* Which keys Emacs uses for the alt modifier.
12878 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
12879 `super'. For example, `alt' means use the Alt_L and Alt_R keysyms.
12880 The default is nil, which is the same as `alt'. */);
12881 Vx_alt_keysym = Qnil;
12883 DEFVAR_LISP ("x-hyper-keysym", Vx_hyper_keysym,
12884 doc: /* Which keys Emacs uses for the hyper modifier.
12885 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
12886 `super'. For example, `hyper' means use the Hyper_L and Hyper_R
12887 keysyms. The default is nil, which is the same as `hyper'. */);
12888 Vx_hyper_keysym = Qnil;
12890 DEFVAR_LISP ("x-meta-keysym", Vx_meta_keysym,
12891 doc: /* Which keys Emacs uses for the meta modifier.
12892 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
12893 `super'. For example, `meta' means use the Meta_L and Meta_R keysyms.
12894 The default is nil, which is the same as `meta'. */);
12895 Vx_meta_keysym = Qnil;
12897 DEFVAR_LISP ("x-super-keysym", Vx_super_keysym,
12898 doc: /* Which keys Emacs uses for the super modifier.
12899 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
12900 `super'. For example, `super' means use the Super_L and Super_R
12901 keysyms. The default is nil, which is the same as `super'. */);
12902 Vx_super_keysym = Qnil;
12904 DEFVAR_LISP ("x-keysym-table", Vx_keysym_table,
12905 doc: /* Hash table of character codes indexed by X keysym codes. */);
12906 Vx_keysym_table = make_hash_table (hashtest_eql, 900,
12907 DEFAULT_REHASH_SIZE,
12908 DEFAULT_REHASH_THRESHOLD,
12909 Qnil, false);
12911 DEFVAR_BOOL ("x-frame-normalize-before-maximize",
12912 x_frame_normalize_before_maximize,
12913 doc: /* Non-nil means normalize frame before maximizing.
12914 If this variable is t, Emacs first asks the window manager to give the
12915 frame its normal size, and only then the final state, whenever changing
12916 from a full-height, full-width or full-both state to the maximized one
12917 or when changing from the maximized to the full-height or full-width
12918 state.
12920 Set this variable only if your window manager cannot handle the
12921 transition between the various maximization states. */);
12922 x_frame_normalize_before_maximize = false;
12924 DEFVAR_BOOL ("x-gtk-use-window-move", x_gtk_use_window_move,
12925 doc: /* Non-nil means rely on gtk_window_move to set frame positions.
12926 If this variable is t, the GTK build uses the function gtk_window_move
12927 to set or store frame positions and disables some time consuming frame
12928 position adjustments. */);
12929 x_gtk_use_window_move = false;