* lisp/gnus/gnus-art.el (gnus-article-send-map): Remove extra quote.
[emacs.git] / src / xterm.c
blob9fb19a16f6011c67ea04caae9e5f4300503418a2
1 /* X Communication module for terminals which understand the X protocol.
3 Copyright (C) 1989, 1993-2016 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 #ifdef USE_CAIRO
26 #include <math.h>
27 #endif
29 #include "lisp.h"
30 #include "blockinput.h"
32 /* This may include sys/types.h, and that somehow loses
33 if this is not done before the other system files. */
34 #include "xterm.h"
35 #include <X11/cursorfont.h>
37 /* If we have Xfixes extension, use it for pointer blanking. */
38 #ifdef HAVE_XFIXES
39 #include <X11/extensions/Xfixes.h>
40 #endif
42 /* Using Xft implies that XRender is available. */
43 #ifdef HAVE_XFT
44 #include <X11/extensions/Xrender.h>
45 #endif
47 /* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49 #ifndef makedev
50 #include <sys/types.h>
51 #endif /* makedev */
53 #include <sys/ioctl.h>
55 #include "systime.h"
57 #include <fcntl.h>
58 #include <errno.h>
59 #include <sys/stat.h>
60 #include "character.h"
61 #include "coding.h"
62 #include "composite.h"
63 #include "frame.h"
64 #include "dispextern.h"
65 #include "xwidget.h"
66 #include "fontset.h"
67 #include "termhooks.h"
68 #include "termopts.h"
69 #include "termchar.h"
70 #include "emacs-icon.h"
71 #include "buffer.h"
72 #include "window.h"
73 #include "keyboard.h"
74 #include "atimer.h"
75 #include "font.h"
76 #include "xsettings.h"
77 #include "sysselect.h"
78 #include "menu.h"
80 #ifdef USE_X_TOOLKIT
81 #include <X11/Shell.h>
82 #endif
84 #include <unistd.h>
86 #ifdef USE_GTK
87 #include "gtkutil.h"
88 #ifdef HAVE_GTK3
89 #include <X11/Xproto.h>
90 #endif
91 #endif
93 #if defined (USE_LUCID) || defined (USE_MOTIF)
94 #include "../lwlib/xlwmenu.h"
95 #endif
97 #ifdef USE_X_TOOLKIT
98 #if !defined (NO_EDITRES)
99 #define HACK_EDITRES
100 extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
101 #endif /* not NO_EDITRES */
103 /* Include toolkit specific headers for the scroll bar widget. */
105 #ifdef USE_TOOLKIT_SCROLL_BARS
106 #if defined USE_MOTIF
107 #include <Xm/Xm.h> /* For LESSTIF_VERSION */
108 #include <Xm/ScrollBar.h>
109 #else /* !USE_MOTIF i.e. use Xaw */
111 #ifdef HAVE_XAW3D
112 #include <X11/Xaw3d/Simple.h>
113 #include <X11/Xaw3d/Scrollbar.h>
114 #include <X11/Xaw3d/ThreeD.h>
115 #else /* !HAVE_XAW3D */
116 #include <X11/Xaw/Simple.h>
117 #include <X11/Xaw/Scrollbar.h>
118 #endif /* !HAVE_XAW3D */
119 #ifndef XtNpickTop
120 #define XtNpickTop "pickTop"
121 #endif /* !XtNpickTop */
122 #endif /* !USE_MOTIF */
123 #endif /* USE_TOOLKIT_SCROLL_BARS */
125 #endif /* USE_X_TOOLKIT */
127 #ifdef USE_X_TOOLKIT
128 #include "widget.h"
129 #ifndef XtNinitialState
130 #define XtNinitialState "initialState"
131 #endif
132 #endif
134 #include "bitmaps/gray.xbm"
136 #ifdef HAVE_XKB
137 #include <X11/XKBlib.h>
138 #endif
140 /* Default to using XIM if available. */
141 #ifdef USE_XIM
142 bool use_xim = true;
143 #else
144 bool use_xim = false; /* configure --without-xim */
145 #endif
147 /* Non-zero means that a HELP_EVENT has been generated since Emacs
148 start. */
150 static bool any_help_event_p;
152 /* This is a chain of structures for all the X displays currently in
153 use. */
155 struct x_display_info *x_display_list;
157 #ifdef USE_X_TOOLKIT
159 /* The application context for Xt use. */
160 XtAppContext Xt_app_con;
161 static String Xt_default_resources[] = {0};
163 /* Non-zero means user is interacting with a toolkit scroll bar. */
164 static bool toolkit_scroll_bar_interaction;
166 #endif /* USE_X_TOOLKIT */
168 /* Non-zero timeout value means ignore next mouse click if it arrives
169 before that timeout elapses (i.e. as part of the same sequence of
170 events resulting from clicking on a frame to select it). */
172 static Time ignore_next_mouse_click_timeout;
174 /* Used locally within XTread_socket. */
176 static int x_noop_count;
178 #ifdef USE_GTK
179 /* The name of the Emacs icon file. */
180 static Lisp_Object xg_default_icon_file;
181 #endif
183 /* Some functions take this as char *, not const char *. */
184 static char emacs_class[] = EMACS_CLASS;
186 enum xembed_info
188 XEMBED_MAPPED = 1 << 0
191 enum xembed_message
193 XEMBED_EMBEDDED_NOTIFY = 0,
194 XEMBED_WINDOW_ACTIVATE = 1,
195 XEMBED_WINDOW_DEACTIVATE = 2,
196 XEMBED_REQUEST_FOCUS = 3,
197 XEMBED_FOCUS_IN = 4,
198 XEMBED_FOCUS_OUT = 5,
199 XEMBED_FOCUS_NEXT = 6,
200 XEMBED_FOCUS_PREV = 7,
202 XEMBED_MODALITY_ON = 10,
203 XEMBED_MODALITY_OFF = 11,
204 XEMBED_REGISTER_ACCELERATOR = 12,
205 XEMBED_UNREGISTER_ACCELERATOR = 13,
206 XEMBED_ACTIVATE_ACCELERATOR = 14
209 static void x_free_cr_resources (struct frame *);
210 static bool x_alloc_nearest_color_1 (Display *, Colormap, XColor *);
211 static void x_raise_frame (struct frame *);
212 static void x_lower_frame (struct frame *);
213 static int x_io_error_quitter (Display *);
214 static struct terminal *x_create_terminal (struct x_display_info *);
215 static void x_frame_rehighlight (struct x_display_info *);
217 static void x_clip_to_row (struct window *, struct glyph_row *,
218 enum glyph_row_area, GC);
219 static struct scroll_bar *x_window_to_scroll_bar (Display *, Window, int);
220 static void x_scroll_bar_report_motion (struct frame **, Lisp_Object *,
221 enum scroll_bar_part *,
222 Lisp_Object *, Lisp_Object *,
223 Time *);
224 static void x_horizontal_scroll_bar_report_motion (struct frame **, Lisp_Object *,
225 enum scroll_bar_part *,
226 Lisp_Object *, Lisp_Object *,
227 Time *);
228 static bool x_handle_net_wm_state (struct frame *, const XPropertyEvent *);
229 static void x_check_fullscreen (struct frame *);
230 static void x_check_expected_move (struct frame *, int, int);
231 static void x_sync_with_move (struct frame *, int, int, bool);
232 static int handle_one_xevent (struct x_display_info *,
233 const XEvent *, int *,
234 struct input_event *);
235 #if ! (defined USE_X_TOOLKIT || defined USE_MOTIF)
236 static int x_dispatch_event (XEvent *, Display *);
237 #endif
238 static void x_wm_set_window_state (struct frame *, int);
239 static void x_wm_set_icon_pixmap (struct frame *, ptrdiff_t);
240 static void x_initialize (void);
242 static bool get_current_wm_state (struct frame *, Window, int *, bool *);
244 /* Flush display of frame F. */
246 static void
247 x_flush (struct frame *f)
249 eassert (f && FRAME_X_P (f));
250 /* Don't call XFlush when it is not safe to redisplay; the X
251 connection may be broken. */
252 if (!NILP (Vinhibit_redisplay))
253 return;
255 block_input ();
256 XFlush (FRAME_X_DISPLAY (f));
257 unblock_input ();
261 /* Remove calls to XFlush by defining XFlush to an empty replacement.
262 Calls to XFlush should be unnecessary because the X output buffer
263 is flushed automatically as needed by calls to XPending,
264 XNextEvent, or XWindowEvent according to the XFlush man page.
265 XTread_socket calls XPending. Removing XFlush improves
266 performance. */
268 #define XFlush(DISPLAY) (void) 0
271 /***********************************************************************
272 Debugging
273 ***********************************************************************/
275 #if false
277 /* This is a function useful for recording debugging information about
278 the sequence of occurrences in this file. */
280 struct record
282 char *locus;
283 int type;
286 struct record event_record[100];
288 int event_record_index;
290 void
291 record_event (char *locus, int type)
293 if (event_record_index == ARRAYELTS (event_record))
294 event_record_index = 0;
296 event_record[event_record_index].locus = locus;
297 event_record[event_record_index].type = type;
298 event_record_index++;
301 #endif
303 #ifdef USE_CAIRO
305 #define FRAME_CR_CONTEXT(f) ((f)->output_data.x->cr_context)
306 #define FRAME_CR_SURFACE(f) ((f)->output_data.x->cr_surface)
308 static struct x_gc_ext_data *
309 x_gc_get_ext_data (struct frame *f, GC gc, int create_if_not_found_p)
311 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
312 XEDataObject object;
313 XExtData **head, *ext_data;
315 object.gc = gc;
316 head = XEHeadOfExtensionList (object);
317 ext_data = XFindOnExtensionList (head, dpyinfo->ext_codes->extension);
318 if (ext_data == NULL)
320 if (!create_if_not_found_p)
321 return NULL;
322 else
324 ext_data = xzalloc (sizeof (*ext_data));
325 ext_data->number = dpyinfo->ext_codes->extension;
326 ext_data->private_data = xzalloc (sizeof (struct x_gc_ext_data));
327 XAddToExtensionList (head, ext_data);
330 return (struct x_gc_ext_data *) ext_data->private_data;
333 static void
334 x_extension_initialize (struct x_display_info *dpyinfo)
336 XExtCodes *ext_codes = XAddExtension (dpyinfo->display);
338 dpyinfo->ext_codes = ext_codes;
341 static void
342 x_cr_destroy_surface (struct frame *f)
344 if (FRAME_CR_SURFACE (f))
346 cairo_t *cr = FRAME_CR_CONTEXT (f);
347 cairo_surface_destroy (FRAME_CR_SURFACE (f));
348 FRAME_CR_SURFACE (f) = 0;
349 if (cr) cairo_destroy (cr);
350 FRAME_CR_CONTEXT (f) = NULL;
354 cairo_t *
355 x_begin_cr_clip (struct frame *f, GC gc)
357 cairo_t *cr = FRAME_CR_CONTEXT (f);
359 if (!cr)
362 if (! FRAME_CR_SURFACE (f))
364 cairo_surface_t *surface;
365 surface = cairo_xlib_surface_create (FRAME_X_DISPLAY (f),
366 FRAME_X_WINDOW (f),
367 FRAME_DISPLAY_INFO (f)->visual,
368 FRAME_PIXEL_WIDTH (f),
369 FRAME_PIXEL_HEIGHT (f));
370 cr = cairo_create (surface);
371 cairo_surface_destroy (surface);
373 else
374 cr = cairo_create (FRAME_CR_SURFACE (f));
375 FRAME_CR_CONTEXT (f) = cr;
377 cairo_save (cr);
379 if (gc)
381 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 0);
383 if (gc_ext && gc_ext->n_clip_rects)
385 int i;
387 for (i = 0; i < gc_ext->n_clip_rects; i++)
388 cairo_rectangle (cr, gc_ext->clip_rects[i].x,
389 gc_ext->clip_rects[i].y,
390 gc_ext->clip_rects[i].width,
391 gc_ext->clip_rects[i].height);
392 cairo_clip (cr);
396 return cr;
399 void
400 x_end_cr_clip (struct frame *f)
402 cairo_restore (FRAME_CR_CONTEXT (f));
405 void
406 x_set_cr_source_with_gc_foreground (struct frame *f, GC gc)
408 XGCValues xgcv;
409 XColor color;
411 XGetGCValues (FRAME_X_DISPLAY (f), gc, GCForeground, &xgcv);
412 color.pixel = xgcv.foreground;
413 x_query_color (f, &color);
414 cairo_set_source_rgb (FRAME_CR_CONTEXT (f), color.red / 65535.0,
415 color.green / 65535.0, color.blue / 65535.0);
418 void
419 x_set_cr_source_with_gc_background (struct frame *f, GC gc)
421 XGCValues xgcv;
422 XColor color;
424 XGetGCValues (FRAME_X_DISPLAY (f), gc, GCBackground, &xgcv);
425 color.pixel = xgcv.background;
426 x_query_color (f, &color);
427 cairo_set_source_rgb (FRAME_CR_CONTEXT (f), color.red / 65535.0,
428 color.green / 65535.0, color.blue / 65535.0);
431 /* Fringe bitmaps. */
433 static int max_fringe_bmp = 0;
434 static cairo_pattern_t **fringe_bmp = 0;
436 static void
437 x_cr_define_fringe_bitmap (int which, unsigned short *bits, int h, int wd)
439 int i, stride;
440 cairo_surface_t *surface;
441 unsigned char *data;
442 cairo_pattern_t *pattern;
444 if (which >= max_fringe_bmp)
446 i = max_fringe_bmp;
447 max_fringe_bmp = which + 20;
448 fringe_bmp = (cairo_pattern_t **) xrealloc (fringe_bmp, max_fringe_bmp * sizeof (cairo_pattern_t *));
449 while (i < max_fringe_bmp)
450 fringe_bmp[i++] = 0;
453 block_input ();
455 surface = cairo_image_surface_create (CAIRO_FORMAT_A1, wd, h);
456 stride = cairo_image_surface_get_stride (surface);
457 data = cairo_image_surface_get_data (surface);
459 for (i = 0; i < h; i++)
461 *((unsigned short *) data) = bits[i];
462 data += stride;
465 cairo_surface_mark_dirty (surface);
466 pattern = cairo_pattern_create_for_surface (surface);
467 cairo_surface_destroy (surface);
469 unblock_input ();
471 fringe_bmp[which] = pattern;
474 static void
475 x_cr_destroy_fringe_bitmap (int which)
477 if (which >= max_fringe_bmp)
478 return;
480 if (fringe_bmp[which])
482 block_input ();
483 cairo_pattern_destroy (fringe_bmp[which]);
484 unblock_input ();
486 fringe_bmp[which] = 0;
489 static void
490 x_cr_draw_image (struct frame *f, GC gc, cairo_pattern_t *image,
491 int src_x, int src_y, int width, int height,
492 int dest_x, int dest_y, bool overlay_p)
494 cairo_t *cr;
495 cairo_matrix_t matrix;
496 cairo_surface_t *surface;
497 cairo_format_t format;
499 cr = x_begin_cr_clip (f, gc);
500 if (overlay_p)
501 cairo_rectangle (cr, dest_x, dest_y, width, height);
502 else
504 x_set_cr_source_with_gc_background (f, gc);
505 cairo_rectangle (cr, dest_x, dest_y, width, height);
506 cairo_fill_preserve (cr);
508 cairo_clip (cr);
509 cairo_matrix_init_translate (&matrix, src_x - dest_x, src_y - dest_y);
510 cairo_pattern_set_matrix (image, &matrix);
511 cairo_pattern_get_surface (image, &surface);
512 format = cairo_image_surface_get_format (surface);
513 if (format != CAIRO_FORMAT_A8 && format != CAIRO_FORMAT_A1)
515 cairo_set_source (cr, image);
516 cairo_fill (cr);
518 else
520 x_set_cr_source_with_gc_foreground (f, gc);
521 cairo_mask (cr, image);
523 x_end_cr_clip (f);
526 void
527 x_cr_draw_frame (cairo_t *cr, struct frame *f)
529 int width, height;
531 width = FRAME_PIXEL_WIDTH (f);
532 height = FRAME_PIXEL_HEIGHT (f);
534 x_free_cr_resources (f);
535 FRAME_CR_CONTEXT (f) = cr;
536 x_clear_area (f, 0, 0, width, height);
537 expose_frame (f, 0, 0, width, height);
538 FRAME_CR_CONTEXT (f) = NULL;
541 static cairo_status_t
542 x_cr_accumulate_data (void *closure, const unsigned char *data,
543 unsigned int length)
545 Lisp_Object *acc = (Lisp_Object *) closure;
547 *acc = Fcons (make_unibyte_string ((char const *) data, length), *acc);
549 return CAIRO_STATUS_SUCCESS;
552 static void
553 x_cr_destroy (Lisp_Object arg)
555 cairo_t *cr = (cairo_t *) XSAVE_POINTER (arg, 0);
557 block_input ();
558 cairo_destroy (cr);
559 unblock_input ();
562 Lisp_Object
563 x_cr_export_frames (Lisp_Object frames, cairo_surface_type_t surface_type)
565 struct frame *f;
566 cairo_surface_t *surface;
567 cairo_t *cr;
568 int width, height;
569 void (*surface_set_size_func) (cairo_surface_t *, double, double) = NULL;
570 Lisp_Object acc = Qnil;
571 int count = SPECPDL_INDEX ();
573 specbind (Qredisplay_dont_pause, Qt);
574 redisplay_preserve_echo_area (31);
576 f = XFRAME (XCAR (frames));
577 frames = XCDR (frames);
578 width = FRAME_PIXEL_WIDTH (f);
579 height = FRAME_PIXEL_HEIGHT (f);
581 block_input ();
582 #ifdef CAIRO_HAS_PDF_SURFACE
583 if (surface_type == CAIRO_SURFACE_TYPE_PDF)
585 surface = cairo_pdf_surface_create_for_stream (x_cr_accumulate_data, &acc,
586 width, height);
587 surface_set_size_func = cairo_pdf_surface_set_size;
589 else
590 #endif
591 #ifdef CAIRO_HAS_PNG_FUNCTIONS
592 if (surface_type == CAIRO_SURFACE_TYPE_IMAGE)
593 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
594 else
595 #endif
596 #ifdef CAIRO_HAS_PS_SURFACE
597 if (surface_type == CAIRO_SURFACE_TYPE_PS)
599 surface = cairo_ps_surface_create_for_stream (x_cr_accumulate_data, &acc,
600 width, height);
601 surface_set_size_func = cairo_ps_surface_set_size;
603 else
604 #endif
605 #ifdef CAIRO_HAS_SVG_SURFACE
606 if (surface_type == CAIRO_SURFACE_TYPE_SVG)
607 surface = cairo_svg_surface_create_for_stream (x_cr_accumulate_data, &acc,
608 width, height);
609 else
610 #endif
611 abort ();
613 cr = cairo_create (surface);
614 cairo_surface_destroy (surface);
615 record_unwind_protect (x_cr_destroy, make_save_ptr (cr));
617 while (1)
619 x_free_cr_resources (f);
620 FRAME_CR_CONTEXT (f) = cr;
621 x_clear_area (f, 0, 0, width, height);
622 expose_frame (f, 0, 0, width, height);
623 FRAME_CR_CONTEXT (f) = NULL;
625 if (NILP (frames))
626 break;
628 cairo_surface_show_page (surface);
629 f = XFRAME (XCAR (frames));
630 frames = XCDR (frames);
631 width = FRAME_PIXEL_WIDTH (f);
632 height = FRAME_PIXEL_HEIGHT (f);
633 if (surface_set_size_func)
634 (*surface_set_size_func) (surface, width, height);
636 unblock_input ();
637 QUIT;
638 block_input ();
641 #ifdef CAIRO_HAS_PNG_FUNCTIONS
642 if (surface_type == CAIRO_SURFACE_TYPE_IMAGE)
644 cairo_surface_flush (surface);
645 cairo_surface_write_to_png_stream (surface, x_cr_accumulate_data, &acc);
647 #endif
648 unblock_input ();
650 unbind_to (count, Qnil);
652 return CALLN (Fapply, intern ("concat"), Fnreverse (acc));
655 #endif /* USE_CAIRO */
657 static void
658 x_free_cr_resources (struct frame *f)
660 #ifdef USE_CAIRO
661 if (f == NULL)
663 Lisp_Object rest, frame;
664 FOR_EACH_FRAME (rest, frame)
665 if (FRAME_X_P (XFRAME (frame)))
666 x_free_cr_resources (XFRAME (frame));
668 else
670 cairo_t *cr = FRAME_CR_CONTEXT (f);
672 if (cr)
674 cairo_surface_t *surface = cairo_get_target (cr);
676 if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_XLIB)
678 cairo_destroy (cr);
679 FRAME_CR_CONTEXT (f) = NULL;
683 #endif
686 static void
687 x_set_clip_rectangles (struct frame *f, GC gc, XRectangle *rectangles, int n)
689 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, rectangles, n, Unsorted);
690 #ifdef USE_CAIRO
691 eassert (n >= 0 && n <= MAX_CLIP_RECTS);
694 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 1);
696 gc_ext->n_clip_rects = n;
697 memcpy (gc_ext->clip_rects, rectangles, sizeof (XRectangle) * n);
699 #endif
702 static void
703 x_reset_clip_rectangles (struct frame *f, GC gc)
705 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
706 #ifdef USE_CAIRO
708 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 0);
710 if (gc_ext)
711 gc_ext->n_clip_rects = 0;
713 #endif
716 static void
717 x_fill_rectangle (struct frame *f, GC gc, int x, int y, int width, int height)
719 #ifdef USE_CAIRO
720 cairo_t *cr;
722 cr = x_begin_cr_clip (f, gc);
723 x_set_cr_source_with_gc_foreground (f, gc);
724 cairo_rectangle (cr, x, y, width, height);
725 cairo_fill (cr);
726 x_end_cr_clip (f);
727 #else
728 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
729 gc, x, y, width, height);
730 #endif
733 static void
734 x_draw_rectangle (struct frame *f, GC gc, int x, int y, int width, int height)
736 #ifdef USE_CAIRO
737 cairo_t *cr;
739 cr = x_begin_cr_clip (f, gc);
740 x_set_cr_source_with_gc_foreground (f, gc);
741 cairo_rectangle (cr, x + 0.5, y + 0.5, width, height);
742 cairo_set_line_width (cr, 1);
743 cairo_stroke (cr);
744 x_end_cr_clip (f);
745 #else
746 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
747 gc, x, y, width, height);
748 #endif
751 static void
752 x_clear_window (struct frame *f)
754 #ifdef USE_CAIRO
755 cairo_t *cr;
757 cr = x_begin_cr_clip (f, NULL);
758 x_set_cr_source_with_gc_background (f, f->output_data.x->normal_gc);
759 cairo_paint (cr);
760 x_end_cr_clip (f);
761 #else
762 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
763 #endif
766 #ifdef USE_CAIRO
767 static void
768 x_fill_trapezoid_for_relief (struct frame *f, GC gc, int x, int y,
769 int width, int height, int top_p)
771 cairo_t *cr;
773 cr = x_begin_cr_clip (f, gc);
774 x_set_cr_source_with_gc_foreground (f, gc);
775 cairo_move_to (cr, top_p ? x : x + height, y);
776 cairo_line_to (cr, x, y + height);
777 cairo_line_to (cr, top_p ? x + width - height : x + width, y + height);
778 cairo_line_to (cr, x + width, y);
779 cairo_fill (cr);
780 x_end_cr_clip (f);
783 enum corners
785 CORNER_BOTTOM_RIGHT, /* 0 -> pi/2 */
786 CORNER_BOTTOM_LEFT, /* pi/2 -> pi */
787 CORNER_TOP_LEFT, /* pi -> 3pi/2 */
788 CORNER_TOP_RIGHT, /* 3pi/2 -> 2pi */
789 CORNER_LAST
792 static void
793 x_erase_corners_for_relief (struct frame *f, GC gc, int x, int y,
794 int width, int height,
795 double radius, double margin, int corners)
797 cairo_t *cr;
798 int i;
800 cr = x_begin_cr_clip (f, gc);
801 x_set_cr_source_with_gc_background (f, gc);
802 for (i = 0; i < CORNER_LAST; i++)
803 if (corners & (1 << i))
805 double xm, ym, xc, yc;
807 if (i == CORNER_TOP_LEFT || i == CORNER_BOTTOM_LEFT)
808 xm = x - margin, xc = xm + radius;
809 else
810 xm = x + width + margin, xc = xm - radius;
811 if (i == CORNER_TOP_LEFT || i == CORNER_TOP_RIGHT)
812 ym = y - margin, yc = ym + radius;
813 else
814 ym = y + height + margin, yc = ym - radius;
816 cairo_move_to (cr, xm, ym);
817 cairo_arc (cr, xc, yc, radius, i * M_PI_2, (i + 1) * M_PI_2);
819 cairo_clip (cr);
820 cairo_rectangle (cr, x, y, width, height);
821 cairo_fill (cr);
822 x_end_cr_clip (f);
825 static void
826 x_draw_horizontal_wave (struct frame *f, GC gc, int x, int y,
827 int width, int height, int wave_length)
829 cairo_t *cr;
830 double dx = wave_length, dy = height - 1;
831 int xoffset, n;
833 cr = x_begin_cr_clip (f, gc);
834 x_set_cr_source_with_gc_foreground (f, gc);
835 cairo_rectangle (cr, x, y, width, height);
836 cairo_clip (cr);
838 if (x >= 0)
840 xoffset = x % (wave_length * 2);
841 if (xoffset == 0)
842 xoffset = wave_length * 2;
844 else
845 xoffset = x % (wave_length * 2) + wave_length * 2;
846 n = (width + xoffset) / wave_length + 1;
847 if (xoffset > wave_length)
849 xoffset -= wave_length;
850 --n;
851 y += height - 1;
852 dy = -dy;
855 cairo_move_to (cr, x - xoffset + 0.5, y + 0.5);
856 while (--n >= 0)
858 cairo_rel_line_to (cr, dx, dy);
859 dy = -dy;
861 cairo_set_line_width (cr, 1);
862 cairo_stroke (cr);
863 x_end_cr_clip (f);
865 #endif
868 /* Return the struct x_display_info corresponding to DPY. */
870 struct x_display_info *
871 x_display_info_for_display (Display *dpy)
873 struct x_display_info *dpyinfo;
875 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
876 if (dpyinfo->display == dpy)
877 return dpyinfo;
879 return 0;
882 static Window
883 x_find_topmost_parent (struct frame *f)
885 struct x_output *x = f->output_data.x;
886 Window win = None, wi = x->parent_desc;
887 Display *dpy = FRAME_X_DISPLAY (f);
889 while (wi != FRAME_DISPLAY_INFO (f)->root_window)
891 Window root;
892 Window *children;
893 unsigned int nchildren;
895 win = wi;
896 if (XQueryTree (dpy, win, &root, &wi, &children, &nchildren))
897 XFree (children);
898 else
899 break;
902 return win;
905 #define OPAQUE 0xffffffff
907 void
908 x_set_frame_alpha (struct frame *f)
910 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
911 Display *dpy = FRAME_X_DISPLAY (f);
912 Window win = FRAME_OUTER_WINDOW (f);
913 double alpha = 1.0;
914 double alpha_min = 1.0;
915 unsigned long opac;
916 Window parent;
918 if (dpyinfo->x_highlight_frame == f)
919 alpha = f->alpha[0];
920 else
921 alpha = f->alpha[1];
923 if (FLOATP (Vframe_alpha_lower_limit))
924 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
925 else if (INTEGERP (Vframe_alpha_lower_limit))
926 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
928 if (alpha < 0.0)
929 return;
930 else if (alpha > 1.0)
931 alpha = 1.0;
932 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
933 alpha = alpha_min;
935 opac = alpha * OPAQUE;
937 x_catch_errors (dpy);
939 /* If there is a parent from the window manager, put the property there
940 also, to work around broken window managers that fail to do that.
941 Do this unconditionally as this function is called on reparent when
942 alpha has not changed on the frame. */
944 parent = x_find_topmost_parent (f);
945 if (parent != None)
946 XChangeProperty (dpy, parent, dpyinfo->Xatom_net_wm_window_opacity,
947 XA_CARDINAL, 32, PropModeReplace,
948 (unsigned char *) &opac, 1);
950 /* return unless necessary */
952 unsigned char *data;
953 Atom actual;
954 int rc, format;
955 unsigned long n, left;
957 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
958 0, 1, False, XA_CARDINAL,
959 &actual, &format, &n, &left,
960 &data);
962 if (rc == Success && actual != None)
964 unsigned long value = *(unsigned long *)data;
965 XFree (data);
966 if (value == opac)
968 x_uncatch_errors ();
969 return;
974 XChangeProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
975 XA_CARDINAL, 32, PropModeReplace,
976 (unsigned char *) &opac, 1);
977 x_uncatch_errors ();
980 /***********************************************************************
981 Starting and ending an update
982 ***********************************************************************/
984 /* Start an update of frame F. This function is installed as a hook
985 for update_begin, i.e. it is called when update_begin is called.
986 This function is called prior to calls to x_update_window_begin for
987 each window being updated. Currently, there is nothing to do here
988 because all interesting stuff is done on a window basis. */
990 static void
991 x_update_begin (struct frame *f)
993 #ifdef USE_CAIRO
994 if (! NILP (tip_frame) && XFRAME (tip_frame) == f
995 && ! FRAME_VISIBLE_P (f))
996 return;
998 if (! FRAME_CR_SURFACE (f))
1000 int width, height;
1001 #ifdef USE_GTK
1002 if (FRAME_GTK_WIDGET (f))
1004 GdkWindow *w = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
1005 width = gdk_window_get_width (w);
1006 height = gdk_window_get_height (w);
1008 else
1009 #endif
1011 width = FRAME_PIXEL_WIDTH (f);
1012 height = FRAME_PIXEL_HEIGHT (f);
1013 if (! FRAME_EXTERNAL_TOOL_BAR (f))
1014 height += FRAME_TOOL_BAR_HEIGHT (f);
1015 if (! FRAME_EXTERNAL_MENU_BAR (f))
1016 height += FRAME_MENU_BAR_HEIGHT (f);
1019 if (width > 0 && height > 0)
1021 block_input();
1022 FRAME_CR_SURFACE (f) = cairo_image_surface_create
1023 (CAIRO_FORMAT_ARGB32, width, height);
1024 unblock_input();
1027 #endif /* USE_CAIRO */
1030 /* Start update of window W. */
1032 static void
1033 x_update_window_begin (struct window *w)
1035 struct frame *f = XFRAME (WINDOW_FRAME (w));
1036 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
1038 w->output_cursor = w->cursor;
1040 block_input ();
1042 if (f == hlinfo->mouse_face_mouse_frame)
1044 /* Don't do highlighting for mouse motion during the update. */
1045 hlinfo->mouse_face_defer = true;
1047 /* If F needs to be redrawn, simply forget about any prior mouse
1048 highlighting. */
1049 if (FRAME_GARBAGED_P (f))
1050 hlinfo->mouse_face_window = Qnil;
1053 unblock_input ();
1057 /* Draw a vertical window border from (x,y0) to (x,y1) */
1059 static void
1060 x_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
1062 struct frame *f = XFRAME (WINDOW_FRAME (w));
1063 struct face *face;
1065 face = FACE_OPT_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
1066 if (face)
1067 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
1068 face->foreground);
1070 #ifdef USE_CAIRO
1071 x_fill_rectangle (f, f->output_data.x->normal_gc, x, y0, 1, y1 - y0);
1072 #else
1073 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1074 f->output_data.x->normal_gc, x, y0, x, y1);
1075 #endif
1078 /* Draw a window divider from (x0,y0) to (x1,y1) */
1080 static void
1081 x_draw_window_divider (struct window *w, int x0, int x1, int y0, int y1)
1083 struct frame *f = XFRAME (WINDOW_FRAME (w));
1084 struct face *face = FACE_OPT_FROM_ID (f, WINDOW_DIVIDER_FACE_ID);
1085 struct face *face_first
1086 = FACE_OPT_FROM_ID (f, WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
1087 struct face *face_last
1088 = FACE_OPT_FROM_ID (f, WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
1089 unsigned long color = face ? face->foreground : FRAME_FOREGROUND_PIXEL (f);
1090 unsigned long color_first = (face_first
1091 ? face_first->foreground
1092 : FRAME_FOREGROUND_PIXEL (f));
1093 unsigned long color_last = (face_last
1094 ? face_last->foreground
1095 : FRAME_FOREGROUND_PIXEL (f));
1096 Display *display = FRAME_X_DISPLAY (f);
1098 if (y1 - y0 > x1 - x0 && x1 - x0 > 2)
1099 /* Vertical. */
1101 XSetForeground (display, f->output_data.x->normal_gc, color_first);
1102 x_fill_rectangle (f, f->output_data.x->normal_gc,
1103 x0, y0, 1, y1 - y0);
1104 XSetForeground (display, f->output_data.x->normal_gc, color);
1105 x_fill_rectangle (f, f->output_data.x->normal_gc,
1106 x0 + 1, y0, x1 - x0 - 2, y1 - y0);
1107 XSetForeground (display, f->output_data.x->normal_gc, color_last);
1108 x_fill_rectangle (f, f->output_data.x->normal_gc,
1109 x1 - 1, y0, 1, y1 - y0);
1111 else if (x1 - x0 > y1 - y0 && y1 - y0 > 3)
1112 /* Horizontal. */
1114 XSetForeground (display, f->output_data.x->normal_gc, color_first);
1115 x_fill_rectangle (f, f->output_data.x->normal_gc,
1116 x0, y0, x1 - x0, 1);
1117 XSetForeground (display, f->output_data.x->normal_gc, color);
1118 x_fill_rectangle (f, f->output_data.x->normal_gc,
1119 x0, y0 + 1, x1 - x0, y1 - y0 - 2);
1120 XSetForeground (display, f->output_data.x->normal_gc, color_last);
1121 x_fill_rectangle (f, f->output_data.x->normal_gc,
1122 x0, y1 - 1, x1 - x0, 1);
1124 else
1126 XSetForeground (display, f->output_data.x->normal_gc, color);
1127 x_fill_rectangle (f, f->output_data.x->normal_gc,
1128 x0, y0, x1 - x0, y1 - y0);
1132 /* End update of window W.
1134 Draw vertical borders between horizontally adjacent windows, and
1135 display W's cursor if CURSOR_ON_P is non-zero.
1137 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
1138 glyphs in mouse-face were overwritten. In that case we have to
1139 make sure that the mouse-highlight is properly redrawn.
1141 W may be a menu bar pseudo-window in case we don't have X toolkit
1142 support. Such windows don't have a cursor, so don't display it
1143 here. */
1145 static void
1146 x_update_window_end (struct window *w, bool cursor_on_p,
1147 bool mouse_face_overwritten_p)
1149 if (!w->pseudo_window_p)
1151 block_input ();
1153 if (cursor_on_p)
1154 display_and_set_cursor (w, true,
1155 w->output_cursor.hpos, w->output_cursor.vpos,
1156 w->output_cursor.x, w->output_cursor.y);
1158 if (draw_window_fringes (w, true))
1160 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
1161 x_draw_right_divider (w);
1162 else
1163 x_draw_vertical_border (w);
1166 unblock_input ();
1169 /* If a row with mouse-face was overwritten, arrange for
1170 XTframe_up_to_date to redisplay the mouse highlight. */
1171 if (mouse_face_overwritten_p)
1173 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
1175 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
1176 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
1177 hlinfo->mouse_face_window = Qnil;
1182 /* End update of frame F. This function is installed as a hook in
1183 update_end. */
1185 static void
1186 x_update_end (struct frame *f)
1188 /* Mouse highlight may be displayed again. */
1189 MOUSE_HL_INFO (f)->mouse_face_defer = false;
1191 #ifdef USE_CAIRO
1192 if (FRAME_CR_SURFACE (f))
1194 cairo_t *cr = 0;
1195 block_input();
1196 #if defined (USE_GTK) && defined (HAVE_GTK3)
1197 if (FRAME_GTK_WIDGET (f))
1199 GdkWindow *w = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
1200 cr = gdk_cairo_create (w);
1202 else
1203 #endif
1205 cairo_surface_t *surface;
1206 int width = FRAME_PIXEL_WIDTH (f);
1207 int height = FRAME_PIXEL_HEIGHT (f);
1208 if (! FRAME_EXTERNAL_TOOL_BAR (f))
1209 height += FRAME_TOOL_BAR_HEIGHT (f);
1210 if (! FRAME_EXTERNAL_MENU_BAR (f))
1211 height += FRAME_MENU_BAR_HEIGHT (f);
1212 surface = cairo_xlib_surface_create (FRAME_X_DISPLAY (f),
1213 FRAME_X_WINDOW (f),
1214 FRAME_DISPLAY_INFO (f)->visual,
1215 width,
1216 height);
1217 cr = cairo_create (surface);
1218 cairo_surface_destroy (surface);
1221 cairo_set_source_surface (cr, FRAME_CR_SURFACE (f), 0, 0);
1222 cairo_paint (cr);
1223 cairo_destroy (cr);
1224 unblock_input ();
1226 #endif /* USE_CAIRO */
1228 #ifndef XFlush
1229 block_input ();
1230 XFlush (FRAME_X_DISPLAY (f));
1231 unblock_input ();
1232 #endif
1236 /* This function is called from various places in xdisp.c
1237 whenever a complete update has been performed. */
1239 static void
1240 XTframe_up_to_date (struct frame *f)
1242 if (FRAME_X_P (f))
1243 FRAME_MOUSE_UPDATE (f);
1247 /* Clear under internal border if any (GTK has its own version). */
1248 #ifndef USE_GTK
1249 void
1250 x_clear_under_internal_border (struct frame *f)
1252 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
1254 int border = FRAME_INTERNAL_BORDER_WIDTH (f);
1255 int width = FRAME_PIXEL_WIDTH (f);
1256 int height = FRAME_PIXEL_HEIGHT (f);
1257 int margin = FRAME_TOP_MARGIN_HEIGHT (f);
1259 block_input ();
1260 x_clear_area (f, 0, 0, border, height);
1261 x_clear_area (f, 0, margin, width, border);
1262 x_clear_area (f, width - border, 0, border, height);
1263 x_clear_area (f, 0, height - border, width, border);
1264 unblock_input ();
1267 #endif
1269 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
1270 arrow bitmaps, or clear the fringes if no bitmaps are required
1271 before DESIRED_ROW is made current. This function is called from
1272 update_window_line only if it is known that there are differences
1273 between bitmaps to be drawn between current row and DESIRED_ROW. */
1275 static void
1276 x_after_update_window_line (struct window *w, struct glyph_row *desired_row)
1278 eassert (w);
1280 if (!desired_row->mode_line_p && !w->pseudo_window_p)
1281 desired_row->redraw_fringe_bitmaps_p = true;
1283 #ifdef USE_X_TOOLKIT
1284 /* When a window has disappeared, make sure that no rest of
1285 full-width rows stays visible in the internal border. Could
1286 check here if updated window is the leftmost/rightmost window,
1287 but I guess it's not worth doing since vertically split windows
1288 are almost never used, internal border is rarely set, and the
1289 overhead is very small. */
1291 struct frame *f;
1292 int width, height;
1294 if (windows_or_buffers_changed
1295 && desired_row->full_width_p
1296 && (f = XFRAME (w->frame),
1297 width = FRAME_INTERNAL_BORDER_WIDTH (f),
1298 width != 0)
1299 && (height = desired_row->visible_height,
1300 height > 0))
1302 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
1304 block_input ();
1305 x_clear_area (f, 0, y, width, height);
1306 x_clear_area (f, FRAME_PIXEL_WIDTH (f) - width, y, width, height);
1307 unblock_input ();
1310 #endif
1313 static void
1314 x_draw_fringe_bitmap (struct window *w, struct glyph_row *row, struct draw_fringe_bitmap_params *p)
1316 struct frame *f = XFRAME (WINDOW_FRAME (w));
1317 Display *display = FRAME_X_DISPLAY (f);
1318 GC gc = f->output_data.x->normal_gc;
1319 struct face *face = p->face;
1321 /* Must clip because of partially visible lines. */
1322 x_clip_to_row (w, row, ANY_AREA, gc);
1324 if (p->bx >= 0 && !p->overlay_p)
1326 /* In case the same realized face is used for fringes and
1327 for something displayed in the text (e.g. face `region' on
1328 mono-displays, the fill style may have been changed to
1329 FillSolid in x_draw_glyph_string_background. */
1330 if (face->stipple)
1331 XSetFillStyle (display, face->gc, FillOpaqueStippled);
1332 else
1333 XSetForeground (display, face->gc, face->background);
1335 x_fill_rectangle (f, face->gc, p->bx, p->by, p->nx, p->ny);
1337 if (!face->stipple)
1338 XSetForeground (display, face->gc, face->foreground);
1341 #ifdef USE_CAIRO
1342 if (p->which && p->which < max_fringe_bmp)
1344 XGCValues gcv;
1346 XGetGCValues (display, gc, GCForeground | GCBackground, &gcv);
1347 XSetForeground (display, gc, (p->cursor_p
1348 ? (p->overlay_p ? face->background
1349 : f->output_data.x->cursor_pixel)
1350 : face->foreground));
1351 XSetBackground (display, gc, face->background);
1352 x_cr_draw_image (f, gc, fringe_bmp[p->which], 0, p->dh,
1353 p->wd, p->h, p->x, p->y, p->overlay_p);
1354 XSetForeground (display, gc, gcv.foreground);
1355 XSetBackground (display, gc, gcv.background);
1357 #else /* not USE_CAIRO */
1358 if (p->which)
1360 Window window = FRAME_X_WINDOW (f);
1361 char *bits;
1362 Pixmap pixmap, clipmask = (Pixmap) 0;
1363 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
1364 XGCValues gcv;
1366 if (p->wd > 8)
1367 bits = (char *) (p->bits + p->dh);
1368 else
1369 bits = (char *) p->bits + p->dh;
1371 /* Draw the bitmap. I believe these small pixmaps can be cached
1372 by the server. */
1373 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
1374 (p->cursor_p
1375 ? (p->overlay_p ? face->background
1376 : f->output_data.x->cursor_pixel)
1377 : face->foreground),
1378 face->background, depth);
1380 if (p->overlay_p)
1382 clipmask = XCreatePixmapFromBitmapData (display,
1383 FRAME_DISPLAY_INFO (f)->root_window,
1384 bits, p->wd, p->h,
1385 1, 0, 1);
1386 gcv.clip_mask = clipmask;
1387 gcv.clip_x_origin = p->x;
1388 gcv.clip_y_origin = p->y;
1389 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
1392 XCopyArea (display, pixmap, window, gc, 0, 0,
1393 p->wd, p->h, p->x, p->y);
1394 XFreePixmap (display, pixmap);
1396 if (p->overlay_p)
1398 gcv.clip_mask = (Pixmap) 0;
1399 XChangeGC (display, gc, GCClipMask, &gcv);
1400 XFreePixmap (display, clipmask);
1403 #endif /* not USE_CAIRO */
1405 x_reset_clip_rectangles (f, gc);
1408 /***********************************************************************
1409 Glyph display
1410 ***********************************************************************/
1414 static void x_set_glyph_string_clipping (struct glyph_string *);
1415 static void x_set_glyph_string_gc (struct glyph_string *);
1416 static void x_draw_glyph_string_foreground (struct glyph_string *);
1417 static void x_draw_composite_glyph_string_foreground (struct glyph_string *);
1418 static void x_draw_glyph_string_box (struct glyph_string *);
1419 static void x_draw_glyph_string (struct glyph_string *);
1420 static _Noreturn void x_delete_glyphs (struct frame *, int);
1421 static void x_compute_glyph_string_overhangs (struct glyph_string *);
1422 static void x_set_cursor_gc (struct glyph_string *);
1423 static void x_set_mode_line_face_gc (struct glyph_string *);
1424 static void x_set_mouse_face_gc (struct glyph_string *);
1425 static bool x_alloc_lighter_color (struct frame *, Display *, Colormap,
1426 unsigned long *, double, int);
1427 static void x_setup_relief_color (struct frame *, struct relief *,
1428 double, int, unsigned long);
1429 static void x_setup_relief_colors (struct glyph_string *);
1430 static void x_draw_image_glyph_string (struct glyph_string *);
1431 static void x_draw_image_relief (struct glyph_string *);
1432 static void x_draw_image_foreground (struct glyph_string *);
1433 static void x_draw_image_foreground_1 (struct glyph_string *, Pixmap);
1434 static void x_clear_glyph_string_rect (struct glyph_string *, int,
1435 int, int, int);
1436 static void x_draw_relief_rect (struct frame *, int, int, int, int,
1437 int, bool, bool, bool, bool, bool,
1438 XRectangle *);
1439 static void x_draw_box_rect (struct glyph_string *, int, int, int, int,
1440 int, bool, bool, XRectangle *);
1441 static void x_scroll_bar_clear (struct frame *);
1443 #ifdef GLYPH_DEBUG
1444 static void x_check_font (struct frame *, struct font *);
1445 #endif
1448 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
1449 face. */
1451 static void
1452 x_set_cursor_gc (struct glyph_string *s)
1454 if (s->font == FRAME_FONT (s->f)
1455 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
1456 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
1457 && !s->cmp)
1458 s->gc = s->f->output_data.x->cursor_gc;
1459 else
1461 /* Cursor on non-default face: must merge. */
1462 XGCValues xgcv;
1463 unsigned long mask;
1465 xgcv.background = s->f->output_data.x->cursor_pixel;
1466 xgcv.foreground = s->face->background;
1468 /* If the glyph would be invisible, try a different foreground. */
1469 if (xgcv.foreground == xgcv.background)
1470 xgcv.foreground = s->face->foreground;
1471 if (xgcv.foreground == xgcv.background)
1472 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1473 if (xgcv.foreground == xgcv.background)
1474 xgcv.foreground = s->face->foreground;
1476 /* Make sure the cursor is distinct from text in this face. */
1477 if (xgcv.background == s->face->background
1478 && xgcv.foreground == s->face->foreground)
1480 xgcv.background = s->face->foreground;
1481 xgcv.foreground = s->face->background;
1484 IF_DEBUG (x_check_font (s->f, s->font));
1485 xgcv.graphics_exposures = False;
1486 mask = GCForeground | GCBackground | GCGraphicsExposures;
1488 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1489 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1490 mask, &xgcv);
1491 else
1492 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1493 = XCreateGC (s->display, s->window, mask, &xgcv);
1495 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1500 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1502 static void
1503 x_set_mouse_face_gc (struct glyph_string *s)
1505 int face_id;
1506 struct face *face;
1508 /* What face has to be used last for the mouse face? */
1509 face_id = MOUSE_HL_INFO (s->f)->mouse_face_face_id;
1510 face = FACE_OPT_FROM_ID (s->f, face_id);
1511 if (face == NULL)
1512 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1514 if (s->first_glyph->type == CHAR_GLYPH)
1515 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1516 else
1517 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1518 s->face = FACE_FROM_ID (s->f, face_id);
1519 prepare_face_for_display (s->f, s->face);
1521 if (s->font == s->face->font)
1522 s->gc = s->face->gc;
1523 else
1525 /* Otherwise construct scratch_cursor_gc with values from FACE
1526 except for FONT. */
1527 XGCValues xgcv;
1528 unsigned long mask;
1530 xgcv.background = s->face->background;
1531 xgcv.foreground = s->face->foreground;
1532 xgcv.graphics_exposures = False;
1533 mask = GCForeground | GCBackground | GCGraphicsExposures;
1535 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1536 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1537 mask, &xgcv);
1538 else
1539 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1540 = XCreateGC (s->display, s->window, mask, &xgcv);
1542 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1545 eassert (s->gc != 0);
1549 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1550 Faces to use in the mode line have already been computed when the
1551 matrix was built, so there isn't much to do, here. */
1553 static void
1554 x_set_mode_line_face_gc (struct glyph_string *s)
1556 s->gc = s->face->gc;
1560 /* Set S->gc of glyph string S for drawing that glyph string. Set
1561 S->stippled_p to a non-zero value if the face of S has a stipple
1562 pattern. */
1564 static void
1565 x_set_glyph_string_gc (struct glyph_string *s)
1567 prepare_face_for_display (s->f, s->face);
1569 if (s->hl == DRAW_NORMAL_TEXT)
1571 s->gc = s->face->gc;
1572 s->stippled_p = s->face->stipple != 0;
1574 else if (s->hl == DRAW_INVERSE_VIDEO)
1576 x_set_mode_line_face_gc (s);
1577 s->stippled_p = s->face->stipple != 0;
1579 else if (s->hl == DRAW_CURSOR)
1581 x_set_cursor_gc (s);
1582 s->stippled_p = false;
1584 else if (s->hl == DRAW_MOUSE_FACE)
1586 x_set_mouse_face_gc (s);
1587 s->stippled_p = s->face->stipple != 0;
1589 else if (s->hl == DRAW_IMAGE_RAISED
1590 || s->hl == DRAW_IMAGE_SUNKEN)
1592 s->gc = s->face->gc;
1593 s->stippled_p = s->face->stipple != 0;
1595 else
1596 emacs_abort ();
1598 /* GC must have been set. */
1599 eassert (s->gc != 0);
1603 /* Set clipping for output of glyph string S. S may be part of a mode
1604 line or menu if we don't have X toolkit support. */
1606 static void
1607 x_set_glyph_string_clipping (struct glyph_string *s)
1609 XRectangle *r = s->clip;
1610 int n = get_glyph_string_clip_rects (s, r, 2);
1612 if (n > 0)
1613 x_set_clip_rectangles (s->f, s->gc, r, n);
1614 s->num_clips = n;
1618 /* Set SRC's clipping for output of glyph string DST. This is called
1619 when we are drawing DST's left_overhang or right_overhang only in
1620 the area of SRC. */
1622 static void
1623 x_set_glyph_string_clipping_exactly (struct glyph_string *src, struct glyph_string *dst)
1625 XRectangle r;
1627 r.x = src->x;
1628 r.width = src->width;
1629 r.y = src->y;
1630 r.height = src->height;
1631 dst->clip[0] = r;
1632 dst->num_clips = 1;
1633 x_set_clip_rectangles (dst->f, dst->gc, &r, 1);
1637 /* RIF:
1638 Compute left and right overhang of glyph string S. */
1640 static void
1641 x_compute_glyph_string_overhangs (struct glyph_string *s)
1643 if (s->cmp == NULL
1644 && (s->first_glyph->type == CHAR_GLYPH
1645 || s->first_glyph->type == COMPOSITE_GLYPH))
1647 struct font_metrics metrics;
1649 if (s->first_glyph->type == CHAR_GLYPH)
1651 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1652 struct font *font = s->font;
1653 int i;
1655 for (i = 0; i < s->nchars; i++)
1656 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1657 font->driver->text_extents (font, code, s->nchars, &metrics);
1659 else
1661 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1663 composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
1665 s->right_overhang = (metrics.rbearing > metrics.width
1666 ? metrics.rbearing - metrics.width : 0);
1667 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1669 else if (s->cmp)
1671 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1672 s->left_overhang = - s->cmp->lbearing;
1677 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1679 static void
1680 x_clear_glyph_string_rect (struct glyph_string *s, int x, int y, int w, int h)
1682 XGCValues xgcv;
1683 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1684 XSetForeground (s->display, s->gc, xgcv.background);
1685 x_fill_rectangle (s->f, s->gc, x, y, w, h);
1686 XSetForeground (s->display, s->gc, xgcv.foreground);
1690 /* Draw the background of glyph_string S. If S->background_filled_p
1691 is non-zero don't draw it. FORCE_P non-zero means draw the
1692 background even if it wouldn't be drawn normally. This is used
1693 when a string preceding S draws into the background of S, or S
1694 contains the first component of a composition. */
1696 static void
1697 x_draw_glyph_string_background (struct glyph_string *s, bool force_p)
1699 /* Nothing to do if background has already been drawn or if it
1700 shouldn't be drawn in the first place. */
1701 if (!s->background_filled_p)
1703 int box_line_width = max (s->face->box_line_width, 0);
1705 if (s->stippled_p)
1707 /* Fill background with a stipple pattern. */
1708 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1709 x_fill_rectangle (s->f, s->gc, s->x,
1710 s->y + box_line_width,
1711 s->background_width,
1712 s->height - 2 * box_line_width);
1713 XSetFillStyle (s->display, s->gc, FillSolid);
1714 s->background_filled_p = true;
1716 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1717 /* When xdisp.c ignores FONT_HEIGHT, we cannot trust
1718 font dimensions, since the actual glyphs might be
1719 much smaller. So in that case we always clear the
1720 rectangle with background color. */
1721 || FONT_TOO_HIGH (s->font)
1722 || s->font_not_found_p
1723 || s->extends_to_end_of_line_p
1724 || force_p)
1726 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1727 s->background_width,
1728 s->height - 2 * box_line_width);
1729 s->background_filled_p = true;
1735 /* Draw the foreground of glyph string S. */
1737 static void
1738 x_draw_glyph_string_foreground (struct glyph_string *s)
1740 int i, x;
1742 /* If first glyph of S has a left box line, start drawing the text
1743 of S to the right of that box line. */
1744 if (s->face->box != FACE_NO_BOX
1745 && s->first_glyph->left_box_line_p)
1746 x = s->x + eabs (s->face->box_line_width);
1747 else
1748 x = s->x;
1750 /* Draw characters of S as rectangles if S's font could not be
1751 loaded. */
1752 if (s->font_not_found_p)
1754 for (i = 0; i < s->nchars; ++i)
1756 struct glyph *g = s->first_glyph + i;
1757 x_draw_rectangle (s->f,
1758 s->gc, x, s->y, g->pixel_width - 1,
1759 s->height - 1);
1760 x += g->pixel_width;
1763 else
1765 struct font *font = s->font;
1766 int boff = font->baseline_offset;
1767 int y;
1769 if (font->vertical_centering)
1770 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1772 y = s->ybase - boff;
1773 if (s->for_overlaps
1774 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1775 font->driver->draw (s, 0, s->nchars, x, y, false);
1776 else
1777 font->driver->draw (s, 0, s->nchars, x, y, true);
1778 if (s->face->overstrike)
1779 font->driver->draw (s, 0, s->nchars, x + 1, y, false);
1783 /* Draw the foreground of composite glyph string S. */
1785 static void
1786 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1788 int i, j, x;
1789 struct font *font = s->font;
1791 /* If first glyph of S has a left box line, start drawing the text
1792 of S to the right of that box line. */
1793 if (s->face && s->face->box != FACE_NO_BOX
1794 && s->first_glyph->left_box_line_p)
1795 x = s->x + eabs (s->face->box_line_width);
1796 else
1797 x = s->x;
1799 /* S is a glyph string for a composition. S->cmp_from is the index
1800 of the first character drawn for glyphs of this composition.
1801 S->cmp_from == 0 means we are drawing the very first character of
1802 this composition. */
1804 /* Draw a rectangle for the composition if the font for the very
1805 first character of the composition could not be loaded. */
1806 if (s->font_not_found_p)
1808 if (s->cmp_from == 0)
1809 x_draw_rectangle (s->f, s->gc, x, s->y,
1810 s->width - 1, s->height - 1);
1812 else if (! s->first_glyph->u.cmp.automatic)
1814 int y = s->ybase;
1816 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1817 /* TAB in a composition means display glyphs with padding
1818 space on the left or right. */
1819 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1821 int xx = x + s->cmp->offsets[j * 2];
1822 int yy = y - s->cmp->offsets[j * 2 + 1];
1824 font->driver->draw (s, j, j + 1, xx, yy, false);
1825 if (s->face->overstrike)
1826 font->driver->draw (s, j, j + 1, xx + 1, yy, false);
1829 else
1831 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1832 Lisp_Object glyph;
1833 int y = s->ybase;
1834 int width = 0;
1836 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1838 glyph = LGSTRING_GLYPH (gstring, i);
1839 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1840 width += LGLYPH_WIDTH (glyph);
1841 else
1843 int xoff, yoff, wadjust;
1845 if (j < i)
1847 font->driver->draw (s, j, i, x, y, false);
1848 if (s->face->overstrike)
1849 font->driver->draw (s, j, i, x + 1, y, false);
1850 x += width;
1852 xoff = LGLYPH_XOFF (glyph);
1853 yoff = LGLYPH_YOFF (glyph);
1854 wadjust = LGLYPH_WADJUST (glyph);
1855 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, false);
1856 if (s->face->overstrike)
1857 font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff,
1858 false);
1859 x += wadjust;
1860 j = i + 1;
1861 width = 0;
1864 if (j < i)
1866 font->driver->draw (s, j, i, x, y, false);
1867 if (s->face->overstrike)
1868 font->driver->draw (s, j, i, x + 1, y, false);
1874 /* Draw the foreground of glyph string S for glyphless characters. */
1876 static void
1877 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1879 struct glyph *glyph = s->first_glyph;
1880 XChar2b char2b[8];
1881 int x, i, j;
1883 /* If first glyph of S has a left box line, start drawing the text
1884 of S to the right of that box line. */
1885 if (s->face && s->face->box != FACE_NO_BOX
1886 && s->first_glyph->left_box_line_p)
1887 x = s->x + eabs (s->face->box_line_width);
1888 else
1889 x = s->x;
1891 s->char2b = char2b;
1893 for (i = 0; i < s->nchars; i++, glyph++)
1895 char buf[7], *str = NULL;
1896 int len = glyph->u.glyphless.len;
1898 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1900 if (len > 0
1901 && CHAR_TABLE_P (Vglyphless_char_display)
1902 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1903 >= 1))
1905 Lisp_Object acronym
1906 = (! glyph->u.glyphless.for_no_font
1907 ? CHAR_TABLE_REF (Vglyphless_char_display,
1908 glyph->u.glyphless.ch)
1909 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
1910 if (STRINGP (acronym))
1911 str = SSDATA (acronym);
1914 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
1916 sprintf (buf, "%0*X",
1917 glyph->u.glyphless.ch < 0x10000 ? 4 : 6,
1918 glyph->u.glyphless.ch + 0u);
1919 str = buf;
1922 if (str)
1924 int upper_len = (len + 1) / 2;
1925 unsigned code;
1927 /* It is assured that all LEN characters in STR is ASCII. */
1928 for (j = 0; j < len; j++)
1930 code = s->font->driver->encode_char (s->font, str[j]);
1931 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
1933 s->font->driver->draw (s, 0, upper_len,
1934 x + glyph->slice.glyphless.upper_xoff,
1935 s->ybase + glyph->slice.glyphless.upper_yoff,
1936 false);
1937 s->font->driver->draw (s, upper_len, len,
1938 x + glyph->slice.glyphless.lower_xoff,
1939 s->ybase + glyph->slice.glyphless.lower_yoff,
1940 false);
1942 if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
1943 x_draw_rectangle (s->f, s->gc,
1944 x, s->ybase - glyph->ascent,
1945 glyph->pixel_width - 1,
1946 glyph->ascent + glyph->descent - 1);
1947 x += glyph->pixel_width;
1951 #ifdef USE_X_TOOLKIT
1953 #ifdef USE_LUCID
1955 /* Return the frame on which widget WIDGET is used.. Abort if frame
1956 cannot be determined. */
1958 static struct frame *
1959 x_frame_of_widget (Widget widget)
1961 struct x_display_info *dpyinfo;
1962 Lisp_Object tail, frame;
1963 struct frame *f;
1965 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1967 /* Find the top-level shell of the widget. Note that this function
1968 can be called when the widget is not yet realized, so XtWindow
1969 (widget) == 0. That's the reason we can't simply use
1970 x_any_window_to_frame. */
1971 while (!XtIsTopLevelShell (widget))
1972 widget = XtParent (widget);
1974 /* Look for a frame with that top-level widget. Allocate the color
1975 on that frame to get the right gamma correction value. */
1976 FOR_EACH_FRAME (tail, frame)
1978 f = XFRAME (frame);
1979 if (FRAME_X_P (f)
1980 && f->output_data.nothing != 1
1981 && FRAME_DISPLAY_INFO (f) == dpyinfo
1982 && f->output_data.x->widget == widget)
1983 return f;
1985 emacs_abort ();
1988 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1989 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1990 If this produces the same color as PIXEL, try a color where all RGB
1991 values have DELTA added. Return the allocated color in *PIXEL.
1992 DISPLAY is the X display, CMAP is the colormap to operate on.
1993 Value is true if successful. */
1995 bool
1996 x_alloc_lighter_color_for_widget (Widget widget, Display *display, Colormap cmap,
1997 unsigned long *pixel, double factor, int delta)
1999 struct frame *f = x_frame_of_widget (widget);
2000 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
2003 #endif /* USE_LUCID */
2006 /* Structure specifying which arguments should be passed by Xt to
2007 cvt_string_to_pixel. We want the widget's screen and colormap. */
2009 static XtConvertArgRec cvt_string_to_pixel_args[] =
2011 {XtWidgetBaseOffset, (XtPointer) offsetof (WidgetRec, core.screen),
2012 sizeof (Screen *)},
2013 {XtWidgetBaseOffset, (XtPointer) offsetof (WidgetRec, core.colormap),
2014 sizeof (Colormap)}
2018 /* The address of this variable is returned by
2019 cvt_string_to_pixel. */
2021 static Pixel cvt_string_to_pixel_value;
2024 /* Convert a color name to a pixel color.
2026 DPY is the display we are working on.
2028 ARGS is an array of *NARGS XrmValue structures holding additional
2029 information about the widget for which the conversion takes place.
2030 The contents of this array are determined by the specification
2031 in cvt_string_to_pixel_args.
2033 FROM is a pointer to an XrmValue which points to the color name to
2034 convert. TO is an XrmValue in which to return the pixel color.
2036 CLOSURE_RET is a pointer to user-data, in which we record if
2037 we allocated the color or not.
2039 Value is True if successful, False otherwise. */
2041 static Boolean
2042 cvt_string_to_pixel (Display *dpy, XrmValue *args, Cardinal *nargs,
2043 XrmValue *from, XrmValue *to,
2044 XtPointer *closure_ret)
2046 Screen *screen;
2047 Colormap cmap;
2048 Pixel pixel;
2049 String color_name;
2050 XColor color;
2052 if (*nargs != 2)
2054 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
2055 "wrongParameters", "cvt_string_to_pixel",
2056 "XtToolkitError",
2057 "Screen and colormap args required", NULL, NULL);
2058 return False;
2061 screen = *(Screen **) args[0].addr;
2062 cmap = *(Colormap *) args[1].addr;
2063 color_name = (String) from->addr;
2065 if (strcmp (color_name, XtDefaultBackground) == 0)
2067 *closure_ret = (XtPointer) False;
2068 pixel = WhitePixelOfScreen (screen);
2070 else if (strcmp (color_name, XtDefaultForeground) == 0)
2072 *closure_ret = (XtPointer) False;
2073 pixel = BlackPixelOfScreen (screen);
2075 else if (XParseColor (dpy, cmap, color_name, &color)
2076 && x_alloc_nearest_color_1 (dpy, cmap, &color))
2078 pixel = color.pixel;
2079 *closure_ret = (XtPointer) True;
2081 else
2083 String params[1];
2084 Cardinal nparams = 1;
2086 params[0] = color_name;
2087 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
2088 "badValue", "cvt_string_to_pixel",
2089 "XtToolkitError", "Invalid color '%s'",
2090 params, &nparams);
2091 return False;
2094 if (to->addr != NULL)
2096 if (to->size < sizeof (Pixel))
2098 to->size = sizeof (Pixel);
2099 return False;
2102 *(Pixel *) to->addr = pixel;
2104 else
2106 cvt_string_to_pixel_value = pixel;
2107 to->addr = (XtPointer) &cvt_string_to_pixel_value;
2110 to->size = sizeof (Pixel);
2111 return True;
2115 /* Free a pixel color which was previously allocated via
2116 cvt_string_to_pixel. This is registered as the destructor
2117 for this type of resource via XtSetTypeConverter.
2119 APP is the application context in which we work.
2121 TO is a pointer to an XrmValue holding the color to free.
2122 CLOSURE is the value we stored in CLOSURE_RET for this color
2123 in cvt_string_to_pixel.
2125 ARGS and NARGS are like for cvt_string_to_pixel. */
2127 static void
2128 cvt_pixel_dtor (XtAppContext app, XrmValuePtr to, XtPointer closure, XrmValuePtr args,
2129 Cardinal *nargs)
2131 if (*nargs != 2)
2133 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
2134 "XtToolkitError",
2135 "Screen and colormap arguments required",
2136 NULL, NULL);
2138 else if (closure != NULL)
2140 /* We did allocate the pixel, so free it. */
2141 Screen *screen = *(Screen **) args[0].addr;
2142 Colormap cmap = *(Colormap *) args[1].addr;
2143 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
2144 (Pixel *) to->addr, 1);
2149 #endif /* USE_X_TOOLKIT */
2152 /* Value is an array of XColor structures for the contents of the
2153 color map of display DPY. Set *NCELLS to the size of the array.
2154 Note that this probably shouldn't be called for large color maps,
2155 say a 24-bit TrueColor map. */
2157 static const XColor *
2158 x_color_cells (Display *dpy, int *ncells)
2160 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
2161 eassume (dpyinfo);
2163 if (dpyinfo->color_cells == NULL)
2165 Screen *screen = dpyinfo->screen;
2166 int ncolor_cells = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
2167 int i;
2169 dpyinfo->color_cells = xnmalloc (ncolor_cells,
2170 sizeof *dpyinfo->color_cells);
2171 dpyinfo->ncolor_cells = ncolor_cells;
2173 for (i = 0; i < ncolor_cells; ++i)
2174 dpyinfo->color_cells[i].pixel = i;
2176 XQueryColors (dpy, dpyinfo->cmap,
2177 dpyinfo->color_cells, ncolor_cells);
2180 *ncells = dpyinfo->ncolor_cells;
2181 return dpyinfo->color_cells;
2185 /* On frame F, translate pixel colors to RGB values for the NCOLORS
2186 colors in COLORS. Use cached information, if available. */
2188 void
2189 x_query_colors (struct frame *f, XColor *colors, int ncolors)
2191 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2193 if (dpyinfo->red_bits > 0)
2195 /* For TrueColor displays, we can decompose the RGB value
2196 directly. */
2197 int i;
2198 unsigned int rmult, gmult, bmult;
2199 unsigned int rmask, gmask, bmask;
2201 rmask = (1 << dpyinfo->red_bits) - 1;
2202 gmask = (1 << dpyinfo->green_bits) - 1;
2203 bmask = (1 << dpyinfo->blue_bits) - 1;
2204 /* If we're widening, for example, 8 bits in the pixel value to
2205 16 bits for the separate-color representation, we want to
2206 extrapolate the lower bits based on those bits available --
2207 in other words, we'd like 0xff to become 0xffff instead of
2208 the 0xff00 we'd get by just zero-filling the lower bits.
2210 We generate a 32-bit scaled-up value and shift it, in case
2211 the bit count doesn't divide 16 evenly (e.g., when dealing
2212 with a 3-3-2 bit RGB display), to get more of the lower bits
2213 correct.
2215 Should we cache the multipliers in dpyinfo? Maybe
2216 special-case the 8-8-8 common case? */
2217 rmult = 0xffffffff / rmask;
2218 gmult = 0xffffffff / gmask;
2219 bmult = 0xffffffff / bmask;
2221 for (i = 0; i < ncolors; ++i)
2223 unsigned int r, g, b;
2224 unsigned long pixel = colors[i].pixel;
2226 r = (pixel >> dpyinfo->red_offset) & rmask;
2227 g = (pixel >> dpyinfo->green_offset) & gmask;
2228 b = (pixel >> dpyinfo->blue_offset) & bmask;
2230 colors[i].red = (r * rmult) >> 16;
2231 colors[i].green = (g * gmult) >> 16;
2232 colors[i].blue = (b * bmult) >> 16;
2234 return;
2237 if (dpyinfo->color_cells)
2239 int i;
2240 for (i = 0; i < ncolors; ++i)
2242 unsigned long pixel = colors[i].pixel;
2243 eassert (pixel < dpyinfo->ncolor_cells);
2244 eassert (dpyinfo->color_cells[pixel].pixel == pixel);
2245 colors[i] = dpyinfo->color_cells[pixel];
2247 return;
2250 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
2254 /* On frame F, translate pixel color to RGB values for the color in
2255 COLOR. Use cached information, if available. */
2257 void
2258 x_query_color (struct frame *f, XColor *color)
2260 x_query_colors (f, color, 1);
2264 /* On frame F, translate the color name to RGB values. Use cached
2265 information, if possible.
2267 Note that there is currently no way to clean old entries out of the
2268 cache. However, it is limited to names in the server's database,
2269 and names we've actually looked up; list-colors-display is probably
2270 the most color-intensive case we're likely to hit. */
2272 Status x_parse_color (struct frame *f, const char *color_name,
2273 XColor *color)
2275 Display *dpy = FRAME_X_DISPLAY (f);
2276 Colormap cmap = FRAME_X_COLORMAP (f);
2277 struct color_name_cache_entry *cache_entry;
2279 if (color_name[0] == '#')
2281 /* The hex form is parsed directly by XParseColor without
2282 talking to the X server. No need for caching. */
2283 return XParseColor (dpy, cmap, color_name, color);
2286 for (cache_entry = FRAME_DISPLAY_INFO (f)->color_names; cache_entry;
2287 cache_entry = cache_entry->next)
2289 if (!xstrcasecmp(cache_entry->name, color_name))
2291 *color = cache_entry->rgb;
2292 return 1;
2296 if (XParseColor (dpy, cmap, color_name, color) == 0)
2297 /* No caching of negative results, currently. */
2298 return 0;
2300 cache_entry = xzalloc (sizeof *cache_entry);
2301 cache_entry->rgb = *color;
2302 cache_entry->name = xstrdup (color_name);
2303 cache_entry->next = FRAME_DISPLAY_INFO (f)->color_names;
2304 FRAME_DISPLAY_INFO (f)->color_names = cache_entry;
2305 return 1;
2309 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
2310 exact match can't be allocated, try the nearest color available.
2311 Value is true if successful. Set *COLOR to the color
2312 allocated. */
2314 static bool
2315 x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color)
2317 bool rc;
2319 rc = XAllocColor (dpy, cmap, color) != 0;
2320 if (rc == 0)
2322 /* If we got to this point, the colormap is full, so we're going
2323 to try to get the next closest color. The algorithm used is
2324 a least-squares matching, which is what X uses for closest
2325 color matching with StaticColor visuals. */
2326 int nearest, i;
2327 int max_color_delta = 255;
2328 int max_delta = 3 * max_color_delta;
2329 int nearest_delta = max_delta + 1;
2330 int ncells;
2331 const XColor *cells = x_color_cells (dpy, &ncells);
2333 for (nearest = i = 0; i < ncells; ++i)
2335 int dred = (color->red >> 8) - (cells[i].red >> 8);
2336 int dgreen = (color->green >> 8) - (cells[i].green >> 8);
2337 int dblue = (color->blue >> 8) - (cells[i].blue >> 8);
2338 int delta = dred * dred + dgreen * dgreen + dblue * dblue;
2340 if (delta < nearest_delta)
2342 nearest = i;
2343 nearest_delta = delta;
2347 color->red = cells[nearest].red;
2348 color->green = cells[nearest].green;
2349 color->blue = cells[nearest].blue;
2350 rc = XAllocColor (dpy, cmap, color) != 0;
2352 else
2354 /* If allocation succeeded, and the allocated pixel color is not
2355 equal to a cached pixel color recorded earlier, there was a
2356 change in the colormap, so clear the color cache. */
2357 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
2358 eassume (dpyinfo);
2360 if (dpyinfo->color_cells)
2362 XColor *cached_color = &dpyinfo->color_cells[color->pixel];
2363 if (cached_color->red != color->red
2364 || cached_color->blue != color->blue
2365 || cached_color->green != color->green)
2367 xfree (dpyinfo->color_cells);
2368 dpyinfo->color_cells = NULL;
2369 dpyinfo->ncolor_cells = 0;
2374 #ifdef DEBUG_X_COLORS
2375 if (rc)
2376 register_color (color->pixel);
2377 #endif /* DEBUG_X_COLORS */
2379 return rc;
2383 /* Allocate the color COLOR->pixel on frame F, colormap CMAP, after
2384 gamma correction. If an exact match can't be allocated, try the
2385 nearest color available. Value is true if successful. Set *COLOR
2386 to the color allocated. */
2388 bool
2389 x_alloc_nearest_color (struct frame *f, Colormap cmap, XColor *color)
2391 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2393 gamma_correct (f, color);
2395 if (dpyinfo->red_bits > 0)
2397 color->pixel = x_make_truecolor_pixel (dpyinfo,
2398 color->red,
2399 color->green,
2400 color->blue);
2401 return true;
2404 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
2408 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
2409 It's necessary to do this instead of just using PIXEL directly to
2410 get color reference counts right. */
2412 unsigned long
2413 x_copy_color (struct frame *f, unsigned long pixel)
2415 XColor color;
2417 /* If display has an immutable color map, freeing colors is not
2418 necessary and some servers don't allow it. Since we won't free a
2419 color once we've allocated it, we don't need to re-allocate it to
2420 maintain the server's reference count. */
2421 if (!x_mutable_colormap (FRAME_X_VISUAL (f)))
2422 return pixel;
2424 color.pixel = pixel;
2425 block_input ();
2426 /* The color could still be found in the color_cells array. */
2427 x_query_color (f, &color);
2428 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
2429 unblock_input ();
2430 #ifdef DEBUG_X_COLORS
2431 register_color (pixel);
2432 #endif
2433 return color.pixel;
2437 /* Brightness beyond which a color won't have its highlight brightness
2438 boosted.
2440 Nominally, highlight colors for `3d' faces are calculated by
2441 brightening an object's color by a constant scale factor, but this
2442 doesn't yield good results for dark colors, so for colors who's
2443 brightness is less than this value (on a scale of 0-65535) have an
2444 use an additional additive factor.
2446 The value here is set so that the default menu-bar/mode-line color
2447 (grey75) will not have its highlights changed at all. */
2448 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
2451 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
2452 or DELTA. Try a color with RGB values multiplied by FACTOR first.
2453 If this produces the same color as PIXEL, try a color where all RGB
2454 values have DELTA added. Return the allocated color in *PIXEL.
2455 DISPLAY is the X display, CMAP is the colormap to operate on.
2456 Value is non-zero if successful. */
2458 static bool
2459 x_alloc_lighter_color (struct frame *f, Display *display, Colormap cmap,
2460 unsigned long *pixel, double factor, int delta)
2462 XColor color, new;
2463 long bright;
2464 bool success_p;
2466 /* Get RGB color values. */
2467 color.pixel = *pixel;
2468 x_query_color (f, &color);
2470 /* Change RGB values by specified FACTOR. Avoid overflow! */
2471 eassert (factor >= 0);
2472 new.red = min (0xffff, factor * color.red);
2473 new.green = min (0xffff, factor * color.green);
2474 new.blue = min (0xffff, factor * color.blue);
2476 /* Calculate brightness of COLOR. */
2477 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
2479 /* We only boost colors that are darker than
2480 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
2481 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
2482 /* Make an additive adjustment to NEW, because it's dark enough so
2483 that scaling by FACTOR alone isn't enough. */
2485 /* How far below the limit this color is (0 - 1, 1 being darker). */
2486 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
2487 /* The additive adjustment. */
2488 int min_delta = delta * dimness * factor / 2;
2490 if (factor < 1)
2492 new.red = max (0, new.red - min_delta);
2493 new.green = max (0, new.green - min_delta);
2494 new.blue = max (0, new.blue - min_delta);
2496 else
2498 new.red = min (0xffff, min_delta + new.red);
2499 new.green = min (0xffff, min_delta + new.green);
2500 new.blue = min (0xffff, min_delta + new.blue);
2504 /* Try to allocate the color. */
2505 success_p = x_alloc_nearest_color (f, cmap, &new);
2506 if (success_p)
2508 if (new.pixel == *pixel)
2510 /* If we end up with the same color as before, try adding
2511 delta to the RGB values. */
2512 x_free_colors (f, &new.pixel, 1);
2514 new.red = min (0xffff, delta + color.red);
2515 new.green = min (0xffff, delta + color.green);
2516 new.blue = min (0xffff, delta + color.blue);
2517 success_p = x_alloc_nearest_color (f, cmap, &new);
2519 else
2520 success_p = true;
2521 *pixel = new.pixel;
2524 return success_p;
2528 /* Set up the foreground color for drawing relief lines of glyph
2529 string S. RELIEF is a pointer to a struct relief containing the GC
2530 with which lines will be drawn. Use a color that is FACTOR or
2531 DELTA lighter or darker than the relief's background which is found
2532 in S->f->output_data.x->relief_background. If such a color cannot
2533 be allocated, use DEFAULT_PIXEL, instead. */
2535 static void
2536 x_setup_relief_color (struct frame *f, struct relief *relief, double factor,
2537 int delta, unsigned long default_pixel)
2539 XGCValues xgcv;
2540 struct x_output *di = f->output_data.x;
2541 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
2542 unsigned long pixel;
2543 unsigned long background = di->relief_background;
2544 Colormap cmap = FRAME_X_COLORMAP (f);
2545 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2546 Display *dpy = FRAME_X_DISPLAY (f);
2548 xgcv.graphics_exposures = False;
2549 xgcv.line_width = 1;
2551 /* Free previously allocated color. The color cell will be reused
2552 when it has been freed as many times as it was allocated, so this
2553 doesn't affect faces using the same colors. */
2554 if (relief->gc && relief->pixel != -1)
2556 x_free_colors (f, &relief->pixel, 1);
2557 relief->pixel = -1;
2560 /* Allocate new color. */
2561 xgcv.foreground = default_pixel;
2562 pixel = background;
2563 if (dpyinfo->n_planes != 1
2564 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
2565 xgcv.foreground = relief->pixel = pixel;
2567 if (relief->gc == 0)
2569 xgcv.stipple = dpyinfo->gray;
2570 mask |= GCStipple;
2571 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
2573 else
2574 XChangeGC (dpy, relief->gc, mask, &xgcv);
2578 /* Set up colors for the relief lines around glyph string S. */
2580 static void
2581 x_setup_relief_colors (struct glyph_string *s)
2583 struct x_output *di = s->f->output_data.x;
2584 unsigned long color;
2586 if (s->face->use_box_color_for_shadows_p)
2587 color = s->face->box_color;
2588 else if (s->first_glyph->type == IMAGE_GLYPH
2589 && s->img->pixmap
2590 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2591 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2592 else
2594 XGCValues xgcv;
2596 /* Get the background color of the face. */
2597 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2598 color = xgcv.background;
2601 if (di->white_relief.gc == 0
2602 || color != di->relief_background)
2604 di->relief_background = color;
2605 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2606 WHITE_PIX_DEFAULT (s->f));
2607 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2608 BLACK_PIX_DEFAULT (s->f));
2613 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2614 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2615 to draw, it must be >= 0. RAISED_P means draw a raised
2616 relief. LEFT_P means draw a relief on the left side of
2617 the rectangle. RIGHT_P means draw a relief on the right
2618 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2619 when drawing. */
2621 static void
2622 x_draw_relief_rect (struct frame *f,
2623 int left_x, int top_y, int right_x, int bottom_y,
2624 int width, bool raised_p, bool top_p, bool bot_p,
2625 bool left_p, bool right_p,
2626 XRectangle *clip_rect)
2628 #ifdef USE_CAIRO
2629 GC top_left_gc, bottom_right_gc;
2630 int corners = 0;
2632 if (raised_p)
2634 top_left_gc = f->output_data.x->white_relief.gc;
2635 bottom_right_gc = f->output_data.x->black_relief.gc;
2637 else
2639 top_left_gc = f->output_data.x->black_relief.gc;
2640 bottom_right_gc = f->output_data.x->white_relief.gc;
2643 x_set_clip_rectangles (f, top_left_gc, clip_rect, 1);
2644 x_set_clip_rectangles (f, bottom_right_gc, clip_rect, 1);
2646 if (left_p)
2648 x_fill_rectangle (f, top_left_gc, left_x, top_y,
2649 width, bottom_y + 1 - top_y);
2650 if (top_p)
2651 corners |= 1 << CORNER_TOP_LEFT;
2652 if (bot_p)
2653 corners |= 1 << CORNER_BOTTOM_LEFT;
2655 if (right_p)
2657 x_fill_rectangle (f, bottom_right_gc, right_x + 1 - width, top_y,
2658 width, bottom_y + 1 - top_y);
2659 if (top_p)
2660 corners |= 1 << CORNER_TOP_RIGHT;
2661 if (bot_p)
2662 corners |= 1 << CORNER_BOTTOM_RIGHT;
2664 if (top_p)
2666 if (!right_p)
2667 x_fill_rectangle (f, top_left_gc, left_x, top_y,
2668 right_x + 1 - left_x, width);
2669 else
2670 x_fill_trapezoid_for_relief (f, top_left_gc, left_x, top_y,
2671 right_x + 1 - left_x, width, 1);
2673 if (bot_p)
2675 if (!left_p)
2676 x_fill_rectangle (f, bottom_right_gc, left_x, bottom_y + 1 - width,
2677 right_x + 1 - left_x, width);
2678 else
2679 x_fill_trapezoid_for_relief (f, bottom_right_gc,
2680 left_x, bottom_y + 1 - width,
2681 right_x + 1 - left_x, width, 0);
2683 if (left_p && width != 1)
2684 x_fill_rectangle (f, bottom_right_gc, left_x, top_y,
2685 1, bottom_y + 1 - top_y);
2686 if (top_p && width != 1)
2687 x_fill_rectangle (f, bottom_right_gc, left_x, top_y,
2688 right_x + 1 - left_x, 1);
2689 if (corners)
2691 XSetBackground (FRAME_X_DISPLAY (f), top_left_gc,
2692 FRAME_BACKGROUND_PIXEL (f));
2693 x_erase_corners_for_relief (f, top_left_gc, left_x, top_y,
2694 right_x - left_x + 1, bottom_y - top_y + 1,
2695 6, 1, corners);
2698 x_reset_clip_rectangles (f, top_left_gc);
2699 x_reset_clip_rectangles (f, bottom_right_gc);
2700 #else
2701 Display *dpy = FRAME_X_DISPLAY (f);
2702 Window window = FRAME_X_WINDOW (f);
2703 int i;
2704 GC gc;
2706 if (raised_p)
2707 gc = f->output_data.x->white_relief.gc;
2708 else
2709 gc = f->output_data.x->black_relief.gc;
2710 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2712 /* This code is more complicated than it has to be, because of two
2713 minor hacks to make the boxes look nicer: (i) if width > 1, draw
2714 the outermost line using the black relief. (ii) Omit the four
2715 corner pixels. */
2717 /* Top. */
2718 if (top_p)
2720 if (width == 1)
2721 XDrawLine (dpy, window, gc,
2722 left_x + left_p, top_y,
2723 right_x + !right_p, top_y);
2725 for (i = 1; i < width; ++i)
2726 XDrawLine (dpy, window, gc,
2727 left_x + i * left_p, top_y + i,
2728 right_x + 1 - i * right_p, top_y + i);
2731 /* Left. */
2732 if (left_p)
2734 if (width == 1)
2735 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
2737 XClearArea (dpy, window, left_x, top_y, 1, 1, False);
2738 XClearArea (dpy, window, left_x, bottom_y, 1, 1, False);
2740 for (i = (width > 1 ? 1 : 0); i < width; ++i)
2741 XDrawLine (dpy, window, gc,
2742 left_x + i, top_y + (i + 1) * top_p,
2743 left_x + i, bottom_y + 1 - (i + 1) * bot_p);
2746 XSetClipMask (dpy, gc, None);
2747 if (raised_p)
2748 gc = f->output_data.x->black_relief.gc;
2749 else
2750 gc = f->output_data.x->white_relief.gc;
2751 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2753 if (width > 1)
2755 /* Outermost top line. */
2756 if (top_p)
2757 XDrawLine (dpy, window, gc,
2758 left_x + left_p, top_y,
2759 right_x + !right_p, top_y);
2761 /* Outermost left line. */
2762 if (left_p)
2763 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
2766 /* Bottom. */
2767 if (bot_p)
2769 XDrawLine (dpy, window, gc,
2770 left_x + left_p, bottom_y,
2771 right_x + !right_p, bottom_y);
2772 for (i = 1; i < width; ++i)
2773 XDrawLine (dpy, window, gc,
2774 left_x + i * left_p, bottom_y - i,
2775 right_x + 1 - i * right_p, bottom_y - i);
2778 /* Right. */
2779 if (right_p)
2781 XClearArea (dpy, window, right_x, top_y, 1, 1, False);
2782 XClearArea (dpy, window, right_x, bottom_y, 1, 1, False);
2783 for (i = 0; i < width; ++i)
2784 XDrawLine (dpy, window, gc,
2785 right_x - i, top_y + (i + 1) * top_p,
2786 right_x - i, bottom_y + 1 - (i + 1) * bot_p);
2789 x_reset_clip_rectangles (f, gc);
2791 #endif
2795 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2796 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2797 draw, it must be >= 0. LEFT_P means draw a line on the
2798 left side of the rectangle. RIGHT_P means draw a line
2799 on the right side of the rectangle. CLIP_RECT is the clipping
2800 rectangle to use when drawing. */
2802 static void
2803 x_draw_box_rect (struct glyph_string *s,
2804 int left_x, int top_y, int right_x, int bottom_y, int width,
2805 bool left_p, bool right_p, XRectangle *clip_rect)
2807 XGCValues xgcv;
2809 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2810 XSetForeground (s->display, s->gc, s->face->box_color);
2811 x_set_clip_rectangles (s->f, s->gc, clip_rect, 1);
2813 /* Top. */
2814 x_fill_rectangle (s->f, s->gc,
2815 left_x, top_y, right_x - left_x + 1, width);
2817 /* Left. */
2818 if (left_p)
2819 x_fill_rectangle (s->f, s->gc,
2820 left_x, top_y, width, bottom_y - top_y + 1);
2822 /* Bottom. */
2823 x_fill_rectangle (s->f, s->gc,
2824 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2826 /* Right. */
2827 if (right_p)
2828 x_fill_rectangle (s->f, s->gc,
2829 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2831 XSetForeground (s->display, s->gc, xgcv.foreground);
2832 x_reset_clip_rectangles (s->f, s->gc);
2836 /* Draw a box around glyph string S. */
2838 static void
2839 x_draw_glyph_string_box (struct glyph_string *s)
2841 int width, left_x, right_x, top_y, bottom_y, last_x;
2842 bool raised_p, left_p, right_p;
2843 struct glyph *last_glyph;
2844 XRectangle clip_rect;
2846 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2847 ? WINDOW_RIGHT_EDGE_X (s->w)
2848 : window_box_right (s->w, s->area));
2850 /* The glyph that may have a right box line. */
2851 last_glyph = (s->cmp || s->img
2852 ? s->first_glyph
2853 : s->first_glyph + s->nchars - 1);
2855 width = eabs (s->face->box_line_width);
2856 raised_p = s->face->box == FACE_RAISED_BOX;
2857 left_x = s->x;
2858 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2859 ? last_x - 1
2860 : min (last_x, s->x + s->background_width) - 1);
2861 top_y = s->y;
2862 bottom_y = top_y + s->height - 1;
2864 left_p = (s->first_glyph->left_box_line_p
2865 || (s->hl == DRAW_MOUSE_FACE
2866 && (s->prev == NULL
2867 || s->prev->hl != s->hl)));
2868 right_p = (last_glyph->right_box_line_p
2869 || (s->hl == DRAW_MOUSE_FACE
2870 && (s->next == NULL
2871 || s->next->hl != s->hl)));
2873 get_glyph_string_clip_rect (s, &clip_rect);
2875 if (s->face->box == FACE_SIMPLE_BOX)
2876 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2877 left_p, right_p, &clip_rect);
2878 else
2880 x_setup_relief_colors (s);
2881 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2882 width, raised_p, true, true, left_p, right_p,
2883 &clip_rect);
2888 /* Draw foreground of image glyph string S. */
2890 static void
2891 x_draw_image_foreground (struct glyph_string *s)
2893 int x = s->x;
2894 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2896 /* If first glyph of S has a left box line, start drawing it to the
2897 right of that line. */
2898 if (s->face->box != FACE_NO_BOX
2899 && s->first_glyph->left_box_line_p
2900 && s->slice.x == 0)
2901 x += eabs (s->face->box_line_width);
2903 /* If there is a margin around the image, adjust x- and y-position
2904 by that margin. */
2905 if (s->slice.x == 0)
2906 x += s->img->hmargin;
2907 if (s->slice.y == 0)
2908 y += s->img->vmargin;
2910 if (s->img->pixmap)
2912 if (s->img->mask)
2914 /* We can't set both a clip mask and use XSetClipRectangles
2915 because the latter also sets a clip mask. We also can't
2916 trust on the shape extension to be available
2917 (XShapeCombineRegion). So, compute the rectangle to draw
2918 manually. */
2919 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2920 | GCFunction);
2921 XGCValues xgcv;
2922 XRectangle clip_rect, image_rect, r;
2924 xgcv.clip_mask = s->img->mask;
2925 xgcv.clip_x_origin = x;
2926 xgcv.clip_y_origin = y;
2927 xgcv.function = GXcopy;
2928 XChangeGC (s->display, s->gc, mask, &xgcv);
2930 get_glyph_string_clip_rect (s, &clip_rect);
2931 image_rect.x = x;
2932 image_rect.y = y;
2933 image_rect.width = s->slice.width;
2934 image_rect.height = s->slice.height;
2935 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2936 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2937 s->slice.x + r.x - x, s->slice.y + r.y - y,
2938 r.width, r.height, r.x, r.y);
2940 else
2942 XRectangle clip_rect, image_rect, r;
2944 get_glyph_string_clip_rect (s, &clip_rect);
2945 image_rect.x = x;
2946 image_rect.y = y;
2947 image_rect.width = s->slice.width;
2948 image_rect.height = s->slice.height;
2949 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2950 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2951 s->slice.x + r.x - x, s->slice.y + r.y - y,
2952 r.width, r.height, r.x, r.y);
2954 /* When the image has a mask, we can expect that at
2955 least part of a mouse highlight or a block cursor will
2956 be visible. If the image doesn't have a mask, make
2957 a block cursor visible by drawing a rectangle around
2958 the image. I believe it's looking better if we do
2959 nothing here for mouse-face. */
2960 if (s->hl == DRAW_CURSOR)
2962 int relief = eabs (s->img->relief);
2963 x_draw_rectangle (s->f, s->gc,
2964 x - relief, y - relief,
2965 s->slice.width + relief*2 - 1,
2966 s->slice.height + relief*2 - 1);
2970 else
2971 /* Draw a rectangle if image could not be loaded. */
2972 x_draw_rectangle (s->f, s->gc, x, y,
2973 s->slice.width - 1, s->slice.height - 1);
2977 /* Draw a relief around the image glyph string S. */
2979 static void
2980 x_draw_image_relief (struct glyph_string *s)
2982 int x1, y1, thick;
2983 bool raised_p, top_p, bot_p, left_p, right_p;
2984 int extra_x, extra_y;
2985 XRectangle r;
2986 int x = s->x;
2987 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2989 /* If first glyph of S has a left box line, start drawing it to the
2990 right of that line. */
2991 if (s->face->box != FACE_NO_BOX
2992 && s->first_glyph->left_box_line_p
2993 && s->slice.x == 0)
2994 x += eabs (s->face->box_line_width);
2996 /* If there is a margin around the image, adjust x- and y-position
2997 by that margin. */
2998 if (s->slice.x == 0)
2999 x += s->img->hmargin;
3000 if (s->slice.y == 0)
3001 y += s->img->vmargin;
3003 if (s->hl == DRAW_IMAGE_SUNKEN
3004 || s->hl == DRAW_IMAGE_RAISED)
3006 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
3007 raised_p = s->hl == DRAW_IMAGE_RAISED;
3009 else
3011 thick = eabs (s->img->relief);
3012 raised_p = s->img->relief > 0;
3015 x1 = x + s->slice.width - 1;
3016 y1 = y + s->slice.height - 1;
3018 extra_x = extra_y = 0;
3019 if (s->face->id == TOOL_BAR_FACE_ID)
3021 if (CONSP (Vtool_bar_button_margin)
3022 && INTEGERP (XCAR (Vtool_bar_button_margin))
3023 && INTEGERP (XCDR (Vtool_bar_button_margin)))
3025 extra_x = XINT (XCAR (Vtool_bar_button_margin));
3026 extra_y = XINT (XCDR (Vtool_bar_button_margin));
3028 else if (INTEGERP (Vtool_bar_button_margin))
3029 extra_x = extra_y = XINT (Vtool_bar_button_margin);
3032 top_p = bot_p = left_p = right_p = false;
3034 if (s->slice.x == 0)
3035 x -= thick + extra_x, left_p = true;
3036 if (s->slice.y == 0)
3037 y -= thick + extra_y, top_p = true;
3038 if (s->slice.x + s->slice.width == s->img->width)
3039 x1 += thick + extra_x, right_p = true;
3040 if (s->slice.y + s->slice.height == s->img->height)
3041 y1 += thick + extra_y, bot_p = true;
3043 x_setup_relief_colors (s);
3044 get_glyph_string_clip_rect (s, &r);
3045 x_draw_relief_rect (s->f, x, y, x1, y1, thick, raised_p,
3046 top_p, bot_p, left_p, right_p, &r);
3050 /* Draw the foreground of image glyph string S to PIXMAP. */
3052 static void
3053 x_draw_image_foreground_1 (struct glyph_string *s, Pixmap pixmap)
3055 int x = 0;
3056 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
3058 /* If first glyph of S has a left box line, start drawing it to the
3059 right of that line. */
3060 if (s->face->box != FACE_NO_BOX
3061 && s->first_glyph->left_box_line_p
3062 && s->slice.x == 0)
3063 x += eabs (s->face->box_line_width);
3065 /* If there is a margin around the image, adjust x- and y-position
3066 by that margin. */
3067 if (s->slice.x == 0)
3068 x += s->img->hmargin;
3069 if (s->slice.y == 0)
3070 y += s->img->vmargin;
3072 if (s->img->pixmap)
3074 if (s->img->mask)
3076 /* We can't set both a clip mask and use XSetClipRectangles
3077 because the latter also sets a clip mask. We also can't
3078 trust on the shape extension to be available
3079 (XShapeCombineRegion). So, compute the rectangle to draw
3080 manually. */
3081 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3082 | GCFunction);
3083 XGCValues xgcv;
3085 xgcv.clip_mask = s->img->mask;
3086 xgcv.clip_x_origin = x - s->slice.x;
3087 xgcv.clip_y_origin = y - s->slice.y;
3088 xgcv.function = GXcopy;
3089 XChangeGC (s->display, s->gc, mask, &xgcv);
3091 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3092 s->slice.x, s->slice.y,
3093 s->slice.width, s->slice.height, x, y);
3094 XSetClipMask (s->display, s->gc, None);
3096 else
3098 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3099 s->slice.x, s->slice.y,
3100 s->slice.width, s->slice.height, x, y);
3102 /* When the image has a mask, we can expect that at
3103 least part of a mouse highlight or a block cursor will
3104 be visible. If the image doesn't have a mask, make
3105 a block cursor visible by drawing a rectangle around
3106 the image. I believe it's looking better if we do
3107 nothing here for mouse-face. */
3108 if (s->hl == DRAW_CURSOR)
3110 int r = eabs (s->img->relief);
3111 x_draw_rectangle (s->f, s->gc, x - r, y - r,
3112 s->slice.width + r*2 - 1,
3113 s->slice.height + r*2 - 1);
3117 else
3118 /* Draw a rectangle if image could not be loaded. */
3119 x_draw_rectangle (s->f, s->gc, x, y,
3120 s->slice.width - 1, s->slice.height - 1);
3124 /* Draw part of the background of glyph string S. X, Y, W, and H
3125 give the rectangle to draw. */
3127 static void
3128 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
3130 if (s->stippled_p)
3132 /* Fill background with a stipple pattern. */
3133 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3134 x_fill_rectangle (s->f, s->gc, x, y, w, h);
3135 XSetFillStyle (s->display, s->gc, FillSolid);
3137 else
3138 x_clear_glyph_string_rect (s, x, y, w, h);
3142 /* Draw image glyph string S.
3144 s->y
3145 s->x +-------------------------
3146 | s->face->box
3148 | +-------------------------
3149 | | s->img->margin
3151 | | +-------------------
3152 | | | the image
3156 static void
3157 x_draw_image_glyph_string (struct glyph_string *s)
3159 int box_line_hwidth = eabs (s->face->box_line_width);
3160 int box_line_vwidth = max (s->face->box_line_width, 0);
3161 int height;
3162 Pixmap pixmap = None;
3164 height = s->height;
3165 if (s->slice.y == 0)
3166 height -= box_line_vwidth;
3167 if (s->slice.y + s->slice.height >= s->img->height)
3168 height -= box_line_vwidth;
3170 /* Fill background with face under the image. Do it only if row is
3171 taller than image or if image has a clip mask to reduce
3172 flickering. */
3173 s->stippled_p = s->face->stipple != 0;
3174 if (height > s->slice.height
3175 || s->img->hmargin
3176 || s->img->vmargin
3177 || s->img->mask
3178 || s->img->pixmap == 0
3179 || s->width != s->background_width)
3181 if (s->img->mask)
3183 /* Create a pixmap as large as the glyph string. Fill it
3184 with the background color. Copy the image to it, using
3185 its mask. Copy the temporary pixmap to the display. */
3186 Screen *screen = FRAME_X_SCREEN (s->f);
3187 int depth = DefaultDepthOfScreen (screen);
3189 /* Create a pixmap as large as the glyph string. */
3190 pixmap = XCreatePixmap (s->display, s->window,
3191 s->background_width,
3192 s->height, depth);
3194 /* Don't clip in the following because we're working on the
3195 pixmap. */
3196 XSetClipMask (s->display, s->gc, None);
3198 /* Fill the pixmap with the background color/stipple. */
3199 if (s->stippled_p)
3201 /* Fill background with a stipple pattern. */
3202 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3203 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
3204 XFillRectangle (s->display, pixmap, s->gc,
3205 0, 0, s->background_width, s->height);
3206 XSetFillStyle (s->display, s->gc, FillSolid);
3207 XSetTSOrigin (s->display, s->gc, 0, 0);
3209 else
3211 XGCValues xgcv;
3212 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3213 &xgcv);
3214 XSetForeground (s->display, s->gc, xgcv.background);
3215 XFillRectangle (s->display, pixmap, s->gc,
3216 0, 0, s->background_width, s->height);
3217 XSetForeground (s->display, s->gc, xgcv.foreground);
3220 else
3222 int x = s->x;
3223 int y = s->y;
3224 int width = s->background_width;
3226 if (s->first_glyph->left_box_line_p
3227 && s->slice.x == 0)
3229 x += box_line_hwidth;
3230 width -= box_line_hwidth;
3233 if (s->slice.y == 0)
3234 y += box_line_vwidth;
3236 x_draw_glyph_string_bg_rect (s, x, y, width, height);
3239 s->background_filled_p = true;
3242 /* Draw the foreground. */
3243 #ifdef USE_CAIRO
3244 if (s->img->cr_data)
3246 cairo_t *cr = x_begin_cr_clip (s->f, s->gc);
3248 int x = s->x + s->img->hmargin;
3249 int y = s->y + s->img->vmargin;
3250 int width = s->background_width;
3252 cairo_set_source_surface (cr, s->img->cr_data,
3253 x - s->slice.x,
3254 y - s->slice.y);
3255 cairo_rectangle (cr, x, y, width, height);
3256 cairo_fill (cr);
3257 x_end_cr_clip (s->f);
3259 else
3260 #endif
3261 if (pixmap != None)
3263 x_draw_image_foreground_1 (s, pixmap);
3264 x_set_glyph_string_clipping (s);
3265 XCopyArea (s->display, pixmap, s->window, s->gc,
3266 0, 0, s->background_width, s->height, s->x, s->y);
3267 XFreePixmap (s->display, pixmap);
3269 else
3270 x_draw_image_foreground (s);
3272 /* If we must draw a relief around the image, do it. */
3273 if (s->img->relief
3274 || s->hl == DRAW_IMAGE_RAISED
3275 || s->hl == DRAW_IMAGE_SUNKEN)
3276 x_draw_image_relief (s);
3280 /* Draw stretch glyph string S. */
3282 static void
3283 x_draw_stretch_glyph_string (struct glyph_string *s)
3285 eassert (s->first_glyph->type == STRETCH_GLYPH);
3287 if (s->hl == DRAW_CURSOR
3288 && !x_stretch_cursor_p)
3290 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
3291 wide as the stretch glyph. */
3292 int width, background_width = s->background_width;
3293 int x = s->x;
3295 if (!s->row->reversed_p)
3297 int left_x = window_box_left_offset (s->w, TEXT_AREA);
3299 if (x < left_x)
3301 background_width -= left_x - x;
3302 x = left_x;
3305 else
3307 /* In R2L rows, draw the cursor on the right edge of the
3308 stretch glyph. */
3309 int right_x = window_box_right (s->w, TEXT_AREA);
3311 if (x + background_width > right_x)
3312 background_width -= x - right_x;
3313 x += background_width;
3315 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
3316 if (s->row->reversed_p)
3317 x -= width;
3319 /* Draw cursor. */
3320 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
3322 /* Clear rest using the GC of the original non-cursor face. */
3323 if (width < background_width)
3325 int y = s->y;
3326 int w = background_width - width, h = s->height;
3327 XRectangle r;
3328 GC gc;
3330 if (!s->row->reversed_p)
3331 x += width;
3332 else
3333 x = s->x;
3334 if (s->row->mouse_face_p
3335 && cursor_in_mouse_face_p (s->w))
3337 x_set_mouse_face_gc (s);
3338 gc = s->gc;
3340 else
3341 gc = s->face->gc;
3343 get_glyph_string_clip_rect (s, &r);
3344 x_set_clip_rectangles (s->f, gc, &r, 1);
3346 if (s->face->stipple)
3348 /* Fill background with a stipple pattern. */
3349 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3350 x_fill_rectangle (s->f, gc, x, y, w, h);
3351 XSetFillStyle (s->display, gc, FillSolid);
3353 else
3355 XGCValues xgcv;
3356 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3357 XSetForeground (s->display, gc, xgcv.background);
3358 x_fill_rectangle (s->f, gc, x, y, w, h);
3359 XSetForeground (s->display, gc, xgcv.foreground);
3362 x_reset_clip_rectangles (s->f, gc);
3365 else if (!s->background_filled_p)
3367 int background_width = s->background_width;
3368 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
3370 /* Don't draw into left margin, fringe or scrollbar area
3371 except for header line and mode line. */
3372 if (x < left_x && !s->row->mode_line_p)
3374 background_width -= left_x - x;
3375 x = left_x;
3377 if (background_width > 0)
3378 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
3381 s->background_filled_p = true;
3385 Draw a wavy line under S. The wave fills wave_height pixels from y0.
3387 x0 wave_length = 2
3389 y0 * * * * *
3390 |* * * * * * * * *
3391 wave_height = 3 | * * * *
3395 static void
3396 x_draw_underwave (struct glyph_string *s)
3398 int wave_height = 3, wave_length = 2;
3399 #ifdef USE_CAIRO
3400 x_draw_horizontal_wave (s->f, s->gc, s->x, s->ybase - wave_height + 3,
3401 s->width, wave_height, wave_length);
3402 #else /* not USE_CAIRO */
3403 int dx, dy, x0, y0, width, x1, y1, x2, y2, xmax;
3404 bool odd;
3405 XRectangle wave_clip, string_clip, final_clip;
3407 dx = wave_length;
3408 dy = wave_height - 1;
3409 x0 = s->x;
3410 y0 = s->ybase - wave_height + 3;
3411 width = s->width;
3412 xmax = x0 + width;
3414 /* Find and set clipping rectangle */
3416 wave_clip.x = x0;
3417 wave_clip.y = y0;
3418 wave_clip.width = width;
3419 wave_clip.height = wave_height;
3420 get_glyph_string_clip_rect (s, &string_clip);
3422 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
3423 return;
3425 XSetClipRectangles (s->display, s->gc, 0, 0, &final_clip, 1, Unsorted);
3427 /* Draw the waves */
3429 x1 = x0 - (x0 % dx);
3430 x2 = x1 + dx;
3431 odd = (x1 / dx) & 1;
3432 y1 = y2 = y0;
3434 if (odd)
3435 y1 += dy;
3436 else
3437 y2 += dy;
3439 if (INT_MAX - dx < xmax)
3440 emacs_abort ();
3442 while (x1 <= xmax)
3444 XDrawLine (s->display, s->window, s->gc, x1, y1, x2, y2);
3445 x1 = x2, y1 = y2;
3446 x2 += dx, y2 = y0 + odd*dy;
3447 odd = !odd;
3450 /* Restore previous clipping rectangle(s) */
3451 XSetClipRectangles (s->display, s->gc, 0, 0, s->clip, s->num_clips, Unsorted);
3452 #endif /* not USE_CAIRO */
3456 /* Draw glyph string S. */
3458 static void
3459 x_draw_glyph_string (struct glyph_string *s)
3461 bool relief_drawn_p = false;
3463 /* If S draws into the background of its successors, draw the
3464 background of the successors first so that S can draw into it.
3465 This makes S->next use XDrawString instead of XDrawImageString. */
3466 if (s->next && s->right_overhang && !s->for_overlaps)
3468 int width;
3469 struct glyph_string *next;
3471 for (width = 0, next = s->next;
3472 next && width < s->right_overhang;
3473 width += next->width, next = next->next)
3474 if (next->first_glyph->type != IMAGE_GLYPH)
3476 x_set_glyph_string_gc (next);
3477 x_set_glyph_string_clipping (next);
3478 if (next->first_glyph->type == STRETCH_GLYPH)
3479 x_draw_stretch_glyph_string (next);
3480 else
3481 x_draw_glyph_string_background (next, true);
3482 next->num_clips = 0;
3486 /* Set up S->gc, set clipping and draw S. */
3487 x_set_glyph_string_gc (s);
3489 /* Draw relief (if any) in advance for char/composition so that the
3490 glyph string can be drawn over it. */
3491 if (!s->for_overlaps
3492 && s->face->box != FACE_NO_BOX
3493 && (s->first_glyph->type == CHAR_GLYPH
3494 || s->first_glyph->type == COMPOSITE_GLYPH))
3497 x_set_glyph_string_clipping (s);
3498 x_draw_glyph_string_background (s, true);
3499 x_draw_glyph_string_box (s);
3500 x_set_glyph_string_clipping (s);
3501 relief_drawn_p = true;
3503 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
3504 && !s->clip_tail
3505 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
3506 || (s->next && s->next->hl != s->hl && s->right_overhang)))
3507 /* We must clip just this glyph. left_overhang part has already
3508 drawn when s->prev was drawn, and right_overhang part will be
3509 drawn later when s->next is drawn. */
3510 x_set_glyph_string_clipping_exactly (s, s);
3511 else
3512 x_set_glyph_string_clipping (s);
3514 switch (s->first_glyph->type)
3516 case IMAGE_GLYPH:
3517 x_draw_image_glyph_string (s);
3518 break;
3520 case XWIDGET_GLYPH:
3521 x_draw_xwidget_glyph_string (s);
3522 break;
3524 case STRETCH_GLYPH:
3525 x_draw_stretch_glyph_string (s);
3526 break;
3528 case CHAR_GLYPH:
3529 if (s->for_overlaps)
3530 s->background_filled_p = true;
3531 else
3532 x_draw_glyph_string_background (s, false);
3533 x_draw_glyph_string_foreground (s);
3534 break;
3536 case COMPOSITE_GLYPH:
3537 if (s->for_overlaps || (s->cmp_from > 0
3538 && ! s->first_glyph->u.cmp.automatic))
3539 s->background_filled_p = true;
3540 else
3541 x_draw_glyph_string_background (s, true);
3542 x_draw_composite_glyph_string_foreground (s);
3543 break;
3545 case GLYPHLESS_GLYPH:
3546 if (s->for_overlaps)
3547 s->background_filled_p = true;
3548 else
3549 x_draw_glyph_string_background (s, true);
3550 x_draw_glyphless_glyph_string_foreground (s);
3551 break;
3553 default:
3554 emacs_abort ();
3557 if (!s->for_overlaps)
3559 /* Draw underline. */
3560 if (s->face->underline_p)
3562 if (s->face->underline_type == FACE_UNDER_WAVE)
3564 if (s->face->underline_defaulted_p)
3565 x_draw_underwave (s);
3566 else
3568 XGCValues xgcv;
3569 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3570 XSetForeground (s->display, s->gc, s->face->underline_color);
3571 x_draw_underwave (s);
3572 XSetForeground (s->display, s->gc, xgcv.foreground);
3575 else if (s->face->underline_type == FACE_UNDER_LINE)
3577 unsigned long thickness, position;
3578 int y;
3580 if (s->prev && s->prev->face->underline_p
3581 && s->prev->face->underline_type == FACE_UNDER_LINE)
3583 /* We use the same underline style as the previous one. */
3584 thickness = s->prev->underline_thickness;
3585 position = s->prev->underline_position;
3587 else
3589 /* Get the underline thickness. Default is 1 pixel. */
3590 if (s->font && s->font->underline_thickness > 0)
3591 thickness = s->font->underline_thickness;
3592 else
3593 thickness = 1;
3594 if (x_underline_at_descent_line)
3595 position = (s->height - thickness) - (s->ybase - s->y);
3596 else
3598 /* Get the underline position. This is the recommended
3599 vertical offset in pixels from the baseline to the top of
3600 the underline. This is a signed value according to the
3601 specs, and its default is
3603 ROUND ((maximum descent) / 2), with
3604 ROUND(x) = floor (x + 0.5) */
3606 if (x_use_underline_position_properties
3607 && s->font && s->font->underline_position >= 0)
3608 position = s->font->underline_position;
3609 else if (s->font)
3610 position = (s->font->descent + 1) / 2;
3611 else
3612 position = underline_minimum_offset;
3614 position = max (position, underline_minimum_offset);
3616 /* Check the sanity of thickness and position. We should
3617 avoid drawing underline out of the current line area. */
3618 if (s->y + s->height <= s->ybase + position)
3619 position = (s->height - 1) - (s->ybase - s->y);
3620 if (s->y + s->height < s->ybase + position + thickness)
3621 thickness = (s->y + s->height) - (s->ybase + position);
3622 s->underline_thickness = thickness;
3623 s->underline_position = position;
3624 y = s->ybase + position;
3625 if (s->face->underline_defaulted_p)
3626 x_fill_rectangle (s->f, s->gc,
3627 s->x, y, s->width, thickness);
3628 else
3630 XGCValues xgcv;
3631 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3632 XSetForeground (s->display, s->gc, s->face->underline_color);
3633 x_fill_rectangle (s->f, s->gc,
3634 s->x, y, s->width, thickness);
3635 XSetForeground (s->display, s->gc, xgcv.foreground);
3639 /* Draw overline. */
3640 if (s->face->overline_p)
3642 unsigned long dy = 0, h = 1;
3644 if (s->face->overline_color_defaulted_p)
3645 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3646 s->width, h);
3647 else
3649 XGCValues xgcv;
3650 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3651 XSetForeground (s->display, s->gc, s->face->overline_color);
3652 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3653 s->width, h);
3654 XSetForeground (s->display, s->gc, xgcv.foreground);
3658 /* Draw strike-through. */
3659 if (s->face->strike_through_p)
3661 unsigned long h = 1;
3662 unsigned long dy = (s->height - h) / 2;
3664 if (s->face->strike_through_color_defaulted_p)
3665 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3666 s->width, h);
3667 else
3669 XGCValues xgcv;
3670 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3671 XSetForeground (s->display, s->gc, s->face->strike_through_color);
3672 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3673 s->width, h);
3674 XSetForeground (s->display, s->gc, xgcv.foreground);
3678 /* Draw relief if not yet drawn. */
3679 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
3680 x_draw_glyph_string_box (s);
3682 if (s->prev)
3684 struct glyph_string *prev;
3686 for (prev = s->prev; prev; prev = prev->prev)
3687 if (prev->hl != s->hl
3688 && prev->x + prev->width + prev->right_overhang > s->x)
3690 /* As prev was drawn while clipped to its own area, we
3691 must draw the right_overhang part using s->hl now. */
3692 enum draw_glyphs_face save = prev->hl;
3694 prev->hl = s->hl;
3695 x_set_glyph_string_gc (prev);
3696 x_set_glyph_string_clipping_exactly (s, prev);
3697 if (prev->first_glyph->type == CHAR_GLYPH)
3698 x_draw_glyph_string_foreground (prev);
3699 else
3700 x_draw_composite_glyph_string_foreground (prev);
3701 x_reset_clip_rectangles (prev->f, prev->gc);
3702 prev->hl = save;
3703 prev->num_clips = 0;
3707 if (s->next)
3709 struct glyph_string *next;
3711 for (next = s->next; next; next = next->next)
3712 if (next->hl != s->hl
3713 && next->x - next->left_overhang < s->x + s->width)
3715 /* As next will be drawn while clipped to its own area,
3716 we must draw the left_overhang part using s->hl now. */
3717 enum draw_glyphs_face save = next->hl;
3719 next->hl = s->hl;
3720 x_set_glyph_string_gc (next);
3721 x_set_glyph_string_clipping_exactly (s, next);
3722 if (next->first_glyph->type == CHAR_GLYPH)
3723 x_draw_glyph_string_foreground (next);
3724 else
3725 x_draw_composite_glyph_string_foreground (next);
3726 x_reset_clip_rectangles (next->f, next->gc);
3727 next->hl = save;
3728 next->num_clips = 0;
3729 next->clip_head = s->next;
3734 /* Reset clipping. */
3735 x_reset_clip_rectangles (s->f, s->gc);
3736 s->num_clips = 0;
3739 /* Shift display to make room for inserted glyphs. */
3741 static void
3742 x_shift_glyphs_for_insert (struct frame *f, int x, int y, int width, int height, int shift_by)
3744 /* Never called on a GUI frame, see
3745 http://lists.gnu.org/archive/html/emacs-devel/2015-05/msg00456.html
3747 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3748 f->output_data.x->normal_gc,
3749 x, y, width, height,
3750 x + shift_by, y);
3753 /* Delete N glyphs at the nominal cursor position. Not implemented
3754 for X frames. */
3756 static void
3757 x_delete_glyphs (struct frame *f, register int n)
3759 emacs_abort ();
3763 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
3764 If they are <= 0, this is probably an error. */
3766 static ATTRIBUTE_UNUSED void
3767 x_clear_area1 (Display *dpy, Window window,
3768 int x, int y, int width, int height, int exposures)
3770 eassert (width > 0 && height > 0);
3771 XClearArea (dpy, window, x, y, width, height, exposures);
3774 void
3775 x_clear_area (struct frame *f, int x, int y, int width, int height)
3777 #ifdef USE_CAIRO
3778 cairo_t *cr;
3780 eassert (width > 0 && height > 0);
3782 cr = x_begin_cr_clip (f, NULL);
3783 x_set_cr_source_with_gc_background (f, f->output_data.x->normal_gc);
3784 cairo_rectangle (cr, x, y, width, height);
3785 cairo_fill (cr);
3786 x_end_cr_clip (f);
3787 #else
3788 x_clear_area1 (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3789 x, y, width, height, False);
3790 #endif
3794 /* Clear an entire frame. */
3796 static void
3797 x_clear_frame (struct frame *f)
3799 /* Clearing the frame will erase any cursor, so mark them all as no
3800 longer visible. */
3801 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
3803 block_input ();
3805 x_clear_window (f);
3807 /* We have to clear the scroll bars. If we have changed colors or
3808 something like that, then they should be notified. */
3809 x_scroll_bar_clear (f);
3811 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3812 /* Make sure scroll bars are redrawn. As they aren't redrawn by
3813 redisplay, do it here. */
3814 if (FRAME_GTK_WIDGET (f))
3815 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
3816 #endif
3818 XFlush (FRAME_X_DISPLAY (f));
3820 unblock_input ();
3823 /* RIF: Show hourglass cursor on frame F. */
3825 static void
3826 x_show_hourglass (struct frame *f)
3828 Display *dpy = FRAME_X_DISPLAY (f);
3830 if (dpy)
3832 struct x_output *x = FRAME_X_OUTPUT (f);
3833 #ifdef USE_X_TOOLKIT
3834 if (x->widget)
3835 #else
3836 if (FRAME_OUTER_WINDOW (f))
3837 #endif
3839 x->hourglass_p = true;
3841 if (!x->hourglass_window)
3843 unsigned long mask = CWCursor;
3844 XSetWindowAttributes attrs;
3845 #ifdef USE_GTK
3846 Window parent = FRAME_X_WINDOW (f);
3847 #else
3848 Window parent = FRAME_OUTER_WINDOW (f);
3849 #endif
3850 attrs.cursor = x->hourglass_cursor;
3852 x->hourglass_window = XCreateWindow
3853 (dpy, parent, 0, 0, 32000, 32000, 0, 0,
3854 InputOnly, CopyFromParent, mask, &attrs);
3857 XMapRaised (dpy, x->hourglass_window);
3858 XFlush (dpy);
3863 /* RIF: Cancel hourglass cursor on frame F. */
3865 static void
3866 x_hide_hourglass (struct frame *f)
3868 struct x_output *x = FRAME_X_OUTPUT (f);
3870 /* Watch out for newly created frames. */
3871 if (x->hourglass_window)
3873 XUnmapWindow (FRAME_X_DISPLAY (f), x->hourglass_window);
3874 /* Sync here because XTread_socket looks at the
3875 hourglass_p flag that is reset to zero below. */
3876 XSync (FRAME_X_DISPLAY (f), False);
3877 x->hourglass_p = false;
3881 /* Invert the middle quarter of the frame for .15 sec. */
3883 static void
3884 XTflash (struct frame *f)
3886 block_input ();
3889 #ifdef USE_GTK
3890 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
3891 when the scroll bars and the edit widget share the same X window. */
3892 GdkWindow *window = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
3893 #ifdef HAVE_GTK3
3894 cairo_t *cr = gdk_cairo_create (window);
3895 cairo_set_source_rgb (cr, 1, 1, 1);
3896 cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
3897 #define XFillRectangle(d, win, gc, x, y, w, h) \
3898 do { \
3899 cairo_rectangle (cr, x, y, w, h); \
3900 cairo_fill (cr); \
3902 while (false)
3903 #else /* ! HAVE_GTK3 */
3904 GdkGCValues vals;
3905 GdkGC *gc;
3906 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
3907 ^ FRAME_BACKGROUND_PIXEL (f));
3908 vals.function = GDK_XOR;
3909 gc = gdk_gc_new_with_values (window,
3910 &vals, GDK_GC_FUNCTION | GDK_GC_FOREGROUND);
3911 #define XFillRectangle(d, win, gc, x, y, w, h) \
3912 gdk_draw_rectangle (window, gc, true, x, y, w, h)
3913 #endif /* ! HAVE_GTK3 */
3914 #else /* ! USE_GTK */
3915 GC gc;
3917 /* Create a GC that will use the GXxor function to flip foreground
3918 pixels into background pixels. */
3920 XGCValues values;
3922 values.function = GXxor;
3923 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3924 ^ FRAME_BACKGROUND_PIXEL (f));
3926 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3927 GCFunction | GCForeground, &values);
3929 #endif
3931 /* Get the height not including a menu bar widget. */
3932 int height = FRAME_PIXEL_HEIGHT (f);
3933 /* Height of each line to flash. */
3934 int flash_height = FRAME_LINE_HEIGHT (f);
3935 /* These will be the left and right margins of the rectangles. */
3936 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3937 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3938 int width = flash_right - flash_left;
3940 /* If window is tall, flash top and bottom line. */
3941 if (height > 3 * FRAME_LINE_HEIGHT (f))
3943 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3944 flash_left,
3945 (FRAME_INTERNAL_BORDER_WIDTH (f)
3946 + FRAME_TOP_MARGIN_HEIGHT (f)),
3947 width, flash_height);
3948 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3949 flash_left,
3950 (height - flash_height
3951 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3952 width, flash_height);
3955 else
3956 /* If it is short, flash it all. */
3957 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3958 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3959 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3961 x_flush (f);
3964 struct timespec delay = make_timespec (0, 150 * 1000 * 1000);
3965 struct timespec wakeup = timespec_add (current_timespec (), delay);
3967 /* Keep waiting until past the time wakeup or any input gets
3968 available. */
3969 while (! detect_input_pending ())
3971 struct timespec current = current_timespec ();
3972 struct timespec timeout;
3974 /* Break if result would not be positive. */
3975 if (timespec_cmp (wakeup, current) <= 0)
3976 break;
3978 /* How long `select' should wait. */
3979 timeout = make_timespec (0, 10 * 1000 * 1000);
3981 /* Try to wait that long--but we might wake up sooner. */
3982 pselect (0, NULL, NULL, NULL, &timeout, NULL);
3986 /* If window is tall, flash top and bottom line. */
3987 if (height > 3 * FRAME_LINE_HEIGHT (f))
3989 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3990 flash_left,
3991 (FRAME_INTERNAL_BORDER_WIDTH (f)
3992 + FRAME_TOP_MARGIN_HEIGHT (f)),
3993 width, flash_height);
3994 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3995 flash_left,
3996 (height - flash_height
3997 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3998 width, flash_height);
4000 else
4001 /* If it is short, flash it all. */
4002 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
4003 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
4004 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
4006 #ifdef USE_GTK
4007 #ifdef HAVE_GTK3
4008 cairo_destroy (cr);
4009 #else
4010 g_object_unref (G_OBJECT (gc));
4011 #endif
4012 #undef XFillRectangle
4013 #else
4014 XFreeGC (FRAME_X_DISPLAY (f), gc);
4015 #endif
4016 x_flush (f);
4020 unblock_input ();
4024 static void
4025 XTtoggle_invisible_pointer (struct frame *f, bool invisible)
4027 block_input ();
4028 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, invisible);
4029 unblock_input ();
4033 /* Make audible bell. */
4035 static void
4036 XTring_bell (struct frame *f)
4038 if (FRAME_X_DISPLAY (f))
4040 if (visible_bell)
4041 XTflash (f);
4042 else
4044 block_input ();
4045 #ifdef HAVE_XKB
4046 XkbBell (FRAME_X_DISPLAY (f), None, 0, None);
4047 #else
4048 XBell (FRAME_X_DISPLAY (f), 0);
4049 #endif
4050 XFlush (FRAME_X_DISPLAY (f));
4051 unblock_input ();
4056 /***********************************************************************
4057 Line Dance
4058 ***********************************************************************/
4060 /* Perform an insert-lines or delete-lines operation, inserting N
4061 lines or deleting -N lines at vertical position VPOS. */
4063 static void
4064 x_ins_del_lines (struct frame *f, int vpos, int n)
4066 emacs_abort ();
4070 /* Scroll part of the display as described by RUN. */
4072 static void
4073 x_scroll_run (struct window *w, struct run *run)
4075 struct frame *f = XFRAME (w->frame);
4076 int x, y, width, height, from_y, to_y, bottom_y;
4078 /* Get frame-relative bounding box of the text display area of W,
4079 without mode lines. Include in this box the left and right
4080 fringe of W. */
4081 window_box (w, ANY_AREA, &x, &y, &width, &height);
4083 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
4084 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
4085 bottom_y = y + height;
4087 if (to_y < from_y)
4089 /* Scrolling up. Make sure we don't copy part of the mode
4090 line at the bottom. */
4091 if (from_y + run->height > bottom_y)
4092 height = bottom_y - from_y;
4093 else
4094 height = run->height;
4096 else
4098 /* Scrolling down. Make sure we don't copy over the mode line.
4099 at the bottom. */
4100 if (to_y + run->height > bottom_y)
4101 height = bottom_y - to_y;
4102 else
4103 height = run->height;
4106 block_input ();
4108 /* Cursor off. Will be switched on again in x_update_window_end. */
4109 x_clear_cursor (w);
4111 #ifdef USE_CAIRO
4112 SET_FRAME_GARBAGED (f);
4113 #else
4114 XCopyArea (FRAME_X_DISPLAY (f),
4115 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4116 f->output_data.x->normal_gc,
4117 x, from_y,
4118 width, height,
4119 x, to_y);
4120 #endif
4122 unblock_input ();
4127 /***********************************************************************
4128 Exposure Events
4129 ***********************************************************************/
4132 static void
4133 frame_highlight (struct frame *f)
4135 /* We used to only do this if Vx_no_window_manager was non-nil, but
4136 the ICCCM (section 4.1.6) says that the window's border pixmap
4137 and border pixel are window attributes which are "private to the
4138 client", so we can always change it to whatever we want. */
4139 block_input ();
4140 /* I recently started to get errors in this XSetWindowBorder, depending on
4141 the window-manager in use, tho something more is at play since I've been
4142 using that same window-manager binary for ever. Let's not crash just
4143 because of this (bug#9310). */
4144 x_catch_errors (FRAME_X_DISPLAY (f));
4145 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4146 f->output_data.x->border_pixel);
4147 x_uncatch_errors ();
4148 unblock_input ();
4149 x_update_cursor (f, true);
4150 x_set_frame_alpha (f);
4153 static void
4154 frame_unhighlight (struct frame *f)
4156 /* We used to only do this if Vx_no_window_manager was non-nil, but
4157 the ICCCM (section 4.1.6) says that the window's border pixmap
4158 and border pixel are window attributes which are "private to the
4159 client", so we can always change it to whatever we want. */
4160 block_input ();
4161 /* Same as above for XSetWindowBorder (bug#9310). */
4162 x_catch_errors (FRAME_X_DISPLAY (f));
4163 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4164 f->output_data.x->border_tile);
4165 x_uncatch_errors ();
4166 unblock_input ();
4167 x_update_cursor (f, true);
4168 x_set_frame_alpha (f);
4171 /* The focus has changed. Update the frames as necessary to reflect
4172 the new situation. Note that we can't change the selected frame
4173 here, because the Lisp code we are interrupting might become confused.
4174 Each event gets marked with the frame in which it occurred, so the
4175 Lisp code can tell when the switch took place by examining the events. */
4177 static void
4178 x_new_focus_frame (struct x_display_info *dpyinfo, struct frame *frame)
4180 struct frame *old_focus = dpyinfo->x_focus_frame;
4182 if (frame != dpyinfo->x_focus_frame)
4184 /* Set this before calling other routines, so that they see
4185 the correct value of x_focus_frame. */
4186 dpyinfo->x_focus_frame = frame;
4188 if (old_focus && old_focus->auto_lower)
4189 x_lower_frame (old_focus);
4191 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
4192 dpyinfo->x_pending_autoraise_frame = dpyinfo->x_focus_frame;
4193 else
4194 dpyinfo->x_pending_autoraise_frame = NULL;
4197 x_frame_rehighlight (dpyinfo);
4200 /* Handle FocusIn and FocusOut state changes for FRAME.
4201 If FRAME has focus and there exists more than one frame, puts
4202 a FOCUS_IN_EVENT into *BUFP. */
4204 static void
4205 x_focus_changed (int type, int state, struct x_display_info *dpyinfo, struct frame *frame, struct input_event *bufp)
4207 if (type == FocusIn)
4209 if (dpyinfo->x_focus_event_frame != frame)
4211 x_new_focus_frame (dpyinfo, frame);
4212 dpyinfo->x_focus_event_frame = frame;
4214 /* Don't stop displaying the initial startup message
4215 for a switch-frame event we don't need. */
4216 /* When run as a daemon, Vterminal_frame is always NIL. */
4217 bufp->arg = (((NILP (Vterminal_frame)
4218 || ! FRAME_X_P (XFRAME (Vterminal_frame))
4219 || EQ (Fdaemonp (), Qt))
4220 && CONSP (Vframe_list)
4221 && !NILP (XCDR (Vframe_list)))
4222 ? Qt : Qnil);
4223 bufp->kind = FOCUS_IN_EVENT;
4224 XSETFRAME (bufp->frame_or_window, frame);
4227 frame->output_data.x->focus_state |= state;
4229 #ifdef HAVE_X_I18N
4230 if (FRAME_XIC (frame))
4231 XSetICFocus (FRAME_XIC (frame));
4232 #endif
4234 else if (type == FocusOut)
4236 frame->output_data.x->focus_state &= ~state;
4238 if (dpyinfo->x_focus_event_frame == frame)
4240 dpyinfo->x_focus_event_frame = 0;
4241 x_new_focus_frame (dpyinfo, 0);
4243 bufp->kind = FOCUS_OUT_EVENT;
4244 XSETFRAME (bufp->frame_or_window, frame);
4247 #ifdef HAVE_X_I18N
4248 if (FRAME_XIC (frame))
4249 XUnsetICFocus (FRAME_XIC (frame));
4250 #endif
4251 if (frame->pointer_invisible)
4252 XTtoggle_invisible_pointer (frame, false);
4256 /* Return the Emacs frame-object corresponding to an X window.
4257 It could be the frame's main window or an icon window. */
4259 static struct frame *
4260 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4262 Lisp_Object tail, frame;
4263 struct frame *f;
4265 if (wdesc == None)
4266 return NULL;
4268 FOR_EACH_FRAME (tail, frame)
4270 f = XFRAME (frame);
4271 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4272 continue;
4273 if (f->output_data.x->hourglass_window == wdesc)
4274 return f;
4275 #ifdef USE_X_TOOLKIT
4276 if ((f->output_data.x->edit_widget
4277 && XtWindow (f->output_data.x->edit_widget) == wdesc)
4278 /* A tooltip frame? */
4279 || (!f->output_data.x->edit_widget
4280 && FRAME_X_WINDOW (f) == wdesc)
4281 || f->output_data.x->icon_desc == wdesc)
4282 return f;
4283 #else /* not USE_X_TOOLKIT */
4284 #ifdef USE_GTK
4285 if (f->output_data.x->edit_widget)
4287 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4288 struct x_output *x = f->output_data.x;
4289 if (gwdesc != 0 && gwdesc == x->edit_widget)
4290 return f;
4292 #endif /* USE_GTK */
4293 if (FRAME_X_WINDOW (f) == wdesc
4294 || f->output_data.x->icon_desc == wdesc)
4295 return f;
4296 #endif /* not USE_X_TOOLKIT */
4298 return 0;
4301 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
4303 /* Like x_window_to_frame but also compares the window with the widget's
4304 windows. */
4306 static struct frame *
4307 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4309 Lisp_Object tail, frame;
4310 struct frame *f, *found = NULL;
4311 struct x_output *x;
4313 if (wdesc == None)
4314 return NULL;
4316 FOR_EACH_FRAME (tail, frame)
4318 if (found)
4319 break;
4320 f = XFRAME (frame);
4321 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
4323 /* This frame matches if the window is any of its widgets. */
4324 x = f->output_data.x;
4325 if (x->hourglass_window == wdesc)
4326 found = f;
4327 else if (x->widget)
4329 #ifdef USE_GTK
4330 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4331 if (gwdesc != 0
4332 && gtk_widget_get_toplevel (gwdesc) == x->widget)
4333 found = f;
4334 #else
4335 if (wdesc == XtWindow (x->widget)
4336 || wdesc == XtWindow (x->column_widget)
4337 || wdesc == XtWindow (x->edit_widget))
4338 found = f;
4339 /* Match if the window is this frame's menubar. */
4340 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
4341 found = f;
4342 #endif
4344 else if (FRAME_X_WINDOW (f) == wdesc)
4345 /* A tooltip frame. */
4346 found = f;
4350 return found;
4353 /* Likewise, but consider only the menu bar widget. */
4355 static struct frame *
4356 x_menubar_window_to_frame (struct x_display_info *dpyinfo,
4357 const XEvent *event)
4359 Window wdesc = event->xany.window;
4360 Lisp_Object tail, frame;
4361 struct frame *f;
4362 struct x_output *x;
4364 if (wdesc == None)
4365 return NULL;
4367 FOR_EACH_FRAME (tail, frame)
4369 f = XFRAME (frame);
4370 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4371 continue;
4372 x = f->output_data.x;
4373 #ifdef USE_GTK
4374 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
4375 return f;
4376 #else
4377 /* Match if the window is this frame's menubar. */
4378 if (x->menubar_widget
4379 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
4380 return f;
4381 #endif
4383 return 0;
4386 /* Return the frame whose principal (outermost) window is WDESC.
4387 If WDESC is some other (smaller) window, we return 0. */
4389 struct frame *
4390 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4392 Lisp_Object tail, frame;
4393 struct frame *f;
4394 struct x_output *x;
4396 if (wdesc == None)
4397 return NULL;
4399 FOR_EACH_FRAME (tail, frame)
4401 f = XFRAME (frame);
4402 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4403 continue;
4404 x = f->output_data.x;
4406 if (x->widget)
4408 /* This frame matches if the window is its topmost widget. */
4409 #ifdef USE_GTK
4410 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4411 if (gwdesc == x->widget)
4412 return f;
4413 #else
4414 if (wdesc == XtWindow (x->widget))
4415 return f;
4416 #endif
4418 else if (FRAME_X_WINDOW (f) == wdesc)
4419 /* Tooltip frame. */
4420 return f;
4422 return 0;
4425 #else /* !USE_X_TOOLKIT && !USE_GTK */
4427 #define x_any_window_to_frame(d, i) x_window_to_frame (d, i)
4428 #define x_top_window_to_frame(d, i) x_window_to_frame (d, i)
4430 #endif /* USE_X_TOOLKIT || USE_GTK */
4432 /* The focus may have changed. Figure out if it is a real focus change,
4433 by checking both FocusIn/Out and Enter/LeaveNotify events.
4435 Returns FOCUS_IN_EVENT event in *BUFP. */
4437 static void
4438 x_detect_focus_change (struct x_display_info *dpyinfo, struct frame *frame,
4439 const XEvent *event, struct input_event *bufp)
4441 if (!frame)
4442 return;
4444 switch (event->type)
4446 case EnterNotify:
4447 case LeaveNotify:
4449 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
4450 int focus_state
4451 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
4453 if (event->xcrossing.detail != NotifyInferior
4454 && event->xcrossing.focus
4455 && ! (focus_state & FOCUS_EXPLICIT))
4456 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
4457 FOCUS_IMPLICIT,
4458 dpyinfo, frame, bufp);
4460 break;
4462 case FocusIn:
4463 case FocusOut:
4464 x_focus_changed (event->type,
4465 (event->xfocus.detail == NotifyPointer ?
4466 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
4467 dpyinfo, frame, bufp);
4468 break;
4470 case ClientMessage:
4471 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
4473 enum xembed_message msg = event->xclient.data.l[1];
4474 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
4475 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
4477 break;
4482 #if !defined USE_X_TOOLKIT && !defined USE_GTK
4483 /* Handle an event saying the mouse has moved out of an Emacs frame. */
4485 void
4486 x_mouse_leave (struct x_display_info *dpyinfo)
4488 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
4490 #endif
4492 /* The focus has changed, or we have redirected a frame's focus to
4493 another frame (this happens when a frame uses a surrogate
4494 mini-buffer frame). Shift the highlight as appropriate.
4496 The FRAME argument doesn't necessarily have anything to do with which
4497 frame is being highlighted or un-highlighted; we only use it to find
4498 the appropriate X display info. */
4500 static void
4501 XTframe_rehighlight (struct frame *frame)
4503 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
4506 static void
4507 x_frame_rehighlight (struct x_display_info *dpyinfo)
4509 struct frame *old_highlight = dpyinfo->x_highlight_frame;
4511 if (dpyinfo->x_focus_frame)
4513 dpyinfo->x_highlight_frame
4514 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
4515 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
4516 : dpyinfo->x_focus_frame);
4517 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
4519 fset_focus_frame (dpyinfo->x_focus_frame, Qnil);
4520 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
4523 else
4524 dpyinfo->x_highlight_frame = 0;
4526 if (dpyinfo->x_highlight_frame != old_highlight)
4528 if (old_highlight)
4529 frame_unhighlight (old_highlight);
4530 if (dpyinfo->x_highlight_frame)
4531 frame_highlight (dpyinfo->x_highlight_frame);
4537 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
4539 /* Initialize mode_switch_bit and modifier_meaning. */
4540 static void
4541 x_find_modifier_meanings (struct x_display_info *dpyinfo)
4543 int min_code, max_code;
4544 KeySym *syms;
4545 int syms_per_code;
4546 XModifierKeymap *mods;
4548 dpyinfo->meta_mod_mask = 0;
4549 dpyinfo->shift_lock_mask = 0;
4550 dpyinfo->alt_mod_mask = 0;
4551 dpyinfo->super_mod_mask = 0;
4552 dpyinfo->hyper_mod_mask = 0;
4554 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
4556 syms = XGetKeyboardMapping (dpyinfo->display,
4557 min_code, max_code - min_code + 1,
4558 &syms_per_code);
4559 mods = XGetModifierMapping (dpyinfo->display);
4561 /* Scan the modifier table to see which modifier bits the Meta and
4562 Alt keysyms are on. */
4564 int row, col; /* The row and column in the modifier table. */
4565 bool found_alt_or_meta;
4567 for (row = 3; row < 8; row++)
4569 found_alt_or_meta = false;
4570 for (col = 0; col < mods->max_keypermod; col++)
4572 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
4574 /* Zeroes are used for filler. Skip them. */
4575 if (code == 0)
4576 continue;
4578 /* Are any of this keycode's keysyms a meta key? */
4580 int code_col;
4582 for (code_col = 0; code_col < syms_per_code; code_col++)
4584 int sym = syms[((code - min_code) * syms_per_code) + code_col];
4586 switch (sym)
4588 case XK_Meta_L:
4589 case XK_Meta_R:
4590 found_alt_or_meta = true;
4591 dpyinfo->meta_mod_mask |= (1 << row);
4592 break;
4594 case XK_Alt_L:
4595 case XK_Alt_R:
4596 found_alt_or_meta = true;
4597 dpyinfo->alt_mod_mask |= (1 << row);
4598 break;
4600 case XK_Hyper_L:
4601 case XK_Hyper_R:
4602 if (!found_alt_or_meta)
4603 dpyinfo->hyper_mod_mask |= (1 << row);
4604 code_col = syms_per_code;
4605 col = mods->max_keypermod;
4606 break;
4608 case XK_Super_L:
4609 case XK_Super_R:
4610 if (!found_alt_or_meta)
4611 dpyinfo->super_mod_mask |= (1 << row);
4612 code_col = syms_per_code;
4613 col = mods->max_keypermod;
4614 break;
4616 case XK_Shift_Lock:
4617 /* Ignore this if it's not on the lock modifier. */
4618 if (!found_alt_or_meta && ((1 << row) == LockMask))
4619 dpyinfo->shift_lock_mask = LockMask;
4620 code_col = syms_per_code;
4621 col = mods->max_keypermod;
4622 break;
4630 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
4631 if (! dpyinfo->meta_mod_mask)
4633 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
4634 dpyinfo->alt_mod_mask = 0;
4637 /* If some keys are both alt and meta,
4638 make them just meta, not alt. */
4639 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
4641 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
4644 XFree (syms);
4645 XFreeModifiermap (mods);
4648 /* Convert between the modifier bits X uses and the modifier bits
4649 Emacs uses. */
4652 x_x_to_emacs_modifiers (struct x_display_info *dpyinfo, int state)
4654 int mod_meta = meta_modifier;
4655 int mod_alt = alt_modifier;
4656 int mod_hyper = hyper_modifier;
4657 int mod_super = super_modifier;
4658 Lisp_Object tem;
4660 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4661 if (INTEGERP (tem)) mod_alt = XINT (tem) & INT_MAX;
4662 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4663 if (INTEGERP (tem)) mod_meta = XINT (tem) & INT_MAX;
4664 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4665 if (INTEGERP (tem)) mod_hyper = XINT (tem) & INT_MAX;
4666 tem = Fget (Vx_super_keysym, Qmodifier_value);
4667 if (INTEGERP (tem)) mod_super = XINT (tem) & INT_MAX;
4669 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
4670 | ((state & ControlMask) ? ctrl_modifier : 0)
4671 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
4672 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
4673 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
4674 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
4677 static int
4678 x_emacs_to_x_modifiers (struct x_display_info *dpyinfo, EMACS_INT state)
4680 EMACS_INT mod_meta = meta_modifier;
4681 EMACS_INT mod_alt = alt_modifier;
4682 EMACS_INT mod_hyper = hyper_modifier;
4683 EMACS_INT mod_super = super_modifier;
4685 Lisp_Object tem;
4687 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4688 if (INTEGERP (tem)) mod_alt = XINT (tem);
4689 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4690 if (INTEGERP (tem)) mod_meta = XINT (tem);
4691 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4692 if (INTEGERP (tem)) mod_hyper = XINT (tem);
4693 tem = Fget (Vx_super_keysym, Qmodifier_value);
4694 if (INTEGERP (tem)) mod_super = XINT (tem);
4697 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
4698 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
4699 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
4700 | ((state & shift_modifier) ? ShiftMask : 0)
4701 | ((state & ctrl_modifier) ? ControlMask : 0)
4702 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
4705 /* Convert a keysym to its name. */
4707 char *
4708 x_get_keysym_name (int keysym)
4710 char *value;
4712 block_input ();
4713 value = XKeysymToString (keysym);
4714 unblock_input ();
4716 return value;
4719 /* Mouse clicks and mouse movement. Rah.
4721 Formerly, we used PointerMotionHintMask (in standard_event_mask)
4722 so that we would have to call XQueryPointer after each MotionNotify
4723 event to ask for another such event. However, this made mouse tracking
4724 slow, and there was a bug that made it eventually stop.
4726 Simply asking for MotionNotify all the time seems to work better.
4728 In order to avoid asking for motion events and then throwing most
4729 of them away or busy-polling the server for mouse positions, we ask
4730 the server for pointer motion hints. This means that we get only
4731 one event per group of mouse movements. "Groups" are delimited by
4732 other kinds of events (focus changes and button clicks, for
4733 example), or by XQueryPointer calls; when one of these happens, we
4734 get another MotionNotify event the next time the mouse moves. This
4735 is at least as efficient as getting motion events when mouse
4736 tracking is on, and I suspect only negligibly worse when tracking
4737 is off. */
4739 /* Prepare a mouse-event in *RESULT for placement in the input queue.
4741 If the event is a button press, then note that we have grabbed
4742 the mouse. */
4744 static Lisp_Object
4745 construct_mouse_click (struct input_event *result,
4746 const XButtonEvent *event,
4747 struct frame *f)
4749 /* Make the event type NO_EVENT; we'll change that when we decide
4750 otherwise. */
4751 result->kind = MOUSE_CLICK_EVENT;
4752 result->code = event->button - Button1;
4753 result->timestamp = event->time;
4754 result->modifiers = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f),
4755 event->state)
4756 | (event->type == ButtonRelease
4757 ? up_modifier