Merge branch 'master' into comment-cache
[emacs.git] / src / xterm.c
blob38229a5f31fb3975d3a5d74a36014a9bc49687c2
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 /* Get the underline thickness. Default is 1 pixel. */
3640 if (s->font && s->font->underline_thickness > 0)
3641 thickness = s->font->underline_thickness;
3642 else
3643 thickness = 1;
3644 if (x_underline_at_descent_line)
3645 position = (s->height - thickness) - (s->ybase - s->y);
3646 else
3648 /* Get the underline position. This is the recommended
3649 vertical offset in pixels from the baseline to the top of
3650 the underline. This is a signed value according to the
3651 specs, and its default is
3653 ROUND ((maximum descent) / 2), with
3654 ROUND(x) = floor (x + 0.5) */
3656 if (x_use_underline_position_properties
3657 && s->font && s->font->underline_position >= 0)
3658 position = s->font->underline_position;
3659 else if (s->font)
3660 position = (s->font->descent + 1) / 2;
3661 else
3662 position = underline_minimum_offset;
3664 position = max (position, underline_minimum_offset);
3666 /* Check the sanity of thickness and position. We should
3667 avoid drawing underline out of the current line area. */
3668 if (s->y + s->height <= s->ybase + position)
3669 position = (s->height - 1) - (s->ybase - s->y);
3670 if (s->y + s->height < s->ybase + position + thickness)
3671 thickness = (s->y + s->height) - (s->ybase + position);
3672 s->underline_thickness = thickness;
3673 s->underline_position = position;
3674 y = s->ybase + position;
3675 if (s->face->underline_defaulted_p)
3676 x_fill_rectangle (s->f, s->gc,
3677 s->x, y, s->width, thickness);
3678 else
3680 XGCValues xgcv;
3681 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3682 XSetForeground (s->display, s->gc, s->face->underline_color);
3683 x_fill_rectangle (s->f, s->gc,
3684 s->x, y, s->width, thickness);
3685 XSetForeground (s->display, s->gc, xgcv.foreground);
3689 /* Draw overline. */
3690 if (s->face->overline_p)
3692 unsigned long dy = 0, h = 1;
3694 if (s->face->overline_color_defaulted_p)
3695 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3696 s->width, h);
3697 else
3699 XGCValues xgcv;
3700 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3701 XSetForeground (s->display, s->gc, s->face->overline_color);
3702 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3703 s->width, h);
3704 XSetForeground (s->display, s->gc, xgcv.foreground);
3708 /* Draw strike-through. */
3709 if (s->face->strike_through_p)
3711 unsigned long h = 1;
3712 unsigned long dy = (s->height - h) / 2;
3714 if (s->face->strike_through_color_defaulted_p)
3715 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3716 s->width, h);
3717 else
3719 XGCValues xgcv;
3720 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3721 XSetForeground (s->display, s->gc, s->face->strike_through_color);
3722 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3723 s->width, h);
3724 XSetForeground (s->display, s->gc, xgcv.foreground);
3728 /* Draw relief if not yet drawn. */
3729 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
3730 x_draw_glyph_string_box (s);
3732 if (s->prev)
3734 struct glyph_string *prev;
3736 for (prev = s->prev; prev; prev = prev->prev)
3737 if (prev->hl != s->hl
3738 && prev->x + prev->width + prev->right_overhang > s->x)
3740 /* As prev was drawn while clipped to its own area, we
3741 must draw the right_overhang part using s->hl now. */
3742 enum draw_glyphs_face save = prev->hl;
3744 prev->hl = s->hl;
3745 x_set_glyph_string_gc (prev);
3746 x_set_glyph_string_clipping_exactly (s, prev);
3747 if (prev->first_glyph->type == CHAR_GLYPH)
3748 x_draw_glyph_string_foreground (prev);
3749 else
3750 x_draw_composite_glyph_string_foreground (prev);
3751 x_reset_clip_rectangles (prev->f, prev->gc);
3752 prev->hl = save;
3753 prev->num_clips = 0;
3757 if (s->next)
3759 struct glyph_string *next;
3761 for (next = s->next; next; next = next->next)
3762 if (next->hl != s->hl
3763 && next->x - next->left_overhang < s->x + s->width)
3765 /* As next will be drawn while clipped to its own area,
3766 we must draw the left_overhang part using s->hl now. */
3767 enum draw_glyphs_face save = next->hl;
3769 next->hl = s->hl;
3770 x_set_glyph_string_gc (next);
3771 x_set_glyph_string_clipping_exactly (s, next);
3772 if (next->first_glyph->type == CHAR_GLYPH)
3773 x_draw_glyph_string_foreground (next);
3774 else
3775 x_draw_composite_glyph_string_foreground (next);
3776 x_reset_clip_rectangles (next->f, next->gc);
3777 next->hl = save;
3778 next->num_clips = 0;
3779 next->clip_head = s->next;
3784 /* Reset clipping. */
3785 x_reset_clip_rectangles (s->f, s->gc);
3786 s->num_clips = 0;
3789 /* Shift display to make room for inserted glyphs. */
3791 static void
3792 x_shift_glyphs_for_insert (struct frame *f, int x, int y, int width, int height, int shift_by)
3794 /* Never called on a GUI frame, see
3795 http://lists.gnu.org/archive/html/emacs-devel/2015-05/msg00456.html
3797 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f), FRAME_X_DRAWABLE (f),
3798 f->output_data.x->normal_gc,
3799 x, y, width, height,
3800 x + shift_by, y);
3803 /* Delete N glyphs at the nominal cursor position. Not implemented
3804 for X frames. */
3806 static void
3807 x_delete_glyphs (struct frame *f, register int n)
3809 emacs_abort ();
3813 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
3814 If they are <= 0, this is probably an error. */
3816 static ATTRIBUTE_UNUSED void
3817 x_clear_area1 (Display *dpy, Window window,
3818 int x, int y, int width, int height, int exposures)
3820 eassert (width > 0 && height > 0);
3821 XClearArea (dpy, window, x, y, width, height, exposures);
3824 void
3825 x_clear_area (struct frame *f, int x, int y, int width, int height)
3827 #ifdef USE_CAIRO
3828 cairo_t *cr;
3830 eassert (width > 0 && height > 0);
3832 cr = x_begin_cr_clip (f, NULL);
3833 x_set_cr_source_with_gc_background (f, f->output_data.x->normal_gc);
3834 cairo_rectangle (cr, x, y, width, height);
3835 cairo_fill (cr);
3836 x_end_cr_clip (f);
3837 #else
3838 if (FRAME_X_DOUBLE_BUFFERED_P (f))
3839 XFillRectangle (FRAME_X_DISPLAY (f),
3840 FRAME_X_DRAWABLE (f),
3841 f->output_data.x->reverse_gc,
3842 x, y, width, height);
3843 else
3844 x_clear_area1 (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3845 x, y, width, height, False);
3846 #endif
3850 /* Clear an entire frame. */
3852 static void
3853 x_clear_frame (struct frame *f)
3855 /* Clearing the frame will erase any cursor, so mark them all as no
3856 longer visible. */
3857 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
3859 block_input ();
3861 font_drop_xrender_surfaces (f);
3862 x_clear_window (f);
3864 /* We have to clear the scroll bars. If we have changed colors or
3865 something like that, then they should be notified. */
3866 x_scroll_bar_clear (f);
3868 XFlush (FRAME_X_DISPLAY (f));
3870 unblock_input ();
3873 /* RIF: Show hourglass cursor on frame F. */
3875 static void
3876 x_show_hourglass (struct frame *f)
3878 Display *dpy = FRAME_X_DISPLAY (f);
3880 if (dpy)
3882 struct x_output *x = FRAME_X_OUTPUT (f);
3883 #ifdef USE_X_TOOLKIT
3884 if (x->widget)
3885 #else
3886 if (FRAME_OUTER_WINDOW (f))
3887 #endif
3889 x->hourglass_p = true;
3891 if (!x->hourglass_window)
3893 unsigned long mask = CWCursor;
3894 XSetWindowAttributes attrs;
3895 #ifdef USE_GTK
3896 Window parent = FRAME_X_WINDOW (f);
3897 #else
3898 Window parent = FRAME_OUTER_WINDOW (f);
3899 #endif
3900 attrs.cursor = x->hourglass_cursor;
3902 x->hourglass_window = XCreateWindow
3903 (dpy, parent, 0, 0, 32000, 32000, 0, 0,
3904 InputOnly, CopyFromParent, mask, &attrs);
3907 XMapRaised (dpy, x->hourglass_window);
3908 XFlush (dpy);
3913 /* RIF: Cancel hourglass cursor on frame F. */
3915 static void
3916 x_hide_hourglass (struct frame *f)
3918 struct x_output *x = FRAME_X_OUTPUT (f);
3920 /* Watch out for newly created frames. */
3921 if (x->hourglass_window)
3923 XUnmapWindow (FRAME_X_DISPLAY (f), x->hourglass_window);
3924 /* Sync here because XTread_socket looks at the
3925 hourglass_p flag that is reset to zero below. */
3926 XSync (FRAME_X_DISPLAY (f), False);
3927 x->hourglass_p = false;
3931 /* Invert the middle quarter of the frame for .15 sec. */
3933 static void
3934 XTflash (struct frame *f)
3936 block_input ();
3939 #ifdef USE_GTK
3940 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
3941 when the scroll bars and the edit widget share the same X window. */
3942 GdkWindow *window = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
3943 #ifdef HAVE_GTK3
3944 cairo_t *cr = gdk_cairo_create (window);
3945 cairo_set_source_rgb (cr, 1, 1, 1);
3946 cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
3947 #define XFillRectangle(d, win, gc, x, y, w, h) \
3948 do { \
3949 cairo_rectangle (cr, x, y, w, h); \
3950 cairo_fill (cr); \
3952 while (false)
3953 #else /* ! HAVE_GTK3 */
3954 GdkGCValues vals;
3955 GdkGC *gc;
3956 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
3957 ^ FRAME_BACKGROUND_PIXEL (f));
3958 vals.function = GDK_XOR;
3959 gc = gdk_gc_new_with_values (window,
3960 &vals, GDK_GC_FUNCTION | GDK_GC_FOREGROUND);
3961 #define XFillRectangle(d, win, gc, x, y, w, h) \
3962 gdk_draw_rectangle (window, gc, true, x, y, w, h)
3963 #endif /* ! HAVE_GTK3 */
3964 #else /* ! USE_GTK */
3965 GC gc;
3967 /* Create a GC that will use the GXxor function to flip foreground
3968 pixels into background pixels. */
3970 XGCValues values;
3972 values.function = GXxor;
3973 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3974 ^ FRAME_BACKGROUND_PIXEL (f));
3976 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3977 GCFunction | GCForeground, &values);
3979 #endif
3981 /* Get the height not including a menu bar widget. */
3982 int height = FRAME_PIXEL_HEIGHT (f);
3983 /* Height of each line to flash. */
3984 int flash_height = FRAME_LINE_HEIGHT (f);
3985 /* These will be the left and right margins of the rectangles. */
3986 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3987 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3988 int width = flash_right - flash_left;
3990 /* If window is tall, flash top and bottom line. */
3991 if (height > 3 * FRAME_LINE_HEIGHT (f))
3993 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3994 flash_left,
3995 (FRAME_INTERNAL_BORDER_WIDTH (f)
3996 + FRAME_TOP_MARGIN_HEIGHT (f)),
3997 width, flash_height);
3998 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3999 flash_left,
4000 (height - flash_height
4001 - FRAME_INTERNAL_BORDER_WIDTH (f)),
4002 width, flash_height);
4005 else
4006 /* If it is short, flash it all. */
4007 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4008 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
4009 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
4011 x_flush (f);
4014 struct timespec delay = make_timespec (0, 150 * 1000 * 1000);
4015 struct timespec wakeup = timespec_add (current_timespec (), delay);
4017 /* Keep waiting until past the time wakeup or any input gets
4018 available. */
4019 while (! detect_input_pending ())
4021 struct timespec current = current_timespec ();
4022 struct timespec timeout;
4024 /* Break if result would not be positive. */
4025 if (timespec_cmp (wakeup, current) <= 0)
4026 break;
4028 /* How long `select' should wait. */
4029 timeout = make_timespec (0, 10 * 1000 * 1000);
4031 /* Try to wait that long--but we might wake up sooner. */
4032 pselect (0, NULL, NULL, NULL, &timeout, NULL);
4036 /* If window is tall, flash top and bottom line. */
4037 if (height > 3 * FRAME_LINE_HEIGHT (f))
4039 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4040 flash_left,
4041 (FRAME_INTERNAL_BORDER_WIDTH (f)
4042 + FRAME_TOP_MARGIN_HEIGHT (f)),
4043 width, flash_height);
4044 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4045 flash_left,
4046 (height - flash_height
4047 - FRAME_INTERNAL_BORDER_WIDTH (f)),
4048 width, flash_height);
4050 else
4051 /* If it is short, flash it all. */
4052 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4053 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
4054 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
4056 #ifdef USE_GTK
4057 #ifdef HAVE_GTK3
4058 cairo_destroy (cr);
4059 #else
4060 g_object_unref (G_OBJECT (gc));
4061 #endif
4062 #undef XFillRectangle
4063 #else
4064 XFreeGC (FRAME_X_DISPLAY (f), gc);
4065 #endif
4066 x_flush (f);
4070 unblock_input ();
4074 static void
4075 XTtoggle_invisible_pointer (struct frame *f, bool invisible)
4077 block_input ();
4078 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, invisible);
4079 unblock_input ();
4083 /* Make audible bell. */
4085 static void
4086 XTring_bell (struct frame *f)
4088 if (FRAME_X_DISPLAY (f))
4090 if (visible_bell)
4091 XTflash (f);
4092 else
4094 block_input ();
4095 #ifdef HAVE_XKB
4096 XkbBell (FRAME_X_DISPLAY (f), None, 0, None);
4097 #else
4098 XBell (FRAME_X_DISPLAY (f), 0);
4099 #endif
4100 XFlush (FRAME_X_DISPLAY (f));
4101 unblock_input ();
4106 /***********************************************************************
4107 Line Dance
4108 ***********************************************************************/
4110 /* Perform an insert-lines or delete-lines operation, inserting N
4111 lines or deleting -N lines at vertical position VPOS. */
4113 static void
4114 x_ins_del_lines (struct frame *f, int vpos, int n)
4116 emacs_abort ();
4120 /* Scroll part of the display as described by RUN. */
4122 static void
4123 x_scroll_run (struct window *w, struct run *run)
4125 struct frame *f = XFRAME (w->frame);
4126 int x, y, width, height, from_y, to_y, bottom_y;
4128 /* Get frame-relative bounding box of the text display area of W,
4129 without mode lines. Include in this box the left and right
4130 fringe of W. */
4131 window_box (w, ANY_AREA, &x, &y, &width, &height);
4133 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
4134 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
4135 bottom_y = y + height;
4137 if (to_y < from_y)
4139 /* Scrolling up. Make sure we don't copy part of the mode
4140 line at the bottom. */
4141 if (from_y + run->height > bottom_y)
4142 height = bottom_y - from_y;
4143 else
4144 height = run->height;
4146 else
4148 /* Scrolling down. Make sure we don't copy over the mode line.
4149 at the bottom. */
4150 if (to_y + run->height > bottom_y)
4151 height = bottom_y - to_y;
4152 else
4153 height = run->height;
4156 block_input ();
4158 /* Cursor off. Will be switched on again in x_update_window_end. */
4159 x_clear_cursor (w);
4161 #ifdef USE_CAIRO
4162 SET_FRAME_GARBAGED (f);
4163 #else
4164 XCopyArea (FRAME_X_DISPLAY (f),
4165 FRAME_X_DRAWABLE (f), FRAME_X_DRAWABLE (f),
4166 f->output_data.x->normal_gc,
4167 x, from_y,
4168 width, height,
4169 x, to_y);
4170 #endif
4172 unblock_input ();
4177 /***********************************************************************
4178 Exposure Events
4179 ***********************************************************************/
4182 static void
4183 frame_highlight (struct frame *f)
4185 /* We used to only do this if Vx_no_window_manager was non-nil, but
4186 the ICCCM (section 4.1.6) says that the window's border pixmap
4187 and border pixel are window attributes which are "private to the
4188 client", so we can always change it to whatever we want. */
4189 block_input ();
4190 /* I recently started to get errors in this XSetWindowBorder, depending on
4191 the window-manager in use, tho something more is at play since I've been
4192 using that same window-manager binary for ever. Let's not crash just
4193 because of this (bug#9310). */
4194 x_catch_errors (FRAME_X_DISPLAY (f));
4195 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4196 f->output_data.x->border_pixel);
4197 x_uncatch_errors ();
4198 unblock_input ();
4199 x_update_cursor (f, true);
4200 x_set_frame_alpha (f);
4203 static void
4204 frame_unhighlight (struct frame *f)
4206 /* We used to only do this if Vx_no_window_manager was non-nil, but
4207 the ICCCM (section 4.1.6) says that the window's border pixmap
4208 and border pixel are window attributes which are "private to the
4209 client", so we can always change it to whatever we want. */
4210 block_input ();
4211 /* Same as above for XSetWindowBorder (bug#9310). */
4212 x_catch_errors (FRAME_X_DISPLAY (f));
4213 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4214 f->output_data.x->border_tile);
4215 x_uncatch_errors ();
4216 unblock_input ();
4217 x_update_cursor (f, true);
4218 x_set_frame_alpha (f);
4221 /* The focus has changed. Update the frames as necessary to reflect
4222 the new situation. Note that we can't change the selected frame
4223 here, because the Lisp code we are interrupting might become confused.
4224 Each event gets marked with the frame in which it occurred, so the
4225 Lisp code can tell when the switch took place by examining the events. */
4227 static void
4228 x_new_focus_frame (struct x_display_info *dpyinfo, struct frame *frame)
4230 struct frame *old_focus = dpyinfo->x_focus_frame;
4232 if (frame != dpyinfo->x_focus_frame)
4234 /* Set this before calling other routines, so that they see
4235 the correct value of x_focus_frame. */
4236 dpyinfo->x_focus_frame = frame;
4238 if (old_focus && old_focus->auto_lower)
4239 x_lower_frame (old_focus);
4241 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
4242 dpyinfo->x_pending_autoraise_frame = dpyinfo->x_focus_frame;
4243 else
4244 dpyinfo->x_pending_autoraise_frame = NULL;
4247 x_frame_rehighlight (dpyinfo);
4250 /* Handle FocusIn and FocusOut state changes for FRAME.
4251 If FRAME has focus and there exists more than one frame, puts
4252 a FOCUS_IN_EVENT into *BUFP. */
4254 static void
4255 x_focus_changed (int type, int state, struct x_display_info *dpyinfo, struct frame *frame, struct input_event *bufp)
4257 if (type == FocusIn)
4259 if (dpyinfo->x_focus_event_frame != frame)
4261 x_new_focus_frame (dpyinfo, frame);
4262 dpyinfo->x_focus_event_frame = frame;
4264 /* Don't stop displaying the initial startup message
4265 for a switch-frame event we don't need. */
4266 /* When run as a daemon, Vterminal_frame is always NIL. */
4267 bufp->arg = (((NILP (Vterminal_frame)
4268 || ! FRAME_X_P (XFRAME (Vterminal_frame))
4269 || EQ (Fdaemonp (), Qt))
4270 && CONSP (Vframe_list)
4271 && !NILP (XCDR (Vframe_list)))
4272 ? Qt : Qnil);
4273 bufp->kind = FOCUS_IN_EVENT;
4274 XSETFRAME (bufp->frame_or_window, frame);
4277 frame->output_data.x->focus_state |= state;
4279 #ifdef HAVE_X_I18N
4280 if (FRAME_XIC (frame))
4281 XSetICFocus (FRAME_XIC (frame));
4282 #endif
4284 else if (type == FocusOut)
4286 frame->output_data.x->focus_state &= ~state;
4288 if (dpyinfo->x_focus_event_frame == frame)
4290 dpyinfo->x_focus_event_frame = 0;
4291 x_new_focus_frame (dpyinfo, 0);
4293 bufp->kind = FOCUS_OUT_EVENT;
4294 XSETFRAME (bufp->frame_or_window, frame);
4297 #ifdef HAVE_X_I18N
4298 if (FRAME_XIC (frame))
4299 XUnsetICFocus (FRAME_XIC (frame));
4300 #endif
4301 if (frame->pointer_invisible)
4302 XTtoggle_invisible_pointer (frame, false);
4306 /* Return the Emacs frame-object corresponding to an X window.
4307 It could be the frame's main window or an icon window. */
4309 static struct frame *
4310 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4312 Lisp_Object tail, frame;
4313 struct frame *f;
4315 if (wdesc == None)
4316 return NULL;
4318 FOR_EACH_FRAME (tail, frame)
4320 f = XFRAME (frame);
4321 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4322 continue;
4323 if (f->output_data.x->hourglass_window == wdesc)
4324 return f;
4325 #ifdef USE_X_TOOLKIT
4326 if ((f->output_data.x->edit_widget
4327 && XtWindow (f->output_data.x->edit_widget) == wdesc)
4328 /* A tooltip frame? */
4329 || (!f->output_data.x->edit_widget
4330 && FRAME_X_WINDOW (f) == wdesc)
4331 || f->output_data.x->icon_desc == wdesc)
4332 return f;
4333 #else /* not USE_X_TOOLKIT */
4334 #ifdef USE_GTK
4335 if (f->output_data.x->edit_widget)
4337 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4338 struct x_output *x = f->output_data.x;
4339 if (gwdesc != 0 && gwdesc == x->edit_widget)
4340 return f;
4342 #endif /* USE_GTK */
4343 if (FRAME_X_WINDOW (f) == wdesc
4344 || f->output_data.x->icon_desc == wdesc)
4345 return f;
4346 #endif /* not USE_X_TOOLKIT */
4348 return 0;
4351 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
4353 /* Like x_window_to_frame but also compares the window with the widget's
4354 windows. */
4356 static struct frame *
4357 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4359 Lisp_Object tail, frame;
4360 struct frame *f, *found = NULL;
4361 struct x_output *x;
4363 if (wdesc == None)
4364 return NULL;
4366 FOR_EACH_FRAME (tail, frame)
4368 if (found)
4369 break;
4370 f = XFRAME (frame);
4371 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
4373 /* This frame matches if the window is any of its widgets. */
4374 x = f->output_data.x;
4375 if (x->hourglass_window == wdesc)
4376 found = f;
4377 else if (x->widget)
4379 #ifdef USE_GTK
4380 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4381 if (gwdesc != 0
4382 && gtk_widget_get_toplevel (gwdesc) == x->widget)
4383 found = f;
4384 #else
4385 if (wdesc == XtWindow (x->widget)
4386 || wdesc == XtWindow (x->column_widget)
4387 || wdesc == XtWindow (x->edit_widget))
4388 found = f;
4389 /* Match if the window is this frame's menubar. */
4390 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
4391 found = f;
4392 #endif
4394 else if (FRAME_X_WINDOW (f) == wdesc)
4395 /* A tooltip frame. */
4396 found = f;
4400 return found;
4403 /* Likewise, but consider only the menu bar widget. */
4405 static struct frame *
4406 x_menubar_window_to_frame (struct x_display_info *dpyinfo,
4407 const XEvent *event)
4409 Window wdesc = event->xany.window;
4410 Lisp_Object tail, frame;
4411 struct frame *f;
4412 struct x_output *x;
4414 if (wdesc == None)
4415 return NULL;
4417 FOR_EACH_FRAME (tail, frame)
4419 f = XFRAME (frame);
4420 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4421 continue;
4422 x = f->output_data.x;
4423 #ifdef USE_GTK
4424 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
4425 return f;
4426 #else
4427 /* Match if the window is this frame's menubar. */
4428 if (x->menubar_widget
4429 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
4430 return f;
4431 #endif
4433 return 0;
4436 /* Return the frame whose principal (outermost) window is WDESC.
4437 If WDESC is some other (smaller) window, we return 0. */
4439 struct frame *
4440 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4442 Lisp_Object tail, frame;
4443 struct frame *f;
4444 struct x_output *x;
4446 if (wdesc == None)
4447 return NULL;
4449 FOR_EACH_FRAME (tail, frame)
4451 f = XFRAME (frame);
4452 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4453 continue;
4454 x = f->output_data.x;
4456 if (x->widget)
4458 /* This frame matches if the window is its topmost widget. */
4459 #ifdef USE_GTK
4460 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4461 if (gwdesc == x->widget)
4462 return f;
4463 #else
4464 if (wdesc == XtWindow (x->widget))
4465 return f;
4466 #endif
4468 else if (FRAME_X_WINDOW (f) == wdesc)
4469 /* Tooltip frame. */
4470 return f;
4472 return 0;
4475 #else /* !USE_X_TOOLKIT && !USE_GTK */
4477 #define x_any_window_to_frame(d, i) x_window_to_frame (d, i)
4478 #define x_top_window_to_frame(d, i) x_window_to_frame (d, i)
4480 #endif /* USE_X_TOOLKIT || USE_GTK */
4482 /* The focus may have changed. Figure out if it is a real focus change,
4483 by checking both FocusIn/Out and Enter/LeaveNotify events.
4485 Returns FOCUS_IN_EVENT event in *BUFP. */
4487 static void
4488 x_detect_focus_change (struct x_display_info *dpyinfo, struct frame *frame,
4489 const XEvent *event, struct input_event *bufp)
4491 if (!frame)
4492 return;
4494 switch (event->type)
4496 case EnterNotify:
4497 case LeaveNotify:
4499 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
4500 int focus_state
4501 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
4503 if (event->xcrossing.detail != NotifyInferior
4504 && event->xcrossing.focus
4505 && ! (focus_state & FOCUS_EXPLICIT))
4506 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
4507 FOCUS_IMPLICIT,
4508 dpyinfo, frame, bufp);
4510 break;
4512 case FocusIn:
4513 case FocusOut:
4514 x_focus_changed (event->type,
4515 (event->xfocus.detail == NotifyPointer ?
4516 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
4517 dpyinfo, frame, bufp);
4518 break;
4520 case ClientMessage:
4521 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
4523 enum xembed_message msg = event->xclient.data.l[1];
4524 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
4525 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
4527 break;
4532 #if !defined USE_X_TOOLKIT && !defined USE_GTK
4533 /* Handle an event saying the mouse has moved out of an Emacs frame. */
4535 void
4536 x_mouse_leave (struct x_display_info *dpyinfo)
4538 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
4540 #endif
4542 /* The focus has changed, or we have redirected a frame's focus to
4543 another frame (this happens when a frame uses a surrogate
4544 mini-buffer frame). Shift the highlight as appropriate.
4546 The FRAME argument doesn't necessarily have anything to do with which
4547 frame is being highlighted or un-highlighted; we only use it to find
4548 the appropriate X display info. */
4550 static void
4551 XTframe_rehighlight (struct frame *frame)
4553 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
4556 static void
4557 x_frame_rehighlight (struct x_display_info *dpyinfo)
4559 struct frame *old_highlight = dpyinfo->x_highlight_frame;
4561 if (dpyinfo->x_focus_frame)
4563 dpyinfo->x_highlight_frame
4564 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
4565 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
4566 : dpyinfo->x_focus_frame);
4567 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
4569 fset_focus_frame (dpyinfo->x_focus_frame, Qnil);
4570 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
4573 else
4574 dpyinfo->x_highlight_frame = 0;
4576 if (dpyinfo->x_highlight_frame != old_highlight)
4578 if (old_highlight)
4579 frame_unhighlight (old_highlight);
4580 if (dpyinfo->x_highlight_frame)
4581 frame_highlight (dpyinfo->x_highlight_frame);
4587 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
4589 /* Initialize mode_switch_bit and modifier_meaning. */
4590 static void
4591 x_find_modifier_meanings (struct x_display_info *dpyinfo)
4593 int min_code, max_code;
4594 KeySym *syms;
4595 int syms_per_code;
4596 XModifierKeymap *mods;
4598 dpyinfo->meta_mod_mask = 0;
4599 dpyinfo->shift_lock_mask = 0;
4600 dpyinfo->alt_mod_mask = 0;
4601 dpyinfo->super_mod_mask = 0;
4602 dpyinfo->hyper_mod_mask = 0;
4604 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
4606 syms = XGetKeyboardMapping (dpyinfo->display,
4607 min_code, max_code - min_code + 1,
4608 &syms_per_code);
4609 mods = XGetModifierMapping (dpyinfo->display);
4611 /* Scan the modifier table to see which modifier bits the Meta and
4612 Alt keysyms are on. */
4614 int row, col; /* The row and column in the modifier table. */
4615 bool found_alt_or_meta;
4617 for (row = 3; row < 8; row++)
4619 found_alt_or_meta = false;
4620 for (col = 0; col < mods->max_keypermod; col++)
4622 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
4624 /* Zeroes are used for filler. Skip them. */
4625 if (code == 0)
4626 continue;
4628 /* Are any of this keycode's keysyms a meta key? */
4630 int code_col;
4632 for (code_col = 0; code_col < syms_per_code; code_col++)
4634 int sym = syms[((code - min_code) * syms_per_code) + code_col];
4636 switch (sym)
4638 case XK_Meta_L:
4639 case XK_Meta_R:
4640 found_alt_or_meta = true;
4641 dpyinfo->meta_mod_mask |= (1 << row);
4642 break;
4644 case XK_Alt_L:
4645 case XK_Alt_R:
4646 found_alt_or_meta = true;
4647 dpyinfo->alt_mod_mask |= (1 << row);
4648 break;
4650 case XK_Hyper_L:
4651 case XK_Hyper_R:
4652 if (!found_alt_or_meta)
4653 dpyinfo->hyper_mod_mask |= (1 << row);
4654 code_col = syms_per_code;
4655 col = mods->max_keypermod;
4656 break;
4658 case XK_Super_L:
4659 case XK_Super_R:
4660 if (!found_alt_or_meta)
4661 dpyinfo->super_mod_mask |= (1 << row);
4662 code_col = syms_per_code;
4663 col = mods->max_keypermod;
4664 break;
4666 case XK_Shift_Lock:
4667 /* Ignore this if it's not on the lock modifier. */
4668 if (!found_alt_or_meta && ((1 << row) == LockMask))
4669 dpyinfo->shift_lock_mask = LockMask;
4670 code_col = syms_per_code;
4671 col = mods->max_keypermod;
4672 break;
4680 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
4681 if (! dpyinfo->meta_mod_mask)
4683 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
4684 dpyinfo->alt_mod_mask = 0;
4687 /* If some keys are both alt and meta,
4688 make them just meta, not alt. */
4689 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
4691 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
4694 XFree (syms);
4695 XFreeModifiermap (mods);
4698 /* Convert between the modifier bits X uses and the modifier bits
4699 Emacs uses. */
4702 x_x_to_emacs_modifiers (struct x_display_info *dpyinfo, int state)
4704 int mod_ctrl = ctrl_modifier;
4705 int mod_meta = meta_modifier;
4706 int mod_alt = alt_modifier;
4707 int mod_hyper = hyper_modifier;
4708 int mod_super = super_modifier;
4709 Lisp_Object tem;
4711 tem = Fget (Vx_ctrl_keysym, Qmodifier_value);
4712 if (INTEGERP (tem)) mod_ctrl = XINT (tem) & INT_MAX;
4713 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4714 if (INTEGERP (tem)) mod_alt = XINT (tem) & INT_MAX;
4715 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4716 if (INTEGERP (tem)) mod_meta = XINT (tem) & INT_MAX;
4717 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4718 if (INTEGERP (tem)) mod_hyper = XINT (tem) & INT_MAX;
4719 tem = Fget (Vx_super_keysym, Qmodifier_value);
4720 if (INTEGERP (tem)) mod_super = XINT (tem) & INT_MAX;
4722 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
4723 | ((state & ControlMask) ? mod_ctrl : 0)
4724 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
4725 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
4726 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
4727 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
4730 static int
4731 x_emacs_to_x_modifiers (struct x_display_info *dpyinfo, EMACS_INT state)
4733 EMACS_INT mod_ctrl = ctrl_modifier;
4734 EMACS_INT mod_meta = meta_modifier;
4735 EMACS_INT mod_alt = alt_modifier;
4736 EMACS_INT mod_hyper = hyper_modifier;
4737 EMACS_INT mod_super = super_modifier;
4739 Lisp_Object tem;
4741 tem = Fget (Vx_ctrl_keysym, Qmodifier_value);
4742 if (INTEGERP (tem)) mod_ctrl = XINT (tem);
4743 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4744 if (INTEGERP (tem)) mod_alt = XINT (tem);
4745 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4746 if (INTEGERP (tem)) mod_meta = XINT (tem);
4747 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4748 if (INTEGERP (tem)) mod_hyper = XINT (tem);
4749 tem = Fget (Vx_super_keysym, Qmodifier_value);
4750 if (INTEGERP (tem)) mod_super = XINT (tem);
4753 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
4754 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
4755 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
4756 | ((state & shift_modifier) ? ShiftMask : 0)
4757 | ((state & mod_ctrl) ? ControlMask : 0)
4758 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
4761 /* Convert a keysym to its name. */
4763 char *
4764 x_get_keysym_name (int keysym)
4766 char *value;
4768 block_input ();
4769 value = XKeysymToString (keysym);
4770 unblock_input ();
4772 return value;
4775 /* Mouse clicks and mouse movement. Rah.
4777 Formerly, we used PointerMotionHintMask (in standard_event_mask)
4778 so that we would have to call XQueryPointer after each MotionNotify
4779 event to ask for another such event. However, this made mouse tracking
4780 slow, and there was a bug that made it eventually stop.
4782 Simply asking for MotionNotify all the time seems to work better.
4784 In order to avoid asking for motion events and then throwing most
4785 of them away or busy-polling the server for mouse positions, we ask
4786 the server for pointer motion hints. This means that we get only
4787 one event per group of mouse movements. "Groups" are delimited by
4788 other kinds of events (focus changes and button clicks, for
4789 example), or by XQueryPointer calls; when one of these happens, we
4790 get another MotionNotify event the next time the mouse moves. This
4791 is at least as efficient as getting motion events when mouse
4792 tracking is on, and I suspect only negligibly worse when tracking
4793 is off. */
4795 /* Prepare a mouse-event in *RESULT for placement in the input queue.
4797 If the event is a button press, then note that we have grabbed
4798 the mouse. */
4800 static Lisp_Object
4801 construct_mouse_click (struct input_event *result,
4802 const XButtonEvent *event,
4803 struct frame *f)
4805 /* Make the event type NO_EVENT; we'll change that when we decide
4806 otherwise. */
4807 result->kind = MOUSE_CLICK_EVENT;
4808 result->code = event->button - Button1;
4809 result->timestamp = event->time;
4810 result->modifiers = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f),
4811 event->state)
4812 | (event->type == ButtonRelease
4813 ? up_modifier
4814 : down_modifier));
4816 XSETINT (result->x, event->x);
4817 XSETINT (result->y, event->y);
4818 XSETFRAME (result->frame_or_window, f);
4819 result->arg = Qnil;
4820 return Qnil;
4823 /* Function to report a mouse movement to the mainstream Emacs code.
4824 The input handler calls this.
4826 We have received a mouse movement event, which is given in *event.
4827 If the mouse is over a different glyph than it was last time, tell
4828 the mainstream emacs code by setting mouse_moved. If not, ask for
4829 another motion event, so we can check again the next time it moves. */
4831 static bool
4832 note_mouse_movement (struct frame *frame, const XMotionEvent *event)
4834 XRectangle *r;
4835 struct x_display_info *dpyinfo;
4837 if (!FRAME_X_OUTPUT (frame))
4838 return false;
4840 dpyinfo = FRAME_DISPLAY_INFO (frame);
4841 dpyinfo->last_mouse_movement_time = event->time;
4842 dpyinfo->last_mouse_motion_frame = frame;
4843 dpyinfo->last_mouse_motion_x = event->x;
4844 dpyinfo->last_mouse_motion_y = event->y;
4846 if (event->window != FRAME_X_WINDOW (frame))
4848 frame->mouse_moved = true;
4849 dpyinfo->last_mouse_scroll_bar = NULL;
4850 note_mouse_highlight (frame, -1, -1);
4851 dpyinfo->last_mouse_glyph_frame = NULL;
4852 return true;
4856 /* Has the mouse moved off the glyph it was on at the last sighting? */
4857 r = &dpyinfo->last_mouse_glyph;
4858 if (frame != dpyinfo->last_mouse_glyph_frame
4859 || event->x < r->x || event->x >= r->x + r->width
4860 || event->y < r->y || event->y >= r->y + r->height)
4862 frame->mouse_moved = true;
4863 dpyinfo->last_mouse_scroll_bar = NULL;
4864 note_mouse_highlight (frame, event->x, event->y);
4865 /* Remember which glyph we're now on. */
4866 remember_mouse_glyph (frame, event->x, event->y, r);
4867 dpyinfo->last_mouse_glyph_frame = frame;
4868 return true;
4871 return false;
4874 /* Return the current position of the mouse.
4875 *FP should be a frame which indicates which display to ask about.
4877 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
4878 and *PART to the frame, window, and scroll bar part that the mouse
4879 is over. Set *X and *Y to the portion and whole of the mouse's
4880 position on the scroll bar.
4882 If the mouse movement started elsewhere, set *FP to the frame the
4883 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
4884 the mouse is over.
4886 Set *TIMESTAMP to the server time-stamp for the time at which the mouse
4887 was at this position.
4889 Don't store anything if we don't have a valid set of values to report.
4891 This clears the mouse_moved flag, so we can wait for the next mouse
4892 movement. */
4894 static void
4895 XTmouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
4896 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
4897 Time *timestamp)
4899 struct frame *f1;
4900 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
4902 block_input ();
4904 if (dpyinfo->last_mouse_scroll_bar && insist == 0)
4906 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
4908 if (bar->horizontal)
4909 x_horizontal_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
4910 else
4911 x_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
4913 else
4915 Window root;
4916 int root_x, root_y;
4918 Window dummy_window;
4919 int dummy;
4921 Lisp_Object frame, tail;
4923 /* Clear the mouse-moved flag for every frame on this display. */
4924 FOR_EACH_FRAME (tail, frame)
4925 if (FRAME_X_P (XFRAME (frame))
4926 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
4927 XFRAME (frame)->mouse_moved = false;
4929 dpyinfo->last_mouse_scroll_bar = NULL;
4931 /* Figure out which root window we're on. */
4932 XQueryPointer (FRAME_X_DISPLAY (*fp),
4933 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
4935 /* The root window which contains the pointer. */
4936 &root,
4938 /* Trash which we can't trust if the pointer is on
4939 a different screen. */
4940 &dummy_window,
4942 /* The position on that root window. */
4943 &root_x, &root_y,
4945 /* More trash we can't trust. */
4946 &dummy, &dummy,
4948 /* Modifier keys and pointer buttons, about which
4949 we don't care. */
4950 (unsigned int *) &dummy);
4952 /* Now we have a position on the root; find the innermost window
4953 containing the pointer. */
4955 Window win, child;
4956 int win_x, win_y;
4957 int parent_x = 0, parent_y = 0;
4959 win = root;
4961 /* XTranslateCoordinates can get errors if the window
4962 structure is changing at the same time this function
4963 is running. So at least we must not crash from them. */
4965 x_catch_errors (FRAME_X_DISPLAY (*fp));
4967 if (x_mouse_grabbed (dpyinfo))
4969 /* If mouse was grabbed on a frame, give coords for that frame
4970 even if the mouse is now outside it. */
4971 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
4973 /* From-window. */
4974 root,
4976 /* To-window. */
4977 FRAME_X_WINDOW (dpyinfo->last_mouse_frame),
4979 /* From-position, to-position. */
4980 root_x, root_y, &win_x, &win_y,
4982 /* Child of win. */
4983 &child);
4984 f1 = dpyinfo->last_mouse_frame;
4986 else
4988 while (true)
4990 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
4992 /* From-window, to-window. */
4993 root, win,
4995 /* From-position, to-position. */
4996 root_x, root_y, &win_x, &win_y,
4998 /* Child of win. */
4999 &child);
5001 if (child == None || child == win)
5002 break;
5003 #ifdef USE_GTK
5004 /* We don't wan't to know the innermost window. We
5005 want the edit window. For non-Gtk+ the innermost
5006 window is the edit window. For Gtk+ it might not
5007 be. It might be the tool bar for example. */
5008 if (x_window_to_frame (dpyinfo, win))
5009 break;
5010 #endif
5011 win = child;
5012 parent_x = win_x;
5013 parent_y = win_y;
5016 /* Now we know that:
5017 win is the innermost window containing the pointer
5018 (XTC says it has no child containing the pointer),
5019 win_x and win_y are the pointer's position in it
5020 (XTC did this the last time through), and
5021 parent_x and parent_y are the pointer's position in win's parent.
5022 (They are what win_x and win_y were when win was child.
5023 If win is the root window, it has no parent, and
5024 parent_{x,y} are invalid, but that's okay, because we'll
5025 never use them in that case.) */
5027 #ifdef USE_GTK
5028 /* We don't wan't to know the innermost window. We
5029 want the edit window. */
5030 f1 = x_window_to_frame (dpyinfo, win);
5031 #else
5032 /* Is win one of our frames? */
5033 f1 = x_any_window_to_frame (dpyinfo, win);
5034 #endif
5036 #ifdef USE_X_TOOLKIT
5037 /* If we end up with the menu bar window, say it's not
5038 on the frame. */
5039 if (f1 != NULL
5040 && f1->output_data.x->menubar_widget
5041 && win == XtWindow (f1->output_data.x->menubar_widget))
5042 f1 = NULL;
5043 #endif /* USE_X_TOOLKIT */
5046 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
5047 f1 = 0;
5049 x_uncatch_errors_after_check ();
5051 /* If not, is it one of our scroll bars? */
5052 if (! f1)
5054 struct scroll_bar *bar;
5056 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win, 2);
5058 if (bar)
5060 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5061 win_x = parent_x;
5062 win_y = parent_y;
5066 if (f1 == 0 && insist > 0)
5067 f1 = SELECTED_FRAME ();
5069 if (f1)
5071 /* Ok, we found a frame. Store all the values.
5072 last_mouse_glyph is a rectangle used to reduce the
5073 generation of mouse events. To not miss any motion
5074 events, we must divide the frame into rectangles of the
5075 size of the smallest character that could be displayed
5076 on it, i.e. into the same rectangles that matrices on
5077 the frame are divided into. */
5079 /* FIXME: what if F1 is not an X frame? */
5080 dpyinfo = FRAME_DISPLAY_INFO (f1);
5081 remember_mouse_glyph (f1, win_x, win_y, &dpyinfo->last_mouse_glyph);
5082 dpyinfo->last_mouse_glyph_frame = f1;
5084 *bar_window = Qnil;
5085 *part = 0;
5086 *fp = f1;
5087 XSETINT (*x, win_x);
5088 XSETINT (*y, win_y);
5089 *timestamp = dpyinfo->last_mouse_movement_time;
5094 unblock_input ();
5099 /***********************************************************************
5100 Scroll bars
5101 ***********************************************************************/
5103 /* Scroll bar support. */
5105 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
5106 manages it.
5107 This can be called in GC, so we have to make sure to strip off mark
5108 bits. */
5110 static struct scroll_bar *
5111 x_window_to_scroll_bar (Display *display, Window window_id, int type)
5113 Lisp_Object tail, frame;
5115 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
5116 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
5117 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
5119 FOR_EACH_FRAME (tail, frame)
5121 Lisp_Object bar, condemned;
5123 if (! FRAME_X_P (XFRAME (frame)))
5124 continue;
5126 /* Scan this frame's scroll bar list for a scroll bar with the
5127 right window ID. */
5128 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
5129 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
5130 /* This trick allows us to search both the ordinary and
5131 condemned scroll bar lists with one loop. */
5132 ! NILP (bar) || (bar = condemned,
5133 condemned = Qnil,
5134 ! NILP (bar));
5135 bar = XSCROLL_BAR (bar)->next)
5136 if (XSCROLL_BAR (bar)->x_window == window_id
5137 && FRAME_X_DISPLAY (XFRAME (frame)) == display
5138 && (type = 2
5139 || (type == 1 && XSCROLL_BAR (bar)->horizontal)
5140 || (type == 0 && !XSCROLL_BAR (bar)->horizontal)))
5141 return XSCROLL_BAR (bar);
5144 return NULL;
5148 #if defined USE_LUCID
5150 /* Return the Lucid menu bar WINDOW is part of. Return null
5151 if WINDOW is not part of a menu bar. */
5153 static Widget
5154 x_window_to_menu_bar (Window window)
5156 Lisp_Object tail, frame;
5158 FOR_EACH_FRAME (tail, frame)
5159 if (FRAME_X_P (XFRAME (frame)))
5161 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
5163 if (menu_bar && xlwmenu_window_p (menu_bar, window))
5164 return menu_bar;
5166 return NULL;
5169 #endif /* USE_LUCID */
5172 /************************************************************************
5173 Toolkit scroll bars
5174 ************************************************************************/
5176 #ifdef USE_TOOLKIT_SCROLL_BARS
5178 static void x_send_scroll_bar_event (Lisp_Object, enum scroll_bar_part,
5179 int, int, bool);
5181 /* Lisp window being scrolled. Set when starting to interact with
5182 a toolkit scroll bar, reset to nil when ending the interaction. */
5184 static Lisp_Object window_being_scrolled;
5186 /* Whether this is an Xaw with arrow-scrollbars. This should imply
5187 that movements of 1/20 of the screen size are mapped to up/down. */
5189 #ifndef USE_GTK
5190 /* Id of action hook installed for scroll bars. */
5192 static XtActionHookId action_hook_id;
5193 static XtActionHookId horizontal_action_hook_id;
5195 static Boolean xaw3d_arrow_scroll;
5197 /* Whether the drag scrolling maintains the mouse at the top of the
5198 thumb. If not, resizing the thumb needs to be done more carefully
5199 to avoid jerkiness. */
5201 static Boolean xaw3d_pick_top;
5203 /* Action hook installed via XtAppAddActionHook when toolkit scroll
5204 bars are used.. The hook is responsible for detecting when
5205 the user ends an interaction with the scroll bar, and generates
5206 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
5208 static void
5209 xt_action_hook (Widget widget, XtPointer client_data, String action_name,
5210 XEvent *event, String *params, Cardinal *num_params)
5212 bool scroll_bar_p;
5213 const char *end_action;
5215 #ifdef USE_MOTIF
5216 scroll_bar_p = XmIsScrollBar (widget);
5217 end_action = "Release";
5218 #else /* !USE_MOTIF i.e. use Xaw */
5219 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
5220 end_action = "EndScroll";
5221 #endif /* USE_MOTIF */
5223 if (scroll_bar_p
5224 && strcmp (action_name, end_action) == 0
5225 && WINDOWP (window_being_scrolled))
5227 struct window *w;
5228 struct scroll_bar *bar;
5230 x_send_scroll_bar_event (window_being_scrolled,
5231 scroll_bar_end_scroll, 0, 0, false);
5232 w = XWINDOW (window_being_scrolled);
5233 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5235 if (bar->dragging != -1)
5237 bar->dragging = -1;
5238 /* The thumb size is incorrect while dragging: fix it. */
5239 set_vertical_scroll_bar (w);
5241 window_being_scrolled = Qnil;
5242 #if defined (USE_LUCID)
5243 bar->last_seen_part = scroll_bar_nowhere;
5244 #endif
5245 /* Xt timeouts no longer needed. */
5246 toolkit_scroll_bar_interaction = false;
5251 static void
5252 xt_horizontal_action_hook (Widget widget, XtPointer client_data, String action_name,
5253 XEvent *event, String *params, Cardinal *num_params)
5255 bool scroll_bar_p;
5256 const char *end_action;
5258 #ifdef USE_MOTIF
5259 scroll_bar_p = XmIsScrollBar (widget);
5260 end_action = "Release";
5261 #else /* !USE_MOTIF i.e. use Xaw */
5262 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
5263 end_action = "EndScroll";
5264 #endif /* USE_MOTIF */
5266 if (scroll_bar_p
5267 && strcmp (action_name, end_action) == 0
5268 && WINDOWP (window_being_scrolled))
5270 struct window *w;
5271 struct scroll_bar *bar;
5273 x_send_scroll_bar_event (window_being_scrolled,
5274 scroll_bar_end_scroll, 0, 0, true);
5275 w = XWINDOW (window_being_scrolled);
5276 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
5278 if (bar->dragging != -1)
5280 bar->dragging = -1;
5281 /* The thumb size is incorrect while dragging: fix it. */
5282 set_horizontal_scroll_bar (w);
5284 window_being_scrolled = Qnil;
5285 #if defined (USE_LUCID)
5286 bar->last_seen_part = scroll_bar_nowhere;
5287 #endif
5288 /* Xt timeouts no longer needed. */
5289 toolkit_scroll_bar_interaction = false;
5292 #endif /* not USE_GTK */
5294 /* Send a client message with message type Xatom_Scrollbar for a
5295 scroll action to the frame of WINDOW. PART is a value identifying
5296 the part of the scroll bar that was clicked on. PORTION is the
5297 amount to scroll of a whole of WHOLE. */
5299 static void
5300 x_send_scroll_bar_event (Lisp_Object window, enum scroll_bar_part part,
5301 int portion, int whole, bool horizontal)
5303 XEvent event;
5304 XClientMessageEvent *ev = &event.xclient;
5305 struct window *w = XWINDOW (window);
5306 struct frame *f = XFRAME (w->frame);
5307 intptr_t iw = (intptr_t) w;
5308 verify (INTPTR_WIDTH <= 64);
5309 int sign_shift = INTPTR_WIDTH - 32;
5311 block_input ();
5313 /* Construct a ClientMessage event to send to the frame. */
5314 ev->type = ClientMessage;
5315 ev->message_type = (horizontal
5316 ? FRAME_DISPLAY_INFO (f)->Xatom_Horizontal_Scrollbar
5317 : FRAME_DISPLAY_INFO (f)->Xatom_Scrollbar);
5318 ev->display = FRAME_X_DISPLAY (f);
5319 ev->window = FRAME_X_WINDOW (f);
5320 ev->format = 32;
5322 /* A 32-bit X client on a 64-bit X server can pass a window pointer
5323 as-is. A 64-bit client on a 32-bit X server is in trouble
5324 because a pointer does not fit and would be truncated while
5325 passing through the server. So use two slots and hope that X12
5326 will resolve such issues someday. */
5327 ev->data.l[0] = iw >> 31 >> 1;
5328 ev->data.l[1] = sign_shift <= 0 ? iw : iw << sign_shift >> sign_shift;
5329 ev->data.l[2] = part;
5330 ev->data.l[3] = portion;
5331 ev->data.l[4] = whole;
5333 /* Make Xt timeouts work while the scroll bar is active. */
5334 #ifdef USE_X_TOOLKIT
5335 toolkit_scroll_bar_interaction = true;
5336 x_activate_timeout_atimer ();
5337 #endif
5339 /* Setting the event mask to zero means that the message will
5340 be sent to the client that created the window, and if that
5341 window no longer exists, no event will be sent. */
5342 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
5343 unblock_input ();
5347 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
5348 in *IEVENT. */
5350 static void
5351 x_scroll_bar_to_input_event (const XEvent *event,
5352 struct input_event *ievent)
5354 const XClientMessageEvent *ev = &event->xclient;
5355 Lisp_Object window;
5356 struct window *w;
5358 /* See the comment in the function above. */
5359 intptr_t iw0 = ev->data.l[0];
5360 intptr_t iw1 = ev->data.l[1];
5361 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
5362 w = (struct window *) iw;
5364 XSETWINDOW (window, w);
5366 ievent->kind = SCROLL_BAR_CLICK_EVENT;
5367 ievent->frame_or_window = window;
5368 ievent->arg = Qnil;
5369 #ifdef USE_GTK
5370 ievent->timestamp = CurrentTime;
5371 #else
5372 ievent->timestamp =
5373 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
5374 #endif
5375 ievent->code = 0;
5376 ievent->part = ev->data.l[2];
5377 ievent->x = make_number (ev->data.l[3]);
5378 ievent->y = make_number (ev->data.l[4]);
5379 ievent->modifiers = 0;
5382 /* Transform a horizontal scroll bar ClientMessage EVENT to an Emacs
5383 input event in *IEVENT. */
5385 static void
5386 x_horizontal_scroll_bar_to_input_event (const XEvent *event,
5387 struct input_event *ievent)
5389 const XClientMessageEvent *ev = &event->xclient;
5390 Lisp_Object window;
5391 struct window *w;
5393 /* See the comment in the function above. */
5394 intptr_t iw0 = ev->data.l[0];
5395 intptr_t iw1 = ev->data.l[1];
5396 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
5397 w = (struct window *) iw;
5399 XSETWINDOW (window, w);
5401 ievent->kind = HORIZONTAL_SCROLL_BAR_CLICK_EVENT;
5402 ievent->frame_or_window = window;
5403 ievent->arg = Qnil;
5404 #ifdef USE_GTK
5405 ievent->timestamp = CurrentTime;
5406 #else
5407 ievent->timestamp =
5408 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
5409 #endif
5410 ievent->code = 0;
5411 ievent->part = ev->data.l[2];
5412 ievent->x = make_number (ev->data.l[3]);
5413 ievent->y = make_number (ev->data.l[4]);
5414 ievent->modifiers = 0;
5418 #ifdef USE_MOTIF
5420 /* Minimum and maximum values used for Motif scroll bars. */
5422 #define XM_SB_MAX 10000000
5424 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
5425 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
5426 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
5428 static void
5429 xm_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5431 struct scroll_bar *bar = client_data;
5432 XmScrollBarCallbackStruct *cs = call_data;
5433 enum scroll_bar_part part = scroll_bar_nowhere;
5434 bool horizontal = bar->horizontal;
5435 int whole = 0, portion = 0;
5437 switch (cs->reason)
5439 case XmCR_DECREMENT:
5440 bar->dragging = -1;
5441 part = horizontal ? scroll_bar_left_arrow : scroll_bar_up_arrow;
5442 break;
5444 case XmCR_INCREMENT:
5445 bar->dragging = -1;
5446 part = horizontal ? scroll_bar_right_arrow : scroll_bar_down_arrow;
5447 break;
5449 case XmCR_PAGE_DECREMENT:
5450 bar->dragging = -1;
5451 part = horizontal ? scroll_bar_before_handle : scroll_bar_above_handle;
5452 break;
5454 case XmCR_PAGE_INCREMENT:
5455 bar->dragging = -1;
5456 part = horizontal ? scroll_bar_after_handle : scroll_bar_below_handle;
5457 break;
5459 case XmCR_TO_TOP:
5460 bar->dragging = -1;
5461 part = horizontal ? scroll_bar_to_leftmost : scroll_bar_to_top;
5462 break;
5464 case XmCR_TO_BOTTOM:
5465 bar->dragging = -1;
5466 part = horizontal ? scroll_bar_to_rightmost : scroll_bar_to_bottom;
5467 break;
5469 case XmCR_DRAG:
5471 int slider_size;
5473 block_input ();
5474 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
5475 unblock_input ();
5477 if (horizontal)
5479 portion = bar->whole * ((float)cs->value / XM_SB_MAX);
5480 whole = bar->whole * ((float)(XM_SB_MAX - slider_size) / XM_SB_MAX);
5481 portion = min (portion, whole);
5482 part = scroll_bar_horizontal_handle;
5484 else
5486 whole = XM_SB_MAX - slider_size;
5487 portion = min (cs->value, whole);
5488 part = scroll_bar_handle;
5491 bar->dragging = cs->value;
5493 break;
5495 case XmCR_VALUE_CHANGED:
5496 break;
5499 if (part != scroll_bar_nowhere)
5501 window_being_scrolled = bar->window;
5502 x_send_scroll_bar_event (bar->window, part, portion, whole,
5503 bar->horizontal);
5507 #elif defined USE_GTK
5509 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
5510 bar widget. DATA is a pointer to the scroll_bar structure. */
5512 static gboolean
5513 xg_scroll_callback (GtkRange *range,
5514 GtkScrollType scroll,
5515 gdouble value,
5516 gpointer user_data)
5518 int whole = 0, portion = 0;
5519 struct scroll_bar *bar = user_data;
5520 enum scroll_bar_part part = scroll_bar_nowhere;
5521 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
5522 struct frame *f = g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
5524 if (xg_ignore_gtk_scrollbar) return false;
5526 switch (scroll)
5528 case GTK_SCROLL_JUMP:
5529 /* Buttons 1 2 or 3 must be grabbed. */
5530 if (FRAME_DISPLAY_INFO (f)->grabbed != 0
5531 && FRAME_DISPLAY_INFO (f)->grabbed < (1 << 4))
5533 if (bar->horizontal)
5535 part = scroll_bar_horizontal_handle;
5536 whole = (int)(gtk_adjustment_get_upper (adj) -
5537 gtk_adjustment_get_page_size (adj));
5538 portion = min ((int)value, whole);
5539 bar->dragging = portion;
5541 else
5543 part = scroll_bar_handle;
5544 whole = gtk_adjustment_get_upper (adj) -
5545 gtk_adjustment_get_page_size (adj);
5546 portion = min ((int)value, whole);
5547 bar->dragging = portion;
5550 break;
5551 case GTK_SCROLL_STEP_BACKWARD:
5552 part = (bar->horizontal
5553 ? scroll_bar_left_arrow : scroll_bar_up_arrow);
5554 bar->dragging = -1;
5555 break;
5556 case GTK_SCROLL_STEP_FORWARD:
5557 part = (bar->horizontal
5558 ? scroll_bar_right_arrow : scroll_bar_down_arrow);
5559 bar->dragging = -1;
5560 break;
5561 case GTK_SCROLL_PAGE_BACKWARD:
5562 part = (bar->horizontal
5563 ? scroll_bar_before_handle : scroll_bar_above_handle);
5564 bar->dragging = -1;
5565 break;
5566 case GTK_SCROLL_PAGE_FORWARD:
5567 part = (bar->horizontal
5568 ? scroll_bar_after_handle : scroll_bar_below_handle);
5569 bar->dragging = -1;
5570 break;
5571 default:
5572 break;
5575 if (part != scroll_bar_nowhere)
5577 window_being_scrolled = bar->window;
5578 x_send_scroll_bar_event (bar->window, part, portion, whole,
5579 bar->horizontal);
5582 return false;
5585 /* Callback for button release. Sets dragging to -1 when dragging is done. */
5587 static gboolean
5588 xg_end_scroll_callback (GtkWidget *widget,
5589 GdkEventButton *event,
5590 gpointer user_data)
5592 struct scroll_bar *bar = user_data;
5593 bar->dragging = -1;
5594 if (WINDOWP (window_being_scrolled))
5596 x_send_scroll_bar_event (window_being_scrolled,
5597 scroll_bar_end_scroll, 0, 0, bar->horizontal);
5598 window_being_scrolled = Qnil;
5601 return false;
5605 #else /* not USE_GTK and not USE_MOTIF */
5607 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
5608 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
5609 scroll bar struct. CALL_DATA is a pointer to a float saying where
5610 the thumb is. */
5612 static void
5613 xaw_jump_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5615 struct scroll_bar *bar = client_data;
5616 float *top_addr = call_data;
5617 float top = *top_addr;
5618 float shown;
5619 int whole, portion, height, width;
5620 enum scroll_bar_part part;
5621 bool horizontal = bar->horizontal;
5624 if (horizontal)
5626 /* Get the size of the thumb, a value between 0 and 1. */
5627 block_input ();
5628 XtVaGetValues (widget, XtNshown, &shown, XtNwidth, &width, NULL);
5629 unblock_input ();
5631 if (shown < 1)
5633 whole = bar->whole - (shown * bar->whole);
5634 portion = min (top * bar->whole, whole);
5636 else
5638 whole = bar->whole;
5639 portion = 0;
5642 part = scroll_bar_horizontal_handle;
5644 else
5646 /* Get the size of the thumb, a value between 0 and 1. */
5647 block_input ();
5648 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
5649 unblock_input ();
5651 whole = 10000000;
5652 portion = shown < 1 ? top * whole : 0;
5654 if (shown < 1 && (eabs (top + shown - 1) < 1.0f / height))
5655 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
5656 the bottom, so we force the scrolling whenever we see that we're
5657 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
5658 we try to ensure that we always stay two pixels away from the
5659 bottom). */
5660 part = scroll_bar_down_arrow;
5661 else
5662 part = scroll_bar_handle;
5665 window_being_scrolled = bar->window;
5666 bar->dragging = portion;
5667 bar->last_seen_part = part;
5668 x_send_scroll_bar_event (bar->window, part, portion, whole, bar->horizontal);
5672 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
5673 i.e. line or page up or down. WIDGET is the Xaw scroll bar
5674 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
5675 the scroll bar. CALL_DATA is an integer specifying the action that
5676 has taken place. Its magnitude is in the range 0..height of the
5677 scroll bar. Negative values mean scroll towards buffer start.
5678 Values < height of scroll bar mean line-wise movement. */
5680 static void
5681 xaw_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5683 struct scroll_bar *bar = client_data;
5684 /* The position really is stored cast to a pointer. */
5685 int position = (intptr_t) call_data;
5686 Dimension height, width;
5687 enum scroll_bar_part part;
5689 if (bar->horizontal)
5691 /* Get the width of the scroll bar. */
5692 block_input ();
5693 XtVaGetValues (widget, XtNwidth, &width, NULL);
5694 unblock_input ();
5696 if (eabs (position) >= width)
5697 part = (position < 0) ? scroll_bar_before_handle : scroll_bar_after_handle;
5699 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
5700 it maps line-movement to call_data = max(5, height/20). */
5701 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, width / 20))
5702 part = (position < 0) ? scroll_bar_left_arrow : scroll_bar_right_arrow;
5703 else
5704 part = scroll_bar_move_ratio;
5706 window_being_scrolled = bar->window;
5707 bar->dragging = -1;
5708 bar->last_seen_part = part;
5709 x_send_scroll_bar_event (bar->window, part, position, width,
5710 bar->horizontal);
5712 else
5715 /* Get the height of the scroll bar. */
5716 block_input ();
5717 XtVaGetValues (widget, XtNheight, &height, NULL);
5718 unblock_input ();
5720 if (eabs (position) >= height)
5721 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
5723 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
5724 it maps line-movement to call_data = max(5, height/20). */
5725 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
5726 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
5727 else
5728 part = scroll_bar_move_ratio;
5730 window_being_scrolled = bar->window;
5731 bar->dragging = -1;
5732 bar->last_seen_part = part;
5733 x_send_scroll_bar_event (bar->window, part, position, height,
5734 bar->horizontal);
5738 #endif /* not USE_GTK and not USE_MOTIF */
5740 #define SCROLL_BAR_NAME "verticalScrollBar"
5741 #define SCROLL_BAR_HORIZONTAL_NAME "horizontalScrollBar"
5743 /* Create the widget for scroll bar BAR on frame F. Record the widget
5744 and X window of the scroll bar in BAR. */
5746 #ifdef USE_GTK
5747 static void
5748 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5750 const char *scroll_bar_name = SCROLL_BAR_NAME;
5752 block_input ();
5753 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
5754 G_CALLBACK (xg_end_scroll_callback),
5755 scroll_bar_name);
5756 unblock_input ();
5759 static void
5760 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5762 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
5764 block_input ();
5765 xg_create_horizontal_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
5766 G_CALLBACK (xg_end_scroll_callback),
5767 scroll_bar_name);
5768 unblock_input ();
5771 #else /* not USE_GTK */
5773 static void
5774 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5776 Window xwindow;
5777 Widget widget;
5778 Arg av[20];
5779 int ac = 0;
5780 const char *scroll_bar_name = SCROLL_BAR_NAME;
5781 unsigned long pixel;
5783 block_input ();
5785 #ifdef USE_MOTIF
5786 /* Set resources. Create the widget. */
5787 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5788 XtSetArg (av[ac], XmNminimum, 0); ++ac;
5789 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
5790 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
5791 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
5792 XtSetArg (av[ac], XmNincrement, 1); ++ac;
5793 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
5795 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5796 if (pixel != -1)
5798 XtSetArg (av[ac], XmNforeground, pixel);
5799 ++ac;
5802 pixel = f->output_data.x->scroll_bar_background_pixel;
5803 if (pixel != -1)
5805 XtSetArg (av[ac], XmNbackground, pixel);
5806 ++ac;
5809 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
5810 (char *) scroll_bar_name, av, ac);
5812 /* Add one callback for everything that can happen. */
5813 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
5814 (XtPointer) bar);
5815 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
5816 (XtPointer) bar);
5817 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
5818 (XtPointer) bar);
5819 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
5820 (XtPointer) bar);
5821 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
5822 (XtPointer) bar);
5823 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
5824 (XtPointer) bar);
5825 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
5826 (XtPointer) bar);
5828 /* Realize the widget. Only after that is the X window created. */
5829 XtRealizeWidget (widget);
5831 /* Set the cursor to an arrow. I didn't find a resource to do that.
5832 And I'm wondering why it hasn't an arrow cursor by default. */
5833 XDefineCursor (XtDisplay (widget), XtWindow (widget),
5834 f->output_data.x->nontext_cursor);
5836 #else /* !USE_MOTIF i.e. use Xaw */
5838 /* Set resources. Create the widget. The background of the
5839 Xaw3d scroll bar widget is a little bit light for my taste.
5840 We don't alter it here to let users change it according
5841 to their taste with `emacs*verticalScrollBar.background: xxx'. */
5842 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5843 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
5844 /* For smoother scrolling with Xaw3d -sm */
5845 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
5847 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5848 if (pixel != -1)
5850 XtSetArg (av[ac], XtNforeground, pixel);
5851 ++ac;
5854 pixel = f->output_data.x->scroll_bar_background_pixel;
5855 if (pixel != -1)
5857 XtSetArg (av[ac], XtNbackground, pixel);
5858 ++ac;
5861 /* Top/bottom shadow colors. */
5863 /* Allocate them, if necessary. */
5864 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
5866 pixel = f->output_data.x->scroll_bar_background_pixel;
5867 if (pixel != -1)
5869 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5870 FRAME_X_COLORMAP (f),
5871 &pixel, 1.2, 0x8000))
5872 pixel = -1;
5873 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
5876 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5878 pixel = f->output_data.x->scroll_bar_background_pixel;
5879 if (pixel != -1)
5881 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5882 FRAME_X_COLORMAP (f),
5883 &pixel, 0.6, 0x4000))
5884 pixel = -1;
5885 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
5889 #ifdef XtNbeNiceToColormap
5890 /* Tell the toolkit about them. */
5891 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
5892 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5893 /* We tried to allocate a color for the top/bottom shadow, and
5894 failed, so tell Xaw3d to use dithering instead. */
5895 /* But only if we have a small colormap. Xaw3d can allocate nice
5896 colors itself. */
5898 XtSetArg (av[ac], XtNbeNiceToColormap,
5899 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
5900 ++ac;
5902 else
5903 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
5904 be more consistent with other emacs 3d colors, and since Xaw3d is
5905 not good at dealing with allocation failure. */
5907 /* This tells Xaw3d to use real colors instead of dithering for
5908 the shadows. */
5909 XtSetArg (av[ac], XtNbeNiceToColormap, False);
5910 ++ac;
5912 /* Specify the colors. */
5913 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
5914 if (pixel != -1)
5916 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
5917 ++ac;
5919 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
5920 if (pixel != -1)
5922 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
5923 ++ac;
5926 #endif
5928 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
5929 f->output_data.x->edit_widget, av, ac);
5932 char const *initial = "";
5933 char const *val = initial;
5934 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
5935 #ifdef XtNarrowScrollbars
5936 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
5937 #endif
5938 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
5939 if (xaw3d_arrow_scroll || val == initial)
5940 { /* ARROW_SCROLL */
5941 xaw3d_arrow_scroll = True;
5942 /* Isn't that just a personal preference ? --Stef */
5943 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
5947 /* Define callbacks. */
5948 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
5949 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
5950 (XtPointer) bar);
5952 /* Realize the widget. Only after that is the X window created. */
5953 XtRealizeWidget (widget);
5955 #endif /* !USE_MOTIF */
5957 /* Install an action hook that lets us detect when the user
5958 finishes interacting with a scroll bar. */
5959 if (action_hook_id == 0)
5960 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
5962 /* Remember X window and widget in the scroll bar vector. */
5963 SET_SCROLL_BAR_X_WIDGET (bar, widget);
5964 xwindow = XtWindow (widget);
5965 bar->x_window = xwindow;
5966 bar->whole = 1;
5967 bar->horizontal = false;
5969 unblock_input ();
5972 static void
5973 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5975 Window xwindow;
5976 Widget widget;
5977 Arg av[20];
5978 int ac = 0;
5979 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
5980 unsigned long pixel;
5982 block_input ();
5984 #ifdef USE_MOTIF
5985 /* Set resources. Create the widget. */
5986 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5987 XtSetArg (av[ac], XmNminimum, 0); ++ac;
5988 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
5989 XtSetArg (av[ac], XmNorientation, XmHORIZONTAL); ++ac;
5990 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_RIGHT), ++ac;
5991 XtSetArg (av[ac], XmNincrement, 1); ++ac;
5992 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
5994 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5995 if (pixel != -1)
5997 XtSetArg (av[ac], XmNforeground, pixel);
5998 ++ac;
6001 pixel = f->output_data.x->scroll_bar_background_pixel;
6002 if (pixel != -1)
6004 XtSetArg (av[ac], XmNbackground, pixel);
6005 ++ac;
6008 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
6009 (char *) scroll_bar_name, av, ac);
6011 /* Add one callback for everything that can happen. */
6012 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
6013 (XtPointer) bar);
6014 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
6015 (XtPointer) bar);
6016 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
6017 (XtPointer) bar);
6018 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
6019 (XtPointer) bar);
6020 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
6021 (XtPointer) bar);
6022 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
6023 (XtPointer) bar);
6024 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
6025 (XtPointer) bar);
6027 /* Realize the widget. Only after that is the X window created. */
6028 XtRealizeWidget (widget);
6030 /* Set the cursor to an arrow. I didn't find a resource to do that.
6031 And I'm wondering why it hasn't an arrow cursor by default. */
6032 XDefineCursor (XtDisplay (widget), XtWindow (widget),
6033 f->output_data.x->nontext_cursor);
6035 #else /* !USE_MOTIF i.e. use Xaw */
6037 /* Set resources. Create the widget. The background of the
6038 Xaw3d scroll bar widget is a little bit light for my taste.
6039 We don't alter it here to let users change it according
6040 to their taste with `emacs*verticalScrollBar.background: xxx'. */
6041 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
6042 XtSetArg (av[ac], XtNorientation, XtorientHorizontal); ++ac;
6043 /* For smoother scrolling with Xaw3d -sm */
6044 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
6046 pixel = f->output_data.x->scroll_bar_foreground_pixel;
6047 if (pixel != -1)
6049 XtSetArg (av[ac], XtNforeground, pixel);
6050 ++ac;
6053 pixel = f->output_data.x->scroll_bar_background_pixel;
6054 if (pixel != -1)
6056 XtSetArg (av[ac], XtNbackground, pixel);
6057 ++ac;
6060 /* Top/bottom shadow colors. */
6062 /* Allocate them, if necessary. */
6063 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
6065 pixel = f->output_data.x->scroll_bar_background_pixel;
6066 if (pixel != -1)
6068 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
6069 FRAME_X_COLORMAP (f),
6070 &pixel, 1.2, 0x8000))
6071 pixel = -1;
6072 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
6075 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
6077 pixel = f->output_data.x->scroll_bar_background_pixel;
6078 if (pixel != -1)
6080 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
6081 FRAME_X_COLORMAP (f),
6082 &pixel, 0.6, 0x4000))
6083 pixel = -1;
6084 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
6088 #ifdef XtNbeNiceToColormap
6089 /* Tell the toolkit about them. */
6090 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
6091 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
6092 /* We tried to allocate a color for the top/bottom shadow, and
6093 failed, so tell Xaw3d to use dithering instead. */
6094 /* But only if we have a small colormap. Xaw3d can allocate nice
6095 colors itself. */
6097 XtSetArg (av[ac], XtNbeNiceToColormap,
6098 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
6099 ++ac;
6101 else
6102 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
6103 be more consistent with other emacs 3d colors, and since Xaw3d is
6104 not good at dealing with allocation failure. */
6106 /* This tells Xaw3d to use real colors instead of dithering for
6107 the shadows. */
6108 XtSetArg (av[ac], XtNbeNiceToColormap, False);
6109 ++ac;
6111 /* Specify the colors. */
6112 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
6113 if (pixel != -1)
6115 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
6116 ++ac;
6118 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
6119 if (pixel != -1)
6121 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
6122 ++ac;
6125 #endif
6127 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
6128 f->output_data.x->edit_widget, av, ac);
6131 char const *initial = "";
6132 char const *val = initial;
6133 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
6134 #ifdef XtNarrowScrollbars
6135 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
6136 #endif
6137 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
6138 if (xaw3d_arrow_scroll || val == initial)
6139 { /* ARROW_SCROLL */
6140 xaw3d_arrow_scroll = True;
6141 /* Isn't that just a personal preference ? --Stef */
6142 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
6146 /* Define callbacks. */
6147 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
6148 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
6149 (XtPointer) bar);
6151 /* Realize the widget. Only after that is the X window created. */
6152 XtRealizeWidget (widget);
6154 #endif /* !USE_MOTIF */
6156 /* Install an action hook that lets us detect when the user
6157 finishes interacting with a scroll bar. */
6158 if (horizontal_action_hook_id == 0)
6159 horizontal_action_hook_id
6160 = XtAppAddActionHook (Xt_app_con, xt_horizontal_action_hook, 0);
6162 /* Remember X window and widget in the scroll bar vector. */
6163 SET_SCROLL_BAR_X_WIDGET (bar, widget);
6164 xwindow = XtWindow (widget);
6165 bar->x_window = xwindow;
6166 bar->whole = 1;
6167 bar->horizontal = true;
6169 unblock_input ();
6171 #endif /* not USE_GTK */
6174 /* Set the thumb size and position of scroll bar BAR. We are currently
6175 displaying PORTION out of a whole WHOLE, and our position POSITION. */
6177 #ifdef USE_GTK
6178 static void
6179 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
6181 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
6184 static void
6185 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
6187 xg_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
6190 #else /* not USE_GTK */
6191 static void
6192 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
6193 int whole)
6195 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6196 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6197 float top, shown;
6199 block_input ();
6201 #ifdef USE_MOTIF
6203 if (scroll_bar_adjust_thumb_portion_p)
6205 /* We use an estimate of 30 chars per line rather than the real
6206 `portion' value. This has the disadvantage that the thumb size
6207 is not very representative, but it makes our life a lot easier.
6208 Otherwise, we have to constantly adjust the thumb size, which
6209 we can't always do quickly enough: while dragging, the size of
6210 the thumb might prevent the user from dragging the thumb all the
6211 way to the end. but Motif and some versions of Xaw3d don't allow
6212 updating the thumb size while dragging. Also, even if we can update
6213 its size, the update will often happen too late.
6214 If you don't believe it, check out revision 1.650 of xterm.c to see
6215 what hoops we were going through and the still poor behavior we got. */
6216 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
6217 /* When the thumb is at the bottom, position == whole.
6218 So we need to increase `whole' to make space for the thumb. */
6219 whole += portion;
6222 if (whole <= 0)
6223 top = 0, shown = 1;
6224 else
6226 top = (float) position / whole;
6227 shown = (float) portion / whole;
6230 if (bar->dragging == -1)
6232 int size, value;
6234 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
6235 is the scroll bar's maximum and MIN is the scroll bar's minimum
6236 value. */
6237 size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
6239 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
6240 value = top * XM_SB_MAX;
6241 value = min (value, XM_SB_MAX - size);
6243 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6245 #else /* !USE_MOTIF i.e. use Xaw */
6247 if (whole == 0)
6248 top = 0, shown = 1;
6249 else
6251 top = (float) position / whole;
6252 shown = (float) portion / whole;
6256 float old_top, old_shown;
6257 Dimension height;
6258 XtVaGetValues (widget,
6259 XtNtopOfThumb, &old_top,
6260 XtNshown, &old_shown,
6261 XtNheight, &height,
6262 NULL);
6264 /* Massage the top+shown values. */
6265 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
6266 top = max (0, min (1, top));
6267 else
6268 top = old_top;
6269 #if ! defined (HAVE_XAW3D)
6270 /* With Xaw, 'top' values too closer to 1.0 may
6271 cause the thumb to disappear. Fix that. */
6272 top = min (top, 0.99f);
6273 #endif
6274 /* Keep two pixels available for moving the thumb down. */
6275 shown = max (0, min (1 - top - (2.0f / height), shown));
6276 #if ! defined (HAVE_XAW3D)
6277 /* Likewise with too small 'shown'. */
6278 shown = max (shown, 0.01f);
6279 #endif
6281 /* If the call to XawScrollbarSetThumb below doesn't seem to
6282 work, check that 'NARROWPROTO' is defined in src/config.h.
6283 If this is not so, most likely you need to fix configure. */
6284 if (top != old_top || shown != old_shown)
6286 if (bar->dragging == -1)
6287 XawScrollbarSetThumb (widget, top, shown);
6288 else
6290 /* Try to make the scrolling a tad smoother. */
6291 if (!xaw3d_pick_top)
6292 shown = min (shown, old_shown);
6294 XawScrollbarSetThumb (widget, top, shown);
6298 #endif /* !USE_MOTIF */
6300 unblock_input ();
6303 static void
6304 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
6305 int whole)
6307 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6308 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6309 float top, shown;
6311 block_input ();
6313 #ifdef USE_MOTIF
6314 bar->whole = whole;
6315 shown = (float) portion / whole;
6316 top = (float) position / (whole - portion);
6318 int size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
6319 int value = clip_to_bounds (0, top * (XM_SB_MAX - size), XM_SB_MAX - size);
6321 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6323 #else /* !USE_MOTIF i.e. use Xaw */
6324 bar->whole = whole;
6325 if (whole == 0)
6326 top = 0, shown = 1;
6327 else
6329 top = (float) position / whole;
6330 shown = (float) portion / whole;
6334 float old_top, old_shown;
6335 Dimension height;
6336 XtVaGetValues (widget,
6337 XtNtopOfThumb, &old_top,
6338 XtNshown, &old_shown,
6339 XtNheight, &height,
6340 NULL);
6342 #if false
6343 /* Massage the top+shown values. */
6344 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
6345 top = max (0, min (1, top));
6346 else
6347 top = old_top;
6348 #if ! defined (HAVE_XAW3D)
6349 /* With Xaw, 'top' values too closer to 1.0 may
6350 cause the thumb to disappear. Fix that. */
6351 top = min (top, 0.99f);
6352 #endif
6353 /* Keep two pixels available for moving the thumb down. */
6354 shown = max (0, min (1 - top - (2.0f / height), shown));
6355 #if ! defined (HAVE_XAW3D)
6356 /* Likewise with too small 'shown'. */
6357 shown = max (shown, 0.01f);
6358 #endif
6359 #endif
6361 /* If the call to XawScrollbarSetThumb below doesn't seem to
6362 work, check that 'NARROWPROTO' is defined in src/config.h.
6363 If this is not so, most likely you need to fix configure. */
6364 XawScrollbarSetThumb (widget, top, shown);
6365 #if false
6366 if (top != old_top || shown != old_shown)
6368 if (bar->dragging == -1)
6369 XawScrollbarSetThumb (widget, top, shown);
6370 else
6372 /* Try to make the scrolling a tad smoother. */
6373 if (!xaw3d_pick_top)
6374 shown = min (shown, old_shown);
6376 XawScrollbarSetThumb (widget, top, shown);
6379 #endif
6381 #endif /* !USE_MOTIF */
6383 unblock_input ();
6385 #endif /* not USE_GTK */
6387 #endif /* USE_TOOLKIT_SCROLL_BARS */
6391 /************************************************************************
6392 Scroll bars, general
6393 ************************************************************************/
6395 /* Create a scroll bar and return the scroll bar vector for it. W is
6396 the Emacs window on which to create the scroll bar. TOP, LEFT,
6397 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
6398 scroll bar. */
6400 static struct scroll_bar *
6401 x_scroll_bar_create (struct window *w, int top, int left,
6402 int width, int height, bool horizontal)
6404 struct frame *f = XFRAME (w->frame);
6405 struct scroll_bar *bar
6406 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
6407 Lisp_Object barobj;
6409 block_input ();
6411 #ifdef USE_TOOLKIT_SCROLL_BARS
6412 if (horizontal)
6413 x_create_horizontal_toolkit_scroll_bar (f, bar);
6414 else
6415 x_create_toolkit_scroll_bar (f, bar);
6416 #else /* not USE_TOOLKIT_SCROLL_BARS */
6418 XSetWindowAttributes a;
6419 unsigned long mask;
6420 Window window;
6422 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
6423 if (a.background_pixel == -1)
6424 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
6426 a.event_mask = (ButtonPressMask | ButtonReleaseMask
6427 | ButtonMotionMask | PointerMotionHintMask
6428 | ExposureMask);
6429 a.cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
6431 mask = (CWBackPixel | CWEventMask | CWCursor);
6433 /* Clear the area of W that will serve as a scroll bar. This is
6434 for the case that a window has been split horizontally. In
6435 this case, no clear_frame is generated to reduce flickering. */
6436 if (width > 0 && window_box_height (w) > 0)
6437 x_clear_area (f, left, top, width, window_box_height (w));
6439 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6440 /* Position and size of scroll bar. */
6441 left, top, width, height,
6442 /* Border width, depth, class, and visual. */
6444 CopyFromParent,
6445 CopyFromParent,
6446 CopyFromParent,
6447 /* Attributes. */
6448 mask, &a);
6449 bar->x_window = window;
6451 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6453 XSETWINDOW (bar->window, w);
6454 bar->top = top;
6455 bar->left = left;
6456 bar->width = width;
6457 bar->height = height;
6458 bar->start = 0;
6459 bar->end = 0;
6460 bar->dragging = -1;
6461 bar->horizontal = horizontal;
6462 #if defined (USE_TOOLKIT_SCROLL_BARS) && defined (USE_LUCID)
6463 bar->last_seen_part = scroll_bar_nowhere;
6464 #endif
6466 /* Add bar to its frame's list of scroll bars. */
6467 bar->next = FRAME_SCROLL_BARS (f);
6468 bar->prev = Qnil;
6469 XSETVECTOR (barobj, bar);
6470 fset_scroll_bars (f, barobj);
6471 if (!NILP (bar->next))
6472 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6474 /* Map the window/widget. */
6475 #ifdef USE_TOOLKIT_SCROLL_BARS
6477 #ifdef USE_GTK
6478 if (horizontal)
6479 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top,
6480 left, width, max (height, 1));
6481 else
6482 xg_update_scrollbar_pos (f, bar->x_window, top,
6483 left, width, max (height, 1));
6484 #else /* not USE_GTK */
6485 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6486 XtConfigureWidget (scroll_bar, left, top, width, max (height, 1), 0);
6487 XtMapWidget (scroll_bar);
6488 #endif /* not USE_GTK */
6490 #else /* not USE_TOOLKIT_SCROLL_BARS */
6491 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
6492 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6494 unblock_input ();
6495 return bar;
6499 #ifndef USE_TOOLKIT_SCROLL_BARS
6501 /* Draw BAR's handle in the proper position.
6503 If the handle is already drawn from START to END, don't bother
6504 redrawing it, unless REBUILD; in that case, always
6505 redraw it. (REBUILD is handy for drawing the handle after expose
6506 events.)
6508 Normally, we want to constrain the start and end of the handle to
6509 fit inside its rectangle, but if the user is dragging the scroll
6510 bar handle, we want to let them drag it down all the way, so that
6511 the bar's top is as far down as it goes; otherwise, there's no way
6512 to move to the very end of the buffer. */
6514 static void
6515 x_scroll_bar_set_handle (struct scroll_bar *bar, int start, int end,
6516 bool rebuild)
6518 bool dragging = bar->dragging != -1;
6519 Window w = bar->x_window;
6520 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6521 GC gc = f->output_data.x->normal_gc;
6523 /* If the display is already accurate, do nothing. */
6524 if (! rebuild
6525 && start == bar->start
6526 && end == bar->end)
6527 return;
6529 block_input ();
6532 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
6533 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
6534 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
6536 /* Make sure the values are reasonable, and try to preserve
6537 the distance between start and end. */
6539 int length = end - start;
6541 if (start < 0)
6542 start = 0;
6543 else if (start > top_range)
6544 start = top_range;
6545 end = start + length;
6547 if (end < start)
6548 end = start;
6549 else if (end > top_range && ! dragging)
6550 end = top_range;
6553 /* Store the adjusted setting in the scroll bar. */
6554 bar->start = start;
6555 bar->end = end;
6557 /* Clip the end position, just for display. */
6558 if (end > top_range)
6559 end = top_range;
6561 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
6562 below top positions, to make sure the handle is always at least
6563 that many pixels tall. */
6564 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
6566 /* Draw the empty space above the handle. Note that we can't clear
6567 zero-height areas; that means "clear to end of window." */
6568 if ((inside_width > 0) && (start > 0))
6569 x_clear_area1 (FRAME_X_DISPLAY (f), w,
6570 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6571 VERTICAL_SCROLL_BAR_TOP_BORDER,
6572 inside_width, start, False);
6574 /* Change to proper foreground color if one is specified. */
6575 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6576 XSetForeground (FRAME_X_DISPLAY (f), gc,
6577 f->output_data.x->scroll_bar_foreground_pixel);
6579 /* Draw the handle itself. */
6580 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
6581 /* x, y, width, height */
6582 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6583 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
6584 inside_width, end - start);
6586 /* Restore the foreground color of the GC if we changed it above. */
6587 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6588 XSetForeground (FRAME_X_DISPLAY (f), gc,
6589 FRAME_FOREGROUND_PIXEL (f));
6591 /* Draw the empty space below the handle. Note that we can't
6592 clear zero-height areas; that means "clear to end of window." */
6593 if ((inside_width > 0) && (end < inside_height))
6594 x_clear_area1 (FRAME_X_DISPLAY (f), w,
6595 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6596 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
6597 inside_width, inside_height - end, False);
6600 unblock_input ();
6603 #endif /* !USE_TOOLKIT_SCROLL_BARS */
6605 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
6606 nil. */
6608 static void
6609 x_scroll_bar_remove (struct scroll_bar *bar)
6611 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6612 block_input ();
6614 #ifdef USE_TOOLKIT_SCROLL_BARS
6615 #ifdef USE_GTK
6616 xg_remove_scroll_bar (f, bar->x_window);
6617 #else /* not USE_GTK */
6618 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
6619 #endif /* not USE_GTK */
6620 #else
6621 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
6622 #endif
6624 /* Dissociate this scroll bar from its window. */
6625 if (bar->horizontal)
6626 wset_horizontal_scroll_bar (XWINDOW (bar->window), Qnil);
6627 else
6628 wset_vertical_scroll_bar (XWINDOW (bar->window), Qnil);
6630 unblock_input ();
6634 /* Set the handle of the vertical scroll bar for WINDOW to indicate
6635 that we are displaying PORTION characters out of a total of WHOLE
6636 characters, starting at POSITION. If WINDOW has no scroll bar,
6637 create one. */
6639 static void
6640 XTset_vertical_scroll_bar (struct window *w, int portion, int whole, int position)
6642 struct frame *f = XFRAME (w->frame);
6643 Lisp_Object barobj;
6644 struct scroll_bar *bar;
6645 int top, height, left, width;
6646 int window_y, window_height;
6648 /* Get window dimensions. */
6649 window_box (w, ANY_AREA, 0, &window_y, 0, &window_height);
6650 top = window_y;
6651 height = window_height;
6652 left = WINDOW_SCROLL_BAR_AREA_X (w);
6653 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
6655 /* Does the scroll bar exist yet? */
6656 if (NILP (w->vertical_scroll_bar))
6658 if (width > 0 && height > 0)
6660 block_input ();
6661 x_clear_area (f, left, top, width, height);
6662 unblock_input ();
6665 bar = x_scroll_bar_create (w, top, left, width, max (height, 1), false);
6667 else
6669 /* It may just need to be moved and resized. */
6670 unsigned int mask = 0;
6672 bar = XSCROLL_BAR (w->vertical_scroll_bar);
6674 block_input ();
6676 if (left != bar->left)
6677 mask |= CWX;
6678 if (top != bar->top)
6679 mask |= CWY;
6680 if (width != bar->width)
6681 mask |= CWWidth;
6682 if (height != bar->height)
6683 mask |= CWHeight;
6685 #ifdef USE_TOOLKIT_SCROLL_BARS
6687 /* Move/size the scroll bar widget. */
6688 if (mask)
6690 /* Since toolkit scroll bars are smaller than the space reserved
6691 for them on the frame, we have to clear "under" them. */
6692 if (width > 0 && height > 0)
6693 x_clear_area (f, left, top, width, height);
6694 #ifdef USE_GTK
6695 xg_update_scrollbar_pos (f, bar->x_window, top,
6696 left, width, max (height, 1));
6697 #else /* not USE_GTK */
6698 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
6699 left, top, width, max (height, 1), 0);
6700 #endif /* not USE_GTK */
6702 #else /* not USE_TOOLKIT_SCROLL_BARS */
6704 /* Move/size the scroll bar window. */
6705 if (mask)
6707 XWindowChanges wc;
6709 wc.x = left;
6710 wc.y = top;
6711 wc.width = width;
6712 wc.height = height;
6713 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
6714 mask, &wc);
6717 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6719 /* Remember new settings. */
6720 bar->left = left;
6721 bar->top = top;
6722 bar->width = width;
6723 bar->height = height;
6725 unblock_input ();
6728 #ifdef USE_TOOLKIT_SCROLL_BARS
6729 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
6730 #else /* not USE_TOOLKIT_SCROLL_BARS */
6731 /* Set the scroll bar's current state, unless we're currently being
6732 dragged. */
6733 if (bar->dragging == -1)
6735 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
6737 if (whole == 0)
6738 x_scroll_bar_set_handle (bar, 0, top_range, false);
6739 else
6741 int start = ((double) position * top_range) / whole;
6742 int end = ((double) (position + portion) * top_range) / whole;
6743 x_scroll_bar_set_handle (bar, start, end, false);
6746 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6748 XSETVECTOR (barobj, bar);
6749 wset_vertical_scroll_bar (w, barobj);
6753 static void
6754 XTset_horizontal_scroll_bar (struct window *w, int portion, int whole, int position)
6756 struct frame *f = XFRAME (w->frame);
6757 Lisp_Object barobj;
6758 struct scroll_bar *bar;
6759 int top, height, left, width;
6760 int window_x, window_width;
6761 int pixel_width = WINDOW_PIXEL_WIDTH (w);
6763 /* Get window dimensions. */
6764 window_box (w, ANY_AREA, &window_x, 0, &window_width, 0);
6765 left = window_x;
6766 width = window_width;
6767 top = WINDOW_SCROLL_BAR_AREA_Y (w);
6768 height = WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
6770 /* Does the scroll bar exist yet? */
6771 if (NILP (w->horizontal_scroll_bar))
6773 if (width > 0 && height > 0)
6775 block_input ();
6777 /* Clear also part between window_width and
6778 WINDOW_PIXEL_WIDTH. */
6779 x_clear_area (f, left, top, pixel_width, height);
6780 unblock_input ();
6783 bar = x_scroll_bar_create (w, top, left, width, height, true);
6785 else
6787 /* It may just need to be moved and resized. */
6788 unsigned int mask = 0;
6790 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
6792 block_input ();
6794 if (left != bar->left)
6795 mask |= CWX;
6796 if (top != bar->top)
6797 mask |= CWY;
6798 if (width != bar->width)
6799 mask |= CWWidth;
6800 if (height != bar->height)
6801 mask |= CWHeight;
6803 #ifdef USE_TOOLKIT_SCROLL_BARS
6804 /* Move/size the scroll bar widget. */
6805 if (mask)
6807 /* Since toolkit scroll bars are smaller than the space reserved
6808 for them on the frame, we have to clear "under" them. */
6809 if (width > 0 && height > 0)
6810 x_clear_area (f,
6811 WINDOW_LEFT_EDGE_X (w), top,
6812 pixel_width - WINDOW_RIGHT_DIVIDER_WIDTH (w), height);
6813 #ifdef USE_GTK
6814 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top, left,
6815 width, height);
6816 #else /* not USE_GTK */
6817 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
6818 left, top, width, height, 0);
6819 #endif /* not USE_GTK */
6821 #else /* not USE_TOOLKIT_SCROLL_BARS */
6823 /* Clear areas not covered by the scroll bar because it's not as
6824 wide as the area reserved for it. This makes sure a
6825 previous mode line display is cleared after C-x 2 C-x 1, for
6826 example. */
6828 int area_height = WINDOW_CONFIG_SCROLL_BAR_HEIGHT (w);
6829 int rest = area_height - height;
6830 if (rest > 0 && width > 0)
6831 x_clear_area (f, left, top, width, rest);
6834 /* Move/size the scroll bar window. */
6835 if (mask)
6837 XWindowChanges wc;
6839 wc.x = left;
6840 wc.y = top;
6841 wc.width = width;
6842 wc.height = height;
6843 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
6844 mask, &wc);
6847 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6849 /* Remember new settings. */
6850 bar->left = left;
6851 bar->top = top;
6852 bar->width = width;
6853 bar->height = height;
6855 unblock_input ();
6858 #ifdef USE_TOOLKIT_SCROLL_BARS
6859 x_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
6860 #else /* not USE_TOOLKIT_SCROLL_BARS */
6861 /* Set the scroll bar's current state, unless we're currently being
6862 dragged. */
6863 if (bar->dragging == -1)
6865 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, width);
6867 if (whole == 0)
6868 x_scroll_bar_set_handle (bar, 0, left_range, false);
6869 else
6871 int start = ((double) position * left_range) / whole;
6872 int end = ((double) (position + portion) * left_range) / whole;
6873 x_scroll_bar_set_handle (bar, start, end, false);
6876 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6878 XSETVECTOR (barobj, bar);
6879 wset_horizontal_scroll_bar (w, barobj);
6883 /* The following three hooks are used when we're doing a thorough
6884 redisplay of the frame. We don't explicitly know which scroll bars
6885 are going to be deleted, because keeping track of when windows go
6886 away is a real pain - "Can you say set-window-configuration, boys
6887 and girls?" Instead, we just assert at the beginning of redisplay
6888 that *all* scroll bars are to be removed, and then save a scroll bar
6889 from the fiery pit when we actually redisplay its window. */
6891 /* Arrange for all scroll bars on FRAME to be removed at the next call
6892 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
6893 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
6895 static void
6896 XTcondemn_scroll_bars (struct frame *frame)
6898 if (!NILP (FRAME_SCROLL_BARS (frame)))
6900 if (!NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
6902 /* Prepend scrollbars to already condemned ones. */
6903 Lisp_Object last = FRAME_SCROLL_BARS (frame);
6905 while (!NILP (XSCROLL_BAR (last)->next))
6906 last = XSCROLL_BAR (last)->next;
6908 XSCROLL_BAR (last)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
6909 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = last;
6912 fset_condemned_scroll_bars (frame, FRAME_SCROLL_BARS (frame));
6913 fset_scroll_bars (frame, Qnil);
6918 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
6919 Note that WINDOW isn't necessarily condemned at all. */
6921 static void
6922 XTredeem_scroll_bar (struct window *w)
6924 struct scroll_bar *bar;
6925 Lisp_Object barobj;
6926 struct frame *f;
6928 /* We can't redeem this window's scroll bar if it doesn't have one. */
6929 if (NILP (w->vertical_scroll_bar) && NILP (w->horizontal_scroll_bar))
6930 emacs_abort ();
6932 if (!NILP (w->vertical_scroll_bar) && WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
6934 bar = XSCROLL_BAR (w->vertical_scroll_bar);
6935 /* Unlink it from the condemned list. */
6936 f = XFRAME (WINDOW_FRAME (w));
6937 if (NILP (bar->prev))
6939 /* If the prev pointer is nil, it must be the first in one of
6940 the lists. */
6941 if (EQ (FRAME_SCROLL_BARS (f), w->vertical_scroll_bar))
6942 /* It's not condemned. Everything's fine. */
6943 goto horizontal;
6944 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
6945 w->vertical_scroll_bar))
6946 fset_condemned_scroll_bars (f, bar->next);
6947 else
6948 /* If its prev pointer is nil, it must be at the front of
6949 one or the other! */
6950 emacs_abort ();
6952 else
6953 XSCROLL_BAR (bar->prev)->next = bar->next;
6955 if (! NILP (bar->next))
6956 XSCROLL_BAR (bar->next)->prev = bar->prev;
6958 bar->next = FRAME_SCROLL_BARS (f);
6959 bar->prev = Qnil;
6960 XSETVECTOR (barobj, bar);
6961 fset_scroll_bars (f, barobj);
6962 if (! NILP (bar->next))
6963 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6966 horizontal:
6967 if (!NILP (w->horizontal_scroll_bar) && WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
6969 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
6970 /* Unlink it from the condemned list. */
6971 f = XFRAME (WINDOW_FRAME (w));
6972 if (NILP (bar->prev))
6974 /* If the prev pointer is nil, it must be the first in one of
6975 the lists. */
6976 if (EQ (FRAME_SCROLL_BARS (f), w->horizontal_scroll_bar))
6977 /* It's not condemned. Everything's fine. */
6978 return;
6979 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
6980 w->horizontal_scroll_bar))
6981 fset_condemned_scroll_bars (f, bar->next);
6982 else
6983 /* If its prev pointer is nil, it must be at the front of
6984 one or the other! */
6985 emacs_abort ();
6987 else
6988 XSCROLL_BAR (bar->prev)->next = bar->next;
6990 if (! NILP (bar->next))
6991 XSCROLL_BAR (bar->next)->prev = bar->prev;
6993 bar->next = FRAME_SCROLL_BARS (f);
6994 bar->prev = Qnil;
6995 XSETVECTOR (barobj, bar);
6996 fset_scroll_bars (f, barobj);
6997 if (! NILP (bar->next))
6998 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
7002 /* Remove all scroll bars on FRAME that haven't been saved since the
7003 last call to `*condemn_scroll_bars_hook'. */
7005 static void
7006 XTjudge_scroll_bars (struct frame *f)
7008 Lisp_Object bar, next;
7010 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
7012 /* Clear out the condemned list now so we won't try to process any
7013 more events on the hapless scroll bars. */
7014 fset_condemned_scroll_bars (f, Qnil);
7016 for (; ! NILP (bar); bar = next)
7018 struct scroll_bar *b = XSCROLL_BAR (bar);
7020 x_scroll_bar_remove (b);
7022 next = b->next;
7023 b->next = b->prev = Qnil;
7026 /* Now there should be no references to the condemned scroll bars,
7027 and they should get garbage-collected. */
7031 #ifndef USE_TOOLKIT_SCROLL_BARS
7032 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
7033 is a no-op when using toolkit scroll bars.
7035 This may be called from a signal handler, so we have to ignore GC
7036 mark bits. */
7038 static void
7039 x_scroll_bar_expose (struct scroll_bar *bar, const XEvent *event)
7041 Window w = bar->x_window;
7042 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7043 GC gc = f->output_data.x->normal_gc;
7045 block_input ();
7047 x_scroll_bar_set_handle (bar, bar->start, bar->end, true);
7049 /* Switch to scroll bar foreground color. */
7050 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
7051 XSetForeground (FRAME_X_DISPLAY (f), gc,
7052 f->output_data.x->scroll_bar_foreground_pixel);
7054 /* Draw a one-pixel border just inside the edges of the scroll bar. */
7055 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
7056 /* x, y, width, height */
7057 0, 0, bar->width - 1, bar->height - 1);
7059 /* Restore the foreground color of the GC if we changed it above. */
7060 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
7061 XSetForeground (FRAME_X_DISPLAY (f), gc,
7062 FRAME_FOREGROUND_PIXEL (f));
7064 unblock_input ();
7067 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7069 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
7070 is set to something other than NO_EVENT, it is enqueued.
7072 This may be called from a signal handler, so we have to ignore GC
7073 mark bits. */
7076 static void
7077 x_scroll_bar_handle_click (struct scroll_bar *bar,
7078 const XEvent *event,
7079 struct input_event *emacs_event)
7081 if (! WINDOWP (bar->window))
7082 emacs_abort ();
7084 emacs_event->kind = (bar->horizontal
7085 ? HORIZONTAL_SCROLL_BAR_CLICK_EVENT
7086 : SCROLL_BAR_CLICK_EVENT);
7087 emacs_event->code = event->xbutton.button - Button1;
7088 emacs_event->modifiers
7089 = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO
7090 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
7091 event->xbutton.state)
7092 | (event->type == ButtonRelease
7093 ? up_modifier
7094 : down_modifier));
7095 emacs_event->frame_or_window = bar->window;
7096 emacs_event->arg = Qnil;
7097 emacs_event->timestamp = event->xbutton.time;
7098 if (bar->horizontal)
7100 int left_range
7101 = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
7102 int x = event->xbutton.x - HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
7104 if (x < 0) x = 0;
7105 if (x > left_range) x = left_range;
7107 if (x < bar->start)
7108 emacs_event->part = scroll_bar_before_handle;
7109 else if (x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
7110 emacs_event->part = scroll_bar_horizontal_handle;
7111 else
7112 emacs_event->part = scroll_bar_after_handle;
7114 #ifndef USE_TOOLKIT_SCROLL_BARS
7115 /* If the user has released the handle, set it to its final position. */
7116 if (event->type == ButtonRelease && bar->dragging != -1)
7118 int new_start = - bar->dragging;
7119 int new_end = new_start + bar->end - bar->start;
7121 x_scroll_bar_set_handle (bar, new_start, new_end, false);
7122 bar->dragging = -1;
7124 #endif
7126 XSETINT (emacs_event->x, left_range);
7127 XSETINT (emacs_event->y, x);
7129 else
7131 int top_range
7132 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
7133 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
7135 if (y < 0) y = 0;
7136 if (y > top_range) y = top_range;
7138 if (y < bar->start)
7139 emacs_event->part = scroll_bar_above_handle;
7140 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
7141 emacs_event->part = scroll_bar_handle;
7142 else
7143 emacs_event->part = scroll_bar_below_handle;
7145 #ifndef USE_TOOLKIT_SCROLL_BARS
7146 /* If the user has released the handle, set it to its final position. */
7147 if (event->type == ButtonRelease && bar->dragging != -1)
7149 int new_start = y - bar->dragging;
7150 int new_end = new_start + bar->end - bar->start;
7152 x_scroll_bar_set_handle (bar, new_start, new_end, false);
7153 bar->dragging = -1;
7155 #endif
7157 XSETINT (emacs_event->x, y);
7158 XSETINT (emacs_event->y, top_range);
7162 #ifndef USE_TOOLKIT_SCROLL_BARS
7164 /* Handle some mouse motion while someone is dragging the scroll bar.
7166 This may be called from a signal handler, so we have to ignore GC
7167 mark bits. */
7169 static void
7170 x_scroll_bar_note_movement (struct scroll_bar *bar,
7171 const XMotionEvent *event)
7173 struct frame *f = XFRAME (XWINDOW (bar->window)->frame);
7174 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
7176 dpyinfo->last_mouse_movement_time = event->time;
7177 dpyinfo->last_mouse_scroll_bar = bar;
7178 f->mouse_moved = true;
7180 /* If we're dragging the bar, display it. */
7181 if (bar->dragging != -1)
7183 /* Where should the handle be now? */
7184 int new_start = event->y - bar->dragging;
7186 if (new_start != bar->start)
7188 int new_end = new_start + bar->end - bar->start;
7190 x_scroll_bar_set_handle (bar, new_start, new_end, false);
7195 #endif /* !USE_TOOLKIT_SCROLL_BARS */
7197 /* Return information to the user about the current position of the mouse
7198 on the scroll bar. */
7200 static void
7201 x_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
7202 enum scroll_bar_part *part, Lisp_Object *x,
7203 Lisp_Object *y, Time *timestamp)
7205 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
7206 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
7207 Window w = bar->x_window;
7208 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7209 int win_x, win_y;
7210 Window dummy_window;
7211 int dummy_coord;
7212 unsigned int dummy_mask;
7214 block_input ();
7216 /* Get the mouse's position relative to the scroll bar window, and
7217 report that. */
7218 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
7220 /* Root, child, root x and root y. */
7221 &dummy_window, &dummy_window,
7222 &dummy_coord, &dummy_coord,
7224 /* Position relative to scroll bar. */
7225 &win_x, &win_y,
7227 /* Mouse buttons and modifier keys. */
7228 &dummy_mask))
7230 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
7232 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
7234 if (bar->dragging != -1)
7235 win_y -= bar->dragging;
7237 if (win_y < 0)
7238 win_y = 0;
7239 if (win_y > top_range)
7240 win_y = top_range;
7242 *fp = f;
7243 *bar_window = bar->window;
7245 if (bar->dragging != -1)
7246 *part = scroll_bar_handle;
7247 else if (win_y < bar->start)
7248 *part = scroll_bar_above_handle;
7249 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
7250 *part = scroll_bar_handle;
7251 else
7252 *part = scroll_bar_below_handle;
7254 XSETINT (*x, win_y);
7255 XSETINT (*y, top_range);
7257 f->mouse_moved = false;
7258 dpyinfo->last_mouse_scroll_bar = NULL;
7259 *timestamp = dpyinfo->last_mouse_movement_time;
7262 unblock_input ();
7266 /* Return information to the user about the current position of the mouse
7267 on the scroll bar. */
7269 static void
7270 x_horizontal_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
7271 enum scroll_bar_part *part, Lisp_Object *x,
7272 Lisp_Object *y, Time *timestamp)
7274 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
7275 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
7276 Window w = bar->x_window;
7277 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7278 int win_x, win_y;
7279 Window dummy_window;
7280 int dummy_coord;
7281 unsigned int dummy_mask;
7283 block_input ();
7285 /* Get the mouse's position relative to the scroll bar window, and
7286 report that. */
7287 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
7289 /* Root, child, root x and root y. */
7290 &dummy_window, &dummy_window,
7291 &dummy_coord, &dummy_coord,
7293 /* Position relative to scroll bar. */
7294 &win_x, &win_y,
7296 /* Mouse buttons and modifier keys. */
7297 &dummy_mask))
7299 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
7301 win_x -= HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
7303 if (bar->dragging != -1)
7304 win_x -= bar->dragging;
7306 if (win_x < 0)
7307 win_x = 0;
7308 if (win_x > left_range)
7309 win_x = left_range;
7311 *fp = f;
7312 *bar_window = bar->window;
7314 if (bar->dragging != -1)
7315 *part = scroll_bar_horizontal_handle;
7316 else if (win_x < bar->start)
7317 *part = scroll_bar_before_handle;
7318 else if (win_x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
7319 *part = scroll_bar_handle;
7320 else
7321 *part = scroll_bar_after_handle;
7323 XSETINT (*y, win_x);
7324 XSETINT (*x, left_range);
7326 f->mouse_moved = false;
7327 dpyinfo->last_mouse_scroll_bar = NULL;
7328 *timestamp = dpyinfo->last_mouse_movement_time;
7331 unblock_input ();
7335 /* The screen has been cleared so we may have changed foreground or
7336 background colors, and the scroll bars may need to be redrawn.
7337 Clear out the scroll bars, and ask for expose events, so we can
7338 redraw them. */
7340 static void
7341 x_scroll_bar_clear (struct frame *f)
7343 #ifndef USE_TOOLKIT_SCROLL_BARS
7344 Lisp_Object bar;
7346 /* We can have scroll bars even if this is 0,
7347 if we just turned off scroll bar mode.
7348 But in that case we should not clear them. */
7349 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
7350 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
7351 bar = XSCROLL_BAR (bar)->next)
7352 XClearArea (FRAME_X_DISPLAY (f),
7353 XSCROLL_BAR (bar)->x_window,
7354 0, 0, 0, 0, True);
7355 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7358 #ifdef ENABLE_CHECKING
7360 /* Record the last 100 characters stored
7361 to help debug the loss-of-chars-during-GC problem. */
7363 static int temp_index;
7364 static short temp_buffer[100];
7366 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
7367 if (temp_index == ARRAYELTS (temp_buffer)) \
7368 temp_index = 0; \
7369 temp_buffer[temp_index++] = (keysym)
7371 #else /* not ENABLE_CHECKING */
7373 #define STORE_KEYSYM_FOR_DEBUG(keysym) ((void)0)
7375 #endif /* ENABLE_CHECKING */
7377 /* Set this to nonzero to fake an "X I/O error"
7378 on a particular display. */
7380 static struct x_display_info *XTread_socket_fake_io_error;
7382 /* When we find no input here, we occasionally do a no-op command
7383 to verify that the X server is still running and we can still talk with it.
7384 We try all the open displays, one by one.
7385 This variable is used for cycling thru the displays. */
7387 static struct x_display_info *next_noop_dpyinfo;
7389 enum
7391 X_EVENT_NORMAL,
7392 X_EVENT_GOTO_OUT,
7393 X_EVENT_DROP
7396 /* Filter events for the current X input method.
7397 DPYINFO is the display this event is for.
7398 EVENT is the X event to filter.
7400 Returns non-zero if the event was filtered, caller shall not process
7401 this event further.
7402 Returns zero if event is wasn't filtered. */
7404 #ifdef HAVE_X_I18N
7405 static int
7406 x_filter_event (struct x_display_info *dpyinfo, XEvent *event)
7408 /* XFilterEvent returns non-zero if the input method has
7409 consumed the event. We pass the frame's X window to
7410 XFilterEvent because that's the one for which the IC
7411 was created. */
7413 struct frame *f1 = x_any_window_to_frame (dpyinfo,
7414 event->xclient.window);
7416 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
7418 #endif
7420 #ifdef USE_GTK
7421 static int current_count;
7422 static int current_finish;
7423 static struct input_event *current_hold_quit;
7425 /* This is the filter function invoked by the GTK event loop.
7426 It is invoked before the XEvent is translated to a GdkEvent,
7427 so we have a chance to act on the event before GTK. */
7428 static GdkFilterReturn
7429 event_handler_gdk (GdkXEvent *gxev, GdkEvent *ev, gpointer data)
7431 XEvent *xev = (XEvent *) gxev;
7433 block_input ();
7434 if (current_count >= 0)
7436 struct x_display_info *dpyinfo;
7438 dpyinfo = x_display_info_for_display (xev->xany.display);
7440 #ifdef HAVE_X_I18N
7441 /* Filter events for the current X input method.
7442 GTK calls XFilterEvent but not for key press and release,
7443 so we do it here. */
7444 if ((xev->type == KeyPress || xev->type == KeyRelease)
7445 && dpyinfo
7446 && x_filter_event (dpyinfo, xev))
7448 unblock_input ();
7449 return GDK_FILTER_REMOVE;
7451 #endif
7453 if (! dpyinfo)
7454 current_finish = X_EVENT_NORMAL;
7455 else
7456 current_count
7457 += handle_one_xevent (dpyinfo, xev, &current_finish,
7458 current_hold_quit);
7460 else
7461 current_finish = x_dispatch_event (xev, xev->xany.display);
7463 unblock_input ();
7465 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
7466 return GDK_FILTER_REMOVE;
7468 return GDK_FILTER_CONTINUE;
7470 #endif /* USE_GTK */
7473 static void xembed_send_message (struct frame *f, Time,
7474 enum xembed_message,
7475 long detail, long data1, long data2);
7477 static void
7478 x_net_wm_state (struct frame *f, Window window)
7480 int value = FULLSCREEN_NONE;
7481 Lisp_Object lval = Qnil;
7482 bool sticky = false;
7484 get_current_wm_state (f, window, &value, &sticky);
7486 switch (value)
7488 case FULLSCREEN_WIDTH:
7489 lval = Qfullwidth;
7490 break;
7491 case FULLSCREEN_HEIGHT:
7492 lval = Qfullheight;
7493 break;
7494 case FULLSCREEN_BOTH:
7495 lval = Qfullboth;
7496 break;
7497 case FULLSCREEN_MAXIMIZED:
7498 lval = Qmaximized;
7499 break;
7502 frame_size_history_add
7503 (f, Qx_net_wm_state, 0, 0,
7504 list2 (get_frame_param (f, Qfullscreen), lval));
7506 store_frame_param (f, Qfullscreen, lval);
7507 /** store_frame_param (f, Qsticky, sticky ? Qt : Qnil); **/
7510 /* Flip back buffers on any frames with undrawn content. */
7511 static void
7512 flush_dirty_back_buffers (void)
7514 block_input ();
7515 Lisp_Object tail, frame;
7516 FOR_EACH_FRAME (tail, frame)
7518 struct frame *f = XFRAME (frame);
7519 if (FRAME_LIVE_P (f) &&
7520 FRAME_X_P (f) &&
7521 FRAME_X_WINDOW (f) &&
7522 !FRAME_GARBAGED_P (f) &&
7523 !buffer_flipping_blocked_p () &&
7524 FRAME_X_NEED_BUFFER_FLIP (f))
7525 show_back_buffer (f);
7527 unblock_input ();
7530 /* Handles the XEvent EVENT on display DPYINFO.
7532 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
7533 *FINISH is zero if caller should continue reading events.
7534 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
7535 *EVENT is unchanged unless we're processing KeyPress event.
7537 We return the number of characters stored into the buffer. */
7539 static int
7540 handle_one_xevent (struct x_display_info *dpyinfo,
7541 const XEvent *event,
7542 int *finish, struct input_event *hold_quit)
7544 union buffered_input_event inev;
7545 int count = 0;
7546 int do_help = 0;
7547 ptrdiff_t nbytes = 0;
7548 struct frame *any, *f = NULL;
7549 struct coding_system coding;
7550 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
7551 /* This holds the state XLookupString needs to implement dead keys
7552 and other tricks known as "compose processing". _X Window System_
7553 says that a portable program can't use this, but Stephen Gildea assures
7554 me that letting the compiler initialize it to zeros will work okay. */
7555 static XComposeStatus compose_status;
7556 XEvent configureEvent;
7557 XEvent next_event;
7559 USE_SAFE_ALLOCA;
7561 *finish = X_EVENT_NORMAL;
7563 EVENT_INIT (inev.ie);
7564 inev.ie.kind = NO_EVENT;
7565 inev.ie.arg = Qnil;
7567 any = x_any_window_to_frame (dpyinfo, event->xany.window);
7569 if (any && any->wait_event_type == event->type)
7570 any->wait_event_type = 0; /* Indicates we got it. */
7572 switch (event->type)
7574 case ClientMessage:
7576 if (event->xclient.message_type == dpyinfo->Xatom_wm_protocols
7577 && event->xclient.format == 32)
7579 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_take_focus)
7581 /* Use the value returned by x_any_window_to_frame
7582 because this could be the shell widget window
7583 if the frame has no title bar. */
7584 f = any;
7585 #ifdef HAVE_X_I18N
7586 /* Not quite sure this is needed -pd */
7587 if (f && FRAME_XIC (f))
7588 XSetICFocus (FRAME_XIC (f));
7589 #endif
7590 #if false
7591 /* Emacs sets WM hints whose `input' field is `true'. This
7592 instructs the WM to set the input focus automatically for
7593 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
7594 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
7595 it has set the focus. So, XSetInputFocus below is not
7596 needed.
7598 The call to XSetInputFocus below has also caused trouble. In
7599 cases where the XSetInputFocus done by the WM and the one
7600 below are temporally close (on a fast machine), the call
7601 below can generate additional FocusIn events which confuse
7602 Emacs. */
7604 /* Since we set WM_TAKE_FOCUS, we must call
7605 XSetInputFocus explicitly. But not if f is null,
7606 since that might be an event for a deleted frame. */
7607 if (f)
7609 Display *d = event->xclient.display;
7610 /* Catch and ignore errors, in case window has been
7611 iconified by a window manager such as GWM. */
7612 x_catch_errors (d);
7613 XSetInputFocus (d, event->xclient.window,
7614 /* The ICCCM says this is
7615 the only valid choice. */
7616 RevertToParent,
7617 event->xclient.data.l[1]);
7618 x_uncatch_errors ();
7620 /* Not certain about handling scroll bars here */
7621 #endif
7622 goto done;
7625 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_save_yourself)
7627 /* Save state modify the WM_COMMAND property to
7628 something which can reinstate us. This notifies
7629 the session manager, who's looking for such a
7630 PropertyNotify. Can restart processing when
7631 a keyboard or mouse event arrives. */
7632 /* If we have a session manager, don't set this.
7633 KDE will then start two Emacsen, one for the
7634 session manager and one for this. */
7635 #ifdef HAVE_X_SM
7636 if (! x_session_have_connection ())
7637 #endif
7639 f = x_top_window_to_frame (dpyinfo,
7640 event->xclient.window);
7641 /* This is just so we only give real data once
7642 for a single Emacs process. */
7643 if (f == SELECTED_FRAME ())
7644 XSetCommand (FRAME_X_DISPLAY (f),
7645 event->xclient.window,
7646 initial_argv, initial_argc);
7647 else if (f)
7648 XSetCommand (FRAME_X_DISPLAY (f),
7649 event->xclient.window,
7650 0, 0);
7652 goto done;
7655 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_delete_window)
7657 f = any;
7658 if (!f)
7659 goto OTHER; /* May be a dialog that is to be removed */
7661 inev.ie.kind = DELETE_WINDOW_EVENT;
7662 XSETFRAME (inev.ie.frame_or_window, f);
7663 goto done;
7666 goto done;
7669 if (event->xclient.message_type == dpyinfo->Xatom_wm_configure_denied)
7670 goto done;
7672 if (event->xclient.message_type == dpyinfo->Xatom_wm_window_moved)
7674 int new_x, new_y;
7675 f = x_window_to_frame (dpyinfo, event->xclient.window);
7677 new_x = event->xclient.data.s[0];
7678 new_y = event->xclient.data.s[1];
7680 if (f)
7682 f->left_pos = new_x;
7683 f->top_pos = new_y;
7685 goto done;
7688 #ifdef X_TOOLKIT_EDITRES
7689 if (event->xclient.message_type == dpyinfo->Xatom_editres)
7691 f = any;
7692 if (f)
7693 _XEditResCheckMessages (f->output_data.x->widget,
7694 NULL, (XEvent *) event, NULL);
7695 goto done;
7697 #endif /* X_TOOLKIT_EDITRES */
7699 if (event->xclient.message_type == dpyinfo->Xatom_DONE
7700 || event->xclient.message_type == dpyinfo->Xatom_PAGE)
7702 /* Ghostview job completed. Kill it. We could
7703 reply with "Next" if we received "Page", but we
7704 currently never do because we are interested in
7705 images, only, which should have 1 page. */
7706 Pixmap pixmap = (Pixmap) event->xclient.data.l[1];
7707 f = x_window_to_frame (dpyinfo, event->xclient.window);
7708 if (!f)
7709 goto OTHER;
7710 x_kill_gs_process (pixmap, f);
7711 expose_frame (f, 0, 0, 0, 0);
7712 goto done;
7715 #ifdef USE_TOOLKIT_SCROLL_BARS
7716 /* Scroll bar callbacks send a ClientMessage from which
7717 we construct an input_event. */
7718 if (event->xclient.message_type == dpyinfo->Xatom_Scrollbar)
7720 x_scroll_bar_to_input_event (event, &inev.ie);
7721 *finish = X_EVENT_GOTO_OUT;
7722 goto done;
7724 else if (event->xclient.message_type == dpyinfo->Xatom_Horizontal_Scrollbar)
7726 x_horizontal_scroll_bar_to_input_event (event, &inev.ie);
7727 *finish = X_EVENT_GOTO_OUT;
7728 goto done;
7730 #endif /* USE_TOOLKIT_SCROLL_BARS */
7732 /* XEmbed messages from the embedder (if any). */
7733 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
7735 enum xembed_message msg = event->xclient.data.l[1];
7736 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
7737 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7739 *finish = X_EVENT_GOTO_OUT;
7740 goto done;
7743 xft_settings_event (dpyinfo, event);
7745 f = any;
7746 if (!f)
7747 goto OTHER;
7748 if (x_handle_dnd_message (f, &event->xclient, dpyinfo, &inev.ie))
7749 *finish = X_EVENT_DROP;
7751 break;
7753 case SelectionNotify:
7754 x_display_set_last_user_time (dpyinfo, event->xselection.time);
7755 #ifdef USE_X_TOOLKIT
7756 if (! x_window_to_frame (dpyinfo, event->xselection.requestor))
7757 goto OTHER;
7758 #endif /* not USE_X_TOOLKIT */
7759 x_handle_selection_notify (&event->xselection);
7760 break;
7762 case SelectionClear: /* Someone has grabbed ownership. */
7763 x_display_set_last_user_time (dpyinfo, event->xselectionclear.time);
7764 #ifdef USE_X_TOOLKIT
7765 if (! x_window_to_frame (dpyinfo, event->xselectionclear.window))
7766 goto OTHER;
7767 #endif /* USE_X_TOOLKIT */
7769 const XSelectionClearEvent *eventp = &event->xselectionclear;
7771 inev.sie.kind = SELECTION_CLEAR_EVENT;
7772 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
7773 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
7774 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
7776 break;
7778 case SelectionRequest: /* Someone wants our selection. */
7779 x_display_set_last_user_time (dpyinfo, event->xselectionrequest.time);
7780 #ifdef USE_X_TOOLKIT
7781 if (!x_window_to_frame (dpyinfo, event->xselectionrequest.owner))
7782 goto OTHER;
7783 #endif /* USE_X_TOOLKIT */
7785 const XSelectionRequestEvent *eventp = &event->xselectionrequest;
7787 inev.sie.kind = SELECTION_REQUEST_EVENT;
7788 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
7789 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
7790 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
7791 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
7792 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
7793 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
7795 break;
7797 case PropertyNotify:
7798 x_display_set_last_user_time (dpyinfo, event->xproperty.time);
7799 f = x_top_window_to_frame (dpyinfo, event->xproperty.window);
7800 if (f && event->xproperty.atom == dpyinfo->Xatom_net_wm_state)
7802 bool not_hidden = x_handle_net_wm_state (f, &event->xproperty);
7803 if (not_hidden && FRAME_ICONIFIED_P (f))
7805 /* Gnome shell does not iconify us when C-z is pressed.
7806 It hides the frame. So if our state says we aren't
7807 hidden anymore, treat it as deiconified. */
7808 SET_FRAME_VISIBLE (f, 1);
7809 SET_FRAME_ICONIFIED (f, false);
7810 f->output_data.x->has_been_visible = true;
7811 inev.ie.kind = DEICONIFY_EVENT;
7812 XSETFRAME (inev.ie.frame_or_window, f);
7814 else if (! not_hidden && ! FRAME_ICONIFIED_P (f))
7816 SET_FRAME_VISIBLE (f, 0);
7817 SET_FRAME_ICONIFIED (f, true);
7818 inev.ie.kind = ICONIFY_EVENT;
7819 XSETFRAME (inev.ie.frame_or_window, f);
7823 x_handle_property_notify (&event->xproperty);
7824 xft_settings_event (dpyinfo, event);
7825 goto OTHER;
7827 case ReparentNotify:
7828 f = x_top_window_to_frame (dpyinfo, event->xreparent.window);
7829 if (f)
7831 f->output_data.x->parent_desc = event->xreparent.parent;
7832 x_real_positions (f, &f->left_pos, &f->top_pos);
7834 /* Perhaps reparented due to a WM restart. Reset this. */
7835 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
7836 FRAME_DISPLAY_INFO (f)->net_supported_window = 0;
7838 x_set_frame_alpha (f);
7840 goto OTHER;
7842 case Expose:
7843 f = x_window_to_frame (dpyinfo, event->xexpose.window);
7844 if (f)
7846 if (!FRAME_VISIBLE_P (f))
7848 block_input ();
7849 SET_FRAME_VISIBLE (f, 1);
7850 SET_FRAME_ICONIFIED (f, false);
7851 if (FRAME_X_DOUBLE_BUFFERED_P (f))
7852 font_drop_xrender_surfaces (f);
7853 f->output_data.x->has_been_visible = true;
7854 SET_FRAME_GARBAGED (f);
7855 unblock_input ();
7857 else if (FRAME_GARBAGED_P (f))
7859 #ifdef USE_GTK
7860 /* Go around the back buffer and manually clear the
7861 window the first time we show it. This way, we avoid
7862 showing users the sanity-defying horror of whatever
7863 GtkWindow is rendering beneath us. We've garbaged
7864 the frame, so we'll redraw the whole thing on next
7865 redisplay anyway. Yuck. */
7866 x_clear_area1 (
7867 FRAME_X_DISPLAY (f),
7868 FRAME_X_WINDOW (f),
7869 event->xexpose.x, event->xexpose.y,
7870 event->xexpose.width, event->xexpose.height,
7872 #endif
7876 if (!FRAME_GARBAGED_P (f))
7878 #ifdef USE_GTK
7879 /* This seems to be needed for GTK 2.6 and later, see
7880 http://debbugs.gnu.org/cgi/bugreport.cgi?bug=15398. */
7881 x_clear_area (f,
7882 event->xexpose.x, event->xexpose.y,
7883 event->xexpose.width, event->xexpose.height);
7884 #endif
7885 expose_frame (f, event->xexpose.x, event->xexpose.y,
7886 event->xexpose.width, event->xexpose.height);
7889 if (!FRAME_GARBAGED_P (f))
7890 show_back_buffer (f);
7892 else
7894 #ifndef USE_TOOLKIT_SCROLL_BARS
7895 struct scroll_bar *bar;
7896 #endif
7897 #if defined USE_LUCID
7898 /* Submenus of the Lucid menu bar aren't widgets
7899 themselves, so there's no way to dispatch events
7900 to them. Recognize this case separately. */
7902 Widget widget = x_window_to_menu_bar (event->xexpose.window);
7903 if (widget)
7904 xlwmenu_redisplay (widget);
7906 #endif /* USE_LUCID */
7908 #ifdef USE_TOOLKIT_SCROLL_BARS
7909 /* Dispatch event to the widget. */
7910 goto OTHER;
7911 #else /* not USE_TOOLKIT_SCROLL_BARS */
7912 bar = x_window_to_scroll_bar (event->xexpose.display,
7913 event->xexpose.window, 2);
7915 if (bar)
7916 x_scroll_bar_expose (bar, event);
7917 #ifdef USE_X_TOOLKIT
7918 else
7919 goto OTHER;
7920 #endif /* USE_X_TOOLKIT */
7921 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7923 break;
7925 case GraphicsExpose: /* This occurs when an XCopyArea's
7926 source area was obscured or not
7927 available. */
7928 f = x_window_to_frame (dpyinfo, event->xgraphicsexpose.drawable);
7929 if (f)
7931 expose_frame (f, event->xgraphicsexpose.x,
7932 event->xgraphicsexpose.y,
7933 event->xgraphicsexpose.width,
7934 event->xgraphicsexpose.height);
7935 show_back_buffer (f);
7937 #ifdef USE_X_TOOLKIT
7938 else
7939 goto OTHER;
7940 #endif /* USE_X_TOOLKIT */
7941 break;
7943 case NoExpose: /* This occurs when an XCopyArea's
7944 source area was completely
7945 available. */
7946 break;
7948 case UnmapNotify:
7949 /* Redo the mouse-highlight after the tooltip has gone. */
7950 if (event->xunmap.window == tip_window)
7952 tip_window = 0;
7953 x_redo_mouse_highlight (dpyinfo);
7956 f = x_top_window_to_frame (dpyinfo, event->xunmap.window);
7957 if (f) /* F may no longer exist if
7958 the frame was deleted. */
7960 bool visible = FRAME_VISIBLE_P (f);
7961 /* While a frame is unmapped, display generation is
7962 disabled; you don't want to spend time updating a
7963 display that won't ever be seen. */
7964 SET_FRAME_VISIBLE (f, 0);
7965 /* We can't distinguish, from the event, whether the window
7966 has become iconified or invisible. So assume, if it
7967 was previously visible, than now it is iconified.
7968 But x_make_frame_invisible clears both
7969 the visible flag and the iconified flag;
7970 and that way, we know the window is not iconified now. */
7971 if (visible || FRAME_ICONIFIED_P (f))
7973 SET_FRAME_ICONIFIED (f, true);
7974 inev.ie.kind = ICONIFY_EVENT;
7975 XSETFRAME (inev.ie.frame_or_window, f);
7978 goto OTHER;
7980 case MapNotify:
7981 /* We use x_top_window_to_frame because map events can
7982 come for sub-windows and they don't mean that the
7983 frame is visible. */
7984 f = x_top_window_to_frame (dpyinfo, event->xmap.window);
7985 if (f)
7987 bool iconified = FRAME_ICONIFIED_P (f);
7989 /* Check if fullscreen was specified before we where mapped the
7990 first time, i.e. from the command line. */
7991 if (!f->output_data.x->has_been_visible)
7992 x_check_fullscreen (f);
7994 SET_FRAME_VISIBLE (f, 1);
7995 SET_FRAME_ICONIFIED (f, false);
7996 f->output_data.x->has_been_visible = true;
7998 if (iconified)
8000 inev.ie.kind = DEICONIFY_EVENT;
8001 XSETFRAME (inev.ie.frame_or_window, f);
8003 else if (! NILP (Vframe_list) && ! NILP (XCDR (Vframe_list)))
8004 /* Force a redisplay sooner or later to update the
8005 frame titles in case this is the second frame. */
8006 record_asynch_buffer_change ();
8008 goto OTHER;
8010 case KeyPress:
8012 x_display_set_last_user_time (dpyinfo, event->xkey.time);
8013 ignore_next_mouse_click_timeout = 0;
8015 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8016 /* Dispatch KeyPress events when in menu. */
8017 if (popup_activated ())
8018 goto OTHER;
8019 #endif
8021 f = any;
8023 /* If mouse-highlight is an integer, input clears out
8024 mouse highlighting. */
8025 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
8026 #if ! defined (USE_GTK)
8027 && (f == 0
8028 || !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
8029 #endif
8032 clear_mouse_face (hlinfo);
8033 hlinfo->mouse_face_hidden = true;
8036 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
8037 if (f == 0)
8039 /* Scroll bars consume key events, but we want
8040 the keys to go to the scroll bar's frame. */
8041 Widget widget = XtWindowToWidget (dpyinfo->display,
8042 event->xkey.window);
8043 if (widget && XmIsScrollBar (widget))
8045 widget = XtParent (widget);
8046 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
8049 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
8051 if (f != 0)
8053 KeySym keysym, orig_keysym;
8054 /* al%imercury@uunet.uu.net says that making this 81
8055 instead of 80 fixed a bug whereby meta chars made
8056 his Emacs hang.
8058 It seems that some version of XmbLookupString has
8059 a bug of not returning XBufferOverflow in
8060 status_return even if the input is too long to
8061 fit in 81 bytes. So, we must prepare sufficient
8062 bytes for copy_buffer. 513 bytes (256 chars for
8063 two-byte character set) seems to be a fairly good
8064 approximation. -- 2000.8.10 handa@etl.go.jp */
8065 unsigned char copy_buffer[513];
8066 unsigned char *copy_bufptr = copy_buffer;
8067 int copy_bufsiz = sizeof (copy_buffer);
8068 int modifiers;
8069 Lisp_Object coding_system = Qlatin_1;
8070 Lisp_Object c;
8071 /* Event will be modified. */
8072 XKeyEvent xkey = event->xkey;
8074 #ifdef USE_GTK
8075 /* Don't pass keys to GTK. A Tab will shift focus to the
8076 tool bar in GTK 2.4. Keys will still go to menus and
8077 dialogs because in that case popup_activated is nonzero
8078 (see above). */
8079 *finish = X_EVENT_DROP;
8080 #endif
8082 xkey.state |= x_emacs_to_x_modifiers (FRAME_DISPLAY_INFO (f),
8083 extra_keyboard_modifiers);
8084 modifiers = xkey.state;
8086 /* This will have to go some day... */
8088 /* make_lispy_event turns chars into control chars.
8089 Don't do it here because XLookupString is too eager. */
8090 xkey.state &= ~ControlMask;
8091 xkey.state &= ~(dpyinfo->meta_mod_mask
8092 | dpyinfo->super_mod_mask
8093 | dpyinfo->hyper_mod_mask
8094 | dpyinfo->alt_mod_mask);
8096 /* In case Meta is ComposeCharacter,
8097 clear its status. According to Markus Ehrnsperger
8098 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
8099 this enables ComposeCharacter to work whether or
8100 not it is combined with Meta. */
8101 if (modifiers & dpyinfo->meta_mod_mask)
8102 memset (&compose_status, 0, sizeof (compose_status));
8104 #ifdef HAVE_X_I18N
8105 if (FRAME_XIC (f))
8107 Status status_return;
8109 coding_system = Vlocale_coding_system;
8110 nbytes = XmbLookupString (FRAME_XIC (f),
8111 &xkey, (char *) copy_bufptr,
8112 copy_bufsiz, &keysym,
8113 &status_return);
8114 if (status_return == XBufferOverflow)
8116 copy_bufsiz = nbytes + 1;
8117 copy_bufptr = alloca (copy_bufsiz);
8118 nbytes = XmbLookupString (FRAME_XIC (f),
8119 &xkey, (char *) copy_bufptr,
8120 copy_bufsiz, &keysym,
8121 &status_return);
8123 /* Xutf8LookupString is a new but already deprecated interface. -stef */
8124 if (status_return == XLookupNone)
8125 break;
8126 else if (status_return == XLookupChars)
8128 keysym = NoSymbol;
8129 modifiers = 0;
8131 else if (status_return != XLookupKeySym
8132 && status_return != XLookupBoth)
8133 emacs_abort ();
8135 else
8136 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
8137 copy_bufsiz, &keysym,
8138 &compose_status);
8139 #else
8140 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
8141 copy_bufsiz, &keysym,
8142 &compose_status);
8143 #endif
8145 /* If not using XIM/XIC, and a compose sequence is in progress,
8146 we break here. Otherwise, chars_matched is always 0. */
8147 if (compose_status.chars_matched > 0 && nbytes == 0)
8148 break;
8150 memset (&compose_status, 0, sizeof (compose_status));
8151 orig_keysym = keysym;
8153 /* Common for all keysym input events. */
8154 XSETFRAME (inev.ie.frame_or_window, f);
8155 inev.ie.modifiers
8156 = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), modifiers);
8157 inev.ie.timestamp = xkey.time;
8159 /* First deal with keysyms which have defined
8160 translations to characters. */
8161 if (keysym >= 32 && keysym < 128)
8162 /* Avoid explicitly decoding each ASCII character. */
8164 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
8165 inev.ie.code = keysym;
8166 goto done_keysym;
8169 /* Keysyms directly mapped to Unicode characters. */
8170 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
8172 if (keysym < 0x01000080)
8173 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
8174 else
8175 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
8176 inev.ie.code = keysym & 0xFFFFFF;
8177 goto done_keysym;
8180 /* Now non-ASCII. */
8181 if (HASH_TABLE_P (Vx_keysym_table)
8182 && (c = Fgethash (make_number (keysym),
8183 Vx_keysym_table,
8184 Qnil),
8185 NATNUMP (c)))
8187 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
8188 ? ASCII_KEYSTROKE_EVENT
8189 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
8190 inev.ie.code = XFASTINT (c);
8191 goto done_keysym;
8194 /* Random non-modifier sorts of keysyms. */
8195 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
8196 || keysym == XK_Delete
8197 #ifdef XK_ISO_Left_Tab
8198 || (keysym >= XK_ISO_Left_Tab
8199 && keysym <= XK_ISO_Enter)
8200 #endif
8201 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
8202 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
8203 #ifdef HPUX
8204 /* This recognizes the "extended function
8205 keys". It seems there's no cleaner way.
8206 Test IsModifierKey to avoid handling
8207 mode_switch incorrectly. */
8208 || (XK_Select <= keysym && keysym < XK_KP_Space)
8209 #endif
8210 #ifdef XK_dead_circumflex
8211 || orig_keysym == XK_dead_circumflex
8212 #endif
8213 #ifdef XK_dead_grave
8214 || orig_keysym == XK_dead_grave
8215 #endif
8216 #ifdef XK_dead_tilde
8217 || orig_keysym == XK_dead_tilde
8218 #endif
8219 #ifdef XK_dead_diaeresis
8220 || orig_keysym == XK_dead_diaeresis
8221 #endif
8222 #ifdef XK_dead_macron
8223 || orig_keysym == XK_dead_macron
8224 #endif
8225 #ifdef XK_dead_degree
8226 || orig_keysym == XK_dead_degree
8227 #endif
8228 #ifdef XK_dead_acute
8229 || orig_keysym == XK_dead_acute
8230 #endif
8231 #ifdef XK_dead_cedilla
8232 || orig_keysym == XK_dead_cedilla
8233 #endif
8234 #ifdef XK_dead_breve
8235 || orig_keysym == XK_dead_breve
8236 #endif
8237 #ifdef XK_dead_ogonek
8238 || orig_keysym == XK_dead_ogonek
8239 #endif
8240 #ifdef XK_dead_caron
8241 || orig_keysym == XK_dead_caron
8242 #endif
8243 #ifdef XK_dead_doubleacute
8244 || orig_keysym == XK_dead_doubleacute
8245 #endif
8246 #ifdef XK_dead_abovedot
8247 || orig_keysym == XK_dead_abovedot
8248 #endif
8249 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
8250 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
8251 /* Any "vendor-specific" key is ok. */
8252 || (orig_keysym & (1 << 28))
8253 || (keysym != NoSymbol && nbytes == 0))
8254 && ! (IsModifierKey (orig_keysym)
8255 /* The symbols from XK_ISO_Lock
8256 to XK_ISO_Last_Group_Lock
8257 don't have real modifiers but
8258 should be treated similarly to
8259 Mode_switch by Emacs. */
8260 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
8261 || (XK_ISO_Lock <= orig_keysym
8262 && orig_keysym <= XK_ISO_Last_Group_Lock)
8263 #endif
8266 STORE_KEYSYM_FOR_DEBUG (keysym);
8267 /* make_lispy_event will convert this to a symbolic
8268 key. */
8269 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
8270 inev.ie.code = keysym;
8271 goto done_keysym;
8274 { /* Raw bytes, not keysym. */
8275 ptrdiff_t i;
8276 int nchars, len;
8278 for (i = 0, nchars = 0; i < nbytes; i++)
8280 if (ASCII_CHAR_P (copy_bufptr[i]))
8281 nchars++;
8282 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
8285 if (nchars < nbytes)
8287 /* Decode the input data. */
8289 /* The input should be decoded with `coding_system'
8290 which depends on which X*LookupString function
8291 we used just above and the locale. */
8292 setup_coding_system (coding_system, &coding);
8293 coding.src_multibyte = false;
8294 coding.dst_multibyte = true;
8295 /* The input is converted to events, thus we can't
8296 handle composition. Anyway, there's no XIM that
8297 gives us composition information. */
8298 coding.common_flags &= ~CODING_ANNOTATION_MASK;
8300 SAFE_NALLOCA (coding.destination, MAX_MULTIBYTE_LENGTH,
8301 nbytes);
8302 coding.dst_bytes = MAX_MULTIBYTE_LENGTH * nbytes;
8303 coding.mode |= CODING_MODE_LAST_BLOCK;
8304 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
8305 nbytes = coding.produced;
8306 nchars = coding.produced_char;
8307 copy_bufptr = coding.destination;
8310 /* Convert the input data to a sequence of
8311 character events. */
8312 for (i = 0; i < nbytes; i += len)
8314 int ch;
8315 if (nchars == nbytes)
8316 ch = copy_bufptr[i], len = 1;
8317 else
8318 ch = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
8319 inev.ie.kind = (SINGLE_BYTE_CHAR_P (ch)
8320 ? ASCII_KEYSTROKE_EVENT
8321 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
8322 inev.ie.code = ch;
8323 kbd_buffer_store_buffered_event (&inev, hold_quit);
8326 count += nchars;
8328 inev.ie.kind = NO_EVENT; /* Already stored above. */
8330 if (keysym == NoSymbol)
8331 break;
8333 /* FIXME: check side effects and remove this. */
8334 ((XEvent *) event)->xkey = xkey;
8336 done_keysym:
8337 #ifdef HAVE_X_I18N
8338 /* Don't dispatch this event since XtDispatchEvent calls
8339 XFilterEvent, and two calls in a row may freeze the
8340 client. */
8341 break;
8342 #else
8343 goto OTHER;
8344 #endif
8346 case KeyRelease:
8347 x_display_set_last_user_time (dpyinfo, event->xkey.time);
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 EnterNotify:
8358 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
8359 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8361 f = any;
8363 if (f && x_mouse_click_focus_ignore_position)
8364 ignore_next_mouse_click_timeout = event->xmotion.time + 200;
8366 /* EnterNotify counts as mouse movement,
8367 so update things that depend on mouse position. */
8368 if (f && !f->output_data.x->hourglass_p)
8369 note_mouse_movement (f, &event->xmotion);
8370 #ifdef USE_GTK
8371 /* We may get an EnterNotify on the buttons in the toolbar. In that
8372 case we moved out of any highlighted area and need to note this. */
8373 if (!f && dpyinfo->last_mouse_glyph_frame)
8374 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
8375 #endif
8376 goto OTHER;
8378 case FocusIn:
8379 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8380 goto OTHER;
8382 case LeaveNotify:
8383 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
8384 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8386 f = x_top_window_to_frame (dpyinfo, event->xcrossing.window);
8387 if (f)
8389 if (f == hlinfo->mouse_face_mouse_frame)
8391 /* If we move outside the frame, then we're
8392 certainly no longer on any text in the frame. */
8393 clear_mouse_face (hlinfo);
8394 hlinfo->mouse_face_mouse_frame = 0;
8397 /* Generate a nil HELP_EVENT to cancel a help-echo.
8398 Do it only if there's something to cancel.
8399 Otherwise, the startup message is cleared when
8400 the mouse leaves the frame. */
8401 if (any_help_event_p)
8402 do_help = -1;
8404 #ifdef USE_GTK
8405 /* See comment in EnterNotify above */
8406 else if (dpyinfo->last_mouse_glyph_frame)
8407 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
8408 #endif
8409 goto OTHER;
8411 case FocusOut:
8412 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8413 goto OTHER;
8415 case MotionNotify:
8417 x_display_set_last_user_time (dpyinfo, event->xmotion.time);
8418 previous_help_echo_string = help_echo_string;
8419 help_echo_string = Qnil;
8421 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
8422 : x_window_to_frame (dpyinfo, event->xmotion.window));
8424 if (hlinfo->mouse_face_hidden)
8426 hlinfo->mouse_face_hidden = false;
8427 clear_mouse_face (hlinfo);
8430 #ifdef USE_GTK
8431 if (f && xg_event_is_for_scrollbar (f, event))
8432 f = 0;
8433 #endif
8434 if (f)
8437 /* Generate SELECT_WINDOW_EVENTs when needed.
8438 Don't let popup menus influence things (bug#1261). */
8439 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
8441 static Lisp_Object last_mouse_window;
8442 Lisp_Object window = window_from_coordinates
8443 (f, event->xmotion.x, event->xmotion.y, 0, false);
8445 /* Window will be selected only when it is not selected now and
8446 last mouse movement event was not in it. Minibuffer window
8447 will be selected only when it is active. */
8448 if (WINDOWP (window)
8449 && !EQ (window, last_mouse_window)
8450 && !EQ (window, selected_window)
8451 /* For click-to-focus window managers
8452 create event iff we don't leave the
8453 selected frame. */
8454 && (focus_follows_mouse
8455 || (EQ (XWINDOW (window)->frame,
8456 XWINDOW (selected_window)->frame))))
8458 inev.ie.kind = SELECT_WINDOW_EVENT;
8459 inev.ie.frame_or_window = window;
8461 /* Remember the last window where we saw the mouse. */
8462 last_mouse_window = window;
8464 if (!note_mouse_movement (f, &event->xmotion))
8465 help_echo_string = previous_help_echo_string;
8467 else
8469 #ifndef USE_TOOLKIT_SCROLL_BARS
8470 struct scroll_bar *bar
8471 = x_window_to_scroll_bar (event->xmotion.display,
8472 event->xmotion.window, 2);
8474 if (bar)
8475 x_scroll_bar_note_movement (bar, &event->xmotion);
8476 #endif /* USE_TOOLKIT_SCROLL_BARS */
8478 /* If we move outside the frame, then we're
8479 certainly no longer on any text in the frame. */
8480 clear_mouse_face (hlinfo);
8483 /* If the contents of the global variable help_echo_string
8484 has changed, generate a HELP_EVENT. */
8485 if (!NILP (help_echo_string)
8486 || !NILP (previous_help_echo_string))
8487 do_help = 1;
8488 goto OTHER;
8491 case ConfigureNotify:
8492 /* An opaque move can generate a stream of events as the window
8493 is dragged around. If the connection round trip time isn't
8494 really short, they may come faster than we can respond to
8495 them, given the multiple queries we can do to check window
8496 manager state, translate coordinates, etc.
8498 So if this ConfigureNotify is immediately followed by another
8499 for the same window, use the info from the latest update, and
8500 consider the events all handled. */
8501 /* Opaque resize may be trickier; ConfigureNotify events are
8502 mixed with Expose events for multiple windows. */
8503 configureEvent = *event;
8504 while (XPending (dpyinfo->display))
8506 XNextEvent (dpyinfo->display, &next_event);
8507 if (next_event.type != ConfigureNotify
8508 || next_event.xconfigure.window != event->xconfigure.window
8509 /* Skipping events with different sizes can lead to a
8510 mispositioned mode line at initial window creation.
8511 Only drop window motion events for now. */
8512 || next_event.xconfigure.width != event->xconfigure.width
8513 || next_event.xconfigure.height != event->xconfigure.height)
8515 XPutBackEvent (dpyinfo->display, &next_event);
8516 break;
8518 else
8519 configureEvent = next_event;
8522 f = x_top_window_to_frame (dpyinfo, configureEvent.xconfigure.window);
8523 /* Unfortunately, we need to call font_drop_xrender_surfaces for
8524 _all_ ConfigureNotify events, otherwise we miss some and
8525 flicker. Don't try to optimize these calls by looking only
8526 for size changes: that's not sufficient. We miss some
8527 surface invalidations and flicker. */
8528 block_input ();
8529 if (f && FRAME_X_DOUBLE_BUFFERED_P (f))
8530 font_drop_xrender_surfaces (f);
8531 unblock_input ();
8532 #ifdef USE_CAIRO
8533 if (f) x_cr_destroy_surface (f);
8534 #endif
8535 #ifdef USE_GTK
8536 if (!f
8537 && (f = any)
8538 && configureEvent.xconfigure.window == FRAME_X_WINDOW (f))
8540 block_input ();
8541 if (FRAME_X_DOUBLE_BUFFERED_P (f))
8542 font_drop_xrender_surfaces (f);
8543 unblock_input ();
8544 xg_frame_resized (f, configureEvent.xconfigure.width,
8545 configureEvent.xconfigure.height);
8546 #ifdef USE_CAIRO
8547 x_cr_destroy_surface (f);
8548 #endif
8549 f = 0;
8551 #endif
8552 if (f)
8555 x_net_wm_state (f, configureEvent.xconfigure.window);
8557 #ifdef USE_X_TOOLKIT
8558 /* Tip frames are pure X window, set size for them. */
8559 if (! NILP (tip_frame) && XFRAME (tip_frame) == f)
8561 if (FRAME_PIXEL_HEIGHT (f) != configureEvent.xconfigure.height
8562 || FRAME_PIXEL_WIDTH (f) != configureEvent.xconfigure.width)
8564 SET_FRAME_GARBAGED (f);
8566 FRAME_PIXEL_HEIGHT (f) = configureEvent.xconfigure.height;
8567 FRAME_PIXEL_WIDTH (f) = configureEvent.xconfigure.width;
8569 #endif
8571 #ifndef USE_X_TOOLKIT
8572 #ifndef USE_GTK
8573 int width =
8574 FRAME_PIXEL_TO_TEXT_WIDTH (f, configureEvent.xconfigure.width);
8575 int height =
8576 FRAME_PIXEL_TO_TEXT_HEIGHT (f, configureEvent.xconfigure.height);
8578 /* In the toolkit version, change_frame_size
8579 is called by the code that handles resizing
8580 of the EmacsFrame widget. */
8582 /* Even if the number of character rows and columns has
8583 not changed, the font size may have changed, so we need
8584 to check the pixel dimensions as well. */
8585 if (width != FRAME_TEXT_WIDTH (f)
8586 || height != FRAME_TEXT_HEIGHT (f)
8587 || configureEvent.xconfigure.width != FRAME_PIXEL_WIDTH (f)
8588 || configureEvent.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
8590 change_frame_size (f, width, height, false, true, false, true);
8591 x_clear_under_internal_border (f);
8592 SET_FRAME_GARBAGED (f);
8593 cancel_mouse_face (f);
8595 #endif /* not USE_GTK */
8596 #endif
8598 #ifdef USE_GTK
8599 /* GTK creates windows but doesn't map them.
8600 Only get real positions when mapped. */
8601 if (FRAME_GTK_OUTER_WIDGET (f)
8602 && gtk_widget_get_mapped (FRAME_GTK_OUTER_WIDGET (f)))
8603 #endif
8604 x_real_positions (f, &f->left_pos, &f->top_pos);
8606 #ifdef HAVE_X_I18N
8607 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
8608 xic_set_statusarea (f);
8609 #endif
8612 goto OTHER;
8614 case ButtonRelease:
8615 case ButtonPress:
8617 /* If we decide we want to generate an event to be seen
8618 by the rest of Emacs, we put it here. */
8619 bool tool_bar_p = false;
8621 memset (&compose_status, 0, sizeof (compose_status));
8622 dpyinfo->last_mouse_glyph_frame = NULL;
8623 x_display_set_last_user_time (dpyinfo, event->xbutton.time);
8625 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
8626 : x_window_to_frame (dpyinfo, event->xbutton.window));
8628 #ifdef USE_GTK
8629 if (f && xg_event_is_for_scrollbar (f, event))
8630 f = 0;
8631 #endif
8632 if (f)
8634 #if ! defined (USE_GTK)
8635 /* Is this in the tool-bar? */
8636 if (WINDOWP (f->tool_bar_window)
8637 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
8639 Lisp_Object window;
8640 int x = event->xbutton.x;
8641 int y = event->xbutton.y;
8643 window = window_from_coordinates (f, x, y, 0, true);
8644 tool_bar_p = EQ (window, f->tool_bar_window);
8646 if (tool_bar_p && event->xbutton.button < 4)
8647 handle_tool_bar_click
8648 (f, x, y, event->xbutton.type == ButtonPress,
8649 x_x_to_emacs_modifiers (dpyinfo, event->xbutton.state));
8651 #endif /* !USE_GTK */
8653 if (!tool_bar_p)
8654 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8655 if (! popup_activated ())
8656 #endif
8658 if (ignore_next_mouse_click_timeout)
8660 if (event->type == ButtonPress
8661 && event->xbutton.time > ignore_next_mouse_click_timeout)
8663 ignore_next_mouse_click_timeout = 0;
8664 construct_mouse_click (&inev.ie, &event->xbutton, f);
8666 if (event->type == ButtonRelease)
8667 ignore_next_mouse_click_timeout = 0;
8669 else
8670 construct_mouse_click (&inev.ie, &event->xbutton, f);
8672 if (FRAME_X_EMBEDDED_P (f))
8673 xembed_send_message (f, event->xbutton.time,
8674 XEMBED_REQUEST_FOCUS, 0, 0, 0);
8676 else
8678 struct scroll_bar *bar
8679 = x_window_to_scroll_bar (event->xbutton.display,
8680 event->xbutton.window, 2);
8682 #ifdef USE_TOOLKIT_SCROLL_BARS
8683 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
8684 scroll bars. */
8685 if (bar && event->xbutton.state & ControlMask)
8687 x_scroll_bar_handle_click (bar, event, &inev.ie);
8688 *finish = X_EVENT_DROP;
8690 #else /* not USE_TOOLKIT_SCROLL_BARS */
8691 if (bar)
8692 x_scroll_bar_handle_click (bar, event, &inev.ie);
8693 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8696 if (event->type == ButtonPress)
8698 dpyinfo->grabbed |= (1 << event->xbutton.button);
8699 dpyinfo->last_mouse_frame = f;
8700 #if ! defined (USE_GTK)
8701 if (f && !tool_bar_p)
8702 f->last_tool_bar_item = -1;
8703 #endif /* not USE_GTK */
8705 else
8706 dpyinfo->grabbed &= ~(1 << event->xbutton.button);
8708 /* Ignore any mouse motion that happened before this event;
8709 any subsequent mouse-movement Emacs events should reflect
8710 only motion after the ButtonPress/Release. */
8711 if (f != 0)
8712 f->mouse_moved = false;
8714 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8715 f = x_menubar_window_to_frame (dpyinfo, event);
8716 /* For a down-event in the menu bar,
8717 don't pass it to Xt right now.
8718 Instead, save it away
8719 and we will pass it to Xt from kbd_buffer_get_event.
8720 That way, we can run some Lisp code first. */
8721 if (! popup_activated ()
8722 #ifdef USE_GTK
8723 /* Gtk+ menus only react to the first three buttons. */
8724 && event->xbutton.button < 3
8725 #endif
8726 && f && event->type == ButtonPress
8727 /* Verify the event is really within the menu bar
8728 and not just sent to it due to grabbing. */
8729 && event->xbutton.x >= 0
8730 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
8731 && event->xbutton.y >= 0
8732 && event->xbutton.y < FRAME_MENUBAR_HEIGHT (f)
8733 && event->xbutton.same_screen)
8735 if (!f->output_data.x->saved_menu_event)
8736 f->output_data.x->saved_menu_event = xmalloc (sizeof *event);
8737 *f->output_data.x->saved_menu_event = *event;
8738 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT;
8739 XSETFRAME (inev.ie.frame_or_window, f);
8740 *finish = X_EVENT_DROP;
8742 else
8743 goto OTHER;
8744 #endif /* USE_X_TOOLKIT || USE_GTK */
8746 break;
8748 case CirculateNotify:
8749 goto OTHER;
8751 case CirculateRequest:
8752 goto OTHER;
8754 case VisibilityNotify:
8755 goto OTHER;
8757 case MappingNotify:
8758 /* Someone has changed the keyboard mapping - update the
8759 local cache. */
8760 switch (event->xmapping.request)
8762 case MappingModifier:
8763 x_find_modifier_meanings (dpyinfo);
8764 /* This is meant to fall through. */
8765 case MappingKeyboard:
8766 XRefreshKeyboardMapping ((XMappingEvent *) &event->xmapping);
8768 goto OTHER;
8770 case DestroyNotify:
8771 xft_settings_event (dpyinfo, event);
8772 break;
8774 default:
8775 OTHER:
8776 #ifdef USE_X_TOOLKIT
8777 block_input ();
8778 if (*finish != X_EVENT_DROP)
8779 XtDispatchEvent ((XEvent *) event);
8780 unblock_input ();
8781 #endif /* USE_X_TOOLKIT */
8782 break;
8785 done:
8786 if (inev.ie.kind != NO_EVENT)
8788 kbd_buffer_store_buffered_event (&inev, hold_quit);
8789 count++;
8792 if (do_help
8793 && !(hold_quit && hold_quit->kind != NO_EVENT))
8795 Lisp_Object frame;
8797 if (f)
8798 XSETFRAME (frame, f);
8799 else
8800 frame = Qnil;
8802 if (do_help > 0)
8804 any_help_event_p = true;
8805 gen_help_event (help_echo_string, frame, help_echo_window,
8806 help_echo_object, help_echo_pos);
8808 else
8810 help_echo_string = Qnil;
8811 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
8813 count++;
8816 /* Sometimes event processing draws to the frame outside redisplay.
8817 To ensure that these changes become visible, draw them here. */
8818 flush_dirty_back_buffers ();
8819 SAFE_FREE ();
8820 return count;
8823 /* Handles the XEvent EVENT on display DISPLAY.
8824 This is used for event loops outside the normal event handling,
8825 i.e. looping while a popup menu or a dialog is posted.
8827 Returns the value handle_one_xevent sets in the finish argument. */
8829 x_dispatch_event (XEvent *event, Display *display)
8831 struct x_display_info *dpyinfo;
8832 int finish = X_EVENT_NORMAL;
8834 dpyinfo = x_display_info_for_display (display);
8836 if (dpyinfo)
8837 handle_one_xevent (dpyinfo, event, &finish, 0);
8839 return finish;
8842 /* Read events coming from the X server.
8843 Return as soon as there are no more events to be read.
8845 Return the number of characters stored into the buffer,
8846 thus pretending to be `read' (except the characters we store
8847 in the keyboard buffer can be multibyte, so are not necessarily
8848 C chars). */
8850 static int
8851 XTread_socket (struct terminal *terminal, struct input_event *hold_quit)
8853 int count = 0;
8854 bool event_found = false;
8855 struct x_display_info *dpyinfo = terminal->display_info.x;
8857 block_input ();
8859 /* For debugging, this gives a way to fake an I/O error. */
8860 if (dpyinfo == XTread_socket_fake_io_error)
8862 XTread_socket_fake_io_error = 0;
8863 x_io_error_quitter (dpyinfo->display);
8866 #ifndef USE_GTK
8867 while (XPending (dpyinfo->display))
8869 int finish;
8870 XEvent event;
8872 XNextEvent (dpyinfo->display, &event);
8874 #ifdef HAVE_X_I18N
8875 /* Filter events for the current X input method. */
8876 if (x_filter_event (dpyinfo, &event))
8877 continue;
8878 #endif
8879 event_found = true;
8881 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
8883 if (finish == X_EVENT_GOTO_OUT)
8884 break;
8887 #else /* USE_GTK */
8889 /* For GTK we must use the GTK event loop. But XEvents gets passed
8890 to our filter function above, and then to the big event switch.
8891 We use a bunch of globals to communicate with our filter function,
8892 that is kind of ugly, but it works.
8894 There is no way to do one display at the time, GTK just does events
8895 from all displays. */
8897 while (gtk_events_pending ())
8899 current_count = count;
8900 current_hold_quit = hold_quit;
8902 gtk_main_iteration ();
8904 count = current_count;
8905 current_count = -1;
8906 current_hold_quit = 0;
8908 if (current_finish == X_EVENT_GOTO_OUT)
8909 break;
8911 #endif /* USE_GTK */
8913 /* On some systems, an X bug causes Emacs to get no more events
8914 when the window is destroyed. Detect that. (1994.) */
8915 if (! event_found)
8917 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
8918 One XNOOP in 100 loops will make Emacs terminate.
8919 B. Bretthauer, 1994 */
8920 x_noop_count++;
8921 if (x_noop_count >= 100)
8923 x_noop_count=0;
8925 if (next_noop_dpyinfo == 0)
8926 next_noop_dpyinfo = x_display_list;
8928 XNoOp (next_noop_dpyinfo->display);
8930 /* Each time we get here, cycle through the displays now open. */
8931 next_noop_dpyinfo = next_noop_dpyinfo->next;
8935 /* If the focus was just given to an auto-raising frame,
8936 raise it now. FIXME: handle more than one such frame. */
8937 if (dpyinfo->x_pending_autoraise_frame)
8939 x_raise_frame (dpyinfo->x_pending_autoraise_frame);
8940 dpyinfo->x_pending_autoraise_frame = NULL;
8943 unblock_input ();
8945 return count;
8951 /***********************************************************************
8952 Text Cursor
8953 ***********************************************************************/
8955 /* Set clipping for output in glyph row ROW. W is the window in which
8956 we operate. GC is the graphics context to set clipping in.
8958 ROW may be a text row or, e.g., a mode line. Text rows must be
8959 clipped to the interior of the window dedicated to text display,
8960 mode lines must be clipped to the whole window. */
8962 static void
8963 x_clip_to_row (struct window *w, struct glyph_row *row,
8964 enum glyph_row_area area, GC gc)
8966 struct frame *f = XFRAME (WINDOW_FRAME (w));
8967 XRectangle clip_rect;
8968 int window_x, window_y, window_width;
8970 window_box (w, area, &window_x, &window_y, &window_width, 0);
8972 clip_rect.x = window_x;
8973 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
8974 clip_rect.y = max (clip_rect.y, window_y);
8975 clip_rect.width = window_width;
8976 clip_rect.height = row->visible_height;
8978 x_set_clip_rectangles (f, gc, &clip_rect, 1);
8982 /* Draw a hollow box cursor on window W in glyph row ROW. */
8984 static void
8985 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
8987 struct frame *f = XFRAME (WINDOW_FRAME (w));
8988 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8989 Display *dpy = FRAME_X_DISPLAY (f);
8990 int x, y, wd, h;
8991 XGCValues xgcv;
8992 struct glyph *cursor_glyph;
8993 GC gc;
8995 /* Get the glyph the cursor is on. If we can't tell because
8996 the current matrix is invalid or such, give up. */
8997 cursor_glyph = get_phys_cursor_glyph (w);
8998 if (cursor_glyph == NULL)
8999 return;
9001 /* Compute frame-relative coordinates for phys cursor. */
9002 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
9003 wd = w->phys_cursor_width - 1;
9005 /* The foreground of cursor_gc is typically the same as the normal
9006 background color, which can cause the cursor box to be invisible. */
9007 xgcv.foreground = f->output_data.x->cursor_pixel;
9008 if (dpyinfo->scratch_cursor_gc)
9009 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
9010 else
9011 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_DRAWABLE (f),
9012 GCForeground, &xgcv);
9013 gc = dpyinfo->scratch_cursor_gc;
9015 /* When on R2L character, show cursor at the right edge of the
9016 glyph, unless the cursor box is as wide as the glyph or wider
9017 (the latter happens when x-stretch-cursor is non-nil). */
9018 if ((cursor_glyph->resolved_level & 1) != 0
9019 && cursor_glyph->pixel_width > wd)
9021 x += cursor_glyph->pixel_width - wd;
9022 if (wd > 0)
9023 wd -= 1;
9025 /* Set clipping, draw the rectangle, and reset clipping again. */
9026 x_clip_to_row (w, row, TEXT_AREA, gc);
9027 x_draw_rectangle (f, gc, x, y, wd, h - 1);
9028 x_reset_clip_rectangles (f, gc);
9032 /* Draw a bar cursor on window W in glyph row ROW.
9034 Implementation note: One would like to draw a bar cursor with an
9035 angle equal to the one given by the font property XA_ITALIC_ANGLE.
9036 Unfortunately, I didn't find a font yet that has this property set.
9037 --gerd. */
9039 static void
9040 x_draw_bar_cursor (struct window *w, struct glyph_row *row, int width, enum text_cursor_kinds kind)
9042 struct frame *f = XFRAME (w->frame);
9043 struct glyph *cursor_glyph;
9045 /* If cursor is out of bounds, don't draw garbage. This can happen
9046 in mini-buffer windows when switching between echo area glyphs
9047 and mini-buffer. */
9048 cursor_glyph = get_phys_cursor_glyph (w);
9049 if (cursor_glyph == NULL)
9050 return;
9052 /* Experimental avoidance of cursor on xwidget. */
9053 if (cursor_glyph->type == XWIDGET_GLYPH)
9054 return;
9056 /* If on an image, draw like a normal cursor. That's usually better
9057 visible than drawing a bar, esp. if the image is large so that
9058 the bar might not be in the window. */
9059 if (cursor_glyph->type == IMAGE_GLYPH)
9061 struct glyph_row *r;
9062 r = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
9063 draw_phys_cursor_glyph (w, r, DRAW_CURSOR);
9065 else
9067 Display *dpy = FRAME_X_DISPLAY (f);
9068 Drawable drawable = FRAME_X_DRAWABLE (f);
9069 GC gc = FRAME_DISPLAY_INFO (f)->scratch_cursor_gc;
9070 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
9071 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
9072 XGCValues xgcv;
9074 /* If the glyph's background equals the color we normally draw
9075 the bars cursor in, the bar cursor in its normal color is
9076 invisible. Use the glyph's foreground color instead in this
9077 case, on the assumption that the glyph's colors are chosen so
9078 that the glyph is legible. */
9079 if (face->background == f->output_data.x->cursor_pixel)
9080 xgcv.background = xgcv.foreground = face->foreground;
9081 else
9082 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
9083 xgcv.graphics_exposures = False;
9085 if (gc)
9086 XChangeGC (dpy, gc, mask, &xgcv);
9087 else
9089 gc = XCreateGC (dpy, drawable, mask, &xgcv);
9090 FRAME_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
9093 x_clip_to_row (w, row, TEXT_AREA, gc);
9095 if (kind == BAR_CURSOR)
9097 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9099 if (width < 0)
9100 width = FRAME_CURSOR_WIDTH (f);
9101 width = min (cursor_glyph->pixel_width, width);
9103 w->phys_cursor_width = width;
9105 /* If the character under cursor is R2L, draw the bar cursor
9106 on the right of its glyph, rather than on the left. */
9107 if ((cursor_glyph->resolved_level & 1) != 0)
9108 x += cursor_glyph->pixel_width - width;
9110 x_fill_rectangle (f, gc, x,
9111 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
9112 width, row->height);
9114 else /* HBAR_CURSOR */
9116 int dummy_x, dummy_y, dummy_h;
9117 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
9119 if (width < 0)
9120 width = row->height;
9122 width = min (row->height, width);
9124 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
9125 &dummy_y, &dummy_h);
9127 if ((cursor_glyph->resolved_level & 1) != 0
9128 && cursor_glyph->pixel_width > w->phys_cursor_width - 1)
9129 x += cursor_glyph->pixel_width - w->phys_cursor_width + 1;
9130 x_fill_rectangle (f, gc, x,
9131 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
9132 row->height - width),
9133 w->phys_cursor_width - 1, width);
9136 x_reset_clip_rectangles (f, gc);
9141 /* RIF: Define cursor CURSOR on frame F. */
9143 static void
9144 x_define_frame_cursor (struct frame *f, Cursor cursor)
9146 if (!f->pointer_invisible
9147 && f->output_data.x->current_cursor != cursor)
9148 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
9149 f->output_data.x->current_cursor = cursor;
9153 /* RIF: Clear area on frame F. */
9155 static void
9156 x_clear_frame_area (struct frame *f, int x, int y, int width, int height)
9158 x_clear_area (f, x, y, width, height);
9162 /* RIF: Draw cursor on window W. */
9164 static void
9165 x_draw_window_cursor (struct window *w, struct glyph_row *glyph_row, int x,
9166 int y, enum text_cursor_kinds cursor_type,
9167 int cursor_width, bool on_p, bool active_p)
9169 struct frame *f = XFRAME (WINDOW_FRAME (w));
9171 if (on_p)
9173 w->phys_cursor_type = cursor_type;
9174 w->phys_cursor_on_p = true;
9176 if (glyph_row->exact_window_width_line_p
9177 && (glyph_row->reversed_p
9178 ? (w->phys_cursor.hpos < 0)
9179 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
9181 glyph_row->cursor_in_fringe_p = true;
9182 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
9184 else
9186 switch (cursor_type)
9188 case HOLLOW_BOX_CURSOR:
9189 x_draw_hollow_cursor (w, glyph_row);
9190 break;
9192 case FILLED_BOX_CURSOR:
9193 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
9194 break;
9196 case BAR_CURSOR:
9197 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
9198 break;
9200 case HBAR_CURSOR:
9201 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
9202 break;
9204 case NO_CURSOR:
9205 w->phys_cursor_width = 0;
9206 break;
9208 default:
9209 emacs_abort ();
9213 #ifdef HAVE_X_I18N
9214 if (w == XWINDOW (f->selected_window))
9215 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
9216 xic_set_preeditarea (w, x, y);
9217 #endif
9220 XFlush (FRAME_X_DISPLAY (f));
9224 /* Icons. */
9226 /* Make the x-window of frame F use the gnu icon bitmap. */
9228 bool
9229 x_bitmap_icon (struct frame *f, Lisp_Object file)
9231 ptrdiff_t bitmap_id;
9233 if (FRAME_X_WINDOW (f) == 0)
9234 return true;
9236 /* Free up our existing icon bitmap and mask if any. */
9237 if (f->output_data.x->icon_bitmap > 0)
9238 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
9239 f->output_data.x->icon_bitmap = 0;
9241 if (STRINGP (file))
9243 #ifdef USE_GTK
9244 /* Use gtk_window_set_icon_from_file () if available,
9245 It's not restricted to bitmaps */
9246 if (xg_set_icon (f, file))
9247 return false;
9248 #endif /* USE_GTK */
9249 bitmap_id = x_create_bitmap_from_file (f, file);
9250 x_create_bitmap_mask (f, bitmap_id);
9252 else
9254 /* Create the GNU bitmap and mask if necessary. */
9255 if (FRAME_DISPLAY_INFO (f)->icon_bitmap_id < 0)
9257 ptrdiff_t rc = -1;
9259 #ifdef USE_GTK
9261 if (xg_set_icon (f, xg_default_icon_file)
9262 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
9264 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = -2;
9265 return false;
9268 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
9270 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
9271 if (rc != -1)
9272 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
9274 #endif
9276 /* If all else fails, use the (black and white) xbm image. */
9277 if (rc == -1)
9279 rc = x_create_bitmap_from_data (f, (char *) gnu_xbm_bits,
9280 gnu_xbm_width, gnu_xbm_height);
9281 if (rc == -1)
9282 return true;
9284 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
9285 x_create_bitmap_mask (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
9289 /* The first time we create the GNU bitmap and mask,
9290 this increments the ref-count one extra time.
9291 As a result, the GNU bitmap and mask are never freed.
9292 That way, we don't have to worry about allocating it again. */
9293 x_reference_bitmap (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
9295 bitmap_id = FRAME_DISPLAY_INFO (f)->icon_bitmap_id;
9298 x_wm_set_icon_pixmap (f, bitmap_id);
9299 f->output_data.x->icon_bitmap = bitmap_id;
9301 return false;
9305 /* Make the x-window of frame F use a rectangle with text.
9306 Use ICON_NAME as the text. */
9308 bool
9309 x_text_icon (struct frame *f, const char *icon_name)
9311 if (FRAME_X_WINDOW (f) == 0)
9312 return true;
9315 XTextProperty text;
9316 text.value = (unsigned char *) icon_name;
9317 text.encoding = XA_STRING;
9318 text.format = 8;
9319 text.nitems = strlen (icon_name);
9320 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
9323 if (f->output_data.x->icon_bitmap > 0)
9324 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
9325 f->output_data.x->icon_bitmap = 0;
9326 x_wm_set_icon_pixmap (f, 0);
9328 return false;
9331 #define X_ERROR_MESSAGE_SIZE 200
9333 /* If non-nil, this should be a string.
9334 It means catch X errors and store the error message in this string.
9336 The reason we use a stack is that x_catch_error/x_uncatch_error can
9337 be called from a signal handler.
9340 struct x_error_message_stack {
9341 char string[X_ERROR_MESSAGE_SIZE];
9342 Display *dpy;
9343 x_special_error_handler handler;
9344 void *handler_data;
9345 struct x_error_message_stack *prev;
9347 static struct x_error_message_stack *x_error_message;
9349 /* An X error handler which stores the error message in
9350 *x_error_message. This is called from x_error_handler if
9351 x_catch_errors is in effect. */
9353 static void
9354 x_error_catcher (Display *display, XErrorEvent *event)
9356 XGetErrorText (display, event->error_code,
9357 x_error_message->string,
9358 X_ERROR_MESSAGE_SIZE);
9359 if (x_error_message->handler)
9360 x_error_message->handler (display, event, x_error_message->string,
9361 x_error_message->handler_data);
9364 /* Begin trapping X errors for display DPY. Actually we trap X errors
9365 for all displays, but DPY should be the display you are actually
9366 operating on.
9368 After calling this function, X protocol errors no longer cause
9369 Emacs to exit; instead, they are recorded in the string
9370 stored in *x_error_message.
9372 Calling x_check_errors signals an Emacs error if an X error has
9373 occurred since the last call to x_catch_errors or x_check_errors.
9375 Calling x_uncatch_errors resumes the normal error handling.
9376 Calling x_uncatch_errors_after_check is similar, but skips an XSync
9377 to the server, and should be used only immediately after
9378 x_had_errors_p or x_check_errors. */
9380 void
9381 x_catch_errors_with_handler (Display *dpy, x_special_error_handler handler,
9382 void *handler_data)
9384 struct x_error_message_stack *data = xmalloc (sizeof *data);
9386 /* Make sure any errors from previous requests have been dealt with. */
9387 XSync (dpy, False);
9389 data->dpy = dpy;
9390 data->string[0] = 0;
9391 data->handler = handler;
9392 data->handler_data = handler_data;
9393 data->prev = x_error_message;
9394 x_error_message = data;
9397 void
9398 x_catch_errors (Display *dpy)
9400 x_catch_errors_with_handler (dpy, NULL, NULL);
9403 /* Undo the last x_catch_errors call.
9404 DPY should be the display that was passed to x_catch_errors.
9406 This version should be used only if the immediately preceding
9407 X-protocol-related thing was x_check_errors or x_had_error_p, both
9408 of which issue XSync calls, so we don't need to re-sync here. */
9410 void
9411 x_uncatch_errors_after_check (void)
9413 struct x_error_message_stack *tmp;
9415 block_input ();
9416 tmp = x_error_message;
9417 x_error_message = x_error_message->prev;
9418 xfree (tmp);
9419 unblock_input ();
9422 /* Undo the last x_catch_errors call.
9423 DPY should be the display that was passed to x_catch_errors. */
9425 void
9426 x_uncatch_errors (void)
9428 struct x_error_message_stack *tmp;
9430 block_input ();
9432 /* The display may have been closed before this function is called.
9433 Check if it is still open before calling XSync. */
9434 if (x_display_info_for_display (x_error_message->dpy) != 0)
9435 XSync (x_error_message->dpy, False);
9437 tmp = x_error_message;
9438 x_error_message = x_error_message->prev;
9439 xfree (tmp);
9440 unblock_input ();
9443 /* If any X protocol errors have arrived since the last call to
9444 x_catch_errors or x_check_errors, signal an Emacs error using
9445 sprintf (a buffer, FORMAT, the x error message text) as the text. */
9447 void
9448 x_check_errors (Display *dpy, const char *format)
9450 /* Make sure to catch any errors incurred so far. */
9451 XSync (dpy, False);
9453 if (x_error_message->string[0])
9455 char string[X_ERROR_MESSAGE_SIZE];
9456 memcpy (string, x_error_message->string, X_ERROR_MESSAGE_SIZE);
9457 x_uncatch_errors ();
9458 error (format, string);
9462 /* Nonzero if we had any X protocol errors
9463 since we did x_catch_errors on DPY. */
9465 bool
9466 x_had_errors_p (Display *dpy)
9468 /* Make sure to catch any errors incurred so far. */
9469 XSync (dpy, False);
9471 return x_error_message->string[0] != 0;
9474 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
9476 void
9477 x_clear_errors (Display *dpy)
9479 x_error_message->string[0] = 0;
9482 #if false
9483 /* See comment in unwind_to_catch why calling this is a bad
9484 * idea. --lorentey */
9485 /* Close off all unclosed x_catch_errors calls. */
9487 void
9488 x_fully_uncatch_errors (void)
9490 while (x_error_message)
9491 x_uncatch_errors ();
9493 #endif
9495 #if false
9496 static unsigned int x_wire_count;
9497 x_trace_wire (void)
9499 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
9501 #endif
9504 /************************************************************************
9505 Handling X errors
9506 ************************************************************************/
9508 /* Error message passed to x_connection_closed. */
9510 static char *error_msg;
9512 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
9513 the text of an error message that lead to the connection loss. */
9515 static _Noreturn void
9516 x_connection_closed (Display *dpy, const char *error_message, bool ioerror)
9518 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
9519 Lisp_Object frame, tail;
9520 ptrdiff_t idx = SPECPDL_INDEX ();
9522 error_msg = alloca (strlen (error_message) + 1);
9523 strcpy (error_msg, error_message);
9525 /* Inhibit redisplay while frames are being deleted. */
9526 specbind (Qinhibit_redisplay, Qt);
9528 if (dpyinfo)
9530 /* Protect display from being closed when we delete the last
9531 frame on it. */
9532 dpyinfo->reference_count++;
9533 dpyinfo->terminal->reference_count++;
9535 if (ioerror) dpyinfo->display = 0;
9537 /* First delete frames whose mini-buffers are on frames
9538 that are on the dead display. */
9539 FOR_EACH_FRAME (tail, frame)
9541 Lisp_Object minibuf_frame;
9542 minibuf_frame
9543 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
9544 if (FRAME_X_P (XFRAME (frame))
9545 && FRAME_X_P (XFRAME (minibuf_frame))
9546 && ! EQ (frame, minibuf_frame)
9547 && FRAME_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
9548 delete_frame (frame, Qnoelisp);
9551 /* Now delete all remaining frames on the dead display.
9552 We are now sure none of these is used as the mini-buffer
9553 for another frame that we need to delete. */
9554 FOR_EACH_FRAME (tail, frame)
9555 if (FRAME_X_P (XFRAME (frame))
9556 && FRAME_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
9558 /* Set this to t so that delete_frame won't get confused
9559 trying to find a replacement. */
9560 kset_default_minibuffer_frame (FRAME_KBOARD (XFRAME (frame)), Qt);
9561 delete_frame (frame, Qnoelisp);
9564 /* If DPYINFO is null, this means we didn't open the display in the
9565 first place, so don't try to close it. */
9566 if (dpyinfo)
9568 /* We can not call XtCloseDisplay here because it calls XSync.
9569 XSync inside the error handler apparently hangs Emacs. On
9570 current Xt versions, this isn't needed either. */
9571 #ifdef USE_GTK
9572 /* A long-standing GTK bug prevents proper disconnect handling
9573 (https://bugzilla.gnome.org/show_bug.cgi?id=85715). Once,
9574 the resulting Glib error message loop filled a user's disk.
9575 To avoid this, kill Emacs unconditionally on disconnect. */
9576 shut_down_emacs (0, Qnil);
9577 fprintf (stderr, "%s\n\
9578 When compiled with GTK, Emacs cannot recover from X disconnects.\n\
9579 This is a GTK bug: https://bugzilla.gnome.org/show_bug.cgi?id=85715\n\
9580 For details, see etc/PROBLEMS.\n",
9581 error_msg);
9582 emacs_abort ();
9583 #endif /* USE_GTK */
9585 /* Indicate that this display is dead. */
9586 dpyinfo->display = 0;
9588 dpyinfo->reference_count--;
9589 dpyinfo->terminal->reference_count--;
9590 if (dpyinfo->reference_count != 0)
9591 /* We have just closed all frames on this display. */
9592 emacs_abort ();
9595 Lisp_Object tmp;
9596 XSETTERMINAL (tmp, dpyinfo->terminal);
9597 Fdelete_terminal (tmp, Qnoelisp);
9601 if (terminal_list == 0)
9603 fprintf (stderr, "%s\n", error_msg);
9604 Fkill_emacs (make_number (70));
9605 /* NOTREACHED */
9608 totally_unblock_input ();
9610 unbind_to (idx, Qnil);
9611 clear_waiting_for_input ();
9613 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
9614 longjmp), because returning from this function would get us back into
9615 Xlib's code which will directly call `exit'. */
9616 error ("%s", error_msg);
9619 /* We specifically use it before defining it, so that gcc doesn't inline it,
9620 otherwise gdb doesn't know how to properly put a breakpoint on it. */
9621 static void x_error_quitter (Display *, XErrorEvent *);
9623 /* This is the first-level handler for X protocol errors.
9624 It calls x_error_quitter or x_error_catcher. */
9626 static int
9627 x_error_handler (Display *display, XErrorEvent *event)
9629 #if defined USE_GTK && defined HAVE_GTK3
9630 if ((event->error_code == BadMatch || event->error_code == BadWindow)
9631 && event->request_code == X_SetInputFocus)
9633 return 0;
9635 #endif
9637 if (x_error_message)
9638 x_error_catcher (display, event);
9639 else
9640 x_error_quitter (display, event);
9641 return 0;
9644 /* This is the usual handler for X protocol errors.
9645 It kills all frames on the display that we got the error for.
9646 If that was the only one, it prints an error message and kills Emacs. */
9648 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
9650 /* On older GCC versions, just putting x_error_quitter
9651 after x_error_handler prevents inlining into the former. */
9653 static void NO_INLINE
9654 x_error_quitter (Display *display, XErrorEvent *event)
9656 char buf[256], buf1[356];
9658 /* Ignore BadName errors. They can happen because of fonts
9659 or colors that are not defined. */
9661 if (event->error_code == BadName)
9662 return;
9664 /* Note that there is no real way portable across R3/R4 to get the
9665 original error handler. */
9667 XGetErrorText (display, event->error_code, buf, sizeof (buf));
9668 sprintf (buf1, "X protocol error: %s on protocol request %d",
9669 buf, event->request_code);
9670 x_connection_closed (display, buf1, false);
9674 /* This is the handler for X IO errors, always.
9675 It kills all frames on the display that we lost touch with.
9676 If that was the only one, it prints an error message and kills Emacs. */
9678 static _Noreturn int
9679 x_io_error_quitter (Display *display)
9681 char buf[256];
9683 snprintf (buf, sizeof buf, "Connection lost to X server '%s'",
9684 DisplayString (display));
9685 x_connection_closed (display, buf, true);
9686 assume (false);
9689 /* Changing the font of the frame. */
9691 /* Give frame F the font FONT-OBJECT as its default font. The return
9692 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
9693 frame. If it is negative, generate a new fontset from
9694 FONT-OBJECT. */
9696 Lisp_Object
9697 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
9699 struct font *font = XFONT_OBJECT (font_object);
9700 int unit, font_ascent, font_descent;
9701 #ifndef USE_X_TOOLKIT
9702 int old_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
9703 Lisp_Object fullscreen;
9704 #endif
9706 if (fontset < 0)
9707 fontset = fontset_from_font (font_object);
9708 FRAME_FONTSET (f) = fontset;
9709 if (FRAME_FONT (f) == font)
9710 /* This font is already set in frame F. There's nothing more to
9711 do. */
9712 return font_object;
9714 FRAME_FONT (f) = font;
9715 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
9716 FRAME_COLUMN_WIDTH (f) = font->average_width;
9717 get_font_ascent_descent (font, &font_ascent, &font_descent);
9718 FRAME_LINE_HEIGHT (f) = font_ascent + font_descent;
9720 #ifndef USE_X_TOOLKIT
9721 FRAME_MENU_BAR_HEIGHT (f) = FRAME_MENU_BAR_LINES (f) * FRAME_LINE_HEIGHT (f);
9722 #endif
9724 /* Compute character columns occupied by scrollbar.
9726 Don't do things differently for non-toolkit scrollbars
9727 (Bug#17163). */
9728 unit = FRAME_COLUMN_WIDTH (f);
9729 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
9730 FRAME_CONFIG_SCROLL_BAR_COLS (f)
9731 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + unit - 1) / unit;
9732 else
9733 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
9735 if (FRAME_X_WINDOW (f) != 0)
9737 /* Don't change the size of a tip frame; there's no point in
9738 doing it because it's done in Fx_show_tip, and it leads to
9739 problems because the tip frame has no widget. */
9740 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
9742 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
9743 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 3,
9744 false, Qfont);
9745 #ifndef USE_X_TOOLKIT
9746 if (FRAME_MENU_BAR_HEIGHT (f) != old_menu_bar_height
9747 && !f->after_make_frame
9748 && (EQ (frame_inhibit_implied_resize, Qt)
9749 || (CONSP (frame_inhibit_implied_resize)
9750 && NILP (Fmemq (Qfont, frame_inhibit_implied_resize))))
9751 && (NILP (fullscreen = get_frame_param (f, Qfullscreen))
9752 || EQ (fullscreen, Qfullwidth)))
9753 /* If the menu bar height changes, try to keep text height
9754 constant. */
9755 adjust_frame_size
9756 (f, -1, FRAME_TEXT_HEIGHT (f) + FRAME_MENU_BAR_HEIGHT (f)
9757 - old_menu_bar_height, 1, false, Qfont);
9758 #endif /* USE_X_TOOLKIT */
9762 #ifdef HAVE_X_I18N
9763 if (FRAME_XIC (f)
9764 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
9766 block_input ();
9767 xic_set_xfontset (f, SSDATA (fontset_ascii (fontset)));
9768 unblock_input ();
9770 #endif
9772 return font_object;
9776 /***********************************************************************
9777 X Input Methods
9778 ***********************************************************************/
9780 #ifdef HAVE_X_I18N
9782 #ifdef HAVE_X11R6
9784 /* XIM destroy callback function, which is called whenever the
9785 connection to input method XIM dies. CLIENT_DATA contains a
9786 pointer to the x_display_info structure corresponding to XIM. */
9788 static void
9789 xim_destroy_callback (XIM xim, XPointer client_data, XPointer call_data)
9791 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
9792 Lisp_Object frame, tail;
9794 block_input ();
9796 /* No need to call XDestroyIC.. */
9797 FOR_EACH_FRAME (tail, frame)
9799 struct frame *f = XFRAME (frame);
9800 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
9802 FRAME_XIC (f) = NULL;
9803 xic_free_xfontset (f);
9807 /* No need to call XCloseIM. */
9808 dpyinfo->xim = NULL;
9809 XFree (dpyinfo->xim_styles);
9810 unblock_input ();
9813 #endif /* HAVE_X11R6 */
9815 /* Open the connection to the XIM server on display DPYINFO.
9816 RESOURCE_NAME is the resource name Emacs uses. */
9818 static void
9819 xim_open_dpy (struct x_display_info *dpyinfo, char *resource_name)
9821 XIM xim;
9823 #ifdef HAVE_XIM
9824 if (use_xim)
9826 if (dpyinfo->xim)
9827 XCloseIM (dpyinfo->xim);
9828 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
9829 emacs_class);
9830 dpyinfo->xim = xim;
9832 if (xim)
9834 #ifdef HAVE_X11R6
9835 XIMCallback destroy;
9836 #endif
9838 /* Get supported styles and XIM values. */
9839 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
9841 #ifdef HAVE_X11R6
9842 destroy.callback = xim_destroy_callback;
9843 destroy.client_data = (XPointer)dpyinfo;
9844 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
9845 #endif
9849 else
9850 #endif /* HAVE_XIM */
9851 dpyinfo->xim = NULL;
9855 #ifdef HAVE_X11R6_XIM
9857 /* XIM instantiate callback function, which is called whenever an XIM
9858 server is available. DISPLAY is the display of the XIM.
9859 CLIENT_DATA contains a pointer to an xim_inst_t structure created
9860 when the callback was registered. */
9862 static void
9863 xim_instantiate_callback (Display *display, XPointer client_data, XPointer call_data)
9865 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
9866 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
9868 /* We don't support multiple XIM connections. */
9869 if (dpyinfo->xim)
9870 return;
9872 xim_open_dpy (dpyinfo, xim_inst->resource_name);
9874 /* Create XIC for the existing frames on the same display, as long
9875 as they have no XIC. */
9876 if (dpyinfo->xim && dpyinfo->reference_count > 0)
9878 Lisp_Object tail, frame;
9880 block_input ();
9881 FOR_EACH_FRAME (tail, frame)
9883 struct frame *f = XFRAME (frame);
9885 if (FRAME_X_P (f)
9886 && FRAME_DISPLAY_INFO (f) == xim_inst->dpyinfo)
9887 if (FRAME_XIC (f) == NULL)
9889 create_frame_xic (f);
9890 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
9891 xic_set_statusarea (f);
9892 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
9894 struct window *w = XWINDOW (f->selected_window);
9895 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
9900 unblock_input ();
9904 #endif /* HAVE_X11R6_XIM */
9907 /* Open a connection to the XIM server on display DPYINFO.
9908 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
9909 connection only at the first time. On X11R6, open the connection
9910 in the XIM instantiate callback function. */
9912 static void
9913 xim_initialize (struct x_display_info *dpyinfo, char *resource_name)
9915 dpyinfo->xim = NULL;
9916 #ifdef HAVE_XIM
9917 if (use_xim)
9919 #ifdef HAVE_X11R6_XIM
9920 struct xim_inst_t *xim_inst = xmalloc (sizeof *xim_inst);
9921 Bool ret;
9923 dpyinfo->xim_callback_data = xim_inst;
9924 xim_inst->dpyinfo = dpyinfo;
9925 xim_inst->resource_name = xstrdup (resource_name);
9926 ret = XRegisterIMInstantiateCallback
9927 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
9928 emacs_class, xim_instantiate_callback,
9929 /* This is XPointer in XFree86 but (XPointer *)
9930 on Tru64, at least, hence the configure test. */
9931 (XRegisterIMInstantiateCallback_arg6) xim_inst);
9932 eassert (ret == True);
9933 #else /* not HAVE_X11R6_XIM */
9934 xim_open_dpy (dpyinfo, resource_name);
9935 #endif /* not HAVE_X11R6_XIM */
9937 #endif /* HAVE_XIM */
9941 /* Close the connection to the XIM server on display DPYINFO. */
9943 static void
9944 xim_close_dpy (struct x_display_info *dpyinfo)
9946 #ifdef HAVE_XIM
9947 if (use_xim)
9949 #ifdef HAVE_X11R6_XIM
9950 struct xim_inst_t *xim_inst = dpyinfo->xim_callback_data;
9952 if (dpyinfo->display)
9954 Bool ret = XUnregisterIMInstantiateCallback
9955 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
9956 emacs_class, xim_instantiate_callback,
9957 (XRegisterIMInstantiateCallback_arg6) xim_inst);
9958 eassert (ret == True);
9960 xfree (xim_inst->resource_name);
9961 xfree (xim_inst);
9962 #endif /* HAVE_X11R6_XIM */
9963 if (dpyinfo->display)
9964 XCloseIM (dpyinfo->xim);
9965 dpyinfo->xim = NULL;
9966 XFree (dpyinfo->xim_styles);
9968 #endif /* HAVE_XIM */
9971 #endif /* not HAVE_X11R6_XIM */
9975 /* Calculate the absolute position in frame F
9976 from its current recorded position values and gravity. */
9978 static void
9979 x_calc_absolute_position (struct frame *f)
9981 int flags = f->size_hint_flags;
9983 /* We have nothing to do if the current position
9984 is already for the top-left corner. */
9985 if (! ((flags & XNegative) || (flags & YNegative)))
9986 return;
9988 /* Treat negative positions as relative to the leftmost bottommost
9989 position that fits on the screen. */
9990 if (flags & XNegative)
9991 f->left_pos = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
9992 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
9995 int height = FRAME_PIXEL_HEIGHT (f);
9997 #if defined USE_X_TOOLKIT && defined USE_MOTIF
9998 /* Something is fishy here. When using Motif, starting Emacs with
9999 `-g -0-0', the frame appears too low by a few pixels.
10001 This seems to be so because initially, while Emacs is starting,
10002 the column widget's height and the frame's pixel height are
10003 different. The column widget's height is the right one. In
10004 later invocations, when Emacs is up, the frame's pixel height
10005 is right, though.
10007 It's not obvious where the initial small difference comes from.
10008 2000-12-01, gerd. */
10010 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
10011 #endif
10013 if (flags & YNegative)
10014 f->top_pos = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
10015 - height + f->top_pos;
10018 /* The left_pos and top_pos
10019 are now relative to the top and left screen edges,
10020 so the flags should correspond. */
10021 f->size_hint_flags &= ~ (XNegative | YNegative);
10024 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
10025 to really change the position, and 0 when calling from
10026 x_make_frame_visible (in that case, XOFF and YOFF are the current
10027 position values). It is -1 when calling from x_set_frame_parameters,
10028 which means, do adjust for borders but don't change the gravity. */
10030 void
10031 x_set_offset (struct frame *f, register int xoff, register int yoff, int change_gravity)
10033 int modified_top, modified_left;
10035 if (change_gravity > 0)
10037 f->top_pos = yoff;
10038 f->left_pos = xoff;
10039 f->size_hint_flags &= ~ (XNegative | YNegative);
10040 if (xoff < 0)
10041 f->size_hint_flags |= XNegative;
10042 if (yoff < 0)
10043 f->size_hint_flags |= YNegative;
10044 f->win_gravity = NorthWestGravity;
10046 x_calc_absolute_position (f);
10048 block_input ();
10049 x_wm_set_size_hint (f, 0, false);
10051 modified_left = f->left_pos;
10052 modified_top = f->top_pos;
10054 if (change_gravity != 0 && FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
10056 /* Some WMs (twm, wmaker at least) has an offset that is smaller
10057 than the WM decorations. So we use the calculated offset instead
10058 of the WM decoration sizes here (x/y_pixels_outer_diff). */
10059 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
10060 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
10063 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10064 modified_left, modified_top);
10066 x_sync_with_move (f, f->left_pos, f->top_pos,
10067 FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN);
10069 /* change_gravity is non-zero when this function is called from Lisp to
10070 programmatically move a frame. In that case, we call
10071 x_check_expected_move to discover if we have a "Type A" or "Type B"
10072 window manager, and, for a "Type A" window manager, adjust the position
10073 of the frame.
10075 We call x_check_expected_move if a programmatic move occurred, and
10076 either the window manager type (A/B) is unknown or it is Type A but we
10077 need to compute the top/left offset adjustment for this frame. */
10079 if (change_gravity != 0
10080 && (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
10081 || (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
10082 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
10083 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
10084 x_check_expected_move (f, modified_left, modified_top);
10086 unblock_input ();
10089 /* Return true if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
10090 on the root window for frame F contains ATOMNAME.
10091 This is how a WM check shall be done according to the Window Manager
10092 Specification/Extended Window Manager Hints at
10093 http://freedesktop.org/wiki/Specifications/wm-spec. */
10095 bool
10096 x_wm_supports (struct frame *f, Atom want_atom)
10098 Atom actual_type;
10099 unsigned long actual_size, bytes_remaining;
10100 int i, rc, actual_format;
10101 bool ret;
10102 Window wmcheck_window;
10103 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10104 Window target_window = dpyinfo->root_window;
10105 int max_len = 65536;
10106 Display *dpy = FRAME_X_DISPLAY (f);
10107 unsigned char *tmp_data = NULL;
10108 Atom target_type = XA_WINDOW;
10110 block_input ();
10112 x_catch_errors (dpy);
10113 rc = XGetWindowProperty (dpy, target_window,
10114 dpyinfo->Xatom_net_supporting_wm_check,
10115 0, max_len, False, target_type,
10116 &actual_type, &actual_format, &actual_size,
10117 &bytes_remaining, &tmp_data);
10119 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
10121 if (tmp_data) XFree (tmp_data);
10122 x_uncatch_errors ();
10123 unblock_input ();
10124 return false;
10127 wmcheck_window = *(Window *) tmp_data;
10128 XFree (tmp_data);
10130 /* Check if window exists. */
10131 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
10132 if (x_had_errors_p (dpy))
10134 x_uncatch_errors_after_check ();
10135 unblock_input ();
10136 return false;
10139 if (dpyinfo->net_supported_window != wmcheck_window)
10141 /* Window changed, reload atoms */
10142 if (dpyinfo->net_supported_atoms != NULL)
10143 XFree (dpyinfo->net_supported_atoms);
10144 dpyinfo->net_supported_atoms = NULL;
10145 dpyinfo->nr_net_supported_atoms = 0;
10146 dpyinfo->net_supported_window = 0;
10148 target_type = XA_ATOM;
10149 tmp_data = NULL;
10150 rc = XGetWindowProperty (dpy, target_window,
10151 dpyinfo->Xatom_net_supported,
10152 0, max_len, False, target_type,
10153 &actual_type, &actual_format, &actual_size,
10154 &bytes_remaining, &tmp_data);
10156 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
10158 if (tmp_data) XFree (tmp_data);
10159 x_uncatch_errors ();
10160 unblock_input ();
10161 return false;
10164 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
10165 dpyinfo->nr_net_supported_atoms = actual_size;
10166 dpyinfo->net_supported_window = wmcheck_window;
10169 ret = false;
10171 for (i = 0; !ret && i < dpyinfo->nr_net_supported_atoms; ++i)
10172 ret = dpyinfo->net_supported_atoms[i] == want_atom;
10174 x_uncatch_errors ();
10175 unblock_input ();
10177 return ret;
10180 static void
10181 set_wm_state (Lisp_Object frame, bool add, Atom atom, Atom value)
10183 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (XFRAME (frame));
10185 x_send_client_event (frame, make_number (0), frame,
10186 dpyinfo->Xatom_net_wm_state,
10187 make_number (32),
10188 /* 1 = add, 0 = remove */
10189 Fcons
10190 (make_number (add),
10191 Fcons
10192 (make_fixnum_or_float (atom),
10193 (value != 0
10194 ? list1 (make_fixnum_or_float (value))
10195 : Qnil))));
10198 void
10199 x_set_sticky (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
10201 Lisp_Object frame;
10202 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10204 XSETFRAME (frame, f);
10206 set_wm_state (frame, !NILP (new_value),
10207 dpyinfo->Xatom_net_wm_state_sticky, None);
10210 /* Return the current _NET_WM_STATE.
10211 SIZE_STATE is set to one of the FULLSCREEN_* values.
10212 Set *STICKY to the sticky state.
10214 Return true iff we are not hidden. */
10216 static bool
10217 get_current_wm_state (struct frame *f,
10218 Window window,
10219 int *size_state,
10220 bool *sticky)
10222 unsigned long actual_size;
10223 int i;
10224 bool is_hidden = false;
10225 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10226 long max_len = 65536;
10227 Atom target_type = XA_ATOM;
10228 /* If XCB is available, we can avoid three XSync calls. */
10229 #ifdef USE_XCB
10230 xcb_get_property_cookie_t prop_cookie;
10231 xcb_get_property_reply_t *prop;
10232 xcb_atom_t *reply_data;
10233 #else
10234 Display *dpy = FRAME_X_DISPLAY (f);
10235 unsigned long bytes_remaining;
10236 int rc, actual_format;
10237 Atom actual_type;
10238 unsigned char *tmp_data = NULL;
10239 Atom *reply_data;
10240 #endif
10242 *sticky = false;
10243 *size_state = FULLSCREEN_NONE;
10245 block_input ();
10247 #ifdef USE_XCB
10248 prop_cookie = xcb_get_property (dpyinfo->xcb_connection, 0, window,
10249 dpyinfo->Xatom_net_wm_state,
10250 target_type, 0, max_len);
10251 prop = xcb_get_property_reply (dpyinfo->xcb_connection, prop_cookie, NULL);
10252 if (prop && prop->type == target_type)
10254 int actual_bytes = xcb_get_property_value_length (prop);
10255 eassume (0 <= actual_bytes);
10256 actual_size = actual_bytes / sizeof *reply_data;
10257 reply_data = xcb_get_property_value (prop);
10259 else
10261 actual_size = 0;
10262 is_hidden = FRAME_ICONIFIED_P (f);
10264 #else
10265 x_catch_errors (dpy);
10266 rc = XGetWindowProperty (dpy, window, dpyinfo->Xatom_net_wm_state,
10267 0, max_len, False, target_type,
10268 &actual_type, &actual_format, &actual_size,
10269 &bytes_remaining, &tmp_data);
10271 if (rc == Success && actual_type == target_type && ! x_had_errors_p (dpy))
10272 reply_data = (Atom *) tmp_data;
10273 else
10275 actual_size = 0;
10276 is_hidden = FRAME_ICONIFIED_P (f);
10279 x_uncatch_errors ();
10280 #endif
10282 for (i = 0; i < actual_size; ++i)
10284 Atom a = reply_data[i];
10285 if (a == dpyinfo->Xatom_net_wm_state_hidden)
10286 is_hidden = true;
10287 else if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
10289 if (*size_state == FULLSCREEN_HEIGHT)
10290 *size_state = FULLSCREEN_MAXIMIZED;
10291 else
10292 *size_state = FULLSCREEN_WIDTH;
10294 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
10296 if (*size_state == FULLSCREEN_WIDTH)
10297 *size_state = FULLSCREEN_MAXIMIZED;
10298 else
10299 *size_state = FULLSCREEN_HEIGHT;
10301 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen)
10302 *size_state = FULLSCREEN_BOTH;
10303 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
10304 *sticky = true;
10307 #ifdef USE_XCB
10308 free (prop);
10309 #else
10310 if (tmp_data) XFree (tmp_data);
10311 #endif
10313 unblock_input ();
10314 return ! is_hidden;
10317 /* Do fullscreen as specified in extended window manager hints */
10319 static bool
10320 do_ewmh_fullscreen (struct frame *f)
10322 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10323 bool have_net_atom = x_wm_supports (f, dpyinfo->Xatom_net_wm_state);
10324 int cur;
10325 bool dummy;
10327 get_current_wm_state (f, FRAME_OUTER_WINDOW (f), &cur, &dummy);
10329 /* Some window managers don't say they support _NET_WM_STATE, but they do say
10330 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
10331 if (!have_net_atom)
10332 have_net_atom = x_wm_supports (f, dpyinfo->Xatom_net_wm_state_fullscreen);
10334 if (have_net_atom && cur != f->want_fullscreen)
10336 Lisp_Object frame;
10338 XSETFRAME (frame, f);
10340 /* Keep number of calls to set_wm_state as low as possible.
10341 Some window managers, or possible Gtk+, hangs when too many
10342 are sent at once. */
10343 switch (f->want_fullscreen)
10345 case FULLSCREEN_BOTH:
10346 if (cur != FULLSCREEN_BOTH)
10347 set_wm_state (frame, true, dpyinfo->Xatom_net_wm_state_fullscreen,
10348 None);
10349 break;
10350 case FULLSCREEN_WIDTH:
10351 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_MAXIMIZED)
10353 set_wm_state (frame, false,
10354 dpyinfo->Xatom_net_wm_state_maximized_horz,
10355 dpyinfo->Xatom_net_wm_state_maximized_vert);
10356 set_wm_state (frame, true,
10357 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10359 else
10361 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_HEIGHT
10362 || cur == FULLSCREEN_MAXIMIZED)
10363 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10364 dpyinfo->Xatom_net_wm_state_maximized_vert);
10365 if (cur != FULLSCREEN_MAXIMIZED || x_frame_normalize_before_maximize)
10366 set_wm_state (frame, true,
10367 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10369 break;
10370 case FULLSCREEN_HEIGHT:
10371 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_MAXIMIZED)
10373 set_wm_state (frame, false,
10374 dpyinfo->Xatom_net_wm_state_maximized_horz,
10375 dpyinfo->Xatom_net_wm_state_maximized_vert);
10376 set_wm_state (frame, true,
10377 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10379 else
10381 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_WIDTH
10382 || cur == FULLSCREEN_MAXIMIZED)
10383 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10384 dpyinfo->Xatom_net_wm_state_maximized_horz);
10385 if (cur != FULLSCREEN_MAXIMIZED || x_frame_normalize_before_maximize)
10386 set_wm_state (frame, true,
10387 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10389 break;
10390 case FULLSCREEN_MAXIMIZED:
10391 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_BOTH)
10393 set_wm_state (frame, false,
10394 dpyinfo->Xatom_net_wm_state_fullscreen, None);
10395 set_wm_state (frame, true,
10396 dpyinfo->Xatom_net_wm_state_maximized_horz,
10397 dpyinfo->Xatom_net_wm_state_maximized_vert);
10399 else if (x_frame_normalize_before_maximize && cur == FULLSCREEN_WIDTH)
10401 set_wm_state (frame, false,
10402 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10403 set_wm_state (frame, true,
10404 dpyinfo->Xatom_net_wm_state_maximized_horz,
10405 dpyinfo->Xatom_net_wm_state_maximized_vert);
10407 else if (x_frame_normalize_before_maximize && cur == FULLSCREEN_HEIGHT)
10409 set_wm_state (frame, false,
10410 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10411 set_wm_state (frame, true,
10412 dpyinfo->Xatom_net_wm_state_maximized_horz,
10413 dpyinfo->Xatom_net_wm_state_maximized_vert);
10415 else
10417 if (cur == FULLSCREEN_BOTH)
10418 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10419 None);
10420 else if (cur == FULLSCREEN_HEIGHT)
10421 set_wm_state (frame, true,
10422 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10423 else if (cur == FULLSCREEN_WIDTH)
10424 set_wm_state (frame, true, None,
10425 dpyinfo->Xatom_net_wm_state_maximized_vert);
10426 else
10427 set_wm_state (frame, true,
10428 dpyinfo->Xatom_net_wm_state_maximized_horz,
10429 dpyinfo->Xatom_net_wm_state_maximized_vert);
10431 break;
10432 case FULLSCREEN_NONE:
10433 if (cur == FULLSCREEN_BOTH)
10434 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10435 None);
10436 else
10437 set_wm_state (frame, false,
10438 dpyinfo->Xatom_net_wm_state_maximized_horz,
10439 dpyinfo->Xatom_net_wm_state_maximized_vert);
10442 f->want_fullscreen = FULLSCREEN_NONE;
10446 return have_net_atom;
10449 static void
10450 XTfullscreen_hook (struct frame *f)
10452 if (FRAME_VISIBLE_P (f))
10454 block_input ();
10455 x_check_fullscreen (f);
10456 x_sync (f);
10457 unblock_input ();
10462 static bool
10463 x_handle_net_wm_state (struct frame *f, const XPropertyEvent *event)
10465 int value = FULLSCREEN_NONE;
10466 Lisp_Object lval;
10467 bool sticky = false;
10468 bool not_hidden = get_current_wm_state (f, event->window, &value, &sticky);
10470 lval = Qnil;
10471 switch (value)
10473 case FULLSCREEN_WIDTH:
10474 lval = Qfullwidth;
10475 break;
10476 case FULLSCREEN_HEIGHT:
10477 lval = Qfullheight;
10478 break;
10479 case FULLSCREEN_BOTH:
10480 lval = Qfullboth;
10481 break;
10482 case FULLSCREEN_MAXIMIZED:
10483 lval = Qmaximized;
10484 break;
10487 frame_size_history_add
10488 (f, Qx_handle_net_wm_state, 0, 0,
10489 list2 (get_frame_param (f, Qfullscreen), lval));
10491 store_frame_param (f, Qfullscreen, lval);
10492 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
10494 return not_hidden;
10497 /* Check if we need to resize the frame due to a fullscreen request.
10498 If so needed, resize the frame. */
10499 static void
10500 x_check_fullscreen (struct frame *f)
10502 Lisp_Object lval = Qnil;
10504 if (do_ewmh_fullscreen (f))
10505 return;
10507 if (f->output_data.x->parent_desc != FRAME_DISPLAY_INFO (f)->root_window)
10508 return; /* Only fullscreen without WM or with EWM hints (above). */
10510 /* Setting fullscreen to nil doesn't do anything. We could save the
10511 last non-fullscreen size and restore it, but it seems like a
10512 lot of work for this unusual case (no window manager running). */
10514 if (f->want_fullscreen != FULLSCREEN_NONE)
10516 int width = FRAME_PIXEL_WIDTH (f), height = FRAME_PIXEL_HEIGHT (f);
10517 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10519 switch (f->want_fullscreen)
10521 /* No difference between these two when there is no WM */
10522 case FULLSCREEN_MAXIMIZED:
10523 lval = Qmaximized;
10524 width = x_display_pixel_width (dpyinfo);
10525 height = x_display_pixel_height (dpyinfo);
10526 break;
10527 case FULLSCREEN_BOTH:
10528 lval = Qfullboth;
10529 width = x_display_pixel_width (dpyinfo);
10530 height = x_display_pixel_height (dpyinfo);
10531 break;
10532 case FULLSCREEN_WIDTH:
10533 lval = Qfullwidth;
10534 width = x_display_pixel_width (dpyinfo);
10535 height = height + FRAME_MENUBAR_HEIGHT (f);
10536 break;
10537 case FULLSCREEN_HEIGHT:
10538 lval = Qfullheight;
10539 height = x_display_pixel_height (dpyinfo);
10540 break;
10541 default:
10542 emacs_abort ();
10545 frame_size_history_add
10546 (f, Qx_check_fullscreen, width, height, Qnil);
10548 x_wm_set_size_hint (f, 0, false);
10550 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10551 width, height);
10553 if (FRAME_VISIBLE_P (f))
10554 x_wait_for_event (f, ConfigureNotify);
10555 else
10557 change_frame_size (f, width, height - FRAME_MENUBAR_HEIGHT (f),
10558 false, true, false, true);
10559 x_sync (f);
10563 /* `x_net_wm_state' might have reset the fullscreen frame parameter,
10564 restore it. */
10565 store_frame_param (f, Qfullscreen, lval);
10568 /* This function is called by x_set_offset to determine whether the window
10569 manager interfered with the positioning of the frame. Type A window
10570 managers position the surrounding window manager decorations a small
10571 amount above and left of the user-supplied position. Type B window
10572 managers position the surrounding window manager decorations at the
10573 user-specified position. If we detect a Type A window manager, we
10574 compensate by moving the window right and down by the proper amount. */
10576 static void
10577 x_check_expected_move (struct frame *f, int expected_left, int expected_top)
10579 int current_left = 0, current_top = 0;
10581 /* x_real_positions returns the left and top offsets of the outermost
10582 window manager window around the frame. */
10584 x_real_positions (f, &current_left, &current_top);
10586 if (current_left != expected_left || current_top != expected_top)
10588 /* It's a "Type A" window manager. */
10590 int adjusted_left;
10591 int adjusted_top;
10593 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
10594 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
10595 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
10597 /* Now fix the mispositioned frame's location. */
10599 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
10600 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
10602 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10603 adjusted_left, adjusted_top);
10605 x_sync_with_move (f, expected_left, expected_top, false);
10607 else
10608 /* It's a "Type B" window manager. We don't have to adjust the
10609 frame's position. */
10611 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
10615 /* Wait for XGetGeometry to return up-to-date position information for a
10616 recently-moved frame. Call this immediately after calling XMoveWindow.
10617 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
10618 frame has been moved to, so we use a fuzzy position comparison instead
10619 of an exact comparison. */
10621 static void
10622 x_sync_with_move (struct frame *f, int left, int top, bool fuzzy)
10624 int count = 0;
10626 while (count++ < 50)
10628 int current_left = 0, current_top = 0;
10630 /* In theory, this call to XSync only needs to happen once, but in
10631 practice, it doesn't seem to work, hence the need for the surrounding
10632 loop. */
10634 XSync (FRAME_X_DISPLAY (f), False);
10635 x_real_positions (f, &current_left, &current_top);
10637 if (fuzzy)
10639 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
10640 pixels. */
10642 if (eabs (current_left - left) <= 10
10643 && eabs (current_top - top) <= 40)
10644 return;
10646 else if (current_left == left && current_top == top)
10647 return;
10650 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
10651 will then return up-to-date position info. */
10653 wait_reading_process_output (0, 500000000, 0, false, Qnil, NULL, 0);
10657 /* Wait for an event on frame F matching EVENTTYPE. */
10658 void
10659 x_wait_for_event (struct frame *f, int eventtype)
10661 int level = interrupt_input_blocked;
10663 fd_set fds;
10664 struct timespec tmo, tmo_at, time_now;
10665 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
10667 f->wait_event_type = eventtype;
10669 /* Set timeout to 0.1 second. Hopefully not noticeable.
10670 Maybe it should be configurable. */
10671 tmo = make_timespec (0, 100 * 1000 * 1000);
10672 tmo_at = timespec_add (current_timespec (), tmo);
10674 while (f->wait_event_type)
10676 pending_signals = true;
10677 totally_unblock_input ();
10678 /* XTread_socket is called after unblock. */
10679 block_input ();
10680 interrupt_input_blocked = level;
10682 FD_ZERO (&fds);
10683 FD_SET (fd, &fds);
10685 time_now = current_timespec ();
10686 if (timespec_cmp (tmo_at, time_now) < 0)
10687 break;
10689 tmo = timespec_sub (tmo_at, time_now);
10690 if (pselect (fd + 1, &fds, NULL, NULL, &tmo, NULL) == 0)
10691 break; /* Timeout */
10694 f->wait_event_type = 0;
10698 /* Change the size of frame F's X window to WIDTH/HEIGHT in the case F
10699 doesn't have a widget. If CHANGE_GRAVITY, change to
10700 top-left-corner window gravity for this size change and subsequent
10701 size changes. Otherwise leave the window gravity unchanged. */
10703 static void
10704 x_set_window_size_1 (struct frame *f, bool change_gravity,
10705 int width, int height)
10707 int pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
10708 int pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
10709 int old_width = FRAME_PIXEL_WIDTH (f);
10710 int old_height = FRAME_PIXEL_HEIGHT (f);
10711 Lisp_Object fullscreen = get_frame_param (f, Qfullscreen);
10713 if (change_gravity) f->win_gravity = NorthWestGravity;
10714 x_wm_set_size_hint (f, 0, false);
10716 /* When the frame is fullheight and we only want to change the width
10717 or it is fullwidth and we only want to change the height we should
10718 be able to preserve the fullscreen property. However, due to the
10719 fact that we have to send a resize request anyway, the window
10720 manager will abolish it. At least the respective size should
10721 remain unchanged but giving the frame back its normal size will
10722 be broken ... */
10723 if (EQ (fullscreen, Qfullwidth) && width == FRAME_TEXT_WIDTH (f))
10725 frame_size_history_add
10726 (f, Qx_set_window_size_1, width, height,
10727 list2 (make_number (old_height),
10728 make_number (pixelheight + FRAME_MENUBAR_HEIGHT (f))));
10730 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10731 old_width, pixelheight + FRAME_MENUBAR_HEIGHT (f));
10733 else if (EQ (fullscreen, Qfullheight) && height == FRAME_TEXT_HEIGHT (f))
10735 frame_size_history_add
10736 (f, Qx_set_window_size_2, width, height,
10737 list2 (make_number (old_width), make_number (pixelwidth)));
10739 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10740 pixelwidth, old_height);
10743 else
10745 frame_size_history_add
10746 (f, Qx_set_window_size_3, width, height,
10747 list3 (make_number (pixelwidth + FRAME_TOOLBAR_WIDTH (f)),
10748 make_number (pixelheight + FRAME_TOOLBAR_HEIGHT (f)
10749 + FRAME_MENUBAR_HEIGHT (f)),
10750 make_number (FRAME_MENUBAR_HEIGHT (f))));
10752 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10753 pixelwidth, pixelheight + FRAME_MENUBAR_HEIGHT (f));
10754 fullscreen = Qnil;
10759 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
10760 receive in the ConfigureNotify event; if we get what we asked
10761 for, then the event won't cause the screen to become garbaged, so
10762 we have to make sure to do it here. */
10763 SET_FRAME_GARBAGED (f);
10765 /* Now, strictly speaking, we can't be sure that this is accurate,
10766 but the window manager will get around to dealing with the size
10767 change request eventually, and we'll hear how it went when the
10768 ConfigureNotify event gets here.
10770 We could just not bother storing any of this information here,
10771 and let the ConfigureNotify event set everything up, but that
10772 might be kind of confusing to the Lisp code, since size changes
10773 wouldn't be reported in the frame parameters until some random
10774 point in the future when the ConfigureNotify event arrives.
10776 Pass true for DELAY since we can't run Lisp code inside of
10777 a BLOCK_INPUT. */
10779 /* But the ConfigureNotify may in fact never arrive, and then this is
10780 not right if the frame is visible. Instead wait (with timeout)
10781 for the ConfigureNotify. */
10782 if (FRAME_VISIBLE_P (f))
10784 x_wait_for_event (f, ConfigureNotify);
10786 if (!NILP (fullscreen))
10787 /* Try to restore fullscreen state. */
10789 store_frame_param (f, Qfullscreen, fullscreen);
10790 x_set_fullscreen (f, fullscreen, fullscreen);
10793 else
10795 change_frame_size (f, width, height, false, true, false, true);
10796 x_sync (f);
10801 /* Call this to change the size of frame F's x-window.
10802 If CHANGE_GRAVITY, change to top-left-corner window gravity
10803 for this size change and subsequent size changes.
10804 Otherwise we leave the window gravity unchanged. */
10806 void
10807 x_set_window_size (struct frame *f, bool change_gravity,
10808 int width, int height, bool pixelwise)
10810 block_input ();
10812 /* The following breaks our calculations. If it's really needed,
10813 think of something else. */
10814 #if false
10815 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
10817 int text_width, text_height;
10819 /* When the frame is maximized/fullscreen or running under for
10820 example Xmonad, x_set_window_size_1 will be a no-op.
10821 In that case, the right thing to do is extend rows/width to
10822 the current frame size. We do that first if x_set_window_size_1
10823 turns out to not be a no-op (there is no way to know).
10824 The size will be adjusted again if the frame gets a
10825 ConfigureNotify event as a result of x_set_window_size. */
10826 int pixelh = FRAME_PIXEL_HEIGHT (f);
10827 #ifdef USE_X_TOOLKIT
10828 /* The menu bar is not part of text lines. The tool bar
10829 is however. */
10830 pixelh -= FRAME_MENUBAR_HEIGHT (f);
10831 #endif
10832 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, FRAME_PIXEL_WIDTH (f));
10833 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelh);
10835 change_frame_size (f, text_width, text_height, false, true, false, true);
10837 #endif
10839 /* Pixelize width and height, if necessary. */
10840 if (! pixelwise)
10842 width = width * FRAME_COLUMN_WIDTH (f);
10843 height = height * FRAME_LINE_HEIGHT (f);
10846 #ifdef USE_GTK
10847 if (FRAME_GTK_WIDGET (f))
10848 xg_frame_set_char_size (f, width, height);
10849 else
10850 x_set_window_size_1 (f, change_gravity, width, height);
10851 #else /* not USE_GTK */
10852 x_set_window_size_1 (f, change_gravity, width, height);
10853 x_clear_under_internal_border (f);
10854 #endif /* not USE_GTK */
10856 /* If cursor was outside the new size, mark it as off. */
10857 mark_window_cursors_off (XWINDOW (f->root_window));
10859 /* Clear out any recollection of where the mouse highlighting was,
10860 since it might be in a place that's outside the new frame size.
10861 Actually checking whether it is outside is a pain in the neck,
10862 so don't try--just let the highlighting be done afresh with new size. */
10863 cancel_mouse_face (f);
10865 unblock_input ();
10867 do_pending_window_change (false);
10870 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
10872 void
10873 frame_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
10875 block_input ();
10877 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
10878 0, 0, 0, 0, pix_x, pix_y);
10879 unblock_input ();
10882 /* Raise frame F. */
10884 void
10885 x_raise_frame (struct frame *f)
10887 block_input ();
10888 if (FRAME_VISIBLE_P (f))
10889 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
10890 XFlush (FRAME_X_DISPLAY (f));
10891 unblock_input ();
10894 /* Lower frame F. */
10896 static void
10897 x_lower_frame (struct frame *f)
10899 if (FRAME_VISIBLE_P (f))
10901 block_input ();
10902 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
10903 XFlush (FRAME_X_DISPLAY (f));
10904 unblock_input ();
10908 /* Request focus with XEmbed */
10910 void
10911 xembed_request_focus (struct frame *f)
10913 /* See XEmbed Protocol Specification at
10914 http://freedesktop.org/wiki/Specifications/xembed-spec */
10915 if (FRAME_VISIBLE_P (f))
10916 xembed_send_message (f, CurrentTime,
10917 XEMBED_REQUEST_FOCUS, 0, 0, 0);
10920 /* Activate frame with Extended Window Manager Hints */
10922 void
10923 x_ewmh_activate_frame (struct frame *f)
10925 /* See Window Manager Specification/Extended Window Manager Hints at
10926 http://freedesktop.org/wiki/Specifications/wm-spec */
10928 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10930 if (FRAME_VISIBLE_P (f) && x_wm_supports (f, dpyinfo->Xatom_net_active_window))
10932 Lisp_Object frame;
10933 XSETFRAME (frame, f);
10934 x_send_client_event (frame, make_number (0), frame,
10935 dpyinfo->Xatom_net_active_window,
10936 make_number (32),
10937 list2i (1, dpyinfo->last_user_time));
10941 static void
10942 XTframe_raise_lower (struct frame *f, bool raise_flag)
10944 if (raise_flag)
10945 x_raise_frame (f);
10946 else
10947 x_lower_frame (f);
10950 /* XEmbed implementation. */
10952 #if defined USE_X_TOOLKIT || ! defined USE_GTK
10954 /* XEmbed implementation. */
10956 #define XEMBED_VERSION 0
10958 static void
10959 xembed_set_info (struct frame *f, enum xembed_info flags)
10961 unsigned long data[2];
10962 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10964 data[0] = XEMBED_VERSION;
10965 data[1] = flags;
10967 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10968 dpyinfo->Xatom_XEMBED_INFO, dpyinfo->Xatom_XEMBED_INFO,
10969 32, PropModeReplace, (unsigned char *) data, 2);
10971 #endif /* defined USE_X_TOOLKIT || ! defined USE_GTK */
10973 static void
10974 xembed_send_message (struct frame *f, Time t, enum xembed_message msg,
10975 long int detail, long int data1, long int data2)
10977 XEvent event;
10979 event.xclient.type = ClientMessage;
10980 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
10981 event.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_XEMBED;
10982 event.xclient.format = 32;
10983 event.xclient.data.l[0] = t;
10984 event.xclient.data.l[1] = msg;
10985 event.xclient.data.l[2] = detail;
10986 event.xclient.data.l[3] = data1;
10987 event.xclient.data.l[4] = data2;
10989 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
10990 False, NoEventMask, &event);
10991 XSync (FRAME_X_DISPLAY (f), False);
10994 /* Change of visibility. */
10996 /* This function sends the request to make the frame visible, but may
10997 return before it the frame's visibility is changed. */
10999 void
11000 x_make_frame_visible (struct frame *f)
11002 block_input ();
11004 x_set_bitmap_icon (f);
11006 if (! FRAME_VISIBLE_P (f))
11008 /* We test asked_for_visible here to make sure we don't
11009 call x_set_offset a second time
11010 if we get to x_make_frame_visible a second time
11011 before the window gets really visible. */
11012 if (! FRAME_ICONIFIED_P (f)
11013 && ! FRAME_X_EMBEDDED_P (f)
11014 && ! f->output_data.x->asked_for_visible)
11015 x_set_offset (f, f->left_pos, f->top_pos, 0);
11017 f->output_data.x->asked_for_visible = true;
11019 if (! EQ (Vx_no_window_manager, Qt))
11020 x_wm_set_window_state (f, NormalState);
11021 #ifdef USE_X_TOOLKIT
11022 if (FRAME_X_EMBEDDED_P (f))
11023 xembed_set_info (f, XEMBED_MAPPED);
11024 else
11026 /* This was XtPopup, but that did nothing for an iconified frame. */
11027 XtMapWidget (f->output_data.x->widget);
11029 #else /* not USE_X_TOOLKIT */
11030 #ifdef USE_GTK
11031 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
11032 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
11033 #else
11034 if (FRAME_X_EMBEDDED_P (f))
11035 xembed_set_info (f, XEMBED_MAPPED);
11036 else
11037 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11038 #endif /* not USE_GTK */
11039 #endif /* not USE_X_TOOLKIT */
11042 XFlush (FRAME_X_DISPLAY (f));
11044 /* Synchronize to ensure Emacs knows the frame is visible
11045 before we do anything else. We do this loop with input not blocked
11046 so that incoming events are handled. */
11048 /* This must be before UNBLOCK_INPUT
11049 since events that arrive in response to the actions above
11050 will set it when they are handled. */
11051 bool previously_visible = f->output_data.x->has_been_visible;
11053 int original_left = f->left_pos;
11054 int original_top = f->top_pos;
11056 /* This must come after we set COUNT. */
11057 unblock_input ();
11059 /* We unblock here so that arriving X events are processed. */
11061 /* Now move the window back to where it was "supposed to be".
11062 But don't do it if the gravity is negative.
11063 When the gravity is negative, this uses a position
11064 that is 3 pixels too low. Perhaps that's really the border width.
11066 Don't do this if the window has never been visible before,
11067 because the window manager may choose the position
11068 and we don't want to override it. */
11070 if (! FRAME_VISIBLE_P (f)
11071 && ! FRAME_ICONIFIED_P (f)
11072 && ! FRAME_X_EMBEDDED_P (f)
11073 && f->win_gravity == NorthWestGravity
11074 && previously_visible)
11076 Drawable rootw;
11077 int x, y;
11078 unsigned int width, height, border, depth;
11080 block_input ();
11082 /* On some window managers (such as FVWM) moving an existing
11083 window, even to the same place, causes the window manager
11084 to introduce an offset. This can cause the window to move
11085 to an unexpected location. Check the geometry (a little
11086 slow here) and then verify that the window is in the right
11087 place. If the window is not in the right place, move it
11088 there, and take the potential window manager hit. */
11089 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11090 &rootw, &x, &y, &width, &height, &border, &depth);
11092 if (original_left != x || original_top != y)
11093 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11094 original_left, original_top);
11096 unblock_input ();
11101 /* Change from mapped state to withdrawn state. */
11103 /* Make the frame visible (mapped and not iconified). */
11105 void
11106 x_make_frame_invisible (struct frame *f)
11108 Window window;
11110 /* Use the frame's outermost window, not the one we normally draw on. */
11111 window = FRAME_OUTER_WINDOW (f);
11113 /* Don't keep the highlight on an invisible frame. */
11114 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
11115 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
11117 block_input ();
11119 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11120 that the current position of the window is user-specified, rather than
11121 program-specified, so that when the window is mapped again, it will be
11122 placed at the same location, without forcing the user to position it
11123 by hand again (they have already done that once for this window.) */
11124 x_wm_set_size_hint (f, 0, true);
11126 #ifdef USE_GTK
11127 if (FRAME_GTK_OUTER_WIDGET (f))
11128 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
11129 else
11130 #else
11131 if (FRAME_X_EMBEDDED_P (f))
11132 xembed_set_info (f, 0);
11133 else
11134 #endif
11137 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11138 DefaultScreen (FRAME_X_DISPLAY (f))))
11140 unblock_input ();
11141 error ("Can't notify window manager of window withdrawal");
11145 /* We can't distinguish this from iconification
11146 just by the event that we get from the server.
11147 So we can't win using the usual strategy of letting
11148 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11149 and synchronize with the server to make sure we agree. */
11150 SET_FRAME_VISIBLE (f, 0);
11151 SET_FRAME_ICONIFIED (f, false);
11153 x_sync (f);
11155 unblock_input ();
11158 /* Change window state from mapped to iconified. */
11160 void
11161 x_iconify_frame (struct frame *f)
11163 #ifdef USE_X_TOOLKIT
11164 int result;
11165 #endif
11167 /* Don't keep the highlight on an invisible frame. */
11168 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
11169 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
11171 if (FRAME_ICONIFIED_P (f))
11172 return;
11174 block_input ();
11176 x_set_bitmap_icon (f);
11178 #if defined (USE_GTK)
11179 if (FRAME_GTK_OUTER_WIDGET (f))
11181 if (! FRAME_VISIBLE_P (f))
11182 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
11184 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
11185 SET_FRAME_VISIBLE (f, 0);
11186 SET_FRAME_ICONIFIED (f, true);
11187 unblock_input ();
11188 return;
11190 #endif
11192 #ifdef USE_X_TOOLKIT
11194 if (! FRAME_VISIBLE_P (f))
11196 if (! EQ (Vx_no_window_manager, Qt))
11197 x_wm_set_window_state (f, IconicState);
11198 /* This was XtPopup, but that did nothing for an iconified frame. */
11199 XtMapWidget (f->output_data.x->widget);
11200 /* The server won't give us any event to indicate
11201 that an invisible frame was changed to an icon,
11202 so we have to record it here. */
11203 SET_FRAME_VISIBLE (f, 0);
11204 SET_FRAME_ICONIFIED (f, true);
11205 unblock_input ();
11206 return;
11209 result = XIconifyWindow (FRAME_X_DISPLAY (f),
11210 XtWindow (f->output_data.x->widget),
11211 DefaultScreen (FRAME_X_DISPLAY (f)));
11212 unblock_input ();
11214 if (!result)
11215 error ("Can't notify window manager of iconification");
11217 SET_FRAME_ICONIFIED (f, true);
11218 SET_FRAME_VISIBLE (f, 0);
11220 block_input ();
11221 XFlush (FRAME_X_DISPLAY (f));
11222 unblock_input ();
11223 #else /* not USE_X_TOOLKIT */
11225 /* Make sure the X server knows where the window should be positioned,
11226 in case the user deiconifies with the window manager. */
11227 if (! FRAME_VISIBLE_P (f)
11228 && ! FRAME_ICONIFIED_P (f)
11229 && ! FRAME_X_EMBEDDED_P (f))
11230 x_set_offset (f, f->left_pos, f->top_pos, 0);
11232 /* Since we don't know which revision of X we're running, we'll use both
11233 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11235 /* X11R4: send a ClientMessage to the window manager using the
11236 WM_CHANGE_STATE type. */
11238 XEvent msg;
11240 msg.xclient.window = FRAME_X_WINDOW (f);
11241 msg.xclient.type = ClientMessage;
11242 msg.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_wm_change_state;
11243 msg.xclient.format = 32;
11244 msg.xclient.data.l[0] = IconicState;
11246 if (! XSendEvent (FRAME_X_DISPLAY (f),
11247 DefaultRootWindow (FRAME_X_DISPLAY (f)),
11248 False,
11249 SubstructureRedirectMask | SubstructureNotifyMask,
11250 &msg))
11252 unblock_input ();
11253 error ("Can't notify window manager of iconification");
11257 /* X11R3: set the initial_state field of the window manager hints to
11258 IconicState. */
11259 x_wm_set_window_state (f, IconicState);
11261 if (!FRAME_VISIBLE_P (f))
11263 /* If the frame was withdrawn, before, we must map it. */
11264 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11267 SET_FRAME_ICONIFIED (f, true);
11268 SET_FRAME_VISIBLE (f, 0);
11270 XFlush (FRAME_X_DISPLAY (f));
11271 unblock_input ();
11272 #endif /* not USE_X_TOOLKIT */
11276 /* Free X resources of frame F. */
11278 void
11279 x_free_frame_resources (struct frame *f)
11281 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11282 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
11283 #ifdef USE_X_TOOLKIT
11284 Lisp_Object bar;
11285 struct scroll_bar *b;
11286 #endif
11288 block_input ();
11290 /* If a display connection is dead, don't try sending more
11291 commands to the X server. */
11292 if (dpyinfo->display)
11294 /* Always exit with visible pointer to avoid weird issue
11295 with Xfixes (Bug#17609). */
11296 if (f->pointer_invisible)
11297 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, 0);
11299 /* We must free faces before destroying windows because some
11300 font-driver (e.g. xft) access a window while finishing a
11301 face. */
11302 free_frame_faces (f);
11303 tear_down_x_back_buffer (f);
11305 if (f->output_data.x->icon_desc)
11306 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
11308 #ifdef USE_X_TOOLKIT
11309 /* Explicitly destroy the scroll bars of the frame. Without
11310 this, we get "BadDrawable" errors from the toolkit later on,
11311 presumably from expose events generated for the disappearing
11312 toolkit scroll bars. */
11313 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
11315 b = XSCROLL_BAR (bar);
11316 x_scroll_bar_remove (b);
11318 #endif
11320 #ifdef HAVE_X_I18N
11321 if (FRAME_XIC (f))
11322 free_frame_xic (f);
11323 #endif
11325 x_free_cr_resources (f);
11326 #ifdef USE_X_TOOLKIT
11327 if (f->output_data.x->widget)
11329 XtDestroyWidget (f->output_data.x->widget);
11330 f->output_data.x->widget = NULL;
11332 /* Tooltips don't have widgets, only a simple X window, even if
11333 we are using a toolkit. */
11334 else if (FRAME_X_WINDOW (f))
11335 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11337 free_frame_menubar (f);
11339 if (f->shell_position)
11340 xfree (f->shell_position);
11341 #else /* !USE_X_TOOLKIT */
11343 #ifdef USE_GTK
11344 xg_free_frame_widgets (f);
11345 #endif /* USE_GTK */
11347 tear_down_x_back_buffer (f);
11348 if (FRAME_X_WINDOW (f))
11349 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11350 #endif /* !USE_X_TOOLKIT */
11352 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
11353 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
11354 unload_color (f, f->output_data.x->cursor_pixel);
11355 unload_color (f, f->output_data.x->cursor_foreground_pixel);
11356 unload_color (f, f->output_data.x->border_pixel);
11357 unload_color (f, f->output_data.x->mouse_pixel);
11359 if (f->output_data.x->scroll_bar_background_pixel != -1)
11360 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
11361 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
11362 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
11363 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
11364 /* Scrollbar shadow colors. */
11365 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
11366 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
11367 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
11368 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
11369 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
11370 if (f->output_data.x->white_relief.pixel != -1)
11371 unload_color (f, f->output_data.x->white_relief.pixel);
11372 if (f->output_data.x->black_relief.pixel != -1)
11373 unload_color (f, f->output_data.x->black_relief.pixel);
11375 x_free_gcs (f);
11377 /* Free extra GCs allocated by x_setup_relief_colors. */
11378 if (f->output_data.x->white_relief.gc)
11380 XFreeGC (dpyinfo->display, f->output_data.x->white_relief.gc);
11381 f->output_data.x->white_relief.gc = 0;
11383 if (f->output_data.x->black_relief.gc)
11385 XFreeGC (dpyinfo->display, f->output_data.x->black_relief.gc);
11386 f->output_data.x->black_relief.gc = 0;
11389 /* Free cursors. */
11390 if (f->output_data.x->text_cursor != 0)
11391 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->text_cursor);
11392 if (f->output_data.x->nontext_cursor != 0)
11393 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->nontext_cursor);
11394 if (f->output_data.x->modeline_cursor != 0)
11395 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->modeline_cursor);
11396 if (f->output_data.x->hand_cursor != 0)
11397 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hand_cursor);
11398 if (f->output_data.x->hourglass_cursor != 0)
11399 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hourglass_cursor);
11400 if (f->output_data.x->horizontal_drag_cursor != 0)
11401 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->horizontal_drag_cursor);
11402 if (f->output_data.x->vertical_drag_cursor != 0)
11403 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->vertical_drag_cursor);
11405 XFlush (FRAME_X_DISPLAY (f));
11408 xfree (f->output_data.x->saved_menu_event);
11409 xfree (f->output_data.x);
11410 f->output_data.x = NULL;
11412 if (f == dpyinfo->x_focus_frame)
11413 dpyinfo->x_focus_frame = 0;
11414 if (f == dpyinfo->x_focus_event_frame)
11415 dpyinfo->x_focus_event_frame = 0;
11416 if (f == dpyinfo->x_highlight_frame)
11417 dpyinfo->x_highlight_frame = 0;
11418 if (f == hlinfo->mouse_face_mouse_frame)
11419 reset_mouse_highlight (hlinfo);
11421 unblock_input ();
11425 /* Destroy the X window of frame F. */
11427 static void
11428 x_destroy_window (struct frame *f)
11430 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11432 /* If a display connection is dead, don't try sending more
11433 commands to the X server. */
11434 if (dpyinfo->display != 0)
11435 x_free_frame_resources (f);
11437 dpyinfo->reference_count--;
11441 /* Setting window manager hints. */
11443 /* Set the normal size hints for the window manager, for frame F.
11444 FLAGS is the flags word to use--or 0 meaning preserve the flags
11445 that the window now has.
11446 If USER_POSITION, set the USPosition
11447 flag (this is useful when FLAGS is 0).
11448 The GTK version is in gtkutils.c. */
11450 #ifndef USE_GTK
11451 void
11452 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
11454 XSizeHints size_hints;
11455 Window window = FRAME_OUTER_WINDOW (f);
11457 if (!window)
11458 return;
11460 #ifdef USE_X_TOOLKIT
11461 if (f->output_data.x->widget)
11463 widget_update_wm_size_hints (f->output_data.x->widget);
11464 return;
11466 #endif
11468 /* Setting PMaxSize caused various problems. */
11469 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
11471 size_hints.x = f->left_pos;
11472 size_hints.y = f->top_pos;
11474 size_hints.width = FRAME_PIXEL_WIDTH (f);
11475 size_hints.height = FRAME_PIXEL_HEIGHT (f);
11477 size_hints.width_inc = frame_resize_pixelwise ? 1 : FRAME_COLUMN_WIDTH (f);
11478 size_hints.height_inc = frame_resize_pixelwise ? 1 : FRAME_LINE_HEIGHT (f);
11480 size_hints.max_width = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
11481 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
11482 size_hints.max_height = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
11483 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
11485 /* Calculate the base and minimum sizes. */
11487 int base_width, base_height;
11489 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
11490 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
11492 /* The window manager uses the base width hints to calculate the
11493 current number of rows and columns in the frame while
11494 resizing; min_width and min_height aren't useful for this
11495 purpose, since they might not give the dimensions for a
11496 zero-row, zero-column frame. */
11498 size_hints.flags |= PBaseSize;
11499 size_hints.base_width = base_width;
11500 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
11501 size_hints.min_width = base_width;
11502 size_hints.min_height = base_height;
11505 /* If we don't need the old flags, we don't need the old hint at all. */
11506 if (flags)
11508 size_hints.flags |= flags;
11509 goto no_read;
11513 XSizeHints hints; /* Sometimes I hate X Windows... */
11514 long supplied_return;
11515 int value;
11517 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
11518 &supplied_return);
11520 if (flags)
11521 size_hints.flags |= flags;
11522 else
11524 if (value == 0)
11525 hints.flags = 0;
11526 if (hints.flags & PSize)
11527 size_hints.flags |= PSize;
11528 if (hints.flags & PPosition)
11529 size_hints.flags |= PPosition;
11530 if (hints.flags & USPosition)
11531 size_hints.flags |= USPosition;
11532 if (hints.flags & USSize)
11533 size_hints.flags |= USSize;
11537 no_read:
11539 #ifdef PWinGravity
11540 size_hints.win_gravity = f->win_gravity;
11541 size_hints.flags |= PWinGravity;
11543 if (user_position)
11545 size_hints.flags &= ~ PPosition;
11546 size_hints.flags |= USPosition;
11548 #endif /* PWinGravity */
11550 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
11552 #endif /* not USE_GTK */
11554 /* Used for IconicState or NormalState */
11556 static void
11557 x_wm_set_window_state (struct frame *f, int state)
11559 #ifdef USE_X_TOOLKIT
11560 Arg al[1];
11562 XtSetArg (al[0], XtNinitialState, state);
11563 XtSetValues (f->output_data.x->widget, al, 1);
11564 #else /* not USE_X_TOOLKIT */
11565 Window window = FRAME_X_WINDOW (f);
11567 f->output_data.x->wm_hints.flags |= StateHint;
11568 f->output_data.x->wm_hints.initial_state = state;
11570 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
11571 #endif /* not USE_X_TOOLKIT */
11574 static void
11575 x_wm_set_icon_pixmap (struct frame *f, ptrdiff_t pixmap_id)
11577 Pixmap icon_pixmap, icon_mask;
11579 #if !defined USE_X_TOOLKIT && !defined USE_GTK
11580 Window window = FRAME_OUTER_WINDOW (f);
11581 #endif
11583 if (pixmap_id > 0)
11585 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
11586 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
11587 icon_mask = x_bitmap_mask (f, pixmap_id);
11588 f->output_data.x->wm_hints.icon_mask = icon_mask;
11590 else
11592 /* It seems there is no way to turn off use of an icon
11593 pixmap. */
11594 return;
11598 #ifdef USE_GTK
11600 xg_set_frame_icon (f, icon_pixmap, icon_mask);
11601 return;
11604 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
11607 Arg al[1];
11608 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
11609 XtSetValues (f->output_data.x->widget, al, 1);
11610 XtSetArg (al[0], XtNiconMask, icon_mask);
11611 XtSetValues (f->output_data.x->widget, al, 1);
11614 #else /* not USE_X_TOOLKIT && not USE_GTK */
11616 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
11617 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
11619 #endif /* not USE_X_TOOLKIT && not USE_GTK */
11622 void
11623 x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
11625 Window window = FRAME_OUTER_WINDOW (f);
11627 f->output_data.x->wm_hints.flags |= IconPositionHint;
11628 f->output_data.x->wm_hints.icon_x = icon_x;
11629 f->output_data.x->wm_hints.icon_y = icon_y;
11631 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
11635 /***********************************************************************
11636 Fonts
11637 ***********************************************************************/
11639 #ifdef GLYPH_DEBUG
11641 /* Check that FONT is valid on frame F. It is if it can be found in F's
11642 font table. */
11644 static void
11645 x_check_font (struct frame *f, struct font *font)
11647 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
11648 if (font->driver->check)
11649 eassert (font->driver->check (f, font) == 0);
11652 #endif /* GLYPH_DEBUG */
11655 /***********************************************************************
11656 Initialization
11657 ***********************************************************************/
11659 #ifdef USE_X_TOOLKIT
11660 static XrmOptionDescRec emacs_options[] = {
11661 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
11662 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
11664 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
11665 XrmoptionSepArg, NULL},
11666 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
11668 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
11669 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
11670 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
11671 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
11672 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
11673 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
11674 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
11677 /* Whether atimer for Xt timeouts is activated or not. */
11679 static bool x_timeout_atimer_activated_flag;
11681 #endif /* USE_X_TOOLKIT */
11683 static int x_initialized;
11685 /* Test whether two display-name strings agree up to the dot that separates
11686 the screen number from the server number. */
11687 static bool
11688 same_x_server (const char *name1, const char *name2)
11690 bool seen_colon = false;
11691 Lisp_Object sysname = Fsystem_name ();
11692 const char *system_name = SSDATA (sysname);
11693 ptrdiff_t system_name_length = SBYTES (sysname);
11694 ptrdiff_t length_until_period = 0;
11696 while (system_name[length_until_period] != 0
11697 && system_name[length_until_period] != '.')
11698 length_until_period++;
11700 /* Treat `unix' like an empty host name. */
11701 if (! strncmp (name1, "unix:", 5))
11702 name1 += 4;
11703 if (! strncmp (name2, "unix:", 5))
11704 name2 += 4;
11705 /* Treat this host's name like an empty host name. */
11706 if (! strncmp (name1, system_name, system_name_length)
11707 && name1[system_name_length] == ':')
11708 name1 += system_name_length;
11709 if (! strncmp (name2, system_name, system_name_length)
11710 && name2[system_name_length] == ':')
11711 name2 += system_name_length;
11712 /* Treat this host's domainless name like an empty host name. */
11713 if (! strncmp (name1, system_name, length_until_period)
11714 && name1[length_until_period] == ':')
11715 name1 += length_until_period;
11716 if (! strncmp (name2, system_name, length_until_period)
11717 && name2[length_until_period] == ':')
11718 name2 += length_until_period;
11720 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
11722 if (*name1 == ':')
11723 seen_colon = true;
11724 if (seen_colon && *name1 == '.')
11725 return true;
11727 return (seen_colon
11728 && (*name1 == '.' || *name1 == '\0')
11729 && (*name2 == '.' || *name2 == '\0'));
11732 /* Count number of set bits in mask and number of bits to shift to
11733 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
11734 to 5. */
11735 static void
11736 get_bits_and_offset (unsigned long mask, int *bits, int *offset)
11738 int nr = 0;
11739 int off = 0;
11741 while (!(mask & 1))
11743 off++;
11744 mask >>= 1;
11747 while (mask & 1)
11749 nr++;
11750 mask >>= 1;
11753 *offset = off;
11754 *bits = nr;
11757 /* Return true iff display DISPLAY is available for use.
11758 But don't permanently open it, just test its availability. */
11760 bool
11761 x_display_ok (const char *display)
11763 /* XOpenDisplay fails if it gets a signal. Block SIGIO which may arrive. */
11764 unrequest_sigio ();
11765 Display *dpy = XOpenDisplay (display);
11766 request_sigio ();
11767 if (!dpy)
11768 return false;
11769 XCloseDisplay (dpy);
11770 return true;
11773 #ifdef USE_GTK
11774 static void
11775 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
11776 const gchar *msg, gpointer user_data)
11778 if (!strstr (msg, "g_set_prgname"))
11779 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, msg);
11781 #endif
11783 /* Create invisible cursor on X display referred by DPYINFO. */
11785 static Cursor
11786 make_invisible_cursor (struct x_display_info *dpyinfo)
11788 Display *dpy = dpyinfo->display;
11789 static char const no_data[] = { 0 };
11790 Pixmap pix;
11791 XColor col;
11792 Cursor c = 0;
11794 x_catch_errors (dpy);
11795 pix = XCreateBitmapFromData (dpy, dpyinfo->root_window, no_data, 1, 1);
11796 if (! x_had_errors_p (dpy) && pix != None)
11798 Cursor pixc;
11799 col.pixel = 0;
11800 col.red = col.green = col.blue = 0;
11801 col.flags = DoRed | DoGreen | DoBlue;
11802 pixc = XCreatePixmapCursor (dpy, pix, pix, &col, &col, 0, 0);
11803 if (! x_had_errors_p (dpy) && pixc != None)
11804 c = pixc;
11805 XFreePixmap (dpy, pix);
11808 x_uncatch_errors ();
11810 return c;
11813 /* True if DPY supports Xfixes extension >= 4. */
11815 static bool
11816 x_probe_xfixes_extension (Display *dpy)
11818 #ifdef HAVE_XFIXES
11819 int major, minor;
11820 return XFixesQueryVersion (dpy, &major, &minor) && major >= 4;
11821 #else
11822 return false;
11823 #endif /* HAVE_XFIXES */
11826 /* Toggle mouse pointer visibility on frame F by using Xfixes functions. */
11828 static void
11829 xfixes_toggle_visible_pointer (struct frame *f, bool invisible)
11831 #ifdef HAVE_XFIXES
11832 if (invisible)
11833 XFixesHideCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11834 else
11835 XFixesShowCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11836 f->pointer_invisible = invisible;
11837 #else
11838 emacs_abort ();
11839 #endif /* HAVE_XFIXES */
11842 /* Toggle mouse pointer visibility on frame F by using invisible cursor. */
11844 static void
11845 x_toggle_visible_pointer (struct frame *f, bool invisible)
11847 eassert (FRAME_DISPLAY_INFO (f)->invisible_cursor != 0);
11848 if (invisible)
11849 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11850 FRAME_DISPLAY_INFO (f)->invisible_cursor);
11851 else
11852 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11853 f->output_data.x->current_cursor);
11854 f->pointer_invisible = invisible;
11857 /* Setup pointer blanking, prefer Xfixes if available. */
11859 static void
11860 x_setup_pointer_blanking (struct x_display_info *dpyinfo)
11862 /* FIXME: the brave tester should set EMACS_XFIXES because we're suspecting
11863 X server bug, see http://debbugs.gnu.org/cgi/bugreport.cgi?bug=17609. */
11864 if (egetenv ("EMACS_XFIXES") && x_probe_xfixes_extension (dpyinfo->display))
11865 dpyinfo->toggle_visible_pointer = xfixes_toggle_visible_pointer;
11866 else
11868 dpyinfo->toggle_visible_pointer = x_toggle_visible_pointer;
11869 dpyinfo->invisible_cursor = make_invisible_cursor (dpyinfo);
11873 /* Current X display connection identifier. Incremented for each next
11874 connection established. */
11875 static unsigned x_display_id;
11877 /* Open a connection to X display DISPLAY_NAME, and return
11878 the structure that describes the open display.
11879 If we cannot contact the display, return null. */
11881 struct x_display_info *
11882 x_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
11884 Display *dpy;
11885 struct terminal *terminal;
11886 struct x_display_info *dpyinfo;
11887 XrmDatabase xrdb;
11888 #ifdef USE_XCB
11889 xcb_connection_t *xcb_conn;
11890 #endif
11892 block_input ();
11894 if (!x_initialized)
11896 x_initialize ();
11897 ++x_initialized;
11900 if (! x_display_ok (SSDATA (display_name)))
11901 error ("Display %s can't be opened", SSDATA (display_name));
11903 #ifdef USE_GTK
11905 #define NUM_ARGV 10
11906 int argc;
11907 char *argv[NUM_ARGV];
11908 char **argv2 = argv;
11909 guint id;
11911 if (x_initialized++ > 1)
11913 xg_display_open (SSDATA (display_name), &dpy);
11915 else
11917 static char display_opt[] = "--display";
11918 static char name_opt[] = "--name";
11920 for (argc = 0; argc < NUM_ARGV; ++argc)
11921 argv[argc] = 0;
11923 argc = 0;
11924 argv[argc++] = initial_argv[0];
11926 if (! NILP (display_name))
11928 argv[argc++] = display_opt;
11929 argv[argc++] = SSDATA (display_name);
11932 argv[argc++] = name_opt;
11933 argv[argc++] = resource_name;
11935 XSetLocaleModifiers ("");
11937 /* Work around GLib bug that outputs a faulty warning. See
11938 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
11939 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
11940 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
11942 /* NULL window -> events for all windows go to our function.
11943 Call before gtk_init so Gtk+ event filters comes after our. */
11944 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
11946 /* gtk_init does set_locale. Fix locale before and after. */
11947 fixup_locale ();
11948 unrequest_sigio (); /* See comment in x_display_ok. */
11949 gtk_init (&argc, &argv2);
11950 request_sigio ();
11951 fixup_locale ();
11953 g_log_remove_handler ("GLib", id);
11955 xg_initialize ();
11957 dpy = DEFAULT_GDK_DISPLAY ();
11959 #if ! GTK_CHECK_VERSION (2, 90, 0)
11960 /* Load our own gtkrc if it exists. */
11962 const char *file = "~/.emacs.d/gtkrc";
11963 Lisp_Object s, abs_file;
11965 s = build_string (file);
11966 abs_file = Fexpand_file_name (s, Qnil);
11968 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
11969 gtk_rc_parse (SSDATA (abs_file));
11971 #endif
11973 XSetErrorHandler (x_error_handler);
11974 XSetIOErrorHandler (x_io_error_quitter);
11977 #else /* not USE_GTK */
11978 #ifdef USE_X_TOOLKIT
11979 /* weiner@footloose.sps.mot.com reports that this causes
11980 errors with X11R5:
11981 X protocol error: BadAtom (invalid Atom parameter)
11982 on protocol request 18skiloaf.
11983 So let's not use it until R6. */
11984 #ifdef HAVE_X11XTR6
11985 XtSetLanguageProc (NULL, NULL, NULL);
11986 #endif
11989 int argc = 0;
11990 char *argv[3];
11992 argv[0] = "";
11993 argc = 1;
11994 if (xrm_option)
11996 argv[argc++] = "-xrm";
11997 argv[argc++] = xrm_option;
11999 turn_on_atimers (false);
12000 unrequest_sigio (); /* See comment in x_display_ok. */
12001 dpy = XtOpenDisplay (Xt_app_con, SSDATA (display_name),
12002 resource_name, EMACS_CLASS,
12003 emacs_options, XtNumber (emacs_options),
12004 &argc, argv);
12005 request_sigio ();
12006 turn_on_atimers (true);
12008 #ifdef HAVE_X11XTR6
12009 /* I think this is to compensate for XtSetLanguageProc. */
12010 fixup_locale ();
12011 #endif
12014 #else /* not USE_X_TOOLKIT */
12015 XSetLocaleModifiers ("");
12016 unrequest_sigio (); /* See comment in x_display_ok. */
12017 dpy = XOpenDisplay (SSDATA (display_name));
12018 request_sigio ();
12019 #endif /* not USE_X_TOOLKIT */
12020 #endif /* not USE_GTK*/
12022 /* Detect failure. */
12023 if (dpy == 0)
12025 unblock_input ();
12026 return 0;
12029 #ifdef USE_XCB
12030 xcb_conn = XGetXCBConnection (dpy);
12031 if (xcb_conn == 0)
12033 #ifdef USE_GTK
12034 xg_display_close (dpy);
12035 #else
12036 #ifdef USE_X_TOOLKIT
12037 XtCloseDisplay (dpy);
12038 #else
12039 XCloseDisplay (dpy);
12040 #endif
12041 #endif /* ! USE_GTK */
12043 unblock_input ();
12044 return 0;
12046 #endif
12048 /* We have definitely succeeded. Record the new connection. */
12050 dpyinfo = xzalloc (sizeof *dpyinfo);
12051 terminal = x_create_terminal (dpyinfo);
12054 struct x_display_info *share;
12056 for (share = x_display_list; share; share = share->next)
12057 if (same_x_server (SSDATA (XCAR (share->name_list_element)),
12058 SSDATA (display_name)))
12059 break;
12060 if (share)
12061 terminal->kboard = share->terminal->kboard;
12062 else
12064 terminal->kboard = allocate_kboard (Qx);
12066 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
12068 char *vendor = ServerVendor (dpy);
12070 /* Temporarily hide the partially initialized terminal. */
12071 terminal_list = terminal->next_terminal;
12072 unblock_input ();
12073 kset_system_key_alist
12074 (terminal->kboard,
12075 call1 (Qvendor_specific_keysyms,
12076 vendor ? build_string (vendor) : empty_unibyte_string));
12077 block_input ();
12078 terminal->next_terminal = terminal_list;
12079 terminal_list = terminal;
12082 /* Don't let the initial kboard remain current longer than necessary.
12083 That would cause problems if a file loaded on startup tries to
12084 prompt in the mini-buffer. */
12085 if (current_kboard == initial_kboard)
12086 current_kboard = terminal->kboard;
12088 terminal->kboard->reference_count++;
12091 /* Put this display on the chain. */
12092 dpyinfo->next = x_display_list;
12093 x_display_list = dpyinfo;
12095 dpyinfo->name_list_element = Fcons (display_name, Qnil);
12096 dpyinfo->display = dpy;
12097 dpyinfo->connection = ConnectionNumber (dpyinfo->display);
12098 #ifdef USE_XCB
12099 dpyinfo->xcb_connection = xcb_conn;
12100 #endif
12102 /* http://lists.gnu.org/archive/html/emacs-devel/2015-11/msg00194.html */
12103 dpyinfo->smallest_font_height = 1;
12104 dpyinfo->smallest_char_width = 1;
12106 /* Set the name of the terminal. */
12107 terminal->name = xlispstrdup (display_name);
12109 #if false
12110 XSetAfterFunction (x_current_display, x_trace_wire);
12111 #endif
12113 Lisp_Object system_name = Fsystem_name ();
12114 ptrdiff_t nbytes;
12115 if (INT_ADD_WRAPV (SBYTES (Vinvocation_name), SBYTES (system_name) + 2,
12116 &nbytes))
12117 memory_full (SIZE_MAX);
12118 dpyinfo->x_id = ++x_display_id;
12119 dpyinfo->x_id_name = xmalloc (nbytes);
12120 char *nametail = lispstpcpy (dpyinfo->x_id_name, Vinvocation_name);
12121 *nametail++ = '@';
12122 lispstpcpy (nametail, system_name);
12124 /* Figure out which modifier bits mean what. */
12125 x_find_modifier_meanings (dpyinfo);
12127 /* Get the scroll bar cursor. */
12128 #ifdef USE_GTK
12129 /* We must create a GTK cursor, it is required for GTK widgets. */
12130 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
12131 #endif /* USE_GTK */
12133 dpyinfo->vertical_scroll_bar_cursor
12134 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
12136 dpyinfo->horizontal_scroll_bar_cursor
12137 = XCreateFontCursor (dpyinfo->display, XC_sb_h_double_arrow);
12139 xrdb = x_load_resources (dpyinfo->display, xrm_option,
12140 resource_name, EMACS_CLASS);
12141 #ifdef HAVE_XRMSETDATABASE
12142 XrmSetDatabase (dpyinfo->display, xrdb);
12143 #else
12144 dpyinfo->display->db = xrdb;
12145 #endif
12146 /* Put the rdb where we can find it in a way that works on
12147 all versions. */
12148 dpyinfo->xrdb = xrdb;
12150 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
12151 DefaultScreen (dpyinfo->display));
12152 select_visual (dpyinfo);
12153 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
12154 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
12155 dpyinfo->icon_bitmap_id = -1;
12156 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
12158 reset_mouse_highlight (&dpyinfo->mouse_highlight);
12160 /* See if we can construct pixel values from RGB values. */
12161 if (dpyinfo->visual->class == TrueColor)
12163 get_bits_and_offset (dpyinfo->visual->red_mask,
12164 &dpyinfo->red_bits, &dpyinfo->red_offset);
12165 get_bits_and_offset (dpyinfo->visual->blue_mask,
12166 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
12167 get_bits_and_offset (dpyinfo->visual->green_mask,
12168 &dpyinfo->green_bits, &dpyinfo->green_offset);
12171 /* See if a private colormap is requested. */
12172 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
12174 if (dpyinfo->visual->class == PseudoColor)
12176 AUTO_STRING (privateColormap, "privateColormap");
12177 AUTO_STRING (PrivateColormap, "PrivateColormap");
12178 Lisp_Object value
12179 = display_x_get_resource (dpyinfo, privateColormap,
12180 PrivateColormap, Qnil, Qnil);
12181 if (STRINGP (value)
12182 && (!strcmp (SSDATA (value), "true")
12183 || !strcmp (SSDATA (value), "on")))
12184 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
12187 else
12188 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
12189 dpyinfo->visual, AllocNone);
12191 #ifdef HAVE_XDBE
12192 dpyinfo->supports_xdbe = false;
12193 int xdbe_major;
12194 int xdbe_minor;
12195 if (XdbeQueryExtension (dpyinfo->display, &xdbe_major, &xdbe_minor))
12196 dpyinfo->supports_xdbe = true;
12197 #endif
12199 #ifdef HAVE_XFT
12201 /* If we are using Xft, the following precautions should be made:
12203 1. Make sure that the Xrender extension is added before the Xft one.
12204 Otherwise, the close-display hook set by Xft is called after the one
12205 for Xrender, and the former tries to re-add the latter. This results
12206 in inconsistency of internal states and leads to X protocol error when
12207 one reconnects to the same X server (Bug#1696).
12209 2. Check dpi value in X resources. It is better we use it as well,
12210 since Xft will use it, as will all Gnome applications. If our real DPI
12211 is smaller or larger than the one Xft uses, our font will look smaller
12212 or larger than other for other applications, even if it is the same
12213 font name (monospace-10 for example). */
12215 int event_base, error_base;
12216 char *v;
12217 double d;
12219 XRenderQueryExtension (dpyinfo->display, &event_base, &error_base);
12221 v = XGetDefault (dpyinfo->display, "Xft", "dpi");
12222 if (v != NULL && sscanf (v, "%lf", &d) == 1)
12223 dpyinfo->resy = dpyinfo->resx = d;
12225 #endif
12227 if (dpyinfo->resy < 1)
12229 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
12230 double pixels = DisplayHeight (dpyinfo->display, screen_number);
12231 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
12232 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
12233 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
12234 pixels = DisplayWidth (dpyinfo->display, screen_number);
12235 mm = DisplayWidthMM (dpyinfo->display, screen_number);
12236 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
12237 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
12241 static const struct
12243 const char *name;
12244 int offset;
12245 } atom_refs[] = {
12246 #define ATOM_REFS_INIT(string, member) \
12247 { string, offsetof (struct x_display_info, member) },
12248 ATOM_REFS_INIT ("WM_PROTOCOLS", Xatom_wm_protocols)
12249 ATOM_REFS_INIT ("WM_TAKE_FOCUS", Xatom_wm_take_focus)
12250 ATOM_REFS_INIT ("WM_SAVE_YOURSELF", Xatom_wm_save_yourself)
12251 ATOM_REFS_INIT ("WM_DELETE_WINDOW", Xatom_wm_delete_window)
12252 ATOM_REFS_INIT ("WM_CHANGE_STATE", Xatom_wm_change_state)
12253 ATOM_REFS_INIT ("WM_CONFIGURE_DENIED", Xatom_wm_configure_denied)
12254 ATOM_REFS_INIT ("WM_MOVED", Xatom_wm_window_moved)
12255 ATOM_REFS_INIT ("WM_CLIENT_LEADER", Xatom_wm_client_leader)
12256 ATOM_REFS_INIT ("Editres", Xatom_editres)
12257 ATOM_REFS_INIT ("CLIPBOARD", Xatom_CLIPBOARD)
12258 ATOM_REFS_INIT ("TIMESTAMP", Xatom_TIMESTAMP)
12259 ATOM_REFS_INIT ("TEXT", Xatom_TEXT)
12260 ATOM_REFS_INIT ("COMPOUND_TEXT", Xatom_COMPOUND_TEXT)
12261 ATOM_REFS_INIT ("UTF8_STRING", Xatom_UTF8_STRING)
12262 ATOM_REFS_INIT ("DELETE", Xatom_DELETE)
12263 ATOM_REFS_INIT ("MULTIPLE", Xatom_MULTIPLE)
12264 ATOM_REFS_INIT ("INCR", Xatom_INCR)
12265 ATOM_REFS_INIT ("_EMACS_TMP_", Xatom_EMACS_TMP)
12266 ATOM_REFS_INIT ("TARGETS", Xatom_TARGETS)
12267 ATOM_REFS_INIT ("NULL", Xatom_NULL)
12268 ATOM_REFS_INIT ("ATOM", Xatom_ATOM)
12269 ATOM_REFS_INIT ("ATOM_PAIR", Xatom_ATOM_PAIR)
12270 ATOM_REFS_INIT ("CLIPBOARD_MANAGER", Xatom_CLIPBOARD_MANAGER)
12271 ATOM_REFS_INIT ("_XEMBED_INFO", Xatom_XEMBED_INFO)
12272 /* For properties of font. */
12273 ATOM_REFS_INIT ("PIXEL_SIZE", Xatom_PIXEL_SIZE)
12274 ATOM_REFS_INIT ("AVERAGE_WIDTH", Xatom_AVERAGE_WIDTH)
12275 ATOM_REFS_INIT ("_MULE_BASELINE_OFFSET", Xatom_MULE_BASELINE_OFFSET)
12276 ATOM_REFS_INIT ("_MULE_RELATIVE_COMPOSE", Xatom_MULE_RELATIVE_COMPOSE)
12277 ATOM_REFS_INIT ("_MULE_DEFAULT_ASCENT", Xatom_MULE_DEFAULT_ASCENT)
12278 /* Ghostscript support. */
12279 ATOM_REFS_INIT ("DONE", Xatom_DONE)
12280 ATOM_REFS_INIT ("PAGE", Xatom_PAGE)
12281 ATOM_REFS_INIT ("SCROLLBAR", Xatom_Scrollbar)
12282 ATOM_REFS_INIT ("HORIZONTAL_SCROLLBAR", Xatom_Horizontal_Scrollbar)
12283 ATOM_REFS_INIT ("_XEMBED", Xatom_XEMBED)
12284 /* EWMH */
12285 ATOM_REFS_INIT ("_NET_WM_STATE", Xatom_net_wm_state)
12286 ATOM_REFS_INIT ("_NET_WM_STATE_FULLSCREEN", Xatom_net_wm_state_fullscreen)
12287 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_HORZ",
12288 Xatom_net_wm_state_maximized_horz)
12289 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_VERT",
12290 Xatom_net_wm_state_maximized_vert)
12291 ATOM_REFS_INIT ("_NET_WM_STATE_STICKY", Xatom_net_wm_state_sticky)
12292 ATOM_REFS_INIT ("_NET_WM_STATE_HIDDEN", Xatom_net_wm_state_hidden)
12293 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE", Xatom_net_window_type)
12294 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE_TOOLTIP",
12295 Xatom_net_window_type_tooltip)
12296 ATOM_REFS_INIT ("_NET_WM_ICON_NAME", Xatom_net_wm_icon_name)
12297 ATOM_REFS_INIT ("_NET_WM_NAME", Xatom_net_wm_name)
12298 ATOM_REFS_INIT ("_NET_SUPPORTED", Xatom_net_supported)
12299 ATOM_REFS_INIT ("_NET_SUPPORTING_WM_CHECK", Xatom_net_supporting_wm_check)
12300 ATOM_REFS_INIT ("_NET_WM_WINDOW_OPACITY", Xatom_net_wm_window_opacity)
12301 ATOM_REFS_INIT ("_NET_ACTIVE_WINDOW", Xatom_net_active_window)
12302 ATOM_REFS_INIT ("_NET_FRAME_EXTENTS", Xatom_net_frame_extents)
12303 ATOM_REFS_INIT ("_NET_CURRENT_DESKTOP", Xatom_net_current_desktop)
12304 ATOM_REFS_INIT ("_NET_WORKAREA", Xatom_net_workarea)
12305 /* Session management */
12306 ATOM_REFS_INIT ("SM_CLIENT_ID", Xatom_SM_CLIENT_ID)
12307 ATOM_REFS_INIT ("_XSETTINGS_SETTINGS", Xatom_xsettings_prop)
12308 ATOM_REFS_INIT ("MANAGER", Xatom_xsettings_mgr)
12311 int i;
12312 enum { atom_count = ARRAYELTS (atom_refs) };
12313 /* 1 for _XSETTINGS_SN. */
12314 enum { total_atom_count = 1 + atom_count };
12315 Atom atoms_return[total_atom_count];
12316 char *atom_names[total_atom_count];
12317 static char const xsettings_fmt[] = "_XSETTINGS_S%d";
12318 char xsettings_atom_name[sizeof xsettings_fmt - 2
12319 + INT_STRLEN_BOUND (int)];
12321 for (i = 0; i < atom_count; i++)
12322 atom_names[i] = (char *) atom_refs[i].name;
12324 /* Build _XSETTINGS_SN atom name. */
12325 sprintf (xsettings_atom_name, xsettings_fmt,
12326 XScreenNumberOfScreen (dpyinfo->screen));
12327 atom_names[i] = xsettings_atom_name;
12329 XInternAtoms (dpyinfo->display, atom_names, total_atom_count,
12330 False, atoms_return);
12332 for (i = 0; i < atom_count; i++)
12333 *(Atom *) ((char *) dpyinfo + atom_refs[i].offset) = atoms_return[i];
12335 /* Manually copy last atom. */
12336 dpyinfo->Xatom_xsettings_sel = atoms_return[i];
12339 dpyinfo->x_dnd_atoms_size = 8;
12340 dpyinfo->x_dnd_atoms = xmalloc (sizeof *dpyinfo->x_dnd_atoms
12341 * dpyinfo->x_dnd_atoms_size);
12342 dpyinfo->gray
12343 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12344 gray_bits, gray_width, gray_height,
12345 1, 0, 1);
12347 x_setup_pointer_blanking (dpyinfo);
12349 #ifdef HAVE_X_I18N
12350 xim_initialize (dpyinfo, resource_name);
12351 #endif
12353 xsettings_initialize (dpyinfo);
12355 /* This is only needed for distinguishing keyboard and process input. */
12356 if (dpyinfo->connection != 0)
12357 add_keyboard_wait_descriptor (dpyinfo->connection);
12359 #ifdef F_SETOWN
12360 fcntl (dpyinfo->connection, F_SETOWN, getpid ());
12361 #endif /* ! defined (F_SETOWN) */
12363 if (interrupt_input)
12364 init_sigio (dpyinfo->connection);
12366 #ifdef USE_LUCID
12368 XrmValue d, fr, to;
12369 Font font;
12371 dpy = dpyinfo->display;
12372 d.addr = (XPointer)&dpy;
12373 d.size = sizeof (Display *);
12374 fr.addr = XtDefaultFont;
12375 fr.size = sizeof (XtDefaultFont);
12376 to.size = sizeof (Font *);
12377 to.addr = (XPointer)&font;
12378 x_catch_errors (dpy);
12379 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
12380 emacs_abort ();
12381 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
12382 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
12383 /* Do not free XFontStruct returned by the above call to XQueryFont.
12384 This leads to X protocol errors at XtCloseDisplay (Bug#18403). */
12385 x_uncatch_errors ();
12387 #endif
12389 /* See if we should run in synchronous mode. This is useful
12390 for debugging X code. */
12392 AUTO_STRING (synchronous, "synchronous");
12393 AUTO_STRING (Synchronous, "Synchronous");
12394 Lisp_Object value = display_x_get_resource (dpyinfo, synchronous,
12395 Synchronous, Qnil, Qnil);
12396 if (STRINGP (value)
12397 && (!strcmp (SSDATA (value), "true")
12398 || !strcmp (SSDATA (value), "on")))
12399 XSynchronize (dpyinfo->display, True);
12403 AUTO_STRING (useXIM, "useXIM");
12404 AUTO_STRING (UseXIM, "UseXIM");
12405 Lisp_Object value = display_x_get_resource (dpyinfo, useXIM, UseXIM,
12406 Qnil, Qnil);
12407 #ifdef USE_XIM
12408 if (STRINGP (value)
12409 && (!strcmp (SSDATA (value), "false")
12410 || !strcmp (SSDATA (value), "off")))
12411 use_xim = false;
12412 #else
12413 if (STRINGP (value)
12414 && (!strcmp (SSDATA (value), "true")
12415 || !strcmp (SSDATA (value), "on")))
12416 use_xim = true;
12417 #endif
12420 #ifdef HAVE_X_SM
12421 /* Only do this for the very first display in the Emacs session.
12422 Ignore X session management when Emacs was first started on a
12423 tty or started as a daemon. */
12424 if (terminal->id == 1 && ! IS_DAEMON)
12425 x_session_initialize (dpyinfo);
12426 #endif
12428 #ifdef USE_CAIRO
12429 x_extension_initialize (dpyinfo);
12430 #endif
12432 unblock_input ();
12434 return dpyinfo;
12437 /* Get rid of display DPYINFO, deleting all frames on it,
12438 and without sending any more commands to the X server. */
12440 static void
12441 x_delete_display (struct x_display_info *dpyinfo)
12443 struct terminal *t;
12444 struct color_name_cache_entry *color_entry, *next_color_entry;
12446 /* Close all frames and delete the generic struct terminal for this
12447 X display. */
12448 for (t = terminal_list; t; t = t->next_terminal)
12449 if (t->type == output_x_window && t->display_info.x == dpyinfo)
12451 #ifdef HAVE_X_SM
12452 /* Close X session management when we close its display. */
12453 if (t->id == 1 && x_session_have_connection ())
12454 x_session_close ();
12455 #endif
12456 delete_terminal (t);
12457 break;
12460 if (next_noop_dpyinfo == dpyinfo)
12461 next_noop_dpyinfo = dpyinfo->next;
12463 if (x_display_list == dpyinfo)
12464 x_display_list = dpyinfo->next;
12465 else
12467 struct x_display_info *tail;
12469 for (tail = x_display_list; tail; tail = tail->next)
12470 if (tail->next == dpyinfo)
12471 tail->next = tail->next->next;
12474 for (color_entry = dpyinfo->color_names;
12475 color_entry;
12476 color_entry = next_color_entry)
12478 next_color_entry = color_entry->next;
12479 xfree (color_entry->name);
12480 xfree (color_entry);
12483 xfree (dpyinfo->x_id_name);
12484 xfree (dpyinfo->x_dnd_atoms);
12485 xfree (dpyinfo->color_cells);
12486 xfree (dpyinfo);
12489 #ifdef USE_X_TOOLKIT
12491 /* Atimer callback function for TIMER. Called every 0.1s to process
12492 Xt timeouts, if needed. We must avoid calling XtAppPending as
12493 much as possible because that function does an implicit XFlush
12494 that slows us down. */
12496 static void
12497 x_process_timeouts (struct atimer *timer)
12499 block_input ();
12500 x_timeout_atimer_activated_flag = false;
12501 if (toolkit_scroll_bar_interaction || popup_activated ())
12503 while (XtAppPending (Xt_app_con) & XtIMTimer)
12504 XtAppProcessEvent (Xt_app_con, XtIMTimer);
12505 /* Reactivate the atimer for next time. */
12506 x_activate_timeout_atimer ();
12508 unblock_input ();
12511 /* Install an asynchronous timer that processes Xt timeout events
12512 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
12513 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
12514 function whenever these variables are set. This is necessary
12515 because some widget sets use timeouts internally, for example the
12516 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
12517 processed, these widgets don't behave normally. */
12519 void
12520 x_activate_timeout_atimer (void)
12522 block_input ();
12523 if (!x_timeout_atimer_activated_flag)
12525 struct timespec interval = make_timespec (0, 100 * 1000 * 1000);
12526 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
12527 x_timeout_atimer_activated_flag = true;
12529 unblock_input ();
12532 #endif /* USE_X_TOOLKIT */
12535 /* Set up use of X before we make the first connection. */
12537 static struct redisplay_interface x_redisplay_interface =
12539 x_frame_parm_handlers,
12540 x_produce_glyphs,
12541 x_write_glyphs,
12542 x_insert_glyphs,
12543 x_clear_end_of_line,
12544 x_scroll_run,
12545 x_after_update_window_line,
12546 x_update_window_begin,
12547 x_update_window_end,
12548 x_flip_and_flush,
12549 x_clear_window_mouse_face,
12550 x_get_glyph_overhangs,
12551 x_fix_overlapping_area,
12552 x_draw_fringe_bitmap,
12553 #ifdef USE_CAIRO
12554 x_cr_define_fringe_bitmap,
12555 x_cr_destroy_fringe_bitmap,
12556 #else
12557 0, /* define_fringe_bitmap */
12558 0, /* destroy_fringe_bitmap */
12559 #endif
12560 x_compute_glyph_string_overhangs,
12561 x_draw_glyph_string,
12562 x_define_frame_cursor,
12563 x_clear_frame_area,
12564 x_draw_window_cursor,
12565 x_draw_vertical_window_border,
12566 x_draw_window_divider,
12567 x_shift_glyphs_for_insert, /* Never called; see comment in function. */
12568 x_show_hourglass,
12569 x_hide_hourglass
12573 /* This function is called when the last frame on a display is deleted. */
12574 void
12575 x_delete_terminal (struct terminal *terminal)
12577 struct x_display_info *dpyinfo = terminal->display_info.x;
12579 /* Protect against recursive calls. delete_frame in
12580 delete_terminal calls us back when it deletes our last frame. */
12581 if (!terminal->name)
12582 return;
12584 block_input ();
12585 #ifdef HAVE_X_I18N
12586 /* We must close our connection to the XIM server before closing the
12587 X display. */
12588 if (dpyinfo->xim)
12589 xim_close_dpy (dpyinfo);
12590 #endif
12592 /* Normally, the display is available... */
12593 if (dpyinfo->display)
12595 x_destroy_all_bitmaps (dpyinfo);
12596 XSetCloseDownMode (dpyinfo->display, DestroyAll);
12598 /* Whether or not XCloseDisplay destroys the associated resource
12599 database depends on the version of libX11. To avoid both
12600 crash and memory leak, we dissociate the database from the
12601 display and then destroy dpyinfo->xrdb ourselves.
12603 Unfortunately, the above strategy does not work in some
12604 situations due to a bug in newer versions of libX11: because
12605 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
12606 dpy->db is NULL, XCloseDisplay destroys the associated
12607 database whereas it has not been created by XGetDefault
12608 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
12609 don't destroy the database here in order to avoid the crash
12610 in the above situations for now, though that may cause memory
12611 leaks in other situations. */
12612 #if false
12613 #ifdef HAVE_XRMSETDATABASE
12614 XrmSetDatabase (dpyinfo->display, NULL);
12615 #else
12616 dpyinfo->display->db = NULL;
12617 #endif
12618 /* We used to call XrmDestroyDatabase from x_delete_display, but
12619 some older versions of libX11 crash if we call it after
12620 closing all the displays. */
12621 XrmDestroyDatabase (dpyinfo->xrdb);
12622 #endif
12624 #ifdef USE_GTK
12625 xg_display_close (dpyinfo->display);
12626 #else
12627 #ifdef USE_X_TOOLKIT
12628 XtCloseDisplay (dpyinfo->display);
12629 #else
12630 XCloseDisplay (dpyinfo->display);
12631 #endif
12632 #endif /* ! USE_GTK */
12633 /* Do not close the connection here because it's already closed
12634 by X(t)CloseDisplay (Bug#18403). */
12635 dpyinfo->display = NULL;
12638 /* ...but if called from x_connection_closed, the display may already
12639 be closed and dpyinfo->display was set to 0 to indicate that. Since
12640 X server is most likely gone, explicit close is the only reliable
12641 way to continue and avoid Bug#19147. */
12642 else if (dpyinfo->connection >= 0)
12643 emacs_close (dpyinfo->connection);
12645 /* No more input on this descriptor. */
12646 delete_keyboard_wait_descriptor (dpyinfo->connection);
12647 /* Mark as dead. */
12648 dpyinfo->connection = -1;
12650 x_delete_display (dpyinfo);
12651 unblock_input ();
12654 /* Create a struct terminal, initialize it with the X11 specific
12655 functions and make DISPLAY->TERMINAL point to it. */
12657 static struct terminal *
12658 x_create_terminal (struct x_display_info *dpyinfo)
12660 struct terminal *terminal;
12662 terminal = create_terminal (output_x_window, &x_redisplay_interface);
12664 terminal->display_info.x = dpyinfo;
12665 dpyinfo->terminal = terminal;
12667 /* kboard is initialized in x_term_init. */
12669 terminal->clear_frame_hook = x_clear_frame;
12670 terminal->ins_del_lines_hook = x_ins_del_lines;
12671 terminal->delete_glyphs_hook = x_delete_glyphs;
12672 terminal->ring_bell_hook = XTring_bell;
12673 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
12674 terminal->update_begin_hook = x_update_begin;
12675 terminal->update_end_hook = x_update_end;
12676 terminal->read_socket_hook = XTread_socket;
12677 terminal->frame_up_to_date_hook = XTframe_up_to_date;
12678 terminal->buffer_flipping_unblocked_hook = XTbuffer_flipping_unblocked_hook;
12679 terminal->mouse_position_hook = XTmouse_position;
12680 terminal->frame_rehighlight_hook = XTframe_rehighlight;
12681 terminal->frame_raise_lower_hook = XTframe_raise_lower;
12682 terminal->fullscreen_hook = XTfullscreen_hook;
12683 terminal->menu_show_hook = x_menu_show;
12684 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
12685 terminal->popup_dialog_hook = xw_popup_dialog;
12686 #endif
12687 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
12688 terminal->set_horizontal_scroll_bar_hook = XTset_horizontal_scroll_bar;
12689 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
12690 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
12691 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
12692 terminal->delete_frame_hook = x_destroy_window;
12693 terminal->delete_terminal_hook = x_delete_terminal;
12694 /* Other hooks are NULL by default. */
12696 return terminal;
12699 static void
12700 x_initialize (void)
12702 baud_rate = 19200;
12704 x_noop_count = 0;
12705 any_help_event_p = false;
12706 ignore_next_mouse_click_timeout = 0;
12708 #ifdef USE_GTK
12709 current_count = -1;
12710 #endif
12712 /* Try to use interrupt input; if we can't, then start polling. */
12713 Fset_input_interrupt_mode (Qt);
12715 #if THREADS_ENABLED
12716 /* This must be called before any other Xlib routines. */
12717 if (XInitThreads () == 0)
12718 fprintf (stderr,
12719 "Warning: An error occurred initializing X11 thread support!\n");
12720 #endif
12722 #ifdef USE_X_TOOLKIT
12723 XtToolkitInitialize ();
12725 Xt_app_con = XtCreateApplicationContext ();
12727 /* Register a converter from strings to pixels, which uses
12728 Emacs' color allocation infrastructure. */
12729 XtAppSetTypeConverter (Xt_app_con,
12730 XtRString, XtRPixel, cvt_string_to_pixel,
12731 cvt_string_to_pixel_args,
12732 XtNumber (cvt_string_to_pixel_args),
12733 XtCacheByDisplay, cvt_pixel_dtor);
12735 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
12736 #endif
12738 #ifdef USE_TOOLKIT_SCROLL_BARS
12739 #ifndef USE_GTK
12740 xaw3d_arrow_scroll = False;
12741 xaw3d_pick_top = True;
12742 #endif
12743 #endif
12745 #ifdef USE_CAIRO
12746 x_cr_init_fringe (&x_redisplay_interface);
12747 #endif
12749 /* Note that there is no real way portable across R3/R4 to get the
12750 original error handler. */
12751 XSetErrorHandler (x_error_handler);
12752 XSetIOErrorHandler (x_io_error_quitter);
12755 #ifdef USE_GTK
12756 void
12757 init_xterm (void)
12759 /* Emacs can handle only core input events, so make sure
12760 Gtk doesn't use Xinput or Xinput2 extensions. */
12761 xputenv ("GDK_CORE_DEVICE_EVENTS=1");
12763 #endif
12765 void
12766 syms_of_xterm (void)
12768 x_error_message = NULL;
12770 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
12771 DEFSYM (Qlatin_1, "latin-1");
12773 #ifdef USE_GTK
12774 xg_default_icon_file = build_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
12775 staticpro (&xg_default_icon_file);
12777 DEFSYM (Qx_gtk_map_stock, "x-gtk-map-stock");
12778 #endif
12780 DEFVAR_BOOL ("x-use-underline-position-properties",
12781 x_use_underline_position_properties,
12782 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
12783 A value of nil means ignore them. If you encounter fonts with bogus
12784 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
12785 to 4.1, set this to nil. You can also use `underline-minimum-offset'
12786 to override the font's UNDERLINE_POSITION for small font display
12787 sizes. */);
12788 x_use_underline_position_properties = true;
12790 DEFVAR_BOOL ("x-underline-at-descent-line",
12791 x_underline_at_descent_line,
12792 doc: /* Non-nil means to draw the underline at the same place as the descent line.
12793 A value of nil means to draw the underline according to the value of the
12794 variable `x-use-underline-position-properties', which is usually at the
12795 baseline level. The default value is nil. */);
12796 x_underline_at_descent_line = false;
12798 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
12799 x_mouse_click_focus_ignore_position,
12800 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
12801 This variable is only used when the window manager requires that you
12802 click on a frame to select it (give it focus). In that case, a value
12803 of nil, means that the selected window and cursor position changes to
12804 reflect the mouse click position, while a non-nil value means that the
12805 selected window or cursor position is preserved. */);
12806 x_mouse_click_focus_ignore_position = false;
12808 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
12809 doc: /* Which toolkit scroll bars Emacs uses, if any.
12810 A value of nil means Emacs doesn't use toolkit scroll bars.
12811 With the X Window system, the value is a symbol describing the
12812 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
12813 With MS Windows or Nextstep, the value is t. */);
12814 #ifdef USE_TOOLKIT_SCROLL_BARS
12815 #ifdef USE_MOTIF
12816 Vx_toolkit_scroll_bars = intern_c_string ("motif");
12817 #elif defined HAVE_XAW3D
12818 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
12819 #elif USE_GTK
12820 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
12821 #else
12822 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
12823 #endif
12824 #else
12825 Vx_toolkit_scroll_bars = Qnil;
12826 #endif
12828 DEFSYM (Qmodifier_value, "modifier-value");
12829 DEFSYM (Qctrl, "ctrl");
12830 Fput (Qctrl, Qmodifier_value, make_number (ctrl_modifier));
12831 DEFSYM (Qalt, "alt");
12832 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
12833 DEFSYM (Qhyper, "hyper");
12834 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
12835 DEFSYM (Qmeta, "meta");
12836 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
12837 DEFSYM (Qsuper, "super");
12838 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
12840 DEFVAR_LISP ("x-ctrl-keysym", Vx_ctrl_keysym,
12841 doc: /* Which keys Emacs uses for the ctrl modifier.
12842 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
12843 `super'. For example, `ctrl' means use the Ctrl_L and Ctrl_R keysyms.
12844 The default is nil, which is the same as `ctrl'. */);
12845 Vx_ctrl_keysym = Qnil;
12847 DEFVAR_LISP ("x-alt-keysym", Vx_alt_keysym,
12848 doc: /* Which keys Emacs uses for the alt modifier.
12849 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
12850 `super'. For example, `alt' means use the Alt_L and Alt_R keysyms.
12851 The default is nil, which is the same as `alt'. */);
12852 Vx_alt_keysym = Qnil;
12854 DEFVAR_LISP ("x-hyper-keysym", Vx_hyper_keysym,
12855 doc: /* Which keys Emacs uses for the hyper modifier.
12856 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
12857 `super'. For example, `hyper' means use the Hyper_L and Hyper_R
12858 keysyms. The default is nil, which is the same as `hyper'. */);
12859 Vx_hyper_keysym = Qnil;
12861 DEFVAR_LISP ("x-meta-keysym", Vx_meta_keysym,
12862 doc: /* Which keys Emacs uses for the meta modifier.
12863 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
12864 `super'. For example, `meta' means use the Meta_L and Meta_R keysyms.
12865 The default is nil, which is the same as `meta'. */);
12866 Vx_meta_keysym = Qnil;
12868 DEFVAR_LISP ("x-super-keysym", Vx_super_keysym,
12869 doc: /* Which keys Emacs uses for the super modifier.
12870 This should be one of the symbols `ctrl', `alt', `hyper', `meta',
12871 `super'. For example, `super' means use the Super_L and Super_R
12872 keysyms. The default is nil, which is the same as `super'. */);
12873 Vx_super_keysym = Qnil;
12875 DEFVAR_LISP ("x-keysym-table", Vx_keysym_table,
12876 doc: /* Hash table of character codes indexed by X keysym codes. */);
12877 Vx_keysym_table = make_hash_table (hashtest_eql, make_number (900),
12878 make_float (DEFAULT_REHASH_SIZE),
12879 make_float (DEFAULT_REHASH_THRESHOLD),
12880 Qnil, Qnil);
12882 DEFVAR_BOOL ("x-frame-normalize-before-maximize",
12883 x_frame_normalize_before_maximize,
12884 doc: /* Non-nil means normalize frame before maximizing.
12885 If this variable is t, Emacs first asks the window manager to give the
12886 frame its normal size, and only then the final state, whenever changing
12887 from a full-height, full-width or full-both state to the maximized one
12888 or when changing from the maximized to the full-height or full-width
12889 state.
12891 Set this variable only if your window manager cannot handle the
12892 transition between the various maximization states. */);
12893 x_frame_normalize_before_maximize = false;