cleanups
[emacs.git] / src / xterm.c
blobe3f473986b2d6a45c7f2475f9b2cff0a83ffe282
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>
26 #include "lisp.h"
27 #include "blockinput.h"
28 #include "syssignal.h"
30 /* This may include sys/types.h, and that somehow loses
31 if this is not done before the other system files. */
32 #include "xterm.h"
33 #include <X11/cursorfont.h>
35 /* If we have Xfixes extension, use it for pointer blanking. */
36 #ifdef HAVE_XFIXES
37 #include <X11/extensions/Xfixes.h>
38 #endif
40 /* Using Xft implies that XRender is available. */
41 #ifdef HAVE_XFT
42 #include <X11/extensions/Xrender.h>
43 #endif
45 /* Load sys/types.h if not already loaded.
46 In some systems loading it twice is suicidal. */
47 #ifndef makedev
48 #include <sys/types.h>
49 #endif /* makedev */
51 #include <sys/ioctl.h>
53 #include "systime.h"
55 #include <fcntl.h>
56 #include <errno.h>
57 #include <sys/stat.h>
58 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
59 /* #include <sys/param.h> */
61 #include "charset.h"
62 #include "character.h"
63 #include "coding.h"
64 #include "frame.h"
65 #include "dispextern.h"
66 #ifdef HAVE_XWIDGETS
67 #include "xwidget.h"
68 #endif
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 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
188 static Lisp_Object Qvendor_specific_keysyms;
189 static Lisp_Object Qlatin_1;
191 #ifdef USE_GTK
192 /* The name of the Emacs icon file. */
193 static Lisp_Object xg_default_icon_file;
195 /* Used in gtkutil.c. */
196 Lisp_Object Qx_gtk_map_stock;
197 #endif
199 /* Some functions take this as char *, not const char *. */
200 static char emacs_class[] = EMACS_CLASS;
202 enum xembed_info
204 XEMBED_MAPPED = 1 << 0
207 enum xembed_message
209 XEMBED_EMBEDDED_NOTIFY = 0,
210 XEMBED_WINDOW_ACTIVATE = 1,
211 XEMBED_WINDOW_DEACTIVATE = 2,
212 XEMBED_REQUEST_FOCUS = 3,
213 XEMBED_FOCUS_IN = 4,
214 XEMBED_FOCUS_OUT = 5,
215 XEMBED_FOCUS_NEXT = 6,
216 XEMBED_FOCUS_PREV = 7,
218 XEMBED_MODALITY_ON = 10,
219 XEMBED_MODALITY_OFF = 11,
220 XEMBED_REGISTER_ACCELERATOR = 12,
221 XEMBED_UNREGISTER_ACCELERATOR = 13,
222 XEMBED_ACTIVATE_ACCELERATOR = 14
225 static bool x_alloc_nearest_color_1 (Display *, Colormap, XColor *);
226 static void x_set_window_size_1 (struct frame *, bool, int, int, bool);
227 static void x_raise_frame (struct frame *);
228 static void x_lower_frame (struct frame *);
229 static const XColor *x_color_cells (Display *, int *);
230 static int x_io_error_quitter (Display *);
231 static struct terminal *x_create_terminal (struct x_display_info *);
232 static void x_update_end (struct frame *);
233 static void XTframe_up_to_date (struct frame *);
234 static void x_clear_frame (struct frame *);
235 static _Noreturn void x_ins_del_lines (struct frame *, int, int);
236 static void frame_highlight (struct frame *);
237 static void frame_unhighlight (struct frame *);
238 static void x_new_focus_frame (struct x_display_info *, struct frame *);
239 static void x_focus_changed (int, int, struct x_display_info *,
240 struct frame *, struct input_event *);
241 static void XTframe_rehighlight (struct frame *);
242 static void x_frame_rehighlight (struct x_display_info *);
243 static void x_draw_hollow_cursor (struct window *, struct glyph_row *);
244 static void x_draw_bar_cursor (struct window *, struct glyph_row *, int,
245 enum text_cursor_kinds);
247 static void x_clip_to_row (struct window *, struct glyph_row *,
248 enum glyph_row_area, GC);
249 static void x_flush (struct frame *f);
250 static void x_update_begin (struct frame *);
251 static void x_update_window_begin (struct window *);
252 static struct scroll_bar *x_window_to_scroll_bar (Display *, Window, int);
253 static void x_scroll_bar_report_motion (struct frame **, Lisp_Object *,
254 enum scroll_bar_part *,
255 Lisp_Object *, Lisp_Object *,
256 Time *);
257 static void x_horizontal_scroll_bar_report_motion (struct frame **, Lisp_Object *,
258 enum scroll_bar_part *,
259 Lisp_Object *, Lisp_Object *,
260 Time *);
261 static bool x_handle_net_wm_state (struct frame *, const XPropertyEvent *);
262 static void x_check_fullscreen (struct frame *);
263 static void x_check_expected_move (struct frame *, int, int);
264 static void x_sync_with_move (struct frame *, int, int, bool);
265 static int handle_one_xevent (struct x_display_info *,
266 const XEvent *, int *,
267 struct input_event *);
268 #if ! (defined USE_X_TOOLKIT || defined USE_MOTIF)
269 static int x_dispatch_event (XEvent *, Display *);
270 #endif
271 /* Don't declare this _Noreturn because we want no
272 interference with debugging failing X calls. */
273 static void x_connection_closed (Display *, const char *);
274 static void x_wm_set_window_state (struct frame *, int);
275 static void x_wm_set_icon_pixmap (struct frame *, ptrdiff_t);
276 static void x_initialize (void);
278 static bool get_current_wm_state (struct frame *, Window, int *, bool *);
280 /* Flush display of frame F. */
282 static void
283 x_flush (struct frame *f)
285 eassert (f && FRAME_X_P (f));
286 /* Don't call XFlush when it is not safe to redisplay; the X
287 connection may be broken. */
288 if (!NILP (Vinhibit_redisplay))
289 return;
291 block_input ();
292 XFlush (FRAME_X_DISPLAY (f));
293 unblock_input ();
297 /* Remove calls to XFlush by defining XFlush to an empty replacement.
298 Calls to XFlush should be unnecessary because the X output buffer
299 is flushed automatically as needed by calls to XPending,
300 XNextEvent, or XWindowEvent according to the XFlush man page.
301 XTread_socket calls XPending. Removing XFlush improves
302 performance. */
304 #define XFlush(DISPLAY) (void) 0
307 /***********************************************************************
308 Debugging
309 ***********************************************************************/
311 #if false
313 /* This is a function useful for recording debugging information about
314 the sequence of occurrences in this file. */
316 struct record
318 char *locus;
319 int type;
322 struct record event_record[100];
324 int event_record_index;
326 void
327 record_event (char *locus, int type)
329 if (event_record_index == ARRAYELTS (event_record))
330 event_record_index = 0;
332 event_record[event_record_index].locus = locus;
333 event_record[event_record_index].type = type;
334 event_record_index++;
337 #endif
341 /* Return the struct x_display_info corresponding to DPY. */
343 struct x_display_info *
344 x_display_info_for_display (Display *dpy)
346 struct x_display_info *dpyinfo;
348 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
349 if (dpyinfo->display == dpy)
350 return dpyinfo;
352 return 0;
355 static Window
356 x_find_topmost_parent (struct frame *f)
358 struct x_output *x = f->output_data.x;
359 Window win = None, wi = x->parent_desc;
360 Display *dpy = FRAME_X_DISPLAY (f);
362 while (wi != FRAME_DISPLAY_INFO (f)->root_window)
364 Window root;
365 Window *children;
366 unsigned int nchildren;
368 win = wi;
369 if (XQueryTree (dpy, win, &root, &wi, &children, &nchildren))
370 XFree (children);
371 else
372 break;
375 return win;
378 #define OPAQUE 0xffffffff
380 void
381 x_set_frame_alpha (struct frame *f)
383 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
384 Display *dpy = FRAME_X_DISPLAY (f);
385 Window win = FRAME_OUTER_WINDOW (f);
386 double alpha = 1.0;
387 double alpha_min = 1.0;
388 unsigned long opac;
389 Window parent;
391 if (dpyinfo->x_highlight_frame == f)
392 alpha = f->alpha[0];
393 else
394 alpha = f->alpha[1];
396 if (FLOATP (Vframe_alpha_lower_limit))
397 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
398 else if (INTEGERP (Vframe_alpha_lower_limit))
399 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
401 if (alpha < 0.0)
402 return;
403 else if (alpha > 1.0)
404 alpha = 1.0;
405 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
406 alpha = alpha_min;
408 opac = alpha * OPAQUE;
410 x_catch_errors (dpy);
412 /* If there is a parent from the window manager, put the property there
413 also, to work around broken window managers that fail to do that.
414 Do this unconditionally as this function is called on reparent when
415 alpha has not changed on the frame. */
417 parent = x_find_topmost_parent (f);
418 if (parent != None)
419 XChangeProperty (dpy, parent, dpyinfo->Xatom_net_wm_window_opacity,
420 XA_CARDINAL, 32, PropModeReplace,
421 (unsigned char *) &opac, 1);
423 /* return unless necessary */
425 unsigned char *data;
426 Atom actual;
427 int rc, format;
428 unsigned long n, left;
430 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
431 0, 1, False, XA_CARDINAL,
432 &actual, &format, &n, &left,
433 &data);
435 if (rc == Success && actual != None)
437 unsigned long value = *(unsigned long *)data;
438 XFree (data);
439 if (value == opac)
441 x_uncatch_errors ();
442 return;
447 XChangeProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
448 XA_CARDINAL, 32, PropModeReplace,
449 (unsigned char *) &opac, 1);
450 x_uncatch_errors ();
453 /***********************************************************************
454 Starting and ending an update
455 ***********************************************************************/
457 /* Start an update of frame F. This function is installed as a hook
458 for update_begin, i.e. it is called when update_begin is called.
459 This function is called prior to calls to x_update_window_begin for
460 each window being updated. Currently, there is nothing to do here
461 because all interesting stuff is done on a window basis. */
463 static void
464 x_update_begin (struct frame *f)
466 /* Nothing to do. */
470 /* Start update of window W. */
472 static void
473 x_update_window_begin (struct window *w)
475 struct frame *f = XFRAME (WINDOW_FRAME (w));
476 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
478 w->output_cursor = w->cursor;
480 block_input ();
482 if (f == hlinfo->mouse_face_mouse_frame)
484 /* Don't do highlighting for mouse motion during the update. */
485 hlinfo->mouse_face_defer = true;
487 /* If F needs to be redrawn, simply forget about any prior mouse
488 highlighting. */
489 if (FRAME_GARBAGED_P (f))
490 hlinfo->mouse_face_window = Qnil;
493 unblock_input ();
497 /* Draw a vertical window border from (x,y0) to (x,y1) */
499 static void
500 x_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
502 struct frame *f = XFRAME (WINDOW_FRAME (w));
503 struct face *face;
505 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
506 if (face)
507 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
508 face->foreground);
510 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
511 f->output_data.x->normal_gc, x, y0, x, y1);
514 /* Draw a window divider from (x0,y0) to (x1,y1) */
516 static void
517 x_draw_window_divider (struct window *w, int x0, int x1, int y0, int y1)
519 struct frame *f = XFRAME (WINDOW_FRAME (w));
520 struct face *face = FACE_FROM_ID (f, WINDOW_DIVIDER_FACE_ID);
521 struct face *face_first = FACE_FROM_ID (f, WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
522 struct face *face_last = FACE_FROM_ID (f, WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
523 unsigned long color = face ? face->foreground : FRAME_FOREGROUND_PIXEL (f);
524 unsigned long color_first = (face_first
525 ? face_first->foreground
526 : FRAME_FOREGROUND_PIXEL (f));
527 unsigned long color_last = (face_last
528 ? face_last->foreground
529 : FRAME_FOREGROUND_PIXEL (f));
530 Display *display = FRAME_X_DISPLAY (f);
531 Window window = FRAME_X_WINDOW (f);
533 if (y1 - y0 > x1 - x0 && x1 - x0 > 2)
534 /* Vertical. */
536 XSetForeground (display, f->output_data.x->normal_gc, color_first);
537 XFillRectangle (display, window, f->output_data.x->normal_gc,
538 x0, y0, 1, y1 - y0);
539 XSetForeground (display, f->output_data.x->normal_gc, color);
540 XFillRectangle (display, window, f->output_data.x->normal_gc,
541 x0 + 1, y0, x1 - x0 - 2, y1 - y0);
542 XSetForeground (display, f->output_data.x->normal_gc, color_last);
543 XFillRectangle (display, window, f->output_data.x->normal_gc,
544 x1 - 1, y0, 1, y1 - y0);
546 else if (x1 - x0 > y1 - y0 && y1 - y0 > 3)
547 /* Horizontal. */
549 XSetForeground (display, f->output_data.x->normal_gc, color_first);
550 XFillRectangle (display, window, f->output_data.x->normal_gc,
551 x0, y0, x1 - x0, 1);
552 XSetForeground (display, f->output_data.x->normal_gc, color);
553 XFillRectangle (display, window, f->output_data.x->normal_gc,
554 x0, y0 + 1, x1 - x0, y1 - y0 - 2);
555 XSetForeground (display, f->output_data.x->normal_gc, color_last);
556 XFillRectangle (display, window, f->output_data.x->normal_gc,
557 x0, y1 - 1, x1 - x0, 1);
559 else
561 XSetForeground (display, f->output_data.x->normal_gc, color);
562 XFillRectangle (display, window, f->output_data.x->normal_gc,
563 x0, y0, x1 - x0, y1 - y0);
567 /* End update of window W.
569 Draw vertical borders between horizontally adjacent windows, and
570 display W's cursor if CURSOR_ON_P is non-zero.
572 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
573 glyphs in mouse-face were overwritten. In that case we have to
574 make sure that the mouse-highlight is properly redrawn.
576 W may be a menu bar pseudo-window in case we don't have X toolkit
577 support. Such windows don't have a cursor, so don't display it
578 here. */
580 static void
581 x_update_window_end (struct window *w, bool cursor_on_p,
582 bool mouse_face_overwritten_p)
584 if (!w->pseudo_window_p)
586 block_input ();
588 if (cursor_on_p)
589 display_and_set_cursor (w, true,
590 w->output_cursor.hpos, w->output_cursor.vpos,
591 w->output_cursor.x, w->output_cursor.y);
593 if (draw_window_fringes (w, true))
595 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
596 x_draw_right_divider (w);
597 else
598 x_draw_vertical_border (w);
601 unblock_input ();
604 /* If a row with mouse-face was overwritten, arrange for
605 XTframe_up_to_date to redisplay the mouse highlight. */
606 if (mouse_face_overwritten_p)
608 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
610 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
611 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
612 hlinfo->mouse_face_window = Qnil;
617 /* End update of frame F. This function is installed as a hook in
618 update_end. */
620 static void
621 x_update_end (struct frame *f)
623 /* Mouse highlight may be displayed again. */
624 MOUSE_HL_INFO (f)->mouse_face_defer = false;
626 #ifndef XFlush
627 block_input ();
628 XFlush (FRAME_X_DISPLAY (f));
629 unblock_input ();
630 #endif
634 /* This function is called from various places in xdisp.c
635 whenever a complete update has been performed. */
637 static void
638 XTframe_up_to_date (struct frame *f)
640 if (FRAME_X_P (f))
641 FRAME_MOUSE_UPDATE (f);
645 /* Clear under internal border if any (GTK has its own version). */
646 #ifndef USE_GTK
647 void
648 x_clear_under_internal_border (struct frame *f)
650 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
652 Display *display = FRAME_X_DISPLAY (f);
653 Window window = FRAME_X_WINDOW (f);
654 int border = FRAME_INTERNAL_BORDER_WIDTH (f);
655 int width = FRAME_PIXEL_WIDTH (f);
656 int height = FRAME_PIXEL_HEIGHT (f);
657 int margin = FRAME_TOP_MARGIN_HEIGHT (f);
659 block_input ();
660 x_clear_area (display, window, 0, 0, border, height);
661 x_clear_area (display, window, 0, margin, width, border);
662 x_clear_area (display, window, width - border, 0, border, height);
663 x_clear_area (display, window, 0, height - border, width, border);
664 unblock_input ();
667 #endif
669 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
670 arrow bitmaps, or clear the fringes if no bitmaps are required
671 before DESIRED_ROW is made current. This function is called from
672 update_window_line only if it is known that there are differences
673 between bitmaps to be drawn between current row and DESIRED_ROW. */
675 static void
676 x_after_update_window_line (struct window *w, struct glyph_row *desired_row)
678 eassert (w);
680 if (!desired_row->mode_line_p && !w->pseudo_window_p)
681 desired_row->redraw_fringe_bitmaps_p = true;
683 #ifdef USE_X_TOOLKIT
684 /* When a window has disappeared, make sure that no rest of
685 full-width rows stays visible in the internal border. Could
686 check here if updated window is the leftmost/rightmost window,
687 but I guess it's not worth doing since vertically split windows
688 are almost never used, internal border is rarely set, and the
689 overhead is very small. */
691 struct frame *f;
692 int width, height;
694 if (windows_or_buffers_changed
695 && desired_row->full_width_p
696 && (f = XFRAME (w->frame),
697 width = FRAME_INTERNAL_BORDER_WIDTH (f),
698 width != 0)
699 && (height = desired_row->visible_height,
700 height > 0))
702 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
704 block_input ();
705 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
706 0, y, width, height);
707 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
708 FRAME_PIXEL_WIDTH (f) - width,
709 y, width, height);
710 unblock_input ();
713 #endif
716 static void
717 x_draw_fringe_bitmap (struct window *w, struct glyph_row *row, struct draw_fringe_bitmap_params *p)
719 struct frame *f = XFRAME (WINDOW_FRAME (w));
720 Display *display = FRAME_X_DISPLAY (f);
721 Window window = FRAME_X_WINDOW (f);
722 GC gc = f->output_data.x->normal_gc;
723 struct face *face = p->face;
725 /* Must clip because of partially visible lines. */
726 x_clip_to_row (w, row, ANY_AREA, gc);
728 if (p->bx >= 0 && !p->overlay_p)
730 /* In case the same realized face is used for fringes and
731 for something displayed in the text (e.g. face `region' on
732 mono-displays, the fill style may have been changed to
733 FillSolid in x_draw_glyph_string_background. */
734 if (face->stipple)
735 XSetFillStyle (display, face->gc, FillOpaqueStippled);
736 else
737 XSetForeground (display, face->gc, face->background);
739 XFillRectangle (display, window, face->gc,
740 p->bx, p->by, p->nx, p->ny);
742 if (!face->stipple)
743 XSetForeground (display, face->gc, face->foreground);
746 if (p->which)
748 char *bits;
749 Pixmap pixmap, clipmask = (Pixmap) 0;
750 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
751 XGCValues gcv;
753 if (p->wd > 8)
754 bits = (char *) (p->bits + p->dh);
755 else
756 bits = (char *) p->bits + p->dh;
758 /* Draw the bitmap. I believe these small pixmaps can be cached
759 by the server. */
760 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
761 (p->cursor_p
762 ? (p->overlay_p ? face->background
763 : f->output_data.x->cursor_pixel)
764 : face->foreground),
765 face->background, depth);
767 if (p->overlay_p)
769 clipmask = XCreatePixmapFromBitmapData (display,
770 FRAME_DISPLAY_INFO (f)->root_window,
771 bits, p->wd, p->h,
772 1, 0, 1);
773 gcv.clip_mask = clipmask;
774 gcv.clip_x_origin = p->x;
775 gcv.clip_y_origin = p->y;
776 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
779 XCopyArea (display, pixmap, window, gc, 0, 0,
780 p->wd, p->h, p->x, p->y);
781 XFreePixmap (display, pixmap);
783 if (p->overlay_p)
785 gcv.clip_mask = (Pixmap) 0;
786 XChangeGC (display, gc, GCClipMask, &gcv);
787 XFreePixmap (display, clipmask);
791 XSetClipMask (display, gc, None);
794 /***********************************************************************
795 Glyph display
796 ***********************************************************************/
800 static void x_set_glyph_string_clipping (struct glyph_string *);
801 static void x_set_glyph_string_gc (struct glyph_string *);
802 static void x_draw_glyph_string_foreground (struct glyph_string *);
803 static void x_draw_composite_glyph_string_foreground (struct glyph_string *);
804 static void x_draw_glyph_string_box (struct glyph_string *);
805 static void x_draw_glyph_string (struct glyph_string *);
806 static _Noreturn void x_delete_glyphs (struct frame *, int);
807 static void x_compute_glyph_string_overhangs (struct glyph_string *);
808 static void x_set_cursor_gc (struct glyph_string *);
809 static void x_set_mode_line_face_gc (struct glyph_string *);
810 static void x_set_mouse_face_gc (struct glyph_string *);
811 static bool x_alloc_lighter_color (struct frame *, Display *, Colormap,
812 unsigned long *, double, int);
813 static void x_setup_relief_color (struct frame *, struct relief *,
814 double, int, unsigned long);
815 static void x_setup_relief_colors (struct glyph_string *);
816 static void x_draw_image_glyph_string (struct glyph_string *);
817 static void x_draw_image_relief (struct glyph_string *);
818 static void x_draw_image_foreground (struct glyph_string *);
819 static void x_draw_image_foreground_1 (struct glyph_string *, Pixmap);
820 static void x_clear_glyph_string_rect (struct glyph_string *, int,
821 int, int, int);
822 static void x_draw_relief_rect (struct frame *, int, int, int, int,
823 int, bool, bool, bool, bool, bool,
824 XRectangle *);
825 static void x_draw_box_rect (struct glyph_string *, int, int, int, int,
826 int, bool, bool, XRectangle *);
827 static void x_scroll_bar_clear (struct frame *);
829 #ifdef GLYPH_DEBUG
830 static void x_check_font (struct frame *, struct font *);
831 #endif
834 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
835 face. */
837 static void
838 x_set_cursor_gc (struct glyph_string *s)
840 if (s->font == FRAME_FONT (s->f)
841 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
842 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
843 && !s->cmp)
844 s->gc = s->f->output_data.x->cursor_gc;
845 else
847 /* Cursor on non-default face: must merge. */
848 XGCValues xgcv;
849 unsigned long mask;
851 xgcv.background = s->f->output_data.x->cursor_pixel;
852 xgcv.foreground = s->face->background;
854 /* If the glyph would be invisible, try a different foreground. */
855 if (xgcv.foreground == xgcv.background)
856 xgcv.foreground = s->face->foreground;
857 if (xgcv.foreground == xgcv.background)
858 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
859 if (xgcv.foreground == xgcv.background)
860 xgcv.foreground = s->face->foreground;
862 /* Make sure the cursor is distinct from text in this face. */
863 if (xgcv.background == s->face->background
864 && xgcv.foreground == s->face->foreground)
866 xgcv.background = s->face->foreground;
867 xgcv.foreground = s->face->background;
870 IF_DEBUG (x_check_font (s->f, s->font));
871 xgcv.graphics_exposures = False;
872 mask = GCForeground | GCBackground | GCGraphicsExposures;
874 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
875 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
876 mask, &xgcv);
877 else
878 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
879 = XCreateGC (s->display, s->window, mask, &xgcv);
881 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
886 /* Set up S->gc of glyph string S for drawing text in mouse face. */
888 static void
889 x_set_mouse_face_gc (struct glyph_string *s)
891 int face_id;
892 struct face *face;
894 /* What face has to be used last for the mouse face? */
895 face_id = MOUSE_HL_INFO (s->f)->mouse_face_face_id;
896 face = FACE_FROM_ID (s->f, face_id);
897 if (face == NULL)
898 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
900 if (s->first_glyph->type == CHAR_GLYPH)
901 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
902 else
903 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
904 s->face = FACE_FROM_ID (s->f, face_id);
905 prepare_face_for_display (s->f, s->face);
907 if (s->font == s->face->font)
908 s->gc = s->face->gc;
909 else
911 /* Otherwise construct scratch_cursor_gc with values from FACE
912 except for FONT. */
913 XGCValues xgcv;
914 unsigned long mask;
916 xgcv.background = s->face->background;
917 xgcv.foreground = s->face->foreground;
918 xgcv.graphics_exposures = False;
919 mask = GCForeground | GCBackground | GCGraphicsExposures;
921 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
922 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
923 mask, &xgcv);
924 else
925 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
926 = XCreateGC (s->display, s->window, mask, &xgcv);
928 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
931 eassert (s->gc != 0);
935 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
936 Faces to use in the mode line have already been computed when the
937 matrix was built, so there isn't much to do, here. */
939 static void
940 x_set_mode_line_face_gc (struct glyph_string *s)
942 s->gc = s->face->gc;
946 /* Set S->gc of glyph string S for drawing that glyph string. Set
947 S->stippled_p to a non-zero value if the face of S has a stipple
948 pattern. */
950 static void
951 x_set_glyph_string_gc (struct glyph_string *s)
953 prepare_face_for_display (s->f, s->face);
955 if (s->hl == DRAW_NORMAL_TEXT)
957 s->gc = s->face->gc;
958 s->stippled_p = s->face->stipple != 0;
960 else if (s->hl == DRAW_INVERSE_VIDEO)
962 x_set_mode_line_face_gc (s);
963 s->stippled_p = s->face->stipple != 0;
965 else if (s->hl == DRAW_CURSOR)
967 x_set_cursor_gc (s);
968 s->stippled_p = false;
970 else if (s->hl == DRAW_MOUSE_FACE)
972 x_set_mouse_face_gc (s);
973 s->stippled_p = s->face->stipple != 0;
975 else if (s->hl == DRAW_IMAGE_RAISED
976 || s->hl == DRAW_IMAGE_SUNKEN)
978 s->gc = s->face->gc;
979 s->stippled_p = s->face->stipple != 0;
981 else
982 emacs_abort ();
984 /* GC must have been set. */
985 eassert (s->gc != 0);
989 /* Set clipping for output of glyph string S. S may be part of a mode
990 line or menu if we don't have X toolkit support. */
992 static void
993 x_set_glyph_string_clipping (struct glyph_string *s)
995 XRectangle *r = s->clip;
996 int n = get_glyph_string_clip_rects (s, r, 2);
998 if (n > 0)
999 XSetClipRectangles (s->display, s->gc, 0, 0, r, n, Unsorted);
1000 s->num_clips = n;
1004 /* Set SRC's clipping for output of glyph string DST. This is called
1005 when we are drawing DST's left_overhang or right_overhang only in
1006 the area of SRC. */
1008 static void
1009 x_set_glyph_string_clipping_exactly (struct glyph_string *src, struct glyph_string *dst)
1011 XRectangle r;
1013 r.x = src->x;
1014 r.width = src->width;
1015 r.y = src->y;
1016 r.height = src->height;
1017 dst->clip[0] = r;
1018 dst->num_clips = 1;
1019 XSetClipRectangles (dst->display, dst->gc, 0, 0, &r, 1, Unsorted);
1023 /* RIF:
1024 Compute left and right overhang of glyph string S. */
1026 static void
1027 x_compute_glyph_string_overhangs (struct glyph_string *s)
1029 if (s->cmp == NULL
1030 && (s->first_glyph->type == CHAR_GLYPH
1031 || s->first_glyph->type == COMPOSITE_GLYPH))
1033 struct font_metrics metrics;
1035 if (s->first_glyph->type == CHAR_GLYPH)
1037 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1038 struct font *font = s->font;
1039 int i;
1041 for (i = 0; i < s->nchars; i++)
1042 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1043 font->driver->text_extents (font, code, s->nchars, &metrics);
1045 else
1047 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1049 composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
1051 s->right_overhang = (metrics.rbearing > metrics.width
1052 ? metrics.rbearing - metrics.width : 0);
1053 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1055 else if (s->cmp)
1057 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1058 s->left_overhang = - s->cmp->lbearing;
1063 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1065 static void
1066 x_clear_glyph_string_rect (struct glyph_string *s, int x, int y, int w, int h)
1068 XGCValues xgcv;
1069 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1070 XSetForeground (s->display, s->gc, xgcv.background);
1071 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1072 XSetForeground (s->display, s->gc, xgcv.foreground);
1076 /* Draw the background of glyph_string S. If S->background_filled_p
1077 is non-zero don't draw it. FORCE_P non-zero means draw the
1078 background even if it wouldn't be drawn normally. This is used
1079 when a string preceding S draws into the background of S, or S
1080 contains the first component of a composition. */
1082 static void
1083 x_draw_glyph_string_background (struct glyph_string *s, bool force_p)
1085 /* Nothing to do if background has already been drawn or if it
1086 shouldn't be drawn in the first place. */
1087 if (!s->background_filled_p)
1089 int box_line_width = max (s->face->box_line_width, 0);
1091 if (s->stippled_p)
1093 /* Fill background with a stipple pattern. */
1094 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1095 XFillRectangle (s->display, s->window, s->gc, s->x,
1096 s->y + box_line_width,
1097 s->background_width,
1098 s->height - 2 * box_line_width);
1099 XSetFillStyle (s->display, s->gc, FillSolid);
1100 s->background_filled_p = true;
1102 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1103 || s->font_not_found_p
1104 || s->extends_to_end_of_line_p
1105 || force_p)
1107 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1108 s->background_width,
1109 s->height - 2 * box_line_width);
1110 s->background_filled_p = true;
1116 /* Draw the foreground of glyph string S. */
1118 static void
1119 x_draw_glyph_string_foreground (struct glyph_string *s)
1121 int i, x;
1123 /* If first glyph of S has a left box line, start drawing the text
1124 of S to the right of that box line. */
1125 if (s->face->box != FACE_NO_BOX
1126 && s->first_glyph->left_box_line_p)
1127 x = s->x + eabs (s->face->box_line_width);
1128 else
1129 x = s->x;
1131 /* Draw characters of S as rectangles if S's font could not be
1132 loaded. */
1133 if (s->font_not_found_p)
1135 for (i = 0; i < s->nchars; ++i)
1137 struct glyph *g = s->first_glyph + i;
1138 XDrawRectangle (s->display, s->window,
1139 s->gc, x, s->y, g->pixel_width - 1,
1140 s->height - 1);
1141 x += g->pixel_width;
1144 else
1146 struct font *font = s->font;
1147 int boff = font->baseline_offset;
1148 int y;
1150 if (font->vertical_centering)
1151 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1153 y = s->ybase - boff;
1154 if (s->for_overlaps
1155 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1156 font->driver->draw (s, 0, s->nchars, x, y, false);
1157 else
1158 font->driver->draw (s, 0, s->nchars, x, y, true);
1159 if (s->face->overstrike)
1160 font->driver->draw (s, 0, s->nchars, x + 1, y, false);
1164 /* Draw the foreground of composite glyph string S. */
1166 static void
1167 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1169 int i, j, x;
1170 struct font *font = s->font;
1172 /* If first glyph of S has a left box line, start drawing the text
1173 of S to the right of that box line. */
1174 if (s->face && s->face->box != FACE_NO_BOX
1175 && s->first_glyph->left_box_line_p)
1176 x = s->x + eabs (s->face->box_line_width);
1177 else
1178 x = s->x;
1180 /* S is a glyph string for a composition. S->cmp_from is the index
1181 of the first character drawn for glyphs of this composition.
1182 S->cmp_from == 0 means we are drawing the very first character of
1183 this composition. */
1185 /* Draw a rectangle for the composition if the font for the very
1186 first character of the composition could not be loaded. */
1187 if (s->font_not_found_p)
1189 if (s->cmp_from == 0)
1190 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1191 s->width - 1, s->height - 1);
1193 else if (! s->first_glyph->u.cmp.automatic)
1195 int y = s->ybase;
1197 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1198 /* TAB in a composition means display glyphs with padding
1199 space on the left or right. */
1200 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1202 int xx = x + s->cmp->offsets[j * 2];
1203 int yy = y - s->cmp->offsets[j * 2 + 1];
1205 font->driver->draw (s, j, j + 1, xx, yy, false);
1206 if (s->face->overstrike)
1207 font->driver->draw (s, j, j + 1, xx + 1, yy, false);
1210 else
1212 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1213 Lisp_Object glyph;
1214 int y = s->ybase;
1215 int width = 0;
1217 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1219 glyph = LGSTRING_GLYPH (gstring, i);
1220 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1221 width += LGLYPH_WIDTH (glyph);
1222 else
1224 int xoff, yoff, wadjust;
1226 if (j < i)
1228 font->driver->draw (s, j, i, x, y, false);
1229 if (s->face->overstrike)
1230 font->driver->draw (s, j, i, x + 1, y, false);
1231 x += width;
1233 xoff = LGLYPH_XOFF (glyph);
1234 yoff = LGLYPH_YOFF (glyph);
1235 wadjust = LGLYPH_WADJUST (glyph);
1236 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, false);
1237 if (s->face->overstrike)
1238 font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff,
1239 false);
1240 x += wadjust;
1241 j = i + 1;
1242 width = 0;
1245 if (j < i)
1247 font->driver->draw (s, j, i, x, y, false);
1248 if (s->face->overstrike)
1249 font->driver->draw (s, j, i, x + 1, y, false);
1255 /* Draw the foreground of glyph string S for glyphless characters. */
1257 static void
1258 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1260 struct glyph *glyph = s->first_glyph;
1261 XChar2b char2b[8];
1262 int x, i, j;
1264 /* If first glyph of S has a left box line, start drawing the text
1265 of S to the right of that box line. */
1266 if (s->face && s->face->box != FACE_NO_BOX
1267 && s->first_glyph->left_box_line_p)
1268 x = s->x + eabs (s->face->box_line_width);
1269 else
1270 x = s->x;
1272 s->char2b = char2b;
1274 for (i = 0; i < s->nchars; i++, glyph++)
1276 char buf[7], *str = NULL;
1277 int len = glyph->u.glyphless.len;
1279 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1281 if (len > 0
1282 && CHAR_TABLE_P (Vglyphless_char_display)
1283 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1284 >= 1))
1286 Lisp_Object acronym
1287 = (! glyph->u.glyphless.for_no_font
1288 ? CHAR_TABLE_REF (Vglyphless_char_display,
1289 glyph->u.glyphless.ch)
1290 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
1291 if (STRINGP (acronym))
1292 str = SSDATA (acronym);
1295 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
1297 sprintf (buf, "%0*X",
1298 glyph->u.glyphless.ch < 0x10000 ? 4 : 6,
1299 glyph->u.glyphless.ch);
1300 str = buf;
1303 if (str)
1305 int upper_len = (len + 1) / 2;
1306 unsigned code;
1308 /* It is assured that all LEN characters in STR is ASCII. */
1309 for (j = 0; j < len; j++)
1311 code = s->font->driver->encode_char (s->font, str[j]);
1312 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
1314 s->font->driver->draw (s, 0, upper_len,
1315 x + glyph->slice.glyphless.upper_xoff,
1316 s->ybase + glyph->slice.glyphless.upper_yoff,
1317 false);
1318 s->font->driver->draw (s, upper_len, len,
1319 x + glyph->slice.glyphless.lower_xoff,
1320 s->ybase + glyph->slice.glyphless.lower_yoff,
1321 false);
1323 if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
1324 XDrawRectangle (s->display, s->window, s->gc,
1325 x, s->ybase - glyph->ascent,
1326 glyph->pixel_width - 1,
1327 glyph->ascent + glyph->descent - 1);
1328 x += glyph->pixel_width;
1332 #ifdef USE_X_TOOLKIT
1334 #ifdef USE_LUCID
1336 /* Return the frame on which widget WIDGET is used.. Abort if frame
1337 cannot be determined. */
1339 static struct frame *
1340 x_frame_of_widget (Widget widget)
1342 struct x_display_info *dpyinfo;
1343 Lisp_Object tail, frame;
1344 struct frame *f;
1346 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1348 /* Find the top-level shell of the widget. Note that this function
1349 can be called when the widget is not yet realized, so XtWindow
1350 (widget) == 0. That's the reason we can't simply use
1351 x_any_window_to_frame. */
1352 while (!XtIsTopLevelShell (widget))
1353 widget = XtParent (widget);
1355 /* Look for a frame with that top-level widget. Allocate the color
1356 on that frame to get the right gamma correction value. */
1357 FOR_EACH_FRAME (tail, frame)
1359 f = XFRAME (frame);
1360 if (FRAME_X_P (f)
1361 && f->output_data.nothing != 1
1362 && FRAME_DISPLAY_INFO (f) == dpyinfo
1363 && f->output_data.x->widget == widget)
1364 return f;
1366 emacs_abort ();
1369 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1370 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1371 If this produces the same color as PIXEL, try a color where all RGB
1372 values have DELTA added. Return the allocated color in *PIXEL.
1373 DISPLAY is the X display, CMAP is the colormap to operate on.
1374 Value is true if successful. */
1376 bool
1377 x_alloc_lighter_color_for_widget (Widget widget, Display *display, Colormap cmap,
1378 unsigned long *pixel, double factor, int delta)
1380 struct frame *f = x_frame_of_widget (widget);
1381 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1384 #endif /* USE_LUCID */
1387 /* Structure specifying which arguments should be passed by Xt to
1388 cvt_string_to_pixel. We want the widget's screen and colormap. */
1390 static XtConvertArgRec cvt_string_to_pixel_args[] =
1392 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1393 sizeof (Screen *)},
1394 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1395 sizeof (Colormap)}
1399 /* The address of this variable is returned by
1400 cvt_string_to_pixel. */
1402 static Pixel cvt_string_to_pixel_value;
1405 /* Convert a color name to a pixel color.
1407 DPY is the display we are working on.
1409 ARGS is an array of *NARGS XrmValue structures holding additional
1410 information about the widget for which the conversion takes place.
1411 The contents of this array are determined by the specification
1412 in cvt_string_to_pixel_args.
1414 FROM is a pointer to an XrmValue which points to the color name to
1415 convert. TO is an XrmValue in which to return the pixel color.
1417 CLOSURE_RET is a pointer to user-data, in which we record if
1418 we allocated the color or not.
1420 Value is True if successful, False otherwise. */
1422 static Boolean
1423 cvt_string_to_pixel (Display *dpy, XrmValue *args, Cardinal *nargs,
1424 XrmValue *from, XrmValue *to,
1425 XtPointer *closure_ret)
1427 Screen *screen;
1428 Colormap cmap;
1429 Pixel pixel;
1430 String color_name;
1431 XColor color;
1433 if (*nargs != 2)
1435 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1436 "wrongParameters", "cvt_string_to_pixel",
1437 "XtToolkitError",
1438 "Screen and colormap args required", NULL, NULL);
1439 return False;
1442 screen = *(Screen **) args[0].addr;
1443 cmap = *(Colormap *) args[1].addr;
1444 color_name = (String) from->addr;
1446 if (strcmp (color_name, XtDefaultBackground) == 0)
1448 *closure_ret = (XtPointer) False;
1449 pixel = WhitePixelOfScreen (screen);
1451 else if (strcmp (color_name, XtDefaultForeground) == 0)
1453 *closure_ret = (XtPointer) False;
1454 pixel = BlackPixelOfScreen (screen);
1456 else if (XParseColor (dpy, cmap, color_name, &color)
1457 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1459 pixel = color.pixel;
1460 *closure_ret = (XtPointer) True;
1462 else
1464 String params[1];
1465 Cardinal nparams = 1;
1467 params[0] = color_name;
1468 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1469 "badValue", "cvt_string_to_pixel",
1470 "XtToolkitError", "Invalid color `%s'",
1471 params, &nparams);
1472 return False;
1475 if (to->addr != NULL)
1477 if (to->size < sizeof (Pixel))
1479 to->size = sizeof (Pixel);
1480 return False;
1483 *(Pixel *) to->addr = pixel;
1485 else
1487 cvt_string_to_pixel_value = pixel;
1488 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1491 to->size = sizeof (Pixel);
1492 return True;
1496 /* Free a pixel color which was previously allocated via
1497 cvt_string_to_pixel. This is registered as the destructor
1498 for this type of resource via XtSetTypeConverter.
1500 APP is the application context in which we work.
1502 TO is a pointer to an XrmValue holding the color to free.
1503 CLOSURE is the value we stored in CLOSURE_RET for this color
1504 in cvt_string_to_pixel.
1506 ARGS and NARGS are like for cvt_string_to_pixel. */
1508 static void
1509 cvt_pixel_dtor (XtAppContext app, XrmValuePtr to, XtPointer closure, XrmValuePtr args,
1510 Cardinal *nargs)
1512 if (*nargs != 2)
1514 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1515 "XtToolkitError",
1516 "Screen and colormap arguments required",
1517 NULL, NULL);
1519 else if (closure != NULL)
1521 /* We did allocate the pixel, so free it. */
1522 Screen *screen = *(Screen **) args[0].addr;
1523 Colormap cmap = *(Colormap *) args[1].addr;
1524 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1525 (Pixel *) to->addr, 1);
1530 #endif /* USE_X_TOOLKIT */
1533 /* Value is an array of XColor structures for the contents of the
1534 color map of display DPY. Set *NCELLS to the size of the array.
1535 Note that this probably shouldn't be called for large color maps,
1536 say a 24-bit TrueColor map. */
1538 static const XColor *
1539 x_color_cells (Display *dpy, int *ncells)
1541 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1543 if (dpyinfo->color_cells == NULL)
1545 Screen *screen = dpyinfo->screen;
1546 int ncolor_cells = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1547 int i;
1549 dpyinfo->color_cells = xnmalloc (ncolor_cells,
1550 sizeof *dpyinfo->color_cells);
1551 dpyinfo->ncolor_cells = ncolor_cells;
1553 for (i = 0; i < ncolor_cells; ++i)
1554 dpyinfo->color_cells[i].pixel = i;
1556 XQueryColors (dpy, dpyinfo->cmap,
1557 dpyinfo->color_cells, ncolor_cells);
1560 *ncells = dpyinfo->ncolor_cells;
1561 return dpyinfo->color_cells;
1565 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1566 colors in COLORS. Use cached information, if available. */
1568 void
1569 x_query_colors (struct frame *f, XColor *colors, int ncolors)
1571 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1573 if (dpyinfo->color_cells)
1575 int i;
1576 for (i = 0; i < ncolors; ++i)
1578 unsigned long pixel = colors[i].pixel;
1579 eassert (pixel < dpyinfo->ncolor_cells);
1580 eassert (dpyinfo->color_cells[pixel].pixel == pixel);
1581 colors[i] = dpyinfo->color_cells[pixel];
1584 else
1585 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1589 /* On frame F, translate pixel color to RGB values for the color in
1590 COLOR. Use cached information, if available. */
1592 void
1593 x_query_color (struct frame *f, XColor *color)
1595 x_query_colors (f, color, 1);
1599 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1600 exact match can't be allocated, try the nearest color available.
1601 Value is true if successful. Set *COLOR to the color
1602 allocated. */
1604 static bool
1605 x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color)
1607 bool rc;
1609 rc = XAllocColor (dpy, cmap, color) != 0;
1610 if (rc == 0)
1612 /* If we got to this point, the colormap is full, so we're going
1613 to try to get the next closest color. The algorithm used is
1614 a least-squares matching, which is what X uses for closest
1615 color matching with StaticColor visuals. */
1616 int nearest, i;
1617 int max_color_delta = 255;
1618 int max_delta = 3 * max_color_delta;
1619 int nearest_delta = max_delta + 1;
1620 int ncells;
1621 const XColor *cells = x_color_cells (dpy, &ncells);
1623 for (nearest = i = 0; i < ncells; ++i)
1625 int dred = (color->red >> 8) - (cells[i].red >> 8);
1626 int dgreen = (color->green >> 8) - (cells[i].green >> 8);
1627 int dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1628 int delta = dred * dred + dgreen * dgreen + dblue * dblue;
1630 if (delta < nearest_delta)
1632 nearest = i;
1633 nearest_delta = delta;
1637 color->red = cells[nearest].red;
1638 color->green = cells[nearest].green;
1639 color->blue = cells[nearest].blue;
1640 rc = XAllocColor (dpy, cmap, color) != 0;
1642 else
1644 /* If allocation succeeded, and the allocated pixel color is not
1645 equal to a cached pixel color recorded earlier, there was a
1646 change in the colormap, so clear the color cache. */
1647 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1648 XColor *cached_color;
1650 if (dpyinfo->color_cells
1651 && (cached_color = &dpyinfo->color_cells[color->pixel],
1652 (cached_color->red != color->red
1653 || cached_color->blue != color->blue
1654 || cached_color->green != color->green)))
1656 xfree (dpyinfo->color_cells);
1657 dpyinfo->color_cells = NULL;
1658 dpyinfo->ncolor_cells = 0;
1662 #ifdef DEBUG_X_COLORS
1663 if (rc)
1664 register_color (color->pixel);
1665 #endif /* DEBUG_X_COLORS */
1667 return rc;
1671 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1672 exact match can't be allocated, try the nearest color available.
1673 Value is true if successful. Set *COLOR to the color
1674 allocated. */
1676 bool
1677 x_alloc_nearest_color (struct frame *f, Colormap cmap, XColor *color)
1679 gamma_correct (f, color);
1680 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1684 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1685 It's necessary to do this instead of just using PIXEL directly to
1686 get color reference counts right. */
1688 unsigned long
1689 x_copy_color (struct frame *f, unsigned long pixel)
1691 XColor color;
1693 color.pixel = pixel;
1694 block_input ();
1695 x_query_color (f, &color);
1696 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1697 unblock_input ();
1698 #ifdef DEBUG_X_COLORS
1699 register_color (pixel);
1700 #endif
1701 return color.pixel;
1705 /* Brightness beyond which a color won't have its highlight brightness
1706 boosted.
1708 Nominally, highlight colors for `3d' faces are calculated by
1709 brightening an object's color by a constant scale factor, but this
1710 doesn't yield good results for dark colors, so for colors who's
1711 brightness is less than this value (on a scale of 0-65535) have an
1712 use an additional additive factor.
1714 The value here is set so that the default menu-bar/mode-line color
1715 (grey75) will not have its highlights changed at all. */
1716 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1719 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1720 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1721 If this produces the same color as PIXEL, try a color where all RGB
1722 values have DELTA added. Return the allocated color in *PIXEL.
1723 DISPLAY is the X display, CMAP is the colormap to operate on.
1724 Value is non-zero if successful. */
1726 static bool
1727 x_alloc_lighter_color (struct frame *f, Display *display, Colormap cmap,
1728 unsigned long *pixel, double factor, int delta)
1730 XColor color, new;
1731 long bright;
1732 bool success_p;
1734 /* Get RGB color values. */
1735 color.pixel = *pixel;
1736 x_query_color (f, &color);
1738 /* Change RGB values by specified FACTOR. Avoid overflow! */
1739 eassert (factor >= 0);
1740 new.red = min (0xffff, factor * color.red);
1741 new.green = min (0xffff, factor * color.green);
1742 new.blue = min (0xffff, factor * color.blue);
1744 /* Calculate brightness of COLOR. */
1745 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1747 /* We only boost colors that are darker than
1748 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1749 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1750 /* Make an additive adjustment to NEW, because it's dark enough so
1751 that scaling by FACTOR alone isn't enough. */
1753 /* How far below the limit this color is (0 - 1, 1 being darker). */
1754 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1755 /* The additive adjustment. */
1756 int min_delta = delta * dimness * factor / 2;
1758 if (factor < 1)
1760 new.red = max (0, new.red - min_delta);
1761 new.green = max (0, new.green - min_delta);
1762 new.blue = max (0, new.blue - min_delta);
1764 else
1766 new.red = min (0xffff, min_delta + new.red);
1767 new.green = min (0xffff, min_delta + new.green);
1768 new.blue = min (0xffff, min_delta + new.blue);
1772 /* Try to allocate the color. */
1773 success_p = x_alloc_nearest_color (f, cmap, &new);
1774 if (success_p)
1776 if (new.pixel == *pixel)
1778 /* If we end up with the same color as before, try adding
1779 delta to the RGB values. */
1780 x_free_colors (f, &new.pixel, 1);
1782 new.red = min (0xffff, delta + color.red);
1783 new.green = min (0xffff, delta + color.green);
1784 new.blue = min (0xffff, delta + color.blue);
1785 success_p = x_alloc_nearest_color (f, cmap, &new);
1787 else
1788 success_p = true;
1789 *pixel = new.pixel;
1792 return success_p;
1796 /* Set up the foreground color for drawing relief lines of glyph
1797 string S. RELIEF is a pointer to a struct relief containing the GC
1798 with which lines will be drawn. Use a color that is FACTOR or
1799 DELTA lighter or darker than the relief's background which is found
1800 in S->f->output_data.x->relief_background. If such a color cannot
1801 be allocated, use DEFAULT_PIXEL, instead. */
1803 static void
1804 x_setup_relief_color (struct frame *f, struct relief *relief, double factor,
1805 int delta, unsigned long default_pixel)
1807 XGCValues xgcv;
1808 struct x_output *di = f->output_data.x;
1809 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1810 unsigned long pixel;
1811 unsigned long background = di->relief_background;
1812 Colormap cmap = FRAME_X_COLORMAP (f);
1813 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1814 Display *dpy = FRAME_X_DISPLAY (f);
1816 xgcv.graphics_exposures = False;
1817 xgcv.line_width = 1;
1819 /* Free previously allocated color. The color cell will be reused
1820 when it has been freed as many times as it was allocated, so this
1821 doesn't affect faces using the same colors. */
1822 if (relief->gc && relief->pixel != -1)
1824 x_free_colors (f, &relief->pixel, 1);
1825 relief->pixel = -1;
1828 /* Allocate new color. */
1829 xgcv.foreground = default_pixel;
1830 pixel = background;
1831 if (dpyinfo->n_planes != 1
1832 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1833 xgcv.foreground = relief->pixel = pixel;
1835 if (relief->gc == 0)
1837 xgcv.stipple = dpyinfo->gray;
1838 mask |= GCStipple;
1839 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1841 else
1842 XChangeGC (dpy, relief->gc, mask, &xgcv);
1846 /* Set up colors for the relief lines around glyph string S. */
1848 static void
1849 x_setup_relief_colors (struct glyph_string *s)
1851 struct x_output *di = s->f->output_data.x;
1852 unsigned long color;
1854 if (s->face->use_box_color_for_shadows_p)
1855 color = s->face->box_color;
1856 else if (s->first_glyph->type == IMAGE_GLYPH
1857 && s->img->pixmap
1858 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1859 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1860 else
1862 XGCValues xgcv;
1864 /* Get the background color of the face. */
1865 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
1866 color = xgcv.background;
1869 if (di->white_relief.gc == 0
1870 || color != di->relief_background)
1872 di->relief_background = color;
1873 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1874 WHITE_PIX_DEFAULT (s->f));
1875 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1876 BLACK_PIX_DEFAULT (s->f));
1881 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
1882 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
1883 to draw, it must be >= 0. RAISED_P means draw a raised
1884 relief. LEFT_P means draw a relief on the left side of
1885 the rectangle. RIGHT_P means draw a relief on the right
1886 side of the rectangle. CLIP_RECT is the clipping rectangle to use
1887 when drawing. */
1889 static void
1890 x_draw_relief_rect (struct frame *f,
1891 int left_x, int top_y, int right_x, int bottom_y,
1892 int width, bool raised_p, bool top_p, bool bot_p,
1893 bool left_p, bool right_p,
1894 XRectangle *clip_rect)
1896 Display *dpy = FRAME_X_DISPLAY (f);
1897 Window window = FRAME_X_WINDOW (f);
1898 int i;
1899 GC gc;
1901 if (raised_p)
1902 gc = f->output_data.x->white_relief.gc;
1903 else
1904 gc = f->output_data.x->black_relief.gc;
1905 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
1907 /* This code is more complicated than it has to be, because of two
1908 minor hacks to make the boxes look nicer: (i) if width > 1, draw
1909 the outermost line using the black relief. (ii) Omit the four
1910 corner pixels. */
1912 /* Top. */
1913 if (top_p)
1915 if (width == 1)
1916 XDrawLine (dpy, window, gc,
1917 left_x + left_p, top_y,
1918 right_x + !right_p, top_y);
1920 for (i = 1; i < width; ++i)
1921 XDrawLine (dpy, window, gc,
1922 left_x + i * left_p, top_y + i,
1923 right_x + 1 - i * right_p, top_y + i);
1926 /* Left. */
1927 if (left_p)
1929 if (width == 1)
1930 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
1932 XClearArea (dpy, window, left_x, top_y, 1, 1, False);
1933 XClearArea (dpy, window, left_x, bottom_y, 1, 1, False);
1935 for (i = (width > 1 ? 1 : 0); i < width; ++i)
1936 XDrawLine (dpy, window, gc,
1937 left_x + i, top_y + (i + 1) * top_p,
1938 left_x + i, bottom_y + 1 - (i + 1) * bot_p);
1941 XSetClipMask (dpy, gc, None);
1942 if (raised_p)
1943 gc = f->output_data.x->black_relief.gc;
1944 else
1945 gc = f->output_data.x->white_relief.gc;
1946 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
1948 if (width > 1)
1950 /* Outermost top line. */
1951 if (top_p)
1952 XDrawLine (dpy, window, gc,
1953 left_x + left_p, top_y,
1954 right_x + !right_p, top_y);
1956 /* Outermost left line. */
1957 if (left_p)
1958 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
1961 /* Bottom. */
1962 if (bot_p)
1964 XDrawLine (dpy, window, gc,
1965 left_x + left_p, bottom_y,
1966 right_x + !right_p, bottom_y);
1967 for (i = 1; i < width; ++i)
1968 XDrawLine (dpy, window, gc,
1969 left_x + i * left_p, bottom_y - i,
1970 right_x + 1 - i * right_p, bottom_y - i);
1973 /* Right. */
1974 if (right_p)
1976 XClearArea (dpy, window, right_x, top_y, 1, 1, False);
1977 XClearArea (dpy, window, right_x, bottom_y, 1, 1, False);
1978 for (i = 0; i < width; ++i)
1979 XDrawLine (dpy, window, gc,
1980 right_x - i, top_y + (i + 1) * top_p,
1981 right_x - i, bottom_y + 1 - (i + 1) * bot_p);
1984 XSetClipMask (dpy, gc, None);
1988 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
1989 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
1990 draw, it must be >= 0. LEFT_P means draw a line on the
1991 left side of the rectangle. RIGHT_P means draw a line
1992 on the right side of the rectangle. CLIP_RECT is the clipping
1993 rectangle to use when drawing. */
1995 static void
1996 x_draw_box_rect (struct glyph_string *s,
1997 int left_x, int top_y, int right_x, int bottom_y, int width,
1998 bool left_p, bool right_p, XRectangle *clip_rect)
2000 XGCValues xgcv;
2002 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2003 XSetForeground (s->display, s->gc, s->face->box_color);
2004 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2006 /* Top. */
2007 XFillRectangle (s->display, s->window, s->gc,
2008 left_x, top_y, right_x - left_x + 1, width);
2010 /* Left. */
2011 if (left_p)
2012 XFillRectangle (s->display, s->window, s->gc,
2013 left_x, top_y, width, bottom_y - top_y + 1);
2015 /* Bottom. */
2016 XFillRectangle (s->display, s->window, s->gc,
2017 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2019 /* Right. */
2020 if (right_p)
2021 XFillRectangle (s->display, s->window, s->gc,
2022 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2024 XSetForeground (s->display, s->gc, xgcv.foreground);
2025 XSetClipMask (s->display, s->gc, None);
2029 /* Draw a box around glyph string S. */
2031 static void
2032 x_draw_glyph_string_box (struct glyph_string *s)
2034 int width, left_x, right_x, top_y, bottom_y, last_x;
2035 bool raised_p, left_p, right_p;
2036 struct glyph *last_glyph;
2037 XRectangle clip_rect;
2039 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2040 ? WINDOW_RIGHT_EDGE_X (s->w)
2041 : window_box_right (s->w, s->area));
2043 /* The glyph that may have a right box line. */
2044 last_glyph = (s->cmp || s->img
2045 ? s->first_glyph
2046 : s->first_glyph + s->nchars - 1);
2048 width = eabs (s->face->box_line_width);
2049 raised_p = s->face->box == FACE_RAISED_BOX;
2050 left_x = s->x;
2051 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2052 ? last_x - 1
2053 : min (last_x, s->x + s->background_width) - 1);
2054 top_y = s->y;
2055 bottom_y = top_y + s->height - 1;
2057 left_p = (s->first_glyph->left_box_line_p
2058 || (s->hl == DRAW_MOUSE_FACE
2059 && (s->prev == NULL
2060 || s->prev->hl != s->hl)));
2061 right_p = (last_glyph->right_box_line_p
2062 || (s->hl == DRAW_MOUSE_FACE
2063 && (s->next == NULL
2064 || s->next->hl != s->hl)));
2066 get_glyph_string_clip_rect (s, &clip_rect);
2068 if (s->face->box == FACE_SIMPLE_BOX)
2069 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2070 left_p, right_p, &clip_rect);
2071 else
2073 x_setup_relief_colors (s);
2074 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2075 width, raised_p, true, true, left_p, right_p,
2076 &clip_rect);
2081 /* Draw foreground of image glyph string S. */
2083 static void
2084 x_draw_image_foreground (struct glyph_string *s)
2086 int x = s->x;
2087 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2089 /* If first glyph of S has a left box line, start drawing it to the
2090 right of that line. */
2091 if (s->face->box != FACE_NO_BOX
2092 && s->first_glyph->left_box_line_p
2093 && s->slice.x == 0)
2094 x += eabs (s->face->box_line_width);
2096 /* If there is a margin around the image, adjust x- and y-position
2097 by that margin. */
2098 if (s->slice.x == 0)
2099 x += s->img->hmargin;
2100 if (s->slice.y == 0)
2101 y += s->img->vmargin;
2103 if (s->img->pixmap)
2105 if (s->img->mask)
2107 /* We can't set both a clip mask and use XSetClipRectangles
2108 because the latter also sets a clip mask. We also can't
2109 trust on the shape extension to be available
2110 (XShapeCombineRegion). So, compute the rectangle to draw
2111 manually. */
2112 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2113 | GCFunction);
2114 XGCValues xgcv;
2115 XRectangle clip_rect, image_rect, r;
2117 xgcv.clip_mask = s->img->mask;
2118 xgcv.clip_x_origin = x;
2119 xgcv.clip_y_origin = y;
2120 xgcv.function = GXcopy;
2121 XChangeGC (s->display, s->gc, mask, &xgcv);
2123 get_glyph_string_clip_rect (s, &clip_rect);
2124 image_rect.x = x;
2125 image_rect.y = y;
2126 image_rect.width = s->slice.width;
2127 image_rect.height = s->slice.height;
2128 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2129 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2130 s->slice.x + r.x - x, s->slice.y + r.y - y,
2131 r.width, r.height, r.x, r.y);
2133 else
2135 XRectangle clip_rect, image_rect, r;
2137 get_glyph_string_clip_rect (s, &clip_rect);
2138 image_rect.x = x;
2139 image_rect.y = y;
2140 image_rect.width = s->slice.width;
2141 image_rect.height = s->slice.height;
2142 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2143 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2144 s->slice.x + r.x - x, s->slice.y + r.y - y,
2145 r.width, r.height, r.x, r.y);
2147 /* When the image has a mask, we can expect that at
2148 least part of a mouse highlight or a block cursor will
2149 be visible. If the image doesn't have a mask, make
2150 a block cursor visible by drawing a rectangle around
2151 the image. I believe it's looking better if we do
2152 nothing here for mouse-face. */
2153 if (s->hl == DRAW_CURSOR)
2155 int relief = eabs (s->img->relief);
2156 XDrawRectangle (s->display, s->window, s->gc,
2157 x - relief, y - relief,
2158 s->slice.width + relief*2 - 1,
2159 s->slice.height + relief*2 - 1);
2163 else
2164 /* Draw a rectangle if image could not be loaded. */
2165 XDrawRectangle (s->display, s->window, s->gc, x, y,
2166 s->slice.width - 1, s->slice.height - 1);
2170 /* Draw a relief around the image glyph string S. */
2172 static void
2173 x_draw_image_relief (struct glyph_string *s)
2175 int x1, y1, thick;
2176 bool raised_p, top_p, bot_p, left_p, right_p;
2177 int extra_x, extra_y;
2178 XRectangle r;
2179 int x = s->x;
2180 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2182 /* If first glyph of S has a left box line, start drawing it to the
2183 right of that line. */
2184 if (s->face->box != FACE_NO_BOX
2185 && s->first_glyph->left_box_line_p
2186 && s->slice.x == 0)
2187 x += eabs (s->face->box_line_width);
2189 /* If there is a margin around the image, adjust x- and y-position
2190 by that margin. */
2191 if (s->slice.x == 0)
2192 x += s->img->hmargin;
2193 if (s->slice.y == 0)
2194 y += s->img->vmargin;
2196 if (s->hl == DRAW_IMAGE_SUNKEN
2197 || s->hl == DRAW_IMAGE_RAISED)
2199 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2200 raised_p = s->hl == DRAW_IMAGE_RAISED;
2202 else
2204 thick = eabs (s->img->relief);
2205 raised_p = s->img->relief > 0;
2208 x1 = x + s->slice.width - 1;
2209 y1 = y + s->slice.height - 1;
2211 extra_x = extra_y = 0;
2212 if (s->face->id == TOOL_BAR_FACE_ID)
2214 if (CONSP (Vtool_bar_button_margin)
2215 && INTEGERP (XCAR (Vtool_bar_button_margin))
2216 && INTEGERP (XCDR (Vtool_bar_button_margin)))
2218 extra_x = XINT (XCAR (Vtool_bar_button_margin));
2219 extra_y = XINT (XCDR (Vtool_bar_button_margin));
2221 else if (INTEGERP (Vtool_bar_button_margin))
2222 extra_x = extra_y = XINT (Vtool_bar_button_margin);
2225 top_p = bot_p = left_p = right_p = false;
2227 if (s->slice.x == 0)
2228 x -= thick + extra_x, left_p = true;
2229 if (s->slice.y == 0)
2230 y -= thick + extra_y, top_p = true;
2231 if (s->slice.x + s->slice.width == s->img->width)
2232 x1 += thick + extra_x, right_p = true;
2233 if (s->slice.y + s->slice.height == s->img->height)
2234 y1 += thick + extra_y, bot_p = true;
2236 x_setup_relief_colors (s);
2237 get_glyph_string_clip_rect (s, &r);
2238 x_draw_relief_rect (s->f, x, y, x1, y1, thick, raised_p,
2239 top_p, bot_p, left_p, right_p, &r);
2243 /* Draw the foreground of image glyph string S to PIXMAP. */
2245 static void
2246 x_draw_image_foreground_1 (struct glyph_string *s, Pixmap pixmap)
2248 int x = 0;
2249 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2251 /* If first glyph of S has a left box line, start drawing it to the
2252 right of that line. */
2253 if (s->face->box != FACE_NO_BOX
2254 && s->first_glyph->left_box_line_p
2255 && s->slice.x == 0)
2256 x += eabs (s->face->box_line_width);
2258 /* If there is a margin around the image, adjust x- and y-position
2259 by that margin. */
2260 if (s->slice.x == 0)
2261 x += s->img->hmargin;
2262 if (s->slice.y == 0)
2263 y += s->img->vmargin;
2265 if (s->img->pixmap)
2267 if (s->img->mask)
2269 /* We can't set both a clip mask and use XSetClipRectangles
2270 because the latter also sets a clip mask. We also can't
2271 trust on the shape extension to be available
2272 (XShapeCombineRegion). So, compute the rectangle to draw
2273 manually. */
2274 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2275 | GCFunction);
2276 XGCValues xgcv;
2278 xgcv.clip_mask = s->img->mask;
2279 xgcv.clip_x_origin = x - s->slice.x;
2280 xgcv.clip_y_origin = y - s->slice.y;
2281 xgcv.function = GXcopy;
2282 XChangeGC (s->display, s->gc, mask, &xgcv);
2284 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2285 s->slice.x, s->slice.y,
2286 s->slice.width, s->slice.height, x, y);
2287 XSetClipMask (s->display, s->gc, None);
2289 else
2291 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2292 s->slice.x, s->slice.y,
2293 s->slice.width, s->slice.height, x, y);
2295 /* When the image has a mask, we can expect that at
2296 least part of a mouse highlight or a block cursor will
2297 be visible. If the image doesn't have a mask, make
2298 a block cursor visible by drawing a rectangle around
2299 the image. I believe it's looking better if we do
2300 nothing here for mouse-face. */
2301 if (s->hl == DRAW_CURSOR)
2303 int r = eabs (s->img->relief);
2304 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2305 s->slice.width + r*2 - 1,
2306 s->slice.height + r*2 - 1);
2310 else
2311 /* Draw a rectangle if image could not be loaded. */
2312 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2313 s->slice.width - 1, s->slice.height - 1);
2317 /* Draw part of the background of glyph string S. X, Y, W, and H
2318 give the rectangle to draw. */
2320 static void
2321 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
2323 if (s->stippled_p)
2325 /* Fill background with a stipple pattern. */
2326 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2327 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2328 XSetFillStyle (s->display, s->gc, FillSolid);
2330 else
2331 x_clear_glyph_string_rect (s, x, y, w, h);
2335 /* Draw image glyph string S.
2337 s->y
2338 s->x +-------------------------
2339 | s->face->box
2341 | +-------------------------
2342 | | s->img->margin
2344 | | +-------------------
2345 | | | the image
2349 static void
2350 x_draw_image_glyph_string (struct glyph_string *s)
2352 int box_line_hwidth = eabs (s->face->box_line_width);
2353 int box_line_vwidth = max (s->face->box_line_width, 0);
2354 int height;
2355 Pixmap pixmap = None;
2357 height = s->height;
2358 if (s->slice.y == 0)
2359 height -= box_line_vwidth;
2360 if (s->slice.y + s->slice.height >= s->img->height)
2361 height -= box_line_vwidth;
2363 /* Fill background with face under the image. Do it only if row is
2364 taller than image or if image has a clip mask to reduce
2365 flickering. */
2366 s->stippled_p = s->face->stipple != 0;
2367 if (height > s->slice.height
2368 || s->img->hmargin
2369 || s->img->vmargin
2370 || s->img->mask
2371 || s->img->pixmap == 0
2372 || s->width != s->background_width)
2374 if (s->img->mask)
2376 /* Create a pixmap as large as the glyph string. Fill it
2377 with the background color. Copy the image to it, using
2378 its mask. Copy the temporary pixmap to the display. */
2379 Screen *screen = FRAME_X_SCREEN (s->f);
2380 int depth = DefaultDepthOfScreen (screen);
2382 /* Create a pixmap as large as the glyph string. */
2383 pixmap = XCreatePixmap (s->display, s->window,
2384 s->background_width,
2385 s->height, depth);
2387 /* Don't clip in the following because we're working on the
2388 pixmap. */
2389 XSetClipMask (s->display, s->gc, None);
2391 /* Fill the pixmap with the background color/stipple. */
2392 if (s->stippled_p)
2394 /* Fill background with a stipple pattern. */
2395 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2396 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
2397 XFillRectangle (s->display, pixmap, s->gc,
2398 0, 0, s->background_width, s->height);
2399 XSetFillStyle (s->display, s->gc, FillSolid);
2400 XSetTSOrigin (s->display, s->gc, 0, 0);
2402 else
2404 XGCValues xgcv;
2405 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2406 &xgcv);
2407 XSetForeground (s->display, s->gc, xgcv.background);
2408 XFillRectangle (s->display, pixmap, s->gc,
2409 0, 0, s->background_width, s->height);
2410 XSetForeground (s->display, s->gc, xgcv.foreground);
2413 else
2415 int x = s->x;
2416 int y = s->y;
2417 int width = s->background_width;
2419 if (s->first_glyph->left_box_line_p
2420 && s->slice.x == 0)
2422 x += box_line_hwidth;
2423 width -= box_line_hwidth;
2426 if (s->slice.y == 0)
2427 y += box_line_vwidth;
2429 x_draw_glyph_string_bg_rect (s, x, y, width, height);
2432 s->background_filled_p = true;
2435 /* Draw the foreground. */
2436 if (pixmap != None)
2438 x_draw_image_foreground_1 (s, pixmap);
2439 x_set_glyph_string_clipping (s);
2440 XCopyArea (s->display, pixmap, s->window, s->gc,
2441 0, 0, s->background_width, s->height, s->x, s->y);
2442 XFreePixmap (s->display, pixmap);
2444 else
2445 x_draw_image_foreground (s);
2447 /* If we must draw a relief around the image, do it. */
2448 if (s->img->relief
2449 || s->hl == DRAW_IMAGE_RAISED
2450 || s->hl == DRAW_IMAGE_SUNKEN)
2451 x_draw_image_relief (s);
2455 /* Draw stretch glyph string S. */
2457 static void
2458 x_draw_stretch_glyph_string (struct glyph_string *s)
2460 eassert (s->first_glyph->type == STRETCH_GLYPH);
2462 if (s->hl == DRAW_CURSOR
2463 && !x_stretch_cursor_p)
2465 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
2466 wide as the stretch glyph. */
2467 int width, background_width = s->background_width;
2468 int x = s->x;
2470 if (!s->row->reversed_p)
2472 int left_x = window_box_left_offset (s->w, TEXT_AREA);
2474 if (x < left_x)
2476 background_width -= left_x - x;
2477 x = left_x;
2480 else
2482 /* In R2L rows, draw the cursor on the right edge of the
2483 stretch glyph. */
2484 int right_x = window_box_right (s->w, TEXT_AREA);
2486 if (x + background_width > right_x)
2487 background_width -= x - right_x;
2488 x += background_width;
2490 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2491 if (s->row->reversed_p)
2492 x -= width;
2494 /* Draw cursor. */
2495 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2497 /* Clear rest using the GC of the original non-cursor face. */
2498 if (width < background_width)
2500 int y = s->y;
2501 int w = background_width - width, h = s->height;
2502 XRectangle r;
2503 GC gc;
2505 if (!s->row->reversed_p)
2506 x += width;
2507 else
2508 x = s->x;
2509 if (s->row->mouse_face_p
2510 && cursor_in_mouse_face_p (s->w))
2512 x_set_mouse_face_gc (s);
2513 gc = s->gc;
2515 else
2516 gc = s->face->gc;
2518 get_glyph_string_clip_rect (s, &r);
2519 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2521 if (s->face->stipple)
2523 /* Fill background with a stipple pattern. */
2524 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2525 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2526 XSetFillStyle (s->display, gc, FillSolid);
2528 else
2530 XGCValues xgcv;
2531 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2532 XSetForeground (s->display, gc, xgcv.background);
2533 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2534 XSetForeground (s->display, gc, xgcv.foreground);
2537 XSetClipMask (s->display, gc, None);
2540 else if (!s->background_filled_p)
2542 int background_width = s->background_width;
2543 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2545 /* Don't draw into left margin, fringe or scrollbar area
2546 except for header line and mode line. */
2547 if (x < left_x && !s->row->mode_line_p)
2549 background_width -= left_x - x;
2550 x = left_x;
2552 if (background_width > 0)
2553 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2556 s->background_filled_p = true;
2560 Draw a wavy line under S. The wave fills wave_height pixels from y0.
2562 x0 wave_length = 2
2564 y0 * * * * *
2565 |* * * * * * * * *
2566 wave_height = 3 | * * * *
2570 static void
2571 x_draw_underwave (struct glyph_string *s)
2573 int wave_height = 3, wave_length = 2;
2574 int dx, dy, x0, y0, width, x1, y1, x2, y2, xmax;
2575 bool odd;
2576 XRectangle wave_clip, string_clip, final_clip;
2578 dx = wave_length;
2579 dy = wave_height - 1;
2580 x0 = s->x;
2581 y0 = s->ybase - wave_height + 3;
2582 width = s->width;
2583 xmax = x0 + width;
2585 /* Find and set clipping rectangle */
2587 wave_clip.x = x0;
2588 wave_clip.y = y0;
2589 wave_clip.width = width;
2590 wave_clip.height = wave_height;
2591 get_glyph_string_clip_rect (s, &string_clip);
2593 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
2594 return;
2596 XSetClipRectangles (s->display, s->gc, 0, 0, &final_clip, 1, Unsorted);
2598 /* Draw the waves */
2600 x1 = x0 - (x0 % dx);
2601 x2 = x1 + dx;
2602 odd = (x1 / dx) & 1;
2603 y1 = y2 = y0;
2605 if (odd)
2606 y1 += dy;
2607 else
2608 y2 += dy;
2610 if (INT_MAX - dx < xmax)
2611 emacs_abort ();
2613 while (x1 <= xmax)
2615 XDrawLine (s->display, s->window, s->gc, x1, y1, x2, y2);
2616 x1 = x2, y1 = y2;
2617 x2 += dx, y2 = y0 + odd*dy;
2618 odd = !odd;
2621 /* Restore previous clipping rectangle(s) */
2622 XSetClipRectangles (s->display, s->gc, 0, 0, s->clip, s->num_clips, Unsorted);
2626 /* Draw glyph string S. */
2628 static void
2629 x_draw_glyph_string (struct glyph_string *s)
2631 bool relief_drawn_p = false;
2633 /* If S draws into the background of its successors, draw the
2634 background of the successors first so that S can draw into it.
2635 This makes S->next use XDrawString instead of XDrawImageString. */
2636 if (s->next && s->right_overhang && !s->for_overlaps)
2638 int width;
2639 struct glyph_string *next;
2641 for (width = 0, next = s->next;
2642 next && width < s->right_overhang;
2643 width += next->width, next = next->next)
2644 if (next->first_glyph->type != IMAGE_GLYPH)
2646 x_set_glyph_string_gc (next);
2647 x_set_glyph_string_clipping (next);
2648 if (next->first_glyph->type == STRETCH_GLYPH)
2649 x_draw_stretch_glyph_string (next);
2650 else
2651 x_draw_glyph_string_background (next, true);
2652 next->num_clips = 0;
2656 /* Set up S->gc, set clipping and draw S. */
2657 x_set_glyph_string_gc (s);
2659 /* Draw relief (if any) in advance for char/composition so that the
2660 glyph string can be drawn over it. */
2661 if (!s->for_overlaps
2662 && s->face->box != FACE_NO_BOX
2663 && (s->first_glyph->type == CHAR_GLYPH
2664 || s->first_glyph->type == COMPOSITE_GLYPH))
2667 x_set_glyph_string_clipping (s);
2668 x_draw_glyph_string_background (s, true);
2669 x_draw_glyph_string_box (s);
2670 x_set_glyph_string_clipping (s);
2671 relief_drawn_p = true;
2673 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2674 && !s->clip_tail
2675 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2676 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2677 /* We must clip just this glyph. left_overhang part has already
2678 drawn when s->prev was drawn, and right_overhang part will be
2679 drawn later when s->next is drawn. */
2680 x_set_glyph_string_clipping_exactly (s, s);
2681 else
2682 x_set_glyph_string_clipping (s);
2684 switch (s->first_glyph->type)
2686 case IMAGE_GLYPH:
2687 x_draw_image_glyph_string (s);
2688 break;
2690 #ifdef HAVE_XWIDGETS
2691 case XWIDGET_GLYPH:
2692 //erase xwidget background
2693 //x_draw_glyph_string_background (s, 0);
2694 x_draw_xwidget_glyph_string (s);
2695 break;
2696 #endif
2697 case STRETCH_GLYPH:
2698 x_draw_stretch_glyph_string (s);
2699 break;
2701 case CHAR_GLYPH:
2702 if (s->for_overlaps)
2703 s->background_filled_p = true;
2704 else
2705 x_draw_glyph_string_background (s, false);
2706 x_draw_glyph_string_foreground (s);
2707 break;
2709 case COMPOSITE_GLYPH:
2710 if (s->for_overlaps || (s->cmp_from > 0
2711 && ! s->first_glyph->u.cmp.automatic))
2712 s->background_filled_p = true;
2713 else
2714 x_draw_glyph_string_background (s, true);
2715 x_draw_composite_glyph_string_foreground (s);
2716 break;
2718 case GLYPHLESS_GLYPH:
2719 if (s->for_overlaps)
2720 s->background_filled_p = true;
2721 else
2722 x_draw_glyph_string_background (s, true);
2723 x_draw_glyphless_glyph_string_foreground (s);
2724 break;
2726 default:
2727 emacs_abort ();
2730 if (!s->for_overlaps)
2732 /* Draw underline. */
2733 if (s->face->underline_p)
2735 if (s->face->underline_type == FACE_UNDER_WAVE)
2737 if (s->face->underline_defaulted_p)
2738 x_draw_underwave (s);
2739 else
2741 XGCValues xgcv;
2742 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2743 XSetForeground (s->display, s->gc, s->face->underline_color);
2744 x_draw_underwave (s);
2745 XSetForeground (s->display, s->gc, xgcv.foreground);
2748 else if (s->face->underline_type == FACE_UNDER_LINE)
2750 unsigned long thickness, position;
2751 int y;
2753 if (s->prev && s->prev->face->underline_p
2754 && s->prev->face->underline_type == FACE_UNDER_LINE)
2756 /* We use the same underline style as the previous one. */
2757 thickness = s->prev->underline_thickness;
2758 position = s->prev->underline_position;
2760 else
2762 /* Get the underline thickness. Default is 1 pixel. */
2763 if (s->font && s->font->underline_thickness > 0)
2764 thickness = s->font->underline_thickness;
2765 else
2766 thickness = 1;
2767 if (x_underline_at_descent_line)
2768 position = (s->height - thickness) - (s->ybase - s->y);
2769 else
2771 /* Get the underline position. This is the recommended
2772 vertical offset in pixels from the baseline to the top of
2773 the underline. This is a signed value according to the
2774 specs, and its default is
2776 ROUND ((maximum descent) / 2), with
2777 ROUND(x) = floor (x + 0.5) */
2779 if (x_use_underline_position_properties
2780 && s->font && s->font->underline_position >= 0)
2781 position = s->font->underline_position;
2782 else if (s->font)
2783 position = (s->font->descent + 1) / 2;
2784 else
2785 position = underline_minimum_offset;
2787 position = max (position, underline_minimum_offset);
2789 /* Check the sanity of thickness and position. We should
2790 avoid drawing underline out of the current line area. */
2791 if (s->y + s->height <= s->ybase + position)
2792 position = (s->height - 1) - (s->ybase - s->y);
2793 if (s->y + s->height < s->ybase + position + thickness)
2794 thickness = (s->y + s->height) - (s->ybase + position);
2795 s->underline_thickness = thickness;
2796 s->underline_position = position;
2797 y = s->ybase + position;
2798 if (s->face->underline_defaulted_p)
2799 XFillRectangle (s->display, s->window, s->gc,
2800 s->x, y, s->width, thickness);
2801 else
2803 XGCValues xgcv;
2804 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2805 XSetForeground (s->display, s->gc, s->face->underline_color);
2806 XFillRectangle (s->display, s->window, s->gc,
2807 s->x, y, s->width, thickness);
2808 XSetForeground (s->display, s->gc, xgcv.foreground);
2812 /* Draw overline. */
2813 if (s->face->overline_p)
2815 unsigned long dy = 0, h = 1;
2817 if (s->face->overline_color_defaulted_p)
2818 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2819 s->width, h);
2820 else
2822 XGCValues xgcv;
2823 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2824 XSetForeground (s->display, s->gc, s->face->overline_color);
2825 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2826 s->width, h);
2827 XSetForeground (s->display, s->gc, xgcv.foreground);
2831 /* Draw strike-through. */
2832 if (s->face->strike_through_p)
2834 unsigned long h = 1;
2835 unsigned long dy = (s->height - h) / 2;
2837 if (s->face->strike_through_color_defaulted_p)
2838 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2839 s->width, h);
2840 else
2842 XGCValues xgcv;
2843 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2844 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2845 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2846 s->width, h);
2847 XSetForeground (s->display, s->gc, xgcv.foreground);
2851 /* Draw relief if not yet drawn. */
2852 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2853 x_draw_glyph_string_box (s);
2855 if (s->prev)
2857 struct glyph_string *prev;
2859 for (prev = s->prev; prev; prev = prev->prev)
2860 if (prev->hl != s->hl
2861 && prev->x + prev->width + prev->right_overhang > s->x)
2863 /* As prev was drawn while clipped to its own area, we
2864 must draw the right_overhang part using s->hl now. */
2865 enum draw_glyphs_face save = prev->hl;
2867 prev->hl = s->hl;
2868 x_set_glyph_string_gc (prev);
2869 x_set_glyph_string_clipping_exactly (s, prev);
2870 if (prev->first_glyph->type == CHAR_GLYPH)
2871 x_draw_glyph_string_foreground (prev);
2872 else
2873 x_draw_composite_glyph_string_foreground (prev);
2874 XSetClipMask (prev->display, prev->gc, None);
2875 prev->hl = save;
2876 prev->num_clips = 0;
2880 if (s->next)
2882 struct glyph_string *next;
2884 for (next = s->next; next; next = next->next)
2885 if (next->hl != s->hl
2886 && next->x - next->left_overhang < s->x + s->width)
2888 /* As next will be drawn while clipped to its own area,
2889 we must draw the left_overhang part using s->hl now. */
2890 enum draw_glyphs_face save = next->hl;
2892 next->hl = s->hl;
2893 x_set_glyph_string_gc (next);
2894 x_set_glyph_string_clipping_exactly (s, next);
2895 if (next->first_glyph->type == CHAR_GLYPH)
2896 x_draw_glyph_string_foreground (next);
2897 else
2898 x_draw_composite_glyph_string_foreground (next);
2899 XSetClipMask (next->display, next->gc, None);
2900 next->hl = save;
2901 next->num_clips = 0;
2902 next->clip_head = s->next;
2907 /* Reset clipping. */
2908 XSetClipMask (s->display, s->gc, None);
2909 s->num_clips = 0;
2912 /* Shift display to make room for inserted glyphs. */
2914 static void
2915 x_shift_glyphs_for_insert (struct frame *f, int x, int y, int width, int height, int shift_by)
2917 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2918 f->output_data.x->normal_gc,
2919 x, y, width, height,
2920 x + shift_by, y);
2923 /* Delete N glyphs at the nominal cursor position. Not implemented
2924 for X frames. */
2926 static void
2927 x_delete_glyphs (struct frame *f, register int n)
2929 emacs_abort ();
2933 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2934 If they are <= 0, this is probably an error. */
2936 void
2937 x_clear_area (Display *dpy, Window window, int x, int y, int width, int height)
2939 eassert (width > 0 && height > 0);
2940 XClearArea (dpy, window, x, y, width, height, False);
2944 /* Clear an entire frame. */
2946 static void
2947 x_clear_frame (struct frame *f)
2949 /* Clearing the frame will erase any cursor, so mark them all as no
2950 longer visible. */
2951 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2953 block_input ();
2955 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2957 /* We have to clear the scroll bars. If we have changed colors or
2958 something like that, then they should be notified. */
2959 x_scroll_bar_clear (f);
2961 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
2962 /* Make sure scroll bars are redrawn. As they aren't redrawn by
2963 redisplay, do it here. */
2964 if (FRAME_GTK_WIDGET (f))
2965 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
2966 #endif
2968 XFlush (FRAME_X_DISPLAY (f));
2970 unblock_input ();
2973 /* RIF: Show hourglass cursor on frame F. */
2975 static void
2976 x_show_hourglass (struct frame *f)
2978 Display *dpy = FRAME_X_DISPLAY (f);
2980 if (dpy)
2982 struct x_output *x = FRAME_X_OUTPUT (f);
2983 #ifdef USE_X_TOOLKIT
2984 if (x->widget)
2985 #else
2986 if (FRAME_OUTER_WINDOW (f))
2987 #endif
2989 x->hourglass_p = true;
2991 if (!x->hourglass_window)
2993 unsigned long mask = CWCursor;
2994 XSetWindowAttributes attrs;
2995 #ifdef USE_GTK
2996 Window parent = FRAME_X_WINDOW (f);
2997 #else
2998 Window parent = FRAME_OUTER_WINDOW (f);
2999 #endif
3000 attrs.cursor = x->hourglass_cursor;
3002 x->hourglass_window = XCreateWindow
3003 (dpy, parent, 0, 0, 32000, 32000, 0, 0,
3004 InputOnly, CopyFromParent, mask, &attrs);
3007 XMapRaised (dpy, x->hourglass_window);
3008 XFlush (dpy);
3013 /* RIF: Cancel hourglass cursor on frame F. */
3015 static void
3016 x_hide_hourglass (struct frame *f)
3018 struct x_output *x = FRAME_X_OUTPUT (f);
3020 /* Watch out for newly created frames. */
3021 if (x->hourglass_window)
3023 XUnmapWindow (FRAME_X_DISPLAY (f), x->hourglass_window);
3024 /* Sync here because XTread_socket looks at the
3025 hourglass_p flag that is reset to zero below. */
3026 XSync (FRAME_X_DISPLAY (f), False);
3027 x->hourglass_p = false;
3031 /* Invert the middle quarter of the frame for .15 sec. */
3033 static void
3034 XTflash (struct frame *f)
3036 block_input ();
3039 #ifdef USE_GTK
3040 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
3041 when the scroll bars and the edit widget share the same X window. */
3042 GdkWindow *window = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
3043 #ifdef HAVE_GTK3
3044 cairo_t *cr = gdk_cairo_create (window);
3045 cairo_set_source_rgb (cr, 1, 1, 1);
3046 cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
3047 #define XFillRectangle(d, win, gc, x, y, w, h) \
3048 do { \
3049 cairo_rectangle (cr, x, y, w, h); \
3050 cairo_fill (cr); \
3052 while (false)
3053 #else /* ! HAVE_GTK3 */
3054 GdkGCValues vals;
3055 GdkGC *gc;
3056 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
3057 ^ FRAME_BACKGROUND_PIXEL (f));
3058 vals.function = GDK_XOR;
3059 gc = gdk_gc_new_with_values (window,
3060 &vals, GDK_GC_FUNCTION | GDK_GC_FOREGROUND);
3061 #define XFillRectangle(d, win, gc, x, y, w, h) \
3062 gdk_draw_rectangle (window, gc, TRUE, x, y, w, h)
3063 #endif /* ! HAVE_GTK3 */
3064 #else /* ! USE_GTK */
3065 GC gc;
3067 /* Create a GC that will use the GXxor function to flip foreground
3068 pixels into background pixels. */
3070 XGCValues values;
3072 values.function = GXxor;
3073 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3074 ^ FRAME_BACKGROUND_PIXEL (f));
3076 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3077 GCFunction | GCForeground, &values);
3079 #endif
3081 /* Get the height not including a menu bar widget. */
3082 int height = FRAME_PIXEL_HEIGHT (f);
3083 /* Height of each line to flash. */
3084 int flash_height = FRAME_LINE_HEIGHT (f);
3085 /* These will be the left and right margins of the rectangles. */
3086 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3087 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3088 int width = flash_right - flash_left;
3090 /* If window is tall, flash top and bottom line. */
3091 if (height > 3 * FRAME_LINE_HEIGHT (f))
3093 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3094 flash_left,
3095 (FRAME_INTERNAL_BORDER_WIDTH (f)
3096 + FRAME_TOP_MARGIN_HEIGHT (f)),
3097 width, flash_height);
3098 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3099 flash_left,
3100 (height - flash_height
3101 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3102 width, flash_height);
3105 else
3106 /* If it is short, flash it all. */
3107 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3108 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3109 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3111 x_flush (f);
3114 struct timespec delay = make_timespec (0, 150 * 1000 * 1000);
3115 struct timespec wakeup = timespec_add (current_timespec (), delay);
3117 /* Keep waiting until past the time wakeup or any input gets
3118 available. */
3119 while (! detect_input_pending ())
3121 struct timespec current = current_timespec ();
3122 struct timespec timeout;
3124 /* Break if result would not be positive. */
3125 if (timespec_cmp (wakeup, current) <= 0)
3126 break;
3128 /* How long `select' should wait. */
3129 timeout = make_timespec (0, 10 * 1000 * 1000);
3131 /* Try to wait that long--but we might wake up sooner. */
3132 pselect (0, NULL, NULL, NULL, &timeout, NULL);
3136 /* If window is tall, flash top and bottom line. */
3137 if (height > 3 * FRAME_LINE_HEIGHT (f))
3139 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3140 flash_left,
3141 (FRAME_INTERNAL_BORDER_WIDTH (f)
3142 + FRAME_TOP_MARGIN_HEIGHT (f)),
3143 width, flash_height);
3144 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3145 flash_left,
3146 (height - flash_height
3147 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3148 width, flash_height);
3150 else
3151 /* If it is short, flash it all. */
3152 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3153 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3154 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3156 #ifdef USE_GTK
3157 #ifdef HAVE_GTK3
3158 cairo_destroy (cr);
3159 #else
3160 g_object_unref (G_OBJECT (gc));
3161 #endif
3162 #undef XFillRectangle
3163 #else
3164 XFreeGC (FRAME_X_DISPLAY (f), gc);
3165 #endif
3166 x_flush (f);
3170 unblock_input ();
3174 static void
3175 XTtoggle_invisible_pointer (struct frame *f, bool invisible)
3177 block_input ();
3178 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, invisible);
3179 unblock_input ();
3183 /* Make audible bell. */
3185 static void
3186 XTring_bell (struct frame *f)
3188 if (FRAME_X_DISPLAY (f))
3190 if (visible_bell)
3191 XTflash (f);
3192 else
3194 block_input ();
3195 #ifdef HAVE_XKB
3196 XkbBell (FRAME_X_DISPLAY (f), None, 0, None);
3197 #else
3198 XBell (FRAME_X_DISPLAY (f), 0);
3199 #endif
3200 XFlush (FRAME_X_DISPLAY (f));
3201 unblock_input ();
3206 /***********************************************************************
3207 Line Dance
3208 ***********************************************************************/
3210 /* Perform an insert-lines or delete-lines operation, inserting N
3211 lines or deleting -N lines at vertical position VPOS. */
3213 static void
3214 x_ins_del_lines (struct frame *f, int vpos, int n)
3216 emacs_abort ();
3220 /* Scroll part of the display as described by RUN. */
3222 static void
3223 x_scroll_run (struct window *w, struct run *run)
3225 struct frame *f = XFRAME (w->frame);
3226 int x, y, width, height, from_y, to_y, bottom_y;
3228 /* Get frame-relative bounding box of the text display area of W,
3229 without mode lines. Include in this box the left and right
3230 fringe of W. */
3231 window_box (w, ANY_AREA, &x, &y, &width, &height);
3233 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3234 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3235 bottom_y = y + height;
3237 if (to_y < from_y)
3239 /* Scrolling up. Make sure we don't copy part of the mode
3240 line at the bottom. */
3241 if (from_y + run->height > bottom_y)
3242 height = bottom_y - from_y;
3243 else
3244 height = run->height;
3246 else
3248 /* Scrolling down. Make sure we don't copy over the mode line.
3249 at the bottom. */
3250 if (to_y + run->height > bottom_y)
3251 height = bottom_y - to_y;
3252 else
3253 height = run->height;
3256 block_input ();
3258 /* Cursor off. Will be switched on again in x_update_window_end. */
3259 x_clear_cursor (w);
3261 XCopyArea (FRAME_X_DISPLAY (f),
3262 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3263 f->output_data.x->normal_gc,
3264 x, from_y,
3265 width, height,
3266 x, to_y);
3268 unblock_input ();
3273 /***********************************************************************
3274 Exposure Events
3275 ***********************************************************************/
3278 static void
3279 frame_highlight (struct frame *f)
3281 /* We used to only do this if Vx_no_window_manager was non-nil, but
3282 the ICCCM (section 4.1.6) says that the window's border pixmap
3283 and border pixel are window attributes which are "private to the
3284 client", so we can always change it to whatever we want. */
3285 block_input ();
3286 /* I recently started to get errors in this XSetWindowBorder, depending on
3287 the window-manager in use, tho something more is at play since I've been
3288 using that same window-manager binary for ever. Let's not crash just
3289 because of this (bug#9310). */
3290 x_catch_errors (FRAME_X_DISPLAY (f));
3291 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3292 f->output_data.x->border_pixel);
3293 x_uncatch_errors ();
3294 unblock_input ();
3295 x_update_cursor (f, true);
3296 x_set_frame_alpha (f);
3299 static void
3300 frame_unhighlight (struct frame *f)
3302 /* We used to only do this if Vx_no_window_manager was non-nil, but
3303 the ICCCM (section 4.1.6) says that the window's border pixmap
3304 and border pixel are window attributes which are "private to the
3305 client", so we can always change it to whatever we want. */
3306 block_input ();
3307 /* Same as above for XSetWindowBorder (bug#9310). */
3308 x_catch_errors (FRAME_X_DISPLAY (f));
3309 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3310 f->output_data.x->border_tile);
3311 x_uncatch_errors ();
3312 unblock_input ();
3313 x_update_cursor (f, true);
3314 x_set_frame_alpha (f);
3317 /* The focus has changed. Update the frames as necessary to reflect
3318 the new situation. Note that we can't change the selected frame
3319 here, because the Lisp code we are interrupting might become confused.
3320 Each event gets marked with the frame in which it occurred, so the
3321 Lisp code can tell when the switch took place by examining the events. */
3323 static void
3324 x_new_focus_frame (struct x_display_info *dpyinfo, struct frame *frame)
3326 struct frame *old_focus = dpyinfo->x_focus_frame;
3328 if (frame != dpyinfo->x_focus_frame)
3330 /* Set this before calling other routines, so that they see
3331 the correct value of x_focus_frame. */
3332 dpyinfo->x_focus_frame = frame;
3334 if (old_focus && old_focus->auto_lower)
3335 x_lower_frame (old_focus);
3337 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3338 dpyinfo->x_pending_autoraise_frame = dpyinfo->x_focus_frame;
3339 else
3340 dpyinfo->x_pending_autoraise_frame = NULL;
3343 x_frame_rehighlight (dpyinfo);
3346 /* Handle FocusIn and FocusOut state changes for FRAME.
3347 If FRAME has focus and there exists more than one frame, puts
3348 a FOCUS_IN_EVENT into *BUFP. */
3350 static void
3351 x_focus_changed (int type, int state, struct x_display_info *dpyinfo, struct frame *frame, struct input_event *bufp)
3353 if (type == FocusIn)
3355 if (dpyinfo->x_focus_event_frame != frame)
3357 x_new_focus_frame (dpyinfo, frame);
3358 dpyinfo->x_focus_event_frame = frame;
3360 /* Don't stop displaying the initial startup message
3361 for a switch-frame event we don't need. */
3362 /* When run as a daemon, Vterminal_frame is always NIL. */
3363 bufp->arg = (((NILP (Vterminal_frame)
3364 || ! FRAME_X_P (XFRAME (Vterminal_frame))
3365 || EQ (Fdaemonp (), Qt))
3366 && CONSP (Vframe_list)
3367 && !NILP (XCDR (Vframe_list)))
3368 ? Qt : Qnil);
3369 bufp->kind = FOCUS_IN_EVENT;
3370 XSETFRAME (bufp->frame_or_window, frame);
3373 frame->output_data.x->focus_state |= state;
3375 #ifdef HAVE_X_I18N
3376 if (FRAME_XIC (frame))
3377 XSetICFocus (FRAME_XIC (frame));
3378 #endif
3380 else if (type == FocusOut)
3382 frame->output_data.x->focus_state &= ~state;
3384 if (dpyinfo->x_focus_event_frame == frame)
3386 dpyinfo->x_focus_event_frame = 0;
3387 x_new_focus_frame (dpyinfo, 0);
3389 bufp->kind = FOCUS_OUT_EVENT;
3390 XSETFRAME (bufp->frame_or_window, frame);
3393 #ifdef HAVE_X_I18N
3394 if (FRAME_XIC (frame))
3395 XUnsetICFocus (FRAME_XIC (frame));
3396 #endif
3397 if (frame->pointer_invisible)
3398 XTtoggle_invisible_pointer (frame, false);
3402 /* Return the Emacs frame-object corresponding to an X window.
3403 It could be the frame's main window or an icon window. */
3405 static struct frame *
3406 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3408 Lisp_Object tail, frame;
3409 struct frame *f;
3411 if (wdesc == None)
3412 return NULL;
3414 FOR_EACH_FRAME (tail, frame)
3416 f = XFRAME (frame);
3417 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3418 continue;
3419 if (f->output_data.x->hourglass_window == wdesc)
3420 return f;
3421 #ifdef USE_X_TOOLKIT
3422 if ((f->output_data.x->edit_widget
3423 && XtWindow (f->output_data.x->edit_widget) == wdesc)
3424 /* A tooltip frame? */
3425 || (!f->output_data.x->edit_widget
3426 && FRAME_X_WINDOW (f) == wdesc)
3427 || f->output_data.x->icon_desc == wdesc)
3428 return f;
3429 #else /* not USE_X_TOOLKIT */
3430 #ifdef USE_GTK
3431 if (f->output_data.x->edit_widget)
3433 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3434 struct x_output *x = f->output_data.x;
3435 if (gwdesc != 0 && gwdesc == x->edit_widget)
3436 return f;
3438 #endif /* USE_GTK */
3439 if (FRAME_X_WINDOW (f) == wdesc
3440 || f->output_data.x->icon_desc == wdesc)
3441 return f;
3442 #endif /* not USE_X_TOOLKIT */
3444 return 0;
3447 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3449 /* Like x_window_to_frame but also compares the window with the widget's
3450 windows. */
3452 static struct frame *
3453 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3455 Lisp_Object tail, frame;
3456 struct frame *f, *found = NULL;
3457 struct x_output *x;
3459 if (wdesc == None)
3460 return NULL;
3462 FOR_EACH_FRAME (tail, frame)
3464 if (found)
3465 break;
3466 f = XFRAME (frame);
3467 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
3469 /* This frame matches if the window is any of its widgets. */
3470 x = f->output_data.x;
3471 if (x->hourglass_window == wdesc)
3472 found = f;
3473 else if (x->widget)
3475 #ifdef USE_GTK
3476 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3477 if (gwdesc != 0
3478 && gtk_widget_get_toplevel (gwdesc) == x->widget)
3479 found = f;
3480 #else
3481 if (wdesc == XtWindow (x->widget)
3482 || wdesc == XtWindow (x->column_widget)
3483 || wdesc == XtWindow (x->edit_widget))
3484 found = f;
3485 /* Match if the window is this frame's menubar. */
3486 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
3487 found = f;
3488 #endif
3490 else if (FRAME_X_WINDOW (f) == wdesc)
3491 /* A tooltip frame. */
3492 found = f;
3496 return found;
3499 /* Likewise, but consider only the menu bar widget. */
3501 static struct frame *
3502 x_menubar_window_to_frame (struct x_display_info *dpyinfo,
3503 const XEvent *event)
3505 Window wdesc = event->xany.window;
3506 Lisp_Object tail, frame;
3507 struct frame *f;
3508 struct x_output *x;
3510 if (wdesc == None)
3511 return NULL;
3513 FOR_EACH_FRAME (tail, frame)
3515 f = XFRAME (frame);
3516 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3517 continue;
3518 x = f->output_data.x;
3519 #ifdef USE_GTK
3520 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
3521 return f;
3522 #else
3523 /* Match if the window is this frame's menubar. */
3524 if (x->menubar_widget
3525 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
3526 return f;
3527 #endif
3529 return 0;
3532 /* Return the frame whose principal (outermost) window is WDESC.
3533 If WDESC is some other (smaller) window, we return 0. */
3535 struct frame *
3536 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3538 Lisp_Object tail, frame;
3539 struct frame *f;
3540 struct x_output *x;
3542 if (wdesc == None)
3543 return NULL;
3545 FOR_EACH_FRAME (tail, frame)
3547 f = XFRAME (frame);
3548 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3549 continue;
3550 x = f->output_data.x;
3552 if (x->widget)
3554 /* This frame matches if the window is its topmost widget. */
3555 #ifdef USE_GTK
3556 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3557 if (gwdesc == x->widget)
3558 return f;
3559 #else
3560 if (wdesc == XtWindow (x->widget))
3561 return f;
3562 #endif
3564 else if (FRAME_X_WINDOW (f) == wdesc)
3565 /* Tooltip frame. */
3566 return f;
3568 return 0;
3571 #else /* !USE_X_TOOLKIT && !USE_GTK */
3573 #define x_any_window_to_frame(d, i) x_window_to_frame (d, i)
3574 #define x_top_window_to_frame(d, i) x_window_to_frame (d, i)
3576 #endif /* USE_X_TOOLKIT || USE_GTK */
3578 /* The focus may have changed. Figure out if it is a real focus change,
3579 by checking both FocusIn/Out and Enter/LeaveNotify events.
3581 Returns FOCUS_IN_EVENT event in *BUFP. */
3583 static void
3584 x_detect_focus_change (struct x_display_info *dpyinfo, struct frame *frame,
3585 const XEvent *event, struct input_event *bufp)
3587 if (!frame)
3588 return;
3590 switch (event->type)
3592 case EnterNotify:
3593 case LeaveNotify:
3595 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3596 int focus_state
3597 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3599 if (event->xcrossing.detail != NotifyInferior
3600 && event->xcrossing.focus
3601 && ! (focus_state & FOCUS_EXPLICIT))
3602 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3603 FOCUS_IMPLICIT,
3604 dpyinfo, frame, bufp);
3606 break;
3608 case FocusIn:
3609 case FocusOut:
3610 x_focus_changed (event->type,
3611 (event->xfocus.detail == NotifyPointer ?
3612 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3613 dpyinfo, frame, bufp);
3614 break;
3616 case ClientMessage:
3617 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
3619 enum xembed_message msg = event->xclient.data.l[1];
3620 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
3621 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
3623 break;
3628 #if !defined USE_X_TOOLKIT && !defined USE_GTK
3629 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3631 void
3632 x_mouse_leave (struct x_display_info *dpyinfo)
3634 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3636 #endif
3638 /* The focus has changed, or we have redirected a frame's focus to
3639 another frame (this happens when a frame uses a surrogate
3640 mini-buffer frame). Shift the highlight as appropriate.
3642 The FRAME argument doesn't necessarily have anything to do with which
3643 frame is being highlighted or un-highlighted; we only use it to find
3644 the appropriate X display info. */
3646 static void
3647 XTframe_rehighlight (struct frame *frame)
3649 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
3652 static void
3653 x_frame_rehighlight (struct x_display_info *dpyinfo)
3655 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3657 if (dpyinfo->x_focus_frame)
3659 dpyinfo->x_highlight_frame
3660 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3661 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3662 : dpyinfo->x_focus_frame);
3663 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3665 fset_focus_frame (dpyinfo->x_focus_frame, Qnil);
3666 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3669 else
3670 dpyinfo->x_highlight_frame = 0;
3672 if (dpyinfo->x_highlight_frame != old_highlight)
3674 if (old_highlight)
3675 frame_unhighlight (old_highlight);
3676 if (dpyinfo->x_highlight_frame)
3677 frame_highlight (dpyinfo->x_highlight_frame);
3683 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3685 /* Initialize mode_switch_bit and modifier_meaning. */
3686 static void
3687 x_find_modifier_meanings (struct x_display_info *dpyinfo)
3689 int min_code, max_code;
3690 KeySym *syms;
3691 int syms_per_code;
3692 XModifierKeymap *mods;
3694 dpyinfo->meta_mod_mask = 0;
3695 dpyinfo->shift_lock_mask = 0;
3696 dpyinfo->alt_mod_mask = 0;
3697 dpyinfo->super_mod_mask = 0;
3698 dpyinfo->hyper_mod_mask = 0;
3700 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3702 syms = XGetKeyboardMapping (dpyinfo->display,
3703 min_code, max_code - min_code + 1,
3704 &syms_per_code);
3705 mods = XGetModifierMapping (dpyinfo->display);
3707 /* Scan the modifier table to see which modifier bits the Meta and
3708 Alt keysyms are on. */
3710 int row, col; /* The row and column in the modifier table. */
3711 bool found_alt_or_meta;
3713 for (row = 3; row < 8; row++)
3715 found_alt_or_meta = false;
3716 for (col = 0; col < mods->max_keypermod; col++)
3718 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3720 /* Zeroes are used for filler. Skip them. */
3721 if (code == 0)
3722 continue;
3724 /* Are any of this keycode's keysyms a meta key? */
3726 int code_col;
3728 for (code_col = 0; code_col < syms_per_code; code_col++)
3730 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3732 switch (sym)
3734 case XK_Meta_L:
3735 case XK_Meta_R:
3736 found_alt_or_meta = true;
3737 dpyinfo->meta_mod_mask |= (1 << row);
3738 break;
3740 case XK_Alt_L:
3741 case XK_Alt_R:
3742 found_alt_or_meta = true;
3743 dpyinfo->alt_mod_mask |= (1 << row);
3744 break;
3746 case XK_Hyper_L:
3747 case XK_Hyper_R:
3748 if (!found_alt_or_meta)
3749 dpyinfo->hyper_mod_mask |= (1 << row);
3750 code_col = syms_per_code;
3751 col = mods->max_keypermod;
3752 break;
3754 case XK_Super_L:
3755 case XK_Super_R:
3756 if (!found_alt_or_meta)
3757 dpyinfo->super_mod_mask |= (1 << row);
3758 code_col = syms_per_code;
3759 col = mods->max_keypermod;
3760 break;
3762 case XK_Shift_Lock:
3763 /* Ignore this if it's not on the lock modifier. */
3764 if (!found_alt_or_meta && ((1 << row) == LockMask))
3765 dpyinfo->shift_lock_mask = LockMask;
3766 code_col = syms_per_code;
3767 col = mods->max_keypermod;
3768 break;
3776 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3777 if (! dpyinfo->meta_mod_mask)
3779 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3780 dpyinfo->alt_mod_mask = 0;
3783 /* If some keys are both alt and meta,
3784 make them just meta, not alt. */
3785 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3787 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3790 XFree (syms);
3791 XFreeModifiermap (mods);
3794 /* Convert between the modifier bits X uses and the modifier bits
3795 Emacs uses. */
3798 x_x_to_emacs_modifiers (struct x_display_info *dpyinfo, int state)
3800 int mod_meta = meta_modifier;
3801 int mod_alt = alt_modifier;
3802 int mod_hyper = hyper_modifier;
3803 int mod_super = super_modifier;
3804 Lisp_Object tem;
3806 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3807 if (INTEGERP (tem)) mod_alt = XINT (tem) & INT_MAX;
3808 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3809 if (INTEGERP (tem)) mod_meta = XINT (tem) & INT_MAX;
3810 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3811 if (INTEGERP (tem)) mod_hyper = XINT (tem) & INT_MAX;
3812 tem = Fget (Vx_super_keysym, Qmodifier_value);
3813 if (INTEGERP (tem)) mod_super = XINT (tem) & INT_MAX;
3815 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3816 | ((state & ControlMask) ? ctrl_modifier : 0)
3817 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3818 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3819 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3820 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3823 static int
3824 x_emacs_to_x_modifiers (struct x_display_info *dpyinfo, EMACS_INT state)
3826 EMACS_INT mod_meta = meta_modifier;
3827 EMACS_INT mod_alt = alt_modifier;
3828 EMACS_INT mod_hyper = hyper_modifier;
3829 EMACS_INT mod_super = super_modifier;
3831 Lisp_Object tem;
3833 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3834 if (INTEGERP (tem)) mod_alt = XINT (tem);
3835 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3836 if (INTEGERP (tem)) mod_meta = XINT (tem);
3837 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3838 if (INTEGERP (tem)) mod_hyper = XINT (tem);
3839 tem = Fget (Vx_super_keysym, Qmodifier_value);
3840 if (INTEGERP (tem)) mod_super = XINT (tem);
3843 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3844 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3845 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3846 | ((state & shift_modifier) ? ShiftMask : 0)
3847 | ((state & ctrl_modifier) ? ControlMask : 0)
3848 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3851 /* Convert a keysym to its name. */
3853 char *
3854 x_get_keysym_name (int keysym)
3856 char *value;
3858 block_input ();
3859 value = XKeysymToString (keysym);
3860 unblock_input ();
3862 return value;
3865 /* Mouse clicks and mouse movement. Rah.
3867 Formerly, we used PointerMotionHintMask (in standard_event_mask)
3868 so that we would have to call XQueryPointer after each MotionNotify
3869 event to ask for another such event. However, this made mouse tracking
3870 slow, and there was a bug that made it eventually stop.
3872 Simply asking for MotionNotify all the time seems to work better.
3874 In order to avoid asking for motion events and then throwing most
3875 of them away or busy-polling the server for mouse positions, we ask
3876 the server for pointer motion hints. This means that we get only
3877 one event per group of mouse movements. "Groups" are delimited by
3878 other kinds of events (focus changes and button clicks, for
3879 example), or by XQueryPointer calls; when one of these happens, we
3880 get another MotionNotify event the next time the mouse moves. This
3881 is at least as efficient as getting motion events when mouse
3882 tracking is on, and I suspect only negligibly worse when tracking
3883 is off. */
3885 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3887 If the event is a button press, then note that we have grabbed
3888 the mouse. */
3890 static Lisp_Object
3891 construct_mouse_click (struct input_event *result,
3892 const XButtonEvent *event,
3893 struct frame *f)
3895 /* Make the event type NO_EVENT; we'll change that when we decide
3896 otherwise. */
3897 result->kind = MOUSE_CLICK_EVENT;
3898 result->code = event->button - Button1;
3899 result->timestamp = event->time;
3900 result->modifiers = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f),
3901 event->state)
3902 | (event->type == ButtonRelease
3903 ? up_modifier
3904 : down_modifier));
3906 XSETINT (result->x, event->x);
3907 XSETINT (result->y, event->y);
3908 XSETFRAME (result->frame_or_window, f);
3909 result->arg = Qnil;
3910 return Qnil;
3913 /* Function to report a mouse movement to the mainstream Emacs code.
3914 The input handler calls this.
3916 We have received a mouse movement event, which is given in *event.
3917 If the mouse is over a different glyph than it was last time, tell
3918 the mainstream emacs code by setting mouse_moved. If not, ask for
3919 another motion event, so we can check again the next time it moves. */
3921 static bool
3922 note_mouse_movement (struct frame *frame, const XMotionEvent *event)
3924 XRectangle *r;
3925 struct x_display_info *dpyinfo;
3927 if (!FRAME_X_OUTPUT (frame))
3928 return false;
3930 dpyinfo = FRAME_DISPLAY_INFO (frame);
3931 dpyinfo->last_mouse_movement_time = event->time;
3932 dpyinfo->last_mouse_motion_frame = frame;
3933 dpyinfo->last_mouse_motion_x = event->x;
3934 dpyinfo->last_mouse_motion_y = event->y;
3936 if (event->window != FRAME_X_WINDOW (frame))
3938 frame->mouse_moved = true;
3939 dpyinfo->last_mouse_scroll_bar = NULL;
3940 note_mouse_highlight (frame, -1, -1);
3941 dpyinfo->last_mouse_glyph_frame = NULL;
3942 return true;
3946 /* Has the mouse moved off the glyph it was on at the last sighting? */
3947 r = &dpyinfo->last_mouse_glyph;
3948 if (frame != dpyinfo->last_mouse_glyph_frame
3949 || event->x < r->x || event->x >= r->x + r->width
3950 || event->y < r->y || event->y >= r->y + r->height)
3952 frame->mouse_moved = true;
3953 dpyinfo->last_mouse_scroll_bar = NULL;
3954 note_mouse_highlight (frame, event->x, event->y);
3955 /* Remember which glyph we're now on. */
3956 remember_mouse_glyph (frame, event->x, event->y, r);
3957 dpyinfo->last_mouse_glyph_frame = frame;
3958 return true;
3961 return false;
3964 /* Return the current position of the mouse.
3965 *FP should be a frame which indicates which display to ask about.
3967 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3968 and *PART to the frame, window, and scroll bar part that the mouse
3969 is over. Set *X and *Y to the portion and whole of the mouse's
3970 position on the scroll bar.
3972 If the mouse movement started elsewhere, set *FP to the frame the
3973 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3974 the mouse is over.
3976 Set *TIMESTAMP to the server time-stamp for the time at which the mouse
3977 was at this position.
3979 Don't store anything if we don't have a valid set of values to report.
3981 This clears the mouse_moved flag, so we can wait for the next mouse
3982 movement. */
3984 static void
3985 XTmouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
3986 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
3987 Time *timestamp)
3989 struct frame *f1;
3990 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
3992 block_input ();
3994 if (dpyinfo->last_mouse_scroll_bar && insist == 0)
3996 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
3998 if (bar->horizontal)
3999 x_horizontal_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
4000 else
4001 x_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
4003 else
4005 Window root;
4006 int root_x, root_y;
4008 Window dummy_window;
4009 int dummy;
4011 Lisp_Object frame, tail;
4013 /* Clear the mouse-moved flag for every frame on this display. */
4014 FOR_EACH_FRAME (tail, frame)
4015 if (FRAME_X_P (XFRAME (frame))
4016 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
4017 XFRAME (frame)->mouse_moved = false;
4019 dpyinfo->last_mouse_scroll_bar = NULL;
4021 /* Figure out which root window we're on. */
4022 XQueryPointer (FRAME_X_DISPLAY (*fp),
4023 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
4025 /* The root window which contains the pointer. */
4026 &root,
4028 /* Trash which we can't trust if the pointer is on
4029 a different screen. */
4030 &dummy_window,
4032 /* The position on that root window. */
4033 &root_x, &root_y,
4035 /* More trash we can't trust. */
4036 &dummy, &dummy,
4038 /* Modifier keys and pointer buttons, about which
4039 we don't care. */
4040 (unsigned int *) &dummy);
4042 /* Now we have a position on the root; find the innermost window
4043 containing the pointer. */
4045 Window win, child;
4046 int win_x, win_y;
4047 int parent_x = 0, parent_y = 0;
4049 win = root;
4051 /* XTranslateCoordinates can get errors if the window
4052 structure is changing at the same time this function
4053 is running. So at least we must not crash from them. */
4055 x_catch_errors (FRAME_X_DISPLAY (*fp));
4057 if (x_mouse_grabbed (dpyinfo))
4059 /* If mouse was grabbed on a frame, give coords for that frame
4060 even if the mouse is now outside it. */
4061 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
4063 /* From-window. */
4064 root,
4066 /* To-window. */
4067 FRAME_X_WINDOW (dpyinfo->last_mouse_frame),
4069 /* From-position, to-position. */
4070 root_x, root_y, &win_x, &win_y,
4072 /* Child of win. */
4073 &child);
4074 f1 = dpyinfo->last_mouse_frame;
4076 else
4078 while (true)
4080 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
4082 /* From-window, to-window. */
4083 root, win,
4085 /* From-position, to-position. */
4086 root_x, root_y, &win_x, &win_y,
4088 /* Child of win. */
4089 &child);
4091 if (child == None || child == win)
4092 break;
4093 #ifdef USE_GTK
4094 /* We don't wan't to know the innermost window. We
4095 want the edit window. For non-Gtk+ the innermost
4096 window is the edit window. For Gtk+ it might not
4097 be. It might be the tool bar for example. */
4098 if (x_window_to_frame (dpyinfo, win))
4099 break;
4100 #endif
4101 win = child;
4102 parent_x = win_x;
4103 parent_y = win_y;
4106 /* Now we know that:
4107 win is the innermost window containing the pointer
4108 (XTC says it has no child containing the pointer),
4109 win_x and win_y are the pointer's position in it
4110 (XTC did this the last time through), and
4111 parent_x and parent_y are the pointer's position in win's parent.
4112 (They are what win_x and win_y were when win was child.
4113 If win is the root window, it has no parent, and
4114 parent_{x,y} are invalid, but that's okay, because we'll
4115 never use them in that case.) */
4117 #ifdef USE_GTK
4118 /* We don't wan't to know the innermost window. We
4119 want the edit window. */
4120 f1 = x_window_to_frame (dpyinfo, win);
4121 #else
4122 /* Is win one of our frames? */
4123 f1 = x_any_window_to_frame (dpyinfo, win);
4124 #endif
4126 #ifdef USE_X_TOOLKIT
4127 /* If we end up with the menu bar window, say it's not
4128 on the frame. */
4129 if (f1 != NULL
4130 && f1->output_data.x->menubar_widget
4131 && win == XtWindow (f1->output_data.x->menubar_widget))
4132 f1 = NULL;
4133 #endif /* USE_X_TOOLKIT */
4136 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
4137 f1 = 0;
4139 x_uncatch_errors ();
4141 /* If not, is it one of our scroll bars? */
4142 if (! f1)
4144 struct scroll_bar *bar;
4146 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win, 2);
4148 if (bar)
4150 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4151 win_x = parent_x;
4152 win_y = parent_y;
4156 if (f1 == 0 && insist > 0)
4157 f1 = SELECTED_FRAME ();
4159 if (f1)
4161 /* Ok, we found a frame. Store all the values.
4162 last_mouse_glyph is a rectangle used to reduce the
4163 generation of mouse events. To not miss any motion
4164 events, we must divide the frame into rectangles of the
4165 size of the smallest character that could be displayed
4166 on it, i.e. into the same rectangles that matrices on
4167 the frame are divided into. */
4169 /* FIXME: what if F1 is not an X frame? */
4170 dpyinfo = FRAME_DISPLAY_INFO (f1);
4171 remember_mouse_glyph (f1, win_x, win_y, &dpyinfo->last_mouse_glyph);
4172 dpyinfo->last_mouse_glyph_frame = f1;
4174 *bar_window = Qnil;
4175 *part = scroll_bar_above_handle;
4176 *fp = f1;
4177 XSETINT (*x, win_x);
4178 XSETINT (*y, win_y);
4179 *timestamp = dpyinfo->last_mouse_movement_time;
4184 unblock_input ();
4189 /***********************************************************************
4190 Scroll bars
4191 ***********************************************************************/
4193 /* Scroll bar support. */
4195 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
4196 manages it.
4197 This can be called in GC, so we have to make sure to strip off mark
4198 bits. */
4200 static struct scroll_bar *
4201 x_window_to_scroll_bar (Display *display, Window window_id, int type)
4203 Lisp_Object tail, frame;
4205 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4206 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4207 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4209 FOR_EACH_FRAME (tail, frame)
4211 Lisp_Object bar, condemned;
4213 if (! FRAME_X_P (XFRAME (frame)))
4214 continue;
4216 /* Scan this frame's scroll bar list for a scroll bar with the
4217 right window ID. */
4218 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4219 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4220 /* This trick allows us to search both the ordinary and
4221 condemned scroll bar lists with one loop. */
4222 ! NILP (bar) || (bar = condemned,
4223 condemned = Qnil,
4224 ! NILP (bar));
4225 bar = XSCROLL_BAR (bar)->next)
4226 if (XSCROLL_BAR (bar)->x_window == window_id
4227 && FRAME_X_DISPLAY (XFRAME (frame)) == display
4228 && (type = 2
4229 || (type == 1 && XSCROLL_BAR (bar)->horizontal)
4230 || (type == 0 && !XSCROLL_BAR (bar)->horizontal)))
4231 return XSCROLL_BAR (bar);
4234 return NULL;
4238 #if defined USE_LUCID
4240 /* Return the Lucid menu bar WINDOW is part of. Return null
4241 if WINDOW is not part of a menu bar. */
4243 static Widget
4244 x_window_to_menu_bar (Window window)
4246 Lisp_Object tail, frame;
4248 FOR_EACH_FRAME (tail, frame)
4249 if (FRAME_X_P (XFRAME (frame)))
4251 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4253 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4254 return menu_bar;
4256 return NULL;
4259 #endif /* USE_LUCID */
4262 /************************************************************************
4263 Toolkit scroll bars
4264 ************************************************************************/
4266 #ifdef USE_TOOLKIT_SCROLL_BARS
4268 static void x_send_scroll_bar_event (Lisp_Object, enum scroll_bar_part,
4269 int, int, bool);
4271 /* Lisp window being scrolled. Set when starting to interact with
4272 a toolkit scroll bar, reset to nil when ending the interaction. */
4274 static Lisp_Object window_being_scrolled;
4276 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4277 that movements of 1/20 of the screen size are mapped to up/down. */
4279 #ifndef USE_GTK
4280 /* Id of action hook installed for scroll bars. */
4282 static XtActionHookId action_hook_id;
4283 static XtActionHookId horizontal_action_hook_id;
4285 static Boolean xaw3d_arrow_scroll;
4287 /* Whether the drag scrolling maintains the mouse at the top of the
4288 thumb. If not, resizing the thumb needs to be done more carefully
4289 to avoid jerkiness. */
4291 static Boolean xaw3d_pick_top;
4293 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4294 bars are used.. The hook is responsible for detecting when
4295 the user ends an interaction with the scroll bar, and generates
4296 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4298 static void
4299 xt_action_hook (Widget widget, XtPointer client_data, String action_name,
4300 XEvent *event, String *params, Cardinal *num_params)
4302 bool scroll_bar_p;
4303 const char *end_action;
4305 #ifdef USE_MOTIF
4306 scroll_bar_p = XmIsScrollBar (widget);
4307 end_action = "Release";
4308 #else /* !USE_MOTIF i.e. use Xaw */
4309 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4310 end_action = "EndScroll";
4311 #endif /* USE_MOTIF */
4313 if (scroll_bar_p
4314 && strcmp (action_name, end_action) == 0
4315 && WINDOWP (window_being_scrolled))
4317 struct window *w;
4318 struct scroll_bar *bar;
4320 x_send_scroll_bar_event (window_being_scrolled,
4321 scroll_bar_end_scroll, 0, 0, false);
4322 w = XWINDOW (window_being_scrolled);
4323 bar = XSCROLL_BAR (w->vertical_scroll_bar);
4325 if (bar->dragging != -1)
4327 bar->dragging = -1;
4328 /* The thumb size is incorrect while dragging: fix it. */
4329 set_vertical_scroll_bar (w);
4331 window_being_scrolled = Qnil;
4332 #if defined (USE_LUCID)
4333 bar->last_seen_part = scroll_bar_nowhere;
4334 #endif
4335 /* Xt timeouts no longer needed. */
4336 toolkit_scroll_bar_interaction = false;
4341 static void
4342 xt_horizontal_action_hook (Widget widget, XtPointer client_data, String action_name,
4343 XEvent *event, String *params, Cardinal *num_params)
4345 bool scroll_bar_p;
4346 const char *end_action;
4348 #ifdef USE_MOTIF
4349 scroll_bar_p = XmIsScrollBar (widget);
4350 end_action = "Release";
4351 #else /* !USE_MOTIF i.e. use Xaw */
4352 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4353 end_action = "EndScroll";
4354 #endif /* USE_MOTIF */
4356 if (scroll_bar_p
4357 && strcmp (action_name, end_action) == 0
4358 && WINDOWP (window_being_scrolled))
4360 struct window *w;
4361 struct scroll_bar *bar;
4363 x_send_scroll_bar_event (window_being_scrolled,
4364 scroll_bar_end_scroll, 0, 0, true);
4365 w = XWINDOW (window_being_scrolled);
4366 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
4368 if (bar->dragging != -1)
4370 bar->dragging = -1;
4371 /* The thumb size is incorrect while dragging: fix it. */
4372 set_horizontal_scroll_bar (w);
4374 window_being_scrolled = Qnil;
4375 #if defined (USE_LUCID)
4376 bar->last_seen_part = scroll_bar_nowhere;
4377 #endif
4378 /* Xt timeouts no longer needed. */
4379 toolkit_scroll_bar_interaction = false;
4382 #endif /* not USE_GTK */
4384 /* Send a client message with message type Xatom_Scrollbar for a
4385 scroll action to the frame of WINDOW. PART is a value identifying
4386 the part of the scroll bar that was clicked on. PORTION is the
4387 amount to scroll of a whole of WHOLE. */
4389 static void
4390 x_send_scroll_bar_event (Lisp_Object window, enum scroll_bar_part part,
4391 int portion, int whole, bool horizontal)
4393 XEvent event;
4394 XClientMessageEvent *ev = &event.xclient;
4395 struct window *w = XWINDOW (window);
4396 struct frame *f = XFRAME (w->frame);
4397 intptr_t iw = (intptr_t) w;
4398 enum { BITS_PER_INTPTR = CHAR_BIT * sizeof iw };
4399 verify (BITS_PER_INTPTR <= 64);
4400 int sign_shift = BITS_PER_INTPTR - 32;
4402 block_input ();
4404 /* Construct a ClientMessage event to send to the frame. */
4405 ev->type = ClientMessage;
4406 ev->message_type = (horizontal
4407 ? FRAME_DISPLAY_INFO (f)->Xatom_Horizontal_Scrollbar
4408 : FRAME_DISPLAY_INFO (f)->Xatom_Scrollbar);
4409 ev->display = FRAME_X_DISPLAY (f);
4410 ev->window = FRAME_X_WINDOW (f);
4411 ev->format = 32;
4413 /* A 32-bit X client on a 64-bit X server can pass a window pointer
4414 as-is. A 64-bit client on a 32-bit X server is in trouble
4415 because a pointer does not fit and would be truncated while
4416 passing through the server. So use two slots and hope that X12
4417 will resolve such issues someday. */
4418 ev->data.l[0] = iw >> 31 >> 1;
4419 ev->data.l[1] = sign_shift <= 0 ? iw : iw << sign_shift >> sign_shift;
4420 ev->data.l[2] = part;
4421 ev->data.l[3] = portion;
4422 ev->data.l[4] = whole;
4424 /* Make Xt timeouts work while the scroll bar is active. */
4425 #ifdef USE_X_TOOLKIT
4426 toolkit_scroll_bar_interaction = true;
4427 x_activate_timeout_atimer ();
4428 #endif
4430 /* Setting the event mask to zero means that the message will
4431 be sent to the client that created the window, and if that
4432 window no longer exists, no event will be sent. */
4433 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4434 unblock_input ();
4438 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4439 in *IEVENT. */
4441 static void
4442 x_scroll_bar_to_input_event (const XEvent *event,
4443 struct input_event *ievent)
4445 const XClientMessageEvent *ev = &event->xclient;
4446 Lisp_Object window;
4447 struct window *w;
4449 /* See the comment in the function above. */
4450 intptr_t iw0 = ev->data.l[0];
4451 intptr_t iw1 = ev->data.l[1];
4452 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
4453 w = (struct window *) iw;
4455 XSETWINDOW (window, w);
4457 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4458 ievent->frame_or_window = window;
4459 ievent->arg = Qnil;
4460 #ifdef USE_GTK
4461 ievent->timestamp = CurrentTime;
4462 #else
4463 ievent->timestamp =
4464 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
4465 #endif
4466 ievent->code = 0;
4467 ievent->part = ev->data.l[2];
4468 ievent->x = make_number (ev->data.l[3]);
4469 ievent->y = make_number (ev->data.l[4]);
4470 ievent->modifiers = 0;
4473 /* Transform a horizontal scroll bar ClientMessage EVENT to an Emacs
4474 input event in *IEVENT. */
4476 static void
4477 x_horizontal_scroll_bar_to_input_event (const XEvent *event,
4478 struct input_event *ievent)
4480 const XClientMessageEvent *ev = &event->xclient;
4481 Lisp_Object window;
4482 struct window *w;
4484 /* See the comment in the function above. */
4485 intptr_t iw0 = ev->data.l[0];
4486 intptr_t iw1 = ev->data.l[1];
4487 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
4488 w = (struct window *) iw;
4490 XSETWINDOW (window, w);
4492 ievent->kind = HORIZONTAL_SCROLL_BAR_CLICK_EVENT;
4493 ievent->frame_or_window = window;
4494 ievent->arg = Qnil;
4495 #ifdef USE_GTK
4496 ievent->timestamp = CurrentTime;
4497 #else
4498 ievent->timestamp =
4499 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
4500 #endif
4501 ievent->code = 0;
4502 ievent->part = ev->data.l[2];
4503 ievent->x = make_number (ev->data.l[3]);
4504 ievent->y = make_number (ev->data.l[4]);
4505 ievent->modifiers = 0;
4509 #ifdef USE_MOTIF
4511 /* Minimum and maximum values used for Motif scroll bars. */
4513 #define XM_SB_MAX 10000000
4515 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4516 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4517 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4519 static void
4520 xm_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4522 struct scroll_bar *bar = client_data;
4523 XmScrollBarCallbackStruct *cs = call_data;
4524 enum scroll_bar_part part = scroll_bar_nowhere;
4525 bool horizontal = bar->horizontal;
4526 int whole = 0, portion = 0;
4528 switch (cs->reason)
4530 case XmCR_DECREMENT:
4531 bar->dragging = -1;
4532 part = horizontal ? scroll_bar_left_arrow : scroll_bar_up_arrow;
4533 break;
4535 case XmCR_INCREMENT:
4536 bar->dragging = -1;
4537 part = horizontal ? scroll_bar_right_arrow : scroll_bar_down_arrow;
4538 break;
4540 case XmCR_PAGE_DECREMENT:
4541 bar->dragging = -1;
4542 part = horizontal ? scroll_bar_before_handle : scroll_bar_above_handle;
4543 break;
4545 case XmCR_PAGE_INCREMENT:
4546 bar->dragging = -1;
4547 part = horizontal ? scroll_bar_after_handle : scroll_bar_below_handle;
4548 break;
4550 case XmCR_TO_TOP:
4551 bar->dragging = -1;
4552 part = horizontal ? scroll_bar_to_leftmost : scroll_bar_to_top;
4553 break;
4555 case XmCR_TO_BOTTOM:
4556 bar->dragging = -1;
4557 part = horizontal ? scroll_bar_to_rightmost : scroll_bar_to_bottom;
4558 break;
4560 case XmCR_DRAG:
4562 int slider_size;
4564 block_input ();
4565 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4566 unblock_input ();
4568 if (horizontal)
4570 portion = bar->whole * ((float)cs->value / XM_SB_MAX);
4571 whole = bar->whole * ((float)(XM_SB_MAX - slider_size) / XM_SB_MAX);
4572 portion = min (portion, whole);
4573 part = scroll_bar_horizontal_handle;
4575 else
4577 whole = XM_SB_MAX - slider_size;
4578 portion = min (cs->value, whole);
4579 part = scroll_bar_handle;
4582 bar->dragging = cs->value;
4584 break;
4586 case XmCR_VALUE_CHANGED:
4587 break;
4590 if (part != scroll_bar_nowhere)
4592 window_being_scrolled = bar->window;
4593 x_send_scroll_bar_event (bar->window, part, portion, whole,
4594 bar->horizontal);
4598 #elif defined USE_GTK
4600 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4601 bar widget. DATA is a pointer to the scroll_bar structure. */
4603 static gboolean
4604 xg_scroll_callback (GtkRange *range,
4605 GtkScrollType scroll,
4606 gdouble value,
4607 gpointer user_data)
4609 int whole = 0, portion = 0;
4610 struct scroll_bar *bar = user_data;
4611 enum scroll_bar_part part = scroll_bar_nowhere;
4612 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
4613 struct frame *f = g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
4615 if (xg_ignore_gtk_scrollbar) return FALSE;
4617 switch (scroll)
4619 case GTK_SCROLL_JUMP:
4620 /* Buttons 1 2 or 3 must be grabbed. */
4621 if (FRAME_DISPLAY_INFO (f)->grabbed != 0
4622 && FRAME_DISPLAY_INFO (f)->grabbed < (1 << 4))
4624 if (bar->horizontal)
4626 part = scroll_bar_horizontal_handle;
4627 whole = (int)(gtk_adjustment_get_upper (adj) -
4628 gtk_adjustment_get_page_size (adj));
4629 portion = min ((int)value, whole);
4630 bar->dragging = portion;
4632 else
4634 part = scroll_bar_handle;
4635 whole = gtk_adjustment_get_upper (adj) -
4636 gtk_adjustment_get_page_size (adj);
4637 portion = min ((int)value, whole);
4638 bar->dragging = portion;
4641 break;
4642 case GTK_SCROLL_STEP_BACKWARD:
4643 part = (bar->horizontal
4644 ? scroll_bar_left_arrow : scroll_bar_up_arrow);
4645 bar->dragging = -1;
4646 break;
4647 case GTK_SCROLL_STEP_FORWARD:
4648 part = (bar->horizontal
4649 ? scroll_bar_right_arrow : scroll_bar_down_arrow);
4650 bar->dragging = -1;
4651 break;
4652 case GTK_SCROLL_PAGE_BACKWARD:
4653 part = (bar->horizontal
4654 ? scroll_bar_before_handle : scroll_bar_above_handle);
4655 bar->dragging = -1;
4656 break;
4657 case GTK_SCROLL_PAGE_FORWARD:
4658 part = (bar->horizontal
4659 ? scroll_bar_after_handle : scroll_bar_below_handle);
4660 bar->dragging = -1;
4661 break;
4664 if (part != scroll_bar_nowhere)
4666 window_being_scrolled = bar->window;
4667 x_send_scroll_bar_event (bar->window, part, portion, whole,
4668 bar->horizontal);
4671 return FALSE;
4674 /* Callback for button release. Sets dragging to -1 when dragging is done. */
4676 static gboolean
4677 xg_end_scroll_callback (GtkWidget *widget,
4678 GdkEventButton *event,
4679 gpointer user_data)
4681 struct scroll_bar *bar = user_data;
4682 bar->dragging = -1;
4683 if (WINDOWP (window_being_scrolled))
4685 x_send_scroll_bar_event (window_being_scrolled,
4686 scroll_bar_end_scroll, 0, 0, bar->horizontal);
4687 window_being_scrolled = Qnil;
4690 return FALSE;
4694 #else /* not USE_GTK and not USE_MOTIF */
4696 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4697 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4698 scroll bar struct. CALL_DATA is a pointer to a float saying where
4699 the thumb is. */
4701 static void
4702 xaw_jump_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4704 struct scroll_bar *bar = client_data;
4705 float *top_addr = call_data;
4706 float top = *top_addr;
4707 float shown;
4708 int whole, portion, height, width;
4709 enum scroll_bar_part part;
4710 bool horizontal = bar->horizontal;
4713 if (horizontal)
4715 /* Get the size of the thumb, a value between 0 and 1. */
4716 block_input ();
4717 XtVaGetValues (widget, XtNshown, &shown, XtNwidth, &width, NULL);
4718 unblock_input ();
4720 if (shown < 1)
4722 whole = bar->whole - (shown * bar->whole);
4723 portion = min (top * bar->whole, whole);
4725 else
4727 whole = bar->whole;
4728 portion = 0;
4731 part = scroll_bar_horizontal_handle;
4733 else
4735 /* Get the size of the thumb, a value between 0 and 1. */
4736 block_input ();
4737 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4738 unblock_input ();
4740 whole = 10000000;
4741 portion = shown < 1 ? top * whole : 0;
4743 if (shown < 1 && (eabs (top + shown - 1) < 1.0f / height))
4744 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4745 the bottom, so we force the scrolling whenever we see that we're
4746 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4747 we try to ensure that we always stay two pixels away from the
4748 bottom). */
4749 part = scroll_bar_down_arrow;
4750 else
4751 part = scroll_bar_handle;
4754 window_being_scrolled = bar->window;
4755 bar->dragging = portion;
4756 bar->last_seen_part = part;
4757 x_send_scroll_bar_event (bar->window, part, portion, whole, bar->horizontal);
4761 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4762 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4763 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4764 the scroll bar. CALL_DATA is an integer specifying the action that
4765 has taken place. Its magnitude is in the range 0..height of the
4766 scroll bar. Negative values mean scroll towards buffer start.
4767 Values < height of scroll bar mean line-wise movement. */
4769 static void
4770 xaw_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4772 struct scroll_bar *bar = client_data;
4773 /* The position really is stored cast to a pointer. */
4774 int position = (intptr_t) call_data;
4775 Dimension height, width;
4776 enum scroll_bar_part part;
4778 if (bar->horizontal)
4780 /* Get the width of the scroll bar. */
4781 block_input ();
4782 XtVaGetValues (widget, XtNwidth, &width, NULL);
4783 unblock_input ();
4785 if (eabs (position) >= width)
4786 part = (position < 0) ? scroll_bar_before_handle : scroll_bar_after_handle;
4788 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4789 it maps line-movement to call_data = max(5, height/20). */
4790 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, width / 20))
4791 part = (position < 0) ? scroll_bar_left_arrow : scroll_bar_right_arrow;
4792 else
4793 part = scroll_bar_move_ratio;
4795 window_being_scrolled = bar->window;
4796 bar->dragging = -1;
4797 bar->last_seen_part = part;
4798 x_send_scroll_bar_event (bar->window, part, position, width,
4799 bar->horizontal);
4801 else
4804 /* Get the height of the scroll bar. */
4805 block_input ();
4806 XtVaGetValues (widget, XtNheight, &height, NULL);
4807 unblock_input ();
4809 if (eabs (position) >= height)
4810 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4812 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4813 it maps line-movement to call_data = max(5, height/20). */
4814 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
4815 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4816 else
4817 part = scroll_bar_move_ratio;
4819 window_being_scrolled = bar->window;
4820 bar->dragging = -1;
4821 bar->last_seen_part = part;
4822 x_send_scroll_bar_event (bar->window, part, position, height,
4823 bar->horizontal);
4827 #endif /* not USE_GTK and not USE_MOTIF */
4829 #define SCROLL_BAR_NAME "verticalScrollBar"
4830 #define SCROLL_BAR_HORIZONTAL_NAME "horizontalScrollBar"
4832 /* Create the widget for scroll bar BAR on frame F. Record the widget
4833 and X window of the scroll bar in BAR. */
4835 #ifdef USE_GTK
4836 static void
4837 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
4839 const char *scroll_bar_name = SCROLL_BAR_NAME;
4841 block_input ();
4842 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4843 G_CALLBACK (xg_end_scroll_callback),
4844 scroll_bar_name);
4845 unblock_input ();
4848 static void
4849 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
4851 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
4853 block_input ();
4854 xg_create_horizontal_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4855 G_CALLBACK (xg_end_scroll_callback),
4856 scroll_bar_name);
4857 unblock_input ();
4860 #else /* not USE_GTK */
4862 static void
4863 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
4865 Window xwindow;
4866 Widget widget;
4867 Arg av[20];
4868 int ac = 0;
4869 const char *scroll_bar_name = SCROLL_BAR_NAME;
4870 unsigned long pixel;
4872 block_input ();
4874 #ifdef USE_MOTIF
4875 /* Set resources. Create the widget. */
4876 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4877 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4878 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4879 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4880 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4881 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4882 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4884 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4885 if (pixel != -1)
4887 XtSetArg (av[ac], XmNforeground, pixel);
4888 ++ac;
4891 pixel = f->output_data.x->scroll_bar_background_pixel;
4892 if (pixel != -1)
4894 XtSetArg (av[ac], XmNbackground, pixel);
4895 ++ac;
4898 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4899 (char *) scroll_bar_name, av, ac);
4901 /* Add one callback for everything that can happen. */
4902 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4903 (XtPointer) bar);
4904 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4905 (XtPointer) bar);
4906 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4907 (XtPointer) bar);
4908 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4909 (XtPointer) bar);
4910 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4911 (XtPointer) bar);
4912 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4913 (XtPointer) bar);
4914 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4915 (XtPointer) bar);
4917 /* Realize the widget. Only after that is the X window created. */
4918 XtRealizeWidget (widget);
4920 /* Set the cursor to an arrow. I didn't find a resource to do that.
4921 And I'm wondering why it hasn't an arrow cursor by default. */
4922 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4923 f->output_data.x->nontext_cursor);
4925 #else /* !USE_MOTIF i.e. use Xaw */
4927 /* Set resources. Create the widget. The background of the
4928 Xaw3d scroll bar widget is a little bit light for my taste.
4929 We don't alter it here to let users change it according
4930 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4931 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4932 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4933 /* For smoother scrolling with Xaw3d -sm */
4934 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4936 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4937 if (pixel != -1)
4939 XtSetArg (av[ac], XtNforeground, pixel);
4940 ++ac;
4943 pixel = f->output_data.x->scroll_bar_background_pixel;
4944 if (pixel != -1)
4946 XtSetArg (av[ac], XtNbackground, pixel);
4947 ++ac;
4950 /* Top/bottom shadow colors. */
4952 /* Allocate them, if necessary. */
4953 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4955 pixel = f->output_data.x->scroll_bar_background_pixel;
4956 if (pixel != -1)
4958 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4959 FRAME_X_COLORMAP (f),
4960 &pixel, 1.2, 0x8000))
4961 pixel = -1;
4962 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4965 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4967 pixel = f->output_data.x->scroll_bar_background_pixel;
4968 if (pixel != -1)
4970 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4971 FRAME_X_COLORMAP (f),
4972 &pixel, 0.6, 0x4000))
4973 pixel = -1;
4974 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4978 #ifdef XtNbeNiceToColormap
4979 /* Tell the toolkit about them. */
4980 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4981 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4982 /* We tried to allocate a color for the top/bottom shadow, and
4983 failed, so tell Xaw3d to use dithering instead. */
4984 /* But only if we have a small colormap. Xaw3d can allocate nice
4985 colors itself. */
4987 XtSetArg (av[ac], XtNbeNiceToColormap,
4988 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
4989 ++ac;
4991 else
4992 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4993 be more consistent with other emacs 3d colors, and since Xaw3d is
4994 not good at dealing with allocation failure. */
4996 /* This tells Xaw3d to use real colors instead of dithering for
4997 the shadows. */
4998 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4999 ++ac;
5001 /* Specify the colors. */
5002 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
5003 if (pixel != -1)
5005 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
5006 ++ac;
5008 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
5009 if (pixel != -1)
5011 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
5012 ++ac;
5015 #endif
5017 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
5018 f->output_data.x->edit_widget, av, ac);
5021 char const *initial = "";
5022 char const *val = initial;
5023 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
5024 #ifdef XtNarrowScrollbars
5025 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
5026 #endif
5027 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
5028 if (xaw3d_arrow_scroll || val == initial)
5029 { /* ARROW_SCROLL */
5030 xaw3d_arrow_scroll = True;
5031 /* Isn't that just a personal preference ? --Stef */
5032 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
5036 /* Define callbacks. */
5037 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
5038 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
5039 (XtPointer) bar);
5041 /* Realize the widget. Only after that is the X window created. */
5042 XtRealizeWidget (widget);
5044 #endif /* !USE_MOTIF */
5046 /* Install an action hook that lets us detect when the user
5047 finishes interacting with a scroll bar. */
5048 if (action_hook_id == 0)
5049 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
5051 /* Remember X window and widget in the scroll bar vector. */
5052 SET_SCROLL_BAR_X_WIDGET (bar, widget);
5053 xwindow = XtWindow (widget);
5054 bar->x_window = xwindow;
5055 bar->whole = 1;
5056 bar->horizontal = false;
5058 unblock_input ();
5061 static void
5062 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5064 Window xwindow;
5065 Widget widget;
5066 Arg av[20];
5067 int ac = 0;
5068 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
5069 unsigned long pixel;
5071 block_input ();
5073 #ifdef USE_MOTIF
5074 /* Set resources. Create the widget. */
5075 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5076 XtSetArg (av[ac], XmNminimum, 0); ++ac;
5077 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
5078 XtSetArg (av[ac], XmNorientation, XmHORIZONTAL); ++ac;
5079 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_RIGHT), ++ac;
5080 XtSetArg (av[ac], XmNincrement, 1); ++ac;
5081 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
5083 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5084 if (pixel != -1)
5086 XtSetArg (av[ac], XmNforeground, pixel);
5087 ++ac;
5090 pixel = f->output_data.x->scroll_bar_background_pixel;
5091 if (pixel != -1)
5093 XtSetArg (av[ac], XmNbackground, pixel);
5094 ++ac;
5097 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
5098 (char *) scroll_bar_name, av, ac);
5100 /* Add one callback for everything that can happen. */
5101 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
5102 (XtPointer) bar);
5103 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
5104 (XtPointer) bar);
5105 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
5106 (XtPointer) bar);
5107 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
5108 (XtPointer) bar);
5109 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
5110 (XtPointer) bar);
5111 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
5112 (XtPointer) bar);
5113 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
5114 (XtPointer) bar);
5116 /* Realize the widget. Only after that is the X window created. */
5117 XtRealizeWidget (widget);
5119 /* Set the cursor to an arrow. I didn't find a resource to do that.
5120 And I'm wondering why it hasn't an arrow cursor by default. */
5121 XDefineCursor (XtDisplay (widget), XtWindow (widget),
5122 f->output_data.x->nontext_cursor);
5124 #else /* !USE_MOTIF i.e. use Xaw */
5126 /* Set resources. Create the widget. The background of the
5127 Xaw3d scroll bar widget is a little bit light for my taste.
5128 We don't alter it here to let users change it according
5129 to their taste with `emacs*verticalScrollBar.background: xxx'. */
5130 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5131 XtSetArg (av[ac], XtNorientation, XtorientHorizontal); ++ac;
5132 /* For smoother scrolling with Xaw3d -sm */
5133 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
5135 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5136 if (pixel != -1)
5138 XtSetArg (av[ac], XtNforeground, pixel);
5139 ++ac;
5142 pixel = f->output_data.x->scroll_bar_background_pixel;
5143 if (pixel != -1)
5145 XtSetArg (av[ac], XtNbackground, pixel);
5146 ++ac;
5149 /* Top/bottom shadow colors. */
5151 /* Allocate them, if necessary. */
5152 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
5154 pixel = f->output_data.x->scroll_bar_background_pixel;
5155 if (pixel != -1)
5157 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5158 FRAME_X_COLORMAP (f),
5159 &pixel, 1.2, 0x8000))
5160 pixel = -1;
5161 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
5164 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5166 pixel = f->output_data.x->scroll_bar_background_pixel;
5167 if (pixel != -1)
5169 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5170 FRAME_X_COLORMAP (f),
5171 &pixel, 0.6, 0x4000))
5172 pixel = -1;
5173 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
5177 #ifdef XtNbeNiceToColormap
5178 /* Tell the toolkit about them. */
5179 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
5180 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5181 /* We tried to allocate a color for the top/bottom shadow, and
5182 failed, so tell Xaw3d to use dithering instead. */
5183 /* But only if we have a small colormap. Xaw3d can allocate nice
5184 colors itself. */
5186 XtSetArg (av[ac], XtNbeNiceToColormap,
5187 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
5188 ++ac;
5190 else
5191 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
5192 be more consistent with other emacs 3d colors, and since Xaw3d is
5193 not good at dealing with allocation failure. */
5195 /* This tells Xaw3d to use real colors instead of dithering for
5196 the shadows. */
5197 XtSetArg (av[ac], XtNbeNiceToColormap, False);
5198 ++ac;
5200 /* Specify the colors. */
5201 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
5202 if (pixel != -1)
5204 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
5205 ++ac;
5207 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
5208 if (pixel != -1)
5210 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
5211 ++ac;
5214 #endif
5216 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
5217 f->output_data.x->edit_widget, av, ac);
5220 char const *initial = "";
5221 char const *val = initial;
5222 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
5223 #ifdef XtNarrowScrollbars
5224 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
5225 #endif
5226 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
5227 if (xaw3d_arrow_scroll || val == initial)
5228 { /* ARROW_SCROLL */
5229 xaw3d_arrow_scroll = True;
5230 /* Isn't that just a personal preference ? --Stef */
5231 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
5235 /* Define callbacks. */
5236 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
5237 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
5238 (XtPointer) bar);
5240 /* Realize the widget. Only after that is the X window created. */
5241 XtRealizeWidget (widget);
5243 #endif /* !USE_MOTIF */
5245 /* Install an action hook that lets us detect when the user
5246 finishes interacting with a scroll bar. */
5247 if (horizontal_action_hook_id == 0)
5248 horizontal_action_hook_id
5249 = XtAppAddActionHook (Xt_app_con, xt_horizontal_action_hook, 0);
5251 /* Remember X window and widget in the scroll bar vector. */
5252 SET_SCROLL_BAR_X_WIDGET (bar, widget);
5253 xwindow = XtWindow (widget);
5254 bar->x_window = xwindow;
5255 bar->whole = 1;
5256 bar->horizontal = true;
5258 unblock_input ();
5260 #endif /* not USE_GTK */
5263 /* Set the thumb size and position of scroll bar BAR. We are currently
5264 displaying PORTION out of a whole WHOLE, and our position POSITION. */
5266 #ifdef USE_GTK
5267 static void
5268 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
5270 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5273 static void
5274 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
5276 xg_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
5279 #else /* not USE_GTK */
5280 static void
5281 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
5282 int whole)
5284 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5285 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
5286 float top, shown;
5288 block_input ();
5290 #ifdef USE_MOTIF
5292 if (scroll_bar_adjust_thumb_portion_p)
5294 /* We use an estimate of 30 chars per line rather than the real
5295 `portion' value. This has the disadvantage that the thumb size
5296 is not very representative, but it makes our life a lot easier.
5297 Otherwise, we have to constantly adjust the thumb size, which
5298 we can't always do quickly enough: while dragging, the size of
5299 the thumb might prevent the user from dragging the thumb all the
5300 way to the end. but Motif and some versions of Xaw3d don't allow
5301 updating the thumb size while dragging. Also, even if we can update
5302 its size, the update will often happen too late.
5303 If you don't believe it, check out revision 1.650 of xterm.c to see
5304 what hoops we were going through and the still poor behavior we got. */
5305 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
5306 /* When the thumb is at the bottom, position == whole.
5307 So we need to increase `whole' to make space for the thumb. */
5308 whole += portion;
5311 if (whole <= 0)
5312 top = 0, shown = 1;
5313 else
5315 top = (float) position / whole;
5316 shown = (float) portion / whole;
5319 if (bar->dragging == -1)
5321 int size, value;
5323 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
5324 is the scroll bar's maximum and MIN is the scroll bar's minimum
5325 value. */
5326 size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
5328 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
5329 value = top * XM_SB_MAX;
5330 value = min (value, XM_SB_MAX - size);
5332 XmScrollBarSetValues (widget, value, size, 0, 0, False);
5334 #else /* !USE_MOTIF i.e. use Xaw */
5336 if (whole == 0)
5337 top = 0, shown = 1;
5338 else
5340 top = (float) position / whole;
5341 shown = (float) portion / whole;
5345 float old_top, old_shown;
5346 Dimension height;
5347 XtVaGetValues (widget,
5348 XtNtopOfThumb, &old_top,
5349 XtNshown, &old_shown,
5350 XtNheight, &height,
5351 NULL);
5353 /* Massage the top+shown values. */
5354 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
5355 top = max (0, min (1, top));
5356 else
5357 top = old_top;
5358 #if ! defined (HAVE_XAW3D)
5359 /* With Xaw, 'top' values too closer to 1.0 may
5360 cause the thumb to disappear. Fix that. */
5361 top = min (top, 0.99f);
5362 #endif
5363 /* Keep two pixels available for moving the thumb down. */
5364 shown = max (0, min (1 - top - (2.0f / height), shown));
5365 #if ! defined (HAVE_XAW3D)
5366 /* Likewise with too small 'shown'. */
5367 shown = max (shown, 0.01f);
5368 #endif
5370 /* If the call to XawScrollbarSetThumb below doesn't seem to
5371 work, check that 'NARROWPROTO' is defined in src/config.h.
5372 If this is not so, most likely you need to fix configure. */
5373 if (top != old_top || shown != old_shown)
5375 if (bar->dragging == -1)
5376 XawScrollbarSetThumb (widget, top, shown);
5377 else
5379 /* Try to make the scrolling a tad smoother. */
5380 if (!xaw3d_pick_top)
5381 shown = min (shown, old_shown);
5383 XawScrollbarSetThumb (widget, top, shown);
5387 #endif /* !USE_MOTIF */
5389 unblock_input ();
5392 static void
5393 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
5394 int whole)
5396 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5397 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
5398 float top, shown;
5400 block_input ();
5402 #ifdef USE_MOTIF
5403 bar->whole = whole;
5404 shown = (float) portion / whole;
5405 top = (float) position / (whole - portion);
5407 int size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
5408 int value = clip_to_bounds (0, top * (XM_SB_MAX - size), XM_SB_MAX - size);
5410 XmScrollBarSetValues (widget, value, size, 0, 0, False);
5412 #else /* !USE_MOTIF i.e. use Xaw */
5413 bar->whole = whole;
5414 if (whole == 0)
5415 top = 0, shown = 1;
5416 else
5418 top = (float) position / whole;
5419 shown = (float) portion / whole;
5423 float old_top, old_shown;
5424 Dimension height;
5425 XtVaGetValues (widget,
5426 XtNtopOfThumb, &old_top,
5427 XtNshown, &old_shown,
5428 XtNheight, &height,
5429 NULL);
5431 #if false
5432 /* Massage the top+shown values. */
5433 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
5434 top = max (0, min (1, top));
5435 else
5436 top = old_top;
5437 #if ! defined (HAVE_XAW3D)
5438 /* With Xaw, 'top' values too closer to 1.0 may
5439 cause the thumb to disappear. Fix that. */
5440 top = min (top, 0.99f);
5441 #endif
5442 /* Keep two pixels available for moving the thumb down. */
5443 shown = max (0, min (1 - top - (2.0f / height), shown));
5444 #if ! defined (HAVE_XAW3D)
5445 /* Likewise with too small 'shown'. */
5446 shown = max (shown, 0.01f);
5447 #endif
5448 #endif
5450 /* If the call to XawScrollbarSetThumb below doesn't seem to
5451 work, check that 'NARROWPROTO' is defined in src/config.h.
5452 If this is not so, most likely you need to fix configure. */
5453 XawScrollbarSetThumb (widget, top, shown);
5454 #if false
5455 if (top != old_top || shown != old_shown)
5457 if (bar->dragging == -1)
5458 XawScrollbarSetThumb (widget, top, shown);
5459 else
5461 /* Try to make the scrolling a tad smoother. */
5462 if (!xaw3d_pick_top)
5463 shown = min (shown, old_shown);
5465 XawScrollbarSetThumb (widget, top, shown);
5468 #endif
5470 #endif /* !USE_MOTIF */
5472 unblock_input ();
5474 #endif /* not USE_GTK */
5476 #endif /* USE_TOOLKIT_SCROLL_BARS */
5480 /************************************************************************
5481 Scroll bars, general
5482 ************************************************************************/
5484 /* Create a scroll bar and return the scroll bar vector for it. W is
5485 the Emacs window on which to create the scroll bar. TOP, LEFT,
5486 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
5487 scroll bar. */
5489 static struct scroll_bar *
5490 x_scroll_bar_create (struct window *w, int top, int left,
5491 int width, int height, bool horizontal)
5493 struct frame *f = XFRAME (w->frame);
5494 struct scroll_bar *bar
5495 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
5496 Lisp_Object barobj;
5498 block_input ();
5500 #ifdef USE_TOOLKIT_SCROLL_BARS
5501 if (horizontal)
5502 x_create_horizontal_toolkit_scroll_bar (f, bar);
5503 else
5504 x_create_toolkit_scroll_bar (f, bar);
5505 #else /* not USE_TOOLKIT_SCROLL_BARS */
5507 XSetWindowAttributes a;
5508 unsigned long mask;
5509 Window window;
5511 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
5512 if (a.background_pixel == -1)
5513 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
5515 a.event_mask = (ButtonPressMask | ButtonReleaseMask
5516 | ButtonMotionMask | PointerMotionHintMask
5517 | ExposureMask);
5518 a.cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
5520 mask = (CWBackPixel | CWEventMask | CWCursor);
5522 /* Clear the area of W that will serve as a scroll bar. This is
5523 for the case that a window has been split horizontally. In
5524 this case, no clear_frame is generated to reduce flickering. */
5525 if (width > 0 && window_box_height (w) > 0)
5526 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5527 left, top, width, window_box_height (w));
5529 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5530 /* Position and size of scroll bar. */
5531 left, top, width, height,
5532 /* Border width, depth, class, and visual. */
5534 CopyFromParent,
5535 CopyFromParent,
5536 CopyFromParent,
5537 /* Attributes. */
5538 mask, &a);
5539 bar->x_window = window;
5541 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5543 XSETWINDOW (bar->window, w);
5544 bar->top = top;
5545 bar->left = left;
5546 bar->width = width;
5547 bar->height = height;
5548 bar->start = 0;
5549 bar->end = 0;
5550 bar->dragging = -1;
5551 bar->horizontal = horizontal;
5552 #if defined (USE_TOOLKIT_SCROLL_BARS) && defined (USE_LUCID)
5553 bar->last_seen_part = scroll_bar_nowhere;
5554 #endif
5556 /* Add bar to its frame's list of scroll bars. */
5557 bar->next = FRAME_SCROLL_BARS (f);
5558 bar->prev = Qnil;
5559 XSETVECTOR (barobj, bar);
5560 fset_scroll_bars (f, barobj);
5561 if (!NILP (bar->next))
5562 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5564 /* Map the window/widget. */
5565 #ifdef USE_TOOLKIT_SCROLL_BARS
5567 #ifdef USE_GTK
5568 if (horizontal)
5569 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top,
5570 left, width, max (height, 1));
5571 else
5572 xg_update_scrollbar_pos (f, bar->x_window, top,
5573 left, width, max (height, 1));
5574 #else /* not USE_GTK */
5575 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
5576 XtConfigureWidget (scroll_bar, left, top, width, max (height, 1), 0);
5577 XtMapWidget (scroll_bar);
5578 #endif /* not USE_GTK */
5580 #else /* not USE_TOOLKIT_SCROLL_BARS */
5581 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
5582 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5584 unblock_input ();
5585 return bar;
5589 #ifndef USE_TOOLKIT_SCROLL_BARS
5591 /* Draw BAR's handle in the proper position.
5593 If the handle is already drawn from START to END, don't bother
5594 redrawing it, unless REBUILD; in that case, always
5595 redraw it. (REBUILD is handy for drawing the handle after expose
5596 events.)
5598 Normally, we want to constrain the start and end of the handle to
5599 fit inside its rectangle, but if the user is dragging the scroll
5600 bar handle, we want to let them drag it down all the way, so that
5601 the bar's top is as far down as it goes; otherwise, there's no way
5602 to move to the very end of the buffer. */
5604 static void
5605 x_scroll_bar_set_handle (struct scroll_bar *bar, int start, int end,
5606 bool rebuild)
5608 bool dragging = bar->dragging != -1;
5609 Window w = bar->x_window;
5610 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5611 GC gc = f->output_data.x->normal_gc;
5613 /* If the display is already accurate, do nothing. */
5614 if (! rebuild
5615 && start == bar->start
5616 && end == bar->end)
5617 return;
5619 block_input ();
5622 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
5623 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5624 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5626 /* Make sure the values are reasonable, and try to preserve
5627 the distance between start and end. */
5629 int length = end - start;
5631 if (start < 0)
5632 start = 0;
5633 else if (start > top_range)
5634 start = top_range;
5635 end = start + length;
5637 if (end < start)
5638 end = start;
5639 else if (end > top_range && ! dragging)
5640 end = top_range;
5643 /* Store the adjusted setting in the scroll bar. */
5644 bar->start = start;
5645 bar->end = end;
5647 /* Clip the end position, just for display. */
5648 if (end > top_range)
5649 end = top_range;
5651 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
5652 below top positions, to make sure the handle is always at least
5653 that many pixels tall. */
5654 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
5656 /* Draw the empty space above the handle. Note that we can't clear
5657 zero-height areas; that means "clear to end of window." */
5658 if ((inside_width > 0) && (start > 0))
5659 x_clear_area (FRAME_X_DISPLAY (f), w,
5660 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5661 VERTICAL_SCROLL_BAR_TOP_BORDER,
5662 inside_width, start);
5664 /* Change to proper foreground color if one is specified. */
5665 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5666 XSetForeground (FRAME_X_DISPLAY (f), gc,
5667 f->output_data.x->scroll_bar_foreground_pixel);
5669 /* Draw the handle itself. */
5670 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
5671 /* x, y, width, height */
5672 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5673 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
5674 inside_width, end - start);
5676 /* Restore the foreground color of the GC if we changed it above. */
5677 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5678 XSetForeground (FRAME_X_DISPLAY (f), gc,
5679 FRAME_FOREGROUND_PIXEL (f));
5681 /* Draw the empty space below the handle. Note that we can't
5682 clear zero-height areas; that means "clear to end of window." */
5683 if ((inside_width > 0) && (end < inside_height))
5684 x_clear_area (FRAME_X_DISPLAY (f), w,
5685 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5686 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
5687 inside_width, inside_height - end);
5690 unblock_input ();
5693 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5695 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
5696 nil. */
5698 static void
5699 x_scroll_bar_remove (struct scroll_bar *bar)
5701 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5702 block_input ();
5704 #ifdef USE_TOOLKIT_SCROLL_BARS
5705 #ifdef USE_GTK
5706 xg_remove_scroll_bar (f, bar->x_window);
5707 #else /* not USE_GTK */
5708 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5709 #endif /* not USE_GTK */
5710 #else
5711 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
5712 #endif
5714 /* Dissociate this scroll bar from its window. */
5715 if (bar->horizontal)
5716 wset_horizontal_scroll_bar (XWINDOW (bar->window), Qnil);
5717 else
5718 wset_vertical_scroll_bar (XWINDOW (bar->window), Qnil);
5720 unblock_input ();
5724 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5725 that we are displaying PORTION characters out of a total of WHOLE
5726 characters, starting at POSITION. If WINDOW has no scroll bar,
5727 create one. */
5729 static void
5730 XTset_vertical_scroll_bar (struct window *w, int portion, int whole, int position)
5732 struct frame *f = XFRAME (w->frame);
5733 Lisp_Object barobj;
5734 struct scroll_bar *bar;
5735 int top, height, left, width;
5736 int window_y, window_height;
5738 /* Get window dimensions. */
5739 window_box (w, ANY_AREA, 0, &window_y, 0, &window_height);
5740 top = window_y;
5741 height = window_height;
5742 left = WINDOW_SCROLL_BAR_AREA_X (w);
5743 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
5745 /* Does the scroll bar exist yet? */
5746 if (NILP (w->vertical_scroll_bar))
5748 if (width > 0 && height > 0)
5750 block_input ();
5751 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5752 left, top, width, height);
5753 unblock_input ();
5756 bar = x_scroll_bar_create (w, top, left, width, max (height, 1), false);
5758 else
5760 /* It may just need to be moved and resized. */
5761 unsigned int mask = 0;
5763 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5765 block_input ();
5767 if (left != bar->left)
5768 mask |= CWX;
5769 if (top != bar->top)
5770 mask |= CWY;
5771 if (width != bar->width)
5772 mask |= CWWidth;
5773 if (height != bar->height)
5774 mask |= CWHeight;
5776 #ifdef USE_TOOLKIT_SCROLL_BARS
5778 /* Move/size the scroll bar widget. */
5779 if (mask)
5781 /* Since toolkit scroll bars are smaller than the space reserved
5782 for them on the frame, we have to clear "under" them. */
5783 if (width > 0 && height > 0)
5784 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5785 left, top, width, height);
5786 #ifdef USE_GTK
5787 xg_update_scrollbar_pos (f, bar->x_window, top,
5788 left, width, max (height, 1));
5789 #else /* not USE_GTK */
5790 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5791 left, top, width, max (height, 1), 0);
5792 #endif /* not USE_GTK */
5794 #else /* not USE_TOOLKIT_SCROLL_BARS */
5796 /* Move/size the scroll bar window. */
5797 if (mask)
5799 XWindowChanges wc;
5801 wc.x = left;
5802 wc.y = top;
5803 wc.width = width;
5804 wc.height = height;
5805 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5806 mask, &wc);
5809 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5811 /* Remember new settings. */
5812 bar->left = left;
5813 bar->top = top;
5814 bar->width = width;
5815 bar->height = height;
5817 unblock_input ();
5820 #ifdef USE_TOOLKIT_SCROLL_BARS
5821 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5822 #else /* not USE_TOOLKIT_SCROLL_BARS */
5823 /* Set the scroll bar's current state, unless we're currently being
5824 dragged. */
5825 if (bar->dragging == -1)
5827 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5829 if (whole == 0)
5830 x_scroll_bar_set_handle (bar, 0, top_range, false);
5831 else
5833 int start = ((double) position * top_range) / whole;
5834 int end = ((double) (position + portion) * top_range) / whole;
5835 x_scroll_bar_set_handle (bar, start, end, false);
5838 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5840 XSETVECTOR (barobj, bar);
5841 wset_vertical_scroll_bar (w, barobj);
5845 static void
5846 XTset_horizontal_scroll_bar (struct window *w, int portion, int whole, int position)
5848 struct frame *f = XFRAME (w->frame);
5849 Lisp_Object barobj;
5850 struct scroll_bar *bar;
5851 int top, height, left, width;
5852 int window_x, window_width;
5853 int pixel_width = WINDOW_PIXEL_WIDTH (w);
5855 /* Get window dimensions. */
5856 window_box (w, ANY_AREA, &window_x, 0, &window_width, 0);
5857 left = window_x;
5858 width = window_width;
5859 top = WINDOW_SCROLL_BAR_AREA_Y (w);
5860 height = WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
5862 /* Does the scroll bar exist yet? */
5863 if (NILP (w->horizontal_scroll_bar))
5865 if (width > 0 && height > 0)
5867 block_input ();
5869 /* Clear also part between window_width and
5870 WINDOW_PIXEL_WIDTH. */
5871 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5872 left, top, pixel_width, height);
5873 unblock_input ();
5876 bar = x_scroll_bar_create (w, top, left, width, height, true);
5878 else
5880 /* It may just need to be moved and resized. */
5881 unsigned int mask = 0;
5883 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
5885 block_input ();
5887 if (left != bar->left)
5888 mask |= CWX;
5889 if (top != bar->top)
5890 mask |= CWY;
5891 if (width != bar->width)
5892 mask |= CWWidth;
5893 if (height != bar->height)
5894 mask |= CWHeight;
5896 #ifdef USE_TOOLKIT_SCROLL_BARS
5897 /* Move/size the scroll bar widget. */
5898 if (mask)
5900 /* Since toolkit scroll bars are smaller than the space reserved
5901 for them on the frame, we have to clear "under" them. */
5902 if (width > 0 && height > 0)
5903 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5904 WINDOW_LEFT_EDGE_X (w), top,
5905 pixel_width - WINDOW_RIGHT_DIVIDER_WIDTH (w), height);
5906 #ifdef USE_GTK
5907 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top, left,
5908 width, height);
5909 #else /* not USE_GTK */
5910 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5911 left, top, width, height, 0);
5912 #endif /* not USE_GTK */
5914 #else /* not USE_TOOLKIT_SCROLL_BARS */
5916 /* Clear areas not covered by the scroll bar because it's not as
5917 wide as the area reserved for it. This makes sure a
5918 previous mode line display is cleared after C-x 2 C-x 1, for
5919 example. */
5921 int area_height = WINDOW_CONFIG_SCROLL_BAR_HEIGHT (w);
5922 int rest = area_height - height;
5923 if (rest > 0 && width > 0)
5924 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5925 left, top, width, rest);
5928 /* Move/size the scroll bar window. */
5929 if (mask)
5931 XWindowChanges wc;
5933 wc.x = left;
5934 wc.y = top;
5935 wc.width = width;
5936 wc.height = height;
5937 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5938 mask, &wc);
5941 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5943 /* Remember new settings. */
5944 bar->left = left;
5945 bar->top = top;
5946 bar->width = width;
5947 bar->height = height;
5949 unblock_input ();
5952 #ifdef USE_TOOLKIT_SCROLL_BARS
5953 x_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
5954 #else /* not USE_TOOLKIT_SCROLL_BARS */
5955 /* Set the scroll bar's current state, unless we're currently being
5956 dragged. */
5957 if (bar->dragging == -1)
5959 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, width);
5961 if (whole == 0)
5962 x_scroll_bar_set_handle (bar, 0, left_range, false);
5963 else
5965 int start = ((double) position * left_range) / whole;
5966 int end = ((double) (position + portion) * left_range) / whole;
5967 x_scroll_bar_set_handle (bar, start, end, false);
5970 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5972 XSETVECTOR (barobj, bar);
5973 wset_horizontal_scroll_bar (w, barobj);
5977 /* The following three hooks are used when we're doing a thorough
5978 redisplay of the frame. We don't explicitly know which scroll bars
5979 are going to be deleted, because keeping track of when windows go
5980 away is a real pain - "Can you say set-window-configuration, boys
5981 and girls?" Instead, we just assert at the beginning of redisplay
5982 that *all* scroll bars are to be removed, and then save a scroll bar
5983 from the fiery pit when we actually redisplay its window. */
5985 /* Arrange for all scroll bars on FRAME to be removed at the next call
5986 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5987 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5989 static void
5990 XTcondemn_scroll_bars (struct frame *frame)
5992 if (!NILP (FRAME_SCROLL_BARS (frame)))
5994 if (!NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5996 /* Prepend scrollbars to already condemned ones. */
5997 Lisp_Object last = FRAME_SCROLL_BARS (frame);
5999 while (!NILP (XSCROLL_BAR (last)->next))
6000 last = XSCROLL_BAR (last)->next;
6002 XSCROLL_BAR (last)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
6003 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = last;
6006 fset_condemned_scroll_bars (frame, FRAME_SCROLL_BARS (frame));
6007 fset_scroll_bars (frame, Qnil);
6012 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
6013 Note that WINDOW isn't necessarily condemned at all. */
6015 static void
6016 XTredeem_scroll_bar (struct window *w)
6018 struct scroll_bar *bar;
6019 Lisp_Object barobj;
6020 struct frame *f;
6022 /* We can't redeem this window's scroll bar if it doesn't have one. */
6023 if (NILP (w->vertical_scroll_bar) && NILP (w->horizontal_scroll_bar))
6024 emacs_abort ();
6026 if (!NILP (w->vertical_scroll_bar) && WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
6028 bar = XSCROLL_BAR (w->vertical_scroll_bar);
6029 /* Unlink it from the condemned list. */
6030 f = XFRAME (WINDOW_FRAME (w));
6031 if (NILP (bar->prev))
6033 /* If the prev pointer is nil, it must be the first in one of
6034 the lists. */
6035 if (EQ (FRAME_SCROLL_BARS (f), w->vertical_scroll_bar))
6036 /* It's not condemned. Everything's fine. */
6037 goto horizontal;
6038 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
6039 w->vertical_scroll_bar))
6040 fset_condemned_scroll_bars (f, bar->next);
6041 else
6042 /* If its prev pointer is nil, it must be at the front of
6043 one or the other! */
6044 emacs_abort ();
6046 else
6047 XSCROLL_BAR (bar->prev)->next = bar->next;
6049 if (! NILP (bar->next))
6050 XSCROLL_BAR (bar->next)->prev = bar->prev;
6052 bar->next = FRAME_SCROLL_BARS (f);
6053 bar->prev = Qnil;
6054 XSETVECTOR (barobj, bar);
6055 fset_scroll_bars (f, barobj);
6056 if (! NILP (bar->next))
6057 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6060 horizontal:
6061 if (!NILP (w->horizontal_scroll_bar) && WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
6063 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
6064 /* Unlink it from the condemned list. */
6065 f = XFRAME (WINDOW_FRAME (w));
6066 if (NILP (bar->prev))
6068 /* If the prev pointer is nil, it must be the first in one of
6069 the lists. */
6070 if (EQ (FRAME_SCROLL_BARS (f), w->horizontal_scroll_bar))
6071 /* It's not condemned. Everything's fine. */
6072 return;
6073 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
6074 w->horizontal_scroll_bar))
6075 fset_condemned_scroll_bars (f, bar->next);
6076 else
6077 /* If its prev pointer is nil, it must be at the front of
6078 one or the other! */
6079 emacs_abort ();
6081 else
6082 XSCROLL_BAR (bar->prev)->next = bar->next;
6084 if (! NILP (bar->next))
6085 XSCROLL_BAR (bar->next)->prev = bar->prev;
6087 bar->next = FRAME_SCROLL_BARS (f);
6088 bar->prev = Qnil;
6089 XSETVECTOR (barobj, bar);
6090 fset_scroll_bars (f, barobj);
6091 if (! NILP (bar->next))
6092 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6096 /* Remove all scroll bars on FRAME that haven't been saved since the
6097 last call to `*condemn_scroll_bars_hook'. */
6099 static void
6100 XTjudge_scroll_bars (struct frame *f)
6102 Lisp_Object bar, next;
6104 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
6106 /* Clear out the condemned list now so we won't try to process any
6107 more events on the hapless scroll bars. */
6108 fset_condemned_scroll_bars (f, Qnil);
6110 for (; ! NILP (bar); bar = next)
6112 struct scroll_bar *b = XSCROLL_BAR (bar);
6114 x_scroll_bar_remove (b);
6116 next = b->next;
6117 b->next = b->prev = Qnil;
6120 /* Now there should be no references to the condemned scroll bars,
6121 and they should get garbage-collected. */
6125 #ifndef USE_TOOLKIT_SCROLL_BARS
6126 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
6127 is a no-op when using toolkit scroll bars.
6129 This may be called from a signal handler, so we have to ignore GC
6130 mark bits. */
6132 static void
6133 x_scroll_bar_expose (struct scroll_bar *bar, const XEvent *event)
6135 Window w = bar->x_window;
6136 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6137 GC gc = f->output_data.x->normal_gc;
6139 block_input ();
6141 x_scroll_bar_set_handle (bar, bar->start, bar->end, true);
6143 /* Switch to scroll bar foreground color. */
6144 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6145 XSetForeground (FRAME_X_DISPLAY (f), gc,
6146 f->output_data.x->scroll_bar_foreground_pixel);
6148 /* Draw a one-pixel border just inside the edges of the scroll bar. */
6149 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
6150 /* x, y, width, height */
6151 0, 0, bar->width - 1, bar->height - 1);
6153 /* Restore the foreground color of the GC if we changed it above. */
6154 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6155 XSetForeground (FRAME_X_DISPLAY (f), gc,
6156 FRAME_FOREGROUND_PIXEL (f));
6158 unblock_input ();
6161 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6163 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
6164 is set to something other than NO_EVENT, it is enqueued.
6166 This may be called from a signal handler, so we have to ignore GC
6167 mark bits. */
6170 static void
6171 x_scroll_bar_handle_click (struct scroll_bar *bar,
6172 const XEvent *event,
6173 struct input_event *emacs_event)
6175 if (! WINDOWP (bar->window))
6176 emacs_abort ();
6178 emacs_event->kind = (bar->horizontal
6179 ? HORIZONTAL_SCROLL_BAR_CLICK_EVENT
6180 : SCROLL_BAR_CLICK_EVENT);
6181 emacs_event->code = event->xbutton.button - Button1;
6182 emacs_event->modifiers
6183 = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO
6184 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
6185 event->xbutton.state)
6186 | (event->type == ButtonRelease
6187 ? up_modifier
6188 : down_modifier));
6189 emacs_event->frame_or_window = bar->window;
6190 emacs_event->arg = Qnil;
6191 emacs_event->timestamp = event->xbutton.time;
6192 if (bar->horizontal)
6194 int left_range
6195 = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
6196 int x = event->xbutton.x - HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
6198 if (x < 0) x = 0;
6199 if (x > left_range) x = left_range;
6201 if (x < bar->start)
6202 emacs_event->part = scroll_bar_before_handle;
6203 else if (x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
6204 emacs_event->part = scroll_bar_horizontal_handle;
6205 else
6206 emacs_event->part = scroll_bar_after_handle;
6208 #ifndef USE_TOOLKIT_SCROLL_BARS
6209 /* If the user has released the handle, set it to its final position. */
6210 if (event->type == ButtonRelease && bar->dragging != -1)
6212 int new_start = - bar->dragging;
6213 int new_end = new_start + bar->end - bar->start;
6215 x_scroll_bar_set_handle (bar, new_start, new_end, false);
6216 bar->dragging = -1;
6218 #endif
6220 XSETINT (emacs_event->x, left_range);
6221 XSETINT (emacs_event->y, x);
6223 else
6225 int top_range
6226 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
6227 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
6229 if (y < 0) y = 0;
6230 if (y > top_range) y = top_range;
6232 if (y < bar->start)
6233 emacs_event->part = scroll_bar_above_handle;
6234 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
6235 emacs_event->part = scroll_bar_handle;
6236 else
6237 emacs_event->part = scroll_bar_below_handle;
6239 #ifndef USE_TOOLKIT_SCROLL_BARS
6240 /* If the user has released the handle, set it to its final position. */
6241 if (event->type == ButtonRelease && bar->dragging != -1)
6243 int new_start = y - bar->dragging;
6244 int new_end = new_start + bar->end - bar->start;
6246 x_scroll_bar_set_handle (bar, new_start, new_end, false);
6247 bar->dragging = -1;
6249 #endif
6251 XSETINT (emacs_event->x, y);
6252 XSETINT (emacs_event->y, top_range);
6256 #ifndef USE_TOOLKIT_SCROLL_BARS
6258 /* Handle some mouse motion while someone is dragging the scroll bar.
6260 This may be called from a signal handler, so we have to ignore GC
6261 mark bits. */
6263 static void
6264 x_scroll_bar_note_movement (struct scroll_bar *bar,
6265 const XMotionEvent *event)
6267 struct frame *f = XFRAME (XWINDOW (bar->window)->frame);
6268 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
6270 dpyinfo->last_mouse_movement_time = event->time;
6271 dpyinfo->last_mouse_scroll_bar = bar;
6272 f->mouse_moved = true;
6274 /* If we're dragging the bar, display it. */
6275 if (bar->dragging != -1)
6277 /* Where should the handle be now? */
6278 int new_start = event->y - bar->dragging;
6280 if (new_start != bar->start)
6282 int new_end = new_start + bar->end - bar->start;
6284 x_scroll_bar_set_handle (bar, new_start, new_end, false);
6289 #endif /* !USE_TOOLKIT_SCROLL_BARS */
6291 /* Return information to the user about the current position of the mouse
6292 on the scroll bar. */
6294 static void
6295 x_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
6296 enum scroll_bar_part *part, Lisp_Object *x,
6297 Lisp_Object *y, Time *timestamp)
6299 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
6300 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
6301 Window w = bar->x_window;
6302 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6303 int win_x, win_y;
6304 Window dummy_window;
6305 int dummy_coord;
6306 unsigned int dummy_mask;
6308 block_input ();
6310 /* Get the mouse's position relative to the scroll bar window, and
6311 report that. */
6312 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
6314 /* Root, child, root x and root y. */
6315 &dummy_window, &dummy_window,
6316 &dummy_coord, &dummy_coord,
6318 /* Position relative to scroll bar. */
6319 &win_x, &win_y,
6321 /* Mouse buttons and modifier keys. */
6322 &dummy_mask))
6324 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
6326 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
6328 if (bar->dragging != -1)
6329 win_y -= bar->dragging;
6331 if (win_y < 0)
6332 win_y = 0;
6333 if (win_y > top_range)
6334 win_y = top_range;
6336 *fp = f;
6337 *bar_window = bar->window;
6339 if (bar->dragging != -1)
6340 *part = scroll_bar_handle;
6341 else if (win_y < bar->start)
6342 *part = scroll_bar_above_handle;
6343 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
6344 *part = scroll_bar_handle;
6345 else
6346 *part = scroll_bar_below_handle;
6348 XSETINT (*x, win_y);
6349 XSETINT (*y, top_range);
6351 f->mouse_moved = false;
6352 dpyinfo->last_mouse_scroll_bar = NULL;
6353 *timestamp = dpyinfo->last_mouse_movement_time;
6356 unblock_input ();
6360 /* Return information to the user about the current position of the mouse
6361 on the scroll bar. */
6363 static void
6364 x_horizontal_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
6365 enum scroll_bar_part *part, Lisp_Object *x,
6366 Lisp_Object *y, Time *timestamp)
6368 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
6369 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
6370 Window w = bar->x_window;
6371 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6372 int win_x, win_y;
6373 Window dummy_window;
6374 int dummy_coord;
6375 unsigned int dummy_mask;
6377 block_input ();
6379 /* Get the mouse's position relative to the scroll bar window, and
6380 report that. */
6381 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
6383 /* Root, child, root x and root y. */
6384 &dummy_window, &dummy_window,
6385 &dummy_coord, &dummy_coord,
6387 /* Position relative to scroll bar. */
6388 &win_x, &win_y,
6390 /* Mouse buttons and modifier keys. */
6391 &dummy_mask))
6393 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
6395 win_x -= HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
6397 if (bar->dragging != -1)
6398 win_x -= bar->dragging;
6400 if (win_x < 0)
6401 win_x = 0;
6402 if (win_x > left_range)
6403 win_x = left_range;
6405 *fp = f;
6406 *bar_window = bar->window;
6408 if (bar->dragging != -1)
6409 *part = scroll_bar_horizontal_handle;
6410 else if (win_x < bar->start)
6411 *part = scroll_bar_before_handle;
6412 else if (win_x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
6413 *part = scroll_bar_handle;
6414 else
6415 *part = scroll_bar_after_handle;
6417 XSETINT (*y, win_x);
6418 XSETINT (*x, left_range);
6420 f->mouse_moved = false;
6421 dpyinfo->last_mouse_scroll_bar = NULL;
6422 *timestamp = dpyinfo->last_mouse_movement_time;
6425 unblock_input ();
6429 /* The screen has been cleared so we may have changed foreground or
6430 background colors, and the scroll bars may need to be redrawn.
6431 Clear out the scroll bars, and ask for expose events, so we can
6432 redraw them. */
6434 static void
6435 x_scroll_bar_clear (struct frame *f)
6437 #ifndef USE_TOOLKIT_SCROLL_BARS
6438 Lisp_Object bar;
6440 /* We can have scroll bars even if this is 0,
6441 if we just turned off scroll bar mode.
6442 But in that case we should not clear them. */
6443 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
6444 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
6445 bar = XSCROLL_BAR (bar)->next)
6446 XClearArea (FRAME_X_DISPLAY (f),
6447 XSCROLL_BAR (bar)->x_window,
6448 0, 0, 0, 0, True);
6449 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6452 #ifdef ENABLE_CHECKING
6454 /* Record the last 100 characters stored
6455 to help debug the loss-of-chars-during-GC problem. */
6457 static int temp_index;
6458 static short temp_buffer[100];
6460 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
6461 if (temp_index == ARRAYELTS (temp_buffer)) \
6462 temp_index = 0; \
6463 temp_buffer[temp_index++] = (keysym)
6465 #else /* not ENABLE_CHECKING */
6467 #define STORE_KEYSYM_FOR_DEBUG(keysym) ((void)0)
6469 #endif /* ENABLE_CHECKING */
6471 /* Set this to nonzero to fake an "X I/O error"
6472 on a particular display. */
6474 static struct x_display_info *XTread_socket_fake_io_error;
6476 /* When we find no input here, we occasionally do a no-op command
6477 to verify that the X server is still running and we can still talk with it.
6478 We try all the open displays, one by one.
6479 This variable is used for cycling thru the displays. */
6481 static struct x_display_info *next_noop_dpyinfo;
6483 enum
6485 X_EVENT_NORMAL,
6486 X_EVENT_GOTO_OUT,
6487 X_EVENT_DROP
6490 /* Filter events for the current X input method.
6491 DPYINFO is the display this event is for.
6492 EVENT is the X event to filter.
6494 Returns non-zero if the event was filtered, caller shall not process
6495 this event further.
6496 Returns zero if event is wasn't filtered. */
6498 #ifdef HAVE_X_I18N
6499 static int
6500 x_filter_event (struct x_display_info *dpyinfo, XEvent *event)
6502 /* XFilterEvent returns non-zero if the input method has
6503 consumed the event. We pass the frame's X window to
6504 XFilterEvent because that's the one for which the IC
6505 was created. */
6507 struct frame *f1 = x_any_window_to_frame (dpyinfo,
6508 event->xclient.window);
6510 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
6512 #endif
6514 #ifdef USE_GTK
6515 static int current_count;
6516 static int current_finish;
6517 static struct input_event *current_hold_quit;
6519 /* This is the filter function invoked by the GTK event loop.
6520 It is invoked before the XEvent is translated to a GdkEvent,
6521 so we have a chance to act on the event before GTK. */
6522 static GdkFilterReturn
6523 event_handler_gdk (GdkXEvent *gxev, GdkEvent *ev, gpointer data)
6525 XEvent *xev = (XEvent *) gxev;
6527 block_input ();
6528 if (current_count >= 0)
6530 struct x_display_info *dpyinfo;
6532 dpyinfo = x_display_info_for_display (xev->xany.display);
6534 #ifdef HAVE_X_I18N
6535 /* Filter events for the current X input method.
6536 GTK calls XFilterEvent but not for key press and release,
6537 so we do it here. */
6538 if ((xev->type == KeyPress || xev->type == KeyRelease)
6539 && dpyinfo
6540 && x_filter_event (dpyinfo, xev))
6542 unblock_input ();
6543 return GDK_FILTER_REMOVE;
6545 #endif
6547 if (! dpyinfo)
6548 current_finish = X_EVENT_NORMAL;
6549 else
6550 current_count
6551 += handle_one_xevent (dpyinfo, xev, &current_finish,
6552 current_hold_quit);
6554 else
6555 current_finish = x_dispatch_event (xev, xev->xany.display);
6557 unblock_input ();
6559 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
6560 return GDK_FILTER_REMOVE;
6562 return GDK_FILTER_CONTINUE;
6564 #endif /* USE_GTK */
6567 static void xembed_send_message (struct frame *f, Time,
6568 enum xembed_message,
6569 long detail, long data1, long data2);
6571 static void
6572 x_net_wm_state (struct frame *f, Window window)
6574 int value = FULLSCREEN_NONE;
6575 Lisp_Object lval = Qnil;
6576 bool sticky = false;
6578 get_current_wm_state (f, window, &value, &sticky);
6580 switch (value)
6582 case FULLSCREEN_WIDTH:
6583 lval = Qfullwidth;
6584 break;
6585 case FULLSCREEN_HEIGHT:
6586 lval = Qfullheight;
6587 break;
6588 case FULLSCREEN_BOTH:
6589 lval = Qfullboth;
6590 break;
6591 case FULLSCREEN_MAXIMIZED:
6592 lval = Qmaximized;
6593 break;
6596 store_frame_param (f, Qfullscreen, lval);
6597 /** store_frame_param (f, Qsticky, sticky ? Qt : Qnil); **/
6600 /* Handles the XEvent EVENT on display DPYINFO.
6602 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
6603 *FINISH is zero if caller should continue reading events.
6604 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
6605 *EVENT is unchanged unless we're processing KeyPress event.
6607 We return the number of characters stored into the buffer. */
6609 static int
6610 handle_one_xevent (struct x_display_info *dpyinfo,
6611 const XEvent *event,
6612 int *finish, struct input_event *hold_quit)
6614 union {
6615 struct input_event ie;
6616 struct selection_input_event sie;
6617 } inev;
6618 int count = 0;
6619 int do_help = 0;
6620 ptrdiff_t nbytes = 0;
6621 struct frame *any, *f = NULL;
6622 struct coding_system coding;
6623 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
6624 /* This holds the state XLookupString needs to implement dead keys
6625 and other tricks known as "compose processing". _X Window System_
6626 says that a portable program can't use this, but Stephen Gildea assures
6627 me that letting the compiler initialize it to zeros will work okay. */
6628 static XComposeStatus compose_status;
6630 USE_SAFE_ALLOCA;
6632 *finish = X_EVENT_NORMAL;
6634 EVENT_INIT (inev.ie);
6635 inev.ie.kind = NO_EVENT;
6636 inev.ie.arg = Qnil;
6638 any = x_any_window_to_frame (dpyinfo, event->xany.window);
6640 if (any && any->wait_event_type == event->type)
6641 any->wait_event_type = 0; /* Indicates we got it. */
6643 switch (event->type)
6645 case ClientMessage:
6647 if (event->xclient.message_type == dpyinfo->Xatom_wm_protocols
6648 && event->xclient.format == 32)
6650 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_take_focus)
6652 /* Use the value returned by x_any_window_to_frame
6653 because this could be the shell widget window
6654 if the frame has no title bar. */
6655 f = any;
6656 #ifdef HAVE_X_I18N
6657 /* Not quite sure this is needed -pd */
6658 if (f && FRAME_XIC (f))
6659 XSetICFocus (FRAME_XIC (f));
6660 #endif
6661 #if false
6662 /* Emacs sets WM hints whose `input' field is `true'. This
6663 instructs the WM to set the input focus automatically for
6664 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
6665 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
6666 it has set the focus. So, XSetInputFocus below is not
6667 needed.
6669 The call to XSetInputFocus below has also caused trouble. In
6670 cases where the XSetInputFocus done by the WM and the one
6671 below are temporally close (on a fast machine), the call
6672 below can generate additional FocusIn events which confuse
6673 Emacs. */
6675 /* Since we set WM_TAKE_FOCUS, we must call
6676 XSetInputFocus explicitly. But not if f is null,
6677 since that might be an event for a deleted frame. */
6678 if (f)
6680 Display *d = event->xclient.display;
6681 /* Catch and ignore errors, in case window has been
6682 iconified by a window manager such as GWM. */
6683 x_catch_errors (d);
6684 XSetInputFocus (d, event->xclient.window,
6685 /* The ICCCM says this is
6686 the only valid choice. */
6687 RevertToParent,
6688 event->xclient.data.l[1]);
6689 /* This is needed to detect the error
6690 if there is an error. */
6691 XSync (d, False);
6692 x_uncatch_errors ();
6694 /* Not certain about handling scroll bars here */
6695 #endif
6696 goto done;
6699 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_save_yourself)
6701 /* Save state modify the WM_COMMAND property to
6702 something which can reinstate us. This notifies
6703 the session manager, who's looking for such a
6704 PropertyNotify. Can restart processing when
6705 a keyboard or mouse event arrives. */
6706 /* If we have a session manager, don't set this.
6707 KDE will then start two Emacsen, one for the
6708 session manager and one for this. */
6709 #ifdef HAVE_X_SM
6710 if (! x_session_have_connection ())
6711 #endif
6713 f = x_top_window_to_frame (dpyinfo,
6714 event->xclient.window);
6715 /* This is just so we only give real data once
6716 for a single Emacs process. */
6717 if (f == SELECTED_FRAME ())
6718 XSetCommand (FRAME_X_DISPLAY (f),
6719 event->xclient.window,
6720 initial_argv, initial_argc);
6721 else if (f)
6722 XSetCommand (FRAME_X_DISPLAY (f),
6723 event->xclient.window,
6724 0, 0);
6726 goto done;
6729 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_delete_window)
6731 f = any;
6732 if (!f)
6733 goto OTHER; /* May be a dialog that is to be removed */
6735 inev.ie.kind = DELETE_WINDOW_EVENT;
6736 XSETFRAME (inev.ie.frame_or_window, f);
6737 goto done;
6740 goto done;
6743 if (event->xclient.message_type == dpyinfo->Xatom_wm_configure_denied)
6744 goto done;
6746 if (event->xclient.message_type == dpyinfo->Xatom_wm_window_moved)
6748 int new_x, new_y;
6749 f = x_window_to_frame (dpyinfo, event->xclient.window);
6751 new_x = event->xclient.data.s[0];
6752 new_y = event->xclient.data.s[1];
6754 if (f)
6756 f->left_pos = new_x;
6757 f->top_pos = new_y;
6759 goto done;
6762 #ifdef HACK_EDITRES
6763 if (event->xclient.message_type == dpyinfo->Xatom_editres)
6765 f = any;
6766 if (f)
6767 _XEditResCheckMessages (f->output_data.x->widget,
6768 NULL, (XEvent *) event, NULL);
6769 goto done;
6771 #endif /* HACK_EDITRES */
6773 if (event->xclient.message_type == dpyinfo->Xatom_DONE
6774 || event->xclient.message_type == dpyinfo->Xatom_PAGE)
6776 /* Ghostview job completed. Kill it. We could
6777 reply with "Next" if we received "Page", but we
6778 currently never do because we are interested in
6779 images, only, which should have 1 page. */
6780 Pixmap pixmap = (Pixmap) event->xclient.data.l[1];
6781 f = x_window_to_frame (dpyinfo, event->xclient.window);
6782 if (!f)
6783 goto OTHER;
6784 x_kill_gs_process (pixmap, f);
6785 expose_frame (f, 0, 0, 0, 0);
6786 goto done;
6789 #ifdef USE_TOOLKIT_SCROLL_BARS
6790 /* Scroll bar callbacks send a ClientMessage from which
6791 we construct an input_event. */
6792 if (event->xclient.message_type == dpyinfo->Xatom_Scrollbar)
6794 x_scroll_bar_to_input_event (event, &inev.ie);
6795 *finish = X_EVENT_GOTO_OUT;
6796 goto done;
6798 else if (event->xclient.message_type == dpyinfo->Xatom_Horizontal_Scrollbar)
6800 x_horizontal_scroll_bar_to_input_event (event, &inev.ie);
6801 *finish = X_EVENT_GOTO_OUT;
6802 goto done;
6804 #endif /* USE_TOOLKIT_SCROLL_BARS */
6806 /* XEmbed messages from the embedder (if any). */
6807 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
6809 enum xembed_message msg = event->xclient.data.l[1];
6810 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
6811 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
6813 *finish = X_EVENT_GOTO_OUT;
6814 goto done;
6817 xft_settings_event (dpyinfo, event);
6819 f = any;
6820 if (!f)
6821 goto OTHER;
6822 if (x_handle_dnd_message (f, &event->xclient, dpyinfo, &inev.ie))
6823 *finish = X_EVENT_DROP;
6825 break;
6827 case SelectionNotify:
6828 x_display_set_last_user_time (dpyinfo, event->xselection.time);
6829 #ifdef USE_X_TOOLKIT
6830 if (! x_window_to_frame (dpyinfo, event->xselection.requestor))
6831 goto OTHER;
6832 #endif /* not USE_X_TOOLKIT */
6833 x_handle_selection_notify (&event->xselection);
6834 break;
6836 case SelectionClear: /* Someone has grabbed ownership. */
6837 x_display_set_last_user_time (dpyinfo, event->xselectionclear.time);
6838 #ifdef USE_X_TOOLKIT
6839 if (! x_window_to_frame (dpyinfo, event->xselectionclear.window))
6840 goto OTHER;
6841 #endif /* USE_X_TOOLKIT */
6843 const XSelectionClearEvent *eventp = &event->xselectionclear;
6845 inev.ie.kind = SELECTION_CLEAR_EVENT;
6846 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
6847 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6848 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6850 break;
6852 case SelectionRequest: /* Someone wants our selection. */
6853 x_display_set_last_user_time (dpyinfo, event->xselectionrequest.time);
6854 #ifdef USE_X_TOOLKIT
6855 if (!x_window_to_frame (dpyinfo, event->xselectionrequest.owner))
6856 goto OTHER;
6857 #endif /* USE_X_TOOLKIT */
6859 const XSelectionRequestEvent *eventp = &event->xselectionrequest;
6861 inev.ie.kind = SELECTION_REQUEST_EVENT;
6862 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
6863 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
6864 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6865 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
6866 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
6867 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6869 break;
6871 case PropertyNotify:
6872 x_display_set_last_user_time (dpyinfo, event->xproperty.time);
6873 f = x_top_window_to_frame (dpyinfo, event->xproperty.window);
6874 if (f && event->xproperty.atom == dpyinfo->Xatom_net_wm_state)
6876 bool not_hidden = x_handle_net_wm_state (f, &event->xproperty);
6877 if (not_hidden && FRAME_ICONIFIED_P (f))
6879 /* Gnome shell does not iconify us when C-z is pressed.
6880 It hides the frame. So if our state says we aren't
6881 hidden anymore, treat it as deiconified. */
6882 SET_FRAME_VISIBLE (f, 1);
6883 SET_FRAME_ICONIFIED (f, false);
6884 f->output_data.x->has_been_visible = true;
6885 inev.ie.kind = DEICONIFY_EVENT;
6886 XSETFRAME (inev.ie.frame_or_window, f);
6888 else if (! not_hidden && ! FRAME_ICONIFIED_P (f))
6890 SET_FRAME_VISIBLE (f, 0);
6891 SET_FRAME_ICONIFIED (f, true);
6892 inev.ie.kind = ICONIFY_EVENT;
6893 XSETFRAME (inev.ie.frame_or_window, f);
6897 x_handle_property_notify (&event->xproperty);
6898 xft_settings_event (dpyinfo, event);
6899 goto OTHER;
6901 case ReparentNotify:
6902 f = x_top_window_to_frame (dpyinfo, event->xreparent.window);
6903 if (f)
6905 f->output_data.x->parent_desc = event->xreparent.parent;
6906 x_real_positions (f, &f->left_pos, &f->top_pos);
6908 /* Perhaps reparented due to a WM restart. Reset this. */
6909 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6910 FRAME_DISPLAY_INFO (f)->net_supported_window = 0;
6912 x_set_frame_alpha (f);
6914 goto OTHER;
6916 case Expose:
6917 f = x_window_to_frame (dpyinfo, event->xexpose.window);
6918 if (f)
6920 if (!FRAME_VISIBLE_P (f))
6922 SET_FRAME_VISIBLE (f, 1);
6923 SET_FRAME_ICONIFIED (f, false);
6924 f->output_data.x->has_been_visible = true;
6925 SET_FRAME_GARBAGED (f);
6927 else
6929 #ifdef USE_GTK
6930 /* This seems to be needed for GTK 2.6 and later, see
6931 http://debbugs.gnu.org/cgi/bugreport.cgi?bug=15398. */
6932 x_clear_area (event->xexpose.display,
6933 event->xexpose.window,
6934 event->xexpose.x, event->xexpose.y,
6935 event->xexpose.width, event->xexpose.height);
6936 #endif
6937 expose_frame (f, event->xexpose.x, event->xexpose.y,
6938 event->xexpose.width, event->xexpose.height);
6941 else
6943 #ifndef USE_TOOLKIT_SCROLL_BARS
6944 struct scroll_bar *bar;
6945 #endif
6946 #if defined USE_LUCID
6947 /* Submenus of the Lucid menu bar aren't widgets
6948 themselves, so there's no way to dispatch events
6949 to them. Recognize this case separately. */
6951 Widget widget = x_window_to_menu_bar (event->xexpose.window);
6952 if (widget)
6953 xlwmenu_redisplay (widget);
6955 #endif /* USE_LUCID */
6957 #ifdef USE_TOOLKIT_SCROLL_BARS
6958 /* Dispatch event to the widget. */
6959 goto OTHER;
6960 #else /* not USE_TOOLKIT_SCROLL_BARS */
6961 bar = x_window_to_scroll_bar (event->xexpose.display,
6962 event->xexpose.window, 2);
6964 if (bar)
6965 x_scroll_bar_expose (bar, event);
6966 #ifdef USE_X_TOOLKIT
6967 else
6968 goto OTHER;
6969 #endif /* USE_X_TOOLKIT */
6970 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6972 break;
6974 case GraphicsExpose: /* This occurs when an XCopyArea's
6975 source area was obscured or not
6976 available. */
6977 f = x_window_to_frame (dpyinfo, event->xgraphicsexpose.drawable);
6978 if (f)
6979 expose_frame (f, event->xgraphicsexpose.x,
6980 event->xgraphicsexpose.y,
6981 event->xgraphicsexpose.width,
6982 event->xgraphicsexpose.height);
6983 #ifdef USE_X_TOOLKIT
6984 else
6985 goto OTHER;
6986 #endif /* USE_X_TOOLKIT */
6987 break;
6989 case NoExpose: /* This occurs when an XCopyArea's
6990 source area was completely
6991 available. */
6992 break;
6994 case UnmapNotify:
6995 /* Redo the mouse-highlight after the tooltip has gone. */
6996 if (event->xunmap.window == tip_window)
6998 tip_window = 0;
6999 x_redo_mouse_highlight (dpyinfo);
7002 f = x_top_window_to_frame (dpyinfo, event->xunmap.window);
7003 if (f) /* F may no longer exist if
7004 the frame was deleted. */
7006 bool visible = FRAME_VISIBLE_P (f);
7007 /* While a frame is unmapped, display generation is
7008 disabled; you don't want to spend time updating a
7009 display that won't ever be seen. */
7010 SET_FRAME_VISIBLE (f, 0);
7011 /* We can't distinguish, from the event, whether the window
7012 has become iconified or invisible. So assume, if it
7013 was previously visible, than now it is iconified.
7014 But x_make_frame_invisible clears both
7015 the visible flag and the iconified flag;
7016 and that way, we know the window is not iconified now. */
7017 if (visible || FRAME_ICONIFIED_P (f))
7019 SET_FRAME_ICONIFIED (f, true);
7020 inev.ie.kind = ICONIFY_EVENT;
7021 XSETFRAME (inev.ie.frame_or_window, f);
7024 goto OTHER;
7026 case MapNotify:
7027 if (event->xmap.window == tip_window)
7028 /* The tooltip has been drawn already. Avoid
7029 the SET_FRAME_GARBAGED below. */
7030 goto OTHER;
7032 /* We use x_top_window_to_frame because map events can
7033 come for sub-windows and they don't mean that the
7034 frame is visible. */
7035 f = x_top_window_to_frame (dpyinfo, event->xmap.window);
7036 if (f)
7038 bool iconified = FRAME_ICONIFIED_P (f);
7040 /* Check if fullscreen was specified before we where mapped the
7041 first time, i.e. from the command line. */
7042 if (!f->output_data.x->has_been_visible)
7043 x_check_fullscreen (f);
7045 SET_FRAME_VISIBLE (f, 1);
7046 SET_FRAME_ICONIFIED (f, false);
7047 f->output_data.x->has_been_visible = true;
7049 if (iconified)
7051 inev.ie.kind = DEICONIFY_EVENT;
7052 XSETFRAME (inev.ie.frame_or_window, f);
7054 else if (! NILP (Vframe_list) && ! NILP (XCDR (Vframe_list)))
7055 /* Force a redisplay sooner or later to update the
7056 frame titles in case this is the second frame. */
7057 record_asynch_buffer_change ();
7059 #ifdef USE_GTK
7060 xg_frame_resized (f, -1, -1);
7061 #endif
7063 goto OTHER;
7065 case KeyPress:
7067 x_display_set_last_user_time (dpyinfo, event->xkey.time);
7068 ignore_next_mouse_click_timeout = 0;
7070 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
7071 /* Dispatch KeyPress events when in menu. */
7072 if (popup_activated ())
7073 goto OTHER;
7074 #endif
7076 f = any;
7078 #if ! defined (USE_GTK)
7079 /* If mouse-highlight is an integer, input clears out
7080 mouse highlighting. */
7081 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
7082 && (f == 0
7083 || !EQ (f->tool_bar_window, hlinfo->mouse_face_window)))
7085 clear_mouse_face (hlinfo);
7086 hlinfo->mouse_face_hidden = true;
7088 #endif
7090 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
7091 if (f == 0)
7093 /* Scroll bars consume key events, but we want
7094 the keys to go to the scroll bar's frame. */
7095 Widget widget = XtWindowToWidget (dpyinfo->display,
7096 event->xkey.window);
7097 if (widget && XmIsScrollBar (widget))
7099 widget = XtParent (widget);
7100 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
7103 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
7105 if (f != 0)
7107 KeySym keysym, orig_keysym;
7108 /* al%imercury@uunet.uu.net says that making this 81
7109 instead of 80 fixed a bug whereby meta chars made
7110 his Emacs hang.
7112 It seems that some version of XmbLookupString has
7113 a bug of not returning XBufferOverflow in
7114 status_return even if the input is too long to
7115 fit in 81 bytes. So, we must prepare sufficient
7116 bytes for copy_buffer. 513 bytes (256 chars for
7117 two-byte character set) seems to be a fairly good
7118 approximation. -- 2000.8.10 handa@etl.go.jp */
7119 unsigned char copy_buffer[513];
7120 unsigned char *copy_bufptr = copy_buffer;
7121 int copy_bufsiz = sizeof (copy_buffer);
7122 int modifiers;
7123 Lisp_Object coding_system = Qlatin_1;
7124 Lisp_Object c;
7125 /* Event will be modified. */
7126 XKeyEvent xkey = event->xkey;
7128 #ifdef USE_GTK
7129 /* Don't pass keys to GTK. A Tab will shift focus to the
7130 tool bar in GTK 2.4. Keys will still go to menus and
7131 dialogs because in that case popup_activated is nonzero
7132 (see above). */
7133 *finish = X_EVENT_DROP;
7134 #endif
7136 xkey.state |= x_emacs_to_x_modifiers (FRAME_DISPLAY_INFO (f),
7137 extra_keyboard_modifiers);
7138 modifiers = xkey.state;
7140 /* This will have to go some day... */
7142 /* make_lispy_event turns chars into control chars.
7143 Don't do it here because XLookupString is too eager. */
7144 xkey.state &= ~ControlMask;
7145 xkey.state &= ~(dpyinfo->meta_mod_mask
7146 | dpyinfo->super_mod_mask
7147 | dpyinfo->hyper_mod_mask
7148 | dpyinfo->alt_mod_mask);
7150 /* In case Meta is ComposeCharacter,
7151 clear its status. According to Markus Ehrnsperger
7152 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
7153 this enables ComposeCharacter to work whether or
7154 not it is combined with Meta. */
7155 if (modifiers & dpyinfo->meta_mod_mask)
7156 memset (&compose_status, 0, sizeof (compose_status));
7158 #ifdef HAVE_X_I18N
7159 if (FRAME_XIC (f))
7161 Status status_return;
7163 coding_system = Vlocale_coding_system;
7164 nbytes = XmbLookupString (FRAME_XIC (f),
7165 &xkey, (char *) copy_bufptr,
7166 copy_bufsiz, &keysym,
7167 &status_return);
7168 if (status_return == XBufferOverflow)
7170 copy_bufsiz = nbytes + 1;
7171 copy_bufptr = alloca (copy_bufsiz);
7172 nbytes = XmbLookupString (FRAME_XIC (f),
7173 &xkey, (char *) copy_bufptr,
7174 copy_bufsiz, &keysym,
7175 &status_return);
7177 /* Xutf8LookupString is a new but already deprecated interface. -stef */
7178 if (status_return == XLookupNone)
7179 break;
7180 else if (status_return == XLookupChars)
7182 keysym = NoSymbol;
7183 modifiers = 0;
7185 else if (status_return != XLookupKeySym
7186 && status_return != XLookupBoth)
7187 emacs_abort ();
7189 else
7190 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
7191 copy_bufsiz, &keysym,
7192 &compose_status);
7193 #else
7194 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
7195 copy_bufsiz, &keysym,
7196 &compose_status);
7197 #endif
7199 /* If not using XIM/XIC, and a compose sequence is in progress,
7200 we break here. Otherwise, chars_matched is always 0. */
7201 if (compose_status.chars_matched > 0 && nbytes == 0)
7202 break;
7204 memset (&compose_status, 0, sizeof (compose_status));
7205 orig_keysym = keysym;
7207 /* Common for all keysym input events. */
7208 XSETFRAME (inev.ie.frame_or_window, f);
7209 inev.ie.modifiers
7210 = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), modifiers);
7211 inev.ie.timestamp = xkey.time;
7213 /* First deal with keysyms which have defined
7214 translations to characters. */
7215 if (keysym >= 32 && keysym < 128)
7216 /* Avoid explicitly decoding each ASCII character. */
7218 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
7219 inev.ie.code = keysym;
7220 goto done_keysym;
7223 /* Keysyms directly mapped to Unicode characters. */
7224 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
7226 if (keysym < 0x01000080)
7227 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
7228 else
7229 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
7230 inev.ie.code = keysym & 0xFFFFFF;
7231 goto done_keysym;
7234 /* Now non-ASCII. */
7235 if (HASH_TABLE_P (Vx_keysym_table)
7236 && (c = Fgethash (make_number (keysym),
7237 Vx_keysym_table,
7238 Qnil),
7239 NATNUMP (c)))
7241 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
7242 ? ASCII_KEYSTROKE_EVENT
7243 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
7244 inev.ie.code = XFASTINT (c);
7245 goto done_keysym;
7248 /* Random non-modifier sorts of keysyms. */
7249 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
7250 || keysym == XK_Delete
7251 #ifdef XK_ISO_Left_Tab
7252 || (keysym >= XK_ISO_Left_Tab
7253 && keysym <= XK_ISO_Enter)
7254 #endif
7255 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
7256 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
7257 #ifdef HPUX
7258 /* This recognizes the "extended function
7259 keys". It seems there's no cleaner way.
7260 Test IsModifierKey to avoid handling
7261 mode_switch incorrectly. */
7262 || (XK_Select <= keysym && keysym < XK_KP_Space)
7263 #endif
7264 #ifdef XK_dead_circumflex
7265 || orig_keysym == XK_dead_circumflex
7266 #endif
7267 #ifdef XK_dead_grave
7268 || orig_keysym == XK_dead_grave
7269 #endif
7270 #ifdef XK_dead_tilde
7271 || orig_keysym == XK_dead_tilde
7272 #endif
7273 #ifdef XK_dead_diaeresis
7274 || orig_keysym == XK_dead_diaeresis
7275 #endif
7276 #ifdef XK_dead_macron
7277 || orig_keysym == XK_dead_macron
7278 #endif
7279 #ifdef XK_dead_degree
7280 || orig_keysym == XK_dead_degree
7281 #endif
7282 #ifdef XK_dead_acute
7283 || orig_keysym == XK_dead_acute
7284 #endif
7285 #ifdef XK_dead_cedilla
7286 || orig_keysym == XK_dead_cedilla
7287 #endif
7288 #ifdef XK_dead_breve
7289 || orig_keysym == XK_dead_breve
7290 #endif
7291 #ifdef XK_dead_ogonek
7292 || orig_keysym == XK_dead_ogonek
7293 #endif
7294 #ifdef XK_dead_caron
7295 || orig_keysym == XK_dead_caron
7296 #endif
7297 #ifdef XK_dead_doubleacute
7298 || orig_keysym == XK_dead_doubleacute
7299 #endif
7300 #ifdef XK_dead_abovedot
7301 || orig_keysym == XK_dead_abovedot
7302 #endif
7303 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
7304 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
7305 /* Any "vendor-specific" key is ok. */
7306 || (orig_keysym & (1 << 28))
7307 || (keysym != NoSymbol && nbytes == 0))
7308 && ! (IsModifierKey (orig_keysym)
7309 /* The symbols from XK_ISO_Lock
7310 to XK_ISO_Last_Group_Lock
7311 don't have real modifiers but
7312 should be treated similarly to
7313 Mode_switch by Emacs. */
7314 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
7315 || (XK_ISO_Lock <= orig_keysym
7316 && orig_keysym <= XK_ISO_Last_Group_Lock)
7317 #endif
7320 STORE_KEYSYM_FOR_DEBUG (keysym);
7321 /* make_lispy_event will convert this to a symbolic
7322 key. */
7323 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
7324 inev.ie.code = keysym;
7325 goto done_keysym;
7328 { /* Raw bytes, not keysym. */
7329 ptrdiff_t i;
7330 int nchars, len;
7332 for (i = 0, nchars = 0; i < nbytes; i++)
7334 if (ASCII_CHAR_P (copy_bufptr[i]))
7335 nchars++;
7336 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
7339 if (nchars < nbytes)
7341 /* Decode the input data. */
7343 /* The input should be decoded with `coding_system'
7344 which depends on which X*LookupString function
7345 we used just above and the locale. */
7346 setup_coding_system (coding_system, &coding);
7347 coding.src_multibyte = false;
7348 coding.dst_multibyte = true;
7349 /* The input is converted to events, thus we can't
7350 handle composition. Anyway, there's no XIM that
7351 gives us composition information. */
7352 coding.common_flags &= ~CODING_ANNOTATION_MASK;
7354 SAFE_NALLOCA (coding.destination, MAX_MULTIBYTE_LENGTH,
7355 nbytes);
7356 coding.dst_bytes = MAX_MULTIBYTE_LENGTH * nbytes;
7357 coding.mode |= CODING_MODE_LAST_BLOCK;
7358 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
7359 nbytes = coding.produced;
7360 nchars = coding.produced_char;
7361 copy_bufptr = coding.destination;
7364 /* Convert the input data to a sequence of
7365 character events. */
7366 for (i = 0; i < nbytes; i += len)
7368 int ch;
7369 if (nchars == nbytes)
7370 ch = copy_bufptr[i], len = 1;
7371 else
7372 ch = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
7373 inev.ie.kind = (SINGLE_BYTE_CHAR_P (ch)
7374 ? ASCII_KEYSTROKE_EVENT
7375 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
7376 inev.ie.code = ch;
7377 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
7380 count += nchars;
7382 inev.ie.kind = NO_EVENT; /* Already stored above. */
7384 if (keysym == NoSymbol)
7385 break;
7387 /* FIXME: check side effects and remove this. */
7388 ((XEvent *) event)->xkey = xkey;
7390 done_keysym:
7391 #ifdef HAVE_X_I18N
7392 /* Don't dispatch this event since XtDispatchEvent calls
7393 XFilterEvent, and two calls in a row may freeze the
7394 client. */
7395 break;
7396 #else
7397 goto OTHER;
7398 #endif
7400 case KeyRelease:
7401 x_display_set_last_user_time (dpyinfo, event->xkey.time);
7402 #ifdef HAVE_X_I18N
7403 /* Don't dispatch this event since XtDispatchEvent calls
7404 XFilterEvent, and two calls in a row may freeze the
7405 client. */
7406 break;
7407 #else
7408 goto OTHER;
7409 #endif
7411 case EnterNotify:
7412 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
7413 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7415 f = any;
7417 if (f && x_mouse_click_focus_ignore_position)
7418 ignore_next_mouse_click_timeout = event->xmotion.time + 200;
7420 /* EnterNotify counts as mouse movement,
7421 so update things that depend on mouse position. */
7422 if (f && !f->output_data.x->hourglass_p)
7423 note_mouse_movement (f, &event->xmotion);
7424 #ifdef USE_GTK
7425 /* We may get an EnterNotify on the buttons in the toolbar. In that
7426 case we moved out of any highlighted area and need to note this. */
7427 if (!f && dpyinfo->last_mouse_glyph_frame)
7428 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
7429 #endif
7430 goto OTHER;
7432 case FocusIn:
7433 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7434 goto OTHER;
7436 case LeaveNotify:
7437 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
7438 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7440 f = x_top_window_to_frame (dpyinfo, event->xcrossing.window);
7441 if (f)
7443 if (f == hlinfo->mouse_face_mouse_frame)
7445 /* If we move outside the frame, then we're
7446 certainly no longer on any text in the frame. */
7447 clear_mouse_face (hlinfo);
7448 hlinfo->mouse_face_mouse_frame = 0;
7451 /* Generate a nil HELP_EVENT to cancel a help-echo.
7452 Do it only if there's something to cancel.
7453 Otherwise, the startup message is cleared when
7454 the mouse leaves the frame. */
7455 if (any_help_event_p)
7456 do_help = -1;
7458 #ifdef USE_GTK
7459 /* See comment in EnterNotify above */
7460 else if (dpyinfo->last_mouse_glyph_frame)
7461 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
7462 #endif
7463 goto OTHER;
7465 case FocusOut:
7466 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7467 goto OTHER;
7469 case MotionNotify:
7471 x_display_set_last_user_time (dpyinfo, event->xmotion.time);
7472 previous_help_echo_string = help_echo_string;
7473 help_echo_string = Qnil;
7475 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
7476 : x_window_to_frame (dpyinfo, event->xmotion.window));
7478 if (hlinfo->mouse_face_hidden)
7480 hlinfo->mouse_face_hidden = false;
7481 clear_mouse_face (hlinfo);
7484 #ifdef USE_GTK
7485 if (f && xg_event_is_for_scrollbar (f, event))
7486 f = 0;
7487 #endif
7488 if (f)
7491 /* Generate SELECT_WINDOW_EVENTs when needed.
7492 Don't let popup menus influence things (bug#1261). */
7493 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
7495 static Lisp_Object last_mouse_window;
7496 Lisp_Object window = window_from_coordinates
7497 (f, event->xmotion.x, event->xmotion.y, 0, false);
7499 /* Window will be selected only when it is not selected now and
7500 last mouse movement event was not in it. Minibuffer window
7501 will be selected only when it is active. */
7502 if (WINDOWP (window)
7503 && !EQ (window, last_mouse_window)
7504 && !EQ (window, selected_window)
7505 /* For click-to-focus window managers
7506 create event iff we don't leave the
7507 selected frame. */
7508 && (focus_follows_mouse
7509 || (EQ (XWINDOW (window)->frame,
7510 XWINDOW (selected_window)->frame))))
7512 inev.ie.kind = SELECT_WINDOW_EVENT;
7513 inev.ie.frame_or_window = window;
7515 /* Remember the last window where we saw the mouse. */
7516 last_mouse_window = window;
7518 if (!note_mouse_movement (f, &event->xmotion))
7519 help_echo_string = previous_help_echo_string;
7521 else
7523 #ifndef USE_TOOLKIT_SCROLL_BARS
7524 struct scroll_bar *bar
7525 = x_window_to_scroll_bar (event->xmotion.display,
7526 event->xmotion.window, 2);
7528 if (bar)
7529 x_scroll_bar_note_movement (bar, &event->xmotion);
7530 #endif /* USE_TOOLKIT_SCROLL_BARS */
7532 /* If we move outside the frame, then we're
7533 certainly no longer on any text in the frame. */
7534 clear_mouse_face (hlinfo);
7537 /* If the contents of the global variable help_echo_string
7538 has changed, generate a HELP_EVENT. */
7539 if (!NILP (help_echo_string)
7540 || !NILP (previous_help_echo_string))
7541 do_help = 1;
7542 goto OTHER;
7545 case ConfigureNotify:
7546 f = x_top_window_to_frame (dpyinfo, event->xconfigure.window);
7547 #ifdef USE_GTK
7548 if (!f
7549 && (f = any)
7550 && event->xconfigure.window == FRAME_X_WINDOW (f))
7552 xg_frame_resized (f, event->xconfigure.width,
7553 event->xconfigure.height);
7554 f = 0;
7556 #endif
7557 if (f)
7559 x_net_wm_state (f, event->xconfigure.window);
7561 #ifndef USE_X_TOOLKIT
7562 #ifndef USE_GTK
7563 int width = FRAME_PIXEL_TO_TEXT_WIDTH (f, event->xconfigure.width);
7564 int height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, event->xconfigure.height);
7566 /* In the toolkit version, change_frame_size
7567 is called by the code that handles resizing
7568 of the EmacsFrame widget. */
7570 /* Even if the number of character rows and columns has
7571 not changed, the font size may have changed, so we need
7572 to check the pixel dimensions as well. */
7573 if (width != FRAME_TEXT_WIDTH (f)
7574 || height != FRAME_TEXT_HEIGHT (f)
7575 || event->xconfigure.width != FRAME_PIXEL_WIDTH (f)
7576 || event->xconfigure.height != FRAME_PIXEL_HEIGHT (f))
7578 change_frame_size (f, width, height, false, true, false, true);
7579 x_clear_under_internal_border (f);
7580 SET_FRAME_GARBAGED (f);
7581 cancel_mouse_face (f);
7583 #endif /* not USE_GTK */
7584 #endif
7586 #ifdef USE_GTK
7587 /* GTK creates windows but doesn't map them.
7588 Only get real positions when mapped. */
7589 if (FRAME_GTK_OUTER_WIDGET (f)
7590 && gtk_widget_get_mapped (FRAME_GTK_OUTER_WIDGET (f)))
7591 #endif
7592 x_real_positions (f, &f->left_pos, &f->top_pos);
7594 #ifdef HAVE_X_I18N
7595 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
7596 xic_set_statusarea (f);
7597 #endif
7600 goto OTHER;
7602 case ButtonRelease:
7603 case ButtonPress:
7605 /* If we decide we want to generate an event to be seen
7606 by the rest of Emacs, we put it here. */
7607 bool tool_bar_p = false;
7609 memset (&compose_status, 0, sizeof (compose_status));
7610 dpyinfo->last_mouse_glyph_frame = NULL;
7611 x_display_set_last_user_time (dpyinfo, event->xbutton.time);
7613 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
7614 : x_window_to_frame (dpyinfo, event->xbutton.window));
7616 #ifdef USE_GTK
7617 if (f && xg_event_is_for_scrollbar (f, event))
7618 f = 0;
7619 #endif
7620 if (f)
7622 #if ! defined (USE_GTK)
7623 /* Is this in the tool-bar? */
7624 if (WINDOWP (f->tool_bar_window)
7625 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
7627 Lisp_Object window;
7628 int x = event->xbutton.x;
7629 int y = event->xbutton.y;
7631 window = window_from_coordinates (f, x, y, 0, true);
7632 tool_bar_p = EQ (window, f->tool_bar_window);
7634 if (tool_bar_p && event->xbutton.button < 4)
7635 handle_tool_bar_click
7636 (f, x, y, event->xbutton.type == ButtonPress,
7637 x_x_to_emacs_modifiers (dpyinfo, event->xbutton.state));
7639 #endif /* !USE_GTK */
7641 if (!tool_bar_p)
7642 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
7643 if (! popup_activated ())
7644 #endif
7646 if (ignore_next_mouse_click_timeout)
7648 if (event->type == ButtonPress
7649 && event->xbutton.time > ignore_next_mouse_click_timeout)
7651 ignore_next_mouse_click_timeout = 0;
7652 construct_mouse_click (&inev.ie, &event->xbutton, f);
7654 if (event->type == ButtonRelease)
7655 ignore_next_mouse_click_timeout = 0;
7657 else
7658 construct_mouse_click (&inev.ie, &event->xbutton, f);
7660 if (FRAME_X_EMBEDDED_P (f))
7661 xembed_send_message (f, event->xbutton.time,
7662 XEMBED_REQUEST_FOCUS, 0, 0, 0);
7664 else
7666 struct scroll_bar *bar
7667 = x_window_to_scroll_bar (event->xbutton.display,
7668 event->xbutton.window, 2);
7670 #ifdef USE_TOOLKIT_SCROLL_BARS
7671 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
7672 scroll bars. */
7673 if (bar && event->xbutton.state & ControlMask)
7675 x_scroll_bar_handle_click (bar, event, &inev.ie);
7676 *finish = X_EVENT_DROP;
7678 #else /* not USE_TOOLKIT_SCROLL_BARS */
7679 if (bar)
7680 x_scroll_bar_handle_click (bar, event, &inev.ie);
7681 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7684 if (event->type == ButtonPress)
7686 dpyinfo->grabbed |= (1 << event->xbutton.button);
7687 dpyinfo->last_mouse_frame = f;
7688 #if ! defined (USE_GTK)
7689 if (f && !tool_bar_p)
7690 f->last_tool_bar_item = -1;
7691 #endif /* not USE_GTK */
7693 else
7694 dpyinfo->grabbed &= ~(1 << event->xbutton.button);
7696 /* Ignore any mouse motion that happened before this event;
7697 any subsequent mouse-movement Emacs events should reflect
7698 only motion after the ButtonPress/Release. */
7699 if (f != 0)
7700 f->mouse_moved = false;
7702 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
7703 f = x_menubar_window_to_frame (dpyinfo, event);
7704 /* For a down-event in the menu bar,
7705 don't pass it to Xt right now.
7706 Instead, save it away
7707 and we will pass it to Xt from kbd_buffer_get_event.
7708 That way, we can run some Lisp code first. */
7709 if (! popup_activated ()
7710 #ifdef USE_GTK
7711 /* Gtk+ menus only react to the first three buttons. */
7712 && event->xbutton.button < 3
7713 #endif
7714 && f && event->type == ButtonPress
7715 /* Verify the event is really within the menu bar
7716 and not just sent to it due to grabbing. */
7717 && event->xbutton.x >= 0
7718 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
7719 && event->xbutton.y >= 0
7720 && event->xbutton.y < FRAME_MENUBAR_HEIGHT (f)
7721 && event->xbutton.same_screen)
7723 if (!f->output_data.x->saved_menu_event)
7724 f->output_data.x->saved_menu_event = xmalloc (sizeof *event);
7725 *f->output_data.x->saved_menu_event = *event;
7726 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT;
7727 XSETFRAME (inev.ie.frame_or_window, f);
7728 *finish = X_EVENT_DROP;
7730 else
7731 goto OTHER;
7732 #endif /* USE_X_TOOLKIT || USE_GTK */
7734 break;
7736 case CirculateNotify:
7737 goto OTHER;
7739 case CirculateRequest:
7740 goto OTHER;
7742 case VisibilityNotify:
7743 goto OTHER;
7745 case MappingNotify:
7746 /* Someone has changed the keyboard mapping - update the
7747 local cache. */
7748 switch (event->xmapping.request)
7750 case MappingModifier:
7751 x_find_modifier_meanings (dpyinfo);
7752 /* This is meant to fall through. */
7753 case MappingKeyboard:
7754 XRefreshKeyboardMapping ((XMappingEvent *) &event->xmapping);
7756 goto OTHER;
7758 case DestroyNotify:
7759 xft_settings_event (dpyinfo, event);
7760 break;
7762 default:
7763 OTHER:
7764 #ifdef USE_X_TOOLKIT
7765 block_input ();
7766 if (*finish != X_EVENT_DROP)
7767 XtDispatchEvent ((XEvent *) event);
7768 unblock_input ();
7769 #endif /* USE_X_TOOLKIT */
7770 break;
7773 done:
7774 if (inev.ie.kind != NO_EVENT)
7776 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
7777 count++;
7780 if (do_help
7781 && !(hold_quit && hold_quit->kind != NO_EVENT))
7783 Lisp_Object frame;
7785 if (f)
7786 XSETFRAME (frame, f);
7787 else
7788 frame = Qnil;
7790 if (do_help > 0)
7792 any_help_event_p = true;
7793 gen_help_event (help_echo_string, frame, help_echo_window,
7794 help_echo_object, help_echo_pos);
7796 else
7798 help_echo_string = Qnil;
7799 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
7801 count++;
7804 SAFE_FREE ();
7805 return count;
7808 /* Handles the XEvent EVENT on display DISPLAY.
7809 This is used for event loops outside the normal event handling,
7810 i.e. looping while a popup menu or a dialog is posted.
7812 Returns the value handle_one_xevent sets in the finish argument. */
7814 x_dispatch_event (XEvent *event, Display *display)
7816 struct x_display_info *dpyinfo;
7817 int finish = X_EVENT_NORMAL;
7819 dpyinfo = x_display_info_for_display (display);
7821 if (dpyinfo)
7822 handle_one_xevent (dpyinfo, event, &finish, 0);
7824 return finish;
7827 /* Read events coming from the X server.
7828 Return as soon as there are no more events to be read.
7830 Return the number of characters stored into the buffer,
7831 thus pretending to be `read' (except the characters we store
7832 in the keyboard buffer can be multibyte, so are not necessarily
7833 C chars). */
7835 static int
7836 XTread_socket (struct terminal *terminal, struct input_event *hold_quit)
7838 int count = 0;
7839 bool event_found = false;
7840 struct x_display_info *dpyinfo = terminal->display_info.x;
7842 block_input ();
7844 /* For debugging, this gives a way to fake an I/O error. */
7845 if (dpyinfo == XTread_socket_fake_io_error)
7847 XTread_socket_fake_io_error = 0;
7848 x_io_error_quitter (dpyinfo->display);
7851 #ifndef USE_GTK
7852 while (XPending (dpyinfo->display))
7854 int finish;
7855 XEvent event;
7857 XNextEvent (dpyinfo->display, &event);
7859 #ifdef HAVE_X_I18N
7860 /* Filter events for the current X input method. */
7861 if (x_filter_event (dpyinfo, &event))
7862 continue;
7863 #endif
7864 event_found = true;
7866 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
7868 if (finish == X_EVENT_GOTO_OUT)
7869 break;
7872 #else /* USE_GTK */
7874 /* For GTK we must use the GTK event loop. But XEvents gets passed
7875 to our filter function above, and then to the big event switch.
7876 We use a bunch of globals to communicate with our filter function,
7877 that is kind of ugly, but it works.
7879 There is no way to do one display at the time, GTK just does events
7880 from all displays. */
7882 while (gtk_events_pending ())
7884 current_count = count;
7885 current_hold_quit = hold_quit;
7887 gtk_main_iteration ();
7889 count = current_count;
7890 current_count = -1;
7891 current_hold_quit = 0;
7893 if (current_finish == X_EVENT_GOTO_OUT)
7894 break;
7896 #endif /* USE_GTK */
7898 /* On some systems, an X bug causes Emacs to get no more events
7899 when the window is destroyed. Detect that. (1994.) */
7900 if (! event_found)
7902 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7903 One XNOOP in 100 loops will make Emacs terminate.
7904 B. Bretthauer, 1994 */
7905 x_noop_count++;
7906 if (x_noop_count >= 100)
7908 x_noop_count=0;
7910 if (next_noop_dpyinfo == 0)
7911 next_noop_dpyinfo = x_display_list;
7913 XNoOp (next_noop_dpyinfo->display);
7915 /* Each time we get here, cycle through the displays now open. */
7916 next_noop_dpyinfo = next_noop_dpyinfo->next;
7920 /* If the focus was just given to an auto-raising frame,
7921 raise it now. FIXME: handle more than one such frame. */
7922 if (dpyinfo->x_pending_autoraise_frame)
7924 x_raise_frame (dpyinfo->x_pending_autoraise_frame);
7925 dpyinfo->x_pending_autoraise_frame = NULL;
7928 unblock_input ();
7930 return count;
7936 /***********************************************************************
7937 Text Cursor
7938 ***********************************************************************/
7940 /* Set clipping for output in glyph row ROW. W is the window in which
7941 we operate. GC is the graphics context to set clipping in.
7943 ROW may be a text row or, e.g., a mode line. Text rows must be
7944 clipped to the interior of the window dedicated to text display,
7945 mode lines must be clipped to the whole window. */
7947 static void
7948 x_clip_to_row (struct window *w, struct glyph_row *row,
7949 enum glyph_row_area area, GC gc)
7951 struct frame *f = XFRAME (WINDOW_FRAME (w));
7952 XRectangle clip_rect;
7953 int window_x, window_y, window_width;
7955 window_box (w, area, &window_x, &window_y, &window_width, 0);
7957 clip_rect.x = window_x;
7958 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7959 clip_rect.y = max (clip_rect.y, window_y);
7960 clip_rect.width = window_width;
7961 clip_rect.height = row->visible_height;
7963 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7967 /* Draw a hollow box cursor on window W in glyph row ROW. */
7969 static void
7970 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
7972 struct frame *f = XFRAME (WINDOW_FRAME (w));
7973 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
7974 Display *dpy = FRAME_X_DISPLAY (f);
7975 int x, y, wd, h;
7976 XGCValues xgcv;
7977 struct glyph *cursor_glyph;
7978 GC gc;
7980 /* Get the glyph the cursor is on. If we can't tell because
7981 the current matrix is invalid or such, give up. */
7982 cursor_glyph = get_phys_cursor_glyph (w);
7983 if (cursor_glyph == NULL)
7984 return;
7986 /* Compute frame-relative coordinates for phys cursor. */
7987 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7988 wd = w->phys_cursor_width - 1;
7990 /* The foreground of cursor_gc is typically the same as the normal
7991 background color, which can cause the cursor box to be invisible. */
7992 xgcv.foreground = f->output_data.x->cursor_pixel;
7993 if (dpyinfo->scratch_cursor_gc)
7994 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7995 else
7996 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7997 GCForeground, &xgcv);
7998 gc = dpyinfo->scratch_cursor_gc;
8000 /* When on R2L character, show cursor at the right edge of the
8001 glyph, unless the cursor box is as wide as the glyph or wider
8002 (the latter happens when x-stretch-cursor is non-nil). */
8003 if ((cursor_glyph->resolved_level & 1) != 0
8004 && cursor_glyph->pixel_width > wd)
8006 x += cursor_glyph->pixel_width - wd;
8007 if (wd > 0)
8008 wd -= 1;
8010 /* Set clipping, draw the rectangle, and reset clipping again. */
8011 x_clip_to_row (w, row, TEXT_AREA, gc);
8012 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
8013 XSetClipMask (dpy, gc, None);
8017 /* Draw a bar cursor on window W in glyph row ROW.
8019 Implementation note: One would like to draw a bar cursor with an
8020 angle equal to the one given by the font property XA_ITALIC_ANGLE.
8021 Unfortunately, I didn't find a font yet that has this property set.
8022 --gerd. */
8024 static void
8025 x_draw_bar_cursor (struct window *w, struct glyph_row *row, int width, enum text_cursor_kinds kind)
8027 struct frame *f = XFRAME (w->frame);
8028 struct glyph *cursor_glyph;
8030 /* If cursor is out of bounds, don't draw garbage. This can happen
8031 in mini-buffer windows when switching between echo area glyphs
8032 and mini-buffer. */
8033 cursor_glyph = get_phys_cursor_glyph (w);
8034 if (cursor_glyph == NULL)
8035 return;
8037 #ifdef HAVE_XWIDGETS
8038 if (cursor_glyph->type == XWIDGET_GLYPH){
8039 printf("tried avoiding xwidget cursor\n");
8040 return; //experimental avoidance of cursor on xwidget
8042 #endif
8043 /* If on an image, draw like a normal cursor. That's usually better
8044 visible than drawing a bar, esp. if the image is large so that
8045 the bar might not be in the window. */
8046 if (cursor_glyph->type == IMAGE_GLYPH)
8048 struct glyph_row *r;
8049 r = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
8050 draw_phys_cursor_glyph (w, r, DRAW_CURSOR);
8052 else
8054 Display *dpy = FRAME_X_DISPLAY (f);
8055 Window window = FRAME_X_WINDOW (f);
8056 GC gc = FRAME_DISPLAY_INFO (f)->scratch_cursor_gc;
8057 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
8058 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
8059 XGCValues xgcv;
8061 /* If the glyph's background equals the color we normally draw
8062 the bars cursor in, the bar cursor in its normal color is
8063 invisible. Use the glyph's foreground color instead in this
8064 case, on the assumption that the glyph's colors are chosen so
8065 that the glyph is legible. */
8066 if (face->background == f->output_data.x->cursor_pixel)
8067 xgcv.background = xgcv.foreground = face->foreground;
8068 else
8069 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
8070 xgcv.graphics_exposures = False;
8072 if (gc)
8073 XChangeGC (dpy, gc, mask, &xgcv);
8074 else
8076 gc = XCreateGC (dpy, window, mask, &xgcv);
8077 FRAME_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
8080 x_clip_to_row (w, row, TEXT_AREA, gc);
8082 if (kind == BAR_CURSOR)
8084 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8086 if (width < 0)
8087 width = FRAME_CURSOR_WIDTH (f);
8088 width = min (cursor_glyph->pixel_width, width);
8090 w->phys_cursor_width = width;
8092 /* If the character under cursor is R2L, draw the bar cursor
8093 on the right of its glyph, rather than on the left. */
8094 if ((cursor_glyph->resolved_level & 1) != 0)
8095 x += cursor_glyph->pixel_width - width;
8097 XFillRectangle (dpy, window, gc, x,
8098 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
8099 width, row->height);
8101 else /* HBAR_CURSOR */
8103 int dummy_x, dummy_y, dummy_h;
8104 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8106 if (width < 0)
8107 width = row->height;
8109 width = min (row->height, width);
8111 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
8112 &dummy_y, &dummy_h);
8114 if ((cursor_glyph->resolved_level & 1) != 0
8115 && cursor_glyph->pixel_width > w->phys_cursor_width - 1)
8116 x += cursor_glyph->pixel_width - w->phys_cursor_width + 1;
8117 XFillRectangle (dpy, window, gc, x,
8118 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
8119 row->height - width),
8120 w->phys_cursor_width - 1, width);
8123 XSetClipMask (dpy, gc, None);
8128 /* RIF: Define cursor CURSOR on frame F. */
8130 static void
8131 x_define_frame_cursor (struct frame *f, Cursor cursor)
8133 if (!f->pointer_invisible
8134 && f->output_data.x->current_cursor != cursor)
8135 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
8136 f->output_data.x->current_cursor = cursor;
8140 /* RIF: Clear area on frame F. */
8142 static void
8143 x_clear_frame_area (struct frame *f, int x, int y, int width, int height)
8145 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), x, y, width, height);
8146 #ifdef USE_GTK
8147 /* Must queue a redraw, because scroll bars might have been cleared. */
8148 if (FRAME_GTK_WIDGET (f))
8149 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
8150 #endif
8154 /* RIF: Draw cursor on window W. */
8156 static void
8157 x_draw_window_cursor (struct window *w, struct glyph_row *glyph_row, int x,
8158 int y, enum text_cursor_kinds cursor_type,
8159 int cursor_width, bool on_p, bool active_p)
8161 struct frame *f = XFRAME (WINDOW_FRAME (w));
8163 if (on_p)
8165 w->phys_cursor_type = cursor_type;
8166 w->phys_cursor_on_p = true;
8168 if (glyph_row->exact_window_width_line_p
8169 && (glyph_row->reversed_p
8170 ? (w->phys_cursor.hpos < 0)
8171 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
8173 glyph_row->cursor_in_fringe_p = true;
8174 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
8176 else
8178 switch (cursor_type)
8180 case HOLLOW_BOX_CURSOR:
8181 x_draw_hollow_cursor (w, glyph_row);
8182 break;
8184 case FILLED_BOX_CURSOR:
8185 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
8186 break;
8188 case BAR_CURSOR:
8189 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
8190 break;
8192 case HBAR_CURSOR:
8193 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
8194 break;
8196 case NO_CURSOR:
8197 w->phys_cursor_width = 0;
8198 break;
8200 default:
8201 emacs_abort ();
8205 #ifdef HAVE_X_I18N
8206 if (w == XWINDOW (f->selected_window))
8207 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
8208 xic_set_preeditarea (w, x, y);
8209 #endif
8212 XFlush (FRAME_X_DISPLAY (f));
8216 /* Icons. */
8218 /* Make the x-window of frame F use the gnu icon bitmap. */
8220 bool
8221 x_bitmap_icon (struct frame *f, Lisp_Object file)
8223 ptrdiff_t bitmap_id;
8225 if (FRAME_X_WINDOW (f) == 0)
8226 return true;
8228 /* Free up our existing icon bitmap and mask if any. */
8229 if (f->output_data.x->icon_bitmap > 0)
8230 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
8231 f->output_data.x->icon_bitmap = 0;
8233 if (STRINGP (file))
8235 #ifdef USE_GTK
8236 /* Use gtk_window_set_icon_from_file () if available,
8237 It's not restricted to bitmaps */
8238 if (xg_set_icon (f, file))
8239 return false;
8240 #endif /* USE_GTK */
8241 bitmap_id = x_create_bitmap_from_file (f, file);
8242 x_create_bitmap_mask (f, bitmap_id);
8244 else
8246 /* Create the GNU bitmap and mask if necessary. */
8247 if (FRAME_DISPLAY_INFO (f)->icon_bitmap_id < 0)
8249 ptrdiff_t rc = -1;
8251 #ifdef USE_GTK
8253 if (xg_set_icon (f, xg_default_icon_file)
8254 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
8256 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = -2;
8257 return false;
8260 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
8262 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
8263 if (rc != -1)
8264 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
8266 #endif
8268 /* If all else fails, use the (black and white) xbm image. */
8269 if (rc == -1)
8271 rc = x_create_bitmap_from_data (f, (char *) gnu_xbm_bits,
8272 gnu_xbm_width, gnu_xbm_height);
8273 if (rc == -1)
8274 return true;
8276 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
8277 x_create_bitmap_mask (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
8281 /* The first time we create the GNU bitmap and mask,
8282 this increments the ref-count one extra time.
8283 As a result, the GNU bitmap and mask are never freed.
8284 That way, we don't have to worry about allocating it again. */
8285 x_reference_bitmap (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
8287 bitmap_id = FRAME_DISPLAY_INFO (f)->icon_bitmap_id;
8290 x_wm_set_icon_pixmap (f, bitmap_id);
8291 f->output_data.x->icon_bitmap = bitmap_id;
8293 return false;
8297 /* Make the x-window of frame F use a rectangle with text.
8298 Use ICON_NAME as the text. */
8300 bool
8301 x_text_icon (struct frame *f, const char *icon_name)
8303 if (FRAME_X_WINDOW (f) == 0)
8304 return true;
8307 XTextProperty text;
8308 text.value = (unsigned char *) icon_name;
8309 text.encoding = XA_STRING;
8310 text.format = 8;
8311 text.nitems = strlen (icon_name);
8312 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
8315 if (f->output_data.x->icon_bitmap > 0)
8316 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
8317 f->output_data.x->icon_bitmap = 0;
8318 x_wm_set_icon_pixmap (f, 0);
8320 return false;
8323 #define X_ERROR_MESSAGE_SIZE 200
8325 /* If non-nil, this should be a string.
8326 It means catch X errors and store the error message in this string.
8328 The reason we use a stack is that x_catch_error/x_uncatch_error can
8329 be called from a signal handler.
8332 struct x_error_message_stack {
8333 char string[X_ERROR_MESSAGE_SIZE];
8334 Display *dpy;
8335 struct x_error_message_stack *prev;
8337 static struct x_error_message_stack *x_error_message;
8339 /* An X error handler which stores the error message in
8340 *x_error_message. This is called from x_error_handler if
8341 x_catch_errors is in effect. */
8343 static void
8344 x_error_catcher (Display *display, XErrorEvent *event)
8346 XGetErrorText (display, event->error_code,
8347 x_error_message->string,
8348 X_ERROR_MESSAGE_SIZE);
8351 /* Begin trapping X errors for display DPY. Actually we trap X errors
8352 for all displays, but DPY should be the display you are actually
8353 operating on.
8355 After calling this function, X protocol errors no longer cause
8356 Emacs to exit; instead, they are recorded in the string
8357 stored in *x_error_message.
8359 Calling x_check_errors signals an Emacs error if an X error has
8360 occurred since the last call to x_catch_errors or x_check_errors.
8362 Calling x_uncatch_errors resumes the normal error handling. */
8364 void
8365 x_catch_errors (Display *dpy)
8367 struct x_error_message_stack *data = xmalloc (sizeof *data);
8369 /* Make sure any errors from previous requests have been dealt with. */
8370 XSync (dpy, False);
8372 data->dpy = dpy;
8373 data->string[0] = 0;
8374 data->prev = x_error_message;
8375 x_error_message = data;
8378 /* Undo the last x_catch_errors call.
8379 DPY should be the display that was passed to x_catch_errors. */
8381 void
8382 x_uncatch_errors (void)
8384 struct x_error_message_stack *tmp;
8386 block_input ();
8388 /* The display may have been closed before this function is called.
8389 Check if it is still open before calling XSync. */
8390 if (x_display_info_for_display (x_error_message->dpy) != 0)
8391 XSync (x_error_message->dpy, False);
8393 tmp = x_error_message;
8394 x_error_message = x_error_message->prev;
8395 xfree (tmp);
8396 unblock_input ();
8399 /* If any X protocol errors have arrived since the last call to
8400 x_catch_errors or x_check_errors, signal an Emacs error using
8401 sprintf (a buffer, FORMAT, the x error message text) as the text. */
8403 void
8404 x_check_errors (Display *dpy, const char *format)
8406 /* Make sure to catch any errors incurred so far. */
8407 XSync (dpy, False);
8409 if (x_error_message->string[0])
8411 char string[X_ERROR_MESSAGE_SIZE];
8412 memcpy (string, x_error_message->string, X_ERROR_MESSAGE_SIZE);
8413 x_uncatch_errors ();
8414 error (format, string);
8418 /* Nonzero if we had any X protocol errors
8419 since we did x_catch_errors on DPY. */
8421 bool
8422 x_had_errors_p (Display *dpy)
8424 /* Make sure to catch any errors incurred so far. */
8425 XSync (dpy, False);
8427 return x_error_message->string[0] != 0;
8430 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
8432 void
8433 x_clear_errors (Display *dpy)
8435 x_error_message->string[0] = 0;
8438 #if false
8439 /* See comment in unwind_to_catch why calling this is a bad
8440 * idea. --lorentey */
8441 /* Close off all unclosed x_catch_errors calls. */
8443 void
8444 x_fully_uncatch_errors (void)
8446 while (x_error_message)
8447 x_uncatch_errors ();
8449 #endif
8451 #if false
8452 static unsigned int x_wire_count;
8453 x_trace_wire (void)
8455 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
8457 #endif
8460 /************************************************************************
8461 Handling X errors
8462 ************************************************************************/
8464 /* Error message passed to x_connection_closed. */
8466 static char *error_msg;
8468 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
8469 the text of an error message that lead to the connection loss. */
8471 static void
8472 x_connection_closed (Display *dpy, const char *error_message)
8474 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
8475 Lisp_Object frame, tail;
8476 ptrdiff_t idx = SPECPDL_INDEX ();
8478 error_msg = alloca (strlen (error_message) + 1);
8479 strcpy (error_msg, error_message);
8481 /* Inhibit redisplay while frames are being deleted. */
8482 specbind (Qinhibit_redisplay, Qt);
8484 if (dpyinfo)
8486 /* Protect display from being closed when we delete the last
8487 frame on it. */
8488 dpyinfo->reference_count++;
8489 dpyinfo->terminal->reference_count++;
8492 /* First delete frames whose mini-buffers are on frames
8493 that are on the dead display. */
8494 FOR_EACH_FRAME (tail, frame)
8496 Lisp_Object minibuf_frame;
8497 minibuf_frame
8498 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
8499 if (FRAME_X_P (XFRAME (frame))
8500 && FRAME_X_P (XFRAME (minibuf_frame))
8501 && ! EQ (frame, minibuf_frame)
8502 && FRAME_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
8503 delete_frame (frame, Qnoelisp);
8506 /* Now delete all remaining frames on the dead display.
8507 We are now sure none of these is used as the mini-buffer
8508 for another frame that we need to delete. */
8509 FOR_EACH_FRAME (tail, frame)
8510 if (FRAME_X_P (XFRAME (frame))
8511 && FRAME_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
8513 /* Set this to t so that delete_frame won't get confused
8514 trying to find a replacement. */
8515 kset_default_minibuffer_frame (FRAME_KBOARD (XFRAME (frame)), Qt);
8516 delete_frame (frame, Qnoelisp);
8519 /* If DPYINFO is null, this means we didn't open the display in the
8520 first place, so don't try to close it. */
8521 if (dpyinfo)
8523 /* We can not call XtCloseDisplay here because it calls XSync.
8524 XSync inside the error handler apparently hangs Emacs. On
8525 current Xt versions, this isn't needed either. */
8526 #ifdef USE_GTK
8527 /* A long-standing GTK bug prevents proper disconnect handling
8528 (https://bugzilla.gnome.org/show_bug.cgi?id=85715). Once,
8529 the resulting Glib error message loop filled a user's disk.
8530 To avoid this, kill Emacs unconditionally on disconnect. */
8531 shut_down_emacs (0, Qnil);
8532 fprintf (stderr, "%s\n\
8533 When compiled with GTK, Emacs cannot recover from X disconnects.\n\
8534 This is a GTK bug: https://bugzilla.gnome.org/show_bug.cgi?id=85715\n\
8535 For details, see etc/PROBLEMS.\n",
8536 error_msg);
8537 emacs_abort ();
8538 #endif /* USE_GTK */
8540 /* Indicate that this display is dead. */
8541 dpyinfo->display = 0;
8543 dpyinfo->reference_count--;
8544 dpyinfo->terminal->reference_count--;
8545 if (dpyinfo->reference_count != 0)
8546 /* We have just closed all frames on this display. */
8547 emacs_abort ();
8550 Lisp_Object tmp;
8551 XSETTERMINAL (tmp, dpyinfo->terminal);
8552 Fdelete_terminal (tmp, Qnoelisp);
8556 if (terminal_list == 0)
8558 fprintf (stderr, "%s\n", error_msg);
8559 Fkill_emacs (make_number (70));
8560 /* NOTREACHED */
8563 totally_unblock_input ();
8565 unbind_to (idx, Qnil);
8566 clear_waiting_for_input ();
8568 /* Tell GCC not to suggest attribute 'noreturn' for this function. */
8569 IF_LINT (if (! terminal_list) return; )
8571 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
8572 longjmp), because returning from this function would get us back into
8573 Xlib's code which will directly call `exit'. */
8574 error ("%s", error_msg);
8577 /* We specifically use it before defining it, so that gcc doesn't inline it,
8578 otherwise gdb doesn't know how to properly put a breakpoint on it. */
8579 static void x_error_quitter (Display *, XErrorEvent *);
8581 /* This is the first-level handler for X protocol errors.
8582 It calls x_error_quitter or x_error_catcher. */
8584 static int
8585 x_error_handler (Display *display, XErrorEvent *event)
8587 #if defined USE_GTK && defined HAVE_GTK3
8588 if ((event->error_code == BadMatch || event->error_code == BadWindow)
8589 && event->request_code == X_SetInputFocus)
8591 return 0;
8593 #endif
8595 if (x_error_message)
8596 x_error_catcher (display, event);
8597 else
8598 x_error_quitter (display, event);
8599 return 0;
8602 /* This is the usual handler for X protocol errors.
8603 It kills all frames on the display that we got the error for.
8604 If that was the only one, it prints an error message and kills Emacs. */
8606 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
8608 /* On older GCC versions, just putting x_error_quitter
8609 after x_error_handler prevents inlining into the former. */
8611 static void NO_INLINE
8612 x_error_quitter (Display *display, XErrorEvent *event)
8614 char buf[256], buf1[356];
8616 /* Ignore BadName errors. They can happen because of fonts
8617 or colors that are not defined. */
8619 if (event->error_code == BadName)
8620 return;
8622 /* Note that there is no real way portable across R3/R4 to get the
8623 original error handler. */
8625 XGetErrorText (display, event->error_code, buf, sizeof (buf));
8626 sprintf (buf1, "X protocol error: %s on protocol request %d",
8627 buf, event->request_code);
8628 x_connection_closed (display, buf1);
8632 /* This is the handler for X IO errors, always.
8633 It kills all frames on the display that we lost touch with.
8634 If that was the only one, it prints an error message and kills Emacs. */
8636 static int
8637 x_io_error_quitter (Display *display)
8639 char buf[256];
8641 snprintf (buf, sizeof buf, "Connection lost to X server `%s'",
8642 DisplayString (display));
8643 x_connection_closed (display, buf);
8644 return 0;
8647 /* Changing the font of the frame. */
8649 /* Give frame F the font FONT-OBJECT as its default font. The return
8650 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
8651 frame. If it is negative, generate a new fontset from
8652 FONT-OBJECT. */
8654 Lisp_Object
8655 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
8657 struct font *font = XFONT_OBJECT (font_object);
8658 int unit;
8660 if (fontset < 0)
8661 fontset = fontset_from_font (font_object);
8662 FRAME_FONTSET (f) = fontset;
8663 if (FRAME_FONT (f) == font)
8664 /* This font is already set in frame F. There's nothing more to
8665 do. */
8666 return font_object;
8668 FRAME_FONT (f) = font;
8669 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
8670 FRAME_COLUMN_WIDTH (f) = font->average_width;
8671 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (font);
8673 #ifndef USE_X_TOOLKIT
8674 FRAME_MENU_BAR_HEIGHT (f) = FRAME_MENU_BAR_LINES (f) * FRAME_LINE_HEIGHT (f);
8675 #endif
8677 /* Compute character columns occupied by scrollbar.
8679 Don't do things differently for non-toolkit scrollbars
8680 (Bug#17163). */
8681 unit = FRAME_COLUMN_WIDTH (f);
8682 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8683 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8684 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + unit - 1) / unit;
8685 else
8686 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
8688 if (FRAME_X_WINDOW (f) != 0)
8690 /* Don't change the size of a tip frame; there's no point in
8691 doing it because it's done in Fx_show_tip, and it leads to
8692 problems because the tip frame has no widget. */
8693 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8694 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
8695 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 3,
8696 false, Qfont);
8699 #ifdef HAVE_X_I18N
8700 if (FRAME_XIC (f)
8701 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8703 block_input ();
8704 xic_set_xfontset (f, SSDATA (fontset_ascii (fontset)));
8705 unblock_input ();
8707 #endif
8709 return font_object;
8713 /***********************************************************************
8714 X Input Methods
8715 ***********************************************************************/
8717 #ifdef HAVE_X_I18N
8719 #ifdef HAVE_X11R6
8721 /* XIM destroy callback function, which is called whenever the
8722 connection to input method XIM dies. CLIENT_DATA contains a
8723 pointer to the x_display_info structure corresponding to XIM. */
8725 static void
8726 xim_destroy_callback (XIM xim, XPointer client_data, XPointer call_data)
8728 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8729 Lisp_Object frame, tail;
8731 block_input ();
8733 /* No need to call XDestroyIC.. */
8734 FOR_EACH_FRAME (tail, frame)
8736 struct frame *f = XFRAME (frame);
8737 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
8739 FRAME_XIC (f) = NULL;
8740 xic_free_xfontset (f);
8744 /* No need to call XCloseIM. */
8745 dpyinfo->xim = NULL;
8746 XFree (dpyinfo->xim_styles);
8747 unblock_input ();
8750 #endif /* HAVE_X11R6 */
8752 /* Open the connection to the XIM server on display DPYINFO.
8753 RESOURCE_NAME is the resource name Emacs uses. */
8755 static void
8756 xim_open_dpy (struct x_display_info *dpyinfo, char *resource_name)
8758 XIM xim;
8760 #ifdef HAVE_XIM
8761 if (use_xim)
8763 if (dpyinfo->xim)
8764 XCloseIM (dpyinfo->xim);
8765 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8766 emacs_class);
8767 dpyinfo->xim = xim;
8769 if (xim)
8771 #ifdef HAVE_X11R6
8772 XIMCallback destroy;
8773 #endif
8775 /* Get supported styles and XIM values. */
8776 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8778 #ifdef HAVE_X11R6
8779 destroy.callback = xim_destroy_callback;
8780 destroy.client_data = (XPointer)dpyinfo;
8781 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8782 #endif
8786 else
8787 #endif /* HAVE_XIM */
8788 dpyinfo->xim = NULL;
8792 #ifdef HAVE_X11R6_XIM
8794 /* XIM instantiate callback function, which is called whenever an XIM
8795 server is available. DISPLAY is the display of the XIM.
8796 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8797 when the callback was registered. */
8799 static void
8800 xim_instantiate_callback (Display *display, XPointer client_data, XPointer call_data)
8802 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8803 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8805 /* We don't support multiple XIM connections. */
8806 if (dpyinfo->xim)
8807 return;
8809 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8811 /* Create XIC for the existing frames on the same display, as long
8812 as they have no XIC. */
8813 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8815 Lisp_Object tail, frame;
8817 block_input ();
8818 FOR_EACH_FRAME (tail, frame)
8820 struct frame *f = XFRAME (frame);
8822 if (FRAME_X_P (f)
8823 && FRAME_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8824 if (FRAME_XIC (f) == NULL)
8826 create_frame_xic (f);
8827 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8828 xic_set_statusarea (f);
8829 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8831 struct window *w = XWINDOW (f->selected_window);
8832 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8837 unblock_input ();
8841 #endif /* HAVE_X11R6_XIM */
8844 /* Open a connection to the XIM server on display DPYINFO.
8845 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8846 connection only at the first time. On X11R6, open the connection
8847 in the XIM instantiate callback function. */
8849 static void
8850 xim_initialize (struct x_display_info *dpyinfo, char *resource_name)
8852 dpyinfo->xim = NULL;
8853 #ifdef HAVE_XIM
8854 if (use_xim)
8856 #ifdef HAVE_X11R6_XIM
8857 struct xim_inst_t *xim_inst = xmalloc (sizeof *xim_inst);
8858 Bool ret;
8860 dpyinfo->xim_callback_data = xim_inst;
8861 xim_inst->dpyinfo = dpyinfo;
8862 xim_inst->resource_name = xstrdup (resource_name);
8863 ret = XRegisterIMInstantiateCallback
8864 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
8865 emacs_class, xim_instantiate_callback,
8866 /* This is XPointer in XFree86 but (XPointer *)
8867 on Tru64, at least, hence the configure test. */
8868 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8869 eassert (ret == True);
8870 #else /* not HAVE_X11R6_XIM */
8871 xim_open_dpy (dpyinfo, resource_name);
8872 #endif /* not HAVE_X11R6_XIM */
8874 #endif /* HAVE_XIM */
8878 /* Close the connection to the XIM server on display DPYINFO. */
8880 static void
8881 xim_close_dpy (struct x_display_info *dpyinfo)
8883 #ifdef HAVE_XIM
8884 if (use_xim)
8886 #ifdef HAVE_X11R6_XIM
8887 struct xim_inst_t *xim_inst = dpyinfo->xim_callback_data;
8889 if (dpyinfo->display)
8891 Bool ret = XUnregisterIMInstantiateCallback
8892 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
8893 emacs_class, xim_instantiate_callback,
8894 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8895 eassert (ret == True);
8897 xfree (xim_inst->resource_name);
8898 xfree (xim_inst);
8899 #endif /* HAVE_X11R6_XIM */
8900 if (dpyinfo->display)
8901 XCloseIM (dpyinfo->xim);
8902 dpyinfo->xim = NULL;
8903 XFree (dpyinfo->xim_styles);
8905 #endif /* HAVE_XIM */
8908 #endif /* not HAVE_X11R6_XIM */
8912 /* Calculate the absolute position in frame F
8913 from its current recorded position values and gravity. */
8915 static void
8916 x_calc_absolute_position (struct frame *f)
8918 int flags = f->size_hint_flags;
8920 /* We have nothing to do if the current position
8921 is already for the top-left corner. */
8922 if (! ((flags & XNegative) || (flags & YNegative)))
8923 return;
8925 /* Treat negative positions as relative to the leftmost bottommost
8926 position that fits on the screen. */
8927 if (flags & XNegative)
8928 f->left_pos = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
8929 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
8932 int height = FRAME_PIXEL_HEIGHT (f);
8934 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8935 /* Something is fishy here. When using Motif, starting Emacs with
8936 `-g -0-0', the frame appears too low by a few pixels.
8938 This seems to be so because initially, while Emacs is starting,
8939 the column widget's height and the frame's pixel height are
8940 different. The column widget's height is the right one. In
8941 later invocations, when Emacs is up, the frame's pixel height
8942 is right, though.
8944 It's not obvious where the initial small difference comes from.
8945 2000-12-01, gerd. */
8947 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8948 #endif
8950 if (flags & YNegative)
8951 f->top_pos = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
8952 - height + f->top_pos;
8955 /* The left_pos and top_pos
8956 are now relative to the top and left screen edges,
8957 so the flags should correspond. */
8958 f->size_hint_flags &= ~ (XNegative | YNegative);
8961 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8962 to really change the position, and 0 when calling from
8963 x_make_frame_visible (in that case, XOFF and YOFF are the current
8964 position values). It is -1 when calling from x_set_frame_parameters,
8965 which means, do adjust for borders but don't change the gravity. */
8967 void
8968 x_set_offset (struct frame *f, register int xoff, register int yoff, int change_gravity)
8970 int modified_top, modified_left;
8972 if (change_gravity > 0)
8974 f->top_pos = yoff;
8975 f->left_pos = xoff;
8976 f->size_hint_flags &= ~ (XNegative | YNegative);
8977 if (xoff < 0)
8978 f->size_hint_flags |= XNegative;
8979 if (yoff < 0)
8980 f->size_hint_flags |= YNegative;
8981 f->win_gravity = NorthWestGravity;
8983 x_calc_absolute_position (f);
8985 block_input ();
8986 x_wm_set_size_hint (f, 0, false);
8988 modified_left = f->left_pos;
8989 modified_top = f->top_pos;
8991 if (change_gravity != 0 && FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8993 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8994 than the WM decorations. So we use the calculated offset instead
8995 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8996 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8997 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
9000 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9001 modified_left, modified_top);
9003 x_sync_with_move (f, f->left_pos, f->top_pos,
9004 FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN);
9006 /* change_gravity is non-zero when this function is called from Lisp to
9007 programmatically move a frame. In that case, we call
9008 x_check_expected_move to discover if we have a "Type A" or "Type B"
9009 window manager, and, for a "Type A" window manager, adjust the position
9010 of the frame.
9012 We call x_check_expected_move if a programmatic move occurred, and
9013 either the window manager type (A/B) is unknown or it is Type A but we
9014 need to compute the top/left offset adjustment for this frame. */
9016 if (change_gravity != 0
9017 && (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
9018 || (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
9019 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
9020 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
9021 x_check_expected_move (f, modified_left, modified_top);
9023 unblock_input ();
9026 /* Return true if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
9027 on the root window for frame F contains ATOMNAME.
9028 This is how a WM check shall be done according to the Window Manager
9029 Specification/Extended Window Manager Hints at
9030 http://freedesktop.org/wiki/Specifications/wm-spec. */
9032 static bool
9033 wm_supports (struct frame *f, Atom want_atom)
9035 Atom actual_type;
9036 unsigned long actual_size, bytes_remaining;
9037 int i, rc, actual_format;
9038 bool ret;
9039 Window wmcheck_window;
9040 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9041 Window target_window = dpyinfo->root_window;
9042 int max_len = 65536;
9043 Display *dpy = FRAME_X_DISPLAY (f);
9044 unsigned char *tmp_data = NULL;
9045 Atom target_type = XA_WINDOW;
9047 block_input ();
9049 x_catch_errors (dpy);
9050 rc = XGetWindowProperty (dpy, target_window,
9051 dpyinfo->Xatom_net_supporting_wm_check,
9052 0, max_len, False, target_type,
9053 &actual_type, &actual_format, &actual_size,
9054 &bytes_remaining, &tmp_data);
9056 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
9058 if (tmp_data) XFree (tmp_data);
9059 x_uncatch_errors ();
9060 unblock_input ();
9061 return false;
9064 wmcheck_window = *(Window *) tmp_data;
9065 XFree (tmp_data);
9067 /* Check if window exists. */
9068 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
9069 x_sync (f);
9070 if (x_had_errors_p (dpy))
9072 x_uncatch_errors ();
9073 unblock_input ();
9074 return false;
9077 if (dpyinfo->net_supported_window != wmcheck_window)
9079 /* Window changed, reload atoms */
9080 if (dpyinfo->net_supported_atoms != NULL)
9081 XFree (dpyinfo->net_supported_atoms);
9082 dpyinfo->net_supported_atoms = NULL;
9083 dpyinfo->nr_net_supported_atoms = 0;
9084 dpyinfo->net_supported_window = 0;
9086 target_type = XA_ATOM;
9087 tmp_data = NULL;
9088 rc = XGetWindowProperty (dpy, target_window,
9089 dpyinfo->Xatom_net_supported,
9090 0, max_len, False, target_type,
9091 &actual_type, &actual_format, &actual_size,
9092 &bytes_remaining, &tmp_data);
9094 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
9096 if (tmp_data) XFree (tmp_data);
9097 x_uncatch_errors ();
9098 unblock_input ();
9099 return false;
9102 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
9103 dpyinfo->nr_net_supported_atoms = actual_size;
9104 dpyinfo->net_supported_window = wmcheck_window;
9107 ret = false;
9109 for (i = 0; !ret && i < dpyinfo->nr_net_supported_atoms; ++i)
9110 ret = dpyinfo->net_supported_atoms[i] == want_atom;
9112 x_uncatch_errors ();
9113 unblock_input ();
9115 return ret;
9118 static void
9119 set_wm_state (Lisp_Object frame, bool add, Atom atom, Atom value)
9121 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (XFRAME (frame));
9123 x_send_client_event (frame, make_number (0), frame,
9124 dpyinfo->Xatom_net_wm_state,
9125 make_number (32),
9126 /* 1 = add, 0 = remove */
9127 Fcons
9128 (make_number (add),
9129 Fcons
9130 (make_fixnum_or_float (atom),
9131 (value != 0
9132 ? list1 (make_fixnum_or_float (value))
9133 : Qnil))));
9136 void
9137 x_set_sticky (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
9139 Lisp_Object frame;
9140 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9142 XSETFRAME (frame, f);
9144 set_wm_state (frame, !NILP (new_value),
9145 dpyinfo->Xatom_net_wm_state_sticky, None);
9148 /* Return the current _NET_WM_STATE.
9149 SIZE_STATE is set to one of the FULLSCREEN_* values.
9150 Set *STICKY to the sticky state.
9152 Return true iff we are not hidden. */
9154 static bool
9155 get_current_wm_state (struct frame *f,
9156 Window window,
9157 int *size_state,
9158 bool *sticky)
9160 Atom actual_type;
9161 unsigned long actual_size, bytes_remaining;
9162 int i, rc, actual_format;
9163 bool is_hidden = false;
9164 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9165 long max_len = 65536;
9166 Display *dpy = FRAME_X_DISPLAY (f);
9167 unsigned char *tmp_data = NULL;
9168 Atom target_type = XA_ATOM;
9170 *sticky = false;
9171 *size_state = FULLSCREEN_NONE;
9173 block_input ();
9174 x_catch_errors (dpy);
9175 rc = XGetWindowProperty (dpy, window, dpyinfo->Xatom_net_wm_state,
9176 0, max_len, False, target_type,
9177 &actual_type, &actual_format, &actual_size,
9178 &bytes_remaining, &tmp_data);
9180 if (rc != Success || actual_type != target_type || x_had_errors_p (dpy))
9182 if (tmp_data) XFree (tmp_data);
9183 x_uncatch_errors ();
9184 unblock_input ();
9185 return !FRAME_ICONIFIED_P (f);
9188 x_uncatch_errors ();
9190 for (i = 0; i < actual_size; ++i)
9192 Atom a = ((Atom*)tmp_data)[i];
9193 if (a == dpyinfo->Xatom_net_wm_state_hidden)
9194 is_hidden = true;
9195 else if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
9197 if (*size_state == FULLSCREEN_HEIGHT)
9198 *size_state = FULLSCREEN_MAXIMIZED;
9199 else
9200 *size_state = FULLSCREEN_WIDTH;
9202 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
9204 if (*size_state == FULLSCREEN_WIDTH)
9205 *size_state = FULLSCREEN_MAXIMIZED;
9206 else
9207 *size_state = FULLSCREEN_HEIGHT;
9209 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen)
9210 *size_state = FULLSCREEN_BOTH;
9211 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
9212 *sticky = true;
9215 if (tmp_data) XFree (tmp_data);
9216 unblock_input ();
9217 return ! is_hidden;
9220 /* Do fullscreen as specified in extended window manager hints */
9222 static bool
9223 do_ewmh_fullscreen (struct frame *f)
9225 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9226 bool have_net_atom = wm_supports (f, dpyinfo->Xatom_net_wm_state);
9227 int cur;
9228 bool dummy;
9230 get_current_wm_state (f, FRAME_OUTER_WINDOW (f), &cur, &dummy);
9232 /* Some window managers don't say they support _NET_WM_STATE, but they do say
9233 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
9234 if (!have_net_atom)
9235 have_net_atom = wm_supports (f, dpyinfo->Xatom_net_wm_state_fullscreen);
9237 if (have_net_atom && cur != f->want_fullscreen)
9239 Lisp_Object frame;
9241 XSETFRAME (frame, f);
9243 /* Keep number of calls to set_wm_state as low as possible.
9244 Some window managers, or possible Gtk+, hangs when too many
9245 are sent at once. */
9246 switch (f->want_fullscreen)
9248 case FULLSCREEN_BOTH:
9249 if (cur != FULLSCREEN_BOTH)
9250 set_wm_state (frame, true, dpyinfo->Xatom_net_wm_state_fullscreen,
9251 None);
9252 break;
9253 case FULLSCREEN_WIDTH:
9254 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_HEIGHT
9255 || cur == FULLSCREEN_MAXIMIZED)
9256 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
9257 dpyinfo->Xatom_net_wm_state_maximized_vert);
9258 if (cur != FULLSCREEN_MAXIMIZED)
9259 set_wm_state (frame, true,
9260 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
9261 break;
9262 case FULLSCREEN_HEIGHT:
9263 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_WIDTH
9264 || cur == FULLSCREEN_MAXIMIZED)
9265 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
9266 dpyinfo->Xatom_net_wm_state_maximized_horz);
9267 if (cur != FULLSCREEN_MAXIMIZED)
9268 set_wm_state (frame, true,
9269 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
9270 break;
9271 case FULLSCREEN_MAXIMIZED:
9272 if (cur == FULLSCREEN_BOTH)
9273 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
9274 None);
9275 set_wm_state (frame, true,
9276 dpyinfo->Xatom_net_wm_state_maximized_horz,
9277 dpyinfo->Xatom_net_wm_state_maximized_vert);
9278 break;
9279 case FULLSCREEN_NONE:
9280 if (cur == FULLSCREEN_BOTH)
9281 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
9282 None);
9283 else
9284 set_wm_state (frame, false,
9285 dpyinfo->Xatom_net_wm_state_maximized_horz,
9286 dpyinfo->Xatom_net_wm_state_maximized_vert);
9289 f->want_fullscreen = FULLSCREEN_NONE;
9293 return have_net_atom;
9296 static void
9297 XTfullscreen_hook (struct frame *f)
9299 if (FRAME_VISIBLE_P (f))
9301 block_input ();
9302 x_check_fullscreen (f);
9303 x_sync (f);
9304 unblock_input ();
9309 static bool
9310 x_handle_net_wm_state (struct frame *f, const XPropertyEvent *event)
9312 int value = FULLSCREEN_NONE;
9313 Lisp_Object lval;
9314 bool sticky = false;
9315 bool not_hidden = get_current_wm_state (f, event->window, &value, &sticky);
9317 lval = Qnil;
9318 switch (value)
9320 case FULLSCREEN_WIDTH:
9321 lval = Qfullwidth;
9322 break;
9323 case FULLSCREEN_HEIGHT:
9324 lval = Qfullheight;
9325 break;
9326 case FULLSCREEN_BOTH:
9327 lval = Qfullboth;
9328 break;
9329 case FULLSCREEN_MAXIMIZED:
9330 lval = Qmaximized;
9331 break;
9334 store_frame_param (f, Qfullscreen, lval);
9335 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
9337 return not_hidden;
9340 /* Check if we need to resize the frame due to a fullscreen request.
9341 If so needed, resize the frame. */
9342 static void
9343 x_check_fullscreen (struct frame *f)
9345 if (do_ewmh_fullscreen (f))
9346 return;
9348 if (f->output_data.x->parent_desc != FRAME_DISPLAY_INFO (f)->root_window)
9349 return; /* Only fullscreen without WM or with EWM hints (above). */
9351 /* Setting fullscreen to nil doesn't do anything. We could save the
9352 last non-fullscreen size and restore it, but it seems like a
9353 lot of work for this unusual case (no window manager running). */
9355 if (f->want_fullscreen != FULLSCREEN_NONE)
9357 int width = FRAME_PIXEL_WIDTH (f), height = FRAME_PIXEL_HEIGHT (f);
9358 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9360 switch (f->want_fullscreen)
9362 /* No difference between these two when there is no WM */
9363 case FULLSCREEN_BOTH:
9364 case FULLSCREEN_MAXIMIZED:
9365 width = x_display_pixel_width (dpyinfo);
9366 height = x_display_pixel_height (dpyinfo);
9367 break;
9368 case FULLSCREEN_WIDTH:
9369 width = x_display_pixel_width (dpyinfo);
9370 break;
9371 case FULLSCREEN_HEIGHT:
9372 height = x_display_pixel_height (dpyinfo);
9375 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9376 width, height);
9380 /* This function is called by x_set_offset to determine whether the window
9381 manager interfered with the positioning of the frame. Type A window
9382 managers position the surrounding window manager decorations a small
9383 amount above and left of the user-supplied position. Type B window
9384 managers position the surrounding window manager decorations at the
9385 user-specified position. If we detect a Type A window manager, we
9386 compensate by moving the window right and down by the proper amount. */
9388 static void
9389 x_check_expected_move (struct frame *f, int expected_left, int expected_top)
9391 int current_left = 0, current_top = 0;
9393 /* x_real_positions returns the left and top offsets of the outermost
9394 window manager window around the frame. */
9396 x_real_positions (f, &current_left, &current_top);
9398 if (current_left != expected_left || current_top != expected_top)
9400 /* It's a "Type A" window manager. */
9402 int adjusted_left;
9403 int adjusted_top;
9405 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
9406 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
9407 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
9409 /* Now fix the mispositioned frame's location. */
9411 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
9412 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
9414 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9415 adjusted_left, adjusted_top);
9417 x_sync_with_move (f, expected_left, expected_top, false);
9419 else
9420 /* It's a "Type B" window manager. We don't have to adjust the
9421 frame's position. */
9423 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
9427 /* Wait for XGetGeometry to return up-to-date position information for a
9428 recently-moved frame. Call this immediately after calling XMoveWindow.
9429 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
9430 frame has been moved to, so we use a fuzzy position comparison instead
9431 of an exact comparison. */
9433 static void
9434 x_sync_with_move (struct frame *f, int left, int top, bool fuzzy)
9436 int count = 0;
9438 while (count++ < 50)
9440 int current_left = 0, current_top = 0;
9442 /* In theory, this call to XSync only needs to happen once, but in
9443 practice, it doesn't seem to work, hence the need for the surrounding
9444 loop. */
9446 XSync (FRAME_X_DISPLAY (f), False);
9447 x_real_positions (f, &current_left, &current_top);
9449 if (fuzzy)
9451 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
9452 pixels. */
9454 if (eabs (current_left - left) <= 10
9455 && eabs (current_top - top) <= 40)
9456 return;
9458 else if (current_left == left && current_top == top)
9459 return;
9462 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
9463 will then return up-to-date position info. */
9465 wait_reading_process_output (0, 500000000, 0, false, Qnil, NULL, 0);
9469 /* Wait for an event on frame F matching EVENTTYPE. */
9470 void
9471 x_wait_for_event (struct frame *f, int eventtype)
9473 int level = interrupt_input_blocked;
9475 fd_set fds;
9476 struct timespec tmo, tmo_at, time_now;
9477 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
9479 f->wait_event_type = eventtype;
9481 /* Set timeout to 0.1 second. Hopefully not noticeable.
9482 Maybe it should be configurable. */
9483 tmo = make_timespec (0, 100 * 1000 * 1000);
9484 tmo_at = timespec_add (current_timespec (), tmo);
9486 while (f->wait_event_type)
9488 pending_signals = true;
9489 totally_unblock_input ();
9490 /* XTread_socket is called after unblock. */
9491 block_input ();
9492 interrupt_input_blocked = level;
9494 FD_ZERO (&fds);
9495 FD_SET (fd, &fds);
9497 time_now = current_timespec ();
9498 if (timespec_cmp (tmo_at, time_now) < 0)
9499 break;
9501 tmo = timespec_sub (tmo_at, time_now);
9502 if (pselect (fd + 1, &fds, NULL, NULL, &tmo, NULL) == 0)
9503 break; /* Timeout */
9506 f->wait_event_type = 0;
9510 /* Change the size of frame F's X window to WIDTH/HEIGHT in the case F
9511 doesn't have a widget. If CHANGE_GRAVITY, change to
9512 top-left-corner window gravity for this size change and subsequent
9513 size changes. Otherwise leave the window gravity unchanged. */
9515 static void
9516 x_set_window_size_1 (struct frame *f, bool change_gravity,
9517 int width, int height, bool pixelwise)
9519 int pixelwidth, pixelheight;
9521 pixelwidth = (pixelwise
9522 ? FRAME_TEXT_TO_PIXEL_WIDTH (f, width)
9523 : FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, width));
9524 pixelheight = ((pixelwise
9525 ? FRAME_TEXT_TO_PIXEL_HEIGHT (f, height)
9526 : FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, height)));
9528 if (change_gravity) f->win_gravity = NorthWestGravity;
9529 x_wm_set_size_hint (f, 0, false);
9530 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9531 pixelwidth, pixelheight + FRAME_MENUBAR_HEIGHT (f));
9534 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
9535 receive in the ConfigureNotify event; if we get what we asked
9536 for, then the event won't cause the screen to become garbaged, so
9537 we have to make sure to do it here. */
9538 SET_FRAME_GARBAGED (f);
9540 /* Now, strictly speaking, we can't be sure that this is accurate,
9541 but the window manager will get around to dealing with the size
9542 change request eventually, and we'll hear how it went when the
9543 ConfigureNotify event gets here.
9545 We could just not bother storing any of this information here,
9546 and let the ConfigureNotify event set everything up, but that
9547 might be kind of confusing to the Lisp code, since size changes
9548 wouldn't be reported in the frame parameters until some random
9549 point in the future when the ConfigureNotify event arrives.
9551 Pass true for DELAY since we can't run Lisp code inside of
9552 a BLOCK_INPUT. */
9554 /* But the ConfigureNotify may in fact never arrive, and then this is
9555 not right if the frame is visible. Instead wait (with timeout)
9556 for the ConfigureNotify. */
9557 if (FRAME_VISIBLE_P (f))
9558 x_wait_for_event (f, ConfigureNotify);
9559 else
9561 change_frame_size (f, width, height, false, true, false, true);
9562 x_sync (f);
9567 /* Call this to change the size of frame F's x-window.
9568 If CHANGE_GRAVITY, change to top-left-corner window gravity
9569 for this size change and subsequent size changes.
9570 Otherwise we leave the window gravity unchanged. */
9572 void
9573 x_set_window_size (struct frame *f, bool change_gravity,
9574 int width, int height, bool pixelwise)
9576 block_input ();
9578 /* The following breaks our calculations. If it's really needed,
9579 think of something else. */
9580 #if false
9581 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
9583 int text_width, text_height;
9585 /* When the frame is maximized/fullscreen or running under for
9586 example Xmonad, x_set_window_size_1 will be a no-op.
9587 In that case, the right thing to do is extend rows/width to
9588 the current frame size. We do that first if x_set_window_size_1
9589 turns out to not be a no-op (there is no way to know).
9590 The size will be adjusted again if the frame gets a
9591 ConfigureNotify event as a result of x_set_window_size. */
9592 int pixelh = FRAME_PIXEL_HEIGHT (f);
9593 #ifdef USE_X_TOOLKIT
9594 /* The menu bar is not part of text lines. The tool bar
9595 is however. */
9596 pixelh -= FRAME_MENUBAR_HEIGHT (f);
9597 #endif
9598 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, FRAME_PIXEL_WIDTH (f));
9599 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelh);
9601 change_frame_size (f, text_width, text_height, false, true, false, true);
9603 #endif
9605 #ifdef USE_GTK
9606 if (FRAME_GTK_WIDGET (f))
9607 if (! pixelwise)
9608 xg_frame_set_char_size (f, width * FRAME_COLUMN_WIDTH (f),
9609 height * FRAME_LINE_HEIGHT (f));
9610 else
9611 xg_frame_set_char_size (f, width, height);
9612 else
9613 x_set_window_size_1 (f, change_gravity, width, height, pixelwise);
9614 #else /* not USE_GTK */
9616 x_set_window_size_1 (f, change_gravity, width, height, pixelwise);
9617 x_clear_under_internal_border (f);
9619 #endif /* not USE_GTK */
9621 /* If cursor was outside the new size, mark it as off. */
9622 mark_window_cursors_off (XWINDOW (f->root_window));
9624 /* Clear out any recollection of where the mouse highlighting was,
9625 since it might be in a place that's outside the new frame size.
9626 Actually checking whether it is outside is a pain in the neck,
9627 so don't try--just let the highlighting be done afresh with new size. */
9628 cancel_mouse_face (f);
9630 unblock_input ();
9632 do_pending_window_change (false);
9635 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
9637 void
9638 frame_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
9640 block_input ();
9642 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9643 0, 0, 0, 0, pix_x, pix_y);
9644 unblock_input ();
9647 /* Raise frame F. */
9649 void
9650 x_raise_frame (struct frame *f)
9652 block_input ();
9653 if (FRAME_VISIBLE_P (f))
9654 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9655 XFlush (FRAME_X_DISPLAY (f));
9656 unblock_input ();
9659 /* Lower frame F. */
9661 static void
9662 x_lower_frame (struct frame *f)
9664 if (FRAME_VISIBLE_P (f))
9666 block_input ();
9667 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9668 XFlush (FRAME_X_DISPLAY (f));
9669 unblock_input ();
9673 /* Request focus with XEmbed */
9675 void
9676 xembed_request_focus (struct frame *f)
9678 /* See XEmbed Protocol Specification at
9679 http://freedesktop.org/wiki/Specifications/xembed-spec */
9680 if (FRAME_VISIBLE_P (f))
9681 xembed_send_message (f, CurrentTime,
9682 XEMBED_REQUEST_FOCUS, 0, 0, 0);
9685 /* Activate frame with Extended Window Manager Hints */
9687 void
9688 x_ewmh_activate_frame (struct frame *f)
9690 /* See Window Manager Specification/Extended Window Manager Hints at
9691 http://freedesktop.org/wiki/Specifications/wm-spec */
9693 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9695 if (FRAME_VISIBLE_P (f) && wm_supports (f, dpyinfo->Xatom_net_active_window))
9697 Lisp_Object frame;
9698 XSETFRAME (frame, f);
9699 x_send_client_event (frame, make_number (0), frame,
9700 dpyinfo->Xatom_net_active_window,
9701 make_number (32),
9702 list2i (1, dpyinfo->last_user_time));
9706 static void
9707 XTframe_raise_lower (struct frame *f, bool raise_flag)
9709 if (raise_flag)
9710 x_raise_frame (f);
9711 else
9712 x_lower_frame (f);
9715 /* XEmbed implementation. */
9717 #if defined USE_X_TOOLKIT || ! defined USE_GTK
9719 /* XEmbed implementation. */
9721 #define XEMBED_VERSION 0
9723 static void
9724 xembed_set_info (struct frame *f, enum xembed_info flags)
9726 unsigned long data[2];
9727 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9729 data[0] = XEMBED_VERSION;
9730 data[1] = flags;
9732 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9733 dpyinfo->Xatom_XEMBED_INFO, dpyinfo->Xatom_XEMBED_INFO,
9734 32, PropModeReplace, (unsigned char *) data, 2);
9736 #endif /* defined USE_X_TOOLKIT || ! defined USE_GTK */
9738 static void
9739 xembed_send_message (struct frame *f, Time t, enum xembed_message msg,
9740 long int detail, long int data1, long int data2)
9742 XEvent event;
9744 event.xclient.type = ClientMessage;
9745 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
9746 event.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_XEMBED;
9747 event.xclient.format = 32;
9748 event.xclient.data.l[0] = t;
9749 event.xclient.data.l[1] = msg;
9750 event.xclient.data.l[2] = detail;
9751 event.xclient.data.l[3] = data1;
9752 event.xclient.data.l[4] = data2;
9754 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
9755 False, NoEventMask, &event);
9756 XSync (FRAME_X_DISPLAY (f), False);
9759 /* Change of visibility. */
9761 /* This tries to wait until the frame is really visible.
9762 However, if the window manager asks the user where to position
9763 the frame, this will return before the user finishes doing that.
9764 The frame will not actually be visible at that time,
9765 but it will become visible later when the window manager
9766 finishes with it. */
9768 void
9769 x_make_frame_visible (struct frame *f)
9771 int original_top, original_left;
9772 int tries = 0;
9774 block_input ();
9776 x_set_bitmap_icon (f);
9778 if (! FRAME_VISIBLE_P (f))
9780 /* We test FRAME_GARBAGED_P here to make sure we don't
9781 call x_set_offset a second time
9782 if we get to x_make_frame_visible a second time
9783 before the window gets really visible. */
9784 if (! FRAME_ICONIFIED_P (f)
9785 && ! FRAME_X_EMBEDDED_P (f)
9786 && ! f->output_data.x->asked_for_visible)
9787 x_set_offset (f, f->left_pos, f->top_pos, 0);
9789 f->output_data.x->asked_for_visible = true;
9791 if (! EQ (Vx_no_window_manager, Qt))
9792 x_wm_set_window_state (f, NormalState);
9793 #ifdef USE_X_TOOLKIT
9794 if (FRAME_X_EMBEDDED_P (f))
9795 xembed_set_info (f, XEMBED_MAPPED);
9796 else
9798 /* This was XtPopup, but that did nothing for an iconified frame. */
9799 XtMapWidget (f->output_data.x->widget);
9801 #else /* not USE_X_TOOLKIT */
9802 #ifdef USE_GTK
9803 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9804 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9805 #else
9806 if (FRAME_X_EMBEDDED_P (f))
9807 xembed_set_info (f, XEMBED_MAPPED);
9808 else
9809 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9810 #endif /* not USE_GTK */
9811 #endif /* not USE_X_TOOLKIT */
9814 XFlush (FRAME_X_DISPLAY (f));
9816 /* Synchronize to ensure Emacs knows the frame is visible
9817 before we do anything else. We do this loop with input not blocked
9818 so that incoming events are handled. */
9820 Lisp_Object frame;
9821 /* This must be before UNBLOCK_INPUT
9822 since events that arrive in response to the actions above
9823 will set it when they are handled. */
9824 bool previously_visible = f->output_data.x->has_been_visible;
9826 original_left = f->left_pos;
9827 original_top = f->top_pos;
9829 /* This must come after we set COUNT. */
9830 unblock_input ();
9832 /* We unblock here so that arriving X events are processed. */
9834 /* Now move the window back to where it was "supposed to be".
9835 But don't do it if the gravity is negative.
9836 When the gravity is negative, this uses a position
9837 that is 3 pixels too low. Perhaps that's really the border width.
9839 Don't do this if the window has never been visible before,
9840 because the window manager may choose the position
9841 and we don't want to override it. */
9843 if (! FRAME_VISIBLE_P (f)
9844 && ! FRAME_ICONIFIED_P (f)
9845 && ! FRAME_X_EMBEDDED_P (f)
9846 && f->win_gravity == NorthWestGravity
9847 && previously_visible)
9849 Drawable rootw;
9850 int x, y;
9851 unsigned int width, height, border, depth;
9853 block_input ();
9855 /* On some window managers (such as FVWM) moving an existing
9856 window, even to the same place, causes the window manager
9857 to introduce an offset. This can cause the window to move
9858 to an unexpected location. Check the geometry (a little
9859 slow here) and then verify that the window is in the right
9860 place. If the window is not in the right place, move it
9861 there, and take the potential window manager hit. */
9862 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9863 &rootw, &x, &y, &width, &height, &border, &depth);
9865 if (original_left != x || original_top != y)
9866 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9867 original_left, original_top);
9869 unblock_input ();
9872 XSETFRAME (frame, f);
9874 /* Process X events until a MapNotify event has been seen. */
9875 while (!FRAME_VISIBLE_P (f))
9877 /* Force processing of queued events. */
9878 x_sync (f);
9880 /* If on another desktop, the deiconify/map may be ignored and the
9881 frame never becomes visible. XMonad does this.
9882 Prevent an endless loop. */
9883 if (FRAME_ICONIFIED_P (f) && ++tries > 100)
9884 break;
9886 /* This hack is still in use at least for Cygwin. See
9887 http://lists.gnu.org/archive/html/emacs-devel/2013-12/msg00351.html.
9889 Machines that do polling rather than SIGIO have been
9890 observed to go into a busy-wait here. So we'll fake an
9891 alarm signal to let the handler know that there's something
9892 to be read. We used to raise a real alarm, but it seems
9893 that the handler isn't always enabled here. This is
9894 probably a bug. */
9895 if (input_polling_used ())
9897 /* It could be confusing if a real alarm arrives while
9898 processing the fake one. Turn it off and let the
9899 handler reset it. */
9900 int old_poll_suppress_count = poll_suppress_count;
9901 poll_suppress_count = 1;
9902 poll_for_input_1 ();
9903 poll_suppress_count = old_poll_suppress_count;
9906 if (XPending (FRAME_X_DISPLAY (f)))
9908 XEvent xev;
9909 XNextEvent (FRAME_X_DISPLAY (f), &xev);
9910 x_dispatch_event (&xev, FRAME_X_DISPLAY (f));
9916 /* Change from mapped state to withdrawn state. */
9918 /* Make the frame visible (mapped and not iconified). */
9920 void
9921 x_make_frame_invisible (struct frame *f)
9923 Window window;
9925 /* Use the frame's outermost window, not the one we normally draw on. */
9926 window = FRAME_OUTER_WINDOW (f);
9928 /* Don't keep the highlight on an invisible frame. */
9929 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
9930 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
9932 block_input ();
9934 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9935 that the current position of the window is user-specified, rather than
9936 program-specified, so that when the window is mapped again, it will be
9937 placed at the same location, without forcing the user to position it
9938 by hand again (they have already done that once for this window.) */
9939 x_wm_set_size_hint (f, 0, true);
9941 #ifdef USE_GTK
9942 if (FRAME_GTK_OUTER_WIDGET (f))
9943 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9944 else
9945 #else
9946 if (FRAME_X_EMBEDDED_P (f))
9947 xembed_set_info (f, 0);
9948 else
9949 #endif
9952 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9953 DefaultScreen (FRAME_X_DISPLAY (f))))
9955 unblock_input ();
9956 error ("Can't notify window manager of window withdrawal");
9960 /* We can't distinguish this from iconification
9961 just by the event that we get from the server.
9962 So we can't win using the usual strategy of letting
9963 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9964 and synchronize with the server to make sure we agree. */
9965 SET_FRAME_VISIBLE (f, 0);
9966 SET_FRAME_ICONIFIED (f, false);
9968 x_sync (f);
9970 unblock_input ();
9973 /* Change window state from mapped to iconified. */
9975 void
9976 x_iconify_frame (struct frame *f)
9978 #ifdef USE_X_TOOLKIT
9979 int result;
9980 #endif
9982 /* Don't keep the highlight on an invisible frame. */
9983 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
9984 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
9986 if (FRAME_ICONIFIED_P (f))
9987 return;
9989 block_input ();
9991 x_set_bitmap_icon (f);
9993 #if defined (USE_GTK)
9994 if (FRAME_GTK_OUTER_WIDGET (f))
9996 if (! FRAME_VISIBLE_P (f))
9997 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9999 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
10000 SET_FRAME_VISIBLE (f, 0);
10001 SET_FRAME_ICONIFIED (f, true);
10002 unblock_input ();
10003 return;
10005 #endif
10007 #ifdef USE_X_TOOLKIT
10009 if (! FRAME_VISIBLE_P (f))
10011 if (! EQ (Vx_no_window_manager, Qt))
10012 x_wm_set_window_state (f, IconicState);
10013 /* This was XtPopup, but that did nothing for an iconified frame. */
10014 XtMapWidget (f->output_data.x->widget);
10015 /* The server won't give us any event to indicate
10016 that an invisible frame was changed to an icon,
10017 so we have to record it here. */
10018 SET_FRAME_VISIBLE (f, 0);
10019 SET_FRAME_ICONIFIED (f, true);
10020 unblock_input ();
10021 return;
10024 result = XIconifyWindow (FRAME_X_DISPLAY (f),
10025 XtWindow (f->output_data.x->widget),
10026 DefaultScreen (FRAME_X_DISPLAY (f)));
10027 unblock_input ();
10029 if (!result)
10030 error ("Can't notify window manager of iconification");
10032 SET_FRAME_ICONIFIED (f, true);
10033 SET_FRAME_VISIBLE (f, 0);
10035 block_input ();
10036 XFlush (FRAME_X_DISPLAY (f));
10037 unblock_input ();
10038 #else /* not USE_X_TOOLKIT */
10040 /* Make sure the X server knows where the window should be positioned,
10041 in case the user deiconifies with the window manager. */
10042 if (! FRAME_VISIBLE_P (f)
10043 && ! FRAME_ICONIFIED_P (f)
10044 && ! FRAME_X_EMBEDDED_P (f))
10045 x_set_offset (f, f->left_pos, f->top_pos, 0);
10047 /* Since we don't know which revision of X we're running, we'll use both
10048 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
10050 /* X11R4: send a ClientMessage to the window manager using the
10051 WM_CHANGE_STATE type. */
10053 XEvent msg;
10055 msg.xclient.window = FRAME_X_WINDOW (f);
10056 msg.xclient.type = ClientMessage;
10057 msg.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_wm_change_state;
10058 msg.xclient.format = 32;
10059 msg.xclient.data.l[0] = IconicState;
10061 if (! XSendEvent (FRAME_X_DISPLAY (f),
10062 DefaultRootWindow (FRAME_X_DISPLAY (f)),
10063 False,
10064 SubstructureRedirectMask | SubstructureNotifyMask,
10065 &msg))
10067 unblock_input ();
10068 error ("Can't notify window manager of iconification");
10072 /* X11R3: set the initial_state field of the window manager hints to
10073 IconicState. */
10074 x_wm_set_window_state (f, IconicState);
10076 if (!FRAME_VISIBLE_P (f))
10078 /* If the frame was withdrawn, before, we must map it. */
10079 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10082 SET_FRAME_ICONIFIED (f, true);
10083 SET_FRAME_VISIBLE (f, 0);
10085 XFlush (FRAME_X_DISPLAY (f));
10086 unblock_input ();
10087 #endif /* not USE_X_TOOLKIT */
10091 /* Free X resources of frame F. */
10093 void
10094 x_free_frame_resources (struct frame *f)
10096 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10097 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
10098 #ifdef USE_X_TOOLKIT
10099 Lisp_Object bar;
10100 struct scroll_bar *b;
10101 #endif
10103 block_input ();
10105 /* If a display connection is dead, don't try sending more
10106 commands to the X server. */
10107 if (dpyinfo->display)
10109 /* Always exit with visible pointer to avoid weird issue
10110 with Xfixes (Bug#17609). */
10111 if (f->pointer_invisible)
10112 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, 0);
10114 /* We must free faces before destroying windows because some
10115 font-driver (e.g. xft) access a window while finishing a
10116 face. */
10117 free_frame_faces (f);
10119 if (f->output_data.x->icon_desc)
10120 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
10122 #ifdef USE_X_TOOLKIT
10123 /* Explicitly destroy the scroll bars of the frame. Without
10124 this, we get "BadDrawable" errors from the toolkit later on,
10125 presumably from expose events generated for the disappearing
10126 toolkit scroll bars. */
10127 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
10129 b = XSCROLL_BAR (bar);
10130 x_scroll_bar_remove (b);
10132 #endif
10134 #ifdef HAVE_X_I18N
10135 if (FRAME_XIC (f))
10136 free_frame_xic (f);
10137 #endif
10139 #ifdef USE_X_TOOLKIT
10140 if (f->output_data.x->widget)
10142 XtDestroyWidget (f->output_data.x->widget);
10143 f->output_data.x->widget = NULL;
10145 /* Tooltips don't have widgets, only a simple X window, even if
10146 we are using a toolkit. */
10147 else if (FRAME_X_WINDOW (f))
10148 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10150 free_frame_menubar (f);
10152 if (f->shell_position)
10153 xfree (f->shell_position);
10154 #else /* !USE_X_TOOLKIT */
10156 #ifdef USE_GTK
10157 xg_free_frame_widgets (f);
10158 #endif /* USE_GTK */
10160 if (FRAME_X_WINDOW (f))
10161 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10162 #endif /* !USE_X_TOOLKIT */
10164 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
10165 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
10166 unload_color (f, f->output_data.x->cursor_pixel);
10167 unload_color (f, f->output_data.x->cursor_foreground_pixel);
10168 unload_color (f, f->output_data.x->border_pixel);
10169 unload_color (f, f->output_data.x->mouse_pixel);
10171 if (f->output_data.x->scroll_bar_background_pixel != -1)
10172 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
10173 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
10174 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
10175 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
10176 /* Scrollbar shadow colors. */
10177 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
10178 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
10179 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
10180 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
10181 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
10182 if (f->output_data.x->white_relief.pixel != -1)
10183 unload_color (f, f->output_data.x->white_relief.pixel);
10184 if (f->output_data.x->black_relief.pixel != -1)
10185 unload_color (f, f->output_data.x->black_relief.pixel);
10187 x_free_gcs (f);
10189 /* Free extra GCs allocated by x_setup_relief_colors. */
10190 if (f->output_data.x->white_relief.gc)
10192 XFreeGC (dpyinfo->display, f->output_data.x->white_relief.gc);
10193 f->output_data.x->white_relief.gc = 0;
10195 if (f->output_data.x->black_relief.gc)
10197 XFreeGC (dpyinfo->display, f->output_data.x->black_relief.gc);
10198 f->output_data.x->black_relief.gc = 0;
10201 /* Free cursors. */
10202 if (f->output_data.x->text_cursor != 0)
10203 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->text_cursor);
10204 if (f->output_data.x->nontext_cursor != 0)
10205 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->nontext_cursor);
10206 if (f->output_data.x->modeline_cursor != 0)
10207 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->modeline_cursor);
10208 if (f->output_data.x->hand_cursor != 0)
10209 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hand_cursor);
10210 if (f->output_data.x->hourglass_cursor != 0)
10211 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hourglass_cursor);
10212 if (f->output_data.x->horizontal_drag_cursor != 0)
10213 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->horizontal_drag_cursor);
10214 if (f->output_data.x->vertical_drag_cursor != 0)
10215 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->vertical_drag_cursor);
10217 XFlush (FRAME_X_DISPLAY (f));
10220 xfree (f->output_data.x->saved_menu_event);
10221 xfree (f->output_data.x);
10222 f->output_data.x = NULL;
10224 if (f == dpyinfo->x_focus_frame)
10225 dpyinfo->x_focus_frame = 0;
10226 if (f == dpyinfo->x_focus_event_frame)
10227 dpyinfo->x_focus_event_frame = 0;
10228 if (f == dpyinfo->x_highlight_frame)
10229 dpyinfo->x_highlight_frame = 0;
10230 if (f == hlinfo->mouse_face_mouse_frame)
10231 reset_mouse_highlight (hlinfo);
10233 unblock_input ();
10237 /* Destroy the X window of frame F. */
10239 static void
10240 x_destroy_window (struct frame *f)
10242 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10244 /* If a display connection is dead, don't try sending more
10245 commands to the X server. */
10246 if (dpyinfo->display != 0)
10247 x_free_frame_resources (f);
10249 dpyinfo->reference_count--;
10253 /* Setting window manager hints. */
10255 /* Set the normal size hints for the window manager, for frame F.
10256 FLAGS is the flags word to use--or 0 meaning preserve the flags
10257 that the window now has.
10258 If USER_POSITION, set the USPosition
10259 flag (this is useful when FLAGS is 0).
10260 The GTK version is in gtkutils.c. */
10262 #ifndef USE_GTK
10263 void
10264 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
10266 XSizeHints size_hints;
10267 Window window = FRAME_OUTER_WINDOW (f);
10269 if (!window)
10270 return;
10272 #ifdef USE_X_TOOLKIT
10273 if (f->output_data.x->widget)
10275 widget_update_wm_size_hints (f->output_data.x->widget);
10276 return;
10278 #endif
10280 /* Setting PMaxSize caused various problems. */
10281 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
10283 size_hints.x = f->left_pos;
10284 size_hints.y = f->top_pos;
10286 size_hints.height = FRAME_PIXEL_HEIGHT (f);
10287 size_hints.width = FRAME_PIXEL_WIDTH (f);
10289 size_hints.width_inc = frame_resize_pixelwise ? 1 : FRAME_COLUMN_WIDTH (f);
10290 size_hints.height_inc = frame_resize_pixelwise ? 1 : FRAME_LINE_HEIGHT (f);
10292 size_hints.max_width = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
10293 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
10294 size_hints.max_height = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
10295 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
10297 /* Calculate the base and minimum sizes. */
10299 int base_width, base_height;
10300 int min_rows = 0, min_cols = 0;
10302 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
10303 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
10305 if (frame_resize_pixelwise)
10306 /* Needed to prevent a bad protocol error crash when making the
10307 frame size very small. */
10309 min_cols = 2 * min_cols;
10310 min_rows = 2 * min_rows;
10313 /* The window manager uses the base width hints to calculate the
10314 current number of rows and columns in the frame while
10315 resizing; min_width and min_height aren't useful for this
10316 purpose, since they might not give the dimensions for a
10317 zero-row, zero-column frame.
10319 We use the base_width and base_height members if we have
10320 them; otherwise, we set the min_width and min_height members
10321 to the size for a zero x zero frame. */
10323 size_hints.flags |= PBaseSize;
10324 size_hints.base_width = base_width;
10325 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
10326 size_hints.min_width = base_width + min_cols * FRAME_COLUMN_WIDTH (f);
10327 size_hints.min_height = base_height + min_rows * FRAME_LINE_HEIGHT (f);
10330 /* If we don't need the old flags, we don't need the old hint at all. */
10331 if (flags)
10333 size_hints.flags |= flags;
10334 goto no_read;
10338 XSizeHints hints; /* Sometimes I hate X Windows... */
10339 long supplied_return;
10340 int value;
10342 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
10343 &supplied_return);
10345 if (flags)
10346 size_hints.flags |= flags;
10347 else
10349 if (value == 0)
10350 hints.flags = 0;
10351 if (hints.flags & PSize)
10352 size_hints.flags |= PSize;
10353 if (hints.flags & PPosition)
10354 size_hints.flags |= PPosition;
10355 if (hints.flags & USPosition)
10356 size_hints.flags |= USPosition;
10357 if (hints.flags & USSize)
10358 size_hints.flags |= USSize;
10362 no_read:
10364 #ifdef PWinGravity
10365 size_hints.win_gravity = f->win_gravity;
10366 size_hints.flags |= PWinGravity;
10368 if (user_position)
10370 size_hints.flags &= ~ PPosition;
10371 size_hints.flags |= USPosition;
10373 #endif /* PWinGravity */
10375 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
10377 #endif /* not USE_GTK */
10379 /* Used for IconicState or NormalState */
10381 static void
10382 x_wm_set_window_state (struct frame *f, int state)
10384 #ifdef USE_X_TOOLKIT
10385 Arg al[1];
10387 XtSetArg (al[0], XtNinitialState, state);
10388 XtSetValues (f->output_data.x->widget, al, 1);
10389 #else /* not USE_X_TOOLKIT */
10390 Window window = FRAME_X_WINDOW (f);
10392 f->output_data.x->wm_hints.flags |= StateHint;
10393 f->output_data.x->wm_hints.initial_state = state;
10395 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
10396 #endif /* not USE_X_TOOLKIT */
10399 static void
10400 x_wm_set_icon_pixmap (struct frame *f, ptrdiff_t pixmap_id)
10402 Pixmap icon_pixmap, icon_mask;
10404 #if !defined USE_X_TOOLKIT && !defined USE_GTK
10405 Window window = FRAME_OUTER_WINDOW (f);
10406 #endif
10408 if (pixmap_id > 0)
10410 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
10411 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
10412 icon_mask = x_bitmap_mask (f, pixmap_id);
10413 f->output_data.x->wm_hints.icon_mask = icon_mask;
10415 else
10417 /* It seems there is no way to turn off use of an icon
10418 pixmap. */
10419 return;
10423 #ifdef USE_GTK
10425 xg_set_frame_icon (f, icon_pixmap, icon_mask);
10426 return;
10429 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
10432 Arg al[1];
10433 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
10434 XtSetValues (f->output_data.x->widget, al, 1);
10435 XtSetArg (al[0], XtNiconMask, icon_mask);
10436 XtSetValues (f->output_data.x->widget, al, 1);
10439 #else /* not USE_X_TOOLKIT && not USE_GTK */
10441 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
10442 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
10444 #endif /* not USE_X_TOOLKIT && not USE_GTK */
10447 void
10448 x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
10450 Window window = FRAME_OUTER_WINDOW (f);
10452 f->output_data.x->wm_hints.flags |= IconPositionHint;
10453 f->output_data.x->wm_hints.icon_x = icon_x;
10454 f->output_data.x->wm_hints.icon_y = icon_y;
10456 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
10460 /***********************************************************************
10461 Fonts
10462 ***********************************************************************/
10464 #ifdef GLYPH_DEBUG
10466 /* Check that FONT is valid on frame F. It is if it can be found in F's
10467 font table. */
10469 static void
10470 x_check_font (struct frame *f, struct font *font)
10472 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
10473 if (font->driver->check)
10474 eassert (font->driver->check (f, font) == 0);
10477 #endif /* GLYPH_DEBUG */
10480 /***********************************************************************
10481 Initialization
10482 ***********************************************************************/
10484 #ifdef USE_X_TOOLKIT
10485 static XrmOptionDescRec emacs_options[] = {
10486 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
10487 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
10489 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
10490 XrmoptionSepArg, NULL},
10491 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
10493 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10494 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10495 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10496 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10497 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10498 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
10499 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
10502 /* Whether atimer for Xt timeouts is activated or not. */
10504 static bool x_timeout_atimer_activated_flag;
10506 #endif /* USE_X_TOOLKIT */
10508 static int x_initialized;
10510 /* Test whether two display-name strings agree up to the dot that separates
10511 the screen number from the server number. */
10512 static bool
10513 same_x_server (const char *name1, const char *name2)
10515 bool seen_colon = false;
10516 Lisp_Object sysname = Fsystem_name ();
10517 const char *system_name = SSDATA (sysname);
10518 ptrdiff_t system_name_length = SBYTES (sysname);
10519 ptrdiff_t length_until_period = 0;
10521 while (system_name[length_until_period] != 0
10522 && system_name[length_until_period] != '.')
10523 length_until_period++;
10525 /* Treat `unix' like an empty host name. */
10526 if (! strncmp (name1, "unix:", 5))
10527 name1 += 4;
10528 if (! strncmp (name2, "unix:", 5))
10529 name2 += 4;
10530 /* Treat this host's name like an empty host name. */
10531 if (! strncmp (name1, system_name, system_name_length)
10532 && name1[system_name_length] == ':')
10533 name1 += system_name_length;
10534 if (! strncmp (name2, system_name, system_name_length)
10535 && name2[system_name_length] == ':')
10536 name2 += system_name_length;
10537 /* Treat this host's domainless name like an empty host name. */
10538 if (! strncmp (name1, system_name, length_until_period)
10539 && name1[length_until_period] == ':')
10540 name1 += length_until_period;
10541 if (! strncmp (name2, system_name, length_until_period)
10542 && name2[length_until_period] == ':')
10543 name2 += length_until_period;
10545 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
10547 if (*name1 == ':')
10548 seen_colon = true;
10549 if (seen_colon && *name1 == '.')
10550 return true;
10552 return (seen_colon
10553 && (*name1 == '.' || *name1 == '\0')
10554 && (*name2 == '.' || *name2 == '\0'));
10557 /* Count number of set bits in mask and number of bits to shift to
10558 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
10559 to 5. */
10560 static void
10561 get_bits_and_offset (unsigned long mask, int *bits, int *offset)
10563 int nr = 0;
10564 int off = 0;
10566 while (!(mask & 1))
10568 off++;
10569 mask >>= 1;
10572 while (mask & 1)
10574 nr++;
10575 mask >>= 1;
10578 *offset = off;
10579 *bits = nr;
10582 /* Return true iff display DISPLAY is available for use.
10583 But don't permanently open it, just test its availability. */
10585 bool
10586 x_display_ok (const char *display)
10588 Display *dpy = XOpenDisplay (display);
10589 if (!dpy)
10590 return false;
10591 XCloseDisplay (dpy);
10592 return true;
10595 #ifdef USE_GTK
10596 static void
10597 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
10598 const gchar *msg, gpointer user_data)
10600 if (!strstr (msg, "g_set_prgname"))
10601 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, msg);
10603 #endif
10605 /* Create invisible cursor on X display referred by DPYINFO. */
10607 static Cursor
10608 make_invisible_cursor (struct x_display_info *dpyinfo)
10610 Display *dpy = dpyinfo->display;
10611 static char const no_data[] = { 0 };
10612 Pixmap pix;
10613 XColor col;
10614 Cursor c = 0;
10616 x_catch_errors (dpy);
10617 pix = XCreateBitmapFromData (dpy, dpyinfo->root_window, no_data, 1, 1);
10618 if (! x_had_errors_p (dpy) && pix != None)
10620 Cursor pixc;
10621 col.pixel = 0;
10622 col.red = col.green = col.blue = 0;
10623 col.flags = DoRed | DoGreen | DoBlue;
10624 pixc = XCreatePixmapCursor (dpy, pix, pix, &col, &col, 0, 0);
10625 if (! x_had_errors_p (dpy) && pixc != None)
10626 c = pixc;
10627 XFreePixmap (dpy, pix);
10630 x_uncatch_errors ();
10632 return c;
10635 /* True if DPY supports Xfixes extension >= 4. */
10637 static bool
10638 x_probe_xfixes_extension (Display *dpy)
10640 #ifdef HAVE_XFIXES
10641 int major, minor;
10642 return XFixesQueryVersion (dpy, &major, &minor) && major >= 4;
10643 #else
10644 return false;
10645 #endif /* HAVE_XFIXES */
10648 /* Toggle mouse pointer visibility on frame F by using Xfixes functions. */
10650 static void
10651 xfixes_toggle_visible_pointer (struct frame *f, bool invisible)
10653 #ifdef HAVE_XFIXES
10654 if (invisible)
10655 XFixesHideCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10656 else
10657 XFixesShowCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10658 f->pointer_invisible = invisible;
10659 #else
10660 emacs_abort ();
10661 #endif /* HAVE_XFIXES */
10664 /* Toggle mouse pointer visibility on frame F by using invisible cursor. */
10666 static void
10667 x_toggle_visible_pointer (struct frame *f, bool invisible)
10669 eassert (FRAME_DISPLAY_INFO (f)->invisible_cursor != 0);
10670 if (invisible)
10671 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10672 FRAME_DISPLAY_INFO (f)->invisible_cursor);
10673 else
10674 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10675 f->output_data.x->current_cursor);
10676 f->pointer_invisible = invisible;
10679 /* Setup pointer blanking, prefer Xfixes if available. */
10681 static void
10682 x_setup_pointer_blanking (struct x_display_info *dpyinfo)
10684 /* FIXME: the brave tester should set EMACS_XFIXES because we're suspecting
10685 X server bug, see http://debbugs.gnu.org/cgi/bugreport.cgi?bug=17609. */
10686 if (egetenv ("EMACS_XFIXES") && x_probe_xfixes_extension (dpyinfo->display))
10687 dpyinfo->toggle_visible_pointer = xfixes_toggle_visible_pointer;
10688 else
10690 dpyinfo->toggle_visible_pointer = x_toggle_visible_pointer;
10691 dpyinfo->invisible_cursor = make_invisible_cursor (dpyinfo);
10695 /* Current X display connection identifier. Incremented for each next
10696 connection established. */
10697 static unsigned x_display_id;
10699 /* Open a connection to X display DISPLAY_NAME, and return
10700 the structure that describes the open display.
10701 If we cannot contact the display, return null. */
10703 struct x_display_info *
10704 x_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
10706 Display *dpy;
10707 struct terminal *terminal;
10708 struct x_display_info *dpyinfo;
10709 XrmDatabase xrdb;
10710 ptrdiff_t lim;
10712 block_input ();
10714 if (!x_initialized)
10716 x_initialize ();
10717 ++x_initialized;
10720 if (! x_display_ok (SSDATA (display_name)))
10721 error ("Display %s can't be opened", SSDATA (display_name));
10723 #ifdef USE_GTK
10725 #define NUM_ARGV 10
10726 int argc;
10727 char *argv[NUM_ARGV];
10728 char **argv2 = argv;
10729 guint id;
10731 if (x_initialized++ > 1)
10733 xg_display_open (SSDATA (display_name), &dpy);
10735 else
10737 static char display_opt[] = "--display";
10738 static char name_opt[] = "--name";
10740 for (argc = 0; argc < NUM_ARGV; ++argc)
10741 argv[argc] = 0;
10743 argc = 0;
10744 argv[argc++] = initial_argv[0];
10746 if (! NILP (display_name))
10748 argv[argc++] = display_opt;
10749 argv[argc++] = SSDATA (display_name);
10752 argv[argc++] = name_opt;
10753 argv[argc++] = resource_name;
10755 XSetLocaleModifiers ("");
10757 /* Work around GLib bug that outputs a faulty warning. See
10758 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
10759 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
10760 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
10762 /* NULL window -> events for all windows go to our function.
10763 Call before gtk_init so Gtk+ event filters comes after our. */
10764 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10766 /* gtk_init does set_locale. Fix locale before and after. */
10767 fixup_locale ();
10768 gtk_init (&argc, &argv2);
10769 fixup_locale ();
10771 g_log_remove_handler ("GLib", id);
10773 xg_initialize ();
10775 dpy = DEFAULT_GDK_DISPLAY ();
10777 #if ! GTK_CHECK_VERSION (2, 90, 0)
10778 /* Load our own gtkrc if it exists. */
10780 const char *file = "~/.emacs.d/gtkrc";
10781 Lisp_Object s, abs_file;
10783 s = build_string (file);
10784 abs_file = Fexpand_file_name (s, Qnil);
10786 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10787 gtk_rc_parse (SSDATA (abs_file));
10789 #endif
10791 XSetErrorHandler (x_error_handler);
10792 XSetIOErrorHandler (x_io_error_quitter);
10795 #else /* not USE_GTK */
10796 #ifdef USE_X_TOOLKIT
10797 /* weiner@footloose.sps.mot.com reports that this causes
10798 errors with X11R5:
10799 X protocol error: BadAtom (invalid Atom parameter)
10800 on protocol request 18skiloaf.
10801 So let's not use it until R6. */
10802 #ifdef HAVE_X11XTR6
10803 XtSetLanguageProc (NULL, NULL, NULL);
10804 #endif
10807 int argc = 0;
10808 char *argv[3];
10810 argv[0] = "";
10811 argc = 1;
10812 if (xrm_option)
10814 argv[argc++] = "-xrm";
10815 argv[argc++] = xrm_option;
10817 turn_on_atimers (false);
10818 dpy = XtOpenDisplay (Xt_app_con, SSDATA (display_name),
10819 resource_name, EMACS_CLASS,
10820 emacs_options, XtNumber (emacs_options),
10821 &argc, argv);
10822 turn_on_atimers (true);
10824 #ifdef HAVE_X11XTR6
10825 /* I think this is to compensate for XtSetLanguageProc. */
10826 fixup_locale ();
10827 #endif
10830 #else /* not USE_X_TOOLKIT */
10831 XSetLocaleModifiers ("");
10832 dpy = XOpenDisplay (SSDATA (display_name));
10833 #endif /* not USE_X_TOOLKIT */
10834 #endif /* not USE_GTK*/
10836 /* Detect failure. */
10837 if (dpy == 0)
10839 unblock_input ();
10840 return 0;
10843 /* We have definitely succeeded. Record the new connection. */
10845 dpyinfo = xzalloc (sizeof *dpyinfo);
10846 terminal = x_create_terminal (dpyinfo);
10849 struct x_display_info *share;
10851 for (share = x_display_list; share; share = share->next)
10852 if (same_x_server (SSDATA (XCAR (share->name_list_element)),
10853 SSDATA (display_name)))
10854 break;
10855 if (share)
10856 terminal->kboard = share->terminal->kboard;
10857 else
10859 terminal->kboard = allocate_kboard (Qx);
10861 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10863 char *vendor = ServerVendor (dpy);
10865 /* Protect terminal from GC before removing it from the
10866 list of terminals. */
10867 struct gcpro gcpro1;
10868 Lisp_Object gcpro_term;
10869 XSETTERMINAL (gcpro_term, terminal);
10870 GCPRO1 (gcpro_term);
10872 /* Temporarily hide the partially initialized terminal. */
10873 terminal_list = terminal->next_terminal;
10874 unblock_input ();
10875 kset_system_key_alist
10876 (terminal->kboard,
10877 call1 (Qvendor_specific_keysyms,
10878 vendor ? build_string (vendor) : empty_unibyte_string));
10879 block_input ();
10880 terminal->next_terminal = terminal_list;
10881 terminal_list = terminal;
10882 UNGCPRO;
10885 /* Don't let the initial kboard remain current longer than necessary.
10886 That would cause problems if a file loaded on startup tries to
10887 prompt in the mini-buffer. */
10888 if (current_kboard == initial_kboard)
10889 current_kboard = terminal->kboard;
10891 terminal->kboard->reference_count++;
10894 /* Put this display on the chain. */
10895 dpyinfo->next = x_display_list;
10896 x_display_list = dpyinfo;
10898 dpyinfo->name_list_element = Fcons (display_name, Qnil);
10899 dpyinfo->display = dpy;
10900 dpyinfo->connection = ConnectionNumber (dpyinfo->display);
10902 /* Set the name of the terminal. */
10903 terminal->name = xlispstrdup (display_name);
10905 #if false
10906 XSetAfterFunction (x_current_display, x_trace_wire);
10907 #endif
10909 lim = min (PTRDIFF_MAX, SIZE_MAX) - sizeof "@";
10910 Lisp_Object system_name = Fsystem_name ();
10911 if (lim - SBYTES (Vinvocation_name) < SBYTES (system_name))
10912 memory_full (SIZE_MAX);
10913 dpyinfo->x_id = ++x_display_id;
10914 dpyinfo->x_id_name = xmalloc (SBYTES (Vinvocation_name)
10915 + SBYTES (system_name) + 2);
10916 char *nametail = lispstpcpy (dpyinfo->x_id_name, Vinvocation_name);
10917 *nametail++ = '@';
10918 lispstpcpy (nametail, system_name);
10920 /* Figure out which modifier bits mean what. */
10921 x_find_modifier_meanings (dpyinfo);
10923 /* Get the scroll bar cursor. */
10924 #ifdef USE_GTK
10925 /* We must create a GTK cursor, it is required for GTK widgets. */
10926 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10927 #endif /* USE_GTK */
10929 dpyinfo->vertical_scroll_bar_cursor
10930 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10932 dpyinfo->horizontal_scroll_bar_cursor
10933 = XCreateFontCursor (dpyinfo->display, XC_sb_h_double_arrow);
10935 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10936 resource_name, EMACS_CLASS);
10937 #ifdef HAVE_XRMSETDATABASE
10938 XrmSetDatabase (dpyinfo->display, xrdb);
10939 #else
10940 dpyinfo->display->db = xrdb;
10941 #endif
10942 /* Put the rdb where we can find it in a way that works on
10943 all versions. */
10944 dpyinfo->xrdb = xrdb;
10946 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10947 DefaultScreen (dpyinfo->display));
10948 select_visual (dpyinfo);
10949 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10950 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10951 dpyinfo->icon_bitmap_id = -1;
10952 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10954 reset_mouse_highlight (&dpyinfo->mouse_highlight);
10956 /* See if we can construct pixel values from RGB values. */
10957 if (dpyinfo->visual->class == TrueColor)
10959 get_bits_and_offset (dpyinfo->visual->red_mask,
10960 &dpyinfo->red_bits, &dpyinfo->red_offset);
10961 get_bits_and_offset (dpyinfo->visual->blue_mask,
10962 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10963 get_bits_and_offset (dpyinfo->visual->green_mask,
10964 &dpyinfo->green_bits, &dpyinfo->green_offset);
10967 /* See if a private colormap is requested. */
10968 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10970 if (dpyinfo->visual->class == PseudoColor)
10972 AUTO_STRING (privateColormap, "privateColormap");
10973 AUTO_STRING (PrivateColormap, "PrivateColormap");
10974 Lisp_Object value
10975 = display_x_get_resource (dpyinfo, privateColormap,
10976 PrivateColormap, Qnil, Qnil);
10977 if (STRINGP (value)
10978 && (!strcmp (SSDATA (value), "true")
10979 || !strcmp (SSDATA (value), "on")))
10980 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10983 else
10984 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10985 dpyinfo->visual, AllocNone);
10987 #ifdef HAVE_XFT
10989 /* If we are using Xft, the following precautions should be made:
10991 1. Make sure that the Xrender extension is added before the Xft one.
10992 Otherwise, the close-display hook set by Xft is called after the one
10993 for Xrender, and the former tries to re-add the latter. This results
10994 in inconsistency of internal states and leads to X protocol error when
10995 one reconnects to the same X server (Bug#1696).
10997 2. Check dpi value in X resources. It is better we use it as well,
10998 since Xft will use it, as will all Gnome applications. If our real DPI
10999 is smaller or larger than the one Xft uses, our font will look smaller
11000 or larger than other for other applications, even if it is the same
11001 font name (monospace-10 for example). */
11003 int event_base, error_base;
11004 char *v;
11005 double d;
11007 XRenderQueryExtension (dpyinfo->display, &event_base, &error_base);
11009 v = XGetDefault (dpyinfo->display, "Xft", "dpi");
11010 if (v != NULL && sscanf (v, "%lf", &d) == 1)
11011 dpyinfo->resy = dpyinfo->resx = d;
11013 #endif
11015 if (dpyinfo->resy < 1)
11017 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
11018 double pixels = DisplayHeight (dpyinfo->display, screen_number);
11019 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
11020 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
11021 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
11022 pixels = DisplayWidth (dpyinfo->display, screen_number);
11023 mm = DisplayWidthMM (dpyinfo->display, screen_number);
11024 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
11025 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
11029 static const struct
11031 const char *name;
11032 int offset;
11033 } atom_refs[] = {
11034 #define ATOM_REFS_INIT(string, member) \
11035 { string, offsetof (struct x_display_info, member) },
11036 ATOM_REFS_INIT ("WM_PROTOCOLS", Xatom_wm_protocols)
11037 ATOM_REFS_INIT ("WM_TAKE_FOCUS", Xatom_wm_take_focus)
11038 ATOM_REFS_INIT ("WM_SAVE_YOURSELF", Xatom_wm_save_yourself)
11039 ATOM_REFS_INIT ("WM_DELETE_WINDOW", Xatom_wm_delete_window)
11040 ATOM_REFS_INIT ("WM_CHANGE_STATE", Xatom_wm_change_state)
11041 ATOM_REFS_INIT ("WM_CONFIGURE_DENIED", Xatom_wm_configure_denied)
11042 ATOM_REFS_INIT ("WM_MOVED", Xatom_wm_window_moved)
11043 ATOM_REFS_INIT ("WM_CLIENT_LEADER", Xatom_wm_client_leader)
11044 ATOM_REFS_INIT ("Editres", Xatom_editres)
11045 ATOM_REFS_INIT ("CLIPBOARD", Xatom_CLIPBOARD)
11046 ATOM_REFS_INIT ("TIMESTAMP", Xatom_TIMESTAMP)
11047 ATOM_REFS_INIT ("TEXT", Xatom_TEXT)
11048 ATOM_REFS_INIT ("COMPOUND_TEXT", Xatom_COMPOUND_TEXT)
11049 ATOM_REFS_INIT ("UTF8_STRING", Xatom_UTF8_STRING)
11050 ATOM_REFS_INIT ("DELETE", Xatom_DELETE)
11051 ATOM_REFS_INIT ("MULTIPLE", Xatom_MULTIPLE)
11052 ATOM_REFS_INIT ("INCR", Xatom_INCR)
11053 ATOM_REFS_INIT ("_EMACS_TMP_", Xatom_EMACS_TMP)
11054 ATOM_REFS_INIT ("TARGETS", Xatom_TARGETS)
11055 ATOM_REFS_INIT ("NULL", Xatom_NULL)
11056 ATOM_REFS_INIT ("ATOM", Xatom_ATOM)
11057 ATOM_REFS_INIT ("ATOM_PAIR", Xatom_ATOM_PAIR)
11058 ATOM_REFS_INIT ("CLIPBOARD_MANAGER", Xatom_CLIPBOARD_MANAGER)
11059 ATOM_REFS_INIT ("_XEMBED_INFO", Xatom_XEMBED_INFO)
11060 /* For properties of font. */
11061 ATOM_REFS_INIT ("PIXEL_SIZE", Xatom_PIXEL_SIZE)
11062 ATOM_REFS_INIT ("AVERAGE_WIDTH", Xatom_AVERAGE_WIDTH)
11063 ATOM_REFS_INIT ("_MULE_BASELINE_OFFSET", Xatom_MULE_BASELINE_OFFSET)
11064 ATOM_REFS_INIT ("_MULE_RELATIVE_COMPOSE", Xatom_MULE_RELATIVE_COMPOSE)
11065 ATOM_REFS_INIT ("_MULE_DEFAULT_ASCENT", Xatom_MULE_DEFAULT_ASCENT)
11066 /* Ghostscript support. */
11067 ATOM_REFS_INIT ("DONE", Xatom_DONE)
11068 ATOM_REFS_INIT ("PAGE", Xatom_PAGE)
11069 ATOM_REFS_INIT ("SCROLLBAR", Xatom_Scrollbar)
11070 ATOM_REFS_INIT ("HORIZONTAL_SCROLLBAR", Xatom_Horizontal_Scrollbar)
11071 ATOM_REFS_INIT ("_XEMBED", Xatom_XEMBED)
11072 /* EWMH */
11073 ATOM_REFS_INIT ("_NET_WM_STATE", Xatom_net_wm_state)
11074 ATOM_REFS_INIT ("_NET_WM_STATE_FULLSCREEN", Xatom_net_wm_state_fullscreen)
11075 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_HORZ",
11076 Xatom_net_wm_state_maximized_horz)
11077 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_VERT",
11078 Xatom_net_wm_state_maximized_vert)
11079 ATOM_REFS_INIT ("_NET_WM_STATE_STICKY", Xatom_net_wm_state_sticky)
11080 ATOM_REFS_INIT ("_NET_WM_STATE_HIDDEN", Xatom_net_wm_state_hidden)
11081 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE", Xatom_net_window_type)
11082 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE_TOOLTIP",
11083 Xatom_net_window_type_tooltip)
11084 ATOM_REFS_INIT ("_NET_WM_ICON_NAME", Xatom_net_wm_icon_name)
11085 ATOM_REFS_INIT ("_NET_WM_NAME", Xatom_net_wm_name)
11086 ATOM_REFS_INIT ("_NET_SUPPORTED", Xatom_net_supported)
11087 ATOM_REFS_INIT ("_NET_SUPPORTING_WM_CHECK", Xatom_net_supporting_wm_check)
11088 ATOM_REFS_INIT ("_NET_WM_WINDOW_OPACITY", Xatom_net_wm_window_opacity)
11089 ATOM_REFS_INIT ("_NET_ACTIVE_WINDOW", Xatom_net_active_window)
11090 ATOM_REFS_INIT ("_NET_FRAME_EXTENTS", Xatom_net_frame_extents)
11091 ATOM_REFS_INIT ("_NET_CURRENT_DESKTOP", Xatom_net_current_desktop)
11092 ATOM_REFS_INIT ("_NET_WORKAREA", Xatom_net_workarea)
11093 /* Session management */
11094 ATOM_REFS_INIT ("SM_CLIENT_ID", Xatom_SM_CLIENT_ID)
11095 ATOM_REFS_INIT ("_XSETTINGS_SETTINGS", Xatom_xsettings_prop)
11096 ATOM_REFS_INIT ("MANAGER", Xatom_xsettings_mgr)
11099 int i;
11100 enum { atom_count = ARRAYELTS (atom_refs) };
11101 /* 1 for _XSETTINGS_SN. */
11102 enum { total_atom_count = 1 + atom_count };
11103 Atom atoms_return[total_atom_count];
11104 char *atom_names[total_atom_count];
11105 static char const xsettings_fmt[] = "_XSETTINGS_S%d";
11106 char xsettings_atom_name[sizeof xsettings_fmt - 2
11107 + INT_STRLEN_BOUND (int)];
11109 for (i = 0; i < atom_count; i++)
11110 atom_names[i] = (char *) atom_refs[i].name;
11112 /* Build _XSETTINGS_SN atom name. */
11113 sprintf (xsettings_atom_name, xsettings_fmt,
11114 XScreenNumberOfScreen (dpyinfo->screen));
11115 atom_names[i] = xsettings_atom_name;
11117 XInternAtoms (dpyinfo->display, atom_names, total_atom_count,
11118 False, atoms_return);
11120 for (i = 0; i < atom_count; i++)
11121 *(Atom *) ((char *) dpyinfo + atom_refs[i].offset) = atoms_return[i];
11123 /* Manually copy last atom. */
11124 dpyinfo->Xatom_xsettings_sel = atoms_return[i];
11127 dpyinfo->x_dnd_atoms_size = 8;
11128 dpyinfo->x_dnd_atoms = xmalloc (sizeof *dpyinfo->x_dnd_atoms
11129 * dpyinfo->x_dnd_atoms_size);
11130 dpyinfo->gray
11131 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
11132 gray_bits, gray_width, gray_height,
11133 1, 0, 1);
11135 x_setup_pointer_blanking (dpyinfo);
11137 #ifdef HAVE_X_I18N
11138 xim_initialize (dpyinfo, resource_name);
11139 #endif
11141 xsettings_initialize (dpyinfo);
11143 /* This is only needed for distinguishing keyboard and process input. */
11144 if (dpyinfo->connection != 0)
11145 add_keyboard_wait_descriptor (dpyinfo->connection);
11147 #ifdef F_SETOWN
11148 fcntl (dpyinfo->connection, F_SETOWN, getpid ());
11149 #endif /* ! defined (F_SETOWN) */
11151 if (interrupt_input)
11152 init_sigio (dpyinfo->connection);
11154 #ifdef USE_LUCID
11156 XrmValue d, fr, to;
11157 Font font;
11159 dpy = dpyinfo->display;
11160 d.addr = (XPointer)&dpy;
11161 d.size = sizeof (Display *);
11162 fr.addr = XtDefaultFont;
11163 fr.size = sizeof (XtDefaultFont);
11164 to.size = sizeof (Font *);
11165 to.addr = (XPointer)&font;
11166 x_catch_errors (dpy);
11167 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
11168 emacs_abort ();
11169 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
11170 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
11171 /* Do not free XFontStruct returned by the above call to XQueryFont.
11172 This leads to X protocol errors at XtCloseDisplay (Bug#18403). */
11173 x_uncatch_errors ();
11175 #endif
11177 /* See if we should run in synchronous mode. This is useful
11178 for debugging X code. */
11180 AUTO_STRING (synchronous, "synchronous");
11181 AUTO_STRING (Synchronous, "Synchronous");
11182 Lisp_Object value = display_x_get_resource (dpyinfo, synchronous,
11183 Synchronous, Qnil, Qnil);
11184 if (STRINGP (value)
11185 && (!strcmp (SSDATA (value), "true")
11186 || !strcmp (SSDATA (value), "on")))
11187 XSynchronize (dpyinfo->display, True);
11191 AUTO_STRING (useXIM, "useXIM");
11192 AUTO_STRING (UseXIM, "UseXIM");
11193 Lisp_Object value = display_x_get_resource (dpyinfo, useXIM, UseXIM,
11194 Qnil, Qnil);
11195 #ifdef USE_XIM
11196 if (STRINGP (value)
11197 && (!strcmp (SSDATA (value), "false")
11198 || !strcmp (SSDATA (value), "off")))
11199 use_xim = false;
11200 #else
11201 if (STRINGP (value)
11202 && (!strcmp (SSDATA (value), "true")
11203 || !strcmp (SSDATA (value), "on")))
11204 use_xim = true;
11205 #endif
11208 #ifdef HAVE_X_SM
11209 /* Only do this for the very first display in the Emacs session.
11210 Ignore X session management when Emacs was first started on a
11211 tty or started as a daemon. */
11212 if (terminal->id == 1 && ! IS_DAEMON)
11213 x_session_initialize (dpyinfo);
11214 #endif
11216 unblock_input ();
11218 return dpyinfo;
11221 /* Get rid of display DPYINFO, deleting all frames on it,
11222 and without sending any more commands to the X server. */
11224 static void
11225 x_delete_display (struct x_display_info *dpyinfo)
11227 struct terminal *t;
11229 /* Close all frames and delete the generic struct terminal for this
11230 X display. */
11231 for (t = terminal_list; t; t = t->next_terminal)
11232 if (t->type == output_x_window && t->display_info.x == dpyinfo)
11234 #ifdef HAVE_X_SM
11235 /* Close X session management when we close its display. */
11236 if (t->id == 1 && x_session_have_connection ())
11237 x_session_close ();
11238 #endif
11239 delete_terminal (t);
11240 break;
11243 if (next_noop_dpyinfo == dpyinfo)
11244 next_noop_dpyinfo = dpyinfo->next;
11246 if (x_display_list == dpyinfo)
11247 x_display_list = dpyinfo->next;
11248 else
11250 struct x_display_info *tail;
11252 for (tail = x_display_list; tail; tail = tail->next)
11253 if (tail->next == dpyinfo)
11254 tail->next = tail->next->next;
11257 xfree (dpyinfo->x_id_name);
11258 xfree (dpyinfo->x_dnd_atoms);
11259 xfree (dpyinfo->color_cells);
11260 xfree (dpyinfo);
11263 #ifdef USE_X_TOOLKIT
11265 /* Atimer callback function for TIMER. Called every 0.1s to process
11266 Xt timeouts, if needed. We must avoid calling XtAppPending as
11267 much as possible because that function does an implicit XFlush
11268 that slows us down. */
11270 static void
11271 x_process_timeouts (struct atimer *timer)
11273 block_input ();
11274 x_timeout_atimer_activated_flag = false;
11275 if (toolkit_scroll_bar_interaction || popup_activated ())
11277 while (XtAppPending (Xt_app_con) & XtIMTimer)
11278 XtAppProcessEvent (Xt_app_con, XtIMTimer);
11279 /* Reactivate the atimer for next time. */
11280 x_activate_timeout_atimer ();
11282 unblock_input ();
11285 /* Install an asynchronous timer that processes Xt timeout events
11286 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
11287 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
11288 function whenever these variables are set. This is necessary
11289 because some widget sets use timeouts internally, for example the
11290 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
11291 processed, these widgets don't behave normally. */
11293 void
11294 x_activate_timeout_atimer (void)
11296 block_input ();
11297 if (!x_timeout_atimer_activated_flag)
11299 struct timespec interval = make_timespec (0, 100 * 1000 * 1000);
11300 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
11301 x_timeout_atimer_activated_flag = true;
11303 unblock_input ();
11306 #endif /* USE_X_TOOLKIT */
11309 /* Set up use of X before we make the first connection. */
11311 static struct redisplay_interface x_redisplay_interface =
11313 x_frame_parm_handlers,
11314 x_produce_glyphs,
11315 x_write_glyphs,
11316 x_insert_glyphs,
11317 x_clear_end_of_line,
11318 x_scroll_run,
11319 x_after_update_window_line,
11320 x_update_window_begin,
11321 x_update_window_end,
11322 x_flush,
11323 x_clear_window_mouse_face,
11324 x_get_glyph_overhangs,
11325 x_fix_overlapping_area,
11326 x_draw_fringe_bitmap,
11327 0, /* define_fringe_bitmap */
11328 0, /* destroy_fringe_bitmap */
11329 x_compute_glyph_string_overhangs,
11330 x_draw_glyph_string,
11331 x_define_frame_cursor,
11332 x_clear_frame_area,
11333 x_draw_window_cursor,
11334 x_draw_vertical_window_border,
11335 x_draw_window_divider,
11336 x_shift_glyphs_for_insert,
11337 x_show_hourglass,
11338 x_hide_hourglass
11342 /* This function is called when the last frame on a display is deleted. */
11343 void
11344 x_delete_terminal (struct terminal *terminal)
11346 struct x_display_info *dpyinfo = terminal->display_info.x;
11348 /* Protect against recursive calls. delete_frame in
11349 delete_terminal calls us back when it deletes our last frame. */
11350 if (!terminal->name)
11351 return;
11353 block_input ();
11354 #ifdef HAVE_X_I18N
11355 /* We must close our connection to the XIM server before closing the
11356 X display. */
11357 if (dpyinfo->xim)
11358 xim_close_dpy (dpyinfo);
11359 #endif
11361 /* Normally, the display is available... */
11362 if (dpyinfo->display)
11364 x_destroy_all_bitmaps (dpyinfo);
11365 XSetCloseDownMode (dpyinfo->display, DestroyAll);
11367 /* Whether or not XCloseDisplay destroys the associated resource
11368 database depends on the version of libX11. To avoid both
11369 crash and memory leak, we dissociate the database from the
11370 display and then destroy dpyinfo->xrdb ourselves.
11372 Unfortunately, the above strategy does not work in some
11373 situations due to a bug in newer versions of libX11: because
11374 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
11375 dpy->db is NULL, XCloseDisplay destroys the associated
11376 database whereas it has not been created by XGetDefault
11377 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
11378 don't destroy the database here in order to avoid the crash
11379 in the above situations for now, though that may cause memory
11380 leaks in other situations. */
11381 #if false
11382 #ifdef HAVE_XRMSETDATABASE
11383 XrmSetDatabase (dpyinfo->display, NULL);
11384 #else
11385 dpyinfo->display->db = NULL;
11386 #endif
11387 /* We used to call XrmDestroyDatabase from x_delete_display, but
11388 some older versions of libX11 crash if we call it after
11389 closing all the displays. */
11390 XrmDestroyDatabase (dpyinfo->xrdb);
11391 #endif
11393 #ifdef USE_GTK
11394 xg_display_close (dpyinfo->display);
11395 #else
11396 #ifdef USE_X_TOOLKIT
11397 XtCloseDisplay (dpyinfo->display);
11398 #else
11399 XCloseDisplay (dpyinfo->display);
11400 #endif
11401 #endif /* ! USE_GTK */
11402 /* Do not close the connection here because it's already closed
11403 by X(t)CloseDisplay (Bug#18403). */
11404 dpyinfo->display = NULL;
11407 /* ...but if called from x_connection_closed, the display may already
11408 be closed and dpyinfo->display was set to 0 to indicate that. Since
11409 X server is most likely gone, explicit close is the only reliable
11410 way to continue and avoid Bug#19147. */
11411 else if (dpyinfo->connection >= 0)
11412 emacs_close (dpyinfo->connection);
11414 /* No more input on this descriptor. */
11415 delete_keyboard_wait_descriptor (dpyinfo->connection);
11416 /* Mark as dead. */
11417 dpyinfo->connection = -1;
11419 x_delete_display (dpyinfo);
11420 unblock_input ();
11423 /* Create a struct terminal, initialize it with the X11 specific
11424 functions and make DISPLAY->TERMINAL point to it. */
11426 static struct terminal *
11427 x_create_terminal (struct x_display_info *dpyinfo)
11429 struct terminal *terminal;
11431 terminal = create_terminal (output_x_window, &x_redisplay_interface);
11433 terminal->display_info.x = dpyinfo;
11434 dpyinfo->terminal = terminal;
11436 /* kboard is initialized in x_term_init. */
11438 terminal->clear_frame_hook = x_clear_frame;
11439 terminal->ins_del_lines_hook = x_ins_del_lines;
11440 terminal->delete_glyphs_hook = x_delete_glyphs;
11441 terminal->ring_bell_hook = XTring_bell;
11442 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
11443 terminal->update_begin_hook = x_update_begin;
11444 terminal->update_end_hook = x_update_end;
11445 terminal->read_socket_hook = XTread_socket;
11446 terminal->frame_up_to_date_hook = XTframe_up_to_date;
11447 terminal->mouse_position_hook = XTmouse_position;
11448 terminal->frame_rehighlight_hook = XTframe_rehighlight;
11449 terminal->frame_raise_lower_hook = XTframe_raise_lower;
11450 terminal->fullscreen_hook = XTfullscreen_hook;
11451 terminal->menu_show_hook = x_menu_show;
11452 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
11453 terminal->popup_dialog_hook = xw_popup_dialog;
11454 #endif
11455 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
11456 terminal->set_horizontal_scroll_bar_hook = XTset_horizontal_scroll_bar;
11457 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
11458 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
11459 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
11460 terminal->delete_frame_hook = x_destroy_window;
11461 terminal->delete_terminal_hook = x_delete_terminal;
11462 /* Other hooks are NULL by default. */
11464 return terminal;
11467 static void
11468 x_initialize (void)
11470 baud_rate = 19200;
11472 x_noop_count = 0;
11473 any_help_event_p = false;
11474 ignore_next_mouse_click_timeout = 0;
11476 #ifdef USE_GTK
11477 current_count = -1;
11478 #endif
11480 /* Try to use interrupt input; if we can't, then start polling. */
11481 Fset_input_interrupt_mode (Qt);
11483 #ifdef USE_X_TOOLKIT
11484 XtToolkitInitialize ();
11486 Xt_app_con = XtCreateApplicationContext ();
11488 /* Register a converter from strings to pixels, which uses
11489 Emacs' color allocation infrastructure. */
11490 XtAppSetTypeConverter (Xt_app_con,
11491 XtRString, XtRPixel, cvt_string_to_pixel,
11492 cvt_string_to_pixel_args,
11493 XtNumber (cvt_string_to_pixel_args),
11494 XtCacheByDisplay, cvt_pixel_dtor);
11496 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
11497 #endif
11499 #ifdef USE_TOOLKIT_SCROLL_BARS
11500 #ifndef USE_GTK
11501 xaw3d_arrow_scroll = False;
11502 xaw3d_pick_top = True;
11503 #endif
11504 #endif
11506 /* Note that there is no real way portable across R3/R4 to get the
11507 original error handler. */
11508 XSetErrorHandler (x_error_handler);
11509 XSetIOErrorHandler (x_io_error_quitter);
11512 #ifdef USE_GTK
11513 void
11514 init_xterm (void)
11516 /* Emacs can handle only core input events, so make sure
11517 Gtk doesn't use Xinput or Xinput2 extensions. */
11518 xputenv ("GDK_CORE_DEVICE_EVENTS=1");
11520 #endif
11522 void
11523 syms_of_xterm (void)
11525 x_error_message = NULL;
11527 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
11528 DEFSYM (Qlatin_1, "latin-1");
11530 #ifdef USE_GTK
11531 xg_default_icon_file = build_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
11532 staticpro (&xg_default_icon_file);
11534 DEFSYM (Qx_gtk_map_stock, "x-gtk-map-stock");
11535 #endif
11537 DEFVAR_BOOL ("x-use-underline-position-properties",
11538 x_use_underline_position_properties,
11539 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
11540 A value of nil means ignore them. If you encounter fonts with bogus
11541 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
11542 to 4.1, set this to nil. You can also use `underline-minimum-offset'
11543 to override the font's UNDERLINE_POSITION for small font display
11544 sizes. */);
11545 x_use_underline_position_properties = true;
11547 DEFVAR_BOOL ("x-underline-at-descent-line",
11548 x_underline_at_descent_line,
11549 doc: /* Non-nil means to draw the underline at the same place as the descent line.
11550 A value of nil means to draw the underline according to the value of the
11551 variable `x-use-underline-position-properties', which is usually at the
11552 baseline level. The default value is nil. */);
11553 x_underline_at_descent_line = false;
11555 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
11556 x_mouse_click_focus_ignore_position,
11557 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
11558 This variable is only used when the window manager requires that you
11559 click on a frame to select it (give it focus). In that case, a value
11560 of nil, means that the selected window and cursor position changes to
11561 reflect the mouse click position, while a non-nil value means that the
11562 selected window or cursor position is preserved. */);
11563 x_mouse_click_focus_ignore_position = false;
11565 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
11566 doc: /* Which toolkit scroll bars Emacs uses, if any.
11567 A value of nil means Emacs doesn't use toolkit scroll bars.
11568 With the X Window system, the value is a symbol describing the
11569 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
11570 With MS Windows or Nextstep, the value is t. */);
11571 #ifdef USE_TOOLKIT_SCROLL_BARS
11572 #ifdef USE_MOTIF
11573 Vx_toolkit_scroll_bars = intern_c_string ("motif");
11574 #elif defined HAVE_XAW3D
11575 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
11576 #elif USE_GTK
11577 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
11578 #else
11579 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
11580 #endif
11581 #else
11582 Vx_toolkit_scroll_bars = Qnil;
11583 #endif
11585 DEFSYM (Qmodifier_value, "modifier-value");
11586 DEFSYM (Qalt, "alt");
11587 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
11588 DEFSYM (Qhyper, "hyper");
11589 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
11590 DEFSYM (Qmeta, "meta");
11591 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
11592 DEFSYM (Qsuper, "super");
11593 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
11595 DEFVAR_LISP ("x-alt-keysym", Vx_alt_keysym,
11596 doc: /* Which keys Emacs uses for the alt modifier.
11597 This should be one of the symbols `alt', `hyper', `meta', `super'.
11598 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
11599 is nil, which is the same as `alt'. */);
11600 Vx_alt_keysym = Qnil;
11602 DEFVAR_LISP ("x-hyper-keysym", Vx_hyper_keysym,
11603 doc: /* Which keys Emacs uses for the hyper modifier.
11604 This should be one of the symbols `alt', `hyper', `meta', `super'.
11605 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
11606 default is nil, which is the same as `hyper'. */);
11607 Vx_hyper_keysym = Qnil;
11609 DEFVAR_LISP ("x-meta-keysym", Vx_meta_keysym,
11610 doc: /* Which keys Emacs uses for the meta modifier.
11611 This should be one of the symbols `alt', `hyper', `meta', `super'.
11612 For example, `meta' means use the Meta_L and Meta_R keysyms. The
11613 default is nil, which is the same as `meta'. */);
11614 Vx_meta_keysym = Qnil;
11616 DEFVAR_LISP ("x-super-keysym", Vx_super_keysym,
11617 doc: /* Which keys Emacs uses for the super modifier.
11618 This should be one of the symbols `alt', `hyper', `meta', `super'.
11619 For example, `super' means use the Super_L and Super_R keysyms. The
11620 default is nil, which is the same as `super'. */);
11621 Vx_super_keysym = Qnil;
11623 DEFVAR_LISP ("x-keysym-table", Vx_keysym_table,
11624 doc: /* Hash table of character codes indexed by X keysym codes. */);
11625 Vx_keysym_table = make_hash_table (hashtest_eql, make_number (900),
11626 make_float (DEFAULT_REHASH_SIZE),
11627 make_float (DEFAULT_REHASH_THRESHOLD),
11628 Qnil);