Add fragmentation module to CSS property list
[emacs.git] / src / xterm.c
blob1f71afd7f70c47cbdcaf6593755288839a89c113
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
10 (at 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
189 };
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
207 };
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
270 \f
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;
284 };
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
790 };
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
867 \f
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_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_FROM_ID (f, WINDOW_DIVIDER_FACE_ID);
1085 struct face *face_first = FACE_FROM_ID (f, WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
1086 struct face *face_last = FACE_FROM_ID (f, WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
1087 unsigned long color = face ? face->foreground : FRAME_FOREGROUND_PIXEL (f);
1088 unsigned long color_first = (face_first
1089 ? face_first->foreground
1090 : FRAME_FOREGROUND_PIXEL (f));
1091 unsigned long color_last = (face_last
1092 ? face_last->foreground
1093 : FRAME_FOREGROUND_PIXEL (f));
1094 Display *display = FRAME_X_DISPLAY (f);
1096 if (y1 - y0 > x1 - x0 && x1 - x0 > 2)
1097 /* Vertical. */
1099 XSetForeground (display, f->output_data.x->normal_gc, color_first);
1100 x_fill_rectangle (f, f->output_data.x->normal_gc,
1101 x0, y0, 1, y1 - y0);
1102 XSetForeground (display, f->output_data.x->normal_gc, color);
1103 x_fill_rectangle (f, f->output_data.x->normal_gc,
1104 x0 + 1, y0, x1 - x0 - 2, y1 - y0);
1105 XSetForeground (display, f->output_data.x->normal_gc, color_last);
1106 x_fill_rectangle (f, f->output_data.x->normal_gc,
1107 x1 - 1, y0, 1, y1 - y0);
1109 else if (x1 - x0 > y1 - y0 && y1 - y0 > 3)
1110 /* Horizontal. */
1112 XSetForeground (display, f->output_data.x->normal_gc, color_first);
1113 x_fill_rectangle (f, f->output_data.x->normal_gc,
1114 x0, y0, x1 - x0, 1);
1115 XSetForeground (display, f->output_data.x->normal_gc, color);
1116 x_fill_rectangle (f, f->output_data.x->normal_gc,
1117 x0, y0 + 1, x1 - x0, y1 - y0 - 2);
1118 XSetForeground (display, f->output_data.x->normal_gc, color_last);
1119 x_fill_rectangle (f, f->output_data.x->normal_gc,
1120 x0, y1 - 1, x1 - x0, 1);
1122 else
1124 XSetForeground (display, f->output_data.x->normal_gc, color);
1125 x_fill_rectangle (f, f->output_data.x->normal_gc,
1126 x0, y0, x1 - x0, y1 - y0);
1130 /* End update of window W.
1132 Draw vertical borders between horizontally adjacent windows, and
1133 display W's cursor if CURSOR_ON_P is non-zero.
1135 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
1136 glyphs in mouse-face were overwritten. In that case we have to
1137 make sure that the mouse-highlight is properly redrawn.
1139 W may be a menu bar pseudo-window in case we don't have X toolkit
1140 support. Such windows don't have a cursor, so don't display it
1141 here. */
1143 static void
1144 x_update_window_end (struct window *w, bool cursor_on_p,
1145 bool mouse_face_overwritten_p)
1147 if (!w->pseudo_window_p)
1149 block_input ();
1151 if (cursor_on_p)
1152 display_and_set_cursor (w, true,
1153 w->output_cursor.hpos, w->output_cursor.vpos,
1154 w->output_cursor.x, w->output_cursor.y);
1156 if (draw_window_fringes (w, true))
1158 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
1159 x_draw_right_divider (w);
1160 else
1161 x_draw_vertical_border (w);
1164 unblock_input ();
1167 /* If a row with mouse-face was overwritten, arrange for
1168 XTframe_up_to_date to redisplay the mouse highlight. */
1169 if (mouse_face_overwritten_p)
1171 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
1173 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
1174 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
1175 hlinfo->mouse_face_window = Qnil;
1180 /* End update of frame F. This function is installed as a hook in
1181 update_end. */
1183 static void
1184 x_update_end (struct frame *f)
1186 /* Mouse highlight may be displayed again. */
1187 MOUSE_HL_INFO (f)->mouse_face_defer = false;
1189 #ifdef USE_CAIRO
1190 if (FRAME_CR_SURFACE (f))
1192 cairo_t *cr = 0;
1193 block_input();
1194 #if defined (USE_GTK) && defined (HAVE_GTK3)
1195 if (FRAME_GTK_WIDGET (f))
1197 GdkWindow *w = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
1198 cr = gdk_cairo_create (w);
1200 else
1201 #endif
1203 cairo_surface_t *surface;
1204 int width = FRAME_PIXEL_WIDTH (f);
1205 int height = FRAME_PIXEL_HEIGHT (f);
1206 if (! FRAME_EXTERNAL_TOOL_BAR (f))
1207 height += FRAME_TOOL_BAR_HEIGHT (f);
1208 if (! FRAME_EXTERNAL_MENU_BAR (f))
1209 height += FRAME_MENU_BAR_HEIGHT (f);
1210 surface = cairo_xlib_surface_create (FRAME_X_DISPLAY (f),
1211 FRAME_X_WINDOW (f),
1212 FRAME_DISPLAY_INFO (f)->visual,
1213 width,
1214 height);
1215 cr = cairo_create (surface);
1216 cairo_surface_destroy (surface);
1219 cairo_set_source_surface (cr, FRAME_CR_SURFACE (f), 0, 0);
1220 cairo_paint (cr);
1221 cairo_destroy (cr);
1222 unblock_input ();
1224 #endif /* USE_CAIRO */
1226 #ifndef XFlush
1227 block_input ();
1228 XFlush (FRAME_X_DISPLAY (f));
1229 unblock_input ();
1230 #endif
1234 /* This function is called from various places in xdisp.c
1235 whenever a complete update has been performed. */
1237 static void
1238 XTframe_up_to_date (struct frame *f)
1240 if (FRAME_X_P (f))
1241 FRAME_MOUSE_UPDATE (f);
1245 /* Clear under internal border if any (GTK has its own version). */
1246 #ifndef USE_GTK
1247 void
1248 x_clear_under_internal_border (struct frame *f)
1250 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
1252 int border = FRAME_INTERNAL_BORDER_WIDTH (f);
1253 int width = FRAME_PIXEL_WIDTH (f);
1254 int height = FRAME_PIXEL_HEIGHT (f);
1255 int margin = FRAME_TOP_MARGIN_HEIGHT (f);
1257 block_input ();
1258 x_clear_area (f, 0, 0, border, height);
1259 x_clear_area (f, 0, margin, width, border);
1260 x_clear_area (f, width - border, 0, border, height);
1261 x_clear_area (f, 0, height - border, width, border);
1262 unblock_input ();
1265 #endif
1267 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
1268 arrow bitmaps, or clear the fringes if no bitmaps are required
1269 before DESIRED_ROW is made current. This function is called from
1270 update_window_line only if it is known that there are differences
1271 between bitmaps to be drawn between current row and DESIRED_ROW. */
1273 static void
1274 x_after_update_window_line (struct window *w, struct glyph_row *desired_row)
1276 eassert (w);
1278 if (!desired_row->mode_line_p && !w->pseudo_window_p)
1279 desired_row->redraw_fringe_bitmaps_p = true;
1281 #ifdef USE_X_TOOLKIT
1282 /* When a window has disappeared, make sure that no rest of
1283 full-width rows stays visible in the internal border. Could
1284 check here if updated window is the leftmost/rightmost window,
1285 but I guess it's not worth doing since vertically split windows
1286 are almost never used, internal border is rarely set, and the
1287 overhead is very small. */
1289 struct frame *f;
1290 int width, height;
1292 if (windows_or_buffers_changed
1293 && desired_row->full_width_p
1294 && (f = XFRAME (w->frame),
1295 width = FRAME_INTERNAL_BORDER_WIDTH (f),
1296 width != 0)
1297 && (height = desired_row->visible_height,
1298 height > 0))
1300 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
1302 block_input ();
1303 x_clear_area (f, 0, y, width, height);
1304 x_clear_area (f, FRAME_PIXEL_WIDTH (f) - width, y, width, height);
1305 unblock_input ();
1308 #endif
1311 static void
1312 x_draw_fringe_bitmap (struct window *w, struct glyph_row *row, struct draw_fringe_bitmap_params *p)
1314 struct frame *f = XFRAME (WINDOW_FRAME (w));
1315 Display *display = FRAME_X_DISPLAY (f);
1316 GC gc = f->output_data.x->normal_gc;
1317 struct face *face = p->face;
1319 /* Must clip because of partially visible lines. */
1320 x_clip_to_row (w, row, ANY_AREA, gc);
1322 if (p->bx >= 0 && !p->overlay_p)
1324 /* In case the same realized face is used for fringes and
1325 for something displayed in the text (e.g. face `region' on
1326 mono-displays, the fill style may have been changed to
1327 FillSolid in x_draw_glyph_string_background. */
1328 if (face->stipple)
1329 XSetFillStyle (display, face->gc, FillOpaqueStippled);
1330 else
1331 XSetForeground (display, face->gc, face->background);
1333 x_fill_rectangle (f, face->gc, p->bx, p->by, p->nx, p->ny);
1335 if (!face->stipple)
1336 XSetForeground (display, face->gc, face->foreground);
1339 #ifdef USE_CAIRO
1340 if (p->which && p->which < max_fringe_bmp)
1342 XGCValues gcv;
1344 XGetGCValues (display, gc, GCForeground | GCBackground, &gcv);
1345 XSetForeground (display, gc, (p->cursor_p
1346 ? (p->overlay_p ? face->background
1347 : f->output_data.x->cursor_pixel)
1348 : face->foreground));
1349 XSetBackground (display, gc, face->background);
1350 x_cr_draw_image (f, gc, fringe_bmp[p->which], 0, p->dh,
1351 p->wd, p->h, p->x, p->y, p->overlay_p);
1352 XSetForeground (display, gc, gcv.foreground);
1353 XSetBackground (display, gc, gcv.background);
1355 #else /* not USE_CAIRO */
1356 if (p->which)
1358 Window window = FRAME_X_WINDOW (f);
1359 char *bits;
1360 Pixmap pixmap, clipmask = (Pixmap) 0;
1361 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
1362 XGCValues gcv;
1364 if (p->wd > 8)
1365 bits = (char *) (p->bits + p->dh);
1366 else
1367 bits = (char *) p->bits + p->dh;
1369 /* Draw the bitmap. I believe these small pixmaps can be cached
1370 by the server. */
1371 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
1372 (p->cursor_p
1373 ? (p->overlay_p ? face->background
1374 : f->output_data.x->cursor_pixel)
1375 : face->foreground),
1376 face->background, depth);
1378 if (p->overlay_p)
1380 clipmask = XCreatePixmapFromBitmapData (display,
1381 FRAME_DISPLAY_INFO (f)->root_window,
1382 bits, p->wd, p->h,
1383 1, 0, 1);
1384 gcv.clip_mask = clipmask;
1385 gcv.clip_x_origin = p->x;
1386 gcv.clip_y_origin = p->y;
1387 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
1390 XCopyArea (display, pixmap, window, gc, 0, 0,
1391 p->wd, p->h, p->x, p->y);
1392 XFreePixmap (display, pixmap);
1394 if (p->overlay_p)
1396 gcv.clip_mask = (Pixmap) 0;
1397 XChangeGC (display, gc, GCClipMask, &gcv);
1398 XFreePixmap (display, clipmask);
1401 #endif /* not USE_CAIRO */
1403 x_reset_clip_rectangles (f, gc);
1406 /***********************************************************************
1407 Glyph display
1408 ***********************************************************************/
1412 static void x_set_glyph_string_clipping (struct glyph_string *);
1413 static void x_set_glyph_string_gc (struct glyph_string *);
1414 static void x_draw_glyph_string_foreground (struct glyph_string *);
1415 static void x_draw_composite_glyph_string_foreground (struct glyph_string *);
1416 static void x_draw_glyph_string_box (struct glyph_string *);
1417 static void x_draw_glyph_string (struct glyph_string *);
1418 static _Noreturn void x_delete_glyphs (struct frame *, int);
1419 static void x_compute_glyph_string_overhangs (struct glyph_string *);
1420 static void x_set_cursor_gc (struct glyph_string *);
1421 static void x_set_mode_line_face_gc (struct glyph_string *);
1422 static void x_set_mouse_face_gc (struct glyph_string *);
1423 static bool x_alloc_lighter_color (struct frame *, Display *, Colormap,
1424 unsigned long *, double, int);
1425 static void x_setup_relief_color (struct frame *, struct relief *,
1426 double, int, unsigned long);
1427 static void x_setup_relief_colors (struct glyph_string *);
1428 static void x_draw_image_glyph_string (struct glyph_string *);
1429 static void x_draw_image_relief (struct glyph_string *);
1430 static void x_draw_image_foreground (struct glyph_string *);
1431 static void x_draw_image_foreground_1 (struct glyph_string *, Pixmap);
1432 static void x_clear_glyph_string_rect (struct glyph_string *, int,
1433 int, int, int);
1434 static void x_draw_relief_rect (struct frame *, int, int, int, int,
1435 int, bool, bool, bool, bool, bool,
1436 XRectangle *);
1437 static void x_draw_box_rect (struct glyph_string *, int, int, int, int,
1438 int, bool, bool, XRectangle *);
1439 static void x_scroll_bar_clear (struct frame *);
1441 #ifdef GLYPH_DEBUG
1442 static void x_check_font (struct frame *, struct font *);
1443 #endif
1446 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
1447 face. */
1449 static void
1450 x_set_cursor_gc (struct glyph_string *s)
1452 if (s->font == FRAME_FONT (s->f)
1453 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
1454 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
1455 && !s->cmp)
1456 s->gc = s->f->output_data.x->cursor_gc;
1457 else
1459 /* Cursor on non-default face: must merge. */
1460 XGCValues xgcv;
1461 unsigned long mask;
1463 xgcv.background = s->f->output_data.x->cursor_pixel;
1464 xgcv.foreground = s->face->background;
1466 /* If the glyph would be invisible, try a different foreground. */
1467 if (xgcv.foreground == xgcv.background)
1468 xgcv.foreground = s->face->foreground;
1469 if (xgcv.foreground == xgcv.background)
1470 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1471 if (xgcv.foreground == xgcv.background)
1472 xgcv.foreground = s->face->foreground;
1474 /* Make sure the cursor is distinct from text in this face. */
1475 if (xgcv.background == s->face->background
1476 && xgcv.foreground == s->face->foreground)
1478 xgcv.background = s->face->foreground;
1479 xgcv.foreground = s->face->background;
1482 IF_DEBUG (x_check_font (s->f, s->font));
1483 xgcv.graphics_exposures = False;
1484 mask = GCForeground | GCBackground | GCGraphicsExposures;
1486 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1487 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1488 mask, &xgcv);
1489 else
1490 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1491 = XCreateGC (s->display, s->window, mask, &xgcv);
1493 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1498 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1500 static void
1501 x_set_mouse_face_gc (struct glyph_string *s)
1503 int face_id;
1504 struct face *face;
1506 /* What face has to be used last for the mouse face? */
1507 face_id = MOUSE_HL_INFO (s->f)->mouse_face_face_id;
1508 face = FACE_FROM_ID (s->f, face_id);
1509 if (face == NULL)
1510 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1512 if (s->first_glyph->type == CHAR_GLYPH)
1513 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1514 else
1515 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1516 s->face = FACE_FROM_ID (s->f, face_id);
1517 prepare_face_for_display (s->f, s->face);
1519 if (s->font == s->face->font)
1520 s->gc = s->face->gc;
1521 else
1523 /* Otherwise construct scratch_cursor_gc with values from FACE
1524 except for FONT. */
1525 XGCValues xgcv;
1526 unsigned long mask;
1528 xgcv.background = s->face->background;
1529 xgcv.foreground = s->face->foreground;
1530 xgcv.graphics_exposures = False;
1531 mask = GCForeground | GCBackground | GCGraphicsExposures;
1533 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1534 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1535 mask, &xgcv);
1536 else
1537 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1538 = XCreateGC (s->display, s->window, mask, &xgcv);
1540 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1543 eassert (s->gc != 0);
1547 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1548 Faces to use in the mode line have already been computed when the
1549 matrix was built, so there isn't much to do, here. */
1551 static void
1552 x_set_mode_line_face_gc (struct glyph_string *s)
1554 s->gc = s->face->gc;
1558 /* Set S->gc of glyph string S for drawing that glyph string. Set
1559 S->stippled_p to a non-zero value if the face of S has a stipple
1560 pattern. */
1562 static void
1563 x_set_glyph_string_gc (struct glyph_string *s)
1565 prepare_face_for_display (s->f, s->face);
1567 if (s->hl == DRAW_NORMAL_TEXT)
1569 s->gc = s->face->gc;
1570 s->stippled_p = s->face->stipple != 0;
1572 else if (s->hl == DRAW_INVERSE_VIDEO)
1574 x_set_mode_line_face_gc (s);
1575 s->stippled_p = s->face->stipple != 0;
1577 else if (s->hl == DRAW_CURSOR)
1579 x_set_cursor_gc (s);
1580 s->stippled_p = false;
1582 else if (s->hl == DRAW_MOUSE_FACE)
1584 x_set_mouse_face_gc (s);
1585 s->stippled_p = s->face->stipple != 0;
1587 else if (s->hl == DRAW_IMAGE_RAISED
1588 || s->hl == DRAW_IMAGE_SUNKEN)
1590 s->gc = s->face->gc;
1591 s->stippled_p = s->face->stipple != 0;
1593 else
1594 emacs_abort ();
1596 /* GC must have been set. */
1597 eassert (s->gc != 0);
1601 /* Set clipping for output of glyph string S. S may be part of a mode
1602 line or menu if we don't have X toolkit support. */
1604 static void
1605 x_set_glyph_string_clipping (struct glyph_string *s)
1607 XRectangle *r = s->clip;
1608 int n = get_glyph_string_clip_rects (s, r, 2);
1610 if (n > 0)
1611 x_set_clip_rectangles (s->f, s->gc, r, n);
1612 s->num_clips = n;
1616 /* Set SRC's clipping for output of glyph string DST. This is called
1617 when we are drawing DST's left_overhang or right_overhang only in
1618 the area of SRC. */
1620 static void
1621 x_set_glyph_string_clipping_exactly (struct glyph_string *src, struct glyph_string *dst)
1623 XRectangle r;
1625 r.x = src->x;
1626 r.width = src->width;
1627 r.y = src->y;
1628 r.height = src->height;
1629 dst->clip[0] = r;
1630 dst->num_clips = 1;
1631 x_set_clip_rectangles (dst->f, dst->gc, &r, 1);
1635 /* RIF:
1636 Compute left and right overhang of glyph string S. */
1638 static void
1639 x_compute_glyph_string_overhangs (struct glyph_string *s)
1641 if (s->cmp == NULL
1642 && (s->first_glyph->type == CHAR_GLYPH
1643 || s->first_glyph->type == COMPOSITE_GLYPH))
1645 struct font_metrics metrics;
1647 if (s->first_glyph->type == CHAR_GLYPH)
1649 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1650 struct font *font = s->font;
1651 int i;
1653 for (i = 0; i < s->nchars; i++)
1654 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1655 font->driver->text_extents (font, code, s->nchars, &metrics);
1657 else
1659 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1661 composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
1663 s->right_overhang = (metrics.rbearing > metrics.width
1664 ? metrics.rbearing - metrics.width : 0);
1665 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1667 else if (s->cmp)
1669 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1670 s->left_overhang = - s->cmp->lbearing;
1675 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1677 static void
1678 x_clear_glyph_string_rect (struct glyph_string *s, int x, int y, int w, int h)
1680 XGCValues xgcv;
1681 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1682 XSetForeground (s->display, s->gc, xgcv.background);
1683 x_fill_rectangle (s->f, s->gc, x, y, w, h);
1684 XSetForeground (s->display, s->gc, xgcv.foreground);
1688 /* Draw the background of glyph_string S. If S->background_filled_p
1689 is non-zero don't draw it. FORCE_P non-zero means draw the
1690 background even if it wouldn't be drawn normally. This is used
1691 when a string preceding S draws into the background of S, or S
1692 contains the first component of a composition. */
1694 static void
1695 x_draw_glyph_string_background (struct glyph_string *s, bool force_p)
1697 /* Nothing to do if background has already been drawn or if it
1698 shouldn't be drawn in the first place. */
1699 if (!s->background_filled_p)
1701 int box_line_width = max (s->face->box_line_width, 0);
1703 if (s->stippled_p)
1705 /* Fill background with a stipple pattern. */
1706 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1707 x_fill_rectangle (s->f, s->gc, s->x,
1708 s->y + box_line_width,
1709 s->background_width,
1710 s->height - 2 * box_line_width);
1711 XSetFillStyle (s->display, s->gc, FillSolid);
1712 s->background_filled_p = true;
1714 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1715 /* When xdisp.c ignores FONT_HEIGHT, we cannot trust
1716 font dimensions, since the actual glyphs might be
1717 much smaller. So in that case we always clear the
1718 rectangle with background color. */
1719 || FONT_TOO_HIGH (s->font)
1720 || s->font_not_found_p
1721 || s->extends_to_end_of_line_p
1722 || force_p)
1724 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1725 s->background_width,
1726 s->height - 2 * box_line_width);
1727 s->background_filled_p = true;
1733 /* Draw the foreground of glyph string S. */
1735 static void
1736 x_draw_glyph_string_foreground (struct glyph_string *s)
1738 int i, x;
1740 /* If first glyph of S has a left box line, start drawing the text
1741 of S to the right of that box line. */
1742 if (s->face->box != FACE_NO_BOX
1743 && s->first_glyph->left_box_line_p)
1744 x = s->x + eabs (s->face->box_line_width);
1745 else
1746 x = s->x;
1748 /* Draw characters of S as rectangles if S's font could not be
1749 loaded. */
1750 if (s->font_not_found_p)
1752 for (i = 0; i < s->nchars; ++i)
1754 struct glyph *g = s->first_glyph + i;
1755 x_draw_rectangle (s->f,
1756 s->gc, x, s->y, g->pixel_width - 1,
1757 s->height - 1);
1758 x += g->pixel_width;
1761 else
1763 struct font *font = s->font;
1764 int boff = font->baseline_offset;
1765 int y;
1767 if (font->vertical_centering)
1768 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1770 y = s->ybase - boff;
1771 if (s->for_overlaps
1772 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1773 font->driver->draw (s, 0, s->nchars, x, y, false);
1774 else
1775 font->driver->draw (s, 0, s->nchars, x, y, true);
1776 if (s->face->overstrike)
1777 font->driver->draw (s, 0, s->nchars, x + 1, y, false);
1781 /* Draw the foreground of composite glyph string S. */
1783 static void
1784 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1786 int i, j, x;
1787 struct font *font = s->font;
1789 /* If first glyph of S has a left box line, start drawing the text
1790 of S to the right of that box line. */
1791 if (s->face && s->face->box != FACE_NO_BOX
1792 && s->first_glyph->left_box_line_p)
1793 x = s->x + eabs (s->face->box_line_width);
1794 else
1795 x = s->x;
1797 /* S is a glyph string for a composition. S->cmp_from is the index
1798 of the first character drawn for glyphs of this composition.
1799 S->cmp_from == 0 means we are drawing the very first character of
1800 this composition. */
1802 /* Draw a rectangle for the composition if the font for the very
1803 first character of the composition could not be loaded. */
1804 if (s->font_not_found_p)
1806 if (s->cmp_from == 0)
1807 x_draw_rectangle (s->f, s->gc, x, s->y,
1808 s->width - 1, s->height - 1);
1810 else if (! s->first_glyph->u.cmp.automatic)
1812 int y = s->ybase;
1814 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1815 /* TAB in a composition means display glyphs with padding
1816 space on the left or right. */
1817 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1819 int xx = x + s->cmp->offsets[j * 2];
1820 int yy = y - s->cmp->offsets[j * 2 + 1];
1822 font->driver->draw (s, j, j + 1, xx, yy, false);
1823 if (s->face->overstrike)
1824 font->driver->draw (s, j, j + 1, xx + 1, yy, false);
1827 else
1829 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1830 Lisp_Object glyph;
1831 int y = s->ybase;
1832 int width = 0;
1834 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1836 glyph = LGSTRING_GLYPH (gstring, i);
1837 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1838 width += LGLYPH_WIDTH (glyph);
1839 else
1841 int xoff, yoff, wadjust;
1843 if (j < i)
1845 font->driver->draw (s, j, i, x, y, false);
1846 if (s->face->overstrike)
1847 font->driver->draw (s, j, i, x + 1, y, false);
1848 x += width;
1850 xoff = LGLYPH_XOFF (glyph);
1851 yoff = LGLYPH_YOFF (glyph);
1852 wadjust = LGLYPH_WADJUST (glyph);
1853 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, false);
1854 if (s->face->overstrike)
1855 font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff,
1856 false);
1857 x += wadjust;
1858 j = i + 1;
1859 width = 0;
1862 if (j < i)
1864 font->driver->draw (s, j, i, x, y, false);
1865 if (s->face->overstrike)
1866 font->driver->draw (s, j, i, x + 1, y, false);
1872 /* Draw the foreground of glyph string S for glyphless characters. */
1874 static void
1875 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1877 struct glyph *glyph = s->first_glyph;
1878 XChar2b char2b[8];
1879 int x, i, j;
1881 /* If first glyph of S has a left box line, start drawing the text
1882 of S to the right of that box line. */
1883 if (s->face && s->face->box != FACE_NO_BOX
1884 && s->first_glyph->left_box_line_p)
1885 x = s->x + eabs (s->face->box_line_width);
1886 else
1887 x = s->x;
1889 s->char2b = char2b;
1891 for (i = 0; i < s->nchars; i++, glyph++)
1893 char buf[7], *str = NULL;
1894 int len = glyph->u.glyphless.len;
1896 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1898 if (len > 0
1899 && CHAR_TABLE_P (Vglyphless_char_display)
1900 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1901 >= 1))
1903 Lisp_Object acronym
1904 = (! glyph->u.glyphless.for_no_font
1905 ? CHAR_TABLE_REF (Vglyphless_char_display,
1906 glyph->u.glyphless.ch)
1907 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
1908 if (STRINGP (acronym))
1909 str = SSDATA (acronym);
1912 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
1914 sprintf (buf, "%0*X",
1915 glyph->u.glyphless.ch < 0x10000 ? 4 : 6,
1916 glyph->u.glyphless.ch + 0u);
1917 str = buf;
1920 if (str)
1922 int upper_len = (len + 1) / 2;
1923 unsigned code;
1925 /* It is assured that all LEN characters in STR is ASCII. */
1926 for (j = 0; j < len; j++)
1928 code = s->font->driver->encode_char (s->font, str[j]);
1929 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
1931 s->font->driver->draw (s, 0, upper_len,
1932 x + glyph->slice.glyphless.upper_xoff,
1933 s->ybase + glyph->slice.glyphless.upper_yoff,
1934 false);
1935 s->font->driver->draw (s, upper_len, len,
1936 x + glyph->slice.glyphless.lower_xoff,
1937 s->ybase + glyph->slice.glyphless.lower_yoff,
1938 false);
1940 if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
1941 x_draw_rectangle (s->f, s->gc,
1942 x, s->ybase - glyph->ascent,
1943 glyph->pixel_width - 1,
1944 glyph->ascent + glyph->descent - 1);
1945 x += glyph->pixel_width;
1949 #ifdef USE_X_TOOLKIT
1951 #ifdef USE_LUCID
1953 /* Return the frame on which widget WIDGET is used.. Abort if frame
1954 cannot be determined. */
1956 static struct frame *
1957 x_frame_of_widget (Widget widget)
1959 struct x_display_info *dpyinfo;
1960 Lisp_Object tail, frame;
1961 struct frame *f;
1963 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1965 /* Find the top-level shell of the widget. Note that this function
1966 can be called when the widget is not yet realized, so XtWindow
1967 (widget) == 0. That's the reason we can't simply use
1968 x_any_window_to_frame. */
1969 while (!XtIsTopLevelShell (widget))
1970 widget = XtParent (widget);
1972 /* Look for a frame with that top-level widget. Allocate the color
1973 on that frame to get the right gamma correction value. */
1974 FOR_EACH_FRAME (tail, frame)
1976 f = XFRAME (frame);
1977 if (FRAME_X_P (f)
1978 && f->output_data.nothing != 1
1979 && FRAME_DISPLAY_INFO (f) == dpyinfo
1980 && f->output_data.x->widget == widget)
1981 return f;
1983 emacs_abort ();
1986 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1987 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1988 If this produces the same color as PIXEL, try a color where all RGB
1989 values have DELTA added. Return the allocated color in *PIXEL.
1990 DISPLAY is the X display, CMAP is the colormap to operate on.
1991 Value is true if successful. */
1993 bool
1994 x_alloc_lighter_color_for_widget (Widget widget, Display *display, Colormap cmap,
1995 unsigned long *pixel, double factor, int delta)
1997 struct frame *f = x_frame_of_widget (widget);
1998 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
2001 #endif /* USE_LUCID */
2004 /* Structure specifying which arguments should be passed by Xt to
2005 cvt_string_to_pixel. We want the widget's screen and colormap. */
2007 static XtConvertArgRec cvt_string_to_pixel_args[] =
2009 {XtWidgetBaseOffset, (XtPointer) offsetof (WidgetRec, core.screen),
2010 sizeof (Screen *)},
2011 {XtWidgetBaseOffset, (XtPointer) offsetof (WidgetRec, core.colormap),
2012 sizeof (Colormap)}
2013 };
2016 /* The address of this variable is returned by
2017 cvt_string_to_pixel. */
2019 static Pixel cvt_string_to_pixel_value;
2022 /* Convert a color name to a pixel color.
2024 DPY is the display we are working on.
2026 ARGS is an array of *NARGS XrmValue structures holding additional
2027 information about the widget for which the conversion takes place.
2028 The contents of this array are determined by the specification
2029 in cvt_string_to_pixel_args.
2031 FROM is a pointer to an XrmValue which points to the color name to
2032 convert. TO is an XrmValue in which to return the pixel color.
2034 CLOSURE_RET is a pointer to user-data, in which we record if
2035 we allocated the color or not.
2037 Value is True if successful, False otherwise. */
2039 static Boolean
2040 cvt_string_to_pixel (Display *dpy, XrmValue *args, Cardinal *nargs,
2041 XrmValue *from, XrmValue *to,
2042 XtPointer *closure_ret)
2044 Screen *screen;
2045 Colormap cmap;
2046 Pixel pixel;
2047 String color_name;
2048 XColor color;
2050 if (*nargs != 2)
2052 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
2053 "wrongParameters", "cvt_string_to_pixel",
2054 "XtToolkitError",
2055 "Screen and colormap args required", NULL, NULL);
2056 return False;
2059 screen = *(Screen **) args[0].addr;
2060 cmap = *(Colormap *) args[1].addr;
2061 color_name = (String) from->addr;
2063 if (strcmp (color_name, XtDefaultBackground) == 0)
2065 *closure_ret = (XtPointer) False;
2066 pixel = WhitePixelOfScreen (screen);
2068 else if (strcmp (color_name, XtDefaultForeground) == 0)
2070 *closure_ret = (XtPointer) False;
2071 pixel = BlackPixelOfScreen (screen);
2073 else if (XParseColor (dpy, cmap, color_name, &color)
2074 && x_alloc_nearest_color_1 (dpy, cmap, &color))
2076 pixel = color.pixel;
2077 *closure_ret = (XtPointer) True;
2079 else
2081 String params[1];
2082 Cardinal nparams = 1;
2084 params[0] = color_name;
2085 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
2086 "badValue", "cvt_string_to_pixel",
2087 "XtToolkitError", "Invalid color '%s'",
2088 params, &nparams);
2089 return False;
2092 if (to->addr != NULL)
2094 if (to->size < sizeof (Pixel))
2096 to->size = sizeof (Pixel);
2097 return False;
2100 *(Pixel *) to->addr = pixel;
2102 else
2104 cvt_string_to_pixel_value = pixel;
2105 to->addr = (XtPointer) &cvt_string_to_pixel_value;
2108 to->size = sizeof (Pixel);
2109 return True;
2113 /* Free a pixel color which was previously allocated via
2114 cvt_string_to_pixel. This is registered as the destructor
2115 for this type of resource via XtSetTypeConverter.
2117 APP is the application context in which we work.
2119 TO is a pointer to an XrmValue holding the color to free.
2120 CLOSURE is the value we stored in CLOSURE_RET for this color
2121 in cvt_string_to_pixel.
2123 ARGS and NARGS are like for cvt_string_to_pixel. */
2125 static void
2126 cvt_pixel_dtor (XtAppContext app, XrmValuePtr to, XtPointer closure, XrmValuePtr args,
2127 Cardinal *nargs)
2129 if (*nargs != 2)
2131 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
2132 "XtToolkitError",
2133 "Screen and colormap arguments required",
2134 NULL, NULL);
2136 else if (closure != NULL)
2138 /* We did allocate the pixel, so free it. */
2139 Screen *screen = *(Screen **) args[0].addr;
2140 Colormap cmap = *(Colormap *) args[1].addr;
2141 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
2142 (Pixel *) to->addr, 1);
2147 #endif /* USE_X_TOOLKIT */
2150 /* Value is an array of XColor structures for the contents of the
2151 color map of display DPY. Set *NCELLS to the size of the array.
2152 Note that this probably shouldn't be called for large color maps,
2153 say a 24-bit TrueColor map. */
2155 static const XColor *
2156 x_color_cells (Display *dpy, int *ncells)
2158 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
2160 if (dpyinfo->color_cells == NULL)
2162 Screen *screen = dpyinfo->screen;
2163 int ncolor_cells = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
2164 int i;
2166 dpyinfo->color_cells = xnmalloc (ncolor_cells,
2167 sizeof *dpyinfo->color_cells);
2168 dpyinfo->ncolor_cells = ncolor_cells;
2170 for (i = 0; i < ncolor_cells; ++i)
2171 dpyinfo->color_cells[i].pixel = i;
2173 XQueryColors (dpy, dpyinfo->cmap,
2174 dpyinfo->color_cells, ncolor_cells);
2177 *ncells = dpyinfo->ncolor_cells;
2178 return dpyinfo->color_cells;
2182 /* On frame F, translate pixel colors to RGB values for the NCOLORS
2183 colors in COLORS. Use cached information, if available. */
2185 void
2186 x_query_colors (struct frame *f, XColor *colors, int ncolors)
2188 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2190 if (dpyinfo->red_bits > 0)
2192 /* For TrueColor displays, we can decompose the RGB value
2193 directly. */
2194 int i;
2195 unsigned int rmult, gmult, bmult;
2196 unsigned int rmask, gmask, bmask;
2198 rmask = (1 << dpyinfo->red_bits) - 1;
2199 gmask = (1 << dpyinfo->green_bits) - 1;
2200 bmask = (1 << dpyinfo->blue_bits) - 1;
2201 /* If we're widening, for example, 8 bits in the pixel value to
2202 16 bits for the separate-color representation, we want to
2203 extrapolate the lower bits based on those bits available --
2204 in other words, we'd like 0xff to become 0xffff instead of
2205 the 0xff00 we'd get by just zero-filling the lower bits.
2207 We generate a 32-bit scaled-up value and shift it, in case
2208 the bit count doesn't divide 16 evenly (e.g., when dealing
2209 with a 3-3-2 bit RGB display), to get more of the lower bits
2210 correct.
2212 Should we cache the multipliers in dpyinfo? Maybe
2213 special-case the 8-8-8 common case? */
2214 rmult = 0xffffffff / rmask;
2215 gmult = 0xffffffff / gmask;
2216 bmult = 0xffffffff / bmask;
2218 for (i = 0; i < ncolors; ++i)
2220 unsigned int r, g, b;
2221 unsigned long pixel = colors[i].pixel;
2223 r = (pixel >> dpyinfo->red_offset) & rmask;
2224 g = (pixel >> dpyinfo->green_offset) & gmask;
2225 b = (pixel >> dpyinfo->blue_offset) & bmask;
2227 colors[i].red = (r * rmult) >> 16;
2228 colors[i].green = (g * gmult) >> 16;
2229 colors[i].blue = (b * bmult) >> 16;
2231 return;
2234 if (dpyinfo->color_cells)
2236 int i;
2237 for (i = 0; i < ncolors; ++i)
2239 unsigned long pixel = colors[i].pixel;
2240 eassert (pixel < dpyinfo->ncolor_cells);
2241 eassert (dpyinfo->color_cells[pixel].pixel == pixel);
2242 colors[i] = dpyinfo->color_cells[pixel];
2244 return;
2247 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
2251 /* On frame F, translate pixel color to RGB values for the color in
2252 COLOR. Use cached information, if available. */
2254 void
2255 x_query_color (struct frame *f, XColor *color)
2257 x_query_colors (f, color, 1);
2261 /* On frame F, translate the color name to RGB values. Use cached
2262 information, if possible.
2264 Note that there is currently no way to clean old entries out of the
2265 cache. However, it is limited to names in the server's database,
2266 and names we've actually looked up; list-colors-display is probably
2267 the most color-intensive case we're likely to hit. */
2269 Status x_parse_color (struct frame *f, const char *color_name,
2270 XColor *color)
2272 Display *dpy = FRAME_X_DISPLAY (f);
2273 Colormap cmap = FRAME_X_COLORMAP (f);
2274 struct color_name_cache_entry *cache_entry;
2276 if (color_name[0] == '#')
2278 /* The hex form is parsed directly by XParseColor without
2279 talking to the X server. No need for caching. */
2280 return XParseColor (dpy, cmap, color_name, color);
2283 for (cache_entry = FRAME_DISPLAY_INFO (f)->color_names; cache_entry;
2284 cache_entry = cache_entry->next)
2286 if (!xstrcasecmp(cache_entry->name, color_name))
2288 *color = cache_entry->rgb;
2289 return 1;
2293 if (XParseColor (dpy, cmap, color_name, color) == 0)
2294 /* No caching of negative results, currently. */
2295 return 0;
2297 cache_entry = xzalloc (sizeof *cache_entry);
2298 cache_entry->rgb = *color;
2299 cache_entry->name = xstrdup (color_name);
2300 cache_entry->next = FRAME_DISPLAY_INFO (f)->color_names;
2301 FRAME_DISPLAY_INFO (f)->color_names = cache_entry;
2302 return 1;
2306 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
2307 exact match can't be allocated, try the nearest color available.
2308 Value is true if successful. Set *COLOR to the color
2309 allocated. */
2311 static bool
2312 x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color)
2314 bool rc;
2316 rc = XAllocColor (dpy, cmap, color) != 0;
2317 if (rc == 0)
2319 /* If we got to this point, the colormap is full, so we're going
2320 to try to get the next closest color. The algorithm used is
2321 a least-squares matching, which is what X uses for closest
2322 color matching with StaticColor visuals. */
2323 int nearest, i;
2324 int max_color_delta = 255;
2325 int max_delta = 3 * max_color_delta;
2326 int nearest_delta = max_delta + 1;
2327 int ncells;
2328 const XColor *cells = x_color_cells (dpy, &ncells);
2330 for (nearest = i = 0; i < ncells; ++i)
2332 int dred = (color->red >> 8) - (cells[i].red >> 8);
2333 int dgreen = (color->green >> 8) - (cells[i].green >> 8);
2334 int dblue = (color->blue >> 8) - (cells[i].blue >> 8);
2335 int delta = dred * dred + dgreen * dgreen + dblue * dblue;
2337 if (delta < nearest_delta)
2339 nearest = i;
2340 nearest_delta = delta;
2344 color->red = cells[nearest].red;
2345 color->green = cells[nearest].green;
2346 color->blue = cells[nearest].blue;
2347 rc = XAllocColor (dpy, cmap, color) != 0;
2349 else
2351 /* If allocation succeeded, and the allocated pixel color is not
2352 equal to a cached pixel color recorded earlier, there was a
2353 change in the colormap, so clear the color cache. */
2354 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
2355 XColor *cached_color;
2357 if (dpyinfo->color_cells
2358 && (cached_color = &dpyinfo->color_cells[color->pixel],
2359 (cached_color->red != color->red
2360 || cached_color->blue != color->blue
2361 || cached_color->green != color->green)))
2363 xfree (dpyinfo->color_cells);
2364 dpyinfo->color_cells = NULL;
2365 dpyinfo->ncolor_cells = 0;
2369 #ifdef DEBUG_X_COLORS
2370 if (rc)
2371 register_color (color->pixel);
2372 #endif /* DEBUG_X_COLORS */
2374 return rc;
2378 /* Allocate the color COLOR->pixel on frame F, colormap CMAP, after
2379 gamma correction. If an exact match can't be allocated, try the
2380 nearest color available. Value is true if successful. Set *COLOR
2381 to the color allocated. */
2383 bool
2384 x_alloc_nearest_color (struct frame *f, Colormap cmap, XColor *color)
2386 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2388 gamma_correct (f, color);
2390 if (dpyinfo->red_bits > 0)
2392 color->pixel = x_make_truecolor_pixel (dpyinfo,
2393 color->red,
2394 color->green,
2395 color->blue);
2396 return true;
2399 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
2403 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
2404 It's necessary to do this instead of just using PIXEL directly to
2405 get color reference counts right. */
2407 unsigned long
2408 x_copy_color (struct frame *f, unsigned long pixel)
2410 XColor color;
2412 /* If display has an immutable color map, freeing colors is not
2413 necessary and some servers don't allow it. Since we won't free a
2414 color once we've allocated it, we don't need to re-allocate it to
2415 maintain the server's reference count. */
2416 if (!x_mutable_colormap (FRAME_X_VISUAL (f)))
2417 return pixel;
2419 color.pixel = pixel;
2420 block_input ();
2421 /* The color could still be found in the color_cells array. */
2422 x_query_color (f, &color);
2423 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
2424 unblock_input ();
2425 #ifdef DEBUG_X_COLORS
2426 register_color (pixel);
2427 #endif
2428 return color.pixel;
2432 /* Brightness beyond which a color won't have its highlight brightness
2433 boosted.
2435 Nominally, highlight colors for `3d' faces are calculated by
2436 brightening an object's color by a constant scale factor, but this
2437 doesn't yield good results for dark colors, so for colors who's
2438 brightness is less than this value (on a scale of 0-65535) have an
2439 use an additional additive factor.
2441 The value here is set so that the default menu-bar/mode-line color
2442 (grey75) will not have its highlights changed at all. */
2443 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
2446 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
2447 or DELTA. Try a color with RGB values multiplied by FACTOR first.
2448 If this produces the same color as PIXEL, try a color where all RGB
2449 values have DELTA added. Return the allocated color in *PIXEL.
2450 DISPLAY is the X display, CMAP is the colormap to operate on.
2451 Value is non-zero if successful. */
2453 static bool
2454 x_alloc_lighter_color (struct frame *f, Display *display, Colormap cmap,
2455 unsigned long *pixel, double factor, int delta)
2457 XColor color, new;
2458 long bright;
2459 bool success_p;
2461 /* Get RGB color values. */
2462 color.pixel = *pixel;
2463 x_query_color (f, &color);
2465 /* Change RGB values by specified FACTOR. Avoid overflow! */
2466 eassert (factor >= 0);
2467 new.red = min (0xffff, factor * color.red);
2468 new.green = min (0xffff, factor * color.green);
2469 new.blue = min (0xffff, factor * color.blue);
2471 /* Calculate brightness of COLOR. */
2472 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
2474 /* We only boost colors that are darker than
2475 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
2476 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
2477 /* Make an additive adjustment to NEW, because it's dark enough so
2478 that scaling by FACTOR alone isn't enough. */
2480 /* How far below the limit this color is (0 - 1, 1 being darker). */
2481 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
2482 /* The additive adjustment. */
2483 int min_delta = delta * dimness * factor / 2;
2485 if (factor < 1)
2487 new.red = max (0, new.red - min_delta);
2488 new.green = max (0, new.green - min_delta);
2489 new.blue = max (0, new.blue - min_delta);
2491 else
2493 new.red = min (0xffff, min_delta + new.red);
2494 new.green = min (0xffff, min_delta + new.green);
2495 new.blue = min (0xffff, min_delta + new.blue);
2499 /* Try to allocate the color. */
2500 success_p = x_alloc_nearest_color (f, cmap, &new);
2501 if (success_p)
2503 if (new.pixel == *pixel)
2505 /* If we end up with the same color as before, try adding
2506 delta to the RGB values. */
2507 x_free_colors (f, &new.pixel, 1);
2509 new.red = min (0xffff, delta + color.red);
2510 new.green = min (0xffff, delta + color.green);
2511 new.blue = min (0xffff, delta + color.blue);
2512 success_p = x_alloc_nearest_color (f, cmap, &new);
2514 else
2515 success_p = true;
2516 *pixel = new.pixel;
2519 return success_p;
2523 /* Set up the foreground color for drawing relief lines of glyph
2524 string S. RELIEF is a pointer to a struct relief containing the GC
2525 with which lines will be drawn. Use a color that is FACTOR or
2526 DELTA lighter or darker than the relief's background which is found
2527 in S->f->output_data.x->relief_background. If such a color cannot
2528 be allocated, use DEFAULT_PIXEL, instead. */
2530 static void
2531 x_setup_relief_color (struct frame *f, struct relief *relief, double factor,
2532 int delta, unsigned long default_pixel)
2534 XGCValues xgcv;
2535 struct x_output *di = f->output_data.x;
2536 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
2537 unsigned long pixel;
2538 unsigned long background = di->relief_background;
2539 Colormap cmap = FRAME_X_COLORMAP (f);
2540 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2541 Display *dpy = FRAME_X_DISPLAY (f);
2543 xgcv.graphics_exposures = False;
2544 xgcv.line_width = 1;
2546 /* Free previously allocated color. The color cell will be reused
2547 when it has been freed as many times as it was allocated, so this
2548 doesn't affect faces using the same colors. */
2549 if (relief->gc && relief->pixel != -1)
2551 x_free_colors (f, &relief->pixel, 1);
2552 relief->pixel = -1;
2555 /* Allocate new color. */
2556 xgcv.foreground = default_pixel;
2557 pixel = background;
2558 if (dpyinfo->n_planes != 1
2559 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
2560 xgcv.foreground = relief->pixel = pixel;
2562 if (relief->gc == 0)
2564 xgcv.stipple = dpyinfo->gray;
2565 mask |= GCStipple;
2566 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
2568 else
2569 XChangeGC (dpy, relief->gc, mask, &xgcv);
2573 /* Set up colors for the relief lines around glyph string S. */
2575 static void
2576 x_setup_relief_colors (struct glyph_string *s)
2578 struct x_output *di = s->f->output_data.x;
2579 unsigned long color;
2581 if (s->face->use_box_color_for_shadows_p)
2582 color = s->face->box_color;
2583 else if (s->first_glyph->type == IMAGE_GLYPH
2584 && s->img->pixmap
2585 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2586 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2587 else
2589 XGCValues xgcv;
2591 /* Get the background color of the face. */
2592 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2593 color = xgcv.background;
2596 if (di->white_relief.gc == 0
2597 || color != di->relief_background)
2599 di->relief_background = color;
2600 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2601 WHITE_PIX_DEFAULT (s->f));
2602 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2603 BLACK_PIX_DEFAULT (s->f));
2608 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2609 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2610 to draw, it must be >= 0. RAISED_P means draw a raised
2611 relief. LEFT_P means draw a relief on the left side of
2612 the rectangle. RIGHT_P means draw a relief on the right
2613 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2614 when drawing. */
2616 static void
2617 x_draw_relief_rect (struct frame *f,
2618 int left_x, int top_y, int right_x, int bottom_y,
2619 int width, bool raised_p, bool top_p, bool bot_p,
2620 bool left_p, bool right_p,
2621 XRectangle *clip_rect)
2623 #ifdef USE_CAIRO
2624 GC top_left_gc, bottom_right_gc;
2625 int corners = 0;
2627 if (raised_p)
2629 top_left_gc = f->output_data.x->white_relief.gc;
2630 bottom_right_gc = f->output_data.x->black_relief.gc;
2632 else
2634 top_left_gc = f->output_data.x->black_relief.gc;
2635 bottom_right_gc = f->output_data.x->white_relief.gc;
2638 x_set_clip_rectangles (f, top_left_gc, clip_rect, 1);
2639 x_set_clip_rectangles (f, bottom_right_gc, clip_rect, 1);
2641 if (left_p)
2643 x_fill_rectangle (f, top_left_gc, left_x, top_y,
2644 width, bottom_y + 1 - top_y);
2645 if (top_p)
2646 corners |= 1 << CORNER_TOP_LEFT;
2647 if (bot_p)
2648 corners |= 1 << CORNER_BOTTOM_LEFT;
2650 if (right_p)
2652 x_fill_rectangle (f, bottom_right_gc, right_x + 1 - width, top_y,
2653 width, bottom_y + 1 - top_y);
2654 if (top_p)
2655 corners |= 1 << CORNER_TOP_RIGHT;
2656 if (bot_p)
2657 corners |= 1 << CORNER_BOTTOM_RIGHT;
2659 if (top_p)
2661 if (!right_p)
2662 x_fill_rectangle (f, top_left_gc, left_x, top_y,
2663 right_x + 1 - left_x, width);
2664 else
2665 x_fill_trapezoid_for_relief (f, top_left_gc, left_x, top_y,
2666 right_x + 1 - left_x, width, 1);
2668 if (bot_p)
2670 if (!left_p)
2671 x_fill_rectangle (f, bottom_right_gc, left_x, bottom_y + 1 - width,
2672 right_x + 1 - left_x, width);
2673 else
2674 x_fill_trapezoid_for_relief (f, bottom_right_gc,
2675 left_x, bottom_y + 1 - width,
2676 right_x + 1 - left_x, width, 0);
2678 if (left_p && width != 1)
2679 x_fill_rectangle (f, bottom_right_gc, left_x, top_y,
2680 1, bottom_y + 1 - top_y);
2681 if (top_p && width != 1)
2682 x_fill_rectangle (f, bottom_right_gc, left_x, top_y,
2683 right_x + 1 - left_x, 1);
2684 if (corners)
2686 XSetBackground (FRAME_X_DISPLAY (f), top_left_gc,
2687 FRAME_BACKGROUND_PIXEL (f));
2688 x_erase_corners_for_relief (f, top_left_gc, left_x, top_y,
2689 right_x - left_x + 1, bottom_y - top_y + 1,
2690 6, 1, corners);
2693 x_reset_clip_rectangles (f, top_left_gc);
2694 x_reset_clip_rectangles (f, bottom_right_gc);
2695 #else
2696 Display *dpy = FRAME_X_DISPLAY (f);
2697 Window window = FRAME_X_WINDOW (f);
2698 int i;
2699 GC gc;
2701 if (raised_p)
2702 gc = f->output_data.x->white_relief.gc;
2703 else
2704 gc = f->output_data.x->black_relief.gc;
2705 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2707 /* This code is more complicated than it has to be, because of two
2708 minor hacks to make the boxes look nicer: (i) if width > 1, draw
2709 the outermost line using the black relief. (ii) Omit the four
2710 corner pixels. */
2712 /* Top. */
2713 if (top_p)
2715 if (width == 1)
2716 XDrawLine (dpy, window, gc,
2717 left_x + left_p, top_y,
2718 right_x + !right_p, top_y);
2720 for (i = 1; i < width; ++i)
2721 XDrawLine (dpy, window, gc,
2722 left_x + i * left_p, top_y + i,
2723 right_x + 1 - i * right_p, top_y + i);
2726 /* Left. */
2727 if (left_p)
2729 if (width == 1)
2730 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
2732 XClearArea (dpy, window, left_x, top_y, 1, 1, False);
2733 XClearArea (dpy, window, left_x, bottom_y, 1, 1, False);
2735 for (i = (width > 1 ? 1 : 0); i < width; ++i)
2736 XDrawLine (dpy, window, gc,
2737 left_x + i, top_y + (i + 1) * top_p,
2738 left_x + i, bottom_y + 1 - (i + 1) * bot_p);
2741 XSetClipMask (dpy, gc, None);
2742 if (raised_p)
2743 gc = f->output_data.x->black_relief.gc;
2744 else
2745 gc = f->output_data.x->white_relief.gc;
2746 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2748 if (width > 1)
2750 /* Outermost top line. */
2751 if (top_p)
2752 XDrawLine (dpy, window, gc,
2753 left_x + left_p, top_y,
2754 right_x + !right_p, top_y);
2756 /* Outermost left line. */
2757 if (left_p)
2758 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
2761 /* Bottom. */
2762 if (bot_p)
2764 XDrawLine (dpy, window, gc,
2765 left_x + left_p, bottom_y,
2766 right_x + !right_p, bottom_y);
2767 for (i = 1; i < width; ++i)
2768 XDrawLine (dpy, window, gc,
2769 left_x + i * left_p, bottom_y - i,
2770 right_x + 1 - i * right_p, bottom_y - i);
2773 /* Right. */
2774 if (right_p)
2776 XClearArea (dpy, window, right_x, top_y, 1, 1, False);
2777 XClearArea (dpy, window, right_x, bottom_y, 1, 1, False);
2778 for (i = 0; i < width; ++i)
2779 XDrawLine (dpy, window, gc,
2780 right_x - i, top_y + (i + 1) * top_p,
2781 right_x - i, bottom_y + 1 - (i + 1) * bot_p);
2784 x_reset_clip_rectangles (f, gc);
2786 #endif
2790 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2791 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2792 draw, it must be >= 0. LEFT_P means draw a line on the
2793 left side of the rectangle. RIGHT_P means draw a line
2794 on the right side of the rectangle. CLIP_RECT is the clipping
2795 rectangle to use when drawing. */
2797 static void
2798 x_draw_box_rect (struct glyph_string *s,
2799 int left_x, int top_y, int right_x, int bottom_y, int width,
2800 bool left_p, bool right_p, XRectangle *clip_rect)
2802 XGCValues xgcv;
2804 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2805 XSetForeground (s->display, s->gc, s->face->box_color);
2806 x_set_clip_rectangles (s->f, s->gc, clip_rect, 1);
2808 /* Top. */
2809 x_fill_rectangle (s->f, s->gc,
2810 left_x, top_y, right_x - left_x + 1, width);
2812 /* Left. */
2813 if (left_p)
2814 x_fill_rectangle (s->f, s->gc,
2815 left_x, top_y, width, bottom_y - top_y + 1);
2817 /* Bottom. */
2818 x_fill_rectangle (s->f, s->gc,
2819 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2821 /* Right. */
2822 if (right_p)
2823 x_fill_rectangle (s->f, s->gc,
2824 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2826 XSetForeground (s->display, s->gc, xgcv.foreground);
2827 x_reset_clip_rectangles (s->f, s->gc);
2831 /* Draw a box around glyph string S. */
2833 static void
2834 x_draw_glyph_string_box (struct glyph_string *s)
2836 int width, left_x, right_x, top_y, bottom_y, last_x;
2837 bool raised_p, left_p, right_p;
2838 struct glyph *last_glyph;
2839 XRectangle clip_rect;
2841 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2842 ? WINDOW_RIGHT_EDGE_X (s->w)
2843 : window_box_right (s->w, s->area));
2845 /* The glyph that may have a right box line. */
2846 last_glyph = (s->cmp || s->img
2847 ? s->first_glyph
2848 : s->first_glyph + s->nchars - 1);
2850 width = eabs (s->face->box_line_width);
2851 raised_p = s->face->box == FACE_RAISED_BOX;
2852 left_x = s->x;
2853 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2854 ? last_x - 1
2855 : min (last_x, s->x + s->background_width) - 1);
2856 top_y = s->y;
2857 bottom_y = top_y + s->height - 1;
2859 left_p = (s->first_glyph->left_box_line_p
2860 || (s->hl == DRAW_MOUSE_FACE
2861 && (s->prev == NULL
2862 || s->prev->hl != s->hl)));
2863 right_p = (last_glyph->right_box_line_p
2864 || (s->hl == DRAW_MOUSE_FACE
2865 && (s->next == NULL
2866 || s->next->hl != s->hl)));
2868 get_glyph_string_clip_rect (s, &clip_rect);
2870 if (s->face->box == FACE_SIMPLE_BOX)
2871 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2872 left_p, right_p, &clip_rect);
2873 else
2875 x_setup_relief_colors (s);
2876 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2877 width, raised_p, true, true, left_p, right_p,
2878 &clip_rect);
2883 /* Draw foreground of image glyph string S. */
2885 static void
2886 x_draw_image_foreground (struct glyph_string *s)
2888 int x = s->x;
2889 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2891 /* If first glyph of S has a left box line, start drawing it to the
2892 right of that line. */
2893 if (s->face->box != FACE_NO_BOX
2894 && s->first_glyph->left_box_line_p
2895 && s->slice.x == 0)
2896 x += eabs (s->face->box_line_width);
2898 /* If there is a margin around the image, adjust x- and y-position
2899 by that margin. */
2900 if (s->slice.x == 0)
2901 x += s->img->hmargin;
2902 if (s->slice.y == 0)
2903 y += s->img->vmargin;
2905 if (s->img->pixmap)
2907 if (s->img->mask)
2909 /* We can't set both a clip mask and use XSetClipRectangles
2910 because the latter also sets a clip mask. We also can't
2911 trust on the shape extension to be available
2912 (XShapeCombineRegion). So, compute the rectangle to draw
2913 manually. */
2914 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2915 | GCFunction);
2916 XGCValues xgcv;
2917 XRectangle clip_rect, image_rect, r;
2919 xgcv.clip_mask = s->img->mask;
2920 xgcv.clip_x_origin = x;
2921 xgcv.clip_y_origin = y;
2922 xgcv.function = GXcopy;
2923 XChangeGC (s->display, s->gc, mask, &xgcv);
2925 get_glyph_string_clip_rect (s, &clip_rect);
2926 image_rect.x = x;
2927 image_rect.y = y;
2928 image_rect.width = s->slice.width;
2929 image_rect.height = s->slice.height;
2930 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2931 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2932 s->slice.x + r.x - x, s->slice.y + r.y - y,
2933 r.width, r.height, r.x, r.y);
2935 else
2937 XRectangle clip_rect, image_rect, r;
2939 get_glyph_string_clip_rect (s, &clip_rect);
2940 image_rect.x = x;
2941 image_rect.y = y;
2942 image_rect.width = s->slice.width;
2943 image_rect.height = s->slice.height;
2944 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2945 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2946 s->slice.x + r.x - x, s->slice.y + r.y - y,
2947 r.width, r.height, r.x, r.y);
2949 /* When the image has a mask, we can expect that at
2950 least part of a mouse highlight or a block cursor will
2951 be visible. If the image doesn't have a mask, make
2952 a block cursor visible by drawing a rectangle around
2953 the image. I believe it's looking better if we do
2954 nothing here for mouse-face. */
2955 if (s->hl == DRAW_CURSOR)
2957 int relief = eabs (s->img->relief);
2958 x_draw_rectangle (s->f, s->gc,
2959 x - relief, y - relief,
2960 s->slice.width + relief*2 - 1,
2961 s->slice.height + relief*2 - 1);
2965 else
2966 /* Draw a rectangle if image could not be loaded. */
2967 x_draw_rectangle (s->f, s->gc, x, y,
2968 s->slice.width - 1, s->slice.height - 1);
2972 /* Draw a relief around the image glyph string S. */
2974 static void
2975 x_draw_image_relief (struct glyph_string *s)
2977 int x1, y1, thick;
2978 bool raised_p, top_p, bot_p, left_p, right_p;
2979 int extra_x, extra_y;
2980 XRectangle r;
2981 int x = s->x;
2982 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2984 /* If first glyph of S has a left box line, start drawing it to the
2985 right of that line. */
2986 if (s->face->box != FACE_NO_BOX
2987 && s->first_glyph->left_box_line_p
2988 && s->slice.x == 0)
2989 x += eabs (s->face->box_line_width);
2991 /* If there is a margin around the image, adjust x- and y-position
2992 by that margin. */
2993 if (s->slice.x == 0)
2994 x += s->img->hmargin;
2995 if (s->slice.y == 0)
2996 y += s->img->vmargin;
2998 if (s->hl == DRAW_IMAGE_SUNKEN
2999 || s->hl == DRAW_IMAGE_RAISED)
3001 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
3002 raised_p = s->hl == DRAW_IMAGE_RAISED;
3004 else
3006 thick = eabs (s->img->relief);
3007 raised_p = s->img->relief > 0;
3010 x1 = x + s->slice.width - 1;
3011 y1 = y + s->slice.height - 1;
3013 extra_x = extra_y = 0;
3014 if (s->face->id == TOOL_BAR_FACE_ID)
3016 if (CONSP (Vtool_bar_button_margin)
3017 && INTEGERP (XCAR (Vtool_bar_button_margin))
3018 && INTEGERP (XCDR (Vtool_bar_button_margin)))
3020 extra_x = XINT (XCAR (Vtool_bar_button_margin));
3021 extra_y = XINT (XCDR (Vtool_bar_button_margin));
3023 else if (INTEGERP (Vtool_bar_button_margin))
3024 extra_x = extra_y = XINT (Vtool_bar_button_margin);
3027 top_p = bot_p = left_p = right_p = false;
3029 if (s->slice.x == 0)
3030 x -= thick + extra_x, left_p = true;
3031 if (s->slice.y == 0)
3032 y -= thick + extra_y, top_p = true;
3033 if (s->slice.x + s->slice.width == s->img->width)
3034 x1 += thick + extra_x, right_p = true;
3035 if (s->slice.y + s->slice.height == s->img->height)
3036 y1 += thick + extra_y, bot_p = true;
3038 x_setup_relief_colors (s);
3039 get_glyph_string_clip_rect (s, &r);
3040 x_draw_relief_rect (s->f, x, y, x1, y1, thick, raised_p,
3041 top_p, bot_p, left_p, right_p, &r);
3045 /* Draw the foreground of image glyph string S to PIXMAP. */
3047 static void
3048 x_draw_image_foreground_1 (struct glyph_string *s, Pixmap pixmap)
3050 int x = 0;
3051 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
3053 /* If first glyph of S has a left box line, start drawing it to the
3054 right of that line. */
3055 if (s->face->box != FACE_NO_BOX
3056 && s->first_glyph->left_box_line_p
3057 && s->slice.x == 0)
3058 x += eabs (s->face->box_line_width);
3060 /* If there is a margin around the image, adjust x- and y-position
3061 by that margin. */
3062 if (s->slice.x == 0)
3063 x += s->img->hmargin;
3064 if (s->slice.y == 0)
3065 y += s->img->vmargin;
3067 if (s->img->pixmap)
3069 if (s->img->mask)
3071 /* We can't set both a clip mask and use XSetClipRectangles
3072 because the latter also sets a clip mask. We also can't
3073 trust on the shape extension to be available
3074 (XShapeCombineRegion). So, compute the rectangle to draw
3075 manually. */
3076 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3077 | GCFunction);
3078 XGCValues xgcv;
3080 xgcv.clip_mask = s->img->mask;
3081 xgcv.clip_x_origin = x - s->slice.x;
3082 xgcv.clip_y_origin = y - s->slice.y;
3083 xgcv.function = GXcopy;
3084 XChangeGC (s->display, s->gc, mask, &xgcv);
3086 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3087 s->slice.x, s->slice.y,
3088 s->slice.width, s->slice.height, x, y);
3089 XSetClipMask (s->display, s->gc, None);
3091 else
3093 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3094 s->slice.x, s->slice.y,
3095 s->slice.width, s->slice.height, x, y);
3097 /* When the image has a mask, we can expect that at
3098 least part of a mouse highlight or a block cursor will
3099 be visible. If the image doesn't have a mask, make
3100 a block cursor visible by drawing a rectangle around
3101 the image. I believe it's looking better if we do
3102 nothing here for mouse-face. */
3103 if (s->hl == DRAW_CURSOR)
3105 int r = eabs (s->img->relief);
3106 x_draw_rectangle (s->f, s->gc, x - r, y - r,
3107 s->slice.width + r*2 - 1,
3108 s->slice.height + r*2 - 1);
3112 else
3113 /* Draw a rectangle if image could not be loaded. */
3114 x_draw_rectangle (s->f, s->gc, x, y,
3115 s->slice.width - 1, s->slice.height - 1);
3119 /* Draw part of the background of glyph string S. X, Y, W, and H
3120 give the rectangle to draw. */
3122 static void
3123 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
3125 if (s->stippled_p)
3127 /* Fill background with a stipple pattern. */
3128 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3129 x_fill_rectangle (s->f, s->gc, x, y, w, h);
3130 XSetFillStyle (s->display, s->gc, FillSolid);
3132 else
3133 x_clear_glyph_string_rect (s, x, y, w, h);
3137 /* Draw image glyph string S.
3139 s->y
3140 s->x +-------------------------
3141 | s->face->box
3143 | +-------------------------
3144 | | s->img->margin
3145 | |
3146 | | +-------------------
3147 | | | the image
3149 */
3151 static void
3152 x_draw_image_glyph_string (struct glyph_string *s)
3154 int box_line_hwidth = eabs (s->face->box_line_width);
3155 int box_line_vwidth = max (s->face->box_line_width, 0);
3156 int height;
3157 Pixmap pixmap = None;
3159 height = s->height;
3160 if (s->slice.y == 0)
3161 height -= box_line_vwidth;
3162 if (s->slice.y + s->slice.height >= s->img->height)
3163 height -= box_line_vwidth;
3165 /* Fill background with face under the image. Do it only if row is
3166 taller than image or if image has a clip mask to reduce
3167 flickering. */
3168 s->stippled_p = s->face->stipple != 0;
3169 if (height > s->slice.height
3170 || s->img->hmargin
3171 || s->img->vmargin
3172 || s->img->mask
3173 || s->img->pixmap == 0
3174 || s->width != s->background_width)
3176 if (s->img->mask)
3178 /* Create a pixmap as large as the glyph string. Fill it
3179 with the background color. Copy the image to it, using
3180 its mask. Copy the temporary pixmap to the display. */
3181 Screen *screen = FRAME_X_SCREEN (s->f);
3182 int depth = DefaultDepthOfScreen (screen);
3184 /* Create a pixmap as large as the glyph string. */
3185 pixmap = XCreatePixmap (s->display, s->window,
3186 s->background_width,
3187 s->height, depth);
3189 /* Don't clip in the following because we're working on the
3190 pixmap. */
3191 XSetClipMask (s->display, s->gc, None);
3193 /* Fill the pixmap with the background color/stipple. */
3194 if (s->stippled_p)
3196 /* Fill background with a stipple pattern. */
3197 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3198 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
3199 XFillRectangle (s->display, pixmap, s->gc,
3200 0, 0, s->background_width, s->height);
3201 XSetFillStyle (s->display, s->gc, FillSolid);
3202 XSetTSOrigin (s->display, s->gc, 0, 0);
3204 else
3206 XGCValues xgcv;
3207 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3208 &xgcv);
3209 XSetForeground (s->display, s->gc, xgcv.background);
3210 XFillRectangle (s->display, pixmap, s->gc,
3211 0, 0, s->background_width, s->height);
3212 XSetForeground (s->display, s->gc, xgcv.foreground);
3215 else
3217 int x = s->x;
3218 int y = s->y;
3219 int width = s->background_width;
3221 if (s->first_glyph->left_box_line_p
3222 && s->slice.x == 0)
3224 x += box_line_hwidth;
3225 width -= box_line_hwidth;
3228 if (s->slice.y == 0)
3229 y += box_line_vwidth;
3231 x_draw_glyph_string_bg_rect (s, x, y, width, height);
3234 s->background_filled_p = true;
3237 /* Draw the foreground. */
3238 #ifdef USE_CAIRO
3239 if (s->img->cr_data)
3241 cairo_t *cr = x_begin_cr_clip (s->f, s->gc);
3243 int x = s->x + s->img->hmargin;
3244 int y = s->y + s->img->vmargin;
3245 int width = s->background_width;
3247 cairo_set_source_surface (cr, s->img->cr_data,
3248 x - s->slice.x,
3249 y - s->slice.y);
3250 cairo_rectangle (cr, x, y, width, height);
3251 cairo_fill (cr);
3252 x_end_cr_clip (s->f);
3254 else
3255 #endif
3256 if (pixmap != None)
3258 x_draw_image_foreground_1 (s, pixmap);
3259 x_set_glyph_string_clipping (s);
3260 XCopyArea (s->display, pixmap, s->window, s->gc,
3261 0, 0, s->background_width, s->height, s->x, s->y);
3262 XFreePixmap (s->display, pixmap);
3264 else
3265 x_draw_image_foreground (s);
3267 /* If we must draw a relief around the image, do it. */
3268 if (s->img->relief
3269 || s->hl == DRAW_IMAGE_RAISED
3270 || s->hl == DRAW_IMAGE_SUNKEN)
3271 x_draw_image_relief (s);
3275 /* Draw stretch glyph string S. */
3277 static void
3278 x_draw_stretch_glyph_string (struct glyph_string *s)
3280 eassert (s->first_glyph->type == STRETCH_GLYPH);
3282 if (s->hl == DRAW_CURSOR
3283 && !x_stretch_cursor_p)
3285 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
3286 wide as the stretch glyph. */
3287 int width, background_width = s->background_width;
3288 int x = s->x;
3290 if (!s->row->reversed_p)
3292 int left_x = window_box_left_offset (s->w, TEXT_AREA);
3294 if (x < left_x)
3296 background_width -= left_x - x;
3297 x = left_x;
3300 else
3302 /* In R2L rows, draw the cursor on the right edge of the
3303 stretch glyph. */
3304 int right_x = window_box_right (s->w, TEXT_AREA);
3306 if (x + background_width > right_x)
3307 background_width -= x - right_x;
3308 x += background_width;
3310 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
3311 if (s->row->reversed_p)
3312 x -= width;
3314 /* Draw cursor. */
3315 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
3317 /* Clear rest using the GC of the original non-cursor face. */
3318 if (width < background_width)
3320 int y = s->y;
3321 int w = background_width - width, h = s->height;
3322 XRectangle r;
3323 GC gc;
3325 if (!s->row->reversed_p)
3326 x += width;
3327 else
3328 x = s->x;
3329 if (s->row->mouse_face_p
3330 && cursor_in_mouse_face_p (s->w))
3332 x_set_mouse_face_gc (s);
3333 gc = s->gc;
3335 else
3336 gc = s->face->gc;
3338 get_glyph_string_clip_rect (s, &r);
3339 x_set_clip_rectangles (s->f, gc, &r, 1);
3341 if (s->face->stipple)
3343 /* Fill background with a stipple pattern. */
3344 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3345 x_fill_rectangle (s->f, gc, x, y, w, h);
3346 XSetFillStyle (s->display, gc, FillSolid);
3348 else
3350 XGCValues xgcv;
3351 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3352 XSetForeground (s->display, gc, xgcv.background);
3353 x_fill_rectangle (s->f, gc, x, y, w, h);
3354 XSetForeground (s->display, gc, xgcv.foreground);
3357 x_reset_clip_rectangles (s->f, gc);
3360 else if (!s->background_filled_p)
3362 int background_width = s->background_width;
3363 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
3365 /* Don't draw into left margin, fringe or scrollbar area
3366 except for header line and mode line. */
3367 if (x < left_x && !s->row->mode_line_p)
3369 background_width -= left_x - x;
3370 x = left_x;
3372 if (background_width > 0)
3373 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
3376 s->background_filled_p = true;
3379 /*
3380 Draw a wavy line under S. The wave fills wave_height pixels from y0.
3382 x0 wave_length = 2
3383 --
3384 y0 * * * * *
3385 |* * * * * * * * *
3386 wave_height = 3 | * * * *
3388 */
3390 static void
3391 x_draw_underwave (struct glyph_string *s)
3393 int wave_height = 3, wave_length = 2;
3394 #ifdef USE_CAIRO
3395 x_draw_horizontal_wave (s->f, s->gc, s->x, s->ybase - wave_height + 3,
3396 s->width, wave_height, wave_length);
3397 #else /* not USE_CAIRO */
3398 int dx, dy, x0, y0, width, x1, y1, x2, y2, xmax;
3399 bool odd;
3400 XRectangle wave_clip, string_clip, final_clip;
3402 dx = wave_length;
3403 dy = wave_height - 1;
3404 x0 = s->x;
3405 y0 = s->ybase - wave_height + 3;
3406 width = s->width;
3407 xmax = x0 + width;
3409 /* Find and set clipping rectangle */
3411 wave_clip.x = x0;
3412 wave_clip.y = y0;
3413 wave_clip.width = width;
3414 wave_clip.height = wave_height;
3415 get_glyph_string_clip_rect (s, &string_clip);
3417 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
3418 return;
3420 XSetClipRectangles (s->display, s->gc, 0, 0, &final_clip, 1, Unsorted);
3422 /* Draw the waves */
3424 x1 = x0 - (x0 % dx);
3425 x2 = x1 + dx;
3426 odd = (x1 / dx) & 1;
3427 y1 = y2 = y0;
3429 if (odd)
3430 y1 += dy;
3431 else
3432 y2 += dy;
3434 if (INT_MAX - dx < xmax)
3435 emacs_abort ();
3437 while (x1 <= xmax)
3439 XDrawLine (s->display, s->window, s->gc, x1, y1, x2, y2);
3440 x1 = x2, y1 = y2;
3441 x2 += dx, y2 = y0 + odd*dy;
3442 odd = !odd;
3445 /* Restore previous clipping rectangle(s) */
3446 XSetClipRectangles (s->display, s->gc, 0, 0, s->clip, s->num_clips, Unsorted);
3447 #endif /* not USE_CAIRO */
3451 /* Draw glyph string S. */
3453 static void
3454 x_draw_glyph_string (struct glyph_string *s)
3456 bool relief_drawn_p = false;
3458 /* If S draws into the background of its successors, draw the
3459 background of the successors first so that S can draw into it.
3460 This makes S->next use XDrawString instead of XDrawImageString. */
3461 if (s->next && s->right_overhang && !s->for_overlaps)
3463 int width;
3464 struct glyph_string *next;
3466 for (width = 0, next = s->next;
3467 next && width < s->right_overhang;
3468 width += next->width, next = next->next)
3469 if (next->first_glyph->type != IMAGE_GLYPH)
3471 x_set_glyph_string_gc (next);
3472 x_set_glyph_string_clipping (next);
3473 if (next->first_glyph->type == STRETCH_GLYPH)
3474 x_draw_stretch_glyph_string (next);
3475 else
3476 x_draw_glyph_string_background (next, true);
3477 next->num_clips = 0;
3481 /* Set up S->gc, set clipping and draw S. */
3482 x_set_glyph_string_gc (s);
3484 /* Draw relief (if any) in advance for char/composition so that the
3485 glyph string can be drawn over it. */
3486 if (!s->for_overlaps
3487 && s->face->box != FACE_NO_BOX
3488 && (s->first_glyph->type == CHAR_GLYPH
3489 || s->first_glyph->type == COMPOSITE_GLYPH))
3492 x_set_glyph_string_clipping (s);
3493 x_draw_glyph_string_background (s, true);
3494 x_draw_glyph_string_box (s);
3495 x_set_glyph_string_clipping (s);
3496 relief_drawn_p = true;
3498 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
3499 && !s->clip_tail
3500 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
3501 || (s->next && s->next->hl != s->hl && s->right_overhang)))
3502 /* We must clip just this glyph. left_overhang part has already
3503 drawn when s->prev was drawn, and right_overhang part will be
3504 drawn later when s->next is drawn. */
3505 x_set_glyph_string_clipping_exactly (s, s);
3506 else
3507 x_set_glyph_string_clipping (s);
3509 switch (s->first_glyph->type)
3511 case IMAGE_GLYPH:
3512 x_draw_image_glyph_string (s);
3513 break;
3515 case XWIDGET_GLYPH:
3516 x_draw_xwidget_glyph_string (s);
3517 break;
3519 case STRETCH_GLYPH:
3520 x_draw_stretch_glyph_string (s);
3521 break;
3523 case CHAR_GLYPH:
3524 if (s->for_overlaps)
3525 s->background_filled_p = true;
3526 else
3527 x_draw_glyph_string_background (s, false);
3528 x_draw_glyph_string_foreground (s);
3529 break;
3531 case COMPOSITE_GLYPH:
3532 if (s->for_overlaps || (s->cmp_from > 0
3533 && ! s->first_glyph->u.cmp.automatic))
3534 s->background_filled_p = true;
3535 else
3536 x_draw_glyph_string_background (s, true);
3537 x_draw_composite_glyph_string_foreground (s);
3538 break;
3540 case GLYPHLESS_GLYPH:
3541 if (s->for_overlaps)
3542 s->background_filled_p = true;
3543 else
3544 x_draw_glyph_string_background (s, true);
3545 x_draw_glyphless_glyph_string_foreground (s);
3546 break;
3548 default:
3549 emacs_abort ();
3552 if (!s->for_overlaps)
3554 /* Draw underline. */
3555 if (s->face->underline_p)
3557 if (s->face->underline_type == FACE_UNDER_WAVE)
3559 if (s->face->underline_defaulted_p)
3560 x_draw_underwave (s);
3561 else
3563 XGCValues xgcv;
3564 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3565 XSetForeground (s->display, s->gc, s->face->underline_color);
3566 x_draw_underwave (s);
3567 XSetForeground (s->display, s->gc, xgcv.foreground);
3570 else if (s->face->underline_type == FACE_UNDER_LINE)
3572 unsigned long thickness, position;
3573 int y;
3575 if (s->prev && s->prev->face->underline_p
3576 && s->prev->face->underline_type == FACE_UNDER_LINE)
3578 /* We use the same underline style as the previous one. */
3579 thickness = s->prev->underline_thickness;
3580 position = s->prev->underline_position;
3582 else
3584 /* Get the underline thickness. Default is 1 pixel. */
3585 if (s->font && s->font->underline_thickness > 0)
3586 thickness = s->font->underline_thickness;
3587 else
3588 thickness = 1;
3589 if (x_underline_at_descent_line)
3590 position = (s->height - thickness) - (s->ybase - s->y);
3591 else
3593 /* Get the underline position. This is the recommended
3594 vertical offset in pixels from the baseline to the top of
3595 the underline. This is a signed value according to the
3596 specs, and its default is
3598 ROUND ((maximum descent) / 2), with
3599 ROUND(x) = floor (x + 0.5) */
3601 if (x_use_underline_position_properties
3602 && s->font && s->font->underline_position >= 0)
3603 position = s->font->underline_position;
3604 else if (s->font)
3605 position = (s->font->descent + 1) / 2;
3606 else
3607 position = underline_minimum_offset;
3609 position = max (position, underline_minimum_offset);
3611 /* Check the sanity of thickness and position. We should
3612 avoid drawing underline out of the current line area. */
3613 if (s->y + s->height <= s->ybase + position)
3614 position = (s->height - 1) - (s->ybase - s->y);
3615 if (s->y + s->height < s->ybase + position + thickness)
3616 thickness = (s->y + s->height) - (s->ybase + position);
3617 s->underline_thickness = thickness;
3618 s->underline_position = position;
3619 y = s->ybase + position;
3620 if (s->face->underline_defaulted_p)
3621 x_fill_rectangle (s->f, s->gc,
3622 s->x, y, s->width, thickness);
3623 else
3625 XGCValues xgcv;
3626 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3627 XSetForeground (s->display, s->gc, s->face->underline_color);
3628 x_fill_rectangle (s->f, s->gc,
3629 s->x, y, s->width, thickness);
3630 XSetForeground (s->display, s->gc, xgcv.foreground);
3634 /* Draw overline. */
3635 if (s->face->overline_p)
3637 unsigned long dy = 0, h = 1;
3639 if (s->face->overline_color_defaulted_p)
3640 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3641 s->width, h);
3642 else
3644 XGCValues xgcv;
3645 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3646 XSetForeground (s->display, s->gc, s->face->overline_color);
3647 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3648 s->width, h);
3649 XSetForeground (s->display, s->gc, xgcv.foreground);
3653 /* Draw strike-through. */
3654 if (s->face->strike_through_p)
3656 unsigned long h = 1;
3657 unsigned long dy = (s->height - h) / 2;
3659 if (s->face->strike_through_color_defaulted_p)
3660 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3661 s->width, h);
3662 else
3664 XGCValues xgcv;
3665 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3666 XSetForeground (s->display, s->gc, s->face->strike_through_color);
3667 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3668 s->width, h);
3669 XSetForeground (s->display, s->gc, xgcv.foreground);
3673 /* Draw relief if not yet drawn. */
3674 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
3675 x_draw_glyph_string_box (s);
3677 if (s->prev)
3679 struct glyph_string *prev;
3681 for (prev = s->prev; prev; prev = prev->prev)
3682 if (prev->hl != s->hl
3683 && prev->x + prev->width + prev->right_overhang > s->x)
3685 /* As prev was drawn while clipped to its own area, we
3686 must draw the right_overhang part using s->hl now. */
3687 enum draw_glyphs_face save = prev->hl;
3689 prev->hl = s->hl;
3690 x_set_glyph_string_gc (prev);
3691 x_set_glyph_string_clipping_exactly (s, prev);
3692 if (prev->first_glyph->type == CHAR_GLYPH)
3693 x_draw_glyph_string_foreground (prev);
3694 else
3695 x_draw_composite_glyph_string_foreground (prev);
3696 x_reset_clip_rectangles (prev->f, prev->gc);
3697 prev->hl = save;
3698 prev->num_clips = 0;
3702 if (s->next)
3704 struct glyph_string *next;
3706 for (next = s->next; next; next = next->next)
3707 if (next->hl != s->hl
3708 && next->x - next->left_overhang < s->x + s->width)
3710 /* As next will be drawn while clipped to its own area,
3711 we must draw the left_overhang part using s->hl now. */
3712 enum draw_glyphs_face save = next->hl;
3714 next->hl = s->hl;
3715 x_set_glyph_string_gc (next);
3716 x_set_glyph_string_clipping_exactly (s, next);
3717 if (next->first_glyph->type == CHAR_GLYPH)
3718 x_draw_glyph_string_foreground (next);
3719 else
3720 x_draw_composite_glyph_string_foreground (next);
3721 x_reset_clip_rectangles (next->f, next->gc);
3722 next->hl = save;
3723 next->num_clips = 0;
3724 next->clip_head = s->next;
3729 /* Reset clipping. */
3730 x_reset_clip_rectangles (s->f, s->gc);
3731 s->num_clips = 0;
3734 /* Shift display to make room for inserted glyphs. */
3736 static void
3737 x_shift_glyphs_for_insert (struct frame *f, int x, int y, int width, int height, int shift_by)
3739 /* Never called on a GUI frame, see
3740 http://lists.gnu.org/archive/html/emacs-devel/2015-05/msg00456.html
3741 */
3742 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3743 f->output_data.x->normal_gc,
3744 x, y, width, height,
3745 x + shift_by, y);
3748 /* Delete N glyphs at the nominal cursor position. Not implemented
3749 for X frames. */
3751 static void
3752 x_delete_glyphs (struct frame *f, register int n)
3754 emacs_abort ();
3758 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
3759 If they are <= 0, this is probably an error. */
3761 static ATTRIBUTE_UNUSED void
3762 x_clear_area1 (Display *dpy, Window window,
3763 int x, int y, int width, int height, int exposures)
3765 eassert (width > 0 && height > 0);
3766 XClearArea (dpy, window, x, y, width, height, exposures);
3769 void
3770 x_clear_area (struct frame *f, int x, int y, int width, int height)
3772 #ifdef USE_CAIRO
3773 cairo_t *cr;
3775 eassert (width > 0 && height > 0);
3777 cr = x_begin_cr_clip (f, NULL);
3778 x_set_cr_source_with_gc_background (f, f->output_data.x->normal_gc);
3779 cairo_rectangle (cr, x, y, width, height);
3780 cairo_fill (cr);
3781 x_end_cr_clip (f);
3782 #else
3783 x_clear_area1 (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3784 x, y, width, height, False);
3785 #endif
3789 /* Clear an entire frame. */
3791 static void
3792 x_clear_frame (struct frame *f)
3794 /* Clearing the frame will erase any cursor, so mark them all as no
3795 longer visible. */
3796 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
3798 block_input ();
3800 x_clear_window (f);
3802 /* We have to clear the scroll bars. If we have changed colors or
3803 something like that, then they should be notified. */
3804 x_scroll_bar_clear (f);
3806 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3807 /* Make sure scroll bars are redrawn. As they aren't redrawn by
3808 redisplay, do it here. */
3809 if (FRAME_GTK_WIDGET (f))
3810 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
3811 #endif
3813 XFlush (FRAME_X_DISPLAY (f));
3815 unblock_input ();
3818 /* RIF: Show hourglass cursor on frame F. */
3820 static void
3821 x_show_hourglass (struct frame *f)
3823 Display *dpy = FRAME_X_DISPLAY (f);
3825 if (dpy)
3827 struct x_output *x = FRAME_X_OUTPUT (f);
3828 #ifdef USE_X_TOOLKIT
3829 if (x->widget)
3830 #else
3831 if (FRAME_OUTER_WINDOW (f))
3832 #endif
3834 x->hourglass_p = true;
3836 if (!x->hourglass_window)
3838 unsigned long mask = CWCursor;
3839 XSetWindowAttributes attrs;
3840 #ifdef USE_GTK
3841 Window parent = FRAME_X_WINDOW (f);
3842 #else
3843 Window parent = FRAME_OUTER_WINDOW (f);
3844 #endif
3845 attrs.cursor = x->hourglass_cursor;
3847 x->hourglass_window = XCreateWindow
3848 (dpy, parent, 0, 0, 32000, 32000, 0, 0,
3849 InputOnly, CopyFromParent, mask, &attrs);
3852 XMapRaised (dpy, x->hourglass_window);
3853 XFlush (dpy);
3858 /* RIF: Cancel hourglass cursor on frame F. */
3860 static void
3861 x_hide_hourglass (struct frame *f)
3863 struct x_output *x = FRAME_X_OUTPUT (f);
3865 /* Watch out for newly created frames. */
3866 if (x->hourglass_window)
3868 XUnmapWindow (FRAME_X_DISPLAY (f), x->hourglass_window);
3869 /* Sync here because XTread_socket looks at the
3870 hourglass_p flag that is reset to zero below. */
3871 XSync (FRAME_X_DISPLAY (f), False);
3872 x->hourglass_p = false;
3876 /* Invert the middle quarter of the frame for .15 sec. */
3878 static void
3879 XTflash (struct frame *f)
3881 block_input ();
3884 #ifdef USE_GTK
3885 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
3886 when the scroll bars and the edit widget share the same X window. */
3887 GdkWindow *window = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
3888 #ifdef HAVE_GTK3
3889 cairo_t *cr = gdk_cairo_create (window);
3890 cairo_set_source_rgb (cr, 1, 1, 1);
3891 cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
3892 #define XFillRectangle(d, win, gc, x, y, w, h) \
3893 do { \
3894 cairo_rectangle (cr, x, y, w, h); \
3895 cairo_fill (cr); \
3896 } \
3897 while (false)
3898 #else /* ! HAVE_GTK3 */
3899 GdkGCValues vals;
3900 GdkGC *gc;
3901 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
3902 ^ FRAME_BACKGROUND_PIXEL (f));
3903 vals.function = GDK_XOR;
3904 gc = gdk_gc_new_with_values (window,
3905 &vals, GDK_GC_FUNCTION | GDK_GC_FOREGROUND);
3906 #define XFillRectangle(d, win, gc, x, y, w, h) \
3907 gdk_draw_rectangle (window, gc, true, x, y, w, h)
3908 #endif /* ! HAVE_GTK3 */
3909 #else /* ! USE_GTK */
3910 GC gc;
3912 /* Create a GC that will use the GXxor function to flip foreground
3913 pixels into background pixels. */
3915 XGCValues values;
3917 values.function = GXxor;
3918 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3919 ^ FRAME_BACKGROUND_PIXEL (f));
3921 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3922 GCFunction | GCForeground, &values);
3924 #endif
3926 /* Get the height not including a menu bar widget. */
3927 int height = FRAME_PIXEL_HEIGHT (f);
3928 /* Height of each line to flash. */
3929 int flash_height = FRAME_LINE_HEIGHT (f);
3930 /* These will be the left and right margins of the rectangles. */
3931 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3932 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3933 int width = flash_right - flash_left;
3935 /* If window is tall, flash top and bottom line. */
3936 if (height > 3 * FRAME_LINE_HEIGHT (f))
3938 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3939 flash_left,
3940 (FRAME_INTERNAL_BORDER_WIDTH (f)
3941 + FRAME_TOP_MARGIN_HEIGHT (f)),
3942 width, flash_height);
3943 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3944 flash_left,
3945 (height - flash_height
3946 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3947 width, flash_height);
3950 else
3951 /* If it is short, flash it all. */
3952 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3953 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3954 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3956 x_flush (f);
3959 struct timespec delay = make_timespec (0, 150 * 1000 * 1000);
3960 struct timespec wakeup = timespec_add (current_timespec (), delay);
3962 /* Keep waiting until past the time wakeup or any input gets
3963 available. */
3964 while (! detect_input_pending ())
3966 struct timespec current = current_timespec ();
3967 struct timespec timeout;
3969 /* Break if result would not be positive. */
3970 if (timespec_cmp (wakeup, current) <= 0)
3971 break;
3973 /* How long `select' should wait. */
3974 timeout = make_timespec (0, 10 * 1000 * 1000);
3976 /* Try to wait that long--but we might wake up sooner. */
3977 pselect (0, NULL, NULL, NULL, &timeout, NULL);
3981 /* If window is tall, flash top and bottom line. */
3982 if (height > 3 * FRAME_LINE_HEIGHT (f))
3984 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3985 flash_left,
3986 (FRAME_INTERNAL_BORDER_WIDTH (f)
3987 + FRAME_TOP_MARGIN_HEIGHT (f)),
3988 width, flash_height);
3989 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3990 flash_left,
3991 (height - flash_height
3992 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3993 width, flash_height);
3995 else
3996 /* If it is short, flash it all. */
3997 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3998 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3999 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
4001 #ifdef USE_GTK
4002 #ifdef HAVE_GTK3
4003 cairo_destroy (cr);
4004 #else
4005 g_object_unref (G_OBJECT (gc));
4006 #endif
4007 #undef XFillRectangle
4008 #else
4009 XFreeGC (FRAME_X_DISPLAY (f), gc);
4010 #endif
4011 x_flush (f);
4015 unblock_input ();
4019 static void
4020 XTtoggle_invisible_pointer (struct frame *f, bool invisible)
4022 block_input ();
4023 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, invisible);
4024 unblock_input ();
4028 /* Make audible bell. */
4030 static void
4031 XTring_bell (struct frame *f)
4033 if (FRAME_X_DISPLAY (f))
4035 if (visible_bell)
4036 XTflash (f);
4037 else
4039 block_input ();
4040 #ifdef HAVE_XKB
4041 XkbBell (FRAME_X_DISPLAY (f), None, 0, None);
4042 #else
4043 XBell (FRAME_X_DISPLAY (f), 0);
4044 #endif
4045 XFlush (FRAME_X_DISPLAY (f));
4046 unblock_input ();
4051 /***********************************************************************
4052 Line Dance
4053 ***********************************************************************/
4055 /* Perform an insert-lines or delete-lines operation, inserting N
4056 lines or deleting -N lines at vertical position VPOS. */
4058 static void
4059 x_ins_del_lines (struct frame *f, int vpos, int n)
4061 emacs_abort ();
4065 /* Scroll part of the display as described by RUN. */
4067 static void
4068 x_scroll_run (struct window *w, struct run *run)
4070 struct frame *f = XFRAME (w->frame);
4071 int x, y, width, height, from_y, to_y, bottom_y;
4073 /* Get frame-relative bounding box of the text display area of W,
4074 without mode lines. Include in this box the left and right
4075 fringe of W. */
4076 window_box (w, ANY_AREA, &x, &y, &width, &height);
4078 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
4079 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
4080 bottom_y = y + height;
4082 if (to_y < from_y)
4084 /* Scrolling up. Make sure we don't copy part of the mode
4085 line at the bottom. */
4086 if (from_y + run->height > bottom_y)
4087 height = bottom_y - from_y;
4088 else
4089 height = run->height;
4091 else
4093 /* Scrolling down. Make sure we don't copy over the mode line.
4094 at the bottom. */
4095 if (to_y + run->height > bottom_y)
4096 height = bottom_y - to_y;
4097 else
4098 height = run->height;
4101 block_input ();
4103 /* Cursor off. Will be switched on again in x_update_window_end. */
4104 x_clear_cursor (w);
4106 #ifdef USE_CAIRO
4107 SET_FRAME_GARBAGED (f);
4108 #else
4109 XCopyArea (FRAME_X_DISPLAY (f),
4110 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4111 f->output_data.x->normal_gc,
4112 x, from_y,
4113 width, height,
4114 x, to_y);
4115 #endif
4117 unblock_input ();
4121 \f
4122 /***********************************************************************
4123 Exposure Events
4124 ***********************************************************************/
4126 \f
4127 static void
4128 frame_highlight (struct frame *f)
4130 /* We used to only do this if Vx_no_window_manager was non-nil, but
4131 the ICCCM (section 4.1.6) says that the window's border pixmap
4132 and border pixel are window attributes which are "private to the
4133 client", so we can always change it to whatever we want. */
4134 block_input ();
4135 /* I recently started to get errors in this XSetWindowBorder, depending on
4136 the window-manager in use, tho something more is at play since I've been
4137 using that same window-manager binary for ever. Let's not crash just
4138 because of this (bug#9310). */
4139 x_catch_errors (FRAME_X_DISPLAY (f));
4140 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4141 f->output_data.x->border_pixel);
4142 x_uncatch_errors ();
4143 unblock_input ();
4144 x_update_cursor (f, true);
4145 x_set_frame_alpha (f);
4148 static void
4149 frame_unhighlight (struct frame *f)
4151 /* We used to only do this if Vx_no_window_manager was non-nil, but
4152 the ICCCM (section 4.1.6) says that the window's border pixmap
4153 and border pixel are window attributes which are "private to the
4154 client", so we can always change it to whatever we want. */
4155 block_input ();
4156 /* Same as above for XSetWindowBorder (bug#9310). */
4157 x_catch_errors (FRAME_X_DISPLAY (f));
4158 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4159 f->output_data.x->border_tile);
4160 x_uncatch_errors ();
4161 unblock_input ();
4162 x_update_cursor (f, true);
4163 x_set_frame_alpha (f);
4166 /* The focus has changed. Update the frames as necessary to reflect
4167 the new situation. Note that we can't change the selected frame
4168 here, because the Lisp code we are interrupting might become confused.
4169 Each event gets marked with the frame in which it occurred, so the
4170 Lisp code can tell when the switch took place by examining the events. */
4172 static void
4173 x_new_focus_frame (struct x_display_info *dpyinfo, struct frame *frame)
4175 struct frame *old_focus = dpyinfo->x_focus_frame;
4177 if (frame != dpyinfo->x_focus_frame)
4179 /* Set this before calling other routines, so that they see
4180 the correct value of x_focus_frame. */
4181 dpyinfo->x_focus_frame = frame;
4183 if (old_focus && old_focus->auto_lower)
4184 x_lower_frame (old_focus);
4186 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
4187 dpyinfo->x_pending_autoraise_frame = dpyinfo->x_focus_frame;
4188 else
4189 dpyinfo->x_pending_autoraise_frame = NULL;
4192 x_frame_rehighlight (dpyinfo);
4195 /* Handle FocusIn and FocusOut state changes for FRAME.
4196 If FRAME has focus and there exists more than one frame, puts
4197 a FOCUS_IN_EVENT into *BUFP. */
4199 static void
4200 x_focus_changed (int type, int state, struct x_display_info *dpyinfo, struct frame *frame, struct input_event *bufp)
4202 if (type == FocusIn)
4204 if (dpyinfo->x_focus_event_frame != frame)
4206 x_new_focus_frame (dpyinfo, frame);
4207 dpyinfo->x_focus_event_frame = frame;
4209 /* Don't stop displaying the initial startup message
4210 for a switch-frame event we don't need. */
4211 /* When run as a daemon, Vterminal_frame is always NIL. */
4212 bufp->arg = (((NILP (Vterminal_frame)
4213 || ! FRAME_X_P (XFRAME (Vterminal_frame))
4214 || EQ (Fdaemonp (), Qt))
4215 && CONSP (Vframe_list)
4216 && !NILP (XCDR (Vframe_list)))
4217 ? Qt : Qnil);
4218 bufp->kind = FOCUS_IN_EVENT;
4219 XSETFRAME (bufp->frame_or_window, frame);
4222 frame->output_data.x->focus_state |= state;
4224 #ifdef HAVE_X_I18N
4225 if (FRAME_XIC (frame))
4226 XSetICFocus (FRAME_XIC (frame));
4227 #endif
4229 else if (type == FocusOut)
4231 frame->output_data.x->focus_state &= ~state;
4233 if (dpyinfo->x_focus_event_frame == frame)
4235 dpyinfo->x_focus_event_frame = 0;
4236 x_new_focus_frame (dpyinfo, 0);
4238 bufp->kind = FOCUS_OUT_EVENT;
4239 XSETFRAME (bufp->frame_or_window, frame);
4242 #ifdef HAVE_X_I18N
4243 if (FRAME_XIC (frame))
4244 XUnsetICFocus (FRAME_XIC (frame));
4245 #endif
4246 if (frame->pointer_invisible)
4247 XTtoggle_invisible_pointer (frame, false);
4251 /* Return the Emacs frame-object corresponding to an X window.
4252 It could be the frame's main window or an icon window. */
4254 static struct frame *
4255 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4257 Lisp_Object tail, frame;
4258 struct frame *f;
4260 if (wdesc == None)
4261 return NULL;
4263 FOR_EACH_FRAME (tail, frame)
4265 f = XFRAME (frame);
4266 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4267 continue;
4268 if (f->output_data.x->hourglass_window == wdesc)
4269 return f;
4270 #ifdef USE_X_TOOLKIT
4271 if ((f->output_data.x->edit_widget
4272 && XtWindow (f->output_data.x->edit_widget) == wdesc)
4273 /* A tooltip frame? */
4274 || (!f->output_data.x->edit_widget
4275 && FRAME_X_WINDOW (f) == wdesc)
4276 || f->output_data.x->icon_desc == wdesc)
4277 return f;
4278 #else /* not USE_X_TOOLKIT */
4279 #ifdef USE_GTK
4280 if (f->output_data.x->edit_widget)
4282 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4283 struct x_output *x = f->output_data.x;
4284 if (gwdesc != 0 && gwdesc == x->edit_widget)
4285 return f;
4287 #endif /* USE_GTK */
4288 if (FRAME_X_WINDOW (f) == wdesc
4289 || f->output_data.x->icon_desc == wdesc)
4290 return f;
4291 #endif /* not USE_X_TOOLKIT */
4293 return 0;
4296 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
4298 /* Like x_window_to_frame but also compares the window with the widget's
4299 windows. */
4301 static struct frame *
4302 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4304 Lisp_Object tail, frame;
4305 struct frame *f, *found = NULL;
4306 struct x_output *x;
4308 if (wdesc == None)
4309 return NULL;
4311 FOR_EACH_FRAME (tail, frame)
4313 if (found)
4314 break;
4315 f = XFRAME (frame);
4316 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
4318 /* This frame matches if the window is any of its widgets. */
4319 x = f->output_data.x;
4320 if (x->hourglass_window == wdesc)
4321 found = f;
4322 else if (x->widget)
4324 #ifdef USE_GTK
4325 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4326 if (gwdesc != 0
4327 && gtk_widget_get_toplevel (gwdesc) == x->widget)
4328 found = f;
4329 #else
4330 if (wdesc == XtWindow (x->widget)
4331 || wdesc == XtWindow (x->column_widget)
4332 || wdesc == XtWindow (x->edit_widget))
4333 found = f;
4334 /* Match if the window is this frame's menubar. */
4335 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
4336 found = f;
4337 #endif
4339 else if (FRAME_X_WINDOW (f) == wdesc)
4340 /* A tooltip frame. */
4341 found = f;
4345 return found;
4348 /* Likewise, but consider only the menu bar widget. */
4350 static struct frame *
4351 x_menubar_window_to_frame (struct x_display_info *dpyinfo,
4352 const XEvent *event)
4354 Window wdesc = event->xany.window;
4355 Lisp_Object tail, frame;
4356 struct frame *f;
4357 struct x_output *x;
4359 if (wdesc == None)
4360 return NULL;
4362 FOR_EACH_FRAME (tail, frame)
4364 f = XFRAME (frame);
4365 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4366 continue;
4367 x = f->output_data.x;
4368 #ifdef USE_GTK
4369 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
4370 return f;
4371 #else
4372 /* Match if the window is this frame's menubar. */
4373 if (x->menubar_widget
4374 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
4375 return f;
4376 #endif
4378 return 0;
4381 /* Return the frame whose principal (outermost) window is WDESC.
4382 If WDESC is some other (smaller) window, we return 0. */
4384 struct frame *
4385 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4387 Lisp_Object tail, frame;
4388 struct frame *f;
4389 struct x_output *x;
4391 if (wdesc == None)
4392 return NULL;
4394 FOR_EACH_FRAME (tail, frame)
4396 f = XFRAME (frame);
4397 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4398 continue;
4399 x = f->output_data.x;
4401 if (x->widget)
4403 /* This frame matches if the window is its topmost widget. */
4404 #ifdef USE_GTK
4405 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4406 if (gwdesc == x->widget)
4407 return f;
4408 #else
4409 if (wdesc == XtWindow (x->widget))
4410 return f;
4411 #endif
4413 else if (FRAME_X_WINDOW (f) == wdesc)
4414 /* Tooltip frame. */
4415 return f;
4417 return 0;
4420 #else /* !USE_X_TOOLKIT && !USE_GTK */
4422 #define x_any_window_to_frame(d, i) x_window_to_frame (d, i)
4423 #define x_top_window_to_frame(d, i) x_window_to_frame (d, i)
4425 #endif /* USE_X_TOOLKIT || USE_GTK */
4427 /* The focus may have changed. Figure out if it is a real focus change,
4428 by checking both FocusIn/Out and Enter/LeaveNotify events.
4430 Returns FOCUS_IN_EVENT event in *BUFP. */
4432 static void
4433 x_detect_focus_change (struct x_display_info *dpyinfo, struct frame *frame,
4434 const XEvent *event, struct input_event *bufp)
4436 if (!frame)
4437 return;
4439 switch (event->type)
4441 case EnterNotify:
4442 case LeaveNotify:
4444 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
4445 int focus_state
4446 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
4448 if (event->xcrossing.detail != NotifyInferior
4449 && event->xcrossing.focus
4450 && ! (focus_state & FOCUS_EXPLICIT))
4451 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
4452 FOCUS_IMPLICIT,
4453 dpyinfo, frame, bufp);
4455 break;
4457 case FocusIn:
4458 case FocusOut:
4459 x_focus_changed (event->type,
4460 (event->xfocus.detail == NotifyPointer ?
4461 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
4462 dpyinfo, frame, bufp);
4463 break;
4465 case ClientMessage:
4466 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
4468 enum xembed_message msg = event->xclient.data.l[1];
4469 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
4470 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
4472 break;
4477 #if !defined USE_X_TOOLKIT && !defined USE_GTK
4478 /* Handle an event saying the mouse has moved out of an Emacs frame. */
4480 void
4481 x_mouse_leave (struct x_display_info *dpyinfo)
4483 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
4485 #endif
4487 /* The focus has changed, or we have redirected a frame's focus to
4488 another frame (this happens when a frame uses a surrogate
4489 mini-buffer frame). Shift the highlight as appropriate.
4491 The FRAME argument doesn't necessarily have anything to do with which
4492 frame is being highlighted or un-highlighted; we only use it to find
4493 the appropriate X display info. */
4495 static void
4496 XTframe_rehighlight (struct frame *frame)
4498 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
4501 static void
4502 x_frame_rehighlight (struct x_display_info *dpyinfo)
4504 struct frame *old_highlight = dpyinfo->x_highlight_frame;
4506 if (dpyinfo->x_focus_frame)
4508 dpyinfo->x_highlight_frame
4509 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
4510 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
4511 : dpyinfo->x_focus_frame);
4512 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
4514 fset_focus_frame (dpyinfo->x_focus_frame, Qnil);
4515 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
4518 else
4519 dpyinfo->x_highlight_frame = 0;
4521 if (dpyinfo->x_highlight_frame != old_highlight)
4523 if (old_highlight)
4524 frame_unhighlight (old_highlight);
4525 if (dpyinfo->x_highlight_frame)
4526 frame_highlight (dpyinfo->x_highlight_frame);
4531 \f
4532 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
4534 /* Initialize mode_switch_bit and modifier_meaning. */
4535 static void
4536 x_find_modifier_meanings (struct x_display_info *dpyinfo)
4538 int min_code, max_code;
4539 KeySym *syms;
4540 int syms_per_code;
4541 XModifierKeymap *mods;
4543 dpyinfo->meta_mod_mask = 0;
4544 dpyinfo->shift_lock_mask = 0;
4545 dpyinfo->alt_mod_mask = 0;
4546 dpyinfo->super_mod_mask = 0;
4547 dpyinfo->hyper_mod_mask = 0;
4549 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
4551 syms = XGetKeyboardMapping (dpyinfo->display,
4552 min_code, max_code - min_code + 1,
4553 &syms_per_code);
4554 mods = XGetModifierMapping (dpyinfo->display);
4556 /* Scan the modifier table to see which modifier bits the Meta and
4557 Alt keysyms are on. */
4559 int row, col; /* The row and column in the modifier table. */
4560 bool found_alt_or_meta;
4562 for (row = 3; row < 8; row++)
4564 found_alt_or_meta = false;
4565 for (col = 0; col < mods->max_keypermod; col++)
4567 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
4569 /* Zeroes are used for filler. Skip them. */
4570 if (code == 0)
4571 continue;
4573 /* Are any of this keycode's keysyms a meta key? */
4575 int code_col;
4577 for (code_col = 0; code_col < syms_per_code; code_col++)
4579 int sym = syms[((code - min_code) * syms_per_code) + code_col];
4581 switch (sym)
4583 case XK_Meta_L:
4584 case XK_Meta_R:
4585 found_alt_or_meta = true;
4586 dpyinfo->meta_mod_mask |= (1 << row);
4587 break;
4589 case XK_Alt_L:
4590 case XK_Alt_R:
4591 found_alt_or_meta = true;
4592 dpyinfo->alt_mod_mask |= (1 << row);
4593 break;
4595 case XK_Hyper_L:
4596 case XK_Hyper_R:
4597 if (!found_alt_or_meta)
4598 dpyinfo->hyper_mod_mask |= (1 << row);
4599 code_col = syms_per_code;
4600 col = mods->max_keypermod;
4601 break;
4603 case XK_Super_L:
4604 case XK_Super_R:
4605 if (!found_alt_or_meta)
4606 dpyinfo->super_mod_mask |= (1 << row);
4607 code_col = syms_per_code;
4608 col = mods->max_keypermod;
4609 break;
4611 case XK_Shift_Lock:
4612 /* Ignore this if it's not on the lock modifier. */
4613 if (!found_alt_or_meta && ((1 << row) == LockMask))
4614 dpyinfo->shift_lock_mask = LockMask;
4615 code_col = syms_per_code;
4616 col = mods->max_keypermod;
4617 break;
4625 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
4626 if (! dpyinfo->meta_mod_mask)
4628 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
4629 dpyinfo->alt_mod_mask = 0;
4632 /* If some keys are both alt and meta,
4633 make them just meta, not alt. */
4634 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
4636 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
4639 XFree (syms);
4640 XFreeModifiermap (mods);
4643 /* Convert between the modifier bits X uses and the modifier bits
4644 Emacs uses. */
4646 int
4647 x_x_to_emacs_modifiers (struct x_display_info *dpyinfo, int state)
4649 int mod_meta = meta_modifier;
4650 int mod_alt = alt_modifier;
4651 int mod_hyper = hyper_modifier;
4652 int mod_super = super_modifier;
4653 Lisp_Object tem;
4655 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4656 if (INTEGERP (tem)) mod_alt = XINT (tem) & INT_MAX;
4657 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4658 if (INTEGERP (tem)) mod_meta = XINT (tem) & INT_MAX;
4659 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4660 if (INTEGERP (tem)) mod_hyper = XINT (tem) & INT_MAX;
4661 tem = Fget (Vx_super_keysym, Qmodifier_value);
4662 if (INTEGERP (tem)) mod_super = XINT (tem) & INT_MAX;
4664 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
4665 | ((state & ControlMask) ? ctrl_modifier : 0)
4666 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
4667 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
4668 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
4669 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
4672 static int
4673 x_emacs_to_x_modifiers (struct x_display_info *dpyinfo, EMACS_INT state)
4675 EMACS_INT mod_meta = meta_modifier;
4676 EMACS_INT mod_alt = alt_modifier;
4677 EMACS_INT mod_hyper = hyper_modifier;
4678 EMACS_INT mod_super = super_modifier;
4680 Lisp_Object tem;
4682 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4683 if (INTEGERP (tem)) mod_alt = XINT (tem);
4684 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4685 if (INTEGERP (tem)) mod_meta = XINT (tem);
4686 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4687 if (INTEGERP (tem)) mod_hyper = XINT (tem);
4688 tem = Fget (Vx_super_keysym, Qmodifier_value);
4689 if (INTEGERP (tem)) mod_super = XINT (tem);
4692 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
4693 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
4694 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
4695 | ((state & shift_modifier) ? ShiftMask : 0)
4696 | ((state & ctrl_modifier) ? ControlMask : 0)
4697 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
4700 /* Convert a keysym to its name. */
4702 char *
4703 x_get_keysym_name (int keysym)
4705 char *value;
4707 block_input ();
4708 value = XKeysymToString (keysym);
4709 unblock_input ();
4711 return value;
4714 /* Mouse clicks and mouse movement. Rah.
4716 Formerly, we used PointerMotionHintMask (in standard_event_mask)
4717 so that we would have to call XQueryPointer after each MotionNotify
4718 event to ask for another such event. However, this made mouse tracking
4719 slow, and there was a bug that made it eventually stop.
4721 Simply asking for MotionNotify all the time seems to work better.
4723 In order to avoid asking for motion events and then throwing most
4724 of them away or busy-polling the server for mouse positions, we ask
4725 the server for pointer motion hints. This means that we get only
4726 one event per group of mouse movements. "Groups" are delimited by
4727 other kinds of events (focus changes and button clicks, for
4728 example), or by XQueryPointer calls; when one of these happens, we
4729 get another MotionNotify event the next time the mouse moves. This
4730 is at least as efficient as getting motion events when mouse
4731 tracking is on, and I suspect only negligibly worse when tracking
4732 is off. */
4734 /* Prepare a mouse-event in *RESULT for placement in the input queue.
4736 If the event is a button press, then note that we have grabbed
4737 the mouse. */
4739 static Lisp_Object
4740 construct_mouse_click (struct input_event *result,
4741 const XButtonEvent *event,
4742 struct frame *f)
4744 /* Make the event type NO_EVENT; we'll change that when we decide
4745 otherwise. */
4746 result->kind = MOUSE_CLICK_EVENT;
4747 result->code = event->button - Button1;
4748 result->timestamp = event->time;
4749 result->modifiers = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f),
4750 event->state)
4751 | (event->type == ButtonRelease
4752 ? up_modifier
4753 : down_modifier));
4755 XSETINT (result->x, event->x);
4756 XSETINT (result->y, event->y);