Subject: * lisp/progmodes/prolog.el: Improve handling of if/then/else
[emacs.git] / src / xterm.c
blob13e1902a0bb6d8992eab5f00d1428f7ca65f7d45
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, args[2];
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 args[0] = intern ("concat");
663 args[1] = Fnreverse (acc);
664 return Fapply (2, args);
667 #endif /* USE_CAIRO */
669 static void
670 x_free_cr_resources (struct frame *f)
672 #ifdef USE_CAIRO
673 if (f == NULL)
675 Lisp_Object rest, frame;
676 FOR_EACH_FRAME (rest, frame)
677 if (FRAME_X_P (XFRAME (frame)))
678 x_free_cr_resources (XFRAME (frame));
680 else
682 cairo_t *cr = FRAME_CR_CONTEXT (f);
684 if (cr)
686 cairo_surface_t *surface = cairo_get_target (cr);
688 if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_XLIB)
690 cairo_destroy (cr);
691 FRAME_CR_CONTEXT (f) = NULL;
695 #endif
698 static void
699 x_set_clip_rectangles (struct frame *f, GC gc, XRectangle *rectangles, int n)
701 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, rectangles, n, Unsorted);
702 #ifdef USE_CAIRO
703 eassert (n >= 0 && n <= MAX_CLIP_RECTS);
706 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 1);
708 gc_ext->n_clip_rects = n;
709 memcpy (gc_ext->clip_rects, rectangles, sizeof (XRectangle) * n);
711 #endif
714 static void
715 x_reset_clip_rectangles (struct frame *f, GC gc)
717 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
718 #ifdef USE_CAIRO
720 struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 0);
722 if (gc_ext)
723 gc_ext->n_clip_rects = 0;
725 #endif
728 static void
729 x_fill_rectangle (struct frame *f, GC gc, int x, int y, int width, int height)
731 #ifdef USE_CAIRO
732 cairo_t *cr;
734 cr = x_begin_cr_clip (f, gc);
735 x_set_cr_source_with_gc_foreground (f, gc);
736 cairo_rectangle (cr, x, y, width, height);
737 cairo_fill (cr);
738 x_end_cr_clip (f);
739 #else
740 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
741 gc, x, y, width, height);
742 #endif
745 static void
746 x_draw_rectangle (struct frame *f, GC gc, int x, int y, int width, int height)
748 #ifdef USE_CAIRO
749 cairo_t *cr;
751 cr = x_begin_cr_clip (f, gc);
752 x_set_cr_source_with_gc_foreground (f, gc);
753 cairo_rectangle (cr, x + 0.5, y + 0.5, width, height);
754 cairo_set_line_width (cr, 1);
755 cairo_stroke (cr);
756 x_end_cr_clip (f);
757 #else
758 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
759 gc, x, y, width, height);
760 #endif
763 static void
764 x_clear_window (struct frame *f)
766 #ifdef USE_CAIRO
767 cairo_t *cr;
769 cr = x_begin_cr_clip (f, NULL);
770 x_set_cr_source_with_gc_background (f, f->output_data.x->normal_gc);
771 cairo_paint (cr);
772 x_end_cr_clip (f);
773 #else
774 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
775 #endif
778 #ifdef USE_CAIRO
779 static void
780 x_fill_trapezoid_for_relief (struct frame *f, GC gc, int x, int y,
781 int width, int height, int top_p)
783 cairo_t *cr;
785 cr = x_begin_cr_clip (f, gc);
786 x_set_cr_source_with_gc_foreground (f, gc);
787 cairo_move_to (cr, top_p ? x : x + height, y);
788 cairo_line_to (cr, x, y + height);
789 cairo_line_to (cr, top_p ? x + width - height : x + width, y + height);
790 cairo_line_to (cr, x + width, y);
791 cairo_fill (cr);
792 x_end_cr_clip (f);
795 enum corners
797 CORNER_BOTTOM_RIGHT, /* 0 -> pi/2 */
798 CORNER_BOTTOM_LEFT, /* pi/2 -> pi */
799 CORNER_TOP_LEFT, /* pi -> 3pi/2 */
800 CORNER_TOP_RIGHT, /* 3pi/2 -> 2pi */
801 CORNER_LAST
804 static void
805 x_erase_corners_for_relief (struct frame *f, GC gc, int x, int y,
806 int width, int height,
807 double radius, double margin, int corners)
809 cairo_t *cr;
810 int i;
812 cr = x_begin_cr_clip (f, gc);
813 x_set_cr_source_with_gc_background (f, gc);
814 for (i = 0; i < CORNER_LAST; i++)
815 if (corners & (1 << i))
817 double xm, ym, xc, yc;
819 if (i == CORNER_TOP_LEFT || i == CORNER_BOTTOM_LEFT)
820 xm = x - margin, xc = xm + radius;
821 else
822 xm = x + width + margin, xc = xm - radius;
823 if (i == CORNER_TOP_LEFT || i == CORNER_TOP_RIGHT)
824 ym = y - margin, yc = ym + radius;
825 else
826 ym = y + height + margin, yc = ym - radius;
828 cairo_move_to (cr, xm, ym);
829 cairo_arc (cr, xc, yc, radius, i * M_PI_2, (i + 1) * M_PI_2);
831 cairo_clip (cr);
832 cairo_rectangle (cr, x, y, width, height);
833 cairo_fill (cr);
834 x_end_cr_clip (f);
837 static void
838 x_draw_horizontal_wave (struct frame *f, GC gc, int x, int y,
839 int width, int height, int wave_length)
841 cairo_t *cr;
842 double dx = wave_length, dy = height - 1;
843 int xoffset, n;
845 cr = x_begin_cr_clip (f, gc);
846 x_set_cr_source_with_gc_foreground (f, gc);
847 cairo_rectangle (cr, x, y, width, height);
848 cairo_clip (cr);
850 if (x >= 0)
852 xoffset = x % (wave_length * 2);
853 if (xoffset == 0)
854 xoffset = wave_length * 2;
856 else
857 xoffset = x % (wave_length * 2) + wave_length * 2;
858 n = (width + xoffset) / wave_length + 1;
859 if (xoffset > wave_length)
861 xoffset -= wave_length;
862 --n;
863 y += height - 1;
864 dy = -dy;
867 cairo_move_to (cr, x - xoffset + 0.5, y + 0.5);
868 while (--n >= 0)
870 cairo_rel_line_to (cr, dx, dy);
871 dy = -dy;
873 cairo_set_line_width (cr, 1);
874 cairo_stroke (cr);
875 x_end_cr_clip (f);
877 #endif
880 /* Return the struct x_display_info corresponding to DPY. */
882 struct x_display_info *
883 x_display_info_for_display (Display *dpy)
885 struct x_display_info *dpyinfo;
887 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
888 if (dpyinfo->display == dpy)
889 return dpyinfo;
891 return 0;
894 static Window
895 x_find_topmost_parent (struct frame *f)
897 struct x_output *x = f->output_data.x;
898 Window win = None, wi = x->parent_desc;
899 Display *dpy = FRAME_X_DISPLAY (f);
901 while (wi != FRAME_DISPLAY_INFO (f)->root_window)
903 Window root;
904 Window *children;
905 unsigned int nchildren;
907 win = wi;
908 if (XQueryTree (dpy, win, &root, &wi, &children, &nchildren))
909 XFree (children);
910 else
911 break;
914 return win;
917 #define OPAQUE 0xffffffff
919 void
920 x_set_frame_alpha (struct frame *f)
922 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
923 Display *dpy = FRAME_X_DISPLAY (f);
924 Window win = FRAME_OUTER_WINDOW (f);
925 double alpha = 1.0;
926 double alpha_min = 1.0;
927 unsigned long opac;
928 Window parent;
930 if (dpyinfo->x_highlight_frame == f)
931 alpha = f->alpha[0];
932 else
933 alpha = f->alpha[1];
935 if (FLOATP (Vframe_alpha_lower_limit))
936 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
937 else if (INTEGERP (Vframe_alpha_lower_limit))
938 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
940 if (alpha < 0.0)
941 return;
942 else if (alpha > 1.0)
943 alpha = 1.0;
944 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
945 alpha = alpha_min;
947 opac = alpha * OPAQUE;
949 x_catch_errors (dpy);
951 /* If there is a parent from the window manager, put the property there
952 also, to work around broken window managers that fail to do that.
953 Do this unconditionally as this function is called on reparent when
954 alpha has not changed on the frame. */
956 parent = x_find_topmost_parent (f);
957 if (parent != None)
958 XChangeProperty (dpy, parent, dpyinfo->Xatom_net_wm_window_opacity,
959 XA_CARDINAL, 32, PropModeReplace,
960 (unsigned char *) &opac, 1);
962 /* return unless necessary */
964 unsigned char *data;
965 Atom actual;
966 int rc, format;
967 unsigned long n, left;
969 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
970 0, 1, False, XA_CARDINAL,
971 &actual, &format, &n, &left,
972 &data);
974 if (rc == Success && actual != None)
976 unsigned long value = *(unsigned long *)data;
977 XFree (data);
978 if (value == opac)
980 x_uncatch_errors ();
981 return;
986 XChangeProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
987 XA_CARDINAL, 32, PropModeReplace,
988 (unsigned char *) &opac, 1);
989 x_uncatch_errors ();
992 /***********************************************************************
993 Starting and ending an update
994 ***********************************************************************/
996 /* Start an update of frame F. This function is installed as a hook
997 for update_begin, i.e. it is called when update_begin is called.
998 This function is called prior to calls to x_update_window_begin for
999 each window being updated. Currently, there is nothing to do here
1000 because all interesting stuff is done on a window basis. */
1002 static void
1003 x_update_begin (struct frame *f)
1005 #ifdef USE_CAIRO
1006 if (! NILP (tip_frame) && XFRAME (tip_frame) == f
1007 && ! FRAME_VISIBLE_P (f))
1008 return;
1010 if (! FRAME_CR_SURFACE (f))
1012 int width, height;
1013 #ifdef USE_GTK
1014 if (FRAME_GTK_WIDGET (f))
1016 GdkWindow *w = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
1017 width = gdk_window_get_width (w);
1018 height = gdk_window_get_height (w);
1020 else
1021 #endif
1023 width = FRAME_PIXEL_WIDTH (f);
1024 height = FRAME_PIXEL_HEIGHT (f);
1025 if (! FRAME_EXTERNAL_TOOL_BAR (f))
1026 height += FRAME_TOOL_BAR_HEIGHT (f);
1027 if (! FRAME_EXTERNAL_MENU_BAR (f))
1028 height += FRAME_MENU_BAR_HEIGHT (f);
1031 if (width > 0 && height > 0)
1033 block_input();
1034 FRAME_CR_SURFACE (f) = cairo_image_surface_create
1035 (CAIRO_FORMAT_ARGB32, width, height);
1036 unblock_input();
1039 #endif /* USE_CAIRO */
1042 /* Start update of window W. */
1044 static void
1045 x_update_window_begin (struct window *w)
1047 struct frame *f = XFRAME (WINDOW_FRAME (w));
1048 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
1050 w->output_cursor = w->cursor;
1052 block_input ();
1054 if (f == hlinfo->mouse_face_mouse_frame)
1056 /* Don't do highlighting for mouse motion during the update. */
1057 hlinfo->mouse_face_defer = true;
1059 /* If F needs to be redrawn, simply forget about any prior mouse
1060 highlighting. */
1061 if (FRAME_GARBAGED_P (f))
1062 hlinfo->mouse_face_window = Qnil;
1065 unblock_input ();
1069 /* Draw a vertical window border from (x,y0) to (x,y1) */
1071 static void
1072 x_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
1074 struct frame *f = XFRAME (WINDOW_FRAME (w));
1075 struct face *face;
1077 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
1078 if (face)
1079 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
1080 face->foreground);
1082 #ifdef USE_CAIRO
1083 x_fill_rectangle (f, f->output_data.x->normal_gc, x, y0, 1, y1 - y0);
1084 #else
1085 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1086 f->output_data.x->normal_gc, x, y0, x, y1);
1087 #endif
1090 /* Draw a window divider from (x0,y0) to (x1,y1) */
1092 static void
1093 x_draw_window_divider (struct window *w, int x0, int x1, int y0, int y1)
1095 struct frame *f = XFRAME (WINDOW_FRAME (w));
1096 struct face *face = FACE_FROM_ID (f, WINDOW_DIVIDER_FACE_ID);
1097 struct face *face_first = FACE_FROM_ID (f, WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
1098 struct face *face_last = FACE_FROM_ID (f, WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
1099 unsigned long color = face ? face->foreground : FRAME_FOREGROUND_PIXEL (f);
1100 unsigned long color_first = (face_first
1101 ? face_first->foreground
1102 : FRAME_FOREGROUND_PIXEL (f));
1103 unsigned long color_last = (face_last
1104 ? face_last->foreground
1105 : FRAME_FOREGROUND_PIXEL (f));
1106 Display *display = FRAME_X_DISPLAY (f);
1108 if (y1 - y0 > x1 - x0 && x1 - x0 > 2)
1109 /* Vertical. */
1111 XSetForeground (display, f->output_data.x->normal_gc, color_first);
1112 x_fill_rectangle (f, f->output_data.x->normal_gc,
1113 x0, y0, 1, y1 - y0);
1114 XSetForeground (display, f->output_data.x->normal_gc, color);
1115 x_fill_rectangle (f, f->output_data.x->normal_gc,
1116 x0 + 1, y0, x1 - x0 - 2, y1 - y0);
1117 XSetForeground (display, f->output_data.x->normal_gc, color_last);
1118 x_fill_rectangle (f, f->output_data.x->normal_gc,
1119 x1 - 1, y0, 1, y1 - y0);
1121 else if (x1 - x0 > y1 - y0 && y1 - y0 > 3)
1122 /* Horizontal. */
1124 XSetForeground (display, f->output_data.x->normal_gc, color_first);
1125 x_fill_rectangle (f, f->output_data.x->normal_gc,
1126 x0, y0, x1 - x0, 1);
1127 XSetForeground (display, f->output_data.x->normal_gc, color);
1128 x_fill_rectangle (f, f->output_data.x->normal_gc,
1129 x0, y0 + 1, x1 - x0, y1 - y0 - 2);
1130 XSetForeground (display, f->output_data.x->normal_gc, color_last);
1131 x_fill_rectangle (f, f->output_data.x->normal_gc,
1132 x0, y1 - 1, x1 - x0, 1);
1134 else
1136 XSetForeground (display, f->output_data.x->normal_gc, color);
1137 x_fill_rectangle (f, f->output_data.x->normal_gc,
1138 x0, y0, x1 - x0, y1 - y0);
1142 /* End update of window W.
1144 Draw vertical borders between horizontally adjacent windows, and
1145 display W's cursor if CURSOR_ON_P is non-zero.
1147 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
1148 glyphs in mouse-face were overwritten. In that case we have to
1149 make sure that the mouse-highlight is properly redrawn.
1151 W may be a menu bar pseudo-window in case we don't have X toolkit
1152 support. Such windows don't have a cursor, so don't display it
1153 here. */
1155 static void
1156 x_update_window_end (struct window *w, bool cursor_on_p,
1157 bool mouse_face_overwritten_p)
1159 if (!w->pseudo_window_p)
1161 block_input ();
1163 if (cursor_on_p)
1164 display_and_set_cursor (w, true,
1165 w->output_cursor.hpos, w->output_cursor.vpos,
1166 w->output_cursor.x, w->output_cursor.y);
1168 if (draw_window_fringes (w, true))
1170 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
1171 x_draw_right_divider (w);
1172 else
1173 x_draw_vertical_border (w);
1176 unblock_input ();
1179 /* If a row with mouse-face was overwritten, arrange for
1180 XTframe_up_to_date to redisplay the mouse highlight. */
1181 if (mouse_face_overwritten_p)
1183 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
1185 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
1186 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
1187 hlinfo->mouse_face_window = Qnil;
1192 /* End update of frame F. This function is installed as a hook in
1193 update_end. */
1195 static void
1196 x_update_end (struct frame *f)
1198 /* Mouse highlight may be displayed again. */
1199 MOUSE_HL_INFO (f)->mouse_face_defer = false;
1201 #ifdef USE_CAIRO
1202 if (FRAME_CR_SURFACE (f))
1204 cairo_t *cr = 0;
1205 block_input();
1206 #if defined (USE_GTK) && defined (HAVE_GTK3)
1207 if (FRAME_GTK_WIDGET (f))
1209 GdkWindow *w = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
1210 cr = gdk_cairo_create (w);
1212 else
1213 #endif
1215 cairo_surface_t *surface;
1216 int width = FRAME_PIXEL_WIDTH (f);
1217 int height = FRAME_PIXEL_HEIGHT (f);
1218 if (! FRAME_EXTERNAL_TOOL_BAR (f))
1219 height += FRAME_TOOL_BAR_HEIGHT (f);
1220 if (! FRAME_EXTERNAL_MENU_BAR (f))
1221 height += FRAME_MENU_BAR_HEIGHT (f);
1222 surface = cairo_xlib_surface_create (FRAME_X_DISPLAY (f),
1223 FRAME_X_WINDOW (f),
1224 FRAME_DISPLAY_INFO (f)->visual,
1225 width,
1226 height);
1227 cr = cairo_create (surface);
1228 cairo_surface_destroy (surface);
1231 cairo_set_source_surface (cr, FRAME_CR_SURFACE (f), 0, 0);
1232 cairo_paint (cr);
1233 cairo_destroy (cr);
1234 unblock_input ();
1236 #endif /* USE_CAIRO */
1238 #ifndef XFlush
1239 block_input ();
1240 XFlush (FRAME_X_DISPLAY (f));
1241 unblock_input ();
1242 #endif
1246 /* This function is called from various places in xdisp.c
1247 whenever a complete update has been performed. */
1249 static void
1250 XTframe_up_to_date (struct frame *f)
1252 if (FRAME_X_P (f))
1253 FRAME_MOUSE_UPDATE (f);
1257 /* Clear under internal border if any (GTK has its own version). */
1258 #ifndef USE_GTK
1259 void
1260 x_clear_under_internal_border (struct frame *f)
1262 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
1264 int border = FRAME_INTERNAL_BORDER_WIDTH (f);
1265 int width = FRAME_PIXEL_WIDTH (f);
1266 int height = FRAME_PIXEL_HEIGHT (f);
1267 int margin = FRAME_TOP_MARGIN_HEIGHT (f);
1269 block_input ();
1270 x_clear_area (f, 0, 0, border, height);
1271 x_clear_area (f, 0, margin, width, border);
1272 x_clear_area (f, width - border, 0, border, height);
1273 x_clear_area (f, 0, height - border, width, border);
1274 unblock_input ();
1277 #endif
1279 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
1280 arrow bitmaps, or clear the fringes if no bitmaps are required
1281 before DESIRED_ROW is made current. This function is called from
1282 update_window_line only if it is known that there are differences
1283 between bitmaps to be drawn between current row and DESIRED_ROW. */
1285 static void
1286 x_after_update_window_line (struct window *w, struct glyph_row *desired_row)
1288 eassert (w);
1290 if (!desired_row->mode_line_p && !w->pseudo_window_p)
1291 desired_row->redraw_fringe_bitmaps_p = true;
1293 #ifdef USE_X_TOOLKIT
1294 /* When a window has disappeared, make sure that no rest of
1295 full-width rows stays visible in the internal border. Could
1296 check here if updated window is the leftmost/rightmost window,
1297 but I guess it's not worth doing since vertically split windows
1298 are almost never used, internal border is rarely set, and the
1299 overhead is very small. */
1301 struct frame *f;
1302 int width, height;
1304 if (windows_or_buffers_changed
1305 && desired_row->full_width_p
1306 && (f = XFRAME (w->frame),
1307 width = FRAME_INTERNAL_BORDER_WIDTH (f),
1308 width != 0)
1309 && (height = desired_row->visible_height,
1310 height > 0))
1312 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
1314 block_input ();
1315 x_clear_area (f, 0, y, width, height);
1316 x_clear_area (f, FRAME_PIXEL_WIDTH (f) - width, y, width, height);
1317 unblock_input ();
1320 #endif
1323 static void
1324 x_draw_fringe_bitmap (struct window *w, struct glyph_row *row, struct draw_fringe_bitmap_params *p)
1326 struct frame *f = XFRAME (WINDOW_FRAME (w));
1327 Display *display = FRAME_X_DISPLAY (f);
1328 Window window = FRAME_X_WINDOW (f);
1329 GC gc = f->output_data.x->normal_gc;
1330 struct face *face = p->face;
1332 /* Must clip because of partially visible lines. */
1333 x_clip_to_row (w, row, ANY_AREA, gc);
1335 if (p->bx >= 0 && !p->overlay_p)
1337 /* In case the same realized face is used for fringes and
1338 for something displayed in the text (e.g. face `region' on
1339 mono-displays, the fill style may have been changed to
1340 FillSolid in x_draw_glyph_string_background. */
1341 if (face->stipple)
1342 XSetFillStyle (display, face->gc, FillOpaqueStippled);
1343 else
1344 XSetForeground (display, face->gc, face->background);
1346 x_fill_rectangle (f, face->gc, p->bx, p->by, p->nx, p->ny);
1348 if (!face->stipple)
1349 XSetForeground (display, face->gc, face->foreground);
1352 #ifdef USE_CAIRO
1353 if (p->which && p->which < max_fringe_bmp)
1355 XGCValues gcv;
1357 XGetGCValues (display, gc, GCForeground | GCBackground, &gcv);
1358 XSetForeground (display, gc, (p->cursor_p
1359 ? (p->overlay_p ? face->background
1360 : f->output_data.x->cursor_pixel)
1361 : face->foreground));
1362 XSetBackground (display, gc, face->background);
1363 x_cr_draw_image (f, gc, fringe_bmp[p->which], 0, p->dh,
1364 p->wd, p->h, p->x, p->y, p->overlay_p);
1365 XSetForeground (display, gc, gcv.foreground);
1366 XSetBackground (display, gc, gcv.background);
1368 #else /* not USE_CAIRO */
1369 if (p->which)
1371 char *bits;
1372 Pixmap pixmap, clipmask = (Pixmap) 0;
1373 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
1374 XGCValues gcv;
1376 if (p->wd > 8)
1377 bits = (char *) (p->bits + p->dh);
1378 else
1379 bits = (char *) p->bits + p->dh;
1381 /* Draw the bitmap. I believe these small pixmaps can be cached
1382 by the server. */
1383 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
1384 (p->cursor_p
1385 ? (p->overlay_p ? face->background
1386 : f->output_data.x->cursor_pixel)
1387 : face->foreground),
1388 face->background, depth);
1390 if (p->overlay_p)
1392 clipmask = XCreatePixmapFromBitmapData (display,
1393 FRAME_DISPLAY_INFO (f)->root_window,
1394 bits, p->wd, p->h,
1395 1, 0, 1);
1396 gcv.clip_mask = clipmask;
1397 gcv.clip_x_origin = p->x;
1398 gcv.clip_y_origin = p->y;
1399 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
1402 XCopyArea (display, pixmap, window, gc, 0, 0,
1403 p->wd, p->h, p->x, p->y);
1404 XFreePixmap (display, pixmap);
1406 if (p->overlay_p)
1408 gcv.clip_mask = (Pixmap) 0;
1409 XChangeGC (display, gc, GCClipMask, &gcv);
1410 XFreePixmap (display, clipmask);
1413 #endif /* not USE_CAIRO */
1415 x_reset_clip_rectangles (f, gc);
1418 /***********************************************************************
1419 Glyph display
1420 ***********************************************************************/
1424 static void x_set_glyph_string_clipping (struct glyph_string *);
1425 static void x_set_glyph_string_gc (struct glyph_string *);
1426 static void x_draw_glyph_string_foreground (struct glyph_string *);
1427 static void x_draw_composite_glyph_string_foreground (struct glyph_string *);
1428 static void x_draw_glyph_string_box (struct glyph_string *);
1429 static void x_draw_glyph_string (struct glyph_string *);
1430 static _Noreturn void x_delete_glyphs (struct frame *, int);
1431 static void x_compute_glyph_string_overhangs (struct glyph_string *);
1432 static void x_set_cursor_gc (struct glyph_string *);
1433 static void x_set_mode_line_face_gc (struct glyph_string *);
1434 static void x_set_mouse_face_gc (struct glyph_string *);
1435 static bool x_alloc_lighter_color (struct frame *, Display *, Colormap,
1436 unsigned long *, double, int);
1437 static void x_setup_relief_color (struct frame *, struct relief *,
1438 double, int, unsigned long);
1439 static void x_setup_relief_colors (struct glyph_string *);
1440 static void x_draw_image_glyph_string (struct glyph_string *);
1441 static void x_draw_image_relief (struct glyph_string *);
1442 static void x_draw_image_foreground (struct glyph_string *);
1443 static void x_draw_image_foreground_1 (struct glyph_string *, Pixmap);
1444 static void x_clear_glyph_string_rect (struct glyph_string *, int,
1445 int, int, int);
1446 static void x_draw_relief_rect (struct frame *, int, int, int, int,
1447 int, bool, bool, bool, bool, bool,
1448 XRectangle *);
1449 static void x_draw_box_rect (struct glyph_string *, int, int, int, int,
1450 int, bool, bool, XRectangle *);
1451 static void x_scroll_bar_clear (struct frame *);
1453 #ifdef GLYPH_DEBUG
1454 static void x_check_font (struct frame *, struct font *);
1455 #endif
1458 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
1459 face. */
1461 static void
1462 x_set_cursor_gc (struct glyph_string *s)
1464 if (s->font == FRAME_FONT (s->f)
1465 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
1466 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
1467 && !s->cmp)
1468 s->gc = s->f->output_data.x->cursor_gc;
1469 else
1471 /* Cursor on non-default face: must merge. */
1472 XGCValues xgcv;
1473 unsigned long mask;
1475 xgcv.background = s->f->output_data.x->cursor_pixel;
1476 xgcv.foreground = s->face->background;
1478 /* If the glyph would be invisible, try a different foreground. */
1479 if (xgcv.foreground == xgcv.background)
1480 xgcv.foreground = s->face->foreground;
1481 if (xgcv.foreground == xgcv.background)
1482 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1483 if (xgcv.foreground == xgcv.background)
1484 xgcv.foreground = s->face->foreground;
1486 /* Make sure the cursor is distinct from text in this face. */
1487 if (xgcv.background == s->face->background
1488 && xgcv.foreground == s->face->foreground)
1490 xgcv.background = s->face->foreground;
1491 xgcv.foreground = s->face->background;
1494 IF_DEBUG (x_check_font (s->f, s->font));
1495 xgcv.graphics_exposures = False;
1496 mask = GCForeground | GCBackground | GCGraphicsExposures;
1498 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1499 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1500 mask, &xgcv);
1501 else
1502 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1503 = XCreateGC (s->display, s->window, mask, &xgcv);
1505 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1510 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1512 static void
1513 x_set_mouse_face_gc (struct glyph_string *s)
1515 int face_id;
1516 struct face *face;
1518 /* What face has to be used last for the mouse face? */
1519 face_id = MOUSE_HL_INFO (s->f)->mouse_face_face_id;
1520 face = FACE_FROM_ID (s->f, face_id);
1521 if (face == NULL)
1522 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1524 if (s->first_glyph->type == CHAR_GLYPH)
1525 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1526 else
1527 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1528 s->face = FACE_FROM_ID (s->f, face_id);
1529 prepare_face_for_display (s->f, s->face);
1531 if (s->font == s->face->font)
1532 s->gc = s->face->gc;
1533 else
1535 /* Otherwise construct scratch_cursor_gc with values from FACE
1536 except for FONT. */
1537 XGCValues xgcv;
1538 unsigned long mask;
1540 xgcv.background = s->face->background;
1541 xgcv.foreground = s->face->foreground;
1542 xgcv.graphics_exposures = False;
1543 mask = GCForeground | GCBackground | GCGraphicsExposures;
1545 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1546 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1547 mask, &xgcv);
1548 else
1549 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
1550 = XCreateGC (s->display, s->window, mask, &xgcv);
1552 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1555 eassert (s->gc != 0);
1559 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1560 Faces to use in the mode line have already been computed when the
1561 matrix was built, so there isn't much to do, here. */
1563 static void
1564 x_set_mode_line_face_gc (struct glyph_string *s)
1566 s->gc = s->face->gc;
1570 /* Set S->gc of glyph string S for drawing that glyph string. Set
1571 S->stippled_p to a non-zero value if the face of S has a stipple
1572 pattern. */
1574 static void
1575 x_set_glyph_string_gc (struct glyph_string *s)
1577 prepare_face_for_display (s->f, s->face);
1579 if (s->hl == DRAW_NORMAL_TEXT)
1581 s->gc = s->face->gc;
1582 s->stippled_p = s->face->stipple != 0;
1584 else if (s->hl == DRAW_INVERSE_VIDEO)
1586 x_set_mode_line_face_gc (s);
1587 s->stippled_p = s->face->stipple != 0;
1589 else if (s->hl == DRAW_CURSOR)
1591 x_set_cursor_gc (s);
1592 s->stippled_p = false;
1594 else if (s->hl == DRAW_MOUSE_FACE)
1596 x_set_mouse_face_gc (s);
1597 s->stippled_p = s->face->stipple != 0;
1599 else if (s->hl == DRAW_IMAGE_RAISED
1600 || s->hl == DRAW_IMAGE_SUNKEN)
1602 s->gc = s->face->gc;
1603 s->stippled_p = s->face->stipple != 0;
1605 else
1606 emacs_abort ();
1608 /* GC must have been set. */
1609 eassert (s->gc != 0);
1613 /* Set clipping for output of glyph string S. S may be part of a mode
1614 line or menu if we don't have X toolkit support. */
1616 static void
1617 x_set_glyph_string_clipping (struct glyph_string *s)
1619 XRectangle *r = s->clip;
1620 int n = get_glyph_string_clip_rects (s, r, 2);
1622 if (n > 0)
1623 x_set_clip_rectangles (s->f, s->gc, r, n);
1624 s->num_clips = n;
1628 /* Set SRC's clipping for output of glyph string DST. This is called
1629 when we are drawing DST's left_overhang or right_overhang only in
1630 the area of SRC. */
1632 static void
1633 x_set_glyph_string_clipping_exactly (struct glyph_string *src, struct glyph_string *dst)
1635 XRectangle r;
1637 r.x = src->x;
1638 r.width = src->width;
1639 r.y = src->y;
1640 r.height = src->height;
1641 dst->clip[0] = r;
1642 dst->num_clips = 1;
1643 x_set_clip_rectangles (dst->f, dst->gc, &r, 1);
1647 /* RIF:
1648 Compute left and right overhang of glyph string S. */
1650 static void
1651 x_compute_glyph_string_overhangs (struct glyph_string *s)
1653 if (s->cmp == NULL
1654 && (s->first_glyph->type == CHAR_GLYPH
1655 || s->first_glyph->type == COMPOSITE_GLYPH))
1657 struct font_metrics metrics;
1659 if (s->first_glyph->type == CHAR_GLYPH)
1661 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1662 struct font *font = s->font;
1663 int i;
1665 for (i = 0; i < s->nchars; i++)
1666 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1667 font->driver->text_extents (font, code, s->nchars, &metrics);
1669 else
1671 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1673 composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
1675 s->right_overhang = (metrics.rbearing > metrics.width
1676 ? metrics.rbearing - metrics.width : 0);
1677 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1679 else if (s->cmp)
1681 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1682 s->left_overhang = - s->cmp->lbearing;
1687 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1689 static void
1690 x_clear_glyph_string_rect (struct glyph_string *s, int x, int y, int w, int h)
1692 XGCValues xgcv;
1693 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1694 XSetForeground (s->display, s->gc, xgcv.background);
1695 x_fill_rectangle (s->f, s->gc, x, y, w, h);
1696 XSetForeground (s->display, s->gc, xgcv.foreground);
1700 /* Draw the background of glyph_string S. If S->background_filled_p
1701 is non-zero don't draw it. FORCE_P non-zero means draw the
1702 background even if it wouldn't be drawn normally. This is used
1703 when a string preceding S draws into the background of S, or S
1704 contains the first component of a composition. */
1706 static void
1707 x_draw_glyph_string_background (struct glyph_string *s, bool force_p)
1709 /* Nothing to do if background has already been drawn or if it
1710 shouldn't be drawn in the first place. */
1711 if (!s->background_filled_p)
1713 int box_line_width = max (s->face->box_line_width, 0);
1715 if (s->stippled_p)
1717 /* Fill background with a stipple pattern. */
1718 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1719 x_fill_rectangle (s->f, s->gc, s->x,
1720 s->y + box_line_width,
1721 s->background_width,
1722 s->height - 2 * box_line_width);
1723 XSetFillStyle (s->display, s->gc, FillSolid);
1724 s->background_filled_p = true;
1726 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1727 /* When xdisp.c ignores FONT_HEIGHT, we cannot trust
1728 font dimensions, since the actual glyphs might be
1729 much smaller. So in that case we always clear the
1730 rectangle with background color. */
1731 || FONT_TOO_HIGH (s->font)
1732 || s->font_not_found_p
1733 || s->extends_to_end_of_line_p
1734 || force_p)
1736 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1737 s->background_width,
1738 s->height - 2 * box_line_width);
1739 s->background_filled_p = true;
1745 /* Draw the foreground of glyph string S. */
1747 static void
1748 x_draw_glyph_string_foreground (struct glyph_string *s)
1750 int i, x;
1752 /* If first glyph of S has a left box line, start drawing the text
1753 of S to the right of that box line. */
1754 if (s->face->box != FACE_NO_BOX
1755 && s->first_glyph->left_box_line_p)
1756 x = s->x + eabs (s->face->box_line_width);
1757 else
1758 x = s->x;
1760 /* Draw characters of S as rectangles if S's font could not be
1761 loaded. */
1762 if (s->font_not_found_p)
1764 for (i = 0; i < s->nchars; ++i)
1766 struct glyph *g = s->first_glyph + i;
1767 x_draw_rectangle (s->f,
1768 s->gc, x, s->y, g->pixel_width - 1,
1769 s->height - 1);
1770 x += g->pixel_width;
1773 else
1775 struct font *font = s->font;
1776 int boff = font->baseline_offset;
1777 int y;
1779 if (font->vertical_centering)
1780 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1782 y = s->ybase - boff;
1783 if (s->for_overlaps
1784 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1785 font->driver->draw (s, 0, s->nchars, x, y, false);
1786 else
1787 font->driver->draw (s, 0, s->nchars, x, y, true);
1788 if (s->face->overstrike)
1789 font->driver->draw (s, 0, s->nchars, x + 1, y, false);
1793 /* Draw the foreground of composite glyph string S. */
1795 static void
1796 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1798 int i, j, x;
1799 struct font *font = s->font;
1801 /* If first glyph of S has a left box line, start drawing the text
1802 of S to the right of that box line. */
1803 if (s->face && s->face->box != FACE_NO_BOX
1804 && s->first_glyph->left_box_line_p)
1805 x = s->x + eabs (s->face->box_line_width);
1806 else
1807 x = s->x;
1809 /* S is a glyph string for a composition. S->cmp_from is the index
1810 of the first character drawn for glyphs of this composition.
1811 S->cmp_from == 0 means we are drawing the very first character of
1812 this composition. */
1814 /* Draw a rectangle for the composition if the font for the very
1815 first character of the composition could not be loaded. */
1816 if (s->font_not_found_p)
1818 if (s->cmp_from == 0)
1819 x_draw_rectangle (s->f, s->gc, x, s->y,
1820 s->width - 1, s->height - 1);
1822 else if (! s->first_glyph->u.cmp.automatic)
1824 int y = s->ybase;
1826 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1827 /* TAB in a composition means display glyphs with padding
1828 space on the left or right. */
1829 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1831 int xx = x + s->cmp->offsets[j * 2];
1832 int yy = y - s->cmp->offsets[j * 2 + 1];
1834 font->driver->draw (s, j, j + 1, xx, yy, false);
1835 if (s->face->overstrike)
1836 font->driver->draw (s, j, j + 1, xx + 1, yy, false);
1839 else
1841 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1842 Lisp_Object glyph;
1843 int y = s->ybase;
1844 int width = 0;
1846 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1848 glyph = LGSTRING_GLYPH (gstring, i);
1849 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1850 width += LGLYPH_WIDTH (glyph);
1851 else
1853 int xoff, yoff, wadjust;
1855 if (j < i)
1857 font->driver->draw (s, j, i, x, y, false);
1858 if (s->face->overstrike)
1859 font->driver->draw (s, j, i, x + 1, y, false);
1860 x += width;
1862 xoff = LGLYPH_XOFF (glyph);
1863 yoff = LGLYPH_YOFF (glyph);
1864 wadjust = LGLYPH_WADJUST (glyph);
1865 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, false);
1866 if (s->face->overstrike)
1867 font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff,
1868 false);
1869 x += wadjust;
1870 j = i + 1;
1871 width = 0;
1874 if (j < i)
1876 font->driver->draw (s, j, i, x, y, false);
1877 if (s->face->overstrike)
1878 font->driver->draw (s, j, i, x + 1, y, false);
1884 /* Draw the foreground of glyph string S for glyphless characters. */
1886 static void
1887 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1889 struct glyph *glyph = s->first_glyph;
1890 XChar2b char2b[8];
1891 int x, i, j;
1893 /* If first glyph of S has a left box line, start drawing the text
1894 of S to the right of that box line. */
1895 if (s->face && s->face->box != FACE_NO_BOX
1896 && s->first_glyph->left_box_line_p)
1897 x = s->x + eabs (s->face->box_line_width);
1898 else
1899 x = s->x;
1901 s->char2b = char2b;
1903 for (i = 0; i < s->nchars; i++, glyph++)
1905 char buf[7], *str = NULL;
1906 int len = glyph->u.glyphless.len;
1908 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1910 if (len > 0
1911 && CHAR_TABLE_P (Vglyphless_char_display)
1912 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1913 >= 1))
1915 Lisp_Object acronym
1916 = (! glyph->u.glyphless.for_no_font
1917 ? CHAR_TABLE_REF (Vglyphless_char_display,
1918 glyph->u.glyphless.ch)
1919 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
1920 if (STRINGP (acronym))
1921 str = SSDATA (acronym);
1924 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
1926 sprintf (buf, "%0*X",
1927 glyph->u.glyphless.ch < 0x10000 ? 4 : 6,
1928 glyph->u.glyphless.ch + 0u);
1929 str = buf;
1932 if (str)
1934 int upper_len = (len + 1) / 2;
1935 unsigned code;
1937 /* It is assured that all LEN characters in STR is ASCII. */
1938 for (j = 0; j < len; j++)
1940 code = s->font->driver->encode_char (s->font, str[j]);
1941 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
1943 s->font->driver->draw (s, 0, upper_len,
1944 x + glyph->slice.glyphless.upper_xoff,
1945 s->ybase + glyph->slice.glyphless.upper_yoff,
1946 false);
1947 s->font->driver->draw (s, upper_len, len,
1948 x + glyph->slice.glyphless.lower_xoff,
1949 s->ybase + glyph->slice.glyphless.lower_yoff,
1950 false);
1952 if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
1953 x_draw_rectangle (s->f, s->gc,
1954 x, s->ybase - glyph->ascent,
1955 glyph->pixel_width - 1,
1956 glyph->ascent + glyph->descent - 1);
1957 x += glyph->pixel_width;
1961 #ifdef USE_X_TOOLKIT
1963 #ifdef USE_LUCID
1965 /* Return the frame on which widget WIDGET is used.. Abort if frame
1966 cannot be determined. */
1968 static struct frame *
1969 x_frame_of_widget (Widget widget)
1971 struct x_display_info *dpyinfo;
1972 Lisp_Object tail, frame;
1973 struct frame *f;
1975 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1977 /* Find the top-level shell of the widget. Note that this function
1978 can be called when the widget is not yet realized, so XtWindow
1979 (widget) == 0. That's the reason we can't simply use
1980 x_any_window_to_frame. */
1981 while (!XtIsTopLevelShell (widget))
1982 widget = XtParent (widget);
1984 /* Look for a frame with that top-level widget. Allocate the color
1985 on that frame to get the right gamma correction value. */
1986 FOR_EACH_FRAME (tail, frame)
1988 f = XFRAME (frame);
1989 if (FRAME_X_P (f)
1990 && f->output_data.nothing != 1
1991 && FRAME_DISPLAY_INFO (f) == dpyinfo
1992 && f->output_data.x->widget == widget)
1993 return f;
1995 emacs_abort ();
1998 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1999 or DELTA. Try a color with RGB values multiplied by FACTOR first.
2000 If this produces the same color as PIXEL, try a color where all RGB
2001 values have DELTA added. Return the allocated color in *PIXEL.
2002 DISPLAY is the X display, CMAP is the colormap to operate on.
2003 Value is true if successful. */
2005 bool
2006 x_alloc_lighter_color_for_widget (Widget widget, Display *display, Colormap cmap,
2007 unsigned long *pixel, double factor, int delta)
2009 struct frame *f = x_frame_of_widget (widget);
2010 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
2013 #endif /* USE_LUCID */
2016 /* Structure specifying which arguments should be passed by Xt to
2017 cvt_string_to_pixel. We want the widget's screen and colormap. */
2019 static XtConvertArgRec cvt_string_to_pixel_args[] =
2021 {XtWidgetBaseOffset, (XtPointer) offsetof (WidgetRec, core.screen),
2022 sizeof (Screen *)},
2023 {XtWidgetBaseOffset, (XtPointer) offsetof (WidgetRec, core.colormap),
2024 sizeof (Colormap)}
2028 /* The address of this variable is returned by
2029 cvt_string_to_pixel. */
2031 static Pixel cvt_string_to_pixel_value;
2034 /* Convert a color name to a pixel color.
2036 DPY is the display we are working on.
2038 ARGS is an array of *NARGS XrmValue structures holding additional
2039 information about the widget for which the conversion takes place.
2040 The contents of this array are determined by the specification
2041 in cvt_string_to_pixel_args.
2043 FROM is a pointer to an XrmValue which points to the color name to
2044 convert. TO is an XrmValue in which to return the pixel color.
2046 CLOSURE_RET is a pointer to user-data, in which we record if
2047 we allocated the color or not.
2049 Value is True if successful, False otherwise. */
2051 static Boolean
2052 cvt_string_to_pixel (Display *dpy, XrmValue *args, Cardinal *nargs,
2053 XrmValue *from, XrmValue *to,
2054 XtPointer *closure_ret)
2056 Screen *screen;
2057 Colormap cmap;
2058 Pixel pixel;
2059 String color_name;
2060 XColor color;
2062 if (*nargs != 2)
2064 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
2065 "wrongParameters", "cvt_string_to_pixel",
2066 "XtToolkitError",
2067 "Screen and colormap args required", NULL, NULL);
2068 return False;
2071 screen = *(Screen **) args[0].addr;
2072 cmap = *(Colormap *) args[1].addr;
2073 color_name = (String) from->addr;
2075 if (strcmp (color_name, XtDefaultBackground) == 0)
2077 *closure_ret = (XtPointer) False;
2078 pixel = WhitePixelOfScreen (screen);
2080 else if (strcmp (color_name, XtDefaultForeground) == 0)
2082 *closure_ret = (XtPointer) False;
2083 pixel = BlackPixelOfScreen (screen);
2085 else if (XParseColor (dpy, cmap, color_name, &color)
2086 && x_alloc_nearest_color_1 (dpy, cmap, &color))
2088 pixel = color.pixel;
2089 *closure_ret = (XtPointer) True;
2091 else
2093 String params[1];
2094 Cardinal nparams = 1;
2096 params[0] = color_name;
2097 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
2098 "badValue", "cvt_string_to_pixel",
2099 "XtToolkitError", "Invalid color '%s'",
2100 params, &nparams);
2101 return False;
2104 if (to->addr != NULL)
2106 if (to->size < sizeof (Pixel))
2108 to->size = sizeof (Pixel);
2109 return False;
2112 *(Pixel *) to->addr = pixel;
2114 else
2116 cvt_string_to_pixel_value = pixel;
2117 to->addr = (XtPointer) &cvt_string_to_pixel_value;
2120 to->size = sizeof (Pixel);
2121 return True;
2125 /* Free a pixel color which was previously allocated via
2126 cvt_string_to_pixel. This is registered as the destructor
2127 for this type of resource via XtSetTypeConverter.
2129 APP is the application context in which we work.
2131 TO is a pointer to an XrmValue holding the color to free.
2132 CLOSURE is the value we stored in CLOSURE_RET for this color
2133 in cvt_string_to_pixel.
2135 ARGS and NARGS are like for cvt_string_to_pixel. */
2137 static void
2138 cvt_pixel_dtor (XtAppContext app, XrmValuePtr to, XtPointer closure, XrmValuePtr args,
2139 Cardinal *nargs)
2141 if (*nargs != 2)
2143 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
2144 "XtToolkitError",
2145 "Screen and colormap arguments required",
2146 NULL, NULL);
2148 else if (closure != NULL)
2150 /* We did allocate the pixel, so free it. */
2151 Screen *screen = *(Screen **) args[0].addr;
2152 Colormap cmap = *(Colormap *) args[1].addr;
2153 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
2154 (Pixel *) to->addr, 1);
2159 #endif /* USE_X_TOOLKIT */
2162 /* Value is an array of XColor structures for the contents of the
2163 color map of display DPY. Set *NCELLS to the size of the array.
2164 Note that this probably shouldn't be called for large color maps,
2165 say a 24-bit TrueColor map. */
2167 static const XColor *
2168 x_color_cells (Display *dpy, int *ncells)
2170 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
2172 if (dpyinfo->color_cells == NULL)
2174 Screen *screen = dpyinfo->screen;
2175 int ncolor_cells = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
2176 int i;
2178 dpyinfo->color_cells = xnmalloc (ncolor_cells,
2179 sizeof *dpyinfo->color_cells);
2180 dpyinfo->ncolor_cells = ncolor_cells;
2182 for (i = 0; i < ncolor_cells; ++i)
2183 dpyinfo->color_cells[i].pixel = i;
2185 XQueryColors (dpy, dpyinfo->cmap,
2186 dpyinfo->color_cells, ncolor_cells);
2189 *ncells = dpyinfo->ncolor_cells;
2190 return dpyinfo->color_cells;
2194 /* On frame F, translate pixel colors to RGB values for the NCOLORS
2195 colors in COLORS. Use cached information, if available. */
2197 void
2198 x_query_colors (struct frame *f, XColor *colors, int ncolors)
2200 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2202 if (dpyinfo->color_cells)
2204 int i;
2205 for (i = 0; i < ncolors; ++i)
2207 unsigned long pixel = colors[i].pixel;
2208 eassert (pixel < dpyinfo->ncolor_cells);
2209 eassert (dpyinfo->color_cells[pixel].pixel == pixel);
2210 colors[i] = dpyinfo->color_cells[pixel];
2213 else
2214 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
2218 /* On frame F, translate pixel color to RGB values for the color in
2219 COLOR. Use cached information, if available. */
2221 void
2222 x_query_color (struct frame *f, XColor *color)
2224 x_query_colors (f, color, 1);
2228 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
2229 exact match can't be allocated, try the nearest color available.
2230 Value is true if successful. Set *COLOR to the color
2231 allocated. */
2233 static bool
2234 x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color)
2236 bool rc;
2238 rc = XAllocColor (dpy, cmap, color) != 0;
2239 if (rc == 0)
2241 /* If we got to this point, the colormap is full, so we're going
2242 to try to get the next closest color. The algorithm used is
2243 a least-squares matching, which is what X uses for closest
2244 color matching with StaticColor visuals. */
2245 int nearest, i;
2246 int max_color_delta = 255;
2247 int max_delta = 3 * max_color_delta;
2248 int nearest_delta = max_delta + 1;
2249 int ncells;
2250 const XColor *cells = x_color_cells (dpy, &ncells);
2252 for (nearest = i = 0; i < ncells; ++i)
2254 int dred = (color->red >> 8) - (cells[i].red >> 8);
2255 int dgreen = (color->green >> 8) - (cells[i].green >> 8);
2256 int dblue = (color->blue >> 8) - (cells[i].blue >> 8);
2257 int delta = dred * dred + dgreen * dgreen + dblue * dblue;
2259 if (delta < nearest_delta)
2261 nearest = i;
2262 nearest_delta = delta;
2266 color->red = cells[nearest].red;
2267 color->green = cells[nearest].green;
2268 color->blue = cells[nearest].blue;
2269 rc = XAllocColor (dpy, cmap, color) != 0;
2271 else
2273 /* If allocation succeeded, and the allocated pixel color is not
2274 equal to a cached pixel color recorded earlier, there was a
2275 change in the colormap, so clear the color cache. */
2276 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
2277 XColor *cached_color;
2279 if (dpyinfo->color_cells
2280 && (cached_color = &dpyinfo->color_cells[color->pixel],
2281 (cached_color->red != color->red
2282 || cached_color->blue != color->blue
2283 || cached_color->green != color->green)))
2285 xfree (dpyinfo->color_cells);
2286 dpyinfo->color_cells = NULL;
2287 dpyinfo->ncolor_cells = 0;
2291 #ifdef DEBUG_X_COLORS
2292 if (rc)
2293 register_color (color->pixel);
2294 #endif /* DEBUG_X_COLORS */
2296 return rc;
2300 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
2301 exact match can't be allocated, try the nearest color available.
2302 Value is true if successful. Set *COLOR to the color
2303 allocated. */
2305 bool
2306 x_alloc_nearest_color (struct frame *f, Colormap cmap, XColor *color)
2308 gamma_correct (f, color);
2309 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
2313 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
2314 It's necessary to do this instead of just using PIXEL directly to
2315 get color reference counts right. */
2317 unsigned long
2318 x_copy_color (struct frame *f, unsigned long pixel)
2320 XColor color;
2322 color.pixel = pixel;
2323 block_input ();
2324 x_query_color (f, &color);
2325 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
2326 unblock_input ();
2327 #ifdef DEBUG_X_COLORS
2328 register_color (pixel);
2329 #endif
2330 return color.pixel;
2334 /* Brightness beyond which a color won't have its highlight brightness
2335 boosted.
2337 Nominally, highlight colors for `3d' faces are calculated by
2338 brightening an object's color by a constant scale factor, but this
2339 doesn't yield good results for dark colors, so for colors who's
2340 brightness is less than this value (on a scale of 0-65535) have an
2341 use an additional additive factor.
2343 The value here is set so that the default menu-bar/mode-line color
2344 (grey75) will not have its highlights changed at all. */
2345 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
2348 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
2349 or DELTA. Try a color with RGB values multiplied by FACTOR first.
2350 If this produces the same color as PIXEL, try a color where all RGB
2351 values have DELTA added. Return the allocated color in *PIXEL.
2352 DISPLAY is the X display, CMAP is the colormap to operate on.
2353 Value is non-zero if successful. */
2355 static bool
2356 x_alloc_lighter_color (struct frame *f, Display *display, Colormap cmap,
2357 unsigned long *pixel, double factor, int delta)
2359 XColor color, new;
2360 long bright;
2361 bool success_p;
2363 /* Get RGB color values. */
2364 color.pixel = *pixel;
2365 x_query_color (f, &color);
2367 /* Change RGB values by specified FACTOR. Avoid overflow! */
2368 eassert (factor >= 0);
2369 new.red = min (0xffff, factor * color.red);
2370 new.green = min (0xffff, factor * color.green);
2371 new.blue = min (0xffff, factor * color.blue);
2373 /* Calculate brightness of COLOR. */
2374 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
2376 /* We only boost colors that are darker than
2377 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
2378 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
2379 /* Make an additive adjustment to NEW, because it's dark enough so
2380 that scaling by FACTOR alone isn't enough. */
2382 /* How far below the limit this color is (0 - 1, 1 being darker). */
2383 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
2384 /* The additive adjustment. */
2385 int min_delta = delta * dimness * factor / 2;
2387 if (factor < 1)
2389 new.red = max (0, new.red - min_delta);
2390 new.green = max (0, new.green - min_delta);
2391 new.blue = max (0, new.blue - min_delta);
2393 else
2395 new.red = min (0xffff, min_delta + new.red);
2396 new.green = min (0xffff, min_delta + new.green);
2397 new.blue = min (0xffff, min_delta + new.blue);
2401 /* Try to allocate the color. */
2402 success_p = x_alloc_nearest_color (f, cmap, &new);
2403 if (success_p)
2405 if (new.pixel == *pixel)
2407 /* If we end up with the same color as before, try adding
2408 delta to the RGB values. */
2409 x_free_colors (f, &new.pixel, 1);
2411 new.red = min (0xffff, delta + color.red);
2412 new.green = min (0xffff, delta + color.green);
2413 new.blue = min (0xffff, delta + color.blue);
2414 success_p = x_alloc_nearest_color (f, cmap, &new);
2416 else
2417 success_p = true;
2418 *pixel = new.pixel;
2421 return success_p;
2425 /* Set up the foreground color for drawing relief lines of glyph
2426 string S. RELIEF is a pointer to a struct relief containing the GC
2427 with which lines will be drawn. Use a color that is FACTOR or
2428 DELTA lighter or darker than the relief's background which is found
2429 in S->f->output_data.x->relief_background. If such a color cannot
2430 be allocated, use DEFAULT_PIXEL, instead. */
2432 static void
2433 x_setup_relief_color (struct frame *f, struct relief *relief, double factor,
2434 int delta, unsigned long default_pixel)
2436 XGCValues xgcv;
2437 struct x_output *di = f->output_data.x;
2438 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
2439 unsigned long pixel;
2440 unsigned long background = di->relief_background;
2441 Colormap cmap = FRAME_X_COLORMAP (f);
2442 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2443 Display *dpy = FRAME_X_DISPLAY (f);
2445 xgcv.graphics_exposures = False;
2446 xgcv.line_width = 1;
2448 /* Free previously allocated color. The color cell will be reused
2449 when it has been freed as many times as it was allocated, so this
2450 doesn't affect faces using the same colors. */
2451 if (relief->gc && relief->pixel != -1)
2453 x_free_colors (f, &relief->pixel, 1);
2454 relief->pixel = -1;
2457 /* Allocate new color. */
2458 xgcv.foreground = default_pixel;
2459 pixel = background;
2460 if (dpyinfo->n_planes != 1
2461 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
2462 xgcv.foreground = relief->pixel = pixel;
2464 if (relief->gc == 0)
2466 xgcv.stipple = dpyinfo->gray;
2467 mask |= GCStipple;
2468 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
2470 else
2471 XChangeGC (dpy, relief->gc, mask, &xgcv);
2475 /* Set up colors for the relief lines around glyph string S. */
2477 static void
2478 x_setup_relief_colors (struct glyph_string *s)
2480 struct x_output *di = s->f->output_data.x;
2481 unsigned long color;
2483 if (s->face->use_box_color_for_shadows_p)
2484 color = s->face->box_color;
2485 else if (s->first_glyph->type == IMAGE_GLYPH
2486 && s->img->pixmap
2487 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2488 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2489 else
2491 XGCValues xgcv;
2493 /* Get the background color of the face. */
2494 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2495 color = xgcv.background;
2498 if (di->white_relief.gc == 0
2499 || color != di->relief_background)
2501 di->relief_background = color;
2502 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2503 WHITE_PIX_DEFAULT (s->f));
2504 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2505 BLACK_PIX_DEFAULT (s->f));
2510 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2511 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2512 to draw, it must be >= 0. RAISED_P means draw a raised
2513 relief. LEFT_P means draw a relief on the left side of
2514 the rectangle. RIGHT_P means draw a relief on the right
2515 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2516 when drawing. */
2518 static void
2519 x_draw_relief_rect (struct frame *f,
2520 int left_x, int top_y, int right_x, int bottom_y,
2521 int width, bool raised_p, bool top_p, bool bot_p,
2522 bool left_p, bool right_p,
2523 XRectangle *clip_rect)
2525 #ifdef USE_CAIRO
2526 GC top_left_gc, bottom_right_gc;
2527 int corners = 0;
2529 if (raised_p)
2531 top_left_gc = f->output_data.x->white_relief.gc;
2532 bottom_right_gc = f->output_data.x->black_relief.gc;
2534 else
2536 top_left_gc = f->output_data.x->black_relief.gc;
2537 bottom_right_gc = f->output_data.x->white_relief.gc;
2540 x_set_clip_rectangles (f, top_left_gc, clip_rect, 1);
2541 x_set_clip_rectangles (f, bottom_right_gc, clip_rect, 1);
2543 if (left_p)
2545 x_fill_rectangle (f, top_left_gc, left_x, top_y,
2546 width, bottom_y + 1 - top_y);
2547 if (top_p)
2548 corners |= 1 << CORNER_TOP_LEFT;
2549 if (bot_p)
2550 corners |= 1 << CORNER_BOTTOM_LEFT;
2552 if (right_p)
2554 x_fill_rectangle (f, bottom_right_gc, right_x + 1 - width, top_y,
2555 width, bottom_y + 1 - top_y);
2556 if (top_p)
2557 corners |= 1 << CORNER_TOP_RIGHT;
2558 if (bot_p)
2559 corners |= 1 << CORNER_BOTTOM_RIGHT;
2561 if (top_p)
2563 if (!right_p)
2564 x_fill_rectangle (f, top_left_gc, left_x, top_y,
2565 right_x + 1 - left_x, width);
2566 else
2567 x_fill_trapezoid_for_relief (f, top_left_gc, left_x, top_y,
2568 right_x + 1 - left_x, width, 1);
2570 if (bot_p)
2572 if (!left_p)
2573 x_fill_rectangle (f, bottom_right_gc, left_x, bottom_y + 1 - width,
2574 right_x + 1 - left_x, width);
2575 else
2576 x_fill_trapezoid_for_relief (f, bottom_right_gc,
2577 left_x, bottom_y + 1 - width,
2578 right_x + 1 - left_x, width, 0);
2580 if (left_p && width != 1)
2581 x_fill_rectangle (f, bottom_right_gc, left_x, top_y,
2582 1, bottom_y + 1 - top_y);
2583 if (top_p && width != 1)
2584 x_fill_rectangle (f, bottom_right_gc, left_x, top_y,
2585 right_x + 1 - left_x, 1);
2586 if (corners)
2588 XSetBackground (FRAME_X_DISPLAY (f), top_left_gc,
2589 FRAME_BACKGROUND_PIXEL (f));
2590 x_erase_corners_for_relief (f, top_left_gc, left_x, top_y,
2591 right_x - left_x + 1, bottom_y - top_y + 1,
2592 6, 1, corners);
2595 x_reset_clip_rectangles (f, top_left_gc);
2596 x_reset_clip_rectangles (f, bottom_right_gc);
2597 #else
2598 Display *dpy = FRAME_X_DISPLAY (f);
2599 Window window = FRAME_X_WINDOW (f);
2600 int i;
2601 GC gc;
2603 if (raised_p)
2604 gc = f->output_data.x->white_relief.gc;
2605 else
2606 gc = f->output_data.x->black_relief.gc;
2607 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2609 /* This code is more complicated than it has to be, because of two
2610 minor hacks to make the boxes look nicer: (i) if width > 1, draw
2611 the outermost line using the black relief. (ii) Omit the four
2612 corner pixels. */
2614 /* Top. */
2615 if (top_p)
2617 if (width == 1)
2618 XDrawLine (dpy, window, gc,
2619 left_x + left_p, top_y,
2620 right_x + !right_p, top_y);
2622 for (i = 1; i < width; ++i)
2623 XDrawLine (dpy, window, gc,
2624 left_x + i * left_p, top_y + i,
2625 right_x + 1 - i * right_p, top_y + i);
2628 /* Left. */
2629 if (left_p)
2631 if (width == 1)
2632 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
2634 XClearArea (dpy, window, left_x, top_y, 1, 1, False);
2635 XClearArea (dpy, window, left_x, bottom_y, 1, 1, False);
2637 for (i = (width > 1 ? 1 : 0); i < width; ++i)
2638 XDrawLine (dpy, window, gc,
2639 left_x + i, top_y + (i + 1) * top_p,
2640 left_x + i, bottom_y + 1 - (i + 1) * bot_p);
2643 XSetClipMask (dpy, gc, None);
2644 if (raised_p)
2645 gc = f->output_data.x->black_relief.gc;
2646 else
2647 gc = f->output_data.x->white_relief.gc;
2648 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2650 if (width > 1)
2652 /* Outermost top line. */
2653 if (top_p)
2654 XDrawLine (dpy, window, gc,
2655 left_x + left_p, top_y,
2656 right_x + !right_p, top_y);
2658 /* Outermost left line. */
2659 if (left_p)
2660 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
2663 /* Bottom. */
2664 if (bot_p)
2666 XDrawLine (dpy, window, gc,
2667 left_x + left_p, bottom_y,
2668 right_x + !right_p, bottom_y);
2669 for (i = 1; i < width; ++i)
2670 XDrawLine (dpy, window, gc,
2671 left_x + i * left_p, bottom_y - i,
2672 right_x + 1 - i * right_p, bottom_y - i);
2675 /* Right. */
2676 if (right_p)
2678 XClearArea (dpy, window, right_x, top_y, 1, 1, False);
2679 XClearArea (dpy, window, right_x, bottom_y, 1, 1, False);
2680 for (i = 0; i < width; ++i)
2681 XDrawLine (dpy, window, gc,
2682 right_x - i, top_y + (i + 1) * top_p,
2683 right_x - i, bottom_y + 1 - (i + 1) * bot_p);
2686 x_reset_clip_rectangles (f, gc);
2688 #endif
2692 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2693 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2694 draw, it must be >= 0. LEFT_P means draw a line on the
2695 left side of the rectangle. RIGHT_P means draw a line
2696 on the right side of the rectangle. CLIP_RECT is the clipping
2697 rectangle to use when drawing. */
2699 static void
2700 x_draw_box_rect (struct glyph_string *s,
2701 int left_x, int top_y, int right_x, int bottom_y, int width,
2702 bool left_p, bool right_p, XRectangle *clip_rect)
2704 XGCValues xgcv;
2706 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2707 XSetForeground (s->display, s->gc, s->face->box_color);
2708 x_set_clip_rectangles (s->f, s->gc, clip_rect, 1);
2710 /* Top. */
2711 x_fill_rectangle (s->f, s->gc,
2712 left_x, top_y, right_x - left_x + 1, width);
2714 /* Left. */
2715 if (left_p)
2716 x_fill_rectangle (s->f, s->gc,
2717 left_x, top_y, width, bottom_y - top_y + 1);
2719 /* Bottom. */
2720 x_fill_rectangle (s->f, s->gc,
2721 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2723 /* Right. */
2724 if (right_p)
2725 x_fill_rectangle (s->f, s->gc,
2726 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2728 XSetForeground (s->display, s->gc, xgcv.foreground);
2729 x_reset_clip_rectangles (s->f, s->gc);
2733 /* Draw a box around glyph string S. */
2735 static void
2736 x_draw_glyph_string_box (struct glyph_string *s)
2738 int width, left_x, right_x, top_y, bottom_y, last_x;
2739 bool raised_p, left_p, right_p;
2740 struct glyph *last_glyph;
2741 XRectangle clip_rect;
2743 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2744 ? WINDOW_RIGHT_EDGE_X (s->w)
2745 : window_box_right (s->w, s->area));
2747 /* The glyph that may have a right box line. */
2748 last_glyph = (s->cmp || s->img
2749 ? s->first_glyph
2750 : s->first_glyph + s->nchars - 1);
2752 width = eabs (s->face->box_line_width);
2753 raised_p = s->face->box == FACE_RAISED_BOX;
2754 left_x = s->x;
2755 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2756 ? last_x - 1
2757 : min (last_x, s->x + s->background_width) - 1);
2758 top_y = s->y;
2759 bottom_y = top_y + s->height - 1;
2761 left_p = (s->first_glyph->left_box_line_p
2762 || (s->hl == DRAW_MOUSE_FACE
2763 && (s->prev == NULL
2764 || s->prev->hl != s->hl)));
2765 right_p = (last_glyph->right_box_line_p
2766 || (s->hl == DRAW_MOUSE_FACE
2767 && (s->next == NULL
2768 || s->next->hl != s->hl)));
2770 get_glyph_string_clip_rect (s, &clip_rect);
2772 if (s->face->box == FACE_SIMPLE_BOX)
2773 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2774 left_p, right_p, &clip_rect);
2775 else
2777 x_setup_relief_colors (s);
2778 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2779 width, raised_p, true, true, left_p, right_p,
2780 &clip_rect);
2785 /* Draw foreground of image glyph string S. */
2787 static void
2788 x_draw_image_foreground (struct glyph_string *s)
2790 int x = s->x;
2791 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2793 /* If first glyph of S has a left box line, start drawing it to the
2794 right of that line. */
2795 if (s->face->box != FACE_NO_BOX
2796 && s->first_glyph->left_box_line_p
2797 && s->slice.x == 0)
2798 x += eabs (s->face->box_line_width);
2800 /* If there is a margin around the image, adjust x- and y-position
2801 by that margin. */
2802 if (s->slice.x == 0)
2803 x += s->img->hmargin;
2804 if (s->slice.y == 0)
2805 y += s->img->vmargin;
2807 if (s->img->pixmap)
2809 if (s->img->mask)
2811 /* We can't set both a clip mask and use XSetClipRectangles
2812 because the latter also sets a clip mask. We also can't
2813 trust on the shape extension to be available
2814 (XShapeCombineRegion). So, compute the rectangle to draw
2815 manually. */
2816 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2817 | GCFunction);
2818 XGCValues xgcv;
2819 XRectangle clip_rect, image_rect, r;
2821 xgcv.clip_mask = s->img->mask;
2822 xgcv.clip_x_origin = x;
2823 xgcv.clip_y_origin = y;
2824 xgcv.function = GXcopy;
2825 XChangeGC (s->display, s->gc, mask, &xgcv);
2827 get_glyph_string_clip_rect (s, &clip_rect);
2828 image_rect.x = x;
2829 image_rect.y = y;
2830 image_rect.width = s->slice.width;
2831 image_rect.height = s->slice.height;
2832 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2833 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2834 s->slice.x + r.x - x, s->slice.y + r.y - y,
2835 r.width, r.height, r.x, r.y);
2837 else
2839 XRectangle clip_rect, image_rect, r;
2841 get_glyph_string_clip_rect (s, &clip_rect);
2842 image_rect.x = x;
2843 image_rect.y = y;
2844 image_rect.width = s->slice.width;
2845 image_rect.height = s->slice.height;
2846 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2847 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2848 s->slice.x + r.x - x, s->slice.y + r.y - y,
2849 r.width, r.height, r.x, r.y);
2851 /* When the image has a mask, we can expect that at
2852 least part of a mouse highlight or a block cursor will
2853 be visible. If the image doesn't have a mask, make
2854 a block cursor visible by drawing a rectangle around
2855 the image. I believe it's looking better if we do
2856 nothing here for mouse-face. */
2857 if (s->hl == DRAW_CURSOR)
2859 int relief = eabs (s->img->relief);
2860 x_draw_rectangle (s->f, s->gc,
2861 x - relief, y - relief,
2862 s->slice.width + relief*2 - 1,
2863 s->slice.height + relief*2 - 1);
2867 else
2868 /* Draw a rectangle if image could not be loaded. */
2869 x_draw_rectangle (s->f, s->gc, x, y,
2870 s->slice.width - 1, s->slice.height - 1);
2874 /* Draw a relief around the image glyph string S. */
2876 static void
2877 x_draw_image_relief (struct glyph_string *s)
2879 int x1, y1, thick;
2880 bool raised_p, top_p, bot_p, left_p, right_p;
2881 int extra_x, extra_y;
2882 XRectangle r;
2883 int x = s->x;
2884 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2886 /* If first glyph of S has a left box line, start drawing it to the
2887 right of that line. */
2888 if (s->face->box != FACE_NO_BOX
2889 && s->first_glyph->left_box_line_p
2890 && s->slice.x == 0)
2891 x += eabs (s->face->box_line_width);
2893 /* If there is a margin around the image, adjust x- and y-position
2894 by that margin. */
2895 if (s->slice.x == 0)
2896 x += s->img->hmargin;
2897 if (s->slice.y == 0)
2898 y += s->img->vmargin;
2900 if (s->hl == DRAW_IMAGE_SUNKEN
2901 || s->hl == DRAW_IMAGE_RAISED)
2903 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2904 raised_p = s->hl == DRAW_IMAGE_RAISED;
2906 else
2908 thick = eabs (s->img->relief);
2909 raised_p = s->img->relief > 0;
2912 x1 = x + s->slice.width - 1;
2913 y1 = y + s->slice.height - 1;
2915 extra_x = extra_y = 0;
2916 if (s->face->id == TOOL_BAR_FACE_ID)
2918 if (CONSP (Vtool_bar_button_margin)
2919 && INTEGERP (XCAR (Vtool_bar_button_margin))
2920 && INTEGERP (XCDR (Vtool_bar_button_margin)))
2922 extra_x = XINT (XCAR (Vtool_bar_button_margin));
2923 extra_y = XINT (XCDR (Vtool_bar_button_margin));
2925 else if (INTEGERP (Vtool_bar_button_margin))
2926 extra_x = extra_y = XINT (Vtool_bar_button_margin);
2929 top_p = bot_p = left_p = right_p = false;
2931 if (s->slice.x == 0)
2932 x -= thick + extra_x, left_p = true;
2933 if (s->slice.y == 0)
2934 y -= thick + extra_y, top_p = true;
2935 if (s->slice.x + s->slice.width == s->img->width)
2936 x1 += thick + extra_x, right_p = true;
2937 if (s->slice.y + s->slice.height == s->img->height)
2938 y1 += thick + extra_y, bot_p = true;
2940 x_setup_relief_colors (s);
2941 get_glyph_string_clip_rect (s, &r);
2942 x_draw_relief_rect (s->f, x, y, x1, y1, thick, raised_p,
2943 top_p, bot_p, left_p, right_p, &r);
2947 /* Draw the foreground of image glyph string S to PIXMAP. */
2949 static void
2950 x_draw_image_foreground_1 (struct glyph_string *s, Pixmap pixmap)
2952 int x = 0;
2953 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2955 /* If first glyph of S has a left box line, start drawing it to the
2956 right of that line. */
2957 if (s->face->box != FACE_NO_BOX
2958 && s->first_glyph->left_box_line_p
2959 && s->slice.x == 0)
2960 x += eabs (s->face->box_line_width);
2962 /* If there is a margin around the image, adjust x- and y-position
2963 by that margin. */
2964 if (s->slice.x == 0)
2965 x += s->img->hmargin;
2966 if (s->slice.y == 0)
2967 y += s->img->vmargin;
2969 if (s->img->pixmap)
2971 if (s->img->mask)
2973 /* We can't set both a clip mask and use XSetClipRectangles
2974 because the latter also sets a clip mask. We also can't
2975 trust on the shape extension to be available
2976 (XShapeCombineRegion). So, compute the rectangle to draw
2977 manually. */
2978 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2979 | GCFunction);
2980 XGCValues xgcv;
2982 xgcv.clip_mask = s->img->mask;
2983 xgcv.clip_x_origin = x - s->slice.x;
2984 xgcv.clip_y_origin = y - s->slice.y;
2985 xgcv.function = GXcopy;
2986 XChangeGC (s->display, s->gc, mask, &xgcv);
2988 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2989 s->slice.x, s->slice.y,
2990 s->slice.width, s->slice.height, x, y);
2991 XSetClipMask (s->display, s->gc, None);
2993 else
2995 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2996 s->slice.x, s->slice.y,
2997 s->slice.width, s->slice.height, x, y);
2999 /* When the image has a mask, we can expect that at
3000 least part of a mouse highlight or a block cursor will
3001 be visible. If the image doesn't have a mask, make
3002 a block cursor visible by drawing a rectangle around
3003 the image. I believe it's looking better if we do
3004 nothing here for mouse-face. */
3005 if (s->hl == DRAW_CURSOR)
3007 int r = eabs (s->img->relief);
3008 x_draw_rectangle (s->f, s->gc, x - r, y - r,
3009 s->slice.width + r*2 - 1,
3010 s->slice.height + r*2 - 1);
3014 else
3015 /* Draw a rectangle if image could not be loaded. */
3016 x_draw_rectangle (s->f, s->gc, x, y,
3017 s->slice.width - 1, s->slice.height - 1);
3021 /* Draw part of the background of glyph string S. X, Y, W, and H
3022 give the rectangle to draw. */
3024 static void
3025 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
3027 if (s->stippled_p)
3029 /* Fill background with a stipple pattern. */
3030 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3031 x_fill_rectangle (s->f, s->gc, x, y, w, h);
3032 XSetFillStyle (s->display, s->gc, FillSolid);
3034 else
3035 x_clear_glyph_string_rect (s, x, y, w, h);
3039 /* Draw image glyph string S.
3041 s->y
3042 s->x +-------------------------
3043 | s->face->box
3045 | +-------------------------
3046 | | s->img->margin
3048 | | +-------------------
3049 | | | the image
3053 static void
3054 x_draw_image_glyph_string (struct glyph_string *s)
3056 int box_line_hwidth = eabs (s->face->box_line_width);
3057 int box_line_vwidth = max (s->face->box_line_width, 0);
3058 int height;
3059 Pixmap pixmap = None;
3061 height = s->height;
3062 if (s->slice.y == 0)
3063 height -= box_line_vwidth;
3064 if (s->slice.y + s->slice.height >= s->img->height)
3065 height -= box_line_vwidth;
3067 /* Fill background with face under the image. Do it only if row is
3068 taller than image or if image has a clip mask to reduce
3069 flickering. */
3070 s->stippled_p = s->face->stipple != 0;
3071 if (height > s->slice.height
3072 || s->img->hmargin
3073 || s->img->vmargin
3074 || s->img->mask
3075 || s->img->pixmap == 0
3076 || s->width != s->background_width)
3078 if (s->img->mask)
3080 /* Create a pixmap as large as the glyph string. Fill it
3081 with the background color. Copy the image to it, using
3082 its mask. Copy the temporary pixmap to the display. */
3083 Screen *screen = FRAME_X_SCREEN (s->f);
3084 int depth = DefaultDepthOfScreen (screen);
3086 /* Create a pixmap as large as the glyph string. */
3087 pixmap = XCreatePixmap (s->display, s->window,
3088 s->background_width,
3089 s->height, depth);
3091 /* Don't clip in the following because we're working on the
3092 pixmap. */
3093 XSetClipMask (s->display, s->gc, None);
3095 /* Fill the pixmap with the background color/stipple. */
3096 if (s->stippled_p)
3098 /* Fill background with a stipple pattern. */
3099 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3100 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
3101 XFillRectangle (s->display, pixmap, s->gc,
3102 0, 0, s->background_width, s->height);
3103 XSetFillStyle (s->display, s->gc, FillSolid);
3104 XSetTSOrigin (s->display, s->gc, 0, 0);
3106 else
3108 XGCValues xgcv;
3109 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3110 &xgcv);
3111 XSetForeground (s->display, s->gc, xgcv.background);
3112 XFillRectangle (s->display, pixmap, s->gc,
3113 0, 0, s->background_width, s->height);
3114 XSetForeground (s->display, s->gc, xgcv.foreground);
3117 else
3119 int x = s->x;
3120 int y = s->y;
3121 int width = s->background_width;
3123 if (s->first_glyph->left_box_line_p
3124 && s->slice.x == 0)
3126 x += box_line_hwidth;
3127 width -= box_line_hwidth;
3130 if (s->slice.y == 0)
3131 y += box_line_vwidth;
3133 x_draw_glyph_string_bg_rect (s, x, y, width, height);
3136 s->background_filled_p = true;
3139 /* Draw the foreground. */
3140 #ifdef USE_CAIRO
3141 if (s->img->cr_data)
3143 cairo_t *cr = x_begin_cr_clip (s->f, s->gc);
3145 int x = s->x + s->img->hmargin;
3146 int y = s->y + s->img->vmargin;
3147 int width = s->background_width;
3149 cairo_set_source_surface (cr, s->img->cr_data,
3150 x - s->slice.x,
3151 y - s->slice.y);
3152 cairo_rectangle (cr, x, y, width, height);
3153 cairo_fill (cr);
3154 x_end_cr_clip (s->f);
3156 else
3157 #endif
3158 if (pixmap != None)
3160 x_draw_image_foreground_1 (s, pixmap);
3161 x_set_glyph_string_clipping (s);
3162 XCopyArea (s->display, pixmap, s->window, s->gc,
3163 0, 0, s->background_width, s->height, s->x, s->y);
3164 XFreePixmap (s->display, pixmap);
3166 else
3167 x_draw_image_foreground (s);
3169 /* If we must draw a relief around the image, do it. */
3170 if (s->img->relief
3171 || s->hl == DRAW_IMAGE_RAISED
3172 || s->hl == DRAW_IMAGE_SUNKEN)
3173 x_draw_image_relief (s);
3177 /* Draw stretch glyph string S. */
3179 static void
3180 x_draw_stretch_glyph_string (struct glyph_string *s)
3182 eassert (s->first_glyph->type == STRETCH_GLYPH);
3184 if (s->hl == DRAW_CURSOR
3185 && !x_stretch_cursor_p)
3187 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
3188 wide as the stretch glyph. */
3189 int width, background_width = s->background_width;
3190 int x = s->x;
3192 if (!s->row->reversed_p)
3194 int left_x = window_box_left_offset (s->w, TEXT_AREA);
3196 if (x < left_x)
3198 background_width -= left_x - x;
3199 x = left_x;
3202 else
3204 /* In R2L rows, draw the cursor on the right edge of the
3205 stretch glyph. */
3206 int right_x = window_box_right (s->w, TEXT_AREA);
3208 if (x + background_width > right_x)
3209 background_width -= x - right_x;
3210 x += background_width;
3212 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
3213 if (s->row->reversed_p)
3214 x -= width;
3216 /* Draw cursor. */
3217 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
3219 /* Clear rest using the GC of the original non-cursor face. */
3220 if (width < background_width)
3222 int y = s->y;
3223 int w = background_width - width, h = s->height;
3224 XRectangle r;
3225 GC gc;
3227 if (!s->row->reversed_p)
3228 x += width;
3229 else
3230 x = s->x;
3231 if (s->row->mouse_face_p
3232 && cursor_in_mouse_face_p (s->w))
3234 x_set_mouse_face_gc (s);
3235 gc = s->gc;
3237 else
3238 gc = s->face->gc;
3240 get_glyph_string_clip_rect (s, &r);
3241 x_set_clip_rectangles (s->f, gc, &r, 1);
3243 if (s->face->stipple)
3245 /* Fill background with a stipple pattern. */
3246 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3247 x_fill_rectangle (s->f, gc, x, y, w, h);
3248 XSetFillStyle (s->display, gc, FillSolid);
3250 else
3252 XGCValues xgcv;
3253 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3254 XSetForeground (s->display, gc, xgcv.background);
3255 x_fill_rectangle (s->f, gc, x, y, w, h);
3256 XSetForeground (s->display, gc, xgcv.foreground);
3259 x_reset_clip_rectangles (s->f, gc);
3262 else if (!s->background_filled_p)
3264 int background_width = s->background_width;
3265 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
3267 /* Don't draw into left margin, fringe or scrollbar area
3268 except for header line and mode line. */
3269 if (x < left_x && !s->row->mode_line_p)
3271 background_width -= left_x - x;
3272 x = left_x;
3274 if (background_width > 0)
3275 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
3278 s->background_filled_p = true;
3282 Draw a wavy line under S. The wave fills wave_height pixels from y0.
3284 x0 wave_length = 2
3286 y0 * * * * *
3287 |* * * * * * * * *
3288 wave_height = 3 | * * * *
3292 static void
3293 x_draw_underwave (struct glyph_string *s)
3295 int wave_height = 3, wave_length = 2;
3296 #ifdef USE_CAIRO
3297 x_draw_horizontal_wave (s->f, s->gc, s->x, s->ybase - wave_height + 3,
3298 s->width, wave_height, wave_length);
3299 #else /* not USE_CAIRO */
3300 int dx, dy, x0, y0, width, x1, y1, x2, y2, xmax;
3301 bool odd;
3302 XRectangle wave_clip, string_clip, final_clip;
3304 dx = wave_length;
3305 dy = wave_height - 1;
3306 x0 = s->x;
3307 y0 = s->ybase - wave_height + 3;
3308 width = s->width;
3309 xmax = x0 + width;
3311 /* Find and set clipping rectangle */
3313 wave_clip.x = x0;
3314 wave_clip.y = y0;
3315 wave_clip.width = width;
3316 wave_clip.height = wave_height;
3317 get_glyph_string_clip_rect (s, &string_clip);
3319 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
3320 return;
3322 XSetClipRectangles (s->display, s->gc, 0, 0, &final_clip, 1, Unsorted);
3324 /* Draw the waves */
3326 x1 = x0 - (x0 % dx);
3327 x2 = x1 + dx;
3328 odd = (x1 / dx) & 1;
3329 y1 = y2 = y0;
3331 if (odd)
3332 y1 += dy;
3333 else
3334 y2 += dy;
3336 if (INT_MAX - dx < xmax)
3337 emacs_abort ();
3339 while (x1 <= xmax)
3341 XDrawLine (s->display, s->window, s->gc, x1, y1, x2, y2);
3342 x1 = x2, y1 = y2;
3343 x2 += dx, y2 = y0 + odd*dy;
3344 odd = !odd;
3347 /* Restore previous clipping rectangle(s) */
3348 XSetClipRectangles (s->display, s->gc, 0, 0, s->clip, s->num_clips, Unsorted);
3349 #endif /* not USE_CAIRO */
3353 /* Draw glyph string S. */
3355 static void
3356 x_draw_glyph_string (struct glyph_string *s)
3358 bool relief_drawn_p = false;
3360 /* If S draws into the background of its successors, draw the
3361 background of the successors first so that S can draw into it.
3362 This makes S->next use XDrawString instead of XDrawImageString. */
3363 if (s->next && s->right_overhang && !s->for_overlaps)
3365 int width;
3366 struct glyph_string *next;
3368 for (width = 0, next = s->next;
3369 next && width < s->right_overhang;
3370 width += next->width, next = next->next)
3371 if (next->first_glyph->type != IMAGE_GLYPH)
3373 x_set_glyph_string_gc (next);
3374 x_set_glyph_string_clipping (next);
3375 if (next->first_glyph->type == STRETCH_GLYPH)
3376 x_draw_stretch_glyph_string (next);
3377 else
3378 x_draw_glyph_string_background (next, true);
3379 next->num_clips = 0;
3383 /* Set up S->gc, set clipping and draw S. */
3384 x_set_glyph_string_gc (s);
3386 /* Draw relief (if any) in advance for char/composition so that the
3387 glyph string can be drawn over it. */
3388 if (!s->for_overlaps
3389 && s->face->box != FACE_NO_BOX
3390 && (s->first_glyph->type == CHAR_GLYPH
3391 || s->first_glyph->type == COMPOSITE_GLYPH))
3394 x_set_glyph_string_clipping (s);
3395 x_draw_glyph_string_background (s, true);
3396 x_draw_glyph_string_box (s);
3397 x_set_glyph_string_clipping (s);
3398 relief_drawn_p = true;
3400 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
3401 && !s->clip_tail
3402 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
3403 || (s->next && s->next->hl != s->hl && s->right_overhang)))
3404 /* We must clip just this glyph. left_overhang part has already
3405 drawn when s->prev was drawn, and right_overhang part will be
3406 drawn later when s->next is drawn. */
3407 x_set_glyph_string_clipping_exactly (s, s);
3408 else
3409 x_set_glyph_string_clipping (s);
3411 switch (s->first_glyph->type)
3413 case IMAGE_GLYPH:
3414 x_draw_image_glyph_string (s);
3415 break;
3417 case STRETCH_GLYPH:
3418 x_draw_stretch_glyph_string (s);
3419 break;
3421 case CHAR_GLYPH:
3422 if (s->for_overlaps)
3423 s->background_filled_p = true;
3424 else
3425 x_draw_glyph_string_background (s, false);
3426 x_draw_glyph_string_foreground (s);
3427 break;
3429 case COMPOSITE_GLYPH:
3430 if (s->for_overlaps || (s->cmp_from > 0
3431 && ! s->first_glyph->u.cmp.automatic))
3432 s->background_filled_p = true;
3433 else
3434 x_draw_glyph_string_background (s, true);
3435 x_draw_composite_glyph_string_foreground (s);
3436 break;
3438 case GLYPHLESS_GLYPH:
3439 if (s->for_overlaps)
3440 s->background_filled_p = true;
3441 else
3442 x_draw_glyph_string_background (s, true);
3443 x_draw_glyphless_glyph_string_foreground (s);
3444 break;
3446 default:
3447 emacs_abort ();
3450 if (!s->for_overlaps)
3452 /* Draw underline. */
3453 if (s->face->underline_p)
3455 if (s->face->underline_type == FACE_UNDER_WAVE)
3457 if (s->face->underline_defaulted_p)
3458 x_draw_underwave (s);
3459 else
3461 XGCValues xgcv;
3462 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3463 XSetForeground (s->display, s->gc, s->face->underline_color);
3464 x_draw_underwave (s);
3465 XSetForeground (s->display, s->gc, xgcv.foreground);
3468 else if (s->face->underline_type == FACE_UNDER_LINE)
3470 unsigned long thickness, position;
3471 int y;
3473 if (s->prev && s->prev->face->underline_p
3474 && s->prev->face->underline_type == FACE_UNDER_LINE)
3476 /* We use the same underline style as the previous one. */
3477 thickness = s->prev->underline_thickness;
3478 position = s->prev->underline_position;
3480 else
3482 /* Get the underline thickness. Default is 1 pixel. */
3483 if (s->font && s->font->underline_thickness > 0)
3484 thickness = s->font->underline_thickness;
3485 else
3486 thickness = 1;
3487 if (x_underline_at_descent_line)
3488 position = (s->height - thickness) - (s->ybase - s->y);
3489 else
3491 /* Get the underline position. This is the recommended
3492 vertical offset in pixels from the baseline to the top of
3493 the underline. This is a signed value according to the
3494 specs, and its default is
3496 ROUND ((maximum descent) / 2), with
3497 ROUND(x) = floor (x + 0.5) */
3499 if (x_use_underline_position_properties
3500 && s->font && s->font->underline_position >= 0)
3501 position = s->font->underline_position;
3502 else if (s->font)
3503 position = (s->font->descent + 1) / 2;
3504 else
3505 position = underline_minimum_offset;
3507 position = max (position, underline_minimum_offset);
3509 /* Check the sanity of thickness and position. We should
3510 avoid drawing underline out of the current line area. */
3511 if (s->y + s->height <= s->ybase + position)
3512 position = (s->height - 1) - (s->ybase - s->y);
3513 if (s->y + s->height < s->ybase + position + thickness)
3514 thickness = (s->y + s->height) - (s->ybase + position);
3515 s->underline_thickness = thickness;
3516 s->underline_position = position;
3517 y = s->ybase + position;
3518 if (s->face->underline_defaulted_p)
3519 x_fill_rectangle (s->f, s->gc,
3520 s->x, y, s->width, thickness);
3521 else
3523 XGCValues xgcv;
3524 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3525 XSetForeground (s->display, s->gc, s->face->underline_color);
3526 x_fill_rectangle (s->f, s->gc,
3527 s->x, y, s->width, thickness);
3528 XSetForeground (s->display, s->gc, xgcv.foreground);
3532 /* Draw overline. */
3533 if (s->face->overline_p)
3535 unsigned long dy = 0, h = 1;
3537 if (s->face->overline_color_defaulted_p)
3538 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3539 s->width, h);
3540 else
3542 XGCValues xgcv;
3543 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3544 XSetForeground (s->display, s->gc, s->face->overline_color);
3545 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3546 s->width, h);
3547 XSetForeground (s->display, s->gc, xgcv.foreground);
3551 /* Draw strike-through. */
3552 if (s->face->strike_through_p)
3554 unsigned long h = 1;
3555 unsigned long dy = (s->height - h) / 2;
3557 if (s->face->strike_through_color_defaulted_p)
3558 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3559 s->width, h);
3560 else
3562 XGCValues xgcv;
3563 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3564 XSetForeground (s->display, s->gc, s->face->strike_through_color);
3565 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3566 s->width, h);
3567 XSetForeground (s->display, s->gc, xgcv.foreground);
3571 /* Draw relief if not yet drawn. */
3572 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
3573 x_draw_glyph_string_box (s);
3575 if (s->prev)
3577 struct glyph_string *prev;
3579 for (prev = s->prev; prev; prev = prev->prev)
3580 if (prev->hl != s->hl
3581 && prev->x + prev->width + prev->right_overhang > s->x)
3583 /* As prev was drawn while clipped to its own area, we
3584 must draw the right_overhang part using s->hl now. */
3585 enum draw_glyphs_face save = prev->hl;
3587 prev->hl = s->hl;
3588 x_set_glyph_string_gc (prev);
3589 x_set_glyph_string_clipping_exactly (s, prev);
3590 if (prev->first_glyph->type == CHAR_GLYPH)
3591 x_draw_glyph_string_foreground (prev);
3592 else
3593 x_draw_composite_glyph_string_foreground (prev);
3594 x_reset_clip_rectangles (prev->f, prev->gc);
3595 prev->hl = save;
3596 prev->num_clips = 0;
3600 if (s->next)
3602 struct glyph_string *next;
3604 for (next = s->next; next; next = next->next)
3605 if (next->hl != s->hl
3606 && next->x - next->left_overhang < s->x + s->width)
3608 /* As next will be drawn while clipped to its own area,
3609 we must draw the left_overhang part using s->hl now. */
3610 enum draw_glyphs_face save = next->hl;
3612 next->hl = s->hl;
3613 x_set_glyph_string_gc (next);
3614 x_set_glyph_string_clipping_exactly (s, next);
3615 if (next->first_glyph->type == CHAR_GLYPH)
3616 x_draw_glyph_string_foreground (next);
3617 else
3618 x_draw_composite_glyph_string_foreground (next);
3619 x_reset_clip_rectangles (next->f, next->gc);
3620 next->hl = save;
3621 next->num_clips = 0;
3622 next->clip_head = s->next;
3627 /* Reset clipping. */
3628 x_reset_clip_rectangles (s->f, s->gc);
3629 s->num_clips = 0;
3632 /* Shift display to make room for inserted glyphs. */
3634 static void
3635 x_shift_glyphs_for_insert (struct frame *f, int x, int y, int width, int height, int shift_by)
3637 /* Never called on a GUI frame, see
3638 http://lists.gnu.org/archive/html/emacs-devel/2015-05/msg00456.html
3640 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3641 f->output_data.x->normal_gc,
3642 x, y, width, height,
3643 x + shift_by, y);
3646 /* Delete N glyphs at the nominal cursor position. Not implemented
3647 for X frames. */
3649 static void
3650 x_delete_glyphs (struct frame *f, register int n)
3652 emacs_abort ();
3656 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
3657 If they are <= 0, this is probably an error. */
3659 static void
3660 x_clear_area1 (Display *dpy, Window window,
3661 int x, int y, int width, int height, int exposures)
3663 eassert (width > 0 && height > 0);
3664 XClearArea (dpy, window, x, y, width, height, exposures);
3668 void
3669 x_clear_area (struct frame *f, int x, int y, int width, int height)
3671 #ifdef USE_CAIRO
3672 cairo_t *cr;
3674 eassert (width > 0 && height > 0);
3676 cr = x_begin_cr_clip (f, NULL);
3677 x_set_cr_source_with_gc_background (f, f->output_data.x->normal_gc);
3678 cairo_rectangle (cr, x, y, width, height);
3679 cairo_fill (cr);
3680 x_end_cr_clip (f);
3681 #else
3682 x_clear_area1 (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3683 x, y, width, height, False);
3684 #endif
3688 /* Clear an entire frame. */
3690 static void
3691 x_clear_frame (struct frame *f)
3693 /* Clearing the frame will erase any cursor, so mark them all as no
3694 longer visible. */
3695 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
3697 block_input ();
3699 x_clear_window (f);
3701 /* We have to clear the scroll bars. If we have changed colors or
3702 something like that, then they should be notified. */
3703 x_scroll_bar_clear (f);
3705 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3706 /* Make sure scroll bars are redrawn. As they aren't redrawn by
3707 redisplay, do it here. */
3708 if (FRAME_GTK_WIDGET (f))
3709 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
3710 #endif
3712 XFlush (FRAME_X_DISPLAY (f));
3714 unblock_input ();
3717 /* RIF: Show hourglass cursor on frame F. */
3719 static void
3720 x_show_hourglass (struct frame *f)
3722 Display *dpy = FRAME_X_DISPLAY (f);
3724 if (dpy)
3726 struct x_output *x = FRAME_X_OUTPUT (f);
3727 #ifdef USE_X_TOOLKIT
3728 if (x->widget)
3729 #else
3730 if (FRAME_OUTER_WINDOW (f))
3731 #endif
3733 x->hourglass_p = true;
3735 if (!x->hourglass_window)
3737 unsigned long mask = CWCursor;
3738 XSetWindowAttributes attrs;
3739 #ifdef USE_GTK
3740 Window parent = FRAME_X_WINDOW (f);
3741 #else
3742 Window parent = FRAME_OUTER_WINDOW (f);
3743 #endif
3744 attrs.cursor = x->hourglass_cursor;
3746 x->hourglass_window = XCreateWindow
3747 (dpy, parent, 0, 0, 32000, 32000, 0, 0,
3748 InputOnly, CopyFromParent, mask, &attrs);
3751 XMapRaised (dpy, x->hourglass_window);
3752 XFlush (dpy);
3757 /* RIF: Cancel hourglass cursor on frame F. */
3759 static void
3760 x_hide_hourglass (struct frame *f)
3762 struct x_output *x = FRAME_X_OUTPUT (f);
3764 /* Watch out for newly created frames. */
3765 if (x->hourglass_window)
3767 XUnmapWindow (FRAME_X_DISPLAY (f), x->hourglass_window);
3768 /* Sync here because XTread_socket looks at the
3769 hourglass_p flag that is reset to zero below. */
3770 XSync (FRAME_X_DISPLAY (f), False);
3771 x->hourglass_p = false;
3775 /* Invert the middle quarter of the frame for .15 sec. */
3777 static void
3778 XTflash (struct frame *f)
3780 block_input ();
3783 #ifdef USE_GTK
3784 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
3785 when the scroll bars and the edit widget share the same X window. */
3786 GdkWindow *window = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
3787 #ifdef HAVE_GTK3
3788 cairo_t *cr = gdk_cairo_create (window);
3789 cairo_set_source_rgb (cr, 1, 1, 1);
3790 cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
3791 #define XFillRectangle(d, win, gc, x, y, w, h) \
3792 do { \
3793 cairo_rectangle (cr, x, y, w, h); \
3794 cairo_fill (cr); \
3796 while (false)
3797 #else /* ! HAVE_GTK3 */
3798 GdkGCValues vals;
3799 GdkGC *gc;
3800 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
3801 ^ FRAME_BACKGROUND_PIXEL (f));
3802 vals.function = GDK_XOR;
3803 gc = gdk_gc_new_with_values (window,
3804 &vals, GDK_GC_FUNCTION | GDK_GC_FOREGROUND);
3805 #define XFillRectangle(d, win, gc, x, y, w, h) \
3806 gdk_draw_rectangle (window, gc, true, x, y, w, h)
3807 #endif /* ! HAVE_GTK3 */
3808 #else /* ! USE_GTK */
3809 GC gc;
3811 /* Create a GC that will use the GXxor function to flip foreground
3812 pixels into background pixels. */
3814 XGCValues values;
3816 values.function = GXxor;
3817 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3818 ^ FRAME_BACKGROUND_PIXEL (f));
3820 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3821 GCFunction | GCForeground, &values);
3823 #endif
3825 /* Get the height not including a menu bar widget. */
3826 int height = FRAME_PIXEL_HEIGHT (f);
3827 /* Height of each line to flash. */
3828 int flash_height = FRAME_LINE_HEIGHT (f);
3829 /* These will be the left and right margins of the rectangles. */
3830 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3831 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3832 int width = flash_right - flash_left;
3834 /* If window is tall, flash top and bottom line. */
3835 if (height > 3 * FRAME_LINE_HEIGHT (f))
3837 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3838 flash_left,
3839 (FRAME_INTERNAL_BORDER_WIDTH (f)
3840 + FRAME_TOP_MARGIN_HEIGHT (f)),
3841 width, flash_height);
3842 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3843 flash_left,
3844 (height - flash_height
3845 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3846 width, flash_height);
3849 else
3850 /* If it is short, flash it all. */
3851 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3852 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3853 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3855 x_flush (f);
3858 struct timespec delay = make_timespec (0, 150 * 1000 * 1000);
3859 struct timespec wakeup = timespec_add (current_timespec (), delay);
3861 /* Keep waiting until past the time wakeup or any input gets
3862 available. */
3863 while (! detect_input_pending ())
3865 struct timespec current = current_timespec ();
3866 struct timespec timeout;
3868 /* Break if result would not be positive. */
3869 if (timespec_cmp (wakeup, current) <= 0)
3870 break;
3872 /* How long `select' should wait. */
3873 timeout = make_timespec (0, 10 * 1000 * 1000);
3875 /* Try to wait that long--but we might wake up sooner. */
3876 pselect (0, NULL, NULL, NULL, &timeout, NULL);
3880 /* If window is tall, flash top and bottom line. */
3881 if (height > 3 * FRAME_LINE_HEIGHT (f))
3883 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3884 flash_left,
3885 (FRAME_INTERNAL_BORDER_WIDTH (f)
3886 + FRAME_TOP_MARGIN_HEIGHT (f)),
3887 width, flash_height);
3888 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3889 flash_left,
3890 (height - flash_height
3891 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3892 width, flash_height);
3894 else
3895 /* If it is short, flash it all. */
3896 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3897 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3898 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3900 #ifdef USE_GTK
3901 #ifdef HAVE_GTK3
3902 cairo_destroy (cr);
3903 #else
3904 g_object_unref (G_OBJECT (gc));
3905 #endif
3906 #undef XFillRectangle
3907 #else
3908 XFreeGC (FRAME_X_DISPLAY (f), gc);
3909 #endif
3910 x_flush (f);
3914 unblock_input ();
3918 static void
3919 XTtoggle_invisible_pointer (struct frame *f, bool invisible)
3921 block_input ();
3922 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, invisible);
3923 unblock_input ();
3927 /* Make audible bell. */
3929 static void
3930 XTring_bell (struct frame *f)
3932 if (FRAME_X_DISPLAY (f))
3934 if (visible_bell)
3935 XTflash (f);
3936 else
3938 block_input ();
3939 #ifdef HAVE_XKB
3940 XkbBell (FRAME_X_DISPLAY (f), None, 0, None);
3941 #else
3942 XBell (FRAME_X_DISPLAY (f), 0);
3943 #endif
3944 XFlush (FRAME_X_DISPLAY (f));
3945 unblock_input ();
3950 /***********************************************************************
3951 Line Dance
3952 ***********************************************************************/
3954 /* Perform an insert-lines or delete-lines operation, inserting N
3955 lines or deleting -N lines at vertical position VPOS. */
3957 static void
3958 x_ins_del_lines (struct frame *f, int vpos, int n)
3960 emacs_abort ();
3964 /* Scroll part of the display as described by RUN. */
3966 static void
3967 x_scroll_run (struct window *w, struct run *run)
3969 struct frame *f = XFRAME (w->frame);
3970 int x, y, width, height, from_y, to_y, bottom_y;
3972 /* Get frame-relative bounding box of the text display area of W,
3973 without mode lines. Include in this box the left and right
3974 fringe of W. */
3975 window_box (w, ANY_AREA, &x, &y, &width, &height);
3977 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3978 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3979 bottom_y = y + height;
3981 if (to_y < from_y)
3983 /* Scrolling up. Make sure we don't copy part of the mode
3984 line at the bottom. */
3985 if (from_y + run->height > bottom_y)
3986 height = bottom_y - from_y;
3987 else
3988 height = run->height;
3990 else
3992 /* Scrolling down. Make sure we don't copy over the mode line.
3993 at the bottom. */
3994 if (to_y + run->height > bottom_y)
3995 height = bottom_y - to_y;
3996 else
3997 height = run->height;
4000 block_input ();
4002 /* Cursor off. Will be switched on again in x_update_window_end. */
4003 x_clear_cursor (w);
4005 #ifdef USE_CAIRO
4006 SET_FRAME_GARBAGED (f);
4007 #else
4008 XCopyArea (FRAME_X_DISPLAY (f),
4009 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4010 f->output_data.x->normal_gc,
4011 x, from_y,
4012 width, height,
4013 x, to_y);
4014 #endif
4016 unblock_input ();
4021 /***********************************************************************
4022 Exposure Events
4023 ***********************************************************************/
4026 static void
4027 frame_highlight (struct frame *f)
4029 /* We used to only do this if Vx_no_window_manager was non-nil, but
4030 the ICCCM (section 4.1.6) says that the window's border pixmap
4031 and border pixel are window attributes which are "private to the
4032 client", so we can always change it to whatever we want. */
4033 block_input ();
4034 /* I recently started to get errors in this XSetWindowBorder, depending on
4035 the window-manager in use, tho something more is at play since I've been
4036 using that same window-manager binary for ever. Let's not crash just
4037 because of this (bug#9310). */
4038 x_catch_errors (FRAME_X_DISPLAY (f));
4039 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4040 f->output_data.x->border_pixel);
4041 x_uncatch_errors ();
4042 unblock_input ();
4043 x_update_cursor (f, true);
4044 x_set_frame_alpha (f);
4047 static void
4048 frame_unhighlight (struct frame *f)
4050 /* We used to only do this if Vx_no_window_manager was non-nil, but
4051 the ICCCM (section 4.1.6) says that the window's border pixmap
4052 and border pixel are window attributes which are "private to the
4053 client", so we can always change it to whatever we want. */
4054 block_input ();
4055 /* Same as above for XSetWindowBorder (bug#9310). */
4056 x_catch_errors (FRAME_X_DISPLAY (f));
4057 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4058 f->output_data.x->border_tile);
4059 x_uncatch_errors ();
4060 unblock_input ();
4061 x_update_cursor (f, true);
4062 x_set_frame_alpha (f);
4065 /* The focus has changed. Update the frames as necessary to reflect
4066 the new situation. Note that we can't change the selected frame
4067 here, because the Lisp code we are interrupting might become confused.
4068 Each event gets marked with the frame in which it occurred, so the
4069 Lisp code can tell when the switch took place by examining the events. */
4071 static void
4072 x_new_focus_frame (struct x_display_info *dpyinfo, struct frame *frame)
4074 struct frame *old_focus = dpyinfo->x_focus_frame;
4076 if (frame != dpyinfo->x_focus_frame)
4078 /* Set this before calling other routines, so that they see
4079 the correct value of x_focus_frame. */
4080 dpyinfo->x_focus_frame = frame;
4082 if (old_focus && old_focus->auto_lower)
4083 x_lower_frame (old_focus);
4085 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
4086 dpyinfo->x_pending_autoraise_frame = dpyinfo->x_focus_frame;
4087 else
4088 dpyinfo->x_pending_autoraise_frame = NULL;
4091 x_frame_rehighlight (dpyinfo);
4094 /* Handle FocusIn and FocusOut state changes for FRAME.
4095 If FRAME has focus and there exists more than one frame, puts
4096 a FOCUS_IN_EVENT into *BUFP. */
4098 static void
4099 x_focus_changed (int type, int state, struct x_display_info *dpyinfo, struct frame *frame, struct input_event *bufp)
4101 if (type == FocusIn)
4103 if (dpyinfo->x_focus_event_frame != frame)
4105 x_new_focus_frame (dpyinfo, frame);
4106 dpyinfo->x_focus_event_frame = frame;
4108 /* Don't stop displaying the initial startup message
4109 for a switch-frame event we don't need. */
4110 /* When run as a daemon, Vterminal_frame is always NIL. */
4111 bufp->arg = (((NILP (Vterminal_frame)
4112 || ! FRAME_X_P (XFRAME (Vterminal_frame))
4113 || EQ (Fdaemonp (), Qt))
4114 && CONSP (Vframe_list)
4115 && !NILP (XCDR (Vframe_list)))
4116 ? Qt : Qnil);
4117 bufp->kind = FOCUS_IN_EVENT;
4118 XSETFRAME (bufp->frame_or_window, frame);
4121 frame->output_data.x->focus_state |= state;
4123 #ifdef HAVE_X_I18N
4124 if (FRAME_XIC (frame))
4125 XSetICFocus (FRAME_XIC (frame));
4126 #endif
4128 else if (type == FocusOut)
4130 frame->output_data.x->focus_state &= ~state;
4132 if (dpyinfo->x_focus_event_frame == frame)
4134 dpyinfo->x_focus_event_frame = 0;
4135 x_new_focus_frame (dpyinfo, 0);
4137 bufp->kind = FOCUS_OUT_EVENT;
4138 XSETFRAME (bufp->frame_or_window, frame);
4141 #ifdef HAVE_X_I18N
4142 if (FRAME_XIC (frame))
4143 XUnsetICFocus (FRAME_XIC (frame));
4144 #endif
4145 if (frame->pointer_invisible)
4146 XTtoggle_invisible_pointer (frame, false);
4150 /* Return the Emacs frame-object corresponding to an X window.
4151 It could be the frame's main window or an icon window. */
4153 static struct frame *
4154 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4156 Lisp_Object tail, frame;
4157 struct frame *f;
4159 if (wdesc == None)
4160 return NULL;
4162 FOR_EACH_FRAME (tail, frame)
4164 f = XFRAME (frame);
4165 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4166 continue;
4167 if (f->output_data.x->hourglass_window == wdesc)
4168 return f;
4169 #ifdef USE_X_TOOLKIT
4170 if ((f->output_data.x->edit_widget
4171 && XtWindow (f->output_data.x->edit_widget) == wdesc)
4172 /* A tooltip frame? */
4173 || (!f->output_data.x->edit_widget
4174 && FRAME_X_WINDOW (f) == wdesc)
4175 || f->output_data.x->icon_desc == wdesc)
4176 return f;
4177 #else /* not USE_X_TOOLKIT */
4178 #ifdef USE_GTK
4179 if (f->output_data.x->edit_widget)
4181 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4182 struct x_output *x = f->output_data.x;
4183 if (gwdesc != 0 && gwdesc == x->edit_widget)
4184 return f;
4186 #endif /* USE_GTK */
4187 if (FRAME_X_WINDOW (f) == wdesc
4188 || f->output_data.x->icon_desc == wdesc)
4189 return f;
4190 #endif /* not USE_X_TOOLKIT */
4192 return 0;
4195 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
4197 /* Like x_window_to_frame but also compares the window with the widget's
4198 windows. */
4200 static struct frame *
4201 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4203 Lisp_Object tail, frame;
4204 struct frame *f, *found = NULL;
4205 struct x_output *x;
4207 if (wdesc == None)
4208 return NULL;
4210 FOR_EACH_FRAME (tail, frame)
4212 if (found)
4213 break;
4214 f = XFRAME (frame);
4215 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
4217 /* This frame matches if the window is any of its widgets. */
4218 x = f->output_data.x;
4219 if (x->hourglass_window == wdesc)
4220 found = f;
4221 else if (x->widget)
4223 #ifdef USE_GTK
4224 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4225 if (gwdesc != 0
4226 && gtk_widget_get_toplevel (gwdesc) == x->widget)
4227 found = f;
4228 #else
4229 if (wdesc == XtWindow (x->widget)
4230 || wdesc == XtWindow (x->column_widget)
4231 || wdesc == XtWindow (x->edit_widget))
4232 found = f;
4233 /* Match if the window is this frame's menubar. */
4234 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
4235 found = f;
4236 #endif
4238 else if (FRAME_X_WINDOW (f) == wdesc)
4239 /* A tooltip frame. */
4240 found = f;
4244 return found;
4247 /* Likewise, but consider only the menu bar widget. */
4249 static struct frame *
4250 x_menubar_window_to_frame (struct x_display_info *dpyinfo,
4251 const XEvent *event)
4253 Window wdesc = event->xany.window;
4254 Lisp_Object tail, frame;
4255 struct frame *f;
4256 struct x_output *x;
4258 if (wdesc == None)
4259 return NULL;
4261 FOR_EACH_FRAME (tail, frame)
4263 f = XFRAME (frame);
4264 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4265 continue;
4266 x = f->output_data.x;
4267 #ifdef USE_GTK
4268 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
4269 return f;
4270 #else
4271 /* Match if the window is this frame's menubar. */
4272 if (x->menubar_widget
4273 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
4274 return f;
4275 #endif
4277 return 0;
4280 /* Return the frame whose principal (outermost) window is WDESC.
4281 If WDESC is some other (smaller) window, we return 0. */
4283 struct frame *
4284 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4286 Lisp_Object tail, frame;
4287 struct frame *f;
4288 struct x_output *x;
4290 if (wdesc == None)
4291 return NULL;
4293 FOR_EACH_FRAME (tail, frame)
4295 f = XFRAME (frame);
4296 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4297 continue;
4298 x = f->output_data.x;
4300 if (x->widget)
4302 /* This frame matches if the window is its topmost widget. */
4303 #ifdef USE_GTK
4304 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4305 if (gwdesc == x->widget)
4306 return f;
4307 #else
4308 if (wdesc == XtWindow (x->widget))
4309 return f;
4310 #endif
4312 else if (FRAME_X_WINDOW (f) == wdesc)
4313 /* Tooltip frame. */
4314 return f;
4316 return 0;
4319 #else /* !USE_X_TOOLKIT && !USE_GTK */
4321 #define x_any_window_to_frame(d, i) x_window_to_frame (d, i)
4322 #define x_top_window_to_frame(d, i) x_window_to_frame (d, i)
4324 #endif /* USE_X_TOOLKIT || USE_GTK */
4326 /* The focus may have changed. Figure out if it is a real focus change,
4327 by checking both FocusIn/Out and Enter/LeaveNotify events.
4329 Returns FOCUS_IN_EVENT event in *BUFP. */
4331 static void
4332 x_detect_focus_change (struct x_display_info *dpyinfo, struct frame *frame,
4333 const XEvent *event, struct input_event *bufp)
4335 if (!frame)
4336 return;
4338 switch (event->type)
4340 case EnterNotify:
4341 case LeaveNotify:
4343 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
4344 int focus_state
4345 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
4347 if (event->xcrossing.detail != NotifyInferior
4348 && event->xcrossing.focus
4349 && ! (focus_state & FOCUS_EXPLICIT))
4350 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
4351 FOCUS_IMPLICIT,
4352 dpyinfo, frame, bufp);
4354 break;
4356 case FocusIn:
4357 case FocusOut:
4358 x_focus_changed (event->type,
4359 (event->xfocus.detail == NotifyPointer ?
4360 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
4361 dpyinfo, frame, bufp);
4362 break;
4364 case ClientMessage:
4365 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
4367 enum xembed_message msg = event->xclient.data.l[1];
4368 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
4369 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
4371 break;
4376 #if !defined USE_X_TOOLKIT && !defined USE_GTK
4377 /* Handle an event saying the mouse has moved out of an Emacs frame. */
4379 void
4380 x_mouse_leave (struct x_display_info *dpyinfo)
4382 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
4384 #endif
4386 /* The focus has changed, or we have redirected a frame's focus to
4387 another frame (this happens when a frame uses a surrogate
4388 mini-buffer frame). Shift the highlight as appropriate.
4390 The FRAME argument doesn't necessarily have anything to do with which
4391 frame is being highlighted or un-highlighted; we only use it to find
4392 the appropriate X display info. */
4394 static void
4395 XTframe_rehighlight (struct frame *frame)
4397 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
4400 static void
4401 x_frame_rehighlight (struct x_display_info *dpyinfo)
4403 struct frame *old_highlight = dpyinfo->x_highlight_frame;
4405 if (dpyinfo->x_focus_frame)
4407 dpyinfo->x_highlight_frame
4408 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
4409 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
4410 : dpyinfo->x_focus_frame);
4411 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
4413 fset_focus_frame (dpyinfo->x_focus_frame, Qnil);
4414 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
4417 else
4418 dpyinfo->x_highlight_frame = 0;
4420 if (dpyinfo->x_highlight_frame != old_highlight)
4422 if (old_highlight)
4423 frame_unhighlight (old_highlight);
4424 if (dpyinfo->x_highlight_frame)
4425 frame_highlight (dpyinfo->x_highlight_frame);
4431 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
4433 /* Initialize mode_switch_bit and modifier_meaning. */
4434 static void
4435 x_find_modifier_meanings (struct x_display_info *dpyinfo)
4437 int min_code, max_code;
4438 KeySym *syms;
4439 int syms_per_code;
4440 XModifierKeymap *mods;
4442 dpyinfo->meta_mod_mask = 0;
4443 dpyinfo->shift_lock_mask = 0;
4444 dpyinfo->alt_mod_mask = 0;
4445 dpyinfo->super_mod_mask = 0;
4446 dpyinfo->hyper_mod_mask = 0;
4448 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
4450 syms = XGetKeyboardMapping (dpyinfo->display,
4451 min_code, max_code - min_code + 1,
4452 &syms_per_code);
4453 mods = XGetModifierMapping (dpyinfo->display);
4455 /* Scan the modifier table to see which modifier bits the Meta and
4456 Alt keysyms are on. */
4458 int row, col; /* The row and column in the modifier table. */
4459 bool found_alt_or_meta;
4461 for (row = 3; row < 8; row++)
4463 found_alt_or_meta = false;
4464 for (col = 0; col < mods->max_keypermod; col++)
4466 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
4468 /* Zeroes are used for filler. Skip them. */
4469 if (code == 0)
4470 continue;
4472 /* Are any of this keycode's keysyms a meta key? */
4474 int code_col;
4476 for (code_col = 0; code_col < syms_per_code; code_col++)
4478 int sym = syms[((code - min_code) * syms_per_code) + code_col];
4480 switch (sym)
4482 case XK_Meta_L:
4483 case XK_Meta_R:
4484 found_alt_or_meta = true;
4485 dpyinfo->meta_mod_mask |= (1 << row);
4486 break;
4488 case XK_Alt_L:
4489 case XK_Alt_R:
4490 found_alt_or_meta = true;
4491 dpyinfo->alt_mod_mask |= (1 << row);
4492 break;
4494 case XK_Hyper_L:
4495 case XK_Hyper_R:
4496 if (!found_alt_or_meta)
4497 dpyinfo->hyper_mod_mask |= (1 << row);
4498 code_col = syms_per_code;
4499 col = mods->max_keypermod;
4500 break;
4502 case XK_Super_L:
4503 case XK_Super_R:
4504 if (!found_alt_or_meta)
4505 dpyinfo->super_mod_mask |= (1 << row);
4506 code_col = syms_per_code;
4507 col = mods->max_keypermod;
4508 break;
4510 case XK_Shift_Lock:
4511 /* Ignore this if it's not on the lock modifier. */
4512 if (!found_alt_or_meta && ((1 << row) == LockMask))
4513 dpyinfo->shift_lock_mask = LockMask;
4514 code_col = syms_per_code;
4515 col = mods->max_keypermod;
4516 break;
4524 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
4525 if (! dpyinfo->meta_mod_mask)
4527 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
4528 dpyinfo->alt_mod_mask = 0;
4531 /* If some keys are both alt and meta,
4532 make them just meta, not alt. */
4533 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
4535 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
4538 XFree (syms);
4539 XFreeModifiermap (mods);
4542 /* Convert between the modifier bits X uses and the modifier bits
4543 Emacs uses. */
4546 x_x_to_emacs_modifiers (struct x_display_info *dpyinfo, int state)
4548 int mod_meta = meta_modifier;
4549 int mod_alt = alt_modifier;
4550 int mod_hyper = hyper_modifier;
4551 int mod_super = super_modifier;
4552 Lisp_Object tem;
4554 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4555 if (INTEGERP (tem)) mod_alt = XINT (tem) & INT_MAX;
4556 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4557 if (INTEGERP (tem)) mod_meta = XINT (tem) & INT_MAX;
4558 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4559 if (INTEGERP (tem)) mod_hyper = XINT (tem) & INT_MAX;
4560 tem = Fget (Vx_super_keysym, Qmodifier_value);
4561 if (INTEGERP (tem)) mod_super = XINT (tem) & INT_MAX;
4563 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
4564 | ((state & ControlMask) ? ctrl_modifier : 0)
4565 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
4566 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
4567 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
4568 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
4571 static int
4572 x_emacs_to_x_modifiers (struct x_display_info *dpyinfo, EMACS_INT state)
4574 EMACS_INT mod_meta = meta_modifier;
4575 EMACS_INT mod_alt = alt_modifier;
4576 EMACS_INT mod_hyper = hyper_modifier;
4577 EMACS_INT mod_super = super_modifier;
4579 Lisp_Object tem;
4581 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4582 if (INTEGERP (tem)) mod_alt = XINT (tem);
4583 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4584 if (INTEGERP (tem)) mod_meta = XINT (tem);
4585 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4586 if (INTEGERP (tem)) mod_hyper = XINT (tem);
4587 tem = Fget (Vx_super_keysym, Qmodifier_value);
4588 if (INTEGERP (tem)) mod_super = XINT (tem);
4591 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
4592 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
4593 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
4594 | ((state & shift_modifier) ? ShiftMask : 0)
4595 | ((state & ctrl_modifier) ? ControlMask : 0)
4596 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
4599 /* Convert a keysym to its name. */
4601 char *
4602 x_get_keysym_name (int keysym)
4604 char *value;
4606 block_input ();
4607 value = XKeysymToString (keysym);
4608 unblock_input ();
4610 return value;
4613 /* Mouse clicks and mouse movement. Rah.
4615 Formerly, we used PointerMotionHintMask (in standard_event_mask)
4616 so that we would have to call XQueryPointer after each MotionNotify
4617 event to ask for another such event. However, this made mouse tracking
4618 slow, and there was a bug that made it eventually stop.
4620 Simply asking for MotionNotify all the time seems to work better.
4622 In order to avoid asking for motion events and then throwing most
4623 of them away or busy-polling the server for mouse positions, we ask
4624 the server for pointer motion hints. This means that we get only
4625 one event per group of mouse movements. "Groups" are delimited by
4626 other kinds of events (focus changes and button clicks, for
4627 example), or by XQueryPointer calls; when one of these happens, we
4628 get another MotionNotify event the next time the mouse moves. This
4629 is at least as efficient as getting motion events when mouse
4630 tracking is on, and I suspect only negligibly worse when tracking
4631 is off. */
4633 /* Prepare a mouse-event in *RESULT for placement in the input queue.
4635 If the event is a button press, then note that we have grabbed
4636 the mouse. */
4638 static Lisp_Object
4639 construct_mouse_click (struct input_event *result,
4640 const XButtonEvent *event,
4641 struct frame *f)
4643 /* Make the event type NO_EVENT; we'll change that when we decide
4644 otherwise. */
4645 result->kind = MOUSE_CLICK_EVENT;
4646 result->code = event->button - Button1;
4647 result->timestamp = event->time;
4648 result->modifiers = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f),
4649 event->state)
4650 | (event->type == ButtonRelease
4651 ? up_modifier
4652 : down_modifier));
4654 XSETINT (result->x, event->x);
4655 XSETINT (result->y, event->y);
4656 XSETFRAME (result->frame_or_window, f);
4657 result->arg = Qnil;
4658 return Qnil;
4661 /* Function to report a mouse movement to the mainstream Emacs code.
4662 The input handler calls this.
4664 We have received a mouse movement event, which is given in *event.
4665 If the mouse is over a different glyph than it was last time, tell
4666 the mainstream emacs code by setting mouse_moved. If not, ask for
4667 another motion event, so we can check again the next time it moves. */
4669 static bool
4670 note_mouse_movement (struct frame *frame, const XMotionEvent *event)
4672 XRectangle *r;
4673 struct x_display_info *dpyinfo;
4675 if (!FRAME_X_OUTPUT (frame))
4676 return false;
4678 dpyinfo = FRAME_DISPLAY_INFO (frame);
4679 dpyinfo->last_mouse_movement_time = event->time;
4680 dpyinfo->last_mouse_motion_frame = frame;
4681 dpyinfo->last_mouse_motion_x = event->x;
4682 dpyinfo->last_mouse_motion_y = event->y;
4684 if (event->window != FRAME_X_WINDOW (frame))
4686 frame->mouse_moved = true;
4687 dpyinfo->last_mouse_scroll_bar = NULL;
4688 note_mouse_highlight (frame, -1, -1);
4689 dpyinfo->last_mouse_glyph_frame = NULL;
4690 return true;
4694 /* Has the mouse moved off the glyph it was on at the last sighting? */
4695 r = &dpyinfo->last_mouse_glyph;
4696 if (frame != dpyinfo->last_mouse_glyph_frame
4697 || event->x < r->x || event->x >= r->x + r->width
4698 || event->y < r->y || event->y >= r->y + r->height)
4700 frame->mouse_moved = true;
4701 dpyinfo->last_mouse_scroll_bar = NULL;
4702 note_mouse_highlight (frame, event->x, event->y);
4703 /* Remember which glyph we're now on. */
4704 remember_mouse_glyph (frame, event->x, event->y, r);
4705 dpyinfo->last_mouse_glyph_frame = frame;
4706 return true;
4709 return false;
4712 /* Return the current position of the mouse.
4713 *FP should be a frame which indicates which display to ask about.
4715 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
4716 and *PART to the frame, window, and scroll bar part that the mouse
4717 is over. Set *X and *Y to the portion and whole of the mouse's
4718 position on the scroll bar.
4720 If the mouse movement started elsewhere, set *FP to the frame the
4721 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
4722 the mouse is over.
4724 Set *TIMESTAMP to the server time-stamp for the time at which the mouse
4725 was at this position.
4727 Don't store anything if we don't have a valid set of values to report.
4729 This clears the mouse_moved flag, so we can wait for the next mouse
4730 movement. */
4732 static void
4733 XTmouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
4734 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
4735 Time *timestamp)
4737 struct frame *f1;
4738 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
4740 block_input ();
4742 if (dpyinfo->last_mouse_scroll_bar && insist == 0)
4744 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
4746 if (bar->horizontal)
4747 x_horizontal_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
4748 else
4749 x_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
4751 else
4753 Window root;
4754 int root_x, root_y;
4756 Window dummy_window;
4757 int dummy;
4759 Lisp_Object frame, tail;
4761 /* Clear the mouse-moved flag for every frame on this display. */
4762 FOR_EACH_FRAME (tail, frame)
4763 if (FRAME_X_P (XFRAME (frame))
4764 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
4765 XFRAME (frame)->mouse_moved = false;
4767 dpyinfo->last_mouse_scroll_bar = NULL;
4769 /* Figure out which root window we're on. */
4770 XQueryPointer (FRAME_X_DISPLAY (*fp),
4771 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
4773 /* The root window which contains the pointer. */
4774 &root,
4776 /* Trash which we can't trust if the pointer is on
4777 a different screen. */
4778 &dummy_window,
4780 /* The position on that root window. */
4781 &root_x, &root_y,
4783 /* More trash we can't trust. */
4784 &dummy, &dummy,
4786 /* Modifier keys and pointer buttons, about which
4787 we don't care. */
4788 (unsigned int *) &dummy);
4790 /* Now we have a position on the root; find the innermost window
4791 containing the pointer. */
4793 Window win, child;
4794 int win_x, win_y;
4795 int parent_x = 0, parent_y = 0;
4797 win = root;
4799 /* XTranslateCoordinates can get errors if the window
4800 structure is changing at the same time this function
4801 is running. So at least we must not crash from them. */
4803 x_catch_errors (FRAME_X_DISPLAY (*fp));
4805 if (x_mouse_grabbed (dpyinfo))
4807 /* If mouse was grabbed on a frame, give coords for that frame
4808 even if the mouse is now outside it. */
4809 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
4811 /* From-window. */
4812 root,
4814 /* To-window. */
4815 FRAME_X_WINDOW (dpyinfo->last_mouse_frame),
4817 /* From-position, to-position. */
4818 root_x, root_y, &win_x, &win_y,
4820 /* Child of win. */
4821 &child);
4822 f1 = dpyinfo->last_mouse_frame;
4824 else
4826 while (true)
4828 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
4830 /* From-window, to-window. */
4831 root, win,
4833 /* From-position, to-position. */
4834 root_x, root_y, &win_x, &win_y,
4836 /* Child of win. */
4837 &child);
4839 if (child == None || child == win)
4840 break;
4841 #ifdef USE_GTK
4842 /* We don't wan't to know the innermost window. We
4843 want the edit window. For non-Gtk+ the innermost
4844 window is the edit window. For Gtk+ it might not
4845 be. It might be the tool bar for example. */
4846 if (x_window_to_frame (dpyinfo, win))
4847 break;
4848 #endif
4849 win = child;
4850 parent_x = win_x;
4851 parent_y = win_y;
4854 /* Now we know that:
4855 win is the innermost window containing the pointer
4856 (XTC says it has no child containing the pointer),
4857 win_x and win_y are the pointer's position in it
4858 (XTC did this the last time through), and
4859 parent_x and parent_y are the pointer's position in win's parent.
4860 (They are what win_x and win_y were when win was child.
4861 If win is the root window, it has no parent, and
4862 parent_{x,y} are invalid, but that's okay, because we'll
4863 never use them in that case.) */
4865 #ifdef USE_GTK
4866 /* We don't wan't to know the innermost window. We
4867 want the edit window. */
4868 f1 = x_window_to_frame (dpyinfo, win);
4869 #else
4870 /* Is win one of our frames? */
4871 f1 = x_any_window_to_frame (dpyinfo, win);
4872 #endif
4874 #ifdef USE_X_TOOLKIT
4875 /* If we end up with the menu bar window, say it's not
4876 on the frame. */
4877 if (f1 != NULL
4878 && f1->output_data.x->menubar_widget
4879 && win == XtWindow (f1->output_data.x->menubar_widget))
4880 f1 = NULL;
4881 #endif /* USE_X_TOOLKIT */
4884 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
4885 f1 = 0;
4887 x_uncatch_errors ();
4889 /* If not, is it one of our scroll bars? */
4890 if (! f1)
4892 struct scroll_bar *bar;
4894 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win, 2);
4896 if (bar)
4898 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4899 win_x = parent_x;
4900 win_y = parent_y;
4904 if (f1 == 0 && insist > 0)
4905 f1 = SELECTED_FRAME ();
4907 if (f1)
4909 /* Ok, we found a frame. Store all the values.
4910 last_mouse_glyph is a rectangle used to reduce the
4911 generation of mouse events. To not miss any motion
4912 events, we must divide the frame into rectangles of the
4913 size of the smallest character that could be displayed
4914 on it, i.e. into the same rectangles that matrices on
4915 the frame are divided into. */
4917 /* FIXME: what if F1 is not an X frame? */
4918 dpyinfo = FRAME_DISPLAY_INFO (f1);
4919 remember_mouse_glyph (f1, win_x, win_y, &dpyinfo->last_mouse_glyph);
4920 dpyinfo->last_mouse_glyph_frame = f1;
4922 *bar_window = Qnil;
4923 *part = 0;
4924 *fp = f1;
4925 XSETINT (*x, win_x);
4926 XSETINT (*y, win_y);
4927 *timestamp = dpyinfo->last_mouse_movement_time;
4932 unblock_input ();
4937 /***********************************************************************
4938 Scroll bars
4939 ***********************************************************************/
4941 /* Scroll bar support. */
4943 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
4944 manages it.
4945 This can be called in GC, so we have to make sure to strip off mark
4946 bits. */
4948 static struct scroll_bar *
4949 x_window_to_scroll_bar (Display *display, Window window_id, int type)
4951 Lisp_Object tail, frame;
4953 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4954 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4955 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4957 FOR_EACH_FRAME (tail, frame)
4959 Lisp_Object bar, condemned;
4961 if (! FRAME_X_P (XFRAME (frame)))
4962 continue;
4964 /* Scan this frame's scroll bar list for a scroll bar with the
4965 right window ID. */
4966 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4967 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4968 /* This trick allows us to search both the ordinary and
4969 condemned scroll bar lists with one loop. */
4970 ! NILP (bar) || (bar = condemned,
4971 condemned = Qnil,
4972 ! NILP (bar));
4973 bar = XSCROLL_BAR (bar)->next)
4974 if (XSCROLL_BAR (bar)->x_window == window_id
4975 && FRAME_X_DISPLAY (XFRAME (frame)) == display
4976 && (type = 2
4977 || (type == 1 && XSCROLL_BAR (bar)->horizontal)
4978 || (type == 0 && !XSCROLL_BAR (bar)->horizontal)))
4979 return XSCROLL_BAR (bar);
4982 return NULL;
4986 #if defined USE_LUCID
4988 /* Return the Lucid menu bar WINDOW is part of. Return null
4989 if WINDOW is not part of a menu bar. */
4991 static Widget
4992 x_window_to_menu_bar (Window window)
4994 Lisp_Object tail, frame;
4996 FOR_EACH_FRAME (tail, frame)
4997 if (FRAME_X_P (XFRAME (frame)))
4999 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
5001 if (menu_bar && xlwmenu_window_p (menu_bar, window))
5002 return menu_bar;
5004 return NULL;
5007 #endif /* USE_LUCID */
5010 /************************************************************************
5011 Toolkit scroll bars
5012 ************************************************************************/
5014 #ifdef USE_TOOLKIT_SCROLL_BARS
5016 static void x_send_scroll_bar_event (Lisp_Object, enum scroll_bar_part,
5017 int, int, bool);
5019 /* Lisp window being scrolled. Set when starting to interact with
5020 a toolkit scroll bar, reset to nil when ending the interaction. */
5022 static Lisp_Object window_being_scrolled;
5024 /* Whether this is an Xaw with arrow-scrollbars. This should imply
5025 that movements of 1/20 of the screen size are mapped to up/down. */
5027 #ifndef USE_GTK
5028 /* Id of action hook installed for scroll bars. */
5030 static XtActionHookId action_hook_id;
5031 static XtActionHookId horizontal_action_hook_id;
5033 static Boolean xaw3d_arrow_scroll;
5035 /* Whether the drag scrolling maintains the mouse at the top of the
5036 thumb. If not, resizing the thumb needs to be done more carefully
5037 to avoid jerkiness. */
5039 static Boolean xaw3d_pick_top;
5041 /* Action hook installed via XtAppAddActionHook when toolkit scroll
5042 bars are used.. The hook is responsible for detecting when
5043 the user ends an interaction with the scroll bar, and generates
5044 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
5046 static void
5047 xt_action_hook (Widget widget, XtPointer client_data, String action_name,
5048 XEvent *event, String *params, Cardinal *num_params)
5050 bool scroll_bar_p;
5051 const char *end_action;
5053 #ifdef USE_MOTIF
5054 scroll_bar_p = XmIsScrollBar (widget);
5055 end_action = "Release";
5056 #else /* !USE_MOTIF i.e. use Xaw */
5057 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
5058 end_action = "EndScroll";
5059 #endif /* USE_MOTIF */
5061 if (scroll_bar_p
5062 && strcmp (action_name, end_action) == 0
5063 && WINDOWP (window_being_scrolled))
5065 struct window *w;
5066 struct scroll_bar *bar;
5068 x_send_scroll_bar_event (window_being_scrolled,
5069 scroll_bar_end_scroll, 0, 0, false);
5070 w = XWINDOW (window_being_scrolled);
5071 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5073 if (bar->dragging != -1)
5075 bar->dragging = -1;
5076 /* The thumb size is incorrect while dragging: fix it. */
5077 set_vertical_scroll_bar (w);
5079 window_being_scrolled = Qnil;
5080 #if defined (USE_LUCID)
5081 bar->last_seen_part = scroll_bar_nowhere;
5082 #endif
5083 /* Xt timeouts no longer needed. */
5084 toolkit_scroll_bar_interaction = false;
5089 static void
5090 xt_horizontal_action_hook (Widget widget, XtPointer client_data, String action_name,
5091 XEvent *event, String *params, Cardinal *num_params)
5093 bool scroll_bar_p;
5094 const char *end_action;
5096 #ifdef USE_MOTIF
5097 scroll_bar_p = XmIsScrollBar (widget);
5098 end_action = "Release";
5099 #else /* !USE_MOTIF i.e. use Xaw */
5100 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
5101 end_action = "EndScroll";
5102 #endif /* USE_MOTIF */
5104 if (scroll_bar_p
5105 && strcmp (action_name, end_action) == 0
5106 && WINDOWP (window_being_scrolled))
5108 struct window *w;
5109 struct scroll_bar *bar;
5111 x_send_scroll_bar_event (window_being_scrolled,
5112 scroll_bar_end_scroll, 0, 0, true);
5113 w = XWINDOW (window_being_scrolled);
5114 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
5116 if (bar->dragging != -1)
5118 bar->dragging = -1;
5119 /* The thumb size is incorrect while dragging: fix it. */
5120 set_horizontal_scroll_bar (w);
5122 window_being_scrolled = Qnil;
5123 #if defined (USE_LUCID)
5124 bar->last_seen_part = scroll_bar_nowhere;
5125 #endif
5126 /* Xt timeouts no longer needed. */
5127 toolkit_scroll_bar_interaction = false;
5130 #endif /* not USE_GTK */
5132 /* Send a client message with message type Xatom_Scrollbar for a
5133 scroll action to the frame of WINDOW. PART is a value identifying
5134 the part of the scroll bar that was clicked on. PORTION is the
5135 amount to scroll of a whole of WHOLE. */
5137 static void
5138 x_send_scroll_bar_event (Lisp_Object window, enum scroll_bar_part part,
5139 int portion, int whole, bool horizontal)
5141 XEvent event;
5142 XClientMessageEvent *ev = &event.xclient;
5143 struct window *w = XWINDOW (window);
5144 struct frame *f = XFRAME (w->frame);
5145 intptr_t iw = (intptr_t) w;
5146 enum { BITS_PER_INTPTR = CHAR_BIT * sizeof iw };
5147 verify (BITS_PER_INTPTR <= 64);
5148 int sign_shift = BITS_PER_INTPTR - 32;
5150 block_input ();
5152 /* Construct a ClientMessage event to send to the frame. */
5153 ev->type = ClientMessage;
5154 ev->message_type = (horizontal
5155 ? FRAME_DISPLAY_INFO (f)->Xatom_Horizontal_Scrollbar
5156 : FRAME_DISPLAY_INFO (f)->Xatom_Scrollbar);
5157 ev->display = FRAME_X_DISPLAY (f);
5158 ev->window = FRAME_X_WINDOW (f);
5159 ev->format = 32;
5161 /* A 32-bit X client on a 64-bit X server can pass a window pointer
5162 as-is. A 64-bit client on a 32-bit X server is in trouble
5163 because a pointer does not fit and would be truncated while
5164 passing through the server. So use two slots and hope that X12
5165 will resolve such issues someday. */
5166 ev->data.l[0] = iw >> 31 >> 1;
5167 ev->data.l[1] = sign_shift <= 0 ? iw : iw << sign_shift >> sign_shift;
5168 ev->data.l[2] = part;
5169 ev->data.l[3] = portion;
5170 ev->data.l[4] = whole;
5172 /* Make Xt timeouts work while the scroll bar is active. */
5173 #ifdef USE_X_TOOLKIT
5174 toolkit_scroll_bar_interaction = true;
5175 x_activate_timeout_atimer ();
5176 #endif
5178 /* Setting the event mask to zero means that the message will
5179 be sent to the client that created the window, and if that
5180 window no longer exists, no event will be sent. */
5181 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
5182 unblock_input ();
5186 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
5187 in *IEVENT. */
5189 static void
5190 x_scroll_bar_to_input_event (const XEvent *event,
5191 struct input_event *ievent)
5193 const XClientMessageEvent *ev = &event->xclient;
5194 Lisp_Object window;
5195 struct window *w;
5197 /* See the comment in the function above. */
5198 intptr_t iw0 = ev->data.l[0];
5199 intptr_t iw1 = ev->data.l[1];
5200 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
5201 w = (struct window *) iw;
5203 XSETWINDOW (window, w);
5205 ievent->kind = SCROLL_BAR_CLICK_EVENT;
5206 ievent->frame_or_window = window;
5207 ievent->arg = Qnil;
5208 #ifdef USE_GTK
5209 ievent->timestamp = CurrentTime;
5210 #else
5211 ievent->timestamp =
5212 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
5213 #endif
5214 ievent->code = 0;
5215 ievent->part = ev->data.l[2];
5216 ievent->x = make_number (ev->data.l[3]);
5217 ievent->y = make_number (ev->data.l[4]);
5218 ievent->modifiers = 0;
5221 /* Transform a horizontal scroll bar ClientMessage EVENT to an Emacs
5222 input event in *IEVENT. */
5224 static void
5225 x_horizontal_scroll_bar_to_input_event (const XEvent *event,
5226 struct input_event *ievent)
5228 const XClientMessageEvent *ev = &event->xclient;
5229 Lisp_Object window;
5230 struct window *w;
5232 /* See the comment in the function above. */
5233 intptr_t iw0 = ev->data.l[0];
5234 intptr_t iw1 = ev->data.l[1];
5235 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
5236 w = (struct window *) iw;
5238 XSETWINDOW (window, w);
5240 ievent->kind = HORIZONTAL_SCROLL_BAR_CLICK_EVENT;
5241 ievent->frame_or_window = window;
5242 ievent->arg = Qnil;
5243 #ifdef USE_GTK
5244 ievent->timestamp = CurrentTime;
5245 #else
5246 ievent->timestamp =
5247 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
5248 #endif
5249 ievent->code = 0;
5250 ievent->part = ev->data.l[2];
5251 ievent->x = make_number (ev->data.l[3]);
5252 ievent->y = make_number (ev->data.l[4]);
5253 ievent->modifiers = 0;
5257 #ifdef USE_MOTIF
5259 /* Minimum and maximum values used for Motif scroll bars. */
5261 #define XM_SB_MAX 10000000
5263 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
5264 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
5265 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
5267 static void
5268 xm_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5270 struct scroll_bar *bar = client_data;
5271 XmScrollBarCallbackStruct *cs = call_data;
5272 enum scroll_bar_part part = scroll_bar_nowhere;
5273 bool horizontal = bar->horizontal;
5274 int whole = 0, portion = 0;
5276 switch (cs->reason)
5278 case XmCR_DECREMENT:
5279 bar->dragging = -1;
5280 part = horizontal ? scroll_bar_left_arrow : scroll_bar_up_arrow;
5281 break;
5283 case XmCR_INCREMENT:
5284 bar->dragging = -1;
5285 part = horizontal ? scroll_bar_right_arrow : scroll_bar_down_arrow;
5286 break;
5288 case XmCR_PAGE_DECREMENT:
5289 bar->dragging = -1;
5290 part = horizontal ? scroll_bar_before_handle : scroll_bar_above_handle;
5291 break;
5293 case XmCR_PAGE_INCREMENT:
5294 bar->dragging = -1;
5295 part = horizontal ? scroll_bar_after_handle : scroll_bar_below_handle;
5296 break;
5298 case XmCR_TO_TOP:
5299 bar->dragging = -1;
5300 part = horizontal ? scroll_bar_to_leftmost : scroll_bar_to_top;
5301 break;
5303 case XmCR_TO_BOTTOM:
5304 bar->dragging = -1;
5305 part = horizontal ? scroll_bar_to_rightmost : scroll_bar_to_bottom;
5306 break;
5308 case XmCR_DRAG:
5310 int slider_size;
5312 block_input ();
5313 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
5314 unblock_input ();
5316 if (horizontal)
5318 portion = bar->whole * ((float)cs->value / XM_SB_MAX);
5319 whole = bar->whole * ((float)(XM_SB_MAX - slider_size) / XM_SB_MAX);
5320 portion = min (portion, whole);
5321 part = scroll_bar_horizontal_handle;
5323 else
5325 whole = XM_SB_MAX - slider_size;
5326 portion = min (cs->value, whole);
5327 part = scroll_bar_handle;
5330 bar->dragging = cs->value;
5332 break;
5334 case XmCR_VALUE_CHANGED:
5335 break;
5338 if (part != scroll_bar_nowhere)
5340 window_being_scrolled = bar->window;
5341 x_send_scroll_bar_event (bar->window, part, portion, whole,
5342 bar->horizontal);
5346 #elif defined USE_GTK
5348 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
5349 bar widget. DATA is a pointer to the scroll_bar structure. */
5351 static gboolean
5352 xg_scroll_callback (GtkRange *range,
5353 GtkScrollType scroll,
5354 gdouble value,
5355 gpointer user_data)
5357 int whole = 0, portion = 0;
5358 struct scroll_bar *bar = user_data;
5359 enum scroll_bar_part part = scroll_bar_nowhere;
5360 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
5361 struct frame *f = g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
5363 if (xg_ignore_gtk_scrollbar) return false;
5365 switch (scroll)
5367 case GTK_SCROLL_JUMP:
5368 /* Buttons 1 2 or 3 must be grabbed. */
5369 if (FRAME_DISPLAY_INFO (f)->grabbed != 0
5370 && FRAME_DISPLAY_INFO (f)->grabbed < (1 << 4))
5372 if (bar->horizontal)
5374 part = scroll_bar_horizontal_handle;
5375 whole = (int)(gtk_adjustment_get_upper (adj) -
5376 gtk_adjustment_get_page_size (adj));
5377 portion = min ((int)value, whole);
5378 bar->dragging = portion;
5380 else
5382 part = scroll_bar_handle;
5383 whole = gtk_adjustment_get_upper (adj) -
5384 gtk_adjustment_get_page_size (adj);
5385 portion = min ((int)value, whole);
5386 bar->dragging = portion;
5389 break;
5390 case GTK_SCROLL_STEP_BACKWARD:
5391 part = (bar->horizontal
5392 ? scroll_bar_left_arrow : scroll_bar_up_arrow);
5393 bar->dragging = -1;
5394 break;
5395 case GTK_SCROLL_STEP_FORWARD:
5396 part = (bar->horizontal
5397 ? scroll_bar_right_arrow : scroll_bar_down_arrow);
5398 bar->dragging = -1;
5399 break;
5400 case GTK_SCROLL_PAGE_BACKWARD:
5401 part = (bar->horizontal
5402 ? scroll_bar_before_handle : scroll_bar_above_handle);
5403 bar->dragging = -1;
5404 break;
5405 case GTK_SCROLL_PAGE_FORWARD:
5406 part = (bar->horizontal
5407 ? scroll_bar_after_handle : scroll_bar_below_handle);
5408 bar->dragging = -1;
5409 break;
5410 default:
5411 break;
5414 if (part != scroll_bar_nowhere)
5416 window_being_scrolled = bar->window;
5417 x_send_scroll_bar_event (bar->window, part, portion, whole,
5418 bar->horizontal);
5421 return false;
5424 /* Callback for button release. Sets dragging to -1 when dragging is done. */
5426 static gboolean
5427 xg_end_scroll_callback (GtkWidget *widget,
5428 GdkEventButton *event,
5429 gpointer user_data)
5431 struct scroll_bar *bar = user_data;
5432 bar->dragging = -1;
5433 if (WINDOWP (window_being_scrolled))
5435 x_send_scroll_bar_event (window_being_scrolled,
5436 scroll_bar_end_scroll, 0, 0, bar->horizontal);
5437 window_being_scrolled = Qnil;
5440 return false;
5444 #else /* not USE_GTK and not USE_MOTIF */
5446 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
5447 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
5448 scroll bar struct. CALL_DATA is a pointer to a float saying where
5449 the thumb is. */
5451 static void
5452 xaw_jump_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5454 struct scroll_bar *bar = client_data;
5455 float *top_addr = call_data;
5456 float top = *top_addr;
5457 float shown;
5458 int whole, portion, height, width;
5459 enum scroll_bar_part part;
5460 bool horizontal = bar->horizontal;
5463 if (horizontal)
5465 /* Get the size of the thumb, a value between 0 and 1. */
5466 block_input ();
5467 XtVaGetValues (widget, XtNshown, &shown, XtNwidth, &width, NULL);
5468 unblock_input ();
5470 if (shown < 1)
5472 whole = bar->whole - (shown * bar->whole);
5473 portion = min (top * bar->whole, whole);
5475 else
5477 whole = bar->whole;
5478 portion = 0;
5481 part = scroll_bar_horizontal_handle;
5483 else
5485 /* Get the size of the thumb, a value between 0 and 1. */
5486 block_input ();
5487 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
5488 unblock_input ();
5490 whole = 10000000;
5491 portion = shown < 1 ? top * whole : 0;
5493 if (shown < 1 && (eabs (top + shown - 1) < 1.0f / height))
5494 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
5495 the bottom, so we force the scrolling whenever we see that we're
5496 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
5497 we try to ensure that we always stay two pixels away from the
5498 bottom). */
5499 part = scroll_bar_down_arrow;
5500 else
5501 part = scroll_bar_handle;
5504 window_being_scrolled = bar->window;
5505 bar->dragging = portion;
5506 bar->last_seen_part = part;
5507 x_send_scroll_bar_event (bar->window, part, portion, whole, bar->horizontal);
5511 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
5512 i.e. line or page up or down. WIDGET is the Xaw scroll bar
5513 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
5514 the scroll bar. CALL_DATA is an integer specifying the action that
5515 has taken place. Its magnitude is in the range 0..height of the
5516 scroll bar. Negative values mean scroll towards buffer start.
5517 Values < height of scroll bar mean line-wise movement. */
5519 static void
5520 xaw_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5522 struct scroll_bar *bar = client_data;
5523 /* The position really is stored cast to a pointer. */
5524 int position = (intptr_t) call_data;
5525 Dimension height, width;
5526 enum scroll_bar_part part;
5528 if (bar->horizontal)
5530 /* Get the width of the scroll bar. */
5531 block_input ();
5532 XtVaGetValues (widget, XtNwidth, &width, NULL);
5533 unblock_input ();
5535 if (eabs (position) >= width)
5536 part = (position < 0) ? scroll_bar_before_handle : scroll_bar_after_handle;
5538 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
5539 it maps line-movement to call_data = max(5, height/20). */
5540 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, width / 20))
5541 part = (position < 0) ? scroll_bar_left_arrow : scroll_bar_right_arrow;
5542 else
5543 part = scroll_bar_move_ratio;
5545 window_being_scrolled = bar->window;
5546 bar->dragging = -1;
5547 bar->last_seen_part = part;
5548 x_send_scroll_bar_event (bar->window, part, position, width,
5549 bar->horizontal);
5551 else
5554 /* Get the height of the scroll bar. */
5555 block_input ();
5556 XtVaGetValues (widget, XtNheight, &height, NULL);
5557 unblock_input ();
5559 if (eabs (position) >= height)
5560 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
5562 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
5563 it maps line-movement to call_data = max(5, height/20). */
5564 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
5565 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
5566 else
5567 part = scroll_bar_move_ratio;
5569 window_being_scrolled = bar->window;
5570 bar->dragging = -1;
5571 bar->last_seen_part = part;
5572 x_send_scroll_bar_event (bar->window, part, position, height,
5573 bar->horizontal);
5577 #endif /* not USE_GTK and not USE_MOTIF */
5579 #define SCROLL_BAR_NAME "verticalScrollBar"
5580 #define SCROLL_BAR_HORIZONTAL_NAME "horizontalScrollBar"
5582 /* Create the widget for scroll bar BAR on frame F. Record the widget
5583 and X window of the scroll bar in BAR. */
5585 #ifdef USE_GTK
5586 static void
5587 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5589 const char *scroll_bar_name = SCROLL_BAR_NAME;
5591 block_input ();
5592 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
5593 G_CALLBACK (xg_end_scroll_callback),
5594 scroll_bar_name);
5595 unblock_input ();
5598 static void
5599 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5601 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
5603 block_input ();
5604 xg_create_horizontal_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
5605 G_CALLBACK (xg_end_scroll_callback),
5606 scroll_bar_name);
5607 unblock_input ();
5610 #else /* not USE_GTK */
5612 static void
5613 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5615 Window xwindow;
5616 Widget widget;
5617 Arg av[20];
5618 int ac = 0;
5619 const char *scroll_bar_name = SCROLL_BAR_NAME;
5620 unsigned long pixel;
5622 block_input ();
5624 #ifdef USE_MOTIF
5625 /* Set resources. Create the widget. */
5626 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5627 XtSetArg (av[ac], XmNminimum, 0); ++ac;
5628 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
5629 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
5630 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
5631 XtSetArg (av[ac], XmNincrement, 1); ++ac;
5632 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
5634 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5635 if (pixel != -1)
5637 XtSetArg (av[ac], XmNforeground, pixel);
5638 ++ac;
5641 pixel = f->output_data.x->scroll_bar_background_pixel;
5642 if (pixel != -1)
5644 XtSetArg (av[ac], XmNbackground, pixel);
5645 ++ac;
5648 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
5649 (char *) scroll_bar_name, av, ac);
5651 /* Add one callback for everything that can happen. */
5652 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
5653 (XtPointer) bar);
5654 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
5655 (XtPointer) bar);
5656 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
5657 (XtPointer) bar);
5658 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
5659 (XtPointer) bar);
5660 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
5661 (XtPointer) bar);
5662 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
5663 (XtPointer) bar);
5664 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
5665 (XtPointer) bar);
5667 /* Realize the widget. Only after that is the X window created. */
5668 XtRealizeWidget (widget);
5670 /* Set the cursor to an arrow. I didn't find a resource to do that.
5671 And I'm wondering why it hasn't an arrow cursor by default. */
5672 XDefineCursor (XtDisplay (widget), XtWindow (widget),
5673 f->output_data.x->nontext_cursor);
5675 #else /* !USE_MOTIF i.e. use Xaw */
5677 /* Set resources. Create the widget. The background of the
5678 Xaw3d scroll bar widget is a little bit light for my taste.
5679 We don't alter it here to let users change it according
5680 to their taste with `emacs*verticalScrollBar.background: xxx'. */
5681 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5682 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
5683 /* For smoother scrolling with Xaw3d -sm */
5684 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
5686 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5687 if (pixel != -1)
5689 XtSetArg (av[ac], XtNforeground, pixel);
5690 ++ac;
5693 pixel = f->output_data.x->scroll_bar_background_pixel;
5694 if (pixel != -1)
5696 XtSetArg (av[ac], XtNbackground, pixel);
5697 ++ac;
5700 /* Top/bottom shadow colors. */
5702 /* Allocate them, if necessary. */
5703 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
5705 pixel = f->output_data.x->scroll_bar_background_pixel;
5706 if (pixel != -1)
5708 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5709 FRAME_X_COLORMAP (f),
5710 &pixel, 1.2, 0x8000))
5711 pixel = -1;
5712 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
5715 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5717 pixel = f->output_data.x->scroll_bar_background_pixel;
5718 if (pixel != -1)
5720 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5721 FRAME_X_COLORMAP (f),
5722 &pixel, 0.6, 0x4000))
5723 pixel = -1;
5724 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
5728 #ifdef XtNbeNiceToColormap
5729 /* Tell the toolkit about them. */
5730 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
5731 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5732 /* We tried to allocate a color for the top/bottom shadow, and
5733 failed, so tell Xaw3d to use dithering instead. */
5734 /* But only if we have a small colormap. Xaw3d can allocate nice
5735 colors itself. */
5737 XtSetArg (av[ac], XtNbeNiceToColormap,
5738 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
5739 ++ac;
5741 else
5742 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
5743 be more consistent with other emacs 3d colors, and since Xaw3d is
5744 not good at dealing with allocation failure. */
5746 /* This tells Xaw3d to use real colors instead of dithering for
5747 the shadows. */
5748 XtSetArg (av[ac], XtNbeNiceToColormap, False);
5749 ++ac;
5751 /* Specify the colors. */
5752 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
5753 if (pixel != -1)
5755 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
5756 ++ac;
5758 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
5759 if (pixel != -1)
5761 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
5762 ++ac;
5765 #endif
5767 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
5768 f->output_data.x->edit_widget, av, ac);
5771 char const *initial = "";
5772 char const *val = initial;
5773 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
5774 #ifdef XtNarrowScrollbars
5775 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
5776 #endif
5777 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
5778 if (xaw3d_arrow_scroll || val == initial)
5779 { /* ARROW_SCROLL */
5780 xaw3d_arrow_scroll = True;
5781 /* Isn't that just a personal preference ? --Stef */
5782 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
5786 /* Define callbacks. */
5787 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
5788 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
5789 (XtPointer) bar);
5791 /* Realize the widget. Only after that is the X window created. */
5792 XtRealizeWidget (widget);
5794 #endif /* !USE_MOTIF */
5796 /* Install an action hook that lets us detect when the user
5797 finishes interacting with a scroll bar. */
5798 if (action_hook_id == 0)
5799 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
5801 /* Remember X window and widget in the scroll bar vector. */
5802 SET_SCROLL_BAR_X_WIDGET (bar, widget);
5803 xwindow = XtWindow (widget);
5804 bar->x_window = xwindow;
5805 bar->whole = 1;
5806 bar->horizontal = false;
5808 unblock_input ();
5811 static void
5812 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5814 Window xwindow;
5815 Widget widget;
5816 Arg av[20];
5817 int ac = 0;
5818 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
5819 unsigned long pixel;
5821 block_input ();
5823 #ifdef USE_MOTIF
5824 /* Set resources. Create the widget. */
5825 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5826 XtSetArg (av[ac], XmNminimum, 0); ++ac;
5827 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
5828 XtSetArg (av[ac], XmNorientation, XmHORIZONTAL); ++ac;
5829 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_RIGHT), ++ac;
5830 XtSetArg (av[ac], XmNincrement, 1); ++ac;
5831 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
5833 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5834 if (pixel != -1)
5836 XtSetArg (av[ac], XmNforeground, pixel);
5837 ++ac;
5840 pixel = f->output_data.x->scroll_bar_background_pixel;
5841 if (pixel != -1)
5843 XtSetArg (av[ac], XmNbackground, pixel);
5844 ++ac;
5847 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
5848 (char *) scroll_bar_name, av, ac);
5850 /* Add one callback for everything that can happen. */
5851 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
5852 (XtPointer) bar);
5853 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
5854 (XtPointer) bar);
5855 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
5856 (XtPointer) bar);
5857 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
5858 (XtPointer) bar);
5859 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
5860 (XtPointer) bar);
5861 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
5862 (XtPointer) bar);
5863 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
5864 (XtPointer) bar);
5866 /* Realize the widget. Only after that is the X window created. */
5867 XtRealizeWidget (widget);
5869 /* Set the cursor to an arrow. I didn't find a resource to do that.
5870 And I'm wondering why it hasn't an arrow cursor by default. */
5871 XDefineCursor (XtDisplay (widget), XtWindow (widget),
5872 f->output_data.x->nontext_cursor);
5874 #else /* !USE_MOTIF i.e. use Xaw */
5876 /* Set resources. Create the widget. The background of the
5877 Xaw3d scroll bar widget is a little bit light for my taste.
5878 We don't alter it here to let users change it according
5879 to their taste with `emacs*verticalScrollBar.background: xxx'. */
5880 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5881 XtSetArg (av[ac], XtNorientation, XtorientHorizontal); ++ac;
5882 /* For smoother scrolling with Xaw3d -sm */
5883 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
5885 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5886 if (pixel != -1)
5888 XtSetArg (av[ac], XtNforeground, pixel);
5889 ++ac;
5892 pixel = f->output_data.x->scroll_bar_background_pixel;
5893 if (pixel != -1)
5895 XtSetArg (av[ac], XtNbackground, pixel);
5896 ++ac;
5899 /* Top/bottom shadow colors. */
5901 /* Allocate them, if necessary. */
5902 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
5904 pixel = f->output_data.x->scroll_bar_background_pixel;
5905 if (pixel != -1)
5907 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5908 FRAME_X_COLORMAP (f),
5909 &pixel, 1.2, 0x8000))
5910 pixel = -1;
5911 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
5914 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5916 pixel = f->output_data.x->scroll_bar_background_pixel;
5917 if (pixel != -1)
5919 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5920 FRAME_X_COLORMAP (f),
5921 &pixel, 0.6, 0x4000))
5922 pixel = -1;
5923 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
5927 #ifdef XtNbeNiceToColormap
5928 /* Tell the toolkit about them. */
5929 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
5930 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5931 /* We tried to allocate a color for the top/bottom shadow, and
5932 failed, so tell Xaw3d to use dithering instead. */
5933 /* But only if we have a small colormap. Xaw3d can allocate nice
5934 colors itself. */
5936 XtSetArg (av[ac], XtNbeNiceToColormap,
5937 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
5938 ++ac;
5940 else
5941 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
5942 be more consistent with other emacs 3d colors, and since Xaw3d is
5943 not good at dealing with allocation failure. */
5945 /* This tells Xaw3d to use real colors instead of dithering for
5946 the shadows. */
5947 XtSetArg (av[ac], XtNbeNiceToColormap, False);
5948 ++ac;
5950 /* Specify the colors. */
5951 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
5952 if (pixel != -1)
5954 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
5955 ++ac;
5957 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
5958 if (pixel != -1)
5960 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
5961 ++ac;
5964 #endif
5966 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
5967 f->output_data.x->edit_widget, av, ac);
5970 char const *initial = "";
5971 char const *val = initial;
5972 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
5973 #ifdef XtNarrowScrollbars
5974 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
5975 #endif
5976 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
5977 if (xaw3d_arrow_scroll || val == initial)
5978 { /* ARROW_SCROLL */
5979 xaw3d_arrow_scroll = True;
5980 /* Isn't that just a personal preference ? --Stef */
5981 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
5985 /* Define callbacks. */
5986 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
5987 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
5988 (XtPointer) bar);
5990 /* Realize the widget. Only after that is the X window created. */
5991 XtRealizeWidget (widget);
5993 #endif /* !USE_MOTIF */
5995 /* Install an action hook that lets us detect when the user
5996 finishes interacting with a scroll bar. */
5997 if (horizontal_action_hook_id == 0)
5998 horizontal_action_hook_id
5999 = XtAppAddActionHook (Xt_app_con, xt_horizontal_action_hook, 0);
6001 /* Remember X window and widget in the scroll bar vector. */
6002 SET_SCROLL_BAR_X_WIDGET (bar, widget);
6003 xwindow = XtWindow (widget);
6004 bar->x_window = xwindow;
6005 bar->whole = 1;
6006 bar->horizontal = true;
6008 unblock_input ();
6010 #endif /* not USE_GTK */
6013 /* Set the thumb size and position of scroll bar BAR. We are currently
6014 displaying PORTION out of a whole WHOLE, and our position POSITION. */
6016 #ifdef USE_GTK
6017 static void
6018 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
6020 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
6023 static void
6024 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
6026 xg_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
6029 #else /* not USE_GTK */
6030 static void
6031 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
6032 int whole)
6034 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6035 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6036 float top, shown;
6038 block_input ();
6040 #ifdef USE_MOTIF
6042 if (scroll_bar_adjust_thumb_portion_p)
6044 /* We use an estimate of 30 chars per line rather than the real
6045 `portion' value. This has the disadvantage that the thumb size
6046 is not very representative, but it makes our life a lot easier.
6047 Otherwise, we have to constantly adjust the thumb size, which
6048 we can't always do quickly enough: while dragging, the size of
6049 the thumb might prevent the user from dragging the thumb all the
6050 way to the end. but Motif and some versions of Xaw3d don't allow
6051 updating the thumb size while dragging. Also, even if we can update
6052 its size, the update will often happen too late.
6053 If you don't believe it, check out revision 1.650 of xterm.c to see
6054 what hoops we were going through and the still poor behavior we got. */
6055 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
6056 /* When the thumb is at the bottom, position == whole.
6057 So we need to increase `whole' to make space for the thumb. */
6058 whole += portion;
6061 if (whole <= 0)
6062 top = 0, shown = 1;
6063 else
6065 top = (float) position / whole;
6066 shown = (float) portion / whole;
6069 if (bar->dragging == -1)
6071 int size, value;
6073 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
6074 is the scroll bar's maximum and MIN is the scroll bar's minimum
6075 value. */
6076 size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
6078 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
6079 value = top * XM_SB_MAX;
6080 value = min (value, XM_SB_MAX - size);
6082 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6084 #else /* !USE_MOTIF i.e. use Xaw */
6086 if (whole == 0)
6087 top = 0, shown = 1;
6088 else
6090 top = (float) position / whole;
6091 shown = (float) portion / whole;
6095 float old_top, old_shown;
6096 Dimension height;
6097 XtVaGetValues (widget,
6098 XtNtopOfThumb, &old_top,
6099 XtNshown, &old_shown,
6100 XtNheight, &height,
6101 NULL);
6103 /* Massage the top+shown values. */
6104 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
6105 top = max (0, min (1, top));
6106 else
6107 top = old_top;
6108 #if ! defined (HAVE_XAW3D)
6109 /* With Xaw, 'top' values too closer to 1.0 may
6110 cause the thumb to disappear. Fix that. */
6111 top = min (top, 0.99f);
6112 #endif
6113 /* Keep two pixels available for moving the thumb down. */
6114 shown = max (0, min (1 - top - (2.0f / height), shown));
6115 #if ! defined (HAVE_XAW3D)
6116 /* Likewise with too small 'shown'. */
6117 shown = max (shown, 0.01f);
6118 #endif
6120 /* If the call to XawScrollbarSetThumb below doesn't seem to
6121 work, check that 'NARROWPROTO' is defined in src/config.h.
6122 If this is not so, most likely you need to fix configure. */
6123 if (top != old_top || shown != old_shown)
6125 if (bar->dragging == -1)
6126 XawScrollbarSetThumb (widget, top, shown);
6127 else
6129 /* Try to make the scrolling a tad smoother. */
6130 if (!xaw3d_pick_top)
6131 shown = min (shown, old_shown);
6133 XawScrollbarSetThumb (widget, top, shown);
6137 #endif /* !USE_MOTIF */
6139 unblock_input ();
6142 static void
6143 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
6144 int whole)
6146 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6147 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6148 float top, shown;
6150 block_input ();
6152 #ifdef USE_MOTIF
6153 bar->whole = whole;
6154 shown = (float) portion / whole;
6155 top = (float) position / (whole - portion);
6157 int size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
6158 int value = clip_to_bounds (0, top * (XM_SB_MAX - size), XM_SB_MAX - size);
6160 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6162 #else /* !USE_MOTIF i.e. use Xaw */
6163 bar->whole = whole;
6164 if (whole == 0)
6165 top = 0, shown = 1;
6166 else
6168 top = (float) position / whole;
6169 shown = (float) portion / whole;
6173 float old_top, old_shown;
6174 Dimension height;
6175 XtVaGetValues (widget,
6176 XtNtopOfThumb, &old_top,
6177 XtNshown, &old_shown,
6178 XtNheight, &height,
6179 NULL);
6181 #if false
6182 /* Massage the top+shown values. */
6183 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
6184 top = max (0, min (1, top));
6185 else
6186 top = old_top;
6187 #if ! defined (HAVE_XAW3D)
6188 /* With Xaw, 'top' values too closer to 1.0 may
6189 cause the thumb to disappear. Fix that. */
6190 top = min (top, 0.99f);
6191 #endif
6192 /* Keep two pixels available for moving the thumb down. */
6193 shown = max (0, min (1 - top - (2.0f / height), shown));
6194 #if ! defined (HAVE_XAW3D)
6195 /* Likewise with too small 'shown'. */
6196 shown = max (shown, 0.01f);
6197 #endif
6198 #endif
6200 /* If the call to XawScrollbarSetThumb below doesn't seem to
6201 work, check that 'NARROWPROTO' is defined in src/config.h.
6202 If this is not so, most likely you need to fix configure. */
6203 XawScrollbarSetThumb (widget, top, shown);
6204 #if false
6205 if (top != old_top || shown != old_shown)
6207 if (bar->dragging == -1)
6208 XawScrollbarSetThumb (widget, top, shown);
6209 else
6211 /* Try to make the scrolling a tad smoother. */
6212 if (!xaw3d_pick_top)
6213 shown = min (shown, old_shown);
6215 XawScrollbarSetThumb (widget, top, shown);
6218 #endif
6220 #endif /* !USE_MOTIF */
6222 unblock_input ();
6224 #endif /* not USE_GTK */
6226 #endif /* USE_TOOLKIT_SCROLL_BARS */
6230 /************************************************************************
6231 Scroll bars, general
6232 ************************************************************************/
6234 /* Create a scroll bar and return the scroll bar vector for it. W is
6235 the Emacs window on which to create the scroll bar. TOP, LEFT,
6236 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
6237 scroll bar. */
6239 static struct scroll_bar *
6240 x_scroll_bar_create (struct window *w, int top, int left,
6241 int width, int height, bool horizontal)
6243 struct frame *f = XFRAME (w->frame);
6244 struct scroll_bar *bar
6245 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
6246 Lisp_Object barobj;
6248 block_input ();
6250 #ifdef USE_TOOLKIT_SCROLL_BARS
6251 if (horizontal)
6252 x_create_horizontal_toolkit_scroll_bar (f, bar);
6253 else
6254 x_create_toolkit_scroll_bar (f, bar);
6255 #else /* not USE_TOOLKIT_SCROLL_BARS */
6257 XSetWindowAttributes a;
6258 unsigned long mask;
6259 Window window;
6261 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
6262 if (a.background_pixel == -1)
6263 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
6265 a.event_mask = (ButtonPressMask | ButtonReleaseMask
6266 | ButtonMotionMask | PointerMotionHintMask
6267 | ExposureMask);
6268 a.cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
6270 mask = (CWBackPixel | CWEventMask | CWCursor);
6272 /* Clear the area of W that will serve as a scroll bar. This is
6273 for the case that a window has been split horizontally. In
6274 this case, no clear_frame is generated to reduce flickering. */
6275 if (width > 0 && window_box_height (w) > 0)
6276 x_clear_area (f, left, top, width, window_box_height (w));
6278 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6279 /* Position and size of scroll bar. */
6280 left, top, width, height,
6281 /* Border width, depth, class, and visual. */
6283 CopyFromParent,
6284 CopyFromParent,
6285 CopyFromParent,
6286 /* Attributes. */
6287 mask, &a);
6288 bar->x_window = window;
6290 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6292 XSETWINDOW (bar->window, w);
6293 bar->top = top;
6294 bar->left = left;
6295 bar->width = width;
6296 bar->height = height;
6297 bar->start = 0;
6298 bar->end = 0;
6299 bar->dragging = -1;
6300 bar->horizontal = horizontal;
6301 #if defined (USE_TOOLKIT_SCROLL_BARS) && defined (USE_LUCID)
6302 bar->last_seen_part = scroll_bar_nowhere;
6303 #endif
6305 /* Add bar to its frame's list of scroll bars. */
6306 bar->next = FRAME_SCROLL_BARS (f);
6307 bar->prev = Qnil;
6308 XSETVECTOR (barobj, bar);
6309 fset_scroll_bars (f, barobj);
6310 if (!NILP (bar->next))
6311 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6313 /* Map the window/widget. */
6314 #ifdef USE_TOOLKIT_SCROLL_BARS
6316 #ifdef USE_GTK
6317 if (horizontal)
6318 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top,
6319 left, width, max (height, 1));
6320 else
6321 xg_update_scrollbar_pos (f, bar->x_window, top,
6322 left, width, max (height, 1));
6323 #else /* not USE_GTK */
6324 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6325 XtConfigureWidget (scroll_bar, left, top, width, max (height, 1), 0);
6326 XtMapWidget (scroll_bar);
6327 #endif /* not USE_GTK */
6329 #else /* not USE_TOOLKIT_SCROLL_BARS */
6330 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
6331 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6333 unblock_input ();
6334 return bar;
6338 #ifndef USE_TOOLKIT_SCROLL_BARS
6340 /* Draw BAR's handle in the proper position.
6342 If the handle is already drawn from START to END, don't bother
6343 redrawing it, unless REBUILD; in that case, always
6344 redraw it. (REBUILD is handy for drawing the handle after expose
6345 events.)
6347 Normally, we want to constrain the start and end of the handle to
6348 fit inside its rectangle, but if the user is dragging the scroll
6349 bar handle, we want to let them drag it down all the way, so that
6350 the bar's top is as far down as it goes; otherwise, there's no way
6351 to move to the very end of the buffer. */
6353 static void
6354 x_scroll_bar_set_handle (struct scroll_bar *bar, int start, int end,
6355 bool rebuild)
6357 bool dragging = bar->dragging != -1;
6358 Window w = bar->x_window;
6359 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6360 GC gc = f->output_data.x->normal_gc;
6362 /* If the display is already accurate, do nothing. */
6363 if (! rebuild
6364 && start == bar->start
6365 && end == bar->end)
6366 return;
6368 block_input ();
6371 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
6372 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
6373 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
6375 /* Make sure the values are reasonable, and try to preserve
6376 the distance between start and end. */
6378 int length = end - start;
6380 if (start < 0)
6381 start = 0;
6382 else if (start > top_range)
6383 start = top_range;
6384 end = start + length;
6386 if (end < start)
6387 end = start;
6388 else if (end > top_range && ! dragging)
6389 end = top_range;
6392 /* Store the adjusted setting in the scroll bar. */
6393 bar->start = start;
6394 bar->end = end;
6396 /* Clip the end position, just for display. */
6397 if (end > top_range)
6398 end = top_range;
6400 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
6401 below top positions, to make sure the handle is always at least
6402 that many pixels tall. */
6403 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
6405 /* Draw the empty space above the handle. Note that we can't clear
6406 zero-height areas; that means "clear to end of window." */
6407 if ((inside_width > 0) && (start > 0))
6408 x_clear_area1 (FRAME_X_DISPLAY (f), w,
6409 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6410 VERTICAL_SCROLL_BAR_TOP_BORDER,
6411 inside_width, start, False);
6413 /* Change to proper foreground color if one is specified. */
6414 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6415 XSetForeground (FRAME_X_DISPLAY (f), gc,
6416 f->output_data.x->scroll_bar_foreground_pixel);
6418 /* Draw the handle itself. */
6419 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
6420 /* x, y, width, height */
6421 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6422 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
6423 inside_width, end - start);
6425 /* Restore the foreground color of the GC if we changed it above. */
6426 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6427 XSetForeground (FRAME_X_DISPLAY (f), gc,
6428 FRAME_FOREGROUND_PIXEL (f));
6430 /* Draw the empty space below the handle. Note that we can't
6431 clear zero-height areas; that means "clear to end of window." */
6432 if ((inside_width > 0) && (end < inside_height))
6433 x_clear_area1 (FRAME_X_DISPLAY (f), w,
6434 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6435 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
6436 inside_width, inside_height - end, False);
6439 unblock_input ();
6442 #endif /* !USE_TOOLKIT_SCROLL_BARS */
6444 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
6445 nil. */
6447 static void
6448 x_scroll_bar_remove (struct scroll_bar *bar)
6450 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6451 block_input ();
6453 #ifdef USE_TOOLKIT_SCROLL_BARS
6454 #ifdef USE_GTK
6455 xg_remove_scroll_bar (f, bar->x_window);
6456 #else /* not USE_GTK */
6457 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
6458 #endif /* not USE_GTK */
6459 #else
6460 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
6461 #endif
6463 /* Dissociate this scroll bar from its window. */
6464 if (bar->horizontal)
6465 wset_horizontal_scroll_bar (XWINDOW (bar->window), Qnil);
6466 else
6467 wset_vertical_scroll_bar (XWINDOW (bar->window), Qnil);
6469 unblock_input ();
6473 /* Set the handle of the vertical scroll bar for WINDOW to indicate
6474 that we are displaying PORTION characters out of a total of WHOLE
6475 characters, starting at POSITION. If WINDOW has no scroll bar,
6476 create one. */
6478 static void
6479 XTset_vertical_scroll_bar (struct window *w, int portion, int whole, int position)
6481 struct frame *f = XFRAME (w->frame);
6482 Lisp_Object barobj;
6483 struct scroll_bar *bar;
6484 int top, height, left, width;
6485 int window_y, window_height;
6487 /* Get window dimensions. */
6488 window_box (w, ANY_AREA, 0, &window_y, 0, &window_height);
6489 top = window_y;
6490 height = window_height;
6491 left = WINDOW_SCROLL_BAR_AREA_X (w);
6492 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
6494 /* Does the scroll bar exist yet? */
6495 if (NILP (w->vertical_scroll_bar))
6497 if (width > 0 && height > 0)
6499 block_input ();
6500 x_clear_area (f, left, top, width, height);
6501 unblock_input ();
6504 bar = x_scroll_bar_create (w, top, left, width, max (height, 1), false);
6506 else
6508 /* It may just need to be moved and resized. */
6509 unsigned int mask = 0;
6511 bar = XSCROLL_BAR (w->vertical_scroll_bar);
6513 block_input ();
6515 if (left != bar->left)
6516 mask |= CWX;
6517 if (top != bar->top)
6518 mask |= CWY;
6519 if (width != bar->width)
6520 mask |= CWWidth;
6521 if (height != bar->height)
6522 mask |= CWHeight;
6524 #ifdef USE_TOOLKIT_SCROLL_BARS
6526 /* Move/size the scroll bar widget. */
6527 if (mask)
6529 /* Since toolkit scroll bars are smaller than the space reserved
6530 for them on the frame, we have to clear "under" them. */
6531 if (width > 0 && height > 0)
6532 x_clear_area (f, left, top, width, height);
6533 #ifdef USE_GTK
6534 xg_update_scrollbar_pos (f, bar->x_window, top,
6535 left, width, max (height, 1));
6536 #else /* not USE_GTK */
6537 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
6538 left, top, width, max (height, 1), 0);
6539 #endif /* not USE_GTK */
6541 #else /* not USE_TOOLKIT_SCROLL_BARS */
6543 /* Move/size the scroll bar window. */
6544 if (mask)
6546 XWindowChanges wc;
6548 wc.x = left;
6549 wc.y = top;
6550 wc.width = width;
6551 wc.height = height;
6552 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
6553 mask, &wc);
6556 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6558 /* Remember new settings. */
6559 bar->left = left;
6560 bar->top = top;
6561 bar->width = width;
6562 bar->height = height;
6564 unblock_input ();
6567 #ifdef USE_TOOLKIT_SCROLL_BARS
6568 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
6569 #else /* not USE_TOOLKIT_SCROLL_BARS */
6570 /* Set the scroll bar's current state, unless we're currently being
6571 dragged. */
6572 if (bar->dragging == -1)
6574 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
6576 if (whole == 0)
6577 x_scroll_bar_set_handle (bar, 0, top_range, false);
6578 else
6580 int start = ((double) position * top_range) / whole;
6581 int end = ((double) (position + portion) * top_range) / whole;
6582 x_scroll_bar_set_handle (bar, start, end, false);
6585 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6587 XSETVECTOR (barobj, bar);
6588 wset_vertical_scroll_bar (w, barobj);
6592 static void
6593 XTset_horizontal_scroll_bar (struct window *w, int portion, int whole, int position)
6595 struct frame *f = XFRAME (w->frame);
6596 Lisp_Object barobj;
6597 struct scroll_bar *bar;
6598 int top, height, left, width;
6599 int window_x, window_width;
6600 int pixel_width = WINDOW_PIXEL_WIDTH (w);
6602 /* Get window dimensions. */
6603 window_box (w, ANY_AREA, &window_x, 0, &window_width, 0);
6604 left = window_x;
6605 width = window_width;
6606 top = WINDOW_SCROLL_BAR_AREA_Y (w);
6607 height = WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
6609 /* Does the scroll bar exist yet? */
6610 if (NILP (w->horizontal_scroll_bar))
6612 if (width > 0 && height > 0)
6614 block_input ();
6616 /* Clear also part between window_width and
6617 WINDOW_PIXEL_WIDTH. */
6618 x_clear_area (f, left, top, pixel_width, height);
6619 unblock_input ();
6622 bar = x_scroll_bar_create (w, top, left, width, height, true);
6624 else
6626 /* It may just need to be moved and resized. */
6627 unsigned int mask = 0;
6629 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
6631 block_input ();
6633 if (left != bar->left)
6634 mask |= CWX;
6635 if (top != bar->top)
6636 mask |= CWY;
6637 if (width != bar->width)
6638 mask |= CWWidth;
6639 if (height != bar->height)
6640 mask |= CWHeight;
6642 #ifdef USE_TOOLKIT_SCROLL_BARS
6643 /* Move/size the scroll bar widget. */
6644 if (mask)
6646 /* Since toolkit scroll bars are smaller than the space reserved
6647 for them on the frame, we have to clear "under" them. */
6648 if (width > 0 && height > 0)
6649 x_clear_area (f,
6650 WINDOW_LEFT_EDGE_X (w), top,
6651 pixel_width - WINDOW_RIGHT_DIVIDER_WIDTH (w), height);
6652 #ifdef USE_GTK
6653 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top, left,
6654 width, height);
6655 #else /* not USE_GTK */
6656 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
6657 left, top, width, height, 0);
6658 #endif /* not USE_GTK */
6660 #else /* not USE_TOOLKIT_SCROLL_BARS */
6662 /* Clear areas not covered by the scroll bar because it's not as
6663 wide as the area reserved for it. This makes sure a
6664 previous mode line display is cleared after C-x 2 C-x 1, for
6665 example. */
6667 int area_height = WINDOW_CONFIG_SCROLL_BAR_HEIGHT (w);
6668 int rest = area_height - height;
6669 if (rest > 0 && width > 0)
6670 x_clear_area (f, left, top, width, rest);
6673 /* Move/size the scroll bar window. */
6674 if (mask)
6676 XWindowChanges wc;
6678 wc.x = left;
6679 wc.y = top;
6680 wc.width = width;
6681 wc.height = height;
6682 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
6683 mask, &wc);
6686 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6688 /* Remember new settings. */
6689 bar->left = left;
6690 bar->top = top;
6691 bar->width = width;
6692 bar->height = height;
6694 unblock_input ();
6697 #ifdef USE_TOOLKIT_SCROLL_BARS
6698 x_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
6699 #else /* not USE_TOOLKIT_SCROLL_BARS */
6700 /* Set the scroll bar's current state, unless we're currently being
6701 dragged. */
6702 if (bar->dragging == -1)
6704 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, width);
6706 if (whole == 0)
6707 x_scroll_bar_set_handle (bar, 0, left_range, false);
6708 else
6710 int start = ((double) position * left_range) / whole;
6711 int end = ((double) (position + portion) * left_range) / whole;
6712 x_scroll_bar_set_handle (bar, start, end, false);
6715 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6717 XSETVECTOR (barobj, bar);
6718 wset_horizontal_scroll_bar (w, barobj);
6722 /* The following three hooks are used when we're doing a thorough
6723 redisplay of the frame. We don't explicitly know which scroll bars
6724 are going to be deleted, because keeping track of when windows go
6725 away is a real pain - "Can you say set-window-configuration, boys
6726 and girls?" Instead, we just assert at the beginning of redisplay
6727 that *all* scroll bars are to be removed, and then save a scroll bar
6728 from the fiery pit when we actually redisplay its window. */
6730 /* Arrange for all scroll bars on FRAME to be removed at the next call
6731 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
6732 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
6734 static void
6735 XTcondemn_scroll_bars (struct frame *frame)
6737 if (!NILP (FRAME_SCROLL_BARS (frame)))
6739 if (!NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
6741 /* Prepend scrollbars to already condemned ones. */
6742 Lisp_Object last = FRAME_SCROLL_BARS (frame);
6744 while (!NILP (XSCROLL_BAR (last)->next))
6745 last = XSCROLL_BAR (last)->next;
6747 XSCROLL_BAR (last)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
6748 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = last;
6751 fset_condemned_scroll_bars (frame, FRAME_SCROLL_BARS (frame));
6752 fset_scroll_bars (frame, Qnil);
6757 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
6758 Note that WINDOW isn't necessarily condemned at all. */
6760 static void
6761 XTredeem_scroll_bar (struct window *w)
6763 struct scroll_bar *bar;
6764 Lisp_Object barobj;
6765 struct frame *f;
6767 /* We can't redeem this window's scroll bar if it doesn't have one. */
6768 if (NILP (w->vertical_scroll_bar) && NILP (w->horizontal_scroll_bar))
6769 emacs_abort ();
6771 if (!NILP (w->vertical_scroll_bar) && WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
6773 bar = XSCROLL_BAR (w->vertical_scroll_bar);
6774 /* Unlink it from the condemned list. */
6775 f = XFRAME (WINDOW_FRAME (w));
6776 if (NILP (bar->prev))
6778 /* If the prev pointer is nil, it must be the first in one of
6779 the lists. */
6780 if (EQ (FRAME_SCROLL_BARS (f), w->vertical_scroll_bar))
6781 /* It's not condemned. Everything's fine. */
6782 goto horizontal;
6783 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
6784 w->vertical_scroll_bar))
6785 fset_condemned_scroll_bars (f, bar->next);
6786 else
6787 /* If its prev pointer is nil, it must be at the front of
6788 one or the other! */
6789 emacs_abort ();
6791 else
6792 XSCROLL_BAR (bar->prev)->next = bar->next;
6794 if (! NILP (bar->next))
6795 XSCROLL_BAR (bar->next)->prev = bar->prev;
6797 bar->next = FRAME_SCROLL_BARS (f);
6798 bar->prev = Qnil;
6799 XSETVECTOR (barobj, bar);
6800 fset_scroll_bars (f, barobj);
6801 if (! NILP (bar->next))
6802 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6805 horizontal:
6806 if (!NILP (w->horizontal_scroll_bar) && WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
6808 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
6809 /* Unlink it from the condemned list. */
6810 f = XFRAME (WINDOW_FRAME (w));
6811 if (NILP (bar->prev))
6813 /* If the prev pointer is nil, it must be the first in one of
6814 the lists. */
6815 if (EQ (FRAME_SCROLL_BARS (f), w->horizontal_scroll_bar))
6816 /* It's not condemned. Everything's fine. */
6817 return;
6818 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
6819 w->horizontal_scroll_bar))
6820 fset_condemned_scroll_bars (f, bar->next);
6821 else
6822 /* If its prev pointer is nil, it must be at the front of
6823 one or the other! */
6824 emacs_abort ();
6826 else
6827 XSCROLL_BAR (bar->prev)->next = bar->next;
6829 if (! NILP (bar->next))
6830 XSCROLL_BAR (bar->next)->prev = bar->prev;
6832 bar->next = FRAME_SCROLL_BARS (f);
6833 bar->prev = Qnil;
6834 XSETVECTOR (barobj, bar);
6835 fset_scroll_bars (f, barobj);
6836 if (! NILP (bar->next))
6837 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6841 /* Remove all scroll bars on FRAME that haven't been saved since the
6842 last call to `*condemn_scroll_bars_hook'. */
6844 static void
6845 XTjudge_scroll_bars (struct frame *f)
6847 Lisp_Object bar, next;
6849 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
6851 /* Clear out the condemned list now so we won't try to process any
6852 more events on the hapless scroll bars. */
6853 fset_condemned_scroll_bars (f, Qnil);
6855 for (; ! NILP (bar); bar = next)
6857 struct scroll_bar *b = XSCROLL_BAR (bar);
6859 x_scroll_bar_remove (b);
6861 next = b->next;
6862 b->next = b->prev = Qnil;
6865 /* Now there should be no references to the condemned scroll bars,
6866 and they should get garbage-collected. */
6870 #ifndef USE_TOOLKIT_SCROLL_BARS
6871 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
6872 is a no-op when using toolkit scroll bars.
6874 This may be called from a signal handler, so we have to ignore GC
6875 mark bits. */
6877 static void
6878 x_scroll_bar_expose (struct scroll_bar *bar, const XEvent *event)
6880 Window w = bar->x_window;
6881 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6882 GC gc = f->output_data.x->normal_gc;
6884 block_input ();
6886 x_scroll_bar_set_handle (bar, bar->start, bar->end, true);
6888 /* Switch to scroll bar foreground color. */
6889 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6890 XSetForeground (FRAME_X_DISPLAY (f), gc,
6891 f->output_data.x->scroll_bar_foreground_pixel);
6893 /* Draw a one-pixel border just inside the edges of the scroll bar. */
6894 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
6895 /* x, y, width, height */
6896 0, 0, bar->width - 1, bar->height - 1);
6898 /* Restore the foreground color of the GC if we changed it above. */
6899 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6900 XSetForeground (FRAME_X_DISPLAY (f), gc,
6901 FRAME_FOREGROUND_PIXEL (f));
6903 unblock_input ();
6906 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6908 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
6909 is set to something other than NO_EVENT, it is enqueued.
6911 This may be called from a signal handler, so we have to ignore GC
6912 mark bits. */
6915 static void
6916 x_scroll_bar_handle_click (struct scroll_bar *bar,
6917 const XEvent *event,
6918 struct input_event *emacs_event)
6920 if (! WINDOWP (bar->window))
6921 emacs_abort ();
6923 emacs_event->kind = (bar->horizontal
6924 ? HORIZONTAL_SCROLL_BAR_CLICK_EVENT
6925 : SCROLL_BAR_CLICK_EVENT);
6926 emacs_event->code = event->xbutton.button - Button1;
6927 emacs_event->modifiers
6928 = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO
6929 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
6930 event->xbutton.state)
6931 | (event->type == ButtonRelease
6932 ? up_modifier
6933 : down_modifier));
6934 emacs_event->frame_or_window = bar->window;
6935 emacs_event->arg = Qnil;
6936 emacs_event->timestamp = event->xbutton.time;
6937 if (bar->horizontal)
6939 int left_range
6940 = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
6941 int x = event->xbutton.x - HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
6943 if (x < 0) x = 0;
6944 if (x > left_range) x = left_range;
6946 if (x < bar->start)
6947 emacs_event->part = scroll_bar_before_handle;
6948 else if (x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
6949 emacs_event->part = scroll_bar_horizontal_handle;
6950 else
6951 emacs_event->part = scroll_bar_after_handle;
6953 #ifndef USE_TOOLKIT_SCROLL_BARS
6954 /* If the user has released the handle, set it to its final position. */
6955 if (event->type == ButtonRelease && bar->dragging != -1)
6957 int new_start = - bar->dragging;
6958 int new_end = new_start + bar->end - bar->start;
6960 x_scroll_bar_set_handle (bar, new_start, new_end, false);
6961 bar->dragging = -1;
6963 #endif
6965 XSETINT (emacs_event->x, left_range);
6966 XSETINT (emacs_event->y, x);
6968 else
6970 int top_range
6971 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
6972 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
6974 if (y < 0) y = 0;
6975 if (y > top_range) y = top_range;
6977 if (y < bar->start)
6978 emacs_event->part = scroll_bar_above_handle;
6979 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
6980 emacs_event->part = scroll_bar_handle;
6981 else
6982 emacs_event->part = scroll_bar_below_handle;
6984 #ifndef USE_TOOLKIT_SCROLL_BARS
6985 /* If the user has released the handle, set it to its final position. */
6986 if (event->type == ButtonRelease && bar->dragging != -1)
6988 int new_start = y - bar->dragging;
6989 int new_end = new_start + bar->end - bar->start;
6991 x_scroll_bar_set_handle (bar, new_start, new_end, false);
6992 bar->dragging = -1;
6994 #endif
6996 XSETINT (emacs_event->x, y);
6997 XSETINT (emacs_event->y, top_range);
7001 #ifndef USE_TOOLKIT_SCROLL_BARS
7003 /* Handle some mouse motion while someone is dragging the scroll bar.
7005 This may be called from a signal handler, so we have to ignore GC
7006 mark bits. */
7008 static void
7009 x_scroll_bar_note_movement (struct scroll_bar *bar,
7010 const XMotionEvent *event)
7012 struct frame *f = XFRAME (XWINDOW (bar->window)->frame);
7013 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
7015 dpyinfo->last_mouse_movement_time = event->time;
7016 dpyinfo->last_mouse_scroll_bar = bar;
7017 f->mouse_moved = true;
7019 /* If we're dragging the bar, display it. */
7020 if (bar->dragging != -1)
7022 /* Where should the handle be now? */
7023 int new_start = event->y - bar->dragging;
7025 if (new_start != bar->start)
7027 int new_end = new_start + bar->end - bar->start;
7029 x_scroll_bar_set_handle (bar, new_start, new_end, false);
7034 #endif /* !USE_TOOLKIT_SCROLL_BARS */
7036 /* Return information to the user about the current position of the mouse
7037 on the scroll bar. */
7039 static void
7040 x_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
7041 enum scroll_bar_part *part, Lisp_Object *x,
7042 Lisp_Object *y, Time *timestamp)
7044 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
7045 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
7046 Window w = bar->x_window;
7047 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7048 int win_x, win_y;
7049 Window dummy_window;
7050 int dummy_coord;
7051 unsigned int dummy_mask;
7053 block_input ();
7055 /* Get the mouse's position relative to the scroll bar window, and
7056 report that. */
7057 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
7059 /* Root, child, root x and root y. */
7060 &dummy_window, &dummy_window,
7061 &dummy_coord, &dummy_coord,
7063 /* Position relative to scroll bar. */
7064 &win_x, &win_y,
7066 /* Mouse buttons and modifier keys. */
7067 &dummy_mask))
7069 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
7071 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
7073 if (bar->dragging != -1)
7074 win_y -= bar->dragging;
7076 if (win_y < 0)
7077 win_y = 0;
7078 if (win_y > top_range)
7079 win_y = top_range;
7081 *fp = f;
7082 *bar_window = bar->window;
7084 if (bar->dragging != -1)
7085 *part = scroll_bar_handle;
7086 else if (win_y < bar->start)
7087 *part = scroll_bar_above_handle;
7088 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
7089 *part = scroll_bar_handle;
7090 else
7091 *part = scroll_bar_below_handle;
7093 XSETINT (*x, win_y);
7094 XSETINT (*y, top_range);
7096 f->mouse_moved = false;
7097 dpyinfo->last_mouse_scroll_bar = NULL;
7098 *timestamp = dpyinfo->last_mouse_movement_time;
7101 unblock_input ();
7105 /* Return information to the user about the current position of the mouse
7106 on the scroll bar. */
7108 static void
7109 x_horizontal_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
7110 enum scroll_bar_part *part, Lisp_Object *x,
7111 Lisp_Object *y, Time *timestamp)
7113 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
7114 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
7115 Window w = bar->x_window;
7116 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7117 int win_x, win_y;
7118 Window dummy_window;
7119 int dummy_coord;
7120 unsigned int dummy_mask;
7122 block_input ();
7124 /* Get the mouse's position relative to the scroll bar window, and
7125 report that. */
7126 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
7128 /* Root, child, root x and root y. */
7129 &dummy_window, &dummy_window,
7130 &dummy_coord, &dummy_coord,
7132 /* Position relative to scroll bar. */
7133 &win_x, &win_y,
7135 /* Mouse buttons and modifier keys. */
7136 &dummy_mask))
7138 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
7140 win_x -= HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
7142 if (bar->dragging != -1)
7143 win_x -= bar->dragging;
7145 if (win_x < 0)
7146 win_x = 0;
7147 if (win_x > left_range)
7148 win_x = left_range;
7150 *fp = f;
7151 *bar_window = bar->window;
7153 if (bar->dragging != -1)
7154 *part = scroll_bar_horizontal_handle;
7155 else if (win_x < bar->start)
7156 *part = scroll_bar_before_handle;
7157 else if (win_x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
7158 *part = scroll_bar_handle;
7159 else
7160 *part = scroll_bar_after_handle;
7162 XSETINT (*y, win_x);
7163 XSETINT (*x, left_range);
7165 f->mouse_moved = false;
7166 dpyinfo->last_mouse_scroll_bar = NULL;
7167 *timestamp = dpyinfo->last_mouse_movement_time;
7170 unblock_input ();
7174 /* The screen has been cleared so we may have changed foreground or
7175 background colors, and the scroll bars may need to be redrawn.
7176 Clear out the scroll bars, and ask for expose events, so we can
7177 redraw them. */
7179 static void
7180 x_scroll_bar_clear (struct frame *f)
7182 #ifndef USE_TOOLKIT_SCROLL_BARS
7183 Lisp_Object bar;
7185 /* We can have scroll bars even if this is 0,
7186 if we just turned off scroll bar mode.
7187 But in that case we should not clear them. */
7188 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
7189 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
7190 bar = XSCROLL_BAR (bar)->next)
7191 XClearArea (FRAME_X_DISPLAY (f),
7192 XSCROLL_BAR (bar)->x_window,
7193 0, 0, 0, 0, True);
7194 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7197 #ifdef ENABLE_CHECKING
7199 /* Record the last 100 characters stored
7200 to help debug the loss-of-chars-during-GC problem. */
7202 static int temp_index;
7203 static short temp_buffer[100];
7205 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
7206 if (temp_index == ARRAYELTS (temp_buffer)) \
7207 temp_index = 0; \
7208 temp_buffer[temp_index++] = (keysym)
7210 #else /* not ENABLE_CHECKING */
7212 #define STORE_KEYSYM_FOR_DEBUG(keysym) ((void)0)
7214 #endif /* ENABLE_CHECKING */
7216 /* Set this to nonzero to fake an "X I/O error"
7217 on a particular display. */
7219 static struct x_display_info *XTread_socket_fake_io_error;
7221 /* When we find no input here, we occasionally do a no-op command
7222 to verify that the X server is still running and we can still talk with it.
7223 We try all the open displays, one by one.
7224 This variable is used for cycling thru the displays. */
7226 static struct x_display_info *next_noop_dpyinfo;
7228 enum
7230 X_EVENT_NORMAL,
7231 X_EVENT_GOTO_OUT,
7232 X_EVENT_DROP
7235 /* Filter events for the current X input method.
7236 DPYINFO is the display this event is for.
7237 EVENT is the X event to filter.
7239 Returns non-zero if the event was filtered, caller shall not process
7240 this event further.
7241 Returns zero if event is wasn't filtered. */
7243 #ifdef HAVE_X_I18N
7244 static int
7245 x_filter_event (struct x_display_info *dpyinfo, XEvent *event)
7247 /* XFilterEvent returns non-zero if the input method has
7248 consumed the event. We pass the frame's X window to
7249 XFilterEvent because that's the one for which the IC
7250 was created. */
7252 struct frame *f1 = x_any_window_to_frame (dpyinfo,
7253 event->xclient.window);
7255 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
7257 #endif
7259 #ifdef USE_GTK
7260 static int current_count;
7261 static int current_finish;
7262 static struct input_event *current_hold_quit;
7264 /* This is the filter function invoked by the GTK event loop.
7265 It is invoked before the XEvent is translated to a GdkEvent,
7266 so we have a chance to act on the event before GTK. */
7267 static GdkFilterReturn
7268 event_handler_gdk (GdkXEvent *gxev, GdkEvent *ev, gpointer data)
7270 XEvent *xev = (XEvent *) gxev;
7272 block_input ();
7273 if (current_count >= 0)
7275 struct x_display_info *dpyinfo;
7277 dpyinfo = x_display_info_for_display (xev->xany.display);
7279 #ifdef HAVE_X_I18N
7280 /* Filter events for the current X input method.
7281 GTK calls XFilterEvent but not for key press and release,
7282 so we do it here. */
7283 if ((xev->type == KeyPress || xev->type == KeyRelease)
7284 && dpyinfo
7285 && x_filter_event (dpyinfo, xev))
7287 unblock_input ();
7288 return GDK_FILTER_REMOVE;
7290 #endif
7292 if (! dpyinfo)
7293 current_finish = X_EVENT_NORMAL;
7294 else
7295 current_count
7296 += handle_one_xevent (dpyinfo, xev, &current_finish,
7297 current_hold_quit);
7299 else
7300 current_finish = x_dispatch_event (xev, xev->xany.display);
7302 unblock_input ();
7304 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
7305 return GDK_FILTER_REMOVE;
7307 return GDK_FILTER_CONTINUE;
7309 #endif /* USE_GTK */
7312 static void xembed_send_message (struct frame *f, Time,
7313 enum xembed_message,
7314 long detail, long data1, long data2);
7316 static void
7317 x_net_wm_state (struct frame *f, Window window)
7319 int value = FULLSCREEN_NONE;
7320 Lisp_Object lval = Qnil;
7321 bool sticky = false;
7323 get_current_wm_state (f, window, &value, &sticky);
7325 switch (value)
7327 case FULLSCREEN_WIDTH:
7328 lval = Qfullwidth;
7329 break;
7330 case FULLSCREEN_HEIGHT:
7331 lval = Qfullheight;
7332 break;
7333 case FULLSCREEN_BOTH:
7334 lval = Qfullboth;
7335 break;
7336 case FULLSCREEN_MAXIMIZED:
7337 lval = Qmaximized;
7338 break;
7341 frame_size_history_add
7342 (f, Qx_net_wm_state, 0, 0,
7343 list2 (get_frame_param (f, Qfullscreen), lval));
7345 store_frame_param (f, Qfullscreen, lval);
7346 /** store_frame_param (f, Qsticky, sticky ? Qt : Qnil); **/
7349 /* Handles the XEvent EVENT on display DPYINFO.
7351 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
7352 *FINISH is zero if caller should continue reading events.
7353 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
7354 *EVENT is unchanged unless we're processing KeyPress event.
7356 We return the number of characters stored into the buffer. */
7358 static int
7359 handle_one_xevent (struct x_display_info *dpyinfo,
7360 const XEvent *event,
7361 int *finish, struct input_event *hold_quit)
7363 union buffered_input_event inev;
7364 int count = 0;
7365 int do_help = 0;
7366 ptrdiff_t nbytes = 0;
7367 struct frame *any, *f = NULL;
7368 struct coding_system coding;
7369 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
7370 /* This holds the state XLookupString needs to implement dead keys
7371 and other tricks known as "compose processing". _X Window System_
7372 says that a portable program can't use this, but Stephen Gildea assures
7373 me that letting the compiler initialize it to zeros will work okay. */
7374 static XComposeStatus compose_status;
7376 USE_SAFE_ALLOCA;
7378 *finish = X_EVENT_NORMAL;
7380 EVENT_INIT (inev.ie);
7381 inev.ie.kind = NO_EVENT;
7382 inev.ie.arg = Qnil;
7384 any = x_any_window_to_frame (dpyinfo, event->xany.window);
7386 if (any && any->wait_event_type == event->type)
7387 any->wait_event_type = 0; /* Indicates we got it. */
7389 switch (event->type)
7391 case ClientMessage:
7393 if (event->xclient.message_type == dpyinfo->Xatom_wm_protocols
7394 && event->xclient.format == 32)
7396 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_take_focus)
7398 /* Use the value returned by x_any_window_to_frame
7399 because this could be the shell widget window
7400 if the frame has no title bar. */
7401 f = any;
7402 #ifdef HAVE_X_I18N
7403 /* Not quite sure this is needed -pd */
7404 if (f && FRAME_XIC (f))
7405 XSetICFocus (FRAME_XIC (f));
7406 #endif
7407 #if false
7408 /* Emacs sets WM hints whose `input' field is `true'. This
7409 instructs the WM to set the input focus automatically for
7410 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
7411 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
7412 it has set the focus. So, XSetInputFocus below is not
7413 needed.
7415 The call to XSetInputFocus below has also caused trouble. In
7416 cases where the XSetInputFocus done by the WM and the one
7417 below are temporally close (on a fast machine), the call
7418 below can generate additional FocusIn events which confuse
7419 Emacs. */
7421 /* Since we set WM_TAKE_FOCUS, we must call
7422 XSetInputFocus explicitly. But not if f is null,
7423 since that might be an event for a deleted frame. */
7424 if (f)
7426 Display *d = event->xclient.display;
7427 /* Catch and ignore errors, in case window has been
7428 iconified by a window manager such as GWM. */
7429 x_catch_errors (d);
7430 XSetInputFocus (d, event->xclient.window,
7431 /* The ICCCM says this is
7432 the only valid choice. */
7433 RevertToParent,
7434 event->xclient.data.l[1]);
7435 /* This is needed to detect the error
7436 if there is an error. */
7437 XSync (d, False);
7438 x_uncatch_errors ();
7440 /* Not certain about handling scroll bars here */
7441 #endif
7442 goto done;
7445 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_save_yourself)
7447 /* Save state modify the WM_COMMAND property to
7448 something which can reinstate us. This notifies
7449 the session manager, who's looking for such a
7450 PropertyNotify. Can restart processing when
7451 a keyboard or mouse event arrives. */
7452 /* If we have a session manager, don't set this.
7453 KDE will then start two Emacsen, one for the
7454 session manager and one for this. */
7455 #ifdef HAVE_X_SM
7456 if (! x_session_have_connection ())
7457 #endif
7459 f = x_top_window_to_frame (dpyinfo,
7460 event->xclient.window);
7461 /* This is just so we only give real data once
7462 for a single Emacs process. */
7463 if (f == SELECTED_FRAME ())
7464 XSetCommand (FRAME_X_DISPLAY (f),
7465 event->xclient.window,
7466 initial_argv, initial_argc);
7467 else if (f)
7468 XSetCommand (FRAME_X_DISPLAY (f),
7469 event->xclient.window,
7470 0, 0);
7472 goto done;
7475 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_delete_window)
7477 f = any;
7478 if (!f)
7479 goto OTHER; /* May be a dialog that is to be removed */
7481 inev.ie.kind = DELETE_WINDOW_EVENT;
7482 XSETFRAME (inev.ie.frame_or_window, f);
7483 goto done;
7486 goto done;
7489 if (event->xclient.message_type == dpyinfo->Xatom_wm_configure_denied)
7490 goto done;
7492 if (event->xclient.message_type == dpyinfo->Xatom_wm_window_moved)
7494 int new_x, new_y;
7495 f = x_window_to_frame (dpyinfo, event->xclient.window);
7497 new_x = event->xclient.data.s[0];
7498 new_y = event->xclient.data.s[1];
7500 if (f)
7502 f->left_pos = new_x;
7503 f->top_pos = new_y;
7505 goto done;
7508 #ifdef HACK_EDITRES
7509 if (event->xclient.message_type == dpyinfo->Xatom_editres)
7511 f = any;
7512 if (f)
7513 _XEditResCheckMessages (f->output_data.x->widget,
7514 NULL, (XEvent *) event, NULL);
7515 goto done;
7517 #endif /* HACK_EDITRES */
7519 if (event->xclient.message_type == dpyinfo->Xatom_DONE
7520 || event->xclient.message_type == dpyinfo->Xatom_PAGE)
7522 /* Ghostview job completed. Kill it. We could
7523 reply with "Next" if we received "Page", but we
7524 currently never do because we are interested in
7525 images, only, which should have 1 page. */
7526 Pixmap pixmap = (Pixmap) event->xclient.data.l[1];
7527 f = x_window_to_frame (dpyinfo, event->xclient.window);
7528 if (!f)
7529 goto OTHER;
7530 x_kill_gs_process (pixmap, f);
7531 expose_frame (f, 0, 0, 0, 0);
7532 goto done;
7535 #ifdef USE_TOOLKIT_SCROLL_BARS
7536 /* Scroll bar callbacks send a ClientMessage from which
7537 we construct an input_event. */
7538 if (event->xclient.message_type == dpyinfo->Xatom_Scrollbar)
7540 x_scroll_bar_to_input_event (event, &inev.ie);
7541 *finish = X_EVENT_GOTO_OUT;
7542 goto done;
7544 else if (event->xclient.message_type == dpyinfo->Xatom_Horizontal_Scrollbar)
7546 x_horizontal_scroll_bar_to_input_event (event, &inev.ie);
7547 *finish = X_EVENT_GOTO_OUT;
7548 goto done;
7550 #endif /* USE_TOOLKIT_SCROLL_BARS */
7552 /* XEmbed messages from the embedder (if any). */
7553 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
7555 enum xembed_message msg = event->xclient.data.l[1];
7556 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
7557 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7559 *finish = X_EVENT_GOTO_OUT;
7560 goto done;
7563 xft_settings_event (dpyinfo, event);
7565 f = any;
7566 if (!f)
7567 goto OTHER;
7568 if (x_handle_dnd_message (f, &event->xclient, dpyinfo, &inev.ie))
7569 *finish = X_EVENT_DROP;
7571 break;
7573 case SelectionNotify:
7574 x_display_set_last_user_time (dpyinfo, event->xselection.time);
7575 #ifdef USE_X_TOOLKIT
7576 if (! x_window_to_frame (dpyinfo, event->xselection.requestor))
7577 goto OTHER;
7578 #endif /* not USE_X_TOOLKIT */
7579 x_handle_selection_notify (&event->xselection);
7580 break;
7582 case SelectionClear: /* Someone has grabbed ownership. */
7583 x_display_set_last_user_time (dpyinfo, event->xselectionclear.time);
7584 #ifdef USE_X_TOOLKIT
7585 if (! x_window_to_frame (dpyinfo, event->xselectionclear.window))
7586 goto OTHER;
7587 #endif /* USE_X_TOOLKIT */
7589 const XSelectionClearEvent *eventp = &event->xselectionclear;
7591 inev.sie.kind = SELECTION_CLEAR_EVENT;
7592 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
7593 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
7594 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
7596 break;
7598 case SelectionRequest: /* Someone wants our selection. */
7599 x_display_set_last_user_time (dpyinfo, event->xselectionrequest.time);
7600 #ifdef USE_X_TOOLKIT
7601 if (!x_window_to_frame (dpyinfo, event->xselectionrequest.owner))
7602 goto OTHER;
7603 #endif /* USE_X_TOOLKIT */
7605 const XSelectionRequestEvent *eventp = &event->xselectionrequest;
7607 inev.sie.kind = SELECTION_REQUEST_EVENT;
7608 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
7609 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
7610 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
7611 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
7612 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
7613 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
7615 break;
7617 case PropertyNotify:
7618 x_display_set_last_user_time (dpyinfo, event->xproperty.time);
7619 f = x_top_window_to_frame (dpyinfo, event->xproperty.window);
7620 if (f && event->xproperty.atom == dpyinfo->Xatom_net_wm_state)
7622 bool not_hidden = x_handle_net_wm_state (f, &event->xproperty);
7623 if (not_hidden && FRAME_ICONIFIED_P (f))
7625 /* Gnome shell does not iconify us when C-z is pressed.
7626 It hides the frame. So if our state says we aren't
7627 hidden anymore, treat it as deiconified. */
7628 SET_FRAME_VISIBLE (f, 1);
7629 SET_FRAME_ICONIFIED (f, false);
7630 f->output_data.x->has_been_visible = true;
7631 inev.ie.kind = DEICONIFY_EVENT;
7632 XSETFRAME (inev.ie.frame_or_window, f);
7634 else if (! not_hidden && ! FRAME_ICONIFIED_P (f))
7636 SET_FRAME_VISIBLE (f, 0);
7637 SET_FRAME_ICONIFIED (f, true);
7638 inev.ie.kind = ICONIFY_EVENT;
7639 XSETFRAME (inev.ie.frame_or_window, f);
7643 x_handle_property_notify (&event->xproperty);
7644 xft_settings_event (dpyinfo, event);
7645 goto OTHER;
7647 case ReparentNotify:
7648 f = x_top_window_to_frame (dpyinfo, event->xreparent.window);
7649 if (f)
7651 f->output_data.x->parent_desc = event->xreparent.parent;
7652 x_real_positions (f, &f->left_pos, &f->top_pos);
7654 /* Perhaps reparented due to a WM restart. Reset this. */
7655 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
7656 FRAME_DISPLAY_INFO (f)->net_supported_window = 0;
7658 x_set_frame_alpha (f);
7660 goto OTHER;
7662 case Expose:
7663 f = x_window_to_frame (dpyinfo, event->xexpose.window);
7664 if (f)
7666 if (!FRAME_VISIBLE_P (f))
7668 SET_FRAME_VISIBLE (f, 1);
7669 SET_FRAME_ICONIFIED (f, false);
7670 f->output_data.x->has_been_visible = true;
7671 SET_FRAME_GARBAGED (f);
7673 else
7675 #ifdef USE_GTK
7676 /* This seems to be needed for GTK 2.6 and later, see
7677 http://debbugs.gnu.org/cgi/bugreport.cgi?bug=15398. */
7678 x_clear_area (f,
7679 event->xexpose.x, event->xexpose.y,
7680 event->xexpose.width, event->xexpose.height);
7681 #endif
7682 expose_frame (f, event->xexpose.x, event->xexpose.y,
7683 event->xexpose.width, event->xexpose.height);
7686 else
7688 #ifndef USE_TOOLKIT_SCROLL_BARS
7689 struct scroll_bar *bar;
7690 #endif
7691 #if defined USE_LUCID
7692 /* Submenus of the Lucid menu bar aren't widgets
7693 themselves, so there's no way to dispatch events
7694 to them. Recognize this case separately. */
7696 Widget widget = x_window_to_menu_bar (event->xexpose.window);
7697 if (widget)
7698 xlwmenu_redisplay (widget);
7700 #endif /* USE_LUCID */
7702 #ifdef USE_TOOLKIT_SCROLL_BARS
7703 /* Dispatch event to the widget. */
7704 goto OTHER;
7705 #else /* not USE_TOOLKIT_SCROLL_BARS */
7706 bar = x_window_to_scroll_bar (event->xexpose.display,
7707 event->xexpose.window, 2);
7709 if (bar)
7710 x_scroll_bar_expose (bar, event);
7711 #ifdef USE_X_TOOLKIT
7712 else
7713 goto OTHER;
7714 #endif /* USE_X_TOOLKIT */
7715 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7717 break;
7719 case GraphicsExpose: /* This occurs when an XCopyArea's
7720 source area was obscured or not
7721 available. */
7722 f = x_window_to_frame (dpyinfo, event->xgraphicsexpose.drawable);
7723 if (f)
7724 expose_frame (f, event->xgraphicsexpose.x,
7725 event->xgraphicsexpose.y,
7726 event->xgraphicsexpose.width,
7727 event->xgraphicsexpose.height);
7728 #ifdef USE_X_TOOLKIT
7729 else
7730 goto OTHER;
7731 #endif /* USE_X_TOOLKIT */
7732 break;
7734 case NoExpose: /* This occurs when an XCopyArea's
7735 source area was completely
7736 available. */
7737 break;
7739 case UnmapNotify:
7740 /* Redo the mouse-highlight after the tooltip has gone. */
7741 if (event->xunmap.window == tip_window)
7743 tip_window = 0;
7744 x_redo_mouse_highlight (dpyinfo);
7747 f = x_top_window_to_frame (dpyinfo, event->xunmap.window);
7748 if (f) /* F may no longer exist if
7749 the frame was deleted. */
7751 bool visible = FRAME_VISIBLE_P (f);
7752 /* While a frame is unmapped, display generation is
7753 disabled; you don't want to spend time updating a
7754 display that won't ever be seen. */
7755 SET_FRAME_VISIBLE (f, 0);
7756 /* We can't distinguish, from the event, whether the window
7757 has become iconified or invisible. So assume, if it
7758 was previously visible, than now it is iconified.
7759 But x_make_frame_invisible clears both
7760 the visible flag and the iconified flag;
7761 and that way, we know the window is not iconified now. */
7762 if (visible || FRAME_ICONIFIED_P (f))
7764 SET_FRAME_ICONIFIED (f, true);
7765 inev.ie.kind = ICONIFY_EVENT;
7766 XSETFRAME (inev.ie.frame_or_window, f);
7769 goto OTHER;
7771 case MapNotify:
7772 /* We use x_top_window_to_frame because map events can
7773 come for sub-windows and they don't mean that the
7774 frame is visible. */
7775 f = x_top_window_to_frame (dpyinfo, event->xmap.window);
7776 if (f)
7778 bool iconified = FRAME_ICONIFIED_P (f);
7780 /* Check if fullscreen was specified before we where mapped the
7781 first time, i.e. from the command line. */
7782 if (!f->output_data.x->has_been_visible)
7783 x_check_fullscreen (f);
7785 SET_FRAME_VISIBLE (f, 1);
7786 SET_FRAME_ICONIFIED (f, false);
7787 f->output_data.x->has_been_visible = true;
7789 if (iconified)
7791 inev.ie.kind = DEICONIFY_EVENT;
7792 XSETFRAME (inev.ie.frame_or_window, f);
7794 else if (! NILP (Vframe_list) && ! NILP (XCDR (Vframe_list)))
7795 /* Force a redisplay sooner or later to update the
7796 frame titles in case this is the second frame. */
7797 record_asynch_buffer_change ();
7799 #ifdef USE_GTK
7800 xg_frame_resized (f, -1, -1);
7801 #endif
7803 goto OTHER;
7805 case KeyPress:
7807 x_display_set_last_user_time (dpyinfo, event->xkey.time);
7808 ignore_next_mouse_click_timeout = 0;
7810 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
7811 /* Dispatch KeyPress events when in menu. */
7812 if (popup_activated ())
7813 goto OTHER;
7814 #endif
7816 f = any;
7818 /* If mouse-highlight is an integer, input clears out
7819 mouse highlighting. */
7820 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
7821 #if ! defined (USE_GTK)
7822 && (f == 0
7823 || !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
7824 #endif
7827 clear_mouse_face (hlinfo);
7828 hlinfo->mouse_face_hidden = true;
7831 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
7832 if (f == 0)
7834 /* Scroll bars consume key events, but we want
7835 the keys to go to the scroll bar's frame. */
7836 Widget widget = XtWindowToWidget (dpyinfo->display,
7837 event->xkey.window);
7838 if (widget && XmIsScrollBar (widget))
7840 widget = XtParent (widget);
7841 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
7844 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
7846 if (f != 0)
7848 KeySym keysym, orig_keysym;
7849 /* al%imercury@uunet.uu.net says that making this 81
7850 instead of 80 fixed a bug whereby meta chars made
7851 his Emacs hang.
7853 It seems that some version of XmbLookupString has
7854 a bug of not returning XBufferOverflow in
7855 status_return even if the input is too long to
7856 fit in 81 bytes. So, we must prepare sufficient
7857 bytes for copy_buffer. 513 bytes (256 chars for
7858 two-byte character set) seems to be a fairly good
7859 approximation. -- 2000.8.10 handa@etl.go.jp */
7860 unsigned char copy_buffer[513];
7861 unsigned char *copy_bufptr = copy_buffer;
7862 int copy_bufsiz = sizeof (copy_buffer);
7863 int modifiers;
7864 Lisp_Object coding_system = Qlatin_1;
7865 Lisp_Object c;
7866 /* Event will be modified. */
7867 XKeyEvent xkey = event->xkey;
7869 #ifdef USE_GTK
7870 /* Don't pass keys to GTK. A Tab will shift focus to the
7871 tool bar in GTK 2.4. Keys will still go to menus and
7872 dialogs because in that case popup_activated is nonzero
7873 (see above). */
7874 *finish = X_EVENT_DROP;
7875 #endif
7877 xkey.state |= x_emacs_to_x_modifiers (FRAME_DISPLAY_INFO (f),
7878 extra_keyboard_modifiers);
7879 modifiers = xkey.state;
7881 /* This will have to go some day... */
7883 /* make_lispy_event turns chars into control chars.
7884 Don't do it here because XLookupString is too eager. */
7885 xkey.state &= ~ControlMask;
7886 xkey.state &= ~(dpyinfo->meta_mod_mask
7887 | dpyinfo->super_mod_mask
7888 | dpyinfo->hyper_mod_mask
7889 | dpyinfo->alt_mod_mask);
7891 /* In case Meta is ComposeCharacter,
7892 clear its status. According to Markus Ehrnsperger
7893 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
7894 this enables ComposeCharacter to work whether or
7895 not it is combined with Meta. */
7896 if (modifiers & dpyinfo->meta_mod_mask)
7897 memset (&compose_status, 0, sizeof (compose_status));
7899 #ifdef HAVE_X_I18N
7900 if (FRAME_XIC (f))
7902 Status status_return;
7904 coding_system = Vlocale_coding_system;
7905 nbytes = XmbLookupString (FRAME_XIC (f),
7906 &xkey, (char *) copy_bufptr,
7907 copy_bufsiz, &keysym,
7908 &status_return);
7909 if (status_return == XBufferOverflow)
7911 copy_bufsiz = nbytes + 1;
7912 copy_bufptr = alloca (copy_bufsiz);
7913 nbytes = XmbLookupString (FRAME_XIC (f),
7914 &xkey, (char *) copy_bufptr,
7915 copy_bufsiz, &keysym,
7916 &status_return);
7918 /* Xutf8LookupString is a new but already deprecated interface. -stef */
7919 if (status_return == XLookupNone)
7920 break;
7921 else if (status_return == XLookupChars)
7923 keysym = NoSymbol;
7924 modifiers = 0;
7926 else if (status_return != XLookupKeySym
7927 && status_return != XLookupBoth)
7928 emacs_abort ();
7930 else
7931 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
7932 copy_bufsiz, &keysym,
7933 &compose_status);
7934 #else
7935 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
7936 copy_bufsiz, &keysym,
7937 &compose_status);
7938 #endif
7940 /* If not using XIM/XIC, and a compose sequence is in progress,
7941 we break here. Otherwise, chars_matched is always 0. */
7942 if (compose_status.chars_matched > 0 && nbytes == 0)
7943 break;
7945 memset (&compose_status, 0, sizeof (compose_status));
7946 orig_keysym = keysym;
7948 /* Common for all keysym input events. */
7949 XSETFRAME (inev.ie.frame_or_window, f);
7950 inev.ie.modifiers
7951 = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), modifiers);
7952 inev.ie.timestamp = xkey.time;
7954 /* First deal with keysyms which have defined
7955 translations to characters. */
7956 if (keysym >= 32 && keysym < 128)
7957 /* Avoid explicitly decoding each ASCII character. */
7959 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
7960 inev.ie.code = keysym;
7961 goto done_keysym;
7964 /* Keysyms directly mapped to Unicode characters. */
7965 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
7967 if (keysym < 0x01000080)
7968 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
7969 else
7970 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
7971 inev.ie.code = keysym & 0xFFFFFF;
7972 goto done_keysym;
7975 /* Now non-ASCII. */
7976 if (HASH_TABLE_P (Vx_keysym_table)
7977 && (c = Fgethash (make_number (keysym),
7978 Vx_keysym_table,
7979 Qnil),
7980 NATNUMP (c)))
7982 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
7983 ? ASCII_KEYSTROKE_EVENT
7984 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
7985 inev.ie.code = XFASTINT (c);
7986 goto done_keysym;
7989 /* Random non-modifier sorts of keysyms. */
7990 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
7991 || keysym == XK_Delete
7992 #ifdef XK_ISO_Left_Tab
7993 || (keysym >= XK_ISO_Left_Tab
7994 && keysym <= XK_ISO_Enter)
7995 #endif
7996 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
7997 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
7998 #ifdef HPUX
7999 /* This recognizes the "extended function
8000 keys". It seems there's no cleaner way.
8001 Test IsModifierKey to avoid handling
8002 mode_switch incorrectly. */
8003 || (XK_Select <= keysym && keysym < XK_KP_Space)
8004 #endif
8005 #ifdef XK_dead_circumflex
8006 || orig_keysym == XK_dead_circumflex
8007 #endif
8008 #ifdef XK_dead_grave
8009 || orig_keysym == XK_dead_grave
8010 #endif
8011 #ifdef XK_dead_tilde
8012 || orig_keysym == XK_dead_tilde
8013 #endif
8014 #ifdef XK_dead_diaeresis
8015 || orig_keysym == XK_dead_diaeresis
8016 #endif
8017 #ifdef XK_dead_macron
8018 || orig_keysym == XK_dead_macron
8019 #endif
8020 #ifdef XK_dead_degree
8021 || orig_keysym == XK_dead_degree
8022 #endif
8023 #ifdef XK_dead_acute
8024 || orig_keysym == XK_dead_acute
8025 #endif
8026 #ifdef XK_dead_cedilla
8027 || orig_keysym == XK_dead_cedilla
8028 #endif
8029 #ifdef XK_dead_breve
8030 || orig_keysym == XK_dead_breve
8031 #endif
8032 #ifdef XK_dead_ogonek
8033 || orig_keysym == XK_dead_ogonek
8034 #endif
8035 #ifdef XK_dead_caron
8036 || orig_keysym == XK_dead_caron
8037 #endif
8038 #ifdef XK_dead_doubleacute
8039 || orig_keysym == XK_dead_doubleacute
8040 #endif
8041 #ifdef XK_dead_abovedot
8042 || orig_keysym == XK_dead_abovedot
8043 #endif
8044 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
8045 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
8046 /* Any "vendor-specific" key is ok. */
8047 || (orig_keysym & (1 << 28))
8048 || (keysym != NoSymbol && nbytes == 0))
8049 && ! (IsModifierKey (orig_keysym)
8050 /* The symbols from XK_ISO_Lock
8051 to XK_ISO_Last_Group_Lock
8052 don't have real modifiers but
8053 should be treated similarly to
8054 Mode_switch by Emacs. */
8055 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
8056 || (XK_ISO_Lock <= orig_keysym
8057 && orig_keysym <= XK_ISO_Last_Group_Lock)
8058 #endif
8061 STORE_KEYSYM_FOR_DEBUG (keysym);
8062 /* make_lispy_event will convert this to a symbolic
8063 key. */
8064 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
8065 inev.ie.code = keysym;
8066 goto done_keysym;
8069 { /* Raw bytes, not keysym. */
8070 ptrdiff_t i;
8071 int nchars, len;
8073 for (i = 0, nchars = 0; i < nbytes; i++)
8075 if (ASCII_CHAR_P (copy_bufptr[i]))
8076 nchars++;
8077 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
8080 if (nchars < nbytes)
8082 /* Decode the input data. */
8084 /* The input should be decoded with `coding_system'
8085 which depends on which X*LookupString function
8086 we used just above and the locale. */
8087 setup_coding_system (coding_system, &coding);
8088 coding.src_multibyte = false;
8089 coding.dst_multibyte = true;
8090 /* The input is converted to events, thus we can't
8091 handle composition. Anyway, there's no XIM that
8092 gives us composition information. */
8093 coding.common_flags &= ~CODING_ANNOTATION_MASK;
8095 SAFE_NALLOCA (coding.destination, MAX_MULTIBYTE_LENGTH,
8096 nbytes);
8097 coding.dst_bytes = MAX_MULTIBYTE_LENGTH * nbytes;
8098 coding.mode |= CODING_MODE_LAST_BLOCK;
8099 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
8100 nbytes = coding.produced;
8101 nchars = coding.produced_char;
8102 copy_bufptr = coding.destination;
8105 /* Convert the input data to a sequence of
8106 character events. */
8107 for (i = 0; i < nbytes; i += len)
8109 int ch;
8110 if (nchars == nbytes)
8111 ch = copy_bufptr[i], len = 1;
8112 else
8113 ch = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
8114 inev.ie.kind = (SINGLE_BYTE_CHAR_P (ch)
8115 ? ASCII_KEYSTROKE_EVENT
8116 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
8117 inev.ie.code = ch;
8118 kbd_buffer_store_buffered_event (&inev, hold_quit);
8121 count += nchars;
8123 inev.ie.kind = NO_EVENT; /* Already stored above. */
8125 if (keysym == NoSymbol)
8126 break;
8128 /* FIXME: check side effects and remove this. */
8129 ((XEvent *) event)->xkey = xkey;
8131 done_keysym:
8132 #ifdef HAVE_X_I18N
8133 /* Don't dispatch this event since XtDispatchEvent calls
8134 XFilterEvent, and two calls in a row may freeze the
8135 client. */
8136 break;
8137 #else
8138 goto OTHER;
8139 #endif
8141 case KeyRelease:
8142 x_display_set_last_user_time (dpyinfo, event->xkey.time);
8143 #ifdef HAVE_X_I18N
8144 /* Don't dispatch this event since XtDispatchEvent calls
8145 XFilterEvent, and two calls in a row may freeze the
8146 client. */
8147 break;
8148 #else
8149 goto OTHER;
8150 #endif
8152 case EnterNotify:
8153 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
8154 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8156 f = any;
8158 if (f && x_mouse_click_focus_ignore_position)
8159 ignore_next_mouse_click_timeout = event->xmotion.time + 200;
8161 /* EnterNotify counts as mouse movement,
8162 so update things that depend on mouse position. */
8163 if (f && !f->output_data.x->hourglass_p)
8164 note_mouse_movement (f, &event->xmotion);
8165 #ifdef USE_GTK
8166 /* We may get an EnterNotify on the buttons in the toolbar. In that
8167 case we moved out of any highlighted area and need to note this. */
8168 if (!f && dpyinfo->last_mouse_glyph_frame)
8169 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
8170 #endif
8171 goto OTHER;
8173 case FocusIn:
8174 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8175 goto OTHER;
8177 case LeaveNotify:
8178 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
8179 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8181 f = x_top_window_to_frame (dpyinfo, event->xcrossing.window);
8182 if (f)
8184 if (f == hlinfo->mouse_face_mouse_frame)
8186 /* If we move outside the frame, then we're
8187 certainly no longer on any text in the frame. */
8188 clear_mouse_face (hlinfo);
8189 hlinfo->mouse_face_mouse_frame = 0;
8192 /* Generate a nil HELP_EVENT to cancel a help-echo.
8193 Do it only if there's something to cancel.
8194 Otherwise, the startup message is cleared when
8195 the mouse leaves the frame. */
8196 if (any_help_event_p)
8197 do_help = -1;
8199 #ifdef USE_GTK
8200 /* See comment in EnterNotify above */
8201 else if (dpyinfo->last_mouse_glyph_frame)
8202 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
8203 #endif
8204 goto OTHER;
8206 case FocusOut:
8207 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8208 goto OTHER;
8210 case MotionNotify:
8212 x_display_set_last_user_time (dpyinfo, event->xmotion.time);
8213 previous_help_echo_string = help_echo_string;
8214 help_echo_string = Qnil;
8216 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
8217 : x_window_to_frame (dpyinfo, event->xmotion.window));
8219 if (hlinfo->mouse_face_hidden)
8221 hlinfo->mouse_face_hidden = false;
8222 clear_mouse_face (hlinfo);
8225 #ifdef USE_GTK
8226 if (f && xg_event_is_for_scrollbar (f, event))
8227 f = 0;
8228 #endif
8229 if (f)
8232 /* Generate SELECT_WINDOW_EVENTs when needed.
8233 Don't let popup menus influence things (bug#1261). */
8234 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
8236 static Lisp_Object last_mouse_window;
8237 Lisp_Object window = window_from_coordinates
8238 (f, event->xmotion.x, event->xmotion.y, 0, false);
8240 /* Window will be selected only when it is not selected now and
8241 last mouse movement event was not in it. Minibuffer window
8242 will be selected only when it is active. */
8243 if (WINDOWP (window)
8244 && !EQ (window, last_mouse_window)
8245 && !EQ (window, selected_window)
8246 /* For click-to-focus window managers
8247 create event iff we don't leave the
8248 selected frame. */
8249 && (focus_follows_mouse
8250 || (EQ (XWINDOW (window)->frame,
8251 XWINDOW (selected_window)->frame))))
8253 inev.ie.kind = SELECT_WINDOW_EVENT;
8254 inev.ie.frame_or_window = window;
8256 /* Remember the last window where we saw the mouse. */
8257 last_mouse_window = window;
8259 if (!note_mouse_movement (f, &event->xmotion))
8260 help_echo_string = previous_help_echo_string;
8262 else
8264 #ifndef USE_TOOLKIT_SCROLL_BARS
8265 struct scroll_bar *bar
8266 = x_window_to_scroll_bar (event->xmotion.display,
8267 event->xmotion.window, 2);
8269 if (bar)
8270 x_scroll_bar_note_movement (bar, &event->xmotion);
8271 #endif /* USE_TOOLKIT_SCROLL_BARS */
8273 /* If we move outside the frame, then we're
8274 certainly no longer on any text in the frame. */
8275 clear_mouse_face (hlinfo);
8278 /* If the contents of the global variable help_echo_string
8279 has changed, generate a HELP_EVENT. */
8280 if (!NILP (help_echo_string)
8281 || !NILP (previous_help_echo_string))
8282 do_help = 1;
8283 goto OTHER;
8286 case ConfigureNotify:
8287 f = x_top_window_to_frame (dpyinfo, event->xconfigure.window);
8288 #ifdef USE_CAIRO
8289 if (f) x_cr_destroy_surface (f);
8290 #endif
8291 #ifdef USE_GTK
8292 if (!f
8293 && (f = any)
8294 && event->xconfigure.window == FRAME_X_WINDOW (f))
8296 xg_frame_resized (f, event->xconfigure.width,
8297 event->xconfigure.height);
8298 #ifdef USE_CAIRO
8299 x_cr_destroy_surface (f);
8300 #endif
8301 f = 0;
8303 #endif
8304 if (f)
8306 x_net_wm_state (f, event->xconfigure.window);
8308 #ifdef USE_X_TOOLKIT
8309 /* Tip frames are pure X window, set size for them. */
8310 if (! NILP (tip_frame) && XFRAME (tip_frame) == f)
8312 if (FRAME_PIXEL_HEIGHT (f) != event->xconfigure.height
8313 || FRAME_PIXEL_WIDTH (f) != event->xconfigure.width)
8314 SET_FRAME_GARBAGED (f);
8315 FRAME_PIXEL_HEIGHT (f) = event->xconfigure.height;
8316 FRAME_PIXEL_WIDTH (f) = event->xconfigure.width;
8318 #endif
8320 #ifndef USE_X_TOOLKIT
8321 #ifndef USE_GTK
8322 int width = FRAME_PIXEL_TO_TEXT_WIDTH (f, event->xconfigure.width);
8323 int height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, event->xconfigure.height);
8325 /* In the toolkit version, change_frame_size
8326 is called by the code that handles resizing
8327 of the EmacsFrame widget. */
8329 /* Even if the number of character rows and columns has
8330 not changed, the font size may have changed, so we need
8331 to check the pixel dimensions as well. */
8332 if (width != FRAME_TEXT_WIDTH (f)
8333 || height != FRAME_TEXT_HEIGHT (f)
8334 || event->xconfigure.width != FRAME_PIXEL_WIDTH (f)
8335 || event->xconfigure.height != FRAME_PIXEL_HEIGHT (f))
8337 change_frame_size (f, width, height, false, true, false, true);
8338 x_clear_under_internal_border (f);
8339 SET_FRAME_GARBAGED (f);
8340 cancel_mouse_face (f);
8342 #endif /* not USE_GTK */
8343 #endif
8345 #ifdef USE_GTK
8346 /* GTK creates windows but doesn't map them.
8347 Only get real positions when mapped. */
8348 if (FRAME_GTK_OUTER_WIDGET (f)
8349 && gtk_widget_get_mapped (FRAME_GTK_OUTER_WIDGET (f)))
8350 #endif
8351 x_real_positions (f, &f->left_pos, &f->top_pos);
8353 #ifdef HAVE_X_I18N
8354 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
8355 xic_set_statusarea (f);
8356 #endif
8359 goto OTHER;
8361 case ButtonRelease:
8362 case ButtonPress:
8364 /* If we decide we want to generate an event to be seen
8365 by the rest of Emacs, we put it here. */
8366 bool tool_bar_p = false;
8368 memset (&compose_status, 0, sizeof (compose_status));
8369 dpyinfo->last_mouse_glyph_frame = NULL;
8370 x_display_set_last_user_time (dpyinfo, event->xbutton.time);
8372 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
8373 : x_window_to_frame (dpyinfo, event->xbutton.window));
8375 #ifdef USE_GTK
8376 if (f && xg_event_is_for_scrollbar (f, event))
8377 f = 0;
8378 #endif
8379 if (f)
8381 #if ! defined (USE_GTK)
8382 /* Is this in the tool-bar? */
8383 if (WINDOWP (f->tool_bar_window)
8384 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
8386 Lisp_Object window;
8387 int x = event->xbutton.x;
8388 int y = event->xbutton.y;
8390 window = window_from_coordinates (f, x, y, 0, true);
8391 tool_bar_p = EQ (window, f->tool_bar_window);
8393 if (tool_bar_p && event->xbutton.button < 4)
8394 handle_tool_bar_click
8395 (f, x, y, event->xbutton.type == ButtonPress,
8396 x_x_to_emacs_modifiers (dpyinfo, event->xbutton.state));
8398 #endif /* !USE_GTK */
8400 if (!tool_bar_p)
8401 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8402 if (! popup_activated ())
8403 #endif
8405 if (ignore_next_mouse_click_timeout)
8407 if (event->type == ButtonPress
8408 && event->xbutton.time > ignore_next_mouse_click_timeout)
8410 ignore_next_mouse_click_timeout = 0;
8411 construct_mouse_click (&inev.ie, &event->xbutton, f);
8413 if (event->type == ButtonRelease)
8414 ignore_next_mouse_click_timeout = 0;
8416 else
8417 construct_mouse_click (&inev.ie, &event->xbutton, f);
8419 if (FRAME_X_EMBEDDED_P (f))
8420 xembed_send_message (f, event->xbutton.time,
8421 XEMBED_REQUEST_FOCUS, 0, 0, 0);
8423 else
8425 struct scroll_bar *bar
8426 = x_window_to_scroll_bar (event->xbutton.display,
8427 event->xbutton.window, 2);
8429 #ifdef USE_TOOLKIT_SCROLL_BARS
8430 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
8431 scroll bars. */
8432 if (bar && event->xbutton.state & ControlMask)
8434 x_scroll_bar_handle_click (bar, event, &inev.ie);
8435 *finish = X_EVENT_DROP;
8437 #else /* not USE_TOOLKIT_SCROLL_BARS */
8438 if (bar)
8439 x_scroll_bar_handle_click (bar, event, &inev.ie);
8440 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8443 if (event->type == ButtonPress)
8445 dpyinfo->grabbed |= (1 << event->xbutton.button);
8446 dpyinfo->last_mouse_frame = f;
8447 #if ! defined (USE_GTK)
8448 if (f && !tool_bar_p)
8449 f->last_tool_bar_item = -1;
8450 #endif /* not USE_GTK */
8452 else
8453 dpyinfo->grabbed &= ~(1 << event->xbutton.button);
8455 /* Ignore any mouse motion that happened before this event;
8456 any subsequent mouse-movement Emacs events should reflect
8457 only motion after the ButtonPress/Release. */
8458 if (f != 0)
8459 f->mouse_moved = false;
8461 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8462 f = x_menubar_window_to_frame (dpyinfo, event);
8463 /* For a down-event in the menu bar,
8464 don't pass it to Xt right now.
8465 Instead, save it away
8466 and we will pass it to Xt from kbd_buffer_get_event.
8467 That way, we can run some Lisp code first. */
8468 if (! popup_activated ()
8469 #ifdef USE_GTK
8470 /* Gtk+ menus only react to the first three buttons. */
8471 && event->xbutton.button < 3
8472 #endif
8473 && f && event->type == ButtonPress
8474 /* Verify the event is really within the menu bar
8475 and not just sent to it due to grabbing. */
8476 && event->xbutton.x >= 0
8477 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
8478 && event->xbutton.y >= 0
8479 && event->xbutton.y < FRAME_MENUBAR_HEIGHT (f)
8480 && event->xbutton.same_screen)
8482 if (!f->output_data.x->saved_menu_event)
8483 f->output_data.x->saved_menu_event = xmalloc (sizeof *event);
8484 *f->output_data.x->saved_menu_event = *event;
8485 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT;
8486 XSETFRAME (inev.ie.frame_or_window, f);
8487 *finish = X_EVENT_DROP;
8489 else
8490 goto OTHER;
8491 #endif /* USE_X_TOOLKIT || USE_GTK */
8493 break;
8495 case CirculateNotify:
8496 goto OTHER;
8498 case CirculateRequest:
8499 goto OTHER;
8501 case VisibilityNotify:
8502 goto OTHER;
8504 case MappingNotify:
8505 /* Someone has changed the keyboard mapping - update the
8506 local cache. */
8507 switch (event->xmapping.request)
8509 case MappingModifier:
8510 x_find_modifier_meanings (dpyinfo);
8511 /* This is meant to fall through. */
8512 case MappingKeyboard:
8513 XRefreshKeyboardMapping ((XMappingEvent *) &event->xmapping);
8515 goto OTHER;
8517 case DestroyNotify:
8518 xft_settings_event (dpyinfo, event);
8519 break;
8521 default:
8522 OTHER:
8523 #ifdef USE_X_TOOLKIT
8524 block_input ();
8525 if (*finish != X_EVENT_DROP)
8526 XtDispatchEvent ((XEvent *) event);
8527 unblock_input ();
8528 #endif /* USE_X_TOOLKIT */
8529 break;
8532 done:
8533 if (inev.ie.kind != NO_EVENT)
8535 kbd_buffer_store_buffered_event (&inev, hold_quit);
8536 count++;
8539 if (do_help
8540 && !(hold_quit && hold_quit->kind != NO_EVENT))
8542 Lisp_Object frame;
8544 if (f)
8545 XSETFRAME (frame, f);
8546 else
8547 frame = Qnil;
8549 if (do_help > 0)
8551 any_help_event_p = true;
8552 gen_help_event (help_echo_string, frame, help_echo_window,
8553 help_echo_object, help_echo_pos);
8555 else
8557 help_echo_string = Qnil;
8558 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
8560 count++;
8563 SAFE_FREE ();
8564 return count;
8567 /* Handles the XEvent EVENT on display DISPLAY.
8568 This is used for event loops outside the normal event handling,
8569 i.e. looping while a popup menu or a dialog is posted.
8571 Returns the value handle_one_xevent sets in the finish argument. */
8573 x_dispatch_event (XEvent *event, Display *display)
8575 struct x_display_info *dpyinfo;
8576 int finish = X_EVENT_NORMAL;
8578 dpyinfo = x_display_info_for_display (display);
8580 if (dpyinfo)
8581 handle_one_xevent (dpyinfo, event, &finish, 0);
8583 return finish;
8586 /* Read events coming from the X server.
8587 Return as soon as there are no more events to be read.
8589 Return the number of characters stored into the buffer,
8590 thus pretending to be `read' (except the characters we store
8591 in the keyboard buffer can be multibyte, so are not necessarily
8592 C chars). */
8594 static int
8595 XTread_socket (struct terminal *terminal, struct input_event *hold_quit)
8597 int count = 0;
8598 bool event_found = false;
8599 struct x_display_info *dpyinfo = terminal->display_info.x;
8601 block_input ();
8603 /* For debugging, this gives a way to fake an I/O error. */
8604 if (dpyinfo == XTread_socket_fake_io_error)
8606 XTread_socket_fake_io_error = 0;
8607 x_io_error_quitter (dpyinfo->display);
8610 #ifndef USE_GTK
8611 while (XPending (dpyinfo->display))
8613 int finish;
8614 XEvent event;
8616 XNextEvent (dpyinfo->display, &event);
8618 #ifdef HAVE_X_I18N
8619 /* Filter events for the current X input method. */
8620 if (x_filter_event (dpyinfo, &event))
8621 continue;
8622 #endif
8623 event_found = true;
8625 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
8627 if (finish == X_EVENT_GOTO_OUT)
8628 break;
8631 #else /* USE_GTK */
8633 /* For GTK we must use the GTK event loop. But XEvents gets passed
8634 to our filter function above, and then to the big event switch.
8635 We use a bunch of globals to communicate with our filter function,
8636 that is kind of ugly, but it works.
8638 There is no way to do one display at the time, GTK just does events
8639 from all displays. */
8641 while (gtk_events_pending ())
8643 current_count = count;
8644 current_hold_quit = hold_quit;
8646 gtk_main_iteration ();
8648 count = current_count;
8649 current_count = -1;
8650 current_hold_quit = 0;
8652 if (current_finish == X_EVENT_GOTO_OUT)
8653 break;
8655 #endif /* USE_GTK */
8657 /* On some systems, an X bug causes Emacs to get no more events
8658 when the window is destroyed. Detect that. (1994.) */
8659 if (! event_found)
8661 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
8662 One XNOOP in 100 loops will make Emacs terminate.
8663 B. Bretthauer, 1994 */
8664 x_noop_count++;
8665 if (x_noop_count >= 100)
8667 x_noop_count=0;
8669 if (next_noop_dpyinfo == 0)
8670 next_noop_dpyinfo = x_display_list;
8672 XNoOp (next_noop_dpyinfo->display);
8674 /* Each time we get here, cycle through the displays now open. */
8675 next_noop_dpyinfo = next_noop_dpyinfo->next;
8679 /* If the focus was just given to an auto-raising frame,
8680 raise it now. FIXME: handle more than one such frame. */
8681 if (dpyinfo->x_pending_autoraise_frame)
8683 x_raise_frame (dpyinfo->x_pending_autoraise_frame);
8684 dpyinfo->x_pending_autoraise_frame = NULL;
8687 unblock_input ();
8689 return count;
8695 /***********************************************************************
8696 Text Cursor
8697 ***********************************************************************/
8699 /* Set clipping for output in glyph row ROW. W is the window in which
8700 we operate. GC is the graphics context to set clipping in.
8702 ROW may be a text row or, e.g., a mode line. Text rows must be
8703 clipped to the interior of the window dedicated to text display,
8704 mode lines must be clipped to the whole window. */
8706 static void
8707 x_clip_to_row (struct window *w, struct glyph_row *row,
8708 enum glyph_row_area area, GC gc)
8710 struct frame *f = XFRAME (WINDOW_FRAME (w));
8711 XRectangle clip_rect;
8712 int window_x, window_y, window_width;
8714 window_box (w, area, &window_x, &window_y, &window_width, 0);
8716 clip_rect.x = window_x;
8717 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
8718 clip_rect.y = max (clip_rect.y, window_y);
8719 clip_rect.width = window_width;
8720 clip_rect.height = row->visible_height;
8722 x_set_clip_rectangles (f, gc, &clip_rect, 1);
8726 /* Draw a hollow box cursor on window W in glyph row ROW. */
8728 static void
8729 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
8731 struct frame *f = XFRAME (WINDOW_FRAME (w));
8732 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8733 Display *dpy = FRAME_X_DISPLAY (f);
8734 int x, y, wd, h;
8735 XGCValues xgcv;
8736 struct glyph *cursor_glyph;
8737 GC gc;
8739 /* Get the glyph the cursor is on. If we can't tell because
8740 the current matrix is invalid or such, give up. */
8741 cursor_glyph = get_phys_cursor_glyph (w);
8742 if (cursor_glyph == NULL)
8743 return;
8745 /* Compute frame-relative coordinates for phys cursor. */
8746 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
8747 wd = w->phys_cursor_width - 1;
8749 /* The foreground of cursor_gc is typically the same as the normal
8750 background color, which can cause the cursor box to be invisible. */
8751 xgcv.foreground = f->output_data.x->cursor_pixel;
8752 if (dpyinfo->scratch_cursor_gc)
8753 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
8754 else
8755 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
8756 GCForeground, &xgcv);
8757 gc = dpyinfo->scratch_cursor_gc;
8759 /* When on R2L character, show cursor at the right edge of the
8760 glyph, unless the cursor box is as wide as the glyph or wider
8761 (the latter happens when x-stretch-cursor is non-nil). */
8762 if ((cursor_glyph->resolved_level & 1) != 0
8763 && cursor_glyph->pixel_width > wd)
8765 x += cursor_glyph->pixel_width - wd;
8766 if (wd > 0)
8767 wd -= 1;
8769 /* Set clipping, draw the rectangle, and reset clipping again. */
8770 x_clip_to_row (w, row, TEXT_AREA, gc);
8771 x_draw_rectangle (f, gc, x, y, wd, h - 1);
8772 x_reset_clip_rectangles (f, gc);
8776 /* Draw a bar cursor on window W in glyph row ROW.
8778 Implementation note: One would like to draw a bar cursor with an
8779 angle equal to the one given by the font property XA_ITALIC_ANGLE.
8780 Unfortunately, I didn't find a font yet that has this property set.
8781 --gerd. */
8783 static void
8784 x_draw_bar_cursor (struct window *w, struct glyph_row *row, int width, enum text_cursor_kinds kind)
8786 struct frame *f = XFRAME (w->frame);
8787 struct glyph *cursor_glyph;
8789 /* If cursor is out of bounds, don't draw garbage. This can happen
8790 in mini-buffer windows when switching between echo area glyphs
8791 and mini-buffer. */
8792 cursor_glyph = get_phys_cursor_glyph (w);
8793 if (cursor_glyph == NULL)
8794 return;
8796 /* If on an image, draw like a normal cursor. That's usually better
8797 visible than drawing a bar, esp. if the image is large so that
8798 the bar might not be in the window. */
8799 if (cursor_glyph->type == IMAGE_GLYPH)
8801 struct glyph_row *r;
8802 r = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
8803 draw_phys_cursor_glyph (w, r, DRAW_CURSOR);
8805 else
8807 Display *dpy = FRAME_X_DISPLAY (f);
8808 Window window = FRAME_X_WINDOW (f);
8809 GC gc = FRAME_DISPLAY_INFO (f)->scratch_cursor_gc;
8810 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
8811 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
8812 XGCValues xgcv;
8814 /* If the glyph's background equals the color we normally draw
8815 the bars cursor in, the bar cursor in its normal color is
8816 invisible. Use the glyph's foreground color instead in this
8817 case, on the assumption that the glyph's colors are chosen so
8818 that the glyph is legible. */
8819 if (face->background == f->output_data.x->cursor_pixel)
8820 xgcv.background = xgcv.foreground = face->foreground;
8821 else
8822 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
8823 xgcv.graphics_exposures = False;
8825 if (gc)
8826 XChangeGC (dpy, gc, mask, &xgcv);
8827 else
8829 gc = XCreateGC (dpy, window, mask, &xgcv);
8830 FRAME_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
8833 x_clip_to_row (w, row, TEXT_AREA, gc);
8835 if (kind == BAR_CURSOR)
8837 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8839 if (width < 0)
8840 width = FRAME_CURSOR_WIDTH (f);
8841 width = min (cursor_glyph->pixel_width, width);
8843 w->phys_cursor_width = width;
8845 /* If the character under cursor is R2L, draw the bar cursor
8846 on the right of its glyph, rather than on the left. */
8847 if ((cursor_glyph->resolved_level & 1) != 0)
8848 x += cursor_glyph->pixel_width - width;
8850 x_fill_rectangle (f, gc, x,
8851 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
8852 width, row->height);
8854 else /* HBAR_CURSOR */
8856 int dummy_x, dummy_y, dummy_h;
8857 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8859 if (width < 0)
8860 width = row->height;
8862 width = min (row->height, width);
8864 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
8865 &dummy_y, &dummy_h);
8867 if ((cursor_glyph->resolved_level & 1) != 0
8868 && cursor_glyph->pixel_width > w->phys_cursor_width - 1)
8869 x += cursor_glyph->pixel_width - w->phys_cursor_width + 1;
8870 x_fill_rectangle (f, gc, x,
8871 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
8872 row->height - width),
8873 w->phys_cursor_width - 1, width);
8876 x_reset_clip_rectangles (f, gc);
8881 /* RIF: Define cursor CURSOR on frame F. */
8883 static void
8884 x_define_frame_cursor (struct frame *f, Cursor cursor)
8886 if (!f->pointer_invisible
8887 && f->output_data.x->current_cursor != cursor)
8888 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
8889 f->output_data.x->current_cursor = cursor;
8893 /* RIF: Clear area on frame F. */
8895 static void
8896 x_clear_frame_area (struct frame *f, int x, int y, int width, int height)
8898 x_clear_area (f, x, y, width, height);
8899 #ifdef USE_GTK
8900 /* Must queue a redraw, because scroll bars might have been cleared. */
8901 if (FRAME_GTK_WIDGET (f))
8902 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
8903 #endif
8907 /* RIF: Draw cursor on window W. */
8909 static void
8910 x_draw_window_cursor (struct window *w, struct glyph_row *glyph_row, int x,
8911 int y, enum text_cursor_kinds cursor_type,
8912 int cursor_width, bool on_p, bool active_p)
8914 struct frame *f = XFRAME (WINDOW_FRAME (w));
8916 if (on_p)
8918 w->phys_cursor_type = cursor_type;
8919 w->phys_cursor_on_p = true;
8921 if (glyph_row->exact_window_width_line_p
8922 && (glyph_row->reversed_p
8923 ? (w->phys_cursor.hpos < 0)
8924 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
8926 glyph_row->cursor_in_fringe_p = true;
8927 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
8929 else
8931 switch (cursor_type)
8933 case HOLLOW_BOX_CURSOR:
8934 x_draw_hollow_cursor (w, glyph_row);
8935 break;
8937 case FILLED_BOX_CURSOR:
8938 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
8939 break;
8941 case BAR_CURSOR:
8942 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
8943 break;
8945 case HBAR_CURSOR:
8946 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
8947 break;
8949 case NO_CURSOR:
8950 w->phys_cursor_width = 0;
8951 break;
8953 default:
8954 emacs_abort ();
8958 #ifdef HAVE_X_I18N
8959 if (w == XWINDOW (f->selected_window))
8960 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
8961 xic_set_preeditarea (w, x, y);
8962 #endif
8965 XFlush (FRAME_X_DISPLAY (f));
8969 /* Icons. */
8971 /* Make the x-window of frame F use the gnu icon bitmap. */
8973 bool
8974 x_bitmap_icon (struct frame *f, Lisp_Object file)
8976 ptrdiff_t bitmap_id;
8978 if (FRAME_X_WINDOW (f) == 0)
8979 return true;
8981 /* Free up our existing icon bitmap and mask if any. */
8982 if (f->output_data.x->icon_bitmap > 0)
8983 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
8984 f->output_data.x->icon_bitmap = 0;
8986 if (STRINGP (file))
8988 #ifdef USE_GTK
8989 /* Use gtk_window_set_icon_from_file () if available,
8990 It's not restricted to bitmaps */
8991 if (xg_set_icon (f, file))
8992 return false;
8993 #endif /* USE_GTK */
8994 bitmap_id = x_create_bitmap_from_file (f, file);
8995 x_create_bitmap_mask (f, bitmap_id);
8997 else
8999 /* Create the GNU bitmap and mask if necessary. */
9000 if (FRAME_DISPLAY_INFO (f)->icon_bitmap_id < 0)
9002 ptrdiff_t rc = -1;
9004 #ifdef USE_GTK
9006 if (xg_set_icon (f, xg_default_icon_file)
9007 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
9009 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = -2;
9010 return false;
9013 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
9015 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
9016 if (rc != -1)
9017 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
9019 #endif
9021 /* If all else fails, use the (black and white) xbm image. */
9022 if (rc == -1)
9024 rc = x_create_bitmap_from_data (f, (char *) gnu_xbm_bits,
9025 gnu_xbm_width, gnu_xbm_height);
9026 if (rc == -1)
9027 return true;
9029 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
9030 x_create_bitmap_mask (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
9034 /* The first time we create the GNU bitmap and mask,
9035 this increments the ref-count one extra time.
9036 As a result, the GNU bitmap and mask are never freed.
9037 That way, we don't have to worry about allocating it again. */
9038 x_reference_bitmap (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
9040 bitmap_id = FRAME_DISPLAY_INFO (f)->icon_bitmap_id;
9043 x_wm_set_icon_pixmap (f, bitmap_id);
9044 f->output_data.x->icon_bitmap = bitmap_id;
9046 return false;
9050 /* Make the x-window of frame F use a rectangle with text.
9051 Use ICON_NAME as the text. */
9053 bool
9054 x_text_icon (struct frame *f, const char *icon_name)
9056 if (FRAME_X_WINDOW (f) == 0)
9057 return true;
9060 XTextProperty text;
9061 text.value = (unsigned char *) icon_name;
9062 text.encoding = XA_STRING;
9063 text.format = 8;
9064 text.nitems = strlen (icon_name);
9065 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
9068 if (f->output_data.x->icon_bitmap > 0)
9069 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
9070 f->output_data.x->icon_bitmap = 0;
9071 x_wm_set_icon_pixmap (f, 0);
9073 return false;
9076 #define X_ERROR_MESSAGE_SIZE 200
9078 /* If non-nil, this should be a string.
9079 It means catch X errors and store the error message in this string.
9081 The reason we use a stack is that x_catch_error/x_uncatch_error can
9082 be called from a signal handler.
9085 struct x_error_message_stack {
9086 char string[X_ERROR_MESSAGE_SIZE];
9087 Display *dpy;
9088 struct x_error_message_stack *prev;
9090 static struct x_error_message_stack *x_error_message;
9092 /* An X error handler which stores the error message in
9093 *x_error_message. This is called from x_error_handler if
9094 x_catch_errors is in effect. */
9096 static void
9097 x_error_catcher (Display *display, XErrorEvent *event)
9099 XGetErrorText (display, event->error_code,
9100 x_error_message->string,
9101 X_ERROR_MESSAGE_SIZE);
9104 /* Begin trapping X errors for display DPY. Actually we trap X errors
9105 for all displays, but DPY should be the display you are actually
9106 operating on.
9108 After calling this function, X protocol errors no longer cause
9109 Emacs to exit; instead, they are recorded in the string
9110 stored in *x_error_message.
9112 Calling x_check_errors signals an Emacs error if an X error has
9113 occurred since the last call to x_catch_errors or x_check_errors.
9115 Calling x_uncatch_errors resumes the normal error handling. */
9117 void
9118 x_catch_errors (Display *dpy)
9120 struct x_error_message_stack *data = xmalloc (sizeof *data);
9122 /* Make sure any errors from previous requests have been dealt with. */
9123 XSync (dpy, False);
9125 data->dpy = dpy;
9126 data->string[0] = 0;
9127 data->prev = x_error_message;
9128 x_error_message = data;
9131 /* Undo the last x_catch_errors call.
9132 DPY should be the display that was passed to x_catch_errors. */
9134 void
9135 x_uncatch_errors (void)
9137 struct x_error_message_stack *tmp;
9139 block_input ();
9141 /* The display may have been closed before this function is called.
9142 Check if it is still open before calling XSync. */
9143 if (x_display_info_for_display (x_error_message->dpy) != 0)
9144 XSync (x_error_message->dpy, False);
9146 tmp = x_error_message;
9147 x_error_message = x_error_message->prev;
9148 xfree (tmp);
9149 unblock_input ();
9152 /* If any X protocol errors have arrived since the last call to
9153 x_catch_errors or x_check_errors, signal an Emacs error using
9154 sprintf (a buffer, FORMAT, the x error message text) as the text. */
9156 void
9157 x_check_errors (Display *dpy, const char *format)
9159 /* Make sure to catch any errors incurred so far. */
9160 XSync (dpy, False);
9162 if (x_error_message->string[0])
9164 char string[X_ERROR_MESSAGE_SIZE];
9165 memcpy (string, x_error_message->string, X_ERROR_MESSAGE_SIZE);
9166 x_uncatch_errors ();
9167 error (format, string);
9171 /* Nonzero if we had any X protocol errors
9172 since we did x_catch_errors on DPY. */
9174 bool
9175 x_had_errors_p (Display *dpy)
9177 /* Make sure to catch any errors incurred so far. */
9178 XSync (dpy, False);
9180 return x_error_message->string[0] != 0;
9183 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
9185 void
9186 x_clear_errors (Display *dpy)
9188 x_error_message->string[0] = 0;
9191 #if false
9192 /* See comment in unwind_to_catch why calling this is a bad
9193 * idea. --lorentey */
9194 /* Close off all unclosed x_catch_errors calls. */
9196 void
9197 x_fully_uncatch_errors (void)
9199 while (x_error_message)
9200 x_uncatch_errors ();
9202 #endif
9204 #if false
9205 static unsigned int x_wire_count;
9206 x_trace_wire (void)
9208 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
9210 #endif
9213 /************************************************************************
9214 Handling X errors
9215 ************************************************************************/
9217 /* Error message passed to x_connection_closed. */
9219 static char *error_msg;
9221 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
9222 the text of an error message that lead to the connection loss. */
9224 static void
9225 x_connection_closed (Display *dpy, const char *error_message, bool ioerror)
9227 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
9228 Lisp_Object frame, tail;
9229 ptrdiff_t idx = SPECPDL_INDEX ();
9231 error_msg = alloca (strlen (error_message) + 1);
9232 strcpy (error_msg, error_message);
9234 /* Inhibit redisplay while frames are being deleted. */
9235 specbind (Qinhibit_redisplay, Qt);
9237 if (dpyinfo)
9239 /* Protect display from being closed when we delete the last
9240 frame on it. */
9241 dpyinfo->reference_count++;
9242 dpyinfo->terminal->reference_count++;
9244 if (ioerror) dpyinfo->display = 0;
9246 /* First delete frames whose mini-buffers are on frames
9247 that are on the dead display. */
9248 FOR_EACH_FRAME (tail, frame)
9250 Lisp_Object minibuf_frame;
9251 minibuf_frame
9252 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
9253 if (FRAME_X_P (XFRAME (frame))
9254 && FRAME_X_P (XFRAME (minibuf_frame))
9255 && ! EQ (frame, minibuf_frame)
9256 && FRAME_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
9257 delete_frame (frame, Qnoelisp);
9260 /* Now delete all remaining frames on the dead display.
9261 We are now sure none of these is used as the mini-buffer
9262 for another frame that we need to delete. */
9263 FOR_EACH_FRAME (tail, frame)
9264 if (FRAME_X_P (XFRAME (frame))
9265 && FRAME_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
9267 /* Set this to t so that delete_frame won't get confused
9268 trying to find a replacement. */
9269 kset_default_minibuffer_frame (FRAME_KBOARD (XFRAME (frame)), Qt);
9270 delete_frame (frame, Qnoelisp);
9273 /* If DPYINFO is null, this means we didn't open the display in the
9274 first place, so don't try to close it. */
9275 if (dpyinfo)
9277 /* We can not call XtCloseDisplay here because it calls XSync.
9278 XSync inside the error handler apparently hangs Emacs. On
9279 current Xt versions, this isn't needed either. */
9280 #ifdef USE_GTK
9281 /* A long-standing GTK bug prevents proper disconnect handling
9282 (https://bugzilla.gnome.org/show_bug.cgi?id=85715). Once,
9283 the resulting Glib error message loop filled a user's disk.
9284 To avoid this, kill Emacs unconditionally on disconnect. */
9285 shut_down_emacs (0, Qnil);
9286 fprintf (stderr, "%s\n\
9287 When compiled with GTK, Emacs cannot recover from X disconnects.\n\
9288 This is a GTK bug: https://bugzilla.gnome.org/show_bug.cgi?id=85715\n\
9289 For details, see etc/PROBLEMS.\n",
9290 error_msg);
9291 emacs_abort ();
9292 #endif /* USE_GTK */
9294 /* Indicate that this display is dead. */
9295 dpyinfo->display = 0;
9297 dpyinfo->reference_count--;
9298 dpyinfo->terminal->reference_count--;
9299 if (dpyinfo->reference_count != 0)
9300 /* We have just closed all frames on this display. */
9301 emacs_abort ();
9304 Lisp_Object tmp;
9305 XSETTERMINAL (tmp, dpyinfo->terminal);
9306 Fdelete_terminal (tmp, Qnoelisp);
9310 if (terminal_list == 0)
9312 fprintf (stderr, "%s\n", error_msg);
9313 Fkill_emacs (make_number (70));
9314 /* NOTREACHED */
9317 totally_unblock_input ();
9319 unbind_to (idx, Qnil);
9320 clear_waiting_for_input ();
9322 /* Tell GCC not to suggest attribute 'noreturn' for this function. */
9323 IF_LINT (if (! terminal_list) return; )
9325 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
9326 longjmp), because returning from this function would get us back into
9327 Xlib's code which will directly call `exit'. */
9328 error ("%s", error_msg);
9331 /* We specifically use it before defining it, so that gcc doesn't inline it,
9332 otherwise gdb doesn't know how to properly put a breakpoint on it. */
9333 static void x_error_quitter (Display *, XErrorEvent *);
9335 /* This is the first-level handler for X protocol errors.
9336 It calls x_error_quitter or x_error_catcher. */
9338 static int
9339 x_error_handler (Display *display, XErrorEvent *event)
9341 #if defined USE_GTK && defined HAVE_GTK3
9342 if ((event->error_code == BadMatch || event->error_code == BadWindow)
9343 && event->request_code == X_SetInputFocus)
9345 return 0;
9347 #endif
9349 if (x_error_message)
9350 x_error_catcher (display, event);
9351 else
9352 x_error_quitter (display, event);
9353 return 0;
9356 /* This is the usual handler for X protocol errors.
9357 It kills all frames on the display that we got the error for.
9358 If that was the only one, it prints an error message and kills Emacs. */
9360 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
9362 /* On older GCC versions, just putting x_error_quitter
9363 after x_error_handler prevents inlining into the former. */
9365 static void NO_INLINE
9366 x_error_quitter (Display *display, XErrorEvent *event)
9368 char buf[256], buf1[356];
9370 /* Ignore BadName errors. They can happen because of fonts
9371 or colors that are not defined. */
9373 if (event->error_code == BadName)
9374 return;
9376 /* Note that there is no real way portable across R3/R4 to get the
9377 original error handler. */
9379 XGetErrorText (display, event->error_code, buf, sizeof (buf));
9380 sprintf (buf1, "X protocol error: %s on protocol request %d",
9381 buf, event->request_code);
9382 x_connection_closed (display, buf1, false);
9386 /* This is the handler for X IO errors, always.
9387 It kills all frames on the display that we lost touch with.
9388 If that was the only one, it prints an error message and kills Emacs. */
9390 static int
9391 x_io_error_quitter (Display *display)
9393 char buf[256];
9395 snprintf (buf, sizeof buf, "Connection lost to X server '%s'",
9396 DisplayString (display));
9397 x_connection_closed (display, buf, true);
9398 return 0;
9401 /* Changing the font of the frame. */
9403 /* Give frame F the font FONT-OBJECT as its default font. The return
9404 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
9405 frame. If it is negative, generate a new fontset from
9406 FONT-OBJECT. */
9408 Lisp_Object
9409 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
9411 struct font *font = XFONT_OBJECT (font_object);
9412 int unit, font_ascent, font_descent;
9414 if (fontset < 0)
9415 fontset = fontset_from_font (font_object);
9416 FRAME_FONTSET (f) = fontset;
9417 if (FRAME_FONT (f) == font)
9418 /* This font is already set in frame F. There's nothing more to
9419 do. */
9420 return font_object;
9422 FRAME_FONT (f) = font;
9423 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
9424 FRAME_COLUMN_WIDTH (f) = font->average_width;
9425 get_font_ascent_descent (font, &font_ascent, &font_descent);
9426 FRAME_LINE_HEIGHT (f) = font_ascent + font_descent;
9428 #ifndef USE_X_TOOLKIT
9429 FRAME_MENU_BAR_HEIGHT (f) = FRAME_MENU_BAR_LINES (f) * FRAME_LINE_HEIGHT (f);
9430 #endif
9432 /* Compute character columns occupied by scrollbar.
9434 Don't do things differently for non-toolkit scrollbars
9435 (Bug#17163). */
9436 unit = FRAME_COLUMN_WIDTH (f);
9437 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
9438 FRAME_CONFIG_SCROLL_BAR_COLS (f)
9439 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + unit - 1) / unit;
9440 else
9441 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
9443 if (FRAME_X_WINDOW (f) != 0)
9445 /* Don't change the size of a tip frame; there's no point in
9446 doing it because it's done in Fx_show_tip, and it leads to
9447 problems because the tip frame has no widget. */
9448 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
9449 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
9450 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 3,
9451 false, Qfont);
9454 #ifdef HAVE_X_I18N
9455 if (FRAME_XIC (f)
9456 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
9458 block_input ();
9459 xic_set_xfontset (f, SSDATA (fontset_ascii (fontset)));
9460 unblock_input ();
9462 #endif
9464 return font_object;
9468 /***********************************************************************
9469 X Input Methods
9470 ***********************************************************************/
9472 #ifdef HAVE_X_I18N
9474 #ifdef HAVE_X11R6
9476 /* XIM destroy callback function, which is called whenever the
9477 connection to input method XIM dies. CLIENT_DATA contains a
9478 pointer to the x_display_info structure corresponding to XIM. */
9480 static void
9481 xim_destroy_callback (XIM xim, XPointer client_data, XPointer call_data)
9483 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
9484 Lisp_Object frame, tail;
9486 block_input ();
9488 /* No need to call XDestroyIC.. */
9489 FOR_EACH_FRAME (tail, frame)
9491 struct frame *f = XFRAME (frame);
9492 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
9494 FRAME_XIC (f) = NULL;
9495 xic_free_xfontset (f);
9499 /* No need to call XCloseIM. */
9500 dpyinfo->xim = NULL;
9501 XFree (dpyinfo->xim_styles);
9502 unblock_input ();
9505 #endif /* HAVE_X11R6 */
9507 /* Open the connection to the XIM server on display DPYINFO.
9508 RESOURCE_NAME is the resource name Emacs uses. */
9510 static void
9511 xim_open_dpy (struct x_display_info *dpyinfo, char *resource_name)
9513 XIM xim;
9515 #ifdef HAVE_XIM
9516 if (use_xim)
9518 if (dpyinfo->xim)
9519 XCloseIM (dpyinfo->xim);
9520 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
9521 emacs_class);
9522 dpyinfo->xim = xim;
9524 if (xim)
9526 #ifdef HAVE_X11R6
9527 XIMCallback destroy;
9528 #endif
9530 /* Get supported styles and XIM values. */
9531 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
9533 #ifdef HAVE_X11R6
9534 destroy.callback = xim_destroy_callback;
9535 destroy.client_data = (XPointer)dpyinfo;
9536 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
9537 #endif
9541 else
9542 #endif /* HAVE_XIM */
9543 dpyinfo->xim = NULL;
9547 #ifdef HAVE_X11R6_XIM
9549 /* XIM instantiate callback function, which is called whenever an XIM
9550 server is available. DISPLAY is the display of the XIM.
9551 CLIENT_DATA contains a pointer to an xim_inst_t structure created
9552 when the callback was registered. */
9554 static void
9555 xim_instantiate_callback (Display *display, XPointer client_data, XPointer call_data)
9557 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
9558 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
9560 /* We don't support multiple XIM connections. */
9561 if (dpyinfo->xim)
9562 return;
9564 xim_open_dpy (dpyinfo, xim_inst->resource_name);
9566 /* Create XIC for the existing frames on the same display, as long
9567 as they have no XIC. */
9568 if (dpyinfo->xim && dpyinfo->reference_count > 0)
9570 Lisp_Object tail, frame;
9572 block_input ();
9573 FOR_EACH_FRAME (tail, frame)
9575 struct frame *f = XFRAME (frame);
9577 if (FRAME_X_P (f)
9578 && FRAME_DISPLAY_INFO (f) == xim_inst->dpyinfo)
9579 if (FRAME_XIC (f) == NULL)
9581 create_frame_xic (f);
9582 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
9583 xic_set_statusarea (f);
9584 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
9586 struct window *w = XWINDOW (f->selected_window);
9587 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
9592 unblock_input ();
9596 #endif /* HAVE_X11R6_XIM */
9599 /* Open a connection to the XIM server on display DPYINFO.
9600 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
9601 connection only at the first time. On X11R6, open the connection
9602 in the XIM instantiate callback function. */
9604 static void
9605 xim_initialize (struct x_display_info *dpyinfo, char *resource_name)
9607 dpyinfo->xim = NULL;
9608 #ifdef HAVE_XIM
9609 if (use_xim)
9611 #ifdef HAVE_X11R6_XIM
9612 struct xim_inst_t *xim_inst = xmalloc (sizeof *xim_inst);
9613 Bool ret;
9615 dpyinfo->xim_callback_data = xim_inst;
9616 xim_inst->dpyinfo = dpyinfo;
9617 xim_inst->resource_name = xstrdup (resource_name);
9618 ret = XRegisterIMInstantiateCallback
9619 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
9620 emacs_class, xim_instantiate_callback,
9621 /* This is XPointer in XFree86 but (XPointer *)
9622 on Tru64, at least, hence the configure test. */
9623 (XRegisterIMInstantiateCallback_arg6) xim_inst);
9624 eassert (ret == True);
9625 #else /* not HAVE_X11R6_XIM */
9626 xim_open_dpy (dpyinfo, resource_name);
9627 #endif /* not HAVE_X11R6_XIM */
9629 #endif /* HAVE_XIM */
9633 /* Close the connection to the XIM server on display DPYINFO. */
9635 static void
9636 xim_close_dpy (struct x_display_info *dpyinfo)
9638 #ifdef HAVE_XIM
9639 if (use_xim)
9641 #ifdef HAVE_X11R6_XIM
9642 struct xim_inst_t *xim_inst = dpyinfo->xim_callback_data;
9644 if (dpyinfo->display)
9646 Bool ret = XUnregisterIMInstantiateCallback
9647 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
9648 emacs_class, xim_instantiate_callback,
9649 (XRegisterIMInstantiateCallback_arg6) xim_inst);
9650 eassert (ret == True);
9652 xfree (xim_inst->resource_name);
9653 xfree (xim_inst);
9654 #endif /* HAVE_X11R6_XIM */
9655 if (dpyinfo->display)
9656 XCloseIM (dpyinfo->xim);
9657 dpyinfo->xim = NULL;
9658 XFree (dpyinfo->xim_styles);
9660 #endif /* HAVE_XIM */
9663 #endif /* not HAVE_X11R6_XIM */
9667 /* Calculate the absolute position in frame F
9668 from its current recorded position values and gravity. */
9670 static void
9671 x_calc_absolute_position (struct frame *f)
9673 int flags = f->size_hint_flags;
9675 /* We have nothing to do if the current position
9676 is already for the top-left corner. */
9677 if (! ((flags & XNegative) || (flags & YNegative)))
9678 return;
9680 /* Treat negative positions as relative to the leftmost bottommost
9681 position that fits on the screen. */
9682 if (flags & XNegative)
9683 f->left_pos = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
9684 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
9687 int height = FRAME_PIXEL_HEIGHT (f);
9689 #if defined USE_X_TOOLKIT && defined USE_MOTIF
9690 /* Something is fishy here. When using Motif, starting Emacs with
9691 `-g -0-0', the frame appears too low by a few pixels.
9693 This seems to be so because initially, while Emacs is starting,
9694 the column widget's height and the frame's pixel height are
9695 different. The column widget's height is the right one. In
9696 later invocations, when Emacs is up, the frame's pixel height
9697 is right, though.
9699 It's not obvious where the initial small difference comes from.
9700 2000-12-01, gerd. */
9702 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
9703 #endif
9705 if (flags & YNegative)
9706 f->top_pos = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
9707 - height + f->top_pos;
9710 /* The left_pos and top_pos
9711 are now relative to the top and left screen edges,
9712 so the flags should correspond. */
9713 f->size_hint_flags &= ~ (XNegative | YNegative);
9716 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
9717 to really change the position, and 0 when calling from
9718 x_make_frame_visible (in that case, XOFF and YOFF are the current
9719 position values). It is -1 when calling from x_set_frame_parameters,
9720 which means, do adjust for borders but don't change the gravity. */
9722 void
9723 x_set_offset (struct frame *f, register int xoff, register int yoff, int change_gravity)
9725 int modified_top, modified_left;
9727 if (change_gravity > 0)
9729 f->top_pos = yoff;
9730 f->left_pos = xoff;
9731 f->size_hint_flags &= ~ (XNegative | YNegative);
9732 if (xoff < 0)
9733 f->size_hint_flags |= XNegative;
9734 if (yoff < 0)
9735 f->size_hint_flags |= YNegative;
9736 f->win_gravity = NorthWestGravity;
9738 x_calc_absolute_position (f);
9740 block_input ();
9741 x_wm_set_size_hint (f, 0, false);
9743 modified_left = f->left_pos;
9744 modified_top = f->top_pos;
9746 if (change_gravity != 0 && FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
9748 /* Some WMs (twm, wmaker at least) has an offset that is smaller
9749 than the WM decorations. So we use the calculated offset instead
9750 of the WM decoration sizes here (x/y_pixels_outer_diff). */
9751 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
9752 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
9755 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9756 modified_left, modified_top);
9758 x_sync_with_move (f, f->left_pos, f->top_pos,
9759 FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN);
9761 /* change_gravity is non-zero when this function is called from Lisp to
9762 programmatically move a frame. In that case, we call
9763 x_check_expected_move to discover if we have a "Type A" or "Type B"
9764 window manager, and, for a "Type A" window manager, adjust the position
9765 of the frame.
9767 We call x_check_expected_move if a programmatic move occurred, and
9768 either the window manager type (A/B) is unknown or it is Type A but we
9769 need to compute the top/left offset adjustment for this frame. */
9771 if (change_gravity != 0
9772 && (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
9773 || (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
9774 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
9775 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
9776 x_check_expected_move (f, modified_left, modified_top);
9778 unblock_input ();
9781 /* Return true if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
9782 on the root window for frame F contains ATOMNAME.
9783 This is how a WM check shall be done according to the Window Manager
9784 Specification/Extended Window Manager Hints at
9785 http://freedesktop.org/wiki/Specifications/wm-spec. */
9787 bool
9788 x_wm_supports (struct frame *f, Atom want_atom)
9790 Atom actual_type;
9791 unsigned long actual_size, bytes_remaining;
9792 int i, rc, actual_format;
9793 bool ret;
9794 Window wmcheck_window;
9795 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9796 Window target_window = dpyinfo->root_window;
9797 int max_len = 65536;
9798 Display *dpy = FRAME_X_DISPLAY (f);
9799 unsigned char *tmp_data = NULL;
9800 Atom target_type = XA_WINDOW;
9802 block_input ();
9804 x_catch_errors (dpy);
9805 rc = XGetWindowProperty (dpy, target_window,
9806 dpyinfo->Xatom_net_supporting_wm_check,
9807 0, max_len, False, target_type,
9808 &actual_type, &actual_format, &actual_size,
9809 &bytes_remaining, &tmp_data);
9811 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
9813 if (tmp_data) XFree (tmp_data);
9814 x_uncatch_errors ();
9815 unblock_input ();
9816 return false;
9819 wmcheck_window = *(Window *) tmp_data;
9820 XFree (tmp_data);
9822 /* Check if window exists. */
9823 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
9824 x_sync (f);
9825 if (x_had_errors_p (dpy))
9827 x_uncatch_errors ();
9828 unblock_input ();
9829 return false;
9832 if (dpyinfo->net_supported_window != wmcheck_window)
9834 /* Window changed, reload atoms */
9835 if (dpyinfo->net_supported_atoms != NULL)
9836 XFree (dpyinfo->net_supported_atoms);
9837 dpyinfo->net_supported_atoms = NULL;
9838 dpyinfo->nr_net_supported_atoms = 0;
9839 dpyinfo->net_supported_window = 0;
9841 target_type = XA_ATOM;
9842 tmp_data = NULL;
9843 rc = XGetWindowProperty (dpy, target_window,
9844 dpyinfo->Xatom_net_supported,
9845 0, max_len, False, target_type,
9846 &actual_type, &actual_format, &actual_size,
9847 &bytes_remaining, &tmp_data);
9849 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
9851 if (tmp_data) XFree (tmp_data);
9852 x_uncatch_errors ();
9853 unblock_input ();
9854 return false;
9857 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
9858 dpyinfo->nr_net_supported_atoms = actual_size;
9859 dpyinfo->net_supported_window = wmcheck_window;
9862 ret = false;
9864 for (i = 0; !ret && i < dpyinfo->nr_net_supported_atoms; ++i)
9865 ret = dpyinfo->net_supported_atoms[i] == want_atom;
9867 x_uncatch_errors ();
9868 unblock_input ();
9870 return ret;
9873 static void
9874 set_wm_state (Lisp_Object frame, bool add, Atom atom, Atom value)
9876 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (XFRAME (frame));
9878 x_send_client_event (frame, make_number (0), frame,
9879 dpyinfo->Xatom_net_wm_state,
9880 make_number (32),
9881 /* 1 = add, 0 = remove */
9882 Fcons
9883 (make_number (add),
9884 Fcons
9885 (make_fixnum_or_float (atom),
9886 (value != 0
9887 ? list1 (make_fixnum_or_float (value))
9888 : Qnil))));
9891 void
9892 x_set_sticky (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
9894 Lisp_Object frame;
9895 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9897 XSETFRAME (frame, f);
9899 set_wm_state (frame, !NILP (new_value),
9900 dpyinfo->Xatom_net_wm_state_sticky, None);
9903 /* Return the current _NET_WM_STATE.
9904 SIZE_STATE is set to one of the FULLSCREEN_* values.
9905 Set *STICKY to the sticky state.
9907 Return true iff we are not hidden. */
9909 static bool
9910 get_current_wm_state (struct frame *f,
9911 Window window,
9912 int *size_state,
9913 bool *sticky)
9915 Atom actual_type;
9916 unsigned long actual_size, bytes_remaining;
9917 int i, rc, actual_format;
9918 bool is_hidden = false;
9919 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9920 long max_len = 65536;
9921 Display *dpy = FRAME_X_DISPLAY (f);
9922 unsigned char *tmp_data = NULL;
9923 Atom target_type = XA_ATOM;
9925 *sticky = false;
9926 *size_state = FULLSCREEN_NONE;
9928 block_input ();
9929 x_catch_errors (dpy);
9930 rc = XGetWindowProperty (dpy, window, dpyinfo->Xatom_net_wm_state,
9931 0, max_len, False, target_type,
9932 &actual_type, &actual_format, &actual_size,
9933 &bytes_remaining, &tmp_data);
9935 if (rc != Success || actual_type != target_type || x_had_errors_p (dpy))
9937 if (tmp_data) XFree (tmp_data);
9938 x_uncatch_errors ();
9939 unblock_input ();
9940 return !FRAME_ICONIFIED_P (f);
9943 x_uncatch_errors ();
9945 for (i = 0; i < actual_size; ++i)
9947 Atom a = ((Atom*)tmp_data)[i];
9948 if (a == dpyinfo->Xatom_net_wm_state_hidden)
9949 is_hidden = true;
9950 else if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
9952 if (*size_state == FULLSCREEN_HEIGHT)
9953 *size_state = FULLSCREEN_MAXIMIZED;
9954 else
9955 *size_state = FULLSCREEN_WIDTH;
9957 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
9959 if (*size_state == FULLSCREEN_WIDTH)
9960 *size_state = FULLSCREEN_MAXIMIZED;
9961 else
9962 *size_state = FULLSCREEN_HEIGHT;
9964 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen)
9965 *size_state = FULLSCREEN_BOTH;
9966 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
9967 *sticky = true;
9970 if (tmp_data) XFree (tmp_data);
9971 unblock_input ();
9972 return ! is_hidden;
9975 /* Do fullscreen as specified in extended window manager hints */
9977 static bool
9978 do_ewmh_fullscreen (struct frame *f)
9980 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9981 bool have_net_atom = x_wm_supports (f, dpyinfo->Xatom_net_wm_state);
9982 int cur;
9983 bool dummy;
9985 get_current_wm_state (f, FRAME_OUTER_WINDOW (f), &cur, &dummy);
9987 /* Some window managers don't say they support _NET_WM_STATE, but they do say
9988 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
9989 if (!have_net_atom)
9990 have_net_atom = x_wm_supports (f, dpyinfo->Xatom_net_wm_state_fullscreen);
9992 if (have_net_atom && cur != f->want_fullscreen)
9994 Lisp_Object frame;
9996 XSETFRAME (frame, f);
9998 /* Keep number of calls to set_wm_state as low as possible.
9999 Some window managers, or possible Gtk+, hangs when too many
10000 are sent at once. */
10001 switch (f->want_fullscreen)
10003 case FULLSCREEN_BOTH:
10004 if (cur != FULLSCREEN_BOTH)
10005 set_wm_state (frame, true, dpyinfo->Xatom_net_wm_state_fullscreen,
10006 None);
10007 break;
10008 case FULLSCREEN_WIDTH:
10009 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_MAXIMIZED)
10011 set_wm_state (frame, false,
10012 dpyinfo->Xatom_net_wm_state_maximized_horz,
10013 dpyinfo->Xatom_net_wm_state_maximized_vert);
10014 set_wm_state (frame, true,
10015 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10017 else
10019 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_HEIGHT
10020 || cur == FULLSCREEN_MAXIMIZED)
10021 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10022 dpyinfo->Xatom_net_wm_state_maximized_vert);
10023 if (cur != FULLSCREEN_MAXIMIZED || x_frame_normalize_before_maximize)
10024 set_wm_state (frame, true,
10025 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10027 break;
10028 case FULLSCREEN_HEIGHT:
10029 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_MAXIMIZED)
10031 set_wm_state (frame, false,
10032 dpyinfo->Xatom_net_wm_state_maximized_horz,
10033 dpyinfo->Xatom_net_wm_state_maximized_vert);
10034 set_wm_state (frame, true,
10035 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10037 else
10039 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_WIDTH
10040 || cur == FULLSCREEN_MAXIMIZED)
10041 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10042 dpyinfo->Xatom_net_wm_state_maximized_horz);
10043 if (cur != FULLSCREEN_MAXIMIZED || x_frame_normalize_before_maximize)
10044 set_wm_state (frame, true,
10045 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10047 break;
10048 case FULLSCREEN_MAXIMIZED:
10049 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_BOTH)
10051 set_wm_state (frame, false,
10052 dpyinfo->Xatom_net_wm_state_fullscreen, None);
10053 set_wm_state (frame, true,
10054 dpyinfo->Xatom_net_wm_state_maximized_horz,
10055 dpyinfo->Xatom_net_wm_state_maximized_vert);
10057 else if (x_frame_normalize_before_maximize && cur == FULLSCREEN_WIDTH)
10059 set_wm_state (frame, false,
10060 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10061 set_wm_state (frame, true,
10062 dpyinfo->Xatom_net_wm_state_maximized_horz,
10063 dpyinfo->Xatom_net_wm_state_maximized_vert);
10065 else if (x_frame_normalize_before_maximize && cur == FULLSCREEN_HEIGHT)
10067 set_wm_state (frame, false,
10068 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10069 set_wm_state (frame, true,
10070 dpyinfo->Xatom_net_wm_state_maximized_horz,
10071 dpyinfo->Xatom_net_wm_state_maximized_vert);
10073 else
10075 if (cur == FULLSCREEN_BOTH)
10076 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10077 None);
10078 else if (cur == FULLSCREEN_HEIGHT)
10079 set_wm_state (frame, true,
10080 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10081 else if (cur == FULLSCREEN_WIDTH)
10082 set_wm_state (frame, true, None,
10083 dpyinfo->Xatom_net_wm_state_maximized_vert);
10084 else
10085 set_wm_state (frame, true,
10086 dpyinfo->Xatom_net_wm_state_maximized_horz,
10087 dpyinfo->Xatom_net_wm_state_maximized_vert);
10089 break;
10090 case FULLSCREEN_NONE:
10091 if (cur == FULLSCREEN_BOTH)
10092 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10093 None);
10094 else
10095 set_wm_state (frame, false,
10096 dpyinfo->Xatom_net_wm_state_maximized_horz,
10097 dpyinfo->Xatom_net_wm_state_maximized_vert);
10100 f->want_fullscreen = FULLSCREEN_NONE;
10104 return have_net_atom;
10107 static void
10108 XTfullscreen_hook (struct frame *f)
10110 if (FRAME_VISIBLE_P (f))
10112 block_input ();
10113 x_check_fullscreen (f);
10114 x_sync (f);
10115 unblock_input ();
10120 static bool
10121 x_handle_net_wm_state (struct frame *f, const XPropertyEvent *event)
10123 int value = FULLSCREEN_NONE;
10124 Lisp_Object lval;
10125 bool sticky = false;
10126 bool not_hidden = get_current_wm_state (f, event->window, &value, &sticky);
10128 lval = Qnil;
10129 switch (value)
10131 case FULLSCREEN_WIDTH:
10132 lval = Qfullwidth;
10133 break;
10134 case FULLSCREEN_HEIGHT:
10135 lval = Qfullheight;
10136 break;
10137 case FULLSCREEN_BOTH:
10138 lval = Qfullboth;
10139 break;
10140 case FULLSCREEN_MAXIMIZED:
10141 lval = Qmaximized;
10142 break;
10145 frame_size_history_add
10146 (f, Qx_handle_net_wm_state, 0, 0,
10147 list2 (get_frame_param (f, Qfullscreen), lval));
10149 store_frame_param (f, Qfullscreen, lval);
10150 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
10152 return not_hidden;
10155 /* Check if we need to resize the frame due to a fullscreen request.
10156 If so needed, resize the frame. */
10157 static void
10158 x_check_fullscreen (struct frame *f)
10160 Lisp_Object lval = Qnil;
10162 if (do_ewmh_fullscreen (f))
10163 return;
10165 if (f->output_data.x->parent_desc != FRAME_DISPLAY_INFO (f)->root_window)
10166 return; /* Only fullscreen without WM or with EWM hints (above). */
10168 /* Setting fullscreen to nil doesn't do anything. We could save the
10169 last non-fullscreen size and restore it, but it seems like a
10170 lot of work for this unusual case (no window manager running). */
10172 if (f->want_fullscreen != FULLSCREEN_NONE)
10174 int width = FRAME_PIXEL_WIDTH (f), height = FRAME_PIXEL_HEIGHT (f);
10175 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10177 switch (f->want_fullscreen)
10179 /* No difference between these two when there is no WM */
10180 case FULLSCREEN_MAXIMIZED:
10181 lval = Qmaximized;
10182 width = x_display_pixel_width (dpyinfo);
10183 height = x_display_pixel_height (dpyinfo);
10184 break;
10185 case FULLSCREEN_BOTH:
10186 lval = Qfullboth;
10187 width = x_display_pixel_width (dpyinfo);
10188 height = x_display_pixel_height (dpyinfo);
10189 break;
10190 case FULLSCREEN_WIDTH:
10191 lval = Qfullwidth;
10192 width = x_display_pixel_width (dpyinfo);
10193 height = height + FRAME_MENUBAR_HEIGHT (f);
10194 break;
10195 case FULLSCREEN_HEIGHT:
10196 lval = Qfullheight;
10197 height = x_display_pixel_height (dpyinfo);
10198 break;
10199 default:
10200 emacs_abort ();
10203 frame_size_history_add
10204 (f, Qx_check_fullscreen, width, height, Qnil);
10206 x_wm_set_size_hint (f, 0, false);
10208 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10209 width, height);
10211 if (FRAME_VISIBLE_P (f))
10212 x_wait_for_event (f, ConfigureNotify);
10213 else
10215 change_frame_size (f, width, height - FRAME_MENUBAR_HEIGHT (f),
10216 false, true, false, true);
10217 x_sync (f);
10221 /* `x_net_wm_state' might have reset the fullscreen frame parameter,
10222 restore it. */
10223 store_frame_param (f, Qfullscreen, lval);
10226 /* This function is called by x_set_offset to determine whether the window
10227 manager interfered with the positioning of the frame. Type A window
10228 managers position the surrounding window manager decorations a small
10229 amount above and left of the user-supplied position. Type B window
10230 managers position the surrounding window manager decorations at the
10231 user-specified position. If we detect a Type A window manager, we
10232 compensate by moving the window right and down by the proper amount. */
10234 static void
10235 x_check_expected_move (struct frame *f, int expected_left, int expected_top)
10237 int current_left = 0, current_top = 0;
10239 /* x_real_positions returns the left and top offsets of the outermost
10240 window manager window around the frame. */
10242 x_real_positions (f, &current_left, &current_top);
10244 if (current_left != expected_left || current_top != expected_top)
10246 /* It's a "Type A" window manager. */
10248 int adjusted_left;
10249 int adjusted_top;
10251 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
10252 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
10253 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
10255 /* Now fix the mispositioned frame's location. */
10257 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
10258 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
10260 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10261 adjusted_left, adjusted_top);
10263 x_sync_with_move (f, expected_left, expected_top, false);
10265 else
10266 /* It's a "Type B" window manager. We don't have to adjust the
10267 frame's position. */
10269 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
10273 /* Wait for XGetGeometry to return up-to-date position information for a
10274 recently-moved frame. Call this immediately after calling XMoveWindow.
10275 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
10276 frame has been moved to, so we use a fuzzy position comparison instead
10277 of an exact comparison. */
10279 static void
10280 x_sync_with_move (struct frame *f, int left, int top, bool fuzzy)
10282 int count = 0;
10284 while (count++ < 50)
10286 int current_left = 0, current_top = 0;
10288 /* In theory, this call to XSync only needs to happen once, but in
10289 practice, it doesn't seem to work, hence the need for the surrounding
10290 loop. */
10292 XSync (FRAME_X_DISPLAY (f), False);
10293 x_real_positions (f, &current_left, &current_top);
10295 if (fuzzy)
10297 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
10298 pixels. */
10300 if (eabs (current_left - left) <= 10
10301 && eabs (current_top - top) <= 40)
10302 return;
10304 else if (current_left == left && current_top == top)
10305 return;
10308 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
10309 will then return up-to-date position info. */
10311 wait_reading_process_output (0, 500000000, 0, false, Qnil, NULL, 0);
10315 /* Wait for an event on frame F matching EVENTTYPE. */
10316 void
10317 x_wait_for_event (struct frame *f, int eventtype)
10319 int level = interrupt_input_blocked;
10321 fd_set fds;
10322 struct timespec tmo, tmo_at, time_now;
10323 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
10325 f->wait_event_type = eventtype;
10327 /* Set timeout to 0.1 second. Hopefully not noticeable.
10328 Maybe it should be configurable. */
10329 tmo = make_timespec (0, 100 * 1000 * 1000);
10330 tmo_at = timespec_add (current_timespec (), tmo);
10332 while (f->wait_event_type)
10334 pending_signals = true;
10335 totally_unblock_input ();
10336 /* XTread_socket is called after unblock. */
10337 block_input ();
10338 interrupt_input_blocked = level;
10340 FD_ZERO (&fds);
10341 FD_SET (fd, &fds);
10343 time_now = current_timespec ();
10344 if (timespec_cmp (tmo_at, time_now) < 0)
10345 break;
10347 tmo = timespec_sub (tmo_at, time_now);
10348 if (pselect (fd + 1, &fds, NULL, NULL, &tmo, NULL) == 0)
10349 break; /* Timeout */
10352 f->wait_event_type = 0;
10356 /* Change the size of frame F's X window to WIDTH/HEIGHT in the case F
10357 doesn't have a widget. If CHANGE_GRAVITY, change to
10358 top-left-corner window gravity for this size change and subsequent
10359 size changes. Otherwise leave the window gravity unchanged. */
10361 static void
10362 x_set_window_size_1 (struct frame *f, bool change_gravity,
10363 int width, int height)
10365 int pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
10366 int pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
10367 int old_width = FRAME_PIXEL_WIDTH (f);
10368 int old_height = FRAME_PIXEL_HEIGHT (f);
10369 Lisp_Object fullscreen = get_frame_param (f, Qfullscreen);
10371 if (change_gravity) f->win_gravity = NorthWestGravity;
10372 x_wm_set_size_hint (f, 0, false);
10374 /* When the frame is fullheight and we only want to change the width
10375 or it is fullwidth and we only want to change the height we should
10376 be able to preserve the fullscreen property. However, due to the
10377 fact that we have to send a resize request anyway, the window
10378 manager will abolish it. At least the respective size should
10379 remain unchanged but giving the frame back its normal size will
10380 be broken ... */
10381 if (EQ (fullscreen, Qfullwidth) && width == FRAME_TEXT_WIDTH (f))
10383 frame_size_history_add
10384 (f, Qxg_frame_set_char_size_1, width, height,
10385 list2 (make_number (old_height),
10386 make_number (pixelheight + FRAME_MENUBAR_HEIGHT (f))));
10388 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10389 old_width, pixelheight + FRAME_MENUBAR_HEIGHT (f));
10391 else if (EQ (fullscreen, Qfullheight) && height == FRAME_TEXT_HEIGHT (f))
10393 frame_size_history_add
10394 (f, Qxg_frame_set_char_size_2, width, height,
10395 list2 (make_number (old_width), make_number (pixelwidth)));
10397 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10398 pixelwidth, old_height);
10401 else
10403 frame_size_history_add
10404 (f, Qxg_frame_set_char_size_3, width, height,
10405 list2 (make_number (pixelwidth + FRAME_TOOLBAR_WIDTH (f)),
10406 make_number (pixelheight + FRAME_TOOLBAR_HEIGHT (f)
10407 + FRAME_MENUBAR_HEIGHT (f))));
10409 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10410 pixelwidth, pixelheight + FRAME_MENUBAR_HEIGHT (f));
10411 fullscreen = Qnil;
10416 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
10417 receive in the ConfigureNotify event; if we get what we asked
10418 for, then the event won't cause the screen to become garbaged, so
10419 we have to make sure to do it here. */
10420 SET_FRAME_GARBAGED (f);
10422 /* Now, strictly speaking, we can't be sure that this is accurate,
10423 but the window manager will get around to dealing with the size
10424 change request eventually, and we'll hear how it went when the
10425 ConfigureNotify event gets here.
10427 We could just not bother storing any of this information here,
10428 and let the ConfigureNotify event set everything up, but that
10429 might be kind of confusing to the Lisp code, since size changes
10430 wouldn't be reported in the frame parameters until some random
10431 point in the future when the ConfigureNotify event arrives.
10433 Pass true for DELAY since we can't run Lisp code inside of
10434 a BLOCK_INPUT. */
10436 /* But the ConfigureNotify may in fact never arrive, and then this is
10437 not right if the frame is visible. Instead wait (with timeout)
10438 for the ConfigureNotify. */
10439 if (FRAME_VISIBLE_P (f))
10441 x_wait_for_event (f, ConfigureNotify);
10443 if (!NILP (fullscreen))
10444 /* Try to restore fullscreen state. */
10446 store_frame_param (f, Qfullscreen, fullscreen);
10447 x_set_fullscreen (f, fullscreen, fullscreen);
10450 else
10452 change_frame_size (f, width, height, false, true, false, true);
10453 x_sync (f);
10458 /* Call this to change the size of frame F's x-window.
10459 If CHANGE_GRAVITY, change to top-left-corner window gravity
10460 for this size change and subsequent size changes.
10461 Otherwise we leave the window gravity unchanged. */
10463 void
10464 x_set_window_size (struct frame *f, bool change_gravity,
10465 int width, int height, bool pixelwise)
10467 block_input ();
10469 /* The following breaks our calculations. If it's really needed,
10470 think of something else. */
10471 #if false
10472 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
10474 int text_width, text_height;
10476 /* When the frame is maximized/fullscreen or running under for
10477 example Xmonad, x_set_window_size_1 will be a no-op.
10478 In that case, the right thing to do is extend rows/width to
10479 the current frame size. We do that first if x_set_window_size_1
10480 turns out to not be a no-op (there is no way to know).
10481 The size will be adjusted again if the frame gets a
10482 ConfigureNotify event as a result of x_set_window_size. */
10483 int pixelh = FRAME_PIXEL_HEIGHT (f);
10484 #ifdef USE_X_TOOLKIT
10485 /* The menu bar is not part of text lines. The tool bar
10486 is however. */
10487 pixelh -= FRAME_MENUBAR_HEIGHT (f);
10488 #endif
10489 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, FRAME_PIXEL_WIDTH (f));
10490 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelh);
10492 change_frame_size (f, text_width, text_height, false, true, false, true);
10494 #endif
10496 /* Pixelize width and height, if necessary. */
10497 if (! pixelwise)
10499 width = width * FRAME_COLUMN_WIDTH (f);
10500 height = height * FRAME_LINE_HEIGHT (f);
10503 #ifdef USE_GTK
10504 if (FRAME_GTK_WIDGET (f))
10505 xg_frame_set_char_size (f, width, height);
10506 else
10507 x_set_window_size_1 (f, change_gravity, width, height);
10508 #else /* not USE_GTK */
10509 x_set_window_size_1 (f, change_gravity, width, height);
10510 x_clear_under_internal_border (f);
10511 #endif /* not USE_GTK */
10513 /* If cursor was outside the new size, mark it as off. */
10514 mark_window_cursors_off (XWINDOW (f->root_window));
10516 /* Clear out any recollection of where the mouse highlighting was,
10517 since it might be in a place that's outside the new frame size.
10518 Actually checking whether it is outside is a pain in the neck,
10519 so don't try--just let the highlighting be done afresh with new size. */
10520 cancel_mouse_face (f);
10522 unblock_input ();
10525 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
10527 void
10528 frame_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
10530 block_input ();
10532 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
10533 0, 0, 0, 0, pix_x, pix_y);
10534 unblock_input ();
10537 /* Raise frame F. */
10539 void
10540 x_raise_frame (struct frame *f)
10542 block_input ();
10543 if (FRAME_VISIBLE_P (f))
10544 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
10545 XFlush (FRAME_X_DISPLAY (f));
10546 unblock_input ();
10549 /* Lower frame F. */
10551 static void
10552 x_lower_frame (struct frame *f)
10554 if (FRAME_VISIBLE_P (f))
10556 block_input ();
10557 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
10558 XFlush (FRAME_X_DISPLAY (f));
10559 unblock_input ();
10563 /* Request focus with XEmbed */
10565 void
10566 xembed_request_focus (struct frame *f)
10568 /* See XEmbed Protocol Specification at
10569 http://freedesktop.org/wiki/Specifications/xembed-spec */
10570 if (FRAME_VISIBLE_P (f))
10571 xembed_send_message (f, CurrentTime,
10572 XEMBED_REQUEST_FOCUS, 0, 0, 0);
10575 /* Activate frame with Extended Window Manager Hints */
10577 void
10578 x_ewmh_activate_frame (struct frame *f)
10580 /* See Window Manager Specification/Extended Window Manager Hints at
10581 http://freedesktop.org/wiki/Specifications/wm-spec */
10583 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10585 if (FRAME_VISIBLE_P (f) && x_wm_supports (f, dpyinfo->Xatom_net_active_window))
10587 Lisp_Object frame;
10588 XSETFRAME (frame, f);
10589 x_send_client_event (frame, make_number (0), frame,
10590 dpyinfo->Xatom_net_active_window,
10591 make_number (32),
10592 list2i (1, dpyinfo->last_user_time));
10596 static void
10597 XTframe_raise_lower (struct frame *f, bool raise_flag)
10599 if (raise_flag)
10600 x_raise_frame (f);
10601 else
10602 x_lower_frame (f);
10605 /* XEmbed implementation. */
10607 #if defined USE_X_TOOLKIT || ! defined USE_GTK
10609 /* XEmbed implementation. */
10611 #define XEMBED_VERSION 0
10613 static void
10614 xembed_set_info (struct frame *f, enum xembed_info flags)
10616 unsigned long data[2];
10617 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10619 data[0] = XEMBED_VERSION;
10620 data[1] = flags;
10622 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10623 dpyinfo->Xatom_XEMBED_INFO, dpyinfo->Xatom_XEMBED_INFO,
10624 32, PropModeReplace, (unsigned char *) data, 2);
10626 #endif /* defined USE_X_TOOLKIT || ! defined USE_GTK */
10628 static void
10629 xembed_send_message (struct frame *f, Time t, enum xembed_message msg,
10630 long int detail, long int data1, long int data2)
10632 XEvent event;
10634 event.xclient.type = ClientMessage;
10635 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
10636 event.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_XEMBED;
10637 event.xclient.format = 32;
10638 event.xclient.data.l[0] = t;
10639 event.xclient.data.l[1] = msg;
10640 event.xclient.data.l[2] = detail;
10641 event.xclient.data.l[3] = data1;
10642 event.xclient.data.l[4] = data2;
10644 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
10645 False, NoEventMask, &event);
10646 XSync (FRAME_X_DISPLAY (f), False);
10649 /* Change of visibility. */
10651 /* This tries to wait until the frame is really visible.
10652 However, if the window manager asks the user where to position
10653 the frame, this will return before the user finishes doing that.
10654 The frame will not actually be visible at that time,
10655 but it will become visible later when the window manager
10656 finishes with it. */
10658 void
10659 x_make_frame_visible (struct frame *f)
10661 int original_top, original_left;
10662 int tries = 0;
10664 block_input ();
10666 x_set_bitmap_icon (f);
10668 if (! FRAME_VISIBLE_P (f))
10670 /* We test FRAME_GARBAGED_P here to make sure we don't
10671 call x_set_offset a second time
10672 if we get to x_make_frame_visible a second time
10673 before the window gets really visible. */
10674 if (! FRAME_ICONIFIED_P (f)
10675 && ! FRAME_X_EMBEDDED_P (f)
10676 && ! f->output_data.x->asked_for_visible)
10677 x_set_offset (f, f->left_pos, f->top_pos, 0);
10679 f->output_data.x->asked_for_visible = true;
10681 if (! EQ (Vx_no_window_manager, Qt))
10682 x_wm_set_window_state (f, NormalState);
10683 #ifdef USE_X_TOOLKIT
10684 if (FRAME_X_EMBEDDED_P (f))
10685 xembed_set_info (f, XEMBED_MAPPED);
10686 else
10688 /* This was XtPopup, but that did nothing for an iconified frame. */
10689 XtMapWidget (f->output_data.x->widget);
10691 #else /* not USE_X_TOOLKIT */
10692 #ifdef USE_GTK
10693 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
10694 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
10695 #else
10696 if (FRAME_X_EMBEDDED_P (f))
10697 xembed_set_info (f, XEMBED_MAPPED);
10698 else
10699 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10700 #endif /* not USE_GTK */
10701 #endif /* not USE_X_TOOLKIT */
10704 XFlush (FRAME_X_DISPLAY (f));
10706 /* Synchronize to ensure Emacs knows the frame is visible
10707 before we do anything else. We do this loop with input not blocked
10708 so that incoming events are handled. */
10710 Lisp_Object frame;
10711 /* This must be before UNBLOCK_INPUT
10712 since events that arrive in response to the actions above
10713 will set it when they are handled. */
10714 bool previously_visible = f->output_data.x->has_been_visible;
10716 original_left = f->left_pos;
10717 original_top = f->top_pos;
10719 /* This must come after we set COUNT. */
10720 unblock_input ();
10722 /* We unblock here so that arriving X events are processed. */
10724 /* Now move the window back to where it was "supposed to be".
10725 But don't do it if the gravity is negative.
10726 When the gravity is negative, this uses a position
10727 that is 3 pixels too low. Perhaps that's really the border width.
10729 Don't do this if the window has never been visible before,
10730 because the window manager may choose the position
10731 and we don't want to override it. */
10733 if (! FRAME_VISIBLE_P (f)
10734 && ! FRAME_ICONIFIED_P (f)
10735 && ! FRAME_X_EMBEDDED_P (f)
10736 && f->win_gravity == NorthWestGravity
10737 && previously_visible)
10739 Drawable rootw;
10740 int x, y;
10741 unsigned int width, height, border, depth;
10743 block_input ();
10745 /* On some window managers (such as FVWM) moving an existing
10746 window, even to the same place, causes the window manager
10747 to introduce an offset. This can cause the window to move
10748 to an unexpected location. Check the geometry (a little
10749 slow here) and then verify that the window is in the right
10750 place. If the window is not in the right place, move it
10751 there, and take the potential window manager hit. */
10752 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10753 &rootw, &x, &y, &width, &height, &border, &depth);
10755 if (original_left != x || original_top != y)
10756 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10757 original_left, original_top);
10759 unblock_input ();
10762 XSETFRAME (frame, f);
10764 /* Process X events until a MapNotify event has been seen. */
10765 while (!FRAME_VISIBLE_P (f))
10767 /* Force processing of queued events. */
10768 x_sync (f);
10770 /* If on another desktop, the deiconify/map may be ignored and the
10771 frame never becomes visible. XMonad does this.
10772 Prevent an endless loop. */
10773 if (FRAME_ICONIFIED_P (f) && ++tries > 100)
10774 break;
10776 /* This hack is still in use at least for Cygwin. See
10777 http://lists.gnu.org/archive/html/emacs-devel/2013-12/msg00351.html.
10779 Machines that do polling rather than SIGIO have been
10780 observed to go into a busy-wait here. So we'll fake an
10781 alarm signal to let the handler know that there's something
10782 to be read. We used to raise a real alarm, but it seems
10783 that the handler isn't always enabled here. This is
10784 probably a bug. */
10785 if (input_polling_used ())
10787 /* It could be confusing if a real alarm arrives while
10788 processing the fake one. Turn it off and let the
10789 handler reset it. */
10790 int old_poll_suppress_count = poll_suppress_count;
10791 poll_suppress_count = 1;
10792 poll_for_input_1 ();
10793 poll_suppress_count = old_poll_suppress_count;
10796 if (XPending (FRAME_X_DISPLAY (f)))
10798 XEvent xev;
10799 XNextEvent (FRAME_X_DISPLAY (f), &xev);
10800 x_dispatch_event (&xev, FRAME_X_DISPLAY (f));
10806 /* Change from mapped state to withdrawn state. */
10808 /* Make the frame visible (mapped and not iconified). */
10810 void
10811 x_make_frame_invisible (struct frame *f)
10813 Window window;
10815 /* Use the frame's outermost window, not the one we normally draw on. */
10816 window = FRAME_OUTER_WINDOW (f);
10818 /* Don't keep the highlight on an invisible frame. */
10819 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
10820 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
10822 block_input ();
10824 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
10825 that the current position of the window is user-specified, rather than
10826 program-specified, so that when the window is mapped again, it will be
10827 placed at the same location, without forcing the user to position it
10828 by hand again (they have already done that once for this window.) */
10829 x_wm_set_size_hint (f, 0, true);
10831 #ifdef USE_GTK
10832 if (FRAME_GTK_OUTER_WIDGET (f))
10833 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
10834 else
10835 #else
10836 if (FRAME_X_EMBEDDED_P (f))
10837 xembed_set_info (f, 0);
10838 else
10839 #endif
10842 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
10843 DefaultScreen (FRAME_X_DISPLAY (f))))
10845 unblock_input ();
10846 error ("Can't notify window manager of window withdrawal");
10850 /* We can't distinguish this from iconification
10851 just by the event that we get from the server.
10852 So we can't win using the usual strategy of letting
10853 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
10854 and synchronize with the server to make sure we agree. */
10855 SET_FRAME_VISIBLE (f, 0);
10856 SET_FRAME_ICONIFIED (f, false);
10858 x_sync (f);
10860 unblock_input ();
10863 /* Change window state from mapped to iconified. */
10865 void
10866 x_iconify_frame (struct frame *f)
10868 #ifdef USE_X_TOOLKIT
10869 int result;
10870 #endif
10872 /* Don't keep the highlight on an invisible frame. */
10873 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
10874 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
10876 if (FRAME_ICONIFIED_P (f))
10877 return;
10879 block_input ();
10881 x_set_bitmap_icon (f);
10883 #if defined (USE_GTK)
10884 if (FRAME_GTK_OUTER_WIDGET (f))
10886 if (! FRAME_VISIBLE_P (f))
10887 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
10889 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
10890 SET_FRAME_VISIBLE (f, 0);
10891 SET_FRAME_ICONIFIED (f, true);
10892 unblock_input ();
10893 return;
10895 #endif
10897 #ifdef USE_X_TOOLKIT
10899 if (! FRAME_VISIBLE_P (f))
10901 if (! EQ (Vx_no_window_manager, Qt))
10902 x_wm_set_window_state (f, IconicState);
10903 /* This was XtPopup, but that did nothing for an iconified frame. */
10904 XtMapWidget (f->output_data.x->widget);
10905 /* The server won't give us any event to indicate
10906 that an invisible frame was changed to an icon,
10907 so we have to record it here. */
10908 SET_FRAME_VISIBLE (f, 0);
10909 SET_FRAME_ICONIFIED (f, true);
10910 unblock_input ();
10911 return;
10914 result = XIconifyWindow (FRAME_X_DISPLAY (f),
10915 XtWindow (f->output_data.x->widget),
10916 DefaultScreen (FRAME_X_DISPLAY (f)));
10917 unblock_input ();
10919 if (!result)
10920 error ("Can't notify window manager of iconification");
10922 SET_FRAME_ICONIFIED (f, true);
10923 SET_FRAME_VISIBLE (f, 0);
10925 block_input ();
10926 XFlush (FRAME_X_DISPLAY (f));
10927 unblock_input ();
10928 #else /* not USE_X_TOOLKIT */
10930 /* Make sure the X server knows where the window should be positioned,
10931 in case the user deiconifies with the window manager. */
10932 if (! FRAME_VISIBLE_P (f)
10933 && ! FRAME_ICONIFIED_P (f)
10934 && ! FRAME_X_EMBEDDED_P (f))
10935 x_set_offset (f, f->left_pos, f->top_pos, 0);
10937 /* Since we don't know which revision of X we're running, we'll use both
10938 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
10940 /* X11R4: send a ClientMessage to the window manager using the
10941 WM_CHANGE_STATE type. */
10943 XEvent msg;
10945 msg.xclient.window = FRAME_X_WINDOW (f);
10946 msg.xclient.type = ClientMessage;
10947 msg.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_wm_change_state;
10948 msg.xclient.format = 32;
10949 msg.xclient.data.l[0] = IconicState;
10951 if (! XSendEvent (FRAME_X_DISPLAY (f),
10952 DefaultRootWindow (FRAME_X_DISPLAY (f)),
10953 False,
10954 SubstructureRedirectMask | SubstructureNotifyMask,
10955 &msg))
10957 unblock_input ();
10958 error ("Can't notify window manager of iconification");
10962 /* X11R3: set the initial_state field of the window manager hints to
10963 IconicState. */
10964 x_wm_set_window_state (f, IconicState);
10966 if (!FRAME_VISIBLE_P (f))
10968 /* If the frame was withdrawn, before, we must map it. */
10969 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10972 SET_FRAME_ICONIFIED (f, true);
10973 SET_FRAME_VISIBLE (f, 0);
10975 XFlush (FRAME_X_DISPLAY (f));
10976 unblock_input ();
10977 #endif /* not USE_X_TOOLKIT */
10981 /* Free X resources of frame F. */
10983 void
10984 x_free_frame_resources (struct frame *f)
10986 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10987 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
10988 #ifdef USE_X_TOOLKIT
10989 Lisp_Object bar;
10990 struct scroll_bar *b;
10991 #endif
10993 block_input ();
10995 /* If a display connection is dead, don't try sending more
10996 commands to the X server. */
10997 if (dpyinfo->display)
10999 /* Always exit with visible pointer to avoid weird issue
11000 with Xfixes (Bug#17609). */
11001 if (f->pointer_invisible)
11002 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, 0);
11004 /* We must free faces before destroying windows because some
11005 font-driver (e.g. xft) access a window while finishing a
11006 face. */
11007 free_frame_faces (f);
11009 if (f->output_data.x->icon_desc)
11010 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
11012 #ifdef USE_X_TOOLKIT
11013 /* Explicitly destroy the scroll bars of the frame. Without
11014 this, we get "BadDrawable" errors from the toolkit later on,
11015 presumably from expose events generated for the disappearing
11016 toolkit scroll bars. */
11017 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
11019 b = XSCROLL_BAR (bar);
11020 x_scroll_bar_remove (b);
11022 #endif
11024 #ifdef HAVE_X_I18N
11025 if (FRAME_XIC (f))
11026 free_frame_xic (f);
11027 #endif
11029 x_free_cr_resources (f);
11030 #ifdef USE_X_TOOLKIT
11031 if (f->output_data.x->widget)
11033 XtDestroyWidget (f->output_data.x->widget);
11034 f->output_data.x->widget = NULL;
11036 /* Tooltips don't have widgets, only a simple X window, even if
11037 we are using a toolkit. */
11038 else if (FRAME_X_WINDOW (f))
11039 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11041 free_frame_menubar (f);
11043 if (f->shell_position)
11044 xfree (f->shell_position);
11045 #else /* !USE_X_TOOLKIT */
11047 #ifdef USE_GTK
11048 xg_free_frame_widgets (f);
11049 #endif /* USE_GTK */
11051 if (FRAME_X_WINDOW (f))
11052 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11053 #endif /* !USE_X_TOOLKIT */
11055 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
11056 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
11057 unload_color (f, f->output_data.x->cursor_pixel);
11058 unload_color (f, f->output_data.x->cursor_foreground_pixel);
11059 unload_color (f, f->output_data.x->border_pixel);
11060 unload_color (f, f->output_data.x->mouse_pixel);
11062 if (f->output_data.x->scroll_bar_background_pixel != -1)
11063 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
11064 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
11065 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
11066 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
11067 /* Scrollbar shadow colors. */
11068 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
11069 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
11070 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
11071 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
11072 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
11073 if (f->output_data.x->white_relief.pixel != -1)
11074 unload_color (f, f->output_data.x->white_relief.pixel);
11075 if (f->output_data.x->black_relief.pixel != -1)
11076 unload_color (f, f->output_data.x->black_relief.pixel);
11078 x_free_gcs (f);
11080 /* Free extra GCs allocated by x_setup_relief_colors. */
11081 if (f->output_data.x->white_relief.gc)
11083 XFreeGC (dpyinfo->display, f->output_data.x->white_relief.gc);
11084 f->output_data.x->white_relief.gc = 0;
11086 if (f->output_data.x->black_relief.gc)
11088 XFreeGC (dpyinfo->display, f->output_data.x->black_relief.gc);
11089 f->output_data.x->black_relief.gc = 0;
11092 /* Free cursors. */
11093 if (f->output_data.x->text_cursor != 0)
11094 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->text_cursor);
11095 if (f->output_data.x->nontext_cursor != 0)
11096 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->nontext_cursor);
11097 if (f->output_data.x->modeline_cursor != 0)
11098 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->modeline_cursor);
11099 if (f->output_data.x->hand_cursor != 0)
11100 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hand_cursor);
11101 if (f->output_data.x->hourglass_cursor != 0)
11102 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hourglass_cursor);
11103 if (f->output_data.x->horizontal_drag_cursor != 0)
11104 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->horizontal_drag_cursor);
11105 if (f->output_data.x->vertical_drag_cursor != 0)
11106 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->vertical_drag_cursor);
11108 XFlush (FRAME_X_DISPLAY (f));
11111 xfree (f->output_data.x->saved_menu_event);
11112 xfree (f->output_data.x);
11113 f->output_data.x = NULL;
11115 if (f == dpyinfo->x_focus_frame)
11116 dpyinfo->x_focus_frame = 0;
11117 if (f == dpyinfo->x_focus_event_frame)
11118 dpyinfo->x_focus_event_frame = 0;
11119 if (f == dpyinfo->x_highlight_frame)
11120 dpyinfo->x_highlight_frame = 0;
11121 if (f == hlinfo->mouse_face_mouse_frame)
11122 reset_mouse_highlight (hlinfo);
11124 unblock_input ();
11128 /* Destroy the X window of frame F. */
11130 static void
11131 x_destroy_window (struct frame *f)
11133 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11135 /* If a display connection is dead, don't try sending more
11136 commands to the X server. */
11137 if (dpyinfo->display != 0)
11138 x_free_frame_resources (f);
11140 dpyinfo->reference_count--;
11144 /* Setting window manager hints. */
11146 /* Set the normal size hints for the window manager, for frame F.
11147 FLAGS is the flags word to use--or 0 meaning preserve the flags
11148 that the window now has.
11149 If USER_POSITION, set the USPosition
11150 flag (this is useful when FLAGS is 0).
11151 The GTK version is in gtkutils.c. */
11153 #ifndef USE_GTK
11154 void
11155 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
11157 XSizeHints size_hints;
11158 Window window = FRAME_OUTER_WINDOW (f);
11160 if (!window)
11161 return;
11163 #ifdef USE_X_TOOLKIT
11164 if (f->output_data.x->widget)
11166 widget_update_wm_size_hints (f->output_data.x->widget);
11167 return;
11169 #endif
11171 /* Setting PMaxSize caused various problems. */
11172 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
11174 size_hints.x = f->left_pos;
11175 size_hints.y = f->top_pos;
11177 size_hints.height = FRAME_PIXEL_HEIGHT (f);
11178 size_hints.width = FRAME_PIXEL_WIDTH (f);
11180 size_hints.width_inc = frame_resize_pixelwise ? 1 : FRAME_COLUMN_WIDTH (f);
11181 size_hints.height_inc = frame_resize_pixelwise ? 1 : FRAME_LINE_HEIGHT (f);
11183 size_hints.max_width = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
11184 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
11185 size_hints.max_height = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
11186 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
11188 /* Calculate the base and minimum sizes. */
11190 int base_width, base_height;
11191 int min_rows = 0, min_cols = 0;
11193 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
11194 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
11196 if (frame_resize_pixelwise)
11197 /* Needed to prevent a bad protocol error crash when making the
11198 frame size very small. */
11200 min_cols = 2 * min_cols;
11201 min_rows = 2 * min_rows;
11204 /* The window manager uses the base width hints to calculate the
11205 current number of rows and columns in the frame while
11206 resizing; min_width and min_height aren't useful for this
11207 purpose, since they might not give the dimensions for a
11208 zero-row, zero-column frame.
11210 We use the base_width and base_height members if we have
11211 them; otherwise, we set the min_width and min_height members
11212 to the size for a zero x zero frame. */
11214 size_hints.flags |= PBaseSize;
11215 size_hints.base_width = base_width;
11216 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
11217 size_hints.min_width = base_width + min_cols * FRAME_COLUMN_WIDTH (f);
11218 size_hints.min_height = base_height + min_rows * FRAME_LINE_HEIGHT (f);
11221 /* If we don't need the old flags, we don't need the old hint at all. */
11222 if (flags)
11224 size_hints.flags |= flags;
11225 goto no_read;
11229 XSizeHints hints; /* Sometimes I hate X Windows... */
11230 long supplied_return;
11231 int value;
11233 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
11234 &supplied_return);
11236 if (flags)
11237 size_hints.flags |= flags;
11238 else
11240 if (value == 0)
11241 hints.flags = 0;
11242 if (hints.flags & PSize)
11243 size_hints.flags |= PSize;
11244 if (hints.flags & PPosition)
11245 size_hints.flags |= PPosition;
11246 if (hints.flags & USPosition)
11247 size_hints.flags |= USPosition;
11248 if (hints.flags & USSize)
11249 size_hints.flags |= USSize;
11253 no_read:
11255 #ifdef PWinGravity
11256 size_hints.win_gravity = f->win_gravity;
11257 size_hints.flags |= PWinGravity;
11259 if (user_position)
11261 size_hints.flags &= ~ PPosition;
11262 size_hints.flags |= USPosition;
11264 #endif /* PWinGravity */
11266 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
11268 #endif /* not USE_GTK */
11270 /* Used for IconicState or NormalState */
11272 static void
11273 x_wm_set_window_state (struct frame *f, int state)
11275 #ifdef USE_X_TOOLKIT
11276 Arg al[1];
11278 XtSetArg (al[0], XtNinitialState, state);
11279 XtSetValues (f->output_data.x->widget, al, 1);
11280 #else /* not USE_X_TOOLKIT */
11281 Window window = FRAME_X_WINDOW (f);
11283 f->output_data.x->wm_hints.flags |= StateHint;
11284 f->output_data.x->wm_hints.initial_state = state;
11286 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
11287 #endif /* not USE_X_TOOLKIT */
11290 static void
11291 x_wm_set_icon_pixmap (struct frame *f, ptrdiff_t pixmap_id)
11293 Pixmap icon_pixmap, icon_mask;
11295 #if !defined USE_X_TOOLKIT && !defined USE_GTK
11296 Window window = FRAME_OUTER_WINDOW (f);
11297 #endif
11299 if (pixmap_id > 0)
11301 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
11302 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
11303 icon_mask = x_bitmap_mask (f, pixmap_id);
11304 f->output_data.x->wm_hints.icon_mask = icon_mask;
11306 else
11308 /* It seems there is no way to turn off use of an icon
11309 pixmap. */
11310 return;
11314 #ifdef USE_GTK
11316 xg_set_frame_icon (f, icon_pixmap, icon_mask);
11317 return;
11320 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
11323 Arg al[1];
11324 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
11325 XtSetValues (f->output_data.x->widget, al, 1);
11326 XtSetArg (al[0], XtNiconMask, icon_mask);
11327 XtSetValues (f->output_data.x->widget, al, 1);
11330 #else /* not USE_X_TOOLKIT && not USE_GTK */
11332 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
11333 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
11335 #endif /* not USE_X_TOOLKIT && not USE_GTK */
11338 void
11339 x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
11341 Window window = FRAME_OUTER_WINDOW (f);
11343 f->output_data.x->wm_hints.flags |= IconPositionHint;
11344 f->output_data.x->wm_hints.icon_x = icon_x;
11345 f->output_data.x->wm_hints.icon_y = icon_y;
11347 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
11351 /***********************************************************************
11352 Fonts
11353 ***********************************************************************/
11355 #ifdef GLYPH_DEBUG
11357 /* Check that FONT is valid on frame F. It is if it can be found in F's
11358 font table. */
11360 static void
11361 x_check_font (struct frame *f, struct font *font)
11363 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
11364 if (font->driver->check)
11365 eassert (font->driver->check (f, font) == 0);
11368 #endif /* GLYPH_DEBUG */
11371 /***********************************************************************
11372 Initialization
11373 ***********************************************************************/
11375 #ifdef USE_X_TOOLKIT
11376 static XrmOptionDescRec emacs_options[] = {
11377 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
11378 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
11380 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
11381 XrmoptionSepArg, NULL},
11382 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
11384 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
11385 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
11386 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
11387 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
11388 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
11389 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
11390 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
11393 /* Whether atimer for Xt timeouts is activated or not. */
11395 static bool x_timeout_atimer_activated_flag;
11397 #endif /* USE_X_TOOLKIT */
11399 static int x_initialized;
11401 /* Test whether two display-name strings agree up to the dot that separates
11402 the screen number from the server number. */
11403 static bool
11404 same_x_server (const char *name1, const char *name2)
11406 bool seen_colon = false;
11407 Lisp_Object sysname = Fsystem_name ();
11408 const char *system_name = SSDATA (sysname);
11409 ptrdiff_t system_name_length = SBYTES (sysname);
11410 ptrdiff_t length_until_period = 0;
11412 while (system_name[length_until_period] != 0
11413 && system_name[length_until_period] != '.')
11414 length_until_period++;
11416 /* Treat `unix' like an empty host name. */
11417 if (! strncmp (name1, "unix:", 5))
11418 name1 += 4;
11419 if (! strncmp (name2, "unix:", 5))
11420 name2 += 4;
11421 /* Treat this host's name like an empty host name. */
11422 if (! strncmp (name1, system_name, system_name_length)
11423 && name1[system_name_length] == ':')
11424 name1 += system_name_length;
11425 if (! strncmp (name2, system_name, system_name_length)
11426 && name2[system_name_length] == ':')
11427 name2 += system_name_length;
11428 /* Treat this host's domainless name like an empty host name. */
11429 if (! strncmp (name1, system_name, length_until_period)
11430 && name1[length_until_period] == ':')
11431 name1 += length_until_period;
11432 if (! strncmp (name2, system_name, length_until_period)
11433 && name2[length_until_period] == ':')
11434 name2 += length_until_period;
11436 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
11438 if (*name1 == ':')
11439 seen_colon = true;
11440 if (seen_colon && *name1 == '.')
11441 return true;
11443 return (seen_colon
11444 && (*name1 == '.' || *name1 == '\0')
11445 && (*name2 == '.' || *name2 == '\0'));
11448 /* Count number of set bits in mask and number of bits to shift to
11449 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
11450 to 5. */
11451 static void
11452 get_bits_and_offset (unsigned long mask, int *bits, int *offset)
11454 int nr = 0;
11455 int off = 0;
11457 while (!(mask & 1))
11459 off++;
11460 mask >>= 1;
11463 while (mask & 1)
11465 nr++;
11466 mask >>= 1;
11469 *offset = off;
11470 *bits = nr;
11473 /* Return true iff display DISPLAY is available for use.
11474 But don't permanently open it, just test its availability. */
11476 bool
11477 x_display_ok (const char *display)
11479 /* XOpenDisplay fails if it gets a signal. Block SIGIO which may arrive. */
11480 unrequest_sigio ();
11481 Display *dpy = XOpenDisplay (display);
11482 request_sigio ();
11483 if (!dpy)
11484 return false;
11485 XCloseDisplay (dpy);
11486 return true;
11489 #ifdef USE_GTK
11490 static void
11491 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
11492 const gchar *msg, gpointer user_data)
11494 if (!strstr (msg, "g_set_prgname"))
11495 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, msg);
11497 #endif
11499 /* Create invisible cursor on X display referred by DPYINFO. */
11501 static Cursor
11502 make_invisible_cursor (struct x_display_info *dpyinfo)
11504 Display *dpy = dpyinfo->display;
11505 static char const no_data[] = { 0 };
11506 Pixmap pix;
11507 XColor col;
11508 Cursor c = 0;
11510 x_catch_errors (dpy);
11511 pix = XCreateBitmapFromData (dpy, dpyinfo->root_window, no_data, 1, 1);
11512 if (! x_had_errors_p (dpy) && pix != None)
11514 Cursor pixc;
11515 col.pixel = 0;
11516 col.red = col.green = col.blue = 0;
11517 col.flags = DoRed | DoGreen | DoBlue;
11518 pixc = XCreatePixmapCursor (dpy, pix, pix, &col, &col, 0, 0);
11519 if (! x_had_errors_p (dpy) && pixc != None)
11520 c = pixc;
11521 XFreePixmap (dpy, pix);
11524 x_uncatch_errors ();
11526 return c;
11529 /* True if DPY supports Xfixes extension >= 4. */
11531 static bool
11532 x_probe_xfixes_extension (Display *dpy)
11534 #ifdef HAVE_XFIXES
11535 int major, minor;
11536 return XFixesQueryVersion (dpy, &major, &minor) && major >= 4;
11537 #else
11538 return false;
11539 #endif /* HAVE_XFIXES */
11542 /* Toggle mouse pointer visibility on frame F by using Xfixes functions. */
11544 static void
11545 xfixes_toggle_visible_pointer (struct frame *f, bool invisible)
11547 #ifdef HAVE_XFIXES
11548 if (invisible)
11549 XFixesHideCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11550 else
11551 XFixesShowCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11552 f->pointer_invisible = invisible;
11553 #else
11554 emacs_abort ();
11555 #endif /* HAVE_XFIXES */
11558 /* Toggle mouse pointer visibility on frame F by using invisible cursor. */
11560 static void
11561 x_toggle_visible_pointer (struct frame *f, bool invisible)
11563 eassert (FRAME_DISPLAY_INFO (f)->invisible_cursor != 0);
11564 if (invisible)
11565 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11566 FRAME_DISPLAY_INFO (f)->invisible_cursor);
11567 else
11568 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11569 f->output_data.x->current_cursor);
11570 f->pointer_invisible = invisible;
11573 /* Setup pointer blanking, prefer Xfixes if available. */
11575 static void
11576 x_setup_pointer_blanking (struct x_display_info *dpyinfo)
11578 /* FIXME: the brave tester should set EMACS_XFIXES because we're suspecting
11579 X server bug, see http://debbugs.gnu.org/cgi/bugreport.cgi?bug=17609. */
11580 if (egetenv ("EMACS_XFIXES") && x_probe_xfixes_extension (dpyinfo->display))
11581 dpyinfo->toggle_visible_pointer = xfixes_toggle_visible_pointer;
11582 else
11584 dpyinfo->toggle_visible_pointer = x_toggle_visible_pointer;
11585 dpyinfo->invisible_cursor = make_invisible_cursor (dpyinfo);
11589 /* Current X display connection identifier. Incremented for each next
11590 connection established. */
11591 static unsigned x_display_id;
11593 /* Open a connection to X display DISPLAY_NAME, and return
11594 the structure that describes the open display.
11595 If we cannot contact the display, return null. */
11597 struct x_display_info *
11598 x_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
11600 Display *dpy;
11601 struct terminal *terminal;
11602 struct x_display_info *dpyinfo;
11603 XrmDatabase xrdb;
11604 ptrdiff_t lim;
11606 block_input ();
11608 if (!x_initialized)
11610 x_initialize ();
11611 ++x_initialized;
11614 if (! x_display_ok (SSDATA (display_name)))
11615 error ("Display %s can't be opened", SSDATA (display_name));
11617 #ifdef USE_GTK
11619 #define NUM_ARGV 10
11620 int argc;
11621 char *argv[NUM_ARGV];
11622 char **argv2 = argv;
11623 guint id;
11625 if (x_initialized++ > 1)
11627 xg_display_open (SSDATA (display_name), &dpy);
11629 else
11631 static char display_opt[] = "--display";
11632 static char name_opt[] = "--name";
11634 for (argc = 0; argc < NUM_ARGV; ++argc)
11635 argv[argc] = 0;
11637 argc = 0;
11638 argv[argc++] = initial_argv[0];
11640 if (! NILP (display_name))
11642 argv[argc++] = display_opt;
11643 argv[argc++] = SSDATA (display_name);
11646 argv[argc++] = name_opt;
11647 argv[argc++] = resource_name;
11649 XSetLocaleModifiers ("");
11651 /* Work around GLib bug that outputs a faulty warning. See
11652 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
11653 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
11654 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
11656 /* NULL window -> events for all windows go to our function.
11657 Call before gtk_init so Gtk+ event filters comes after our. */
11658 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
11660 /* gtk_init does set_locale. Fix locale before and after. */
11661 fixup_locale ();
11662 unrequest_sigio (); /* See comment in x_display_ok. */
11663 gtk_init (&argc, &argv2);
11664 request_sigio ();
11665 fixup_locale ();
11667 g_log_remove_handler ("GLib", id);
11669 xg_initialize ();
11671 dpy = DEFAULT_GDK_DISPLAY ();
11673 #if ! GTK_CHECK_VERSION (2, 90, 0)
11674 /* Load our own gtkrc if it exists. */
11676 const char *file = "~/.emacs.d/gtkrc";
11677 Lisp_Object s, abs_file;
11679 s = build_string (file);
11680 abs_file = Fexpand_file_name (s, Qnil);
11682 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
11683 gtk_rc_parse (SSDATA (abs_file));
11685 #endif
11687 XSetErrorHandler (x_error_handler);
11688 XSetIOErrorHandler (x_io_error_quitter);
11691 #else /* not USE_GTK */
11692 #ifdef USE_X_TOOLKIT
11693 /* weiner@footloose.sps.mot.com reports that this causes
11694 errors with X11R5:
11695 X protocol error: BadAtom (invalid Atom parameter)
11696 on protocol request 18skiloaf.
11697 So let's not use it until R6. */
11698 #ifdef HAVE_X11XTR6
11699 XtSetLanguageProc (NULL, NULL, NULL);
11700 #endif
11703 int argc = 0;
11704 char *argv[3];
11706 argv[0] = "";
11707 argc = 1;
11708 if (xrm_option)
11710 argv[argc++] = "-xrm";
11711 argv[argc++] = xrm_option;
11713 turn_on_atimers (false);
11714 unrequest_sigio (); /* See comment in x_display_ok. */
11715 dpy = XtOpenDisplay (Xt_app_con, SSDATA (display_name),
11716 resource_name, EMACS_CLASS,
11717 emacs_options, XtNumber (emacs_options),
11718 &argc, argv);
11719 request_sigio ();
11720 turn_on_atimers (true);
11722 #ifdef HAVE_X11XTR6
11723 /* I think this is to compensate for XtSetLanguageProc. */
11724 fixup_locale ();
11725 #endif
11728 #else /* not USE_X_TOOLKIT */
11729 XSetLocaleModifiers ("");
11730 unrequest_sigio (); // See comment in x_display_ok.
11731 dpy = XOpenDisplay (SSDATA (display_name));
11732 request_sigio ();
11733 #endif /* not USE_X_TOOLKIT */
11734 #endif /* not USE_GTK*/
11736 /* Detect failure. */
11737 if (dpy == 0)
11739 unblock_input ();
11740 return 0;
11743 /* We have definitely succeeded. Record the new connection. */
11745 dpyinfo = xzalloc (sizeof *dpyinfo);
11746 terminal = x_create_terminal (dpyinfo);
11749 struct x_display_info *share;
11751 for (share = x_display_list; share; share = share->next)
11752 if (same_x_server (SSDATA (XCAR (share->name_list_element)),
11753 SSDATA (display_name)))
11754 break;
11755 if (share)
11756 terminal->kboard = share->terminal->kboard;
11757 else
11759 terminal->kboard = allocate_kboard (Qx);
11761 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
11763 char *vendor = ServerVendor (dpy);
11765 /* Temporarily hide the partially initialized terminal. */
11766 terminal_list = terminal->next_terminal;
11767 unblock_input ();
11768 kset_system_key_alist
11769 (terminal->kboard,
11770 call1 (Qvendor_specific_keysyms,
11771 vendor ? build_string (vendor) : empty_unibyte_string));
11772 block_input ();
11773 terminal->next_terminal = terminal_list;
11774 terminal_list = terminal;
11777 /* Don't let the initial kboard remain current longer than necessary.
11778 That would cause problems if a file loaded on startup tries to
11779 prompt in the mini-buffer. */
11780 if (current_kboard == initial_kboard)
11781 current_kboard = terminal->kboard;
11783 terminal->kboard->reference_count++;
11786 /* Put this display on the chain. */
11787 dpyinfo->next = x_display_list;
11788 x_display_list = dpyinfo;
11790 dpyinfo->name_list_element = Fcons (display_name, Qnil);
11791 dpyinfo->display = dpy;
11792 dpyinfo->connection = ConnectionNumber (dpyinfo->display);
11794 /* Set the name of the terminal. */
11795 terminal->name = xlispstrdup (display_name);
11797 #if false
11798 XSetAfterFunction (x_current_display, x_trace_wire);
11799 #endif
11801 lim = min (PTRDIFF_MAX, SIZE_MAX) - sizeof "@";
11802 Lisp_Object system_name = Fsystem_name ();
11803 if (lim - SBYTES (Vinvocation_name) < SBYTES (system_name))
11804 memory_full (SIZE_MAX);
11805 dpyinfo->x_id = ++x_display_id;
11806 dpyinfo->x_id_name = xmalloc (SBYTES (Vinvocation_name)
11807 + SBYTES (system_name) + 2);
11808 char *nametail = lispstpcpy (dpyinfo->x_id_name, Vinvocation_name);
11809 *nametail++ = '@';
11810 lispstpcpy (nametail, system_name);
11812 /* Figure out which modifier bits mean what. */
11813 x_find_modifier_meanings (dpyinfo);
11815 /* Get the scroll bar cursor. */
11816 #ifdef USE_GTK
11817 /* We must create a GTK cursor, it is required for GTK widgets. */
11818 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
11819 #endif /* USE_GTK */
11821 dpyinfo->vertical_scroll_bar_cursor
11822 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
11824 dpyinfo->horizontal_scroll_bar_cursor
11825 = XCreateFontCursor (dpyinfo->display, XC_sb_h_double_arrow);
11827 xrdb = x_load_resources (dpyinfo->display, xrm_option,
11828 resource_name, EMACS_CLASS);
11829 #ifdef HAVE_XRMSETDATABASE
11830 XrmSetDatabase (dpyinfo->display, xrdb);
11831 #else
11832 dpyinfo->display->db = xrdb;
11833 #endif
11834 /* Put the rdb where we can find it in a way that works on
11835 all versions. */
11836 dpyinfo->xrdb = xrdb;
11838 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
11839 DefaultScreen (dpyinfo->display));
11840 select_visual (dpyinfo);
11841 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
11842 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
11843 dpyinfo->icon_bitmap_id = -1;
11844 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
11846 reset_mouse_highlight (&dpyinfo->mouse_highlight);
11848 /* See if we can construct pixel values from RGB values. */
11849 if (dpyinfo->visual->class == TrueColor)
11851 get_bits_and_offset (dpyinfo->visual->red_mask,
11852 &dpyinfo->red_bits, &dpyinfo->red_offset);
11853 get_bits_and_offset (dpyinfo->visual->blue_mask,
11854 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
11855 get_bits_and_offset (dpyinfo->visual->green_mask,
11856 &dpyinfo->green_bits, &dpyinfo->green_offset);
11859 /* See if a private colormap is requested. */
11860 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
11862 if (dpyinfo->visual->class == PseudoColor)
11864 AUTO_STRING (privateColormap, "privateColormap");
11865 AUTO_STRING (PrivateColormap, "PrivateColormap");
11866 Lisp_Object value
11867 = display_x_get_resource (dpyinfo, privateColormap,
11868 PrivateColormap, Qnil, Qnil);
11869 if (STRINGP (value)
11870 && (!strcmp (SSDATA (value), "true")
11871 || !strcmp (SSDATA (value), "on")))
11872 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
11875 else
11876 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
11877 dpyinfo->visual, AllocNone);
11879 #ifdef HAVE_XFT
11881 /* If we are using Xft, the following precautions should be made:
11883 1. Make sure that the Xrender extension is added before the Xft one.
11884 Otherwise, the close-display hook set by Xft is called after the one
11885 for Xrender, and the former tries to re-add the latter. This results
11886 in inconsistency of internal states and leads to X protocol error when
11887 one reconnects to the same X server (Bug#1696).
11889 2. Check dpi value in X resources. It is better we use it as well,
11890 since Xft will use it, as will all Gnome applications. If our real DPI
11891 is smaller or larger than the one Xft uses, our font will look smaller
11892 or larger than other for other applications, even if it is the same
11893 font name (monospace-10 for example). */
11895 int event_base, error_base;
11896 char *v;
11897 double d;
11899 XRenderQueryExtension (dpyinfo->display, &event_base, &error_base);
11901 v = XGetDefault (dpyinfo->display, "Xft", "dpi");
11902 if (v != NULL && sscanf (v, "%lf", &d) == 1)
11903 dpyinfo->resy = dpyinfo->resx = d;
11905 #endif
11907 if (dpyinfo->resy < 1)
11909 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
11910 double pixels = DisplayHeight (dpyinfo->display, screen_number);
11911 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
11912 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
11913 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
11914 pixels = DisplayWidth (dpyinfo->display, screen_number);
11915 mm = DisplayWidthMM (dpyinfo->display, screen_number);
11916 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
11917 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
11921 static const struct
11923 const char *name;
11924 int offset;
11925 } atom_refs[] = {
11926 #define ATOM_REFS_INIT(string, member) \
11927 { string, offsetof (struct x_display_info, member) },
11928 ATOM_REFS_INIT ("WM_PROTOCOLS", Xatom_wm_protocols)
11929 ATOM_REFS_INIT ("WM_TAKE_FOCUS", Xatom_wm_take_focus)
11930 ATOM_REFS_INIT ("WM_SAVE_YOURSELF", Xatom_wm_save_yourself)
11931 ATOM_REFS_INIT ("WM_DELETE_WINDOW", Xatom_wm_delete_window)
11932 ATOM_REFS_INIT ("WM_CHANGE_STATE", Xatom_wm_change_state)
11933 ATOM_REFS_INIT ("WM_CONFIGURE_DENIED", Xatom_wm_configure_denied)
11934 ATOM_REFS_INIT ("WM_MOVED", Xatom_wm_window_moved)
11935 ATOM_REFS_INIT ("WM_CLIENT_LEADER", Xatom_wm_client_leader)
11936 ATOM_REFS_INIT ("Editres", Xatom_editres)
11937 ATOM_REFS_INIT ("CLIPBOARD", Xatom_CLIPBOARD)
11938 ATOM_REFS_INIT ("TIMESTAMP", Xatom_TIMESTAMP)
11939 ATOM_REFS_INIT ("TEXT", Xatom_TEXT)
11940 ATOM_REFS_INIT ("COMPOUND_TEXT", Xatom_COMPOUND_TEXT)
11941 ATOM_REFS_INIT ("UTF8_STRING", Xatom_UTF8_STRING)
11942 ATOM_REFS_INIT ("DELETE", Xatom_DELETE)
11943 ATOM_REFS_INIT ("MULTIPLE", Xatom_MULTIPLE)
11944 ATOM_REFS_INIT ("INCR", Xatom_INCR)
11945 ATOM_REFS_INIT ("_EMACS_TMP_", Xatom_EMACS_TMP)
11946 ATOM_REFS_INIT ("TARGETS", Xatom_TARGETS)
11947 ATOM_REFS_INIT ("NULL", Xatom_NULL)
11948 ATOM_REFS_INIT ("ATOM", Xatom_ATOM)
11949 ATOM_REFS_INIT ("ATOM_PAIR", Xatom_ATOM_PAIR)
11950 ATOM_REFS_INIT ("CLIPBOARD_MANAGER", Xatom_CLIPBOARD_MANAGER)
11951 ATOM_REFS_INIT ("_XEMBED_INFO", Xatom_XEMBED_INFO)
11952 /* For properties of font. */
11953 ATOM_REFS_INIT ("PIXEL_SIZE", Xatom_PIXEL_SIZE)
11954 ATOM_REFS_INIT ("AVERAGE_WIDTH", Xatom_AVERAGE_WIDTH)
11955 ATOM_REFS_INIT ("_MULE_BASELINE_OFFSET", Xatom_MULE_BASELINE_OFFSET)
11956 ATOM_REFS_INIT ("_MULE_RELATIVE_COMPOSE", Xatom_MULE_RELATIVE_COMPOSE)
11957 ATOM_REFS_INIT ("_MULE_DEFAULT_ASCENT", Xatom_MULE_DEFAULT_ASCENT)
11958 /* Ghostscript support. */
11959 ATOM_REFS_INIT ("DONE", Xatom_DONE)
11960 ATOM_REFS_INIT ("PAGE", Xatom_PAGE)
11961 ATOM_REFS_INIT ("SCROLLBAR", Xatom_Scrollbar)
11962 ATOM_REFS_INIT ("HORIZONTAL_SCROLLBAR", Xatom_Horizontal_Scrollbar)
11963 ATOM_REFS_INIT ("_XEMBED", Xatom_XEMBED)
11964 /* EWMH */
11965 ATOM_REFS_INIT ("_NET_WM_STATE", Xatom_net_wm_state)
11966 ATOM_REFS_INIT ("_NET_WM_STATE_FULLSCREEN", Xatom_net_wm_state_fullscreen)
11967 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_HORZ",
11968 Xatom_net_wm_state_maximized_horz)
11969 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_VERT",
11970 Xatom_net_wm_state_maximized_vert)
11971 ATOM_REFS_INIT ("_NET_WM_STATE_STICKY", Xatom_net_wm_state_sticky)
11972 ATOM_REFS_INIT ("_NET_WM_STATE_HIDDEN", Xatom_net_wm_state_hidden)
11973 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE", Xatom_net_window_type)
11974 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE_TOOLTIP",
11975 Xatom_net_window_type_tooltip)
11976 ATOM_REFS_INIT ("_NET_WM_ICON_NAME", Xatom_net_wm_icon_name)
11977 ATOM_REFS_INIT ("_NET_WM_NAME", Xatom_net_wm_name)
11978 ATOM_REFS_INIT ("_NET_SUPPORTED", Xatom_net_supported)
11979 ATOM_REFS_INIT ("_NET_SUPPORTING_WM_CHECK", Xatom_net_supporting_wm_check)
11980 ATOM_REFS_INIT ("_NET_WM_WINDOW_OPACITY", Xatom_net_wm_window_opacity)
11981 ATOM_REFS_INIT ("_NET_ACTIVE_WINDOW", Xatom_net_active_window)
11982 ATOM_REFS_INIT ("_NET_FRAME_EXTENTS", Xatom_net_frame_extents)
11983 ATOM_REFS_INIT ("_NET_CURRENT_DESKTOP", Xatom_net_current_desktop)
11984 ATOM_REFS_INIT ("_NET_WORKAREA", Xatom_net_workarea)
11985 /* Session management */
11986 ATOM_REFS_INIT ("SM_CLIENT_ID", Xatom_SM_CLIENT_ID)
11987 ATOM_REFS_INIT ("_XSETTINGS_SETTINGS", Xatom_xsettings_prop)
11988 ATOM_REFS_INIT ("MANAGER", Xatom_xsettings_mgr)
11991 int i;
11992 enum { atom_count = ARRAYELTS (atom_refs) };
11993 /* 1 for _XSETTINGS_SN. */
11994 enum { total_atom_count = 1 + atom_count };
11995 Atom atoms_return[total_atom_count];
11996 char *atom_names[total_atom_count];
11997 static char const xsettings_fmt[] = "_XSETTINGS_S%d";
11998 char xsettings_atom_name[sizeof xsettings_fmt - 2
11999 + INT_STRLEN_BOUND (int)];
12001 for (i = 0; i < atom_count; i++)
12002 atom_names[i] = (char *) atom_refs[i].name;
12004 /* Build _XSETTINGS_SN atom name. */
12005 sprintf (xsettings_atom_name, xsettings_fmt,
12006 XScreenNumberOfScreen (dpyinfo->screen));
12007 atom_names[i] = xsettings_atom_name;
12009 XInternAtoms (dpyinfo->display, atom_names, total_atom_count,
12010 False, atoms_return);
12012 for (i = 0; i < atom_count; i++)
12013 *(Atom *) ((char *) dpyinfo + atom_refs[i].offset) = atoms_return[i];
12015 /* Manually copy last atom. */
12016 dpyinfo->Xatom_xsettings_sel = atoms_return[i];
12019 dpyinfo->x_dnd_atoms_size = 8;
12020 dpyinfo->x_dnd_atoms = xmalloc (sizeof *dpyinfo->x_dnd_atoms
12021 * dpyinfo->x_dnd_atoms_size);
12022 dpyinfo->gray
12023 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12024 gray_bits, gray_width, gray_height,
12025 1, 0, 1);
12027 x_setup_pointer_blanking (dpyinfo);
12029 #ifdef HAVE_X_I18N
12030 xim_initialize (dpyinfo, resource_name);
12031 #endif
12033 xsettings_initialize (dpyinfo);
12035 /* This is only needed for distinguishing keyboard and process input. */
12036 if (dpyinfo->connection != 0)
12037 add_keyboard_wait_descriptor (dpyinfo->connection);
12039 #ifdef F_SETOWN
12040 fcntl (dpyinfo->connection, F_SETOWN, getpid ());
12041 #endif /* ! defined (F_SETOWN) */
12043 if (interrupt_input)
12044 init_sigio (dpyinfo->connection);
12046 #ifdef USE_LUCID
12048 XrmValue d, fr, to;
12049 Font font;
12051 dpy = dpyinfo->display;
12052 d.addr = (XPointer)&dpy;
12053 d.size = sizeof (Display *);
12054 fr.addr = XtDefaultFont;
12055 fr.size = sizeof (XtDefaultFont);
12056 to.size = sizeof (Font *);
12057 to.addr = (XPointer)&font;
12058 x_catch_errors (dpy);
12059 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
12060 emacs_abort ();
12061 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
12062 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
12063 /* Do not free XFontStruct returned by the above call to XQueryFont.
12064 This leads to X protocol errors at XtCloseDisplay (Bug#18403). */
12065 x_uncatch_errors ();
12067 #endif
12069 /* See if we should run in synchronous mode. This is useful
12070 for debugging X code. */
12072 AUTO_STRING (synchronous, "synchronous");
12073 AUTO_STRING (Synchronous, "Synchronous");
12074 Lisp_Object value = display_x_get_resource (dpyinfo, synchronous,
12075 Synchronous, Qnil, Qnil);
12076 if (STRINGP (value)
12077 && (!strcmp (SSDATA (value), "true")
12078 || !strcmp (SSDATA (value), "on")))
12079 XSynchronize (dpyinfo->display, True);
12083 AUTO_STRING (useXIM, "useXIM");
12084 AUTO_STRING (UseXIM, "UseXIM");
12085 Lisp_Object value = display_x_get_resource (dpyinfo, useXIM, UseXIM,
12086 Qnil, Qnil);
12087 #ifdef USE_XIM
12088 if (STRINGP (value)
12089 && (!strcmp (SSDATA (value), "false")
12090 || !strcmp (SSDATA (value), "off")))
12091 use_xim = false;
12092 #else
12093 if (STRINGP (value)
12094 && (!strcmp (SSDATA (value), "true")
12095 || !strcmp (SSDATA (value), "on")))
12096 use_xim = true;
12097 #endif
12100 #ifdef HAVE_X_SM
12101 /* Only do this for the very first display in the Emacs session.
12102 Ignore X session management when Emacs was first started on a
12103 tty or started as a daemon. */
12104 if (terminal->id == 1 && ! IS_DAEMON)
12105 x_session_initialize (dpyinfo);
12106 #endif
12108 #ifdef USE_CAIRO
12109 x_extension_initialize (dpyinfo);
12110 #endif
12112 unblock_input ();
12114 return dpyinfo;
12117 /* Get rid of display DPYINFO, deleting all frames on it,
12118 and without sending any more commands to the X server. */
12120 static void
12121 x_delete_display (struct x_display_info *dpyinfo)
12123 struct terminal *t;
12125 /* Close all frames and delete the generic struct terminal for this
12126 X display. */
12127 for (t = terminal_list; t; t = t->next_terminal)
12128 if (t->type == output_x_window && t->display_info.x == dpyinfo)
12130 #ifdef HAVE_X_SM
12131 /* Close X session management when we close its display. */
12132 if (t->id == 1 && x_session_have_connection ())
12133 x_session_close ();
12134 #endif
12135 delete_terminal (t);
12136 break;
12139 if (next_noop_dpyinfo == dpyinfo)
12140 next_noop_dpyinfo = dpyinfo->next;
12142 if (x_display_list == dpyinfo)
12143 x_display_list = dpyinfo->next;
12144 else
12146 struct x_display_info *tail;
12148 for (tail = x_display_list; tail; tail = tail->next)
12149 if (tail->next == dpyinfo)
12150 tail->next = tail->next->next;
12153 xfree (dpyinfo->x_id_name);
12154 xfree (dpyinfo->x_dnd_atoms);
12155 xfree (dpyinfo->color_cells);
12156 xfree (dpyinfo);
12159 #ifdef USE_X_TOOLKIT
12161 /* Atimer callback function for TIMER. Called every 0.1s to process
12162 Xt timeouts, if needed. We must avoid calling XtAppPending as
12163 much as possible because that function does an implicit XFlush
12164 that slows us down. */
12166 static void
12167 x_process_timeouts (struct atimer *timer)
12169 block_input ();
12170 x_timeout_atimer_activated_flag = false;
12171 if (toolkit_scroll_bar_interaction || popup_activated ())
12173 while (XtAppPending (Xt_app_con) & XtIMTimer)
12174 XtAppProcessEvent (Xt_app_con, XtIMTimer);
12175 /* Reactivate the atimer for next time. */
12176 x_activate_timeout_atimer ();
12178 unblock_input ();
12181 /* Install an asynchronous timer that processes Xt timeout events
12182 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
12183 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
12184 function whenever these variables are set. This is necessary
12185 because some widget sets use timeouts internally, for example the
12186 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
12187 processed, these widgets don't behave normally. */
12189 void
12190 x_activate_timeout_atimer (void)
12192 block_input ();
12193 if (!x_timeout_atimer_activated_flag)
12195 struct timespec interval = make_timespec (0, 100 * 1000 * 1000);
12196 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
12197 x_timeout_atimer_activated_flag = true;
12199 unblock_input ();
12202 #endif /* USE_X_TOOLKIT */
12205 /* Set up use of X before we make the first connection. */
12207 static struct redisplay_interface x_redisplay_interface =
12209 x_frame_parm_handlers,
12210 x_produce_glyphs,
12211 x_write_glyphs,
12212 x_insert_glyphs,
12213 x_clear_end_of_line,
12214 x_scroll_run,
12215 x_after_update_window_line,
12216 x_update_window_begin,
12217 x_update_window_end,
12218 x_flush,
12219 x_clear_window_mouse_face,
12220 x_get_glyph_overhangs,
12221 x_fix_overlapping_area,
12222 x_draw_fringe_bitmap,
12223 #ifdef USE_CAIRO
12224 x_cr_define_fringe_bitmap,
12225 x_cr_destroy_fringe_bitmap,
12226 #else
12227 0, /* define_fringe_bitmap */
12228 0, /* destroy_fringe_bitmap */
12229 #endif
12230 x_compute_glyph_string_overhangs,
12231 x_draw_glyph_string,
12232 x_define_frame_cursor,
12233 x_clear_frame_area,
12234 x_draw_window_cursor,
12235 x_draw_vertical_window_border,
12236 x_draw_window_divider,
12237 x_shift_glyphs_for_insert, /* Never called; see comment in function. */
12238 x_show_hourglass,
12239 x_hide_hourglass
12243 /* This function is called when the last frame on a display is deleted. */
12244 void
12245 x_delete_terminal (struct terminal *terminal)
12247 struct x_display_info *dpyinfo = terminal->display_info.x;
12249 /* Protect against recursive calls. delete_frame in
12250 delete_terminal calls us back when it deletes our last frame. */
12251 if (!terminal->name)
12252 return;
12254 block_input ();
12255 #ifdef HAVE_X_I18N
12256 /* We must close our connection to the XIM server before closing the
12257 X display. */
12258 if (dpyinfo->xim)
12259 xim_close_dpy (dpyinfo);
12260 #endif
12262 /* Normally, the display is available... */
12263 if (dpyinfo->display)
12265 x_destroy_all_bitmaps (dpyinfo);
12266 XSetCloseDownMode (dpyinfo->display, DestroyAll);
12268 /* Whether or not XCloseDisplay destroys the associated resource
12269 database depends on the version of libX11. To avoid both
12270 crash and memory leak, we dissociate the database from the
12271 display and then destroy dpyinfo->xrdb ourselves.
12273 Unfortunately, the above strategy does not work in some
12274 situations due to a bug in newer versions of libX11: because
12275 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
12276 dpy->db is NULL, XCloseDisplay destroys the associated
12277 database whereas it has not been created by XGetDefault
12278 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
12279 don't destroy the database here in order to avoid the crash
12280 in the above situations for now, though that may cause memory
12281 leaks in other situations. */
12282 #if false
12283 #ifdef HAVE_XRMSETDATABASE
12284 XrmSetDatabase (dpyinfo->display, NULL);
12285 #else
12286 dpyinfo->display->db = NULL;
12287 #endif
12288 /* We used to call XrmDestroyDatabase from x_delete_display, but
12289 some older versions of libX11 crash if we call it after
12290 closing all the displays. */
12291 XrmDestroyDatabase (dpyinfo->xrdb);
12292 #endif
12294 #ifdef USE_GTK
12295 xg_display_close (dpyinfo->display);
12296 #else
12297 #ifdef USE_X_TOOLKIT
12298 XtCloseDisplay (dpyinfo->display);
12299 #else
12300 XCloseDisplay (dpyinfo->display);
12301 #endif
12302 #endif /* ! USE_GTK */
12303 /* Do not close the connection here because it's already closed
12304 by X(t)CloseDisplay (Bug#18403). */
12305 dpyinfo->display = NULL;
12308 /* ...but if called from x_connection_closed, the display may already
12309 be closed and dpyinfo->display was set to 0 to indicate that. Since
12310 X server is most likely gone, explicit close is the only reliable
12311 way to continue and avoid Bug#19147. */
12312 else if (dpyinfo->connection >= 0)
12313 emacs_close (dpyinfo->connection);
12315 /* No more input on this descriptor. */
12316 delete_keyboard_wait_descriptor (dpyinfo->connection);
12317 /* Mark as dead. */
12318 dpyinfo->connection = -1;
12320 x_delete_display (dpyinfo);
12321 unblock_input ();
12324 /* Create a struct terminal, initialize it with the X11 specific
12325 functions and make DISPLAY->TERMINAL point to it. */
12327 static struct terminal *
12328 x_create_terminal (struct x_display_info *dpyinfo)
12330 struct terminal *terminal;
12332 terminal = create_terminal (output_x_window, &x_redisplay_interface);
12334 terminal->display_info.x = dpyinfo;
12335 dpyinfo->terminal = terminal;
12337 /* kboard is initialized in x_term_init. */
12339 terminal->clear_frame_hook = x_clear_frame;
12340 terminal->ins_del_lines_hook = x_ins_del_lines;
12341 terminal->delete_glyphs_hook = x_delete_glyphs;
12342 terminal->ring_bell_hook = XTring_bell;
12343 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
12344 terminal->update_begin_hook = x_update_begin;
12345 terminal->update_end_hook = x_update_end;
12346 terminal->read_socket_hook = XTread_socket;
12347 terminal->frame_up_to_date_hook = XTframe_up_to_date;
12348 terminal->mouse_position_hook = XTmouse_position;
12349 terminal->frame_rehighlight_hook = XTframe_rehighlight;
12350 terminal->frame_raise_lower_hook = XTframe_raise_lower;
12351 terminal->fullscreen_hook = XTfullscreen_hook;
12352 terminal->menu_show_hook = x_menu_show;
12353 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
12354 terminal->popup_dialog_hook = xw_popup_dialog;
12355 #endif
12356 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
12357 terminal->set_horizontal_scroll_bar_hook = XTset_horizontal_scroll_bar;
12358 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
12359 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
12360 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
12361 terminal->delete_frame_hook = x_destroy_window;
12362 terminal->delete_terminal_hook = x_delete_terminal;
12363 /* Other hooks are NULL by default. */
12365 return terminal;
12368 static void
12369 x_initialize (void)
12371 baud_rate = 19200;
12373 x_noop_count = 0;
12374 any_help_event_p = false;
12375 ignore_next_mouse_click_timeout = 0;
12377 #ifdef USE_GTK
12378 current_count = -1;
12379 #endif
12381 /* Try to use interrupt input; if we can't, then start polling. */
12382 Fset_input_interrupt_mode (Qt);
12384 #ifdef USE_X_TOOLKIT
12385 XtToolkitInitialize ();
12387 Xt_app_con = XtCreateApplicationContext ();
12389 /* Register a converter from strings to pixels, which uses
12390 Emacs' color allocation infrastructure. */
12391 XtAppSetTypeConverter (Xt_app_con,
12392 XtRString, XtRPixel, cvt_string_to_pixel,
12393 cvt_string_to_pixel_args,
12394 XtNumber (cvt_string_to_pixel_args),
12395 XtCacheByDisplay, cvt_pixel_dtor);
12397 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
12398 #endif
12400 #ifdef USE_TOOLKIT_SCROLL_BARS
12401 #ifndef USE_GTK
12402 xaw3d_arrow_scroll = False;
12403 xaw3d_pick_top = True;
12404 #endif
12405 #endif
12407 #ifdef USE_CAIRO
12408 x_cr_init_fringe (&x_redisplay_interface);
12409 #endif
12411 /* Note that there is no real way portable across R3/R4 to get the
12412 original error handler. */
12413 XSetErrorHandler (x_error_handler);
12414 XSetIOErrorHandler (x_io_error_quitter);
12417 #ifdef USE_GTK
12418 void
12419 init_xterm (void)
12421 /* Emacs can handle only core input events, so make sure
12422 Gtk doesn't use Xinput or Xinput2 extensions. */
12423 xputenv ("GDK_CORE_DEVICE_EVENTS=1");
12425 #endif
12427 void
12428 syms_of_xterm (void)
12430 x_error_message = NULL;
12432 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
12433 DEFSYM (Qlatin_1, "latin-1");
12435 #ifdef USE_GTK
12436 xg_default_icon_file = build_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
12437 staticpro (&xg_default_icon_file);
12439 DEFSYM (Qx_gtk_map_stock, "x-gtk-map-stock");
12440 #endif
12442 DEFVAR_BOOL ("x-use-underline-position-properties",
12443 x_use_underline_position_properties,
12444 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
12445 A value of nil means ignore them. If you encounter fonts with bogus
12446 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
12447 to 4.1, set this to nil. You can also use `underline-minimum-offset'
12448 to override the font's UNDERLINE_POSITION for small font display
12449 sizes. */);
12450 x_use_underline_position_properties = true;
12452 DEFVAR_BOOL ("x-underline-at-descent-line",
12453 x_underline_at_descent_line,
12454 doc: /* Non-nil means to draw the underline at the same place as the descent line.
12455 A value of nil means to draw the underline according to the value of the
12456 variable `x-use-underline-position-properties', which is usually at the
12457 baseline level. The default value is nil. */);
12458 x_underline_at_descent_line = false;
12460 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
12461 x_mouse_click_focus_ignore_position,
12462 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
12463 This variable is only used when the window manager requires that you
12464 click on a frame to select it (give it focus). In that case, a value
12465 of nil, means that the selected window and cursor position changes to
12466 reflect the mouse click position, while a non-nil value means that the
12467 selected window or cursor position is preserved. */);
12468 x_mouse_click_focus_ignore_position = false;
12470 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
12471 doc: /* Which toolkit scroll bars Emacs uses, if any.
12472 A value of nil means Emacs doesn't use toolkit scroll bars.
12473 With the X Window system, the value is a symbol describing the
12474 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
12475 With MS Windows or Nextstep, the value is t. */);
12476 #ifdef USE_TOOLKIT_SCROLL_BARS
12477 #ifdef USE_MOTIF
12478 Vx_toolkit_scroll_bars = intern_c_string ("motif");
12479 #elif defined HAVE_XAW3D
12480 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
12481 #elif USE_GTK
12482 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
12483 #else
12484 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
12485 #endif
12486 #else
12487 Vx_toolkit_scroll_bars = Qnil;
12488 #endif
12490 DEFSYM (Qmodifier_value, "modifier-value");
12491 DEFSYM (Qalt, "alt");
12492 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
12493 DEFSYM (Qhyper, "hyper");
12494 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
12495 DEFSYM (Qmeta, "meta");
12496 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
12497 DEFSYM (Qsuper, "super");
12498 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
12500 DEFVAR_LISP ("x-alt-keysym", Vx_alt_keysym,
12501 doc: /* Which keys Emacs uses for the alt modifier.
12502 This should be one of the symbols `alt', `hyper', `meta', `super'.
12503 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
12504 is nil, which is the same as `alt'. */);
12505 Vx_alt_keysym = Qnil;
12507 DEFVAR_LISP ("x-hyper-keysym", Vx_hyper_keysym,
12508 doc: /* Which keys Emacs uses for the hyper modifier.
12509 This should be one of the symbols `alt', `hyper', `meta', `super'.
12510 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
12511 default is nil, which is the same as `hyper'. */);
12512 Vx_hyper_keysym = Qnil;
12514 DEFVAR_LISP ("x-meta-keysym", Vx_meta_keysym,
12515 doc: /* Which keys Emacs uses for the meta modifier.
12516 This should be one of the symbols `alt', `hyper', `meta', `super'.
12517 For example, `meta' means use the Meta_L and Meta_R keysyms. The
12518 default is nil, which is the same as `meta'. */);
12519 Vx_meta_keysym = Qnil;
12521 DEFVAR_LISP ("x-super-keysym", Vx_super_keysym,
12522 doc: /* Which keys Emacs uses for the super modifier.
12523 This should be one of the symbols `alt', `hyper', `meta', `super'.
12524 For example, `super' means use the Super_L and Super_R keysyms. The
12525 default is nil, which is the same as `super'. */);
12526 Vx_super_keysym = Qnil;
12528 DEFVAR_LISP ("x-keysym-table", Vx_keysym_table,
12529 doc: /* Hash table of character codes indexed by X keysym codes. */);
12530 Vx_keysym_table = make_hash_table (hashtest_eql, make_number (900),
12531 make_float (DEFAULT_REHASH_SIZE),
12532 make_float (DEFAULT_REHASH_THRESHOLD),
12533 Qnil);
12535 DEFVAR_BOOL ("x-frame-normalize-before-maximize",
12536 x_frame_normalize_before_maximize,
12537 doc: /* Non-nil means normalize frame before maximizing.
12538 If this variable is t, Emacs first asks the window manager to give the
12539 frame its normal size, and only then the final state, whenever changing
12540 from a full-height, full-width or full-both state to the maximized one
12541 or when changing from the maximized to the full-height or full-width
12542 state.
12544 Set this variable only if your window manager cannot handle the
12545 transition between the various maximization states. */);
12546 x_frame_normalize_before_maximize = false;