Some fixes for scroll bar code.
[emacs.git] / src / xterm.c
blobe4a725a4ab1f51ddf9fab2c7d4ce79eb37ac6089
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 #include "fontset.h"
67 #include "termhooks.h"
68 #include "termopts.h"
69 #include "termchar.h"
70 #include "emacs-icon.h"
71 #include "disptab.h"
72 #include "buffer.h"
73 #include "window.h"
74 #include "keyboard.h"
75 #include "intervals.h"
76 #include "process.h"
77 #include "atimer.h"
78 #include "keymap.h"
79 #include "font.h"
80 #include "xsettings.h"
81 #include "xgselect.h"
82 #include "sysselect.h"
83 #include "menu.h"
85 #ifdef USE_X_TOOLKIT
86 #include <X11/Shell.h>
87 #endif
89 #include <unistd.h>
91 #ifdef USE_GTK
92 #include "gtkutil.h"
93 #ifdef HAVE_GTK3
94 #include <X11/Xproto.h>
95 #endif
96 #endif
98 #if defined (USE_LUCID) || defined (USE_MOTIF)
99 #include "../lwlib/xlwmenu.h"
100 #endif
102 #ifdef USE_X_TOOLKIT
103 #if !defined (NO_EDITRES)
104 #define HACK_EDITRES
105 extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
106 #endif /* not NO_EDITRES */
108 /* Include toolkit specific headers for the scroll bar widget. */
110 #ifdef USE_TOOLKIT_SCROLL_BARS
111 #if defined USE_MOTIF
112 #include <Xm/Xm.h> /* For LESSTIF_VERSION */
113 #include <Xm/ScrollBar.h>
114 #else /* !USE_MOTIF i.e. use Xaw */
116 #ifdef HAVE_XAW3D
117 #include <X11/Xaw3d/Simple.h>
118 #include <X11/Xaw3d/Scrollbar.h>
119 #include <X11/Xaw3d/ThreeD.h>
120 #else /* !HAVE_XAW3D */
121 #include <X11/Xaw/Simple.h>
122 #include <X11/Xaw/Scrollbar.h>
123 #endif /* !HAVE_XAW3D */
124 #ifndef XtNpickTop
125 #define XtNpickTop "pickTop"
126 #endif /* !XtNpickTop */
127 #endif /* !USE_MOTIF */
128 #endif /* USE_TOOLKIT_SCROLL_BARS */
130 #endif /* USE_X_TOOLKIT */
132 #ifdef USE_X_TOOLKIT
133 #include "widget.h"
134 #ifndef XtNinitialState
135 #define XtNinitialState "initialState"
136 #endif
137 #endif
139 #include "bitmaps/gray.xbm"
141 #ifdef HAVE_XKB
142 #include <X11/XKBlib.h>
143 #endif
145 /* Default to using XIM if available. */
146 #ifdef USE_XIM
147 bool use_xim = true;
148 #else
149 bool use_xim = false; /* configure --without-xim */
150 #endif
152 /* Non-zero means that a HELP_EVENT has been generated since Emacs
153 start. */
155 static bool any_help_event_p;
157 /* This is a chain of structures for all the X displays currently in
158 use. */
160 struct x_display_info *x_display_list;
162 #ifdef USE_X_TOOLKIT
164 /* The application context for Xt use. */
165 XtAppContext Xt_app_con;
166 static String Xt_default_resources[] = {0};
168 /* Non-zero means user is interacting with a toolkit scroll bar. */
169 static bool toolkit_scroll_bar_interaction;
171 #endif /* USE_X_TOOLKIT */
173 /* Non-zero timeout value means ignore next mouse click if it arrives
174 before that timeout elapses (i.e. as part of the same sequence of
175 events resulting from clicking on a frame to select it). */
177 static Time ignore_next_mouse_click_timeout;
179 /* Used locally within XTread_socket. */
181 static int x_noop_count;
183 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
185 static Lisp_Object Qvendor_specific_keysyms;
186 static Lisp_Object Qlatin_1;
188 #ifdef USE_GTK
189 /* The name of the Emacs icon file. */
190 static Lisp_Object xg_default_icon_file;
192 /* Used in gtkutil.c. */
193 Lisp_Object Qx_gtk_map_stock;
194 #endif
196 /* Some functions take this as char *, not const char *. */
197 static char emacs_class[] = EMACS_CLASS;
199 enum xembed_info
201 XEMBED_MAPPED = 1 << 0
204 enum xembed_message
206 XEMBED_EMBEDDED_NOTIFY = 0,
207 XEMBED_WINDOW_ACTIVATE = 1,
208 XEMBED_WINDOW_DEACTIVATE = 2,
209 XEMBED_REQUEST_FOCUS = 3,
210 XEMBED_FOCUS_IN = 4,
211 XEMBED_FOCUS_OUT = 5,
212 XEMBED_FOCUS_NEXT = 6,
213 XEMBED_FOCUS_PREV = 7,
215 XEMBED_MODALITY_ON = 10,
216 XEMBED_MODALITY_OFF = 11,
217 XEMBED_REGISTER_ACCELERATOR = 12,
218 XEMBED_UNREGISTER_ACCELERATOR = 13,
219 XEMBED_ACTIVATE_ACCELERATOR = 14
222 static bool x_alloc_nearest_color_1 (Display *, Colormap, XColor *);
223 static void x_set_window_size_1 (struct frame *, int, int, int, bool);
224 static void x_raise_frame (struct frame *);
225 static void x_lower_frame (struct frame *);
226 static const XColor *x_color_cells (Display *, int *);
227 static int x_io_error_quitter (Display *);
228 static struct terminal *x_create_terminal (struct x_display_info *);
229 static void x_update_end (struct frame *);
230 static void XTframe_up_to_date (struct frame *);
231 static void x_clear_frame (struct frame *);
232 static _Noreturn void x_ins_del_lines (struct frame *, int, int);
233 static void frame_highlight (struct frame *);
234 static void frame_unhighlight (struct frame *);
235 static void x_new_focus_frame (struct x_display_info *, struct frame *);
236 static void x_focus_changed (int, int, struct x_display_info *,
237 struct frame *, struct input_event *);
238 static void XTframe_rehighlight (struct frame *);
239 static void x_frame_rehighlight (struct x_display_info *);
240 static void x_draw_hollow_cursor (struct window *, struct glyph_row *);
241 static void x_draw_bar_cursor (struct window *, struct glyph_row *, int,
242 enum text_cursor_kinds);
244 static void x_clip_to_row (struct window *, struct glyph_row *,
245 enum glyph_row_area, GC);
246 static void x_flush (struct frame *f);
247 static void x_update_begin (struct frame *);
248 static void x_update_window_begin (struct window *);
249 static struct scroll_bar *x_window_to_scroll_bar (Display *, Window, int);
250 static void x_scroll_bar_report_motion (struct frame **, Lisp_Object *,
251 enum scroll_bar_part *,
252 Lisp_Object *, Lisp_Object *,
253 Time *);
254 static void x_horizontal_scroll_bar_report_motion (struct frame **, Lisp_Object *,
255 enum scroll_bar_part *,
256 Lisp_Object *, Lisp_Object *,
257 Time *);
258 static int x_handle_net_wm_state (struct frame *, const XPropertyEvent *);
259 static void x_check_fullscreen (struct frame *);
260 static void x_check_expected_move (struct frame *, int, int);
261 static void x_sync_with_move (struct frame *, int, int, int);
262 static int handle_one_xevent (struct x_display_info *,
263 const XEvent *, int *,
264 struct input_event *);
265 #if ! (defined USE_X_TOOLKIT || defined USE_MOTIF)
266 static int x_dispatch_event (XEvent *, Display *);
267 #endif
268 /* Don't declare this _Noreturn because we want no
269 interference with debugging failing X calls. */
270 static void x_connection_closed (Display *, const char *);
271 static void x_wm_set_window_state (struct frame *, int);
272 static void x_wm_set_icon_pixmap (struct frame *, ptrdiff_t);
273 static void x_initialize (void);
275 static int get_current_wm_state (struct frame *, Window, int *, int *);
277 /* Flush display of frame F. */
279 static void
280 x_flush (struct frame *f)
282 eassert (f && FRAME_X_P (f));
283 /* Don't call XFlush when it is not safe to redisplay; the X
284 connection may be broken. */
285 if (!NILP (Vinhibit_redisplay))
286 return;
288 block_input ();
289 XFlush (FRAME_X_DISPLAY (f));
290 unblock_input ();
294 /* Remove calls to XFlush by defining XFlush to an empty replacement.
295 Calls to XFlush should be unnecessary because the X output buffer
296 is flushed automatically as needed by calls to XPending,
297 XNextEvent, or XWindowEvent according to the XFlush man page.
298 XTread_socket calls XPending. Removing XFlush improves
299 performance. */
301 #define XFlush(DISPLAY) (void) 0
304 /***********************************************************************
305 Debugging
306 ***********************************************************************/
308 #if 0
310 /* This is a function useful for recording debugging information about
311 the sequence of occurrences in this file. */
313 struct record
315 char *locus;
316 int type;
319 struct record event_record[100];
321 int event_record_index;
323 void
324 record_event (char *locus, int type)
326 if (event_record_index == ARRAYELTS (event_record))
327 event_record_index = 0;
329 event_record[event_record_index].locus = locus;
330 event_record[event_record_index].type = type;
331 event_record_index++;
334 #endif /* 0 */
338 /* Return the struct x_display_info corresponding to DPY. */
340 struct x_display_info *
341 x_display_info_for_display (Display *dpy)
343 struct x_display_info *dpyinfo;
345 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
346 if (dpyinfo->display == dpy)
347 return dpyinfo;
349 return 0;
352 static Window
353 x_find_topmost_parent (struct frame *f)
355 struct x_output *x = f->output_data.x;
356 Window win = None, wi = x->parent_desc;
357 Display *dpy = FRAME_X_DISPLAY (f);
359 while (wi != FRAME_DISPLAY_INFO (f)->root_window)
361 Window root;
362 Window *children;
363 unsigned int nchildren;
365 win = wi;
366 if (XQueryTree (dpy, win, &root, &wi, &children, &nchildren))
367 XFree (children);
368 else
369 break;
372 return win;
375 #define OPAQUE 0xffffffff
377 void
378 x_set_frame_alpha (struct frame *f)
380 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
381 Display *dpy = FRAME_X_DISPLAY (f);
382 Window win = FRAME_OUTER_WINDOW (f);
383 double alpha = 1.0;
384 double alpha_min = 1.0;
385 unsigned long opac;
386 Window parent;
388 if (dpyinfo->x_highlight_frame == f)
389 alpha = f->alpha[0];
390 else
391 alpha = f->alpha[1];
393 if (FLOATP (Vframe_alpha_lower_limit))
394 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
395 else if (INTEGERP (Vframe_alpha_lower_limit))
396 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
398 if (alpha < 0.0)
399 return;
400 else if (alpha > 1.0)
401 alpha = 1.0;
402 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
403 alpha = alpha_min;
405 opac = alpha * OPAQUE;
407 x_catch_errors (dpy);
409 /* If there is a parent from the window manager, put the property there
410 also, to work around broken window managers that fail to do that.
411 Do this unconditionally as this function is called on reparent when
412 alpha has not changed on the frame. */
414 parent = x_find_topmost_parent (f);
415 if (parent != None)
416 XChangeProperty (dpy, parent, dpyinfo->Xatom_net_wm_window_opacity,
417 XA_CARDINAL, 32, PropModeReplace,
418 (unsigned char *) &opac, 1);
420 /* return unless necessary */
422 unsigned char *data;
423 Atom actual;
424 int rc, format;
425 unsigned long n, left;
427 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
428 0, 1, False, XA_CARDINAL,
429 &actual, &format, &n, &left,
430 &data);
432 if (rc == Success && actual != None)
434 unsigned long value = *(unsigned long *)data;
435 XFree (data);
436 if (value == opac)
438 x_uncatch_errors ();
439 return;
444 XChangeProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
445 XA_CARDINAL, 32, PropModeReplace,
446 (unsigned char *) &opac, 1);
447 x_uncatch_errors ();
450 /***********************************************************************
451 Starting and ending an update
452 ***********************************************************************/
454 /* Start an update of frame F. This function is installed as a hook
455 for update_begin, i.e. it is called when update_begin is called.
456 This function is called prior to calls to x_update_window_begin for
457 each window being updated. Currently, there is nothing to do here
458 because all interesting stuff is done on a window basis. */
460 static void
461 x_update_begin (struct frame *f)
463 /* Nothing to do. */
467 /* Start update of window W. */
469 static void
470 x_update_window_begin (struct window *w)
472 struct frame *f = XFRAME (WINDOW_FRAME (w));
473 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
475 w->output_cursor = w->cursor;
477 block_input ();
479 if (f == hlinfo->mouse_face_mouse_frame)
481 /* Don't do highlighting for mouse motion during the update. */
482 hlinfo->mouse_face_defer = 1;
484 /* If F needs to be redrawn, simply forget about any prior mouse
485 highlighting. */
486 if (FRAME_GARBAGED_P (f))
487 hlinfo->mouse_face_window = Qnil;
490 unblock_input ();
494 /* Draw a vertical window border from (x,y0) to (x,y1) */
496 static void
497 x_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
499 struct frame *f = XFRAME (WINDOW_FRAME (w));
500 struct face *face;
502 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
503 if (face)
504 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
505 face->foreground);
507 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
508 f->output_data.x->normal_gc, x, y0, x, y1);
511 /* Draw a window divider from (x0,y0) to (x1,y1) */
513 static void
514 x_draw_window_divider (struct window *w, int x0, int x1, int y0, int y1)
516 struct frame *f = XFRAME (WINDOW_FRAME (w));
517 struct face *face = FACE_FROM_ID (f, WINDOW_DIVIDER_FACE_ID);
518 struct face *face_first = FACE_FROM_ID (f, WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
519 struct face *face_last = FACE_FROM_ID (f, WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
520 unsigned long color = face ? face->foreground : FRAME_FOREGROUND_PIXEL (f);
521 unsigned long color_first = (face_first
522 ? face_first->foreground
523 : FRAME_FOREGROUND_PIXEL (f));
524 unsigned long color_last = (face_last
525 ? face_last->foreground
526 : FRAME_FOREGROUND_PIXEL (f));
527 Display *display = FRAME_X_DISPLAY (f);
528 Window window = FRAME_X_WINDOW (f);
530 if (y1 - y0 > x1 - x0 && x1 - x0 > 2)
531 /* Vertical. */
533 XSetForeground (display, f->output_data.x->normal_gc, color_first);
534 XFillRectangle (display, window, f->output_data.x->normal_gc,
535 x0, y0, 1, y1 - y0);
536 XSetForeground (display, f->output_data.x->normal_gc, color);
537 XFillRectangle (display, window, f->output_data.x->normal_gc,
538 x0 + 1, y0, x1 - x0 - 2, y1 - y0);
539 XSetForeground (display, f->output_data.x->normal_gc, color_last);
540 XFillRectangle (display, window, f->output_data.x->normal_gc,
541 x1 - 1, y0, 1, y1 - y0);
543 else if (x1 - x0 > y1 - y0 && y1 - y0 > 3)
544 /* Horizontal. */
546 XSetForeground (display, f->output_data.x->normal_gc, color_first);
547 XFillRectangle (display, window, f->output_data.x->normal_gc,
548 x0, y0, x1 - x0, 1);
549 XSetForeground (display, f->output_data.x->normal_gc, color);
550 XFillRectangle (display, window, f->output_data.x->normal_gc,
551 x0, y0 + 1, x1 - x0, y1 - y0 - 2);
552 XSetForeground (display, f->output_data.x->normal_gc, color_last);
553 XFillRectangle (display, window, f->output_data.x->normal_gc,
554 x0, y1 - 1, x1 - x0, 1);
556 else
558 XSetForeground (display, f->output_data.x->normal_gc, color);
559 XFillRectangle (display, window, f->output_data.x->normal_gc,
560 x0, y0, x1 - x0, y1 - y0);
564 /* End update of window W.
566 Draw vertical borders between horizontally adjacent windows, and
567 display W's cursor if CURSOR_ON_P is non-zero.
569 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
570 glyphs in mouse-face were overwritten. In that case we have to
571 make sure that the mouse-highlight is properly redrawn.
573 W may be a menu bar pseudo-window in case we don't have X toolkit
574 support. Such windows don't have a cursor, so don't display it
575 here. */
577 static void
578 x_update_window_end (struct window *w, bool cursor_on_p,
579 bool mouse_face_overwritten_p)
581 if (!w->pseudo_window_p)
583 block_input ();
585 if (cursor_on_p)
586 display_and_set_cursor (w, 1,
587 w->output_cursor.hpos, w->output_cursor.vpos,
588 w->output_cursor.x, w->output_cursor.y);
590 if (draw_window_fringes (w, 1))
592 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
593 x_draw_right_divider (w);
594 else
595 x_draw_vertical_border (w);
598 unblock_input ();
601 /* If a row with mouse-face was overwritten, arrange for
602 XTframe_up_to_date to redisplay the mouse highlight. */
603 if (mouse_face_overwritten_p)
605 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
607 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
608 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
609 hlinfo->mouse_face_window = Qnil;
614 /* End update of frame F. This function is installed as a hook in
615 update_end. */
617 static void
618 x_update_end (struct frame *f)
620 /* Mouse highlight may be displayed again. */
621 MOUSE_HL_INFO (f)->mouse_face_defer = 0;
623 #ifndef XFlush
624 block_input ();
625 XFlush (FRAME_X_DISPLAY (f));
626 unblock_input ();
627 #endif
631 /* This function is called from various places in xdisp.c
632 whenever a complete update has been performed. */
634 static void
635 XTframe_up_to_date (struct frame *f)
637 if (FRAME_X_P (f))
638 FRAME_MOUSE_UPDATE (f);
642 /* Clear under internal border if any (GTK has its own version). */
643 #ifndef USE_GTK
644 void
645 x_clear_under_internal_border (struct frame *f)
647 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
649 Display *display = FRAME_X_DISPLAY (f);
650 Window window = FRAME_X_WINDOW (f);
651 int border = FRAME_INTERNAL_BORDER_WIDTH (f);
652 int width = FRAME_PIXEL_WIDTH (f);
653 int height = FRAME_PIXEL_HEIGHT (f);
654 int margin = FRAME_TOP_MARGIN_HEIGHT (f);
656 block_input ();
657 x_clear_area (display, window, 0, 0, border, height);
658 x_clear_area (display, window, 0, margin, width, border);
659 x_clear_area (display, window, width - border, 0, border, height);
660 x_clear_area (display, window, 0, height - border, width, border);
661 unblock_input ();
664 #endif
666 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
667 arrow bitmaps, or clear the fringes if no bitmaps are required
668 before DESIRED_ROW is made current. This function is called from
669 update_window_line only if it is known that there are differences
670 between bitmaps to be drawn between current row and DESIRED_ROW. */
672 static void
673 x_after_update_window_line (struct window *w, struct glyph_row *desired_row)
675 eassert (w);
677 if (!desired_row->mode_line_p && !w->pseudo_window_p)
678 desired_row->redraw_fringe_bitmaps_p = 1;
680 #ifdef USE_X_TOOLKIT
681 /* When a window has disappeared, make sure that no rest of
682 full-width rows stays visible in the internal border. Could
683 check here if updated window is the leftmost/rightmost window,
684 but I guess it's not worth doing since vertically split windows
685 are almost never used, internal border is rarely set, and the
686 overhead is very small. */
688 struct frame *f;
689 int width, height;
691 if (windows_or_buffers_changed
692 && desired_row->full_width_p
693 && (f = XFRAME (w->frame),
694 width = FRAME_INTERNAL_BORDER_WIDTH (f),
695 width != 0)
696 && (height = desired_row->visible_height,
697 height > 0))
699 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
701 block_input ();
702 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
703 0, y, width, height);
704 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
705 FRAME_PIXEL_WIDTH (f) - width,
706 y, width, height);
707 unblock_input ();
710 #endif
713 static void
714 x_draw_fringe_bitmap (struct window *w, struct glyph_row *row, struct draw_fringe_bitmap_params *p)
716 struct frame *f = XFRAME (WINDOW_FRAME (w));
717 Display *display = FRAME_X_DISPLAY (f);
718 Window window = FRAME_X_WINDOW (f);
719 GC gc = f->output_data.x->normal_gc;
720 struct face *face = p->face;
722 /* Must clip because of partially visible lines. */
723 x_clip_to_row (w, row, ANY_AREA, gc);
725 if (p->bx >= 0 && !p->overlay_p)
727 /* In case the same realized face is used for fringes and
728 for something displayed in the text (e.g. face `region' on
729 mono-displays, the fill style may have been changed to
730 FillSolid in x_draw_glyph_string_background. */
731 if (face->stipple)
732 XSetFillStyle (display, face->gc, FillOpaqueStippled);
733 else
734 XSetForeground (display, face->gc, face->background);
736 XFillRectangle (display, window, face->gc,
737 p->bx, p->by, p->nx, p->ny);
739 if (!face->stipple)
740 XSetForeground (display, face->gc, face->foreground);
743 if (p->which)
745 char *bits;
746 Pixmap pixmap, clipmask = (Pixmap) 0;
747 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
748 XGCValues gcv;
750 if (p->wd > 8)
751 bits = (char *) (p->bits + p->dh);
752 else
753 bits = (char *) p->bits + p->dh;
755 /* Draw the bitmap. I believe these small pixmaps can be cached
756 by the server. */
757 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
758 (p->cursor_p
759 ? (p->overlay_p ? face->background
760 : f->output_data.x->cursor_pixel)
761 : face->foreground),
762 face->background, depth);
764 if (p->overlay_p)
766 clipmask = XCreatePixmapFromBitmapData (display,
767 FRAME_DISPLAY_INFO (f)->root_window,
768 bits, p->wd, p->h,
769 1, 0, 1);
770 gcv.clip_mask = clipmask;
771 gcv.clip_x_origin = p->x;
772 gcv.clip_y_origin = p->y;
773 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
776 XCopyArea (display, pixmap, window, gc, 0, 0,
777 p->wd, p->h, p->x, p->y);
778 XFreePixmap (display, pixmap);
780 if (p->overlay_p)
782 gcv.clip_mask = (Pixmap) 0;
783 XChangeGC (display, gc, GCClipMask, &gcv);
784 XFreePixmap (display, clipmask);
788 XSetClipMask (display, gc, None);
791 /***********************************************************************
792 Glyph display
793 ***********************************************************************/
797 static void x_set_glyph_string_clipping (struct glyph_string *);
798 static void x_set_glyph_string_gc (struct glyph_string *);
799 static void x_draw_glyph_string_foreground (struct glyph_string *);
800 static void x_draw_composite_glyph_string_foreground (struct glyph_string *);
801 static void x_draw_glyph_string_box (struct glyph_string *);
802 static void x_draw_glyph_string (struct glyph_string *);
803 static _Noreturn void x_delete_glyphs (struct frame *, int);
804 static void x_compute_glyph_string_overhangs (struct glyph_string *);
805 static void x_set_cursor_gc (struct glyph_string *);
806 static void x_set_mode_line_face_gc (struct glyph_string *);
807 static void x_set_mouse_face_gc (struct glyph_string *);
808 static bool x_alloc_lighter_color (struct frame *, Display *, Colormap,
809 unsigned long *, double, int);
810 static void x_setup_relief_color (struct frame *, struct relief *,
811 double, int, unsigned long);
812 static void x_setup_relief_colors (struct glyph_string *);
813 static void x_draw_image_glyph_string (struct glyph_string *);
814 static void x_draw_image_relief (struct glyph_string *);
815 static void x_draw_image_foreground (struct glyph_string *);
816 static void x_draw_image_foreground_1 (struct glyph_string *, Pixmap);
817 static void x_clear_glyph_string_rect (struct glyph_string *, int,
818 int, int, int);
819 static void x_draw_relief_rect (struct frame *, int, int, int, int,
820 int, int, int, int, int, int,
821 XRectangle *);
822 static void x_draw_box_rect (struct glyph_string *, int, int, int, int,
823 int, int, int, XRectangle *);
824 static void x_scroll_bar_clear (struct frame *);
826 #ifdef GLYPH_DEBUG
827 static void x_check_font (struct frame *, struct font *);
828 #endif
831 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
832 face. */
834 static void
835 x_set_cursor_gc (struct glyph_string *s)
837 if (s->font == FRAME_FONT (s->f)
838 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
839 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
840 && !s->cmp)
841 s->gc = s->f->output_data.x->cursor_gc;
842 else
844 /* Cursor on non-default face: must merge. */
845 XGCValues xgcv;
846 unsigned long mask;
848 xgcv.background = s->f->output_data.x->cursor_pixel;
849 xgcv.foreground = s->face->background;
851 /* If the glyph would be invisible, try a different foreground. */
852 if (xgcv.foreground == xgcv.background)
853 xgcv.foreground = s->face->foreground;
854 if (xgcv.foreground == xgcv.background)
855 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
856 if (xgcv.foreground == xgcv.background)
857 xgcv.foreground = s->face->foreground;
859 /* Make sure the cursor is distinct from text in this face. */
860 if (xgcv.background == s->face->background
861 && xgcv.foreground == s->face->foreground)
863 xgcv.background = s->face->foreground;
864 xgcv.foreground = s->face->background;
867 IF_DEBUG (x_check_font (s->f, s->font));
868 xgcv.graphics_exposures = False;
869 mask = GCForeground | GCBackground | GCGraphicsExposures;
871 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
872 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
873 mask, &xgcv);
874 else
875 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
876 = XCreateGC (s->display, s->window, mask, &xgcv);
878 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
883 /* Set up S->gc of glyph string S for drawing text in mouse face. */
885 static void
886 x_set_mouse_face_gc (struct glyph_string *s)
888 int face_id;
889 struct face *face;
891 /* What face has to be used last for the mouse face? */
892 face_id = MOUSE_HL_INFO (s->f)->mouse_face_face_id;
893 face = FACE_FROM_ID (s->f, face_id);
894 if (face == NULL)
895 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
897 if (s->first_glyph->type == CHAR_GLYPH)
898 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
899 else
900 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
901 s->face = FACE_FROM_ID (s->f, face_id);
902 prepare_face_for_display (s->f, s->face);
904 if (s->font == s->face->font)
905 s->gc = s->face->gc;
906 else
908 /* Otherwise construct scratch_cursor_gc with values from FACE
909 except for FONT. */
910 XGCValues xgcv;
911 unsigned long mask;
913 xgcv.background = s->face->background;
914 xgcv.foreground = s->face->foreground;
915 xgcv.graphics_exposures = False;
916 mask = GCForeground | GCBackground | GCGraphicsExposures;
918 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
919 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
920 mask, &xgcv);
921 else
922 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
923 = XCreateGC (s->display, s->window, mask, &xgcv);
925 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
928 eassert (s->gc != 0);
932 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
933 Faces to use in the mode line have already been computed when the
934 matrix was built, so there isn't much to do, here. */
936 static void
937 x_set_mode_line_face_gc (struct glyph_string *s)
939 s->gc = s->face->gc;
943 /* Set S->gc of glyph string S for drawing that glyph string. Set
944 S->stippled_p to a non-zero value if the face of S has a stipple
945 pattern. */
947 static void
948 x_set_glyph_string_gc (struct glyph_string *s)
950 prepare_face_for_display (s->f, s->face);
952 if (s->hl == DRAW_NORMAL_TEXT)
954 s->gc = s->face->gc;
955 s->stippled_p = s->face->stipple != 0;
957 else if (s->hl == DRAW_INVERSE_VIDEO)
959 x_set_mode_line_face_gc (s);
960 s->stippled_p = s->face->stipple != 0;
962 else if (s->hl == DRAW_CURSOR)
964 x_set_cursor_gc (s);
965 s->stippled_p = 0;
967 else if (s->hl == DRAW_MOUSE_FACE)
969 x_set_mouse_face_gc (s);
970 s->stippled_p = s->face->stipple != 0;
972 else if (s->hl == DRAW_IMAGE_RAISED
973 || s->hl == DRAW_IMAGE_SUNKEN)
975 s->gc = s->face->gc;
976 s->stippled_p = s->face->stipple != 0;
978 else
979 emacs_abort ();
981 /* GC must have been set. */
982 eassert (s->gc != 0);
986 /* Set clipping for output of glyph string S. S may be part of a mode
987 line or menu if we don't have X toolkit support. */
989 static void
990 x_set_glyph_string_clipping (struct glyph_string *s)
992 XRectangle *r = s->clip;
993 int n = get_glyph_string_clip_rects (s, r, 2);
995 if (n > 0)
996 XSetClipRectangles (s->display, s->gc, 0, 0, r, n, Unsorted);
997 s->num_clips = n;
1001 /* Set SRC's clipping for output of glyph string DST. This is called
1002 when we are drawing DST's left_overhang or right_overhang only in
1003 the area of SRC. */
1005 static void
1006 x_set_glyph_string_clipping_exactly (struct glyph_string *src, struct glyph_string *dst)
1008 XRectangle r;
1010 r.x = src->x;
1011 r.width = src->width;
1012 r.y = src->y;
1013 r.height = src->height;
1014 dst->clip[0] = r;
1015 dst->num_clips = 1;
1016 XSetClipRectangles (dst->display, dst->gc, 0, 0, &r, 1, Unsorted);
1020 /* RIF:
1021 Compute left and right overhang of glyph string S. */
1023 static void
1024 x_compute_glyph_string_overhangs (struct glyph_string *s)
1026 if (s->cmp == NULL
1027 && (s->first_glyph->type == CHAR_GLYPH
1028 || s->first_glyph->type == COMPOSITE_GLYPH))
1030 struct font_metrics metrics;
1032 if (s->first_glyph->type == CHAR_GLYPH)
1034 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1035 struct font *font = s->font;
1036 int i;
1038 for (i = 0; i < s->nchars; i++)
1039 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1040 font->driver->text_extents (font, code, s->nchars, &metrics);
1042 else
1044 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1046 composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
1048 s->right_overhang = (metrics.rbearing > metrics.width
1049 ? metrics.rbearing - metrics.width : 0);
1050 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1052 else if (s->cmp)
1054 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1055 s->left_overhang = - s->cmp->lbearing;
1060 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1062 static void
1063 x_clear_glyph_string_rect (struct glyph_string *s, int x, int y, int w, int h)
1065 XGCValues xgcv;
1066 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1067 XSetForeground (s->display, s->gc, xgcv.background);
1068 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1069 XSetForeground (s->display, s->gc, xgcv.foreground);
1073 /* Draw the background of glyph_string S. If S->background_filled_p
1074 is non-zero don't draw it. FORCE_P non-zero means draw the
1075 background even if it wouldn't be drawn normally. This is used
1076 when a string preceding S draws into the background of S, or S
1077 contains the first component of a composition. */
1079 static void
1080 x_draw_glyph_string_background (struct glyph_string *s, bool force_p)
1082 /* Nothing to do if background has already been drawn or if it
1083 shouldn't be drawn in the first place. */
1084 if (!s->background_filled_p)
1086 int box_line_width = max (s->face->box_line_width, 0);
1088 if (s->stippled_p)
1090 /* Fill background with a stipple pattern. */
1091 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1092 XFillRectangle (s->display, s->window, s->gc, s->x,
1093 s->y + box_line_width,
1094 s->background_width,
1095 s->height - 2 * box_line_width);
1096 XSetFillStyle (s->display, s->gc, FillSolid);
1097 s->background_filled_p = 1;
1099 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1100 || s->font_not_found_p
1101 || s->extends_to_end_of_line_p
1102 || force_p)
1104 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1105 s->background_width,
1106 s->height - 2 * box_line_width);
1107 s->background_filled_p = 1;
1113 /* Draw the foreground of glyph string S. */
1115 static void
1116 x_draw_glyph_string_foreground (struct glyph_string *s)
1118 int i, x;
1120 /* If first glyph of S has a left box line, start drawing the text
1121 of S to the right of that box line. */
1122 if (s->face->box != FACE_NO_BOX
1123 && s->first_glyph->left_box_line_p)
1124 x = s->x + eabs (s->face->box_line_width);
1125 else
1126 x = s->x;
1128 /* Draw characters of S as rectangles if S's font could not be
1129 loaded. */
1130 if (s->font_not_found_p)
1132 for (i = 0; i < s->nchars; ++i)
1134 struct glyph *g = s->first_glyph + i;
1135 XDrawRectangle (s->display, s->window,
1136 s->gc, x, s->y, g->pixel_width - 1,
1137 s->height - 1);
1138 x += g->pixel_width;
1141 else
1143 struct font *font = s->font;
1144 int boff = font->baseline_offset;
1145 int y;
1147 if (font->vertical_centering)
1148 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1150 y = s->ybase - boff;
1151 if (s->for_overlaps
1152 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1153 font->driver->draw (s, 0, s->nchars, x, y, 0);
1154 else
1155 font->driver->draw (s, 0, s->nchars, x, y, 1);
1156 if (s->face->overstrike)
1157 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1161 /* Draw the foreground of composite glyph string S. */
1163 static void
1164 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1166 int i, j, x;
1167 struct font *font = s->font;
1169 /* If first glyph of S has a left box line, start drawing the text
1170 of S to the right of that box line. */
1171 if (s->face && s->face->box != FACE_NO_BOX
1172 && s->first_glyph->left_box_line_p)
1173 x = s->x + eabs (s->face->box_line_width);
1174 else
1175 x = s->x;
1177 /* S is a glyph string for a composition. S->cmp_from is the index
1178 of the first character drawn for glyphs of this composition.
1179 S->cmp_from == 0 means we are drawing the very first character of
1180 this composition. */
1182 /* Draw a rectangle for the composition if the font for the very
1183 first character of the composition could not be loaded. */
1184 if (s->font_not_found_p)
1186 if (s->cmp_from == 0)
1187 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1188 s->width - 1, s->height - 1);
1190 else if (! s->first_glyph->u.cmp.automatic)
1192 int y = s->ybase;
1194 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1195 /* TAB in a composition means display glyphs with padding
1196 space on the left or right. */
1197 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1199 int xx = x + s->cmp->offsets[j * 2];
1200 int yy = y - s->cmp->offsets[j * 2 + 1];
1202 font->driver->draw (s, j, j + 1, xx, yy, 0);
1203 if (s->face->overstrike)
1204 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
1207 else
1209 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1210 Lisp_Object glyph;
1211 int y = s->ybase;
1212 int width = 0;
1214 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1216 glyph = LGSTRING_GLYPH (gstring, i);
1217 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1218 width += LGLYPH_WIDTH (glyph);
1219 else
1221 int xoff, yoff, wadjust;
1223 if (j < i)
1225 font->driver->draw (s, j, i, x, y, 0);
1226 if (s->face->overstrike)
1227 font->driver->draw (s, j, i, x + 1, y, 0);
1228 x += width;
1230 xoff = LGLYPH_XOFF (glyph);
1231 yoff = LGLYPH_YOFF (glyph);
1232 wadjust = LGLYPH_WADJUST (glyph);
1233 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1234 if (s->face->overstrike)
1235 font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff, 0);
1236 x += wadjust;
1237 j = i + 1;
1238 width = 0;
1241 if (j < i)
1243 font->driver->draw (s, j, i, x, y, 0);
1244 if (s->face->overstrike)
1245 font->driver->draw (s, j, i, x + 1, y, 0);
1251 /* Draw the foreground of glyph string S for glyphless characters. */
1253 static void
1254 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1256 struct glyph *glyph = s->first_glyph;
1257 XChar2b char2b[8];
1258 int x, i, j;
1260 /* If first glyph of S has a left box line, start drawing the text
1261 of S to the right of that box line. */
1262 if (s->face && s->face->box != FACE_NO_BOX
1263 && s->first_glyph->left_box_line_p)
1264 x = s->x + eabs (s->face->box_line_width);
1265 else
1266 x = s->x;
1268 s->char2b = char2b;
1270 for (i = 0; i < s->nchars; i++, glyph++)
1272 char buf[7], *str = NULL;
1273 int len = glyph->u.glyphless.len;
1275 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1277 if (len > 0
1278 && CHAR_TABLE_P (Vglyphless_char_display)
1279 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1280 >= 1))
1282 Lisp_Object acronym
1283 = (! glyph->u.glyphless.for_no_font
1284 ? CHAR_TABLE_REF (Vglyphless_char_display,
1285 glyph->u.glyphless.ch)
1286 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
1287 if (STRINGP (acronym))
1288 str = SSDATA (acronym);
1291 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
1293 sprintf (buf, "%0*X",
1294 glyph->u.glyphless.ch < 0x10000 ? 4 : 6,
1295 glyph->u.glyphless.ch);
1296 str = buf;
1299 if (str)
1301 int upper_len = (len + 1) / 2;
1302 unsigned code;
1304 /* It is assured that all LEN characters in STR is ASCII. */
1305 for (j = 0; j < len; j++)
1307 code = s->font->driver->encode_char (s->font, str[j]);
1308 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
1310 s->font->driver->draw (s, 0, upper_len,
1311 x + glyph->slice.glyphless.upper_xoff,
1312 s->ybase + glyph->slice.glyphless.upper_yoff,
1314 s->font->driver->draw (s, upper_len, len,
1315 x + glyph->slice.glyphless.lower_xoff,
1316 s->ybase + glyph->slice.glyphless.lower_yoff,
1319 if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
1320 XDrawRectangle (s->display, s->window, s->gc,
1321 x, s->ybase - glyph->ascent,
1322 glyph->pixel_width - 1,
1323 glyph->ascent + glyph->descent - 1);
1324 x += glyph->pixel_width;
1328 #ifdef USE_X_TOOLKIT
1330 #ifdef USE_LUCID
1332 /* Return the frame on which widget WIDGET is used.. Abort if frame
1333 cannot be determined. */
1335 static struct frame *
1336 x_frame_of_widget (Widget widget)
1338 struct x_display_info *dpyinfo;
1339 Lisp_Object tail, frame;
1340 struct frame *f;
1342 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1344 /* Find the top-level shell of the widget. Note that this function
1345 can be called when the widget is not yet realized, so XtWindow
1346 (widget) == 0. That's the reason we can't simply use
1347 x_any_window_to_frame. */
1348 while (!XtIsTopLevelShell (widget))
1349 widget = XtParent (widget);
1351 /* Look for a frame with that top-level widget. Allocate the color
1352 on that frame to get the right gamma correction value. */
1353 FOR_EACH_FRAME (tail, frame)
1355 f = XFRAME (frame);
1356 if (FRAME_X_P (f)
1357 && f->output_data.nothing != 1
1358 && FRAME_DISPLAY_INFO (f) == dpyinfo
1359 && f->output_data.x->widget == widget)
1360 return f;
1362 emacs_abort ();
1365 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1366 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1367 If this produces the same color as PIXEL, try a color where all RGB
1368 values have DELTA added. Return the allocated color in *PIXEL.
1369 DISPLAY is the X display, CMAP is the colormap to operate on.
1370 Value is true if successful. */
1372 bool
1373 x_alloc_lighter_color_for_widget (Widget widget, Display *display, Colormap cmap,
1374 unsigned long *pixel, double factor, int delta)
1376 struct frame *f = x_frame_of_widget (widget);
1377 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1380 #endif /* USE_LUCID */
1383 /* Structure specifying which arguments should be passed by Xt to
1384 cvt_string_to_pixel. We want the widget's screen and colormap. */
1386 static XtConvertArgRec cvt_string_to_pixel_args[] =
1388 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1389 sizeof (Screen *)},
1390 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1391 sizeof (Colormap)}
1395 /* The address of this variable is returned by
1396 cvt_string_to_pixel. */
1398 static Pixel cvt_string_to_pixel_value;
1401 /* Convert a color name to a pixel color.
1403 DPY is the display we are working on.
1405 ARGS is an array of *NARGS XrmValue structures holding additional
1406 information about the widget for which the conversion takes place.
1407 The contents of this array are determined by the specification
1408 in cvt_string_to_pixel_args.
1410 FROM is a pointer to an XrmValue which points to the color name to
1411 convert. TO is an XrmValue in which to return the pixel color.
1413 CLOSURE_RET is a pointer to user-data, in which we record if
1414 we allocated the color or not.
1416 Value is True if successful, False otherwise. */
1418 static Boolean
1419 cvt_string_to_pixel (Display *dpy, XrmValue *args, Cardinal *nargs,
1420 XrmValue *from, XrmValue *to,
1421 XtPointer *closure_ret)
1423 Screen *screen;
1424 Colormap cmap;
1425 Pixel pixel;
1426 String color_name;
1427 XColor color;
1429 if (*nargs != 2)
1431 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1432 "wrongParameters", "cvt_string_to_pixel",
1433 "XtToolkitError",
1434 "Screen and colormap args required", NULL, NULL);
1435 return False;
1438 screen = *(Screen **) args[0].addr;
1439 cmap = *(Colormap *) args[1].addr;
1440 color_name = (String) from->addr;
1442 if (strcmp (color_name, XtDefaultBackground) == 0)
1444 *closure_ret = (XtPointer) False;
1445 pixel = WhitePixelOfScreen (screen);
1447 else if (strcmp (color_name, XtDefaultForeground) == 0)
1449 *closure_ret = (XtPointer) False;
1450 pixel = BlackPixelOfScreen (screen);
1452 else if (XParseColor (dpy, cmap, color_name, &color)
1453 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1455 pixel = color.pixel;
1456 *closure_ret = (XtPointer) True;
1458 else
1460 String params[1];
1461 Cardinal nparams = 1;
1463 params[0] = color_name;
1464 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1465 "badValue", "cvt_string_to_pixel",
1466 "XtToolkitError", "Invalid color `%s'",
1467 params, &nparams);
1468 return False;
1471 if (to->addr != NULL)
1473 if (to->size < sizeof (Pixel))
1475 to->size = sizeof (Pixel);
1476 return False;
1479 *(Pixel *) to->addr = pixel;
1481 else
1483 cvt_string_to_pixel_value = pixel;
1484 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1487 to->size = sizeof (Pixel);
1488 return True;
1492 /* Free a pixel color which was previously allocated via
1493 cvt_string_to_pixel. This is registered as the destructor
1494 for this type of resource via XtSetTypeConverter.
1496 APP is the application context in which we work.
1498 TO is a pointer to an XrmValue holding the color to free.
1499 CLOSURE is the value we stored in CLOSURE_RET for this color
1500 in cvt_string_to_pixel.
1502 ARGS and NARGS are like for cvt_string_to_pixel. */
1504 static void
1505 cvt_pixel_dtor (XtAppContext app, XrmValuePtr to, XtPointer closure, XrmValuePtr args,
1506 Cardinal *nargs)
1508 if (*nargs != 2)
1510 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1511 "XtToolkitError",
1512 "Screen and colormap arguments required",
1513 NULL, NULL);
1515 else if (closure != NULL)
1517 /* We did allocate the pixel, so free it. */
1518 Screen *screen = *(Screen **) args[0].addr;
1519 Colormap cmap = *(Colormap *) args[1].addr;
1520 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1521 (Pixel *) to->addr, 1);
1526 #endif /* USE_X_TOOLKIT */
1529 /* Value is an array of XColor structures for the contents of the
1530 color map of display DPY. Set *NCELLS to the size of the array.
1531 Note that this probably shouldn't be called for large color maps,
1532 say a 24-bit TrueColor map. */
1534 static const XColor *
1535 x_color_cells (Display *dpy, int *ncells)
1537 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1539 if (dpyinfo->color_cells == NULL)
1541 Screen *screen = dpyinfo->screen;
1542 int ncolor_cells = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1543 int i;
1545 dpyinfo->color_cells = xnmalloc (ncolor_cells,
1546 sizeof *dpyinfo->color_cells);
1547 dpyinfo->ncolor_cells = ncolor_cells;
1549 for (i = 0; i < ncolor_cells; ++i)
1550 dpyinfo->color_cells[i].pixel = i;
1552 XQueryColors (dpy, dpyinfo->cmap,
1553 dpyinfo->color_cells, ncolor_cells);
1556 *ncells = dpyinfo->ncolor_cells;
1557 return dpyinfo->color_cells;
1561 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1562 colors in COLORS. Use cached information, if available. */
1564 void
1565 x_query_colors (struct frame *f, XColor *colors, int ncolors)
1567 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1569 if (dpyinfo->color_cells)
1571 int i;
1572 for (i = 0; i < ncolors; ++i)
1574 unsigned long pixel = colors[i].pixel;
1575 eassert (pixel < dpyinfo->ncolor_cells);
1576 eassert (dpyinfo->color_cells[pixel].pixel == pixel);
1577 colors[i] = dpyinfo->color_cells[pixel];
1580 else
1581 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1585 /* On frame F, translate pixel color to RGB values for the color in
1586 COLOR. Use cached information, if available. */
1588 void
1589 x_query_color (struct frame *f, XColor *color)
1591 x_query_colors (f, color, 1);
1595 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1596 exact match can't be allocated, try the nearest color available.
1597 Value is true if successful. Set *COLOR to the color
1598 allocated. */
1600 static bool
1601 x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color)
1603 bool rc;
1605 rc = XAllocColor (dpy, cmap, color) != 0;
1606 if (rc == 0)
1608 /* If we got to this point, the colormap is full, so we're going
1609 to try to get the next closest color. The algorithm used is
1610 a least-squares matching, which is what X uses for closest
1611 color matching with StaticColor visuals. */
1612 int nearest, i;
1613 int max_color_delta = 255;
1614 int max_delta = 3 * max_color_delta;
1615 int nearest_delta = max_delta + 1;
1616 int ncells;
1617 const XColor *cells = x_color_cells (dpy, &ncells);
1619 for (nearest = i = 0; i < ncells; ++i)
1621 int dred = (color->red >> 8) - (cells[i].red >> 8);
1622 int dgreen = (color->green >> 8) - (cells[i].green >> 8);
1623 int dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1624 int delta = dred * dred + dgreen * dgreen + dblue * dblue;
1626 if (delta < nearest_delta)
1628 nearest = i;
1629 nearest_delta = delta;
1633 color->red = cells[nearest].red;
1634 color->green = cells[nearest].green;
1635 color->blue = cells[nearest].blue;
1636 rc = XAllocColor (dpy, cmap, color) != 0;
1638 else
1640 /* If allocation succeeded, and the allocated pixel color is not
1641 equal to a cached pixel color recorded earlier, there was a
1642 change in the colormap, so clear the color cache. */
1643 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1644 XColor *cached_color;
1646 if (dpyinfo->color_cells
1647 && (cached_color = &dpyinfo->color_cells[color->pixel],
1648 (cached_color->red != color->red
1649 || cached_color->blue != color->blue
1650 || cached_color->green != color->green)))
1652 xfree (dpyinfo->color_cells);
1653 dpyinfo->color_cells = NULL;
1654 dpyinfo->ncolor_cells = 0;
1658 #ifdef DEBUG_X_COLORS
1659 if (rc)
1660 register_color (color->pixel);
1661 #endif /* DEBUG_X_COLORS */
1663 return rc;
1667 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1668 exact match can't be allocated, try the nearest color available.
1669 Value is true if successful. Set *COLOR to the color
1670 allocated. */
1672 bool
1673 x_alloc_nearest_color (struct frame *f, Colormap cmap, XColor *color)
1675 gamma_correct (f, color);
1676 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1680 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1681 It's necessary to do this instead of just using PIXEL directly to
1682 get color reference counts right. */
1684 unsigned long
1685 x_copy_color (struct frame *f, unsigned long pixel)
1687 XColor color;
1689 color.pixel = pixel;
1690 block_input ();
1691 x_query_color (f, &color);
1692 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1693 unblock_input ();
1694 #ifdef DEBUG_X_COLORS
1695 register_color (pixel);
1696 #endif
1697 return color.pixel;
1701 /* Brightness beyond which a color won't have its highlight brightness
1702 boosted.
1704 Nominally, highlight colors for `3d' faces are calculated by
1705 brightening an object's color by a constant scale factor, but this
1706 doesn't yield good results for dark colors, so for colors who's
1707 brightness is less than this value (on a scale of 0-65535) have an
1708 use an additional additive factor.
1710 The value here is set so that the default menu-bar/mode-line color
1711 (grey75) will not have its highlights changed at all. */
1712 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1715 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1716 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1717 If this produces the same color as PIXEL, try a color where all RGB
1718 values have DELTA added. Return the allocated color in *PIXEL.
1719 DISPLAY is the X display, CMAP is the colormap to operate on.
1720 Value is non-zero if successful. */
1722 static bool
1723 x_alloc_lighter_color (struct frame *f, Display *display, Colormap cmap,
1724 unsigned long *pixel, double factor, int delta)
1726 XColor color, new;
1727 long bright;
1728 bool success_p;
1730 /* Get RGB color values. */
1731 color.pixel = *pixel;
1732 x_query_color (f, &color);
1734 /* Change RGB values by specified FACTOR. Avoid overflow! */
1735 eassert (factor >= 0);
1736 new.red = min (0xffff, factor * color.red);
1737 new.green = min (0xffff, factor * color.green);
1738 new.blue = min (0xffff, factor * color.blue);
1740 /* Calculate brightness of COLOR. */
1741 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1743 /* We only boost colors that are darker than
1744 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1745 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1746 /* Make an additive adjustment to NEW, because it's dark enough so
1747 that scaling by FACTOR alone isn't enough. */
1749 /* How far below the limit this color is (0 - 1, 1 being darker). */
1750 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1751 /* The additive adjustment. */
1752 int min_delta = delta * dimness * factor / 2;
1754 if (factor < 1)
1756 new.red = max (0, new.red - min_delta);
1757 new.green = max (0, new.green - min_delta);
1758 new.blue = max (0, new.blue - min_delta);
1760 else
1762 new.red = min (0xffff, min_delta + new.red);
1763 new.green = min (0xffff, min_delta + new.green);
1764 new.blue = min (0xffff, min_delta + new.blue);
1768 /* Try to allocate the color. */
1769 success_p = x_alloc_nearest_color (f, cmap, &new);
1770 if (success_p)
1772 if (new.pixel == *pixel)
1774 /* If we end up with the same color as before, try adding
1775 delta to the RGB values. */
1776 x_free_colors (f, &new.pixel, 1);
1778 new.red = min (0xffff, delta + color.red);
1779 new.green = min (0xffff, delta + color.green);
1780 new.blue = min (0xffff, delta + color.blue);
1781 success_p = x_alloc_nearest_color (f, cmap, &new);
1783 else
1784 success_p = 1;
1785 *pixel = new.pixel;
1788 return success_p;
1792 /* Set up the foreground color for drawing relief lines of glyph
1793 string S. RELIEF is a pointer to a struct relief containing the GC
1794 with which lines will be drawn. Use a color that is FACTOR or
1795 DELTA lighter or darker than the relief's background which is found
1796 in S->f->output_data.x->relief_background. If such a color cannot
1797 be allocated, use DEFAULT_PIXEL, instead. */
1799 static void
1800 x_setup_relief_color (struct frame *f, struct relief *relief, double factor,
1801 int delta, unsigned long default_pixel)
1803 XGCValues xgcv;
1804 struct x_output *di = f->output_data.x;
1805 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1806 unsigned long pixel;
1807 unsigned long background = di->relief_background;
1808 Colormap cmap = FRAME_X_COLORMAP (f);
1809 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1810 Display *dpy = FRAME_X_DISPLAY (f);
1812 xgcv.graphics_exposures = False;
1813 xgcv.line_width = 1;
1815 /* Free previously allocated color. The color cell will be reused
1816 when it has been freed as many times as it was allocated, so this
1817 doesn't affect faces using the same colors. */
1818 if (relief->gc && relief->pixel != -1)
1820 x_free_colors (f, &relief->pixel, 1);
1821 relief->pixel = -1;
1824 /* Allocate new color. */
1825 xgcv.foreground = default_pixel;
1826 pixel = background;
1827 if (dpyinfo->n_planes != 1
1828 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1829 xgcv.foreground = relief->pixel = pixel;
1831 if (relief->gc == 0)
1833 xgcv.stipple = dpyinfo->gray;
1834 mask |= GCStipple;
1835 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1837 else
1838 XChangeGC (dpy, relief->gc, mask, &xgcv);
1842 /* Set up colors for the relief lines around glyph string S. */
1844 static void
1845 x_setup_relief_colors (struct glyph_string *s)
1847 struct x_output *di = s->f->output_data.x;
1848 unsigned long color;
1850 if (s->face->use_box_color_for_shadows_p)
1851 color = s->face->box_color;
1852 else if (s->first_glyph->type == IMAGE_GLYPH
1853 && s->img->pixmap
1854 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1855 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1856 else
1858 XGCValues xgcv;
1860 /* Get the background color of the face. */
1861 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
1862 color = xgcv.background;
1865 if (di->white_relief.gc == 0
1866 || color != di->relief_background)
1868 di->relief_background = color;
1869 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1870 WHITE_PIX_DEFAULT (s->f));
1871 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1872 BLACK_PIX_DEFAULT (s->f));
1877 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
1878 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
1879 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
1880 relief. LEFT_P non-zero means draw a relief on the left side of
1881 the rectangle. RIGHT_P non-zero means draw a relief on the right
1882 side of the rectangle. CLIP_RECT is the clipping rectangle to use
1883 when drawing. */
1885 static void
1886 x_draw_relief_rect (struct frame *f,
1887 int left_x, int top_y, int right_x, int bottom_y, int width,
1888 int raised_p, int top_p, int bot_p, int left_p, int right_p,
1889 XRectangle *clip_rect)
1891 Display *dpy = FRAME_X_DISPLAY (f);
1892 Window window = FRAME_X_WINDOW (f);
1893 int i;
1894 GC gc;
1896 if (raised_p)
1897 gc = f->output_data.x->white_relief.gc;
1898 else
1899 gc = f->output_data.x->black_relief.gc;
1900 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
1902 /* This code is more complicated than it has to be, because of two
1903 minor hacks to make the boxes look nicer: (i) if width > 1, draw
1904 the outermost line using the black relief. (ii) Omit the four
1905 corner pixels. */
1907 /* Top. */
1908 if (top_p)
1910 if (width == 1)
1911 XDrawLine (dpy, window, gc,
1912 left_x + (left_p ? 1 : 0), top_y,
1913 right_x + (right_p ? 0 : 1), top_y);
1915 for (i = 1; i < width; ++i)
1916 XDrawLine (dpy, window, gc,
1917 left_x + i * left_p, top_y + i,
1918 right_x + 1 - i * right_p, top_y + i);
1921 /* Left. */
1922 if (left_p)
1924 if (width == 1)
1925 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
1927 XClearArea (dpy, window, left_x, top_y, 1, 1, False);
1928 XClearArea (dpy, window, left_x, bottom_y, 1, 1, False);
1930 for (i = (width > 1 ? 1 : 0); i < width; ++i)
1931 XDrawLine (dpy, window, gc,
1932 left_x + i, top_y + (i + 1) * top_p,
1933 left_x + i, bottom_y + 1 - (i + 1) * bot_p);
1936 XSetClipMask (dpy, gc, None);
1937 if (raised_p)
1938 gc = f->output_data.x->black_relief.gc;
1939 else
1940 gc = f->output_data.x->white_relief.gc;
1941 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
1943 if (width > 1)
1945 /* Outermost top line. */
1946 if (top_p)
1947 XDrawLine (dpy, window, gc,
1948 left_x + (left_p ? 1 : 0), top_y,
1949 right_x + (right_p ? 0 : 1), top_y);
1951 /* Outermost left line. */
1952 if (left_p)
1953 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
1956 /* Bottom. */
1957 if (bot_p)
1959 XDrawLine (dpy, window, gc,
1960 left_x + (left_p ? 1 : 0), bottom_y,
1961 right_x + (right_p ? 0 : 1), bottom_y);
1962 for (i = 1; i < width; ++i)
1963 XDrawLine (dpy, window, gc,
1964 left_x + i * left_p, bottom_y - i,
1965 right_x + 1 - i * right_p, bottom_y - i);
1968 /* Right. */
1969 if (right_p)
1971 XClearArea (dpy, window, right_x, top_y, 1, 1, False);
1972 XClearArea (dpy, window, right_x, bottom_y, 1, 1, False);
1973 for (i = 0; i < width; ++i)
1974 XDrawLine (dpy, window, gc,
1975 right_x - i, top_y + (i + 1) * top_p,
1976 right_x - i, bottom_y + 1 - (i + 1) * bot_p);
1979 XSetClipMask (dpy, gc, None);
1983 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
1984 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
1985 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
1986 left side of the rectangle. RIGHT_P non-zero means draw a line
1987 on the right side of the rectangle. CLIP_RECT is the clipping
1988 rectangle to use when drawing. */
1990 static void
1991 x_draw_box_rect (struct glyph_string *s,
1992 int left_x, int top_y, int right_x, int bottom_y, int width,
1993 int left_p, int right_p, XRectangle *clip_rect)
1995 XGCValues xgcv;
1997 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
1998 XSetForeground (s->display, s->gc, s->face->box_color);
1999 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2001 /* Top. */
2002 XFillRectangle (s->display, s->window, s->gc,
2003 left_x, top_y, right_x - left_x + 1, width);
2005 /* Left. */
2006 if (left_p)
2007 XFillRectangle (s->display, s->window, s->gc,
2008 left_x, top_y, width, bottom_y - top_y + 1);
2010 /* Bottom. */
2011 XFillRectangle (s->display, s->window, s->gc,
2012 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2014 /* Right. */
2015 if (right_p)
2016 XFillRectangle (s->display, s->window, s->gc,
2017 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2019 XSetForeground (s->display, s->gc, xgcv.foreground);
2020 XSetClipMask (s->display, s->gc, None);
2024 /* Draw a box around glyph string S. */
2026 static void
2027 x_draw_glyph_string_box (struct glyph_string *s)
2029 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2030 int left_p, right_p;
2031 struct glyph *last_glyph;
2032 XRectangle clip_rect;
2034 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2035 ? WINDOW_RIGHT_EDGE_X (s->w)
2036 : window_box_right (s->w, s->area));
2038 /* The glyph that may have a right box line. */
2039 last_glyph = (s->cmp || s->img
2040 ? s->first_glyph
2041 : s->first_glyph + s->nchars - 1);
2043 width = eabs (s->face->box_line_width);
2044 raised_p = s->face->box == FACE_RAISED_BOX;
2045 left_x = s->x;
2046 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2047 ? last_x - 1
2048 : min (last_x, s->x + s->background_width) - 1);
2049 top_y = s->y;
2050 bottom_y = top_y + s->height - 1;
2052 left_p = (s->first_glyph->left_box_line_p
2053 || (s->hl == DRAW_MOUSE_FACE
2054 && (s->prev == NULL
2055 || s->prev->hl != s->hl)));
2056 right_p = (last_glyph->right_box_line_p
2057 || (s->hl == DRAW_MOUSE_FACE
2058 && (s->next == NULL
2059 || s->next->hl != s->hl)));
2061 get_glyph_string_clip_rect (s, &clip_rect);
2063 if (s->face->box == FACE_SIMPLE_BOX)
2064 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2065 left_p, right_p, &clip_rect);
2066 else
2068 x_setup_relief_colors (s);
2069 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2070 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2075 /* Draw foreground of image glyph string S. */
2077 static void
2078 x_draw_image_foreground (struct glyph_string *s)
2080 int x = s->x;
2081 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2083 /* If first glyph of S has a left box line, start drawing it to the
2084 right of that line. */
2085 if (s->face->box != FACE_NO_BOX
2086 && s->first_glyph->left_box_line_p
2087 && s->slice.x == 0)
2088 x += eabs (s->face->box_line_width);
2090 /* If there is a margin around the image, adjust x- and y-position
2091 by that margin. */
2092 if (s->slice.x == 0)
2093 x += s->img->hmargin;
2094 if (s->slice.y == 0)
2095 y += s->img->vmargin;
2097 if (s->img->pixmap)
2099 if (s->img->mask)
2101 /* We can't set both a clip mask and use XSetClipRectangles
2102 because the latter also sets a clip mask. We also can't
2103 trust on the shape extension to be available
2104 (XShapeCombineRegion). So, compute the rectangle to draw
2105 manually. */
2106 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2107 | GCFunction);
2108 XGCValues xgcv;
2109 XRectangle clip_rect, image_rect, r;
2111 xgcv.clip_mask = s->img->mask;
2112 xgcv.clip_x_origin = x;
2113 xgcv.clip_y_origin = y;
2114 xgcv.function = GXcopy;
2115 XChangeGC (s->display, s->gc, mask, &xgcv);
2117 get_glyph_string_clip_rect (s, &clip_rect);
2118 image_rect.x = x;
2119 image_rect.y = y;
2120 image_rect.width = s->slice.width;
2121 image_rect.height = s->slice.height;
2122 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2123 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2124 s->slice.x + r.x - x, s->slice.y + r.y - y,
2125 r.width, r.height, r.x, r.y);
2127 else
2129 XRectangle clip_rect, image_rect, r;
2131 get_glyph_string_clip_rect (s, &clip_rect);
2132 image_rect.x = x;
2133 image_rect.y = y;
2134 image_rect.width = s->slice.width;
2135 image_rect.height = s->slice.height;
2136 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2137 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2138 s->slice.x + r.x - x, s->slice.y + r.y - y,
2139 r.width, r.height, r.x, r.y);
2141 /* When the image has a mask, we can expect that at
2142 least part of a mouse highlight or a block cursor will
2143 be visible. If the image doesn't have a mask, make
2144 a block cursor visible by drawing a rectangle around
2145 the image. I believe it's looking better if we do
2146 nothing here for mouse-face. */
2147 if (s->hl == DRAW_CURSOR)
2149 int relief = eabs (s->img->relief);
2150 XDrawRectangle (s->display, s->window, s->gc,
2151 x - relief, y - relief,
2152 s->slice.width + relief*2 - 1,
2153 s->slice.height + relief*2 - 1);
2157 else
2158 /* Draw a rectangle if image could not be loaded. */
2159 XDrawRectangle (s->display, s->window, s->gc, x, y,
2160 s->slice.width - 1, s->slice.height - 1);
2164 /* Draw a relief around the image glyph string S. */
2166 static void
2167 x_draw_image_relief (struct glyph_string *s)
2169 int x1, y1, thick, raised_p, top_p, bot_p, left_p, right_p;
2170 int extra_x, extra_y;
2171 XRectangle r;
2172 int x = s->x;
2173 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2175 /* If first glyph of S has a left box line, start drawing it to the
2176 right of that line. */
2177 if (s->face->box != FACE_NO_BOX
2178 && s->first_glyph->left_box_line_p
2179 && s->slice.x == 0)
2180 x += eabs (s->face->box_line_width);
2182 /* If there is a margin around the image, adjust x- and y-position
2183 by that margin. */
2184 if (s->slice.x == 0)
2185 x += s->img->hmargin;
2186 if (s->slice.y == 0)
2187 y += s->img->vmargin;
2189 if (s->hl == DRAW_IMAGE_SUNKEN
2190 || s->hl == DRAW_IMAGE_RAISED)
2192 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2193 raised_p = s->hl == DRAW_IMAGE_RAISED;
2195 else
2197 thick = eabs (s->img->relief);
2198 raised_p = s->img->relief > 0;
2201 x1 = x + s->slice.width - 1;
2202 y1 = y + s->slice.height - 1;
2204 extra_x = extra_y = 0;
2205 if (s->face->id == TOOL_BAR_FACE_ID)
2207 if (CONSP (Vtool_bar_button_margin)
2208 && INTEGERP (XCAR (Vtool_bar_button_margin))
2209 && INTEGERP (XCDR (Vtool_bar_button_margin)))
2211 extra_x = XINT (XCAR (Vtool_bar_button_margin));
2212 extra_y = XINT (XCDR (Vtool_bar_button_margin));
2214 else if (INTEGERP (Vtool_bar_button_margin))
2215 extra_x = extra_y = XINT (Vtool_bar_button_margin);
2218 top_p = bot_p = left_p = right_p = 0;
2220 if (s->slice.x == 0)
2221 x -= thick + extra_x, left_p = 1;
2222 if (s->slice.y == 0)
2223 y -= thick + extra_y, top_p = 1;
2224 if (s->slice.x + s->slice.width == s->img->width)
2225 x1 += thick + extra_x, right_p = 1;
2226 if (s->slice.y + s->slice.height == s->img->height)
2227 y1 += thick + extra_y, bot_p = 1;
2229 x_setup_relief_colors (s);
2230 get_glyph_string_clip_rect (s, &r);
2231 x_draw_relief_rect (s->f, x, y, x1, y1, thick, raised_p,
2232 top_p, bot_p, left_p, right_p, &r);
2236 /* Draw the foreground of image glyph string S to PIXMAP. */
2238 static void
2239 x_draw_image_foreground_1 (struct glyph_string *s, Pixmap pixmap)
2241 int x = 0;
2242 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2244 /* If first glyph of S has a left box line, start drawing it to the
2245 right of that line. */
2246 if (s->face->box != FACE_NO_BOX
2247 && s->first_glyph->left_box_line_p
2248 && s->slice.x == 0)
2249 x += eabs (s->face->box_line_width);
2251 /* If there is a margin around the image, adjust x- and y-position
2252 by that margin. */
2253 if (s->slice.x == 0)
2254 x += s->img->hmargin;
2255 if (s->slice.y == 0)
2256 y += s->img->vmargin;
2258 if (s->img->pixmap)
2260 if (s->img->mask)
2262 /* We can't set both a clip mask and use XSetClipRectangles
2263 because the latter also sets a clip mask. We also can't
2264 trust on the shape extension to be available
2265 (XShapeCombineRegion). So, compute the rectangle to draw
2266 manually. */
2267 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2268 | GCFunction);
2269 XGCValues xgcv;
2271 xgcv.clip_mask = s->img->mask;
2272 xgcv.clip_x_origin = x - s->slice.x;
2273 xgcv.clip_y_origin = y - s->slice.y;
2274 xgcv.function = GXcopy;
2275 XChangeGC (s->display, s->gc, mask, &xgcv);
2277 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2278 s->slice.x, s->slice.y,
2279 s->slice.width, s->slice.height, x, y);
2280 XSetClipMask (s->display, s->gc, None);
2282 else
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);
2288 /* When the image has a mask, we can expect that at
2289 least part of a mouse highlight or a block cursor will
2290 be visible. If the image doesn't have a mask, make
2291 a block cursor visible by drawing a rectangle around
2292 the image. I believe it's looking better if we do
2293 nothing here for mouse-face. */
2294 if (s->hl == DRAW_CURSOR)
2296 int r = eabs (s->img->relief);
2297 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2298 s->slice.width + r*2 - 1,
2299 s->slice.height + r*2 - 1);
2303 else
2304 /* Draw a rectangle if image could not be loaded. */
2305 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2306 s->slice.width - 1, s->slice.height - 1);
2310 /* Draw part of the background of glyph string S. X, Y, W, and H
2311 give the rectangle to draw. */
2313 static void
2314 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
2316 if (s->stippled_p)
2318 /* Fill background with a stipple pattern. */
2319 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2320 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2321 XSetFillStyle (s->display, s->gc, FillSolid);
2323 else
2324 x_clear_glyph_string_rect (s, x, y, w, h);
2328 /* Draw image glyph string S.
2330 s->y
2331 s->x +-------------------------
2332 | s->face->box
2334 | +-------------------------
2335 | | s->img->margin
2337 | | +-------------------
2338 | | | the image
2342 static void
2343 x_draw_image_glyph_string (struct glyph_string *s)
2345 int box_line_hwidth = eabs (s->face->box_line_width);
2346 int box_line_vwidth = max (s->face->box_line_width, 0);
2347 int height;
2348 Pixmap pixmap = None;
2350 height = s->height;
2351 if (s->slice.y == 0)
2352 height -= box_line_vwidth;
2353 if (s->slice.y + s->slice.height >= s->img->height)
2354 height -= box_line_vwidth;
2356 /* Fill background with face under the image. Do it only if row is
2357 taller than image or if image has a clip mask to reduce
2358 flickering. */
2359 s->stippled_p = s->face->stipple != 0;
2360 if (height > s->slice.height
2361 || s->img->hmargin
2362 || s->img->vmargin
2363 || s->img->mask
2364 || s->img->pixmap == 0
2365 || s->width != s->background_width)
2367 if (s->img->mask)
2369 /* Create a pixmap as large as the glyph string. Fill it
2370 with the background color. Copy the image to it, using
2371 its mask. Copy the temporary pixmap to the display. */
2372 Screen *screen = FRAME_X_SCREEN (s->f);
2373 int depth = DefaultDepthOfScreen (screen);
2375 /* Create a pixmap as large as the glyph string. */
2376 pixmap = XCreatePixmap (s->display, s->window,
2377 s->background_width,
2378 s->height, depth);
2380 /* Don't clip in the following because we're working on the
2381 pixmap. */
2382 XSetClipMask (s->display, s->gc, None);
2384 /* Fill the pixmap with the background color/stipple. */
2385 if (s->stippled_p)
2387 /* Fill background with a stipple pattern. */
2388 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2389 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
2390 XFillRectangle (s->display, pixmap, s->gc,
2391 0, 0, s->background_width, s->height);
2392 XSetFillStyle (s->display, s->gc, FillSolid);
2393 XSetTSOrigin (s->display, s->gc, 0, 0);
2395 else
2397 XGCValues xgcv;
2398 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2399 &xgcv);
2400 XSetForeground (s->display, s->gc, xgcv.background);
2401 XFillRectangle (s->display, pixmap, s->gc,
2402 0, 0, s->background_width, s->height);
2403 XSetForeground (s->display, s->gc, xgcv.foreground);
2406 else
2408 int x = s->x;
2409 int y = s->y;
2410 int width = s->background_width;
2412 if (s->first_glyph->left_box_line_p
2413 && s->slice.x == 0)
2415 x += box_line_hwidth;
2416 width -= box_line_hwidth;
2419 if (s->slice.y == 0)
2420 y += box_line_vwidth;
2422 x_draw_glyph_string_bg_rect (s, x, y, width, height);
2425 s->background_filled_p = 1;
2428 /* Draw the foreground. */
2429 if (pixmap != None)
2431 x_draw_image_foreground_1 (s, pixmap);
2432 x_set_glyph_string_clipping (s);
2433 XCopyArea (s->display, pixmap, s->window, s->gc,
2434 0, 0, s->background_width, s->height, s->x, s->y);
2435 XFreePixmap (s->display, pixmap);
2437 else
2438 x_draw_image_foreground (s);
2440 /* If we must draw a relief around the image, do it. */
2441 if (s->img->relief
2442 || s->hl == DRAW_IMAGE_RAISED
2443 || s->hl == DRAW_IMAGE_SUNKEN)
2444 x_draw_image_relief (s);
2448 /* Draw stretch glyph string S. */
2450 static void
2451 x_draw_stretch_glyph_string (struct glyph_string *s)
2453 eassert (s->first_glyph->type == STRETCH_GLYPH);
2455 if (s->hl == DRAW_CURSOR
2456 && !x_stretch_cursor_p)
2458 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
2459 wide as the stretch glyph. */
2460 int width, background_width = s->background_width;
2461 int x = s->x;
2463 if (!s->row->reversed_p)
2465 int left_x = window_box_left_offset (s->w, TEXT_AREA);
2467 if (x < left_x)
2469 background_width -= left_x - x;
2470 x = left_x;
2473 else
2475 /* In R2L rows, draw the cursor on the right edge of the
2476 stretch glyph. */
2477 int right_x = window_box_right_offset (s->w, TEXT_AREA);
2479 if (x + background_width > right_x)
2480 background_width -= x - right_x;
2481 x += background_width;
2483 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2484 if (s->row->reversed_p)
2485 x -= width;
2487 /* Draw cursor. */
2488 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2490 /* Clear rest using the GC of the original non-cursor face. */
2491 if (width < background_width)
2493 int y = s->y;
2494 int w = background_width - width, h = s->height;
2495 XRectangle r;
2496 GC gc;
2498 if (!s->row->reversed_p)
2499 x += width;
2500 else
2501 x = s->x;
2502 if (s->row->mouse_face_p
2503 && cursor_in_mouse_face_p (s->w))
2505 x_set_mouse_face_gc (s);
2506 gc = s->gc;
2508 else
2509 gc = s->face->gc;
2511 get_glyph_string_clip_rect (s, &r);
2512 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2514 if (s->face->stipple)
2516 /* Fill background with a stipple pattern. */
2517 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2518 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2519 XSetFillStyle (s->display, gc, FillSolid);
2521 else
2523 XGCValues xgcv;
2524 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2525 XSetForeground (s->display, gc, xgcv.background);
2526 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2527 XSetForeground (s->display, gc, xgcv.foreground);
2530 XSetClipMask (s->display, gc, None);
2533 else if (!s->background_filled_p)
2535 int background_width = s->background_width;
2536 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2538 /* Don't draw into left margin, fringe or scrollbar area
2539 except for header line and mode line. */
2540 if (x < left_x && !s->row->mode_line_p)
2542 background_width -= left_x - x;
2543 x = left_x;
2545 if (background_width > 0)
2546 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2549 s->background_filled_p = 1;
2553 Draw a wavy line under S. The wave fills wave_height pixels from y0.
2555 x0 wave_length = 2
2557 y0 * * * * *
2558 |* * * * * * * * *
2559 wave_height = 3 | * * * *
2563 static void
2564 x_draw_underwave (struct glyph_string *s)
2566 int wave_height = 3, wave_length = 2;
2567 int dx, dy, x0, y0, width, x1, y1, x2, y2, odd, xmax;
2568 XRectangle wave_clip, string_clip, final_clip;
2570 dx = wave_length;
2571 dy = wave_height - 1;
2572 x0 = s->x;
2573 y0 = s->ybase - wave_height + 3;
2574 width = s->width;
2575 xmax = x0 + width;
2577 /* Find and set clipping rectangle */
2579 wave_clip.x = x0;
2580 wave_clip.y = y0;
2581 wave_clip.width = width;
2582 wave_clip.height = wave_height;
2583 get_glyph_string_clip_rect (s, &string_clip);
2585 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
2586 return;
2588 XSetClipRectangles (s->display, s->gc, 0, 0, &final_clip, 1, Unsorted);
2590 /* Draw the waves */
2592 x1 = x0 - (x0 % dx);
2593 x2 = x1 + dx;
2594 odd = (x1/dx) % 2;
2595 y1 = y2 = y0;
2597 if (odd)
2598 y1 += dy;
2599 else
2600 y2 += dy;
2602 if (INT_MAX - dx < xmax)
2603 emacs_abort ();
2605 while (x1 <= xmax)
2607 XDrawLine (s->display, s->window, s->gc, x1, y1, x2, y2);
2608 x1 = x2, y1 = y2;
2609 x2 += dx, y2 = y0 + odd*dy;
2610 odd = !odd;
2613 /* Restore previous clipping rectangle(s) */
2614 XSetClipRectangles (s->display, s->gc, 0, 0, s->clip, s->num_clips, Unsorted);
2618 /* Draw glyph string S. */
2620 static void
2621 x_draw_glyph_string (struct glyph_string *s)
2623 bool relief_drawn_p = 0;
2625 /* If S draws into the background of its successors, draw the
2626 background of the successors first so that S can draw into it.
2627 This makes S->next use XDrawString instead of XDrawImageString. */
2628 if (s->next && s->right_overhang && !s->for_overlaps)
2630 int width;
2631 struct glyph_string *next;
2633 for (width = 0, next = s->next;
2634 next && width < s->right_overhang;
2635 width += next->width, next = next->next)
2636 if (next->first_glyph->type != IMAGE_GLYPH)
2638 x_set_glyph_string_gc (next);
2639 x_set_glyph_string_clipping (next);
2640 if (next->first_glyph->type == STRETCH_GLYPH)
2641 x_draw_stretch_glyph_string (next);
2642 else
2643 x_draw_glyph_string_background (next, 1);
2644 next->num_clips = 0;
2648 /* Set up S->gc, set clipping and draw S. */
2649 x_set_glyph_string_gc (s);
2651 /* Draw relief (if any) in advance for char/composition so that the
2652 glyph string can be drawn over it. */
2653 if (!s->for_overlaps
2654 && s->face->box != FACE_NO_BOX
2655 && (s->first_glyph->type == CHAR_GLYPH
2656 || s->first_glyph->type == COMPOSITE_GLYPH))
2659 x_set_glyph_string_clipping (s);
2660 x_draw_glyph_string_background (s, 1);
2661 x_draw_glyph_string_box (s);
2662 x_set_glyph_string_clipping (s);
2663 relief_drawn_p = 1;
2665 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2666 && !s->clip_tail
2667 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2668 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2669 /* We must clip just this glyph. left_overhang part has already
2670 drawn when s->prev was drawn, and right_overhang part will be
2671 drawn later when s->next is drawn. */
2672 x_set_glyph_string_clipping_exactly (s, s);
2673 else
2674 x_set_glyph_string_clipping (s);
2676 switch (s->first_glyph->type)
2678 case IMAGE_GLYPH:
2679 x_draw_image_glyph_string (s);
2680 break;
2682 case STRETCH_GLYPH:
2683 x_draw_stretch_glyph_string (s);
2684 break;
2686 case CHAR_GLYPH:
2687 if (s->for_overlaps)
2688 s->background_filled_p = 1;
2689 else
2690 x_draw_glyph_string_background (s, 0);
2691 x_draw_glyph_string_foreground (s);
2692 break;
2694 case COMPOSITE_GLYPH:
2695 if (s->for_overlaps || (s->cmp_from > 0
2696 && ! s->first_glyph->u.cmp.automatic))
2697 s->background_filled_p = 1;
2698 else
2699 x_draw_glyph_string_background (s, 1);
2700 x_draw_composite_glyph_string_foreground (s);
2701 break;
2703 case GLYPHLESS_GLYPH:
2704 if (s->for_overlaps)
2705 s->background_filled_p = 1;
2706 else
2707 x_draw_glyph_string_background (s, 1);
2708 x_draw_glyphless_glyph_string_foreground (s);
2709 break;
2711 default:
2712 emacs_abort ();
2715 if (!s->for_overlaps)
2717 /* Draw underline. */
2718 if (s->face->underline_p)
2720 if (s->face->underline_type == FACE_UNDER_WAVE)
2722 if (s->face->underline_defaulted_p)
2723 x_draw_underwave (s);
2724 else
2726 XGCValues xgcv;
2727 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2728 XSetForeground (s->display, s->gc, s->face->underline_color);
2729 x_draw_underwave (s);
2730 XSetForeground (s->display, s->gc, xgcv.foreground);
2733 else if (s->face->underline_type == FACE_UNDER_LINE)
2735 unsigned long thickness, position;
2736 int y;
2738 if (s->prev && s->prev->face->underline_p
2739 && s->prev->face->underline_type == FACE_UNDER_LINE)
2741 /* We use the same underline style as the previous one. */
2742 thickness = s->prev->underline_thickness;
2743 position = s->prev->underline_position;
2745 else
2747 /* Get the underline thickness. Default is 1 pixel. */
2748 if (s->font && s->font->underline_thickness > 0)
2749 thickness = s->font->underline_thickness;
2750 else
2751 thickness = 1;
2752 if (x_underline_at_descent_line)
2753 position = (s->height - thickness) - (s->ybase - s->y);
2754 else
2756 /* Get the underline position. This is the recommended
2757 vertical offset in pixels from the baseline to the top of
2758 the underline. This is a signed value according to the
2759 specs, and its default is
2761 ROUND ((maximum descent) / 2), with
2762 ROUND(x) = floor (x + 0.5) */
2764 if (x_use_underline_position_properties
2765 && s->font && s->font->underline_position >= 0)
2766 position = s->font->underline_position;
2767 else if (s->font)
2768 position = (s->font->descent + 1) / 2;
2769 else
2770 position = underline_minimum_offset;
2772 position = max (position, underline_minimum_offset);
2774 /* Check the sanity of thickness and position. We should
2775 avoid drawing underline out of the current line area. */
2776 if (s->y + s->height <= s->ybase + position)
2777 position = (s->height - 1) - (s->ybase - s->y);
2778 if (s->y + s->height < s->ybase + position + thickness)
2779 thickness = (s->y + s->height) - (s->ybase + position);
2780 s->underline_thickness = thickness;
2781 s->underline_position = position;
2782 y = s->ybase + position;
2783 if (s->face->underline_defaulted_p)
2784 XFillRectangle (s->display, s->window, s->gc,
2785 s->x, y, s->width, thickness);
2786 else
2788 XGCValues xgcv;
2789 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2790 XSetForeground (s->display, s->gc, s->face->underline_color);
2791 XFillRectangle (s->display, s->window, s->gc,
2792 s->x, y, s->width, thickness);
2793 XSetForeground (s->display, s->gc, xgcv.foreground);
2797 /* Draw overline. */
2798 if (s->face->overline_p)
2800 unsigned long dy = 0, h = 1;
2802 if (s->face->overline_color_defaulted_p)
2803 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2804 s->width, h);
2805 else
2807 XGCValues xgcv;
2808 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2809 XSetForeground (s->display, s->gc, s->face->overline_color);
2810 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2811 s->width, h);
2812 XSetForeground (s->display, s->gc, xgcv.foreground);
2816 /* Draw strike-through. */
2817 if (s->face->strike_through_p)
2819 unsigned long h = 1;
2820 unsigned long dy = (s->height - h) / 2;
2822 if (s->face->strike_through_color_defaulted_p)
2823 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2824 s->width, h);
2825 else
2827 XGCValues xgcv;
2828 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2829 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2830 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2831 s->width, h);
2832 XSetForeground (s->display, s->gc, xgcv.foreground);
2836 /* Draw relief if not yet drawn. */
2837 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2838 x_draw_glyph_string_box (s);
2840 if (s->prev)
2842 struct glyph_string *prev;
2844 for (prev = s->prev; prev; prev = prev->prev)
2845 if (prev->hl != s->hl
2846 && prev->x + prev->width + prev->right_overhang > s->x)
2848 /* As prev was drawn while clipped to its own area, we
2849 must draw the right_overhang part using s->hl now. */
2850 enum draw_glyphs_face save = prev->hl;
2852 prev->hl = s->hl;
2853 x_set_glyph_string_gc (prev);
2854 x_set_glyph_string_clipping_exactly (s, prev);
2855 if (prev->first_glyph->type == CHAR_GLYPH)
2856 x_draw_glyph_string_foreground (prev);
2857 else
2858 x_draw_composite_glyph_string_foreground (prev);
2859 XSetClipMask (prev->display, prev->gc, None);
2860 prev->hl = save;
2861 prev->num_clips = 0;
2865 if (s->next)
2867 struct glyph_string *next;
2869 for (next = s->next; next; next = next->next)
2870 if (next->hl != s->hl
2871 && next->x - next->left_overhang < s->x + s->width)
2873 /* As next will be drawn while clipped to its own area,
2874 we must draw the left_overhang part using s->hl now. */
2875 enum draw_glyphs_face save = next->hl;
2877 next->hl = s->hl;
2878 x_set_glyph_string_gc (next);
2879 x_set_glyph_string_clipping_exactly (s, next);
2880 if (next->first_glyph->type == CHAR_GLYPH)
2881 x_draw_glyph_string_foreground (next);
2882 else
2883 x_draw_composite_glyph_string_foreground (next);
2884 XSetClipMask (next->display, next->gc, None);
2885 next->hl = save;
2886 next->num_clips = 0;
2887 next->clip_head = s->next;
2892 /* Reset clipping. */
2893 XSetClipMask (s->display, s->gc, None);
2894 s->num_clips = 0;
2897 /* Shift display to make room for inserted glyphs. */
2899 static void
2900 x_shift_glyphs_for_insert (struct frame *f, int x, int y, int width, int height, int shift_by)
2902 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2903 f->output_data.x->normal_gc,
2904 x, y, width, height,
2905 x + shift_by, y);
2908 /* Delete N glyphs at the nominal cursor position. Not implemented
2909 for X frames. */
2911 static void
2912 x_delete_glyphs (struct frame *f, register int n)
2914 emacs_abort ();
2918 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2919 If they are <= 0, this is probably an error. */
2921 void
2922 x_clear_area (Display *dpy, Window window, int x, int y, int width, int height)
2924 eassert (width > 0 && height > 0);
2925 XClearArea (dpy, window, x, y, width, height, False);
2929 /* Clear an entire frame. */
2931 static void
2932 x_clear_frame (struct frame *f)
2934 /* Clearing the frame will erase any cursor, so mark them all as no
2935 longer visible. */
2936 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2938 block_input ();
2940 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2942 /* We have to clear the scroll bars. If we have changed colors or
2943 something like that, then they should be notified. */
2944 x_scroll_bar_clear (f);
2946 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
2947 /* Make sure scroll bars are redrawn. As they aren't redrawn by
2948 redisplay, do it here. */
2949 if (FRAME_GTK_WIDGET (f))
2950 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
2951 #endif
2953 XFlush (FRAME_X_DISPLAY (f));
2955 unblock_input ();
2958 /* RIF: Show hourglass cursor on frame F. */
2960 static void
2961 x_show_hourglass (struct frame *f)
2963 Display *dpy = FRAME_X_DISPLAY (f);
2965 if (dpy)
2967 struct x_output *x = FRAME_X_OUTPUT (f);
2968 #ifdef USE_X_TOOLKIT
2969 if (x->widget)
2970 #else
2971 if (FRAME_OUTER_WINDOW (f))
2972 #endif
2974 x->hourglass_p = 1;
2976 if (!x->hourglass_window)
2978 unsigned long mask = CWCursor;
2979 XSetWindowAttributes attrs;
2980 #ifdef USE_GTK
2981 Window parent = FRAME_X_WINDOW (f);
2982 #else
2983 Window parent = FRAME_OUTER_WINDOW (f);
2984 #endif
2985 attrs.cursor = x->hourglass_cursor;
2987 x->hourglass_window = XCreateWindow
2988 (dpy, parent, 0, 0, 32000, 32000, 0, 0,
2989 InputOnly, CopyFromParent, mask, &attrs);
2992 XMapRaised (dpy, x->hourglass_window);
2993 XFlush (dpy);
2998 /* RIF: Cancel hourglass cursor on frame F. */
3000 static void
3001 x_hide_hourglass (struct frame *f)
3003 struct x_output *x = FRAME_X_OUTPUT (f);
3005 /* Watch out for newly created frames. */
3006 if (x->hourglass_window)
3008 XUnmapWindow (FRAME_X_DISPLAY (f), x->hourglass_window);
3009 /* Sync here because XTread_socket looks at the
3010 hourglass_p flag that is reset to zero below. */
3011 XSync (FRAME_X_DISPLAY (f), False);
3012 x->hourglass_p = 0;
3016 /* Invert the middle quarter of the frame for .15 sec. */
3018 static void
3019 XTflash (struct frame *f)
3021 block_input ();
3024 #ifdef USE_GTK
3025 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
3026 when the scroll bars and the edit widget share the same X window. */
3027 GdkWindow *window = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
3028 #ifdef HAVE_GTK3
3029 cairo_t *cr = gdk_cairo_create (window);
3030 cairo_set_source_rgb (cr, 1, 1, 1);
3031 cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
3032 #define XFillRectangle(d, win, gc, x, y, w, h) \
3033 do { \
3034 cairo_rectangle (cr, x, y, w, h); \
3035 cairo_fill (cr); \
3037 while (0)
3038 #else /* ! HAVE_GTK3 */
3039 GdkGCValues vals;
3040 GdkGC *gc;
3041 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
3042 ^ FRAME_BACKGROUND_PIXEL (f));
3043 vals.function = GDK_XOR;
3044 gc = gdk_gc_new_with_values (window,
3045 &vals, GDK_GC_FUNCTION | GDK_GC_FOREGROUND);
3046 #define XFillRectangle(d, win, gc, x, y, w, h) \
3047 gdk_draw_rectangle (window, gc, TRUE, x, y, w, h)
3048 #endif /* ! HAVE_GTK3 */
3049 #else /* ! USE_GTK */
3050 GC gc;
3052 /* Create a GC that will use the GXxor function to flip foreground
3053 pixels into background pixels. */
3055 XGCValues values;
3057 values.function = GXxor;
3058 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3059 ^ FRAME_BACKGROUND_PIXEL (f));
3061 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3062 GCFunction | GCForeground, &values);
3064 #endif
3066 /* Get the height not including a menu bar widget. */
3067 int height = FRAME_PIXEL_HEIGHT (f);
3068 /* Height of each line to flash. */
3069 int flash_height = FRAME_LINE_HEIGHT (f);
3070 /* These will be the left and right margins of the rectangles. */
3071 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3072 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3073 int width = flash_right - flash_left;
3075 /* If window is tall, flash top and bottom line. */
3076 if (height > 3 * FRAME_LINE_HEIGHT (f))
3078 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3079 flash_left,
3080 (FRAME_INTERNAL_BORDER_WIDTH (f)
3081 + FRAME_TOP_MARGIN_HEIGHT (f)),
3082 width, flash_height);
3083 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3084 flash_left,
3085 (height - flash_height
3086 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3087 width, flash_height);
3090 else
3091 /* If it is short, flash it all. */
3092 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3093 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3094 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3096 x_flush (f);
3099 struct timespec delay = make_timespec (0, 150 * 1000 * 1000);
3100 struct timespec wakeup = timespec_add (current_timespec (), delay);
3102 /* Keep waiting until past the time wakeup or any input gets
3103 available. */
3104 while (! detect_input_pending ())
3106 struct timespec current = current_timespec ();
3107 struct timespec timeout;
3109 /* Break if result would not be positive. */
3110 if (timespec_cmp (wakeup, current) <= 0)
3111 break;
3113 /* How long `select' should wait. */
3114 timeout = make_timespec (0, 10 * 1000 * 1000);
3116 /* Try to wait that long--but we might wake up sooner. */
3117 pselect (0, NULL, NULL, NULL, &timeout, NULL);
3121 /* If window is tall, flash top and bottom line. */
3122 if (height > 3 * FRAME_LINE_HEIGHT (f))
3124 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3125 flash_left,
3126 (FRAME_INTERNAL_BORDER_WIDTH (f)
3127 + FRAME_TOP_MARGIN_HEIGHT (f)),
3128 width, flash_height);
3129 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3130 flash_left,
3131 (height - flash_height
3132 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3133 width, flash_height);
3135 else
3136 /* If it is short, flash it all. */
3137 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3138 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3139 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3141 #ifdef USE_GTK
3142 #ifdef HAVE_GTK3
3143 cairo_destroy (cr);
3144 #else
3145 g_object_unref (G_OBJECT (gc));
3146 #endif
3147 #undef XFillRectangle
3148 #else
3149 XFreeGC (FRAME_X_DISPLAY (f), gc);
3150 #endif
3151 x_flush (f);
3155 unblock_input ();
3159 static void
3160 XTtoggle_invisible_pointer (struct frame *f, int invisible)
3162 block_input ();
3163 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, invisible);
3164 unblock_input ();
3168 /* Make audible bell. */
3170 static void
3171 XTring_bell (struct frame *f)
3173 if (FRAME_X_DISPLAY (f))
3175 if (visible_bell)
3176 XTflash (f);
3177 else
3179 block_input ();
3180 #ifdef HAVE_XKB
3181 XkbBell (FRAME_X_DISPLAY (f), None, 0, None);
3182 #else
3183 XBell (FRAME_X_DISPLAY (f), 0);
3184 #endif
3185 XFlush (FRAME_X_DISPLAY (f));
3186 unblock_input ();
3191 /***********************************************************************
3192 Line Dance
3193 ***********************************************************************/
3195 /* Perform an insert-lines or delete-lines operation, inserting N
3196 lines or deleting -N lines at vertical position VPOS. */
3198 static void
3199 x_ins_del_lines (struct frame *f, int vpos, int n)
3201 emacs_abort ();
3205 /* Scroll part of the display as described by RUN. */
3207 static void
3208 x_scroll_run (struct window *w, struct run *run)
3210 struct frame *f = XFRAME (w->frame);
3211 int x, y, width, height, from_y, to_y, bottom_y;
3213 /* Get frame-relative bounding box of the text display area of W,
3214 without mode lines. Include in this box the left and right
3215 fringe of W. */
3216 window_box (w, ANY_AREA, &x, &y, &width, &height);
3218 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3219 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3220 bottom_y = y + height;
3222 if (to_y < from_y)
3224 /* Scrolling up. Make sure we don't copy part of the mode
3225 line at the bottom. */
3226 if (from_y + run->height > bottom_y)
3227 height = bottom_y - from_y;
3228 else
3229 height = run->height;
3231 else
3233 /* Scrolling down. Make sure we don't copy over the mode line.
3234 at the bottom. */
3235 if (to_y + run->height > bottom_y)
3236 height = bottom_y - to_y;
3237 else
3238 height = run->height;
3241 block_input ();
3243 /* Cursor off. Will be switched on again in x_update_window_end. */
3244 x_clear_cursor (w);
3246 XCopyArea (FRAME_X_DISPLAY (f),
3247 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3248 f->output_data.x->normal_gc,
3249 x, from_y,
3250 width, height,
3251 x, to_y);
3253 unblock_input ();
3258 /***********************************************************************
3259 Exposure Events
3260 ***********************************************************************/
3263 static void
3264 frame_highlight (struct frame *f)
3266 /* We used to only do this if Vx_no_window_manager was non-nil, but
3267 the ICCCM (section 4.1.6) says that the window's border pixmap
3268 and border pixel are window attributes which are "private to the
3269 client", so we can always change it to whatever we want. */
3270 block_input ();
3271 /* I recently started to get errors in this XSetWindowBorder, depending on
3272 the window-manager in use, tho something more is at play since I've been
3273 using that same window-manager binary for ever. Let's not crash just
3274 because of this (bug#9310). */
3275 x_catch_errors (FRAME_X_DISPLAY (f));
3276 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3277 f->output_data.x->border_pixel);
3278 x_uncatch_errors ();
3279 unblock_input ();
3280 x_update_cursor (f, 1);
3281 x_set_frame_alpha (f);
3284 static void
3285 frame_unhighlight (struct frame *f)
3287 /* We used to only do this if Vx_no_window_manager was non-nil, but
3288 the ICCCM (section 4.1.6) says that the window's border pixmap
3289 and border pixel are window attributes which are "private to the
3290 client", so we can always change it to whatever we want. */
3291 block_input ();
3292 /* Same as above for XSetWindowBorder (bug#9310). */
3293 x_catch_errors (FRAME_X_DISPLAY (f));
3294 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3295 f->output_data.x->border_tile);
3296 x_uncatch_errors ();
3297 unblock_input ();
3298 x_update_cursor (f, 1);
3299 x_set_frame_alpha (f);
3302 /* The focus has changed. Update the frames as necessary to reflect
3303 the new situation. Note that we can't change the selected frame
3304 here, because the Lisp code we are interrupting might become confused.
3305 Each event gets marked with the frame in which it occurred, so the
3306 Lisp code can tell when the switch took place by examining the events. */
3308 static void
3309 x_new_focus_frame (struct x_display_info *dpyinfo, struct frame *frame)
3311 struct frame *old_focus = dpyinfo->x_focus_frame;
3313 if (frame != dpyinfo->x_focus_frame)
3315 /* Set this before calling other routines, so that they see
3316 the correct value of x_focus_frame. */
3317 dpyinfo->x_focus_frame = frame;
3319 if (old_focus && old_focus->auto_lower)
3320 x_lower_frame (old_focus);
3322 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3323 dpyinfo->x_pending_autoraise_frame = dpyinfo->x_focus_frame;
3324 else
3325 dpyinfo->x_pending_autoraise_frame = NULL;
3328 x_frame_rehighlight (dpyinfo);
3331 /* Handle FocusIn and FocusOut state changes for FRAME.
3332 If FRAME has focus and there exists more than one frame, puts
3333 a FOCUS_IN_EVENT into *BUFP. */
3335 static void
3336 x_focus_changed (int type, int state, struct x_display_info *dpyinfo, struct frame *frame, struct input_event *bufp)
3338 if (type == FocusIn)
3340 if (dpyinfo->x_focus_event_frame != frame)
3342 x_new_focus_frame (dpyinfo, frame);
3343 dpyinfo->x_focus_event_frame = frame;
3345 /* Don't stop displaying the initial startup message
3346 for a switch-frame event we don't need. */
3347 /* When run as a daemon, Vterminal_frame is always NIL. */
3348 bufp->arg = (((NILP (Vterminal_frame)
3349 || ! FRAME_X_P (XFRAME (Vterminal_frame))
3350 || EQ (Fdaemonp (), Qt))
3351 && CONSP (Vframe_list)
3352 && !NILP (XCDR (Vframe_list)))
3353 ? Qt : Qnil);
3354 bufp->kind = FOCUS_IN_EVENT;
3355 XSETFRAME (bufp->frame_or_window, frame);
3358 frame->output_data.x->focus_state |= state;
3360 #ifdef HAVE_X_I18N
3361 if (FRAME_XIC (frame))
3362 XSetICFocus (FRAME_XIC (frame));
3363 #endif
3365 else if (type == FocusOut)
3367 frame->output_data.x->focus_state &= ~state;
3369 if (dpyinfo->x_focus_event_frame == frame)
3371 dpyinfo->x_focus_event_frame = 0;
3372 x_new_focus_frame (dpyinfo, 0);
3374 bufp->kind = FOCUS_OUT_EVENT;
3375 XSETFRAME (bufp->frame_or_window, frame);
3378 #ifdef HAVE_X_I18N
3379 if (FRAME_XIC (frame))
3380 XUnsetICFocus (FRAME_XIC (frame));
3381 #endif
3382 if (frame->pointer_invisible)
3383 XTtoggle_invisible_pointer (frame, 0);
3387 /* Return the Emacs frame-object corresponding to an X window.
3388 It could be the frame's main window or an icon window. */
3390 static struct frame *
3391 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3393 Lisp_Object tail, frame;
3394 struct frame *f;
3396 if (wdesc == None)
3397 return NULL;
3399 FOR_EACH_FRAME (tail, frame)
3401 f = XFRAME (frame);
3402 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3403 continue;
3404 if (f->output_data.x->hourglass_window == wdesc)
3405 return f;
3406 #ifdef USE_X_TOOLKIT
3407 if ((f->output_data.x->edit_widget
3408 && XtWindow (f->output_data.x->edit_widget) == wdesc)
3409 /* A tooltip frame? */
3410 || (!f->output_data.x->edit_widget
3411 && FRAME_X_WINDOW (f) == wdesc)
3412 || f->output_data.x->icon_desc == wdesc)
3413 return f;
3414 #else /* not USE_X_TOOLKIT */
3415 #ifdef USE_GTK
3416 if (f->output_data.x->edit_widget)
3418 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3419 struct x_output *x = f->output_data.x;
3420 if (gwdesc != 0 && gwdesc == x->edit_widget)
3421 return f;
3423 #endif /* USE_GTK */
3424 if (FRAME_X_WINDOW (f) == wdesc
3425 || f->output_data.x->icon_desc == wdesc)
3426 return f;
3427 #endif /* not USE_X_TOOLKIT */
3429 return 0;
3432 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3434 /* Like x_window_to_frame but also compares the window with the widget's
3435 windows. */
3437 static struct frame *
3438 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3440 Lisp_Object tail, frame;
3441 struct frame *f, *found = NULL;
3442 struct x_output *x;
3444 if (wdesc == None)
3445 return NULL;
3447 FOR_EACH_FRAME (tail, frame)
3449 if (found)
3450 break;
3451 f = XFRAME (frame);
3452 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
3454 /* This frame matches if the window is any of its widgets. */
3455 x = f->output_data.x;
3456 if (x->hourglass_window == wdesc)
3457 found = f;
3458 else if (x->widget)
3460 #ifdef USE_GTK
3461 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3462 if (gwdesc != 0
3463 && gtk_widget_get_toplevel (gwdesc) == x->widget)
3464 found = f;
3465 #else
3466 if (wdesc == XtWindow (x->widget)
3467 || wdesc == XtWindow (x->column_widget)
3468 || wdesc == XtWindow (x->edit_widget))
3469 found = f;
3470 /* Match if the window is this frame's menubar. */
3471 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
3472 found = f;
3473 #endif
3475 else if (FRAME_X_WINDOW (f) == wdesc)
3476 /* A tooltip frame. */
3477 found = f;
3481 return found;
3484 /* Likewise, but consider only the menu bar widget. */
3486 static struct frame *
3487 x_menubar_window_to_frame (struct x_display_info *dpyinfo,
3488 const XEvent *event)
3490 Window wdesc = event->xany.window;
3491 Lisp_Object tail, frame;
3492 struct frame *f;
3493 struct x_output *x;
3495 if (wdesc == None)
3496 return NULL;
3498 FOR_EACH_FRAME (tail, frame)
3500 f = XFRAME (frame);
3501 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3502 continue;
3503 x = f->output_data.x;
3504 #ifdef USE_GTK
3505 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
3506 return f;
3507 #else
3508 /* Match if the window is this frame's menubar. */
3509 if (x->menubar_widget
3510 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
3511 return f;
3512 #endif
3514 return 0;
3517 /* Return the frame whose principal (outermost) window is WDESC.
3518 If WDESC is some other (smaller) window, we return 0. */
3520 struct frame *
3521 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3523 Lisp_Object tail, frame;
3524 struct frame *f;
3525 struct x_output *x;
3527 if (wdesc == None)
3528 return NULL;
3530 FOR_EACH_FRAME (tail, frame)
3532 f = XFRAME (frame);
3533 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3534 continue;
3535 x = f->output_data.x;
3537 if (x->widget)
3539 /* This frame matches if the window is its topmost widget. */
3540 #ifdef USE_GTK
3541 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3542 if (gwdesc == x->widget)
3543 return f;
3544 #else
3545 if (wdesc == XtWindow (x->widget))
3546 return f;
3547 #endif
3549 else if (FRAME_X_WINDOW (f) == wdesc)
3550 /* Tooltip frame. */
3551 return f;
3553 return 0;
3556 #else /* !USE_X_TOOLKIT && !USE_GTK */
3558 #define x_any_window_to_frame(d, i) x_window_to_frame (d, i)
3559 #define x_top_window_to_frame(d, i) x_window_to_frame (d, i)
3561 #endif /* USE_X_TOOLKIT || USE_GTK */
3563 /* The focus may have changed. Figure out if it is a real focus change,
3564 by checking both FocusIn/Out and Enter/LeaveNotify events.
3566 Returns FOCUS_IN_EVENT event in *BUFP. */
3568 static void
3569 x_detect_focus_change (struct x_display_info *dpyinfo, struct frame *frame,
3570 const XEvent *event, struct input_event *bufp)
3572 if (!frame)
3573 return;
3575 switch (event->type)
3577 case EnterNotify:
3578 case LeaveNotify:
3580 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3581 int focus_state
3582 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3584 if (event->xcrossing.detail != NotifyInferior
3585 && event->xcrossing.focus
3586 && ! (focus_state & FOCUS_EXPLICIT))
3587 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3588 FOCUS_IMPLICIT,
3589 dpyinfo, frame, bufp);
3591 break;
3593 case FocusIn:
3594 case FocusOut:
3595 x_focus_changed (event->type,
3596 (event->xfocus.detail == NotifyPointer ?
3597 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3598 dpyinfo, frame, bufp);
3599 break;
3601 case ClientMessage:
3602 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
3604 enum xembed_message msg = event->xclient.data.l[1];
3605 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
3606 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
3608 break;
3613 #if !defined USE_X_TOOLKIT && !defined USE_GTK
3614 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3616 void
3617 x_mouse_leave (struct x_display_info *dpyinfo)
3619 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3621 #endif
3623 /* The focus has changed, or we have redirected a frame's focus to
3624 another frame (this happens when a frame uses a surrogate
3625 mini-buffer frame). Shift the highlight as appropriate.
3627 The FRAME argument doesn't necessarily have anything to do with which
3628 frame is being highlighted or un-highlighted; we only use it to find
3629 the appropriate X display info. */
3631 static void
3632 XTframe_rehighlight (struct frame *frame)
3634 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
3637 static void
3638 x_frame_rehighlight (struct x_display_info *dpyinfo)
3640 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3642 if (dpyinfo->x_focus_frame)
3644 dpyinfo->x_highlight_frame
3645 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3646 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3647 : dpyinfo->x_focus_frame);
3648 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3650 fset_focus_frame (dpyinfo->x_focus_frame, Qnil);
3651 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3654 else
3655 dpyinfo->x_highlight_frame = 0;
3657 if (dpyinfo->x_highlight_frame != old_highlight)
3659 if (old_highlight)
3660 frame_unhighlight (old_highlight);
3661 if (dpyinfo->x_highlight_frame)
3662 frame_highlight (dpyinfo->x_highlight_frame);
3668 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3670 /* Initialize mode_switch_bit and modifier_meaning. */
3671 static void
3672 x_find_modifier_meanings (struct x_display_info *dpyinfo)
3674 int min_code, max_code;
3675 KeySym *syms;
3676 int syms_per_code;
3677 XModifierKeymap *mods;
3679 dpyinfo->meta_mod_mask = 0;
3680 dpyinfo->shift_lock_mask = 0;
3681 dpyinfo->alt_mod_mask = 0;
3682 dpyinfo->super_mod_mask = 0;
3683 dpyinfo->hyper_mod_mask = 0;
3685 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3687 syms = XGetKeyboardMapping (dpyinfo->display,
3688 min_code, max_code - min_code + 1,
3689 &syms_per_code);
3690 mods = XGetModifierMapping (dpyinfo->display);
3692 /* Scan the modifier table to see which modifier bits the Meta and
3693 Alt keysyms are on. */
3695 int row, col; /* The row and column in the modifier table. */
3696 int found_alt_or_meta;
3698 for (row = 3; row < 8; row++)
3700 found_alt_or_meta = 0;
3701 for (col = 0; col < mods->max_keypermod; col++)
3703 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3705 /* Zeroes are used for filler. Skip them. */
3706 if (code == 0)
3707 continue;
3709 /* Are any of this keycode's keysyms a meta key? */
3711 int code_col;
3713 for (code_col = 0; code_col < syms_per_code; code_col++)
3715 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3717 switch (sym)
3719 case XK_Meta_L:
3720 case XK_Meta_R:
3721 found_alt_or_meta = 1;
3722 dpyinfo->meta_mod_mask |= (1 << row);
3723 break;
3725 case XK_Alt_L:
3726 case XK_Alt_R:
3727 found_alt_or_meta = 1;
3728 dpyinfo->alt_mod_mask |= (1 << row);
3729 break;
3731 case XK_Hyper_L:
3732 case XK_Hyper_R:
3733 if (!found_alt_or_meta)
3734 dpyinfo->hyper_mod_mask |= (1 << row);
3735 code_col = syms_per_code;
3736 col = mods->max_keypermod;
3737 break;
3739 case XK_Super_L:
3740 case XK_Super_R:
3741 if (!found_alt_or_meta)
3742 dpyinfo->super_mod_mask |= (1 << row);
3743 code_col = syms_per_code;
3744 col = mods->max_keypermod;
3745 break;
3747 case XK_Shift_Lock:
3748 /* Ignore this if it's not on the lock modifier. */
3749 if (!found_alt_or_meta && ((1 << row) == LockMask))
3750 dpyinfo->shift_lock_mask = LockMask;
3751 code_col = syms_per_code;
3752 col = mods->max_keypermod;
3753 break;
3761 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3762 if (! dpyinfo->meta_mod_mask)
3764 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3765 dpyinfo->alt_mod_mask = 0;
3768 /* If some keys are both alt and meta,
3769 make them just meta, not alt. */
3770 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3772 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3775 XFree (syms);
3776 XFreeModifiermap (mods);
3779 /* Convert between the modifier bits X uses and the modifier bits
3780 Emacs uses. */
3783 x_x_to_emacs_modifiers (struct x_display_info *dpyinfo, int state)
3785 int mod_meta = meta_modifier;
3786 int mod_alt = alt_modifier;
3787 int mod_hyper = hyper_modifier;
3788 int mod_super = super_modifier;
3789 Lisp_Object tem;
3791 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3792 if (INTEGERP (tem)) mod_alt = XINT (tem) & INT_MAX;
3793 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3794 if (INTEGERP (tem)) mod_meta = XINT (tem) & INT_MAX;
3795 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3796 if (INTEGERP (tem)) mod_hyper = XINT (tem) & INT_MAX;
3797 tem = Fget (Vx_super_keysym, Qmodifier_value);
3798 if (INTEGERP (tem)) mod_super = XINT (tem) & INT_MAX;
3800 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3801 | ((state & ControlMask) ? ctrl_modifier : 0)
3802 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3803 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3804 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3805 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3808 static int
3809 x_emacs_to_x_modifiers (struct x_display_info *dpyinfo, EMACS_INT state)
3811 EMACS_INT mod_meta = meta_modifier;
3812 EMACS_INT mod_alt = alt_modifier;
3813 EMACS_INT mod_hyper = hyper_modifier;
3814 EMACS_INT mod_super = super_modifier;
3816 Lisp_Object tem;
3818 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3819 if (INTEGERP (tem)) mod_alt = XINT (tem);
3820 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3821 if (INTEGERP (tem)) mod_meta = XINT (tem);
3822 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3823 if (INTEGERP (tem)) mod_hyper = XINT (tem);
3824 tem = Fget (Vx_super_keysym, Qmodifier_value);
3825 if (INTEGERP (tem)) mod_super = XINT (tem);
3828 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3829 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3830 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3831 | ((state & shift_modifier) ? ShiftMask : 0)
3832 | ((state & ctrl_modifier) ? ControlMask : 0)
3833 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3836 /* Convert a keysym to its name. */
3838 char *
3839 x_get_keysym_name (int keysym)
3841 char *value;
3843 block_input ();
3844 value = XKeysymToString (keysym);
3845 unblock_input ();
3847 return value;
3850 /* Mouse clicks and mouse movement. Rah.
3852 Formerly, we used PointerMotionHintMask (in standard_event_mask)
3853 so that we would have to call XQueryPointer after each MotionNotify
3854 event to ask for another such event. However, this made mouse tracking
3855 slow, and there was a bug that made it eventually stop.
3857 Simply asking for MotionNotify all the time seems to work better.
3859 In order to avoid asking for motion events and then throwing most
3860 of them away or busy-polling the server for mouse positions, we ask
3861 the server for pointer motion hints. This means that we get only
3862 one event per group of mouse movements. "Groups" are delimited by
3863 other kinds of events (focus changes and button clicks, for
3864 example), or by XQueryPointer calls; when one of these happens, we
3865 get another MotionNotify event the next time the mouse moves. This
3866 is at least as efficient as getting motion events when mouse
3867 tracking is on, and I suspect only negligibly worse when tracking
3868 is off. */
3870 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3872 If the event is a button press, then note that we have grabbed
3873 the mouse. */
3875 static Lisp_Object
3876 construct_mouse_click (struct input_event *result,
3877 const XButtonEvent *event,
3878 struct frame *f)
3880 /* Make the event type NO_EVENT; we'll change that when we decide
3881 otherwise. */
3882 result->kind = MOUSE_CLICK_EVENT;
3883 result->code = event->button - Button1;
3884 result->timestamp = event->time;
3885 result->modifiers = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f),
3886 event->state)
3887 | (event->type == ButtonRelease
3888 ? up_modifier
3889 : down_modifier));
3891 XSETINT (result->x, event->x);
3892 XSETINT (result->y, event->y);
3893 XSETFRAME (result->frame_or_window, f);
3894 result->arg = Qnil;
3895 return Qnil;
3898 /* Function to report a mouse movement to the mainstream Emacs code.
3899 The input handler calls this.
3901 We have received a mouse movement event, which is given in *event.
3902 If the mouse is over a different glyph than it was last time, tell
3903 the mainstream emacs code by setting mouse_moved. If not, ask for
3904 another motion event, so we can check again the next time it moves. */
3906 static int
3907 note_mouse_movement (struct frame *frame, const XMotionEvent *event)
3909 XRectangle *r;
3910 struct x_display_info *dpyinfo;
3912 if (!FRAME_X_OUTPUT (frame))
3913 return 0;
3915 dpyinfo = FRAME_DISPLAY_INFO (frame);
3916 dpyinfo->last_mouse_movement_time = event->time;
3917 dpyinfo->last_mouse_motion_frame = frame;
3918 dpyinfo->last_mouse_motion_x = event->x;
3919 dpyinfo->last_mouse_motion_y = event->y;
3921 if (event->window != FRAME_X_WINDOW (frame))
3923 frame->mouse_moved = 1;
3924 dpyinfo->last_mouse_scroll_bar = NULL;
3925 note_mouse_highlight (frame, -1, -1);
3926 dpyinfo->last_mouse_glyph_frame = NULL;
3927 return 1;
3931 /* Has the mouse moved off the glyph it was on at the last sighting? */
3932 r = &dpyinfo->last_mouse_glyph;
3933 if (frame != dpyinfo->last_mouse_glyph_frame
3934 || event->x < r->x || event->x >= r->x + r->width
3935 || event->y < r->y || event->y >= r->y + r->height)
3937 frame->mouse_moved = 1;
3938 dpyinfo->last_mouse_scroll_bar = NULL;
3939 note_mouse_highlight (frame, event->x, event->y);
3940 /* Remember which glyph we're now on. */
3941 remember_mouse_glyph (frame, event->x, event->y, r);
3942 dpyinfo->last_mouse_glyph_frame = frame;
3943 return 1;
3946 return 0;
3949 /* Return the current position of the mouse.
3950 *FP should be a frame which indicates which display to ask about.
3952 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3953 and *PART to the frame, window, and scroll bar part that the mouse
3954 is over. Set *X and *Y to the portion and whole of the mouse's
3955 position on the scroll bar.
3957 If the mouse movement started elsewhere, set *FP to the frame the
3958 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3959 the mouse is over.
3961 Set *TIMESTAMP to the server time-stamp for the time at which the mouse
3962 was at this position.
3964 Don't store anything if we don't have a valid set of values to report.
3966 This clears the mouse_moved flag, so we can wait for the next mouse
3967 movement. */
3969 static void
3970 XTmouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
3971 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
3972 Time *timestamp)
3974 struct frame *f1;
3975 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
3977 block_input ();
3979 if (dpyinfo->last_mouse_scroll_bar && insist == 0)
3981 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
3983 if (bar->horizontal)
3984 x_horizontal_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
3985 else
3986 x_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
3988 else
3990 Window root;
3991 int root_x, root_y;
3993 Window dummy_window;
3994 int dummy;
3996 Lisp_Object frame, tail;
3998 /* Clear the mouse-moved flag for every frame on this display. */
3999 FOR_EACH_FRAME (tail, frame)
4000 if (FRAME_X_P (XFRAME (frame))
4001 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
4002 XFRAME (frame)->mouse_moved = 0;
4004 dpyinfo->last_mouse_scroll_bar = NULL;
4006 /* Figure out which root window we're on. */
4007 XQueryPointer (FRAME_X_DISPLAY (*fp),
4008 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
4010 /* The root window which contains the pointer. */
4011 &root,
4013 /* Trash which we can't trust if the pointer is on
4014 a different screen. */
4015 &dummy_window,
4017 /* The position on that root window. */
4018 &root_x, &root_y,
4020 /* More trash we can't trust. */
4021 &dummy, &dummy,
4023 /* Modifier keys and pointer buttons, about which
4024 we don't care. */
4025 (unsigned int *) &dummy);
4027 /* Now we have a position on the root; find the innermost window
4028 containing the pointer. */
4030 Window win, child;
4031 int win_x, win_y;
4032 int parent_x = 0, parent_y = 0;
4034 win = root;
4036 /* XTranslateCoordinates can get errors if the window
4037 structure is changing at the same time this function
4038 is running. So at least we must not crash from them. */
4040 x_catch_errors (FRAME_X_DISPLAY (*fp));
4042 if (x_mouse_grabbed (dpyinfo))
4044 /* If mouse was grabbed on a frame, give coords for that frame
4045 even if the mouse is now outside it. */
4046 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
4048 /* From-window. */
4049 root,
4051 /* To-window. */
4052 FRAME_X_WINDOW (dpyinfo->last_mouse_frame),
4054 /* From-position, to-position. */
4055 root_x, root_y, &win_x, &win_y,
4057 /* Child of win. */
4058 &child);
4059 f1 = dpyinfo->last_mouse_frame;
4061 else
4063 while (1)
4065 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
4067 /* From-window, to-window. */
4068 root, win,
4070 /* From-position, to-position. */
4071 root_x, root_y, &win_x, &win_y,
4073 /* Child of win. */
4074 &child);
4076 if (child == None || child == win)
4077 break;
4078 #ifdef USE_GTK
4079 /* We don't wan't to know the innermost window. We
4080 want the edit window. For non-Gtk+ the innermost
4081 window is the edit window. For Gtk+ it might not
4082 be. It might be the tool bar for example. */
4083 if (x_window_to_frame (dpyinfo, win))
4084 break;
4085 #endif
4086 win = child;
4087 parent_x = win_x;
4088 parent_y = win_y;
4091 /* Now we know that:
4092 win is the innermost window containing the pointer
4093 (XTC says it has no child containing the pointer),
4094 win_x and win_y are the pointer's position in it
4095 (XTC did this the last time through), and
4096 parent_x and parent_y are the pointer's position in win's parent.
4097 (They are what win_x and win_y were when win was child.
4098 If win is the root window, it has no parent, and
4099 parent_{x,y} are invalid, but that's okay, because we'll
4100 never use them in that case.) */
4102 #ifdef USE_GTK
4103 /* We don't wan't to know the innermost window. We
4104 want the edit window. */
4105 f1 = x_window_to_frame (dpyinfo, win);
4106 #else
4107 /* Is win one of our frames? */
4108 f1 = x_any_window_to_frame (dpyinfo, win);
4109 #endif
4111 #ifdef USE_X_TOOLKIT
4112 /* If we end up with the menu bar window, say it's not
4113 on the frame. */
4114 if (f1 != NULL
4115 && f1->output_data.x->menubar_widget
4116 && win == XtWindow (f1->output_data.x->menubar_widget))
4117 f1 = NULL;
4118 #endif /* USE_X_TOOLKIT */
4121 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
4122 f1 = 0;
4124 x_uncatch_errors ();
4126 /* If not, is it one of our scroll bars? */
4127 if (! f1)
4129 struct scroll_bar *bar;
4131 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win, 2);
4133 if (bar)
4135 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4136 win_x = parent_x;
4137 win_y = parent_y;
4141 if (f1 == 0 && insist > 0)
4142 f1 = SELECTED_FRAME ();
4144 if (f1)
4146 /* Ok, we found a frame. Store all the values.
4147 last_mouse_glyph is a rectangle used to reduce the
4148 generation of mouse events. To not miss any motion
4149 events, we must divide the frame into rectangles of the
4150 size of the smallest character that could be displayed
4151 on it, i.e. into the same rectangles that matrices on
4152 the frame are divided into. */
4154 /* FIXME: what if F1 is not an X frame? */
4155 dpyinfo = FRAME_DISPLAY_INFO (f1);
4156 remember_mouse_glyph (f1, win_x, win_y, &dpyinfo->last_mouse_glyph);
4157 dpyinfo->last_mouse_glyph_frame = f1;
4159 *bar_window = Qnil;
4160 *part = 0;
4161 *fp = f1;
4162 XSETINT (*x, win_x);
4163 XSETINT (*y, win_y);
4164 *timestamp = dpyinfo->last_mouse_movement_time;
4169 unblock_input ();
4174 /***********************************************************************
4175 Scroll bars
4176 ***********************************************************************/
4178 /* Scroll bar support. */
4180 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
4181 manages it.
4182 This can be called in GC, so we have to make sure to strip off mark
4183 bits. */
4185 static struct scroll_bar *
4186 x_window_to_scroll_bar (Display *display, Window window_id, int type)
4188 Lisp_Object tail, frame;
4190 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4191 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4192 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4194 FOR_EACH_FRAME (tail, frame)
4196 Lisp_Object bar, condemned;
4198 if (! FRAME_X_P (XFRAME (frame)))
4199 continue;
4201 /* Scan this frame's scroll bar list for a scroll bar with the
4202 right window ID. */
4203 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4204 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4205 /* This trick allows us to search both the ordinary and
4206 condemned scroll bar lists with one loop. */
4207 ! NILP (bar) || (bar = condemned,
4208 condemned = Qnil,
4209 ! NILP (bar));
4210 bar = XSCROLL_BAR (bar)->next)
4211 if (XSCROLL_BAR (bar)->x_window == window_id
4212 && FRAME_X_DISPLAY (XFRAME (frame)) == display
4213 && (type = 2
4214 || (type == 1 && XSCROLL_BAR (bar)->horizontal)
4215 || (type == 0 && !XSCROLL_BAR (bar)->horizontal)))
4216 return XSCROLL_BAR (bar);
4219 return NULL;
4223 #if defined USE_LUCID
4225 /* Return the Lucid menu bar WINDOW is part of. Return null
4226 if WINDOW is not part of a menu bar. */
4228 static Widget
4229 x_window_to_menu_bar (Window window)
4231 Lisp_Object tail, frame;
4233 FOR_EACH_FRAME (tail, frame)
4234 if (FRAME_X_P (XFRAME (frame)))
4236 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4238 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4239 return menu_bar;
4241 return NULL;
4244 #endif /* USE_LUCID */
4247 /************************************************************************
4248 Toolkit scroll bars
4249 ************************************************************************/
4251 #ifdef USE_TOOLKIT_SCROLL_BARS
4253 static void x_send_scroll_bar_event (Lisp_Object, int, int, int, bool);
4255 /* Lisp window being scrolled. Set when starting to interact with
4256 a toolkit scroll bar, reset to nil when ending the interaction. */
4258 static Lisp_Object window_being_scrolled;
4260 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4261 that movements of 1/20 of the screen size are mapped to up/down. */
4263 #ifndef USE_GTK
4264 /* Id of action hook installed for scroll bars. */
4266 static XtActionHookId action_hook_id;
4267 static XtActionHookId horizontal_action_hook_id;
4269 static Boolean xaw3d_arrow_scroll;
4271 /* Whether the drag scrolling maintains the mouse at the top of the
4272 thumb. If not, resizing the thumb needs to be done more carefully
4273 to avoid jerkiness. */
4275 static Boolean xaw3d_pick_top;
4277 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4278 bars are used.. The hook is responsible for detecting when
4279 the user ends an interaction with the scroll bar, and generates
4280 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4282 static void
4283 xt_action_hook (Widget widget, XtPointer client_data, String action_name,
4284 XEvent *event, String *params, Cardinal *num_params)
4286 int scroll_bar_p;
4287 const char *end_action;
4289 #ifdef USE_MOTIF
4290 scroll_bar_p = XmIsScrollBar (widget);
4291 end_action = "Release";
4292 #else /* !USE_MOTIF i.e. use Xaw */
4293 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4294 end_action = "EndScroll";
4295 #endif /* USE_MOTIF */
4297 if (scroll_bar_p
4298 && strcmp (action_name, end_action) == 0
4299 && WINDOWP (window_being_scrolled))
4301 struct window *w;
4302 struct scroll_bar *bar;
4304 x_send_scroll_bar_event (window_being_scrolled,
4305 scroll_bar_end_scroll, 0, 0, 0);
4306 w = XWINDOW (window_being_scrolled);
4307 bar = XSCROLL_BAR (w->vertical_scroll_bar);
4309 if (bar->dragging != -1)
4311 bar->dragging = -1;
4312 /* The thumb size is incorrect while dragging: fix it. */
4313 set_vertical_scroll_bar (w);
4315 window_being_scrolled = Qnil;
4316 #if defined (USE_LUCID)
4317 bar->last_seen_part = scroll_bar_nowhere;
4318 #endif
4319 /* Xt timeouts no longer needed. */
4320 toolkit_scroll_bar_interaction = 0;
4325 static void
4326 xt_horizontal_action_hook (Widget widget, XtPointer client_data, String action_name,
4327 XEvent *event, String *params, Cardinal *num_params)
4329 int scroll_bar_p;
4330 const char *end_action;
4332 #ifdef USE_MOTIF
4333 scroll_bar_p = XmIsScrollBar (widget);
4334 end_action = "Release";
4335 #else /* !USE_MOTIF i.e. use Xaw */
4336 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4337 end_action = "EndScroll";
4338 #endif /* USE_MOTIF */
4340 if (scroll_bar_p
4341 && strcmp (action_name, end_action) == 0
4342 && WINDOWP (window_being_scrolled))
4344 struct window *w;
4345 struct scroll_bar *bar;
4347 x_send_scroll_bar_event (window_being_scrolled,
4348 scroll_bar_end_scroll, 0, 0, 1);
4349 w = XWINDOW (window_being_scrolled);
4350 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
4352 if (bar->dragging != -1)
4354 bar->dragging = -1;
4355 /* The thumb size is incorrect while dragging: fix it. */
4356 set_horizontal_scroll_bar (w);
4358 window_being_scrolled = Qnil;
4359 #if defined (USE_LUCID)
4360 bar->last_seen_part = scroll_bar_nowhere;
4361 #endif
4362 /* Xt timeouts no longer needed. */
4363 toolkit_scroll_bar_interaction = 0;
4366 #endif /* not USE_GTK */
4368 /* Send a client message with message type Xatom_Scrollbar for a
4369 scroll action to the frame of WINDOW. PART is a value identifying
4370 the part of the scroll bar that was clicked on. PORTION is the
4371 amount to scroll of a whole of WHOLE. */
4373 static void
4374 x_send_scroll_bar_event (Lisp_Object window, int part, int portion, int whole, bool horizontal)
4376 XEvent event;
4377 XClientMessageEvent *ev = &event.xclient;
4378 struct window *w = XWINDOW (window);
4379 struct frame *f = XFRAME (w->frame);
4380 intptr_t iw = (intptr_t) w;
4381 enum { BITS_PER_INTPTR = CHAR_BIT * sizeof iw };
4382 verify (BITS_PER_INTPTR <= 64);
4383 int sign_shift = BITS_PER_INTPTR - 32;
4385 block_input ();
4387 /* Construct a ClientMessage event to send to the frame. */
4388 ev->type = ClientMessage;
4389 ev->message_type = (horizontal
4390 ? FRAME_DISPLAY_INFO (f)->Xatom_Horizontal_Scrollbar
4391 : FRAME_DISPLAY_INFO (f)->Xatom_Scrollbar);
4392 ev->display = FRAME_X_DISPLAY (f);
4393 ev->window = FRAME_X_WINDOW (f);
4394 ev->format = 32;
4396 /* A 32-bit X client on a 64-bit X server can pass a window pointer
4397 as-is. A 64-bit client on a 32-bit X server is in trouble
4398 because a pointer does not fit and would be truncated while
4399 passing through the server. So use two slots and hope that X12
4400 will resolve such issues someday. */
4401 ev->data.l[0] = iw >> 31 >> 1;
4402 ev->data.l[1] = sign_shift <= 0 ? iw : iw << sign_shift >> sign_shift;
4403 ev->data.l[2] = part;
4404 ev->data.l[3] = portion;
4405 ev->data.l[4] = whole;
4407 /* Make Xt timeouts work while the scroll bar is active. */
4408 #ifdef USE_X_TOOLKIT
4409 toolkit_scroll_bar_interaction = 1;
4410 x_activate_timeout_atimer ();
4411 #endif
4413 /* Setting the event mask to zero means that the message will
4414 be sent to the client that created the window, and if that
4415 window no longer exists, no event will be sent. */
4416 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4417 unblock_input ();
4421 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4422 in *IEVENT. */
4424 static void
4425 x_scroll_bar_to_input_event (const XEvent *event,
4426 struct input_event *ievent)
4428 const XClientMessageEvent *ev = &event->xclient;
4429 Lisp_Object window;
4430 struct window *w;
4432 /* See the comment in the function above. */
4433 intptr_t iw0 = ev->data.l[0];
4434 intptr_t iw1 = ev->data.l[1];
4435 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
4436 w = (struct window *) iw;
4438 XSETWINDOW (window, w);
4440 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4441 ievent->frame_or_window = window;
4442 ievent->arg = Qnil;
4443 #ifdef USE_GTK
4444 ievent->timestamp = CurrentTime;
4445 #else
4446 ievent->timestamp =
4447 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
4448 #endif
4449 ievent->code = 0;
4450 ievent->part = ev->data.l[2];
4451 ievent->x = make_number (ev->data.l[3]);
4452 ievent->y = make_number (ev->data.l[4]);
4453 ievent->modifiers = 0;
4456 /* Transform a horizontal scroll bar ClientMessage EVENT to an Emacs
4457 input event in *IEVENT. */
4459 static void
4460 x_horizontal_scroll_bar_to_input_event (const XEvent *event,
4461 struct input_event *ievent)
4463 const XClientMessageEvent *ev = &event->xclient;
4464 Lisp_Object window;
4465 struct window *w;
4467 /* See the comment in the function above. */
4468 intptr_t iw0 = ev->data.l[0];
4469 intptr_t iw1 = ev->data.l[1];
4470 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
4471 w = (struct window *) iw;
4473 XSETWINDOW (window, w);
4475 ievent->kind = HORIZONTAL_SCROLL_BAR_CLICK_EVENT;
4476 ievent->frame_or_window = window;
4477 ievent->arg = Qnil;
4478 #ifdef USE_GTK
4479 ievent->timestamp = CurrentTime;
4480 #else
4481 ievent->timestamp =
4482 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
4483 #endif
4484 ievent->code = 0;
4485 ievent->part = ev->data.l[2];
4486 ievent->x = make_number (ev->data.l[3]);
4487 ievent->y = make_number (ev->data.l[4]);
4488 ievent->modifiers = 0;
4492 #ifdef USE_MOTIF
4494 /* Minimum and maximum values used for Motif scroll bars. */
4496 #define XM_SB_MAX 10000000
4498 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4499 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4500 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4502 static void
4503 xm_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4505 struct scroll_bar *bar = client_data;
4506 XmScrollBarCallbackStruct *cs = call_data;
4507 int part = -1, whole = 0, portion = 0;
4508 int horizontal = bar->horizontal;
4510 switch (cs->reason)
4512 case XmCR_DECREMENT:
4513 bar->dragging = -1;
4514 part = horizontal ? scroll_bar_left_arrow : scroll_bar_up_arrow;
4515 break;
4517 case XmCR_INCREMENT:
4518 bar->dragging = -1;
4519 part = horizontal ? scroll_bar_right_arrow : scroll_bar_down_arrow;
4520 break;
4522 case XmCR_PAGE_DECREMENT:
4523 bar->dragging = -1;
4524 part = horizontal ? scroll_bar_before_handle : scroll_bar_above_handle;
4525 break;
4527 case XmCR_PAGE_INCREMENT:
4528 bar->dragging = -1;
4529 part = horizontal ? scroll_bar_after_handle : scroll_bar_below_handle;
4530 break;
4532 case XmCR_TO_TOP:
4533 bar->dragging = -1;
4534 part = horizontal ? scroll_bar_to_leftmost : scroll_bar_to_top;
4535 break;
4537 case XmCR_TO_BOTTOM:
4538 bar->dragging = -1;
4539 part = horizontal ? scroll_bar_to_rightmost : scroll_bar_to_bottom;
4540 break;
4542 case XmCR_DRAG:
4544 int slider_size;
4546 block_input ();
4547 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4548 unblock_input ();
4550 if (horizontal)
4552 portion = bar->whole * ((float)cs->value / XM_SB_MAX);
4553 whole = bar->whole * ((float)(XM_SB_MAX - slider_size) / XM_SB_MAX);
4554 portion = min (portion, whole);
4555 part = scroll_bar_horizontal_handle;
4557 else
4559 whole = XM_SB_MAX - slider_size;
4560 portion = min (cs->value, whole);
4561 part = scroll_bar_handle;
4564 bar->dragging = cs->value;
4566 break;
4568 case XmCR_VALUE_CHANGED:
4569 break;
4572 if (part >= 0)
4574 window_being_scrolled = bar->window;
4575 x_send_scroll_bar_event (bar->window, part, portion, whole, bar->horizontal);
4579 #elif defined USE_GTK
4581 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4582 bar widget. DATA is a pointer to the scroll_bar structure. */
4584 static gboolean
4585 xg_scroll_callback (GtkRange *range,
4586 GtkScrollType scroll,
4587 gdouble value,
4588 gpointer user_data)
4590 struct scroll_bar *bar = user_data;
4591 int part = -1, whole = 0, portion = 0;
4592 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
4593 struct frame *f = g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
4595 if (xg_ignore_gtk_scrollbar) return FALSE;
4597 switch (scroll)
4599 case GTK_SCROLL_JUMP:
4600 /* Buttons 1 2 or 3 must be grabbed. */
4601 if (FRAME_DISPLAY_INFO (f)->grabbed != 0
4602 && FRAME_DISPLAY_INFO (f)->grabbed < (1 << 4))
4604 if (bar->horizontal)
4606 part = scroll_bar_horizontal_handle;
4607 whole = (int)(gtk_adjustment_get_upper (adj) -
4608 gtk_adjustment_get_page_size (adj));
4609 portion = min ((int)value, whole);
4610 bar->dragging = portion;
4612 else
4614 part = scroll_bar_handle;
4615 whole = gtk_adjustment_get_upper (adj) -
4616 gtk_adjustment_get_page_size (adj);
4617 portion = min ((int)value, whole);
4618 bar->dragging = portion;
4621 break;
4622 case GTK_SCROLL_STEP_BACKWARD:
4623 part = (bar->horizontal
4624 ? scroll_bar_left_arrow : scroll_bar_up_arrow);
4625 bar->dragging = -1;
4626 break;
4627 case GTK_SCROLL_STEP_FORWARD:
4628 part = (bar->horizontal
4629 ? scroll_bar_right_arrow : scroll_bar_down_arrow);
4630 bar->dragging = -1;
4631 break;
4632 case GTK_SCROLL_PAGE_BACKWARD:
4633 part = (bar->horizontal
4634 ? scroll_bar_before_handle : scroll_bar_above_handle);
4635 bar->dragging = -1;
4636 break;
4637 case GTK_SCROLL_PAGE_FORWARD:
4638 part = (bar->horizontal
4639 ? scroll_bar_after_handle : scroll_bar_below_handle);
4640 bar->dragging = -1;
4641 break;
4644 if (part >= 0)
4646 window_being_scrolled = bar->window;
4647 x_send_scroll_bar_event (bar->window, part, portion, whole, bar->horizontal);
4650 return FALSE;
4653 /* Callback for button release. Sets dragging to -1 when dragging is done. */
4655 static gboolean
4656 xg_end_scroll_callback (GtkWidget *widget,
4657 GdkEventButton *event,
4658 gpointer user_data)
4660 struct scroll_bar *bar = user_data;
4661 bar->dragging = -1;
4662 if (WINDOWP (window_being_scrolled))
4664 x_send_scroll_bar_event (window_being_scrolled,
4665 scroll_bar_end_scroll, 0, 0, bar->horizontal);
4666 window_being_scrolled = Qnil;
4669 return FALSE;
4673 #else /* not USE_GTK and not USE_MOTIF */
4675 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4676 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4677 scroll bar struct. CALL_DATA is a pointer to a float saying where
4678 the thumb is. */
4680 static void
4681 xaw_jump_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4683 struct scroll_bar *bar = client_data;
4684 float *top_addr = call_data;
4685 float top = *top_addr;
4686 float shown;
4687 int whole, portion, height, width;
4688 enum scroll_bar_part part;
4689 int horizontal = bar->horizontal;
4692 if (horizontal)
4694 /* Get the size of the thumb, a value between 0 and 1. */
4695 block_input ();
4696 XtVaGetValues (widget, XtNshown, &shown, XtNwidth, &width, NULL);
4697 unblock_input ();
4699 if (shown < 1)
4701 whole = bar->whole - (shown * bar->whole);
4702 portion = min (top * bar->whole, whole);
4704 else
4706 whole = bar->whole;
4707 portion = 0;
4710 part = scroll_bar_horizontal_handle;
4712 else
4714 /* Get the size of the thumb, a value between 0 and 1. */
4715 block_input ();
4716 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4717 unblock_input ();
4719 whole = 10000000;
4720 portion = shown < 1 ? top * whole : 0;
4722 if (shown < 1 && (eabs (top + shown - 1) < 1.0f / height))
4723 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4724 the bottom, so we force the scrolling whenever we see that we're
4725 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4726 we try to ensure that we always stay two pixels away from the
4727 bottom). */
4728 part = scroll_bar_down_arrow;
4729 else
4730 part = scroll_bar_handle;
4733 window_being_scrolled = bar->window;
4734 bar->dragging = portion;
4735 bar->last_seen_part = part;
4736 x_send_scroll_bar_event (bar->window, part, portion, whole, bar->horizontal);
4740 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4741 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4742 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4743 the scroll bar. CALL_DATA is an integer specifying the action that
4744 has taken place. Its magnitude is in the range 0..height of the
4745 scroll bar. Negative values mean scroll towards buffer start.
4746 Values < height of scroll bar mean line-wise movement. */
4748 static void
4749 xaw_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4751 struct scroll_bar *bar = client_data;
4752 /* The position really is stored cast to a pointer. */
4753 int position = (intptr_t) call_data;
4754 Dimension height, width;
4755 enum scroll_bar_part part;
4757 if (bar->horizontal)
4759 /* Get the width of the scroll bar. */
4760 block_input ();
4761 XtVaGetValues (widget, XtNwidth, &width, NULL);
4762 unblock_input ();
4764 if (eabs (position) >= width)
4765 part = (position < 0) ? scroll_bar_before_handle : scroll_bar_after_handle;
4767 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4768 it maps line-movement to call_data = max(5, height/20). */
4769 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, width / 20))
4770 part = (position < 0) ? scroll_bar_left_arrow : scroll_bar_right_arrow;
4771 else
4772 part = scroll_bar_move_ratio;
4774 window_being_scrolled = bar->window;
4775 bar->dragging = -1;
4776 bar->last_seen_part = part;
4777 x_send_scroll_bar_event (bar->window, part, position, width, bar->horizontal);
4779 else
4782 /* Get the height of the scroll bar. */
4783 block_input ();
4784 XtVaGetValues (widget, XtNheight, &height, NULL);
4785 unblock_input ();
4787 if (eabs (position) >= height)
4788 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4790 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4791 it maps line-movement to call_data = max(5, height/20). */
4792 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
4793 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4794 else
4795 part = scroll_bar_move_ratio;
4797 window_being_scrolled = bar->window;
4798 bar->dragging = -1;
4799 bar->last_seen_part = part;
4800 x_send_scroll_bar_event (bar->window, part, position, height, bar->horizontal);
4804 #endif /* not USE_GTK and not USE_MOTIF */
4806 #define SCROLL_BAR_NAME "verticalScrollBar"
4807 #define SCROLL_BAR_HORIZONTAL_NAME "horizontalScrollBar"
4809 /* Create the widget for scroll bar BAR on frame F. Record the widget
4810 and X window of the scroll bar in BAR. */
4812 #ifdef USE_GTK
4813 static void
4814 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
4816 const char *scroll_bar_name = SCROLL_BAR_NAME;
4818 block_input ();
4819 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4820 G_CALLBACK (xg_end_scroll_callback),
4821 scroll_bar_name);
4822 unblock_input ();
4825 static void
4826 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
4828 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
4830 block_input ();
4831 xg_create_horizontal_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4832 G_CALLBACK (xg_end_scroll_callback),
4833 scroll_bar_name);
4834 unblock_input ();
4837 #else /* not USE_GTK */
4839 static void
4840 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
4842 Window xwindow;
4843 Widget widget;
4844 Arg av[20];
4845 int ac = 0;
4846 const char *scroll_bar_name = SCROLL_BAR_NAME;
4847 unsigned long pixel;
4849 block_input ();
4851 #ifdef USE_MOTIF
4852 /* Set resources. Create the widget. */
4853 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4854 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4855 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4856 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4857 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4858 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4859 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4861 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4862 if (pixel != -1)
4864 XtSetArg (av[ac], XmNforeground, pixel);
4865 ++ac;
4868 pixel = f->output_data.x->scroll_bar_background_pixel;
4869 if (pixel != -1)
4871 XtSetArg (av[ac], XmNbackground, pixel);
4872 ++ac;
4875 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4876 (char *) scroll_bar_name, av, ac);
4878 /* Add one callback for everything that can happen. */
4879 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4880 (XtPointer) bar);
4881 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4882 (XtPointer) bar);
4883 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4884 (XtPointer) bar);
4885 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4886 (XtPointer) bar);
4887 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4888 (XtPointer) bar);
4889 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4890 (XtPointer) bar);
4891 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4892 (XtPointer) bar);
4894 /* Realize the widget. Only after that is the X window created. */
4895 XtRealizeWidget (widget);
4897 /* Set the cursor to an arrow. I didn't find a resource to do that.
4898 And I'm wondering why it hasn't an arrow cursor by default. */
4899 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4900 f->output_data.x->nontext_cursor);
4902 #else /* !USE_MOTIF i.e. use Xaw */
4904 /* Set resources. Create the widget. The background of the
4905 Xaw3d scroll bar widget is a little bit light for my taste.
4906 We don't alter it here to let users change it according
4907 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4908 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4909 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4910 /* For smoother scrolling with Xaw3d -sm */
4911 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4913 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4914 if (pixel != -1)
4916 XtSetArg (av[ac], XtNforeground, pixel);
4917 ++ac;
4920 pixel = f->output_data.x->scroll_bar_background_pixel;
4921 if (pixel != -1)
4923 XtSetArg (av[ac], XtNbackground, pixel);
4924 ++ac;
4927 /* Top/bottom shadow colors. */
4929 /* Allocate them, if necessary. */
4930 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4932 pixel = f->output_data.x->scroll_bar_background_pixel;
4933 if (pixel != -1)
4935 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4936 FRAME_X_COLORMAP (f),
4937 &pixel, 1.2, 0x8000))
4938 pixel = -1;
4939 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4942 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4944 pixel = f->output_data.x->scroll_bar_background_pixel;
4945 if (pixel != -1)
4947 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4948 FRAME_X_COLORMAP (f),
4949 &pixel, 0.6, 0x4000))
4950 pixel = -1;
4951 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4955 #ifdef XtNbeNiceToColormap
4956 /* Tell the toolkit about them. */
4957 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4958 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4959 /* We tried to allocate a color for the top/bottom shadow, and
4960 failed, so tell Xaw3d to use dithering instead. */
4961 /* But only if we have a small colormap. Xaw3d can allocate nice
4962 colors itself. */
4964 XtSetArg (av[ac], XtNbeNiceToColormap,
4965 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
4966 ++ac;
4968 else
4969 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4970 be more consistent with other emacs 3d colors, and since Xaw3d is
4971 not good at dealing with allocation failure. */
4973 /* This tells Xaw3d to use real colors instead of dithering for
4974 the shadows. */
4975 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4976 ++ac;
4978 /* Specify the colors. */
4979 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4980 if (pixel != -1)
4982 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4983 ++ac;
4985 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4986 if (pixel != -1)
4988 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4989 ++ac;
4992 #endif
4994 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4995 f->output_data.x->edit_widget, av, ac);
4998 char const *initial = "";
4999 char const *val = initial;
5000 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
5001 #ifdef XtNarrowScrollbars
5002 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
5003 #endif
5004 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
5005 if (xaw3d_arrow_scroll || val == initial)
5006 { /* ARROW_SCROLL */
5007 xaw3d_arrow_scroll = True;
5008 /* Isn't that just a personal preference ? --Stef */
5009 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
5013 /* Define callbacks. */
5014 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
5015 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
5016 (XtPointer) bar);
5018 /* Realize the widget. Only after that is the X window created. */
5019 XtRealizeWidget (widget);
5021 #endif /* !USE_MOTIF */
5023 /* Install an action hook that lets us detect when the user
5024 finishes interacting with a scroll bar. */
5025 if (action_hook_id == 0)
5026 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
5028 /* Remember X window and widget in the scroll bar vector. */
5029 SET_SCROLL_BAR_X_WIDGET (bar, widget);
5030 xwindow = XtWindow (widget);
5031 bar->x_window = xwindow;
5032 bar->whole = 1;
5033 bar->horizontal = 0;
5035 unblock_input ();
5038 static void
5039 x_create_horizontal_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
5041 Window xwindow;
5042 Widget widget;
5043 Arg av[20];
5044 int ac = 0;
5045 const char *scroll_bar_name = SCROLL_BAR_HORIZONTAL_NAME;
5046 unsigned long pixel;
5048 block_input ();
5050 #ifdef USE_MOTIF
5051 /* Set resources. Create the widget. */
5052 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5053 XtSetArg (av[ac], XmNminimum, 0); ++ac;
5054 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
5055 XtSetArg (av[ac], XmNorientation, XmHORIZONTAL); ++ac;
5056 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_RIGHT), ++ac;
5057 XtSetArg (av[ac], XmNincrement, 1); ++ac;
5058 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
5060 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5061 if (pixel != -1)
5063 XtSetArg (av[ac], XmNforeground, pixel);
5064 ++ac;
5067 pixel = f->output_data.x->scroll_bar_background_pixel;
5068 if (pixel != -1)
5070 XtSetArg (av[ac], XmNbackground, pixel);
5071 ++ac;
5074 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
5075 (char *) scroll_bar_name, av, ac);
5077 /* Add one callback for everything that can happen. */
5078 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
5079 (XtPointer) bar);
5080 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
5081 (XtPointer) bar);
5082 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
5083 (XtPointer) bar);
5084 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
5085 (XtPointer) bar);
5086 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
5087 (XtPointer) bar);
5088 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
5089 (XtPointer) bar);
5090 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
5091 (XtPointer) bar);
5093 /* Realize the widget. Only after that is the X window created. */
5094 XtRealizeWidget (widget);
5096 /* Set the cursor to an arrow. I didn't find a resource to do that.
5097 And I'm wondering why it hasn't an arrow cursor by default. */
5098 XDefineCursor (XtDisplay (widget), XtWindow (widget),
5099 f->output_data.x->nontext_cursor);
5101 #else /* !USE_MOTIF i.e. use Xaw */
5103 /* Set resources. Create the widget. The background of the
5104 Xaw3d scroll bar widget is a little bit light for my taste.
5105 We don't alter it here to let users change it according
5106 to their taste with `emacs*verticalScrollBar.background: xxx'. */
5107 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
5108 XtSetArg (av[ac], XtNorientation, XtorientHorizontal); ++ac;
5109 /* For smoother scrolling with Xaw3d -sm */
5110 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
5112 pixel = f->output_data.x->scroll_bar_foreground_pixel;
5113 if (pixel != -1)
5115 XtSetArg (av[ac], XtNforeground, pixel);
5116 ++ac;
5119 pixel = f->output_data.x->scroll_bar_background_pixel;
5120 if (pixel != -1)
5122 XtSetArg (av[ac], XtNbackground, pixel);
5123 ++ac;
5126 /* Top/bottom shadow colors. */
5128 /* Allocate them, if necessary. */
5129 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
5131 pixel = f->output_data.x->scroll_bar_background_pixel;
5132 if (pixel != -1)
5134 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5135 FRAME_X_COLORMAP (f),
5136 &pixel, 1.2, 0x8000))
5137 pixel = -1;
5138 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
5141 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5143 pixel = f->output_data.x->scroll_bar_background_pixel;
5144 if (pixel != -1)
5146 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
5147 FRAME_X_COLORMAP (f),
5148 &pixel, 0.6, 0x4000))
5149 pixel = -1;
5150 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
5154 #ifdef XtNbeNiceToColormap
5155 /* Tell the toolkit about them. */
5156 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
5157 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
5158 /* We tried to allocate a color for the top/bottom shadow, and
5159 failed, so tell Xaw3d to use dithering instead. */
5160 /* But only if we have a small colormap. Xaw3d can allocate nice
5161 colors itself. */
5163 XtSetArg (av[ac], XtNbeNiceToColormap,
5164 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
5165 ++ac;
5167 else
5168 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
5169 be more consistent with other emacs 3d colors, and since Xaw3d is
5170 not good at dealing with allocation failure. */
5172 /* This tells Xaw3d to use real colors instead of dithering for
5173 the shadows. */
5174 XtSetArg (av[ac], XtNbeNiceToColormap, False);
5175 ++ac;
5177 /* Specify the colors. */
5178 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
5179 if (pixel != -1)
5181 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
5182 ++ac;
5184 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
5185 if (pixel != -1)
5187 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
5188 ++ac;
5191 #endif
5193 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
5194 f->output_data.x->edit_widget, av, ac);
5197 char const *initial = "";
5198 char const *val = initial;
5199 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
5200 #ifdef XtNarrowScrollbars
5201 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
5202 #endif
5203 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
5204 if (xaw3d_arrow_scroll || val == initial)
5205 { /* ARROW_SCROLL */
5206 xaw3d_arrow_scroll = True;
5207 /* Isn't that just a personal preference ? --Stef */
5208 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
5212 /* Define callbacks. */
5213 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
5214 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
5215 (XtPointer) bar);
5217 /* Realize the widget. Only after that is the X window created. */
5218 XtRealizeWidget (widget);
5220 #endif /* !USE_MOTIF */
5222 /* Install an action hook that lets us detect when the user
5223 finishes interacting with a scroll bar. */
5224 if (horizontal_action_hook_id == 0)
5225 horizontal_action_hook_id
5226 = XtAppAddActionHook (Xt_app_con, xt_horizontal_action_hook, 0);
5228 /* Remember X window and widget in the scroll bar vector. */
5229 SET_SCROLL_BAR_X_WIDGET (bar, widget);
5230 xwindow = XtWindow (widget);
5231 bar->x_window = xwindow;
5232 bar->whole = 1;
5233 bar->horizontal = 1;
5235 unblock_input ();
5237 #endif /* not USE_GTK */
5240 /* Set the thumb size and position of scroll bar BAR. We are currently
5241 displaying PORTION out of a whole WHOLE, and our position POSITION. */
5243 #ifdef USE_GTK
5244 static void
5245 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
5247 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5250 static void
5251 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
5253 xg_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
5256 #else /* not USE_GTK */
5257 static void
5258 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
5259 int whole)
5261 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5262 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
5263 float top, shown;
5265 block_input ();
5267 #ifdef USE_MOTIF
5269 if (scroll_bar_adjust_thumb_portion_p)
5271 /* We use an estimate of 30 chars per line rather than the real
5272 `portion' value. This has the disadvantage that the thumb size
5273 is not very representative, but it makes our life a lot easier.
5274 Otherwise, we have to constantly adjust the thumb size, which
5275 we can't always do quickly enough: while dragging, the size of
5276 the thumb might prevent the user from dragging the thumb all the
5277 way to the end. but Motif and some versions of Xaw3d don't allow
5278 updating the thumb size while dragging. Also, even if we can update
5279 its size, the update will often happen too late.
5280 If you don't believe it, check out revision 1.650 of xterm.c to see
5281 what hoops we were going through and the still poor behavior we got. */
5282 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
5283 /* When the thumb is at the bottom, position == whole.
5284 So we need to increase `whole' to make space for the thumb. */
5285 whole += portion;
5288 if (whole <= 0)
5289 top = 0, shown = 1;
5290 else
5292 top = (float) position / whole;
5293 shown = (float) portion / whole;
5296 if (bar->dragging == -1)
5298 int size, value;
5300 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
5301 is the scroll bar's maximum and MIN is the scroll bar's minimum
5302 value. */
5303 size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
5305 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
5306 value = top * XM_SB_MAX;
5307 value = min (value, XM_SB_MAX - size);
5309 XmScrollBarSetValues (widget, value, size, 0, 0, False);
5311 #else /* !USE_MOTIF i.e. use Xaw */
5313 if (whole == 0)
5314 top = 0, shown = 1;
5315 else
5317 top = (float) position / whole;
5318 shown = (float) portion / whole;
5322 float old_top, old_shown;
5323 Dimension height;
5324 XtVaGetValues (widget,
5325 XtNtopOfThumb, &old_top,
5326 XtNshown, &old_shown,
5327 XtNheight, &height,
5328 NULL);
5330 /* Massage the top+shown values. */
5331 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
5332 top = max (0, min (1, top));
5333 else
5334 top = old_top;
5335 #if ! defined (HAVE_XAW3D)
5336 /* With Xaw, 'top' values too closer to 1.0 may
5337 cause the thumb to disappear. Fix that. */
5338 top = min (top, 0.99f);
5339 #endif
5340 /* Keep two pixels available for moving the thumb down. */
5341 shown = max (0, min (1 - top - (2.0f / height), shown));
5342 #if ! defined (HAVE_XAW3D)
5343 /* Likewise with too small 'shown'. */
5344 shown = max (shown, 0.01f);
5345 #endif
5347 /* If the call to XawScrollbarSetThumb below doesn't seem to
5348 work, check that 'NARROWPROTO' is defined in src/config.h.
5349 If this is not so, most likely you need to fix configure. */
5350 if (top != old_top || shown != old_shown)
5352 if (bar->dragging == -1)
5353 XawScrollbarSetThumb (widget, top, shown);
5354 else
5356 /* Try to make the scrolling a tad smoother. */
5357 if (!xaw3d_pick_top)
5358 shown = min (shown, old_shown);
5360 XawScrollbarSetThumb (widget, top, shown);
5364 #endif /* !USE_MOTIF */
5366 unblock_input ();
5369 static void
5370 x_set_toolkit_horizontal_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
5371 int whole)
5373 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5374 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
5375 float top, shown;
5377 block_input ();
5379 #ifdef USE_MOTIF
5380 bar->whole = whole;
5381 shown = (float) portion / whole;
5382 top = (float) position / (whole - portion);
5384 int size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
5385 int value = clip_to_bounds (0, top * (XM_SB_MAX - size), XM_SB_MAX - size);
5387 XmScrollBarSetValues (widget, value, size, 0, 0, False);
5389 #else /* !USE_MOTIF i.e. use Xaw */
5390 bar->whole = whole;
5391 if (whole == 0)
5392 top = 0, shown = 1;
5393 else
5395 top = (float) position / whole;
5396 shown = (float) portion / whole;
5400 float old_top, old_shown;
5401 Dimension height;
5402 XtVaGetValues (widget,
5403 XtNtopOfThumb, &old_top,
5404 XtNshown, &old_shown,
5405 XtNheight, &height,
5406 NULL);
5408 #if 0
5409 /* Massage the top+shown values. */
5410 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
5411 top = max (0, min (1, top));
5412 else
5413 top = old_top;
5414 #if ! defined (HAVE_XAW3D)
5415 /* With Xaw, 'top' values too closer to 1.0 may
5416 cause the thumb to disappear. Fix that. */
5417 top = min (top, 0.99f);
5418 #endif
5419 /* Keep two pixels available for moving the thumb down. */
5420 shown = max (0, min (1 - top - (2.0f / height), shown));
5421 #if ! defined (HAVE_XAW3D)
5422 /* Likewise with too small 'shown'. */
5423 shown = max (shown, 0.01f);
5424 #endif
5425 #endif
5427 /* If the call to XawScrollbarSetThumb below doesn't seem to
5428 work, check that 'NARROWPROTO' is defined in src/config.h.
5429 If this is not so, most likely you need to fix configure. */
5430 XawScrollbarSetThumb (widget, top, shown);
5431 #if 0
5432 if (top != old_top || shown != old_shown)
5434 if (bar->dragging == -1)
5435 XawScrollbarSetThumb (widget, top, shown);
5436 else
5438 /* Try to make the scrolling a tad smoother. */
5439 if (!xaw3d_pick_top)
5440 shown = min (shown, old_shown);
5442 XawScrollbarSetThumb (widget, top, shown);
5445 #endif
5447 #endif /* !USE_MOTIF */
5449 unblock_input ();
5451 #endif /* not USE_GTK */
5453 #endif /* USE_TOOLKIT_SCROLL_BARS */
5457 /************************************************************************
5458 Scroll bars, general
5459 ************************************************************************/
5461 /* Create a scroll bar and return the scroll bar vector for it. W is
5462 the Emacs window on which to create the scroll bar. TOP, LEFT,
5463 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
5464 scroll bar. */
5466 static struct scroll_bar *
5467 x_scroll_bar_create (struct window *w, int top, int left, int width, int height, bool horizontal)
5469 struct frame *f = XFRAME (w->frame);
5470 struct scroll_bar *bar
5471 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
5472 Lisp_Object barobj;
5474 block_input ();
5476 #ifdef USE_TOOLKIT_SCROLL_BARS
5477 if (horizontal)
5478 x_create_horizontal_toolkit_scroll_bar (f, bar);
5479 else
5480 x_create_toolkit_scroll_bar (f, bar);
5481 #else /* not USE_TOOLKIT_SCROLL_BARS */
5483 XSetWindowAttributes a;
5484 unsigned long mask;
5485 Window window;
5487 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
5488 if (a.background_pixel == -1)
5489 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
5491 a.event_mask = (ButtonPressMask | ButtonReleaseMask
5492 | ButtonMotionMask | PointerMotionHintMask
5493 | ExposureMask);
5494 a.cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
5496 mask = (CWBackPixel | CWEventMask | CWCursor);
5498 /* Clear the area of W that will serve as a scroll bar. This is
5499 for the case that a window has been split horizontally. In
5500 this case, no clear_frame is generated to reduce flickering. */
5501 if (width > 0 && window_box_height (w) > 0)
5502 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5503 left, top, width, window_box_height (w));
5505 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5506 /* Position and size of scroll bar. */
5507 left, top, width, height,
5508 /* Border width, depth, class, and visual. */
5510 CopyFromParent,
5511 CopyFromParent,
5512 CopyFromParent,
5513 /* Attributes. */
5514 mask, &a);
5515 bar->x_window = window;
5517 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5519 XSETWINDOW (bar->window, w);
5520 bar->top = top;
5521 bar->left = left;
5522 bar->width = width;
5523 bar->height = height;
5524 bar->start = 0;
5525 bar->end = 0;
5526 bar->dragging = -1;
5527 bar->horizontal = horizontal;
5528 #if defined (USE_TOOLKIT_SCROLL_BARS) && defined (USE_LUCID)
5529 bar->last_seen_part = scroll_bar_nowhere;
5530 #endif
5532 /* Add bar to its frame's list of scroll bars. */
5533 bar->next = FRAME_SCROLL_BARS (f);
5534 bar->prev = Qnil;
5535 XSETVECTOR (barobj, bar);
5536 fset_scroll_bars (f, barobj);
5537 if (!NILP (bar->next))
5538 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5540 /* Map the window/widget. */
5541 #ifdef USE_TOOLKIT_SCROLL_BARS
5543 #ifdef USE_GTK
5544 if (horizontal)
5545 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top,
5546 left, width, max (height, 1));
5547 else
5548 xg_update_scrollbar_pos (f, bar->x_window, top,
5549 left, width, max (height, 1));
5550 #else /* not USE_GTK */
5551 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
5552 XtConfigureWidget (scroll_bar, left, top, width, max (height, 1), 0);
5553 XtMapWidget (scroll_bar);
5554 #endif /* not USE_GTK */
5556 #else /* not USE_TOOLKIT_SCROLL_BARS */
5557 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
5558 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5560 unblock_input ();
5561 return bar;
5565 #ifndef USE_TOOLKIT_SCROLL_BARS
5567 /* Draw BAR's handle in the proper position.
5569 If the handle is already drawn from START to END, don't bother
5570 redrawing it, unless REBUILD is non-zero; in that case, always
5571 redraw it. (REBUILD is handy for drawing the handle after expose
5572 events.)
5574 Normally, we want to constrain the start and end of the handle to
5575 fit inside its rectangle, but if the user is dragging the scroll
5576 bar handle, we want to let them drag it down all the way, so that
5577 the bar's top is as far down as it goes; otherwise, there's no way
5578 to move to the very end of the buffer. */
5580 static void
5581 x_scroll_bar_set_handle (struct scroll_bar *bar, int start, int end, int rebuild)
5583 bool dragging = bar->dragging != -1;
5584 Window w = bar->x_window;
5585 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5586 GC gc = f->output_data.x->normal_gc;
5588 /* If the display is already accurate, do nothing. */
5589 if (! rebuild
5590 && start == bar->start
5591 && end == bar->end)
5592 return;
5594 block_input ();
5597 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
5598 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5599 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5601 /* Make sure the values are reasonable, and try to preserve
5602 the distance between start and end. */
5604 int length = end - start;
5606 if (start < 0)
5607 start = 0;
5608 else if (start > top_range)
5609 start = top_range;
5610 end = start + length;
5612 if (end < start)
5613 end = start;
5614 else if (end > top_range && ! dragging)
5615 end = top_range;
5618 /* Store the adjusted setting in the scroll bar. */
5619 bar->start = start;
5620 bar->end = end;
5622 /* Clip the end position, just for display. */
5623 if (end > top_range)
5624 end = top_range;
5626 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
5627 below top positions, to make sure the handle is always at least
5628 that many pixels tall. */
5629 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
5631 /* Draw the empty space above the handle. Note that we can't clear
5632 zero-height areas; that means "clear to end of window." */
5633 if ((inside_width > 0) && (start > 0))
5634 x_clear_area (FRAME_X_DISPLAY (f), w,
5635 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5636 VERTICAL_SCROLL_BAR_TOP_BORDER,
5637 inside_width, start);
5639 /* Change to proper foreground color if one is specified. */
5640 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5641 XSetForeground (FRAME_X_DISPLAY (f), gc,
5642 f->output_data.x->scroll_bar_foreground_pixel);
5644 /* Draw the handle itself. */
5645 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
5646 /* x, y, width, height */
5647 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5648 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
5649 inside_width, end - start);
5651 /* Restore the foreground color of the GC if we changed it above. */
5652 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5653 XSetForeground (FRAME_X_DISPLAY (f), gc,
5654 FRAME_FOREGROUND_PIXEL (f));
5656 /* Draw the empty space below the handle. Note that we can't
5657 clear zero-height areas; that means "clear to end of window." */
5658 if ((inside_width > 0) && (end < inside_height))
5659 x_clear_area (FRAME_X_DISPLAY (f), w,
5660 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5661 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
5662 inside_width, inside_height - end);
5665 unblock_input ();
5668 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5670 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
5671 nil. */
5673 static void
5674 x_scroll_bar_remove (struct scroll_bar *bar)
5676 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5677 block_input ();
5679 #ifdef USE_TOOLKIT_SCROLL_BARS
5680 #ifdef USE_GTK
5681 xg_remove_scroll_bar (f, bar->x_window);
5682 #else /* not USE_GTK */
5683 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5684 #endif /* not USE_GTK */
5685 #else
5686 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
5687 #endif
5689 /* Dissociate this scroll bar from its window. */
5690 if (bar->horizontal)
5691 wset_horizontal_scroll_bar (XWINDOW (bar->window), Qnil);
5692 else
5693 wset_vertical_scroll_bar (XWINDOW (bar->window), Qnil);
5695 unblock_input ();
5699 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5700 that we are displaying PORTION characters out of a total of WHOLE
5701 characters, starting at POSITION. If WINDOW has no scroll bar,
5702 create one. */
5704 static void
5705 XTset_vertical_scroll_bar (struct window *w, int portion, int whole, int position)
5707 struct frame *f = XFRAME (w->frame);
5708 Lisp_Object barobj;
5709 struct scroll_bar *bar;
5710 int top, height, left, width;
5711 int window_y, window_height;
5713 /* Get window dimensions. */
5714 window_box (w, ANY_AREA, 0, &window_y, 0, &window_height);
5715 top = window_y;
5716 height = window_height;
5717 left = WINDOW_SCROLL_BAR_AREA_X (w);
5718 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
5720 /* Does the scroll bar exist yet? */
5721 if (NILP (w->vertical_scroll_bar))
5723 if (width > 0 && height > 0)
5725 block_input ();
5726 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5727 left, top, width, height);
5728 unblock_input ();
5731 bar = x_scroll_bar_create (w, top, left, width, max (height, 1), 0);
5733 else
5735 /* It may just need to be moved and resized. */
5736 unsigned int mask = 0;
5738 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5740 block_input ();
5742 if (left != bar->left)
5743 mask |= CWX;
5744 if (top != bar->top)
5745 mask |= CWY;
5746 if (width != bar->width)
5747 mask |= CWWidth;
5748 if (height != bar->height)
5749 mask |= CWHeight;
5751 #ifdef USE_TOOLKIT_SCROLL_BARS
5753 /* Move/size the scroll bar widget. */
5754 if (mask)
5756 /* Since toolkit scroll bars are smaller than the space reserved
5757 for them on the frame, we have to clear "under" them. */
5758 if (width > 0 && height > 0)
5759 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5760 left, top, width, height);
5761 #ifdef USE_GTK
5762 xg_update_scrollbar_pos (f, bar->x_window, top,
5763 left, width, max (height, 1));
5764 #else /* not USE_GTK */
5765 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5766 left, top, width, max (height, 1), 0);
5767 #endif /* not USE_GTK */
5769 #else /* not USE_TOOLKIT_SCROLL_BARS */
5771 /* Move/size the scroll bar window. */
5772 if (mask)
5774 XWindowChanges wc;
5776 wc.x = left;
5777 wc.y = top;
5778 wc.width = width;
5779 wc.height = height;
5780 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5781 mask, &wc);
5784 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5786 /* Remember new settings. */
5787 bar->left = left;
5788 bar->top = top;
5789 bar->width = width;
5790 bar->height = height;
5792 unblock_input ();
5795 #ifdef USE_TOOLKIT_SCROLL_BARS
5796 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5797 #else /* not USE_TOOLKIT_SCROLL_BARS */
5798 /* Set the scroll bar's current state, unless we're currently being
5799 dragged. */
5800 if (bar->dragging == -1)
5802 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5804 if (whole == 0)
5805 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5806 else
5808 int start = ((double) position * top_range) / whole;
5809 int end = ((double) (position + portion) * top_range) / whole;
5810 x_scroll_bar_set_handle (bar, start, end, 0);
5813 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5815 XSETVECTOR (barobj, bar);
5816 wset_vertical_scroll_bar (w, barobj);
5820 static void
5821 XTset_horizontal_scroll_bar (struct window *w, int portion, int whole, int position)
5823 struct frame *f = XFRAME (w->frame);
5824 Lisp_Object barobj;
5825 struct scroll_bar *bar;
5826 int top, height, left, width;
5827 int window_x, window_width;
5828 int pixel_width = WINDOW_PIXEL_WIDTH (w);
5830 /* Get window dimensions. */
5831 window_box (w, ANY_AREA, &window_x, 0, &window_width, 0);
5832 left = window_x;
5833 width = window_width;
5834 top = WINDOW_SCROLL_BAR_AREA_Y (w);
5835 height = WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
5837 /* Does the scroll bar exist yet? */
5838 if (NILP (w->horizontal_scroll_bar))
5840 if (width > 0 && height > 0)
5842 block_input ();
5844 /* Clear also part between window_width and
5845 WINDOW_PIXEL_WIDTH. */
5846 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5847 left, top, pixel_width, height);
5848 unblock_input ();
5851 bar = x_scroll_bar_create (w, top, left, width, height, 1);
5853 else
5855 /* It may just need to be moved and resized. */
5856 unsigned int mask = 0;
5858 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
5860 block_input ();
5862 if (left != bar->left)
5863 mask |= CWX;
5864 if (top != bar->top)
5865 mask |= CWY;
5866 if (width != bar->width)
5867 mask |= CWWidth;
5868 if (height != bar->height)
5869 mask |= CWHeight;
5871 #ifdef USE_TOOLKIT_SCROLL_BARS
5872 /* Move/size the scroll bar widget. */
5873 if (mask)
5875 /* Since toolkit scroll bars are smaller than the space reserved
5876 for them on the frame, we have to clear "under" them. */
5877 if (width > 0 && height > 0)
5878 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5879 WINDOW_LEFT_EDGE_X (w), top,
5880 pixel_width - WINDOW_RIGHT_DIVIDER_WIDTH (w), height);
5881 #ifdef USE_GTK
5882 xg_update_horizontal_scrollbar_pos (f, bar->x_window, top, left,
5883 width, height);
5884 #else /* not USE_GTK */
5885 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5886 left, top, width, height, 0);
5887 #endif /* not USE_GTK */
5889 #else /* not USE_TOOLKIT_SCROLL_BARS */
5891 /* Clear areas not covered by the scroll bar because it's not as
5892 wide as the area reserved for it. This makes sure a
5893 previous mode line display is cleared after C-x 2 C-x 1, for
5894 example. */
5896 int area_height = WINDOW_CONFIG_SCROLL_BAR_HEIGHT (w);
5897 int rest = area_height - height;
5898 if (rest > 0 && width > 0)
5899 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5900 left, top, width, rest);
5903 /* Move/size the scroll bar window. */
5904 if (mask)
5906 XWindowChanges wc;
5908 wc.x = left;
5909 wc.y = top;
5910 wc.width = width;
5911 wc.height = height;
5912 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5913 mask, &wc);
5916 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5918 /* Remember new settings. */
5919 bar->left = left;
5920 bar->top = top;
5921 bar->width = width;
5922 bar->height = height;
5924 unblock_input ();
5927 #ifdef USE_TOOLKIT_SCROLL_BARS
5928 x_set_toolkit_horizontal_scroll_bar_thumb (bar, portion, position, whole);
5929 #else /* not USE_TOOLKIT_SCROLL_BARS */
5930 /* Set the scroll bar's current state, unless we're currently being
5931 dragged. */
5932 if (bar->dragging == -1)
5934 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, width);
5936 if (whole == 0)
5937 x_scroll_bar_set_handle (bar, 0, left_range, 0);
5938 else
5940 int start = ((double) position * left_range) / whole;
5941 int end = ((double) (position + portion) * left_range) / whole;
5942 x_scroll_bar_set_handle (bar, start, end, 0);
5945 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5947 XSETVECTOR (barobj, bar);
5948 wset_horizontal_scroll_bar (w, barobj);
5952 /* The following three hooks are used when we're doing a thorough
5953 redisplay of the frame. We don't explicitly know which scroll bars
5954 are going to be deleted, because keeping track of when windows go
5955 away is a real pain - "Can you say set-window-configuration, boys
5956 and girls?" Instead, we just assert at the beginning of redisplay
5957 that *all* scroll bars are to be removed, and then save a scroll bar
5958 from the fiery pit when we actually redisplay its window. */
5960 /* Arrange for all scroll bars on FRAME to be removed at the next call
5961 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5962 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5964 static void
5965 XTcondemn_scroll_bars (struct frame *frame)
5967 if (!NILP (FRAME_SCROLL_BARS (frame)))
5969 if (!NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5971 /* Prepend scrollbars to already condemned ones. */
5972 Lisp_Object last = FRAME_SCROLL_BARS (frame);
5974 while (!NILP (XSCROLL_BAR (last)->next))
5975 last = XSCROLL_BAR (last)->next;
5977 XSCROLL_BAR (last)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5978 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = last;
5981 fset_condemned_scroll_bars (frame, FRAME_SCROLL_BARS (frame));
5982 fset_scroll_bars (frame, Qnil);
5987 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5988 Note that WINDOW isn't necessarily condemned at all. */
5990 static void
5991 XTredeem_scroll_bar (struct window *w)
5993 struct scroll_bar *bar;
5994 Lisp_Object barobj;
5995 struct frame *f;
5997 /* We can't redeem this window's scroll bar if it doesn't have one. */
5998 if (NILP (w->vertical_scroll_bar) && NILP (w->horizontal_scroll_bar))
5999 emacs_abort ();
6001 if (!NILP (w->vertical_scroll_bar) && WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
6003 bar = XSCROLL_BAR (w->vertical_scroll_bar);
6004 /* Unlink it from the condemned list. */
6005 f = XFRAME (WINDOW_FRAME (w));
6006 if (NILP (bar->prev))
6008 /* If the prev pointer is nil, it must be the first in one of
6009 the lists. */
6010 if (EQ (FRAME_SCROLL_BARS (f), w->vertical_scroll_bar))
6011 /* It's not condemned. Everything's fine. */
6012 goto horizontal;
6013 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
6014 w->vertical_scroll_bar))
6015 fset_condemned_scroll_bars (f, bar->next);
6016 else
6017 /* If its prev pointer is nil, it must be at the front of
6018 one or the other! */
6019 emacs_abort ();
6021 else
6022 XSCROLL_BAR (bar->prev)->next = bar->next;
6024 if (! NILP (bar->next))
6025 XSCROLL_BAR (bar->next)->prev = bar->prev;
6027 bar->next = FRAME_SCROLL_BARS (f);
6028 bar->prev = Qnil;
6029 XSETVECTOR (barobj, bar);
6030 fset_scroll_bars (f, barobj);
6031 if (! NILP (bar->next))
6032 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6035 horizontal:
6036 if (!NILP (w->horizontal_scroll_bar) && WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
6038 bar = XSCROLL_BAR (w->horizontal_scroll_bar);
6039 /* Unlink it from the condemned list. */
6040 f = XFRAME (WINDOW_FRAME (w));
6041 if (NILP (bar->prev))
6043 /* If the prev pointer is nil, it must be the first in one of
6044 the lists. */
6045 if (EQ (FRAME_SCROLL_BARS (f), w->horizontal_scroll_bar))
6046 /* It's not condemned. Everything's fine. */
6047 return;
6048 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
6049 w->horizontal_scroll_bar))
6050 fset_condemned_scroll_bars (f, bar->next);
6051 else
6052 /* If its prev pointer is nil, it must be at the front of
6053 one or the other! */
6054 emacs_abort ();
6056 else
6057 XSCROLL_BAR (bar->prev)->next = bar->next;
6059 if (! NILP (bar->next))
6060 XSCROLL_BAR (bar->next)->prev = bar->prev;
6062 bar->next = FRAME_SCROLL_BARS (f);
6063 bar->prev = Qnil;
6064 XSETVECTOR (barobj, bar);
6065 fset_scroll_bars (f, barobj);
6066 if (! NILP (bar->next))
6067 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
6071 /* Remove all scroll bars on FRAME that haven't been saved since the
6072 last call to `*condemn_scroll_bars_hook'. */
6074 static void
6075 XTjudge_scroll_bars (struct frame *f)
6077 Lisp_Object bar, next;
6079 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
6081 /* Clear out the condemned list now so we won't try to process any
6082 more events on the hapless scroll bars. */
6083 fset_condemned_scroll_bars (f, Qnil);
6085 for (; ! NILP (bar); bar = next)
6087 struct scroll_bar *b = XSCROLL_BAR (bar);
6089 x_scroll_bar_remove (b);
6091 next = b->next;
6092 b->next = b->prev = Qnil;
6095 /* Now there should be no references to the condemned scroll bars,
6096 and they should get garbage-collected. */
6100 #ifndef USE_TOOLKIT_SCROLL_BARS
6101 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
6102 is a no-op when using toolkit scroll bars.
6104 This may be called from a signal handler, so we have to ignore GC
6105 mark bits. */
6107 static void
6108 x_scroll_bar_expose (struct scroll_bar *bar, const XEvent *event)
6110 Window w = bar->x_window;
6111 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6112 GC gc = f->output_data.x->normal_gc;
6114 block_input ();
6116 x_scroll_bar_set_handle (bar, bar->start, bar->end, 1);
6118 /* Switch to scroll bar foreground color. */
6119 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6120 XSetForeground (FRAME_X_DISPLAY (f), gc,
6121 f->output_data.x->scroll_bar_foreground_pixel);
6123 /* Draw a one-pixel border just inside the edges of the scroll bar. */
6124 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
6125 /* x, y, width, height */
6126 0, 0, bar->width - 1, bar->height - 1);
6128 /* Restore the foreground color of the GC if we changed it above. */
6129 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
6130 XSetForeground (FRAME_X_DISPLAY (f), gc,
6131 FRAME_FOREGROUND_PIXEL (f));
6133 unblock_input ();
6136 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6138 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
6139 is set to something other than NO_EVENT, it is enqueued.
6141 This may be called from a signal handler, so we have to ignore GC
6142 mark bits. */
6145 static void
6146 x_scroll_bar_handle_click (struct scroll_bar *bar,
6147 const XEvent *event,
6148 struct input_event *emacs_event)
6150 if (! WINDOWP (bar->window))
6151 emacs_abort ();
6153 emacs_event->kind = (bar->horizontal
6154 ? HORIZONTAL_SCROLL_BAR_CLICK_EVENT
6155 : SCROLL_BAR_CLICK_EVENT);
6156 emacs_event->code = event->xbutton.button - Button1;
6157 emacs_event->modifiers
6158 = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO
6159 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
6160 event->xbutton.state)
6161 | (event->type == ButtonRelease
6162 ? up_modifier
6163 : down_modifier));
6164 emacs_event->frame_or_window = bar->window;
6165 emacs_event->arg = Qnil;
6166 emacs_event->timestamp = event->xbutton.time;
6167 if (bar->horizontal)
6169 int left_range
6170 = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
6171 int x = event->xbutton.x - HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
6173 if (x < 0) x = 0;
6174 if (x > left_range) x = left_range;
6176 if (x < bar->start)
6177 emacs_event->part = scroll_bar_before_handle;
6178 else if (x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
6179 emacs_event->part = scroll_bar_horizontal_handle;
6180 else
6181 emacs_event->part = scroll_bar_after_handle;
6183 #ifndef USE_TOOLKIT_SCROLL_BARS
6184 /* If the user has released the handle, set it to its final position. */
6185 if (event->type == ButtonRelease && bar->dragging != -1)
6187 int new_start = y - bar->dragging;
6188 int new_end = new_start + bar->end - bar->start;
6190 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
6191 bar->dragging = -1;
6193 #endif
6195 XSETINT (emacs_event->x, left_range);
6196 XSETINT (emacs_event->y, x);
6198 else
6200 int top_range
6201 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
6202 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
6204 if (y < 0) y = 0;
6205 if (y > top_range) y = top_range;
6207 if (y < bar->start)
6208 emacs_event->part = scroll_bar_above_handle;
6209 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
6210 emacs_event->part = scroll_bar_handle;
6211 else
6212 emacs_event->part = scroll_bar_below_handle;
6214 #ifndef USE_TOOLKIT_SCROLL_BARS
6215 /* If the user has released the handle, set it to its final position. */
6216 if (event->type == ButtonRelease && bar->dragging != -1)
6218 int new_start = y - bar->dragging;
6219 int new_end = new_start + bar->end - bar->start;
6221 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
6222 bar->dragging = -1;
6224 #endif
6226 XSETINT (emacs_event->x, y);
6227 XSETINT (emacs_event->y, top_range);
6231 #ifndef USE_TOOLKIT_SCROLL_BARS
6233 /* Handle some mouse motion while someone is dragging the scroll bar.
6235 This may be called from a signal handler, so we have to ignore GC
6236 mark bits. */
6238 static void
6239 x_scroll_bar_note_movement (struct scroll_bar *bar,
6240 const XMotionEvent *event)
6242 struct frame *f = XFRAME (XWINDOW (bar->window)->frame);
6243 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
6245 dpyinfo->last_mouse_movement_time = event->time;
6246 dpyinfo->last_mouse_scroll_bar = bar;
6247 f->mouse_moved = 1;
6249 /* If we're dragging the bar, display it. */
6250 if (bar->dragging != -1)
6252 /* Where should the handle be now? */
6253 int new_start = event->y - bar->dragging;
6255 if (new_start != bar->start)
6257 int new_end = new_start + bar->end - bar->start;
6259 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
6264 #endif /* !USE_TOOLKIT_SCROLL_BARS */
6266 /* Return information to the user about the current position of the mouse
6267 on the scroll bar. */
6269 static void
6270 x_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
6271 enum scroll_bar_part *part, Lisp_Object *x,
6272 Lisp_Object *y, Time *timestamp)
6274 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
6275 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
6276 Window w = bar->x_window;
6277 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6278 int win_x, win_y;
6279 Window dummy_window;
6280 int dummy_coord;
6281 unsigned int dummy_mask;
6283 block_input ();
6285 /* Get the mouse's position relative to the scroll bar window, and
6286 report that. */
6287 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
6289 /* Root, child, root x and root y. */
6290 &dummy_window, &dummy_window,
6291 &dummy_coord, &dummy_coord,
6293 /* Position relative to scroll bar. */
6294 &win_x, &win_y,
6296 /* Mouse buttons and modifier keys. */
6297 &dummy_mask))
6299 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
6301 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
6303 if (bar->dragging != -1)
6304 win_y -= bar->dragging;
6306 if (win_y < 0)
6307 win_y = 0;
6308 if (win_y > top_range)
6309 win_y = top_range;
6311 *fp = f;
6312 *bar_window = bar->window;
6314 if (bar->dragging != -1)
6315 *part = scroll_bar_handle;
6316 else if (win_y < bar->start)
6317 *part = scroll_bar_above_handle;
6318 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
6319 *part = scroll_bar_handle;
6320 else
6321 *part = scroll_bar_below_handle;
6323 XSETINT (*x, win_y);
6324 XSETINT (*y, top_range);
6326 f->mouse_moved = 0;
6327 dpyinfo->last_mouse_scroll_bar = NULL;
6328 *timestamp = dpyinfo->last_mouse_movement_time;
6331 unblock_input ();
6335 /* Return information to the user about the current position of the mouse
6336 on the scroll bar. */
6338 static void
6339 x_horizontal_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
6340 enum scroll_bar_part *part, Lisp_Object *x,
6341 Lisp_Object *y, Time *timestamp)
6343 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
6344 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
6345 Window w = bar->x_window;
6346 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
6347 int win_x, win_y;
6348 Window dummy_window;
6349 int dummy_coord;
6350 unsigned int dummy_mask;
6352 block_input ();
6354 /* Get the mouse's position relative to the scroll bar window, and
6355 report that. */
6356 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
6358 /* Root, child, root x and root y. */
6359 &dummy_window, &dummy_window,
6360 &dummy_coord, &dummy_coord,
6362 /* Position relative to scroll bar. */
6363 &win_x, &win_y,
6365 /* Mouse buttons and modifier keys. */
6366 &dummy_mask))
6368 int left_range = HORIZONTAL_SCROLL_BAR_LEFT_RANGE (f, bar->width);
6370 win_x -= HORIZONTAL_SCROLL_BAR_LEFT_BORDER;
6372 if (bar->dragging != -1)
6373 win_x -= bar->dragging;
6375 if (win_x < 0)
6376 win_x = 0;
6377 if (win_x > left_range)
6378 win_x = left_range;
6380 *fp = f;
6381 *bar_window = bar->window;
6383 if (bar->dragging != -1)
6384 *part = scroll_bar_horizontal_handle;
6385 else if (win_x < bar->start)
6386 *part = scroll_bar_before_handle;
6387 else if (win_x < bar->end + HORIZONTAL_SCROLL_BAR_MIN_HANDLE)
6388 *part = scroll_bar_handle;
6389 else
6390 *part = scroll_bar_after_handle;
6392 XSETINT (*y, win_x);
6393 XSETINT (*x, left_range);
6395 f->mouse_moved = 0;
6396 dpyinfo->last_mouse_scroll_bar = NULL;
6397 *timestamp = dpyinfo->last_mouse_movement_time;
6400 unblock_input ();
6404 /* The screen has been cleared so we may have changed foreground or
6405 background colors, and the scroll bars may need to be redrawn.
6406 Clear out the scroll bars, and ask for expose events, so we can
6407 redraw them. */
6409 static void
6410 x_scroll_bar_clear (struct frame *f)
6412 #ifndef USE_TOOLKIT_SCROLL_BARS
6413 Lisp_Object bar;
6415 /* We can have scroll bars even if this is 0,
6416 if we just turned off scroll bar mode.
6417 But in that case we should not clear them. */
6418 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
6419 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
6420 bar = XSCROLL_BAR (bar)->next)
6421 XClearArea (FRAME_X_DISPLAY (f),
6422 XSCROLL_BAR (bar)->x_window,
6423 0, 0, 0, 0, True);
6424 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6427 #ifdef ENABLE_CHECKING
6429 /* Record the last 100 characters stored
6430 to help debug the loss-of-chars-during-GC problem. */
6432 static int temp_index;
6433 static short temp_buffer[100];
6435 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
6436 if (temp_index == ARRAYELTS (temp_buffer)) \
6437 temp_index = 0; \
6438 temp_buffer[temp_index++] = (keysym)
6440 #else /* not ENABLE_CHECKING */
6442 #define STORE_KEYSYM_FOR_DEBUG(keysym) ((void)0)
6444 #endif /* ENABLE_CHECKING */
6446 /* Set this to nonzero to fake an "X I/O error"
6447 on a particular display. */
6449 static struct x_display_info *XTread_socket_fake_io_error;
6451 /* When we find no input here, we occasionally do a no-op command
6452 to verify that the X server is still running and we can still talk with it.
6453 We try all the open displays, one by one.
6454 This variable is used for cycling thru the displays. */
6456 static struct x_display_info *next_noop_dpyinfo;
6458 enum
6460 X_EVENT_NORMAL,
6461 X_EVENT_GOTO_OUT,
6462 X_EVENT_DROP
6465 /* Filter events for the current X input method.
6466 DPYINFO is the display this event is for.
6467 EVENT is the X event to filter.
6469 Returns non-zero if the event was filtered, caller shall not process
6470 this event further.
6471 Returns zero if event is wasn't filtered. */
6473 #ifdef HAVE_X_I18N
6474 static int
6475 x_filter_event (struct x_display_info *dpyinfo, XEvent *event)
6477 /* XFilterEvent returns non-zero if the input method has
6478 consumed the event. We pass the frame's X window to
6479 XFilterEvent because that's the one for which the IC
6480 was created. */
6482 struct frame *f1 = x_any_window_to_frame (dpyinfo,
6483 event->xclient.window);
6485 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
6487 #endif
6489 #ifdef USE_GTK
6490 static int current_count;
6491 static int current_finish;
6492 static struct input_event *current_hold_quit;
6494 /* This is the filter function invoked by the GTK event loop.
6495 It is invoked before the XEvent is translated to a GdkEvent,
6496 so we have a chance to act on the event before GTK. */
6497 static GdkFilterReturn
6498 event_handler_gdk (GdkXEvent *gxev, GdkEvent *ev, gpointer data)
6500 XEvent *xev = (XEvent *) gxev;
6502 block_input ();
6503 if (current_count >= 0)
6505 struct x_display_info *dpyinfo;
6507 dpyinfo = x_display_info_for_display (xev->xany.display);
6509 #ifdef HAVE_X_I18N
6510 /* Filter events for the current X input method.
6511 GTK calls XFilterEvent but not for key press and release,
6512 so we do it here. */
6513 if ((xev->type == KeyPress || xev->type == KeyRelease)
6514 && dpyinfo
6515 && x_filter_event (dpyinfo, xev))
6517 unblock_input ();
6518 return GDK_FILTER_REMOVE;
6520 #endif
6522 if (! dpyinfo)
6523 current_finish = X_EVENT_NORMAL;
6524 else
6525 current_count
6526 += handle_one_xevent (dpyinfo, xev, &current_finish,
6527 current_hold_quit);
6529 else
6530 current_finish = x_dispatch_event (xev, xev->xany.display);
6532 unblock_input ();
6534 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
6535 return GDK_FILTER_REMOVE;
6537 return GDK_FILTER_CONTINUE;
6539 #endif /* USE_GTK */
6542 static void xembed_send_message (struct frame *f, Time,
6543 enum xembed_message,
6544 long detail, long data1, long data2);
6546 static void
6547 x_net_wm_state (struct frame *f, Window window)
6549 int value = FULLSCREEN_NONE;
6550 Lisp_Object lval = Qnil;
6551 int sticky = 0;
6553 (void)get_current_wm_state (f, window, &value, &sticky);
6555 switch (value)
6557 case FULLSCREEN_WIDTH:
6558 lval = Qfullwidth;
6559 break;
6560 case FULLSCREEN_HEIGHT:
6561 lval = Qfullheight;
6562 break;
6563 case FULLSCREEN_BOTH:
6564 lval = Qfullboth;
6565 break;
6566 case FULLSCREEN_MAXIMIZED:
6567 lval = Qmaximized;
6568 break;
6571 store_frame_param (f, Qfullscreen, lval);
6572 /** store_frame_param (f, Qsticky, sticky ? Qt : Qnil); **/
6575 /* Handles the XEvent EVENT on display DPYINFO.
6577 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
6578 *FINISH is zero if caller should continue reading events.
6579 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
6580 *EVENT is unchanged unless we're processing KeyPress event.
6582 We return the number of characters stored into the buffer. */
6584 static int
6585 handle_one_xevent (struct x_display_info *dpyinfo,
6586 const XEvent *event,
6587 int *finish, struct input_event *hold_quit)
6589 union {
6590 struct input_event ie;
6591 struct selection_input_event sie;
6592 } inev;
6593 int count = 0;
6594 int do_help = 0;
6595 ptrdiff_t nbytes = 0;
6596 struct frame *any, *f = NULL;
6597 struct coding_system coding;
6598 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
6599 /* This holds the state XLookupString needs to implement dead keys
6600 and other tricks known as "compose processing". _X Window System_
6601 says that a portable program can't use this, but Stephen Gildea assures
6602 me that letting the compiler initialize it to zeros will work okay. */
6603 static XComposeStatus compose_status;
6605 USE_SAFE_ALLOCA;
6607 *finish = X_EVENT_NORMAL;
6609 EVENT_INIT (inev.ie);
6610 inev.ie.kind = NO_EVENT;
6611 inev.ie.arg = Qnil;
6613 any = x_any_window_to_frame (dpyinfo, event->xany.window);
6615 if (any && any->wait_event_type == event->type)
6616 any->wait_event_type = 0; /* Indicates we got it. */
6618 switch (event->type)
6620 case ClientMessage:
6622 if (event->xclient.message_type == dpyinfo->Xatom_wm_protocols
6623 && event->xclient.format == 32)
6625 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_take_focus)
6627 /* Use the value returned by x_any_window_to_frame
6628 because this could be the shell widget window
6629 if the frame has no title bar. */
6630 f = any;
6631 #ifdef HAVE_X_I18N
6632 /* Not quite sure this is needed -pd */
6633 if (f && FRAME_XIC (f))
6634 XSetICFocus (FRAME_XIC (f));
6635 #endif
6636 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
6637 instructs the WM to set the input focus automatically for
6638 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
6639 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
6640 it has set the focus. So, XSetInputFocus below is not
6641 needed.
6643 The call to XSetInputFocus below has also caused trouble. In
6644 cases where the XSetInputFocus done by the WM and the one
6645 below are temporally close (on a fast machine), the call
6646 below can generate additional FocusIn events which confuse
6647 Emacs. */
6649 /* Since we set WM_TAKE_FOCUS, we must call
6650 XSetInputFocus explicitly. But not if f is null,
6651 since that might be an event for a deleted frame. */
6652 if (f)
6654 Display *d = event->xclient.display;
6655 /* Catch and ignore errors, in case window has been
6656 iconified by a window manager such as GWM. */
6657 x_catch_errors (d);
6658 XSetInputFocus (d, event->xclient.window,
6659 /* The ICCCM says this is
6660 the only valid choice. */
6661 RevertToParent,
6662 event->xclient.data.l[1]);
6663 /* This is needed to detect the error
6664 if there is an error. */
6665 XSync (d, False);
6666 x_uncatch_errors ();
6668 /* Not certain about handling scroll bars here */
6669 #endif /* 0 */
6670 goto done;
6673 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_save_yourself)
6675 /* Save state modify the WM_COMMAND property to
6676 something which can reinstate us. This notifies
6677 the session manager, who's looking for such a
6678 PropertyNotify. Can restart processing when
6679 a keyboard or mouse event arrives. */
6680 /* If we have a session manager, don't set this.
6681 KDE will then start two Emacsen, one for the
6682 session manager and one for this. */
6683 #ifdef HAVE_X_SM
6684 if (! x_session_have_connection ())
6685 #endif
6687 f = x_top_window_to_frame (dpyinfo,
6688 event->xclient.window);
6689 /* This is just so we only give real data once
6690 for a single Emacs process. */
6691 if (f == SELECTED_FRAME ())
6692 XSetCommand (FRAME_X_DISPLAY (f),
6693 event->xclient.window,
6694 initial_argv, initial_argc);
6695 else if (f)
6696 XSetCommand (FRAME_X_DISPLAY (f),
6697 event->xclient.window,
6698 0, 0);
6700 goto done;
6703 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_delete_window)
6705 f = any;
6706 if (!f)
6707 goto OTHER; /* May be a dialog that is to be removed */
6709 inev.ie.kind = DELETE_WINDOW_EVENT;
6710 XSETFRAME (inev.ie.frame_or_window, f);
6711 goto done;
6714 goto done;
6717 if (event->xclient.message_type == dpyinfo->Xatom_wm_configure_denied)
6718 goto done;
6720 if (event->xclient.message_type == dpyinfo->Xatom_wm_window_moved)
6722 int new_x, new_y;
6723 f = x_window_to_frame (dpyinfo, event->xclient.window);
6725 new_x = event->xclient.data.s[0];
6726 new_y = event->xclient.data.s[1];
6728 if (f)
6730 f->left_pos = new_x;
6731 f->top_pos = new_y;
6733 goto done;
6736 #ifdef HACK_EDITRES
6737 if (event->xclient.message_type == dpyinfo->Xatom_editres)
6739 f = any;
6740 if (f)
6741 _XEditResCheckMessages (f->output_data.x->widget,
6742 NULL, (XEvent *) event, NULL);
6743 goto done;
6745 #endif /* HACK_EDITRES */
6747 if (event->xclient.message_type == dpyinfo->Xatom_DONE
6748 || event->xclient.message_type == dpyinfo->Xatom_PAGE)
6750 /* Ghostview job completed. Kill it. We could
6751 reply with "Next" if we received "Page", but we
6752 currently never do because we are interested in
6753 images, only, which should have 1 page. */
6754 Pixmap pixmap = (Pixmap) event->xclient.data.l[1];
6755 f = x_window_to_frame (dpyinfo, event->xclient.window);
6756 if (!f)
6757 goto OTHER;
6758 x_kill_gs_process (pixmap, f);
6759 expose_frame (f, 0, 0, 0, 0);
6760 goto done;
6763 #ifdef USE_TOOLKIT_SCROLL_BARS
6764 /* Scroll bar callbacks send a ClientMessage from which
6765 we construct an input_event. */
6766 if (event->xclient.message_type == dpyinfo->Xatom_Scrollbar)
6768 x_scroll_bar_to_input_event (event, &inev.ie);
6769 *finish = X_EVENT_GOTO_OUT;
6770 goto done;
6772 else if (event->xclient.message_type == dpyinfo->Xatom_Horizontal_Scrollbar)
6774 x_horizontal_scroll_bar_to_input_event (event, &inev.ie);
6775 *finish = X_EVENT_GOTO_OUT;
6776 goto done;
6778 #endif /* USE_TOOLKIT_SCROLL_BARS */
6780 /* XEmbed messages from the embedder (if any). */
6781 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
6783 enum xembed_message msg = event->xclient.data.l[1];
6784 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
6785 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
6787 *finish = X_EVENT_GOTO_OUT;
6788 goto done;
6791 xft_settings_event (dpyinfo, event);
6793 f = any;
6794 if (!f)
6795 goto OTHER;
6796 if (x_handle_dnd_message (f, &event->xclient, dpyinfo, &inev.ie))
6797 *finish = X_EVENT_DROP;
6799 break;
6801 case SelectionNotify:
6802 dpyinfo->last_user_time = event->xselection.time;
6803 #ifdef USE_X_TOOLKIT
6804 if (! x_window_to_frame (dpyinfo, event->xselection.requestor))
6805 goto OTHER;
6806 #endif /* not USE_X_TOOLKIT */
6807 x_handle_selection_notify (&event->xselection);
6808 break;
6810 case SelectionClear: /* Someone has grabbed ownership. */
6811 dpyinfo->last_user_time = event->xselectionclear.time;
6812 #ifdef USE_X_TOOLKIT
6813 if (! x_window_to_frame (dpyinfo, event->xselectionclear.window))
6814 goto OTHER;
6815 #endif /* USE_X_TOOLKIT */
6817 const XSelectionClearEvent *eventp = &event->xselectionclear;
6819 inev.ie.kind = SELECTION_CLEAR_EVENT;
6820 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
6821 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6822 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6824 break;
6826 case SelectionRequest: /* Someone wants our selection. */
6827 dpyinfo->last_user_time = event->xselectionrequest.time;
6828 #ifdef USE_X_TOOLKIT
6829 if (!x_window_to_frame (dpyinfo, event->xselectionrequest.owner))
6830 goto OTHER;
6831 #endif /* USE_X_TOOLKIT */
6833 const XSelectionRequestEvent *eventp = &event->xselectionrequest;
6835 inev.ie.kind = SELECTION_REQUEST_EVENT;
6836 SELECTION_EVENT_DPYINFO (&inev.sie) = dpyinfo;
6837 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
6838 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6839 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
6840 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
6841 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6843 break;
6845 case PropertyNotify:
6846 dpyinfo->last_user_time = event->xproperty.time;
6847 f = x_top_window_to_frame (dpyinfo, event->xproperty.window);
6848 if (f && event->xproperty.atom == dpyinfo->Xatom_net_wm_state)
6849 if (x_handle_net_wm_state (f, &event->xproperty)
6850 && FRAME_ICONIFIED_P (f)
6851 && f->output_data.x->net_wm_state_hidden_seen)
6853 /* Gnome shell does not iconify us when C-z is pressed.
6854 It hides the frame. So if our state says we aren't
6855 hidden anymore, treat it as deiconified. */
6856 SET_FRAME_VISIBLE (f, 1);
6857 SET_FRAME_ICONIFIED (f, 0);
6858 f->output_data.x->has_been_visible = 1;
6859 f->output_data.x->net_wm_state_hidden_seen = 0;
6860 inev.ie.kind = DEICONIFY_EVENT;
6861 XSETFRAME (inev.ie.frame_or_window, f);
6864 x_handle_property_notify (&event->xproperty);
6865 xft_settings_event (dpyinfo, event);
6866 goto OTHER;
6868 case ReparentNotify:
6869 f = x_top_window_to_frame (dpyinfo, event->xreparent.window);
6870 if (f)
6872 f->output_data.x->parent_desc = event->xreparent.parent;
6873 x_real_positions (f, &f->left_pos, &f->top_pos);
6875 /* Perhaps reparented due to a WM restart. Reset this. */
6876 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6877 FRAME_DISPLAY_INFO (f)->net_supported_window = 0;
6879 x_set_frame_alpha (f);
6881 goto OTHER;
6883 case Expose:
6884 f = x_window_to_frame (dpyinfo, event->xexpose.window);
6885 if (f)
6887 if (!FRAME_VISIBLE_P (f))
6889 SET_FRAME_VISIBLE (f, 1);
6890 SET_FRAME_ICONIFIED (f, 0);
6891 f->output_data.x->has_been_visible = 1;
6892 SET_FRAME_GARBAGED (f);
6894 else
6896 #ifdef USE_GTK
6897 /* This seems to be needed for GTK 2.6 and later, see
6898 http://debbugs.gnu.org/cgi/bugreport.cgi?bug=15398. */
6899 x_clear_area (event->xexpose.display,
6900 event->xexpose.window,
6901 event->xexpose.x, event->xexpose.y,
6902 event->xexpose.width, event->xexpose.height);
6903 #endif
6904 expose_frame (f, event->xexpose.x, event->xexpose.y,
6905 event->xexpose.width, event->xexpose.height);
6908 else
6910 #ifndef USE_TOOLKIT_SCROLL_BARS
6911 struct scroll_bar *bar;
6912 #endif
6913 #if defined USE_LUCID
6914 /* Submenus of the Lucid menu bar aren't widgets
6915 themselves, so there's no way to dispatch events
6916 to them. Recognize this case separately. */
6918 Widget widget = x_window_to_menu_bar (event->xexpose.window);
6919 if (widget)
6920 xlwmenu_redisplay (widget);
6922 #endif /* USE_LUCID */
6924 #ifdef USE_TOOLKIT_SCROLL_BARS
6925 /* Dispatch event to the widget. */
6926 goto OTHER;
6927 #else /* not USE_TOOLKIT_SCROLL_BARS */
6928 bar = x_window_to_scroll_bar (event->xexpose.display,
6929 event->xexpose.window, 2);
6931 if (bar)
6932 x_scroll_bar_expose (bar, event);
6933 #ifdef USE_X_TOOLKIT
6934 else
6935 goto OTHER;
6936 #endif /* USE_X_TOOLKIT */
6937 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6939 break;
6941 case GraphicsExpose: /* This occurs when an XCopyArea's
6942 source area was obscured or not
6943 available. */
6944 f = x_window_to_frame (dpyinfo, event->xgraphicsexpose.drawable);
6945 if (f)
6946 expose_frame (f, event->xgraphicsexpose.x,
6947 event->xgraphicsexpose.y,
6948 event->xgraphicsexpose.width,
6949 event->xgraphicsexpose.height);
6950 #ifdef USE_X_TOOLKIT
6951 else
6952 goto OTHER;
6953 #endif /* USE_X_TOOLKIT */
6954 break;
6956 case NoExpose: /* This occurs when an XCopyArea's
6957 source area was completely
6958 available. */
6959 break;
6961 case UnmapNotify:
6962 /* Redo the mouse-highlight after the tooltip has gone. */
6963 if (event->xunmap.window == tip_window)
6965 tip_window = 0;
6966 x_redo_mouse_highlight (dpyinfo);
6969 f = x_top_window_to_frame (dpyinfo, event->xunmap.window);
6970 if (f) /* F may no longer exist if
6971 the frame was deleted. */
6973 bool visible = FRAME_VISIBLE_P (f);
6974 /* While a frame is unmapped, display generation is
6975 disabled; you don't want to spend time updating a
6976 display that won't ever be seen. */
6977 SET_FRAME_VISIBLE (f, 0);
6978 /* We can't distinguish, from the event, whether the window
6979 has become iconified or invisible. So assume, if it
6980 was previously visible, than now it is iconified.
6981 But x_make_frame_invisible clears both
6982 the visible flag and the iconified flag;
6983 and that way, we know the window is not iconified now. */
6984 if (visible || FRAME_ICONIFIED_P (f))
6986 SET_FRAME_ICONIFIED (f, 1);
6987 inev.ie.kind = ICONIFY_EVENT;
6988 XSETFRAME (inev.ie.frame_or_window, f);
6991 goto OTHER;
6993 case MapNotify:
6994 if (event->xmap.window == tip_window)
6995 /* The tooltip has been drawn already. Avoid
6996 the SET_FRAME_GARBAGED below. */
6997 goto OTHER;
6999 /* We use x_top_window_to_frame because map events can
7000 come for sub-windows and they don't mean that the
7001 frame is visible. */
7002 f = x_top_window_to_frame (dpyinfo, event->xmap.window);
7003 if (f)
7005 bool iconified = FRAME_ICONIFIED_P (f);
7007 /* Check if fullscreen was specified before we where mapped the
7008 first time, i.e. from the command line. */
7009 if (!f->output_data.x->has_been_visible)
7010 x_check_fullscreen (f);
7012 SET_FRAME_VISIBLE (f, 1);
7013 SET_FRAME_ICONIFIED (f, 0);
7014 f->output_data.x->has_been_visible = 1;
7016 if (iconified)
7018 inev.ie.kind = DEICONIFY_EVENT;
7019 XSETFRAME (inev.ie.frame_or_window, f);
7021 else if (! NILP (Vframe_list) && ! NILP (XCDR (Vframe_list)))
7022 /* Force a redisplay sooner or later to update the
7023 frame titles in case this is the second frame. */
7024 record_asynch_buffer_change ();
7026 #ifdef USE_GTK
7027 xg_frame_resized (f, -1, -1);
7028 #endif
7030 goto OTHER;
7032 case KeyPress:
7034 dpyinfo->last_user_time = event->xkey.time;
7035 ignore_next_mouse_click_timeout = 0;
7037 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
7038 /* Dispatch KeyPress events when in menu. */
7039 if (popup_activated ())
7040 goto OTHER;
7041 #endif
7043 f = any;
7045 #if ! defined (USE_GTK)
7046 /* If mouse-highlight is an integer, input clears out
7047 mouse highlighting. */
7048 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
7049 && (f == 0
7050 || !EQ (f->tool_bar_window, hlinfo->mouse_face_window)))
7052 clear_mouse_face (hlinfo);
7053 hlinfo->mouse_face_hidden = 1;
7055 #endif
7057 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
7058 if (f == 0)
7060 /* Scroll bars consume key events, but we want
7061 the keys to go to the scroll bar's frame. */
7062 Widget widget = XtWindowToWidget (dpyinfo->display,
7063 event->xkey.window);
7064 if (widget && XmIsScrollBar (widget))
7066 widget = XtParent (widget);
7067 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
7070 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
7072 if (f != 0)
7074 KeySym keysym, orig_keysym;
7075 /* al%imercury@uunet.uu.net says that making this 81
7076 instead of 80 fixed a bug whereby meta chars made
7077 his Emacs hang.
7079 It seems that some version of XmbLookupString has
7080 a bug of not returning XBufferOverflow in
7081 status_return even if the input is too long to
7082 fit in 81 bytes. So, we must prepare sufficient
7083 bytes for copy_buffer. 513 bytes (256 chars for
7084 two-byte character set) seems to be a fairly good
7085 approximation. -- 2000.8.10 handa@etl.go.jp */
7086 unsigned char copy_buffer[513];
7087 unsigned char *copy_bufptr = copy_buffer;
7088 int copy_bufsiz = sizeof (copy_buffer);
7089 int modifiers;
7090 Lisp_Object coding_system = Qlatin_1;
7091 Lisp_Object c;
7092 /* Event will be modified. */
7093 XKeyEvent xkey = event->xkey;
7095 #ifdef USE_GTK
7096 /* Don't pass keys to GTK. A Tab will shift focus to the
7097 tool bar in GTK 2.4. Keys will still go to menus and
7098 dialogs because in that case popup_activated is nonzero
7099 (see above). */
7100 *finish = X_EVENT_DROP;
7101 #endif
7103 xkey.state |= x_emacs_to_x_modifiers (FRAME_DISPLAY_INFO (f),
7104 extra_keyboard_modifiers);
7105 modifiers = xkey.state;
7107 /* This will have to go some day... */
7109 /* make_lispy_event turns chars into control chars.
7110 Don't do it here because XLookupString is too eager. */
7111 xkey.state &= ~ControlMask;
7112 xkey.state &= ~(dpyinfo->meta_mod_mask
7113 | dpyinfo->super_mod_mask
7114 | dpyinfo->hyper_mod_mask
7115 | dpyinfo->alt_mod_mask);
7117 /* In case Meta is ComposeCharacter,
7118 clear its status. According to Markus Ehrnsperger
7119 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
7120 this enables ComposeCharacter to work whether or
7121 not it is combined with Meta. */
7122 if (modifiers & dpyinfo->meta_mod_mask)
7123 memset (&compose_status, 0, sizeof (compose_status));
7125 #ifdef HAVE_X_I18N
7126 if (FRAME_XIC (f))
7128 Status status_return;
7130 coding_system = Vlocale_coding_system;
7131 nbytes = XmbLookupString (FRAME_XIC (f),
7132 &xkey, (char *) copy_bufptr,
7133 copy_bufsiz, &keysym,
7134 &status_return);
7135 if (status_return == XBufferOverflow)
7137 copy_bufsiz = nbytes + 1;
7138 copy_bufptr = alloca (copy_bufsiz);
7139 nbytes = XmbLookupString (FRAME_XIC (f),
7140 &xkey, (char *) copy_bufptr,
7141 copy_bufsiz, &keysym,
7142 &status_return);
7144 /* Xutf8LookupString is a new but already deprecated interface. -stef */
7145 if (status_return == XLookupNone)
7146 break;
7147 else if (status_return == XLookupChars)
7149 keysym = NoSymbol;
7150 modifiers = 0;
7152 else if (status_return != XLookupKeySym
7153 && status_return != XLookupBoth)
7154 emacs_abort ();
7156 else
7157 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
7158 copy_bufsiz, &keysym,
7159 &compose_status);
7160 #else
7161 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
7162 copy_bufsiz, &keysym,
7163 &compose_status);
7164 #endif
7166 /* If not using XIM/XIC, and a compose sequence is in progress,
7167 we break here. Otherwise, chars_matched is always 0. */
7168 if (compose_status.chars_matched > 0 && nbytes == 0)
7169 break;
7171 memset (&compose_status, 0, sizeof (compose_status));
7172 orig_keysym = keysym;
7174 /* Common for all keysym input events. */
7175 XSETFRAME (inev.ie.frame_or_window, f);
7176 inev.ie.modifiers
7177 = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), modifiers);
7178 inev.ie.timestamp = xkey.time;
7180 /* First deal with keysyms which have defined
7181 translations to characters. */
7182 if (keysym >= 32 && keysym < 128)
7183 /* Avoid explicitly decoding each ASCII character. */
7185 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
7186 inev.ie.code = keysym;
7187 goto done_keysym;
7190 /* Keysyms directly mapped to Unicode characters. */
7191 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
7193 if (keysym < 0x01000080)
7194 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
7195 else
7196 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
7197 inev.ie.code = keysym & 0xFFFFFF;
7198 goto done_keysym;
7201 /* Now non-ASCII. */
7202 if (HASH_TABLE_P (Vx_keysym_table)
7203 && (c = Fgethash (make_number (keysym),
7204 Vx_keysym_table,
7205 Qnil),
7206 NATNUMP (c)))
7208 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
7209 ? ASCII_KEYSTROKE_EVENT
7210 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
7211 inev.ie.code = XFASTINT (c);
7212 goto done_keysym;
7215 /* Random non-modifier sorts of keysyms. */
7216 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
7217 || keysym == XK_Delete
7218 #ifdef XK_ISO_Left_Tab
7219 || (keysym >= XK_ISO_Left_Tab
7220 && keysym <= XK_ISO_Enter)
7221 #endif
7222 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
7223 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
7224 #ifdef HPUX
7225 /* This recognizes the "extended function
7226 keys". It seems there's no cleaner way.
7227 Test IsModifierKey to avoid handling
7228 mode_switch incorrectly. */
7229 || (XK_Select <= keysym && keysym < XK_KP_Space)
7230 #endif
7231 #ifdef XK_dead_circumflex
7232 || orig_keysym == XK_dead_circumflex
7233 #endif
7234 #ifdef XK_dead_grave
7235 || orig_keysym == XK_dead_grave
7236 #endif
7237 #ifdef XK_dead_tilde
7238 || orig_keysym == XK_dead_tilde
7239 #endif
7240 #ifdef XK_dead_diaeresis
7241 || orig_keysym == XK_dead_diaeresis
7242 #endif
7243 #ifdef XK_dead_macron
7244 || orig_keysym == XK_dead_macron
7245 #endif
7246 #ifdef XK_dead_degree
7247 || orig_keysym == XK_dead_degree
7248 #endif
7249 #ifdef XK_dead_acute
7250 || orig_keysym == XK_dead_acute
7251 #endif
7252 #ifdef XK_dead_cedilla
7253 || orig_keysym == XK_dead_cedilla
7254 #endif
7255 #ifdef XK_dead_breve
7256 || orig_keysym == XK_dead_breve
7257 #endif
7258 #ifdef XK_dead_ogonek
7259 || orig_keysym == XK_dead_ogonek
7260 #endif
7261 #ifdef XK_dead_caron
7262 || orig_keysym == XK_dead_caron
7263 #endif
7264 #ifdef XK_dead_doubleacute
7265 || orig_keysym == XK_dead_doubleacute
7266 #endif
7267 #ifdef XK_dead_abovedot
7268 || orig_keysym == XK_dead_abovedot
7269 #endif
7270 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
7271 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
7272 /* Any "vendor-specific" key is ok. */
7273 || (orig_keysym & (1 << 28))
7274 || (keysym != NoSymbol && nbytes == 0))
7275 && ! (IsModifierKey (orig_keysym)
7276 /* The symbols from XK_ISO_Lock
7277 to XK_ISO_Last_Group_Lock
7278 don't have real modifiers but
7279 should be treated similarly to
7280 Mode_switch by Emacs. */
7281 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
7282 || (XK_ISO_Lock <= orig_keysym
7283 && orig_keysym <= XK_ISO_Last_Group_Lock)
7284 #endif
7287 STORE_KEYSYM_FOR_DEBUG (keysym);
7288 /* make_lispy_event will convert this to a symbolic
7289 key. */
7290 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
7291 inev.ie.code = keysym;
7292 goto done_keysym;
7295 { /* Raw bytes, not keysym. */
7296 ptrdiff_t i;
7297 int nchars, len;
7299 for (i = 0, nchars = 0; i < nbytes; i++)
7301 if (ASCII_CHAR_P (copy_bufptr[i]))
7302 nchars++;
7303 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
7306 if (nchars < nbytes)
7308 /* Decode the input data. */
7310 /* The input should be decoded with `coding_system'
7311 which depends on which X*LookupString function
7312 we used just above and the locale. */
7313 setup_coding_system (coding_system, &coding);
7314 coding.src_multibyte = 0;
7315 coding.dst_multibyte = 1;
7316 /* The input is converted to events, thus we can't
7317 handle composition. Anyway, there's no XIM that
7318 gives us composition information. */
7319 coding.common_flags &= ~CODING_ANNOTATION_MASK;
7321 SAFE_NALLOCA (coding.destination, MAX_MULTIBYTE_LENGTH,
7322 nbytes);
7323 coding.dst_bytes = MAX_MULTIBYTE_LENGTH * nbytes;
7324 coding.mode |= CODING_MODE_LAST_BLOCK;
7325 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
7326 nbytes = coding.produced;
7327 nchars = coding.produced_char;
7328 copy_bufptr = coding.destination;
7331 /* Convert the input data to a sequence of
7332 character events. */
7333 for (i = 0; i < nbytes; i += len)
7335 int ch;
7336 if (nchars == nbytes)
7337 ch = copy_bufptr[i], len = 1;
7338 else
7339 ch = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
7340 inev.ie.kind = (SINGLE_BYTE_CHAR_P (ch)
7341 ? ASCII_KEYSTROKE_EVENT
7342 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
7343 inev.ie.code = ch;
7344 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
7347 count += nchars;
7349 inev.ie.kind = NO_EVENT; /* Already stored above. */
7351 if (keysym == NoSymbol)
7352 break;
7354 /* FIXME: check side effects and remove this. */
7355 ((XEvent *) event)->xkey = xkey;
7357 done_keysym:
7358 #ifdef HAVE_X_I18N
7359 /* Don't dispatch this event since XtDispatchEvent calls
7360 XFilterEvent, and two calls in a row may freeze the
7361 client. */
7362 break;
7363 #else
7364 goto OTHER;
7365 #endif
7367 case KeyRelease:
7368 dpyinfo->last_user_time = event->xkey.time;
7369 #ifdef HAVE_X_I18N
7370 /* Don't dispatch this event since XtDispatchEvent calls
7371 XFilterEvent, and two calls in a row may freeze the
7372 client. */
7373 break;
7374 #else
7375 goto OTHER;
7376 #endif
7378 case EnterNotify:
7379 dpyinfo->last_user_time = event->xcrossing.time;
7380 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7382 f = any;
7384 if (f && x_mouse_click_focus_ignore_position)
7385 ignore_next_mouse_click_timeout = event->xmotion.time + 200;
7387 /* EnterNotify counts as mouse movement,
7388 so update things that depend on mouse position. */
7389 if (f && !f->output_data.x->hourglass_p)
7390 note_mouse_movement (f, &event->xmotion);
7391 #ifdef USE_GTK
7392 /* We may get an EnterNotify on the buttons in the toolbar. In that
7393 case we moved out of any highlighted area and need to note this. */
7394 if (!f && dpyinfo->last_mouse_glyph_frame)
7395 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
7396 #endif
7397 goto OTHER;
7399 case FocusIn:
7400 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7401 goto OTHER;
7403 case LeaveNotify:
7404 dpyinfo->last_user_time = event->xcrossing.time;
7405 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7407 f = x_top_window_to_frame (dpyinfo, event->xcrossing.window);
7408 if (f)
7410 if (f == hlinfo->mouse_face_mouse_frame)
7412 /* If we move outside the frame, then we're
7413 certainly no longer on any text in the frame. */
7414 clear_mouse_face (hlinfo);
7415 hlinfo->mouse_face_mouse_frame = 0;
7418 /* Generate a nil HELP_EVENT to cancel a help-echo.
7419 Do it only if there's something to cancel.
7420 Otherwise, the startup message is cleared when
7421 the mouse leaves the frame. */
7422 if (any_help_event_p)
7423 do_help = -1;
7425 #ifdef USE_GTK
7426 /* See comment in EnterNotify above */
7427 else if (dpyinfo->last_mouse_glyph_frame)
7428 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
7429 #endif
7430 goto OTHER;
7432 case FocusOut:
7433 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
7434 goto OTHER;
7436 case MotionNotify:
7438 dpyinfo->last_user_time = event->xmotion.time;
7439 previous_help_echo_string = help_echo_string;
7440 help_echo_string = Qnil;
7442 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
7443 : x_window_to_frame (dpyinfo, event->xmotion.window));
7445 if (hlinfo->mouse_face_hidden)
7447 hlinfo->mouse_face_hidden = 0;
7448 clear_mouse_face (hlinfo);
7451 #ifdef USE_GTK
7452 if (f && xg_event_is_for_scrollbar (f, event))
7453 f = 0;
7454 #endif
7455 if (f)
7458 /* Generate SELECT_WINDOW_EVENTs when needed.
7459 Don't let popup menus influence things (bug#1261). */
7460 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
7462 static Lisp_Object last_mouse_window;
7463 Lisp_Object window = window_from_coordinates
7464 (f, event->xmotion.x, event->xmotion.y, 0, 0);
7466 /* Window will be selected only when it is not selected now and
7467 last mouse movement event was not in it. Minibuffer window
7468 will be selected only when it is active. */
7469 if (WINDOWP (window)
7470 && !EQ (window, last_mouse_window)
7471 && !EQ (window, selected_window)
7472 /* For click-to-focus window managers
7473 create event iff we don't leave the
7474 selected frame. */
7475 && (focus_follows_mouse
7476 || (EQ (XWINDOW (window)->frame,
7477 XWINDOW (selected_window)->frame))))
7479 inev.ie.kind = SELECT_WINDOW_EVENT;
7480 inev.ie.frame_or_window = window;
7482 /* Remember the last window where we saw the mouse. */
7483 last_mouse_window = window;
7485 if (!note_mouse_movement (f, &event->xmotion))
7486 help_echo_string = previous_help_echo_string;
7488 else
7490 #ifndef USE_TOOLKIT_SCROLL_BARS
7491 struct scroll_bar *bar
7492 = x_window_to_scroll_bar (event->xmotion.display,
7493 event->xmotion.window, 2);
7495 if (bar)
7496 x_scroll_bar_note_movement (bar, &event->xmotion);
7497 #endif /* USE_TOOLKIT_SCROLL_BARS */
7499 /* If we move outside the frame, then we're
7500 certainly no longer on any text in the frame. */
7501 clear_mouse_face (hlinfo);
7504 /* If the contents of the global variable help_echo_string
7505 has changed, generate a HELP_EVENT. */
7506 if (!NILP (help_echo_string)
7507 || !NILP (previous_help_echo_string))
7508 do_help = 1;
7509 goto OTHER;
7512 case ConfigureNotify:
7513 f = x_top_window_to_frame (dpyinfo, event->xconfigure.window);
7514 #ifdef USE_GTK
7515 if (!f
7516 && (f = any)
7517 && event->xconfigure.window == FRAME_X_WINDOW (f))
7519 xg_frame_resized (f, event->xconfigure.width,
7520 event->xconfigure.height);
7521 f = 0;
7523 #endif
7524 if (f)
7526 x_net_wm_state (f, event->xconfigure.window);
7528 #ifndef USE_X_TOOLKIT
7529 #ifndef USE_GTK
7530 int width = FRAME_PIXEL_TO_TEXT_WIDTH (f, event->xconfigure.width);
7531 int height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, event->xconfigure.height);
7533 /* In the toolkit version, change_frame_size
7534 is called by the code that handles resizing
7535 of the EmacsFrame widget. */
7537 /* Even if the number of character rows and columns has
7538 not changed, the font size may have changed, so we need
7539 to check the pixel dimensions as well. */
7540 if (width != FRAME_TEXT_WIDTH (f)
7541 || height != FRAME_TEXT_HEIGHT (f)
7542 || event->xconfigure.width != FRAME_PIXEL_WIDTH (f)
7543 || event->xconfigure.height != FRAME_PIXEL_HEIGHT (f))
7545 change_frame_size (f, width, height, 0, 1, 0, 1);
7546 x_clear_under_internal_border (f);
7547 SET_FRAME_GARBAGED (f);
7548 cancel_mouse_face (f);
7551 /** FRAME_PIXEL_WIDTH (f) = event->xconfigure.width; **/
7552 /** FRAME_PIXEL_HEIGHT (f) = event->xconfigure.height; **/
7553 #endif /* not USE_GTK */
7554 #endif
7556 #ifdef USE_GTK
7557 /* GTK creates windows but doesn't map them.
7558 Only get real positions when mapped. */
7559 if (FRAME_GTK_OUTER_WIDGET (f)
7560 && gtk_widget_get_mapped (FRAME_GTK_OUTER_WIDGET (f)))
7561 #endif
7562 x_real_positions (f, &f->left_pos, &f->top_pos);
7564 #ifdef HAVE_X_I18N
7565 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
7566 xic_set_statusarea (f);
7567 #endif
7570 goto OTHER;
7572 case ButtonRelease:
7573 case ButtonPress:
7575 /* If we decide we want to generate an event to be seen
7576 by the rest of Emacs, we put it here. */
7577 bool tool_bar_p = 0;
7579 memset (&compose_status, 0, sizeof (compose_status));
7580 dpyinfo->last_mouse_glyph_frame = NULL;
7581 dpyinfo->last_user_time = event->xbutton.time;
7583 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
7584 : x_window_to_frame (dpyinfo, event->xbutton.window));
7586 #ifdef USE_GTK
7587 if (f && xg_event_is_for_scrollbar (f, event))
7588 f = 0;
7589 #endif
7590 if (f)
7592 #if ! defined (USE_GTK)
7593 /* Is this in the tool-bar? */
7594 if (WINDOWP (f->tool_bar_window)
7595 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
7597 Lisp_Object window;
7598 int x = event->xbutton.x;
7599 int y = event->xbutton.y;
7601 window = window_from_coordinates (f, x, y, 0, 1);
7602 tool_bar_p = EQ (window, f->tool_bar_window);
7604 if (tool_bar_p && event->xbutton.button < 4)
7605 handle_tool_bar_click
7606 (f, x, y, event->xbutton.type == ButtonPress,
7607 x_x_to_emacs_modifiers (dpyinfo, event->xbutton.state));
7609 #endif /* !USE_GTK */
7611 if (!tool_bar_p)
7612 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
7613 if (! popup_activated ())
7614 #endif
7616 if (ignore_next_mouse_click_timeout)
7618 if (event->type == ButtonPress
7619 && event->xbutton.time > ignore_next_mouse_click_timeout)
7621 ignore_next_mouse_click_timeout = 0;
7622 construct_mouse_click (&inev.ie, &event->xbutton, f);
7624 if (event->type == ButtonRelease)
7625 ignore_next_mouse_click_timeout = 0;
7627 else
7628 construct_mouse_click (&inev.ie, &event->xbutton, f);
7630 if (FRAME_X_EMBEDDED_P (f))
7631 xembed_send_message (f, event->xbutton.time,
7632 XEMBED_REQUEST_FOCUS, 0, 0, 0);
7634 else
7636 struct scroll_bar *bar
7637 = x_window_to_scroll_bar (event->xbutton.display,
7638 event->xbutton.window, 2);
7640 #ifdef USE_TOOLKIT_SCROLL_BARS
7641 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
7642 scroll bars. */
7643 if (bar && event->xbutton.state & ControlMask)
7645 x_scroll_bar_handle_click (bar, event, &inev.ie);
7646 *finish = X_EVENT_DROP;
7648 #else /* not USE_TOOLKIT_SCROLL_BARS */
7649 if (bar)
7650 x_scroll_bar_handle_click (bar, event, &inev.ie);
7651 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7654 if (event->type == ButtonPress)
7656 dpyinfo->grabbed |= (1 << event->xbutton.button);
7657 dpyinfo->last_mouse_frame = f;
7658 #if ! defined (USE_GTK)
7659 if (f && !tool_bar_p)
7660 f->last_tool_bar_item = -1;
7661 #endif /* not USE_GTK */
7663 else
7664 dpyinfo->grabbed &= ~(1 << event->xbutton.button);
7666 /* Ignore any mouse motion that happened before this event;
7667 any subsequent mouse-movement Emacs events should reflect
7668 only motion after the ButtonPress/Release. */
7669 if (f != 0)
7670 f->mouse_moved = 0;
7672 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
7673 f = x_menubar_window_to_frame (dpyinfo, event);
7674 /* For a down-event in the menu bar,
7675 don't pass it to Xt right now.
7676 Instead, save it away
7677 and we will pass it to Xt from kbd_buffer_get_event.
7678 That way, we can run some Lisp code first. */
7679 if (! popup_activated ()
7680 #ifdef USE_GTK
7681 /* Gtk+ menus only react to the first three buttons. */
7682 && event->xbutton.button < 3
7683 #endif
7684 && f && event->type == ButtonPress
7685 /* Verify the event is really within the menu bar
7686 and not just sent to it due to grabbing. */
7687 && event->xbutton.x >= 0
7688 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
7689 && event->xbutton.y >= 0
7690 && event->xbutton.y < FRAME_MENUBAR_HEIGHT (f)
7691 && event->xbutton.same_screen)
7693 if (!f->output_data.x->saved_menu_event)
7694 f->output_data.x->saved_menu_event = xmalloc (sizeof *event);
7695 *f->output_data.x->saved_menu_event = *event;
7696 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT;
7697 XSETFRAME (inev.ie.frame_or_window, f);
7698 *finish = X_EVENT_DROP;
7700 else
7701 goto OTHER;
7702 #endif /* USE_X_TOOLKIT || USE_GTK */
7704 break;
7706 case CirculateNotify:
7707 goto OTHER;
7709 case CirculateRequest:
7710 goto OTHER;
7712 case VisibilityNotify:
7713 goto OTHER;
7715 case MappingNotify:
7716 /* Someone has changed the keyboard mapping - update the
7717 local cache. */
7718 switch (event->xmapping.request)
7720 case MappingModifier:
7721 x_find_modifier_meanings (dpyinfo);
7722 /* This is meant to fall through. */
7723 case MappingKeyboard:
7724 XRefreshKeyboardMapping ((XMappingEvent *) &event->xmapping);
7726 goto OTHER;
7728 case DestroyNotify:
7729 xft_settings_event (dpyinfo, event);
7730 break;
7732 default:
7733 OTHER:
7734 #ifdef USE_X_TOOLKIT
7735 block_input ();
7736 if (*finish != X_EVENT_DROP)
7737 XtDispatchEvent ((XEvent *) event);
7738 unblock_input ();
7739 #endif /* USE_X_TOOLKIT */
7740 break;
7743 done:
7744 if (inev.ie.kind != NO_EVENT)
7746 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
7747 count++;
7750 if (do_help
7751 && !(hold_quit && hold_quit->kind != NO_EVENT))
7753 Lisp_Object frame;
7755 if (f)
7756 XSETFRAME (frame, f);
7757 else
7758 frame = Qnil;
7760 if (do_help > 0)
7762 any_help_event_p = 1;
7763 gen_help_event (help_echo_string, frame, help_echo_window,
7764 help_echo_object, help_echo_pos);
7766 else
7768 help_echo_string = Qnil;
7769 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
7771 count++;
7774 SAFE_FREE ();
7775 return count;
7778 /* Handles the XEvent EVENT on display DISPLAY.
7779 This is used for event loops outside the normal event handling,
7780 i.e. looping while a popup menu or a dialog is posted.
7782 Returns the value handle_one_xevent sets in the finish argument. */
7784 x_dispatch_event (XEvent *event, Display *display)
7786 struct x_display_info *dpyinfo;
7787 int finish = X_EVENT_NORMAL;
7789 dpyinfo = x_display_info_for_display (display);
7791 if (dpyinfo)
7792 handle_one_xevent (dpyinfo, event, &finish, 0);
7794 return finish;
7797 /* Read events coming from the X server.
7798 Return as soon as there are no more events to be read.
7800 Return the number of characters stored into the buffer,
7801 thus pretending to be `read' (except the characters we store
7802 in the keyboard buffer can be multibyte, so are not necessarily
7803 C chars). */
7805 static int
7806 XTread_socket (struct terminal *terminal, struct input_event *hold_quit)
7808 int count = 0;
7809 int event_found = 0;
7810 struct x_display_info *dpyinfo = terminal->display_info.x;
7812 block_input ();
7814 /* For debugging, this gives a way to fake an I/O error. */
7815 if (dpyinfo == XTread_socket_fake_io_error)
7817 XTread_socket_fake_io_error = 0;
7818 x_io_error_quitter (dpyinfo->display);
7821 #ifndef USE_GTK
7822 while (XPending (dpyinfo->display))
7824 int finish;
7825 XEvent event;
7827 XNextEvent (dpyinfo->display, &event);
7829 #ifdef HAVE_X_I18N
7830 /* Filter events for the current X input method. */
7831 if (x_filter_event (dpyinfo, &event))
7832 continue;
7833 #endif
7834 event_found = 1;
7836 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
7838 if (finish == X_EVENT_GOTO_OUT)
7839 break;
7842 #else /* USE_GTK */
7844 /* For GTK we must use the GTK event loop. But XEvents gets passed
7845 to our filter function above, and then to the big event switch.
7846 We use a bunch of globals to communicate with our filter function,
7847 that is kind of ugly, but it works.
7849 There is no way to do one display at the time, GTK just does events
7850 from all displays. */
7852 while (gtk_events_pending ())
7854 current_count = count;
7855 current_hold_quit = hold_quit;
7857 gtk_main_iteration ();
7859 count = current_count;
7860 current_count = -1;
7861 current_hold_quit = 0;
7863 if (current_finish == X_EVENT_GOTO_OUT)
7864 break;
7866 #endif /* USE_GTK */
7868 /* On some systems, an X bug causes Emacs to get no more events
7869 when the window is destroyed. Detect that. (1994.) */
7870 if (! event_found)
7872 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7873 One XNOOP in 100 loops will make Emacs terminate.
7874 B. Bretthauer, 1994 */
7875 x_noop_count++;
7876 if (x_noop_count >= 100)
7878 x_noop_count=0;
7880 if (next_noop_dpyinfo == 0)
7881 next_noop_dpyinfo = x_display_list;
7883 XNoOp (next_noop_dpyinfo->display);
7885 /* Each time we get here, cycle through the displays now open. */
7886 next_noop_dpyinfo = next_noop_dpyinfo->next;
7890 /* If the focus was just given to an auto-raising frame,
7891 raise it now. FIXME: handle more than one such frame. */
7892 if (dpyinfo->x_pending_autoraise_frame)
7894 x_raise_frame (dpyinfo->x_pending_autoraise_frame);
7895 dpyinfo->x_pending_autoraise_frame = NULL;
7898 unblock_input ();
7900 return count;
7906 /***********************************************************************
7907 Text Cursor
7908 ***********************************************************************/
7910 /* Set clipping for output in glyph row ROW. W is the window in which
7911 we operate. GC is the graphics context to set clipping in.
7913 ROW may be a text row or, e.g., a mode line. Text rows must be
7914 clipped to the interior of the window dedicated to text display,
7915 mode lines must be clipped to the whole window. */
7917 static void
7918 x_clip_to_row (struct window *w, struct glyph_row *row,
7919 enum glyph_row_area area, GC gc)
7921 struct frame *f = XFRAME (WINDOW_FRAME (w));
7922 XRectangle clip_rect;
7923 int window_x, window_y, window_width;
7925 window_box (w, area, &window_x, &window_y, &window_width, 0);
7927 clip_rect.x = window_x;
7928 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7929 clip_rect.y = max (clip_rect.y, window_y);
7930 clip_rect.width = window_width;
7931 clip_rect.height = row->visible_height;
7933 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7937 /* Draw a hollow box cursor on window W in glyph row ROW. */
7939 static void
7940 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
7942 struct frame *f = XFRAME (WINDOW_FRAME (w));
7943 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
7944 Display *dpy = FRAME_X_DISPLAY (f);
7945 int x, y, wd, h;
7946 XGCValues xgcv;
7947 struct glyph *cursor_glyph;
7948 GC gc;
7950 /* Get the glyph the cursor is on. If we can't tell because
7951 the current matrix is invalid or such, give up. */
7952 cursor_glyph = get_phys_cursor_glyph (w);
7953 if (cursor_glyph == NULL)
7954 return;
7956 /* Compute frame-relative coordinates for phys cursor. */
7957 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7958 wd = w->phys_cursor_width;
7960 /* The foreground of cursor_gc is typically the same as the normal
7961 background color, which can cause the cursor box to be invisible. */
7962 xgcv.foreground = f->output_data.x->cursor_pixel;
7963 if (dpyinfo->scratch_cursor_gc)
7964 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7965 else
7966 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7967 GCForeground, &xgcv);
7968 gc = dpyinfo->scratch_cursor_gc;
7970 /* Set clipping, draw the rectangle, and reset clipping again. */
7971 x_clip_to_row (w, row, TEXT_AREA, gc);
7972 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7973 XSetClipMask (dpy, gc, None);
7977 /* Draw a bar cursor on window W in glyph row ROW.
7979 Implementation note: One would like to draw a bar cursor with an
7980 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7981 Unfortunately, I didn't find a font yet that has this property set.
7982 --gerd. */
7984 static void
7985 x_draw_bar_cursor (struct window *w, struct glyph_row *row, int width, enum text_cursor_kinds kind)
7987 struct frame *f = XFRAME (w->frame);
7988 struct glyph *cursor_glyph;
7990 /* If cursor is out of bounds, don't draw garbage. This can happen
7991 in mini-buffer windows when switching between echo area glyphs
7992 and mini-buffer. */
7993 cursor_glyph = get_phys_cursor_glyph (w);
7994 if (cursor_glyph == NULL)
7995 return;
7997 /* If on an image, draw like a normal cursor. That's usually better
7998 visible than drawing a bar, esp. if the image is large so that
7999 the bar might not be in the window. */
8000 if (cursor_glyph->type == IMAGE_GLYPH)
8002 struct glyph_row *r;
8003 r = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
8004 draw_phys_cursor_glyph (w, r, DRAW_CURSOR);
8006 else
8008 Display *dpy = FRAME_X_DISPLAY (f);
8009 Window window = FRAME_X_WINDOW (f);
8010 GC gc = FRAME_DISPLAY_INFO (f)->scratch_cursor_gc;
8011 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
8012 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
8013 XGCValues xgcv;
8015 /* If the glyph's background equals the color we normally draw
8016 the bars cursor in, the bar cursor in its normal color is
8017 invisible. Use the glyph's foreground color instead in this
8018 case, on the assumption that the glyph's colors are chosen so
8019 that the glyph is legible. */
8020 if (face->background == f->output_data.x->cursor_pixel)
8021 xgcv.background = xgcv.foreground = face->foreground;
8022 else
8023 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
8024 xgcv.graphics_exposures = 0;
8026 if (gc)
8027 XChangeGC (dpy, gc, mask, &xgcv);
8028 else
8030 gc = XCreateGC (dpy, window, mask, &xgcv);
8031 FRAME_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
8034 x_clip_to_row (w, row, TEXT_AREA, gc);
8036 if (kind == BAR_CURSOR)
8038 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
8040 if (width < 0)
8041 width = FRAME_CURSOR_WIDTH (f);
8042 width = min (cursor_glyph->pixel_width, width);
8044 w->phys_cursor_width = width;
8046 /* If the character under cursor is R2L, draw the bar cursor
8047 on the right of its glyph, rather than on the left. */
8048 if ((cursor_glyph->resolved_level & 1) != 0)
8049 x += cursor_glyph->pixel_width - width;
8051 XFillRectangle (dpy, window, gc, x,
8052 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
8053 width, row->height);
8055 else
8057 int dummy_x, dummy_y, dummy_h;
8059 if (width < 0)
8060 width = row->height;
8062 width = min (row->height, width);
8064 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
8065 &dummy_y, &dummy_h);
8067 XFillRectangle (dpy, window, gc,
8068 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
8069 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
8070 row->height - width),
8071 w->phys_cursor_width, width);
8074 XSetClipMask (dpy, gc, None);
8079 /* RIF: Define cursor CURSOR on frame F. */
8081 static void
8082 x_define_frame_cursor (struct frame *f, Cursor cursor)
8084 if (!f->pointer_invisible
8085 && f->output_data.x->current_cursor != cursor)
8086 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
8087 f->output_data.x->current_cursor = cursor;
8091 /* RIF: Clear area on frame F. */
8093 static void
8094 x_clear_frame_area (struct frame *f, int x, int y, int width, int height)
8096 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), x, y, width, height);
8097 #ifdef USE_GTK
8098 /* Must queue a redraw, because scroll bars might have been cleared. */
8099 if (FRAME_GTK_WIDGET (f))
8100 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
8101 #endif
8105 /* RIF: Draw cursor on window W. */
8107 static void
8108 x_draw_window_cursor (struct window *w, struct glyph_row *glyph_row, int x,
8109 int y, enum text_cursor_kinds cursor_type,
8110 int cursor_width, bool on_p, bool active_p)
8112 struct frame *f = XFRAME (WINDOW_FRAME (w));
8114 if (on_p)
8116 w->phys_cursor_type = cursor_type;
8117 w->phys_cursor_on_p = 1;
8119 if (glyph_row->exact_window_width_line_p
8120 && (glyph_row->reversed_p
8121 ? (w->phys_cursor.hpos < 0)
8122 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
8124 glyph_row->cursor_in_fringe_p = 1;
8125 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
8127 else
8129 switch (cursor_type)
8131 case HOLLOW_BOX_CURSOR:
8132 x_draw_hollow_cursor (w, glyph_row);
8133 break;
8135 case FILLED_BOX_CURSOR:
8136 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
8137 break;
8139 case BAR_CURSOR:
8140 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
8141 break;
8143 case HBAR_CURSOR:
8144 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
8145 break;
8147 case NO_CURSOR:
8148 w->phys_cursor_width = 0;
8149 break;
8151 default:
8152 emacs_abort ();
8156 #ifdef HAVE_X_I18N
8157 if (w == XWINDOW (f->selected_window))
8158 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
8159 xic_set_preeditarea (w, x, y);
8160 #endif
8163 XFlush (FRAME_X_DISPLAY (f));
8167 /* Icons. */
8169 /* Make the x-window of frame F use the gnu icon bitmap. */
8172 x_bitmap_icon (struct frame *f, Lisp_Object file)
8174 ptrdiff_t bitmap_id;
8176 if (FRAME_X_WINDOW (f) == 0)
8177 return 1;
8179 /* Free up our existing icon bitmap and mask if any. */
8180 if (f->output_data.x->icon_bitmap > 0)
8181 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
8182 f->output_data.x->icon_bitmap = 0;
8184 if (STRINGP (file))
8186 #ifdef USE_GTK
8187 /* Use gtk_window_set_icon_from_file () if available,
8188 It's not restricted to bitmaps */
8189 if (xg_set_icon (f, file))
8190 return 0;
8191 #endif /* USE_GTK */
8192 bitmap_id = x_create_bitmap_from_file (f, file);
8193 x_create_bitmap_mask (f, bitmap_id);
8195 else
8197 /* Create the GNU bitmap and mask if necessary. */
8198 if (FRAME_DISPLAY_INFO (f)->icon_bitmap_id < 0)
8200 ptrdiff_t rc = -1;
8202 #ifdef USE_GTK
8204 if (FRAME_DISPLAY_INFO (f)->icon_bitmap_id == -2
8205 || xg_set_icon (f, xg_default_icon_file)
8206 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
8208 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = -2;
8209 return 0;
8212 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
8214 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
8215 if (rc != -1)
8216 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
8218 #endif
8220 /* If all else fails, use the (black and white) xbm image. */
8221 if (rc == -1)
8223 rc = x_create_bitmap_from_data (f, (char *) gnu_xbm_bits,
8224 gnu_xbm_width, gnu_xbm_height);
8225 if (rc == -1)
8226 return 1;
8228 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
8229 x_create_bitmap_mask (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
8233 /* The first time we create the GNU bitmap and mask,
8234 this increments the ref-count one extra time.
8235 As a result, the GNU bitmap and mask are never freed.
8236 That way, we don't have to worry about allocating it again. */
8237 x_reference_bitmap (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
8239 bitmap_id = FRAME_DISPLAY_INFO (f)->icon_bitmap_id;
8242 x_wm_set_icon_pixmap (f, bitmap_id);
8243 f->output_data.x->icon_bitmap = bitmap_id;
8245 return 0;
8249 /* Make the x-window of frame F use a rectangle with text.
8250 Use ICON_NAME as the text. */
8253 x_text_icon (struct frame *f, const char *icon_name)
8255 if (FRAME_X_WINDOW (f) == 0)
8256 return 1;
8259 XTextProperty text;
8260 text.value = (unsigned char *) icon_name;
8261 text.encoding = XA_STRING;
8262 text.format = 8;
8263 text.nitems = strlen (icon_name);
8264 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
8267 if (f->output_data.x->icon_bitmap > 0)
8268 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
8269 f->output_data.x->icon_bitmap = 0;
8270 x_wm_set_icon_pixmap (f, 0);
8272 return 0;
8275 #define X_ERROR_MESSAGE_SIZE 200
8277 /* If non-nil, this should be a string.
8278 It means catch X errors and store the error message in this string.
8280 The reason we use a stack is that x_catch_error/x_uncatch_error can
8281 be called from a signal handler.
8284 struct x_error_message_stack {
8285 char string[X_ERROR_MESSAGE_SIZE];
8286 Display *dpy;
8287 struct x_error_message_stack *prev;
8289 static struct x_error_message_stack *x_error_message;
8291 /* An X error handler which stores the error message in
8292 *x_error_message. This is called from x_error_handler if
8293 x_catch_errors is in effect. */
8295 static void
8296 x_error_catcher (Display *display, XErrorEvent *event)
8298 XGetErrorText (display, event->error_code,
8299 x_error_message->string,
8300 X_ERROR_MESSAGE_SIZE);
8303 /* Begin trapping X errors for display DPY. Actually we trap X errors
8304 for all displays, but DPY should be the display you are actually
8305 operating on.
8307 After calling this function, X protocol errors no longer cause
8308 Emacs to exit; instead, they are recorded in the string
8309 stored in *x_error_message.
8311 Calling x_check_errors signals an Emacs error if an X error has
8312 occurred since the last call to x_catch_errors or x_check_errors.
8314 Calling x_uncatch_errors resumes the normal error handling. */
8316 void
8317 x_catch_errors (Display *dpy)
8319 struct x_error_message_stack *data = xmalloc (sizeof *data);
8321 /* Make sure any errors from previous requests have been dealt with. */
8322 XSync (dpy, False);
8324 data->dpy = dpy;
8325 data->string[0] = 0;
8326 data->prev = x_error_message;
8327 x_error_message = data;
8330 /* Undo the last x_catch_errors call.
8331 DPY should be the display that was passed to x_catch_errors. */
8333 void
8334 x_uncatch_errors (void)
8336 struct x_error_message_stack *tmp;
8338 block_input ();
8340 /* The display may have been closed before this function is called.
8341 Check if it is still open before calling XSync. */
8342 if (x_display_info_for_display (x_error_message->dpy) != 0)
8343 XSync (x_error_message->dpy, False);
8345 tmp = x_error_message;
8346 x_error_message = x_error_message->prev;
8347 xfree (tmp);
8348 unblock_input ();
8351 /* If any X protocol errors have arrived since the last call to
8352 x_catch_errors or x_check_errors, signal an Emacs error using
8353 sprintf (a buffer, FORMAT, the x error message text) as the text. */
8355 void
8356 x_check_errors (Display *dpy, const char *format)
8358 /* Make sure to catch any errors incurred so far. */
8359 XSync (dpy, False);
8361 if (x_error_message->string[0])
8363 char string[X_ERROR_MESSAGE_SIZE];
8364 memcpy (string, x_error_message->string, X_ERROR_MESSAGE_SIZE);
8365 x_uncatch_errors ();
8366 error (format, string);
8370 /* Nonzero if we had any X protocol errors
8371 since we did x_catch_errors on DPY. */
8373 bool
8374 x_had_errors_p (Display *dpy)
8376 /* Make sure to catch any errors incurred so far. */
8377 XSync (dpy, False);
8379 return x_error_message->string[0] != 0;
8382 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
8384 void
8385 x_clear_errors (Display *dpy)
8387 x_error_message->string[0] = 0;
8390 #if 0 /* See comment in unwind_to_catch why calling this is a bad
8391 * idea. --lorentey */
8392 /* Close off all unclosed x_catch_errors calls. */
8394 void
8395 x_fully_uncatch_errors (void)
8397 while (x_error_message)
8398 x_uncatch_errors ();
8400 #endif
8402 #if 0
8403 static unsigned int x_wire_count;
8404 x_trace_wire (void)
8406 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
8408 #endif /* ! 0 */
8411 /************************************************************************
8412 Handling X errors
8413 ************************************************************************/
8415 /* Error message passed to x_connection_closed. */
8417 static char *error_msg;
8419 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
8420 the text of an error message that lead to the connection loss. */
8422 static void
8423 x_connection_closed (Display *dpy, const char *error_message)
8425 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
8426 Lisp_Object frame, tail;
8427 ptrdiff_t idx = SPECPDL_INDEX ();
8429 error_msg = alloca (strlen (error_message) + 1);
8430 strcpy (error_msg, error_message);
8432 /* Inhibit redisplay while frames are being deleted. */
8433 specbind (Qinhibit_redisplay, Qt);
8435 if (dpyinfo)
8437 /* Protect display from being closed when we delete the last
8438 frame on it. */
8439 dpyinfo->reference_count++;
8440 dpyinfo->terminal->reference_count++;
8443 /* First delete frames whose mini-buffers are on frames
8444 that are on the dead display. */
8445 FOR_EACH_FRAME (tail, frame)
8447 Lisp_Object minibuf_frame;
8448 minibuf_frame
8449 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
8450 if (FRAME_X_P (XFRAME (frame))
8451 && FRAME_X_P (XFRAME (minibuf_frame))
8452 && ! EQ (frame, minibuf_frame)
8453 && FRAME_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
8454 delete_frame (frame, Qnoelisp);
8457 /* Now delete all remaining frames on the dead display.
8458 We are now sure none of these is used as the mini-buffer
8459 for another frame that we need to delete. */
8460 FOR_EACH_FRAME (tail, frame)
8461 if (FRAME_X_P (XFRAME (frame))
8462 && FRAME_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
8464 /* Set this to t so that delete_frame won't get confused
8465 trying to find a replacement. */
8466 kset_default_minibuffer_frame (FRAME_KBOARD (XFRAME (frame)), Qt);
8467 delete_frame (frame, Qnoelisp);
8470 /* If DPYINFO is null, this means we didn't open the display in the
8471 first place, so don't try to close it. */
8472 if (dpyinfo)
8474 /* We can not call XtCloseDisplay here because it calls XSync.
8475 XSync inside the error handler apparently hangs Emacs. On
8476 current Xt versions, this isn't needed either. */
8477 #ifdef USE_GTK
8478 /* A long-standing GTK bug prevents proper disconnect handling
8479 (https://bugzilla.gnome.org/show_bug.cgi?id=85715). Once,
8480 the resulting Glib error message loop filled a user's disk.
8481 To avoid this, kill Emacs unconditionally on disconnect. */
8482 shut_down_emacs (0, Qnil);
8483 fprintf (stderr, "%s\n\
8484 When compiled with GTK, Emacs cannot recover from X disconnects.\n\
8485 This is a GTK bug: https://bugzilla.gnome.org/show_bug.cgi?id=85715\n\
8486 For details, see etc/PROBLEMS.\n",
8487 error_msg);
8488 emacs_abort ();
8489 #endif /* USE_GTK */
8491 /* Indicate that this display is dead. */
8492 dpyinfo->display = 0;
8494 dpyinfo->reference_count--;
8495 dpyinfo->terminal->reference_count--;
8496 if (dpyinfo->reference_count != 0)
8497 /* We have just closed all frames on this display. */
8498 emacs_abort ();
8501 Lisp_Object tmp;
8502 XSETTERMINAL (tmp, dpyinfo->terminal);
8503 Fdelete_terminal (tmp, Qnoelisp);
8507 if (terminal_list == 0)
8509 fprintf (stderr, "%s\n", error_msg);
8510 Fkill_emacs (make_number (70));
8511 /* NOTREACHED */
8514 totally_unblock_input ();
8516 unbind_to (idx, Qnil);
8517 clear_waiting_for_input ();
8519 /* Tell GCC not to suggest attribute 'noreturn' for this function. */
8520 IF_LINT (if (! terminal_list) return; )
8522 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
8523 longjmp), because returning from this function would get us back into
8524 Xlib's code which will directly call `exit'. */
8525 error ("%s", error_msg);
8528 /* We specifically use it before defining it, so that gcc doesn't inline it,
8529 otherwise gdb doesn't know how to properly put a breakpoint on it. */
8530 static void x_error_quitter (Display *, XErrorEvent *);
8532 /* This is the first-level handler for X protocol errors.
8533 It calls x_error_quitter or x_error_catcher. */
8535 static int
8536 x_error_handler (Display *display, XErrorEvent *event)
8538 #if defined USE_GTK && defined HAVE_GTK3
8539 if ((event->error_code == BadMatch || event->error_code == BadWindow)
8540 && event->request_code == X_SetInputFocus)
8542 return 0;
8544 #endif
8546 if (x_error_message)
8547 x_error_catcher (display, event);
8548 else
8549 x_error_quitter (display, event);
8550 return 0;
8553 /* This is the usual handler for X protocol errors.
8554 It kills all frames on the display that we got the error for.
8555 If that was the only one, it prints an error message and kills Emacs. */
8557 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
8559 /* On older GCC versions, just putting x_error_quitter
8560 after x_error_handler prevents inlining into the former. */
8562 static void NO_INLINE
8563 x_error_quitter (Display *display, XErrorEvent *event)
8565 char buf[256], buf1[356];
8567 /* Ignore BadName errors. They can happen because of fonts
8568 or colors that are not defined. */
8570 if (event->error_code == BadName)
8571 return;
8573 /* Note that there is no real way portable across R3/R4 to get the
8574 original error handler. */
8576 XGetErrorText (display, event->error_code, buf, sizeof (buf));
8577 sprintf (buf1, "X protocol error: %s on protocol request %d",
8578 buf, event->request_code);
8579 x_connection_closed (display, buf1);
8583 /* This is the handler for X IO errors, always.
8584 It kills all frames on the display that we lost touch with.
8585 If that was the only one, it prints an error message and kills Emacs. */
8587 static int
8588 x_io_error_quitter (Display *display)
8590 char buf[256];
8592 snprintf (buf, sizeof buf, "Connection lost to X server `%s'",
8593 DisplayString (display));
8594 x_connection_closed (display, buf);
8595 return 0;
8598 /* Changing the font of the frame. */
8600 /* Give frame F the font FONT-OBJECT as its default font. The return
8601 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
8602 frame. If it is negative, generate a new fontset from
8603 FONT-OBJECT. */
8605 Lisp_Object
8606 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
8608 struct font *font = XFONT_OBJECT (font_object);
8609 int unit;
8611 if (fontset < 0)
8612 fontset = fontset_from_font (font_object);
8613 FRAME_FONTSET (f) = fontset;
8614 if (FRAME_FONT (f) == font)
8615 /* This font is already set in frame F. There's nothing more to
8616 do. */
8617 return font_object;
8619 FRAME_FONT (f) = font;
8620 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
8621 FRAME_COLUMN_WIDTH (f) = font->average_width;
8622 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (font);
8624 #ifndef USE_X_TOOLKIT \
8625 /** FRAME_TOOL_BAR_HEIGHT (f) = FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f); **/
8626 FRAME_MENU_BAR_HEIGHT (f) = FRAME_MENU_BAR_LINES (f) * FRAME_LINE_HEIGHT (f);
8627 #endif
8629 /* Compute character columns occupied by scrollbar.
8631 Don't do things differently for non-toolkit scrollbars
8632 (Bug#17163). */
8633 unit = FRAME_COLUMN_WIDTH (f);
8634 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8635 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8636 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + unit - 1) / unit;
8637 else
8638 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
8640 if (FRAME_X_WINDOW (f) != 0)
8642 /* Don't change the size of a tip frame; there's no point in
8643 doing it because it's done in Fx_show_tip, and it leads to
8644 problems because the tip frame has no widget. */
8645 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8646 x_set_window_size (f, 0, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
8647 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 1);
8650 #ifdef HAVE_X_I18N
8651 if (FRAME_XIC (f)
8652 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8654 block_input ();
8655 xic_set_xfontset (f, SSDATA (fontset_ascii (fontset)));
8656 unblock_input ();
8658 #endif
8660 return font_object;
8664 /***********************************************************************
8665 X Input Methods
8666 ***********************************************************************/
8668 #ifdef HAVE_X_I18N
8670 #ifdef HAVE_X11R6
8672 /* XIM destroy callback function, which is called whenever the
8673 connection to input method XIM dies. CLIENT_DATA contains a
8674 pointer to the x_display_info structure corresponding to XIM. */
8676 static void
8677 xim_destroy_callback (XIM xim, XPointer client_data, XPointer call_data)
8679 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8680 Lisp_Object frame, tail;
8682 block_input ();
8684 /* No need to call XDestroyIC.. */
8685 FOR_EACH_FRAME (tail, frame)
8687 struct frame *f = XFRAME (frame);
8688 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
8690 FRAME_XIC (f) = NULL;
8691 xic_free_xfontset (f);
8695 /* No need to call XCloseIM. */
8696 dpyinfo->xim = NULL;
8697 XFree (dpyinfo->xim_styles);
8698 unblock_input ();
8701 #endif /* HAVE_X11R6 */
8703 /* Open the connection to the XIM server on display DPYINFO.
8704 RESOURCE_NAME is the resource name Emacs uses. */
8706 static void
8707 xim_open_dpy (struct x_display_info *dpyinfo, char *resource_name)
8709 XIM xim;
8711 #ifdef HAVE_XIM
8712 if (use_xim)
8714 if (dpyinfo->xim)
8715 XCloseIM (dpyinfo->xim);
8716 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8717 emacs_class);
8718 dpyinfo->xim = xim;
8720 if (xim)
8722 #ifdef HAVE_X11R6
8723 XIMCallback destroy;
8724 #endif
8726 /* Get supported styles and XIM values. */
8727 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8729 #ifdef HAVE_X11R6
8730 destroy.callback = xim_destroy_callback;
8731 destroy.client_data = (XPointer)dpyinfo;
8732 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8733 #endif
8737 else
8738 #endif /* HAVE_XIM */
8739 dpyinfo->xim = NULL;
8743 #ifdef HAVE_X11R6_XIM
8745 /* XIM instantiate callback function, which is called whenever an XIM
8746 server is available. DISPLAY is the display of the XIM.
8747 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8748 when the callback was registered. */
8750 static void
8751 xim_instantiate_callback (Display *display, XPointer client_data, XPointer call_data)
8753 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8754 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8756 /* We don't support multiple XIM connections. */
8757 if (dpyinfo->xim)
8758 return;
8760 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8762 /* Create XIC for the existing frames on the same display, as long
8763 as they have no XIC. */
8764 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8766 Lisp_Object tail, frame;
8768 block_input ();
8769 FOR_EACH_FRAME (tail, frame)
8771 struct frame *f = XFRAME (frame);
8773 if (FRAME_X_P (f)
8774 && FRAME_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8775 if (FRAME_XIC (f) == NULL)
8777 create_frame_xic (f);
8778 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8779 xic_set_statusarea (f);
8780 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8782 struct window *w = XWINDOW (f->selected_window);
8783 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8788 unblock_input ();
8792 #endif /* HAVE_X11R6_XIM */
8795 /* Open a connection to the XIM server on display DPYINFO.
8796 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8797 connection only at the first time. On X11R6, open the connection
8798 in the XIM instantiate callback function. */
8800 static void
8801 xim_initialize (struct x_display_info *dpyinfo, char *resource_name)
8803 dpyinfo->xim = NULL;
8804 #ifdef HAVE_XIM
8805 if (use_xim)
8807 #ifdef HAVE_X11R6_XIM
8808 struct xim_inst_t *xim_inst = xmalloc (sizeof *xim_inst);
8809 Bool ret;
8811 dpyinfo->xim_callback_data = xim_inst;
8812 xim_inst->dpyinfo = dpyinfo;
8813 xim_inst->resource_name = xstrdup (resource_name);
8814 ret = XRegisterIMInstantiateCallback
8815 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
8816 emacs_class, xim_instantiate_callback,
8817 /* This is XPointer in XFree86 but (XPointer *)
8818 on Tru64, at least, hence the configure test. */
8819 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8820 eassert (ret == True);
8821 #else /* not HAVE_X11R6_XIM */
8822 xim_open_dpy (dpyinfo, resource_name);
8823 #endif /* not HAVE_X11R6_XIM */
8825 #endif /* HAVE_XIM */
8829 /* Close the connection to the XIM server on display DPYINFO. */
8831 static void
8832 xim_close_dpy (struct x_display_info *dpyinfo)
8834 #ifdef HAVE_XIM
8835 if (use_xim)
8837 #ifdef HAVE_X11R6_XIM
8838 struct xim_inst_t *xim_inst = dpyinfo->xim_callback_data;
8840 if (dpyinfo->display)
8842 Bool ret = XUnregisterIMInstantiateCallback
8843 (dpyinfo->display, dpyinfo->xrdb, xim_inst->resource_name,
8844 emacs_class, xim_instantiate_callback,
8845 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8846 eassert (ret == True);
8848 xfree (xim_inst->resource_name);
8849 xfree (xim_inst);
8850 #endif /* HAVE_X11R6_XIM */
8851 if (dpyinfo->display)
8852 XCloseIM (dpyinfo->xim);
8853 dpyinfo->xim = NULL;
8854 XFree (dpyinfo->xim_styles);
8856 #endif /* HAVE_XIM */
8859 #endif /* not HAVE_X11R6_XIM */
8863 /* Calculate the absolute position in frame F
8864 from its current recorded position values and gravity. */
8866 static void
8867 x_calc_absolute_position (struct frame *f)
8869 int flags = f->size_hint_flags;
8871 /* We have nothing to do if the current position
8872 is already for the top-left corner. */
8873 if (! ((flags & XNegative) || (flags & YNegative)))
8874 return;
8876 /* Treat negative positions as relative to the leftmost bottommost
8877 position that fits on the screen. */
8878 if (flags & XNegative)
8879 f->left_pos = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
8880 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
8883 int height = FRAME_PIXEL_HEIGHT (f);
8885 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8886 /* Something is fishy here. When using Motif, starting Emacs with
8887 `-g -0-0', the frame appears too low by a few pixels.
8889 This seems to be so because initially, while Emacs is starting,
8890 the column widget's height and the frame's pixel height are
8891 different. The column widget's height is the right one. In
8892 later invocations, when Emacs is up, the frame's pixel height
8893 is right, though.
8895 It's not obvious where the initial small difference comes from.
8896 2000-12-01, gerd. */
8898 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8899 #endif
8901 if (flags & YNegative)
8902 f->top_pos = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
8903 - height + f->top_pos;
8906 /* The left_pos and top_pos
8907 are now relative to the top and left screen edges,
8908 so the flags should correspond. */
8909 f->size_hint_flags &= ~ (XNegative | YNegative);
8912 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8913 to really change the position, and 0 when calling from
8914 x_make_frame_visible (in that case, XOFF and YOFF are the current
8915 position values). It is -1 when calling from x_set_frame_parameters,
8916 which means, do adjust for borders but don't change the gravity. */
8918 void
8919 x_set_offset (struct frame *f, register int xoff, register int yoff, int change_gravity)
8921 int modified_top, modified_left;
8923 if (change_gravity > 0)
8925 f->top_pos = yoff;
8926 f->left_pos = xoff;
8927 f->size_hint_flags &= ~ (XNegative | YNegative);
8928 if (xoff < 0)
8929 f->size_hint_flags |= XNegative;
8930 if (yoff < 0)
8931 f->size_hint_flags |= YNegative;
8932 f->win_gravity = NorthWestGravity;
8934 x_calc_absolute_position (f);
8936 block_input ();
8937 x_wm_set_size_hint (f, 0, 0);
8939 modified_left = f->left_pos;
8940 modified_top = f->top_pos;
8942 if (change_gravity != 0 && FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8944 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8945 than the WM decorations. So we use the calculated offset instead
8946 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8947 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8948 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8951 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8952 modified_left, modified_top);
8954 x_sync_with_move (f, f->left_pos, f->top_pos,
8955 FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8956 ? 1 : 0);
8958 /* change_gravity is non-zero when this function is called from Lisp to
8959 programmatically move a frame. In that case, we call
8960 x_check_expected_move to discover if we have a "Type A" or "Type B"
8961 window manager, and, for a "Type A" window manager, adjust the position
8962 of the frame.
8964 We call x_check_expected_move if a programmatic move occurred, and
8965 either the window manager type (A/B) is unknown or it is Type A but we
8966 need to compute the top/left offset adjustment for this frame. */
8968 if (change_gravity != 0 &&
8969 (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8970 || (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8971 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8972 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8973 x_check_expected_move (f, modified_left, modified_top);
8975 unblock_input ();
8978 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8979 on the root window for frame F contains ATOMNAME.
8980 This is how a WM check shall be done according to the Window Manager
8981 Specification/Extended Window Manager Hints at
8982 http://freedesktop.org/wiki/Specifications/wm-spec. */
8984 static int
8985 wm_supports (struct frame *f, Atom want_atom)
8987 Atom actual_type;
8988 unsigned long actual_size, bytes_remaining;
8989 int i, rc, actual_format;
8990 Window wmcheck_window;
8991 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8992 Window target_window = dpyinfo->root_window;
8993 long max_len = 65536;
8994 Display *dpy = FRAME_X_DISPLAY (f);
8995 unsigned char *tmp_data = NULL;
8996 Atom target_type = XA_WINDOW;
8998 block_input ();
9000 x_catch_errors (dpy);
9001 rc = XGetWindowProperty (dpy, target_window,
9002 dpyinfo->Xatom_net_supporting_wm_check,
9003 0, max_len, False, target_type,
9004 &actual_type, &actual_format, &actual_size,
9005 &bytes_remaining, &tmp_data);
9007 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
9009 if (tmp_data) XFree (tmp_data);
9010 x_uncatch_errors ();
9011 unblock_input ();
9012 return 0;
9015 wmcheck_window = *(Window *) tmp_data;
9016 XFree (tmp_data);
9018 /* Check if window exists. */
9019 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
9020 x_sync (f);
9021 if (x_had_errors_p (dpy))
9023 x_uncatch_errors ();
9024 unblock_input ();
9025 return 0;
9028 if (dpyinfo->net_supported_window != wmcheck_window)
9030 /* Window changed, reload atoms */
9031 if (dpyinfo->net_supported_atoms != NULL)
9032 XFree (dpyinfo->net_supported_atoms);
9033 dpyinfo->net_supported_atoms = NULL;
9034 dpyinfo->nr_net_supported_atoms = 0;
9035 dpyinfo->net_supported_window = 0;
9037 target_type = XA_ATOM;
9038 tmp_data = NULL;
9039 rc = XGetWindowProperty (dpy, target_window,
9040 dpyinfo->Xatom_net_supported,
9041 0, max_len, False, target_type,
9042 &actual_type, &actual_format, &actual_size,
9043 &bytes_remaining, &tmp_data);
9045 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
9047 if (tmp_data) XFree (tmp_data);
9048 x_uncatch_errors ();
9049 unblock_input ();
9050 return 0;
9053 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
9054 dpyinfo->nr_net_supported_atoms = actual_size;
9055 dpyinfo->net_supported_window = wmcheck_window;
9058 rc = 0;
9060 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
9061 rc = dpyinfo->net_supported_atoms[i] == want_atom;
9063 x_uncatch_errors ();
9064 unblock_input ();
9066 return rc;
9069 static void
9070 set_wm_state (Lisp_Object frame, int add, Atom atom, Atom value)
9072 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (XFRAME (frame));
9074 x_send_client_event (frame, make_number (0), frame,
9075 dpyinfo->Xatom_net_wm_state,
9076 make_number (32),
9077 /* 1 = add, 0 = remove */
9078 Fcons
9079 (make_number (add ? 1 : 0),
9080 Fcons
9081 (make_fixnum_or_float (atom),
9082 (value != 0
9083 ? list1 (make_fixnum_or_float (value))
9084 : Qnil))));
9087 void
9088 x_set_sticky (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
9090 Lisp_Object frame;
9091 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9093 XSETFRAME (frame, f);
9095 set_wm_state (frame, NILP (new_value) ? 0 : 1,
9096 dpyinfo->Xatom_net_wm_state_sticky, None);
9099 /* Return the current _NET_WM_STATE.
9100 SIZE_STATE is set to one of the FULLSCREEN_* values.
9101 STICKY is set to 1 if the sticky state is set, 0 if not.
9103 Return non-zero if we are not hidden, zero if we are. */
9105 static int
9106 get_current_wm_state (struct frame *f,
9107 Window window,
9108 int *size_state,
9109 int *sticky)
9111 Atom actual_type;
9112 unsigned long actual_size, bytes_remaining;
9113 int i, rc, actual_format, is_hidden = 0;
9114 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9115 long max_len = 65536;
9116 Display *dpy = FRAME_X_DISPLAY (f);
9117 unsigned char *tmp_data = NULL;
9118 Atom target_type = XA_ATOM;
9120 *sticky = 0;
9121 *size_state = FULLSCREEN_NONE;
9123 block_input ();
9124 x_catch_errors (dpy);
9125 rc = XGetWindowProperty (dpy, window, dpyinfo->Xatom_net_wm_state,
9126 0, max_len, False, target_type,
9127 &actual_type, &actual_format, &actual_size,
9128 &bytes_remaining, &tmp_data);
9130 if (rc != Success || actual_type != target_type || x_had_errors_p (dpy))
9132 if (tmp_data) XFree (tmp_data);
9133 x_uncatch_errors ();
9134 unblock_input ();
9135 return !FRAME_ICONIFIED_P (f);
9138 x_uncatch_errors ();
9140 for (i = 0; i < actual_size; ++i)
9142 Atom a = ((Atom*)tmp_data)[i];
9143 if (a == dpyinfo->Xatom_net_wm_state_hidden)
9145 is_hidden = 1;
9146 f->output_data.x->net_wm_state_hidden_seen = 1;
9148 else if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
9150 if (*size_state == FULLSCREEN_HEIGHT)
9151 *size_state = FULLSCREEN_MAXIMIZED;
9152 else
9153 *size_state = FULLSCREEN_WIDTH;
9155 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
9157 if (*size_state == FULLSCREEN_WIDTH)
9158 *size_state = FULLSCREEN_MAXIMIZED;
9159 else
9160 *size_state = FULLSCREEN_HEIGHT;
9162 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen)
9163 *size_state = FULLSCREEN_BOTH;
9164 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
9165 *sticky = 1;
9168 if (tmp_data) XFree (tmp_data);
9169 unblock_input ();
9170 return ! is_hidden;
9173 /* Do fullscreen as specified in extended window manager hints */
9175 static int
9176 do_ewmh_fullscreen (struct frame *f)
9178 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9179 int have_net_atom = wm_supports (f, dpyinfo->Xatom_net_wm_state);
9180 int cur, dummy;
9182 (void)get_current_wm_state (f, FRAME_OUTER_WINDOW (f), &cur, &dummy);
9184 /* Some window managers don't say they support _NET_WM_STATE, but they do say
9185 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
9186 if (!have_net_atom)
9187 have_net_atom = wm_supports (f, dpyinfo->Xatom_net_wm_state_fullscreen);
9189 if (have_net_atom && cur != f->want_fullscreen)
9191 Lisp_Object frame;
9193 XSETFRAME (frame, f);
9195 /* Keep number of calls to set_wm_state as low as possible.
9196 Some window managers, or possible Gtk+, hangs when too many
9197 are sent at once. */
9198 switch (f->want_fullscreen)
9200 case FULLSCREEN_BOTH:
9201 if (cur == FULLSCREEN_WIDTH || cur == FULLSCREEN_MAXIMIZED
9202 || cur == FULLSCREEN_HEIGHT)
9203 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_maximized_horz,
9204 dpyinfo->Xatom_net_wm_state_maximized_vert);
9205 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_fullscreen, None);
9206 break;
9207 case FULLSCREEN_WIDTH:
9208 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_HEIGHT
9209 || cur == FULLSCREEN_MAXIMIZED)
9210 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen,
9211 dpyinfo->Xatom_net_wm_state_maximized_vert);
9212 if (cur != FULLSCREEN_MAXIMIZED)
9213 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_maximized_horz, None);
9214 break;
9215 case FULLSCREEN_HEIGHT:
9216 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_WIDTH
9217 || cur == FULLSCREEN_MAXIMIZED)
9218 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen,
9219 dpyinfo->Xatom_net_wm_state_maximized_horz);
9220 if (cur != FULLSCREEN_MAXIMIZED)
9221 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_maximized_vert, None);
9222 break;
9223 case FULLSCREEN_MAXIMIZED:
9224 if (cur == FULLSCREEN_BOTH)
9225 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen, None);
9226 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_maximized_horz,
9227 dpyinfo->Xatom_net_wm_state_maximized_vert);
9228 break;
9229 case FULLSCREEN_NONE:
9230 if (cur == FULLSCREEN_BOTH)
9231 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen, None);
9232 else
9233 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_maximized_horz,
9234 dpyinfo->Xatom_net_wm_state_maximized_vert);
9237 f->want_fullscreen = FULLSCREEN_NONE;
9241 return have_net_atom;
9244 static void
9245 XTfullscreen_hook (struct frame *f)
9247 if (FRAME_VISIBLE_P (f))
9249 block_input ();
9250 x_check_fullscreen (f);
9251 x_sync (f);
9252 unblock_input ();
9257 static int
9258 x_handle_net_wm_state (struct frame *f, const XPropertyEvent *event)
9260 int value = FULLSCREEN_NONE;
9261 Lisp_Object lval;
9262 int sticky = 0;
9263 int not_hidden = get_current_wm_state (f, event->window, &value, &sticky);
9265 lval = Qnil;
9266 switch (value)
9268 case FULLSCREEN_WIDTH:
9269 lval = Qfullwidth;
9270 break;
9271 case FULLSCREEN_HEIGHT:
9272 lval = Qfullheight;
9273 break;
9274 case FULLSCREEN_BOTH:
9275 lval = Qfullboth;
9276 break;
9277 case FULLSCREEN_MAXIMIZED:
9278 lval = Qmaximized;
9279 break;
9282 store_frame_param (f, Qfullscreen, lval);
9283 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
9285 return not_hidden;
9288 /* Check if we need to resize the frame due to a fullscreen request.
9289 If so needed, resize the frame. */
9290 static void
9291 x_check_fullscreen (struct frame *f)
9293 if (do_ewmh_fullscreen (f))
9294 return;
9296 if (f->output_data.x->parent_desc != FRAME_DISPLAY_INFO (f)->root_window)
9297 return; /* Only fullscreen without WM or with EWM hints (above). */
9299 /* Setting fullscreen to nil doesn't do anything. We could save the
9300 last non-fullscreen size and restore it, but it seems like a
9301 lot of work for this unusual case (no window manager running). */
9303 if (f->want_fullscreen != FULLSCREEN_NONE)
9305 int width = FRAME_PIXEL_WIDTH (f), height = FRAME_PIXEL_HEIGHT (f);
9306 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9308 switch (f->want_fullscreen)
9310 /* No difference between these two when there is no WM */
9311 case FULLSCREEN_BOTH:
9312 case FULLSCREEN_MAXIMIZED:
9313 width = x_display_pixel_width (dpyinfo);
9314 height = x_display_pixel_height (dpyinfo);
9315 break;
9316 case FULLSCREEN_WIDTH:
9317 width = x_display_pixel_width (dpyinfo);
9318 break;
9319 case FULLSCREEN_HEIGHT:
9320 height = x_display_pixel_height (dpyinfo);
9323 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9324 width, height);
9328 /* This function is called by x_set_offset to determine whether the window
9329 manager interfered with the positioning of the frame. Type A window
9330 managers position the surrounding window manager decorations a small
9331 amount above and left of the user-supplied position. Type B window
9332 managers position the surrounding window manager decorations at the
9333 user-specified position. If we detect a Type A window manager, we
9334 compensate by moving the window right and down by the proper amount. */
9336 static void
9337 x_check_expected_move (struct frame *f, int expected_left, int expected_top)
9339 int current_left = 0, current_top = 0;
9341 /* x_real_positions returns the left and top offsets of the outermost
9342 window manager window around the frame. */
9344 x_real_positions (f, &current_left, &current_top);
9346 if (current_left != expected_left || current_top != expected_top)
9348 /* It's a "Type A" window manager. */
9350 int adjusted_left;
9351 int adjusted_top;
9353 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
9354 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
9355 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
9357 /* Now fix the mispositioned frame's location. */
9359 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
9360 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
9362 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9363 adjusted_left, adjusted_top);
9365 x_sync_with_move (f, expected_left, expected_top, 0);
9367 else
9368 /* It's a "Type B" window manager. We don't have to adjust the
9369 frame's position. */
9371 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
9375 /* Wait for XGetGeometry to return up-to-date position information for a
9376 recently-moved frame. Call this immediately after calling XMoveWindow.
9377 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
9378 frame has been moved to, so we use a fuzzy position comparison instead
9379 of an exact comparison. */
9381 static void
9382 x_sync_with_move (struct frame *f, int left, int top, int fuzzy)
9384 int count = 0;
9386 while (count++ < 50)
9388 int current_left = 0, current_top = 0;
9390 /* In theory, this call to XSync only needs to happen once, but in
9391 practice, it doesn't seem to work, hence the need for the surrounding
9392 loop. */
9394 XSync (FRAME_X_DISPLAY (f), False);
9395 x_real_positions (f, &current_left, &current_top);
9397 if (fuzzy)
9399 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
9400 pixels. */
9402 if (eabs (current_left - left) <= 10
9403 && eabs (current_top - top) <= 40)
9404 return;
9406 else if (current_left == left && current_top == top)
9407 return;
9410 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
9411 will then return up-to-date position info. */
9413 wait_reading_process_output (0, 500000000, 0, 0, Qnil, NULL, 0);
9417 /* Wait for an event on frame F matching EVENTTYPE. */
9418 void
9419 x_wait_for_event (struct frame *f, int eventtype)
9421 int level = interrupt_input_blocked;
9423 fd_set fds;
9424 struct timespec tmo, tmo_at, time_now;
9425 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
9427 f->wait_event_type = eventtype;
9429 /* Set timeout to 0.1 second. Hopefully not noticeable.
9430 Maybe it should be configurable. */
9431 tmo = make_timespec (0, 100 * 1000 * 1000);
9432 tmo_at = timespec_add (current_timespec (), tmo);
9434 while (f->wait_event_type)
9436 pending_signals = 1;
9437 totally_unblock_input ();
9438 /* XTread_socket is called after unblock. */
9439 block_input ();
9440 interrupt_input_blocked = level;
9442 FD_ZERO (&fds);
9443 FD_SET (fd, &fds);
9445 time_now = current_timespec ();
9446 if (timespec_cmp (tmo_at, time_now) < 0)
9447 break;
9449 tmo = timespec_sub (tmo_at, time_now);
9450 if (pselect (fd + 1, &fds, NULL, NULL, &tmo, NULL) == 0)
9451 break; /* Timeout */
9454 f->wait_event_type = 0;
9458 /* Change the size of frame F's X window to WIDTH/HEIGHT in the case F
9459 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
9460 top-left-corner window gravity for this size change and subsequent
9461 size changes. Otherwise we leave the window gravity unchanged. */
9463 static void
9464 x_set_window_size_1 (struct frame *f, int change_gravity, int width, int height, bool pixelwise)
9466 int pixelwidth, pixelheight;
9468 /** if (pixelwise) **/
9469 /** { **/
9470 /** pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width); **/
9471 /** pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height); **/
9472 /** } **/
9473 /** else **/
9474 /** { **/
9475 /** pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, width); **/
9476 /** pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, height); **/
9477 /** } **/
9479 /** FRAME_TOOL_BAR_HEIGHT (f) = FRAME_TOOLBAR_HEIGHT (f); **/
9480 pixelwidth = (pixelwise
9481 ? FRAME_TEXT_TO_PIXEL_WIDTH (f, width)
9482 : FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, width));
9483 pixelheight = ((pixelwise
9484 ? FRAME_TEXT_TO_PIXEL_HEIGHT (f, height)
9485 : FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, height)));
9487 /** pixelwidth = ((pixelwise ? width : (width * FRAME_COLUMN_WIDTH (f))) **/
9488 /** + FRAME_SCROLL_BAR_AREA_WIDTH (f) **/
9489 /** + FRAME_TOTAL_FRINGE_WIDTH (f) **/
9490 /** + 2 * FRAME_INTERNAL_BORDER_WIDTH (f)); **/
9492 /** pixelheight = ((pixelwise ? height : (height * FRAME_LINE_HEIGHT (f))) **/
9493 /** + FRAME_TOOLBAR_HEIGHT (f) **/
9494 /** + FRAME_SCROLL_BAR_AREA_HEIGHT (f) **/
9495 /** + 2 * FRAME_INTERNAL_BORDER_WIDTH (f)); **/
9497 if (change_gravity) f->win_gravity = NorthWestGravity;
9498 x_wm_set_size_hint (f, 0, 0);
9499 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9500 pixelwidth, pixelheight + FRAME_MENUBAR_HEIGHT (f));
9503 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
9504 receive in the ConfigureNotify event; if we get what we asked
9505 for, then the event won't cause the screen to become garbaged, so
9506 we have to make sure to do it here. */
9507 SET_FRAME_GARBAGED (f);
9509 /* Now, strictly speaking, we can't be sure that this is accurate,
9510 but the window manager will get around to dealing with the size
9511 change request eventually, and we'll hear how it went when the
9512 ConfigureNotify event gets here.
9514 We could just not bother storing any of this information here,
9515 and let the ConfigureNotify event set everything up, but that
9516 might be kind of confusing to the Lisp code, since size changes
9517 wouldn't be reported in the frame parameters until some random
9518 point in the future when the ConfigureNotify event arrives.
9520 We pass 1 for DELAY since we can't run Lisp code inside of
9521 a BLOCK_INPUT. */
9523 /* But the ConfigureNotify may in fact never arrive, and then this is
9524 not right if the frame is visible. Instead wait (with timeout)
9525 for the ConfigureNotify. */
9526 if (FRAME_VISIBLE_P (f))
9527 x_wait_for_event (f, ConfigureNotify);
9528 else
9530 change_frame_size (f, pixelwidth, pixelheight, 0, 1, 0, 1);
9531 x_sync (f);
9536 /* Call this to change the size of frame F's x-window.
9537 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
9538 for this size change and subsequent size changes.
9539 Otherwise we leave the window gravity unchanged. */
9541 void
9542 x_set_window_size (struct frame *f, int change_gravity, int width, int height, bool pixelwise)
9544 block_input ();
9546 /* The following breaks our calculations. If it's really needed,
9547 think of something else. */
9548 #if 0
9549 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
9551 int text_width, text_height;
9553 /* When the frame is maximized/fullscreen or running under for
9554 example Xmonad, x_set_window_size_1 will be a no-op.
9555 In that case, the right thing to do is extend rows/width to
9556 the current frame size. We do that first if x_set_window_size_1
9557 turns out to not be a no-op (there is no way to know).
9558 The size will be adjusted again if the frame gets a
9559 ConfigureNotify event as a result of x_set_window_size. */
9560 int pixelh = FRAME_PIXEL_HEIGHT (f);
9561 #ifdef USE_X_TOOLKIT
9562 /* The menu bar is not part of text lines. The tool bar
9563 is however. */
9564 pixelh -= FRAME_MENUBAR_HEIGHT (f);
9565 #endif
9566 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, FRAME_PIXEL_WIDTH (f));
9567 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelh);
9569 change_frame_size (f, text_width, text_height, 0, 1, 0, 1);
9571 #endif
9573 #ifdef USE_GTK
9574 if (FRAME_GTK_WIDGET (f))
9575 if (! pixelwise)
9576 xg_frame_set_char_size (f, width * FRAME_COLUMN_WIDTH (f),
9577 height * FRAME_LINE_HEIGHT (f));
9578 else
9579 xg_frame_set_char_size (f, width, height);
9580 else
9581 x_set_window_size_1 (f, change_gravity, width, height, pixelwise);
9582 #else /* not USE_GTK */
9584 x_set_window_size_1 (f, change_gravity, width, height, pixelwise);
9585 x_clear_under_internal_border (f);
9587 #endif /* not USE_GTK */
9589 /* If cursor was outside the new size, mark it as off. */
9590 mark_window_cursors_off (XWINDOW (f->root_window));
9592 /* Clear out any recollection of where the mouse highlighting was,
9593 since it might be in a place that's outside the new frame size.
9594 Actually checking whether it is outside is a pain in the neck,
9595 so don't try--just let the highlighting be done afresh with new size. */
9596 cancel_mouse_face (f);
9598 unblock_input ();
9600 do_pending_window_change (0);
9603 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
9605 void
9606 frame_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
9608 block_input ();
9610 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9611 0, 0, 0, 0, pix_x, pix_y);
9612 unblock_input ();
9615 /* Raise frame F. */
9617 void
9618 x_raise_frame (struct frame *f)
9620 block_input ();
9621 if (FRAME_VISIBLE_P (f))
9622 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9623 XFlush (FRAME_X_DISPLAY (f));
9624 unblock_input ();
9627 /* Lower frame F. */
9629 static void
9630 x_lower_frame (struct frame *f)
9632 if (FRAME_VISIBLE_P (f))
9634 block_input ();
9635 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9636 XFlush (FRAME_X_DISPLAY (f));
9637 unblock_input ();
9641 /* Request focus with XEmbed */
9643 void
9644 xembed_request_focus (struct frame *f)
9646 /* See XEmbed Protocol Specification at
9647 http://freedesktop.org/wiki/Specifications/xembed-spec */
9648 if (FRAME_VISIBLE_P (f))
9649 xembed_send_message (f, CurrentTime,
9650 XEMBED_REQUEST_FOCUS, 0, 0, 0);
9653 /* Activate frame with Extended Window Manager Hints */
9655 void
9656 x_ewmh_activate_frame (struct frame *f)
9658 /* See Window Manager Specification/Extended Window Manager Hints at
9659 http://freedesktop.org/wiki/Specifications/wm-spec */
9661 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9663 if (FRAME_VISIBLE_P (f) && wm_supports (f, dpyinfo->Xatom_net_active_window))
9665 Lisp_Object frame;
9666 XSETFRAME (frame, f);
9667 x_send_client_event (frame, make_number (0), frame,
9668 dpyinfo->Xatom_net_active_window,
9669 make_number (32),
9670 list2i (1, dpyinfo->last_user_time));
9674 static void
9675 XTframe_raise_lower (struct frame *f, int raise_flag)
9677 if (raise_flag)
9678 x_raise_frame (f);
9679 else
9680 x_lower_frame (f);
9683 /* XEmbed implementation. */
9685 #if defined USE_X_TOOLKIT || ! defined USE_GTK
9687 /* XEmbed implementation. */
9689 #define XEMBED_VERSION 0
9691 static void
9692 xembed_set_info (struct frame *f, enum xembed_info flags)
9694 unsigned long data[2];
9695 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9697 data[0] = XEMBED_VERSION;
9698 data[1] = flags;
9700 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9701 dpyinfo->Xatom_XEMBED_INFO, dpyinfo->Xatom_XEMBED_INFO,
9702 32, PropModeReplace, (unsigned char *) data, 2);
9704 #endif /* defined USE_X_TOOLKIT || ! defined USE_GTK */
9706 static void
9707 xembed_send_message (struct frame *f, Time t, enum xembed_message msg,
9708 long int detail, long int data1, long int data2)
9710 XEvent event;
9712 event.xclient.type = ClientMessage;
9713 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
9714 event.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_XEMBED;
9715 event.xclient.format = 32;
9716 event.xclient.data.l[0] = t;
9717 event.xclient.data.l[1] = msg;
9718 event.xclient.data.l[2] = detail;
9719 event.xclient.data.l[3] = data1;
9720 event.xclient.data.l[4] = data2;
9722 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
9723 False, NoEventMask, &event);
9724 XSync (FRAME_X_DISPLAY (f), False);
9727 /* Change of visibility. */
9729 /* This tries to wait until the frame is really visible.
9730 However, if the window manager asks the user where to position
9731 the frame, this will return before the user finishes doing that.
9732 The frame will not actually be visible at that time,
9733 but it will become visible later when the window manager
9734 finishes with it. */
9736 void
9737 x_make_frame_visible (struct frame *f)
9739 int original_top, original_left;
9740 int tries = 0;
9742 block_input ();
9744 x_set_bitmap_icon (f);
9746 if (! FRAME_VISIBLE_P (f))
9748 /* We test FRAME_GARBAGED_P here to make sure we don't
9749 call x_set_offset a second time
9750 if we get to x_make_frame_visible a second time
9751 before the window gets really visible. */
9752 if (! FRAME_ICONIFIED_P (f)
9753 && ! FRAME_X_EMBEDDED_P (f)
9754 && ! f->output_data.x->asked_for_visible)
9755 x_set_offset (f, f->left_pos, f->top_pos, 0);
9757 f->output_data.x->asked_for_visible = 1;
9759 if (! EQ (Vx_no_window_manager, Qt))
9760 x_wm_set_window_state (f, NormalState);
9761 #ifdef USE_X_TOOLKIT
9762 if (FRAME_X_EMBEDDED_P (f))
9763 xembed_set_info (f, XEMBED_MAPPED);
9764 else
9766 /* This was XtPopup, but that did nothing for an iconified frame. */
9767 XtMapWidget (f->output_data.x->widget);
9769 #else /* not USE_X_TOOLKIT */
9770 #ifdef USE_GTK
9771 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9772 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9773 #else
9774 if (FRAME_X_EMBEDDED_P (f))
9775 xembed_set_info (f, XEMBED_MAPPED);
9776 else
9777 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9778 #endif /* not USE_GTK */
9779 #endif /* not USE_X_TOOLKIT */
9782 XFlush (FRAME_X_DISPLAY (f));
9784 /* Synchronize to ensure Emacs knows the frame is visible
9785 before we do anything else. We do this loop with input not blocked
9786 so that incoming events are handled. */
9788 Lisp_Object frame;
9789 /* This must be before UNBLOCK_INPUT
9790 since events that arrive in response to the actions above
9791 will set it when they are handled. */
9792 int previously_visible = f->output_data.x->has_been_visible;
9794 original_left = f->left_pos;
9795 original_top = f->top_pos;
9797 /* This must come after we set COUNT. */
9798 unblock_input ();
9800 /* We unblock here so that arriving X events are processed. */
9802 /* Now move the window back to where it was "supposed to be".
9803 But don't do it if the gravity is negative.
9804 When the gravity is negative, this uses a position
9805 that is 3 pixels too low. Perhaps that's really the border width.
9807 Don't do this if the window has never been visible before,
9808 because the window manager may choose the position
9809 and we don't want to override it. */
9811 if (! FRAME_VISIBLE_P (f)
9812 && ! FRAME_ICONIFIED_P (f)
9813 && ! FRAME_X_EMBEDDED_P (f)
9814 && f->win_gravity == NorthWestGravity
9815 && previously_visible)
9817 Drawable rootw;
9818 int x, y;
9819 unsigned int width, height, border, depth;
9821 block_input ();
9823 /* On some window managers (such as FVWM) moving an existing
9824 window, even to the same place, causes the window manager
9825 to introduce an offset. This can cause the window to move
9826 to an unexpected location. Check the geometry (a little
9827 slow here) and then verify that the window is in the right
9828 place. If the window is not in the right place, move it
9829 there, and take the potential window manager hit. */
9830 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9831 &rootw, &x, &y, &width, &height, &border, &depth);
9833 if (original_left != x || original_top != y)
9834 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9835 original_left, original_top);
9837 unblock_input ();
9840 XSETFRAME (frame, f);
9842 /* Process X events until a MapNotify event has been seen. */
9843 while (!FRAME_VISIBLE_P (f))
9845 /* Force processing of queued events. */
9846 x_sync (f);
9848 /* If on another desktop, the deiconify/map may be ignored and the
9849 frame never becomes visible. XMonad does this.
9850 Prevent an endless loop. */
9851 if (FRAME_ICONIFIED_P (f) && ++tries > 100)
9852 break;
9854 /* This hack is still in use at least for Cygwin. See
9855 http://lists.gnu.org/archive/html/emacs-devel/2013-12/msg00351.html.
9857 Machines that do polling rather than SIGIO have been
9858 observed to go into a busy-wait here. So we'll fake an
9859 alarm signal to let the handler know that there's something
9860 to be read. We used to raise a real alarm, but it seems
9861 that the handler isn't always enabled here. This is
9862 probably a bug. */
9863 if (input_polling_used ())
9865 /* It could be confusing if a real alarm arrives while
9866 processing the fake one. Turn it off and let the
9867 handler reset it. */
9868 int old_poll_suppress_count = poll_suppress_count;
9869 poll_suppress_count = 1;
9870 poll_for_input_1 ();
9871 poll_suppress_count = old_poll_suppress_count;
9874 if (XPending (FRAME_X_DISPLAY (f)))
9876 XEvent xev;
9877 XNextEvent (FRAME_X_DISPLAY (f), &xev);
9878 x_dispatch_event (&xev, FRAME_X_DISPLAY (f));
9884 /* Change from mapped state to withdrawn state. */
9886 /* Make the frame visible (mapped and not iconified). */
9888 void
9889 x_make_frame_invisible (struct frame *f)
9891 Window window;
9893 /* Use the frame's outermost window, not the one we normally draw on. */
9894 window = FRAME_OUTER_WINDOW (f);
9896 /* Don't keep the highlight on an invisible frame. */
9897 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
9898 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
9900 block_input ();
9902 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9903 that the current position of the window is user-specified, rather than
9904 program-specified, so that when the window is mapped again, it will be
9905 placed at the same location, without forcing the user to position it
9906 by hand again (they have already done that once for this window.) */
9907 x_wm_set_size_hint (f, 0, 1);
9909 #ifdef USE_GTK
9910 if (FRAME_GTK_OUTER_WIDGET (f))
9911 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9912 else
9913 #else
9914 if (FRAME_X_EMBEDDED_P (f))
9915 xembed_set_info (f, 0);
9916 else
9917 #endif
9920 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9921 DefaultScreen (FRAME_X_DISPLAY (f))))
9923 unblock_input ();
9924 error ("Can't notify window manager of window withdrawal");
9928 /* We can't distinguish this from iconification
9929 just by the event that we get from the server.
9930 So we can't win using the usual strategy of letting
9931 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9932 and synchronize with the server to make sure we agree. */
9933 SET_FRAME_VISIBLE (f, 0);
9934 SET_FRAME_ICONIFIED (f, 0);
9936 x_sync (f);
9938 unblock_input ();
9941 /* Change window state from mapped to iconified. */
9943 void
9944 x_iconify_frame (struct frame *f)
9946 #ifdef USE_X_TOOLKIT
9947 int result;
9948 #endif
9950 /* Don't keep the highlight on an invisible frame. */
9951 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
9952 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
9954 if (FRAME_ICONIFIED_P (f))
9955 return;
9957 block_input ();
9959 x_set_bitmap_icon (f);
9961 #if defined (USE_GTK)
9962 if (FRAME_GTK_OUTER_WIDGET (f))
9964 if (! FRAME_VISIBLE_P (f))
9965 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9967 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9968 SET_FRAME_VISIBLE (f, 0);
9969 SET_FRAME_ICONIFIED (f, 1);
9970 unblock_input ();
9971 return;
9973 #endif
9975 #ifdef USE_X_TOOLKIT
9977 if (! FRAME_VISIBLE_P (f))
9979 if (! EQ (Vx_no_window_manager, Qt))
9980 x_wm_set_window_state (f, IconicState);
9981 /* This was XtPopup, but that did nothing for an iconified frame. */
9982 XtMapWidget (f->output_data.x->widget);
9983 /* The server won't give us any event to indicate
9984 that an invisible frame was changed to an icon,
9985 so we have to record it here. */
9986 SET_FRAME_VISIBLE (f, 0);
9987 SET_FRAME_ICONIFIED (f, 1);
9988 unblock_input ();
9989 return;
9992 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9993 XtWindow (f->output_data.x->widget),
9994 DefaultScreen (FRAME_X_DISPLAY (f)));
9995 unblock_input ();
9997 if (!result)
9998 error ("Can't notify window manager of iconification");
10000 SET_FRAME_ICONIFIED (f, 1);
10001 SET_FRAME_VISIBLE (f, 0);
10003 block_input ();
10004 XFlush (FRAME_X_DISPLAY (f));
10005 unblock_input ();
10006 #else /* not USE_X_TOOLKIT */
10008 /* Make sure the X server knows where the window should be positioned,
10009 in case the user deiconifies with the window manager. */
10010 if (! FRAME_VISIBLE_P (f)
10011 && ! FRAME_ICONIFIED_P (f)
10012 && ! FRAME_X_EMBEDDED_P (f))
10013 x_set_offset (f, f->left_pos, f->top_pos, 0);
10015 /* Since we don't know which revision of X we're running, we'll use both
10016 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
10018 /* X11R4: send a ClientMessage to the window manager using the
10019 WM_CHANGE_STATE type. */
10021 XEvent msg;
10023 msg.xclient.window = FRAME_X_WINDOW (f);
10024 msg.xclient.type = ClientMessage;
10025 msg.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_wm_change_state;
10026 msg.xclient.format = 32;
10027 msg.xclient.data.l[0] = IconicState;
10029 if (! XSendEvent (FRAME_X_DISPLAY (f),
10030 DefaultRootWindow (FRAME_X_DISPLAY (f)),
10031 False,
10032 SubstructureRedirectMask | SubstructureNotifyMask,
10033 &msg))
10035 unblock_input ();
10036 error ("Can't notify window manager of iconification");
10040 /* X11R3: set the initial_state field of the window manager hints to
10041 IconicState. */
10042 x_wm_set_window_state (f, IconicState);
10044 if (!FRAME_VISIBLE_P (f))
10046 /* If the frame was withdrawn, before, we must map it. */
10047 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10050 SET_FRAME_ICONIFIED (f, 1);
10051 SET_FRAME_VISIBLE (f, 0);
10053 XFlush (FRAME_X_DISPLAY (f));
10054 unblock_input ();
10055 #endif /* not USE_X_TOOLKIT */
10059 /* Free X resources of frame F. */
10061 void
10062 x_free_frame_resources (struct frame *f)
10064 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10065 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
10066 #ifdef USE_X_TOOLKIT
10067 Lisp_Object bar;
10068 struct scroll_bar *b;
10069 #endif
10071 block_input ();
10073 /* If a display connection is dead, don't try sending more
10074 commands to the X server. */
10075 if (dpyinfo->display)
10077 /* Always exit with visible pointer to avoid weird issue
10078 with Xfixes (Bug#17609). */
10079 if (f->pointer_invisible)
10080 FRAME_DISPLAY_INFO (f)->toggle_visible_pointer (f, 0);
10082 /* We must free faces before destroying windows because some
10083 font-driver (e.g. xft) access a window while finishing a
10084 face. */
10085 free_frame_faces (f);
10087 if (f->output_data.x->icon_desc)
10088 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
10090 #ifdef USE_X_TOOLKIT
10091 /* Explicitly destroy the scroll bars of the frame. Without
10092 this, we get "BadDrawable" errors from the toolkit later on,
10093 presumably from expose events generated for the disappearing
10094 toolkit scroll bars. */
10095 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
10097 b = XSCROLL_BAR (bar);
10098 x_scroll_bar_remove (b);
10100 #endif
10102 #ifdef HAVE_X_I18N
10103 if (FRAME_XIC (f))
10104 free_frame_xic (f);
10105 #endif
10107 #ifdef USE_X_TOOLKIT
10108 if (f->output_data.x->widget)
10110 XtDestroyWidget (f->output_data.x->widget);
10111 f->output_data.x->widget = NULL;
10113 /* Tooltips don't have widgets, only a simple X window, even if
10114 we are using a toolkit. */
10115 else if (FRAME_X_WINDOW (f))
10116 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10118 free_frame_menubar (f);
10120 if (f->shell_position)
10121 xfree (f->shell_position);
10122 #else /* !USE_X_TOOLKIT */
10124 #ifdef USE_GTK
10125 xg_free_frame_widgets (f);
10126 #endif /* USE_GTK */
10128 if (FRAME_X_WINDOW (f))
10129 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10130 #endif /* !USE_X_TOOLKIT */
10132 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
10133 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
10134 unload_color (f, f->output_data.x->cursor_pixel);
10135 unload_color (f, f->output_data.x->cursor_foreground_pixel);
10136 unload_color (f, f->output_data.x->border_pixel);
10137 unload_color (f, f->output_data.x->mouse_pixel);
10139 if (f->output_data.x->scroll_bar_background_pixel != -1)
10140 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
10141 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
10142 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
10143 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
10144 /* Scrollbar shadow colors. */
10145 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
10146 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
10147 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
10148 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
10149 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
10150 if (f->output_data.x->white_relief.pixel != -1)
10151 unload_color (f, f->output_data.x->white_relief.pixel);
10152 if (f->output_data.x->black_relief.pixel != -1)
10153 unload_color (f, f->output_data.x->black_relief.pixel);
10155 x_free_gcs (f);
10157 /* Free extra GCs allocated by x_setup_relief_colors. */
10158 if (f->output_data.x->white_relief.gc)
10160 XFreeGC (dpyinfo->display, f->output_data.x->white_relief.gc);
10161 f->output_data.x->white_relief.gc = 0;
10163 if (f->output_data.x->black_relief.gc)
10165 XFreeGC (dpyinfo->display, f->output_data.x->black_relief.gc);
10166 f->output_data.x->black_relief.gc = 0;
10169 /* Free cursors. */
10170 if (f->output_data.x->text_cursor != 0)
10171 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->text_cursor);
10172 if (f->output_data.x->nontext_cursor != 0)
10173 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->nontext_cursor);
10174 if (f->output_data.x->modeline_cursor != 0)
10175 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->modeline_cursor);
10176 if (f->output_data.x->hand_cursor != 0)
10177 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hand_cursor);
10178 if (f->output_data.x->hourglass_cursor != 0)
10179 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->hourglass_cursor);
10180 if (f->output_data.x->horizontal_drag_cursor != 0)
10181 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->horizontal_drag_cursor);
10182 if (f->output_data.x->vertical_drag_cursor != 0)
10183 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->vertical_drag_cursor);
10185 XFlush (FRAME_X_DISPLAY (f));
10188 xfree (f->output_data.x->saved_menu_event);
10189 xfree (f->output_data.x);
10190 f->output_data.x = NULL;
10192 if (f == dpyinfo->x_focus_frame)
10193 dpyinfo->x_focus_frame = 0;
10194 if (f == dpyinfo->x_focus_event_frame)
10195 dpyinfo->x_focus_event_frame = 0;
10196 if (f == dpyinfo->x_highlight_frame)
10197 dpyinfo->x_highlight_frame = 0;
10198 if (f == hlinfo->mouse_face_mouse_frame)
10199 reset_mouse_highlight (hlinfo);
10201 unblock_input ();
10205 /* Destroy the X window of frame F. */
10207 static void
10208 x_destroy_window (struct frame *f)
10210 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
10212 /* If a display connection is dead, don't try sending more
10213 commands to the X server. */
10214 if (dpyinfo->display != 0)
10215 x_free_frame_resources (f);
10217 dpyinfo->reference_count--;
10221 /* Setting window manager hints. */
10223 /* Set the normal size hints for the window manager, for frame F.
10224 FLAGS is the flags word to use--or 0 meaning preserve the flags
10225 that the window now has.
10226 If USER_POSITION, set the USPosition
10227 flag (this is useful when FLAGS is 0).
10228 The GTK version is in gtkutils.c. */
10230 #ifndef USE_GTK
10231 void
10232 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
10234 XSizeHints size_hints;
10235 Window window = FRAME_OUTER_WINDOW (f);
10237 if (!window)
10238 return;
10240 #ifdef USE_X_TOOLKIT
10241 if (f->output_data.x->widget)
10243 widget_update_wm_size_hints (f->output_data.x->widget);
10244 return;
10246 #endif
10248 /* Setting PMaxSize caused various problems. */
10249 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
10251 size_hints.x = f->left_pos;
10252 size_hints.y = f->top_pos;
10254 size_hints.height = FRAME_PIXEL_HEIGHT (f);
10255 size_hints.width = FRAME_PIXEL_WIDTH (f);
10257 size_hints.width_inc = frame_resize_pixelwise ? 1 : FRAME_COLUMN_WIDTH (f);
10258 size_hints.height_inc = frame_resize_pixelwise ? 1 : FRAME_LINE_HEIGHT (f);
10260 size_hints.max_width = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
10261 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
10262 size_hints.max_height = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
10263 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
10265 /* Calculate the base and minimum sizes. */
10267 int base_width, base_height;
10268 int min_rows = 0, min_cols = 0;
10270 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
10271 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
10273 if (frame_resize_pixelwise)
10274 /* Needed to prevent a bad protocol error crash when making the
10275 frame size very small. */
10277 min_cols = 2 * min_cols;
10278 min_rows = 2 * min_rows;
10281 /* The window manager uses the base width hints to calculate the
10282 current number of rows and columns in the frame while
10283 resizing; min_width and min_height aren't useful for this
10284 purpose, since they might not give the dimensions for a
10285 zero-row, zero-column frame.
10287 We use the base_width and base_height members if we have
10288 them; otherwise, we set the min_width and min_height members
10289 to the size for a zero x zero frame. */
10291 size_hints.flags |= PBaseSize;
10292 size_hints.base_width = base_width;
10293 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
10294 size_hints.min_width = base_width + min_cols * FRAME_COLUMN_WIDTH (f);
10295 size_hints.min_height = base_height + min_rows * FRAME_LINE_HEIGHT (f);
10298 /* If we don't need the old flags, we don't need the old hint at all. */
10299 if (flags)
10301 size_hints.flags |= flags;
10302 goto no_read;
10306 XSizeHints hints; /* Sometimes I hate X Windows... */
10307 long supplied_return;
10308 int value;
10310 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
10311 &supplied_return);
10313 if (flags)
10314 size_hints.flags |= flags;
10315 else
10317 if (value == 0)
10318 hints.flags = 0;
10319 if (hints.flags & PSize)
10320 size_hints.flags |= PSize;
10321 if (hints.flags & PPosition)
10322 size_hints.flags |= PPosition;
10323 if (hints.flags & USPosition)
10324 size_hints.flags |= USPosition;
10325 if (hints.flags & USSize)
10326 size_hints.flags |= USSize;
10330 no_read:
10332 #ifdef PWinGravity
10333 size_hints.win_gravity = f->win_gravity;
10334 size_hints.flags |= PWinGravity;
10336 if (user_position)
10338 size_hints.flags &= ~ PPosition;
10339 size_hints.flags |= USPosition;
10341 #endif /* PWinGravity */
10343 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
10345 #endif /* not USE_GTK */
10347 /* Used for IconicState or NormalState */
10349 static void
10350 x_wm_set_window_state (struct frame *f, int state)
10352 #ifdef USE_X_TOOLKIT
10353 Arg al[1];
10355 XtSetArg (al[0], XtNinitialState, state);
10356 XtSetValues (f->output_data.x->widget, al, 1);
10357 #else /* not USE_X_TOOLKIT */
10358 Window window = FRAME_X_WINDOW (f);
10360 f->output_data.x->wm_hints.flags |= StateHint;
10361 f->output_data.x->wm_hints.initial_state = state;
10363 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
10364 #endif /* not USE_X_TOOLKIT */
10367 static void
10368 x_wm_set_icon_pixmap (struct frame *f, ptrdiff_t pixmap_id)
10370 Pixmap icon_pixmap, icon_mask;
10372 #if !defined USE_X_TOOLKIT && !defined USE_GTK
10373 Window window = FRAME_OUTER_WINDOW (f);
10374 #endif
10376 if (pixmap_id > 0)
10378 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
10379 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
10380 icon_mask = x_bitmap_mask (f, pixmap_id);
10381 f->output_data.x->wm_hints.icon_mask = icon_mask;
10383 else
10385 /* It seems there is no way to turn off use of an icon
10386 pixmap. */
10387 return;
10391 #ifdef USE_GTK
10393 xg_set_frame_icon (f, icon_pixmap, icon_mask);
10394 return;
10397 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
10400 Arg al[1];
10401 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
10402 XtSetValues (f->output_data.x->widget, al, 1);
10403 XtSetArg (al[0], XtNiconMask, icon_mask);
10404 XtSetValues (f->output_data.x->widget, al, 1);
10407 #else /* not USE_X_TOOLKIT && not USE_GTK */
10409 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
10410 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
10412 #endif /* not USE_X_TOOLKIT && not USE_GTK */
10415 void
10416 x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
10418 Window window = FRAME_OUTER_WINDOW (f);
10420 f->output_data.x->wm_hints.flags |= IconPositionHint;
10421 f->output_data.x->wm_hints.icon_x = icon_x;
10422 f->output_data.x->wm_hints.icon_y = icon_y;
10424 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
10428 /***********************************************************************
10429 Fonts
10430 ***********************************************************************/
10432 #ifdef GLYPH_DEBUG
10434 /* Check that FONT is valid on frame F. It is if it can be found in F's
10435 font table. */
10437 static void
10438 x_check_font (struct frame *f, struct font *font)
10440 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
10441 if (font->driver->check)
10442 eassert (font->driver->check (f, font) == 0);
10445 #endif /* GLYPH_DEBUG */
10448 /***********************************************************************
10449 Initialization
10450 ***********************************************************************/
10452 #ifdef USE_X_TOOLKIT
10453 static XrmOptionDescRec emacs_options[] = {
10454 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
10455 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
10457 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
10458 XrmoptionSepArg, NULL},
10459 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
10461 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10462 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10463 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10464 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10465 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10466 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
10467 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
10470 /* Whether atimer for Xt timeouts is activated or not. */
10472 static int x_timeout_atimer_activated_flag;
10474 #endif /* USE_X_TOOLKIT */
10476 static int x_initialized;
10478 /* Test whether two display-name strings agree up to the dot that separates
10479 the screen number from the server number. */
10480 static int
10481 same_x_server (const char *name1, const char *name2)
10483 int seen_colon = 0;
10484 const char *system_name = SSDATA (Vsystem_name);
10485 ptrdiff_t system_name_length = SBYTES (Vsystem_name);
10486 ptrdiff_t length_until_period = 0;
10488 while (system_name[length_until_period] != 0
10489 && system_name[length_until_period] != '.')
10490 length_until_period++;
10492 /* Treat `unix' like an empty host name. */
10493 if (! strncmp (name1, "unix:", 5))
10494 name1 += 4;
10495 if (! strncmp (name2, "unix:", 5))
10496 name2 += 4;
10497 /* Treat this host's name like an empty host name. */
10498 if (! strncmp (name1, system_name, system_name_length)
10499 && name1[system_name_length] == ':')
10500 name1 += system_name_length;
10501 if (! strncmp (name2, system_name, system_name_length)
10502 && name2[system_name_length] == ':')
10503 name2 += system_name_length;
10504 /* Treat this host's domainless name like an empty host name. */
10505 if (! strncmp (name1, system_name, length_until_period)
10506 && name1[length_until_period] == ':')
10507 name1 += length_until_period;
10508 if (! strncmp (name2, system_name, length_until_period)
10509 && name2[length_until_period] == ':')
10510 name2 += length_until_period;
10512 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
10514 if (*name1 == ':')
10515 seen_colon = 1;
10516 if (seen_colon && *name1 == '.')
10517 return 1;
10519 return (seen_colon
10520 && (*name1 == '.' || *name1 == '\0')
10521 && (*name2 == '.' || *name2 == '\0'));
10524 /* Count number of set bits in mask and number of bits to shift to
10525 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
10526 to 5. */
10527 static void
10528 get_bits_and_offset (unsigned long mask, int *bits, int *offset)
10530 int nr = 0;
10531 int off = 0;
10533 while (!(mask & 1))
10535 off++;
10536 mask >>= 1;
10539 while (mask & 1)
10541 nr++;
10542 mask >>= 1;
10545 *offset = off;
10546 *bits = nr;
10549 /* Return 1 if display DISPLAY is available for use, 0 otherwise.
10550 But don't permanently open it, just test its availability. */
10552 bool
10553 x_display_ok (const char *display)
10555 Display *dpy = XOpenDisplay (display);
10556 return dpy ? (XCloseDisplay (dpy), 1) : 0;
10559 #ifdef USE_GTK
10560 static void
10561 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
10562 const gchar *msg, gpointer user_data)
10564 if (!strstr (msg, "g_set_prgname"))
10565 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, msg);
10567 #endif
10569 /* Create invisible cursor on X display referred by DPYINFO. */
10571 static Cursor
10572 make_invisible_cursor (struct x_display_info *dpyinfo)
10574 Display *dpy = dpyinfo->display;
10575 static char const no_data[] = { 0 };
10576 Pixmap pix;
10577 XColor col;
10578 Cursor c = 0;
10580 x_catch_errors (dpy);
10581 pix = XCreateBitmapFromData (dpy, dpyinfo->root_window, no_data, 1, 1);
10582 if (! x_had_errors_p (dpy) && pix != None)
10584 Cursor pixc;
10585 col.pixel = 0;
10586 col.red = col.green = col.blue = 0;
10587 col.flags = DoRed | DoGreen | DoBlue;
10588 pixc = XCreatePixmapCursor (dpy, pix, pix, &col, &col, 0, 0);
10589 if (! x_had_errors_p (dpy) && pixc != None)
10590 c = pixc;
10591 XFreePixmap (dpy, pix);
10594 x_uncatch_errors ();
10596 return c;
10599 /* True if DPY supports Xfixes extension >= 4. */
10601 static bool
10602 x_probe_xfixes_extension (Display *dpy)
10604 #ifdef HAVE_XFIXES
10605 int major, minor;
10606 return XFixesQueryVersion (dpy, &major, &minor) && major >= 4;
10607 #else
10608 return false;
10609 #endif /* HAVE_XFIXES */
10612 /* Toggle mouse pointer visibility on frame F by using Xfixes functions. */
10614 static void
10615 xfixes_toggle_visible_pointer (struct frame *f, bool invisible)
10617 #ifdef HAVE_XFIXES
10618 if (invisible)
10619 XFixesHideCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10620 else
10621 XFixesShowCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10622 f->pointer_invisible = invisible;
10623 #else
10624 emacs_abort ();
10625 #endif /* HAVE_XFIXES */
10628 /* Toggle mouse pointer visibility on frame F by using invisible cursor. */
10630 static void
10631 x_toggle_visible_pointer (struct frame *f, bool invisible)
10633 eassert (FRAME_DISPLAY_INFO (f)->invisible_cursor != 0);
10634 if (invisible)
10635 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10636 FRAME_DISPLAY_INFO (f)->invisible_cursor);
10637 else
10638 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10639 f->output_data.x->current_cursor);
10640 f->pointer_invisible = invisible;
10643 /* Setup pointer blanking, prefer Xfixes if available. */
10645 static void
10646 x_setup_pointer_blanking (struct x_display_info *dpyinfo)
10648 /* FIXME: the brave tester should set EMACS_XFIXES because we're suspecting
10649 X server bug, see http://debbugs.gnu.org/cgi/bugreport.cgi?bug=17609. */
10650 if (egetenv ("EMACS_XFIXES") && x_probe_xfixes_extension (dpyinfo->display))
10651 dpyinfo->toggle_visible_pointer = xfixes_toggle_visible_pointer;
10652 else
10654 dpyinfo->toggle_visible_pointer = x_toggle_visible_pointer;
10655 dpyinfo->invisible_cursor = make_invisible_cursor (dpyinfo);
10659 /* Current X display connection identifier. Incremented for each next
10660 connection established. */
10661 static unsigned x_display_id;
10663 /* Open a connection to X display DISPLAY_NAME, and return
10664 the structure that describes the open display.
10665 If we cannot contact the display, return null. */
10667 struct x_display_info *
10668 x_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
10670 int connection;
10671 Display *dpy;
10672 struct terminal *terminal;
10673 struct x_display_info *dpyinfo;
10674 XrmDatabase xrdb;
10675 ptrdiff_t lim;
10677 block_input ();
10679 if (!x_initialized)
10681 x_initialize ();
10682 ++x_initialized;
10685 if (! x_display_ok (SSDATA (display_name)))
10686 error ("Display %s can't be opened", SSDATA (display_name));
10688 #ifdef USE_GTK
10690 #define NUM_ARGV 10
10691 int argc;
10692 char *argv[NUM_ARGV];
10693 char **argv2 = argv;
10694 guint id;
10696 if (x_initialized++ > 1)
10698 xg_display_open (SSDATA (display_name), &dpy);
10700 else
10702 static char display_opt[] = "--display";
10703 static char name_opt[] = "--name";
10705 for (argc = 0; argc < NUM_ARGV; ++argc)
10706 argv[argc] = 0;
10708 argc = 0;
10709 argv[argc++] = initial_argv[0];
10711 if (! NILP (display_name))
10713 argv[argc++] = display_opt;
10714 argv[argc++] = SSDATA (display_name);
10717 argv[argc++] = name_opt;
10718 argv[argc++] = resource_name;
10720 XSetLocaleModifiers ("");
10722 /* Emacs can only handle core input events, so make sure
10723 Gtk doesn't use Xinput or Xinput2 extensions. */
10724 xputenv ("GDK_CORE_DEVICE_EVENTS=1");
10726 /* Work around GLib bug that outputs a faulty warning. See
10727 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
10728 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
10729 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
10731 /* NULL window -> events for all windows go to our function.
10732 Call before gtk_init so Gtk+ event filters comes after our. */
10733 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10735 /* gtk_init does set_locale. Fix locale before and after. */
10736 fixup_locale ();
10737 gtk_init (&argc, &argv2);
10738 fixup_locale ();
10740 g_log_remove_handler ("GLib", id);
10742 xg_initialize ();
10744 dpy = DEFAULT_GDK_DISPLAY ();
10746 #if ! GTK_CHECK_VERSION (2, 90, 0)
10747 /* Load our own gtkrc if it exists. */
10749 const char *file = "~/.emacs.d/gtkrc";
10750 Lisp_Object s, abs_file;
10752 s = build_string (file);
10753 abs_file = Fexpand_file_name (s, Qnil);
10755 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10756 gtk_rc_parse (SSDATA (abs_file));
10758 #endif
10760 XSetErrorHandler (x_error_handler);
10761 XSetIOErrorHandler (x_io_error_quitter);
10764 #else /* not USE_GTK */
10765 #ifdef USE_X_TOOLKIT
10766 /* weiner@footloose.sps.mot.com reports that this causes
10767 errors with X11R5:
10768 X protocol error: BadAtom (invalid Atom parameter)
10769 on protocol request 18skiloaf.
10770 So let's not use it until R6. */
10771 #ifdef HAVE_X11XTR6
10772 XtSetLanguageProc (NULL, NULL, NULL);
10773 #endif
10776 int argc = 0;
10777 char *argv[3];
10779 argv[0] = "";
10780 argc = 1;
10781 if (xrm_option)
10783 argv[argc++] = "-xrm";
10784 argv[argc++] = xrm_option;
10786 turn_on_atimers (0);
10787 dpy = XtOpenDisplay (Xt_app_con, SSDATA (display_name),
10788 resource_name, EMACS_CLASS,
10789 emacs_options, XtNumber (emacs_options),
10790 &argc, argv);
10791 turn_on_atimers (1);
10793 #ifdef HAVE_X11XTR6
10794 /* I think this is to compensate for XtSetLanguageProc. */
10795 fixup_locale ();
10796 #endif
10799 #else /* not USE_X_TOOLKIT */
10800 XSetLocaleModifiers ("");
10801 dpy = XOpenDisplay (SSDATA (display_name));
10802 #endif /* not USE_X_TOOLKIT */
10803 #endif /* not USE_GTK*/
10805 /* Detect failure. */
10806 if (dpy == 0)
10808 unblock_input ();
10809 return 0;
10812 /* We have definitely succeeded. Record the new connection. */
10814 dpyinfo = xzalloc (sizeof *dpyinfo);
10815 terminal = x_create_terminal (dpyinfo);
10818 struct x_display_info *share;
10820 for (share = x_display_list; share; share = share->next)
10821 if (same_x_server (SSDATA (XCAR (share->name_list_element)),
10822 SSDATA (display_name)))
10823 break;
10824 if (share)
10825 terminal->kboard = share->terminal->kboard;
10826 else
10828 terminal->kboard = allocate_kboard (Qx);
10830 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10832 char *vendor = ServerVendor (dpy);
10834 /* Protect terminal from GC before removing it from the
10835 list of terminals. */
10836 struct gcpro gcpro1;
10837 Lisp_Object gcpro_term;
10838 XSETTERMINAL (gcpro_term, terminal);
10839 GCPRO1 (gcpro_term);
10841 /* Temporarily hide the partially initialized terminal. */
10842 terminal_list = terminal->next_terminal;
10843 unblock_input ();
10844 kset_system_key_alist
10845 (terminal->kboard,
10846 call1 (Qvendor_specific_keysyms,
10847 vendor ? build_string (vendor) : empty_unibyte_string));
10848 block_input ();
10849 terminal->next_terminal = terminal_list;
10850 terminal_list = terminal;
10851 UNGCPRO;
10854 /* Don't let the initial kboard remain current longer than necessary.
10855 That would cause problems if a file loaded on startup tries to
10856 prompt in the mini-buffer. */
10857 if (current_kboard == initial_kboard)
10858 current_kboard = terminal->kboard;
10860 terminal->kboard->reference_count++;
10863 /* Put this display on the chain. */
10864 dpyinfo->next = x_display_list;
10865 x_display_list = dpyinfo;
10867 dpyinfo->name_list_element = Fcons (display_name, Qnil);
10868 dpyinfo->display = dpy;
10869 dpyinfo->connection = ConnectionNumber (dpyinfo->display);
10871 /* Set the name of the terminal. */
10872 terminal->name = xlispstrdup (display_name);
10874 #if 0
10875 XSetAfterFunction (x_current_display, x_trace_wire);
10876 #endif /* ! 0 */
10878 lim = min (PTRDIFF_MAX, SIZE_MAX) - sizeof "@";
10879 if (lim - SBYTES (Vinvocation_name) < SBYTES (Vsystem_name))
10880 memory_full (SIZE_MAX);
10881 dpyinfo->x_id = ++x_display_id;
10882 dpyinfo->x_id_name = xmalloc (SBYTES (Vinvocation_name)
10883 + SBYTES (Vsystem_name) + 2);
10884 strcat (strcat (strcpy (dpyinfo->x_id_name, SSDATA (Vinvocation_name)), "@"),
10885 SSDATA (Vsystem_name));
10887 /* Figure out which modifier bits mean what. */
10888 x_find_modifier_meanings (dpyinfo);
10890 /* Get the scroll bar cursor. */
10891 #ifdef USE_GTK
10892 /* We must create a GTK cursor, it is required for GTK widgets. */
10893 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10894 #endif /* USE_GTK */
10896 dpyinfo->vertical_scroll_bar_cursor
10897 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10899 dpyinfo->horizontal_scroll_bar_cursor
10900 = XCreateFontCursor (dpyinfo->display, XC_sb_h_double_arrow);
10902 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10903 resource_name, EMACS_CLASS);
10904 #ifdef HAVE_XRMSETDATABASE
10905 XrmSetDatabase (dpyinfo->display, xrdb);
10906 #else
10907 dpyinfo->display->db = xrdb;
10908 #endif
10909 /* Put the rdb where we can find it in a way that works on
10910 all versions. */
10911 dpyinfo->xrdb = xrdb;
10913 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10914 DefaultScreen (dpyinfo->display));
10915 select_visual (dpyinfo);
10916 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10917 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10918 dpyinfo->icon_bitmap_id = -1;
10919 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10921 reset_mouse_highlight (&dpyinfo->mouse_highlight);
10923 /* See if we can construct pixel values from RGB values. */
10924 if (dpyinfo->visual->class == TrueColor)
10926 get_bits_and_offset (dpyinfo->visual->red_mask,
10927 &dpyinfo->red_bits, &dpyinfo->red_offset);
10928 get_bits_and_offset (dpyinfo->visual->blue_mask,
10929 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10930 get_bits_and_offset (dpyinfo->visual->green_mask,
10931 &dpyinfo->green_bits, &dpyinfo->green_offset);
10934 /* See if a private colormap is requested. */
10935 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10937 if (dpyinfo->visual->class == PseudoColor)
10939 Lisp_Object value;
10940 value = display_x_get_resource (dpyinfo,
10941 build_string ("privateColormap"),
10942 build_string ("PrivateColormap"),
10943 Qnil, Qnil);
10944 if (STRINGP (value)
10945 && (!strcmp (SSDATA (value), "true")
10946 || !strcmp (SSDATA (value), "on")))
10947 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10950 else
10951 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10952 dpyinfo->visual, AllocNone);
10954 #ifdef HAVE_XFT
10956 /* If we are using Xft, the following precautions should be made:
10958 1. Make sure that the Xrender extension is added before the Xft one.
10959 Otherwise, the close-display hook set by Xft is called after the one
10960 for Xrender, and the former tries to re-add the latter. This results
10961 in inconsistency of internal states and leads to X protocol error when
10962 one reconnects to the same X server (Bug#1696).
10964 2. Check dpi value in X resources. It is better we use it as well,
10965 since Xft will use it, as will all Gnome applications. If our real DPI
10966 is smaller or larger than the one Xft uses, our font will look smaller
10967 or larger than other for other applications, even if it is the same
10968 font name (monospace-10 for example). */
10970 int event_base, error_base;
10971 char *v;
10972 double d;
10974 XRenderQueryExtension (dpyinfo->display, &event_base, &error_base);
10976 v = XGetDefault (dpyinfo->display, "Xft", "dpi");
10977 if (v != NULL && sscanf (v, "%lf", &d) == 1)
10978 dpyinfo->resy = dpyinfo->resx = d;
10980 #endif
10982 if (dpyinfo->resy < 1)
10984 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10985 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10986 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10987 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10988 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
10989 pixels = DisplayWidth (dpyinfo->display, screen_number);
10990 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10991 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10992 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
10996 static const struct
10998 const char *name;
10999 int offset;
11000 } atom_refs[] = {
11001 #define ATOM_REFS_INIT(string, member) \
11002 { string, offsetof (struct x_display_info, member) },
11003 ATOM_REFS_INIT ("WM_PROTOCOLS", Xatom_wm_protocols)
11004 ATOM_REFS_INIT ("WM_TAKE_FOCUS", Xatom_wm_take_focus)
11005 ATOM_REFS_INIT ("WM_SAVE_YOURSELF", Xatom_wm_save_yourself)
11006 ATOM_REFS_INIT ("WM_DELETE_WINDOW", Xatom_wm_delete_window)
11007 ATOM_REFS_INIT ("WM_CHANGE_STATE", Xatom_wm_change_state)
11008 ATOM_REFS_INIT ("WM_CONFIGURE_DENIED", Xatom_wm_configure_denied)
11009 ATOM_REFS_INIT ("WM_MOVED", Xatom_wm_window_moved)
11010 ATOM_REFS_INIT ("WM_CLIENT_LEADER", Xatom_wm_client_leader)
11011 ATOM_REFS_INIT ("Editres", Xatom_editres)
11012 ATOM_REFS_INIT ("CLIPBOARD", Xatom_CLIPBOARD)
11013 ATOM_REFS_INIT ("TIMESTAMP", Xatom_TIMESTAMP)
11014 ATOM_REFS_INIT ("TEXT", Xatom_TEXT)
11015 ATOM_REFS_INIT ("COMPOUND_TEXT", Xatom_COMPOUND_TEXT)
11016 ATOM_REFS_INIT ("UTF8_STRING", Xatom_UTF8_STRING)
11017 ATOM_REFS_INIT ("DELETE", Xatom_DELETE)
11018 ATOM_REFS_INIT ("MULTIPLE", Xatom_MULTIPLE)
11019 ATOM_REFS_INIT ("INCR", Xatom_INCR)
11020 ATOM_REFS_INIT ("_EMACS_TMP_", Xatom_EMACS_TMP)
11021 ATOM_REFS_INIT ("TARGETS", Xatom_TARGETS)
11022 ATOM_REFS_INIT ("NULL", Xatom_NULL)
11023 ATOM_REFS_INIT ("ATOM", Xatom_ATOM)
11024 ATOM_REFS_INIT ("ATOM_PAIR", Xatom_ATOM_PAIR)
11025 ATOM_REFS_INIT ("CLIPBOARD_MANAGER", Xatom_CLIPBOARD_MANAGER)
11026 ATOM_REFS_INIT ("_XEMBED_INFO", Xatom_XEMBED_INFO)
11027 /* For properties of font. */
11028 ATOM_REFS_INIT ("PIXEL_SIZE", Xatom_PIXEL_SIZE)
11029 ATOM_REFS_INIT ("AVERAGE_WIDTH", Xatom_AVERAGE_WIDTH)
11030 ATOM_REFS_INIT ("_MULE_BASELINE_OFFSET", Xatom_MULE_BASELINE_OFFSET)
11031 ATOM_REFS_INIT ("_MULE_RELATIVE_COMPOSE", Xatom_MULE_RELATIVE_COMPOSE)
11032 ATOM_REFS_INIT ("_MULE_DEFAULT_ASCENT", Xatom_MULE_DEFAULT_ASCENT)
11033 /* Ghostscript support. */
11034 ATOM_REFS_INIT ("DONE", Xatom_DONE)
11035 ATOM_REFS_INIT ("PAGE", Xatom_PAGE)
11036 ATOM_REFS_INIT ("SCROLLBAR", Xatom_Scrollbar)
11037 ATOM_REFS_INIT ("HORIZONTAL_SCROLLBAR", Xatom_Horizontal_Scrollbar)
11038 ATOM_REFS_INIT ("_XEMBED", Xatom_XEMBED)
11039 /* EWMH */
11040 ATOM_REFS_INIT ("_NET_WM_STATE", Xatom_net_wm_state)
11041 ATOM_REFS_INIT ("_NET_WM_STATE_FULLSCREEN", Xatom_net_wm_state_fullscreen)
11042 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_HORZ",
11043 Xatom_net_wm_state_maximized_horz)
11044 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_VERT",
11045 Xatom_net_wm_state_maximized_vert)
11046 ATOM_REFS_INIT ("_NET_WM_STATE_STICKY", Xatom_net_wm_state_sticky)
11047 ATOM_REFS_INIT ("_NET_WM_STATE_HIDDEN", Xatom_net_wm_state_hidden)
11048 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE", Xatom_net_window_type)
11049 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE_TOOLTIP",
11050 Xatom_net_window_type_tooltip)
11051 ATOM_REFS_INIT ("_NET_WM_ICON_NAME", Xatom_net_wm_icon_name)
11052 ATOM_REFS_INIT ("_NET_WM_NAME", Xatom_net_wm_name)
11053 ATOM_REFS_INIT ("_NET_SUPPORTED", Xatom_net_supported)
11054 ATOM_REFS_INIT ("_NET_SUPPORTING_WM_CHECK", Xatom_net_supporting_wm_check)
11055 ATOM_REFS_INIT ("_NET_WM_WINDOW_OPACITY", Xatom_net_wm_window_opacity)
11056 ATOM_REFS_INIT ("_NET_ACTIVE_WINDOW", Xatom_net_active_window)
11057 ATOM_REFS_INIT ("_NET_FRAME_EXTENTS", Xatom_net_frame_extents)
11058 ATOM_REFS_INIT ("_NET_CURRENT_DESKTOP", Xatom_net_current_desktop)
11059 ATOM_REFS_INIT ("_NET_WORKAREA", Xatom_net_workarea)
11060 /* Session management */
11061 ATOM_REFS_INIT ("SM_CLIENT_ID", Xatom_SM_CLIENT_ID)
11062 ATOM_REFS_INIT ("_XSETTINGS_SETTINGS", Xatom_xsettings_prop)
11063 ATOM_REFS_INIT ("MANAGER", Xatom_xsettings_mgr)
11066 int i;
11067 const int atom_count = ARRAYELTS (atom_refs);
11068 /* 1 for _XSETTINGS_SN */
11069 const int total_atom_count = 1 + atom_count;
11070 Atom *atoms_return = xmalloc (total_atom_count * sizeof *atoms_return);
11071 char **atom_names = xmalloc (total_atom_count * sizeof *atom_names);
11072 static char const xsettings_fmt[] = "_XSETTINGS_S%d";
11073 char xsettings_atom_name[sizeof xsettings_fmt - 2
11074 + INT_STRLEN_BOUND (int)];
11076 for (i = 0; i < atom_count; i++)
11077 atom_names[i] = (char *) atom_refs[i].name;
11079 /* Build _XSETTINGS_SN atom name */
11080 sprintf (xsettings_atom_name, xsettings_fmt,
11081 XScreenNumberOfScreen (dpyinfo->screen));
11082 atom_names[i] = xsettings_atom_name;
11084 XInternAtoms (dpyinfo->display, atom_names, total_atom_count,
11085 False, atoms_return);
11087 for (i = 0; i < atom_count; i++)
11088 *(Atom *) ((char *) dpyinfo + atom_refs[i].offset) = atoms_return[i];
11090 /* Manual copy of last atom */
11091 dpyinfo->Xatom_xsettings_sel = atoms_return[i];
11093 xfree (atom_names);
11094 xfree (atoms_return);
11097 dpyinfo->x_dnd_atoms_size = 8;
11098 dpyinfo->x_dnd_atoms = xmalloc (sizeof *dpyinfo->x_dnd_atoms
11099 * dpyinfo->x_dnd_atoms_size);
11100 dpyinfo->gray
11101 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
11102 gray_bits, gray_width, gray_height,
11103 1, 0, 1);
11105 x_setup_pointer_blanking (dpyinfo);
11107 #ifdef HAVE_X_I18N
11108 xim_initialize (dpyinfo, resource_name);
11109 #endif
11111 xsettings_initialize (dpyinfo);
11113 connection = ConnectionNumber (dpyinfo->display);
11115 /* This is only needed for distinguishing keyboard and process input. */
11116 if (connection != 0)
11117 add_keyboard_wait_descriptor (connection);
11119 #ifdef F_SETOWN
11120 fcntl (connection, F_SETOWN, getpid ());
11121 #endif /* ! defined (F_SETOWN) */
11123 if (interrupt_input)
11124 init_sigio (connection);
11126 #ifdef USE_LUCID
11128 XFontStruct *xfont = NULL;
11129 XrmValue d, fr, to;
11130 Font font;
11132 dpy = dpyinfo->display;
11133 d.addr = (XPointer)&dpy;
11134 d.size = sizeof (Display *);
11135 fr.addr = XtDefaultFont;
11136 fr.size = sizeof (XtDefaultFont);
11137 to.size = sizeof (Font *);
11138 to.addr = (XPointer)&font;
11139 x_catch_errors (dpy);
11140 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
11141 emacs_abort ();
11142 if (x_had_errors_p (dpy) || !((xfont = XQueryFont (dpy, font))))
11143 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
11144 if (xfont)
11145 XFreeFont (dpy, xfont);
11146 x_uncatch_errors ();
11148 #endif
11150 /* See if we should run in synchronous mode. This is useful
11151 for debugging X code. */
11153 Lisp_Object value;
11154 value = display_x_get_resource (dpyinfo,
11155 build_string ("synchronous"),
11156 build_string ("Synchronous"),
11157 Qnil, Qnil);
11158 if (STRINGP (value)
11159 && (!strcmp (SSDATA (value), "true")
11160 || !strcmp (SSDATA (value), "on")))
11161 XSynchronize (dpyinfo->display, True);
11165 Lisp_Object value;
11166 value = display_x_get_resource (dpyinfo,
11167 build_string ("useXIM"),
11168 build_string ("UseXIM"),
11169 Qnil, Qnil);
11170 #ifdef USE_XIM
11171 if (STRINGP (value)
11172 && (!strcmp (SSDATA (value), "false")
11173 || !strcmp (SSDATA (value), "off")))
11174 use_xim = false;
11175 #else
11176 if (STRINGP (value)
11177 && (!strcmp (SSDATA (value), "true")
11178 || !strcmp (SSDATA (value), "on")))
11179 use_xim = true;
11180 #endif
11183 #ifdef HAVE_X_SM
11184 /* Only do this for the very first display in the Emacs session.
11185 Ignore X session management when Emacs was first started on a
11186 tty. */
11187 if (terminal->id == 1)
11188 x_session_initialize (dpyinfo);
11189 #endif
11191 unblock_input ();
11193 return dpyinfo;
11196 /* Get rid of display DPYINFO, deleting all frames on it,
11197 and without sending any more commands to the X server. */
11199 static void
11200 x_delete_display (struct x_display_info *dpyinfo)
11202 struct terminal *t;
11204 /* Close all frames and delete the generic struct terminal for this
11205 X display. */
11206 for (t = terminal_list; t; t = t->next_terminal)
11207 if (t->type == output_x_window && t->display_info.x == dpyinfo)
11209 #ifdef HAVE_X_SM
11210 /* Close X session management when we close its display. */
11211 if (t->id == 1 && x_session_have_connection ())
11212 x_session_close ();
11213 #endif
11214 delete_terminal (t);
11215 break;
11218 if (next_noop_dpyinfo == dpyinfo)
11219 next_noop_dpyinfo = dpyinfo->next;
11221 if (x_display_list == dpyinfo)
11222 x_display_list = dpyinfo->next;
11223 else
11225 struct x_display_info *tail;
11227 for (tail = x_display_list; tail; tail = tail->next)
11228 if (tail->next == dpyinfo)
11229 tail->next = tail->next->next;
11232 xfree (dpyinfo->x_id_name);
11233 xfree (dpyinfo->x_dnd_atoms);
11234 xfree (dpyinfo->color_cells);
11235 xfree (dpyinfo);
11238 #ifdef USE_X_TOOLKIT
11240 /* Atimer callback function for TIMER. Called every 0.1s to process
11241 Xt timeouts, if needed. We must avoid calling XtAppPending as
11242 much as possible because that function does an implicit XFlush
11243 that slows us down. */
11245 static void
11246 x_process_timeouts (struct atimer *timer)
11248 block_input ();
11249 x_timeout_atimer_activated_flag = 0;
11250 if (toolkit_scroll_bar_interaction || popup_activated ())
11252 while (XtAppPending (Xt_app_con) & XtIMTimer)
11253 XtAppProcessEvent (Xt_app_con, XtIMTimer);
11254 /* Reactivate the atimer for next time. */
11255 x_activate_timeout_atimer ();
11257 unblock_input ();
11260 /* Install an asynchronous timer that processes Xt timeout events
11261 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
11262 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
11263 function whenever these variables are set. This is necessary
11264 because some widget sets use timeouts internally, for example the
11265 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
11266 processed, these widgets don't behave normally. */
11268 void
11269 x_activate_timeout_atimer (void)
11271 block_input ();
11272 if (!x_timeout_atimer_activated_flag)
11274 struct timespec interval = make_timespec (0, 100 * 1000 * 1000);
11275 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
11276 x_timeout_atimer_activated_flag = 1;
11278 unblock_input ();
11281 #endif /* USE_X_TOOLKIT */
11284 /* Set up use of X before we make the first connection. */
11286 static struct redisplay_interface x_redisplay_interface =
11288 x_frame_parm_handlers,
11289 x_produce_glyphs,
11290 x_write_glyphs,
11291 x_insert_glyphs,
11292 x_clear_end_of_line,
11293 x_scroll_run,
11294 x_after_update_window_line,
11295 x_update_window_begin,
11296 x_update_window_end,
11297 x_flush,
11298 x_clear_window_mouse_face,
11299 x_get_glyph_overhangs,
11300 x_fix_overlapping_area,
11301 x_draw_fringe_bitmap,
11302 0, /* define_fringe_bitmap */
11303 0, /* destroy_fringe_bitmap */
11304 x_compute_glyph_string_overhangs,
11305 x_draw_glyph_string,
11306 x_define_frame_cursor,
11307 x_clear_frame_area,
11308 x_draw_window_cursor,
11309 x_draw_vertical_window_border,
11310 x_draw_window_divider,
11311 x_shift_glyphs_for_insert,
11312 x_show_hourglass,
11313 x_hide_hourglass
11317 /* This function is called when the last frame on a display is deleted. */
11318 void
11319 x_delete_terminal (struct terminal *terminal)
11321 struct x_display_info *dpyinfo = terminal->display_info.x;
11323 /* Protect against recursive calls. delete_frame in
11324 delete_terminal calls us back when it deletes our last frame. */
11325 if (!terminal->name)
11326 return;
11328 block_input ();
11329 #ifdef HAVE_X_I18N
11330 /* We must close our connection to the XIM server before closing the
11331 X display. */
11332 if (dpyinfo->xim)
11333 xim_close_dpy (dpyinfo);
11334 #endif
11336 /* If called from x_connection_closed, the display may already be closed
11337 and dpyinfo->display was set to 0 to indicate that. */
11338 if (dpyinfo->display)
11340 x_destroy_all_bitmaps (dpyinfo);
11341 XSetCloseDownMode (dpyinfo->display, DestroyAll);
11343 /* Whether or not XCloseDisplay destroys the associated resource
11344 database depends on the version of libX11. To avoid both
11345 crash and memory leak, we dissociate the database from the
11346 display and then destroy dpyinfo->xrdb ourselves.
11348 Unfortunately, the above strategy does not work in some
11349 situations due to a bug in newer versions of libX11: because
11350 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
11351 dpy->db is NULL, XCloseDisplay destroys the associated
11352 database whereas it has not been created by XGetDefault
11353 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
11354 don't destroy the database here in order to avoid the crash
11355 in the above situations for now, though that may cause memory
11356 leaks in other situations. */
11357 #if 0
11358 #ifdef HAVE_XRMSETDATABASE
11359 XrmSetDatabase (dpyinfo->display, NULL);
11360 #else
11361 dpyinfo->display->db = NULL;
11362 #endif
11363 /* We used to call XrmDestroyDatabase from x_delete_display, but
11364 some older versions of libX11 crash if we call it after
11365 closing all the displays. */
11366 XrmDestroyDatabase (dpyinfo->xrdb);
11367 #endif
11369 #ifdef USE_GTK
11370 xg_display_close (dpyinfo->display);
11371 #else
11372 #ifdef USE_X_TOOLKIT
11373 XtCloseDisplay (dpyinfo->display);
11374 #else
11375 XCloseDisplay (dpyinfo->display);
11376 #endif
11377 #endif /* ! USE_GTK */
11380 /* No more input on this descriptor. */
11381 if (0 <= dpyinfo->connection)
11383 delete_keyboard_wait_descriptor (dpyinfo->connection);
11384 emacs_close (dpyinfo->connection);
11387 /* Mark as dead. */
11388 dpyinfo->display = NULL;
11389 dpyinfo->connection = -1;
11390 x_delete_display (dpyinfo);
11391 unblock_input ();
11394 /* Create a struct terminal, initialize it with the X11 specific
11395 functions and make DISPLAY->TERMINAL point to it. */
11397 static struct terminal *
11398 x_create_terminal (struct x_display_info *dpyinfo)
11400 struct terminal *terminal;
11402 terminal = create_terminal (output_x_window, &x_redisplay_interface);
11404 terminal->display_info.x = dpyinfo;
11405 dpyinfo->terminal = terminal;
11407 /* kboard is initialized in x_term_init. */
11409 terminal->clear_frame_hook = x_clear_frame;
11410 terminal->ins_del_lines_hook = x_ins_del_lines;
11411 terminal->delete_glyphs_hook = x_delete_glyphs;
11412 terminal->ring_bell_hook = XTring_bell;
11413 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
11414 terminal->update_begin_hook = x_update_begin;
11415 terminal->update_end_hook = x_update_end;
11416 terminal->read_socket_hook = XTread_socket;
11417 terminal->frame_up_to_date_hook = XTframe_up_to_date;
11418 terminal->mouse_position_hook = XTmouse_position;
11419 terminal->frame_rehighlight_hook = XTframe_rehighlight;
11420 terminal->frame_raise_lower_hook = XTframe_raise_lower;
11421 terminal->fullscreen_hook = XTfullscreen_hook;
11422 terminal->menu_show_hook = x_menu_show;
11423 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
11424 terminal->popup_dialog_hook = xw_popup_dialog;
11425 #endif
11426 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
11427 terminal->set_horizontal_scroll_bar_hook = XTset_horizontal_scroll_bar;
11428 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
11429 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
11430 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
11431 terminal->delete_frame_hook = x_destroy_window;
11432 terminal->delete_terminal_hook = x_delete_terminal;
11433 /* Other hooks are NULL by default. */
11435 return terminal;
11438 static void
11439 x_initialize (void)
11441 baud_rate = 19200;
11443 x_noop_count = 0;
11444 any_help_event_p = 0;
11445 ignore_next_mouse_click_timeout = 0;
11447 #ifdef USE_GTK
11448 current_count = -1;
11449 #endif
11451 /* Try to use interrupt input; if we can't, then start polling. */
11452 Fset_input_interrupt_mode (Qt);
11454 #ifdef USE_X_TOOLKIT
11455 XtToolkitInitialize ();
11457 Xt_app_con = XtCreateApplicationContext ();
11459 /* Register a converter from strings to pixels, which uses
11460 Emacs' color allocation infrastructure. */
11461 XtAppSetTypeConverter (Xt_app_con,
11462 XtRString, XtRPixel, cvt_string_to_pixel,
11463 cvt_string_to_pixel_args,
11464 XtNumber (cvt_string_to_pixel_args),
11465 XtCacheByDisplay, cvt_pixel_dtor);
11467 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
11468 #endif
11470 #ifdef USE_TOOLKIT_SCROLL_BARS
11471 #ifndef USE_GTK
11472 xaw3d_arrow_scroll = False;
11473 xaw3d_pick_top = True;
11474 #endif
11475 #endif
11477 /* Note that there is no real way portable across R3/R4 to get the
11478 original error handler. */
11479 XSetErrorHandler (x_error_handler);
11480 XSetIOErrorHandler (x_io_error_quitter);
11484 void
11485 syms_of_xterm (void)
11487 x_error_message = NULL;
11489 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
11490 DEFSYM (Qlatin_1, "latin-1");
11492 #ifdef USE_GTK
11493 xg_default_icon_file = build_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
11494 staticpro (&xg_default_icon_file);
11496 DEFSYM (Qx_gtk_map_stock, "x-gtk-map-stock");
11497 #endif
11499 DEFVAR_BOOL ("x-use-underline-position-properties",
11500 x_use_underline_position_properties,
11501 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
11502 A value of nil means ignore them. If you encounter fonts with bogus
11503 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
11504 to 4.1, set this to nil. You can also use `underline-minimum-offset'
11505 to override the font's UNDERLINE_POSITION for small font display
11506 sizes. */);
11507 x_use_underline_position_properties = 1;
11509 DEFVAR_BOOL ("x-underline-at-descent-line",
11510 x_underline_at_descent_line,
11511 doc: /* Non-nil means to draw the underline at the same place as the descent line.
11512 A value of nil means to draw the underline according to the value of the
11513 variable `x-use-underline-position-properties', which is usually at the
11514 baseline level. The default value is nil. */);
11515 x_underline_at_descent_line = 0;
11517 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
11518 x_mouse_click_focus_ignore_position,
11519 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
11520 This variable is only used when the window manager requires that you
11521 click on a frame to select it (give it focus). In that case, a value
11522 of nil, means that the selected window and cursor position changes to
11523 reflect the mouse click position, while a non-nil value means that the
11524 selected window or cursor position is preserved. */);
11525 x_mouse_click_focus_ignore_position = 0;
11527 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
11528 doc: /* Which toolkit scroll bars Emacs uses, if any.
11529 A value of nil means Emacs doesn't use toolkit scroll bars.
11530 With the X Window system, the value is a symbol describing the
11531 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
11532 With MS Windows or Nextstep, the value is t. */);
11533 #ifdef USE_TOOLKIT_SCROLL_BARS
11534 #ifdef USE_MOTIF
11535 Vx_toolkit_scroll_bars = intern_c_string ("motif");
11536 #elif defined HAVE_XAW3D
11537 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
11538 #elif USE_GTK
11539 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
11540 #else
11541 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
11542 #endif
11543 #else
11544 Vx_toolkit_scroll_bars = Qnil;
11545 #endif
11547 DEFSYM (Qmodifier_value, "modifier-value");
11548 DEFSYM (Qalt, "alt");
11549 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
11550 DEFSYM (Qhyper, "hyper");
11551 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
11552 DEFSYM (Qmeta, "meta");
11553 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
11554 DEFSYM (Qsuper, "super");
11555 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
11557 DEFVAR_LISP ("x-alt-keysym", Vx_alt_keysym,
11558 doc: /* Which keys Emacs uses for the alt modifier.
11559 This should be one of the symbols `alt', `hyper', `meta', `super'.
11560 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
11561 is nil, which is the same as `alt'. */);
11562 Vx_alt_keysym = Qnil;
11564 DEFVAR_LISP ("x-hyper-keysym", Vx_hyper_keysym,
11565 doc: /* Which keys Emacs uses for the hyper modifier.
11566 This should be one of the symbols `alt', `hyper', `meta', `super'.
11567 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
11568 default is nil, which is the same as `hyper'. */);
11569 Vx_hyper_keysym = Qnil;
11571 DEFVAR_LISP ("x-meta-keysym", Vx_meta_keysym,
11572 doc: /* Which keys Emacs uses for the meta modifier.
11573 This should be one of the symbols `alt', `hyper', `meta', `super'.
11574 For example, `meta' means use the Meta_L and Meta_R keysyms. The
11575 default is nil, which is the same as `meta'. */);
11576 Vx_meta_keysym = Qnil;
11578 DEFVAR_LISP ("x-super-keysym", Vx_super_keysym,
11579 doc: /* Which keys Emacs uses for the super modifier.
11580 This should be one of the symbols `alt', `hyper', `meta', `super'.
11581 For example, `super' means use the Super_L and Super_R keysyms. The
11582 default is nil, which is the same as `super'. */);
11583 Vx_super_keysym = Qnil;
11585 DEFVAR_LISP ("x-keysym-table", Vx_keysym_table,
11586 doc: /* Hash table of character codes indexed by X keysym codes. */);
11587 Vx_keysym_table = make_hash_table (hashtest_eql, make_number (900),
11588 make_float (DEFAULT_REHASH_SIZE),
11589 make_float (DEFAULT_REHASH_THRESHOLD),
11590 Qnil);