Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / src / xterm.c
blobbffd85c8de4227024251fc8a3bd2752a1bb8fe66
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 /* Load sys/types.h if not already loaded.
36 In some systems loading it twice is suicidal. */
37 #ifndef makedev
38 #include <sys/types.h>
39 #endif /* makedev */
41 #include <sys/ioctl.h>
43 #include "systime.h"
45 #include <fcntl.h>
46 #include <errno.h>
47 #include <sys/stat.h>
48 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
49 /* #include <sys/param.h> */
51 #include "charset.h"
52 #include "character.h"
53 #include "coding.h"
54 #include "frame.h"
55 #include "dispextern.h"
56 #include "fontset.h"
57 #include "termhooks.h"
58 #include "termopts.h"
59 #include "termchar.h"
60 #include "emacs-icon.h"
61 #include "disptab.h"
62 #include "buffer.h"
63 #include "window.h"
64 #include "keyboard.h"
65 #include "intervals.h"
66 #include "process.h"
67 #include "atimer.h"
68 #include "keymap.h"
69 #include "font.h"
70 #include "xsettings.h"
71 #include "xgselect.h"
72 #include "sysselect.h"
74 #ifdef USE_X_TOOLKIT
75 #include <X11/Shell.h>
76 #endif
78 #include <unistd.h>
80 #ifdef USE_GTK
81 #include "gtkutil.h"
82 #ifdef HAVE_GTK3
83 #include <X11/Xproto.h>
84 #endif
85 #endif
87 #if defined (USE_LUCID) || defined (USE_MOTIF)
88 #include "../lwlib/xlwmenu.h"
89 #endif
91 #ifdef USE_X_TOOLKIT
92 #if !defined (NO_EDITRES)
93 #define HACK_EDITRES
94 extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
95 #endif /* not NO_EDITRES */
97 /* Include toolkit specific headers for the scroll bar widget. */
99 #ifdef USE_TOOLKIT_SCROLL_BARS
100 #if defined USE_MOTIF
101 #include <Xm/Xm.h> /* For LESSTIF_VERSION */
102 #include <Xm/ScrollBar.h>
103 #else /* !USE_MOTIF i.e. use Xaw */
105 #ifdef HAVE_XAW3D
106 #include <X11/Xaw3d/Simple.h>
107 #include <X11/Xaw3d/Scrollbar.h>
108 #include <X11/Xaw3d/ThreeD.h>
109 #else /* !HAVE_XAW3D */
110 #include <X11/Xaw/Simple.h>
111 #include <X11/Xaw/Scrollbar.h>
112 #endif /* !HAVE_XAW3D */
113 #ifndef XtNpickTop
114 #define XtNpickTop "pickTop"
115 #endif /* !XtNpickTop */
116 #endif /* !USE_MOTIF */
117 #endif /* USE_TOOLKIT_SCROLL_BARS */
119 #endif /* USE_X_TOOLKIT */
121 #ifdef USE_X_TOOLKIT
122 #include "widget.h"
123 #ifndef XtNinitialState
124 #define XtNinitialState "initialState"
125 #endif
126 #endif
128 #include "bitmaps/gray.xbm"
130 #ifdef HAVE_XKB
131 #include <X11/XKBlib.h>
132 #endif
134 /* Default to using XIM if available. */
135 #ifdef USE_XIM
136 bool use_xim = true;
137 #else
138 bool use_xim = false; /* configure --without-xim */
139 #endif
141 /* Non-zero means that a HELP_EVENT has been generated since Emacs
142 start. */
144 static bool any_help_event_p;
146 /* This is a chain of structures for all the X displays currently in
147 use. */
149 struct x_display_info *x_display_list;
151 #ifdef USE_X_TOOLKIT
153 /* The application context for Xt use. */
154 XtAppContext Xt_app_con;
155 static String Xt_default_resources[] = {0};
157 /* Non-zero means user is interacting with a toolkit scroll bar. */
158 static bool toolkit_scroll_bar_interaction;
160 #endif /* USE_X_TOOLKIT */
162 /* Non-zero timeout value means ignore next mouse click if it arrives
163 before that timeout elapses (i.e. as part of the same sequence of
164 events resulting from clicking on a frame to select it). */
166 static Time ignore_next_mouse_click_timeout;
168 /* Used locally within XTread_socket. */
170 static int x_noop_count;
172 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
174 static Lisp_Object Qvendor_specific_keysyms;
175 static Lisp_Object Qlatin_1;
177 #ifdef USE_GTK
178 /* The name of the Emacs icon file. */
179 static Lisp_Object xg_default_icon_file;
181 /* Used in gtkutil.c. */
182 Lisp_Object Qx_gtk_map_stock;
183 #endif
185 /* Some functions take this as char *, not const char *. */
186 static char emacs_class[] = EMACS_CLASS;
188 enum xembed_info
190 XEMBED_MAPPED = 1 << 0
193 enum xembed_message
195 XEMBED_EMBEDDED_NOTIFY = 0,
196 XEMBED_WINDOW_ACTIVATE = 1,
197 XEMBED_WINDOW_DEACTIVATE = 2,
198 XEMBED_REQUEST_FOCUS = 3,
199 XEMBED_FOCUS_IN = 4,
200 XEMBED_FOCUS_OUT = 5,
201 XEMBED_FOCUS_NEXT = 6,
202 XEMBED_FOCUS_PREV = 7,
204 XEMBED_MODALITY_ON = 10,
205 XEMBED_MODALITY_OFF = 11,
206 XEMBED_REGISTER_ACCELERATOR = 12,
207 XEMBED_UNREGISTER_ACCELERATOR = 13,
208 XEMBED_ACTIVATE_ACCELERATOR = 14
211 static bool x_alloc_nearest_color_1 (Display *, Colormap, XColor *);
212 static void x_set_window_size_1 (struct frame *, int, int, int, bool);
213 static void x_raise_frame (struct frame *);
214 static void x_lower_frame (struct frame *);
215 static const XColor *x_color_cells (Display *, int *);
216 static int x_io_error_quitter (Display *);
217 static struct terminal *x_create_terminal (struct x_display_info *);
218 void x_delete_terminal (struct terminal *);
219 static void x_update_end (struct frame *);
220 static void XTframe_up_to_date (struct frame *);
221 static void x_clear_frame (struct frame *);
222 static _Noreturn void x_ins_del_lines (struct frame *, int, int);
223 static void frame_highlight (struct frame *);
224 static void frame_unhighlight (struct frame *);
225 static void x_new_focus_frame (struct x_display_info *, struct frame *);
226 static void x_focus_changed (int, int, struct x_display_info *,
227 struct frame *, struct input_event *);
228 static void XTframe_rehighlight (struct frame *);
229 static void x_frame_rehighlight (struct x_display_info *);
230 static void x_draw_hollow_cursor (struct window *, struct glyph_row *);
231 static void x_draw_bar_cursor (struct window *, struct glyph_row *, int,
232 enum text_cursor_kinds);
234 static void x_clip_to_row (struct window *, struct glyph_row *,
235 enum glyph_row_area, GC);
236 static void x_flush (struct frame *f);
237 static void x_update_begin (struct frame *);
238 static void x_update_window_begin (struct window *);
239 static struct scroll_bar *x_window_to_scroll_bar (Display *, Window);
240 static void x_scroll_bar_report_motion (struct frame **, Lisp_Object *,
241 enum scroll_bar_part *,
242 Lisp_Object *, Lisp_Object *,
243 Time *);
244 static int x_handle_net_wm_state (struct frame *, const XPropertyEvent *);
245 static void x_check_fullscreen (struct frame *);
246 static void x_check_expected_move (struct frame *, int, int);
247 static void x_sync_with_move (struct frame *, int, int, int);
248 static int handle_one_xevent (struct x_display_info *,
249 const XEvent *, int *,
250 struct input_event *);
251 #if ! (defined USE_X_TOOLKIT || defined USE_MOTIF)
252 static int x_dispatch_event (XEvent *, Display *);
253 #endif
254 /* Don't declare this _Noreturn because we want no
255 interference with debugging failing X calls. */
256 static void x_connection_closed (Display *, const char *);
257 static void x_wm_set_window_state (struct frame *, int);
258 static void x_wm_set_icon_pixmap (struct frame *, ptrdiff_t);
259 static void x_initialize (void);
262 /* Flush display of frame F. */
264 static void
265 x_flush (struct frame *f)
267 eassert (f && FRAME_X_P (f));
268 /* Don't call XFlush when it is not safe to redisplay; the X
269 connection may be broken. */
270 if (!NILP (Vinhibit_redisplay))
271 return;
273 block_input ();
274 XFlush (FRAME_X_DISPLAY (f));
275 unblock_input ();
279 /* Remove calls to XFlush by defining XFlush to an empty replacement.
280 Calls to XFlush should be unnecessary because the X output buffer
281 is flushed automatically as needed by calls to XPending,
282 XNextEvent, or XWindowEvent according to the XFlush man page.
283 XTread_socket calls XPending. Removing XFlush improves
284 performance. */
286 #define XFlush(DISPLAY) (void) 0
289 /***********************************************************************
290 Debugging
291 ***********************************************************************/
293 #if 0
295 /* This is a function useful for recording debugging information about
296 the sequence of occurrences in this file. */
298 struct record
300 char *locus;
301 int type;
304 struct record event_record[100];
306 int event_record_index;
308 void
309 record_event (char *locus, int type)
311 if (event_record_index == sizeof (event_record) / sizeof (struct record))
312 event_record_index = 0;
314 event_record[event_record_index].locus = locus;
315 event_record[event_record_index].type = type;
316 event_record_index++;
319 #endif /* 0 */
323 /* Return the struct x_display_info corresponding to DPY. */
325 struct x_display_info *
326 x_display_info_for_display (Display *dpy)
328 struct x_display_info *dpyinfo;
330 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
331 if (dpyinfo->display == dpy)
332 return dpyinfo;
334 return 0;
337 static Window
338 x_find_topmost_parent (struct frame *f)
340 struct x_output *x = f->output_data.x;
341 Window win = None, wi = x->parent_desc;
342 Display *dpy = FRAME_X_DISPLAY (f);
344 while (wi != FRAME_DISPLAY_INFO (f)->root_window)
346 Window root;
347 Window *children;
348 unsigned int nchildren;
350 win = wi;
351 XQueryTree (dpy, win, &root, &wi, &children, &nchildren);
352 XFree (children);
355 return win;
358 #define OPAQUE 0xffffffff
360 void
361 x_set_frame_alpha (struct frame *f)
363 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
364 Display *dpy = FRAME_X_DISPLAY (f);
365 Window win = FRAME_OUTER_WINDOW (f);
366 double alpha = 1.0;
367 double alpha_min = 1.0;
368 unsigned long opac;
369 Window parent;
371 if (dpyinfo->x_highlight_frame == f)
372 alpha = f->alpha[0];
373 else
374 alpha = f->alpha[1];
376 if (FLOATP (Vframe_alpha_lower_limit))
377 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
378 else if (INTEGERP (Vframe_alpha_lower_limit))
379 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
381 if (alpha < 0.0)
382 return;
383 else if (alpha > 1.0)
384 alpha = 1.0;
385 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
386 alpha = alpha_min;
388 opac = alpha * OPAQUE;
390 x_catch_errors (dpy);
392 /* If there is a parent from the window manager, put the property there
393 also, to work around broken window managers that fail to do that.
394 Do this unconditionally as this function is called on reparent when
395 alpha has not changed on the frame. */
397 parent = x_find_topmost_parent (f);
398 if (parent != None)
399 XChangeProperty (dpy, parent, dpyinfo->Xatom_net_wm_window_opacity,
400 XA_CARDINAL, 32, PropModeReplace,
401 (unsigned char *) &opac, 1L);
403 /* return unless necessary */
405 unsigned char *data;
406 Atom actual;
407 int rc, format;
408 unsigned long n, left;
410 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
411 0L, 1L, False, XA_CARDINAL,
412 &actual, &format, &n, &left,
413 &data);
415 if (rc == Success && actual != None)
417 unsigned long value = *(unsigned long *)data;
418 XFree (data);
419 if (value == opac)
421 x_uncatch_errors ();
422 return;
427 XChangeProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
428 XA_CARDINAL, 32, PropModeReplace,
429 (unsigned char *) &opac, 1L);
430 x_uncatch_errors ();
434 x_display_pixel_height (struct x_display_info *dpyinfo)
436 return HeightOfScreen (dpyinfo->screen);
440 x_display_pixel_width (struct x_display_info *dpyinfo)
442 return WidthOfScreen (dpyinfo->screen);
446 /***********************************************************************
447 Starting and ending an update
448 ***********************************************************************/
450 /* Start an update of frame F. This function is installed as a hook
451 for update_begin, i.e. it is called when update_begin is called.
452 This function is called prior to calls to x_update_window_begin for
453 each window being updated. Currently, there is nothing to do here
454 because all interesting stuff is done on a window basis. */
456 static void
457 x_update_begin (struct frame *f)
459 /* Nothing to do. */
463 /* Start update of window W. */
465 static void
466 x_update_window_begin (struct window *w)
468 struct frame *f = XFRAME (WINDOW_FRAME (w));
469 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
471 w->output_cursor = w->cursor;
473 block_input ();
475 if (f == hlinfo->mouse_face_mouse_frame)
477 /* Don't do highlighting for mouse motion during the update. */
478 hlinfo->mouse_face_defer = 1;
480 /* If F needs to be redrawn, simply forget about any prior mouse
481 highlighting. */
482 if (FRAME_GARBAGED_P (f))
483 hlinfo->mouse_face_window = Qnil;
486 unblock_input ();
490 /* Draw a vertical window border from (x,y0) to (x,y1) */
492 static void
493 x_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
495 struct frame *f = XFRAME (WINDOW_FRAME (w));
496 struct face *face;
498 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
499 if (face)
500 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
501 face->foreground);
503 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
504 f->output_data.x->normal_gc, x, y0, x, y1);
507 /* Draw a window divider from (x0,y0) to (x1,y1) */
509 static void
510 x_draw_window_divider (struct window *w, int x0, int x1, int y0, int y1)
512 struct frame *f = XFRAME (WINDOW_FRAME (w));
513 struct face *face;
515 face = FACE_FROM_ID (f, WINDOW_DIVIDER_FACE_ID);
516 if (face)
517 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
518 face->foreground);
520 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
521 f->output_data.x->normal_gc, x0, y0, x1 - x0, y1 - y0);
524 /* End update of window W.
526 Draw vertical borders between horizontally adjacent windows, and
527 display W's cursor if CURSOR_ON_P is non-zero.
529 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
530 glyphs in mouse-face were overwritten. In that case we have to
531 make sure that the mouse-highlight is properly redrawn.
533 W may be a menu bar pseudo-window in case we don't have X toolkit
534 support. Such windows don't have a cursor, so don't display it
535 here. */
537 static void
538 x_update_window_end (struct window *w, bool cursor_on_p,
539 bool mouse_face_overwritten_p)
541 if (!w->pseudo_window_p)
543 block_input ();
545 if (cursor_on_p)
546 display_and_set_cursor (w, 1,
547 w->output_cursor.hpos, w->output_cursor.vpos,
548 w->output_cursor.x, w->output_cursor.y);
550 if (draw_window_fringes (w, 1))
552 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
553 x_draw_right_divider (w);
554 else
555 x_draw_vertical_border (w);
558 unblock_input ();
561 /* If a row with mouse-face was overwritten, arrange for
562 XTframe_up_to_date to redisplay the mouse highlight. */
563 if (mouse_face_overwritten_p)
564 reset_mouse_highlight (MOUSE_HL_INFO (XFRAME (w->frame)));
568 /* End update of frame F. This function is installed as a hook in
569 update_end. */
571 static void
572 x_update_end (struct frame *f)
574 /* Mouse highlight may be displayed again. */
575 MOUSE_HL_INFO (f)->mouse_face_defer = 0;
577 #ifndef XFlush
578 block_input ();
579 XFlush (FRAME_X_DISPLAY (f));
580 unblock_input ();
581 #endif
585 /* This function is called from various places in xdisp.c
586 whenever a complete update has been performed. */
588 static void
589 XTframe_up_to_date (struct frame *f)
591 if (FRAME_X_P (f))
592 FRAME_MOUSE_UPDATE (f);
596 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
597 arrow bitmaps, or clear the fringes if no bitmaps are required
598 before DESIRED_ROW is made current. This function is called from
599 update_window_line only if it is known that there are differences
600 between bitmaps to be drawn between current row and DESIRED_ROW. */
602 static void
603 x_after_update_window_line (struct window *w, struct glyph_row *desired_row)
605 struct frame *f;
606 int width, height;
608 eassert (w);
610 if (!desired_row->mode_line_p && !w->pseudo_window_p)
611 desired_row->redraw_fringe_bitmaps_p = 1;
613 /* When a window has disappeared, make sure that no rest of
614 full-width rows stays visible in the internal border. Could
615 check here if updated window is the leftmost/rightmost window,
616 but I guess it's not worth doing since vertically split windows
617 are almost never used, internal border is rarely set, and the
618 overhead is very small. */
619 if (windows_or_buffers_changed
620 && desired_row->full_width_p
621 && (f = XFRAME (w->frame),
622 width = FRAME_INTERNAL_BORDER_WIDTH (f),
623 width != 0)
624 && (height = desired_row->visible_height,
625 height > 0))
627 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
629 block_input ();
630 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
631 0, y, width, height);
632 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
633 FRAME_PIXEL_WIDTH (f) - width,
634 y, width, height);
635 unblock_input ();
639 static void
640 x_draw_fringe_bitmap (struct window *w, struct glyph_row *row, struct draw_fringe_bitmap_params *p)
642 struct frame *f = XFRAME (WINDOW_FRAME (w));
643 Display *display = FRAME_X_DISPLAY (f);
644 Window window = FRAME_X_WINDOW (f);
645 GC gc = f->output_data.x->normal_gc;
646 struct face *face = p->face;
648 /* Must clip because of partially visible lines. */
649 x_clip_to_row (w, row, ANY_AREA, gc);
651 if (p->bx >= 0 && !p->overlay_p)
653 /* In case the same realized face is used for fringes and
654 for something displayed in the text (e.g. face `region' on
655 mono-displays, the fill style may have been changed to
656 FillSolid in x_draw_glyph_string_background. */
657 if (face->stipple)
658 XSetFillStyle (display, face->gc, FillOpaqueStippled);
659 else
660 XSetForeground (display, face->gc, face->background);
662 XFillRectangle (display, window, face->gc,
663 p->bx, p->by, p->nx, p->ny);
665 if (!face->stipple)
666 XSetForeground (display, face->gc, face->foreground);
669 if (p->which)
671 char *bits;
672 Pixmap pixmap, clipmask = (Pixmap) 0;
673 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
674 XGCValues gcv;
676 if (p->wd > 8)
677 bits = (char *) (p->bits + p->dh);
678 else
679 bits = (char *) p->bits + p->dh;
681 /* Draw the bitmap. I believe these small pixmaps can be cached
682 by the server. */
683 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
684 (p->cursor_p
685 ? (p->overlay_p ? face->background
686 : f->output_data.x->cursor_pixel)
687 : face->foreground),
688 face->background, depth);
690 if (p->overlay_p)
692 clipmask = XCreatePixmapFromBitmapData (display,
693 FRAME_DISPLAY_INFO (f)->root_window,
694 bits, p->wd, p->h,
695 1, 0, 1);
696 gcv.clip_mask = clipmask;
697 gcv.clip_x_origin = p->x;
698 gcv.clip_y_origin = p->y;
699 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
702 XCopyArea (display, pixmap, window, gc, 0, 0,
703 p->wd, p->h, p->x, p->y);
704 XFreePixmap (display, pixmap);
706 if (p->overlay_p)
708 gcv.clip_mask = (Pixmap) 0;
709 XChangeGC (display, gc, GCClipMask, &gcv);
710 XFreePixmap (display, clipmask);
714 XSetClipMask (display, gc, None);
717 /***********************************************************************
718 Glyph display
719 ***********************************************************************/
723 static void x_set_glyph_string_clipping (struct glyph_string *);
724 static void x_set_glyph_string_gc (struct glyph_string *);
725 static void x_draw_glyph_string_foreground (struct glyph_string *);
726 static void x_draw_composite_glyph_string_foreground (struct glyph_string *);
727 static void x_draw_glyph_string_box (struct glyph_string *);
728 static void x_draw_glyph_string (struct glyph_string *);
729 static _Noreturn void x_delete_glyphs (struct frame *, int);
730 static void x_compute_glyph_string_overhangs (struct glyph_string *);
731 static void x_set_cursor_gc (struct glyph_string *);
732 static void x_set_mode_line_face_gc (struct glyph_string *);
733 static void x_set_mouse_face_gc (struct glyph_string *);
734 static bool x_alloc_lighter_color (struct frame *, Display *, Colormap,
735 unsigned long *, double, int);
736 static void x_setup_relief_color (struct frame *, struct relief *,
737 double, int, unsigned long);
738 static void x_setup_relief_colors (struct glyph_string *);
739 static void x_draw_image_glyph_string (struct glyph_string *);
740 static void x_draw_image_relief (struct glyph_string *);
741 static void x_draw_image_foreground (struct glyph_string *);
742 static void x_draw_image_foreground_1 (struct glyph_string *, Pixmap);
743 static void x_clear_glyph_string_rect (struct glyph_string *, int,
744 int, int, int);
745 static void x_draw_relief_rect (struct frame *, int, int, int, int,
746 int, int, int, int, int, int,
747 XRectangle *);
748 static void x_draw_box_rect (struct glyph_string *, int, int, int, int,
749 int, int, int, XRectangle *);
750 static void x_scroll_bar_clear (struct frame *);
752 #ifdef GLYPH_DEBUG
753 static void x_check_font (struct frame *, struct font *);
754 #endif
757 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
758 face. */
760 static void
761 x_set_cursor_gc (struct glyph_string *s)
763 if (s->font == FRAME_FONT (s->f)
764 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
765 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
766 && !s->cmp)
767 s->gc = s->f->output_data.x->cursor_gc;
768 else
770 /* Cursor on non-default face: must merge. */
771 XGCValues xgcv;
772 unsigned long mask;
774 xgcv.background = s->f->output_data.x->cursor_pixel;
775 xgcv.foreground = s->face->background;
777 /* If the glyph would be invisible, try a different foreground. */
778 if (xgcv.foreground == xgcv.background)
779 xgcv.foreground = s->face->foreground;
780 if (xgcv.foreground == xgcv.background)
781 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
782 if (xgcv.foreground == xgcv.background)
783 xgcv.foreground = s->face->foreground;
785 /* Make sure the cursor is distinct from text in this face. */
786 if (xgcv.background == s->face->background
787 && xgcv.foreground == s->face->foreground)
789 xgcv.background = s->face->foreground;
790 xgcv.foreground = s->face->background;
793 IF_DEBUG (x_check_font (s->f, s->font));
794 xgcv.graphics_exposures = False;
795 mask = GCForeground | GCBackground | GCGraphicsExposures;
797 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
798 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
799 mask, &xgcv);
800 else
801 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
802 = XCreateGC (s->display, s->window, mask, &xgcv);
804 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
809 /* Set up S->gc of glyph string S for drawing text in mouse face. */
811 static void
812 x_set_mouse_face_gc (struct glyph_string *s)
814 int face_id;
815 struct face *face;
817 /* What face has to be used last for the mouse face? */
818 face_id = MOUSE_HL_INFO (s->f)->mouse_face_face_id;
819 face = FACE_FROM_ID (s->f, face_id);
820 if (face == NULL)
821 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
823 if (s->first_glyph->type == CHAR_GLYPH)
824 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
825 else
826 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
827 s->face = FACE_FROM_ID (s->f, face_id);
828 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
830 if (s->font == s->face->font)
831 s->gc = s->face->gc;
832 else
834 /* Otherwise construct scratch_cursor_gc with values from FACE
835 except for FONT. */
836 XGCValues xgcv;
837 unsigned long mask;
839 xgcv.background = s->face->background;
840 xgcv.foreground = s->face->foreground;
841 xgcv.graphics_exposures = False;
842 mask = GCForeground | GCBackground | GCGraphicsExposures;
844 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
845 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
846 mask, &xgcv);
847 else
848 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
849 = XCreateGC (s->display, s->window, mask, &xgcv);
851 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
854 eassert (s->gc != 0);
858 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
859 Faces to use in the mode line have already been computed when the
860 matrix was built, so there isn't much to do, here. */
862 static void
863 x_set_mode_line_face_gc (struct glyph_string *s)
865 s->gc = s->face->gc;
869 /* Set S->gc of glyph string S for drawing that glyph string. Set
870 S->stippled_p to a non-zero value if the face of S has a stipple
871 pattern. */
873 static void
874 x_set_glyph_string_gc (struct glyph_string *s)
876 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
878 if (s->hl == DRAW_NORMAL_TEXT)
880 s->gc = s->face->gc;
881 s->stippled_p = s->face->stipple != 0;
883 else if (s->hl == DRAW_INVERSE_VIDEO)
885 x_set_mode_line_face_gc (s);
886 s->stippled_p = s->face->stipple != 0;
888 else if (s->hl == DRAW_CURSOR)
890 x_set_cursor_gc (s);
891 s->stippled_p = 0;
893 else if (s->hl == DRAW_MOUSE_FACE)
895 x_set_mouse_face_gc (s);
896 s->stippled_p = s->face->stipple != 0;
898 else if (s->hl == DRAW_IMAGE_RAISED
899 || s->hl == DRAW_IMAGE_SUNKEN)
901 s->gc = s->face->gc;
902 s->stippled_p = s->face->stipple != 0;
904 else
906 s->gc = s->face->gc;
907 s->stippled_p = s->face->stipple != 0;
910 /* GC must have been set. */
911 eassert (s->gc != 0);
915 /* Set clipping for output of glyph string S. S may be part of a mode
916 line or menu if we don't have X toolkit support. */
918 static void
919 x_set_glyph_string_clipping (struct glyph_string *s)
921 XRectangle *r = s->clip;
922 int n = get_glyph_string_clip_rects (s, r, 2);
924 if (n > 0)
925 XSetClipRectangles (s->display, s->gc, 0, 0, r, n, Unsorted);
926 s->num_clips = n;
930 /* Set SRC's clipping for output of glyph string DST. This is called
931 when we are drawing DST's left_overhang or right_overhang only in
932 the area of SRC. */
934 static void
935 x_set_glyph_string_clipping_exactly (struct glyph_string *src, struct glyph_string *dst)
937 XRectangle r;
939 r.x = src->x;
940 r.width = src->width;
941 r.y = src->y;
942 r.height = src->height;
943 dst->clip[0] = r;
944 dst->num_clips = 1;
945 XSetClipRectangles (dst->display, dst->gc, 0, 0, &r, 1, Unsorted);
949 /* RIF:
950 Compute left and right overhang of glyph string S. */
952 static void
953 x_compute_glyph_string_overhangs (struct glyph_string *s)
955 if (s->cmp == NULL
956 && (s->first_glyph->type == CHAR_GLYPH
957 || s->first_glyph->type == COMPOSITE_GLYPH))
959 struct font_metrics metrics;
961 if (s->first_glyph->type == CHAR_GLYPH)
963 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
964 struct font *font = s->font;
965 int i;
967 for (i = 0; i < s->nchars; i++)
968 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
969 font->driver->text_extents (font, code, s->nchars, &metrics);
971 else
973 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
975 composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
977 s->right_overhang = (metrics.rbearing > metrics.width
978 ? metrics.rbearing - metrics.width : 0);
979 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
981 else if (s->cmp)
983 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
984 s->left_overhang = - s->cmp->lbearing;
989 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
991 static void
992 x_clear_glyph_string_rect (struct glyph_string *s, int x, int y, int w, int h)
994 XGCValues xgcv;
995 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
996 XSetForeground (s->display, s->gc, xgcv.background);
997 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
998 XSetForeground (s->display, s->gc, xgcv.foreground);
1002 /* Draw the background of glyph_string S. If S->background_filled_p
1003 is non-zero don't draw it. FORCE_P non-zero means draw the
1004 background even if it wouldn't be drawn normally. This is used
1005 when a string preceding S draws into the background of S, or S
1006 contains the first component of a composition. */
1008 static void
1009 x_draw_glyph_string_background (struct glyph_string *s, bool force_p)
1011 /* Nothing to do if background has already been drawn or if it
1012 shouldn't be drawn in the first place. */
1013 if (!s->background_filled_p)
1015 int box_line_width = max (s->face->box_line_width, 0);
1017 if (s->stippled_p)
1019 /* Fill background with a stipple pattern. */
1020 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1021 XFillRectangle (s->display, s->window, s->gc, s->x,
1022 s->y + box_line_width,
1023 s->background_width,
1024 s->height - 2 * box_line_width);
1025 XSetFillStyle (s->display, s->gc, FillSolid);
1026 s->background_filled_p = 1;
1028 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1029 || s->font_not_found_p
1030 || s->extends_to_end_of_line_p
1031 || force_p)
1033 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1034 s->background_width,
1035 s->height - 2 * box_line_width);
1036 s->background_filled_p = 1;
1042 /* Draw the foreground of glyph string S. */
1044 static void
1045 x_draw_glyph_string_foreground (struct glyph_string *s)
1047 int i, x;
1049 /* If first glyph of S has a left box line, start drawing the text
1050 of S to the right of that box line. */
1051 if (s->face->box != FACE_NO_BOX
1052 && s->first_glyph->left_box_line_p)
1053 x = s->x + eabs (s->face->box_line_width);
1054 else
1055 x = s->x;
1057 /* Draw characters of S as rectangles if S's font could not be
1058 loaded. */
1059 if (s->font_not_found_p)
1061 for (i = 0; i < s->nchars; ++i)
1063 struct glyph *g = s->first_glyph + i;
1064 XDrawRectangle (s->display, s->window,
1065 s->gc, x, s->y, g->pixel_width - 1,
1066 s->height - 1);
1067 x += g->pixel_width;
1070 else
1072 struct font *font = s->font;
1073 int boff = font->baseline_offset;
1074 int y;
1076 if (font->vertical_centering)
1077 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1079 y = s->ybase - boff;
1080 if (s->for_overlaps
1081 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1082 font->driver->draw (s, 0, s->nchars, x, y, 0);
1083 else
1084 font->driver->draw (s, 0, s->nchars, x, y, 1);
1085 if (s->face->overstrike)
1086 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1090 /* Draw the foreground of composite glyph string S. */
1092 static void
1093 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1095 int i, j, x;
1096 struct font *font = s->font;
1098 /* If first glyph of S has a left box line, start drawing the text
1099 of S to the right of that box line. */
1100 if (s->face && s->face->box != FACE_NO_BOX
1101 && s->first_glyph->left_box_line_p)
1102 x = s->x + eabs (s->face->box_line_width);
1103 else
1104 x = s->x;
1106 /* S is a glyph string for a composition. S->cmp_from is the index
1107 of the first character drawn for glyphs of this composition.
1108 S->cmp_from == 0 means we are drawing the very first character of
1109 this composition. */
1111 /* Draw a rectangle for the composition if the font for the very
1112 first character of the composition could not be loaded. */
1113 if (s->font_not_found_p)
1115 if (s->cmp_from == 0)
1116 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1117 s->width - 1, s->height - 1);
1119 else if (! s->first_glyph->u.cmp.automatic)
1121 int y = s->ybase;
1123 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1124 /* TAB in a composition means display glyphs with padding
1125 space on the left or right. */
1126 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1128 int xx = x + s->cmp->offsets[j * 2];
1129 int yy = y - s->cmp->offsets[j * 2 + 1];
1131 font->driver->draw (s, j, j + 1, xx, yy, 0);
1132 if (s->face->overstrike)
1133 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
1136 else
1138 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1139 Lisp_Object glyph;
1140 int y = s->ybase;
1141 int width = 0;
1143 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1145 glyph = LGSTRING_GLYPH (gstring, i);
1146 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1147 width += LGLYPH_WIDTH (glyph);
1148 else
1150 int xoff, yoff, wadjust;
1152 if (j < i)
1154 font->driver->draw (s, j, i, x, y, 0);
1155 if (s->face->overstrike)
1156 font->driver->draw (s, j, i, x + 1, y, 0);
1157 x += width;
1159 xoff = LGLYPH_XOFF (glyph);
1160 yoff = LGLYPH_YOFF (glyph);
1161 wadjust = LGLYPH_WADJUST (glyph);
1162 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1163 if (s->face->overstrike)
1164 font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff, 0);
1165 x += wadjust;
1166 j = i + 1;
1167 width = 0;
1170 if (j < i)
1172 font->driver->draw (s, j, i, x, y, 0);
1173 if (s->face->overstrike)
1174 font->driver->draw (s, j, i, x + 1, y, 0);
1180 /* Draw the foreground of glyph string S for glyphless characters. */
1182 static void
1183 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1185 struct glyph *glyph = s->first_glyph;
1186 XChar2b char2b[8];
1187 int x, i, j;
1189 /* If first glyph of S has a left box line, start drawing the text
1190 of S to the right of that box line. */
1191 if (s->face && s->face->box != FACE_NO_BOX
1192 && s->first_glyph->left_box_line_p)
1193 x = s->x + eabs (s->face->box_line_width);
1194 else
1195 x = s->x;
1197 s->char2b = char2b;
1199 for (i = 0; i < s->nchars; i++, glyph++)
1201 char buf[7], *str = NULL;
1202 int len = glyph->u.glyphless.len;
1204 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1206 if (len > 0
1207 && CHAR_TABLE_P (Vglyphless_char_display)
1208 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1209 >= 1))
1211 Lisp_Object acronym
1212 = (! glyph->u.glyphless.for_no_font
1213 ? CHAR_TABLE_REF (Vglyphless_char_display,
1214 glyph->u.glyphless.ch)
1215 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
1216 if (STRINGP (acronym))
1217 str = SSDATA (acronym);
1220 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
1222 sprintf (buf, "%0*X",
1223 glyph->u.glyphless.ch < 0x10000 ? 4 : 6,
1224 glyph->u.glyphless.ch);
1225 str = buf;
1228 if (str)
1230 int upper_len = (len + 1) / 2;
1231 unsigned code;
1233 /* It is assured that all LEN characters in STR is ASCII. */
1234 for (j = 0; j < len; j++)
1236 code = s->font->driver->encode_char (s->font, str[j]);
1237 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
1239 s->font->driver->draw (s, 0, upper_len,
1240 x + glyph->slice.glyphless.upper_xoff,
1241 s->ybase + glyph->slice.glyphless.upper_yoff,
1243 s->font->driver->draw (s, upper_len, len,
1244 x + glyph->slice.glyphless.lower_xoff,
1245 s->ybase + glyph->slice.glyphless.lower_yoff,
1248 if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
1249 XDrawRectangle (s->display, s->window, s->gc,
1250 x, s->ybase - glyph->ascent,
1251 glyph->pixel_width - 1,
1252 glyph->ascent + glyph->descent - 1);
1253 x += glyph->pixel_width;
1257 #ifdef USE_X_TOOLKIT
1259 #ifdef USE_LUCID
1261 /* Return the frame on which widget WIDGET is used.. Abort if frame
1262 cannot be determined. */
1264 static struct frame *
1265 x_frame_of_widget (Widget widget)
1267 struct x_display_info *dpyinfo;
1268 Lisp_Object tail, frame;
1269 struct frame *f;
1271 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1273 /* Find the top-level shell of the widget. Note that this function
1274 can be called when the widget is not yet realized, so XtWindow
1275 (widget) == 0. That's the reason we can't simply use
1276 x_any_window_to_frame. */
1277 while (!XtIsTopLevelShell (widget))
1278 widget = XtParent (widget);
1280 /* Look for a frame with that top-level widget. Allocate the color
1281 on that frame to get the right gamma correction value. */
1282 FOR_EACH_FRAME (tail, frame)
1284 f = XFRAME (frame);
1285 if (FRAME_X_P (f)
1286 && f->output_data.nothing != 1
1287 && FRAME_DISPLAY_INFO (f) == dpyinfo
1288 && f->output_data.x->widget == widget)
1289 return f;
1291 emacs_abort ();
1294 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1295 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1296 If this produces the same color as PIXEL, try a color where all RGB
1297 values have DELTA added. Return the allocated color in *PIXEL.
1298 DISPLAY is the X display, CMAP is the colormap to operate on.
1299 Value is true if successful. */
1301 bool
1302 x_alloc_lighter_color_for_widget (Widget widget, Display *display, Colormap cmap,
1303 unsigned long *pixel, double factor, int delta)
1305 struct frame *f = x_frame_of_widget (widget);
1306 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1309 #endif /* USE_LUCID */
1312 /* Structure specifying which arguments should be passed by Xt to
1313 cvt_string_to_pixel. We want the widget's screen and colormap. */
1315 static XtConvertArgRec cvt_string_to_pixel_args[] =
1317 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1318 sizeof (Screen *)},
1319 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1320 sizeof (Colormap)}
1324 /* The address of this variable is returned by
1325 cvt_string_to_pixel. */
1327 static Pixel cvt_string_to_pixel_value;
1330 /* Convert a color name to a pixel color.
1332 DPY is the display we are working on.
1334 ARGS is an array of *NARGS XrmValue structures holding additional
1335 information about the widget for which the conversion takes place.
1336 The contents of this array are determined by the specification
1337 in cvt_string_to_pixel_args.
1339 FROM is a pointer to an XrmValue which points to the color name to
1340 convert. TO is an XrmValue in which to return the pixel color.
1342 CLOSURE_RET is a pointer to user-data, in which we record if
1343 we allocated the color or not.
1345 Value is True if successful, False otherwise. */
1347 static Boolean
1348 cvt_string_to_pixel (Display *dpy, XrmValue *args, Cardinal *nargs,
1349 XrmValue *from, XrmValue *to,
1350 XtPointer *closure_ret)
1352 Screen *screen;
1353 Colormap cmap;
1354 Pixel pixel;
1355 String color_name;
1356 XColor color;
1358 if (*nargs != 2)
1360 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1361 "wrongParameters", "cvt_string_to_pixel",
1362 "XtToolkitError",
1363 "Screen and colormap args required", NULL, NULL);
1364 return False;
1367 screen = *(Screen **) args[0].addr;
1368 cmap = *(Colormap *) args[1].addr;
1369 color_name = (String) from->addr;
1371 if (strcmp (color_name, XtDefaultBackground) == 0)
1373 *closure_ret = (XtPointer) False;
1374 pixel = WhitePixelOfScreen (screen);
1376 else if (strcmp (color_name, XtDefaultForeground) == 0)
1378 *closure_ret = (XtPointer) False;
1379 pixel = BlackPixelOfScreen (screen);
1381 else if (XParseColor (dpy, cmap, color_name, &color)
1382 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1384 pixel = color.pixel;
1385 *closure_ret = (XtPointer) True;
1387 else
1389 String params[1];
1390 Cardinal nparams = 1;
1392 params[0] = color_name;
1393 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1394 "badValue", "cvt_string_to_pixel",
1395 "XtToolkitError", "Invalid color `%s'",
1396 params, &nparams);
1397 return False;
1400 if (to->addr != NULL)
1402 if (to->size < sizeof (Pixel))
1404 to->size = sizeof (Pixel);
1405 return False;
1408 *(Pixel *) to->addr = pixel;
1410 else
1412 cvt_string_to_pixel_value = pixel;
1413 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1416 to->size = sizeof (Pixel);
1417 return True;
1421 /* Free a pixel color which was previously allocated via
1422 cvt_string_to_pixel. This is registered as the destructor
1423 for this type of resource via XtSetTypeConverter.
1425 APP is the application context in which we work.
1427 TO is a pointer to an XrmValue holding the color to free.
1428 CLOSURE is the value we stored in CLOSURE_RET for this color
1429 in cvt_string_to_pixel.
1431 ARGS and NARGS are like for cvt_string_to_pixel. */
1433 static void
1434 cvt_pixel_dtor (XtAppContext app, XrmValuePtr to, XtPointer closure, XrmValuePtr args,
1435 Cardinal *nargs)
1437 if (*nargs != 2)
1439 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1440 "XtToolkitError",
1441 "Screen and colormap arguments required",
1442 NULL, NULL);
1444 else if (closure != NULL)
1446 /* We did allocate the pixel, so free it. */
1447 Screen *screen = *(Screen **) args[0].addr;
1448 Colormap cmap = *(Colormap *) args[1].addr;
1449 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1450 (Pixel *) to->addr, 1);
1455 #endif /* USE_X_TOOLKIT */
1458 /* Value is an array of XColor structures for the contents of the
1459 color map of display DPY. Set *NCELLS to the size of the array.
1460 Note that this probably shouldn't be called for large color maps,
1461 say a 24-bit TrueColor map. */
1463 static const XColor *
1464 x_color_cells (Display *dpy, int *ncells)
1466 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1468 if (dpyinfo->color_cells == NULL)
1470 Screen *screen = dpyinfo->screen;
1471 int ncolor_cells = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1472 int i;
1474 dpyinfo->color_cells = xnmalloc (ncolor_cells,
1475 sizeof *dpyinfo->color_cells);
1476 dpyinfo->ncolor_cells = ncolor_cells;
1478 for (i = 0; i < ncolor_cells; ++i)
1479 dpyinfo->color_cells[i].pixel = i;
1481 XQueryColors (dpy, dpyinfo->cmap,
1482 dpyinfo->color_cells, ncolor_cells);
1485 *ncells = dpyinfo->ncolor_cells;
1486 return dpyinfo->color_cells;
1490 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1491 colors in COLORS. Use cached information, if available. */
1493 void
1494 x_query_colors (struct frame *f, XColor *colors, int ncolors)
1496 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1498 if (dpyinfo->color_cells)
1500 int i;
1501 for (i = 0; i < ncolors; ++i)
1503 unsigned long pixel = colors[i].pixel;
1504 eassert (pixel < dpyinfo->ncolor_cells);
1505 eassert (dpyinfo->color_cells[pixel].pixel == pixel);
1506 colors[i] = dpyinfo->color_cells[pixel];
1509 else
1510 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1514 /* On frame F, translate pixel color to RGB values for the color in
1515 COLOR. Use cached information, if available. */
1517 void
1518 x_query_color (struct frame *f, XColor *color)
1520 x_query_colors (f, color, 1);
1524 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1525 exact match can't be allocated, try the nearest color available.
1526 Value is true if successful. Set *COLOR to the color
1527 allocated. */
1529 static bool
1530 x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color)
1532 bool rc;
1534 rc = XAllocColor (dpy, cmap, color) != 0;
1535 if (rc == 0)
1537 /* If we got to this point, the colormap is full, so we're going
1538 to try to get the next closest color. The algorithm used is
1539 a least-squares matching, which is what X uses for closest
1540 color matching with StaticColor visuals. */
1541 int nearest, i;
1542 int max_color_delta = 255;
1543 int max_delta = 3 * max_color_delta;
1544 int nearest_delta = max_delta + 1;
1545 int ncells;
1546 const XColor *cells = x_color_cells (dpy, &ncells);
1548 for (nearest = i = 0; i < ncells; ++i)
1550 int dred = (color->red >> 8) - (cells[i].red >> 8);
1551 int dgreen = (color->green >> 8) - (cells[i].green >> 8);
1552 int dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1553 int delta = dred * dred + dgreen * dgreen + dblue * dblue;
1555 if (delta < nearest_delta)
1557 nearest = i;
1558 nearest_delta = delta;
1562 color->red = cells[nearest].red;
1563 color->green = cells[nearest].green;
1564 color->blue = cells[nearest].blue;
1565 rc = XAllocColor (dpy, cmap, color) != 0;
1567 else
1569 /* If allocation succeeded, and the allocated pixel color is not
1570 equal to a cached pixel color recorded earlier, there was a
1571 change in the colormap, so clear the color cache. */
1572 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1573 XColor *cached_color;
1575 if (dpyinfo->color_cells
1576 && (cached_color = &dpyinfo->color_cells[color->pixel],
1577 (cached_color->red != color->red
1578 || cached_color->blue != color->blue
1579 || cached_color->green != color->green)))
1581 xfree (dpyinfo->color_cells);
1582 dpyinfo->color_cells = NULL;
1583 dpyinfo->ncolor_cells = 0;
1587 #ifdef DEBUG_X_COLORS
1588 if (rc)
1589 register_color (color->pixel);
1590 #endif /* DEBUG_X_COLORS */
1592 return rc;
1596 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1597 exact match can't be allocated, try the nearest color available.
1598 Value is true if successful. Set *COLOR to the color
1599 allocated. */
1601 bool
1602 x_alloc_nearest_color (struct frame *f, Colormap cmap, XColor *color)
1604 gamma_correct (f, color);
1605 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1609 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1610 It's necessary to do this instead of just using PIXEL directly to
1611 get color reference counts right. */
1613 unsigned long
1614 x_copy_color (struct frame *f, unsigned long pixel)
1616 XColor color;
1618 color.pixel = pixel;
1619 block_input ();
1620 x_query_color (f, &color);
1621 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1622 unblock_input ();
1623 #ifdef DEBUG_X_COLORS
1624 register_color (pixel);
1625 #endif
1626 return color.pixel;
1630 /* Brightness beyond which a color won't have its highlight brightness
1631 boosted.
1633 Nominally, highlight colors for `3d' faces are calculated by
1634 brightening an object's color by a constant scale factor, but this
1635 doesn't yield good results for dark colors, so for colors who's
1636 brightness is less than this value (on a scale of 0-65535) have an
1637 use an additional additive factor.
1639 The value here is set so that the default menu-bar/mode-line color
1640 (grey75) will not have its highlights changed at all. */
1641 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1644 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1645 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1646 If this produces the same color as PIXEL, try a color where all RGB
1647 values have DELTA added. Return the allocated color in *PIXEL.
1648 DISPLAY is the X display, CMAP is the colormap to operate on.
1649 Value is non-zero if successful. */
1651 static bool
1652 x_alloc_lighter_color (struct frame *f, Display *display, Colormap cmap,
1653 unsigned long *pixel, double factor, int delta)
1655 XColor color, new;
1656 long bright;
1657 bool success_p;
1659 /* Get RGB color values. */
1660 color.pixel = *pixel;
1661 x_query_color (f, &color);
1663 /* Change RGB values by specified FACTOR. Avoid overflow! */
1664 eassert (factor >= 0);
1665 new.red = min (0xffff, factor * color.red);
1666 new.green = min (0xffff, factor * color.green);
1667 new.blue = min (0xffff, factor * color.blue);
1669 /* Calculate brightness of COLOR. */
1670 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1672 /* We only boost colors that are darker than
1673 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1674 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1675 /* Make an additive adjustment to NEW, because it's dark enough so
1676 that scaling by FACTOR alone isn't enough. */
1678 /* How far below the limit this color is (0 - 1, 1 being darker). */
1679 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1680 /* The additive adjustment. */
1681 int min_delta = delta * dimness * factor / 2;
1683 if (factor < 1)
1685 new.red = max (0, new.red - min_delta);
1686 new.green = max (0, new.green - min_delta);
1687 new.blue = max (0, new.blue - min_delta);
1689 else
1691 new.red = min (0xffff, min_delta + new.red);
1692 new.green = min (0xffff, min_delta + new.green);
1693 new.blue = min (0xffff, min_delta + new.blue);
1697 /* Try to allocate the color. */
1698 success_p = x_alloc_nearest_color (f, cmap, &new);
1699 if (success_p)
1701 if (new.pixel == *pixel)
1703 /* If we end up with the same color as before, try adding
1704 delta to the RGB values. */
1705 x_free_colors (f, &new.pixel, 1);
1707 new.red = min (0xffff, delta + color.red);
1708 new.green = min (0xffff, delta + color.green);
1709 new.blue = min (0xffff, delta + color.blue);
1710 success_p = x_alloc_nearest_color (f, cmap, &new);
1712 else
1713 success_p = 1;
1714 *pixel = new.pixel;
1717 return success_p;
1721 /* Set up the foreground color for drawing relief lines of glyph
1722 string S. RELIEF is a pointer to a struct relief containing the GC
1723 with which lines will be drawn. Use a color that is FACTOR or
1724 DELTA lighter or darker than the relief's background which is found
1725 in S->f->output_data.x->relief_background. If such a color cannot
1726 be allocated, use DEFAULT_PIXEL, instead. */
1728 static void
1729 x_setup_relief_color (struct frame *f, struct relief *relief, double factor,
1730 int delta, unsigned long default_pixel)
1732 XGCValues xgcv;
1733 struct x_output *di = f->output_data.x;
1734 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1735 unsigned long pixel;
1736 unsigned long background = di->relief_background;
1737 Colormap cmap = FRAME_X_COLORMAP (f);
1738 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1739 Display *dpy = FRAME_X_DISPLAY (f);
1741 xgcv.graphics_exposures = False;
1742 xgcv.line_width = 1;
1744 /* Free previously allocated color. The color cell will be reused
1745 when it has been freed as many times as it was allocated, so this
1746 doesn't affect faces using the same colors. */
1747 if (relief->gc && relief->pixel != -1)
1749 x_free_colors (f, &relief->pixel, 1);
1750 relief->pixel = -1;
1753 /* Allocate new color. */
1754 xgcv.foreground = default_pixel;
1755 pixel = background;
1756 if (dpyinfo->n_planes != 1
1757 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1758 xgcv.foreground = relief->pixel = pixel;
1760 if (relief->gc == 0)
1762 xgcv.stipple = dpyinfo->gray;
1763 mask |= GCStipple;
1764 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1766 else
1767 XChangeGC (dpy, relief->gc, mask, &xgcv);
1771 /* Set up colors for the relief lines around glyph string S. */
1773 static void
1774 x_setup_relief_colors (struct glyph_string *s)
1776 struct x_output *di = s->f->output_data.x;
1777 unsigned long color;
1779 if (s->face->use_box_color_for_shadows_p)
1780 color = s->face->box_color;
1781 else if (s->first_glyph->type == IMAGE_GLYPH
1782 && s->img->pixmap
1783 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1784 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1785 else
1787 XGCValues xgcv;
1789 /* Get the background color of the face. */
1790 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
1791 color = xgcv.background;
1794 if (di->white_relief.gc == 0
1795 || color != di->relief_background)
1797 di->relief_background = color;
1798 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1799 WHITE_PIX_DEFAULT (s->f));
1800 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1801 BLACK_PIX_DEFAULT (s->f));
1806 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
1807 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
1808 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
1809 relief. LEFT_P non-zero means draw a relief on the left side of
1810 the rectangle. RIGHT_P non-zero means draw a relief on the right
1811 side of the rectangle. CLIP_RECT is the clipping rectangle to use
1812 when drawing. */
1814 static void
1815 x_draw_relief_rect (struct frame *f,
1816 int left_x, int top_y, int right_x, int bottom_y, int width,
1817 int raised_p, int top_p, int bot_p, int left_p, int right_p,
1818 XRectangle *clip_rect)
1820 Display *dpy = FRAME_X_DISPLAY (f);
1821 Window window = FRAME_X_WINDOW (f);
1822 int i;
1823 GC gc;
1825 if (raised_p)
1826 gc = f->output_data.x->white_relief.gc;
1827 else
1828 gc = f->output_data.x->black_relief.gc;
1829 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
1831 /* This code is more complicated than it has to be, because of two
1832 minor hacks to make the boxes look nicer: (i) if width > 1, draw
1833 the outermost line using the black relief. (ii) Omit the four
1834 corner pixels. */
1836 /* Top. */
1837 if (top_p)
1839 if (width == 1)
1840 XDrawLine (dpy, window, gc,
1841 left_x + (left_p ? 1 : 0), top_y,
1842 right_x + (right_p ? 0 : 1), top_y);
1844 for (i = 1; i < width; ++i)
1845 XDrawLine (dpy, window, gc,
1846 left_x + i * left_p, top_y + i,
1847 right_x + 1 - i * right_p, top_y + i);
1850 /* Left. */
1851 if (left_p)
1853 if (width == 1)
1854 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
1856 XClearArea (dpy, window, left_x, top_y, 1, 1, False);
1857 XClearArea (dpy, window, left_x, bottom_y, 1, 1, False);
1859 for (i = (width > 1 ? 1 : 0); i < width; ++i)
1860 XDrawLine (dpy, window, gc,
1861 left_x + i, top_y + (i + 1) * top_p,
1862 left_x + i, bottom_y + 1 - (i + 1) * bot_p);
1865 XSetClipMask (dpy, gc, None);
1866 if (raised_p)
1867 gc = f->output_data.x->black_relief.gc;
1868 else
1869 gc = f->output_data.x->white_relief.gc;
1870 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
1872 if (width > 1)
1874 /* Outermost top line. */
1875 if (top_p)
1876 XDrawLine (dpy, window, gc,
1877 left_x + (left_p ? 1 : 0), top_y,
1878 right_x + (right_p ? 0 : 1), top_y);
1880 /* Outermost left line. */
1881 if (left_p)
1882 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
1885 /* Bottom. */
1886 if (bot_p)
1888 XDrawLine (dpy, window, gc,
1889 left_x + (left_p ? 1 : 0), bottom_y,
1890 right_x + (right_p ? 0 : 1), bottom_y);
1891 for (i = 1; i < width; ++i)
1892 XDrawLine (dpy, window, gc,
1893 left_x + i * left_p, bottom_y - i,
1894 right_x + 1 - i * right_p, bottom_y - i);
1897 /* Right. */
1898 if (right_p)
1900 XClearArea (dpy, window, right_x, top_y, 1, 1, False);
1901 XClearArea (dpy, window, right_x, bottom_y, 1, 1, False);
1902 for (i = 0; i < width; ++i)
1903 XDrawLine (dpy, window, gc,
1904 right_x - i, top_y + (i + 1) * top_p,
1905 right_x - i, bottom_y + 1 - (i + 1) * bot_p);
1908 XSetClipMask (dpy, gc, None);
1912 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
1913 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
1914 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
1915 left side of the rectangle. RIGHT_P non-zero means draw a line
1916 on the right side of the rectangle. CLIP_RECT is the clipping
1917 rectangle to use when drawing. */
1919 static void
1920 x_draw_box_rect (struct glyph_string *s,
1921 int left_x, int top_y, int right_x, int bottom_y, int width,
1922 int left_p, int right_p, XRectangle *clip_rect)
1924 XGCValues xgcv;
1926 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
1927 XSetForeground (s->display, s->gc, s->face->box_color);
1928 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
1930 /* Top. */
1931 XFillRectangle (s->display, s->window, s->gc,
1932 left_x, top_y, right_x - left_x + 1, width);
1934 /* Left. */
1935 if (left_p)
1936 XFillRectangle (s->display, s->window, s->gc,
1937 left_x, top_y, width, bottom_y - top_y + 1);
1939 /* Bottom. */
1940 XFillRectangle (s->display, s->window, s->gc,
1941 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
1943 /* Right. */
1944 if (right_p)
1945 XFillRectangle (s->display, s->window, s->gc,
1946 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
1948 XSetForeground (s->display, s->gc, xgcv.foreground);
1949 XSetClipMask (s->display, s->gc, None);
1953 /* Draw a box around glyph string S. */
1955 static void
1956 x_draw_glyph_string_box (struct glyph_string *s)
1958 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
1959 int left_p, right_p;
1960 struct glyph *last_glyph;
1961 XRectangle clip_rect;
1963 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
1964 ? WINDOW_RIGHT_EDGE_X (s->w)
1965 : window_box_right (s->w, s->area));
1967 /* The glyph that may have a right box line. */
1968 last_glyph = (s->cmp || s->img
1969 ? s->first_glyph
1970 : s->first_glyph + s->nchars - 1);
1972 width = eabs (s->face->box_line_width);
1973 raised_p = s->face->box == FACE_RAISED_BOX;
1974 left_x = s->x;
1975 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
1976 ? last_x - 1
1977 : min (last_x, s->x + s->background_width) - 1);
1978 top_y = s->y;
1979 bottom_y = top_y + s->height - 1;
1981 left_p = (s->first_glyph->left_box_line_p
1982 || (s->hl == DRAW_MOUSE_FACE
1983 && (s->prev == NULL
1984 || s->prev->hl != s->hl)));
1985 right_p = (last_glyph->right_box_line_p
1986 || (s->hl == DRAW_MOUSE_FACE
1987 && (s->next == NULL
1988 || s->next->hl != s->hl)));
1990 get_glyph_string_clip_rect (s, &clip_rect);
1992 if (s->face->box == FACE_SIMPLE_BOX)
1993 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
1994 left_p, right_p, &clip_rect);
1995 else
1997 x_setup_relief_colors (s);
1998 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
1999 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2004 /* Draw foreground of image glyph string S. */
2006 static void
2007 x_draw_image_foreground (struct glyph_string *s)
2009 int x = s->x;
2010 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2012 /* If first glyph of S has a left box line, start drawing it to the
2013 right of that line. */
2014 if (s->face->box != FACE_NO_BOX
2015 && s->first_glyph->left_box_line_p
2016 && s->slice.x == 0)
2017 x += eabs (s->face->box_line_width);
2019 /* If there is a margin around the image, adjust x- and y-position
2020 by that margin. */
2021 if (s->slice.x == 0)
2022 x += s->img->hmargin;
2023 if (s->slice.y == 0)
2024 y += s->img->vmargin;
2026 if (s->img->pixmap)
2028 if (s->img->mask)
2030 /* We can't set both a clip mask and use XSetClipRectangles
2031 because the latter also sets a clip mask. We also can't
2032 trust on the shape extension to be available
2033 (XShapeCombineRegion). So, compute the rectangle to draw
2034 manually. */
2035 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2036 | GCFunction);
2037 XGCValues xgcv;
2038 XRectangle clip_rect, image_rect, r;
2040 xgcv.clip_mask = s->img->mask;
2041 xgcv.clip_x_origin = x;
2042 xgcv.clip_y_origin = y;
2043 xgcv.function = GXcopy;
2044 XChangeGC (s->display, s->gc, mask, &xgcv);
2046 get_glyph_string_clip_rect (s, &clip_rect);
2047 image_rect.x = x;
2048 image_rect.y = y;
2049 image_rect.width = s->slice.width;
2050 image_rect.height = s->slice.height;
2051 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2052 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2053 s->slice.x + r.x - x, s->slice.y + r.y - y,
2054 r.width, r.height, r.x, r.y);
2056 else
2058 XRectangle clip_rect, image_rect, r;
2060 get_glyph_string_clip_rect (s, &clip_rect);
2061 image_rect.x = x;
2062 image_rect.y = y;
2063 image_rect.width = s->slice.width;
2064 image_rect.height = s->slice.height;
2065 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2066 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2067 s->slice.x + r.x - x, s->slice.y + r.y - y,
2068 r.width, r.height, r.x, r.y);
2070 /* When the image has a mask, we can expect that at
2071 least part of a mouse highlight or a block cursor will
2072 be visible. If the image doesn't have a mask, make
2073 a block cursor visible by drawing a rectangle around
2074 the image. I believe it's looking better if we do
2075 nothing here for mouse-face. */
2076 if (s->hl == DRAW_CURSOR)
2078 int relief = eabs (s->img->relief);
2079 XDrawRectangle (s->display, s->window, s->gc,
2080 x - relief, y - relief,
2081 s->slice.width + relief*2 - 1,
2082 s->slice.height + relief*2 - 1);
2086 else
2087 /* Draw a rectangle if image could not be loaded. */
2088 XDrawRectangle (s->display, s->window, s->gc, x, y,
2089 s->slice.width - 1, s->slice.height - 1);
2093 /* Draw a relief around the image glyph string S. */
2095 static void
2096 x_draw_image_relief (struct glyph_string *s)
2098 int x1, y1, thick, raised_p, top_p, bot_p, left_p, right_p;
2099 XRectangle r;
2100 int x = s->x;
2101 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2103 /* If first glyph of S has a left box line, start drawing it to the
2104 right of that line. */
2105 if (s->face->box != FACE_NO_BOX
2106 && s->first_glyph->left_box_line_p
2107 && s->slice.x == 0)
2108 x += eabs (s->face->box_line_width);
2110 /* If there is a margin around the image, adjust x- and y-position
2111 by that margin. */
2112 if (s->slice.x == 0)
2113 x += s->img->hmargin;
2114 if (s->slice.y == 0)
2115 y += s->img->vmargin;
2117 if (s->hl == DRAW_IMAGE_SUNKEN
2118 || s->hl == DRAW_IMAGE_RAISED)
2120 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2121 raised_p = s->hl == DRAW_IMAGE_RAISED;
2123 else
2125 thick = eabs (s->img->relief);
2126 raised_p = s->img->relief > 0;
2129 x1 = x + s->slice.width - 1;
2130 y1 = y + s->slice.height - 1;
2131 top_p = bot_p = left_p = right_p = 0;
2133 if (s->slice.x == 0)
2134 x -= thick, left_p = 1;
2135 if (s->slice.y == 0)
2136 y -= thick, top_p = 1;
2137 if (s->slice.x + s->slice.width == s->img->width)
2138 x1 += thick, right_p = 1;
2139 if (s->slice.y + s->slice.height == s->img->height)
2140 y1 += thick, bot_p = 1;
2142 x_setup_relief_colors (s);
2143 get_glyph_string_clip_rect (s, &r);
2144 x_draw_relief_rect (s->f, x, y, x1, y1, thick, raised_p,
2145 top_p, bot_p, left_p, right_p, &r);
2149 /* Draw the foreground of image glyph string S to PIXMAP. */
2151 static void
2152 x_draw_image_foreground_1 (struct glyph_string *s, Pixmap pixmap)
2154 int x = 0;
2155 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2157 /* If first glyph of S has a left box line, start drawing it to the
2158 right of that line. */
2159 if (s->face->box != FACE_NO_BOX
2160 && s->first_glyph->left_box_line_p
2161 && s->slice.x == 0)
2162 x += eabs (s->face->box_line_width);
2164 /* If there is a margin around the image, adjust x- and y-position
2165 by that margin. */
2166 if (s->slice.x == 0)
2167 x += s->img->hmargin;
2168 if (s->slice.y == 0)
2169 y += s->img->vmargin;
2171 if (s->img->pixmap)
2173 if (s->img->mask)
2175 /* We can't set both a clip mask and use XSetClipRectangles
2176 because the latter also sets a clip mask. We also can't
2177 trust on the shape extension to be available
2178 (XShapeCombineRegion). So, compute the rectangle to draw
2179 manually. */
2180 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2181 | GCFunction);
2182 XGCValues xgcv;
2184 xgcv.clip_mask = s->img->mask;
2185 xgcv.clip_x_origin = x - s->slice.x;
2186 xgcv.clip_y_origin = y - s->slice.y;
2187 xgcv.function = GXcopy;
2188 XChangeGC (s->display, s->gc, mask, &xgcv);
2190 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2191 s->slice.x, s->slice.y,
2192 s->slice.width, s->slice.height, x, y);
2193 XSetClipMask (s->display, s->gc, None);
2195 else
2197 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2198 s->slice.x, s->slice.y,
2199 s->slice.width, s->slice.height, x, y);
2201 /* When the image has a mask, we can expect that at
2202 least part of a mouse highlight or a block cursor will
2203 be visible. If the image doesn't have a mask, make
2204 a block cursor visible by drawing a rectangle around
2205 the image. I believe it's looking better if we do
2206 nothing here for mouse-face. */
2207 if (s->hl == DRAW_CURSOR)
2209 int r = eabs (s->img->relief);
2210 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2211 s->slice.width + r*2 - 1,
2212 s->slice.height + r*2 - 1);
2216 else
2217 /* Draw a rectangle if image could not be loaded. */
2218 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2219 s->slice.width - 1, s->slice.height - 1);
2223 /* Draw part of the background of glyph string S. X, Y, W, and H
2224 give the rectangle to draw. */
2226 static void
2227 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
2229 if (s->stippled_p)
2231 /* Fill background with a stipple pattern. */
2232 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2233 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2234 XSetFillStyle (s->display, s->gc, FillSolid);
2236 else
2237 x_clear_glyph_string_rect (s, x, y, w, h);
2241 /* Draw image glyph string S.
2243 s->y
2244 s->x +-------------------------
2245 | s->face->box
2247 | +-------------------------
2248 | | s->img->margin
2250 | | +-------------------
2251 | | | the image
2255 static void
2256 x_draw_image_glyph_string (struct glyph_string *s)
2258 int box_line_hwidth = eabs (s->face->box_line_width);
2259 int box_line_vwidth = max (s->face->box_line_width, 0);
2260 int height;
2261 Pixmap pixmap = None;
2263 height = s->height;
2264 if (s->slice.y == 0)
2265 height -= box_line_vwidth;
2266 if (s->slice.y + s->slice.height >= s->img->height)
2267 height -= box_line_vwidth;
2269 /* Fill background with face under the image. Do it only if row is
2270 taller than image or if image has a clip mask to reduce
2271 flickering. */
2272 s->stippled_p = s->face->stipple != 0;
2273 if (height > s->slice.height
2274 || s->img->hmargin
2275 || s->img->vmargin
2276 || s->img->mask
2277 || s->img->pixmap == 0
2278 || s->width != s->background_width)
2280 if (s->img->mask)
2282 /* Create a pixmap as large as the glyph string. Fill it
2283 with the background color. Copy the image to it, using
2284 its mask. Copy the temporary pixmap to the display. */
2285 Screen *screen = FRAME_X_SCREEN (s->f);
2286 int depth = DefaultDepthOfScreen (screen);
2288 /* Create a pixmap as large as the glyph string. */
2289 pixmap = XCreatePixmap (s->display, s->window,
2290 s->background_width,
2291 s->height, depth);
2293 /* Don't clip in the following because we're working on the
2294 pixmap. */
2295 XSetClipMask (s->display, s->gc, None);
2297 /* Fill the pixmap with the background color/stipple. */
2298 if (s->stippled_p)
2300 /* Fill background with a stipple pattern. */
2301 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2302 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
2303 XFillRectangle (s->display, pixmap, s->gc,
2304 0, 0, s->background_width, s->height);
2305 XSetFillStyle (s->display, s->gc, FillSolid);
2306 XSetTSOrigin (s->display, s->gc, 0, 0);
2308 else
2310 XGCValues xgcv;
2311 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2312 &xgcv);
2313 XSetForeground (s->display, s->gc, xgcv.background);
2314 XFillRectangle (s->display, pixmap, s->gc,
2315 0, 0, s->background_width, s->height);
2316 XSetForeground (s->display, s->gc, xgcv.foreground);
2319 else
2321 int x = s->x;
2322 int y = s->y;
2324 if (s->first_glyph->left_box_line_p
2325 && s->slice.x == 0)
2326 x += box_line_hwidth;
2328 if (s->slice.y == 0)
2329 y += box_line_vwidth;
2331 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2334 s->background_filled_p = 1;
2337 /* Draw the foreground. */
2338 if (pixmap != None)
2340 x_draw_image_foreground_1 (s, pixmap);
2341 x_set_glyph_string_clipping (s);
2342 XCopyArea (s->display, pixmap, s->window, s->gc,
2343 0, 0, s->background_width, s->height, s->x, s->y);
2344 XFreePixmap (s->display, pixmap);
2346 else
2347 x_draw_image_foreground (s);
2349 /* If we must draw a relief around the image, do it. */
2350 if (s->img->relief
2351 || s->hl == DRAW_IMAGE_RAISED
2352 || s->hl == DRAW_IMAGE_SUNKEN)
2353 x_draw_image_relief (s);
2357 /* Draw stretch glyph string S. */
2359 static void
2360 x_draw_stretch_glyph_string (struct glyph_string *s)
2362 eassert (s->first_glyph->type == STRETCH_GLYPH);
2364 if (s->hl == DRAW_CURSOR
2365 && !x_stretch_cursor_p)
2367 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
2368 wide as the stretch glyph. */
2369 int width, background_width = s->background_width;
2370 int x = s->x;
2372 if (!s->row->reversed_p)
2374 int left_x = window_box_left_offset (s->w, TEXT_AREA);
2376 if (x < left_x)
2378 background_width -= left_x - x;
2379 x = left_x;
2382 else
2384 /* In R2L rows, draw the cursor on the right edge of the
2385 stretch glyph. */
2386 int right_x = window_box_right_offset (s->w, TEXT_AREA);
2388 if (x + background_width > right_x)
2389 background_width -= x - right_x;
2390 x += background_width;
2392 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2393 if (s->row->reversed_p)
2394 x -= width;
2396 /* Draw cursor. */
2397 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2399 /* Clear rest using the GC of the original non-cursor face. */
2400 if (width < background_width)
2402 int y = s->y;
2403 int w = background_width - width, h = s->height;
2404 XRectangle r;
2405 GC gc;
2407 if (!s->row->reversed_p)
2408 x += width;
2409 else
2410 x = s->x;
2411 if (s->row->mouse_face_p
2412 && cursor_in_mouse_face_p (s->w))
2414 x_set_mouse_face_gc (s);
2415 gc = s->gc;
2417 else
2418 gc = s->face->gc;
2420 get_glyph_string_clip_rect (s, &r);
2421 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2423 if (s->face->stipple)
2425 /* Fill background with a stipple pattern. */
2426 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2427 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2428 XSetFillStyle (s->display, gc, FillSolid);
2430 else
2432 XGCValues xgcv;
2433 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2434 XSetForeground (s->display, gc, xgcv.background);
2435 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2436 XSetForeground (s->display, gc, xgcv.foreground);
2440 else if (!s->background_filled_p)
2442 int background_width = s->background_width;
2443 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2445 /* Don't draw into left margin, fringe or scrollbar area
2446 except for header line and mode line. */
2447 if (x < left_x && !s->row->mode_line_p)
2449 background_width -= left_x - x;
2450 x = left_x;
2452 if (background_width > 0)
2453 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2456 s->background_filled_p = 1;
2460 Draw a wavy line under S. The wave fills wave_height pixels from y0.
2462 x0 wave_length = 2
2464 y0 * * * * *
2465 |* * * * * * * * *
2466 wave_height = 3 | * * * *
2470 static void
2471 x_draw_underwave (struct glyph_string *s)
2473 int wave_height = 3, wave_length = 2;
2474 int dx, dy, x0, y0, width, x1, y1, x2, y2, odd, xmax;
2475 XRectangle wave_clip, string_clip, final_clip;
2477 dx = wave_length;
2478 dy = wave_height - 1;
2479 x0 = s->x;
2480 y0 = s->ybase - wave_height + 3;
2481 width = s->width;
2482 xmax = x0 + width;
2484 /* Find and set clipping rectangle */
2486 wave_clip.x = x0;
2487 wave_clip.y = y0;
2488 wave_clip.width = width;
2489 wave_clip.height = wave_height;
2490 get_glyph_string_clip_rect (s, &string_clip);
2492 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
2493 return;
2495 XSetClipRectangles (s->display, s->gc, 0, 0, &final_clip, 1, Unsorted);
2497 /* Draw the waves */
2499 x1 = x0 - (x0 % dx);
2500 x2 = x1 + dx;
2501 odd = (x1/dx) % 2;
2502 y1 = y2 = y0;
2504 if (odd)
2505 y1 += dy;
2506 else
2507 y2 += dy;
2509 if (INT_MAX - dx < xmax)
2510 emacs_abort ();
2512 while (x1 <= xmax)
2514 XDrawLine (s->display, s->window, s->gc, x1, y1, x2, y2);
2515 x1 = x2, y1 = y2;
2516 x2 += dx, y2 = y0 + odd*dy;
2517 odd = !odd;
2520 /* Restore previous clipping rectangle(s) */
2521 XSetClipRectangles (s->display, s->gc, 0, 0, s->clip, s->num_clips, Unsorted);
2525 /* Draw glyph string S. */
2527 static void
2528 x_draw_glyph_string (struct glyph_string *s)
2530 bool relief_drawn_p = 0;
2532 /* If S draws into the background of its successors, draw the
2533 background of the successors first so that S can draw into it.
2534 This makes S->next use XDrawString instead of XDrawImageString. */
2535 if (s->next && s->right_overhang && !s->for_overlaps)
2537 int width;
2538 struct glyph_string *next;
2540 for (width = 0, next = s->next;
2541 next && width < s->right_overhang;
2542 width += next->width, next = next->next)
2543 if (next->first_glyph->type != IMAGE_GLYPH)
2545 x_set_glyph_string_gc (next);
2546 x_set_glyph_string_clipping (next);
2547 if (next->first_glyph->type == STRETCH_GLYPH)
2548 x_draw_stretch_glyph_string (next);
2549 else
2550 x_draw_glyph_string_background (next, 1);
2551 next->num_clips = 0;
2555 /* Set up S->gc, set clipping and draw S. */
2556 x_set_glyph_string_gc (s);
2558 /* Draw relief (if any) in advance for char/composition so that the
2559 glyph string can be drawn over it. */
2560 if (!s->for_overlaps
2561 && s->face->box != FACE_NO_BOX
2562 && (s->first_glyph->type == CHAR_GLYPH
2563 || s->first_glyph->type == COMPOSITE_GLYPH))
2566 x_set_glyph_string_clipping (s);
2567 x_draw_glyph_string_background (s, 1);
2568 x_draw_glyph_string_box (s);
2569 x_set_glyph_string_clipping (s);
2570 relief_drawn_p = 1;
2572 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2573 && !s->clip_tail
2574 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2575 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2576 /* We must clip just this glyph. left_overhang part has already
2577 drawn when s->prev was drawn, and right_overhang part will be
2578 drawn later when s->next is drawn. */
2579 x_set_glyph_string_clipping_exactly (s, s);
2580 else
2581 x_set_glyph_string_clipping (s);
2583 switch (s->first_glyph->type)
2585 case IMAGE_GLYPH:
2586 x_draw_image_glyph_string (s);
2587 break;
2589 case STRETCH_GLYPH:
2590 x_draw_stretch_glyph_string (s);
2591 break;
2593 case CHAR_GLYPH:
2594 if (s->for_overlaps)
2595 s->background_filled_p = 1;
2596 else
2597 x_draw_glyph_string_background (s, 0);
2598 x_draw_glyph_string_foreground (s);
2599 break;
2601 case COMPOSITE_GLYPH:
2602 if (s->for_overlaps || (s->cmp_from > 0
2603 && ! s->first_glyph->u.cmp.automatic))
2604 s->background_filled_p = 1;
2605 else
2606 x_draw_glyph_string_background (s, 1);
2607 x_draw_composite_glyph_string_foreground (s);
2608 break;
2610 case GLYPHLESS_GLYPH:
2611 if (s->for_overlaps)
2612 s->background_filled_p = 1;
2613 else
2614 x_draw_glyph_string_background (s, 1);
2615 x_draw_glyphless_glyph_string_foreground (s);
2616 break;
2618 default:
2619 emacs_abort ();
2622 if (!s->for_overlaps)
2624 /* Draw underline. */
2625 if (s->face->underline_p)
2627 if (s->face->underline_type == FACE_UNDER_WAVE)
2629 if (s->face->underline_defaulted_p)
2630 x_draw_underwave (s);
2631 else
2633 XGCValues xgcv;
2634 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2635 XSetForeground (s->display, s->gc, s->face->underline_color);
2636 x_draw_underwave (s);
2637 XSetForeground (s->display, s->gc, xgcv.foreground);
2640 else if (s->face->underline_type == FACE_UNDER_LINE)
2642 unsigned long thickness, position;
2643 int y;
2645 if (s->prev && s->prev->face->underline_p
2646 && s->prev->face->underline_type == FACE_UNDER_LINE)
2648 /* We use the same underline style as the previous one. */
2649 thickness = s->prev->underline_thickness;
2650 position = s->prev->underline_position;
2652 else
2654 /* Get the underline thickness. Default is 1 pixel. */
2655 if (s->font && s->font->underline_thickness > 0)
2656 thickness = s->font->underline_thickness;
2657 else
2658 thickness = 1;
2659 if (x_underline_at_descent_line)
2660 position = (s->height - thickness) - (s->ybase - s->y);
2661 else
2663 /* Get the underline position. This is the recommended
2664 vertical offset in pixels from the baseline to the top of
2665 the underline. This is a signed value according to the
2666 specs, and its default is
2668 ROUND ((maximum descent) / 2), with
2669 ROUND(x) = floor (x + 0.5) */
2671 if (x_use_underline_position_properties
2672 && s->font && s->font->underline_position >= 0)
2673 position = s->font->underline_position;
2674 else if (s->font)
2675 position = (s->font->descent + 1) / 2;
2676 else
2677 position = underline_minimum_offset;
2679 position = max (position, underline_minimum_offset);
2681 /* Check the sanity of thickness and position. We should
2682 avoid drawing underline out of the current line area. */
2683 if (s->y + s->height <= s->ybase + position)
2684 position = (s->height - 1) - (s->ybase - s->y);
2685 if (s->y + s->height < s->ybase + position + thickness)
2686 thickness = (s->y + s->height) - (s->ybase + position);
2687 s->underline_thickness = thickness;
2688 s->underline_position = position;
2689 y = s->ybase + position;
2690 if (s->face->underline_defaulted_p)
2691 XFillRectangle (s->display, s->window, s->gc,
2692 s->x, y, s->width, thickness);
2693 else
2695 XGCValues xgcv;
2696 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2697 XSetForeground (s->display, s->gc, s->face->underline_color);
2698 XFillRectangle (s->display, s->window, s->gc,
2699 s->x, y, s->width, thickness);
2700 XSetForeground (s->display, s->gc, xgcv.foreground);
2704 /* Draw overline. */
2705 if (s->face->overline_p)
2707 unsigned long dy = 0, h = 1;
2709 if (s->face->overline_color_defaulted_p)
2710 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2711 s->width, h);
2712 else
2714 XGCValues xgcv;
2715 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2716 XSetForeground (s->display, s->gc, s->face->overline_color);
2717 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2718 s->width, h);
2719 XSetForeground (s->display, s->gc, xgcv.foreground);
2723 /* Draw strike-through. */
2724 if (s->face->strike_through_p)
2726 unsigned long h = 1;
2727 unsigned long dy = (s->height - h) / 2;
2729 if (s->face->strike_through_color_defaulted_p)
2730 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2731 s->width, h);
2732 else
2734 XGCValues xgcv;
2735 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2736 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2737 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2738 s->width, h);
2739 XSetForeground (s->display, s->gc, xgcv.foreground);
2743 /* Draw relief if not yet drawn. */
2744 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2745 x_draw_glyph_string_box (s);
2747 if (s->prev)
2749 struct glyph_string *prev;
2751 for (prev = s->prev; prev; prev = prev->prev)
2752 if (prev->hl != s->hl
2753 && prev->x + prev->width + prev->right_overhang > s->x)
2755 /* As prev was drawn while clipped to its own area, we
2756 must draw the right_overhang part using s->hl now. */
2757 enum draw_glyphs_face save = prev->hl;
2759 prev->hl = s->hl;
2760 x_set_glyph_string_gc (prev);
2761 x_set_glyph_string_clipping_exactly (s, prev);
2762 if (prev->first_glyph->type == CHAR_GLYPH)
2763 x_draw_glyph_string_foreground (prev);
2764 else
2765 x_draw_composite_glyph_string_foreground (prev);
2766 XSetClipMask (prev->display, prev->gc, None);
2767 prev->hl = save;
2768 prev->num_clips = 0;
2772 if (s->next)
2774 struct glyph_string *next;
2776 for (next = s->next; next; next = next->next)
2777 if (next->hl != s->hl
2778 && next->x - next->left_overhang < s->x + s->width)
2780 /* As next will be drawn while clipped to its own area,
2781 we must draw the left_overhang part using s->hl now. */
2782 enum draw_glyphs_face save = next->hl;
2784 next->hl = s->hl;
2785 x_set_glyph_string_gc (next);
2786 x_set_glyph_string_clipping_exactly (s, next);
2787 if (next->first_glyph->type == CHAR_GLYPH)
2788 x_draw_glyph_string_foreground (next);
2789 else
2790 x_draw_composite_glyph_string_foreground (next);
2791 XSetClipMask (next->display, next->gc, None);
2792 next->hl = save;
2793 next->num_clips = 0;
2794 next->clip_head = s->next;
2799 /* Reset clipping. */
2800 XSetClipMask (s->display, s->gc, None);
2801 s->num_clips = 0;
2804 /* Shift display to make room for inserted glyphs. */
2806 static void
2807 x_shift_glyphs_for_insert (struct frame *f, int x, int y, int width, int height, int shift_by)
2809 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2810 f->output_data.x->normal_gc,
2811 x, y, width, height,
2812 x + shift_by, y);
2815 /* Delete N glyphs at the nominal cursor position. Not implemented
2816 for X frames. */
2818 static void
2819 x_delete_glyphs (struct frame *f, register int n)
2821 emacs_abort ();
2825 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2826 If they are <= 0, this is probably an error. */
2828 void
2829 x_clear_area (Display *dpy, Window window, int x, int y, int width, int height)
2831 eassert (width > 0 && height > 0);
2832 XClearArea (dpy, window, x, y, width, height, False);
2836 /* Clear an entire frame. */
2838 static void
2839 x_clear_frame (struct frame *f)
2841 /* Clearing the frame will erase any cursor, so mark them all as no
2842 longer visible. */
2843 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2845 block_input ();
2847 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2849 /* We have to clear the scroll bars. If we have changed colors or
2850 something like that, then they should be notified. */
2851 x_scroll_bar_clear (f);
2853 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
2854 /* Make sure scroll bars are redrawn. As they aren't redrawn by
2855 redisplay, do it here. */
2856 if (FRAME_GTK_WIDGET (f))
2857 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
2858 #endif
2860 XFlush (FRAME_X_DISPLAY (f));
2862 unblock_input ();
2867 /* Invert the middle quarter of the frame for .15 sec. */
2869 static void
2870 XTflash (struct frame *f)
2872 block_input ();
2875 #ifdef USE_GTK
2876 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
2877 when the scroll bars and the edit widget share the same X window. */
2878 GdkWindow *window = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
2879 #ifdef HAVE_GTK3
2880 cairo_t *cr = gdk_cairo_create (window);
2881 cairo_set_source_rgb (cr, 1, 1, 1);
2882 cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
2883 #define XFillRectangle(d, win, gc, x, y, w, h) \
2884 do { \
2885 cairo_rectangle (cr, x, y, w, h); \
2886 cairo_fill (cr); \
2888 while (0)
2889 #else /* ! HAVE_GTK3 */
2890 GdkGCValues vals;
2891 GdkGC *gc;
2892 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
2893 ^ FRAME_BACKGROUND_PIXEL (f));
2894 vals.function = GDK_XOR;
2895 gc = gdk_gc_new_with_values (window,
2896 &vals, GDK_GC_FUNCTION | GDK_GC_FOREGROUND);
2897 #define XFillRectangle(d, win, gc, x, y, w, h) \
2898 gdk_draw_rectangle (window, gc, TRUE, x, y, w, h)
2899 #endif /* ! HAVE_GTK3 */
2900 #else /* ! USE_GTK */
2901 GC gc;
2903 /* Create a GC that will use the GXxor function to flip foreground
2904 pixels into background pixels. */
2906 XGCValues values;
2908 values.function = GXxor;
2909 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
2910 ^ FRAME_BACKGROUND_PIXEL (f));
2912 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2913 GCFunction | GCForeground, &values);
2915 #endif
2917 /* Get the height not including a menu bar widget. */
2918 int height = FRAME_PIXEL_HEIGHT (f);
2919 /* Height of each line to flash. */
2920 int flash_height = FRAME_LINE_HEIGHT (f);
2921 /* These will be the left and right margins of the rectangles. */
2922 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
2923 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
2924 int width = flash_right - flash_left;
2926 /* If window is tall, flash top and bottom line. */
2927 if (height > 3 * FRAME_LINE_HEIGHT (f))
2929 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2930 flash_left,
2931 (FRAME_INTERNAL_BORDER_WIDTH (f)
2932 + FRAME_TOP_MARGIN_HEIGHT (f)),
2933 width, flash_height);
2934 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2935 flash_left,
2936 (height - flash_height
2937 - FRAME_INTERNAL_BORDER_WIDTH (f)),
2938 width, flash_height);
2941 else
2942 /* If it is short, flash it all. */
2943 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2944 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
2945 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
2947 x_flush (f);
2950 struct timespec delay = make_timespec (0, 150 * 1000 * 1000);
2951 struct timespec wakeup = timespec_add (current_timespec (), delay);
2953 /* Keep waiting until past the time wakeup or any input gets
2954 available. */
2955 while (! detect_input_pending ())
2957 struct timespec current = current_timespec ();
2958 struct timespec timeout;
2960 /* Break if result would not be positive. */
2961 if (timespec_cmp (wakeup, current) <= 0)
2962 break;
2964 /* How long `select' should wait. */
2965 timeout = make_timespec (0, 10 * 1000 * 1000);
2967 /* Try to wait that long--but we might wake up sooner. */
2968 pselect (0, NULL, NULL, NULL, &timeout, NULL);
2972 /* If window is tall, flash top and bottom line. */
2973 if (height > 3 * FRAME_LINE_HEIGHT (f))
2975 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2976 flash_left,
2977 (FRAME_INTERNAL_BORDER_WIDTH (f)
2978 + FRAME_TOP_MARGIN_HEIGHT (f)),
2979 width, flash_height);
2980 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2981 flash_left,
2982 (height - flash_height
2983 - FRAME_INTERNAL_BORDER_WIDTH (f)),
2984 width, flash_height);
2986 else
2987 /* If it is short, flash it all. */
2988 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2989 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
2990 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
2992 #ifdef USE_GTK
2993 #ifdef HAVE_GTK3
2994 cairo_destroy (cr);
2995 #else
2996 g_object_unref (G_OBJECT (gc));
2997 #endif
2998 #undef XFillRectangle
2999 #else
3000 XFreeGC (FRAME_X_DISPLAY (f), gc);
3001 #endif
3002 x_flush (f);
3006 unblock_input ();
3010 static void
3011 XTtoggle_invisible_pointer (struct frame *f, int invisible)
3013 block_input ();
3014 if (invisible)
3016 if (FRAME_DISPLAY_INFO (f)->invisible_cursor != 0)
3017 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3018 FRAME_DISPLAY_INFO (f)->invisible_cursor);
3020 else
3021 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3022 f->output_data.x->current_cursor);
3023 f->pointer_invisible = invisible;
3024 unblock_input ();
3028 /* Make audible bell. */
3030 static void
3031 XTring_bell (struct frame *f)
3033 if (FRAME_X_DISPLAY (f))
3035 if (visible_bell)
3036 XTflash (f);
3037 else
3039 block_input ();
3040 #ifdef HAVE_XKB
3041 XkbBell (FRAME_X_DISPLAY (f), None, 0, None);
3042 #else
3043 XBell (FRAME_X_DISPLAY (f), 0);
3044 #endif
3045 XFlush (FRAME_X_DISPLAY (f));
3046 unblock_input ();
3051 /***********************************************************************
3052 Line Dance
3053 ***********************************************************************/
3055 /* Perform an insert-lines or delete-lines operation, inserting N
3056 lines or deleting -N lines at vertical position VPOS. */
3058 static void
3059 x_ins_del_lines (struct frame *f, int vpos, int n)
3061 emacs_abort ();
3065 /* Scroll part of the display as described by RUN. */
3067 static void
3068 x_scroll_run (struct window *w, struct run *run)
3070 struct frame *f = XFRAME (w->frame);
3071 int x, y, width, height, from_y, to_y, bottom_y;
3073 /* Get frame-relative bounding box of the text display area of W,
3074 without mode lines. Include in this box the left and right
3075 fringe of W. */
3076 window_box (w, ANY_AREA, &x, &y, &width, &height);
3078 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3079 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3080 bottom_y = y + height;
3082 if (to_y < from_y)
3084 /* Scrolling up. Make sure we don't copy part of the mode
3085 line at the bottom. */
3086 if (from_y + run->height > bottom_y)
3087 height = bottom_y - from_y;
3088 else
3089 height = run->height;
3091 else
3093 /* Scrolling down. Make sure we don't copy over the mode line.
3094 at the bottom. */
3095 if (to_y + run->height > bottom_y)
3096 height = bottom_y - to_y;
3097 else
3098 height = run->height;
3101 block_input ();
3103 /* Cursor off. Will be switched on again in x_update_window_end. */
3104 x_clear_cursor (w);
3106 XCopyArea (FRAME_X_DISPLAY (f),
3107 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3108 f->output_data.x->normal_gc,
3109 x, from_y,
3110 width, height,
3111 x, to_y);
3113 unblock_input ();
3118 /***********************************************************************
3119 Exposure Events
3120 ***********************************************************************/
3123 static void
3124 frame_highlight (struct frame *f)
3126 /* We used to only do this if Vx_no_window_manager was non-nil, but
3127 the ICCCM (section 4.1.6) says that the window's border pixmap
3128 and border pixel are window attributes which are "private to the
3129 client", so we can always change it to whatever we want. */
3130 block_input ();
3131 /* I recently started to get errors in this XSetWindowBorder, depending on
3132 the window-manager in use, tho something more is at play since I've been
3133 using that same window-manager binary for ever. Let's not crash just
3134 because of this (bug#9310). */
3135 x_catch_errors (FRAME_X_DISPLAY (f));
3136 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3137 f->output_data.x->border_pixel);
3138 x_uncatch_errors ();
3139 unblock_input ();
3140 x_update_cursor (f, 1);
3141 x_set_frame_alpha (f);
3144 static void
3145 frame_unhighlight (struct frame *f)
3147 /* We used to only do this if Vx_no_window_manager was non-nil, but
3148 the ICCCM (section 4.1.6) says that the window's border pixmap
3149 and border pixel are window attributes which are "private to the
3150 client", so we can always change it to whatever we want. */
3151 block_input ();
3152 /* Same as above for XSetWindowBorder (bug#9310). */
3153 x_catch_errors (FRAME_X_DISPLAY (f));
3154 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3155 f->output_data.x->border_tile);
3156 x_uncatch_errors ();
3157 unblock_input ();
3158 x_update_cursor (f, 1);
3159 x_set_frame_alpha (f);
3162 /* The focus has changed. Update the frames as necessary to reflect
3163 the new situation. Note that we can't change the selected frame
3164 here, because the Lisp code we are interrupting might become confused.
3165 Each event gets marked with the frame in which it occurred, so the
3166 Lisp code can tell when the switch took place by examining the events. */
3168 static void
3169 x_new_focus_frame (struct x_display_info *dpyinfo, struct frame *frame)
3171 struct frame *old_focus = dpyinfo->x_focus_frame;
3173 if (frame != dpyinfo->x_focus_frame)
3175 /* Set this before calling other routines, so that they see
3176 the correct value of x_focus_frame. */
3177 dpyinfo->x_focus_frame = frame;
3179 if (old_focus && old_focus->auto_lower)
3180 x_lower_frame (old_focus);
3182 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3183 dpyinfo->x_pending_autoraise_frame = dpyinfo->x_focus_frame;
3184 else
3185 dpyinfo->x_pending_autoraise_frame = NULL;
3188 x_frame_rehighlight (dpyinfo);
3191 /* Handle FocusIn and FocusOut state changes for FRAME.
3192 If FRAME has focus and there exists more than one frame, puts
3193 a FOCUS_IN_EVENT into *BUFP. */
3195 static void
3196 x_focus_changed (int type, int state, struct x_display_info *dpyinfo, struct frame *frame, struct input_event *bufp)
3198 if (type == FocusIn)
3200 if (dpyinfo->x_focus_event_frame != frame)
3202 x_new_focus_frame (dpyinfo, frame);
3203 dpyinfo->x_focus_event_frame = frame;
3205 /* Don't stop displaying the initial startup message
3206 for a switch-frame event we don't need. */
3207 /* When run as a daemon, Vterminal_frame is always NIL. */
3208 bufp->arg = (((NILP (Vterminal_frame) || EQ (Fdaemonp (), Qt))
3209 && CONSP (Vframe_list)
3210 && !NILP (XCDR (Vframe_list)))
3211 ? Qt : Qnil);
3212 bufp->kind = FOCUS_IN_EVENT;
3213 XSETFRAME (bufp->frame_or_window, frame);
3216 frame->output_data.x->focus_state |= state;
3218 #ifdef HAVE_X_I18N
3219 if (FRAME_XIC (frame))
3220 XSetICFocus (FRAME_XIC (frame));
3221 #endif
3223 else if (type == FocusOut)
3225 frame->output_data.x->focus_state &= ~state;
3227 if (dpyinfo->x_focus_event_frame == frame)
3229 dpyinfo->x_focus_event_frame = 0;
3230 x_new_focus_frame (dpyinfo, 0);
3232 bufp->kind = FOCUS_OUT_EVENT;
3233 XSETFRAME (bufp->frame_or_window, frame);
3236 #ifdef HAVE_X_I18N
3237 if (FRAME_XIC (frame))
3238 XUnsetICFocus (FRAME_XIC (frame));
3239 #endif
3240 if (frame->pointer_invisible)
3241 XTtoggle_invisible_pointer (frame, 0);
3245 /* Return the Emacs frame-object corresponding to an X window.
3246 It could be the frame's main window or an icon window. */
3248 static struct frame *
3249 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3251 Lisp_Object tail, frame;
3252 struct frame *f;
3254 if (wdesc == None)
3255 return NULL;
3257 FOR_EACH_FRAME (tail, frame)
3259 f = XFRAME (frame);
3260 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3261 continue;
3262 if (f->output_data.x->hourglass_window == wdesc)
3263 return f;
3264 #ifdef USE_X_TOOLKIT
3265 if ((f->output_data.x->edit_widget
3266 && XtWindow (f->output_data.x->edit_widget) == wdesc)
3267 /* A tooltip frame? */
3268 || (!f->output_data.x->edit_widget
3269 && FRAME_X_WINDOW (f) == wdesc)
3270 || f->output_data.x->icon_desc == wdesc)
3271 return f;
3272 #else /* not USE_X_TOOLKIT */
3273 #ifdef USE_GTK
3274 if (f->output_data.x->edit_widget)
3276 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3277 struct x_output *x = f->output_data.x;
3278 if (gwdesc != 0 && gwdesc == x->edit_widget)
3279 return f;
3281 #endif /* USE_GTK */
3282 if (FRAME_X_WINDOW (f) == wdesc
3283 || f->output_data.x->icon_desc == wdesc)
3284 return f;
3285 #endif /* not USE_X_TOOLKIT */
3287 return 0;
3290 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3292 /* Like x_window_to_frame but also compares the window with the widget's
3293 windows. */
3295 static struct frame *
3296 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3298 Lisp_Object tail, frame;
3299 struct frame *f, *found = NULL;
3300 struct x_output *x;
3302 if (wdesc == None)
3303 return NULL;
3305 FOR_EACH_FRAME (tail, frame)
3307 if (found)
3308 break;
3309 f = XFRAME (frame);
3310 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
3312 /* This frame matches if the window is any of its widgets. */
3313 x = f->output_data.x;
3314 if (x->hourglass_window == wdesc)
3315 found = f;
3316 else if (x->widget)
3318 #ifdef USE_GTK
3319 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3320 if (gwdesc != 0
3321 && gtk_widget_get_toplevel (gwdesc) == x->widget)
3322 found = f;
3323 #else
3324 if (wdesc == XtWindow (x->widget)
3325 || wdesc == XtWindow (x->column_widget)
3326 || wdesc == XtWindow (x->edit_widget))
3327 found = f;
3328 /* Match if the window is this frame's menubar. */
3329 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
3330 found = f;
3331 #endif
3333 else if (FRAME_X_WINDOW (f) == wdesc)
3334 /* A tooltip frame. */
3335 found = f;
3339 return found;
3342 /* Likewise, but consider only the menu bar widget. */
3344 static struct frame *
3345 x_menubar_window_to_frame (struct x_display_info *dpyinfo,
3346 const XEvent *event)
3348 Window wdesc = event->xany.window;
3349 Lisp_Object tail, frame;
3350 struct frame *f;
3351 struct x_output *x;
3353 if (wdesc == None)
3354 return NULL;
3356 FOR_EACH_FRAME (tail, frame)
3358 f = XFRAME (frame);
3359 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3360 continue;
3361 x = f->output_data.x;
3362 #ifdef USE_GTK
3363 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
3364 return f;
3365 #else
3366 /* Match if the window is this frame's menubar. */
3367 if (x->menubar_widget
3368 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
3369 return f;
3370 #endif
3372 return 0;
3375 /* Return the frame whose principal (outermost) window is WDESC.
3376 If WDESC is some other (smaller) window, we return 0. */
3378 struct frame *
3379 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3381 Lisp_Object tail, frame;
3382 struct frame *f;
3383 struct x_output *x;
3385 if (wdesc == None)
3386 return NULL;
3388 FOR_EACH_FRAME (tail, frame)
3390 f = XFRAME (frame);
3391 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3392 continue;
3393 x = f->output_data.x;
3395 if (x->widget)
3397 /* This frame matches if the window is its topmost widget. */
3398 #ifdef USE_GTK
3399 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3400 if (gwdesc == x->widget)
3401 return f;
3402 #else
3403 if (wdesc == XtWindow (x->widget))
3404 return f;
3405 #endif
3407 else if (FRAME_X_WINDOW (f) == wdesc)
3408 /* Tooltip frame. */
3409 return f;
3411 return 0;
3414 #else /* !USE_X_TOOLKIT && !USE_GTK */
3416 #define x_any_window_to_frame(d, i) x_window_to_frame (d, i)
3417 #define x_top_window_to_frame(d, i) x_window_to_frame (d, i)
3419 #endif /* USE_X_TOOLKIT || USE_GTK */
3421 /* The focus may have changed. Figure out if it is a real focus change,
3422 by checking both FocusIn/Out and Enter/LeaveNotify events.
3424 Returns FOCUS_IN_EVENT event in *BUFP. */
3426 static void
3427 x_detect_focus_change (struct x_display_info *dpyinfo, struct frame *frame,
3428 const XEvent *event, struct input_event *bufp)
3430 if (!frame)
3431 return;
3433 switch (event->type)
3435 case EnterNotify:
3436 case LeaveNotify:
3438 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3439 int focus_state
3440 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3442 if (event->xcrossing.detail != NotifyInferior
3443 && event->xcrossing.focus
3444 && ! (focus_state & FOCUS_EXPLICIT))
3445 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3446 FOCUS_IMPLICIT,
3447 dpyinfo, frame, bufp);
3449 break;
3451 case FocusIn:
3452 case FocusOut:
3453 x_focus_changed (event->type,
3454 (event->xfocus.detail == NotifyPointer ?
3455 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3456 dpyinfo, frame, bufp);
3457 break;
3459 case ClientMessage:
3460 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
3462 enum xembed_message msg = event->xclient.data.l[1];
3463 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
3464 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
3466 break;
3471 #if !defined USE_X_TOOLKIT && !defined USE_GTK
3472 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3474 void
3475 x_mouse_leave (struct x_display_info *dpyinfo)
3477 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3479 #endif
3481 /* The focus has changed, or we have redirected a frame's focus to
3482 another frame (this happens when a frame uses a surrogate
3483 mini-buffer frame). Shift the highlight as appropriate.
3485 The FRAME argument doesn't necessarily have anything to do with which
3486 frame is being highlighted or un-highlighted; we only use it to find
3487 the appropriate X display info. */
3489 static void
3490 XTframe_rehighlight (struct frame *frame)
3492 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
3495 static void
3496 x_frame_rehighlight (struct x_display_info *dpyinfo)
3498 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3500 if (dpyinfo->x_focus_frame)
3502 dpyinfo->x_highlight_frame
3503 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3504 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3505 : dpyinfo->x_focus_frame);
3506 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3508 fset_focus_frame (dpyinfo->x_focus_frame, Qnil);
3509 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3512 else
3513 dpyinfo->x_highlight_frame = 0;
3515 if (dpyinfo->x_highlight_frame != old_highlight)
3517 if (old_highlight)
3518 frame_unhighlight (old_highlight);
3519 if (dpyinfo->x_highlight_frame)
3520 frame_highlight (dpyinfo->x_highlight_frame);
3526 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3528 /* Initialize mode_switch_bit and modifier_meaning. */
3529 static void
3530 x_find_modifier_meanings (struct x_display_info *dpyinfo)
3532 int min_code, max_code;
3533 KeySym *syms;
3534 int syms_per_code;
3535 XModifierKeymap *mods;
3537 dpyinfo->meta_mod_mask = 0;
3538 dpyinfo->shift_lock_mask = 0;
3539 dpyinfo->alt_mod_mask = 0;
3540 dpyinfo->super_mod_mask = 0;
3541 dpyinfo->hyper_mod_mask = 0;
3543 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3545 syms = XGetKeyboardMapping (dpyinfo->display,
3546 min_code, max_code - min_code + 1,
3547 &syms_per_code);
3548 mods = XGetModifierMapping (dpyinfo->display);
3550 /* Scan the modifier table to see which modifier bits the Meta and
3551 Alt keysyms are on. */
3553 int row, col; /* The row and column in the modifier table. */
3554 int found_alt_or_meta;
3556 for (row = 3; row < 8; row++)
3558 found_alt_or_meta = 0;
3559 for (col = 0; col < mods->max_keypermod; col++)
3561 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3563 /* Zeroes are used for filler. Skip them. */
3564 if (code == 0)
3565 continue;
3567 /* Are any of this keycode's keysyms a meta key? */
3569 int code_col;
3571 for (code_col = 0; code_col < syms_per_code; code_col++)
3573 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3575 switch (sym)
3577 case XK_Meta_L:
3578 case XK_Meta_R:
3579 found_alt_or_meta = 1;
3580 dpyinfo->meta_mod_mask |= (1 << row);
3581 break;
3583 case XK_Alt_L:
3584 case XK_Alt_R:
3585 found_alt_or_meta = 1;
3586 dpyinfo->alt_mod_mask |= (1 << row);
3587 break;
3589 case XK_Hyper_L:
3590 case XK_Hyper_R:
3591 if (!found_alt_or_meta)
3592 dpyinfo->hyper_mod_mask |= (1 << row);
3593 code_col = syms_per_code;
3594 col = mods->max_keypermod;
3595 break;
3597 case XK_Super_L:
3598 case XK_Super_R:
3599 if (!found_alt_or_meta)
3600 dpyinfo->super_mod_mask |= (1 << row);
3601 code_col = syms_per_code;
3602 col = mods->max_keypermod;
3603 break;
3605 case XK_Shift_Lock:
3606 /* Ignore this if it's not on the lock modifier. */
3607 if (!found_alt_or_meta && ((1 << row) == LockMask))
3608 dpyinfo->shift_lock_mask = LockMask;
3609 code_col = syms_per_code;
3610 col = mods->max_keypermod;
3611 break;
3619 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3620 if (! dpyinfo->meta_mod_mask)
3622 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3623 dpyinfo->alt_mod_mask = 0;
3626 /* If some keys are both alt and meta,
3627 make them just meta, not alt. */
3628 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3630 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3633 XFree (syms);
3634 XFreeModifiermap (mods);
3637 /* Convert between the modifier bits X uses and the modifier bits
3638 Emacs uses. */
3641 x_x_to_emacs_modifiers (struct x_display_info *dpyinfo, int state)
3643 int mod_meta = meta_modifier;
3644 int mod_alt = alt_modifier;
3645 int mod_hyper = hyper_modifier;
3646 int mod_super = super_modifier;
3647 Lisp_Object tem;
3649 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3650 if (INTEGERP (tem)) mod_alt = XINT (tem) & INT_MAX;
3651 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3652 if (INTEGERP (tem)) mod_meta = XINT (tem) & INT_MAX;
3653 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3654 if (INTEGERP (tem)) mod_hyper = XINT (tem) & INT_MAX;
3655 tem = Fget (Vx_super_keysym, Qmodifier_value);
3656 if (INTEGERP (tem)) mod_super = XINT (tem) & INT_MAX;
3658 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3659 | ((state & ControlMask) ? ctrl_modifier : 0)
3660 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3661 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3662 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3663 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3666 static int
3667 x_emacs_to_x_modifiers (struct x_display_info *dpyinfo, EMACS_INT state)
3669 EMACS_INT mod_meta = meta_modifier;
3670 EMACS_INT mod_alt = alt_modifier;
3671 EMACS_INT mod_hyper = hyper_modifier;
3672 EMACS_INT mod_super = super_modifier;
3674 Lisp_Object tem;
3676 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3677 if (INTEGERP (tem)) mod_alt = XINT (tem);
3678 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3679 if (INTEGERP (tem)) mod_meta = XINT (tem);
3680 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3681 if (INTEGERP (tem)) mod_hyper = XINT (tem);
3682 tem = Fget (Vx_super_keysym, Qmodifier_value);
3683 if (INTEGERP (tem)) mod_super = XINT (tem);
3686 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3687 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3688 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3689 | ((state & shift_modifier) ? ShiftMask : 0)
3690 | ((state & ctrl_modifier) ? ControlMask : 0)
3691 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3694 /* Convert a keysym to its name. */
3696 char *
3697 x_get_keysym_name (int keysym)
3699 char *value;
3701 block_input ();
3702 value = XKeysymToString (keysym);
3703 unblock_input ();
3705 return value;
3708 /* Mouse clicks and mouse movement. Rah.
3710 Formerly, we used PointerMotionHintMask (in standard_event_mask)
3711 so that we would have to call XQueryPointer after each MotionNotify
3712 event to ask for another such event. However, this made mouse tracking
3713 slow, and there was a bug that made it eventually stop.
3715 Simply asking for MotionNotify all the time seems to work better.
3717 In order to avoid asking for motion events and then throwing most
3718 of them away or busy-polling the server for mouse positions, we ask
3719 the server for pointer motion hints. This means that we get only
3720 one event per group of mouse movements. "Groups" are delimited by
3721 other kinds of events (focus changes and button clicks, for
3722 example), or by XQueryPointer calls; when one of these happens, we
3723 get another MotionNotify event the next time the mouse moves. This
3724 is at least as efficient as getting motion events when mouse
3725 tracking is on, and I suspect only negligibly worse when tracking
3726 is off. */
3728 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3730 If the event is a button press, then note that we have grabbed
3731 the mouse. */
3733 static Lisp_Object
3734 construct_mouse_click (struct input_event *result,
3735 const XButtonEvent *event,
3736 struct frame *f)
3738 /* Make the event type NO_EVENT; we'll change that when we decide
3739 otherwise. */
3740 result->kind = MOUSE_CLICK_EVENT;
3741 result->code = event->button - Button1;
3742 result->timestamp = event->time;
3743 result->modifiers = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f),
3744 event->state)
3745 | (event->type == ButtonRelease
3746 ? up_modifier
3747 : down_modifier));
3749 XSETINT (result->x, event->x);
3750 XSETINT (result->y, event->y);
3751 XSETFRAME (result->frame_or_window, f);
3752 result->arg = Qnil;
3753 return Qnil;
3756 /* Function to report a mouse movement to the mainstream Emacs code.
3757 The input handler calls this.
3759 We have received a mouse movement event, which is given in *event.
3760 If the mouse is over a different glyph than it was last time, tell
3761 the mainstream emacs code by setting mouse_moved. If not, ask for
3762 another motion event, so we can check again the next time it moves. */
3764 static int
3765 note_mouse_movement (struct frame *frame, const XMotionEvent *event)
3767 XRectangle *r;
3768 struct x_display_info *dpyinfo;
3770 if (!FRAME_X_OUTPUT (frame))
3771 return 0;
3773 dpyinfo = FRAME_DISPLAY_INFO (frame);
3774 dpyinfo->last_mouse_movement_time = event->time;
3775 dpyinfo->last_mouse_motion_frame = frame;
3776 dpyinfo->last_mouse_motion_x = event->x;
3777 dpyinfo->last_mouse_motion_y = event->y;
3779 if (event->window != FRAME_X_WINDOW (frame))
3781 frame->mouse_moved = 1;
3782 dpyinfo->last_mouse_scroll_bar = NULL;
3783 note_mouse_highlight (frame, -1, -1);
3784 dpyinfo->last_mouse_glyph_frame = NULL;
3785 return 1;
3789 /* Has the mouse moved off the glyph it was on at the last sighting? */
3790 r = &dpyinfo->last_mouse_glyph;
3791 if (frame != dpyinfo->last_mouse_glyph_frame
3792 || event->x < r->x || event->x >= r->x + r->width
3793 || event->y < r->y || event->y >= r->y + r->height)
3795 frame->mouse_moved = 1;
3796 dpyinfo->last_mouse_scroll_bar = NULL;
3797 note_mouse_highlight (frame, event->x, event->y);
3798 /* Remember which glyph we're now on. */
3799 remember_mouse_glyph (frame, event->x, event->y, r);
3800 dpyinfo->last_mouse_glyph_frame = frame;
3801 return 1;
3804 return 0;
3807 /* Return the current position of the mouse.
3808 *FP should be a frame which indicates which display to ask about.
3810 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3811 and *PART to the frame, window, and scroll bar part that the mouse
3812 is over. Set *X and *Y to the portion and whole of the mouse's
3813 position on the scroll bar.
3815 If the mouse movement started elsewhere, set *FP to the frame the
3816 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3817 the mouse is over.
3819 Set *TIMESTAMP to the server time-stamp for the time at which the mouse
3820 was at this position.
3822 Don't store anything if we don't have a valid set of values to report.
3824 This clears the mouse_moved flag, so we can wait for the next mouse
3825 movement. */
3827 static void
3828 XTmouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
3829 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
3830 Time *timestamp)
3832 struct frame *f1;
3833 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
3835 block_input ();
3837 if (dpyinfo->last_mouse_scroll_bar && insist == 0)
3838 x_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
3839 else
3841 Window root;
3842 int root_x, root_y;
3844 Window dummy_window;
3845 int dummy;
3847 Lisp_Object frame, tail;
3849 /* Clear the mouse-moved flag for every frame on this display. */
3850 FOR_EACH_FRAME (tail, frame)
3851 if (FRAME_X_P (XFRAME (frame))
3852 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3853 XFRAME (frame)->mouse_moved = 0;
3855 dpyinfo->last_mouse_scroll_bar = NULL;
3857 /* Figure out which root window we're on. */
3858 XQueryPointer (FRAME_X_DISPLAY (*fp),
3859 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3861 /* The root window which contains the pointer. */
3862 &root,
3864 /* Trash which we can't trust if the pointer is on
3865 a different screen. */
3866 &dummy_window,
3868 /* The position on that root window. */
3869 &root_x, &root_y,
3871 /* More trash we can't trust. */
3872 &dummy, &dummy,
3874 /* Modifier keys and pointer buttons, about which
3875 we don't care. */
3876 (unsigned int *) &dummy);
3878 /* Now we have a position on the root; find the innermost window
3879 containing the pointer. */
3881 Window win, child;
3882 int win_x, win_y;
3883 int parent_x = 0, parent_y = 0;
3885 win = root;
3887 /* XTranslateCoordinates can get errors if the window
3888 structure is changing at the same time this function
3889 is running. So at least we must not crash from them. */
3891 x_catch_errors (FRAME_X_DISPLAY (*fp));
3893 if (x_mouse_grabbed (dpyinfo))
3895 /* If mouse was grabbed on a frame, give coords for that frame
3896 even if the mouse is now outside it. */
3897 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3899 /* From-window. */
3900 root,
3902 /* To-window. */
3903 FRAME_X_WINDOW (dpyinfo->last_mouse_frame),
3905 /* From-position, to-position. */
3906 root_x, root_y, &win_x, &win_y,
3908 /* Child of win. */
3909 &child);
3910 f1 = dpyinfo->last_mouse_frame;
3912 else
3914 while (1)
3916 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3918 /* From-window, to-window. */
3919 root, win,
3921 /* From-position, to-position. */
3922 root_x, root_y, &win_x, &win_y,
3924 /* Child of win. */
3925 &child);
3927 if (child == None || child == win)
3928 break;
3929 #ifdef USE_GTK
3930 /* We don't wan't to know the innermost window. We
3931 want the edit window. For non-Gtk+ the innermost
3932 window is the edit window. For Gtk+ it might not
3933 be. It might be the tool bar for example. */
3934 if (x_window_to_frame (dpyinfo, win))
3935 break;
3936 #endif
3937 win = child;
3938 parent_x = win_x;
3939 parent_y = win_y;
3942 /* Now we know that:
3943 win is the innermost window containing the pointer
3944 (XTC says it has no child containing the pointer),
3945 win_x and win_y are the pointer's position in it
3946 (XTC did this the last time through), and
3947 parent_x and parent_y are the pointer's position in win's parent.
3948 (They are what win_x and win_y were when win was child.
3949 If win is the root window, it has no parent, and
3950 parent_{x,y} are invalid, but that's okay, because we'll
3951 never use them in that case.) */
3953 #ifdef USE_GTK
3954 /* We don't wan't to know the innermost window. We
3955 want the edit window. */
3956 f1 = x_window_to_frame (dpyinfo, win);
3957 #else
3958 /* Is win one of our frames? */
3959 f1 = x_any_window_to_frame (dpyinfo, win);
3960 #endif
3962 #ifdef USE_X_TOOLKIT
3963 /* If we end up with the menu bar window, say it's not
3964 on the frame. */
3965 if (f1 != NULL
3966 && f1->output_data.x->menubar_widget
3967 && win == XtWindow (f1->output_data.x->menubar_widget))
3968 f1 = NULL;
3969 #endif /* USE_X_TOOLKIT */
3972 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3973 f1 = 0;
3975 x_uncatch_errors ();
3977 /* If not, is it one of our scroll bars? */
3978 if (! f1)
3980 struct scroll_bar *bar;
3982 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
3984 if (bar)
3986 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3987 win_x = parent_x;
3988 win_y = parent_y;
3992 if (f1 == 0 && insist > 0)
3993 f1 = SELECTED_FRAME ();
3995 if (f1)
3997 /* Ok, we found a frame. Store all the values.
3998 last_mouse_glyph is a rectangle used to reduce the
3999 generation of mouse events. To not miss any motion
4000 events, we must divide the frame into rectangles of the
4001 size of the smallest character that could be displayed
4002 on it, i.e. into the same rectangles that matrices on
4003 the frame are divided into. */
4005 /* FIXME: what if F1 is not an X frame? */
4006 dpyinfo = FRAME_DISPLAY_INFO (f1);
4007 remember_mouse_glyph (f1, win_x, win_y, &dpyinfo->last_mouse_glyph);
4008 dpyinfo->last_mouse_glyph_frame = f1;
4010 *bar_window = Qnil;
4011 *part = 0;
4012 *fp = f1;
4013 XSETINT (*x, win_x);
4014 XSETINT (*y, win_y);
4015 *timestamp = dpyinfo->last_mouse_movement_time;
4020 unblock_input ();
4025 /***********************************************************************
4026 Scroll bars
4027 ***********************************************************************/
4029 /* Scroll bar support. */
4031 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
4032 manages it.
4033 This can be called in GC, so we have to make sure to strip off mark
4034 bits. */
4036 static struct scroll_bar *
4037 x_window_to_scroll_bar (Display *display, Window window_id)
4039 Lisp_Object tail, frame;
4041 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4042 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4043 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4045 FOR_EACH_FRAME (tail, frame)
4047 Lisp_Object bar, condemned;
4049 if (! FRAME_X_P (XFRAME (frame)))
4050 continue;
4052 /* Scan this frame's scroll bar list for a scroll bar with the
4053 right window ID. */
4054 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4055 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4056 /* This trick allows us to search both the ordinary and
4057 condemned scroll bar lists with one loop. */
4058 ! NILP (bar) || (bar = condemned,
4059 condemned = Qnil,
4060 ! NILP (bar));
4061 bar = XSCROLL_BAR (bar)->next)
4062 if (XSCROLL_BAR (bar)->x_window == window_id &&
4063 FRAME_X_DISPLAY (XFRAME (frame)) == display)
4064 return XSCROLL_BAR (bar);
4067 return NULL;
4071 #if defined USE_LUCID
4073 /* Return the Lucid menu bar WINDOW is part of. Return null
4074 if WINDOW is not part of a menu bar. */
4076 static Widget
4077 x_window_to_menu_bar (Window window)
4079 Lisp_Object tail, frame;
4081 FOR_EACH_FRAME (tail, frame)
4082 if (FRAME_X_P (XFRAME (frame)))
4084 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4086 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4087 return menu_bar;
4089 return NULL;
4092 #endif /* USE_LUCID */
4095 /************************************************************************
4096 Toolkit scroll bars
4097 ************************************************************************/
4099 #ifdef USE_TOOLKIT_SCROLL_BARS
4101 static void x_send_scroll_bar_event (Lisp_Object, int, int, int);
4103 /* Lisp window being scrolled. Set when starting to interact with
4104 a toolkit scroll bar, reset to nil when ending the interaction. */
4106 static Lisp_Object window_being_scrolled;
4108 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4109 that movements of 1/20 of the screen size are mapped to up/down. */
4111 #ifndef USE_GTK
4112 /* Id of action hook installed for scroll bars. */
4114 static XtActionHookId action_hook_id;
4116 static Boolean xaw3d_arrow_scroll;
4118 /* Whether the drag scrolling maintains the mouse at the top of the
4119 thumb. If not, resizing the thumb needs to be done more carefully
4120 to avoid jerkiness. */
4122 static Boolean xaw3d_pick_top;
4124 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4125 bars are used.. The hook is responsible for detecting when
4126 the user ends an interaction with the scroll bar, and generates
4127 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4129 static void
4130 xt_action_hook (Widget widget, XtPointer client_data, String action_name,
4131 XEvent *event, String *params, Cardinal *num_params)
4133 int scroll_bar_p;
4134 const char *end_action;
4136 #ifdef USE_MOTIF
4137 scroll_bar_p = XmIsScrollBar (widget);
4138 end_action = "Release";
4139 #else /* !USE_MOTIF i.e. use Xaw */
4140 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4141 end_action = "EndScroll";
4142 #endif /* USE_MOTIF */
4144 if (scroll_bar_p
4145 && strcmp (action_name, end_action) == 0
4146 && WINDOWP (window_being_scrolled))
4148 struct window *w;
4149 struct scroll_bar *bar;
4151 x_send_scroll_bar_event (window_being_scrolled,
4152 scroll_bar_end_scroll, 0, 0);
4153 w = XWINDOW (window_being_scrolled);
4154 bar = XSCROLL_BAR (w->vertical_scroll_bar);
4156 if (bar->dragging != -1)
4158 bar->dragging = -1;
4159 /* The thumb size is incorrect while dragging: fix it. */
4160 set_vertical_scroll_bar (w);
4162 window_being_scrolled = Qnil;
4163 #if defined (USE_LUCID)
4164 bar->last_seen_part = scroll_bar_nowhere;
4165 #endif
4166 /* Xt timeouts no longer needed. */
4167 toolkit_scroll_bar_interaction = 0;
4170 #endif /* not USE_GTK */
4172 /* Send a client message with message type Xatom_Scrollbar for a
4173 scroll action to the frame of WINDOW. PART is a value identifying
4174 the part of the scroll bar that was clicked on. PORTION is the
4175 amount to scroll of a whole of WHOLE. */
4177 static void
4178 x_send_scroll_bar_event (Lisp_Object window, int part, int portion, int whole)
4180 XEvent event;
4181 XClientMessageEvent *ev = &event.xclient;
4182 struct window *w = XWINDOW (window);
4183 struct frame *f = XFRAME (w->frame);
4184 intptr_t iw = (intptr_t) w;
4185 enum { BITS_PER_INTPTR = CHAR_BIT * sizeof iw };
4186 verify (BITS_PER_INTPTR <= 64);
4187 int sign_shift = BITS_PER_INTPTR - 32;
4189 block_input ();
4191 /* Construct a ClientMessage event to send to the frame. */
4192 ev->type = ClientMessage;
4193 ev->message_type = FRAME_DISPLAY_INFO (f)->Xatom_Scrollbar;
4194 ev->display = FRAME_X_DISPLAY (f);
4195 ev->window = FRAME_X_WINDOW (f);
4196 ev->format = 32;
4198 /* A 32-bit X client on a 64-bit X server can pass a window pointer
4199 as-is. A 64-bit client on a 32-bit X server is in trouble
4200 because a pointer does not fit and would be truncated while
4201 passing through the server. So use two slots and hope that X12
4202 will resolve such issues someday. */
4203 ev->data.l[0] = iw >> 31 >> 1;
4204 ev->data.l[1] = sign_shift <= 0 ? iw : iw << sign_shift >> sign_shift;
4205 ev->data.l[2] = part;
4206 ev->data.l[3] = portion;
4207 ev->data.l[4] = whole;
4209 /* Make Xt timeouts work while the scroll bar is active. */
4210 #ifdef USE_X_TOOLKIT
4211 toolkit_scroll_bar_interaction = 1;
4212 x_activate_timeout_atimer ();
4213 #endif
4215 /* Setting the event mask to zero means that the message will
4216 be sent to the client that created the window, and if that
4217 window no longer exists, no event will be sent. */
4218 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4219 unblock_input ();
4223 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4224 in *IEVENT. */
4226 static void
4227 x_scroll_bar_to_input_event (const XEvent *event,
4228 struct input_event *ievent)
4230 const XClientMessageEvent *ev = &event->xclient;
4231 Lisp_Object window;
4232 struct window *w;
4234 /* See the comment in the function above. */
4235 intptr_t iw0 = ev->data.l[0];
4236 intptr_t iw1 = ev->data.l[1];
4237 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
4238 w = (struct window *) iw;
4240 XSETWINDOW (window, w);
4242 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4243 ievent->frame_or_window = window;
4244 ievent->arg = Qnil;
4245 #ifdef USE_GTK
4246 ievent->timestamp = CurrentTime;
4247 #else
4248 ievent->timestamp =
4249 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
4250 #endif
4251 ievent->code = 0;
4252 ievent->part = ev->data.l[2];
4253 ievent->x = make_number (ev->data.l[3]);
4254 ievent->y = make_number (ev->data.l[4]);
4255 ievent->modifiers = 0;
4259 #ifdef USE_MOTIF
4261 /* Minimum and maximum values used for Motif scroll bars. */
4263 #define XM_SB_MAX 10000000
4266 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4267 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4268 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4270 static void
4271 xm_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4273 struct scroll_bar *bar = client_data;
4274 XmScrollBarCallbackStruct *cs = call_data;
4275 int part = -1, whole = 0, portion = 0;
4277 switch (cs->reason)
4279 case XmCR_DECREMENT:
4280 bar->dragging = -1;
4281 part = scroll_bar_up_arrow;
4282 break;
4284 case XmCR_INCREMENT:
4285 bar->dragging = -1;
4286 part = scroll_bar_down_arrow;
4287 break;
4289 case XmCR_PAGE_DECREMENT:
4290 bar->dragging = -1;
4291 part = scroll_bar_above_handle;
4292 break;
4294 case XmCR_PAGE_INCREMENT:
4295 bar->dragging = -1;
4296 part = scroll_bar_below_handle;
4297 break;
4299 case XmCR_TO_TOP:
4300 bar->dragging = -1;
4301 part = scroll_bar_to_top;
4302 break;
4304 case XmCR_TO_BOTTOM:
4305 bar->dragging = -1;
4306 part = scroll_bar_to_bottom;
4307 break;
4309 case XmCR_DRAG:
4311 int slider_size;
4313 /* Get the slider size. */
4314 block_input ();
4315 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4316 unblock_input ();
4318 whole = XM_SB_MAX - slider_size;
4319 portion = min (cs->value, whole);
4320 part = scroll_bar_handle;
4321 bar->dragging = cs->value;
4323 break;
4325 case XmCR_VALUE_CHANGED:
4326 break;
4329 if (part >= 0)
4331 window_being_scrolled = bar->window;
4332 x_send_scroll_bar_event (bar->window, part, portion, whole);
4336 #elif defined USE_GTK
4338 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4339 bar widget. DATA is a pointer to the scroll_bar structure. */
4341 static gboolean
4342 xg_scroll_callback (GtkRange *range,
4343 GtkScrollType scroll,
4344 gdouble value,
4345 gpointer user_data)
4347 struct scroll_bar *bar = user_data;
4348 gdouble position;
4349 int part = -1, whole = 0, portion = 0;
4350 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
4351 struct frame *f = g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
4353 if (xg_ignore_gtk_scrollbar) return FALSE;
4354 position = gtk_adjustment_get_value (adj);
4357 switch (scroll)
4359 case GTK_SCROLL_JUMP:
4360 /* Buttons 1 2 or 3 must be grabbed. */
4361 if (FRAME_DISPLAY_INFO (f)->grabbed != 0
4362 && FRAME_DISPLAY_INFO (f)->grabbed < (1 << 4))
4364 part = scroll_bar_handle;
4365 whole = gtk_adjustment_get_upper (adj) -
4366 gtk_adjustment_get_page_size (adj);
4367 portion = min ((int)position, whole);
4368 bar->dragging = portion;
4370 break;
4371 case GTK_SCROLL_STEP_BACKWARD:
4372 part = scroll_bar_up_arrow;
4373 bar->dragging = -1;
4374 break;
4375 case GTK_SCROLL_STEP_FORWARD:
4376 part = scroll_bar_down_arrow;
4377 bar->dragging = -1;
4378 break;
4379 case GTK_SCROLL_PAGE_BACKWARD:
4380 part = scroll_bar_above_handle;
4381 bar->dragging = -1;
4382 break;
4383 case GTK_SCROLL_PAGE_FORWARD:
4384 part = scroll_bar_below_handle;
4385 bar->dragging = -1;
4386 break;
4389 if (part >= 0)
4391 window_being_scrolled = bar->window;
4392 x_send_scroll_bar_event (bar->window, part, portion, whole);
4395 return FALSE;
4398 /* Callback for button release. Sets dragging to -1 when dragging is done. */
4400 static gboolean
4401 xg_end_scroll_callback (GtkWidget *widget,
4402 GdkEventButton *event,
4403 gpointer user_data)
4405 struct scroll_bar *bar = user_data;
4406 bar->dragging = -1;
4407 if (WINDOWP (window_being_scrolled))
4409 x_send_scroll_bar_event (window_being_scrolled,
4410 scroll_bar_end_scroll, 0, 0);
4411 window_being_scrolled = Qnil;
4414 return FALSE;
4418 #else /* not USE_GTK and not USE_MOTIF */
4420 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4421 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4422 scroll bar struct. CALL_DATA is a pointer to a float saying where
4423 the thumb is. */
4425 static void
4426 xaw_jump_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4428 struct scroll_bar *bar = client_data;
4429 float *top_addr = call_data;
4430 float top = *top_addr;
4431 float shown;
4432 int whole, portion, height;
4433 enum scroll_bar_part part;
4435 /* Get the size of the thumb, a value between 0 and 1. */
4436 block_input ();
4437 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4438 unblock_input ();
4440 whole = 10000000;
4441 portion = shown < 1 ? top * whole : 0;
4443 if (shown < 1 && (eabs (top + shown - 1) < 1.0f / height))
4444 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4445 the bottom, so we force the scrolling whenever we see that we're
4446 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4447 we try to ensure that we always stay two pixels away from the
4448 bottom). */
4449 part = scroll_bar_down_arrow;
4450 else
4451 part = scroll_bar_handle;
4453 window_being_scrolled = bar->window;
4454 bar->dragging = portion;
4455 bar->last_seen_part = part;
4456 x_send_scroll_bar_event (bar->window, part, portion, whole);
4460 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4461 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4462 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4463 the scroll bar. CALL_DATA is an integer specifying the action that
4464 has taken place. Its magnitude is in the range 0..height of the
4465 scroll bar. Negative values mean scroll towards buffer start.
4466 Values < height of scroll bar mean line-wise movement. */
4468 static void
4469 xaw_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4471 struct scroll_bar *bar = client_data;
4472 /* The position really is stored cast to a pointer. */
4473 int position = (intptr_t) call_data;
4474 Dimension height;
4475 enum scroll_bar_part part;
4477 /* Get the height of the scroll bar. */
4478 block_input ();
4479 XtVaGetValues (widget, XtNheight, &height, NULL);
4480 unblock_input ();
4482 if (eabs (position) >= height)
4483 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4485 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4486 it maps line-movement to call_data = max(5, height/20). */
4487 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
4488 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4489 else
4490 part = scroll_bar_move_ratio;
4492 window_being_scrolled = bar->window;
4493 bar->dragging = -1;
4494 bar->last_seen_part = part;
4495 x_send_scroll_bar_event (bar->window, part, position, height);
4498 #endif /* not USE_GTK and not USE_MOTIF */
4500 #define SCROLL_BAR_NAME "verticalScrollBar"
4502 /* Create the widget for scroll bar BAR on frame F. Record the widget
4503 and X window of the scroll bar in BAR. */
4505 #ifdef USE_GTK
4506 static void
4507 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
4509 const char *scroll_bar_name = SCROLL_BAR_NAME;
4511 block_input ();
4512 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4513 G_CALLBACK (xg_end_scroll_callback),
4514 scroll_bar_name);
4515 unblock_input ();
4518 #else /* not USE_GTK */
4520 static void
4521 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
4523 Window xwindow;
4524 Widget widget;
4525 Arg av[20];
4526 int ac = 0;
4527 const char *scroll_bar_name = SCROLL_BAR_NAME;
4528 unsigned long pixel;
4530 block_input ();
4532 #ifdef USE_MOTIF
4533 /* Set resources. Create the widget. */
4534 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4535 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4536 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4537 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4538 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4539 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4540 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4542 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4543 if (pixel != -1)
4545 XtSetArg (av[ac], XmNforeground, pixel);
4546 ++ac;
4549 pixel = f->output_data.x->scroll_bar_background_pixel;
4550 if (pixel != -1)
4552 XtSetArg (av[ac], XmNbackground, pixel);
4553 ++ac;
4556 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4557 (char *) scroll_bar_name, av, ac);
4559 /* Add one callback for everything that can happen. */
4560 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4561 (XtPointer) bar);
4562 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4563 (XtPointer) bar);
4564 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4565 (XtPointer) bar);
4566 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4567 (XtPointer) bar);
4568 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4569 (XtPointer) bar);
4570 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4571 (XtPointer) bar);
4572 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4573 (XtPointer) bar);
4575 /* Realize the widget. Only after that is the X window created. */
4576 XtRealizeWidget (widget);
4578 /* Set the cursor to an arrow. I didn't find a resource to do that.
4579 And I'm wondering why it hasn't an arrow cursor by default. */
4580 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4581 f->output_data.x->nontext_cursor);
4583 #else /* !USE_MOTIF i.e. use Xaw */
4585 /* Set resources. Create the widget. The background of the
4586 Xaw3d scroll bar widget is a little bit light for my taste.
4587 We don't alter it here to let users change it according
4588 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4589 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4590 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4591 /* For smoother scrolling with Xaw3d -sm */
4592 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4594 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4595 if (pixel != -1)
4597 XtSetArg (av[ac], XtNforeground, pixel);
4598 ++ac;
4601 pixel = f->output_data.x->scroll_bar_background_pixel;
4602 if (pixel != -1)
4604 XtSetArg (av[ac], XtNbackground, pixel);
4605 ++ac;
4608 /* Top/bottom shadow colors. */
4610 /* Allocate them, if necessary. */
4611 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4613 pixel = f->output_data.x->scroll_bar_background_pixel;
4614 if (pixel != -1)
4616 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4617 FRAME_X_COLORMAP (f),
4618 &pixel, 1.2, 0x8000))
4619 pixel = -1;
4620 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4623 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4625 pixel = f->output_data.x->scroll_bar_background_pixel;
4626 if (pixel != -1)
4628 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4629 FRAME_X_COLORMAP (f),
4630 &pixel, 0.6, 0x4000))
4631 pixel = -1;
4632 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4636 #ifdef XtNbeNiceToColormap
4637 /* Tell the toolkit about them. */
4638 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4639 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4640 /* We tried to allocate a color for the top/bottom shadow, and
4641 failed, so tell Xaw3d to use dithering instead. */
4642 /* But only if we have a small colormap. Xaw3d can allocate nice
4643 colors itself. */
4645 XtSetArg (av[ac], XtNbeNiceToColormap,
4646 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
4647 ++ac;
4649 else
4650 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4651 be more consistent with other emacs 3d colors, and since Xaw3d is
4652 not good at dealing with allocation failure. */
4654 /* This tells Xaw3d to use real colors instead of dithering for
4655 the shadows. */
4656 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4657 ++ac;
4659 /* Specify the colors. */
4660 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4661 if (pixel != -1)
4663 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4664 ++ac;
4666 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4667 if (pixel != -1)
4669 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4670 ++ac;
4673 #endif
4675 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4676 f->output_data.x->edit_widget, av, ac);
4679 char const *initial = "";
4680 char const *val = initial;
4681 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4682 #ifdef XtNarrowScrollbars
4683 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4684 #endif
4685 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4686 if (xaw3d_arrow_scroll || val == initial)
4687 { /* ARROW_SCROLL */
4688 xaw3d_arrow_scroll = True;
4689 /* Isn't that just a personal preference ? --Stef */
4690 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4694 /* Define callbacks. */
4695 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4696 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4697 (XtPointer) bar);
4699 /* Realize the widget. Only after that is the X window created. */
4700 XtRealizeWidget (widget);
4702 #endif /* !USE_MOTIF */
4704 /* Install an action hook that lets us detect when the user
4705 finishes interacting with a scroll bar. */
4706 if (action_hook_id == 0)
4707 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4709 /* Remember X window and widget in the scroll bar vector. */
4710 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4711 xwindow = XtWindow (widget);
4712 bar->x_window = xwindow;
4714 unblock_input ();
4716 #endif /* not USE_GTK */
4719 /* Set the thumb size and position of scroll bar BAR. We are currently
4720 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4722 #ifdef USE_GTK
4723 static void
4724 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
4726 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4729 #else /* not USE_GTK */
4730 static void
4731 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
4732 int whole)
4734 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4735 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4736 float top, shown;
4738 block_input ();
4740 #ifdef USE_MOTIF
4742 if (scroll_bar_adjust_thumb_portion_p)
4744 /* We use an estimate of 30 chars per line rather than the real
4745 `portion' value. This has the disadvantage that the thumb size
4746 is not very representative, but it makes our life a lot easier.
4747 Otherwise, we have to constantly adjust the thumb size, which
4748 we can't always do quickly enough: while dragging, the size of
4749 the thumb might prevent the user from dragging the thumb all the
4750 way to the end. but Motif and some versions of Xaw3d don't allow
4751 updating the thumb size while dragging. Also, even if we can update
4752 its size, the update will often happen too late.
4753 If you don't believe it, check out revision 1.650 of xterm.c to see
4754 what hoops we were going through and the still poor behavior we got. */
4755 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4756 /* When the thumb is at the bottom, position == whole.
4757 So we need to increase `whole' to make space for the thumb. */
4758 whole += portion;
4761 if (whole <= 0)
4762 top = 0, shown = 1;
4763 else
4765 top = (float) position / whole;
4766 shown = (float) portion / whole;
4769 if (bar->dragging == -1)
4771 int size, value;
4773 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4774 is the scroll bar's maximum and MIN is the scroll bar's minimum
4775 value. */
4776 size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
4778 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4779 value = top * XM_SB_MAX;
4780 value = min (value, XM_SB_MAX - size);
4782 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4784 #else /* !USE_MOTIF i.e. use Xaw */
4786 if (whole == 0)
4787 top = 0, shown = 1;
4788 else
4790 top = (float) position / whole;
4791 shown = (float) portion / whole;
4795 float old_top, old_shown;
4796 Dimension height;
4797 XtVaGetValues (widget,
4798 XtNtopOfThumb, &old_top,
4799 XtNshown, &old_shown,
4800 XtNheight, &height,
4801 NULL);
4803 /* Massage the top+shown values. */
4804 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
4805 top = max (0, min (1, top));
4806 else
4807 top = old_top;
4808 #if ! defined (HAVE_XAW3D)
4809 /* With Xaw, 'top' values too closer to 1.0 may
4810 cause the thumb to disappear. Fix that. */
4811 top = min (top, 0.99f);
4812 #endif
4813 /* Keep two pixels available for moving the thumb down. */
4814 shown = max (0, min (1 - top - (2.0f / height), shown));
4815 #if ! defined (HAVE_XAW3D)
4816 /* Likewise with too small 'shown'. */
4817 shown = max (shown, 0.01f);
4818 #endif
4820 /* If the call to XawScrollbarSetThumb below doesn't seem to
4821 work, check that 'NARROWPROTO' is defined in src/config.h.
4822 If this is not so, most likely you need to fix configure. */
4823 if (top != old_top || shown != old_shown)
4825 if (bar->dragging == -1)
4826 XawScrollbarSetThumb (widget, top, shown);
4827 else
4829 /* Try to make the scrolling a tad smoother. */
4830 if (!xaw3d_pick_top)
4831 shown = min (shown, old_shown);
4833 XawScrollbarSetThumb (widget, top, shown);
4837 #endif /* !USE_MOTIF */
4839 unblock_input ();
4841 #endif /* not USE_GTK */
4843 #endif /* USE_TOOLKIT_SCROLL_BARS */
4847 /************************************************************************
4848 Scroll bars, general
4849 ************************************************************************/
4851 /* Create a scroll bar and return the scroll bar vector for it. W is
4852 the Emacs window on which to create the scroll bar. TOP, LEFT,
4853 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4854 scroll bar. */
4856 static struct scroll_bar *
4857 x_scroll_bar_create (struct window *w, int top, int left, int width, int height)
4859 struct frame *f = XFRAME (w->frame);
4860 struct scroll_bar *bar
4861 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
4862 Lisp_Object barobj;
4864 block_input ();
4866 #ifdef USE_TOOLKIT_SCROLL_BARS
4867 x_create_toolkit_scroll_bar (f, bar);
4868 #else /* not USE_TOOLKIT_SCROLL_BARS */
4870 XSetWindowAttributes a;
4871 unsigned long mask;
4872 Window window;
4874 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4875 if (a.background_pixel == -1)
4876 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4878 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4879 | ButtonMotionMask | PointerMotionHintMask
4880 | ExposureMask);
4881 a.cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4883 mask = (CWBackPixel | CWEventMask | CWCursor);
4885 /* Clear the area of W that will serve as a scroll bar. This is
4886 for the case that a window has been split horizontally. In
4887 this case, no clear_frame is generated to reduce flickering. */
4888 if (width > 0 && window_box_height (w) > 0)
4889 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4890 left, top, width, window_box_height (w));
4892 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4893 /* Position and size of scroll bar. */
4894 left, top, width, height,
4895 /* Border width, depth, class, and visual. */
4897 CopyFromParent,
4898 CopyFromParent,
4899 CopyFromParent,
4900 /* Attributes. */
4901 mask, &a);
4902 bar->x_window = window;
4904 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4906 XSETWINDOW (bar->window, w);
4907 bar->top = top;
4908 bar->left = left;
4909 bar->width = width;
4910 bar->height = height;
4911 bar->start = 0;
4912 bar->end = 0;
4913 bar->dragging = -1;
4914 #if defined (USE_TOOLKIT_SCROLL_BARS) && defined (USE_LUCID)
4915 bar->last_seen_part = scroll_bar_nowhere;
4916 #endif
4918 /* Add bar to its frame's list of scroll bars. */
4919 bar->next = FRAME_SCROLL_BARS (f);
4920 bar->prev = Qnil;
4921 XSETVECTOR (barobj, bar);
4922 fset_scroll_bars (f, barobj);
4923 if (!NILP (bar->next))
4924 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4926 /* Map the window/widget. */
4927 #ifdef USE_TOOLKIT_SCROLL_BARS
4929 #ifdef USE_GTK
4930 xg_update_scrollbar_pos (f, bar->x_window, top,
4931 left,width, max (height, 1));
4932 #else /* not USE_GTK */
4933 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4934 XtConfigureWidget (scroll_bar, left, top, width, max (height, 1), 0);
4935 XtMapWidget (scroll_bar);
4936 #endif /* not USE_GTK */
4938 #else /* not USE_TOOLKIT_SCROLL_BARS */
4939 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
4940 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4942 unblock_input ();
4943 return bar;
4947 #ifndef USE_TOOLKIT_SCROLL_BARS
4949 /* Draw BAR's handle in the proper position.
4951 If the handle is already drawn from START to END, don't bother
4952 redrawing it, unless REBUILD is non-zero; in that case, always
4953 redraw it. (REBUILD is handy for drawing the handle after expose
4954 events.)
4956 Normally, we want to constrain the start and end of the handle to
4957 fit inside its rectangle, but if the user is dragging the scroll
4958 bar handle, we want to let them drag it down all the way, so that
4959 the bar's top is as far down as it goes; otherwise, there's no way
4960 to move to the very end of the buffer. */
4962 static void
4963 x_scroll_bar_set_handle (struct scroll_bar *bar, int start, int end, int rebuild)
4965 bool dragging = bar->dragging != -1;
4966 Window w = bar->x_window;
4967 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4968 GC gc = f->output_data.x->normal_gc;
4970 /* If the display is already accurate, do nothing. */
4971 if (! rebuild
4972 && start == bar->start
4973 && end == bar->end)
4974 return;
4976 block_input ();
4979 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
4980 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
4981 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
4983 /* Make sure the values are reasonable, and try to preserve
4984 the distance between start and end. */
4986 int length = end - start;
4988 if (start < 0)
4989 start = 0;
4990 else if (start > top_range)
4991 start = top_range;
4992 end = start + length;
4994 if (end < start)
4995 end = start;
4996 else if (end > top_range && ! dragging)
4997 end = top_range;
5000 /* Store the adjusted setting in the scroll bar. */
5001 bar->start = start;
5002 bar->end = end;
5004 /* Clip the end position, just for display. */
5005 if (end > top_range)
5006 end = top_range;
5008 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
5009 below top positions, to make sure the handle is always at least
5010 that many pixels tall. */
5011 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
5013 /* Draw the empty space above the handle. Note that we can't clear
5014 zero-height areas; that means "clear to end of window." */
5015 if (start > 0)
5016 x_clear_area (FRAME_X_DISPLAY (f), w,
5017 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5018 VERTICAL_SCROLL_BAR_TOP_BORDER,
5019 inside_width, start);
5021 /* Change to proper foreground color if one is specified. */
5022 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5023 XSetForeground (FRAME_X_DISPLAY (f), gc,
5024 f->output_data.x->scroll_bar_foreground_pixel);
5026 /* Draw the handle itself. */
5027 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
5028 /* x, y, width, height */
5029 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5030 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
5031 inside_width, end - start);
5033 /* Restore the foreground color of the GC if we changed it above. */
5034 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5035 XSetForeground (FRAME_X_DISPLAY (f), gc,
5036 FRAME_FOREGROUND_PIXEL (f));
5038 /* Draw the empty space below the handle. Note that we can't
5039 clear zero-height areas; that means "clear to end of window." */
5040 if (end < inside_height)
5041 x_clear_area (FRAME_X_DISPLAY (f), w,
5042 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5043 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
5044 inside_width, inside_height - end);
5047 unblock_input ();
5050 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5052 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
5053 nil. */
5055 static void
5056 x_scroll_bar_remove (struct scroll_bar *bar)
5058 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5059 block_input ();
5061 #ifdef USE_TOOLKIT_SCROLL_BARS
5062 #ifdef USE_GTK
5063 xg_remove_scroll_bar (f, bar->x_window);
5064 #else /* not USE_GTK */
5065 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5066 #endif /* not USE_GTK */
5067 #else
5068 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
5069 #endif
5071 /* Dissociate this scroll bar from its window. */
5072 wset_vertical_scroll_bar (XWINDOW (bar->window), Qnil);
5074 unblock_input ();
5078 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5079 that we are displaying PORTION characters out of a total of WHOLE
5080 characters, starting at POSITION. If WINDOW has no scroll bar,
5081 create one. */
5083 static void
5084 XTset_vertical_scroll_bar (struct window *w, int portion, int whole, int position)
5086 struct frame *f = XFRAME (w->frame);
5087 Lisp_Object barobj;
5088 struct scroll_bar *bar;
5089 int top, height, left, width;
5090 int window_y, window_height;
5092 /* Get window dimensions. */
5093 window_box (w, ANY_AREA, 0, &window_y, 0, &window_height);
5094 top = window_y;
5095 height = window_height;
5097 /* Compute the left edge and the width of the scroll bar area. */
5098 left = WINDOW_SCROLL_BAR_AREA_X (w);
5099 width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
5101 /* Does the scroll bar exist yet? */
5102 if (NILP (w->vertical_scroll_bar))
5104 if (width > 0 && height > 0)
5106 block_input ();
5107 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5108 left, top, width, height);
5109 unblock_input ();
5112 bar = x_scroll_bar_create (w, top, left, width, max (height, 1));
5114 else
5116 /* It may just need to be moved and resized. */
5117 unsigned int mask = 0;
5119 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5121 block_input ();
5123 if (left != bar->left)
5124 mask |= CWX;
5125 if (top != bar->top)
5126 mask |= CWY;
5127 if (width != bar->width)
5128 mask |= CWWidth;
5129 if (height != bar->height)
5130 mask |= CWHeight;
5132 #ifdef USE_TOOLKIT_SCROLL_BARS
5134 /* Move/size the scroll bar widget. */
5135 if (mask)
5137 /* Since toolkit scroll bars are smaller than the space reserved
5138 for them on the frame, we have to clear "under" them. */
5139 if (width > 0 && height > 0)
5140 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5141 left, top, width, height);
5142 #ifdef USE_GTK
5143 xg_update_scrollbar_pos (f, bar->x_window, top,
5144 left, width, max (height, 1));
5145 #else /* not USE_GTK */
5146 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5147 left, top, width, max (height, 1), 0);
5148 #endif /* not USE_GTK */
5150 #else /* not USE_TOOLKIT_SCROLL_BARS */
5152 /* Move/size the scroll bar window. */
5153 if (mask)
5155 XWindowChanges wc;
5157 wc.x = left;
5158 wc.y = top;
5159 wc.width = width;
5160 wc.height = height;
5161 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5162 mask, &wc);
5165 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5167 /* Remember new settings. */
5168 bar->left = left;
5169 bar->top = top;
5170 bar->width = width;
5171 bar->height = height;
5173 unblock_input ();
5176 #ifdef USE_TOOLKIT_SCROLL_BARS
5177 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5178 #else /* not USE_TOOLKIT_SCROLL_BARS */
5179 /* Set the scroll bar's current state, unless we're currently being
5180 dragged. */
5181 if (bar->dragging == -1)
5183 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5185 if (whole == 0)
5186 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5187 else
5189 int start = ((double) position * top_range) / whole;
5190 int end = ((double) (position + portion) * top_range) / whole;
5191 x_scroll_bar_set_handle (bar, start, end, 0);
5194 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5196 XSETVECTOR (barobj, bar);
5197 wset_vertical_scroll_bar (w, barobj);
5201 /* The following three hooks are used when we're doing a thorough
5202 redisplay of the frame. We don't explicitly know which scroll bars
5203 are going to be deleted, because keeping track of when windows go
5204 away is a real pain - "Can you say set-window-configuration, boys
5205 and girls?" Instead, we just assert at the beginning of redisplay
5206 that *all* scroll bars are to be removed, and then save a scroll bar
5207 from the fiery pit when we actually redisplay its window. */
5209 /* Arrange for all scroll bars on FRAME to be removed at the next call
5210 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5211 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5213 static void
5214 XTcondemn_scroll_bars (struct frame *frame)
5216 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5217 while (! NILP (FRAME_SCROLL_BARS (frame)))
5219 Lisp_Object bar;
5220 bar = FRAME_SCROLL_BARS (frame);
5221 fset_scroll_bars (frame, XSCROLL_BAR (bar)->next);
5222 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5223 XSCROLL_BAR (bar)->prev = Qnil;
5224 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5225 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5226 fset_condemned_scroll_bars (frame, bar);
5231 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5232 Note that WINDOW isn't necessarily condemned at all. */
5234 static void
5235 XTredeem_scroll_bar (struct window *window)
5237 struct scroll_bar *bar;
5238 struct frame *f;
5239 Lisp_Object barobj;
5241 /* We can't redeem this window's scroll bar if it doesn't have one. */
5242 if (NILP (window->vertical_scroll_bar))
5243 emacs_abort ();
5245 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5247 /* Unlink it from the condemned list. */
5248 f = XFRAME (WINDOW_FRAME (window));
5249 if (NILP (bar->prev))
5251 /* If the prev pointer is nil, it must be the first in one of
5252 the lists. */
5253 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5254 /* It's not condemned. Everything's fine. */
5255 return;
5256 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5257 window->vertical_scroll_bar))
5258 fset_condemned_scroll_bars (f, bar->next);
5259 else
5260 /* If its prev pointer is nil, it must be at the front of
5261 one or the other! */
5262 emacs_abort ();
5264 else
5265 XSCROLL_BAR (bar->prev)->next = bar->next;
5267 if (! NILP (bar->next))
5268 XSCROLL_BAR (bar->next)->prev = bar->prev;
5270 bar->next = FRAME_SCROLL_BARS (f);
5271 bar->prev = Qnil;
5272 XSETVECTOR (barobj, bar);
5273 fset_scroll_bars (f, barobj);
5274 if (! NILP (bar->next))
5275 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5278 /* Remove all scroll bars on FRAME that haven't been saved since the
5279 last call to `*condemn_scroll_bars_hook'. */
5281 static void
5282 XTjudge_scroll_bars (struct frame *f)
5284 Lisp_Object bar, next;
5286 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5288 /* Clear out the condemned list now so we won't try to process any
5289 more events on the hapless scroll bars. */
5290 fset_condemned_scroll_bars (f, Qnil);
5292 for (; ! NILP (bar); bar = next)
5294 struct scroll_bar *b = XSCROLL_BAR (bar);
5296 x_scroll_bar_remove (b);
5298 next = b->next;
5299 b->next = b->prev = Qnil;
5302 /* Now there should be no references to the condemned scroll bars,
5303 and they should get garbage-collected. */
5307 #ifndef USE_TOOLKIT_SCROLL_BARS
5308 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5309 is a no-op when using toolkit scroll bars.
5311 This may be called from a signal handler, so we have to ignore GC
5312 mark bits. */
5314 static void
5315 x_scroll_bar_expose (struct scroll_bar *bar, const XEvent *event)
5317 Window w = bar->x_window;
5318 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5319 GC gc = f->output_data.x->normal_gc;
5321 block_input ();
5323 x_scroll_bar_set_handle (bar, bar->start, bar->end, 1);
5325 /* Switch to scroll bar foreground color. */
5326 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5327 XSetForeground (FRAME_X_DISPLAY (f), gc,
5328 f->output_data.x->scroll_bar_foreground_pixel);
5330 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5331 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5332 /* x, y, width, height */
5333 0, 0, bar->width - 1, bar->height - 1);
5335 /* Restore the foreground color of the GC if we changed it above. */
5336 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5337 XSetForeground (FRAME_X_DISPLAY (f), gc,
5338 FRAME_FOREGROUND_PIXEL (f));
5340 unblock_input ();
5343 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5345 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5346 is set to something other than NO_EVENT, it is enqueued.
5348 This may be called from a signal handler, so we have to ignore GC
5349 mark bits. */
5352 static void
5353 x_scroll_bar_handle_click (struct scroll_bar *bar,
5354 const XEvent *event,
5355 struct input_event *emacs_event)
5357 if (! WINDOWP (bar->window))
5358 emacs_abort ();
5360 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5361 emacs_event->code = event->xbutton.button - Button1;
5362 emacs_event->modifiers
5363 = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO
5364 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5365 event->xbutton.state)
5366 | (event->type == ButtonRelease
5367 ? up_modifier
5368 : down_modifier));
5369 emacs_event->frame_or_window = bar->window;
5370 emacs_event->arg = Qnil;
5371 emacs_event->timestamp = event->xbutton.time;
5373 int top_range
5374 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5375 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5377 if (y < 0) y = 0;
5378 if (y > top_range) y = top_range;
5380 if (y < bar->start)
5381 emacs_event->part = scroll_bar_above_handle;
5382 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5383 emacs_event->part = scroll_bar_handle;
5384 else
5385 emacs_event->part = scroll_bar_below_handle;
5387 #ifndef USE_TOOLKIT_SCROLL_BARS
5388 /* If the user has released the handle, set it to its final position. */
5389 if (event->type == ButtonRelease && bar->dragging != -1)
5391 int new_start = y - bar->dragging;
5392 int new_end = new_start + bar->end - bar->start;
5394 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5395 bar->dragging = -1;
5397 #endif
5399 XSETINT (emacs_event->x, y);
5400 XSETINT (emacs_event->y, top_range);
5404 #ifndef USE_TOOLKIT_SCROLL_BARS
5406 /* Handle some mouse motion while someone is dragging the scroll bar.
5408 This may be called from a signal handler, so we have to ignore GC
5409 mark bits. */
5411 static void
5412 x_scroll_bar_note_movement (struct scroll_bar *bar,
5413 const XMotionEvent *event)
5415 struct frame *f = XFRAME (XWINDOW (bar->window)->frame);
5416 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
5418 dpyinfo->last_mouse_movement_time = event->time;
5419 dpyinfo->last_mouse_scroll_bar = bar;
5420 f->mouse_moved = 1;
5422 /* If we're dragging the bar, display it. */
5423 if (bar->dragging != -1)
5425 /* Where should the handle be now? */
5426 int new_start = event->y - bar->dragging;
5428 if (new_start != bar->start)
5430 int new_end = new_start + bar->end - bar->start;
5432 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5437 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5439 /* Return information to the user about the current position of the mouse
5440 on the scroll bar. */
5442 static void
5443 x_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
5444 enum scroll_bar_part *part, Lisp_Object *x,
5445 Lisp_Object *y, Time *timestamp)
5447 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
5448 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
5449 Window w = bar->x_window;
5450 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5451 int win_x, win_y;
5452 Window dummy_window;
5453 int dummy_coord;
5454 unsigned int dummy_mask;
5456 block_input ();
5458 /* Get the mouse's position relative to the scroll bar window, and
5459 report that. */
5460 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
5462 /* Root, child, root x and root y. */
5463 &dummy_window, &dummy_window,
5464 &dummy_coord, &dummy_coord,
5466 /* Position relative to scroll bar. */
5467 &win_x, &win_y,
5469 /* Mouse buttons and modifier keys. */
5470 &dummy_mask))
5472 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5474 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5476 if (bar->dragging != -1)
5477 win_y -= bar->dragging;
5479 if (win_y < 0)
5480 win_y = 0;
5481 if (win_y > top_range)
5482 win_y = top_range;
5484 *fp = f;
5485 *bar_window = bar->window;
5487 if (bar->dragging != -1)
5488 *part = scroll_bar_handle;
5489 else if (win_y < bar->start)
5490 *part = scroll_bar_above_handle;
5491 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5492 *part = scroll_bar_handle;
5493 else
5494 *part = scroll_bar_below_handle;
5496 XSETINT (*x, win_y);
5497 XSETINT (*y, top_range);
5499 f->mouse_moved = 0;
5500 dpyinfo->last_mouse_scroll_bar = NULL;
5501 *timestamp = dpyinfo->last_mouse_movement_time;
5504 unblock_input ();
5508 /* The screen has been cleared so we may have changed foreground or
5509 background colors, and the scroll bars may need to be redrawn.
5510 Clear out the scroll bars, and ask for expose events, so we can
5511 redraw them. */
5513 static void
5514 x_scroll_bar_clear (struct frame *f)
5516 #ifndef USE_TOOLKIT_SCROLL_BARS
5517 Lisp_Object bar;
5519 /* We can have scroll bars even if this is 0,
5520 if we just turned off scroll bar mode.
5521 But in that case we should not clear them. */
5522 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5523 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5524 bar = XSCROLL_BAR (bar)->next)
5525 XClearArea (FRAME_X_DISPLAY (f),
5526 XSCROLL_BAR (bar)->x_window,
5527 0, 0, 0, 0, True);
5528 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5531 #ifdef ENABLE_CHECKING
5533 /* Record the last 100 characters stored
5534 to help debug the loss-of-chars-during-GC problem. */
5536 static int temp_index;
5537 static short temp_buffer[100];
5539 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5540 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5541 temp_index = 0; \
5542 temp_buffer[temp_index++] = (keysym)
5544 #else /* not ENABLE_CHECKING */
5546 #define STORE_KEYSYM_FOR_DEBUG(keysym) ((void)0)
5548 #endif /* ENABLE_CHECKING */
5550 /* Set this to nonzero to fake an "X I/O error"
5551 on a particular display. */
5553 static struct x_display_info *XTread_socket_fake_io_error;
5555 /* When we find no input here, we occasionally do a no-op command
5556 to verify that the X server is still running and we can still talk with it.
5557 We try all the open displays, one by one.
5558 This variable is used for cycling thru the displays. */
5560 static struct x_display_info *next_noop_dpyinfo;
5562 enum
5564 X_EVENT_NORMAL,
5565 X_EVENT_GOTO_OUT,
5566 X_EVENT_DROP
5569 /* Filter events for the current X input method.
5570 DPYINFO is the display this event is for.
5571 EVENT is the X event to filter.
5573 Returns non-zero if the event was filtered, caller shall not process
5574 this event further.
5575 Returns zero if event is wasn't filtered. */
5577 #ifdef HAVE_X_I18N
5578 static int
5579 x_filter_event (struct x_display_info *dpyinfo, XEvent *event)
5581 /* XFilterEvent returns non-zero if the input method has
5582 consumed the event. We pass the frame's X window to
5583 XFilterEvent because that's the one for which the IC
5584 was created. */
5586 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5587 event->xclient.window);
5589 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5591 #endif
5593 #ifdef USE_GTK
5594 static int current_count;
5595 static int current_finish;
5596 static struct input_event *current_hold_quit;
5598 /* This is the filter function invoked by the GTK event loop.
5599 It is invoked before the XEvent is translated to a GdkEvent,
5600 so we have a chance to act on the event before GTK. */
5601 static GdkFilterReturn
5602 event_handler_gdk (GdkXEvent *gxev, GdkEvent *ev, gpointer data)
5604 XEvent *xev = (XEvent *) gxev;
5606 block_input ();
5607 if (current_count >= 0)
5609 struct x_display_info *dpyinfo;
5611 dpyinfo = x_display_info_for_display (xev->xany.display);
5613 #ifdef HAVE_X_I18N
5614 /* Filter events for the current X input method.
5615 GTK calls XFilterEvent but not for key press and release,
5616 so we do it here. */
5617 if ((xev->type == KeyPress || xev->type == KeyRelease)
5618 && dpyinfo
5619 && x_filter_event (dpyinfo, xev))
5621 unblock_input ();
5622 return GDK_FILTER_REMOVE;
5624 #endif
5626 if (! dpyinfo)
5627 current_finish = X_EVENT_NORMAL;
5628 else
5629 current_count +=
5630 handle_one_xevent (dpyinfo, xev, &current_finish,
5631 current_hold_quit);
5633 else
5634 current_finish = x_dispatch_event (xev, xev->xany.display);
5636 unblock_input ();
5638 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5639 return GDK_FILTER_REMOVE;
5641 return GDK_FILTER_CONTINUE;
5643 #endif /* USE_GTK */
5646 static void xembed_send_message (struct frame *f, Time,
5647 enum xembed_message,
5648 long detail, long data1, long data2);
5650 /* Handles the XEvent EVENT on display DPYINFO.
5652 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5653 *FINISH is zero if caller should continue reading events.
5654 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5655 *EVENT is unchanged unless we're processing KeyPress event.
5657 We return the number of characters stored into the buffer. */
5659 static int
5660 handle_one_xevent (struct x_display_info *dpyinfo,
5661 const XEvent *event,
5662 int *finish, struct input_event *hold_quit)
5664 union {
5665 struct input_event ie;
5666 struct selection_input_event sie;
5667 } inev;
5668 int count = 0;
5669 int do_help = 0;
5670 ptrdiff_t nbytes = 0;
5671 struct frame *any, *f = NULL;
5672 struct coding_system coding;
5673 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
5674 /* This holds the state XLookupString needs to implement dead keys
5675 and other tricks known as "compose processing". _X Window System_
5676 says that a portable program can't use this, but Stephen Gildea assures
5677 me that letting the compiler initialize it to zeros will work okay. */
5678 static XComposeStatus compose_status;
5680 USE_SAFE_ALLOCA;
5682 *finish = X_EVENT_NORMAL;
5684 EVENT_INIT (inev.ie);
5685 inev.ie.kind = NO_EVENT;
5686 inev.ie.arg = Qnil;
5688 any = x_any_window_to_frame (dpyinfo, event->xany.window);
5690 if (any && any->wait_event_type == event->type)
5691 any->wait_event_type = 0; /* Indicates we got it. */
5693 switch (event->type)
5695 case ClientMessage:
5697 if (event->xclient.message_type == dpyinfo->Xatom_wm_protocols
5698 && event->xclient.format == 32)
5700 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_take_focus)
5702 /* Use the value returned by x_any_window_to_frame
5703 because this could be the shell widget window
5704 if the frame has no title bar. */
5705 f = any;
5706 #ifdef HAVE_X_I18N
5707 /* Not quite sure this is needed -pd */
5708 if (f && FRAME_XIC (f))
5709 XSetICFocus (FRAME_XIC (f));
5710 #endif
5711 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5712 instructs the WM to set the input focus automatically for
5713 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5714 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5715 it has set the focus. So, XSetInputFocus below is not
5716 needed.
5718 The call to XSetInputFocus below has also caused trouble. In
5719 cases where the XSetInputFocus done by the WM and the one
5720 below are temporally close (on a fast machine), the call
5721 below can generate additional FocusIn events which confuse
5722 Emacs. */
5724 /* Since we set WM_TAKE_FOCUS, we must call
5725 XSetInputFocus explicitly. But not if f is null,
5726 since that might be an event for a deleted frame. */
5727 if (f)
5729 Display *d = event->xclient.display;
5730 /* Catch and ignore errors, in case window has been
5731 iconified by a window manager such as GWM. */
5732 x_catch_errors (d);
5733 XSetInputFocus (d, event->xclient.window,
5734 /* The ICCCM says this is
5735 the only valid choice. */
5736 RevertToParent,
5737 event->xclient.data.l[1]);
5738 /* This is needed to detect the error
5739 if there is an error. */
5740 XSync (d, False);
5741 x_uncatch_errors ();
5743 /* Not certain about handling scroll bars here */
5744 #endif /* 0 */
5745 goto done;
5748 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_save_yourself)
5750 /* Save state modify the WM_COMMAND property to
5751 something which can reinstate us. This notifies
5752 the session manager, who's looking for such a
5753 PropertyNotify. Can restart processing when
5754 a keyboard or mouse event arrives. */
5755 /* If we have a session manager, don't set this.
5756 KDE will then start two Emacsen, one for the
5757 session manager and one for this. */
5758 #ifdef HAVE_X_SM
5759 if (! x_session_have_connection ())
5760 #endif
5762 f = x_top_window_to_frame (dpyinfo,
5763 event->xclient.window);
5764 /* This is just so we only give real data once
5765 for a single Emacs process. */
5766 if (f == SELECTED_FRAME ())
5767 XSetCommand (FRAME_X_DISPLAY (f),
5768 event->xclient.window,
5769 initial_argv, initial_argc);
5770 else if (f)
5771 XSetCommand (FRAME_X_DISPLAY (f),
5772 event->xclient.window,
5773 0, 0);
5775 goto done;
5778 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_delete_window)
5780 f = any;
5781 if (!f)
5782 goto OTHER; /* May be a dialog that is to be removed */
5784 inev.ie.kind = DELETE_WINDOW_EVENT;
5785 XSETFRAME (inev.ie.frame_or_window, f);
5786 goto done;
5789 goto done;
5792 if (event->xclient.message_type == dpyinfo->Xatom_wm_configure_denied)
5793 goto done;
5795 if (event->xclient.message_type == dpyinfo->Xatom_wm_window_moved)
5797 int new_x, new_y;
5798 f = x_window_to_frame (dpyinfo, event->xclient.window);
5800 new_x = event->xclient.data.s[0];
5801 new_y = event->xclient.data.s[1];
5803 if (f)
5805 f->left_pos = new_x;
5806 f->top_pos = new_y;
5808 goto done;
5811 #ifdef HACK_EDITRES
5812 if (event->xclient.message_type == dpyinfo->Xatom_editres)
5814 f = any;
5815 if (f)
5816 _XEditResCheckMessages (f->output_data.x->widget,
5817 NULL, (XEvent *) event, NULL);
5818 goto done;
5820 #endif /* HACK_EDITRES */
5822 if (event->xclient.message_type == dpyinfo->Xatom_DONE
5823 || event->xclient.message_type == dpyinfo->Xatom_PAGE)
5825 /* Ghostview job completed. Kill it. We could
5826 reply with "Next" if we received "Page", but we
5827 currently never do because we are interested in
5828 images, only, which should have 1 page. */
5829 Pixmap pixmap = (Pixmap) event->xclient.data.l[1];
5830 f = x_window_to_frame (dpyinfo, event->xclient.window);
5831 if (!f)
5832 goto OTHER;
5833 x_kill_gs_process (pixmap, f);
5834 expose_frame (f, 0, 0, 0, 0);
5835 goto done;
5838 #ifdef USE_TOOLKIT_SCROLL_BARS
5839 /* Scroll bar callbacks send a ClientMessage from which
5840 we construct an input_event. */
5841 if (event->xclient.message_type == dpyinfo->Xatom_Scrollbar)
5843 x_scroll_bar_to_input_event (event, &inev.ie);
5844 *finish = X_EVENT_GOTO_OUT;
5845 goto done;
5847 #endif /* USE_TOOLKIT_SCROLL_BARS */
5849 /* XEmbed messages from the embedder (if any). */
5850 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
5852 enum xembed_message msg = event->xclient.data.l[1];
5853 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
5854 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
5856 *finish = X_EVENT_GOTO_OUT;
5857 goto done;
5860 xft_settings_event (dpyinfo, event);
5862 f = any;
5863 if (!f)
5864 goto OTHER;
5865 if (x_handle_dnd_message (f, &event->xclient, dpyinfo, &inev.ie))
5866 *finish = X_EVENT_DROP;
5868 break;
5870 case SelectionNotify:
5871 dpyinfo->last_user_time = event->xselection.time;
5872 #ifdef USE_X_TOOLKIT
5873 if (! x_window_to_frame (dpyinfo, event->xselection.requestor))
5874 goto OTHER;
5875 #endif /* not USE_X_TOOLKIT */
5876 x_handle_selection_notify (&event->xselection);
5877 break;
5879 case SelectionClear: /* Someone has grabbed ownership. */
5880 dpyinfo->last_user_time = event->xselectionclear.time;
5881 #ifdef USE_X_TOOLKIT
5882 if (! x_window_to_frame (dpyinfo, event->xselectionclear.window))
5883 goto OTHER;
5884 #endif /* USE_X_TOOLKIT */
5886 const XSelectionClearEvent *eventp = &event->xselectionclear;
5888 inev.ie.kind = SELECTION_CLEAR_EVENT;
5889 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
5890 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
5891 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
5893 break;
5895 case SelectionRequest: /* Someone wants our selection. */
5896 dpyinfo->last_user_time = event->xselectionrequest.time;
5897 #ifdef USE_X_TOOLKIT
5898 if (!x_window_to_frame (dpyinfo, event->xselectionrequest.owner))
5899 goto OTHER;
5900 #endif /* USE_X_TOOLKIT */
5902 const XSelectionRequestEvent *eventp = &event->xselectionrequest;
5904 inev.ie.kind = SELECTION_REQUEST_EVENT;
5905 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
5906 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
5907 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
5908 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
5909 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
5910 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
5912 break;
5914 case PropertyNotify:
5915 dpyinfo->last_user_time = event->xproperty.time;
5916 f = x_top_window_to_frame (dpyinfo, event->xproperty.window);
5917 if (f && event->xproperty.atom == dpyinfo->Xatom_net_wm_state)
5918 if (x_handle_net_wm_state (f, &event->xproperty)
5919 && FRAME_ICONIFIED_P (f)
5920 && f->output_data.x->net_wm_state_hidden_seen)
5922 /* Gnome shell does not iconify us when C-z is pressed.
5923 It hides the frame. So if our state says we aren't
5924 hidden anymore, treat it as deiconified. */
5925 SET_FRAME_VISIBLE (f, 1);
5926 SET_FRAME_ICONIFIED (f, 0);
5927 f->output_data.x->has_been_visible = 1;
5928 f->output_data.x->net_wm_state_hidden_seen = 0;
5929 inev.ie.kind = DEICONIFY_EVENT;
5930 XSETFRAME (inev.ie.frame_or_window, f);
5933 x_handle_property_notify (&event->xproperty);
5934 xft_settings_event (dpyinfo, event);
5935 goto OTHER;
5937 case ReparentNotify:
5938 f = x_top_window_to_frame (dpyinfo, event->xreparent.window);
5939 if (f)
5941 f->output_data.x->parent_desc = event->xreparent.parent;
5942 x_real_positions (f, &f->left_pos, &f->top_pos);
5944 /* Perhaps reparented due to a WM restart. Reset this. */
5945 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
5946 FRAME_DISPLAY_INFO (f)->net_supported_window = 0;
5948 x_set_frame_alpha (f);
5950 goto OTHER;
5952 case Expose:
5953 f = x_window_to_frame (dpyinfo, event->xexpose.window);
5954 if (f)
5956 if (!FRAME_VISIBLE_P (f))
5958 SET_FRAME_VISIBLE (f, 1);
5959 SET_FRAME_ICONIFIED (f, 0);
5960 f->output_data.x->has_been_visible = 1;
5961 SET_FRAME_GARBAGED (f);
5963 else
5965 #ifdef USE_GTK
5966 /* This seems to be needed for GTK 2.6 and later, see
5967 http://debbugs.gnu.org/cgi/bugreport.cgi?bug=15398. */
5968 x_clear_area (event->xexpose.display,
5969 event->xexpose.window,
5970 event->xexpose.x, event->xexpose.y,
5971 event->xexpose.width, event->xexpose.height);
5972 #endif
5973 expose_frame (f, event->xexpose.x, event->xexpose.y,
5974 event->xexpose.width, event->xexpose.height);
5977 else
5979 #ifndef USE_TOOLKIT_SCROLL_BARS
5980 struct scroll_bar *bar;
5981 #endif
5982 #if defined USE_LUCID
5983 /* Submenus of the Lucid menu bar aren't widgets
5984 themselves, so there's no way to dispatch events
5985 to them. Recognize this case separately. */
5987 Widget widget = x_window_to_menu_bar (event->xexpose.window);
5988 if (widget)
5989 xlwmenu_redisplay (widget);
5991 #endif /* USE_LUCID */
5993 #ifdef USE_TOOLKIT_SCROLL_BARS
5994 /* Dispatch event to the widget. */
5995 goto OTHER;
5996 #else /* not USE_TOOLKIT_SCROLL_BARS */
5997 bar = x_window_to_scroll_bar (event->xexpose.display,
5998 event->xexpose.window);
6000 if (bar)
6001 x_scroll_bar_expose (bar, event);
6002 #ifdef USE_X_TOOLKIT
6003 else
6004 goto OTHER;
6005 #endif /* USE_X_TOOLKIT */
6006 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6008 break;
6010 case GraphicsExpose: /* This occurs when an XCopyArea's
6011 source area was obscured or not
6012 available. */
6013 f = x_window_to_frame (dpyinfo, event->xgraphicsexpose.drawable);
6014 if (f)
6015 expose_frame (f, event->xgraphicsexpose.x,
6016 event->xgraphicsexpose.y,
6017 event->xgraphicsexpose.width,
6018 event->xgraphicsexpose.height);
6019 #ifdef USE_X_TOOLKIT
6020 else
6021 goto OTHER;
6022 #endif /* USE_X_TOOLKIT */
6023 break;
6025 case NoExpose: /* This occurs when an XCopyArea's
6026 source area was completely
6027 available. */
6028 break;
6030 case UnmapNotify:
6031 /* Redo the mouse-highlight after the tooltip has gone. */
6032 if (event->xunmap.window == tip_window)
6034 tip_window = 0;
6035 x_redo_mouse_highlight (dpyinfo);
6038 f = x_top_window_to_frame (dpyinfo, event->xunmap.window);
6039 if (f) /* F may no longer exist if
6040 the frame was deleted. */
6042 bool visible = FRAME_VISIBLE_P (f);
6043 /* While a frame is unmapped, display generation is
6044 disabled; you don't want to spend time updating a
6045 display that won't ever be seen. */
6046 SET_FRAME_VISIBLE (f, 0);
6047 /* We can't distinguish, from the event, whether the window
6048 has become iconified or invisible. So assume, if it
6049 was previously visible, than now it is iconified.
6050 But x_make_frame_invisible clears both
6051 the visible flag and the iconified flag;
6052 and that way, we know the window is not iconified now. */
6053 if (visible || FRAME_ICONIFIED_P (f))
6055 SET_FRAME_ICONIFIED (f, 1);
6056 inev.ie.kind = ICONIFY_EVENT;
6057 XSETFRAME (inev.ie.frame_or_window, f);
6060 goto OTHER;
6062 case MapNotify:
6063 if (event->xmap.window == tip_window)
6064 /* The tooltip has been drawn already. Avoid
6065 the SET_FRAME_GARBAGED below. */
6066 goto OTHER;
6068 /* We use x_top_window_to_frame because map events can
6069 come for sub-windows and they don't mean that the
6070 frame is visible. */
6071 f = x_top_window_to_frame (dpyinfo, event->xmap.window);
6072 if (f)
6074 bool iconified = FRAME_ICONIFIED_P (f);
6075 /* wait_reading_process_output will notice this and update
6076 the frame's display structures.
6077 If we where iconified, we should not set garbaged,
6078 because that stops redrawing on Expose events. This looks
6079 bad if we are called from a recursive event loop
6080 (x_dispatch_event), for example when a dialog is up. */
6081 if (!iconified)
6082 SET_FRAME_GARBAGED (f);
6084 /* Check if fullscreen was specified before we where mapped the
6085 first time, i.e. from the command line. */
6086 if (!f->output_data.x->has_been_visible)
6087 x_check_fullscreen (f);
6089 SET_FRAME_VISIBLE (f, 1);
6090 SET_FRAME_ICONIFIED (f, 0);
6091 f->output_data.x->has_been_visible = 1;
6093 if (iconified)
6095 inev.ie.kind = DEICONIFY_EVENT;
6096 XSETFRAME (inev.ie.frame_or_window, f);
6098 else if (! NILP (Vframe_list) && ! NILP (XCDR (Vframe_list)))
6099 /* Force a redisplay sooner or later to update the
6100 frame titles in case this is the second frame. */
6101 record_asynch_buffer_change ();
6103 #ifdef USE_GTK
6104 xg_frame_resized (f, -1, -1);
6105 #endif
6107 goto OTHER;
6109 case KeyPress:
6111 dpyinfo->last_user_time = event->xkey.time;
6112 ignore_next_mouse_click_timeout = 0;
6114 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6115 /* Dispatch KeyPress events when in menu. */
6116 if (popup_activated ())
6117 goto OTHER;
6118 #endif
6120 f = any;
6122 #if ! defined (USE_GTK)
6123 /* If mouse-highlight is an integer, input clears out
6124 mouse highlighting. */
6125 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
6126 && (f == 0
6127 || !EQ (f->tool_bar_window, hlinfo->mouse_face_window)))
6129 clear_mouse_face (hlinfo);
6130 hlinfo->mouse_face_hidden = 1;
6132 #endif
6134 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6135 if (f == 0)
6137 /* Scroll bars consume key events, but we want
6138 the keys to go to the scroll bar's frame. */
6139 Widget widget = XtWindowToWidget (dpyinfo->display,
6140 event->xkey.window);
6141 if (widget && XmIsScrollBar (widget))
6143 widget = XtParent (widget);
6144 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6147 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6149 if (f != 0)
6151 KeySym keysym, orig_keysym;
6152 /* al%imercury@uunet.uu.net says that making this 81
6153 instead of 80 fixed a bug whereby meta chars made
6154 his Emacs hang.
6156 It seems that some version of XmbLookupString has
6157 a bug of not returning XBufferOverflow in
6158 status_return even if the input is too long to
6159 fit in 81 bytes. So, we must prepare sufficient
6160 bytes for copy_buffer. 513 bytes (256 chars for
6161 two-byte character set) seems to be a fairly good
6162 approximation. -- 2000.8.10 handa@etl.go.jp */
6163 unsigned char copy_buffer[513];
6164 unsigned char *copy_bufptr = copy_buffer;
6165 int copy_bufsiz = sizeof (copy_buffer);
6166 int modifiers;
6167 Lisp_Object coding_system = Qlatin_1;
6168 Lisp_Object c;
6169 /* Event will be modified. */
6170 XKeyEvent xkey = event->xkey;
6172 #ifdef USE_GTK
6173 /* Don't pass keys to GTK. A Tab will shift focus to the
6174 tool bar in GTK 2.4. Keys will still go to menus and
6175 dialogs because in that case popup_activated is nonzero
6176 (see above). */
6177 *finish = X_EVENT_DROP;
6178 #endif
6180 xkey.state |= x_emacs_to_x_modifiers (FRAME_DISPLAY_INFO (f),
6181 extra_keyboard_modifiers);
6182 modifiers = xkey.state;
6184 /* This will have to go some day... */
6186 /* make_lispy_event turns chars into control chars.
6187 Don't do it here because XLookupString is too eager. */
6188 xkey.state &= ~ControlMask;
6189 xkey.state &= ~(dpyinfo->meta_mod_mask
6190 | dpyinfo->super_mod_mask
6191 | dpyinfo->hyper_mod_mask
6192 | dpyinfo->alt_mod_mask);
6194 /* In case Meta is ComposeCharacter,
6195 clear its status. According to Markus Ehrnsperger
6196 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6197 this enables ComposeCharacter to work whether or
6198 not it is combined with Meta. */
6199 if (modifiers & dpyinfo->meta_mod_mask)
6200 memset (&compose_status, 0, sizeof (compose_status));
6202 #ifdef HAVE_X_I18N
6203 if (FRAME_XIC (f))
6205 Status status_return;
6207 coding_system = Vlocale_coding_system;
6208 nbytes = XmbLookupString (FRAME_XIC (f),
6209 &xkey, (char *) copy_bufptr,
6210 copy_bufsiz, &keysym,
6211 &status_return);
6212 if (status_return == XBufferOverflow)
6214 copy_bufsiz = nbytes + 1;
6215 copy_bufptr = alloca (copy_bufsiz);
6216 nbytes = XmbLookupString (FRAME_XIC (f),
6217 &xkey, (char *) copy_bufptr,
6218 copy_bufsiz, &keysym,
6219 &status_return);
6221 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6222 if (status_return == XLookupNone)
6223 break;
6224 else if (status_return == XLookupChars)
6226 keysym = NoSymbol;
6227 modifiers = 0;
6229 else if (status_return != XLookupKeySym
6230 && status_return != XLookupBoth)
6231 emacs_abort ();
6233 else
6234 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
6235 copy_bufsiz, &keysym,
6236 &compose_status);
6237 #else
6238 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
6239 copy_bufsiz, &keysym,
6240 &compose_status);
6241 #endif
6243 /* If not using XIM/XIC, and a compose sequence is in progress,
6244 we break here. Otherwise, chars_matched is always 0. */
6245 if (compose_status.chars_matched > 0 && nbytes == 0)
6246 break;
6248 memset (&compose_status, 0, sizeof (compose_status));
6249 orig_keysym = keysym;
6251 /* Common for all keysym input events. */
6252 XSETFRAME (inev.ie.frame_or_window, f);
6253 inev.ie.modifiers
6254 = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), modifiers);
6255 inev.ie.timestamp = xkey.time;
6257 /* First deal with keysyms which have defined
6258 translations to characters. */
6259 if (keysym >= 32 && keysym < 128)
6260 /* Avoid explicitly decoding each ASCII character. */
6262 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6263 inev.ie.code = keysym;
6264 goto done_keysym;
6267 /* Keysyms directly mapped to Unicode characters. */
6268 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
6270 if (keysym < 0x01000080)
6271 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6272 else
6273 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6274 inev.ie.code = keysym & 0xFFFFFF;
6275 goto done_keysym;
6278 /* Now non-ASCII. */
6279 if (HASH_TABLE_P (Vx_keysym_table)
6280 && (c = Fgethash (make_number (keysym),
6281 Vx_keysym_table,
6282 Qnil),
6283 NATNUMP (c)))
6285 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6286 ? ASCII_KEYSTROKE_EVENT
6287 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6288 inev.ie.code = XFASTINT (c);
6289 goto done_keysym;
6292 /* Random non-modifier sorts of keysyms. */
6293 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6294 || keysym == XK_Delete
6295 #ifdef XK_ISO_Left_Tab
6296 || (keysym >= XK_ISO_Left_Tab
6297 && keysym <= XK_ISO_Enter)
6298 #endif
6299 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6300 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6301 #ifdef HPUX
6302 /* This recognizes the "extended function
6303 keys". It seems there's no cleaner way.
6304 Test IsModifierKey to avoid handling
6305 mode_switch incorrectly. */
6306 || (XK_Select <= keysym && keysym < XK_KP_Space)
6307 #endif
6308 #ifdef XK_dead_circumflex
6309 || orig_keysym == XK_dead_circumflex
6310 #endif
6311 #ifdef XK_dead_grave
6312 || orig_keysym == XK_dead_grave
6313 #endif
6314 #ifdef XK_dead_tilde
6315 || orig_keysym == XK_dead_tilde
6316 #endif
6317 #ifdef XK_dead_diaeresis
6318 || orig_keysym == XK_dead_diaeresis
6319 #endif
6320 #ifdef XK_dead_macron
6321 || orig_keysym == XK_dead_macron
6322 #endif
6323 #ifdef XK_dead_degree
6324 || orig_keysym == XK_dead_degree
6325 #endif
6326 #ifdef XK_dead_acute
6327 || orig_keysym == XK_dead_acute
6328 #endif
6329 #ifdef XK_dead_cedilla
6330 || orig_keysym == XK_dead_cedilla
6331 #endif
6332 #ifdef XK_dead_breve
6333 || orig_keysym == XK_dead_breve
6334 #endif
6335 #ifdef XK_dead_ogonek
6336 || orig_keysym == XK_dead_ogonek
6337 #endif
6338 #ifdef XK_dead_caron
6339 || orig_keysym == XK_dead_caron
6340 #endif
6341 #ifdef XK_dead_doubleacute
6342 || orig_keysym == XK_dead_doubleacute
6343 #endif
6344 #ifdef XK_dead_abovedot
6345 || orig_keysym == XK_dead_abovedot
6346 #endif
6347 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6348 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6349 /* Any "vendor-specific" key is ok. */
6350 || (orig_keysym & (1 << 28))
6351 || (keysym != NoSymbol && nbytes == 0))
6352 && ! (IsModifierKey (orig_keysym)
6353 /* The symbols from XK_ISO_Lock
6354 to XK_ISO_Last_Group_Lock
6355 don't have real modifiers but
6356 should be treated similarly to
6357 Mode_switch by Emacs. */
6358 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6359 || (XK_ISO_Lock <= orig_keysym
6360 && orig_keysym <= XK_ISO_Last_Group_Lock)
6361 #endif
6364 STORE_KEYSYM_FOR_DEBUG (keysym);
6365 /* make_lispy_event will convert this to a symbolic
6366 key. */
6367 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6368 inev.ie.code = keysym;
6369 goto done_keysym;
6372 { /* Raw bytes, not keysym. */
6373 ptrdiff_t i;
6374 int nchars, len;
6376 for (i = 0, nchars = 0; i < nbytes; i++)
6378 if (ASCII_BYTE_P (copy_bufptr[i]))
6379 nchars++;
6380 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6383 if (nchars < nbytes)
6385 /* Decode the input data. */
6387 /* The input should be decoded with `coding_system'
6388 which depends on which X*LookupString function
6389 we used just above and the locale. */
6390 setup_coding_system (coding_system, &coding);
6391 coding.src_multibyte = 0;
6392 coding.dst_multibyte = 1;
6393 /* The input is converted to events, thus we can't
6394 handle composition. Anyway, there's no XIM that
6395 gives us composition information. */
6396 coding.common_flags &= ~CODING_ANNOTATION_MASK;
6398 SAFE_NALLOCA (coding.destination, MAX_MULTIBYTE_LENGTH,
6399 nbytes);
6400 coding.dst_bytes = MAX_MULTIBYTE_LENGTH * nbytes;
6401 coding.mode |= CODING_MODE_LAST_BLOCK;
6402 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
6403 nbytes = coding.produced;
6404 nchars = coding.produced_char;
6405 copy_bufptr = coding.destination;
6408 /* Convert the input data to a sequence of
6409 character events. */
6410 for (i = 0; i < nbytes; i += len)
6412 int ch;
6413 if (nchars == nbytes)
6414 ch = copy_bufptr[i], len = 1;
6415 else
6416 ch = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
6417 inev.ie.kind = (SINGLE_BYTE_CHAR_P (ch)
6418 ? ASCII_KEYSTROKE_EVENT
6419 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6420 inev.ie.code = ch;
6421 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6424 count += nchars;
6426 inev.ie.kind = NO_EVENT; /* Already stored above. */
6428 if (keysym == NoSymbol)
6429 break;
6431 /* FIXME: check side effects and remove this. */
6432 ((XEvent *) event)->xkey = xkey;
6434 done_keysym:
6435 #ifdef HAVE_X_I18N
6436 /* Don't dispatch this event since XtDispatchEvent calls
6437 XFilterEvent, and two calls in a row may freeze the
6438 client. */
6439 break;
6440 #else
6441 goto OTHER;
6442 #endif
6444 case KeyRelease:
6445 dpyinfo->last_user_time = event->xkey.time;
6446 #ifdef HAVE_X_I18N
6447 /* Don't dispatch this event since XtDispatchEvent calls
6448 XFilterEvent, and two calls in a row may freeze the
6449 client. */
6450 break;
6451 #else
6452 goto OTHER;
6453 #endif
6455 case EnterNotify:
6456 dpyinfo->last_user_time = event->xcrossing.time;
6457 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
6459 f = any;
6461 if (f && x_mouse_click_focus_ignore_position)
6462 ignore_next_mouse_click_timeout = event->xmotion.time + 200;
6464 /* EnterNotify counts as mouse movement,
6465 so update things that depend on mouse position. */
6466 if (f && !f->output_data.x->hourglass_p)
6467 note_mouse_movement (f, &event->xmotion);
6468 #ifdef USE_GTK
6469 /* We may get an EnterNotify on the buttons in the toolbar. In that
6470 case we moved out of any highlighted area and need to note this. */
6471 if (!f && dpyinfo->last_mouse_glyph_frame)
6472 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
6473 #endif
6474 goto OTHER;
6476 case FocusIn:
6477 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
6478 goto OTHER;
6480 case LeaveNotify:
6481 dpyinfo->last_user_time = event->xcrossing.time;
6482 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
6484 f = x_top_window_to_frame (dpyinfo, event->xcrossing.window);
6485 if (f)
6487 if (f == hlinfo->mouse_face_mouse_frame)
6489 /* If we move outside the frame, then we're
6490 certainly no longer on any text in the frame. */
6491 clear_mouse_face (hlinfo);
6492 hlinfo->mouse_face_mouse_frame = 0;
6495 /* Generate a nil HELP_EVENT to cancel a help-echo.
6496 Do it only if there's something to cancel.
6497 Otherwise, the startup message is cleared when
6498 the mouse leaves the frame. */
6499 if (any_help_event_p)
6500 do_help = -1;
6502 #ifdef USE_GTK
6503 /* See comment in EnterNotify above */
6504 else if (dpyinfo->last_mouse_glyph_frame)
6505 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
6506 #endif
6507 goto OTHER;
6509 case FocusOut:
6510 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
6511 goto OTHER;
6513 case MotionNotify:
6515 dpyinfo->last_user_time = event->xmotion.time;
6516 previous_help_echo_string = help_echo_string;
6517 help_echo_string = Qnil;
6519 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
6520 : x_window_to_frame (dpyinfo, event->xmotion.window));
6522 if (hlinfo->mouse_face_hidden)
6524 hlinfo->mouse_face_hidden = 0;
6525 clear_mouse_face (hlinfo);
6528 #ifdef USE_GTK
6529 if (f && xg_event_is_for_scrollbar (f, event))
6530 f = 0;
6531 #endif
6532 if (f)
6535 /* Generate SELECT_WINDOW_EVENTs when needed.
6536 Don't let popup menus influence things (bug#1261). */
6537 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
6539 static Lisp_Object last_mouse_window;
6540 Lisp_Object window = window_from_coordinates
6541 (f, event->xmotion.x, event->xmotion.y, 0, 0);
6543 /* Window will be selected only when it is not selected now and
6544 last mouse movement event was not in it. Minibuffer window
6545 will be selected only when it is active. */
6546 if (WINDOWP (window)
6547 && !EQ (window, last_mouse_window)
6548 && !EQ (window, selected_window)
6549 /* For click-to-focus window managers
6550 create event iff we don't leave the
6551 selected frame. */
6552 && (focus_follows_mouse
6553 || (EQ (XWINDOW (window)->frame,
6554 XWINDOW (selected_window)->frame))))
6556 inev.ie.kind = SELECT_WINDOW_EVENT;
6557 inev.ie.frame_or_window = window;
6559 /* Remember the last window where we saw the mouse. */
6560 last_mouse_window = window;
6562 if (!note_mouse_movement (f, &event->xmotion))
6563 help_echo_string = previous_help_echo_string;
6565 else
6567 #ifndef USE_TOOLKIT_SCROLL_BARS
6568 struct scroll_bar *bar
6569 = x_window_to_scroll_bar (event->xmotion.display,
6570 event->xmotion.window);
6572 if (bar)
6573 x_scroll_bar_note_movement (bar, &event->xmotion);
6574 #endif /* USE_TOOLKIT_SCROLL_BARS */
6576 /* If we move outside the frame, then we're
6577 certainly no longer on any text in the frame. */
6578 clear_mouse_face (hlinfo);
6581 /* If the contents of the global variable help_echo_string
6582 has changed, generate a HELP_EVENT. */
6583 if (!NILP (help_echo_string)
6584 || !NILP (previous_help_echo_string))
6585 do_help = 1;
6586 goto OTHER;
6589 case ConfigureNotify:
6590 f = x_top_window_to_frame (dpyinfo, event->xconfigure.window);
6591 #ifdef USE_GTK
6592 if (!f
6593 && (f = any)
6594 && event->xconfigure.window == FRAME_X_WINDOW (f))
6596 xg_frame_resized (f, event->xconfigure.width,
6597 event->xconfigure.height);
6598 f = 0;
6600 #endif
6601 if (f)
6603 #ifndef USE_X_TOOLKIT
6604 #ifndef USE_GTK
6605 int width = FRAME_PIXEL_TO_TEXT_WIDTH (f, event->xconfigure.width);
6606 int height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, event->xconfigure.height);
6608 /* In the toolkit version, change_frame_size
6609 is called by the code that handles resizing
6610 of the EmacsFrame widget. */
6612 /* Even if the number of character rows and columns has
6613 not changed, the font size may have changed, so we need
6614 to check the pixel dimensions as well. */
6615 if (width != FRAME_TEXT_WIDTH (f)
6616 || height != FRAME_TEXT_HEIGHT (f)
6617 || event->xconfigure.width != FRAME_PIXEL_WIDTH (f)
6618 || event->xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6620 change_frame_size (f, width, height, 0, 1, 0, 1);
6621 SET_FRAME_GARBAGED (f);
6622 cancel_mouse_face (f);
6625 /** FRAME_PIXEL_WIDTH (f) = event->xconfigure.width; **/
6626 /** FRAME_PIXEL_HEIGHT (f) = event->xconfigure.height; **/
6627 #endif /* not USE_GTK */
6628 #endif
6630 #ifdef USE_GTK
6631 /* GTK creates windows but doesn't map them.
6632 Only get real positions when mapped. */
6633 if (FRAME_GTK_OUTER_WIDGET (f)
6634 && gtk_widget_get_mapped (FRAME_GTK_OUTER_WIDGET (f)))
6635 #endif
6636 x_real_positions (f, &f->left_pos, &f->top_pos);
6638 #ifdef HAVE_X_I18N
6639 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6640 xic_set_statusarea (f);
6641 #endif
6644 goto OTHER;
6646 case ButtonRelease:
6647 case ButtonPress:
6649 /* If we decide we want to generate an event to be seen
6650 by the rest of Emacs, we put it here. */
6651 bool tool_bar_p = 0;
6653 memset (&compose_status, 0, sizeof (compose_status));
6654 dpyinfo->last_mouse_glyph_frame = NULL;
6655 dpyinfo->last_user_time = event->xbutton.time;
6657 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
6658 : x_window_to_frame (dpyinfo, event->xbutton.window));
6660 #ifdef USE_GTK
6661 if (f && xg_event_is_for_scrollbar (f, event))
6662 f = 0;
6663 #endif
6664 if (f)
6666 #if ! defined (USE_GTK)
6667 /* Is this in the tool-bar? */
6668 if (WINDOWP (f->tool_bar_window)
6669 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6671 Lisp_Object window;
6672 int x = event->xbutton.x;
6673 int y = event->xbutton.y;
6675 window = window_from_coordinates (f, x, y, 0, 1);
6676 tool_bar_p = EQ (window, f->tool_bar_window);
6678 if (tool_bar_p && event->xbutton.button < 4)
6679 handle_tool_bar_click
6680 (f, x, y, event->xbutton.type == ButtonPress,
6681 x_x_to_emacs_modifiers (dpyinfo, event->xbutton.state));
6683 #endif /* !USE_GTK */
6685 if (!tool_bar_p)
6686 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6687 if (! popup_activated ())
6688 #endif
6690 if (ignore_next_mouse_click_timeout)
6692 if (event->type == ButtonPress
6693 && event->xbutton.time > ignore_next_mouse_click_timeout)
6695 ignore_next_mouse_click_timeout = 0;
6696 construct_mouse_click (&inev.ie, &event->xbutton, f);
6698 if (event->type == ButtonRelease)
6699 ignore_next_mouse_click_timeout = 0;
6701 else
6702 construct_mouse_click (&inev.ie, &event->xbutton, f);
6704 if (FRAME_X_EMBEDDED_P (f))
6705 xembed_send_message (f, event->xbutton.time,
6706 XEMBED_REQUEST_FOCUS, 0, 0, 0);
6708 else
6710 struct scroll_bar *bar
6711 = x_window_to_scroll_bar (event->xbutton.display,
6712 event->xbutton.window);
6714 #ifdef USE_TOOLKIT_SCROLL_BARS
6715 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6716 scroll bars. */
6717 if (bar && event->xbutton.state & ControlMask)
6719 x_scroll_bar_handle_click (bar, event, &inev.ie);
6720 *finish = X_EVENT_DROP;
6722 #else /* not USE_TOOLKIT_SCROLL_BARS */
6723 if (bar)
6724 x_scroll_bar_handle_click (bar, event, &inev.ie);
6725 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6728 if (event->type == ButtonPress)
6730 dpyinfo->grabbed |= (1 << event->xbutton.button);
6731 dpyinfo->last_mouse_frame = f;
6733 if (!tool_bar_p)
6734 last_tool_bar_item = -1;
6736 else
6737 dpyinfo->grabbed &= ~(1 << event->xbutton.button);
6739 /* Ignore any mouse motion that happened before this event;
6740 any subsequent mouse-movement Emacs events should reflect
6741 only motion after the ButtonPress/Release. */
6742 if (f != 0)
6743 f->mouse_moved = 0;
6745 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6746 f = x_menubar_window_to_frame (dpyinfo, event);
6747 /* For a down-event in the menu bar,
6748 don't pass it to Xt right now.
6749 Instead, save it away
6750 and we will pass it to Xt from kbd_buffer_get_event.
6751 That way, we can run some Lisp code first. */
6752 if (! popup_activated ()
6753 #ifdef USE_GTK
6754 /* Gtk+ menus only react to the first three buttons. */
6755 && event->xbutton.button < 3
6756 #endif
6757 && f && event->type == ButtonPress
6758 /* Verify the event is really within the menu bar
6759 and not just sent to it due to grabbing. */
6760 && event->xbutton.x >= 0
6761 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
6762 && event->xbutton.y >= 0
6763 && event->xbutton.y < f->output_data.x->menubar_height
6764 && event->xbutton.same_screen)
6766 if (!f->output_data.x->saved_menu_event)
6767 f->output_data.x->saved_menu_event = xmalloc (sizeof *event);
6768 *f->output_data.x->saved_menu_event = *event;
6769 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT;
6770 XSETFRAME (inev.ie.frame_or_window, f);
6771 *finish = X_EVENT_DROP;
6773 else
6774 goto OTHER;
6775 #endif /* USE_X_TOOLKIT || USE_GTK */
6777 break;
6779 case CirculateNotify:
6780 goto OTHER;
6782 case CirculateRequest:
6783 goto OTHER;
6785 case VisibilityNotify:
6786 goto OTHER;
6788 case MappingNotify:
6789 /* Someone has changed the keyboard mapping - update the
6790 local cache. */
6791 switch (event->xmapping.request)
6793 case MappingModifier:
6794 x_find_modifier_meanings (dpyinfo);
6795 /* This is meant to fall through. */
6796 case MappingKeyboard:
6797 XRefreshKeyboardMapping ((XMappingEvent *) &event->xmapping);
6799 goto OTHER;
6801 case DestroyNotify:
6802 xft_settings_event (dpyinfo, event);
6803 break;
6805 default:
6806 OTHER:
6807 #ifdef USE_X_TOOLKIT
6808 block_input ();
6809 if (*finish != X_EVENT_DROP)
6810 XtDispatchEvent ((XEvent *) event);
6811 unblock_input ();
6812 #endif /* USE_X_TOOLKIT */
6813 break;
6816 done:
6817 if (inev.ie.kind != NO_EVENT)
6819 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6820 count++;
6823 if (do_help
6824 && !(hold_quit && hold_quit->kind != NO_EVENT))
6826 Lisp_Object frame;
6828 if (f)
6829 XSETFRAME (frame, f);
6830 else
6831 frame = Qnil;
6833 if (do_help > 0)
6835 any_help_event_p = 1;
6836 gen_help_event (help_echo_string, frame, help_echo_window,
6837 help_echo_object, help_echo_pos);
6839 else
6841 help_echo_string = Qnil;
6842 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
6844 count++;
6847 SAFE_FREE ();
6848 return count;
6851 /* Handles the XEvent EVENT on display DISPLAY.
6852 This is used for event loops outside the normal event handling,
6853 i.e. looping while a popup menu or a dialog is posted.
6855 Returns the value handle_one_xevent sets in the finish argument. */
6857 x_dispatch_event (XEvent *event, Display *display)
6859 struct x_display_info *dpyinfo;
6860 int finish = X_EVENT_NORMAL;
6862 dpyinfo = x_display_info_for_display (display);
6864 if (dpyinfo)
6865 handle_one_xevent (dpyinfo, event, &finish, 0);
6867 return finish;
6870 /* Read events coming from the X server.
6871 Return as soon as there are no more events to be read.
6873 Return the number of characters stored into the buffer,
6874 thus pretending to be `read' (except the characters we store
6875 in the keyboard buffer can be multibyte, so are not necessarily
6876 C chars). */
6878 static int
6879 XTread_socket (struct terminal *terminal, struct input_event *hold_quit)
6881 int count = 0;
6882 int event_found = 0;
6883 struct x_display_info *dpyinfo = terminal->display_info.x;
6885 block_input ();
6887 /* For debugging, this gives a way to fake an I/O error. */
6888 if (dpyinfo == XTread_socket_fake_io_error)
6890 XTread_socket_fake_io_error = 0;
6891 x_io_error_quitter (dpyinfo->display);
6894 #ifndef USE_GTK
6895 while (XPending (dpyinfo->display))
6897 int finish;
6898 XEvent event;
6900 XNextEvent (dpyinfo->display, &event);
6902 #ifdef HAVE_X_I18N
6903 /* Filter events for the current X input method. */
6904 if (x_filter_event (dpyinfo, &event))
6905 continue;
6906 #endif
6907 event_found = 1;
6909 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
6911 if (finish == X_EVENT_GOTO_OUT)
6912 break;
6915 #else /* USE_GTK */
6917 /* For GTK we must use the GTK event loop. But XEvents gets passed
6918 to our filter function above, and then to the big event switch.
6919 We use a bunch of globals to communicate with our filter function,
6920 that is kind of ugly, but it works.
6922 There is no way to do one display at the time, GTK just does events
6923 from all displays. */
6925 while (gtk_events_pending ())
6927 current_count = count;
6928 current_hold_quit = hold_quit;
6930 gtk_main_iteration ();
6932 count = current_count;
6933 current_count = -1;
6934 current_hold_quit = 0;
6936 if (current_finish == X_EVENT_GOTO_OUT)
6937 break;
6939 #endif /* USE_GTK */
6941 /* On some systems, an X bug causes Emacs to get no more events
6942 when the window is destroyed. Detect that. (1994.) */
6943 if (! event_found)
6945 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
6946 One XNOOP in 100 loops will make Emacs terminate.
6947 B. Bretthauer, 1994 */
6948 x_noop_count++;
6949 if (x_noop_count >= 100)
6951 x_noop_count=0;
6953 if (next_noop_dpyinfo == 0)
6954 next_noop_dpyinfo = x_display_list;
6956 XNoOp (next_noop_dpyinfo->display);
6958 /* Each time we get here, cycle through the displays now open. */
6959 next_noop_dpyinfo = next_noop_dpyinfo->next;
6963 /* If the focus was just given to an auto-raising frame,
6964 raise it now. FIXME: handle more than one such frame. */
6965 if (dpyinfo->x_pending_autoraise_frame)
6967 x_raise_frame (dpyinfo->x_pending_autoraise_frame);
6968 dpyinfo->x_pending_autoraise_frame = NULL;
6971 unblock_input ();
6973 return count;
6979 /***********************************************************************
6980 Text Cursor
6981 ***********************************************************************/
6983 /* Set clipping for output in glyph row ROW. W is the window in which
6984 we operate. GC is the graphics context to set clipping in.
6986 ROW may be a text row or, e.g., a mode line. Text rows must be
6987 clipped to the interior of the window dedicated to text display,
6988 mode lines must be clipped to the whole window. */
6990 static void
6991 x_clip_to_row (struct window *w, struct glyph_row *row,
6992 enum glyph_row_area area, GC gc)
6994 struct frame *f = XFRAME (WINDOW_FRAME (w));
6995 XRectangle clip_rect;
6996 int window_x, window_y, window_width;
6998 window_box (w, area, &window_x, &window_y, &window_width, 0);
7000 clip_rect.x = window_x;
7001 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7002 clip_rect.y = max (clip_rect.y, window_y);
7003 clip_rect.width = window_width;
7004 clip_rect.height = row->visible_height;
7006 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7010 /* Draw a hollow box cursor on window W in glyph row ROW. */
7012 static void
7013 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
7015 struct frame *f = XFRAME (WINDOW_FRAME (w));
7016 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
7017 Display *dpy = FRAME_X_DISPLAY (f);
7018 int x, y, wd, h;
7019 XGCValues xgcv;
7020 struct glyph *cursor_glyph;
7021 GC gc;
7023 /* Get the glyph the cursor is on. If we can't tell because
7024 the current matrix is invalid or such, give up. */
7025 cursor_glyph = get_phys_cursor_glyph (w);
7026 if (cursor_glyph == NULL)
7027 return;
7029 /* Compute frame-relative coordinates for phys cursor. */
7030 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7031 wd = w->phys_cursor_width;
7033 /* The foreground of cursor_gc is typically the same as the normal
7034 background color, which can cause the cursor box to be invisible. */
7035 xgcv.foreground = f->output_data.x->cursor_pixel;
7036 if (dpyinfo->scratch_cursor_gc)
7037 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7038 else
7039 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7040 GCForeground, &xgcv);
7041 gc = dpyinfo->scratch_cursor_gc;
7043 /* Set clipping, draw the rectangle, and reset clipping again. */
7044 x_clip_to_row (w, row, TEXT_AREA, gc);
7045 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7046 XSetClipMask (dpy, gc, None);
7050 /* Draw a bar cursor on window W in glyph row ROW.
7052 Implementation note: One would like to draw a bar cursor with an
7053 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7054 Unfortunately, I didn't find a font yet that has this property set.
7055 --gerd. */
7057 static void
7058 x_draw_bar_cursor (struct window *w, struct glyph_row *row, int width, enum text_cursor_kinds kind)
7060 struct frame *f = XFRAME (w->frame);
7061 struct glyph *cursor_glyph;
7063 /* If cursor is out of bounds, don't draw garbage. This can happen
7064 in mini-buffer windows when switching between echo area glyphs
7065 and mini-buffer. */
7066 cursor_glyph = get_phys_cursor_glyph (w);
7067 if (cursor_glyph == NULL)
7068 return;
7070 /* If on an image, draw like a normal cursor. That's usually better
7071 visible than drawing a bar, esp. if the image is large so that
7072 the bar might not be in the window. */
7073 if (cursor_glyph->type == IMAGE_GLYPH)
7075 struct glyph_row *r;
7076 r = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7077 draw_phys_cursor_glyph (w, r, DRAW_CURSOR);
7079 else
7081 Display *dpy = FRAME_X_DISPLAY (f);
7082 Window window = FRAME_X_WINDOW (f);
7083 GC gc = FRAME_DISPLAY_INFO (f)->scratch_cursor_gc;
7084 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7085 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7086 XGCValues xgcv;
7088 /* If the glyph's background equals the color we normally draw
7089 the bars cursor in, the bar cursor in its normal color is
7090 invisible. Use the glyph's foreground color instead in this
7091 case, on the assumption that the glyph's colors are chosen so
7092 that the glyph is legible. */
7093 if (face->background == f->output_data.x->cursor_pixel)
7094 xgcv.background = xgcv.foreground = face->foreground;
7095 else
7096 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7097 xgcv.graphics_exposures = 0;
7099 if (gc)
7100 XChangeGC (dpy, gc, mask, &xgcv);
7101 else
7103 gc = XCreateGC (dpy, window, mask, &xgcv);
7104 FRAME_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7107 x_clip_to_row (w, row, TEXT_AREA, gc);
7109 if (kind == BAR_CURSOR)
7111 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
7113 if (width < 0)
7114 width = FRAME_CURSOR_WIDTH (f);
7115 width = min (cursor_glyph->pixel_width, width);
7117 w->phys_cursor_width = width;
7119 /* If the character under cursor is R2L, draw the bar cursor
7120 on the right of its glyph, rather than on the left. */
7121 if ((cursor_glyph->resolved_level & 1) != 0)
7122 x += cursor_glyph->pixel_width - width;
7124 XFillRectangle (dpy, window, gc, x,
7125 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7126 width, row->height);
7128 else
7130 int dummy_x, dummy_y, dummy_h;
7132 if (width < 0)
7133 width = row->height;
7135 width = min (row->height, width);
7137 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
7138 &dummy_y, &dummy_h);
7140 XFillRectangle (dpy, window, gc,
7141 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7142 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7143 row->height - width),
7144 w->phys_cursor_width, width);
7147 XSetClipMask (dpy, gc, None);
7152 /* RIF: Define cursor CURSOR on frame F. */
7154 static void
7155 x_define_frame_cursor (struct frame *f, Cursor cursor)
7157 if (!f->pointer_invisible
7158 && f->output_data.x->current_cursor != cursor)
7159 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7160 f->output_data.x->current_cursor = cursor;
7164 /* RIF: Clear area on frame F. */
7166 static void
7167 x_clear_frame_area (struct frame *f, int x, int y, int width, int height)
7169 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), x, y, width, height);
7170 #ifdef USE_GTK
7171 /* Must queue a redraw, because scroll bars might have been cleared. */
7172 if (FRAME_GTK_WIDGET (f))
7173 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
7174 #endif
7178 /* RIF: Draw cursor on window W. */
7180 static void
7181 x_draw_window_cursor (struct window *w, struct glyph_row *glyph_row, int x,
7182 int y, enum text_cursor_kinds cursor_type,
7183 int cursor_width, bool on_p, bool active_p)
7185 struct frame *f = XFRAME (WINDOW_FRAME (w));
7187 if (on_p)
7189 w->phys_cursor_type = cursor_type;
7190 w->phys_cursor_on_p = 1;
7192 if (glyph_row->exact_window_width_line_p
7193 && (glyph_row->reversed_p
7194 ? (w->phys_cursor.hpos < 0)
7195 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
7197 glyph_row->cursor_in_fringe_p = 1;
7198 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
7200 else
7202 switch (cursor_type)
7204 case HOLLOW_BOX_CURSOR:
7205 x_draw_hollow_cursor (w, glyph_row);
7206 break;
7208 case FILLED_BOX_CURSOR:
7209 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7210 break;
7212 case BAR_CURSOR:
7213 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7214 break;
7216 case HBAR_CURSOR:
7217 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7218 break;
7220 case NO_CURSOR:
7221 w->phys_cursor_width = 0;
7222 break;
7224 default:
7225 emacs_abort ();
7229 #ifdef HAVE_X_I18N
7230 if (w == XWINDOW (f->selected_window))
7231 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7232 xic_set_preeditarea (w, x, y);
7233 #endif
7236 XFlush (FRAME_X_DISPLAY (f));
7240 /* Icons. */
7242 /* Make the x-window of frame F use the gnu icon bitmap. */
7245 x_bitmap_icon (struct frame *f, Lisp_Object file)
7247 ptrdiff_t bitmap_id;
7249 if (FRAME_X_WINDOW (f) == 0)
7250 return 1;
7252 /* Free up our existing icon bitmap and mask if any. */
7253 if (f->output_data.x->icon_bitmap > 0)
7254 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7255 f->output_data.x->icon_bitmap = 0;
7257 if (STRINGP (file))
7259 #ifdef USE_GTK
7260 /* Use gtk_window_set_icon_from_file () if available,
7261 It's not restricted to bitmaps */
7262 if (xg_set_icon (f, file))
7263 return 0;
7264 #endif /* USE_GTK */
7265 bitmap_id = x_create_bitmap_from_file (f, file);
7266 x_create_bitmap_mask (f, bitmap_id);
7268 else
7270 /* Create the GNU bitmap and mask if necessary. */
7271 if (FRAME_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7273 ptrdiff_t rc = -1;
7275 #ifdef USE_GTK
7277 if (xg_set_icon (f, xg_default_icon_file)
7278 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7279 return 0;
7281 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7283 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7284 if (rc != -1)
7285 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7287 #endif
7289 /* If all else fails, use the (black and white) xbm image. */
7290 if (rc == -1)
7292 rc = x_create_bitmap_from_data (f, (char *) gnu_xbm_bits,
7293 gnu_xbm_width, gnu_xbm_height);
7294 if (rc == -1)
7295 return 1;
7297 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7298 x_create_bitmap_mask (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
7302 /* The first time we create the GNU bitmap and mask,
7303 this increments the ref-count one extra time.
7304 As a result, the GNU bitmap and mask are never freed.
7305 That way, we don't have to worry about allocating it again. */
7306 x_reference_bitmap (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
7308 bitmap_id = FRAME_DISPLAY_INFO (f)->icon_bitmap_id;
7311 x_wm_set_icon_pixmap (f, bitmap_id);
7312 f->output_data.x->icon_bitmap = bitmap_id;
7314 return 0;
7318 /* Make the x-window of frame F use a rectangle with text.
7319 Use ICON_NAME as the text. */
7322 x_text_icon (struct frame *f, const char *icon_name)
7324 if (FRAME_X_WINDOW (f) == 0)
7325 return 1;
7328 XTextProperty text;
7329 text.value = (unsigned char *) icon_name;
7330 text.encoding = XA_STRING;
7331 text.format = 8;
7332 text.nitems = strlen (icon_name);
7333 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7336 if (f->output_data.x->icon_bitmap > 0)
7337 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7338 f->output_data.x->icon_bitmap = 0;
7339 x_wm_set_icon_pixmap (f, 0);
7341 return 0;
7344 #define X_ERROR_MESSAGE_SIZE 200
7346 /* If non-nil, this should be a string.
7347 It means catch X errors and store the error message in this string.
7349 The reason we use a stack is that x_catch_error/x_uncatch_error can
7350 be called from a signal handler.
7353 struct x_error_message_stack {
7354 char string[X_ERROR_MESSAGE_SIZE];
7355 Display *dpy;
7356 struct x_error_message_stack *prev;
7358 static struct x_error_message_stack *x_error_message;
7360 /* An X error handler which stores the error message in
7361 *x_error_message. This is called from x_error_handler if
7362 x_catch_errors is in effect. */
7364 static void
7365 x_error_catcher (Display *display, XErrorEvent *event)
7367 XGetErrorText (display, event->error_code,
7368 x_error_message->string,
7369 X_ERROR_MESSAGE_SIZE);
7372 /* Begin trapping X errors for display DPY. Actually we trap X errors
7373 for all displays, but DPY should be the display you are actually
7374 operating on.
7376 After calling this function, X protocol errors no longer cause
7377 Emacs to exit; instead, they are recorded in the string
7378 stored in *x_error_message.
7380 Calling x_check_errors signals an Emacs error if an X error has
7381 occurred since the last call to x_catch_errors or x_check_errors.
7383 Calling x_uncatch_errors resumes the normal error handling. */
7385 void
7386 x_catch_errors (Display *dpy)
7388 struct x_error_message_stack *data = xmalloc (sizeof *data);
7390 /* Make sure any errors from previous requests have been dealt with. */
7391 XSync (dpy, False);
7393 data->dpy = dpy;
7394 data->string[0] = 0;
7395 data->prev = x_error_message;
7396 x_error_message = data;
7399 /* Undo the last x_catch_errors call.
7400 DPY should be the display that was passed to x_catch_errors. */
7402 void
7403 x_uncatch_errors (void)
7405 struct x_error_message_stack *tmp;
7407 block_input ();
7409 /* The display may have been closed before this function is called.
7410 Check if it is still open before calling XSync. */
7411 if (x_display_info_for_display (x_error_message->dpy) != 0)
7412 XSync (x_error_message->dpy, False);
7414 tmp = x_error_message;
7415 x_error_message = x_error_message->prev;
7416 xfree (tmp);
7417 unblock_input ();
7420 /* If any X protocol errors have arrived since the last call to
7421 x_catch_errors or x_check_errors, signal an Emacs error using
7422 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7424 void
7425 x_check_errors (Display *dpy, const char *format)
7427 /* Make sure to catch any errors incurred so far. */
7428 XSync (dpy, False);
7430 if (x_error_message->string[0])
7432 char string[X_ERROR_MESSAGE_SIZE];
7433 memcpy (string, x_error_message->string, X_ERROR_MESSAGE_SIZE);
7434 x_uncatch_errors ();
7435 error (format, string);
7439 /* Nonzero if we had any X protocol errors
7440 since we did x_catch_errors on DPY. */
7442 bool
7443 x_had_errors_p (Display *dpy)
7445 /* Make sure to catch any errors incurred so far. */
7446 XSync (dpy, False);
7448 return x_error_message->string[0] != 0;
7451 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7453 void
7454 x_clear_errors (Display *dpy)
7456 x_error_message->string[0] = 0;
7459 #if 0 /* See comment in unwind_to_catch why calling this is a bad
7460 * idea. --lorentey */
7461 /* Close off all unclosed x_catch_errors calls. */
7463 void
7464 x_fully_uncatch_errors (void)
7466 while (x_error_message)
7467 x_uncatch_errors ();
7469 #endif
7471 #if 0
7472 static unsigned int x_wire_count;
7473 x_trace_wire (void)
7475 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7477 #endif /* ! 0 */
7480 /************************************************************************
7481 Handling X errors
7482 ************************************************************************/
7484 /* Error message passed to x_connection_closed. */
7486 static char *error_msg;
7488 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7489 the text of an error message that lead to the connection loss. */
7491 static void
7492 x_connection_closed (Display *dpy, const char *error_message)
7494 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7495 Lisp_Object frame, tail;
7496 ptrdiff_t idx = SPECPDL_INDEX ();
7498 error_msg = alloca (strlen (error_message) + 1);
7499 strcpy (error_msg, error_message);
7501 /* Inhibit redisplay while frames are being deleted. */
7502 specbind (Qinhibit_redisplay, Qt);
7504 if (dpyinfo)
7506 /* Protect display from being closed when we delete the last
7507 frame on it. */
7508 dpyinfo->reference_count++;
7509 dpyinfo->terminal->reference_count++;
7512 /* First delete frames whose mini-buffers are on frames
7513 that are on the dead display. */
7514 FOR_EACH_FRAME (tail, frame)
7516 Lisp_Object minibuf_frame;
7517 minibuf_frame
7518 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7519 if (FRAME_X_P (XFRAME (frame))
7520 && FRAME_X_P (XFRAME (minibuf_frame))
7521 && ! EQ (frame, minibuf_frame)
7522 && FRAME_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7523 delete_frame (frame, Qnoelisp);
7526 /* Now delete all remaining frames on the dead display.
7527 We are now sure none of these is used as the mini-buffer
7528 for another frame that we need to delete. */
7529 FOR_EACH_FRAME (tail, frame)
7530 if (FRAME_X_P (XFRAME (frame))
7531 && FRAME_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7533 /* Set this to t so that delete_frame won't get confused
7534 trying to find a replacement. */
7535 kset_default_minibuffer_frame (FRAME_KBOARD (XFRAME (frame)), Qt);
7536 delete_frame (frame, Qnoelisp);
7539 /* If DPYINFO is null, this means we didn't open the display in the
7540 first place, so don't try to close it. */
7541 if (dpyinfo)
7543 /* We can not call XtCloseDisplay here because it calls XSync.
7544 XSync inside the error handler apparently hangs Emacs. On
7545 current Xt versions, this isn't needed either. */
7546 #ifdef USE_GTK
7547 /* A long-standing GTK bug prevents proper disconnect handling
7548 (https://bugzilla.gnome.org/show_bug.cgi?id=85715). Once,
7549 the resulting Glib error message loop filled a user's disk.
7550 To avoid this, kill Emacs unconditionally on disconnect. */
7551 shut_down_emacs (0, Qnil);
7552 fprintf (stderr, "%s\n\
7553 When compiled with GTK, Emacs cannot recover from X disconnects.\n\
7554 This is a GTK bug: https://bugzilla.gnome.org/show_bug.cgi?id=85715\n\
7555 For details, see etc/PROBLEMS.\n",
7556 error_msg);
7557 emacs_abort ();
7558 #endif /* USE_GTK */
7560 /* Indicate that this display is dead. */
7561 dpyinfo->display = 0;
7563 dpyinfo->reference_count--;
7564 dpyinfo->terminal->reference_count--;
7565 if (dpyinfo->reference_count != 0)
7566 /* We have just closed all frames on this display. */
7567 emacs_abort ();
7570 Lisp_Object tmp;
7571 XSETTERMINAL (tmp, dpyinfo->terminal);
7572 Fdelete_terminal (tmp, Qnoelisp);
7576 if (terminal_list == 0)
7578 fprintf (stderr, "%s\n", error_msg);
7579 Fkill_emacs (make_number (70));
7580 /* NOTREACHED */
7583 totally_unblock_input ();
7585 unbind_to (idx, Qnil);
7586 clear_waiting_for_input ();
7588 /* Tell GCC not to suggest attribute 'noreturn' for this function. */
7589 IF_LINT (if (! terminal_list) return; )
7591 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
7592 longjmp), because returning from this function would get us back into
7593 Xlib's code which will directly call `exit'. */
7594 error ("%s", error_msg);
7597 /* We specifically use it before defining it, so that gcc doesn't inline it,
7598 otherwise gdb doesn't know how to properly put a breakpoint on it. */
7599 static void x_error_quitter (Display *, XErrorEvent *);
7601 /* This is the first-level handler for X protocol errors.
7602 It calls x_error_quitter or x_error_catcher. */
7604 static int
7605 x_error_handler (Display *display, XErrorEvent *event)
7607 #if defined USE_GTK && defined HAVE_GTK3
7608 if ((event->error_code == BadMatch || event->error_code == BadWindow)
7609 && event->request_code == X_SetInputFocus)
7611 return 0;
7613 #endif
7615 if (x_error_message)
7616 x_error_catcher (display, event);
7617 else
7618 x_error_quitter (display, event);
7619 return 0;
7622 /* This is the usual handler for X protocol errors.
7623 It kills all frames on the display that we got the error for.
7624 If that was the only one, it prints an error message and kills Emacs. */
7626 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
7628 /* On older GCC versions, just putting x_error_quitter
7629 after x_error_handler prevents inlining into the former. */
7631 static void NO_INLINE
7632 x_error_quitter (Display *display, XErrorEvent *event)
7634 char buf[256], buf1[356];
7636 /* Ignore BadName errors. They can happen because of fonts
7637 or colors that are not defined. */
7639 if (event->error_code == BadName)
7640 return;
7642 /* Note that there is no real way portable across R3/R4 to get the
7643 original error handler. */
7645 XGetErrorText (display, event->error_code, buf, sizeof (buf));
7646 sprintf (buf1, "X protocol error: %s on protocol request %d",
7647 buf, event->request_code);
7648 x_connection_closed (display, buf1);
7652 /* This is the handler for X IO errors, always.
7653 It kills all frames on the display that we lost touch with.
7654 If that was the only one, it prints an error message and kills Emacs. */
7656 static int
7657 x_io_error_quitter (Display *display)
7659 char buf[256];
7661 snprintf (buf, sizeof buf, "Connection lost to X server `%s'",
7662 DisplayString (display));
7663 x_connection_closed (display, buf);
7664 return 0;
7667 /* Changing the font of the frame. */
7669 /* Give frame F the font FONT-OBJECT as its default font. The return
7670 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
7671 frame. If it is negative, generate a new fontset from
7672 FONT-OBJECT. */
7674 Lisp_Object
7675 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
7677 struct font *font = XFONT_OBJECT (font_object);
7679 if (fontset < 0)
7680 fontset = fontset_from_font (font_object);
7681 FRAME_FONTSET (f) = fontset;
7682 if (FRAME_FONT (f) == font)
7683 /* This font is already set in frame F. There's nothing more to
7684 do. */
7685 return font_object;
7687 FRAME_FONT (f) = font;
7688 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
7689 FRAME_COLUMN_WIDTH (f) = font->average_width;
7690 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (font);
7692 FRAME_TOOL_BAR_HEIGHT (f) = FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f);
7693 FRAME_MENU_BAR_HEIGHT (f) = FRAME_MENU_BAR_LINES (f) * FRAME_LINE_HEIGHT (f);
7695 compute_fringe_widths (f, 1);
7697 /* Compute the scroll bar width in character columns. */
7698 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
7700 int wid = FRAME_COLUMN_WIDTH (f);
7702 FRAME_CONFIG_SCROLL_BAR_COLS (f)
7703 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
7705 else
7707 int wid = FRAME_COLUMN_WIDTH (f);
7709 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = 14;
7710 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
7713 if (FRAME_X_WINDOW (f) != 0)
7715 /* Don't change the size of a tip frame; there's no point in
7716 doing it because it's done in Fx_show_tip, and it leads to
7717 problems because the tip frame has no widget. */
7718 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
7719 x_set_window_size (f, 0, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
7720 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 1);
7723 #ifdef HAVE_X_I18N
7724 if (FRAME_XIC (f)
7725 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
7727 block_input ();
7728 xic_set_xfontset (f, SSDATA (fontset_ascii (fontset)));
7729 unblock_input ();
7731 #endif
7733 return font_object;
7737 /***********************************************************************
7738 X Input Methods
7739 ***********************************************************************/
7741 #ifdef HAVE_X_I18N
7743 #ifdef HAVE_X11R6
7745 /* XIM destroy callback function, which is called whenever the
7746 connection to input method XIM dies. CLIENT_DATA contains a
7747 pointer to the x_display_info structure corresponding to XIM. */
7749 static void
7750 xim_destroy_callback (XIM xim, XPointer client_data, XPointer call_data)
7752 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
7753 Lisp_Object frame, tail;
7755 block_input ();
7757 /* No need to call XDestroyIC.. */
7758 FOR_EACH_FRAME (tail, frame)
7760 struct frame *f = XFRAME (frame);
7761 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
7763 FRAME_XIC (f) = NULL;
7764 xic_free_xfontset (f);
7768 /* No need to call XCloseIM. */
7769 dpyinfo->xim = NULL;
7770 XFree (dpyinfo->xim_styles);
7771 unblock_input ();
7774 #endif /* HAVE_X11R6 */
7776 #ifdef HAVE_X11R6
7777 /* This isn't prototyped in OSF 5.0 or 5.1a. */
7778 extern char *XSetIMValues (XIM, ...);
7779 #endif
7781 /* Open the connection to the XIM server on display DPYINFO.
7782 RESOURCE_NAME is the resource name Emacs uses. */
7784 static void
7785 xim_open_dpy (struct x_display_info *dpyinfo, char *resource_name)
7787 XIM xim;
7789 #ifdef HAVE_XIM
7790 if (use_xim)
7792 if (dpyinfo->xim)
7793 XCloseIM (dpyinfo->xim);
7794 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
7795 emacs_class);
7796 dpyinfo->xim = xim;
7798 if (xim)
7800 #ifdef HAVE_X11R6
7801 XIMCallback destroy;
7802 #endif
7804 /* Get supported styles and XIM values. */
7805 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
7807 #ifdef HAVE_X11R6
7808 destroy.callback = xim_destroy_callback;
7809 destroy.client_data = (XPointer)dpyinfo;
7810 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
7811 #endif
7815 else
7816 #endif /* HAVE_XIM */
7817 dpyinfo->xim = NULL;
7821 #ifdef HAVE_X11R6_XIM
7823 /* XIM instantiate callback function, which is called whenever an XIM
7824 server is available. DISPLAY is the display of the XIM.
7825 CLIENT_DATA contains a pointer to an xim_inst_t structure created
7826 when the callback was registered. */
7828 static void
7829 xim_instantiate_callback (Display *display, XPointer client_data, XPointer call_data)
7831 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
7832 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
7834 /* We don't support multiple XIM connections. */
7835 if (dpyinfo->xim)
7836 return;
7838 xim_open_dpy (dpyinfo, xim_inst->resource_name);
7840 /* Create XIC for the existing frames on the same display, as long
7841 as they have no XIC. */
7842 if (dpyinfo->xim && dpyinfo->reference_count > 0)
7844 Lisp_Object tail, frame;
7846 block_input ();
7847 FOR_EACH_FRAME (tail, frame)
7849 struct frame *f = XFRAME (frame);
7851 if (FRAME_X_P (f)
7852 && FRAME_DISPLAY_INFO (f) == xim_inst->dpyinfo)
7853 if (FRAME_XIC (f) == NULL)
7855 create_frame_xic (f);
7856 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
7857 xic_set_statusarea (f);
7858 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
7860 struct window *w = XWINDOW (f->selected_window);
7861 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
7866 unblock_input ();
7870 #endif /* HAVE_X11R6_XIM */
7873 /* Open a connection to the XIM server on display DPYINFO.
7874 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
7875 connection only at the first time. On X11R6, open the connection
7876 in the XIM instantiate callback function. */
7878 static void
7879 xim_initialize (struct x_display_info *dpyinfo, char *resource_name)
7881 dpyinfo->xim = NULL;
7882 #ifdef HAVE_XIM
7883 if (use_xim)
7885 #ifdef HAVE_X11R6_XIM
7886 struct xim_inst_t *xim_inst = xmalloc (sizeof *xim_inst);
7888 dpyinfo->xim_callback_data = xim_inst;
7889 xim_inst->dpyinfo = dpyinfo;
7890 xim_inst->resource_name = xstrdup (resource_name);
7891 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
7892 resource_name, emacs_class,
7893 xim_instantiate_callback,
7894 /* This is XPointer in XFree86
7895 but (XPointer *) on Tru64, at
7896 least, hence the configure test. */
7897 (XRegisterIMInstantiateCallback_arg6) xim_inst);
7898 #else /* not HAVE_X11R6_XIM */
7899 xim_open_dpy (dpyinfo, resource_name);
7900 #endif /* not HAVE_X11R6_XIM */
7902 #endif /* HAVE_XIM */
7906 /* Close the connection to the XIM server on display DPYINFO. */
7908 static void
7909 xim_close_dpy (struct x_display_info *dpyinfo)
7911 #ifdef HAVE_XIM
7912 if (use_xim)
7914 #ifdef HAVE_X11R6_XIM
7915 if (dpyinfo->display)
7916 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
7917 NULL, emacs_class,
7918 xim_instantiate_callback, NULL);
7919 xfree (dpyinfo->xim_callback_data->resource_name);
7920 xfree (dpyinfo->xim_callback_data);
7921 #endif /* HAVE_X11R6_XIM */
7922 if (dpyinfo->display)
7923 XCloseIM (dpyinfo->xim);
7924 dpyinfo->xim = NULL;
7925 XFree (dpyinfo->xim_styles);
7927 #endif /* HAVE_XIM */
7930 #endif /* not HAVE_X11R6_XIM */
7934 /* Calculate the absolute position in frame F
7935 from its current recorded position values and gravity. */
7937 static void
7938 x_calc_absolute_position (struct frame *f)
7940 int flags = f->size_hint_flags;
7942 /* We have nothing to do if the current position
7943 is already for the top-left corner. */
7944 if (! ((flags & XNegative) || (flags & YNegative)))
7945 return;
7947 /* Treat negative positions as relative to the leftmost bottommost
7948 position that fits on the screen. */
7949 if (flags & XNegative)
7950 f->left_pos = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
7951 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
7954 int height = FRAME_PIXEL_HEIGHT (f);
7956 #if defined USE_X_TOOLKIT && defined USE_MOTIF
7957 /* Something is fishy here. When using Motif, starting Emacs with
7958 `-g -0-0', the frame appears too low by a few pixels.
7960 This seems to be so because initially, while Emacs is starting,
7961 the column widget's height and the frame's pixel height are
7962 different. The column widget's height is the right one. In
7963 later invocations, when Emacs is up, the frame's pixel height
7964 is right, though.
7966 It's not obvious where the initial small difference comes from.
7967 2000-12-01, gerd. */
7969 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
7970 #endif
7972 if (flags & YNegative)
7973 f->top_pos = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
7974 - height + f->top_pos;
7977 /* The left_pos and top_pos
7978 are now relative to the top and left screen edges,
7979 so the flags should correspond. */
7980 f->size_hint_flags &= ~ (XNegative | YNegative);
7983 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
7984 to really change the position, and 0 when calling from
7985 x_make_frame_visible (in that case, XOFF and YOFF are the current
7986 position values). It is -1 when calling from x_set_frame_parameters,
7987 which means, do adjust for borders but don't change the gravity. */
7989 void
7990 x_set_offset (struct frame *f, register int xoff, register int yoff, int change_gravity)
7992 int modified_top, modified_left;
7994 if (change_gravity > 0)
7996 f->top_pos = yoff;
7997 f->left_pos = xoff;
7998 f->size_hint_flags &= ~ (XNegative | YNegative);
7999 if (xoff < 0)
8000 f->size_hint_flags |= XNegative;
8001 if (yoff < 0)
8002 f->size_hint_flags |= YNegative;
8003 f->win_gravity = NorthWestGravity;
8005 x_calc_absolute_position (f);
8007 block_input ();
8008 x_wm_set_size_hint (f, (long) 0, 0);
8010 modified_left = f->left_pos;
8011 modified_top = f->top_pos;
8013 if (change_gravity != 0 && FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8015 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8016 than the WM decorations. So we use the calculated offset instead
8017 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8018 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8019 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8022 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8023 modified_left, modified_top);
8025 x_sync_with_move (f, f->left_pos, f->top_pos,
8026 FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8027 ? 1 : 0);
8029 /* change_gravity is non-zero when this function is called from Lisp to
8030 programmatically move a frame. In that case, we call
8031 x_check_expected_move to discover if we have a "Type A" or "Type B"
8032 window manager, and, for a "Type A" window manager, adjust the position
8033 of the frame.
8035 We call x_check_expected_move if a programmatic move occurred, and
8036 either the window manager type (A/B) is unknown or it is Type A but we
8037 need to compute the top/left offset adjustment for this frame. */
8039 if (change_gravity != 0 &&
8040 (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8041 || (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8042 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8043 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8044 x_check_expected_move (f, modified_left, modified_top);
8046 unblock_input ();
8049 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8050 on the root window for frame F contains ATOMNAME.
8051 This is how a WM check shall be done according to the Window Manager
8052 Specification/Extended Window Manager Hints at
8053 http://freedesktop.org/wiki/Specifications/wm-spec. */
8055 static int
8056 wm_supports (struct frame *f, Atom want_atom)
8058 Atom actual_type;
8059 unsigned long actual_size, bytes_remaining;
8060 int i, rc, actual_format;
8061 Window wmcheck_window;
8062 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8063 Window target_window = dpyinfo->root_window;
8064 long max_len = 65536;
8065 Display *dpy = FRAME_X_DISPLAY (f);
8066 unsigned char *tmp_data = NULL;
8067 Atom target_type = XA_WINDOW;
8069 block_input ();
8071 x_catch_errors (dpy);
8072 rc = XGetWindowProperty (dpy, target_window,
8073 dpyinfo->Xatom_net_supporting_wm_check,
8074 0, max_len, False, target_type,
8075 &actual_type, &actual_format, &actual_size,
8076 &bytes_remaining, &tmp_data);
8078 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8080 if (tmp_data) XFree (tmp_data);
8081 x_uncatch_errors ();
8082 unblock_input ();
8083 return 0;
8086 wmcheck_window = *(Window *) tmp_data;
8087 XFree (tmp_data);
8089 /* Check if window exists. */
8090 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8091 x_sync (f);
8092 if (x_had_errors_p (dpy))
8094 x_uncatch_errors ();
8095 unblock_input ();
8096 return 0;
8099 if (dpyinfo->net_supported_window != wmcheck_window)
8101 /* Window changed, reload atoms */
8102 if (dpyinfo->net_supported_atoms != NULL)
8103 XFree (dpyinfo->net_supported_atoms);
8104 dpyinfo->net_supported_atoms = NULL;
8105 dpyinfo->nr_net_supported_atoms = 0;
8106 dpyinfo->net_supported_window = 0;
8108 target_type = XA_ATOM;
8109 tmp_data = NULL;
8110 rc = XGetWindowProperty (dpy, target_window,
8111 dpyinfo->Xatom_net_supported,
8112 0, max_len, False, target_type,
8113 &actual_type, &actual_format, &actual_size,
8114 &bytes_remaining, &tmp_data);
8116 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
8118 if (tmp_data) XFree (tmp_data);
8119 x_uncatch_errors ();
8120 unblock_input ();
8121 return 0;
8124 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8125 dpyinfo->nr_net_supported_atoms = actual_size;
8126 dpyinfo->net_supported_window = wmcheck_window;
8129 rc = 0;
8131 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
8132 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8134 x_uncatch_errors ();
8135 unblock_input ();
8137 return rc;
8140 static void
8141 set_wm_state (Lisp_Object frame, int add, Atom atom, Atom value)
8143 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (XFRAME (frame));
8145 x_send_client_event (frame, make_number (0), frame,
8146 dpyinfo->Xatom_net_wm_state,
8147 make_number (32),
8148 /* 1 = add, 0 = remove */
8149 Fcons
8150 (make_number (add ? 1 : 0),
8151 Fcons
8152 (make_fixnum_or_float (atom),
8153 (value != 0
8154 ? list1 (make_fixnum_or_float (value))
8155 : Qnil))));
8158 void
8159 x_set_sticky (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
8161 Lisp_Object frame;
8162 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8164 XSETFRAME (frame, f);
8166 set_wm_state (frame, NILP (new_value) ? 0 : 1,
8167 dpyinfo->Xatom_net_wm_state_sticky, None);
8170 /* Return the current _NET_WM_STATE.
8171 SIZE_STATE is set to one of the FULLSCREEN_* values.
8172 STICKY is set to 1 if the sticky state is set, 0 if not.
8174 Return non-zero if we are not hidden, zero if we are. */
8176 static int
8177 get_current_wm_state (struct frame *f,
8178 Window window,
8179 int *size_state,
8180 int *sticky)
8182 Atom actual_type;
8183 unsigned long actual_size, bytes_remaining;
8184 int i, rc, actual_format, is_hidden = 0;
8185 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8186 long max_len = 65536;
8187 Display *dpy = FRAME_X_DISPLAY (f);
8188 unsigned char *tmp_data = NULL;
8189 Atom target_type = XA_ATOM;
8191 *sticky = 0;
8192 *size_state = FULLSCREEN_NONE;
8194 block_input ();
8195 x_catch_errors (dpy);
8196 rc = XGetWindowProperty (dpy, window, dpyinfo->Xatom_net_wm_state,
8197 0, max_len, False, target_type,
8198 &actual_type, &actual_format, &actual_size,
8199 &bytes_remaining, &tmp_data);
8201 if (rc != Success || actual_type != target_type || x_had_errors_p (dpy))
8203 if (tmp_data) XFree (tmp_data);
8204 x_uncatch_errors ();
8205 unblock_input ();
8206 return !FRAME_ICONIFIED_P (f);
8209 x_uncatch_errors ();
8211 for (i = 0; i < actual_size; ++i)
8213 Atom a = ((Atom*)tmp_data)[i];
8214 if (a == dpyinfo->Xatom_net_wm_state_hidden)
8216 is_hidden = 1;
8217 f->output_data.x->net_wm_state_hidden_seen = 1;
8219 else if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
8221 if (*size_state == FULLSCREEN_HEIGHT)
8222 *size_state = FULLSCREEN_MAXIMIZED;
8223 else
8224 *size_state = FULLSCREEN_WIDTH;
8226 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
8228 if (*size_state == FULLSCREEN_WIDTH)
8229 *size_state = FULLSCREEN_MAXIMIZED;
8230 else
8231 *size_state = FULLSCREEN_HEIGHT;
8233 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen)
8234 *size_state = FULLSCREEN_BOTH;
8235 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
8236 *sticky = 1;
8239 if (tmp_data) XFree (tmp_data);
8240 unblock_input ();
8241 return ! is_hidden;
8244 /* Do fullscreen as specified in extended window manager hints */
8246 static int
8247 do_ewmh_fullscreen (struct frame *f)
8249 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8250 int have_net_atom = wm_supports (f, dpyinfo->Xatom_net_wm_state);
8251 int cur, dummy;
8253 (void)get_current_wm_state (f, FRAME_OUTER_WINDOW (f), &cur, &dummy);
8255 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8256 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8257 if (!have_net_atom)
8258 have_net_atom = wm_supports (f, dpyinfo->Xatom_net_wm_state_fullscreen);
8260 if (have_net_atom && cur != f->want_fullscreen)
8262 Lisp_Object frame;
8264 XSETFRAME (frame, f);
8266 /* Keep number of calls to set_wm_state as low as possible.
8267 Some window managers, or possible Gtk+, hangs when too many
8268 are sent at once. */
8269 switch (f->want_fullscreen)
8271 case FULLSCREEN_BOTH:
8272 if (cur == FULLSCREEN_WIDTH || cur == FULLSCREEN_MAXIMIZED
8273 || cur == FULLSCREEN_HEIGHT)
8274 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_maximized_horz,
8275 dpyinfo->Xatom_net_wm_state_maximized_vert);
8276 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_fullscreen, None);
8277 break;
8278 case FULLSCREEN_WIDTH:
8279 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_HEIGHT
8280 || cur == FULLSCREEN_MAXIMIZED)
8281 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen,
8282 dpyinfo->Xatom_net_wm_state_maximized_vert);
8283 if (cur != FULLSCREEN_MAXIMIZED)
8284 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_maximized_horz, None);
8285 break;
8286 case FULLSCREEN_HEIGHT:
8287 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_WIDTH
8288 || cur == FULLSCREEN_MAXIMIZED)
8289 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen,
8290 dpyinfo->Xatom_net_wm_state_maximized_horz);
8291 if (cur != FULLSCREEN_MAXIMIZED)
8292 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_maximized_vert, None);
8293 break;
8294 case FULLSCREEN_MAXIMIZED:
8295 if (cur == FULLSCREEN_BOTH)
8296 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen, None);
8297 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_maximized_horz,
8298 dpyinfo->Xatom_net_wm_state_maximized_vert);
8299 break;
8300 case FULLSCREEN_NONE:
8301 if (cur == FULLSCREEN_BOTH)
8302 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen, None);
8303 else
8304 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_maximized_horz,
8305 dpyinfo->Xatom_net_wm_state_maximized_vert);
8308 f->want_fullscreen = FULLSCREEN_NONE;
8312 return have_net_atom;
8315 static void
8316 XTfullscreen_hook (struct frame *f)
8318 if (FRAME_VISIBLE_P (f))
8320 block_input ();
8321 x_check_fullscreen (f);
8322 x_sync (f);
8323 unblock_input ();
8328 static int
8329 x_handle_net_wm_state (struct frame *f, const XPropertyEvent *event)
8331 int value = FULLSCREEN_NONE;
8332 Lisp_Object lval;
8333 int sticky = 0;
8334 int not_hidden = get_current_wm_state (f, event->window, &value, &sticky);
8336 lval = Qnil;
8337 switch (value)
8339 case FULLSCREEN_WIDTH:
8340 lval = Qfullwidth;
8341 break;
8342 case FULLSCREEN_HEIGHT:
8343 lval = Qfullheight;
8344 break;
8345 case FULLSCREEN_BOTH:
8346 lval = Qfullboth;
8347 break;
8348 case FULLSCREEN_MAXIMIZED:
8349 lval = Qmaximized;
8350 break;
8353 store_frame_param (f, Qfullscreen, lval);
8354 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
8356 return not_hidden;
8359 /* Check if we need to resize the frame due to a fullscreen request.
8360 If so needed, resize the frame. */
8361 static void
8362 x_check_fullscreen (struct frame *f)
8364 if (do_ewmh_fullscreen (f))
8365 return;
8367 if (f->output_data.x->parent_desc != FRAME_DISPLAY_INFO (f)->root_window)
8368 return; /* Only fullscreen without WM or with EWM hints (above). */
8370 /* Setting fullscreen to nil doesn't do anything. We could save the
8371 last non-fullscreen size and restore it, but it seems like a
8372 lot of work for this unusual case (no window manager running). */
8374 if (f->want_fullscreen != FULLSCREEN_NONE)
8376 int width = FRAME_PIXEL_WIDTH (f), height = FRAME_PIXEL_HEIGHT (f);
8377 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8379 switch (f->want_fullscreen)
8381 /* No difference between these two when there is no WM */
8382 case FULLSCREEN_BOTH:
8383 case FULLSCREEN_MAXIMIZED:
8384 width = x_display_pixel_width (dpyinfo);
8385 height = x_display_pixel_height (dpyinfo);
8386 break;
8387 case FULLSCREEN_WIDTH:
8388 width = x_display_pixel_width (dpyinfo);
8389 break;
8390 case FULLSCREEN_HEIGHT:
8391 height = x_display_pixel_height (dpyinfo);
8394 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8395 width, height);
8399 /* This function is called by x_set_offset to determine whether the window
8400 manager interfered with the positioning of the frame. Type A window
8401 managers position the surrounding window manager decorations a small
8402 amount above and left of the user-supplied position. Type B window
8403 managers position the surrounding window manager decorations at the
8404 user-specified position. If we detect a Type A window manager, we
8405 compensate by moving the window right and down by the proper amount. */
8407 static void
8408 x_check_expected_move (struct frame *f, int expected_left, int expected_top)
8410 int current_left = 0, current_top = 0;
8412 /* x_real_positions returns the left and top offsets of the outermost
8413 window manager window around the frame. */
8415 x_real_positions (f, &current_left, &current_top);
8417 if (current_left != expected_left || current_top != expected_top)
8419 /* It's a "Type A" window manager. */
8421 int adjusted_left;
8422 int adjusted_top;
8424 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8425 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
8426 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
8428 /* Now fix the mispositioned frame's location. */
8430 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
8431 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
8433 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8434 adjusted_left, adjusted_top);
8436 x_sync_with_move (f, expected_left, expected_top, 0);
8438 else
8439 /* It's a "Type B" window manager. We don't have to adjust the
8440 frame's position. */
8442 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8446 /* Wait for XGetGeometry to return up-to-date position information for a
8447 recently-moved frame. Call this immediately after calling XMoveWindow.
8448 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
8449 frame has been moved to, so we use a fuzzy position comparison instead
8450 of an exact comparison. */
8452 static void
8453 x_sync_with_move (struct frame *f, int left, int top, int fuzzy)
8455 int count = 0;
8457 while (count++ < 50)
8459 int current_left = 0, current_top = 0;
8461 /* In theory, this call to XSync only needs to happen once, but in
8462 practice, it doesn't seem to work, hence the need for the surrounding
8463 loop. */
8465 XSync (FRAME_X_DISPLAY (f), False);
8466 x_real_positions (f, &current_left, &current_top);
8468 if (fuzzy)
8470 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
8471 pixels. */
8473 if (eabs (current_left - left) <= 10
8474 && eabs (current_top - top) <= 40)
8475 return;
8477 else if (current_left == left && current_top == top)
8478 return;
8481 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
8482 will then return up-to-date position info. */
8484 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
8488 /* Wait for an event on frame F matching EVENTTYPE. */
8489 void
8490 x_wait_for_event (struct frame *f, int eventtype)
8492 int level = interrupt_input_blocked;
8494 fd_set fds;
8495 struct timespec tmo, tmo_at, time_now;
8496 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
8498 f->wait_event_type = eventtype;
8500 /* Set timeout to 0.1 second. Hopefully not noticeable.
8501 Maybe it should be configurable. */
8502 tmo = make_timespec (0, 100 * 1000 * 1000);
8503 tmo_at = timespec_add (current_timespec (), tmo);
8505 while (f->wait_event_type)
8507 pending_signals = 1;
8508 totally_unblock_input ();
8509 /* XTread_socket is called after unblock. */
8510 block_input ();
8511 interrupt_input_blocked = level;
8513 FD_ZERO (&fds);
8514 FD_SET (fd, &fds);
8516 time_now = current_timespec ();
8517 if (timespec_cmp (tmo_at, time_now) < 0)
8518 break;
8520 tmo = timespec_sub (tmo_at, time_now);
8521 if (pselect (fd + 1, &fds, NULL, NULL, &tmo, NULL) == 0)
8522 break; /* Timeout */
8525 f->wait_event_type = 0;
8529 /* Change the size of frame F's X window to COLS/ROWS in the case F
8530 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8531 top-left-corner window gravity for this size change and subsequent
8532 size changes. Otherwise we leave the window gravity unchanged. */
8534 static void
8535 x_set_window_size_1 (struct frame *f, int change_gravity, int width, int height, bool pixelwise)
8537 int pixelwidth, pixelheight;
8539 check_frame_size (f, &width, &height, pixelwise);
8541 compute_fringe_widths (f, 0);
8543 pixelwidth = ((pixelwise
8544 ? FRAME_TEXT_TO_PIXEL_WIDTH (f, width)
8545 : FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, width))
8546 + FRAME_TOOLBAR_WIDTH (f));
8547 pixelheight = ((pixelwise
8548 ? FRAME_TEXT_TO_PIXEL_HEIGHT (f, height)
8549 : FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, height))
8550 + FRAME_MENUBAR_HEIGHT (f)
8551 + FRAME_TOOLBAR_HEIGHT (f));
8552 if (change_gravity) f->win_gravity = NorthWestGravity;
8553 x_wm_set_size_hint (f, (long) 0, 0);
8554 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8555 pixelwidth, pixelheight);
8558 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8559 receive in the ConfigureNotify event; if we get what we asked
8560 for, then the event won't cause the screen to become garbaged, so
8561 we have to make sure to do it here. */
8562 SET_FRAME_GARBAGED (f);
8564 /* Now, strictly speaking, we can't be sure that this is accurate,
8565 but the window manager will get around to dealing with the size
8566 change request eventually, and we'll hear how it went when the
8567 ConfigureNotify event gets here.
8569 We could just not bother storing any of this information here,
8570 and let the ConfigureNotify event set everything up, but that
8571 might be kind of confusing to the Lisp code, since size changes
8572 wouldn't be reported in the frame parameters until some random
8573 point in the future when the ConfigureNotify event arrives.
8575 We pass 1 for DELAY since we can't run Lisp code inside of
8576 a BLOCK_INPUT. */
8578 /* But the ConfigureNotify may in fact never arrive, and then this is
8579 not right if the frame is visible. Instead wait (with timeout)
8580 for the ConfigureNotify. */
8581 if (FRAME_VISIBLE_P (f))
8582 x_wait_for_event (f, ConfigureNotify);
8583 else
8585 change_frame_size (f, width, height, 0, 1, 0, 1);
8586 x_sync (f);
8591 /* Call this to change the size of frame F's x-window.
8592 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8593 for this size change and subsequent size changes.
8594 Otherwise we leave the window gravity unchanged. */
8596 void
8597 x_set_window_size (struct frame *f, int change_gravity, int width, int height, bool pixelwise)
8599 block_input ();
8601 check_frame_size (f, &width, &height, pixelwise);
8603 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8605 int text_width, text_height;
8607 /* When the frame is maximized/fullscreen or running under for
8608 example Xmonad, x_set_window_size_1 will be a no-op.
8609 In that case, the right thing to do is extend rows/width to
8610 the current frame size. We do that first if x_set_window_size_1
8611 turns out to not be a no-op (there is no way to know).
8612 The size will be adjusted again if the frame gets a
8613 ConfigureNotify event as a result of x_set_window_size. */
8614 int pixelh = FRAME_PIXEL_HEIGHT (f);
8615 #ifdef USE_X_TOOLKIT
8616 /* The menu bar is not part of text lines. The tool bar
8617 is however. */
8618 pixelh -= FRAME_MENUBAR_HEIGHT (f);
8619 #endif
8620 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, FRAME_PIXEL_WIDTH (f));
8621 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelh);
8623 change_frame_size (f, text_width, text_height, 0, 1, 0, 1);
8626 #ifdef USE_GTK
8627 if (FRAME_GTK_WIDGET (f))
8628 if (! pixelwise)
8629 xg_frame_set_char_size (f, width * FRAME_COLUMN_WIDTH (f),
8630 height * FRAME_LINE_HEIGHT (f));
8631 else
8632 xg_frame_set_char_size (f, width, height);
8633 else
8634 x_set_window_size_1 (f, change_gravity, width, height, pixelwise);
8635 #else /* not USE_GTK */
8637 x_set_window_size_1 (f, change_gravity, width, height, pixelwise);
8639 #endif /* not USE_GTK */
8641 /* If cursor was outside the new size, mark it as off. */
8642 mark_window_cursors_off (XWINDOW (f->root_window));
8644 /* Clear out any recollection of where the mouse highlighting was,
8645 since it might be in a place that's outside the new frame size.
8646 Actually checking whether it is outside is a pain in the neck,
8647 so don't try--just let the highlighting be done afresh with new size. */
8648 cancel_mouse_face (f);
8650 unblock_input ();
8653 /* Mouse warping. */
8655 void
8656 x_set_mouse_position (struct frame *f, int x, int y)
8658 int pix_x, pix_y;
8660 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
8661 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
8663 if (pix_x < 0) pix_x = 0;
8664 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
8666 if (pix_y < 0) pix_y = 0;
8667 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
8669 block_input ();
8671 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8672 0, 0, 0, 0, pix_x, pix_y);
8673 unblock_input ();
8676 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
8678 void
8679 x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
8681 block_input ();
8683 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8684 0, 0, 0, 0, pix_x, pix_y);
8685 unblock_input ();
8688 /* Raise frame F. */
8690 void
8691 x_raise_frame (struct frame *f)
8693 block_input ();
8694 if (FRAME_VISIBLE_P (f))
8695 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8696 XFlush (FRAME_X_DISPLAY (f));
8697 unblock_input ();
8700 /* Lower frame F. */
8702 static void
8703 x_lower_frame (struct frame *f)
8705 if (FRAME_VISIBLE_P (f))
8707 block_input ();
8708 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8709 XFlush (FRAME_X_DISPLAY (f));
8710 unblock_input ();
8714 /* Request focus with XEmbed */
8716 void
8717 xembed_request_focus (struct frame *f)
8719 /* See XEmbed Protocol Specification at
8720 http://freedesktop.org/wiki/Specifications/xembed-spec */
8721 if (FRAME_VISIBLE_P (f))
8722 xembed_send_message (f, CurrentTime,
8723 XEMBED_REQUEST_FOCUS, 0, 0, 0);
8726 /* Activate frame with Extended Window Manager Hints */
8728 void
8729 x_ewmh_activate_frame (struct frame *f)
8731 /* See Window Manager Specification/Extended Window Manager Hints at
8732 http://freedesktop.org/wiki/Specifications/wm-spec */
8734 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8736 if (FRAME_VISIBLE_P (f) && wm_supports (f, dpyinfo->Xatom_net_active_window))
8738 Lisp_Object frame;
8739 XSETFRAME (frame, f);
8740 x_send_client_event (frame, make_number (0), frame,
8741 dpyinfo->Xatom_net_active_window,
8742 make_number (32),
8743 list2i (1, dpyinfo->last_user_time));
8747 static void
8748 XTframe_raise_lower (struct frame *f, int raise_flag)
8750 if (raise_flag)
8751 x_raise_frame (f);
8752 else
8753 x_lower_frame (f);
8756 /* XEmbed implementation. */
8758 #if defined USE_X_TOOLKIT || ! defined USE_GTK
8760 /* XEmbed implementation. */
8762 #define XEMBED_VERSION 0
8764 static void
8765 xembed_set_info (struct frame *f, enum xembed_info flags)
8767 unsigned long data[2];
8768 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8770 data[0] = XEMBED_VERSION;
8771 data[1] = flags;
8773 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8774 dpyinfo->Xatom_XEMBED_INFO, dpyinfo->Xatom_XEMBED_INFO,
8775 32, PropModeReplace, (unsigned char *) data, 2);
8777 #endif /* defined USE_X_TOOLKIT || ! defined USE_GTK */
8779 static void
8780 xembed_send_message (struct frame *f, Time t, enum xembed_message msg,
8781 long int detail, long int data1, long int data2)
8783 XEvent event;
8785 event.xclient.type = ClientMessage;
8786 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
8787 event.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_XEMBED;
8788 event.xclient.format = 32;
8789 event.xclient.data.l[0] = t;
8790 event.xclient.data.l[1] = msg;
8791 event.xclient.data.l[2] = detail;
8792 event.xclient.data.l[3] = data1;
8793 event.xclient.data.l[4] = data2;
8795 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
8796 False, NoEventMask, &event);
8797 XSync (FRAME_X_DISPLAY (f), False);
8800 /* Change of visibility. */
8802 /* This tries to wait until the frame is really visible.
8803 However, if the window manager asks the user where to position
8804 the frame, this will return before the user finishes doing that.
8805 The frame will not actually be visible at that time,
8806 but it will become visible later when the window manager
8807 finishes with it. */
8809 void
8810 x_make_frame_visible (struct frame *f)
8812 int original_top, original_left;
8814 block_input ();
8816 x_set_bitmap_icon (f);
8818 if (! FRAME_VISIBLE_P (f))
8820 /* We test FRAME_GARBAGED_P here to make sure we don't
8821 call x_set_offset a second time
8822 if we get to x_make_frame_visible a second time
8823 before the window gets really visible. */
8824 if (! FRAME_ICONIFIED_P (f)
8825 && ! FRAME_X_EMBEDDED_P (f)
8826 && ! f->output_data.x->asked_for_visible)
8827 x_set_offset (f, f->left_pos, f->top_pos, 0);
8829 f->output_data.x->asked_for_visible = 1;
8831 if (! EQ (Vx_no_window_manager, Qt))
8832 x_wm_set_window_state (f, NormalState);
8833 #ifdef USE_X_TOOLKIT
8834 if (FRAME_X_EMBEDDED_P (f))
8835 xembed_set_info (f, XEMBED_MAPPED);
8836 else
8838 /* This was XtPopup, but that did nothing for an iconified frame. */
8839 XtMapWidget (f->output_data.x->widget);
8841 #else /* not USE_X_TOOLKIT */
8842 #ifdef USE_GTK
8843 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
8844 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
8845 #else
8846 if (FRAME_X_EMBEDDED_P (f))
8847 xembed_set_info (f, XEMBED_MAPPED);
8848 else
8849 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8850 #endif /* not USE_GTK */
8851 #endif /* not USE_X_TOOLKIT */
8854 XFlush (FRAME_X_DISPLAY (f));
8856 /* Synchronize to ensure Emacs knows the frame is visible
8857 before we do anything else. We do this loop with input not blocked
8858 so that incoming events are handled. */
8860 Lisp_Object frame;
8861 /* This must be before UNBLOCK_INPUT
8862 since events that arrive in response to the actions above
8863 will set it when they are handled. */
8864 int previously_visible = f->output_data.x->has_been_visible;
8866 original_left = f->left_pos;
8867 original_top = f->top_pos;
8869 /* This must come after we set COUNT. */
8870 unblock_input ();
8872 /* We unblock here so that arriving X events are processed. */
8874 /* Now move the window back to where it was "supposed to be".
8875 But don't do it if the gravity is negative.
8876 When the gravity is negative, this uses a position
8877 that is 3 pixels too low. Perhaps that's really the border width.
8879 Don't do this if the window has never been visible before,
8880 because the window manager may choose the position
8881 and we don't want to override it. */
8883 if (! FRAME_VISIBLE_P (f)
8884 && ! FRAME_ICONIFIED_P (f)
8885 && ! FRAME_X_EMBEDDED_P (f)
8886 && f->win_gravity == NorthWestGravity
8887 && previously_visible)
8889 Drawable rootw;
8890 int x, y;
8891 unsigned int width, height, border, depth;
8893 block_input ();
8895 /* On some window managers (such as FVWM) moving an existing
8896 window, even to the same place, causes the window manager
8897 to introduce an offset. This can cause the window to move
8898 to an unexpected location. Check the geometry (a little
8899 slow here) and then verify that the window is in the right
8900 place. If the window is not in the right place, move it
8901 there, and take the potential window manager hit. */
8902 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8903 &rootw, &x, &y, &width, &height, &border, &depth);
8905 if (original_left != x || original_top != y)
8906 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8907 original_left, original_top);
8909 unblock_input ();
8912 XSETFRAME (frame, f);
8914 /* Process X events until a MapNotify event has been seen. */
8915 while (!FRAME_VISIBLE_P (f))
8917 /* Force processing of queued events. */
8918 x_sync (f);
8920 /* This hack is still in use at least for Cygwin. See
8921 http://lists.gnu.org/archive/html/emacs-devel/2013-12/msg00351.html.
8923 Machines that do polling rather than SIGIO have been
8924 observed to go into a busy-wait here. So we'll fake an
8925 alarm signal to let the handler know that there's something
8926 to be read. We used to raise a real alarm, but it seems
8927 that the handler isn't always enabled here. This is
8928 probably a bug. */
8929 if (input_polling_used ())
8931 /* It could be confusing if a real alarm arrives while
8932 processing the fake one. Turn it off and let the
8933 handler reset it. */
8934 int old_poll_suppress_count = poll_suppress_count;
8935 poll_suppress_count = 1;
8936 poll_for_input_1 ();
8937 poll_suppress_count = old_poll_suppress_count;
8940 if (XPending (FRAME_X_DISPLAY (f)))
8942 XEvent xev;
8943 XNextEvent (FRAME_X_DISPLAY (f), &xev);
8944 x_dispatch_event (&xev, FRAME_X_DISPLAY (f));
8950 /* Change from mapped state to withdrawn state. */
8952 /* Make the frame visible (mapped and not iconified). */
8954 void
8955 x_make_frame_invisible (struct frame *f)
8957 Window window;
8959 /* Use the frame's outermost window, not the one we normally draw on. */
8960 window = FRAME_OUTER_WINDOW (f);
8962 /* Don't keep the highlight on an invisible frame. */
8963 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
8964 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
8966 block_input ();
8968 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
8969 that the current position of the window is user-specified, rather than
8970 program-specified, so that when the window is mapped again, it will be
8971 placed at the same location, without forcing the user to position it
8972 by hand again (they have already done that once for this window.) */
8973 x_wm_set_size_hint (f, (long) 0, 1);
8975 #ifdef USE_GTK
8976 if (FRAME_GTK_OUTER_WIDGET (f))
8977 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
8978 else
8979 #else
8980 if (FRAME_X_EMBEDDED_P (f))
8981 xembed_set_info (f, 0);
8982 else
8983 #endif
8986 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
8987 DefaultScreen (FRAME_X_DISPLAY (f))))
8989 unblock_input ();
8990 error ("Can't notify window manager of window withdrawal");
8994 /* We can't distinguish this from iconification
8995 just by the event that we get from the server.
8996 So we can't win using the usual strategy of letting
8997 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
8998 and synchronize with the server to make sure we agree. */
8999 SET_FRAME_VISIBLE (f, 0);
9000 SET_FRAME_ICONIFIED (f, 0);
9002 x_sync (f);
9004 unblock_input ();
9007 /* Change window state from mapped to iconified. */
9009 void
9010 x_iconify_frame (struct frame *f)
9012 #ifdef USE_X_TOOLKIT
9013 int result;
9014 #endif
9016 /* Don't keep the highlight on an invisible frame. */
9017 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
9018 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
9020 if (FRAME_ICONIFIED_P (f))
9021 return;
9023 block_input ();
9025 x_set_bitmap_icon (f);
9027 #if defined (USE_GTK)
9028 if (FRAME_GTK_OUTER_WIDGET (f))
9030 if (! FRAME_VISIBLE_P (f))
9031 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9033 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9034 SET_FRAME_VISIBLE (f, 0);
9035 SET_FRAME_ICONIFIED (f, 1);
9036 unblock_input ();
9037 return;
9039 #endif
9041 #ifdef USE_X_TOOLKIT
9043 if (! FRAME_VISIBLE_P (f))
9045 if (! EQ (Vx_no_window_manager, Qt))
9046 x_wm_set_window_state (f, IconicState);
9047 /* This was XtPopup, but that did nothing for an iconified frame. */
9048 XtMapWidget (f->output_data.x->widget);
9049 /* The server won't give us any event to indicate
9050 that an invisible frame was changed to an icon,
9051 so we have to record it here. */
9052 SET_FRAME_VISIBLE (f, 0);
9053 SET_FRAME_ICONIFIED (f, 1);
9054 unblock_input ();
9055 return;
9058 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9059 XtWindow (f->output_data.x->widget),
9060 DefaultScreen (FRAME_X_DISPLAY (f)));
9061 unblock_input ();
9063 if (!result)
9064 error ("Can't notify window manager of iconification");
9066 SET_FRAME_ICONIFIED (f, 1);
9067 SET_FRAME_VISIBLE (f, 0);
9069 block_input ();
9070 XFlush (FRAME_X_DISPLAY (f));
9071 unblock_input ();
9072 #else /* not USE_X_TOOLKIT */
9074 /* Make sure the X server knows where the window should be positioned,
9075 in case the user deiconifies with the window manager. */
9076 if (! FRAME_VISIBLE_P (f)
9077 && ! FRAME_ICONIFIED_P (f)
9078 && ! FRAME_X_EMBEDDED_P (f))
9079 x_set_offset (f, f->left_pos, f->top_pos, 0);
9081 /* Since we don't know which revision of X we're running, we'll use both
9082 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9084 /* X11R4: send a ClientMessage to the window manager using the
9085 WM_CHANGE_STATE type. */
9087 XEvent msg;
9089 msg.xclient.window = FRAME_X_WINDOW (f);
9090 msg.xclient.type = ClientMessage;
9091 msg.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_wm_change_state;
9092 msg.xclient.format = 32;
9093 msg.xclient.data.l[0] = IconicState;
9095 if (! XSendEvent (FRAME_X_DISPLAY (f),
9096 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9097 False,
9098 SubstructureRedirectMask | SubstructureNotifyMask,
9099 &msg))
9101 unblock_input ();
9102 error ("Can't notify window manager of iconification");
9106 /* X11R3: set the initial_state field of the window manager hints to
9107 IconicState. */
9108 x_wm_set_window_state (f, IconicState);
9110 if (!FRAME_VISIBLE_P (f))
9112 /* If the frame was withdrawn, before, we must map it. */
9113 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9116 SET_FRAME_ICONIFIED (f, 1);
9117 SET_FRAME_VISIBLE (f, 0);
9119 XFlush (FRAME_X_DISPLAY (f));
9120 unblock_input ();
9121 #endif /* not USE_X_TOOLKIT */
9125 /* Free X resources of frame F. */
9127 void
9128 x_free_frame_resources (struct frame *f)
9130 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9131 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
9132 #ifdef USE_X_TOOLKIT
9133 Lisp_Object bar;
9134 struct scroll_bar *b;
9135 #endif
9137 block_input ();
9139 /* If a display connection is dead, don't try sending more
9140 commands to the X server. */
9141 if (dpyinfo->display)
9143 /* We must free faces before destroying windows because some
9144 font-driver (e.g. xft) access a window while finishing a
9145 face. */
9146 free_frame_faces (f);
9148 if (f->output_data.x->icon_desc)
9149 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9151 #ifdef USE_X_TOOLKIT
9152 /* Explicitly destroy the scroll bars of the frame. Without
9153 this, we get "BadDrawable" errors from the toolkit later on,
9154 presumably from expose events generated for the disappearing
9155 toolkit scroll bars. */
9156 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9158 b = XSCROLL_BAR (bar);
9159 x_scroll_bar_remove (b);
9161 #endif
9163 #ifdef HAVE_X_I18N
9164 if (FRAME_XIC (f))
9165 free_frame_xic (f);
9166 #endif
9168 #ifdef USE_X_TOOLKIT
9169 if (f->output_data.x->widget)
9171 XtDestroyWidget (f->output_data.x->widget);
9172 f->output_data.x->widget = NULL;
9174 /* Tooltips don't have widgets, only a simple X window, even if
9175 we are using a toolkit. */
9176 else if (FRAME_X_WINDOW (f))
9177 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9179 free_frame_menubar (f);
9180 #else /* !USE_X_TOOLKIT */
9182 #ifdef USE_GTK
9183 xg_free_frame_widgets (f);
9184 #endif /* USE_GTK */
9186 if (FRAME_X_WINDOW (f))
9187 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9188 #endif /* !USE_X_TOOLKIT */
9190 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
9191 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
9192 unload_color (f, f->output_data.x->cursor_pixel);
9193 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9194 unload_color (f, f->output_data.x->border_pixel);
9195 unload_color (f, f->output_data.x->mouse_pixel);
9197 if (f->output_data.x->scroll_bar_background_pixel != -1)
9198 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9199 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9200 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9201 #ifdef USE_TOOLKIT_SCROLL_BARS
9202 /* Scrollbar shadow colors. */
9203 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9204 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9205 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9206 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9207 #endif /* USE_TOOLKIT_SCROLL_BARS */
9208 if (f->output_data.x->white_relief.pixel != -1)
9209 unload_color (f, f->output_data.x->white_relief.pixel);
9210 if (f->output_data.x->black_relief.pixel != -1)
9211 unload_color (f, f->output_data.x->black_relief.pixel);
9213 x_free_gcs (f);
9215 /* Free extra GCs allocated by x_setup_relief_colors. */
9216 if (f->output_data.x->white_relief.gc)
9218 XFreeGC (dpyinfo->display, f->output_data.x->white_relief.gc);
9219 f->output_data.x->white_relief.gc = 0;
9221 if (f->output_data.x->black_relief.gc)
9223 XFreeGC (dpyinfo->display, f->output_data.x->black_relief.gc);
9224 f->output_data.x->black_relief.gc = 0;
9227 XFlush (FRAME_X_DISPLAY (f));
9230 xfree (f->output_data.x->saved_menu_event);
9231 xfree (f->output_data.x);
9232 f->output_data.x = NULL;
9234 if (f == dpyinfo->x_focus_frame)
9235 dpyinfo->x_focus_frame = 0;
9236 if (f == dpyinfo->x_focus_event_frame)
9237 dpyinfo->x_focus_event_frame = 0;
9238 if (f == dpyinfo->x_highlight_frame)
9239 dpyinfo->x_highlight_frame = 0;
9240 if (f == hlinfo->mouse_face_mouse_frame)
9241 reset_mouse_highlight (hlinfo);
9243 unblock_input ();
9247 /* Destroy the X window of frame F. */
9249 static void
9250 x_destroy_window (struct frame *f)
9252 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9254 /* If a display connection is dead, don't try sending more
9255 commands to the X server. */
9256 if (dpyinfo->display != 0)
9257 x_free_frame_resources (f);
9259 dpyinfo->reference_count--;
9263 /* Setting window manager hints. */
9265 /* Set the normal size hints for the window manager, for frame F.
9266 FLAGS is the flags word to use--or 0 meaning preserve the flags
9267 that the window now has.
9268 If USER_POSITION, set the USPosition
9269 flag (this is useful when FLAGS is 0).
9270 The GTK version is in gtkutils.c. */
9272 #ifndef USE_GTK
9273 void
9274 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
9276 XSizeHints size_hints;
9277 Window window = FRAME_OUTER_WINDOW (f);
9279 #ifdef USE_X_TOOLKIT
9280 if (f->output_data.x->widget)
9282 widget_update_wm_size_hints (f->output_data.x->widget);
9283 return;
9285 #endif
9287 /* Setting PMaxSize caused various problems. */
9288 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9290 size_hints.x = f->left_pos;
9291 size_hints.y = f->top_pos;
9293 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9294 size_hints.width = FRAME_PIXEL_WIDTH (f);
9296 size_hints.width_inc = frame_resize_pixelwise ? 1 : FRAME_COLUMN_WIDTH (f);
9297 size_hints.height_inc = frame_resize_pixelwise ? 1 : FRAME_LINE_HEIGHT (f);
9299 size_hints.max_width = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
9300 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9301 size_hints.max_height = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
9302 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9304 /* Calculate the base and minimum sizes. */
9306 int base_width, base_height;
9307 int min_rows = 0, min_cols = 0;
9309 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9310 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9312 check_frame_size (f, &min_cols, &min_rows, 0);
9314 if (frame_resize_pixelwise)
9315 /* Needed to prevent a bad protocol error crash when making the
9316 frame size very small. */
9318 min_cols = 2 * min_cols;
9319 min_rows = 2 * min_rows;
9322 /* The window manager uses the base width hints to calculate the
9323 current number of rows and columns in the frame while
9324 resizing; min_width and min_height aren't useful for this
9325 purpose, since they might not give the dimensions for a
9326 zero-row, zero-column frame.
9328 We use the base_width and base_height members if we have
9329 them; otherwise, we set the min_width and min_height members
9330 to the size for a zero x zero frame. */
9332 size_hints.flags |= PBaseSize;
9333 size_hints.base_width = base_width;
9334 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
9335 size_hints.min_width = base_width + min_cols * FRAME_COLUMN_WIDTH (f);
9336 size_hints.min_height = base_height + min_rows * FRAME_LINE_HEIGHT (f);
9339 /* If we don't need the old flags, we don't need the old hint at all. */
9340 if (flags)
9342 size_hints.flags |= flags;
9343 goto no_read;
9347 XSizeHints hints; /* Sometimes I hate X Windows... */
9348 long supplied_return;
9349 int value;
9351 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9352 &supplied_return);
9354 if (flags)
9355 size_hints.flags |= flags;
9356 else
9358 if (value == 0)
9359 hints.flags = 0;
9360 if (hints.flags & PSize)
9361 size_hints.flags |= PSize;
9362 if (hints.flags & PPosition)
9363 size_hints.flags |= PPosition;
9364 if (hints.flags & USPosition)
9365 size_hints.flags |= USPosition;
9366 if (hints.flags & USSize)
9367 size_hints.flags |= USSize;
9371 no_read:
9373 #ifdef PWinGravity
9374 size_hints.win_gravity = f->win_gravity;
9375 size_hints.flags |= PWinGravity;
9377 if (user_position)
9379 size_hints.flags &= ~ PPosition;
9380 size_hints.flags |= USPosition;
9382 #endif /* PWinGravity */
9384 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9386 #endif /* not USE_GTK */
9388 /* Used for IconicState or NormalState */
9390 static void
9391 x_wm_set_window_state (struct frame *f, int state)
9393 #ifdef USE_X_TOOLKIT
9394 Arg al[1];
9396 XtSetArg (al[0], XtNinitialState, state);
9397 XtSetValues (f->output_data.x->widget, al, 1);
9398 #else /* not USE_X_TOOLKIT */
9399 Window window = FRAME_X_WINDOW (f);
9401 f->output_data.x->wm_hints.flags |= StateHint;
9402 f->output_data.x->wm_hints.initial_state = state;
9404 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9405 #endif /* not USE_X_TOOLKIT */
9408 static void
9409 x_wm_set_icon_pixmap (struct frame *f, ptrdiff_t pixmap_id)
9411 Pixmap icon_pixmap, icon_mask;
9413 #if !defined USE_X_TOOLKIT && !defined USE_GTK
9414 Window window = FRAME_OUTER_WINDOW (f);
9415 #endif
9417 if (pixmap_id > 0)
9419 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9420 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9421 icon_mask = x_bitmap_mask (f, pixmap_id);
9422 f->output_data.x->wm_hints.icon_mask = icon_mask;
9424 else
9426 /* It seems there is no way to turn off use of an icon
9427 pixmap. */
9428 return;
9432 #ifdef USE_GTK
9434 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9435 return;
9438 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
9441 Arg al[1];
9442 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9443 XtSetValues (f->output_data.x->widget, al, 1);
9444 XtSetArg (al[0], XtNiconMask, icon_mask);
9445 XtSetValues (f->output_data.x->widget, al, 1);
9448 #else /* not USE_X_TOOLKIT && not USE_GTK */
9450 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9451 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9453 #endif /* not USE_X_TOOLKIT && not USE_GTK */
9456 void
9457 x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
9459 Window window = FRAME_OUTER_WINDOW (f);
9461 f->output_data.x->wm_hints.flags |= IconPositionHint;
9462 f->output_data.x->wm_hints.icon_x = icon_x;
9463 f->output_data.x->wm_hints.icon_y = icon_y;
9465 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9469 /***********************************************************************
9470 Fonts
9471 ***********************************************************************/
9473 #ifdef GLYPH_DEBUG
9475 /* Check that FONT is valid on frame F. It is if it can be found in F's
9476 font table. */
9478 static void
9479 x_check_font (struct frame *f, struct font *font)
9481 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
9482 if (font->driver->check)
9483 eassert (font->driver->check (f, font) == 0);
9486 #endif /* GLYPH_DEBUG */
9489 /***********************************************************************
9490 Initialization
9491 ***********************************************************************/
9493 #ifdef USE_X_TOOLKIT
9494 static XrmOptionDescRec emacs_options[] = {
9495 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
9496 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
9498 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
9499 XrmoptionSepArg, NULL},
9500 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
9502 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9503 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9504 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9505 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9506 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9507 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
9508 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
9511 /* Whether atimer for Xt timeouts is activated or not. */
9513 static int x_timeout_atimer_activated_flag;
9515 #endif /* USE_X_TOOLKIT */
9517 static int x_initialized;
9519 /* Test whether two display-name strings agree up to the dot that separates
9520 the screen number from the server number. */
9521 static int
9522 same_x_server (const char *name1, const char *name2)
9524 int seen_colon = 0;
9525 const char *system_name = SSDATA (Vsystem_name);
9526 ptrdiff_t system_name_length = SBYTES (Vsystem_name);
9527 ptrdiff_t length_until_period = 0;
9529 while (system_name[length_until_period] != 0
9530 && system_name[length_until_period] != '.')
9531 length_until_period++;
9533 /* Treat `unix' like an empty host name. */
9534 if (! strncmp (name1, "unix:", 5))
9535 name1 += 4;
9536 if (! strncmp (name2, "unix:", 5))
9537 name2 += 4;
9538 /* Treat this host's name like an empty host name. */
9539 if (! strncmp (name1, system_name, system_name_length)
9540 && name1[system_name_length] == ':')
9541 name1 += system_name_length;
9542 if (! strncmp (name2, system_name, system_name_length)
9543 && name2[system_name_length] == ':')
9544 name2 += system_name_length;
9545 /* Treat this host's domainless name like an empty host name. */
9546 if (! strncmp (name1, system_name, length_until_period)
9547 && name1[length_until_period] == ':')
9548 name1 += length_until_period;
9549 if (! strncmp (name2, system_name, length_until_period)
9550 && name2[length_until_period] == ':')
9551 name2 += length_until_period;
9553 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
9555 if (*name1 == ':')
9556 seen_colon = 1;
9557 if (seen_colon && *name1 == '.')
9558 return 1;
9560 return (seen_colon
9561 && (*name1 == '.' || *name1 == '\0')
9562 && (*name2 == '.' || *name2 == '\0'));
9565 /* Count number of set bits in mask and number of bits to shift to
9566 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
9567 to 5. */
9568 static void
9569 get_bits_and_offset (unsigned long mask, int *bits, int *offset)
9571 int nr = 0;
9572 int off = 0;
9574 while (!(mask & 1))
9576 off++;
9577 mask >>= 1;
9580 while (mask & 1)
9582 nr++;
9583 mask >>= 1;
9586 *offset = off;
9587 *bits = nr;
9590 /* Return 1 if display DISPLAY is available for use, 0 otherwise.
9591 But don't permanently open it, just test its availability. */
9593 bool
9594 x_display_ok (const char *display)
9596 Display *dpy = XOpenDisplay (display);
9597 return dpy ? (XCloseDisplay (dpy), 1) : 0;
9600 #ifdef USE_GTK
9601 static void
9602 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
9603 const gchar *msg, gpointer user_data)
9605 if (!strstr (msg, "g_set_prgname"))
9606 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, msg);
9608 #endif
9610 /* Open a connection to X display DISPLAY_NAME, and return
9611 the structure that describes the open display.
9612 If we cannot contact the display, return null. */
9614 struct x_display_info *
9615 x_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
9617 int connection;
9618 Display *dpy;
9619 struct terminal *terminal;
9620 struct x_display_info *dpyinfo;
9621 XrmDatabase xrdb;
9622 ptrdiff_t lim;
9624 block_input ();
9626 if (!x_initialized)
9628 x_initialize ();
9629 ++x_initialized;
9632 if (! x_display_ok (SSDATA (display_name)))
9633 error ("Display %s can't be opened", SSDATA (display_name));
9635 #ifdef USE_GTK
9637 #define NUM_ARGV 10
9638 int argc;
9639 char *argv[NUM_ARGV];
9640 char **argv2 = argv;
9641 guint id;
9643 if (x_initialized++ > 1)
9645 xg_display_open (SSDATA (display_name), &dpy);
9647 else
9649 static char display_opt[] = "--display";
9650 static char name_opt[] = "--name";
9652 for (argc = 0; argc < NUM_ARGV; ++argc)
9653 argv[argc] = 0;
9655 argc = 0;
9656 argv[argc++] = initial_argv[0];
9658 if (! NILP (display_name))
9660 argv[argc++] = display_opt;
9661 argv[argc++] = SSDATA (display_name);
9664 argv[argc++] = name_opt;
9665 argv[argc++] = resource_name;
9667 XSetLocaleModifiers ("");
9669 /* Emacs can only handle core input events, so make sure
9670 Gtk doesn't use Xinput or Xinput2 extensions. */
9671 xputenv ("GDK_CORE_DEVICE_EVENTS=1");
9673 /* Work around GLib bug that outputs a faulty warning. See
9674 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
9675 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
9676 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
9678 /* NULL window -> events for all windows go to our function.
9679 Call before gtk_init so Gtk+ event filters comes after our. */
9680 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
9682 /* gtk_init does set_locale. Fix locale before and after. */
9683 fixup_locale ();
9684 gtk_init (&argc, &argv2);
9685 fixup_locale ();
9687 g_log_remove_handler ("GLib", id);
9689 xg_initialize ();
9691 dpy = DEFAULT_GDK_DISPLAY ();
9693 #if ! GTK_CHECK_VERSION (2, 90, 0)
9694 /* Load our own gtkrc if it exists. */
9696 const char *file = "~/.emacs.d/gtkrc";
9697 Lisp_Object s, abs_file;
9699 s = build_string (file);
9700 abs_file = Fexpand_file_name (s, Qnil);
9702 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
9703 gtk_rc_parse (SSDATA (abs_file));
9705 #endif
9707 XSetErrorHandler (x_error_handler);
9708 XSetIOErrorHandler (x_io_error_quitter);
9711 #else /* not USE_GTK */
9712 #ifdef USE_X_TOOLKIT
9713 /* weiner@footloose.sps.mot.com reports that this causes
9714 errors with X11R5:
9715 X protocol error: BadAtom (invalid Atom parameter)
9716 on protocol request 18skiloaf.
9717 So let's not use it until R6. */
9718 #ifdef HAVE_X11XTR6
9719 XtSetLanguageProc (NULL, NULL, NULL);
9720 #endif
9723 int argc = 0;
9724 char *argv[3];
9726 argv[0] = "";
9727 argc = 1;
9728 if (xrm_option)
9730 argv[argc++] = "-xrm";
9731 argv[argc++] = xrm_option;
9733 turn_on_atimers (0);
9734 dpy = XtOpenDisplay (Xt_app_con, SSDATA (display_name),
9735 resource_name, EMACS_CLASS,
9736 emacs_options, XtNumber (emacs_options),
9737 &argc, argv);
9738 turn_on_atimers (1);
9740 #ifdef HAVE_X11XTR6
9741 /* I think this is to compensate for XtSetLanguageProc. */
9742 fixup_locale ();
9743 #endif
9746 #else /* not USE_X_TOOLKIT */
9747 XSetLocaleModifiers ("");
9748 dpy = XOpenDisplay (SSDATA (display_name));
9749 #endif /* not USE_X_TOOLKIT */
9750 #endif /* not USE_GTK*/
9752 /* Detect failure. */
9753 if (dpy == 0)
9755 unblock_input ();
9756 return 0;
9759 /* We have definitely succeeded. Record the new connection. */
9761 dpyinfo = xzalloc (sizeof *dpyinfo);
9762 terminal = x_create_terminal (dpyinfo);
9765 struct x_display_info *share;
9767 for (share = x_display_list; share; share = share->next)
9768 if (same_x_server (SSDATA (XCAR (share->name_list_element)),
9769 SSDATA (display_name)))
9770 break;
9771 if (share)
9772 terminal->kboard = share->terminal->kboard;
9773 else
9775 terminal->kboard = allocate_kboard (Qx);
9777 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
9779 char *vendor = ServerVendor (dpy);
9781 /* Protect terminal from GC before removing it from the
9782 list of terminals. */
9783 struct gcpro gcpro1;
9784 Lisp_Object gcpro_term;
9785 XSETTERMINAL (gcpro_term, terminal);
9786 GCPRO1 (gcpro_term);
9788 /* Temporarily hide the partially initialized terminal. */
9789 terminal_list = terminal->next_terminal;
9790 unblock_input ();
9791 kset_system_key_alist
9792 (terminal->kboard,
9793 call1 (Qvendor_specific_keysyms,
9794 vendor ? build_string (vendor) : empty_unibyte_string));
9795 block_input ();
9796 terminal->next_terminal = terminal_list;
9797 terminal_list = terminal;
9798 UNGCPRO;
9801 /* Don't let the initial kboard remain current longer than necessary.
9802 That would cause problems if a file loaded on startup tries to
9803 prompt in the mini-buffer. */
9804 if (current_kboard == initial_kboard)
9805 current_kboard = terminal->kboard;
9807 terminal->kboard->reference_count++;
9810 /* Put this display on the chain. */
9811 dpyinfo->next = x_display_list;
9812 x_display_list = dpyinfo;
9814 dpyinfo->name_list_element = Fcons (display_name, Qnil);
9815 dpyinfo->display = dpy;
9817 /* Set the name of the terminal. */
9818 terminal->name = xlispstrdup (display_name);
9820 #if 0
9821 XSetAfterFunction (x_current_display, x_trace_wire);
9822 #endif /* ! 0 */
9824 lim = min (PTRDIFF_MAX, SIZE_MAX) - sizeof "@";
9825 if (lim - SBYTES (Vinvocation_name) < SBYTES (Vsystem_name))
9826 memory_full (SIZE_MAX);
9827 dpyinfo->x_id_name = xmalloc (SBYTES (Vinvocation_name)
9828 + SBYTES (Vsystem_name) + 2);
9829 strcat (strcat (strcpy (dpyinfo->x_id_name, SSDATA (Vinvocation_name)), "@"),
9830 SSDATA (Vsystem_name));
9832 /* Figure out which modifier bits mean what. */
9833 x_find_modifier_meanings (dpyinfo);
9835 /* Get the scroll bar cursor. */
9836 #ifdef USE_GTK
9837 /* We must create a GTK cursor, it is required for GTK widgets. */
9838 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
9839 #endif /* USE_GTK */
9841 dpyinfo->vertical_scroll_bar_cursor
9842 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
9844 xrdb = x_load_resources (dpyinfo->display, xrm_option,
9845 resource_name, EMACS_CLASS);
9846 #ifdef HAVE_XRMSETDATABASE
9847 XrmSetDatabase (dpyinfo->display, xrdb);
9848 #else
9849 dpyinfo->display->db = xrdb;
9850 #endif
9851 /* Put the rdb where we can find it in a way that works on
9852 all versions. */
9853 dpyinfo->xrdb = xrdb;
9855 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
9856 DefaultScreen (dpyinfo->display));
9857 select_visual (dpyinfo);
9858 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
9859 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
9860 dpyinfo->icon_bitmap_id = -1;
9861 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
9863 reset_mouse_highlight (&dpyinfo->mouse_highlight);
9865 /* See if we can construct pixel values from RGB values. */
9866 if (dpyinfo->visual->class == TrueColor)
9868 get_bits_and_offset (dpyinfo->visual->red_mask,
9869 &dpyinfo->red_bits, &dpyinfo->red_offset);
9870 get_bits_and_offset (dpyinfo->visual->blue_mask,
9871 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
9872 get_bits_and_offset (dpyinfo->visual->green_mask,
9873 &dpyinfo->green_bits, &dpyinfo->green_offset);
9876 /* See if a private colormap is requested. */
9877 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
9879 if (dpyinfo->visual->class == PseudoColor)
9881 Lisp_Object value;
9882 value = display_x_get_resource (dpyinfo,
9883 build_string ("privateColormap"),
9884 build_string ("PrivateColormap"),
9885 Qnil, Qnil);
9886 if (STRINGP (value)
9887 && (!strcmp (SSDATA (value), "true")
9888 || !strcmp (SSDATA (value), "on")))
9889 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
9892 else
9893 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
9894 dpyinfo->visual, AllocNone);
9896 #ifdef HAVE_XFT
9898 /* If we are using Xft, check dpi value in X resources.
9899 It is better we use it as well, since Xft will use it, as will all
9900 Gnome applications. If our real DPI is smaller or larger than the
9901 one Xft uses, our font will look smaller or larger than other
9902 for other applications, even if it is the same font name (monospace-10
9903 for example). */
9904 char *v = XGetDefault (dpyinfo->display, "Xft", "dpi");
9905 double d;
9906 if (v != NULL && sscanf (v, "%lf", &d) == 1)
9907 dpyinfo->resy = dpyinfo->resx = d;
9909 #endif
9911 if (dpyinfo->resy < 1)
9913 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
9914 double pixels = DisplayHeight (dpyinfo->display, screen_number);
9915 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
9916 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
9917 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
9918 pixels = DisplayWidth (dpyinfo->display, screen_number);
9919 mm = DisplayWidthMM (dpyinfo->display, screen_number);
9920 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
9921 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
9925 static const struct
9927 const char *name;
9928 int offset;
9929 } atom_refs[] = {
9930 #define ATOM_REFS_INIT(string, member) \
9931 { string, offsetof (struct x_display_info, member) },
9932 ATOM_REFS_INIT ("WM_PROTOCOLS", Xatom_wm_protocols)
9933 ATOM_REFS_INIT ("WM_TAKE_FOCUS", Xatom_wm_take_focus)
9934 ATOM_REFS_INIT ("WM_SAVE_YOURSELF", Xatom_wm_save_yourself)
9935 ATOM_REFS_INIT ("WM_DELETE_WINDOW", Xatom_wm_delete_window)
9936 ATOM_REFS_INIT ("WM_CHANGE_STATE", Xatom_wm_change_state)
9937 ATOM_REFS_INIT ("WM_CONFIGURE_DENIED", Xatom_wm_configure_denied)
9938 ATOM_REFS_INIT ("WM_MOVED", Xatom_wm_window_moved)
9939 ATOM_REFS_INIT ("WM_CLIENT_LEADER", Xatom_wm_client_leader)
9940 ATOM_REFS_INIT ("Editres", Xatom_editres)
9941 ATOM_REFS_INIT ("CLIPBOARD", Xatom_CLIPBOARD)
9942 ATOM_REFS_INIT ("TIMESTAMP", Xatom_TIMESTAMP)
9943 ATOM_REFS_INIT ("TEXT", Xatom_TEXT)
9944 ATOM_REFS_INIT ("COMPOUND_TEXT", Xatom_COMPOUND_TEXT)
9945 ATOM_REFS_INIT ("UTF8_STRING", Xatom_UTF8_STRING)
9946 ATOM_REFS_INIT ("DELETE", Xatom_DELETE)
9947 ATOM_REFS_INIT ("MULTIPLE", Xatom_MULTIPLE)
9948 ATOM_REFS_INIT ("INCR", Xatom_INCR)
9949 ATOM_REFS_INIT ("_EMACS_TMP_", Xatom_EMACS_TMP)
9950 ATOM_REFS_INIT ("TARGETS", Xatom_TARGETS)
9951 ATOM_REFS_INIT ("NULL", Xatom_NULL)
9952 ATOM_REFS_INIT ("ATOM", Xatom_ATOM)
9953 ATOM_REFS_INIT ("ATOM_PAIR", Xatom_ATOM_PAIR)
9954 ATOM_REFS_INIT ("CLIPBOARD_MANAGER", Xatom_CLIPBOARD_MANAGER)
9955 ATOM_REFS_INIT ("_XEMBED_INFO", Xatom_XEMBED_INFO)
9956 /* For properties of font. */
9957 ATOM_REFS_INIT ("PIXEL_SIZE", Xatom_PIXEL_SIZE)
9958 ATOM_REFS_INIT ("AVERAGE_WIDTH", Xatom_AVERAGE_WIDTH)
9959 ATOM_REFS_INIT ("_MULE_BASELINE_OFFSET", Xatom_MULE_BASELINE_OFFSET)
9960 ATOM_REFS_INIT ("_MULE_RELATIVE_COMPOSE", Xatom_MULE_RELATIVE_COMPOSE)
9961 ATOM_REFS_INIT ("_MULE_DEFAULT_ASCENT", Xatom_MULE_DEFAULT_ASCENT)
9962 /* Ghostscript support. */
9963 ATOM_REFS_INIT ("DONE", Xatom_DONE)
9964 ATOM_REFS_INIT ("PAGE", Xatom_PAGE)
9965 ATOM_REFS_INIT ("SCROLLBAR", Xatom_Scrollbar)
9966 ATOM_REFS_INIT ("_XEMBED", Xatom_XEMBED)
9967 /* EWMH */
9968 ATOM_REFS_INIT ("_NET_WM_STATE", Xatom_net_wm_state)
9969 ATOM_REFS_INIT ("_NET_WM_STATE_FULLSCREEN", Xatom_net_wm_state_fullscreen)
9970 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_HORZ",
9971 Xatom_net_wm_state_maximized_horz)
9972 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_VERT",
9973 Xatom_net_wm_state_maximized_vert)
9974 ATOM_REFS_INIT ("_NET_WM_STATE_STICKY", Xatom_net_wm_state_sticky)
9975 ATOM_REFS_INIT ("_NET_WM_STATE_HIDDEN", Xatom_net_wm_state_hidden)
9976 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE", Xatom_net_window_type)
9977 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE_TOOLTIP",
9978 Xatom_net_window_type_tooltip)
9979 ATOM_REFS_INIT ("_NET_WM_ICON_NAME", Xatom_net_wm_icon_name)
9980 ATOM_REFS_INIT ("_NET_WM_NAME", Xatom_net_wm_name)
9981 ATOM_REFS_INIT ("_NET_SUPPORTED", Xatom_net_supported)
9982 ATOM_REFS_INIT ("_NET_SUPPORTING_WM_CHECK", Xatom_net_supporting_wm_check)
9983 ATOM_REFS_INIT ("_NET_WM_WINDOW_OPACITY", Xatom_net_wm_window_opacity)
9984 ATOM_REFS_INIT ("_NET_ACTIVE_WINDOW", Xatom_net_active_window)
9985 ATOM_REFS_INIT ("_NET_FRAME_EXTENTS", Xatom_net_frame_extents)
9986 ATOM_REFS_INIT ("_NET_CURRENT_DESKTOP", Xatom_net_current_desktop)
9987 ATOM_REFS_INIT ("_NET_WORKAREA", Xatom_net_workarea)
9988 /* Session management */
9989 ATOM_REFS_INIT ("SM_CLIENT_ID", Xatom_SM_CLIENT_ID)
9990 ATOM_REFS_INIT ("_XSETTINGS_SETTINGS", Xatom_xsettings_prop)
9991 ATOM_REFS_INIT ("MANAGER", Xatom_xsettings_mgr)
9994 int i;
9995 const int atom_count = sizeof (atom_refs) / sizeof (atom_refs[0]);
9996 /* 1 for _XSETTINGS_SN */
9997 const int total_atom_count = 1 + atom_count;
9998 Atom *atoms_return = xmalloc (total_atom_count * sizeof *atoms_return);
9999 char **atom_names = xmalloc (total_atom_count * sizeof *atom_names);
10000 static char const xsettings_fmt[] = "_XSETTINGS_S%d";
10001 char xsettings_atom_name[sizeof xsettings_fmt - 2
10002 + INT_STRLEN_BOUND (int)];
10004 for (i = 0; i < atom_count; i++)
10005 atom_names[i] = (char *) atom_refs[i].name;
10007 /* Build _XSETTINGS_SN atom name */
10008 sprintf (xsettings_atom_name, xsettings_fmt,
10009 XScreenNumberOfScreen (dpyinfo->screen));
10010 atom_names[i] = xsettings_atom_name;
10012 XInternAtoms (dpyinfo->display, atom_names, total_atom_count,
10013 False, atoms_return);
10015 for (i = 0; i < atom_count; i++)
10016 *(Atom *) ((char *) dpyinfo + atom_refs[i].offset) = atoms_return[i];
10018 /* Manual copy of last atom */
10019 dpyinfo->Xatom_xsettings_sel = atoms_return[i];
10021 xfree (atom_names);
10022 xfree (atoms_return);
10025 dpyinfo->x_dnd_atoms_size = 8;
10026 dpyinfo->x_dnd_atoms = xmalloc (sizeof *dpyinfo->x_dnd_atoms
10027 * dpyinfo->x_dnd_atoms_size);
10028 dpyinfo->gray
10029 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10030 gray_bits, gray_width, gray_height,
10031 1, 0, 1);
10033 #ifdef HAVE_X_I18N
10034 xim_initialize (dpyinfo, resource_name);
10035 #endif
10037 xsettings_initialize (dpyinfo);
10039 connection = ConnectionNumber (dpyinfo->display);
10041 /* This is only needed for distinguishing keyboard and process input. */
10042 if (connection != 0)
10043 add_keyboard_wait_descriptor (connection);
10045 #ifdef F_SETOWN
10046 fcntl (connection, F_SETOWN, getpid ());
10047 #endif /* ! defined (F_SETOWN) */
10049 if (interrupt_input)
10050 init_sigio (connection);
10052 #ifdef USE_LUCID
10054 XrmValue d, fr, to;
10055 Font font;
10057 dpy = dpyinfo->display;
10058 d.addr = (XPointer)&dpy;
10059 d.size = sizeof (Display *);
10060 fr.addr = XtDefaultFont;
10061 fr.size = sizeof (XtDefaultFont);
10062 to.size = sizeof (Font *);
10063 to.addr = (XPointer)&font;
10064 x_catch_errors (dpy);
10065 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10066 emacs_abort ();
10067 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10068 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10069 x_uncatch_errors ();
10071 #endif
10073 /* See if we should run in synchronous mode. This is useful
10074 for debugging X code. */
10076 Lisp_Object value;
10077 value = display_x_get_resource (dpyinfo,
10078 build_string ("synchronous"),
10079 build_string ("Synchronous"),
10080 Qnil, Qnil);
10081 if (STRINGP (value)
10082 && (!strcmp (SSDATA (value), "true")
10083 || !strcmp (SSDATA (value), "on")))
10084 XSynchronize (dpyinfo->display, True);
10088 Lisp_Object value;
10089 value = display_x_get_resource (dpyinfo,
10090 build_string ("useXIM"),
10091 build_string ("UseXIM"),
10092 Qnil, Qnil);
10093 #ifdef USE_XIM
10094 if (STRINGP (value)
10095 && (!strcmp (SSDATA (value), "false")
10096 || !strcmp (SSDATA (value), "off")))
10097 use_xim = false;
10098 #else
10099 if (STRINGP (value)
10100 && (!strcmp (SSDATA (value), "true")
10101 || !strcmp (SSDATA (value), "on")))
10102 use_xim = true;
10103 #endif
10106 #ifdef HAVE_X_SM
10107 /* Only do this for the very first display in the Emacs session.
10108 Ignore X session management when Emacs was first started on a
10109 tty. */
10110 if (terminal->id == 1)
10111 x_session_initialize (dpyinfo);
10112 #endif
10114 unblock_input ();
10116 return dpyinfo;
10119 /* Get rid of display DPYINFO, deleting all frames on it,
10120 and without sending any more commands to the X server. */
10122 static void
10123 x_delete_display (struct x_display_info *dpyinfo)
10125 struct terminal *t;
10127 /* Close all frames and delete the generic struct terminal for this
10128 X display. */
10129 for (t = terminal_list; t; t = t->next_terminal)
10130 if (t->type == output_x_window && t->display_info.x == dpyinfo)
10132 #ifdef HAVE_X_SM
10133 /* Close X session management when we close its display. */
10134 if (t->id == 1 && x_session_have_connection ())
10135 x_session_close ();
10136 #endif
10137 delete_terminal (t);
10138 break;
10141 if (next_noop_dpyinfo == dpyinfo)
10142 next_noop_dpyinfo = dpyinfo->next;
10144 if (x_display_list == dpyinfo)
10145 x_display_list = dpyinfo->next;
10146 else
10148 struct x_display_info *tail;
10150 for (tail = x_display_list; tail; tail = tail->next)
10151 if (tail->next == dpyinfo)
10152 tail->next = tail->next->next;
10155 xfree (dpyinfo->x_id_name);
10156 xfree (dpyinfo->x_dnd_atoms);
10157 xfree (dpyinfo->color_cells);
10158 xfree (dpyinfo);
10161 #ifdef USE_X_TOOLKIT
10163 /* Atimer callback function for TIMER. Called every 0.1s to process
10164 Xt timeouts, if needed. We must avoid calling XtAppPending as
10165 much as possible because that function does an implicit XFlush
10166 that slows us down. */
10168 static void
10169 x_process_timeouts (struct atimer *timer)
10171 block_input ();
10172 x_timeout_atimer_activated_flag = 0;
10173 if (toolkit_scroll_bar_interaction || popup_activated ())
10175 while (XtAppPending (Xt_app_con) & XtIMTimer)
10176 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10177 /* Reactivate the atimer for next time. */
10178 x_activate_timeout_atimer ();
10180 unblock_input ();
10183 /* Install an asynchronous timer that processes Xt timeout events
10184 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
10185 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
10186 function whenever these variables are set. This is necessary
10187 because some widget sets use timeouts internally, for example the
10188 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
10189 processed, these widgets don't behave normally. */
10191 void
10192 x_activate_timeout_atimer (void)
10194 block_input ();
10195 if (!x_timeout_atimer_activated_flag)
10197 struct timespec interval = make_timespec (0, 100 * 1000 * 1000);
10198 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
10199 x_timeout_atimer_activated_flag = 1;
10201 unblock_input ();
10204 #endif /* USE_X_TOOLKIT */
10207 /* Set up use of X before we make the first connection. */
10209 static struct redisplay_interface x_redisplay_interface =
10211 x_frame_parm_handlers,
10212 x_produce_glyphs,
10213 x_write_glyphs,
10214 x_insert_glyphs,
10215 x_clear_end_of_line,
10216 x_scroll_run,
10217 x_after_update_window_line,
10218 x_update_window_begin,
10219 x_update_window_end,
10220 x_flush,
10221 x_clear_window_mouse_face,
10222 x_get_glyph_overhangs,
10223 x_fix_overlapping_area,
10224 x_draw_fringe_bitmap,
10225 0, /* define_fringe_bitmap */
10226 0, /* destroy_fringe_bitmap */
10227 x_compute_glyph_string_overhangs,
10228 x_draw_glyph_string,
10229 x_define_frame_cursor,
10230 x_clear_frame_area,
10231 x_draw_window_cursor,
10232 x_draw_vertical_window_border,
10233 x_draw_window_divider,
10234 x_shift_glyphs_for_insert
10238 /* This function is called when the last frame on a display is deleted. */
10239 void
10240 x_delete_terminal (struct terminal *terminal)
10242 struct x_display_info *dpyinfo = terminal->display_info.x;
10243 int connection = -1;
10245 /* Protect against recursive calls. delete_frame in
10246 delete_terminal calls us back when it deletes our last frame. */
10247 if (!terminal->name)
10248 return;
10250 block_input ();
10251 #ifdef HAVE_X_I18N
10252 /* We must close our connection to the XIM server before closing the
10253 X display. */
10254 if (dpyinfo->xim)
10255 xim_close_dpy (dpyinfo);
10256 #endif
10258 /* If called from x_connection_closed, the display may already be closed
10259 and dpyinfo->display was set to 0 to indicate that. */
10260 if (dpyinfo->display)
10262 connection = ConnectionNumber (dpyinfo->display);
10264 x_destroy_all_bitmaps (dpyinfo);
10265 XSetCloseDownMode (dpyinfo->display, DestroyAll);
10267 /* Whether or not XCloseDisplay destroys the associated resource
10268 database depends on the version of libX11. To avoid both
10269 crash and memory leak, we dissociate the database from the
10270 display and then destroy dpyinfo->xrdb ourselves.
10272 Unfortunately, the above strategy does not work in some
10273 situations due to a bug in newer versions of libX11: because
10274 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
10275 dpy->db is NULL, XCloseDisplay destroys the associated
10276 database whereas it has not been created by XGetDefault
10277 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
10278 don't destroy the database here in order to avoid the crash
10279 in the above situations for now, though that may cause memory
10280 leaks in other situations. */
10281 #if 0
10282 #ifdef HAVE_XRMSETDATABASE
10283 XrmSetDatabase (dpyinfo->display, NULL);
10284 #else
10285 dpyinfo->display->db = NULL;
10286 #endif
10287 /* We used to call XrmDestroyDatabase from x_delete_display, but
10288 some older versions of libX11 crash if we call it after
10289 closing all the displays. */
10290 XrmDestroyDatabase (dpyinfo->xrdb);
10291 #endif
10293 #ifdef USE_GTK
10294 xg_display_close (dpyinfo->display);
10295 #else
10296 #ifdef USE_X_TOOLKIT
10297 XtCloseDisplay (dpyinfo->display);
10298 #else
10299 XCloseDisplay (dpyinfo->display);
10300 #endif
10301 #endif /* ! USE_GTK */
10304 /* No more input on this descriptor. */
10305 if (connection != -1)
10306 delete_keyboard_wait_descriptor (connection);
10308 /* Mark as dead. */
10309 dpyinfo->display = NULL;
10310 x_delete_display (dpyinfo);
10311 unblock_input ();
10314 /* Create a struct terminal, initialize it with the X11 specific
10315 functions and make DISPLAY->TERMINAL point to it. */
10317 static struct terminal *
10318 x_create_terminal (struct x_display_info *dpyinfo)
10320 struct terminal *terminal;
10322 terminal = create_terminal ();
10324 terminal->type = output_x_window;
10325 terminal->display_info.x = dpyinfo;
10326 dpyinfo->terminal = terminal;
10328 /* kboard is initialized in x_term_init. */
10330 terminal->clear_frame_hook = x_clear_frame;
10331 terminal->ins_del_lines_hook = x_ins_del_lines;
10332 terminal->delete_glyphs_hook = x_delete_glyphs;
10333 terminal->ring_bell_hook = XTring_bell;
10334 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
10335 terminal->reset_terminal_modes_hook = NULL;
10336 terminal->set_terminal_modes_hook = NULL;
10337 terminal->update_begin_hook = x_update_begin;
10338 terminal->update_end_hook = x_update_end;
10339 terminal->set_terminal_window_hook = NULL;
10340 terminal->read_socket_hook = XTread_socket;
10341 terminal->frame_up_to_date_hook = XTframe_up_to_date;
10342 terminal->mouse_position_hook = XTmouse_position;
10343 terminal->frame_rehighlight_hook = XTframe_rehighlight;
10344 terminal->frame_raise_lower_hook = XTframe_raise_lower;
10345 terminal->fullscreen_hook = XTfullscreen_hook;
10346 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10347 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10348 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
10349 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
10351 terminal->delete_frame_hook = x_destroy_window;
10352 terminal->delete_terminal_hook = x_delete_terminal;
10354 terminal->rif = &x_redisplay_interface;
10356 return terminal;
10359 void
10360 x_initialize (void)
10362 baud_rate = 19200;
10364 x_noop_count = 0;
10365 last_tool_bar_item = -1;
10366 any_help_event_p = 0;
10367 ignore_next_mouse_click_timeout = 0;
10369 #ifdef USE_GTK
10370 current_count = -1;
10371 #endif
10373 /* Try to use interrupt input; if we can't, then start polling. */
10374 Fset_input_interrupt_mode (Qt);
10376 #ifdef USE_X_TOOLKIT
10377 XtToolkitInitialize ();
10379 Xt_app_con = XtCreateApplicationContext ();
10381 /* Register a converter from strings to pixels, which uses
10382 Emacs' color allocation infrastructure. */
10383 XtAppSetTypeConverter (Xt_app_con,
10384 XtRString, XtRPixel, cvt_string_to_pixel,
10385 cvt_string_to_pixel_args,
10386 XtNumber (cvt_string_to_pixel_args),
10387 XtCacheByDisplay, cvt_pixel_dtor);
10389 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10390 #endif
10392 #ifdef USE_TOOLKIT_SCROLL_BARS
10393 #ifndef USE_GTK
10394 xaw3d_arrow_scroll = False;
10395 xaw3d_pick_top = True;
10396 #endif
10397 #endif
10399 /* Note that there is no real way portable across R3/R4 to get the
10400 original error handler. */
10401 XSetErrorHandler (x_error_handler);
10402 XSetIOErrorHandler (x_io_error_quitter);
10406 void
10407 syms_of_xterm (void)
10409 x_error_message = NULL;
10411 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
10412 DEFSYM (Qlatin_1, "latin-1");
10414 #ifdef USE_GTK
10415 xg_default_icon_file = build_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
10416 staticpro (&xg_default_icon_file);
10418 DEFSYM (Qx_gtk_map_stock, "x-gtk-map-stock");
10419 #endif
10421 DEFVAR_BOOL ("x-use-underline-position-properties",
10422 x_use_underline_position_properties,
10423 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
10424 A value of nil means ignore them. If you encounter fonts with bogus
10425 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10426 to 4.1, set this to nil. You can also use `underline-minimum-offset'
10427 to override the font's UNDERLINE_POSITION for small font display
10428 sizes. */);
10429 x_use_underline_position_properties = 1;
10431 DEFVAR_BOOL ("x-underline-at-descent-line",
10432 x_underline_at_descent_line,
10433 doc: /* Non-nil means to draw the underline at the same place as the descent line.
10434 A value of nil means to draw the underline according to the value of the
10435 variable `x-use-underline-position-properties', which is usually at the
10436 baseline level. The default value is nil. */);
10437 x_underline_at_descent_line = 0;
10439 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
10440 x_mouse_click_focus_ignore_position,
10441 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
10442 This variable is only used when the window manager requires that you
10443 click on a frame to select it (give it focus). In that case, a value
10444 of nil, means that the selected window and cursor position changes to
10445 reflect the mouse click position, while a non-nil value means that the
10446 selected window or cursor position is preserved. */);
10447 x_mouse_click_focus_ignore_position = 0;
10449 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
10450 doc: /* Which toolkit scroll bars Emacs uses, if any.
10451 A value of nil means Emacs doesn't use toolkit scroll bars.
10452 With the X Window system, the value is a symbol describing the
10453 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
10454 With MS Windows or Nextstep, the value is t. */);
10455 #ifdef USE_TOOLKIT_SCROLL_BARS
10456 #ifdef USE_MOTIF
10457 Vx_toolkit_scroll_bars = intern_c_string ("motif");
10458 #elif defined HAVE_XAW3D
10459 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
10460 #elif USE_GTK
10461 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
10462 #else
10463 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
10464 #endif
10465 #else
10466 Vx_toolkit_scroll_bars = Qnil;
10467 #endif
10469 DEFSYM (Qmodifier_value, "modifier-value");
10470 DEFSYM (Qalt, "alt");
10471 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
10472 DEFSYM (Qhyper, "hyper");
10473 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
10474 DEFSYM (Qmeta, "meta");
10475 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
10476 DEFSYM (Qsuper, "super");
10477 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
10479 DEFVAR_LISP ("x-alt-keysym", Vx_alt_keysym,
10480 doc: /* Which keys Emacs uses for the alt modifier.
10481 This should be one of the symbols `alt', `hyper', `meta', `super'.
10482 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
10483 is nil, which is the same as `alt'. */);
10484 Vx_alt_keysym = Qnil;
10486 DEFVAR_LISP ("x-hyper-keysym", Vx_hyper_keysym,
10487 doc: /* Which keys Emacs uses for the hyper modifier.
10488 This should be one of the symbols `alt', `hyper', `meta', `super'.
10489 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
10490 default is nil, which is the same as `hyper'. */);
10491 Vx_hyper_keysym = Qnil;
10493 DEFVAR_LISP ("x-meta-keysym", Vx_meta_keysym,
10494 doc: /* Which keys Emacs uses for the meta modifier.
10495 This should be one of the symbols `alt', `hyper', `meta', `super'.
10496 For example, `meta' means use the Meta_L and Meta_R keysyms. The
10497 default is nil, which is the same as `meta'. */);
10498 Vx_meta_keysym = Qnil;
10500 DEFVAR_LISP ("x-super-keysym", Vx_super_keysym,
10501 doc: /* Which keys Emacs uses for the super modifier.
10502 This should be one of the symbols `alt', `hyper', `meta', `super'.
10503 For example, `super' means use the Super_L and Super_R keysyms. The
10504 default is nil, which is the same as `super'. */);
10505 Vx_super_keysym = Qnil;
10507 DEFVAR_LISP ("x-keysym-table", Vx_keysym_table,
10508 doc: /* Hash table of character codes indexed by X keysym codes. */);
10509 Vx_keysym_table = make_hash_table (hashtest_eql, make_number (900),
10510 make_float (DEFAULT_REHASH_SIZE),
10511 make_float (DEFAULT_REHASH_THRESHOLD),
10512 Qnil);