; Fix last commit
[emacs.git] / src / xterm.c
blobdd54552510c9b336e88fdbbaac53b32eeaeed46c
1 /* X Communication module for terminals which understand the X protocol.
3 Copyright (C) 1989, 1993-2015 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"
31 #include "syssignal.h"
33 /* This may include sys/types.h, and that somehow loses
34 if this is not done before the other system files. */
35 #include "xterm.h"
36 #include <X11/cursorfont.h>
38 /* If we have Xfixes extension, use it for pointer blanking. */
39 #ifdef HAVE_XFIXES
40 #include <X11/extensions/Xfixes.h>
41 #endif
43 /* Using Xft implies that XRender is available. */
44 #ifdef HAVE_XFT
45 #include <X11/extensions/Xrender.h>
46 #endif
48 /* Load sys/types.h if not already loaded.
49 In some systems loading it twice is suicidal. */
50 #ifndef makedev
51 #include <sys/types.h>
52 #endif /* makedev */
54 #include <sys/ioctl.h>
56 #include "systime.h"
58 #include <fcntl.h>
59 #include <errno.h>
60 #include <sys/stat.h>
61 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
62 /* #include <sys/param.h> */
64 #include "charset.h"
65 #include "character.h"
66 #include "coding.h"
67 #include "frame.h"
68 #include "dispextern.h"
69 #include "fontset.h"
70 #include "termhooks.h"
71 #include "termopts.h"
72 #include "termchar.h"
73 #include "emacs-icon.h"
74 #include "disptab.h"
75 #include "buffer.h"
76 #include "window.h"
77 #include "keyboard.h"
78 #include "intervals.h"
79 #include "process.h"
80 #include "atimer.h"
81 #include "keymap.h"
82 #include "font.h"
83 #include "xsettings.h"
84 #include "xgselect.h"
85 #include "sysselect.h"
86 #include "menu.h"
88 #ifdef USE_X_TOOLKIT
89 #include <X11/Shell.h>
90 #endif
92 #include <unistd.h>
94 #ifdef USE_GTK
95 #include "gtkutil.h"
96 #ifdef HAVE_GTK3
97 #include <X11/Xproto.h>
98 #endif
99 #endif
101 #if defined (USE_LUCID) || defined (USE_MOTIF)
102 #include "../lwlib/xlwmenu.h"
103 #endif
105 #ifdef USE_X_TOOLKIT
106 #if !defined (NO_EDITRES)
107 #define HACK_EDITRES
108 extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
109 #endif /* not NO_EDITRES */
111 /* Include toolkit specific headers for the scroll bar widget. */
113 #ifdef USE_TOOLKIT_SCROLL_BARS
114 #if defined USE_MOTIF
115 #include <Xm/Xm.h> /* For LESSTIF_VERSION */
116 #include <Xm/ScrollBar.h>
117 #else /* !USE_MOTIF i.e. use Xaw */
119 #ifdef HAVE_XAW3D
120 #include <X11/Xaw3d/Simple.h>
121 #include <X11/Xaw3d/Scrollbar.h>
122 #include <X11/Xaw3d/ThreeD.h>
123 #else /* !HAVE_XAW3D */
124 #include <X11/Xaw/Simple.h>
125 #include <X11/Xaw/Scrollbar.h>
126 #endif /* !HAVE_XAW3D */
127 #ifndef XtNpickTop
128 #define XtNpickTop "pickTop"
129 #endif /* !XtNpickTop */
130 #endif /* !USE_MOTIF */
131 #endif /* USE_TOOLKIT_SCROLL_BARS */
133 #endif /* USE_X_TOOLKIT */
135 #ifdef USE_X_TOOLKIT
136 #include "widget.h"
137 #ifndef XtNinitialState
138 #define XtNinitialState "initialState"
139 #endif
140 #endif
142 #include "bitmaps/gray.xbm"
144 #ifdef HAVE_XKB
145 #include <X11/XKBlib.h>
146 #endif
148 /* Default to using XIM if available. */
149 #ifdef USE_XIM
150 bool use_xim = true;
151 #else
152 bool use_xim = false; /* configure --without-xim */
153 #endif
155 /* Non-zero means that a HELP_EVENT has been generated since Emacs
156 start. */
158 static bool any_help_event_p;
160 /* This is a chain of structures for all the X displays currently in
161 use. */
163 struct x_display_info *x_display_list;
165 #ifdef USE_X_TOOLKIT
167 /* The application context for Xt use. */
168 XtAppContext Xt_app_con;
169 static String Xt_default_resources[] = {0};
171 /* Non-zero means user is interacting with a toolkit scroll bar. */
172 static bool toolkit_scroll_bar_interaction;
174 #endif /* USE_X_TOOLKIT */
176 /* Non-zero timeout value means ignore next mouse click if it arrives
177 before that timeout elapses (i.e. as part of the same sequence of
178 events resulting from clicking on a frame to select it). */
180 static Time ignore_next_mouse_click_timeout;
182 /* Used locally within XTread_socket. */
184 static int x_noop_count;
186 #ifdef USE_GTK
187 /* The name of the Emacs icon file. */
188 static Lisp_Object xg_default_icon_file;
189 #endif
191 /* Some functions take this as char *, not const char *. */
192 static char emacs_class[] = EMACS_CLASS;
194 enum xembed_info
196 XEMBED_MAPPED = 1 << 0
199 enum xembed_message
201 XEMBED_EMBEDDED_NOTIFY = 0,
202 XEMBED_WINDOW_ACTIVATE = 1,
203 XEMBED_WINDOW_DEACTIVATE = 2,
204 XEMBED_REQUEST_FOCUS = 3,
205 XEMBED_FOCUS_IN = 4,
206 XEMBED_FOCUS_OUT = 5,
207 XEMBED_FOCUS_NEXT = 6,
208 XEMBED_FOCUS_PREV = 7,
210 XEMBED_MODALITY_ON = 10,
211 XEMBED_MODALITY_OFF = 11,
212 XEMBED_REGISTER_ACCELERATOR = 12,
213 XEMBED_UNREGISTER_ACCELERATOR = 13,
214 XEMBED_ACTIVATE_ACCELERATOR = 14
217 static void x_free_cr_resources (struct frame *);
218 static bool x_alloc_nearest_color_1 (Display *, Colormap, XColor *);
219 static void x_raise_frame (struct frame *);
220 static void x_lower_frame (struct frame *);
221 static int x_io_error_quitter (Display *);
222 static struct terminal *x_create_terminal (struct x_display_info *);
223 static void x_frame_rehighlight (struct x_display_info *);
225 static void x_clip_to_row (struct window *, struct glyph_row *,
226 enum glyph_row_area, GC);
227 static struct scroll_bar *x_window_to_scroll_bar (Display *, Window, int);
228 static void x_scroll_bar_report_motion (struct frame **, Lisp_Object *,
229 enum scroll_bar_part *,
230 Lisp_Object *, Lisp_Object *,
231 Time *);
232 static void x_horizontal_scroll_bar_report_motion (struct frame **, Lisp_Object *,
233 enum scroll_bar_part *,
234 Lisp_Object *, Lisp_Object *,
235 Time *);
236 static bool x_handle_net_wm_state (struct frame *, const XPropertyEvent *);
237 static void x_check_fullscreen (struct frame *);
238 static void x_check_expected_move (struct frame *, int, int);
239 static void x_sync_with_move (struct frame *, int, int, bool);
240 static int handle_one_xevent (struct x_display_info *,
241 const XEvent *, int *,
242 struct input_event *);
243 #if ! (defined USE_X_TOOLKIT || defined USE_MOTIF)
244 static int x_dispatch_event (XEvent *, Display *);
245 #endif
246 static void x_wm_set_window_state (struct frame *, int);
247 static void x_wm_set_icon_pixmap (struct frame *, ptrdiff_t);
248 static void x_initialize (void);
250 static bool get_current_wm_state (struct frame *, Window, int *, bool *);
252 /* Flush display of frame F. */
254 static void
255 x_flush (struct frame *f)
257 eassert (f && FRAME_X_P (f));
258 /* Don't call XFlush when it is not safe to redisplay; the X
259 connection may be broken. */
260 if (!NILP (Vinhibit_redisplay))
261 return;
263 block_input ();
264 XFlush (FRAME_X_DISPLAY (f));
265 unblock_input ();
269 /* Remove calls to XFlush by defining XFlush to an empty replacement.
270 Calls to XFlush should be unnecessary because the X output buffer
271 is flushed automatically as needed by calls to XPending,
272 XNextEvent, or XWindowEvent according to the XFlush man page.
273 XTread_socket calls XPending. Removing XFlush improves
274 performance. */
276 #define XFlush(DISPLAY) (void) 0
279 /***********************************************************************
280 Debugging
281 ***********************************************************************/
283 #if false
285 /* This is a function useful for recording debugging information about
286 the sequence of occurrences in this file. */
288 struct record
290 char *locus;
291 int type;
294 struct record event_record[100];
296 int event_record_index;
298 void
299 record_event (char *locus, int type)
301 if (event_record_index == ARRAYELTS (event_record))
302 event_record_index = 0;
304 event_record[event_record_index].locus = locus;
305 event_record[event_record_index].type = type;
306 event_record_index++;
309 #endif
311 #ifdef USE_CAIRO
313 #define FRAME_CR_CONTEXT(f) ((f)->output_data.x->cr_context)
314 #define FRAME_CR_SURFACE(f) ((f)->output_data.x->cr_surface)
316 static struct x_gc_ext_data *
317 x_gc_get_ext_data (struct frame *f, GC gc, int create_if_not_found_p)
319 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
320 XEDataObject object;
321 XExtData **head, *ext_data;
323 object.gc = gc;
324 head = XEHeadOfExtensionList (object);
325 ext_data = XFindOnExtensionList (head, dpyinfo->ext_codes->extension);
326 if (ext_data == NULL)
328 if (!create_if_not_found_p)
329 return NULL;
330 else
332 ext_data = xzalloc (sizeof (*ext_data));
333 ext_data->number = dpyinfo->ext_codes->extension;
334 ext_data->private_data = xzalloc (sizeof (struct x_gc_ext_data));
335 XAddToExtensionList (head, ext_data);
338 return (struct x_gc_ext_data *) ext_data->private_data;
341 static void
342 x_extension_initialize (struct x_display_info *dpyinfo)
344 XExtCodes *ext_codes = XAddExtension (dpyinfo->display);
346 dpyinfo->ext_codes = ext_codes;
349 static void
350 x_cr_destroy_surface (struct frame *f)
352 if (FRAME_CR_SURFACE (f))
354 cairo_t *cr = FRAME_CR_CONTEXT (f);
355 cairo_surface_destroy (FRAME_CR_SURFACE (f));
356 FRAME_CR_SURFACE (f) = 0;
357 if (cr) cairo_destroy (cr);
358 FRAME_CR_CONTEXT (f) = NULL;
362 cairo_t *
363 x_begin_cr_clip (struct frame *f, GC gc)
365 cairo_t *cr = FRAME_CR_CONTEXT (f);
367 if (!cr)
370 if (! FRAME_CR_SURFACE (f))
372 cairo_surface_t *surface;
373 surface = cairo_xlib_surface_create (FRAME_X_DISPLAY (f),
374 FRAME_X_WINDOW (f),
375 FRAME_DISPLAY_INFO (f)->visual,
376 FRAME_PIXEL_WIDTH (f),
377 FRAME_PIXEL_HEIGHT (f));
378 cr = cairo_create (surface);
379 cairo_surface_destroy (surface);
381 else
382 cr = cairo_create (FRAME_CR_SURFACE (f));
383 FRAME_CR_CONTEXT (f) = cr;
385 cairo_save (cr);
387 if (gc)
389 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 0);
391 if (gc_ext && gc_ext->n_clip_rects)
393 int i;
395 for (i = 0; i < gc_ext->n_clip_rects; i++)
396 cairo_rectangle (cr, gc_ext->clip_rects[i].x,
397 gc_ext->clip_rects[i].y,
398 gc_ext->clip_rects[i].width,
399 gc_ext->clip_rects[i].height);
400 cairo_clip (cr);
404 return cr;
407 void
408 x_end_cr_clip (struct frame *f)
410 cairo_restore (FRAME_CR_CONTEXT (f));
413 void
414 x_set_cr_source_with_gc_foreground (struct frame *f, GC gc)
416 XGCValues xgcv;
417 XColor color;
419 XGetGCValues (FRAME_X_DISPLAY (f), gc, GCForeground, &xgcv);
420 color.pixel = xgcv.foreground;
421 x_query_color (f, &color);
422 cairo_set_source_rgb (FRAME_CR_CONTEXT (f), color.red / 65535.0,
423 color.green / 65535.0, color.blue / 65535.0);
426 void
427 x_set_cr_source_with_gc_background (struct frame *f, GC gc)
429 XGCValues xgcv;
430 XColor color;
432 XGetGCValues (FRAME_X_DISPLAY (f), gc, GCBackground, &xgcv);
433 color.pixel = xgcv.background;
434 x_query_color (f, &color);
435 cairo_set_source_rgb (FRAME_CR_CONTEXT (f), color.red / 65535.0,
436 color.green / 65535.0, color.blue / 65535.0);
439 /* Fringe bitmaps. */
441 static int max_fringe_bmp = 0;
442 static cairo_pattern_t **fringe_bmp = 0;
444 static void
445 x_cr_define_fringe_bitmap (int which, unsigned short *bits, int h, int wd)
447 int i, stride;
448 cairo_surface_t *surface;
449 unsigned char *data;
450 cairo_pattern_t *pattern;
452 if (which >= max_fringe_bmp)
454 i = max_fringe_bmp;
455 max_fringe_bmp = which + 20;
456 fringe_bmp = (cairo_pattern_t **) xrealloc (fringe_bmp, max_fringe_bmp * sizeof (cairo_pattern_t *));
457 while (i < max_fringe_bmp)
458 fringe_bmp[i++] = 0;
461 block_input ();
463 surface = cairo_image_surface_create (CAIRO_FORMAT_A1, wd, h);
464 stride = cairo_image_surface_get_stride (surface);
465 data = cairo_image_surface_get_data (surface);
467 for (i = 0; i < h; i++)
469 *((unsigned short *) data) = bits[i];
470 data += stride;
473 cairo_surface_mark_dirty (surface);
474 pattern = cairo_pattern_create_for_surface (surface);
475 cairo_surface_destroy (surface);
477 unblock_input ();
479 fringe_bmp[which] = pattern;
482 static void
483 x_cr_destroy_fringe_bitmap (int which)
485 if (which >= max_fringe_bmp)
486 return;
488 if (fringe_bmp[which])
490 block_input ();
491 cairo_pattern_destroy (fringe_bmp[which]);
492 unblock_input ();
494 fringe_bmp[which] = 0;
497 static void
498 x_cr_draw_image (struct frame *f, GC gc, cairo_pattern_t *image,
499 int src_x, int src_y, int width, int height,
500 int dest_x, int dest_y, bool overlay_p)
502 cairo_t *cr;
503 cairo_matrix_t matrix;
504 cairo_surface_t *surface;
505 cairo_format_t format;
507 cr = x_begin_cr_clip (f, gc);
508 if (overlay_p)
509 cairo_rectangle (cr, dest_x, dest_y, width, height);
510 else
512 x_set_cr_source_with_gc_background (f, gc);
513 cairo_rectangle (cr, dest_x, dest_y, width, height);
514 cairo_fill_preserve (cr);
516 cairo_clip (cr);
517 cairo_matrix_init_translate (&matrix, src_x - dest_x, src_y - dest_y);
518 cairo_pattern_set_matrix (image, &matrix);
519 cairo_pattern_get_surface (image, &surface);
520 format = cairo_image_surface_get_format (surface);
521 if (format != CAIRO_FORMAT_A8 && format != CAIRO_FORMAT_A1)
523 cairo_set_source (cr, image);
524 cairo_fill (cr);
526 else
528 x_set_cr_source_with_gc_foreground (f, gc);
529 cairo_mask (cr, image);
531 x_end_cr_clip (f);
534 void
535 x_cr_draw_frame (cairo_t *cr, struct frame *f)
537 int width, height;
539 width = FRAME_PIXEL_WIDTH (f);
540 height = FRAME_PIXEL_HEIGHT (f);
542 x_free_cr_resources (f);
543 FRAME_CR_CONTEXT (f) = cr;
544 x_clear_area (f, 0, 0, width, height);
545 expose_frame (f, 0, 0, width, height);
546 FRAME_CR_CONTEXT (f) = NULL;
549 static cairo_status_t
550 x_cr_accumulate_data (void *closure, const unsigned char *data,
551 unsigned int length)
553 Lisp_Object *acc = (Lisp_Object *) closure;
555 *acc = Fcons (make_unibyte_string ((char const *) data, length), *acc);
557 return CAIRO_STATUS_SUCCESS;
560 static void
561 x_cr_destroy (Lisp_Object arg)
563 cairo_t *cr = (cairo_t *) XSAVE_POINTER (arg, 0);
565 block_input ();
566 cairo_destroy (cr);
567 unblock_input ();
570 Lisp_Object
571 x_cr_export_frames (Lisp_Object frames, cairo_surface_type_t surface_type)
573 struct frame *f;
574 cairo_surface_t *surface;
575 cairo_t *cr;
576 int width, height;
577 void (*surface_set_size_func) (cairo_surface_t *, double, double) = NULL;
578 Lisp_Object acc = Qnil;
579 int count = SPECPDL_INDEX ();
581 Fredisplay (Qt);
583 f = XFRAME (XCAR (frames));
584 frames = XCDR (frames);
585 width = FRAME_PIXEL_WIDTH (f);
586 height = FRAME_PIXEL_HEIGHT (f);
588 block_input ();
589 #ifdef CAIRO_HAS_PDF_SURFACE
590 if (surface_type == CAIRO_SURFACE_TYPE_PDF)
592 surface = cairo_pdf_surface_create_for_stream (x_cr_accumulate_data, &acc,
593 width, height);
594 surface_set_size_func = cairo_pdf_surface_set_size;
596 else
597 #endif
598 #ifdef CAIRO_HAS_PNG_FUNCTIONS
599 if (surface_type == CAIRO_SURFACE_TYPE_IMAGE)
600 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
601 else
602 #endif
603 #ifdef CAIRO_HAS_PS_SURFACE
604 if (surface_type == CAIRO_SURFACE_TYPE_PS)
606 surface = cairo_ps_surface_create_for_stream (x_cr_accumulate_data, &acc,
607 width, height);
608 surface_set_size_func = cairo_ps_surface_set_size;
610 else
611 #endif
612 #ifdef CAIRO_HAS_SVG_SURFACE
613 if (surface_type == CAIRO_SURFACE_TYPE_SVG)
614 surface = cairo_svg_surface_create_for_stream (x_cr_accumulate_data, &acc,
615 width, height);
616 else
617 #endif
618 abort ();
620 cr = cairo_create (surface);
621 cairo_surface_destroy (surface);
622 record_unwind_protect (x_cr_destroy, make_save_ptr (cr));
623 unblock_input ();
625 while (1)
627 QUIT;
629 block_input ();
630 x_free_cr_resources (f);
631 FRAME_CR_CONTEXT (f) = cr;
632 x_clear_area (f, 0, 0, width, height);
633 expose_frame (f, 0, 0, width, height);
634 FRAME_CR_CONTEXT (f) = NULL;
635 unblock_input ();
637 if (NILP (frames))
638 break;
640 block_input ();
641 cairo_surface_show_page (surface);
642 f = XFRAME (XCAR (frames));
643 frames = XCDR (frames);
644 width = FRAME_PIXEL_WIDTH (f);
645 height = FRAME_PIXEL_HEIGHT (f);
646 if (surface_set_size_func)
647 (*surface_set_size_func) (surface, width, height);
648 unblock_input ();
651 #ifdef CAIRO_HAS_PNG_FUNCTIONS
652 if (surface_type == CAIRO_SURFACE_TYPE_IMAGE)
654 block_input ();
655 cairo_surface_flush (surface);
656 cairo_surface_write_to_png_stream (surface, x_cr_accumulate_data, &acc);
657 unblock_input ();
659 #endif
660 unbind_to (count, Qnil);
662 return CALLN (Fapply, intern ("concat"), Fnreverse (acc));
665 #endif /* USE_CAIRO */
667 static void
668 x_free_cr_resources (struct frame *f)
670 #ifdef USE_CAIRO
671 if (f == NULL)
673 Lisp_Object rest, frame;
674 FOR_EACH_FRAME (rest, frame)
675 if (FRAME_X_P (XFRAME (frame)))
676 x_free_cr_resources (XFRAME (frame));
678 else
680 cairo_t *cr = FRAME_CR_CONTEXT (f);
682 if (cr)
684 cairo_surface_t *surface = cairo_get_target (cr);
686 if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_XLIB)
688 cairo_destroy (cr);
689 FRAME_CR_CONTEXT (f) = NULL;
693 #endif
696 static void
697 x_set_clip_rectangles (struct frame *f, GC gc, XRectangle *rectangles, int n)
699 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, rectangles, n, Unsorted);
700 #ifdef USE_CAIRO
701 eassert (n >= 0 && n <= MAX_CLIP_RECTS);
704 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 1);
706 gc_ext->n_clip_rects = n;
707 memcpy (gc_ext->clip_rects, rectangles, sizeof (XRectangle) * n);
709 #endif
712 static void
713 x_reset_clip_rectangles (struct frame *f, GC gc)
715 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
716 #ifdef USE_CAIRO
718 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 0);
720 if (gc_ext)
721 gc_ext->n_clip_rects = 0;
723 #endif
726 static void
727 x_fill_rectangle (struct frame *f, GC gc, int x, int y, int width, int height)
729 #ifdef USE_CAIRO
730 cairo_t *cr;
732 cr = x_begin_cr_clip (f, gc);
733 x_set_cr_source_with_gc_foreground (f, gc);
734 cairo_rectangle (cr, x, y, width, height);
735 cairo_fill (cr);
736 x_end_cr_clip (f);
737 #else
738 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
739 gc, x, y, width, height);
740 #endif
743 static void
744 x_draw_rectangle (struct frame *f, GC gc, int x, int y, int width, int height)
746 #ifdef USE_CAIRO
747 cairo_t *cr;
749 cr = x_begin_cr_clip (f, gc);
750 x_set_cr_source_with_gc_foreground (f, gc);
751 cairo_rectangle (cr, x + 0.5, y + 0.5, width, height);
752 cairo_set_line_width (cr, 1);
753 cairo_stroke (cr);
754 x_end_cr_clip (f);
755 #else
756 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
757 gc, x, y, width, height);
758 #endif
761 static void
762 x_clear_window (struct frame *f)
764 #ifdef USE_CAIRO
765 cairo_t *cr;
767 cr = x_begin_cr_clip (f, NULL);
768 x_set_cr_source_with_gc_background (f, f->output_data.x->normal_gc);
769 cairo_paint (cr);
770 x_end_cr_clip (f);
771 #else
772 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
773 #endif
776 #ifdef USE_CAIRO
777 static void
778 x_fill_trapezoid_for_relief (struct frame *f, GC gc, int x, int y,
779 int width, int height, int top_p)
781 cairo_t *cr;
783 cr = x_begin_cr_clip (f, gc);
784 x_set_cr_source_with_gc_foreground (f, gc);
785 cairo_move_to (cr, top_p ? x : x + height, y);
786 cairo_line_to (cr, x, y + height);
787 cairo_line_to (cr, top_p ? x + width - height : x + width, y + height);
788 cairo_line_to (cr, x + width, y);
789 cairo_fill (cr);
790 x_end_cr_clip (f);
793 enum corners
795 CORNER_BOTTOM_RIGHT, /* 0 -> pi/2 */
796 CORNER_BOTTOM_LEFT, /* pi/2 -> pi */
797 CORNER_TOP_LEFT, /* pi -> 3pi/2 */
798 CORNER_TOP_RIGHT, /* 3pi/2 -> 2pi */
799 CORNER_LAST
802 static void
803 x_erase_corners_for_relief (struct frame *f, GC gc, int x, int y,
804 int width, int height,
805 double radius, double margin, int corners)
807 cairo_t *cr;
808 int i;
810 cr = x_begin_cr_clip (f, gc);
811 x_set_cr_source_with_gc_background (f, gc);
812 for (i = 0; i < CORNER_LAST; i++)
813 if (corners & (1 << i))
815 double xm, ym, xc, yc;
817 if (i == CORNER_TOP_LEFT || i == CORNER_BOTTOM_LEFT)
818 xm = x - margin, xc = xm + radius;
819 else
820 xm = x + width + margin, xc = xm - radius;
821 if (i == CORNER_TOP_LEFT || i == CORNER_TOP_RIGHT)
822 ym = y - margin, yc = ym + radius;
823 else
824 ym = y + height + margin, yc = ym - radius;
826 cairo_move_to (cr, xm, ym);
827 cairo_arc (cr, xc, yc, radius, i * M_PI_2, (i + 1) * M_PI_2);
829 cairo_clip (cr);
830 cairo_rectangle (cr, x, y, width, height);
831 cairo_fill (cr);
832 x_end_cr_clip (f);
835 static void
836 x_draw_horizontal_wave (struct frame *f, GC gc, int x, int y,
837 int width, int height, int wave_length)
839 cairo_t *cr;
840 double dx = wave_length, dy = height - 1;
841 int xoffset, n;
843 cr = x_begin_cr_clip (f, gc);
844 x_set_cr_source_with_gc_foreground (f, gc);
845 cairo_rectangle (cr, x, y, width, height);
846 cairo_clip (cr);
848 if (x >= 0)
850 xoffset = x % (wave_length * 2);
851 if (xoffset == 0)
852 xoffset = wave_length * 2;
854 else
855 xoffset = x % (wave_length * 2) + wave_length * 2;
856 n = (width + xoffset) / wave_length + 1;
857 if (xoffset > wave_length)
859 xoffset -= wave_length;
860 --n;
861 y += height - 1;
862 dy = -dy;
865 cairo_move_to (cr, x - xoffset + 0.5, y + 0.5);
866 while (--n >= 0)
868 cairo_rel_line_to (cr, dx, dy);
869 dy = -dy;
871 cairo_set_line_width (cr, 1);
872 cairo_stroke (cr);
873 x_end_cr_clip (f);
875 #endif
878 /* Return the struct x_display_info corresponding to DPY. */
880 struct x_display_info *
881 x_display_info_for_display (Display *dpy)
883 struct x_display_info *dpyinfo;
885 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
886 if (dpyinfo->display == dpy)
887 return dpyinfo;
889 return 0;
892 static Window
893 x_find_topmost_parent (struct frame *f)
895 struct x_output *x = f->output_data.x;
896 Window win = None, wi = x->parent_desc;
897 Display *dpy = FRAME_X_DISPLAY (f);
899 while (wi != FRAME_DISPLAY_INFO (f)->root_window)
901 Window root;
902 Window *children;
903 unsigned int nchildren;
905 win = wi;
906 if (XQueryTree (dpy, win, &root, &wi, &children, &nchildren))
907 XFree (children);
908 else
909 break;
912 return win;
915 #define OPAQUE 0xffffffff
917 void
918 x_set_frame_alpha (struct frame *f)
920 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
921 Display *dpy = FRAME_X_DISPLAY (f);
922 Window win = FRAME_OUTER_WINDOW (f);
923 double alpha = 1.0;
924 double alpha_min = 1.0;
925 unsigned long opac;
926 Window parent;
928 if (dpyinfo->x_highlight_frame == f)
929 alpha = f->alpha[0];
930 else
931 alpha = f->alpha[1];
933 if (FLOATP (Vframe_alpha_lower_limit))
934 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
935 else if (INTEGERP (Vframe_alpha_lower_limit))
936 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
938 if (alpha < 0.0)
939 return;
940 else if (alpha > 1.0)
941 alpha = 1.0;
942 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
943 alpha = alpha_min;
945 opac = alpha * OPAQUE;
947 x_catch_errors (dpy);
949 /* If there is a parent from the window manager, put the property there
950 also, to work around broken window managers that fail to do that.
951 Do this unconditionally as this function is called on reparent when
952 alpha has not changed on the frame. */
954 parent = x_find_topmost_parent (f);
955 if (parent != None)
956 XChangeProperty (dpy, parent, dpyinfo->Xatom_net_wm_window_opacity,
957 XA_CARDINAL, 32, PropModeReplace,
958 (unsigned char *) &opac, 1);
960 /* return unless necessary */
962 unsigned char *data;
963 Atom actual;
964 int rc, format;
965 unsigned long n, left;
967 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
968 0, 1, False, XA_CARDINAL,
969 &actual, &format, &n, &left,
970 &data);
972 if (rc == Success && actual != None)
974 unsigned long value = *(unsigned long *)data;
975 XFree (data);
976 if (value == opac)
978 x_uncatch_errors ();
979 return;
984 XChangeProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
985 XA_CARDINAL, 32, PropModeReplace,
986 (unsigned char *) &opac, 1);
987 x_uncatch_errors ();
990 /***********************************************************************
991 Starting and ending an update
992 ***********************************************************************/
994 /* Start an update of frame F. This function is installed as a hook
995 for update_begin, i.e. it is called when update_begin is called.
996 This function is called prior to calls to x_update_window_begin for
997 each window being updated. Currently, there is nothing to do here
998 because all interesting stuff is done on a window basis. */
1000 static void
1001 x_update_begin (struct frame *f)
1003 #ifdef USE_CAIRO
1004 if (! NILP (tip_frame) && XFRAME (tip_frame) == f
1005 && ! FRAME_VISIBLE_P (f))
1006 return;
1008 if (! FRAME_CR_SURFACE (f))
1010 int width, height;
1011 #ifdef USE_GTK
1012 if (FRAME_GTK_WIDGET (f))
1014 GdkWindow *w = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
1015 width = gdk_window_get_width (w);
1016 height = gdk_window_get_height (w);
1018 else
1019 #endif
1021 width = FRAME_PIXEL_WIDTH (f);
1022 height = FRAME_PIXEL_HEIGHT (f);
1023 if (! FRAME_EXTERNAL_TOOL_BAR (f))
1024 height += FRAME_TOOL_BAR_HEIGHT (f);
1025 if (! FRAME_EXTERNAL_MENU_BAR (f))
1026 height += FRAME_MENU_BAR_HEIGHT (f);
1029 if (width > 0 && height > 0)
1031 block_input();
1032 FRAME_CR_SURFACE (f) = cairo_image_surface_create
1033 (CAIRO_FORMAT_ARGB32, width, height);
1034 unblock_input();
1037 #endif /* USE_CAIRO */
1040 /* Start update of window W. */
1042 static void
1043 x_update_window_begin (struct window *w)
1045 struct frame *f = XFRAME (WINDOW_FRAME (w));
1046 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
1048 w->output_cursor = w->cursor;
1050 block_input ();
1052 if (f == hlinfo->mouse_face_mouse_frame)
1054 /* Don't do highlighting for mouse motion during the update. */
1055 hlinfo->mouse_face_defer = true;
1057 /* If F needs to be redrawn, simply forget about any prior mouse
1058 highlighting. */
1059 if (FRAME_GARBAGED_P (f))
1060 hlinfo->mouse_face_window = Qnil;
1063 unblock_input ();
1067 /* Draw a vertical window border from (x,y0) to (x,y1) */
1069 static void
1070 x_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
1072 struct frame *f = XFRAME (WINDOW_FRAME (w));
1073 struct face *face;
1075 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
1076 if (face)
1077 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
1078 face->foreground);
1080 #ifdef USE_CAIRO
1081 x_fill_rectangle (f, f->output_data.x->normal_gc, x, y0, 1, y1 - y0);
1082 #else
1083 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1084 f->output_data.x->normal_gc, x, y0, x, y1);
1085 #endif
1088 /* Draw a window divider from (x0,y0) to (x1,y1) */
1090 static void
1091 x_draw_window_divider (struct window *w, int x0, int x1, int y0, int y1)
1093 struct frame *f = XFRAME (WINDOW_FRAME (w));
1094 struct face *face = FACE_FROM_ID (f, WINDOW_DIVIDER_FACE_ID);
1095 struct face *face_first = FACE_FROM_ID (f, WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
1096 struct face *face_last = FACE_FROM_ID (f, WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
1097 unsigned long color = face ? face->foreground : FRAME_FOREGROUND_PIXEL (f);
1098 unsigned long color_first = (face_first
1099 ? face_first->foreground
1100 : FRAME_FOREGROUND_PIXEL (f));
1101 unsigned long color_last = (face_last
1102 ? face_last->foreground
1103 : FRAME_FOREGROUND_PIXEL (f));
1104 Display *display = FRAME_X_DISPLAY (f);
1106 if (y1 - y0 > x1 - x0 && x1 - x0 > 2)
1107 /* Vertical. */
1109 XSetForeground (display, f->output_data.x->normal_gc, color_first);
1110 x_fill_rectangle (f, f->output_data.x->normal_gc,
1111 x0, y0, 1, y1 - y0);
1112 XSetForeground (display, f->output_data.x->normal_gc, color);
1113 x_fill_rectangle (f, f->output_data.x->normal_gc,
1114 x0 + 1, y0, x1 - x0 - 2, y1 - y0);
1115 XSetForeground (display, f->output_data.x->normal_gc, color_last);
1116 x_fill_rectangle (f, f->output_data.x->normal_gc,
1117 x1 - 1, y0, 1, y1 - y0);
1119 else if (x1 - x0 > y1 - y0 && y1 - y0 > 3)
1120 /* Horizontal. */
1122 XSetForeground (display, f->output_data.x->normal_gc, color_first);
1123 x_fill_rectangle (f, f->output_data.x->normal_gc,
1124 x0, y0, x1 - x0, 1);
1125 XSetForeground (display, f->output_data.x->normal_gc, color);
1126 x_fill_rectangle (f, f->output_data.x->normal_gc,
1127 x0, y0 + 1, x1 - x0, y1 - y0 - 2);
1128 XSetForeground (display, f->output_data.x->normal_gc, color_last);
1129 x_fill_rectangle (f, f->output_data.x->normal_gc,
1130 x0, y1 - 1, x1 - x0, 1);
1132 else
1134 XSetForeground (display, f->output_data.x->normal_gc, color);
1135 x_fill_rectangle (f, f->output_data.x->normal_gc,
1136 x0, y0, x1 - x0, y1 - y0);
1140 /* End update of window W.
1142 Draw vertical borders between horizontally adjacent windows, and
1143 display W's cursor if CURSOR_ON_P is non-zero.
1145 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
1146 glyphs in mouse-face were overwritten. In that case we have to
1147 make sure that the mouse-highlight is properly redrawn.
1149 W may be a menu bar pseudo-window in case we don't have X toolkit
1150 support. Such windows don't have a cursor, so don't display it
1151 here. */
1153 static void
1154 x_update_window_end (struct window *w, bool cursor_on_p,
1155 bool mouse_face_overwritten_p)
1157 if (!w->pseudo_window_p)
1159 block_input ();
1161 if (cursor_on_p)
1162 display_and_set_cursor (w, true,
1163 w->output_cursor.hpos, w->output_cursor.vpos,
1164 w->output_cursor.x, w->output_cursor.y);
1166 if (draw_window_fringes (w, true))
1168 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
1169 x_draw_right_divider (w);
1170 else
1171 x_draw_vertical_border (w);
1174 unblock_input ();
1177 /* If a row with mouse-face was overwritten, arrange for
1178 XTframe_up_to_date to redisplay the mouse highlight. */
1179 if (mouse_face_overwritten_p)
1181 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
1183 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
1184 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
1185 hlinfo->mouse_face_window = Qnil;
1190 /* End update of frame F. This function is installed as a hook in
1191 update_end. */
1193 static void
1194 x_update_end (struct frame *f)
1196 /* Mouse highlight may be displayed again. */
1197 MOUSE_HL_INFO (f)->mouse_face_defer = false;
1199 #ifdef USE_CAIRO
1200 if (FRAME_CR_SURFACE (f))
1202 cairo_t *cr = 0;
1203 block_input();
1204 #if defined (USE_GTK) && defined (HAVE_GTK3)
1205 if (FRAME_GTK_WIDGET (f))
1207 GdkWindow *w = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
1208 cr = gdk_cairo_create (w);
1210 else
1211 #endif
1213 cairo_surface_t *surface;
1214 int width = FRAME_PIXEL_WIDTH (f);
1215 int height = FRAME_PIXEL_HEIGHT (f);
1216 if (! FRAME_EXTERNAL_TOOL_BAR (f))
1217 height += FRAME_TOOL_BAR_HEIGHT (f);
1218 if (! FRAME_EXTERNAL_MENU_BAR (f))
1219 height += FRAME_MENU_BAR_HEIGHT (f);
1220 surface = cairo_xlib_surface_create (FRAME_X_DISPLAY (f),
1221 FRAME_X_WINDOW (f),
1222 FRAME_DISPLAY_INFO (f)->visual,
1223 width,
1224 height);
1225 cr = cairo_create (surface);
1226 cairo_surface_destroy (surface);
1229 cairo_set_source_surface (cr, FRAME_CR_SURFACE (f), 0, 0);
1230 cairo_paint (cr);
1231 cairo_destroy (cr);
1232 unblock_input ();
1234 #endif /* USE_CAIRO */
1236 #ifndef XFlush
1237 block_input ();
1238 XFlush (FRAME_X_DISPLAY (f));
1239 unblock_input ();
1240 #endif
1244 /* This function is called from various places in xdisp.c
1245 whenever a complete update has been performed. */
1247 static void
1248 XTframe_up_to_date (struct frame *f)
1250 if (FRAME_X_P (f))
1251 FRAME_MOUSE_UPDATE (f);
1255 /* Clear under internal border if any (GTK has its own version). */
1256 #ifndef USE_GTK
1257 void
1258 x_clear_under_internal_border (struct frame *f)
1260 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
1262 int border = FRAME_INTERNAL_BORDER_WIDTH (f);
1263 int width = FRAME_PIXEL_WIDTH (f);
1264 int height = FRAME_PIXEL_HEIGHT (f);
1265 int margin = FRAME_TOP_MARGIN_HEIGHT (f);
1267 block_input ();
1268 x_clear_area (f, 0, 0, border, height);
1269 x_clear_area (f, 0, margin, width, border);
1270 x_clear_area (f, width - border, 0, border, height);
1271 x_clear_area (f, 0, height - border, width, border);
1272 unblock_input ();
1275 #endif
1277 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
1278 arrow bitmaps, or clear the fringes if no bitmaps are required
1279 before DESIRED_ROW is made current. This function is called from
1280 update_window_line only if it is known that there are differences
1281 between bitmaps to be drawn between current row and DESIRED_ROW. */
1283 static void
1284 x_after_update_window_line (struct window *w, struct glyph_row *desired_row)
1286 eassert (w);
1288 if (!desired_row->mode_line_p && !w->pseudo_window_p)
1289 desired_row->redraw_fringe_bitmaps_p = true;
1291 #ifdef USE_X_TOOLKIT
1292 /* When a window has disappeared, make sure that no rest of
1293 full-width rows stays visible in the internal border. Could
1294 check here if updated window is the leftmost/rightmost window,
1295 but I guess it's not worth doing since vertically split windows
1296 are almost never used, internal border is rarely set, and the
1297 overhead is very small. */
1299 struct frame *f;
1300 int width, height;
1302 if (windows_or_buffers_changed
1303 && desired_row->full_width_p
1304 && (f = XFRAME (w->frame),
1305 width = FRAME_INTERNAL_BORDER_WIDTH (f),
1306 width != 0)
1307 && (height = desired_row->visible_height,
1308 height > 0))
1310 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
1312 block_input ();
1313 x_clear_area (f, 0, y, width, height);
1314 x_clear_area (f, FRAME_PIXEL_WIDTH (f) - width, y, width, height);
1315 unblock_input ();
1318 #endif
1321 static void
1322 x_draw_fringe_bitmap (struct window *w, struct glyph_row *row, struct draw_fringe_bitmap_params *p)
1324 struct frame *f = XFRAME (WINDOW_FRAME (w));
1325 Display *display = FRAME_X_DISPLAY (f);
1326 Window window = FRAME_X_WINDOW (f);
1327 GC gc = f->output_data.x->normal_gc;
1328 struct face *face = p->face;
1330 /* Must clip because of partially visible lines. */
1331 x_clip_to_row (w, row, ANY_AREA, gc);
1333 if (p->bx >= 0 && !p->overlay_p)
1335 /* In case the same realized face is used for fringes and
1336 for something displayed in the text (e.g. face `region' on
1337 mono-displays, the fill style may have been changed to
1338 FillSolid in x_draw_glyph_string_background. */
1339 if (face->stipple)
1340 XSetFillStyle (display, face->gc, FillOpaqueStippled);
1341 else
1342 XSetForeground (display, face->gc, face->background);
1344 x_fill_rectangle (f, face->gc, p->bx, p->by, p->nx, p->ny);
1346 if (!face->stipple)
1347 XSetForeground (display, face->gc, face->foreground);
1350 #ifdef USE_CAIRO
1351 if (p->which && p->which < max_fringe_bmp)
1353 XGCValues gcv;
1355 XGetGCValues (display, gc, GCForeground | GCBackground, &gcv);
1356 XSetForeground (display, gc, (p->cursor_p
1357 ? (p->overlay_p ? face->background
1358 : f->output_data.x->cursor_pixel)
1359 : face->foreground));
1360 XSetBackground (display, gc, face->background);
1361 x_cr_draw_image (f, gc, fringe_bmp[p->which], 0, p->dh,
1362 p->wd, p->h, p->x, p->y, p->overlay_p);
1363 XSetForeground (display, gc, gcv.foreground);
1364 XSetBackground (display, gc, gcv.background);
1366 #else /* not USE_CAIRO */
1367 if (p->which)
1369 char *bits;
1370 Pixmap pixmap, clipmask = (Pixmap) 0;
1371 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
1372 XGCValues gcv;
1374 if (p->wd > 8)
1375 bits = (char *) (p->bits + p->dh);
1376 else
1377 bits = (char *) p->bits + p->dh;
1379 /* Draw the bitmap. I believe these small pixmaps can be cached
1380 by the server. */
1381 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
1382 (p->cursor_p
1383 ? (p->overlay_p ? face->background
1384 : f->output_data.x->cursor_pixel)
1385 : face->foreground),
1386 face->background, depth);
1388 if (p->overlay_p)
1390 clipmask = XCreatePixmapFromBitmapData (display,
1391 FRAME_DISPLAY_INFO (f)->root_window,
1392 bits, p->wd, p->h,
1393 1, 0, 1);
1394 gcv.clip_mask = clipmask;
1395 gcv.clip_x_origin = p->x;
1396 gcv.clip_y_origin = p->y;
1397 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
1400 XCopyArea (display, pixmap, window, gc, 0, 0,
1401 p->wd, p->h, p->x, p->y);
1402 XFreePixmap (display, pixmap);
1404 if (p->overlay_p)
1406 gcv.clip_mask = (Pixmap) 0;
1407 XChangeGC (display, gc, GCClipMask, &gcv);
1408 XFreePixmap (display, clipmask);
1411 #endif /* not USE_CAIRO */
1413 x_reset_clip_rectangles (f, gc);
1416 /***********************************************************************
1417 Glyph display
1418 ***********************************************************************/
1422 static void x_set_glyph_string_clipping (struct glyph_string *);
1423 static void x_set_glyph_string_gc (struct glyph_string *);
1424 static void x_draw_glyph_string_foreground (struct glyph_string *);
1425 static void x_draw_composite_glyph_string_foreground (struct glyph_string *);
1426 static void x_draw_glyph_string_box (struct glyph_string *);
1427 static void x_draw_glyph_string (struct glyph_string *);
1428 static _Noreturn void x_delete_glyphs (struct frame *, int);
1429 static void x_compute_glyph_string_overhangs (struct glyph_string *);
1430 static void x_set_cursor_gc (struct glyph_string *);
1431 static void x_set_mode_line_face_gc (struct glyph_string *);
1432 static void x_set_mouse_face_gc (struct glyph_string *);
1433 static bool x_alloc_lighter_color (struct frame *, Display *, Colormap,
1434 unsigned long *, double, int);
1435 static void x_setup_relief_color (struct frame *, struct relief *,
1436 double, int, unsigned long);
1437 static void x_setup_relief_colors (struct glyph_string *);
1438 static void x_draw_image_glyph_string (struct glyph_string *);
1439 static void x_draw_image_relief (struct glyph_string *);
1440 static void x_draw_image_foreground (struct glyph_string *);
1441 static void x_draw_image_foreground_1 (struct glyph_string *, Pixmap);
1442 static void x_clear_glyph_string_rect (struct glyph_string *, int,
1443 int, int, int);
1444 static void x_draw_relief_rect (struct frame *, int, int, int, int,
1445 int, bool, bool, bool, bool, bool,
1446 XRectangle *);
1447 static void x_draw_box_rect (struct glyph_string *, int, int, int, int,
1448 int, bool, bool, XRectangle *);
1449 static void x_scroll_bar_clear (struct frame *);
1451 #ifdef GLYPH_DEBUG
1452 static void x_check_font (struct frame *, struct font *);
1453 #endif
1456 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
1457 face. */
1459 static void
1460 x_set_cursor_gc (struct glyph_string *s)
1462 if (s->font == FRAME_FONT (s->f)
1463 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
1464 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
1465 && !s->cmp)
1466 s->gc = s->f->output_data.x->cursor_gc;
1467 else
1469 /* Cursor on non-default face: must merge. */
1470 XGCValues xgcv;
1471 unsigned long mask;
1473 xgcv.background = s->f->output_data.x->cursor_pixel;
1474 xgcv.foreground = s->face->background;
1476 /* If the glyph would be invisible, try a different foreground. */
1477 if (xgcv.foreground == xgcv.background)
1478 xgcv.foreground = s->face->foreground;
1479 if (xgcv.foreground == xgcv.background)
1480 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1481 if (xgcv.foreground == xgcv.background)
1482 xgcv.foreground = s->face->foreground;
1484 /* Make sure the cursor is distinct from text in this face. */
1485 if (xgcv.background == s->face->background
1486 && xgcv.foreground == s->face->foreground)
1488 xgcv.background = s->face->foreground;
1489 xgcv.foreground = s->face->background;
1492 IF_DEBUG (x_check_font (s->f, s->font));
1493 xgcv.graphics_exposures = False;
1494 mask = GCForeground | GCBackground | GCGraphicsExposures;
1496 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1497 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1498 mask, &xgcv);
1499 else
1500 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1501 = XCreateGC (s->display, s->window, mask, &xgcv);
1503 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1508 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1510 static void
1511 x_set_mouse_face_gc (struct glyph_string *s)
1513 int face_id;
1514 struct face *face;
1516 /* What face has to be used last for the mouse face? */
1517 face_id = MOUSE_HL_INFO (s->f)->mouse_face_face_id;
1518 face = FACE_FROM_ID (s->f, face_id);
1519 if (face == NULL)
1520 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1522 if (s->first_glyph->type == CHAR_GLYPH)
1523 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1524 else
1525 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1526 s->face = FACE_FROM_ID (s->f, face_id);
1527 prepare_face_for_display (s->f, s->face);
1529 if (s->font == s->face->font)
1530 s->gc = s->face->gc;
1531 else
1533 /* Otherwise construct scratch_cursor_gc with values from FACE
1534 except for FONT. */
1535 XGCValues xgcv;
1536 unsigned long mask;
1538 xgcv.background = s->face->background;
1539 xgcv.foreground = s->face->foreground;
1540 xgcv.graphics_exposures = False;
1541 mask = GCForeground | GCBackground | GCGraphicsExposures;
1543 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1544 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1545 mask, &xgcv);
1546 else
1547 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1548 = XCreateGC (s->display, s->window, mask, &xgcv);
1550 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1553 eassert (s->gc != 0);
1557 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1558 Faces to use in the mode line have already been computed when the
1559 matrix was built, so there isn't much to do, here. */
1561 static void
1562 x_set_mode_line_face_gc (struct glyph_string *s)
1564 s->gc = s->face->gc;
1568 /* Set S->gc of glyph string S for drawing that glyph string. Set
1569 S->stippled_p to a non-zero value if the face of S has a stipple
1570 pattern. */
1572 static void
1573 x_set_glyph_string_gc (struct glyph_string *s)
1575 prepare_face_for_display (s->f, s->face);
1577 if (s->hl == DRAW_NORMAL_TEXT)
1579 s->gc = s->face->gc;
1580 s->stippled_p = s->face->stipple != 0;
1582 else if (s->hl == DRAW_INVERSE_VIDEO)
1584 x_set_mode_line_face_gc (s);
1585 s->stippled_p = s->face->stipple != 0;
1587 else if (s->hl == DRAW_CURSOR)
1589 x_set_cursor_gc (s);
1590 s->stippled_p = false;
1592 else if (s->hl == DRAW_MOUSE_FACE)
1594 x_set_mouse_face_gc (s);
1595 s->stippled_p = s->face->stipple != 0;
1597 else if (s->hl == DRAW_IMAGE_RAISED
1598 || s->hl == DRAW_IMAGE_SUNKEN)
1600 s->gc = s->face->gc;
1601 s->stippled_p = s->face->stipple != 0;
1603 else
1604 emacs_abort ();
1606 /* GC must have been set. */
1607 eassert (s->gc != 0);
1611 /* Set clipping for output of glyph string S. S may be part of a mode
1612 line or menu if we don't have X toolkit support. */
1614 static void
1615 x_set_glyph_string_clipping (struct glyph_string *s)
1617 XRectangle *r = s->clip;
1618 int n = get_glyph_string_clip_rects (s, r, 2);
1620 if (n > 0)
1621 x_set_clip_rectangles (s->f, s->gc, r, n);
1622 s->num_clips = n;
1626 /* Set SRC's clipping for output of glyph string DST. This is called
1627 when we are drawing DST's left_overhang or right_overhang only in
1628 the area of SRC. */
1630 static void
1631 x_set_glyph_string_clipping_exactly (struct glyph_string *src, struct glyph_string *dst)
1633 XRectangle r;
1635 r.x = src->x;
1636 r.width = src->width;
1637 r.y = src->y;
1638 r.height = src->height;
1639 dst->clip[0] = r;
1640 dst->num_clips = 1;
1641 x_set_clip_rectangles (dst->f, dst->gc, &r, 1);
1645 /* RIF:
1646 Compute left and right overhang of glyph string S. */
1648 static void
1649 x_compute_glyph_string_overhangs (struct glyph_string *s)
1651 if (s->cmp == NULL
1652 && (s->first_glyph->type == CHAR_GLYPH
1653 || s->first_glyph->type == COMPOSITE_GLYPH))
1655 struct font_metrics metrics;
1657 if (s->first_glyph->type == CHAR_GLYPH)
1659 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1660 struct font *font = s->font;
1661 int i;
1663 for (i = 0; i < s->nchars; i++)
1664 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1665 font->driver->text_extents (font, code, s->nchars, &metrics);
1667 else
1669 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1671 composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
1673 s->right_overhang = (metrics.rbearing > metrics.width
1674 ? metrics.rbearing - metrics.width : 0);
1675 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1677 else if (s->cmp)
1679 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1680 s->left_overhang = - s->cmp->lbearing;
1685 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1687 static void
1688 x_clear_glyph_string_rect (struct glyph_string *s, int x, int y, int w, int h)
1690 XGCValues xgcv;
1691 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1692 XSetForeground (s->display, s->gc, xgcv.background);
1693 x_fill_rectangle (s->f, s->gc, x, y, w, h);
1694 XSetForeground (s->display, s->gc, xgcv.foreground);
1698 /* Draw the background of glyph_string S. If S->background_filled_p
1699 is non-zero don't draw it. FORCE_P non-zero means draw the
1700 background even if it wouldn't be drawn normally. This is used
1701 when a string preceding S draws into the background of S, or S
1702 contains the first component of a composition. */
1704 static void
1705 x_draw_glyph_string_background (struct glyph_string *s, bool force_p)
1707 /* Nothing to do if background has already been drawn or if it
1708 shouldn't be drawn in the first place. */
1709 if (!s->background_filled_p)
1711 int box_line_width = max (s->face->box_line_width, 0);
1713 if (s->stippled_p)
1715 /* Fill background with a stipple pattern. */
1716 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1717 x_fill_rectangle (s->f, s->gc, s->x,
1718 s->y + box_line_width,
1719 s->background_width,
1720 s->height - 2 * box_line_width);
1721 XSetFillStyle (s->display, s->gc, FillSolid);
1722 s->background_filled_p = true;
1724 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1725 /* When xdisp.c ignores FONT_HEIGHT, we cannot trust
1726 font dimensions, since the actual glyphs might be
1727 much smaller. So in that case we always clear the
1728 rectangle with background color. */
1729 || FONT_TOO_HIGH (s->font)
1730 || s->font_not_found_p
1731 || s->extends_to_end_of_line_p
1732 || force_p)
1734 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1735 s->background_width,
1736 s->height - 2 * box_line_width);
1737 s->background_filled_p = true;
1743 /* Draw the foreground of glyph string S. */
1745 static void
1746 x_draw_glyph_string_foreground (struct glyph_string *s)
1748 int i, x;
1750 /* If first glyph of S has a left box line, start drawing the text
1751 of S to the right of that box line. */
1752 if (s->face->box != FACE_NO_BOX
1753 && s->first_glyph->left_box_line_p)
1754 x = s->x + eabs (s->face->box_line_width);
1755 else
1756 x = s->x;
1758 /* Draw characters of S as rectangles if S's font could not be
1759 loaded. */
1760 if (s->font_not_found_p)
1762 for (i = 0; i < s->nchars; ++i)
1764 struct glyph *g = s->first_glyph + i;
1765 x_draw_rectangle (s->f,
1766 s->gc, x, s->y, g->pixel_width - 1,
1767 s->height - 1);
1768 x += g->pixel_width;
1771 else
1773 struct font *font = s->font;
1774 int boff = font->baseline_offset;
1775 int y;
1777 if (font->vertical_centering)
1778 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1780 y = s->ybase - boff;
1781 if (s->for_overlaps
1782 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1783 font->driver->draw (s, 0, s->nchars, x, y, false);
1784 else
1785 font->driver->draw (s, 0, s->nchars, x, y, true);
1786 if (s->face->overstrike)
1787 font->driver->draw (s, 0, s->nchars, x + 1, y, false);
1791 /* Draw the foreground of composite glyph string S. */
1793 static void
1794 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1796 int i, j, x;
1797 struct font *font = s->font;
1799 /* If first glyph of S has a left box line, start drawing the text
1800 of S to the right of that box line. */
1801 if (s->face && s->face->box != FACE_NO_BOX
1802 && s->first_glyph->left_box_line_p)
1803 x = s->x + eabs (s->face->box_line_width);
1804 else
1805 x = s->x;
1807 /* S is a glyph string for a composition. S->cmp_from is the index
1808 of the first character drawn for glyphs of this composition.
1809 S->cmp_from == 0 means we are drawing the very first character of
1810 this composition. */
1812 /* Draw a rectangle for the composition if the font for the very
1813 first character of the composition could not be loaded. */
1814 if (s->font_not_found_p)
1816 if (s->cmp_from == 0)
1817 x_draw_rectangle (s->f, s->gc, x, s->y,
1818 s->width - 1, s->height - 1);
1820 else if (! s->first_glyph->u.cmp.automatic)
1822 int y = s->ybase;
1824 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1825 /* TAB in a composition means display glyphs with padding
1826 space on the left or right. */
1827 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1829 int xx = x + s->cmp->offsets[j * 2];
1830 int yy = y - s->cmp->offsets[j * 2 + 1];
1832 font->driver->draw (s, j, j + 1, xx, yy, false);
1833 if (s->face->overstrike)
1834 font->driver->draw (s, j, j + 1, xx + 1, yy, false);
1837 else
1839 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1840 Lisp_Object glyph;
1841 int y = s->ybase;
1842 int width = 0;
1844 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1846 glyph = LGSTRING_GLYPH (gstring, i);
1847 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1848 width += LGLYPH_WIDTH (glyph);
1849 else
1851 int xoff, yoff, wadjust;
1853 if (j < i)
1855 font->driver->draw (s, j, i, x, y, false);
1856 if (s->face->overstrike)
1857 font->driver->draw (s, j, i, x + 1, y, false);
1858 x += width;
1860 xoff = LGLYPH_XOFF (glyph);
1861 yoff = LGLYPH_YOFF (glyph);
1862 wadjust = LGLYPH_WADJUST (glyph);
1863 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, false);
1864 if (s->face->overstrike)
1865 font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff,
1866 false);
1867 x += wadjust;
1868 j = i + 1;
1869 width = 0;
1872 if (j < i)
1874 font->driver->draw (s, j, i, x, y, false);
1875 if (s->face->overstrike)
1876 font->driver->draw (s, j, i, x + 1, y, false);
1882 /* Draw the foreground of glyph string S for glyphless characters. */
1884 static void
1885 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1887 struct glyph *glyph = s->first_glyph;
1888 XChar2b char2b[8];
1889 int x, i, j;
1891 /* If first glyph of S has a left box line, start drawing the text
1892 of S to the right of that box line. */
1893 if (s->face && s->face->box != FACE_NO_BOX
1894 && s->first_glyph->left_box_line_p)
1895 x = s->x + eabs (s->face->box_line_width);
1896 else
1897 x = s->x;
1899 s->char2b = char2b;
1901 for (i = 0; i < s->nchars; i++, glyph++)
1903 char buf[7], *str = NULL;
1904 int len = glyph->u.glyphless.len;
1906 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1908 if (len > 0
1909 && CHAR_TABLE_P (Vglyphless_char_display)
1910 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1911 >= 1))
1913 Lisp_Object acronym
1914 = (! glyph->u.glyphless.for_no_font
1915 ? CHAR_TABLE_REF (Vglyphless_char_display,
1916 glyph->u.glyphless.ch)
1917 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
1918 if (STRINGP (acronym))
1919 str = SSDATA (acronym);
1922 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
1924 sprintf (buf, "%0*X",
1925 glyph->u.glyphless.ch < 0x10000 ? 4 : 6,
1926 glyph->u.glyphless.ch + 0u);
1927 str = buf;
1930 if (str)
1932 int upper_len = (len + 1) / 2;
1933 unsigned code;
1935 /* It is assured that all LEN characters in STR is ASCII. */
1936 for (j = 0; j < len; j++)
1938 code = s->font->driver->encode_char (s->font, str[j]);
1939 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
1941 s->font->driver->draw (s, 0, upper_len,
1942 x + glyph->slice.glyphless.upper_xoff,
1943 s->ybase + glyph->slice.glyphless.upper_yoff,
1944 false);
1945 s->font->driver->draw (s, upper_len, len,
1946 x + glyph->slice.glyphless.lower_xoff,
1947 s->ybase + glyph->slice.glyphless.lower_yoff,
1948 false);
1950 if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
1951 x_draw_rectangle (s->f, s->gc,
1952 x, s->ybase - glyph->ascent,
1953 glyph->pixel_width - 1,
1954 glyph->ascent + glyph->descent - 1);
1955 x += glyph->pixel_width;
1959 #ifdef USE_X_TOOLKIT
1961 #ifdef USE_LUCID
1963 /* Return the frame on which widget WIDGET is used.. Abort if frame
1964 cannot be determined. */
1966 static struct frame *
1967 x_frame_of_widget (Widget widget)
1969 struct x_display_info *dpyinfo;
1970 Lisp_Object tail, frame;
1971 struct frame *f;
1973 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1975 /* Find the top-level shell of the widget. Note that this function
1976 can be called when the widget is not yet realized, so XtWindow
1977 (widget) == 0. That's the reason we can't simply use
1978 x_any_window_to_frame. */
1979 while (!XtIsTopLevelShell (widget))
1980 widget = XtParent (widget);
1982 /* Look for a frame with that top-level widget. Allocate the color
1983 on that frame to get the right gamma correction value. */
1984 FOR_EACH_FRAME (tail, frame)
1986 f = XFRAME (frame);
1987 if (FRAME_X_P (f)
1988 && f->output_data.nothing != 1
1989 && FRAME_DISPLAY_INFO (f) == dpyinfo
1990 && f->output_data.x->widget == widget)
1991 return f;
1993 emacs_abort ();
1996 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1997 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1998 If this produces the same color as PIXEL, try a color where all RGB
1999 values have DELTA added. Return the allocated color in *PIXEL.
2000 DISPLAY is the X display, CMAP is the colormap to operate on.
2001 Value is true if successful. */
2003 bool
2004 x_alloc_lighter_color_for_widget (Widget widget, Display *display, Colormap cmap,
2005 unsigned long *pixel, double factor, int delta)
2007 struct frame *f = x_frame_of_widget (widget);
2008 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
2011 #endif /* USE_LUCID */
2014 /* Structure specifying which arguments should be passed by Xt to
2015 cvt_string_to_pixel. We want the widget's screen and colormap. */
2017 static XtConvertArgRec cvt_string_to_pixel_args[] =
2019 {XtWidgetBaseOffset, (XtPointer) offsetof (WidgetRec, core.screen),
2020 sizeof (Screen *)},
2021 {XtWidgetBaseOffset, (XtPointer) offsetof (WidgetRec, core.colormap),
2022 sizeof (Colormap)}
2026 /* The address of this variable is returned by
2027 cvt_string_to_pixel. */
2029 static Pixel cvt_string_to_pixel_value;
2032 /* Convert a color name to a pixel color.
2034 DPY is the display we are working on.
2036 ARGS is an array of *NARGS XrmValue structures holding additional
2037 information about the widget for which the conversion takes place.
2038 The contents of this array are determined by the specification
2039 in cvt_string_to_pixel_args.
2041 FROM is a pointer to an XrmValue which points to the color name to
2042 convert. TO is an XrmValue in which to return the pixel color.
2044 CLOSURE_RET is a pointer to user-data, in which we record if
2045 we allocated the color or not.
2047 Value is True if successful, False otherwise. */
2049 static Boolean
2050 cvt_string_to_pixel (Display *dpy, XrmValue *args, Cardinal *nargs,
2051 XrmValue *from, XrmValue *to,
2052 XtPointer *closure_ret)
2054 Screen *screen;
2055 Colormap cmap;
2056 Pixel pixel;
2057 String color_name;
2058 XColor color;
2060 if (*nargs != 2)
2062 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
2063 "wrongParameters", "cvt_string_to_pixel",
2064 "XtToolkitError",
2065 "Screen and colormap args required", NULL, NULL);
2066 return False;
2069 screen = *(Screen **) args[0].addr;
2070 cmap = *(Colormap *) args[1].addr;
2071 color_name = (String) from->addr;
2073 if (strcmp (color_name, XtDefaultBackground) == 0)
2075 *closure_ret = (XtPointer) False;
2076 pixel = WhitePixelOfScreen (screen);
2078 else if (strcmp (color_name, XtDefaultForeground) == 0)
2080 *closure_ret = (XtPointer) False;
2081 pixel = BlackPixelOfScreen (screen);
2083 else if (XParseColor (dpy, cmap, color_name, &color)
2084 && x_alloc_nearest_color_1 (dpy, cmap, &color))
2086 pixel = color.pixel;
2087 *closure_ret = (XtPointer) True;
2089 else
2091 String params[1];
2092 Cardinal nparams = 1;
2094 params[0] = color_name;
2095 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
2096 "badValue", "cvt_string_to_pixel",
2097 "XtToolkitError", "Invalid color '%s'",
2098 params, &nparams);
2099 return False;
2102 if (to->addr != NULL)
2104 if (to->size < sizeof (Pixel))
2106 to->size = sizeof (Pixel);
2107 return False;
2110 *(Pixel *) to->addr = pixel;
2112 else
2114 cvt_string_to_pixel_value = pixel;
2115 to->addr = (XtPointer) &cvt_string_to_pixel_value;
2118 to->size = sizeof (Pixel);
2119 return True;
2123 /* Free a pixel color which was previously allocated via
2124 cvt_string_to_pixel. This is registered as the destructor
2125 for this type of resource via XtSetTypeConverter.
2127 APP is the application context in which we work.
2129 TO is a pointer to an XrmValue holding the color to free.
2130 CLOSURE is the value we stored in CLOSURE_RET for this color
2131 in cvt_string_to_pixel.
2133 ARGS and NARGS are like for cvt_string_to_pixel. */
2135 static void
2136 cvt_pixel_dtor (XtAppContext app, XrmValuePtr to, XtPointer closure, XrmValuePtr args,
2137 Cardinal *nargs)
2139 if (*nargs != 2)
2141 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
2142 "XtToolkitError",
2143 "Screen and colormap arguments required",
2144 NULL, NULL);
2146 else if (closure != NULL)
2148 /* We did allocate the pixel, so free it. */
2149 Screen *screen = *(Screen **) args[0].addr;
2150 Colormap cmap = *(Colormap *) args[1].addr;
2151 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
2152 (Pixel *) to->addr, 1);
2157 #endif /* USE_X_TOOLKIT */
2160 /* Value is an array of XColor structures for the contents of the
2161 color map of display DPY. Set *NCELLS to the size of the array.
2162 Note that this probably shouldn't be called for large color maps,
2163 say a 24-bit TrueColor map. */
2165 static const XColor *
2166 x_color_cells (Display *dpy, int *ncells)
2168 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
2170 if (dpyinfo->color_cells == NULL)
2172 Screen *screen = dpyinfo->screen;
2173 int ncolor_cells = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
2174 int i;
2176 dpyinfo->color_cells = xnmalloc (ncolor_cells,
2177 sizeof *dpyinfo->color_cells);
2178 dpyinfo->ncolor_cells = ncolor_cells;
2180 for (i = 0; i < ncolor_cells; ++i)
2181 dpyinfo->color_cells[i].pixel = i;
2183 XQueryColors (dpy, dpyinfo->cmap,
2184 dpyinfo->color_cells, ncolor_cells);
2187 *ncells = dpyinfo->ncolor_cells;
2188 return dpyinfo->color_cells;
2192 /* On frame F, translate pixel colors to RGB values for the NCOLORS
2193 colors in COLORS. Use cached information, if available. */
2195 void
2196 x_query_colors (struct frame *f, XColor *colors, int ncolors)
2198 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2200 if (dpyinfo->color_cells)
2202 int i;
2203 for (i = 0; i < ncolors; ++i)
2205 unsigned long pixel = colors[i].pixel;
2206 eassert (pixel < dpyinfo->ncolor_cells);
2207 eassert (dpyinfo->color_cells[pixel].pixel == pixel);
2208 colors[i] = dpyinfo->color_cells[pixel];
2211 else
2212 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
2216 /* On frame F, translate pixel color to RGB values for the color in
2217 COLOR. Use cached information, if available. */
2219 void
2220 x_query_color (struct frame *f, XColor *color)
2222 x_query_colors (f, color, 1);
2226 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
2227 exact match can't be allocated, try the nearest color available.
2228 Value is true if successful. Set *COLOR to the color
2229 allocated. */
2231 static bool
2232 x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color)
2234 bool rc;
2236 rc = XAllocColor (dpy, cmap, color) != 0;
2237 if (rc == 0)
2239 /* If we got to this point, the colormap is full, so we're going
2240 to try to get the next closest color. The algorithm used is
2241 a least-squares matching, which is what X uses for closest
2242 color matching with StaticColor visuals. */
2243 int nearest, i;
2244 int max_color_delta = 255;
2245 int max_delta = 3 * max_color_delta;
2246 int nearest_delta = max_delta + 1;
2247 int ncells;
2248 const XColor *cells = x_color_cells (dpy, &ncells);
2250 for (nearest = i = 0; i < ncells; ++i)
2252 int dred = (color->red >> 8) - (cells[i].red >> 8);
2253 int dgreen = (color->green >> 8) - (cells[i].green >> 8);
2254 int dblue = (color->blue >> 8) - (cells[i].blue >> 8);
2255 int delta = dred * dred + dgreen * dgreen + dblue * dblue;
2257 if (delta < nearest_delta)
2259 nearest = i;
2260 nearest_delta = delta;
2264 color->red = cells[nearest].red;
2265 color->green = cells[nearest].green;
2266 color->blue = cells[nearest].blue;
2267 rc = XAllocColor (dpy, cmap, color) != 0;
2269 else
2271 /* If allocation succeeded, and the allocated pixel color is not
2272 equal to a cached pixel color recorded earlier, there was a
2273 change in the colormap, so clear the color cache. */
2274 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
2275 XColor *cached_color;
2277 if (dpyinfo->color_cells
2278 && (cached_color = &dpyinfo->color_cells[color->pixel],
2279 (cached_color->red != color->red
2280 || cached_color->blue != color->blue
2281 || cached_color->green != color->green)))
2283 xfree (dpyinfo->color_cells);
2284 dpyinfo->color_cells = NULL;
2285 dpyinfo->ncolor_cells = 0;
2289 #ifdef DEBUG_X_COLORS
2290 if (rc)
2291 register_color (color->pixel);
2292 #endif /* DEBUG_X_COLORS */
2294 return rc;
2298 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
2299 exact match can't be allocated, try the nearest color available.
2300 Value is true if successful. Set *COLOR to the color
2301 allocated. */
2303 bool
2304 x_alloc_nearest_color (struct frame *f, Colormap cmap, XColor *color)
2306 gamma_correct (f, color);
2307 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
2311 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
2312 It's necessary to do this instead of just using PIXEL directly to
2313 get color reference counts right. */
2315 unsigned long
2316 x_copy_color (struct frame *f, unsigned long pixel)
2318 XColor color;
2320 color.pixel = pixel;
2321 block_input ();
2322 x_query_color (f, &color);
2323 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
2324 unblock_input ();
2325 #ifdef DEBUG_X_COLORS
2326 register_color (pixel);
2327 #endif
2328 return color.pixel;
2332 /* Brightness beyond which a color won't have its highlight brightness
2333 boosted.
2335 Nominally, highlight colors for `3d' faces are calculated by
2336 brightening an object's color by a constant scale factor, but this
2337 doesn't yield good results for dark colors, so for colors who's
2338 brightness is less than this value (on a scale of 0-65535) have an
2339 use an additional additive factor.
2341 The value here is set so that the default menu-bar/mode-line color
2342 (grey75) will not have its highlights changed at all. */
2343 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
2346 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
2347 or DELTA. Try a color with RGB values multiplied by FACTOR first.
2348 If this produces the same color as PIXEL, try a color where all RGB
2349 values have DELTA added. Return the allocated color in *PIXEL.
2350 DISPLAY is the X display, CMAP is the colormap to operate on.
2351 Value is non-zero if successful. */
2353 static bool
2354 x_alloc_lighter_color (struct frame *f, Display *display, Colormap cmap,
2355 unsigned long *pixel, double factor, int delta)
2357 XColor color, new;
2358 long bright;
2359 bool success_p;
2361 /* Get RGB color values. */
2362 color.pixel = *pixel;
2363 x_query_color (f, &color);
2365 /* Change RGB values by specified FACTOR. Avoid overflow! */
2366 eassert (factor >= 0);
2367 new.red = min (0xffff, factor * color.red);
2368 new.green = min (0xffff, factor * color.green);
2369 new.blue = min (0xffff, factor * color.blue);
2371 /* Calculate brightness of COLOR. */
2372 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
2374 /* We only boost colors that are darker than
2375 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
2376 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
2377 /* Make an additive adjustment to NEW, because it's dark enough so
2378 that scaling by FACTOR alone isn't enough. */
2380 /* How far below the limit this color is (0 - 1, 1 being darker). */
2381 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
2382 /* The additive adjustment. */
2383 int min_delta = delta * dimness * factor / 2;
2385 if (factor < 1)
2387 new.red = max (0, new.red - min_delta);
2388 new.green = max (0, new.green - min_delta);
2389 new.blue = max (0, new.blue - min_delta);
2391 else
2393 new.red = min (0xffff, min_delta + new.red);
2394 new.green = min (0xffff, min_delta + new.green);
2395 new.blue = min (0xffff, min_delta + new.blue);
2399 /* Try to allocate the color. */
2400 success_p = x_alloc_nearest_color (f, cmap, &new);
2401 if (success_p)
2403 if (new.pixel == *pixel)
2405 /* If we end up with the same color as before, try adding
2406 delta to the RGB values. */
2407 x_free_colors (f, &new.pixel, 1);
2409 new.red = min (0xffff, delta + color.red);
2410 new.green = min (0xffff, delta + color.green);
2411 new.blue = min (0xffff, delta + color.blue);
2412 success_p = x_alloc_nearest_color (f, cmap, &new);
2414 else
2415 success_p = true;
2416 *pixel = new.pixel;
2419 return success_p;
2423 /* Set up the foreground color for drawing relief lines of glyph
2424 string S. RELIEF is a pointer to a struct relief containing the GC
2425 with which lines will be drawn. Use a color that is FACTOR or
2426 DELTA lighter or darker than the relief's background which is found
2427 in S->f->output_data.x->relief_background. If such a color cannot
2428 be allocated, use DEFAULT_PIXEL, instead. */
2430 static void
2431 x_setup_relief_color (struct frame *f, struct relief *relief, double factor,
2432 int delta, unsigned long default_pixel)
2434 XGCValues xgcv;
2435 struct x_output *di = f->output_data.x;
2436 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
2437 unsigned long pixel;
2438 unsigned long background = di->relief_background;
2439 Colormap cmap = FRAME_X_COLORMAP (f);
2440 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2441 Display *dpy = FRAME_X_DISPLAY (f);
2443 xgcv.graphics_exposures = False;
2444 xgcv.line_width = 1;
2446 /* Free previously allocated color. The color cell will be reused
2447 when it has been freed as many times as it was allocated, so this
2448 doesn't affect faces using the same colors. */
2449 if (relief->gc && relief->pixel != -1)
2451 x_free_colors (f, &relief->pixel, 1);
2452 relief->pixel = -1;
2455 /* Allocate new color. */
2456 xgcv.foreground = default_pixel;
2457 pixel = background;
2458 if (dpyinfo->n_planes != 1
2459 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
2460 xgcv.foreground = relief->pixel = pixel;
2462 if (relief->gc == 0)
2464 xgcv.stipple = dpyinfo->gray;
2465 mask |= GCStipple;
2466 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
2468 else
2469 XChangeGC (dpy, relief->gc, mask, &xgcv);
2473 /* Set up colors for the relief lines around glyph string S. */
2475 static void
2476 x_setup_relief_colors (struct glyph_string *s)
2478 struct x_output *di = s->f->output_data.x;
2479 unsigned long color;
2481 if (s->face->use_box_color_for_shadows_p)
2482 color = s->face->box_color;
2483 else if (s->first_glyph->type == IMAGE_GLYPH
2484 && s->img->pixmap
2485 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2486 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2487 else
2489 XGCValues xgcv;
2491 /* Get the background color of the face. */
2492 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2493 color = xgcv.background;
2496 if (di->white_relief.gc == 0
2497 || color != di->relief_background)
2499 di->relief_background = color;
2500 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2501 WHITE_PIX_DEFAULT (s->f));
2502 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2503 BLACK_PIX_DEFAULT (s->f));
2508 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2509 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2510 to draw, it must be >= 0. RAISED_P means draw a raised
2511 relief. LEFT_P means draw a relief on the left side of
2512 the rectangle. RIGHT_P means draw a relief on the right
2513 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2514 when drawing. */
2516 static void
2517 x_draw_relief_rect (struct frame *f,
2518 int left_x, int top_y, int right_x, int bottom_y,
2519 int width, bool raised_p, bool top_p, bool bot_p,
2520 bool left_p, bool right_p,
2521 XRectangle *clip_rect)
2523 #ifdef USE_CAIRO
2524 GC top_left_gc, bottom_right_gc;
2525 int corners = 0;
2527 if (raised_p)
2529 top_left_gc = f->output_data.x->white_relief.gc;
2530 bottom_right_gc = f->output_data.x->black_relief.gc;
2532 else
2534 top_left_gc = f->output_data.x->black_relief.gc;
2535 bottom_right_gc = f->output_data.x->white_relief.gc;
2538 x_set_clip_rectangles (f, top_left_gc, clip_rect, 1);
2539 x_set_clip_rectangles (f, bottom_right_gc, clip_rect, 1);
2541 if (left_p)
2543 x_fill_rectangle (f, top_left_gc, left_x, top_y,
2544 width, bottom_y + 1 - top_y);
2545 if (top_p)
2546 corners |= 1 << CORNER_TOP_LEFT;
2547 if (bot_p)
2548 corners |= 1 << CORNER_BOTTOM_LEFT;
2550 if (right_p)
2552 x_fill_rectangle (f, bottom_right_gc, right_x + 1 - width, top_y,
2553 width, bottom_y + 1 - top_y);
2554 if (top_p)
2555 corners |= 1 << CORNER_TOP_RIGHT;
2556 if (bot_p)
2557 corners |= 1 << CORNER_BOTTOM_RIGHT;
2559 if (top_p)
2561 if (!right_p)
2562 x_fill_rectangle (f, top_left_gc, left_x, top_y,
2563 right_x + 1 - left_x, width);
2564 else
2565 x_fill_trapezoid_for_relief (f, top_left_gc, left_x, top_y,
2566 right_x + 1 - left_x, width, 1);
2568 if (bot_p)
2570 if (!left_p)
2571 x_fill_rectangle (f, bottom_right_gc, left_x, bottom_y + 1 - width,
2572 right_x + 1 - left_x, width);
2573 else
2574 x_fill_trapezoid_for_relief (f, bottom_right_gc,
2575 left_x, bottom_y + 1 - width,
2576 right_x + 1 - left_x, width, 0);
2578 if (left_p && width != 1)
2579 x_fill_rectangle (f, bottom_right_gc, left_x, top_y,
2580 1, bottom_y + 1 - top_y);
2581 if (top_p && width != 1)
2582 x_fill_rectangle (f, bottom_right_gc, left_x, top_y,
2583 right_x + 1 - left_x, 1);
2584 if (corners)
2586 XSetBackground (FRAME_X_DISPLAY (f), top_left_gc,
2587 FRAME_BACKGROUND_PIXEL (f));
2588 x_erase_corners_for_relief (f, top_left_gc, left_x, top_y,
2589 right_x - left_x + 1, bottom_y - top_y + 1,
2590 6, 1, corners);
2593 x_reset_clip_rectangles (f, top_left_gc);
2594 x_reset_clip_rectangles (f, bottom_right_gc);
2595 #else
2596 Display *dpy = FRAME_X_DISPLAY (f);
2597 Window window = FRAME_X_WINDOW (f);
2598 int i;
2599 GC gc;
2601 if (raised_p)
2602 gc = f->output_data.x->white_relief.gc;
2603 else
2604 gc = f->output_data.x->black_relief.gc;
2605 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2607 /* This code is more complicated than it has to be, because of two
2608 minor hacks to make the boxes look nicer: (i) if width > 1, draw
2609 the outermost line using the black relief. (ii) Omit the four
2610 corner pixels. */
2612 /* Top. */
2613 if (top_p)
2615 if (width == 1)
2616 XDrawLine (dpy, window, gc,
2617 left_x + left_p, top_y,
2618 right_x + !right_p, top_y);
2620 for (i = 1; i < width; ++i)
2621 XDrawLine (dpy, window, gc,
2622 left_x + i * left_p, top_y + i,
2623 right_x + 1 - i * right_p, top_y + i);
2626 /* Left. */
2627 if (left_p)
2629 if (width == 1)
2630 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
2632 XClearArea (dpy, window, left_x, top_y, 1, 1, False);
2633 XClearArea (dpy, window, left_x, bottom_y, 1, 1, False);
2635 for (i = (width > 1 ? 1 : 0); i < width; ++i)
2636 XDrawLine (dpy, window, gc,
2637 left_x + i, top_y + (i + 1) * top_p,
2638 left_x + i, bottom_y + 1 - (i + 1) * bot_p);
2641 XSetClipMask (dpy, gc, None);
2642 if (raised_p)
2643 gc = f->output_data.x->black_relief.gc;
2644 else
2645 gc = f->output_data.x->white_relief.gc;
2646 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2648 if (width > 1)
2650 /* Outermost top line. */
2651 if (top_p)
2652 XDrawLine (dpy, window, gc,
2653 left_x + left_p, top_y,
2654 right_x + !right_p, top_y);
2656 /* Outermost left line. */
2657 if (left_p)
2658 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
2661 /* Bottom. */
2662 if (bot_p)
2664 XDrawLine (dpy, window, gc,
2665 left_x + left_p, bottom_y,
2666 right_x + !right_p, bottom_y);
2667 for (i = 1; i < width; ++i)
2668 XDrawLine (dpy, window, gc,
2669 left_x + i * left_p, bottom_y - i,
2670 right_x + 1 - i * right_p, bottom_y - i);
2673 /* Right. */
2674 if (right_p)
2676 XClearArea (dpy, window, right_x, top_y, 1, 1, False);
2677 XClearArea (dpy, window, right_x, bottom_y, 1, 1, False);
2678 for (i = 0; i < width; ++i)
2679 XDrawLine (dpy, window, gc,
2680 right_x - i, top_y + (i + 1) * top_p,
2681 right_x - i, bottom_y + 1 - (i + 1) * bot_p);
2684 x_reset_clip_rectangles (f, gc);
2686 #endif
2690 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2691 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2692 draw, it must be >= 0. LEFT_P means draw a line on the
2693 left side of the rectangle. RIGHT_P means draw a line
2694 on the right side of the rectangle. CLIP_RECT is the clipping
2695 rectangle to use when drawing. */
2697 static void
2698 x_draw_box_rect (struct glyph_string *s,
2699 int left_x, int top_y, int right_x, int bottom_y, int width,
2700 bool left_p, bool right_p, XRectangle *clip_rect)
2702 XGCValues xgcv;
2704 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2705 XSetForeground (s->display, s->gc, s->face->box_color);
2706 x_set_clip_rectangles (s->f, s->gc, clip_rect, 1);
2708 /* Top. */
2709 x_fill_rectangle (s->f, s->gc,
2710 left_x, top_y, right_x - left_x + 1, width);
2712 /* Left. */
2713 if (left_p)
2714 x_fill_rectangle (s->f, s->gc,
2715 left_x, top_y, width, bottom_y - top_y + 1);
2717 /* Bottom. */
2718 x_fill_rectangle (s->f, s->gc,
2719 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2721 /* Right. */
2722 if (right_p)
2723 x_fill_rectangle (s->f, s->gc,
2724 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2726 XSetForeground (s->display, s->gc, xgcv.foreground);
2727 x_reset_clip_rectangles (s->f, s->gc);
2731 /* Draw a box around glyph string S. */
2733 static void
2734 x_draw_glyph_string_box (struct glyph_string *s)
2736 int width, left_x, right_x, top_y, bottom_y, last_x;
2737 bool raised_p, left_p, right_p;
2738 struct glyph *last_glyph;
2739 XRectangle clip_rect;
2741 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2742 ? WINDOW_RIGHT_EDGE_X (s->w)
2743 : window_box_right (s->w, s->area));
2745 /* The glyph that may have a right box line. */
2746 last_glyph = (s->cmp || s->img
2747 ? s->first_glyph
2748 : s->first_glyph + s->nchars - 1);
2750 width = eabs (s->face->box_line_width);
2751 raised_p = s->face->box == FACE_RAISED_BOX;
2752 left_x = s->x;
2753 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2754 ? last_x - 1
2755 : min (last_x, s->x + s->background_width) - 1);
2756 top_y = s->y;
2757 bottom_y = top_y + s->height - 1;
2759 left_p = (s->first_glyph->left_box_line_p
2760 || (s->hl == DRAW_MOUSE_FACE
2761 && (s->prev == NULL
2762 || s->prev->hl != s->hl)));
2763 right_p = (last_glyph->right_box_line_p
2764 || (s->hl == DRAW_MOUSE_FACE
2765 && (s->next == NULL
2766 || s->next->hl != s->hl)));
2768 get_glyph_string_clip_rect (s, &clip_rect);
2770 if (s->face->box == FACE_SIMPLE_BOX)
2771 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2772 left_p, right_p, &clip_rect);
2773 else
2775 x_setup_relief_colors (s);
2776 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2777 width, raised_p, true, true, left_p, right_p,
2778 &clip_rect);
2783 /* Draw foreground of image glyph string S. */
2785 static void
2786 x_draw_image_foreground (struct glyph_string *s)
2788 int x = s->x;
2789 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2791 /* If first glyph of S has a left box line, start drawing it to the
2792 right of that line. */
2793 if (s->face->box != FACE_NO_BOX
2794 && s->first_glyph->left_box_line_p
2795 && s->slice.x == 0)
2796 x += eabs (s->face->box_line_width);
2798 /* If there is a margin around the image, adjust x- and y-position
2799 by that margin. */
2800 if (s->slice.x == 0)
2801 x += s->img->hmargin;
2802 if (s->slice.y == 0)
2803 y += s->img->vmargin;
2805 if (s->img->pixmap)
2807 if (s->img->mask)
2809 /* We can't set both a clip mask and use XSetClipRectangles
2810 because the latter also sets a clip mask. We also can't
2811 trust on the shape extension to be available
2812 (XShapeCombineRegion). So, compute the rectangle to draw
2813 manually. */
2814 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2815 | GCFunction);
2816 XGCValues xgcv;
2817 XRectangle clip_rect, image_rect, r;
2819 xgcv.clip_mask = s->img->mask;
2820 xgcv.clip_x_origin = x;
2821 xgcv.clip_y_origin = y;
2822 xgcv.function = GXcopy;
2823 XChangeGC (s->display, s->gc, mask, &xgcv);
2825 get_glyph_string_clip_rect (s, &clip_rect);
2826 image_rect.x = x;
2827 image_rect.y = y;
2828 image_rect.width = s->slice.width;
2829 image_rect.height = s->slice.height;
2830 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2831 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2832 s->slice.x + r.x - x, s->slice.y + r.y - y,
2833 r.width, r.height, r.x, r.y);
2835 else
2837 XRectangle clip_rect, image_rect, r;
2839 get_glyph_string_clip_rect (s, &clip_rect);
2840 image_rect.x = x;
2841 image_rect.y = y;
2842 image_rect.width = s->slice.width;
2843 image_rect.height = s->slice.height;
2844 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2845 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2846 s->slice.x + r.x - x, s->slice.y + r.y - y,
2847 r.width, r.height, r.x, r.y);
2849 /* When the image has a mask, we can expect that at
2850 least part of a mouse highlight or a block cursor will
2851 be visible. If the image doesn't have a mask, make
2852 a block cursor visible by drawing a rectangle around
2853 the image. I believe it's looking better if we do
2854 nothing here for mouse-face. */
2855 if (s->hl == DRAW_CURSOR)
2857 int relief = eabs (s->img->relief);
2858 x_draw_rectangle (s->f, s->gc,
2859 x - relief, y - relief,
2860 s->slice.width + relief*2 - 1,
2861 s->slice.height + relief*2 - 1);
2865 else
2866 /* Draw a rectangle if image could not be loaded. */
2867 x_draw_rectangle (s->f, s->gc, x, y,
2868 s->slice.width - 1, s->slice.height - 1);
2872 /* Draw a relief around the image glyph string S. */
2874 static void
2875 x_draw_image_relief (struct glyph_string *s)
2877 int x1, y1, thick;
2878 bool raised_p, top_p, bot_p, left_p, right_p;
2879 int extra_x, extra_y;
2880 XRectangle r;
2881 int x = s->x;
2882 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2884 /* If first glyph of S has a left box line, start drawing it to the
2885 right of that line. */
2886 if (s->face->box != FACE_NO_BOX
2887 && s->first_glyph->left_box_line_p
2888 && s->slice.x == 0)
2889 x += eabs (s->face->box_line_width);
2891 /* If there is a margin around the image, adjust x- and y-position
2892 by that margin. */
2893 if (s->slice.x == 0)
2894 x += s->img->hmargin;
2895 if (s->slice.y == 0)
2896 y += s->img->vmargin;
2898 if (s->hl == DRAW_IMAGE_SUNKEN
2899 || s->hl == DRAW_IMAGE_RAISED)
2901 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2902 raised_p = s->hl == DRAW_IMAGE_RAISED;
2904 else
2906 thick = eabs (s->img->relief);
2907 raised_p = s->img->relief > 0;
2910 x1 = x + s->slice.width - 1;
2911 y1 = y + s->slice.height - 1;
2913 extra_x = extra_y = 0;
2914 if (s->face->id == TOOL_BAR_FACE_ID)
2916 if (CONSP (Vtool_bar_button_margin)
2917 && INTEGERP (XCAR (Vtool_bar_button_margin))
2918 && INTEGERP (XCDR (Vtool_bar_button_margin)))
2920 extra_x = XINT (XCAR (Vtool_bar_button_margin));
2921 extra_y = XINT (XCDR (Vtool_bar_button_margin));
2923 else if (INTEGERP (Vtool_bar_button_margin))
2924 extra_x = extra_y = XINT (Vtool_bar_button_margin);
2927 top_p = bot_p = left_p = right_p = false;
2929 if (s->slice.x == 0)
2930 x -= thick + extra_x, left_p = true;
2931 if (s->slice.y == 0)
2932 y -= thick + extra_y, top_p = true;
2933 if (s->slice.x + s->slice.width == s->img->width)
2934 x1 += thick + extra_x, right_p = true;
2935 if (s->slice.y + s->slice.height == s->img->height)
2936 y1 += thick + extra_y, bot_p = true;
2938 x_setup_relief_colors (s);
2939 get_glyph_string_clip_rect (s, &r);
2940 x_draw_relief_rect (s->f, x, y, x1, y1, thick, raised_p,
2941 top_p, bot_p, left_p, right_p, &r);
2945 /* Draw the foreground of image glyph string S to PIXMAP. */
2947 static void
2948 x_draw_image_foreground_1 (struct glyph_string *s, Pixmap pixmap)
2950 int x = 0;
2951 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2953 /* If first glyph of S has a left box line, start drawing it to the
2954 right of that line. */
2955 if (s->face->box != FACE_NO_BOX
2956 && s->first_glyph->left_box_line_p
2957 && s->slice.x == 0)
2958 x += eabs (s->face->box_line_width);
2960 /* If there is a margin around the image, adjust x- and y-position
2961 by that margin. */
2962 if (s->slice.x == 0)
2963 x += s->img->hmargin;
2964 if (s->slice.y == 0)
2965 y += s->img->vmargin;
2967 if (s->img->pixmap)
2969 if (s->img->mask)
2971 /* We can't set both a clip mask and use XSetClipRectangles
2972 because the latter also sets a clip mask. We also can't
2973 trust on the shape extension to be available
2974 (XShapeCombineRegion). So, compute the rectangle to draw
2975 manually. */
2976 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2977 | GCFunction);
2978 XGCValues xgcv;
2980 xgcv.clip_mask = s->img->mask;
2981 xgcv.clip_x_origin = x - s->slice.x;
2982 xgcv.clip_y_origin = y - s->slice.y;
2983 xgcv.function = GXcopy;
2984 XChangeGC (s->display, s->gc, mask, &xgcv);
2986 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2987 s->slice.x, s->slice.y,
2988 s->slice.width, s->slice.height, x, y);
2989 XSetClipMask (s->display, s->gc, None);
2991 else
2993 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2994 s->slice.x, s->slice.y,
2995 s->slice.width, s->slice.height, x, y);
2997 /* When the image has a mask, we can expect that at
2998 least part of a mouse highlight or a block cursor will
2999 be visible. If the image doesn't have a mask, make
3000 a block cursor visible by drawing a rectangle around
3001 the image. I believe it's looking better if we do
3002 nothing here for mouse-face. */
3003 if (s->hl == DRAW_CURSOR)
3005 int r = eabs (s->img->relief);
3006 x_draw_rectangle (s->f, s->gc, x - r, y - r,
3007 s->slice.width + r*2 - 1,
3008 s->slice.height + r*2 - 1);
3012 else
3013 /* Draw a rectangle if image could not be loaded. */
3014 x_draw_rectangle (s->f, s->gc, x, y,
3015 s->slice.width - 1, s->slice.height - 1);
3019 /* Draw part of the background of glyph string S. X, Y, W, and H
3020 give the rectangle to draw. */
3022 static void
3023 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
3025 if (s->stippled_p)
3027 /* Fill background with a stipple pattern. */
3028 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3029 x_fill_rectangle (s->f, s->gc, x, y, w, h);
3030 XSetFillStyle (s->display, s->gc, FillSolid);
3032 else
3033 x_clear_glyph_string_rect (s, x, y, w, h);
3037 /* Draw image glyph string S.
3039 s->y
3040 s->x +-------------------------
3041 | s->face->box
3043 | +-------------------------
3044 | | s->img->margin
3046 | | +-------------------
3047 | | | the image
3051 static void
3052 x_draw_image_glyph_string (struct glyph_string *s)
3054 int box_line_hwidth = eabs (s->face->box_line_width);
3055 int box_line_vwidth = max (s->face->box_line_width, 0);
3056 int height;
3057 Pixmap pixmap = None;
3059 height = s->height;
3060 if (s->slice.y == 0)
3061 height -= box_line_vwidth;
3062 if (s->slice.y + s->slice.height >= s->img->height)
3063 height -= box_line_vwidth;
3065 /* Fill background with face under the image. Do it only if row is
3066 taller than image or if image has a clip mask to reduce
3067 flickering. */
3068 s->stippled_p = s->face->stipple != 0;
3069 if (height > s->slice.height
3070 || s->img->hmargin
3071 || s->img->vmargin
3072 || s->img->mask
3073 || s->img->pixmap == 0
3074 || s->width != s->background_width)
3076 if (s->img->mask)
3078 /* Create a pixmap as large as the glyph string. Fill it
3079 with the background color. Copy the image to it, using
3080 its mask. Copy the temporary pixmap to the display. */
3081 Screen *screen = FRAME_X_SCREEN (s->f);
3082 int depth = DefaultDepthOfScreen (screen);
3084 /* Create a pixmap as large as the glyph string. */
3085 pixmap = XCreatePixmap (s->display, s->window,
3086 s->background_width,
3087 s->height, depth);
3089 /* Don't clip in the following because we're working on the
3090 pixmap. */
3091 XSetClipMask (s->display, s->gc, None);
3093 /* Fill the pixmap with the background color/stipple. */
3094 if (s->stippled_p)
3096 /* Fill background with a stipple pattern. */
3097 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3098 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
3099 XFillRectangle (s->display, pixmap, s->gc,
3100 0, 0, s->background_width, s->height);
3101 XSetFillStyle (s->display, s->gc, FillSolid);
3102 XSetTSOrigin (s->display, s->gc, 0, 0);
3104 else
3106 XGCValues xgcv;
3107 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3108 &xgcv);
3109 XSetForeground (s->display, s->gc, xgcv.background);
3110 XFillRectangle (s->display, pixmap, s->gc,
3111 0, 0, s->background_width, s->height);
3112 XSetForeground (s->display, s->gc, xgcv.foreground);
3115 else
3117 int x = s->x;
3118 int y = s->y;
3119 int width = s->background_width;
3121 if (s->first_glyph->left_box_line_p
3122 && s->slice.x == 0)
3124 x += box_line_hwidth;
3125 width -= box_line_hwidth;
3128 if (s->slice.y == 0)
3129 y += box_line_vwidth;
3131 x_draw_glyph_string_bg_rect (s, x, y, width, height);
3134 s->background_filled_p = true;
3137 /* Draw the foreground. */
3138 #ifdef USE_CAIRO
3139 if (s->img->cr_data)
3141 cairo_t *cr = x_begin_cr_clip (s->f, s->gc);
3143 int x = s->x + s->img->hmargin;
3144 int y = s->y + s->img->vmargin;
3145 int width = s->background_width;
3147 cairo_set_source_surface (cr, s->img->cr_data,
3148 x - s->slice.x,
3149 y - s->slice.y);
3150 cairo_rectangle (cr, x, y, width, height);
3151 cairo_fill (cr);
3152 x_end_cr_clip (s->f);
3154 else
3155 #endif
3156 if (pixmap != None)
3158 x_draw_image_foreground_1 (s, pixmap);
3159 x_set_glyph_string_clipping (s);
3160 XCopyArea (s->display, pixmap, s->window, s->gc,
3161 0, 0, s->background_width, s->height, s->x, s->y);
3162 XFreePixmap (s->display, pixmap);
3164 else
3165 x_draw_image_foreground (s);
3167 /* If we must draw a relief around the image, do it. */
3168 if (s->img->relief
3169 || s->hl == DRAW_IMAGE_RAISED
3170 || s->hl == DRAW_IMAGE_SUNKEN)
3171 x_draw_image_relief (s);
3175 /* Draw stretch glyph string S. */
3177 static void
3178 x_draw_stretch_glyph_string (struct glyph_string *s)
3180 eassert (s->first_glyph->type == STRETCH_GLYPH);
3182 if (s->hl == DRAW_CURSOR
3183 && !x_stretch_cursor_p)
3185 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
3186 wide as the stretch glyph. */
3187 int width, background_width = s->background_width;
3188 int x = s->x;
3190 if (!s->row->reversed_p)
3192 int left_x = window_box_left_offset (s->w, TEXT_AREA);
3194 if (x < left_x)
3196 background_width -= left_x - x;
3197 x = left_x;
3200 else
3202 /* In R2L rows, draw the cursor on the right edge of the
3203 stretch glyph. */
3204 int right_x = window_box_right (s->w, TEXT_AREA);
3206 if (x + background_width > right_x)
3207 background_width -= x - right_x;
3208 x += background_width;
3210 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
3211 if (s->row->reversed_p)
3212 x -= width;
3214 /* Draw cursor. */
3215 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
3217 /* Clear rest using the GC of the original non-cursor face. */
3218 if (width < background_width)
3220 int y = s->y;
3221 int w = background_width - width, h = s->height;
3222 XRectangle r;
3223 GC gc;
3225 if (!s->row->reversed_p)
3226 x += width;
3227 else
3228 x = s->x;
3229 if (s->row->mouse_face_p
3230 && cursor_in_mouse_face_p (s->w))
3232 x_set_mouse_face_gc (s);
3233 gc = s->gc;
3235 else
3236 gc = s->face->gc;
3238 get_glyph_string_clip_rect (s, &r);
3239 x_set_clip_rectangles (s->f, gc, &r, 1);
3241 if (s->face->stipple)
3243 /* Fill background with a stipple pattern. */
3244 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3245 x_fill_rectangle (s->f, gc, x, y, w, h);
3246 XSetFillStyle (s->display, gc, FillSolid);
3248 else
3250 XGCValues xgcv;
3251 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3252 XSetForeground (s->display, gc, xgcv.background);
3253 x_fill_rectangle (s->f, gc, x, y, w, h);
3254 XSetForeground (s->display, gc, xgcv.foreground);
3257 x_reset_clip_rectangles (s->f, gc);
3260 else if (!s->background_filled_p)
3262 int background_width = s->background_width;
3263 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
3265 /* Don't draw into left margin, fringe or scrollbar area
3266 except for header line and mode line. */
3267 if (x < left_x && !s->row->mode_line_p)
3269 background_width -= left_x - x;
3270 x = left_x;
3272 if (background_width > 0)
3273 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
3276 s->background_filled_p = true;
3280 Draw a wavy line under S. The wave fills wave_height pixels from y0.
3282 x0 wave_length = 2
3284 y0 * * * * *
3285 |* * * * * * * * *
3286 wave_height = 3 | * * * *
3290 static void
3291 x_draw_underwave (struct glyph_string *s)
3293 int wave_height = 3, wave_length = 2;
3294 #ifdef USE_CAIRO
3295 x_draw_horizontal_wave (s->f, s->gc, s->x, s->ybase - wave_height + 3,
3296 s->width, wave_height, wave_length);
3297 #else /* not USE_CAIRO */
3298 int dx, dy, x0, y0, width, x1, y1, x2, y2, xmax;
3299 bool odd;
3300 XRectangle wave_clip, string_clip, final_clip;
3302 dx = wave_length;
3303 dy = wave_height - 1;
3304 x0 = s->x;
3305 y0 = s->ybase - wave_height + 3;
3306 width = s->width;
3307 xmax = x0 + width;
3309 /* Find and set clipping rectangle */
3311 wave_clip.x = x0;
3312 wave_clip.y = y0;
3313 wave_clip.width = width;
3314 wave_clip.height = wave_height;
3315 get_glyph_string_clip_rect (s, &string_clip);
3317 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
3318 return;
3320 XSetClipRectangles (s->display, s->gc, 0, 0, &final_clip, 1, Unsorted);
3322 /* Draw the waves */
3324 x1 = x0 - (x0 % dx);
3325 x2 = x1 + dx;
3326 odd = (x1 / dx) & 1;
3327 y1 = y2 = y0;
3329 if (odd)
3330 y1 += dy;
3331 else
3332 y2 += dy;
3334 if (INT_MAX - dx < xmax)
3335 emacs_abort ();
3337 while (x1 <= xmax)
3339 XDrawLine (s->display, s->window, s->gc, x1, y1, x2, y2);
3340 x1 = x2, y1 = y2;
3341 x2 += dx, y2 = y0 + odd*dy;
3342 odd = !odd;
3345 /* Restore previous clipping rectangle(s) */
3346 XSetClipRectangles (s->display, s->gc, 0, 0, s->clip, s->num_clips, Unsorted);
3347 #endif /* not USE_CAIRO */
3351 /* Draw glyph string S. */
3353 static void
3354 x_draw_glyph_string (struct glyph_string *s)
3356 bool relief_drawn_p = false;
3358 /* If S draws into the background of its successors, draw the
3359 background of the successors first so that S can draw into it.
3360 This makes S->next use XDrawString instead of XDrawImageString. */
3361 if (s->next && s->right_overhang && !s->for_overlaps)
3363 int width;
3364 struct glyph_string *next;
3366 for (width = 0, next = s->next;
3367 next && width < s->right_overhang;
3368 width += next->width, next = next->next)
3369 if (next->first_glyph->type != IMAGE_GLYPH)
3371 x_set_glyph_string_gc (next);
3372 x_set_glyph_string_clipping (next);
3373 if (next->first_glyph->type == STRETCH_GLYPH)
3374 x_draw_stretch_glyph_string (next);
3375 else
3376 x_draw_glyph_string_background (next, true);
3377 next->num_clips = 0;
3381 /* Set up S->gc, set clipping and draw S. */
3382 x_set_glyph_string_gc (s);
3384 /* Draw relief (if any) in advance for char/composition so that the
3385 glyph string can be drawn over it. */
3386 if (!s->for_overlaps
3387 && s->face->box != FACE_NO_BOX
3388 && (s->first_glyph->type == CHAR_GLYPH
3389 || s->first_glyph->type == COMPOSITE_GLYPH))
3392 x_set_glyph_string_clipping (s);
3393 x_draw_glyph_string_background (s, true);
3394 x_draw_glyph_string_box (s);
3395 x_set_glyph_string_clipping (s);
3396 relief_drawn_p = true;
3398 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
3399 && !s->clip_tail
3400 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
3401 || (s->next && s->next->hl != s->hl && s->right_overhang)))
3402 /* We must clip just this glyph. left_overhang part has already
3403 drawn when s->prev was drawn, and right_overhang part will be
3404 drawn later when s->next is drawn. */
3405 x_set_glyph_string_clipping_exactly (s, s);
3406 else
3407 x_set_glyph_string_clipping (s);
3409 switch (s->first_glyph->type)
3411 case IMAGE_GLYPH:
3412 x_draw_image_glyph_string (s);
3413 break;
3415 case STRETCH_GLYPH:
3416 x_draw_stretch_glyph_string (s);
3417 break;
3419 case CHAR_GLYPH:
3420 if (s->for_overlaps)
3421 s->background_filled_p = true;
3422 else
3423 x_draw_glyph_string_background (s, false);
3424 x_draw_glyph_string_foreground (s);
3425 break;
3427 case COMPOSITE_GLYPH:
3428 if (s->for_overlaps || (s->cmp_from > 0
3429 && ! s->first_glyph->u.cmp.automatic))
3430 s->background_filled_p = true;
3431 else
3432 x_draw_glyph_string_background (s, true);
3433 x_draw_composite_glyph_string_foreground (s);
3434 break;
3436 case GLYPHLESS_GLYPH:
3437 if (s->for_overlaps)
3438 s->background_filled_p = true;
3439 else
3440 x_draw_glyph_string_background (s, true);
3441 x_draw_glyphless_glyph_string_foreground (s);
3442 break;
3444 default:
3445 emacs_abort ();
3448 if (!s->for_overlaps)
3450 /* Draw underline. */
3451 if (s->face->underline_p)
3453 if (s->face->underline_type == FACE_UNDER_WAVE)
3455 if (s->face->underline_defaulted_p)
3456 x_draw_underwave (s);
3457 else
3459 XGCValues xgcv;
3460 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3461 XSetForeground (s->display, s->gc, s->face->underline_color);
3462 x_draw_underwave (s);
3463 XSetForeground (s->display, s->gc, xgcv.foreground);
3466 else if (s->face->underline_type == FACE_UNDER_LINE)
3468 unsigned long thickness, position;
3469 int y;
3471 if (s->prev && s->prev->face->underline_p
3472 && s->prev->face->underline_type == FACE_UNDER_LINE)
3474 /* We use the same underline style as the previous one. */
3475 thickness = s->prev->underline_thickness;
3476 position = s->prev->underline_position;
3478 else
3480 /* Get the underline thickness. Default is 1 pixel. */
3481 if (s->font && s->font->underline_thickness > 0)
3482 thickness = s->font->underline_thickness;
3483 else
3484 thickness = 1;
3485 if (x_underline_at_descent_line)
3486 position = (s->height - thickness) - (s->ybase - s->y);
3487 else
3489 /* Get the underline position. This is the recommended
3490 vertical offset in pixels from the baseline to the top of
3491 the underline. This is a signed value according to the
3492 specs, and its default is
3494 ROUND ((maximum descent) / 2), with
3495 ROUND(x) = floor (x + 0.5) */
3497 if (x_use_underline_position_properties
3498 && s->font && s->font->underline_position >= 0)
3499 position = s->font->underline_position;
3500 else if (s->font)
3501 position = (s->font->descent + 1) / 2;
3502 else
3503 position = underline_minimum_offset;
3505 position = max (position, underline_minimum_offset);
3507 /* Check the sanity of thickness and position. We should
3508 avoid drawing underline out of the current line area. */
3509 if (s->y + s->height <= s->ybase + position)
3510 position = (s->height - 1) - (s->ybase - s->y);
3511 if (s->y + s->height < s->ybase + position + thickness)
3512 thickness = (s->y + s->height) - (s->ybase + position);
3513 s->underline_thickness = thickness;
3514 s->underline_position = position;
3515 y = s->ybase + position;
3516 if (s->face->underline_defaulted_p)
3517 x_fill_rectangle (s->f, s->gc,
3518 s->x, y, s->width, thickness);
3519 else
3521 XGCValues xgcv;
3522 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3523 XSetForeground (s->display, s->gc, s->face->underline_color);
3524 x_fill_rectangle (s->f, s->gc,
3525 s->x, y, s->width, thickness);
3526 XSetForeground (s->display, s->gc, xgcv.foreground);
3530 /* Draw overline. */
3531 if (s->face->overline_p)
3533 unsigned long dy = 0, h = 1;
3535 if (s->face->overline_color_defaulted_p)
3536 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3537 s->width, h);
3538 else
3540 XGCValues xgcv;
3541 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3542 XSetForeground (s->display, s->gc, s->face->overline_color);
3543 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3544 s->width, h);
3545 XSetForeground (s->display, s->gc, xgcv.foreground);
3549 /* Draw strike-through. */
3550 if (s->face->strike_through_p)
3552 unsigned long h = 1;
3553 unsigned long dy = (s->height - h) / 2;
3555 if (s->face->strike_through_color_defaulted_p)
3556 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3557 s->width, h);
3558 else
3560 XGCValues xgcv;
3561 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3562 XSetForeground (s->display, s->gc, s->face->strike_through_color);
3563 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3564 s->width, h);
3565 XSetForeground (s->display, s->gc, xgcv.foreground);
3569 /* Draw relief if not yet drawn. */
3570 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
3571 x_draw_glyph_string_box (s);
3573 if (s->prev)
3575 struct glyph_string *prev;
3577 for (prev = s->prev; prev; prev = prev->prev)
3578 if (prev->hl != s->hl
3579 && prev->x + prev->width + prev->right_overhang > s->x)
3581 /* As prev was drawn while clipped to its own area, we
3582 must draw the right_overhang part using s->hl now. */
3583 enum draw_glyphs_face save = prev->hl;
3585 prev->hl = s->hl;
3586 x_set_glyph_string_gc (prev);
3587 x_set_glyph_string_clipping_exactly (s, prev);
3588 if (prev->first_glyph->type == CHAR_GLYPH)
3589 x_draw_glyph_string_foreground (prev);
3590 else
3591 x_draw_composite_glyph_string_foreground (prev);
3592 x_reset_clip_rectangles (prev->f, prev->gc);
3593 prev->hl = save;
3594 prev->num_clips = 0;
3598 if (s->next)
3600 struct glyph_string *next;
3602 for (next = s->next; next; next = next->next)
3603 if (next->hl != s->hl
3604 && next->x - next->left_overhang < s->x + s->width)
3606 /* As next will be drawn while clipped to its own area,
3607 we must draw the left_overhang part using s->hl now. */
3608 enum draw_glyphs_face save = next->hl;
3610 next->hl = s->hl;
3611 x_set_glyph_string_gc (next);
3612 x_set_glyph_string_clipping_exactly (s, next);
3613 if (next->first_glyph->type == CHAR_GLYPH)
3614 x_draw_glyph_string_foreground (next);
3615 else
3616 x_draw_composite_glyph_string_foreground (next);
3617 x_reset_clip_rectangles (next->f, next->gc);
3618 next->hl = save;
3619 next->num_clips = 0;
3620 next->clip_head = s->next;
3625 /* Reset clipping. */
3626 x_reset_clip_rectangles (s->f, s->gc);
3627 s->num_clips = 0;
3630 /* Shift display to make room for inserted glyphs. */
3632 static void
3633 x_shift_glyphs_for_insert (struct frame *f, int x, int y, int width, int height, int shift_by)
3635 /* Never called on a GUI frame, see
3636 http://lists.gnu.org/archive/html/emacs-devel/2015-05/msg00456.html
3638 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3639 f->output_data.x->normal_gc,
3640 x, y, width, height,
3641 x + shift_by, y);
3644 /* Delete N glyphs at the nominal cursor position. Not implemented
3645 for X frames. */
3647 static void
3648 x_delete_glyphs (struct frame *f, register int n)
3650 emacs_abort ();
3654 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
3655 If they are <= 0, this is probably an error. */
3657 static void
3658 x_clear_area1 (Display *dpy, Window window,
3659 int x, int y, int width, int height, int exposures)
3661 eassert (width > 0 && height > 0);
3662 XClearArea (dpy, window, x, y, width, height, exposures);
3666 void
3667 x_clear_area (struct frame *f, int x, int y, int width, int height)
3669 #ifdef USE_CAIRO
3670 cairo_t *cr;
3672 eassert (width > 0 && height > 0);
3674 cr = x_begin_cr_clip (f, NULL);
3675 x_set_cr_source_with_gc_background (f, f->output_data.x->normal_gc);
3676 cairo_rectangle (cr, x, y, width, height);
3677 cairo_fill (cr);
3678 x_end_cr_clip (f);
3679 #else
3680 x_clear_area1 (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3681 x, y, width, height, False);
3682 #endif
3686 /* Clear an entire frame. */
3688 static void
3689 x_clear_frame (struct frame *f)
3691 /* Clearing the frame will erase any cursor, so mark them all as no
3692 longer visible. */
3693 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
3695 block_input ();
3697 x_clear_window (f);
3699 /* We have to clear the scroll bars. If we have changed colors or
3700 something like that, then they should be notified. */
3701 x_scroll_bar_clear (f);
3703 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3704 /* Make sure scroll bars are redrawn. As they aren't redrawn by
3705 redisplay, do it here. */
3706 if (FRAME_GTK_WIDGET (f))
3707 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
3708 #endif
3710 XFlush (FRAME_X_DISPLAY (f));
3712 unblock_input ();
3715 /* RIF: Show hourglass cursor on frame F. */
3717 static void
3718 x_show_hourglass (struct frame *f)
3720 Display *dpy = FRAME_X_DISPLAY (f);
3722 if (dpy)
3724 struct x_output *x = FRAME_X_OUTPUT (f);
3725 #ifdef USE_X_TOOLKIT
3726 if (x->widget)
3727 #else
3728 if (FRAME_OUTER_WINDOW (f))
3729 #endif
3731 x->hourglass_p = true;
3733 if (!x->hourglass_window)
3735 unsigned long mask = CWCursor;
3736 XSetWindowAttributes attrs;
3737 #ifdef USE_GTK
3738 Window parent = FRAME_X_WINDOW (f);
3739 #else
3740 Window parent = FRAME_OUTER_WINDOW (f);
3741 #endif
3742 attrs.cursor = x->hourglass_cursor;
3744 x->hourglass_window = XCreateWindow
3745 (dpy, parent, 0, 0, 32000, 32000, 0, 0,
3746 InputOnly, CopyFromParent, mask, &attrs);
3749 XMapRaised (dpy, x->hourglass_window);
3750 XFlush (dpy);
3755 /* RIF: Cancel hourglass cursor on frame F. */
3757 static void
3758 x_hide_hourglass (struct frame *f)
3760 struct x_output *x = FRAME_X_OUTPUT (f);
3762 /* Watch out for newly created frames. */
3763 if (x->hourglass_window)
3765 XUnmapWindow (FRAME_X_DISPLAY (f), x->hourglass_window);
3766 /* Sync here because XTread_socket looks at the
3767 hourglass_p flag that is reset to zero below. */
3768 XSync (FRAME_X_DISPLAY (f), False);
3769 x->hourglass_p = false;
3773 /* Invert the middle quarter of the frame for .15 sec. */
3775 static void
3776 XTflash (struct frame *f)
3778 block_input ();
3781 #ifdef USE_GTK
3782 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
3783 when the scroll bars and the edit widget share the same X window. */
3784 GdkWindow *window = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
3785 #ifdef HAVE_GTK3
3786 cairo_t *cr = gdk_cairo_create (window);
3787 cairo_set_source_rgb (cr, 1, 1, 1);
3788 cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
3789 #define XFillRectangle(d, win, gc, x, y, w, h) \
3790 do { \
3791 cairo_rectangle (cr, x, y, w, h); \
3792 cairo_fill (cr); \
3794 while (false)
3795 #else /* ! HAVE_GTK3 */
3796 GdkGCValues vals;
3797 GdkGC *gc;
3798 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
3799 ^ FRAME_BACKGROUND_PIXEL (f));
3800 vals.function = GDK_XOR;
3801 gc = gdk_gc_new_with_values (window,
3802 &vals, GDK_GC_FUNCTION | GDK_GC_FOREGROUND);
3803 #define XFillRectangle(d, win, gc, x, y, w, h) \
3804 gdk_draw_rectangle (window, gc, true, x, y, w, h)
3805 #endif /* ! HAVE_GTK3 */
3806 #else /* ! USE_GTK */
3807 GC gc;
3809 /* Create a GC that will use the GXxor function to flip foreground
3810 pixels into background pixels. */
3812 XGCValues values;
3814 values.function = GXxor;
3815 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3816 ^ FRAME_BACKGROUND_PIXEL (f));
3818 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3819 GCFunction | GCForeground, &values);
3821 #endif
3823 /* Get the height not including a menu bar widget. */
3824 int height = FRAME_PIXEL_HEIGHT (f);
3825 /* Height of each line to flash. */
3826 int flash_height = FRAME_LINE_HEIGHT (f);
3827 /* These will be the left and right margins of the rectangles. */
3828 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3829 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3830 int width = flash_right - flash_left;
3832 /* If window is tall, flash top and bottom line. */
3833 if (height > 3 * FRAME_LINE_HEIGHT (f))
3835 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3836 flash_left,
3837 (FRAME_INTERNAL_BORDER_WIDTH (f)
3838 + FRAME_TOP_MARGIN_HEIGHT (f)),
3839 width, flash_height);
3840 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3841 flash_left,
3842 (height - flash_height
3843 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3844 width, flash_height);
3847 else
3848 /* If it is short, flash it all. */
3849 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3850 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3851 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3853 x_flush (f);
3856 struct timespec delay = make_timespec (0, 150 * 1000 * 1000);
3857 struct timespec wakeup = timespec_add (current_timespec (), delay);
3859 /* Keep waiting until past the time wakeup or any input gets
3860 available. */
3861 while (! detect_input_pending ())
3863 struct timespec current = current_timespec ();
3864 struct timespec timeout;
3866 /* Break if result would not be positive. */
3867 if (timespec_cmp (wakeup, current) <= 0)
3868 break;
3870 /* How long `select' should wait. */
3871 timeout = make_timespec (0, 10 * 1000 * 1000);
3873 /* Try to wait that long--but we might wake up sooner. */
3874 pselect (0, NULL, NULL, NULL, &timeout, NULL);
3878 /* If window is tall, flash top and bottom line. */
3879 if (height > 3 * FRAME_LINE_HEIGHT (f))
3881 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3882 flash_left,
3883 (FRAME_INTERNAL_BORDER_WIDTH (f)
3884 + FRAME_TOP_MARGIN_HEIGHT (f)),
3885 width, flash_height);
3886 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3887 flash_left,
3888 (height - flash_height
3889 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3890 width, flash_height);
3892 else
3893 /* If it is short, flash it all. */
3894 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3895 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3896 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3898 #ifdef USE_GTK
3899 #ifdef HAVE_GTK3
3900 cairo_destroy (cr);
3901 #else
3902 g_object_unref (G_OBJECT (gc));
3903 #endif
3904 #undef XFillRectangle
3905 #else
3906 XFreeGC (FRAME_X_DISPLAY (f), gc);
3907 #endif
3908 x_flush (f);
3912 unblock_input ();
3916 static void
3917 XTtoggle_invisible_pointer (struct frame *f, bool invisible)
3919 block_input ();
3920 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, invisible);
3921 unblock_input ();
3925 /* Make audible bell. */
3927 static void
3928 XTring_bell (struct frame *f)
3930 if (FRAME_X_DISPLAY (f))
3932 if (visible_bell)
3933 XTflash (f);
3934 else
3936 block_input ();
3937 #ifdef HAVE_XKB
3938 XkbBell (FRAME_X_DISPLAY (f), None, 0, None);
3939 #else
3940 XBell (FRAME_X_DISPLAY (f), 0);
3941 #endif
3942 XFlush (FRAME_X_DISPLAY (f));
3943 unblock_input ();
3948 /***********************************************************************
3949 Line Dance
3950 ***********************************************************************/
3952 /* Perform an insert-lines or delete-lines operation, inserting N
3953 lines or deleting -N lines at vertical position VPOS. */
3955 static void
3956 x_ins_del_lines (struct frame *f, int vpos, int n)
3958 emacs_abort ();
3962 /* Scroll part of the display as described by RUN. */
3964 static void
3965 x_scroll_run (struct window *w, struct run *run)
3967 struct frame *f = XFRAME (w->frame);
3968 int x, y, width, height, from_y, to_y, bottom_y;
3970 /* Get frame-relative bounding box of the text display area of W,
3971 without mode lines. Include in this box the left and right
3972 fringe of W. */
3973 window_box (w, ANY_AREA, &x, &y, &width, &height);
3975 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3976 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3977 bottom_y = y + height;
3979 if (to_y < from_y)
3981 /* Scrolling up. Make sure we don't copy part of the mode
3982 line at the bottom. */
3983 if (from_y + run->height > bottom_y)
3984 height = bottom_y - from_y;
3985 else
3986 height = run->height;
3988 else
3990 /* Scrolling down. Make sure we don't copy over the mode line.
3991 at the bottom. */
3992 if (to_y + run->height > bottom_y)
3993 height = bottom_y - to_y;
3994 else
3995 height = run->height;
3998 block_input ();
4000 /* Cursor off. Will be switched on again in x_update_window_end. */
4001 x_clear_cursor (w);
4003 #ifdef USE_CAIRO
4004 SET_FRAME_GARBAGED (f);
4005 #else
4006 XCopyArea (FRAME_X_DISPLAY (f),
4007 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4008 f->output_data.x->normal_gc,
4009 x, from_y,
4010 width, height,
4011 x, to_y);
4012 #endif
4014 unblock_input ();
4019 /***********************************************************************
4020 Exposure Events
4021 ***********************************************************************/
4024 static void
4025 frame_highlight (struct frame *f)
4027 /* We used to only do this if Vx_no_window_manager was non-nil, but
4028 the ICCCM (section 4.1.6) says that the window's border pixmap
4029 and border pixel are window attributes which are "private to the
4030 client", so we can always change it to whatever we want. */
4031 block_input ();
4032 /* I recently started to get errors in this XSetWindowBorder, depending on
4033 the window-manager in use, tho something more is at play since I've been
4034 using that same window-manager binary for ever. Let's not crash just
4035 because of this (bug#9310). */
4036 x_catch_errors (FRAME_X_DISPLAY (f));
4037 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4038 f->output_data.x->border_pixel);
4039 x_uncatch_errors ();
4040 unblock_input ();
4041 x_update_cursor (f, true);
4042 x_set_frame_alpha (f);
4045 static void
4046 frame_unhighlight (struct frame *f)
4048 /* We used to only do this if Vx_no_window_manager was non-nil, but
4049 the ICCCM (section 4.1.6) says that the window's border pixmap
4050 and border pixel are window attributes which are "private to the
4051 client", so we can always change it to whatever we want. */
4052 block_input ();
4053 /* Same as above for XSetWindowBorder (bug#9310). */
4054 x_catch_errors (FRAME_X_DISPLAY (f));
4055 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4056 f->output_data.x->border_tile);
4057 x_uncatch_errors ();
4058 unblock_input ();
4059 x_update_cursor (f, true);
4060 x_set_frame_alpha (f);
4063 /* The focus has changed. Update the frames as necessary to reflect
4064 the new situation. Note that we can't change the selected frame
4065 here, because the Lisp code we are interrupting might become confused.
4066 Each event gets marked with the frame in which it occurred, so the
4067 Lisp code can tell when the switch took place by examining the events. */
4069 static void
4070 x_new_focus_frame (struct x_display_info *dpyinfo, struct frame *frame)
4072 struct frame *old_focus = dpyinfo->x_focus_frame;
4074 if (frame != dpyinfo->x_focus_frame)
4076 /* Set this before calling other routines, so that they see
4077 the correct value of x_focus_frame. */
4078 dpyinfo->x_focus_frame = frame;
4080 if (old_focus && old_focus->auto_lower)
4081 x_lower_frame (old_focus);
4083 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
4084 dpyinfo->x_pending_autoraise_frame = dpyinfo->x_focus_frame;
4085 else
4086 dpyinfo->x_pending_autoraise_frame = NULL;
4089 x_frame_rehighlight (dpyinfo);
4092 /* Handle FocusIn and FocusOut state changes for FRAME.
4093 If FRAME has focus and there exists more than one frame, puts
4094 a FOCUS_IN_EVENT into *BUFP. */
4096 static void
4097 x_focus_changed (int type, int state, struct x_display_info *dpyinfo, struct frame *frame, struct input_event *bufp)
4099 if (type == FocusIn)
4101 if (dpyinfo->x_focus_event_frame != frame)
4103 x_new_focus_frame (dpyinfo, frame);
4104 dpyinfo->x_focus_event_frame = frame;
4106 /* Don't stop displaying the initial startup message
4107 for a switch-frame event we don't need. */
4108 /* When run as a daemon, Vterminal_frame is always NIL. */
4109 bufp->arg = (((NILP (Vterminal_frame)
4110 || ! FRAME_X_P (XFRAME (Vterminal_frame))
4111 || EQ (Fdaemonp (), Qt))
4112 && CONSP (Vframe_list)
4113 && !NILP (XCDR (Vframe_list)))
4114 ? Qt : Qnil);
4115 bufp->kind = FOCUS_IN_EVENT;
4116 XSETFRAME (bufp->frame_or_window, frame);
4119 frame->output_data.x->focus_state |= state;
4121 #ifdef HAVE_X_I18N
4122 if (FRAME_XIC (frame))
4123 XSetICFocus (FRAME_XIC (frame));
4124 #endif
4126 else if (type == FocusOut)
4128 frame->output_data.x->focus_state &= ~state;
4130 if (dpyinfo->x_focus_event_frame == frame)
4132 dpyinfo->x_focus_event_frame = 0;
4133 x_new_focus_frame (dpyinfo, 0);
4135 bufp->kind = FOCUS_OUT_EVENT;
4136 XSETFRAME (bufp->frame_or_window, frame);
4139 #ifdef HAVE_X_I18N
4140 if (FRAME_XIC (frame))
4141 XUnsetICFocus (FRAME_XIC (frame));
4142 #endif
4143 if (frame->pointer_invisible)
4144 XTtoggle_invisible_pointer (frame, false);
4148 /* Return the Emacs frame-object corresponding to an X window.
4149 It could be the frame's main window or an icon window. */
4151 static struct frame *
4152 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4154 Lisp_Object tail, frame;
4155 struct frame *f;
4157 if (wdesc == None)
4158 return NULL;
4160 FOR_EACH_FRAME (tail, frame)
4162 f = XFRAME (frame);
4163 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4164 continue;
4165 if (f->output_data.x->hourglass_window == wdesc)
4166 return f;
4167 #ifdef USE_X_TOOLKIT
4168 if ((f->output_data.x->edit_widget
4169 && XtWindow (f->output_data.x->edit_widget) == wdesc)
4170 /* A tooltip frame? */
4171 || (!f->output_data.x->edit_widget
4172 && FRAME_X_WINDOW (f) == wdesc)
4173 || f->output_data.x->icon_desc == wdesc)
4174 return f;
4175 #else /* not USE_X_TOOLKIT */
4176 #ifdef USE_GTK
4177 if (f->output_data.x->edit_widget)
4179 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4180 struct x_output *x = f->output_data.x;
4181 if (gwdesc != 0 && gwdesc == x->edit_widget)
4182 return f;
4184 #endif /* USE_GTK */
4185 if (FRAME_X_WINDOW (f) == wdesc
4186 || f->output_data.x->icon_desc == wdesc)
4187 return f;
4188 #endif /* not USE_X_TOOLKIT */
4190 return 0;
4193 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
4195 /* Like x_window_to_frame but also compares the window with the widget's
4196 windows. */
4198 static struct frame *
4199 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4201 Lisp_Object tail, frame;
4202 struct frame *f, *found = NULL;
4203 struct x_output *x;
4205 if (wdesc == None)
4206 return NULL;
4208 FOR_EACH_FRAME (tail, frame)
4210 if (found)
4211 break;
4212 f = XFRAME (frame);
4213 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
4215 /* This frame matches if the window is any of its widgets. */
4216 x = f->output_data.x;
4217 if (x->hourglass_window == wdesc)
4218 found = f;
4219 else if (x->widget)
4221 #ifdef USE_GTK
4222 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4223 if (gwdesc != 0
4224 && gtk_widget_get_toplevel (gwdesc) == x->widget)
4225 found = f;
4226 #else
4227 if (wdesc == XtWindow (x->widget)
4228 || wdesc == XtWindow (x->column_widget)
4229 || wdesc == XtWindow (x->edit_widget))
4230 found = f;
4231 /* Match if the window is this frame's menubar. */
4232 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
4233 found = f;
4234 #endif
4236 else if (FRAME_X_WINDOW (f) == wdesc)
4237 /* A tooltip frame. */
4238 found = f;
4242 return found;
4245 /* Likewise, but consider only the menu bar widget. */
4247 static struct frame *
4248 x_menubar_window_to_frame (struct x_display_info *dpyinfo,
4249 const XEvent *event)
4251 Window wdesc = event->xany.window;
4252 Lisp_Object tail, frame;
4253 struct frame *f;
4254 struct x_output *x;
4256 if (wdesc == None)
4257 return NULL;
4259 FOR_EACH_FRAME (tail, frame)
4261 f = XFRAME (frame);
4262 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4263 continue;
4264 x = f->output_data.x;
4265 #ifdef USE_GTK
4266 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
4267 return f;
4268 #else
4269 /* Match if the window is this frame's menubar. */
4270 if (x->menubar_widget
4271 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
4272 return f;
4273 #endif
4275 return 0;
4278 /* Return the frame whose principal (outermost) window is WDESC.
4279 If WDESC is some other (smaller) window, we return 0. */
4281 struct frame *
4282 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4284 Lisp_Object tail, frame;
4285 struct frame *f;
4286 struct x_output *x;
4288 if (wdesc == None)
4289 return NULL;
4291 FOR_EACH_FRAME (tail, frame)
4293 f = XFRAME (frame);
4294 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4295 continue;
4296 x = f->output_data.x;
4298 if (x->widget)
4300 /* This frame matches if the window is its topmost widget. */
4301 #ifdef USE_GTK
4302 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4303 if (gwdesc == x->widget)
4304 return f;
4305 #else
4306 if (wdesc == XtWindow (x->widget))
4307 return f;
4308 #endif
4310 else if (FRAME_X_WINDOW (f) == wdesc)
4311 /* Tooltip frame. */
4312 return f;
4314 return 0;
4317 #else /* !USE_X_TOOLKIT && !USE_GTK */
4319 #define x_any_window_to_frame(d, i) x_window_to_frame (d, i)
4320 #define x_top_window_to_frame(d, i) x_window_to_frame (d, i)
4322 #endif /* USE_X_TOOLKIT || USE_GTK */
4324 /* The focus may have changed. Figure out if it is a real focus change,
4325 by checking both FocusIn/Out and Enter/LeaveNotify events.
4327 Returns FOCUS_IN_EVENT event in *BUFP. */
4329 static void
4330 x_detect_focus_change (struct x_display_info *dpyinfo, struct frame *frame,
4331 const XEvent *event, struct input_event *bufp)
4333 if (!frame)
4334 return;
4336 switch (event->type)
4338 case EnterNotify:
4339 case LeaveNotify:
4341 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
4342 int focus_state
4343 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
4345 if (event->xcrossing.detail != NotifyInferior
4346 && event->xcrossing.focus
4347 && ! (focus_state & FOCUS_EXPLICIT))
4348 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
4349 FOCUS_IMPLICIT,
4350 dpyinfo, frame, bufp);
4352 break;
4354 case FocusIn:
4355 case FocusOut:
4356 x_focus_changed (event->type,
4357 (event->xfocus.detail == NotifyPointer ?
4358 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
4359 dpyinfo, frame, bufp);
4360 break;
4362 case ClientMessage:
4363 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
4365 enum xembed_message msg = event->xclient.data.l[1];
4366 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
4367 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
4369 break;
4374 #if !defined USE_X_TOOLKIT && !defined USE_GTK
4375 /* Handle an event saying the mouse has moved out of an Emacs frame. */
4377 void
4378 x_mouse_leave (struct x_display_info *dpyinfo)
4380 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
4382 #endif
4384 /* The focus has changed, or we have redirected a frame's focus to
4385 another frame (this happens when a frame uses a surrogate
4386 mini-buffer frame). Shift the highlight as appropriate.
4388 The FRAME argument doesn't necessarily have anything to do with which
4389 frame is being highlighted or un-highlighted; we only use it to find
4390 the appropriate X display info. */
4392 static void
4393 XTframe_rehighlight (struct frame *frame)
4395 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
4398 static void
4399 x_frame_rehighlight (struct x_display_info *dpyinfo)
4401 struct frame *old_highlight = dpyinfo->x_highlight_frame;
4403 if (dpyinfo->x_focus_frame)
4405 dpyinfo->x_highlight_frame
4406 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
4407 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
4408 : dpyinfo->x_focus_frame);
4409 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
4411 fset_focus_frame (dpyinfo->x_focus_frame, Qnil);
4412 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
4415 else
4416 dpyinfo->x_highlight_frame = 0;
4418 if (dpyinfo->x_highlight_frame != old_highlight)
4420 if (old_highlight)
4421 frame_unhighlight (old_highlight);
4422 if (dpyinfo->x_highlight_frame)
4423 frame_highlight (dpyinfo->x_highlight_frame);
4429 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
4431 /* Initialize mode_switch_bit and modifier_meaning. */
4432 static void
4433 x_find_modifier_meanings (struct x_display_info *dpyinfo)
4435 int min_code, max_code;
4436 KeySym *syms;
4437 int syms_per_code;
4438 XModifierKeymap *mods;
4440 dpyinfo->meta_mod_mask = 0;
4441 dpyinfo->shift_lock_mask = 0;
4442 dpyinfo->alt_mod_mask = 0;
4443 dpyinfo->super_mod_mask = 0;
4444 dpyinfo->hyper_mod_mask = 0;
4446 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
4448 syms = XGetKeyboardMapping (dpyinfo->display,
4449 min_code, max_code - min_code + 1,
4450 &syms_per_code);
4451 mods = XGetModifierMapping (dpyinfo->display);
4453 /* Scan the modifier table to see which modifier bits the Meta and
4454 Alt keysyms are on. */
4456 int row, col; /* The row and column in the modifier table. */
4457 bool found_alt_or_meta;
4459 for (row = 3; row < 8; row++)
4461 found_alt_or_meta = false;
4462 for (col = 0; col < mods->max_keypermod; col++)
4464 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
4466 /* Zeroes are used for filler. Skip them. */
4467 if (code == 0)
4468 continue;
4470 /* Are any of this keycode's keysyms a meta key? */
4472 int code_col;
4474 for (code_col = 0; code_col < syms_per_code; code_col++)
4476 int sym = syms[((code - min_code) * syms_per_code) + code_col];
4478 switch (sym)
4480 case XK_Meta_L:
4481 case XK_Meta_R:
4482 found_alt_or_meta = true;
4483 dpyinfo->meta_mod_mask |= (1 << row);
4484 break;
4486 case XK_Alt_L:
4487 case XK_Alt_R:
4488 found_alt_or_meta = true;
4489 dpyinfo->alt_mod_mask |= (1 << row);
4490 break;
4492 case XK_Hyper_L:
4493 case XK_Hyper_R:
4494 if (!found_alt_or_meta)
4495 dpyinfo->hyper_mod_mask |= (1 << row);
4496 code_col = syms_per_code;
4497 col = mods->max_keypermod;
4498 break;
4500 case XK_Super_L:
4501 case XK_Super_R:
4502 if (!found_alt_or_meta)
4503 dpyinfo->super_mod_mask |= (1 << row);
4504 code_col = syms_per_code;
4505 col = mods->max_keypermod;
4506 break;
4508 case XK_Shift_Lock:
4509 /* Ignore this if it's not on the lock modifier. */
4510 if (!found_alt_or_meta && ((1 << row) == LockMask))
4511 dpyinfo->shift_lock_mask = LockMask;
4512 code_col = syms_per_code;
4513 col = mods->max_keypermod;
4514 break;
4522 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
4523 if (! dpyinfo->meta_mod_mask)
4525 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
4526 dpyinfo->alt_mod_mask = 0;
4529 /* If some keys are both alt and meta,
4530 make them just meta, not alt. */
4531 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
4533 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
4536 XFree (syms);
4537 XFreeModifiermap (mods);
4540 /* Convert between the modifier bits X uses and the modifier bits
4541 Emacs uses. */
4544 x_x_to_emacs_modifiers (struct x_display_info *dpyinfo, int state)
4546 int mod_meta = meta_modifier;
4547 int mod_alt = alt_modifier;
4548 int mod_hyper = hyper_modifier;
4549 int mod_super = super_modifier;
4550 Lisp_Object tem;
4552 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4553 if (INTEGERP (tem)) mod_alt = XINT (tem) & INT_MAX;
4554 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4555 if (INTEGERP (tem)) mod_meta = XINT (tem) & INT_MAX;
4556 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4557 if (INTEGERP (tem)) mod_hyper = XINT (tem) & INT_MAX;
4558 tem = Fget (Vx_super_keysym, Qmodifier_value);
4559 if (INTEGERP (tem)) mod_super = XINT (tem) & INT_MAX;
4561 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
4562 | ((state & ControlMask) ? ctrl_modifier : 0)
4563 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
4564 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
4565 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
4566 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
4569 static int
4570 x_emacs_to_x_modifiers (struct x_display_info *dpyinfo, EMACS_INT state)
4572 EMACS_INT mod_meta = meta_modifier;
4573 EMACS_INT mod_alt = alt_modifier;
4574 EMACS_INT mod_hyper = hyper_modifier;
4575 EMACS_INT mod_super = super_modifier;
4577 Lisp_Object tem;
4579 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4580 if (INTEGERP (tem)) mod_alt = XINT (tem);
4581 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4582 if (INTEGERP (tem)) mod_meta = XINT (tem);
4583 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4584 if (INTEGERP (tem)) mod_hyper = XINT (tem);
4585 tem = Fget (Vx_super_keysym, Qmodifier_value);
4586 if (INTEGERP (tem)) mod_super = XINT (tem);
4589 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
4590 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
4591 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
4592 | ((state & shift_modifier) ? ShiftMask : 0)
4593 | ((state & ctrl_modifier) ? ControlMask : 0)
4594 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
4597 /* Convert a keysym to its name. */
4599 char *
4600 x_get_keysym_name (int keysym)
4602 char *value;
4604 block_input ();
4605 value = XKeysymToString (keysym);
4606 unblock_input ();
4608 return value;
4611 /* Mouse clicks and mouse movement. Rah.
4613 Formerly, we used PointerMotionHintMask (in standard_event_mask)
4614 so that we would have to call XQueryPointer after each MotionNotify
4615 event to ask for another such event. However, this made mouse tracking
4616 slow, and there was a bug that made it eventually stop.
4618 Simply asking for MotionNotify all the time seems to work better.
4620 In order to avoid asking for motion events and then throwing most
4621 of them away or busy-polling the server for mouse positions, we ask
4622 the server for pointer motion hints. This means that we get only
4623 one event per group of mouse movements. "Groups" are delimited by
4624 other kinds of events (focus changes and button clicks, for
4625 example), or by XQueryPointer calls; when one of these happens, we
4626 get another MotionNotify event the next time the mouse moves. This
4627 is at least as efficient as getting motion events when mouse
4628 tracking is on, and I suspect only negligibly worse when tracking
4629 is off. */
4631 /* Prepare a mouse-event in *RESULT for placement in the input queue.
4633 If the event is a button press, then note that we have grabbed
4634 the mouse. */
4636 static Lisp_Object
4637 construct_mouse_click (struct input_event *result,
4638 const XButtonEvent *event,
4639 struct frame *f)
4641 /* Make the event type NO_EVENT; we'll change that when we decide
4642 otherwise. */
4643 result->kind = MOUSE_CLICK_EVENT;
4644 result->code = event->button - Button1;
4645 result->timestamp = event->time;
4646 result->modifiers = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f),
4647 event->state)
4648 | (event->type == ButtonRelease
4649 ? up_modifier
4650 : down_modifier));
4652 XSETINT (result->x, event->x);
4653 XSETINT (result->y, event->y);
4654 XSETFRAME (result->frame_or_window, f);
4655 result->arg = Qnil;
4656 return Qnil;
4659 /* Function to report a mouse movement to the mainstream Emacs code.
4660 The input handler calls this.
4662 We have received a mouse movement event, which is given in *event.
4663 If the mouse is over a different glyph than it was last time, tell
4664 the mainstream emacs code by setting mouse_moved. If not, ask for
4665 another motion event, so we can check again the next time it moves. */
4667 static bool
4668 note_mouse_movement (struct frame *frame, const XMotionEvent *event)
4670 XRectangle *r;
4671 struct x_display_info *dpyinfo;
4673 if (!FRAME_X_OUTPUT (frame))
4674 return false;
4676 dpyinfo = FRAME_DISPLAY_INFO (frame);
4677 dpyinfo->last_mouse_movement_time = event->time;
4678 dpyinfo->last_mouse_motion_frame = frame;
4679 dpyinfo->last_mouse_motion_x = event->x;
4680 dpyinfo->last_mouse_motion_y = event->y;
4682 if (event->window != FRAME_X_WINDOW (frame))
4684 frame->mouse_moved = true;
4685 dpyinfo->last_mouse_scroll_bar = NULL;
4686 note_mouse_highlight (frame, -1, -1);
4687 dpyinfo->last_mouse_glyph_frame = NULL;
4688 return true;
4692 /* Has the mouse moved off the glyph it was on at the last sighting? */
4693 r = &dpyinfo->last_mouse_glyph;
4694 if (frame != dpyinfo->last_mouse_glyph_frame
4695 || event->x < r->x || event->x >= r->x + r->width
4696 || event->y < r->y || event->y >= r->y + r->height)
4698 frame->mouse_moved = true;
4699 dpyinfo->last_mouse_scroll_bar = NULL;
4700 note_mouse_highlight (frame, event->x, event->y);
4701 /* Remember which glyph we're now on. */
4702 remember_mouse_glyph (frame, event->x, event->y, r);
4703 dpyinfo->last_mouse_glyph_frame = frame;
4704 return true;
4707 return false;
4710 /* Return the current position of the mouse.
4711 *FP should be a frame which indicates which display to ask about.
4713 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
4714 and *PART to the frame, window, and scroll bar part that the mouse
4715 is over. Set *X and *Y to the portion and whole of the mouse's
4716 position on the scroll bar.
4718 If the mouse movement started elsewhere, set *FP to the frame the
4719 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
4720 the mouse is over.
4722 Set *TIMESTAMP to the server time-stamp for the time at which the mouse
4723 was at this position.
4725 Don't store anything if we don't have a valid set of values to report.
4727 This clears the mouse_moved flag, so we can wait for the next mouse
4728 movement. */
4730 static void
4731 XTmouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
4732 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
4733 Time *timestamp)
4735 struct frame *f1;
4736 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
4738 block_input ();
4740 if (dpyinfo->last_mouse_scroll_bar && insist == 0)
4742 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
4744 if (bar->horizontal)
4745 x_horizontal_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
4746 else
4747 x_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
4749 else
4751 Window root;
4752 int root_x, root_y;
4754 Window dummy_window;
4755 int dummy;
4757 Lisp_Object frame, tail;
4759 /* Clear the mouse-moved flag for every frame on this display. */
4760 FOR_EACH_FRAME (tail, frame)
4761 if (FRAME_X_P (XFRAME (frame))
4762 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
4763 XFRAME (frame)->mouse_moved = false;
4765 dpyinfo->last_mouse_scroll_bar = NULL;
4767 /* Figure out which root window we're on. */
4768 XQueryPointer (FRAME_X_DISPLAY (*fp),
4769 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
4771 /* The root window which contains the pointer. */
4772 &root,
4774 /* Trash which we can't trust if the pointer is on
4775 a different screen. */
4776 &dummy_window,
4778 /* The position on that root window. */
4779 &root_x, &root_y,
4781 /* More trash we can't trust. */
4782 &dummy, &dummy,
4784 /* Modifier keys and pointer buttons, about which
4785 we don't care. */
4786 (unsigned int *) &dummy);
4788 /* Now we have a position on the root; find the innermost window
4789 containing the pointer. */
4791 Window win, child;
4792 int win_x, win_y;
4793 int parent_x = 0, parent_y = 0;
4795 win = root;
4797 /* XTranslateCoordinates can get errors if the window
4798 structure is changing at the same time this function
4799 is running. So at least we must not crash from them. */
4801 x_catch_errors (FRAME_X_DISPLAY (*fp));
4803 if (x_mouse_grabbed (dpyinfo))
4805 /* If mouse was grabbed on a frame, give coords for that frame
4806 even if the mouse is now outside it. */
4807 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
4809 /* From-window. */
4810 root,
4812 /* To-window. */
4813 FRAME_X_WINDOW (dpyinfo->last_mouse_frame),
4815 /* From-position, to-position. */
4816 root_x, root_y, &win_x, &win_y,
4818 /* Child of win. */
4819 &child);
4820 f1 = dpyinfo->last_mouse_frame;
4822 else
4824 while (true)
4826 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
4828 /* From-window, to-window. */
4829 root, win,
4831 /* From-position, to-position. */
4832 root_x, root_y, &win_x, &win_y,
4834 /* Child of win. */
4835 &child);
4837 if (child == None || child == win)
4838 break;
4839 #ifdef USE_GTK
4840 /* We don't wan't to know the innermost window. We
4841 want the edit window. For non-Gtk+ the innermost
4842 window is the edit window. For Gtk+ it might not
4843 be. It might be the tool bar for example. */
4844 if (x_window_to_frame (dpyinfo, win))
4845 break;
4846 #endif
4847 win = child;
4848 parent_x = win_x;
4849 parent_y = win_y;
4852 /* Now we know that:
4853 win is the innermost window containing the pointer
4854 (XTC says it has no child containing the pointer),
4855 win_x and win_y are the pointer's position in it
4856 (XTC did this the last time through), and
4857 parent_x and parent_y are the pointer's position in win's parent.
4858 (They are what win_x and win_y were when win was child.
4859 If win is the root window, it has no parent, and
4860 parent_{x,y} are invalid, but that's okay, because we'll
4861 never use them in that case.) */
4863 #ifdef USE_GTK
4864 /* We don't wan't to know the innermost window. We
4865 want the edit window. */
4866 f1 = x_window_to_frame (dpyinfo, win);
4867 #else
4868 /* Is win one of our frames? */
4869 f1 = x_any_window_to_frame (dpyinfo, win);
4870 #endif
4872 #ifdef USE_X_TOOLKIT
4873 /* If we end up with the menu bar window, say it's not
4874 on the frame. */
4875 if (f1 != NULL
4876 && f1->output_data.x->menubar_widget
4877 && win == XtWindow (f1->output_data.x->menubar_widget))
4878 f1 = NULL;
4879 #endif /* USE_X_TOOLKIT */
4882 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
4883 f1 = 0;
4885 x_uncatch_errors ();
4887 /* If not, is it one of our scroll bars? */
4888 if (! f1)
4890 struct scroll_bar *bar;
4892 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win, 2);
4894 if (bar)
4896 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4897 win_x = parent_x;
4898 win_y = parent_y;
4902 if (f1 == 0 && insist > 0)
4903 f1 = SELECTED_FRAME ();
4905 if (f1)
4907 /* Ok, we found a frame. Store all the values.
4908 last_mouse_glyph is a rectangle used to reduce the
4909 generation of mouse events. To not miss any motion
4910 events, we must divide the frame into rectangles of the
4911 size of the smallest character that could be displayed
4912 on it, i.e. into the same rectangles that matrices on
4913 the frame are divided into. */
4915 /* FIXME: what if F1 is not an X frame? */
4916 dpyinfo = FRAME_DISPLAY_INFO (f1);
4917 remember_mouse_glyph (f1, win_x, win_y, &dpyinfo->last_mouse_glyph);
4918 dpyinfo->last_mouse_glyph_frame = f1;
4920 *bar_window = Qnil;
4921 *part = 0;
4922 *fp = f1;
4923 XSETINT (*x, win_x);
4924 XSETINT (*y, win_y);
4925 *timestamp = dpyinfo->last_mouse_movement_time;
4930 unblock_input ();
4935 /***********************************************************************
4936 Scroll bars
4937 ***********************************************************************/
4939 /* Scroll bar support. */
4941 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
4942 manages it.
4943 This can be called in GC, so we have to make sure to strip off mark
4944 bits. */
4946 static struct scroll_bar *
4947 x_window_to_scroll_bar (Display *display, Window window_id, int type)
4949 Lisp_Object tail, frame;
4951 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4952 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4953 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4955 FOR_EACH_FRAME (tail, frame)
4957 Lisp_Object bar, condemned;
4959 if (! FRAME_X_P (XFRAME (frame)))
4960 continue;
4962 /* Scan this frame's scroll bar list for a scroll bar with the
4963 right window ID. */
4964 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4965 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4966 /* This trick allows us to search both the ordinary and
4967 condemned scroll bar lists with one loop. */
4968 ! NILP (bar) || (bar = condemned,
4969 condemned = Qnil,
4970 ! NILP (bar));
4971 bar = XSCROLL_BAR (bar)->next)
4972 if (XSCROLL_BAR (bar)->x_window == window_id
4973 && FRAME_X_DISPLAY (XFRAME (frame)) == display
4974 && (type = 2
4975 || (type == 1 && XSCROLL_BAR (bar)->horizontal)
4976 || (type == 0 && !XSCROLL_BAR (bar)->horizontal)))
4977 return XSCROLL_BAR (bar);
4980 return NULL;
4984 #if defined USE_LUCID
4986 /* Return the Lucid menu bar WINDOW is part of. Return null
4987 if WINDOW is not part of a menu bar. */
4989 static Widget
4990 x_window_to_menu_bar (Window window)
4992 Lisp_Object tail, frame;
4994 FOR_EACH_FRAME (tail, frame)
4995 if (FRAME_X_P (XFRAME (frame)))
4997 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4999 if (menu_bar && xlwmenu_window_p (menu_bar, window))
5000 return menu_bar;
5002 return NULL;
5005 #endif /* USE_LUCID */
5008 /************************************************************************
5009 Toolkit scroll bars
5010 ************************************************************************/
5012 #ifdef USE_TOOLKIT_SCROLL_BARS
5014 static void x_send_scroll_bar_event (Lisp_Object, enum scroll_bar_part,
5015 int, int, bool);
5017 /* Lisp window being scrolled. Set when starting to interact with
5018 a toolkit scroll bar, reset to nil when ending the interaction. */
5020 static Lisp_Object window_being_scrolled;
5022 /* Whether this is an Xaw with arrow-scrollbars. This should imply
5023 that movements of 1/20 of the screen size are mapped to up/down. */
5025 #ifndef USE_GTK
5026 /* Id of action hook installed for scroll bars. */
5028 static XtActionHookId action_hook_id;
5029 static XtActionHookId horizontal_action_hook_id;
5031 static Boolean xaw3d_arrow_scroll;
5033 /* Whether the drag scrolling maintains the mouse at the top of the
5034 thumb. If not, resizing the thumb needs to be done more carefully
5035 to avoid jerkiness. */
5037 static Boolean xaw3d_pick_top;
5039 /* Action hook installed via XtAppAddActionHook when toolkit scroll
5040 bars are used.. The hook is responsible for detecting when
5041 the user ends an interaction with the scroll bar, and generates
5042 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
5044 static void
5045 xt_action_hook (Widget widget, XtPointer client_data, String action_name,
5046 XEvent *event, String *params, Cardinal *num_params)
5048 bool scroll_bar_p;
5049 const char *end_action;
5051 #ifdef USE_MOTIF
5052 scroll_bar_p = XmIsScrollBar (widget);
5053 end_action = "Release";
5054 #else /* !USE_MOTIF i.e. use Xaw */
5055 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
5056 end_action = "EndScroll";
5057 #endif /* USE_MOTIF */
5059 if (scroll_bar_p
5060 && strcmp (action_name, end_action) == 0
5061 && WINDOWP (window_being_scrolled))
5063 struct window *w;
5064 struct scroll_bar *bar;
5066 x_send_scroll_bar_event (window_being_scrolled,
5067 scroll_bar_end_scroll, 0, 0, false);
5068 w = XWINDOW (window_being_scrolled);
5069 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5071 if (bar->dragging != -1)
5073 bar->dragging = -1;
5074 /* The thumb size is incorrect while dragging: fix it. */
5075 set_vertical_scroll_bar (w);
5077 window_being_scrolled = Qnil;
5078 #if defined (USE_LUCID)
5079 bar->last_seen_part = scroll_bar_nowhere;
5080 #endif
5081 /* Xt timeouts no longer needed. */
5082 toolkit_scroll_bar_interaction = false;
5087 static void
5088 xt_horizontal_action_hook (Widget widget, XtPointer client_data, String action_name,
5089 XEvent *event, String *params, Cardinal *num_params)
5091 bool scroll_bar_p;
5092 const char *end_action;
5094 #ifdef USE_MOTIF
5095 scroll_bar_p = XmIsScrollBar (widget);
5096 end_action = "Release";
5097 #else /* !USE_MOTIF i.e. use Xaw */
5098 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
5099 end_action = "EndScroll";
5100 #endif /* USE_MOTIF */
5102 if (scroll_bar_p
5103 && strcmp (action_name, end_action) == 0
5104 && WINDOWP (window_being_scrolled))
5106 struct window *w;
5107 struct scroll_bar *bar;
5109 x_send_scroll_bar_event (window_being_scrolled,
5110 scroll_bar_end_scroll, 0, 0, true);
5111 w = XWINDOW (window_being_scrolled);
5112 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
5114 if (bar->dragging != -1)
5116 bar->dragging = -1;
5117 /* The thumb size is incorrect while dragging: fix it. */
5118 set_horizontal_scroll_bar (w);
5120 window_being_scrolled = Qnil;
5121 #if defined (USE_LUCID)
5122 bar->last_seen_part = scroll_bar_nowhere;
5123 #endif
5124 /* Xt timeouts no longer needed. */
5125 toolkit_scroll_bar_interaction = false;
5128 #endif /* not USE_GTK */
5130 /* Send a client message with message type Xatom_Scrollbar for a
5131 scroll action to the frame of WINDOW. PART is a value identifying
5132 the part of the scroll bar that was clicked on. PORTION is the
5133 amount to scroll of a whole of WHOLE. */
5135 static void
5136 x_send_scroll_bar_event (Lisp_Object window, enum scroll_bar_part part,
5137 int portion, int whole, bool horizontal)
5139 XEvent event;
5140 XClientMessageEvent *ev = &event.xclient;
5141 struct window *w = XWINDOW (window);
5142 struct frame *f = XFRAME (w->frame);
5143 intptr_t iw = (intptr_t) w;
5144 enum { BITS_PER_INTPTR = CHAR_BIT * sizeof iw };
5145 verify (BITS_PER_INTPTR <= 64);
5146 int sign_shift = BITS_PER_INTPTR - 32;
5148 block_input ();
5150 /* Construct a ClientMessage event to send to the frame. */
5151 ev->type = ClientMessage;
5152 ev->message_type = (horizontal
5153 ? FRAME_DISPLAY_INFO (f)->Xatom_Horizontal_Scrollbar
5154 : FRAME_DISPLAY_INFO (f)->Xatom_Scrollbar);
5155 ev->display = FRAME_X_DISPLAY (f);
5156 ev->window = FRAME_X_WINDOW (f);
5157 ev->format = 32;
5159 /* A 32-bit X client on a 64-bit X server can pass a window pointer
5160 as-is. A 64-bit client on a 32-bit X server is in trouble
5161 because a pointer does not fit and would be truncated while
5162 passing through the server. So use two slots and hope that X12
5163 will resolve such issues someday. */
5164 ev->data.l[0] = iw >> 31 >> 1;
5165 ev->data.l[1] = sign_shift <= 0 ? iw : iw << sign_shift >> sign_shift;
5166 ev->data.l[2] = part;
5167 ev->data.l[3] = portion;
5168 ev->data.l[4] = whole;
5170 /* Make Xt timeouts work while the scroll bar is active. */
5171 #ifdef USE_X_TOOLKIT
5172 toolkit_scroll_bar_interaction = true;
5173 x_activate_timeout_atimer ();
5174 #endif
5176 /* Setting the event mask to zero means that the message will
5177 be sent to the client that created the window, and if that
5178 window no longer exists, no event will be sent. */
5179 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
5180 unblock_input ();
5184 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
5185 in *IEVENT. */
5187 static void
5188 x_scroll_bar_to_input_event (const XEvent *event,
5189 struct input_event *ievent)
5191 const XClientMessageEvent *ev = &event->xclient;
5192 Lisp_Object window;
5193 struct window *w;
5195 /* See the comment in the function above. */
5196 intptr_t iw0 = ev->data.l[0];
5197 intptr_t iw1 = ev->data.l[1];
5198 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
5199 w = (struct window *) iw;
5201 XSETWINDOW (window, w);
5203 ievent->kind = SCROLL_BAR_CLICK_EVENT;
5204 ievent->frame_or_window = window;
5205 ievent->arg = Qnil;
5206 #ifdef USE_GTK
5207 ievent->timestamp = CurrentTime;
5208 #else
5209 ievent->timestamp =
5210 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
5211 #endif
5212 ievent->code = 0;
5213 ievent->part = ev->data.l[2];
5214 ievent->x = make_number (ev->data.l[3]);
5215 ievent->y = make_number (ev->data.l[4]);
5216 ievent->modifiers = 0;
5219 /* Transform a horizontal scroll bar ClientMessage EVENT to an Emacs
5220 input event in *IEVENT. */
5222 static void
5223 x_horizontal_scroll_bar_to_input_event (const XEvent *event,
5224 struct input_event *ievent)
5226 const XClientMessageEvent *ev = &event->xclient;
5227 Lisp_Object window;
5228 struct window *w;
5230 /* See the comment in the function above. */
5231 intptr_t iw0 = ev->data.l[0];
5232 intptr_t iw1 = ev->data.l[1];
5233 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
5234 w = (struct window *) iw;
5236 XSETWINDOW (window, w);
5238 ievent->kind = HORIZONTAL_SCROLL_BAR_CLICK_EVENT;
5239 ievent->frame_or_window = window;
5240 ievent->arg = Qnil;
5241 #ifdef USE_GTK
5242 ievent->timestamp = CurrentTime;
5243 #else
5244 ievent->timestamp =
5245 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
5246 #endif
5247 ievent->code = 0;
5248 ievent->part = ev->data.l[2];
5249 ievent->x = make_number (ev->data.l[3]);
5250 ievent->y = make_number (ev->data.l[4]);
5251 ievent->modifiers = 0;
5255 #ifdef USE_MOTIF
5257 /* Minimum and maximum values used for Motif scroll bars. */
5259 #define XM_SB_MAX 10000000
5261 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
5262 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
5263 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
5265 static void
5266 xm_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5268 struct scroll_bar *bar = client_data;
5269 XmScrollBarCallbackStruct *cs = call_data;
5270 enum scroll_bar_part part = scroll_bar_nowhere;
5271 bool horizontal = bar->horizontal;
5272 int whole = 0, portion = 0;
5274 switch (cs->reason)
5276 case XmCR_DECREMENT:
5277 bar->dragging = -1;
5278 part = horizontal ? scroll_bar_left_arrow : scroll_bar_up_arrow;
5279 break;
5281 case XmCR_INCREMENT:
5282 bar->dragging = -1;
5283 part = horizontal ? scroll_bar_right_arrow : scroll_bar_down_arrow;
5284 break;
5286 case XmCR_PAGE_DECREMENT:
5287 bar->dragging = -1;
5288 part = horizontal ? scroll_bar_before_handle : scroll_bar_above_handle;
5289 break;
5291 case XmCR_PAGE_INCREMENT:
5292 bar->dragging = -1;
5293 part = horizontal ? scroll_bar_after_handle : scroll_bar_below_handle;
5294 break;
5296 case XmCR_TO_TOP:
5297 bar->dragging = -1;
5298 part = horizontal ? scroll_bar_to_leftmost : scroll_bar_to_top;
5299 break;
5301 case XmCR_TO_BOTTOM:
5302 bar->dragging = -1;
5303 part = horizontal ? scroll_bar_to_rightmost : scroll_bar_to_bottom;
5304 break;
5306 case XmCR_DRAG:
5308 int slider_size;
5310 block_input ();
5311 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
5312 unblock_input ();
5314 if (horizontal)
5316 portion = bar->whole * ((float)cs->value / XM_SB_MAX);
5317 whole = bar->whole * ((float)(XM_SB_MAX - slider_size) / XM_SB_MAX);
5318 portion = min (portion, whole);
5319 part = scroll_bar_horizontal_handle;
5321 else
5323 whole = XM_SB_MAX - slider_size;
5324 portion = min (cs->value, whole);
5325 part = scroll_bar_handle;
5328 bar->dragging = cs->value;
5330 break;
5332 case XmCR_VALUE_CHANGED:
5333 break;
5336 if (part != scroll_bar_nowhere)
5338 window_being_scrolled = bar->window;
5339 x_send_scroll_bar_event (bar->window, part, portion, whole,
5340 bar->horizontal);
5344 #elif defined USE_GTK
5346 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
5347 bar widget. DATA is a pointer to the scroll_bar structure. */
5349 static gboolean
5350 xg_scroll_callback (GtkRange *range,
5351 GtkScrollType scroll,
5352 gdouble value,
5353 gpointer user_data)
5355 int whole = 0, portion = 0;
5356 struct scroll_bar *bar = user_data;
5357 enum scroll_bar_part part = scroll_bar_nowhere;
5358 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
5359 struct frame *f = g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
5361 if (xg_ignore_gtk_scrollbar) return false;
5363 switch (scroll)
5365 case GTK_SCROLL_JUMP:
5366 /* Buttons 1 2 or 3 must be grabbed. */
5367 if (FRAME_DISPLAY_INFO (f)->grabbed != 0
5368 && FRAME_DISPLAY_INFO (f)->grabbed < (1 << 4))
5370 if (bar->horizontal)
5372 part = scroll_bar_horizontal_handle;
5373 whole = (int)(gtk_adjustment_get_upper (adj) -
5374 gtk_adjustment_get_page_size (adj));
5375 portion = min ((int)value, whole);
5376 bar->dragging = portion;
5378 else
5380 part = scroll_bar_handle;
5381 whole = gtk_adjustment_get_upper (adj) -
5382 gtk_adjustment_get_page_size (adj);
5383 portion = min ((int)value, whole);
5384 bar->dragging = portion;
5387 break;
5388 case GTK_SCROLL_STEP_BACKWARD:
5389 part = (bar->horizontal
5390 ? scroll_bar_left_arrow : scroll_bar_up_arrow);
5391 bar->dragging = -1;
5392 break;
5393 case GTK_SCROLL_STEP_FORWARD:
5394 part = (bar->horizontal
5395 ? scroll_bar_right_arrow : scroll_bar_down_arrow);
5396 bar->dragging = -1;
5397 break;
5398 case GTK_SCROLL_PAGE_BACKWARD:
5399 part = (bar->horizontal
5400 ? scroll_bar_before_handle : scroll_bar_above_handle);
5401 bar->dragging = -1;
5402 break;
5403 case GTK_SCROLL_PAGE_FORWARD:
5404 part = (bar->horizontal
5405 ? scroll_bar_after_handle : scroll_bar_below_handle);
5406 bar->dragging = -1;
5407 break;
5408 default:
5409 break;
5412 if (part != scroll_bar_nowhere)
5414 window_being_scrolled = bar->window;
5415 x_send_scroll_bar_event (bar->window, part, portion, whole,
5416 bar->horizontal);
5419 return false;
5422 /* Callback for button release. Sets dragging to -1 when dragging is done. */
5424 static gboolean
5425 xg_end_scroll_callback (GtkWidget *widget,
5426 GdkEventButton *event,
5427 gpointer user_data)
5429 struct scroll_bar *bar = user_data;
5430 bar->dragging = -1;
5431 if (WINDOWP (window_being_scrolled))
5433 x_send_scroll_bar_event (window_being_scrolled,
5434 scroll_bar_end_scroll, 0, 0, bar->horizontal);
5435 window_being_scrolled = Qnil;
5438 return false;
5442 #else /* not USE_GTK and not USE_MOTIF */
5444 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
5445 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
5446 scroll bar struct. CALL_DATA is a pointer to a float saying where
5447 the thumb is. */
5449 static void
5450 xaw_jump_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5452 struct scroll_bar *bar = client_data;
5453 float *top_addr = call_data;
5454 float top = *top_addr;
5455 float shown;
5456 int whole, portion, height, width;
5457 enum scroll_bar_part part;
5458 bool horizontal = bar->horizontal;
5461 if (horizontal)
5463 /* Get the size of the thumb, a value between 0 and 1. */
5464 block_input ();
5465 XtVaGetValues (widget, XtNshown, &shown, XtNwidth, &width, NULL);
5466 unblock_input ();
5468 if (shown < 1)
5470 whole = bar->whole - (shown * bar->whole);
5471 portion = min (top * bar->whole, whole);
5473 else
5475 whole = bar->whole;
5476 portion = 0;
5479 part = scroll_bar_horizontal_handle;
5481 else
5483 /* Get the size of the thumb, a value between 0 and 1. */
5484 block_input ();
5485 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
5486 unblock_input ();
5488 whole = 10000000;
5489 portion = shown < 1 ? top * whole : 0;
5491 if (shown < 1 && (eabs (top + shown - 1) < 1.0f / height))
5492 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
5493 the bottom, so we force the scrolling whenever we see that we're
5494 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
5495 we try to ensure that we always stay two pixels away from the
5496 bottom). */
5497 part = scroll_bar_down_arrow;
5498 else
5499 part = scroll_bar_handle;
5502 window_being_scrolled = bar->window;
5503 bar->dragging = portion;
5504 bar->last_seen_part = part;
5505 x_send_scroll_bar_event (bar->window, part, portion, whole, bar->horizontal);
5509 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
5510 i.e. line or page up or down. WIDGET is the Xaw scroll bar
5511 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
5512 the scroll bar. CALL_DATA is an integer specifying the action that
5513 has taken place. Its magnitude is in the range 0..height of the
5514 scroll bar. Negative values mean scroll towards buffer start.
5515 Values < height of scroll bar mean line-wise movement. */
5517 static void
5518 xaw_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5520 struct scroll_bar *bar = client_data;
5521 /* The position really is stored cast to a pointer. */
5522 int position = (intptr_t) call_data;
5523 Dimension height, width;
5524 enum scroll_bar_part part;
5526 if (bar->horizontal)
5528 /* Get the width of the scroll bar. */
5529 block_input ();
5530 XtVaGetValues (widget, XtNwidth, &width, NULL);
5531 unblock_input ();
5533 if (eabs (position) >= width)
5534 part = (position < 0) ? scroll_bar_before_handle : scroll_bar_after_handle;
5536 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
5537 it maps line-movement to call_data = max(5, height/20). */
5538 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, width / 20))
5539 part = (position < 0) ? scroll_bar_left_arrow : scroll_bar_right_arrow;
5540 else
5541 part = scroll_bar_move_ratio;
5543 window_being_scrolled = bar->window;
5544 bar->dragging = -1;
5545 bar->last_seen_part = part;
5546 x_send_scroll_bar_event (bar->window, part, position, width,
5547 bar->horizontal);
5549 else
5552 /* Get the height of the scroll bar. */
5553 block_input ();
5554 XtVaGetValues (widget, XtNheight, &height, NULL);
5555 unblock_input ();
5557 if (eabs (position) >= height)
5558 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
5560 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
5561 it maps line-movement to call_data = max(5, height/20). */
5562 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
5563 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
5564 else
5565 part = scroll_bar_move_ratio;
5567 window_being_scrolled = bar->window;
5568 bar->dragging = -1;
5569 bar->last_seen_part = part;
5570 x_send_scroll_bar_event (bar->window, part, position, height,
5571 bar->horizontal);
5575 #endif /* not USE_GTK and not USE_MOTIF */
5577 #define SCROLL_BAR_NAME "verticalScrollBar"
5578 #define SCROLL_BAR_HORIZONTAL_NAME "horizontalScrollBar"
5580 /* Create the widget for scroll bar BAR on frame F. Record the widget
5581 and X window of the scroll bar in BAR. */
5583 #ifdef USE_GTK
5584 static void
5585 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5587 const char *scroll_bar_name = SCROLL_BAR_NAME;
5589 block_input ();
5590 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
5591 G_CALLBACK (xg_end_scroll_callback),
5592 scroll_bar_name);
5593 unblock_input ();
5596 static void
5597 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5599 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
5601 block_input ();
5602 xg_create_horizontal_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
5603 G_CALLBACK (xg_end_scroll_callback),
5604 scroll_bar_name);
5605 unblock_input ();
5608 #else /* not USE_GTK */
5610 static void
5611 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5613 Window xwindow;
5614 Widget widget;
5615 Arg av[20];
5616 int ac = 0;
5617 const char *scroll_bar_name = SCROLL_BAR_NAME;
5618 unsigned long pixel;
5620 block_input ();
5622 #ifdef USE_MOTIF
5623 /* Set resources. Create the widget. */
5624 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5625 XtSetArg (av[ac], XmNminimum, 0); ++ac;
5626 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
5627 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
5628 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
5629 XtSetArg (av[ac], XmNincrement, 1); ++ac;
5630 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
5632 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5633 if (pixel != -1)
5635 XtSetArg (av[ac], XmNforeground, pixel);
5636 ++ac;
5639 pixel = f->output_data.x->scroll_bar_background_pixel;
5640 if (pixel != -1)
5642 XtSetArg (av[ac], XmNbackground, pixel);
5643 ++ac;
5646 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
5647 (char *) scroll_bar_name, av, ac);
5649 /* Add one callback for everything that can happen. */
5650 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
5651 (XtPointer) bar);
5652 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
5653 (XtPointer) bar);
5654 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
5655 (XtPointer) bar);
5656 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
5657 (XtPointer) bar);
5658 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
5659 (XtPointer) bar);
5660 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
5661 (XtPointer) bar);
5662 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
5663 (XtPointer) bar);
5665 /* Realize the widget. Only after that is the X window created. */
5666 XtRealizeWidget (widget);
5668 /* Set the cursor to an arrow. I didn't find a resource to do that.
5669 And I'm wondering why it hasn't an arrow cursor by default. */
5670 XDefineCursor (XtDisplay (widget), XtWindow (widget),
5671 f->output_data.x->nontext_cursor);
5673 #else /* !USE_MOTIF i.e. use Xaw */
5675 /* Set resources. Create the widget. The background of the
5676 Xaw3d scroll bar widget is a little bit light for my taste.
5677 We don't alter it here to let users change it according
5678 to their taste with `emacs*verticalScrollBar.background: xxx'. */
5679 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5680 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
5681 /* For smoother scrolling with Xaw3d -sm */
5682 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
5684 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5685 if (pixel != -1)
5687 XtSetArg (av[ac], XtNforeground, pixel);
5688 ++ac;
5691 pixel = f->output_data.x->scroll_bar_background_pixel;
5692 if (pixel != -1)
5694 XtSetArg (av[ac], XtNbackground, pixel);
5695 ++ac;
5698 /* Top/bottom shadow colors. */
5700 /* Allocate them, if necessary. */
5701 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
5703 pixel = f->output_data.x->scroll_bar_background_pixel;
5704 if (pixel != -1)
5706 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5707 FRAME_X_COLORMAP (f),
5708 &pixel, 1.2, 0x8000))
5709 pixel = -1;
5710 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
5713 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5715 pixel = f->output_data.x->scroll_bar_background_pixel;
5716 if (pixel != -1)
5718 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5719 FRAME_X_COLORMAP (f),
5720 &pixel, 0.6, 0x4000))
5721 pixel = -1;
5722 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
5726 #ifdef XtNbeNiceToColormap
5727 /* Tell the toolkit about them. */
5728 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
5729 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5730 /* We tried to allocate a color for the top/bottom shadow, and
5731 failed, so tell Xaw3d to use dithering instead. */
5732 /* But only if we have a small colormap. Xaw3d can allocate nice
5733 colors itself. */
5735 XtSetArg (av[ac], XtNbeNiceToColormap,
5736 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
5737 ++ac;
5739 else
5740 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
5741 be more consistent with other emacs 3d colors, and since Xaw3d is
5742 not good at dealing with allocation failure. */
5744 /* This tells Xaw3d to use real colors instead of dithering for
5745 the shadows. */
5746 XtSetArg (av[ac], XtNbeNiceToColormap, False);
5747 ++ac;
5749 /* Specify the colors. */
5750 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
5751 if (pixel != -1)
5753 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
5754 ++ac;
5756 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
5757 if (pixel != -1)
5759 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
5760 ++ac;
5763 #endif
5765 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
5766 f->output_data.x->edit_widget, av, ac);
5769 char const *initial = "";
5770 char const *val = initial;
5771 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
5772 #ifdef XtNarrowScrollbars
5773 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
5774 #endif
5775 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
5776 if (xaw3d_arrow_scroll || val == initial)
5777 { /* ARROW_SCROLL */
5778 xaw3d_arrow_scroll = True;
5779 /* Isn't that just a personal preference ? --Stef */
5780 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
5784 /* Define callbacks. */
5785 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
5786 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
5787 (XtPointer) bar);
5789 /* Realize the widget. Only after that is the X window created. */
5790 XtRealizeWidget (widget);
5792 #endif /* !USE_MOTIF */
5794 /* Install an action hook that lets us detect when the user
5795 finishes interacting with a scroll bar. */
5796 if (action_hook_id == 0)
5797 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
5799 /* Remember X window and widget in the scroll bar vector. */
5800 SET_SCROLL_BAR_X_WIDGET (bar, widget);
5801 xwindow = XtWindow (widget);
5802 bar->x_window = xwindow;
5803 bar->whole = 1;
5804 bar->horizontal = false;
5806 unblock_input ();
5809 static void
5810 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5812 Window xwindow;
5813 Widget widget;
5814 Arg av[20];
5815 int ac = 0;
5816 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
5817 unsigned long pixel;
5819 block_input ();
5821 #ifdef USE_MOTIF
5822 /* Set resources. Create the widget. */
5823 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5824 XtSetArg (av[ac], XmNminimum, 0); ++ac;
5825 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
5826 XtSetArg (av[ac], XmNorientation, XmHORIZONTAL); ++ac;
5827 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_RIGHT), ++ac;
5828 XtSetArg (av[ac], XmNincrement, 1); ++ac;
5829 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
5831 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5832 if (pixel != -1)
5834 XtSetArg (av[ac], XmNforeground, pixel);
5835 ++ac;
5838 pixel = f->output_data.x->scroll_bar_background_pixel;
5839 if (pixel != -1)
5841 XtSetArg (av[ac], XmNbackground, pixel);
5842 ++ac;
5845 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
5846 (char *) scroll_bar_name, av, ac);
5848 /* Add one callback for everything that can happen. */
5849 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
5850 (XtPointer) bar);
5851 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
5852 (XtPointer) bar);
5853 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
5854 (XtPointer) bar);
5855 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
5856 (XtPointer) bar);
5857 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
5858 (XtPointer) bar);
5859 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
5860 (XtPointer) bar);
5861 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
5862 (XtPointer) bar);
5864 /* Realize the widget. Only after that is the X window created. */
5865 XtRealizeWidget (widget);
5867 /* Set the cursor to an arrow. I didn't find a resource to do that.
5868 And I'm wondering why it hasn't an arrow cursor by default. */
5869 XDefineCursor (XtDisplay (widget), XtWindow (widget),
5870 f->output_data.x->nontext_cursor);
5872 #else /* !USE_MOTIF i.e. use Xaw */
5874 /* Set resources. Create the widget. The background of the
5875 Xaw3d scroll bar widget is a little bit light for my taste.
5876 We don't alter it here to let users change it according
5877 to their taste with `emacs*verticalScrollBar.background: xxx'. */
5878 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5879 XtSetArg (av[ac], XtNorientation, XtorientHorizontal); ++ac;
5880 /* For smoother scrolling with Xaw3d -sm */
5881 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
5883 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5884 if (pixel != -1)
5886 XtSetArg (av[ac], XtNforeground, pixel);
5887 ++ac;
5890 pixel = f->output_data.x->scroll_bar_background_pixel;
5891 if (pixel != -1)
5893 XtSetArg (av[ac], XtNbackground, pixel);
5894 ++ac;
5897 /* Top/bottom shadow colors. */
5899 /* Allocate them, if necessary. */
5900 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
5902 pixel = f->output_data.x->scroll_bar_background_pixel;
5903 if (pixel != -1)
5905 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5906 FRAME_X_COLORMAP (f),
5907 &pixel, 1.2, 0x8000))
5908 pixel = -1;
5909 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
5912 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5914 pixel = f->output_data.x->scroll_bar_background_pixel;
5915 if (pixel != -1)
5917 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5918 FRAME_X_COLORMAP (f),
5919 &pixel, 0.6, 0x4000))
5920 pixel = -1;
5921 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
5925 #ifdef XtNbeNiceToColormap
5926 /* Tell the toolkit about them. */
5927 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
5928 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5929 /* We tried to allocate a color for the top/bottom shadow, and
5930 failed, so tell Xaw3d to use dithering instead. */
5931 /* But only if we have a small colormap. Xaw3d can allocate nice
5932 colors itself. */
5934 XtSetArg (av[ac], XtNbeNiceToColormap,
5935 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
5936 ++ac;
5938 else
5939 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
5940 be more consistent with other emacs 3d colors, and since Xaw3d is
5941 not good at dealing with allocation failure. */
5943 /* This tells Xaw3d to use real colors instead of dithering for
5944 the shadows. */
5945 XtSetArg (av[ac], XtNbeNiceToColormap, False);
5946 ++ac;
5948 /* Specify the colors. */
5949 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
5950 if (pixel != -1)
5952 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
5953 ++ac;
5955 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
5956 if (pixel != -1)
5958 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
5959 ++ac;
5962 #endif
5964 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
5965 f->output_data.x->edit_widget, av, ac);
5968 char const *initial = "";
5969 char const *val = initial;
5970 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
5971 #ifdef XtNarrowScrollbars
5972 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
5973 #endif
5974 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
5975 if (xaw3d_arrow_scroll || val == initial)
5976 { /* ARROW_SCROLL */
5977 xaw3d_arrow_scroll = True;
5978 /* Isn't that just a personal preference ? --Stef */
5979 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
5983 /* Define callbacks. */
5984 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
5985 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
5986 (XtPointer) bar);
5988 /* Realize the widget. Only after that is the X window created. */
5989 XtRealizeWidget (widget);
5991 #endif /* !USE_MOTIF */
5993 /* Install an action hook that lets us detect when the user
5994 finishes interacting with a scroll bar. */
5995 if (horizontal_action_hook_id == 0)
5996 horizontal_action_hook_id
5997 = XtAppAddActionHook (Xt_app_con, xt_horizontal_action_hook, 0);
5999 /* Remember X window and widget in the scroll bar vector. */
6000 SET_SCROLL_BAR_X_WIDGET (bar, widget);
6001 xwindow = XtWindow (widget);
6002 bar->x_window = xwindow;
6003 bar->whole = 1;
6004 bar->horizontal = true;
6006 unblock_input ();
6008 #endif /* not USE_GTK */
6011 /* Set the thumb size and position of scroll bar BAR. We are currently
6012 displaying PORTION out of a whole WHOLE, and our position POSITION. */
6014 #ifdef USE_GTK
6015 static void
6016 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
6018 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
6021 static void
6022 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
6024 xg_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
6027 #else /* not USE_GTK */
6028 static void
6029 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
6030 int whole)
6032 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6033 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6034 float top, shown;
6036 block_input ();
6038 #ifdef USE_MOTIF
6040 if (scroll_bar_adjust_thumb_portion_p)
6042 /* We use an estimate of 30 chars per line rather than the real
6043 `portion' value. This has the disadvantage that the thumb size
6044 is not very representative, but it makes our life a lot easier.
6045 Otherwise, we have to constantly adjust the thumb size, which
6046 we can't always do quickly enough: while dragging, the size of
6047 the thumb might prevent the user from dragging the thumb all the
6048 way to the end. but Motif and some versions of Xaw3d don't allow
6049 updating the thumb size while dragging. Also, even if we can update
6050 its size, the update will often happen too late.
6051 If you don't believe it, check out revision 1.650 of xterm.c to see
6052 what hoops we were going through and the still poor behavior we got. */
6053 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
6054 /* When the thumb is at the bottom, position == whole.
6055 So we need to increase `whole' to make space for the thumb. */
6056 whole += portion;
6059 if (whole <= 0)
6060 top = 0, shown = 1;
6061 else
6063 top = (float) position / whole;
6064 shown = (float) portion / whole;
6067 if (bar->dragging == -1)
6069 int size, value;
6071 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
6072 is the scroll bar's maximum and MIN is the scroll bar's minimum
6073 value. */
6074 size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
6076 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
6077 value = top * XM_SB_MAX;
6078 value = min (value, XM_SB_MAX - size);
6080 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6082 #else /* !USE_MOTIF i.e. use Xaw */
6084 if (whole == 0)
6085 top = 0, shown = 1;
6086 else
6088 top = (float) position / whole;
6089 shown = (float) portion / whole;
6093 float old_top, old_shown;
6094 Dimension height;
6095 XtVaGetValues (widget,
6096 XtNtopOfThumb, &old_top,
6097 XtNshown, &old_shown,
6098 XtNheight, &height,
6099 NULL);
6101 /* Massage the top+shown values. */
6102 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
6103 top = max (0, min (1, top));
6104 else
6105 top = old_top;
6106 #if ! defined (HAVE_XAW3D)
6107 /* With Xaw, 'top' values too closer to 1.0 may
6108 cause the thumb to disappear. Fix that. */
6109 top = min (top, 0.99f);
6110 #endif
6111 /* Keep two pixels available for moving the thumb down. */
6112 shown = max (0, min (1 - top - (2.0f / height), shown));
6113 #if ! defined (HAVE_XAW3D)
6114 /* Likewise with too small 'shown'. */
6115 shown = max (shown, 0.01f);
6116 #endif
6118 /* If the call to XawScrollbarSetThumb below doesn't seem to
6119 work, check that 'NARROWPROTO' is defined in src/config.h.
6120 If this is not so, most likely you need to fix configure. */
6121 if (top != old_top || shown != old_shown)
6123 if (bar->dragging == -1)
6124 XawScrollbarSetThumb (widget, top, shown);
6125 else
6127 /* Try to make the scrolling a tad smoother. */
6128 if (!xaw3d_pick_top)
6129 shown = min (shown, old_shown);
6131 XawScrollbarSetThumb (widget, top, shown);
6135 #endif /* !USE_MOTIF */
6137 unblock_input ();
6140 static void
6141 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
6142 int whole)
6144 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6145 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6146 float top, shown;
6148 block_input ();
6150 #ifdef USE_MOTIF
6151 bar->whole = whole;
6152 shown = (float) portion / whole;
6153 top = (float) position / (whole - portion);
6155 int size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
6156 int value = clip_to_bounds (0, top * (XM_SB_MAX - size), XM_SB_MAX - size);
6158 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6160 #else /* !USE_MOTIF i.e. use Xaw */
6161 bar->whole = whole;
6162 if (whole == 0)
6163 top = 0, shown = 1;
6164 else
6166 top = (float) position / whole;
6167 shown = (float) portion / whole;
6171 float old_top, old_shown;
6172 Dimension height;
6173 XtVaGetValues (widget,
6174 XtNtopOfThumb, &old_top,
6175 XtNshown, &old_shown,
6176 XtNheight, &height,
6177 NULL);
6179 #if false
6180 /* Massage the top+shown values. */
6181 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
6182 top = max (0, min (1, top));
6183 else
6184 top = old_top;
6185 #if ! defined (HAVE_XAW3D)
6186 /* With Xaw, 'top' values too closer to 1.0 may
6187 cause the thumb to disappear. Fix that. */
6188 top = min (top, 0.99f);
6189 #endif
6190 /* Keep two pixels available for moving the thumb down. */
6191 shown = max (0, min (1 - top - (2.0f / height), shown));
6192 #if ! defined (HAVE_XAW3D)
6193 /* Likewise with too small 'shown'. */
6194 shown = max (shown, 0.01f);
6195 #endif
6196 #endif
6198 /* If the call to XawScrollbarSetThumb below doesn't seem to
6199 work, check that 'NARROWPROTO' is defined in src/config.h.
6200 If this is not so, most likely you need to fix configure. */
6201 XawScrollbarSetThumb (widget, top, shown);
6202 #if false
6203 if (top != old_top || shown != old_shown)
6205 if (bar->dragging == -1)
6206 XawScrollbarSetThumb (widget, top, shown);
6207 else
6209 /* Try to make the scrolling a tad smoother. */
6210 if (!xaw3d_pick_top)
6211 shown = min (shown, old_shown);
6213 XawScrollbarSetThumb (widget, top, shown);
6216 #endif
6218 #endif /* !USE_MOTIF */
6220 unblock_input ();
6222 #endif /* not USE_GTK */
6224 #endif /* USE_TOOLKIT_SCROLL_BARS */
6228 /************************************************************************
6229 Scroll bars, general
6230 ************************************************************************/
6232 /* Create a scroll bar and return the scroll bar vector for it. W is
6233 the Emacs window on which to create the scroll bar. TOP, LEFT,
6234 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
6235 scroll bar. */
6237 static struct scroll_bar *
6238 x_scroll_bar_create (struct window *w, int top, int left,
6239 int width, int height, bool horizontal)
6241 struct frame *f = XFRAME (w->frame);
6242 struct scroll_bar *bar
6243 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
6244 Lisp_Object barobj;
6246 block_input ();
6248 #ifdef USE_TOOLKIT_SCROLL_BARS
6249 if (horizontal)
6250 x_create_horizontal_toolkit_scroll_bar (f, bar);
6251 else
6252 x_create_toolkit_scroll_bar (f, bar);
6253 #else /* not USE_TOOLKIT_SCROLL_BARS */
6255 XSetWindowAttributes a;
6256 unsigned long mask;
6257 Window window;
6259 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
6260 if (a.background_pixel == -1)
6261 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
6263 a.event_mask = (ButtonPressMask | ButtonReleaseMask
6264 | ButtonMotionMask | PointerMotionHintMask
6265 | ExposureMask);
6266 a.cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
6268 mask = (CWBackPixel | CWEventMask | CWCursor);
6270 /* Clear the area of W that will serve as a scroll bar. This is
6271 for the case that a window has been split horizontally. In
6272 this case, no clear_frame is generated to reduce flickering. */
6273 if (width > 0 && window_box_height (w) > 0)
6274 x_clear_area (f, left, top, width, window_box_height (w));
6276 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6277 /* Position and size of scroll bar. */
6278 left, top, width, height,
6279 /* Border width, depth, class, and visual. */
6281 CopyFromParent,
6282 CopyFromParent,
6283 CopyFromParent,
6284 /* Attributes. */
6285 mask, &a);
6286 bar->x_window = window;
6288 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6290 XSETWINDOW (bar->window, w);
6291 bar->top = top;
6292 bar->left = left;
6293 bar->width = width;
6294 bar->height = height;
6295 bar->start = 0;
6296 bar->end = 0;
6297 bar->dragging = -1;
6298 bar->horizontal = horizontal;
6299 #if defined (USE_TOOLKIT_SCROLL_BARS) && defined (USE_LUCID)
6300 bar->last_seen_part = scroll_bar_nowhere;
6301 #endif
6303 /* Add bar to its frame's list of scroll bars. */
6304 bar->next = FRAME_SCROLL_BARS (f);
6305 bar->prev = Qnil;
6306 XSETVECTOR (barobj, bar);
6307 fset_scroll_bars (f, barobj);
6308 if (!NILP (bar->next))
6309 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6311 /* Map the window/widget. */
6312 #ifdef USE_TOOLKIT_SCROLL_BARS
6314 #ifdef USE_GTK
6315 if (horizontal)
6316 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top,
6317 left, width, max (height, 1));
6318 else
6319 xg_update_scrollbar_pos (f, bar->x_window, top,
6320 left, width, max (height, 1));
6321 #else /* not USE_GTK */
6322 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6323 XtConfigureWidget (scroll_bar, left, top, width, max (height, 1), 0);
6324 XtMapWidget (scroll_bar);
6325 #endif /* not USE_GTK */
6327 #else /* not USE_TOOLKIT_SCROLL_BARS */
6328 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
6329 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6331 unblock_input ();
6332 return bar;
6336 #ifndef USE_TOOLKIT_SCROLL_BARS
6338 /* Draw BAR's handle in the proper position.
6340 If the handle is already drawn from START to END, don't bother
6341 redrawing it, unless REBUILD; in that case, always
6342 redraw it. (REBUILD is handy for drawing the handle after expose
6343 events.)
6345 Normally, we want to constrain the start and end of the handle to
6346 fit inside its rectangle, but if the user is dragging the scroll
6347 bar handle, we want to let them drag it down all the way, so that
6348 the bar's top is as far down as it goes; otherwise, there's no way
6349 to move to the very end of the buffer. */
6351 static void
6352 x_scroll_bar_set_handle (struct scroll_bar *bar, int start, int end,
6353 bool rebuild)
6355 bool dragging = bar->dragging != -1;
6356 Window w = bar->x_window;
6357 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6358 GC gc = f->output_data.x->normal_gc;
6360 /* If the display is already accurate, do nothing. */
6361 if (! rebuild
6362 && start == bar->start
6363 && end == bar->end)
6364 return;
6366 block_input ();
6369 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
6370 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
6371 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
6373 /* Make sure the values are reasonable, and try to preserve
6374 the distance between start and end. */
6376 int length = end - start;
6378 if (start < 0)
6379 start = 0;
6380 else if (start > top_range)
6381 start = top_range;
6382 end = start + length;
6384 if (end < start)
6385 end = start;
6386 else if (end > top_range && ! dragging)
6387 end = top_range;
6390 /* Store the adjusted setting in the scroll bar. */
6391 bar->start = start;
6392 bar->end = end;
6394 /* Clip the end position, just for display. */
6395 if (end > top_range)
6396 end = top_range;
6398 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
6399 below top positions, to make sure the handle is always at least
6400 that many pixels tall. */
6401 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
6403 /* Draw the empty space above the handle. Note that we can't clear
6404 zero-height areas; that means "clear to end of window." */
6405 if ((inside_width > 0) && (start > 0))
6406 x_clear_area1 (FRAME_X_DISPLAY (f), w,
6407 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6408 VERTICAL_SCROLL_BAR_TOP_BORDER,
6409 inside_width, start, False);
6411 /* Change to proper foreground color if one is specified. */
6412 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6413 XSetForeground (FRAME_X_DISPLAY (f), gc,
6414 f->output_data.x->scroll_bar_foreground_pixel);
6416 /* Draw the handle itself. */
6417 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
6418 /* x, y, width, height */
6419 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6420 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
6421 inside_width, end - start);
6423 /* Restore the foreground color of the GC if we changed it above. */
6424 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6425 XSetForeground (FRAME_X_DISPLAY (f), gc,
6426 FRAME_FOREGROUND_PIXEL (f));
6428 /* Draw the empty space below the handle. Note that we can't
6429 clear zero-height areas; that means "clear to end of window." */
6430 if ((inside_width > 0) && (end < inside_height))
6431 x_clear_area1 (FRAME_X_DISPLAY (f), w,
6432 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6433 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
6434 inside_width, inside_height - end, False);
6437 unblock_input ();
6440 #endif /* !USE_TOOLKIT_SCROLL_BARS */
6442 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
6443 nil. */
6445 static void
6446 x_scroll_bar_remove (struct scroll_bar *bar)
6448 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6449 block_input ();
6451 #ifdef USE_TOOLKIT_SCROLL_BARS
6452 #ifdef USE_GTK
6453 xg_remove_scroll_bar (f, bar->x_window);
6454 #else /* not USE_GTK */
6455 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
6456 #endif /* not USE_GTK */
6457 #else
6458 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
6459 #endif
6461 /* Dissociate this scroll bar from its window. */
6462 if (bar->horizontal)
6463 wset_horizontal_scroll_bar (XWINDOW (bar->window), Qnil);
6464 else
6465 wset_vertical_scroll_bar (XWINDOW (bar->window), Qnil);
6467 unblock_input ();
6471 /* Set the handle of the vertical scroll bar for WINDOW to indicate
6472 that we are displaying PORTION characters out of a total of WHOLE
6473 characters, starting at POSITION. If WINDOW has no scroll bar,
6474 create one. */
6476 static void
6477 XTset_vertical_scroll_bar (struct window *w, int portion, int whole, int position)
6479 struct frame *f = XFRAME (w->frame);
6480 Lisp_Object barobj;
6481 struct scroll_bar *bar;
6482 int top, height, left, width;
6483 int window_y, window_height;
6485 /* Get window dimensions. */
6486 window_box (w, ANY_AREA, 0, &window_y, 0, &window_height);
6487 top = window_y;
6488 height = window_height;
6489 left = WINDOW_SCROLL_BAR_AREA_X (w);
6490 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
6492 /* Does the scroll bar exist yet? */
6493 if (NILP (w->vertical_scroll_bar))
6495 if (width > 0 && height > 0)
6497 block_input ();
6498 x_clear_area (f, left, top, width, height);
6499 unblock_input ();
6502 bar = x_scroll_bar_create (w, top, left, width, max (height, 1), false);
6504 else
6506 /* It may just need to be moved and resized. */
6507 unsigned int mask = 0;
6509 bar = XSCROLL_BAR (w->vertical_scroll_bar);
6511 block_input ();
6513 if (left != bar->left)
6514 mask |= CWX;
6515 if (top != bar->top)
6516 mask |= CWY;
6517 if (width != bar->width)
6518 mask |= CWWidth;
6519 if (height != bar->height)
6520 mask |= CWHeight;
6522 #ifdef USE_TOOLKIT_SCROLL_BARS
6524 /* Move/size the scroll bar widget. */
6525 if (mask)
6527 /* Since toolkit scroll bars are smaller than the space reserved
6528 for them on the frame, we have to clear "under" them. */
6529 if (width > 0 && height > 0)
6530 x_clear_area (f, left, top, width, height);
6531 #ifdef USE_GTK
6532 xg_update_scrollbar_pos (f, bar->x_window, top,
6533 left, width, max (height, 1));
6534 #else /* not USE_GTK */
6535 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
6536 left, top, width, max (height, 1), 0);
6537 #endif /* not USE_GTK */
6539 #else /* not USE_TOOLKIT_SCROLL_BARS */
6541 /* Move/size the scroll bar window. */
6542 if (mask)
6544 XWindowChanges wc;
6546 wc.x = left;
6547 wc.y = top;
6548 wc.width = width;
6549 wc.height = height;
6550 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
6551 mask, &wc);
6554 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6556 /* Remember new settings. */
6557 bar->left = left;
6558 bar->top = top;
6559 bar->width = width;
6560 bar->height = height;
6562 unblock_input ();
6565 #ifdef USE_TOOLKIT_SCROLL_BARS
6566 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
6567 #else /* not USE_TOOLKIT_SCROLL_BARS */
6568 /* Set the scroll bar's current state, unless we're currently being
6569 dragged. */
6570 if (bar->dragging == -1)
6572 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
6574 if (whole == 0)
6575 x_scroll_bar_set_handle (bar, 0, top_range, false);
6576 else
6578 int start = ((double) position * top_range) / whole;
6579 int end = ((double) (position + portion) * top_range) / whole;
6580 x_scroll_bar_set_handle (bar, start, end, false);
6583 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6585 XSETVECTOR (barobj, bar);
6586 wset_vertical_scroll_bar (w, barobj);
6590 static void
6591 XTset_horizontal_scroll_bar (struct window *w, int portion, int whole, int position)
6593 struct frame *f = XFRAME (w->frame);
6594 Lisp_Object barobj;
6595 struct scroll_bar *bar;
6596 int top, height, left, width;
6597 int window_x, window_width;
6598 int pixel_width = WINDOW_PIXEL_WIDTH (w);
6600 /* Get window dimensions. */
6601 window_box (w, ANY_AREA, &window_x, 0, &window_width, 0);
6602 left = window_x;
6603 width = window_width;
6604 top = WINDOW_SCROLL_BAR_AREA_Y (w);
6605 height = WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
6607 /* Does the scroll bar exist yet? */
6608 if (NILP (w->horizontal_scroll_bar))
6610 if (width > 0 && height > 0)
6612 block_input ();
6614 /* Clear also part between window_width and
6615 WINDOW_PIXEL_WIDTH. */
6616 x_clear_area (f, left, top, pixel_width, height);
6617 unblock_input ();
6620 bar = x_scroll_bar_create (w, top, left, width, height, true);
6622 else
6624 /* It may just need to be moved and resized. */
6625 unsigned int mask = 0;
6627 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
6629 block_input ();
6631 if (left != bar->left)
6632 mask |= CWX;
6633 if (top != bar->top)
6634 mask |= CWY;
6635 if (width != bar->width)
6636 mask |= CWWidth;
6637 if (height != bar->height)
6638 mask |= CWHeight;
6640 #ifdef USE_TOOLKIT_SCROLL_BARS
6641 /* Move/size the scroll bar widget. */
6642 if (mask)
6644 /* Since toolkit scroll bars are smaller than the space reserved
6645 for them on the frame, we have to clear "under" them. */
6646 if (width > 0 && height > 0)
6647 x_clear_area (f,
6648 WINDOW_LEFT_EDGE_X (w), top,
6649 pixel_width - WINDOW_RIGHT_DIVIDER_WIDTH (w), height);
6650 #ifdef USE_GTK
6651 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top, left,
6652 width, height);
6653 #else /* not USE_GTK */
6654 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
6655 left, top, width, height, 0);
6656 #endif /* not USE_GTK */
6658 #else /* not USE_TOOLKIT_SCROLL_BARS */
6660 /* Clear areas not covered by the scroll bar because it's not as
6661 wide as the area reserved for it. This makes sure a
6662 previous mode line display is cleared after C-x 2 C-x 1, for
6663 example. */
6665 int area_height = WINDOW_CONFIG_SCROLL_BAR_HEIGHT (w);
6666 int rest = area_height - height;
6667 if (rest > 0 && width > 0)
6668 x_clear_area (f, left, top, width, rest);
6671 /* Move/size the scroll bar window. */
6672 if (mask)
6674 XWindowChanges wc;
6676 wc.x = left;
6677 wc.y = top;
6678 wc.width = width;
6679 wc.height = height;
6680 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
6681 mask, &wc);
6684 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6686 /* Remember new settings. */
6687 bar->left = left;
6688 bar->top = top;
6689 bar->width = width;
6690 bar->height = height;
6692 unblock_input ();
6695 #ifdef USE_TOOLKIT_SCROLL_BARS
6696 x_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
6697 #else /* not USE_TOOLKIT_SCROLL_BARS */
6698 /* Set the scroll bar's current state, unless we're currently being
6699 dragged. */
6700 if (bar->dragging == -1)
6702 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, width);
6704 if (whole == 0)
6705 x_scroll_bar_set_handle (bar, 0, left_range, false);
6706 else
6708 int start = ((double) position * left_range) / whole;
6709 int end = ((double) (position + portion) * left_range) / whole;
6710 x_scroll_bar_set_handle (bar, start, end, false);
6713 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6715 XSETVECTOR (barobj, bar);
6716 wset_horizontal_scroll_bar (w, barobj);
6720 /* The following three hooks are used when we're doing a thorough
6721 redisplay of the frame. We don't explicitly know which scroll bars
6722 are going to be deleted, because keeping track of when windows go
6723 away is a real pain - "Can you say set-window-configuration, boys
6724 and girls?" Instead, we just assert at the beginning of redisplay
6725 that *all* scroll bars are to be removed, and then save a scroll bar
6726 from the fiery pit when we actually redisplay its window. */
6728 /* Arrange for all scroll bars on FRAME to be removed at the next call
6729 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
6730 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
6732 static void
6733 XTcondemn_scroll_bars (struct frame *frame)
6735 if (!NILP (FRAME_SCROLL_BARS (frame)))
6737 if (!NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
6739 /* Prepend scrollbars to already condemned ones. */
6740 Lisp_Object last = FRAME_SCROLL_BARS (frame);
6742 while (!NILP (XSCROLL_BAR (last)->next))
6743 last = XSCROLL_BAR (last)->next;
6745 XSCROLL_BAR (last)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
6746 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = last;
6749 fset_condemned_scroll_bars (frame, FRAME_SCROLL_BARS (frame));
6750 fset_scroll_bars (frame, Qnil);
6755 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
6756 Note that WINDOW isn't necessarily condemned at all. */
6758 static void
6759 XTredeem_scroll_bar (struct window *w)
6761 struct scroll_bar *bar;
6762 Lisp_Object barobj;
6763 struct frame *f;
6765 /* We can't redeem this window's scroll bar if it doesn't have one. */
6766 if (NILP (w->vertical_scroll_bar) && NILP (w->horizontal_scroll_bar))
6767 emacs_abort ();
6769 if (!NILP (w->vertical_scroll_bar) && WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
6771 bar = XSCROLL_BAR (w->vertical_scroll_bar);
6772 /* Unlink it from the condemned list. */
6773 f = XFRAME (WINDOW_FRAME (w));
6774 if (NILP (bar->prev))
6776 /* If the prev pointer is nil, it must be the first in one of
6777 the lists. */
6778 if (EQ (FRAME_SCROLL_BARS (f), w->vertical_scroll_bar))
6779 /* It's not condemned. Everything's fine. */
6780 goto horizontal;
6781 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
6782 w->vertical_scroll_bar))
6783 fset_condemned_scroll_bars (f, bar->next);
6784 else
6785 /* If its prev pointer is nil, it must be at the front of
6786 one or the other! */
6787 emacs_abort ();
6789 else
6790 XSCROLL_BAR (bar->prev)->next = bar->next;
6792 if (! NILP (bar->next))
6793 XSCROLL_BAR (bar->next)->prev = bar->prev;
6795 bar->next = FRAME_SCROLL_BARS (f);
6796 bar->prev = Qnil;
6797 XSETVECTOR (barobj, bar);
6798 fset_scroll_bars (f, barobj);
6799 if (! NILP (bar->next))
6800 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6803 horizontal:
6804 if (!NILP (w->horizontal_scroll_bar) && WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
6806 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
6807 /* Unlink it from the condemned list. */
6808 f = XFRAME (WINDOW_FRAME (w));
6809 if (NILP (bar->prev))
6811 /* If the prev pointer is nil, it must be the first in one of
6812 the lists. */
6813 if (EQ (FRAME_SCROLL_BARS (f), w->horizontal_scroll_bar))
6814 /* It's not condemned. Everything's fine. */
6815 return;
6816 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
6817 w->horizontal_scroll_bar))
6818 fset_condemned_scroll_bars (f, bar->next);
6819 else
6820 /* If its prev pointer is nil, it must be at the front of
6821 one or the other! */
6822 emacs_abort ();
6824 else
6825 XSCROLL_BAR (bar->prev)->next = bar->next;
6827 if (! NILP (bar->next))
6828 XSCROLL_BAR (bar->next)->prev = bar->prev;
6830 bar->next = FRAME_SCROLL_BARS (f);
6831 bar->prev = Qnil;
6832 XSETVECTOR (barobj, bar);
6833 fset_scroll_bars (f, barobj);
6834 if (! NILP (bar->next))
6835 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6839 /* Remove all scroll bars on FRAME that haven't been saved since the
6840 last call to `*condemn_scroll_bars_hook'. */
6842 static void
6843 XTjudge_scroll_bars (struct frame *f)
6845 Lisp_Object bar, next;
6847 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
6849 /* Clear out the condemned list now so we won't try to process any
6850 more events on the hapless scroll bars. */
6851 fset_condemned_scroll_bars (f, Qnil);
6853 for (; ! NILP (bar); bar = next)
6855 struct scroll_bar *b = XSCROLL_BAR (bar);
6857 x_scroll_bar_remove (b);
6859 next = b->next;
6860 b->next = b->prev = Qnil;
6863 /* Now there should be no references to the condemned scroll bars,
6864 and they should get garbage-collected. */
6868 #ifndef USE_TOOLKIT_SCROLL_BARS
6869 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
6870 is a no-op when using toolkit scroll bars.
6872 This may be called from a signal handler, so we have to ignore GC
6873 mark bits. */
6875 static void
6876 x_scroll_bar_expose (struct scroll_bar *bar, const XEvent *event)
6878 Window w = bar->x_window;
6879 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6880 GC gc = f->output_data.x->normal_gc;
6882 block_input ();
6884 x_scroll_bar_set_handle (bar, bar->start, bar->end, true);
6886 /* Switch to scroll bar foreground color. */
6887 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6888 XSetForeground (FRAME_X_DISPLAY (f), gc,
6889 f->output_data.x->scroll_bar_foreground_pixel);
6891 /* Draw a one-pixel border just inside the edges of the scroll bar. */
6892 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
6893 /* x, y, width, height */
6894 0, 0, bar->width - 1, bar->height - 1);
6896 /* Restore the foreground color of the GC if we changed it above. */
6897 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6898 XSetForeground (FRAME_X_DISPLAY (f), gc,
6899 FRAME_FOREGROUND_PIXEL (f));
6901 unblock_input ();
6904 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6906 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
6907 is set to something other than NO_EVENT, it is enqueued.
6909 This may be called from a signal handler, so we have to ignore GC
6910 mark bits. */
6913 static void
6914 x_scroll_bar_handle_click (struct scroll_bar *bar,
6915 const XEvent *event,
6916 struct input_event *emacs_event)
6918 if (! WINDOWP (bar->window))
6919 emacs_abort ();
6921 emacs_event->kind = (bar->horizontal
6922 ? HORIZONTAL_SCROLL_BAR_CLICK_EVENT
6923 : SCROLL_BAR_CLICK_EVENT);
6924 emacs_event->code = event->xbutton.button - Button1;
6925 emacs_event->modifiers
6926 = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO
6927 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
6928 event->xbutton.state)
6929 | (event->type == ButtonRelease
6930 ? up_modifier
6931 : down_modifier));
6932 emacs_event->frame_or_window = bar->window;
6933 emacs_event->arg = Qnil;
6934 emacs_event->timestamp = event->xbutton.time;
6935 if (bar->horizontal)
6937 int left_range
6938 = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
6939 int x = event->xbutton.x - HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
6941 if (x < 0) x = 0;
6942 if (x > left_range) x = left_range;
6944 if (x < bar->start)
6945 emacs_event->part = scroll_bar_before_handle;
6946 else if (x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
6947 emacs_event->part = scroll_bar_horizontal_handle;
6948 else
6949 emacs_event->part = scroll_bar_after_handle;
6951 #ifndef USE_TOOLKIT_SCROLL_BARS
6952 /* If the user has released the handle, set it to its final position. */
6953 if (event->type == ButtonRelease && bar->dragging != -1)
6955 int new_start = - bar->dragging;
6956 int new_end = new_start + bar->end - bar->start;
6958 x_scroll_bar_set_handle (bar, new_start, new_end, false);
6959 bar->dragging = -1;
6961 #endif
6963 XSETINT (emacs_event->x, left_range);
6964 XSETINT (emacs_event->y, x);
6966 else
6968 int top_range
6969 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
6970 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
6972 if (y < 0) y = 0;
6973 if (y > top_range) y = top_range;
6975 if (y < bar->start)
6976 emacs_event->part = scroll_bar_above_handle;
6977 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
6978 emacs_event->part = scroll_bar_handle;
6979 else
6980 emacs_event->part = scroll_bar_below_handle;
6982 #ifndef USE_TOOLKIT_SCROLL_BARS
6983 /* If the user has released the handle, set it to its final position. */
6984 if (event->type == ButtonRelease && bar->dragging != -1)
6986 int new_start = y - bar->dragging;
6987 int new_end = new_start + bar->end - bar->start;
6989 x_scroll_bar_set_handle (bar, new_start, new_end, false);
6990 bar->dragging = -1;
6992 #endif
6994 XSETINT (emacs_event->x, y);
6995 XSETINT (emacs_event->y, top_range);
6999 #ifndef USE_TOOLKIT_SCROLL_BARS
7001 /* Handle some mouse motion while someone is dragging the scroll bar.
7003 This may be called from a signal handler, so we have to ignore GC
7004 mark bits. */
7006 static void
7007 x_scroll_bar_note_movement (struct scroll_bar *bar,
7008 const XMotionEvent *event)
7010 struct frame *f = XFRAME (XWINDOW (bar->window)->frame);
7011 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
7013 dpyinfo->last_mouse_movement_time = event->time;
7014 dpyinfo->last_mouse_scroll_bar = bar;
7015 f->mouse_moved = true;
7017 /* If we're dragging the bar, display it. */
7018 if (bar->dragging != -1)
7020 /* Where should the handle be now? */
7021 int new_start = event->y - bar->dragging;
7023 if (new_start != bar->start)
7025 int new_end = new_start + bar->end - bar->start;
7027 x_scroll_bar_set_handle (bar, new_start, new_end, false);
7032 #endif /* !USE_TOOLKIT_SCROLL_BARS */
7034 /* Return information to the user about the current position of the mouse
7035 on the scroll bar. */
7037 static void
7038 x_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
7039 enum scroll_bar_part *part, Lisp_Object *x,
7040 Lisp_Object *y, Time *timestamp)
7042 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
7043 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
7044 Window w = bar->x_window;
7045 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7046 int win_x, win_y;
7047 Window dummy_window;
7048 int dummy_coord;
7049 unsigned int dummy_mask;
7051 block_input ();
7053 /* Get the mouse's position relative to the scroll bar window, and
7054 report that. */
7055 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
7057 /* Root, child, root x and root y. */
7058 &dummy_window, &dummy_window,
7059 &dummy_coord, &dummy_coord,
7061 /* Position relative to scroll bar. */
7062 &win_x, &win_y,
7064 /* Mouse buttons and modifier keys. */
7065 &dummy_mask))
7067 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
7069 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
7071 if (bar->dragging != -1)
7072 win_y -= bar->dragging;
7074 if (win_y < 0)
7075 win_y = 0;
7076 if (win_y > top_range)
7077 win_y = top_range;
7079 *fp = f;
7080 *bar_window = bar->window;
7082 if (bar->dragging != -1)
7083 *part = scroll_bar_handle;
7084 else if (win_y < bar->start)
7085 *part = scroll_bar_above_handle;
7086 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
7087 *part = scroll_bar_handle;
7088 else
7089 *part = scroll_bar_below_handle;
7091 XSETINT (*x, win_y);
7092 XSETINT (*y, top_range);
7094 f->mouse_moved = false;
7095 dpyinfo->last_mouse_scroll_bar = NULL;
7096 *timestamp = dpyinfo->last_mouse_movement_time;
7099 unblock_input ();
7103 /* Return information to the user about the current position of the mouse
7104 on the scroll bar. */
7106 static void
7107 x_horizontal_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
7108 enum scroll_bar_part *part, Lisp_Object *x,
7109 Lisp_Object *y, Time *timestamp)
7111 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
7112 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
7113 Window w = bar->x_window;
7114 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7115 int win_x, win_y;
7116 Window dummy_window;
7117 int dummy_coord;
7118 unsigned int dummy_mask;
7120 block_input ();
7122 /* Get the mouse's position relative to the scroll bar window, and
7123 report that. */
7124 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
7126 /* Root, child, root x and root y. */
7127 &dummy_window, &dummy_window,
7128 &dummy_coord, &dummy_coord,
7130 /* Position relative to scroll bar. */
7131 &win_x, &win_y,
7133 /* Mouse buttons and modifier keys. */
7134 &dummy_mask))
7136 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
7138 win_x -= HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
7140 if (bar->dragging != -1)
7141 win_x -= bar->dragging;
7143 if (win_x < 0)
7144 win_x = 0;
7145 if (win_x > left_range)
7146 win_x = left_range;
7148 *fp = f;
7149 *bar_window = bar->window;
7151 if (bar->dragging != -1)
7152 *part = scroll_bar_horizontal_handle;
7153 else if (win_x < bar->start)
7154 *part = scroll_bar_before_handle;
7155 else if (win_x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
7156 *part = scroll_bar_handle;
7157 else
7158 *part = scroll_bar_after_handle;
7160 XSETINT (*y, win_x);
7161 XSETINT (*x, left_range);
7163 f->mouse_moved = false;
7164 dpyinfo->last_mouse_scroll_bar = NULL;
7165 *timestamp = dpyinfo->last_mouse_movement_time;
7168 unblock_input ();
7172 /* The screen has been cleared so we may have changed foreground or
7173 background colors, and the scroll bars may need to be redrawn.
7174 Clear out the scroll bars, and ask for expose events, so we can
7175 redraw them. */
7177 static void
7178 x_scroll_bar_clear (struct frame *f)
7180 #ifndef USE_TOOLKIT_SCROLL_BARS
7181 Lisp_Object bar;
7183 /* We can have scroll bars even if this is 0,
7184 if we just turned off scroll bar mode.
7185 But in that case we should not clear them. */
7186 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
7187 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
7188 bar = XSCROLL_BAR (bar)->next)
7189 XClearArea (FRAME_X_DISPLAY (f),
7190 XSCROLL_BAR (bar)->x_window,
7191 0, 0, 0, 0, True);
7192 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7195 #ifdef ENABLE_CHECKING
7197 /* Record the last 100 characters stored
7198 to help debug the loss-of-chars-during-GC problem. */
7200 static int temp_index;
7201 static short temp_buffer[100];
7203 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
7204 if (temp_index == ARRAYELTS (temp_buffer)) \
7205 temp_index = 0; \
7206 temp_buffer[temp_index++] = (keysym)
7208 #else /* not ENABLE_CHECKING */
7210 #define STORE_KEYSYM_FOR_DEBUG(keysym) ((void)0)
7212 #endif /* ENABLE_CHECKING */
7214 /* Set this to nonzero to fake an "X I/O error"
7215 on a particular display. */
7217 static struct x_display_info *XTread_socket_fake_io_error;
7219 /* When we find no input here, we occasionally do a no-op command
7220 to verify that the X server is still running and we can still talk with it.
7221 We try all the open displays, one by one.
7222 This variable is used for cycling thru the displays. */
7224 static struct x_display_info *next_noop_dpyinfo;
7226 enum
7228 X_EVENT_NORMAL,
7229 X_EVENT_GOTO_OUT,
7230 X_EVENT_DROP
7233 /* Filter events for the current X input method.
7234 DPYINFO is the display this event is for.
7235 EVENT is the X event to filter.
7237 Returns non-zero if the event was filtered, caller shall not process
7238 this event further.
7239 Returns zero if event is wasn't filtered. */
7241 #ifdef HAVE_X_I18N
7242 static int
7243 x_filter_event (struct x_display_info *dpyinfo, XEvent *event)
7245 /* XFilterEvent returns non-zero if the input method has
7246 consumed the event. We pass the frame's X window to
7247 XFilterEvent because that's the one for which the IC
7248 was created. */
7250 struct frame *f1 = x_any_window_to_frame (dpyinfo,
7251 event->xclient.window);
7253 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
7255 #endif
7257 #ifdef USE_GTK
7258 static int current_count;
7259 static int current_finish;
7260 static struct input_event *current_hold_quit;
7262 /* This is the filter function invoked by the GTK event loop.
7263 It is invoked before the XEvent is translated to a GdkEvent,
7264 so we have a chance to act on the event before GTK. */
7265 static GdkFilterReturn
7266 event_handler_gdk (GdkXEvent *gxev, GdkEvent *ev, gpointer data)
7268 XEvent *xev = (XEvent *) gxev;
7270 block_input ();
7271 if (current_count >= 0)
7273 struct x_display_info *dpyinfo;
7275 dpyinfo = x_display_info_for_display (xev->xany.display);
7277 #ifdef HAVE_X_I18N
7278 /* Filter events for the current X input method.
7279 GTK calls XFilterEvent but not for key press and release,
7280 so we do it here. */
7281 if ((xev->type == KeyPress || xev->type == KeyRelease)
7282 && dpyinfo
7283 && x_filter_event (dpyinfo, xev))
7285 unblock_input ();
7286 return GDK_FILTER_REMOVE;
7288 #endif
7290 if (! dpyinfo)
7291 current_finish = X_EVENT_NORMAL;
7292 else
7293 current_count
7294 += handle_one_xevent (dpyinfo, xev, &current_finish,
7295 current_hold_quit);
7297 else
7298 current_finish = x_dispatch_event (xev, xev->xany.display);
7300 unblock_input ();
7302 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
7303 return GDK_FILTER_REMOVE;
7305 return GDK_FILTER_CONTINUE;
7307 #endif /* USE_GTK */
7310 static void xembed_send_message (struct frame *f, Time,
7311 enum xembed_message,
7312 long detail, long data1, long data2);
7314 static void
7315 x_net_wm_state (struct frame *f, Window window)
7317 int value = FULLSCREEN_NONE;
7318 Lisp_Object lval = Qnil;
7319 bool sticky = false;
7321 get_current_wm_state (f, window, &value, &sticky);
7323 switch (value)
7325 case FULLSCREEN_WIDTH:
7326 lval = Qfullwidth;
7327 break;
7328 case FULLSCREEN_HEIGHT:
7329 lval = Qfullheight;
7330 break;
7331 case FULLSCREEN_BOTH:
7332 lval = Qfullboth;
7333 break;
7334 case FULLSCREEN_MAXIMIZED:
7335 lval = Qmaximized;
7336 break;
7339 frame_size_history_add
7340 (f, Qx_net_wm_state, 0, 0,
7341 list2 (get_frame_param (f, Qfullscreen), lval));
7343 store_frame_param (f, Qfullscreen, lval);
7344 /** store_frame_param (f, Qsticky, sticky ? Qt : Qnil); **/
7347 /* Handles the XEvent EVENT on display DPYINFO.
7349 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
7350 *FINISH is zero if caller should continue reading events.
7351 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
7352 *EVENT is unchanged unless we're processing KeyPress event.
7354 We return the number of characters stored into the buffer. */
7356 static int
7357 handle_one_xevent (struct x_display_info *dpyinfo,
7358 const XEvent *event,
7359 int *finish, struct input_event *hold_quit)
7361 union buffered_input_event inev;
7362 int count = 0;
7363 int do_help = 0;
7364 ptrdiff_t nbytes = 0;
7365 struct frame *any, *f = NULL;
7366 struct coding_system coding;
7367 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
7368 /* This holds the state XLookupString needs to implement dead keys
7369 and other tricks known as "compose processing". _X Window System_
7370 says that a portable program can't use this, but Stephen Gildea assures
7371 me that letting the compiler initialize it to zeros will work okay. */
7372 static XComposeStatus compose_status;
7374 USE_SAFE_ALLOCA;
7376 *finish = X_EVENT_NORMAL;
7378 EVENT_INIT (inev.ie);
7379 inev.ie.kind = NO_EVENT;
7380 inev.ie.arg = Qnil;
7382 any = x_any_window_to_frame (dpyinfo, event->xany.window);
7384 if (any && any->wait_event_type == event->type)
7385 any->wait_event_type = 0; /* Indicates we got it. */
7387 switch (event->type)
7389 case ClientMessage:
7391 if (event->xclient.message_type == dpyinfo->Xatom_wm_protocols
7392 && event->xclient.format == 32)
7394 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_take_focus)
7396 /* Use the value returned by x_any_window_to_frame
7397 because this could be the shell widget window
7398 if the frame has no title bar. */
7399 f = any;
7400 #ifdef HAVE_X_I18N
7401 /* Not quite sure this is needed -pd */
7402 if (f && FRAME_XIC (f))
7403 XSetICFocus (FRAME_XIC (f));
7404 #endif
7405 #if false
7406 /* Emacs sets WM hints whose `input' field is `true'. This
7407 instructs the WM to set the input focus automatically for
7408 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
7409 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
7410 it has set the focus. So, XSetInputFocus below is not
7411 needed.
7413 The call to XSetInputFocus below has also caused trouble. In
7414 cases where the XSetInputFocus done by the WM and the one
7415 below are temporally close (on a fast machine), the call
7416 below can generate additional FocusIn events which confuse
7417 Emacs. */
7419 /* Since we set WM_TAKE_FOCUS, we must call
7420 XSetInputFocus explicitly. But not if f is null,
7421 since that might be an event for a deleted frame. */
7422 if (f)
7424 Display *d = event->xclient.display;
7425 /* Catch and ignore errors, in case window has been
7426 iconified by a window manager such as GWM. */
7427 x_catch_errors (d);
7428 XSetInputFocus (d, event->xclient.window,
7429 /* The ICCCM says this is
7430 the only valid choice. */
7431 RevertToParent,
7432 event->xclient.data.l[1]);
7433 /* This is needed to detect the error
7434 if there is an error. */
7435 XSync (d, False);
7436 x_uncatch_errors ();
7438 /* Not certain about handling scroll bars here */
7439 #endif
7440 goto done;
7443 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_save_yourself)
7445 /* Save state modify the WM_COMMAND property to
7446 something which can reinstate us. This notifies
7447 the session manager, who's looking for such a
7448 PropertyNotify. Can restart processing when
7449 a keyboard or mouse event arrives. */
7450 /* If we have a session manager, don't set this.
7451 KDE will then start two Emacsen, one for the
7452 session manager and one for this. */
7453 #ifdef HAVE_X_SM
7454 if (! x_session_have_connection ())
7455 #endif
7457 f = x_top_window_to_frame (dpyinfo,
7458 event->xclient.window);
7459 /* This is just so we only give real data once
7460 for a single Emacs process. */
7461 if (f == SELECTED_FRAME ())
7462 XSetCommand (FRAME_X_DISPLAY (f),
7463 event->xclient.window,
7464 initial_argv, initial_argc);
7465 else if (f)
7466 XSetCommand (FRAME_X_DISPLAY (f),
7467 event->xclient.window,
7468 0, 0);
7470 goto done;
7473 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_delete_window)
7475 f = any;
7476 if (!f)
7477 goto OTHER; /* May be a dialog that is to be removed */
7479 inev.ie.kind = DELETE_WINDOW_EVENT;
7480 XSETFRAME (inev.ie.frame_or_window, f);
7481 goto done;
7484 goto done;
7487 if (event->xclient.message_type == dpyinfo->Xatom_wm_configure_denied)
7488 goto done;
7490 if (event->xclient.message_type == dpyinfo->Xatom_wm_window_moved)
7492 int new_x, new_y;
7493 f = x_window_to_frame (dpyinfo, event->xclient.window);
7495 new_x = event->xclient.data.s[0];
7496 new_y = event->xclient.data.s[1];
7498 if (f)
7500 f->left_pos = new_x;
7501 f->top_pos = new_y;
7503 goto done;
7506 #ifdef HACK_EDITRES
7507 if (event->xclient.message_type == dpyinfo->Xatom_editres)
7509 f = any;
7510 if (f)
7511 _XEditResCheckMessages (f->output_data.x->widget,
7512 NULL, (XEvent *) event, NULL);
7513 goto done;
7515 #endif /* HACK_EDITRES */
7517 if (event->xclient.message_type == dpyinfo->Xatom_DONE
7518 || event->xclient.message_type == dpyinfo->Xatom_PAGE)
7520 /* Ghostview job completed. Kill it. We could
7521 reply with "Next" if we received "Page", but we
7522 currently never do because we are interested in
7523 images, only, which should have 1 page. */
7524 Pixmap pixmap = (Pixmap) event->xclient.data.l[1];
7525 f = x_window_to_frame (dpyinfo, event->xclient.window);
7526 if (!f)
7527 goto OTHER;
7528 x_kill_gs_process (pixmap, f);
7529 expose_frame (f, 0, 0, 0, 0);
7530 goto done;
7533 #ifdef USE_TOOLKIT_SCROLL_BARS
7534 /* Scroll bar callbacks send a ClientMessage from which
7535 we construct an input_event. */
7536 if (event->xclient.message_type == dpyinfo->Xatom_Scrollbar)
7538 x_scroll_bar_to_input_event (event, &inev.ie);
7539 *finish = X_EVENT_GOTO_OUT;
7540 goto done;
7542 else if (event->xclient.message_type == dpyinfo->Xatom_Horizontal_Scrollbar)
7544 x_horizontal_scroll_bar_to_input_event (event, &inev.ie);
7545 *finish = X_EVENT_GOTO_OUT;
7546 goto done;
7548 #endif /* USE_TOOLKIT_SCROLL_BARS */
7550 /* XEmbed messages from the embedder (if any). */
7551 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
7553 enum xembed_message msg = event->xclient.data.l[1];
7554 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
7555 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7557 *finish = X_EVENT_GOTO_OUT;
7558 goto done;
7561 xft_settings_event (dpyinfo, event);
7563 f = any;
7564 if (!f)
7565 goto OTHER;
7566 if (x_handle_dnd_message (f, &event->xclient, dpyinfo, &inev.ie))
7567 *finish = X_EVENT_DROP;
7569 break;
7571 case SelectionNotify:
7572 x_display_set_last_user_time (dpyinfo, event->xselection.time);
7573 #ifdef USE_X_TOOLKIT
7574 if (! x_window_to_frame (dpyinfo, event->xselection.requestor))
7575 goto OTHER;
7576 #endif /* not USE_X_TOOLKIT */
7577 x_handle_selection_notify (&event->xselection);
7578 break;
7580 case SelectionClear: /* Someone has grabbed ownership. */
7581 x_display_set_last_user_time (dpyinfo, event->xselectionclear.time);
7582 #ifdef USE_X_TOOLKIT
7583 if (! x_window_to_frame (dpyinfo, event->xselectionclear.window))
7584 goto OTHER;
7585 #endif /* USE_X_TOOLKIT */
7587 const XSelectionClearEvent *eventp = &event->xselectionclear;
7589 inev.sie.kind = SELECTION_CLEAR_EVENT;
7590 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
7591 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
7592 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
7594 break;
7596 case SelectionRequest: /* Someone wants our selection. */
7597 x_display_set_last_user_time (dpyinfo, event->xselectionrequest.time);
7598 #ifdef USE_X_TOOLKIT
7599 if (!x_window_to_frame (dpyinfo, event->xselectionrequest.owner))
7600 goto OTHER;
7601 #endif /* USE_X_TOOLKIT */
7603 const XSelectionRequestEvent *eventp = &event->xselectionrequest;
7605 inev.sie.kind = SELECTION_REQUEST_EVENT;
7606 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
7607 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
7608 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
7609 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
7610 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
7611 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
7613 break;
7615 case PropertyNotify:
7616 x_display_set_last_user_time (dpyinfo, event->xproperty.time);
7617 f = x_top_window_to_frame (dpyinfo, event->xproperty.window);
7618 if (f && event->xproperty.atom == dpyinfo->Xatom_net_wm_state)
7620 bool not_hidden = x_handle_net_wm_state (f, &event->xproperty);
7621 if (not_hidden && FRAME_ICONIFIED_P (f))
7623 /* Gnome shell does not iconify us when C-z is pressed.
7624 It hides the frame. So if our state says we aren't
7625 hidden anymore, treat it as deiconified. */
7626 SET_FRAME_VISIBLE (f, 1);
7627 SET_FRAME_ICONIFIED (f, false);
7628 f->output_data.x->has_been_visible = true;
7629 inev.ie.kind = DEICONIFY_EVENT;
7630 XSETFRAME (inev.ie.frame_or_window, f);
7632 else if (! not_hidden && ! FRAME_ICONIFIED_P (f))
7634 SET_FRAME_VISIBLE (f, 0);
7635 SET_FRAME_ICONIFIED (f, true);
7636 inev.ie.kind = ICONIFY_EVENT;
7637 XSETFRAME (inev.ie.frame_or_window, f);
7641 x_handle_property_notify (&event->xproperty);
7642 xft_settings_event (dpyinfo, event);
7643 goto OTHER;
7645 case ReparentNotify:
7646 f = x_top_window_to_frame (dpyinfo, event->xreparent.window);
7647 if (f)
7649 f->output_data.x->parent_desc = event->xreparent.parent;
7650 x_real_positions (f, &f->left_pos, &f->top_pos);
7652 /* Perhaps reparented due to a WM restart. Reset this. */
7653 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
7654 FRAME_DISPLAY_INFO (f)->net_supported_window = 0;
7656 x_set_frame_alpha (f);
7658 goto OTHER;
7660 case Expose:
7661 f = x_window_to_frame (dpyinfo, event->xexpose.window);
7662 if (f)
7664 if (!FRAME_VISIBLE_P (f))
7666 SET_FRAME_VISIBLE (f, 1);
7667 SET_FRAME_ICONIFIED (f, false);
7668 f->output_data.x->has_been_visible = true;
7669 SET_FRAME_GARBAGED (f);
7671 else
7673 #ifdef USE_GTK
7674 /* This seems to be needed for GTK 2.6 and later, see
7675 http://debbugs.gnu.org/cgi/bugreport.cgi?bug=15398. */
7676 x_clear_area (f,
7677 event->xexpose.x, event->xexpose.y,
7678 event->xexpose.width, event->xexpose.height);
7679 #endif
7680 expose_frame (f, event->xexpose.x, event->xexpose.y,
7681 event->xexpose.width, event->xexpose.height);
7684 else
7686 #ifndef USE_TOOLKIT_SCROLL_BARS
7687 struct scroll_bar *bar;
7688 #endif
7689 #if defined USE_LUCID
7690 /* Submenus of the Lucid menu bar aren't widgets
7691 themselves, so there's no way to dispatch events
7692 to them. Recognize this case separately. */
7694 Widget widget = x_window_to_menu_bar (event->xexpose.window);
7695 if (widget)
7696 xlwmenu_redisplay (widget);
7698 #endif /* USE_LUCID */
7700 #ifdef USE_TOOLKIT_SCROLL_BARS
7701 /* Dispatch event to the widget. */
7702 goto OTHER;
7703 #else /* not USE_TOOLKIT_SCROLL_BARS */
7704 bar = x_window_to_scroll_bar (event->xexpose.display,
7705 event->xexpose.window, 2);
7707 if (bar)
7708 x_scroll_bar_expose (bar, event);
7709 #ifdef USE_X_TOOLKIT
7710 else
7711 goto OTHER;
7712 #endif /* USE_X_TOOLKIT */
7713 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7715 break;
7717 case GraphicsExpose: /* This occurs when an XCopyArea's
7718 source area was obscured or not
7719 available. */
7720 f = x_window_to_frame (dpyinfo, event->xgraphicsexpose.drawable);
7721 if (f)
7722 expose_frame (f, event->xgraphicsexpose.x,
7723 event->xgraphicsexpose.y,
7724 event->xgraphicsexpose.width,
7725 event->xgraphicsexpose.height);
7726 #ifdef USE_X_TOOLKIT
7727 else
7728 goto OTHER;
7729 #endif /* USE_X_TOOLKIT */
7730 break;
7732 case NoExpose: /* This occurs when an XCopyArea's
7733 source area was completely
7734 available. */
7735 break;
7737 case UnmapNotify:
7738 /* Redo the mouse-highlight after the tooltip has gone. */
7739 if (event->xunmap.window == tip_window)
7741 tip_window = 0;
7742 x_redo_mouse_highlight (dpyinfo);
7745 f = x_top_window_to_frame (dpyinfo, event->xunmap.window);
7746 if (f) /* F may no longer exist if
7747 the frame was deleted. */
7749 bool visible = FRAME_VISIBLE_P (f);
7750 /* While a frame is unmapped, display generation is
7751 disabled; you don't want to spend time updating a
7752 display that won't ever be seen. */
7753 SET_FRAME_VISIBLE (f, 0);
7754 /* We can't distinguish, from the event, whether the window
7755 has become iconified or invisible. So assume, if it
7756 was previously visible, than now it is iconified.
7757 But x_make_frame_invisible clears both
7758 the visible flag and the iconified flag;
7759 and that way, we know the window is not iconified now. */
7760 if (visible || FRAME_ICONIFIED_P (f))
7762 SET_FRAME_ICONIFIED (f, true);
7763 inev.ie.kind = ICONIFY_EVENT;
7764 XSETFRAME (inev.ie.frame_or_window, f);
7767 goto OTHER;
7769 case MapNotify:
7770 /* We use x_top_window_to_frame because map events can
7771 come for sub-windows and they don't mean that the
7772 frame is visible. */
7773 f = x_top_window_to_frame (dpyinfo, event->xmap.window);
7774 if (f)
7776 bool iconified = FRAME_ICONIFIED_P (f);
7778 /* Check if fullscreen was specified before we where mapped the
7779 first time, i.e. from the command line. */
7780 if (!f->output_data.x->has_been_visible)
7781 x_check_fullscreen (f);
7783 SET_FRAME_VISIBLE (f, 1);
7784 SET_FRAME_ICONIFIED (f, false);
7785 f->output_data.x->has_been_visible = true;
7787 if (iconified)
7789 inev.ie.kind = DEICONIFY_EVENT;
7790 XSETFRAME (inev.ie.frame_or_window, f);
7792 else if (! NILP (Vframe_list) && ! NILP (XCDR (Vframe_list)))
7793 /* Force a redisplay sooner or later to update the
7794 frame titles in case this is the second frame. */
7795 record_asynch_buffer_change ();
7797 #ifdef USE_GTK
7798 xg_frame_resized (f, -1, -1);
7799 #endif
7801 goto OTHER;
7803 case KeyPress:
7805 x_display_set_last_user_time (dpyinfo, event->xkey.time);
7806 ignore_next_mouse_click_timeout = 0;
7808 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
7809 /* Dispatch KeyPress events when in menu. */
7810 if (popup_activated ())
7811 goto OTHER;
7812 #endif
7814 f = any;
7816 /* If mouse-highlight is an integer, input clears out
7817 mouse highlighting. */
7818 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
7819 #if ! defined (USE_GTK)
7820 && (f == 0
7821 || !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
7822 #endif
7825 clear_mouse_face (hlinfo);
7826 hlinfo->mouse_face_hidden = true;
7829 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
7830 if (f == 0)
7832 /* Scroll bars consume key events, but we want
7833 the keys to go to the scroll bar's frame. */
7834 Widget widget = XtWindowToWidget (dpyinfo->display,
7835 event->xkey.window);
7836 if (widget && XmIsScrollBar (widget))
7838 widget = XtParent (widget);
7839 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
7842 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
7844 if (f != 0)
7846 KeySym keysym, orig_keysym;
7847 /* al%imercury@uunet.uu.net says that making this 81
7848 instead of 80 fixed a bug whereby meta chars made
7849 his Emacs hang.
7851 It seems that some version of XmbLookupString has
7852 a bug of not returning XBufferOverflow in
7853 status_return even if the input is too long to
7854 fit in 81 bytes. So, we must prepare sufficient
7855 bytes for copy_buffer. 513 bytes (256 chars for
7856 two-byte character set) seems to be a fairly good
7857 approximation. -- 2000.8.10 handa@etl.go.jp */
7858 unsigned char copy_buffer[513];
7859 unsigned char *copy_bufptr = copy_buffer;
7860 int copy_bufsiz = sizeof (copy_buffer);
7861 int modifiers;
7862 Lisp_Object coding_system = Qlatin_1;
7863 Lisp_Object c;
7864 /* Event will be modified. */
7865 XKeyEvent xkey = event->xkey;
7867 #ifdef USE_GTK
7868 /* Don't pass keys to GTK. A Tab will shift focus to the
7869 tool bar in GTK 2.4. Keys will still go to menus and
7870 dialogs because in that case popup_activated is nonzero
7871 (see above). */
7872 *finish = X_EVENT_DROP;
7873 #endif
7875 xkey.state |= x_emacs_to_x_modifiers (FRAME_DISPLAY_INFO (f),
7876 extra_keyboard_modifiers);
7877 modifiers = xkey.state;
7879 /* This will have to go some day... */
7881 /* make_lispy_event turns chars into control chars.
7882 Don't do it here because XLookupString is too eager. */
7883 xkey.state &= ~ControlMask;
7884 xkey.state &= ~(dpyinfo->meta_mod_mask
7885 | dpyinfo->super_mod_mask
7886 | dpyinfo->hyper_mod_mask
7887 | dpyinfo->alt_mod_mask);
7889 /* In case Meta is ComposeCharacter,
7890 clear its status. According to Markus Ehrnsperger
7891 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
7892 this enables ComposeCharacter to work whether or
7893 not it is combined with Meta. */
7894 if (modifiers & dpyinfo->meta_mod_mask)
7895 memset (&compose_status, 0, sizeof (compose_status));
7897 #ifdef HAVE_X_I18N
7898 if (FRAME_XIC (f))
7900 Status status_return;
7902 coding_system = Vlocale_coding_system;
7903 nbytes = XmbLookupString (FRAME_XIC (f),
7904 &xkey, (char *) copy_bufptr,
7905 copy_bufsiz, &keysym,
7906 &status_return);
7907 if (status_return == XBufferOverflow)
7909 copy_bufsiz = nbytes + 1;
7910 copy_bufptr = alloca (copy_bufsiz);
7911 nbytes = XmbLookupString (FRAME_XIC (f),
7912 &xkey, (char *) copy_bufptr,
7913 copy_bufsiz, &keysym,
7914 &status_return);
7916 /* Xutf8LookupString is a new but already deprecated interface. -stef */
7917 if (status_return == XLookupNone)
7918 break;
7919 else if (status_return == XLookupChars)
7921 keysym = NoSymbol;
7922 modifiers = 0;
7924 else if (status_return != XLookupKeySym
7925 && status_return != XLookupBoth)
7926 emacs_abort ();
7928 else
7929 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
7930 copy_bufsiz, &keysym,
7931 &compose_status);
7932 #else
7933 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
7934 copy_bufsiz, &keysym,
7935 &compose_status);
7936 #endif
7938 /* If not using XIM/XIC, and a compose sequence is in progress,
7939 we break here. Otherwise, chars_matched is always 0. */
7940 if (compose_status.chars_matched > 0 && nbytes == 0)
7941 break;
7943 memset (&compose_status, 0, sizeof (compose_status));
7944 orig_keysym = keysym;
7946 /* Common for all keysym input events. */
7947 XSETFRAME (inev.ie.frame_or_window, f);
7948 inev.ie.modifiers
7949 = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), modifiers);
7950 inev.ie.timestamp = xkey.time;
7952 /* First deal with keysyms which have defined
7953 translations to characters. */
7954 if (keysym >= 32 && keysym < 128)
7955 /* Avoid explicitly decoding each ASCII character. */
7957 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
7958 inev.ie.code = keysym;
7959 goto done_keysym;
7962 /* Keysyms directly mapped to Unicode characters. */
7963 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
7965 if (keysym < 0x01000080)
7966 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
7967 else
7968 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
7969 inev.ie.code = keysym & 0xFFFFFF;
7970 goto done_keysym;
7973 /* Now non-ASCII. */
7974 if (HASH_TABLE_P (Vx_keysym_table)
7975 && (c = Fgethash (make_number (keysym),
7976 Vx_keysym_table,
7977 Qnil),
7978 NATNUMP (c)))
7980 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
7981 ? ASCII_KEYSTROKE_EVENT
7982 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
7983 inev.ie.code = XFASTINT (c);
7984 goto done_keysym;
7987 /* Random non-modifier sorts of keysyms. */
7988 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
7989 || keysym == XK_Delete
7990 #ifdef XK_ISO_Left_Tab
7991 || (keysym >= XK_ISO_Left_Tab
7992 && keysym <= XK_ISO_Enter)
7993 #endif
7994 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
7995 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
7996 #ifdef HPUX
7997 /* This recognizes the "extended function
7998 keys". It seems there's no cleaner way.
7999 Test IsModifierKey to avoid handling
8000 mode_switch incorrectly. */
8001 || (XK_Select <= keysym && keysym < XK_KP_Space)
8002 #endif
8003 #ifdef XK_dead_circumflex
8004 || orig_keysym == XK_dead_circumflex
8005 #endif
8006 #ifdef XK_dead_grave
8007 || orig_keysym == XK_dead_grave
8008 #endif
8009 #ifdef XK_dead_tilde
8010 || orig_keysym == XK_dead_tilde
8011 #endif
8012 #ifdef XK_dead_diaeresis
8013 || orig_keysym == XK_dead_diaeresis
8014 #endif
8015 #ifdef XK_dead_macron
8016 || orig_keysym == XK_dead_macron
8017 #endif
8018 #ifdef XK_dead_degree
8019 || orig_keysym == XK_dead_degree
8020 #endif
8021 #ifdef XK_dead_acute
8022 || orig_keysym == XK_dead_acute
8023 #endif
8024 #ifdef XK_dead_cedilla
8025 || orig_keysym == XK_dead_cedilla
8026 #endif
8027 #ifdef XK_dead_breve
8028 || orig_keysym == XK_dead_breve
8029 #endif
8030 #ifdef XK_dead_ogonek
8031 || orig_keysym == XK_dead_ogonek
8032 #endif
8033 #ifdef XK_dead_caron
8034 || orig_keysym == XK_dead_caron
8035 #endif
8036 #ifdef XK_dead_doubleacute
8037 || orig_keysym == XK_dead_doubleacute
8038 #endif
8039 #ifdef XK_dead_abovedot
8040 || orig_keysym == XK_dead_abovedot
8041 #endif
8042 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
8043 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
8044 /* Any "vendor-specific" key is ok. */
8045 || (orig_keysym & (1 << 28))
8046 || (keysym != NoSymbol && nbytes == 0))
8047 && ! (IsModifierKey (orig_keysym)
8048 /* The symbols from XK_ISO_Lock
8049 to XK_ISO_Last_Group_Lock
8050 don't have real modifiers but
8051 should be treated similarly to
8052 Mode_switch by Emacs. */
8053 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
8054 || (XK_ISO_Lock <= orig_keysym
8055 && orig_keysym <= XK_ISO_Last_Group_Lock)
8056 #endif
8059 STORE_KEYSYM_FOR_DEBUG (keysym);
8060 /* make_lispy_event will convert this to a symbolic
8061 key. */
8062 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
8063 inev.ie.code = keysym;
8064 goto done_keysym;
8067 { /* Raw bytes, not keysym. */
8068 ptrdiff_t i;
8069 int nchars, len;
8071 for (i = 0, nchars = 0; i < nbytes; i++)
8073 if (ASCII_CHAR_P (copy_bufptr[i]))
8074 nchars++;
8075 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
8078 if (nchars < nbytes)
8080 /* Decode the input data. */
8082 /* The input should be decoded with `coding_system'
8083 which depends on which X*LookupString function
8084 we used just above and the locale. */
8085 setup_coding_system (coding_system, &coding);
8086 coding.src_multibyte = false;
8087 coding.dst_multibyte = true;
8088 /* The input is converted to events, thus we can't
8089 handle composition. Anyway, there's no XIM that
8090 gives us composition information. */
8091 coding.common_flags &= ~CODING_ANNOTATION_MASK;
8093 SAFE_NALLOCA (coding.destination, MAX_MULTIBYTE_LENGTH,
8094 nbytes);
8095 coding.dst_bytes = MAX_MULTIBYTE_LENGTH * nbytes;
8096 coding.mode |= CODING_MODE_LAST_BLOCK;
8097 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
8098 nbytes = coding.produced;
8099 nchars = coding.produced_char;
8100 copy_bufptr = coding.destination;
8103 /* Convert the input data to a sequence of
8104 character events. */
8105 for (i = 0; i < nbytes; i += len)
8107 int ch;
8108 if (nchars == nbytes)
8109 ch = copy_bufptr[i], len = 1;
8110 else
8111 ch = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
8112 inev.ie.kind = (SINGLE_BYTE_CHAR_P (ch)
8113 ? ASCII_KEYSTROKE_EVENT
8114 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
8115 inev.ie.code = ch;
8116 kbd_buffer_store_buffered_event (&inev, hold_quit);
8119 count += nchars;
8121 inev.ie.kind = NO_EVENT; /* Already stored above. */
8123 if (keysym == NoSymbol)
8124 break;
8126 /* FIXME: check side effects and remove this. */
8127 ((XEvent *) event)->xkey = xkey;
8129 done_keysym:
8130 #ifdef HAVE_X_I18N
8131 /* Don't dispatch this event since XtDispatchEvent calls
8132 XFilterEvent, and two calls in a row may freeze the
8133 client. */
8134 break;
8135 #else
8136 goto OTHER;
8137 #endif
8139 case KeyRelease:
8140 x_display_set_last_user_time (dpyinfo, event->xkey.time);
8141 #ifdef HAVE_X_I18N
8142 /* Don't dispatch this event since XtDispatchEvent calls
8143 XFilterEvent, and two calls in a row may freeze the
8144 client. */
8145 break;
8146 #else
8147 goto OTHER;
8148 #endif
8150 case EnterNotify:
8151 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
8152 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8154 f = any;
8156 if (f && x_mouse_click_focus_ignore_position)
8157 ignore_next_mouse_click_timeout = event->xmotion.time + 200;
8159 /* EnterNotify counts as mouse movement,
8160 so update things that depend on mouse position. */
8161 if (f && !f->output_data.x->hourglass_p)
8162 note_mouse_movement (f, &event->xmotion);
8163 #ifdef USE_GTK
8164 /* We may get an EnterNotify on the buttons in the toolbar. In that
8165 case we moved out of any highlighted area and need to note this. */
8166 if (!f && dpyinfo->last_mouse_glyph_frame)
8167 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
8168 #endif
8169 goto OTHER;
8171 case FocusIn:
8172 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8173 goto OTHER;
8175 case LeaveNotify:
8176 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
8177 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8179 f = x_top_window_to_frame (dpyinfo, event->xcrossing.window);
8180 if (f)
8182 if (f == hlinfo->mouse_face_mouse_frame)
8184 /* If we move outside the frame, then we're
8185 certainly no longer on any text in the frame. */
8186 clear_mouse_face (hlinfo);
8187 hlinfo->mouse_face_mouse_frame = 0;
8190 /* Generate a nil HELP_EVENT to cancel a help-echo.
8191 Do it only if there's something to cancel.
8192 Otherwise, the startup message is cleared when
8193 the mouse leaves the frame. */
8194 if (any_help_event_p)
8195 do_help = -1;
8197 #ifdef USE_GTK
8198 /* See comment in EnterNotify above */
8199 else if (dpyinfo->last_mouse_glyph_frame)
8200 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
8201 #endif
8202 goto OTHER;
8204 case FocusOut:
8205 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8206 goto OTHER;
8208 case MotionNotify:
8210 x_display_set_last_user_time (dpyinfo, event->xmotion.time);
8211 previous_help_echo_string = help_echo_string;
8212 help_echo_string = Qnil;
8214 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
8215 : x_window_to_frame (dpyinfo, event->xmotion.window));
8217 if (hlinfo->mouse_face_hidden)
8219 hlinfo->mouse_face_hidden = false;
8220 clear_mouse_face (hlinfo);
8223 #ifdef USE_GTK
8224 if (f && xg_event_is_for_scrollbar (f, event))
8225 f = 0;
8226 #endif
8227 if (f)
8230 /* Generate SELECT_WINDOW_EVENTs when needed.
8231 Don't let popup menus influence things (bug#1261). */
8232 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
8234 static Lisp_Object last_mouse_window;
8235 Lisp_Object window = window_from_coordinates
8236 (f, event->xmotion.x, event->xmotion.y, 0, false);
8238 /* Window will be selected only when it is not selected now and
8239 last mouse movement event was not in it. Minibuffer window
8240 will be selected only when it is active. */
8241 if (WINDOWP (window)
8242 && !EQ (window, last_mouse_window)
8243 && !EQ (window, selected_window)
8244 /* For click-to-focus window managers
8245 create event iff we don't leave the
8246 selected frame. */
8247 && (focus_follows_mouse
8248 || (EQ (XWINDOW (window)->frame,
8249 XWINDOW (selected_window)->frame))))
8251 inev.ie.kind = SELECT_WINDOW_EVENT;
8252 inev.ie.frame_or_window = window;
8254 /* Remember the last window where we saw the mouse. */
8255 last_mouse_window = window;
8257 if (!note_mouse_movement (f, &event->xmotion))
8258 help_echo_string = previous_help_echo_string;
8260 else
8262 #ifndef USE_TOOLKIT_SCROLL_BARS
8263 struct scroll_bar *bar
8264 = x_window_to_scroll_bar (event->xmotion.display,
8265 event->xmotion.window, 2);
8267 if (bar)
8268 x_scroll_bar_note_movement (bar, &event->xmotion);
8269 #endif /* USE_TOOLKIT_SCROLL_BARS */
8271 /* If we move outside the frame, then we're
8272 certainly no longer on any text in the frame. */
8273 clear_mouse_face (hlinfo);
8276 /* If the contents of the global variable help_echo_string
8277 has changed, generate a HELP_EVENT. */
8278 if (!NILP (help_echo_string)
8279 || !NILP (previous_help_echo_string))
8280 do_help = 1;
8281 goto OTHER;
8284 case ConfigureNotify:
8285 f = x_top_window_to_frame (dpyinfo, event->xconfigure.window);
8286 #ifdef USE_CAIRO
8287 if (f) x_cr_destroy_surface (f);
8288 #endif
8289 #ifdef USE_GTK
8290 if (!f
8291 && (f = any)
8292 && event->xconfigure.window == FRAME_X_WINDOW (f))
8294 xg_frame_resized (f, event->xconfigure.width,
8295 event->xconfigure.height);
8296 #ifdef USE_CAIRO
8297 x_cr_destroy_surface (f);
8298 #endif
8299 f = 0;
8301 #endif
8302 if (f)
8304 x_net_wm_state (f, event->xconfigure.window);
8306 #ifdef USE_X_TOOLKIT
8307 /* Tip frames are pure X window, set size for them. */
8308 if (! NILP (tip_frame) && XFRAME (tip_frame) == f)
8310 if (FRAME_PIXEL_HEIGHT (f) != event->xconfigure.height
8311 || FRAME_PIXEL_WIDTH (f) != event->xconfigure.width)
8312 SET_FRAME_GARBAGED (f);
8313 FRAME_PIXEL_HEIGHT (f) = event->xconfigure.height;
8314 FRAME_PIXEL_WIDTH (f) = event->xconfigure.width;
8316 #endif
8318 #ifndef USE_X_TOOLKIT
8319 #ifndef USE_GTK
8320 int width = FRAME_PIXEL_TO_TEXT_WIDTH (f, event->xconfigure.width);
8321 int height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, event->xconfigure.height);
8323 /* In the toolkit version, change_frame_size
8324 is called by the code that handles resizing
8325 of the EmacsFrame widget. */
8327 /* Even if the number of character rows and columns has
8328 not changed, the font size may have changed, so we need
8329 to check the pixel dimensions as well. */
8330 if (width != FRAME_TEXT_WIDTH (f)
8331 || height != FRAME_TEXT_HEIGHT (f)
8332 || event->xconfigure.width != FRAME_PIXEL_WIDTH (f)
8333 || event->xconfigure.height != FRAME_PIXEL_HEIGHT (f))
8335 change_frame_size (f, width, height, false, true, false, true);
8336 x_clear_under_internal_border (f);
8337 SET_FRAME_GARBAGED (f);
8338 cancel_mouse_face (f);
8340 #endif /* not USE_GTK */
8341 #endif
8343 #ifdef USE_GTK
8344 /* GTK creates windows but doesn't map them.
8345 Only get real positions when mapped. */
8346 if (FRAME_GTK_OUTER_WIDGET (f)
8347 && gtk_widget_get_mapped (FRAME_GTK_OUTER_WIDGET (f)))
8348 #endif
8349 x_real_positions (f, &f->left_pos, &f->top_pos);
8351 #ifdef HAVE_X_I18N
8352 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
8353 xic_set_statusarea (f);
8354 #endif
8357 goto OTHER;
8359 case ButtonRelease:
8360 case ButtonPress:
8362 /* If we decide we want to generate an event to be seen
8363 by the rest of Emacs, we put it here. */
8364 bool tool_bar_p = false;
8366 memset (&compose_status, 0, sizeof (compose_status));
8367 dpyinfo->last_mouse_glyph_frame = NULL;
8368 x_display_set_last_user_time (dpyinfo, event->xbutton.time);
8370 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
8371 : x_window_to_frame (dpyinfo, event->xbutton.window));
8373 #ifdef USE_GTK
8374 if (f && xg_event_is_for_scrollbar (f, event))
8375 f = 0;
8376 #endif
8377 if (f)
8379 #if ! defined (USE_GTK)
8380 /* Is this in the tool-bar? */
8381 if (WINDOWP (f->tool_bar_window)
8382 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
8384 Lisp_Object window;
8385 int x = event->xbutton.x;
8386 int y = event->xbutton.y;
8388 window = window_from_coordinates (f, x, y, 0, true);
8389 tool_bar_p = EQ (window, f->tool_bar_window);
8391 if (tool_bar_p && event->xbutton.button < 4)
8392 handle_tool_bar_click
8393 (f, x, y, event->xbutton.type == ButtonPress,
8394 x_x_to_emacs_modifiers (dpyinfo, event->xbutton.state));
8396 #endif /* !USE_GTK */
8398 if (!tool_bar_p)
8399 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8400 if (! popup_activated ())
8401 #endif
8403 if (ignore_next_mouse_click_timeout)
8405 if (event->type == ButtonPress
8406 && event->xbutton.time > ignore_next_mouse_click_timeout)
8408 ignore_next_mouse_click_timeout = 0;
8409 construct_mouse_click (&inev.ie, &event->xbutton, f);
8411 if (event->type == ButtonRelease)
8412 ignore_next_mouse_click_timeout = 0;
8414 else
8415 construct_mouse_click (&inev.ie, &event->xbutton, f);
8417 if (FRAME_X_EMBEDDED_P (f))
8418 xembed_send_message (f, event->xbutton.time,
8419 XEMBED_REQUEST_FOCUS, 0, 0, 0);
8421 else
8423 struct scroll_bar *bar
8424 = x_window_to_scroll_bar (event->xbutton.display,
8425 event->xbutton.window, 2);
8427 #ifdef USE_TOOLKIT_SCROLL_BARS
8428 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
8429 scroll bars. */
8430 if (bar && event->xbutton.state & ControlMask)
8432 x_scroll_bar_handle_click (bar, event, &inev.ie);
8433 *finish = X_EVENT_DROP;
8435 #else /* not USE_TOOLKIT_SCROLL_BARS */
8436 if (bar)
8437 x_scroll_bar_handle_click (bar, event, &inev.ie);
8438 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8441 if (event->type == ButtonPress)
8443 dpyinfo->grabbed |= (1 << event->xbutton.button);
8444 dpyinfo->last_mouse_frame = f;
8445 #if ! defined (USE_GTK)
8446 if (f && !tool_bar_p)
8447 f->last_tool_bar_item = -1;
8448 #endif /* not USE_GTK */
8450 else
8451 dpyinfo->grabbed &= ~(1 << event->xbutton.button);
8453 /* Ignore any mouse motion that happened before this event;
8454 any subsequent mouse-movement Emacs events should reflect
8455 only motion after the ButtonPress/Release. */
8456 if (f != 0)
8457 f->mouse_moved = false;
8459 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8460 f = x_menubar_window_to_frame (dpyinfo, event);
8461 /* For a down-event in the menu bar,
8462 don't pass it to Xt right now.
8463 Instead, save it away
8464 and we will pass it to Xt from kbd_buffer_get_event.
8465 That way, we can run some Lisp code first. */
8466 if (! popup_activated ()
8467 #ifdef USE_GTK
8468 /* Gtk+ menus only react to the first three buttons. */
8469 && event->xbutton.button < 3
8470 #endif
8471 && f && event->type == ButtonPress
8472 /* Verify the event is really within the menu bar
8473 and not just sent to it due to grabbing. */
8474 && event->xbutton.x >= 0
8475 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
8476 && event->xbutton.y >= 0
8477 && event->xbutton.y < FRAME_MENUBAR_HEIGHT (f)
8478 && event->xbutton.same_screen)
8480 if (!f->output_data.x->saved_menu_event)
8481 f->output_data.x->saved_menu_event = xmalloc (sizeof *event);
8482 *f->output_data.x->saved_menu_event = *event;
8483 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT;
8484 XSETFRAME (inev.ie.frame_or_window, f);
8485 *finish = X_EVENT_DROP;
8487 else
8488 goto OTHER;
8489 #endif /* USE_X_TOOLKIT || USE_GTK */
8491 break;
8493 case CirculateNotify:
8494 goto OTHER;
8496 case CirculateRequest:
8497 goto OTHER;
8499 case VisibilityNotify:
8500 goto OTHER;
8502 case MappingNotify:
8503 /* Someone has changed the keyboard mapping - update the
8504 local cache. */
8505 switch (event->xmapping.request)
8507 case MappingModifier:
8508 x_find_modifier_meanings (dpyinfo);
8509 /* This is meant to fall through. */
8510 case MappingKeyboard:
8511 XRefreshKeyboardMapping ((XMappingEvent *) &event->xmapping);
8513 goto OTHER;
8515 case DestroyNotify:
8516 xft_settings_event (dpyinfo, event);
8517 break;
8519 default:
8520 OTHER:
8521 #ifdef USE_X_TOOLKIT
8522 block_input ();
8523 if (*finish != X_EVENT_DROP)
8524 XtDispatchEvent ((XEvent *) event);
8525 unblock_input ();
8526 #endif /* USE_X_TOOLKIT */
8527 break;
8530 done:
8531 if (inev.ie.kind != NO_EVENT)
8533 kbd_buffer_store_buffered_event (&inev, hold_quit);
8534 count++;
8537 if (do_help
8538 && !(hold_quit && hold_quit->kind != NO_EVENT))
8540 Lisp_Object frame;
8542 if (f)
8543 XSETFRAME (frame, f);
8544 else
8545 frame = Qnil;
8547 if (do_help > 0)
8549 any_help_event_p = true;
8550 gen_help_event (help_echo_string, frame, help_echo_window,
8551 help_echo_object, help_echo_pos);
8553 else
8555 help_echo_string = Qnil;
8556 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
8558 count++;
8561 SAFE_FREE ();
8562 return count;
8565 /* Handles the XEvent EVENT on display DISPLAY.
8566 This is used for event loops outside the normal event handling,
8567 i.e. looping while a popup menu or a dialog is posted.
8569 Returns the value handle_one_xevent sets in the finish argument. */
8571 x_dispatch_event (XEvent *event, Display *display)
8573 struct x_display_info *dpyinfo;
8574 int finish = X_EVENT_NORMAL;
8576 dpyinfo = x_display_info_for_display (display);
8578 if (dpyinfo)
8579 handle_one_xevent (dpyinfo, event, &finish, 0);
8581 return finish;
8584 /* Read events coming from the X server.
8585 Return as soon as there are no more events to be read.
8587 Return the number of characters stored into the buffer,
8588 thus pretending to be `read' (except the characters we store
8589 in the keyboard buffer can be multibyte, so are not necessarily
8590 C chars). */
8592 static int
8593 XTread_socket (struct terminal *terminal, struct input_event *hold_quit)
8595 int count = 0;
8596 bool event_found = false;
8597 struct x_display_info *dpyinfo = terminal->display_info.x;
8599 block_input ();
8601 /* For debugging, this gives a way to fake an I/O error. */
8602 if (dpyinfo == XTread_socket_fake_io_error)
8604 XTread_socket_fake_io_error = 0;
8605 x_io_error_quitter (dpyinfo->display);
8608 #ifndef USE_GTK
8609 while (XPending (dpyinfo->display))
8611 int finish;
8612 XEvent event;
8614 XNextEvent (dpyinfo->display, &event);
8616 #ifdef HAVE_X_I18N
8617 /* Filter events for the current X input method. */
8618 if (x_filter_event (dpyinfo, &event))
8619 continue;
8620 #endif
8621 event_found = true;
8623 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
8625 if (finish == X_EVENT_GOTO_OUT)
8626 break;
8629 #else /* USE_GTK */
8631 /* For GTK we must use the GTK event loop. But XEvents gets passed
8632 to our filter function above, and then to the big event switch.
8633 We use a bunch of globals to communicate with our filter function,
8634 that is kind of ugly, but it works.
8636 There is no way to do one display at the time, GTK just does events
8637 from all displays. */
8639 while (gtk_events_pending ())
8641 current_count = count;
8642 current_hold_quit = hold_quit;
8644 gtk_main_iteration ();
8646 count = current_count;
8647 current_count = -1;
8648 current_hold_quit = 0;
8650 if (current_finish == X_EVENT_GOTO_OUT)
8651 break;
8653 #endif /* USE_GTK */
8655 /* On some systems, an X bug causes Emacs to get no more events
8656 when the window is destroyed. Detect that. (1994.) */
8657 if (! event_found)
8659 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
8660 One XNOOP in 100 loops will make Emacs terminate.
8661 B. Bretthauer, 1994 */
8662 x_noop_count++;
8663 if (x_noop_count >= 100)
8665 x_noop_count=0;
8667 if (next_noop_dpyinfo == 0)
8668 next_noop_dpyinfo = x_display_list;
8670 XNoOp (next_noop_dpyinfo->display);
8672 /* Each time we get here, cycle through the displays now open. */
8673 next_noop_dpyinfo = next_noop_dpyinfo->next;
8677 /* If the focus was just given to an auto-raising frame,
8678 raise it now. FIXME: handle more than one such frame. */
8679 if (dpyinfo->x_pending_autoraise_frame)
8681 x_raise_frame (dpyinfo->x_pending_autoraise_frame);
8682 dpyinfo->x_pending_autoraise_frame = NULL;
8685 unblock_input ();
8687 return count;
8693 /***********************************************************************
8694 Text Cursor
8695 ***********************************************************************/
8697 /* Set clipping for output in glyph row ROW. W is the window in which
8698 we operate. GC is the graphics context to set clipping in.
8700 ROW may be a text row or, e.g., a mode line. Text rows must be
8701 clipped to the interior of the window dedicated to text display,
8702 mode lines must be clipped to the whole window. */
8704 static void
8705 x_clip_to_row (struct window *w, struct glyph_row *row,
8706 enum glyph_row_area area, GC gc)
8708 struct frame *f = XFRAME (WINDOW_FRAME (w));
8709 XRectangle clip_rect;
8710 int window_x, window_y, window_width;
8712 window_box (w, area, &window_x, &window_y, &window_width, 0);
8714 clip_rect.x = window_x;
8715 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
8716 clip_rect.y = max (clip_rect.y, window_y);
8717 clip_rect.width = window_width;
8718 clip_rect.height = row->visible_height;
8720 x_set_clip_rectangles (f, gc, &clip_rect, 1);
8724 /* Draw a hollow box cursor on window W in glyph row ROW. */
8726 static void
8727 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
8729 struct frame *f = XFRAME (WINDOW_FRAME (w));
8730 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8731 Display *dpy = FRAME_X_DISPLAY (f);
8732 int x, y, wd, h;
8733 XGCValues xgcv;
8734 struct glyph *cursor_glyph;
8735 GC gc;
8737 /* Get the glyph the cursor is on. If we can't tell because
8738 the current matrix is invalid or such, give up. */
8739 cursor_glyph = get_phys_cursor_glyph (w);
8740 if (cursor_glyph == NULL)
8741 return;
8743 /* Compute frame-relative coordinates for phys cursor. */
8744 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
8745 wd = w->phys_cursor_width - 1;
8747 /* The foreground of cursor_gc is typically the same as the normal
8748 background color, which can cause the cursor box to be invisible. */
8749 xgcv.foreground = f->output_data.x->cursor_pixel;
8750 if (dpyinfo->scratch_cursor_gc)
8751 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
8752 else
8753 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
8754 GCForeground, &xgcv);
8755 gc = dpyinfo->scratch_cursor_gc;
8757 /* When on R2L character, show cursor at the right edge of the
8758 glyph, unless the cursor box is as wide as the glyph or wider
8759 (the latter happens when x-stretch-cursor is non-nil). */
8760 if ((cursor_glyph->resolved_level & 1) != 0
8761 && cursor_glyph->pixel_width > wd)
8763 x += cursor_glyph->pixel_width - wd;
8764 if (wd > 0)
8765 wd -= 1;
8767 /* Set clipping, draw the rectangle, and reset clipping again. */
8768 x_clip_to_row (w, row, TEXT_AREA, gc);
8769 x_draw_rectangle (f, gc, x, y, wd, h - 1);
8770 x_reset_clip_rectangles (f, gc);
8774 /* Draw a bar cursor on window W in glyph row ROW.
8776 Implementation note: One would like to draw a bar cursor with an
8777 angle equal to the one given by the font property XA_ITALIC_ANGLE.
8778 Unfortunately, I didn't find a font yet that has this property set.
8779 --gerd. */
8781 static void
8782 x_draw_bar_cursor (struct window *w, struct glyph_row *row, int width, enum text_cursor_kinds kind)
8784 struct frame *f = XFRAME (w->frame);
8785 struct glyph *cursor_glyph;
8787 /* If cursor is out of bounds, don't draw garbage. This can happen
8788 in mini-buffer windows when switching between echo area glyphs
8789 and mini-buffer. */
8790 cursor_glyph = get_phys_cursor_glyph (w);
8791 if (cursor_glyph == NULL)
8792 return;
8794 /* If on an image, draw like a normal cursor. That's usually better
8795 visible than drawing a bar, esp. if the image is large so that
8796 the bar might not be in the window. */
8797 if (cursor_glyph->type == IMAGE_GLYPH)
8799 struct glyph_row *r;
8800 r = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
8801 draw_phys_cursor_glyph (w, r, DRAW_CURSOR);
8803 else
8805 Display *dpy = FRAME_X_DISPLAY (f);
8806 Window window = FRAME_X_WINDOW (f);
8807 GC gc = FRAME_DISPLAY_INFO (f)->scratch_cursor_gc;
8808 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
8809 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
8810 XGCValues xgcv;
8812 /* If the glyph's background equals the color we normally draw
8813 the bars cursor in, the bar cursor in its normal color is
8814 invisible. Use the glyph's foreground color instead in this
8815 case, on the assumption that the glyph's colors are chosen so
8816 that the glyph is legible. */
8817 if (face->background == f->output_data.x->cursor_pixel)
8818 xgcv.background = xgcv.foreground = face->foreground;
8819 else
8820 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
8821 xgcv.graphics_exposures = False;
8823 if (gc)
8824 XChangeGC (dpy, gc, mask, &xgcv);
8825 else
8827 gc = XCreateGC (dpy, window, mask, &xgcv);
8828 FRAME_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
8831 x_clip_to_row (w, row, TEXT_AREA, gc);
8833 if (kind == BAR_CURSOR)
8835 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8837 if (width < 0)
8838 width = FRAME_CURSOR_WIDTH (f);
8839 width = min (cursor_glyph->pixel_width, width);
8841 w->phys_cursor_width = width;
8843 /* If the character under cursor is R2L, draw the bar cursor
8844 on the right of its glyph, rather than on the left. */
8845 if ((cursor_glyph->resolved_level & 1) != 0)
8846 x += cursor_glyph->pixel_width - width;
8848 x_fill_rectangle (f, gc, x,
8849 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
8850 width, row->height);
8852 else /* HBAR_CURSOR */
8854 int dummy_x, dummy_y, dummy_h;
8855 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8857 if (width < 0)
8858 width = row->height;
8860 width = min (row->height, width);
8862 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
8863 &dummy_y, &dummy_h);
8865 if ((cursor_glyph->resolved_level & 1) != 0
8866 && cursor_glyph->pixel_width > w->phys_cursor_width - 1)
8867 x += cursor_glyph->pixel_width - w->phys_cursor_width + 1;
8868 x_fill_rectangle (f, gc, x,
8869 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
8870 row->height - width),
8871 w->phys_cursor_width - 1, width);
8874 x_reset_clip_rectangles (f, gc);
8879 /* RIF: Define cursor CURSOR on frame F. */
8881 static void
8882 x_define_frame_cursor (struct frame *f, Cursor cursor)
8884 if (!f->pointer_invisible
8885 && f->output_data.x->current_cursor != cursor)
8886 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
8887 f->output_data.x->current_cursor = cursor;
8891 /* RIF: Clear area on frame F. */
8893 static void
8894 x_clear_frame_area (struct frame *f, int x, int y, int width, int height)
8896 x_clear_area (f, x, y, width, height);
8897 #ifdef USE_GTK
8898 /* Must queue a redraw, because scroll bars might have been cleared. */
8899 if (FRAME_GTK_WIDGET (f))
8900 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
8901 #endif
8905 /* RIF: Draw cursor on window W. */
8907 static void
8908 x_draw_window_cursor (struct window *w, struct glyph_row *glyph_row, int x,
8909 int y, enum text_cursor_kinds cursor_type,
8910 int cursor_width, bool on_p, bool active_p)
8912 struct frame *f = XFRAME (WINDOW_FRAME (w));
8914 if (on_p)
8916 w->phys_cursor_type = cursor_type;
8917 w->phys_cursor_on_p = true;
8919 if (glyph_row->exact_window_width_line_p
8920 && (glyph_row->reversed_p
8921 ? (w->phys_cursor.hpos < 0)
8922 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
8924 glyph_row->cursor_in_fringe_p = true;
8925 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
8927 else
8929 switch (cursor_type)
8931 case HOLLOW_BOX_CURSOR:
8932 x_draw_hollow_cursor (w, glyph_row);
8933 break;
8935 case FILLED_BOX_CURSOR:
8936 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
8937 break;
8939 case BAR_CURSOR:
8940 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
8941 break;
8943 case HBAR_CURSOR:
8944 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
8945 break;
8947 case NO_CURSOR:
8948 w->phys_cursor_width = 0;
8949 break;
8951 default:
8952 emacs_abort ();
8956 #ifdef HAVE_X_I18N
8957 if (w == XWINDOW (f->selected_window))
8958 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
8959 xic_set_preeditarea (w, x, y);
8960 #endif
8963 XFlush (FRAME_X_DISPLAY (f));
8967 /* Icons. */
8969 /* Make the x-window of frame F use the gnu icon bitmap. */
8971 bool
8972 x_bitmap_icon (struct frame *f, Lisp_Object file)
8974 ptrdiff_t bitmap_id;
8976 if (FRAME_X_WINDOW (f) == 0)
8977 return true;
8979 /* Free up our existing icon bitmap and mask if any. */
8980 if (f->output_data.x->icon_bitmap > 0)
8981 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
8982 f->output_data.x->icon_bitmap = 0;
8984 if (STRINGP (file))
8986 #ifdef USE_GTK
8987 /* Use gtk_window_set_icon_from_file () if available,
8988 It's not restricted to bitmaps */
8989 if (xg_set_icon (f, file))
8990 return false;
8991 #endif /* USE_GTK */
8992 bitmap_id = x_create_bitmap_from_file (f, file);
8993 x_create_bitmap_mask (f, bitmap_id);
8995 else
8997 /* Create the GNU bitmap and mask if necessary. */
8998 if (FRAME_DISPLAY_INFO (f)->icon_bitmap_id < 0)
9000 ptrdiff_t rc = -1;
9002 #ifdef USE_GTK
9004 if (xg_set_icon (f, xg_default_icon_file)
9005 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
9007 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = -2;
9008 return false;
9011 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
9013 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
9014 if (rc != -1)
9015 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
9017 #endif
9019 /* If all else fails, use the (black and white) xbm image. */
9020 if (rc == -1)
9022 rc = x_create_bitmap_from_data (f, (char *) gnu_xbm_bits,
9023 gnu_xbm_width, gnu_xbm_height);
9024 if (rc == -1)
9025 return true;
9027 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
9028 x_create_bitmap_mask (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
9032 /* The first time we create the GNU bitmap and mask,
9033 this increments the ref-count one extra time.
9034 As a result, the GNU bitmap and mask are never freed.
9035 That way, we don't have to worry about allocating it again. */
9036 x_reference_bitmap (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
9038 bitmap_id = FRAME_DISPLAY_INFO (f)->icon_bitmap_id;
9041 x_wm_set_icon_pixmap (f, bitmap_id);
9042 f->output_data.x->icon_bitmap = bitmap_id;
9044 return false;
9048 /* Make the x-window of frame F use a rectangle with text.
9049 Use ICON_NAME as the text. */
9051 bool
9052 x_text_icon (struct frame *f, const char *icon_name)
9054 if (FRAME_X_WINDOW (f) == 0)
9055 return true;
9058 XTextProperty text;
9059 text.value = (unsigned char *) icon_name;
9060 text.encoding = XA_STRING;
9061 text.format = 8;
9062 text.nitems = strlen (icon_name);
9063 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
9066 if (f->output_data.x->icon_bitmap > 0)
9067 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
9068 f->output_data.x->icon_bitmap = 0;
9069 x_wm_set_icon_pixmap (f, 0);
9071 return false;
9074 #define X_ERROR_MESSAGE_SIZE 200
9076 /* If non-nil, this should be a string.
9077 It means catch X errors and store the error message in this string.
9079 The reason we use a stack is that x_catch_error/x_uncatch_error can
9080 be called from a signal handler.
9083 struct x_error_message_stack {
9084 char string[X_ERROR_MESSAGE_SIZE];
9085 Display *dpy;
9086 struct x_error_message_stack *prev;
9088 static struct x_error_message_stack *x_error_message;
9090 /* An X error handler which stores the error message in
9091 *x_error_message. This is called from x_error_handler if
9092 x_catch_errors is in effect. */
9094 static void
9095 x_error_catcher (Display *display, XErrorEvent *event)
9097 XGetErrorText (display, event->error_code,
9098 x_error_message->string,
9099 X_ERROR_MESSAGE_SIZE);
9102 /* Begin trapping X errors for display DPY. Actually we trap X errors
9103 for all displays, but DPY should be the display you are actually
9104 operating on.
9106 After calling this function, X protocol errors no longer cause
9107 Emacs to exit; instead, they are recorded in the string
9108 stored in *x_error_message.
9110 Calling x_check_errors signals an Emacs error if an X error has
9111 occurred since the last call to x_catch_errors or x_check_errors.
9113 Calling x_uncatch_errors resumes the normal error handling. */
9115 void
9116 x_catch_errors (Display *dpy)
9118 struct x_error_message_stack *data = xmalloc (sizeof *data);
9120 /* Make sure any errors from previous requests have been dealt with. */
9121 XSync (dpy, False);
9123 data->dpy = dpy;
9124 data->string[0] = 0;
9125 data->prev = x_error_message;
9126 x_error_message = data;
9129 /* Undo the last x_catch_errors call.
9130 DPY should be the display that was passed to x_catch_errors. */
9132 void
9133 x_uncatch_errors (void)
9135 struct x_error_message_stack *tmp;
9137 block_input ();
9139 /* The display may have been closed before this function is called.
9140 Check if it is still open before calling XSync. */
9141 if (x_display_info_for_display (x_error_message->dpy) != 0)
9142 XSync (x_error_message->dpy, False);
9144 tmp = x_error_message;
9145 x_error_message = x_error_message->prev;
9146 xfree (tmp);
9147 unblock_input ();
9150 /* If any X protocol errors have arrived since the last call to
9151 x_catch_errors or x_check_errors, signal an Emacs error using
9152 sprintf (a buffer, FORMAT, the x error message text) as the text. */
9154 void
9155 x_check_errors (Display *dpy, const char *format)
9157 /* Make sure to catch any errors incurred so far. */
9158 XSync (dpy, False);
9160 if (x_error_message->string[0])
9162 char string[X_ERROR_MESSAGE_SIZE];
9163 memcpy (string, x_error_message->string, X_ERROR_MESSAGE_SIZE);
9164 x_uncatch_errors ();
9165 error (format, string);
9169 /* Nonzero if we had any X protocol errors
9170 since we did x_catch_errors on DPY. */
9172 bool
9173 x_had_errors_p (Display *dpy)
9175 /* Make sure to catch any errors incurred so far. */
9176 XSync (dpy, False);
9178 return x_error_message->string[0] != 0;
9181 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
9183 void
9184 x_clear_errors (Display *dpy)
9186 x_error_message->string[0] = 0;
9189 #if false
9190 /* See comment in unwind_to_catch why calling this is a bad
9191 * idea. --lorentey */
9192 /* Close off all unclosed x_catch_errors calls. */
9194 void
9195 x_fully_uncatch_errors (void)
9197 while (x_error_message)
9198 x_uncatch_errors ();
9200 #endif
9202 #if false
9203 static unsigned int x_wire_count;
9204 x_trace_wire (void)
9206 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
9208 #endif
9211 /************************************************************************
9212 Handling X errors
9213 ************************************************************************/
9215 /* Error message passed to x_connection_closed. */
9217 static char *error_msg;
9219 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
9220 the text of an error message that lead to the connection loss. */
9222 static void
9223 x_connection_closed (Display *dpy, const char *error_message, bool ioerror)
9225 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
9226 Lisp_Object frame, tail;
9227 ptrdiff_t idx = SPECPDL_INDEX ();
9229 error_msg = alloca (strlen (error_message) + 1);
9230 strcpy (error_msg, error_message);
9232 /* Inhibit redisplay while frames are being deleted. */
9233 specbind (Qinhibit_redisplay, Qt);
9235 if (dpyinfo)
9237 /* Protect display from being closed when we delete the last
9238 frame on it. */
9239 dpyinfo->reference_count++;
9240 dpyinfo->terminal->reference_count++;
9242 if (ioerror) dpyinfo->display = 0;
9244 /* First delete frames whose mini-buffers are on frames
9245 that are on the dead display. */
9246 FOR_EACH_FRAME (tail, frame)
9248 Lisp_Object minibuf_frame;
9249 minibuf_frame
9250 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
9251 if (FRAME_X_P (XFRAME (frame))
9252 && FRAME_X_P (XFRAME (minibuf_frame))
9253 && ! EQ (frame, minibuf_frame)
9254 && FRAME_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
9255 delete_frame (frame, Qnoelisp);
9258 /* Now delete all remaining frames on the dead display.
9259 We are now sure none of these is used as the mini-buffer
9260 for another frame that we need to delete. */
9261 FOR_EACH_FRAME (tail, frame)
9262 if (FRAME_X_P (XFRAME (frame))
9263 && FRAME_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
9265 /* Set this to t so that delete_frame won't get confused
9266 trying to find a replacement. */
9267 kset_default_minibuffer_frame (FRAME_KBOARD (XFRAME (frame)), Qt);
9268 delete_frame (frame, Qnoelisp);
9271 /* If DPYINFO is null, this means we didn't open the display in the
9272 first place, so don't try to close it. */
9273 if (dpyinfo)
9275 /* We can not call XtCloseDisplay here because it calls XSync.
9276 XSync inside the error handler apparently hangs Emacs. On
9277 current Xt versions, this isn't needed either. */
9278 #ifdef USE_GTK
9279 /* A long-standing GTK bug prevents proper disconnect handling
9280 (https://bugzilla.gnome.org/show_bug.cgi?id=85715). Once,
9281 the resulting Glib error message loop filled a user's disk.
9282 To avoid this, kill Emacs unconditionally on disconnect. */
9283 shut_down_emacs (0, Qnil);
9284 fprintf (stderr, "%s\n\
9285 When compiled with GTK, Emacs cannot recover from X disconnects.\n\
9286 This is a GTK bug: https://bugzilla.gnome.org/show_bug.cgi?id=85715\n\
9287 For details, see etc/PROBLEMS.\n",
9288 error_msg);
9289 emacs_abort ();
9290 #endif /* USE_GTK */
9292 /* Indicate that this display is dead. */
9293 dpyinfo->display = 0;
9295 dpyinfo->reference_count--;
9296 dpyinfo->terminal->reference_count--;
9297 if (dpyinfo->reference_count != 0)
9298 /* We have just closed all frames on this display. */
9299 emacs_abort ();
9302 Lisp_Object tmp;
9303 XSETTERMINAL (tmp, dpyinfo->terminal);
9304 Fdelete_terminal (tmp, Qnoelisp);
9308 if (terminal_list == 0)
9310 fprintf (stderr, "%s\n", error_msg);
9311 Fkill_emacs (make_number (70));
9312 /* NOTREACHED */
9315 totally_unblock_input ();
9317 unbind_to (idx, Qnil);
9318 clear_waiting_for_input ();
9320 /* Tell GCC not to suggest attribute 'noreturn' for this function. */
9321 IF_LINT (if (! terminal_list) return; )
9323 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
9324 longjmp), because returning from this function would get us back into
9325 Xlib's code which will directly call `exit'. */
9326 error ("%s", error_msg);
9329 /* We specifically use it before defining it, so that gcc doesn't inline it,
9330 otherwise gdb doesn't know how to properly put a breakpoint on it. */
9331 static void x_error_quitter (Display *, XErrorEvent *);
9333 /* This is the first-level handler for X protocol errors.
9334 It calls x_error_quitter or x_error_catcher. */
9336 static int
9337 x_error_handler (Display *display, XErrorEvent *event)
9339 #if defined USE_GTK && defined HAVE_GTK3
9340 if ((event->error_code == BadMatch || event->error_code == BadWindow)
9341 && event->request_code == X_SetInputFocus)
9343 return 0;
9345 #endif
9347 if (x_error_message)
9348 x_error_catcher (display, event);
9349 else
9350 x_error_quitter (display, event);
9351 return 0;
9354 /* This is the usual handler for X protocol errors.
9355 It kills all frames on the display that we got the error for.
9356 If that was the only one, it prints an error message and kills Emacs. */
9358 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
9360 /* On older GCC versions, just putting x_error_quitter
9361 after x_error_handler prevents inlining into the former. */
9363 static void NO_INLINE
9364 x_error_quitter (Display *display, XErrorEvent *event)
9366 char buf[256], buf1[356];
9368 /* Ignore BadName errors. They can happen because of fonts
9369 or colors that are not defined. */
9371 if (event->error_code == BadName)
9372 return;
9374 /* Note that there is no real way portable across R3/R4 to get the
9375 original error handler. */
9377 XGetErrorText (display, event->error_code, buf, sizeof (buf));
9378 sprintf (buf1, "X protocol error: %s on protocol request %d",
9379 buf, event->request_code);
9380 x_connection_closed (display, buf1, false);
9384 /* This is the handler for X IO errors, always.
9385 It kills all frames on the display that we lost touch with.
9386 If that was the only one, it prints an error message and kills Emacs. */
9388 static int
9389 x_io_error_quitter (Display *display)
9391 char buf[256];
9393 snprintf (buf, sizeof buf, "Connection lost to X server '%s'",
9394 DisplayString (display));
9395 x_connection_closed (display, buf, true);
9396 return 0;
9399 /* Changing the font of the frame. */
9401 /* Give frame F the font FONT-OBJECT as its default font. The return
9402 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
9403 frame. If it is negative, generate a new fontset from
9404 FONT-OBJECT. */
9406 Lisp_Object
9407 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
9409 struct font *font = XFONT_OBJECT (font_object);
9410 int unit, font_ascent, font_descent;
9412 if (fontset < 0)
9413 fontset = fontset_from_font (font_object);
9414 FRAME_FONTSET (f) = fontset;
9415 if (FRAME_FONT (f) == font)
9416 /* This font is already set in frame F. There's nothing more to
9417 do. */
9418 return font_object;
9420 FRAME_FONT (f) = font;
9421 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
9422 FRAME_COLUMN_WIDTH (f) = font->average_width;
9423 get_font_ascent_descent (font, &font_ascent, &font_descent);
9424 FRAME_LINE_HEIGHT (f) = font_ascent + font_descent;
9426 #ifndef USE_X_TOOLKIT
9427 FRAME_MENU_BAR_HEIGHT (f) = FRAME_MENU_BAR_LINES (f) * FRAME_LINE_HEIGHT (f);
9428 #endif
9430 /* Compute character columns occupied by scrollbar.
9432 Don't do things differently for non-toolkit scrollbars
9433 (Bug#17163). */
9434 unit = FRAME_COLUMN_WIDTH (f);
9435 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
9436 FRAME_CONFIG_SCROLL_BAR_COLS (f)
9437 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + unit - 1) / unit;
9438 else
9439 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
9441 if (FRAME_X_WINDOW (f) != 0)
9443 /* Don't change the size of a tip frame; there's no point in
9444 doing it because it's done in Fx_show_tip, and it leads to
9445 problems because the tip frame has no widget. */
9446 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
9447 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
9448 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 3,
9449 false, Qfont);
9452 #ifdef HAVE_X_I18N
9453 if (FRAME_XIC (f)
9454 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
9456 block_input ();
9457 xic_set_xfontset (f, SSDATA (fontset_ascii (fontset)));
9458 unblock_input ();
9460 #endif
9462 return font_object;
9466 /***********************************************************************
9467 X Input Methods
9468 ***********************************************************************/
9470 #ifdef HAVE_X_I18N
9472 #ifdef HAVE_X11R6
9474 /* XIM destroy callback function, which is called whenever the
9475 connection to input method XIM dies. CLIENT_DATA contains a
9476 pointer to the x_display_info structure corresponding to XIM. */
9478 static void
9479 xim_destroy_callback (XIM xim, XPointer client_data, XPointer call_data)
9481 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
9482 Lisp_Object frame, tail;
9484 block_input ();
9486 /* No need to call XDestroyIC.. */
9487 FOR_EACH_FRAME (tail, frame)
9489 struct frame *f = XFRAME (frame);
9490 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
9492 FRAME_XIC (f) = NULL;
9493 xic_free_xfontset (f);
9497 /* No need to call XCloseIM. */
9498 dpyinfo->xim = NULL;
9499 XFree (dpyinfo->xim_styles);
9500 unblock_input ();
9503 #endif /* HAVE_X11R6 */
9505 /* Open the connection to the XIM server on display DPYINFO.
9506 RESOURCE_NAME is the resource name Emacs uses. */
9508 static void
9509 xim_open_dpy (struct x_display_info *dpyinfo, char *resource_name)
9511 XIM xim;
9513 #ifdef HAVE_XIM
9514 if (use_xim)
9516 if (dpyinfo->xim)
9517 XCloseIM (dpyinfo->xim);
9518 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
9519 emacs_class);
9520 dpyinfo->xim = xim;
9522 if (xim)
9524 #ifdef HAVE_X11R6
9525 XIMCallback destroy;
9526 #endif
9528 /* Get supported styles and XIM values. */
9529 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
9531 #ifdef HAVE_X11R6
9532 destroy.callback = xim_destroy_callback;
9533 destroy.client_data = (XPointer)dpyinfo;
9534 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
9535 #endif
9539 else
9540 #endif /* HAVE_XIM */
9541 dpyinfo->xim = NULL;
9545 #ifdef HAVE_X11R6_XIM
9547 /* XIM instantiate callback function, which is called whenever an XIM
9548 server is available. DISPLAY is the display of the XIM.
9549 CLIENT_DATA contains a pointer to an xim_inst_t structure created
9550 when the callback was registered. */
9552 static void
9553 xim_instantiate_callback (Display *display, XPointer client_data, XPointer call_data)
9555 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
9556 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
9558 /* We don't support multiple XIM connections. */
9559 if (dpyinfo->xim)
9560 return;
9562 xim_open_dpy (dpyinfo, xim_inst->resource_name);
9564 /* Create XIC for the existing frames on the same display, as long
9565 as they have no XIC. */
9566 if (dpyinfo->xim && dpyinfo->reference_count > 0)
9568 Lisp_Object tail, frame;
9570 block_input ();
9571 FOR_EACH_FRAME (tail, frame)
9573 struct frame *f = XFRAME (frame);
9575 if (FRAME_X_P (f)
9576 && FRAME_DISPLAY_INFO (f) == xim_inst->dpyinfo)
9577 if (FRAME_XIC (f) == NULL)
9579 create_frame_xic (f);
9580 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
9581 xic_set_statusarea (f);
9582 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
9584 struct window *w = XWINDOW (f->selected_window);
9585 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
9590 unblock_input ();
9594 #endif /* HAVE_X11R6_XIM */
9597 /* Open a connection to the XIM server on display DPYINFO.
9598 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
9599 connection only at the first time. On X11R6, open the connection
9600 in the XIM instantiate callback function. */
9602 static void
9603 xim_initialize (struct x_display_info *dpyinfo, char *resource_name)
9605 dpyinfo->xim = NULL;
9606 #ifdef HAVE_XIM
9607 if (use_xim)
9609 #ifdef HAVE_X11R6_XIM
9610 struct xim_inst_t *xim_inst = xmalloc (sizeof *xim_inst);
9611 Bool ret;
9613 dpyinfo->xim_callback_data = xim_inst;
9614 xim_inst->dpyinfo = dpyinfo;
9615 xim_inst->resource_name = xstrdup (resource_name);
9616 ret = XRegisterIMInstantiateCallback
9617 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
9618 emacs_class, xim_instantiate_callback,
9619 /* This is XPointer in XFree86 but (XPointer *)
9620 on Tru64, at least, hence the configure test. */
9621 (XRegisterIMInstantiateCallback_arg6) xim_inst);
9622 eassert (ret == True);
9623 #else /* not HAVE_X11R6_XIM */
9624 xim_open_dpy (dpyinfo, resource_name);
9625 #endif /* not HAVE_X11R6_XIM */
9627 #endif /* HAVE_XIM */
9631 /* Close the connection to the XIM server on display DPYINFO. */
9633 static void
9634 xim_close_dpy (struct x_display_info *dpyinfo)
9636 #ifdef HAVE_XIM
9637 if (use_xim)
9639 #ifdef HAVE_X11R6_XIM
9640 struct xim_inst_t *xim_inst = dpyinfo->xim_callback_data;
9642 if (dpyinfo->display)
9644 Bool ret = XUnregisterIMInstantiateCallback
9645 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
9646 emacs_class, xim_instantiate_callback,
9647 (XRegisterIMInstantiateCallback_arg6) xim_inst);
9648 eassert (ret == True);
9650 xfree (xim_inst->resource_name);
9651 xfree (xim_inst);
9652 #endif /* HAVE_X11R6_XIM */
9653 if (dpyinfo->display)
9654 XCloseIM (dpyinfo->xim);
9655 dpyinfo->xim = NULL;
9656 XFree (dpyinfo->xim_styles);
9658 #endif /* HAVE_XIM */
9661 #endif /* not HAVE_X11R6_XIM */
9665 /* Calculate the absolute position in frame F
9666 from its current recorded position values and gravity. */
9668 static void
9669 x_calc_absolute_position (struct frame *f)
9671 int flags = f->size_hint_flags;
9673 /* We have nothing to do if the current position
9674 is already for the top-left corner. */
9675 if (! ((flags & XNegative) || (flags & YNegative)))
9676 return;
9678 /* Treat negative positions as relative to the leftmost bottommost
9679 position that fits on the screen. */
9680 if (flags & XNegative)
9681 f->left_pos = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
9682 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
9685 int height = FRAME_PIXEL_HEIGHT (f);
9687 #if defined USE_X_TOOLKIT && defined USE_MOTIF
9688 /* Something is fishy here. When using Motif, starting Emacs with
9689 `-g -0-0', the frame appears too low by a few pixels.
9691 This seems to be so because initially, while Emacs is starting,
9692 the column widget's height and the frame's pixel height are
9693 different. The column widget's height is the right one. In
9694 later invocations, when Emacs is up, the frame's pixel height
9695 is right, though.
9697 It's not obvious where the initial small difference comes from.
9698 2000-12-01, gerd. */
9700 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
9701 #endif
9703 if (flags & YNegative)
9704 f->top_pos = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
9705 - height + f->top_pos;
9708 /* The left_pos and top_pos
9709 are now relative to the top and left screen edges,
9710 so the flags should correspond. */
9711 f->size_hint_flags &= ~ (XNegative | YNegative);
9714 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
9715 to really change the position, and 0 when calling from
9716 x_make_frame_visible (in that case, XOFF and YOFF are the current
9717 position values). It is -1 when calling from x_set_frame_parameters,
9718 which means, do adjust for borders but don't change the gravity. */
9720 void
9721 x_set_offset (struct frame *f, register int xoff, register int yoff, int change_gravity)
9723 int modified_top, modified_left;
9725 if (change_gravity > 0)
9727 f->top_pos = yoff;
9728 f->left_pos = xoff;
9729 f->size_hint_flags &= ~ (XNegative | YNegative);
9730 if (xoff < 0)
9731 f->size_hint_flags |= XNegative;
9732 if (yoff < 0)
9733 f->size_hint_flags |= YNegative;
9734 f->win_gravity = NorthWestGravity;
9736 x_calc_absolute_position (f);
9738 block_input ();
9739 x_wm_set_size_hint (f, 0, false);
9741 modified_left = f->left_pos;
9742 modified_top = f->top_pos;
9744 if (change_gravity != 0 && FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
9746 /* Some WMs (twm, wmaker at least) has an offset that is smaller
9747 than the WM decorations. So we use the calculated offset instead
9748 of the WM decoration sizes here (x/y_pixels_outer_diff). */
9749 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
9750 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
9753 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9754 modified_left, modified_top);
9756 x_sync_with_move (f, f->left_pos, f->top_pos,
9757 FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN);
9759 /* change_gravity is non-zero when this function is called from Lisp to
9760 programmatically move a frame. In that case, we call
9761 x_check_expected_move to discover if we have a "Type A" or "Type B"
9762 window manager, and, for a "Type A" window manager, adjust the position
9763 of the frame.
9765 We call x_check_expected_move if a programmatic move occurred, and
9766 either the window manager type (A/B) is unknown or it is Type A but we
9767 need to compute the top/left offset adjustment for this frame. */
9769 if (change_gravity != 0
9770 && (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
9771 || (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
9772 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
9773 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
9774 x_check_expected_move (f, modified_left, modified_top);
9776 unblock_input ();
9779 /* Return true if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
9780 on the root window for frame F contains ATOMNAME.
9781 This is how a WM check shall be done according to the Window Manager
9782 Specification/Extended Window Manager Hints at
9783 http://freedesktop.org/wiki/Specifications/wm-spec. */
9785 bool
9786 x_wm_supports (struct frame *f, Atom want_atom)
9788 Atom actual_type;
9789 unsigned long actual_size, bytes_remaining;
9790 int i, rc, actual_format;
9791 bool ret;
9792 Window wmcheck_window;
9793 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9794 Window target_window = dpyinfo->root_window;
9795 int max_len = 65536;
9796 Display *dpy = FRAME_X_DISPLAY (f);
9797 unsigned char *tmp_data = NULL;
9798 Atom target_type = XA_WINDOW;
9800 block_input ();
9802 x_catch_errors (dpy);
9803 rc = XGetWindowProperty (dpy, target_window,
9804 dpyinfo->Xatom_net_supporting_wm_check,
9805 0, max_len, False, target_type,
9806 &actual_type, &actual_format, &actual_size,
9807 &bytes_remaining, &tmp_data);
9809 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
9811 if (tmp_data) XFree (tmp_data);
9812 x_uncatch_errors ();
9813 unblock_input ();
9814 return false;
9817 wmcheck_window = *(Window *) tmp_data;
9818 XFree (tmp_data);
9820 /* Check if window exists. */
9821 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
9822 x_sync (f);
9823 if (x_had_errors_p (dpy))
9825 x_uncatch_errors ();
9826 unblock_input ();
9827 return false;
9830 if (dpyinfo->net_supported_window != wmcheck_window)
9832 /* Window changed, reload atoms */
9833 if (dpyinfo->net_supported_atoms != NULL)
9834 XFree (dpyinfo->net_supported_atoms);
9835 dpyinfo->net_supported_atoms = NULL;
9836 dpyinfo->nr_net_supported_atoms = 0;
9837 dpyinfo->net_supported_window = 0;
9839 target_type = XA_ATOM;
9840 tmp_data = NULL;
9841 rc = XGetWindowProperty (dpy, target_window,
9842 dpyinfo->Xatom_net_supported,
9843 0, max_len, False, target_type,
9844 &actual_type, &actual_format, &actual_size,
9845 &bytes_remaining, &tmp_data);
9847 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
9849 if (tmp_data) XFree (tmp_data);
9850 x_uncatch_errors ();
9851 unblock_input ();
9852 return false;
9855 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
9856 dpyinfo->nr_net_supported_atoms = actual_size;
9857 dpyinfo->net_supported_window = wmcheck_window;
9860 ret = false;
9862 for (i = 0; !ret && i < dpyinfo->nr_net_supported_atoms; ++i)
9863 ret = dpyinfo->net_supported_atoms[i] == want_atom;
9865 x_uncatch_errors ();
9866 unblock_input ();
9868 return ret;
9871 static void
9872 set_wm_state (Lisp_Object frame, bool add, Atom atom, Atom value)
9874 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (XFRAME (frame));
9876 x_send_client_event (frame, make_number (0), frame,
9877 dpyinfo->Xatom_net_wm_state,
9878 make_number (32),
9879 /* 1 = add, 0 = remove */
9880 Fcons
9881 (make_number (add),
9882 Fcons
9883 (make_fixnum_or_float (atom),
9884 (value != 0
9885 ? list1 (make_fixnum_or_float (value))
9886 : Qnil))));
9889 void
9890 x_set_sticky (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
9892 Lisp_Object frame;
9893 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9895 XSETFRAME (frame, f);
9897 set_wm_state (frame, !NILP (new_value),
9898 dpyinfo->Xatom_net_wm_state_sticky, None);
9901 /* Return the current _NET_WM_STATE.
9902 SIZE_STATE is set to one of the FULLSCREEN_* values.
9903 Set *STICKY to the sticky state.
9905 Return true iff we are not hidden. */
9907 static bool
9908 get_current_wm_state (struct frame *f,
9909 Window window,
9910 int *size_state,
9911 bool *sticky)
9913 Atom actual_type;
9914 unsigned long actual_size, bytes_remaining;
9915 int i, rc, actual_format;
9916 bool is_hidden = false;
9917 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9918 long max_len = 65536;
9919 Display *dpy = FRAME_X_DISPLAY (f);
9920 unsigned char *tmp_data = NULL;
9921 Atom target_type = XA_ATOM;
9923 *sticky = false;
9924 *size_state = FULLSCREEN_NONE;
9926 block_input ();
9927 x_catch_errors (dpy);
9928 rc = XGetWindowProperty (dpy, window, dpyinfo->Xatom_net_wm_state,
9929 0, max_len, False, target_type,
9930 &actual_type, &actual_format, &actual_size,
9931 &bytes_remaining, &tmp_data);
9933 if (rc != Success || actual_type != target_type || x_had_errors_p (dpy))
9935 if (tmp_data) XFree (tmp_data);
9936 x_uncatch_errors ();
9937 unblock_input ();
9938 return !FRAME_ICONIFIED_P (f);
9941 x_uncatch_errors ();
9943 for (i = 0; i < actual_size; ++i)
9945 Atom a = ((Atom*)tmp_data)[i];
9946 if (a == dpyinfo->Xatom_net_wm_state_hidden)
9947 is_hidden = true;
9948 else if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
9950 if (*size_state == FULLSCREEN_HEIGHT)
9951 *size_state = FULLSCREEN_MAXIMIZED;
9952 else
9953 *size_state = FULLSCREEN_WIDTH;
9955 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
9957 if (*size_state == FULLSCREEN_WIDTH)
9958 *size_state = FULLSCREEN_MAXIMIZED;
9959 else
9960 *size_state = FULLSCREEN_HEIGHT;
9962 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen)
9963 *size_state = FULLSCREEN_BOTH;
9964 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
9965 *sticky = true;
9968 if (tmp_data) XFree (tmp_data);
9969 unblock_input ();
9970 return ! is_hidden;
9973 /* Do fullscreen as specified in extended window manager hints */
9975 static bool
9976 do_ewmh_fullscreen (struct frame *f)
9978 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9979 bool have_net_atom = x_wm_supports (f, dpyinfo->Xatom_net_wm_state);
9980 int cur;
9981 bool dummy;
9983 get_current_wm_state (f, FRAME_OUTER_WINDOW (f), &cur, &dummy);
9985 /* Some window managers don't say they support _NET_WM_STATE, but they do say
9986 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
9987 if (!have_net_atom)
9988 have_net_atom = x_wm_supports (f, dpyinfo->Xatom_net_wm_state_fullscreen);
9990 if (have_net_atom && cur != f->want_fullscreen)
9992 Lisp_Object frame;
9994 XSETFRAME (frame, f);
9996 /* Keep number of calls to set_wm_state as low as possible.
9997 Some window managers, or possible Gtk+, hangs when too many
9998 are sent at once. */
9999 switch (f->want_fullscreen)
10001 case FULLSCREEN_BOTH:
10002 if (cur != FULLSCREEN_BOTH)
10003 set_wm_state (frame, true, dpyinfo->Xatom_net_wm_state_fullscreen,
10004 None);
10005 break;
10006 case FULLSCREEN_WIDTH:
10007 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_MAXIMIZED)
10009 set_wm_state (frame, false,
10010 dpyinfo->Xatom_net_wm_state_maximized_horz,
10011 dpyinfo->Xatom_net_wm_state_maximized_vert);
10012 set_wm_state (frame, true,
10013 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10015 else
10017 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_HEIGHT
10018 || cur == FULLSCREEN_MAXIMIZED)
10019 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10020 dpyinfo->Xatom_net_wm_state_maximized_vert);
10021 if (cur != FULLSCREEN_MAXIMIZED || x_frame_normalize_before_maximize)
10022 set_wm_state (frame, true,
10023 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10025 break;
10026 case FULLSCREEN_HEIGHT:
10027 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_MAXIMIZED)
10029 set_wm_state (frame, false,
10030 dpyinfo->Xatom_net_wm_state_maximized_horz,
10031 dpyinfo->Xatom_net_wm_state_maximized_vert);
10032 set_wm_state (frame, true,
10033 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10035 else
10037 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_WIDTH
10038 || cur == FULLSCREEN_MAXIMIZED)
10039 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10040 dpyinfo->Xatom_net_wm_state_maximized_horz);
10041 if (cur != FULLSCREEN_MAXIMIZED || x_frame_normalize_before_maximize)
10042 set_wm_state (frame, true,
10043 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10045 break;
10046 case FULLSCREEN_MAXIMIZED:
10047 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_BOTH)
10049 set_wm_state (frame, false,
10050 dpyinfo->Xatom_net_wm_state_fullscreen, None);
10051 set_wm_state (frame, true,
10052 dpyinfo->Xatom_net_wm_state_maximized_horz,
10053 dpyinfo->Xatom_net_wm_state_maximized_vert);
10055 else if (x_frame_normalize_before_maximize && cur == FULLSCREEN_WIDTH)
10057 set_wm_state (frame, false,
10058 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10059 set_wm_state (frame, true,
10060 dpyinfo->Xatom_net_wm_state_maximized_horz,
10061 dpyinfo->Xatom_net_wm_state_maximized_vert);
10063 else if (x_frame_normalize_before_maximize && cur == FULLSCREEN_HEIGHT)
10065 set_wm_state (frame, false,
10066 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10067 set_wm_state (frame, true,
10068 dpyinfo->Xatom_net_wm_state_maximized_horz,
10069 dpyinfo->Xatom_net_wm_state_maximized_vert);
10071 else
10073 if (cur == FULLSCREEN_BOTH)
10074 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10075 None);
10076 else if (cur == FULLSCREEN_HEIGHT)
10077 set_wm_state (frame, true,
10078 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10079 else if (cur == FULLSCREEN_WIDTH)
10080 set_wm_state (frame, true, None,
10081 dpyinfo->Xatom_net_wm_state_maximized_vert);
10082 else
10083 set_wm_state (frame, true,
10084 dpyinfo->Xatom_net_wm_state_maximized_horz,
10085 dpyinfo->Xatom_net_wm_state_maximized_vert);
10087 break;
10088 case FULLSCREEN_NONE:
10089 if (cur == FULLSCREEN_BOTH)
10090 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10091 None);
10092 else
10093 set_wm_state (frame, false,
10094 dpyinfo->Xatom_net_wm_state_maximized_horz,
10095 dpyinfo->Xatom_net_wm_state_maximized_vert);
10098 f->want_fullscreen = FULLSCREEN_NONE;
10102 return have_net_atom;
10105 static void
10106 XTfullscreen_hook (struct frame *f)
10108 if (FRAME_VISIBLE_P (f))
10110 block_input ();
10111 x_check_fullscreen (f);
10112 x_sync (f);
10113 unblock_input ();
10118 static bool
10119 x_handle_net_wm_state (struct frame *f, const XPropertyEvent *event)
10121 int value = FULLSCREEN_NONE;
10122 Lisp_Object lval;
10123 bool sticky = false;
10124 bool not_hidden = get_current_wm_state (f, event->window, &value, &sticky);
10126 lval = Qnil;
10127 switch (value)
10129 case FULLSCREEN_WIDTH:
10130 lval = Qfullwidth;
10131 break;
10132 case FULLSCREEN_HEIGHT:
10133 lval = Qfullheight;
10134 break;
10135 case FULLSCREEN_BOTH:
10136 lval = Qfullboth;
10137 break;
10138 case FULLSCREEN_MAXIMIZED:
10139 lval = Qmaximized;
10140 break;
10143 frame_size_history_add
10144 (f, Qx_handle_net_wm_state, 0, 0,
10145 list2 (get_frame_param (f, Qfullscreen), lval));
10147 store_frame_param (f, Qfullscreen, lval);
10148 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
10150 return not_hidden;
10153 /* Check if we need to resize the frame due to a fullscreen request.
10154 If so needed, resize the frame. */
10155 static void
10156 x_check_fullscreen (struct frame *f)
10158 Lisp_Object lval = Qnil;
10160 if (do_ewmh_fullscreen (f))
10161 return;
10163 if (f->output_data.x->parent_desc != FRAME_DISPLAY_INFO (f)->root_window)
10164 return; /* Only fullscreen without WM or with EWM hints (above). */
10166 /* Setting fullscreen to nil doesn't do anything. We could save the
10167 last non-fullscreen size and restore it, but it seems like a
10168 lot of work for this unusual case (no window manager running). */
10170 if (f->want_fullscreen != FULLSCREEN_NONE)
10172 int width = FRAME_PIXEL_WIDTH (f), height = FRAME_PIXEL_HEIGHT (f);
10173 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10175 switch (f->want_fullscreen)
10177 /* No difference between these two when there is no WM */
10178 case FULLSCREEN_MAXIMIZED:
10179 lval = Qmaximized;
10180 width = x_display_pixel_width (dpyinfo);
10181 height = x_display_pixel_height (dpyinfo);
10182 break;
10183 case FULLSCREEN_BOTH:
10184 lval = Qfullboth;
10185 width = x_display_pixel_width (dpyinfo);
10186 height = x_display_pixel_height (dpyinfo);
10187 break;
10188 case FULLSCREEN_WIDTH:
10189 lval = Qfullwidth;
10190 width = x_display_pixel_width (dpyinfo);
10191 height = height + FRAME_MENUBAR_HEIGHT (f);
10192 break;
10193 case FULLSCREEN_HEIGHT:
10194 lval = Qfullheight;
10195 height = x_display_pixel_height (dpyinfo);
10196 break;
10197 default:
10198 emacs_abort ();
10201 frame_size_history_add
10202 (f, Qx_check_fullscreen, width, height, Qnil);
10204 x_wm_set_size_hint (f, 0, false);
10206 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10207 width, height);
10209 if (FRAME_VISIBLE_P (f))
10210 x_wait_for_event (f, ConfigureNotify);
10211 else
10213 change_frame_size (f, width, height - FRAME_MENUBAR_HEIGHT (f),
10214 false, true, false, true);
10215 x_sync (f);
10219 /* `x_net_wm_state' might have reset the fullscreen frame parameter,
10220 restore it. */
10221 store_frame_param (f, Qfullscreen, lval);
10224 /* This function is called by x_set_offset to determine whether the window
10225 manager interfered with the positioning of the frame. Type A window
10226 managers position the surrounding window manager decorations a small
10227 amount above and left of the user-supplied position. Type B window
10228 managers position the surrounding window manager decorations at the
10229 user-specified position. If we detect a Type A window manager, we
10230 compensate by moving the window right and down by the proper amount. */
10232 static void
10233 x_check_expected_move (struct frame *f, int expected_left, int expected_top)
10235 int current_left = 0, current_top = 0;
10237 /* x_real_positions returns the left and top offsets of the outermost
10238 window manager window around the frame. */
10240 x_real_positions (f, &current_left, &current_top);
10242 if (current_left != expected_left || current_top != expected_top)
10244 /* It's a "Type A" window manager. */
10246 int adjusted_left;
10247 int adjusted_top;
10249 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
10250 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
10251 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
10253 /* Now fix the mispositioned frame's location. */
10255 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
10256 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
10258 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10259 adjusted_left, adjusted_top);
10261 x_sync_with_move (f, expected_left, expected_top, false);
10263 else
10264 /* It's a "Type B" window manager. We don't have to adjust the
10265 frame's position. */
10267 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
10271 /* Wait for XGetGeometry to return up-to-date position information for a
10272 recently-moved frame. Call this immediately after calling XMoveWindow.
10273 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
10274 frame has been moved to, so we use a fuzzy position comparison instead
10275 of an exact comparison. */
10277 static void
10278 x_sync_with_move (struct frame *f, int left, int top, bool fuzzy)
10280 int count = 0;
10282 while (count++ < 50)
10284 int current_left = 0, current_top = 0;
10286 /* In theory, this call to XSync only needs to happen once, but in
10287 practice, it doesn't seem to work, hence the need for the surrounding
10288 loop. */
10290 XSync (FRAME_X_DISPLAY (f), False);
10291 x_real_positions (f, &current_left, &current_top);
10293 if (fuzzy)
10295 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
10296 pixels. */
10298 if (eabs (current_left - left) <= 10
10299 && eabs (current_top - top) <= 40)
10300 return;
10302 else if (current_left == left && current_top == top)
10303 return;
10306 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
10307 will then return up-to-date position info. */
10309 wait_reading_process_output (0, 500000000, 0, false, Qnil, NULL, 0);
10313 /* Wait for an event on frame F matching EVENTTYPE. */
10314 void
10315 x_wait_for_event (struct frame *f, int eventtype)
10317 int level = interrupt_input_blocked;
10319 fd_set fds;
10320 struct timespec tmo, tmo_at, time_now;
10321 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
10323 f->wait_event_type = eventtype;
10325 /* Set timeout to 0.1 second. Hopefully not noticeable.
10326 Maybe it should be configurable. */
10327 tmo = make_timespec (0, 100 * 1000 * 1000);
10328 tmo_at = timespec_add (current_timespec (), tmo);
10330 while (f->wait_event_type)
10332 pending_signals = true;
10333 totally_unblock_input ();
10334 /* XTread_socket is called after unblock. */
10335 block_input ();
10336 interrupt_input_blocked = level;
10338 FD_ZERO (&fds);
10339 FD_SET (fd, &fds);
10341 time_now = current_timespec ();
10342 if (timespec_cmp (tmo_at, time_now) < 0)
10343 break;
10345 tmo = timespec_sub (tmo_at, time_now);
10346 if (pselect (fd + 1, &fds, NULL, NULL, &tmo, NULL) == 0)
10347 break; /* Timeout */
10350 f->wait_event_type = 0;
10354 /* Change the size of frame F's X window to WIDTH/HEIGHT in the case F
10355 doesn't have a widget. If CHANGE_GRAVITY, change to
10356 top-left-corner window gravity for this size change and subsequent
10357 size changes. Otherwise leave the window gravity unchanged. */
10359 static void
10360 x_set_window_size_1 (struct frame *f, bool change_gravity,
10361 int width, int height)
10363 int pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
10364 int pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
10365 int old_width = FRAME_PIXEL_WIDTH (f);
10366 int old_height = FRAME_PIXEL_HEIGHT (f);
10367 Lisp_Object fullscreen = get_frame_param (f, Qfullscreen);
10369 if (change_gravity) f->win_gravity = NorthWestGravity;
10370 x_wm_set_size_hint (f, 0, false);
10372 /* When the frame is fullheight and we only want to change the width
10373 or it is fullwidth and we only want to change the height we should
10374 be able to preserve the fullscreen property. However, due to the
10375 fact that we have to send a resize request anyway, the window
10376 manager will abolish it. At least the respective size should
10377 remain unchanged but giving the frame back its normal size will
10378 be broken ... */
10379 if (EQ (fullscreen, Qfullwidth) && width == FRAME_TEXT_WIDTH (f))
10381 frame_size_history_add
10382 (f, Qxg_frame_set_char_size_1, width, height,
10383 list2 (make_number (old_height),
10384 make_number (pixelheight + FRAME_MENUBAR_HEIGHT (f))));
10386 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10387 old_width, pixelheight + FRAME_MENUBAR_HEIGHT (f));
10389 else if (EQ (fullscreen, Qfullheight) && height == FRAME_TEXT_HEIGHT (f))
10391 frame_size_history_add
10392 (f, Qxg_frame_set_char_size_2, width, height,
10393 list2 (make_number (old_width), make_number (pixelwidth)));
10395 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10396 pixelwidth, old_height);
10399 else
10401 frame_size_history_add
10402 (f, Qxg_frame_set_char_size_3, width, height,
10403 list2 (make_number (pixelwidth + FRAME_TOOLBAR_WIDTH (f)),
10404 make_number (pixelheight + FRAME_TOOLBAR_HEIGHT (f)
10405 + FRAME_MENUBAR_HEIGHT (f))));
10407 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10408 pixelwidth, pixelheight + FRAME_MENUBAR_HEIGHT (f));
10409 fullscreen = Qnil;
10414 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
10415 receive in the ConfigureNotify event; if we get what we asked
10416 for, then the event won't cause the screen to become garbaged, so
10417 we have to make sure to do it here. */
10418 SET_FRAME_GARBAGED (f);
10420 /* Now, strictly speaking, we can't be sure that this is accurate,
10421 but the window manager will get around to dealing with the size
10422 change request eventually, and we'll hear how it went when the
10423 ConfigureNotify event gets here.
10425 We could just not bother storing any of this information here,
10426 and let the ConfigureNotify event set everything up, but that
10427 might be kind of confusing to the Lisp code, since size changes
10428 wouldn't be reported in the frame parameters until some random
10429 point in the future when the ConfigureNotify event arrives.
10431 Pass true for DELAY since we can't run Lisp code inside of
10432 a BLOCK_INPUT. */
10434 /* But the ConfigureNotify may in fact never arrive, and then this is
10435 not right if the frame is visible. Instead wait (with timeout)
10436 for the ConfigureNotify. */
10437 if (FRAME_VISIBLE_P (f))
10439 x_wait_for_event (f, ConfigureNotify);
10441 if (!NILP (fullscreen))
10442 /* Try to restore fullscreen state. */
10444 store_frame_param (f, Qfullscreen, fullscreen);
10445 x_set_fullscreen (f, fullscreen, fullscreen);
10448 else
10450 change_frame_size (f, width, height, false, true, false, true);
10451 x_sync (f);
10456 /* Call this to change the size of frame F's x-window.
10457 If CHANGE_GRAVITY, change to top-left-corner window gravity
10458 for this size change and subsequent size changes.
10459 Otherwise we leave the window gravity unchanged. */
10461 void
10462 x_set_window_size (struct frame *f, bool change_gravity,
10463 int width, int height, bool pixelwise)
10465 block_input ();
10467 /* The following breaks our calculations. If it's really needed,
10468 think of something else. */
10469 #if false
10470 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
10472 int text_width, text_height;
10474 /* When the frame is maximized/fullscreen or running under for
10475 example Xmonad, x_set_window_size_1 will be a no-op.
10476 In that case, the right thing to do is extend rows/width to
10477 the current frame size. We do that first if x_set_window_size_1
10478 turns out to not be a no-op (there is no way to know).
10479 The size will be adjusted again if the frame gets a
10480 ConfigureNotify event as a result of x_set_window_size. */
10481 int pixelh = FRAME_PIXEL_HEIGHT (f);
10482 #ifdef USE_X_TOOLKIT
10483 /* The menu bar is not part of text lines. The tool bar
10484 is however. */
10485 pixelh -= FRAME_MENUBAR_HEIGHT (f);
10486 #endif
10487 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, FRAME_PIXEL_WIDTH (f));
10488 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelh);
10490 change_frame_size (f, text_width, text_height, false, true, false, true);
10492 #endif
10494 /* Pixelize width and height, if necessary. */
10495 if (! pixelwise)
10497 width = width * FRAME_COLUMN_WIDTH (f);
10498 height = height * FRAME_LINE_HEIGHT (f);
10501 #ifdef USE_GTK
10502 if (FRAME_GTK_WIDGET (f))
10503 xg_frame_set_char_size (f, width, height);
10504 else
10505 x_set_window_size_1 (f, change_gravity, width, height);
10506 #else /* not USE_GTK */
10507 x_set_window_size_1 (f, change_gravity, width, height);
10508 x_clear_under_internal_border (f);
10509 #endif /* not USE_GTK */
10511 /* If cursor was outside the new size, mark it as off. */
10512 mark_window_cursors_off (XWINDOW (f->root_window));
10514 /* Clear out any recollection of where the mouse highlighting was,
10515 since it might be in a place that's outside the new frame size.
10516 Actually checking whether it is outside is a pain in the neck,
10517 so don't try--just let the highlighting be done afresh with new size. */
10518 cancel_mouse_face (f);
10520 unblock_input ();
10523 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
10525 void
10526 frame_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
10528 block_input ();
10530 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
10531 0, 0, 0, 0, pix_x, pix_y);
10532 unblock_input ();
10535 /* Raise frame F. */
10537 void
10538 x_raise_frame (struct frame *f)
10540 block_input ();
10541 if (FRAME_VISIBLE_P (f))
10542 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
10543 XFlush (FRAME_X_DISPLAY (f));
10544 unblock_input ();
10547 /* Lower frame F. */
10549 static void
10550 x_lower_frame (struct frame *f)
10552 if (FRAME_VISIBLE_P (f))
10554 block_input ();
10555 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
10556 XFlush (FRAME_X_DISPLAY (f));
10557 unblock_input ();
10561 /* Request focus with XEmbed */
10563 void
10564 xembed_request_focus (struct frame *f)
10566 /* See XEmbed Protocol Specification at
10567 http://freedesktop.org/wiki/Specifications/xembed-spec */
10568 if (FRAME_VISIBLE_P (f))
10569 xembed_send_message (f, CurrentTime,
10570 XEMBED_REQUEST_FOCUS, 0, 0, 0);
10573 /* Activate frame with Extended Window Manager Hints */
10575 void
10576 x_ewmh_activate_frame (struct frame *f)
10578 /* See Window Manager Specification/Extended Window Manager Hints at
10579 http://freedesktop.org/wiki/Specifications/wm-spec */
10581 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10583 if (FRAME_VISIBLE_P (f) && x_wm_supports (f, dpyinfo->Xatom_net_active_window))
10585 Lisp_Object frame;
10586 XSETFRAME (frame, f);
10587 x_send_client_event (frame, make_number (0), frame,
10588 dpyinfo->Xatom_net_active_window,
10589 make_number (32),
10590 list2i (1, dpyinfo->last_user_time));
10594 static void
10595 XTframe_raise_lower (struct frame *f, bool raise_flag)
10597 if (raise_flag)
10598 x_raise_frame (f);
10599 else
10600 x_lower_frame (f);
10603 /* XEmbed implementation. */
10605 #if defined USE_X_TOOLKIT || ! defined USE_GTK
10607 /* XEmbed implementation. */
10609 #define XEMBED_VERSION 0
10611 static void
10612 xembed_set_info (struct frame *f, enum xembed_info flags)
10614 unsigned long data[2];
10615 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10617 data[0] = XEMBED_VERSION;
10618 data[1] = flags;
10620 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10621 dpyinfo->Xatom_XEMBED_INFO, dpyinfo->Xatom_XEMBED_INFO,
10622 32, PropModeReplace, (unsigned char *) data, 2);
10624 #endif /* defined USE_X_TOOLKIT || ! defined USE_GTK */
10626 static void
10627 xembed_send_message (struct frame *f, Time t, enum xembed_message msg,
10628 long int detail, long int data1, long int data2)
10630 XEvent event;
10632 event.xclient.type = ClientMessage;
10633 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
10634 event.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_XEMBED;
10635 event.xclient.format = 32;
10636 event.xclient.data.l[0] = t;
10637 event.xclient.data.l[1] = msg;
10638 event.xclient.data.l[2] = detail;
10639 event.xclient.data.l[3] = data1;
10640 event.xclient.data.l[4] = data2;
10642 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
10643 False, NoEventMask, &event);
10644 XSync (FRAME_X_DISPLAY (f), False);
10647 /* Change of visibility. */
10649 /* This tries to wait until the frame is really visible.
10650 However, if the window manager asks the user where to position
10651 the frame, this will return before the user finishes doing that.
10652 The frame will not actually be visible at that time,
10653 but it will become visible later when the window manager
10654 finishes with it. */
10656 void
10657 x_make_frame_visible (struct frame *f)
10659 int original_top, original_left;
10660 int tries = 0;
10662 block_input ();
10664 x_set_bitmap_icon (f);
10666 if (! FRAME_VISIBLE_P (f))
10668 /* We test FRAME_GARBAGED_P here to make sure we don't
10669 call x_set_offset a second time
10670 if we get to x_make_frame_visible a second time
10671 before the window gets really visible. */
10672 if (! FRAME_ICONIFIED_P (f)
10673 && ! FRAME_X_EMBEDDED_P (f)
10674 && ! f->output_data.x->asked_for_visible)
10675 x_set_offset (f, f->left_pos, f->top_pos, 0);
10677 f->output_data.x->asked_for_visible = true;
10679 if (! EQ (Vx_no_window_manager, Qt))
10680 x_wm_set_window_state (f, NormalState);
10681 #ifdef USE_X_TOOLKIT
10682 if (FRAME_X_EMBEDDED_P (f))
10683 xembed_set_info (f, XEMBED_MAPPED);
10684 else
10686 /* This was XtPopup, but that did nothing for an iconified frame. */
10687 XtMapWidget (f->output_data.x->widget);
10689 #else /* not USE_X_TOOLKIT */
10690 #ifdef USE_GTK
10691 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
10692 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
10693 #else
10694 if (FRAME_X_EMBEDDED_P (f))
10695 xembed_set_info (f, XEMBED_MAPPED);
10696 else
10697 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10698 #endif /* not USE_GTK */
10699 #endif /* not USE_X_TOOLKIT */
10702 XFlush (FRAME_X_DISPLAY (f));
10704 /* Synchronize to ensure Emacs knows the frame is visible
10705 before we do anything else. We do this loop with input not blocked
10706 so that incoming events are handled. */
10708 Lisp_Object frame;
10709 /* This must be before UNBLOCK_INPUT
10710 since events that arrive in response to the actions above
10711 will set it when they are handled. */
10712 bool previously_visible = f->output_data.x->has_been_visible;
10714 original_left = f->left_pos;
10715 original_top = f->top_pos;
10717 /* This must come after we set COUNT. */
10718 unblock_input ();
10720 /* We unblock here so that arriving X events are processed. */
10722 /* Now move the window back to where it was "supposed to be".
10723 But don't do it if the gravity is negative.
10724 When the gravity is negative, this uses a position
10725 that is 3 pixels too low. Perhaps that's really the border width.
10727 Don't do this if the window has never been visible before,
10728 because the window manager may choose the position
10729 and we don't want to override it. */
10731 if (! FRAME_VISIBLE_P (f)
10732 && ! FRAME_ICONIFIED_P (f)
10733 && ! FRAME_X_EMBEDDED_P (f)
10734 && f->win_gravity == NorthWestGravity
10735 && previously_visible)
10737 Drawable rootw;
10738 int x, y;
10739 unsigned int width, height, border, depth;
10741 block_input ();
10743 /* On some window managers (such as FVWM) moving an existing
10744 window, even to the same place, causes the window manager
10745 to introduce an offset. This can cause the window to move
10746 to an unexpected location. Check the geometry (a little
10747 slow here) and then verify that the window is in the right
10748 place. If the window is not in the right place, move it
10749 there, and take the potential window manager hit. */
10750 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10751 &rootw, &x, &y, &width, &height, &border, &depth);
10753 if (original_left != x || original_top != y)
10754 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10755 original_left, original_top);
10757 unblock_input ();
10760 XSETFRAME (frame, f);
10762 /* Process X events until a MapNotify event has been seen. */
10763 while (!FRAME_VISIBLE_P (f))
10765 /* Force processing of queued events. */
10766 x_sync (f);
10768 /* If on another desktop, the deiconify/map may be ignored and the
10769 frame never becomes visible. XMonad does this.
10770 Prevent an endless loop. */
10771 if (FRAME_ICONIFIED_P (f) && ++tries > 100)
10772 break;
10774 /* This hack is still in use at least for Cygwin. See
10775 http://lists.gnu.org/archive/html/emacs-devel/2013-12/msg00351.html.
10777 Machines that do polling rather than SIGIO have been
10778 observed to go into a busy-wait here. So we'll fake an
10779 alarm signal to let the handler know that there's something
10780 to be read. We used to raise a real alarm, but it seems
10781 that the handler isn't always enabled here. This is
10782 probably a bug. */
10783 if (input_polling_used ())
10785 /* It could be confusing if a real alarm arrives while
10786 processing the fake one. Turn it off and let the
10787 handler reset it. */
10788 int old_poll_suppress_count = poll_suppress_count;
10789 poll_suppress_count = 1;
10790 poll_for_input_1 ();
10791 poll_suppress_count = old_poll_suppress_count;
10794 if (XPending (FRAME_X_DISPLAY (f)))
10796 XEvent xev;
10797 XNextEvent (FRAME_X_DISPLAY (f), &xev);
10798 x_dispatch_event (&xev, FRAME_X_DISPLAY (f));
10804 /* Change from mapped state to withdrawn state. */
10806 /* Make the frame visible (mapped and not iconified). */
10808 void
10809 x_make_frame_invisible (struct frame *f)
10811 Window window;
10813 /* Use the frame's outermost window, not the one we normally draw on. */
10814 window = FRAME_OUTER_WINDOW (f);
10816 /* Don't keep the highlight on an invisible frame. */
10817 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
10818 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
10820 block_input ();
10822 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
10823 that the current position of the window is user-specified, rather than
10824 program-specified, so that when the window is mapped again, it will be
10825 placed at the same location, without forcing the user to position it
10826 by hand again (they have already done that once for this window.) */
10827 x_wm_set_size_hint (f, 0, true);
10829 #ifdef USE_GTK
10830 if (FRAME_GTK_OUTER_WIDGET (f))
10831 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
10832 else
10833 #else
10834 if (FRAME_X_EMBEDDED_P (f))
10835 xembed_set_info (f, 0);
10836 else
10837 #endif
10840 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
10841 DefaultScreen (FRAME_X_DISPLAY (f))))
10843 unblock_input ();
10844 error ("Can't notify window manager of window withdrawal");
10848 /* We can't distinguish this from iconification
10849 just by the event that we get from the server.
10850 So we can't win using the usual strategy of letting
10851 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
10852 and synchronize with the server to make sure we agree. */
10853 SET_FRAME_VISIBLE (f, 0);
10854 SET_FRAME_ICONIFIED (f, false);
10856 x_sync (f);
10858 unblock_input ();
10861 /* Change window state from mapped to iconified. */
10863 void
10864 x_iconify_frame (struct frame *f)
10866 #ifdef USE_X_TOOLKIT
10867 int result;
10868 #endif
10870 /* Don't keep the highlight on an invisible frame. */
10871 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
10872 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
10874 if (FRAME_ICONIFIED_P (f))
10875 return;
10877 block_input ();
10879 x_set_bitmap_icon (f);
10881 #if defined (USE_GTK)
10882 if (FRAME_GTK_OUTER_WIDGET (f))
10884 if (! FRAME_VISIBLE_P (f))
10885 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
10887 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
10888 SET_FRAME_VISIBLE (f, 0);
10889 SET_FRAME_ICONIFIED (f, true);
10890 unblock_input ();
10891 return;
10893 #endif
10895 #ifdef USE_X_TOOLKIT
10897 if (! FRAME_VISIBLE_P (f))
10899 if (! EQ (Vx_no_window_manager, Qt))
10900 x_wm_set_window_state (f, IconicState);
10901 /* This was XtPopup, but that did nothing for an iconified frame. */
10902 XtMapWidget (f->output_data.x->widget);
10903 /* The server won't give us any event to indicate
10904 that an invisible frame was changed to an icon,
10905 so we have to record it here. */
10906 SET_FRAME_VISIBLE (f, 0);
10907 SET_FRAME_ICONIFIED (f, true);
10908 unblock_input ();
10909 return;
10912 result = XIconifyWindow (FRAME_X_DISPLAY (f),
10913 XtWindow (f->output_data.x->widget),
10914 DefaultScreen (FRAME_X_DISPLAY (f)));
10915 unblock_input ();
10917 if (!result)
10918 error ("Can't notify window manager of iconification");
10920 SET_FRAME_ICONIFIED (f, true);
10921 SET_FRAME_VISIBLE (f, 0);
10923 block_input ();
10924 XFlush (FRAME_X_DISPLAY (f));
10925 unblock_input ();
10926 #else /* not USE_X_TOOLKIT */
10928 /* Make sure the X server knows where the window should be positioned,
10929 in case the user deiconifies with the window manager. */
10930 if (! FRAME_VISIBLE_P (f)
10931 && ! FRAME_ICONIFIED_P (f)
10932 && ! FRAME_X_EMBEDDED_P (f))
10933 x_set_offset (f, f->left_pos, f->top_pos, 0);
10935 /* Since we don't know which revision of X we're running, we'll use both
10936 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
10938 /* X11R4: send a ClientMessage to the window manager using the
10939 WM_CHANGE_STATE type. */
10941 XEvent msg;
10943 msg.xclient.window = FRAME_X_WINDOW (f);
10944 msg.xclient.type = ClientMessage;
10945 msg.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_wm_change_state;
10946 msg.xclient.format = 32;
10947 msg.xclient.data.l[0] = IconicState;
10949 if (! XSendEvent (FRAME_X_DISPLAY (f),
10950 DefaultRootWindow (FRAME_X_DISPLAY (f)),
10951 False,
10952 SubstructureRedirectMask | SubstructureNotifyMask,
10953 &msg))
10955 unblock_input ();
10956 error ("Can't notify window manager of iconification");
10960 /* X11R3: set the initial_state field of the window manager hints to
10961 IconicState. */
10962 x_wm_set_window_state (f, IconicState);
10964 if (!FRAME_VISIBLE_P (f))
10966 /* If the frame was withdrawn, before, we must map it. */
10967 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10970 SET_FRAME_ICONIFIED (f, true);
10971 SET_FRAME_VISIBLE (f, 0);
10973 XFlush (FRAME_X_DISPLAY (f));
10974 unblock_input ();
10975 #endif /* not USE_X_TOOLKIT */
10979 /* Free X resources of frame F. */
10981 void
10982 x_free_frame_resources (struct frame *f)
10984 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10985 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
10986 #ifdef USE_X_TOOLKIT
10987 Lisp_Object bar;
10988 struct scroll_bar *b;
10989 #endif
10991 block_input ();
10993 /* If a display connection is dead, don't try sending more
10994 commands to the X server. */
10995 if (dpyinfo->display)
10997 /* Always exit with visible pointer to avoid weird issue
10998 with Xfixes (Bug#17609). */
10999 if (f->pointer_invisible)
11000 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, 0);
11002 /* We must free faces before destroying windows because some
11003 font-driver (e.g. xft) access a window while finishing a
11004 face. */
11005 free_frame_faces (f);
11007 if (f->output_data.x->icon_desc)
11008 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
11010 #ifdef USE_X_TOOLKIT
11011 /* Explicitly destroy the scroll bars of the frame. Without
11012 this, we get "BadDrawable" errors from the toolkit later on,
11013 presumably from expose events generated for the disappearing
11014 toolkit scroll bars. */
11015 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
11017 b = XSCROLL_BAR (bar);
11018 x_scroll_bar_remove (b);
11020 #endif
11022 #ifdef HAVE_X_I18N
11023 if (FRAME_XIC (f))
11024 free_frame_xic (f);
11025 #endif
11027 x_free_cr_resources (f);
11028 #ifdef USE_X_TOOLKIT
11029 if (f->output_data.x->widget)
11031 XtDestroyWidget (f->output_data.x->widget);
11032 f->output_data.x->widget = NULL;
11034 /* Tooltips don't have widgets, only a simple X window, even if
11035 we are using a toolkit. */
11036 else if (FRAME_X_WINDOW (f))
11037 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11039 free_frame_menubar (f);
11041 if (f->shell_position)
11042 xfree (f->shell_position);
11043 #else /* !USE_X_TOOLKIT */
11045 #ifdef USE_GTK
11046 xg_free_frame_widgets (f);
11047 #endif /* USE_GTK */
11049 if (FRAME_X_WINDOW (f))
11050 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11051 #endif /* !USE_X_TOOLKIT */
11053 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
11054 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
11055 unload_color (f, f->output_data.x->cursor_pixel);
11056 unload_color (f, f->output_data.x->cursor_foreground_pixel);
11057 unload_color (f, f->output_data.x->border_pixel);
11058 unload_color (f, f->output_data.x->mouse_pixel);
11060 if (f->output_data.x->scroll_bar_background_pixel != -1)
11061 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
11062 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
11063 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
11064 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
11065 /* Scrollbar shadow colors. */
11066 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
11067 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
11068 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
11069 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
11070 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
11071 if (f->output_data.x->white_relief.pixel != -1)
11072 unload_color (f, f->output_data.x->white_relief.pixel);
11073 if (f->output_data.x->black_relief.pixel != -1)
11074 unload_color (f, f->output_data.x->black_relief.pixel);
11076 x_free_gcs (f);
11078 /* Free extra GCs allocated by x_setup_relief_colors. */
11079 if (f->output_data.x->white_relief.gc)
11081 XFreeGC (dpyinfo->display, f->output_data.x->white_relief.gc);
11082 f->output_data.x->white_relief.gc = 0;
11084 if (f->output_data.x->black_relief.gc)
11086 XFreeGC (dpyinfo->display, f->output_data.x->black_relief.gc);
11087 f->output_data.x->black_relief.gc = 0;
11090 /* Free cursors. */
11091 if (f->output_data.x->text_cursor != 0)
11092 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->text_cursor);
11093 if (f->output_data.x->nontext_cursor != 0)
11094 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->nontext_cursor);
11095 if (f->output_data.x->modeline_cursor != 0)
11096 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->modeline_cursor);
11097 if (f->output_data.x->hand_cursor != 0)
11098 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hand_cursor);
11099 if (f->output_data.x->hourglass_cursor != 0)
11100 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hourglass_cursor);
11101 if (f->output_data.x->horizontal_drag_cursor != 0)
11102 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->horizontal_drag_cursor);
11103 if (f->output_data.x->vertical_drag_cursor != 0)
11104 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->vertical_drag_cursor);
11106 XFlush (FRAME_X_DISPLAY (f));
11109 xfree (f->output_data.x->saved_menu_event);
11110 xfree (f->output_data.x);
11111 f->output_data.x = NULL;
11113 if (f == dpyinfo->x_focus_frame)
11114 dpyinfo->x_focus_frame = 0;
11115 if (f == dpyinfo->x_focus_event_frame)
11116 dpyinfo->x_focus_event_frame = 0;
11117 if (f == dpyinfo->x_highlight_frame)
11118 dpyinfo->x_highlight_frame = 0;
11119 if (f == hlinfo->mouse_face_mouse_frame)
11120 reset_mouse_highlight (hlinfo);
11122 unblock_input ();
11126 /* Destroy the X window of frame F. */
11128 static void
11129 x_destroy_window (struct frame *f)
11131 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11133 /* If a display connection is dead, don't try sending more
11134 commands to the X server. */
11135 if (dpyinfo->display != 0)
11136 x_free_frame_resources (f);
11138 dpyinfo->reference_count--;
11142 /* Setting window manager hints. */
11144 /* Set the normal size hints for the window manager, for frame F.
11145 FLAGS is the flags word to use--or 0 meaning preserve the flags
11146 that the window now has.
11147 If USER_POSITION, set the USPosition
11148 flag (this is useful when FLAGS is 0).
11149 The GTK version is in gtkutils.c. */
11151 #ifndef USE_GTK
11152 void
11153 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
11155 XSizeHints size_hints;
11156 Window window = FRAME_OUTER_WINDOW (f);
11158 if (!window)
11159 return;
11161 #ifdef USE_X_TOOLKIT
11162 if (f->output_data.x->widget)
11164 widget_update_wm_size_hints (f->output_data.x->widget);
11165 return;
11167 #endif
11169 /* Setting PMaxSize caused various problems. */
11170 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
11172 size_hints.x = f->left_pos;
11173 size_hints.y = f->top_pos;
11175 size_hints.height = FRAME_PIXEL_HEIGHT (f);
11176 size_hints.width = FRAME_PIXEL_WIDTH (f);
11178 size_hints.width_inc = frame_resize_pixelwise ? 1 : FRAME_COLUMN_WIDTH (f);
11179 size_hints.height_inc = frame_resize_pixelwise ? 1 : FRAME_LINE_HEIGHT (f);
11181 size_hints.max_width = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
11182 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
11183 size_hints.max_height = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
11184 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
11186 /* Calculate the base and minimum sizes. */
11188 int base_width, base_height;
11189 int min_rows = 0, min_cols = 0;
11191 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
11192 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
11194 if (frame_resize_pixelwise)
11195 /* Needed to prevent a bad protocol error crash when making the
11196 frame size very small. */
11198 min_cols = 2 * min_cols;
11199 min_rows = 2 * min_rows;
11202 /* The window manager uses the base width hints to calculate the
11203 current number of rows and columns in the frame while
11204 resizing; min_width and min_height aren't useful for this
11205 purpose, since they might not give the dimensions for a
11206 zero-row, zero-column frame.
11208 We use the base_width and base_height members if we have
11209 them; otherwise, we set the min_width and min_height members
11210 to the size for a zero x zero frame. */
11212 size_hints.flags |= PBaseSize;
11213 size_hints.base_width = base_width;
11214 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
11215 size_hints.min_width = base_width + min_cols * FRAME_COLUMN_WIDTH (f);
11216 size_hints.min_height = base_height + min_rows * FRAME_LINE_HEIGHT (f);
11219 /* If we don't need the old flags, we don't need the old hint at all. */
11220 if (flags)
11222 size_hints.flags |= flags;
11223 goto no_read;
11227 XSizeHints hints; /* Sometimes I hate X Windows... */
11228 long supplied_return;
11229 int value;
11231 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
11232 &supplied_return);
11234 if (flags)
11235 size_hints.flags |= flags;
11236 else
11238 if (value == 0)
11239 hints.flags = 0;
11240 if (hints.flags & PSize)
11241 size_hints.flags |= PSize;
11242 if (hints.flags & PPosition)
11243 size_hints.flags |= PPosition;
11244 if (hints.flags & USPosition)
11245 size_hints.flags |= USPosition;
11246 if (hints.flags & USSize)
11247 size_hints.flags |= USSize;
11251 no_read:
11253 #ifdef PWinGravity
11254 size_hints.win_gravity = f->win_gravity;
11255 size_hints.flags |= PWinGravity;
11257 if (user_position)
11259 size_hints.flags &= ~ PPosition;
11260 size_hints.flags |= USPosition;
11262 #endif /* PWinGravity */
11264 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
11266 #endif /* not USE_GTK */
11268 /* Used for IconicState or NormalState */
11270 static void
11271 x_wm_set_window_state (struct frame *f, int state)
11273 #ifdef USE_X_TOOLKIT
11274 Arg al[1];
11276 XtSetArg (al[0], XtNinitialState, state);
11277 XtSetValues (f->output_data.x->widget, al, 1);
11278 #else /* not USE_X_TOOLKIT */
11279 Window window = FRAME_X_WINDOW (f);
11281 f->output_data.x->wm_hints.flags |= StateHint;
11282 f->output_data.x->wm_hints.initial_state = state;
11284 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
11285 #endif /* not USE_X_TOOLKIT */
11288 static void
11289 x_wm_set_icon_pixmap (struct frame *f, ptrdiff_t pixmap_id)
11291 Pixmap icon_pixmap, icon_mask;
11293 #if !defined USE_X_TOOLKIT && !defined USE_GTK
11294 Window window = FRAME_OUTER_WINDOW (f);
11295 #endif
11297 if (pixmap_id > 0)
11299 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
11300 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
11301 icon_mask = x_bitmap_mask (f, pixmap_id);
11302 f->output_data.x->wm_hints.icon_mask = icon_mask;
11304 else
11306 /* It seems there is no way to turn off use of an icon
11307 pixmap. */
11308 return;
11312 #ifdef USE_GTK
11314 xg_set_frame_icon (f, icon_pixmap, icon_mask);
11315 return;
11318 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
11321 Arg al[1];
11322 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
11323 XtSetValues (f->output_data.x->widget, al, 1);
11324 XtSetArg (al[0], XtNiconMask, icon_mask);
11325 XtSetValues (f->output_data.x->widget, al, 1);
11328 #else /* not USE_X_TOOLKIT && not USE_GTK */
11330 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
11331 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
11333 #endif /* not USE_X_TOOLKIT && not USE_GTK */
11336 void
11337 x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
11339 Window window = FRAME_OUTER_WINDOW (f);
11341 f->output_data.x->wm_hints.flags |= IconPositionHint;
11342 f->output_data.x->wm_hints.icon_x = icon_x;
11343 f->output_data.x->wm_hints.icon_y = icon_y;
11345 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
11349 /***********************************************************************
11350 Fonts
11351 ***********************************************************************/
11353 #ifdef GLYPH_DEBUG
11355 /* Check that FONT is valid on frame F. It is if it can be found in F's
11356 font table. */
11358 static void
11359 x_check_font (struct frame *f, struct font *font)
11361 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
11362 if (font->driver->check)
11363 eassert (font->driver->check (f, font) == 0);
11366 #endif /* GLYPH_DEBUG */
11369 /***********************************************************************
11370 Initialization
11371 ***********************************************************************/
11373 #ifdef USE_X_TOOLKIT
11374 static XrmOptionDescRec emacs_options[] = {
11375 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
11376 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
11378 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
11379 XrmoptionSepArg, NULL},
11380 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
11382 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
11383 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
11384 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
11385 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
11386 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
11387 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
11388 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
11391 /* Whether atimer for Xt timeouts is activated or not. */
11393 static bool x_timeout_atimer_activated_flag;
11395 #endif /* USE_X_TOOLKIT */
11397 static int x_initialized;
11399 /* Test whether two display-name strings agree up to the dot that separates
11400 the screen number from the server number. */
11401 static bool
11402 same_x_server (const char *name1, const char *name2)
11404 bool seen_colon = false;
11405 Lisp_Object sysname = Fsystem_name ();
11406 const char *system_name = SSDATA (sysname);
11407 ptrdiff_t system_name_length = SBYTES (sysname);
11408 ptrdiff_t length_until_period = 0;
11410 while (system_name[length_until_period] != 0
11411 && system_name[length_until_period] != '.')
11412 length_until_period++;
11414 /* Treat `unix' like an empty host name. */
11415 if (! strncmp (name1, "unix:", 5))
11416 name1 += 4;
11417 if (! strncmp (name2, "unix:", 5))
11418 name2 += 4;
11419 /* Treat this host's name like an empty host name. */
11420 if (! strncmp (name1, system_name, system_name_length)
11421 && name1[system_name_length] == ':')
11422 name1 += system_name_length;
11423 if (! strncmp (name2, system_name, system_name_length)
11424 && name2[system_name_length] == ':')
11425 name2 += system_name_length;
11426 /* Treat this host's domainless name like an empty host name. */
11427 if (! strncmp (name1, system_name, length_until_period)
11428 && name1[length_until_period] == ':')
11429 name1 += length_until_period;
11430 if (! strncmp (name2, system_name, length_until_period)
11431 && name2[length_until_period] == ':')
11432 name2 += length_until_period;
11434 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
11436 if (*name1 == ':')
11437 seen_colon = true;
11438 if (seen_colon && *name1 == '.')
11439 return true;
11441 return (seen_colon
11442 && (*name1 == '.' || *name1 == '\0')
11443 && (*name2 == '.' || *name2 == '\0'));
11446 /* Count number of set bits in mask and number of bits to shift to
11447 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
11448 to 5. */
11449 static void
11450 get_bits_and_offset (unsigned long mask, int *bits, int *offset)
11452 int nr = 0;
11453 int off = 0;
11455 while (!(mask & 1))
11457 off++;
11458 mask >>= 1;
11461 while (mask & 1)
11463 nr++;
11464 mask >>= 1;
11467 *offset = off;
11468 *bits = nr;
11471 /* Return true iff display DISPLAY is available for use.
11472 But don't permanently open it, just test its availability. */
11474 bool
11475 x_display_ok (const char *display)
11477 /* XOpenDisplay fails if it gets a signal. Block SIGIO which may arrive. */
11478 unrequest_sigio ();
11479 Display *dpy = XOpenDisplay (display);
11480 request_sigio ();
11481 if (!dpy)
11482 return false;
11483 XCloseDisplay (dpy);
11484 return true;
11487 #ifdef USE_GTK
11488 static void
11489 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
11490 const gchar *msg, gpointer user_data)
11492 if (!strstr (msg, "g_set_prgname"))
11493 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, msg);
11495 #endif
11497 /* Create invisible cursor on X display referred by DPYINFO. */
11499 static Cursor
11500 make_invisible_cursor (struct x_display_info *dpyinfo)
11502 Display *dpy = dpyinfo->display;
11503 static char const no_data[] = { 0 };
11504 Pixmap pix;
11505 XColor col;
11506 Cursor c = 0;
11508 x_catch_errors (dpy);
11509 pix = XCreateBitmapFromData (dpy, dpyinfo->root_window, no_data, 1, 1);
11510 if (! x_had_errors_p (dpy) && pix != None)
11512 Cursor pixc;
11513 col.pixel = 0;
11514 col.red = col.green = col.blue = 0;
11515 col.flags = DoRed | DoGreen | DoBlue;
11516 pixc = XCreatePixmapCursor (dpy, pix, pix, &col, &col, 0, 0);
11517 if (! x_had_errors_p (dpy) && pixc != None)
11518 c = pixc;
11519 XFreePixmap (dpy, pix);
11522 x_uncatch_errors ();
11524 return c;
11527 /* True if DPY supports Xfixes extension >= 4. */
11529 static bool
11530 x_probe_xfixes_extension (Display *dpy)
11532 #ifdef HAVE_XFIXES
11533 int major, minor;
11534 return XFixesQueryVersion (dpy, &major, &minor) && major >= 4;
11535 #else
11536 return false;
11537 #endif /* HAVE_XFIXES */
11540 /* Toggle mouse pointer visibility on frame F by using Xfixes functions. */
11542 static void
11543 xfixes_toggle_visible_pointer (struct frame *f, bool invisible)
11545 #ifdef HAVE_XFIXES
11546 if (invisible)
11547 XFixesHideCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11548 else
11549 XFixesShowCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11550 f->pointer_invisible = invisible;
11551 #else
11552 emacs_abort ();
11553 #endif /* HAVE_XFIXES */
11556 /* Toggle mouse pointer visibility on frame F by using invisible cursor. */
11558 static void
11559 x_toggle_visible_pointer (struct frame *f, bool invisible)
11561 eassert (FRAME_DISPLAY_INFO (f)->invisible_cursor != 0);
11562 if (invisible)
11563 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11564 FRAME_DISPLAY_INFO (f)->invisible_cursor);
11565 else
11566 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11567 f->output_data.x->current_cursor);
11568 f->pointer_invisible = invisible;
11571 /* Setup pointer blanking, prefer Xfixes if available. */
11573 static void
11574 x_setup_pointer_blanking (struct x_display_info *dpyinfo)
11576 /* FIXME: the brave tester should set EMACS_XFIXES because we're suspecting
11577 X server bug, see http://debbugs.gnu.org/cgi/bugreport.cgi?bug=17609. */
11578 if (egetenv ("EMACS_XFIXES") && x_probe_xfixes_extension (dpyinfo->display))
11579 dpyinfo->toggle_visible_pointer = xfixes_toggle_visible_pointer;
11580 else
11582 dpyinfo->toggle_visible_pointer = x_toggle_visible_pointer;
11583 dpyinfo->invisible_cursor = make_invisible_cursor (dpyinfo);
11587 /* Current X display connection identifier. Incremented for each next
11588 connection established. */
11589 static unsigned x_display_id;
11591 /* Open a connection to X display DISPLAY_NAME, and return
11592 the structure that describes the open display.
11593 If we cannot contact the display, return null. */
11595 struct x_display_info *
11596 x_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
11598 Display *dpy;
11599 struct terminal *terminal;
11600 struct x_display_info *dpyinfo;
11601 XrmDatabase xrdb;
11602 ptrdiff_t lim;
11604 block_input ();
11606 if (!x_initialized)
11608 x_initialize ();
11609 ++x_initialized;
11612 if (! x_display_ok (SSDATA (display_name)))
11613 error ("Display %s can't be opened", SSDATA (display_name));
11615 #ifdef USE_GTK
11617 #define NUM_ARGV 10
11618 int argc;
11619 char *argv[NUM_ARGV];
11620 char **argv2 = argv;
11621 guint id;
11623 if (x_initialized++ > 1)
11625 xg_display_open (SSDATA (display_name), &dpy);
11627 else
11629 static char display_opt[] = "--display";
11630 static char name_opt[] = "--name";
11632 for (argc = 0; argc < NUM_ARGV; ++argc)
11633 argv[argc] = 0;
11635 argc = 0;
11636 argv[argc++] = initial_argv[0];
11638 if (! NILP (display_name))
11640 argv[argc++] = display_opt;
11641 argv[argc++] = SSDATA (display_name);
11644 argv[argc++] = name_opt;
11645 argv[argc++] = resource_name;
11647 XSetLocaleModifiers ("");
11649 /* Work around GLib bug that outputs a faulty warning. See
11650 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
11651 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
11652 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
11654 /* NULL window -> events for all windows go to our function.
11655 Call before gtk_init so Gtk+ event filters comes after our. */
11656 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
11658 /* gtk_init does set_locale. Fix locale before and after. */
11659 fixup_locale ();
11660 unrequest_sigio (); /* See comment in x_display_ok. */
11661 gtk_init (&argc, &argv2);
11662 request_sigio ();
11663 fixup_locale ();
11665 g_log_remove_handler ("GLib", id);
11667 xg_initialize ();
11669 dpy = DEFAULT_GDK_DISPLAY ();
11671 #if ! GTK_CHECK_VERSION (2, 90, 0)
11672 /* Load our own gtkrc if it exists. */
11674 const char *file = "~/.emacs.d/gtkrc";
11675 Lisp_Object s, abs_file;
11677 s = build_string (file);
11678 abs_file = Fexpand_file_name (s, Qnil);
11680 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
11681 gtk_rc_parse (SSDATA (abs_file));
11683 #endif
11685 XSetErrorHandler (x_error_handler);
11686 XSetIOErrorHandler (x_io_error_quitter);
11689 #else /* not USE_GTK */
11690 #ifdef USE_X_TOOLKIT
11691 /* weiner@footloose.sps.mot.com reports that this causes
11692 errors with X11R5:
11693 X protocol error: BadAtom (invalid Atom parameter)
11694 on protocol request 18skiloaf.
11695 So let's not use it until R6. */
11696 #ifdef HAVE_X11XTR6
11697 XtSetLanguageProc (NULL, NULL, NULL);
11698 #endif
11701 int argc = 0;
11702 char *argv[3];
11704 argv[0] = "";
11705 argc = 1;
11706 if (xrm_option)
11708 argv[argc++] = "-xrm";
11709 argv[argc++] = xrm_option;
11711 turn_on_atimers (false);
11712 unrequest_sigio (); /* See comment in x_display_ok. */
11713 dpy = XtOpenDisplay (Xt_app_con, SSDATA (display_name),
11714 resource_name, EMACS_CLASS,
11715 emacs_options, XtNumber (emacs_options),
11716 &argc, argv);
11717 request_sigio ();
11718 turn_on_atimers (true);
11720 #ifdef HAVE_X11XTR6
11721 /* I think this is to compensate for XtSetLanguageProc. */
11722 fixup_locale ();
11723 #endif
11726 #else /* not USE_X_TOOLKIT */
11727 XSetLocaleModifiers ("");
11728 unrequest_sigio (); // See comment in x_display_ok.
11729 dpy = XOpenDisplay (SSDATA (display_name));
11730 request_sigio ();
11731 #endif /* not USE_X_TOOLKIT */
11732 #endif /* not USE_GTK*/
11734 /* Detect failure. */
11735 if (dpy == 0)
11737 unblock_input ();
11738 return 0;
11741 /* We have definitely succeeded. Record the new connection. */
11743 dpyinfo = xzalloc (sizeof *dpyinfo);
11744 terminal = x_create_terminal (dpyinfo);
11747 struct x_display_info *share;
11749 for (share = x_display_list; share; share = share->next)
11750 if (same_x_server (SSDATA (XCAR (share->name_list_element)),
11751 SSDATA (display_name)))
11752 break;
11753 if (share)
11754 terminal->kboard = share->terminal->kboard;
11755 else
11757 terminal->kboard = allocate_kboard (Qx);
11759 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
11761 char *vendor = ServerVendor (dpy);
11763 /* Temporarily hide the partially initialized terminal. */
11764 terminal_list = terminal->next_terminal;
11765 unblock_input ();
11766 kset_system_key_alist
11767 (terminal->kboard,
11768 call1 (Qvendor_specific_keysyms,
11769 vendor ? build_string (vendor) : empty_unibyte_string));
11770 block_input ();
11771 terminal->next_terminal = terminal_list;
11772 terminal_list = terminal;
11775 /* Don't let the initial kboard remain current longer than necessary.
11776 That would cause problems if a file loaded on startup tries to
11777 prompt in the mini-buffer. */
11778 if (current_kboard == initial_kboard)
11779 current_kboard = terminal->kboard;
11781 terminal->kboard->reference_count++;
11784 /* Put this display on the chain. */
11785 dpyinfo->next = x_display_list;
11786 x_display_list = dpyinfo;
11788 dpyinfo->name_list_element = Fcons (display_name, Qnil);
11789 dpyinfo->display = dpy;
11790 dpyinfo->connection = ConnectionNumber (dpyinfo->display);
11792 /* Set the name of the terminal. */
11793 terminal->name = xlispstrdup (display_name);
11795 #if false
11796 XSetAfterFunction (x_current_display, x_trace_wire);
11797 #endif
11799 lim = min (PTRDIFF_MAX, SIZE_MAX) - sizeof "@";
11800 Lisp_Object system_name = Fsystem_name ();
11801 if (lim - SBYTES (Vinvocation_name) < SBYTES (system_name))
11802 memory_full (SIZE_MAX);
11803 dpyinfo->x_id = ++x_display_id;
11804 dpyinfo->x_id_name = xmalloc (SBYTES (Vinvocation_name)
11805 + SBYTES (system_name) + 2);
11806 char *nametail = lispstpcpy (dpyinfo->x_id_name, Vinvocation_name);
11807 *nametail++ = '@';
11808 lispstpcpy (nametail, system_name);
11810 /* Figure out which modifier bits mean what. */
11811 x_find_modifier_meanings (dpyinfo);
11813 /* Get the scroll bar cursor. */
11814 #ifdef USE_GTK
11815 /* We must create a GTK cursor, it is required for GTK widgets. */
11816 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
11817 #endif /* USE_GTK */
11819 dpyinfo->vertical_scroll_bar_cursor
11820 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
11822 dpyinfo->horizontal_scroll_bar_cursor
11823 = XCreateFontCursor (dpyinfo->display, XC_sb_h_double_arrow);
11825 xrdb = x_load_resources (dpyinfo->display, xrm_option,
11826 resource_name, EMACS_CLASS);
11827 #ifdef HAVE_XRMSETDATABASE
11828 XrmSetDatabase (dpyinfo->display, xrdb);
11829 #else
11830 dpyinfo->display->db = xrdb;
11831 #endif
11832 /* Put the rdb where we can find it in a way that works on
11833 all versions. */
11834 dpyinfo->xrdb = xrdb;
11836 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
11837 DefaultScreen (dpyinfo->display));
11838 select_visual (dpyinfo);
11839 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
11840 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
11841 dpyinfo->icon_bitmap_id = -1;
11842 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
11844 reset_mouse_highlight (&dpyinfo->mouse_highlight);
11846 /* See if we can construct pixel values from RGB values. */
11847 if (dpyinfo->visual->class == TrueColor)
11849 get_bits_and_offset (dpyinfo->visual->red_mask,
11850 &dpyinfo->red_bits, &dpyinfo->red_offset);
11851 get_bits_and_offset (dpyinfo->visual->blue_mask,
11852 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
11853 get_bits_and_offset (dpyinfo->visual->green_mask,
11854 &dpyinfo->green_bits, &dpyinfo->green_offset);
11857 /* See if a private colormap is requested. */
11858 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
11860 if (dpyinfo->visual->class == PseudoColor)
11862 AUTO_STRING (privateColormap, "privateColormap");
11863 AUTO_STRING (PrivateColormap, "PrivateColormap");
11864 Lisp_Object value
11865 = display_x_get_resource (dpyinfo, privateColormap,
11866 PrivateColormap, Qnil, Qnil);
11867 if (STRINGP (value)
11868 && (!strcmp (SSDATA (value), "true")
11869 || !strcmp (SSDATA (value), "on")))
11870 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
11873 else
11874 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
11875 dpyinfo->visual, AllocNone);
11877 #ifdef HAVE_XFT
11879 /* If we are using Xft, the following precautions should be made:
11881 1. Make sure that the Xrender extension is added before the Xft one.
11882 Otherwise, the close-display hook set by Xft is called after the one
11883 for Xrender, and the former tries to re-add the latter. This results
11884 in inconsistency of internal states and leads to X protocol error when
11885 one reconnects to the same X server (Bug#1696).
11887 2. Check dpi value in X resources. It is better we use it as well,
11888 since Xft will use it, as will all Gnome applications. If our real DPI
11889 is smaller or larger than the one Xft uses, our font will look smaller
11890 or larger than other for other applications, even if it is the same
11891 font name (monospace-10 for example). */
11893 int event_base, error_base;
11894 char *v;
11895 double d;
11897 XRenderQueryExtension (dpyinfo->display, &event_base, &error_base);
11899 v = XGetDefault (dpyinfo->display, "Xft", "dpi");
11900 if (v != NULL && sscanf (v, "%lf", &d) == 1)
11901 dpyinfo->resy = dpyinfo->resx = d;
11903 #endif
11905 if (dpyinfo->resy < 1)
11907 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
11908 double pixels = DisplayHeight (dpyinfo->display, screen_number);
11909 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
11910 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
11911 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
11912 pixels = DisplayWidth (dpyinfo->display, screen_number);
11913 mm = DisplayWidthMM (dpyinfo->display, screen_number);
11914 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
11915 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
11919 static const struct
11921 const char *name;
11922 int offset;
11923 } atom_refs[] = {
11924 #define ATOM_REFS_INIT(string, member) \
11925 { string, offsetof (struct x_display_info, member) },
11926 ATOM_REFS_INIT ("WM_PROTOCOLS", Xatom_wm_protocols)
11927 ATOM_REFS_INIT ("WM_TAKE_FOCUS", Xatom_wm_take_focus)
11928 ATOM_REFS_INIT ("WM_SAVE_YOURSELF", Xatom_wm_save_yourself)
11929 ATOM_REFS_INIT ("WM_DELETE_WINDOW", Xatom_wm_delete_window)
11930 ATOM_REFS_INIT ("WM_CHANGE_STATE", Xatom_wm_change_state)
11931 ATOM_REFS_INIT ("WM_CONFIGURE_DENIED", Xatom_wm_configure_denied)
11932 ATOM_REFS_INIT ("WM_MOVED", Xatom_wm_window_moved)
11933 ATOM_REFS_INIT ("WM_CLIENT_LEADER", Xatom_wm_client_leader)
11934 ATOM_REFS_INIT ("Editres", Xatom_editres)
11935 ATOM_REFS_INIT ("CLIPBOARD", Xatom_CLIPBOARD)
11936 ATOM_REFS_INIT ("TIMESTAMP", Xatom_TIMESTAMP)
11937 ATOM_REFS_INIT ("TEXT", Xatom_TEXT)
11938 ATOM_REFS_INIT ("COMPOUND_TEXT", Xatom_COMPOUND_TEXT)
11939 ATOM_REFS_INIT ("UTF8_STRING", Xatom_UTF8_STRING)
11940 ATOM_REFS_INIT ("DELETE", Xatom_DELETE)
11941 ATOM_REFS_INIT ("MULTIPLE", Xatom_MULTIPLE)
11942 ATOM_REFS_INIT ("INCR", Xatom_INCR)
11943 ATOM_REFS_INIT ("_EMACS_TMP_", Xatom_EMACS_TMP)
11944 ATOM_REFS_INIT ("TARGETS", Xatom_TARGETS)
11945 ATOM_REFS_INIT ("NULL", Xatom_NULL)
11946 ATOM_REFS_INIT ("ATOM", Xatom_ATOM)
11947 ATOM_REFS_INIT ("ATOM_PAIR", Xatom_ATOM_PAIR)
11948 ATOM_REFS_INIT ("CLIPBOARD_MANAGER", Xatom_CLIPBOARD_MANAGER)
11949 ATOM_REFS_INIT ("_XEMBED_INFO", Xatom_XEMBED_INFO)
11950 /* For properties of font. */
11951 ATOM_REFS_INIT ("PIXEL_SIZE", Xatom_PIXEL_SIZE)
11952 ATOM_REFS_INIT ("AVERAGE_WIDTH", Xatom_AVERAGE_WIDTH)
11953 ATOM_REFS_INIT ("_MULE_BASELINE_OFFSET", Xatom_MULE_BASELINE_OFFSET)
11954 ATOM_REFS_INIT ("_MULE_RELATIVE_COMPOSE", Xatom_MULE_RELATIVE_COMPOSE)
11955 ATOM_REFS_INIT ("_MULE_DEFAULT_ASCENT", Xatom_MULE_DEFAULT_ASCENT)
11956 /* Ghostscript support. */
11957 ATOM_REFS_INIT ("DONE", Xatom_DONE)
11958 ATOM_REFS_INIT ("PAGE", Xatom_PAGE)
11959 ATOM_REFS_INIT ("SCROLLBAR", Xatom_Scrollbar)
11960 ATOM_REFS_INIT ("HORIZONTAL_SCROLLBAR", Xatom_Horizontal_Scrollbar)
11961 ATOM_REFS_INIT ("_XEMBED", Xatom_XEMBED)
11962 /* EWMH */
11963 ATOM_REFS_INIT ("_NET_WM_STATE", Xatom_net_wm_state)
11964 ATOM_REFS_INIT ("_NET_WM_STATE_FULLSCREEN", Xatom_net_wm_state_fullscreen)
11965 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_HORZ",
11966 Xatom_net_wm_state_maximized_horz)
11967 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_VERT",
11968 Xatom_net_wm_state_maximized_vert)
11969 ATOM_REFS_INIT ("_NET_WM_STATE_STICKY", Xatom_net_wm_state_sticky)
11970 ATOM_REFS_INIT ("_NET_WM_STATE_HIDDEN", Xatom_net_wm_state_hidden)
11971 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE", Xatom_net_window_type)
11972 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE_TOOLTIP",
11973 Xatom_net_window_type_tooltip)
11974 ATOM_REFS_INIT ("_NET_WM_ICON_NAME", Xatom_net_wm_icon_name)
11975 ATOM_REFS_INIT ("_NET_WM_NAME", Xatom_net_wm_name)
11976 ATOM_REFS_INIT ("_NET_SUPPORTED", Xatom_net_supported)
11977 ATOM_REFS_INIT ("_NET_SUPPORTING_WM_CHECK", Xatom_net_supporting_wm_check)
11978 ATOM_REFS_INIT ("_NET_WM_WINDOW_OPACITY", Xatom_net_wm_window_opacity)
11979 ATOM_REFS_INIT ("_NET_ACTIVE_WINDOW", Xatom_net_active_window)
11980 ATOM_REFS_INIT ("_NET_FRAME_EXTENTS", Xatom_net_frame_extents)
11981 ATOM_REFS_INIT ("_NET_CURRENT_DESKTOP", Xatom_net_current_desktop)
11982 ATOM_REFS_INIT ("_NET_WORKAREA", Xatom_net_workarea)
11983 /* Session management */
11984 ATOM_REFS_INIT ("SM_CLIENT_ID", Xatom_SM_CLIENT_ID)
11985 ATOM_REFS_INIT ("_XSETTINGS_SETTINGS", Xatom_xsettings_prop)
11986 ATOM_REFS_INIT ("MANAGER", Xatom_xsettings_mgr)
11989 int i;
11990 enum { atom_count = ARRAYELTS (atom_refs) };
11991 /* 1 for _XSETTINGS_SN. */
11992 enum { total_atom_count = 1 + atom_count };
11993 Atom atoms_return[total_atom_count];
11994 char *atom_names[total_atom_count];
11995 static char const xsettings_fmt[] = "_XSETTINGS_S%d";
11996 char xsettings_atom_name[sizeof xsettings_fmt - 2
11997 + INT_STRLEN_BOUND (int)];
11999 for (i = 0; i < atom_count; i++)
12000 atom_names[i] = (char *) atom_refs[i].name;
12002 /* Build _XSETTINGS_SN atom name. */
12003 sprintf (xsettings_atom_name, xsettings_fmt,
12004 XScreenNumberOfScreen (dpyinfo->screen));
12005 atom_names[i] = xsettings_atom_name;
12007 XInternAtoms (dpyinfo->display, atom_names, total_atom_count,
12008 False, atoms_return);
12010 for (i = 0; i < atom_count; i++)
12011 *(Atom *) ((char *) dpyinfo + atom_refs[i].offset) = atoms_return[i];
12013 /* Manually copy last atom. */
12014 dpyinfo->Xatom_xsettings_sel = atoms_return[i];
12017 dpyinfo->x_dnd_atoms_size = 8;
12018 dpyinfo->x_dnd_atoms = xmalloc (sizeof *dpyinfo->x_dnd_atoms
12019 * dpyinfo->x_dnd_atoms_size);
12020 dpyinfo->gray
12021 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12022 gray_bits, gray_width, gray_height,
12023 1, 0, 1);
12025 x_setup_pointer_blanking (dpyinfo);
12027 #ifdef HAVE_X_I18N
12028 xim_initialize (dpyinfo, resource_name);
12029 #endif
12031 xsettings_initialize (dpyinfo);
12033 /* This is only needed for distinguishing keyboard and process input. */
12034 if (dpyinfo->connection != 0)
12035 add_keyboard_wait_descriptor (dpyinfo->connection);
12037 #ifdef F_SETOWN
12038 fcntl (dpyinfo->connection, F_SETOWN, getpid ());
12039 #endif /* ! defined (F_SETOWN) */
12041 if (interrupt_input)
12042 init_sigio (dpyinfo->connection);
12044 #ifdef USE_LUCID
12046 XrmValue d, fr, to;
12047 Font font;
12049 dpy = dpyinfo->display;
12050 d.addr = (XPointer)&dpy;
12051 d.size = sizeof (Display *);
12052 fr.addr = XtDefaultFont;
12053 fr.size = sizeof (XtDefaultFont);
12054 to.size = sizeof (Font *);
12055 to.addr = (XPointer)&font;
12056 x_catch_errors (dpy);
12057 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
12058 emacs_abort ();
12059 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
12060 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
12061 /* Do not free XFontStruct returned by the above call to XQueryFont.
12062 This leads to X protocol errors at XtCloseDisplay (Bug#18403). */
12063 x_uncatch_errors ();
12065 #endif
12067 /* See if we should run in synchronous mode. This is useful
12068 for debugging X code. */
12070 AUTO_STRING (synchronous, "synchronous");
12071 AUTO_STRING (Synchronous, "Synchronous");
12072 Lisp_Object value = display_x_get_resource (dpyinfo, synchronous,
12073 Synchronous, Qnil, Qnil);
12074 if (STRINGP (value)
12075 && (!strcmp (SSDATA (value), "true")
12076 || !strcmp (SSDATA (value), "on")))
12077 XSynchronize (dpyinfo->display, True);
12081 AUTO_STRING (useXIM, "useXIM");
12082 AUTO_STRING (UseXIM, "UseXIM");
12083 Lisp_Object value = display_x_get_resource (dpyinfo, useXIM, UseXIM,
12084 Qnil, Qnil);
12085 #ifdef USE_XIM
12086 if (STRINGP (value)
12087 && (!strcmp (SSDATA (value), "false")
12088 || !strcmp (SSDATA (value), "off")))
12089 use_xim = false;
12090 #else
12091 if (STRINGP (value)
12092 && (!strcmp (SSDATA (value), "true")
12093 || !strcmp (SSDATA (value), "on")))
12094 use_xim = true;
12095 #endif
12098 #ifdef HAVE_X_SM
12099 /* Only do this for the very first display in the Emacs session.
12100 Ignore X session management when Emacs was first started on a
12101 tty or started as a daemon. */
12102 if (terminal->id == 1 && ! IS_DAEMON)
12103 x_session_initialize (dpyinfo);
12104 #endif
12106 #ifdef USE_CAIRO
12107 x_extension_initialize (dpyinfo);
12108 #endif
12110 unblock_input ();
12112 return dpyinfo;
12115 /* Get rid of display DPYINFO, deleting all frames on it,
12116 and without sending any more commands to the X server. */
12118 static void
12119 x_delete_display (struct x_display_info *dpyinfo)
12121 struct terminal *t;
12123 /* Close all frames and delete the generic struct terminal for this
12124 X display. */
12125 for (t = terminal_list; t; t = t->next_terminal)
12126 if (t->type == output_x_window && t->display_info.x == dpyinfo)
12128 #ifdef HAVE_X_SM
12129 /* Close X session management when we close its display. */
12130 if (t->id == 1 && x_session_have_connection ())
12131 x_session_close ();
12132 #endif
12133 delete_terminal (t);
12134 break;
12137 if (next_noop_dpyinfo == dpyinfo)
12138 next_noop_dpyinfo = dpyinfo->next;
12140 if (x_display_list == dpyinfo)
12141 x_display_list = dpyinfo->next;
12142 else
12144 struct x_display_info *tail;
12146 for (tail = x_display_list; tail; tail = tail->next)
12147 if (tail->next == dpyinfo)
12148 tail->next = tail->next->next;
12151 xfree (dpyinfo->x_id_name);
12152 xfree (dpyinfo->x_dnd_atoms);
12153 xfree (dpyinfo->color_cells);
12154 xfree (dpyinfo);
12157 #ifdef USE_X_TOOLKIT
12159 /* Atimer callback function for TIMER. Called every 0.1s to process
12160 Xt timeouts, if needed. We must avoid calling XtAppPending as
12161 much as possible because that function does an implicit XFlush
12162 that slows us down. */
12164 static void
12165 x_process_timeouts (struct atimer *timer)
12167 block_input ();
12168 x_timeout_atimer_activated_flag = false;
12169 if (toolkit_scroll_bar_interaction || popup_activated ())
12171 while (XtAppPending (Xt_app_con) & XtIMTimer)
12172 XtAppProcessEvent (Xt_app_con, XtIMTimer);
12173 /* Reactivate the atimer for next time. */
12174 x_activate_timeout_atimer ();
12176 unblock_input ();
12179 /* Install an asynchronous timer that processes Xt timeout events
12180 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
12181 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
12182 function whenever these variables are set. This is necessary
12183 because some widget sets use timeouts internally, for example the
12184 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
12185 processed, these widgets don't behave normally. */
12187 void
12188 x_activate_timeout_atimer (void)
12190 block_input ();
12191 if (!x_timeout_atimer_activated_flag)
12193 struct timespec interval = make_timespec (0, 100 * 1000 * 1000);
12194 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
12195 x_timeout_atimer_activated_flag = true;
12197 unblock_input ();
12200 #endif /* USE_X_TOOLKIT */
12203 /* Set up use of X before we make the first connection. */
12205 static struct redisplay_interface x_redisplay_interface =
12207 x_frame_parm_handlers,
12208 x_produce_glyphs,
12209 x_write_glyphs,
12210 x_insert_glyphs,
12211 x_clear_end_of_line,
12212 x_scroll_run,
12213 x_after_update_window_line,
12214 x_update_window_begin,
12215 x_update_window_end,
12216 x_flush,
12217 x_clear_window_mouse_face,
12218 x_get_glyph_overhangs,
12219 x_fix_overlapping_area,
12220 x_draw_fringe_bitmap,
12221 #ifdef USE_CAIRO
12222 x_cr_define_fringe_bitmap,
12223 x_cr_destroy_fringe_bitmap,
12224 #else
12225 0, /* define_fringe_bitmap */
12226 0, /* destroy_fringe_bitmap */
12227 #endif
12228 x_compute_glyph_string_overhangs,
12229 x_draw_glyph_string,
12230 x_define_frame_cursor,
12231 x_clear_frame_area,
12232 x_draw_window_cursor,
12233 x_draw_vertical_window_border,
12234 x_draw_window_divider,
12235 x_shift_glyphs_for_insert, /* Never called; see comment in function. */
12236 x_show_hourglass,
12237 x_hide_hourglass
12241 /* This function is called when the last frame on a display is deleted. */
12242 void
12243 x_delete_terminal (struct terminal *terminal)
12245 struct x_display_info *dpyinfo = terminal->display_info.x;
12247 /* Protect against recursive calls. delete_frame in
12248 delete_terminal calls us back when it deletes our last frame. */
12249 if (!terminal->name)
12250 return;
12252 block_input ();
12253 #ifdef HAVE_X_I18N
12254 /* We must close our connection to the XIM server before closing the
12255 X display. */
12256 if (dpyinfo->xim)
12257 xim_close_dpy (dpyinfo);
12258 #endif
12260 /* Normally, the display is available... */
12261 if (dpyinfo->display)
12263 x_destroy_all_bitmaps (dpyinfo);
12264 XSetCloseDownMode (dpyinfo->display, DestroyAll);
12266 /* Whether or not XCloseDisplay destroys the associated resource
12267 database depends on the version of libX11. To avoid both
12268 crash and memory leak, we dissociate the database from the
12269 display and then destroy dpyinfo->xrdb ourselves.
12271 Unfortunately, the above strategy does not work in some
12272 situations due to a bug in newer versions of libX11: because
12273 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
12274 dpy->db is NULL, XCloseDisplay destroys the associated
12275 database whereas it has not been created by XGetDefault
12276 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
12277 don't destroy the database here in order to avoid the crash
12278 in the above situations for now, though that may cause memory
12279 leaks in other situations. */
12280 #if false
12281 #ifdef HAVE_XRMSETDATABASE
12282 XrmSetDatabase (dpyinfo->display, NULL);
12283 #else
12284 dpyinfo->display->db = NULL;
12285 #endif
12286 /* We used to call XrmDestroyDatabase from x_delete_display, but
12287 some older versions of libX11 crash if we call it after
12288 closing all the displays. */
12289 XrmDestroyDatabase (dpyinfo->xrdb);
12290 #endif
12292 #ifdef USE_GTK
12293 xg_display_close (dpyinfo->display);
12294 #else
12295 #ifdef USE_X_TOOLKIT
12296 XtCloseDisplay (dpyinfo->display);
12297 #else
12298 XCloseDisplay (dpyinfo->display);
12299 #endif
12300 #endif /* ! USE_GTK */
12301 /* Do not close the connection here because it's already closed
12302 by X(t)CloseDisplay (Bug#18403). */
12303 dpyinfo->display = NULL;
12306 /* ...but if called from x_connection_closed, the display may already
12307 be closed and dpyinfo->display was set to 0 to indicate that. Since
12308 X server is most likely gone, explicit close is the only reliable
12309 way to continue and avoid Bug#19147. */
12310 else if (dpyinfo->connection >= 0)
12311 emacs_close (dpyinfo->connection);
12313 /* No more input on this descriptor. */
12314 delete_keyboard_wait_descriptor (dpyinfo->connection);
12315 /* Mark as dead. */
12316 dpyinfo->connection = -1;
12318 x_delete_display (dpyinfo);
12319 unblock_input ();
12322 /* Create a struct terminal, initialize it with the X11 specific
12323 functions and make DISPLAY->TERMINAL point to it. */
12325 static struct terminal *
12326 x_create_terminal (struct x_display_info *dpyinfo)
12328 struct terminal *terminal;
12330 terminal = create_terminal (output_x_window, &x_redisplay_interface);
12332 terminal->display_info.x = dpyinfo;
12333 dpyinfo->terminal = terminal;
12335 /* kboard is initialized in x_term_init. */
12337 terminal->clear_frame_hook = x_clear_frame;
12338 terminal->ins_del_lines_hook = x_ins_del_lines;
12339 terminal->delete_glyphs_hook = x_delete_glyphs;
12340 terminal->ring_bell_hook = XTring_bell;
12341 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
12342 terminal->update_begin_hook = x_update_begin;
12343 terminal->update_end_hook = x_update_end;
12344 terminal->read_socket_hook = XTread_socket;
12345 terminal->frame_up_to_date_hook = XTframe_up_to_date;
12346 terminal->mouse_position_hook = XTmouse_position;
12347 terminal->frame_rehighlight_hook = XTframe_rehighlight;
12348 terminal->frame_raise_lower_hook = XTframe_raise_lower;
12349 terminal->fullscreen_hook = XTfullscreen_hook;
12350 terminal->menu_show_hook = x_menu_show;
12351 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
12352 terminal->popup_dialog_hook = xw_popup_dialog;
12353 #endif
12354 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
12355 terminal->set_horizontal_scroll_bar_hook = XTset_horizontal_scroll_bar;
12356 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
12357 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
12358 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
12359 terminal->delete_frame_hook = x_destroy_window;
12360 terminal->delete_terminal_hook = x_delete_terminal;
12361 /* Other hooks are NULL by default. */
12363 return terminal;
12366 static void
12367 x_initialize (void)
12369 baud_rate = 19200;
12371 x_noop_count = 0;
12372 any_help_event_p = false;
12373 ignore_next_mouse_click_timeout = 0;
12375 #ifdef USE_GTK
12376 current_count = -1;
12377 #endif
12379 /* Try to use interrupt input; if we can't, then start polling. */
12380 Fset_input_interrupt_mode (Qt);
12382 #ifdef USE_X_TOOLKIT
12383 XtToolkitInitialize ();
12385 Xt_app_con = XtCreateApplicationContext ();
12387 /* Register a converter from strings to pixels, which uses
12388 Emacs' color allocation infrastructure. */
12389 XtAppSetTypeConverter (Xt_app_con,
12390 XtRString, XtRPixel, cvt_string_to_pixel,
12391 cvt_string_to_pixel_args,
12392 XtNumber (cvt_string_to_pixel_args),
12393 XtCacheByDisplay, cvt_pixel_dtor);
12395 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
12396 #endif
12398 #ifdef USE_TOOLKIT_SCROLL_BARS
12399 #ifndef USE_GTK
12400 xaw3d_arrow_scroll = False;
12401 xaw3d_pick_top = True;
12402 #endif
12403 #endif
12405 #ifdef USE_CAIRO
12406 x_cr_init_fringe (&x_redisplay_interface);
12407 #endif
12409 /* Note that there is no real way portable across R3/R4 to get the
12410 original error handler. */
12411 XSetErrorHandler (x_error_handler);
12412 XSetIOErrorHandler (x_io_error_quitter);
12415 #ifdef USE_GTK
12416 void
12417 init_xterm (void)
12419 /* Emacs can handle only core input events, so make sure
12420 Gtk doesn't use Xinput or Xinput2 extensions. */
12421 xputenv ("GDK_CORE_DEVICE_EVENTS=1");
12423 #endif
12425 void
12426 syms_of_xterm (void)
12428 x_error_message = NULL;
12430 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
12431 DEFSYM (Qlatin_1, "latin-1");
12433 #ifdef USE_GTK
12434 xg_default_icon_file = build_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
12435 staticpro (&xg_default_icon_file);
12437 DEFSYM (Qx_gtk_map_stock, "x-gtk-map-stock");
12438 #endif
12440 DEFVAR_BOOL ("x-use-underline-position-properties",
12441 x_use_underline_position_properties,
12442 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
12443 A value of nil means ignore them. If you encounter fonts with bogus
12444 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
12445 to 4.1, set this to nil. You can also use `underline-minimum-offset'
12446 to override the font's UNDERLINE_POSITION for small font display
12447 sizes. */);
12448 x_use_underline_position_properties = true;
12450 DEFVAR_BOOL ("x-underline-at-descent-line",
12451 x_underline_at_descent_line,
12452 doc: /* Non-nil means to draw the underline at the same place as the descent line.
12453 A value of nil means to draw the underline according to the value of the
12454 variable `x-use-underline-position-properties', which is usually at the
12455 baseline level. The default value is nil. */);
12456 x_underline_at_descent_line = false;
12458 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
12459 x_mouse_click_focus_ignore_position,
12460 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
12461 This variable is only used when the window manager requires that you
12462 click on a frame to select it (give it focus). In that case, a value
12463 of nil, means that the selected window and cursor position changes to
12464 reflect the mouse click position, while a non-nil value means that the
12465 selected window or cursor position is preserved. */);
12466 x_mouse_click_focus_ignore_position = false;
12468 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
12469 doc: /* Which toolkit scroll bars Emacs uses, if any.
12470 A value of nil means Emacs doesn't use toolkit scroll bars.
12471 With the X Window system, the value is a symbol describing the
12472 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
12473 With MS Windows or Nextstep, the value is t. */);
12474 #ifdef USE_TOOLKIT_SCROLL_BARS
12475 #ifdef USE_MOTIF
12476 Vx_toolkit_scroll_bars = intern_c_string ("motif");
12477 #elif defined HAVE_XAW3D
12478 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
12479 #elif USE_GTK
12480 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
12481 #else
12482 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
12483 #endif
12484 #else
12485 Vx_toolkit_scroll_bars = Qnil;
12486 #endif
12488 DEFSYM (Qmodifier_value, "modifier-value");
12489 DEFSYM (Qalt, "alt");
12490 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
12491 DEFSYM (Qhyper, "hyper");
12492 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
12493 DEFSYM (Qmeta, "meta");
12494 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
12495 DEFSYM (Qsuper, "super");
12496 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
12498 DEFVAR_LISP ("x-alt-keysym", Vx_alt_keysym,
12499 doc: /* Which keys Emacs uses for the alt modifier.
12500 This should be one of the symbols `alt', `hyper', `meta', `super'.
12501 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
12502 is nil, which is the same as `alt'. */);
12503 Vx_alt_keysym = Qnil;
12505 DEFVAR_LISP ("x-hyper-keysym", Vx_hyper_keysym,
12506 doc: /* Which keys Emacs uses for the hyper modifier.
12507 This should be one of the symbols `alt', `hyper', `meta', `super'.
12508 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
12509 default is nil, which is the same as `hyper'. */);
12510 Vx_hyper_keysym = Qnil;
12512 DEFVAR_LISP ("x-meta-keysym", Vx_meta_keysym,
12513 doc: /* Which keys Emacs uses for the meta modifier.
12514 This should be one of the symbols `alt', `hyper', `meta', `super'.
12515 For example, `meta' means use the Meta_L and Meta_R keysyms. The
12516 default is nil, which is the same as `meta'. */);
12517 Vx_meta_keysym = Qnil;
12519 DEFVAR_LISP ("x-super-keysym", Vx_super_keysym,
12520 doc: /* Which keys Emacs uses for the super modifier.
12521 This should be one of the symbols `alt', `hyper', `meta', `super'.
12522 For example, `super' means use the Super_L and Super_R keysyms. The
12523 default is nil, which is the same as `super'. */);
12524 Vx_super_keysym = Qnil;
12526 DEFVAR_LISP ("x-keysym-table", Vx_keysym_table,
12527 doc: /* Hash table of character codes indexed by X keysym codes. */);
12528 Vx_keysym_table = make_hash_table (hashtest_eql, make_number (900),
12529 make_float (DEFAULT_REHASH_SIZE),
12530 make_float (DEFAULT_REHASH_THRESHOLD),
12531 Qnil);
12533 DEFVAR_BOOL ("x-frame-normalize-before-maximize",
12534 x_frame_normalize_before_maximize,
12535 doc: /* Non-nil means normalize frame before maximizing.
12536 If this variable is t, Emacs first asks the window manager to give the
12537 frame its normal size, and only then the final state, whenever changing
12538 from a full-height, full-width or full-both state to the maximized one
12539 or when changing from the maximized to the full-height or full-width
12540 state.
12542 Set this variable only if your window manager cannot handle the
12543 transition between the various maximization states. */);
12544 x_frame_normalize_before_maximize = false;