Install the actual changes missing in previous revision
[emacs.git] / src / xterm.c
blob35f7facc80ef393e27eda59d6e2b95f2a41e3f71
1 /* X Communication module for terminals which understand the X protocol.
3 Copyright (C) 1989, 1993-2013 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 int use_xim = 1;
137 #else
138 int use_xim = 0; /* 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 /* Incremented by XTread_socket whenever it really tries to read
169 events. */
171 static int volatile input_signal_count;
173 /* Used locally within XTread_socket. */
175 static int x_noop_count;
177 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
179 static Lisp_Object Qvendor_specific_keysyms;
180 static Lisp_Object Qlatin_1;
182 #ifdef USE_GTK
183 /* The name of the Emacs icon file. */
184 static Lisp_Object xg_default_icon_file;
186 /* Used in gtkutil.c. */
187 Lisp_Object Qx_gtk_map_stock;
188 #endif
190 /* Some functions take this as char *, not const char *. */
191 static char emacs_class[] = EMACS_CLASS;
193 enum xembed_info
195 XEMBED_MAPPED = 1 << 0
198 enum xembed_message
200 XEMBED_EMBEDDED_NOTIFY = 0,
201 XEMBED_WINDOW_ACTIVATE = 1,
202 XEMBED_WINDOW_DEACTIVATE = 2,
203 XEMBED_REQUEST_FOCUS = 3,
204 XEMBED_FOCUS_IN = 4,
205 XEMBED_FOCUS_OUT = 5,
206 XEMBED_FOCUS_NEXT = 6,
207 XEMBED_FOCUS_PREV = 7,
209 XEMBED_MODALITY_ON = 10,
210 XEMBED_MODALITY_OFF = 11,
211 XEMBED_REGISTER_ACCELERATOR = 12,
212 XEMBED_UNREGISTER_ACCELERATOR = 13,
213 XEMBED_ACTIVATE_ACCELERATOR = 14
216 static bool x_alloc_nearest_color_1 (Display *, Colormap, XColor *);
217 static void x_set_window_size_1 (struct frame *, int, int, int, bool);
218 static void x_raise_frame (struct frame *);
219 static void x_lower_frame (struct frame *);
220 static const XColor *x_color_cells (Display *, int *);
221 static int x_io_error_quitter (Display *);
222 static struct terminal *x_create_terminal (struct x_display_info *);
223 void x_delete_terminal (struct terminal *);
224 static void x_update_end (struct frame *);
225 static void XTframe_up_to_date (struct frame *);
226 static void x_clear_frame (struct frame *);
227 static _Noreturn void x_ins_del_lines (struct frame *, int, int);
228 static void frame_highlight (struct frame *);
229 static void frame_unhighlight (struct frame *);
230 static void x_new_focus_frame (struct x_display_info *, struct frame *);
231 static void x_focus_changed (int, int, struct x_display_info *,
232 struct frame *, struct input_event *);
233 static void XTframe_rehighlight (struct frame *);
234 static void x_frame_rehighlight (struct x_display_info *);
235 static void x_draw_hollow_cursor (struct window *, struct glyph_row *);
236 static void x_draw_bar_cursor (struct window *, struct glyph_row *, int,
237 enum text_cursor_kinds);
239 static void x_clip_to_row (struct window *, struct glyph_row *,
240 enum glyph_row_area, GC);
241 static void x_flush (struct frame *f);
242 static void x_update_begin (struct frame *);
243 static void x_update_window_begin (struct window *);
244 static struct scroll_bar *x_window_to_scroll_bar (Display *, Window);
245 static void x_scroll_bar_report_motion (struct frame **, Lisp_Object *,
246 enum scroll_bar_part *,
247 Lisp_Object *, Lisp_Object *,
248 Time *);
249 static int x_handle_net_wm_state (struct frame *, const XPropertyEvent *);
250 static void x_check_fullscreen (struct frame *);
251 static void x_check_expected_move (struct frame *, int, int);
252 static void x_sync_with_move (struct frame *, int, int, int);
253 static int handle_one_xevent (struct x_display_info *,
254 const XEvent *, int *,
255 struct input_event *);
256 #ifdef USE_GTK
257 static int x_dispatch_event (XEvent *, Display *);
258 #endif
259 /* Don't declare this _Noreturn because we want no
260 interference with debugging failing X calls. */
261 static void x_connection_closed (Display *, const char *);
262 static void x_wm_set_window_state (struct frame *, int);
263 static void x_wm_set_icon_pixmap (struct frame *, ptrdiff_t);
264 static void x_initialize (void);
267 /* Flush display of frame F. */
269 static void
270 x_flush (struct frame *f)
272 eassert (f && FRAME_X_P (f));
273 /* Don't call XFlush when it is not safe to redisplay; the X
274 connection may be broken. */
275 if (!NILP (Vinhibit_redisplay))
276 return;
278 block_input ();
279 XFlush (FRAME_X_DISPLAY (f));
280 unblock_input ();
284 /* Remove calls to XFlush by defining XFlush to an empty replacement.
285 Calls to XFlush should be unnecessary because the X output buffer
286 is flushed automatically as needed by calls to XPending,
287 XNextEvent, or XWindowEvent according to the XFlush man page.
288 XTread_socket calls XPending. Removing XFlush improves
289 performance. */
291 #define XFlush(DISPLAY) (void) 0
294 /***********************************************************************
295 Debugging
296 ***********************************************************************/
298 #if 0
300 /* This is a function useful for recording debugging information about
301 the sequence of occurrences in this file. */
303 struct record
305 char *locus;
306 int type;
309 struct record event_record[100];
311 int event_record_index;
313 void
314 record_event (char *locus, int type)
316 if (event_record_index == sizeof (event_record) / sizeof (struct record))
317 event_record_index = 0;
319 event_record[event_record_index].locus = locus;
320 event_record[event_record_index].type = type;
321 event_record_index++;
324 #endif /* 0 */
328 /* Return the struct x_display_info corresponding to DPY. */
330 struct x_display_info *
331 x_display_info_for_display (Display *dpy)
333 struct x_display_info *dpyinfo;
335 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
336 if (dpyinfo->display == dpy)
337 return dpyinfo;
339 return 0;
342 static Window
343 x_find_topmost_parent (struct frame *f)
345 struct x_output *x = f->output_data.x;
346 Window win = None, wi = x->parent_desc;
347 Display *dpy = FRAME_X_DISPLAY (f);
349 while (wi != FRAME_DISPLAY_INFO (f)->root_window)
351 Window root;
352 Window *children;
353 unsigned int nchildren;
355 win = wi;
356 XQueryTree (dpy, win, &root, &wi, &children, &nchildren);
357 XFree (children);
360 return win;
363 #define OPAQUE 0xffffffff
365 void
366 x_set_frame_alpha (struct frame *f)
368 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
369 Display *dpy = FRAME_X_DISPLAY (f);
370 Window win = FRAME_OUTER_WINDOW (f);
371 double alpha = 1.0;
372 double alpha_min = 1.0;
373 unsigned long opac;
374 Window parent;
376 if (dpyinfo->x_highlight_frame == f)
377 alpha = f->alpha[0];
378 else
379 alpha = f->alpha[1];
381 if (FLOATP (Vframe_alpha_lower_limit))
382 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
383 else if (INTEGERP (Vframe_alpha_lower_limit))
384 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
386 if (alpha < 0.0)
387 return;
388 else if (alpha > 1.0)
389 alpha = 1.0;
390 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
391 alpha = alpha_min;
393 opac = alpha * OPAQUE;
395 x_catch_errors (dpy);
397 /* If there is a parent from the window manager, put the property there
398 also, to work around broken window managers that fail to do that.
399 Do this unconditionally as this function is called on reparent when
400 alpha has not changed on the frame. */
402 parent = x_find_topmost_parent (f);
403 if (parent != None)
404 XChangeProperty (dpy, parent, dpyinfo->Xatom_net_wm_window_opacity,
405 XA_CARDINAL, 32, PropModeReplace,
406 (unsigned char *) &opac, 1L);
408 /* return unless necessary */
410 unsigned char *data;
411 Atom actual;
412 int rc, format;
413 unsigned long n, left;
415 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
416 0L, 1L, False, XA_CARDINAL,
417 &actual, &format, &n, &left,
418 &data);
420 if (rc == Success && actual != None)
422 unsigned long value = *(unsigned long *)data;
423 XFree (data);
424 if (value == opac)
426 x_uncatch_errors ();
427 return;
432 XChangeProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
433 XA_CARDINAL, 32, PropModeReplace,
434 (unsigned char *) &opac, 1L);
435 x_uncatch_errors ();
439 x_display_pixel_height (struct x_display_info *dpyinfo)
441 return HeightOfScreen (dpyinfo->screen);
445 x_display_pixel_width (struct x_display_info *dpyinfo)
447 return WidthOfScreen (dpyinfo->screen);
451 /***********************************************************************
452 Starting and ending an update
453 ***********************************************************************/
455 /* Start an update of frame F. This function is installed as a hook
456 for update_begin, i.e. it is called when update_begin is called.
457 This function is called prior to calls to x_update_window_begin for
458 each window being updated. Currently, there is nothing to do here
459 because all interesting stuff is done on a window basis. */
461 static void
462 x_update_begin (struct frame *f)
464 /* Nothing to do. */
468 /* Start update of window W. */
470 static void
471 x_update_window_begin (struct window *w)
473 struct frame *f = XFRAME (WINDOW_FRAME (w));
474 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
476 w->output_cursor = w->cursor;
478 block_input ();
480 if (f == hlinfo->mouse_face_mouse_frame)
482 /* Don't do highlighting for mouse motion during the update. */
483 hlinfo->mouse_face_defer = 1;
485 /* If F needs to be redrawn, simply forget about any prior mouse
486 highlighting. */
487 if (FRAME_GARBAGED_P (f))
488 hlinfo->mouse_face_window = Qnil;
491 unblock_input ();
495 /* Draw a vertical window border from (x,y0) to (x,y1) */
497 static void
498 x_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
500 struct frame *f = XFRAME (WINDOW_FRAME (w));
501 struct face *face;
503 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
504 if (face)
505 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
506 face->foreground);
508 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
509 f->output_data.x->normal_gc, x, y0, x, y1);
512 /* Draw a window divider from (x0,y0) to (x1,y1) */
514 static void
515 x_draw_window_divider (struct window *w, int x0, int x1, int y0, int y1)
517 struct frame *f = XFRAME (WINDOW_FRAME (w));
518 struct face *face;
520 face = FACE_FROM_ID (f, WINDOW_DIVIDER_FACE_ID);
521 if (face)
522 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
523 face->foreground);
525 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
526 f->output_data.x->normal_gc, x0, y0, x1 - x0, y1 - y0);
529 /* End update of window W.
531 Draw vertical borders between horizontally adjacent windows, and
532 display W's cursor if CURSOR_ON_P is non-zero.
534 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
535 glyphs in mouse-face were overwritten. In that case we have to
536 make sure that the mouse-highlight is properly redrawn.
538 W may be a menu bar pseudo-window in case we don't have X toolkit
539 support. Such windows don't have a cursor, so don't display it
540 here. */
542 static void
543 x_update_window_end (struct window *w, bool cursor_on_p,
544 bool mouse_face_overwritten_p)
546 if (!w->pseudo_window_p)
548 block_input ();
550 if (cursor_on_p)
551 display_and_set_cursor (w, 1,
552 w->output_cursor.hpos, w->output_cursor.vpos,
553 w->output_cursor.x, w->output_cursor.y);
555 if (draw_window_fringes (w, 1))
557 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
558 x_draw_right_divider (w);
559 else
560 x_draw_vertical_border (w);
563 unblock_input ();
566 /* If a row with mouse-face was overwritten, arrange for
567 XTframe_up_to_date to redisplay the mouse highlight. */
568 if (mouse_face_overwritten_p)
569 reset_mouse_highlight (MOUSE_HL_INFO (XFRAME (w->frame)));
573 /* End update of frame F. This function is installed as a hook in
574 update_end. */
576 static void
577 x_update_end (struct frame *f)
579 /* Mouse highlight may be displayed again. */
580 MOUSE_HL_INFO (f)->mouse_face_defer = 0;
582 #ifndef XFlush
583 block_input ();
584 XFlush (FRAME_X_DISPLAY (f));
585 unblock_input ();
586 #endif
590 /* This function is called from various places in xdisp.c
591 whenever a complete update has been performed. */
593 static void
594 XTframe_up_to_date (struct frame *f)
596 if (FRAME_X_P (f))
597 FRAME_MOUSE_UPDATE (f);
601 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
602 arrow bitmaps, or clear the fringes if no bitmaps are required
603 before DESIRED_ROW is made current. This function is called from
604 update_window_line only if it is known that there are differences
605 between bitmaps to be drawn between current row and DESIRED_ROW. */
607 static void
608 x_after_update_window_line (struct window *w, struct glyph_row *desired_row)
610 struct frame *f;
611 int width, height;
613 eassert (w);
615 if (!desired_row->mode_line_p && !w->pseudo_window_p)
616 desired_row->redraw_fringe_bitmaps_p = 1;
618 /* When a window has disappeared, make sure that no rest of
619 full-width rows stays visible in the internal border. Could
620 check here if updated window is the leftmost/rightmost window,
621 but I guess it's not worth doing since vertically split windows
622 are almost never used, internal border is rarely set, and the
623 overhead is very small. */
624 if (windows_or_buffers_changed
625 && desired_row->full_width_p
626 && (f = XFRAME (w->frame),
627 width = FRAME_INTERNAL_BORDER_WIDTH (f),
628 width != 0)
629 && (height = desired_row->visible_height,
630 height > 0))
632 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
634 block_input ();
635 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
636 0, y, width, height);
637 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
638 FRAME_PIXEL_WIDTH (f) - width,
639 y, width, height);
640 unblock_input ();
644 static void
645 x_draw_fringe_bitmap (struct window *w, struct glyph_row *row, struct draw_fringe_bitmap_params *p)
647 struct frame *f = XFRAME (WINDOW_FRAME (w));
648 Display *display = FRAME_X_DISPLAY (f);
649 Window window = FRAME_X_WINDOW (f);
650 GC gc = f->output_data.x->normal_gc;
651 struct face *face = p->face;
653 /* Must clip because of partially visible lines. */
654 x_clip_to_row (w, row, ANY_AREA, gc);
656 if (p->bx >= 0 && !p->overlay_p)
658 /* In case the same realized face is used for fringes and
659 for something displayed in the text (e.g. face `region' on
660 mono-displays, the fill style may have been changed to
661 FillSolid in x_draw_glyph_string_background. */
662 if (face->stipple)
663 XSetFillStyle (display, face->gc, FillOpaqueStippled);
664 else
665 XSetForeground (display, face->gc, face->background);
667 XFillRectangle (display, window, face->gc,
668 p->bx, p->by, p->nx, p->ny);
670 if (!face->stipple)
671 XSetForeground (display, face->gc, face->foreground);
674 if (p->which)
676 char *bits;
677 Pixmap pixmap, clipmask = (Pixmap) 0;
678 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
679 XGCValues gcv;
681 if (p->wd > 8)
682 bits = (char *) (p->bits + p->dh);
683 else
684 bits = (char *) p->bits + p->dh;
686 /* Draw the bitmap. I believe these small pixmaps can be cached
687 by the server. */
688 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
689 (p->cursor_p
690 ? (p->overlay_p ? face->background
691 : f->output_data.x->cursor_pixel)
692 : face->foreground),
693 face->background, depth);
695 if (p->overlay_p)
697 clipmask = XCreatePixmapFromBitmapData (display,
698 FRAME_DISPLAY_INFO (f)->root_window,
699 bits, p->wd, p->h,
700 1, 0, 1);
701 gcv.clip_mask = clipmask;
702 gcv.clip_x_origin = p->x;
703 gcv.clip_y_origin = p->y;
704 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
707 XCopyArea (display, pixmap, window, gc, 0, 0,
708 p->wd, p->h, p->x, p->y);
709 XFreePixmap (display, pixmap);
711 if (p->overlay_p)
713 gcv.clip_mask = (Pixmap) 0;
714 XChangeGC (display, gc, GCClipMask, &gcv);
715 XFreePixmap (display, clipmask);
719 XSetClipMask (display, gc, None);
722 /***********************************************************************
723 Glyph display
724 ***********************************************************************/
728 static void x_set_glyph_string_clipping (struct glyph_string *);
729 static void x_set_glyph_string_gc (struct glyph_string *);
730 static void x_draw_glyph_string_foreground (struct glyph_string *);
731 static void x_draw_composite_glyph_string_foreground (struct glyph_string *);
732 static void x_draw_glyph_string_box (struct glyph_string *);
733 static void x_draw_glyph_string (struct glyph_string *);
734 static _Noreturn void x_delete_glyphs (struct frame *, int);
735 static void x_compute_glyph_string_overhangs (struct glyph_string *);
736 static void x_set_cursor_gc (struct glyph_string *);
737 static void x_set_mode_line_face_gc (struct glyph_string *);
738 static void x_set_mouse_face_gc (struct glyph_string *);
739 static bool x_alloc_lighter_color (struct frame *, Display *, Colormap,
740 unsigned long *, double, int);
741 static void x_setup_relief_color (struct frame *, struct relief *,
742 double, int, unsigned long);
743 static void x_setup_relief_colors (struct glyph_string *);
744 static void x_draw_image_glyph_string (struct glyph_string *);
745 static void x_draw_image_relief (struct glyph_string *);
746 static void x_draw_image_foreground (struct glyph_string *);
747 static void x_draw_image_foreground_1 (struct glyph_string *, Pixmap);
748 static void x_clear_glyph_string_rect (struct glyph_string *, int,
749 int, int, int);
750 static void x_draw_relief_rect (struct frame *, int, int, int, int,
751 int, int, int, int, int, int,
752 XRectangle *);
753 static void x_draw_box_rect (struct glyph_string *, int, int, int, int,
754 int, int, int, XRectangle *);
755 static void x_scroll_bar_clear (struct frame *);
757 #ifdef GLYPH_DEBUG
758 static void x_check_font (struct frame *, struct font *);
759 #endif
762 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
763 face. */
765 static void
766 x_set_cursor_gc (struct glyph_string *s)
768 if (s->font == FRAME_FONT (s->f)
769 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
770 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
771 && !s->cmp)
772 s->gc = s->f->output_data.x->cursor_gc;
773 else
775 /* Cursor on non-default face: must merge. */
776 XGCValues xgcv;
777 unsigned long mask;
779 xgcv.background = s->f->output_data.x->cursor_pixel;
780 xgcv.foreground = s->face->background;
782 /* If the glyph would be invisible, try a different foreground. */
783 if (xgcv.foreground == xgcv.background)
784 xgcv.foreground = s->face->foreground;
785 if (xgcv.foreground == xgcv.background)
786 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
787 if (xgcv.foreground == xgcv.background)
788 xgcv.foreground = s->face->foreground;
790 /* Make sure the cursor is distinct from text in this face. */
791 if (xgcv.background == s->face->background
792 && xgcv.foreground == s->face->foreground)
794 xgcv.background = s->face->foreground;
795 xgcv.foreground = s->face->background;
798 IF_DEBUG (x_check_font (s->f, s->font));
799 xgcv.graphics_exposures = False;
800 mask = GCForeground | GCBackground | GCGraphicsExposures;
802 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
803 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
804 mask, &xgcv);
805 else
806 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
807 = XCreateGC (s->display, s->window, mask, &xgcv);
809 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
814 /* Set up S->gc of glyph string S for drawing text in mouse face. */
816 static void
817 x_set_mouse_face_gc (struct glyph_string *s)
819 int face_id;
820 struct face *face;
822 /* What face has to be used last for the mouse face? */
823 face_id = MOUSE_HL_INFO (s->f)->mouse_face_face_id;
824 face = FACE_FROM_ID (s->f, face_id);
825 if (face == NULL)
826 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
828 if (s->first_glyph->type == CHAR_GLYPH)
829 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
830 else
831 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
832 s->face = FACE_FROM_ID (s->f, face_id);
833 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
835 if (s->font == s->face->font)
836 s->gc = s->face->gc;
837 else
839 /* Otherwise construct scratch_cursor_gc with values from FACE
840 except for FONT. */
841 XGCValues xgcv;
842 unsigned long mask;
844 xgcv.background = s->face->background;
845 xgcv.foreground = s->face->foreground;
846 xgcv.graphics_exposures = False;
847 mask = GCForeground | GCBackground | GCGraphicsExposures;
849 if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
850 XChangeGC (s->display, FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
851 mask, &xgcv);
852 else
853 FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
854 = XCreateGC (s->display, s->window, mask, &xgcv);
856 s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
859 eassert (s->gc != 0);
863 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
864 Faces to use in the mode line have already been computed when the
865 matrix was built, so there isn't much to do, here. */
867 static void
868 x_set_mode_line_face_gc (struct glyph_string *s)
870 s->gc = s->face->gc;
874 /* Set S->gc of glyph string S for drawing that glyph string. Set
875 S->stippled_p to a non-zero value if the face of S has a stipple
876 pattern. */
878 static void
879 x_set_glyph_string_gc (struct glyph_string *s)
881 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
883 if (s->hl == DRAW_NORMAL_TEXT)
885 s->gc = s->face->gc;
886 s->stippled_p = s->face->stipple != 0;
888 else if (s->hl == DRAW_INVERSE_VIDEO)
890 x_set_mode_line_face_gc (s);
891 s->stippled_p = s->face->stipple != 0;
893 else if (s->hl == DRAW_CURSOR)
895 x_set_cursor_gc (s);
896 s->stippled_p = 0;
898 else if (s->hl == DRAW_MOUSE_FACE)
900 x_set_mouse_face_gc (s);
901 s->stippled_p = s->face->stipple != 0;
903 else if (s->hl == DRAW_IMAGE_RAISED
904 || s->hl == DRAW_IMAGE_SUNKEN)
906 s->gc = s->face->gc;
907 s->stippled_p = s->face->stipple != 0;
909 else
911 s->gc = s->face->gc;
912 s->stippled_p = s->face->stipple != 0;
915 /* GC must have been set. */
916 eassert (s->gc != 0);
920 /* Set clipping for output of glyph string S. S may be part of a mode
921 line or menu if we don't have X toolkit support. */
923 static void
924 x_set_glyph_string_clipping (struct glyph_string *s)
926 XRectangle *r = s->clip;
927 int n = get_glyph_string_clip_rects (s, r, 2);
929 if (n > 0)
930 XSetClipRectangles (s->display, s->gc, 0, 0, r, n, Unsorted);
931 s->num_clips = n;
935 /* Set SRC's clipping for output of glyph string DST. This is called
936 when we are drawing DST's left_overhang or right_overhang only in
937 the area of SRC. */
939 static void
940 x_set_glyph_string_clipping_exactly (struct glyph_string *src, struct glyph_string *dst)
942 XRectangle r;
944 r.x = src->x;
945 r.width = src->width;
946 r.y = src->y;
947 r.height = src->height;
948 dst->clip[0] = r;
949 dst->num_clips = 1;
950 XSetClipRectangles (dst->display, dst->gc, 0, 0, &r, 1, Unsorted);
954 /* RIF:
955 Compute left and right overhang of glyph string S. */
957 static void
958 x_compute_glyph_string_overhangs (struct glyph_string *s)
960 if (s->cmp == NULL
961 && (s->first_glyph->type == CHAR_GLYPH
962 || s->first_glyph->type == COMPOSITE_GLYPH))
964 struct font_metrics metrics;
966 if (s->first_glyph->type == CHAR_GLYPH)
968 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
969 struct font *font = s->font;
970 int i;
972 for (i = 0; i < s->nchars; i++)
973 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
974 font->driver->text_extents (font, code, s->nchars, &metrics);
976 else
978 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
980 composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
982 s->right_overhang = (metrics.rbearing > metrics.width
983 ? metrics.rbearing - metrics.width : 0);
984 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
986 else if (s->cmp)
988 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
989 s->left_overhang = - s->cmp->lbearing;
994 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
996 static void
997 x_clear_glyph_string_rect (struct glyph_string *s, int x, int y, int w, int h)
999 XGCValues xgcv;
1000 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1001 XSetForeground (s->display, s->gc, xgcv.background);
1002 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1003 XSetForeground (s->display, s->gc, xgcv.foreground);
1007 /* Draw the background of glyph_string S. If S->background_filled_p
1008 is non-zero don't draw it. FORCE_P non-zero means draw the
1009 background even if it wouldn't be drawn normally. This is used
1010 when a string preceding S draws into the background of S, or S
1011 contains the first component of a composition. */
1013 static void
1014 x_draw_glyph_string_background (struct glyph_string *s, bool force_p)
1016 /* Nothing to do if background has already been drawn or if it
1017 shouldn't be drawn in the first place. */
1018 if (!s->background_filled_p)
1020 int box_line_width = max (s->face->box_line_width, 0);
1022 if (s->stippled_p)
1024 /* Fill background with a stipple pattern. */
1025 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1026 XFillRectangle (s->display, s->window, s->gc, s->x,
1027 s->y + box_line_width,
1028 s->background_width,
1029 s->height - 2 * box_line_width);
1030 XSetFillStyle (s->display, s->gc, FillSolid);
1031 s->background_filled_p = 1;
1033 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1034 || s->font_not_found_p
1035 || s->extends_to_end_of_line_p
1036 || force_p)
1038 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1039 s->background_width,
1040 s->height - 2 * box_line_width);
1041 s->background_filled_p = 1;
1047 /* Draw the foreground of glyph string S. */
1049 static void
1050 x_draw_glyph_string_foreground (struct glyph_string *s)
1052 int i, x;
1054 /* If first glyph of S has a left box line, start drawing the text
1055 of S to the right of that box line. */
1056 if (s->face->box != FACE_NO_BOX
1057 && s->first_glyph->left_box_line_p)
1058 x = s->x + eabs (s->face->box_line_width);
1059 else
1060 x = s->x;
1062 /* Draw characters of S as rectangles if S's font could not be
1063 loaded. */
1064 if (s->font_not_found_p)
1066 for (i = 0; i < s->nchars; ++i)
1068 struct glyph *g = s->first_glyph + i;
1069 XDrawRectangle (s->display, s->window,
1070 s->gc, x, s->y, g->pixel_width - 1,
1071 s->height - 1);
1072 x += g->pixel_width;
1075 else
1077 struct font *font = s->font;
1078 int boff = font->baseline_offset;
1079 int y;
1081 if (font->vertical_centering)
1082 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1084 y = s->ybase - boff;
1085 if (s->for_overlaps
1086 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1087 font->driver->draw (s, 0, s->nchars, x, y, 0);
1088 else
1089 font->driver->draw (s, 0, s->nchars, x, y, 1);
1090 if (s->face->overstrike)
1091 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1095 /* Draw the foreground of composite glyph string S. */
1097 static void
1098 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1100 int i, j, x;
1101 struct font *font = s->font;
1103 /* If first glyph of S has a left box line, start drawing the text
1104 of S to the right of that box line. */
1105 if (s->face && s->face->box != FACE_NO_BOX
1106 && s->first_glyph->left_box_line_p)
1107 x = s->x + eabs (s->face->box_line_width);
1108 else
1109 x = s->x;
1111 /* S is a glyph string for a composition. S->cmp_from is the index
1112 of the first character drawn for glyphs of this composition.
1113 S->cmp_from == 0 means we are drawing the very first character of
1114 this composition. */
1116 /* Draw a rectangle for the composition if the font for the very
1117 first character of the composition could not be loaded. */
1118 if (s->font_not_found_p)
1120 if (s->cmp_from == 0)
1121 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1122 s->width - 1, s->height - 1);
1124 else if (! s->first_glyph->u.cmp.automatic)
1126 int y = s->ybase;
1128 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1129 /* TAB in a composition means display glyphs with padding
1130 space on the left or right. */
1131 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1133 int xx = x + s->cmp->offsets[j * 2];
1134 int yy = y - s->cmp->offsets[j * 2 + 1];
1136 font->driver->draw (s, j, j + 1, xx, yy, 0);
1137 if (s->face->overstrike)
1138 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
1141 else
1143 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1144 Lisp_Object glyph;
1145 int y = s->ybase;
1146 int width = 0;
1148 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1150 glyph = LGSTRING_GLYPH (gstring, i);
1151 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1152 width += LGLYPH_WIDTH (glyph);
1153 else
1155 int xoff, yoff, wadjust;
1157 if (j < i)
1159 font->driver->draw (s, j, i, x, y, 0);
1160 if (s->face->overstrike)
1161 font->driver->draw (s, j, i, x + 1, y, 0);
1162 x += width;
1164 xoff = LGLYPH_XOFF (glyph);
1165 yoff = LGLYPH_YOFF (glyph);
1166 wadjust = LGLYPH_WADJUST (glyph);
1167 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1168 if (s->face->overstrike)
1169 font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff, 0);
1170 x += wadjust;
1171 j = i + 1;
1172 width = 0;
1175 if (j < i)
1177 font->driver->draw (s, j, i, x, y, 0);
1178 if (s->face->overstrike)
1179 font->driver->draw (s, j, i, x + 1, y, 0);
1185 /* Draw the foreground of glyph string S for glyphless characters. */
1187 static void
1188 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1190 struct glyph *glyph = s->first_glyph;
1191 XChar2b char2b[8];
1192 int x, i, j;
1194 /* If first glyph of S has a left box line, start drawing the text
1195 of S to the right of that box line. */
1196 if (s->face && s->face->box != FACE_NO_BOX
1197 && s->first_glyph->left_box_line_p)
1198 x = s->x + eabs (s->face->box_line_width);
1199 else
1200 x = s->x;
1202 s->char2b = char2b;
1204 for (i = 0; i < s->nchars; i++, glyph++)
1206 char buf[7], *str = NULL;
1207 int len = glyph->u.glyphless.len;
1209 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1211 if (len > 0
1212 && CHAR_TABLE_P (Vglyphless_char_display)
1213 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1214 >= 1))
1216 Lisp_Object acronym
1217 = (! glyph->u.glyphless.for_no_font
1218 ? CHAR_TABLE_REF (Vglyphless_char_display,
1219 glyph->u.glyphless.ch)
1220 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
1221 if (STRINGP (acronym))
1222 str = SSDATA (acronym);
1225 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
1227 sprintf (buf, "%0*X",
1228 glyph->u.glyphless.ch < 0x10000 ? 4 : 6,
1229 glyph->u.glyphless.ch);
1230 str = buf;
1233 if (str)
1235 int upper_len = (len + 1) / 2;
1236 unsigned code;
1238 /* It is assured that all LEN characters in STR is ASCII. */
1239 for (j = 0; j < len; j++)
1241 code = s->font->driver->encode_char (s->font, str[j]);
1242 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
1244 s->font->driver->draw (s, 0, upper_len,
1245 x + glyph->slice.glyphless.upper_xoff,
1246 s->ybase + glyph->slice.glyphless.upper_yoff,
1248 s->font->driver->draw (s, upper_len, len,
1249 x + glyph->slice.glyphless.lower_xoff,
1250 s->ybase + glyph->slice.glyphless.lower_yoff,
1253 if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
1254 XDrawRectangle (s->display, s->window, s->gc,
1255 x, s->ybase - glyph->ascent,
1256 glyph->pixel_width - 1,
1257 glyph->ascent + glyph->descent - 1);
1258 x += glyph->pixel_width;
1262 #ifdef USE_X_TOOLKIT
1264 #ifdef USE_LUCID
1266 /* Return the frame on which widget WIDGET is used.. Abort if frame
1267 cannot be determined. */
1269 static struct frame *
1270 x_frame_of_widget (Widget widget)
1272 struct x_display_info *dpyinfo;
1273 Lisp_Object tail, frame;
1274 struct frame *f;
1276 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1278 /* Find the top-level shell of the widget. Note that this function
1279 can be called when the widget is not yet realized, so XtWindow
1280 (widget) == 0. That's the reason we can't simply use
1281 x_any_window_to_frame. */
1282 while (!XtIsTopLevelShell (widget))
1283 widget = XtParent (widget);
1285 /* Look for a frame with that top-level widget. Allocate the color
1286 on that frame to get the right gamma correction value. */
1287 FOR_EACH_FRAME (tail, frame)
1289 f = XFRAME (frame);
1290 if (FRAME_X_P (f)
1291 && f->output_data.nothing != 1
1292 && FRAME_DISPLAY_INFO (f) == dpyinfo
1293 && f->output_data.x->widget == widget)
1294 return f;
1296 emacs_abort ();
1299 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1300 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1301 If this produces the same color as PIXEL, try a color where all RGB
1302 values have DELTA added. Return the allocated color in *PIXEL.
1303 DISPLAY is the X display, CMAP is the colormap to operate on.
1304 Value is true if successful. */
1306 bool
1307 x_alloc_lighter_color_for_widget (Widget widget, Display *display, Colormap cmap,
1308 unsigned long *pixel, double factor, int delta)
1310 struct frame *f = x_frame_of_widget (widget);
1311 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1314 #endif /* USE_LUCID */
1317 /* Structure specifying which arguments should be passed by Xt to
1318 cvt_string_to_pixel. We want the widget's screen and colormap. */
1320 static XtConvertArgRec cvt_string_to_pixel_args[] =
1322 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1323 sizeof (Screen *)},
1324 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1325 sizeof (Colormap)}
1329 /* The address of this variable is returned by
1330 cvt_string_to_pixel. */
1332 static Pixel cvt_string_to_pixel_value;
1335 /* Convert a color name to a pixel color.
1337 DPY is the display we are working on.
1339 ARGS is an array of *NARGS XrmValue structures holding additional
1340 information about the widget for which the conversion takes place.
1341 The contents of this array are determined by the specification
1342 in cvt_string_to_pixel_args.
1344 FROM is a pointer to an XrmValue which points to the color name to
1345 convert. TO is an XrmValue in which to return the pixel color.
1347 CLOSURE_RET is a pointer to user-data, in which we record if
1348 we allocated the color or not.
1350 Value is True if successful, False otherwise. */
1352 static Boolean
1353 cvt_string_to_pixel (Display *dpy, XrmValue *args, Cardinal *nargs,
1354 XrmValue *from, XrmValue *to,
1355 XtPointer *closure_ret)
1357 Screen *screen;
1358 Colormap cmap;
1359 Pixel pixel;
1360 String color_name;
1361 XColor color;
1363 if (*nargs != 2)
1365 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1366 "wrongParameters", "cvt_string_to_pixel",
1367 "XtToolkitError",
1368 "Screen and colormap args required", NULL, NULL);
1369 return False;
1372 screen = *(Screen **) args[0].addr;
1373 cmap = *(Colormap *) args[1].addr;
1374 color_name = (String) from->addr;
1376 if (strcmp (color_name, XtDefaultBackground) == 0)
1378 *closure_ret = (XtPointer) False;
1379 pixel = WhitePixelOfScreen (screen);
1381 else if (strcmp (color_name, XtDefaultForeground) == 0)
1383 *closure_ret = (XtPointer) False;
1384 pixel = BlackPixelOfScreen (screen);
1386 else if (XParseColor (dpy, cmap, color_name, &color)
1387 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1389 pixel = color.pixel;
1390 *closure_ret = (XtPointer) True;
1392 else
1394 String params[1];
1395 Cardinal nparams = 1;
1397 params[0] = color_name;
1398 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1399 "badValue", "cvt_string_to_pixel",
1400 "XtToolkitError", "Invalid color `%s'",
1401 params, &nparams);
1402 return False;
1405 if (to->addr != NULL)
1407 if (to->size < sizeof (Pixel))
1409 to->size = sizeof (Pixel);
1410 return False;
1413 *(Pixel *) to->addr = pixel;
1415 else
1417 cvt_string_to_pixel_value = pixel;
1418 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1421 to->size = sizeof (Pixel);
1422 return True;
1426 /* Free a pixel color which was previously allocated via
1427 cvt_string_to_pixel. This is registered as the destructor
1428 for this type of resource via XtSetTypeConverter.
1430 APP is the application context in which we work.
1432 TO is a pointer to an XrmValue holding the color to free.
1433 CLOSURE is the value we stored in CLOSURE_RET for this color
1434 in cvt_string_to_pixel.
1436 ARGS and NARGS are like for cvt_string_to_pixel. */
1438 static void
1439 cvt_pixel_dtor (XtAppContext app, XrmValuePtr to, XtPointer closure, XrmValuePtr args,
1440 Cardinal *nargs)
1442 if (*nargs != 2)
1444 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1445 "XtToolkitError",
1446 "Screen and colormap arguments required",
1447 NULL, NULL);
1449 else if (closure != NULL)
1451 /* We did allocate the pixel, so free it. */
1452 Screen *screen = *(Screen **) args[0].addr;
1453 Colormap cmap = *(Colormap *) args[1].addr;
1454 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1455 (Pixel *) to->addr, 1);
1460 #endif /* USE_X_TOOLKIT */
1463 /* Value is an array of XColor structures for the contents of the
1464 color map of display DPY. Set *NCELLS to the size of the array.
1465 Note that this probably shouldn't be called for large color maps,
1466 say a 24-bit TrueColor map. */
1468 static const XColor *
1469 x_color_cells (Display *dpy, int *ncells)
1471 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1473 if (dpyinfo->color_cells == NULL)
1475 Screen *screen = dpyinfo->screen;
1476 int ncolor_cells = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1477 int i;
1479 dpyinfo->color_cells = xnmalloc (ncolor_cells,
1480 sizeof *dpyinfo->color_cells);
1481 dpyinfo->ncolor_cells = ncolor_cells;
1483 for (i = 0; i < ncolor_cells; ++i)
1484 dpyinfo->color_cells[i].pixel = i;
1486 XQueryColors (dpy, dpyinfo->cmap,
1487 dpyinfo->color_cells, ncolor_cells);
1490 *ncells = dpyinfo->ncolor_cells;
1491 return dpyinfo->color_cells;
1495 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1496 colors in COLORS. Use cached information, if available. */
1498 void
1499 x_query_colors (struct frame *f, XColor *colors, int ncolors)
1501 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1503 if (dpyinfo->color_cells)
1505 int i;
1506 for (i = 0; i < ncolors; ++i)
1508 unsigned long pixel = colors[i].pixel;
1509 eassert (pixel < dpyinfo->ncolor_cells);
1510 eassert (dpyinfo->color_cells[pixel].pixel == pixel);
1511 colors[i] = dpyinfo->color_cells[pixel];
1514 else
1515 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1519 /* On frame F, translate pixel color to RGB values for the color in
1520 COLOR. Use cached information, if available. */
1522 void
1523 x_query_color (struct frame *f, XColor *color)
1525 x_query_colors (f, color, 1);
1529 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1530 exact match can't be allocated, try the nearest color available.
1531 Value is true if successful. Set *COLOR to the color
1532 allocated. */
1534 static bool
1535 x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color)
1537 bool rc;
1539 rc = XAllocColor (dpy, cmap, color) != 0;
1540 if (rc == 0)
1542 /* If we got to this point, the colormap is full, so we're going
1543 to try to get the next closest color. The algorithm used is
1544 a least-squares matching, which is what X uses for closest
1545 color matching with StaticColor visuals. */
1546 int nearest, i;
1547 int max_color_delta = 255;
1548 int max_delta = 3 * max_color_delta;
1549 int nearest_delta = max_delta + 1;
1550 int ncells;
1551 const XColor *cells = x_color_cells (dpy, &ncells);
1553 for (nearest = i = 0; i < ncells; ++i)
1555 int dred = (color->red >> 8) - (cells[i].red >> 8);
1556 int dgreen = (color->green >> 8) - (cells[i].green >> 8);
1557 int dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1558 int delta = dred * dred + dgreen * dgreen + dblue * dblue;
1560 if (delta < nearest_delta)
1562 nearest = i;
1563 nearest_delta = delta;
1567 color->red = cells[nearest].red;
1568 color->green = cells[nearest].green;
1569 color->blue = cells[nearest].blue;
1570 rc = XAllocColor (dpy, cmap, color) != 0;
1572 else
1574 /* If allocation succeeded, and the allocated pixel color is not
1575 equal to a cached pixel color recorded earlier, there was a
1576 change in the colormap, so clear the color cache. */
1577 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1578 XColor *cached_color;
1580 if (dpyinfo->color_cells
1581 && (cached_color = &dpyinfo->color_cells[color->pixel],
1582 (cached_color->red != color->red
1583 || cached_color->blue != color->blue
1584 || cached_color->green != color->green)))
1586 xfree (dpyinfo->color_cells);
1587 dpyinfo->color_cells = NULL;
1588 dpyinfo->ncolor_cells = 0;
1592 #ifdef DEBUG_X_COLORS
1593 if (rc)
1594 register_color (color->pixel);
1595 #endif /* DEBUG_X_COLORS */
1597 return rc;
1601 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1602 exact match can't be allocated, try the nearest color available.
1603 Value is true if successful. Set *COLOR to the color
1604 allocated. */
1606 bool
1607 x_alloc_nearest_color (struct frame *f, Colormap cmap, XColor *color)
1609 gamma_correct (f, color);
1610 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1614 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1615 It's necessary to do this instead of just using PIXEL directly to
1616 get color reference counts right. */
1618 unsigned long
1619 x_copy_color (struct frame *f, unsigned long pixel)
1621 XColor color;
1623 color.pixel = pixel;
1624 block_input ();
1625 x_query_color (f, &color);
1626 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1627 unblock_input ();
1628 #ifdef DEBUG_X_COLORS
1629 register_color (pixel);
1630 #endif
1631 return color.pixel;
1635 /* Brightness beyond which a color won't have its highlight brightness
1636 boosted.
1638 Nominally, highlight colors for `3d' faces are calculated by
1639 brightening an object's color by a constant scale factor, but this
1640 doesn't yield good results for dark colors, so for colors who's
1641 brightness is less than this value (on a scale of 0-65535) have an
1642 use an additional additive factor.
1644 The value here is set so that the default menu-bar/mode-line color
1645 (grey75) will not have its highlights changed at all. */
1646 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1649 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1650 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1651 If this produces the same color as PIXEL, try a color where all RGB
1652 values have DELTA added. Return the allocated color in *PIXEL.
1653 DISPLAY is the X display, CMAP is the colormap to operate on.
1654 Value is non-zero if successful. */
1656 static bool
1657 x_alloc_lighter_color (struct frame *f, Display *display, Colormap cmap,
1658 unsigned long *pixel, double factor, int delta)
1660 XColor color, new;
1661 long bright;
1662 bool success_p;
1664 /* Get RGB color values. */
1665 color.pixel = *pixel;
1666 x_query_color (f, &color);
1668 /* Change RGB values by specified FACTOR. Avoid overflow! */
1669 eassert (factor >= 0);
1670 new.red = min (0xffff, factor * color.red);
1671 new.green = min (0xffff, factor * color.green);
1672 new.blue = min (0xffff, factor * color.blue);
1674 /* Calculate brightness of COLOR. */
1675 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1677 /* We only boost colors that are darker than
1678 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1679 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1680 /* Make an additive adjustment to NEW, because it's dark enough so
1681 that scaling by FACTOR alone isn't enough. */
1683 /* How far below the limit this color is (0 - 1, 1 being darker). */
1684 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1685 /* The additive adjustment. */
1686 int min_delta = delta * dimness * factor / 2;
1688 if (factor < 1)
1690 new.red = max (0, new.red - min_delta);
1691 new.green = max (0, new.green - min_delta);
1692 new.blue = max (0, new.blue - min_delta);
1694 else
1696 new.red = min (0xffff, min_delta + new.red);
1697 new.green = min (0xffff, min_delta + new.green);
1698 new.blue = min (0xffff, min_delta + new.blue);
1702 /* Try to allocate the color. */
1703 success_p = x_alloc_nearest_color (f, cmap, &new);
1704 if (success_p)
1706 if (new.pixel == *pixel)
1708 /* If we end up with the same color as before, try adding
1709 delta to the RGB values. */
1710 x_free_colors (f, &new.pixel, 1);
1712 new.red = min (0xffff, delta + color.red);
1713 new.green = min (0xffff, delta + color.green);
1714 new.blue = min (0xffff, delta + color.blue);
1715 success_p = x_alloc_nearest_color (f, cmap, &new);
1717 else
1718 success_p = 1;
1719 *pixel = new.pixel;
1722 return success_p;
1726 /* Set up the foreground color for drawing relief lines of glyph
1727 string S. RELIEF is a pointer to a struct relief containing the GC
1728 with which lines will be drawn. Use a color that is FACTOR or
1729 DELTA lighter or darker than the relief's background which is found
1730 in S->f->output_data.x->relief_background. If such a color cannot
1731 be allocated, use DEFAULT_PIXEL, instead. */
1733 static void
1734 x_setup_relief_color (struct frame *f, struct relief *relief, double factor,
1735 int delta, unsigned long default_pixel)
1737 XGCValues xgcv;
1738 struct x_output *di = f->output_data.x;
1739 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1740 unsigned long pixel;
1741 unsigned long background = di->relief_background;
1742 Colormap cmap = FRAME_X_COLORMAP (f);
1743 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1744 Display *dpy = FRAME_X_DISPLAY (f);
1746 xgcv.graphics_exposures = False;
1747 xgcv.line_width = 1;
1749 /* Free previously allocated color. The color cell will be reused
1750 when it has been freed as many times as it was allocated, so this
1751 doesn't affect faces using the same colors. */
1752 if (relief->gc && relief->pixel != -1)
1754 x_free_colors (f, &relief->pixel, 1);
1755 relief->pixel = -1;
1758 /* Allocate new color. */
1759 xgcv.foreground = default_pixel;
1760 pixel = background;
1761 if (dpyinfo->n_planes != 1
1762 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1763 xgcv.foreground = relief->pixel = pixel;
1765 if (relief->gc == 0)
1767 xgcv.stipple = dpyinfo->gray;
1768 mask |= GCStipple;
1769 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1771 else
1772 XChangeGC (dpy, relief->gc, mask, &xgcv);
1776 /* Set up colors for the relief lines around glyph string S. */
1778 static void
1779 x_setup_relief_colors (struct glyph_string *s)
1781 struct x_output *di = s->f->output_data.x;
1782 unsigned long color;
1784 if (s->face->use_box_color_for_shadows_p)
1785 color = s->face->box_color;
1786 else if (s->first_glyph->type == IMAGE_GLYPH
1787 && s->img->pixmap
1788 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1789 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1790 else
1792 XGCValues xgcv;
1794 /* Get the background color of the face. */
1795 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
1796 color = xgcv.background;
1799 if (di->white_relief.gc == 0
1800 || color != di->relief_background)
1802 di->relief_background = color;
1803 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1804 WHITE_PIX_DEFAULT (s->f));
1805 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1806 BLACK_PIX_DEFAULT (s->f));
1811 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
1812 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
1813 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
1814 relief. LEFT_P non-zero means draw a relief on the left side of
1815 the rectangle. RIGHT_P non-zero means draw a relief on the right
1816 side of the rectangle. CLIP_RECT is the clipping rectangle to use
1817 when drawing. */
1819 static void
1820 x_draw_relief_rect (struct frame *f,
1821 int left_x, int top_y, int right_x, int bottom_y, int width,
1822 int raised_p, int top_p, int bot_p, int left_p, int right_p,
1823 XRectangle *clip_rect)
1825 Display *dpy = FRAME_X_DISPLAY (f);
1826 Window window = FRAME_X_WINDOW (f);
1827 int i;
1828 GC gc;
1830 if (raised_p)
1831 gc = f->output_data.x->white_relief.gc;
1832 else
1833 gc = f->output_data.x->black_relief.gc;
1834 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
1836 /* This code is more complicated than it has to be, because of two
1837 minor hacks to make the boxes look nicer: (i) if width > 1, draw
1838 the outermost line using the black relief. (ii) Omit the four
1839 corner pixels. */
1841 /* Top. */
1842 if (top_p)
1844 if (width == 1)
1845 XDrawLine (dpy, window, gc,
1846 left_x + (left_p ? 1 : 0), top_y,
1847 right_x + (right_p ? 0 : 1), top_y);
1849 for (i = 1; i < width; ++i)
1850 XDrawLine (dpy, window, gc,
1851 left_x + i * left_p, top_y + i,
1852 right_x + 1 - i * right_p, top_y + i);
1855 /* Left. */
1856 if (left_p)
1858 if (width == 1)
1859 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
1861 XClearArea (dpy, window, left_x, top_y, 1, 1, False);
1862 XClearArea (dpy, window, left_x, bottom_y, 1, 1, False);
1864 for (i = (width > 1 ? 1 : 0); i < width; ++i)
1865 XDrawLine (dpy, window, gc,
1866 left_x + i, top_y + (i + 1) * top_p,
1867 left_x + i, bottom_y + 1 - (i + 1) * bot_p);
1870 XSetClipMask (dpy, gc, None);
1871 if (raised_p)
1872 gc = f->output_data.x->black_relief.gc;
1873 else
1874 gc = f->output_data.x->white_relief.gc;
1875 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
1877 if (width > 1)
1879 /* Outermost top line. */
1880 if (top_p)
1881 XDrawLine (dpy, window, gc,
1882 left_x + (left_p ? 1 : 0), top_y,
1883 right_x + (right_p ? 0 : 1), top_y);
1885 /* Outermost left line. */
1886 if (left_p)
1887 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
1890 /* Bottom. */
1891 if (bot_p)
1893 XDrawLine (dpy, window, gc,
1894 left_x + (left_p ? 1 : 0), bottom_y,
1895 right_x + (right_p ? 0 : 1), bottom_y);
1896 for (i = 1; i < width; ++i)
1897 XDrawLine (dpy, window, gc,
1898 left_x + i * left_p, bottom_y - i,
1899 right_x + 1 - i * right_p, bottom_y - i);
1902 /* Right. */
1903 if (right_p)
1905 XClearArea (dpy, window, right_x, top_y, 1, 1, False);
1906 XClearArea (dpy, window, right_x, bottom_y, 1, 1, False);
1907 for (i = 0; i < width; ++i)
1908 XDrawLine (dpy, window, gc,
1909 right_x - i, top_y + (i + 1) * top_p,
1910 right_x - i, bottom_y + 1 - (i + 1) * bot_p);
1913 XSetClipMask (dpy, gc, None);
1917 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
1918 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
1919 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
1920 left side of the rectangle. RIGHT_P non-zero means draw a line
1921 on the right side of the rectangle. CLIP_RECT is the clipping
1922 rectangle to use when drawing. */
1924 static void
1925 x_draw_box_rect (struct glyph_string *s,
1926 int left_x, int top_y, int right_x, int bottom_y, int width,
1927 int left_p, int right_p, XRectangle *clip_rect)
1929 XGCValues xgcv;
1931 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
1932 XSetForeground (s->display, s->gc, s->face->box_color);
1933 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
1935 /* Top. */
1936 XFillRectangle (s->display, s->window, s->gc,
1937 left_x, top_y, right_x - left_x + 1, width);
1939 /* Left. */
1940 if (left_p)
1941 XFillRectangle (s->display, s->window, s->gc,
1942 left_x, top_y, width, bottom_y - top_y + 1);
1944 /* Bottom. */
1945 XFillRectangle (s->display, s->window, s->gc,
1946 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
1948 /* Right. */
1949 if (right_p)
1950 XFillRectangle (s->display, s->window, s->gc,
1951 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
1953 XSetForeground (s->display, s->gc, xgcv.foreground);
1954 XSetClipMask (s->display, s->gc, None);
1958 /* Draw a box around glyph string S. */
1960 static void
1961 x_draw_glyph_string_box (struct glyph_string *s)
1963 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
1964 int left_p, right_p;
1965 struct glyph *last_glyph;
1966 XRectangle clip_rect;
1968 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
1969 ? WINDOW_RIGHT_EDGE_X (s->w)
1970 : window_box_right (s->w, s->area));
1972 /* The glyph that may have a right box line. */
1973 last_glyph = (s->cmp || s->img
1974 ? s->first_glyph
1975 : s->first_glyph + s->nchars - 1);
1977 width = eabs (s->face->box_line_width);
1978 raised_p = s->face->box == FACE_RAISED_BOX;
1979 left_x = s->x;
1980 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
1981 ? last_x - 1
1982 : min (last_x, s->x + s->background_width) - 1);
1983 top_y = s->y;
1984 bottom_y = top_y + s->height - 1;
1986 left_p = (s->first_glyph->left_box_line_p
1987 || (s->hl == DRAW_MOUSE_FACE
1988 && (s->prev == NULL
1989 || s->prev->hl != s->hl)));
1990 right_p = (last_glyph->right_box_line_p
1991 || (s->hl == DRAW_MOUSE_FACE
1992 && (s->next == NULL
1993 || s->next->hl != s->hl)));
1995 get_glyph_string_clip_rect (s, &clip_rect);
1997 if (s->face->box == FACE_SIMPLE_BOX)
1998 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
1999 left_p, right_p, &clip_rect);
2000 else
2002 x_setup_relief_colors (s);
2003 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2004 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2009 /* Draw foreground of image glyph string S. */
2011 static void
2012 x_draw_image_foreground (struct glyph_string *s)
2014 int x = s->x;
2015 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2017 /* If first glyph of S has a left box line, start drawing it to the
2018 right of that line. */
2019 if (s->face->box != FACE_NO_BOX
2020 && s->first_glyph->left_box_line_p
2021 && s->slice.x == 0)
2022 x += eabs (s->face->box_line_width);
2024 /* If there is a margin around the image, adjust x- and y-position
2025 by that margin. */
2026 if (s->slice.x == 0)
2027 x += s->img->hmargin;
2028 if (s->slice.y == 0)
2029 y += s->img->vmargin;
2031 if (s->img->pixmap)
2033 if (s->img->mask)
2035 /* We can't set both a clip mask and use XSetClipRectangles
2036 because the latter also sets a clip mask. We also can't
2037 trust on the shape extension to be available
2038 (XShapeCombineRegion). So, compute the rectangle to draw
2039 manually. */
2040 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2041 | GCFunction);
2042 XGCValues xgcv;
2043 XRectangle clip_rect, image_rect, r;
2045 xgcv.clip_mask = s->img->mask;
2046 xgcv.clip_x_origin = x;
2047 xgcv.clip_y_origin = y;
2048 xgcv.function = GXcopy;
2049 XChangeGC (s->display, s->gc, mask, &xgcv);
2051 get_glyph_string_clip_rect (s, &clip_rect);
2052 image_rect.x = x;
2053 image_rect.y = y;
2054 image_rect.width = s->slice.width;
2055 image_rect.height = s->slice.height;
2056 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2057 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2058 s->slice.x + r.x - x, s->slice.y + r.y - y,
2059 r.width, r.height, r.x, r.y);
2061 else
2063 XRectangle clip_rect, image_rect, r;
2065 get_glyph_string_clip_rect (s, &clip_rect);
2066 image_rect.x = x;
2067 image_rect.y = y;
2068 image_rect.width = s->slice.width;
2069 image_rect.height = s->slice.height;
2070 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2071 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2072 s->slice.x + r.x - x, s->slice.y + r.y - y,
2073 r.width, r.height, r.x, r.y);
2075 /* When the image has a mask, we can expect that at
2076 least part of a mouse highlight or a block cursor will
2077 be visible. If the image doesn't have a mask, make
2078 a block cursor visible by drawing a rectangle around
2079 the image. I believe it's looking better if we do
2080 nothing here for mouse-face. */
2081 if (s->hl == DRAW_CURSOR)
2083 int relief = eabs (s->img->relief);
2084 XDrawRectangle (s->display, s->window, s->gc,
2085 x - relief, y - relief,
2086 s->slice.width + relief*2 - 1,
2087 s->slice.height + relief*2 - 1);
2091 else
2092 /* Draw a rectangle if image could not be loaded. */
2093 XDrawRectangle (s->display, s->window, s->gc, x, y,
2094 s->slice.width - 1, s->slice.height - 1);
2098 /* Draw a relief around the image glyph string S. */
2100 static void
2101 x_draw_image_relief (struct glyph_string *s)
2103 int x1, y1, thick, raised_p, top_p, bot_p, left_p, right_p;
2104 XRectangle r;
2105 int x = s->x;
2106 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2108 /* If first glyph of S has a left box line, start drawing it to the
2109 right of that line. */
2110 if (s->face->box != FACE_NO_BOX
2111 && s->first_glyph->left_box_line_p
2112 && s->slice.x == 0)
2113 x += eabs (s->face->box_line_width);
2115 /* If there is a margin around the image, adjust x- and y-position
2116 by that margin. */
2117 if (s->slice.x == 0)
2118 x += s->img->hmargin;
2119 if (s->slice.y == 0)
2120 y += s->img->vmargin;
2122 if (s->hl == DRAW_IMAGE_SUNKEN
2123 || s->hl == DRAW_IMAGE_RAISED)
2125 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2126 raised_p = s->hl == DRAW_IMAGE_RAISED;
2128 else
2130 thick = eabs (s->img->relief);
2131 raised_p = s->img->relief > 0;
2134 x1 = x + s->slice.width - 1;
2135 y1 = y + s->slice.height - 1;
2136 top_p = bot_p = left_p = right_p = 0;
2138 if (s->slice.x == 0)
2139 x -= thick, left_p = 1;
2140 if (s->slice.y == 0)
2141 y -= thick, top_p = 1;
2142 if (s->slice.x + s->slice.width == s->img->width)
2143 x1 += thick, right_p = 1;
2144 if (s->slice.y + s->slice.height == s->img->height)
2145 y1 += thick, bot_p = 1;
2147 x_setup_relief_colors (s);
2148 get_glyph_string_clip_rect (s, &r);
2149 x_draw_relief_rect (s->f, x, y, x1, y1, thick, raised_p,
2150 top_p, bot_p, left_p, right_p, &r);
2154 /* Draw the foreground of image glyph string S to PIXMAP. */
2156 static void
2157 x_draw_image_foreground_1 (struct glyph_string *s, Pixmap pixmap)
2159 int x = 0;
2160 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2162 /* If first glyph of S has a left box line, start drawing it to the
2163 right of that line. */
2164 if (s->face->box != FACE_NO_BOX
2165 && s->first_glyph->left_box_line_p
2166 && s->slice.x == 0)
2167 x += eabs (s->face->box_line_width);
2169 /* If there is a margin around the image, adjust x- and y-position
2170 by that margin. */
2171 if (s->slice.x == 0)
2172 x += s->img->hmargin;
2173 if (s->slice.y == 0)
2174 y += s->img->vmargin;
2176 if (s->img->pixmap)
2178 if (s->img->mask)
2180 /* We can't set both a clip mask and use XSetClipRectangles
2181 because the latter also sets a clip mask. We also can't
2182 trust on the shape extension to be available
2183 (XShapeCombineRegion). So, compute the rectangle to draw
2184 manually. */
2185 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2186 | GCFunction);
2187 XGCValues xgcv;
2189 xgcv.clip_mask = s->img->mask;
2190 xgcv.clip_x_origin = x - s->slice.x;
2191 xgcv.clip_y_origin = y - s->slice.y;
2192 xgcv.function = GXcopy;
2193 XChangeGC (s->display, s->gc, mask, &xgcv);
2195 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2196 s->slice.x, s->slice.y,
2197 s->slice.width, s->slice.height, x, y);
2198 XSetClipMask (s->display, s->gc, None);
2200 else
2202 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2203 s->slice.x, s->slice.y,
2204 s->slice.width, s->slice.height, x, y);
2206 /* When the image has a mask, we can expect that at
2207 least part of a mouse highlight or a block cursor will
2208 be visible. If the image doesn't have a mask, make
2209 a block cursor visible by drawing a rectangle around
2210 the image. I believe it's looking better if we do
2211 nothing here for mouse-face. */
2212 if (s->hl == DRAW_CURSOR)
2214 int r = eabs (s->img->relief);
2215 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2216 s->slice.width + r*2 - 1,
2217 s->slice.height + r*2 - 1);
2221 else
2222 /* Draw a rectangle if image could not be loaded. */
2223 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2224 s->slice.width - 1, s->slice.height - 1);
2228 /* Draw part of the background of glyph string S. X, Y, W, and H
2229 give the rectangle to draw. */
2231 static void
2232 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
2234 if (s->stippled_p)
2236 /* Fill background with a stipple pattern. */
2237 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2238 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2239 XSetFillStyle (s->display, s->gc, FillSolid);
2241 else
2242 x_clear_glyph_string_rect (s, x, y, w, h);
2246 /* Draw image glyph string S.
2248 s->y
2249 s->x +-------------------------
2250 | s->face->box
2252 | +-------------------------
2253 | | s->img->margin
2255 | | +-------------------
2256 | | | the image
2260 static void
2261 x_draw_image_glyph_string (struct glyph_string *s)
2263 int box_line_hwidth = eabs (s->face->box_line_width);
2264 int box_line_vwidth = max (s->face->box_line_width, 0);
2265 int height;
2266 Pixmap pixmap = None;
2268 height = s->height;
2269 if (s->slice.y == 0)
2270 height -= box_line_vwidth;
2271 if (s->slice.y + s->slice.height >= s->img->height)
2272 height -= box_line_vwidth;
2274 /* Fill background with face under the image. Do it only if row is
2275 taller than image or if image has a clip mask to reduce
2276 flickering. */
2277 s->stippled_p = s->face->stipple != 0;
2278 if (height > s->slice.height
2279 || s->img->hmargin
2280 || s->img->vmargin
2281 || s->img->mask
2282 || s->img->pixmap == 0
2283 || s->width != s->background_width)
2285 if (s->img->mask)
2287 /* Create a pixmap as large as the glyph string. Fill it
2288 with the background color. Copy the image to it, using
2289 its mask. Copy the temporary pixmap to the display. */
2290 Screen *screen = FRAME_X_SCREEN (s->f);
2291 int depth = DefaultDepthOfScreen (screen);
2293 /* Create a pixmap as large as the glyph string. */
2294 pixmap = XCreatePixmap (s->display, s->window,
2295 s->background_width,
2296 s->height, depth);
2298 /* Don't clip in the following because we're working on the
2299 pixmap. */
2300 XSetClipMask (s->display, s->gc, None);
2302 /* Fill the pixmap with the background color/stipple. */
2303 if (s->stippled_p)
2305 /* Fill background with a stipple pattern. */
2306 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2307 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
2308 XFillRectangle (s->display, pixmap, s->gc,
2309 0, 0, s->background_width, s->height);
2310 XSetFillStyle (s->display, s->gc, FillSolid);
2311 XSetTSOrigin (s->display, s->gc, 0, 0);
2313 else
2315 XGCValues xgcv;
2316 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2317 &xgcv);
2318 XSetForeground (s->display, s->gc, xgcv.background);
2319 XFillRectangle (s->display, pixmap, s->gc,
2320 0, 0, s->background_width, s->height);
2321 XSetForeground (s->display, s->gc, xgcv.foreground);
2324 else
2326 int x = s->x;
2327 int y = s->y;
2329 if (s->first_glyph->left_box_line_p
2330 && s->slice.x == 0)
2331 x += box_line_hwidth;
2333 if (s->slice.y == 0)
2334 y += box_line_vwidth;
2336 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2339 s->background_filled_p = 1;
2342 /* Draw the foreground. */
2343 if (pixmap != None)
2345 x_draw_image_foreground_1 (s, pixmap);
2346 x_set_glyph_string_clipping (s);
2347 XCopyArea (s->display, pixmap, s->window, s->gc,
2348 0, 0, s->background_width, s->height, s->x, s->y);
2349 XFreePixmap (s->display, pixmap);
2351 else
2352 x_draw_image_foreground (s);
2354 /* If we must draw a relief around the image, do it. */
2355 if (s->img->relief
2356 || s->hl == DRAW_IMAGE_RAISED
2357 || s->hl == DRAW_IMAGE_SUNKEN)
2358 x_draw_image_relief (s);
2362 /* Draw stretch glyph string S. */
2364 static void
2365 x_draw_stretch_glyph_string (struct glyph_string *s)
2367 eassert (s->first_glyph->type == STRETCH_GLYPH);
2369 if (s->hl == DRAW_CURSOR
2370 && !x_stretch_cursor_p)
2372 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
2373 wide as the stretch glyph. */
2374 int width, background_width = s->background_width;
2375 int x = s->x;
2377 if (!s->row->reversed_p)
2379 int left_x = window_box_left_offset (s->w, TEXT_AREA);
2381 if (x < left_x)
2383 background_width -= left_x - x;
2384 x = left_x;
2387 else
2389 /* In R2L rows, draw the cursor on the right edge of the
2390 stretch glyph. */
2391 int right_x = window_box_right_offset (s->w, TEXT_AREA);
2393 if (x + background_width > right_x)
2394 background_width -= x - right_x;
2395 x += background_width;
2397 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2398 if (s->row->reversed_p)
2399 x -= width;
2401 /* Draw cursor. */
2402 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2404 /* Clear rest using the GC of the original non-cursor face. */
2405 if (width < background_width)
2407 int y = s->y;
2408 int w = background_width - width, h = s->height;
2409 XRectangle r;
2410 GC gc;
2412 if (!s->row->reversed_p)
2413 x += width;
2414 else
2415 x = s->x;
2416 if (s->row->mouse_face_p
2417 && cursor_in_mouse_face_p (s->w))
2419 x_set_mouse_face_gc (s);
2420 gc = s->gc;
2422 else
2423 gc = s->face->gc;
2425 get_glyph_string_clip_rect (s, &r);
2426 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2428 if (s->face->stipple)
2430 /* Fill background with a stipple pattern. */
2431 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2432 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2433 XSetFillStyle (s->display, gc, FillSolid);
2435 else
2437 XGCValues xgcv;
2438 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2439 XSetForeground (s->display, gc, xgcv.background);
2440 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2441 XSetForeground (s->display, gc, xgcv.foreground);
2445 else if (!s->background_filled_p)
2447 int background_width = s->background_width;
2448 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2450 /* Don't draw into left margin, fringe or scrollbar area
2451 except for header line and mode line. */
2452 if (x < left_x && !s->row->mode_line_p)
2454 background_width -= left_x - x;
2455 x = left_x;
2457 if (background_width > 0)
2458 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2461 s->background_filled_p = 1;
2465 Draw a wavy line under S. The wave fills wave_height pixels from y0.
2467 x0 wave_length = 2
2469 y0 * * * * *
2470 |* * * * * * * * *
2471 wave_height = 3 | * * * *
2475 static void
2476 x_draw_underwave (struct glyph_string *s)
2478 int wave_height = 3, wave_length = 2;
2479 int dx, dy, x0, y0, width, x1, y1, x2, y2, odd, xmax;
2480 XRectangle wave_clip, string_clip, final_clip;
2482 dx = wave_length;
2483 dy = wave_height - 1;
2484 x0 = s->x;
2485 y0 = s->ybase - wave_height + 3;
2486 width = s->width;
2487 xmax = x0 + width;
2489 /* Find and set clipping rectangle */
2491 wave_clip.x = x0;
2492 wave_clip.y = y0;
2493 wave_clip.width = width;
2494 wave_clip.height = wave_height;
2495 get_glyph_string_clip_rect (s, &string_clip);
2497 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
2498 return;
2500 XSetClipRectangles (s->display, s->gc, 0, 0, &final_clip, 1, Unsorted);
2502 /* Draw the waves */
2504 x1 = x0 - (x0 % dx);
2505 x2 = x1 + dx;
2506 odd = (x1/dx) % 2;
2507 y1 = y2 = y0;
2509 if (odd)
2510 y1 += dy;
2511 else
2512 y2 += dy;
2514 if (INT_MAX - dx < xmax)
2515 emacs_abort ();
2517 while (x1 <= xmax)
2519 XDrawLine (s->display, s->window, s->gc, x1, y1, x2, y2);
2520 x1 = x2, y1 = y2;
2521 x2 += dx, y2 = y0 + odd*dy;
2522 odd = !odd;
2525 /* Restore previous clipping rectangle(s) */
2526 XSetClipRectangles (s->display, s->gc, 0, 0, s->clip, s->num_clips, Unsorted);
2530 /* Draw glyph string S. */
2532 static void
2533 x_draw_glyph_string (struct glyph_string *s)
2535 bool relief_drawn_p = 0;
2537 /* If S draws into the background of its successors, draw the
2538 background of the successors first so that S can draw into it.
2539 This makes S->next use XDrawString instead of XDrawImageString. */
2540 if (s->next && s->right_overhang && !s->for_overlaps)
2542 int width;
2543 struct glyph_string *next;
2545 for (width = 0, next = s->next;
2546 next && width < s->right_overhang;
2547 width += next->width, next = next->next)
2548 if (next->first_glyph->type != IMAGE_GLYPH)
2550 x_set_glyph_string_gc (next);
2551 x_set_glyph_string_clipping (next);
2552 if (next->first_glyph->type == STRETCH_GLYPH)
2553 x_draw_stretch_glyph_string (next);
2554 else
2555 x_draw_glyph_string_background (next, 1);
2556 next->num_clips = 0;
2560 /* Set up S->gc, set clipping and draw S. */
2561 x_set_glyph_string_gc (s);
2563 /* Draw relief (if any) in advance for char/composition so that the
2564 glyph string can be drawn over it. */
2565 if (!s->for_overlaps
2566 && s->face->box != FACE_NO_BOX
2567 && (s->first_glyph->type == CHAR_GLYPH
2568 || s->first_glyph->type == COMPOSITE_GLYPH))
2571 x_set_glyph_string_clipping (s);
2572 x_draw_glyph_string_background (s, 1);
2573 x_draw_glyph_string_box (s);
2574 x_set_glyph_string_clipping (s);
2575 relief_drawn_p = 1;
2577 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2578 && !s->clip_tail
2579 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2580 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2581 /* We must clip just this glyph. left_overhang part has already
2582 drawn when s->prev was drawn, and right_overhang part will be
2583 drawn later when s->next is drawn. */
2584 x_set_glyph_string_clipping_exactly (s, s);
2585 else
2586 x_set_glyph_string_clipping (s);
2588 switch (s->first_glyph->type)
2590 case IMAGE_GLYPH:
2591 x_draw_image_glyph_string (s);
2592 break;
2594 case STRETCH_GLYPH:
2595 x_draw_stretch_glyph_string (s);
2596 break;
2598 case CHAR_GLYPH:
2599 if (s->for_overlaps)
2600 s->background_filled_p = 1;
2601 else
2602 x_draw_glyph_string_background (s, 0);
2603 x_draw_glyph_string_foreground (s);
2604 break;
2606 case COMPOSITE_GLYPH:
2607 if (s->for_overlaps || (s->cmp_from > 0
2608 && ! s->first_glyph->u.cmp.automatic))
2609 s->background_filled_p = 1;
2610 else
2611 x_draw_glyph_string_background (s, 1);
2612 x_draw_composite_glyph_string_foreground (s);
2613 break;
2615 case GLYPHLESS_GLYPH:
2616 if (s->for_overlaps)
2617 s->background_filled_p = 1;
2618 else
2619 x_draw_glyph_string_background (s, 1);
2620 x_draw_glyphless_glyph_string_foreground (s);
2621 break;
2623 default:
2624 emacs_abort ();
2627 if (!s->for_overlaps)
2629 /* Draw underline. */
2630 if (s->face->underline_p)
2632 if (s->face->underline_type == FACE_UNDER_WAVE)
2634 if (s->face->underline_defaulted_p)
2635 x_draw_underwave (s);
2636 else
2638 XGCValues xgcv;
2639 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2640 XSetForeground (s->display, s->gc, s->face->underline_color);
2641 x_draw_underwave (s);
2642 XSetForeground (s->display, s->gc, xgcv.foreground);
2645 else if (s->face->underline_type == FACE_UNDER_LINE)
2647 unsigned long thickness, position;
2648 int y;
2650 if (s->prev && s->prev->face->underline_p
2651 && s->prev->face->underline_type == FACE_UNDER_LINE)
2653 /* We use the same underline style as the previous one. */
2654 thickness = s->prev->underline_thickness;
2655 position = s->prev->underline_position;
2657 else
2659 /* Get the underline thickness. Default is 1 pixel. */
2660 if (s->font && s->font->underline_thickness > 0)
2661 thickness = s->font->underline_thickness;
2662 else
2663 thickness = 1;
2664 if (x_underline_at_descent_line)
2665 position = (s->height - thickness) - (s->ybase - s->y);
2666 else
2668 /* Get the underline position. This is the recommended
2669 vertical offset in pixels from the baseline to the top of
2670 the underline. This is a signed value according to the
2671 specs, and its default is
2673 ROUND ((maximum descent) / 2), with
2674 ROUND(x) = floor (x + 0.5) */
2676 if (x_use_underline_position_properties
2677 && s->font && s->font->underline_position >= 0)
2678 position = s->font->underline_position;
2679 else if (s->font)
2680 position = (s->font->descent + 1) / 2;
2681 else
2682 position = underline_minimum_offset;
2684 position = max (position, underline_minimum_offset);
2686 /* Check the sanity of thickness and position. We should
2687 avoid drawing underline out of the current line area. */
2688 if (s->y + s->height <= s->ybase + position)
2689 position = (s->height - 1) - (s->ybase - s->y);
2690 if (s->y + s->height < s->ybase + position + thickness)
2691 thickness = (s->y + s->height) - (s->ybase + position);
2692 s->underline_thickness = thickness;
2693 s->underline_position = position;
2694 y = s->ybase + position;
2695 if (s->face->underline_defaulted_p)
2696 XFillRectangle (s->display, s->window, s->gc,
2697 s->x, y, s->width, thickness);
2698 else
2700 XGCValues xgcv;
2701 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2702 XSetForeground (s->display, s->gc, s->face->underline_color);
2703 XFillRectangle (s->display, s->window, s->gc,
2704 s->x, y, s->width, thickness);
2705 XSetForeground (s->display, s->gc, xgcv.foreground);
2709 /* Draw overline. */
2710 if (s->face->overline_p)
2712 unsigned long dy = 0, h = 1;
2714 if (s->face->overline_color_defaulted_p)
2715 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2716 s->width, h);
2717 else
2719 XGCValues xgcv;
2720 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2721 XSetForeground (s->display, s->gc, s->face->overline_color);
2722 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2723 s->width, h);
2724 XSetForeground (s->display, s->gc, xgcv.foreground);
2728 /* Draw strike-through. */
2729 if (s->face->strike_through_p)
2731 unsigned long h = 1;
2732 unsigned long dy = (s->height - h) / 2;
2734 if (s->face->strike_through_color_defaulted_p)
2735 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2736 s->width, h);
2737 else
2739 XGCValues xgcv;
2740 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2741 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2742 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2743 s->width, h);
2744 XSetForeground (s->display, s->gc, xgcv.foreground);
2748 /* Draw relief if not yet drawn. */
2749 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2750 x_draw_glyph_string_box (s);
2752 if (s->prev)
2754 struct glyph_string *prev;
2756 for (prev = s->prev; prev; prev = prev->prev)
2757 if (prev->hl != s->hl
2758 && prev->x + prev->width + prev->right_overhang > s->x)
2760 /* As prev was drawn while clipped to its own area, we
2761 must draw the right_overhang part using s->hl now. */
2762 enum draw_glyphs_face save = prev->hl;
2764 prev->hl = s->hl;
2765 x_set_glyph_string_gc (prev);
2766 x_set_glyph_string_clipping_exactly (s, prev);
2767 if (prev->first_glyph->type == CHAR_GLYPH)
2768 x_draw_glyph_string_foreground (prev);
2769 else
2770 x_draw_composite_glyph_string_foreground (prev);
2771 XSetClipMask (prev->display, prev->gc, None);
2772 prev->hl = save;
2773 prev->num_clips = 0;
2777 if (s->next)
2779 struct glyph_string *next;
2781 for (next = s->next; next; next = next->next)
2782 if (next->hl != s->hl
2783 && next->x - next->left_overhang < s->x + s->width)
2785 /* As next will be drawn while clipped to its own area,
2786 we must draw the left_overhang part using s->hl now. */
2787 enum draw_glyphs_face save = next->hl;
2789 next->hl = s->hl;
2790 x_set_glyph_string_gc (next);
2791 x_set_glyph_string_clipping_exactly (s, next);
2792 if (next->first_glyph->type == CHAR_GLYPH)
2793 x_draw_glyph_string_foreground (next);
2794 else
2795 x_draw_composite_glyph_string_foreground (next);
2796 XSetClipMask (next->display, next->gc, None);
2797 next->hl = save;
2798 next->num_clips = 0;
2799 next->clip_head = s->next;
2804 /* Reset clipping. */
2805 XSetClipMask (s->display, s->gc, None);
2806 s->num_clips = 0;
2809 /* Shift display to make room for inserted glyphs. */
2811 static void
2812 x_shift_glyphs_for_insert (struct frame *f, int x, int y, int width, int height, int shift_by)
2814 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2815 f->output_data.x->normal_gc,
2816 x, y, width, height,
2817 x + shift_by, y);
2820 /* Delete N glyphs at the nominal cursor position. Not implemented
2821 for X frames. */
2823 static void
2824 x_delete_glyphs (struct frame *f, register int n)
2826 emacs_abort ();
2830 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2831 If they are <= 0, this is probably an error. */
2833 void
2834 x_clear_area (Display *dpy, Window window, int x, int y, int width, int height)
2836 eassert (width > 0 && height > 0);
2837 XClearArea (dpy, window, x, y, width, height, False);
2841 /* Clear an entire frame. */
2843 static void
2844 x_clear_frame (struct frame *f)
2846 /* Clearing the frame will erase any cursor, so mark them all as no
2847 longer visible. */
2848 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2850 block_input ();
2852 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2854 /* We have to clear the scroll bars. If we have changed colors or
2855 something like that, then they should be notified. */
2856 x_scroll_bar_clear (f);
2858 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
2859 /* Make sure scroll bars are redrawn. As they aren't redrawn by
2860 redisplay, do it here. */
2861 if (FRAME_GTK_WIDGET (f))
2862 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
2863 #endif
2865 XFlush (FRAME_X_DISPLAY (f));
2867 unblock_input ();
2872 /* Invert the middle quarter of the frame for .15 sec. */
2874 static void
2875 XTflash (struct frame *f)
2877 block_input ();
2880 #ifdef USE_GTK
2881 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
2882 when the scroll bars and the edit widget share the same X window. */
2883 GdkWindow *window = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
2884 #ifdef HAVE_GTK3
2885 cairo_t *cr = gdk_cairo_create (window);
2886 cairo_set_source_rgb (cr, 1, 1, 1);
2887 cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
2888 #define XFillRectangle(d, win, gc, x, y, w, h) \
2889 do { \
2890 cairo_rectangle (cr, x, y, w, h); \
2891 cairo_fill (cr); \
2893 while (0)
2894 #else /* ! HAVE_GTK3 */
2895 GdkGCValues vals;
2896 GdkGC *gc;
2897 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
2898 ^ FRAME_BACKGROUND_PIXEL (f));
2899 vals.function = GDK_XOR;
2900 gc = gdk_gc_new_with_values (window,
2901 &vals, GDK_GC_FUNCTION | GDK_GC_FOREGROUND);
2902 #define XFillRectangle(d, win, gc, x, y, w, h) \
2903 gdk_draw_rectangle (window, gc, TRUE, x, y, w, h)
2904 #endif /* ! HAVE_GTK3 */
2905 #else /* ! USE_GTK */
2906 GC gc;
2908 /* Create a GC that will use the GXxor function to flip foreground
2909 pixels into background pixels. */
2911 XGCValues values;
2913 values.function = GXxor;
2914 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
2915 ^ FRAME_BACKGROUND_PIXEL (f));
2917 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2918 GCFunction | GCForeground, &values);
2920 #endif
2922 /* Get the height not including a menu bar widget. */
2923 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
2924 /* Height of each line to flash. */
2925 int flash_height = FRAME_LINE_HEIGHT (f);
2926 /* These will be the left and right margins of the rectangles. */
2927 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
2928 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
2929 int width = flash_right - flash_left;
2931 /* If window is tall, flash top and bottom line. */
2932 if (height > 3 * FRAME_LINE_HEIGHT (f))
2934 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2935 flash_left,
2936 (FRAME_INTERNAL_BORDER_WIDTH (f)
2937 + FRAME_TOP_MARGIN_HEIGHT (f)),
2938 width, flash_height);
2939 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2940 flash_left,
2941 (height - flash_height
2942 - FRAME_INTERNAL_BORDER_WIDTH (f)),
2943 width, flash_height);
2946 else
2947 /* If it is short, flash it all. */
2948 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2949 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
2950 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
2952 x_flush (f);
2955 struct timespec delay = make_timespec (0, 150 * 1000 * 1000);
2956 struct timespec wakeup = timespec_add (current_timespec (), delay);
2958 /* Keep waiting until past the time wakeup or any input gets
2959 available. */
2960 while (! detect_input_pending ())
2962 struct timespec current = current_timespec ();
2963 struct timespec timeout;
2965 /* Break if result would not be positive. */
2966 if (timespec_cmp (wakeup, current) <= 0)
2967 break;
2969 /* How long `select' should wait. */
2970 timeout = make_timespec (0, 10 * 1000 * 1000);
2972 /* Try to wait that long--but we might wake up sooner. */
2973 pselect (0, NULL, NULL, NULL, &timeout, NULL);
2977 /* If window is tall, flash top and bottom line. */
2978 if (height > 3 * FRAME_LINE_HEIGHT (f))
2980 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2981 flash_left,
2982 (FRAME_INTERNAL_BORDER_WIDTH (f)
2983 + FRAME_TOP_MARGIN_HEIGHT (f)),
2984 width, flash_height);
2985 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2986 flash_left,
2987 (height - flash_height
2988 - FRAME_INTERNAL_BORDER_WIDTH (f)),
2989 width, flash_height);
2991 else
2992 /* If it is short, flash it all. */
2993 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2994 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
2995 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
2997 #ifdef USE_GTK
2998 #ifdef HAVE_GTK3
2999 cairo_destroy (cr);
3000 #else
3001 g_object_unref (G_OBJECT (gc));
3002 #endif
3003 #undef XFillRectangle
3004 #else
3005 XFreeGC (FRAME_X_DISPLAY (f), gc);
3006 #endif
3007 x_flush (f);
3011 unblock_input ();
3015 static void
3016 XTtoggle_invisible_pointer (struct frame *f, int invisible)
3018 block_input ();
3019 if (invisible)
3021 if (FRAME_DISPLAY_INFO (f)->invisible_cursor != 0)
3022 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3023 FRAME_DISPLAY_INFO (f)->invisible_cursor);
3025 else
3026 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3027 f->output_data.x->current_cursor);
3028 f->pointer_invisible = invisible;
3029 unblock_input ();
3033 /* Make audible bell. */
3035 static void
3036 XTring_bell (struct frame *f)
3038 if (FRAME_X_DISPLAY (f))
3040 if (visible_bell)
3041 XTflash (f);
3042 else
3044 block_input ();
3045 #ifdef HAVE_XKB
3046 XkbBell (FRAME_X_DISPLAY (f), None, 0, None);
3047 #else
3048 XBell (FRAME_X_DISPLAY (f), 0);
3049 #endif
3050 XFlush (FRAME_X_DISPLAY (f));
3051 unblock_input ();
3056 /***********************************************************************
3057 Line Dance
3058 ***********************************************************************/
3060 /* Perform an insert-lines or delete-lines operation, inserting N
3061 lines or deleting -N lines at vertical position VPOS. */
3063 static void
3064 x_ins_del_lines (struct frame *f, int vpos, int n)
3066 emacs_abort ();
3070 /* Scroll part of the display as described by RUN. */
3072 static void
3073 x_scroll_run (struct window *w, struct run *run)
3075 struct frame *f = XFRAME (w->frame);
3076 int x, y, width, height, from_y, to_y, bottom_y;
3078 /* Get frame-relative bounding box of the text display area of W,
3079 without mode lines. Include in this box the left and right
3080 fringe of W. */
3081 window_box (w, ANY_AREA, &x, &y, &width, &height);
3083 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3084 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3085 bottom_y = y + height;
3087 if (to_y < from_y)
3089 /* Scrolling up. Make sure we don't copy part of the mode
3090 line at the bottom. */
3091 if (from_y + run->height > bottom_y)
3092 height = bottom_y - from_y;
3093 else
3094 height = run->height;
3096 else
3098 /* Scrolling down. Make sure we don't copy over the mode line.
3099 at the bottom. */
3100 if (to_y + run->height > bottom_y)
3101 height = bottom_y - to_y;
3102 else
3103 height = run->height;
3106 block_input ();
3108 /* Cursor off. Will be switched on again in x_update_window_end. */
3109 x_clear_cursor (w);
3111 XCopyArea (FRAME_X_DISPLAY (f),
3112 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3113 f->output_data.x->normal_gc,
3114 x, from_y,
3115 width, height,
3116 x, to_y);
3118 unblock_input ();
3123 /***********************************************************************
3124 Exposure Events
3125 ***********************************************************************/
3128 static void
3129 frame_highlight (struct frame *f)
3131 /* We used to only do this if Vx_no_window_manager was non-nil, but
3132 the ICCCM (section 4.1.6) says that the window's border pixmap
3133 and border pixel are window attributes which are "private to the
3134 client", so we can always change it to whatever we want. */
3135 block_input ();
3136 /* I recently started to get errors in this XSetWindowBorder, depending on
3137 the window-manager in use, tho something more is at play since I've been
3138 using that same window-manager binary for ever. Let's not crash just
3139 because of this (bug#9310). */
3140 x_catch_errors (FRAME_X_DISPLAY (f));
3141 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3142 f->output_data.x->border_pixel);
3143 x_uncatch_errors ();
3144 unblock_input ();
3145 x_update_cursor (f, 1);
3146 x_set_frame_alpha (f);
3149 static void
3150 frame_unhighlight (struct frame *f)
3152 /* We used to only do this if Vx_no_window_manager was non-nil, but
3153 the ICCCM (section 4.1.6) says that the window's border pixmap
3154 and border pixel are window attributes which are "private to the
3155 client", so we can always change it to whatever we want. */
3156 block_input ();
3157 /* Same as above for XSetWindowBorder (bug#9310). */
3158 x_catch_errors (FRAME_X_DISPLAY (f));
3159 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3160 f->output_data.x->border_tile);
3161 x_uncatch_errors ();
3162 unblock_input ();
3163 x_update_cursor (f, 1);
3164 x_set_frame_alpha (f);
3167 /* The focus has changed. Update the frames as necessary to reflect
3168 the new situation. Note that we can't change the selected frame
3169 here, because the Lisp code we are interrupting might become confused.
3170 Each event gets marked with the frame in which it occurred, so the
3171 Lisp code can tell when the switch took place by examining the events. */
3173 static void
3174 x_new_focus_frame (struct x_display_info *dpyinfo, struct frame *frame)
3176 struct frame *old_focus = dpyinfo->x_focus_frame;
3178 if (frame != dpyinfo->x_focus_frame)
3180 /* Set this before calling other routines, so that they see
3181 the correct value of x_focus_frame. */
3182 dpyinfo->x_focus_frame = frame;
3184 if (old_focus && old_focus->auto_lower)
3185 x_lower_frame (old_focus);
3187 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3188 dpyinfo->x_pending_autoraise_frame = dpyinfo->x_focus_frame;
3189 else
3190 dpyinfo->x_pending_autoraise_frame = NULL;
3193 x_frame_rehighlight (dpyinfo);
3196 /* Handle FocusIn and FocusOut state changes for FRAME.
3197 If FRAME has focus and there exists more than one frame, puts
3198 a FOCUS_IN_EVENT into *BUFP. */
3200 static void
3201 x_focus_changed (int type, int state, struct x_display_info *dpyinfo, struct frame *frame, struct input_event *bufp)
3203 if (type == FocusIn)
3205 if (dpyinfo->x_focus_event_frame != frame)
3207 x_new_focus_frame (dpyinfo, frame);
3208 dpyinfo->x_focus_event_frame = frame;
3210 /* Don't stop displaying the initial startup message
3211 for a switch-frame event we don't need. */
3212 /* When run as a daemon, Vterminal_frame is always NIL. */
3213 bufp->arg = (((NILP (Vterminal_frame) || EQ (Fdaemonp (), Qt))
3214 && CONSP (Vframe_list)
3215 && !NILP (XCDR (Vframe_list)))
3216 ? Qt : Qnil);
3217 bufp->kind = FOCUS_IN_EVENT;
3218 XSETFRAME (bufp->frame_or_window, frame);
3221 frame->output_data.x->focus_state |= state;
3223 #ifdef HAVE_X_I18N
3224 if (FRAME_XIC (frame))
3225 XSetICFocus (FRAME_XIC (frame));
3226 #endif
3228 else if (type == FocusOut)
3230 frame->output_data.x->focus_state &= ~state;
3232 if (dpyinfo->x_focus_event_frame == frame)
3234 dpyinfo->x_focus_event_frame = 0;
3235 x_new_focus_frame (dpyinfo, 0);
3237 bufp->kind = FOCUS_OUT_EVENT;
3238 XSETFRAME (bufp->frame_or_window, frame);
3241 #ifdef HAVE_X_I18N
3242 if (FRAME_XIC (frame))
3243 XUnsetICFocus (FRAME_XIC (frame));
3244 #endif
3245 if (frame->pointer_invisible)
3246 XTtoggle_invisible_pointer (frame, 0);
3250 /* Return the Emacs frame-object corresponding to an X window.
3251 It could be the frame's main window or an icon window. */
3253 static struct frame *
3254 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3256 Lisp_Object tail, frame;
3257 struct frame *f;
3259 if (wdesc == None)
3260 return NULL;
3262 FOR_EACH_FRAME (tail, frame)
3264 f = XFRAME (frame);
3265 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3266 continue;
3267 if (f->output_data.x->hourglass_window == wdesc)
3268 return f;
3269 #ifdef USE_X_TOOLKIT
3270 if ((f->output_data.x->edit_widget
3271 && XtWindow (f->output_data.x->edit_widget) == wdesc)
3272 /* A tooltip frame? */
3273 || (!f->output_data.x->edit_widget
3274 && FRAME_X_WINDOW (f) == wdesc)
3275 || f->output_data.x->icon_desc == wdesc)
3276 return f;
3277 #else /* not USE_X_TOOLKIT */
3278 #ifdef USE_GTK
3279 if (f->output_data.x->edit_widget)
3281 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3282 struct x_output *x = f->output_data.x;
3283 if (gwdesc != 0 && gwdesc == x->edit_widget)
3284 return f;
3286 #endif /* USE_GTK */
3287 if (FRAME_X_WINDOW (f) == wdesc
3288 || f->output_data.x->icon_desc == wdesc)
3289 return f;
3290 #endif /* not USE_X_TOOLKIT */
3292 return 0;
3295 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3297 /* Like x_window_to_frame but also compares the window with the widget's
3298 windows. */
3300 static struct frame *
3301 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3303 Lisp_Object tail, frame;
3304 struct frame *f, *found = NULL;
3305 struct x_output *x;
3307 if (wdesc == None)
3308 return NULL;
3310 FOR_EACH_FRAME (tail, frame)
3312 if (found)
3313 break;
3314 f = XFRAME (frame);
3315 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
3317 /* This frame matches if the window is any of its widgets. */
3318 x = f->output_data.x;
3319 if (x->hourglass_window == wdesc)
3320 found = f;
3321 else if (x->widget)
3323 #ifdef USE_GTK
3324 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3325 if (gwdesc != 0
3326 && gtk_widget_get_toplevel (gwdesc) == x->widget)
3327 found = f;
3328 #else
3329 if (wdesc == XtWindow (x->widget)
3330 || wdesc == XtWindow (x->column_widget)
3331 || wdesc == XtWindow (x->edit_widget))
3332 found = f;
3333 /* Match if the window is this frame's menubar. */
3334 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
3335 found = f;
3336 #endif
3338 else if (FRAME_X_WINDOW (f) == wdesc)
3339 /* A tooltip frame. */
3340 found = f;
3344 return found;
3347 /* Likewise, but consider only the menu bar widget. */
3349 static struct frame *
3350 x_menubar_window_to_frame (struct x_display_info *dpyinfo,
3351 const XEvent *event)
3353 Window wdesc = event->xany.window;
3354 Lisp_Object tail, frame;
3355 struct frame *f;
3356 struct x_output *x;
3358 if (wdesc == None)
3359 return NULL;
3361 FOR_EACH_FRAME (tail, frame)
3363 f = XFRAME (frame);
3364 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3365 continue;
3366 x = f->output_data.x;
3367 #ifdef USE_GTK
3368 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
3369 return f;
3370 #else
3371 /* Match if the window is this frame's menubar. */
3372 if (x->menubar_widget
3373 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
3374 return f;
3375 #endif
3377 return 0;
3380 /* Return the frame whose principal (outermost) window is WDESC.
3381 If WDESC is some other (smaller) window, we return 0. */
3383 struct frame *
3384 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3386 Lisp_Object tail, frame;
3387 struct frame *f;
3388 struct x_output *x;
3390 if (wdesc == None)
3391 return NULL;
3393 FOR_EACH_FRAME (tail, frame)
3395 f = XFRAME (frame);
3396 if (!FRAME_X_P (f) || FRAME_DISPLAY_INFO (f) != dpyinfo)
3397 continue;
3398 x = f->output_data.x;
3400 if (x->widget)
3402 /* This frame matches if the window is its topmost widget. */
3403 #ifdef USE_GTK
3404 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3405 if (gwdesc == x->widget)
3406 return f;
3407 #else
3408 if (wdesc == XtWindow (x->widget))
3409 return f;
3410 #endif
3412 else if (FRAME_X_WINDOW (f) == wdesc)
3413 /* Tooltip frame. */
3414 return f;
3416 return 0;
3419 #else /* !USE_X_TOOLKIT && !USE_GTK */
3421 #define x_any_window_to_frame(d, i) x_window_to_frame (d, i)
3422 #define x_top_window_to_frame(d, i) x_window_to_frame (d, i)
3424 #endif /* USE_X_TOOLKIT || USE_GTK */
3426 /* The focus may have changed. Figure out if it is a real focus change,
3427 by checking both FocusIn/Out and Enter/LeaveNotify events.
3429 Returns FOCUS_IN_EVENT event in *BUFP. */
3431 static void
3432 x_detect_focus_change (struct x_display_info *dpyinfo, struct frame *frame,
3433 const XEvent *event, struct input_event *bufp)
3435 if (!frame)
3436 return;
3438 switch (event->type)
3440 case EnterNotify:
3441 case LeaveNotify:
3443 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3444 int focus_state
3445 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3447 if (event->xcrossing.detail != NotifyInferior
3448 && event->xcrossing.focus
3449 && ! (focus_state & FOCUS_EXPLICIT))
3450 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3451 FOCUS_IMPLICIT,
3452 dpyinfo, frame, bufp);
3454 break;
3456 case FocusIn:
3457 case FocusOut:
3458 x_focus_changed (event->type,
3459 (event->xfocus.detail == NotifyPointer ?
3460 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3461 dpyinfo, frame, bufp);
3462 break;
3464 case ClientMessage:
3465 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
3467 enum xembed_message msg = event->xclient.data.l[1];
3468 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
3469 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
3471 break;
3476 #if !defined USE_X_TOOLKIT && !defined USE_GTK
3477 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3479 void
3480 x_mouse_leave (struct x_display_info *dpyinfo)
3482 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3484 #endif
3486 /* The focus has changed, or we have redirected a frame's focus to
3487 another frame (this happens when a frame uses a surrogate
3488 mini-buffer frame). Shift the highlight as appropriate.
3490 The FRAME argument doesn't necessarily have anything to do with which
3491 frame is being highlighted or un-highlighted; we only use it to find
3492 the appropriate X display info. */
3494 static void
3495 XTframe_rehighlight (struct frame *frame)
3497 x_frame_rehighlight (FRAME_DISPLAY_INFO (frame));
3500 static void
3501 x_frame_rehighlight (struct x_display_info *dpyinfo)
3503 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3505 if (dpyinfo->x_focus_frame)
3507 dpyinfo->x_highlight_frame
3508 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3509 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3510 : dpyinfo->x_focus_frame);
3511 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3513 fset_focus_frame (dpyinfo->x_focus_frame, Qnil);
3514 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3517 else
3518 dpyinfo->x_highlight_frame = 0;
3520 if (dpyinfo->x_highlight_frame != old_highlight)
3522 if (old_highlight)
3523 frame_unhighlight (old_highlight);
3524 if (dpyinfo->x_highlight_frame)
3525 frame_highlight (dpyinfo->x_highlight_frame);
3531 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3533 /* Initialize mode_switch_bit and modifier_meaning. */
3534 static void
3535 x_find_modifier_meanings (struct x_display_info *dpyinfo)
3537 int min_code, max_code;
3538 KeySym *syms;
3539 int syms_per_code;
3540 XModifierKeymap *mods;
3542 dpyinfo->meta_mod_mask = 0;
3543 dpyinfo->shift_lock_mask = 0;
3544 dpyinfo->alt_mod_mask = 0;
3545 dpyinfo->super_mod_mask = 0;
3546 dpyinfo->hyper_mod_mask = 0;
3548 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3550 syms = XGetKeyboardMapping (dpyinfo->display,
3551 min_code, max_code - min_code + 1,
3552 &syms_per_code);
3553 mods = XGetModifierMapping (dpyinfo->display);
3555 /* Scan the modifier table to see which modifier bits the Meta and
3556 Alt keysyms are on. */
3558 int row, col; /* The row and column in the modifier table. */
3559 int found_alt_or_meta;
3561 for (row = 3; row < 8; row++)
3563 found_alt_or_meta = 0;
3564 for (col = 0; col < mods->max_keypermod; col++)
3566 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3568 /* Zeroes are used for filler. Skip them. */
3569 if (code == 0)
3570 continue;
3572 /* Are any of this keycode's keysyms a meta key? */
3574 int code_col;
3576 for (code_col = 0; code_col < syms_per_code; code_col++)
3578 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3580 switch (sym)
3582 case XK_Meta_L:
3583 case XK_Meta_R:
3584 found_alt_or_meta = 1;
3585 dpyinfo->meta_mod_mask |= (1 << row);
3586 break;
3588 case XK_Alt_L:
3589 case XK_Alt_R:
3590 found_alt_or_meta = 1;
3591 dpyinfo->alt_mod_mask |= (1 << row);
3592 break;
3594 case XK_Hyper_L:
3595 case XK_Hyper_R:
3596 if (!found_alt_or_meta)
3597 dpyinfo->hyper_mod_mask |= (1 << row);
3598 code_col = syms_per_code;
3599 col = mods->max_keypermod;
3600 break;
3602 case XK_Super_L:
3603 case XK_Super_R:
3604 if (!found_alt_or_meta)
3605 dpyinfo->super_mod_mask |= (1 << row);
3606 code_col = syms_per_code;
3607 col = mods->max_keypermod;
3608 break;
3610 case XK_Shift_Lock:
3611 /* Ignore this if it's not on the lock modifier. */
3612 if (!found_alt_or_meta && ((1 << row) == LockMask))
3613 dpyinfo->shift_lock_mask = LockMask;
3614 code_col = syms_per_code;
3615 col = mods->max_keypermod;
3616 break;
3624 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3625 if (! dpyinfo->meta_mod_mask)
3627 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3628 dpyinfo->alt_mod_mask = 0;
3631 /* If some keys are both alt and meta,
3632 make them just meta, not alt. */
3633 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3635 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3638 XFree (syms);
3639 XFreeModifiermap (mods);
3642 /* Convert between the modifier bits X uses and the modifier bits
3643 Emacs uses. */
3646 x_x_to_emacs_modifiers (struct x_display_info *dpyinfo, int state)
3648 int mod_meta = meta_modifier;
3649 int mod_alt = alt_modifier;
3650 int mod_hyper = hyper_modifier;
3651 int mod_super = super_modifier;
3652 Lisp_Object tem;
3654 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3655 if (INTEGERP (tem)) mod_alt = XINT (tem) & INT_MAX;
3656 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3657 if (INTEGERP (tem)) mod_meta = XINT (tem) & INT_MAX;
3658 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3659 if (INTEGERP (tem)) mod_hyper = XINT (tem) & INT_MAX;
3660 tem = Fget (Vx_super_keysym, Qmodifier_value);
3661 if (INTEGERP (tem)) mod_super = XINT (tem) & INT_MAX;
3663 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3664 | ((state & ControlMask) ? ctrl_modifier : 0)
3665 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3666 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3667 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3668 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3671 static int
3672 x_emacs_to_x_modifiers (struct x_display_info *dpyinfo, EMACS_INT state)
3674 EMACS_INT mod_meta = meta_modifier;
3675 EMACS_INT mod_alt = alt_modifier;
3676 EMACS_INT mod_hyper = hyper_modifier;
3677 EMACS_INT mod_super = super_modifier;
3679 Lisp_Object tem;
3681 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3682 if (INTEGERP (tem)) mod_alt = XINT (tem);
3683 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3684 if (INTEGERP (tem)) mod_meta = XINT (tem);
3685 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3686 if (INTEGERP (tem)) mod_hyper = XINT (tem);
3687 tem = Fget (Vx_super_keysym, Qmodifier_value);
3688 if (INTEGERP (tem)) mod_super = XINT (tem);
3691 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3692 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3693 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3694 | ((state & shift_modifier) ? ShiftMask : 0)
3695 | ((state & ctrl_modifier) ? ControlMask : 0)
3696 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3699 /* Convert a keysym to its name. */
3701 char *
3702 x_get_keysym_name (int keysym)
3704 char *value;
3706 block_input ();
3707 value = XKeysymToString (keysym);
3708 unblock_input ();
3710 return value;
3713 /* Mouse clicks and mouse movement. Rah.
3715 Formerly, we used PointerMotionHintMask (in standard_event_mask)
3716 so that we would have to call XQueryPointer after each MotionNotify
3717 event to ask for another such event. However, this made mouse tracking
3718 slow, and there was a bug that made it eventually stop.
3720 Simply asking for MotionNotify all the time seems to work better.
3722 In order to avoid asking for motion events and then throwing most
3723 of them away or busy-polling the server for mouse positions, we ask
3724 the server for pointer motion hints. This means that we get only
3725 one event per group of mouse movements. "Groups" are delimited by
3726 other kinds of events (focus changes and button clicks, for
3727 example), or by XQueryPointer calls; when one of these happens, we
3728 get another MotionNotify event the next time the mouse moves. This
3729 is at least as efficient as getting motion events when mouse
3730 tracking is on, and I suspect only negligibly worse when tracking
3731 is off. */
3733 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3735 If the event is a button press, then note that we have grabbed
3736 the mouse. */
3738 static Lisp_Object
3739 construct_mouse_click (struct input_event *result,
3740 const XButtonEvent *event,
3741 struct frame *f)
3743 /* Make the event type NO_EVENT; we'll change that when we decide
3744 otherwise. */
3745 result->kind = MOUSE_CLICK_EVENT;
3746 result->code = event->button - Button1;
3747 result->timestamp = event->time;
3748 result->modifiers = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f),
3749 event->state)
3750 | (event->type == ButtonRelease
3751 ? up_modifier
3752 : down_modifier));
3754 XSETINT (result->x, event->x);
3755 XSETINT (result->y, event->y);
3756 XSETFRAME (result->frame_or_window, f);
3757 result->arg = Qnil;
3758 return Qnil;
3761 /* Function to report a mouse movement to the mainstream Emacs code.
3762 The input handler calls this.
3764 We have received a mouse movement event, which is given in *event.
3765 If the mouse is over a different glyph than it was last time, tell
3766 the mainstream emacs code by setting mouse_moved. If not, ask for
3767 another motion event, so we can check again the next time it moves. */
3769 static int
3770 note_mouse_movement (struct frame *frame, const XMotionEvent *event)
3772 XRectangle *r;
3773 struct x_display_info *dpyinfo;
3775 if (!FRAME_X_OUTPUT (frame))
3776 return 0;
3778 dpyinfo = FRAME_DISPLAY_INFO (frame);
3779 dpyinfo->last_mouse_movement_time = event->time;
3780 dpyinfo->last_mouse_motion_frame = frame;
3781 dpyinfo->last_mouse_motion_x = event->x;
3782 dpyinfo->last_mouse_motion_y = event->y;
3784 if (event->window != FRAME_X_WINDOW (frame))
3786 frame->mouse_moved = 1;
3787 dpyinfo->last_mouse_scroll_bar = NULL;
3788 note_mouse_highlight (frame, -1, -1);
3789 dpyinfo->last_mouse_glyph_frame = NULL;
3790 return 1;
3794 /* Has the mouse moved off the glyph it was on at the last sighting? */
3795 r = &dpyinfo->last_mouse_glyph;
3796 if (frame != dpyinfo->last_mouse_glyph_frame
3797 || event->x < r->x || event->x >= r->x + r->width
3798 || event->y < r->y || event->y >= r->y + r->height)
3800 frame->mouse_moved = 1;
3801 dpyinfo->last_mouse_scroll_bar = NULL;
3802 note_mouse_highlight (frame, event->x, event->y);
3803 /* Remember which glyph we're now on. */
3804 remember_mouse_glyph (frame, event->x, event->y, r);
3805 dpyinfo->last_mouse_glyph_frame = frame;
3806 return 1;
3809 return 0;
3812 /* Return the current position of the mouse.
3813 *FP should be a frame which indicates which display to ask about.
3815 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3816 and *PART to the frame, window, and scroll bar part that the mouse
3817 is over. Set *X and *Y to the portion and whole of the mouse's
3818 position on the scroll bar.
3820 If the mouse movement started elsewhere, set *FP to the frame the
3821 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3822 the mouse is over.
3824 Set *TIMESTAMP to the server time-stamp for the time at which the mouse
3825 was at this position.
3827 Don't store anything if we don't have a valid set of values to report.
3829 This clears the mouse_moved flag, so we can wait for the next mouse
3830 movement. */
3832 static void
3833 XTmouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
3834 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
3835 Time *timestamp)
3837 struct frame *f1;
3838 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
3840 block_input ();
3842 if (dpyinfo->last_mouse_scroll_bar && insist == 0)
3843 x_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
3844 else
3846 Window root;
3847 int root_x, root_y;
3849 Window dummy_window;
3850 int dummy;
3852 Lisp_Object frame, tail;
3854 /* Clear the mouse-moved flag for every frame on this display. */
3855 FOR_EACH_FRAME (tail, frame)
3856 if (FRAME_X_P (XFRAME (frame))
3857 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3858 XFRAME (frame)->mouse_moved = 0;
3860 dpyinfo->last_mouse_scroll_bar = NULL;
3862 /* Figure out which root window we're on. */
3863 XQueryPointer (FRAME_X_DISPLAY (*fp),
3864 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3866 /* The root window which contains the pointer. */
3867 &root,
3869 /* Trash which we can't trust if the pointer is on
3870 a different screen. */
3871 &dummy_window,
3873 /* The position on that root window. */
3874 &root_x, &root_y,
3876 /* More trash we can't trust. */
3877 &dummy, &dummy,
3879 /* Modifier keys and pointer buttons, about which
3880 we don't care. */
3881 (unsigned int *) &dummy);
3883 /* Now we have a position on the root; find the innermost window
3884 containing the pointer. */
3886 Window win, child;
3887 int win_x, win_y;
3888 int parent_x = 0, parent_y = 0;
3890 win = root;
3892 /* XTranslateCoordinates can get errors if the window
3893 structure is changing at the same time this function
3894 is running. So at least we must not crash from them. */
3896 x_catch_errors (FRAME_X_DISPLAY (*fp));
3898 if (x_mouse_grabbed (dpyinfo))
3900 /* If mouse was grabbed on a frame, give coords for that frame
3901 even if the mouse is now outside it. */
3902 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3904 /* From-window. */
3905 root,
3907 /* To-window. */
3908 FRAME_X_WINDOW (dpyinfo->last_mouse_frame),
3910 /* From-position, to-position. */
3911 root_x, root_y, &win_x, &win_y,
3913 /* Child of win. */
3914 &child);
3915 f1 = dpyinfo->last_mouse_frame;
3917 else
3919 while (1)
3921 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3923 /* From-window, to-window. */
3924 root, win,
3926 /* From-position, to-position. */
3927 root_x, root_y, &win_x, &win_y,
3929 /* Child of win. */
3930 &child);
3932 if (child == None || child == win)
3933 break;
3934 #ifdef USE_GTK
3935 /* We don't wan't to know the innermost window. We
3936 want the edit window. For non-Gtk+ the innermost
3937 window is the edit window. For Gtk+ it might not
3938 be. It might be the tool bar for example. */
3939 if (x_window_to_frame (dpyinfo, win))
3940 break;
3941 #endif
3942 win = child;
3943 parent_x = win_x;
3944 parent_y = win_y;
3947 /* Now we know that:
3948 win is the innermost window containing the pointer
3949 (XTC says it has no child containing the pointer),
3950 win_x and win_y are the pointer's position in it
3951 (XTC did this the last time through), and
3952 parent_x and parent_y are the pointer's position in win's parent.
3953 (They are what win_x and win_y were when win was child.
3954 If win is the root window, it has no parent, and
3955 parent_{x,y} are invalid, but that's okay, because we'll
3956 never use them in that case.) */
3958 #ifdef USE_GTK
3959 /* We don't wan't to know the innermost window. We
3960 want the edit window. */
3961 f1 = x_window_to_frame (dpyinfo, win);
3962 #else
3963 /* Is win one of our frames? */
3964 f1 = x_any_window_to_frame (dpyinfo, win);
3965 #endif
3967 #ifdef USE_X_TOOLKIT
3968 /* If we end up with the menu bar window, say it's not
3969 on the frame. */
3970 if (f1 != NULL
3971 && f1->output_data.x->menubar_widget
3972 && win == XtWindow (f1->output_data.x->menubar_widget))
3973 f1 = NULL;
3974 #endif /* USE_X_TOOLKIT */
3977 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3978 f1 = 0;
3980 x_uncatch_errors ();
3982 /* If not, is it one of our scroll bars? */
3983 if (! f1)
3985 struct scroll_bar *bar;
3987 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
3989 if (bar)
3991 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3992 win_x = parent_x;
3993 win_y = parent_y;
3997 if (f1 == 0 && insist > 0)
3998 f1 = SELECTED_FRAME ();
4000 if (f1)
4002 /* Ok, we found a frame. Store all the values.
4003 last_mouse_glyph is a rectangle used to reduce the
4004 generation of mouse events. To not miss any motion
4005 events, we must divide the frame into rectangles of the
4006 size of the smallest character that could be displayed
4007 on it, i.e. into the same rectangles that matrices on
4008 the frame are divided into. */
4010 /* FIXME: what if F1 is not an X frame? */
4011 dpyinfo = FRAME_DISPLAY_INFO (f1);
4012 remember_mouse_glyph (f1, win_x, win_y, &dpyinfo->last_mouse_glyph);
4013 dpyinfo->last_mouse_glyph_frame = f1;
4015 *bar_window = Qnil;
4016 *part = 0;
4017 *fp = f1;
4018 XSETINT (*x, win_x);
4019 XSETINT (*y, win_y);
4020 *timestamp = dpyinfo->last_mouse_movement_time;
4025 unblock_input ();
4030 /***********************************************************************
4031 Scroll bars
4032 ***********************************************************************/
4034 /* Scroll bar support. */
4036 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
4037 manages it.
4038 This can be called in GC, so we have to make sure to strip off mark
4039 bits. */
4041 static struct scroll_bar *
4042 x_window_to_scroll_bar (Display *display, Window window_id)
4044 Lisp_Object tail, frame;
4046 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4047 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4048 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4050 FOR_EACH_FRAME (tail, frame)
4052 Lisp_Object bar, condemned;
4054 if (! FRAME_X_P (XFRAME (frame)))
4055 continue;
4057 /* Scan this frame's scroll bar list for a scroll bar with the
4058 right window ID. */
4059 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4060 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4061 /* This trick allows us to search both the ordinary and
4062 condemned scroll bar lists with one loop. */
4063 ! NILP (bar) || (bar = condemned,
4064 condemned = Qnil,
4065 ! NILP (bar));
4066 bar = XSCROLL_BAR (bar)->next)
4067 if (XSCROLL_BAR (bar)->x_window == window_id &&
4068 FRAME_X_DISPLAY (XFRAME (frame)) == display)
4069 return XSCROLL_BAR (bar);
4072 return NULL;
4076 #if defined USE_LUCID
4078 /* Return the Lucid menu bar WINDOW is part of. Return null
4079 if WINDOW is not part of a menu bar. */
4081 static Widget
4082 x_window_to_menu_bar (Window window)
4084 Lisp_Object tail, frame;
4086 FOR_EACH_FRAME (tail, frame)
4087 if (FRAME_X_P (XFRAME (frame)))
4089 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4091 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4092 return menu_bar;
4094 return NULL;
4097 #endif /* USE_LUCID */
4100 /************************************************************************
4101 Toolkit scroll bars
4102 ************************************************************************/
4104 #ifdef USE_TOOLKIT_SCROLL_BARS
4106 static void x_send_scroll_bar_event (Lisp_Object, int, int, int);
4108 /* Lisp window being scrolled. Set when starting to interact with
4109 a toolkit scroll bar, reset to nil when ending the interaction. */
4111 static Lisp_Object window_being_scrolled;
4113 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4114 that movements of 1/20 of the screen size are mapped to up/down. */
4116 #ifndef USE_GTK
4117 /* Id of action hook installed for scroll bars. */
4119 static XtActionHookId action_hook_id;
4121 static Boolean xaw3d_arrow_scroll;
4123 /* Whether the drag scrolling maintains the mouse at the top of the
4124 thumb. If not, resizing the thumb needs to be done more carefully
4125 to avoid jerkiness. */
4127 static Boolean xaw3d_pick_top;
4129 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4130 bars are used.. The hook is responsible for detecting when
4131 the user ends an interaction with the scroll bar, and generates
4132 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4134 static void
4135 xt_action_hook (Widget widget, XtPointer client_data, String action_name,
4136 XEvent *event, String *params, Cardinal *num_params)
4138 int scroll_bar_p;
4139 const char *end_action;
4141 #ifdef USE_MOTIF
4142 scroll_bar_p = XmIsScrollBar (widget);
4143 end_action = "Release";
4144 #else /* !USE_MOTIF i.e. use Xaw */
4145 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4146 end_action = "EndScroll";
4147 #endif /* USE_MOTIF */
4149 if (scroll_bar_p
4150 && strcmp (action_name, end_action) == 0
4151 && WINDOWP (window_being_scrolled))
4153 struct window *w;
4154 struct scroll_bar *bar;
4156 x_send_scroll_bar_event (window_being_scrolled,
4157 scroll_bar_end_scroll, 0, 0);
4158 w = XWINDOW (window_being_scrolled);
4159 bar = XSCROLL_BAR (w->vertical_scroll_bar);
4161 if (bar->dragging != -1)
4163 bar->dragging = -1;
4164 /* The thumb size is incorrect while dragging: fix it. */
4165 set_vertical_scroll_bar (w);
4167 window_being_scrolled = Qnil;
4168 #if defined (USE_LUCID)
4169 bar->last_seen_part = scroll_bar_nowhere;
4170 #endif
4171 /* Xt timeouts no longer needed. */
4172 toolkit_scroll_bar_interaction = 0;
4175 #endif /* not USE_GTK */
4177 /* Send a client message with message type Xatom_Scrollbar for a
4178 scroll action to the frame of WINDOW. PART is a value identifying
4179 the part of the scroll bar that was clicked on. PORTION is the
4180 amount to scroll of a whole of WHOLE. */
4182 static void
4183 x_send_scroll_bar_event (Lisp_Object window, int part, int portion, int whole)
4185 XEvent event;
4186 XClientMessageEvent *ev = &event.xclient;
4187 struct window *w = XWINDOW (window);
4188 struct frame *f = XFRAME (w->frame);
4189 intptr_t iw = (intptr_t) w;
4190 enum { BITS_PER_INTPTR = CHAR_BIT * sizeof iw };
4191 verify (BITS_PER_INTPTR <= 64);
4192 int sign_shift = BITS_PER_INTPTR - 32;
4194 block_input ();
4196 /* Construct a ClientMessage event to send to the frame. */
4197 ev->type = ClientMessage;
4198 ev->message_type = FRAME_DISPLAY_INFO (f)->Xatom_Scrollbar;
4199 ev->display = FRAME_X_DISPLAY (f);
4200 ev->window = FRAME_X_WINDOW (f);
4201 ev->format = 32;
4203 /* A 32-bit X client on a 64-bit X server can pass a window pointer
4204 as-is. A 64-bit client on a 32-bit X server is in trouble
4205 because a pointer does not fit and would be truncated while
4206 passing through the server. So use two slots and hope that X12
4207 will resolve such issues someday. */
4208 ev->data.l[0] = iw >> 31 >> 1;
4209 ev->data.l[1] = sign_shift <= 0 ? iw : iw << sign_shift >> sign_shift;
4210 ev->data.l[2] = part;
4211 ev->data.l[3] = portion;
4212 ev->data.l[4] = whole;
4214 /* Make Xt timeouts work while the scroll bar is active. */
4215 #ifdef USE_X_TOOLKIT
4216 toolkit_scroll_bar_interaction = 1;
4217 x_activate_timeout_atimer ();
4218 #endif
4220 /* Setting the event mask to zero means that the message will
4221 be sent to the client that created the window, and if that
4222 window no longer exists, no event will be sent. */
4223 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4224 unblock_input ();
4228 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4229 in *IEVENT. */
4231 static void
4232 x_scroll_bar_to_input_event (const XEvent *event,
4233 struct input_event *ievent)
4235 const XClientMessageEvent *ev = &event->xclient;
4236 Lisp_Object window;
4237 struct window *w;
4239 /* See the comment in the function above. */
4240 intptr_t iw0 = ev->data.l[0];
4241 intptr_t iw1 = ev->data.l[1];
4242 intptr_t iw = (iw0 << 31 << 1) + (iw1 & 0xffffffffu);
4243 w = (struct window *) iw;
4245 XSETWINDOW (window, w);
4247 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4248 ievent->frame_or_window = window;
4249 ievent->arg = Qnil;
4250 #ifdef USE_GTK
4251 ievent->timestamp = CurrentTime;
4252 #else
4253 ievent->timestamp =
4254 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
4255 #endif
4256 ievent->code = 0;
4257 ievent->part = ev->data.l[2];
4258 ievent->x = make_number (ev->data.l[3]);
4259 ievent->y = make_number (ev->data.l[4]);
4260 ievent->modifiers = 0;
4264 #ifdef USE_MOTIF
4266 /* Minimum and maximum values used for Motif scroll bars. */
4268 #define XM_SB_MAX 10000000
4271 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4272 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4273 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4275 static void
4276 xm_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4278 struct scroll_bar *bar = client_data;
4279 XmScrollBarCallbackStruct *cs = call_data;
4280 int part = -1, whole = 0, portion = 0;
4282 switch (cs->reason)
4284 case XmCR_DECREMENT:
4285 bar->dragging = -1;
4286 part = scroll_bar_up_arrow;
4287 break;
4289 case XmCR_INCREMENT:
4290 bar->dragging = -1;
4291 part = scroll_bar_down_arrow;
4292 break;
4294 case XmCR_PAGE_DECREMENT:
4295 bar->dragging = -1;
4296 part = scroll_bar_above_handle;
4297 break;
4299 case XmCR_PAGE_INCREMENT:
4300 bar->dragging = -1;
4301 part = scroll_bar_below_handle;
4302 break;
4304 case XmCR_TO_TOP:
4305 bar->dragging = -1;
4306 part = scroll_bar_to_top;
4307 break;
4309 case XmCR_TO_BOTTOM:
4310 bar->dragging = -1;
4311 part = scroll_bar_to_bottom;
4312 break;
4314 case XmCR_DRAG:
4316 int slider_size;
4318 /* Get the slider size. */
4319 block_input ();
4320 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4321 unblock_input ();
4323 whole = XM_SB_MAX - slider_size;
4324 portion = min (cs->value, whole);
4325 part = scroll_bar_handle;
4326 bar->dragging = cs->value;
4328 break;
4330 case XmCR_VALUE_CHANGED:
4331 break;
4334 if (part >= 0)
4336 window_being_scrolled = bar->window;
4337 x_send_scroll_bar_event (bar->window, part, portion, whole);
4341 #elif defined USE_GTK
4343 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4344 bar widget. DATA is a pointer to the scroll_bar structure. */
4346 static gboolean
4347 xg_scroll_callback (GtkRange *range,
4348 GtkScrollType scroll,
4349 gdouble value,
4350 gpointer user_data)
4352 struct scroll_bar *bar = user_data;
4353 gdouble position;
4354 int part = -1, whole = 0, portion = 0;
4355 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
4356 struct frame *f = g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
4358 if (xg_ignore_gtk_scrollbar) return FALSE;
4359 position = gtk_adjustment_get_value (adj);
4362 switch (scroll)
4364 case GTK_SCROLL_JUMP:
4365 /* Buttons 1 2 or 3 must be grabbed. */
4366 if (FRAME_DISPLAY_INFO (f)->grabbed != 0
4367 && FRAME_DISPLAY_INFO (f)->grabbed < (1 << 4))
4369 part = scroll_bar_handle;
4370 whole = gtk_adjustment_get_upper (adj) -
4371 gtk_adjustment_get_page_size (adj);
4372 portion = min ((int)position, whole);
4373 bar->dragging = portion;
4375 break;
4376 case GTK_SCROLL_STEP_BACKWARD:
4377 part = scroll_bar_up_arrow;
4378 bar->dragging = -1;
4379 break;
4380 case GTK_SCROLL_STEP_FORWARD:
4381 part = scroll_bar_down_arrow;
4382 bar->dragging = -1;
4383 break;
4384 case GTK_SCROLL_PAGE_BACKWARD:
4385 part = scroll_bar_above_handle;
4386 bar->dragging = -1;
4387 break;
4388 case GTK_SCROLL_PAGE_FORWARD:
4389 part = scroll_bar_below_handle;
4390 bar->dragging = -1;
4391 break;
4394 if (part >= 0)
4396 window_being_scrolled = bar->window;
4397 x_send_scroll_bar_event (bar->window, part, portion, whole);
4400 return FALSE;
4403 /* Callback for button release. Sets dragging to -1 when dragging is done. */
4405 static gboolean
4406 xg_end_scroll_callback (GtkWidget *widget,
4407 GdkEventButton *event,
4408 gpointer user_data)
4410 struct scroll_bar *bar = user_data;
4411 bar->dragging = -1;
4412 if (WINDOWP (window_being_scrolled))
4414 x_send_scroll_bar_event (window_being_scrolled,
4415 scroll_bar_end_scroll, 0, 0);
4416 window_being_scrolled = Qnil;
4419 return FALSE;
4423 #else /* not USE_GTK and not USE_MOTIF */
4425 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4426 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4427 scroll bar struct. CALL_DATA is a pointer to a float saying where
4428 the thumb is. */
4430 static void
4431 xaw_jump_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4433 struct scroll_bar *bar = client_data;
4434 float *top_addr = call_data;
4435 float top = *top_addr;
4436 float shown;
4437 int whole, portion, height;
4438 enum scroll_bar_part part;
4440 /* Get the size of the thumb, a value between 0 and 1. */
4441 block_input ();
4442 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4443 unblock_input ();
4445 whole = 10000000;
4446 portion = shown < 1 ? top * whole : 0;
4448 if (shown < 1 && (eabs (top + shown - 1) < 1.0f / height))
4449 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4450 the bottom, so we force the scrolling whenever we see that we're
4451 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4452 we try to ensure that we always stay two pixels away from the
4453 bottom). */
4454 part = scroll_bar_down_arrow;
4455 else
4456 part = scroll_bar_handle;
4458 window_being_scrolled = bar->window;
4459 bar->dragging = portion;
4460 bar->last_seen_part = part;
4461 x_send_scroll_bar_event (bar->window, part, portion, whole);
4465 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4466 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4467 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4468 the scroll bar. CALL_DATA is an integer specifying the action that
4469 has taken place. Its magnitude is in the range 0..height of the
4470 scroll bar. Negative values mean scroll towards buffer start.
4471 Values < height of scroll bar mean line-wise movement. */
4473 static void
4474 xaw_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4476 struct scroll_bar *bar = client_data;
4477 /* The position really is stored cast to a pointer. */
4478 int position = (intptr_t) call_data;
4479 Dimension height;
4480 enum scroll_bar_part part;
4482 /* Get the height of the scroll bar. */
4483 block_input ();
4484 XtVaGetValues (widget, XtNheight, &height, NULL);
4485 unblock_input ();
4487 if (eabs (position) >= height)
4488 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4490 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4491 it maps line-movement to call_data = max(5, height/20). */
4492 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
4493 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4494 else
4495 part = scroll_bar_move_ratio;
4497 window_being_scrolled = bar->window;
4498 bar->dragging = -1;
4499 bar->last_seen_part = part;
4500 x_send_scroll_bar_event (bar->window, part, position, height);
4503 #endif /* not USE_GTK and not USE_MOTIF */
4505 #define SCROLL_BAR_NAME "verticalScrollBar"
4507 /* Create the widget for scroll bar BAR on frame F. Record the widget
4508 and X window of the scroll bar in BAR. */
4510 #ifdef USE_GTK
4511 static void
4512 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
4514 const char *scroll_bar_name = SCROLL_BAR_NAME;
4516 block_input ();
4517 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4518 G_CALLBACK (xg_end_scroll_callback),
4519 scroll_bar_name);
4520 unblock_input ();
4523 #else /* not USE_GTK */
4525 static void
4526 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
4528 Window xwindow;
4529 Widget widget;
4530 Arg av[20];
4531 int ac = 0;
4532 const char *scroll_bar_name = SCROLL_BAR_NAME;
4533 unsigned long pixel;
4535 block_input ();
4537 #ifdef USE_MOTIF
4538 /* Set resources. Create the widget. */
4539 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4540 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4541 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4542 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4543 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4544 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4545 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4547 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4548 if (pixel != -1)
4550 XtSetArg (av[ac], XmNforeground, pixel);
4551 ++ac;
4554 pixel = f->output_data.x->scroll_bar_background_pixel;
4555 if (pixel != -1)
4557 XtSetArg (av[ac], XmNbackground, pixel);
4558 ++ac;
4561 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4562 (char *) scroll_bar_name, av, ac);
4564 /* Add one callback for everything that can happen. */
4565 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4566 (XtPointer) bar);
4567 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4568 (XtPointer) bar);
4569 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4570 (XtPointer) bar);
4571 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4572 (XtPointer) bar);
4573 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4574 (XtPointer) bar);
4575 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4576 (XtPointer) bar);
4577 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4578 (XtPointer) bar);
4580 /* Realize the widget. Only after that is the X window created. */
4581 XtRealizeWidget (widget);
4583 /* Set the cursor to an arrow. I didn't find a resource to do that.
4584 And I'm wondering why it hasn't an arrow cursor by default. */
4585 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4586 f->output_data.x->nontext_cursor);
4588 #else /* !USE_MOTIF i.e. use Xaw */
4590 /* Set resources. Create the widget. The background of the
4591 Xaw3d scroll bar widget is a little bit light for my taste.
4592 We don't alter it here to let users change it according
4593 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4594 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4595 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4596 /* For smoother scrolling with Xaw3d -sm */
4597 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4599 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4600 if (pixel != -1)
4602 XtSetArg (av[ac], XtNforeground, pixel);
4603 ++ac;
4606 pixel = f->output_data.x->scroll_bar_background_pixel;
4607 if (pixel != -1)
4609 XtSetArg (av[ac], XtNbackground, pixel);
4610 ++ac;
4613 /* Top/bottom shadow colors. */
4615 /* Allocate them, if necessary. */
4616 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4618 pixel = f->output_data.x->scroll_bar_background_pixel;
4619 if (pixel != -1)
4621 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4622 FRAME_X_COLORMAP (f),
4623 &pixel, 1.2, 0x8000))
4624 pixel = -1;
4625 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4628 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4630 pixel = f->output_data.x->scroll_bar_background_pixel;
4631 if (pixel != -1)
4633 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4634 FRAME_X_COLORMAP (f),
4635 &pixel, 0.6, 0x4000))
4636 pixel = -1;
4637 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4641 #ifdef XtNbeNiceToColormap
4642 /* Tell the toolkit about them. */
4643 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4644 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4645 /* We tried to allocate a color for the top/bottom shadow, and
4646 failed, so tell Xaw3d to use dithering instead. */
4647 /* But only if we have a small colormap. Xaw3d can allocate nice
4648 colors itself. */
4650 XtSetArg (av[ac], XtNbeNiceToColormap,
4651 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
4652 ++ac;
4654 else
4655 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4656 be more consistent with other emacs 3d colors, and since Xaw3d is
4657 not good at dealing with allocation failure. */
4659 /* This tells Xaw3d to use real colors instead of dithering for
4660 the shadows. */
4661 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4662 ++ac;
4664 /* Specify the colors. */
4665 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4666 if (pixel != -1)
4668 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4669 ++ac;
4671 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4672 if (pixel != -1)
4674 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4675 ++ac;
4678 #endif
4680 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4681 f->output_data.x->edit_widget, av, ac);
4684 char const *initial = "";
4685 char const *val = initial;
4686 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4687 #ifdef XtNarrowScrollbars
4688 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4689 #endif
4690 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4691 if (xaw3d_arrow_scroll || val == initial)
4692 { /* ARROW_SCROLL */
4693 xaw3d_arrow_scroll = True;
4694 /* Isn't that just a personal preference ? --Stef */
4695 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4699 /* Define callbacks. */
4700 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4701 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4702 (XtPointer) bar);
4704 /* Realize the widget. Only after that is the X window created. */
4705 XtRealizeWidget (widget);
4707 #endif /* !USE_MOTIF */
4709 /* Install an action hook that lets us detect when the user
4710 finishes interacting with a scroll bar. */
4711 if (action_hook_id == 0)
4712 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4714 /* Remember X window and widget in the scroll bar vector. */
4715 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4716 xwindow = XtWindow (widget);
4717 bar->x_window = xwindow;
4719 unblock_input ();
4721 #endif /* not USE_GTK */
4724 /* Set the thumb size and position of scroll bar BAR. We are currently
4725 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4727 #ifdef USE_GTK
4728 static void
4729 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
4731 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4734 #else /* not USE_GTK */
4735 static void
4736 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
4737 int whole)
4739 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4740 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4741 float top, shown;
4743 block_input ();
4745 #ifdef USE_MOTIF
4747 if (scroll_bar_adjust_thumb_portion_p)
4749 /* We use an estimate of 30 chars per line rather than the real
4750 `portion' value. This has the disadvantage that the thumb size
4751 is not very representative, but it makes our life a lot easier.
4752 Otherwise, we have to constantly adjust the thumb size, which
4753 we can't always do quickly enough: while dragging, the size of
4754 the thumb might prevent the user from dragging the thumb all the
4755 way to the end. but Motif and some versions of Xaw3d don't allow
4756 updating the thumb size while dragging. Also, even if we can update
4757 its size, the update will often happen too late.
4758 If you don't believe it, check out revision 1.650 of xterm.c to see
4759 what hoops we were going through and the still poor behavior we got. */
4760 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4761 /* When the thumb is at the bottom, position == whole.
4762 So we need to increase `whole' to make space for the thumb. */
4763 whole += portion;
4766 if (whole <= 0)
4767 top = 0, shown = 1;
4768 else
4770 top = (float) position / whole;
4771 shown = (float) portion / whole;
4774 if (bar->dragging == -1)
4776 int size, value;
4778 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4779 is the scroll bar's maximum and MIN is the scroll bar's minimum
4780 value. */
4781 size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
4783 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4784 value = top * XM_SB_MAX;
4785 value = min (value, XM_SB_MAX - size);
4787 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4789 #else /* !USE_MOTIF i.e. use Xaw */
4791 if (whole == 0)
4792 top = 0, shown = 1;
4793 else
4795 top = (float) position / whole;
4796 shown = (float) portion / whole;
4800 float old_top, old_shown;
4801 Dimension height;
4802 XtVaGetValues (widget,
4803 XtNtopOfThumb, &old_top,
4804 XtNshown, &old_shown,
4805 XtNheight, &height,
4806 NULL);
4808 /* Massage the top+shown values. */
4809 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
4810 top = max (0, min (1, top));
4811 else
4812 top = old_top;
4813 #if ! defined (HAVE_XAW3D)
4814 /* With Xaw, 'top' values too closer to 1.0 may
4815 cause the thumb to disappear. Fix that. */
4816 top = min (top, 0.99f);
4817 #endif
4818 /* Keep two pixels available for moving the thumb down. */
4819 shown = max (0, min (1 - top - (2.0f / height), shown));
4820 #if ! defined (HAVE_XAW3D)
4821 /* Likewise with too small 'shown'. */
4822 shown = max (shown, 0.01f);
4823 #endif
4825 /* If the call to XawScrollbarSetThumb below doesn't seem to
4826 work, check that 'NARROWPROTO' is defined in src/config.h.
4827 If this is not so, most likely you need to fix configure. */
4828 if (top != old_top || shown != old_shown)
4830 if (bar->dragging == -1)
4831 XawScrollbarSetThumb (widget, top, shown);
4832 else
4834 /* Try to make the scrolling a tad smoother. */
4835 if (!xaw3d_pick_top)
4836 shown = min (shown, old_shown);
4838 XawScrollbarSetThumb (widget, top, shown);
4842 #endif /* !USE_MOTIF */
4844 unblock_input ();
4846 #endif /* not USE_GTK */
4848 #endif /* USE_TOOLKIT_SCROLL_BARS */
4852 /************************************************************************
4853 Scroll bars, general
4854 ************************************************************************/
4856 /* Create a scroll bar and return the scroll bar vector for it. W is
4857 the Emacs window on which to create the scroll bar. TOP, LEFT,
4858 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4859 scroll bar. */
4861 static struct scroll_bar *
4862 x_scroll_bar_create (struct window *w, int top, int left, int width, int height)
4864 struct frame *f = XFRAME (w->frame);
4865 struct scroll_bar *bar
4866 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
4867 Lisp_Object barobj;
4869 block_input ();
4871 #ifdef USE_TOOLKIT_SCROLL_BARS
4872 x_create_toolkit_scroll_bar (f, bar);
4873 #else /* not USE_TOOLKIT_SCROLL_BARS */
4875 XSetWindowAttributes a;
4876 unsigned long mask;
4877 Window window;
4879 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4880 if (a.background_pixel == -1)
4881 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4883 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4884 | ButtonMotionMask | PointerMotionHintMask
4885 | ExposureMask);
4886 a.cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4888 mask = (CWBackPixel | CWEventMask | CWCursor);
4890 /* Clear the area of W that will serve as a scroll bar. This is
4891 for the case that a window has been split horizontally. In
4892 this case, no clear_frame is generated to reduce flickering. */
4893 if (width > 0 && window_box_height (w) > 0)
4894 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4895 left, top, width, window_box_height (w));
4897 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4898 /* Position and size of scroll bar. */
4899 left, top, width, height,
4900 /* Border width, depth, class, and visual. */
4902 CopyFromParent,
4903 CopyFromParent,
4904 CopyFromParent,
4905 /* Attributes. */
4906 mask, &a);
4907 bar->x_window = window;
4909 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4911 XSETWINDOW (bar->window, w);
4912 bar->top = top;
4913 bar->left = left;
4914 bar->width = width;
4915 bar->height = height;
4916 bar->start = 0;
4917 bar->end = 0;
4918 bar->dragging = -1;
4919 #if defined (USE_TOOLKIT_SCROLL_BARS) && defined (USE_LUCID)
4920 bar->last_seen_part = scroll_bar_nowhere;
4921 #endif
4923 /* Add bar to its frame's list of scroll bars. */
4924 bar->next = FRAME_SCROLL_BARS (f);
4925 bar->prev = Qnil;
4926 XSETVECTOR (barobj, bar);
4927 fset_scroll_bars (f, barobj);
4928 if (!NILP (bar->next))
4929 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4931 /* Map the window/widget. */
4932 #ifdef USE_TOOLKIT_SCROLL_BARS
4934 #ifdef USE_GTK
4935 xg_update_scrollbar_pos (f, bar->x_window, top,
4936 left,width, max (height, 1));
4937 #else /* not USE_GTK */
4938 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4939 XtConfigureWidget (scroll_bar, left, top, width, max (height, 1), 0);
4940 XtMapWidget (scroll_bar);
4941 #endif /* not USE_GTK */
4943 #else /* not USE_TOOLKIT_SCROLL_BARS */
4944 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
4945 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4947 unblock_input ();
4948 return bar;
4952 #ifndef USE_TOOLKIT_SCROLL_BARS
4954 /* Draw BAR's handle in the proper position.
4956 If the handle is already drawn from START to END, don't bother
4957 redrawing it, unless REBUILD is non-zero; in that case, always
4958 redraw it. (REBUILD is handy for drawing the handle after expose
4959 events.)
4961 Normally, we want to constrain the start and end of the handle to
4962 fit inside its rectangle, but if the user is dragging the scroll
4963 bar handle, we want to let them drag it down all the way, so that
4964 the bar's top is as far down as it goes; otherwise, there's no way
4965 to move to the very end of the buffer. */
4967 static void
4968 x_scroll_bar_set_handle (struct scroll_bar *bar, int start, int end, int rebuild)
4970 bool dragging = bar->dragging != -1;
4971 Window w = bar->x_window;
4972 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4973 GC gc = f->output_data.x->normal_gc;
4975 /* If the display is already accurate, do nothing. */
4976 if (! rebuild
4977 && start == bar->start
4978 && end == bar->end)
4979 return;
4981 block_input ();
4984 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
4985 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
4986 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
4988 /* Make sure the values are reasonable, and try to preserve
4989 the distance between start and end. */
4991 int length = end - start;
4993 if (start < 0)
4994 start = 0;
4995 else if (start > top_range)
4996 start = top_range;
4997 end = start + length;
4999 if (end < start)
5000 end = start;
5001 else if (end > top_range && ! dragging)
5002 end = top_range;
5005 /* Store the adjusted setting in the scroll bar. */
5006 bar->start = start;
5007 bar->end = end;
5009 /* Clip the end position, just for display. */
5010 if (end > top_range)
5011 end = top_range;
5013 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
5014 below top positions, to make sure the handle is always at least
5015 that many pixels tall. */
5016 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
5018 /* Draw the empty space above the handle. Note that we can't clear
5019 zero-height areas; that means "clear to end of window." */
5020 if (start > 0)
5021 x_clear_area (FRAME_X_DISPLAY (f), w,
5022 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5023 VERTICAL_SCROLL_BAR_TOP_BORDER,
5024 inside_width, start);
5026 /* Change to proper foreground color if one is specified. */
5027 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5028 XSetForeground (FRAME_X_DISPLAY (f), gc,
5029 f->output_data.x->scroll_bar_foreground_pixel);
5031 /* Draw the handle itself. */
5032 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
5033 /* x, y, width, height */
5034 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5035 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
5036 inside_width, end - start);
5038 /* Restore the foreground color of the GC if we changed it above. */
5039 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5040 XSetForeground (FRAME_X_DISPLAY (f), gc,
5041 FRAME_FOREGROUND_PIXEL (f));
5043 /* Draw the empty space below the handle. Note that we can't
5044 clear zero-height areas; that means "clear to end of window." */
5045 if (end < inside_height)
5046 x_clear_area (FRAME_X_DISPLAY (f), w,
5047 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5048 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
5049 inside_width, inside_height - end);
5052 unblock_input ();
5055 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5057 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
5058 nil. */
5060 static void
5061 x_scroll_bar_remove (struct scroll_bar *bar)
5063 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5064 block_input ();
5066 #ifdef USE_TOOLKIT_SCROLL_BARS
5067 #ifdef USE_GTK
5068 xg_remove_scroll_bar (f, bar->x_window);
5069 #else /* not USE_GTK */
5070 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5071 #endif /* not USE_GTK */
5072 #else
5073 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
5074 #endif
5076 /* Dissociate this scroll bar from its window. */
5077 wset_vertical_scroll_bar (XWINDOW (bar->window), Qnil);
5079 unblock_input ();
5083 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5084 that we are displaying PORTION characters out of a total of WHOLE
5085 characters, starting at POSITION. If WINDOW has no scroll bar,
5086 create one. */
5088 static void
5089 XTset_vertical_scroll_bar (struct window *w, int portion, int whole, int position)
5091 struct frame *f = XFRAME (w->frame);
5092 Lisp_Object barobj;
5093 struct scroll_bar *bar;
5094 int top, height, left, width;
5095 int window_y, window_height;
5097 /* Get window dimensions. */
5098 window_box (w, ANY_AREA, 0, &window_y, 0, &window_height);
5099 top = window_y;
5100 height = window_height;
5102 /* Compute the left edge and the width of the scroll bar area. */
5103 left = WINDOW_SCROLL_BAR_AREA_X (w);
5104 width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
5106 /* Does the scroll bar exist yet? */
5107 if (NILP (w->vertical_scroll_bar))
5109 if (width > 0 && height > 0)
5111 block_input ();
5112 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5113 left, top, width, height);
5114 unblock_input ();
5117 bar = x_scroll_bar_create (w, top, left, width, max (height, 1));
5119 else
5121 /* It may just need to be moved and resized. */
5122 unsigned int mask = 0;
5124 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5126 block_input ();
5128 if (left != bar->left)
5129 mask |= CWX;
5130 if (top != bar->top)
5131 mask |= CWY;
5132 if (width != bar->width)
5133 mask |= CWWidth;
5134 if (height != bar->height)
5135 mask |= CWHeight;
5137 #ifdef USE_TOOLKIT_SCROLL_BARS
5139 /* Move/size the scroll bar widget. */
5140 if (mask)
5142 /* Since toolkit scroll bars are smaller than the space reserved
5143 for them on the frame, we have to clear "under" them. */
5144 if (width > 0 && height > 0)
5145 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5146 left, top, width, height);
5147 #ifdef USE_GTK
5148 xg_update_scrollbar_pos (f, bar->x_window, top,
5149 left, width, max (height, 1));
5150 #else /* not USE_GTK */
5151 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5152 left, top, width, max (height, 1), 0);
5153 #endif /* not USE_GTK */
5155 #else /* not USE_TOOLKIT_SCROLL_BARS */
5157 /* Move/size the scroll bar window. */
5158 if (mask)
5160 XWindowChanges wc;
5162 wc.x = left;
5163 wc.y = top;
5164 wc.width = width;
5165 wc.height = height;
5166 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5167 mask, &wc);
5170 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5172 /* Remember new settings. */
5173 bar->left = left;
5174 bar->top = top;
5175 bar->width = width;
5176 bar->height = height;
5178 unblock_input ();
5181 #ifdef USE_TOOLKIT_SCROLL_BARS
5182 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5183 #else /* not USE_TOOLKIT_SCROLL_BARS */
5184 /* Set the scroll bar's current state, unless we're currently being
5185 dragged. */
5186 if (bar->dragging == -1)
5188 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5190 if (whole == 0)
5191 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5192 else
5194 int start = ((double) position * top_range) / whole;
5195 int end = ((double) (position + portion) * top_range) / whole;
5196 x_scroll_bar_set_handle (bar, start, end, 0);
5199 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5201 XSETVECTOR (barobj, bar);
5202 wset_vertical_scroll_bar (w, barobj);
5206 /* The following three hooks are used when we're doing a thorough
5207 redisplay of the frame. We don't explicitly know which scroll bars
5208 are going to be deleted, because keeping track of when windows go
5209 away is a real pain - "Can you say set-window-configuration, boys
5210 and girls?" Instead, we just assert at the beginning of redisplay
5211 that *all* scroll bars are to be removed, and then save a scroll bar
5212 from the fiery pit when we actually redisplay its window. */
5214 /* Arrange for all scroll bars on FRAME to be removed at the next call
5215 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5216 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5218 static void
5219 XTcondemn_scroll_bars (struct frame *frame)
5221 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5222 while (! NILP (FRAME_SCROLL_BARS (frame)))
5224 Lisp_Object bar;
5225 bar = FRAME_SCROLL_BARS (frame);
5226 fset_scroll_bars (frame, XSCROLL_BAR (bar)->next);
5227 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5228 XSCROLL_BAR (bar)->prev = Qnil;
5229 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5230 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5231 fset_condemned_scroll_bars (frame, bar);
5236 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5237 Note that WINDOW isn't necessarily condemned at all. */
5239 static void
5240 XTredeem_scroll_bar (struct window *window)
5242 struct scroll_bar *bar;
5243 struct frame *f;
5244 Lisp_Object barobj;
5246 /* We can't redeem this window's scroll bar if it doesn't have one. */
5247 if (NILP (window->vertical_scroll_bar))
5248 emacs_abort ();
5250 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5252 /* Unlink it from the condemned list. */
5253 f = XFRAME (WINDOW_FRAME (window));
5254 if (NILP (bar->prev))
5256 /* If the prev pointer is nil, it must be the first in one of
5257 the lists. */
5258 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5259 /* It's not condemned. Everything's fine. */
5260 return;
5261 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5262 window->vertical_scroll_bar))
5263 fset_condemned_scroll_bars (f, bar->next);
5264 else
5265 /* If its prev pointer is nil, it must be at the front of
5266 one or the other! */
5267 emacs_abort ();
5269 else
5270 XSCROLL_BAR (bar->prev)->next = bar->next;
5272 if (! NILP (bar->next))
5273 XSCROLL_BAR (bar->next)->prev = bar->prev;
5275 bar->next = FRAME_SCROLL_BARS (f);
5276 bar->prev = Qnil;
5277 XSETVECTOR (barobj, bar);
5278 fset_scroll_bars (f, barobj);
5279 if (! NILP (bar->next))
5280 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5283 /* Remove all scroll bars on FRAME that haven't been saved since the
5284 last call to `*condemn_scroll_bars_hook'. */
5286 static void
5287 XTjudge_scroll_bars (struct frame *f)
5289 Lisp_Object bar, next;
5291 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5293 /* Clear out the condemned list now so we won't try to process any
5294 more events on the hapless scroll bars. */
5295 fset_condemned_scroll_bars (f, Qnil);
5297 for (; ! NILP (bar); bar = next)
5299 struct scroll_bar *b = XSCROLL_BAR (bar);
5301 x_scroll_bar_remove (b);
5303 next = b->next;
5304 b->next = b->prev = Qnil;
5307 /* Now there should be no references to the condemned scroll bars,
5308 and they should get garbage-collected. */
5312 #ifndef USE_TOOLKIT_SCROLL_BARS
5313 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5314 is a no-op when using toolkit scroll bars.
5316 This may be called from a signal handler, so we have to ignore GC
5317 mark bits. */
5319 static void
5320 x_scroll_bar_expose (struct scroll_bar *bar, const XEvent *event)
5322 Window w = bar->x_window;
5323 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5324 GC gc = f->output_data.x->normal_gc;
5326 block_input ();
5328 x_scroll_bar_set_handle (bar, bar->start, bar->end, 1);
5330 /* Switch to scroll bar foreground color. */
5331 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5332 XSetForeground (FRAME_X_DISPLAY (f), gc,
5333 f->output_data.x->scroll_bar_foreground_pixel);
5335 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5336 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5337 /* x, y, width, height */
5338 0, 0, bar->width - 1, bar->height - 1);
5340 /* Restore the foreground color of the GC if we changed it above. */
5341 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5342 XSetForeground (FRAME_X_DISPLAY (f), gc,
5343 FRAME_FOREGROUND_PIXEL (f));
5345 unblock_input ();
5348 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5350 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5351 is set to something other than NO_EVENT, it is enqueued.
5353 This may be called from a signal handler, so we have to ignore GC
5354 mark bits. */
5357 static void
5358 x_scroll_bar_handle_click (struct scroll_bar *bar,
5359 const XEvent *event,
5360 struct input_event *emacs_event)
5362 if (! WINDOWP (bar->window))
5363 emacs_abort ();
5365 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5366 emacs_event->code = event->xbutton.button - Button1;
5367 emacs_event->modifiers
5368 = (x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO
5369 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5370 event->xbutton.state)
5371 | (event->type == ButtonRelease
5372 ? up_modifier
5373 : down_modifier));
5374 emacs_event->frame_or_window = bar->window;
5375 emacs_event->arg = Qnil;
5376 emacs_event->timestamp = event->xbutton.time;
5378 int top_range
5379 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5380 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5382 if (y < 0) y = 0;
5383 if (y > top_range) y = top_range;
5385 if (y < bar->start)
5386 emacs_event->part = scroll_bar_above_handle;
5387 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5388 emacs_event->part = scroll_bar_handle;
5389 else
5390 emacs_event->part = scroll_bar_below_handle;
5392 #ifndef USE_TOOLKIT_SCROLL_BARS
5393 /* If the user has released the handle, set it to its final position. */
5394 if (event->type == ButtonRelease && bar->dragging != -1)
5396 int new_start = y - bar->dragging;
5397 int new_end = new_start + bar->end - bar->start;
5399 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5400 bar->dragging = -1;
5402 #endif
5404 XSETINT (emacs_event->x, y);
5405 XSETINT (emacs_event->y, top_range);
5409 #ifndef USE_TOOLKIT_SCROLL_BARS
5411 /* Handle some mouse motion while someone is dragging the scroll bar.
5413 This may be called from a signal handler, so we have to ignore GC
5414 mark bits. */
5416 static void
5417 x_scroll_bar_note_movement (struct scroll_bar *bar,
5418 const XMotionEvent *event)
5420 struct frame *f = XFRAME (XWINDOW (bar->window)->frame);
5421 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
5423 dpyinfo->last_mouse_movement_time = event->time;
5424 dpyinfo->last_mouse_scroll_bar = bar;
5425 f->mouse_moved = 1;
5427 /* If we're dragging the bar, display it. */
5428 if (bar->dragging != -1)
5430 /* Where should the handle be now? */
5431 int new_start = event->y - bar->dragging;
5433 if (new_start != bar->start)
5435 int new_end = new_start + bar->end - bar->start;
5437 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5442 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5444 /* Return information to the user about the current position of the mouse
5445 on the scroll bar. */
5447 static void
5448 x_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
5449 enum scroll_bar_part *part, Lisp_Object *x,
5450 Lisp_Object *y, Time *timestamp)
5452 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (*fp);
5453 struct scroll_bar *bar = dpyinfo->last_mouse_scroll_bar;
5454 Window w = bar->x_window;
5455 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5456 int win_x, win_y;
5457 Window dummy_window;
5458 int dummy_coord;
5459 unsigned int dummy_mask;
5461 block_input ();
5463 /* Get the mouse's position relative to the scroll bar window, and
5464 report that. */
5465 if (XQueryPointer (FRAME_X_DISPLAY (f), w,
5467 /* Root, child, root x and root y. */
5468 &dummy_window, &dummy_window,
5469 &dummy_coord, &dummy_coord,
5471 /* Position relative to scroll bar. */
5472 &win_x, &win_y,
5474 /* Mouse buttons and modifier keys. */
5475 &dummy_mask))
5477 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5479 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5481 if (bar->dragging != -1)
5482 win_y -= bar->dragging;
5484 if (win_y < 0)
5485 win_y = 0;
5486 if (win_y > top_range)
5487 win_y = top_range;
5489 *fp = f;
5490 *bar_window = bar->window;
5492 if (bar->dragging != -1)
5493 *part = scroll_bar_handle;
5494 else if (win_y < bar->start)
5495 *part = scroll_bar_above_handle;
5496 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5497 *part = scroll_bar_handle;
5498 else
5499 *part = scroll_bar_below_handle;
5501 XSETINT (*x, win_y);
5502 XSETINT (*y, top_range);
5504 f->mouse_moved = 0;
5505 dpyinfo->last_mouse_scroll_bar = NULL;
5506 *timestamp = dpyinfo->last_mouse_movement_time;
5509 unblock_input ();
5513 /* The screen has been cleared so we may have changed foreground or
5514 background colors, and the scroll bars may need to be redrawn.
5515 Clear out the scroll bars, and ask for expose events, so we can
5516 redraw them. */
5518 static void
5519 x_scroll_bar_clear (struct frame *f)
5521 #ifndef USE_TOOLKIT_SCROLL_BARS
5522 Lisp_Object bar;
5524 /* We can have scroll bars even if this is 0,
5525 if we just turned off scroll bar mode.
5526 But in that case we should not clear them. */
5527 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5528 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5529 bar = XSCROLL_BAR (bar)->next)
5530 XClearArea (FRAME_X_DISPLAY (f),
5531 XSCROLL_BAR (bar)->x_window,
5532 0, 0, 0, 0, True);
5533 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5536 #ifdef ENABLE_CHECKING
5538 /* Record the last 100 characters stored
5539 to help debug the loss-of-chars-during-GC problem. */
5541 static int temp_index;
5542 static short temp_buffer[100];
5544 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5545 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5546 temp_index = 0; \
5547 temp_buffer[temp_index++] = (keysym)
5549 #else /* not ENABLE_CHECKING */
5551 #define STORE_KEYSYM_FOR_DEBUG(keysym) ((void)0)
5553 #endif /* ENABLE_CHECKING */
5555 /* Set this to nonzero to fake an "X I/O error"
5556 on a particular display. */
5558 static struct x_display_info *XTread_socket_fake_io_error;
5560 /* When we find no input here, we occasionally do a no-op command
5561 to verify that the X server is still running and we can still talk with it.
5562 We try all the open displays, one by one.
5563 This variable is used for cycling thru the displays. */
5565 static struct x_display_info *next_noop_dpyinfo;
5567 enum
5569 X_EVENT_NORMAL,
5570 X_EVENT_GOTO_OUT,
5571 X_EVENT_DROP
5574 /* Filter events for the current X input method.
5575 DPYINFO is the display this event is for.
5576 EVENT is the X event to filter.
5578 Returns non-zero if the event was filtered, caller shall not process
5579 this event further.
5580 Returns zero if event is wasn't filtered. */
5582 #ifdef HAVE_X_I18N
5583 static int
5584 x_filter_event (struct x_display_info *dpyinfo, XEvent *event)
5586 /* XFilterEvent returns non-zero if the input method has
5587 consumed the event. We pass the frame's X window to
5588 XFilterEvent because that's the one for which the IC
5589 was created. */
5591 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5592 event->xclient.window);
5594 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5596 #endif
5598 #ifdef USE_GTK
5599 static int current_count;
5600 static int current_finish;
5601 static struct input_event *current_hold_quit;
5603 /* This is the filter function invoked by the GTK event loop.
5604 It is invoked before the XEvent is translated to a GdkEvent,
5605 so we have a chance to act on the event before GTK. */
5606 static GdkFilterReturn
5607 event_handler_gdk (GdkXEvent *gxev, GdkEvent *ev, gpointer data)
5609 XEvent *xev = (XEvent *) gxev;
5611 block_input ();
5612 if (current_count >= 0)
5614 struct x_display_info *dpyinfo;
5616 dpyinfo = x_display_info_for_display (xev->xany.display);
5618 #ifdef HAVE_X_I18N
5619 /* Filter events for the current X input method.
5620 GTK calls XFilterEvent but not for key press and release,
5621 so we do it here. */
5622 if ((xev->type == KeyPress || xev->type == KeyRelease)
5623 && dpyinfo
5624 && x_filter_event (dpyinfo, xev))
5626 unblock_input ();
5627 return GDK_FILTER_REMOVE;
5629 #endif
5631 if (! dpyinfo)
5632 current_finish = X_EVENT_NORMAL;
5633 else
5634 current_count +=
5635 handle_one_xevent (dpyinfo, xev, &current_finish,
5636 current_hold_quit);
5638 else
5639 current_finish = x_dispatch_event (xev, xev->xany.display);
5641 unblock_input ();
5643 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5644 return GDK_FILTER_REMOVE;
5646 return GDK_FILTER_CONTINUE;
5648 #endif /* USE_GTK */
5651 static void xembed_send_message (struct frame *f, Time,
5652 enum xembed_message,
5653 long detail, long data1, long data2);
5655 /* Handles the XEvent EVENT on display DPYINFO.
5657 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5658 *FINISH is zero if caller should continue reading events.
5659 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5660 *EVENT is unchanged unless we're processing KeyPress event.
5662 We return the number of characters stored into the buffer. */
5664 static int
5665 handle_one_xevent (struct x_display_info *dpyinfo,
5666 const XEvent *event,
5667 int *finish, struct input_event *hold_quit)
5669 union {
5670 struct input_event ie;
5671 struct selection_input_event sie;
5672 } inev;
5673 int count = 0;
5674 int do_help = 0;
5675 ptrdiff_t nbytes = 0;
5676 struct frame *any, *f = NULL;
5677 struct coding_system coding;
5678 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
5679 /* This holds the state XLookupString needs to implement dead keys
5680 and other tricks known as "compose processing". _X Window System_
5681 says that a portable program can't use this, but Stephen Gildea assures
5682 me that letting the compiler initialize it to zeros will work okay. */
5683 static XComposeStatus compose_status;
5685 USE_SAFE_ALLOCA;
5687 *finish = X_EVENT_NORMAL;
5689 EVENT_INIT (inev.ie);
5690 inev.ie.kind = NO_EVENT;
5691 inev.ie.arg = Qnil;
5693 any = x_any_window_to_frame (dpyinfo, event->xany.window);
5695 if (any && any->wait_event_type == event->type)
5696 any->wait_event_type = 0; /* Indicates we got it. */
5698 switch (event->type)
5700 case ClientMessage:
5702 if (event->xclient.message_type == dpyinfo->Xatom_wm_protocols
5703 && event->xclient.format == 32)
5705 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_take_focus)
5707 /* Use the value returned by x_any_window_to_frame
5708 because this could be the shell widget window
5709 if the frame has no title bar. */
5710 f = any;
5711 #ifdef HAVE_X_I18N
5712 /* Not quite sure this is needed -pd */
5713 if (f && FRAME_XIC (f))
5714 XSetICFocus (FRAME_XIC (f));
5715 #endif
5716 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5717 instructs the WM to set the input focus automatically for
5718 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5719 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5720 it has set the focus. So, XSetInputFocus below is not
5721 needed.
5723 The call to XSetInputFocus below has also caused trouble. In
5724 cases where the XSetInputFocus done by the WM and the one
5725 below are temporally close (on a fast machine), the call
5726 below can generate additional FocusIn events which confuse
5727 Emacs. */
5729 /* Since we set WM_TAKE_FOCUS, we must call
5730 XSetInputFocus explicitly. But not if f is null,
5731 since that might be an event for a deleted frame. */
5732 if (f)
5734 Display *d = event->xclient.display;
5735 /* Catch and ignore errors, in case window has been
5736 iconified by a window manager such as GWM. */
5737 x_catch_errors (d);
5738 XSetInputFocus (d, event->xclient.window,
5739 /* The ICCCM says this is
5740 the only valid choice. */
5741 RevertToParent,
5742 event->xclient.data.l[1]);
5743 /* This is needed to detect the error
5744 if there is an error. */
5745 XSync (d, False);
5746 x_uncatch_errors ();
5748 /* Not certain about handling scroll bars here */
5749 #endif /* 0 */
5750 goto done;
5753 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_save_yourself)
5755 /* Save state modify the WM_COMMAND property to
5756 something which can reinstate us. This notifies
5757 the session manager, who's looking for such a
5758 PropertyNotify. Can restart processing when
5759 a keyboard or mouse event arrives. */
5760 /* If we have a session manager, don't set this.
5761 KDE will then start two Emacsen, one for the
5762 session manager and one for this. */
5763 #ifdef HAVE_X_SM
5764 if (! x_session_have_connection ())
5765 #endif
5767 f = x_top_window_to_frame (dpyinfo,
5768 event->xclient.window);
5769 /* This is just so we only give real data once
5770 for a single Emacs process. */
5771 if (f == SELECTED_FRAME ())
5772 XSetCommand (FRAME_X_DISPLAY (f),
5773 event->xclient.window,
5774 initial_argv, initial_argc);
5775 else if (f)
5776 XSetCommand (FRAME_X_DISPLAY (f),
5777 event->xclient.window,
5778 0, 0);
5780 goto done;
5783 if (event->xclient.data.l[0] == dpyinfo->Xatom_wm_delete_window)
5785 f = any;
5786 if (!f)
5787 goto OTHER; /* May be a dialog that is to be removed */
5789 inev.ie.kind = DELETE_WINDOW_EVENT;
5790 XSETFRAME (inev.ie.frame_or_window, f);
5791 goto done;
5794 goto done;
5797 if (event->xclient.message_type == dpyinfo->Xatom_wm_configure_denied)
5798 goto done;
5800 if (event->xclient.message_type == dpyinfo->Xatom_wm_window_moved)
5802 int new_x, new_y;
5803 f = x_window_to_frame (dpyinfo, event->xclient.window);
5805 new_x = event->xclient.data.s[0];
5806 new_y = event->xclient.data.s[1];
5808 if (f)
5810 f->left_pos = new_x;
5811 f->top_pos = new_y;
5813 goto done;
5816 #ifdef HACK_EDITRES
5817 if (event->xclient.message_type == dpyinfo->Xatom_editres)
5819 f = any;
5820 if (f)
5821 _XEditResCheckMessages (f->output_data.x->widget,
5822 NULL, (XEvent *) event, NULL);
5823 goto done;
5825 #endif /* HACK_EDITRES */
5827 if (event->xclient.message_type == dpyinfo->Xatom_DONE
5828 || event->xclient.message_type == dpyinfo->Xatom_PAGE)
5830 /* Ghostview job completed. Kill it. We could
5831 reply with "Next" if we received "Page", but we
5832 currently never do because we are interested in
5833 images, only, which should have 1 page. */
5834 Pixmap pixmap = (Pixmap) event->xclient.data.l[1];
5835 f = x_window_to_frame (dpyinfo, event->xclient.window);
5836 if (!f)
5837 goto OTHER;
5838 x_kill_gs_process (pixmap, f);
5839 expose_frame (f, 0, 0, 0, 0);
5840 goto done;
5843 #ifdef USE_TOOLKIT_SCROLL_BARS
5844 /* Scroll bar callbacks send a ClientMessage from which
5845 we construct an input_event. */
5846 if (event->xclient.message_type == dpyinfo->Xatom_Scrollbar)
5848 x_scroll_bar_to_input_event (event, &inev.ie);
5849 *finish = X_EVENT_GOTO_OUT;
5850 goto done;
5852 #endif /* USE_TOOLKIT_SCROLL_BARS */
5854 /* XEmbed messages from the embedder (if any). */
5855 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
5857 enum xembed_message msg = event->xclient.data.l[1];
5858 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
5859 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
5861 *finish = X_EVENT_GOTO_OUT;
5862 goto done;
5865 xft_settings_event (dpyinfo, event);
5867 f = any;
5868 if (!f)
5869 goto OTHER;
5870 if (x_handle_dnd_message (f, &event->xclient, dpyinfo, &inev.ie))
5871 *finish = X_EVENT_DROP;
5873 break;
5875 case SelectionNotify:
5876 dpyinfo->last_user_time = event->xselection.time;
5877 #ifdef USE_X_TOOLKIT
5878 if (! x_window_to_frame (dpyinfo, event->xselection.requestor))
5879 goto OTHER;
5880 #endif /* not USE_X_TOOLKIT */
5881 x_handle_selection_notify (&event->xselection);
5882 break;
5884 case SelectionClear: /* Someone has grabbed ownership. */
5885 dpyinfo->last_user_time = event->xselectionclear.time;
5886 #ifdef USE_X_TOOLKIT
5887 if (! x_window_to_frame (dpyinfo, event->xselectionclear.window))
5888 goto OTHER;
5889 #endif /* USE_X_TOOLKIT */
5891 const XSelectionClearEvent *eventp = &event->xselectionclear;
5893 inev.ie.kind = SELECTION_CLEAR_EVENT;
5894 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
5895 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
5896 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
5898 break;
5900 case SelectionRequest: /* Someone wants our selection. */
5901 dpyinfo->last_user_time = event->xselectionrequest.time;
5902 #ifdef USE_X_TOOLKIT
5903 if (!x_window_to_frame (dpyinfo, event->xselectionrequest.owner))
5904 goto OTHER;
5905 #endif /* USE_X_TOOLKIT */
5907 const XSelectionRequestEvent *eventp = &event->xselectionrequest;
5909 inev.ie.kind = SELECTION_REQUEST_EVENT;
5910 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
5911 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
5912 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
5913 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
5914 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
5915 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
5917 break;
5919 case PropertyNotify:
5920 dpyinfo->last_user_time = event->xproperty.time;
5921 f = x_top_window_to_frame (dpyinfo, event->xproperty.window);
5922 if (f && event->xproperty.atom == dpyinfo->Xatom_net_wm_state)
5923 if (x_handle_net_wm_state (f, &event->xproperty)
5924 && FRAME_ICONIFIED_P (f)
5925 && f->output_data.x->net_wm_state_hidden_seen)
5927 /* Gnome shell does not iconify us when C-z is pressed.
5928 It hides the frame. So if our state says we aren't
5929 hidden anymore, treat it as deiconified. */
5930 SET_FRAME_VISIBLE (f, 1);
5931 SET_FRAME_ICONIFIED (f, 0);
5932 f->output_data.x->has_been_visible = 1;
5933 f->output_data.x->net_wm_state_hidden_seen = 0;
5934 inev.ie.kind = DEICONIFY_EVENT;
5935 XSETFRAME (inev.ie.frame_or_window, f);
5938 x_handle_property_notify (&event->xproperty);
5939 xft_settings_event (dpyinfo, event);
5940 goto OTHER;
5942 case ReparentNotify:
5943 f = x_top_window_to_frame (dpyinfo, event->xreparent.window);
5944 if (f)
5946 f->output_data.x->parent_desc = event->xreparent.parent;
5947 x_real_positions (f, &f->left_pos, &f->top_pos);
5949 /* Perhaps reparented due to a WM restart. Reset this. */
5950 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
5951 FRAME_DISPLAY_INFO (f)->net_supported_window = 0;
5953 x_set_frame_alpha (f);
5955 goto OTHER;
5957 case Expose:
5958 f = x_window_to_frame (dpyinfo, event->xexpose.window);
5959 if (f)
5961 if (!FRAME_VISIBLE_P (f))
5963 SET_FRAME_VISIBLE (f, 1);
5964 SET_FRAME_ICONIFIED (f, 0);
5965 f->output_data.x->has_been_visible = 1;
5966 SET_FRAME_GARBAGED (f);
5968 else
5970 #ifdef USE_GTK
5971 /* This seems to be needed for GTK 2.6 and later, see
5972 http://debbugs.gnu.org/cgi/bugreport.cgi?bug=15398. */
5973 x_clear_area (event->xexpose.display,
5974 event->xexpose.window,
5975 event->xexpose.x, event->xexpose.y,
5976 event->xexpose.width, event->xexpose.height);
5977 #endif
5978 expose_frame (f, event->xexpose.x, event->xexpose.y,
5979 event->xexpose.width, event->xexpose.height);
5982 else
5984 #ifndef USE_TOOLKIT_SCROLL_BARS
5985 struct scroll_bar *bar;
5986 #endif
5987 #if defined USE_LUCID
5988 /* Submenus of the Lucid menu bar aren't widgets
5989 themselves, so there's no way to dispatch events
5990 to them. Recognize this case separately. */
5992 Widget widget = x_window_to_menu_bar (event->xexpose.window);
5993 if (widget)
5994 xlwmenu_redisplay (widget);
5996 #endif /* USE_LUCID */
5998 #ifdef USE_TOOLKIT_SCROLL_BARS
5999 /* Dispatch event to the widget. */
6000 goto OTHER;
6001 #else /* not USE_TOOLKIT_SCROLL_BARS */
6002 bar = x_window_to_scroll_bar (event->xexpose.display,
6003 event->xexpose.window);
6005 if (bar)
6006 x_scroll_bar_expose (bar, event);
6007 #ifdef USE_X_TOOLKIT
6008 else
6009 goto OTHER;
6010 #endif /* USE_X_TOOLKIT */
6011 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6013 break;
6015 case GraphicsExpose: /* This occurs when an XCopyArea's
6016 source area was obscured or not
6017 available. */
6018 f = x_window_to_frame (dpyinfo, event->xgraphicsexpose.drawable);
6019 if (f)
6020 expose_frame (f, event->xgraphicsexpose.x,
6021 event->xgraphicsexpose.y,
6022 event->xgraphicsexpose.width,
6023 event->xgraphicsexpose.height);
6024 #ifdef USE_X_TOOLKIT
6025 else
6026 goto OTHER;
6027 #endif /* USE_X_TOOLKIT */
6028 break;
6030 case NoExpose: /* This occurs when an XCopyArea's
6031 source area was completely
6032 available. */
6033 break;
6035 case UnmapNotify:
6036 /* Redo the mouse-highlight after the tooltip has gone. */
6037 if (event->xunmap.window == tip_window)
6039 tip_window = 0;
6040 x_redo_mouse_highlight (dpyinfo);
6043 f = x_top_window_to_frame (dpyinfo, event->xunmap.window);
6044 if (f) /* F may no longer exist if
6045 the frame was deleted. */
6047 bool visible = FRAME_VISIBLE_P (f);
6048 /* While a frame is unmapped, display generation is
6049 disabled; you don't want to spend time updating a
6050 display that won't ever be seen. */
6051 SET_FRAME_VISIBLE (f, 0);
6052 /* We can't distinguish, from the event, whether the window
6053 has become iconified or invisible. So assume, if it
6054 was previously visible, than now it is iconified.
6055 But x_make_frame_invisible clears both
6056 the visible flag and the iconified flag;
6057 and that way, we know the window is not iconified now. */
6058 if (visible || FRAME_ICONIFIED_P (f))
6060 SET_FRAME_ICONIFIED (f, 1);
6061 inev.ie.kind = ICONIFY_EVENT;
6062 XSETFRAME (inev.ie.frame_or_window, f);
6065 goto OTHER;
6067 case MapNotify:
6068 if (event->xmap.window == tip_window)
6069 /* The tooltip has been drawn already. Avoid
6070 the SET_FRAME_GARBAGED below. */
6071 goto OTHER;
6073 /* We use x_top_window_to_frame because map events can
6074 come for sub-windows and they don't mean that the
6075 frame is visible. */
6076 f = x_top_window_to_frame (dpyinfo, event->xmap.window);
6077 if (f)
6079 bool iconified = FRAME_ICONIFIED_P (f);
6080 /* wait_reading_process_output will notice this and update
6081 the frame's display structures.
6082 If we where iconified, we should not set garbaged,
6083 because that stops redrawing on Expose events. This looks
6084 bad if we are called from a recursive event loop
6085 (x_dispatch_event), for example when a dialog is up. */
6086 if (!iconified)
6087 SET_FRAME_GARBAGED (f);
6089 /* Check if fullscreen was specified before we where mapped the
6090 first time, i.e. from the command line. */
6091 if (!f->output_data.x->has_been_visible)
6092 x_check_fullscreen (f);
6094 SET_FRAME_VISIBLE (f, 1);
6095 SET_FRAME_ICONIFIED (f, 0);
6096 f->output_data.x->has_been_visible = 1;
6098 if (iconified)
6100 inev.ie.kind = DEICONIFY_EVENT;
6101 XSETFRAME (inev.ie.frame_or_window, f);
6103 else if (! NILP (Vframe_list) && ! NILP (XCDR (Vframe_list)))
6104 /* Force a redisplay sooner or later to update the
6105 frame titles in case this is the second frame. */
6106 record_asynch_buffer_change ();
6108 #ifdef USE_GTK
6109 xg_frame_resized (f, -1, -1);
6110 #endif
6112 goto OTHER;
6114 case KeyPress:
6116 dpyinfo->last_user_time = event->xkey.time;
6117 ignore_next_mouse_click_timeout = 0;
6119 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6120 /* Dispatch KeyPress events when in menu. */
6121 if (popup_activated ())
6122 goto OTHER;
6123 #endif
6125 f = any;
6127 #if ! defined (USE_GTK)
6128 /* If mouse-highlight is an integer, input clears out
6129 mouse highlighting. */
6130 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
6131 && (f == 0
6132 || !EQ (f->tool_bar_window, hlinfo->mouse_face_window)))
6134 clear_mouse_face (hlinfo);
6135 hlinfo->mouse_face_hidden = 1;
6137 #endif
6139 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6140 if (f == 0)
6142 /* Scroll bars consume key events, but we want
6143 the keys to go to the scroll bar's frame. */
6144 Widget widget = XtWindowToWidget (dpyinfo->display,
6145 event->xkey.window);
6146 if (widget && XmIsScrollBar (widget))
6148 widget = XtParent (widget);
6149 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6152 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6154 if (f != 0)
6156 KeySym keysym, orig_keysym;
6157 /* al%imercury@uunet.uu.net says that making this 81
6158 instead of 80 fixed a bug whereby meta chars made
6159 his Emacs hang.
6161 It seems that some version of XmbLookupString has
6162 a bug of not returning XBufferOverflow in
6163 status_return even if the input is too long to
6164 fit in 81 bytes. So, we must prepare sufficient
6165 bytes for copy_buffer. 513 bytes (256 chars for
6166 two-byte character set) seems to be a fairly good
6167 approximation. -- 2000.8.10 handa@etl.go.jp */
6168 unsigned char copy_buffer[513];
6169 unsigned char *copy_bufptr = copy_buffer;
6170 int copy_bufsiz = sizeof (copy_buffer);
6171 int modifiers;
6172 Lisp_Object coding_system = Qlatin_1;
6173 Lisp_Object c;
6174 /* Event will be modified. */
6175 XKeyEvent xkey = event->xkey;
6177 #ifdef USE_GTK
6178 /* Don't pass keys to GTK. A Tab will shift focus to the
6179 tool bar in GTK 2.4. Keys will still go to menus and
6180 dialogs because in that case popup_activated is TRUE
6181 (see above). */
6182 *finish = X_EVENT_DROP;
6183 #endif
6185 xkey.state |= x_emacs_to_x_modifiers (FRAME_DISPLAY_INFO (f),
6186 extra_keyboard_modifiers);
6187 modifiers = xkey.state;
6189 /* This will have to go some day... */
6191 /* make_lispy_event turns chars into control chars.
6192 Don't do it here because XLookupString is too eager. */
6193 xkey.state &= ~ControlMask;
6194 xkey.state &= ~(dpyinfo->meta_mod_mask
6195 | dpyinfo->super_mod_mask
6196 | dpyinfo->hyper_mod_mask
6197 | dpyinfo->alt_mod_mask);
6199 /* In case Meta is ComposeCharacter,
6200 clear its status. According to Markus Ehrnsperger
6201 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6202 this enables ComposeCharacter to work whether or
6203 not it is combined with Meta. */
6204 if (modifiers & dpyinfo->meta_mod_mask)
6205 memset (&compose_status, 0, sizeof (compose_status));
6207 #ifdef HAVE_X_I18N
6208 if (FRAME_XIC (f))
6210 Status status_return;
6212 coding_system = Vlocale_coding_system;
6213 nbytes = XmbLookupString (FRAME_XIC (f),
6214 &xkey, (char *) copy_bufptr,
6215 copy_bufsiz, &keysym,
6216 &status_return);
6217 if (status_return == XBufferOverflow)
6219 copy_bufsiz = nbytes + 1;
6220 copy_bufptr = alloca (copy_bufsiz);
6221 nbytes = XmbLookupString (FRAME_XIC (f),
6222 &xkey, (char *) copy_bufptr,
6223 copy_bufsiz, &keysym,
6224 &status_return);
6226 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6227 if (status_return == XLookupNone)
6228 break;
6229 else if (status_return == XLookupChars)
6231 keysym = NoSymbol;
6232 modifiers = 0;
6234 else if (status_return != XLookupKeySym
6235 && status_return != XLookupBoth)
6236 emacs_abort ();
6238 else
6239 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
6240 copy_bufsiz, &keysym,
6241 &compose_status);
6242 #else
6243 nbytes = XLookupString (&xkey, (char *) copy_bufptr,
6244 copy_bufsiz, &keysym,
6245 &compose_status);
6246 #endif
6248 /* If not using XIM/XIC, and a compose sequence is in progress,
6249 we break here. Otherwise, chars_matched is always 0. */
6250 if (compose_status.chars_matched > 0 && nbytes == 0)
6251 break;
6253 memset (&compose_status, 0, sizeof (compose_status));
6254 orig_keysym = keysym;
6256 /* Common for all keysym input events. */
6257 XSETFRAME (inev.ie.frame_or_window, f);
6258 inev.ie.modifiers
6259 = x_x_to_emacs_modifiers (FRAME_DISPLAY_INFO (f), modifiers);
6260 inev.ie.timestamp = xkey.time;
6262 /* First deal with keysyms which have defined
6263 translations to characters. */
6264 if (keysym >= 32 && keysym < 128)
6265 /* Avoid explicitly decoding each ASCII character. */
6267 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6268 inev.ie.code = keysym;
6269 goto done_keysym;
6272 /* Keysyms directly mapped to Unicode characters. */
6273 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
6275 if (keysym < 0x01000080)
6276 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6277 else
6278 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6279 inev.ie.code = keysym & 0xFFFFFF;
6280 goto done_keysym;
6283 /* Now non-ASCII. */
6284 if (HASH_TABLE_P (Vx_keysym_table)
6285 && (c = Fgethash (make_number (keysym),
6286 Vx_keysym_table,
6287 Qnil),
6288 NATNUMP (c)))
6290 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6291 ? ASCII_KEYSTROKE_EVENT
6292 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6293 inev.ie.code = XFASTINT (c);
6294 goto done_keysym;
6297 /* Random non-modifier sorts of keysyms. */
6298 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6299 || keysym == XK_Delete
6300 #ifdef XK_ISO_Left_Tab
6301 || (keysym >= XK_ISO_Left_Tab
6302 && keysym <= XK_ISO_Enter)
6303 #endif
6304 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6305 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6306 #ifdef HPUX
6307 /* This recognizes the "extended function
6308 keys". It seems there's no cleaner way.
6309 Test IsModifierKey to avoid handling
6310 mode_switch incorrectly. */
6311 || (XK_Select <= keysym && keysym < XK_KP_Space)
6312 #endif
6313 #ifdef XK_dead_circumflex
6314 || orig_keysym == XK_dead_circumflex
6315 #endif
6316 #ifdef XK_dead_grave
6317 || orig_keysym == XK_dead_grave
6318 #endif
6319 #ifdef XK_dead_tilde
6320 || orig_keysym == XK_dead_tilde
6321 #endif
6322 #ifdef XK_dead_diaeresis
6323 || orig_keysym == XK_dead_diaeresis
6324 #endif
6325 #ifdef XK_dead_macron
6326 || orig_keysym == XK_dead_macron
6327 #endif
6328 #ifdef XK_dead_degree
6329 || orig_keysym == XK_dead_degree
6330 #endif
6331 #ifdef XK_dead_acute
6332 || orig_keysym == XK_dead_acute
6333 #endif
6334 #ifdef XK_dead_cedilla
6335 || orig_keysym == XK_dead_cedilla
6336 #endif
6337 #ifdef XK_dead_breve
6338 || orig_keysym == XK_dead_breve
6339 #endif
6340 #ifdef XK_dead_ogonek
6341 || orig_keysym == XK_dead_ogonek
6342 #endif
6343 #ifdef XK_dead_caron
6344 || orig_keysym == XK_dead_caron
6345 #endif
6346 #ifdef XK_dead_doubleacute
6347 || orig_keysym == XK_dead_doubleacute
6348 #endif
6349 #ifdef XK_dead_abovedot
6350 || orig_keysym == XK_dead_abovedot
6351 #endif
6352 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6353 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6354 /* Any "vendor-specific" key is ok. */
6355 || (orig_keysym & (1 << 28))
6356 || (keysym != NoSymbol && nbytes == 0))
6357 && ! (IsModifierKey (orig_keysym)
6358 /* The symbols from XK_ISO_Lock
6359 to XK_ISO_Last_Group_Lock
6360 don't have real modifiers but
6361 should be treated similarly to
6362 Mode_switch by Emacs. */
6363 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6364 || (XK_ISO_Lock <= orig_keysym
6365 && orig_keysym <= XK_ISO_Last_Group_Lock)
6366 #endif
6369 STORE_KEYSYM_FOR_DEBUG (keysym);
6370 /* make_lispy_event will convert this to a symbolic
6371 key. */
6372 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6373 inev.ie.code = keysym;
6374 goto done_keysym;
6377 { /* Raw bytes, not keysym. */
6378 ptrdiff_t i;
6379 int nchars, len;
6381 for (i = 0, nchars = 0; i < nbytes; i++)
6383 if (ASCII_BYTE_P (copy_bufptr[i]))
6384 nchars++;
6385 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6388 if (nchars < nbytes)
6390 /* Decode the input data. */
6392 /* The input should be decoded with `coding_system'
6393 which depends on which X*LookupString function
6394 we used just above and the locale. */
6395 setup_coding_system (coding_system, &coding);
6396 coding.src_multibyte = 0;
6397 coding.dst_multibyte = 1;
6398 /* The input is converted to events, thus we can't
6399 handle composition. Anyway, there's no XIM that
6400 gives us composition information. */
6401 coding.common_flags &= ~CODING_ANNOTATION_MASK;
6403 SAFE_NALLOCA (coding.destination, MAX_MULTIBYTE_LENGTH,
6404 nbytes);
6405 coding.dst_bytes = MAX_MULTIBYTE_LENGTH * nbytes;
6406 coding.mode |= CODING_MODE_LAST_BLOCK;
6407 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
6408 nbytes = coding.produced;
6409 nchars = coding.produced_char;
6410 copy_bufptr = coding.destination;
6413 /* Convert the input data to a sequence of
6414 character events. */
6415 for (i = 0; i < nbytes; i += len)
6417 int ch;
6418 if (nchars == nbytes)
6419 ch = copy_bufptr[i], len = 1;
6420 else
6421 ch = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
6422 inev.ie.kind = (SINGLE_BYTE_CHAR_P (ch)
6423 ? ASCII_KEYSTROKE_EVENT
6424 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6425 inev.ie.code = ch;
6426 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6429 count += nchars;
6431 inev.ie.kind = NO_EVENT; /* Already stored above. */
6433 if (keysym == NoSymbol)
6434 break;
6436 /* FIXME: check side effects and remove this. */
6437 ((XEvent *) event)->xkey = xkey;
6439 done_keysym:
6440 #ifdef HAVE_X_I18N
6441 /* Don't dispatch this event since XtDispatchEvent calls
6442 XFilterEvent, and two calls in a row may freeze the
6443 client. */
6444 break;
6445 #else
6446 goto OTHER;
6447 #endif
6449 case KeyRelease:
6450 dpyinfo->last_user_time = event->xkey.time;
6451 #ifdef HAVE_X_I18N
6452 /* Don't dispatch this event since XtDispatchEvent calls
6453 XFilterEvent, and two calls in a row may freeze the
6454 client. */
6455 break;
6456 #else
6457 goto OTHER;
6458 #endif
6460 case EnterNotify:
6461 dpyinfo->last_user_time = event->xcrossing.time;
6462 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
6464 f = any;
6466 if (f && x_mouse_click_focus_ignore_position)
6467 ignore_next_mouse_click_timeout = event->xmotion.time + 200;
6469 /* EnterNotify counts as mouse movement,
6470 so update things that depend on mouse position. */
6471 if (f && !f->output_data.x->hourglass_p)
6472 note_mouse_movement (f, &event->xmotion);
6473 #ifdef USE_GTK
6474 /* We may get an EnterNotify on the buttons in the toolbar. In that
6475 case we moved out of any highlighted area and need to note this. */
6476 if (!f && dpyinfo->last_mouse_glyph_frame)
6477 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
6478 #endif
6479 goto OTHER;
6481 case FocusIn:
6482 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
6483 goto OTHER;
6485 case LeaveNotify:
6486 dpyinfo->last_user_time = event->xcrossing.time;
6487 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
6489 f = x_top_window_to_frame (dpyinfo, event->xcrossing.window);
6490 if (f)
6492 if (f == hlinfo->mouse_face_mouse_frame)
6494 /* If we move outside the frame, then we're
6495 certainly no longer on any text in the frame. */
6496 clear_mouse_face (hlinfo);
6497 hlinfo->mouse_face_mouse_frame = 0;
6500 /* Generate a nil HELP_EVENT to cancel a help-echo.
6501 Do it only if there's something to cancel.
6502 Otherwise, the startup message is cleared when
6503 the mouse leaves the frame. */
6504 if (any_help_event_p)
6505 do_help = -1;
6507 #ifdef USE_GTK
6508 /* See comment in EnterNotify above */
6509 else if (dpyinfo->last_mouse_glyph_frame)
6510 note_mouse_movement (dpyinfo->last_mouse_glyph_frame, &event->xmotion);
6511 #endif
6512 goto OTHER;
6514 case FocusOut:
6515 x_detect_focus_change (dpyinfo, any, event, &inev.ie);
6516 goto OTHER;
6518 case MotionNotify:
6520 dpyinfo->last_user_time = event->xmotion.time;
6521 previous_help_echo_string = help_echo_string;
6522 help_echo_string = Qnil;
6524 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
6525 : x_window_to_frame (dpyinfo, event->xmotion.window));
6527 if (hlinfo->mouse_face_hidden)
6529 hlinfo->mouse_face_hidden = 0;
6530 clear_mouse_face (hlinfo);
6533 #ifdef USE_GTK
6534 if (f && xg_event_is_for_scrollbar (f, event))
6535 f = 0;
6536 #endif
6537 if (f)
6540 /* Generate SELECT_WINDOW_EVENTs when needed.
6541 Don't let popup menus influence things (bug#1261). */
6542 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
6544 static Lisp_Object last_mouse_window;
6545 Lisp_Object window = window_from_coordinates
6546 (f, event->xmotion.x, event->xmotion.y, 0, 0);
6548 /* Window will be selected only when it is not selected now and
6549 last mouse movement event was not in it. Minibuffer window
6550 will be selected only when it is active. */
6551 if (WINDOWP (window)
6552 && !EQ (window, last_mouse_window)
6553 && !EQ (window, selected_window)
6554 /* For click-to-focus window managers
6555 create event iff we don't leave the
6556 selected frame. */
6557 && (focus_follows_mouse
6558 || (EQ (XWINDOW (window)->frame,
6559 XWINDOW (selected_window)->frame))))
6561 inev.ie.kind = SELECT_WINDOW_EVENT;
6562 inev.ie.frame_or_window = window;
6564 /* Remember the last window where we saw the mouse. */
6565 last_mouse_window = window;
6567 if (!note_mouse_movement (f, &event->xmotion))
6568 help_echo_string = previous_help_echo_string;
6570 else
6572 #ifndef USE_TOOLKIT_SCROLL_BARS
6573 struct scroll_bar *bar
6574 = x_window_to_scroll_bar (event->xmotion.display,
6575 event->xmotion.window);
6577 if (bar)
6578 x_scroll_bar_note_movement (bar, &event->xmotion);
6579 #endif /* USE_TOOLKIT_SCROLL_BARS */
6581 /* If we move outside the frame, then we're
6582 certainly no longer on any text in the frame. */
6583 clear_mouse_face (hlinfo);
6586 /* If the contents of the global variable help_echo_string
6587 has changed, generate a HELP_EVENT. */
6588 if (!NILP (help_echo_string)
6589 || !NILP (previous_help_echo_string))
6590 do_help = 1;
6591 goto OTHER;
6594 case ConfigureNotify:
6595 f = x_top_window_to_frame (dpyinfo, event->xconfigure.window);
6596 #ifdef USE_GTK
6597 if (!f
6598 && (f = any)
6599 && event->xconfigure.window == FRAME_X_WINDOW (f))
6601 xg_frame_resized (f, event->xconfigure.width,
6602 event->xconfigure.height);
6603 f = 0;
6605 #endif
6606 if (f)
6608 #ifndef USE_X_TOOLKIT
6609 #ifndef USE_GTK
6610 int width = FRAME_PIXEL_TO_TEXT_WIDTH (f, event->xconfigure.width);
6611 int height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, event->xconfigure.height);
6613 /* In the toolkit version, change_frame_size
6614 is called by the code that handles resizing
6615 of the EmacsFrame widget. */
6617 /* Even if the number of character rows and columns has
6618 not changed, the font size may have changed, so we need
6619 to check the pixel dimensions as well. */
6620 if (width != FRAME_TEXT_WIDTH (f)
6621 || height != FRAME_TEXT_HEIGHT (f)
6622 || event->xconfigure.width != FRAME_PIXEL_WIDTH (f)
6623 || event->xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6625 change_frame_size (f, width, height, 0, 1, 0, 1);
6626 SET_FRAME_GARBAGED (f);
6627 cancel_mouse_face (f);
6630 FRAME_PIXEL_WIDTH (f) = event->xconfigure.width;
6631 FRAME_PIXEL_HEIGHT (f) = event->xconfigure.height;
6632 #endif /* not USE_GTK */
6633 #endif
6635 #ifdef USE_GTK
6636 /* GTK creates windows but doesn't map them.
6637 Only get real positions when mapped. */
6638 if (FRAME_GTK_OUTER_WIDGET (f)
6639 && gtk_widget_get_mapped (FRAME_GTK_OUTER_WIDGET (f)))
6640 #endif
6641 x_real_positions (f, &f->left_pos, &f->top_pos);
6643 #ifdef HAVE_X_I18N
6644 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6645 xic_set_statusarea (f);
6646 #endif
6649 goto OTHER;
6651 case ButtonRelease:
6652 case ButtonPress:
6654 /* If we decide we want to generate an event to be seen
6655 by the rest of Emacs, we put it here. */
6656 bool tool_bar_p = 0;
6658 memset (&compose_status, 0, sizeof (compose_status));
6659 dpyinfo->last_mouse_glyph_frame = NULL;
6660 dpyinfo->last_user_time = event->xbutton.time;
6662 f = (x_mouse_grabbed (dpyinfo) ? dpyinfo->last_mouse_frame
6663 : x_window_to_frame (dpyinfo, event->xbutton.window));
6665 #ifdef USE_GTK
6666 if (f && xg_event_is_for_scrollbar (f, event))
6667 f = 0;
6668 #endif
6669 if (f)
6671 #if ! defined (USE_GTK)
6672 /* Is this in the tool-bar? */
6673 if (WINDOWP (f->tool_bar_window)
6674 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6676 Lisp_Object window;
6677 int x = event->xbutton.x;
6678 int y = event->xbutton.y;
6680 window = window_from_coordinates (f, x, y, 0, 1);
6681 tool_bar_p = EQ (window, f->tool_bar_window);
6683 if (tool_bar_p && event->xbutton.button < 4)
6684 handle_tool_bar_click
6685 (f, x, y, event->xbutton.type == ButtonPress,
6686 x_x_to_emacs_modifiers (dpyinfo, event->xbutton.state));
6688 #endif /* !USE_GTK */
6690 if (!tool_bar_p)
6691 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6692 if (! popup_activated ())
6693 #endif
6695 if (ignore_next_mouse_click_timeout)
6697 if (event->type == ButtonPress
6698 && event->xbutton.time > ignore_next_mouse_click_timeout)
6700 ignore_next_mouse_click_timeout = 0;
6701 construct_mouse_click (&inev.ie, &event->xbutton, f);
6703 if (event->type == ButtonRelease)
6704 ignore_next_mouse_click_timeout = 0;
6706 else
6707 construct_mouse_click (&inev.ie, &event->xbutton, f);
6709 if (FRAME_X_EMBEDDED_P (f))
6710 xembed_send_message (f, event->xbutton.time,
6711 XEMBED_REQUEST_FOCUS, 0, 0, 0);
6713 else
6715 struct scroll_bar *bar
6716 = x_window_to_scroll_bar (event->xbutton.display,
6717 event->xbutton.window);
6719 #ifdef USE_TOOLKIT_SCROLL_BARS
6720 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6721 scroll bars. */
6722 if (bar && event->xbutton.state & ControlMask)
6724 x_scroll_bar_handle_click (bar, event, &inev.ie);
6725 *finish = X_EVENT_DROP;
6727 #else /* not USE_TOOLKIT_SCROLL_BARS */
6728 if (bar)
6729 x_scroll_bar_handle_click (bar, event, &inev.ie);
6730 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6733 if (event->type == ButtonPress)
6735 dpyinfo->grabbed |= (1 << event->xbutton.button);
6736 dpyinfo->last_mouse_frame = f;
6738 if (!tool_bar_p)
6739 last_tool_bar_item = -1;
6741 else
6742 dpyinfo->grabbed &= ~(1 << event->xbutton.button);
6744 /* Ignore any mouse motion that happened before this event;
6745 any subsequent mouse-movement Emacs events should reflect
6746 only motion after the ButtonPress/Release. */
6747 if (f != 0)
6748 f->mouse_moved = 0;
6750 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6751 f = x_menubar_window_to_frame (dpyinfo, event);
6752 /* For a down-event in the menu bar,
6753 don't pass it to Xt right now.
6754 Instead, save it away
6755 and we will pass it to Xt from kbd_buffer_get_event.
6756 That way, we can run some Lisp code first. */
6757 if (! popup_activated ()
6758 #ifdef USE_GTK
6759 /* Gtk+ menus only react to the first three buttons. */
6760 && event->xbutton.button < 3
6761 #endif
6762 && f && event->type == ButtonPress
6763 /* Verify the event is really within the menu bar
6764 and not just sent to it due to grabbing. */
6765 && event->xbutton.x >= 0
6766 && event->xbutton.x < FRAME_PIXEL_WIDTH (f)
6767 && event->xbutton.y >= 0
6768 && event->xbutton.y < f->output_data.x->menubar_height
6769 && event->xbutton.same_screen)
6771 if (!f->output_data.x->saved_menu_event)
6772 f->output_data.x->saved_menu_event = xmalloc (sizeof *event);
6773 *f->output_data.x->saved_menu_event = *event;
6774 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT;
6775 XSETFRAME (inev.ie.frame_or_window, f);
6776 *finish = X_EVENT_DROP;
6778 else
6779 goto OTHER;
6780 #endif /* USE_X_TOOLKIT || USE_GTK */
6782 break;
6784 case CirculateNotify:
6785 goto OTHER;
6787 case CirculateRequest:
6788 goto OTHER;
6790 case VisibilityNotify:
6791 goto OTHER;
6793 case MappingNotify:
6794 /* Someone has changed the keyboard mapping - update the
6795 local cache. */
6796 switch (event->xmapping.request)
6798 case MappingModifier:
6799 x_find_modifier_meanings (dpyinfo);
6800 /* This is meant to fall through. */
6801 case MappingKeyboard:
6802 XRefreshKeyboardMapping ((XMappingEvent *) &event->xmapping);
6804 goto OTHER;
6806 case DestroyNotify:
6807 xft_settings_event (dpyinfo, event);
6808 break;
6810 default:
6811 OTHER:
6812 #ifdef USE_X_TOOLKIT
6813 block_input ();
6814 if (*finish != X_EVENT_DROP)
6815 XtDispatchEvent ((XEvent *) event);
6816 unblock_input ();
6817 #endif /* USE_X_TOOLKIT */
6818 break;
6821 done:
6822 if (inev.ie.kind != NO_EVENT)
6824 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6825 count++;
6828 if (do_help
6829 && !(hold_quit && hold_quit->kind != NO_EVENT))
6831 Lisp_Object frame;
6833 if (f)
6834 XSETFRAME (frame, f);
6835 else
6836 frame = Qnil;
6838 if (do_help > 0)
6840 any_help_event_p = 1;
6841 gen_help_event (help_echo_string, frame, help_echo_window,
6842 help_echo_object, help_echo_pos);
6844 else
6846 help_echo_string = Qnil;
6847 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
6849 count++;
6852 SAFE_FREE ();
6853 return count;
6856 #if defined USE_GTK || defined USE_X_TOOLKIT
6858 /* Handles the XEvent EVENT on display DISPLAY.
6859 This is used for event loops outside the normal event handling,
6860 i.e. looping while a popup menu or a dialog is posted.
6862 Returns the value handle_one_xevent sets in the finish argument. */
6864 x_dispatch_event (XEvent *event, Display *display)
6866 struct x_display_info *dpyinfo;
6867 int finish = X_EVENT_NORMAL;
6869 dpyinfo = x_display_info_for_display (display);
6871 if (dpyinfo)
6872 handle_one_xevent (dpyinfo, event, &finish, 0);
6874 return finish;
6876 #endif
6879 /* Read events coming from the X server.
6880 Return as soon as there are no more events to be read.
6882 Return the number of characters stored into the buffer,
6883 thus pretending to be `read' (except the characters we store
6884 in the keyboard buffer can be multibyte, so are not necessarily
6885 C chars). */
6887 static int
6888 XTread_socket (struct terminal *terminal, struct input_event *hold_quit)
6890 int count = 0;
6891 int event_found = 0;
6892 struct x_display_info *dpyinfo = terminal->display_info.x;
6894 block_input ();
6896 /* So people can tell when we have read the available input. */
6897 input_signal_count++;
6899 /* For debugging, this gives a way to fake an I/O error. */
6900 if (dpyinfo == XTread_socket_fake_io_error)
6902 XTread_socket_fake_io_error = 0;
6903 x_io_error_quitter (dpyinfo->display);
6906 #ifndef USE_GTK
6907 while (XPending (dpyinfo->display))
6909 int finish;
6910 XEvent event;
6912 XNextEvent (dpyinfo->display, &event);
6914 #ifdef HAVE_X_I18N
6915 /* Filter events for the current X input method. */
6916 if (x_filter_event (dpyinfo, &event))
6917 continue;
6918 #endif
6919 event_found = 1;
6921 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
6923 if (finish == X_EVENT_GOTO_OUT)
6924 break;
6927 #else /* USE_GTK */
6929 /* For GTK we must use the GTK event loop. But XEvents gets passed
6930 to our filter function above, and then to the big event switch.
6931 We use a bunch of globals to communicate with our filter function,
6932 that is kind of ugly, but it works.
6934 There is no way to do one display at the time, GTK just does events
6935 from all displays. */
6937 while (gtk_events_pending ())
6939 current_count = count;
6940 current_hold_quit = hold_quit;
6942 gtk_main_iteration ();
6944 count = current_count;
6945 current_count = -1;
6946 current_hold_quit = 0;
6948 if (current_finish == X_EVENT_GOTO_OUT)
6949 break;
6951 #endif /* USE_GTK */
6953 /* On some systems, an X bug causes Emacs to get no more events
6954 when the window is destroyed. Detect that. (1994.) */
6955 if (! event_found)
6957 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
6958 One XNOOP in 100 loops will make Emacs terminate.
6959 B. Bretthauer, 1994 */
6960 x_noop_count++;
6961 if (x_noop_count >= 100)
6963 x_noop_count=0;
6965 if (next_noop_dpyinfo == 0)
6966 next_noop_dpyinfo = x_display_list;
6968 XNoOp (next_noop_dpyinfo->display);
6970 /* Each time we get here, cycle through the displays now open. */
6971 next_noop_dpyinfo = next_noop_dpyinfo->next;
6975 /* If the focus was just given to an auto-raising frame,
6976 raise it now. FIXME: handle more than one such frame. */
6977 if (dpyinfo->x_pending_autoraise_frame)
6979 x_raise_frame (dpyinfo->x_pending_autoraise_frame);
6980 dpyinfo->x_pending_autoraise_frame = NULL;
6983 unblock_input ();
6985 return count;
6991 /***********************************************************************
6992 Text Cursor
6993 ***********************************************************************/
6995 /* Set clipping for output in glyph row ROW. W is the window in which
6996 we operate. GC is the graphics context to set clipping in.
6998 ROW may be a text row or, e.g., a mode line. Text rows must be
6999 clipped to the interior of the window dedicated to text display,
7000 mode lines must be clipped to the whole window. */
7002 static void
7003 x_clip_to_row (struct window *w, struct glyph_row *row,
7004 enum glyph_row_area area, GC gc)
7006 struct frame *f = XFRAME (WINDOW_FRAME (w));
7007 XRectangle clip_rect;
7008 int window_x, window_y, window_width;
7010 window_box (w, area, &window_x, &window_y, &window_width, 0);
7012 clip_rect.x = window_x;
7013 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7014 clip_rect.y = max (clip_rect.y, window_y);
7015 clip_rect.width = window_width;
7016 clip_rect.height = row->visible_height;
7018 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7022 /* Draw a hollow box cursor on window W in glyph row ROW. */
7024 static void
7025 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
7027 struct frame *f = XFRAME (WINDOW_FRAME (w));
7028 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
7029 Display *dpy = FRAME_X_DISPLAY (f);
7030 int x, y, wd, h;
7031 XGCValues xgcv;
7032 struct glyph *cursor_glyph;
7033 GC gc;
7035 /* Get the glyph the cursor is on. If we can't tell because
7036 the current matrix is invalid or such, give up. */
7037 cursor_glyph = get_phys_cursor_glyph (w);
7038 if (cursor_glyph == NULL)
7039 return;
7041 /* Compute frame-relative coordinates for phys cursor. */
7042 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7043 wd = w->phys_cursor_width;
7045 /* The foreground of cursor_gc is typically the same as the normal
7046 background color, which can cause the cursor box to be invisible. */
7047 xgcv.foreground = f->output_data.x->cursor_pixel;
7048 if (dpyinfo->scratch_cursor_gc)
7049 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7050 else
7051 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7052 GCForeground, &xgcv);
7053 gc = dpyinfo->scratch_cursor_gc;
7055 /* Set clipping, draw the rectangle, and reset clipping again. */
7056 x_clip_to_row (w, row, TEXT_AREA, gc);
7057 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7058 XSetClipMask (dpy, gc, None);
7062 /* Draw a bar cursor on window W in glyph row ROW.
7064 Implementation note: One would like to draw a bar cursor with an
7065 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7066 Unfortunately, I didn't find a font yet that has this property set.
7067 --gerd. */
7069 static void
7070 x_draw_bar_cursor (struct window *w, struct glyph_row *row, int width, enum text_cursor_kinds kind)
7072 struct frame *f = XFRAME (w->frame);
7073 struct glyph *cursor_glyph;
7075 /* If cursor is out of bounds, don't draw garbage. This can happen
7076 in mini-buffer windows when switching between echo area glyphs
7077 and mini-buffer. */
7078 cursor_glyph = get_phys_cursor_glyph (w);
7079 if (cursor_glyph == NULL)
7080 return;
7082 /* If on an image, draw like a normal cursor. That's usually better
7083 visible than drawing a bar, esp. if the image is large so that
7084 the bar might not be in the window. */
7085 if (cursor_glyph->type == IMAGE_GLYPH)
7087 struct glyph_row *r;
7088 r = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7089 draw_phys_cursor_glyph (w, r, DRAW_CURSOR);
7091 else
7093 Display *dpy = FRAME_X_DISPLAY (f);
7094 Window window = FRAME_X_WINDOW (f);
7095 GC gc = FRAME_DISPLAY_INFO (f)->scratch_cursor_gc;
7096 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7097 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7098 XGCValues xgcv;
7100 /* If the glyph's background equals the color we normally draw
7101 the bars cursor in, the bar cursor in its normal color is
7102 invisible. Use the glyph's foreground color instead in this
7103 case, on the assumption that the glyph's colors are chosen so
7104 that the glyph is legible. */
7105 if (face->background == f->output_data.x->cursor_pixel)
7106 xgcv.background = xgcv.foreground = face->foreground;
7107 else
7108 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7109 xgcv.graphics_exposures = 0;
7111 if (gc)
7112 XChangeGC (dpy, gc, mask, &xgcv);
7113 else
7115 gc = XCreateGC (dpy, window, mask, &xgcv);
7116 FRAME_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7119 x_clip_to_row (w, row, TEXT_AREA, gc);
7121 if (kind == BAR_CURSOR)
7123 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
7125 if (width < 0)
7126 width = FRAME_CURSOR_WIDTH (f);
7127 width = min (cursor_glyph->pixel_width, width);
7129 w->phys_cursor_width = width;
7131 /* If the character under cursor is R2L, draw the bar cursor
7132 on the right of its glyph, rather than on the left. */
7133 if ((cursor_glyph->resolved_level & 1) != 0)
7134 x += cursor_glyph->pixel_width - width;
7136 XFillRectangle (dpy, window, gc, x,
7137 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7138 width, row->height);
7140 else
7142 int dummy_x, dummy_y, dummy_h;
7144 if (width < 0)
7145 width = row->height;
7147 width = min (row->height, width);
7149 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
7150 &dummy_y, &dummy_h);
7152 XFillRectangle (dpy, window, gc,
7153 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7154 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7155 row->height - width),
7156 w->phys_cursor_width, width);
7159 XSetClipMask (dpy, gc, None);
7164 /* RIF: Define cursor CURSOR on frame F. */
7166 static void
7167 x_define_frame_cursor (struct frame *f, Cursor cursor)
7169 if (!f->pointer_invisible
7170 && f->output_data.x->current_cursor != cursor)
7171 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7172 f->output_data.x->current_cursor = cursor;
7176 /* RIF: Clear area on frame F. */
7178 static void
7179 x_clear_frame_area (struct frame *f, int x, int y, int width, int height)
7181 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), x, y, width, height);
7182 #ifdef USE_GTK
7183 /* Must queue a redraw, because scroll bars might have been cleared. */
7184 if (FRAME_GTK_WIDGET (f))
7185 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
7186 #endif
7190 /* RIF: Draw cursor on window W. */
7192 static void
7193 x_draw_window_cursor (struct window *w, struct glyph_row *glyph_row, int x,
7194 int y, enum text_cursor_kinds cursor_type,
7195 int cursor_width, bool on_p, bool active_p)
7197 struct frame *f = XFRAME (WINDOW_FRAME (w));
7199 if (on_p)
7201 w->phys_cursor_type = cursor_type;
7202 w->phys_cursor_on_p = 1;
7204 if (glyph_row->exact_window_width_line_p
7205 && (glyph_row->reversed_p
7206 ? (w->phys_cursor.hpos < 0)
7207 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
7209 glyph_row->cursor_in_fringe_p = 1;
7210 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
7212 else
7214 switch (cursor_type)
7216 case HOLLOW_BOX_CURSOR:
7217 x_draw_hollow_cursor (w, glyph_row);
7218 break;
7220 case FILLED_BOX_CURSOR:
7221 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7222 break;
7224 case BAR_CURSOR:
7225 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7226 break;
7228 case HBAR_CURSOR:
7229 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7230 break;
7232 case NO_CURSOR:
7233 w->phys_cursor_width = 0;
7234 break;
7236 default:
7237 emacs_abort ();
7241 #ifdef HAVE_X_I18N
7242 if (w == XWINDOW (f->selected_window))
7243 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7244 xic_set_preeditarea (w, x, y);
7245 #endif
7248 XFlush (FRAME_X_DISPLAY (f));
7252 /* Icons. */
7254 /* Make the x-window of frame F use the gnu icon bitmap. */
7257 x_bitmap_icon (struct frame *f, Lisp_Object file)
7259 ptrdiff_t bitmap_id;
7261 if (FRAME_X_WINDOW (f) == 0)
7262 return 1;
7264 /* Free up our existing icon bitmap and mask if any. */
7265 if (f->output_data.x->icon_bitmap > 0)
7266 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7267 f->output_data.x->icon_bitmap = 0;
7269 if (STRINGP (file))
7271 #ifdef USE_GTK
7272 /* Use gtk_window_set_icon_from_file () if available,
7273 It's not restricted to bitmaps */
7274 if (xg_set_icon (f, file))
7275 return 0;
7276 #endif /* USE_GTK */
7277 bitmap_id = x_create_bitmap_from_file (f, file);
7278 x_create_bitmap_mask (f, bitmap_id);
7280 else
7282 /* Create the GNU bitmap and mask if necessary. */
7283 if (FRAME_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7285 ptrdiff_t rc = -1;
7287 #ifdef USE_GTK
7289 if (xg_set_icon (f, xg_default_icon_file)
7290 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7291 return 0;
7293 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7295 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7296 if (rc != -1)
7297 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7299 #endif
7301 /* If all else fails, use the (black and white) xbm image. */
7302 if (rc == -1)
7304 rc = x_create_bitmap_from_data (f, (char *) gnu_xbm_bits,
7305 gnu_xbm_width, gnu_xbm_height);
7306 if (rc == -1)
7307 return 1;
7309 FRAME_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7310 x_create_bitmap_mask (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
7314 /* The first time we create the GNU bitmap and mask,
7315 this increments the ref-count one extra time.
7316 As a result, the GNU bitmap and mask are never freed.
7317 That way, we don't have to worry about allocating it again. */
7318 x_reference_bitmap (f, FRAME_DISPLAY_INFO (f)->icon_bitmap_id);
7320 bitmap_id = FRAME_DISPLAY_INFO (f)->icon_bitmap_id;
7323 x_wm_set_icon_pixmap (f, bitmap_id);
7324 f->output_data.x->icon_bitmap = bitmap_id;
7326 return 0;
7330 /* Make the x-window of frame F use a rectangle with text.
7331 Use ICON_NAME as the text. */
7334 x_text_icon (struct frame *f, const char *icon_name)
7336 if (FRAME_X_WINDOW (f) == 0)
7337 return 1;
7340 XTextProperty text;
7341 text.value = (unsigned char *) icon_name;
7342 text.encoding = XA_STRING;
7343 text.format = 8;
7344 text.nitems = strlen (icon_name);
7345 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7348 if (f->output_data.x->icon_bitmap > 0)
7349 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7350 f->output_data.x->icon_bitmap = 0;
7351 x_wm_set_icon_pixmap (f, 0);
7353 return 0;
7356 #define X_ERROR_MESSAGE_SIZE 200
7358 /* If non-nil, this should be a string.
7359 It means catch X errors and store the error message in this string.
7361 The reason we use a stack is that x_catch_error/x_uncatch_error can
7362 be called from a signal handler.
7365 struct x_error_message_stack {
7366 char string[X_ERROR_MESSAGE_SIZE];
7367 Display *dpy;
7368 struct x_error_message_stack *prev;
7370 static struct x_error_message_stack *x_error_message;
7372 /* An X error handler which stores the error message in
7373 *x_error_message. This is called from x_error_handler if
7374 x_catch_errors is in effect. */
7376 static void
7377 x_error_catcher (Display *display, XErrorEvent *event)
7379 XGetErrorText (display, event->error_code,
7380 x_error_message->string,
7381 X_ERROR_MESSAGE_SIZE);
7384 /* Begin trapping X errors for display DPY. Actually we trap X errors
7385 for all displays, but DPY should be the display you are actually
7386 operating on.
7388 After calling this function, X protocol errors no longer cause
7389 Emacs to exit; instead, they are recorded in the string
7390 stored in *x_error_message.
7392 Calling x_check_errors signals an Emacs error if an X error has
7393 occurred since the last call to x_catch_errors or x_check_errors.
7395 Calling x_uncatch_errors resumes the normal error handling. */
7397 void
7398 x_catch_errors (Display *dpy)
7400 struct x_error_message_stack *data = xmalloc (sizeof *data);
7402 /* Make sure any errors from previous requests have been dealt with. */
7403 XSync (dpy, False);
7405 data->dpy = dpy;
7406 data->string[0] = 0;
7407 data->prev = x_error_message;
7408 x_error_message = data;
7411 /* Undo the last x_catch_errors call.
7412 DPY should be the display that was passed to x_catch_errors. */
7414 void
7415 x_uncatch_errors (void)
7417 struct x_error_message_stack *tmp;
7419 block_input ();
7421 /* The display may have been closed before this function is called.
7422 Check if it is still open before calling XSync. */
7423 if (x_display_info_for_display (x_error_message->dpy) != 0)
7424 XSync (x_error_message->dpy, False);
7426 tmp = x_error_message;
7427 x_error_message = x_error_message->prev;
7428 xfree (tmp);
7429 unblock_input ();
7432 /* If any X protocol errors have arrived since the last call to
7433 x_catch_errors or x_check_errors, signal an Emacs error using
7434 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7436 void
7437 x_check_errors (Display *dpy, const char *format)
7439 /* Make sure to catch any errors incurred so far. */
7440 XSync (dpy, False);
7442 if (x_error_message->string[0])
7444 char string[X_ERROR_MESSAGE_SIZE];
7445 memcpy (string, x_error_message->string, X_ERROR_MESSAGE_SIZE);
7446 x_uncatch_errors ();
7447 error (format, string);
7451 /* Nonzero if we had any X protocol errors
7452 since we did x_catch_errors on DPY. */
7454 bool
7455 x_had_errors_p (Display *dpy)
7457 /* Make sure to catch any errors incurred so far. */
7458 XSync (dpy, False);
7460 return x_error_message->string[0] != 0;
7463 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7465 void
7466 x_clear_errors (Display *dpy)
7468 x_error_message->string[0] = 0;
7471 #if 0 /* See comment in unwind_to_catch why calling this is a bad
7472 * idea. --lorentey */
7473 /* Close off all unclosed x_catch_errors calls. */
7475 void
7476 x_fully_uncatch_errors (void)
7478 while (x_error_message)
7479 x_uncatch_errors ();
7481 #endif
7483 #if 0
7484 static unsigned int x_wire_count;
7485 x_trace_wire (void)
7487 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7489 #endif /* ! 0 */
7492 /************************************************************************
7493 Handling X errors
7494 ************************************************************************/
7496 /* Error message passed to x_connection_closed. */
7498 static char *error_msg;
7500 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7501 the text of an error message that lead to the connection loss. */
7503 static void
7504 x_connection_closed (Display *dpy, const char *error_message)
7506 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7507 Lisp_Object frame, tail;
7508 ptrdiff_t idx = SPECPDL_INDEX ();
7510 error_msg = alloca (strlen (error_message) + 1);
7511 strcpy (error_msg, error_message);
7513 /* Inhibit redisplay while frames are being deleted. */
7514 specbind (Qinhibit_redisplay, Qt);
7516 if (dpyinfo)
7518 /* Protect display from being closed when we delete the last
7519 frame on it. */
7520 dpyinfo->reference_count++;
7521 dpyinfo->terminal->reference_count++;
7524 /* First delete frames whose mini-buffers are on frames
7525 that are on the dead display. */
7526 FOR_EACH_FRAME (tail, frame)
7528 Lisp_Object minibuf_frame;
7529 minibuf_frame
7530 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7531 if (FRAME_X_P (XFRAME (frame))
7532 && FRAME_X_P (XFRAME (minibuf_frame))
7533 && ! EQ (frame, minibuf_frame)
7534 && FRAME_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7535 delete_frame (frame, Qnoelisp);
7538 /* Now delete all remaining frames on the dead display.
7539 We are now sure none of these is used as the mini-buffer
7540 for another frame that we need to delete. */
7541 FOR_EACH_FRAME (tail, frame)
7542 if (FRAME_X_P (XFRAME (frame))
7543 && FRAME_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7545 /* Set this to t so that delete_frame won't get confused
7546 trying to find a replacement. */
7547 kset_default_minibuffer_frame (FRAME_KBOARD (XFRAME (frame)), Qt);
7548 delete_frame (frame, Qnoelisp);
7551 /* If DPYINFO is null, this means we didn't open the display in the
7552 first place, so don't try to close it. */
7553 if (dpyinfo)
7555 /* We can not call XtCloseDisplay here because it calls XSync.
7556 XSync inside the error handler apparently hangs Emacs. On
7557 current Xt versions, this isn't needed either. */
7558 #ifdef USE_GTK
7559 /* A long-standing GTK bug prevents proper disconnect handling
7560 (https://bugzilla.gnome.org/show_bug.cgi?id=85715). Once,
7561 the resulting Glib error message loop filled a user's disk.
7562 To avoid this, kill Emacs unconditionally on disconnect. */
7563 shut_down_emacs (0, Qnil);
7564 fprintf (stderr, "%s\n\
7565 When compiled with GTK, Emacs cannot recover from X disconnects.\n\
7566 This is a GTK bug: https://bugzilla.gnome.org/show_bug.cgi?id=85715\n\
7567 For details, see etc/PROBLEMS.\n",
7568 error_msg);
7569 emacs_abort ();
7570 #endif /* USE_GTK */
7572 /* Indicate that this display is dead. */
7573 dpyinfo->display = 0;
7575 dpyinfo->reference_count--;
7576 dpyinfo->terminal->reference_count--;
7577 if (dpyinfo->reference_count != 0)
7578 /* We have just closed all frames on this display. */
7579 emacs_abort ();
7582 Lisp_Object tmp;
7583 XSETTERMINAL (tmp, dpyinfo->terminal);
7584 Fdelete_terminal (tmp, Qnoelisp);
7588 if (terminal_list == 0)
7590 fprintf (stderr, "%s\n", error_msg);
7591 Fkill_emacs (make_number (70));
7592 /* NOTREACHED */
7595 totally_unblock_input ();
7597 unbind_to (idx, Qnil);
7598 clear_waiting_for_input ();
7600 /* Tell GCC not to suggest attribute 'noreturn' for this function. */
7601 IF_LINT (if (! terminal_list) return; )
7603 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
7604 longjmp), because returning from this function would get us back into
7605 Xlib's code which will directly call `exit'. */
7606 error ("%s", error_msg);
7609 /* We specifically use it before defining it, so that gcc doesn't inline it,
7610 otherwise gdb doesn't know how to properly put a breakpoint on it. */
7611 static void x_error_quitter (Display *, XErrorEvent *);
7613 /* This is the first-level handler for X protocol errors.
7614 It calls x_error_quitter or x_error_catcher. */
7616 static int
7617 x_error_handler (Display *display, XErrorEvent *event)
7619 #if defined USE_GTK && defined HAVE_GTK3
7620 if ((event->error_code == BadMatch || event->error_code == BadWindow)
7621 && event->request_code == X_SetInputFocus)
7623 return 0;
7625 #endif
7627 if (x_error_message)
7628 x_error_catcher (display, event);
7629 else
7630 x_error_quitter (display, event);
7631 return 0;
7634 /* This is the usual handler for X protocol errors.
7635 It kills all frames on the display that we got the error for.
7636 If that was the only one, it prints an error message and kills Emacs. */
7638 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
7640 /* On older GCC versions, just putting x_error_quitter
7641 after x_error_handler prevents inlining into the former. */
7643 static void NO_INLINE
7644 x_error_quitter (Display *display, XErrorEvent *event)
7646 char buf[256], buf1[356];
7648 /* Ignore BadName errors. They can happen because of fonts
7649 or colors that are not defined. */
7651 if (event->error_code == BadName)
7652 return;
7654 /* Note that there is no real way portable across R3/R4 to get the
7655 original error handler. */
7657 XGetErrorText (display, event->error_code, buf, sizeof (buf));
7658 sprintf (buf1, "X protocol error: %s on protocol request %d",
7659 buf, event->request_code);
7660 x_connection_closed (display, buf1);
7664 /* This is the handler for X IO errors, always.
7665 It kills all frames on the display that we lost touch with.
7666 If that was the only one, it prints an error message and kills Emacs. */
7668 static int
7669 x_io_error_quitter (Display *display)
7671 char buf[256];
7673 snprintf (buf, sizeof buf, "Connection lost to X server `%s'",
7674 DisplayString (display));
7675 x_connection_closed (display, buf);
7676 return 0;
7679 /* Changing the font of the frame. */
7681 /* Give frame F the font FONT-OBJECT as its default font. The return
7682 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
7683 frame. If it is negative, generate a new fontset from
7684 FONT-OBJECT. */
7686 Lisp_Object
7687 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
7689 struct font *font = XFONT_OBJECT (font_object);
7691 if (fontset < 0)
7692 fontset = fontset_from_font (font_object);
7693 FRAME_FONTSET (f) = fontset;
7694 if (FRAME_FONT (f) == font)
7695 /* This font is already set in frame F. There's nothing more to
7696 do. */
7697 return font_object;
7699 FRAME_FONT (f) = font;
7700 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
7701 FRAME_COLUMN_WIDTH (f) = font->average_width;
7702 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (font);
7704 compute_fringe_widths (f, 1);
7706 /* Compute the scroll bar width in character columns. */
7707 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
7709 int wid = FRAME_COLUMN_WIDTH (f);
7711 FRAME_CONFIG_SCROLL_BAR_COLS (f)
7712 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
7714 else
7716 int wid = FRAME_COLUMN_WIDTH (f);
7718 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = 14;
7719 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
7722 if (FRAME_X_WINDOW (f) != 0)
7724 /* Don't change the size of a tip frame; there's no point in
7725 doing it because it's done in Fx_show_tip, and it leads to
7726 problems because the tip frame has no widget. */
7727 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
7728 x_set_window_size (f, 0, FRAME_TEXT_WIDTH (f), FRAME_TEXT_HEIGHT (f), 1);
7731 #ifdef HAVE_X_I18N
7732 if (FRAME_XIC (f)
7733 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
7735 block_input ();
7736 xic_set_xfontset (f, SSDATA (fontset_ascii (fontset)));
7737 unblock_input ();
7739 #endif
7741 return font_object;
7745 /***********************************************************************
7746 X Input Methods
7747 ***********************************************************************/
7749 #ifdef HAVE_X_I18N
7751 #ifdef HAVE_X11R6
7753 /* XIM destroy callback function, which is called whenever the
7754 connection to input method XIM dies. CLIENT_DATA contains a
7755 pointer to the x_display_info structure corresponding to XIM. */
7757 static void
7758 xim_destroy_callback (XIM xim, XPointer client_data, XPointer call_data)
7760 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
7761 Lisp_Object frame, tail;
7763 block_input ();
7765 /* No need to call XDestroyIC.. */
7766 FOR_EACH_FRAME (tail, frame)
7768 struct frame *f = XFRAME (frame);
7769 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo)
7771 FRAME_XIC (f) = NULL;
7772 xic_free_xfontset (f);
7776 /* No need to call XCloseIM. */
7777 dpyinfo->xim = NULL;
7778 XFree (dpyinfo->xim_styles);
7779 unblock_input ();
7782 #endif /* HAVE_X11R6 */
7784 #ifdef HAVE_X11R6
7785 /* This isn't prototyped in OSF 5.0 or 5.1a. */
7786 extern char *XSetIMValues (XIM, ...);
7787 #endif
7789 /* Open the connection to the XIM server on display DPYINFO.
7790 RESOURCE_NAME is the resource name Emacs uses. */
7792 static void
7793 xim_open_dpy (struct x_display_info *dpyinfo, char *resource_name)
7795 XIM xim;
7797 #ifdef HAVE_XIM
7798 if (use_xim)
7800 if (dpyinfo->xim)
7801 XCloseIM (dpyinfo->xim);
7802 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
7803 emacs_class);
7804 dpyinfo->xim = xim;
7806 if (xim)
7808 #ifdef HAVE_X11R6
7809 XIMCallback destroy;
7810 #endif
7812 /* Get supported styles and XIM values. */
7813 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
7815 #ifdef HAVE_X11R6
7816 destroy.callback = xim_destroy_callback;
7817 destroy.client_data = (XPointer)dpyinfo;
7818 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
7819 #endif
7823 else
7824 #endif /* HAVE_XIM */
7825 dpyinfo->xim = NULL;
7829 #ifdef HAVE_X11R6_XIM
7831 /* XIM instantiate callback function, which is called whenever an XIM
7832 server is available. DISPLAY is the display of the XIM.
7833 CLIENT_DATA contains a pointer to an xim_inst_t structure created
7834 when the callback was registered. */
7836 static void
7837 xim_instantiate_callback (Display *display, XPointer client_data, XPointer call_data)
7839 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
7840 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
7842 /* We don't support multiple XIM connections. */
7843 if (dpyinfo->xim)
7844 return;
7846 xim_open_dpy (dpyinfo, xim_inst->resource_name);
7848 /* Create XIC for the existing frames on the same display, as long
7849 as they have no XIC. */
7850 if (dpyinfo->xim && dpyinfo->reference_count > 0)
7852 Lisp_Object tail, frame;
7854 block_input ();
7855 FOR_EACH_FRAME (tail, frame)
7857 struct frame *f = XFRAME (frame);
7859 if (FRAME_X_P (f)
7860 && FRAME_DISPLAY_INFO (f) == xim_inst->dpyinfo)
7861 if (FRAME_XIC (f) == NULL)
7863 create_frame_xic (f);
7864 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
7865 xic_set_statusarea (f);
7866 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
7868 struct window *w = XWINDOW (f->selected_window);
7869 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
7874 unblock_input ();
7878 #endif /* HAVE_X11R6_XIM */
7881 /* Open a connection to the XIM server on display DPYINFO.
7882 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
7883 connection only at the first time. On X11R6, open the connection
7884 in the XIM instantiate callback function. */
7886 static void
7887 xim_initialize (struct x_display_info *dpyinfo, char *resource_name)
7889 dpyinfo->xim = NULL;
7890 #ifdef HAVE_XIM
7891 if (use_xim)
7893 #ifdef HAVE_X11R6_XIM
7894 struct xim_inst_t *xim_inst = xmalloc (sizeof *xim_inst);
7896 dpyinfo->xim_callback_data = xim_inst;
7897 xim_inst->dpyinfo = dpyinfo;
7898 xim_inst->resource_name = xstrdup (resource_name);
7899 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
7900 resource_name, emacs_class,
7901 xim_instantiate_callback,
7902 /* This is XPointer in XFree86
7903 but (XPointer *) on Tru64, at
7904 least, hence the configure test. */
7905 (XRegisterIMInstantiateCallback_arg6) xim_inst);
7906 #else /* not HAVE_X11R6_XIM */
7907 xim_open_dpy (dpyinfo, resource_name);
7908 #endif /* not HAVE_X11R6_XIM */
7910 #endif /* HAVE_XIM */
7914 /* Close the connection to the XIM server on display DPYINFO. */
7916 static void
7917 xim_close_dpy (struct x_display_info *dpyinfo)
7919 #ifdef HAVE_XIM
7920 if (use_xim)
7922 #ifdef HAVE_X11R6_XIM
7923 if (dpyinfo->display)
7924 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
7925 NULL, emacs_class,
7926 xim_instantiate_callback, NULL);
7927 xfree (dpyinfo->xim_callback_data->resource_name);
7928 xfree (dpyinfo->xim_callback_data);
7929 #endif /* HAVE_X11R6_XIM */
7930 if (dpyinfo->display)
7931 XCloseIM (dpyinfo->xim);
7932 dpyinfo->xim = NULL;
7933 XFree (dpyinfo->xim_styles);
7935 #endif /* HAVE_XIM */
7938 #endif /* not HAVE_X11R6_XIM */
7942 /* Calculate the absolute position in frame F
7943 from its current recorded position values and gravity. */
7945 static void
7946 x_calc_absolute_position (struct frame *f)
7948 int flags = f->size_hint_flags;
7950 /* We have nothing to do if the current position
7951 is already for the top-left corner. */
7952 if (! ((flags & XNegative) || (flags & YNegative)))
7953 return;
7955 /* Treat negative positions as relative to the leftmost bottommost
7956 position that fits on the screen. */
7957 if (flags & XNegative)
7958 f->left_pos = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
7959 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
7962 int height = FRAME_PIXEL_HEIGHT (f);
7964 #if defined USE_X_TOOLKIT && defined USE_MOTIF
7965 /* Something is fishy here. When using Motif, starting Emacs with
7966 `-g -0-0', the frame appears too low by a few pixels.
7968 This seems to be so because initially, while Emacs is starting,
7969 the column widget's height and the frame's pixel height are
7970 different. The column widget's height is the right one. In
7971 later invocations, when Emacs is up, the frame's pixel height
7972 is right, though.
7974 It's not obvious where the initial small difference comes from.
7975 2000-12-01, gerd. */
7977 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
7978 #endif
7980 if (flags & YNegative)
7981 f->top_pos = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
7982 - height + f->top_pos;
7985 /* The left_pos and top_pos
7986 are now relative to the top and left screen edges,
7987 so the flags should correspond. */
7988 f->size_hint_flags &= ~ (XNegative | YNegative);
7991 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
7992 to really change the position, and 0 when calling from
7993 x_make_frame_visible (in that case, XOFF and YOFF are the current
7994 position values). It is -1 when calling from x_set_frame_parameters,
7995 which means, do adjust for borders but don't change the gravity. */
7997 void
7998 x_set_offset (struct frame *f, register int xoff, register int yoff, int change_gravity)
8000 int modified_top, modified_left;
8002 if (change_gravity > 0)
8004 f->top_pos = yoff;
8005 f->left_pos = xoff;
8006 f->size_hint_flags &= ~ (XNegative | YNegative);
8007 if (xoff < 0)
8008 f->size_hint_flags |= XNegative;
8009 if (yoff < 0)
8010 f->size_hint_flags |= YNegative;
8011 f->win_gravity = NorthWestGravity;
8013 x_calc_absolute_position (f);
8015 block_input ();
8016 x_wm_set_size_hint (f, (long) 0, 0);
8018 modified_left = f->left_pos;
8019 modified_top = f->top_pos;
8021 if (change_gravity != 0 && FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8023 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8024 than the WM decorations. So we use the calculated offset instead
8025 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8026 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8027 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8030 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8031 modified_left, modified_top);
8033 x_sync_with_move (f, f->left_pos, f->top_pos,
8034 FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8035 ? 1 : 0);
8037 /* change_gravity is non-zero when this function is called from Lisp to
8038 programmatically move a frame. In that case, we call
8039 x_check_expected_move to discover if we have a "Type A" or "Type B"
8040 window manager, and, for a "Type A" window manager, adjust the position
8041 of the frame.
8043 We call x_check_expected_move if a programmatic move occurred, and
8044 either the window manager type (A/B) is unknown or it is Type A but we
8045 need to compute the top/left offset adjustment for this frame. */
8047 if (change_gravity != 0 &&
8048 (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8049 || (FRAME_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8050 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8051 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8052 x_check_expected_move (f, modified_left, modified_top);
8054 unblock_input ();
8057 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8058 on the root window for frame F contains ATOMNAME.
8059 This is how a WM check shall be done according to the Window Manager
8060 Specification/Extended Window Manager Hints at
8061 http://freedesktop.org/wiki/Specifications/wm-spec. */
8063 static int
8064 wm_supports (struct frame *f, Atom want_atom)
8066 Atom actual_type;
8067 unsigned long actual_size, bytes_remaining;
8068 int i, rc, actual_format;
8069 Window wmcheck_window;
8070 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8071 Window target_window = dpyinfo->root_window;
8072 long max_len = 65536;
8073 Display *dpy = FRAME_X_DISPLAY (f);
8074 unsigned char *tmp_data = NULL;
8075 Atom target_type = XA_WINDOW;
8077 block_input ();
8079 x_catch_errors (dpy);
8080 rc = XGetWindowProperty (dpy, target_window,
8081 dpyinfo->Xatom_net_supporting_wm_check,
8082 0, max_len, False, target_type,
8083 &actual_type, &actual_format, &actual_size,
8084 &bytes_remaining, &tmp_data);
8086 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8088 if (tmp_data) XFree (tmp_data);
8089 x_uncatch_errors ();
8090 unblock_input ();
8091 return 0;
8094 wmcheck_window = *(Window *) tmp_data;
8095 XFree (tmp_data);
8097 /* Check if window exists. */
8098 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8099 x_sync (f);
8100 if (x_had_errors_p (dpy))
8102 x_uncatch_errors ();
8103 unblock_input ();
8104 return 0;
8107 if (dpyinfo->net_supported_window != wmcheck_window)
8109 /* Window changed, reload atoms */
8110 if (dpyinfo->net_supported_atoms != NULL)
8111 XFree (dpyinfo->net_supported_atoms);
8112 dpyinfo->net_supported_atoms = NULL;
8113 dpyinfo->nr_net_supported_atoms = 0;
8114 dpyinfo->net_supported_window = 0;
8116 target_type = XA_ATOM;
8117 tmp_data = NULL;
8118 rc = XGetWindowProperty (dpy, target_window,
8119 dpyinfo->Xatom_net_supported,
8120 0, max_len, False, target_type,
8121 &actual_type, &actual_format, &actual_size,
8122 &bytes_remaining, &tmp_data);
8124 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
8126 if (tmp_data) XFree (tmp_data);
8127 x_uncatch_errors ();
8128 unblock_input ();
8129 return 0;
8132 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8133 dpyinfo->nr_net_supported_atoms = actual_size;
8134 dpyinfo->net_supported_window = wmcheck_window;
8137 rc = 0;
8139 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
8140 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8142 x_uncatch_errors ();
8143 unblock_input ();
8145 return rc;
8148 static void
8149 set_wm_state (Lisp_Object frame, int add, Atom atom, Atom value)
8151 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (XFRAME (frame));
8153 x_send_client_event (frame, make_number (0), frame,
8154 dpyinfo->Xatom_net_wm_state,
8155 make_number (32),
8156 /* 1 = add, 0 = remove */
8157 Fcons
8158 (make_number (add ? 1 : 0),
8159 Fcons
8160 (make_fixnum_or_float (atom),
8161 (value != 0
8162 ? list1 (make_fixnum_or_float (value))
8163 : Qnil))));
8166 void
8167 x_set_sticky (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
8169 Lisp_Object frame;
8170 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8172 XSETFRAME (frame, f);
8174 set_wm_state (frame, NILP (new_value) ? 0 : 1,
8175 dpyinfo->Xatom_net_wm_state_sticky, None);
8178 /* Return the current _NET_WM_STATE.
8179 SIZE_STATE is set to one of the FULLSCREEN_* values.
8180 STICKY is set to 1 if the sticky state is set, 0 if not.
8182 Return non-zero if we are not hidden, zero if we are. */
8184 static int
8185 get_current_wm_state (struct frame *f,
8186 Window window,
8187 int *size_state,
8188 int *sticky)
8190 Atom actual_type;
8191 unsigned long actual_size, bytes_remaining;
8192 int i, rc, actual_format, is_hidden = 0;
8193 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8194 long max_len = 65536;
8195 Display *dpy = FRAME_X_DISPLAY (f);
8196 unsigned char *tmp_data = NULL;
8197 Atom target_type = XA_ATOM;
8199 *sticky = 0;
8200 *size_state = FULLSCREEN_NONE;
8202 block_input ();
8203 x_catch_errors (dpy);
8204 rc = XGetWindowProperty (dpy, window, dpyinfo->Xatom_net_wm_state,
8205 0, max_len, False, target_type,
8206 &actual_type, &actual_format, &actual_size,
8207 &bytes_remaining, &tmp_data);
8209 if (rc != Success || actual_type != target_type || x_had_errors_p (dpy))
8211 if (tmp_data) XFree (tmp_data);
8212 x_uncatch_errors ();
8213 unblock_input ();
8214 return !FRAME_ICONIFIED_P (f);
8217 x_uncatch_errors ();
8219 for (i = 0; i < actual_size; ++i)
8221 Atom a = ((Atom*)tmp_data)[i];
8222 if (a == dpyinfo->Xatom_net_wm_state_hidden)
8224 is_hidden = 1;
8225 f->output_data.x->net_wm_state_hidden_seen = 1;
8227 else if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
8229 if (*size_state == FULLSCREEN_HEIGHT)
8230 *size_state = FULLSCREEN_MAXIMIZED;
8231 else
8232 *size_state = FULLSCREEN_WIDTH;
8234 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
8236 if (*size_state == FULLSCREEN_WIDTH)
8237 *size_state = FULLSCREEN_MAXIMIZED;
8238 else
8239 *size_state = FULLSCREEN_HEIGHT;
8241 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen)
8242 *size_state = FULLSCREEN_BOTH;
8243 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
8244 *sticky = 1;
8247 if (tmp_data) XFree (tmp_data);
8248 unblock_input ();
8249 return ! is_hidden;
8252 /* Do fullscreen as specified in extended window manager hints */
8254 static int
8255 do_ewmh_fullscreen (struct frame *f)
8257 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8258 int have_net_atom = wm_supports (f, dpyinfo->Xatom_net_wm_state);
8259 int cur, dummy;
8261 (void)get_current_wm_state (f, FRAME_OUTER_WINDOW (f), &cur, &dummy);
8263 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8264 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8265 if (!have_net_atom)
8266 have_net_atom = wm_supports (f, dpyinfo->Xatom_net_wm_state_fullscreen);
8268 if (have_net_atom && cur != f->want_fullscreen)
8270 Lisp_Object frame;
8272 XSETFRAME (frame, f);
8274 /* Keep number of calls to set_wm_state as low as possible.
8275 Some window managers, or possible Gtk+, hangs when too many
8276 are sent at once. */
8277 switch (f->want_fullscreen)
8279 case FULLSCREEN_BOTH:
8280 if (cur == FULLSCREEN_WIDTH || cur == FULLSCREEN_MAXIMIZED
8281 || cur == FULLSCREEN_HEIGHT)
8282 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_maximized_horz,
8283 dpyinfo->Xatom_net_wm_state_maximized_vert);
8284 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_fullscreen, None);
8285 break;
8286 case FULLSCREEN_WIDTH:
8287 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_HEIGHT
8288 || cur == FULLSCREEN_MAXIMIZED)
8289 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen,
8290 dpyinfo->Xatom_net_wm_state_maximized_vert);
8291 if (cur != FULLSCREEN_MAXIMIZED)
8292 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_maximized_horz, None);
8293 break;
8294 case FULLSCREEN_HEIGHT:
8295 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_WIDTH
8296 || cur == FULLSCREEN_MAXIMIZED)
8297 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen,
8298 dpyinfo->Xatom_net_wm_state_maximized_horz);
8299 if (cur != FULLSCREEN_MAXIMIZED)
8300 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_maximized_vert, None);
8301 break;
8302 case FULLSCREEN_MAXIMIZED:
8303 if (cur == FULLSCREEN_BOTH)
8304 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen, None);
8305 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_maximized_horz,
8306 dpyinfo->Xatom_net_wm_state_maximized_vert);
8307 break;
8308 case FULLSCREEN_NONE:
8309 if (cur == FULLSCREEN_BOTH)
8310 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen, None);
8311 else
8312 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_maximized_horz,
8313 dpyinfo->Xatom_net_wm_state_maximized_vert);
8316 f->want_fullscreen = FULLSCREEN_NONE;
8320 return have_net_atom;
8323 static void
8324 XTfullscreen_hook (struct frame *f)
8326 if (FRAME_VISIBLE_P (f))
8328 block_input ();
8329 x_check_fullscreen (f);
8330 x_sync (f);
8331 unblock_input ();
8336 static int
8337 x_handle_net_wm_state (struct frame *f, const XPropertyEvent *event)
8339 int value = FULLSCREEN_NONE;
8340 Lisp_Object lval;
8341 int sticky = 0;
8342 int not_hidden = get_current_wm_state (f, event->window, &value, &sticky);
8344 lval = Qnil;
8345 switch (value)
8347 case FULLSCREEN_WIDTH:
8348 lval = Qfullwidth;
8349 break;
8350 case FULLSCREEN_HEIGHT:
8351 lval = Qfullheight;
8352 break;
8353 case FULLSCREEN_BOTH:
8354 lval = Qfullboth;
8355 break;
8356 case FULLSCREEN_MAXIMIZED:
8357 lval = Qmaximized;
8358 break;
8361 store_frame_param (f, Qfullscreen, lval);
8362 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
8364 return not_hidden;
8367 /* Check if we need to resize the frame due to a fullscreen request.
8368 If so needed, resize the frame. */
8369 static void
8370 x_check_fullscreen (struct frame *f)
8372 if (do_ewmh_fullscreen (f))
8373 return;
8375 if (f->output_data.x->parent_desc != FRAME_DISPLAY_INFO (f)->root_window)
8376 return; /* Only fullscreen without WM or with EWM hints (above). */
8378 /* Setting fullscreen to nil doesn't do anything. We could save the
8379 last non-fullscreen size and restore it, but it seems like a
8380 lot of work for this unusual case (no window manager running). */
8382 if (f->want_fullscreen != FULLSCREEN_NONE)
8384 int width = FRAME_PIXEL_WIDTH (f), height = FRAME_PIXEL_HEIGHT (f);
8385 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8387 switch (f->want_fullscreen)
8389 /* No difference between these two when there is no WM */
8390 case FULLSCREEN_BOTH:
8391 case FULLSCREEN_MAXIMIZED:
8392 width = x_display_pixel_width (dpyinfo);
8393 height = x_display_pixel_height (dpyinfo);
8394 break;
8395 case FULLSCREEN_WIDTH:
8396 width = x_display_pixel_width (dpyinfo);
8397 break;
8398 case FULLSCREEN_HEIGHT:
8399 height = x_display_pixel_height (dpyinfo);
8402 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8403 width, height);
8407 /* This function is called by x_set_offset to determine whether the window
8408 manager interfered with the positioning of the frame. Type A window
8409 managers position the surrounding window manager decorations a small
8410 amount above and left of the user-supplied position. Type B window
8411 managers position the surrounding window manager decorations at the
8412 user-specified position. If we detect a Type A window manager, we
8413 compensate by moving the window right and down by the proper amount. */
8415 static void
8416 x_check_expected_move (struct frame *f, int expected_left, int expected_top)
8418 int current_left = 0, current_top = 0;
8420 /* x_real_positions returns the left and top offsets of the outermost
8421 window manager window around the frame. */
8423 x_real_positions (f, &current_left, &current_top);
8425 if (current_left != expected_left || current_top != expected_top)
8427 /* It's a "Type A" window manager. */
8429 int adjusted_left;
8430 int adjusted_top;
8432 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8433 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
8434 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
8436 /* Now fix the mispositioned frame's location. */
8438 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
8439 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
8441 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8442 adjusted_left, adjusted_top);
8444 x_sync_with_move (f, expected_left, expected_top, 0);
8446 else
8447 /* It's a "Type B" window manager. We don't have to adjust the
8448 frame's position. */
8450 FRAME_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8454 /* Wait for XGetGeometry to return up-to-date position information for a
8455 recently-moved frame. Call this immediately after calling XMoveWindow.
8456 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
8457 frame has been moved to, so we use a fuzzy position comparison instead
8458 of an exact comparison. */
8460 static void
8461 x_sync_with_move (struct frame *f, int left, int top, int fuzzy)
8463 int count = 0;
8465 while (count++ < 50)
8467 int current_left = 0, current_top = 0;
8469 /* In theory, this call to XSync only needs to happen once, but in
8470 practice, it doesn't seem to work, hence the need for the surrounding
8471 loop. */
8473 XSync (FRAME_X_DISPLAY (f), False);
8474 x_real_positions (f, &current_left, &current_top);
8476 if (fuzzy)
8478 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
8479 pixels. */
8481 if (eabs (current_left - left) <= 10
8482 && eabs (current_top - top) <= 40)
8483 return;
8485 else if (current_left == left && current_top == top)
8486 return;
8489 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
8490 will then return up-to-date position info. */
8492 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
8496 /* Wait for an event on frame F matching EVENTTYPE. */
8497 void
8498 x_wait_for_event (struct frame *f, int eventtype)
8500 int level = interrupt_input_blocked;
8502 fd_set fds;
8503 struct timespec tmo, tmo_at, time_now;
8504 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
8506 f->wait_event_type = eventtype;
8508 /* Set timeout to 0.1 second. Hopefully not noticeable.
8509 Maybe it should be configurable. */
8510 tmo = make_timespec (0, 100 * 1000 * 1000);
8511 tmo_at = timespec_add (current_timespec (), tmo);
8513 while (f->wait_event_type)
8515 pending_signals = 1;
8516 totally_unblock_input ();
8517 /* XTread_socket is called after unblock. */
8518 block_input ();
8519 interrupt_input_blocked = level;
8521 FD_ZERO (&fds);
8522 FD_SET (fd, &fds);
8524 time_now = current_timespec ();
8525 if (timespec_cmp (tmo_at, time_now) < 0)
8526 break;
8528 tmo = timespec_sub (tmo_at, time_now);
8529 if (pselect (fd + 1, &fds, NULL, NULL, &tmo, NULL) == 0)
8530 break; /* Timeout */
8533 f->wait_event_type = 0;
8537 /* Change the size of frame F's X window to COLS/ROWS in the case F
8538 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8539 top-left-corner window gravity for this size change and subsequent
8540 size changes. Otherwise we leave the window gravity unchanged. */
8542 static void
8543 x_set_window_size_1 (struct frame *f, int change_gravity, int width, int height, bool pixelwise)
8545 int pixelwidth, pixelheight;
8547 check_frame_size (f, &width, &height, pixelwise);
8548 f->scroll_bar_actual_width
8549 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8551 : FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f));
8553 compute_fringe_widths (f, 0);
8555 pixelwidth =
8556 (pixelwise ? width : FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, width))
8557 + FRAME_TOOLBAR_WIDTH (f);
8558 pixelheight =
8559 (pixelwise ? height : FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, height))
8560 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
8562 if (change_gravity) f->win_gravity = NorthWestGravity;
8563 x_wm_set_size_hint (f, (long) 0, 0);
8564 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8565 pixelwidth, pixelheight);
8568 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8569 receive in the ConfigureNotify event; if we get what we asked
8570 for, then the event won't cause the screen to become garbaged, so
8571 we have to make sure to do it here. */
8572 SET_FRAME_GARBAGED (f);
8574 /* Now, strictly speaking, we can't be sure that this is accurate,
8575 but the window manager will get around to dealing with the size
8576 change request eventually, and we'll hear how it went when the
8577 ConfigureNotify event gets here.
8579 We could just not bother storing any of this information here,
8580 and let the ConfigureNotify event set everything up, but that
8581 might be kind of confusing to the Lisp code, since size changes
8582 wouldn't be reported in the frame parameters until some random
8583 point in the future when the ConfigureNotify event arrives.
8585 We pass 1 for DELAY since we can't run Lisp code inside of
8586 a BLOCK_INPUT. */
8588 /* But the ConfigureNotify may in fact never arrive, and then this is
8589 not right if the frame is visible. Instead wait (with timeout)
8590 for the ConfigureNotify. */
8591 if (FRAME_VISIBLE_P (f))
8592 x_wait_for_event (f, ConfigureNotify);
8593 else
8595 change_frame_size (f, width, height, 0, 1, 0, 1);
8596 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8597 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8598 x_sync (f);
8603 /* Call this to change the size of frame F's x-window.
8604 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8605 for this size change and subsequent size changes.
8606 Otherwise we leave the window gravity unchanged. */
8608 void
8609 x_set_window_size (struct frame *f, int change_gravity, int width, int height, bool pixelwise)
8611 block_input ();
8613 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8615 int text_width, text_height;
8617 /* When the frame is maximized/fullscreen or running under for
8618 example Xmonad, x_set_window_size_1 will be a no-op.
8619 In that case, the right thing to do is extend rows/width to
8620 the current frame size. We do that first if x_set_window_size_1
8621 turns out to not be a no-op (there is no way to know).
8622 The size will be adjusted again if the frame gets a
8623 ConfigureNotify event as a result of x_set_window_size. */
8624 int pixelh = FRAME_PIXEL_HEIGHT (f);
8625 #ifdef USE_X_TOOLKIT
8626 /* The menu bar is not part of text lines. The tool bar
8627 is however. */
8628 pixelh -= FRAME_MENUBAR_HEIGHT (f);
8629 #endif
8630 text_width = FRAME_PIXEL_TO_TEXT_WIDTH (f, FRAME_PIXEL_WIDTH (f));
8631 text_height = FRAME_PIXEL_TO_TEXT_HEIGHT (f, pixelh);
8632 /* Update f->scroll_bar_actual_width because it is used in
8633 FRAME_PIXEL_WIDTH_TO_TEXT_COLS. */
8634 f->scroll_bar_actual_width
8635 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
8636 change_frame_size (f, text_width, text_height, 0, 1, 0, 1);
8639 #ifdef USE_GTK
8640 if (FRAME_GTK_WIDGET (f))
8641 xg_frame_set_char_size (f, width, height);
8642 else
8643 x_set_window_size_1 (f, change_gravity, width, height, pixelwise);
8644 #else /* not USE_GTK */
8646 x_set_window_size_1 (f, change_gravity, width, height, pixelwise);
8648 #endif /* not USE_GTK */
8650 /* If cursor was outside the new size, mark it as off. */
8651 mark_window_cursors_off (XWINDOW (f->root_window));
8653 /* Clear out any recollection of where the mouse highlighting was,
8654 since it might be in a place that's outside the new frame size.
8655 Actually checking whether it is outside is a pain in the neck,
8656 so don't try--just let the highlighting be done afresh with new size. */
8657 cancel_mouse_face (f);
8659 unblock_input ();
8662 /* Mouse warping. */
8664 void
8665 x_set_mouse_position (struct frame *f, int x, int y)
8667 int pix_x, pix_y;
8669 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
8670 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
8672 if (pix_x < 0) pix_x = 0;
8673 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
8675 if (pix_y < 0) pix_y = 0;
8676 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
8678 block_input ();
8680 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8681 0, 0, 0, 0, pix_x, pix_y);
8682 unblock_input ();
8685 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
8687 void
8688 x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
8690 block_input ();
8692 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8693 0, 0, 0, 0, pix_x, pix_y);
8694 unblock_input ();
8697 /* Raise frame F. */
8699 void
8700 x_raise_frame (struct frame *f)
8702 block_input ();
8703 if (FRAME_VISIBLE_P (f))
8704 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8705 XFlush (FRAME_X_DISPLAY (f));
8706 unblock_input ();
8709 /* Lower frame F. */
8711 static void
8712 x_lower_frame (struct frame *f)
8714 if (FRAME_VISIBLE_P (f))
8716 block_input ();
8717 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8718 XFlush (FRAME_X_DISPLAY (f));
8719 unblock_input ();
8723 /* Request focus with XEmbed */
8725 void
8726 xembed_request_focus (struct frame *f)
8728 /* See XEmbed Protocol Specification at
8729 http://freedesktop.org/wiki/Specifications/xembed-spec */
8730 if (FRAME_VISIBLE_P (f))
8731 xembed_send_message (f, CurrentTime,
8732 XEMBED_REQUEST_FOCUS, 0, 0, 0);
8735 /* Activate frame with Extended Window Manager Hints */
8737 void
8738 x_ewmh_activate_frame (struct frame *f)
8740 /* See Window Manager Specification/Extended Window Manager Hints at
8741 http://freedesktop.org/wiki/Specifications/wm-spec */
8743 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8745 if (FRAME_VISIBLE_P (f) && wm_supports (f, dpyinfo->Xatom_net_active_window))
8747 Lisp_Object frame;
8748 XSETFRAME (frame, f);
8749 x_send_client_event (frame, make_number (0), frame,
8750 dpyinfo->Xatom_net_active_window,
8751 make_number (32),
8752 list2i (1, dpyinfo->last_user_time));
8756 static void
8757 XTframe_raise_lower (struct frame *f, int raise_flag)
8759 if (raise_flag)
8760 x_raise_frame (f);
8761 else
8762 x_lower_frame (f);
8765 /* XEmbed implementation. */
8767 #if defined USE_X_TOOLKIT || ! defined USE_GTK
8769 /* XEmbed implementation. */
8771 #define XEMBED_VERSION 0
8773 static void
8774 xembed_set_info (struct frame *f, enum xembed_info flags)
8776 unsigned long data[2];
8777 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
8779 data[0] = XEMBED_VERSION;
8780 data[1] = flags;
8782 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8783 dpyinfo->Xatom_XEMBED_INFO, dpyinfo->Xatom_XEMBED_INFO,
8784 32, PropModeReplace, (unsigned char *) data, 2);
8786 #endif /* defined USE_X_TOOLKIT || ! defined USE_GTK */
8788 static void
8789 xembed_send_message (struct frame *f, Time t, enum xembed_message msg,
8790 long int detail, long int data1, long int data2)
8792 XEvent event;
8794 event.xclient.type = ClientMessage;
8795 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
8796 event.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_XEMBED;
8797 event.xclient.format = 32;
8798 event.xclient.data.l[0] = t;
8799 event.xclient.data.l[1] = msg;
8800 event.xclient.data.l[2] = detail;
8801 event.xclient.data.l[3] = data1;
8802 event.xclient.data.l[4] = data2;
8804 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
8805 False, NoEventMask, &event);
8806 XSync (FRAME_X_DISPLAY (f), False);
8809 /* Change of visibility. */
8811 /* This tries to wait until the frame is really visible.
8812 However, if the window manager asks the user where to position
8813 the frame, this will return before the user finishes doing that.
8814 The frame will not actually be visible at that time,
8815 but it will become visible later when the window manager
8816 finishes with it. */
8818 void
8819 x_make_frame_visible (struct frame *f)
8821 int original_top, original_left;
8822 int retry_count = 2;
8824 retry:
8826 block_input ();
8828 x_set_bitmap_icon (f);
8830 if (! FRAME_VISIBLE_P (f))
8832 /* We test FRAME_GARBAGED_P here to make sure we don't
8833 call x_set_offset a second time
8834 if we get to x_make_frame_visible a second time
8835 before the window gets really visible. */
8836 if (! FRAME_ICONIFIED_P (f)
8837 && ! FRAME_X_EMBEDDED_P (f)
8838 && ! f->output_data.x->asked_for_visible)
8839 x_set_offset (f, f->left_pos, f->top_pos, 0);
8841 f->output_data.x->asked_for_visible = 1;
8843 if (! EQ (Vx_no_window_manager, Qt))
8844 x_wm_set_window_state (f, NormalState);
8845 #ifdef USE_X_TOOLKIT
8846 if (FRAME_X_EMBEDDED_P (f))
8847 xembed_set_info (f, XEMBED_MAPPED);
8848 else
8850 /* This was XtPopup, but that did nothing for an iconified frame. */
8851 XtMapWidget (f->output_data.x->widget);
8853 #else /* not USE_X_TOOLKIT */
8854 #ifdef USE_GTK
8855 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
8856 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
8857 #else
8858 if (FRAME_X_EMBEDDED_P (f))
8859 xembed_set_info (f, XEMBED_MAPPED);
8860 else
8861 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8862 #endif /* not USE_GTK */
8863 #endif /* not USE_X_TOOLKIT */
8866 XFlush (FRAME_X_DISPLAY (f));
8868 /* Synchronize to ensure Emacs knows the frame is visible
8869 before we do anything else. We do this loop with input not blocked
8870 so that incoming events are handled. */
8872 Lisp_Object frame;
8873 int count;
8874 /* This must be before UNBLOCK_INPUT
8875 since events that arrive in response to the actions above
8876 will set it when they are handled. */
8877 int previously_visible = f->output_data.x->has_been_visible;
8879 original_left = f->left_pos;
8880 original_top = f->top_pos;
8882 /* This must come after we set COUNT. */
8883 unblock_input ();
8885 /* We unblock here so that arriving X events are processed. */
8887 /* Now move the window back to where it was "supposed to be".
8888 But don't do it if the gravity is negative.
8889 When the gravity is negative, this uses a position
8890 that is 3 pixels too low. Perhaps that's really the border width.
8892 Don't do this if the window has never been visible before,
8893 because the window manager may choose the position
8894 and we don't want to override it. */
8896 if (! FRAME_VISIBLE_P (f)
8897 && ! FRAME_ICONIFIED_P (f)
8898 && ! FRAME_X_EMBEDDED_P (f)
8899 && f->win_gravity == NorthWestGravity
8900 && previously_visible)
8902 Drawable rootw;
8903 int x, y;
8904 unsigned int width, height, border, depth;
8906 block_input ();
8908 /* On some window managers (such as FVWM) moving an existing
8909 window, even to the same place, causes the window manager
8910 to introduce an offset. This can cause the window to move
8911 to an unexpected location. Check the geometry (a little
8912 slow here) and then verify that the window is in the right
8913 place. If the window is not in the right place, move it
8914 there, and take the potential window manager hit. */
8915 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8916 &rootw, &x, &y, &width, &height, &border, &depth);
8918 if (original_left != x || original_top != y)
8919 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8920 original_left, original_top);
8922 unblock_input ();
8925 XSETFRAME (frame, f);
8927 /* Wait until the frame is visible. Process X events until a
8928 MapNotify event has been seen, or until we think we won't get a
8929 MapNotify at all.. */
8930 for (count = input_signal_count + 10;
8931 input_signal_count < count && !FRAME_VISIBLE_P (f);)
8933 /* Force processing of queued events. */
8934 x_sync (f);
8936 /* Machines that do polling rather than SIGIO have been
8937 observed to go into a busy-wait here. So we'll fake an
8938 alarm signal to let the handler know that there's something
8939 to be read. We used to raise a real alarm, but it seems
8940 that the handler isn't always enabled here. This is
8941 probably a bug. */
8942 if (input_polling_used ())
8944 /* It could be confusing if a real alarm arrives while
8945 processing the fake one. Turn it off and let the
8946 handler reset it. */
8947 int old_poll_suppress_count = poll_suppress_count;
8948 poll_suppress_count = 1;
8949 poll_for_input_1 ();
8950 poll_suppress_count = old_poll_suppress_count;
8954 /* 2000-09-28: In
8956 (let ((f (selected-frame)))
8957 (iconify-frame f)
8958 (raise-frame f))
8960 the frame is not raised with various window managers on
8961 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
8962 unknown reason, the call to XtMapWidget is completely ignored.
8963 Mapping the widget a second time works. */
8965 if (!FRAME_VISIBLE_P (f) && --retry_count != 0)
8966 goto retry;
8970 /* Change from mapped state to withdrawn state. */
8972 /* Make the frame visible (mapped and not iconified). */
8974 void
8975 x_make_frame_invisible (struct frame *f)
8977 Window window;
8979 /* Use the frame's outermost window, not the one we normally draw on. */
8980 window = FRAME_OUTER_WINDOW (f);
8982 /* Don't keep the highlight on an invisible frame. */
8983 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
8984 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
8986 block_input ();
8988 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
8989 that the current position of the window is user-specified, rather than
8990 program-specified, so that when the window is mapped again, it will be
8991 placed at the same location, without forcing the user to position it
8992 by hand again (they have already done that once for this window.) */
8993 x_wm_set_size_hint (f, (long) 0, 1);
8995 #ifdef USE_GTK
8996 if (FRAME_GTK_OUTER_WIDGET (f))
8997 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
8998 else
8999 #else
9000 if (FRAME_X_EMBEDDED_P (f))
9001 xembed_set_info (f, 0);
9002 else
9003 #endif
9006 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9007 DefaultScreen (FRAME_X_DISPLAY (f))))
9009 unblock_input ();
9010 error ("Can't notify window manager of window withdrawal");
9014 /* We can't distinguish this from iconification
9015 just by the event that we get from the server.
9016 So we can't win using the usual strategy of letting
9017 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9018 and synchronize with the server to make sure we agree. */
9019 SET_FRAME_VISIBLE (f, 0);
9020 SET_FRAME_ICONIFIED (f, 0);
9022 x_sync (f);
9024 unblock_input ();
9027 /* Change window state from mapped to iconified. */
9029 void
9030 x_iconify_frame (struct frame *f)
9032 #ifdef USE_X_TOOLKIT
9033 int result;
9034 #endif
9036 /* Don't keep the highlight on an invisible frame. */
9037 if (FRAME_DISPLAY_INFO (f)->x_highlight_frame == f)
9038 FRAME_DISPLAY_INFO (f)->x_highlight_frame = 0;
9040 if (FRAME_ICONIFIED_P (f))
9041 return;
9043 block_input ();
9045 x_set_bitmap_icon (f);
9047 #if defined (USE_GTK)
9048 if (FRAME_GTK_OUTER_WIDGET (f))
9050 if (! FRAME_VISIBLE_P (f))
9051 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9053 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9054 SET_FRAME_VISIBLE (f, 0);
9055 SET_FRAME_ICONIFIED (f, 1);
9056 unblock_input ();
9057 return;
9059 #endif
9061 #ifdef USE_X_TOOLKIT
9063 if (! FRAME_VISIBLE_P (f))
9065 if (! EQ (Vx_no_window_manager, Qt))
9066 x_wm_set_window_state (f, IconicState);
9067 /* This was XtPopup, but that did nothing for an iconified frame. */
9068 XtMapWidget (f->output_data.x->widget);
9069 /* The server won't give us any event to indicate
9070 that an invisible frame was changed to an icon,
9071 so we have to record it here. */
9072 SET_FRAME_VISIBLE (f, 0);
9073 SET_FRAME_ICONIFIED (f, 1);
9074 unblock_input ();
9075 return;
9078 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9079 XtWindow (f->output_data.x->widget),
9080 DefaultScreen (FRAME_X_DISPLAY (f)));
9081 unblock_input ();
9083 if (!result)
9084 error ("Can't notify window manager of iconification");
9086 SET_FRAME_ICONIFIED (f, 1);
9087 SET_FRAME_VISIBLE (f, 0);
9089 block_input ();
9090 XFlush (FRAME_X_DISPLAY (f));
9091 unblock_input ();
9092 #else /* not USE_X_TOOLKIT */
9094 /* Make sure the X server knows where the window should be positioned,
9095 in case the user deiconifies with the window manager. */
9096 if (! FRAME_VISIBLE_P (f)
9097 && ! FRAME_ICONIFIED_P (f)
9098 && ! FRAME_X_EMBEDDED_P (f))
9099 x_set_offset (f, f->left_pos, f->top_pos, 0);
9101 /* Since we don't know which revision of X we're running, we'll use both
9102 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9104 /* X11R4: send a ClientMessage to the window manager using the
9105 WM_CHANGE_STATE type. */
9107 XEvent msg;
9109 msg.xclient.window = FRAME_X_WINDOW (f);
9110 msg.xclient.type = ClientMessage;
9111 msg.xclient.message_type = FRAME_DISPLAY_INFO (f)->Xatom_wm_change_state;
9112 msg.xclient.format = 32;
9113 msg.xclient.data.l[0] = IconicState;
9115 if (! XSendEvent (FRAME_X_DISPLAY (f),
9116 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9117 False,
9118 SubstructureRedirectMask | SubstructureNotifyMask,
9119 &msg))
9121 unblock_input ();
9122 error ("Can't notify window manager of iconification");
9126 /* X11R3: set the initial_state field of the window manager hints to
9127 IconicState. */
9128 x_wm_set_window_state (f, IconicState);
9130 if (!FRAME_VISIBLE_P (f))
9132 /* If the frame was withdrawn, before, we must map it. */
9133 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9136 SET_FRAME_ICONIFIED (f, 1);
9137 SET_FRAME_VISIBLE (f, 0);
9139 XFlush (FRAME_X_DISPLAY (f));
9140 unblock_input ();
9141 #endif /* not USE_X_TOOLKIT */
9145 /* Free X resources of frame F. */
9147 void
9148 x_free_frame_resources (struct frame *f)
9150 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9151 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
9152 #ifdef USE_X_TOOLKIT
9153 Lisp_Object bar;
9154 struct scroll_bar *b;
9155 #endif
9157 block_input ();
9159 /* If a display connection is dead, don't try sending more
9160 commands to the X server. */
9161 if (dpyinfo->display)
9163 /* We must free faces before destroying windows because some
9164 font-driver (e.g. xft) access a window while finishing a
9165 face. */
9166 if (FRAME_FACE_CACHE (f))
9167 free_frame_faces (f);
9169 if (f->output_data.x->icon_desc)
9170 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9172 #ifdef USE_X_TOOLKIT
9173 /* Explicitly destroy the scroll bars of the frame. Without
9174 this, we get "BadDrawable" errors from the toolkit later on,
9175 presumably from expose events generated for the disappearing
9176 toolkit scroll bars. */
9177 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9179 b = XSCROLL_BAR (bar);
9180 x_scroll_bar_remove (b);
9182 #endif
9184 #ifdef HAVE_X_I18N
9185 if (FRAME_XIC (f))
9186 free_frame_xic (f);
9187 #endif
9189 #ifdef USE_X_TOOLKIT
9190 if (f->output_data.x->widget)
9192 XtDestroyWidget (f->output_data.x->widget);
9193 f->output_data.x->widget = NULL;
9195 /* Tooltips don't have widgets, only a simple X window, even if
9196 we are using a toolkit. */
9197 else if (FRAME_X_WINDOW (f))
9198 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9200 free_frame_menubar (f);
9201 #else /* !USE_X_TOOLKIT */
9203 #ifdef USE_GTK
9204 xg_free_frame_widgets (f);
9205 #endif /* USE_GTK */
9207 if (FRAME_X_WINDOW (f))
9208 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9209 #endif /* !USE_X_TOOLKIT */
9211 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
9212 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
9213 unload_color (f, f->output_data.x->cursor_pixel);
9214 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9215 unload_color (f, f->output_data.x->border_pixel);
9216 unload_color (f, f->output_data.x->mouse_pixel);
9218 if (f->output_data.x->scroll_bar_background_pixel != -1)
9219 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9220 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9221 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9222 #ifdef USE_TOOLKIT_SCROLL_BARS
9223 /* Scrollbar shadow colors. */
9224 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9225 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9226 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9227 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9228 #endif /* USE_TOOLKIT_SCROLL_BARS */
9229 if (f->output_data.x->white_relief.pixel != -1)
9230 unload_color (f, f->output_data.x->white_relief.pixel);
9231 if (f->output_data.x->black_relief.pixel != -1)
9232 unload_color (f, f->output_data.x->black_relief.pixel);
9234 x_free_gcs (f);
9236 /* Free extra GCs allocated by x_setup_relief_colors. */
9237 if (f->output_data.x->white_relief.gc)
9239 XFreeGC (dpyinfo->display, f->output_data.x->white_relief.gc);
9240 f->output_data.x->white_relief.gc = 0;
9242 if (f->output_data.x->black_relief.gc)
9244 XFreeGC (dpyinfo->display, f->output_data.x->black_relief.gc);
9245 f->output_data.x->black_relief.gc = 0;
9248 XFlush (FRAME_X_DISPLAY (f));
9251 xfree (f->output_data.x->saved_menu_event);
9252 xfree (f->output_data.x);
9253 f->output_data.x = NULL;
9255 if (f == dpyinfo->x_focus_frame)
9256 dpyinfo->x_focus_frame = 0;
9257 if (f == dpyinfo->x_focus_event_frame)
9258 dpyinfo->x_focus_event_frame = 0;
9259 if (f == dpyinfo->x_highlight_frame)
9260 dpyinfo->x_highlight_frame = 0;
9261 if (f == hlinfo->mouse_face_mouse_frame)
9262 reset_mouse_highlight (hlinfo);
9264 unblock_input ();
9268 /* Destroy the X window of frame F. */
9270 static void
9271 x_destroy_window (struct frame *f)
9273 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
9275 /* If a display connection is dead, don't try sending more
9276 commands to the X server. */
9277 if (dpyinfo->display != 0)
9278 x_free_frame_resources (f);
9280 dpyinfo->reference_count--;
9284 /* Setting window manager hints. */
9286 /* Set the normal size hints for the window manager, for frame F.
9287 FLAGS is the flags word to use--or 0 meaning preserve the flags
9288 that the window now has.
9289 If USER_POSITION, set the USPosition
9290 flag (this is useful when FLAGS is 0).
9291 The GTK version is in gtkutils.c. */
9293 #ifndef USE_GTK
9294 void
9295 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
9297 XSizeHints size_hints;
9298 Window window = FRAME_OUTER_WINDOW (f);
9300 #ifdef USE_X_TOOLKIT
9301 if (f->output_data.x->widget)
9303 widget_update_wm_size_hints (f->output_data.x->widget);
9304 return;
9306 #endif
9308 /* Setting PMaxSize caused various problems. */
9309 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9311 size_hints.x = f->left_pos;
9312 size_hints.y = f->top_pos;
9314 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9315 size_hints.width = FRAME_PIXEL_WIDTH (f);
9317 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9318 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9319 size_hints.max_width = x_display_pixel_width (FRAME_DISPLAY_INFO (f))
9320 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9321 size_hints.max_height = x_display_pixel_height (FRAME_DISPLAY_INFO (f))
9322 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9324 /* Calculate the base and minimum sizes. */
9326 int base_width, base_height;
9327 int min_rows = 0, min_cols = 0;
9329 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9330 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9332 check_frame_size (f, &min_cols, &min_rows, 0);
9334 /* The window manager uses the base width hints to calculate the
9335 current number of rows and columns in the frame while
9336 resizing; min_width and min_height aren't useful for this
9337 purpose, since they might not give the dimensions for a
9338 zero-row, zero-column frame.
9340 We use the base_width and base_height members if we have
9341 them; otherwise, we set the min_width and min_height members
9342 to the size for a zero x zero frame. */
9344 size_hints.flags |= PBaseSize;
9345 size_hints.base_width = base_width;
9346 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
9347 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9348 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9351 /* If we don't need the old flags, we don't need the old hint at all. */
9352 if (flags)
9354 size_hints.flags |= flags;
9355 goto no_read;
9359 XSizeHints hints; /* Sometimes I hate X Windows... */
9360 long supplied_return;
9361 int value;
9363 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9364 &supplied_return);
9366 if (flags)
9367 size_hints.flags |= flags;
9368 else
9370 if (value == 0)
9371 hints.flags = 0;
9372 if (hints.flags & PSize)
9373 size_hints.flags |= PSize;
9374 if (hints.flags & PPosition)
9375 size_hints.flags |= PPosition;
9376 if (hints.flags & USPosition)
9377 size_hints.flags |= USPosition;
9378 if (hints.flags & USSize)
9379 size_hints.flags |= USSize;
9383 no_read:
9385 #ifdef PWinGravity
9386 size_hints.win_gravity = f->win_gravity;
9387 size_hints.flags |= PWinGravity;
9389 if (user_position)
9391 size_hints.flags &= ~ PPosition;
9392 size_hints.flags |= USPosition;
9394 #endif /* PWinGravity */
9396 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9398 #endif /* not USE_GTK */
9400 /* Used for IconicState or NormalState */
9402 static void
9403 x_wm_set_window_state (struct frame *f, int state)
9405 #ifdef USE_X_TOOLKIT
9406 Arg al[1];
9408 XtSetArg (al[0], XtNinitialState, state);
9409 XtSetValues (f->output_data.x->widget, al, 1);
9410 #else /* not USE_X_TOOLKIT */
9411 Window window = FRAME_X_WINDOW (f);
9413 f->output_data.x->wm_hints.flags |= StateHint;
9414 f->output_data.x->wm_hints.initial_state = state;
9416 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9417 #endif /* not USE_X_TOOLKIT */
9420 static void
9421 x_wm_set_icon_pixmap (struct frame *f, ptrdiff_t pixmap_id)
9423 Pixmap icon_pixmap, icon_mask;
9425 #if !defined USE_X_TOOLKIT && !defined USE_GTK
9426 Window window = FRAME_OUTER_WINDOW (f);
9427 #endif
9429 if (pixmap_id > 0)
9431 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9432 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9433 icon_mask = x_bitmap_mask (f, pixmap_id);
9434 f->output_data.x->wm_hints.icon_mask = icon_mask;
9436 else
9438 /* It seems there is no way to turn off use of an icon
9439 pixmap. */
9440 return;
9444 #ifdef USE_GTK
9446 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9447 return;
9450 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
9453 Arg al[1];
9454 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9455 XtSetValues (f->output_data.x->widget, al, 1);
9456 XtSetArg (al[0], XtNiconMask, icon_mask);
9457 XtSetValues (f->output_data.x->widget, al, 1);
9460 #else /* not USE_X_TOOLKIT && not USE_GTK */
9462 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9463 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9465 #endif /* not USE_X_TOOLKIT && not USE_GTK */
9468 void
9469 x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
9471 Window window = FRAME_OUTER_WINDOW (f);
9473 f->output_data.x->wm_hints.flags |= IconPositionHint;
9474 f->output_data.x->wm_hints.icon_x = icon_x;
9475 f->output_data.x->wm_hints.icon_y = icon_y;
9477 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9481 /***********************************************************************
9482 Fonts
9483 ***********************************************************************/
9485 #ifdef GLYPH_DEBUG
9487 /* Check that FONT is valid on frame F. It is if it can be found in F's
9488 font table. */
9490 static void
9491 x_check_font (struct frame *f, struct font *font)
9493 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
9494 if (font->driver->check)
9495 eassert (font->driver->check (f, font) == 0);
9498 #endif /* GLYPH_DEBUG */
9501 /***********************************************************************
9502 Initialization
9503 ***********************************************************************/
9505 #ifdef USE_X_TOOLKIT
9506 static XrmOptionDescRec emacs_options[] = {
9507 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
9508 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
9510 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
9511 XrmoptionSepArg, NULL},
9512 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
9514 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9515 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9516 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9517 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9518 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9519 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
9520 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
9523 /* Whether atimer for Xt timeouts is activated or not. */
9525 static int x_timeout_atimer_activated_flag;
9527 #endif /* USE_X_TOOLKIT */
9529 static int x_initialized;
9531 /* Test whether two display-name strings agree up to the dot that separates
9532 the screen number from the server number. */
9533 static int
9534 same_x_server (const char *name1, const char *name2)
9536 int seen_colon = 0;
9537 const char *system_name = SSDATA (Vsystem_name);
9538 ptrdiff_t system_name_length = SBYTES (Vsystem_name);
9539 ptrdiff_t length_until_period = 0;
9541 while (system_name[length_until_period] != 0
9542 && system_name[length_until_period] != '.')
9543 length_until_period++;
9545 /* Treat `unix' like an empty host name. */
9546 if (! strncmp (name1, "unix:", 5))
9547 name1 += 4;
9548 if (! strncmp (name2, "unix:", 5))
9549 name2 += 4;
9550 /* Treat this host's name like an empty host name. */
9551 if (! strncmp (name1, system_name, system_name_length)
9552 && name1[system_name_length] == ':')
9553 name1 += system_name_length;
9554 if (! strncmp (name2, system_name, system_name_length)
9555 && name2[system_name_length] == ':')
9556 name2 += system_name_length;
9557 /* Treat this host's domainless name like an empty host name. */
9558 if (! strncmp (name1, system_name, length_until_period)
9559 && name1[length_until_period] == ':')
9560 name1 += length_until_period;
9561 if (! strncmp (name2, system_name, length_until_period)
9562 && name2[length_until_period] == ':')
9563 name2 += length_until_period;
9565 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
9567 if (*name1 == ':')
9568 seen_colon = 1;
9569 if (seen_colon && *name1 == '.')
9570 return 1;
9572 return (seen_colon
9573 && (*name1 == '.' || *name1 == '\0')
9574 && (*name2 == '.' || *name2 == '\0'));
9577 /* Count number of set bits in mask and number of bits to shift to
9578 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
9579 to 5. */
9580 static void
9581 get_bits_and_offset (unsigned long mask, int *bits, int *offset)
9583 int nr = 0;
9584 int off = 0;
9586 while (!(mask & 1))
9588 off++;
9589 mask >>= 1;
9592 while (mask & 1)
9594 nr++;
9595 mask >>= 1;
9598 *offset = off;
9599 *bits = nr;
9602 /* Return 1 if display DISPLAY is available for use, 0 otherwise.
9603 But don't permanently open it, just test its availability. */
9605 bool
9606 x_display_ok (const char *display)
9608 Display *dpy = XOpenDisplay (display);
9609 return dpy ? (XCloseDisplay (dpy), 1) : 0;
9612 #ifdef USE_GTK
9613 static void
9614 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
9615 const gchar *msg, gpointer user_data)
9617 if (!strstr (msg, "g_set_prgname"))
9618 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, msg);
9620 #endif
9622 /* Open a connection to X display DISPLAY_NAME, and return
9623 the structure that describes the open display.
9624 If we cannot contact the display, return null. */
9626 struct x_display_info *
9627 x_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
9629 int connection;
9630 Display *dpy;
9631 struct terminal *terminal;
9632 struct x_display_info *dpyinfo;
9633 XrmDatabase xrdb;
9634 ptrdiff_t lim;
9636 block_input ();
9638 if (!x_initialized)
9640 x_initialize ();
9641 ++x_initialized;
9644 if (! x_display_ok (SSDATA (display_name)))
9645 error ("Display %s can't be opened", SSDATA (display_name));
9647 #ifdef USE_GTK
9649 #define NUM_ARGV 10
9650 int argc;
9651 char *argv[NUM_ARGV];
9652 char **argv2 = argv;
9653 guint id;
9655 if (x_initialized++ > 1)
9657 xg_display_open (SSDATA (display_name), &dpy);
9659 else
9661 static char display_opt[] = "--display";
9662 static char name_opt[] = "--name";
9664 for (argc = 0; argc < NUM_ARGV; ++argc)
9665 argv[argc] = 0;
9667 argc = 0;
9668 argv[argc++] = initial_argv[0];
9670 if (! NILP (display_name))
9672 argv[argc++] = display_opt;
9673 argv[argc++] = SSDATA (display_name);
9676 argv[argc++] = name_opt;
9677 argv[argc++] = resource_name;
9679 XSetLocaleModifiers ("");
9681 /* Emacs can only handle core input events, so make sure
9682 Gtk doesn't use Xinput or Xinput2 extensions. */
9683 xputenv ("GDK_CORE_DEVICE_EVENTS=1");
9685 /* Work around GLib bug that outputs a faulty warning. See
9686 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
9687 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
9688 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
9690 /* NULL window -> events for all windows go to our function.
9691 Call before gtk_init so Gtk+ event filters comes after our. */
9692 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
9694 /* gtk_init does set_locale. Fix locale before and after. */
9695 fixup_locale ();
9696 gtk_init (&argc, &argv2);
9697 fixup_locale ();
9699 g_log_remove_handler ("GLib", id);
9701 xg_initialize ();
9703 dpy = DEFAULT_GDK_DISPLAY ();
9705 #if ! GTK_CHECK_VERSION (2, 90, 0)
9706 /* Load our own gtkrc if it exists. */
9708 const char *file = "~/.emacs.d/gtkrc";
9709 Lisp_Object s, abs_file;
9711 s = build_string (file);
9712 abs_file = Fexpand_file_name (s, Qnil);
9714 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
9715 gtk_rc_parse (SSDATA (abs_file));
9717 #endif
9719 XSetErrorHandler (x_error_handler);
9720 XSetIOErrorHandler (x_io_error_quitter);
9723 #else /* not USE_GTK */
9724 #ifdef USE_X_TOOLKIT
9725 /* weiner@footloose.sps.mot.com reports that this causes
9726 errors with X11R5:
9727 X protocol error: BadAtom (invalid Atom parameter)
9728 on protocol request 18skiloaf.
9729 So let's not use it until R6. */
9730 #ifdef HAVE_X11XTR6
9731 XtSetLanguageProc (NULL, NULL, NULL);
9732 #endif
9735 int argc = 0;
9736 char *argv[3];
9738 argv[0] = "";
9739 argc = 1;
9740 if (xrm_option)
9742 argv[argc++] = "-xrm";
9743 argv[argc++] = xrm_option;
9745 turn_on_atimers (0);
9746 dpy = XtOpenDisplay (Xt_app_con, SSDATA (display_name),
9747 resource_name, EMACS_CLASS,
9748 emacs_options, XtNumber (emacs_options),
9749 &argc, argv);
9750 turn_on_atimers (1);
9752 #ifdef HAVE_X11XTR6
9753 /* I think this is to compensate for XtSetLanguageProc. */
9754 fixup_locale ();
9755 #endif
9758 #else /* not USE_X_TOOLKIT */
9759 XSetLocaleModifiers ("");
9760 dpy = XOpenDisplay (SSDATA (display_name));
9761 #endif /* not USE_X_TOOLKIT */
9762 #endif /* not USE_GTK*/
9764 /* Detect failure. */
9765 if (dpy == 0)
9767 unblock_input ();
9768 return 0;
9771 /* We have definitely succeeded. Record the new connection. */
9773 dpyinfo = xzalloc (sizeof *dpyinfo);
9774 terminal = x_create_terminal (dpyinfo);
9777 struct x_display_info *share;
9779 for (share = x_display_list; share; share = share->next)
9780 if (same_x_server (SSDATA (XCAR (share->name_list_element)),
9781 SSDATA (display_name)))
9782 break;
9783 if (share)
9784 terminal->kboard = share->terminal->kboard;
9785 else
9787 terminal->kboard = allocate_kboard (Qx);
9789 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
9791 char *vendor = ServerVendor (dpy);
9793 /* Protect terminal from GC before removing it from the
9794 list of terminals. */
9795 struct gcpro gcpro1;
9796 Lisp_Object gcpro_term;
9797 XSETTERMINAL (gcpro_term, terminal);
9798 GCPRO1 (gcpro_term);
9800 /* Temporarily hide the partially initialized terminal. */
9801 terminal_list = terminal->next_terminal;
9802 unblock_input ();
9803 kset_system_key_alist
9804 (terminal->kboard,
9805 call1 (Qvendor_specific_keysyms,
9806 vendor ? build_string (vendor) : empty_unibyte_string));
9807 block_input ();
9808 terminal->next_terminal = terminal_list;
9809 terminal_list = terminal;
9810 UNGCPRO;
9813 /* Don't let the initial kboard remain current longer than necessary.
9814 That would cause problems if a file loaded on startup tries to
9815 prompt in the mini-buffer. */
9816 if (current_kboard == initial_kboard)
9817 current_kboard = terminal->kboard;
9819 terminal->kboard->reference_count++;
9822 /* Put this display on the chain. */
9823 dpyinfo->next = x_display_list;
9824 x_display_list = dpyinfo;
9826 dpyinfo->name_list_element = Fcons (display_name, Qnil);
9827 dpyinfo->display = dpy;
9829 /* Set the name of the terminal. */
9830 terminal->name = xlispstrdup (display_name);
9832 #if 0
9833 XSetAfterFunction (x_current_display, x_trace_wire);
9834 #endif /* ! 0 */
9836 lim = min (PTRDIFF_MAX, SIZE_MAX) - sizeof "@";
9837 if (lim - SBYTES (Vinvocation_name) < SBYTES (Vsystem_name))
9838 memory_full (SIZE_MAX);
9839 dpyinfo->x_id_name = xmalloc (SBYTES (Vinvocation_name)
9840 + SBYTES (Vsystem_name) + 2);
9841 strcat (strcat (strcpy (dpyinfo->x_id_name, SSDATA (Vinvocation_name)), "@"),
9842 SSDATA (Vsystem_name));
9844 /* Figure out which modifier bits mean what. */
9845 x_find_modifier_meanings (dpyinfo);
9847 /* Get the scroll bar cursor. */
9848 #ifdef USE_GTK
9849 /* We must create a GTK cursor, it is required for GTK widgets. */
9850 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
9851 #endif /* USE_GTK */
9853 dpyinfo->vertical_scroll_bar_cursor
9854 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
9856 xrdb = x_load_resources (dpyinfo->display, xrm_option,
9857 resource_name, EMACS_CLASS);
9858 #ifdef HAVE_XRMSETDATABASE
9859 XrmSetDatabase (dpyinfo->display, xrdb);
9860 #else
9861 dpyinfo->display->db = xrdb;
9862 #endif
9863 /* Put the rdb where we can find it in a way that works on
9864 all versions. */
9865 dpyinfo->xrdb = xrdb;
9867 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
9868 DefaultScreen (dpyinfo->display));
9869 select_visual (dpyinfo);
9870 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
9871 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
9872 dpyinfo->icon_bitmap_id = -1;
9873 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
9875 reset_mouse_highlight (&dpyinfo->mouse_highlight);
9877 /* See if we can construct pixel values from RGB values. */
9878 if (dpyinfo->visual->class == TrueColor)
9880 get_bits_and_offset (dpyinfo->visual->red_mask,
9881 &dpyinfo->red_bits, &dpyinfo->red_offset);
9882 get_bits_and_offset (dpyinfo->visual->blue_mask,
9883 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
9884 get_bits_and_offset (dpyinfo->visual->green_mask,
9885 &dpyinfo->green_bits, &dpyinfo->green_offset);
9888 /* See if a private colormap is requested. */
9889 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
9891 if (dpyinfo->visual->class == PseudoColor)
9893 Lisp_Object value;
9894 value = display_x_get_resource (dpyinfo,
9895 build_string ("privateColormap"),
9896 build_string ("PrivateColormap"),
9897 Qnil, Qnil);
9898 if (STRINGP (value)
9899 && (!strcmp (SSDATA (value), "true")
9900 || !strcmp (SSDATA (value), "on")))
9901 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
9904 else
9905 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
9906 dpyinfo->visual, AllocNone);
9908 #ifdef HAVE_XFT
9910 /* If we are using Xft, check dpi value in X resources.
9911 It is better we use it as well, since Xft will use it, as will all
9912 Gnome applications. If our real DPI is smaller or larger than the
9913 one Xft uses, our font will look smaller or larger than other
9914 for other applications, even if it is the same font name (monospace-10
9915 for example). */
9916 char *v = XGetDefault (dpyinfo->display, "Xft", "dpi");
9917 double d;
9918 if (v != NULL && sscanf (v, "%lf", &d) == 1)
9919 dpyinfo->resy = dpyinfo->resx = d;
9921 #endif
9923 if (dpyinfo->resy < 1)
9925 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
9926 double pixels = DisplayHeight (dpyinfo->display, screen_number);
9927 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
9928 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
9929 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
9930 pixels = DisplayWidth (dpyinfo->display, screen_number);
9931 mm = DisplayWidthMM (dpyinfo->display, screen_number);
9932 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
9933 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
9937 static const struct
9939 const char *name;
9940 int offset;
9941 } atom_refs[] = {
9942 #define ATOM_REFS_INIT(string, member) \
9943 { string, offsetof (struct x_display_info, member) },
9944 ATOM_REFS_INIT ("WM_PROTOCOLS", Xatom_wm_protocols)
9945 ATOM_REFS_INIT ("WM_TAKE_FOCUS", Xatom_wm_take_focus)
9946 ATOM_REFS_INIT ("WM_SAVE_YOURSELF", Xatom_wm_save_yourself)
9947 ATOM_REFS_INIT ("WM_DELETE_WINDOW", Xatom_wm_delete_window)
9948 ATOM_REFS_INIT ("WM_CHANGE_STATE", Xatom_wm_change_state)
9949 ATOM_REFS_INIT ("WM_CONFIGURE_DENIED", Xatom_wm_configure_denied)
9950 ATOM_REFS_INIT ("WM_MOVED", Xatom_wm_window_moved)
9951 ATOM_REFS_INIT ("WM_CLIENT_LEADER", Xatom_wm_client_leader)
9952 ATOM_REFS_INIT ("Editres", Xatom_editres)
9953 ATOM_REFS_INIT ("CLIPBOARD", Xatom_CLIPBOARD)
9954 ATOM_REFS_INIT ("TIMESTAMP", Xatom_TIMESTAMP)
9955 ATOM_REFS_INIT ("TEXT", Xatom_TEXT)
9956 ATOM_REFS_INIT ("COMPOUND_TEXT", Xatom_COMPOUND_TEXT)
9957 ATOM_REFS_INIT ("UTF8_STRING", Xatom_UTF8_STRING)
9958 ATOM_REFS_INIT ("DELETE", Xatom_DELETE)
9959 ATOM_REFS_INIT ("MULTIPLE", Xatom_MULTIPLE)
9960 ATOM_REFS_INIT ("INCR", Xatom_INCR)
9961 ATOM_REFS_INIT ("_EMACS_TMP_", Xatom_EMACS_TMP)
9962 ATOM_REFS_INIT ("TARGETS", Xatom_TARGETS)
9963 ATOM_REFS_INIT ("NULL", Xatom_NULL)
9964 ATOM_REFS_INIT ("ATOM", Xatom_ATOM)
9965 ATOM_REFS_INIT ("ATOM_PAIR", Xatom_ATOM_PAIR)
9966 ATOM_REFS_INIT ("CLIPBOARD_MANAGER", Xatom_CLIPBOARD_MANAGER)
9967 ATOM_REFS_INIT ("_XEMBED_INFO", Xatom_XEMBED_INFO)
9968 /* For properties of font. */
9969 ATOM_REFS_INIT ("PIXEL_SIZE", Xatom_PIXEL_SIZE)
9970 ATOM_REFS_INIT ("AVERAGE_WIDTH", Xatom_AVERAGE_WIDTH)
9971 ATOM_REFS_INIT ("_MULE_BASELINE_OFFSET", Xatom_MULE_BASELINE_OFFSET)
9972 ATOM_REFS_INIT ("_MULE_RELATIVE_COMPOSE", Xatom_MULE_RELATIVE_COMPOSE)
9973 ATOM_REFS_INIT ("_MULE_DEFAULT_ASCENT", Xatom_MULE_DEFAULT_ASCENT)
9974 /* Ghostscript support. */
9975 ATOM_REFS_INIT ("DONE", Xatom_DONE)
9976 ATOM_REFS_INIT ("PAGE", Xatom_PAGE)
9977 ATOM_REFS_INIT ("SCROLLBAR", Xatom_Scrollbar)
9978 ATOM_REFS_INIT ("_XEMBED", Xatom_XEMBED)
9979 /* EWMH */
9980 ATOM_REFS_INIT ("_NET_WM_STATE", Xatom_net_wm_state)
9981 ATOM_REFS_INIT ("_NET_WM_STATE_FULLSCREEN", Xatom_net_wm_state_fullscreen)
9982 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_HORZ",
9983 Xatom_net_wm_state_maximized_horz)
9984 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_VERT",
9985 Xatom_net_wm_state_maximized_vert)
9986 ATOM_REFS_INIT ("_NET_WM_STATE_STICKY", Xatom_net_wm_state_sticky)
9987 ATOM_REFS_INIT ("_NET_WM_STATE_HIDDEN", Xatom_net_wm_state_hidden)
9988 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE", Xatom_net_window_type)
9989 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE_TOOLTIP",
9990 Xatom_net_window_type_tooltip)
9991 ATOM_REFS_INIT ("_NET_WM_ICON_NAME", Xatom_net_wm_icon_name)
9992 ATOM_REFS_INIT ("_NET_WM_NAME", Xatom_net_wm_name)
9993 ATOM_REFS_INIT ("_NET_SUPPORTED", Xatom_net_supported)
9994 ATOM_REFS_INIT ("_NET_SUPPORTING_WM_CHECK", Xatom_net_supporting_wm_check)
9995 ATOM_REFS_INIT ("_NET_WM_WINDOW_OPACITY", Xatom_net_wm_window_opacity)
9996 ATOM_REFS_INIT ("_NET_ACTIVE_WINDOW", Xatom_net_active_window)
9997 ATOM_REFS_INIT ("_NET_FRAME_EXTENTS", Xatom_net_frame_extents)
9998 ATOM_REFS_INIT ("_NET_CURRENT_DESKTOP", Xatom_net_current_desktop)
9999 ATOM_REFS_INIT ("_NET_WORKAREA", Xatom_net_workarea)
10000 /* Session management */
10001 ATOM_REFS_INIT ("SM_CLIENT_ID", Xatom_SM_CLIENT_ID)
10002 ATOM_REFS_INIT ("_XSETTINGS_SETTINGS", Xatom_xsettings_prop)
10003 ATOM_REFS_INIT ("MANAGER", Xatom_xsettings_mgr)
10006 int i;
10007 const int atom_count = sizeof (atom_refs) / sizeof (atom_refs[0]);
10008 /* 1 for _XSETTINGS_SN */
10009 const int total_atom_count = 1 + atom_count;
10010 Atom *atoms_return = xmalloc (total_atom_count * sizeof *atoms_return);
10011 char **atom_names = xmalloc (total_atom_count * sizeof *atom_names);
10012 static char const xsettings_fmt[] = "_XSETTINGS_S%d";
10013 char xsettings_atom_name[sizeof xsettings_fmt - 2
10014 + INT_STRLEN_BOUND (int)];
10016 for (i = 0; i < atom_count; i++)
10017 atom_names[i] = (char *) atom_refs[i].name;
10019 /* Build _XSETTINGS_SN atom name */
10020 sprintf (xsettings_atom_name, xsettings_fmt,
10021 XScreenNumberOfScreen (dpyinfo->screen));
10022 atom_names[i] = xsettings_atom_name;
10024 XInternAtoms (dpyinfo->display, atom_names, total_atom_count,
10025 False, atoms_return);
10027 for (i = 0; i < atom_count; i++)
10028 *(Atom *) ((char *) dpyinfo + atom_refs[i].offset) = atoms_return[i];
10030 /* Manual copy of last atom */
10031 dpyinfo->Xatom_xsettings_sel = atoms_return[i];
10033 xfree (atom_names);
10034 xfree (atoms_return);
10037 dpyinfo->x_dnd_atoms_size = 8;
10038 dpyinfo->x_dnd_atoms = xmalloc (sizeof *dpyinfo->x_dnd_atoms
10039 * dpyinfo->x_dnd_atoms_size);
10040 dpyinfo->gray
10041 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10042 gray_bits, gray_width, gray_height,
10043 1, 0, 1);
10045 #ifdef HAVE_X_I18N
10046 xim_initialize (dpyinfo, resource_name);
10047 #endif
10049 xsettings_initialize (dpyinfo);
10051 connection = ConnectionNumber (dpyinfo->display);
10053 /* This is only needed for distinguishing keyboard and process input. */
10054 if (connection != 0)
10055 add_keyboard_wait_descriptor (connection);
10057 #ifdef F_SETOWN
10058 fcntl (connection, F_SETOWN, getpid ());
10059 #endif /* ! defined (F_SETOWN) */
10061 if (interrupt_input)
10062 init_sigio (connection);
10064 #ifdef USE_LUCID
10066 XrmValue d, fr, to;
10067 Font font;
10069 dpy = dpyinfo->display;
10070 d.addr = (XPointer)&dpy;
10071 d.size = sizeof (Display *);
10072 fr.addr = XtDefaultFont;
10073 fr.size = sizeof (XtDefaultFont);
10074 to.size = sizeof (Font *);
10075 to.addr = (XPointer)&font;
10076 x_catch_errors (dpy);
10077 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10078 emacs_abort ();
10079 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10080 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10081 x_uncatch_errors ();
10083 #endif
10085 /* See if we should run in synchronous mode. This is useful
10086 for debugging X code. */
10088 Lisp_Object value;
10089 value = display_x_get_resource (dpyinfo,
10090 build_string ("synchronous"),
10091 build_string ("Synchronous"),
10092 Qnil, Qnil);
10093 if (STRINGP (value)
10094 && (!strcmp (SSDATA (value), "true")
10095 || !strcmp (SSDATA (value), "on")))
10096 XSynchronize (dpyinfo->display, True);
10100 Lisp_Object value;
10101 value = display_x_get_resource (dpyinfo,
10102 build_string ("useXIM"),
10103 build_string ("UseXIM"),
10104 Qnil, Qnil);
10105 #ifdef USE_XIM
10106 if (STRINGP (value)
10107 && (!strcmp (SSDATA (value), "false")
10108 || !strcmp (SSDATA (value), "off")))
10109 use_xim = 0;
10110 #else
10111 if (STRINGP (value)
10112 && (!strcmp (SSDATA (value), "true")
10113 || !strcmp (SSDATA (value), "on")))
10114 use_xim = 1;
10115 #endif
10118 #ifdef HAVE_X_SM
10119 /* Only do this for the very first display in the Emacs session.
10120 Ignore X session management when Emacs was first started on a
10121 tty. */
10122 if (terminal->id == 1)
10123 x_session_initialize (dpyinfo);
10124 #endif
10126 unblock_input ();
10128 return dpyinfo;
10131 /* Get rid of display DPYINFO, deleting all frames on it,
10132 and without sending any more commands to the X server. */
10134 static void
10135 x_delete_display (struct x_display_info *dpyinfo)
10137 struct terminal *t;
10139 /* Close all frames and delete the generic struct terminal for this
10140 X display. */
10141 for (t = terminal_list; t; t = t->next_terminal)
10142 if (t->type == output_x_window && t->display_info.x == dpyinfo)
10144 #ifdef HAVE_X_SM
10145 /* Close X session management when we close its display. */
10146 if (t->id == 1 && x_session_have_connection ())
10147 x_session_close ();
10148 #endif
10149 delete_terminal (t);
10150 break;
10153 if (next_noop_dpyinfo == dpyinfo)
10154 next_noop_dpyinfo = dpyinfo->next;
10156 if (x_display_list == dpyinfo)
10157 x_display_list = dpyinfo->next;
10158 else
10160 struct x_display_info *tail;
10162 for (tail = x_display_list; tail; tail = tail->next)
10163 if (tail->next == dpyinfo)
10164 tail->next = tail->next->next;
10167 xfree (dpyinfo->x_id_name);
10168 xfree (dpyinfo->x_dnd_atoms);
10169 xfree (dpyinfo->color_cells);
10170 xfree (dpyinfo);
10173 #ifdef USE_X_TOOLKIT
10175 /* Atimer callback function for TIMER. Called every 0.1s to process
10176 Xt timeouts, if needed. We must avoid calling XtAppPending as
10177 much as possible because that function does an implicit XFlush
10178 that slows us down. */
10180 static void
10181 x_process_timeouts (struct atimer *timer)
10183 block_input ();
10184 x_timeout_atimer_activated_flag = 0;
10185 if (toolkit_scroll_bar_interaction || popup_activated ())
10187 while (XtAppPending (Xt_app_con) & XtIMTimer)
10188 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10189 /* Reactivate the atimer for next time. */
10190 x_activate_timeout_atimer ();
10192 unblock_input ();
10195 /* Install an asynchronous timer that processes Xt timeout events
10196 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
10197 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
10198 function whenever these variables are set. This is necessary
10199 because some widget sets use timeouts internally, for example the
10200 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
10201 processed, these widgets don't behave normally. */
10203 void
10204 x_activate_timeout_atimer (void)
10206 block_input ();
10207 if (!x_timeout_atimer_activated_flag)
10209 struct timespec interval = make_timespec (0, 100 * 1000 * 1000);
10210 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
10211 x_timeout_atimer_activated_flag = 1;
10213 unblock_input ();
10216 #endif /* USE_X_TOOLKIT */
10219 /* Set up use of X before we make the first connection. */
10221 static struct redisplay_interface x_redisplay_interface =
10223 x_frame_parm_handlers,
10224 x_produce_glyphs,
10225 x_write_glyphs,
10226 x_insert_glyphs,
10227 x_clear_end_of_line,
10228 x_scroll_run,
10229 x_after_update_window_line,
10230 x_update_window_begin,
10231 x_update_window_end,
10232 x_flush,
10233 x_clear_window_mouse_face,
10234 x_get_glyph_overhangs,
10235 x_fix_overlapping_area,
10236 x_draw_fringe_bitmap,
10237 0, /* define_fringe_bitmap */
10238 0, /* destroy_fringe_bitmap */
10239 x_compute_glyph_string_overhangs,
10240 x_draw_glyph_string,
10241 x_define_frame_cursor,
10242 x_clear_frame_area,
10243 x_draw_window_cursor,
10244 x_draw_vertical_window_border,
10245 x_draw_window_divider,
10246 x_shift_glyphs_for_insert
10250 /* This function is called when the last frame on a display is deleted. */
10251 void
10252 x_delete_terminal (struct terminal *terminal)
10254 struct x_display_info *dpyinfo = terminal->display_info.x;
10255 int connection = -1;
10257 /* Protect against recursive calls. delete_frame in
10258 delete_terminal calls us back when it deletes our last frame. */
10259 if (!terminal->name)
10260 return;
10262 block_input ();
10263 #ifdef HAVE_X_I18N
10264 /* We must close our connection to the XIM server before closing the
10265 X display. */
10266 if (dpyinfo->xim)
10267 xim_close_dpy (dpyinfo);
10268 #endif
10270 /* If called from x_connection_closed, the display may already be closed
10271 and dpyinfo->display was set to 0 to indicate that. */
10272 if (dpyinfo->display)
10274 connection = ConnectionNumber (dpyinfo->display);
10276 x_destroy_all_bitmaps (dpyinfo);
10277 XSetCloseDownMode (dpyinfo->display, DestroyAll);
10279 /* Whether or not XCloseDisplay destroys the associated resource
10280 database depends on the version of libX11. To avoid both
10281 crash and memory leak, we dissociate the database from the
10282 display and then destroy dpyinfo->xrdb ourselves.
10284 Unfortunately, the above strategy does not work in some
10285 situations due to a bug in newer versions of libX11: because
10286 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
10287 dpy->db is NULL, XCloseDisplay destroys the associated
10288 database whereas it has not been created by XGetDefault
10289 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
10290 don't destroy the database here in order to avoid the crash
10291 in the above situations for now, though that may cause memory
10292 leaks in other situations. */
10293 #if 0
10294 #ifdef HAVE_XRMSETDATABASE
10295 XrmSetDatabase (dpyinfo->display, NULL);
10296 #else
10297 dpyinfo->display->db = NULL;
10298 #endif
10299 /* We used to call XrmDestroyDatabase from x_delete_display, but
10300 some older versions of libX11 crash if we call it after
10301 closing all the displays. */
10302 XrmDestroyDatabase (dpyinfo->xrdb);
10303 #endif
10305 #ifdef USE_GTK
10306 xg_display_close (dpyinfo->display);
10307 #else
10308 #ifdef USE_X_TOOLKIT
10309 XtCloseDisplay (dpyinfo->display);
10310 #else
10311 XCloseDisplay (dpyinfo->display);
10312 #endif
10313 #endif /* ! USE_GTK */
10316 /* No more input on this descriptor. */
10317 if (connection != -1)
10318 delete_keyboard_wait_descriptor (connection);
10320 /* Mark as dead. */
10321 dpyinfo->display = NULL;
10322 x_delete_display (dpyinfo);
10323 unblock_input ();
10326 /* Create a struct terminal, initialize it with the X11 specific
10327 functions and make DISPLAY->TERMINAL point to it. */
10329 static struct terminal *
10330 x_create_terminal (struct x_display_info *dpyinfo)
10332 struct terminal *terminal;
10334 terminal = create_terminal ();
10336 terminal->type = output_x_window;
10337 terminal->display_info.x = dpyinfo;
10338 dpyinfo->terminal = terminal;
10340 /* kboard is initialized in x_term_init. */
10342 terminal->clear_frame_hook = x_clear_frame;
10343 terminal->ins_del_lines_hook = x_ins_del_lines;
10344 terminal->delete_glyphs_hook = x_delete_glyphs;
10345 terminal->ring_bell_hook = XTring_bell;
10346 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
10347 terminal->reset_terminal_modes_hook = NULL;
10348 terminal->set_terminal_modes_hook = NULL;
10349 terminal->update_begin_hook = x_update_begin;
10350 terminal->update_end_hook = x_update_end;
10351 terminal->set_terminal_window_hook = NULL;
10352 terminal->read_socket_hook = XTread_socket;
10353 terminal->frame_up_to_date_hook = XTframe_up_to_date;
10354 terminal->mouse_position_hook = XTmouse_position;
10355 terminal->frame_rehighlight_hook = XTframe_rehighlight;
10356 terminal->frame_raise_lower_hook = XTframe_raise_lower;
10357 terminal->fullscreen_hook = XTfullscreen_hook;
10358 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10359 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10360 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
10361 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
10363 terminal->delete_frame_hook = x_destroy_window;
10364 terminal->delete_terminal_hook = x_delete_terminal;
10366 terminal->rif = &x_redisplay_interface;
10368 return terminal;
10371 void
10372 x_initialize (void)
10374 baud_rate = 19200;
10376 x_noop_count = 0;
10377 last_tool_bar_item = -1;
10378 any_help_event_p = 0;
10379 ignore_next_mouse_click_timeout = 0;
10381 #ifdef USE_GTK
10382 current_count = -1;
10383 #endif
10385 /* Try to use interrupt input; if we can't, then start polling. */
10386 Fset_input_interrupt_mode (Qt);
10388 #ifdef USE_X_TOOLKIT
10389 XtToolkitInitialize ();
10391 Xt_app_con = XtCreateApplicationContext ();
10393 /* Register a converter from strings to pixels, which uses
10394 Emacs' color allocation infrastructure. */
10395 XtAppSetTypeConverter (Xt_app_con,
10396 XtRString, XtRPixel, cvt_string_to_pixel,
10397 cvt_string_to_pixel_args,
10398 XtNumber (cvt_string_to_pixel_args),
10399 XtCacheByDisplay, cvt_pixel_dtor);
10401 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10402 #endif
10404 #ifdef USE_TOOLKIT_SCROLL_BARS
10405 #ifndef USE_GTK
10406 xaw3d_arrow_scroll = False;
10407 xaw3d_pick_top = True;
10408 #endif
10409 #endif
10411 /* Note that there is no real way portable across R3/R4 to get the
10412 original error handler. */
10413 XSetErrorHandler (x_error_handler);
10414 XSetIOErrorHandler (x_io_error_quitter);
10418 void
10419 syms_of_xterm (void)
10421 x_error_message = NULL;
10423 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
10424 DEFSYM (Qlatin_1, "latin-1");
10426 #ifdef USE_GTK
10427 xg_default_icon_file = build_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
10428 staticpro (&xg_default_icon_file);
10430 DEFSYM (Qx_gtk_map_stock, "x-gtk-map-stock");
10431 #endif
10433 DEFVAR_BOOL ("x-use-underline-position-properties",
10434 x_use_underline_position_properties,
10435 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
10436 A value of nil means ignore them. If you encounter fonts with bogus
10437 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10438 to 4.1, set this to nil. You can also use `underline-minimum-offset'
10439 to override the font's UNDERLINE_POSITION for small font display
10440 sizes. */);
10441 x_use_underline_position_properties = 1;
10443 DEFVAR_BOOL ("x-underline-at-descent-line",
10444 x_underline_at_descent_line,
10445 doc: /* Non-nil means to draw the underline at the same place as the descent line.
10446 A value of nil means to draw the underline according to the value of the
10447 variable `x-use-underline-position-properties', which is usually at the
10448 baseline level. The default value is nil. */);
10449 x_underline_at_descent_line = 0;
10451 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
10452 x_mouse_click_focus_ignore_position,
10453 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
10454 This variable is only used when the window manager requires that you
10455 click on a frame to select it (give it focus). In that case, a value
10456 of nil, means that the selected window and cursor position changes to
10457 reflect the mouse click position, while a non-nil value means that the
10458 selected window or cursor position is preserved. */);
10459 x_mouse_click_focus_ignore_position = 0;
10461 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
10462 doc: /* Which toolkit scroll bars Emacs uses, if any.
10463 A value of nil means Emacs doesn't use toolkit scroll bars.
10464 With the X Window system, the value is a symbol describing the
10465 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
10466 With MS Windows or Nextstep, the value is t. */);
10467 #ifdef USE_TOOLKIT_SCROLL_BARS
10468 #ifdef USE_MOTIF
10469 Vx_toolkit_scroll_bars = intern_c_string ("motif");
10470 #elif defined HAVE_XAW3D
10471 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
10472 #elif USE_GTK
10473 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
10474 #else
10475 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
10476 #endif
10477 #else
10478 Vx_toolkit_scroll_bars = Qnil;
10479 #endif
10481 DEFSYM (Qmodifier_value, "modifier-value");
10482 DEFSYM (Qalt, "alt");
10483 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
10484 DEFSYM (Qhyper, "hyper");
10485 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
10486 DEFSYM (Qmeta, "meta");
10487 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
10488 DEFSYM (Qsuper, "super");
10489 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
10491 DEFVAR_LISP ("x-alt-keysym", Vx_alt_keysym,
10492 doc: /* Which keys Emacs uses for the alt modifier.
10493 This should be one of the symbols `alt', `hyper', `meta', `super'.
10494 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
10495 is nil, which is the same as `alt'. */);
10496 Vx_alt_keysym = Qnil;
10498 DEFVAR_LISP ("x-hyper-keysym", Vx_hyper_keysym,
10499 doc: /* Which keys Emacs uses for the hyper modifier.
10500 This should be one of the symbols `alt', `hyper', `meta', `super'.
10501 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
10502 default is nil, which is the same as `hyper'. */);
10503 Vx_hyper_keysym = Qnil;
10505 DEFVAR_LISP ("x-meta-keysym", Vx_meta_keysym,
10506 doc: /* Which keys Emacs uses for the meta modifier.
10507 This should be one of the symbols `alt', `hyper', `meta', `super'.
10508 For example, `meta' means use the Meta_L and Meta_R keysyms. The
10509 default is nil, which is the same as `meta'. */);
10510 Vx_meta_keysym = Qnil;
10512 DEFVAR_LISP ("x-super-keysym", Vx_super_keysym,
10513 doc: /* Which keys Emacs uses for the super modifier.
10514 This should be one of the symbols `alt', `hyper', `meta', `super'.
10515 For example, `super' means use the Super_L and Super_R keysyms. The
10516 default is nil, which is the same as `super'. */);
10517 Vx_super_keysym = Qnil;
10519 DEFVAR_LISP ("x-keysym-table", Vx_keysym_table,
10520 doc: /* Hash table of character codes indexed by X keysym codes. */);
10521 Vx_keysym_table = make_hash_table (hashtest_eql, make_number (900),
10522 make_float (DEFAULT_REHASH_SIZE),
10523 make_float (DEFAULT_REHASH_THRESHOLD),
10524 Qnil);