Pacify --enable-gcc-warnings
[emacs.git] / src / xterm.c
blobc793a5111f7210608f4e0a9acf3f42e878ebc493
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 (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 || s->font_not_found_p
1728 || s->extends_to_end_of_line_p
1729 || force_p)
1731 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1732 s->background_width,
1733 s->height - 2 * box_line_width);
1734 s->background_filled_p = true;
1740 /* Draw the foreground of glyph string S. */
1742 static void
1743 x_draw_glyph_string_foreground (struct glyph_string *s)
1745 int i, x;
1747 /* If first glyph of S has a left box line, start drawing the text
1748 of S to the right of that box line. */
1749 if (s->face->box != FACE_NO_BOX
1750 && s->first_glyph->left_box_line_p)
1751 x = s->x + eabs (s->face->box_line_width);
1752 else
1753 x = s->x;
1755 /* Draw characters of S as rectangles if S's font could not be
1756 loaded. */
1757 if (s->font_not_found_p)
1759 for (i = 0; i < s->nchars; ++i)
1761 struct glyph *g = s->first_glyph + i;
1762 x_draw_rectangle (s->f,
1763 s->gc, x, s->y, g->pixel_width - 1,
1764 s->height - 1);
1765 x += g->pixel_width;
1768 else
1770 struct font *font = s->font;
1771 int boff = font->baseline_offset;
1772 int y;
1774 if (font->vertical_centering)
1775 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1777 y = s->ybase - boff;
1778 if (s->for_overlaps
1779 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1780 font->driver->draw (s, 0, s->nchars, x, y, false);
1781 else
1782 font->driver->draw (s, 0, s->nchars, x, y, true);
1783 if (s->face->overstrike)
1784 font->driver->draw (s, 0, s->nchars, x + 1, y, false);
1788 /* Draw the foreground of composite glyph string S. */
1790 static void
1791 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1793 int i, j, x;
1794 struct font *font = s->font;
1796 /* If first glyph of S has a left box line, start drawing the text
1797 of S to the right of that box line. */
1798 if (s->face && s->face->box != FACE_NO_BOX
1799 && s->first_glyph->left_box_line_p)
1800 x = s->x + eabs (s->face->box_line_width);
1801 else
1802 x = s->x;
1804 /* S is a glyph string for a composition. S->cmp_from is the index
1805 of the first character drawn for glyphs of this composition.
1806 S->cmp_from == 0 means we are drawing the very first character of
1807 this composition. */
1809 /* Draw a rectangle for the composition if the font for the very
1810 first character of the composition could not be loaded. */
1811 if (s->font_not_found_p)
1813 if (s->cmp_from == 0)
1814 x_draw_rectangle (s->f, s->gc, x, s->y,
1815 s->width - 1, s->height - 1);
1817 else if (! s->first_glyph->u.cmp.automatic)
1819 int y = s->ybase;
1821 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1822 /* TAB in a composition means display glyphs with padding
1823 space on the left or right. */
1824 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1826 int xx = x + s->cmp->offsets[j * 2];
1827 int yy = y - s->cmp->offsets[j * 2 + 1];
1829 font->driver->draw (s, j, j + 1, xx, yy, false);
1830 if (s->face->overstrike)
1831 font->driver->draw (s, j, j + 1, xx + 1, yy, false);
1834 else
1836 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1837 Lisp_Object glyph;
1838 int y = s->ybase;
1839 int width = 0;
1841 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1843 glyph = LGSTRING_GLYPH (gstring, i);
1844 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1845 width += LGLYPH_WIDTH (glyph);
1846 else
1848 int xoff, yoff, wadjust;
1850 if (j < i)
1852 font->driver->draw (s, j, i, x, y, false);
1853 if (s->face->overstrike)
1854 font->driver->draw (s, j, i, x + 1, y, false);
1855 x += width;
1857 xoff = LGLYPH_XOFF (glyph);
1858 yoff = LGLYPH_YOFF (glyph);
1859 wadjust = LGLYPH_WADJUST (glyph);
1860 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, false);
1861 if (s->face->overstrike)
1862 font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff,
1863 false);
1864 x += wadjust;
1865 j = i + 1;
1866 width = 0;
1869 if (j < i)
1871 font->driver->draw (s, j, i, x, y, false);
1872 if (s->face->overstrike)
1873 font->driver->draw (s, j, i, x + 1, y, false);
1879 /* Draw the foreground of glyph string S for glyphless characters. */
1881 static void
1882 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1884 struct glyph *glyph = s->first_glyph;
1885 XChar2b char2b[8];
1886 int x, i, j;
1888 /* If first glyph of S has a left box line, start drawing the text
1889 of S to the right of that box line. */
1890 if (s->face && s->face->box != FACE_NO_BOX
1891 && s->first_glyph->left_box_line_p)
1892 x = s->x + eabs (s->face->box_line_width);
1893 else
1894 x = s->x;
1896 s->char2b = char2b;
1898 for (i = 0; i < s->nchars; i++, glyph++)
1900 char buf[7], *str = NULL;
1901 int len = glyph->u.glyphless.len;
1903 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1905 if (len > 0
1906 && CHAR_TABLE_P (Vglyphless_char_display)
1907 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1908 >= 1))
1910 Lisp_Object acronym
1911 = (! glyph->u.glyphless.for_no_font
1912 ? CHAR_TABLE_REF (Vglyphless_char_display,
1913 glyph->u.glyphless.ch)
1914 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
1915 if (STRINGP (acronym))
1916 str = SSDATA (acronym);
1919 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
1921 sprintf (buf, "%0*X",
1922 glyph->u.glyphless.ch < 0x10000 ? 4 : 6,
1923 glyph->u.glyphless.ch + 0u);
1924 str = buf;
1927 if (str)
1929 int upper_len = (len + 1) / 2;
1930 unsigned code;
1932 /* It is assured that all LEN characters in STR is ASCII. */
1933 for (j = 0; j < len; j++)
1935 code = s->font->driver->encode_char (s->font, str[j]);
1936 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
1938 s->font->driver->draw (s, 0, upper_len,
1939 x + glyph->slice.glyphless.upper_xoff,
1940 s->ybase + glyph->slice.glyphless.upper_yoff,
1941 false);
1942 s->font->driver->draw (s, upper_len, len,
1943 x + glyph->slice.glyphless.lower_xoff,
1944 s->ybase + glyph->slice.glyphless.lower_yoff,
1945 false);
1947 if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
1948 x_draw_rectangle (s->f, s->gc,
1949 x, s->ybase - glyph->ascent,
1950 glyph->pixel_width - 1,
1951 glyph->ascent + glyph->descent - 1);
1952 x += glyph->pixel_width;
1956 #ifdef USE_X_TOOLKIT
1958 #ifdef USE_LUCID
1960 /* Return the frame on which widget WIDGET is used.. Abort if frame
1961 cannot be determined. */
1963 static struct frame *
1964 x_frame_of_widget (Widget widget)
1966 struct x_display_info *dpyinfo;
1967 Lisp_Object tail, frame;
1968 struct frame *f;
1970 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1972 /* Find the top-level shell of the widget. Note that this function
1973 can be called when the widget is not yet realized, so XtWindow
1974 (widget) == 0. That's the reason we can't simply use
1975 x_any_window_to_frame. */
1976 while (!XtIsTopLevelShell (widget))
1977 widget = XtParent (widget);
1979 /* Look for a frame with that top-level widget. Allocate the color
1980 on that frame to get the right gamma correction value. */
1981 FOR_EACH_FRAME (tail, frame)
1983 f = XFRAME (frame);
1984 if (FRAME_X_P (f)
1985 && f->output_data.nothing != 1
1986 && FRAME_DISPLAY_INFO (f) == dpyinfo
1987 && f->output_data.x->widget == widget)
1988 return f;
1990 emacs_abort ();
1993 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1994 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1995 If this produces the same color as PIXEL, try a color where all RGB
1996 values have DELTA added. Return the allocated color in *PIXEL.
1997 DISPLAY is the X display, CMAP is the colormap to operate on.
1998 Value is true if successful. */
2000 bool
2001 x_alloc_lighter_color_for_widget (Widget widget, Display *display, Colormap cmap,
2002 unsigned long *pixel, double factor, int delta)
2004 struct frame *f = x_frame_of_widget (widget);
2005 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
2008 #endif /* USE_LUCID */
2011 /* Structure specifying which arguments should be passed by Xt to
2012 cvt_string_to_pixel. We want the widget's screen and colormap. */
2014 static XtConvertArgRec cvt_string_to_pixel_args[] =
2016 {XtWidgetBaseOffset, (XtPointer) offsetof (WidgetRec, core.screen),
2017 sizeof (Screen *)},
2018 {XtWidgetBaseOffset, (XtPointer) offsetof (WidgetRec, core.colormap),
2019 sizeof (Colormap)}
2023 /* The address of this variable is returned by
2024 cvt_string_to_pixel. */
2026 static Pixel cvt_string_to_pixel_value;
2029 /* Convert a color name to a pixel color.
2031 DPY is the display we are working on.
2033 ARGS is an array of *NARGS XrmValue structures holding additional
2034 information about the widget for which the conversion takes place.
2035 The contents of this array are determined by the specification
2036 in cvt_string_to_pixel_args.
2038 FROM is a pointer to an XrmValue which points to the color name to
2039 convert. TO is an XrmValue in which to return the pixel color.
2041 CLOSURE_RET is a pointer to user-data, in which we record if
2042 we allocated the color or not.
2044 Value is True if successful, False otherwise. */
2046 static Boolean
2047 cvt_string_to_pixel (Display *dpy, XrmValue *args, Cardinal *nargs,
2048 XrmValue *from, XrmValue *to,
2049 XtPointer *closure_ret)
2051 Screen *screen;
2052 Colormap cmap;
2053 Pixel pixel;
2054 String color_name;
2055 XColor color;
2057 if (*nargs != 2)
2059 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
2060 "wrongParameters", "cvt_string_to_pixel",
2061 "XtToolkitError",
2062 "Screen and colormap args required", NULL, NULL);
2063 return False;
2066 screen = *(Screen **) args[0].addr;
2067 cmap = *(Colormap *) args[1].addr;
2068 color_name = (String) from->addr;
2070 if (strcmp (color_name, XtDefaultBackground) == 0)
2072 *closure_ret = (XtPointer) False;
2073 pixel = WhitePixelOfScreen (screen);
2075 else if (strcmp (color_name, XtDefaultForeground) == 0)
2077 *closure_ret = (XtPointer) False;
2078 pixel = BlackPixelOfScreen (screen);
2080 else if (XParseColor (dpy, cmap, color_name, &color)
2081 && x_alloc_nearest_color_1 (dpy, cmap, &color))
2083 pixel = color.pixel;
2084 *closure_ret = (XtPointer) True;
2086 else
2088 String params[1];
2089 Cardinal nparams = 1;
2091 params[0] = color_name;
2092 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
2093 "badValue", "cvt_string_to_pixel",
2094 "XtToolkitError", "Invalid color `%s'",
2095 params, &nparams);
2096 return False;
2099 if (to->addr != NULL)
2101 if (to->size < sizeof (Pixel))
2103 to->size = sizeof (Pixel);
2104 return False;
2107 *(Pixel *) to->addr = pixel;
2109 else
2111 cvt_string_to_pixel_value = pixel;
2112 to->addr = (XtPointer) &cvt_string_to_pixel_value;
2115 to->size = sizeof (Pixel);
2116 return True;
2120 /* Free a pixel color which was previously allocated via
2121 cvt_string_to_pixel. This is registered as the destructor
2122 for this type of resource via XtSetTypeConverter.
2124 APP is the application context in which we work.
2126 TO is a pointer to an XrmValue holding the color to free.
2127 CLOSURE is the value we stored in CLOSURE_RET for this color
2128 in cvt_string_to_pixel.
2130 ARGS and NARGS are like for cvt_string_to_pixel. */
2132 static void
2133 cvt_pixel_dtor (XtAppContext app, XrmValuePtr to, XtPointer closure, XrmValuePtr args,
2134 Cardinal *nargs)
2136 if (*nargs != 2)
2138 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
2139 "XtToolkitError",
2140 "Screen and colormap arguments required",
2141 NULL, NULL);
2143 else if (closure != NULL)
2145 /* We did allocate the pixel, so free it. */
2146 Screen *screen = *(Screen **) args[0].addr;
2147 Colormap cmap = *(Colormap *) args[1].addr;
2148 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
2149 (Pixel *) to->addr, 1);
2154 #endif /* USE_X_TOOLKIT */
2157 /* Value is an array of XColor structures for the contents of the
2158 color map of display DPY. Set *NCELLS to the size of the array.
2159 Note that this probably shouldn't be called for large color maps,
2160 say a 24-bit TrueColor map. */
2162 static const XColor *
2163 x_color_cells (Display *dpy, int *ncells)
2165 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
2167 if (dpyinfo->color_cells == NULL)
2169 Screen *screen = dpyinfo->screen;
2170 int ncolor_cells = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
2171 int i;
2173 dpyinfo->color_cells = xnmalloc (ncolor_cells,
2174 sizeof *dpyinfo->color_cells);
2175 dpyinfo->ncolor_cells = ncolor_cells;
2177 for (i = 0; i < ncolor_cells; ++i)
2178 dpyinfo->color_cells[i].pixel = i;
2180 XQueryColors (dpy, dpyinfo->cmap,
2181 dpyinfo->color_cells, ncolor_cells);
2184 *ncells = dpyinfo->ncolor_cells;
2185 return dpyinfo->color_cells;
2189 /* On frame F, translate pixel colors to RGB values for the NCOLORS
2190 colors in COLORS. Use cached information, if available. */
2192 void
2193 x_query_colors (struct frame *f, XColor *colors, int ncolors)
2195 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2197 if (dpyinfo->color_cells)
2199 int i;
2200 for (i = 0; i < ncolors; ++i)
2202 unsigned long pixel = colors[i].pixel;
2203 eassert (pixel < dpyinfo->ncolor_cells);
2204 eassert (dpyinfo->color_cells[pixel].pixel == pixel);
2205 colors[i] = dpyinfo->color_cells[pixel];
2208 else
2209 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
2213 /* On frame F, translate pixel color to RGB values for the color in
2214 COLOR. Use cached information, if available. */
2216 void
2217 x_query_color (struct frame *f, XColor *color)
2219 x_query_colors (f, color, 1);
2223 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
2224 exact match can't be allocated, try the nearest color available.
2225 Value is true if successful. Set *COLOR to the color
2226 allocated. */
2228 static bool
2229 x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color)
2231 bool rc;
2233 rc = XAllocColor (dpy, cmap, color) != 0;
2234 if (rc == 0)
2236 /* If we got to this point, the colormap is full, so we're going
2237 to try to get the next closest color. The algorithm used is
2238 a least-squares matching, which is what X uses for closest
2239 color matching with StaticColor visuals. */
2240 int nearest, i;
2241 int max_color_delta = 255;
2242 int max_delta = 3 * max_color_delta;
2243 int nearest_delta = max_delta + 1;
2244 int ncells;
2245 const XColor *cells = x_color_cells (dpy, &ncells);
2247 for (nearest = i = 0; i < ncells; ++i)
2249 int dred = (color->red >> 8) - (cells[i].red >> 8);
2250 int dgreen = (color->green >> 8) - (cells[i].green >> 8);
2251 int dblue = (color->blue >> 8) - (cells[i].blue >> 8);
2252 int delta = dred * dred + dgreen * dgreen + dblue * dblue;
2254 if (delta < nearest_delta)
2256 nearest = i;
2257 nearest_delta = delta;
2261 color->red = cells[nearest].red;
2262 color->green = cells[nearest].green;
2263 color->blue = cells[nearest].blue;
2264 rc = XAllocColor (dpy, cmap, color) != 0;
2266 else
2268 /* If allocation succeeded, and the allocated pixel color is not
2269 equal to a cached pixel color recorded earlier, there was a
2270 change in the colormap, so clear the color cache. */
2271 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
2272 XColor *cached_color;
2274 if (dpyinfo->color_cells
2275 && (cached_color = &dpyinfo->color_cells[color->pixel],
2276 (cached_color->red != color->red
2277 || cached_color->blue != color->blue
2278 || cached_color->green != color->green)))
2280 xfree (dpyinfo->color_cells);
2281 dpyinfo->color_cells = NULL;
2282 dpyinfo->ncolor_cells = 0;
2286 #ifdef DEBUG_X_COLORS
2287 if (rc)
2288 register_color (color->pixel);
2289 #endif /* DEBUG_X_COLORS */
2291 return rc;
2295 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
2296 exact match can't be allocated, try the nearest color available.
2297 Value is true if successful. Set *COLOR to the color
2298 allocated. */
2300 bool
2301 x_alloc_nearest_color (struct frame *f, Colormap cmap, XColor *color)
2303 gamma_correct (f, color);
2304 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
2308 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
2309 It's necessary to do this instead of just using PIXEL directly to
2310 get color reference counts right. */
2312 unsigned long
2313 x_copy_color (struct frame *f, unsigned long pixel)
2315 XColor color;
2317 color.pixel = pixel;
2318 block_input ();
2319 x_query_color (f, &color);
2320 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
2321 unblock_input ();
2322 #ifdef DEBUG_X_COLORS
2323 register_color (pixel);
2324 #endif
2325 return color.pixel;
2329 /* Brightness beyond which a color won't have its highlight brightness
2330 boosted.
2332 Nominally, highlight colors for `3d' faces are calculated by
2333 brightening an object's color by a constant scale factor, but this
2334 doesn't yield good results for dark colors, so for colors who's
2335 brightness is less than this value (on a scale of 0-65535) have an
2336 use an additional additive factor.
2338 The value here is set so that the default menu-bar/mode-line color
2339 (grey75) will not have its highlights changed at all. */
2340 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
2343 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
2344 or DELTA. Try a color with RGB values multiplied by FACTOR first.
2345 If this produces the same color as PIXEL, try a color where all RGB
2346 values have DELTA added. Return the allocated color in *PIXEL.
2347 DISPLAY is the X display, CMAP is the colormap to operate on.
2348 Value is non-zero if successful. */
2350 static bool
2351 x_alloc_lighter_color (struct frame *f, Display *display, Colormap cmap,
2352 unsigned long *pixel, double factor, int delta)
2354 XColor color, new;
2355 long bright;
2356 bool success_p;
2358 /* Get RGB color values. */
2359 color.pixel = *pixel;
2360 x_query_color (f, &color);
2362 /* Change RGB values by specified FACTOR. Avoid overflow! */
2363 eassert (factor >= 0);
2364 new.red = min (0xffff, factor * color.red);
2365 new.green = min (0xffff, factor * color.green);
2366 new.blue = min (0xffff, factor * color.blue);
2368 /* Calculate brightness of COLOR. */
2369 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
2371 /* We only boost colors that are darker than
2372 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
2373 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
2374 /* Make an additive adjustment to NEW, because it's dark enough so
2375 that scaling by FACTOR alone isn't enough. */
2377 /* How far below the limit this color is (0 - 1, 1 being darker). */
2378 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
2379 /* The additive adjustment. */
2380 int min_delta = delta * dimness * factor / 2;
2382 if (factor < 1)
2384 new.red = max (0, new.red - min_delta);
2385 new.green = max (0, new.green - min_delta);
2386 new.blue = max (0, new.blue - min_delta);
2388 else
2390 new.red = min (0xffff, min_delta + new.red);
2391 new.green = min (0xffff, min_delta + new.green);
2392 new.blue = min (0xffff, min_delta + new.blue);
2396 /* Try to allocate the color. */
2397 success_p = x_alloc_nearest_color (f, cmap, &new);
2398 if (success_p)
2400 if (new.pixel == *pixel)
2402 /* If we end up with the same color as before, try adding
2403 delta to the RGB values. */
2404 x_free_colors (f, &new.pixel, 1);
2406 new.red = min (0xffff, delta + color.red);
2407 new.green = min (0xffff, delta + color.green);
2408 new.blue = min (0xffff, delta + color.blue);
2409 success_p = x_alloc_nearest_color (f, cmap, &new);
2411 else
2412 success_p = true;
2413 *pixel = new.pixel;
2416 return success_p;
2420 /* Set up the foreground color for drawing relief lines of glyph
2421 string S. RELIEF is a pointer to a struct relief containing the GC
2422 with which lines will be drawn. Use a color that is FACTOR or
2423 DELTA lighter or darker than the relief's background which is found
2424 in S->f->output_data.x->relief_background. If such a color cannot
2425 be allocated, use DEFAULT_PIXEL, instead. */
2427 static void
2428 x_setup_relief_color (struct frame *f, struct relief *relief, double factor,
2429 int delta, unsigned long default_pixel)
2431 XGCValues xgcv;
2432 struct x_output *di = f->output_data.x;
2433 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
2434 unsigned long pixel;
2435 unsigned long background = di->relief_background;
2436 Colormap cmap = FRAME_X_COLORMAP (f);
2437 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2438 Display *dpy = FRAME_X_DISPLAY (f);
2440 xgcv.graphics_exposures = False;
2441 xgcv.line_width = 1;
2443 /* Free previously allocated color. The color cell will be reused
2444 when it has been freed as many times as it was allocated, so this
2445 doesn't affect faces using the same colors. */
2446 if (relief->gc && relief->pixel != -1)
2448 x_free_colors (f, &relief->pixel, 1);
2449 relief->pixel = -1;
2452 /* Allocate new color. */
2453 xgcv.foreground = default_pixel;
2454 pixel = background;
2455 if (dpyinfo->n_planes != 1
2456 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
2457 xgcv.foreground = relief->pixel = pixel;
2459 if (relief->gc == 0)
2461 xgcv.stipple = dpyinfo->gray;
2462 mask |= GCStipple;
2463 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
2465 else
2466 XChangeGC (dpy, relief->gc, mask, &xgcv);
2470 /* Set up colors for the relief lines around glyph string S. */
2472 static void
2473 x_setup_relief_colors (struct glyph_string *s)
2475 struct x_output *di = s->f->output_data.x;
2476 unsigned long color;
2478 if (s->face->use_box_color_for_shadows_p)
2479 color = s->face->box_color;
2480 else if (s->first_glyph->type == IMAGE_GLYPH
2481 && s->img->pixmap
2482 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2483 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2484 else
2486 XGCValues xgcv;
2488 /* Get the background color of the face. */
2489 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2490 color = xgcv.background;
2493 if (di->white_relief.gc == 0
2494 || color != di->relief_background)
2496 di->relief_background = color;
2497 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2498 WHITE_PIX_DEFAULT (s->f));
2499 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2500 BLACK_PIX_DEFAULT (s->f));
2505 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2506 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2507 to draw, it must be >= 0. RAISED_P means draw a raised
2508 relief. LEFT_P means draw a relief on the left side of
2509 the rectangle. RIGHT_P means draw a relief on the right
2510 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2511 when drawing. */
2513 static void
2514 x_draw_relief_rect (struct frame *f,
2515 int left_x, int top_y, int right_x, int bottom_y,
2516 int width, bool raised_p, bool top_p, bool bot_p,
2517 bool left_p, bool right_p,
2518 XRectangle *clip_rect)
2520 #ifdef USE_CAIRO
2521 GC top_left_gc, bottom_right_gc;
2522 int corners = 0;
2524 if (raised_p)
2526 top_left_gc = f->output_data.x->white_relief.gc;
2527 bottom_right_gc = f->output_data.x->black_relief.gc;
2529 else
2531 top_left_gc = f->output_data.x->black_relief.gc;
2532 bottom_right_gc = f->output_data.x->white_relief.gc;
2535 x_set_clip_rectangles (f, top_left_gc, clip_rect, 1);
2536 x_set_clip_rectangles (f, bottom_right_gc, clip_rect, 1);
2538 if (left_p)
2540 x_fill_rectangle (f, top_left_gc, left_x, top_y,
2541 width, bottom_y + 1 - top_y);
2542 if (top_p)
2543 corners |= 1 << CORNER_TOP_LEFT;
2544 if (bot_p)
2545 corners |= 1 << CORNER_BOTTOM_LEFT;
2547 if (right_p)
2549 x_fill_rectangle (f, bottom_right_gc, right_x + 1 - width, top_y,
2550 width, bottom_y + 1 - top_y);
2551 if (top_p)
2552 corners |= 1 << CORNER_TOP_RIGHT;
2553 if (bot_p)
2554 corners |= 1 << CORNER_BOTTOM_RIGHT;
2556 if (top_p)
2558 if (!right_p)
2559 x_fill_rectangle (f, top_left_gc, left_x, top_y,
2560 right_x + 1 - left_x, width);
2561 else
2562 x_fill_trapezoid_for_relief (f, top_left_gc, left_x, top_y,
2563 right_x + 1 - left_x, width, 1);
2565 if (bot_p)
2567 if (!left_p)
2568 x_fill_rectangle (f, bottom_right_gc, left_x, bottom_y + 1 - width,
2569 right_x + 1 - left_x, width);
2570 else
2571 x_fill_trapezoid_for_relief (f, bottom_right_gc,
2572 left_x, bottom_y + 1 - width,
2573 right_x + 1 - left_x, width, 0);
2575 if (left_p && width != 1)
2576 x_fill_rectangle (f, bottom_right_gc, left_x, top_y,
2577 1, bottom_y + 1 - top_y);
2578 if (top_p && width != 1)
2579 x_fill_rectangle (f, bottom_right_gc, left_x, top_y,
2580 right_x + 1 - left_x, 1);
2581 if (corners)
2583 XSetBackground (FRAME_X_DISPLAY (f), top_left_gc,
2584 FRAME_BACKGROUND_PIXEL (f));
2585 x_erase_corners_for_relief (f, top_left_gc, left_x, top_y,
2586 right_x - left_x + 1, bottom_y - top_y + 1,
2587 6, 1, corners);
2590 x_reset_clip_rectangles (f, top_left_gc);
2591 x_reset_clip_rectangles (f, bottom_right_gc);
2592 #else
2593 Display *dpy = FRAME_X_DISPLAY (f);
2594 Window window = FRAME_X_WINDOW (f);
2595 int i;
2596 GC gc;
2598 if (raised_p)
2599 gc = f->output_data.x->white_relief.gc;
2600 else
2601 gc = f->output_data.x->black_relief.gc;
2602 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2604 /* This code is more complicated than it has to be, because of two
2605 minor hacks to make the boxes look nicer: (i) if width > 1, draw
2606 the outermost line using the black relief. (ii) Omit the four
2607 corner pixels. */
2609 /* Top. */
2610 if (top_p)
2612 if (width == 1)
2613 XDrawLine (dpy, window, gc,
2614 left_x + left_p, top_y,
2615 right_x + !right_p, top_y);
2617 for (i = 1; i < width; ++i)
2618 XDrawLine (dpy, window, gc,
2619 left_x + i * left_p, top_y + i,
2620 right_x + 1 - i * right_p, top_y + i);
2623 /* Left. */
2624 if (left_p)
2626 if (width == 1)
2627 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
2629 XClearArea (dpy, window, left_x, top_y, 1, 1, False);
2630 XClearArea (dpy, window, left_x, bottom_y, 1, 1, False);
2632 for (i = (width > 1 ? 1 : 0); i < width; ++i)
2633 XDrawLine (dpy, window, gc,
2634 left_x + i, top_y + (i + 1) * top_p,
2635 left_x + i, bottom_y + 1 - (i + 1) * bot_p);
2638 XSetClipMask (dpy, gc, None);
2639 if (raised_p)
2640 gc = f->output_data.x->black_relief.gc;
2641 else
2642 gc = f->output_data.x->white_relief.gc;
2643 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2645 if (width > 1)
2647 /* Outermost top line. */
2648 if (top_p)
2649 XDrawLine (dpy, window, gc,
2650 left_x + left_p, top_y,
2651 right_x + !right_p, top_y);
2653 /* Outermost left line. */
2654 if (left_p)
2655 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
2658 /* Bottom. */
2659 if (bot_p)
2661 XDrawLine (dpy, window, gc,
2662 left_x + left_p, bottom_y,
2663 right_x + !right_p, bottom_y);
2664 for (i = 1; i < width; ++i)
2665 XDrawLine (dpy, window, gc,
2666 left_x + i * left_p, bottom_y - i,
2667 right_x + 1 - i * right_p, bottom_y - i);
2670 /* Right. */
2671 if (right_p)
2673 XClearArea (dpy, window, right_x, top_y, 1, 1, False);
2674 XClearArea (dpy, window, right_x, bottom_y, 1, 1, False);
2675 for (i = 0; i < width; ++i)
2676 XDrawLine (dpy, window, gc,
2677 right_x - i, top_y + (i + 1) * top_p,
2678 right_x - i, bottom_y + 1 - (i + 1) * bot_p);
2681 x_reset_clip_rectangles (f, gc);
2683 #endif
2687 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2688 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2689 draw, it must be >= 0. LEFT_P means draw a line on the
2690 left side of the rectangle. RIGHT_P means draw a line
2691 on the right side of the rectangle. CLIP_RECT is the clipping
2692 rectangle to use when drawing. */
2694 static void
2695 x_draw_box_rect (struct glyph_string *s,
2696 int left_x, int top_y, int right_x, int bottom_y, int width,
2697 bool left_p, bool right_p, XRectangle *clip_rect)
2699 XGCValues xgcv;
2701 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2702 XSetForeground (s->display, s->gc, s->face->box_color);
2703 x_set_clip_rectangles (s->f, s->gc, clip_rect, 1);
2705 /* Top. */
2706 x_fill_rectangle (s->f, s->gc,
2707 left_x, top_y, right_x - left_x + 1, width);
2709 /* Left. */
2710 if (left_p)
2711 x_fill_rectangle (s->f, s->gc,
2712 left_x, top_y, width, bottom_y - top_y + 1);
2714 /* Bottom. */
2715 x_fill_rectangle (s->f, s->gc,
2716 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2718 /* Right. */
2719 if (right_p)
2720 x_fill_rectangle (s->f, s->gc,
2721 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2723 XSetForeground (s->display, s->gc, xgcv.foreground);
2724 x_reset_clip_rectangles (s->f, s->gc);
2728 /* Draw a box around glyph string S. */
2730 static void
2731 x_draw_glyph_string_box (struct glyph_string *s)
2733 int width, left_x, right_x, top_y, bottom_y, last_x;
2734 bool raised_p, left_p, right_p;
2735 struct glyph *last_glyph;
2736 XRectangle clip_rect;
2738 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2739 ? WINDOW_RIGHT_EDGE_X (s->w)
2740 : window_box_right (s->w, s->area));
2742 /* The glyph that may have a right box line. */
2743 last_glyph = (s->cmp || s->img
2744 ? s->first_glyph
2745 : s->first_glyph + s->nchars - 1);
2747 width = eabs (s->face->box_line_width);
2748 raised_p = s->face->box == FACE_RAISED_BOX;
2749 left_x = s->x;
2750 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2751 ? last_x - 1
2752 : min (last_x, s->x + s->background_width) - 1);
2753 top_y = s->y;
2754 bottom_y = top_y + s->height - 1;
2756 left_p = (s->first_glyph->left_box_line_p
2757 || (s->hl == DRAW_MOUSE_FACE
2758 && (s->prev == NULL
2759 || s->prev->hl != s->hl)));
2760 right_p = (last_glyph->right_box_line_p
2761 || (s->hl == DRAW_MOUSE_FACE
2762 && (s->next == NULL
2763 || s->next->hl != s->hl)));
2765 get_glyph_string_clip_rect (s, &clip_rect);
2767 if (s->face->box == FACE_SIMPLE_BOX)
2768 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2769 left_p, right_p, &clip_rect);
2770 else
2772 x_setup_relief_colors (s);
2773 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2774 width, raised_p, true, true, left_p, right_p,
2775 &clip_rect);
2780 /* Draw foreground of image glyph string S. */
2782 static void
2783 x_draw_image_foreground (struct glyph_string *s)
2785 int x = s->x;
2786 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2788 /* If first glyph of S has a left box line, start drawing it to the
2789 right of that line. */
2790 if (s->face->box != FACE_NO_BOX
2791 && s->first_glyph->left_box_line_p
2792 && s->slice.x == 0)
2793 x += eabs (s->face->box_line_width);
2795 /* If there is a margin around the image, adjust x- and y-position
2796 by that margin. */
2797 if (s->slice.x == 0)
2798 x += s->img->hmargin;
2799 if (s->slice.y == 0)
2800 y += s->img->vmargin;
2802 if (s->img->pixmap)
2804 if (s->img->mask)
2806 /* We can't set both a clip mask and use XSetClipRectangles
2807 because the latter also sets a clip mask. We also can't
2808 trust on the shape extension to be available
2809 (XShapeCombineRegion). So, compute the rectangle to draw
2810 manually. */
2811 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2812 | GCFunction);
2813 XGCValues xgcv;
2814 XRectangle clip_rect, image_rect, r;
2816 xgcv.clip_mask = s->img->mask;
2817 xgcv.clip_x_origin = x;
2818 xgcv.clip_y_origin = y;
2819 xgcv.function = GXcopy;
2820 XChangeGC (s->display, s->gc, mask, &xgcv);
2822 get_glyph_string_clip_rect (s, &clip_rect);
2823 image_rect.x = x;
2824 image_rect.y = y;
2825 image_rect.width = s->slice.width;
2826 image_rect.height = s->slice.height;
2827 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2828 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2829 s->slice.x + r.x - x, s->slice.y + r.y - y,
2830 r.width, r.height, r.x, r.y);
2832 else
2834 XRectangle clip_rect, image_rect, r;
2836 get_glyph_string_clip_rect (s, &clip_rect);
2837 image_rect.x = x;
2838 image_rect.y = y;
2839 image_rect.width = s->slice.width;
2840 image_rect.height = s->slice.height;
2841 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2842 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2843 s->slice.x + r.x - x, s->slice.y + r.y - y,
2844 r.width, r.height, r.x, r.y);
2846 /* When the image has a mask, we can expect that at
2847 least part of a mouse highlight or a block cursor will
2848 be visible. If the image doesn't have a mask, make
2849 a block cursor visible by drawing a rectangle around
2850 the image. I believe it's looking better if we do
2851 nothing here for mouse-face. */
2852 if (s->hl == DRAW_CURSOR)
2854 int relief = eabs (s->img->relief);
2855 x_draw_rectangle (s->f, s->gc,
2856 x - relief, y - relief,
2857 s->slice.width + relief*2 - 1,
2858 s->slice.height + relief*2 - 1);
2862 else
2863 /* Draw a rectangle if image could not be loaded. */
2864 x_draw_rectangle (s->f, s->gc, x, y,
2865 s->slice.width - 1, s->slice.height - 1);
2869 /* Draw a relief around the image glyph string S. */
2871 static void
2872 x_draw_image_relief (struct glyph_string *s)
2874 int x1, y1, thick;
2875 bool raised_p, top_p, bot_p, left_p, right_p;
2876 int extra_x, extra_y;
2877 XRectangle r;
2878 int x = s->x;
2879 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2881 /* If first glyph of S has a left box line, start drawing it to the
2882 right of that line. */
2883 if (s->face->box != FACE_NO_BOX
2884 && s->first_glyph->left_box_line_p
2885 && s->slice.x == 0)
2886 x += eabs (s->face->box_line_width);
2888 /* If there is a margin around the image, adjust x- and y-position
2889 by that margin. */
2890 if (s->slice.x == 0)
2891 x += s->img->hmargin;
2892 if (s->slice.y == 0)
2893 y += s->img->vmargin;
2895 if (s->hl == DRAW_IMAGE_SUNKEN
2896 || s->hl == DRAW_IMAGE_RAISED)
2898 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2899 raised_p = s->hl == DRAW_IMAGE_RAISED;
2901 else
2903 thick = eabs (s->img->relief);
2904 raised_p = s->img->relief > 0;
2907 x1 = x + s->slice.width - 1;
2908 y1 = y + s->slice.height - 1;
2910 extra_x = extra_y = 0;
2911 if (s->face->id == TOOL_BAR_FACE_ID)
2913 if (CONSP (Vtool_bar_button_margin)
2914 && INTEGERP (XCAR (Vtool_bar_button_margin))
2915 && INTEGERP (XCDR (Vtool_bar_button_margin)))
2917 extra_x = XINT (XCAR (Vtool_bar_button_margin));
2918 extra_y = XINT (XCDR (Vtool_bar_button_margin));
2920 else if (INTEGERP (Vtool_bar_button_margin))
2921 extra_x = extra_y = XINT (Vtool_bar_button_margin);
2924 top_p = bot_p = left_p = right_p = false;
2926 if (s->slice.x == 0)
2927 x -= thick + extra_x, left_p = true;
2928 if (s->slice.y == 0)
2929 y -= thick + extra_y, top_p = true;
2930 if (s->slice.x + s->slice.width == s->img->width)
2931 x1 += thick + extra_x, right_p = true;
2932 if (s->slice.y + s->slice.height == s->img->height)
2933 y1 += thick + extra_y, bot_p = true;
2935 x_setup_relief_colors (s);
2936 get_glyph_string_clip_rect (s, &r);
2937 x_draw_relief_rect (s->f, x, y, x1, y1, thick, raised_p,
2938 top_p, bot_p, left_p, right_p, &r);
2942 /* Draw the foreground of image glyph string S to PIXMAP. */
2944 static void
2945 x_draw_image_foreground_1 (struct glyph_string *s, Pixmap pixmap)
2947 int x = 0;
2948 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2950 /* If first glyph of S has a left box line, start drawing it to the
2951 right of that line. */
2952 if (s->face->box != FACE_NO_BOX
2953 && s->first_glyph->left_box_line_p
2954 && s->slice.x == 0)
2955 x += eabs (s->face->box_line_width);
2957 /* If there is a margin around the image, adjust x- and y-position
2958 by that margin. */
2959 if (s->slice.x == 0)
2960 x += s->img->hmargin;
2961 if (s->slice.y == 0)
2962 y += s->img->vmargin;
2964 if (s->img->pixmap)
2966 if (s->img->mask)
2968 /* We can't set both a clip mask and use XSetClipRectangles
2969 because the latter also sets a clip mask. We also can't
2970 trust on the shape extension to be available
2971 (XShapeCombineRegion). So, compute the rectangle to draw
2972 manually. */
2973 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2974 | GCFunction);
2975 XGCValues xgcv;
2977 xgcv.clip_mask = s->img->mask;
2978 xgcv.clip_x_origin = x - s->slice.x;
2979 xgcv.clip_y_origin = y - s->slice.y;
2980 xgcv.function = GXcopy;
2981 XChangeGC (s->display, s->gc, mask, &xgcv);
2983 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2984 s->slice.x, s->slice.y,
2985 s->slice.width, s->slice.height, x, y);
2986 XSetClipMask (s->display, s->gc, None);
2988 else
2990 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2991 s->slice.x, s->slice.y,
2992 s->slice.width, s->slice.height, x, y);
2994 /* When the image has a mask, we can expect that at
2995 least part of a mouse highlight or a block cursor will
2996 be visible. If the image doesn't have a mask, make
2997 a block cursor visible by drawing a rectangle around
2998 the image. I believe it's looking better if we do
2999 nothing here for mouse-face. */
3000 if (s->hl == DRAW_CURSOR)
3002 int r = eabs (s->img->relief);
3003 x_draw_rectangle (s->f, s->gc, x - r, y - r,
3004 s->slice.width + r*2 - 1,
3005 s->slice.height + r*2 - 1);
3009 else
3010 /* Draw a rectangle if image could not be loaded. */
3011 x_draw_rectangle (s->f, s->gc, x, y,
3012 s->slice.width - 1, s->slice.height - 1);
3016 /* Draw part of the background of glyph string S. X, Y, W, and H
3017 give the rectangle to draw. */
3019 static void
3020 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
3022 if (s->stippled_p)
3024 /* Fill background with a stipple pattern. */
3025 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3026 x_fill_rectangle (s->f, s->gc, x, y, w, h);
3027 XSetFillStyle (s->display, s->gc, FillSolid);
3029 else
3030 x_clear_glyph_string_rect (s, x, y, w, h);
3034 /* Draw image glyph string S.
3036 s->y
3037 s->x +-------------------------
3038 | s->face->box
3040 | +-------------------------
3041 | | s->img->margin
3043 | | +-------------------
3044 | | | the image
3048 static void
3049 x_draw_image_glyph_string (struct glyph_string *s)
3051 int box_line_hwidth = eabs (s->face->box_line_width);
3052 int box_line_vwidth = max (s->face->box_line_width, 0);
3053 int height;
3054 Pixmap pixmap = None;
3056 height = s->height;
3057 if (s->slice.y == 0)
3058 height -= box_line_vwidth;
3059 if (s->slice.y + s->slice.height >= s->img->height)
3060 height -= box_line_vwidth;
3062 /* Fill background with face under the image. Do it only if row is
3063 taller than image or if image has a clip mask to reduce
3064 flickering. */
3065 s->stippled_p = s->face->stipple != 0;
3066 if (height > s->slice.height
3067 || s->img->hmargin
3068 || s->img->vmargin
3069 || s->img->mask
3070 || s->img->pixmap == 0
3071 || s->width != s->background_width)
3073 if (s->img->mask)
3075 /* Create a pixmap as large as the glyph string. Fill it
3076 with the background color. Copy the image to it, using
3077 its mask. Copy the temporary pixmap to the display. */
3078 Screen *screen = FRAME_X_SCREEN (s->f);
3079 int depth = DefaultDepthOfScreen (screen);
3081 /* Create a pixmap as large as the glyph string. */
3082 pixmap = XCreatePixmap (s->display, s->window,
3083 s->background_width,
3084 s->height, depth);
3086 /* Don't clip in the following because we're working on the
3087 pixmap. */
3088 XSetClipMask (s->display, s->gc, None);
3090 /* Fill the pixmap with the background color/stipple. */
3091 if (s->stippled_p)
3093 /* Fill background with a stipple pattern. */
3094 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3095 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
3096 XFillRectangle (s->display, pixmap, s->gc,
3097 0, 0, s->background_width, s->height);
3098 XSetFillStyle (s->display, s->gc, FillSolid);
3099 XSetTSOrigin (s->display, s->gc, 0, 0);
3101 else
3103 XGCValues xgcv;
3104 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3105 &xgcv);
3106 XSetForeground (s->display, s->gc, xgcv.background);
3107 XFillRectangle (s->display, pixmap, s->gc,
3108 0, 0, s->background_width, s->height);
3109 XSetForeground (s->display, s->gc, xgcv.foreground);
3112 else
3114 int x = s->x;
3115 int y = s->y;
3116 int width = s->background_width;
3118 if (s->first_glyph->left_box_line_p
3119 && s->slice.x == 0)
3121 x += box_line_hwidth;
3122 width -= box_line_hwidth;
3125 if (s->slice.y == 0)
3126 y += box_line_vwidth;
3128 x_draw_glyph_string_bg_rect (s, x, y, width, height);
3131 s->background_filled_p = true;
3134 /* Draw the foreground. */
3135 #ifdef USE_CAIRO
3136 if (s->img->cr_data)
3138 cairo_t *cr = x_begin_cr_clip (s->f, s->gc);
3140 int x = s->x + s->img->hmargin;
3141 int y = s->y + s->img->vmargin;
3142 int width = s->background_width;
3144 cairo_set_source_surface (cr, s->img->cr_data,
3145 x - s->slice.x,
3146 y - s->slice.y);
3147 cairo_rectangle (cr, x, y, width, height);
3148 cairo_fill (cr);
3149 x_end_cr_clip (s->f);
3151 else
3152 #endif
3153 if (pixmap != None)
3155 x_draw_image_foreground_1 (s, pixmap);
3156 x_set_glyph_string_clipping (s);
3157 XCopyArea (s->display, pixmap, s->window, s->gc,
3158 0, 0, s->background_width, s->height, s->x, s->y);
3159 XFreePixmap (s->display, pixmap);
3161 else
3162 x_draw_image_foreground (s);
3164 /* If we must draw a relief around the image, do it. */
3165 if (s->img->relief
3166 || s->hl == DRAW_IMAGE_RAISED
3167 || s->hl == DRAW_IMAGE_SUNKEN)
3168 x_draw_image_relief (s);
3172 /* Draw stretch glyph string S. */
3174 static void
3175 x_draw_stretch_glyph_string (struct glyph_string *s)
3177 eassert (s->first_glyph->type == STRETCH_GLYPH);
3179 if (s->hl == DRAW_CURSOR
3180 && !x_stretch_cursor_p)
3182 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
3183 wide as the stretch glyph. */
3184 int width, background_width = s->background_width;
3185 int x = s->x;
3187 if (!s->row->reversed_p)
3189 int left_x = window_box_left_offset (s->w, TEXT_AREA);
3191 if (x < left_x)
3193 background_width -= left_x - x;
3194 x = left_x;
3197 else
3199 /* In R2L rows, draw the cursor on the right edge of the
3200 stretch glyph. */
3201 int right_x = window_box_right (s->w, TEXT_AREA);
3203 if (x + background_width > right_x)
3204 background_width -= x - right_x;
3205 x += background_width;
3207 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
3208 if (s->row->reversed_p)
3209 x -= width;
3211 /* Draw cursor. */
3212 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
3214 /* Clear rest using the GC of the original non-cursor face. */
3215 if (width < background_width)
3217 int y = s->y;
3218 int w = background_width - width, h = s->height;
3219 XRectangle r;
3220 GC gc;
3222 if (!s->row->reversed_p)
3223 x += width;
3224 else
3225 x = s->x;
3226 if (s->row->mouse_face_p
3227 && cursor_in_mouse_face_p (s->w))
3229 x_set_mouse_face_gc (s);
3230 gc = s->gc;
3232 else
3233 gc = s->face->gc;
3235 get_glyph_string_clip_rect (s, &r);
3236 x_set_clip_rectangles (s->f, gc, &r, 1);
3238 if (s->face->stipple)
3240 /* Fill background with a stipple pattern. */
3241 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3242 x_fill_rectangle (s->f, gc, x, y, w, h);
3243 XSetFillStyle (s->display, gc, FillSolid);
3245 else
3247 XGCValues xgcv;
3248 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3249 XSetForeground (s->display, gc, xgcv.background);
3250 x_fill_rectangle (s->f, gc, x, y, w, h);
3251 XSetForeground (s->display, gc, xgcv.foreground);
3254 x_reset_clip_rectangles (s->f, gc);
3257 else if (!s->background_filled_p)
3259 int background_width = s->background_width;
3260 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
3262 /* Don't draw into left margin, fringe or scrollbar area
3263 except for header line and mode line. */
3264 if (x < left_x && !s->row->mode_line_p)
3266 background_width -= left_x - x;
3267 x = left_x;
3269 if (background_width > 0)
3270 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
3273 s->background_filled_p = true;
3277 Draw a wavy line under S. The wave fills wave_height pixels from y0.
3279 x0 wave_length = 2
3281 y0 * * * * *
3282 |* * * * * * * * *
3283 wave_height = 3 | * * * *
3287 static void
3288 x_draw_underwave (struct glyph_string *s)
3290 int wave_height = 3, wave_length = 2;
3291 #ifdef USE_CAIRO
3292 x_draw_horizontal_wave (s->f, s->gc, s->x, s->ybase - wave_height + 3,
3293 s->width, wave_height, wave_length);
3294 #else /* not USE_CAIRO */
3295 int dx, dy, x0, y0, width, x1, y1, x2, y2, xmax;
3296 bool odd;
3297 XRectangle wave_clip, string_clip, final_clip;
3299 dx = wave_length;
3300 dy = wave_height - 1;
3301 x0 = s->x;
3302 y0 = s->ybase - wave_height + 3;
3303 width = s->width;
3304 xmax = x0 + width;
3306 /* Find and set clipping rectangle */
3308 wave_clip.x = x0;
3309 wave_clip.y = y0;
3310 wave_clip.width = width;
3311 wave_clip.height = wave_height;
3312 get_glyph_string_clip_rect (s, &string_clip);
3314 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
3315 return;
3317 XSetClipRectangles (s->display, s->gc, 0, 0, &final_clip, 1, Unsorted);
3319 /* Draw the waves */
3321 x1 = x0 - (x0 % dx);
3322 x2 = x1 + dx;
3323 odd = (x1 / dx) & 1;
3324 y1 = y2 = y0;
3326 if (odd)
3327 y1 += dy;
3328 else
3329 y2 += dy;
3331 if (INT_MAX - dx < xmax)
3332 emacs_abort ();
3334 while (x1 <= xmax)
3336 XDrawLine (s->display, s->window, s->gc, x1, y1, x2, y2);
3337 x1 = x2, y1 = y2;
3338 x2 += dx, y2 = y0 + odd*dy;
3339 odd = !odd;
3342 /* Restore previous clipping rectangle(s) */
3343 XSetClipRectangles (s->display, s->gc, 0, 0, s->clip, s->num_clips, Unsorted);
3344 #endif /* not USE_CAIRO */
3348 /* Draw glyph string S. */
3350 static void
3351 x_draw_glyph_string (struct glyph_string *s)
3353 bool relief_drawn_p = false;
3355 /* If S draws into the background of its successors, draw the
3356 background of the successors first so that S can draw into it.
3357 This makes S->next use XDrawString instead of XDrawImageString. */
3358 if (s->next && s->right_overhang && !s->for_overlaps)
3360 int width;
3361 struct glyph_string *next;
3363 for (width = 0, next = s->next;
3364 next && width < s->right_overhang;
3365 width += next->width, next = next->next)
3366 if (next->first_glyph->type != IMAGE_GLYPH)
3368 x_set_glyph_string_gc (next);
3369 x_set_glyph_string_clipping (next);
3370 if (next->first_glyph->type == STRETCH_GLYPH)
3371 x_draw_stretch_glyph_string (next);
3372 else
3373 x_draw_glyph_string_background (next, true);
3374 next->num_clips = 0;
3378 /* Set up S->gc, set clipping and draw S. */
3379 x_set_glyph_string_gc (s);
3381 /* Draw relief (if any) in advance for char/composition so that the
3382 glyph string can be drawn over it. */
3383 if (!s->for_overlaps
3384 && s->face->box != FACE_NO_BOX
3385 && (s->first_glyph->type == CHAR_GLYPH
3386 || s->first_glyph->type == COMPOSITE_GLYPH))
3389 x_set_glyph_string_clipping (s);
3390 x_draw_glyph_string_background (s, true);
3391 x_draw_glyph_string_box (s);
3392 x_set_glyph_string_clipping (s);
3393 relief_drawn_p = true;
3395 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
3396 && !s->clip_tail
3397 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
3398 || (s->next && s->next->hl != s->hl && s->right_overhang)))
3399 /* We must clip just this glyph. left_overhang part has already
3400 drawn when s->prev was drawn, and right_overhang part will be
3401 drawn later when s->next is drawn. */
3402 x_set_glyph_string_clipping_exactly (s, s);
3403 else
3404 x_set_glyph_string_clipping (s);
3406 switch (s->first_glyph->type)
3408 case IMAGE_GLYPH:
3409 x_draw_image_glyph_string (s);
3410 break;
3412 case STRETCH_GLYPH:
3413 x_draw_stretch_glyph_string (s);
3414 break;
3416 case CHAR_GLYPH:
3417 if (s->for_overlaps)
3418 s->background_filled_p = true;
3419 else
3420 x_draw_glyph_string_background (s, false);
3421 x_draw_glyph_string_foreground (s);
3422 break;
3424 case COMPOSITE_GLYPH:
3425 if (s->for_overlaps || (s->cmp_from > 0
3426 && ! s->first_glyph->u.cmp.automatic))
3427 s->background_filled_p = true;
3428 else
3429 x_draw_glyph_string_background (s, true);
3430 x_draw_composite_glyph_string_foreground (s);
3431 break;
3433 case GLYPHLESS_GLYPH:
3434 if (s->for_overlaps)
3435 s->background_filled_p = true;
3436 else
3437 x_draw_glyph_string_background (s, true);
3438 x_draw_glyphless_glyph_string_foreground (s);
3439 break;
3441 default:
3442 emacs_abort ();
3445 if (!s->for_overlaps)
3447 /* Draw underline. */
3448 if (s->face->underline_p)
3450 if (s->face->underline_type == FACE_UNDER_WAVE)
3452 if (s->face->underline_defaulted_p)
3453 x_draw_underwave (s);
3454 else
3456 XGCValues xgcv;
3457 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3458 XSetForeground (s->display, s->gc, s->face->underline_color);
3459 x_draw_underwave (s);
3460 XSetForeground (s->display, s->gc, xgcv.foreground);
3463 else if (s->face->underline_type == FACE_UNDER_LINE)
3465 unsigned long thickness, position;
3466 int y;
3468 if (s->prev && s->prev->face->underline_p
3469 && s->prev->face->underline_type == FACE_UNDER_LINE)
3471 /* We use the same underline style as the previous one. */
3472 thickness = s->prev->underline_thickness;
3473 position = s->prev->underline_position;
3475 else
3477 /* Get the underline thickness. Default is 1 pixel. */
3478 if (s->font && s->font->underline_thickness > 0)
3479 thickness = s->font->underline_thickness;
3480 else
3481 thickness = 1;
3482 if (x_underline_at_descent_line)
3483 position = (s->height - thickness) - (s->ybase - s->y);
3484 else
3486 /* Get the underline position. This is the recommended
3487 vertical offset in pixels from the baseline to the top of
3488 the underline. This is a signed value according to the
3489 specs, and its default is
3491 ROUND ((maximum descent) / 2), with
3492 ROUND(x) = floor (x + 0.5) */
3494 if (x_use_underline_position_properties
3495 && s->font && s->font->underline_position >= 0)
3496 position = s->font->underline_position;
3497 else if (s->font)
3498 position = (s->font->descent + 1) / 2;
3499 else
3500 position = underline_minimum_offset;
3502 position = max (position, underline_minimum_offset);
3504 /* Check the sanity of thickness and position. We should
3505 avoid drawing underline out of the current line area. */
3506 if (s->y + s->height <= s->ybase + position)
3507 position = (s->height - 1) - (s->ybase - s->y);
3508 if (s->y + s->height < s->ybase + position + thickness)
3509 thickness = (s->y + s->height) - (s->ybase + position);
3510 s->underline_thickness = thickness;
3511 s->underline_position = position;
3512 y = s->ybase + position;
3513 if (s->face->underline_defaulted_p)
3514 x_fill_rectangle (s->f, s->gc,
3515 s->x, y, s->width, thickness);
3516 else
3518 XGCValues xgcv;
3519 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3520 XSetForeground (s->display, s->gc, s->face->underline_color);
3521 x_fill_rectangle (s->f, s->gc,
3522 s->x, y, s->width, thickness);
3523 XSetForeground (s->display, s->gc, xgcv.foreground);
3527 /* Draw overline. */
3528 if (s->face->overline_p)
3530 unsigned long dy = 0, h = 1;
3532 if (s->face->overline_color_defaulted_p)
3533 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3534 s->width, h);
3535 else
3537 XGCValues xgcv;
3538 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3539 XSetForeground (s->display, s->gc, s->face->overline_color);
3540 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3541 s->width, h);
3542 XSetForeground (s->display, s->gc, xgcv.foreground);
3546 /* Draw strike-through. */
3547 if (s->face->strike_through_p)
3549 unsigned long h = 1;
3550 unsigned long dy = (s->height - h) / 2;
3552 if (s->face->strike_through_color_defaulted_p)
3553 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3554 s->width, h);
3555 else
3557 XGCValues xgcv;
3558 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3559 XSetForeground (s->display, s->gc, s->face->strike_through_color);
3560 x_fill_rectangle (s->f, s->gc, s->x, s->y + dy,
3561 s->width, h);
3562 XSetForeground (s->display, s->gc, xgcv.foreground);
3566 /* Draw relief if not yet drawn. */
3567 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
3568 x_draw_glyph_string_box (s);
3570 if (s->prev)
3572 struct glyph_string *prev;
3574 for (prev = s->prev; prev; prev = prev->prev)
3575 if (prev->hl != s->hl
3576 && prev->x + prev->width + prev->right_overhang > s->x)
3578 /* As prev was drawn while clipped to its own area, we
3579 must draw the right_overhang part using s->hl now. */
3580 enum draw_glyphs_face save = prev->hl;
3582 prev->hl = s->hl;
3583 x_set_glyph_string_gc (prev);
3584 x_set_glyph_string_clipping_exactly (s, prev);
3585 if (prev->first_glyph->type == CHAR_GLYPH)
3586 x_draw_glyph_string_foreground (prev);
3587 else
3588 x_draw_composite_glyph_string_foreground (prev);
3589 x_reset_clip_rectangles (prev->f, prev->gc);
3590 prev->hl = save;
3591 prev->num_clips = 0;
3595 if (s->next)
3597 struct glyph_string *next;
3599 for (next = s->next; next; next = next->next)
3600 if (next->hl != s->hl
3601 && next->x - next->left_overhang < s->x + s->width)
3603 /* As next will be drawn while clipped to its own area,
3604 we must draw the left_overhang part using s->hl now. */
3605 enum draw_glyphs_face save = next->hl;
3607 next->hl = s->hl;
3608 x_set_glyph_string_gc (next);
3609 x_set_glyph_string_clipping_exactly (s, next);
3610 if (next->first_glyph->type == CHAR_GLYPH)
3611 x_draw_glyph_string_foreground (next);
3612 else
3613 x_draw_composite_glyph_string_foreground (next);
3614 x_reset_clip_rectangles (next->f, next->gc);
3615 next->hl = save;
3616 next->num_clips = 0;
3617 next->clip_head = s->next;
3622 /* Reset clipping. */
3623 x_reset_clip_rectangles (s->f, s->gc);
3624 s->num_clips = 0;
3627 /* Shift display to make room for inserted glyphs. */
3629 static void
3630 x_shift_glyphs_for_insert (struct frame *f, int x, int y, int width, int height, int shift_by)
3632 /* Never called on a GUI frame, see
3633 http://lists.gnu.org/archive/html/emacs-devel/2015-05/msg00456.html
3635 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3636 f->output_data.x->normal_gc,
3637 x, y, width, height,
3638 x + shift_by, y);
3641 /* Delete N glyphs at the nominal cursor position. Not implemented
3642 for X frames. */
3644 static void
3645 x_delete_glyphs (struct frame *f, register int n)
3647 emacs_abort ();
3651 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
3652 If they are <= 0, this is probably an error. */
3654 static void
3655 x_clear_area1 (Display *dpy, Window window,
3656 int x, int y, int width, int height, int exposures)
3658 eassert (width > 0 && height > 0);
3659 XClearArea (dpy, window, x, y, width, height, exposures);
3663 void
3664 x_clear_area (struct frame *f, int x, int y, int width, int height)
3666 #ifdef USE_CAIRO
3667 cairo_t *cr;
3669 eassert (width > 0 && height > 0);
3671 cr = x_begin_cr_clip (f, NULL);
3672 x_set_cr_source_with_gc_background (f, f->output_data.x->normal_gc);
3673 cairo_rectangle (cr, x, y, width, height);
3674 cairo_fill (cr);
3675 x_end_cr_clip (f);
3676 #else
3677 x_clear_area1 (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3678 x, y, width, height, False);
3679 #endif
3683 /* Clear an entire frame. */
3685 static void
3686 x_clear_frame (struct frame *f)
3688 /* Clearing the frame will erase any cursor, so mark them all as no
3689 longer visible. */
3690 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
3692 block_input ();
3694 x_clear_window (f);
3696 /* We have to clear the scroll bars. If we have changed colors or
3697 something like that, then they should be notified. */
3698 x_scroll_bar_clear (f);
3700 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3701 /* Make sure scroll bars are redrawn. As they aren't redrawn by
3702 redisplay, do it here. */
3703 if (FRAME_GTK_WIDGET (f))
3704 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
3705 #endif
3707 XFlush (FRAME_X_DISPLAY (f));
3709 unblock_input ();
3712 /* RIF: Show hourglass cursor on frame F. */
3714 static void
3715 x_show_hourglass (struct frame *f)
3717 Display *dpy = FRAME_X_DISPLAY (f);
3719 if (dpy)
3721 struct x_output *x = FRAME_X_OUTPUT (f);
3722 #ifdef USE_X_TOOLKIT
3723 if (x->widget)
3724 #else
3725 if (FRAME_OUTER_WINDOW (f))
3726 #endif
3728 x->hourglass_p = true;
3730 if (!x->hourglass_window)
3732 unsigned long mask = CWCursor;
3733 XSetWindowAttributes attrs;
3734 #ifdef USE_GTK
3735 Window parent = FRAME_X_WINDOW (f);
3736 #else
3737 Window parent = FRAME_OUTER_WINDOW (f);
3738 #endif
3739 attrs.cursor = x->hourglass_cursor;
3741 x->hourglass_window = XCreateWindow
3742 (dpy, parent, 0, 0, 32000, 32000, 0, 0,
3743 InputOnly, CopyFromParent, mask, &attrs);
3746 XMapRaised (dpy, x->hourglass_window);
3747 XFlush (dpy);
3752 /* RIF: Cancel hourglass cursor on frame F. */
3754 static void
3755 x_hide_hourglass (struct frame *f)
3757 struct x_output *x = FRAME_X_OUTPUT (f);
3759 /* Watch out for newly created frames. */
3760 if (x->hourglass_window)
3762 XUnmapWindow (FRAME_X_DISPLAY (f), x->hourglass_window);
3763 /* Sync here because XTread_socket looks at the
3764 hourglass_p flag that is reset to zero below. */
3765 XSync (FRAME_X_DISPLAY (f), False);
3766 x->hourglass_p = false;
3770 /* Invert the middle quarter of the frame for .15 sec. */
3772 static void
3773 XTflash (struct frame *f)
3775 block_input ();
3778 #ifdef USE_GTK
3779 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
3780 when the scroll bars and the edit widget share the same X window. */
3781 GdkWindow *window = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
3782 #ifdef HAVE_GTK3
3783 cairo_t *cr = gdk_cairo_create (window);
3784 cairo_set_source_rgb (cr, 1, 1, 1);
3785 cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
3786 #define XFillRectangle(d, win, gc, x, y, w, h) \
3787 do { \
3788 cairo_rectangle (cr, x, y, w, h); \
3789 cairo_fill (cr); \
3791 while (false)
3792 #else /* ! HAVE_GTK3 */
3793 GdkGCValues vals;
3794 GdkGC *gc;
3795 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
3796 ^ FRAME_BACKGROUND_PIXEL (f));
3797 vals.function = GDK_XOR;
3798 gc = gdk_gc_new_with_values (window,
3799 &vals, GDK_GC_FUNCTION | GDK_GC_FOREGROUND);
3800 #define XFillRectangle(d, win, gc, x, y, w, h) \
3801 gdk_draw_rectangle (window, gc, true, x, y, w, h)
3802 #endif /* ! HAVE_GTK3 */
3803 #else /* ! USE_GTK */
3804 GC gc;
3806 /* Create a GC that will use the GXxor function to flip foreground
3807 pixels into background pixels. */
3809 XGCValues values;
3811 values.function = GXxor;
3812 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3813 ^ FRAME_BACKGROUND_PIXEL (f));
3815 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3816 GCFunction | GCForeground, &values);
3818 #endif
3820 /* Get the height not including a menu bar widget. */
3821 int height = FRAME_PIXEL_HEIGHT (f);
3822 /* Height of each line to flash. */
3823 int flash_height = FRAME_LINE_HEIGHT (f);
3824 /* These will be the left and right margins of the rectangles. */
3825 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3826 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3827 int width = flash_right - flash_left;
3829 /* If window is tall, flash top and bottom line. */
3830 if (height > 3 * FRAME_LINE_HEIGHT (f))
3832 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3833 flash_left,
3834 (FRAME_INTERNAL_BORDER_WIDTH (f)
3835 + FRAME_TOP_MARGIN_HEIGHT (f)),
3836 width, flash_height);
3837 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3838 flash_left,
3839 (height - flash_height
3840 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3841 width, flash_height);
3844 else
3845 /* If it is short, flash it all. */
3846 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3847 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3848 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3850 x_flush (f);
3853 struct timespec delay = make_timespec (0, 150 * 1000 * 1000);
3854 struct timespec wakeup = timespec_add (current_timespec (), delay);
3856 /* Keep waiting until past the time wakeup or any input gets
3857 available. */
3858 while (! detect_input_pending ())
3860 struct timespec current = current_timespec ();
3861 struct timespec timeout;
3863 /* Break if result would not be positive. */
3864 if (timespec_cmp (wakeup, current) <= 0)
3865 break;
3867 /* How long `select' should wait. */
3868 timeout = make_timespec (0, 10 * 1000 * 1000);
3870 /* Try to wait that long--but we might wake up sooner. */
3871 pselect (0, NULL, NULL, NULL, &timeout, NULL);
3875 /* If window is tall, flash top and bottom line. */
3876 if (height > 3 * FRAME_LINE_HEIGHT (f))
3878 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3879 flash_left,
3880 (FRAME_INTERNAL_BORDER_WIDTH (f)
3881 + FRAME_TOP_MARGIN_HEIGHT (f)),
3882 width, flash_height);
3883 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3884 flash_left,
3885 (height - flash_height
3886 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3887 width, flash_height);
3889 else
3890 /* If it is short, flash it all. */
3891 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3892 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3893 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3895 #ifdef USE_GTK
3896 #ifdef HAVE_GTK3
3897 cairo_destroy (cr);
3898 #else
3899 g_object_unref (G_OBJECT (gc));
3900 #endif
3901 #undef XFillRectangle
3902 #else
3903 XFreeGC (FRAME_X_DISPLAY (f), gc);
3904 #endif
3905 x_flush (f);
3909 unblock_input ();
3913 static void
3914 XTtoggle_invisible_pointer (struct frame *f, bool invisible)
3916 block_input ();
3917 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, invisible);
3918 unblock_input ();
3922 /* Make audible bell. */
3924 static void
3925 XTring_bell (struct frame *f)
3927 if (FRAME_X_DISPLAY (f))
3929 if (visible_bell)
3930 XTflash (f);
3931 else
3933 block_input ();
3934 #ifdef HAVE_XKB
3935 XkbBell (FRAME_X_DISPLAY (f), None, 0, None);
3936 #else
3937 XBell (FRAME_X_DISPLAY (f), 0);
3938 #endif
3939 XFlush (FRAME_X_DISPLAY (f));
3940 unblock_input ();
3945 /***********************************************************************
3946 Line Dance
3947 ***********************************************************************/
3949 /* Perform an insert-lines or delete-lines operation, inserting N
3950 lines or deleting -N lines at vertical position VPOS. */
3952 static void
3953 x_ins_del_lines (struct frame *f, int vpos, int n)
3955 emacs_abort ();
3959 /* Scroll part of the display as described by RUN. */
3961 static void
3962 x_scroll_run (struct window *w, struct run *run)
3964 struct frame *f = XFRAME (w->frame);
3965 int x, y, width, height, from_y, to_y, bottom_y;
3967 /* Get frame-relative bounding box of the text display area of W,
3968 without mode lines. Include in this box the left and right
3969 fringe of W. */
3970 window_box (w, ANY_AREA, &x, &y, &width, &height);
3972 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3973 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3974 bottom_y = y + height;
3976 if (to_y < from_y)
3978 /* Scrolling up. Make sure we don't copy part of the mode
3979 line at the bottom. */
3980 if (from_y + run->height > bottom_y)
3981 height = bottom_y - from_y;
3982 else
3983 height = run->height;
3985 else
3987 /* Scrolling down. Make sure we don't copy over the mode line.
3988 at the bottom. */
3989 if (to_y + run->height > bottom_y)
3990 height = bottom_y - to_y;
3991 else
3992 height = run->height;
3995 block_input ();
3997 /* Cursor off. Will be switched on again in x_update_window_end. */
3998 x_clear_cursor (w);
4000 #ifdef USE_CAIRO
4001 SET_FRAME_GARBAGED (f);
4002 #else
4003 XCopyArea (FRAME_X_DISPLAY (f),
4004 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4005 f->output_data.x->normal_gc,
4006 x, from_y,
4007 width, height,
4008 x, to_y);
4009 #endif
4011 unblock_input ();
4016 /***********************************************************************
4017 Exposure Events
4018 ***********************************************************************/
4021 static void
4022 frame_highlight (struct frame *f)
4024 /* We used to only do this if Vx_no_window_manager was non-nil, but
4025 the ICCCM (section 4.1.6) says that the window's border pixmap
4026 and border pixel are window attributes which are "private to the
4027 client", so we can always change it to whatever we want. */
4028 block_input ();
4029 /* I recently started to get errors in this XSetWindowBorder, depending on
4030 the window-manager in use, tho something more is at play since I've been
4031 using that same window-manager binary for ever. Let's not crash just
4032 because of this (bug#9310). */
4033 x_catch_errors (FRAME_X_DISPLAY (f));
4034 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4035 f->output_data.x->border_pixel);
4036 x_uncatch_errors ();
4037 unblock_input ();
4038 x_update_cursor (f, true);
4039 x_set_frame_alpha (f);
4042 static void
4043 frame_unhighlight (struct frame *f)
4045 /* We used to only do this if Vx_no_window_manager was non-nil, but
4046 the ICCCM (section 4.1.6) says that the window's border pixmap
4047 and border pixel are window attributes which are "private to the
4048 client", so we can always change it to whatever we want. */
4049 block_input ();
4050 /* Same as above for XSetWindowBorder (bug#9310). */
4051 x_catch_errors (FRAME_X_DISPLAY (f));
4052 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4053 f->output_data.x->border_tile);
4054 x_uncatch_errors ();
4055 unblock_input ();
4056 x_update_cursor (f, true);
4057 x_set_frame_alpha (f);
4060 /* The focus has changed. Update the frames as necessary to reflect
4061 the new situation. Note that we can't change the selected frame
4062 here, because the Lisp code we are interrupting might become confused.
4063 Each event gets marked with the frame in which it occurred, so the
4064 Lisp code can tell when the switch took place by examining the events. */
4066 static void
4067 x_new_focus_frame (struct x_display_info *dpyinfo, struct frame *frame)
4069 struct frame *old_focus = dpyinfo->x_focus_frame;
4071 if (frame != dpyinfo->x_focus_frame)
4073 /* Set this before calling other routines, so that they see
4074 the correct value of x_focus_frame. */
4075 dpyinfo->x_focus_frame = frame;
4077 if (old_focus && old_focus->auto_lower)
4078 x_lower_frame (old_focus);
4080 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
4081 dpyinfo->x_pending_autoraise_frame = dpyinfo->x_focus_frame;
4082 else
4083 dpyinfo->x_pending_autoraise_frame = NULL;
4086 x_frame_rehighlight (dpyinfo);
4089 /* Handle FocusIn and FocusOut state changes for FRAME.
4090 If FRAME has focus and there exists more than one frame, puts
4091 a FOCUS_IN_EVENT into *BUFP. */
4093 static void
4094 x_focus_changed (int type, int state, struct x_display_info *dpyinfo, struct frame *frame, struct input_event *bufp)
4096 if (type == FocusIn)
4098 if (dpyinfo->x_focus_event_frame != frame)
4100 x_new_focus_frame (dpyinfo, frame);
4101 dpyinfo->x_focus_event_frame = frame;
4103 /* Don't stop displaying the initial startup message
4104 for a switch-frame event we don't need. */
4105 /* When run as a daemon, Vterminal_frame is always NIL. */
4106 bufp->arg = (((NILP (Vterminal_frame)
4107 || ! FRAME_X_P (XFRAME (Vterminal_frame))
4108 || EQ (Fdaemonp (), Qt))
4109 && CONSP (Vframe_list)
4110 && !NILP (XCDR (Vframe_list)))
4111 ? Qt : Qnil);
4112 bufp->kind = FOCUS_IN_EVENT;
4113 XSETFRAME (bufp->frame_or_window, frame);
4116 frame->output_data.x->focus_state |= state;
4118 #ifdef HAVE_X_I18N
4119 if (FRAME_XIC (frame))
4120 XSetICFocus (FRAME_XIC (frame));
4121 #endif
4123 else if (type == FocusOut)
4125 frame->output_data.x->focus_state &= ~state;
4127 if (dpyinfo->x_focus_event_frame == frame)
4129 dpyinfo->x_focus_event_frame = 0;
4130 x_new_focus_frame (dpyinfo, 0);
4132 bufp->kind = FOCUS_OUT_EVENT;
4133 XSETFRAME (bufp->frame_or_window, frame);
4136 #ifdef HAVE_X_I18N
4137 if (FRAME_XIC (frame))
4138 XUnsetICFocus (FRAME_XIC (frame));
4139 #endif
4140 if (frame->pointer_invisible)
4141 XTtoggle_invisible_pointer (frame, false);
4145 /* Return the Emacs frame-object corresponding to an X window.
4146 It could be the frame's main window or an icon window. */
4148 static struct frame *
4149 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4151 Lisp_Object tail, frame;
4152 struct frame *f;
4154 if (wdesc == None)
4155 return NULL;
4157 FOR_EACH_FRAME (tail, frame)
4159 f = XFRAME (frame);
4160 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4161 continue;
4162 if (f->output_data.x->hourglass_window == wdesc)
4163 return f;
4164 #ifdef USE_X_TOOLKIT
4165 if ((f->output_data.x->edit_widget
4166 && XtWindow (f->output_data.x->edit_widget) == wdesc)
4167 /* A tooltip frame? */
4168 || (!f->output_data.x->edit_widget
4169 && FRAME_X_WINDOW (f) == wdesc)
4170 || f->output_data.x->icon_desc == wdesc)
4171 return f;
4172 #else /* not USE_X_TOOLKIT */
4173 #ifdef USE_GTK
4174 if (f->output_data.x->edit_widget)
4176 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4177 struct x_output *x = f->output_data.x;
4178 if (gwdesc != 0 && gwdesc == x->edit_widget)
4179 return f;
4181 #endif /* USE_GTK */
4182 if (FRAME_X_WINDOW (f) == wdesc
4183 || f->output_data.x->icon_desc == wdesc)
4184 return f;
4185 #endif /* not USE_X_TOOLKIT */
4187 return 0;
4190 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
4192 /* Like x_window_to_frame but also compares the window with the widget's
4193 windows. */
4195 static struct frame *
4196 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4198 Lisp_Object tail, frame;
4199 struct frame *f, *found = NULL;
4200 struct x_output *x;
4202 if (wdesc == None)
4203 return NULL;
4205 FOR_EACH_FRAME (tail, frame)
4207 if (found)
4208 break;
4209 f = XFRAME (frame);
4210 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
4212 /* This frame matches if the window is any of its widgets. */
4213 x = f->output_data.x;
4214 if (x->hourglass_window == wdesc)
4215 found = f;
4216 else if (x->widget)
4218 #ifdef USE_GTK
4219 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4220 if (gwdesc != 0
4221 && gtk_widget_get_toplevel (gwdesc) == x->widget)
4222 found = f;
4223 #else
4224 if (wdesc == XtWindow (x->widget)
4225 || wdesc == XtWindow (x->column_widget)
4226 || wdesc == XtWindow (x->edit_widget))
4227 found = f;
4228 /* Match if the window is this frame's menubar. */
4229 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
4230 found = f;
4231 #endif
4233 else if (FRAME_X_WINDOW (f) == wdesc)
4234 /* A tooltip frame. */
4235 found = f;
4239 return found;
4242 /* Likewise, but consider only the menu bar widget. */
4244 static struct frame *
4245 x_menubar_window_to_frame (struct x_display_info *dpyinfo,
4246 const XEvent *event)
4248 Window wdesc = event->xany.window;
4249 Lisp_Object tail, frame;
4250 struct frame *f;
4251 struct x_output *x;
4253 if (wdesc == None)
4254 return NULL;
4256 FOR_EACH_FRAME (tail, frame)
4258 f = XFRAME (frame);
4259 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4260 continue;
4261 x = f->output_data.x;
4262 #ifdef USE_GTK
4263 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
4264 return f;
4265 #else
4266 /* Match if the window is this frame's menubar. */
4267 if (x->menubar_widget
4268 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
4269 return f;
4270 #endif
4272 return 0;
4275 /* Return the frame whose principal (outermost) window is WDESC.
4276 If WDESC is some other (smaller) window, we return 0. */
4278 struct frame *
4279 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
4281 Lisp_Object tail, frame;
4282 struct frame *f;
4283 struct x_output *x;
4285 if (wdesc == None)
4286 return NULL;
4288 FOR_EACH_FRAME (tail, frame)
4290 f = XFRAME (frame);
4291 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
4292 continue;
4293 x = f->output_data.x;
4295 if (x->widget)
4297 /* This frame matches if the window is its topmost widget. */
4298 #ifdef USE_GTK
4299 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
4300 if (gwdesc == x->widget)
4301 return f;
4302 #else
4303 if (wdesc == XtWindow (x->widget))
4304 return f;
4305 #endif
4307 else if (FRAME_X_WINDOW (f) == wdesc)
4308 /* Tooltip frame. */
4309 return f;
4311 return 0;
4314 #else /* !USE_X_TOOLKIT && !USE_GTK */
4316 #define x_any_window_to_frame(d, i) x_window_to_frame (d, i)
4317 #define x_top_window_to_frame(d, i) x_window_to_frame (d, i)
4319 #endif /* USE_X_TOOLKIT || USE_GTK */
4321 /* The focus may have changed. Figure out if it is a real focus change,
4322 by checking both FocusIn/Out and Enter/LeaveNotify events.
4324 Returns FOCUS_IN_EVENT event in *BUFP. */
4326 static void
4327 x_detect_focus_change (struct x_display_info *dpyinfo, struct frame *frame,
4328 const XEvent *event, struct input_event *bufp)
4330 if (!frame)
4331 return;
4333 switch (event->type)
4335 case EnterNotify:
4336 case LeaveNotify:
4338 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
4339 int focus_state
4340 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
4342 if (event->xcrossing.detail != NotifyInferior
4343 && event->xcrossing.focus
4344 && ! (focus_state & FOCUS_EXPLICIT))
4345 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
4346 FOCUS_IMPLICIT,
4347 dpyinfo, frame, bufp);
4349 break;
4351 case FocusIn:
4352 case FocusOut:
4353 x_focus_changed (event->type,
4354 (event->xfocus.detail == NotifyPointer ?
4355 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
4356 dpyinfo, frame, bufp);
4357 break;
4359 case ClientMessage:
4360 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
4362 enum xembed_message msg = event->xclient.data.l[1];
4363 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
4364 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
4366 break;
4371 #if !defined USE_X_TOOLKIT && !defined USE_GTK
4372 /* Handle an event saying the mouse has moved out of an Emacs frame. */
4374 void
4375 x_mouse_leave (struct x_display_info *dpyinfo)
4377 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
4379 #endif
4381 /* The focus has changed, or we have redirected a frame's focus to
4382 another frame (this happens when a frame uses a surrogate
4383 mini-buffer frame). Shift the highlight as appropriate.
4385 The FRAME argument doesn't necessarily have anything to do with which
4386 frame is being highlighted or un-highlighted; we only use it to find
4387 the appropriate X display info. */
4389 static void
4390 XTframe_rehighlight (struct frame *frame)
4392 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
4395 static void
4396 x_frame_rehighlight (struct x_display_info *dpyinfo)
4398 struct frame *old_highlight = dpyinfo->x_highlight_frame;
4400 if (dpyinfo->x_focus_frame)
4402 dpyinfo->x_highlight_frame
4403 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
4404 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
4405 : dpyinfo->x_focus_frame);
4406 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
4408 fset_focus_frame (dpyinfo->x_focus_frame, Qnil);
4409 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
4412 else
4413 dpyinfo->x_highlight_frame = 0;
4415 if (dpyinfo->x_highlight_frame != old_highlight)
4417 if (old_highlight)
4418 frame_unhighlight (old_highlight);
4419 if (dpyinfo->x_highlight_frame)
4420 frame_highlight (dpyinfo->x_highlight_frame);
4426 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
4428 /* Initialize mode_switch_bit and modifier_meaning. */
4429 static void
4430 x_find_modifier_meanings (struct x_display_info *dpyinfo)
4432 int min_code, max_code;
4433 KeySym *syms;
4434 int syms_per_code;
4435 XModifierKeymap *mods;
4437 dpyinfo->meta_mod_mask = 0;
4438 dpyinfo->shift_lock_mask = 0;
4439 dpyinfo->alt_mod_mask = 0;
4440 dpyinfo->super_mod_mask = 0;
4441 dpyinfo->hyper_mod_mask = 0;
4443 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
4445 syms = XGetKeyboardMapping (dpyinfo->display,
4446 min_code, max_code - min_code + 1,
4447 &syms_per_code);
4448 mods = XGetModifierMapping (dpyinfo->display);
4450 /* Scan the modifier table to see which modifier bits the Meta and
4451 Alt keysyms are on. */
4453 int row, col; /* The row and column in the modifier table. */
4454 bool found_alt_or_meta;
4456 for (row = 3; row < 8; row++)
4458 found_alt_or_meta = false;
4459 for (col = 0; col < mods->max_keypermod; col++)
4461 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
4463 /* Zeroes are used for filler. Skip them. */
4464 if (code == 0)
4465 continue;
4467 /* Are any of this keycode's keysyms a meta key? */
4469 int code_col;
4471 for (code_col = 0; code_col < syms_per_code; code_col++)
4473 int sym = syms[((code - min_code) * syms_per_code) + code_col];
4475 switch (sym)
4477 case XK_Meta_L:
4478 case XK_Meta_R:
4479 found_alt_or_meta = true;
4480 dpyinfo->meta_mod_mask |= (1 << row);
4481 break;
4483 case XK_Alt_L:
4484 case XK_Alt_R:
4485 found_alt_or_meta = true;
4486 dpyinfo->alt_mod_mask |= (1 << row);
4487 break;
4489 case XK_Hyper_L:
4490 case XK_Hyper_R:
4491 if (!found_alt_or_meta)
4492 dpyinfo->hyper_mod_mask |= (1 << row);
4493 code_col = syms_per_code;
4494 col = mods->max_keypermod;
4495 break;
4497 case XK_Super_L:
4498 case XK_Super_R:
4499 if (!found_alt_or_meta)
4500 dpyinfo->super_mod_mask |= (1 << row);
4501 code_col = syms_per_code;
4502 col = mods->max_keypermod;
4503 break;
4505 case XK_Shift_Lock:
4506 /* Ignore this if it's not on the lock modifier. */
4507 if (!found_alt_or_meta && ((1 << row) == LockMask))
4508 dpyinfo->shift_lock_mask = LockMask;
4509 code_col = syms_per_code;
4510 col = mods->max_keypermod;
4511 break;
4519 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
4520 if (! dpyinfo->meta_mod_mask)
4522 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
4523 dpyinfo->alt_mod_mask = 0;
4526 /* If some keys are both alt and meta,
4527 make them just meta, not alt. */
4528 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
4530 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
4533 XFree (syms);
4534 XFreeModifiermap (mods);
4537 /* Convert between the modifier bits X uses and the modifier bits
4538 Emacs uses. */
4541 x_x_to_emacs_modifiers (struct x_display_info *dpyinfo, int state)
4543 int mod_meta = meta_modifier;
4544 int mod_alt = alt_modifier;
4545 int mod_hyper = hyper_modifier;
4546 int mod_super = super_modifier;
4547 Lisp_Object tem;
4549 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4550 if (INTEGERP (tem)) mod_alt = XINT (tem) & INT_MAX;
4551 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4552 if (INTEGERP (tem)) mod_meta = XINT (tem) & INT_MAX;
4553 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4554 if (INTEGERP (tem)) mod_hyper = XINT (tem) & INT_MAX;
4555 tem = Fget (Vx_super_keysym, Qmodifier_value);
4556 if (INTEGERP (tem)) mod_super = XINT (tem) & INT_MAX;
4558 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
4559 | ((state & ControlMask) ? ctrl_modifier : 0)
4560 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
4561 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
4562 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
4563 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
4566 static int
4567 x_emacs_to_x_modifiers (struct x_display_info *dpyinfo, EMACS_INT state)
4569 EMACS_INT mod_meta = meta_modifier;
4570 EMACS_INT mod_alt = alt_modifier;
4571 EMACS_INT mod_hyper = hyper_modifier;
4572 EMACS_INT mod_super = super_modifier;
4574 Lisp_Object tem;
4576 tem = Fget (Vx_alt_keysym, Qmodifier_value);
4577 if (INTEGERP (tem)) mod_alt = XINT (tem);
4578 tem = Fget (Vx_meta_keysym, Qmodifier_value);
4579 if (INTEGERP (tem)) mod_meta = XINT (tem);
4580 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
4581 if (INTEGERP (tem)) mod_hyper = XINT (tem);
4582 tem = Fget (Vx_super_keysym, Qmodifier_value);
4583 if (INTEGERP (tem)) mod_super = XINT (tem);
4586 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
4587 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
4588 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
4589 | ((state & shift_modifier) ? ShiftMask : 0)
4590 | ((state & ctrl_modifier) ? ControlMask : 0)
4591 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
4594 /* Convert a keysym to its name. */
4596 char *
4597 x_get_keysym_name (int keysym)
4599 char *value;
4601 block_input ();
4602 value = XKeysymToString (keysym);
4603 unblock_input ();
4605 return value;
4608 /* Mouse clicks and mouse movement. Rah.
4610 Formerly, we used PointerMotionHintMask (in standard_event_mask)
4611 so that we would have to call XQueryPointer after each MotionNotify
4612 event to ask for another such event. However, this made mouse tracking
4613 slow, and there was a bug that made it eventually stop.
4615 Simply asking for MotionNotify all the time seems to work better.
4617 In order to avoid asking for motion events and then throwing most
4618 of them away or busy-polling the server for mouse positions, we ask
4619 the server for pointer motion hints. This means that we get only
4620 one event per group of mouse movements. "Groups" are delimited by
4621 other kinds of events (focus changes and button clicks, for
4622 example), or by XQueryPointer calls; when one of these happens, we
4623 get another MotionNotify event the next time the mouse moves. This
4624 is at least as efficient as getting motion events when mouse
4625 tracking is on, and I suspect only negligibly worse when tracking
4626 is off. */
4628 /* Prepare a mouse-event in *RESULT for placement in the input queue.
4630 If the event is a button press, then note that we have grabbed
4631 the mouse. */
4633 static Lisp_Object
4634 construct_mouse_click (struct input_event *result,
4635 const XButtonEvent *event,
4636 struct frame *f)
4638 /* Make the event type NO_EVENT; we'll change that when we decide
4639 otherwise. */
4640 result->kind = MOUSE_CLICK_EVENT;
4641 result->code = event->button - Button1;
4642 result->timestamp = event->time;
4643 result->modifiers = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f),
4644 event->state)
4645 | (event->type == ButtonRelease
4646 ? up_modifier
4647 : down_modifier));
4649 XSETINT (result->x, event->x);
4650 XSETINT (result->y, event->y);
4651 XSETFRAME (result->frame_or_window, f);
4652 result->arg = Qnil;
4653 return Qnil;
4656 /* Function to report a mouse movement to the mainstream Emacs code.
4657 The input handler calls this.
4659 We have received a mouse movement event, which is given in *event.
4660 If the mouse is over a different glyph than it was last time, tell
4661 the mainstream emacs code by setting mouse_moved. If not, ask for
4662 another motion event, so we can check again the next time it moves. */
4664 static bool
4665 note_mouse_movement (struct frame *frame, const XMotionEvent *event)
4667 XRectangle *r;
4668 struct x_display_info *dpyinfo;
4670 if (!FRAME_X_OUTPUT (frame))
4671 return false;
4673 dpyinfo = FRAME_DISPLAY_INFO (frame);
4674 dpyinfo->last_mouse_movement_time = event->time;
4675 dpyinfo->last_mouse_motion_frame = frame;
4676 dpyinfo->last_mouse_motion_x = event->x;
4677 dpyinfo->last_mouse_motion_y = event->y;
4679 if (event->window != FRAME_X_WINDOW (frame))
4681 frame->mouse_moved = true;
4682 dpyinfo->last_mouse_scroll_bar = NULL;
4683 note_mouse_highlight (frame, -1, -1);
4684 dpyinfo->last_mouse_glyph_frame = NULL;
4685 return true;
4689 /* Has the mouse moved off the glyph it was on at the last sighting? */
4690 r = &dpyinfo->last_mouse_glyph;
4691 if (frame != dpyinfo->last_mouse_glyph_frame
4692 || event->x < r->x || event->x >= r->x + r->width
4693 || event->y < r->y || event->y >= r->y + r->height)
4695 frame->mouse_moved = true;
4696 dpyinfo->last_mouse_scroll_bar = NULL;
4697 note_mouse_highlight (frame, event->x, event->y);
4698 /* Remember which glyph we're now on. */
4699 remember_mouse_glyph (frame, event->x, event->y, r);
4700 dpyinfo->last_mouse_glyph_frame = frame;
4701 return true;
4704 return false;
4707 /* Return the current position of the mouse.
4708 *FP should be a frame which indicates which display to ask about.
4710 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
4711 and *PART to the frame, window, and scroll bar part that the mouse
4712 is over. Set *X and *Y to the portion and whole of the mouse's
4713 position on the scroll bar.
4715 If the mouse movement started elsewhere, set *FP to the frame the
4716 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
4717 the mouse is over.
4719 Set *TIMESTAMP to the server time-stamp for the time at which the mouse
4720 was at this position.
4722 Don't store anything if we don't have a valid set of values to report.
4724 This clears the mouse_moved flag, so we can wait for the next mouse
4725 movement. */
4727 static void
4728 XTmouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
4729 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
4730 Time *timestamp)
4732 struct frame *f1;
4733 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
4735 block_input ();
4737 if (dpyinfo->last_mouse_scroll_bar && insist == 0)
4739 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
4741 if (bar->horizontal)
4742 x_horizontal_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
4743 else
4744 x_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
4746 else
4748 Window root;
4749 int root_x, root_y;
4751 Window dummy_window;
4752 int dummy;
4754 Lisp_Object frame, tail;
4756 /* Clear the mouse-moved flag for every frame on this display. */
4757 FOR_EACH_FRAME (tail, frame)
4758 if (FRAME_X_P (XFRAME (frame))
4759 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
4760 XFRAME (frame)->mouse_moved = false;
4762 dpyinfo->last_mouse_scroll_bar = NULL;
4764 /* Figure out which root window we're on. */
4765 XQueryPointer (FRAME_X_DISPLAY (*fp),
4766 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
4768 /* The root window which contains the pointer. */
4769 &root,
4771 /* Trash which we can't trust if the pointer is on
4772 a different screen. */
4773 &dummy_window,
4775 /* The position on that root window. */
4776 &root_x, &root_y,
4778 /* More trash we can't trust. */
4779 &dummy, &dummy,
4781 /* Modifier keys and pointer buttons, about which
4782 we don't care. */
4783 (unsigned int *) &dummy);
4785 /* Now we have a position on the root; find the innermost window
4786 containing the pointer. */
4788 Window win, child;
4789 int win_x, win_y;
4790 int parent_x = 0, parent_y = 0;
4792 win = root;
4794 /* XTranslateCoordinates can get errors if the window
4795 structure is changing at the same time this function
4796 is running. So at least we must not crash from them. */
4798 x_catch_errors (FRAME_X_DISPLAY (*fp));
4800 if (x_mouse_grabbed (dpyinfo))
4802 /* If mouse was grabbed on a frame, give coords for that frame
4803 even if the mouse is now outside it. */
4804 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
4806 /* From-window. */
4807 root,
4809 /* To-window. */
4810 FRAME_X_WINDOW (dpyinfo->last_mouse_frame),
4812 /* From-position, to-position. */
4813 root_x, root_y, &win_x, &win_y,
4815 /* Child of win. */
4816 &child);
4817 f1 = dpyinfo->last_mouse_frame;
4819 else
4821 while (true)
4823 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
4825 /* From-window, to-window. */
4826 root, win,
4828 /* From-position, to-position. */
4829 root_x, root_y, &win_x, &win_y,
4831 /* Child of win. */
4832 &child);
4834 if (child == None || child == win)
4835 break;
4836 #ifdef USE_GTK
4837 /* We don't wan't to know the innermost window. We
4838 want the edit window. For non-Gtk+ the innermost
4839 window is the edit window. For Gtk+ it might not
4840 be. It might be the tool bar for example. */
4841 if (x_window_to_frame (dpyinfo, win))
4842 break;
4843 #endif
4844 win = child;
4845 parent_x = win_x;
4846 parent_y = win_y;
4849 /* Now we know that:
4850 win is the innermost window containing the pointer
4851 (XTC says it has no child containing the pointer),
4852 win_x and win_y are the pointer's position in it
4853 (XTC did this the last time through), and
4854 parent_x and parent_y are the pointer's position in win's parent.
4855 (They are what win_x and win_y were when win was child.
4856 If win is the root window, it has no parent, and
4857 parent_{x,y} are invalid, but that's okay, because we'll
4858 never use them in that case.) */
4860 #ifdef USE_GTK
4861 /* We don't wan't to know the innermost window. We
4862 want the edit window. */
4863 f1 = x_window_to_frame (dpyinfo, win);
4864 #else
4865 /* Is win one of our frames? */
4866 f1 = x_any_window_to_frame (dpyinfo, win);
4867 #endif
4869 #ifdef USE_X_TOOLKIT
4870 /* If we end up with the menu bar window, say it's not
4871 on the frame. */
4872 if (f1 != NULL
4873 && f1->output_data.x->menubar_widget
4874 && win == XtWindow (f1->output_data.x->menubar_widget))
4875 f1 = NULL;
4876 #endif /* USE_X_TOOLKIT */
4879 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
4880 f1 = 0;
4882 x_uncatch_errors ();
4884 /* If not, is it one of our scroll bars? */
4885 if (! f1)
4887 struct scroll_bar *bar;
4889 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win, 2);
4891 if (bar)
4893 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4894 win_x = parent_x;
4895 win_y = parent_y;
4899 if (f1 == 0 && insist > 0)
4900 f1 = SELECTED_FRAME ();
4902 if (f1)
4904 /* Ok, we found a frame. Store all the values.
4905 last_mouse_glyph is a rectangle used to reduce the
4906 generation of mouse events. To not miss any motion
4907 events, we must divide the frame into rectangles of the
4908 size of the smallest character that could be displayed
4909 on it, i.e. into the same rectangles that matrices on
4910 the frame are divided into. */
4912 /* FIXME: what if F1 is not an X frame? */
4913 dpyinfo = FRAME_DISPLAY_INFO (f1);
4914 remember_mouse_glyph (f1, win_x, win_y, &dpyinfo->last_mouse_glyph);
4915 dpyinfo->last_mouse_glyph_frame = f1;
4917 *bar_window = Qnil;
4918 *part = 0;
4919 *fp = f1;
4920 XSETINT (*x, win_x);
4921 XSETINT (*y, win_y);
4922 *timestamp = dpyinfo->last_mouse_movement_time;
4927 unblock_input ();
4932 /***********************************************************************
4933 Scroll bars
4934 ***********************************************************************/
4936 /* Scroll bar support. */
4938 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
4939 manages it.
4940 This can be called in GC, so we have to make sure to strip off mark
4941 bits. */
4943 static struct scroll_bar *
4944 x_window_to_scroll_bar (Display *display, Window window_id, int type)
4946 Lisp_Object tail, frame;
4948 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4949 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4950 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4952 FOR_EACH_FRAME (tail, frame)
4954 Lisp_Object bar, condemned;
4956 if (! FRAME_X_P (XFRAME (frame)))
4957 continue;
4959 /* Scan this frame's scroll bar list for a scroll bar with the
4960 right window ID. */
4961 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4962 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4963 /* This trick allows us to search both the ordinary and
4964 condemned scroll bar lists with one loop. */
4965 ! NILP (bar) || (bar = condemned,
4966 condemned = Qnil,
4967 ! NILP (bar));
4968 bar = XSCROLL_BAR (bar)->next)
4969 if (XSCROLL_BAR (bar)->x_window == window_id
4970 && FRAME_X_DISPLAY (XFRAME (frame)) == display
4971 && (type = 2
4972 || (type == 1 && XSCROLL_BAR (bar)->horizontal)
4973 || (type == 0 && !XSCROLL_BAR (bar)->horizontal)))
4974 return XSCROLL_BAR (bar);
4977 return NULL;
4981 #if defined USE_LUCID
4983 /* Return the Lucid menu bar WINDOW is part of. Return null
4984 if WINDOW is not part of a menu bar. */
4986 static Widget
4987 x_window_to_menu_bar (Window window)
4989 Lisp_Object tail, frame;
4991 FOR_EACH_FRAME (tail, frame)
4992 if (FRAME_X_P (XFRAME (frame)))
4994 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4996 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4997 return menu_bar;
4999 return NULL;
5002 #endif /* USE_LUCID */
5005 /************************************************************************
5006 Toolkit scroll bars
5007 ************************************************************************/
5009 #ifdef USE_TOOLKIT_SCROLL_BARS
5011 static void x_send_scroll_bar_event (Lisp_Object, enum scroll_bar_part,
5012 int, int, bool);
5014 /* Lisp window being scrolled. Set when starting to interact with
5015 a toolkit scroll bar, reset to nil when ending the interaction. */
5017 static Lisp_Object window_being_scrolled;
5019 /* Whether this is an Xaw with arrow-scrollbars. This should imply
5020 that movements of 1/20 of the screen size are mapped to up/down. */
5022 #ifndef USE_GTK
5023 /* Id of action hook installed for scroll bars. */
5025 static XtActionHookId action_hook_id;
5026 static XtActionHookId horizontal_action_hook_id;
5028 static Boolean xaw3d_arrow_scroll;
5030 /* Whether the drag scrolling maintains the mouse at the top of the
5031 thumb. If not, resizing the thumb needs to be done more carefully
5032 to avoid jerkiness. */
5034 static Boolean xaw3d_pick_top;
5036 /* Action hook installed via XtAppAddActionHook when toolkit scroll
5037 bars are used.. The hook is responsible for detecting when
5038 the user ends an interaction with the scroll bar, and generates
5039 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
5041 static void
5042 xt_action_hook (Widget widget, XtPointer client_data, String action_name,
5043 XEvent *event, String *params, Cardinal *num_params)
5045 bool scroll_bar_p;
5046 const char *end_action;
5048 #ifdef USE_MOTIF
5049 scroll_bar_p = XmIsScrollBar (widget);
5050 end_action = "Release";
5051 #else /* !USE_MOTIF i.e. use Xaw */
5052 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
5053 end_action = "EndScroll";
5054 #endif /* USE_MOTIF */
5056 if (scroll_bar_p
5057 && strcmp (action_name, end_action) == 0
5058 && WINDOWP (window_being_scrolled))
5060 struct window *w;
5061 struct scroll_bar *bar;
5063 x_send_scroll_bar_event (window_being_scrolled,
5064 scroll_bar_end_scroll, 0, 0, false);
5065 w = XWINDOW (window_being_scrolled);
5066 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5068 if (bar->dragging != -1)
5070 bar->dragging = -1;
5071 /* The thumb size is incorrect while dragging: fix it. */
5072 set_vertical_scroll_bar (w);
5074 window_being_scrolled = Qnil;
5075 #if defined (USE_LUCID)
5076 bar->last_seen_part = scroll_bar_nowhere;
5077 #endif
5078 /* Xt timeouts no longer needed. */
5079 toolkit_scroll_bar_interaction = false;
5084 static void
5085 xt_horizontal_action_hook (Widget widget, XtPointer client_data, String action_name,
5086 XEvent *event, String *params, Cardinal *num_params)
5088 bool scroll_bar_p;
5089 const char *end_action;
5091 #ifdef USE_MOTIF
5092 scroll_bar_p = XmIsScrollBar (widget);
5093 end_action = "Release";
5094 #else /* !USE_MOTIF i.e. use Xaw */
5095 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
5096 end_action = "EndScroll";
5097 #endif /* USE_MOTIF */
5099 if (scroll_bar_p
5100 && strcmp (action_name, end_action) == 0
5101 && WINDOWP (window_being_scrolled))
5103 struct window *w;
5104 struct scroll_bar *bar;
5106 x_send_scroll_bar_event (window_being_scrolled,
5107 scroll_bar_end_scroll, 0, 0, true);
5108 w = XWINDOW (window_being_scrolled);
5109 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
5111 if (bar->dragging != -1)
5113 bar->dragging = -1;
5114 /* The thumb size is incorrect while dragging: fix it. */
5115 set_horizontal_scroll_bar (w);
5117 window_being_scrolled = Qnil;
5118 #if defined (USE_LUCID)
5119 bar->last_seen_part = scroll_bar_nowhere;
5120 #endif
5121 /* Xt timeouts no longer needed. */
5122 toolkit_scroll_bar_interaction = false;
5125 #endif /* not USE_GTK */
5127 /* Send a client message with message type Xatom_Scrollbar for a
5128 scroll action to the frame of WINDOW. PART is a value identifying
5129 the part of the scroll bar that was clicked on. PORTION is the
5130 amount to scroll of a whole of WHOLE. */
5132 static void
5133 x_send_scroll_bar_event (Lisp_Object window, enum scroll_bar_part part,
5134 int portion, int whole, bool horizontal)
5136 XEvent event;
5137 XClientMessageEvent *ev = &event.xclient;
5138 struct window *w = XWINDOW (window);
5139 struct frame *f = XFRAME (w->frame);
5140 intptr_t iw = (intptr_t) w;
5141 enum { BITS_PER_INTPTR = CHAR_BIT * sizeof iw };
5142 verify (BITS_PER_INTPTR <= 64);
5143 int sign_shift = BITS_PER_INTPTR - 32;
5145 block_input ();
5147 /* Construct a ClientMessage event to send to the frame. */
5148 ev->type = ClientMessage;
5149 ev->message_type = (horizontal
5150 ? FRAME_DISPLAY_INFO (f)->Xatom_Horizontal_Scrollbar
5151 : FRAME_DISPLAY_INFO (f)->Xatom_Scrollbar);
5152 ev->display = FRAME_X_DISPLAY (f);
5153 ev->window = FRAME_X_WINDOW (f);
5154 ev->format = 32;
5156 /* A 32-bit X client on a 64-bit X server can pass a window pointer
5157 as-is. A 64-bit client on a 32-bit X server is in trouble
5158 because a pointer does not fit and would be truncated while
5159 passing through the server. So use two slots and hope that X12
5160 will resolve such issues someday. */
5161 ev->data.l[0] = iw >> 31 >> 1;
5162 ev->data.l[1] = sign_shift <= 0 ? iw : iw << sign_shift >> sign_shift;
5163 ev->data.l[2] = part;
5164 ev->data.l[3] = portion;
5165 ev->data.l[4] = whole;
5167 /* Make Xt timeouts work while the scroll bar is active. */
5168 #ifdef USE_X_TOOLKIT
5169 toolkit_scroll_bar_interaction = true;
5170 x_activate_timeout_atimer ();
5171 #endif
5173 /* Setting the event mask to zero means that the message will
5174 be sent to the client that created the window, and if that
5175 window no longer exists, no event will be sent. */
5176 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
5177 unblock_input ();
5181 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
5182 in *IEVENT. */
5184 static void
5185 x_scroll_bar_to_input_event (const XEvent *event,
5186 struct input_event *ievent)
5188 const XClientMessageEvent *ev = &event->xclient;
5189 Lisp_Object window;
5190 struct window *w;
5192 /* See the comment in the function above. */
5193 intptr_t iw0 = ev->data.l[0];
5194 intptr_t iw1 = ev->data.l[1];
5195 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
5196 w = (struct window *) iw;
5198 XSETWINDOW (window, w);
5200 ievent->kind = SCROLL_BAR_CLICK_EVENT;
5201 ievent->frame_or_window = window;
5202 ievent->arg = Qnil;
5203 #ifdef USE_GTK
5204 ievent->timestamp = CurrentTime;
5205 #else
5206 ievent->timestamp =
5207 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
5208 #endif
5209 ievent->code = 0;
5210 ievent->part = ev->data.l[2];
5211 ievent->x = make_number (ev->data.l[3]);
5212 ievent->y = make_number (ev->data.l[4]);
5213 ievent->modifiers = 0;
5216 /* Transform a horizontal scroll bar ClientMessage EVENT to an Emacs
5217 input event in *IEVENT. */
5219 static void
5220 x_horizontal_scroll_bar_to_input_event (const XEvent *event,
5221 struct input_event *ievent)
5223 const XClientMessageEvent *ev = &event->xclient;
5224 Lisp_Object window;
5225 struct window *w;
5227 /* See the comment in the function above. */
5228 intptr_t iw0 = ev->data.l[0];
5229 intptr_t iw1 = ev->data.l[1];
5230 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
5231 w = (struct window *) iw;
5233 XSETWINDOW (window, w);
5235 ievent->kind = HORIZONTAL_SCROLL_BAR_CLICK_EVENT;
5236 ievent->frame_or_window = window;
5237 ievent->arg = Qnil;
5238 #ifdef USE_GTK
5239 ievent->timestamp = CurrentTime;
5240 #else
5241 ievent->timestamp =
5242 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
5243 #endif
5244 ievent->code = 0;
5245 ievent->part = ev->data.l[2];
5246 ievent->x = make_number (ev->data.l[3]);
5247 ievent->y = make_number (ev->data.l[4]);
5248 ievent->modifiers = 0;
5252 #ifdef USE_MOTIF
5254 /* Minimum and maximum values used for Motif scroll bars. */
5256 #define XM_SB_MAX 10000000
5258 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
5259 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
5260 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
5262 static void
5263 xm_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5265 struct scroll_bar *bar = client_data;
5266 XmScrollBarCallbackStruct *cs = call_data;
5267 enum scroll_bar_part part = scroll_bar_nowhere;
5268 bool horizontal = bar->horizontal;
5269 int whole = 0, portion = 0;
5271 switch (cs->reason)
5273 case XmCR_DECREMENT:
5274 bar->dragging = -1;
5275 part = horizontal ? scroll_bar_left_arrow : scroll_bar_up_arrow;
5276 break;
5278 case XmCR_INCREMENT:
5279 bar->dragging = -1;
5280 part = horizontal ? scroll_bar_right_arrow : scroll_bar_down_arrow;
5281 break;
5283 case XmCR_PAGE_DECREMENT:
5284 bar->dragging = -1;
5285 part = horizontal ? scroll_bar_before_handle : scroll_bar_above_handle;
5286 break;
5288 case XmCR_PAGE_INCREMENT:
5289 bar->dragging = -1;
5290 part = horizontal ? scroll_bar_after_handle : scroll_bar_below_handle;
5291 break;
5293 case XmCR_TO_TOP:
5294 bar->dragging = -1;
5295 part = horizontal ? scroll_bar_to_leftmost : scroll_bar_to_top;
5296 break;
5298 case XmCR_TO_BOTTOM:
5299 bar->dragging = -1;
5300 part = horizontal ? scroll_bar_to_rightmost : scroll_bar_to_bottom;
5301 break;
5303 case XmCR_DRAG:
5305 int slider_size;
5307 block_input ();
5308 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
5309 unblock_input ();
5311 if (horizontal)
5313 portion = bar->whole * ((float)cs->value / XM_SB_MAX);
5314 whole = bar->whole * ((float)(XM_SB_MAX - slider_size) / XM_SB_MAX);
5315 portion = min (portion, whole);
5316 part = scroll_bar_horizontal_handle;
5318 else
5320 whole = XM_SB_MAX - slider_size;
5321 portion = min (cs->value, whole);
5322 part = scroll_bar_handle;
5325 bar->dragging = cs->value;
5327 break;
5329 case XmCR_VALUE_CHANGED:
5330 break;
5333 if (part != scroll_bar_nowhere)
5335 window_being_scrolled = bar->window;
5336 x_send_scroll_bar_event (bar->window, part, portion, whole,
5337 bar->horizontal);
5341 #elif defined USE_GTK
5343 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
5344 bar widget. DATA is a pointer to the scroll_bar structure. */
5346 static gboolean
5347 xg_scroll_callback (GtkRange *range,
5348 GtkScrollType scroll,
5349 gdouble value,
5350 gpointer user_data)
5352 int whole = 0, portion = 0;
5353 struct scroll_bar *bar = user_data;
5354 enum scroll_bar_part part = scroll_bar_nowhere;
5355 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
5356 struct frame *f = g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
5358 if (xg_ignore_gtk_scrollbar) return false;
5360 switch (scroll)
5362 case GTK_SCROLL_JUMP:
5363 /* Buttons 1 2 or 3 must be grabbed. */
5364 if (FRAME_DISPLAY_INFO (f)->grabbed != 0
5365 && FRAME_DISPLAY_INFO (f)->grabbed < (1 << 4))
5367 if (bar->horizontal)
5369 part = scroll_bar_horizontal_handle;
5370 whole = (int)(gtk_adjustment_get_upper (adj) -
5371 gtk_adjustment_get_page_size (adj));
5372 portion = min ((int)value, whole);
5373 bar->dragging = portion;
5375 else
5377 part = scroll_bar_handle;
5378 whole = gtk_adjustment_get_upper (adj) -
5379 gtk_adjustment_get_page_size (adj);
5380 portion = min ((int)value, whole);
5381 bar->dragging = portion;
5384 break;
5385 case GTK_SCROLL_STEP_BACKWARD:
5386 part = (bar->horizontal
5387 ? scroll_bar_left_arrow : scroll_bar_up_arrow);
5388 bar->dragging = -1;
5389 break;
5390 case GTK_SCROLL_STEP_FORWARD:
5391 part = (bar->horizontal
5392 ? scroll_bar_right_arrow : scroll_bar_down_arrow);
5393 bar->dragging = -1;
5394 break;
5395 case GTK_SCROLL_PAGE_BACKWARD:
5396 part = (bar->horizontal
5397 ? scroll_bar_before_handle : scroll_bar_above_handle);
5398 bar->dragging = -1;
5399 break;
5400 case GTK_SCROLL_PAGE_FORWARD:
5401 part = (bar->horizontal
5402 ? scroll_bar_after_handle : scroll_bar_below_handle);
5403 bar->dragging = -1;
5404 break;
5407 if (part != scroll_bar_nowhere)
5409 window_being_scrolled = bar->window;
5410 x_send_scroll_bar_event (bar->window, part, portion, whole,
5411 bar->horizontal);
5414 return false;
5417 /* Callback for button release. Sets dragging to -1 when dragging is done. */
5419 static gboolean
5420 xg_end_scroll_callback (GtkWidget *widget,
5421 GdkEventButton *event,
5422 gpointer user_data)
5424 struct scroll_bar *bar = user_data;
5425 bar->dragging = -1;
5426 if (WINDOWP (window_being_scrolled))
5428 x_send_scroll_bar_event (window_being_scrolled,
5429 scroll_bar_end_scroll, 0, 0, bar->horizontal);
5430 window_being_scrolled = Qnil;
5433 return false;
5437 #else /* not USE_GTK and not USE_MOTIF */
5439 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
5440 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
5441 scroll bar struct. CALL_DATA is a pointer to a float saying where
5442 the thumb is. */
5444 static void
5445 xaw_jump_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5447 struct scroll_bar *bar = client_data;
5448 float *top_addr = call_data;
5449 float top = *top_addr;
5450 float shown;
5451 int whole, portion, height, width;
5452 enum scroll_bar_part part;
5453 bool horizontal = bar->horizontal;
5456 if (horizontal)
5458 /* Get the size of the thumb, a value between 0 and 1. */
5459 block_input ();
5460 XtVaGetValues (widget, XtNshown, &shown, XtNwidth, &width, NULL);
5461 unblock_input ();
5463 if (shown < 1)
5465 whole = bar->whole - (shown * bar->whole);
5466 portion = min (top * bar->whole, whole);
5468 else
5470 whole = bar->whole;
5471 portion = 0;
5474 part = scroll_bar_horizontal_handle;
5476 else
5478 /* Get the size of the thumb, a value between 0 and 1. */
5479 block_input ();
5480 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
5481 unblock_input ();
5483 whole = 10000000;
5484 portion = shown < 1 ? top * whole : 0;
5486 if (shown < 1 && (eabs (top + shown - 1) < 1.0f / height))
5487 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
5488 the bottom, so we force the scrolling whenever we see that we're
5489 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
5490 we try to ensure that we always stay two pixels away from the
5491 bottom). */
5492 part = scroll_bar_down_arrow;
5493 else
5494 part = scroll_bar_handle;
5497 window_being_scrolled = bar->window;
5498 bar->dragging = portion;
5499 bar->last_seen_part = part;
5500 x_send_scroll_bar_event (bar->window, part, portion, whole, bar->horizontal);
5504 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
5505 i.e. line or page up or down. WIDGET is the Xaw scroll bar
5506 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
5507 the scroll bar. CALL_DATA is an integer specifying the action that
5508 has taken place. Its magnitude is in the range 0..height of the
5509 scroll bar. Negative values mean scroll towards buffer start.
5510 Values < height of scroll bar mean line-wise movement. */
5512 static void
5513 xaw_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
5515 struct scroll_bar *bar = client_data;
5516 /* The position really is stored cast to a pointer. */
5517 int position = (intptr_t) call_data;
5518 Dimension height, width;
5519 enum scroll_bar_part part;
5521 if (bar->horizontal)
5523 /* Get the width of the scroll bar. */
5524 block_input ();
5525 XtVaGetValues (widget, XtNwidth, &width, NULL);
5526 unblock_input ();
5528 if (eabs (position) >= width)
5529 part = (position < 0) ? scroll_bar_before_handle : scroll_bar_after_handle;
5531 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
5532 it maps line-movement to call_data = max(5, height/20). */
5533 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, width / 20))
5534 part = (position < 0) ? scroll_bar_left_arrow : scroll_bar_right_arrow;
5535 else
5536 part = scroll_bar_move_ratio;
5538 window_being_scrolled = bar->window;
5539 bar->dragging = -1;
5540 bar->last_seen_part = part;
5541 x_send_scroll_bar_event (bar->window, part, position, width,
5542 bar->horizontal);
5544 else
5547 /* Get the height of the scroll bar. */
5548 block_input ();
5549 XtVaGetValues (widget, XtNheight, &height, NULL);
5550 unblock_input ();
5552 if (eabs (position) >= height)
5553 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
5555 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
5556 it maps line-movement to call_data = max(5, height/20). */
5557 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
5558 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
5559 else
5560 part = scroll_bar_move_ratio;
5562 window_being_scrolled = bar->window;
5563 bar->dragging = -1;
5564 bar->last_seen_part = part;
5565 x_send_scroll_bar_event (bar->window, part, position, height,
5566 bar->horizontal);
5570 #endif /* not USE_GTK and not USE_MOTIF */
5572 #define SCROLL_BAR_NAME "verticalScrollBar"
5573 #define SCROLL_BAR_HORIZONTAL_NAME "horizontalScrollBar"
5575 /* Create the widget for scroll bar BAR on frame F. Record the widget
5576 and X window of the scroll bar in BAR. */
5578 #ifdef USE_GTK
5579 static void
5580 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5582 const char *scroll_bar_name = SCROLL_BAR_NAME;
5584 block_input ();
5585 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
5586 G_CALLBACK (xg_end_scroll_callback),
5587 scroll_bar_name);
5588 unblock_input ();
5591 static void
5592 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5594 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
5596 block_input ();
5597 xg_create_horizontal_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
5598 G_CALLBACK (xg_end_scroll_callback),
5599 scroll_bar_name);
5600 unblock_input ();
5603 #else /* not USE_GTK */
5605 static void
5606 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5608 Window xwindow;
5609 Widget widget;
5610 Arg av[20];
5611 int ac = 0;
5612 const char *scroll_bar_name = SCROLL_BAR_NAME;
5613 unsigned long pixel;
5615 block_input ();
5617 #ifdef USE_MOTIF
5618 /* Set resources. Create the widget. */
5619 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5620 XtSetArg (av[ac], XmNminimum, 0); ++ac;
5621 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
5622 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
5623 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
5624 XtSetArg (av[ac], XmNincrement, 1); ++ac;
5625 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
5627 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5628 if (pixel != -1)
5630 XtSetArg (av[ac], XmNforeground, pixel);
5631 ++ac;
5634 pixel = f->output_data.x->scroll_bar_background_pixel;
5635 if (pixel != -1)
5637 XtSetArg (av[ac], XmNbackground, pixel);
5638 ++ac;
5641 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
5642 (char *) scroll_bar_name, av, ac);
5644 /* Add one callback for everything that can happen. */
5645 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
5646 (XtPointer) bar);
5647 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
5648 (XtPointer) bar);
5649 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
5650 (XtPointer) bar);
5651 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
5652 (XtPointer) bar);
5653 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
5654 (XtPointer) bar);
5655 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
5656 (XtPointer) bar);
5657 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
5658 (XtPointer) bar);
5660 /* Realize the widget. Only after that is the X window created. */
5661 XtRealizeWidget (widget);
5663 /* Set the cursor to an arrow. I didn't find a resource to do that.
5664 And I'm wondering why it hasn't an arrow cursor by default. */
5665 XDefineCursor (XtDisplay (widget), XtWindow (widget),
5666 f->output_data.x->nontext_cursor);
5668 #else /* !USE_MOTIF i.e. use Xaw */
5670 /* Set resources. Create the widget. The background of the
5671 Xaw3d scroll bar widget is a little bit light for my taste.
5672 We don't alter it here to let users change it according
5673 to their taste with `emacs*verticalScrollBar.background: xxx'. */
5674 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5675 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
5676 /* For smoother scrolling with Xaw3d -sm */
5677 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
5679 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5680 if (pixel != -1)
5682 XtSetArg (av[ac], XtNforeground, pixel);
5683 ++ac;
5686 pixel = f->output_data.x->scroll_bar_background_pixel;
5687 if (pixel != -1)
5689 XtSetArg (av[ac], XtNbackground, pixel);
5690 ++ac;
5693 /* Top/bottom shadow colors. */
5695 /* Allocate them, if necessary. */
5696 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
5698 pixel = f->output_data.x->scroll_bar_background_pixel;
5699 if (pixel != -1)
5701 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5702 FRAME_X_COLORMAP (f),
5703 &pixel, 1.2, 0x8000))
5704 pixel = -1;
5705 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
5708 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5710 pixel = f->output_data.x->scroll_bar_background_pixel;
5711 if (pixel != -1)
5713 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5714 FRAME_X_COLORMAP (f),
5715 &pixel, 0.6, 0x4000))
5716 pixel = -1;
5717 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
5721 #ifdef XtNbeNiceToColormap
5722 /* Tell the toolkit about them. */
5723 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
5724 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5725 /* We tried to allocate a color for the top/bottom shadow, and
5726 failed, so tell Xaw3d to use dithering instead. */
5727 /* But only if we have a small colormap. Xaw3d can allocate nice
5728 colors itself. */
5730 XtSetArg (av[ac], XtNbeNiceToColormap,
5731 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
5732 ++ac;
5734 else
5735 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
5736 be more consistent with other emacs 3d colors, and since Xaw3d is
5737 not good at dealing with allocation failure. */
5739 /* This tells Xaw3d to use real colors instead of dithering for
5740 the shadows. */
5741 XtSetArg (av[ac], XtNbeNiceToColormap, False);
5742 ++ac;
5744 /* Specify the colors. */
5745 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
5746 if (pixel != -1)
5748 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
5749 ++ac;
5751 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
5752 if (pixel != -1)
5754 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
5755 ++ac;
5758 #endif
5760 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
5761 f->output_data.x->edit_widget, av, ac);
5764 char const *initial = "";
5765 char const *val = initial;
5766 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
5767 #ifdef XtNarrowScrollbars
5768 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
5769 #endif
5770 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
5771 if (xaw3d_arrow_scroll || val == initial)
5772 { /* ARROW_SCROLL */
5773 xaw3d_arrow_scroll = True;
5774 /* Isn't that just a personal preference ? --Stef */
5775 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
5779 /* Define callbacks. */
5780 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
5781 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
5782 (XtPointer) bar);
5784 /* Realize the widget. Only after that is the X window created. */
5785 XtRealizeWidget (widget);
5787 #endif /* !USE_MOTIF */
5789 /* Install an action hook that lets us detect when the user
5790 finishes interacting with a scroll bar. */
5791 if (action_hook_id == 0)
5792 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
5794 /* Remember X window and widget in the scroll bar vector. */
5795 SET_SCROLL_BAR_X_WIDGET (bar, widget);
5796 xwindow = XtWindow (widget);
5797 bar->x_window = xwindow;
5798 bar->whole = 1;
5799 bar->horizontal = false;
5801 unblock_input ();
5804 static void
5805 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5807 Window xwindow;
5808 Widget widget;
5809 Arg av[20];
5810 int ac = 0;
5811 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
5812 unsigned long pixel;
5814 block_input ();
5816 #ifdef USE_MOTIF
5817 /* Set resources. Create the widget. */
5818 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5819 XtSetArg (av[ac], XmNminimum, 0); ++ac;
5820 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
5821 XtSetArg (av[ac], XmNorientation, XmHORIZONTAL); ++ac;
5822 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_RIGHT), ++ac;
5823 XtSetArg (av[ac], XmNincrement, 1); ++ac;
5824 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
5826 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5827 if (pixel != -1)
5829 XtSetArg (av[ac], XmNforeground, pixel);
5830 ++ac;
5833 pixel = f->output_data.x->scroll_bar_background_pixel;
5834 if (pixel != -1)
5836 XtSetArg (av[ac], XmNbackground, pixel);
5837 ++ac;
5840 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
5841 (char *) scroll_bar_name, av, ac);
5843 /* Add one callback for everything that can happen. */
5844 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
5845 (XtPointer) bar);
5846 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
5847 (XtPointer) bar);
5848 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
5849 (XtPointer) bar);
5850 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
5851 (XtPointer) bar);
5852 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
5853 (XtPointer) bar);
5854 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
5855 (XtPointer) bar);
5856 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
5857 (XtPointer) bar);
5859 /* Realize the widget. Only after that is the X window created. */
5860 XtRealizeWidget (widget);
5862 /* Set the cursor to an arrow. I didn't find a resource to do that.
5863 And I'm wondering why it hasn't an arrow cursor by default. */
5864 XDefineCursor (XtDisplay (widget), XtWindow (widget),
5865 f->output_data.x->nontext_cursor);
5867 #else /* !USE_MOTIF i.e. use Xaw */
5869 /* Set resources. Create the widget. The background of the
5870 Xaw3d scroll bar widget is a little bit light for my taste.
5871 We don't alter it here to let users change it according
5872 to their taste with `emacs*verticalScrollBar.background: xxx'. */
5873 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5874 XtSetArg (av[ac], XtNorientation, XtorientHorizontal); ++ac;
5875 /* For smoother scrolling with Xaw3d -sm */
5876 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
5878 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5879 if (pixel != -1)
5881 XtSetArg (av[ac], XtNforeground, pixel);
5882 ++ac;
5885 pixel = f->output_data.x->scroll_bar_background_pixel;
5886 if (pixel != -1)
5888 XtSetArg (av[ac], XtNbackground, pixel);
5889 ++ac;
5892 /* Top/bottom shadow colors. */
5894 /* Allocate them, if necessary. */
5895 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
5897 pixel = f->output_data.x->scroll_bar_background_pixel;
5898 if (pixel != -1)
5900 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5901 FRAME_X_COLORMAP (f),
5902 &pixel, 1.2, 0x8000))
5903 pixel = -1;
5904 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
5907 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5909 pixel = f->output_data.x->scroll_bar_background_pixel;
5910 if (pixel != -1)
5912 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5913 FRAME_X_COLORMAP (f),
5914 &pixel, 0.6, 0x4000))
5915 pixel = -1;
5916 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
5920 #ifdef XtNbeNiceToColormap
5921 /* Tell the toolkit about them. */
5922 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
5923 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5924 /* We tried to allocate a color for the top/bottom shadow, and
5925 failed, so tell Xaw3d to use dithering instead. */
5926 /* But only if we have a small colormap. Xaw3d can allocate nice
5927 colors itself. */
5929 XtSetArg (av[ac], XtNbeNiceToColormap,
5930 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
5931 ++ac;
5933 else
5934 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
5935 be more consistent with other emacs 3d colors, and since Xaw3d is
5936 not good at dealing with allocation failure. */
5938 /* This tells Xaw3d to use real colors instead of dithering for
5939 the shadows. */
5940 XtSetArg (av[ac], XtNbeNiceToColormap, False);
5941 ++ac;
5943 /* Specify the colors. */
5944 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
5945 if (pixel != -1)
5947 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
5948 ++ac;
5950 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
5951 if (pixel != -1)
5953 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
5954 ++ac;
5957 #endif
5959 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
5960 f->output_data.x->edit_widget, av, ac);
5963 char const *initial = "";
5964 char const *val = initial;
5965 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
5966 #ifdef XtNarrowScrollbars
5967 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
5968 #endif
5969 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
5970 if (xaw3d_arrow_scroll || val == initial)
5971 { /* ARROW_SCROLL */
5972 xaw3d_arrow_scroll = True;
5973 /* Isn't that just a personal preference ? --Stef */
5974 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
5978 /* Define callbacks. */
5979 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
5980 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
5981 (XtPointer) bar);
5983 /* Realize the widget. Only after that is the X window created. */
5984 XtRealizeWidget (widget);
5986 #endif /* !USE_MOTIF */
5988 /* Install an action hook that lets us detect when the user
5989 finishes interacting with a scroll bar. */
5990 if (horizontal_action_hook_id == 0)
5991 horizontal_action_hook_id
5992 = XtAppAddActionHook (Xt_app_con, xt_horizontal_action_hook, 0);
5994 /* Remember X window and widget in the scroll bar vector. */
5995 SET_SCROLL_BAR_X_WIDGET (bar, widget);
5996 xwindow = XtWindow (widget);
5997 bar->x_window = xwindow;
5998 bar->whole = 1;
5999 bar->horizontal = true;
6001 unblock_input ();
6003 #endif /* not USE_GTK */
6006 /* Set the thumb size and position of scroll bar BAR. We are currently
6007 displaying PORTION out of a whole WHOLE, and our position POSITION. */
6009 #ifdef USE_GTK
6010 static void
6011 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
6013 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
6016 static void
6017 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
6019 xg_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
6022 #else /* not USE_GTK */
6023 static void
6024 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
6025 int whole)
6027 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6028 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6029 float top, shown;
6031 block_input ();
6033 #ifdef USE_MOTIF
6035 if (scroll_bar_adjust_thumb_portion_p)
6037 /* We use an estimate of 30 chars per line rather than the real
6038 `portion' value. This has the disadvantage that the thumb size
6039 is not very representative, but it makes our life a lot easier.
6040 Otherwise, we have to constantly adjust the thumb size, which
6041 we can't always do quickly enough: while dragging, the size of
6042 the thumb might prevent the user from dragging the thumb all the
6043 way to the end. but Motif and some versions of Xaw3d don't allow
6044 updating the thumb size while dragging. Also, even if we can update
6045 its size, the update will often happen too late.
6046 If you don't believe it, check out revision 1.650 of xterm.c to see
6047 what hoops we were going through and the still poor behavior we got. */
6048 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
6049 /* When the thumb is at the bottom, position == whole.
6050 So we need to increase `whole' to make space for the thumb. */
6051 whole += portion;
6054 if (whole <= 0)
6055 top = 0, shown = 1;
6056 else
6058 top = (float) position / whole;
6059 shown = (float) portion / whole;
6062 if (bar->dragging == -1)
6064 int size, value;
6066 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
6067 is the scroll bar's maximum and MIN is the scroll bar's minimum
6068 value. */
6069 size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
6071 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
6072 value = top * XM_SB_MAX;
6073 value = min (value, XM_SB_MAX - size);
6075 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6077 #else /* !USE_MOTIF i.e. use Xaw */
6079 if (whole == 0)
6080 top = 0, shown = 1;
6081 else
6083 top = (float) position / whole;
6084 shown = (float) portion / whole;
6088 float old_top, old_shown;
6089 Dimension height;
6090 XtVaGetValues (widget,
6091 XtNtopOfThumb, &old_top,
6092 XtNshown, &old_shown,
6093 XtNheight, &height,
6094 NULL);
6096 /* Massage the top+shown values. */
6097 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
6098 top = max (0, min (1, top));
6099 else
6100 top = old_top;
6101 #if ! defined (HAVE_XAW3D)
6102 /* With Xaw, 'top' values too closer to 1.0 may
6103 cause the thumb to disappear. Fix that. */
6104 top = min (top, 0.99f);
6105 #endif
6106 /* Keep two pixels available for moving the thumb down. */
6107 shown = max (0, min (1 - top - (2.0f / height), shown));
6108 #if ! defined (HAVE_XAW3D)
6109 /* Likewise with too small 'shown'. */
6110 shown = max (shown, 0.01f);
6111 #endif
6113 /* If the call to XawScrollbarSetThumb below doesn't seem to
6114 work, check that 'NARROWPROTO' is defined in src/config.h.
6115 If this is not so, most likely you need to fix configure. */
6116 if (top != old_top || shown != old_shown)
6118 if (bar->dragging == -1)
6119 XawScrollbarSetThumb (widget, top, shown);
6120 else
6122 /* Try to make the scrolling a tad smoother. */
6123 if (!xaw3d_pick_top)
6124 shown = min (shown, old_shown);
6126 XawScrollbarSetThumb (widget, top, shown);
6130 #endif /* !USE_MOTIF */
6132 unblock_input ();
6135 static void
6136 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
6137 int whole)
6139 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6140 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6141 float top, shown;
6143 block_input ();
6145 #ifdef USE_MOTIF
6146 bar->whole = whole;
6147 shown = (float) portion / whole;
6148 top = (float) position / (whole - portion);
6150 int size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
6151 int value = clip_to_bounds (0, top * (XM_SB_MAX - size), XM_SB_MAX - size);
6153 XmScrollBarSetValues (widget, value, size, 0, 0, False);
6155 #else /* !USE_MOTIF i.e. use Xaw */
6156 bar->whole = whole;
6157 if (whole == 0)
6158 top = 0, shown = 1;
6159 else
6161 top = (float) position / whole;
6162 shown = (float) portion / whole;
6166 float old_top, old_shown;
6167 Dimension height;
6168 XtVaGetValues (widget,
6169 XtNtopOfThumb, &old_top,
6170 XtNshown, &old_shown,
6171 XtNheight, &height,
6172 NULL);
6174 #if false
6175 /* Massage the top+shown values. */
6176 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
6177 top = max (0, min (1, top));
6178 else
6179 top = old_top;
6180 #if ! defined (HAVE_XAW3D)
6181 /* With Xaw, 'top' values too closer to 1.0 may
6182 cause the thumb to disappear. Fix that. */
6183 top = min (top, 0.99f);
6184 #endif
6185 /* Keep two pixels available for moving the thumb down. */
6186 shown = max (0, min (1 - top - (2.0f / height), shown));
6187 #if ! defined (HAVE_XAW3D)
6188 /* Likewise with too small 'shown'. */
6189 shown = max (shown, 0.01f);
6190 #endif
6191 #endif
6193 /* If the call to XawScrollbarSetThumb below doesn't seem to
6194 work, check that 'NARROWPROTO' is defined in src/config.h.
6195 If this is not so, most likely you need to fix configure. */
6196 XawScrollbarSetThumb (widget, top, shown);
6197 #if false
6198 if (top != old_top || shown != old_shown)
6200 if (bar->dragging == -1)
6201 XawScrollbarSetThumb (widget, top, shown);
6202 else
6204 /* Try to make the scrolling a tad smoother. */
6205 if (!xaw3d_pick_top)
6206 shown = min (shown, old_shown);
6208 XawScrollbarSetThumb (widget, top, shown);
6211 #endif
6213 #endif /* !USE_MOTIF */
6215 unblock_input ();
6217 #endif /* not USE_GTK */
6219 #endif /* USE_TOOLKIT_SCROLL_BARS */
6223 /************************************************************************
6224 Scroll bars, general
6225 ************************************************************************/
6227 /* Create a scroll bar and return the scroll bar vector for it. W is
6228 the Emacs window on which to create the scroll bar. TOP, LEFT,
6229 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
6230 scroll bar. */
6232 static struct scroll_bar *
6233 x_scroll_bar_create (struct window *w, int top, int left,
6234 int width, int height, bool horizontal)
6236 struct frame *f = XFRAME (w->frame);
6237 struct scroll_bar *bar
6238 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
6239 Lisp_Object barobj;
6241 block_input ();
6243 #ifdef USE_TOOLKIT_SCROLL_BARS
6244 if (horizontal)
6245 x_create_horizontal_toolkit_scroll_bar (f, bar);
6246 else
6247 x_create_toolkit_scroll_bar (f, bar);
6248 #else /* not USE_TOOLKIT_SCROLL_BARS */
6250 XSetWindowAttributes a;
6251 unsigned long mask;
6252 Window window;
6254 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
6255 if (a.background_pixel == -1)
6256 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
6258 a.event_mask = (ButtonPressMask | ButtonReleaseMask
6259 | ButtonMotionMask | PointerMotionHintMask
6260 | ExposureMask);
6261 a.cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
6263 mask = (CWBackPixel | CWEventMask | CWCursor);
6265 /* Clear the area of W that will serve as a scroll bar. This is
6266 for the case that a window has been split horizontally. In
6267 this case, no clear_frame is generated to reduce flickering. */
6268 if (width > 0 && window_box_height (w) > 0)
6269 x_clear_area (f, left, top, width, window_box_height (w));
6271 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6272 /* Position and size of scroll bar. */
6273 left, top, width, height,
6274 /* Border width, depth, class, and visual. */
6276 CopyFromParent,
6277 CopyFromParent,
6278 CopyFromParent,
6279 /* Attributes. */
6280 mask, &a);
6281 bar->x_window = window;
6283 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6285 XSETWINDOW (bar->window, w);
6286 bar->top = top;
6287 bar->left = left;
6288 bar->width = width;
6289 bar->height = height;
6290 bar->start = 0;
6291 bar->end = 0;
6292 bar->dragging = -1;
6293 bar->horizontal = horizontal;
6294 #if defined (USE_TOOLKIT_SCROLL_BARS) && defined (USE_LUCID)
6295 bar->last_seen_part = scroll_bar_nowhere;
6296 #endif
6298 /* Add bar to its frame's list of scroll bars. */
6299 bar->next = FRAME_SCROLL_BARS (f);
6300 bar->prev = Qnil;
6301 XSETVECTOR (barobj, bar);
6302 fset_scroll_bars (f, barobj);
6303 if (!NILP (bar->next))
6304 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6306 /* Map the window/widget. */
6307 #ifdef USE_TOOLKIT_SCROLL_BARS
6309 #ifdef USE_GTK
6310 if (horizontal)
6311 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top,
6312 left, width, max (height, 1));
6313 else
6314 xg_update_scrollbar_pos (f, bar->x_window, top,
6315 left, width, max (height, 1));
6316 #else /* not USE_GTK */
6317 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
6318 XtConfigureWidget (scroll_bar, left, top, width, max (height, 1), 0);
6319 XtMapWidget (scroll_bar);
6320 #endif /* not USE_GTK */
6322 #else /* not USE_TOOLKIT_SCROLL_BARS */
6323 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
6324 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6326 unblock_input ();
6327 return bar;
6331 #ifndef USE_TOOLKIT_SCROLL_BARS
6333 /* Draw BAR's handle in the proper position.
6335 If the handle is already drawn from START to END, don't bother
6336 redrawing it, unless REBUILD; in that case, always
6337 redraw it. (REBUILD is handy for drawing the handle after expose
6338 events.)
6340 Normally, we want to constrain the start and end of the handle to
6341 fit inside its rectangle, but if the user is dragging the scroll
6342 bar handle, we want to let them drag it down all the way, so that
6343 the bar's top is as far down as it goes; otherwise, there's no way
6344 to move to the very end of the buffer. */
6346 static void
6347 x_scroll_bar_set_handle (struct scroll_bar *bar, int start, int end,
6348 bool rebuild)
6350 bool dragging = bar->dragging != -1;
6351 Window w = bar->x_window;
6352 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6353 GC gc = f->output_data.x->normal_gc;
6355 /* If the display is already accurate, do nothing. */
6356 if (! rebuild
6357 && start == bar->start
6358 && end == bar->end)
6359 return;
6361 block_input ();
6364 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
6365 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
6366 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
6368 /* Make sure the values are reasonable, and try to preserve
6369 the distance between start and end. */
6371 int length = end - start;
6373 if (start < 0)
6374 start = 0;
6375 else if (start > top_range)
6376 start = top_range;
6377 end = start + length;
6379 if (end < start)
6380 end = start;
6381 else if (end > top_range && ! dragging)
6382 end = top_range;
6385 /* Store the adjusted setting in the scroll bar. */
6386 bar->start = start;
6387 bar->end = end;
6389 /* Clip the end position, just for display. */
6390 if (end > top_range)
6391 end = top_range;
6393 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
6394 below top positions, to make sure the handle is always at least
6395 that many pixels tall. */
6396 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
6398 /* Draw the empty space above the handle. Note that we can't clear
6399 zero-height areas; that means "clear to end of window." */
6400 if ((inside_width > 0) && (start > 0))
6401 x_clear_area1 (FRAME_X_DISPLAY (f), w,
6402 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6403 VERTICAL_SCROLL_BAR_TOP_BORDER,
6404 inside_width, start);
6406 /* Change to proper foreground color if one is specified. */
6407 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6408 XSetForeground (FRAME_X_DISPLAY (f), gc,
6409 f->output_data.x->scroll_bar_foreground_pixel);
6411 /* Draw the handle itself. */
6412 x_fill_rectangle (f, gc,
6413 /* x, y, width, height */
6414 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6415 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
6416 inside_width, end - start);
6418 /* Restore the foreground color of the GC if we changed it above. */
6419 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6420 XSetForeground (FRAME_X_DISPLAY (f), gc,
6421 FRAME_FOREGROUND_PIXEL (f));
6423 /* Draw the empty space below the handle. Note that we can't
6424 clear zero-height areas; that means "clear to end of window." */
6425 if ((inside_width > 0) && (end < inside_height))
6426 x_clear_area1 (FRAME_X_DISPLAY (f), w,
6427 VERTICAL_SCROLL_BAR_LEFT_BORDER,
6428 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
6429 inside_width, inside_height - end);
6432 unblock_input ();
6435 #endif /* !USE_TOOLKIT_SCROLL_BARS */
6437 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
6438 nil. */
6440 static void
6441 x_scroll_bar_remove (struct scroll_bar *bar)
6443 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6444 block_input ();
6446 #ifdef USE_TOOLKIT_SCROLL_BARS
6447 #ifdef USE_GTK
6448 xg_remove_scroll_bar (f, bar->x_window);
6449 #else /* not USE_GTK */
6450 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
6451 #endif /* not USE_GTK */
6452 #else
6453 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
6454 #endif
6456 /* Dissociate this scroll bar from its window. */
6457 if (bar->horizontal)
6458 wset_horizontal_scroll_bar (XWINDOW (bar->window), Qnil);
6459 else
6460 wset_vertical_scroll_bar (XWINDOW (bar->window), Qnil);
6462 unblock_input ();
6466 /* Set the handle of the vertical scroll bar for WINDOW to indicate
6467 that we are displaying PORTION characters out of a total of WHOLE
6468 characters, starting at POSITION. If WINDOW has no scroll bar,
6469 create one. */
6471 static void
6472 XTset_vertical_scroll_bar (struct window *w, int portion, int whole, int position)
6474 struct frame *f = XFRAME (w->frame);
6475 Lisp_Object barobj;
6476 struct scroll_bar *bar;
6477 int top, height, left, width;
6478 int window_y, window_height;
6480 /* Get window dimensions. */
6481 window_box (w, ANY_AREA, 0, &window_y, 0, &window_height);
6482 top = window_y;
6483 height = window_height;
6484 left = WINDOW_SCROLL_BAR_AREA_X (w);
6485 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
6487 /* Does the scroll bar exist yet? */
6488 if (NILP (w->vertical_scroll_bar))
6490 if (width > 0 && height > 0)
6492 block_input ();
6493 x_clear_area (f, left, top, width, height);
6494 unblock_input ();
6497 bar = x_scroll_bar_create (w, top, left, width, max (height, 1), false);
6499 else
6501 /* It may just need to be moved and resized. */
6502 unsigned int mask = 0;
6504 bar = XSCROLL_BAR (w->vertical_scroll_bar);
6506 block_input ();
6508 if (left != bar->left)
6509 mask |= CWX;
6510 if (top != bar->top)
6511 mask |= CWY;
6512 if (width != bar->width)
6513 mask |= CWWidth;
6514 if (height != bar->height)
6515 mask |= CWHeight;
6517 #ifdef USE_TOOLKIT_SCROLL_BARS
6519 /* Move/size the scroll bar widget. */
6520 if (mask)
6522 /* Since toolkit scroll bars are smaller than the space reserved
6523 for them on the frame, we have to clear "under" them. */
6524 if (width > 0 && height > 0)
6525 x_clear_area (f, left, top, width, height);
6526 #ifdef USE_GTK
6527 xg_update_scrollbar_pos (f, bar->x_window, top,
6528 left, width, max (height, 1));
6529 #else /* not USE_GTK */
6530 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
6531 left, top, width, max (height, 1), 0);
6532 #endif /* not USE_GTK */
6534 #else /* not USE_TOOLKIT_SCROLL_BARS */
6536 /* Move/size the scroll bar window. */
6537 if (mask)
6539 XWindowChanges wc;
6541 wc.x = left;
6542 wc.y = top;
6543 wc.width = width;
6544 wc.height = height;
6545 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
6546 mask, &wc);
6549 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6551 /* Remember new settings. */
6552 bar->left = left;
6553 bar->top = top;
6554 bar->width = width;
6555 bar->height = height;
6557 unblock_input ();
6560 #ifdef USE_TOOLKIT_SCROLL_BARS
6561 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
6562 #else /* not USE_TOOLKIT_SCROLL_BARS */
6563 /* Set the scroll bar's current state, unless we're currently being
6564 dragged. */
6565 if (bar->dragging == -1)
6567 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
6569 if (whole == 0)
6570 x_scroll_bar_set_handle (bar, 0, top_range, false);
6571 else
6573 int start = ((double) position * top_range) / whole;
6574 int end = ((double) (position + portion) * top_range) / whole;
6575 x_scroll_bar_set_handle (bar, start, end, false);
6578 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6580 XSETVECTOR (barobj, bar);
6581 wset_vertical_scroll_bar (w, barobj);
6585 static void
6586 XTset_horizontal_scroll_bar (struct window *w, int portion, int whole, int position)
6588 struct frame *f = XFRAME (w->frame);
6589 Lisp_Object barobj;
6590 struct scroll_bar *bar;
6591 int top, height, left, width;
6592 int window_x, window_width;
6593 int pixel_width = WINDOW_PIXEL_WIDTH (w);
6595 /* Get window dimensions. */
6596 window_box (w, ANY_AREA, &window_x, 0, &window_width, 0);
6597 left = window_x;
6598 width = window_width;
6599 top = WINDOW_SCROLL_BAR_AREA_Y (w);
6600 height = WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
6602 /* Does the scroll bar exist yet? */
6603 if (NILP (w->horizontal_scroll_bar))
6605 if (width > 0 && height > 0)
6607 block_input ();
6609 /* Clear also part between window_width and
6610 WINDOW_PIXEL_WIDTH. */
6611 x_clear_area (f, left, top, pixel_width, height);
6612 unblock_input ();
6615 bar = x_scroll_bar_create (w, top, left, width, height, true);
6617 else
6619 /* It may just need to be moved and resized. */
6620 unsigned int mask = 0;
6622 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
6624 block_input ();
6626 if (left != bar->left)
6627 mask |= CWX;
6628 if (top != bar->top)
6629 mask |= CWY;
6630 if (width != bar->width)
6631 mask |= CWWidth;
6632 if (height != bar->height)
6633 mask |= CWHeight;
6635 #ifdef USE_TOOLKIT_SCROLL_BARS
6636 /* Move/size the scroll bar widget. */
6637 if (mask)
6639 /* Since toolkit scroll bars are smaller than the space reserved
6640 for them on the frame, we have to clear "under" them. */
6641 if (width > 0 && height > 0)
6642 x_clear_area (f,
6643 WINDOW_LEFT_EDGE_X (w), top,
6644 pixel_width - WINDOW_RIGHT_DIVIDER_WIDTH (w), height);
6645 #ifdef USE_GTK
6646 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top, left,
6647 width, height);
6648 #else /* not USE_GTK */
6649 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
6650 left, top, width, height, 0);
6651 #endif /* not USE_GTK */
6653 #else /* not USE_TOOLKIT_SCROLL_BARS */
6655 /* Clear areas not covered by the scroll bar because it's not as
6656 wide as the area reserved for it. This makes sure a
6657 previous mode line display is cleared after C-x 2 C-x 1, for
6658 example. */
6660 int area_height = WINDOW_CONFIG_SCROLL_BAR_HEIGHT (w);
6661 int rest = area_height - height;
6662 if (rest > 0 && width > 0)
6663 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6664 left, top, width, rest);
6667 /* Move/size the scroll bar window. */
6668 if (mask)
6670 XWindowChanges wc;
6672 wc.x = left;
6673 wc.y = top;
6674 wc.width = width;
6675 wc.height = height;
6676 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
6677 mask, &wc);
6680 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6682 /* Remember new settings. */
6683 bar->left = left;
6684 bar->top = top;
6685 bar->width = width;
6686 bar->height = height;
6688 unblock_input ();
6691 #ifdef USE_TOOLKIT_SCROLL_BARS
6692 x_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
6693 #else /* not USE_TOOLKIT_SCROLL_BARS */
6694 /* Set the scroll bar's current state, unless we're currently being
6695 dragged. */
6696 if (bar->dragging == -1)
6698 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, width);
6700 if (whole == 0)
6701 x_scroll_bar_set_handle (bar, 0, left_range, false);
6702 else
6704 int start = ((double) position * left_range) / whole;
6705 int end = ((double) (position + portion) * left_range) / whole;
6706 x_scroll_bar_set_handle (bar, start, end, false);
6709 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6711 XSETVECTOR (barobj, bar);
6712 wset_horizontal_scroll_bar (w, barobj);
6716 /* The following three hooks are used when we're doing a thorough
6717 redisplay of the frame. We don't explicitly know which scroll bars
6718 are going to be deleted, because keeping track of when windows go
6719 away is a real pain - "Can you say set-window-configuration, boys
6720 and girls?" Instead, we just assert at the beginning of redisplay
6721 that *all* scroll bars are to be removed, and then save a scroll bar
6722 from the fiery pit when we actually redisplay its window. */
6724 /* Arrange for all scroll bars on FRAME to be removed at the next call
6725 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
6726 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
6728 static void
6729 XTcondemn_scroll_bars (struct frame *frame)
6731 if (!NILP (FRAME_SCROLL_BARS (frame)))
6733 if (!NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
6735 /* Prepend scrollbars to already condemned ones. */
6736 Lisp_Object last = FRAME_SCROLL_BARS (frame);
6738 while (!NILP (XSCROLL_BAR (last)->next))
6739 last = XSCROLL_BAR (last)->next;
6741 XSCROLL_BAR (last)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
6742 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = last;
6745 fset_condemned_scroll_bars (frame, FRAME_SCROLL_BARS (frame));
6746 fset_scroll_bars (frame, Qnil);
6751 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
6752 Note that WINDOW isn't necessarily condemned at all. */
6754 static void
6755 XTredeem_scroll_bar (struct window *w)
6757 struct scroll_bar *bar;
6758 Lisp_Object barobj;
6759 struct frame *f;
6761 /* We can't redeem this window's scroll bar if it doesn't have one. */
6762 if (NILP (w->vertical_scroll_bar) && NILP (w->horizontal_scroll_bar))
6763 emacs_abort ();
6765 if (!NILP (w->vertical_scroll_bar) && WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
6767 bar = XSCROLL_BAR (w->vertical_scroll_bar);
6768 /* Unlink it from the condemned list. */
6769 f = XFRAME (WINDOW_FRAME (w));
6770 if (NILP (bar->prev))
6772 /* If the prev pointer is nil, it must be the first in one of
6773 the lists. */
6774 if (EQ (FRAME_SCROLL_BARS (f), w->vertical_scroll_bar))
6775 /* It's not condemned. Everything's fine. */
6776 goto horizontal;
6777 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
6778 w->vertical_scroll_bar))
6779 fset_condemned_scroll_bars (f, bar->next);
6780 else
6781 /* If its prev pointer is nil, it must be at the front of
6782 one or the other! */
6783 emacs_abort ();
6785 else
6786 XSCROLL_BAR (bar->prev)->next = bar->next;
6788 if (! NILP (bar->next))
6789 XSCROLL_BAR (bar->next)->prev = bar->prev;
6791 bar->next = FRAME_SCROLL_BARS (f);
6792 bar->prev = Qnil;
6793 XSETVECTOR (barobj, bar);
6794 fset_scroll_bars (f, barobj);
6795 if (! NILP (bar->next))
6796 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6799 horizontal:
6800 if (!NILP (w->horizontal_scroll_bar) && WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
6802 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
6803 /* Unlink it from the condemned list. */
6804 f = XFRAME (WINDOW_FRAME (w));
6805 if (NILP (bar->prev))
6807 /* If the prev pointer is nil, it must be the first in one of
6808 the lists. */
6809 if (EQ (FRAME_SCROLL_BARS (f), w->horizontal_scroll_bar))
6810 /* It's not condemned. Everything's fine. */
6811 return;
6812 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
6813 w->horizontal_scroll_bar))
6814 fset_condemned_scroll_bars (f, bar->next);
6815 else
6816 /* If its prev pointer is nil, it must be at the front of
6817 one or the other! */
6818 emacs_abort ();
6820 else
6821 XSCROLL_BAR (bar->prev)->next = bar->next;
6823 if (! NILP (bar->next))
6824 XSCROLL_BAR (bar->next)->prev = bar->prev;
6826 bar->next = FRAME_SCROLL_BARS (f);
6827 bar->prev = Qnil;
6828 XSETVECTOR (barobj, bar);
6829 fset_scroll_bars (f, barobj);
6830 if (! NILP (bar->next))
6831 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6835 /* Remove all scroll bars on FRAME that haven't been saved since the
6836 last call to `*condemn_scroll_bars_hook'. */
6838 static void
6839 XTjudge_scroll_bars (struct frame *f)
6841 Lisp_Object bar, next;
6843 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
6845 /* Clear out the condemned list now so we won't try to process any
6846 more events on the hapless scroll bars. */
6847 fset_condemned_scroll_bars (f, Qnil);
6849 for (; ! NILP (bar); bar = next)
6851 struct scroll_bar *b = XSCROLL_BAR (bar);
6853 x_scroll_bar_remove (b);
6855 next = b->next;
6856 b->next = b->prev = Qnil;
6859 /* Now there should be no references to the condemned scroll bars,
6860 and they should get garbage-collected. */
6864 #ifndef USE_TOOLKIT_SCROLL_BARS
6865 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
6866 is a no-op when using toolkit scroll bars.
6868 This may be called from a signal handler, so we have to ignore GC
6869 mark bits. */
6871 static void
6872 x_scroll_bar_expose (struct scroll_bar *bar, const XEvent *event)
6874 Window w = bar->x_window;
6875 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6876 GC gc = f->output_data.x->normal_gc;
6878 block_input ();
6880 x_scroll_bar_set_handle (bar, bar->start, bar->end, true);
6882 /* Switch to scroll bar foreground color. */
6883 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6884 XSetForeground (FRAME_X_DISPLAY (f), gc,
6885 f->output_data.x->scroll_bar_foreground_pixel);
6887 /* Draw a one-pixel border just inside the edges of the scroll bar. */
6888 x_draw_rectangle (f, gc,
6889 /* x, y, width, height */
6890 0, 0, bar->width - 1, bar->height - 1);
6892 /* Restore the foreground color of the GC if we changed it above. */
6893 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6894 XSetForeground (FRAME_X_DISPLAY (f), gc,
6895 FRAME_FOREGROUND_PIXEL (f));
6897 unblock_input ();
6900 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6902 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
6903 is set to something other than NO_EVENT, it is enqueued.
6905 This may be called from a signal handler, so we have to ignore GC
6906 mark bits. */
6909 static void
6910 x_scroll_bar_handle_click (struct scroll_bar *bar,
6911 const XEvent *event,
6912 struct input_event *emacs_event)
6914 if (! WINDOWP (bar->window))
6915 emacs_abort ();
6917 emacs_event->kind = (bar->horizontal
6918 ? HORIZONTAL_SCROLL_BAR_CLICK_EVENT
6919 : SCROLL_BAR_CLICK_EVENT);
6920 emacs_event->code = event->xbutton.button - Button1;
6921 emacs_event->modifiers
6922 = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO
6923 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
6924 event->xbutton.state)
6925 | (event->type == ButtonRelease
6926 ? up_modifier
6927 : down_modifier));
6928 emacs_event->frame_or_window = bar->window;
6929 emacs_event->arg = Qnil;
6930 emacs_event->timestamp = event->xbutton.time;
6931 if (bar->horizontal)
6933 int left_range
6934 = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
6935 int x = event->xbutton.x - HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
6937 if (x < 0) x = 0;
6938 if (x > left_range) x = left_range;
6940 if (x < bar->start)
6941 emacs_event->part = scroll_bar_before_handle;
6942 else if (x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
6943 emacs_event->part = scroll_bar_horizontal_handle;
6944 else
6945 emacs_event->part = scroll_bar_after_handle;
6947 #ifndef USE_TOOLKIT_SCROLL_BARS
6948 /* If the user has released the handle, set it to its final position. */
6949 if (event->type == ButtonRelease && bar->dragging != -1)
6951 int new_start = - bar->dragging;
6952 int new_end = new_start + bar->end - bar->start;
6954 x_scroll_bar_set_handle (bar, new_start, new_end, false);
6955 bar->dragging = -1;
6957 #endif
6959 XSETINT (emacs_event->x, left_range);
6960 XSETINT (emacs_event->y, x);
6962 else
6964 int top_range
6965 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
6966 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
6968 if (y < 0) y = 0;
6969 if (y > top_range) y = top_range;
6971 if (y < bar->start)
6972 emacs_event->part = scroll_bar_above_handle;
6973 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
6974 emacs_event->part = scroll_bar_handle;
6975 else
6976 emacs_event->part = scroll_bar_below_handle;
6978 #ifndef USE_TOOLKIT_SCROLL_BARS
6979 /* If the user has released the handle, set it to its final position. */
6980 if (event->type == ButtonRelease && bar->dragging != -1)
6982 int new_start = y - bar->dragging;
6983 int new_end = new_start + bar->end - bar->start;
6985 x_scroll_bar_set_handle (bar, new_start, new_end, false);
6986 bar->dragging = -1;
6988 #endif
6990 XSETINT (emacs_event->x, y);
6991 XSETINT (emacs_event->y, top_range);
6995 #ifndef USE_TOOLKIT_SCROLL_BARS
6997 /* Handle some mouse motion while someone is dragging the scroll bar.
6999 This may be called from a signal handler, so we have to ignore GC
7000 mark bits. */
7002 static void
7003 x_scroll_bar_note_movement (struct scroll_bar *bar,
7004 const XMotionEvent *event)
7006 struct frame *f = XFRAME (XWINDOW (bar->window)->frame);
7007 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
7009 dpyinfo->last_mouse_movement_time = event->time;
7010 dpyinfo->last_mouse_scroll_bar = bar;
7011 f->mouse_moved = true;
7013 /* If we're dragging the bar, display it. */
7014 if (bar->dragging != -1)
7016 /* Where should the handle be now? */
7017 int new_start = event->y - bar->dragging;
7019 if (new_start != bar->start)
7021 int new_end = new_start + bar->end - bar->start;
7023 x_scroll_bar_set_handle (bar, new_start, new_end, false);
7028 #endif /* !USE_TOOLKIT_SCROLL_BARS */
7030 /* Return information to the user about the current position of the mouse
7031 on the scroll bar. */
7033 static void
7034 x_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
7035 enum scroll_bar_part *part, Lisp_Object *x,
7036 Lisp_Object *y, Time *timestamp)
7038 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
7039 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
7040 Window w = bar->x_window;
7041 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7042 int win_x, win_y;
7043 Window dummy_window;
7044 int dummy_coord;
7045 unsigned int dummy_mask;
7047 block_input ();
7049 /* Get the mouse's position relative to the scroll bar window, and
7050 report that. */
7051 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
7053 /* Root, child, root x and root y. */
7054 &dummy_window, &dummy_window,
7055 &dummy_coord, &dummy_coord,
7057 /* Position relative to scroll bar. */
7058 &win_x, &win_y,
7060 /* Mouse buttons and modifier keys. */
7061 &dummy_mask))
7063 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
7065 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
7067 if (bar->dragging != -1)
7068 win_y -= bar->dragging;
7070 if (win_y < 0)
7071 win_y = 0;
7072 if (win_y > top_range)
7073 win_y = top_range;
7075 *fp = f;
7076 *bar_window = bar->window;
7078 if (bar->dragging != -1)
7079 *part = scroll_bar_handle;
7080 else if (win_y < bar->start)
7081 *part = scroll_bar_above_handle;
7082 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
7083 *part = scroll_bar_handle;
7084 else
7085 *part = scroll_bar_below_handle;
7087 XSETINT (*x, win_y);
7088 XSETINT (*y, top_range);
7090 f->mouse_moved = false;
7091 dpyinfo->last_mouse_scroll_bar = NULL;
7092 *timestamp = dpyinfo->last_mouse_movement_time;
7095 unblock_input ();
7099 /* Return information to the user about the current position of the mouse
7100 on the scroll bar. */
7102 static void
7103 x_horizontal_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
7104 enum scroll_bar_part *part, Lisp_Object *x,
7105 Lisp_Object *y, Time *timestamp)
7107 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
7108 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
7109 Window w = bar->x_window;
7110 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7111 int win_x, win_y;
7112 Window dummy_window;
7113 int dummy_coord;
7114 unsigned int dummy_mask;
7116 block_input ();
7118 /* Get the mouse's position relative to the scroll bar window, and
7119 report that. */
7120 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
7122 /* Root, child, root x and root y. */
7123 &dummy_window, &dummy_window,
7124 &dummy_coord, &dummy_coord,
7126 /* Position relative to scroll bar. */
7127 &win_x, &win_y,
7129 /* Mouse buttons and modifier keys. */
7130 &dummy_mask))
7132 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
7134 win_x -= HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
7136 if (bar->dragging != -1)
7137 win_x -= bar->dragging;
7139 if (win_x < 0)
7140 win_x = 0;
7141 if (win_x > left_range)
7142 win_x = left_range;
7144 *fp = f;
7145 *bar_window = bar->window;
7147 if (bar->dragging != -1)
7148 *part = scroll_bar_horizontal_handle;
7149 else if (win_x < bar->start)
7150 *part = scroll_bar_before_handle;
7151 else if (win_x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
7152 *part = scroll_bar_handle;
7153 else
7154 *part = scroll_bar_after_handle;
7156 XSETINT (*y, win_x);
7157 XSETINT (*x, left_range);
7159 f->mouse_moved = false;
7160 dpyinfo->last_mouse_scroll_bar = NULL;
7161 *timestamp = dpyinfo->last_mouse_movement_time;
7164 unblock_input ();
7168 /* The screen has been cleared so we may have changed foreground or
7169 background colors, and the scroll bars may need to be redrawn.
7170 Clear out the scroll bars, and ask for expose events, so we can
7171 redraw them. */
7173 static void
7174 x_scroll_bar_clear (struct frame *f)
7176 #ifndef USE_TOOLKIT_SCROLL_BARS
7177 Lisp_Object bar;
7179 /* We can have scroll bars even if this is 0,
7180 if we just turned off scroll bar mode.
7181 But in that case we should not clear them. */
7182 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
7183 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
7184 bar = XSCROLL_BAR (bar)->next)
7185 XClearArea (FRAME_X_DISPLAY (f),
7186 XSCROLL_BAR (bar)->x_window,
7187 0, 0, 0, 0, True);
7188 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7191 #ifdef ENABLE_CHECKING
7193 /* Record the last 100 characters stored
7194 to help debug the loss-of-chars-during-GC problem. */
7196 static int temp_index;
7197 static short temp_buffer[100];
7199 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
7200 if (temp_index == ARRAYELTS (temp_buffer)) \
7201 temp_index = 0; \
7202 temp_buffer[temp_index++] = (keysym)
7204 #else /* not ENABLE_CHECKING */
7206 #define STORE_KEYSYM_FOR_DEBUG(keysym) ((void)0)
7208 #endif /* ENABLE_CHECKING */
7210 /* Set this to nonzero to fake an "X I/O error"
7211 on a particular display. */
7213 static struct x_display_info *XTread_socket_fake_io_error;
7215 /* When we find no input here, we occasionally do a no-op command
7216 to verify that the X server is still running and we can still talk with it.
7217 We try all the open displays, one by one.
7218 This variable is used for cycling thru the displays. */
7220 static struct x_display_info *next_noop_dpyinfo;
7222 enum
7224 X_EVENT_NORMAL,
7225 X_EVENT_GOTO_OUT,
7226 X_EVENT_DROP
7229 /* Filter events for the current X input method.
7230 DPYINFO is the display this event is for.
7231 EVENT is the X event to filter.
7233 Returns non-zero if the event was filtered, caller shall not process
7234 this event further.
7235 Returns zero if event is wasn't filtered. */
7237 #ifdef HAVE_X_I18N
7238 static int
7239 x_filter_event (struct x_display_info *dpyinfo, XEvent *event)
7241 /* XFilterEvent returns non-zero if the input method has
7242 consumed the event. We pass the frame's X window to
7243 XFilterEvent because that's the one for which the IC
7244 was created. */
7246 struct frame *f1 = x_any_window_to_frame (dpyinfo,
7247 event->xclient.window);
7249 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
7251 #endif
7253 #ifdef USE_GTK
7254 static int current_count;
7255 static int current_finish;
7256 static struct input_event *current_hold_quit;
7258 /* This is the filter function invoked by the GTK event loop.
7259 It is invoked before the XEvent is translated to a GdkEvent,
7260 so we have a chance to act on the event before GTK. */
7261 static GdkFilterReturn
7262 event_handler_gdk (GdkXEvent *gxev, GdkEvent *ev, gpointer data)
7264 XEvent *xev = (XEvent *) gxev;
7266 block_input ();
7267 if (current_count >= 0)
7269 struct x_display_info *dpyinfo;
7271 dpyinfo = x_display_info_for_display (xev->xany.display);
7273 #ifdef HAVE_X_I18N
7274 /* Filter events for the current X input method.
7275 GTK calls XFilterEvent but not for key press and release,
7276 so we do it here. */
7277 if ((xev->type == KeyPress || xev->type == KeyRelease)
7278 && dpyinfo
7279 && x_filter_event (dpyinfo, xev))
7281 unblock_input ();
7282 return GDK_FILTER_REMOVE;
7284 #endif
7286 if (! dpyinfo)
7287 current_finish = X_EVENT_NORMAL;
7288 else
7289 current_count
7290 += handle_one_xevent (dpyinfo, xev, &current_finish,
7291 current_hold_quit);
7293 else
7294 current_finish = x_dispatch_event (xev, xev->xany.display);
7296 unblock_input ();
7298 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
7299 return GDK_FILTER_REMOVE;
7301 return GDK_FILTER_CONTINUE;
7303 #endif /* USE_GTK */
7306 static void xembed_send_message (struct frame *f, Time,
7307 enum xembed_message,
7308 long detail, long data1, long data2);
7310 static void
7311 x_net_wm_state (struct frame *f, Window window)
7313 int value = FULLSCREEN_NONE;
7314 Lisp_Object lval = Qnil;
7315 bool sticky = false;
7317 get_current_wm_state (f, window, &value, &sticky);
7319 switch (value)
7321 case FULLSCREEN_WIDTH:
7322 lval = Qfullwidth;
7323 break;
7324 case FULLSCREEN_HEIGHT:
7325 lval = Qfullheight;
7326 break;
7327 case FULLSCREEN_BOTH:
7328 lval = Qfullboth;
7329 break;
7330 case FULLSCREEN_MAXIMIZED:
7331 lval = Qmaximized;
7332 break;
7335 frame_size_history_add
7336 (f, Qx_net_wm_state, 0, 0,
7337 list2 (get_frame_param (f, Qfullscreen), lval));
7339 store_frame_param (f, Qfullscreen, lval);
7340 /** store_frame_param (f, Qsticky, sticky ? Qt : Qnil); **/
7343 /* Handles the XEvent EVENT on display DPYINFO.
7345 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
7346 *FINISH is zero if caller should continue reading events.
7347 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
7348 *EVENT is unchanged unless we're processing KeyPress event.
7350 We return the number of characters stored into the buffer. */
7352 static int
7353 handle_one_xevent (struct x_display_info *dpyinfo,
7354 const XEvent *event,
7355 int *finish, struct input_event *hold_quit)
7357 union {
7358 struct input_event ie;
7359 struct selection_input_event sie;
7360 } inev;
7361 int count = 0;
7362 int do_help = 0;
7363 ptrdiff_t nbytes = 0;
7364 struct frame *any, *f = NULL;
7365 struct coding_system coding;
7366 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
7367 /* This holds the state XLookupString needs to implement dead keys
7368 and other tricks known as "compose processing". _X Window System_
7369 says that a portable program can't use this, but Stephen Gildea assures
7370 me that letting the compiler initialize it to zeros will work okay. */
7371 static XComposeStatus compose_status;
7373 USE_SAFE_ALLOCA;
7375 *finish = X_EVENT_NORMAL;
7377 EVENT_INIT (inev.ie);
7378 inev.ie.kind = NO_EVENT;
7379 inev.ie.arg = Qnil;
7381 any = x_any_window_to_frame (dpyinfo, event->xany.window);
7383 if (any && any->wait_event_type == event->type)
7384 any->wait_event_type = 0; /* Indicates we got it. */
7386 switch (event->type)
7388 case ClientMessage:
7390 if (event->xclient.message_type == dpyinfo->Xatom_wm_protocols
7391 && event->xclient.format == 32)
7393 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_take_focus)
7395 /* Use the value returned by x_any_window_to_frame
7396 because this could be the shell widget window
7397 if the frame has no title bar. */
7398 f = any;
7399 #ifdef HAVE_X_I18N
7400 /* Not quite sure this is needed -pd */
7401 if (f && FRAME_XIC (f))
7402 XSetICFocus (FRAME_XIC (f));
7403 #endif
7404 #if false
7405 /* Emacs sets WM hints whose `input' field is `true'. This
7406 instructs the WM to set the input focus automatically for
7407 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
7408 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
7409 it has set the focus. So, XSetInputFocus below is not
7410 needed.
7412 The call to XSetInputFocus below has also caused trouble. In
7413 cases where the XSetInputFocus done by the WM and the one
7414 below are temporally close (on a fast machine), the call
7415 below can generate additional FocusIn events which confuse
7416 Emacs. */
7418 /* Since we set WM_TAKE_FOCUS, we must call
7419 XSetInputFocus explicitly. But not if f is null,
7420 since that might be an event for a deleted frame. */
7421 if (f)
7423 Display *d = event->xclient.display;
7424 /* Catch and ignore errors, in case window has been
7425 iconified by a window manager such as GWM. */
7426 x_catch_errors (d);
7427 XSetInputFocus (d, event->xclient.window,
7428 /* The ICCCM says this is
7429 the only valid choice. */
7430 RevertToParent,
7431 event->xclient.data.l[1]);
7432 /* This is needed to detect the error
7433 if there is an error. */
7434 XSync (d, False);
7435 x_uncatch_errors ();
7437 /* Not certain about handling scroll bars here */
7438 #endif
7439 goto done;
7442 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_save_yourself)
7444 /* Save state modify the WM_COMMAND property to
7445 something which can reinstate us. This notifies
7446 the session manager, who's looking for such a
7447 PropertyNotify. Can restart processing when
7448 a keyboard or mouse event arrives. */
7449 /* If we have a session manager, don't set this.
7450 KDE will then start two Emacsen, one for the
7451 session manager and one for this. */
7452 #ifdef HAVE_X_SM
7453 if (! x_session_have_connection ())
7454 #endif
7456 f = x_top_window_to_frame (dpyinfo,
7457 event->xclient.window);
7458 /* This is just so we only give real data once
7459 for a single Emacs process. */
7460 if (f == SELECTED_FRAME ())
7461 XSetCommand (FRAME_X_DISPLAY (f),
7462 event->xclient.window,
7463 initial_argv, initial_argc);
7464 else if (f)
7465 XSetCommand (FRAME_X_DISPLAY (f),
7466 event->xclient.window,
7467 0, 0);
7469 goto done;
7472 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_delete_window)
7474 f = any;
7475 if (!f)
7476 goto OTHER; /* May be a dialog that is to be removed */
7478 inev.ie.kind = DELETE_WINDOW_EVENT;
7479 XSETFRAME (inev.ie.frame_or_window, f);
7480 goto done;
7483 goto done;
7486 if (event->xclient.message_type == dpyinfo->Xatom_wm_configure_denied)
7487 goto done;
7489 if (event->xclient.message_type == dpyinfo->Xatom_wm_window_moved)
7491 int new_x, new_y;
7492 f = x_window_to_frame (dpyinfo, event->xclient.window);
7494 new_x = event->xclient.data.s[0];
7495 new_y = event->xclient.data.s[1];
7497 if (f)
7499 f->left_pos = new_x;
7500 f->top_pos = new_y;
7502 goto done;
7505 #ifdef HACK_EDITRES
7506 if (event->xclient.message_type == dpyinfo->Xatom_editres)
7508 f = any;
7509 if (f)
7510 _XEditResCheckMessages (f->output_data.x->widget,
7511 NULL, (XEvent *) event, NULL);
7512 goto done;
7514 #endif /* HACK_EDITRES */
7516 if (event->xclient.message_type == dpyinfo->Xatom_DONE
7517 || event->xclient.message_type == dpyinfo->Xatom_PAGE)
7519 /* Ghostview job completed. Kill it. We could
7520 reply with "Next" if we received "Page", but we
7521 currently never do because we are interested in
7522 images, only, which should have 1 page. */
7523 Pixmap pixmap = (Pixmap) event->xclient.data.l[1];
7524 f = x_window_to_frame (dpyinfo, event->xclient.window);
7525 if (!f)
7526 goto OTHER;
7527 x_kill_gs_process (pixmap, f);
7528 expose_frame (f, 0, 0, 0, 0);
7529 goto done;
7532 #ifdef USE_TOOLKIT_SCROLL_BARS
7533 /* Scroll bar callbacks send a ClientMessage from which
7534 we construct an input_event. */
7535 if (event->xclient.message_type == dpyinfo->Xatom_Scrollbar)
7537 x_scroll_bar_to_input_event (event, &inev.ie);
7538 *finish = X_EVENT_GOTO_OUT;
7539 goto done;
7541 else if (event->xclient.message_type == dpyinfo->Xatom_Horizontal_Scrollbar)
7543 x_horizontal_scroll_bar_to_input_event (event, &inev.ie);
7544 *finish = X_EVENT_GOTO_OUT;
7545 goto done;
7547 #endif /* USE_TOOLKIT_SCROLL_BARS */
7549 /* XEmbed messages from the embedder (if any). */
7550 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
7552 enum xembed_message msg = event->xclient.data.l[1];
7553 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
7554 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7556 *finish = X_EVENT_GOTO_OUT;
7557 goto done;
7560 xft_settings_event (dpyinfo, event);
7562 f = any;
7563 if (!f)
7564 goto OTHER;
7565 if (x_handle_dnd_message (f, &event->xclient, dpyinfo, &inev.ie))
7566 *finish = X_EVENT_DROP;
7568 break;
7570 case SelectionNotify:
7571 x_display_set_last_user_time (dpyinfo, event->xselection.time);
7572 #ifdef USE_X_TOOLKIT
7573 if (! x_window_to_frame (dpyinfo, event->xselection.requestor))
7574 goto OTHER;
7575 #endif /* not USE_X_TOOLKIT */
7576 x_handle_selection_notify (&event->xselection);
7577 break;
7579 case SelectionClear: /* Someone has grabbed ownership. */
7580 x_display_set_last_user_time (dpyinfo, event->xselectionclear.time);
7581 #ifdef USE_X_TOOLKIT
7582 if (! x_window_to_frame (dpyinfo, event->xselectionclear.window))
7583 goto OTHER;
7584 #endif /* USE_X_TOOLKIT */
7586 const XSelectionClearEvent *eventp = &event->xselectionclear;
7588 inev.ie.kind = SELECTION_CLEAR_EVENT;
7589 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
7590 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
7591 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
7593 break;
7595 case SelectionRequest: /* Someone wants our selection. */
7596 x_display_set_last_user_time (dpyinfo, event->xselectionrequest.time);
7597 #ifdef USE_X_TOOLKIT
7598 if (!x_window_to_frame (dpyinfo, event->xselectionrequest.owner))
7599 goto OTHER;
7600 #endif /* USE_X_TOOLKIT */
7602 const XSelectionRequestEvent *eventp = &event->xselectionrequest;
7604 inev.ie.kind = SELECTION_REQUEST_EVENT;
7605 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
7606 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
7607 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
7608 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
7609 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
7610 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
7612 break;
7614 case PropertyNotify:
7615 x_display_set_last_user_time (dpyinfo, event->xproperty.time);
7616 f = x_top_window_to_frame (dpyinfo, event->xproperty.window);
7617 if (f && event->xproperty.atom == dpyinfo->Xatom_net_wm_state)
7619 bool not_hidden = x_handle_net_wm_state (f, &event->xproperty);
7620 if (not_hidden && FRAME_ICONIFIED_P (f))
7622 /* Gnome shell does not iconify us when C-z is pressed.
7623 It hides the frame. So if our state says we aren't
7624 hidden anymore, treat it as deiconified. */
7625 SET_FRAME_VISIBLE (f, 1);
7626 SET_FRAME_ICONIFIED (f, false);
7627 f->output_data.x->has_been_visible = true;
7628 inev.ie.kind = DEICONIFY_EVENT;
7629 XSETFRAME (inev.ie.frame_or_window, f);
7631 else if (! not_hidden && ! FRAME_ICONIFIED_P (f))
7633 SET_FRAME_VISIBLE (f, 0);
7634 SET_FRAME_ICONIFIED (f, true);
7635 inev.ie.kind = ICONIFY_EVENT;
7636 XSETFRAME (inev.ie.frame_or_window, f);
7640 x_handle_property_notify (&event->xproperty);
7641 xft_settings_event (dpyinfo, event);
7642 goto OTHER;
7644 case ReparentNotify:
7645 f = x_top_window_to_frame (dpyinfo, event->xreparent.window);
7646 if (f)
7648 f->output_data.x->parent_desc = event->xreparent.parent;
7649 x_real_positions (f, &f->left_pos, &f->top_pos);
7651 /* Perhaps reparented due to a WM restart. Reset this. */
7652 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
7653 FRAME_DISPLAY_INFO (f)->net_supported_window = 0;
7655 x_set_frame_alpha (f);
7657 goto OTHER;
7659 case Expose:
7660 f = x_window_to_frame (dpyinfo, event->xexpose.window);
7661 if (f)
7663 if (!FRAME_VISIBLE_P (f))
7665 SET_FRAME_VISIBLE (f, 1);
7666 SET_FRAME_ICONIFIED (f, false);
7667 f->output_data.x->has_been_visible = true;
7668 SET_FRAME_GARBAGED (f);
7670 else
7672 #if defined (USE_GTK) && ! defined (HAVE_GTK3) && ! defined (USE_CAIRO)
7673 /* This seems to be needed for GTK 2.6 and later, see
7674 http://debbugs.gnu.org/cgi/bugreport.cgi?bug=15398. */
7675 x_clear_area (f,
7676 event->xexpose.x, event->xexpose.y,
7677 event->xexpose.width, event->xexpose.height);
7678 #endif
7679 expose_frame (f, event->xexpose.x, event->xexpose.y,
7680 event->xexpose.width, event->xexpose.height);
7683 else
7685 #ifndef USE_TOOLKIT_SCROLL_BARS
7686 struct scroll_bar *bar;
7687 #endif
7688 #if defined USE_LUCID
7689 /* Submenus of the Lucid menu bar aren't widgets
7690 themselves, so there's no way to dispatch events
7691 to them. Recognize this case separately. */
7693 Widget widget = x_window_to_menu_bar (event->xexpose.window);
7694 if (widget)
7695 xlwmenu_redisplay (widget);
7697 #endif /* USE_LUCID */
7699 #ifdef USE_TOOLKIT_SCROLL_BARS
7700 /* Dispatch event to the widget. */
7701 goto OTHER;
7702 #else /* not USE_TOOLKIT_SCROLL_BARS */
7703 bar = x_window_to_scroll_bar (event->xexpose.display,
7704 event->xexpose.window, 2);
7706 if (bar)
7707 x_scroll_bar_expose (bar, event);
7708 #ifdef USE_X_TOOLKIT
7709 else
7710 goto OTHER;
7711 #endif /* USE_X_TOOLKIT */
7712 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7714 break;
7716 case GraphicsExpose: /* This occurs when an XCopyArea's
7717 source area was obscured or not
7718 available. */
7719 f = x_window_to_frame (dpyinfo, event->xgraphicsexpose.drawable);
7720 if (f)
7721 expose_frame (f, event->xgraphicsexpose.x,
7722 event->xgraphicsexpose.y,
7723 event->xgraphicsexpose.width,
7724 event->xgraphicsexpose.height);
7725 #ifdef USE_X_TOOLKIT
7726 else
7727 goto OTHER;
7728 #endif /* USE_X_TOOLKIT */
7729 break;
7731 case NoExpose: /* This occurs when an XCopyArea's
7732 source area was completely
7733 available. */
7734 break;
7736 case UnmapNotify:
7737 /* Redo the mouse-highlight after the tooltip has gone. */
7738 if (event->xunmap.window == tip_window)
7740 tip_window = 0;
7741 x_redo_mouse_highlight (dpyinfo);
7744 f = x_top_window_to_frame (dpyinfo, event->xunmap.window);
7745 if (f) /* F may no longer exist if
7746 the frame was deleted. */
7748 bool visible = FRAME_VISIBLE_P (f);
7749 /* While a frame is unmapped, display generation is
7750 disabled; you don't want to spend time updating a
7751 display that won't ever be seen. */
7752 SET_FRAME_VISIBLE (f, 0);
7753 /* We can't distinguish, from the event, whether the window
7754 has become iconified or invisible. So assume, if it
7755 was previously visible, than now it is iconified.
7756 But x_make_frame_invisible clears both
7757 the visible flag and the iconified flag;
7758 and that way, we know the window is not iconified now. */
7759 if (visible || FRAME_ICONIFIED_P (f))
7761 SET_FRAME_ICONIFIED (f, true);
7762 inev.ie.kind = ICONIFY_EVENT;
7763 XSETFRAME (inev.ie.frame_or_window, f);
7766 goto OTHER;
7768 case MapNotify:
7769 /* We use x_top_window_to_frame because map events can
7770 come for sub-windows and they don't mean that the
7771 frame is visible. */
7772 f = x_top_window_to_frame (dpyinfo, event->xmap.window);
7773 if (f)
7775 bool iconified = FRAME_ICONIFIED_P (f);
7777 /* Check if fullscreen was specified before we where mapped the
7778 first time, i.e. from the command line. */
7779 if (!f->output_data.x->has_been_visible)
7780 x_check_fullscreen (f);
7782 SET_FRAME_VISIBLE (f, 1);
7783 SET_FRAME_ICONIFIED (f, false);
7784 f->output_data.x->has_been_visible = true;
7786 if (iconified)
7788 inev.ie.kind = DEICONIFY_EVENT;
7789 XSETFRAME (inev.ie.frame_or_window, f);
7791 else if (! NILP (Vframe_list) && ! NILP (XCDR (Vframe_list)))
7792 /* Force a redisplay sooner or later to update the
7793 frame titles in case this is the second frame. */
7794 record_asynch_buffer_change ();
7796 #ifdef USE_GTK
7797 xg_frame_resized (f, -1, -1);
7798 #endif
7800 goto OTHER;
7802 case KeyPress:
7804 x_display_set_last_user_time (dpyinfo, event->xkey.time);
7805 ignore_next_mouse_click_timeout = 0;
7807 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
7808 /* Dispatch KeyPress events when in menu. */
7809 if (popup_activated ())
7810 goto OTHER;
7811 #endif
7813 f = any;
7815 /* If mouse-highlight is an integer, input clears out
7816 mouse highlighting. */
7817 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
7818 #if ! defined (USE_GTK)
7819 && (f == 0
7820 || !EQ (f->tool_bar_window, hlinfo->mouse_face_window))
7821 #endif
7824 clear_mouse_face (hlinfo);
7825 hlinfo->mouse_face_hidden = true;
7828 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
7829 if (f == 0)
7831 /* Scroll bars consume key events, but we want
7832 the keys to go to the scroll bar's frame. */
7833 Widget widget = XtWindowToWidget (dpyinfo->display,
7834 event->xkey.window);
7835 if (widget && XmIsScrollBar (widget))
7837 widget = XtParent (widget);
7838 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
7841 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
7843 if (f != 0)
7845 KeySym keysym, orig_keysym;
7846 /* al%imercury@uunet.uu.net says that making this 81
7847 instead of 80 fixed a bug whereby meta chars made
7848 his Emacs hang.
7850 It seems that some version of XmbLookupString has
7851 a bug of not returning XBufferOverflow in
7852 status_return even if the input is too long to
7853 fit in 81 bytes. So, we must prepare sufficient
7854 bytes for copy_buffer. 513 bytes (256 chars for
7855 two-byte character set) seems to be a fairly good
7856 approximation. -- 2000.8.10 handa@etl.go.jp */
7857 unsigned char copy_buffer[513];
7858 unsigned char *copy_bufptr = copy_buffer;
7859 int copy_bufsiz = sizeof (copy_buffer);
7860 int modifiers;
7861 Lisp_Object coding_system = Qlatin_1;
7862 Lisp_Object c;
7863 /* Event will be modified. */
7864 XKeyEvent xkey = event->xkey;
7866 #ifdef USE_GTK
7867 /* Don't pass keys to GTK. A Tab will shift focus to the
7868 tool bar in GTK 2.4. Keys will still go to menus and
7869 dialogs because in that case popup_activated is nonzero
7870 (see above). */
7871 *finish = X_EVENT_DROP;
7872 #endif
7874 xkey.state |= x_emacs_to_x_modifiers (FRAME_DISPLAY_INFO (f),
7875 extra_keyboard_modifiers);
7876 modifiers = xkey.state;
7878 /* This will have to go some day... */
7880 /* make_lispy_event turns chars into control chars.
7881 Don't do it here because XLookupString is too eager. */
7882 xkey.state &= ~ControlMask;
7883 xkey.state &= ~(dpyinfo->meta_mod_mask
7884 | dpyinfo->super_mod_mask
7885 | dpyinfo->hyper_mod_mask
7886 | dpyinfo->alt_mod_mask);
7888 /* In case Meta is ComposeCharacter,
7889 clear its status. According to Markus Ehrnsperger
7890 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
7891 this enables ComposeCharacter to work whether or
7892 not it is combined with Meta. */
7893 if (modifiers & dpyinfo->meta_mod_mask)
7894 memset (&compose_status, 0, sizeof (compose_status));
7896 #ifdef HAVE_X_I18N
7897 if (FRAME_XIC (f))
7899 Status status_return;
7901 coding_system = Vlocale_coding_system;
7902 nbytes = XmbLookupString (FRAME_XIC (f),
7903 &xkey, (char *) copy_bufptr,
7904 copy_bufsiz, &keysym,
7905 &status_return);
7906 if (status_return == XBufferOverflow)
7908 copy_bufsiz = nbytes + 1;
7909 copy_bufptr = alloca (copy_bufsiz);
7910 nbytes = XmbLookupString (FRAME_XIC (f),
7911 &xkey, (char *) copy_bufptr,
7912 copy_bufsiz, &keysym,
7913 &status_return);
7915 /* Xutf8LookupString is a new but already deprecated interface. -stef */
7916 if (status_return == XLookupNone)
7917 break;
7918 else if (status_return == XLookupChars)
7920 keysym = NoSymbol;
7921 modifiers = 0;
7923 else if (status_return != XLookupKeySym
7924 && status_return != XLookupBoth)
7925 emacs_abort ();
7927 else
7928 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
7929 copy_bufsiz, &keysym,
7930 &compose_status);
7931 #else
7932 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
7933 copy_bufsiz, &keysym,
7934 &compose_status);
7935 #endif
7937 /* If not using XIM/XIC, and a compose sequence is in progress,
7938 we break here. Otherwise, chars_matched is always 0. */
7939 if (compose_status.chars_matched > 0 && nbytes == 0)
7940 break;
7942 memset (&compose_status, 0, sizeof (compose_status));
7943 orig_keysym = keysym;
7945 /* Common for all keysym input events. */
7946 XSETFRAME (inev.ie.frame_or_window, f);
7947 inev.ie.modifiers
7948 = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), modifiers);
7949 inev.ie.timestamp = xkey.time;
7951 /* First deal with keysyms which have defined
7952 translations to characters. */
7953 if (keysym >= 32 && keysym < 128)
7954 /* Avoid explicitly decoding each ASCII character. */
7956 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
7957 inev.ie.code = keysym;
7958 goto done_keysym;
7961 /* Keysyms directly mapped to Unicode characters. */
7962 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
7964 if (keysym < 0x01000080)
7965 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
7966 else
7967 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
7968 inev.ie.code = keysym & 0xFFFFFF;
7969 goto done_keysym;
7972 /* Now non-ASCII. */
7973 if (HASH_TABLE_P (Vx_keysym_table)
7974 && (c = Fgethash (make_number (keysym),
7975 Vx_keysym_table,
7976 Qnil),
7977 NATNUMP (c)))
7979 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
7980 ? ASCII_KEYSTROKE_EVENT
7981 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
7982 inev.ie.code = XFASTINT (c);
7983 goto done_keysym;
7986 /* Random non-modifier sorts of keysyms. */
7987 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
7988 || keysym == XK_Delete
7989 #ifdef XK_ISO_Left_Tab
7990 || (keysym >= XK_ISO_Left_Tab
7991 && keysym <= XK_ISO_Enter)
7992 #endif
7993 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
7994 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
7995 #ifdef HPUX
7996 /* This recognizes the "extended function
7997 keys". It seems there's no cleaner way.
7998 Test IsModifierKey to avoid handling
7999 mode_switch incorrectly. */
8000 || (XK_Select <= keysym && keysym < XK_KP_Space)
8001 #endif
8002 #ifdef XK_dead_circumflex
8003 || orig_keysym == XK_dead_circumflex
8004 #endif
8005 #ifdef XK_dead_grave
8006 || orig_keysym == XK_dead_grave
8007 #endif
8008 #ifdef XK_dead_tilde
8009 || orig_keysym == XK_dead_tilde
8010 #endif
8011 #ifdef XK_dead_diaeresis
8012 || orig_keysym == XK_dead_diaeresis
8013 #endif
8014 #ifdef XK_dead_macron
8015 || orig_keysym == XK_dead_macron
8016 #endif
8017 #ifdef XK_dead_degree
8018 || orig_keysym == XK_dead_degree
8019 #endif
8020 #ifdef XK_dead_acute
8021 || orig_keysym == XK_dead_acute
8022 #endif
8023 #ifdef XK_dead_cedilla
8024 || orig_keysym == XK_dead_cedilla
8025 #endif
8026 #ifdef XK_dead_breve
8027 || orig_keysym == XK_dead_breve
8028 #endif
8029 #ifdef XK_dead_ogonek
8030 || orig_keysym == XK_dead_ogonek
8031 #endif
8032 #ifdef XK_dead_caron
8033 || orig_keysym == XK_dead_caron
8034 #endif
8035 #ifdef XK_dead_doubleacute
8036 || orig_keysym == XK_dead_doubleacute
8037 #endif
8038 #ifdef XK_dead_abovedot
8039 || orig_keysym == XK_dead_abovedot
8040 #endif
8041 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
8042 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
8043 /* Any "vendor-specific" key is ok. */
8044 || (orig_keysym & (1 << 28))
8045 || (keysym != NoSymbol && nbytes == 0))
8046 && ! (IsModifierKey (orig_keysym)
8047 /* The symbols from XK_ISO_Lock
8048 to XK_ISO_Last_Group_Lock
8049 don't have real modifiers but
8050 should be treated similarly to
8051 Mode_switch by Emacs. */
8052 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
8053 || (XK_ISO_Lock <= orig_keysym
8054 && orig_keysym <= XK_ISO_Last_Group_Lock)
8055 #endif
8058 STORE_KEYSYM_FOR_DEBUG (keysym);
8059 /* make_lispy_event will convert this to a symbolic
8060 key. */
8061 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
8062 inev.ie.code = keysym;
8063 goto done_keysym;
8066 { /* Raw bytes, not keysym. */
8067 ptrdiff_t i;
8068 int nchars, len;
8070 for (i = 0, nchars = 0; i < nbytes; i++)
8072 if (ASCII_CHAR_P (copy_bufptr[i]))
8073 nchars++;
8074 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
8077 if (nchars < nbytes)
8079 /* Decode the input data. */
8081 /* The input should be decoded with `coding_system'
8082 which depends on which X*LookupString function
8083 we used just above and the locale. */
8084 setup_coding_system (coding_system, &coding);
8085 coding.src_multibyte = false;
8086 coding.dst_multibyte = true;
8087 /* The input is converted to events, thus we can't
8088 handle composition. Anyway, there's no XIM that
8089 gives us composition information. */
8090 coding.common_flags &= ~CODING_ANNOTATION_MASK;
8092 SAFE_NALLOCA (coding.destination, MAX_MULTIBYTE_LENGTH,
8093 nbytes);
8094 coding.dst_bytes = MAX_MULTIBYTE_LENGTH * nbytes;
8095 coding.mode |= CODING_MODE_LAST_BLOCK;
8096 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
8097 nbytes = coding.produced;
8098 nchars = coding.produced_char;
8099 copy_bufptr = coding.destination;
8102 /* Convert the input data to a sequence of
8103 character events. */
8104 for (i = 0; i < nbytes; i += len)
8106 int ch;
8107 if (nchars == nbytes)
8108 ch = copy_bufptr[i], len = 1;
8109 else
8110 ch = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
8111 inev.ie.kind = (SINGLE_BYTE_CHAR_P (ch)
8112 ? ASCII_KEYSTROKE_EVENT
8113 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
8114 inev.ie.code = ch;
8115 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
8118 count += nchars;
8120 inev.ie.kind = NO_EVENT; /* Already stored above. */
8122 if (keysym == NoSymbol)
8123 break;
8125 /* FIXME: check side effects and remove this. */
8126 ((XEvent *) event)->xkey = xkey;
8128 done_keysym:
8129 #ifdef HAVE_X_I18N
8130 /* Don't dispatch this event since XtDispatchEvent calls
8131 XFilterEvent, and two calls in a row may freeze the
8132 client. */
8133 break;
8134 #else
8135 goto OTHER;
8136 #endif
8138 case KeyRelease:
8139 x_display_set_last_user_time (dpyinfo, event->xkey.time);
8140 #ifdef HAVE_X_I18N
8141 /* Don't dispatch this event since XtDispatchEvent calls
8142 XFilterEvent, and two calls in a row may freeze the
8143 client. */
8144 break;
8145 #else
8146 goto OTHER;
8147 #endif
8149 case EnterNotify:
8150 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
8151 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8153 f = any;
8155 if (f && x_mouse_click_focus_ignore_position)
8156 ignore_next_mouse_click_timeout = event->xmotion.time + 200;
8158 /* EnterNotify counts as mouse movement,
8159 so update things that depend on mouse position. */
8160 if (f && !f->output_data.x->hourglass_p)
8161 note_mouse_movement (f, &event->xmotion);
8162 #ifdef USE_GTK
8163 /* We may get an EnterNotify on the buttons in the toolbar. In that
8164 case we moved out of any highlighted area and need to note this. */
8165 if (!f && dpyinfo->last_mouse_glyph_frame)
8166 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
8167 #endif
8168 goto OTHER;
8170 case FocusIn:
8171 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8172 goto OTHER;
8174 case LeaveNotify:
8175 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
8176 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8178 f = x_top_window_to_frame (dpyinfo, event->xcrossing.window);
8179 if (f)
8181 if (f == hlinfo->mouse_face_mouse_frame)
8183 /* If we move outside the frame, then we're
8184 certainly no longer on any text in the frame. */
8185 clear_mouse_face (hlinfo);
8186 hlinfo->mouse_face_mouse_frame = 0;
8189 /* Generate a nil HELP_EVENT to cancel a help-echo.
8190 Do it only if there's something to cancel.
8191 Otherwise, the startup message is cleared when
8192 the mouse leaves the frame. */
8193 if (any_help_event_p)
8194 do_help = -1;
8196 #ifdef USE_GTK
8197 /* See comment in EnterNotify above */
8198 else if (dpyinfo->last_mouse_glyph_frame)
8199 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
8200 #endif
8201 goto OTHER;
8203 case FocusOut:
8204 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
8205 goto OTHER;
8207 case MotionNotify:
8209 x_display_set_last_user_time (dpyinfo, event->xmotion.time);
8210 previous_help_echo_string = help_echo_string;
8211 help_echo_string = Qnil;
8213 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
8214 : x_window_to_frame (dpyinfo, event->xmotion.window));
8216 if (hlinfo->mouse_face_hidden)
8218 hlinfo->mouse_face_hidden = false;
8219 clear_mouse_face (hlinfo);
8222 #ifdef USE_GTK
8223 if (f && xg_event_is_for_scrollbar (f, event))
8224 f = 0;
8225 #endif
8226 if (f)
8229 /* Generate SELECT_WINDOW_EVENTs when needed.
8230 Don't let popup menus influence things (bug#1261). */
8231 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
8233 static Lisp_Object last_mouse_window;
8234 Lisp_Object window = window_from_coordinates
8235 (f, event->xmotion.x, event->xmotion.y, 0, false);
8237 /* Window will be selected only when it is not selected now and
8238 last mouse movement event was not in it. Minibuffer window
8239 will be selected only when it is active. */
8240 if (WINDOWP (window)
8241 && !EQ (window, last_mouse_window)
8242 && !EQ (window, selected_window)
8243 /* For click-to-focus window managers
8244 create event iff we don't leave the
8245 selected frame. */
8246 && (focus_follows_mouse
8247 || (EQ (XWINDOW (window)->frame,
8248 XWINDOW (selected_window)->frame))))
8250 inev.ie.kind = SELECT_WINDOW_EVENT;
8251 inev.ie.frame_or_window = window;
8253 /* Remember the last window where we saw the mouse. */
8254 last_mouse_window = window;
8256 if (!note_mouse_movement (f, &event->xmotion))
8257 help_echo_string = previous_help_echo_string;
8259 else
8261 #ifndef USE_TOOLKIT_SCROLL_BARS
8262 struct scroll_bar *bar
8263 = x_window_to_scroll_bar (event->xmotion.display,
8264 event->xmotion.window, 2);
8266 if (bar)
8267 x_scroll_bar_note_movement (bar, &event->xmotion);
8268 #endif /* USE_TOOLKIT_SCROLL_BARS */
8270 /* If we move outside the frame, then we're
8271 certainly no longer on any text in the frame. */
8272 clear_mouse_face (hlinfo);
8275 /* If the contents of the global variable help_echo_string
8276 has changed, generate a HELP_EVENT. */
8277 if (!NILP (help_echo_string)
8278 || !NILP (previous_help_echo_string))
8279 do_help = 1;
8280 goto OTHER;
8283 case ConfigureNotify:
8284 f = x_top_window_to_frame (dpyinfo, event->xconfigure.window);
8285 #ifdef USE_CAIRO
8286 if (f) x_cr_destroy_surface (f);
8287 #endif
8288 #ifdef USE_GTK
8289 if (!f
8290 && (f = any)
8291 && event->xconfigure.window == FRAME_X_WINDOW (f))
8293 xg_frame_resized (f, event->xconfigure.width,
8294 event->xconfigure.height);
8295 #ifdef USE_CAIRO
8296 x_cr_destroy_surface (f);
8297 #endif
8298 f = 0;
8300 #endif
8301 if (f)
8303 x_net_wm_state (f, event->xconfigure.window);
8305 #ifdef USE_X_TOOLKIT
8306 /* Tip frames are pure X window, set size for them. */
8307 if (! NILP (tip_frame) && XFRAME (tip_frame) == f)
8309 if (FRAME_PIXEL_HEIGHT (f) != event->xconfigure.height
8310 || FRAME_PIXEL_WIDTH (f) != event->xconfigure.width)
8311 SET_FRAME_GARBAGED (f);
8312 FRAME_PIXEL_HEIGHT (f) = event->xconfigure.height;
8313 FRAME_PIXEL_WIDTH (f) = event->xconfigure.width;
8315 #endif
8317 #ifndef USE_X_TOOLKIT
8318 #ifndef USE_GTK
8319 int width = FRAME_PIXEL_TO_TEXT_WIDTH (f, event->xconfigure.width);
8320 int height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, event->xconfigure.height);
8322 /* In the toolkit version, change_frame_size
8323 is called by the code that handles resizing
8324 of the EmacsFrame widget. */
8326 /* Even if the number of character rows and columns has
8327 not changed, the font size may have changed, so we need
8328 to check the pixel dimensions as well. */
8329 if (width != FRAME_TEXT_WIDTH (f)
8330 || height != FRAME_TEXT_HEIGHT (f)
8331 || event->xconfigure.width != FRAME_PIXEL_WIDTH (f)
8332 || event->xconfigure.height != FRAME_PIXEL_HEIGHT (f))
8334 change_frame_size (f, width, height, false, true, false, true);
8335 x_clear_under_internal_border (f);
8336 SET_FRAME_GARBAGED (f);
8337 cancel_mouse_face (f);
8339 #endif /* not USE_GTK */
8340 #endif
8342 #ifdef USE_GTK
8343 /* GTK creates windows but doesn't map them.
8344 Only get real positions when mapped. */
8345 if (FRAME_GTK_OUTER_WIDGET (f)
8346 && gtk_widget_get_mapped (FRAME_GTK_OUTER_WIDGET (f)))
8347 #endif
8348 x_real_positions (f, &f->left_pos, &f->top_pos);
8350 #ifdef HAVE_X_I18N
8351 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
8352 xic_set_statusarea (f);
8353 #endif
8356 goto OTHER;
8358 case ButtonRelease:
8359 case ButtonPress:
8361 /* If we decide we want to generate an event to be seen
8362 by the rest of Emacs, we put it here. */
8363 bool tool_bar_p = false;
8365 memset (&compose_status, 0, sizeof (compose_status));
8366 dpyinfo->last_mouse_glyph_frame = NULL;
8367 x_display_set_last_user_time (dpyinfo, event->xbutton.time);
8369 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
8370 : x_window_to_frame (dpyinfo, event->xbutton.window));
8372 #ifdef USE_GTK
8373 if (f && xg_event_is_for_scrollbar (f, event))
8374 f = 0;
8375 #endif
8376 if (f)
8378 #if ! defined (USE_GTK)
8379 /* Is this in the tool-bar? */
8380 if (WINDOWP (f->tool_bar_window)
8381 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
8383 Lisp_Object window;
8384 int x = event->xbutton.x;
8385 int y = event->xbutton.y;
8387 window = window_from_coordinates (f, x, y, 0, true);
8388 tool_bar_p = EQ (window, f->tool_bar_window);
8390 if (tool_bar_p && event->xbutton.button < 4)
8391 handle_tool_bar_click
8392 (f, x, y, event->xbutton.type == ButtonPress,
8393 x_x_to_emacs_modifiers (dpyinfo, event->xbutton.state));
8395 #endif /* !USE_GTK */
8397 if (!tool_bar_p)
8398 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8399 if (! popup_activated ())
8400 #endif
8402 if (ignore_next_mouse_click_timeout)
8404 if (event->type == ButtonPress
8405 && event->xbutton.time > ignore_next_mouse_click_timeout)
8407 ignore_next_mouse_click_timeout = 0;
8408 construct_mouse_click (&inev.ie, &event->xbutton, f);
8410 if (event->type == ButtonRelease)
8411 ignore_next_mouse_click_timeout = 0;
8413 else
8414 construct_mouse_click (&inev.ie, &event->xbutton, f);
8416 if (FRAME_X_EMBEDDED_P (f))
8417 xembed_send_message (f, event->xbutton.time,
8418 XEMBED_REQUEST_FOCUS, 0, 0, 0);
8420 else
8422 struct scroll_bar *bar
8423 = x_window_to_scroll_bar (event->xbutton.display,
8424 event->xbutton.window, 2);
8426 #ifdef USE_TOOLKIT_SCROLL_BARS
8427 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
8428 scroll bars. */
8429 if (bar && event->xbutton.state & ControlMask)
8431 x_scroll_bar_handle_click (bar, event, &inev.ie);
8432 *finish = X_EVENT_DROP;
8434 #else /* not USE_TOOLKIT_SCROLL_BARS */
8435 if (bar)
8436 x_scroll_bar_handle_click (bar, event, &inev.ie);
8437 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8440 if (event->type == ButtonPress)
8442 dpyinfo->grabbed |= (1 << event->xbutton.button);
8443 dpyinfo->last_mouse_frame = f;
8444 #if ! defined (USE_GTK)
8445 if (f && !tool_bar_p)
8446 f->last_tool_bar_item = -1;
8447 #endif /* not USE_GTK */
8449 else
8450 dpyinfo->grabbed &= ~(1 << event->xbutton.button);
8452 /* Ignore any mouse motion that happened before this event;
8453 any subsequent mouse-movement Emacs events should reflect
8454 only motion after the ButtonPress/Release. */
8455 if (f != 0)
8456 f->mouse_moved = false;
8458 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8459 f = x_menubar_window_to_frame (dpyinfo, event);
8460 /* For a down-event in the menu bar,
8461 don't pass it to Xt right now.
8462 Instead, save it away
8463 and we will pass it to Xt from kbd_buffer_get_event.
8464 That way, we can run some Lisp code first. */
8465 if (! popup_activated ()
8466 #ifdef USE_GTK
8467 /* Gtk+ menus only react to the first three buttons. */
8468 && event->xbutton.button < 3
8469 #endif
8470 && f && event->type == ButtonPress
8471 /* Verify the event is really within the menu bar
8472 and not just sent to it due to grabbing. */
8473 && event->xbutton.x >= 0
8474 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
8475 && event->xbutton.y >= 0
8476 && event->xbutton.y < FRAME_MENUBAR_HEIGHT (f)
8477 && event->xbutton.same_screen)
8479 if (!f->output_data.x->saved_menu_event)
8480 f->output_data.x->saved_menu_event = xmalloc (sizeof *event);
8481 *f->output_data.x->saved_menu_event = *event;
8482 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT;
8483 XSETFRAME (inev.ie.frame_or_window, f);
8484 *finish = X_EVENT_DROP;
8486 else
8487 goto OTHER;
8488 #endif /* USE_X_TOOLKIT || USE_GTK */
8490 break;
8492 case CirculateNotify:
8493 goto OTHER;
8495 case CirculateRequest:
8496 goto OTHER;
8498 case VisibilityNotify:
8499 goto OTHER;
8501 case MappingNotify:
8502 /* Someone has changed the keyboard mapping - update the
8503 local cache. */
8504 switch (event->xmapping.request)
8506 case MappingModifier:
8507 x_find_modifier_meanings (dpyinfo);
8508 /* This is meant to fall through. */
8509 case MappingKeyboard:
8510 XRefreshKeyboardMapping ((XMappingEvent *) &event->xmapping);
8512 goto OTHER;
8514 case DestroyNotify:
8515 xft_settings_event (dpyinfo, event);
8516 break;
8518 default:
8519 OTHER:
8520 #ifdef USE_X_TOOLKIT
8521 block_input ();
8522 if (*finish != X_EVENT_DROP)
8523 XtDispatchEvent ((XEvent *) event);
8524 unblock_input ();
8525 #endif /* USE_X_TOOLKIT */
8526 break;
8529 done:
8530 if (inev.ie.kind != NO_EVENT)
8532 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
8533 count++;
8536 if (do_help
8537 && !(hold_quit && hold_quit->kind != NO_EVENT))
8539 Lisp_Object frame;
8541 if (f)
8542 XSETFRAME (frame, f);
8543 else
8544 frame = Qnil;
8546 if (do_help > 0)
8548 any_help_event_p = true;
8549 gen_help_event (help_echo_string, frame, help_echo_window,
8550 help_echo_object, help_echo_pos);
8552 else
8554 help_echo_string = Qnil;
8555 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
8557 count++;
8560 SAFE_FREE ();
8561 return count;
8564 /* Handles the XEvent EVENT on display DISPLAY.
8565 This is used for event loops outside the normal event handling,
8566 i.e. looping while a popup menu or a dialog is posted.
8568 Returns the value handle_one_xevent sets in the finish argument. */
8570 x_dispatch_event (XEvent *event, Display *display)
8572 struct x_display_info *dpyinfo;
8573 int finish = X_EVENT_NORMAL;
8575 dpyinfo = x_display_info_for_display (display);
8577 if (dpyinfo)
8578 handle_one_xevent (dpyinfo, event, &finish, 0);
8580 return finish;
8583 /* Read events coming from the X server.
8584 Return as soon as there are no more events to be read.
8586 Return the number of characters stored into the buffer,
8587 thus pretending to be `read' (except the characters we store
8588 in the keyboard buffer can be multibyte, so are not necessarily
8589 C chars). */
8591 static int
8592 XTread_socket (struct terminal *terminal, struct input_event *hold_quit)
8594 int count = 0;
8595 bool event_found = false;
8596 struct x_display_info *dpyinfo = terminal->display_info.x;
8598 block_input ();
8600 /* For debugging, this gives a way to fake an I/O error. */
8601 if (dpyinfo == XTread_socket_fake_io_error)
8603 XTread_socket_fake_io_error = 0;
8604 x_io_error_quitter (dpyinfo->display);
8607 #ifndef USE_GTK
8608 while (XPending (dpyinfo->display))
8610 int finish;
8611 XEvent event;
8613 XNextEvent (dpyinfo->display, &event);
8615 #ifdef HAVE_X_I18N
8616 /* Filter events for the current X input method. */
8617 if (x_filter_event (dpyinfo, &event))
8618 continue;
8619 #endif
8620 event_found = true;
8622 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
8624 if (finish == X_EVENT_GOTO_OUT)
8625 break;
8628 #else /* USE_GTK */
8630 /* For GTK we must use the GTK event loop. But XEvents gets passed
8631 to our filter function above, and then to the big event switch.
8632 We use a bunch of globals to communicate with our filter function,
8633 that is kind of ugly, but it works.
8635 There is no way to do one display at the time, GTK just does events
8636 from all displays. */
8638 while (gtk_events_pending ())
8640 current_count = count;
8641 current_hold_quit = hold_quit;
8643 gtk_main_iteration ();
8645 count = current_count;
8646 current_count = -1;
8647 current_hold_quit = 0;
8649 if (current_finish == X_EVENT_GOTO_OUT)
8650 break;
8652 #endif /* USE_GTK */
8654 /* On some systems, an X bug causes Emacs to get no more events
8655 when the window is destroyed. Detect that. (1994.) */
8656 if (! event_found)
8658 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
8659 One XNOOP in 100 loops will make Emacs terminate.
8660 B. Bretthauer, 1994 */
8661 x_noop_count++;
8662 if (x_noop_count >= 100)
8664 x_noop_count=0;
8666 if (next_noop_dpyinfo == 0)
8667 next_noop_dpyinfo = x_display_list;
8669 XNoOp (next_noop_dpyinfo->display);
8671 /* Each time we get here, cycle through the displays now open. */
8672 next_noop_dpyinfo = next_noop_dpyinfo->next;
8676 /* If the focus was just given to an auto-raising frame,
8677 raise it now. FIXME: handle more than one such frame. */
8678 if (dpyinfo->x_pending_autoraise_frame)
8680 x_raise_frame (dpyinfo->x_pending_autoraise_frame);
8681 dpyinfo->x_pending_autoraise_frame = NULL;
8684 unblock_input ();
8686 return count;
8692 /***********************************************************************
8693 Text Cursor
8694 ***********************************************************************/
8696 /* Set clipping for output in glyph row ROW. W is the window in which
8697 we operate. GC is the graphics context to set clipping in.
8699 ROW may be a text row or, e.g., a mode line. Text rows must be
8700 clipped to the interior of the window dedicated to text display,
8701 mode lines must be clipped to the whole window. */
8703 static void
8704 x_clip_to_row (struct window *w, struct glyph_row *row,
8705 enum glyph_row_area area, GC gc)
8707 struct frame *f = XFRAME (WINDOW_FRAME (w));
8708 XRectangle clip_rect;
8709 int window_x, window_y, window_width;
8711 window_box (w, area, &window_x, &window_y, &window_width, 0);
8713 clip_rect.x = window_x;
8714 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
8715 clip_rect.y = max (clip_rect.y, window_y);
8716 clip_rect.width = window_width;
8717 clip_rect.height = row->visible_height;
8719 x_set_clip_rectangles (f, gc, &clip_rect, 1);
8723 /* Draw a hollow box cursor on window W in glyph row ROW. */
8725 static void
8726 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
8728 struct frame *f = XFRAME (WINDOW_FRAME (w));
8729 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8730 Display *dpy = FRAME_X_DISPLAY (f);
8731 int x, y, wd, h;
8732 XGCValues xgcv;
8733 struct glyph *cursor_glyph;
8734 GC gc;
8736 /* Get the glyph the cursor is on. If we can't tell because
8737 the current matrix is invalid or such, give up. */
8738 cursor_glyph = get_phys_cursor_glyph (w);
8739 if (cursor_glyph == NULL)
8740 return;
8742 /* Compute frame-relative coordinates for phys cursor. */
8743 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
8744 wd = w->phys_cursor_width - 1;
8746 /* The foreground of cursor_gc is typically the same as the normal
8747 background color, which can cause the cursor box to be invisible. */
8748 xgcv.foreground = f->output_data.x->cursor_pixel;
8749 if (dpyinfo->scratch_cursor_gc)
8750 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
8751 else
8752 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
8753 GCForeground, &xgcv);
8754 gc = dpyinfo->scratch_cursor_gc;
8756 /* When on R2L character, show cursor at the right edge of the
8757 glyph, unless the cursor box is as wide as the glyph or wider
8758 (the latter happens when x-stretch-cursor is non-nil). */
8759 if ((cursor_glyph->resolved_level & 1) != 0
8760 && cursor_glyph->pixel_width > wd)
8762 x += cursor_glyph->pixel_width - wd;
8763 if (wd > 0)
8764 wd -= 1;
8766 /* Set clipping, draw the rectangle, and reset clipping again. */
8767 x_clip_to_row (w, row, TEXT_AREA, gc);
8768 x_draw_rectangle (f, gc, x, y, wd, h - 1);
8769 x_reset_clip_rectangles (f, gc);
8773 /* Draw a bar cursor on window W in glyph row ROW.
8775 Implementation note: One would like to draw a bar cursor with an
8776 angle equal to the one given by the font property XA_ITALIC_ANGLE.
8777 Unfortunately, I didn't find a font yet that has this property set.
8778 --gerd. */
8780 static void
8781 x_draw_bar_cursor (struct window *w, struct glyph_row *row, int width, enum text_cursor_kinds kind)
8783 struct frame *f = XFRAME (w->frame);
8784 struct glyph *cursor_glyph;
8786 /* If cursor is out of bounds, don't draw garbage. This can happen
8787 in mini-buffer windows when switching between echo area glyphs
8788 and mini-buffer. */
8789 cursor_glyph = get_phys_cursor_glyph (w);
8790 if (cursor_glyph == NULL)
8791 return;
8793 /* If on an image, draw like a normal cursor. That's usually better
8794 visible than drawing a bar, esp. if the image is large so that
8795 the bar might not be in the window. */
8796 if (cursor_glyph->type == IMAGE_GLYPH)
8798 struct glyph_row *r;
8799 r = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
8800 draw_phys_cursor_glyph (w, r, DRAW_CURSOR);
8802 else
8804 Display *dpy = FRAME_X_DISPLAY (f);
8805 Window window = FRAME_X_WINDOW (f);
8806 GC gc = FRAME_DISPLAY_INFO (f)->scratch_cursor_gc;
8807 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
8808 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
8809 XGCValues xgcv;
8811 /* If the glyph's background equals the color we normally draw
8812 the bars cursor in, the bar cursor in its normal color is
8813 invisible. Use the glyph's foreground color instead in this
8814 case, on the assumption that the glyph's colors are chosen so
8815 that the glyph is legible. */
8816 if (face->background == f->output_data.x->cursor_pixel)
8817 xgcv.background = xgcv.foreground = face->foreground;
8818 else
8819 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
8820 xgcv.graphics_exposures = False;
8822 if (gc)
8823 XChangeGC (dpy, gc, mask, &xgcv);
8824 else
8826 gc = XCreateGC (dpy, window, mask, &xgcv);
8827 FRAME_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
8830 x_clip_to_row (w, row, TEXT_AREA, gc);
8832 if (kind == BAR_CURSOR)
8834 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8836 if (width < 0)
8837 width = FRAME_CURSOR_WIDTH (f);
8838 width = min (cursor_glyph->pixel_width, width);
8840 w->phys_cursor_width = width;
8842 /* If the character under cursor is R2L, draw the bar cursor
8843 on the right of its glyph, rather than on the left. */
8844 if ((cursor_glyph->resolved_level & 1) != 0)
8845 x += cursor_glyph->pixel_width - width;
8847 x_fill_rectangle (f, gc, x,
8848 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
8849 width, row->height);
8851 else /* HBAR_CURSOR */
8853 int dummy_x, dummy_y, dummy_h;
8854 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8856 if (width < 0)
8857 width = row->height;
8859 width = min (row->height, width);
8861 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
8862 &dummy_y, &dummy_h);
8864 if ((cursor_glyph->resolved_level & 1) != 0
8865 && cursor_glyph->pixel_width > w->phys_cursor_width - 1)
8866 x += cursor_glyph->pixel_width - w->phys_cursor_width + 1;
8867 x_fill_rectangle (f, gc, x,
8868 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
8869 row->height - width),
8870 w->phys_cursor_width - 1, width);
8873 x_reset_clip_rectangles (f, gc);
8878 /* RIF: Define cursor CURSOR on frame F. */
8880 static void
8881 x_define_frame_cursor (struct frame *f, Cursor cursor)
8883 if (!f->pointer_invisible
8884 && f->output_data.x->current_cursor != cursor)
8885 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
8886 f->output_data.x->current_cursor = cursor;
8890 /* RIF: Clear area on frame F. */
8892 static void
8893 x_clear_frame_area (struct frame *f, int x, int y, int width, int height)
8895 x_clear_area (f, x, y, width, height);
8896 #ifdef USE_GTK
8897 /* Must queue a redraw, because scroll bars might have been cleared. */
8898 if (FRAME_GTK_WIDGET (f))
8899 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
8900 #endif
8904 /* RIF: Draw cursor on window W. */
8906 static void
8907 x_draw_window_cursor (struct window *w, struct glyph_row *glyph_row, int x,
8908 int y, enum text_cursor_kinds cursor_type,
8909 int cursor_width, bool on_p, bool active_p)
8911 struct frame *f = XFRAME (WINDOW_FRAME (w));
8913 if (on_p)
8915 w->phys_cursor_type = cursor_type;
8916 w->phys_cursor_on_p = true;
8918 if (glyph_row->exact_window_width_line_p
8919 && (glyph_row->reversed_p
8920 ? (w->phys_cursor.hpos < 0)
8921 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
8923 glyph_row->cursor_in_fringe_p = true;
8924 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
8926 else
8928 switch (cursor_type)
8930 case HOLLOW_BOX_CURSOR:
8931 x_draw_hollow_cursor (w, glyph_row);
8932 break;
8934 case FILLED_BOX_CURSOR:
8935 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
8936 break;
8938 case BAR_CURSOR:
8939 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
8940 break;
8942 case HBAR_CURSOR:
8943 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
8944 break;
8946 case NO_CURSOR:
8947 w->phys_cursor_width = 0;
8948 break;
8950 default:
8951 emacs_abort ();
8955 #ifdef HAVE_X_I18N
8956 if (w == XWINDOW (f->selected_window))
8957 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
8958 xic_set_preeditarea (w, x, y);
8959 #endif
8962 XFlush (FRAME_X_DISPLAY (f));
8966 /* Icons. */
8968 /* Make the x-window of frame F use the gnu icon bitmap. */
8970 bool
8971 x_bitmap_icon (struct frame *f, Lisp_Object file)
8973 ptrdiff_t bitmap_id;
8975 if (FRAME_X_WINDOW (f) == 0)
8976 return true;
8978 /* Free up our existing icon bitmap and mask if any. */
8979 if (f->output_data.x->icon_bitmap > 0)
8980 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
8981 f->output_data.x->icon_bitmap = 0;
8983 if (STRINGP (file))
8985 #ifdef USE_GTK
8986 /* Use gtk_window_set_icon_from_file () if available,
8987 It's not restricted to bitmaps */
8988 if (xg_set_icon (f, file))
8989 return false;
8990 #endif /* USE_GTK */
8991 bitmap_id = x_create_bitmap_from_file (f, file);
8992 x_create_bitmap_mask (f, bitmap_id);
8994 else
8996 /* Create the GNU bitmap and mask if necessary. */
8997 if (FRAME_DISPLAY_INFO (f)->icon_bitmap_id < 0)
8999 ptrdiff_t rc = -1;
9001 #ifdef USE_GTK
9003 if (xg_set_icon (f, xg_default_icon_file)
9004 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
9006 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = -2;
9007 return false;
9010 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
9012 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
9013 if (rc != -1)
9014 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
9016 #endif
9018 /* If all else fails, use the (black and white) xbm image. */
9019 if (rc == -1)
9021 rc = x_create_bitmap_from_data (f, (char *) gnu_xbm_bits,
9022 gnu_xbm_width, gnu_xbm_height);
9023 if (rc == -1)
9024 return true;
9026 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
9027 x_create_bitmap_mask (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
9031 /* The first time we create the GNU bitmap and mask,
9032 this increments the ref-count one extra time.
9033 As a result, the GNU bitmap and mask are never freed.
9034 That way, we don't have to worry about allocating it again. */
9035 x_reference_bitmap (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
9037 bitmap_id = FRAME_DISPLAY_INFO (f)->icon_bitmap_id;
9040 x_wm_set_icon_pixmap (f, bitmap_id);
9041 f->output_data.x->icon_bitmap = bitmap_id;
9043 return false;
9047 /* Make the x-window of frame F use a rectangle with text.
9048 Use ICON_NAME as the text. */
9050 bool
9051 x_text_icon (struct frame *f, const char *icon_name)
9053 if (FRAME_X_WINDOW (f) == 0)
9054 return true;
9057 XTextProperty text;
9058 text.value = (unsigned char *) icon_name;
9059 text.encoding = XA_STRING;
9060 text.format = 8;
9061 text.nitems = strlen (icon_name);
9062 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
9065 if (f->output_data.x->icon_bitmap > 0)
9066 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
9067 f->output_data.x->icon_bitmap = 0;
9068 x_wm_set_icon_pixmap (f, 0);
9070 return false;
9073 #define X_ERROR_MESSAGE_SIZE 200
9075 /* If non-nil, this should be a string.
9076 It means catch X errors and store the error message in this string.
9078 The reason we use a stack is that x_catch_error/x_uncatch_error can
9079 be called from a signal handler.
9082 struct x_error_message_stack {
9083 char string[X_ERROR_MESSAGE_SIZE];
9084 Display *dpy;
9085 struct x_error_message_stack *prev;
9087 static struct x_error_message_stack *x_error_message;
9089 /* An X error handler which stores the error message in
9090 *x_error_message. This is called from x_error_handler if
9091 x_catch_errors is in effect. */
9093 static void
9094 x_error_catcher (Display *display, XErrorEvent *event)
9096 XGetErrorText (display, event->error_code,
9097 x_error_message->string,
9098 X_ERROR_MESSAGE_SIZE);
9101 /* Begin trapping X errors for display DPY. Actually we trap X errors
9102 for all displays, but DPY should be the display you are actually
9103 operating on.
9105 After calling this function, X protocol errors no longer cause
9106 Emacs to exit; instead, they are recorded in the string
9107 stored in *x_error_message.
9109 Calling x_check_errors signals an Emacs error if an X error has
9110 occurred since the last call to x_catch_errors or x_check_errors.
9112 Calling x_uncatch_errors resumes the normal error handling. */
9114 void
9115 x_catch_errors (Display *dpy)
9117 struct x_error_message_stack *data = xmalloc (sizeof *data);
9119 /* Make sure any errors from previous requests have been dealt with. */
9120 XSync (dpy, False);
9122 data->dpy = dpy;
9123 data->string[0] = 0;
9124 data->prev = x_error_message;
9125 x_error_message = data;
9128 /* Undo the last x_catch_errors call.
9129 DPY should be the display that was passed to x_catch_errors. */
9131 void
9132 x_uncatch_errors (void)
9134 struct x_error_message_stack *tmp;
9136 block_input ();
9138 /* The display may have been closed before this function is called.
9139 Check if it is still open before calling XSync. */
9140 if (x_display_info_for_display (x_error_message->dpy) != 0)
9141 XSync (x_error_message->dpy, False);
9143 tmp = x_error_message;
9144 x_error_message = x_error_message->prev;
9145 xfree (tmp);
9146 unblock_input ();
9149 /* If any X protocol errors have arrived since the last call to
9150 x_catch_errors or x_check_errors, signal an Emacs error using
9151 sprintf (a buffer, FORMAT, the x error message text) as the text. */
9153 void
9154 x_check_errors (Display *dpy, const char *format)
9156 /* Make sure to catch any errors incurred so far. */
9157 XSync (dpy, False);
9159 if (x_error_message->string[0])
9161 char string[X_ERROR_MESSAGE_SIZE];
9162 memcpy (string, x_error_message->string, X_ERROR_MESSAGE_SIZE);
9163 x_uncatch_errors ();
9164 error (format, string);
9168 /* Nonzero if we had any X protocol errors
9169 since we did x_catch_errors on DPY. */
9171 bool
9172 x_had_errors_p (Display *dpy)
9174 /* Make sure to catch any errors incurred so far. */
9175 XSync (dpy, False);
9177 return x_error_message->string[0] != 0;
9180 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
9182 void
9183 x_clear_errors (Display *dpy)
9185 x_error_message->string[0] = 0;
9188 #if false
9189 /* See comment in unwind_to_catch why calling this is a bad
9190 * idea. --lorentey */
9191 /* Close off all unclosed x_catch_errors calls. */
9193 void
9194 x_fully_uncatch_errors (void)
9196 while (x_error_message)
9197 x_uncatch_errors ();
9199 #endif
9201 #if false
9202 static unsigned int x_wire_count;
9203 x_trace_wire (void)
9205 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
9207 #endif
9210 /************************************************************************
9211 Handling X errors
9212 ************************************************************************/
9214 /* Error message passed to x_connection_closed. */
9216 static char *error_msg;
9218 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
9219 the text of an error message that lead to the connection loss. */
9221 static void
9222 x_connection_closed (Display *dpy, const char *error_message, bool ioerror)
9224 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
9225 Lisp_Object frame, tail;
9226 ptrdiff_t idx = SPECPDL_INDEX ();
9228 error_msg = alloca (strlen (error_message) + 1);
9229 strcpy (error_msg, error_message);
9231 /* Inhibit redisplay while frames are being deleted. */
9232 specbind (Qinhibit_redisplay, Qt);
9234 if (dpyinfo)
9236 /* Protect display from being closed when we delete the last
9237 frame on it. */
9238 dpyinfo->reference_count++;
9239 dpyinfo->terminal->reference_count++;
9241 if (ioerror) dpyinfo->display = 0;
9243 /* First delete frames whose mini-buffers are on frames
9244 that are on the dead display. */
9245 FOR_EACH_FRAME (tail, frame)
9247 Lisp_Object minibuf_frame;
9248 minibuf_frame
9249 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
9250 if (FRAME_X_P (XFRAME (frame))
9251 && FRAME_X_P (XFRAME (minibuf_frame))
9252 && ! EQ (frame, minibuf_frame)
9253 && FRAME_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
9254 delete_frame (frame, Qnoelisp);
9257 /* Now delete all remaining frames on the dead display.
9258 We are now sure none of these is used as the mini-buffer
9259 for another frame that we need to delete. */
9260 FOR_EACH_FRAME (tail, frame)
9261 if (FRAME_X_P (XFRAME (frame))
9262 && FRAME_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
9264 /* Set this to t so that delete_frame won't get confused
9265 trying to find a replacement. */
9266 kset_default_minibuffer_frame (FRAME_KBOARD (XFRAME (frame)), Qt);
9267 delete_frame (frame, Qnoelisp);
9270 /* If DPYINFO is null, this means we didn't open the display in the
9271 first place, so don't try to close it. */
9272 if (dpyinfo)
9274 /* We can not call XtCloseDisplay here because it calls XSync.
9275 XSync inside the error handler apparently hangs Emacs. On
9276 current Xt versions, this isn't needed either. */
9277 #ifdef USE_GTK
9278 /* A long-standing GTK bug prevents proper disconnect handling
9279 (https://bugzilla.gnome.org/show_bug.cgi?id=85715). Once,
9280 the resulting Glib error message loop filled a user's disk.
9281 To avoid this, kill Emacs unconditionally on disconnect. */
9282 shut_down_emacs (0, Qnil);
9283 fprintf (stderr, "%s\n\
9284 When compiled with GTK, Emacs cannot recover from X disconnects.\n\
9285 This is a GTK bug: https://bugzilla.gnome.org/show_bug.cgi?id=85715\n\
9286 For details, see etc/PROBLEMS.\n",
9287 error_msg);
9288 emacs_abort ();
9289 #endif /* USE_GTK */
9291 /* Indicate that this display is dead. */
9292 dpyinfo->display = 0;
9294 dpyinfo->reference_count--;
9295 dpyinfo->terminal->reference_count--;
9296 if (dpyinfo->reference_count != 0)
9297 /* We have just closed all frames on this display. */
9298 emacs_abort ();
9301 Lisp_Object tmp;
9302 XSETTERMINAL (tmp, dpyinfo->terminal);
9303 Fdelete_terminal (tmp, Qnoelisp);
9307 if (terminal_list == 0)
9309 fprintf (stderr, "%s\n", error_msg);
9310 Fkill_emacs (make_number (70));
9311 /* NOTREACHED */
9314 totally_unblock_input ();
9316 unbind_to (idx, Qnil);
9317 clear_waiting_for_input ();
9319 /* Tell GCC not to suggest attribute 'noreturn' for this function. */
9320 IF_LINT (if (! terminal_list) return; )
9322 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
9323 longjmp), because returning from this function would get us back into
9324 Xlib's code which will directly call `exit'. */
9325 error ("%s", error_msg);
9328 /* We specifically use it before defining it, so that gcc doesn't inline it,
9329 otherwise gdb doesn't know how to properly put a breakpoint on it. */
9330 static void x_error_quitter (Display *, XErrorEvent *);
9332 /* This is the first-level handler for X protocol errors.
9333 It calls x_error_quitter or x_error_catcher. */
9335 static int
9336 x_error_handler (Display *display, XErrorEvent *event)
9338 #if defined USE_GTK && defined HAVE_GTK3
9339 if ((event->error_code == BadMatch || event->error_code == BadWindow)
9340 && event->request_code == X_SetInputFocus)
9342 return 0;
9344 #endif
9346 if (x_error_message)
9347 x_error_catcher (display, event);
9348 else
9349 x_error_quitter (display, event);
9350 return 0;
9353 /* This is the usual handler for X protocol errors.
9354 It kills all frames on the display that we got the error for.
9355 If that was the only one, it prints an error message and kills Emacs. */
9357 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
9359 /* On older GCC versions, just putting x_error_quitter
9360 after x_error_handler prevents inlining into the former. */
9362 static void NO_INLINE
9363 x_error_quitter (Display *display, XErrorEvent *event)
9365 char buf[256], buf1[356];
9367 /* Ignore BadName errors. They can happen because of fonts
9368 or colors that are not defined. */
9370 if (event->error_code == BadName)
9371 return;
9373 /* Note that there is no real way portable across R3/R4 to get the
9374 original error handler. */
9376 XGetErrorText (display, event->error_code, buf, sizeof (buf));
9377 sprintf (buf1, "X protocol error: %s on protocol request %d",
9378 buf, event->request_code);
9379 x_connection_closed (display, buf1, false);
9383 /* This is the handler for X IO errors, always.
9384 It kills all frames on the display that we lost touch with.
9385 If that was the only one, it prints an error message and kills Emacs. */
9387 static int
9388 x_io_error_quitter (Display *display)
9390 char buf[256];
9392 snprintf (buf, sizeof buf, "Connection lost to X server `%s'",
9393 DisplayString (display));
9394 x_connection_closed (display, buf, true);
9395 return 0;
9398 /* Changing the font of the frame. */
9400 /* Give frame F the font FONT-OBJECT as its default font. The return
9401 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
9402 frame. If it is negative, generate a new fontset from
9403 FONT-OBJECT. */
9405 Lisp_Object
9406 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
9408 struct font *font = XFONT_OBJECT (font_object);
9409 int unit;
9411 if (fontset < 0)
9412 fontset = fontset_from_font (font_object);
9413 FRAME_FONTSET (f) = fontset;
9414 if (FRAME_FONT (f) == font)
9415 /* This font is already set in frame F. There's nothing more to
9416 do. */
9417 return font_object;
9419 FRAME_FONT (f) = font;
9420 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
9421 FRAME_COLUMN_WIDTH (f) = font->average_width;
9422 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (font);
9424 #ifndef USE_X_TOOLKIT
9425 FRAME_MENU_BAR_HEIGHT (f) = FRAME_MENU_BAR_LINES (f) * FRAME_LINE_HEIGHT (f);
9426 #endif
9428 /* Compute character columns occupied by scrollbar.
9430 Don't do things differently for non-toolkit scrollbars
9431 (Bug#17163). */
9432 unit = FRAME_COLUMN_WIDTH (f);
9433 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
9434 FRAME_CONFIG_SCROLL_BAR_COLS (f)
9435 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + unit - 1) / unit;
9436 else
9437 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
9439 if (FRAME_X_WINDOW (f) != 0)
9441 /* Don't change the size of a tip frame; there's no point in
9442 doing it because it's done in Fx_show_tip, and it leads to
9443 problems because the tip frame has no widget. */
9444 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
9445 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
9446 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 3,
9447 false, Qfont);
9450 #ifdef HAVE_X_I18N
9451 if (FRAME_XIC (f)
9452 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
9454 block_input ();
9455 xic_set_xfontset (f, SSDATA (fontset_ascii (fontset)));
9456 unblock_input ();
9458 #endif
9460 return font_object;
9464 /***********************************************************************
9465 X Input Methods
9466 ***********************************************************************/
9468 #ifdef HAVE_X_I18N
9470 #ifdef HAVE_X11R6
9472 /* XIM destroy callback function, which is called whenever the
9473 connection to input method XIM dies. CLIENT_DATA contains a
9474 pointer to the x_display_info structure corresponding to XIM. */
9476 static void
9477 xim_destroy_callback (XIM xim, XPointer client_data, XPointer call_data)
9479 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
9480 Lisp_Object frame, tail;
9482 block_input ();
9484 /* No need to call XDestroyIC.. */
9485 FOR_EACH_FRAME (tail, frame)
9487 struct frame *f = XFRAME (frame);
9488 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
9490 FRAME_XIC (f) = NULL;
9491 xic_free_xfontset (f);
9495 /* No need to call XCloseIM. */
9496 dpyinfo->xim = NULL;
9497 XFree (dpyinfo->xim_styles);
9498 unblock_input ();
9501 #endif /* HAVE_X11R6 */
9503 /* Open the connection to the XIM server on display DPYINFO.
9504 RESOURCE_NAME is the resource name Emacs uses. */
9506 static void
9507 xim_open_dpy (struct x_display_info *dpyinfo, char *resource_name)
9509 XIM xim;
9511 #ifdef HAVE_XIM
9512 if (use_xim)
9514 if (dpyinfo->xim)
9515 XCloseIM (dpyinfo->xim);
9516 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
9517 emacs_class);
9518 dpyinfo->xim = xim;
9520 if (xim)
9522 #ifdef HAVE_X11R6
9523 XIMCallback destroy;
9524 #endif
9526 /* Get supported styles and XIM values. */
9527 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
9529 #ifdef HAVE_X11R6
9530 destroy.callback = xim_destroy_callback;
9531 destroy.client_data = (XPointer)dpyinfo;
9532 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
9533 #endif
9537 else
9538 #endif /* HAVE_XIM */
9539 dpyinfo->xim = NULL;
9543 #ifdef HAVE_X11R6_XIM
9545 /* XIM instantiate callback function, which is called whenever an XIM
9546 server is available. DISPLAY is the display of the XIM.
9547 CLIENT_DATA contains a pointer to an xim_inst_t structure created
9548 when the callback was registered. */
9550 static void
9551 xim_instantiate_callback (Display *display, XPointer client_data, XPointer call_data)
9553 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
9554 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
9556 /* We don't support multiple XIM connections. */
9557 if (dpyinfo->xim)
9558 return;
9560 xim_open_dpy (dpyinfo, xim_inst->resource_name);
9562 /* Create XIC for the existing frames on the same display, as long
9563 as they have no XIC. */
9564 if (dpyinfo->xim && dpyinfo->reference_count > 0)
9566 Lisp_Object tail, frame;
9568 block_input ();
9569 FOR_EACH_FRAME (tail, frame)
9571 struct frame *f = XFRAME (frame);
9573 if (FRAME_X_P (f)
9574 && FRAME_DISPLAY_INFO (f) == xim_inst->dpyinfo)
9575 if (FRAME_XIC (f) == NULL)
9577 create_frame_xic (f);
9578 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
9579 xic_set_statusarea (f);
9580 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
9582 struct window *w = XWINDOW (f->selected_window);
9583 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
9588 unblock_input ();
9592 #endif /* HAVE_X11R6_XIM */
9595 /* Open a connection to the XIM server on display DPYINFO.
9596 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
9597 connection only at the first time. On X11R6, open the connection
9598 in the XIM instantiate callback function. */
9600 static void
9601 xim_initialize (struct x_display_info *dpyinfo, char *resource_name)
9603 dpyinfo->xim = NULL;
9604 #ifdef HAVE_XIM
9605 if (use_xim)
9607 #ifdef HAVE_X11R6_XIM
9608 struct xim_inst_t *xim_inst = xmalloc (sizeof *xim_inst);
9609 Bool ret;
9611 dpyinfo->xim_callback_data = xim_inst;
9612 xim_inst->dpyinfo = dpyinfo;
9613 xim_inst->resource_name = xstrdup (resource_name);
9614 ret = XRegisterIMInstantiateCallback
9615 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
9616 emacs_class, xim_instantiate_callback,
9617 /* This is XPointer in XFree86 but (XPointer *)
9618 on Tru64, at least, hence the configure test. */
9619 (XRegisterIMInstantiateCallback_arg6) xim_inst);
9620 eassert (ret == True);
9621 #else /* not HAVE_X11R6_XIM */
9622 xim_open_dpy (dpyinfo, resource_name);
9623 #endif /* not HAVE_X11R6_XIM */
9625 #endif /* HAVE_XIM */
9629 /* Close the connection to the XIM server on display DPYINFO. */
9631 static void
9632 xim_close_dpy (struct x_display_info *dpyinfo)
9634 #ifdef HAVE_XIM
9635 if (use_xim)
9637 #ifdef HAVE_X11R6_XIM
9638 struct xim_inst_t *xim_inst = dpyinfo->xim_callback_data;
9640 if (dpyinfo->display)
9642 Bool ret = XUnregisterIMInstantiateCallback
9643 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
9644 emacs_class, xim_instantiate_callback,
9645 (XRegisterIMInstantiateCallback_arg6) xim_inst);
9646 eassert (ret == True);
9648 xfree (xim_inst->resource_name);
9649 xfree (xim_inst);
9650 #endif /* HAVE_X11R6_XIM */
9651 if (dpyinfo->display)
9652 XCloseIM (dpyinfo->xim);
9653 dpyinfo->xim = NULL;
9654 XFree (dpyinfo->xim_styles);
9656 #endif /* HAVE_XIM */
9659 #endif /* not HAVE_X11R6_XIM */
9663 /* Calculate the absolute position in frame F
9664 from its current recorded position values and gravity. */
9666 static void
9667 x_calc_absolute_position (struct frame *f)
9669 int flags = f->size_hint_flags;
9671 /* We have nothing to do if the current position
9672 is already for the top-left corner. */
9673 if (! ((flags & XNegative) || (flags & YNegative)))
9674 return;
9676 /* Treat negative positions as relative to the leftmost bottommost
9677 position that fits on the screen. */
9678 if (flags & XNegative)
9679 f->left_pos = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
9680 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
9683 int height = FRAME_PIXEL_HEIGHT (f);
9685 #if defined USE_X_TOOLKIT && defined USE_MOTIF
9686 /* Something is fishy here. When using Motif, starting Emacs with
9687 `-g -0-0', the frame appears too low by a few pixels.
9689 This seems to be so because initially, while Emacs is starting,
9690 the column widget's height and the frame's pixel height are
9691 different. The column widget's height is the right one. In
9692 later invocations, when Emacs is up, the frame's pixel height
9693 is right, though.
9695 It's not obvious where the initial small difference comes from.
9696 2000-12-01, gerd. */
9698 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
9699 #endif
9701 if (flags & YNegative)
9702 f->top_pos = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
9703 - height + f->top_pos;
9706 /* The left_pos and top_pos
9707 are now relative to the top and left screen edges,
9708 so the flags should correspond. */
9709 f->size_hint_flags &= ~ (XNegative | YNegative);
9712 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
9713 to really change the position, and 0 when calling from
9714 x_make_frame_visible (in that case, XOFF and YOFF are the current
9715 position values). It is -1 when calling from x_set_frame_parameters,
9716 which means, do adjust for borders but don't change the gravity. */
9718 void
9719 x_set_offset (struct frame *f, register int xoff, register int yoff, int change_gravity)
9721 int modified_top, modified_left;
9723 if (change_gravity > 0)
9725 f->top_pos = yoff;
9726 f->left_pos = xoff;
9727 f->size_hint_flags &= ~ (XNegative | YNegative);
9728 if (xoff < 0)
9729 f->size_hint_flags |= XNegative;
9730 if (yoff < 0)
9731 f->size_hint_flags |= YNegative;
9732 f->win_gravity = NorthWestGravity;
9734 x_calc_absolute_position (f);
9736 block_input ();
9737 x_wm_set_size_hint (f, 0, false);
9739 modified_left = f->left_pos;
9740 modified_top = f->top_pos;
9742 if (change_gravity != 0 && FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
9744 /* Some WMs (twm, wmaker at least) has an offset that is smaller
9745 than the WM decorations. So we use the calculated offset instead
9746 of the WM decoration sizes here (x/y_pixels_outer_diff). */
9747 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
9748 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
9751 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9752 modified_left, modified_top);
9754 x_sync_with_move (f, f->left_pos, f->top_pos,
9755 FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN);
9757 /* change_gravity is non-zero when this function is called from Lisp to
9758 programmatically move a frame. In that case, we call
9759 x_check_expected_move to discover if we have a "Type A" or "Type B"
9760 window manager, and, for a "Type A" window manager, adjust the position
9761 of the frame.
9763 We call x_check_expected_move if a programmatic move occurred, and
9764 either the window manager type (A/B) is unknown or it is Type A but we
9765 need to compute the top/left offset adjustment for this frame. */
9767 if (change_gravity != 0
9768 && (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
9769 || (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
9770 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
9771 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
9772 x_check_expected_move (f, modified_left, modified_top);
9774 unblock_input ();
9777 /* Return true if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
9778 on the root window for frame F contains ATOMNAME.
9779 This is how a WM check shall be done according to the Window Manager
9780 Specification/Extended Window Manager Hints at
9781 http://freedesktop.org/wiki/Specifications/wm-spec. */
9783 static bool
9784 wm_supports (struct frame *f, Atom want_atom)
9786 Atom actual_type;
9787 unsigned long actual_size, bytes_remaining;
9788 int i, rc, actual_format;
9789 bool ret;
9790 Window wmcheck_window;
9791 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9792 Window target_window = dpyinfo->root_window;
9793 int max_len = 65536;
9794 Display *dpy = FRAME_X_DISPLAY (f);
9795 unsigned char *tmp_data = NULL;
9796 Atom target_type = XA_WINDOW;
9798 block_input ();
9800 x_catch_errors (dpy);
9801 rc = XGetWindowProperty (dpy, target_window,
9802 dpyinfo->Xatom_net_supporting_wm_check,
9803 0, max_len, False, target_type,
9804 &actual_type, &actual_format, &actual_size,
9805 &bytes_remaining, &tmp_data);
9807 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
9809 if (tmp_data) XFree (tmp_data);
9810 x_uncatch_errors ();
9811 unblock_input ();
9812 return false;
9815 wmcheck_window = *(Window *) tmp_data;
9816 XFree (tmp_data);
9818 /* Check if window exists. */
9819 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
9820 x_sync (f);
9821 if (x_had_errors_p (dpy))
9823 x_uncatch_errors ();
9824 unblock_input ();
9825 return false;
9828 if (dpyinfo->net_supported_window != wmcheck_window)
9830 /* Window changed, reload atoms */
9831 if (dpyinfo->net_supported_atoms != NULL)
9832 XFree (dpyinfo->net_supported_atoms);
9833 dpyinfo->net_supported_atoms = NULL;
9834 dpyinfo->nr_net_supported_atoms = 0;
9835 dpyinfo->net_supported_window = 0;
9837 target_type = XA_ATOM;
9838 tmp_data = NULL;
9839 rc = XGetWindowProperty (dpy, target_window,
9840 dpyinfo->Xatom_net_supported,
9841 0, max_len, False, target_type,
9842 &actual_type, &actual_format, &actual_size,
9843 &bytes_remaining, &tmp_data);
9845 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
9847 if (tmp_data) XFree (tmp_data);
9848 x_uncatch_errors ();
9849 unblock_input ();
9850 return false;
9853 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
9854 dpyinfo->nr_net_supported_atoms = actual_size;
9855 dpyinfo->net_supported_window = wmcheck_window;
9858 ret = false;
9860 for (i = 0; !ret && i < dpyinfo->nr_net_supported_atoms; ++i)
9861 ret = dpyinfo->net_supported_atoms[i] == want_atom;
9863 x_uncatch_errors ();
9864 unblock_input ();
9866 return ret;
9869 static void
9870 set_wm_state (Lisp_Object frame, bool add, Atom atom, Atom value)
9872 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (XFRAME (frame));
9874 x_send_client_event (frame, make_number (0), frame,
9875 dpyinfo->Xatom_net_wm_state,
9876 make_number (32),
9877 /* 1 = add, 0 = remove */
9878 Fcons
9879 (make_number (add),
9880 Fcons
9881 (make_fixnum_or_float (atom),
9882 (value != 0
9883 ? list1 (make_fixnum_or_float (value))
9884 : Qnil))));
9887 void
9888 x_set_sticky (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
9890 Lisp_Object frame;
9891 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9893 XSETFRAME (frame, f);
9895 set_wm_state (frame, !NILP (new_value),
9896 dpyinfo->Xatom_net_wm_state_sticky, None);
9899 /* Return the current _NET_WM_STATE.
9900 SIZE_STATE is set to one of the FULLSCREEN_* values.
9901 Set *STICKY to the sticky state.
9903 Return true iff we are not hidden. */
9905 static bool
9906 get_current_wm_state (struct frame *f,
9907 Window window,
9908 int *size_state,
9909 bool *sticky)
9911 Atom actual_type;
9912 unsigned long actual_size, bytes_remaining;
9913 int i, rc, actual_format;
9914 bool is_hidden = false;
9915 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9916 long max_len = 65536;
9917 Display *dpy = FRAME_X_DISPLAY (f);
9918 unsigned char *tmp_data = NULL;
9919 Atom target_type = XA_ATOM;
9921 *sticky = false;
9922 *size_state = FULLSCREEN_NONE;
9924 block_input ();
9925 x_catch_errors (dpy);
9926 rc = XGetWindowProperty (dpy, window, dpyinfo->Xatom_net_wm_state,
9927 0, max_len, False, target_type,
9928 &actual_type, &actual_format, &actual_size,
9929 &bytes_remaining, &tmp_data);
9931 if (rc != Success || actual_type != target_type || x_had_errors_p (dpy))
9933 if (tmp_data) XFree (tmp_data);
9934 x_uncatch_errors ();
9935 unblock_input ();
9936 return !FRAME_ICONIFIED_P (f);
9939 x_uncatch_errors ();
9941 for (i = 0; i < actual_size; ++i)
9943 Atom a = ((Atom*)tmp_data)[i];
9944 if (a == dpyinfo->Xatom_net_wm_state_hidden)
9945 is_hidden = true;
9946 else if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
9948 if (*size_state == FULLSCREEN_HEIGHT)
9949 *size_state = FULLSCREEN_MAXIMIZED;
9950 else
9951 *size_state = FULLSCREEN_WIDTH;
9953 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
9955 if (*size_state == FULLSCREEN_WIDTH)
9956 *size_state = FULLSCREEN_MAXIMIZED;
9957 else
9958 *size_state = FULLSCREEN_HEIGHT;
9960 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen)
9961 *size_state = FULLSCREEN_BOTH;
9962 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
9963 *sticky = true;
9966 if (tmp_data) XFree (tmp_data);
9967 unblock_input ();
9968 return ! is_hidden;
9971 /* Do fullscreen as specified in extended window manager hints */
9973 static bool
9974 do_ewmh_fullscreen (struct frame *f)
9976 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9977 bool have_net_atom = wm_supports (f, dpyinfo->Xatom_net_wm_state);
9978 int cur;
9979 bool dummy;
9981 get_current_wm_state (f, FRAME_OUTER_WINDOW (f), &cur, &dummy);
9983 /* Some window managers don't say they support _NET_WM_STATE, but they do say
9984 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
9985 if (!have_net_atom)
9986 have_net_atom = wm_supports (f, dpyinfo->Xatom_net_wm_state_fullscreen);
9988 if (have_net_atom && cur != f->want_fullscreen)
9990 Lisp_Object frame;
9992 XSETFRAME (frame, f);
9994 /* Keep number of calls to set_wm_state as low as possible.
9995 Some window managers, or possible Gtk+, hangs when too many
9996 are sent at once. */
9997 switch (f->want_fullscreen)
9999 case FULLSCREEN_BOTH:
10000 if (cur != FULLSCREEN_BOTH)
10001 set_wm_state (frame, true, dpyinfo->Xatom_net_wm_state_fullscreen,
10002 None);
10003 break;
10004 case FULLSCREEN_WIDTH:
10005 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_MAXIMIZED)
10007 set_wm_state (frame, false,
10008 dpyinfo->Xatom_net_wm_state_maximized_horz,
10009 dpyinfo->Xatom_net_wm_state_maximized_vert);
10010 set_wm_state (frame, true,
10011 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10013 else
10015 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_HEIGHT
10016 || cur == FULLSCREEN_MAXIMIZED)
10017 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10018 dpyinfo->Xatom_net_wm_state_maximized_vert);
10019 if (cur != FULLSCREEN_MAXIMIZED || x_frame_normalize_before_maximize)
10020 set_wm_state (frame, true,
10021 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10023 break;
10024 case FULLSCREEN_HEIGHT:
10025 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_MAXIMIZED)
10027 set_wm_state (frame, false,
10028 dpyinfo->Xatom_net_wm_state_maximized_horz,
10029 dpyinfo->Xatom_net_wm_state_maximized_vert);
10030 set_wm_state (frame, true,
10031 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10033 else
10035 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_WIDTH
10036 || cur == FULLSCREEN_MAXIMIZED)
10037 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10038 dpyinfo->Xatom_net_wm_state_maximized_horz);
10039 if (cur != FULLSCREEN_MAXIMIZED || x_frame_normalize_before_maximize)
10040 set_wm_state (frame, true,
10041 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10043 break;
10044 case FULLSCREEN_MAXIMIZED:
10045 if (x_frame_normalize_before_maximize && cur == FULLSCREEN_BOTH)
10047 set_wm_state (frame, false,
10048 dpyinfo->Xatom_net_wm_state_fullscreen, None);
10049 set_wm_state (frame, true,
10050 dpyinfo->Xatom_net_wm_state_maximized_horz,
10051 dpyinfo->Xatom_net_wm_state_maximized_vert);
10053 else if (x_frame_normalize_before_maximize && cur == FULLSCREEN_WIDTH)
10055 set_wm_state (frame, false,
10056 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10057 set_wm_state (frame, true,
10058 dpyinfo->Xatom_net_wm_state_maximized_horz,
10059 dpyinfo->Xatom_net_wm_state_maximized_vert);
10061 else if (x_frame_normalize_before_maximize && cur == FULLSCREEN_HEIGHT)
10063 set_wm_state (frame, false,
10064 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
10065 set_wm_state (frame, true,
10066 dpyinfo->Xatom_net_wm_state_maximized_horz,
10067 dpyinfo->Xatom_net_wm_state_maximized_vert);
10069 else
10071 if (cur == FULLSCREEN_BOTH)
10072 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10073 None);
10074 else if (cur == FULLSCREEN_HEIGHT)
10075 set_wm_state (frame, true,
10076 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
10077 else if (cur == FULLSCREEN_WIDTH)
10078 set_wm_state (frame, true, None,
10079 dpyinfo->Xatom_net_wm_state_maximized_vert);
10080 else
10081 set_wm_state (frame, true,
10082 dpyinfo->Xatom_net_wm_state_maximized_horz,
10083 dpyinfo->Xatom_net_wm_state_maximized_vert);
10085 break;
10086 case FULLSCREEN_NONE:
10087 if (cur == FULLSCREEN_BOTH)
10088 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
10089 None);
10090 else
10091 set_wm_state (frame, false,
10092 dpyinfo->Xatom_net_wm_state_maximized_horz,
10093 dpyinfo->Xatom_net_wm_state_maximized_vert);
10096 f->want_fullscreen = FULLSCREEN_NONE;
10100 return have_net_atom;
10103 static void
10104 XTfullscreen_hook (struct frame *f)
10106 if (FRAME_VISIBLE_P (f))
10108 block_input ();
10109 x_check_fullscreen (f);
10110 x_sync (f);
10111 unblock_input ();
10116 static bool
10117 x_handle_net_wm_state (struct frame *f, const XPropertyEvent *event)
10119 int value = FULLSCREEN_NONE;
10120 Lisp_Object lval;
10121 bool sticky = false;
10122 bool not_hidden = get_current_wm_state (f, event->window, &value, &sticky);
10124 lval = Qnil;
10125 switch (value)
10127 case FULLSCREEN_WIDTH:
10128 lval = Qfullwidth;
10129 break;
10130 case FULLSCREEN_HEIGHT:
10131 lval = Qfullheight;
10132 break;
10133 case FULLSCREEN_BOTH:
10134 lval = Qfullboth;
10135 break;
10136 case FULLSCREEN_MAXIMIZED:
10137 lval = Qmaximized;
10138 break;
10141 frame_size_history_add
10142 (f, Qx_handle_net_wm_state, 0, 0,
10143 list2 (get_frame_param (f, Qfullscreen), lval));
10145 store_frame_param (f, Qfullscreen, lval);
10146 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
10148 return not_hidden;
10151 /* Check if we need to resize the frame due to a fullscreen request.
10152 If so needed, resize the frame. */
10153 static void
10154 x_check_fullscreen (struct frame *f)
10156 if (do_ewmh_fullscreen (f))
10157 return;
10159 if (f->output_data.x->parent_desc != FRAME_DISPLAY_INFO (f)->root_window)
10160 return; /* Only fullscreen without WM or with EWM hints (above). */
10162 /* Setting fullscreen to nil doesn't do anything. We could save the
10163 last non-fullscreen size and restore it, but it seems like a
10164 lot of work for this unusual case (no window manager running). */
10166 if (f->want_fullscreen != FULLSCREEN_NONE)
10168 int width = FRAME_PIXEL_WIDTH (f), height = FRAME_PIXEL_HEIGHT (f);
10169 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10171 switch (f->want_fullscreen)
10173 /* No difference between these two when there is no WM */
10174 case FULLSCREEN_BOTH:
10175 case FULLSCREEN_MAXIMIZED:
10176 width = x_display_pixel_width (dpyinfo);
10177 height = x_display_pixel_height (dpyinfo);
10178 break;
10179 case FULLSCREEN_WIDTH:
10180 width = x_display_pixel_width (dpyinfo);
10181 height = height + FRAME_MENUBAR_HEIGHT (f);
10182 break;
10183 case FULLSCREEN_HEIGHT:
10184 height = x_display_pixel_height (dpyinfo);
10187 frame_size_history_add
10188 (f, Qx_check_fullscreen, width, height, Qnil);
10190 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10191 width, height);
10193 if (FRAME_VISIBLE_P (f))
10194 x_wait_for_event (f, ConfigureNotify);
10195 else
10197 change_frame_size (f, width, height - FRAME_MENUBAR_HEIGHT (f),
10198 false, true, false, true);
10199 x_sync (f);
10204 /* This function is called by x_set_offset to determine whether the window
10205 manager interfered with the positioning of the frame. Type A window
10206 managers position the surrounding window manager decorations a small
10207 amount above and left of the user-supplied position. Type B window
10208 managers position the surrounding window manager decorations at the
10209 user-specified position. If we detect a Type A window manager, we
10210 compensate by moving the window right and down by the proper amount. */
10212 static void
10213 x_check_expected_move (struct frame *f, int expected_left, int expected_top)
10215 int current_left = 0, current_top = 0;
10217 /* x_real_positions returns the left and top offsets of the outermost
10218 window manager window around the frame. */
10220 x_real_positions (f, &current_left, &current_top);
10222 if (current_left != expected_left || current_top != expected_top)
10224 /* It's a "Type A" window manager. */
10226 int adjusted_left;
10227 int adjusted_top;
10229 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
10230 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
10231 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
10233 /* Now fix the mispositioned frame's location. */
10235 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
10236 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
10238 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10239 adjusted_left, adjusted_top);
10241 x_sync_with_move (f, expected_left, expected_top, false);
10243 else
10244 /* It's a "Type B" window manager. We don't have to adjust the
10245 frame's position. */
10247 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
10251 /* Wait for XGetGeometry to return up-to-date position information for a
10252 recently-moved frame. Call this immediately after calling XMoveWindow.
10253 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
10254 frame has been moved to, so we use a fuzzy position comparison instead
10255 of an exact comparison. */
10257 static void
10258 x_sync_with_move (struct frame *f, int left, int top, bool fuzzy)
10260 int count = 0;
10262 while (count++ < 50)
10264 int current_left = 0, current_top = 0;
10266 /* In theory, this call to XSync only needs to happen once, but in
10267 practice, it doesn't seem to work, hence the need for the surrounding
10268 loop. */
10270 XSync (FRAME_X_DISPLAY (f), False);
10271 x_real_positions (f, &current_left, &current_top);
10273 if (fuzzy)
10275 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
10276 pixels. */
10278 if (eabs (current_left - left) <= 10
10279 && eabs (current_top - top) <= 40)
10280 return;
10282 else if (current_left == left && current_top == top)
10283 return;
10286 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
10287 will then return up-to-date position info. */
10289 wait_reading_process_output (0, 500000000, 0, false, Qnil, NULL, 0);
10293 /* Wait for an event on frame F matching EVENTTYPE. */
10294 void
10295 x_wait_for_event (struct frame *f, int eventtype)
10297 int level = interrupt_input_blocked;
10299 fd_set fds;
10300 struct timespec tmo, tmo_at, time_now;
10301 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
10303 f->wait_event_type = eventtype;
10305 /* Set timeout to 0.1 second. Hopefully not noticeable.
10306 Maybe it should be configurable. */
10307 tmo = make_timespec (0, 100 * 1000 * 1000);
10308 tmo_at = timespec_add (current_timespec (), tmo);
10310 while (f->wait_event_type)
10312 pending_signals = true;
10313 totally_unblock_input ();
10314 /* XTread_socket is called after unblock. */
10315 block_input ();
10316 interrupt_input_blocked = level;
10318 FD_ZERO (&fds);
10319 FD_SET (fd, &fds);
10321 time_now = current_timespec ();
10322 if (timespec_cmp (tmo_at, time_now) < 0)
10323 break;
10325 tmo = timespec_sub (tmo_at, time_now);
10326 if (pselect (fd + 1, &fds, NULL, NULL, &tmo, NULL) == 0)
10327 break; /* Timeout */
10330 f->wait_event_type = 0;
10334 /* Change the size of frame F's X window to WIDTH/HEIGHT in the case F
10335 doesn't have a widget. If CHANGE_GRAVITY, change to
10336 top-left-corner window gravity for this size change and subsequent
10337 size changes. Otherwise leave the window gravity unchanged. */
10339 static void
10340 x_set_window_size_1 (struct frame *f, bool change_gravity,
10341 int width, int height)
10343 int pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
10344 int pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
10345 int old_width = FRAME_PIXEL_WIDTH (f);
10346 int old_height = FRAME_PIXEL_HEIGHT (f);
10347 Lisp_Object fullscreen = get_frame_param (f, Qfullscreen);
10349 if (change_gravity) f->win_gravity = NorthWestGravity;
10350 x_wm_set_size_hint (f, 0, false);
10352 /* When the frame is fullheight and we only want to change the width
10353 or it is fullwidth and we only want to change the height we should
10354 be able to preserve the fullscreen property. However, due to the
10355 fact that we have to send a resize request anyway, the window
10356 manager will abolish it. At least the respective size should
10357 remain unchanged but giving the frame back its normal size will
10358 be broken ... */
10359 if (EQ (fullscreen, Qfullwidth) && width == FRAME_TEXT_WIDTH (f))
10361 frame_size_history_add
10362 (f, Qxg_frame_set_char_size_1, width, height,
10363 list2 (make_number (old_height),
10364 make_number (pixelheight + FRAME_MENUBAR_HEIGHT (f))));
10366 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10367 old_width, pixelheight + FRAME_MENUBAR_HEIGHT (f));
10369 else if (EQ (fullscreen, Qfullheight) && height == FRAME_TEXT_HEIGHT (f))
10371 frame_size_history_add
10372 (f, Qxg_frame_set_char_size_2, width, height,
10373 list2 (make_number (old_width), make_number (pixelwidth)));
10375 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10376 pixelwidth, old_height);
10379 else
10381 frame_size_history_add
10382 (f, Qxg_frame_set_char_size_3, width, height,
10383 list2 (make_number (pixelwidth + FRAME_TOOLBAR_WIDTH (f)),
10384 make_number (pixelheight + FRAME_TOOLBAR_HEIGHT (f)
10385 + FRAME_MENUBAR_HEIGHT (f))));
10387 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10388 pixelwidth, pixelheight + FRAME_MENUBAR_HEIGHT (f));
10389 fullscreen = Qnil;
10394 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
10395 receive in the ConfigureNotify event; if we get what we asked
10396 for, then the event won't cause the screen to become garbaged, so
10397 we have to make sure to do it here. */
10398 SET_FRAME_GARBAGED (f);
10400 /* Now, strictly speaking, we can't be sure that this is accurate,
10401 but the window manager will get around to dealing with the size
10402 change request eventually, and we'll hear how it went when the
10403 ConfigureNotify event gets here.
10405 We could just not bother storing any of this information here,
10406 and let the ConfigureNotify event set everything up, but that
10407 might be kind of confusing to the Lisp code, since size changes
10408 wouldn't be reported in the frame parameters until some random
10409 point in the future when the ConfigureNotify event arrives.
10411 Pass true for DELAY since we can't run Lisp code inside of
10412 a BLOCK_INPUT. */
10414 /* But the ConfigureNotify may in fact never arrive, and then this is
10415 not right if the frame is visible. Instead wait (with timeout)
10416 for the ConfigureNotify. */
10417 if (FRAME_VISIBLE_P (f))
10419 x_wait_for_event (f, ConfigureNotify);
10421 if (!NILP (fullscreen))
10422 /* Try to restore fullscreen state. */
10424 store_frame_param (f, Qfullscreen, fullscreen);
10425 x_set_fullscreen (f, fullscreen, fullscreen);
10428 else
10430 change_frame_size (f, width, height, false, true, false, true);
10431 x_sync (f);
10436 /* Call this to change the size of frame F's x-window.
10437 If CHANGE_GRAVITY, change to top-left-corner window gravity
10438 for this size change and subsequent size changes.
10439 Otherwise we leave the window gravity unchanged. */
10441 void
10442 x_set_window_size (struct frame *f, bool change_gravity,
10443 int width, int height, bool pixelwise)
10445 block_input ();
10447 /* The following breaks our calculations. If it's really needed,
10448 think of something else. */
10449 #if false
10450 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
10452 int text_width, text_height;
10454 /* When the frame is maximized/fullscreen or running under for
10455 example Xmonad, x_set_window_size_1 will be a no-op.
10456 In that case, the right thing to do is extend rows/width to
10457 the current frame size. We do that first if x_set_window_size_1
10458 turns out to not be a no-op (there is no way to know).
10459 The size will be adjusted again if the frame gets a
10460 ConfigureNotify event as a result of x_set_window_size. */
10461 int pixelh = FRAME_PIXEL_HEIGHT (f);
10462 #ifdef USE_X_TOOLKIT
10463 /* The menu bar is not part of text lines. The tool bar
10464 is however. */
10465 pixelh -= FRAME_MENUBAR_HEIGHT (f);
10466 #endif
10467 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, FRAME_PIXEL_WIDTH (f));
10468 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelh);
10470 change_frame_size (f, text_width, text_height, false, true, false, true);
10472 #endif
10474 /* Pixelize width and height, if necessary. */
10475 if (! pixelwise)
10477 width = width * FRAME_COLUMN_WIDTH (f);
10478 height = height * FRAME_LINE_HEIGHT (f);
10481 #ifdef USE_GTK
10482 if (FRAME_GTK_WIDGET (f))
10483 xg_frame_set_char_size (f, width, height);
10484 else
10485 x_set_window_size_1 (f, change_gravity, width, height);
10486 #else /* not USE_GTK */
10487 x_set_window_size_1 (f, change_gravity, width, height);
10488 x_clear_under_internal_border (f);
10489 #endif /* not USE_GTK */
10491 /* If cursor was outside the new size, mark it as off. */
10492 mark_window_cursors_off (XWINDOW (f->root_window));
10494 /* Clear out any recollection of where the mouse highlighting was,
10495 since it might be in a place that's outside the new frame size.
10496 Actually checking whether it is outside is a pain in the neck,
10497 so don't try--just let the highlighting be done afresh with new size. */
10498 cancel_mouse_face (f);
10500 unblock_input ();
10502 do_pending_window_change (false);
10505 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
10507 void
10508 frame_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
10510 block_input ();
10512 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
10513 0, 0, 0, 0, pix_x, pix_y);
10514 unblock_input ();
10517 /* Raise frame F. */
10519 void
10520 x_raise_frame (struct frame *f)
10522 block_input ();
10523 if (FRAME_VISIBLE_P (f))
10524 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
10525 XFlush (FRAME_X_DISPLAY (f));
10526 unblock_input ();
10529 /* Lower frame F. */
10531 static void
10532 x_lower_frame (struct frame *f)
10534 if (FRAME_VISIBLE_P (f))
10536 block_input ();
10537 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
10538 XFlush (FRAME_X_DISPLAY (f));
10539 unblock_input ();
10543 /* Request focus with XEmbed */
10545 void
10546 xembed_request_focus (struct frame *f)
10548 /* See XEmbed Protocol Specification at
10549 http://freedesktop.org/wiki/Specifications/xembed-spec */
10550 if (FRAME_VISIBLE_P (f))
10551 xembed_send_message (f, CurrentTime,
10552 XEMBED_REQUEST_FOCUS, 0, 0, 0);
10555 /* Activate frame with Extended Window Manager Hints */
10557 void
10558 x_ewmh_activate_frame (struct frame *f)
10560 /* See Window Manager Specification/Extended Window Manager Hints at
10561 http://freedesktop.org/wiki/Specifications/wm-spec */
10563 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10565 if (FRAME_VISIBLE_P (f) && wm_supports (f, dpyinfo->Xatom_net_active_window))
10567 Lisp_Object frame;
10568 XSETFRAME (frame, f);
10569 x_send_client_event (frame, make_number (0), frame,
10570 dpyinfo->Xatom_net_active_window,
10571 make_number (32),
10572 list2i (1, dpyinfo->last_user_time));
10576 static void
10577 XTframe_raise_lower (struct frame *f, bool raise_flag)
10579 if (raise_flag)
10580 x_raise_frame (f);
10581 else
10582 x_lower_frame (f);
10585 /* XEmbed implementation. */
10587 #if defined USE_X_TOOLKIT || ! defined USE_GTK
10589 /* XEmbed implementation. */
10591 #define XEMBED_VERSION 0
10593 static void
10594 xembed_set_info (struct frame *f, enum xembed_info flags)
10596 unsigned long data[2];
10597 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10599 data[0] = XEMBED_VERSION;
10600 data[1] = flags;
10602 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10603 dpyinfo->Xatom_XEMBED_INFO, dpyinfo->Xatom_XEMBED_INFO,
10604 32, PropModeReplace, (unsigned char *) data, 2);
10606 #endif /* defined USE_X_TOOLKIT || ! defined USE_GTK */
10608 static void
10609 xembed_send_message (struct frame *f, Time t, enum xembed_message msg,
10610 long int detail, long int data1, long int data2)
10612 XEvent event;
10614 event.xclient.type = ClientMessage;
10615 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
10616 event.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_XEMBED;
10617 event.xclient.format = 32;
10618 event.xclient.data.l[0] = t;
10619 event.xclient.data.l[1] = msg;
10620 event.xclient.data.l[2] = detail;
10621 event.xclient.data.l[3] = data1;
10622 event.xclient.data.l[4] = data2;
10624 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
10625 False, NoEventMask, &event);
10626 XSync (FRAME_X_DISPLAY (f), False);
10629 /* Change of visibility. */
10631 /* This tries to wait until the frame is really visible.
10632 However, if the window manager asks the user where to position
10633 the frame, this will return before the user finishes doing that.
10634 The frame will not actually be visible at that time,
10635 but it will become visible later when the window manager
10636 finishes with it. */
10638 void
10639 x_make_frame_visible (struct frame *f)
10641 int original_top, original_left;
10642 int tries = 0;
10644 block_input ();
10646 x_set_bitmap_icon (f);
10648 if (! FRAME_VISIBLE_P (f))
10650 /* We test FRAME_GARBAGED_P here to make sure we don't
10651 call x_set_offset a second time
10652 if we get to x_make_frame_visible a second time
10653 before the window gets really visible. */
10654 if (! FRAME_ICONIFIED_P (f)
10655 && ! FRAME_X_EMBEDDED_P (f)
10656 && ! f->output_data.x->asked_for_visible)
10657 x_set_offset (f, f->left_pos, f->top_pos, 0);
10659 f->output_data.x->asked_for_visible = true;
10661 if (! EQ (Vx_no_window_manager, Qt))
10662 x_wm_set_window_state (f, NormalState);
10663 #ifdef USE_X_TOOLKIT
10664 if (FRAME_X_EMBEDDED_P (f))
10665 xembed_set_info (f, XEMBED_MAPPED);
10666 else
10668 /* This was XtPopup, but that did nothing for an iconified frame. */
10669 XtMapWidget (f->output_data.x->widget);
10671 #else /* not USE_X_TOOLKIT */
10672 #ifdef USE_GTK
10673 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
10674 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
10675 #else
10676 if (FRAME_X_EMBEDDED_P (f))
10677 xembed_set_info (f, XEMBED_MAPPED);
10678 else
10679 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10680 #endif /* not USE_GTK */
10681 #endif /* not USE_X_TOOLKIT */
10684 XFlush (FRAME_X_DISPLAY (f));
10686 /* Synchronize to ensure Emacs knows the frame is visible
10687 before we do anything else. We do this loop with input not blocked
10688 so that incoming events are handled. */
10690 Lisp_Object frame;
10691 /* This must be before UNBLOCK_INPUT
10692 since events that arrive in response to the actions above
10693 will set it when they are handled. */
10694 bool previously_visible = f->output_data.x->has_been_visible;
10696 original_left = f->left_pos;
10697 original_top = f->top_pos;
10699 /* This must come after we set COUNT. */
10700 unblock_input ();
10702 /* We unblock here so that arriving X events are processed. */
10704 /* Now move the window back to where it was "supposed to be".
10705 But don't do it if the gravity is negative.
10706 When the gravity is negative, this uses a position
10707 that is 3 pixels too low. Perhaps that's really the border width.
10709 Don't do this if the window has never been visible before,
10710 because the window manager may choose the position
10711 and we don't want to override it. */
10713 if (! FRAME_VISIBLE_P (f)
10714 && ! FRAME_ICONIFIED_P (f)
10715 && ! FRAME_X_EMBEDDED_P (f)
10716 && f->win_gravity == NorthWestGravity
10717 && previously_visible)
10719 Drawable rootw;
10720 int x, y;
10721 unsigned int width, height, border, depth;
10723 block_input ();
10725 /* On some window managers (such as FVWM) moving an existing
10726 window, even to the same place, causes the window manager
10727 to introduce an offset. This can cause the window to move
10728 to an unexpected location. Check the geometry (a little
10729 slow here) and then verify that the window is in the right
10730 place. If the window is not in the right place, move it
10731 there, and take the potential window manager hit. */
10732 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10733 &rootw, &x, &y, &width, &height, &border, &depth);
10735 if (original_left != x || original_top != y)
10736 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
10737 original_left, original_top);
10739 unblock_input ();
10742 XSETFRAME (frame, f);
10744 /* Process X events until a MapNotify event has been seen. */
10745 while (!FRAME_VISIBLE_P (f))
10747 /* Force processing of queued events. */
10748 x_sync (f);
10750 /* If on another desktop, the deiconify/map may be ignored and the
10751 frame never becomes visible. XMonad does this.
10752 Prevent an endless loop. */
10753 if (FRAME_ICONIFIED_P (f) && ++tries > 100)
10754 break;
10756 /* This hack is still in use at least for Cygwin. See
10757 http://lists.gnu.org/archive/html/emacs-devel/2013-12/msg00351.html.
10759 Machines that do polling rather than SIGIO have been
10760 observed to go into a busy-wait here. So we'll fake an
10761 alarm signal to let the handler know that there's something
10762 to be read. We used to raise a real alarm, but it seems
10763 that the handler isn't always enabled here. This is
10764 probably a bug. */
10765 if (input_polling_used ())
10767 /* It could be confusing if a real alarm arrives while
10768 processing the fake one. Turn it off and let the
10769 handler reset it. */
10770 int old_poll_suppress_count = poll_suppress_count;
10771 poll_suppress_count = 1;
10772 poll_for_input_1 ();
10773 poll_suppress_count = old_poll_suppress_count;
10776 if (XPending (FRAME_X_DISPLAY (f)))
10778 XEvent xev;
10779 XNextEvent (FRAME_X_DISPLAY (f), &xev);
10780 x_dispatch_event (&xev, FRAME_X_DISPLAY (f));
10786 /* Change from mapped state to withdrawn state. */
10788 /* Make the frame visible (mapped and not iconified). */
10790 void
10791 x_make_frame_invisible (struct frame *f)
10793 Window window;
10795 /* Use the frame's outermost window, not the one we normally draw on. */
10796 window = FRAME_OUTER_WINDOW (f);
10798 /* Don't keep the highlight on an invisible frame. */
10799 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
10800 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
10802 block_input ();
10804 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
10805 that the current position of the window is user-specified, rather than
10806 program-specified, so that when the window is mapped again, it will be
10807 placed at the same location, without forcing the user to position it
10808 by hand again (they have already done that once for this window.) */
10809 x_wm_set_size_hint (f, 0, true);
10811 #ifdef USE_GTK
10812 if (FRAME_GTK_OUTER_WIDGET (f))
10813 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
10814 else
10815 #else
10816 if (FRAME_X_EMBEDDED_P (f))
10817 xembed_set_info (f, 0);
10818 else
10819 #endif
10822 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
10823 DefaultScreen (FRAME_X_DISPLAY (f))))
10825 unblock_input ();
10826 error ("Can't notify window manager of window withdrawal");
10830 /* We can't distinguish this from iconification
10831 just by the event that we get from the server.
10832 So we can't win using the usual strategy of letting
10833 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
10834 and synchronize with the server to make sure we agree. */
10835 SET_FRAME_VISIBLE (f, 0);
10836 SET_FRAME_ICONIFIED (f, false);
10838 x_sync (f);
10840 unblock_input ();
10843 /* Change window state from mapped to iconified. */
10845 void
10846 x_iconify_frame (struct frame *f)
10848 #ifdef USE_X_TOOLKIT
10849 int result;
10850 #endif
10852 /* Don't keep the highlight on an invisible frame. */
10853 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
10854 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
10856 if (FRAME_ICONIFIED_P (f))
10857 return;
10859 block_input ();
10861 x_set_bitmap_icon (f);
10863 #if defined (USE_GTK)
10864 if (FRAME_GTK_OUTER_WIDGET (f))
10866 if (! FRAME_VISIBLE_P (f))
10867 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
10869 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
10870 SET_FRAME_VISIBLE (f, 0);
10871 SET_FRAME_ICONIFIED (f, true);
10872 unblock_input ();
10873 return;
10875 #endif
10877 #ifdef USE_X_TOOLKIT
10879 if (! FRAME_VISIBLE_P (f))
10881 if (! EQ (Vx_no_window_manager, Qt))
10882 x_wm_set_window_state (f, IconicState);
10883 /* This was XtPopup, but that did nothing for an iconified frame. */
10884 XtMapWidget (f->output_data.x->widget);
10885 /* The server won't give us any event to indicate
10886 that an invisible frame was changed to an icon,
10887 so we have to record it here. */
10888 SET_FRAME_VISIBLE (f, 0);
10889 SET_FRAME_ICONIFIED (f, true);
10890 unblock_input ();
10891 return;
10894 result = XIconifyWindow (FRAME_X_DISPLAY (f),
10895 XtWindow (f->output_data.x->widget),
10896 DefaultScreen (FRAME_X_DISPLAY (f)));
10897 unblock_input ();
10899 if (!result)
10900 error ("Can't notify window manager of iconification");
10902 SET_FRAME_ICONIFIED (f, true);
10903 SET_FRAME_VISIBLE (f, 0);
10905 block_input ();
10906 XFlush (FRAME_X_DISPLAY (f));
10907 unblock_input ();
10908 #else /* not USE_X_TOOLKIT */
10910 /* Make sure the X server knows where the window should be positioned,
10911 in case the user deiconifies with the window manager. */
10912 if (! FRAME_VISIBLE_P (f)
10913 && ! FRAME_ICONIFIED_P (f)
10914 && ! FRAME_X_EMBEDDED_P (f))
10915 x_set_offset (f, f->left_pos, f->top_pos, 0);
10917 /* Since we don't know which revision of X we're running, we'll use both
10918 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
10920 /* X11R4: send a ClientMessage to the window manager using the
10921 WM_CHANGE_STATE type. */
10923 XEvent msg;
10925 msg.xclient.window = FRAME_X_WINDOW (f);
10926 msg.xclient.type = ClientMessage;
10927 msg.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_wm_change_state;
10928 msg.xclient.format = 32;
10929 msg.xclient.data.l[0] = IconicState;
10931 if (! XSendEvent (FRAME_X_DISPLAY (f),
10932 DefaultRootWindow (FRAME_X_DISPLAY (f)),
10933 False,
10934 SubstructureRedirectMask | SubstructureNotifyMask,
10935 &msg))
10937 unblock_input ();
10938 error ("Can't notify window manager of iconification");
10942 /* X11R3: set the initial_state field of the window manager hints to
10943 IconicState. */
10944 x_wm_set_window_state (f, IconicState);
10946 if (!FRAME_VISIBLE_P (f))
10948 /* If the frame was withdrawn, before, we must map it. */
10949 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10952 SET_FRAME_ICONIFIED (f, true);
10953 SET_FRAME_VISIBLE (f, 0);
10955 XFlush (FRAME_X_DISPLAY (f));
10956 unblock_input ();
10957 #endif /* not USE_X_TOOLKIT */
10961 /* Free X resources of frame F. */
10963 void
10964 x_free_frame_resources (struct frame *f)
10966 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10967 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
10968 #ifdef USE_X_TOOLKIT
10969 Lisp_Object bar;
10970 struct scroll_bar *b;
10971 #endif
10973 block_input ();
10975 /* If a display connection is dead, don't try sending more
10976 commands to the X server. */
10977 if (dpyinfo->display)
10979 /* Always exit with visible pointer to avoid weird issue
10980 with Xfixes (Bug#17609). */
10981 if (f->pointer_invisible)
10982 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, 0);
10984 /* We must free faces before destroying windows because some
10985 font-driver (e.g. xft) access a window while finishing a
10986 face. */
10987 free_frame_faces (f);
10989 if (f->output_data.x->icon_desc)
10990 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
10992 #ifdef USE_X_TOOLKIT
10993 /* Explicitly destroy the scroll bars of the frame. Without
10994 this, we get "BadDrawable" errors from the toolkit later on,
10995 presumably from expose events generated for the disappearing
10996 toolkit scroll bars. */
10997 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
10999 b = XSCROLL_BAR (bar);
11000 x_scroll_bar_remove (b);
11002 #endif
11004 #ifdef HAVE_X_I18N
11005 if (FRAME_XIC (f))
11006 free_frame_xic (f);
11007 #endif
11009 x_free_cr_resources (f);
11010 #ifdef USE_X_TOOLKIT
11011 if (f->output_data.x->widget)
11013 XtDestroyWidget (f->output_data.x->widget);
11014 f->output_data.x->widget = NULL;
11016 /* Tooltips don't have widgets, only a simple X window, even if
11017 we are using a toolkit. */
11018 else if (FRAME_X_WINDOW (f))
11019 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11021 free_frame_menubar (f);
11023 if (f->shell_position)
11024 xfree (f->shell_position);
11025 #else /* !USE_X_TOOLKIT */
11027 #ifdef USE_GTK
11028 xg_free_frame_widgets (f);
11029 #endif /* USE_GTK */
11031 if (FRAME_X_WINDOW (f))
11032 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11033 #endif /* !USE_X_TOOLKIT */
11035 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
11036 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
11037 unload_color (f, f->output_data.x->cursor_pixel);
11038 unload_color (f, f->output_data.x->cursor_foreground_pixel);
11039 unload_color (f, f->output_data.x->border_pixel);
11040 unload_color (f, f->output_data.x->mouse_pixel);
11042 if (f->output_data.x->scroll_bar_background_pixel != -1)
11043 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
11044 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
11045 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
11046 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
11047 /* Scrollbar shadow colors. */
11048 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
11049 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
11050 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
11051 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
11052 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
11053 if (f->output_data.x->white_relief.pixel != -1)
11054 unload_color (f, f->output_data.x->white_relief.pixel);
11055 if (f->output_data.x->black_relief.pixel != -1)
11056 unload_color (f, f->output_data.x->black_relief.pixel);
11058 x_free_gcs (f);
11060 /* Free extra GCs allocated by x_setup_relief_colors. */
11061 if (f->output_data.x->white_relief.gc)
11063 XFreeGC (dpyinfo->display, f->output_data.x->white_relief.gc);
11064 f->output_data.x->white_relief.gc = 0;
11066 if (f->output_data.x->black_relief.gc)
11068 XFreeGC (dpyinfo->display, f->output_data.x->black_relief.gc);
11069 f->output_data.x->black_relief.gc = 0;
11072 /* Free cursors. */
11073 if (f->output_data.x->text_cursor != 0)
11074 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->text_cursor);
11075 if (f->output_data.x->nontext_cursor != 0)
11076 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->nontext_cursor);
11077 if (f->output_data.x->modeline_cursor != 0)
11078 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->modeline_cursor);
11079 if (f->output_data.x->hand_cursor != 0)
11080 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hand_cursor);
11081 if (f->output_data.x->hourglass_cursor != 0)
11082 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hourglass_cursor);
11083 if (f->output_data.x->horizontal_drag_cursor != 0)
11084 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->horizontal_drag_cursor);
11085 if (f->output_data.x->vertical_drag_cursor != 0)
11086 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->vertical_drag_cursor);
11088 XFlush (FRAME_X_DISPLAY (f));
11091 xfree (f->output_data.x->saved_menu_event);
11092 xfree (f->output_data.x);
11093 f->output_data.x = NULL;
11095 if (f == dpyinfo->x_focus_frame)
11096 dpyinfo->x_focus_frame = 0;
11097 if (f == dpyinfo->x_focus_event_frame)
11098 dpyinfo->x_focus_event_frame = 0;
11099 if (f == dpyinfo->x_highlight_frame)
11100 dpyinfo->x_highlight_frame = 0;
11101 if (f == hlinfo->mouse_face_mouse_frame)
11102 reset_mouse_highlight (hlinfo);
11104 unblock_input ();
11108 /* Destroy the X window of frame F. */
11110 static void
11111 x_destroy_window (struct frame *f)
11113 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
11115 /* If a display connection is dead, don't try sending more
11116 commands to the X server. */
11117 if (dpyinfo->display != 0)
11118 x_free_frame_resources (f);
11120 dpyinfo->reference_count--;
11124 /* Setting window manager hints. */
11126 /* Set the normal size hints for the window manager, for frame F.
11127 FLAGS is the flags word to use--or 0 meaning preserve the flags
11128 that the window now has.
11129 If USER_POSITION, set the USPosition
11130 flag (this is useful when FLAGS is 0).
11131 The GTK version is in gtkutils.c. */
11133 #ifndef USE_GTK
11134 void
11135 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
11137 XSizeHints size_hints;
11138 Window window = FRAME_OUTER_WINDOW (f);
11140 if (!window)
11141 return;
11143 #ifdef USE_X_TOOLKIT
11144 if (f->output_data.x->widget)
11146 widget_update_wm_size_hints (f->output_data.x->widget);
11147 return;
11149 #endif
11151 /* Setting PMaxSize caused various problems. */
11152 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
11154 size_hints.x = f->left_pos;
11155 size_hints.y = f->top_pos;
11157 size_hints.height = FRAME_PIXEL_HEIGHT (f);
11158 size_hints.width = FRAME_PIXEL_WIDTH (f);
11160 size_hints.width_inc = frame_resize_pixelwise ? 1 : FRAME_COLUMN_WIDTH (f);
11161 size_hints.height_inc = frame_resize_pixelwise ? 1 : FRAME_LINE_HEIGHT (f);
11163 size_hints.max_width = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
11164 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
11165 size_hints.max_height = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
11166 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
11168 /* Calculate the base and minimum sizes. */
11170 int base_width, base_height;
11171 int min_rows = 0, min_cols = 0;
11173 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
11174 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
11176 if (frame_resize_pixelwise)
11177 /* Needed to prevent a bad protocol error crash when making the
11178 frame size very small. */
11180 min_cols = 2 * min_cols;
11181 min_rows = 2 * min_rows;
11184 /* The window manager uses the base width hints to calculate the
11185 current number of rows and columns in the frame while
11186 resizing; min_width and min_height aren't useful for this
11187 purpose, since they might not give the dimensions for a
11188 zero-row, zero-column frame.
11190 We use the base_width and base_height members if we have
11191 them; otherwise, we set the min_width and min_height members
11192 to the size for a zero x zero frame. */
11194 size_hints.flags |= PBaseSize;
11195 size_hints.base_width = base_width;
11196 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
11197 size_hints.min_width = base_width + min_cols * FRAME_COLUMN_WIDTH (f);
11198 size_hints.min_height = base_height + min_rows * FRAME_LINE_HEIGHT (f);
11201 /* If we don't need the old flags, we don't need the old hint at all. */
11202 if (flags)
11204 size_hints.flags |= flags;
11205 goto no_read;
11209 XSizeHints hints; /* Sometimes I hate X Windows... */
11210 long supplied_return;
11211 int value;
11213 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
11214 &supplied_return);
11216 if (flags)
11217 size_hints.flags |= flags;
11218 else
11220 if (value == 0)
11221 hints.flags = 0;
11222 if (hints.flags & PSize)
11223 size_hints.flags |= PSize;
11224 if (hints.flags & PPosition)
11225 size_hints.flags |= PPosition;
11226 if (hints.flags & USPosition)
11227 size_hints.flags |= USPosition;
11228 if (hints.flags & USSize)
11229 size_hints.flags |= USSize;
11233 no_read:
11235 #ifdef PWinGravity
11236 size_hints.win_gravity = f->win_gravity;
11237 size_hints.flags |= PWinGravity;
11239 if (user_position)
11241 size_hints.flags &= ~ PPosition;
11242 size_hints.flags |= USPosition;
11244 #endif /* PWinGravity */
11246 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
11248 #endif /* not USE_GTK */
11250 /* Used for IconicState or NormalState */
11252 static void
11253 x_wm_set_window_state (struct frame *f, int state)
11255 #ifdef USE_X_TOOLKIT
11256 Arg al[1];
11258 XtSetArg (al[0], XtNinitialState, state);
11259 XtSetValues (f->output_data.x->widget, al, 1);
11260 #else /* not USE_X_TOOLKIT */
11261 Window window = FRAME_X_WINDOW (f);
11263 f->output_data.x->wm_hints.flags |= StateHint;
11264 f->output_data.x->wm_hints.initial_state = state;
11266 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
11267 #endif /* not USE_X_TOOLKIT */
11270 static void
11271 x_wm_set_icon_pixmap (struct frame *f, ptrdiff_t pixmap_id)
11273 Pixmap icon_pixmap, icon_mask;
11275 #if !defined USE_X_TOOLKIT && !defined USE_GTK
11276 Window window = FRAME_OUTER_WINDOW (f);
11277 #endif
11279 if (pixmap_id > 0)
11281 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
11282 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
11283 icon_mask = x_bitmap_mask (f, pixmap_id);
11284 f->output_data.x->wm_hints.icon_mask = icon_mask;
11286 else
11288 /* It seems there is no way to turn off use of an icon
11289 pixmap. */
11290 return;
11294 #ifdef USE_GTK
11296 xg_set_frame_icon (f, icon_pixmap, icon_mask);
11297 return;
11300 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
11303 Arg al[1];
11304 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
11305 XtSetValues (f->output_data.x->widget, al, 1);
11306 XtSetArg (al[0], XtNiconMask, icon_mask);
11307 XtSetValues (f->output_data.x->widget, al, 1);
11310 #else /* not USE_X_TOOLKIT && not USE_GTK */
11312 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
11313 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
11315 #endif /* not USE_X_TOOLKIT && not USE_GTK */
11318 void
11319 x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
11321 Window window = FRAME_OUTER_WINDOW (f);
11323 f->output_data.x->wm_hints.flags |= IconPositionHint;
11324 f->output_data.x->wm_hints.icon_x = icon_x;
11325 f->output_data.x->wm_hints.icon_y = icon_y;
11327 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
11331 /***********************************************************************
11332 Fonts
11333 ***********************************************************************/
11335 #ifdef GLYPH_DEBUG
11337 /* Check that FONT is valid on frame F. It is if it can be found in F's
11338 font table. */
11340 static void
11341 x_check_font (struct frame *f, struct font *font)
11343 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
11344 if (font->driver->check)
11345 eassert (font->driver->check (f, font) == 0);
11348 #endif /* GLYPH_DEBUG */
11351 /***********************************************************************
11352 Initialization
11353 ***********************************************************************/
11355 #ifdef USE_X_TOOLKIT
11356 static XrmOptionDescRec emacs_options[] = {
11357 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
11358 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
11360 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
11361 XrmoptionSepArg, NULL},
11362 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
11364 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
11365 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
11366 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
11367 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
11368 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
11369 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
11370 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
11373 /* Whether atimer for Xt timeouts is activated or not. */
11375 static bool x_timeout_atimer_activated_flag;
11377 #endif /* USE_X_TOOLKIT */
11379 static int x_initialized;
11381 /* Test whether two display-name strings agree up to the dot that separates
11382 the screen number from the server number. */
11383 static bool
11384 same_x_server (const char *name1, const char *name2)
11386 bool seen_colon = false;
11387 Lisp_Object sysname = Fsystem_name ();
11388 const char *system_name = SSDATA (sysname);
11389 ptrdiff_t system_name_length = SBYTES (sysname);
11390 ptrdiff_t length_until_period = 0;
11392 while (system_name[length_until_period] != 0
11393 && system_name[length_until_period] != '.')
11394 length_until_period++;
11396 /* Treat `unix' like an empty host name. */
11397 if (! strncmp (name1, "unix:", 5))
11398 name1 += 4;
11399 if (! strncmp (name2, "unix:", 5))
11400 name2 += 4;
11401 /* Treat this host's name like an empty host name. */
11402 if (! strncmp (name1, system_name, system_name_length)
11403 && name1[system_name_length] == ':')
11404 name1 += system_name_length;
11405 if (! strncmp (name2, system_name, system_name_length)
11406 && name2[system_name_length] == ':')
11407 name2 += system_name_length;
11408 /* Treat this host's domainless name like an empty host name. */
11409 if (! strncmp (name1, system_name, length_until_period)
11410 && name1[length_until_period] == ':')
11411 name1 += length_until_period;
11412 if (! strncmp (name2, system_name, length_until_period)
11413 && name2[length_until_period] == ':')
11414 name2 += length_until_period;
11416 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
11418 if (*name1 == ':')
11419 seen_colon = true;
11420 if (seen_colon && *name1 == '.')
11421 return true;
11423 return (seen_colon
11424 && (*name1 == '.' || *name1 == '\0')
11425 && (*name2 == '.' || *name2 == '\0'));
11428 /* Count number of set bits in mask and number of bits to shift to
11429 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
11430 to 5. */
11431 static void
11432 get_bits_and_offset (unsigned long mask, int *bits, int *offset)
11434 int nr = 0;
11435 int off = 0;
11437 while (!(mask & 1))
11439 off++;
11440 mask >>= 1;
11443 while (mask & 1)
11445 nr++;
11446 mask >>= 1;
11449 *offset = off;
11450 *bits = nr;
11453 /* Return true iff display DISPLAY is available for use.
11454 But don't permanently open it, just test its availability. */
11456 bool
11457 x_display_ok (const char *display)
11459 /* XOpenDisplay fails if it gets a signal. Block SIGIO which may arrive. */
11460 unrequest_sigio ();
11461 Display *dpy = XOpenDisplay (display);
11462 request_sigio ();
11463 if (!dpy)
11464 return false;
11465 XCloseDisplay (dpy);
11466 return true;
11469 #ifdef USE_GTK
11470 static void
11471 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
11472 const gchar *msg, gpointer user_data)
11474 if (!strstr (msg, "g_set_prgname"))
11475 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, msg);
11477 #endif
11479 /* Create invisible cursor on X display referred by DPYINFO. */
11481 static Cursor
11482 make_invisible_cursor (struct x_display_info *dpyinfo)
11484 Display *dpy = dpyinfo->display;
11485 static char const no_data[] = { 0 };
11486 Pixmap pix;
11487 XColor col;
11488 Cursor c = 0;
11490 x_catch_errors (dpy);
11491 pix = XCreateBitmapFromData (dpy, dpyinfo->root_window, no_data, 1, 1);
11492 if (! x_had_errors_p (dpy) && pix != None)
11494 Cursor pixc;
11495 col.pixel = 0;
11496 col.red = col.green = col.blue = 0;
11497 col.flags = DoRed | DoGreen | DoBlue;
11498 pixc = XCreatePixmapCursor (dpy, pix, pix, &col, &col, 0, 0);
11499 if (! x_had_errors_p (dpy) && pixc != None)
11500 c = pixc;
11501 XFreePixmap (dpy, pix);
11504 x_uncatch_errors ();
11506 return c;
11509 /* True if DPY supports Xfixes extension >= 4. */
11511 static bool
11512 x_probe_xfixes_extension (Display *dpy)
11514 #ifdef HAVE_XFIXES
11515 int major, minor;
11516 return XFixesQueryVersion (dpy, &major, &minor) && major >= 4;
11517 #else
11518 return false;
11519 #endif /* HAVE_XFIXES */
11522 /* Toggle mouse pointer visibility on frame F by using Xfixes functions. */
11524 static void
11525 xfixes_toggle_visible_pointer (struct frame *f, bool invisible)
11527 #ifdef HAVE_XFIXES
11528 if (invisible)
11529 XFixesHideCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11530 else
11531 XFixesShowCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11532 f->pointer_invisible = invisible;
11533 #else
11534 emacs_abort ();
11535 #endif /* HAVE_XFIXES */
11538 /* Toggle mouse pointer visibility on frame F by using invisible cursor. */
11540 static void
11541 x_toggle_visible_pointer (struct frame *f, bool invisible)
11543 eassert (FRAME_DISPLAY_INFO (f)->invisible_cursor != 0);
11544 if (invisible)
11545 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11546 FRAME_DISPLAY_INFO (f)->invisible_cursor);
11547 else
11548 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11549 f->output_data.x->current_cursor);
11550 f->pointer_invisible = invisible;
11553 /* Setup pointer blanking, prefer Xfixes if available. */
11555 static void
11556 x_setup_pointer_blanking (struct x_display_info *dpyinfo)
11558 /* FIXME: the brave tester should set EMACS_XFIXES because we're suspecting
11559 X server bug, see http://debbugs.gnu.org/cgi/bugreport.cgi?bug=17609. */
11560 if (egetenv ("EMACS_XFIXES") && x_probe_xfixes_extension (dpyinfo->display))
11561 dpyinfo->toggle_visible_pointer = xfixes_toggle_visible_pointer;
11562 else
11564 dpyinfo->toggle_visible_pointer = x_toggle_visible_pointer;
11565 dpyinfo->invisible_cursor = make_invisible_cursor (dpyinfo);
11569 /* Current X display connection identifier. Incremented for each next
11570 connection established. */
11571 static unsigned x_display_id;
11573 /* Open a connection to X display DISPLAY_NAME, and return
11574 the structure that describes the open display.
11575 If we cannot contact the display, return null. */
11577 struct x_display_info *
11578 x_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
11580 Display *dpy;
11581 struct terminal *terminal;
11582 struct x_display_info *dpyinfo;
11583 XrmDatabase xrdb;
11584 ptrdiff_t lim;
11586 block_input ();
11588 if (!x_initialized)
11590 x_initialize ();
11591 ++x_initialized;
11594 if (! x_display_ok (SSDATA (display_name)))
11595 error ("Display %s can't be opened", SSDATA (display_name));
11597 #ifdef USE_GTK
11599 #define NUM_ARGV 10
11600 int argc;
11601 char *argv[NUM_ARGV];
11602 char **argv2 = argv;
11603 guint id;
11605 if (x_initialized++ > 1)
11607 xg_display_open (SSDATA (display_name), &dpy);
11609 else
11611 static char display_opt[] = "--display";
11612 static char name_opt[] = "--name";
11614 for (argc = 0; argc < NUM_ARGV; ++argc)
11615 argv[argc] = 0;
11617 argc = 0;
11618 argv[argc++] = initial_argv[0];
11620 if (! NILP (display_name))
11622 argv[argc++] = display_opt;
11623 argv[argc++] = SSDATA (display_name);
11626 argv[argc++] = name_opt;
11627 argv[argc++] = resource_name;
11629 XSetLocaleModifiers ("");
11631 /* Work around GLib bug that outputs a faulty warning. See
11632 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
11633 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
11634 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
11636 /* NULL window -> events for all windows go to our function.
11637 Call before gtk_init so Gtk+ event filters comes after our. */
11638 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
11640 /* gtk_init does set_locale. Fix locale before and after. */
11641 fixup_locale ();
11642 unrequest_sigio (); /* See comment in x_display_ok. */
11643 gtk_init (&argc, &argv2);
11644 request_sigio ();
11645 fixup_locale ();
11647 g_log_remove_handler ("GLib", id);
11649 xg_initialize ();
11651 dpy = DEFAULT_GDK_DISPLAY ();
11653 #if ! GTK_CHECK_VERSION (2, 90, 0)
11654 /* Load our own gtkrc if it exists. */
11656 const char *file = "~/.emacs.d/gtkrc";
11657 Lisp_Object s, abs_file;
11659 s = build_string (file);
11660 abs_file = Fexpand_file_name (s, Qnil);
11662 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
11663 gtk_rc_parse (SSDATA (abs_file));
11665 #endif
11667 XSetErrorHandler (x_error_handler);
11668 XSetIOErrorHandler (x_io_error_quitter);
11671 #else /* not USE_GTK */
11672 #ifdef USE_X_TOOLKIT
11673 /* weiner@footloose.sps.mot.com reports that this causes
11674 errors with X11R5:
11675 X protocol error: BadAtom (invalid Atom parameter)
11676 on protocol request 18skiloaf.
11677 So let's not use it until R6. */
11678 #ifdef HAVE_X11XTR6
11679 XtSetLanguageProc (NULL, NULL, NULL);
11680 #endif
11683 int argc = 0;
11684 char *argv[3];
11686 argv[0] = "";
11687 argc = 1;
11688 if (xrm_option)
11690 argv[argc++] = "-xrm";
11691 argv[argc++] = xrm_option;
11693 turn_on_atimers (false);
11694 unrequest_sigio (); /* See comment in x_display_ok. */
11695 dpy = XtOpenDisplay (Xt_app_con, SSDATA (display_name),
11696 resource_name, EMACS_CLASS,
11697 emacs_options, XtNumber (emacs_options),
11698 &argc, argv);
11699 request_sigio ();
11700 turn_on_atimers (true);
11702 #ifdef HAVE_X11XTR6
11703 /* I think this is to compensate for XtSetLanguageProc. */
11704 fixup_locale ();
11705 #endif
11708 #else /* not USE_X_TOOLKIT */
11709 XSetLocaleModifiers ("");
11710 unrequest_sigio (); // See comment in x_display_ok.
11711 dpy = XOpenDisplay (SSDATA (display_name));
11712 request_sigio ();
11713 #endif /* not USE_X_TOOLKIT */
11714 #endif /* not USE_GTK*/
11716 /* Detect failure. */
11717 if (dpy == 0)
11719 unblock_input ();
11720 return 0;
11723 /* We have definitely succeeded. Record the new connection. */
11725 dpyinfo = xzalloc (sizeof *dpyinfo);
11726 terminal = x_create_terminal (dpyinfo);
11729 struct x_display_info *share;
11731 for (share = x_display_list; share; share = share->next)
11732 if (same_x_server (SSDATA (XCAR (share->name_list_element)),
11733 SSDATA (display_name)))
11734 break;
11735 if (share)
11736 terminal->kboard = share->terminal->kboard;
11737 else
11739 terminal->kboard = allocate_kboard (Qx);
11741 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
11743 char *vendor = ServerVendor (dpy);
11745 /* Protect terminal from GC before removing it from the
11746 list of terminals. */
11747 struct gcpro gcpro1;
11748 Lisp_Object gcpro_term;
11749 XSETTERMINAL (gcpro_term, terminal);
11750 GCPRO1 (gcpro_term);
11752 /* Temporarily hide the partially initialized terminal. */
11753 terminal_list = terminal->next_terminal;
11754 unblock_input ();
11755 kset_system_key_alist
11756 (terminal->kboard,
11757 call1 (Qvendor_specific_keysyms,
11758 vendor ? build_string (vendor) : empty_unibyte_string));
11759 block_input ();
11760 terminal->next_terminal = terminal_list;
11761 terminal_list = terminal;
11762 UNGCPRO;
11765 /* Don't let the initial kboard remain current longer than necessary.
11766 That would cause problems if a file loaded on startup tries to
11767 prompt in the mini-buffer. */
11768 if (current_kboard == initial_kboard)
11769 current_kboard = terminal->kboard;
11771 terminal->kboard->reference_count++;
11774 /* Put this display on the chain. */
11775 dpyinfo->next = x_display_list;
11776 x_display_list = dpyinfo;
11778 dpyinfo->name_list_element = Fcons (display_name, Qnil);
11779 dpyinfo->display = dpy;
11780 dpyinfo->connection = ConnectionNumber (dpyinfo->display);
11782 /* Set the name of the terminal. */
11783 terminal->name = xlispstrdup (display_name);
11785 #if false
11786 XSetAfterFunction (x_current_display, x_trace_wire);
11787 #endif
11789 lim = min (PTRDIFF_MAX, SIZE_MAX) - sizeof "@";
11790 Lisp_Object system_name = Fsystem_name ();
11791 if (lim - SBYTES (Vinvocation_name) < SBYTES (system_name))
11792 memory_full (SIZE_MAX);
11793 dpyinfo->x_id = ++x_display_id;
11794 dpyinfo->x_id_name = xmalloc (SBYTES (Vinvocation_name)
11795 + SBYTES (system_name) + 2);
11796 char *nametail = lispstpcpy (dpyinfo->x_id_name, Vinvocation_name);
11797 *nametail++ = '@';
11798 lispstpcpy (nametail, system_name);
11800 /* Figure out which modifier bits mean what. */
11801 x_find_modifier_meanings (dpyinfo);
11803 /* Get the scroll bar cursor. */
11804 #ifdef USE_GTK
11805 /* We must create a GTK cursor, it is required for GTK widgets. */
11806 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
11807 #endif /* USE_GTK */
11809 dpyinfo->vertical_scroll_bar_cursor
11810 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
11812 dpyinfo->horizontal_scroll_bar_cursor
11813 = XCreateFontCursor (dpyinfo->display, XC_sb_h_double_arrow);
11815 xrdb = x_load_resources (dpyinfo->display, xrm_option,
11816 resource_name, EMACS_CLASS);
11817 #ifdef HAVE_XRMSETDATABASE
11818 XrmSetDatabase (dpyinfo->display, xrdb);
11819 #else
11820 dpyinfo->display->db = xrdb;
11821 #endif
11822 /* Put the rdb where we can find it in a way that works on
11823 all versions. */
11824 dpyinfo->xrdb = xrdb;
11826 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
11827 DefaultScreen (dpyinfo->display));
11828 select_visual (dpyinfo);
11829 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
11830 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
11831 dpyinfo->icon_bitmap_id = -1;
11832 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
11834 reset_mouse_highlight (&dpyinfo->mouse_highlight);
11836 /* See if we can construct pixel values from RGB values. */
11837 if (dpyinfo->visual->class == TrueColor)
11839 get_bits_and_offset (dpyinfo->visual->red_mask,
11840 &dpyinfo->red_bits, &dpyinfo->red_offset);
11841 get_bits_and_offset (dpyinfo->visual->blue_mask,
11842 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
11843 get_bits_and_offset (dpyinfo->visual->green_mask,
11844 &dpyinfo->green_bits, &dpyinfo->green_offset);
11847 /* See if a private colormap is requested. */
11848 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
11850 if (dpyinfo->visual->class == PseudoColor)
11852 AUTO_STRING (privateColormap, "privateColormap");
11853 AUTO_STRING (PrivateColormap, "PrivateColormap");
11854 Lisp_Object value
11855 = display_x_get_resource (dpyinfo, privateColormap,
11856 PrivateColormap, Qnil, Qnil);
11857 if (STRINGP (value)
11858 && (!strcmp (SSDATA (value), "true")
11859 || !strcmp (SSDATA (value), "on")))
11860 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
11863 else
11864 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
11865 dpyinfo->visual, AllocNone);
11867 #ifdef HAVE_XFT
11869 /* If we are using Xft, the following precautions should be made:
11871 1. Make sure that the Xrender extension is added before the Xft one.
11872 Otherwise, the close-display hook set by Xft is called after the one
11873 for Xrender, and the former tries to re-add the latter. This results
11874 in inconsistency of internal states and leads to X protocol error when
11875 one reconnects to the same X server (Bug#1696).
11877 2. Check dpi value in X resources. It is better we use it as well,
11878 since Xft will use it, as will all Gnome applications. If our real DPI
11879 is smaller or larger than the one Xft uses, our font will look smaller
11880 or larger than other for other applications, even if it is the same
11881 font name (monospace-10 for example). */
11883 int event_base, error_base;
11884 char *v;
11885 double d;
11887 XRenderQueryExtension (dpyinfo->display, &event_base, &error_base);
11889 v = XGetDefault (dpyinfo->display, "Xft", "dpi");
11890 if (v != NULL && sscanf (v, "%lf", &d) == 1)
11891 dpyinfo->resy = dpyinfo->resx = d;
11893 #endif
11895 if (dpyinfo->resy < 1)
11897 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
11898 double pixels = DisplayHeight (dpyinfo->display, screen_number);
11899 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
11900 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
11901 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
11902 pixels = DisplayWidth (dpyinfo->display, screen_number);
11903 mm = DisplayWidthMM (dpyinfo->display, screen_number);
11904 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
11905 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
11909 static const struct
11911 const char *name;
11912 int offset;
11913 } atom_refs[] = {
11914 #define ATOM_REFS_INIT(string, member) \
11915 { string, offsetof (struct x_display_info, member) },
11916 ATOM_REFS_INIT ("WM_PROTOCOLS", Xatom_wm_protocols)
11917 ATOM_REFS_INIT ("WM_TAKE_FOCUS", Xatom_wm_take_focus)
11918 ATOM_REFS_INIT ("WM_SAVE_YOURSELF", Xatom_wm_save_yourself)
11919 ATOM_REFS_INIT ("WM_DELETE_WINDOW", Xatom_wm_delete_window)
11920 ATOM_REFS_INIT ("WM_CHANGE_STATE", Xatom_wm_change_state)
11921 ATOM_REFS_INIT ("WM_CONFIGURE_DENIED", Xatom_wm_configure_denied)
11922 ATOM_REFS_INIT ("WM_MOVED", Xatom_wm_window_moved)
11923 ATOM_REFS_INIT ("WM_CLIENT_LEADER", Xatom_wm_client_leader)
11924 ATOM_REFS_INIT ("Editres", Xatom_editres)
11925 ATOM_REFS_INIT ("CLIPBOARD", Xatom_CLIPBOARD)
11926 ATOM_REFS_INIT ("TIMESTAMP", Xatom_TIMESTAMP)
11927 ATOM_REFS_INIT ("TEXT", Xatom_TEXT)
11928 ATOM_REFS_INIT ("COMPOUND_TEXT", Xatom_COMPOUND_TEXT)
11929 ATOM_REFS_INIT ("UTF8_STRING", Xatom_UTF8_STRING)
11930 ATOM_REFS_INIT ("DELETE", Xatom_DELETE)
11931 ATOM_REFS_INIT ("MULTIPLE", Xatom_MULTIPLE)
11932 ATOM_REFS_INIT ("INCR", Xatom_INCR)
11933 ATOM_REFS_INIT ("_EMACS_TMP_", Xatom_EMACS_TMP)
11934 ATOM_REFS_INIT ("TARGETS", Xatom_TARGETS)
11935 ATOM_REFS_INIT ("NULL", Xatom_NULL)
11936 ATOM_REFS_INIT ("ATOM", Xatom_ATOM)
11937 ATOM_REFS_INIT ("ATOM_PAIR", Xatom_ATOM_PAIR)
11938 ATOM_REFS_INIT ("CLIPBOARD_MANAGER", Xatom_CLIPBOARD_MANAGER)
11939 ATOM_REFS_INIT ("_XEMBED_INFO", Xatom_XEMBED_INFO)
11940 /* For properties of font. */
11941 ATOM_REFS_INIT ("PIXEL_SIZE", Xatom_PIXEL_SIZE)
11942 ATOM_REFS_INIT ("AVERAGE_WIDTH", Xatom_AVERAGE_WIDTH)
11943 ATOM_REFS_INIT ("_MULE_BASELINE_OFFSET", Xatom_MULE_BASELINE_OFFSET)
11944 ATOM_REFS_INIT ("_MULE_RELATIVE_COMPOSE", Xatom_MULE_RELATIVE_COMPOSE)
11945 ATOM_REFS_INIT ("_MULE_DEFAULT_ASCENT", Xatom_MULE_DEFAULT_ASCENT)
11946 /* Ghostscript support. */
11947 ATOM_REFS_INIT ("DONE", Xatom_DONE)
11948 ATOM_REFS_INIT ("PAGE", Xatom_PAGE)
11949 ATOM_REFS_INIT ("SCROLLBAR", Xatom_Scrollbar)
11950 ATOM_REFS_INIT ("HORIZONTAL_SCROLLBAR", Xatom_Horizontal_Scrollbar)
11951 ATOM_REFS_INIT ("_XEMBED", Xatom_XEMBED)
11952 /* EWMH */
11953 ATOM_REFS_INIT ("_NET_WM_STATE", Xatom_net_wm_state)
11954 ATOM_REFS_INIT ("_NET_WM_STATE_FULLSCREEN", Xatom_net_wm_state_fullscreen)
11955 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_HORZ",
11956 Xatom_net_wm_state_maximized_horz)
11957 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_VERT",
11958 Xatom_net_wm_state_maximized_vert)
11959 ATOM_REFS_INIT ("_NET_WM_STATE_STICKY", Xatom_net_wm_state_sticky)
11960 ATOM_REFS_INIT ("_NET_WM_STATE_HIDDEN", Xatom_net_wm_state_hidden)
11961 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE", Xatom_net_window_type)
11962 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE_TOOLTIP",
11963 Xatom_net_window_type_tooltip)
11964 ATOM_REFS_INIT ("_NET_WM_ICON_NAME", Xatom_net_wm_icon_name)
11965 ATOM_REFS_INIT ("_NET_WM_NAME", Xatom_net_wm_name)
11966 ATOM_REFS_INIT ("_NET_SUPPORTED", Xatom_net_supported)
11967 ATOM_REFS_INIT ("_NET_SUPPORTING_WM_CHECK", Xatom_net_supporting_wm_check)
11968 ATOM_REFS_INIT ("_NET_WM_WINDOW_OPACITY", Xatom_net_wm_window_opacity)
11969 ATOM_REFS_INIT ("_NET_ACTIVE_WINDOW", Xatom_net_active_window)
11970 ATOM_REFS_INIT ("_NET_FRAME_EXTENTS", Xatom_net_frame_extents)
11971 ATOM_REFS_INIT ("_NET_CURRENT_DESKTOP", Xatom_net_current_desktop)
11972 ATOM_REFS_INIT ("_NET_WORKAREA", Xatom_net_workarea)
11973 /* Session management */
11974 ATOM_REFS_INIT ("SM_CLIENT_ID", Xatom_SM_CLIENT_ID)
11975 ATOM_REFS_INIT ("_XSETTINGS_SETTINGS", Xatom_xsettings_prop)
11976 ATOM_REFS_INIT ("MANAGER", Xatom_xsettings_mgr)
11979 int i;
11980 enum { atom_count = ARRAYELTS (atom_refs) };
11981 /* 1 for _XSETTINGS_SN. */
11982 enum { total_atom_count = 1 + atom_count };
11983 Atom atoms_return[total_atom_count];
11984 char *atom_names[total_atom_count];
11985 static char const xsettings_fmt[] = "_XSETTINGS_S%d";
11986 char xsettings_atom_name[sizeof xsettings_fmt - 2
11987 + INT_STRLEN_BOUND (int)];
11989 for (i = 0; i < atom_count; i++)
11990 atom_names[i] = (char *) atom_refs[i].name;
11992 /* Build _XSETTINGS_SN atom name. */
11993 sprintf (xsettings_atom_name, xsettings_fmt,
11994 XScreenNumberOfScreen (dpyinfo->screen));
11995 atom_names[i] = xsettings_atom_name;
11997 XInternAtoms (dpyinfo->display, atom_names, total_atom_count,
11998 False, atoms_return);
12000 for (i = 0; i < atom_count; i++)
12001 *(Atom *) ((char *) dpyinfo + atom_refs[i].offset) = atoms_return[i];
12003 /* Manually copy last atom. */
12004 dpyinfo->Xatom_xsettings_sel = atoms_return[i];
12007 dpyinfo->x_dnd_atoms_size = 8;
12008 dpyinfo->x_dnd_atoms = xmalloc (sizeof *dpyinfo->x_dnd_atoms
12009 * dpyinfo->x_dnd_atoms_size);
12010 dpyinfo->gray
12011 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
12012 gray_bits, gray_width, gray_height,
12013 1, 0, 1);
12015 x_setup_pointer_blanking (dpyinfo);
12017 #ifdef HAVE_X_I18N
12018 xim_initialize (dpyinfo, resource_name);
12019 #endif
12021 xsettings_initialize (dpyinfo);
12023 /* This is only needed for distinguishing keyboard and process input. */
12024 if (dpyinfo->connection != 0)
12025 add_keyboard_wait_descriptor (dpyinfo->connection);
12027 #ifdef F_SETOWN
12028 fcntl (dpyinfo->connection, F_SETOWN, getpid ());
12029 #endif /* ! defined (F_SETOWN) */
12031 if (interrupt_input)
12032 init_sigio (dpyinfo->connection);
12034 #ifdef USE_LUCID
12036 XrmValue d, fr, to;
12037 Font font;
12039 dpy = dpyinfo->display;
12040 d.addr = (XPointer)&dpy;
12041 d.size = sizeof (Display *);
12042 fr.addr = XtDefaultFont;
12043 fr.size = sizeof (XtDefaultFont);
12044 to.size = sizeof (Font *);
12045 to.addr = (XPointer)&font;
12046 x_catch_errors (dpy);
12047 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
12048 emacs_abort ();
12049 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
12050 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
12051 /* Do not free XFontStruct returned by the above call to XQueryFont.
12052 This leads to X protocol errors at XtCloseDisplay (Bug#18403). */
12053 x_uncatch_errors ();
12055 #endif
12057 /* See if we should run in synchronous mode. This is useful
12058 for debugging X code. */
12060 AUTO_STRING (synchronous, "synchronous");
12061 AUTO_STRING (Synchronous, "Synchronous");
12062 Lisp_Object value = display_x_get_resource (dpyinfo, synchronous,
12063 Synchronous, Qnil, Qnil);
12064 if (STRINGP (value)
12065 && (!strcmp (SSDATA (value), "true")
12066 || !strcmp (SSDATA (value), "on")))
12067 XSynchronize (dpyinfo->display, True);
12071 AUTO_STRING (useXIM, "useXIM");
12072 AUTO_STRING (UseXIM, "UseXIM");
12073 Lisp_Object value = display_x_get_resource (dpyinfo, useXIM, UseXIM,
12074 Qnil, Qnil);
12075 #ifdef USE_XIM
12076 if (STRINGP (value)
12077 && (!strcmp (SSDATA (value), "false")
12078 || !strcmp (SSDATA (value), "off")))
12079 use_xim = false;
12080 #else
12081 if (STRINGP (value)
12082 && (!strcmp (SSDATA (value), "true")
12083 || !strcmp (SSDATA (value), "on")))
12084 use_xim = true;
12085 #endif
12088 #ifdef HAVE_X_SM
12089 /* Only do this for the very first display in the Emacs session.
12090 Ignore X session management when Emacs was first started on a
12091 tty or started as a daemon. */
12092 if (terminal->id == 1 && ! IS_DAEMON)
12093 x_session_initialize (dpyinfo);
12094 #endif
12096 #ifdef USE_CAIRO
12097 x_extension_initialize (dpyinfo);
12098 #endif
12100 unblock_input ();
12102 return dpyinfo;
12105 /* Get rid of display DPYINFO, deleting all frames on it,
12106 and without sending any more commands to the X server. */
12108 static void
12109 x_delete_display (struct x_display_info *dpyinfo)
12111 struct terminal *t;
12113 /* Close all frames and delete the generic struct terminal for this
12114 X display. */
12115 for (t = terminal_list; t; t = t->next_terminal)
12116 if (t->type == output_x_window && t->display_info.x == dpyinfo)
12118 #ifdef HAVE_X_SM
12119 /* Close X session management when we close its display. */
12120 if (t->id == 1 && x_session_have_connection ())
12121 x_session_close ();
12122 #endif
12123 delete_terminal (t);
12124 break;
12127 if (next_noop_dpyinfo == dpyinfo)
12128 next_noop_dpyinfo = dpyinfo->next;
12130 if (x_display_list == dpyinfo)
12131 x_display_list = dpyinfo->next;
12132 else
12134 struct x_display_info *tail;
12136 for (tail = x_display_list; tail; tail = tail->next)
12137 if (tail->next == dpyinfo)
12138 tail->next = tail->next->next;
12141 xfree (dpyinfo->x_id_name);
12142 xfree (dpyinfo->x_dnd_atoms);
12143 xfree (dpyinfo->color_cells);
12144 xfree (dpyinfo);
12147 #ifdef USE_X_TOOLKIT
12149 /* Atimer callback function for TIMER. Called every 0.1s to process
12150 Xt timeouts, if needed. We must avoid calling XtAppPending as
12151 much as possible because that function does an implicit XFlush
12152 that slows us down. */
12154 static void
12155 x_process_timeouts (struct atimer *timer)
12157 block_input ();
12158 x_timeout_atimer_activated_flag = false;
12159 if (toolkit_scroll_bar_interaction || popup_activated ())
12161 while (XtAppPending (Xt_app_con) & XtIMTimer)
12162 XtAppProcessEvent (Xt_app_con, XtIMTimer);
12163 /* Reactivate the atimer for next time. */
12164 x_activate_timeout_atimer ();
12166 unblock_input ();
12169 /* Install an asynchronous timer that processes Xt timeout events
12170 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
12171 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
12172 function whenever these variables are set. This is necessary
12173 because some widget sets use timeouts internally, for example the
12174 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
12175 processed, these widgets don't behave normally. */
12177 void
12178 x_activate_timeout_atimer (void)
12180 block_input ();
12181 if (!x_timeout_atimer_activated_flag)
12183 struct timespec interval = make_timespec (0, 100 * 1000 * 1000);
12184 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
12185 x_timeout_atimer_activated_flag = true;
12187 unblock_input ();
12190 #endif /* USE_X_TOOLKIT */
12193 /* Set up use of X before we make the first connection. */
12195 static struct redisplay_interface x_redisplay_interface =
12197 x_frame_parm_handlers,
12198 x_produce_glyphs,
12199 x_write_glyphs,
12200 x_insert_glyphs,
12201 x_clear_end_of_line,
12202 x_scroll_run,
12203 x_after_update_window_line,
12204 x_update_window_begin,
12205 x_update_window_end,
12206 x_flush,
12207 x_clear_window_mouse_face,
12208 x_get_glyph_overhangs,
12209 x_fix_overlapping_area,
12210 x_draw_fringe_bitmap,
12211 #ifdef USE_CAIRO
12212 x_cr_define_fringe_bitmap,
12213 x_cr_destroy_fringe_bitmap,
12214 #else
12215 0, /* define_fringe_bitmap */
12216 0, /* destroy_fringe_bitmap */
12217 #endif
12218 x_compute_glyph_string_overhangs,
12219 x_draw_glyph_string,
12220 x_define_frame_cursor,
12221 x_clear_frame_area,
12222 x_draw_window_cursor,
12223 x_draw_vertical_window_border,
12224 x_draw_window_divider,
12225 x_shift_glyphs_for_insert, /* Never called; see comment in function. */
12226 x_show_hourglass,
12227 x_hide_hourglass
12231 /* This function is called when the last frame on a display is deleted. */
12232 void
12233 x_delete_terminal (struct terminal *terminal)
12235 struct x_display_info *dpyinfo = terminal->display_info.x;
12237 /* Protect against recursive calls. delete_frame in
12238 delete_terminal calls us back when it deletes our last frame. */
12239 if (!terminal->name)
12240 return;
12242 block_input ();
12243 #ifdef HAVE_X_I18N
12244 /* We must close our connection to the XIM server before closing the
12245 X display. */
12246 if (dpyinfo->xim)
12247 xim_close_dpy (dpyinfo);
12248 #endif
12250 /* Normally, the display is available... */
12251 if (dpyinfo->display)
12253 x_destroy_all_bitmaps (dpyinfo);
12254 XSetCloseDownMode (dpyinfo->display, DestroyAll);
12256 /* Whether or not XCloseDisplay destroys the associated resource
12257 database depends on the version of libX11. To avoid both
12258 crash and memory leak, we dissociate the database from the
12259 display and then destroy dpyinfo->xrdb ourselves.
12261 Unfortunately, the above strategy does not work in some
12262 situations due to a bug in newer versions of libX11: because
12263 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
12264 dpy->db is NULL, XCloseDisplay destroys the associated
12265 database whereas it has not been created by XGetDefault
12266 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
12267 don't destroy the database here in order to avoid the crash
12268 in the above situations for now, though that may cause memory
12269 leaks in other situations. */
12270 #if false
12271 #ifdef HAVE_XRMSETDATABASE
12272 XrmSetDatabase (dpyinfo->display, NULL);
12273 #else
12274 dpyinfo->display->db = NULL;
12275 #endif
12276 /* We used to call XrmDestroyDatabase from x_delete_display, but
12277 some older versions of libX11 crash if we call it after
12278 closing all the displays. */
12279 XrmDestroyDatabase (dpyinfo->xrdb);
12280 #endif
12282 #ifdef USE_GTK
12283 xg_display_close (dpyinfo->display);
12284 #else
12285 #ifdef USE_X_TOOLKIT
12286 XtCloseDisplay (dpyinfo->display);
12287 #else
12288 XCloseDisplay (dpyinfo->display);
12289 #endif
12290 #endif /* ! USE_GTK */
12291 /* Do not close the connection here because it's already closed
12292 by X(t)CloseDisplay (Bug#18403). */
12293 dpyinfo->display = NULL;
12296 /* ...but if called from x_connection_closed, the display may already
12297 be closed and dpyinfo->display was set to 0 to indicate that. Since
12298 X server is most likely gone, explicit close is the only reliable
12299 way to continue and avoid Bug#19147. */
12300 else if (dpyinfo->connection >= 0)
12301 emacs_close (dpyinfo->connection);
12303 /* No more input on this descriptor. */
12304 delete_keyboard_wait_descriptor (dpyinfo->connection);
12305 /* Mark as dead. */
12306 dpyinfo->connection = -1;
12308 x_delete_display (dpyinfo);
12309 unblock_input ();
12312 /* Create a struct terminal, initialize it with the X11 specific
12313 functions and make DISPLAY->TERMINAL point to it. */
12315 static struct terminal *
12316 x_create_terminal (struct x_display_info *dpyinfo)
12318 struct terminal *terminal;
12320 terminal = create_terminal (output_x_window, &x_redisplay_interface);
12322 terminal->display_info.x = dpyinfo;
12323 dpyinfo->terminal = terminal;
12325 /* kboard is initialized in x_term_init. */
12327 terminal->clear_frame_hook = x_clear_frame;
12328 terminal->ins_del_lines_hook = x_ins_del_lines;
12329 terminal->delete_glyphs_hook = x_delete_glyphs;
12330 terminal->ring_bell_hook = XTring_bell;
12331 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
12332 terminal->update_begin_hook = x_update_begin;
12333 terminal->update_end_hook = x_update_end;
12334 terminal->read_socket_hook = XTread_socket;
12335 terminal->frame_up_to_date_hook = XTframe_up_to_date;
12336 terminal->mouse_position_hook = XTmouse_position;
12337 terminal->frame_rehighlight_hook = XTframe_rehighlight;
12338 terminal->frame_raise_lower_hook = XTframe_raise_lower;
12339 terminal->fullscreen_hook = XTfullscreen_hook;
12340 terminal->menu_show_hook = x_menu_show;
12341 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
12342 terminal->popup_dialog_hook = xw_popup_dialog;
12343 #endif
12344 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
12345 terminal->set_horizontal_scroll_bar_hook = XTset_horizontal_scroll_bar;
12346 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
12347 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
12348 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
12349 terminal->delete_frame_hook = x_destroy_window;
12350 terminal->delete_terminal_hook = x_delete_terminal;
12351 /* Other hooks are NULL by default. */
12353 return terminal;
12356 static void
12357 x_initialize (void)
12359 baud_rate = 19200;
12361 x_noop_count = 0;
12362 any_help_event_p = false;
12363 ignore_next_mouse_click_timeout = 0;
12365 #ifdef USE_GTK
12366 current_count = -1;
12367 #endif
12369 /* Try to use interrupt input; if we can't, then start polling. */
12370 Fset_input_interrupt_mode (Qt);
12372 #ifdef USE_X_TOOLKIT
12373 XtToolkitInitialize ();
12375 Xt_app_con = XtCreateApplicationContext ();
12377 /* Register a converter from strings to pixels, which uses
12378 Emacs' color allocation infrastructure. */
12379 XtAppSetTypeConverter (Xt_app_con,
12380 XtRString, XtRPixel, cvt_string_to_pixel,
12381 cvt_string_to_pixel_args,
12382 XtNumber (cvt_string_to_pixel_args),
12383 XtCacheByDisplay, cvt_pixel_dtor);
12385 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
12386 #endif
12388 #ifdef USE_TOOLKIT_SCROLL_BARS
12389 #ifndef USE_GTK
12390 xaw3d_arrow_scroll = False;
12391 xaw3d_pick_top = True;
12392 #endif
12393 #endif
12395 #ifdef USE_CAIRO
12396 x_cr_init_fringe (&x_redisplay_interface);
12397 #endif
12399 /* Note that there is no real way portable across R3/R4 to get the
12400 original error handler. */
12401 XSetErrorHandler (x_error_handler);
12402 XSetIOErrorHandler (x_io_error_quitter);
12405 #ifdef USE_GTK
12406 void
12407 init_xterm (void)
12409 /* Emacs can handle only core input events, so make sure
12410 Gtk doesn't use Xinput or Xinput2 extensions. */
12411 xputenv ("GDK_CORE_DEVICE_EVENTS=1");
12413 #endif
12415 void
12416 syms_of_xterm (void)
12418 x_error_message = NULL;
12420 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
12421 DEFSYM (Qlatin_1, "latin-1");
12423 #ifdef USE_GTK
12424 xg_default_icon_file = build_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
12425 staticpro (&xg_default_icon_file);
12427 DEFSYM (Qx_gtk_map_stock, "x-gtk-map-stock");
12428 #endif
12430 DEFVAR_BOOL ("x-use-underline-position-properties",
12431 x_use_underline_position_properties,
12432 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
12433 A value of nil means ignore them. If you encounter fonts with bogus
12434 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
12435 to 4.1, set this to nil. You can also use `underline-minimum-offset'
12436 to override the font's UNDERLINE_POSITION for small font display
12437 sizes. */);
12438 x_use_underline_position_properties = true;
12440 DEFVAR_BOOL ("x-underline-at-descent-line",
12441 x_underline_at_descent_line,
12442 doc: /* Non-nil means to draw the underline at the same place as the descent line.
12443 A value of nil means to draw the underline according to the value of the
12444 variable `x-use-underline-position-properties', which is usually at the
12445 baseline level. The default value is nil. */);
12446 x_underline_at_descent_line = false;
12448 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
12449 x_mouse_click_focus_ignore_position,
12450 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
12451 This variable is only used when the window manager requires that you
12452 click on a frame to select it (give it focus). In that case, a value
12453 of nil, means that the selected window and cursor position changes to
12454 reflect the mouse click position, while a non-nil value means that the
12455 selected window or cursor position is preserved. */);
12456 x_mouse_click_focus_ignore_position = false;
12458 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
12459 doc: /* Which toolkit scroll bars Emacs uses, if any.
12460 A value of nil means Emacs doesn't use toolkit scroll bars.
12461 With the X Window system, the value is a symbol describing the
12462 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
12463 With MS Windows or Nextstep, the value is t. */);
12464 #ifdef USE_TOOLKIT_SCROLL_BARS
12465 #ifdef USE_MOTIF
12466 Vx_toolkit_scroll_bars = intern_c_string ("motif");
12467 #elif defined HAVE_XAW3D
12468 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
12469 #elif USE_GTK
12470 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
12471 #else
12472 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
12473 #endif
12474 #else
12475 Vx_toolkit_scroll_bars = Qnil;
12476 #endif
12478 DEFSYM (Qmodifier_value, "modifier-value");
12479 DEFSYM (Qalt, "alt");
12480 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
12481 DEFSYM (Qhyper, "hyper");
12482 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
12483 DEFSYM (Qmeta, "meta");
12484 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
12485 DEFSYM (Qsuper, "super");
12486 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
12488 DEFVAR_LISP ("x-alt-keysym", Vx_alt_keysym,
12489 doc: /* Which keys Emacs uses for the alt modifier.
12490 This should be one of the symbols `alt', `hyper', `meta', `super'.
12491 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
12492 is nil, which is the same as `alt'. */);
12493 Vx_alt_keysym = Qnil;
12495 DEFVAR_LISP ("x-hyper-keysym", Vx_hyper_keysym,
12496 doc: /* Which keys Emacs uses for the hyper modifier.
12497 This should be one of the symbols `alt', `hyper', `meta', `super'.
12498 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
12499 default is nil, which is the same as `hyper'. */);
12500 Vx_hyper_keysym = Qnil;
12502 DEFVAR_LISP ("x-meta-keysym", Vx_meta_keysym,
12503 doc: /* Which keys Emacs uses for the meta modifier.
12504 This should be one of the symbols `alt', `hyper', `meta', `super'.
12505 For example, `meta' means use the Meta_L and Meta_R keysyms. The
12506 default is nil, which is the same as `meta'. */);
12507 Vx_meta_keysym = Qnil;
12509 DEFVAR_LISP ("x-super-keysym", Vx_super_keysym,
12510 doc: /* Which keys Emacs uses for the super modifier.
12511 This should be one of the symbols `alt', `hyper', `meta', `super'.
12512 For example, `super' means use the Super_L and Super_R keysyms. The
12513 default is nil, which is the same as `super'. */);
12514 Vx_super_keysym = Qnil;
12516 DEFVAR_LISP ("x-keysym-table", Vx_keysym_table,
12517 doc: /* Hash table of character codes indexed by X keysym codes. */);
12518 Vx_keysym_table = make_hash_table (hashtest_eql, make_number (900),
12519 make_float (DEFAULT_REHASH_SIZE),
12520 make_float (DEFAULT_REHASH_THRESHOLD),
12521 Qnil);
12523 DEFVAR_BOOL ("x-frame-normalize-before-maximize",
12524 x_frame_normalize_before_maximize,
12525 doc: /* Non-nil means normalize frame before maximizing.
12526 If this variable is t, Emacs first asks the window manager to give the
12527 frame its normal size, and only then the final state, whenever changing
12528 from a full-height, full-width or full-both state to the maximized one
12529 or when changing from the maximized to the full-height or full-width
12530 state.
12532 Set this variable only if your window manager cannot handle the
12533 transition between the various maximization states. */);
12534 x_frame_normalize_before_maximize = false;