Removed remains of Clutter experiment
[emacs.git] / src / xterm.c
blob58fb8866c98b8d648bfdcf4585f4976a5e7bff18
1 /* X Communication module for terminals which understand the X protocol.
3 Copyright (C) 1989, 1993-2014 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;
2689 #ifdef HAVE_XWIDGETS
2690 case XWIDGET_GLYPH:
2691 //erase xwidget background
2692 //x_draw_glyph_string_background (s, 0);
2693 x_draw_xwidget_glyph_string (s);
2694 break;
2695 #endif
2696 case STRETCH_GLYPH:
2697 x_draw_stretch_glyph_string (s);
2698 break;
2700 case CHAR_GLYPH:
2701 if (s->for_overlaps)
2702 s->background_filled_p = true;
2703 else
2704 x_draw_glyph_string_background (s, false);
2705 x_draw_glyph_string_foreground (s);
2706 break;
2708 case COMPOSITE_GLYPH:
2709 if (s->for_overlaps || (s->cmp_from > 0
2710 && ! s->first_glyph->u.cmp.automatic))
2711 s->background_filled_p = true;
2712 else
2713 x_draw_glyph_string_background (s, true);
2714 x_draw_composite_glyph_string_foreground (s);
2715 break;
2717 case GLYPHLESS_GLYPH:
2718 if (s->for_overlaps)
2719 s->background_filled_p = true;
2720 else
2721 x_draw_glyph_string_background (s, true);
2722 x_draw_glyphless_glyph_string_foreground (s);
2723 break;
2725 default:
2726 emacs_abort ();
2729 if (!s->for_overlaps)
2731 /* Draw underline. */
2732 if (s->face->underline_p)
2734 if (s->face->underline_type == FACE_UNDER_WAVE)
2736 if (s->face->underline_defaulted_p)
2737 x_draw_underwave (s);
2738 else
2740 XGCValues xgcv;
2741 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2742 XSetForeground (s->display, s->gc, s->face->underline_color);
2743 x_draw_underwave (s);
2744 XSetForeground (s->display, s->gc, xgcv.foreground);
2747 else if (s->face->underline_type == FACE_UNDER_LINE)
2749 unsigned long thickness, position;
2750 int y;
2752 if (s->prev && s->prev->face->underline_p
2753 && s->prev->face->underline_type == FACE_UNDER_LINE)
2755 /* We use the same underline style as the previous one. */
2756 thickness = s->prev->underline_thickness;
2757 position = s->prev->underline_position;
2759 else
2761 /* Get the underline thickness. Default is 1 pixel. */
2762 if (s->font && s->font->underline_thickness > 0)
2763 thickness = s->font->underline_thickness;
2764 else
2765 thickness = 1;
2766 if (x_underline_at_descent_line)
2767 position = (s->height - thickness) - (s->ybase - s->y);
2768 else
2770 /* Get the underline position. This is the recommended
2771 vertical offset in pixels from the baseline to the top of
2772 the underline. This is a signed value according to the
2773 specs, and its default is
2775 ROUND ((maximum descent) / 2), with
2776 ROUND(x) = floor (x + 0.5) */
2778 if (x_use_underline_position_properties
2779 && s->font && s->font->underline_position >= 0)
2780 position = s->font->underline_position;
2781 else if (s->font)
2782 position = (s->font->descent + 1) / 2;
2783 else
2784 position = underline_minimum_offset;
2786 position = max (position, underline_minimum_offset);
2788 /* Check the sanity of thickness and position. We should
2789 avoid drawing underline out of the current line area. */
2790 if (s->y + s->height <= s->ybase + position)
2791 position = (s->height - 1) - (s->ybase - s->y);
2792 if (s->y + s->height < s->ybase + position + thickness)
2793 thickness = (s->y + s->height) - (s->ybase + position);
2794 s->underline_thickness = thickness;
2795 s->underline_position = position;
2796 y = s->ybase + position;
2797 if (s->face->underline_defaulted_p)
2798 XFillRectangle (s->display, s->window, s->gc,
2799 s->x, y, s->width, thickness);
2800 else
2802 XGCValues xgcv;
2803 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2804 XSetForeground (s->display, s->gc, s->face->underline_color);
2805 XFillRectangle (s->display, s->window, s->gc,
2806 s->x, y, s->width, thickness);
2807 XSetForeground (s->display, s->gc, xgcv.foreground);
2811 /* Draw overline. */
2812 if (s->face->overline_p)
2814 unsigned long dy = 0, h = 1;
2816 if (s->face->overline_color_defaulted_p)
2817 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2818 s->width, h);
2819 else
2821 XGCValues xgcv;
2822 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2823 XSetForeground (s->display, s->gc, s->face->overline_color);
2824 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2825 s->width, h);
2826 XSetForeground (s->display, s->gc, xgcv.foreground);
2830 /* Draw strike-through. */
2831 if (s->face->strike_through_p)
2833 unsigned long h = 1;
2834 unsigned long dy = (s->height - h) / 2;
2836 if (s->face->strike_through_color_defaulted_p)
2837 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2838 s->width, h);
2839 else
2841 XGCValues xgcv;
2842 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2843 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2844 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2845 s->width, h);
2846 XSetForeground (s->display, s->gc, xgcv.foreground);
2850 /* Draw relief if not yet drawn. */
2851 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2852 x_draw_glyph_string_box (s);
2854 if (s->prev)
2856 struct glyph_string *prev;
2858 for (prev = s->prev; prev; prev = prev->prev)
2859 if (prev->hl != s->hl
2860 && prev->x + prev->width + prev->right_overhang > s->x)
2862 /* As prev was drawn while clipped to its own area, we
2863 must draw the right_overhang part using s->hl now. */
2864 enum draw_glyphs_face save = prev->hl;
2866 prev->hl = s->hl;
2867 x_set_glyph_string_gc (prev);
2868 x_set_glyph_string_clipping_exactly (s, prev);
2869 if (prev->first_glyph->type == CHAR_GLYPH)
2870 x_draw_glyph_string_foreground (prev);
2871 else
2872 x_draw_composite_glyph_string_foreground (prev);
2873 XSetClipMask (prev->display, prev->gc, None);
2874 prev->hl = save;
2875 prev->num_clips = 0;
2879 if (s->next)
2881 struct glyph_string *next;
2883 for (next = s->next; next; next = next->next)
2884 if (next->hl != s->hl
2885 && next->x - next->left_overhang < s->x + s->width)
2887 /* As next will be drawn while clipped to its own area,
2888 we must draw the left_overhang part using s->hl now. */
2889 enum draw_glyphs_face save = next->hl;
2891 next->hl = s->hl;
2892 x_set_glyph_string_gc (next);
2893 x_set_glyph_string_clipping_exactly (s, next);
2894 if (next->first_glyph->type == CHAR_GLYPH)
2895 x_draw_glyph_string_foreground (next);
2896 else
2897 x_draw_composite_glyph_string_foreground (next);
2898 XSetClipMask (next->display, next->gc, None);
2899 next->hl = save;
2900 next->num_clips = 0;
2901 next->clip_head = s->next;
2906 /* Reset clipping. */
2907 XSetClipMask (s->display, s->gc, None);
2908 s->num_clips = 0;
2911 /* Shift display to make room for inserted glyphs. */
2913 static void
2914 x_shift_glyphs_for_insert (struct frame *f, int x, int y, int width, int height, int shift_by)
2916 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2917 f->output_data.x->normal_gc,
2918 x, y, width, height,
2919 x + shift_by, y);
2922 /* Delete N glyphs at the nominal cursor position. Not implemented
2923 for X frames. */
2925 static void
2926 x_delete_glyphs (struct frame *f, register int n)
2928 emacs_abort ();
2932 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2933 If they are <= 0, this is probably an error. */
2935 void
2936 x_clear_area (Display *dpy, Window window, int x, int y, int width, int height)
2938 eassert (width > 0 && height > 0);
2939 XClearArea (dpy, window, x, y, width, height, False);
2943 /* Clear an entire frame. */
2945 static void
2946 x_clear_frame (struct frame *f)
2948 /* Clearing the frame will erase any cursor, so mark them all as no
2949 longer visible. */
2950 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2952 block_input ();
2954 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2956 /* We have to clear the scroll bars. If we have changed colors or
2957 something like that, then they should be notified. */
2958 x_scroll_bar_clear (f);
2960 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
2961 /* Make sure scroll bars are redrawn. As they aren't redrawn by
2962 redisplay, do it here. */
2963 if (FRAME_GTK_WIDGET (f))
2964 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
2965 #endif
2967 XFlush (FRAME_X_DISPLAY (f));
2969 unblock_input ();
2972 /* RIF: Show hourglass cursor on frame F. */
2974 static void
2975 x_show_hourglass (struct frame *f)
2977 Display *dpy = FRAME_X_DISPLAY (f);
2979 if (dpy)
2981 struct x_output *x = FRAME_X_OUTPUT (f);
2982 #ifdef USE_X_TOOLKIT
2983 if (x->widget)
2984 #else
2985 if (FRAME_OUTER_WINDOW (f))
2986 #endif
2988 x->hourglass_p = true;
2990 if (!x->hourglass_window)
2992 unsigned long mask = CWCursor;
2993 XSetWindowAttributes attrs;
2994 #ifdef USE_GTK
2995 Window parent = FRAME_X_WINDOW (f);
2996 #else
2997 Window parent = FRAME_OUTER_WINDOW (f);
2998 #endif
2999 attrs.cursor = x->hourglass_cursor;
3001 x->hourglass_window = XCreateWindow
3002 (dpy, parent, 0, 0, 32000, 32000, 0, 0,
3003 InputOnly, CopyFromParent, mask, &attrs);
3006 XMapRaised (dpy, x->hourglass_window);
3007 XFlush (dpy);
3012 /* RIF: Cancel hourglass cursor on frame F. */
3014 static void
3015 x_hide_hourglass (struct frame *f)
3017 struct x_output *x = FRAME_X_OUTPUT (f);
3019 /* Watch out for newly created frames. */
3020 if (x->hourglass_window)
3022 XUnmapWindow (FRAME_X_DISPLAY (f), x->hourglass_window);
3023 /* Sync here because XTread_socket looks at the
3024 hourglass_p flag that is reset to zero below. */
3025 XSync (FRAME_X_DISPLAY (f), False);
3026 x->hourglass_p = false;
3030 /* Invert the middle quarter of the frame for .15 sec. */
3032 static void
3033 XTflash (struct frame *f)
3035 block_input ();
3038 #ifdef USE_GTK
3039 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
3040 when the scroll bars and the edit widget share the same X window. */
3041 GdkWindow *window = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
3042 #ifdef HAVE_GTK3
3043 cairo_t *cr = gdk_cairo_create (window);
3044 cairo_set_source_rgb (cr, 1, 1, 1);
3045 cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
3046 #define XFillRectangle(d, win, gc, x, y, w, h) \
3047 do { \
3048 cairo_rectangle (cr, x, y, w, h); \
3049 cairo_fill (cr); \
3051 while (false)
3052 #else /* ! HAVE_GTK3 */
3053 GdkGCValues vals;
3054 GdkGC *gc;
3055 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
3056 ^ FRAME_BACKGROUND_PIXEL (f));
3057 vals.function = GDK_XOR;
3058 gc = gdk_gc_new_with_values (window,
3059 &vals, GDK_GC_FUNCTION | GDK_GC_FOREGROUND);
3060 #define XFillRectangle(d, win, gc, x, y, w, h) \
3061 gdk_draw_rectangle (window, gc, TRUE, x, y, w, h)
3062 #endif /* ! HAVE_GTK3 */
3063 #else /* ! USE_GTK */
3064 GC gc;
3066 /* Create a GC that will use the GXxor function to flip foreground
3067 pixels into background pixels. */
3069 XGCValues values;
3071 values.function = GXxor;
3072 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3073 ^ FRAME_BACKGROUND_PIXEL (f));
3075 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3076 GCFunction | GCForeground, &values);
3078 #endif
3080 /* Get the height not including a menu bar widget. */
3081 int height = FRAME_PIXEL_HEIGHT (f);
3082 /* Height of each line to flash. */
3083 int flash_height = FRAME_LINE_HEIGHT (f);
3084 /* These will be the left and right margins of the rectangles. */
3085 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3086 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3087 int width = flash_right - flash_left;
3089 /* If window is tall, flash top and bottom line. */
3090 if (height > 3 * FRAME_LINE_HEIGHT (f))
3092 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3093 flash_left,
3094 (FRAME_INTERNAL_BORDER_WIDTH (f)
3095 + FRAME_TOP_MARGIN_HEIGHT (f)),
3096 width, flash_height);
3097 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3098 flash_left,
3099 (height - flash_height
3100 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3101 width, flash_height);
3104 else
3105 /* If it is short, flash it all. */
3106 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3107 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3108 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3110 x_flush (f);
3113 struct timespec delay = make_timespec (0, 150 * 1000 * 1000);
3114 struct timespec wakeup = timespec_add (current_timespec (), delay);
3116 /* Keep waiting until past the time wakeup or any input gets
3117 available. */
3118 while (! detect_input_pending ())
3120 struct timespec current = current_timespec ();
3121 struct timespec timeout;
3123 /* Break if result would not be positive. */
3124 if (timespec_cmp (wakeup, current) <= 0)
3125 break;
3127 /* How long `select' should wait. */
3128 timeout = make_timespec (0, 10 * 1000 * 1000);
3130 /* Try to wait that long--but we might wake up sooner. */
3131 pselect (0, NULL, NULL, NULL, &timeout, NULL);
3135 /* If window is tall, flash top and bottom line. */
3136 if (height > 3 * FRAME_LINE_HEIGHT (f))
3138 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3139 flash_left,
3140 (FRAME_INTERNAL_BORDER_WIDTH (f)
3141 + FRAME_TOP_MARGIN_HEIGHT (f)),
3142 width, flash_height);
3143 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3144 flash_left,
3145 (height - flash_height
3146 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3147 width, flash_height);
3149 else
3150 /* If it is short, flash it all. */
3151 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3152 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3153 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3155 #ifdef USE_GTK
3156 #ifdef HAVE_GTK3
3157 cairo_destroy (cr);
3158 #else
3159 g_object_unref (G_OBJECT (gc));
3160 #endif
3161 #undef XFillRectangle
3162 #else
3163 XFreeGC (FRAME_X_DISPLAY (f), gc);
3164 #endif
3165 x_flush (f);
3169 unblock_input ();
3173 static void
3174 XTtoggle_invisible_pointer (struct frame *f, bool invisible)
3176 block_input ();
3177 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, invisible);
3178 unblock_input ();
3182 /* Make audible bell. */
3184 static void
3185 XTring_bell (struct frame *f)
3187 if (FRAME_X_DISPLAY (f))
3189 if (visible_bell)
3190 XTflash (f);
3191 else
3193 block_input ();
3194 #ifdef HAVE_XKB
3195 XkbBell (FRAME_X_DISPLAY (f), None, 0, None);
3196 #else
3197 XBell (FRAME_X_DISPLAY (f), 0);
3198 #endif
3199 XFlush (FRAME_X_DISPLAY (f));
3200 unblock_input ();
3205 /***********************************************************************
3206 Line Dance
3207 ***********************************************************************/
3209 /* Perform an insert-lines or delete-lines operation, inserting N
3210 lines or deleting -N lines at vertical position VPOS. */
3212 static void
3213 x_ins_del_lines (struct frame *f, int vpos, int n)
3215 emacs_abort ();
3219 /* Scroll part of the display as described by RUN. */
3221 static void
3222 x_scroll_run (struct window *w, struct run *run)
3224 struct frame *f = XFRAME (w->frame);
3225 int x, y, width, height, from_y, to_y, bottom_y;
3227 /* Get frame-relative bounding box of the text display area of W,
3228 without mode lines. Include in this box the left and right
3229 fringe of W. */
3230 window_box (w, ANY_AREA, &x, &y, &width, &height);
3232 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3233 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3234 bottom_y = y + height;
3236 if (to_y < from_y)
3238 /* Scrolling up. Make sure we don't copy part of the mode
3239 line at the bottom. */
3240 if (from_y + run->height > bottom_y)
3241 height = bottom_y - from_y;
3242 else
3243 height = run->height;
3245 else
3247 /* Scrolling down. Make sure we don't copy over the mode line.
3248 at the bottom. */
3249 if (to_y + run->height > bottom_y)
3250 height = bottom_y - to_y;
3251 else
3252 height = run->height;
3255 block_input ();
3257 /* Cursor off. Will be switched on again in x_update_window_end. */
3258 x_clear_cursor (w);
3260 XCopyArea (FRAME_X_DISPLAY (f),
3261 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3262 f->output_data.x->normal_gc,
3263 x, from_y,
3264 width, height,
3265 x, to_y);
3267 unblock_input ();
3272 /***********************************************************************
3273 Exposure Events
3274 ***********************************************************************/
3277 static void
3278 frame_highlight (struct frame *f)
3280 /* We used to only do this if Vx_no_window_manager was non-nil, but
3281 the ICCCM (section 4.1.6) says that the window's border pixmap
3282 and border pixel are window attributes which are "private to the
3283 client", so we can always change it to whatever we want. */
3284 block_input ();
3285 /* I recently started to get errors in this XSetWindowBorder, depending on
3286 the window-manager in use, tho something more is at play since I've been
3287 using that same window-manager binary for ever. Let's not crash just
3288 because of this (bug#9310). */
3289 x_catch_errors (FRAME_X_DISPLAY (f));
3290 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3291 f->output_data.x->border_pixel);
3292 x_uncatch_errors ();
3293 unblock_input ();
3294 x_update_cursor (f, true);
3295 x_set_frame_alpha (f);
3298 static void
3299 frame_unhighlight (struct frame *f)
3301 /* We used to only do this if Vx_no_window_manager was non-nil, but
3302 the ICCCM (section 4.1.6) says that the window's border pixmap
3303 and border pixel are window attributes which are "private to the
3304 client", so we can always change it to whatever we want. */
3305 block_input ();
3306 /* Same as above for XSetWindowBorder (bug#9310). */
3307 x_catch_errors (FRAME_X_DISPLAY (f));
3308 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3309 f->output_data.x->border_tile);
3310 x_uncatch_errors ();
3311 unblock_input ();
3312 x_update_cursor (f, true);
3313 x_set_frame_alpha (f);
3316 /* The focus has changed. Update the frames as necessary to reflect
3317 the new situation. Note that we can't change the selected frame
3318 here, because the Lisp code we are interrupting might become confused.
3319 Each event gets marked with the frame in which it occurred, so the
3320 Lisp code can tell when the switch took place by examining the events. */
3322 static void
3323 x_new_focus_frame (struct x_display_info *dpyinfo, struct frame *frame)
3325 struct frame *old_focus = dpyinfo->x_focus_frame;
3327 if (frame != dpyinfo->x_focus_frame)
3329 /* Set this before calling other routines, so that they see
3330 the correct value of x_focus_frame. */
3331 dpyinfo->x_focus_frame = frame;
3333 if (old_focus && old_focus->auto_lower)
3334 x_lower_frame (old_focus);
3336 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3337 dpyinfo->x_pending_autoraise_frame = dpyinfo->x_focus_frame;
3338 else
3339 dpyinfo->x_pending_autoraise_frame = NULL;
3342 x_frame_rehighlight (dpyinfo);
3345 /* Handle FocusIn and FocusOut state changes for FRAME.
3346 If FRAME has focus and there exists more than one frame, puts
3347 a FOCUS_IN_EVENT into *BUFP. */
3349 static void
3350 x_focus_changed (int type, int state, struct x_display_info *dpyinfo, struct frame *frame, struct input_event *bufp)
3352 if (type == FocusIn)
3354 if (dpyinfo->x_focus_event_frame != frame)
3356 x_new_focus_frame (dpyinfo, frame);
3357 dpyinfo->x_focus_event_frame = frame;
3359 /* Don't stop displaying the initial startup message
3360 for a switch-frame event we don't need. */
3361 /* When run as a daemon, Vterminal_frame is always NIL. */
3362 bufp->arg = (((NILP (Vterminal_frame)
3363 || ! FRAME_X_P (XFRAME (Vterminal_frame))
3364 || EQ (Fdaemonp (), Qt))
3365 && CONSP (Vframe_list)
3366 && !NILP (XCDR (Vframe_list)))
3367 ? Qt : Qnil);
3368 bufp->kind = FOCUS_IN_EVENT;
3369 XSETFRAME (bufp->frame_or_window, frame);
3372 frame->output_data.x->focus_state |= state;
3374 #ifdef HAVE_X_I18N
3375 if (FRAME_XIC (frame))
3376 XSetICFocus (FRAME_XIC (frame));
3377 #endif
3379 else if (type == FocusOut)
3381 frame->output_data.x->focus_state &= ~state;
3383 if (dpyinfo->x_focus_event_frame == frame)
3385 dpyinfo->x_focus_event_frame = 0;
3386 x_new_focus_frame (dpyinfo, 0);
3388 bufp->kind = FOCUS_OUT_EVENT;
3389 XSETFRAME (bufp->frame_or_window, frame);
3392 #ifdef HAVE_X_I18N
3393 if (FRAME_XIC (frame))
3394 XUnsetICFocus (FRAME_XIC (frame));
3395 #endif
3396 if (frame->pointer_invisible)
3397 XTtoggle_invisible_pointer (frame, false);
3401 /* Return the Emacs frame-object corresponding to an X window.
3402 It could be the frame's main window or an icon window. */
3404 static struct frame *
3405 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3407 Lisp_Object tail, frame;
3408 struct frame *f;
3410 if (wdesc == None)
3411 return NULL;
3413 FOR_EACH_FRAME (tail, frame)
3415 f = XFRAME (frame);
3416 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3417 continue;
3418 if (f->output_data.x->hourglass_window == wdesc)
3419 return f;
3420 #ifdef USE_X_TOOLKIT
3421 if ((f->output_data.x->edit_widget
3422 && XtWindow (f->output_data.x->edit_widget) == wdesc)
3423 /* A tooltip frame? */
3424 || (!f->output_data.x->edit_widget
3425 && FRAME_X_WINDOW (f) == wdesc)
3426 || f->output_data.x->icon_desc == wdesc)
3427 return f;
3428 #else /* not USE_X_TOOLKIT */
3429 #ifdef USE_GTK
3430 if (f->output_data.x->edit_widget)
3432 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3433 struct x_output *x = f->output_data.x;
3434 if (gwdesc != 0 && gwdesc == x->edit_widget)
3435 return f;
3437 #endif /* USE_GTK */
3438 if (FRAME_X_WINDOW (f) == wdesc
3439 || f->output_data.x->icon_desc == wdesc)
3440 return f;
3441 #endif /* not USE_X_TOOLKIT */
3443 return 0;
3446 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3448 /* Like x_window_to_frame but also compares the window with the widget's
3449 windows. */
3451 static struct frame *
3452 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3454 Lisp_Object tail, frame;
3455 struct frame *f, *found = NULL;
3456 struct x_output *x;
3458 if (wdesc == None)
3459 return NULL;
3461 FOR_EACH_FRAME (tail, frame)
3463 if (found)
3464 break;
3465 f = XFRAME (frame);
3466 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
3468 /* This frame matches if the window is any of its widgets. */
3469 x = f->output_data.x;
3470 if (x->hourglass_window == wdesc)
3471 found = f;
3472 else if (x->widget)
3474 #ifdef USE_GTK
3475 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3476 if (gwdesc != 0
3477 && gtk_widget_get_toplevel (gwdesc) == x->widget)
3478 found = f;
3479 #else
3480 if (wdesc == XtWindow (x->widget)
3481 || wdesc == XtWindow (x->column_widget)
3482 || wdesc == XtWindow (x->edit_widget))
3483 found = f;
3484 /* Match if the window is this frame's menubar. */
3485 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
3486 found = f;
3487 #endif
3489 else if (FRAME_X_WINDOW (f) == wdesc)
3490 /* A tooltip frame. */
3491 found = f;
3495 return found;
3498 /* Likewise, but consider only the menu bar widget. */
3500 static struct frame *
3501 x_menubar_window_to_frame (struct x_display_info *dpyinfo,
3502 const XEvent *event)
3504 Window wdesc = event->xany.window;
3505 Lisp_Object tail, frame;
3506 struct frame *f;
3507 struct x_output *x;
3509 if (wdesc == None)
3510 return NULL;
3512 FOR_EACH_FRAME (tail, frame)
3514 f = XFRAME (frame);
3515 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3516 continue;
3517 x = f->output_data.x;
3518 #ifdef USE_GTK
3519 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
3520 return f;
3521 #else
3522 /* Match if the window is this frame's menubar. */
3523 if (x->menubar_widget
3524 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
3525 return f;
3526 #endif
3528 return 0;
3531 /* Return the frame whose principal (outermost) window is WDESC.
3532 If WDESC is some other (smaller) window, we return 0. */
3534 struct frame *
3535 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3537 Lisp_Object tail, frame;
3538 struct frame *f;
3539 struct x_output *x;
3541 if (wdesc == None)
3542 return NULL;
3544 FOR_EACH_FRAME (tail, frame)
3546 f = XFRAME (frame);
3547 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3548 continue;
3549 x = f->output_data.x;
3551 if (x->widget)
3553 /* This frame matches if the window is its topmost widget. */
3554 #ifdef USE_GTK
3555 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3556 if (gwdesc == x->widget)
3557 return f;
3558 #else
3559 if (wdesc == XtWindow (x->widget))
3560 return f;
3561 #endif
3563 else if (FRAME_X_WINDOW (f) == wdesc)
3564 /* Tooltip frame. */
3565 return f;
3567 return 0;
3570 #else /* !USE_X_TOOLKIT && !USE_GTK */
3572 #define x_any_window_to_frame(d, i) x_window_to_frame (d, i)
3573 #define x_top_window_to_frame(d, i) x_window_to_frame (d, i)
3575 #endif /* USE_X_TOOLKIT || USE_GTK */
3577 /* The focus may have changed. Figure out if it is a real focus change,
3578 by checking both FocusIn/Out and Enter/LeaveNotify events.
3580 Returns FOCUS_IN_EVENT event in *BUFP. */
3582 static void
3583 x_detect_focus_change (struct x_display_info *dpyinfo, struct frame *frame,
3584 const XEvent *event, struct input_event *bufp)
3586 if (!frame)
3587 return;
3589 switch (event->type)
3591 case EnterNotify:
3592 case LeaveNotify:
3594 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3595 int focus_state
3596 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3598 if (event->xcrossing.detail != NotifyInferior
3599 && event->xcrossing.focus
3600 && ! (focus_state & FOCUS_EXPLICIT))
3601 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3602 FOCUS_IMPLICIT,
3603 dpyinfo, frame, bufp);
3605 break;
3607 case FocusIn:
3608 case FocusOut:
3609 x_focus_changed (event->type,
3610 (event->xfocus.detail == NotifyPointer ?
3611 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3612 dpyinfo, frame, bufp);
3613 break;
3615 case ClientMessage:
3616 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
3618 enum xembed_message msg = event->xclient.data.l[1];
3619 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
3620 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
3622 break;
3627 #if !defined USE_X_TOOLKIT && !defined USE_GTK
3628 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3630 void
3631 x_mouse_leave (struct x_display_info *dpyinfo)
3633 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3635 #endif
3637 /* The focus has changed, or we have redirected a frame's focus to
3638 another frame (this happens when a frame uses a surrogate
3639 mini-buffer frame). Shift the highlight as appropriate.
3641 The FRAME argument doesn't necessarily have anything to do with which
3642 frame is being highlighted or un-highlighted; we only use it to find
3643 the appropriate X display info. */
3645 static void
3646 XTframe_rehighlight (struct frame *frame)
3648 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
3651 static void
3652 x_frame_rehighlight (struct x_display_info *dpyinfo)
3654 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3656 if (dpyinfo->x_focus_frame)
3658 dpyinfo->x_highlight_frame
3659 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3660 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3661 : dpyinfo->x_focus_frame);
3662 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3664 fset_focus_frame (dpyinfo->x_focus_frame, Qnil);
3665 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3668 else
3669 dpyinfo->x_highlight_frame = 0;
3671 if (dpyinfo->x_highlight_frame != old_highlight)
3673 if (old_highlight)
3674 frame_unhighlight (old_highlight);
3675 if (dpyinfo->x_highlight_frame)
3676 frame_highlight (dpyinfo->x_highlight_frame);
3682 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3684 /* Initialize mode_switch_bit and modifier_meaning. */
3685 static void
3686 x_find_modifier_meanings (struct x_display_info *dpyinfo)
3688 int min_code, max_code;
3689 KeySym *syms;
3690 int syms_per_code;
3691 XModifierKeymap *mods;
3693 dpyinfo->meta_mod_mask = 0;
3694 dpyinfo->shift_lock_mask = 0;
3695 dpyinfo->alt_mod_mask = 0;
3696 dpyinfo->super_mod_mask = 0;
3697 dpyinfo->hyper_mod_mask = 0;
3699 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3701 syms = XGetKeyboardMapping (dpyinfo->display,
3702 min_code, max_code - min_code + 1,
3703 &syms_per_code);
3704 mods = XGetModifierMapping (dpyinfo->display);
3706 /* Scan the modifier table to see which modifier bits the Meta and
3707 Alt keysyms are on. */
3709 int row, col; /* The row and column in the modifier table. */
3710 bool found_alt_or_meta;
3712 for (row = 3; row < 8; row++)
3714 found_alt_or_meta = false;
3715 for (col = 0; col < mods->max_keypermod; col++)
3717 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3719 /* Zeroes are used for filler. Skip them. */
3720 if (code == 0)
3721 continue;
3723 /* Are any of this keycode's keysyms a meta key? */
3725 int code_col;
3727 for (code_col = 0; code_col < syms_per_code; code_col++)
3729 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3731 switch (sym)
3733 case XK_Meta_L:
3734 case XK_Meta_R:
3735 found_alt_or_meta = true;
3736 dpyinfo->meta_mod_mask |= (1 << row);
3737 break;
3739 case XK_Alt_L:
3740 case XK_Alt_R:
3741 found_alt_or_meta = true;
3742 dpyinfo->alt_mod_mask |= (1 << row);
3743 break;
3745 case XK_Hyper_L:
3746 case XK_Hyper_R:
3747 if (!found_alt_or_meta)
3748 dpyinfo->hyper_mod_mask |= (1 << row);
3749 code_col = syms_per_code;
3750 col = mods->max_keypermod;
3751 break;
3753 case XK_Super_L:
3754 case XK_Super_R:
3755 if (!found_alt_or_meta)
3756 dpyinfo->super_mod_mask |= (1 << row);
3757 code_col = syms_per_code;
3758 col = mods->max_keypermod;
3759 break;
3761 case XK_Shift_Lock:
3762 /* Ignore this if it's not on the lock modifier. */
3763 if (!found_alt_or_meta && ((1 << row) == LockMask))
3764 dpyinfo->shift_lock_mask = LockMask;
3765 code_col = syms_per_code;
3766 col = mods->max_keypermod;
3767 break;
3775 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3776 if (! dpyinfo->meta_mod_mask)
3778 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3779 dpyinfo->alt_mod_mask = 0;
3782 /* If some keys are both alt and meta,
3783 make them just meta, not alt. */
3784 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3786 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3789 XFree (syms);
3790 XFreeModifiermap (mods);
3793 /* Convert between the modifier bits X uses and the modifier bits
3794 Emacs uses. */
3797 x_x_to_emacs_modifiers (struct x_display_info *dpyinfo, int state)
3799 int mod_meta = meta_modifier;
3800 int mod_alt = alt_modifier;
3801 int mod_hyper = hyper_modifier;
3802 int mod_super = super_modifier;
3803 Lisp_Object tem;
3805 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3806 if (INTEGERP (tem)) mod_alt = XINT (tem) & INT_MAX;
3807 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3808 if (INTEGERP (tem)) mod_meta = XINT (tem) & INT_MAX;
3809 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3810 if (INTEGERP (tem)) mod_hyper = XINT (tem) & INT_MAX;
3811 tem = Fget (Vx_super_keysym, Qmodifier_value);
3812 if (INTEGERP (tem)) mod_super = XINT (tem) & INT_MAX;
3814 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3815 | ((state & ControlMask) ? ctrl_modifier : 0)
3816 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3817 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3818 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3819 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3822 static int
3823 x_emacs_to_x_modifiers (struct x_display_info *dpyinfo, EMACS_INT state)
3825 EMACS_INT mod_meta = meta_modifier;
3826 EMACS_INT mod_alt = alt_modifier;
3827 EMACS_INT mod_hyper = hyper_modifier;
3828 EMACS_INT mod_super = super_modifier;
3830 Lisp_Object tem;
3832 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3833 if (INTEGERP (tem)) mod_alt = XINT (tem);
3834 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3835 if (INTEGERP (tem)) mod_meta = XINT (tem);
3836 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3837 if (INTEGERP (tem)) mod_hyper = XINT (tem);
3838 tem = Fget (Vx_super_keysym, Qmodifier_value);
3839 if (INTEGERP (tem)) mod_super = XINT (tem);
3842 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3843 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3844 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3845 | ((state & shift_modifier) ? ShiftMask : 0)
3846 | ((state & ctrl_modifier) ? ControlMask : 0)
3847 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3850 /* Convert a keysym to its name. */
3852 char *
3853 x_get_keysym_name (int keysym)
3855 char *value;
3857 block_input ();
3858 value = XKeysymToString (keysym);
3859 unblock_input ();
3861 return value;
3864 /* Mouse clicks and mouse movement. Rah.
3866 Formerly, we used PointerMotionHintMask (in standard_event_mask)
3867 so that we would have to call XQueryPointer after each MotionNotify
3868 event to ask for another such event. However, this made mouse tracking
3869 slow, and there was a bug that made it eventually stop.
3871 Simply asking for MotionNotify all the time seems to work better.
3873 In order to avoid asking for motion events and then throwing most
3874 of them away or busy-polling the server for mouse positions, we ask
3875 the server for pointer motion hints. This means that we get only
3876 one event per group of mouse movements. "Groups" are delimited by
3877 other kinds of events (focus changes and button clicks, for
3878 example), or by XQueryPointer calls; when one of these happens, we
3879 get another MotionNotify event the next time the mouse moves. This
3880 is at least as efficient as getting motion events when mouse
3881 tracking is on, and I suspect only negligibly worse when tracking
3882 is off. */
3884 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3886 If the event is a button press, then note that we have grabbed
3887 the mouse. */
3889 static Lisp_Object
3890 construct_mouse_click (struct input_event *result,
3891 const XButtonEvent *event,
3892 struct frame *f)
3894 /* Make the event type NO_EVENT; we'll change that when we decide
3895 otherwise. */
3896 result->kind = MOUSE_CLICK_EVENT;
3897 result->code = event->button - Button1;
3898 result->timestamp = event->time;
3899 result->modifiers = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f),
3900 event->state)
3901 | (event->type == ButtonRelease
3902 ? up_modifier
3903 : down_modifier));
3905 XSETINT (result->x, event->x);
3906 XSETINT (result->y, event->y);
3907 XSETFRAME (result->frame_or_window, f);
3908 result->arg = Qnil;
3909 return Qnil;
3912 /* Function to report a mouse movement to the mainstream Emacs code.
3913 The input handler calls this.
3915 We have received a mouse movement event, which is given in *event.
3916 If the mouse is over a different glyph than it was last time, tell
3917 the mainstream emacs code by setting mouse_moved. If not, ask for
3918 another motion event, so we can check again the next time it moves. */
3920 static bool
3921 note_mouse_movement (struct frame *frame, const XMotionEvent *event)
3923 XRectangle *r;
3924 struct x_display_info *dpyinfo;
3926 if (!FRAME_X_OUTPUT (frame))
3927 return false;
3929 dpyinfo = FRAME_DISPLAY_INFO (frame);
3930 dpyinfo->last_mouse_movement_time = event->time;
3931 dpyinfo->last_mouse_motion_frame = frame;
3932 dpyinfo->last_mouse_motion_x = event->x;
3933 dpyinfo->last_mouse_motion_y = event->y;
3935 if (event->window != FRAME_X_WINDOW (frame))
3937 frame->mouse_moved = true;
3938 dpyinfo->last_mouse_scroll_bar = NULL;
3939 note_mouse_highlight (frame, -1, -1);
3940 dpyinfo->last_mouse_glyph_frame = NULL;
3941 return true;
3945 /* Has the mouse moved off the glyph it was on at the last sighting? */
3946 r = &dpyinfo->last_mouse_glyph;
3947 if (frame != dpyinfo->last_mouse_glyph_frame
3948 || event->x < r->x || event->x >= r->x + r->width
3949 || event->y < r->y || event->y >= r->y + r->height)
3951 frame->mouse_moved = true;
3952 dpyinfo->last_mouse_scroll_bar = NULL;
3953 note_mouse_highlight (frame, event->x, event->y);
3954 /* Remember which glyph we're now on. */
3955 remember_mouse_glyph (frame, event->x, event->y, r);
3956 dpyinfo->last_mouse_glyph_frame = frame;
3957 return true;
3960 return false;
3963 /* Return the current position of the mouse.
3964 *FP should be a frame which indicates which display to ask about.
3966 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3967 and *PART to the frame, window, and scroll bar part that the mouse
3968 is over. Set *X and *Y to the portion and whole of the mouse's
3969 position on the scroll bar.
3971 If the mouse movement started elsewhere, set *FP to the frame the
3972 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3973 the mouse is over.
3975 Set *TIMESTAMP to the server time-stamp for the time at which the mouse
3976 was at this position.
3978 Don't store anything if we don't have a valid set of values to report.
3980 This clears the mouse_moved flag, so we can wait for the next mouse
3981 movement. */
3983 static void
3984 XTmouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
3985 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
3986 Time *timestamp)
3988 struct frame *f1;
3989 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
3991 block_input ();
3993 if (dpyinfo->last_mouse_scroll_bar && insist == 0)
3995 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
3997 if (bar->horizontal)
3998 x_horizontal_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
3999 else
4000 x_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
4002 else
4004 Window root;
4005 int root_x, root_y;
4007 Window dummy_window;
4008 int dummy;
4010 Lisp_Object frame, tail;
4012 /* Clear the mouse-moved flag for every frame on this display. */
4013 FOR_EACH_FRAME (tail, frame)
4014 if (FRAME_X_P (XFRAME (frame))
4015 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
4016 XFRAME (frame)->mouse_moved = false;
4018 dpyinfo->last_mouse_scroll_bar = NULL;
4020 /* Figure out which root window we're on. */
4021 XQueryPointer (FRAME_X_DISPLAY (*fp),
4022 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
4024 /* The root window which contains the pointer. */
4025 &root,
4027 /* Trash which we can't trust if the pointer is on
4028 a different screen. */
4029 &dummy_window,
4031 /* The position on that root window. */
4032 &root_x, &root_y,
4034 /* More trash we can't trust. */
4035 &dummy, &dummy,
4037 /* Modifier keys and pointer buttons, about which
4038 we don't care. */
4039 (unsigned int *) &dummy);
4041 /* Now we have a position on the root; find the innermost window
4042 containing the pointer. */
4044 Window win, child;
4045 int win_x, win_y;
4046 int parent_x = 0, parent_y = 0;
4048 win = root;
4050 /* XTranslateCoordinates can get errors if the window
4051 structure is changing at the same time this function
4052 is running. So at least we must not crash from them. */
4054 x_catch_errors (FRAME_X_DISPLAY (*fp));
4056 if (x_mouse_grabbed (dpyinfo))
4058 /* If mouse was grabbed on a frame, give coords for that frame
4059 even if the mouse is now outside it. */
4060 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
4062 /* From-window. */
4063 root,
4065 /* To-window. */
4066 FRAME_X_WINDOW (dpyinfo->last_mouse_frame),
4068 /* From-position, to-position. */
4069 root_x, root_y, &win_x, &win_y,
4071 /* Child of win. */
4072 &child);
4073 f1 = dpyinfo->last_mouse_frame;
4075 else
4077 while (true)
4079 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
4081 /* From-window, to-window. */
4082 root, win,
4084 /* From-position, to-position. */
4085 root_x, root_y, &win_x, &win_y,
4087 /* Child of win. */
4088 &child);
4090 if (child == None || child == win)
4091 break;
4092 #ifdef USE_GTK
4093 /* We don't wan't to know the innermost window. We
4094 want the edit window. For non-Gtk+ the innermost
4095 window is the edit window. For Gtk+ it might not
4096 be. It might be the tool bar for example. */
4097 if (x_window_to_frame (dpyinfo, win))
4098 break;
4099 #endif
4100 win = child;
4101 parent_x = win_x;
4102 parent_y = win_y;
4105 /* Now we know that:
4106 win is the innermost window containing the pointer
4107 (XTC says it has no child containing the pointer),
4108 win_x and win_y are the pointer's position in it
4109 (XTC did this the last time through), and
4110 parent_x and parent_y are the pointer's position in win's parent.
4111 (They are what win_x and win_y were when win was child.
4112 If win is the root window, it has no parent, and
4113 parent_{x,y} are invalid, but that's okay, because we'll
4114 never use them in that case.) */
4116 #ifdef USE_GTK
4117 /* We don't wan't to know the innermost window. We
4118 want the edit window. */
4119 f1 = x_window_to_frame (dpyinfo, win);
4120 #else
4121 /* Is win one of our frames? */
4122 f1 = x_any_window_to_frame (dpyinfo, win);
4123 #endif
4125 #ifdef USE_X_TOOLKIT
4126 /* If we end up with the menu bar window, say it's not
4127 on the frame. */
4128 if (f1 != NULL
4129 && f1->output_data.x->menubar_widget
4130 && win == XtWindow (f1->output_data.x->menubar_widget))
4131 f1 = NULL;
4132 #endif /* USE_X_TOOLKIT */
4135 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
4136 f1 = 0;
4138 x_uncatch_errors ();
4140 /* If not, is it one of our scroll bars? */
4141 if (! f1)
4143 struct scroll_bar *bar;
4145 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win, 2);
4147 if (bar)
4149 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4150 win_x = parent_x;
4151 win_y = parent_y;
4155 if (f1 == 0 && insist > 0)
4156 f1 = SELECTED_FRAME ();
4158 if (f1)
4160 /* Ok, we found a frame. Store all the values.
4161 last_mouse_glyph is a rectangle used to reduce the
4162 generation of mouse events. To not miss any motion
4163 events, we must divide the frame into rectangles of the
4164 size of the smallest character that could be displayed
4165 on it, i.e. into the same rectangles that matrices on
4166 the frame are divided into. */
4168 /* FIXME: what if F1 is not an X frame? */
4169 dpyinfo = FRAME_DISPLAY_INFO (f1);
4170 remember_mouse_glyph (f1, win_x, win_y, &dpyinfo->last_mouse_glyph);
4171 dpyinfo->last_mouse_glyph_frame = f1;
4173 *bar_window = Qnil;
4174 *part = scroll_bar_above_handle;
4175 *fp = f1;
4176 XSETINT (*x, win_x);
4177 XSETINT (*y, win_y);
4178 *timestamp = dpyinfo->last_mouse_movement_time;
4183 unblock_input ();
4188 /***********************************************************************
4189 Scroll bars
4190 ***********************************************************************/
4192 /* Scroll bar support. */
4194 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
4195 manages it.
4196 This can be called in GC, so we have to make sure to strip off mark
4197 bits. */
4199 static struct scroll_bar *
4200 x_window_to_scroll_bar (Display *display, Window window_id, int type)
4202 Lisp_Object tail, frame;
4204 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4205 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4206 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4208 FOR_EACH_FRAME (tail, frame)
4210 Lisp_Object bar, condemned;
4212 if (! FRAME_X_P (XFRAME (frame)))
4213 continue;
4215 /* Scan this frame's scroll bar list for a scroll bar with the
4216 right window ID. */
4217 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4218 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4219 /* This trick allows us to search both the ordinary and
4220 condemned scroll bar lists with one loop. */
4221 ! NILP (bar) || (bar = condemned,
4222 condemned = Qnil,
4223 ! NILP (bar));
4224 bar = XSCROLL_BAR (bar)->next)
4225 if (XSCROLL_BAR (bar)->x_window == window_id
4226 && FRAME_X_DISPLAY (XFRAME (frame)) == display
4227 && (type = 2
4228 || (type == 1 && XSCROLL_BAR (bar)->horizontal)
4229 || (type == 0 && !XSCROLL_BAR (bar)->horizontal)))
4230 return XSCROLL_BAR (bar);
4233 return NULL;
4237 #if defined USE_LUCID
4239 /* Return the Lucid menu bar WINDOW is part of. Return null
4240 if WINDOW is not part of a menu bar. */
4242 static Widget
4243 x_window_to_menu_bar (Window window)
4245 Lisp_Object tail, frame;
4247 FOR_EACH_FRAME (tail, frame)
4248 if (FRAME_X_P (XFRAME (frame)))
4250 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4252 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4253 return menu_bar;
4255 return NULL;
4258 #endif /* USE_LUCID */
4261 /************************************************************************
4262 Toolkit scroll bars
4263 ************************************************************************/
4265 #ifdef USE_TOOLKIT_SCROLL_BARS
4267 static void x_send_scroll_bar_event (Lisp_Object, enum scroll_bar_part,
4268 int, int, bool);
4270 /* Lisp window being scrolled. Set when starting to interact with
4271 a toolkit scroll bar, reset to nil when ending the interaction. */
4273 static Lisp_Object window_being_scrolled;
4275 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4276 that movements of 1/20 of the screen size are mapped to up/down. */
4278 #ifndef USE_GTK
4279 /* Id of action hook installed for scroll bars. */
4281 static XtActionHookId action_hook_id;
4282 static XtActionHookId horizontal_action_hook_id;
4284 static Boolean xaw3d_arrow_scroll;
4286 /* Whether the drag scrolling maintains the mouse at the top of the
4287 thumb. If not, resizing the thumb needs to be done more carefully
4288 to avoid jerkiness. */
4290 static Boolean xaw3d_pick_top;
4292 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4293 bars are used.. The hook is responsible for detecting when
4294 the user ends an interaction with the scroll bar, and generates
4295 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4297 static void
4298 xt_action_hook (Widget widget, XtPointer client_data, String action_name,
4299 XEvent *event, String *params, Cardinal *num_params)
4301 bool scroll_bar_p;
4302 const char *end_action;
4304 #ifdef USE_MOTIF
4305 scroll_bar_p = XmIsScrollBar (widget);
4306 end_action = "Release";
4307 #else /* !USE_MOTIF i.e. use Xaw */
4308 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4309 end_action = "EndScroll";
4310 #endif /* USE_MOTIF */
4312 if (scroll_bar_p
4313 && strcmp (action_name, end_action) == 0
4314 && WINDOWP (window_being_scrolled))
4316 struct window *w;
4317 struct scroll_bar *bar;
4319 x_send_scroll_bar_event (window_being_scrolled,
4320 scroll_bar_end_scroll, 0, 0, false);
4321 w = XWINDOW (window_being_scrolled);
4322 bar = XSCROLL_BAR (w->vertical_scroll_bar);
4324 if (bar->dragging != -1)
4326 bar->dragging = -1;
4327 /* The thumb size is incorrect while dragging: fix it. */
4328 set_vertical_scroll_bar (w);
4330 window_being_scrolled = Qnil;
4331 #if defined (USE_LUCID)
4332 bar->last_seen_part = scroll_bar_nowhere;
4333 #endif
4334 /* Xt timeouts no longer needed. */
4335 toolkit_scroll_bar_interaction = false;
4340 static void
4341 xt_horizontal_action_hook (Widget widget, XtPointer client_data, String action_name,
4342 XEvent *event, String *params, Cardinal *num_params)
4344 bool scroll_bar_p;
4345 const char *end_action;
4347 #ifdef USE_MOTIF
4348 scroll_bar_p = XmIsScrollBar (widget);
4349 end_action = "Release";
4350 #else /* !USE_MOTIF i.e. use Xaw */
4351 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4352 end_action = "EndScroll";
4353 #endif /* USE_MOTIF */
4355 if (scroll_bar_p
4356 && strcmp (action_name, end_action) == 0
4357 && WINDOWP (window_being_scrolled))
4359 struct window *w;
4360 struct scroll_bar *bar;
4362 x_send_scroll_bar_event (window_being_scrolled,
4363 scroll_bar_end_scroll, 0, 0, true);
4364 w = XWINDOW (window_being_scrolled);
4365 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
4367 if (bar->dragging != -1)
4369 bar->dragging = -1;
4370 /* The thumb size is incorrect while dragging: fix it. */
4371 set_horizontal_scroll_bar (w);
4373 window_being_scrolled = Qnil;
4374 #if defined (USE_LUCID)
4375 bar->last_seen_part = scroll_bar_nowhere;
4376 #endif
4377 /* Xt timeouts no longer needed. */
4378 toolkit_scroll_bar_interaction = false;
4381 #endif /* not USE_GTK */
4383 /* Send a client message with message type Xatom_Scrollbar for a
4384 scroll action to the frame of WINDOW. PART is a value identifying
4385 the part of the scroll bar that was clicked on. PORTION is the
4386 amount to scroll of a whole of WHOLE. */
4388 static void
4389 x_send_scroll_bar_event (Lisp_Object window, enum scroll_bar_part part,
4390 int portion, int whole, bool horizontal)
4392 XEvent event;
4393 XClientMessageEvent *ev = &event.xclient;
4394 struct window *w = XWINDOW (window);
4395 struct frame *f = XFRAME (w->frame);
4396 intptr_t iw = (intptr_t) w;
4397 enum { BITS_PER_INTPTR = CHAR_BIT * sizeof iw };
4398 verify (BITS_PER_INTPTR <= 64);
4399 int sign_shift = BITS_PER_INTPTR - 32;
4401 block_input ();
4403 /* Construct a ClientMessage event to send to the frame. */
4404 ev->type = ClientMessage;
4405 ev->message_type = (horizontal
4406 ? FRAME_DISPLAY_INFO (f)->Xatom_Horizontal_Scrollbar
4407 : FRAME_DISPLAY_INFO (f)->Xatom_Scrollbar);
4408 ev->display = FRAME_X_DISPLAY (f);
4409 ev->window = FRAME_X_WINDOW (f);
4410 ev->format = 32;
4412 /* A 32-bit X client on a 64-bit X server can pass a window pointer
4413 as-is. A 64-bit client on a 32-bit X server is in trouble
4414 because a pointer does not fit and would be truncated while
4415 passing through the server. So use two slots and hope that X12
4416 will resolve such issues someday. */
4417 ev->data.l[0] = iw >> 31 >> 1;
4418 ev->data.l[1] = sign_shift <= 0 ? iw : iw << sign_shift >> sign_shift;
4419 ev->data.l[2] = part;
4420 ev->data.l[3] = portion;
4421 ev->data.l[4] = whole;
4423 /* Make Xt timeouts work while the scroll bar is active. */
4424 #ifdef USE_X_TOOLKIT
4425 toolkit_scroll_bar_interaction = true;
4426 x_activate_timeout_atimer ();
4427 #endif
4429 /* Setting the event mask to zero means that the message will
4430 be sent to the client that created the window, and if that
4431 window no longer exists, no event will be sent. */
4432 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4433 unblock_input ();
4437 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4438 in *IEVENT. */
4440 static void
4441 x_scroll_bar_to_input_event (const XEvent *event,
4442 struct input_event *ievent)
4444 const XClientMessageEvent *ev = &event->xclient;
4445 Lisp_Object window;
4446 struct window *w;
4448 /* See the comment in the function above. */
4449 intptr_t iw0 = ev->data.l[0];
4450 intptr_t iw1 = ev->data.l[1];
4451 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
4452 w = (struct window *) iw;
4454 XSETWINDOW (window, w);
4456 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4457 ievent->frame_or_window = window;
4458 ievent->arg = Qnil;
4459 #ifdef USE_GTK
4460 ievent->timestamp = CurrentTime;
4461 #else
4462 ievent->timestamp =
4463 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
4464 #endif
4465 ievent->code = 0;
4466 ievent->part = ev->data.l[2];
4467 ievent->x = make_number (ev->data.l[3]);
4468 ievent->y = make_number (ev->data.l[4]);
4469 ievent->modifiers = 0;
4472 /* Transform a horizontal scroll bar ClientMessage EVENT to an Emacs
4473 input event in *IEVENT. */
4475 static void
4476 x_horizontal_scroll_bar_to_input_event (const XEvent *event,
4477 struct input_event *ievent)
4479 const XClientMessageEvent *ev = &event->xclient;
4480 Lisp_Object window;
4481 struct window *w;
4483 /* See the comment in the function above. */
4484 intptr_t iw0 = ev->data.l[0];
4485 intptr_t iw1 = ev->data.l[1];
4486 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
4487 w = (struct window *) iw;
4489 XSETWINDOW (window, w);
4491 ievent->kind = HORIZONTAL_SCROLL_BAR_CLICK_EVENT;
4492 ievent->frame_or_window = window;
4493 ievent->arg = Qnil;
4494 #ifdef USE_GTK
4495 ievent->timestamp = CurrentTime;
4496 #else
4497 ievent->timestamp =
4498 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
4499 #endif
4500 ievent->code = 0;
4501 ievent->part = ev->data.l[2];
4502 ievent->x = make_number (ev->data.l[3]);
4503 ievent->y = make_number (ev->data.l[4]);
4504 ievent->modifiers = 0;
4508 #ifdef USE_MOTIF
4510 /* Minimum and maximum values used for Motif scroll bars. */
4512 #define XM_SB_MAX 10000000
4514 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4515 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4516 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4518 static void
4519 xm_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4521 struct scroll_bar *bar = client_data;
4522 XmScrollBarCallbackStruct *cs = call_data;
4523 enum scroll_bar_part part = scroll_bar_nowhere;
4524 bool horizontal = bar->horizontal;
4525 int whole = 0, portion = 0;
4527 switch (cs->reason)
4529 case XmCR_DECREMENT:
4530 bar->dragging = -1;
4531 part = horizontal ? scroll_bar_left_arrow : scroll_bar_up_arrow;
4532 break;
4534 case XmCR_INCREMENT:
4535 bar->dragging = -1;
4536 part = horizontal ? scroll_bar_right_arrow : scroll_bar_down_arrow;
4537 break;
4539 case XmCR_PAGE_DECREMENT:
4540 bar->dragging = -1;
4541 part = horizontal ? scroll_bar_before_handle : scroll_bar_above_handle;
4542 break;
4544 case XmCR_PAGE_INCREMENT:
4545 bar->dragging = -1;
4546 part = horizontal ? scroll_bar_after_handle : scroll_bar_below_handle;
4547 break;
4549 case XmCR_TO_TOP:
4550 bar->dragging = -1;
4551 part = horizontal ? scroll_bar_to_leftmost : scroll_bar_to_top;
4552 break;
4554 case XmCR_TO_BOTTOM:
4555 bar->dragging = -1;
4556 part = horizontal ? scroll_bar_to_rightmost : scroll_bar_to_bottom;
4557 break;
4559 case XmCR_DRAG:
4561 int slider_size;
4563 block_input ();
4564 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4565 unblock_input ();
4567 if (horizontal)
4569 portion = bar->whole * ((float)cs->value / XM_SB_MAX);
4570 whole = bar->whole * ((float)(XM_SB_MAX - slider_size) / XM_SB_MAX);
4571 portion = min (portion, whole);
4572 part = scroll_bar_horizontal_handle;
4574 else
4576 whole = XM_SB_MAX - slider_size;
4577 portion = min (cs->value, whole);
4578 part = scroll_bar_handle;
4581 bar->dragging = cs->value;
4583 break;
4585 case XmCR_VALUE_CHANGED:
4586 break;
4589 if (part != scroll_bar_nowhere)
4591 window_being_scrolled = bar->window;
4592 x_send_scroll_bar_event (bar->window, part, portion, whole,
4593 bar->horizontal);
4597 #elif defined USE_GTK
4599 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4600 bar widget. DATA is a pointer to the scroll_bar structure. */
4602 static gboolean
4603 xg_scroll_callback (GtkRange *range,
4604 GtkScrollType scroll,
4605 gdouble value,
4606 gpointer user_data)
4608 int whole = 0, portion = 0;
4609 struct scroll_bar *bar = user_data;
4610 enum scroll_bar_part part = scroll_bar_nowhere;
4611 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
4612 struct frame *f = g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
4614 if (xg_ignore_gtk_scrollbar) return FALSE;
4616 switch (scroll)
4618 case GTK_SCROLL_JUMP:
4619 /* Buttons 1 2 or 3 must be grabbed. */
4620 if (FRAME_DISPLAY_INFO (f)->grabbed != 0
4621 && FRAME_DISPLAY_INFO (f)->grabbed < (1 << 4))
4623 if (bar->horizontal)
4625 part = scroll_bar_horizontal_handle;
4626 whole = (int)(gtk_adjustment_get_upper (adj) -
4627 gtk_adjustment_get_page_size (adj));
4628 portion = min ((int)value, whole);
4629 bar->dragging = portion;
4631 else
4633 part = scroll_bar_handle;
4634 whole = gtk_adjustment_get_upper (adj) -
4635 gtk_adjustment_get_page_size (adj);
4636 portion = min ((int)value, whole);
4637 bar->dragging = portion;
4640 break;
4641 case GTK_SCROLL_STEP_BACKWARD:
4642 part = (bar->horizontal
4643 ? scroll_bar_left_arrow : scroll_bar_up_arrow);
4644 bar->dragging = -1;
4645 break;
4646 case GTK_SCROLL_STEP_FORWARD:
4647 part = (bar->horizontal
4648 ? scroll_bar_right_arrow : scroll_bar_down_arrow);
4649 bar->dragging = -1;
4650 break;
4651 case GTK_SCROLL_PAGE_BACKWARD:
4652 part = (bar->horizontal
4653 ? scroll_bar_before_handle : scroll_bar_above_handle);
4654 bar->dragging = -1;
4655 break;
4656 case GTK_SCROLL_PAGE_FORWARD:
4657 part = (bar->horizontal
4658 ? scroll_bar_after_handle : scroll_bar_below_handle);
4659 bar->dragging = -1;
4660 break;
4663 if (part != scroll_bar_nowhere)
4665 window_being_scrolled = bar->window;
4666 x_send_scroll_bar_event (bar->window, part, portion, whole,
4667 bar->horizontal);
4670 return FALSE;
4673 /* Callback for button release. Sets dragging to -1 when dragging is done. */
4675 static gboolean
4676 xg_end_scroll_callback (GtkWidget *widget,
4677 GdkEventButton *event,
4678 gpointer user_data)
4680 struct scroll_bar *bar = user_data;
4681 bar->dragging = -1;
4682 if (WINDOWP (window_being_scrolled))
4684 x_send_scroll_bar_event (window_being_scrolled,
4685 scroll_bar_end_scroll, 0, 0, bar->horizontal);
4686 window_being_scrolled = Qnil;
4689 return FALSE;
4693 #else /* not USE_GTK and not USE_MOTIF */
4695 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4696 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4697 scroll bar struct. CALL_DATA is a pointer to a float saying where
4698 the thumb is. */
4700 static void
4701 xaw_jump_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4703 struct scroll_bar *bar = client_data;
4704 float *top_addr = call_data;
4705 float top = *top_addr;
4706 float shown;
4707 int whole, portion, height, width;
4708 enum scroll_bar_part part;
4709 bool horizontal = bar->horizontal;
4712 if (horizontal)
4714 /* Get the size of the thumb, a value between 0 and 1. */
4715 block_input ();
4716 XtVaGetValues (widget, XtNshown, &shown, XtNwidth, &width, NULL);
4717 unblock_input ();
4719 if (shown < 1)
4721 whole = bar->whole - (shown * bar->whole);
4722 portion = min (top * bar->whole, whole);
4724 else
4726 whole = bar->whole;
4727 portion = 0;
4730 part = scroll_bar_horizontal_handle;
4732 else
4734 /* Get the size of the thumb, a value between 0 and 1. */
4735 block_input ();
4736 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4737 unblock_input ();
4739 whole = 10000000;
4740 portion = shown < 1 ? top * whole : 0;
4742 if (shown < 1 && (eabs (top + shown - 1) < 1.0f / height))
4743 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4744 the bottom, so we force the scrolling whenever we see that we're
4745 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4746 we try to ensure that we always stay two pixels away from the
4747 bottom). */
4748 part = scroll_bar_down_arrow;
4749 else
4750 part = scroll_bar_handle;
4753 window_being_scrolled = bar->window;
4754 bar->dragging = portion;
4755 bar->last_seen_part = part;
4756 x_send_scroll_bar_event (bar->window, part, portion, whole, bar->horizontal);
4760 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4761 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4762 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4763 the scroll bar. CALL_DATA is an integer specifying the action that
4764 has taken place. Its magnitude is in the range 0..height of the
4765 scroll bar. Negative values mean scroll towards buffer start.
4766 Values < height of scroll bar mean line-wise movement. */
4768 static void
4769 xaw_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4771 struct scroll_bar *bar = client_data;
4772 /* The position really is stored cast to a pointer. */
4773 int position = (intptr_t) call_data;
4774 Dimension height, width;
4775 enum scroll_bar_part part;
4777 if (bar->horizontal)
4779 /* Get the width of the scroll bar. */
4780 block_input ();
4781 XtVaGetValues (widget, XtNwidth, &width, NULL);
4782 unblock_input ();
4784 if (eabs (position) >= width)
4785 part = (position < 0) ? scroll_bar_before_handle : scroll_bar_after_handle;
4787 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4788 it maps line-movement to call_data = max(5, height/20). */
4789 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, width / 20))
4790 part = (position < 0) ? scroll_bar_left_arrow : scroll_bar_right_arrow;
4791 else
4792 part = scroll_bar_move_ratio;
4794 window_being_scrolled = bar->window;
4795 bar->dragging = -1;
4796 bar->last_seen_part = part;
4797 x_send_scroll_bar_event (bar->window, part, position, width,
4798 bar->horizontal);
4800 else
4803 /* Get the height of the scroll bar. */
4804 block_input ();
4805 XtVaGetValues (widget, XtNheight, &height, NULL);
4806 unblock_input ();
4808 if (eabs (position) >= height)
4809 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4811 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4812 it maps line-movement to call_data = max(5, height/20). */
4813 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
4814 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4815 else
4816 part = scroll_bar_move_ratio;
4818 window_being_scrolled = bar->window;
4819 bar->dragging = -1;
4820 bar->last_seen_part = part;
4821 x_send_scroll_bar_event (bar->window, part, position, height,
4822 bar->horizontal);
4826 #endif /* not USE_GTK and not USE_MOTIF */
4828 #define SCROLL_BAR_NAME "verticalScrollBar"
4829 #define SCROLL_BAR_HORIZONTAL_NAME "horizontalScrollBar"
4831 /* Create the widget for scroll bar BAR on frame F. Record the widget
4832 and X window of the scroll bar in BAR. */
4834 #ifdef USE_GTK
4835 static void
4836 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
4838 const char *scroll_bar_name = SCROLL_BAR_NAME;
4840 block_input ();
4841 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4842 G_CALLBACK (xg_end_scroll_callback),
4843 scroll_bar_name);
4844 unblock_input ();
4847 static void
4848 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
4850 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
4852 block_input ();
4853 xg_create_horizontal_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4854 G_CALLBACK (xg_end_scroll_callback),
4855 scroll_bar_name);
4856 unblock_input ();
4859 #else /* not USE_GTK */
4861 static void
4862 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
4864 Window xwindow;
4865 Widget widget;
4866 Arg av[20];
4867 int ac = 0;
4868 const char *scroll_bar_name = SCROLL_BAR_NAME;
4869 unsigned long pixel;
4871 block_input ();
4873 #ifdef USE_MOTIF
4874 /* Set resources. Create the widget. */
4875 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4876 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4877 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4878 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4879 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4880 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4881 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4883 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4884 if (pixel != -1)
4886 XtSetArg (av[ac], XmNforeground, pixel);
4887 ++ac;
4890 pixel = f->output_data.x->scroll_bar_background_pixel;
4891 if (pixel != -1)
4893 XtSetArg (av[ac], XmNbackground, pixel);
4894 ++ac;
4897 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4898 (char *) scroll_bar_name, av, ac);
4900 /* Add one callback for everything that can happen. */
4901 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4902 (XtPointer) bar);
4903 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4904 (XtPointer) bar);
4905 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4906 (XtPointer) bar);
4907 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4908 (XtPointer) bar);
4909 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4910 (XtPointer) bar);
4911 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4912 (XtPointer) bar);
4913 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4914 (XtPointer) bar);
4916 /* Realize the widget. Only after that is the X window created. */
4917 XtRealizeWidget (widget);
4919 /* Set the cursor to an arrow. I didn't find a resource to do that.
4920 And I'm wondering why it hasn't an arrow cursor by default. */
4921 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4922 f->output_data.x->nontext_cursor);
4924 #else /* !USE_MOTIF i.e. use Xaw */
4926 /* Set resources. Create the widget. The background of the
4927 Xaw3d scroll bar widget is a little bit light for my taste.
4928 We don't alter it here to let users change it according
4929 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4930 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4931 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4932 /* For smoother scrolling with Xaw3d -sm */
4933 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4935 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4936 if (pixel != -1)
4938 XtSetArg (av[ac], XtNforeground, pixel);
4939 ++ac;
4942 pixel = f->output_data.x->scroll_bar_background_pixel;
4943 if (pixel != -1)
4945 XtSetArg (av[ac], XtNbackground, pixel);
4946 ++ac;
4949 /* Top/bottom shadow colors. */
4951 /* Allocate them, if necessary. */
4952 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4954 pixel = f->output_data.x->scroll_bar_background_pixel;
4955 if (pixel != -1)
4957 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4958 FRAME_X_COLORMAP (f),
4959 &pixel, 1.2, 0x8000))
4960 pixel = -1;
4961 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4964 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4966 pixel = f->output_data.x->scroll_bar_background_pixel;
4967 if (pixel != -1)
4969 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4970 FRAME_X_COLORMAP (f),
4971 &pixel, 0.6, 0x4000))
4972 pixel = -1;
4973 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4977 #ifdef XtNbeNiceToColormap
4978 /* Tell the toolkit about them. */
4979 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4980 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4981 /* We tried to allocate a color for the top/bottom shadow, and
4982 failed, so tell Xaw3d to use dithering instead. */
4983 /* But only if we have a small colormap. Xaw3d can allocate nice
4984 colors itself. */
4986 XtSetArg (av[ac], XtNbeNiceToColormap,
4987 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
4988 ++ac;
4990 else
4991 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4992 be more consistent with other emacs 3d colors, and since Xaw3d is
4993 not good at dealing with allocation failure. */
4995 /* This tells Xaw3d to use real colors instead of dithering for
4996 the shadows. */
4997 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4998 ++ac;
5000 /* Specify the colors. */
5001 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
5002 if (pixel != -1)
5004 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
5005 ++ac;
5007 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
5008 if (pixel != -1)
5010 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
5011 ++ac;
5014 #endif
5016 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
5017 f->output_data.x->edit_widget, av, ac);
5020 char const *initial = "";
5021 char const *val = initial;
5022 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
5023 #ifdef XtNarrowScrollbars
5024 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
5025 #endif
5026 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
5027 if (xaw3d_arrow_scroll || val == initial)
5028 { /* ARROW_SCROLL */
5029 xaw3d_arrow_scroll = True;
5030 /* Isn't that just a personal preference ? --Stef */
5031 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
5035 /* Define callbacks. */
5036 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
5037 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
5038 (XtPointer) bar);
5040 /* Realize the widget. Only after that is the X window created. */
5041 XtRealizeWidget (widget);
5043 #endif /* !USE_MOTIF */
5045 /* Install an action hook that lets us detect when the user
5046 finishes interacting with a scroll bar. */
5047 if (action_hook_id == 0)
5048 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
5050 /* Remember X window and widget in the scroll bar vector. */
5051 SET_SCROLL_BAR_X_WIDGET (bar, widget);
5052 xwindow = XtWindow (widget);
5053 bar->x_window = xwindow;
5054 bar->whole = 1;
5055 bar->horizontal = false;
5057 unblock_input ();
5060 static void
5061 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5063 Window xwindow;
5064 Widget widget;
5065 Arg av[20];
5066 int ac = 0;
5067 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
5068 unsigned long pixel;
5070 block_input ();
5072 #ifdef USE_MOTIF
5073 /* Set resources. Create the widget. */
5074 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5075 XtSetArg (av[ac], XmNminimum, 0); ++ac;
5076 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
5077 XtSetArg (av[ac], XmNorientation, XmHORIZONTAL); ++ac;
5078 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_RIGHT), ++ac;
5079 XtSetArg (av[ac], XmNincrement, 1); ++ac;
5080 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
5082 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5083 if (pixel != -1)
5085 XtSetArg (av[ac], XmNforeground, pixel);
5086 ++ac;
5089 pixel = f->output_data.x->scroll_bar_background_pixel;
5090 if (pixel != -1)
5092 XtSetArg (av[ac], XmNbackground, pixel);
5093 ++ac;
5096 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
5097 (char *) scroll_bar_name, av, ac);
5099 /* Add one callback for everything that can happen. */
5100 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
5101 (XtPointer) bar);
5102 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
5103 (XtPointer) bar);
5104 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
5105 (XtPointer) bar);
5106 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
5107 (XtPointer) bar);
5108 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
5109 (XtPointer) bar);
5110 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
5111 (XtPointer) bar);
5112 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
5113 (XtPointer) bar);
5115 /* Realize the widget. Only after that is the X window created. */
5116 XtRealizeWidget (widget);
5118 /* Set the cursor to an arrow. I didn't find a resource to do that.
5119 And I'm wondering why it hasn't an arrow cursor by default. */
5120 XDefineCursor (XtDisplay (widget), XtWindow (widget),
5121 f->output_data.x->nontext_cursor);
5123 #else /* !USE_MOTIF i.e. use Xaw */
5125 /* Set resources. Create the widget. The background of the
5126 Xaw3d scroll bar widget is a little bit light for my taste.
5127 We don't alter it here to let users change it according
5128 to their taste with `emacs*verticalScrollBar.background: xxx'. */
5129 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5130 XtSetArg (av[ac], XtNorientation, XtorientHorizontal); ++ac;
5131 /* For smoother scrolling with Xaw3d -sm */
5132 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
5134 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5135 if (pixel != -1)
5137 XtSetArg (av[ac], XtNforeground, pixel);
5138 ++ac;
5141 pixel = f->output_data.x->scroll_bar_background_pixel;
5142 if (pixel != -1)
5144 XtSetArg (av[ac], XtNbackground, pixel);
5145 ++ac;
5148 /* Top/bottom shadow colors. */
5150 /* Allocate them, if necessary. */
5151 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
5153 pixel = f->output_data.x->scroll_bar_background_pixel;
5154 if (pixel != -1)
5156 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5157 FRAME_X_COLORMAP (f),
5158 &pixel, 1.2, 0x8000))
5159 pixel = -1;
5160 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
5163 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5165 pixel = f->output_data.x->scroll_bar_background_pixel;
5166 if (pixel != -1)
5168 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5169 FRAME_X_COLORMAP (f),
5170 &pixel, 0.6, 0x4000))
5171 pixel = -1;
5172 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
5176 #ifdef XtNbeNiceToColormap
5177 /* Tell the toolkit about them. */
5178 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
5179 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5180 /* We tried to allocate a color for the top/bottom shadow, and
5181 failed, so tell Xaw3d to use dithering instead. */
5182 /* But only if we have a small colormap. Xaw3d can allocate nice
5183 colors itself. */
5185 XtSetArg (av[ac], XtNbeNiceToColormap,
5186 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
5187 ++ac;
5189 else
5190 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
5191 be more consistent with other emacs 3d colors, and since Xaw3d is
5192 not good at dealing with allocation failure. */
5194 /* This tells Xaw3d to use real colors instead of dithering for
5195 the shadows. */
5196 XtSetArg (av[ac], XtNbeNiceToColormap, False);
5197 ++ac;
5199 /* Specify the colors. */
5200 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
5201 if (pixel != -1)
5203 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
5204 ++ac;
5206 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
5207 if (pixel != -1)
5209 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
5210 ++ac;
5213 #endif
5215 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
5216 f->output_data.x->edit_widget, av, ac);
5219 char const *initial = "";
5220 char const *val = initial;
5221 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
5222 #ifdef XtNarrowScrollbars
5223 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
5224 #endif
5225 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
5226 if (xaw3d_arrow_scroll || val == initial)
5227 { /* ARROW_SCROLL */
5228 xaw3d_arrow_scroll = True;
5229 /* Isn't that just a personal preference ? --Stef */
5230 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
5234 /* Define callbacks. */
5235 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
5236 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
5237 (XtPointer) bar);
5239 /* Realize the widget. Only after that is the X window created. */
5240 XtRealizeWidget (widget);
5242 #endif /* !USE_MOTIF */
5244 /* Install an action hook that lets us detect when the user
5245 finishes interacting with a scroll bar. */
5246 if (horizontal_action_hook_id == 0)
5247 horizontal_action_hook_id
5248 = XtAppAddActionHook (Xt_app_con, xt_horizontal_action_hook, 0);
5250 /* Remember X window and widget in the scroll bar vector. */
5251 SET_SCROLL_BAR_X_WIDGET (bar, widget);
5252 xwindow = XtWindow (widget);
5253 bar->x_window = xwindow;
5254 bar->whole = 1;
5255 bar->horizontal = true;
5257 unblock_input ();
5259 #endif /* not USE_GTK */
5262 /* Set the thumb size and position of scroll bar BAR. We are currently
5263 displaying PORTION out of a whole WHOLE, and our position POSITION. */
5265 #ifdef USE_GTK
5266 static void
5267 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
5269 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5272 static void
5273 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
5275 xg_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
5278 #else /* not USE_GTK */
5279 static void
5280 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
5281 int whole)
5283 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5284 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
5285 float top, shown;
5287 block_input ();
5289 #ifdef USE_MOTIF
5291 if (scroll_bar_adjust_thumb_portion_p)
5293 /* We use an estimate of 30 chars per line rather than the real
5294 `portion' value. This has the disadvantage that the thumb size
5295 is not very representative, but it makes our life a lot easier.
5296 Otherwise, we have to constantly adjust the thumb size, which
5297 we can't always do quickly enough: while dragging, the size of
5298 the thumb might prevent the user from dragging the thumb all the
5299 way to the end. but Motif and some versions of Xaw3d don't allow
5300 updating the thumb size while dragging. Also, even if we can update
5301 its size, the update will often happen too late.
5302 If you don't believe it, check out revision 1.650 of xterm.c to see
5303 what hoops we were going through and the still poor behavior we got. */
5304 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
5305 /* When the thumb is at the bottom, position == whole.
5306 So we need to increase `whole' to make space for the thumb. */
5307 whole += portion;
5310 if (whole <= 0)
5311 top = 0, shown = 1;
5312 else
5314 top = (float) position / whole;
5315 shown = (float) portion / whole;
5318 if (bar->dragging == -1)
5320 int size, value;
5322 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
5323 is the scroll bar's maximum and MIN is the scroll bar's minimum
5324 value. */
5325 size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
5327 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
5328 value = top * XM_SB_MAX;
5329 value = min (value, XM_SB_MAX - size);
5331 XmScrollBarSetValues (widget, value, size, 0, 0, False);
5333 #else /* !USE_MOTIF i.e. use Xaw */
5335 if (whole == 0)
5336 top = 0, shown = 1;
5337 else
5339 top = (float) position / whole;
5340 shown = (float) portion / whole;
5344 float old_top, old_shown;
5345 Dimension height;
5346 XtVaGetValues (widget,
5347 XtNtopOfThumb, &old_top,
5348 XtNshown, &old_shown,
5349 XtNheight, &height,
5350 NULL);
5352 /* Massage the top+shown values. */
5353 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
5354 top = max (0, min (1, top));
5355 else
5356 top = old_top;
5357 #if ! defined (HAVE_XAW3D)
5358 /* With Xaw, 'top' values too closer to 1.0 may
5359 cause the thumb to disappear. Fix that. */
5360 top = min (top, 0.99f);
5361 #endif
5362 /* Keep two pixels available for moving the thumb down. */
5363 shown = max (0, min (1 - top - (2.0f / height), shown));
5364 #if ! defined (HAVE_XAW3D)
5365 /* Likewise with too small 'shown'. */
5366 shown = max (shown, 0.01f);
5367 #endif
5369 /* If the call to XawScrollbarSetThumb below doesn't seem to
5370 work, check that 'NARROWPROTO' is defined in src/config.h.
5371 If this is not so, most likely you need to fix configure. */
5372 if (top != old_top || shown != old_shown)
5374 if (bar->dragging == -1)
5375 XawScrollbarSetThumb (widget, top, shown);
5376 else
5378 /* Try to make the scrolling a tad smoother. */
5379 if (!xaw3d_pick_top)
5380 shown = min (shown, old_shown);
5382 XawScrollbarSetThumb (widget, top, shown);
5386 #endif /* !USE_MOTIF */
5388 unblock_input ();
5391 static void
5392 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
5393 int whole)
5395 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5396 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
5397 float top, shown;
5399 block_input ();
5401 #ifdef USE_MOTIF
5402 bar->whole = whole;
5403 shown = (float) portion / whole;
5404 top = (float) position / (whole - portion);
5406 int size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
5407 int value = clip_to_bounds (0, top * (XM_SB_MAX - size), XM_SB_MAX - size);
5409 XmScrollBarSetValues (widget, value, size, 0, 0, False);
5411 #else /* !USE_MOTIF i.e. use Xaw */
5412 bar->whole = whole;
5413 if (whole == 0)
5414 top = 0, shown = 1;
5415 else
5417 top = (float) position / whole;
5418 shown = (float) portion / whole;
5422 float old_top, old_shown;
5423 Dimension height;
5424 XtVaGetValues (widget,
5425 XtNtopOfThumb, &old_top,
5426 XtNshown, &old_shown,
5427 XtNheight, &height,
5428 NULL);
5430 #if false
5431 /* Massage the top+shown values. */
5432 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
5433 top = max (0, min (1, top));
5434 else
5435 top = old_top;
5436 #if ! defined (HAVE_XAW3D)
5437 /* With Xaw, 'top' values too closer to 1.0 may
5438 cause the thumb to disappear. Fix that. */
5439 top = min (top, 0.99f);
5440 #endif
5441 /* Keep two pixels available for moving the thumb down. */
5442 shown = max (0, min (1 - top - (2.0f / height), shown));
5443 #if ! defined (HAVE_XAW3D)
5444 /* Likewise with too small 'shown'. */
5445 shown = max (shown, 0.01f);
5446 #endif
5447 #endif
5449 /* If the call to XawScrollbarSetThumb below doesn't seem to
5450 work, check that 'NARROWPROTO' is defined in src/config.h.
5451 If this is not so, most likely you need to fix configure. */
5452 XawScrollbarSetThumb (widget, top, shown);
5453 #if false
5454 if (top != old_top || shown != old_shown)
5456 if (bar->dragging == -1)
5457 XawScrollbarSetThumb (widget, top, shown);
5458 else
5460 /* Try to make the scrolling a tad smoother. */
5461 if (!xaw3d_pick_top)
5462 shown = min (shown, old_shown);
5464 XawScrollbarSetThumb (widget, top, shown);
5467 #endif
5469 #endif /* !USE_MOTIF */
5471 unblock_input ();
5473 #endif /* not USE_GTK */
5475 #endif /* USE_TOOLKIT_SCROLL_BARS */
5479 /************************************************************************
5480 Scroll bars, general
5481 ************************************************************************/
5483 /* Create a scroll bar and return the scroll bar vector for it. W is
5484 the Emacs window on which to create the scroll bar. TOP, LEFT,
5485 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
5486 scroll bar. */
5488 static struct scroll_bar *
5489 x_scroll_bar_create (struct window *w, int top, int left,
5490 int width, int height, bool horizontal)
5492 struct frame *f = XFRAME (w->frame);
5493 struct scroll_bar *bar
5494 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
5495 Lisp_Object barobj;
5497 block_input ();
5499 #ifdef USE_TOOLKIT_SCROLL_BARS
5500 if (horizontal)
5501 x_create_horizontal_toolkit_scroll_bar (f, bar);
5502 else
5503 x_create_toolkit_scroll_bar (f, bar);
5504 #else /* not USE_TOOLKIT_SCROLL_BARS */
5506 XSetWindowAttributes a;
5507 unsigned long mask;
5508 Window window;
5510 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
5511 if (a.background_pixel == -1)
5512 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
5514 a.event_mask = (ButtonPressMask | ButtonReleaseMask
5515 | ButtonMotionMask | PointerMotionHintMask
5516 | ExposureMask);
5517 a.cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
5519 mask = (CWBackPixel | CWEventMask | CWCursor);
5521 /* Clear the area of W that will serve as a scroll bar. This is
5522 for the case that a window has been split horizontally. In
5523 this case, no clear_frame is generated to reduce flickering. */
5524 if (width > 0 && window_box_height (w) > 0)
5525 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5526 left, top, width, window_box_height (w));
5528 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5529 /* Position and size of scroll bar. */
5530 left, top, width, height,
5531 /* Border width, depth, class, and visual. */
5533 CopyFromParent,
5534 CopyFromParent,
5535 CopyFromParent,
5536 /* Attributes. */
5537 mask, &a);
5538 bar->x_window = window;
5540 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5542 XSETWINDOW (bar->window, w);
5543 bar->top = top;
5544 bar->left = left;
5545 bar->width = width;
5546 bar->height = height;
5547 bar->start = 0;
5548 bar->end = 0;
5549 bar->dragging = -1;
5550 bar->horizontal = horizontal;
5551 #if defined (USE_TOOLKIT_SCROLL_BARS) && defined (USE_LUCID)
5552 bar->last_seen_part = scroll_bar_nowhere;
5553 #endif
5555 /* Add bar to its frame's list of scroll bars. */
5556 bar->next = FRAME_SCROLL_BARS (f);
5557 bar->prev = Qnil;
5558 XSETVECTOR (barobj, bar);
5559 fset_scroll_bars (f, barobj);
5560 if (!NILP (bar->next))
5561 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5563 /* Map the window/widget. */
5564 #ifdef USE_TOOLKIT_SCROLL_BARS
5566 #ifdef USE_GTK
5567 if (horizontal)
5568 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top,
5569 left, width, max (height, 1));
5570 else
5571 xg_update_scrollbar_pos (f, bar->x_window, top,
5572 left, width, max (height, 1));
5573 #else /* not USE_GTK */
5574 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
5575 XtConfigureWidget (scroll_bar, left, top, width, max (height, 1), 0);
5576 XtMapWidget (scroll_bar);
5577 #endif /* not USE_GTK */
5579 #else /* not USE_TOOLKIT_SCROLL_BARS */
5580 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
5581 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5583 unblock_input ();
5584 return bar;
5588 #ifndef USE_TOOLKIT_SCROLL_BARS
5590 /* Draw BAR's handle in the proper position.
5592 If the handle is already drawn from START to END, don't bother
5593 redrawing it, unless REBUILD; in that case, always
5594 redraw it. (REBUILD is handy for drawing the handle after expose
5595 events.)
5597 Normally, we want to constrain the start and end of the handle to
5598 fit inside its rectangle, but if the user is dragging the scroll
5599 bar handle, we want to let them drag it down all the way, so that
5600 the bar's top is as far down as it goes; otherwise, there's no way
5601 to move to the very end of the buffer. */
5603 static void
5604 x_scroll_bar_set_handle (struct scroll_bar *bar, int start, int end,
5605 bool rebuild)
5607 bool dragging = bar->dragging != -1;
5608 Window w = bar->x_window;
5609 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5610 GC gc = f->output_data.x->normal_gc;
5612 /* If the display is already accurate, do nothing. */
5613 if (! rebuild
5614 && start == bar->start
5615 && end == bar->end)
5616 return;
5618 block_input ();
5621 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
5622 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5623 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5625 /* Make sure the values are reasonable, and try to preserve
5626 the distance between start and end. */
5628 int length = end - start;
5630 if (start < 0)
5631 start = 0;
5632 else if (start > top_range)
5633 start = top_range;
5634 end = start + length;
5636 if (end < start)
5637 end = start;
5638 else if (end > top_range && ! dragging)
5639 end = top_range;
5642 /* Store the adjusted setting in the scroll bar. */
5643 bar->start = start;
5644 bar->end = end;
5646 /* Clip the end position, just for display. */
5647 if (end > top_range)
5648 end = top_range;
5650 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
5651 below top positions, to make sure the handle is always at least
5652 that many pixels tall. */
5653 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
5655 /* Draw the empty space above the handle. Note that we can't clear
5656 zero-height areas; that means "clear to end of window." */
5657 if ((inside_width > 0) && (start > 0))
5658 x_clear_area (FRAME_X_DISPLAY (f), w,
5659 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5660 VERTICAL_SCROLL_BAR_TOP_BORDER,
5661 inside_width, start);
5663 /* Change to proper foreground color if one is specified. */
5664 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5665 XSetForeground (FRAME_X_DISPLAY (f), gc,
5666 f->output_data.x->scroll_bar_foreground_pixel);
5668 /* Draw the handle itself. */
5669 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
5670 /* x, y, width, height */
5671 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5672 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
5673 inside_width, end - start);
5675 /* Restore the foreground color of the GC if we changed it above. */
5676 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5677 XSetForeground (FRAME_X_DISPLAY (f), gc,
5678 FRAME_FOREGROUND_PIXEL (f));
5680 /* Draw the empty space below the handle. Note that we can't
5681 clear zero-height areas; that means "clear to end of window." */
5682 if ((inside_width > 0) && (end < inside_height))
5683 x_clear_area (FRAME_X_DISPLAY (f), w,
5684 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5685 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
5686 inside_width, inside_height - end);
5689 unblock_input ();
5692 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5694 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
5695 nil. */
5697 static void
5698 x_scroll_bar_remove (struct scroll_bar *bar)
5700 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5701 block_input ();
5703 #ifdef USE_TOOLKIT_SCROLL_BARS
5704 #ifdef USE_GTK
5705 xg_remove_scroll_bar (f, bar->x_window);
5706 #else /* not USE_GTK */
5707 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5708 #endif /* not USE_GTK */
5709 #else
5710 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
5711 #endif
5713 /* Dissociate this scroll bar from its window. */
5714 if (bar->horizontal)
5715 wset_horizontal_scroll_bar (XWINDOW (bar->window), Qnil);
5716 else
5717 wset_vertical_scroll_bar (XWINDOW (bar->window), Qnil);
5719 unblock_input ();
5723 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5724 that we are displaying PORTION characters out of a total of WHOLE
5725 characters, starting at POSITION. If WINDOW has no scroll bar,
5726 create one. */
5728 static void
5729 XTset_vertical_scroll_bar (struct window *w, int portion, int whole, int position)
5731 struct frame *f = XFRAME (w->frame);
5732 Lisp_Object barobj;
5733 struct scroll_bar *bar;
5734 int top, height, left, width;
5735 int window_y, window_height;
5737 /* Get window dimensions. */
5738 window_box (w, ANY_AREA, 0, &window_y, 0, &window_height);
5739 top = window_y;
5740 height = window_height;
5741 left = WINDOW_SCROLL_BAR_AREA_X (w);
5742 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
5744 /* Does the scroll bar exist yet? */
5745 if (NILP (w->vertical_scroll_bar))
5747 if (width > 0 && height > 0)
5749 block_input ();
5750 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5751 left, top, width, height);
5752 unblock_input ();
5755 bar = x_scroll_bar_create (w, top, left, width, max (height, 1), false);
5757 else
5759 /* It may just need to be moved and resized. */
5760 unsigned int mask = 0;
5762 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5764 block_input ();
5766 if (left != bar->left)
5767 mask |= CWX;
5768 if (top != bar->top)
5769 mask |= CWY;
5770 if (width != bar->width)
5771 mask |= CWWidth;
5772 if (height != bar->height)
5773 mask |= CWHeight;
5775 #ifdef USE_TOOLKIT_SCROLL_BARS
5777 /* Move/size the scroll bar widget. */
5778 if (mask)
5780 /* Since toolkit scroll bars are smaller than the space reserved
5781 for them on the frame, we have to clear "under" them. */
5782 if (width > 0 && height > 0)
5783 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5784 left, top, width, height);
5785 #ifdef USE_GTK
5786 xg_update_scrollbar_pos (f, bar->x_window, top,
5787 left, width, max (height, 1));
5788 #else /* not USE_GTK */
5789 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5790 left, top, width, max (height, 1), 0);
5791 #endif /* not USE_GTK */
5793 #else /* not USE_TOOLKIT_SCROLL_BARS */
5795 /* Move/size the scroll bar window. */
5796 if (mask)
5798 XWindowChanges wc;
5800 wc.x = left;
5801 wc.y = top;
5802 wc.width = width;
5803 wc.height = height;
5804 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5805 mask, &wc);
5808 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5810 /* Remember new settings. */
5811 bar->left = left;
5812 bar->top = top;
5813 bar->width = width;
5814 bar->height = height;
5816 unblock_input ();
5819 #ifdef USE_TOOLKIT_SCROLL_BARS
5820 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5821 #else /* not USE_TOOLKIT_SCROLL_BARS */
5822 /* Set the scroll bar's current state, unless we're currently being
5823 dragged. */
5824 if (bar->dragging == -1)
5826 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5828 if (whole == 0)
5829 x_scroll_bar_set_handle (bar, 0, top_range, false);
5830 else
5832 int start = ((double) position * top_range) / whole;
5833 int end = ((double) (position + portion) * top_range) / whole;
5834 x_scroll_bar_set_handle (bar, start, end, false);
5837 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5839 XSETVECTOR (barobj, bar);
5840 wset_vertical_scroll_bar (w, barobj);
5844 static void
5845 XTset_horizontal_scroll_bar (struct window *w, int portion, int whole, int position)
5847 struct frame *f = XFRAME (w->frame);
5848 Lisp_Object barobj;
5849 struct scroll_bar *bar;
5850 int top, height, left, width;
5851 int window_x, window_width;
5852 int pixel_width = WINDOW_PIXEL_WIDTH (w);
5854 /* Get window dimensions. */
5855 window_box (w, ANY_AREA, &window_x, 0, &window_width, 0);
5856 left = window_x;
5857 width = window_width;
5858 top = WINDOW_SCROLL_BAR_AREA_Y (w);
5859 height = WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
5861 /* Does the scroll bar exist yet? */
5862 if (NILP (w->horizontal_scroll_bar))
5864 if (width > 0 && height > 0)
5866 block_input ();
5868 /* Clear also part between window_width and
5869 WINDOW_PIXEL_WIDTH. */
5870 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5871 left, top, pixel_width, height);
5872 unblock_input ();
5875 bar = x_scroll_bar_create (w, top, left, width, height, true);
5877 else
5879 /* It may just need to be moved and resized. */
5880 unsigned int mask = 0;
5882 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
5884 block_input ();
5886 if (left != bar->left)
5887 mask |= CWX;
5888 if (top != bar->top)
5889 mask |= CWY;
5890 if (width != bar->width)
5891 mask |= CWWidth;
5892 if (height != bar->height)
5893 mask |= CWHeight;
5895 #ifdef USE_TOOLKIT_SCROLL_BARS
5896 /* Move/size the scroll bar widget. */
5897 if (mask)
5899 /* Since toolkit scroll bars are smaller than the space reserved
5900 for them on the frame, we have to clear "under" them. */
5901 if (width > 0 && height > 0)
5902 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5903 WINDOW_LEFT_EDGE_X (w), top,
5904 pixel_width - WINDOW_RIGHT_DIVIDER_WIDTH (w), height);
5905 #ifdef USE_GTK
5906 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top, left,
5907 width, height);
5908 #else /* not USE_GTK */
5909 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5910 left, top, width, height, 0);
5911 #endif /* not USE_GTK */
5913 #else /* not USE_TOOLKIT_SCROLL_BARS */
5915 /* Clear areas not covered by the scroll bar because it's not as
5916 wide as the area reserved for it. This makes sure a
5917 previous mode line display is cleared after C-x 2 C-x 1, for
5918 example. */
5920 int area_height = WINDOW_CONFIG_SCROLL_BAR_HEIGHT (w);
5921 int rest = area_height - height;
5922 if (rest > 0 && width > 0)
5923 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5924 left, top, width, rest);
5927 /* Move/size the scroll bar window. */
5928 if (mask)
5930 XWindowChanges wc;
5932 wc.x = left;
5933 wc.y = top;
5934 wc.width = width;
5935 wc.height = height;
5936 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5937 mask, &wc);
5940 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5942 /* Remember new settings. */
5943 bar->left = left;
5944 bar->top = top;
5945 bar->width = width;
5946 bar->height = height;
5948 unblock_input ();
5951 #ifdef USE_TOOLKIT_SCROLL_BARS
5952 x_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
5953 #else /* not USE_TOOLKIT_SCROLL_BARS */
5954 /* Set the scroll bar's current state, unless we're currently being
5955 dragged. */
5956 if (bar->dragging == -1)
5958 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, width);
5960 if (whole == 0)
5961 x_scroll_bar_set_handle (bar, 0, left_range, false);
5962 else
5964 int start = ((double) position * left_range) / whole;
5965 int end = ((double) (position + portion) * left_range) / whole;
5966 x_scroll_bar_set_handle (bar, start, end, false);
5969 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5971 XSETVECTOR (barobj, bar);
5972 wset_horizontal_scroll_bar (w, barobj);
5976 /* The following three hooks are used when we're doing a thorough
5977 redisplay of the frame. We don't explicitly know which scroll bars
5978 are going to be deleted, because keeping track of when windows go
5979 away is a real pain - "Can you say set-window-configuration, boys
5980 and girls?" Instead, we just assert at the beginning of redisplay
5981 that *all* scroll bars are to be removed, and then save a scroll bar
5982 from the fiery pit when we actually redisplay its window. */
5984 /* Arrange for all scroll bars on FRAME to be removed at the next call
5985 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5986 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5988 static void
5989 XTcondemn_scroll_bars (struct frame *frame)
5991 if (!NILP (FRAME_SCROLL_BARS (frame)))
5993 if (!NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5995 /* Prepend scrollbars to already condemned ones. */
5996 Lisp_Object last = FRAME_SCROLL_BARS (frame);
5998 while (!NILP (XSCROLL_BAR (last)->next))
5999 last = XSCROLL_BAR (last)->next;
6001 XSCROLL_BAR (last)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
6002 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = last;
6005 fset_condemned_scroll_bars (frame, FRAME_SCROLL_BARS (frame));
6006 fset_scroll_bars (frame, Qnil);
6011 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
6012 Note that WINDOW isn't necessarily condemned at all. */
6014 static void
6015 XTredeem_scroll_bar (struct window *w)
6017 struct scroll_bar *bar;
6018 Lisp_Object barobj;
6019 struct frame *f;
6021 /* We can't redeem this window's scroll bar if it doesn't have one. */
6022 if (NILP (w->vertical_scroll_bar) && NILP (w->horizontal_scroll_bar))
6023 emacs_abort ();
6025 if (!NILP (w->vertical_scroll_bar) && WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
6027 bar = XSCROLL_BAR (w->vertical_scroll_bar);
6028 /* Unlink it from the condemned list. */
6029 f = XFRAME (WINDOW_FRAME (w));
6030 if (NILP (bar->prev))
6032 /* If the prev pointer is nil, it must be the first in one of
6033 the lists. */
6034 if (EQ (FRAME_SCROLL_BARS (f), w->vertical_scroll_bar))
6035 /* It's not condemned. Everything's fine. */
6036 goto horizontal;
6037 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
6038 w->vertical_scroll_bar))
6039 fset_condemned_scroll_bars (f, bar->next);
6040 else
6041 /* If its prev pointer is nil, it must be at the front of
6042 one or the other! */
6043 emacs_abort ();
6045 else
6046 XSCROLL_BAR (bar->prev)->next = bar->next;
6048 if (! NILP (bar->next))
6049 XSCROLL_BAR (bar->next)->prev = bar->prev;
6051 bar->next = FRAME_SCROLL_BARS (f);
6052 bar->prev = Qnil;
6053 XSETVECTOR (barobj, bar);
6054 fset_scroll_bars (f, barobj);
6055 if (! NILP (bar->next))
6056 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6059 horizontal:
6060 if (!NILP (w->horizontal_scroll_bar) && WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
6062 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
6063 /* Unlink it from the condemned list. */
6064 f = XFRAME (WINDOW_FRAME (w));
6065 if (NILP (bar->prev))
6067 /* If the prev pointer is nil, it must be the first in one of
6068 the lists. */
6069 if (EQ (FRAME_SCROLL_BARS (f), w->horizontal_scroll_bar))
6070 /* It's not condemned. Everything's fine. */
6071 return;
6072 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
6073 w->horizontal_scroll_bar))
6074 fset_condemned_scroll_bars (f, bar->next);
6075 else
6076 /* If its prev pointer is nil, it must be at the front of
6077 one or the other! */
6078 emacs_abort ();
6080 else
6081 XSCROLL_BAR (bar->prev)->next = bar->next;
6083 if (! NILP (bar->next))
6084 XSCROLL_BAR (bar->next)->prev = bar->prev;
6086 bar->next = FRAME_SCROLL_BARS (f);
6087 bar->prev = Qnil;
6088 XSETVECTOR (barobj, bar);
6089 fset_scroll_bars (f, barobj);
6090 if (! NILP (bar->next))
6091 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6095 /* Remove all scroll bars on FRAME that haven't been saved since the
6096 last call to `*condemn_scroll_bars_hook'. */
6098 static void
6099 XTjudge_scroll_bars (struct frame *f)
6101 Lisp_Object bar, next;
6103 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
6105 /* Clear out the condemned list now so we won't try to process any
6106 more events on the hapless scroll bars. */
6107 fset_condemned_scroll_bars (f, Qnil);
6109 for (; ! NILP (bar); bar = next)
6111 struct scroll_bar *b = XSCROLL_BAR (bar);
6113 x_scroll_bar_remove (b);
6115 next = b->next;
6116 b->next = b->prev = Qnil;
6119 /* Now there should be no references to the condemned scroll bars,
6120 and they should get garbage-collected. */
6124 #ifndef USE_TOOLKIT_SCROLL_BARS
6125 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
6126 is a no-op when using toolkit scroll bars.
6128 This may be called from a signal handler, so we have to ignore GC
6129 mark bits. */
6131 static void
6132 x_scroll_bar_expose (struct scroll_bar *bar, const XEvent *event)
6134 Window w = bar->x_window;
6135 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6136 GC gc = f->output_data.x->normal_gc;
6138 block_input ();
6140 x_scroll_bar_set_handle (bar, bar->start, bar->end, true);
6142 /* Switch to scroll bar foreground color. */
6143 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6144 XSetForeground (FRAME_X_DISPLAY (f), gc,
6145 f->output_data.x->scroll_bar_foreground_pixel);
6147 /* Draw a one-pixel border just inside the edges of the scroll bar. */
6148 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
6149 /* x, y, width, height */
6150 0, 0, bar->width - 1, bar->height - 1);
6152 /* Restore the foreground color of the GC if we changed it above. */
6153 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6154 XSetForeground (FRAME_X_DISPLAY (f), gc,
6155 FRAME_FOREGROUND_PIXEL (f));
6157 unblock_input ();
6160 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6162 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
6163 is set to something other than NO_EVENT, it is enqueued.
6165 This may be called from a signal handler, so we have to ignore GC
6166 mark bits. */
6169 static void
6170 x_scroll_bar_handle_click (struct scroll_bar *bar,
6171 const XEvent *event,
6172 struct input_event *emacs_event)
6174 if (! WINDOWP (bar->window))
6175 emacs_abort ();
6177 emacs_event->kind = (bar->horizontal
6178 ? HORIZONTAL_SCROLL_BAR_CLICK_EVENT
6179 : SCROLL_BAR_CLICK_EVENT);
6180 emacs_event->code = event->xbutton.button - Button1;
6181 emacs_event->modifiers
6182 = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO
6183 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
6184 event->xbutton.state)
6185 | (event->type == ButtonRelease
6186 ? up_modifier
6187 : down_modifier));
6188 emacs_event->frame_or_window = bar->window;
6189 emacs_event->arg = Qnil;
6190 emacs_event->timestamp = event->xbutton.time;
6191 if (bar->horizontal)
6193 int left_range
6194 = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
6195 int x = event->xbutton.x - HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
6197 if (x < 0) x = 0;
6198 if (x > left_range) x = left_range;
6200 if (x < bar->start)
6201 emacs_event->part = scroll_bar_before_handle;
6202 else if (x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
6203 emacs_event->part = scroll_bar_horizontal_handle;
6204 else
6205 emacs_event->part = scroll_bar_after_handle;
6207 #ifndef USE_TOOLKIT_SCROLL_BARS
6208 /* If the user has released the handle, set it to its final position. */
6209 if (event->type == ButtonRelease && bar->dragging != -1)
6211 int new_start = - bar->dragging;
6212 int new_end = new_start + bar->end - bar->start;
6214 x_scroll_bar_set_handle (bar, new_start, new_end, false);
6215 bar->dragging = -1;
6217 #endif
6219 XSETINT (emacs_event->x, left_range);
6220 XSETINT (emacs_event->y, x);
6222 else
6224 int top_range
6225 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
6226 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
6228 if (y < 0) y = 0;
6229 if (y > top_range) y = top_range;
6231 if (y < bar->start)
6232 emacs_event->part = scroll_bar_above_handle;
6233 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
6234 emacs_event->part = scroll_bar_handle;
6235 else
6236 emacs_event->part = scroll_bar_below_handle;
6238 #ifndef USE_TOOLKIT_SCROLL_BARS
6239 /* If the user has released the handle, set it to its final position. */
6240 if (event->type == ButtonRelease && bar->dragging != -1)
6242 int new_start = y - bar->dragging;
6243 int new_end = new_start + bar->end - bar->start;
6245 x_scroll_bar_set_handle (bar, new_start, new_end, false);
6246 bar->dragging = -1;
6248 #endif
6250 XSETINT (emacs_event->x, y);
6251 XSETINT (emacs_event->y, top_range);
6255 #ifndef USE_TOOLKIT_SCROLL_BARS
6257 /* Handle some mouse motion while someone is dragging the scroll bar.
6259 This may be called from a signal handler, so we have to ignore GC
6260 mark bits. */
6262 static void
6263 x_scroll_bar_note_movement (struct scroll_bar *bar,
6264 const XMotionEvent *event)
6266 struct frame *f = XFRAME (XWINDOW (bar->window)->frame);
6267 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
6269 dpyinfo->last_mouse_movement_time = event->time;
6270 dpyinfo->last_mouse_scroll_bar = bar;
6271 f->mouse_moved = true;
6273 /* If we're dragging the bar, display it. */
6274 if (bar->dragging != -1)
6276 /* Where should the handle be now? */
6277 int new_start = event->y - bar->dragging;
6279 if (new_start != bar->start)
6281 int new_end = new_start + bar->end - bar->start;
6283 x_scroll_bar_set_handle (bar, new_start, new_end, false);
6288 #endif /* !USE_TOOLKIT_SCROLL_BARS */
6290 /* Return information to the user about the current position of the mouse
6291 on the scroll bar. */
6293 static void
6294 x_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
6295 enum scroll_bar_part *part, Lisp_Object *x,
6296 Lisp_Object *y, Time *timestamp)
6298 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
6299 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
6300 Window w = bar->x_window;
6301 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6302 int win_x, win_y;
6303 Window dummy_window;
6304 int dummy_coord;
6305 unsigned int dummy_mask;
6307 block_input ();
6309 /* Get the mouse's position relative to the scroll bar window, and
6310 report that. */
6311 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
6313 /* Root, child, root x and root y. */
6314 &dummy_window, &dummy_window,
6315 &dummy_coord, &dummy_coord,
6317 /* Position relative to scroll bar. */
6318 &win_x, &win_y,
6320 /* Mouse buttons and modifier keys. */
6321 &dummy_mask))
6323 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
6325 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
6327 if (bar->dragging != -1)
6328 win_y -= bar->dragging;
6330 if (win_y < 0)
6331 win_y = 0;
6332 if (win_y > top_range)
6333 win_y = top_range;
6335 *fp = f;
6336 *bar_window = bar->window;
6338 if (bar->dragging != -1)
6339 *part = scroll_bar_handle;
6340 else if (win_y < bar->start)
6341 *part = scroll_bar_above_handle;
6342 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
6343 *part = scroll_bar_handle;
6344 else
6345 *part = scroll_bar_below_handle;
6347 XSETINT (*x, win_y);
6348 XSETINT (*y, top_range);
6350 f->mouse_moved = false;
6351 dpyinfo->last_mouse_scroll_bar = NULL;
6352 *timestamp = dpyinfo->last_mouse_movement_time;
6355 unblock_input ();
6359 /* Return information to the user about the current position of the mouse
6360 on the scroll bar. */
6362 static void
6363 x_horizontal_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
6364 enum scroll_bar_part *part, Lisp_Object *x,
6365 Lisp_Object *y, Time *timestamp)
6367 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
6368 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
6369 Window w = bar->x_window;
6370 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6371 int win_x, win_y;
6372 Window dummy_window;
6373 int dummy_coord;
6374 unsigned int dummy_mask;
6376 block_input ();
6378 /* Get the mouse's position relative to the scroll bar window, and
6379 report that. */
6380 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
6382 /* Root, child, root x and root y. */
6383 &dummy_window, &dummy_window,
6384 &dummy_coord, &dummy_coord,
6386 /* Position relative to scroll bar. */
6387 &win_x, &win_y,
6389 /* Mouse buttons and modifier keys. */
6390 &dummy_mask))
6392 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
6394 win_x -= HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
6396 if (bar->dragging != -1)
6397 win_x -= bar->dragging;
6399 if (win_x < 0)
6400 win_x = 0;
6401 if (win_x > left_range)
6402 win_x = left_range;
6404 *fp = f;
6405 *bar_window = bar->window;
6407 if (bar->dragging != -1)
6408 *part = scroll_bar_horizontal_handle;
6409 else if (win_x < bar->start)
6410 *part = scroll_bar_before_handle;
6411 else if (win_x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
6412 *part = scroll_bar_handle;
6413 else
6414 *part = scroll_bar_after_handle;
6416 XSETINT (*y, win_x);
6417 XSETINT (*x, left_range);
6419 f->mouse_moved = false;
6420 dpyinfo->last_mouse_scroll_bar = NULL;
6421 *timestamp = dpyinfo->last_mouse_movement_time;
6424 unblock_input ();
6428 /* The screen has been cleared so we may have changed foreground or
6429 background colors, and the scroll bars may need to be redrawn.
6430 Clear out the scroll bars, and ask for expose events, so we can
6431 redraw them. */
6433 static void
6434 x_scroll_bar_clear (struct frame *f)
6436 #ifndef USE_TOOLKIT_SCROLL_BARS
6437 Lisp_Object bar;
6439 /* We can have scroll bars even if this is 0,
6440 if we just turned off scroll bar mode.
6441 But in that case we should not clear them. */
6442 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
6443 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
6444 bar = XSCROLL_BAR (bar)->next)
6445 XClearArea (FRAME_X_DISPLAY (f),
6446 XSCROLL_BAR (bar)->x_window,
6447 0, 0, 0, 0, True);
6448 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6451 #ifdef ENABLE_CHECKING
6453 /* Record the last 100 characters stored
6454 to help debug the loss-of-chars-during-GC problem. */
6456 static int temp_index;
6457 static short temp_buffer[100];
6459 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
6460 if (temp_index == ARRAYELTS (temp_buffer)) \
6461 temp_index = 0; \
6462 temp_buffer[temp_index++] = (keysym)
6464 #else /* not ENABLE_CHECKING */
6466 #define STORE_KEYSYM_FOR_DEBUG(keysym) ((void)0)
6468 #endif /* ENABLE_CHECKING */
6470 /* Set this to nonzero to fake an "X I/O error"
6471 on a particular display. */
6473 static struct x_display_info *XTread_socket_fake_io_error;
6475 /* When we find no input here, we occasionally do a no-op command
6476 to verify that the X server is still running and we can still talk with it.
6477 We try all the open displays, one by one.
6478 This variable is used for cycling thru the displays. */
6480 static struct x_display_info *next_noop_dpyinfo;
6482 enum
6484 X_EVENT_NORMAL,
6485 X_EVENT_GOTO_OUT,
6486 X_EVENT_DROP
6489 /* Filter events for the current X input method.
6490 DPYINFO is the display this event is for.
6491 EVENT is the X event to filter.
6493 Returns non-zero if the event was filtered, caller shall not process
6494 this event further.
6495 Returns zero if event is wasn't filtered. */
6497 #ifdef HAVE_X_I18N
6498 static int
6499 x_filter_event (struct x_display_info *dpyinfo, XEvent *event)
6501 /* XFilterEvent returns non-zero if the input method has
6502 consumed the event. We pass the frame's X window to
6503 XFilterEvent because that's the one for which the IC
6504 was created. */
6506 struct frame *f1 = x_any_window_to_frame (dpyinfo,
6507 event->xclient.window);
6509 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
6511 #endif
6513 #ifdef USE_GTK
6514 static int current_count;
6515 static int current_finish;
6516 static struct input_event *current_hold_quit;
6518 /* This is the filter function invoked by the GTK event loop.
6519 It is invoked before the XEvent is translated to a GdkEvent,
6520 so we have a chance to act on the event before GTK. */
6521 static GdkFilterReturn
6522 event_handler_gdk (GdkXEvent *gxev, GdkEvent *ev, gpointer data)
6524 XEvent *xev = (XEvent *) gxev;
6526 block_input ();
6527 if (current_count >= 0)
6529 struct x_display_info *dpyinfo;
6531 dpyinfo = x_display_info_for_display (xev->xany.display);
6533 #ifdef HAVE_X_I18N
6534 /* Filter events for the current X input method.
6535 GTK calls XFilterEvent but not for key press and release,
6536 so we do it here. */
6537 if ((xev->type == KeyPress || xev->type == KeyRelease)
6538 && dpyinfo
6539 && x_filter_event (dpyinfo, xev))
6541 unblock_input ();
6542 return GDK_FILTER_REMOVE;
6544 #endif
6546 if (! dpyinfo)
6547 current_finish = X_EVENT_NORMAL;
6548 else
6549 current_count
6550 += handle_one_xevent (dpyinfo, xev, &current_finish,
6551 current_hold_quit);
6553 else
6554 current_finish = x_dispatch_event (xev, xev->xany.display);
6556 unblock_input ();
6558 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
6559 return GDK_FILTER_REMOVE;
6561 return GDK_FILTER_CONTINUE;
6563 #endif /* USE_GTK */
6566 static void xembed_send_message (struct frame *f, Time,
6567 enum xembed_message,
6568 long detail, long data1, long data2);
6570 static void
6571 x_net_wm_state (struct frame *f, Window window)
6573 int value = FULLSCREEN_NONE;
6574 Lisp_Object lval = Qnil;
6575 bool sticky = false;
6577 get_current_wm_state (f, window, &value, &sticky);
6579 switch (value)
6581 case FULLSCREEN_WIDTH:
6582 lval = Qfullwidth;
6583 break;
6584 case FULLSCREEN_HEIGHT:
6585 lval = Qfullheight;
6586 break;
6587 case FULLSCREEN_BOTH:
6588 lval = Qfullboth;
6589 break;
6590 case FULLSCREEN_MAXIMIZED:
6591 lval = Qmaximized;
6592 break;
6595 store_frame_param (f, Qfullscreen, lval);
6596 /** store_frame_param (f, Qsticky, sticky ? Qt : Qnil); **/
6599 /* Handles the XEvent EVENT on display DPYINFO.
6601 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
6602 *FINISH is zero if caller should continue reading events.
6603 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
6604 *EVENT is unchanged unless we're processing KeyPress event.
6606 We return the number of characters stored into the buffer. */
6608 static int
6609 handle_one_xevent (struct x_display_info *dpyinfo,
6610 const XEvent *event,
6611 int *finish, struct input_event *hold_quit)
6613 union {
6614 struct input_event ie;
6615 struct selection_input_event sie;
6616 } inev;
6617 int count = 0;
6618 int do_help = 0;
6619 ptrdiff_t nbytes = 0;
6620 struct frame *any, *f = NULL;
6621 struct coding_system coding;
6622 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
6623 /* This holds the state XLookupString needs to implement dead keys
6624 and other tricks known as "compose processing". _X Window System_
6625 says that a portable program can't use this, but Stephen Gildea assures
6626 me that letting the compiler initialize it to zeros will work okay. */
6627 static XComposeStatus compose_status;
6629 USE_SAFE_ALLOCA;
6631 *finish = X_EVENT_NORMAL;
6633 EVENT_INIT (inev.ie);
6634 inev.ie.kind = NO_EVENT;
6635 inev.ie.arg = Qnil;
6637 any = x_any_window_to_frame (dpyinfo, event->xany.window);
6639 if (any && any->wait_event_type == event->type)
6640 any->wait_event_type = 0; /* Indicates we got it. */
6642 switch (event->type)
6644 case ClientMessage:
6646 if (event->xclient.message_type == dpyinfo->Xatom_wm_protocols
6647 && event->xclient.format == 32)
6649 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_take_focus)
6651 /* Use the value returned by x_any_window_to_frame
6652 because this could be the shell widget window
6653 if the frame has no title bar. */
6654 f = any;
6655 #ifdef HAVE_X_I18N
6656 /* Not quite sure this is needed -pd */
6657 if (f && FRAME_XIC (f))
6658 XSetICFocus (FRAME_XIC (f));
6659 #endif
6660 #if false
6661 /* Emacs sets WM hints whose `input' field is `true'. This
6662 instructs the WM to set the input focus automatically for
6663 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
6664 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
6665 it has set the focus. So, XSetInputFocus below is not
6666 needed.
6668 The call to XSetInputFocus below has also caused trouble. In
6669 cases where the XSetInputFocus done by the WM and the one
6670 below are temporally close (on a fast machine), the call
6671 below can generate additional FocusIn events which confuse
6672 Emacs. */
6674 /* Since we set WM_TAKE_FOCUS, we must call
6675 XSetInputFocus explicitly. But not if f is null,
6676 since that might be an event for a deleted frame. */
6677 if (f)
6679 Display *d = event->xclient.display;
6680 /* Catch and ignore errors, in case window has been
6681 iconified by a window manager such as GWM. */
6682 x_catch_errors (d);
6683 XSetInputFocus (d, event->xclient.window,
6684 /* The ICCCM says this is
6685 the only valid choice. */
6686 RevertToParent,
6687 event->xclient.data.l[1]);
6688 /* This is needed to detect the error
6689 if there is an error. */
6690 XSync (d, False);
6691 x_uncatch_errors ();
6693 /* Not certain about handling scroll bars here */
6694 #endif
6695 goto done;
6698 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_save_yourself)
6700 /* Save state modify the WM_COMMAND property to
6701 something which can reinstate us. This notifies
6702 the session manager, who's looking for such a
6703 PropertyNotify. Can restart processing when
6704 a keyboard or mouse event arrives. */
6705 /* If we have a session manager, don't set this.
6706 KDE will then start two Emacsen, one for the
6707 session manager and one for this. */
6708 #ifdef HAVE_X_SM
6709 if (! x_session_have_connection ())
6710 #endif
6712 f = x_top_window_to_frame (dpyinfo,
6713 event->xclient.window);
6714 /* This is just so we only give real data once
6715 for a single Emacs process. */
6716 if (f == SELECTED_FRAME ())
6717 XSetCommand (FRAME_X_DISPLAY (f),
6718 event->xclient.window,
6719 initial_argv, initial_argc);
6720 else if (f)
6721 XSetCommand (FRAME_X_DISPLAY (f),
6722 event->xclient.window,
6723 0, 0);
6725 goto done;
6728 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_delete_window)
6730 f = any;
6731 if (!f)
6732 goto OTHER; /* May be a dialog that is to be removed */
6734 inev.ie.kind = DELETE_WINDOW_EVENT;
6735 XSETFRAME (inev.ie.frame_or_window, f);
6736 goto done;
6739 goto done;
6742 if (event->xclient.message_type == dpyinfo->Xatom_wm_configure_denied)
6743 goto done;
6745 if (event->xclient.message_type == dpyinfo->Xatom_wm_window_moved)
6747 int new_x, new_y;
6748 f = x_window_to_frame (dpyinfo, event->xclient.window);
6750 new_x = event->xclient.data.s[0];
6751 new_y = event->xclient.data.s[1];
6753 if (f)
6755 f->left_pos = new_x;
6756 f->top_pos = new_y;
6758 goto done;
6761 #ifdef HACK_EDITRES
6762 if (event->xclient.message_type == dpyinfo->Xatom_editres)
6764 f = any;
6765 if (f)
6766 _XEditResCheckMessages (f->output_data.x->widget,
6767 NULL, (XEvent *) event, NULL);
6768 goto done;
6770 #endif /* HACK_EDITRES */
6772 if (event->xclient.message_type == dpyinfo->Xatom_DONE
6773 || event->xclient.message_type == dpyinfo->Xatom_PAGE)
6775 /* Ghostview job completed. Kill it. We could
6776 reply with "Next" if we received "Page", but we
6777 currently never do because we are interested in
6778 images, only, which should have 1 page. */
6779 Pixmap pixmap = (Pixmap) event->xclient.data.l[1];
6780 f = x_window_to_frame (dpyinfo, event->xclient.window);
6781 if (!f)
6782 goto OTHER;
6783 x_kill_gs_process (pixmap, f);
6784 expose_frame (f, 0, 0, 0, 0);
6785 goto done;
6788 #ifdef USE_TOOLKIT_SCROLL_BARS
6789 /* Scroll bar callbacks send a ClientMessage from which
6790 we construct an input_event. */
6791 if (event->xclient.message_type == dpyinfo->Xatom_Scrollbar)
6793 x_scroll_bar_to_input_event (event, &inev.ie);
6794 *finish = X_EVENT_GOTO_OUT;
6795 goto done;
6797 else if (event->xclient.message_type == dpyinfo->Xatom_Horizontal_Scrollbar)
6799 x_horizontal_scroll_bar_to_input_event (event, &inev.ie);
6800 *finish = X_EVENT_GOTO_OUT;
6801 goto done;
6803 #endif /* USE_TOOLKIT_SCROLL_BARS */
6805 /* XEmbed messages from the embedder (if any). */
6806 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
6808 enum xembed_message msg = event->xclient.data.l[1];
6809 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
6810 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
6812 *finish = X_EVENT_GOTO_OUT;
6813 goto done;
6816 xft_settings_event (dpyinfo, event);
6818 f = any;
6819 if (!f)
6820 goto OTHER;
6821 if (x_handle_dnd_message (f, &event->xclient, dpyinfo, &inev.ie))
6822 *finish = X_EVENT_DROP;
6824 break;
6826 case SelectionNotify:
6827 x_display_set_last_user_time (dpyinfo, event->xselection.time);
6828 #ifdef USE_X_TOOLKIT
6829 if (! x_window_to_frame (dpyinfo, event->xselection.requestor))
6830 goto OTHER;
6831 #endif /* not USE_X_TOOLKIT */
6832 x_handle_selection_notify (&event->xselection);
6833 break;
6835 case SelectionClear: /* Someone has grabbed ownership. */
6836 x_display_set_last_user_time (dpyinfo, event->xselectionclear.time);
6837 #ifdef USE_X_TOOLKIT
6838 if (! x_window_to_frame (dpyinfo, event->xselectionclear.window))
6839 goto OTHER;
6840 #endif /* USE_X_TOOLKIT */
6842 const XSelectionClearEvent *eventp = &event->xselectionclear;
6844 inev.ie.kind = SELECTION_CLEAR_EVENT;
6845 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
6846 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6847 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6849 break;
6851 case SelectionRequest: /* Someone wants our selection. */
6852 x_display_set_last_user_time (dpyinfo, event->xselectionrequest.time);
6853 #ifdef USE_X_TOOLKIT
6854 if (!x_window_to_frame (dpyinfo, event->xselectionrequest.owner))
6855 goto OTHER;
6856 #endif /* USE_X_TOOLKIT */
6858 const XSelectionRequestEvent *eventp = &event->xselectionrequest;
6860 inev.ie.kind = SELECTION_REQUEST_EVENT;
6861 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
6862 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
6863 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6864 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
6865 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
6866 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6868 break;
6870 case PropertyNotify:
6871 x_display_set_last_user_time (dpyinfo, event->xproperty.time);
6872 f = x_top_window_to_frame (dpyinfo, event->xproperty.window);
6873 if (f && event->xproperty.atom == dpyinfo->Xatom_net_wm_state)
6875 bool not_hidden = x_handle_net_wm_state (f, &event->xproperty);
6876 if (not_hidden && FRAME_ICONIFIED_P (f))
6878 /* Gnome shell does not iconify us when C-z is pressed.
6879 It hides the frame. So if our state says we aren't
6880 hidden anymore, treat it as deiconified. */
6881 SET_FRAME_VISIBLE (f, 1);
6882 SET_FRAME_ICONIFIED (f, false);
6883 f->output_data.x->has_been_visible = true;
6884 inev.ie.kind = DEICONIFY_EVENT;
6885 XSETFRAME (inev.ie.frame_or_window, f);
6887 else if (! not_hidden && ! FRAME_ICONIFIED_P (f))
6889 SET_FRAME_VISIBLE (f, 0);
6890 SET_FRAME_ICONIFIED (f, true);
6891 inev.ie.kind = ICONIFY_EVENT;
6892 XSETFRAME (inev.ie.frame_or_window, f);
6896 x_handle_property_notify (&event->xproperty);
6897 xft_settings_event (dpyinfo, event);
6898 goto OTHER;
6900 case ReparentNotify:
6901 f = x_top_window_to_frame (dpyinfo, event->xreparent.window);
6902 if (f)
6904 f->output_data.x->parent_desc = event->xreparent.parent;
6905 x_real_positions (f, &f->left_pos, &f->top_pos);
6907 /* Perhaps reparented due to a WM restart. Reset this. */
6908 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6909 FRAME_DISPLAY_INFO (f)->net_supported_window = 0;
6911 x_set_frame_alpha (f);
6913 goto OTHER;
6915 case Expose:
6916 f = x_window_to_frame (dpyinfo, event->xexpose.window);
6917 if (f)
6919 if (!FRAME_VISIBLE_P (f))
6921 SET_FRAME_VISIBLE (f, 1);
6922 SET_FRAME_ICONIFIED (f, false);
6923 f->output_data.x->has_been_visible = true;
6924 SET_FRAME_GARBAGED (f);
6926 else
6928 #ifdef USE_GTK
6929 /* This seems to be needed for GTK 2.6 and later, see
6930 http://debbugs.gnu.org/cgi/bugreport.cgi?bug=15398. */
6931 x_clear_area (event->xexpose.display,
6932 event->xexpose.window,
6933 event->xexpose.x, event->xexpose.y,
6934 event->xexpose.width, event->xexpose.height);
6935 #endif
6936 expose_frame (f, event->xexpose.x, event->xexpose.y,
6937 event->xexpose.width, event->xexpose.height);
6940 else
6942 #ifndef USE_TOOLKIT_SCROLL_BARS
6943 struct scroll_bar *bar;
6944 #endif
6945 #if defined USE_LUCID
6946 /* Submenus of the Lucid menu bar aren't widgets
6947 themselves, so there's no way to dispatch events
6948 to them. Recognize this case separately. */
6950 Widget widget = x_window_to_menu_bar (event->xexpose.window);
6951 if (widget)
6952 xlwmenu_redisplay (widget);
6954 #endif /* USE_LUCID */
6956 #ifdef USE_TOOLKIT_SCROLL_BARS
6957 /* Dispatch event to the widget. */
6958 goto OTHER;
6959 #else /* not USE_TOOLKIT_SCROLL_BARS */
6960 bar = x_window_to_scroll_bar (event->xexpose.display,
6961 event->xexpose.window, 2);
6963 if (bar)
6964 x_scroll_bar_expose (bar, event);
6965 #ifdef USE_X_TOOLKIT
6966 else
6967 goto OTHER;
6968 #endif /* USE_X_TOOLKIT */
6969 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6971 break;
6973 case GraphicsExpose: /* This occurs when an XCopyArea's
6974 source area was obscured or not
6975 available. */
6976 f = x_window_to_frame (dpyinfo, event->xgraphicsexpose.drawable);
6977 if (f)
6978 expose_frame (f, event->xgraphicsexpose.x,
6979 event->xgraphicsexpose.y,
6980 event->xgraphicsexpose.width,
6981 event->xgraphicsexpose.height);
6982 #ifdef USE_X_TOOLKIT
6983 else
6984 goto OTHER;
6985 #endif /* USE_X_TOOLKIT */
6986 break;
6988 case NoExpose: /* This occurs when an XCopyArea's
6989 source area was completely
6990 available. */
6991 break;
6993 case UnmapNotify:
6994 /* Redo the mouse-highlight after the tooltip has gone. */
6995 if (event->xunmap.window == tip_window)
6997 tip_window = 0;
6998 x_redo_mouse_highlight (dpyinfo);
7001 f = x_top_window_to_frame (dpyinfo, event->xunmap.window);
7002 if (f) /* F may no longer exist if
7003 the frame was deleted. */
7005 bool visible = FRAME_VISIBLE_P (f);
7006 /* While a frame is unmapped, display generation is
7007 disabled; you don't want to spend time updating a
7008 display that won't ever be seen. */
7009 SET_FRAME_VISIBLE (f, 0);
7010 /* We can't distinguish, from the event, whether the window
7011 has become iconified or invisible. So assume, if it
7012 was previously visible, than now it is iconified.
7013 But x_make_frame_invisible clears both
7014 the visible flag and the iconified flag;
7015 and that way, we know the window is not iconified now. */
7016 if (visible || FRAME_ICONIFIED_P (f))
7018 SET_FRAME_ICONIFIED (f, true);
7019 inev.ie.kind = ICONIFY_EVENT;
7020 XSETFRAME (inev.ie.frame_or_window, f);
7023 goto OTHER;
7025 case MapNotify:
7026 if (event->xmap.window == tip_window)
7027 /* The tooltip has been drawn already. Avoid
7028 the SET_FRAME_GARBAGED below. */
7029 goto OTHER;
7031 /* We use x_top_window_to_frame because map events can
7032 come for sub-windows and they don't mean that the
7033 frame is visible. */
7034 f = x_top_window_to_frame (dpyinfo, event->xmap.window);
7035 if (f)
7037 bool iconified = FRAME_ICONIFIED_P (f);
7039 /* Check if fullscreen was specified before we where mapped the
7040 first time, i.e. from the command line. */
7041 if (!f->output_data.x->has_been_visible)
7042 x_check_fullscreen (f);
7044 SET_FRAME_VISIBLE (f, 1);
7045 SET_FRAME_ICONIFIED (f, false);
7046 f->output_data.x->has_been_visible = true;
7048 if (iconified)
7050 inev.ie.kind = DEICONIFY_EVENT;
7051 XSETFRAME (inev.ie.frame_or_window, f);
7053 else if (! NILP (Vframe_list) && ! NILP (XCDR (Vframe_list)))
7054 /* Force a redisplay sooner or later to update the
7055 frame titles in case this is the second frame. */
7056 record_asynch_buffer_change ();
7058 #ifdef USE_GTK
7059 xg_frame_resized (f, -1, -1);
7060 #endif
7062 goto OTHER;
7064 case KeyPress:
7066 x_display_set_last_user_time (dpyinfo, event->xkey.time);
7067 ignore_next_mouse_click_timeout = 0;
7069 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
7070 /* Dispatch KeyPress events when in menu. */
7071 if (popup_activated ())
7072 goto OTHER;
7073 #endif
7075 f = any;
7077 #if ! defined (USE_GTK)
7078 /* If mouse-highlight is an integer, input clears out
7079 mouse highlighting. */
7080 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
7081 && (f == 0
7082 || !EQ (f->tool_bar_window, hlinfo->mouse_face_window)))
7084 clear_mouse_face (hlinfo);
7085 hlinfo->mouse_face_hidden = true;
7087 #endif
7089 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
7090 if (f == 0)
7092 /* Scroll bars consume key events, but we want
7093 the keys to go to the scroll bar's frame. */
7094 Widget widget = XtWindowToWidget (dpyinfo->display,
7095 event->xkey.window);
7096 if (widget && XmIsScrollBar (widget))
7098 widget = XtParent (widget);
7099 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
7102 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
7104 if (f != 0)
7106 KeySym keysym, orig_keysym;
7107 /* al%imercury@uunet.uu.net says that making this 81
7108 instead of 80 fixed a bug whereby meta chars made
7109 his Emacs hang.
7111 It seems that some version of XmbLookupString has
7112 a bug of not returning XBufferOverflow in
7113 status_return even if the input is too long to
7114 fit in 81 bytes. So, we must prepare sufficient
7115 bytes for copy_buffer. 513 bytes (256 chars for
7116 two-byte character set) seems to be a fairly good
7117 approximation. -- 2000.8.10 handa@etl.go.jp */
7118 unsigned char copy_buffer[513];
7119 unsigned char *copy_bufptr = copy_buffer;
7120 int copy_bufsiz = sizeof (copy_buffer);
7121 int modifiers;
7122 Lisp_Object coding_system = Qlatin_1;
7123 Lisp_Object c;
7124 /* Event will be modified. */
7125 XKeyEvent xkey = event->xkey;
7127 #ifdef USE_GTK
7128 /* Don't pass keys to GTK. A Tab will shift focus to the
7129 tool bar in GTK 2.4. Keys will still go to menus and
7130 dialogs because in that case popup_activated is nonzero
7131 (see above). */
7132 *finish = X_EVENT_DROP;
7133 #endif
7135 xkey.state |= x_emacs_to_x_modifiers (FRAME_DISPLAY_INFO (f),
7136 extra_keyboard_modifiers);
7137 modifiers = xkey.state;
7139 /* This will have to go some day... */
7141 /* make_lispy_event turns chars into control chars.
7142 Don't do it here because XLookupString is too eager. */
7143 xkey.state &= ~ControlMask;
7144 xkey.state &= ~(dpyinfo->meta_mod_mask
7145 | dpyinfo->super_mod_mask
7146 | dpyinfo->hyper_mod_mask
7147 | dpyinfo->alt_mod_mask);
7149 /* In case Meta is ComposeCharacter,
7150 clear its status. According to Markus Ehrnsperger
7151 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
7152 this enables ComposeCharacter to work whether or
7153 not it is combined with Meta. */
7154 if (modifiers & dpyinfo->meta_mod_mask)
7155 memset (&compose_status, 0, sizeof (compose_status));
7157 #ifdef HAVE_X_I18N
7158 if (FRAME_XIC (f))
7160 Status status_return;
7162 coding_system = Vlocale_coding_system;
7163 nbytes = XmbLookupString (FRAME_XIC (f),
7164 &xkey, (char *) copy_bufptr,
7165 copy_bufsiz, &keysym,
7166 &status_return);
7167 if (status_return == XBufferOverflow)
7169 copy_bufsiz = nbytes + 1;
7170 copy_bufptr = alloca (copy_bufsiz);
7171 nbytes = XmbLookupString (FRAME_XIC (f),
7172 &xkey, (char *) copy_bufptr,
7173 copy_bufsiz, &keysym,
7174 &status_return);
7176 /* Xutf8LookupString is a new but already deprecated interface. -stef */
7177 if (status_return == XLookupNone)
7178 break;
7179 else if (status_return == XLookupChars)
7181 keysym = NoSymbol;
7182 modifiers = 0;
7184 else if (status_return != XLookupKeySym
7185 && status_return != XLookupBoth)
7186 emacs_abort ();
7188 else
7189 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
7190 copy_bufsiz, &keysym,
7191 &compose_status);
7192 #else
7193 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
7194 copy_bufsiz, &keysym,
7195 &compose_status);
7196 #endif
7198 /* If not using XIM/XIC, and a compose sequence is in progress,
7199 we break here. Otherwise, chars_matched is always 0. */
7200 if (compose_status.chars_matched > 0 && nbytes == 0)
7201 break;
7203 memset (&compose_status, 0, sizeof (compose_status));
7204 orig_keysym = keysym;
7206 /* Common for all keysym input events. */
7207 XSETFRAME (inev.ie.frame_or_window, f);
7208 inev.ie.modifiers
7209 = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), modifiers);
7210 inev.ie.timestamp = xkey.time;
7212 /* First deal with keysyms which have defined
7213 translations to characters. */
7214 if (keysym >= 32 && keysym < 128)
7215 /* Avoid explicitly decoding each ASCII character. */
7217 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
7218 inev.ie.code = keysym;
7219 goto done_keysym;
7222 /* Keysyms directly mapped to Unicode characters. */
7223 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
7225 if (keysym < 0x01000080)
7226 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
7227 else
7228 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
7229 inev.ie.code = keysym & 0xFFFFFF;
7230 goto done_keysym;
7233 /* Now non-ASCII. */
7234 if (HASH_TABLE_P (Vx_keysym_table)
7235 && (c = Fgethash (make_number (keysym),
7236 Vx_keysym_table,
7237 Qnil),
7238 NATNUMP (c)))
7240 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
7241 ? ASCII_KEYSTROKE_EVENT
7242 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
7243 inev.ie.code = XFASTINT (c);
7244 goto done_keysym;
7247 /* Random non-modifier sorts of keysyms. */
7248 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
7249 || keysym == XK_Delete
7250 #ifdef XK_ISO_Left_Tab
7251 || (keysym >= XK_ISO_Left_Tab
7252 && keysym <= XK_ISO_Enter)
7253 #endif
7254 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
7255 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
7256 #ifdef HPUX
7257 /* This recognizes the "extended function
7258 keys". It seems there's no cleaner way.
7259 Test IsModifierKey to avoid handling
7260 mode_switch incorrectly. */
7261 || (XK_Select <= keysym && keysym < XK_KP_Space)
7262 #endif
7263 #ifdef XK_dead_circumflex
7264 || orig_keysym == XK_dead_circumflex
7265 #endif
7266 #ifdef XK_dead_grave
7267 || orig_keysym == XK_dead_grave
7268 #endif
7269 #ifdef XK_dead_tilde
7270 || orig_keysym == XK_dead_tilde
7271 #endif
7272 #ifdef XK_dead_diaeresis
7273 || orig_keysym == XK_dead_diaeresis
7274 #endif
7275 #ifdef XK_dead_macron
7276 || orig_keysym == XK_dead_macron
7277 #endif
7278 #ifdef XK_dead_degree
7279 || orig_keysym == XK_dead_degree
7280 #endif
7281 #ifdef XK_dead_acute
7282 || orig_keysym == XK_dead_acute
7283 #endif
7284 #ifdef XK_dead_cedilla
7285 || orig_keysym == XK_dead_cedilla
7286 #endif
7287 #ifdef XK_dead_breve
7288 || orig_keysym == XK_dead_breve
7289 #endif
7290 #ifdef XK_dead_ogonek
7291 || orig_keysym == XK_dead_ogonek
7292 #endif
7293 #ifdef XK_dead_caron
7294 || orig_keysym == XK_dead_caron
7295 #endif
7296 #ifdef XK_dead_doubleacute
7297 || orig_keysym == XK_dead_doubleacute
7298 #endif
7299 #ifdef XK_dead_abovedot
7300 || orig_keysym == XK_dead_abovedot
7301 #endif
7302 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
7303 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
7304 /* Any "vendor-specific" key is ok. */
7305 || (orig_keysym & (1 << 28))
7306 || (keysym != NoSymbol && nbytes == 0))
7307 && ! (IsModifierKey (orig_keysym)
7308 /* The symbols from XK_ISO_Lock
7309 to XK_ISO_Last_Group_Lock
7310 don't have real modifiers but
7311 should be treated similarly to
7312 Mode_switch by Emacs. */
7313 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
7314 || (XK_ISO_Lock <= orig_keysym
7315 && orig_keysym <= XK_ISO_Last_Group_Lock)
7316 #endif
7319 STORE_KEYSYM_FOR_DEBUG (keysym);
7320 /* make_lispy_event will convert this to a symbolic
7321 key. */
7322 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
7323 inev.ie.code = keysym;
7324 goto done_keysym;
7327 { /* Raw bytes, not keysym. */
7328 ptrdiff_t i;
7329 int nchars, len;
7331 for (i = 0, nchars = 0; i < nbytes; i++)
7333 if (ASCII_CHAR_P (copy_bufptr[i]))
7334 nchars++;
7335 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
7338 if (nchars < nbytes)
7340 /* Decode the input data. */
7342 /* The input should be decoded with `coding_system'
7343 which depends on which X*LookupString function
7344 we used just above and the locale. */
7345 setup_coding_system (coding_system, &coding);
7346 coding.src_multibyte = false;
7347 coding.dst_multibyte = true;
7348 /* The input is converted to events, thus we can't
7349 handle composition. Anyway, there's no XIM that
7350 gives us composition information. */
7351 coding.common_flags &= ~CODING_ANNOTATION_MASK;
7353 SAFE_NALLOCA (coding.destination, MAX_MULTIBYTE_LENGTH,
7354 nbytes);
7355 coding.dst_bytes = MAX_MULTIBYTE_LENGTH * nbytes;
7356 coding.mode |= CODING_MODE_LAST_BLOCK;
7357 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
7358 nbytes = coding.produced;
7359 nchars = coding.produced_char;
7360 copy_bufptr = coding.destination;
7363 /* Convert the input data to a sequence of
7364 character events. */
7365 for (i = 0; i < nbytes; i += len)
7367 int ch;
7368 if (nchars == nbytes)
7369 ch = copy_bufptr[i], len = 1;
7370 else
7371 ch = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
7372 inev.ie.kind = (SINGLE_BYTE_CHAR_P (ch)
7373 ? ASCII_KEYSTROKE_EVENT
7374 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
7375 inev.ie.code = ch;
7376 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
7379 count += nchars;
7381 inev.ie.kind = NO_EVENT; /* Already stored above. */
7383 if (keysym == NoSymbol)
7384 break;
7386 /* FIXME: check side effects and remove this. */
7387 ((XEvent *) event)->xkey = xkey;
7389 done_keysym:
7390 #ifdef HAVE_X_I18N
7391 /* Don't dispatch this event since XtDispatchEvent calls
7392 XFilterEvent, and two calls in a row may freeze the
7393 client. */
7394 break;
7395 #else
7396 goto OTHER;
7397 #endif
7399 case KeyRelease:
7400 x_display_set_last_user_time (dpyinfo, event->xkey.time);
7401 #ifdef HAVE_X_I18N
7402 /* Don't dispatch this event since XtDispatchEvent calls
7403 XFilterEvent, and two calls in a row may freeze the
7404 client. */
7405 break;
7406 #else
7407 goto OTHER;
7408 #endif
7410 case EnterNotify:
7411 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
7412 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7414 f = any;
7416 if (f && x_mouse_click_focus_ignore_position)
7417 ignore_next_mouse_click_timeout = event->xmotion.time + 200;
7419 /* EnterNotify counts as mouse movement,
7420 so update things that depend on mouse position. */
7421 if (f && !f->output_data.x->hourglass_p)
7422 note_mouse_movement (f, &event->xmotion);
7423 #ifdef USE_GTK
7424 /* We may get an EnterNotify on the buttons in the toolbar. In that
7425 case we moved out of any highlighted area and need to note this. */
7426 if (!f && dpyinfo->last_mouse_glyph_frame)
7427 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
7428 #endif
7429 goto OTHER;
7431 case FocusIn:
7432 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7433 goto OTHER;
7435 case LeaveNotify:
7436 x_display_set_last_user_time (dpyinfo, event->xcrossing.time);
7437 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7439 f = x_top_window_to_frame (dpyinfo, event->xcrossing.window);
7440 if (f)
7442 if (f == hlinfo->mouse_face_mouse_frame)
7444 /* If we move outside the frame, then we're
7445 certainly no longer on any text in the frame. */
7446 clear_mouse_face (hlinfo);
7447 hlinfo->mouse_face_mouse_frame = 0;
7450 /* Generate a nil HELP_EVENT to cancel a help-echo.
7451 Do it only if there's something to cancel.
7452 Otherwise, the startup message is cleared when
7453 the mouse leaves the frame. */
7454 if (any_help_event_p)
7455 do_help = -1;
7457 #ifdef USE_GTK
7458 /* See comment in EnterNotify above */
7459 else if (dpyinfo->last_mouse_glyph_frame)
7460 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
7461 #endif
7462 goto OTHER;
7464 case FocusOut:
7465 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7466 goto OTHER;
7468 case MotionNotify:
7470 x_display_set_last_user_time (dpyinfo, event->xmotion.time);
7471 previous_help_echo_string = help_echo_string;
7472 help_echo_string = Qnil;
7474 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
7475 : x_window_to_frame (dpyinfo, event->xmotion.window));
7477 if (hlinfo->mouse_face_hidden)
7479 hlinfo->mouse_face_hidden = false;
7480 clear_mouse_face (hlinfo);
7483 #ifdef USE_GTK
7484 if (f && xg_event_is_for_scrollbar (f, event))
7485 f = 0;
7486 #endif
7487 if (f)
7490 /* Generate SELECT_WINDOW_EVENTs when needed.
7491 Don't let popup menus influence things (bug#1261). */
7492 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
7494 static Lisp_Object last_mouse_window;
7495 Lisp_Object window = window_from_coordinates
7496 (f, event->xmotion.x, event->xmotion.y, 0, false);
7498 /* Window will be selected only when it is not selected now and
7499 last mouse movement event was not in it. Minibuffer window
7500 will be selected only when it is active. */
7501 if (WINDOWP (window)
7502 && !EQ (window, last_mouse_window)
7503 && !EQ (window, selected_window)
7504 /* For click-to-focus window managers
7505 create event iff we don't leave the
7506 selected frame. */
7507 && (focus_follows_mouse
7508 || (EQ (XWINDOW (window)->frame,
7509 XWINDOW (selected_window)->frame))))
7511 inev.ie.kind = SELECT_WINDOW_EVENT;
7512 inev.ie.frame_or_window = window;
7514 /* Remember the last window where we saw the mouse. */
7515 last_mouse_window = window;
7517 if (!note_mouse_movement (f, &event->xmotion))
7518 help_echo_string = previous_help_echo_string;
7520 else
7522 #ifndef USE_TOOLKIT_SCROLL_BARS
7523 struct scroll_bar *bar
7524 = x_window_to_scroll_bar (event->xmotion.display,
7525 event->xmotion.window, 2);
7527 if (bar)
7528 x_scroll_bar_note_movement (bar, &event->xmotion);
7529 #endif /* USE_TOOLKIT_SCROLL_BARS */
7531 /* If we move outside the frame, then we're
7532 certainly no longer on any text in the frame. */
7533 clear_mouse_face (hlinfo);
7536 /* If the contents of the global variable help_echo_string
7537 has changed, generate a HELP_EVENT. */
7538 if (!NILP (help_echo_string)
7539 || !NILP (previous_help_echo_string))
7540 do_help = 1;
7541 goto OTHER;
7544 case ConfigureNotify:
7545 f = x_top_window_to_frame (dpyinfo, event->xconfigure.window);
7546 #ifdef USE_GTK
7547 if (!f
7548 && (f = any)
7549 && event->xconfigure.window == FRAME_X_WINDOW (f))
7551 xg_frame_resized (f, event->xconfigure.width,
7552 event->xconfigure.height);
7553 f = 0;
7555 #endif
7556 if (f)
7558 x_net_wm_state (f, event->xconfigure.window);
7560 #ifndef USE_X_TOOLKIT
7561 #ifndef USE_GTK
7562 int width = FRAME_PIXEL_TO_TEXT_WIDTH (f, event->xconfigure.width);
7563 int height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, event->xconfigure.height);
7565 /* In the toolkit version, change_frame_size
7566 is called by the code that handles resizing
7567 of the EmacsFrame widget. */
7569 /* Even if the number of character rows and columns has
7570 not changed, the font size may have changed, so we need
7571 to check the pixel dimensions as well. */
7572 if (width != FRAME_TEXT_WIDTH (f)
7573 || height != FRAME_TEXT_HEIGHT (f)
7574 || event->xconfigure.width != FRAME_PIXEL_WIDTH (f)
7575 || event->xconfigure.height != FRAME_PIXEL_HEIGHT (f))
7577 change_frame_size (f, width, height, false, true, false, true);
7578 x_clear_under_internal_border (f);
7579 SET_FRAME_GARBAGED (f);
7580 cancel_mouse_face (f);
7582 #endif /* not USE_GTK */
7583 #endif
7585 #ifdef USE_GTK
7586 /* GTK creates windows but doesn't map them.
7587 Only get real positions when mapped. */
7588 if (FRAME_GTK_OUTER_WIDGET (f)
7589 && gtk_widget_get_mapped (FRAME_GTK_OUTER_WIDGET (f)))
7590 #endif
7591 x_real_positions (f, &f->left_pos, &f->top_pos);
7593 #ifdef HAVE_X_I18N
7594 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
7595 xic_set_statusarea (f);
7596 #endif
7599 goto OTHER;
7601 case ButtonRelease:
7602 case ButtonPress:
7604 /* If we decide we want to generate an event to be seen
7605 by the rest of Emacs, we put it here. */
7606 bool tool_bar_p = false;
7608 memset (&compose_status, 0, sizeof (compose_status));
7609 dpyinfo->last_mouse_glyph_frame = NULL;
7610 x_display_set_last_user_time (dpyinfo, event->xbutton.time);
7612 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
7613 : x_window_to_frame (dpyinfo, event->xbutton.window));
7615 #ifdef USE_GTK
7616 if (f && xg_event_is_for_scrollbar (f, event))
7617 f = 0;
7618 #endif
7619 if (f)
7621 #if ! defined (USE_GTK)
7622 /* Is this in the tool-bar? */
7623 if (WINDOWP (f->tool_bar_window)
7624 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
7626 Lisp_Object window;
7627 int x = event->xbutton.x;
7628 int y = event->xbutton.y;
7630 window = window_from_coordinates (f, x, y, 0, true);
7631 tool_bar_p = EQ (window, f->tool_bar_window);
7633 if (tool_bar_p && event->xbutton.button < 4)
7634 handle_tool_bar_click
7635 (f, x, y, event->xbutton.type == ButtonPress,
7636 x_x_to_emacs_modifiers (dpyinfo, event->xbutton.state));
7638 #endif /* !USE_GTK */
7640 if (!tool_bar_p)
7641 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
7642 if (! popup_activated ())
7643 #endif
7645 if (ignore_next_mouse_click_timeout)
7647 if (event->type == ButtonPress
7648 && event->xbutton.time > ignore_next_mouse_click_timeout)
7650 ignore_next_mouse_click_timeout = 0;
7651 construct_mouse_click (&inev.ie, &event->xbutton, f);
7653 if (event->type == ButtonRelease)
7654 ignore_next_mouse_click_timeout = 0;
7656 else
7657 construct_mouse_click (&inev.ie, &event->xbutton, f);
7659 if (FRAME_X_EMBEDDED_P (f))
7660 xembed_send_message (f, event->xbutton.time,
7661 XEMBED_REQUEST_FOCUS, 0, 0, 0);
7663 else
7665 struct scroll_bar *bar
7666 = x_window_to_scroll_bar (event->xbutton.display,
7667 event->xbutton.window, 2);
7669 #ifdef USE_TOOLKIT_SCROLL_BARS
7670 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
7671 scroll bars. */
7672 if (bar && event->xbutton.state & ControlMask)
7674 x_scroll_bar_handle_click (bar, event, &inev.ie);
7675 *finish = X_EVENT_DROP;
7677 #else /* not USE_TOOLKIT_SCROLL_BARS */
7678 if (bar)
7679 x_scroll_bar_handle_click (bar, event, &inev.ie);
7680 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7683 if (event->type == ButtonPress)
7685 dpyinfo->grabbed |= (1 << event->xbutton.button);
7686 dpyinfo->last_mouse_frame = f;
7687 #if ! defined (USE_GTK)
7688 if (f && !tool_bar_p)
7689 f->last_tool_bar_item = -1;
7690 #endif /* not USE_GTK */
7692 else
7693 dpyinfo->grabbed &= ~(1 << event->xbutton.button);
7695 /* Ignore any mouse motion that happened before this event;
7696 any subsequent mouse-movement Emacs events should reflect
7697 only motion after the ButtonPress/Release. */
7698 if (f != 0)
7699 f->mouse_moved = false;
7701 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
7702 f = x_menubar_window_to_frame (dpyinfo, event);
7703 /* For a down-event in the menu bar,
7704 don't pass it to Xt right now.
7705 Instead, save it away
7706 and we will pass it to Xt from kbd_buffer_get_event.
7707 That way, we can run some Lisp code first. */
7708 if (! popup_activated ()
7709 #ifdef USE_GTK
7710 /* Gtk+ menus only react to the first three buttons. */
7711 && event->xbutton.button < 3
7712 #endif
7713 && f && event->type == ButtonPress
7714 /* Verify the event is really within the menu bar
7715 and not just sent to it due to grabbing. */
7716 && event->xbutton.x >= 0
7717 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
7718 && event->xbutton.y >= 0
7719 && event->xbutton.y < FRAME_MENUBAR_HEIGHT (f)
7720 && event->xbutton.same_screen)
7722 if (!f->output_data.x->saved_menu_event)
7723 f->output_data.x->saved_menu_event = xmalloc (sizeof *event);
7724 *f->output_data.x->saved_menu_event = *event;
7725 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT;
7726 XSETFRAME (inev.ie.frame_or_window, f);
7727 *finish = X_EVENT_DROP;
7729 else
7730 goto OTHER;
7731 #endif /* USE_X_TOOLKIT || USE_GTK */
7733 break;
7735 case CirculateNotify:
7736 goto OTHER;
7738 case CirculateRequest:
7739 goto OTHER;
7741 case VisibilityNotify:
7742 goto OTHER;
7744 case MappingNotify:
7745 /* Someone has changed the keyboard mapping - update the
7746 local cache. */
7747 switch (event->xmapping.request)
7749 case MappingModifier:
7750 x_find_modifier_meanings (dpyinfo);
7751 /* This is meant to fall through. */
7752 case MappingKeyboard:
7753 XRefreshKeyboardMapping ((XMappingEvent *) &event->xmapping);
7755 goto OTHER;
7757 case DestroyNotify:
7758 xft_settings_event (dpyinfo, event);
7759 break;
7761 default:
7762 OTHER:
7763 #ifdef USE_X_TOOLKIT
7764 block_input ();
7765 if (*finish != X_EVENT_DROP)
7766 XtDispatchEvent ((XEvent *) event);
7767 unblock_input ();
7768 #endif /* USE_X_TOOLKIT */
7769 break;
7772 done:
7773 if (inev.ie.kind != NO_EVENT)
7775 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
7776 count++;
7779 if (do_help
7780 && !(hold_quit && hold_quit->kind != NO_EVENT))
7782 Lisp_Object frame;
7784 if (f)
7785 XSETFRAME (frame, f);
7786 else
7787 frame = Qnil;
7789 if (do_help > 0)
7791 any_help_event_p = true;
7792 gen_help_event (help_echo_string, frame, help_echo_window,
7793 help_echo_object, help_echo_pos);
7795 else
7797 help_echo_string = Qnil;
7798 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
7800 count++;
7803 SAFE_FREE ();
7804 return count;
7807 /* Handles the XEvent EVENT on display DISPLAY.
7808 This is used for event loops outside the normal event handling,
7809 i.e. looping while a popup menu or a dialog is posted.
7811 Returns the value handle_one_xevent sets in the finish argument. */
7813 x_dispatch_event (XEvent *event, Display *display)
7815 struct x_display_info *dpyinfo;
7816 int finish = X_EVENT_NORMAL;
7818 dpyinfo = x_display_info_for_display (display);
7820 if (dpyinfo)
7821 handle_one_xevent (dpyinfo, event, &finish, 0);
7823 return finish;
7826 /* Read events coming from the X server.
7827 Return as soon as there are no more events to be read.
7829 Return the number of characters stored into the buffer,
7830 thus pretending to be `read' (except the characters we store
7831 in the keyboard buffer can be multibyte, so are not necessarily
7832 C chars). */
7834 static int
7835 XTread_socket (struct terminal *terminal, struct input_event *hold_quit)
7837 int count = 0;
7838 bool event_found = false;
7839 struct x_display_info *dpyinfo = terminal->display_info.x;
7841 block_input ();
7843 /* For debugging, this gives a way to fake an I/O error. */
7844 if (dpyinfo == XTread_socket_fake_io_error)
7846 XTread_socket_fake_io_error = 0;
7847 x_io_error_quitter (dpyinfo->display);
7850 #ifndef USE_GTK
7851 while (XPending (dpyinfo->display))
7853 int finish;
7854 XEvent event;
7856 XNextEvent (dpyinfo->display, &event);
7858 #ifdef HAVE_X_I18N
7859 /* Filter events for the current X input method. */
7860 if (x_filter_event (dpyinfo, &event))
7861 continue;
7862 #endif
7863 event_found = true;
7865 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
7867 if (finish == X_EVENT_GOTO_OUT)
7868 break;
7871 #else /* USE_GTK */
7873 /* For GTK we must use the GTK event loop. But XEvents gets passed
7874 to our filter function above, and then to the big event switch.
7875 We use a bunch of globals to communicate with our filter function,
7876 that is kind of ugly, but it works.
7878 There is no way to do one display at the time, GTK just does events
7879 from all displays. */
7881 while (gtk_events_pending ())
7883 current_count = count;
7884 current_hold_quit = hold_quit;
7886 gtk_main_iteration ();
7888 count = current_count;
7889 current_count = -1;
7890 current_hold_quit = 0;
7892 if (current_finish == X_EVENT_GOTO_OUT)
7893 break;
7895 #endif /* USE_GTK */
7897 /* On some systems, an X bug causes Emacs to get no more events
7898 when the window is destroyed. Detect that. (1994.) */
7899 if (! event_found)
7901 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7902 One XNOOP in 100 loops will make Emacs terminate.
7903 B. Bretthauer, 1994 */
7904 x_noop_count++;
7905 if (x_noop_count >= 100)
7907 x_noop_count=0;
7909 if (next_noop_dpyinfo == 0)
7910 next_noop_dpyinfo = x_display_list;
7912 XNoOp (next_noop_dpyinfo->display);
7914 /* Each time we get here, cycle through the displays now open. */
7915 next_noop_dpyinfo = next_noop_dpyinfo->next;
7919 /* If the focus was just given to an auto-raising frame,
7920 raise it now. FIXME: handle more than one such frame. */
7921 if (dpyinfo->x_pending_autoraise_frame)
7923 x_raise_frame (dpyinfo->x_pending_autoraise_frame);
7924 dpyinfo->x_pending_autoraise_frame = NULL;
7927 unblock_input ();
7929 return count;
7935 /***********************************************************************
7936 Text Cursor
7937 ***********************************************************************/
7939 /* Set clipping for output in glyph row ROW. W is the window in which
7940 we operate. GC is the graphics context to set clipping in.
7942 ROW may be a text row or, e.g., a mode line. Text rows must be
7943 clipped to the interior of the window dedicated to text display,
7944 mode lines must be clipped to the whole window. */
7946 static void
7947 x_clip_to_row (struct window *w, struct glyph_row *row,
7948 enum glyph_row_area area, GC gc)
7950 struct frame *f = XFRAME (WINDOW_FRAME (w));
7951 XRectangle clip_rect;
7952 int window_x, window_y, window_width;
7954 window_box (w, area, &window_x, &window_y, &window_width, 0);
7956 clip_rect.x = window_x;
7957 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7958 clip_rect.y = max (clip_rect.y, window_y);
7959 clip_rect.width = window_width;
7960 clip_rect.height = row->visible_height;
7962 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7966 /* Draw a hollow box cursor on window W in glyph row ROW. */
7968 static void
7969 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
7971 struct frame *f = XFRAME (WINDOW_FRAME (w));
7972 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
7973 Display *dpy = FRAME_X_DISPLAY (f);
7974 int x, y, wd, h;
7975 XGCValues xgcv;
7976 struct glyph *cursor_glyph;
7977 GC gc;
7979 /* Get the glyph the cursor is on. If we can't tell because
7980 the current matrix is invalid or such, give up. */
7981 cursor_glyph = get_phys_cursor_glyph (w);
7982 if (cursor_glyph == NULL)
7983 return;
7985 /* Compute frame-relative coordinates for phys cursor. */
7986 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7987 wd = w->phys_cursor_width - 1;
7989 /* The foreground of cursor_gc is typically the same as the normal
7990 background color, which can cause the cursor box to be invisible. */
7991 xgcv.foreground = f->output_data.x->cursor_pixel;
7992 if (dpyinfo->scratch_cursor_gc)
7993 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7994 else
7995 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7996 GCForeground, &xgcv);
7997 gc = dpyinfo->scratch_cursor_gc;
7999 /* When on R2L character, show cursor at the right edge of the
8000 glyph, unless the cursor box is as wide as the glyph or wider
8001 (the latter happens when x-stretch-cursor is non-nil). */
8002 if ((cursor_glyph->resolved_level & 1) != 0
8003 && cursor_glyph->pixel_width > wd)
8005 x += cursor_glyph->pixel_width - wd;
8006 if (wd > 0)
8007 wd -= 1;
8009 /* Set clipping, draw the rectangle, and reset clipping again. */
8010 x_clip_to_row (w, row, TEXT_AREA, gc);
8011 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
8012 XSetClipMask (dpy, gc, None);
8016 /* Draw a bar cursor on window W in glyph row ROW.
8018 Implementation note: One would like to draw a bar cursor with an
8019 angle equal to the one given by the font property XA_ITALIC_ANGLE.
8020 Unfortunately, I didn't find a font yet that has this property set.
8021 --gerd. */
8023 static void
8024 x_draw_bar_cursor (struct window *w, struct glyph_row *row, int width, enum text_cursor_kinds kind)
8026 struct frame *f = XFRAME (w->frame);
8027 struct glyph *cursor_glyph;
8029 /* If cursor is out of bounds, don't draw garbage. This can happen
8030 in mini-buffer windows when switching between echo area glyphs
8031 and mini-buffer. */
8032 cursor_glyph = get_phys_cursor_glyph (w);
8033 if (cursor_glyph == NULL)
8034 return;
8035 #ifdef HAVE_XWIDGETS
8036 if (cursor_glyph->type == XWIDGET_GLYPH){
8037 printf("tried avoiding xwidget cursor\n");
8038 return; //experimental avoidance of cursor on xwidget
8040 #endif
8041 /* If on an image, draw like a normal cursor. That's usually better
8042 visible than drawing a bar, esp. if the image is large so that
8043 the bar might not be in the window. */
8044 if (cursor_glyph->type == IMAGE_GLYPH)
8046 struct glyph_row *r;
8047 r = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
8048 draw_phys_cursor_glyph (w, r, DRAW_CURSOR);
8050 else
8052 Display *dpy = FRAME_X_DISPLAY (f);
8053 Window window = FRAME_X_WINDOW (f);
8054 GC gc = FRAME_DISPLAY_INFO (f)->scratch_cursor_gc;
8055 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
8056 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
8057 XGCValues xgcv;
8059 /* If the glyph's background equals the color we normally draw
8060 the bars cursor in, the bar cursor in its normal color is
8061 invisible. Use the glyph's foreground color instead in this
8062 case, on the assumption that the glyph's colors are chosen so
8063 that the glyph is legible. */
8064 if (face->background == f->output_data.x->cursor_pixel)
8065 xgcv.background = xgcv.foreground = face->foreground;
8066 else
8067 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
8068 xgcv.graphics_exposures = False;
8070 if (gc)
8071 XChangeGC (dpy, gc, mask, &xgcv);
8072 else
8074 gc = XCreateGC (dpy, window, mask, &xgcv);
8075 FRAME_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
8078 x_clip_to_row (w, row, TEXT_AREA, gc);
8080 if (kind == BAR_CURSOR)
8082 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8084 if (width < 0)
8085 width = FRAME_CURSOR_WIDTH (f);
8086 width = min (cursor_glyph->pixel_width, width);
8088 w->phys_cursor_width = width;
8090 /* If the character under cursor is R2L, draw the bar cursor
8091 on the right of its glyph, rather than on the left. */
8092 if ((cursor_glyph->resolved_level & 1) != 0)
8093 x += cursor_glyph->pixel_width - width;
8095 XFillRectangle (dpy, window, gc, x,
8096 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
8097 width, row->height);
8099 else /* HBAR_CURSOR */
8101 int dummy_x, dummy_y, dummy_h;
8102 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8104 if (width < 0)
8105 width = row->height;
8107 width = min (row->height, width);
8109 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
8110 &dummy_y, &dummy_h);
8112 if ((cursor_glyph->resolved_level & 1) != 0
8113 && cursor_glyph->pixel_width > w->phys_cursor_width - 1)
8114 x += cursor_glyph->pixel_width - w->phys_cursor_width + 1;
8115 XFillRectangle (dpy, window, gc, x,
8116 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
8117 row->height - width),
8118 w->phys_cursor_width - 1, width);
8121 XSetClipMask (dpy, gc, None);
8126 /* RIF: Define cursor CURSOR on frame F. */
8128 static void
8129 x_define_frame_cursor (struct frame *f, Cursor cursor)
8131 if (!f->pointer_invisible
8132 && f->output_data.x->current_cursor != cursor)
8133 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
8134 f->output_data.x->current_cursor = cursor;
8138 /* RIF: Clear area on frame F. */
8140 static void
8141 x_clear_frame_area (struct frame *f, int x, int y, int width, int height)
8143 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), x, y, width, height);
8144 #ifdef USE_GTK
8145 /* Must queue a redraw, because scroll bars might have been cleared. */
8146 if (FRAME_GTK_WIDGET (f))
8147 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
8148 #endif
8152 /* RIF: Draw cursor on window W. */
8154 static void
8155 x_draw_window_cursor (struct window *w, struct glyph_row *glyph_row, int x,
8156 int y, enum text_cursor_kinds cursor_type,
8157 int cursor_width, bool on_p, bool active_p)
8159 struct frame *f = XFRAME (WINDOW_FRAME (w));
8161 if (on_p)
8163 w->phys_cursor_type = cursor_type;
8164 w->phys_cursor_on_p = true;
8166 if (glyph_row->exact_window_width_line_p
8167 && (glyph_row->reversed_p
8168 ? (w->phys_cursor.hpos < 0)
8169 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
8171 glyph_row->cursor_in_fringe_p = true;
8172 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
8174 else
8176 switch (cursor_type)
8178 case HOLLOW_BOX_CURSOR:
8179 x_draw_hollow_cursor (w, glyph_row);
8180 break;
8182 case FILLED_BOX_CURSOR:
8183 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
8184 break;
8186 case BAR_CURSOR:
8187 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
8188 break;
8190 case HBAR_CURSOR:
8191 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
8192 break;
8194 case NO_CURSOR:
8195 w->phys_cursor_width = 0;
8196 break;
8198 default:
8199 emacs_abort ();
8203 #ifdef HAVE_X_I18N
8204 if (w == XWINDOW (f->selected_window))
8205 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
8206 xic_set_preeditarea (w, x, y);
8207 #endif
8210 XFlush (FRAME_X_DISPLAY (f));
8214 /* Icons. */
8216 /* Make the x-window of frame F use the gnu icon bitmap. */
8218 bool
8219 x_bitmap_icon (struct frame *f, Lisp_Object file)
8221 ptrdiff_t bitmap_id;
8223 if (FRAME_X_WINDOW (f) == 0)
8224 return true;
8226 /* Free up our existing icon bitmap and mask if any. */
8227 if (f->output_data.x->icon_bitmap > 0)
8228 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
8229 f->output_data.x->icon_bitmap = 0;
8231 if (STRINGP (file))
8233 #ifdef USE_GTK
8234 /* Use gtk_window_set_icon_from_file () if available,
8235 It's not restricted to bitmaps */
8236 if (xg_set_icon (f, file))
8237 return false;
8238 #endif /* USE_GTK */
8239 bitmap_id = x_create_bitmap_from_file (f, file);
8240 x_create_bitmap_mask (f, bitmap_id);
8242 else
8244 /* Create the GNU bitmap and mask if necessary. */
8245 if (FRAME_DISPLAY_INFO (f)->icon_bitmap_id < 0)
8247 ptrdiff_t rc = -1;
8249 #ifdef USE_GTK
8251 if (xg_set_icon (f, xg_default_icon_file)
8252 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
8254 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = -2;
8255 return false;
8258 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
8260 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
8261 if (rc != -1)
8262 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
8264 #endif
8266 /* If all else fails, use the (black and white) xbm image. */
8267 if (rc == -1)
8269 rc = x_create_bitmap_from_data (f, (char *) gnu_xbm_bits,
8270 gnu_xbm_width, gnu_xbm_height);
8271 if (rc == -1)
8272 return true;
8274 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
8275 x_create_bitmap_mask (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
8279 /* The first time we create the GNU bitmap and mask,
8280 this increments the ref-count one extra time.
8281 As a result, the GNU bitmap and mask are never freed.
8282 That way, we don't have to worry about allocating it again. */
8283 x_reference_bitmap (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
8285 bitmap_id = FRAME_DISPLAY_INFO (f)->icon_bitmap_id;
8288 x_wm_set_icon_pixmap (f, bitmap_id);
8289 f->output_data.x->icon_bitmap = bitmap_id;
8291 return false;
8295 /* Make the x-window of frame F use a rectangle with text.
8296 Use ICON_NAME as the text. */
8298 bool
8299 x_text_icon (struct frame *f, const char *icon_name)
8301 if (FRAME_X_WINDOW (f) == 0)
8302 return true;
8305 XTextProperty text;
8306 text.value = (unsigned char *) icon_name;
8307 text.encoding = XA_STRING;
8308 text.format = 8;
8309 text.nitems = strlen (icon_name);
8310 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
8313 if (f->output_data.x->icon_bitmap > 0)
8314 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
8315 f->output_data.x->icon_bitmap = 0;
8316 x_wm_set_icon_pixmap (f, 0);
8318 return false;
8321 #define X_ERROR_MESSAGE_SIZE 200
8323 /* If non-nil, this should be a string.
8324 It means catch X errors and store the error message in this string.
8326 The reason we use a stack is that x_catch_error/x_uncatch_error can
8327 be called from a signal handler.
8330 struct x_error_message_stack {
8331 char string[X_ERROR_MESSAGE_SIZE];
8332 Display *dpy;
8333 struct x_error_message_stack *prev;
8335 static struct x_error_message_stack *x_error_message;
8337 /* An X error handler which stores the error message in
8338 *x_error_message. This is called from x_error_handler if
8339 x_catch_errors is in effect. */
8341 static void
8342 x_error_catcher (Display *display, XErrorEvent *event)
8344 XGetErrorText (display, event->error_code,
8345 x_error_message->string,
8346 X_ERROR_MESSAGE_SIZE);
8349 /* Begin trapping X errors for display DPY. Actually we trap X errors
8350 for all displays, but DPY should be the display you are actually
8351 operating on.
8353 After calling this function, X protocol errors no longer cause
8354 Emacs to exit; instead, they are recorded in the string
8355 stored in *x_error_message.
8357 Calling x_check_errors signals an Emacs error if an X error has
8358 occurred since the last call to x_catch_errors or x_check_errors.
8360 Calling x_uncatch_errors resumes the normal error handling. */
8362 void
8363 x_catch_errors (Display *dpy)
8365 struct x_error_message_stack *data = xmalloc (sizeof *data);
8367 /* Make sure any errors from previous requests have been dealt with. */
8368 XSync (dpy, False);
8370 data->dpy = dpy;
8371 data->string[0] = 0;
8372 data->prev = x_error_message;
8373 x_error_message = data;
8376 /* Undo the last x_catch_errors call.
8377 DPY should be the display that was passed to x_catch_errors. */
8379 void
8380 x_uncatch_errors (void)
8382 struct x_error_message_stack *tmp;
8384 block_input ();
8386 /* The display may have been closed before this function is called.
8387 Check if it is still open before calling XSync. */
8388 if (x_display_info_for_display (x_error_message->dpy) != 0)
8389 XSync (x_error_message->dpy, False);
8391 tmp = x_error_message;
8392 x_error_message = x_error_message->prev;
8393 xfree (tmp);
8394 unblock_input ();
8397 /* If any X protocol errors have arrived since the last call to
8398 x_catch_errors or x_check_errors, signal an Emacs error using
8399 sprintf (a buffer, FORMAT, the x error message text) as the text. */
8401 void
8402 x_check_errors (Display *dpy, const char *format)
8404 /* Make sure to catch any errors incurred so far. */
8405 XSync (dpy, False);
8407 if (x_error_message->string[0])
8409 char string[X_ERROR_MESSAGE_SIZE];
8410 memcpy (string, x_error_message->string, X_ERROR_MESSAGE_SIZE);
8411 x_uncatch_errors ();
8412 error (format, string);
8416 /* Nonzero if we had any X protocol errors
8417 since we did x_catch_errors on DPY. */
8419 bool
8420 x_had_errors_p (Display *dpy)
8422 /* Make sure to catch any errors incurred so far. */
8423 XSync (dpy, False);
8425 return x_error_message->string[0] != 0;
8428 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
8430 void
8431 x_clear_errors (Display *dpy)
8433 x_error_message->string[0] = 0;
8436 #if false
8437 /* See comment in unwind_to_catch why calling this is a bad
8438 * idea. --lorentey */
8439 /* Close off all unclosed x_catch_errors calls. */
8441 void
8442 x_fully_uncatch_errors (void)
8444 while (x_error_message)
8445 x_uncatch_errors ();
8447 #endif
8449 #if false
8450 static unsigned int x_wire_count;
8451 x_trace_wire (void)
8453 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
8455 #endif
8458 /************************************************************************
8459 Handling X errors
8460 ************************************************************************/
8462 /* Error message passed to x_connection_closed. */
8464 static char *error_msg;
8466 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
8467 the text of an error message that lead to the connection loss. */
8469 static void
8470 x_connection_closed (Display *dpy, const char *error_message)
8472 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
8473 Lisp_Object frame, tail;
8474 ptrdiff_t idx = SPECPDL_INDEX ();
8476 error_msg = alloca (strlen (error_message) + 1);
8477 strcpy (error_msg, error_message);
8479 /* Inhibit redisplay while frames are being deleted. */
8480 specbind (Qinhibit_redisplay, Qt);
8482 if (dpyinfo)
8484 /* Protect display from being closed when we delete the last
8485 frame on it. */
8486 dpyinfo->reference_count++;
8487 dpyinfo->terminal->reference_count++;
8490 /* First delete frames whose mini-buffers are on frames
8491 that are on the dead display. */
8492 FOR_EACH_FRAME (tail, frame)
8494 Lisp_Object minibuf_frame;
8495 minibuf_frame
8496 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
8497 if (FRAME_X_P (XFRAME (frame))
8498 && FRAME_X_P (XFRAME (minibuf_frame))
8499 && ! EQ (frame, minibuf_frame)
8500 && FRAME_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
8501 delete_frame (frame, Qnoelisp);
8504 /* Now delete all remaining frames on the dead display.
8505 We are now sure none of these is used as the mini-buffer
8506 for another frame that we need to delete. */
8507 FOR_EACH_FRAME (tail, frame)
8508 if (FRAME_X_P (XFRAME (frame))
8509 && FRAME_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
8511 /* Set this to t so that delete_frame won't get confused
8512 trying to find a replacement. */
8513 kset_default_minibuffer_frame (FRAME_KBOARD (XFRAME (frame)), Qt);
8514 delete_frame (frame, Qnoelisp);
8517 /* If DPYINFO is null, this means we didn't open the display in the
8518 first place, so don't try to close it. */
8519 if (dpyinfo)
8521 /* We can not call XtCloseDisplay here because it calls XSync.
8522 XSync inside the error handler apparently hangs Emacs. On
8523 current Xt versions, this isn't needed either. */
8524 #ifdef USE_GTK
8525 /* A long-standing GTK bug prevents proper disconnect handling
8526 (https://bugzilla.gnome.org/show_bug.cgi?id=85715). Once,
8527 the resulting Glib error message loop filled a user's disk.
8528 To avoid this, kill Emacs unconditionally on disconnect. */
8529 shut_down_emacs (0, Qnil);
8530 fprintf (stderr, "%s\n\
8531 When compiled with GTK, Emacs cannot recover from X disconnects.\n\
8532 This is a GTK bug: https://bugzilla.gnome.org/show_bug.cgi?id=85715\n\
8533 For details, see etc/PROBLEMS.\n",
8534 error_msg);
8535 emacs_abort ();
8536 #endif /* USE_GTK */
8538 /* Indicate that this display is dead. */
8539 dpyinfo->display = 0;
8541 dpyinfo->reference_count--;
8542 dpyinfo->terminal->reference_count--;
8543 if (dpyinfo->reference_count != 0)
8544 /* We have just closed all frames on this display. */
8545 emacs_abort ();
8548 Lisp_Object tmp;
8549 XSETTERMINAL (tmp, dpyinfo->terminal);
8550 Fdelete_terminal (tmp, Qnoelisp);
8554 if (terminal_list == 0)
8556 fprintf (stderr, "%s\n", error_msg);
8557 Fkill_emacs (make_number (70));
8558 /* NOTREACHED */
8561 totally_unblock_input ();
8563 unbind_to (idx, Qnil);
8564 clear_waiting_for_input ();
8566 /* Tell GCC not to suggest attribute 'noreturn' for this function. */
8567 IF_LINT (if (! terminal_list) return; )
8569 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
8570 longjmp), because returning from this function would get us back into
8571 Xlib's code which will directly call `exit'. */
8572 error ("%s", error_msg);
8575 /* We specifically use it before defining it, so that gcc doesn't inline it,
8576 otherwise gdb doesn't know how to properly put a breakpoint on it. */
8577 static void x_error_quitter (Display *, XErrorEvent *);
8579 /* This is the first-level handler for X protocol errors.
8580 It calls x_error_quitter or x_error_catcher. */
8582 static int
8583 x_error_handler (Display *display, XErrorEvent *event)
8585 #if defined USE_GTK && defined HAVE_GTK3
8586 if ((event->error_code == BadMatch || event->error_code == BadWindow)
8587 && event->request_code == X_SetInputFocus)
8589 return 0;
8591 #endif
8593 if (x_error_message)
8594 x_error_catcher (display, event);
8595 else
8596 x_error_quitter (display, event);
8597 return 0;
8600 /* This is the usual handler for X protocol errors.
8601 It kills all frames on the display that we got the error for.
8602 If that was the only one, it prints an error message and kills Emacs. */
8604 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
8606 /* On older GCC versions, just putting x_error_quitter
8607 after x_error_handler prevents inlining into the former. */
8609 static void NO_INLINE
8610 x_error_quitter (Display *display, XErrorEvent *event)
8612 char buf[256], buf1[356];
8614 /* Ignore BadName errors. They can happen because of fonts
8615 or colors that are not defined. */
8617 if (event->error_code == BadName)
8618 return;
8620 /* Note that there is no real way portable across R3/R4 to get the
8621 original error handler. */
8623 XGetErrorText (display, event->error_code, buf, sizeof (buf));
8624 sprintf (buf1, "X protocol error: %s on protocol request %d",
8625 buf, event->request_code);
8626 x_connection_closed (display, buf1);
8630 /* This is the handler for X IO errors, always.
8631 It kills all frames on the display that we lost touch with.
8632 If that was the only one, it prints an error message and kills Emacs. */
8634 static int
8635 x_io_error_quitter (Display *display)
8637 char buf[256];
8639 snprintf (buf, sizeof buf, "Connection lost to X server `%s'",
8640 DisplayString (display));
8641 x_connection_closed (display, buf);
8642 return 0;
8645 /* Changing the font of the frame. */
8647 /* Give frame F the font FONT-OBJECT as its default font. The return
8648 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
8649 frame. If it is negative, generate a new fontset from
8650 FONT-OBJECT. */
8652 Lisp_Object
8653 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
8655 struct font *font = XFONT_OBJECT (font_object);
8656 int unit;
8658 if (fontset < 0)
8659 fontset = fontset_from_font (font_object);
8660 FRAME_FONTSET (f) = fontset;
8661 if (FRAME_FONT (f) == font)
8662 /* This font is already set in frame F. There's nothing more to
8663 do. */
8664 return font_object;
8666 FRAME_FONT (f) = font;
8667 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
8668 FRAME_COLUMN_WIDTH (f) = font->average_width;
8669 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (font);
8671 #ifndef USE_X_TOOLKIT
8672 FRAME_MENU_BAR_HEIGHT (f) = FRAME_MENU_BAR_LINES (f) * FRAME_LINE_HEIGHT (f);
8673 #endif
8675 /* Compute character columns occupied by scrollbar.
8677 Don't do things differently for non-toolkit scrollbars
8678 (Bug#17163). */
8679 unit = FRAME_COLUMN_WIDTH (f);
8680 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8681 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8682 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + unit - 1) / unit;
8683 else
8684 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
8686 if (FRAME_X_WINDOW (f) != 0)
8688 /* Don't change the size of a tip frame; there's no point in
8689 doing it because it's done in Fx_show_tip, and it leads to
8690 problems because the tip frame has no widget. */
8691 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8692 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
8693 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 3,
8694 false, Qfont);
8697 #ifdef HAVE_X_I18N
8698 if (FRAME_XIC (f)
8699 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8701 block_input ();
8702 xic_set_xfontset (f, SSDATA (fontset_ascii (fontset)));
8703 unblock_input ();
8705 #endif
8707 return font_object;
8711 /***********************************************************************
8712 X Input Methods
8713 ***********************************************************************/
8715 #ifdef HAVE_X_I18N
8717 #ifdef HAVE_X11R6
8719 /* XIM destroy callback function, which is called whenever the
8720 connection to input method XIM dies. CLIENT_DATA contains a
8721 pointer to the x_display_info structure corresponding to XIM. */
8723 static void
8724 xim_destroy_callback (XIM xim, XPointer client_data, XPointer call_data)
8726 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8727 Lisp_Object frame, tail;
8729 block_input ();
8731 /* No need to call XDestroyIC.. */
8732 FOR_EACH_FRAME (tail, frame)
8734 struct frame *f = XFRAME (frame);
8735 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
8737 FRAME_XIC (f) = NULL;
8738 xic_free_xfontset (f);
8742 /* No need to call XCloseIM. */
8743 dpyinfo->xim = NULL;
8744 XFree (dpyinfo->xim_styles);
8745 unblock_input ();
8748 #endif /* HAVE_X11R6 */
8750 /* Open the connection to the XIM server on display DPYINFO.
8751 RESOURCE_NAME is the resource name Emacs uses. */
8753 static void
8754 xim_open_dpy (struct x_display_info *dpyinfo, char *resource_name)
8756 XIM xim;
8758 #ifdef HAVE_XIM
8759 if (use_xim)
8761 if (dpyinfo->xim)
8762 XCloseIM (dpyinfo->xim);
8763 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8764 emacs_class);
8765 dpyinfo->xim = xim;
8767 if (xim)
8769 #ifdef HAVE_X11R6
8770 XIMCallback destroy;
8771 #endif
8773 /* Get supported styles and XIM values. */
8774 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8776 #ifdef HAVE_X11R6
8777 destroy.callback = xim_destroy_callback;
8778 destroy.client_data = (XPointer)dpyinfo;
8779 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8780 #endif
8784 else
8785 #endif /* HAVE_XIM */
8786 dpyinfo->xim = NULL;
8790 #ifdef HAVE_X11R6_XIM
8792 /* XIM instantiate callback function, which is called whenever an XIM
8793 server is available. DISPLAY is the display of the XIM.
8794 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8795 when the callback was registered. */
8797 static void
8798 xim_instantiate_callback (Display *display, XPointer client_data, XPointer call_data)
8800 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8801 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8803 /* We don't support multiple XIM connections. */
8804 if (dpyinfo->xim)
8805 return;
8807 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8809 /* Create XIC for the existing frames on the same display, as long
8810 as they have no XIC. */
8811 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8813 Lisp_Object tail, frame;
8815 block_input ();
8816 FOR_EACH_FRAME (tail, frame)
8818 struct frame *f = XFRAME (frame);
8820 if (FRAME_X_P (f)
8821 && FRAME_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8822 if (FRAME_XIC (f) == NULL)
8824 create_frame_xic (f);
8825 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8826 xic_set_statusarea (f);
8827 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8829 struct window *w = XWINDOW (f->selected_window);
8830 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8835 unblock_input ();
8839 #endif /* HAVE_X11R6_XIM */
8842 /* Open a connection to the XIM server on display DPYINFO.
8843 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8844 connection only at the first time. On X11R6, open the connection
8845 in the XIM instantiate callback function. */
8847 static void
8848 xim_initialize (struct x_display_info *dpyinfo, char *resource_name)
8850 dpyinfo->xim = NULL;
8851 #ifdef HAVE_XIM
8852 if (use_xim)
8854 #ifdef HAVE_X11R6_XIM
8855 struct xim_inst_t *xim_inst = xmalloc (sizeof *xim_inst);
8856 Bool ret;
8858 dpyinfo->xim_callback_data = xim_inst;
8859 xim_inst->dpyinfo = dpyinfo;
8860 xim_inst->resource_name = xstrdup (resource_name);
8861 ret = XRegisterIMInstantiateCallback
8862 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
8863 emacs_class, xim_instantiate_callback,
8864 /* This is XPointer in XFree86 but (XPointer *)
8865 on Tru64, at least, hence the configure test. */
8866 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8867 eassert (ret == True);
8868 #else /* not HAVE_X11R6_XIM */
8869 xim_open_dpy (dpyinfo, resource_name);
8870 #endif /* not HAVE_X11R6_XIM */
8872 #endif /* HAVE_XIM */
8876 /* Close the connection to the XIM server on display DPYINFO. */
8878 static void
8879 xim_close_dpy (struct x_display_info *dpyinfo)
8881 #ifdef HAVE_XIM
8882 if (use_xim)
8884 #ifdef HAVE_X11R6_XIM
8885 struct xim_inst_t *xim_inst = dpyinfo->xim_callback_data;
8887 if (dpyinfo->display)
8889 Bool ret = XUnregisterIMInstantiateCallback
8890 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
8891 emacs_class, xim_instantiate_callback,
8892 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8893 eassert (ret == True);
8895 xfree (xim_inst->resource_name);
8896 xfree (xim_inst);
8897 #endif /* HAVE_X11R6_XIM */
8898 if (dpyinfo->display)
8899 XCloseIM (dpyinfo->xim);
8900 dpyinfo->xim = NULL;
8901 XFree (dpyinfo->xim_styles);
8903 #endif /* HAVE_XIM */
8906 #endif /* not HAVE_X11R6_XIM */
8910 /* Calculate the absolute position in frame F
8911 from its current recorded position values and gravity. */
8913 static void
8914 x_calc_absolute_position (struct frame *f)
8916 int flags = f->size_hint_flags;
8918 /* We have nothing to do if the current position
8919 is already for the top-left corner. */
8920 if (! ((flags & XNegative) || (flags & YNegative)))
8921 return;
8923 /* Treat negative positions as relative to the leftmost bottommost
8924 position that fits on the screen. */
8925 if (flags & XNegative)
8926 f->left_pos = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
8927 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
8930 int height = FRAME_PIXEL_HEIGHT (f);
8932 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8933 /* Something is fishy here. When using Motif, starting Emacs with
8934 `-g -0-0', the frame appears too low by a few pixels.
8936 This seems to be so because initially, while Emacs is starting,
8937 the column widget's height and the frame's pixel height are
8938 different. The column widget's height is the right one. In
8939 later invocations, when Emacs is up, the frame's pixel height
8940 is right, though.
8942 It's not obvious where the initial small difference comes from.
8943 2000-12-01, gerd. */
8945 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8946 #endif
8948 if (flags & YNegative)
8949 f->top_pos = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
8950 - height + f->top_pos;
8953 /* The left_pos and top_pos
8954 are now relative to the top and left screen edges,
8955 so the flags should correspond. */
8956 f->size_hint_flags &= ~ (XNegative | YNegative);
8959 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8960 to really change the position, and 0 when calling from
8961 x_make_frame_visible (in that case, XOFF and YOFF are the current
8962 position values). It is -1 when calling from x_set_frame_parameters,
8963 which means, do adjust for borders but don't change the gravity. */
8965 void
8966 x_set_offset (struct frame *f, register int xoff, register int yoff, int change_gravity)
8968 int modified_top, modified_left;
8970 if (change_gravity > 0)
8972 f->top_pos = yoff;
8973 f->left_pos = xoff;
8974 f->size_hint_flags &= ~ (XNegative | YNegative);
8975 if (xoff < 0)
8976 f->size_hint_flags |= XNegative;
8977 if (yoff < 0)
8978 f->size_hint_flags |= YNegative;
8979 f->win_gravity = NorthWestGravity;
8981 x_calc_absolute_position (f);
8983 block_input ();
8984 x_wm_set_size_hint (f, 0, false);
8986 modified_left = f->left_pos;
8987 modified_top = f->top_pos;
8989 if (change_gravity != 0 && FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8991 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8992 than the WM decorations. So we use the calculated offset instead
8993 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8994 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8995 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8998 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8999 modified_left, modified_top);
9001 x_sync_with_move (f, f->left_pos, f->top_pos,
9002 FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN);
9004 /* change_gravity is non-zero when this function is called from Lisp to
9005 programmatically move a frame. In that case, we call
9006 x_check_expected_move to discover if we have a "Type A" or "Type B"
9007 window manager, and, for a "Type A" window manager, adjust the position
9008 of the frame.
9010 We call x_check_expected_move if a programmatic move occurred, and
9011 either the window manager type (A/B) is unknown or it is Type A but we
9012 need to compute the top/left offset adjustment for this frame. */
9014 if (change_gravity != 0
9015 && (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
9016 || (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
9017 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
9018 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
9019 x_check_expected_move (f, modified_left, modified_top);
9021 unblock_input ();
9024 /* Return true if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
9025 on the root window for frame F contains ATOMNAME.
9026 This is how a WM check shall be done according to the Window Manager
9027 Specification/Extended Window Manager Hints at
9028 http://freedesktop.org/wiki/Specifications/wm-spec. */
9030 static bool
9031 wm_supports (struct frame *f, Atom want_atom)
9033 Atom actual_type;
9034 unsigned long actual_size, bytes_remaining;
9035 int i, rc, actual_format;
9036 bool ret;
9037 Window wmcheck_window;
9038 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9039 Window target_window = dpyinfo->root_window;
9040 int max_len = 65536;
9041 Display *dpy = FRAME_X_DISPLAY (f);
9042 unsigned char *tmp_data = NULL;
9043 Atom target_type = XA_WINDOW;
9045 block_input ();
9047 x_catch_errors (dpy);
9048 rc = XGetWindowProperty (dpy, target_window,
9049 dpyinfo->Xatom_net_supporting_wm_check,
9050 0, max_len, False, target_type,
9051 &actual_type, &actual_format, &actual_size,
9052 &bytes_remaining, &tmp_data);
9054 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
9056 if (tmp_data) XFree (tmp_data);
9057 x_uncatch_errors ();
9058 unblock_input ();
9059 return false;
9062 wmcheck_window = *(Window *) tmp_data;
9063 XFree (tmp_data);
9065 /* Check if window exists. */
9066 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
9067 x_sync (f);
9068 if (x_had_errors_p (dpy))
9070 x_uncatch_errors ();
9071 unblock_input ();
9072 return false;
9075 if (dpyinfo->net_supported_window != wmcheck_window)
9077 /* Window changed, reload atoms */
9078 if (dpyinfo->net_supported_atoms != NULL)
9079 XFree (dpyinfo->net_supported_atoms);
9080 dpyinfo->net_supported_atoms = NULL;
9081 dpyinfo->nr_net_supported_atoms = 0;
9082 dpyinfo->net_supported_window = 0;
9084 target_type = XA_ATOM;
9085 tmp_data = NULL;
9086 rc = XGetWindowProperty (dpy, target_window,
9087 dpyinfo->Xatom_net_supported,
9088 0, max_len, False, target_type,
9089 &actual_type, &actual_format, &actual_size,
9090 &bytes_remaining, &tmp_data);
9092 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
9094 if (tmp_data) XFree (tmp_data);
9095 x_uncatch_errors ();
9096 unblock_input ();
9097 return false;
9100 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
9101 dpyinfo->nr_net_supported_atoms = actual_size;
9102 dpyinfo->net_supported_window = wmcheck_window;
9105 ret = false;
9107 for (i = 0; !ret && i < dpyinfo->nr_net_supported_atoms; ++i)
9108 ret = dpyinfo->net_supported_atoms[i] == want_atom;
9110 x_uncatch_errors ();
9111 unblock_input ();
9113 return ret;
9116 static void
9117 set_wm_state (Lisp_Object frame, bool add, Atom atom, Atom value)
9119 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (XFRAME (frame));
9121 x_send_client_event (frame, make_number (0), frame,
9122 dpyinfo->Xatom_net_wm_state,
9123 make_number (32),
9124 /* 1 = add, 0 = remove */
9125 Fcons
9126 (make_number (add),
9127 Fcons
9128 (make_fixnum_or_float (atom),
9129 (value != 0
9130 ? list1 (make_fixnum_or_float (value))
9131 : Qnil))));
9134 void
9135 x_set_sticky (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
9137 Lisp_Object frame;
9138 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9140 XSETFRAME (frame, f);
9142 set_wm_state (frame, !NILP (new_value),
9143 dpyinfo->Xatom_net_wm_state_sticky, None);
9146 /* Return the current _NET_WM_STATE.
9147 SIZE_STATE is set to one of the FULLSCREEN_* values.
9148 Set *STICKY to the sticky state.
9150 Return true iff we are not hidden. */
9152 static bool
9153 get_current_wm_state (struct frame *f,
9154 Window window,
9155 int *size_state,
9156 bool *sticky)
9158 Atom actual_type;
9159 unsigned long actual_size, bytes_remaining;
9160 int i, rc, actual_format;
9161 bool is_hidden = false;
9162 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9163 long max_len = 65536;
9164 Display *dpy = FRAME_X_DISPLAY (f);
9165 unsigned char *tmp_data = NULL;
9166 Atom target_type = XA_ATOM;
9168 *sticky = false;
9169 *size_state = FULLSCREEN_NONE;
9171 block_input ();
9172 x_catch_errors (dpy);
9173 rc = XGetWindowProperty (dpy, window, dpyinfo->Xatom_net_wm_state,
9174 0, max_len, False, target_type,
9175 &actual_type, &actual_format, &actual_size,
9176 &bytes_remaining, &tmp_data);
9178 if (rc != Success || actual_type != target_type || x_had_errors_p (dpy))
9180 if (tmp_data) XFree (tmp_data);
9181 x_uncatch_errors ();
9182 unblock_input ();
9183 return !FRAME_ICONIFIED_P (f);
9186 x_uncatch_errors ();
9188 for (i = 0; i < actual_size; ++i)
9190 Atom a = ((Atom*)tmp_data)[i];
9191 if (a == dpyinfo->Xatom_net_wm_state_hidden)
9192 is_hidden = true;
9193 else if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
9195 if (*size_state == FULLSCREEN_HEIGHT)
9196 *size_state = FULLSCREEN_MAXIMIZED;
9197 else
9198 *size_state = FULLSCREEN_WIDTH;
9200 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
9202 if (*size_state == FULLSCREEN_WIDTH)
9203 *size_state = FULLSCREEN_MAXIMIZED;
9204 else
9205 *size_state = FULLSCREEN_HEIGHT;
9207 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen)
9208 *size_state = FULLSCREEN_BOTH;
9209 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
9210 *sticky = true;
9213 if (tmp_data) XFree (tmp_data);
9214 unblock_input ();
9215 return ! is_hidden;
9218 /* Do fullscreen as specified in extended window manager hints */
9220 static bool
9221 do_ewmh_fullscreen (struct frame *f)
9223 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9224 bool have_net_atom = wm_supports (f, dpyinfo->Xatom_net_wm_state);
9225 int cur;
9226 bool dummy;
9228 get_current_wm_state (f, FRAME_OUTER_WINDOW (f), &cur, &dummy);
9230 /* Some window managers don't say they support _NET_WM_STATE, but they do say
9231 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
9232 if (!have_net_atom)
9233 have_net_atom = wm_supports (f, dpyinfo->Xatom_net_wm_state_fullscreen);
9235 if (have_net_atom && cur != f->want_fullscreen)
9237 Lisp_Object frame;
9239 XSETFRAME (frame, f);
9241 /* Keep number of calls to set_wm_state as low as possible.
9242 Some window managers, or possible Gtk+, hangs when too many
9243 are sent at once. */
9244 switch (f->want_fullscreen)
9246 case FULLSCREEN_BOTH:
9247 if (cur != FULLSCREEN_BOTH)
9248 set_wm_state (frame, true, dpyinfo->Xatom_net_wm_state_fullscreen,
9249 None);
9250 break;
9251 case FULLSCREEN_WIDTH:
9252 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_HEIGHT
9253 || cur == FULLSCREEN_MAXIMIZED)
9254 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
9255 dpyinfo->Xatom_net_wm_state_maximized_vert);
9256 if (cur != FULLSCREEN_MAXIMIZED)
9257 set_wm_state (frame, true,
9258 dpyinfo->Xatom_net_wm_state_maximized_horz, None);
9259 break;
9260 case FULLSCREEN_HEIGHT:
9261 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_WIDTH
9262 || cur == FULLSCREEN_MAXIMIZED)
9263 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
9264 dpyinfo->Xatom_net_wm_state_maximized_horz);
9265 if (cur != FULLSCREEN_MAXIMIZED)
9266 set_wm_state (frame, true,
9267 dpyinfo->Xatom_net_wm_state_maximized_vert, None);
9268 break;
9269 case FULLSCREEN_MAXIMIZED:
9270 if (cur == FULLSCREEN_BOTH)
9271 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
9272 None);
9273 set_wm_state (frame, true,
9274 dpyinfo->Xatom_net_wm_state_maximized_horz,
9275 dpyinfo->Xatom_net_wm_state_maximized_vert);
9276 break;
9277 case FULLSCREEN_NONE:
9278 if (cur == FULLSCREEN_BOTH)
9279 set_wm_state (frame, false, dpyinfo->Xatom_net_wm_state_fullscreen,
9280 None);
9281 else
9282 set_wm_state (frame, false,
9283 dpyinfo->Xatom_net_wm_state_maximized_horz,
9284 dpyinfo->Xatom_net_wm_state_maximized_vert);
9287 f->want_fullscreen = FULLSCREEN_NONE;
9291 return have_net_atom;
9294 static void
9295 XTfullscreen_hook (struct frame *f)
9297 if (FRAME_VISIBLE_P (f))
9299 block_input ();
9300 x_check_fullscreen (f);
9301 x_sync (f);
9302 unblock_input ();
9307 static bool
9308 x_handle_net_wm_state (struct frame *f, const XPropertyEvent *event)
9310 int value = FULLSCREEN_NONE;
9311 Lisp_Object lval;
9312 bool sticky = false;
9313 bool not_hidden = get_current_wm_state (f, event->window, &value, &sticky);
9315 lval = Qnil;
9316 switch (value)
9318 case FULLSCREEN_WIDTH:
9319 lval = Qfullwidth;
9320 break;
9321 case FULLSCREEN_HEIGHT:
9322 lval = Qfullheight;
9323 break;
9324 case FULLSCREEN_BOTH:
9325 lval = Qfullboth;
9326 break;
9327 case FULLSCREEN_MAXIMIZED:
9328 lval = Qmaximized;
9329 break;
9332 store_frame_param (f, Qfullscreen, lval);
9333 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
9335 return not_hidden;
9338 /* Check if we need to resize the frame due to a fullscreen request.
9339 If so needed, resize the frame. */
9340 static void
9341 x_check_fullscreen (struct frame *f)
9343 if (do_ewmh_fullscreen (f))
9344 return;
9346 if (f->output_data.x->parent_desc != FRAME_DISPLAY_INFO (f)->root_window)
9347 return; /* Only fullscreen without WM or with EWM hints (above). */
9349 /* Setting fullscreen to nil doesn't do anything. We could save the
9350 last non-fullscreen size and restore it, but it seems like a
9351 lot of work for this unusual case (no window manager running). */
9353 if (f->want_fullscreen != FULLSCREEN_NONE)
9355 int width = FRAME_PIXEL_WIDTH (f), height = FRAME_PIXEL_HEIGHT (f);
9356 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9358 switch (f->want_fullscreen)
9360 /* No difference between these two when there is no WM */
9361 case FULLSCREEN_BOTH:
9362 case FULLSCREEN_MAXIMIZED:
9363 width = x_display_pixel_width (dpyinfo);
9364 height = x_display_pixel_height (dpyinfo);
9365 break;
9366 case FULLSCREEN_WIDTH:
9367 width = x_display_pixel_width (dpyinfo);
9368 break;
9369 case FULLSCREEN_HEIGHT:
9370 height = x_display_pixel_height (dpyinfo);
9373 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9374 width, height);
9378 /* This function is called by x_set_offset to determine whether the window
9379 manager interfered with the positioning of the frame. Type A window
9380 managers position the surrounding window manager decorations a small
9381 amount above and left of the user-supplied position. Type B window
9382 managers position the surrounding window manager decorations at the
9383 user-specified position. If we detect a Type A window manager, we
9384 compensate by moving the window right and down by the proper amount. */
9386 static void
9387 x_check_expected_move (struct frame *f, int expected_left, int expected_top)
9389 int current_left = 0, current_top = 0;
9391 /* x_real_positions returns the left and top offsets of the outermost
9392 window manager window around the frame. */
9394 x_real_positions (f, &current_left, &current_top);
9396 if (current_left != expected_left || current_top != expected_top)
9398 /* It's a "Type A" window manager. */
9400 int adjusted_left;
9401 int adjusted_top;
9403 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
9404 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
9405 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
9407 /* Now fix the mispositioned frame's location. */
9409 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
9410 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
9412 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9413 adjusted_left, adjusted_top);
9415 x_sync_with_move (f, expected_left, expected_top, false);
9417 else
9418 /* It's a "Type B" window manager. We don't have to adjust the
9419 frame's position. */
9421 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
9425 /* Wait for XGetGeometry to return up-to-date position information for a
9426 recently-moved frame. Call this immediately after calling XMoveWindow.
9427 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
9428 frame has been moved to, so we use a fuzzy position comparison instead
9429 of an exact comparison. */
9431 static void
9432 x_sync_with_move (struct frame *f, int left, int top, bool fuzzy)
9434 int count = 0;
9436 while (count++ < 50)
9438 int current_left = 0, current_top = 0;
9440 /* In theory, this call to XSync only needs to happen once, but in
9441 practice, it doesn't seem to work, hence the need for the surrounding
9442 loop. */
9444 XSync (FRAME_X_DISPLAY (f), False);
9445 x_real_positions (f, &current_left, &current_top);
9447 if (fuzzy)
9449 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
9450 pixels. */
9452 if (eabs (current_left - left) <= 10
9453 && eabs (current_top - top) <= 40)
9454 return;
9456 else if (current_left == left && current_top == top)
9457 return;
9460 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
9461 will then return up-to-date position info. */
9463 wait_reading_process_output (0, 500000000, 0, false, Qnil, NULL, 0);
9467 /* Wait for an event on frame F matching EVENTTYPE. */
9468 void
9469 x_wait_for_event (struct frame *f, int eventtype)
9471 int level = interrupt_input_blocked;
9473 fd_set fds;
9474 struct timespec tmo, tmo_at, time_now;
9475 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
9477 f->wait_event_type = eventtype;
9479 /* Set timeout to 0.1 second. Hopefully not noticeable.
9480 Maybe it should be configurable. */
9481 tmo = make_timespec (0, 100 * 1000 * 1000);
9482 tmo_at = timespec_add (current_timespec (), tmo);
9484 while (f->wait_event_type)
9486 pending_signals = true;
9487 totally_unblock_input ();
9488 /* XTread_socket is called after unblock. */
9489 block_input ();
9490 interrupt_input_blocked = level;
9492 FD_ZERO (&fds);
9493 FD_SET (fd, &fds);
9495 time_now = current_timespec ();
9496 if (timespec_cmp (tmo_at, time_now) < 0)
9497 break;
9499 tmo = timespec_sub (tmo_at, time_now);
9500 if (pselect (fd + 1, &fds, NULL, NULL, &tmo, NULL) == 0)
9501 break; /* Timeout */
9504 f->wait_event_type = 0;
9508 /* Change the size of frame F's X window to WIDTH/HEIGHT in the case F
9509 doesn't have a widget. If CHANGE_GRAVITY, change to
9510 top-left-corner window gravity for this size change and subsequent
9511 size changes. Otherwise leave the window gravity unchanged. */
9513 static void
9514 x_set_window_size_1 (struct frame *f, bool change_gravity,
9515 int width, int height, bool pixelwise)
9517 int pixelwidth, pixelheight;
9519 pixelwidth = (pixelwise
9520 ? FRAME_TEXT_TO_PIXEL_WIDTH (f, width)
9521 : FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, width));
9522 pixelheight = ((pixelwise
9523 ? FRAME_TEXT_TO_PIXEL_HEIGHT (f, height)
9524 : FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, height)));
9526 if (change_gravity) f->win_gravity = NorthWestGravity;
9527 x_wm_set_size_hint (f, 0, false);
9528 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9529 pixelwidth, pixelheight + FRAME_MENUBAR_HEIGHT (f));
9532 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
9533 receive in the ConfigureNotify event; if we get what we asked
9534 for, then the event won't cause the screen to become garbaged, so
9535 we have to make sure to do it here. */
9536 SET_FRAME_GARBAGED (f);
9538 /* Now, strictly speaking, we can't be sure that this is accurate,
9539 but the window manager will get around to dealing with the size
9540 change request eventually, and we'll hear how it went when the
9541 ConfigureNotify event gets here.
9543 We could just not bother storing any of this information here,
9544 and let the ConfigureNotify event set everything up, but that
9545 might be kind of confusing to the Lisp code, since size changes
9546 wouldn't be reported in the frame parameters until some random
9547 point in the future when the ConfigureNotify event arrives.
9549 Pass true for DELAY since we can't run Lisp code inside of
9550 a BLOCK_INPUT. */
9552 /* But the ConfigureNotify may in fact never arrive, and then this is
9553 not right if the frame is visible. Instead wait (with timeout)
9554 for the ConfigureNotify. */
9555 if (FRAME_VISIBLE_P (f))
9556 x_wait_for_event (f, ConfigureNotify);
9557 else
9559 change_frame_size (f, pixelwidth, pixelheight, false, true, false, true);
9560 x_sync (f);
9565 /* Call this to change the size of frame F's x-window.
9566 If CHANGE_GRAVITY, change to top-left-corner window gravity
9567 for this size change and subsequent size changes.
9568 Otherwise we leave the window gravity unchanged. */
9570 void
9571 x_set_window_size (struct frame *f, bool change_gravity,
9572 int width, int height, bool pixelwise)
9574 block_input ();
9576 /* The following breaks our calculations. If it's really needed,
9577 think of something else. */
9578 #if false
9579 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
9581 int text_width, text_height;
9583 /* When the frame is maximized/fullscreen or running under for
9584 example Xmonad, x_set_window_size_1 will be a no-op.
9585 In that case, the right thing to do is extend rows/width to
9586 the current frame size. We do that first if x_set_window_size_1
9587 turns out to not be a no-op (there is no way to know).
9588 The size will be adjusted again if the frame gets a
9589 ConfigureNotify event as a result of x_set_window_size. */
9590 int pixelh = FRAME_PIXEL_HEIGHT (f);
9591 #ifdef USE_X_TOOLKIT
9592 /* The menu bar is not part of text lines. The tool bar
9593 is however. */
9594 pixelh -= FRAME_MENUBAR_HEIGHT (f);
9595 #endif
9596 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, FRAME_PIXEL_WIDTH (f));
9597 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelh);
9599 change_frame_size (f, text_width, text_height, false, true, false, true);
9601 #endif
9603 #ifdef USE_GTK
9604 if (FRAME_GTK_WIDGET (f))
9605 if (! pixelwise)
9606 xg_frame_set_char_size (f, width * FRAME_COLUMN_WIDTH (f),
9607 height * FRAME_LINE_HEIGHT (f));
9608 else
9609 xg_frame_set_char_size (f, width, height);
9610 else
9611 x_set_window_size_1 (f, change_gravity, width, height, pixelwise);
9612 #else /* not USE_GTK */
9614 x_set_window_size_1 (f, change_gravity, width, height, pixelwise);
9615 x_clear_under_internal_border (f);
9617 #endif /* not USE_GTK */
9619 /* If cursor was outside the new size, mark it as off. */
9620 mark_window_cursors_off (XWINDOW (f->root_window));
9622 /* Clear out any recollection of where the mouse highlighting was,
9623 since it might be in a place that's outside the new frame size.
9624 Actually checking whether it is outside is a pain in the neck,
9625 so don't try--just let the highlighting be done afresh with new size. */
9626 cancel_mouse_face (f);
9628 unblock_input ();
9630 do_pending_window_change (false);
9633 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
9635 void
9636 frame_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
9638 block_input ();
9640 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9641 0, 0, 0, 0, pix_x, pix_y);
9642 unblock_input ();
9645 /* Raise frame F. */
9647 void
9648 x_raise_frame (struct frame *f)
9650 block_input ();
9651 if (FRAME_VISIBLE_P (f))
9652 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9653 XFlush (FRAME_X_DISPLAY (f));
9654 unblock_input ();
9657 /* Lower frame F. */
9659 static void
9660 x_lower_frame (struct frame *f)
9662 if (FRAME_VISIBLE_P (f))
9664 block_input ();
9665 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9666 XFlush (FRAME_X_DISPLAY (f));
9667 unblock_input ();
9671 /* Request focus with XEmbed */
9673 void
9674 xembed_request_focus (struct frame *f)
9676 /* See XEmbed Protocol Specification at
9677 http://freedesktop.org/wiki/Specifications/xembed-spec */
9678 if (FRAME_VISIBLE_P (f))
9679 xembed_send_message (f, CurrentTime,
9680 XEMBED_REQUEST_FOCUS, 0, 0, 0);
9683 /* Activate frame with Extended Window Manager Hints */
9685 void
9686 x_ewmh_activate_frame (struct frame *f)
9688 /* See Window Manager Specification/Extended Window Manager Hints at
9689 http://freedesktop.org/wiki/Specifications/wm-spec */
9691 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9693 if (FRAME_VISIBLE_P (f) && wm_supports (f, dpyinfo->Xatom_net_active_window))
9695 Lisp_Object frame;
9696 XSETFRAME (frame, f);
9697 x_send_client_event (frame, make_number (0), frame,
9698 dpyinfo->Xatom_net_active_window,
9699 make_number (32),
9700 list2i (1, dpyinfo->last_user_time));
9704 static void
9705 XTframe_raise_lower (struct frame *f, bool raise_flag)
9707 if (raise_flag)
9708 x_raise_frame (f);
9709 else
9710 x_lower_frame (f);
9713 /* XEmbed implementation. */
9715 #if defined USE_X_TOOLKIT || ! defined USE_GTK
9717 /* XEmbed implementation. */
9719 #define XEMBED_VERSION 0
9721 static void
9722 xembed_set_info (struct frame *f, enum xembed_info flags)
9724 unsigned long data[2];
9725 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9727 data[0] = XEMBED_VERSION;
9728 data[1] = flags;
9730 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9731 dpyinfo->Xatom_XEMBED_INFO, dpyinfo->Xatom_XEMBED_INFO,
9732 32, PropModeReplace, (unsigned char *) data, 2);
9734 #endif /* defined USE_X_TOOLKIT || ! defined USE_GTK */
9736 static void
9737 xembed_send_message (struct frame *f, Time t, enum xembed_message msg,
9738 long int detail, long int data1, long int data2)
9740 XEvent event;
9742 event.xclient.type = ClientMessage;
9743 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
9744 event.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_XEMBED;
9745 event.xclient.format = 32;
9746 event.xclient.data.l[0] = t;
9747 event.xclient.data.l[1] = msg;
9748 event.xclient.data.l[2] = detail;
9749 event.xclient.data.l[3] = data1;
9750 event.xclient.data.l[4] = data2;
9752 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
9753 False, NoEventMask, &event);
9754 XSync (FRAME_X_DISPLAY (f), False);
9757 /* Change of visibility. */
9759 /* This tries to wait until the frame is really visible.
9760 However, if the window manager asks the user where to position
9761 the frame, this will return before the user finishes doing that.
9762 The frame will not actually be visible at that time,
9763 but it will become visible later when the window manager
9764 finishes with it. */
9766 void
9767 x_make_frame_visible (struct frame *f)
9769 int original_top, original_left;
9770 int tries = 0;
9772 block_input ();
9774 x_set_bitmap_icon (f);
9776 if (! FRAME_VISIBLE_P (f))
9778 /* We test FRAME_GARBAGED_P here to make sure we don't
9779 call x_set_offset a second time
9780 if we get to x_make_frame_visible a second time
9781 before the window gets really visible. */
9782 if (! FRAME_ICONIFIED_P (f)
9783 && ! FRAME_X_EMBEDDED_P (f)
9784 && ! f->output_data.x->asked_for_visible)
9785 x_set_offset (f, f->left_pos, f->top_pos, 0);
9787 f->output_data.x->asked_for_visible = true;
9789 if (! EQ (Vx_no_window_manager, Qt))
9790 x_wm_set_window_state (f, NormalState);
9791 #ifdef USE_X_TOOLKIT
9792 if (FRAME_X_EMBEDDED_P (f))
9793 xembed_set_info (f, XEMBED_MAPPED);
9794 else
9796 /* This was XtPopup, but that did nothing for an iconified frame. */
9797 XtMapWidget (f->output_data.x->widget);
9799 #else /* not USE_X_TOOLKIT */
9800 #ifdef USE_GTK
9801 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9802 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9803 #else
9804 if (FRAME_X_EMBEDDED_P (f))
9805 xembed_set_info (f, XEMBED_MAPPED);
9806 else
9807 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9808 #endif /* not USE_GTK */
9809 #endif /* not USE_X_TOOLKIT */
9812 XFlush (FRAME_X_DISPLAY (f));
9814 /* Synchronize to ensure Emacs knows the frame is visible
9815 before we do anything else. We do this loop with input not blocked
9816 so that incoming events are handled. */
9818 Lisp_Object frame;
9819 /* This must be before UNBLOCK_INPUT
9820 since events that arrive in response to the actions above
9821 will set it when they are handled. */
9822 bool previously_visible = f->output_data.x->has_been_visible;
9824 original_left = f->left_pos;
9825 original_top = f->top_pos;
9827 /* This must come after we set COUNT. */
9828 unblock_input ();
9830 /* We unblock here so that arriving X events are processed. */
9832 /* Now move the window back to where it was "supposed to be".
9833 But don't do it if the gravity is negative.
9834 When the gravity is negative, this uses a position
9835 that is 3 pixels too low. Perhaps that's really the border width.
9837 Don't do this if the window has never been visible before,
9838 because the window manager may choose the position
9839 and we don't want to override it. */
9841 if (! FRAME_VISIBLE_P (f)
9842 && ! FRAME_ICONIFIED_P (f)
9843 && ! FRAME_X_EMBEDDED_P (f)
9844 && f->win_gravity == NorthWestGravity
9845 && previously_visible)
9847 Drawable rootw;
9848 int x, y;
9849 unsigned int width, height, border, depth;
9851 block_input ();
9853 /* On some window managers (such as FVWM) moving an existing
9854 window, even to the same place, causes the window manager
9855 to introduce an offset. This can cause the window to move
9856 to an unexpected location. Check the geometry (a little
9857 slow here) and then verify that the window is in the right
9858 place. If the window is not in the right place, move it
9859 there, and take the potential window manager hit. */
9860 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9861 &rootw, &x, &y, &width, &height, &border, &depth);
9863 if (original_left != x || original_top != y)
9864 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9865 original_left, original_top);
9867 unblock_input ();
9870 XSETFRAME (frame, f);
9872 /* Process X events until a MapNotify event has been seen. */
9873 while (!FRAME_VISIBLE_P (f))
9875 /* Force processing of queued events. */
9876 x_sync (f);
9878 /* If on another desktop, the deiconify/map may be ignored and the
9879 frame never becomes visible. XMonad does this.
9880 Prevent an endless loop. */
9881 if (FRAME_ICONIFIED_P (f) && ++tries > 100)
9882 break;
9884 /* This hack is still in use at least for Cygwin. See
9885 http://lists.gnu.org/archive/html/emacs-devel/2013-12/msg00351.html.
9887 Machines that do polling rather than SIGIO have been
9888 observed to go into a busy-wait here. So we'll fake an
9889 alarm signal to let the handler know that there's something
9890 to be read. We used to raise a real alarm, but it seems
9891 that the handler isn't always enabled here. This is
9892 probably a bug. */
9893 if (input_polling_used ())
9895 /* It could be confusing if a real alarm arrives while
9896 processing the fake one. Turn it off and let the
9897 handler reset it. */
9898 int old_poll_suppress_count = poll_suppress_count;
9899 poll_suppress_count = 1;
9900 poll_for_input_1 ();
9901 poll_suppress_count = old_poll_suppress_count;
9904 if (XPending (FRAME_X_DISPLAY (f)))
9906 XEvent xev;
9907 XNextEvent (FRAME_X_DISPLAY (f), &xev);
9908 x_dispatch_event (&xev, FRAME_X_DISPLAY (f));
9914 /* Change from mapped state to withdrawn state. */
9916 /* Make the frame visible (mapped and not iconified). */
9918 void
9919 x_make_frame_invisible (struct frame *f)
9921 Window window;
9923 /* Use the frame's outermost window, not the one we normally draw on. */
9924 window = FRAME_OUTER_WINDOW (f);
9926 /* Don't keep the highlight on an invisible frame. */
9927 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
9928 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
9930 block_input ();
9932 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9933 that the current position of the window is user-specified, rather than
9934 program-specified, so that when the window is mapped again, it will be
9935 placed at the same location, without forcing the user to position it
9936 by hand again (they have already done that once for this window.) */
9937 x_wm_set_size_hint (f, 0, true);
9939 #ifdef USE_GTK
9940 if (FRAME_GTK_OUTER_WIDGET (f))
9941 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9942 else
9943 #else
9944 if (FRAME_X_EMBEDDED_P (f))
9945 xembed_set_info (f, 0);
9946 else
9947 #endif
9950 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9951 DefaultScreen (FRAME_X_DISPLAY (f))))
9953 unblock_input ();
9954 error ("Can't notify window manager of window withdrawal");
9958 /* We can't distinguish this from iconification
9959 just by the event that we get from the server.
9960 So we can't win using the usual strategy of letting
9961 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9962 and synchronize with the server to make sure we agree. */
9963 SET_FRAME_VISIBLE (f, 0);
9964 SET_FRAME_ICONIFIED (f, false);
9966 x_sync (f);
9968 unblock_input ();
9971 /* Change window state from mapped to iconified. */
9973 void
9974 x_iconify_frame (struct frame *f)
9976 #ifdef USE_X_TOOLKIT
9977 int result;
9978 #endif
9980 /* Don't keep the highlight on an invisible frame. */
9981 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
9982 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
9984 if (FRAME_ICONIFIED_P (f))
9985 return;
9987 block_input ();
9989 x_set_bitmap_icon (f);
9991 #if defined (USE_GTK)
9992 if (FRAME_GTK_OUTER_WIDGET (f))
9994 if (! FRAME_VISIBLE_P (f))
9995 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9997 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9998 SET_FRAME_VISIBLE (f, 0);
9999 SET_FRAME_ICONIFIED (f, true);
10000 unblock_input ();
10001 return;
10003 #endif
10005 #ifdef USE_X_TOOLKIT
10007 if (! FRAME_VISIBLE_P (f))
10009 if (! EQ (Vx_no_window_manager, Qt))
10010 x_wm_set_window_state (f, IconicState);
10011 /* This was XtPopup, but that did nothing for an iconified frame. */
10012 XtMapWidget (f->output_data.x->widget);
10013 /* The server won't give us any event to indicate
10014 that an invisible frame was changed to an icon,
10015 so we have to record it here. */
10016 SET_FRAME_VISIBLE (f, 0);
10017 SET_FRAME_ICONIFIED (f, true);
10018 unblock_input ();
10019 return;
10022 result = XIconifyWindow (FRAME_X_DISPLAY (f),
10023 XtWindow (f->output_data.x->widget),
10024 DefaultScreen (FRAME_X_DISPLAY (f)));
10025 unblock_input ();
10027 if (!result)
10028 error ("Can't notify window manager of iconification");
10030 SET_FRAME_ICONIFIED (f, true);
10031 SET_FRAME_VISIBLE (f, 0);
10033 block_input ();
10034 XFlush (FRAME_X_DISPLAY (f));
10035 unblock_input ();
10036 #else /* not USE_X_TOOLKIT */
10038 /* Make sure the X server knows where the window should be positioned,
10039 in case the user deiconifies with the window manager. */
10040 if (! FRAME_VISIBLE_P (f)
10041 && ! FRAME_ICONIFIED_P (f)
10042 && ! FRAME_X_EMBEDDED_P (f))
10043 x_set_offset (f, f->left_pos, f->top_pos, 0);
10045 /* Since we don't know which revision of X we're running, we'll use both
10046 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
10048 /* X11R4: send a ClientMessage to the window manager using the
10049 WM_CHANGE_STATE type. */
10051 XEvent msg;
10053 msg.xclient.window = FRAME_X_WINDOW (f);
10054 msg.xclient.type = ClientMessage;
10055 msg.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_wm_change_state;
10056 msg.xclient.format = 32;
10057 msg.xclient.data.l[0] = IconicState;
10059 if (! XSendEvent (FRAME_X_DISPLAY (f),
10060 DefaultRootWindow (FRAME_X_DISPLAY (f)),
10061 False,
10062 SubstructureRedirectMask | SubstructureNotifyMask,
10063 &msg))
10065 unblock_input ();
10066 error ("Can't notify window manager of iconification");
10070 /* X11R3: set the initial_state field of the window manager hints to
10071 IconicState. */
10072 x_wm_set_window_state (f, IconicState);
10074 if (!FRAME_VISIBLE_P (f))
10076 /* If the frame was withdrawn, before, we must map it. */
10077 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10080 SET_FRAME_ICONIFIED (f, true);
10081 SET_FRAME_VISIBLE (f, 0);
10083 XFlush (FRAME_X_DISPLAY (f));
10084 unblock_input ();
10085 #endif /* not USE_X_TOOLKIT */
10089 /* Free X resources of frame F. */
10091 void
10092 x_free_frame_resources (struct frame *f)
10094 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10095 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
10096 #ifdef USE_X_TOOLKIT
10097 Lisp_Object bar;
10098 struct scroll_bar *b;
10099 #endif
10101 block_input ();
10103 /* If a display connection is dead, don't try sending more
10104 commands to the X server. */
10105 if (dpyinfo->display)
10107 /* Always exit with visible pointer to avoid weird issue
10108 with Xfixes (Bug#17609). */
10109 if (f->pointer_invisible)
10110 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, 0);
10112 /* We must free faces before destroying windows because some
10113 font-driver (e.g. xft) access a window while finishing a
10114 face. */
10115 free_frame_faces (f);
10117 if (f->output_data.x->icon_desc)
10118 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
10120 #ifdef USE_X_TOOLKIT
10121 /* Explicitly destroy the scroll bars of the frame. Without
10122 this, we get "BadDrawable" errors from the toolkit later on,
10123 presumably from expose events generated for the disappearing
10124 toolkit scroll bars. */
10125 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
10127 b = XSCROLL_BAR (bar);
10128 x_scroll_bar_remove (b);
10130 #endif
10132 #ifdef HAVE_X_I18N
10133 if (FRAME_XIC (f))
10134 free_frame_xic (f);
10135 #endif
10137 #ifdef USE_X_TOOLKIT
10138 if (f->output_data.x->widget)
10140 XtDestroyWidget (f->output_data.x->widget);
10141 f->output_data.x->widget = NULL;
10143 /* Tooltips don't have widgets, only a simple X window, even if
10144 we are using a toolkit. */
10145 else if (FRAME_X_WINDOW (f))
10146 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10148 free_frame_menubar (f);
10150 if (f->shell_position)
10151 xfree (f->shell_position);
10152 #else /* !USE_X_TOOLKIT */
10154 #ifdef USE_GTK
10155 xg_free_frame_widgets (f);
10156 #endif /* USE_GTK */
10158 if (FRAME_X_WINDOW (f))
10159 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10160 #endif /* !USE_X_TOOLKIT */
10162 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
10163 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
10164 unload_color (f, f->output_data.x->cursor_pixel);
10165 unload_color (f, f->output_data.x->cursor_foreground_pixel);
10166 unload_color (f, f->output_data.x->border_pixel);
10167 unload_color (f, f->output_data.x->mouse_pixel);
10169 if (f->output_data.x->scroll_bar_background_pixel != -1)
10170 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
10171 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
10172 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
10173 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
10174 /* Scrollbar shadow colors. */
10175 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
10176 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
10177 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
10178 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
10179 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
10180 if (f->output_data.x->white_relief.pixel != -1)
10181 unload_color (f, f->output_data.x->white_relief.pixel);
10182 if (f->output_data.x->black_relief.pixel != -1)
10183 unload_color (f, f->output_data.x->black_relief.pixel);
10185 x_free_gcs (f);
10187 /* Free extra GCs allocated by x_setup_relief_colors. */
10188 if (f->output_data.x->white_relief.gc)
10190 XFreeGC (dpyinfo->display, f->output_data.x->white_relief.gc);
10191 f->output_data.x->white_relief.gc = 0;
10193 if (f->output_data.x->black_relief.gc)
10195 XFreeGC (dpyinfo->display, f->output_data.x->black_relief.gc);
10196 f->output_data.x->black_relief.gc = 0;
10199 /* Free cursors. */
10200 if (f->output_data.x->text_cursor != 0)
10201 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->text_cursor);
10202 if (f->output_data.x->nontext_cursor != 0)
10203 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->nontext_cursor);
10204 if (f->output_data.x->modeline_cursor != 0)
10205 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->modeline_cursor);
10206 if (f->output_data.x->hand_cursor != 0)
10207 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hand_cursor);
10208 if (f->output_data.x->hourglass_cursor != 0)
10209 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hourglass_cursor);
10210 if (f->output_data.x->horizontal_drag_cursor != 0)
10211 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->horizontal_drag_cursor);
10212 if (f->output_data.x->vertical_drag_cursor != 0)
10213 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->vertical_drag_cursor);
10215 XFlush (FRAME_X_DISPLAY (f));
10218 xfree (f->output_data.x->saved_menu_event);
10219 xfree (f->output_data.x);
10220 f->output_data.x = NULL;
10222 if (f == dpyinfo->x_focus_frame)
10223 dpyinfo->x_focus_frame = 0;
10224 if (f == dpyinfo->x_focus_event_frame)
10225 dpyinfo->x_focus_event_frame = 0;
10226 if (f == dpyinfo->x_highlight_frame)
10227 dpyinfo->x_highlight_frame = 0;
10228 if (f == hlinfo->mouse_face_mouse_frame)
10229 reset_mouse_highlight (hlinfo);
10231 unblock_input ();
10235 /* Destroy the X window of frame F. */
10237 static void
10238 x_destroy_window (struct frame *f)
10240 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10242 /* If a display connection is dead, don't try sending more
10243 commands to the X server. */
10244 if (dpyinfo->display != 0)
10245 x_free_frame_resources (f);
10247 dpyinfo->reference_count--;
10251 /* Setting window manager hints. */
10253 /* Set the normal size hints for the window manager, for frame F.
10254 FLAGS is the flags word to use--or 0 meaning preserve the flags
10255 that the window now has.
10256 If USER_POSITION, set the USPosition
10257 flag (this is useful when FLAGS is 0).
10258 The GTK version is in gtkutils.c. */
10260 #ifndef USE_GTK
10261 void
10262 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
10264 XSizeHints size_hints;
10265 Window window = FRAME_OUTER_WINDOW (f);
10267 if (!window)
10268 return;
10270 #ifdef USE_X_TOOLKIT
10271 if (f->output_data.x->widget)
10273 widget_update_wm_size_hints (f->output_data.x->widget);
10274 return;
10276 #endif
10278 /* Setting PMaxSize caused various problems. */
10279 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
10281 size_hints.x = f->left_pos;
10282 size_hints.y = f->top_pos;
10284 size_hints.height = FRAME_PIXEL_HEIGHT (f);
10285 size_hints.width = FRAME_PIXEL_WIDTH (f);
10287 size_hints.width_inc = frame_resize_pixelwise ? 1 : FRAME_COLUMN_WIDTH (f);
10288 size_hints.height_inc = frame_resize_pixelwise ? 1 : FRAME_LINE_HEIGHT (f);
10290 size_hints.max_width = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
10291 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
10292 size_hints.max_height = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
10293 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
10295 /* Calculate the base and minimum sizes. */
10297 int base_width, base_height;
10298 int min_rows = 0, min_cols = 0;
10300 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
10301 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
10303 if (frame_resize_pixelwise)
10304 /* Needed to prevent a bad protocol error crash when making the
10305 frame size very small. */
10307 min_cols = 2 * min_cols;
10308 min_rows = 2 * min_rows;
10311 /* The window manager uses the base width hints to calculate the
10312 current number of rows and columns in the frame while
10313 resizing; min_width and min_height aren't useful for this
10314 purpose, since they might not give the dimensions for a
10315 zero-row, zero-column frame.
10317 We use the base_width and base_height members if we have
10318 them; otherwise, we set the min_width and min_height members
10319 to the size for a zero x zero frame. */
10321 size_hints.flags |= PBaseSize;
10322 size_hints.base_width = base_width;
10323 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
10324 size_hints.min_width = base_width + min_cols * FRAME_COLUMN_WIDTH (f);
10325 size_hints.min_height = base_height + min_rows * FRAME_LINE_HEIGHT (f);
10328 /* If we don't need the old flags, we don't need the old hint at all. */
10329 if (flags)
10331 size_hints.flags |= flags;
10332 goto no_read;
10336 XSizeHints hints; /* Sometimes I hate X Windows... */
10337 long supplied_return;
10338 int value;
10340 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
10341 &supplied_return);
10343 if (flags)
10344 size_hints.flags |= flags;
10345 else
10347 if (value == 0)
10348 hints.flags = 0;
10349 if (hints.flags & PSize)
10350 size_hints.flags |= PSize;
10351 if (hints.flags & PPosition)
10352 size_hints.flags |= PPosition;
10353 if (hints.flags & USPosition)
10354 size_hints.flags |= USPosition;
10355 if (hints.flags & USSize)
10356 size_hints.flags |= USSize;
10360 no_read:
10362 #ifdef PWinGravity
10363 size_hints.win_gravity = f->win_gravity;
10364 size_hints.flags |= PWinGravity;
10366 if (user_position)
10368 size_hints.flags &= ~ PPosition;
10369 size_hints.flags |= USPosition;
10371 #endif /* PWinGravity */
10373 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
10375 #endif /* not USE_GTK */
10377 /* Used for IconicState or NormalState */
10379 static void
10380 x_wm_set_window_state (struct frame *f, int state)
10382 #ifdef USE_X_TOOLKIT
10383 Arg al[1];
10385 XtSetArg (al[0], XtNinitialState, state);
10386 XtSetValues (f->output_data.x->widget, al, 1);
10387 #else /* not USE_X_TOOLKIT */
10388 Window window = FRAME_X_WINDOW (f);
10390 f->output_data.x->wm_hints.flags |= StateHint;
10391 f->output_data.x->wm_hints.initial_state = state;
10393 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
10394 #endif /* not USE_X_TOOLKIT */
10397 static void
10398 x_wm_set_icon_pixmap (struct frame *f, ptrdiff_t pixmap_id)
10400 Pixmap icon_pixmap, icon_mask;
10402 #if !defined USE_X_TOOLKIT && !defined USE_GTK
10403 Window window = FRAME_OUTER_WINDOW (f);
10404 #endif
10406 if (pixmap_id > 0)
10408 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
10409 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
10410 icon_mask = x_bitmap_mask (f, pixmap_id);
10411 f->output_data.x->wm_hints.icon_mask = icon_mask;
10413 else
10415 /* It seems there is no way to turn off use of an icon
10416 pixmap. */
10417 return;
10421 #ifdef USE_GTK
10423 xg_set_frame_icon (f, icon_pixmap, icon_mask);
10424 return;
10427 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
10430 Arg al[1];
10431 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
10432 XtSetValues (f->output_data.x->widget, al, 1);
10433 XtSetArg (al[0], XtNiconMask, icon_mask);
10434 XtSetValues (f->output_data.x->widget, al, 1);
10437 #else /* not USE_X_TOOLKIT && not USE_GTK */
10439 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
10440 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
10442 #endif /* not USE_X_TOOLKIT && not USE_GTK */
10445 void
10446 x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
10448 Window window = FRAME_OUTER_WINDOW (f);
10450 f->output_data.x->wm_hints.flags |= IconPositionHint;
10451 f->output_data.x->wm_hints.icon_x = icon_x;
10452 f->output_data.x->wm_hints.icon_y = icon_y;
10454 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
10458 /***********************************************************************
10459 Fonts
10460 ***********************************************************************/
10462 #ifdef GLYPH_DEBUG
10464 /* Check that FONT is valid on frame F. It is if it can be found in F's
10465 font table. */
10467 static void
10468 x_check_font (struct frame *f, struct font *font)
10470 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
10471 if (font->driver->check)
10472 eassert (font->driver->check (f, font) == 0);
10475 #endif /* GLYPH_DEBUG */
10478 /***********************************************************************
10479 Initialization
10480 ***********************************************************************/
10482 #ifdef USE_X_TOOLKIT
10483 static XrmOptionDescRec emacs_options[] = {
10484 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
10485 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
10487 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
10488 XrmoptionSepArg, NULL},
10489 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
10491 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10492 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10493 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10494 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10495 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10496 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
10497 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
10500 /* Whether atimer for Xt timeouts is activated or not. */
10502 static bool x_timeout_atimer_activated_flag;
10504 #endif /* USE_X_TOOLKIT */
10506 static int x_initialized;
10508 /* Test whether two display-name strings agree up to the dot that separates
10509 the screen number from the server number. */
10510 static bool
10511 same_x_server (const char *name1, const char *name2)
10513 bool seen_colon = false;
10514 Lisp_Object sysname = Fsystem_name ();
10515 const char *system_name = SSDATA (sysname);
10516 ptrdiff_t system_name_length = SBYTES (sysname);
10517 ptrdiff_t length_until_period = 0;
10519 while (system_name[length_until_period] != 0
10520 && system_name[length_until_period] != '.')
10521 length_until_period++;
10523 /* Treat `unix' like an empty host name. */
10524 if (! strncmp (name1, "unix:", 5))
10525 name1 += 4;
10526 if (! strncmp (name2, "unix:", 5))
10527 name2 += 4;
10528 /* Treat this host's name like an empty host name. */
10529 if (! strncmp (name1, system_name, system_name_length)
10530 && name1[system_name_length] == ':')
10531 name1 += system_name_length;
10532 if (! strncmp (name2, system_name, system_name_length)
10533 && name2[system_name_length] == ':')
10534 name2 += system_name_length;
10535 /* Treat this host's domainless name like an empty host name. */
10536 if (! strncmp (name1, system_name, length_until_period)
10537 && name1[length_until_period] == ':')
10538 name1 += length_until_period;
10539 if (! strncmp (name2, system_name, length_until_period)
10540 && name2[length_until_period] == ':')
10541 name2 += length_until_period;
10543 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
10545 if (*name1 == ':')
10546 seen_colon = true;
10547 if (seen_colon && *name1 == '.')
10548 return true;
10550 return (seen_colon
10551 && (*name1 == '.' || *name1 == '\0')
10552 && (*name2 == '.' || *name2 == '\0'));
10555 /* Count number of set bits in mask and number of bits to shift to
10556 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
10557 to 5. */
10558 static void
10559 get_bits_and_offset (unsigned long mask, int *bits, int *offset)
10561 int nr = 0;
10562 int off = 0;
10564 while (!(mask & 1))
10566 off++;
10567 mask >>= 1;
10570 while (mask & 1)
10572 nr++;
10573 mask >>= 1;
10576 *offset = off;
10577 *bits = nr;
10580 /* Return true iff display DISPLAY is available for use.
10581 But don't permanently open it, just test its availability. */
10583 bool
10584 x_display_ok (const char *display)
10586 Display *dpy = XOpenDisplay (display);
10587 if (!dpy)
10588 return false;
10589 XCloseDisplay (dpy);
10590 return true;
10593 #ifdef USE_GTK
10594 static void
10595 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
10596 const gchar *msg, gpointer user_data)
10598 if (!strstr (msg, "g_set_prgname"))
10599 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, msg);
10601 #endif
10603 /* Create invisible cursor on X display referred by DPYINFO. */
10605 static Cursor
10606 make_invisible_cursor (struct x_display_info *dpyinfo)
10608 Display *dpy = dpyinfo->display;
10609 static char const no_data[] = { 0 };
10610 Pixmap pix;
10611 XColor col;
10612 Cursor c = 0;
10614 x_catch_errors (dpy);
10615 pix = XCreateBitmapFromData (dpy, dpyinfo->root_window, no_data, 1, 1);
10616 if (! x_had_errors_p (dpy) && pix != None)
10618 Cursor pixc;
10619 col.pixel = 0;
10620 col.red = col.green = col.blue = 0;
10621 col.flags = DoRed | DoGreen | DoBlue;
10622 pixc = XCreatePixmapCursor (dpy, pix, pix, &col, &col, 0, 0);
10623 if (! x_had_errors_p (dpy) && pixc != None)
10624 c = pixc;
10625 XFreePixmap (dpy, pix);
10628 x_uncatch_errors ();
10630 return c;
10633 /* True if DPY supports Xfixes extension >= 4. */
10635 static bool
10636 x_probe_xfixes_extension (Display *dpy)
10638 #ifdef HAVE_XFIXES
10639 int major, minor;
10640 return XFixesQueryVersion (dpy, &major, &minor) && major >= 4;
10641 #else
10642 return false;
10643 #endif /* HAVE_XFIXES */
10646 /* Toggle mouse pointer visibility on frame F by using Xfixes functions. */
10648 static void
10649 xfixes_toggle_visible_pointer (struct frame *f, bool invisible)
10651 #ifdef HAVE_XFIXES
10652 if (invisible)
10653 XFixesHideCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10654 else
10655 XFixesShowCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10656 f->pointer_invisible = invisible;
10657 #else
10658 emacs_abort ();
10659 #endif /* HAVE_XFIXES */
10662 /* Toggle mouse pointer visibility on frame F by using invisible cursor. */
10664 static void
10665 x_toggle_visible_pointer (struct frame *f, bool invisible)
10667 eassert (FRAME_DISPLAY_INFO (f)->invisible_cursor != 0);
10668 if (invisible)
10669 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10670 FRAME_DISPLAY_INFO (f)->invisible_cursor);
10671 else
10672 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10673 f->output_data.x->current_cursor);
10674 f->pointer_invisible = invisible;
10677 /* Setup pointer blanking, prefer Xfixes if available. */
10679 static void
10680 x_setup_pointer_blanking (struct x_display_info *dpyinfo)
10682 /* FIXME: the brave tester should set EMACS_XFIXES because we're suspecting
10683 X server bug, see http://debbugs.gnu.org/cgi/bugreport.cgi?bug=17609. */
10684 if (egetenv ("EMACS_XFIXES") && x_probe_xfixes_extension (dpyinfo->display))
10685 dpyinfo->toggle_visible_pointer = xfixes_toggle_visible_pointer;
10686 else
10688 dpyinfo->toggle_visible_pointer = x_toggle_visible_pointer;
10689 dpyinfo->invisible_cursor = make_invisible_cursor (dpyinfo);
10693 /* Current X display connection identifier. Incremented for each next
10694 connection established. */
10695 static unsigned x_display_id;
10697 /* Open a connection to X display DISPLAY_NAME, and return
10698 the structure that describes the open display.
10699 If we cannot contact the display, return null. */
10701 struct x_display_info *
10702 x_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
10704 Display *dpy;
10705 struct terminal *terminal;
10706 struct x_display_info *dpyinfo;
10707 XrmDatabase xrdb;
10708 ptrdiff_t lim;
10710 block_input ();
10712 if (!x_initialized)
10714 x_initialize ();
10715 ++x_initialized;
10718 if (! x_display_ok (SSDATA (display_name)))
10719 error ("Display %s can't be opened", SSDATA (display_name));
10721 #ifdef USE_GTK
10723 #define NUM_ARGV 10
10724 int argc;
10725 char *argv[NUM_ARGV];
10726 char **argv2 = argv;
10727 guint id;
10729 if (x_initialized++ > 1)
10731 xg_display_open (SSDATA (display_name), &dpy);
10733 else
10735 static char display_opt[] = "--display";
10736 static char name_opt[] = "--name";
10738 for (argc = 0; argc < NUM_ARGV; ++argc)
10739 argv[argc] = 0;
10741 argc = 0;
10742 argv[argc++] = initial_argv[0];
10744 if (! NILP (display_name))
10746 argv[argc++] = display_opt;
10747 argv[argc++] = SSDATA (display_name);
10750 argv[argc++] = name_opt;
10751 argv[argc++] = resource_name;
10753 XSetLocaleModifiers ("");
10755 /* Work around GLib bug that outputs a faulty warning. See
10756 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
10757 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
10758 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
10759 /* NULL window -> events for all windows go to our function.
10760 Call before gtk_init so Gtk+ event filters comes after our. */
10761 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10763 /* gtk_init does set_locale. Fix locale before and after. */
10764 fixup_locale ();
10765 gtk_init (&argc, &argv2);
10766 fixup_locale ();
10768 g_log_remove_handler ("GLib", id);
10770 xg_initialize ();
10772 dpy = DEFAULT_GDK_DISPLAY ();
10774 #if ! GTK_CHECK_VERSION (2, 90, 0)
10775 /* Load our own gtkrc if it exists. */
10777 const char *file = "~/.emacs.d/gtkrc";
10778 Lisp_Object s, abs_file;
10780 s = build_string (file);
10781 abs_file = Fexpand_file_name (s, Qnil);
10783 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10784 gtk_rc_parse (SSDATA (abs_file));
10786 #endif
10788 XSetErrorHandler (x_error_handler);
10789 XSetIOErrorHandler (x_io_error_quitter);
10792 #else /* not USE_GTK */
10793 #ifdef USE_X_TOOLKIT
10794 /* weiner@footloose.sps.mot.com reports that this causes
10795 errors with X11R5:
10796 X protocol error: BadAtom (invalid Atom parameter)
10797 on protocol request 18skiloaf.
10798 So let's not use it until R6. */
10799 #ifdef HAVE_X11XTR6
10800 XtSetLanguageProc (NULL, NULL, NULL);
10801 #endif
10804 int argc = 0;
10805 char *argv[3];
10807 argv[0] = "";
10808 argc = 1;
10809 if (xrm_option)
10811 argv[argc++] = "-xrm";
10812 argv[argc++] = xrm_option;
10814 turn_on_atimers (false);
10815 dpy = XtOpenDisplay (Xt_app_con, SSDATA (display_name),
10816 resource_name, EMACS_CLASS,
10817 emacs_options, XtNumber (emacs_options),
10818 &argc, argv);
10819 turn_on_atimers (true);
10821 #ifdef HAVE_X11XTR6
10822 /* I think this is to compensate for XtSetLanguageProc. */
10823 fixup_locale ();
10824 #endif
10827 #else /* not USE_X_TOOLKIT */
10828 XSetLocaleModifiers ("");
10829 dpy = XOpenDisplay (SSDATA (display_name));
10830 #endif /* not USE_X_TOOLKIT */
10831 #endif /* not USE_GTK*/
10833 /* Detect failure. */
10834 if (dpy == 0)
10836 unblock_input ();
10837 return 0;
10840 /* We have definitely succeeded. Record the new connection. */
10842 dpyinfo = xzalloc (sizeof *dpyinfo);
10843 terminal = x_create_terminal (dpyinfo);
10846 struct x_display_info *share;
10848 for (share = x_display_list; share; share = share->next)
10849 if (same_x_server (SSDATA (XCAR (share->name_list_element)),
10850 SSDATA (display_name)))
10851 break;
10852 if (share)
10853 terminal->kboard = share->terminal->kboard;
10854 else
10856 terminal->kboard = allocate_kboard (Qx);
10858 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10860 char *vendor = ServerVendor (dpy);
10862 /* Protect terminal from GC before removing it from the
10863 list of terminals. */
10864 struct gcpro gcpro1;
10865 Lisp_Object gcpro_term;
10866 XSETTERMINAL (gcpro_term, terminal);
10867 GCPRO1 (gcpro_term);
10869 /* Temporarily hide the partially initialized terminal. */
10870 terminal_list = terminal->next_terminal;
10871 unblock_input ();
10872 kset_system_key_alist
10873 (terminal->kboard,
10874 call1 (Qvendor_specific_keysyms,
10875 vendor ? build_string (vendor) : empty_unibyte_string));
10876 block_input ();
10877 terminal->next_terminal = terminal_list;
10878 terminal_list = terminal;
10879 UNGCPRO;
10882 /* Don't let the initial kboard remain current longer than necessary.
10883 That would cause problems if a file loaded on startup tries to
10884 prompt in the mini-buffer. */
10885 if (current_kboard == initial_kboard)
10886 current_kboard = terminal->kboard;
10888 terminal->kboard->reference_count++;
10891 /* Put this display on the chain. */
10892 dpyinfo->next = x_display_list;
10893 x_display_list = dpyinfo;
10895 dpyinfo->name_list_element = Fcons (display_name, Qnil);
10896 dpyinfo->display = dpy;
10897 dpyinfo->connection = ConnectionNumber (dpyinfo->display);
10899 /* Set the name of the terminal. */
10900 terminal->name = xlispstrdup (display_name);
10902 #if false
10903 XSetAfterFunction (x_current_display, x_trace_wire);
10904 #endif
10906 lim = min (PTRDIFF_MAX, SIZE_MAX) - sizeof "@";
10907 Lisp_Object system_name = Fsystem_name ();
10908 if (lim - SBYTES (Vinvocation_name) < SBYTES (system_name))
10909 memory_full (SIZE_MAX);
10910 dpyinfo->x_id = ++x_display_id;
10911 dpyinfo->x_id_name = xmalloc (SBYTES (Vinvocation_name)
10912 + SBYTES (system_name) + 2);
10913 char *nametail = lispstpcpy (dpyinfo->x_id_name, Vinvocation_name);
10914 *nametail++ = '@';
10915 lispstpcpy (nametail, system_name);
10917 /* Figure out which modifier bits mean what. */
10918 x_find_modifier_meanings (dpyinfo);
10920 /* Get the scroll bar cursor. */
10921 #ifdef USE_GTK
10922 /* We must create a GTK cursor, it is required for GTK widgets. */
10923 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10924 #endif /* USE_GTK */
10926 dpyinfo->vertical_scroll_bar_cursor
10927 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10929 dpyinfo->horizontal_scroll_bar_cursor
10930 = XCreateFontCursor (dpyinfo->display, XC_sb_h_double_arrow);
10932 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10933 resource_name, EMACS_CLASS);
10934 #ifdef HAVE_XRMSETDATABASE
10935 XrmSetDatabase (dpyinfo->display, xrdb);
10936 #else
10937 dpyinfo->display->db = xrdb;
10938 #endif
10939 /* Put the rdb where we can find it in a way that works on
10940 all versions. */
10941 dpyinfo->xrdb = xrdb;
10943 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10944 DefaultScreen (dpyinfo->display));
10945 select_visual (dpyinfo);
10946 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10947 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10948 dpyinfo->icon_bitmap_id = -1;
10949 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10951 reset_mouse_highlight (&dpyinfo->mouse_highlight);
10953 /* See if we can construct pixel values from RGB values. */
10954 if (dpyinfo->visual->class == TrueColor)
10956 get_bits_and_offset (dpyinfo->visual->red_mask,
10957 &dpyinfo->red_bits, &dpyinfo->red_offset);
10958 get_bits_and_offset (dpyinfo->visual->blue_mask,
10959 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10960 get_bits_and_offset (dpyinfo->visual->green_mask,
10961 &dpyinfo->green_bits, &dpyinfo->green_offset);
10964 /* See if a private colormap is requested. */
10965 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10967 if (dpyinfo->visual->class == PseudoColor)
10969 AUTO_STRING (privateColormap, "privateColormap");
10970 AUTO_STRING (PrivateColormap, "PrivateColormap");
10971 Lisp_Object value
10972 = display_x_get_resource (dpyinfo, privateColormap,
10973 PrivateColormap, Qnil, Qnil);
10974 if (STRINGP (value)
10975 && (!strcmp (SSDATA (value), "true")
10976 || !strcmp (SSDATA (value), "on")))
10977 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10980 else
10981 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10982 dpyinfo->visual, AllocNone);
10984 #ifdef HAVE_XFT
10986 /* If we are using Xft, the following precautions should be made:
10988 1. Make sure that the Xrender extension is added before the Xft one.
10989 Otherwise, the close-display hook set by Xft is called after the one
10990 for Xrender, and the former tries to re-add the latter. This results
10991 in inconsistency of internal states and leads to X protocol error when
10992 one reconnects to the same X server (Bug#1696).
10994 2. Check dpi value in X resources. It is better we use it as well,
10995 since Xft will use it, as will all Gnome applications. If our real DPI
10996 is smaller or larger than the one Xft uses, our font will look smaller
10997 or larger than other for other applications, even if it is the same
10998 font name (monospace-10 for example). */
11000 int event_base, error_base;
11001 char *v;
11002 double d;
11004 XRenderQueryExtension (dpyinfo->display, &event_base, &error_base);
11006 v = XGetDefault (dpyinfo->display, "Xft", "dpi");
11007 if (v != NULL && sscanf (v, "%lf", &d) == 1)
11008 dpyinfo->resy = dpyinfo->resx = d;
11010 #endif
11012 if (dpyinfo->resy < 1)
11014 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
11015 double pixels = DisplayHeight (dpyinfo->display, screen_number);
11016 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
11017 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
11018 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
11019 pixels = DisplayWidth (dpyinfo->display, screen_number);
11020 mm = DisplayWidthMM (dpyinfo->display, screen_number);
11021 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
11022 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
11026 static const struct
11028 const char *name;
11029 int offset;
11030 } atom_refs[] = {
11031 #define ATOM_REFS_INIT(string, member) \
11032 { string, offsetof (struct x_display_info, member) },
11033 ATOM_REFS_INIT ("WM_PROTOCOLS", Xatom_wm_protocols)
11034 ATOM_REFS_INIT ("WM_TAKE_FOCUS", Xatom_wm_take_focus)
11035 ATOM_REFS_INIT ("WM_SAVE_YOURSELF", Xatom_wm_save_yourself)
11036 ATOM_REFS_INIT ("WM_DELETE_WINDOW", Xatom_wm_delete_window)
11037 ATOM_REFS_INIT ("WM_CHANGE_STATE", Xatom_wm_change_state)
11038 ATOM_REFS_INIT ("WM_CONFIGURE_DENIED", Xatom_wm_configure_denied)
11039 ATOM_REFS_INIT ("WM_MOVED", Xatom_wm_window_moved)
11040 ATOM_REFS_INIT ("WM_CLIENT_LEADER", Xatom_wm_client_leader)
11041 ATOM_REFS_INIT ("Editres", Xatom_editres)
11042 ATOM_REFS_INIT ("CLIPBOARD", Xatom_CLIPBOARD)
11043 ATOM_REFS_INIT ("TIMESTAMP", Xatom_TIMESTAMP)
11044 ATOM_REFS_INIT ("TEXT", Xatom_TEXT)
11045 ATOM_REFS_INIT ("COMPOUND_TEXT", Xatom_COMPOUND_TEXT)
11046 ATOM_REFS_INIT ("UTF8_STRING", Xatom_UTF8_STRING)
11047 ATOM_REFS_INIT ("DELETE", Xatom_DELETE)
11048 ATOM_REFS_INIT ("MULTIPLE", Xatom_MULTIPLE)
11049 ATOM_REFS_INIT ("INCR", Xatom_INCR)
11050 ATOM_REFS_INIT ("_EMACS_TMP_", Xatom_EMACS_TMP)
11051 ATOM_REFS_INIT ("TARGETS", Xatom_TARGETS)
11052 ATOM_REFS_INIT ("NULL", Xatom_NULL)
11053 ATOM_REFS_INIT ("ATOM", Xatom_ATOM)
11054 ATOM_REFS_INIT ("ATOM_PAIR", Xatom_ATOM_PAIR)
11055 ATOM_REFS_INIT ("CLIPBOARD_MANAGER", Xatom_CLIPBOARD_MANAGER)
11056 ATOM_REFS_INIT ("_XEMBED_INFO", Xatom_XEMBED_INFO)
11057 /* For properties of font. */
11058 ATOM_REFS_INIT ("PIXEL_SIZE", Xatom_PIXEL_SIZE)
11059 ATOM_REFS_INIT ("AVERAGE_WIDTH", Xatom_AVERAGE_WIDTH)
11060 ATOM_REFS_INIT ("_MULE_BASELINE_OFFSET", Xatom_MULE_BASELINE_OFFSET)
11061 ATOM_REFS_INIT ("_MULE_RELATIVE_COMPOSE", Xatom_MULE_RELATIVE_COMPOSE)
11062 ATOM_REFS_INIT ("_MULE_DEFAULT_ASCENT", Xatom_MULE_DEFAULT_ASCENT)
11063 /* Ghostscript support. */
11064 ATOM_REFS_INIT ("DONE", Xatom_DONE)
11065 ATOM_REFS_INIT ("PAGE", Xatom_PAGE)
11066 ATOM_REFS_INIT ("SCROLLBAR", Xatom_Scrollbar)
11067 ATOM_REFS_INIT ("HORIZONTAL_SCROLLBAR", Xatom_Horizontal_Scrollbar)
11068 ATOM_REFS_INIT ("_XEMBED", Xatom_XEMBED)
11069 /* EWMH */
11070 ATOM_REFS_INIT ("_NET_WM_STATE", Xatom_net_wm_state)
11071 ATOM_REFS_INIT ("_NET_WM_STATE_FULLSCREEN", Xatom_net_wm_state_fullscreen)
11072 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_HORZ",
11073 Xatom_net_wm_state_maximized_horz)
11074 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_VERT",
11075 Xatom_net_wm_state_maximized_vert)
11076 ATOM_REFS_INIT ("_NET_WM_STATE_STICKY", Xatom_net_wm_state_sticky)
11077 ATOM_REFS_INIT ("_NET_WM_STATE_HIDDEN", Xatom_net_wm_state_hidden)
11078 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE", Xatom_net_window_type)
11079 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE_TOOLTIP",
11080 Xatom_net_window_type_tooltip)
11081 ATOM_REFS_INIT ("_NET_WM_ICON_NAME", Xatom_net_wm_icon_name)
11082 ATOM_REFS_INIT ("_NET_WM_NAME", Xatom_net_wm_name)
11083 ATOM_REFS_INIT ("_NET_SUPPORTED", Xatom_net_supported)
11084 ATOM_REFS_INIT ("_NET_SUPPORTING_WM_CHECK", Xatom_net_supporting_wm_check)
11085 ATOM_REFS_INIT ("_NET_WM_WINDOW_OPACITY", Xatom_net_wm_window_opacity)
11086 ATOM_REFS_INIT ("_NET_ACTIVE_WINDOW", Xatom_net_active_window)
11087 ATOM_REFS_INIT ("_NET_FRAME_EXTENTS", Xatom_net_frame_extents)
11088 ATOM_REFS_INIT ("_NET_CURRENT_DESKTOP", Xatom_net_current_desktop)
11089 ATOM_REFS_INIT ("_NET_WORKAREA", Xatom_net_workarea)
11090 /* Session management */
11091 ATOM_REFS_INIT ("SM_CLIENT_ID", Xatom_SM_CLIENT_ID)
11092 ATOM_REFS_INIT ("_XSETTINGS_SETTINGS", Xatom_xsettings_prop)
11093 ATOM_REFS_INIT ("MANAGER", Xatom_xsettings_mgr)
11096 int i;
11097 enum { atom_count = ARRAYELTS (atom_refs) };
11098 /* 1 for _XSETTINGS_SN. */
11099 enum { total_atom_count = 1 + atom_count };
11100 Atom atoms_return[total_atom_count];
11101 char *atom_names[total_atom_count];
11102 static char const xsettings_fmt[] = "_XSETTINGS_S%d";
11103 char xsettings_atom_name[sizeof xsettings_fmt - 2
11104 + INT_STRLEN_BOUND (int)];
11106 for (i = 0; i < atom_count; i++)
11107 atom_names[i] = (char *) atom_refs[i].name;
11109 /* Build _XSETTINGS_SN atom name. */
11110 sprintf (xsettings_atom_name, xsettings_fmt,
11111 XScreenNumberOfScreen (dpyinfo->screen));
11112 atom_names[i] = xsettings_atom_name;
11114 XInternAtoms (dpyinfo->display, atom_names, total_atom_count,
11115 False, atoms_return);
11117 for (i = 0; i < atom_count; i++)
11118 *(Atom *) ((char *) dpyinfo + atom_refs[i].offset) = atoms_return[i];
11120 /* Manually copy last atom. */
11121 dpyinfo->Xatom_xsettings_sel = atoms_return[i];
11124 dpyinfo->x_dnd_atoms_size = 8;
11125 dpyinfo->x_dnd_atoms = xmalloc (sizeof *dpyinfo->x_dnd_atoms
11126 * dpyinfo->x_dnd_atoms_size);
11127 dpyinfo->gray
11128 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
11129 gray_bits, gray_width, gray_height,
11130 1, 0, 1);
11132 x_setup_pointer_blanking (dpyinfo);
11134 #ifdef HAVE_X_I18N
11135 xim_initialize (dpyinfo, resource_name);
11136 #endif
11138 xsettings_initialize (dpyinfo);
11140 /* This is only needed for distinguishing keyboard and process input. */
11141 if (dpyinfo->connection != 0)
11142 add_keyboard_wait_descriptor (dpyinfo->connection);
11144 #ifdef F_SETOWN
11145 fcntl (dpyinfo->connection, F_SETOWN, getpid ());
11146 #endif /* ! defined (F_SETOWN) */
11148 if (interrupt_input)
11149 init_sigio (dpyinfo->connection);
11151 #ifdef USE_LUCID
11153 XrmValue d, fr, to;
11154 Font font;
11156 dpy = dpyinfo->display;
11157 d.addr = (XPointer)&dpy;
11158 d.size = sizeof (Display *);
11159 fr.addr = XtDefaultFont;
11160 fr.size = sizeof (XtDefaultFont);
11161 to.size = sizeof (Font *);
11162 to.addr = (XPointer)&font;
11163 x_catch_errors (dpy);
11164 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
11165 emacs_abort ();
11166 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
11167 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
11168 /* Do not free XFontStruct returned by the above call to XQueryFont.
11169 This leads to X protocol errors at XtCloseDisplay (Bug#18403). */
11170 x_uncatch_errors ();
11172 #endif
11174 /* See if we should run in synchronous mode. This is useful
11175 for debugging X code. */
11177 AUTO_STRING (synchronous, "synchronous");
11178 AUTO_STRING (Synchronous, "Synchronous");
11179 Lisp_Object value = display_x_get_resource (dpyinfo, synchronous,
11180 Synchronous, Qnil, Qnil);
11181 if (STRINGP (value)
11182 && (!strcmp (SSDATA (value), "true")
11183 || !strcmp (SSDATA (value), "on")))
11184 XSynchronize (dpyinfo->display, True);
11188 AUTO_STRING (useXIM, "useXIM");
11189 AUTO_STRING (UseXIM, "UseXIM");
11190 Lisp_Object value = display_x_get_resource (dpyinfo, useXIM, UseXIM,
11191 Qnil, Qnil);
11192 #ifdef USE_XIM
11193 if (STRINGP (value)
11194 && (!strcmp (SSDATA (value), "false")
11195 || !strcmp (SSDATA (value), "off")))
11196 use_xim = false;
11197 #else
11198 if (STRINGP (value)
11199 && (!strcmp (SSDATA (value), "true")
11200 || !strcmp (SSDATA (value), "on")))
11201 use_xim = true;
11202 #endif
11205 #ifdef HAVE_X_SM
11206 /* Only do this for the very first display in the Emacs session.
11207 Ignore X session management when Emacs was first started on a
11208 tty or started as a daemon. */
11209 if (terminal->id == 1 && ! IS_DAEMON)
11210 x_session_initialize (dpyinfo);
11211 #endif
11213 unblock_input ();
11215 return dpyinfo;
11218 /* Get rid of display DPYINFO, deleting all frames on it,
11219 and without sending any more commands to the X server. */
11221 static void
11222 x_delete_display (struct x_display_info *dpyinfo)
11224 struct terminal *t;
11226 /* Close all frames and delete the generic struct terminal for this
11227 X display. */
11228 for (t = terminal_list; t; t = t->next_terminal)
11229 if (t->type == output_x_window && t->display_info.x == dpyinfo)
11231 #ifdef HAVE_X_SM
11232 /* Close X session management when we close its display. */
11233 if (t->id == 1 && x_session_have_connection ())
11234 x_session_close ();
11235 #endif
11236 delete_terminal (t);
11237 break;
11240 if (next_noop_dpyinfo == dpyinfo)
11241 next_noop_dpyinfo = dpyinfo->next;
11243 if (x_display_list == dpyinfo)
11244 x_display_list = dpyinfo->next;
11245 else
11247 struct x_display_info *tail;
11249 for (tail = x_display_list; tail; tail = tail->next)
11250 if (tail->next == dpyinfo)
11251 tail->next = tail->next->next;
11254 xfree (dpyinfo->x_id_name);
11255 xfree (dpyinfo->x_dnd_atoms);
11256 xfree (dpyinfo->color_cells);
11257 xfree (dpyinfo);
11260 #ifdef USE_X_TOOLKIT
11262 /* Atimer callback function for TIMER. Called every 0.1s to process
11263 Xt timeouts, if needed. We must avoid calling XtAppPending as
11264 much as possible because that function does an implicit XFlush
11265 that slows us down. */
11267 static void
11268 x_process_timeouts (struct atimer *timer)
11270 block_input ();
11271 x_timeout_atimer_activated_flag = false;
11272 if (toolkit_scroll_bar_interaction || popup_activated ())
11274 while (XtAppPending (Xt_app_con) & XtIMTimer)
11275 XtAppProcessEvent (Xt_app_con, XtIMTimer);
11276 /* Reactivate the atimer for next time. */
11277 x_activate_timeout_atimer ();
11279 unblock_input ();
11282 /* Install an asynchronous timer that processes Xt timeout events
11283 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
11284 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
11285 function whenever these variables are set. This is necessary
11286 because some widget sets use timeouts internally, for example the
11287 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
11288 processed, these widgets don't behave normally. */
11290 void
11291 x_activate_timeout_atimer (void)
11293 block_input ();
11294 if (!x_timeout_atimer_activated_flag)
11296 struct timespec interval = make_timespec (0, 100 * 1000 * 1000);
11297 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
11298 x_timeout_atimer_activated_flag = true;
11300 unblock_input ();
11303 #endif /* USE_X_TOOLKIT */
11306 /* Set up use of X before we make the first connection. */
11308 static struct redisplay_interface x_redisplay_interface =
11310 x_frame_parm_handlers,
11311 x_produce_glyphs,
11312 x_write_glyphs,
11313 x_insert_glyphs,
11314 x_clear_end_of_line,
11315 x_scroll_run,
11316 x_after_update_window_line,
11317 x_update_window_begin,
11318 x_update_window_end,
11319 x_flush,
11320 x_clear_window_mouse_face,
11321 x_get_glyph_overhangs,
11322 x_fix_overlapping_area,
11323 x_draw_fringe_bitmap,
11324 0, /* define_fringe_bitmap */
11325 0, /* destroy_fringe_bitmap */
11326 x_compute_glyph_string_overhangs,
11327 x_draw_glyph_string,
11328 x_define_frame_cursor,
11329 x_clear_frame_area,
11330 x_draw_window_cursor,
11331 x_draw_vertical_window_border,
11332 x_draw_window_divider,
11333 x_shift_glyphs_for_insert,
11334 x_show_hourglass,
11335 x_hide_hourglass
11339 /* This function is called when the last frame on a display is deleted. */
11340 void
11341 x_delete_terminal (struct terminal *terminal)
11343 struct x_display_info *dpyinfo = terminal->display_info.x;
11345 /* Protect against recursive calls. delete_frame in
11346 delete_terminal calls us back when it deletes our last frame. */
11347 if (!terminal->name)
11348 return;
11350 block_input ();
11351 #ifdef HAVE_X_I18N
11352 /* We must close our connection to the XIM server before closing the
11353 X display. */
11354 if (dpyinfo->xim)
11355 xim_close_dpy (dpyinfo);
11356 #endif
11358 /* Normally, the display is available... */
11359 if (dpyinfo->display)
11361 x_destroy_all_bitmaps (dpyinfo);
11362 XSetCloseDownMode (dpyinfo->display, DestroyAll);
11364 /* Whether or not XCloseDisplay destroys the associated resource
11365 database depends on the version of libX11. To avoid both
11366 crash and memory leak, we dissociate the database from the
11367 display and then destroy dpyinfo->xrdb ourselves.
11369 Unfortunately, the above strategy does not work in some
11370 situations due to a bug in newer versions of libX11: because
11371 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
11372 dpy->db is NULL, XCloseDisplay destroys the associated
11373 database whereas it has not been created by XGetDefault
11374 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
11375 don't destroy the database here in order to avoid the crash
11376 in the above situations for now, though that may cause memory
11377 leaks in other situations. */
11378 #if false
11379 #ifdef HAVE_XRMSETDATABASE
11380 XrmSetDatabase (dpyinfo->display, NULL);
11381 #else
11382 dpyinfo->display->db = NULL;
11383 #endif
11384 /* We used to call XrmDestroyDatabase from x_delete_display, but
11385 some older versions of libX11 crash if we call it after
11386 closing all the displays. */
11387 XrmDestroyDatabase (dpyinfo->xrdb);
11388 #endif
11390 #ifdef USE_GTK
11391 xg_display_close (dpyinfo->display);
11392 #else
11393 #ifdef USE_X_TOOLKIT
11394 XtCloseDisplay (dpyinfo->display);
11395 #else
11396 XCloseDisplay (dpyinfo->display);
11397 #endif
11398 #endif /* ! USE_GTK */
11399 /* Do not close the connection here because it's already closed
11400 by X(t)CloseDisplay (Bug#18403). */
11401 dpyinfo->display = NULL;
11404 /* ...but if called from x_connection_closed, the display may already
11405 be closed and dpyinfo->display was set to 0 to indicate that. Since
11406 X server is most likely gone, explicit close is the only reliable
11407 way to continue and avoid Bug#19147. */
11408 else if (dpyinfo->connection >= 0)
11409 emacs_close (dpyinfo->connection);
11411 /* No more input on this descriptor. */
11412 delete_keyboard_wait_descriptor (dpyinfo->connection);
11413 /* Mark as dead. */
11414 dpyinfo->connection = -1;
11416 x_delete_display (dpyinfo);
11417 unblock_input ();
11420 /* Create a struct terminal, initialize it with the X11 specific
11421 functions and make DISPLAY->TERMINAL point to it. */
11423 static struct terminal *
11424 x_create_terminal (struct x_display_info *dpyinfo)
11426 struct terminal *terminal;
11428 terminal = create_terminal (output_x_window, &x_redisplay_interface);
11430 terminal->display_info.x = dpyinfo;
11431 dpyinfo->terminal = terminal;
11433 /* kboard is initialized in x_term_init. */
11435 terminal->clear_frame_hook = x_clear_frame;
11436 terminal->ins_del_lines_hook = x_ins_del_lines;
11437 terminal->delete_glyphs_hook = x_delete_glyphs;
11438 terminal->ring_bell_hook = XTring_bell;
11439 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
11440 terminal->update_begin_hook = x_update_begin;
11441 terminal->update_end_hook = x_update_end;
11442 terminal->read_socket_hook = XTread_socket;
11443 terminal->frame_up_to_date_hook = XTframe_up_to_date;
11444 terminal->mouse_position_hook = XTmouse_position;
11445 terminal->frame_rehighlight_hook = XTframe_rehighlight;
11446 terminal->frame_raise_lower_hook = XTframe_raise_lower;
11447 terminal->fullscreen_hook = XTfullscreen_hook;
11448 terminal->menu_show_hook = x_menu_show;
11449 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
11450 terminal->popup_dialog_hook = xw_popup_dialog;
11451 #endif
11452 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
11453 terminal->set_horizontal_scroll_bar_hook = XTset_horizontal_scroll_bar;
11454 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
11455 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
11456 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
11457 terminal->delete_frame_hook = x_destroy_window;
11458 terminal->delete_terminal_hook = x_delete_terminal;
11459 /* Other hooks are NULL by default. */
11461 return terminal;
11464 static void
11465 x_initialize (void)
11467 baud_rate = 19200;
11469 x_noop_count = 0;
11470 any_help_event_p = false;
11471 ignore_next_mouse_click_timeout = 0;
11473 #ifdef USE_GTK
11474 current_count = -1;
11475 #endif
11477 /* Try to use interrupt input; if we can't, then start polling. */
11478 Fset_input_interrupt_mode (Qt);
11480 #ifdef USE_X_TOOLKIT
11481 XtToolkitInitialize ();
11483 Xt_app_con = XtCreateApplicationContext ();
11485 /* Register a converter from strings to pixels, which uses
11486 Emacs' color allocation infrastructure. */
11487 XtAppSetTypeConverter (Xt_app_con,
11488 XtRString, XtRPixel, cvt_string_to_pixel,
11489 cvt_string_to_pixel_args,
11490 XtNumber (cvt_string_to_pixel_args),
11491 XtCacheByDisplay, cvt_pixel_dtor);
11493 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
11494 #endif
11496 #ifdef USE_TOOLKIT_SCROLL_BARS
11497 #ifndef USE_GTK
11498 xaw3d_arrow_scroll = False;
11499 xaw3d_pick_top = True;
11500 #endif
11501 #endif
11503 /* Note that there is no real way portable across R3/R4 to get the
11504 original error handler. */
11505 XSetErrorHandler (x_error_handler);
11506 XSetIOErrorHandler (x_io_error_quitter);
11509 #ifdef USE_GTK
11510 void
11511 init_xterm (void)
11513 /* Emacs can handle only core input events, so make sure
11514 Gtk doesn't use Xinput or Xinput2 extensions. */
11515 xputenv ("GDK_CORE_DEVICE_EVENTS=1");
11517 #endif
11519 void
11520 syms_of_xterm (void)
11522 x_error_message = NULL;
11524 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
11525 DEFSYM (Qlatin_1, "latin-1");
11527 #ifdef USE_GTK
11528 xg_default_icon_file = build_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
11529 staticpro (&xg_default_icon_file);
11531 DEFSYM (Qx_gtk_map_stock, "x-gtk-map-stock");
11532 #endif
11534 DEFVAR_BOOL ("x-use-underline-position-properties",
11535 x_use_underline_position_properties,
11536 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
11537 A value of nil means ignore them. If you encounter fonts with bogus
11538 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
11539 to 4.1, set this to nil. You can also use `underline-minimum-offset'
11540 to override the font's UNDERLINE_POSITION for small font display
11541 sizes. */);
11542 x_use_underline_position_properties = true;
11544 DEFVAR_BOOL ("x-underline-at-descent-line",
11545 x_underline_at_descent_line,
11546 doc: /* Non-nil means to draw the underline at the same place as the descent line.
11547 A value of nil means to draw the underline according to the value of the
11548 variable `x-use-underline-position-properties', which is usually at the
11549 baseline level. The default value is nil. */);
11550 x_underline_at_descent_line = false;
11552 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
11553 x_mouse_click_focus_ignore_position,
11554 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
11555 This variable is only used when the window manager requires that you
11556 click on a frame to select it (give it focus). In that case, a value
11557 of nil, means that the selected window and cursor position changes to
11558 reflect the mouse click position, while a non-nil value means that the
11559 selected window or cursor position is preserved. */);
11560 x_mouse_click_focus_ignore_position = false;
11562 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
11563 doc: /* Which toolkit scroll bars Emacs uses, if any.
11564 A value of nil means Emacs doesn't use toolkit scroll bars.
11565 With the X Window system, the value is a symbol describing the
11566 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
11567 With MS Windows or Nextstep, the value is t. */);
11568 #ifdef USE_TOOLKIT_SCROLL_BARS
11569 #ifdef USE_MOTIF
11570 Vx_toolkit_scroll_bars = intern_c_string ("motif");
11571 #elif defined HAVE_XAW3D
11572 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
11573 #elif USE_GTK
11574 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
11575 #else
11576 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
11577 #endif
11578 #else
11579 Vx_toolkit_scroll_bars = Qnil;
11580 #endif
11582 DEFSYM (Qmodifier_value, "modifier-value");
11583 DEFSYM (Qalt, "alt");
11584 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
11585 DEFSYM (Qhyper, "hyper");
11586 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
11587 DEFSYM (Qmeta, "meta");
11588 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
11589 DEFSYM (Qsuper, "super");
11590 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
11592 DEFVAR_LISP ("x-alt-keysym", Vx_alt_keysym,
11593 doc: /* Which keys Emacs uses for the alt modifier.
11594 This should be one of the symbols `alt', `hyper', `meta', `super'.
11595 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
11596 is nil, which is the same as `alt'. */);
11597 Vx_alt_keysym = Qnil;
11599 DEFVAR_LISP ("x-hyper-keysym", Vx_hyper_keysym,
11600 doc: /* Which keys Emacs uses for the hyper modifier.
11601 This should be one of the symbols `alt', `hyper', `meta', `super'.
11602 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
11603 default is nil, which is the same as `hyper'. */);
11604 Vx_hyper_keysym = Qnil;
11606 DEFVAR_LISP ("x-meta-keysym", Vx_meta_keysym,
11607 doc: /* Which keys Emacs uses for the meta modifier.
11608 This should be one of the symbols `alt', `hyper', `meta', `super'.
11609 For example, `meta' means use the Meta_L and Meta_R keysyms. The
11610 default is nil, which is the same as `meta'. */);
11611 Vx_meta_keysym = Qnil;
11613 DEFVAR_LISP ("x-super-keysym", Vx_super_keysym,
11614 doc: /* Which keys Emacs uses for the super modifier.
11615 This should be one of the symbols `alt', `hyper', `meta', `super'.
11616 For example, `super' means use the Super_L and Super_R keysyms. The
11617 default is nil, which is the same as `super'. */);
11618 Vx_super_keysym = Qnil;
11620 DEFVAR_LISP ("x-keysym-table", Vx_keysym_table,
11621 doc: /* Hash table of character codes indexed by X keysym codes. */);
11622 Vx_keysym_table = make_hash_table (hashtest_eql, make_number (900),
11623 make_float (DEFAULT_REHASH_SIZE),
11624 make_float (DEFAULT_REHASH_THRESHOLD),
11625 Qnil);