Merged from
[emacs.git] / src / xterm.c
blob86e780cc611e0c44851259bae3fd6d2e2571ebc5
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006 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 2, or (at your option)
10 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; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
23 /* Xt features made by Fred Pierresteguy. */
25 #include <config.h>
27 /* On 4.3 these lose if they come after xterm.h. */
28 /* Putting these at the beginning seems to be standard for other .c files. */
29 #include <signal.h>
31 #include <stdio.h>
33 #ifdef HAVE_X_WINDOWS
35 #include "lisp.h"
36 #include "blockinput.h"
38 /* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40 #include "syssignal.h"
42 /* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44 #include "xterm.h"
45 #include <X11/cursorfont.h>
47 /* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49 #ifndef makedev
50 #include <sys/types.h>
51 #endif /* makedev */
53 #ifdef BSD_SYSTEM
54 #include <sys/ioctl.h>
55 #endif /* ! defined (BSD_SYSTEM) */
57 #include "systime.h"
59 #ifndef INCLUDED_FCNTL
60 #include <fcntl.h>
61 #endif
62 #include <ctype.h>
63 #include <errno.h>
64 #include <setjmp.h>
65 #include <sys/stat.h>
66 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
67 /* #include <sys/param.h> */
69 #include "charset.h"
70 #include "coding.h"
71 #include "ccl.h"
72 #include "frame.h"
73 #include "dispextern.h"
74 #include "fontset.h"
75 #include "termhooks.h"
76 #include "termopts.h"
77 #include "termchar.h"
78 #include "gnu.h"
79 #include "disptab.h"
80 #include "buffer.h"
81 #include "window.h"
82 #include "keyboard.h"
83 #include "intervals.h"
84 #include "process.h"
85 #include "atimer.h"
86 #include "keymap.h"
88 #ifdef USE_X_TOOLKIT
89 #include <X11/Shell.h>
90 #endif
92 #ifdef HAVE_SYS_TIME_H
93 #include <sys/time.h>
94 #endif
95 #ifdef HAVE_UNISTD_H
96 #include <unistd.h>
97 #endif
99 #ifdef USE_GTK
100 #include "gtkutil.h"
101 #endif
103 #ifdef USE_LUCID
104 extern int xlwmenu_window_p P_ ((Widget w, Window window));
105 extern void xlwmenu_redisplay P_ ((Widget));
106 #endif
108 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
110 extern void free_frame_menubar P_ ((struct frame *));
111 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
112 int));
113 #endif
115 #ifdef USE_X_TOOLKIT
116 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
117 #define HACK_EDITRES
118 extern void _XEditResCheckMessages ();
119 #endif /* not NO_EDITRES */
121 /* Include toolkit specific headers for the scroll bar widget. */
123 #ifdef USE_TOOLKIT_SCROLL_BARS
124 #if defined USE_MOTIF
125 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
126 #include <Xm/ScrollBar.h>
127 #else /* !USE_MOTIF i.e. use Xaw */
129 #ifdef HAVE_XAW3D
130 #include <X11/Xaw3d/Simple.h>
131 #include <X11/Xaw3d/Scrollbar.h>
132 #include <X11/Xaw3d/ThreeD.h>
133 #else /* !HAVE_XAW3D */
134 #include <X11/Xaw/Simple.h>
135 #include <X11/Xaw/Scrollbar.h>
136 #endif /* !HAVE_XAW3D */
137 #ifndef XtNpickTop
138 #define XtNpickTop "pickTop"
139 #endif /* !XtNpickTop */
140 #endif /* !USE_MOTIF */
141 #endif /* USE_TOOLKIT_SCROLL_BARS */
143 #endif /* USE_X_TOOLKIT */
145 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
146 #define x_any_window_to_frame x_window_to_frame
147 #define x_top_window_to_frame x_window_to_frame
148 #endif
150 #ifdef USE_X_TOOLKIT
151 #include "widget.h"
152 #ifndef XtNinitialState
153 #define XtNinitialState "initialState"
154 #endif
155 #endif
157 #define abs(x) ((x) < 0 ? -(x) : (x))
159 /* Default to using XIM if available. */
160 #ifdef USE_XIM
161 int use_xim = 1;
162 #else
163 int use_xim = 0; /* configure --without-xim */
164 #endif
168 /* Non-nil means Emacs uses toolkit scroll bars. */
170 Lisp_Object Vx_toolkit_scroll_bars;
172 /* Non-zero means that a HELP_EVENT has been generated since Emacs
173 start. */
175 static int any_help_event_p;
177 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
178 static Lisp_Object last_window;
180 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
182 int x_use_underline_position_properties;
184 /* This is a chain of structures for all the X displays currently in
185 use. */
187 struct x_display_info *x_display_list;
189 /* This is a list of cons cells, each of the form (NAME
190 . FONT-LIST-CACHE), one for each element of x_display_list and in
191 the same order. NAME is the name of the frame. FONT-LIST-CACHE
192 records previous values returned by x-list-fonts. */
194 Lisp_Object x_display_name_list;
196 /* Frame being updated by update_frame. This is declared in term.c.
197 This is set by update_begin and looked at by all the XT functions.
198 It is zero while not inside an update. In that case, the XT
199 functions assume that `selected_frame' is the frame to apply to. */
201 extern struct frame *updating_frame;
203 /* This is a frame waiting to be auto-raised, within XTread_socket. */
205 struct frame *pending_autoraise_frame;
207 #ifdef USE_X_TOOLKIT
208 /* The application context for Xt use. */
209 XtAppContext Xt_app_con;
210 static String Xt_default_resources[] = {0};
211 #endif /* USE_X_TOOLKIT */
213 /* Non-zero means user is interacting with a toolkit scroll bar. */
215 static int toolkit_scroll_bar_interaction;
217 /* Non-zero means to not move point as a result of clicking on a
218 frame to focus it (when focus-follows-mouse is nil). */
220 int x_mouse_click_focus_ignore_position;
222 /* Non-zero timeout value means ignore next mouse click if it arrives
223 before that timeout elapses (i.e. as part of the same sequence of
224 events resulting from clicking on a frame to select it). */
226 static unsigned long ignore_next_mouse_click_timeout;
228 /* Mouse movement.
230 Formerly, we used PointerMotionHintMask (in standard_event_mask)
231 so that we would have to call XQueryPointer after each MotionNotify
232 event to ask for another such event. However, this made mouse tracking
233 slow, and there was a bug that made it eventually stop.
235 Simply asking for MotionNotify all the time seems to work better.
237 In order to avoid asking for motion events and then throwing most
238 of them away or busy-polling the server for mouse positions, we ask
239 the server for pointer motion hints. This means that we get only
240 one event per group of mouse movements. "Groups" are delimited by
241 other kinds of events (focus changes and button clicks, for
242 example), or by XQueryPointer calls; when one of these happens, we
243 get another MotionNotify event the next time the mouse moves. This
244 is at least as efficient as getting motion events when mouse
245 tracking is on, and I suspect only negligibly worse when tracking
246 is off. */
248 /* Where the mouse was last time we reported a mouse event. */
250 static XRectangle last_mouse_glyph;
251 static FRAME_PTR last_mouse_glyph_frame;
252 static Lisp_Object last_mouse_press_frame;
254 /* The scroll bar in which the last X motion event occurred.
256 If the last X motion event occurred in a scroll bar, we set this so
257 XTmouse_position can know whether to report a scroll bar motion or
258 an ordinary motion.
260 If the last X motion event didn't occur in a scroll bar, we set
261 this to Qnil, to tell XTmouse_position to return an ordinary motion
262 event. */
264 static Lisp_Object last_mouse_scroll_bar;
266 /* This is a hack. We would really prefer that XTmouse_position would
267 return the time associated with the position it returns, but there
268 doesn't seem to be any way to wrest the time-stamp from the server
269 along with the position query. So, we just keep track of the time
270 of the last movement we received, and return that in hopes that
271 it's somewhat accurate. */
273 static Time last_mouse_movement_time;
275 /* Incremented by XTread_socket whenever it really tries to read
276 events. */
278 #ifdef __STDC__
279 static int volatile input_signal_count;
280 #else
281 static int input_signal_count;
282 #endif
284 /* Used locally within XTread_socket. */
286 static int x_noop_count;
288 /* Initial values of argv and argc. */
290 extern char **initial_argv;
291 extern int initial_argc;
293 extern Lisp_Object Vcommand_line_args, Vsystem_name;
295 /* Tells if a window manager is present or not. */
297 extern Lisp_Object Vx_no_window_manager;
299 extern Lisp_Object Qeql;
301 extern int errno;
303 /* A mask of extra modifier bits to put into every keyboard char. */
305 extern EMACS_INT extra_keyboard_modifiers;
307 /* The keysyms to use for the various modifiers. */
309 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
310 Lisp_Object Vx_keysym_table;
311 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
313 static Lisp_Object Qvendor_specific_keysyms;
314 static Lisp_Object Qlatin_1;
316 /* Used in x_flush. */
318 extern Lisp_Object Vinhibit_redisplay;
320 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
321 extern int x_bitmap_mask P_ ((FRAME_PTR, int));
323 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
324 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
325 static const XColor *x_color_cells P_ ((Display *, int *));
326 static void x_update_window_end P_ ((struct window *, int, int));
327 void x_delete_display P_ ((struct x_display_info *));
329 static int x_io_error_quitter P_ ((Display *));
330 int x_catch_errors P_ ((Display *));
331 void x_uncatch_errors P_ ((Display *, int));
332 void x_lower_frame P_ ((struct frame *));
333 void x_scroll_bar_clear P_ ((struct frame *));
334 int x_had_errors_p P_ ((Display *));
335 void x_wm_set_size_hint P_ ((struct frame *, long, int));
336 void x_raise_frame P_ ((struct frame *));
337 void x_set_window_size P_ ((struct frame *, int, int, int));
338 void x_wm_set_window_state P_ ((struct frame *, int));
339 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
340 static struct terminal *x_create_terminal P_ ((struct x_display_info *));
341 void x_delete_terminal P_ ((struct terminal *));
342 void x_initialize P_ ((void));
343 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
344 static int x_compute_min_glyph_bounds P_ ((struct frame *));
345 static void x_update_end P_ ((struct frame *));
346 static void XTframe_up_to_date P_ ((struct frame *));
347 static void XTset_terminal_modes P_ ((struct terminal *));
348 static void XTreset_terminal_modes P_ ((struct terminal *));
349 static void x_clear_frame P_ ((struct frame *));
350 static void frame_highlight P_ ((struct frame *));
351 static void frame_unhighlight P_ ((struct frame *));
352 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
353 static void x_focus_changed P_ ((int, int, struct x_display_info *,
354 struct frame *, struct input_event *));
355 static void x_detect_focus_change P_ ((struct x_display_info *,
356 XEvent *, struct input_event *));
357 static void XTframe_rehighlight P_ ((struct frame *));
358 static void x_frame_rehighlight P_ ((struct x_display_info *));
359 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
360 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
361 enum text_cursor_kinds));
363 static void x_clip_to_row P_ ((struct window *, struct glyph_row *, int, GC));
364 static void x_flush P_ ((struct frame *f));
365 static void x_update_begin P_ ((struct frame *));
366 static void x_update_window_begin P_ ((struct window *));
367 static void x_after_update_window_line P_ ((struct glyph_row *));
368 static struct scroll_bar *x_window_to_scroll_bar P_ ((Display *, Window));
369 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
370 enum scroll_bar_part *,
371 Lisp_Object *, Lisp_Object *,
372 unsigned long *));
373 static void x_check_fullscreen P_ ((struct frame *));
374 static void x_check_expected_move P_ ((struct frame *));
375 static int handle_one_xevent P_ ((struct x_display_info *, XEvent *,
376 int *, struct input_event *));
379 /* Flush display of frame F, or of all frames if F is null. */
381 static void
382 x_flush (f)
383 struct frame *f;
385 /* Don't call XFlush when it is not safe to redisplay; the X
386 connection may be broken. */
387 if (!NILP (Vinhibit_redisplay))
388 return;
390 BLOCK_INPUT;
391 if (f == NULL)
393 Lisp_Object rest, frame;
394 FOR_EACH_FRAME (rest, frame)
395 if (FRAME_X_P (XFRAME (frame)))
396 x_flush (XFRAME (frame));
398 else if (FRAME_X_P (f))
399 XFlush (FRAME_X_DISPLAY (f));
400 UNBLOCK_INPUT;
404 /* Remove calls to XFlush by defining XFlush to an empty replacement.
405 Calls to XFlush should be unnecessary because the X output buffer
406 is flushed automatically as needed by calls to XPending,
407 XNextEvent, or XWindowEvent according to the XFlush man page.
408 XTread_socket calls XPending. Removing XFlush improves
409 performance. */
411 #define XFlush(DISPLAY) (void) 0
414 /***********************************************************************
415 Debugging
416 ***********************************************************************/
418 #if 0
420 /* This is a function useful for recording debugging information about
421 the sequence of occurrences in this file. */
423 struct record
425 char *locus;
426 int type;
429 struct record event_record[100];
431 int event_record_index;
433 record_event (locus, type)
434 char *locus;
435 int type;
437 if (event_record_index == sizeof (event_record) / sizeof (struct record))
438 event_record_index = 0;
440 event_record[event_record_index].locus = locus;
441 event_record[event_record_index].type = type;
442 event_record_index++;
445 #endif /* 0 */
449 /* Return the struct x_display_info corresponding to DPY. */
451 struct x_display_info *
452 x_display_info_for_display (dpy)
453 Display *dpy;
455 struct x_display_info *dpyinfo;
457 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
458 if (dpyinfo->display == dpy)
459 return dpyinfo;
461 return 0;
466 /***********************************************************************
467 Starting and ending an update
468 ***********************************************************************/
470 /* Start an update of frame F. This function is installed as a hook
471 for update_begin, i.e. it is called when update_begin is called.
472 This function is called prior to calls to x_update_window_begin for
473 each window being updated. Currently, there is nothing to do here
474 because all interesting stuff is done on a window basis. */
476 static void
477 x_update_begin (f)
478 struct frame *f;
480 /* Nothing to do. */
484 /* Start update of window W. Set the global variable updated_window
485 to the window being updated and set output_cursor to the cursor
486 position of W. */
488 static void
489 x_update_window_begin (w)
490 struct window *w;
492 struct frame *f = XFRAME (WINDOW_FRAME (w));
493 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
495 updated_window = w;
496 set_output_cursor (&w->cursor);
498 BLOCK_INPUT;
500 if (f == display_info->mouse_face_mouse_frame)
502 /* Don't do highlighting for mouse motion during the update. */
503 display_info->mouse_face_defer = 1;
505 /* If F needs to be redrawn, simply forget about any prior mouse
506 highlighting. */
507 if (FRAME_GARBAGED_P (f))
508 display_info->mouse_face_window = Qnil;
510 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
511 their mouse_face_p flag set, which means that they are always
512 unequal to rows in a desired matrix which never have that
513 flag set. So, rows containing mouse-face glyphs are never
514 scrolled, and we don't have to switch the mouse highlight off
515 here to prevent it from being scrolled. */
517 /* Can we tell that this update does not affect the window
518 where the mouse highlight is? If so, no need to turn off.
519 Likewise, don't do anything if the frame is garbaged;
520 in that case, the frame's current matrix that we would use
521 is all wrong, and we will redisplay that line anyway. */
522 if (!NILP (display_info->mouse_face_window)
523 && w == XWINDOW (display_info->mouse_face_window))
525 int i;
527 for (i = 0; i < w->desired_matrix->nrows; ++i)
528 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
529 break;
531 if (i < w->desired_matrix->nrows)
532 clear_mouse_face (display_info);
534 #endif /* 0 */
537 UNBLOCK_INPUT;
541 /* Draw a vertical window border from (x,y0) to (x,y1) */
543 static void
544 x_draw_vertical_window_border (w, x, y0, y1)
545 struct window *w;
546 int x, y0, y1;
548 struct frame *f = XFRAME (WINDOW_FRAME (w));
549 struct face *face;
551 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
552 if (face)
553 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
554 face->foreground);
556 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
557 f->output_data.x->normal_gc, x, y0, x, y1);
560 /* End update of window W (which is equal to updated_window).
562 Draw vertical borders between horizontally adjacent windows, and
563 display W's cursor if CURSOR_ON_P is non-zero.
565 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
566 glyphs in mouse-face were overwritten. In that case we have to
567 make sure that the mouse-highlight is properly redrawn.
569 W may be a menu bar pseudo-window in case we don't have X toolkit
570 support. Such windows don't have a cursor, so don't display it
571 here. */
573 static void
574 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
575 struct window *w;
576 int cursor_on_p, mouse_face_overwritten_p;
578 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
580 if (!w->pseudo_window_p)
582 BLOCK_INPUT;
584 if (cursor_on_p)
585 display_and_set_cursor (w, 1, output_cursor.hpos,
586 output_cursor.vpos,
587 output_cursor.x, output_cursor.y);
589 if (draw_window_fringes (w, 1))
590 x_draw_vertical_border (w);
592 UNBLOCK_INPUT;
595 /* If a row with mouse-face was overwritten, arrange for
596 XTframe_up_to_date to redisplay the mouse highlight. */
597 if (mouse_face_overwritten_p)
599 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
600 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
601 dpyinfo->mouse_face_window = Qnil;
604 updated_window = NULL;
608 /* End update of frame F. This function is installed as a hook in
609 update_end. */
611 static void
612 x_update_end (f)
613 struct frame *f;
615 /* Mouse highlight may be displayed again. */
616 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
618 #ifndef XFlush
619 BLOCK_INPUT;
620 XFlush (FRAME_X_DISPLAY (f));
621 UNBLOCK_INPUT;
622 #endif
626 /* This function is called from various places in xdisp.c whenever a
627 complete update has been performed. The global variable
628 updated_window is not available here. */
630 static void
631 XTframe_up_to_date (f)
632 struct frame *f;
634 if (FRAME_X_P (f))
636 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
638 if (dpyinfo->mouse_face_deferred_gc
639 || f == dpyinfo->mouse_face_mouse_frame)
641 BLOCK_INPUT;
642 if (dpyinfo->mouse_face_mouse_frame)
643 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
644 dpyinfo->mouse_face_mouse_x,
645 dpyinfo->mouse_face_mouse_y);
646 dpyinfo->mouse_face_deferred_gc = 0;
647 UNBLOCK_INPUT;
653 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
654 arrow bitmaps, or clear the fringes if no bitmaps are required
655 before DESIRED_ROW is made current. The window being updated is
656 found in updated_window. This function It is called from
657 update_window_line only if it is known that there are differences
658 between bitmaps to be drawn between current row and DESIRED_ROW. */
660 static void
661 x_after_update_window_line (desired_row)
662 struct glyph_row *desired_row;
664 struct window *w = updated_window;
665 struct frame *f;
666 int width, height;
668 xassert (w);
670 if (!desired_row->mode_line_p && !w->pseudo_window_p)
671 desired_row->redraw_fringe_bitmaps_p = 1;
673 /* When a window has disappeared, make sure that no rest of
674 full-width rows stays visible in the internal border. Could
675 check here if updated_window is the leftmost/rightmost window,
676 but I guess it's not worth doing since vertically split windows
677 are almost never used, internal border is rarely set, and the
678 overhead is very small. */
679 if (windows_or_buffers_changed
680 && desired_row->full_width_p
681 && (f = XFRAME (w->frame),
682 width = FRAME_INTERNAL_BORDER_WIDTH (f),
683 width != 0)
684 && (height = desired_row->visible_height,
685 height > 0))
687 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
689 /* Internal border is drawn below the tool bar. */
690 if (WINDOWP (f->tool_bar_window)
691 && w == XWINDOW (f->tool_bar_window))
692 y -= width;
694 BLOCK_INPUT;
695 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
696 0, y, width, height, False);
697 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
698 FRAME_PIXEL_WIDTH (f) - width,
699 y, width, height, False);
700 UNBLOCK_INPUT;
704 static void
705 x_draw_fringe_bitmap (w, row, p)
706 struct window *w;
707 struct glyph_row *row;
708 struct draw_fringe_bitmap_params *p;
710 struct frame *f = XFRAME (WINDOW_FRAME (w));
711 Display *display = FRAME_X_DISPLAY (f);
712 Window window = FRAME_X_WINDOW (f);
713 GC gc = f->output_data.x->normal_gc;
714 struct face *face = p->face;
715 int rowY;
717 /* Must clip because of partially visible lines. */
718 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
719 if (p->y < rowY)
721 /* Adjust position of "bottom aligned" bitmap on partially
722 visible last row. */
723 int oldY = row->y;
724 int oldVH = row->visible_height;
725 row->visible_height = p->h;
726 row->y -= rowY - p->y;
727 x_clip_to_row (w, row, -1, gc);
728 row->y = oldY;
729 row->visible_height = oldVH;
731 else
732 x_clip_to_row (w, row, -1, gc);
734 if (p->bx >= 0 && !p->overlay_p)
736 /* In case the same realized face is used for fringes and
737 for something displayed in the text (e.g. face `region' on
738 mono-displays, the fill style may have been changed to
739 FillSolid in x_draw_glyph_string_background. */
740 if (face->stipple)
741 XSetFillStyle (display, face->gc, FillOpaqueStippled);
742 else
743 XSetForeground (display, face->gc, face->background);
745 XFillRectangle (display, window, face->gc,
746 p->bx, p->by, p->nx, p->ny);
748 if (!face->stipple)
749 XSetForeground (display, face->gc, face->foreground);
752 if (p->which)
754 unsigned char *bits;
755 Pixmap pixmap, clipmask = (Pixmap) 0;
756 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
757 XGCValues gcv;
759 if (p->wd > 8)
760 bits = (unsigned char *)(p->bits + p->dh);
761 else
762 bits = (unsigned char *)p->bits + p->dh;
764 /* Draw the bitmap. I believe these small pixmaps can be cached
765 by the server. */
766 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
767 (p->cursor_p
768 ? (p->overlay_p ? face->background
769 : f->output_data.x->cursor_pixel)
770 : face->foreground),
771 face->background, depth);
773 if (p->overlay_p)
775 clipmask = XCreatePixmapFromBitmapData (display,
776 FRAME_X_DISPLAY_INFO (f)->root_window,
777 bits, p->wd, p->h,
778 1, 0, 1);
779 gcv.clip_mask = clipmask;
780 gcv.clip_x_origin = p->x;
781 gcv.clip_y_origin = p->y;
782 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
785 XCopyArea (display, pixmap, window, gc, 0, 0,
786 p->wd, p->h, p->x, p->y);
787 XFreePixmap (display, pixmap);
789 if (p->overlay_p)
791 gcv.clip_mask = (Pixmap) 0;
792 XChangeGC (display, gc, GCClipMask, &gcv);
793 XFreePixmap (display, clipmask);
797 XSetClipMask (display, gc, None);
802 /* This is called when starting Emacs and when restarting after
803 suspend. When starting Emacs, no X window is mapped. And nothing
804 must be done to Emacs's own window if it is suspended (though that
805 rarely happens). */
807 static void
808 XTset_terminal_modes (struct terminal *terminal)
812 /* This is called when exiting or suspending Emacs. Exiting will make
813 the X-windows go away, and suspending requires no action. */
815 static void
816 XTreset_terminal_modes (struct terminal *terminal)
822 /***********************************************************************
823 Display Iterator
824 ***********************************************************************/
826 /* Function prototypes of this page. */
828 static int x_encode_char P_ ((int, XChar2b *, struct font_info *, int *));
831 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
832 is not contained in the font. */
834 static XCharStruct *
835 x_per_char_metric (font, char2b, font_type)
836 XFontStruct *font;
837 XChar2b *char2b;
838 int font_type; /* unused on X */
840 /* The result metric information. */
841 XCharStruct *pcm = NULL;
843 xassert (font && char2b);
845 if (font->per_char != NULL)
847 if (font->min_byte1 == 0 && font->max_byte1 == 0)
849 /* min_char_or_byte2 specifies the linear character index
850 corresponding to the first element of the per_char array,
851 max_char_or_byte2 is the index of the last character. A
852 character with non-zero CHAR2B->byte1 is not in the font.
853 A character with byte2 less than min_char_or_byte2 or
854 greater max_char_or_byte2 is not in the font. */
855 if (char2b->byte1 == 0
856 && char2b->byte2 >= font->min_char_or_byte2
857 && char2b->byte2 <= font->max_char_or_byte2)
858 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
860 else
862 /* If either min_byte1 or max_byte1 are nonzero, both
863 min_char_or_byte2 and max_char_or_byte2 are less than
864 256, and the 2-byte character index values corresponding
865 to the per_char array element N (counting from 0) are:
867 byte1 = N/D + min_byte1
868 byte2 = N\D + min_char_or_byte2
870 where:
872 D = max_char_or_byte2 - min_char_or_byte2 + 1
873 / = integer division
874 \ = integer modulus */
875 if (char2b->byte1 >= font->min_byte1
876 && char2b->byte1 <= font->max_byte1
877 && char2b->byte2 >= font->min_char_or_byte2
878 && char2b->byte2 <= font->max_char_or_byte2)
880 pcm = (font->per_char
881 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
882 * (char2b->byte1 - font->min_byte1))
883 + (char2b->byte2 - font->min_char_or_byte2));
887 else
889 /* If the per_char pointer is null, all glyphs between the first
890 and last character indexes inclusive have the same
891 information, as given by both min_bounds and max_bounds. */
892 if (char2b->byte2 >= font->min_char_or_byte2
893 && char2b->byte2 <= font->max_char_or_byte2)
894 pcm = &font->max_bounds;
897 return ((pcm == NULL
898 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
899 ? NULL : pcm);
903 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
904 the two-byte form of C. Encoding is returned in *CHAR2B. */
906 static int
907 x_encode_char (c, char2b, font_info, two_byte_p)
908 int c;
909 XChar2b *char2b;
910 struct font_info *font_info;
911 int *two_byte_p;
913 int charset = CHAR_CHARSET (c);
914 XFontStruct *font = font_info->font;
916 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
917 This may be either a program in a special encoder language or a
918 fixed encoding. */
919 if (font_info->font_encoder)
921 /* It's a program. */
922 struct ccl_program *ccl = font_info->font_encoder;
924 check_ccl_update (ccl);
925 if (CHARSET_DIMENSION (charset) == 1)
927 ccl->reg[0] = charset;
928 ccl->reg[1] = char2b->byte2;
929 ccl->reg[2] = -1;
931 else
933 ccl->reg[0] = charset;
934 ccl->reg[1] = char2b->byte1;
935 ccl->reg[2] = char2b->byte2;
938 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
940 /* We assume that MSBs are appropriately set/reset by CCL
941 program. */
942 if (font->max_byte1 == 0) /* 1-byte font */
943 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
944 else
945 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
947 else if (font_info->encoding[charset])
949 /* Fixed encoding scheme. See fontset.h for the meaning of the
950 encoding numbers. */
951 int enc = font_info->encoding[charset];
953 if ((enc == 1 || enc == 2)
954 && CHARSET_DIMENSION (charset) == 2)
955 char2b->byte1 |= 0x80;
957 if (enc == 1 || enc == 3)
958 char2b->byte2 |= 0x80;
961 if (two_byte_p)
962 *two_byte_p = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
964 return FONT_TYPE_UNKNOWN;
969 /***********************************************************************
970 Glyph display
971 ***********************************************************************/
975 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
976 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
977 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
978 int));
979 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
980 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
981 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
982 static void x_draw_glyph_string P_ ((struct glyph_string *));
983 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
984 static void x_set_cursor_gc P_ ((struct glyph_string *));
985 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
986 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
987 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
988 unsigned long *, double, int));
989 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
990 double, int, unsigned long));
991 static void x_setup_relief_colors P_ ((struct glyph_string *));
992 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
993 static void x_draw_image_relief P_ ((struct glyph_string *));
994 static void x_draw_image_foreground P_ ((struct glyph_string *));
995 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
996 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
997 int, int, int));
998 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
999 int, int, int, int, int, int,
1000 XRectangle *));
1001 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
1002 int, int, int, XRectangle *));
1004 #if GLYPH_DEBUG
1005 static void x_check_font P_ ((struct frame *, XFontStruct *));
1006 #endif
1009 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
1010 face. */
1012 static void
1013 x_set_cursor_gc (s)
1014 struct glyph_string *s;
1016 if (s->font == FRAME_FONT (s->f)
1017 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
1018 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
1019 && !s->cmp)
1020 s->gc = s->f->output_data.x->cursor_gc;
1021 else
1023 /* Cursor on non-default face: must merge. */
1024 XGCValues xgcv;
1025 unsigned long mask;
1027 xgcv.background = s->f->output_data.x->cursor_pixel;
1028 xgcv.foreground = s->face->background;
1030 /* If the glyph would be invisible, try a different foreground. */
1031 if (xgcv.foreground == xgcv.background)
1032 xgcv.foreground = s->face->foreground;
1033 if (xgcv.foreground == xgcv.background)
1034 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1035 if (xgcv.foreground == xgcv.background)
1036 xgcv.foreground = s->face->foreground;
1038 /* Make sure the cursor is distinct from text in this face. */
1039 if (xgcv.background == s->face->background
1040 && xgcv.foreground == s->face->foreground)
1042 xgcv.background = s->face->foreground;
1043 xgcv.foreground = s->face->background;
1046 IF_DEBUG (x_check_font (s->f, s->font));
1047 xgcv.font = s->font->fid;
1048 xgcv.graphics_exposures = False;
1049 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
1051 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1052 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1053 mask, &xgcv);
1054 else
1055 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1056 = XCreateGC (s->display, s->window, mask, &xgcv);
1058 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1063 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1065 static void
1066 x_set_mouse_face_gc (s)
1067 struct glyph_string *s;
1069 int face_id;
1070 struct face *face;
1072 /* What face has to be used last for the mouse face? */
1073 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
1074 face = FACE_FROM_ID (s->f, face_id);
1075 if (face == NULL)
1076 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1078 if (s->first_glyph->type == CHAR_GLYPH)
1079 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
1080 else
1081 face_id = FACE_FOR_CHAR (s->f, face, 0);
1082 s->face = FACE_FROM_ID (s->f, face_id);
1083 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1085 /* If font in this face is same as S->font, use it. */
1086 if (s->font == s->face->font)
1087 s->gc = s->face->gc;
1088 else
1090 /* Otherwise construct scratch_cursor_gc with values from FACE
1091 but font FONT. */
1092 XGCValues xgcv;
1093 unsigned long mask;
1095 xgcv.background = s->face->background;
1096 xgcv.foreground = s->face->foreground;
1097 IF_DEBUG (x_check_font (s->f, s->font));
1098 xgcv.font = s->font->fid;
1099 xgcv.graphics_exposures = False;
1100 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
1102 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1103 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1104 mask, &xgcv);
1105 else
1106 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1107 = XCreateGC (s->display, s->window, mask, &xgcv);
1109 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1112 xassert (s->gc != 0);
1116 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1117 Faces to use in the mode line have already been computed when the
1118 matrix was built, so there isn't much to do, here. */
1120 static INLINE void
1121 x_set_mode_line_face_gc (s)
1122 struct glyph_string *s;
1124 s->gc = s->face->gc;
1128 /* Set S->gc of glyph string S for drawing that glyph string. Set
1129 S->stippled_p to a non-zero value if the face of S has a stipple
1130 pattern. */
1132 static INLINE void
1133 x_set_glyph_string_gc (s)
1134 struct glyph_string *s;
1136 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1138 if (s->hl == DRAW_NORMAL_TEXT)
1140 s->gc = s->face->gc;
1141 s->stippled_p = s->face->stipple != 0;
1143 else if (s->hl == DRAW_INVERSE_VIDEO)
1145 x_set_mode_line_face_gc (s);
1146 s->stippled_p = s->face->stipple != 0;
1148 else if (s->hl == DRAW_CURSOR)
1150 x_set_cursor_gc (s);
1151 s->stippled_p = 0;
1153 else if (s->hl == DRAW_MOUSE_FACE)
1155 x_set_mouse_face_gc (s);
1156 s->stippled_p = s->face->stipple != 0;
1158 else if (s->hl == DRAW_IMAGE_RAISED
1159 || s->hl == DRAW_IMAGE_SUNKEN)
1161 s->gc = s->face->gc;
1162 s->stippled_p = s->face->stipple != 0;
1164 else
1166 s->gc = s->face->gc;
1167 s->stippled_p = s->face->stipple != 0;
1170 /* GC must have been set. */
1171 xassert (s->gc != 0);
1175 /* Set clipping for output of glyph string S. S may be part of a mode
1176 line or menu if we don't have X toolkit support. */
1178 static INLINE void
1179 x_set_glyph_string_clipping (s)
1180 struct glyph_string *s;
1182 XRectangle r;
1183 get_glyph_string_clip_rect (s, &r);
1184 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
1188 /* RIF:
1189 Compute left and right overhang of glyph string S. If S is a glyph
1190 string for a composition, assume overhangs don't exist. */
1192 static void
1193 x_compute_glyph_string_overhangs (s)
1194 struct glyph_string *s;
1196 if (s->cmp == NULL
1197 && s->first_glyph->type == CHAR_GLYPH)
1199 XCharStruct cs;
1200 int direction, font_ascent, font_descent;
1201 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
1202 &font_ascent, &font_descent, &cs);
1203 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
1204 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
1209 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1211 static INLINE void
1212 x_clear_glyph_string_rect (s, x, y, w, h)
1213 struct glyph_string *s;
1214 int x, y, w, h;
1216 XGCValues xgcv;
1217 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1218 XSetForeground (s->display, s->gc, xgcv.background);
1219 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1220 XSetForeground (s->display, s->gc, xgcv.foreground);
1224 /* Draw the background of glyph_string S. If S->background_filled_p
1225 is non-zero don't draw it. FORCE_P non-zero means draw the
1226 background even if it wouldn't be drawn normally. This is used
1227 when a string preceding S draws into the background of S, or S
1228 contains the first component of a composition. */
1230 static void
1231 x_draw_glyph_string_background (s, force_p)
1232 struct glyph_string *s;
1233 int force_p;
1235 /* Nothing to do if background has already been drawn or if it
1236 shouldn't be drawn in the first place. */
1237 if (!s->background_filled_p)
1239 int box_line_width = max (s->face->box_line_width, 0);
1241 if (s->stippled_p)
1243 /* Fill background with a stipple pattern. */
1244 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1245 XFillRectangle (s->display, s->window, s->gc, s->x,
1246 s->y + box_line_width,
1247 s->background_width,
1248 s->height - 2 * box_line_width);
1249 XSetFillStyle (s->display, s->gc, FillSolid);
1250 s->background_filled_p = 1;
1252 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1253 || s->font_not_found_p
1254 || s->extends_to_end_of_line_p
1255 || force_p)
1257 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1258 s->background_width,
1259 s->height - 2 * box_line_width);
1260 s->background_filled_p = 1;
1266 /* Draw the foreground of glyph string S. */
1268 static void
1269 x_draw_glyph_string_foreground (s)
1270 struct glyph_string *s;
1272 int i, x;
1274 /* If first glyph of S has a left box line, start drawing the text
1275 of S to the right of that box line. */
1276 if (s->face->box != FACE_NO_BOX
1277 && s->first_glyph->left_box_line_p)
1278 x = s->x + abs (s->face->box_line_width);
1279 else
1280 x = s->x;
1282 /* Draw characters of S as rectangles if S's font could not be
1283 loaded. */
1284 if (s->font_not_found_p)
1286 for (i = 0; i < s->nchars; ++i)
1288 struct glyph *g = s->first_glyph + i;
1289 XDrawRectangle (s->display, s->window,
1290 s->gc, x, s->y, g->pixel_width - 1,
1291 s->height - 1);
1292 x += g->pixel_width;
1295 else
1297 char *char1b = (char *) s->char2b;
1298 int boff = s->font_info->baseline_offset;
1300 if (s->font_info->vertical_centering)
1301 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
1303 /* If we can use 8-bit functions, condense S->char2b. */
1304 if (!s->two_byte_p)
1305 for (i = 0; i < s->nchars; ++i)
1306 char1b[i] = s->char2b[i].byte2;
1308 /* Draw text with XDrawString if background has already been
1309 filled. Otherwise, use XDrawImageString. (Note that
1310 XDrawImageString is usually faster than XDrawString.) Always
1311 use XDrawImageString when drawing the cursor so that there is
1312 no chance that characters under a box cursor are invisible. */
1313 if (s->for_overlaps
1314 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1316 /* Draw characters with 16-bit or 8-bit functions. */
1317 if (s->two_byte_p)
1318 XDrawString16 (s->display, s->window, s->gc, x,
1319 s->ybase - boff, s->char2b, s->nchars);
1320 else
1321 XDrawString (s->display, s->window, s->gc, x,
1322 s->ybase - boff, char1b, s->nchars);
1324 else
1326 if (s->two_byte_p)
1327 XDrawImageString16 (s->display, s->window, s->gc, x,
1328 s->ybase - boff, s->char2b, s->nchars);
1329 else
1330 XDrawImageString (s->display, s->window, s->gc, x,
1331 s->ybase - boff, char1b, s->nchars);
1334 if (s->face->overstrike)
1336 /* For overstriking (to simulate bold-face), draw the
1337 characters again shifted to the right by one pixel. */
1338 if (s->two_byte_p)
1339 XDrawString16 (s->display, s->window, s->gc, x + 1,
1340 s->ybase - boff, s->char2b, s->nchars);
1341 else
1342 XDrawString (s->display, s->window, s->gc, x + 1,
1343 s->ybase - boff, char1b, s->nchars);
1348 /* Draw the foreground of composite glyph string S. */
1350 static void
1351 x_draw_composite_glyph_string_foreground (s)
1352 struct glyph_string *s;
1354 int i, x;
1356 /* If first glyph of S has a left box line, start drawing the text
1357 of S to the right of that box line. */
1358 if (s->face->box != FACE_NO_BOX
1359 && s->first_glyph->left_box_line_p)
1360 x = s->x + abs (s->face->box_line_width);
1361 else
1362 x = s->x;
1364 /* S is a glyph string for a composition. S->gidx is the index of
1365 the first character drawn for glyphs of this composition.
1366 S->gidx == 0 means we are drawing the very first character of
1367 this composition. */
1369 /* Draw a rectangle for the composition if the font for the very
1370 first character of the composition could not be loaded. */
1371 if (s->font_not_found_p)
1373 if (s->gidx == 0)
1374 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1375 s->width - 1, s->height - 1);
1377 else
1379 for (i = 0; i < s->nchars; i++, ++s->gidx)
1381 XDrawString16 (s->display, s->window, s->gc,
1382 x + s->cmp->offsets[s->gidx * 2],
1383 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1384 s->char2b + i, 1);
1385 if (s->face->overstrike)
1386 XDrawString16 (s->display, s->window, s->gc,
1387 x + s->cmp->offsets[s->gidx * 2] + 1,
1388 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1389 s->char2b + i, 1);
1395 #ifdef USE_X_TOOLKIT
1397 static struct frame *x_frame_of_widget P_ ((Widget));
1398 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1399 XrmValue *, XrmValue *, XtPointer *));
1400 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1401 XrmValue *, Cardinal *));
1404 /* Return the frame on which widget WIDGET is used.. Abort if frame
1405 cannot be determined. */
1407 static struct frame *
1408 x_frame_of_widget (widget)
1409 Widget widget;
1411 struct x_display_info *dpyinfo;
1412 Lisp_Object tail;
1413 struct frame *f;
1415 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1417 /* Find the top-level shell of the widget. Note that this function
1418 can be called when the widget is not yet realized, so XtWindow
1419 (widget) == 0. That's the reason we can't simply use
1420 x_any_window_to_frame. */
1421 while (!XtIsTopLevelShell (widget))
1422 widget = XtParent (widget);
1424 /* Look for a frame with that top-level widget. Allocate the color
1425 on that frame to get the right gamma correction value. */
1426 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1427 if (GC_FRAMEP (XCAR (tail))
1428 && (f = XFRAME (XCAR (tail)),
1429 (FRAME_X_P (f)
1430 && f->output_data.nothing != 1
1431 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1432 && f->output_data.x->widget == widget)
1433 return f;
1435 abort ();
1439 /* Allocate the color COLOR->pixel on the screen and display of
1440 widget WIDGET in colormap CMAP. If an exact match cannot be
1441 allocated, try the nearest color available. Value is non-zero
1442 if successful. This is called from lwlib. */
1445 x_alloc_nearest_color_for_widget (widget, cmap, color)
1446 Widget widget;
1447 Colormap cmap;
1448 XColor *color;
1450 struct frame *f = x_frame_of_widget (widget);
1451 return x_alloc_nearest_color (f, cmap, color);
1455 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1456 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1457 If this produces the same color as PIXEL, try a color where all RGB
1458 values have DELTA added. Return the allocated color in *PIXEL.
1459 DISPLAY is the X display, CMAP is the colormap to operate on.
1460 Value is non-zero if successful. */
1463 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1464 Widget widget;
1465 Display *display;
1466 Colormap cmap;
1467 unsigned long *pixel;
1468 double factor;
1469 int delta;
1471 struct frame *f = x_frame_of_widget (widget);
1472 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1476 /* Structure specifying which arguments should be passed by Xt to
1477 cvt_string_to_pixel. We want the widget's screen and colormap. */
1479 static XtConvertArgRec cvt_string_to_pixel_args[] =
1481 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1482 sizeof (Screen *)},
1483 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1484 sizeof (Colormap)}
1488 /* The address of this variable is returned by
1489 cvt_string_to_pixel. */
1491 static Pixel cvt_string_to_pixel_value;
1494 /* Convert a color name to a pixel color.
1496 DPY is the display we are working on.
1498 ARGS is an array of *NARGS XrmValue structures holding additional
1499 information about the widget for which the conversion takes place.
1500 The contents of this array are determined by the specification
1501 in cvt_string_to_pixel_args.
1503 FROM is a pointer to an XrmValue which points to the color name to
1504 convert. TO is an XrmValue in which to return the pixel color.
1506 CLOSURE_RET is a pointer to user-data, in which we record if
1507 we allocated the color or not.
1509 Value is True if successful, False otherwise. */
1511 static Boolean
1512 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1513 Display *dpy;
1514 XrmValue *args;
1515 Cardinal *nargs;
1516 XrmValue *from, *to;
1517 XtPointer *closure_ret;
1519 Screen *screen;
1520 Colormap cmap;
1521 Pixel pixel;
1522 String color_name;
1523 XColor color;
1525 if (*nargs != 2)
1527 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1528 "wrongParameters", "cvt_string_to_pixel",
1529 "XtToolkitError",
1530 "Screen and colormap args required", NULL, NULL);
1531 return False;
1534 screen = *(Screen **) args[0].addr;
1535 cmap = *(Colormap *) args[1].addr;
1536 color_name = (String) from->addr;
1538 if (strcmp (color_name, XtDefaultBackground) == 0)
1540 *closure_ret = (XtPointer) False;
1541 pixel = WhitePixelOfScreen (screen);
1543 else if (strcmp (color_name, XtDefaultForeground) == 0)
1545 *closure_ret = (XtPointer) False;
1546 pixel = BlackPixelOfScreen (screen);
1548 else if (XParseColor (dpy, cmap, color_name, &color)
1549 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1551 pixel = color.pixel;
1552 *closure_ret = (XtPointer) True;
1554 else
1556 String params[1];
1557 Cardinal nparams = 1;
1559 params[0] = color_name;
1560 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1561 "badValue", "cvt_string_to_pixel",
1562 "XtToolkitError", "Invalid color `%s'",
1563 params, &nparams);
1564 return False;
1567 if (to->addr != NULL)
1569 if (to->size < sizeof (Pixel))
1571 to->size = sizeof (Pixel);
1572 return False;
1575 *(Pixel *) to->addr = pixel;
1577 else
1579 cvt_string_to_pixel_value = pixel;
1580 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1583 to->size = sizeof (Pixel);
1584 return True;
1588 /* Free a pixel color which was previously allocated via
1589 cvt_string_to_pixel. This is registered as the destructor
1590 for this type of resource via XtSetTypeConverter.
1592 APP is the application context in which we work.
1594 TO is a pointer to an XrmValue holding the color to free.
1595 CLOSURE is the value we stored in CLOSURE_RET for this color
1596 in cvt_string_to_pixel.
1598 ARGS and NARGS are like for cvt_string_to_pixel. */
1600 static void
1601 cvt_pixel_dtor (app, to, closure, args, nargs)
1602 XtAppContext app;
1603 XrmValuePtr to;
1604 XtPointer closure;
1605 XrmValuePtr args;
1606 Cardinal *nargs;
1608 if (*nargs != 2)
1610 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1611 "XtToolkitError",
1612 "Screen and colormap arguments required",
1613 NULL, NULL);
1615 else if (closure != NULL)
1617 /* We did allocate the pixel, so free it. */
1618 Screen *screen = *(Screen **) args[0].addr;
1619 Colormap cmap = *(Colormap *) args[1].addr;
1620 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1621 (Pixel *) to->addr, 1);
1626 #endif /* USE_X_TOOLKIT */
1629 /* Value is an array of XColor structures for the contents of the
1630 color map of display DPY. Set *NCELLS to the size of the array.
1631 Note that this probably shouldn't be called for large color maps,
1632 say a 24-bit TrueColor map. */
1634 static const XColor *
1635 x_color_cells (dpy, ncells)
1636 Display *dpy;
1637 int *ncells;
1639 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1641 if (dpyinfo->color_cells == NULL)
1643 Screen *screen = dpyinfo->screen;
1644 int i;
1646 dpyinfo->ncolor_cells
1647 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1648 dpyinfo->color_cells
1649 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1650 * sizeof *dpyinfo->color_cells);
1652 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1653 dpyinfo->color_cells[i].pixel = i;
1655 XQueryColors (dpy, dpyinfo->cmap,
1656 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1659 *ncells = dpyinfo->ncolor_cells;
1660 return dpyinfo->color_cells;
1664 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1665 colors in COLORS. Use cached information, if available. */
1667 void
1668 x_query_colors (f, colors, ncolors)
1669 struct frame *f;
1670 XColor *colors;
1671 int ncolors;
1673 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1675 if (dpyinfo->color_cells)
1677 int i;
1678 for (i = 0; i < ncolors; ++i)
1680 unsigned long pixel = colors[i].pixel;
1681 xassert (pixel < dpyinfo->ncolor_cells);
1682 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1683 colors[i] = dpyinfo->color_cells[pixel];
1686 else
1687 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1691 /* On frame F, translate pixel color to RGB values for the color in
1692 COLOR. Use cached information, if available. */
1694 void
1695 x_query_color (f, color)
1696 struct frame *f;
1697 XColor *color;
1699 x_query_colors (f, color, 1);
1703 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1704 exact match can't be allocated, try the nearest color available.
1705 Value is non-zero if successful. Set *COLOR to the color
1706 allocated. */
1708 static int
1709 x_alloc_nearest_color_1 (dpy, cmap, color)
1710 Display *dpy;
1711 Colormap cmap;
1712 XColor *color;
1714 int rc;
1716 rc = XAllocColor (dpy, cmap, color);
1717 if (rc == 0)
1719 /* If we got to this point, the colormap is full, so we're going
1720 to try to get the next closest color. The algorithm used is
1721 a least-squares matching, which is what X uses for closest
1722 color matching with StaticColor visuals. */
1723 int nearest, i;
1724 unsigned long nearest_delta = ~0;
1725 int ncells;
1726 const XColor *cells = x_color_cells (dpy, &ncells);
1728 for (nearest = i = 0; i < ncells; ++i)
1730 long dred = (color->red >> 8) - (cells[i].red >> 8);
1731 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1732 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1733 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1735 if (delta < nearest_delta)
1737 nearest = i;
1738 nearest_delta = delta;
1742 color->red = cells[nearest].red;
1743 color->green = cells[nearest].green;
1744 color->blue = cells[nearest].blue;
1745 rc = XAllocColor (dpy, cmap, color);
1747 else
1749 /* If allocation succeeded, and the allocated pixel color is not
1750 equal to a cached pixel color recorded earlier, there was a
1751 change in the colormap, so clear the color cache. */
1752 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1753 XColor *cached_color;
1755 if (dpyinfo->color_cells
1756 && (cached_color = &dpyinfo->color_cells[color->pixel],
1757 (cached_color->red != color->red
1758 || cached_color->blue != color->blue
1759 || cached_color->green != color->green)))
1761 xfree (dpyinfo->color_cells);
1762 dpyinfo->color_cells = NULL;
1763 dpyinfo->ncolor_cells = 0;
1767 #ifdef DEBUG_X_COLORS
1768 if (rc)
1769 register_color (color->pixel);
1770 #endif /* DEBUG_X_COLORS */
1772 return rc;
1776 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1777 exact match can't be allocated, try the nearest color available.
1778 Value is non-zero if successful. Set *COLOR to the color
1779 allocated. */
1782 x_alloc_nearest_color (f, cmap, color)
1783 struct frame *f;
1784 Colormap cmap;
1785 XColor *color;
1787 gamma_correct (f, color);
1788 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1792 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1793 It's necessary to do this instead of just using PIXEL directly to
1794 get color reference counts right. */
1796 unsigned long
1797 x_copy_color (f, pixel)
1798 struct frame *f;
1799 unsigned long pixel;
1801 XColor color;
1803 color.pixel = pixel;
1804 BLOCK_INPUT;
1805 x_query_color (f, &color);
1806 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1807 UNBLOCK_INPUT;
1808 #ifdef DEBUG_X_COLORS
1809 register_color (pixel);
1810 #endif
1811 return color.pixel;
1815 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
1816 It's necessary to do this instead of just using PIXEL directly to
1817 get color reference counts right. */
1819 unsigned long
1820 x_copy_dpy_color (dpy, cmap, pixel)
1821 Display *dpy;
1822 Colormap cmap;
1823 unsigned long pixel;
1825 XColor color;
1827 color.pixel = pixel;
1828 BLOCK_INPUT;
1829 XQueryColor (dpy, cmap, &color);
1830 XAllocColor (dpy, cmap, &color);
1831 UNBLOCK_INPUT;
1832 #ifdef DEBUG_X_COLORS
1833 register_color (pixel);
1834 #endif
1835 return color.pixel;
1839 /* Brightness beyond which a color won't have its highlight brightness
1840 boosted.
1842 Nominally, highlight colors for `3d' faces are calculated by
1843 brightening an object's color by a constant scale factor, but this
1844 doesn't yield good results for dark colors, so for colors who's
1845 brightness is less than this value (on a scale of 0-65535) have an
1846 use an additional additive factor.
1848 The value here is set so that the default menu-bar/mode-line color
1849 (grey75) will not have its highlights changed at all. */
1850 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1853 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1854 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1855 If this produces the same color as PIXEL, try a color where all RGB
1856 values have DELTA added. Return the allocated color in *PIXEL.
1857 DISPLAY is the X display, CMAP is the colormap to operate on.
1858 Value is non-zero if successful. */
1860 static int
1861 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
1862 struct frame *f;
1863 Display *display;
1864 Colormap cmap;
1865 unsigned long *pixel;
1866 double factor;
1867 int delta;
1869 XColor color, new;
1870 long bright;
1871 int success_p;
1873 /* Get RGB color values. */
1874 color.pixel = *pixel;
1875 x_query_color (f, &color);
1877 /* Change RGB values by specified FACTOR. Avoid overflow! */
1878 xassert (factor >= 0);
1879 new.red = min (0xffff, factor * color.red);
1880 new.green = min (0xffff, factor * color.green);
1881 new.blue = min (0xffff, factor * color.blue);
1883 /* Calculate brightness of COLOR. */
1884 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1886 /* We only boost colors that are darker than
1887 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1888 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1889 /* Make an additive adjustment to NEW, because it's dark enough so
1890 that scaling by FACTOR alone isn't enough. */
1892 /* How far below the limit this color is (0 - 1, 1 being darker). */
1893 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1894 /* The additive adjustment. */
1895 int min_delta = delta * dimness * factor / 2;
1897 if (factor < 1)
1899 new.red = max (0, new.red - min_delta);
1900 new.green = max (0, new.green - min_delta);
1901 new.blue = max (0, new.blue - min_delta);
1903 else
1905 new.red = min (0xffff, min_delta + new.red);
1906 new.green = min (0xffff, min_delta + new.green);
1907 new.blue = min (0xffff, min_delta + new.blue);
1911 /* Try to allocate the color. */
1912 success_p = x_alloc_nearest_color (f, cmap, &new);
1913 if (success_p)
1915 if (new.pixel == *pixel)
1917 /* If we end up with the same color as before, try adding
1918 delta to the RGB values. */
1919 x_free_colors (f, &new.pixel, 1);
1921 new.red = min (0xffff, delta + color.red);
1922 new.green = min (0xffff, delta + color.green);
1923 new.blue = min (0xffff, delta + color.blue);
1924 success_p = x_alloc_nearest_color (f, cmap, &new);
1926 else
1927 success_p = 1;
1928 *pixel = new.pixel;
1931 return success_p;
1935 /* Set up the foreground color for drawing relief lines of glyph
1936 string S. RELIEF is a pointer to a struct relief containing the GC
1937 with which lines will be drawn. Use a color that is FACTOR or
1938 DELTA lighter or darker than the relief's background which is found
1939 in S->f->output_data.x->relief_background. If such a color cannot
1940 be allocated, use DEFAULT_PIXEL, instead. */
1942 static void
1943 x_setup_relief_color (f, relief, factor, delta, default_pixel)
1944 struct frame *f;
1945 struct relief *relief;
1946 double factor;
1947 int delta;
1948 unsigned long default_pixel;
1950 XGCValues xgcv;
1951 struct x_output *di = f->output_data.x;
1952 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1953 unsigned long pixel;
1954 unsigned long background = di->relief_background;
1955 Colormap cmap = FRAME_X_COLORMAP (f);
1956 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1957 Display *dpy = FRAME_X_DISPLAY (f);
1959 xgcv.graphics_exposures = False;
1960 xgcv.line_width = 1;
1962 /* Free previously allocated color. The color cell will be reused
1963 when it has been freed as many times as it was allocated, so this
1964 doesn't affect faces using the same colors. */
1965 if (relief->gc
1966 && relief->allocated_p)
1968 x_free_colors (f, &relief->pixel, 1);
1969 relief->allocated_p = 0;
1972 /* Allocate new color. */
1973 xgcv.foreground = default_pixel;
1974 pixel = background;
1975 if (dpyinfo->n_planes != 1
1976 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1978 relief->allocated_p = 1;
1979 xgcv.foreground = relief->pixel = pixel;
1982 if (relief->gc == 0)
1984 xgcv.stipple = dpyinfo->gray;
1985 mask |= GCStipple;
1986 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1988 else
1989 XChangeGC (dpy, relief->gc, mask, &xgcv);
1993 /* Set up colors for the relief lines around glyph string S. */
1995 static void
1996 x_setup_relief_colors (s)
1997 struct glyph_string *s;
1999 struct x_output *di = s->f->output_data.x;
2000 unsigned long color;
2002 if (s->face->use_box_color_for_shadows_p)
2003 color = s->face->box_color;
2004 else if (s->first_glyph->type == IMAGE_GLYPH
2005 && s->img->pixmap
2006 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2007 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2008 else
2010 XGCValues xgcv;
2012 /* Get the background color of the face. */
2013 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2014 color = xgcv.background;
2017 if (di->white_relief.gc == 0
2018 || color != di->relief_background)
2020 di->relief_background = color;
2021 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2022 WHITE_PIX_DEFAULT (s->f));
2023 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2024 BLACK_PIX_DEFAULT (s->f));
2029 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2030 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2031 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
2032 relief. LEFT_P non-zero means draw a relief on the left side of
2033 the rectangle. RIGHT_P non-zero means draw a relief on the right
2034 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2035 when drawing. */
2037 static void
2038 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
2039 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
2040 struct frame *f;
2041 int left_x, top_y, right_x, bottom_y, width;
2042 int top_p, bot_p, left_p, right_p, raised_p;
2043 XRectangle *clip_rect;
2045 Display *dpy = FRAME_X_DISPLAY (f);
2046 Window window = FRAME_X_WINDOW (f);
2047 int i;
2048 GC gc;
2050 if (raised_p)
2051 gc = f->output_data.x->white_relief.gc;
2052 else
2053 gc = f->output_data.x->black_relief.gc;
2054 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2056 /* Top. */
2057 if (top_p)
2058 for (i = 0; i < width; ++i)
2059 XDrawLine (dpy, window, gc,
2060 left_x + i * left_p, top_y + i,
2061 right_x + 1 - i * right_p, top_y + i);
2063 /* Left. */
2064 if (left_p)
2065 for (i = 0; i < width; ++i)
2066 XDrawLine (dpy, window, gc,
2067 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
2069 XSetClipMask (dpy, gc, None);
2070 if (raised_p)
2071 gc = f->output_data.x->black_relief.gc;
2072 else
2073 gc = f->output_data.x->white_relief.gc;
2074 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2076 /* Bottom. */
2077 if (bot_p)
2078 for (i = 0; i < width; ++i)
2079 XDrawLine (dpy, window, gc,
2080 left_x + i * left_p, bottom_y - i,
2081 right_x + 1 - i * right_p, bottom_y - i);
2083 /* Right. */
2084 if (right_p)
2085 for (i = 0; i < width; ++i)
2086 XDrawLine (dpy, window, gc,
2087 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2089 XSetClipMask (dpy, gc, None);
2093 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2094 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2095 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2096 left side of the rectangle. RIGHT_P non-zero means draw a line
2097 on the right side of the rectangle. CLIP_RECT is the clipping
2098 rectangle to use when drawing. */
2100 static void
2101 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2102 left_p, right_p, clip_rect)
2103 struct glyph_string *s;
2104 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
2105 XRectangle *clip_rect;
2107 XGCValues xgcv;
2109 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2110 XSetForeground (s->display, s->gc, s->face->box_color);
2111 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2113 /* Top. */
2114 XFillRectangle (s->display, s->window, s->gc,
2115 left_x, top_y, right_x - left_x + 1, width);
2117 /* Left. */
2118 if (left_p)
2119 XFillRectangle (s->display, s->window, s->gc,
2120 left_x, top_y, width, bottom_y - top_y + 1);
2122 /* Bottom. */
2123 XFillRectangle (s->display, s->window, s->gc,
2124 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2126 /* Right. */
2127 if (right_p)
2128 XFillRectangle (s->display, s->window, s->gc,
2129 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2131 XSetForeground (s->display, s->gc, xgcv.foreground);
2132 XSetClipMask (s->display, s->gc, None);
2136 /* Draw a box around glyph string S. */
2138 static void
2139 x_draw_glyph_string_box (s)
2140 struct glyph_string *s;
2142 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2143 int left_p, right_p;
2144 struct glyph *last_glyph;
2145 XRectangle clip_rect;
2147 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2148 ? WINDOW_RIGHT_EDGE_X (s->w)
2149 : window_box_right (s->w, s->area));
2151 /* The glyph that may have a right box line. */
2152 last_glyph = (s->cmp || s->img
2153 ? s->first_glyph
2154 : s->first_glyph + s->nchars - 1);
2156 width = abs (s->face->box_line_width);
2157 raised_p = s->face->box == FACE_RAISED_BOX;
2158 left_x = s->x;
2159 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2160 ? last_x - 1
2161 : min (last_x, s->x + s->background_width) - 1);
2162 top_y = s->y;
2163 bottom_y = top_y + s->height - 1;
2165 left_p = (s->first_glyph->left_box_line_p
2166 || (s->hl == DRAW_MOUSE_FACE
2167 && (s->prev == NULL
2168 || s->prev->hl != s->hl)));
2169 right_p = (last_glyph->right_box_line_p
2170 || (s->hl == DRAW_MOUSE_FACE
2171 && (s->next == NULL
2172 || s->next->hl != s->hl)));
2174 get_glyph_string_clip_rect (s, &clip_rect);
2176 if (s->face->box == FACE_SIMPLE_BOX)
2177 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2178 left_p, right_p, &clip_rect);
2179 else
2181 x_setup_relief_colors (s);
2182 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2183 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2188 /* Draw foreground of image glyph string S. */
2190 static void
2191 x_draw_image_foreground (s)
2192 struct glyph_string *s;
2194 int x = s->x;
2195 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2197 /* If first glyph of S has a left box line, start drawing it to the
2198 right of that line. */
2199 if (s->face->box != FACE_NO_BOX
2200 && s->first_glyph->left_box_line_p
2201 && s->slice.x == 0)
2202 x += abs (s->face->box_line_width);
2204 /* If there is a margin around the image, adjust x- and y-position
2205 by that margin. */
2206 if (s->slice.x == 0)
2207 x += s->img->hmargin;
2208 if (s->slice.y == 0)
2209 y += s->img->vmargin;
2211 if (s->img->pixmap)
2213 if (s->img->mask)
2215 /* We can't set both a clip mask and use XSetClipRectangles
2216 because the latter also sets a clip mask. We also can't
2217 trust on the shape extension to be available
2218 (XShapeCombineRegion). So, compute the rectangle to draw
2219 manually. */
2220 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2221 | GCFunction);
2222 XGCValues xgcv;
2223 XRectangle clip_rect, image_rect, r;
2225 xgcv.clip_mask = s->img->mask;
2226 xgcv.clip_x_origin = x;
2227 xgcv.clip_y_origin = y;
2228 xgcv.function = GXcopy;
2229 XChangeGC (s->display, s->gc, mask, &xgcv);
2231 get_glyph_string_clip_rect (s, &clip_rect);
2232 image_rect.x = x;
2233 image_rect.y = y;
2234 image_rect.width = s->slice.width;
2235 image_rect.height = s->slice.height;
2236 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2237 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2238 s->slice.x + r.x - x, s->slice.y + r.y - y,
2239 r.width, r.height, r.x, r.y);
2241 else
2243 XRectangle clip_rect, image_rect, r;
2245 get_glyph_string_clip_rect (s, &clip_rect);
2246 image_rect.x = x;
2247 image_rect.y = y;
2248 image_rect.width = s->slice.width;
2249 image_rect.height = s->slice.height;
2250 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2251 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2252 s->slice.x + r.x - x, s->slice.y + r.y - y,
2253 r.width, r.height, r.x, r.y);
2255 /* When the image has a mask, we can expect that at
2256 least part of a mouse highlight or a block cursor will
2257 be visible. If the image doesn't have a mask, make
2258 a block cursor visible by drawing a rectangle around
2259 the image. I believe it's looking better if we do
2260 nothing here for mouse-face. */
2261 if (s->hl == DRAW_CURSOR)
2263 int r = s->img->relief;
2264 if (r < 0) r = -r;
2265 XDrawRectangle (s->display, s->window, s->gc,
2266 x - r, y - r,
2267 s->slice.width + r*2 - 1,
2268 s->slice.height + r*2 - 1);
2272 else
2273 /* Draw a rectangle if image could not be loaded. */
2274 XDrawRectangle (s->display, s->window, s->gc, x, y,
2275 s->slice.width - 1, s->slice.height - 1);
2279 /* Draw a relief around the image glyph string S. */
2281 static void
2282 x_draw_image_relief (s)
2283 struct glyph_string *s;
2285 int x0, y0, x1, y1, thick, raised_p;
2286 XRectangle r;
2287 int x = s->x;
2288 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2290 /* If first glyph of S has a left box line, start drawing it to the
2291 right of that line. */
2292 if (s->face->box != FACE_NO_BOX
2293 && s->first_glyph->left_box_line_p
2294 && s->slice.x == 0)
2295 x += abs (s->face->box_line_width);
2297 /* If there is a margin around the image, adjust x- and y-position
2298 by that margin. */
2299 if (s->slice.x == 0)
2300 x += s->img->hmargin;
2301 if (s->slice.y == 0)
2302 y += s->img->vmargin;
2304 if (s->hl == DRAW_IMAGE_SUNKEN
2305 || s->hl == DRAW_IMAGE_RAISED)
2307 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2308 raised_p = s->hl == DRAW_IMAGE_RAISED;
2310 else
2312 thick = abs (s->img->relief);
2313 raised_p = s->img->relief > 0;
2316 x0 = x - thick;
2317 y0 = y - thick;
2318 x1 = x + s->slice.width + thick - 1;
2319 y1 = y + s->slice.height + thick - 1;
2321 x_setup_relief_colors (s);
2322 get_glyph_string_clip_rect (s, &r);
2323 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
2324 s->slice.y == 0,
2325 s->slice.y + s->slice.height == s->img->height,
2326 s->slice.x == 0,
2327 s->slice.x + s->slice.width == s->img->width,
2328 &r);
2332 /* Draw the foreground of image glyph string S to PIXMAP. */
2334 static void
2335 x_draw_image_foreground_1 (s, pixmap)
2336 struct glyph_string *s;
2337 Pixmap pixmap;
2339 int x = 0;
2340 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2342 /* If first glyph of S has a left box line, start drawing it to the
2343 right of that line. */
2344 if (s->face->box != FACE_NO_BOX
2345 && s->first_glyph->left_box_line_p
2346 && s->slice.x == 0)
2347 x += abs (s->face->box_line_width);
2349 /* If there is a margin around the image, adjust x- and y-position
2350 by that margin. */
2351 if (s->slice.x == 0)
2352 x += s->img->hmargin;
2353 if (s->slice.y == 0)
2354 y += s->img->vmargin;
2356 if (s->img->pixmap)
2358 if (s->img->mask)
2360 /* We can't set both a clip mask and use XSetClipRectangles
2361 because the latter also sets a clip mask. We also can't
2362 trust on the shape extension to be available
2363 (XShapeCombineRegion). So, compute the rectangle to draw
2364 manually. */
2365 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2366 | GCFunction);
2367 XGCValues xgcv;
2369 xgcv.clip_mask = s->img->mask;
2370 xgcv.clip_x_origin = x - s->slice.x;
2371 xgcv.clip_y_origin = y - s->slice.y;
2372 xgcv.function = GXcopy;
2373 XChangeGC (s->display, s->gc, mask, &xgcv);
2375 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2376 s->slice.x, s->slice.y,
2377 s->slice.width, s->slice.height, x, y);
2378 XSetClipMask (s->display, s->gc, None);
2380 else
2382 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2383 s->slice.x, s->slice.y,
2384 s->slice.width, s->slice.height, x, y);
2386 /* When the image has a mask, we can expect that at
2387 least part of a mouse highlight or a block cursor will
2388 be visible. If the image doesn't have a mask, make
2389 a block cursor visible by drawing a rectangle around
2390 the image. I believe it's looking better if we do
2391 nothing here for mouse-face. */
2392 if (s->hl == DRAW_CURSOR)
2394 int r = s->img->relief;
2395 if (r < 0) r = -r;
2396 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2397 s->slice.width + r*2 - 1,
2398 s->slice.height + r*2 - 1);
2402 else
2403 /* Draw a rectangle if image could not be loaded. */
2404 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2405 s->slice.width - 1, s->slice.height - 1);
2409 /* Draw part of the background of glyph string S. X, Y, W, and H
2410 give the rectangle to draw. */
2412 static void
2413 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2414 struct glyph_string *s;
2415 int x, y, w, h;
2417 if (s->stippled_p)
2419 /* Fill background with a stipple pattern. */
2420 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2421 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2422 XSetFillStyle (s->display, s->gc, FillSolid);
2424 else
2425 x_clear_glyph_string_rect (s, x, y, w, h);
2429 /* Draw image glyph string S.
2431 s->y
2432 s->x +-------------------------
2433 | s->face->box
2435 | +-------------------------
2436 | | s->img->margin
2438 | | +-------------------
2439 | | | the image
2443 static void
2444 x_draw_image_glyph_string (s)
2445 struct glyph_string *s;
2447 int box_line_hwidth = abs (s->face->box_line_width);
2448 int box_line_vwidth = max (s->face->box_line_width, 0);
2449 int height;
2450 Pixmap pixmap = None;
2452 height = s->height;
2453 if (s->slice.y == 0)
2454 height -= box_line_vwidth;
2455 if (s->slice.y + s->slice.height >= s->img->height)
2456 height -= box_line_vwidth;
2458 /* Fill background with face under the image. Do it only if row is
2459 taller than image or if image has a clip mask to reduce
2460 flickering. */
2461 s->stippled_p = s->face->stipple != 0;
2462 if (height > s->slice.height
2463 || s->img->hmargin
2464 || s->img->vmargin
2465 || s->img->mask
2466 || s->img->pixmap == 0
2467 || s->width != s->background_width)
2469 if (s->img->mask)
2471 /* Create a pixmap as large as the glyph string. Fill it
2472 with the background color. Copy the image to it, using
2473 its mask. Copy the temporary pixmap to the display. */
2474 Screen *screen = FRAME_X_SCREEN (s->f);
2475 int depth = DefaultDepthOfScreen (screen);
2477 /* Create a pixmap as large as the glyph string. */
2478 pixmap = XCreatePixmap (s->display, s->window,
2479 s->background_width,
2480 s->height, depth);
2482 /* Don't clip in the following because we're working on the
2483 pixmap. */
2484 XSetClipMask (s->display, s->gc, None);
2486 /* Fill the pixmap with the background color/stipple. */
2487 if (s->stippled_p)
2489 /* Fill background with a stipple pattern. */
2490 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2491 XFillRectangle (s->display, pixmap, s->gc,
2492 0, 0, s->background_width, s->height);
2493 XSetFillStyle (s->display, s->gc, FillSolid);
2495 else
2497 XGCValues xgcv;
2498 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2499 &xgcv);
2500 XSetForeground (s->display, s->gc, xgcv.background);
2501 XFillRectangle (s->display, pixmap, s->gc,
2502 0, 0, s->background_width, s->height);
2503 XSetForeground (s->display, s->gc, xgcv.foreground);
2506 else
2508 int x = s->x;
2509 int y = s->y;
2511 if (s->first_glyph->left_box_line_p
2512 && s->slice.x == 0)
2513 x += box_line_hwidth;
2515 if (s->slice.y == 0)
2516 y += box_line_vwidth;
2518 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2521 s->background_filled_p = 1;
2524 /* Draw the foreground. */
2525 if (pixmap != None)
2527 x_draw_image_foreground_1 (s, pixmap);
2528 x_set_glyph_string_clipping (s);
2529 XCopyArea (s->display, pixmap, s->window, s->gc,
2530 0, 0, s->background_width, s->height, s->x, s->y);
2531 XFreePixmap (s->display, pixmap);
2533 else
2534 x_draw_image_foreground (s);
2536 /* If we must draw a relief around the image, do it. */
2537 if (s->img->relief
2538 || s->hl == DRAW_IMAGE_RAISED
2539 || s->hl == DRAW_IMAGE_SUNKEN)
2540 x_draw_image_relief (s);
2544 /* Draw stretch glyph string S. */
2546 static void
2547 x_draw_stretch_glyph_string (s)
2548 struct glyph_string *s;
2550 xassert (s->first_glyph->type == STRETCH_GLYPH);
2551 s->stippled_p = s->face->stipple != 0;
2553 if (s->hl == DRAW_CURSOR
2554 && !x_stretch_cursor_p)
2556 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2557 as wide as the stretch glyph. */
2558 int width = min (FRAME_COLUMN_WIDTH (s->f), s->background_width);
2560 /* Draw cursor. */
2561 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
2563 /* Clear rest using the GC of the original non-cursor face. */
2564 if (width < s->background_width)
2566 int x = s->x + width, y = s->y;
2567 int w = s->background_width - width, h = s->height;
2568 XRectangle r;
2569 GC gc;
2571 if (s->row->mouse_face_p
2572 && cursor_in_mouse_face_p (s->w))
2574 x_set_mouse_face_gc (s);
2575 gc = s->gc;
2577 else
2578 gc = s->face->gc;
2580 get_glyph_string_clip_rect (s, &r);
2581 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2583 if (s->face->stipple)
2585 /* Fill background with a stipple pattern. */
2586 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2587 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2588 XSetFillStyle (s->display, gc, FillSolid);
2590 else
2592 XGCValues xgcv;
2593 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2594 XSetForeground (s->display, gc, xgcv.background);
2595 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2596 XSetForeground (s->display, gc, xgcv.foreground);
2600 else if (!s->background_filled_p)
2601 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
2602 s->height);
2604 s->background_filled_p = 1;
2608 /* Draw glyph string S. */
2610 static void
2611 x_draw_glyph_string (s)
2612 struct glyph_string *s;
2614 int relief_drawn_p = 0;
2616 /* If S draws into the background of its successor, draw the
2617 background of the successor first so that S can draw into it.
2618 This makes S->next use XDrawString instead of XDrawImageString. */
2619 if (s->next && s->right_overhang && !s->for_overlaps)
2621 xassert (s->next->img == NULL);
2622 x_set_glyph_string_gc (s->next);
2623 x_set_glyph_string_clipping (s->next);
2624 x_draw_glyph_string_background (s->next, 1);
2627 /* Set up S->gc, set clipping and draw S. */
2628 x_set_glyph_string_gc (s);
2630 /* Draw relief (if any) in advance for char/composition so that the
2631 glyph string can be drawn over it. */
2632 if (!s->for_overlaps
2633 && s->face->box != FACE_NO_BOX
2634 && (s->first_glyph->type == CHAR_GLYPH
2635 || s->first_glyph->type == COMPOSITE_GLYPH))
2638 x_set_glyph_string_clipping (s);
2639 x_draw_glyph_string_background (s, 1);
2640 x_draw_glyph_string_box (s);
2641 x_set_glyph_string_clipping (s);
2642 relief_drawn_p = 1;
2644 else
2645 x_set_glyph_string_clipping (s);
2647 switch (s->first_glyph->type)
2649 case IMAGE_GLYPH:
2650 x_draw_image_glyph_string (s);
2651 break;
2653 case STRETCH_GLYPH:
2654 x_draw_stretch_glyph_string (s);
2655 break;
2657 case CHAR_GLYPH:
2658 if (s->for_overlaps)
2659 s->background_filled_p = 1;
2660 else
2661 x_draw_glyph_string_background (s, 0);
2662 x_draw_glyph_string_foreground (s);
2663 break;
2665 case COMPOSITE_GLYPH:
2666 if (s->for_overlaps || s->gidx > 0)
2667 s->background_filled_p = 1;
2668 else
2669 x_draw_glyph_string_background (s, 1);
2670 x_draw_composite_glyph_string_foreground (s);
2671 break;
2673 default:
2674 abort ();
2677 if (!s->for_overlaps)
2679 /* Draw underline. */
2680 if (s->face->underline_p)
2682 unsigned long tem, h;
2683 int y;
2685 /* Get the underline thickness. Default is 1 pixel. */
2686 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
2687 h = 1;
2689 /* Get the underline position. This is the recommended
2690 vertical offset in pixels from the baseline to the top of
2691 the underline. This is a signed value according to the
2692 specs, and its default is
2694 ROUND ((maximum descent) / 2), with
2695 ROUND(x) = floor (x + 0.5) */
2697 if (x_use_underline_position_properties
2698 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
2699 y = s->ybase + (long) tem;
2700 else if (s->face->font)
2701 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
2702 else
2703 y = s->y + s->height - h;
2705 if (s->face->underline_defaulted_p)
2706 XFillRectangle (s->display, s->window, s->gc,
2707 s->x, y, s->width, h);
2708 else
2710 XGCValues xgcv;
2711 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2712 XSetForeground (s->display, s->gc, s->face->underline_color);
2713 XFillRectangle (s->display, s->window, s->gc,
2714 s->x, y, s->width, h);
2715 XSetForeground (s->display, s->gc, xgcv.foreground);
2719 /* Draw overline. */
2720 if (s->face->overline_p)
2722 unsigned long dy = 0, h = 1;
2724 if (s->face->overline_color_defaulted_p)
2725 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2726 s->width, h);
2727 else
2729 XGCValues xgcv;
2730 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2731 XSetForeground (s->display, s->gc, s->face->overline_color);
2732 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2733 s->width, h);
2734 XSetForeground (s->display, s->gc, xgcv.foreground);
2738 /* Draw strike-through. */
2739 if (s->face->strike_through_p)
2741 unsigned long h = 1;
2742 unsigned long dy = (s->height - h) / 2;
2744 if (s->face->strike_through_color_defaulted_p)
2745 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2746 s->width, h);
2747 else
2749 XGCValues xgcv;
2750 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2751 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2752 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2753 s->width, h);
2754 XSetForeground (s->display, s->gc, xgcv.foreground);
2758 /* Draw relief if not yet drawn. */
2759 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2760 x_draw_glyph_string_box (s);
2763 /* Reset clipping. */
2764 XSetClipMask (s->display, s->gc, None);
2767 /* Shift display to make room for inserted glyphs. */
2769 void
2770 x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2771 struct frame *f;
2772 int x, y, width, height, shift_by;
2774 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2775 f->output_data.x->normal_gc,
2776 x, y, width, height,
2777 x + shift_by, y);
2780 /* Delete N glyphs at the nominal cursor position. Not implemented
2781 for X frames. */
2783 static void
2784 x_delete_glyphs (f, n)
2785 struct frame *f;
2786 register int n;
2788 abort ();
2792 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2793 If they are <= 0, this is probably an error. */
2795 void
2796 x_clear_area (dpy, window, x, y, width, height, exposures)
2797 Display *dpy;
2798 Window window;
2799 int x, y;
2800 int width, height;
2801 int exposures;
2803 xassert (width > 0 && height > 0);
2804 XClearArea (dpy, window, x, y, width, height, exposures);
2808 /* Clear an entire frame. */
2810 static void
2811 x_clear_frame (struct frame *f)
2813 /* Clearing the frame will erase any cursor, so mark them all as no
2814 longer visible. */
2815 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2816 output_cursor.hpos = output_cursor.vpos = 0;
2817 output_cursor.x = -1;
2819 /* We don't set the output cursor here because there will always
2820 follow an explicit cursor_to. */
2821 BLOCK_INPUT;
2822 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2824 /* We have to clear the scroll bars, too. If we have changed
2825 colors or something like that, then they should be notified. */
2826 x_scroll_bar_clear (f);
2828 XFlush (FRAME_X_DISPLAY (f));
2830 UNBLOCK_INPUT;
2835 /* Invert the middle quarter of the frame for .15 sec. */
2837 /* We use the select system call to do the waiting, so we have to make
2838 sure it's available. If it isn't, we just won't do visual bells. */
2840 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2843 /* Subtract the `struct timeval' values X and Y, storing the result in
2844 *RESULT. Return 1 if the difference is negative, otherwise 0. */
2846 static int
2847 timeval_subtract (result, x, y)
2848 struct timeval *result, x, y;
2850 /* Perform the carry for the later subtraction by updating y. This
2851 is safer because on some systems the tv_sec member is unsigned. */
2852 if (x.tv_usec < y.tv_usec)
2854 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
2855 y.tv_usec -= 1000000 * nsec;
2856 y.tv_sec += nsec;
2859 if (x.tv_usec - y.tv_usec > 1000000)
2861 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
2862 y.tv_usec += 1000000 * nsec;
2863 y.tv_sec -= nsec;
2866 /* Compute the time remaining to wait. tv_usec is certainly
2867 positive. */
2868 result->tv_sec = x.tv_sec - y.tv_sec;
2869 result->tv_usec = x.tv_usec - y.tv_usec;
2871 /* Return indication of whether the result should be considered
2872 negative. */
2873 return x.tv_sec < y.tv_sec;
2876 void
2877 XTflash (f)
2878 struct frame *f;
2880 BLOCK_INPUT;
2883 GC gc;
2885 /* Create a GC that will use the GXxor function to flip foreground
2886 pixels into background pixels. */
2888 XGCValues values;
2890 values.function = GXxor;
2891 values.foreground = (f->output_data.x->foreground_pixel
2892 ^ f->output_data.x->background_pixel);
2894 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2895 GCFunction | GCForeground, &values);
2899 /* Get the height not including a menu bar widget. */
2900 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
2901 /* Height of each line to flash. */
2902 int flash_height = FRAME_LINE_HEIGHT (f);
2903 /* These will be the left and right margins of the rectangles. */
2904 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
2905 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
2907 int width;
2909 /* Don't flash the area between a scroll bar and the frame
2910 edge it is next to. */
2911 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
2913 case vertical_scroll_bar_left:
2914 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2915 break;
2917 case vertical_scroll_bar_right:
2918 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2919 break;
2921 default:
2922 break;
2925 width = flash_right - flash_left;
2927 /* If window is tall, flash top and bottom line. */
2928 if (height > 3 * FRAME_LINE_HEIGHT (f))
2930 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2931 flash_left,
2932 (FRAME_INTERNAL_BORDER_WIDTH (f)
2933 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
2934 width, flash_height);
2935 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2936 flash_left,
2937 (height - flash_height
2938 - FRAME_INTERNAL_BORDER_WIDTH (f)),
2939 width, flash_height);
2941 else
2942 /* If it is short, flash it all. */
2943 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2944 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
2945 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
2947 x_flush (f);
2950 struct timeval wakeup;
2952 EMACS_GET_TIME (wakeup);
2954 /* Compute time to wait until, propagating carry from usecs. */
2955 wakeup.tv_usec += 150000;
2956 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
2957 wakeup.tv_usec %= 1000000;
2959 /* Keep waiting until past the time wakeup or any input gets
2960 available. */
2961 while (! detect_input_pending ())
2963 struct timeval current;
2964 struct timeval timeout;
2966 EMACS_GET_TIME (current);
2968 /* Break if result would be negative. */
2969 if (timeval_subtract (&current, wakeup, current))
2970 break;
2972 /* How long `select' should wait. */
2973 timeout.tv_sec = 0;
2974 timeout.tv_usec = 10000;
2976 /* Try to wait that long--but we might wake up sooner. */
2977 select (0, NULL, NULL, NULL, &timeout);
2981 /* If window is tall, flash top and bottom line. */
2982 if (height > 3 * FRAME_LINE_HEIGHT (f))
2984 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2985 flash_left,
2986 (FRAME_INTERNAL_BORDER_WIDTH (f)
2987 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
2988 width, flash_height);
2989 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2990 flash_left,
2991 (height - flash_height
2992 - FRAME_INTERNAL_BORDER_WIDTH (f)),
2993 width, flash_height);
2995 else
2996 /* If it is short, flash it all. */
2997 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2998 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
2999 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3001 XFreeGC (FRAME_X_DISPLAY (f), gc);
3002 x_flush (f);
3006 UNBLOCK_INPUT;
3009 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
3012 /* Make audible bell. */
3014 void
3015 XTring_bell ()
3017 struct frame *f = SELECTED_FRAME ();
3019 if (FRAME_X_DISPLAY (f))
3021 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3022 if (visible_bell)
3023 XTflash (f);
3024 else
3025 #endif
3027 BLOCK_INPUT;
3028 XBell (FRAME_X_DISPLAY (f), 0);
3029 XFlush (FRAME_X_DISPLAY (f));
3030 UNBLOCK_INPUT;
3036 /* Specify how many text lines, from the top of the window,
3037 should be affected by insert-lines and delete-lines operations.
3038 This, and those operations, are used only within an update
3039 that is bounded by calls to x_update_begin and x_update_end. */
3041 static void
3042 XTset_terminal_window (n)
3043 register int n;
3045 /* This function intentionally left blank. */
3050 /***********************************************************************
3051 Line Dance
3052 ***********************************************************************/
3054 /* Perform an insert-lines or delete-lines operation, inserting N
3055 lines or deleting -N lines at vertical position VPOS. */
3057 static void
3058 x_ins_del_lines (f, vpos, n)
3059 struct frame *f;
3060 int vpos, n;
3062 abort ();
3066 /* Scroll part of the display as described by RUN. */
3068 static void
3069 x_scroll_run (w, run)
3070 struct window *w;
3071 struct run *run;
3073 struct frame *f = XFRAME (w->frame);
3074 int x, y, width, height, from_y, to_y, bottom_y;
3076 /* Get frame-relative bounding box of the text display area of W,
3077 without mode lines. Include in this box the left and right
3078 fringe of W. */
3079 window_box (w, -1, &x, &y, &width, &height);
3081 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3082 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3083 bottom_y = y + height;
3085 if (to_y < from_y)
3087 /* Scrolling up. Make sure we don't copy part of the mode
3088 line at the bottom. */
3089 if (from_y + run->height > bottom_y)
3090 height = bottom_y - from_y;
3091 else
3092 height = run->height;
3094 else
3096 /* Scolling down. Make sure we don't copy over the mode line.
3097 at the bottom. */
3098 if (to_y + run->height > bottom_y)
3099 height = bottom_y - to_y;
3100 else
3101 height = run->height;
3104 BLOCK_INPUT;
3106 /* Cursor off. Will be switched on again in x_update_window_end. */
3107 updated_window = w;
3108 x_clear_cursor (w);
3110 XCopyArea (FRAME_X_DISPLAY (f),
3111 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3112 f->output_data.x->normal_gc,
3113 x, from_y,
3114 width, height,
3115 x, to_y);
3117 UNBLOCK_INPUT;
3122 /***********************************************************************
3123 Exposure Events
3124 ***********************************************************************/
3127 static void
3128 frame_highlight (f)
3129 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 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3137 f->output_data.x->border_pixel);
3138 UNBLOCK_INPUT;
3139 x_update_cursor (f, 1);
3142 static void
3143 frame_unhighlight (f)
3144 struct frame *f;
3146 /* We used to only do this if Vx_no_window_manager was non-nil, but
3147 the ICCCM (section 4.1.6) says that the window's border pixmap
3148 and border pixel are window attributes which are "private to the
3149 client", so we can always change it to whatever we want. */
3150 BLOCK_INPUT;
3151 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3152 f->output_data.x->border_tile);
3153 UNBLOCK_INPUT;
3154 x_update_cursor (f, 1);
3157 /* The focus has changed. Update the frames as necessary to reflect
3158 the new situation. Note that we can't change the selected frame
3159 here, because the Lisp code we are interrupting might become confused.
3160 Each event gets marked with the frame in which it occurred, so the
3161 Lisp code can tell when the switch took place by examining the events. */
3163 static void
3164 x_new_focus_frame (dpyinfo, frame)
3165 struct x_display_info *dpyinfo;
3166 struct frame *frame;
3168 struct frame *old_focus = dpyinfo->x_focus_frame;
3170 if (frame != dpyinfo->x_focus_frame)
3172 /* Set this before calling other routines, so that they see
3173 the correct value of x_focus_frame. */
3174 dpyinfo->x_focus_frame = frame;
3176 if (old_focus && old_focus->auto_lower)
3177 x_lower_frame (old_focus);
3179 #if 0
3180 selected_frame = frame;
3181 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
3182 selected_frame);
3183 Fselect_window (selected_frame->selected_window, Qnil);
3184 choose_minibuf_frame ();
3185 #endif /* ! 0 */
3187 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3188 pending_autoraise_frame = dpyinfo->x_focus_frame;
3189 else
3190 pending_autoraise_frame = 0;
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 (type, state, dpyinfo, frame, bufp)
3202 int type;
3203 int state;
3204 struct x_display_info *dpyinfo;
3205 struct frame *frame;
3206 struct input_event *bufp;
3208 if (type == FocusIn)
3210 if (dpyinfo->x_focus_event_frame != frame)
3212 x_new_focus_frame (dpyinfo, frame);
3213 dpyinfo->x_focus_event_frame = frame;
3215 /* Don't stop displaying the initial startup message
3216 for a switch-frame event we don't need. */
3217 if (GC_NILP (Vterminal_frame)
3218 && GC_CONSP (Vframe_list)
3219 && !GC_NILP (XCDR (Vframe_list)))
3221 bufp->kind = FOCUS_IN_EVENT;
3222 XSETFRAME (bufp->frame_or_window, frame);
3226 frame->output_data.x->focus_state |= state;
3228 #ifdef HAVE_X_I18N
3229 if (FRAME_XIC (frame))
3230 XSetICFocus (FRAME_XIC (frame));
3231 #endif
3233 else if (type == FocusOut)
3235 frame->output_data.x->focus_state &= ~state;
3237 if (dpyinfo->x_focus_event_frame == frame)
3239 dpyinfo->x_focus_event_frame = 0;
3240 x_new_focus_frame (dpyinfo, 0);
3243 #ifdef HAVE_X_I18N
3244 if (FRAME_XIC (frame))
3245 XUnsetICFocus (FRAME_XIC (frame));
3246 #endif
3250 /* The focus may have changed. Figure out if it is a real focus change,
3251 by checking both FocusIn/Out and Enter/LeaveNotify events.
3253 Returns FOCUS_IN_EVENT event in *BUFP. */
3255 static void
3256 x_detect_focus_change (dpyinfo, event, bufp)
3257 struct x_display_info *dpyinfo;
3258 XEvent *event;
3259 struct input_event *bufp;
3261 struct frame *frame;
3263 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3264 if (! frame)
3265 return;
3267 switch (event->type)
3269 case EnterNotify:
3270 case LeaveNotify:
3272 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3273 int focus_state
3274 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3276 if (event->xcrossing.detail != NotifyInferior
3277 && event->xcrossing.focus
3278 && ! (focus_state & FOCUS_EXPLICIT))
3279 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3280 FOCUS_IMPLICIT,
3281 dpyinfo, frame, bufp);
3283 break;
3285 case FocusIn:
3286 case FocusOut:
3287 x_focus_changed (event->type,
3288 (event->xfocus.detail == NotifyPointer ?
3289 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3290 dpyinfo, frame, bufp);
3291 break;
3296 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3298 void
3299 x_mouse_leave (dpyinfo)
3300 struct x_display_info *dpyinfo;
3302 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3305 /* The focus has changed, or we have redirected a frame's focus to
3306 another frame (this happens when a frame uses a surrogate
3307 mini-buffer frame). Shift the highlight as appropriate.
3309 The FRAME argument doesn't necessarily have anything to do with which
3310 frame is being highlighted or un-highlighted; we only use it to find
3311 the appropriate X display info. */
3313 static void
3314 XTframe_rehighlight (frame)
3315 struct frame *frame;
3317 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3320 static void
3321 x_frame_rehighlight (dpyinfo)
3322 struct x_display_info *dpyinfo;
3324 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3326 if (dpyinfo->x_focus_frame)
3328 dpyinfo->x_highlight_frame
3329 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3330 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3331 : dpyinfo->x_focus_frame);
3332 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3334 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3335 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3338 else
3339 dpyinfo->x_highlight_frame = 0;
3341 if (dpyinfo->x_highlight_frame != old_highlight)
3343 if (old_highlight)
3344 frame_unhighlight (old_highlight);
3345 if (dpyinfo->x_highlight_frame)
3346 frame_highlight (dpyinfo->x_highlight_frame);
3352 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3354 /* Initialize mode_switch_bit and modifier_meaning. */
3355 static void
3356 x_find_modifier_meanings (dpyinfo)
3357 struct x_display_info *dpyinfo;
3359 int min_code, max_code;
3360 KeySym *syms;
3361 int syms_per_code;
3362 XModifierKeymap *mods;
3364 dpyinfo->meta_mod_mask = 0;
3365 dpyinfo->shift_lock_mask = 0;
3366 dpyinfo->alt_mod_mask = 0;
3367 dpyinfo->super_mod_mask = 0;
3368 dpyinfo->hyper_mod_mask = 0;
3370 #ifdef HAVE_X11R4
3371 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3372 #else
3373 min_code = dpyinfo->display->min_keycode;
3374 max_code = dpyinfo->display->max_keycode;
3375 #endif
3377 syms = XGetKeyboardMapping (dpyinfo->display,
3378 min_code, max_code - min_code + 1,
3379 &syms_per_code);
3380 mods = XGetModifierMapping (dpyinfo->display);
3382 /* Scan the modifier table to see which modifier bits the Meta and
3383 Alt keysyms are on. */
3385 int row, col; /* The row and column in the modifier table. */
3386 int found_alt_or_meta;
3388 for (row = 3; row < 8; row++)
3390 found_alt_or_meta = 0;
3391 for (col = 0; col < mods->max_keypermod; col++)
3393 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3395 /* Zeroes are used for filler. Skip them. */
3396 if (code == 0)
3397 continue;
3399 /* Are any of this keycode's keysyms a meta key? */
3401 int code_col;
3403 for (code_col = 0; code_col < syms_per_code; code_col++)
3405 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3407 switch (sym)
3409 case XK_Meta_L:
3410 case XK_Meta_R:
3411 found_alt_or_meta = 1;
3412 dpyinfo->meta_mod_mask |= (1 << row);
3413 break;
3415 case XK_Alt_L:
3416 case XK_Alt_R:
3417 found_alt_or_meta = 1;
3418 dpyinfo->alt_mod_mask |= (1 << row);
3419 break;
3421 case XK_Hyper_L:
3422 case XK_Hyper_R:
3423 if (!found_alt_or_meta)
3424 dpyinfo->hyper_mod_mask |= (1 << row);
3425 code_col = syms_per_code;
3426 col = mods->max_keypermod;
3427 break;
3429 case XK_Super_L:
3430 case XK_Super_R:
3431 if (!found_alt_or_meta)
3432 dpyinfo->super_mod_mask |= (1 << row);
3433 code_col = syms_per_code;
3434 col = mods->max_keypermod;
3435 break;
3437 case XK_Shift_Lock:
3438 /* Ignore this if it's not on the lock modifier. */
3439 if (!found_alt_or_meta && ((1 << row) == LockMask))
3440 dpyinfo->shift_lock_mask = LockMask;
3441 code_col = syms_per_code;
3442 col = mods->max_keypermod;
3443 break;
3451 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3452 if (! dpyinfo->meta_mod_mask)
3454 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3455 dpyinfo->alt_mod_mask = 0;
3458 /* If some keys are both alt and meta,
3459 make them just meta, not alt. */
3460 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3462 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3465 XFree ((char *) syms);
3466 XFreeModifiermap (mods);
3469 /* Convert between the modifier bits X uses and the modifier bits
3470 Emacs uses. */
3472 unsigned int
3473 x_x_to_emacs_modifiers (dpyinfo, state)
3474 struct x_display_info *dpyinfo;
3475 unsigned int state;
3477 EMACS_UINT mod_meta = meta_modifier;
3478 EMACS_UINT mod_alt = alt_modifier;
3479 EMACS_UINT mod_hyper = hyper_modifier;
3480 EMACS_UINT mod_super = super_modifier;
3481 Lisp_Object tem;
3483 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3484 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3485 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3486 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3487 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3488 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3489 tem = Fget (Vx_super_keysym, Qmodifier_value);
3490 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3493 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3494 | ((state & ControlMask) ? ctrl_modifier : 0)
3495 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3496 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3497 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3498 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3501 static unsigned int
3502 x_emacs_to_x_modifiers (dpyinfo, state)
3503 struct x_display_info *dpyinfo;
3504 unsigned int state;
3506 EMACS_UINT mod_meta = meta_modifier;
3507 EMACS_UINT mod_alt = alt_modifier;
3508 EMACS_UINT mod_hyper = hyper_modifier;
3509 EMACS_UINT mod_super = super_modifier;
3511 Lisp_Object tem;
3513 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3514 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3515 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3516 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3517 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3518 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3519 tem = Fget (Vx_super_keysym, Qmodifier_value);
3520 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3523 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3524 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3525 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3526 | ((state & shift_modifier) ? ShiftMask : 0)
3527 | ((state & ctrl_modifier) ? ControlMask : 0)
3528 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3531 /* Convert a keysym to its name. */
3533 char *
3534 x_get_keysym_name (keysym)
3535 KeySym keysym;
3537 char *value;
3539 BLOCK_INPUT;
3540 value = XKeysymToString (keysym);
3541 UNBLOCK_INPUT;
3543 return value;
3548 /* Mouse clicks and mouse movement. Rah. */
3550 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3552 If the event is a button press, then note that we have grabbed
3553 the mouse. */
3555 static Lisp_Object
3556 construct_mouse_click (result, event, f)
3557 struct input_event *result;
3558 XButtonEvent *event;
3559 struct frame *f;
3561 /* Make the event type NO_EVENT; we'll change that when we decide
3562 otherwise. */
3563 result->kind = MOUSE_CLICK_EVENT;
3564 result->code = event->button - Button1;
3565 result->timestamp = event->time;
3566 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3567 event->state)
3568 | (event->type == ButtonRelease
3569 ? up_modifier
3570 : down_modifier));
3572 XSETINT (result->x, event->x);
3573 XSETINT (result->y, event->y);
3574 XSETFRAME (result->frame_or_window, f);
3575 result->arg = Qnil;
3576 return Qnil;
3580 /* Function to report a mouse movement to the mainstream Emacs code.
3581 The input handler calls this.
3583 We have received a mouse movement event, which is given in *event.
3584 If the mouse is over a different glyph than it was last time, tell
3585 the mainstream emacs code by setting mouse_moved. If not, ask for
3586 another motion event, so we can check again the next time it moves. */
3588 static XMotionEvent last_mouse_motion_event;
3589 static Lisp_Object last_mouse_motion_frame;
3591 static int
3592 note_mouse_movement (frame, event)
3593 FRAME_PTR frame;
3594 XMotionEvent *event;
3596 last_mouse_movement_time = event->time;
3597 last_mouse_motion_event = *event;
3598 XSETFRAME (last_mouse_motion_frame, frame);
3600 if (!FRAME_X_OUTPUT (frame))
3601 return 0;
3603 if (event->window != FRAME_X_WINDOW (frame))
3605 frame->mouse_moved = 1;
3606 last_mouse_scroll_bar = Qnil;
3607 note_mouse_highlight (frame, -1, -1);
3608 last_mouse_glyph_frame = 0;
3609 return 1;
3613 /* Has the mouse moved off the glyph it was on at the last sighting? */
3614 if (frame != last_mouse_glyph_frame
3615 || event->x < last_mouse_glyph.x
3616 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3617 || event->y < last_mouse_glyph.y
3618 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3620 frame->mouse_moved = 1;
3621 last_mouse_scroll_bar = Qnil;
3622 note_mouse_highlight (frame, event->x, event->y);
3623 /* Remember which glyph we're now on. */
3624 remember_mouse_glyph (frame, event->x, event->y, &last_mouse_glyph);
3625 last_mouse_glyph_frame = frame;
3626 return 1;
3629 return 0;
3633 /************************************************************************
3634 Mouse Face
3635 ************************************************************************/
3637 static void
3638 redo_mouse_highlight ()
3640 if (!NILP (last_mouse_motion_frame)
3641 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3642 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3643 last_mouse_motion_event.x,
3644 last_mouse_motion_event.y);
3649 /* Return the current position of the mouse.
3650 *FP should be a frame which indicates which display to ask about.
3652 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3653 and *PART to the frame, window, and scroll bar part that the mouse
3654 is over. Set *X and *Y to the portion and whole of the mouse's
3655 position on the scroll bar.
3657 If the mouse movement started elsewhere, set *FP to the frame the
3658 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3659 the mouse is over.
3661 Set *TIME to the server time-stamp for the time at which the mouse
3662 was at this position.
3664 Don't store anything if we don't have a valid set of values to report.
3666 This clears the mouse_moved flag, so we can wait for the next mouse
3667 movement. */
3669 static void
3670 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3671 FRAME_PTR *fp;
3672 int insist;
3673 Lisp_Object *bar_window;
3674 enum scroll_bar_part *part;
3675 Lisp_Object *x, *y;
3676 unsigned long *time;
3678 FRAME_PTR f1;
3680 BLOCK_INPUT;
3682 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3683 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3684 else
3686 Window root;
3687 int root_x, root_y;
3689 Window dummy_window;
3690 int dummy;
3692 Lisp_Object frame, tail;
3694 /* Clear the mouse-moved flag for every frame on this display. */
3695 FOR_EACH_FRAME (tail, frame)
3696 if (FRAME_X_P (XFRAME (frame))
3697 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3698 XFRAME (frame)->mouse_moved = 0;
3700 last_mouse_scroll_bar = Qnil;
3702 /* Figure out which root window we're on. */
3703 XQueryPointer (FRAME_X_DISPLAY (*fp),
3704 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3706 /* The root window which contains the pointer. */
3707 &root,
3709 /* Trash which we can't trust if the pointer is on
3710 a different screen. */
3711 &dummy_window,
3713 /* The position on that root window. */
3714 &root_x, &root_y,
3716 /* More trash we can't trust. */
3717 &dummy, &dummy,
3719 /* Modifier keys and pointer buttons, about which
3720 we don't care. */
3721 (unsigned int *) &dummy);
3723 /* Now we have a position on the root; find the innermost window
3724 containing the pointer. */
3726 Window win, child;
3727 int win_x, win_y;
3728 int parent_x = 0, parent_y = 0;
3729 int count;
3731 win = root;
3733 /* XTranslateCoordinates can get errors if the window
3734 structure is changing at the same time this function
3735 is running. So at least we must not crash from them. */
3737 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
3739 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3740 && FRAME_LIVE_P (last_mouse_frame))
3742 /* If mouse was grabbed on a frame, give coords for that frame
3743 even if the mouse is now outside it. */
3744 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3746 /* From-window, to-window. */
3747 root, FRAME_X_WINDOW (last_mouse_frame),
3749 /* From-position, to-position. */
3750 root_x, root_y, &win_x, &win_y,
3752 /* Child of win. */
3753 &child);
3754 f1 = last_mouse_frame;
3756 else
3758 while (1)
3760 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3762 /* From-window, to-window. */
3763 root, win,
3765 /* From-position, to-position. */
3766 root_x, root_y, &win_x, &win_y,
3768 /* Child of win. */
3769 &child);
3771 if (child == None || child == win)
3772 break;
3774 win = child;
3775 parent_x = win_x;
3776 parent_y = win_y;
3779 /* Now we know that:
3780 win is the innermost window containing the pointer
3781 (XTC says it has no child containing the pointer),
3782 win_x and win_y are the pointer's position in it
3783 (XTC did this the last time through), and
3784 parent_x and parent_y are the pointer's position in win's parent.
3785 (They are what win_x and win_y were when win was child.
3786 If win is the root window, it has no parent, and
3787 parent_{x,y} are invalid, but that's okay, because we'll
3788 never use them in that case.) */
3790 /* Is win one of our frames? */
3791 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3793 #ifdef USE_X_TOOLKIT
3794 /* If we end up with the menu bar window, say it's not
3795 on the frame. */
3796 if (f1 != NULL
3797 && f1->output_data.x->menubar_widget
3798 && win == XtWindow (f1->output_data.x->menubar_widget))
3799 f1 = NULL;
3800 #endif /* USE_X_TOOLKIT */
3803 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3804 f1 = 0;
3806 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
3808 /* If not, is it one of our scroll bars? */
3809 if (! f1)
3811 struct scroll_bar *bar;
3813 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
3815 if (bar)
3817 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3818 win_x = parent_x;
3819 win_y = parent_y;
3823 if (f1 == 0 && insist > 0)
3824 f1 = SELECTED_FRAME ();
3826 if (f1)
3828 /* Ok, we found a frame. Store all the values.
3829 last_mouse_glyph is a rectangle used to reduce the
3830 generation of mouse events. To not miss any motion
3831 events, we must divide the frame into rectangles of the
3832 size of the smallest character that could be displayed
3833 on it, i.e. into the same rectangles that matrices on
3834 the frame are divided into. */
3836 remember_mouse_glyph (f1, win_x, win_y, &last_mouse_glyph);
3837 last_mouse_glyph_frame = f1;
3839 *bar_window = Qnil;
3840 *part = 0;
3841 *fp = f1;
3842 XSETINT (*x, win_x);
3843 XSETINT (*y, win_y);
3844 *time = last_mouse_movement_time;
3849 UNBLOCK_INPUT;
3854 /***********************************************************************
3855 Scroll bars
3856 ***********************************************************************/
3858 /* Scroll bar support. */
3860 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
3861 manages it.
3862 This can be called in GC, so we have to make sure to strip off mark
3863 bits. */
3865 static struct scroll_bar *
3866 x_window_to_scroll_bar (display, window_id)
3867 Display *display;
3868 Window window_id;
3870 Lisp_Object tail;
3872 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3873 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
3874 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
3876 for (tail = Vframe_list;
3877 XGCTYPE (tail) == Lisp_Cons;
3878 tail = XCDR (tail))
3880 Lisp_Object frame, bar, condemned;
3882 frame = XCAR (tail);
3883 /* All elements of Vframe_list should be frames. */
3884 if (! GC_FRAMEP (frame))
3885 abort ();
3887 if (! FRAME_X_P (XFRAME (frame)))
3888 continue;
3890 /* Scan this frame's scroll bar list for a scroll bar with the
3891 right window ID. */
3892 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3893 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3894 /* This trick allows us to search both the ordinary and
3895 condemned scroll bar lists with one loop. */
3896 ! GC_NILP (bar) || (bar = condemned,
3897 condemned = Qnil,
3898 ! GC_NILP (bar));
3899 bar = XSCROLL_BAR (bar)->next)
3900 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id &&
3901 FRAME_X_DISPLAY (XFRAME (frame)) == display)
3902 return XSCROLL_BAR (bar);
3905 return 0;
3909 #if defined USE_LUCID
3911 /* Return the Lucid menu bar WINDOW is part of. Return null
3912 if WINDOW is not part of a menu bar. */
3914 static Widget
3915 x_window_to_menu_bar (window)
3916 Window window;
3918 Lisp_Object tail;
3920 for (tail = Vframe_list;
3921 XGCTYPE (tail) == Lisp_Cons;
3922 tail = XCDR (tail))
3924 if (FRAME_X_P (XFRAME (XCAR (tail))))
3926 Lisp_Object frame = XCAR (tail);
3927 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
3929 if (menu_bar && xlwmenu_window_p (menu_bar, window))
3930 return menu_bar;
3934 return NULL;
3937 #endif /* USE_LUCID */
3940 /************************************************************************
3941 Toolkit scroll bars
3942 ************************************************************************/
3944 #ifdef USE_TOOLKIT_SCROLL_BARS
3946 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
3947 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
3948 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
3949 struct scroll_bar *));
3950 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
3951 int, int, int));
3954 /* Lisp window being scrolled. Set when starting to interact with
3955 a toolkit scroll bar, reset to nil when ending the interaction. */
3957 static Lisp_Object window_being_scrolled;
3959 /* Last scroll bar part sent in xm_scroll_callback. */
3961 static int last_scroll_bar_part;
3963 /* Whether this is an Xaw with arrow-scrollbars. This should imply
3964 that movements of 1/20 of the screen size are mapped to up/down. */
3966 #ifndef USE_GTK
3967 /* Id of action hook installed for scroll bars. */
3969 static XtActionHookId action_hook_id;
3971 static Boolean xaw3d_arrow_scroll;
3973 /* Whether the drag scrolling maintains the mouse at the top of the
3974 thumb. If not, resizing the thumb needs to be done more carefully
3975 to avoid jerkyness. */
3977 static Boolean xaw3d_pick_top;
3979 /* Action hook installed via XtAppAddActionHook when toolkit scroll
3980 bars are used.. The hook is responsible for detecting when
3981 the user ends an interaction with the scroll bar, and generates
3982 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
3984 static void
3985 xt_action_hook (widget, client_data, action_name, event, params,
3986 num_params)
3987 Widget widget;
3988 XtPointer client_data;
3989 String action_name;
3990 XEvent *event;
3991 String *params;
3992 Cardinal *num_params;
3994 int scroll_bar_p;
3995 char *end_action;
3997 #ifdef USE_MOTIF
3998 scroll_bar_p = XmIsScrollBar (widget);
3999 end_action = "Release";
4000 #else /* !USE_MOTIF i.e. use Xaw */
4001 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4002 end_action = "EndScroll";
4003 #endif /* USE_MOTIF */
4005 if (scroll_bar_p
4006 && strcmp (action_name, end_action) == 0
4007 && WINDOWP (window_being_scrolled))
4009 struct window *w;
4011 x_send_scroll_bar_event (window_being_scrolled,
4012 scroll_bar_end_scroll, 0, 0);
4013 w = XWINDOW (window_being_scrolled);
4015 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
4017 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4018 /* The thumb size is incorrect while dragging: fix it. */
4019 set_vertical_scroll_bar (w);
4021 window_being_scrolled = Qnil;
4022 last_scroll_bar_part = -1;
4024 /* Xt timeouts no longer needed. */
4025 toolkit_scroll_bar_interaction = 0;
4028 #endif /* not USE_GTK */
4030 /* A vector of windows used for communication between
4031 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4033 static struct window **scroll_bar_windows;
4034 static int scroll_bar_windows_size;
4037 /* Send a client message with message type Xatom_Scrollbar for a
4038 scroll action to the frame of WINDOW. PART is a value identifying
4039 the part of the scroll bar that was clicked on. PORTION is the
4040 amount to scroll of a whole of WHOLE. */
4042 static void
4043 x_send_scroll_bar_event (window, part, portion, whole)
4044 Lisp_Object window;
4045 int part, portion, whole;
4047 XEvent event;
4048 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4049 struct window *w = XWINDOW (window);
4050 struct frame *f = XFRAME (w->frame);
4051 int i;
4053 BLOCK_INPUT;
4055 /* Construct a ClientMessage event to send to the frame. */
4056 ev->type = ClientMessage;
4057 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4058 ev->display = FRAME_X_DISPLAY (f);
4059 ev->window = FRAME_X_WINDOW (f);
4060 ev->format = 32;
4062 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4063 not enough to store a pointer or Lisp_Object on a 64 bit system.
4064 So, store the window in scroll_bar_windows and pass the index
4065 into that array in the event. */
4066 for (i = 0; i < scroll_bar_windows_size; ++i)
4067 if (scroll_bar_windows[i] == NULL)
4068 break;
4070 if (i == scroll_bar_windows_size)
4072 int new_size = max (10, 2 * scroll_bar_windows_size);
4073 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4074 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4076 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4077 nbytes);
4078 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4079 scroll_bar_windows_size = new_size;
4082 scroll_bar_windows[i] = w;
4083 ev->data.l[0] = (long) i;
4084 ev->data.l[1] = (long) part;
4085 ev->data.l[2] = (long) 0;
4086 ev->data.l[3] = (long) portion;
4087 ev->data.l[4] = (long) whole;
4089 /* Make Xt timeouts work while the scroll bar is active. */
4090 toolkit_scroll_bar_interaction = 1;
4092 /* Setting the event mask to zero means that the message will
4093 be sent to the client that created the window, and if that
4094 window no longer exists, no event will be sent. */
4095 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4096 UNBLOCK_INPUT;
4100 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4101 in *IEVENT. */
4103 static void
4104 x_scroll_bar_to_input_event (event, ievent)
4105 XEvent *event;
4106 struct input_event *ievent;
4108 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4109 Lisp_Object window;
4110 struct frame *f;
4111 struct window *w;
4113 w = scroll_bar_windows[ev->data.l[0]];
4114 scroll_bar_windows[ev->data.l[0]] = NULL;
4116 XSETWINDOW (window, w);
4117 f = XFRAME (w->frame);
4119 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4120 ievent->frame_or_window = window;
4121 ievent->arg = Qnil;
4122 #ifdef USE_GTK
4123 ievent->timestamp = CurrentTime;
4124 #else
4125 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4126 #endif
4127 ievent->part = ev->data.l[1];
4128 ievent->code = ev->data.l[2];
4129 ievent->x = make_number ((int) ev->data.l[3]);
4130 ievent->y = make_number ((int) ev->data.l[4]);
4131 ievent->modifiers = 0;
4135 #ifdef USE_MOTIF
4137 /* Minimum and maximum values used for Motif scroll bars. */
4139 #define XM_SB_MAX 10000000
4142 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4143 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4144 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4146 static void
4147 xm_scroll_callback (widget, client_data, call_data)
4148 Widget widget;
4149 XtPointer client_data, call_data;
4151 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4152 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4153 int part = -1, whole = 0, portion = 0;
4155 switch (cs->reason)
4157 case XmCR_DECREMENT:
4158 bar->dragging = Qnil;
4159 part = scroll_bar_up_arrow;
4160 break;
4162 case XmCR_INCREMENT:
4163 bar->dragging = Qnil;
4164 part = scroll_bar_down_arrow;
4165 break;
4167 case XmCR_PAGE_DECREMENT:
4168 bar->dragging = Qnil;
4169 part = scroll_bar_above_handle;
4170 break;
4172 case XmCR_PAGE_INCREMENT:
4173 bar->dragging = Qnil;
4174 part = scroll_bar_below_handle;
4175 break;
4177 case XmCR_TO_TOP:
4178 bar->dragging = Qnil;
4179 part = scroll_bar_to_top;
4180 break;
4182 case XmCR_TO_BOTTOM:
4183 bar->dragging = Qnil;
4184 part = scroll_bar_to_bottom;
4185 break;
4187 case XmCR_DRAG:
4189 int slider_size;
4191 /* Get the slider size. */
4192 BLOCK_INPUT;
4193 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4194 UNBLOCK_INPUT;
4196 whole = XM_SB_MAX - slider_size;
4197 portion = min (cs->value, whole);
4198 part = scroll_bar_handle;
4199 bar->dragging = make_number (cs->value);
4201 break;
4203 case XmCR_VALUE_CHANGED:
4204 break;
4207 if (part >= 0)
4209 window_being_scrolled = bar->window;
4210 last_scroll_bar_part = part;
4211 x_send_scroll_bar_event (bar->window, part, portion, whole);
4216 #else /* !USE_MOTIF, i.e. Xaw or GTK */
4217 #ifdef USE_GTK
4218 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4219 bar widget. DATA is a pointer to the scroll_bar structure. */
4221 static void
4222 xg_scroll_callback (widget, data)
4223 GtkRange *widget;
4224 gpointer data;
4226 struct scroll_bar *bar = (struct scroll_bar *) data;
4227 gdouble previous;
4228 gdouble position;
4229 gdouble *p;
4230 int diff;
4232 int part = -1, whole = 0, portion = 0;
4233 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (widget));
4235 position = gtk_adjustment_get_value (adj);
4237 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
4238 if (! p)
4240 p = (gdouble*) xmalloc (sizeof (gdouble));
4241 *p = XG_SB_MIN;
4242 g_object_set_data (G_OBJECT (widget), XG_LAST_SB_DATA, p);
4245 previous = *p;
4246 *p = position;
4248 if (xg_ignore_gtk_scrollbar) return;
4250 diff = (int) (position - previous);
4252 if (diff == (int) adj->step_increment)
4254 part = scroll_bar_down_arrow;
4255 bar->dragging = Qnil;
4257 else if (-diff == (int) adj->step_increment)
4259 part = scroll_bar_up_arrow;
4260 bar->dragging = Qnil;
4262 else if (diff == (int) adj->page_increment)
4264 part = scroll_bar_below_handle;
4265 bar->dragging = Qnil;
4267 else if (-diff == (int) adj->page_increment)
4269 part = scroll_bar_above_handle;
4270 bar->dragging = Qnil;
4272 else
4274 part = scroll_bar_handle;
4275 whole = adj->upper - adj->page_size;
4276 portion = min ((int)position, whole);
4277 bar->dragging = make_number ((int)portion);
4280 if (part >= 0)
4282 window_being_scrolled = bar->window;
4283 last_scroll_bar_part = part;
4284 x_send_scroll_bar_event (bar->window, part, portion, whole);
4288 #else /* not USE_GTK */
4290 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4291 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4292 scroll bar struct. CALL_DATA is a pointer to a float saying where
4293 the thumb is. */
4295 static void
4296 xaw_jump_callback (widget, client_data, call_data)
4297 Widget widget;
4298 XtPointer client_data, call_data;
4300 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4301 float top = *(float *) call_data;
4302 float shown;
4303 int whole, portion, height;
4304 int part;
4306 /* Get the size of the thumb, a value between 0 and 1. */
4307 BLOCK_INPUT;
4308 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4309 UNBLOCK_INPUT;
4311 whole = 10000000;
4312 portion = shown < 1 ? top * whole : 0;
4314 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
4315 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4316 the bottom, so we force the scrolling whenever we see that we're
4317 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4318 we try to ensure that we always stay two pixels away from the
4319 bottom). */
4320 part = scroll_bar_down_arrow;
4321 else
4322 part = scroll_bar_handle;
4324 window_being_scrolled = bar->window;
4325 bar->dragging = make_number (portion);
4326 last_scroll_bar_part = part;
4327 x_send_scroll_bar_event (bar->window, part, portion, whole);
4331 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4332 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4333 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4334 the scroll bar. CALL_DATA is an integer specifying the action that
4335 has taken place. Its magnitude is in the range 0..height of the
4336 scroll bar. Negative values mean scroll towards buffer start.
4337 Values < height of scroll bar mean line-wise movement. */
4339 static void
4340 xaw_scroll_callback (widget, client_data, call_data)
4341 Widget widget;
4342 XtPointer client_data, call_data;
4344 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4345 /* The position really is stored cast to a pointer. */
4346 int position = (long) call_data;
4347 Dimension height;
4348 int part;
4350 /* Get the height of the scroll bar. */
4351 BLOCK_INPUT;
4352 XtVaGetValues (widget, XtNheight, &height, NULL);
4353 UNBLOCK_INPUT;
4355 if (abs (position) >= height)
4356 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4358 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4359 it maps line-movement to call_data = max(5, height/20). */
4360 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
4361 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4362 else
4363 part = scroll_bar_move_ratio;
4365 window_being_scrolled = bar->window;
4366 bar->dragging = Qnil;
4367 last_scroll_bar_part = part;
4368 x_send_scroll_bar_event (bar->window, part, position, height);
4371 #endif /* not USE_GTK */
4372 #endif /* not USE_MOTIF */
4374 #define SCROLL_BAR_NAME "verticalScrollBar"
4376 /* Create the widget for scroll bar BAR on frame F. Record the widget
4377 and X window of the scroll bar in BAR. */
4379 #ifdef USE_GTK
4380 static void
4381 x_create_toolkit_scroll_bar (f, bar)
4382 struct frame *f;
4383 struct scroll_bar *bar;
4385 char *scroll_bar_name = SCROLL_BAR_NAME;
4387 BLOCK_INPUT;
4388 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4389 scroll_bar_name);
4390 UNBLOCK_INPUT;
4393 #else /* not USE_GTK */
4395 static void
4396 x_create_toolkit_scroll_bar (f, bar)
4397 struct frame *f;
4398 struct scroll_bar *bar;
4400 Window xwindow;
4401 Widget widget;
4402 Arg av[20];
4403 int ac = 0;
4404 char *scroll_bar_name = SCROLL_BAR_NAME;
4405 unsigned long pixel;
4407 BLOCK_INPUT;
4409 #ifdef USE_MOTIF
4410 /* Set resources. Create the widget. */
4411 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4412 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4413 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4414 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4415 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4416 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4417 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4419 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4420 if (pixel != -1)
4422 XtSetArg (av[ac], XmNforeground, pixel);
4423 ++ac;
4426 pixel = f->output_data.x->scroll_bar_background_pixel;
4427 if (pixel != -1)
4429 XtSetArg (av[ac], XmNbackground, pixel);
4430 ++ac;
4433 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4434 scroll_bar_name, av, ac);
4436 /* Add one callback for everything that can happen. */
4437 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4438 (XtPointer) bar);
4439 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4440 (XtPointer) bar);
4441 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4442 (XtPointer) bar);
4443 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4444 (XtPointer) bar);
4445 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4446 (XtPointer) bar);
4447 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4448 (XtPointer) bar);
4449 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4450 (XtPointer) bar);
4452 /* Realize the widget. Only after that is the X window created. */
4453 XtRealizeWidget (widget);
4455 /* Set the cursor to an arrow. I didn't find a resource to do that.
4456 And I'm wondering why it hasn't an arrow cursor by default. */
4457 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4458 f->output_data.x->nontext_cursor);
4460 #else /* !USE_MOTIF i.e. use Xaw */
4462 /* Set resources. Create the widget. The background of the
4463 Xaw3d scroll bar widget is a little bit light for my taste.
4464 We don't alter it here to let users change it according
4465 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4466 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4467 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4468 /* For smoother scrolling with Xaw3d -sm */
4469 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4471 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4472 if (pixel != -1)
4474 XtSetArg (av[ac], XtNforeground, pixel);
4475 ++ac;
4478 pixel = f->output_data.x->scroll_bar_background_pixel;
4479 if (pixel != -1)
4481 XtSetArg (av[ac], XtNbackground, pixel);
4482 ++ac;
4485 /* Top/bottom shadow colors. */
4487 /* Allocate them, if necessary. */
4488 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4490 pixel = f->output_data.x->scroll_bar_background_pixel;
4491 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4492 &pixel, 1.2, 0x8000))
4493 pixel = -1;
4494 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4496 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4498 pixel = f->output_data.x->scroll_bar_background_pixel;
4499 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4500 &pixel, 0.6, 0x4000))
4501 pixel = -1;
4502 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4505 #ifdef XtNbeNiceToColormap
4506 /* Tell the toolkit about them. */
4507 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4508 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4509 /* We tried to allocate a color for the top/bottom shadow, and
4510 failed, so tell Xaw3d to use dithering instead. */
4512 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4513 ++ac;
4515 else
4516 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4517 be more consistent with other emacs 3d colors, and since Xaw3d is
4518 not good at dealing with allocation failure. */
4520 /* This tells Xaw3d to use real colors instead of dithering for
4521 the shadows. */
4522 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4523 ++ac;
4525 /* Specify the colors. */
4526 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4527 if (pixel != -1)
4529 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4530 ++ac;
4532 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4533 if (pixel != -1)
4535 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4536 ++ac;
4539 #endif
4541 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4542 f->output_data.x->edit_widget, av, ac);
4545 char *initial = "";
4546 char *val = initial;
4547 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4548 #ifdef XtNarrowScrollbars
4549 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4550 #endif
4551 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4552 if (xaw3d_arrow_scroll || val == initial)
4553 { /* ARROW_SCROLL */
4554 xaw3d_arrow_scroll = True;
4555 /* Isn't that just a personal preference ? --Stef */
4556 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4560 /* Define callbacks. */
4561 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4562 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4563 (XtPointer) bar);
4565 /* Realize the widget. Only after that is the X window created. */
4566 XtRealizeWidget (widget);
4568 #endif /* !USE_MOTIF */
4570 /* Install an action hook that lets us detect when the user
4571 finishes interacting with a scroll bar. */
4572 if (action_hook_id == 0)
4573 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4575 /* Remember X window and widget in the scroll bar vector. */
4576 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4577 xwindow = XtWindow (widget);
4578 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
4580 UNBLOCK_INPUT;
4582 #endif /* not USE_GTK */
4585 /* Set the thumb size and position of scroll bar BAR. We are currently
4586 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4588 #ifdef USE_GTK
4589 static void
4590 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4591 struct scroll_bar *bar;
4592 int portion, position, whole;
4594 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4597 #else /* not USE_GTK */
4598 static void
4599 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4600 struct scroll_bar *bar;
4601 int portion, position, whole;
4603 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4604 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4605 float top, shown;
4607 BLOCK_INPUT;
4609 #ifdef USE_MOTIF
4611 /* We use an estimate of 30 chars per line rather than the real
4612 `portion' value. This has the disadvantage that the thumb size
4613 is not very representative, but it makes our life a lot easier.
4614 Otherwise, we have to constantly adjust the thumb size, which
4615 we can't always do quickly enough: while dragging, the size of
4616 the thumb might prevent the user from dragging the thumb all the
4617 way to the end. but Motif and some versions of Xaw3d don't allow
4618 updating the thumb size while dragging. Also, even if we can update
4619 its size, the update will often happen too late.
4620 If you don't believe it, check out revision 1.650 of xterm.c to see
4621 what hoops we were going through and the still poor behavior we got. */
4622 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4623 /* When the thumb is at the bottom, position == whole.
4624 So we need to increase `whole' to make space for the thumb. */
4625 whole += portion;
4627 if (whole <= 0)
4628 top = 0, shown = 1;
4629 else
4631 top = (float) position / whole;
4632 shown = (float) portion / whole;
4635 if (NILP (bar->dragging))
4637 int size, value;
4639 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4640 is the scroll bar's maximum and MIN is the scroll bar's minimum
4641 value. */
4642 size = shown * XM_SB_MAX;
4643 size = min (size, XM_SB_MAX);
4644 size = max (size, 1);
4646 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4647 value = top * XM_SB_MAX;
4648 value = min (value, XM_SB_MAX - size);
4650 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4652 #else /* !USE_MOTIF i.e. use Xaw */
4654 if (whole == 0)
4655 top = 0, shown = 1;
4656 else
4658 top = (float) position / whole;
4659 shown = (float) portion / whole;
4663 float old_top, old_shown;
4664 Dimension height;
4665 XtVaGetValues (widget,
4666 XtNtopOfThumb, &old_top,
4667 XtNshown, &old_shown,
4668 XtNheight, &height,
4669 NULL);
4671 /* Massage the top+shown values. */
4672 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4673 top = max (0, min (1, top));
4674 else
4675 top = old_top;
4676 /* Keep two pixels available for moving the thumb down. */
4677 shown = max (0, min (1 - top - (2.0 / height), shown));
4679 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4680 check that your system's configuration file contains a define
4681 for `NARROWPROTO'. See s/freebsd.h for an example. */
4682 if (top != old_top || shown != old_shown)
4684 if (NILP (bar->dragging))
4685 XawScrollbarSetThumb (widget, top, shown);
4686 else
4688 /* Try to make the scrolling a tad smoother. */
4689 if (!xaw3d_pick_top)
4690 shown = min (shown, old_shown);
4692 XawScrollbarSetThumb (widget, top, shown);
4696 #endif /* !USE_MOTIF */
4698 UNBLOCK_INPUT;
4700 #endif /* not USE_GTK */
4702 #endif /* USE_TOOLKIT_SCROLL_BARS */
4706 /************************************************************************
4707 Scroll bars, general
4708 ************************************************************************/
4710 /* Create a scroll bar and return the scroll bar vector for it. W is
4711 the Emacs window on which to create the scroll bar. TOP, LEFT,
4712 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4713 scroll bar. */
4715 static struct scroll_bar *
4716 x_scroll_bar_create (w, top, left, width, height)
4717 struct window *w;
4718 int top, left, width, height;
4720 struct frame *f = XFRAME (w->frame);
4721 struct scroll_bar *bar
4722 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
4724 BLOCK_INPUT;
4726 #ifdef USE_TOOLKIT_SCROLL_BARS
4727 x_create_toolkit_scroll_bar (f, bar);
4728 #else /* not USE_TOOLKIT_SCROLL_BARS */
4730 XSetWindowAttributes a;
4731 unsigned long mask;
4732 Window window;
4734 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4735 if (a.background_pixel == -1)
4736 a.background_pixel = f->output_data.x->background_pixel;
4738 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4739 | ButtonMotionMask | PointerMotionHintMask
4740 | ExposureMask);
4741 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4743 mask = (CWBackPixel | CWEventMask | CWCursor);
4745 /* Clear the area of W that will serve as a scroll bar. This is
4746 for the case that a window has been split horizontally. In
4747 this case, no clear_frame is generated to reduce flickering. */
4748 if (width > 0 && height > 0)
4749 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4750 left, top, width,
4751 window_box_height (w), False);
4753 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4754 /* Position and size of scroll bar. */
4755 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4756 top,
4757 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4758 height,
4759 /* Border width, depth, class, and visual. */
4761 CopyFromParent,
4762 CopyFromParent,
4763 CopyFromParent,
4764 /* Attributes. */
4765 mask, &a);
4766 SET_SCROLL_BAR_X_WINDOW (bar, window);
4768 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4770 XSETWINDOW (bar->window, w);
4771 XSETINT (bar->top, top);
4772 XSETINT (bar->left, left);
4773 XSETINT (bar->width, width);
4774 XSETINT (bar->height, height);
4775 XSETINT (bar->start, 0);
4776 XSETINT (bar->end, 0);
4777 bar->dragging = Qnil;
4779 /* Add bar to its frame's list of scroll bars. */
4780 bar->next = FRAME_SCROLL_BARS (f);
4781 bar->prev = Qnil;
4782 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4783 if (!NILP (bar->next))
4784 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4786 /* Map the window/widget. */
4787 #ifdef USE_TOOLKIT_SCROLL_BARS
4789 #ifdef USE_GTK
4790 xg_update_scrollbar_pos (f,
4791 SCROLL_BAR_X_WINDOW (bar),
4792 top,
4793 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4794 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4795 max (height, 1));
4796 xg_show_scroll_bar (SCROLL_BAR_X_WINDOW (bar));
4797 #else /* not USE_GTK */
4798 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4799 XtConfigureWidget (scroll_bar,
4800 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4801 top,
4802 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4803 max (height, 1), 0);
4804 XtMapWidget (scroll_bar);
4805 #endif /* not USE_GTK */
4807 #else /* not USE_TOOLKIT_SCROLL_BARS */
4808 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
4809 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4811 UNBLOCK_INPUT;
4812 return bar;
4816 /* Draw BAR's handle in the proper position.
4818 If the handle is already drawn from START to END, don't bother
4819 redrawing it, unless REBUILD is non-zero; in that case, always
4820 redraw it. (REBUILD is handy for drawing the handle after expose
4821 events.)
4823 Normally, we want to constrain the start and end of the handle to
4824 fit inside its rectangle, but if the user is dragging the scroll
4825 bar handle, we want to let them drag it down all the way, so that
4826 the bar's top is as far down as it goes; otherwise, there's no way
4827 to move to the very end of the buffer. */
4829 #ifndef USE_TOOLKIT_SCROLL_BARS
4831 static void
4832 x_scroll_bar_set_handle (bar, start, end, rebuild)
4833 struct scroll_bar *bar;
4834 int start, end;
4835 int rebuild;
4837 int dragging = ! NILP (bar->dragging);
4838 Window w = SCROLL_BAR_X_WINDOW (bar);
4839 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4840 GC gc = f->output_data.x->normal_gc;
4842 /* If the display is already accurate, do nothing. */
4843 if (! rebuild
4844 && start == XINT (bar->start)
4845 && end == XINT (bar->end))
4846 return;
4848 BLOCK_INPUT;
4851 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
4852 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
4853 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
4855 /* Make sure the values are reasonable, and try to preserve
4856 the distance between start and end. */
4858 int length = end - start;
4860 if (start < 0)
4861 start = 0;
4862 else if (start > top_range)
4863 start = top_range;
4864 end = start + length;
4866 if (end < start)
4867 end = start;
4868 else if (end > top_range && ! dragging)
4869 end = top_range;
4872 /* Store the adjusted setting in the scroll bar. */
4873 XSETINT (bar->start, start);
4874 XSETINT (bar->end, end);
4876 /* Clip the end position, just for display. */
4877 if (end > top_range)
4878 end = top_range;
4880 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
4881 below top positions, to make sure the handle is always at least
4882 that many pixels tall. */
4883 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
4885 /* Draw the empty space above the handle. Note that we can't clear
4886 zero-height areas; that means "clear to end of window." */
4887 if (0 < start)
4888 x_clear_area (FRAME_X_DISPLAY (f), w,
4889 /* x, y, width, height, and exposures. */
4890 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4891 VERTICAL_SCROLL_BAR_TOP_BORDER,
4892 inside_width, start,
4893 False);
4895 /* Change to proper foreground color if one is specified. */
4896 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4897 XSetForeground (FRAME_X_DISPLAY (f), gc,
4898 f->output_data.x->scroll_bar_foreground_pixel);
4900 /* Draw the handle itself. */
4901 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
4902 /* x, y, width, height */
4903 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4904 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
4905 inside_width, end - start);
4907 /* Restore the foreground color of the GC if we changed it above. */
4908 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4909 XSetForeground (FRAME_X_DISPLAY (f), gc,
4910 f->output_data.x->foreground_pixel);
4912 /* Draw the empty space below the handle. Note that we can't
4913 clear zero-height areas; that means "clear to end of window." */
4914 if (end < inside_height)
4915 x_clear_area (FRAME_X_DISPLAY (f), w,
4916 /* x, y, width, height, and exposures. */
4917 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4918 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
4919 inside_width, inside_height - end,
4920 False);
4924 UNBLOCK_INPUT;
4927 #endif /* !USE_TOOLKIT_SCROLL_BARS */
4929 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
4930 nil. */
4932 static void
4933 x_scroll_bar_remove (bar)
4934 struct scroll_bar *bar;
4936 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4937 BLOCK_INPUT;
4939 #ifdef USE_TOOLKIT_SCROLL_BARS
4940 #ifdef USE_GTK
4941 xg_remove_scroll_bar (f, SCROLL_BAR_X_WINDOW (bar));
4942 #else /* not USE_GTK */
4943 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
4944 #endif /* not USE_GTK */
4945 #else
4946 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
4947 #endif
4949 /* Disassociate this scroll bar from its window. */
4950 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
4952 UNBLOCK_INPUT;
4956 /* Set the handle of the vertical scroll bar for WINDOW to indicate
4957 that we are displaying PORTION characters out of a total of WHOLE
4958 characters, starting at POSITION. If WINDOW has no scroll bar,
4959 create one. */
4961 static void
4962 XTset_vertical_scroll_bar (w, portion, whole, position)
4963 struct window *w;
4964 int portion, whole, position;
4966 struct frame *f = XFRAME (w->frame);
4967 struct scroll_bar *bar;
4968 int top, height, left, sb_left, width, sb_width;
4969 int window_y, window_height;
4971 /* Get window dimensions. */
4972 window_box (w, -1, 0, &window_y, 0, &window_height);
4973 top = window_y;
4974 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
4975 height = window_height;
4977 /* Compute the left edge of the scroll bar area. */
4978 left = WINDOW_SCROLL_BAR_AREA_X (w);
4980 /* Compute the width of the scroll bar which might be less than
4981 the width of the area reserved for the scroll bar. */
4982 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
4983 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
4984 else
4985 sb_width = width;
4987 /* Compute the left edge of the scroll bar. */
4988 #ifdef USE_TOOLKIT_SCROLL_BARS
4989 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
4990 sb_left = (left +
4991 (WINDOW_RIGHTMOST_P (w)
4992 ? width - sb_width - (width - sb_width) / 2
4993 : 0));
4994 else
4995 sb_left = (left +
4996 (WINDOW_LEFTMOST_P (w)
4997 ? (width - sb_width) / 2
4998 : width - sb_width));
4999 #else
5000 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5001 sb_left = left + width - sb_width;
5002 else
5003 sb_left = left;
5004 #endif
5006 /* Does the scroll bar exist yet? */
5007 if (NILP (w->vertical_scroll_bar))
5009 if (width > 0 && height > 0)
5011 BLOCK_INPUT;
5012 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5013 left, top, width, height, False);
5014 UNBLOCK_INPUT;
5017 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5019 else
5021 /* It may just need to be moved and resized. */
5022 unsigned int mask = 0;
5024 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5026 BLOCK_INPUT;
5028 if (sb_left != XINT (bar->left))
5029 mask |= CWX;
5030 if (top != XINT (bar->top))
5031 mask |= CWY;
5032 if (sb_width != XINT (bar->width))
5033 mask |= CWWidth;
5034 if (height != XINT (bar->height))
5035 mask |= CWHeight;
5037 #ifdef USE_TOOLKIT_SCROLL_BARS
5039 /* Move/size the scroll bar widget. */
5040 if (mask)
5042 /* Since toolkit scroll bars are smaller than the space reserved
5043 for them on the frame, we have to clear "under" them. */
5044 if (width > 0 && height > 0)
5045 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5046 left, top, width, height, False);
5047 #ifdef USE_GTK
5048 xg_update_scrollbar_pos (f,
5049 SCROLL_BAR_X_WINDOW (bar),
5050 top,
5051 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5052 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM *2,
5053 max (height, 1));
5054 #else /* not USE_GTK */
5055 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5056 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5057 top,
5058 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5059 max (height, 1), 0);
5060 #endif /* not USE_GTK */
5062 #else /* not USE_TOOLKIT_SCROLL_BARS */
5064 /* Clear areas not covered by the scroll bar because of
5065 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5066 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5068 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5069 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5070 height, False);
5071 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5072 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5073 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5074 height, False);
5077 /* Clear areas not covered by the scroll bar because it's not as
5078 wide as the area reserved for it. This makes sure a
5079 previous mode line display is cleared after C-x 2 C-x 1, for
5080 example. */
5082 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5083 int rest = area_width - sb_width;
5084 if (rest > 0 && height > 0)
5086 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5087 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5088 left + area_width - rest, top,
5089 rest, height, False);
5090 else
5091 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5092 left, top, rest, height, False);
5096 /* Move/size the scroll bar window. */
5097 if (mask)
5099 XWindowChanges wc;
5101 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5102 wc.y = top;
5103 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5104 wc.height = height;
5105 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
5106 mask, &wc);
5109 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5111 /* Remember new settings. */
5112 XSETINT (bar->left, sb_left);
5113 XSETINT (bar->top, top);
5114 XSETINT (bar->width, sb_width);
5115 XSETINT (bar->height, height);
5117 UNBLOCK_INPUT;
5120 #ifdef USE_TOOLKIT_SCROLL_BARS
5121 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5122 #else /* not USE_TOOLKIT_SCROLL_BARS */
5123 /* Set the scroll bar's current state, unless we're currently being
5124 dragged. */
5125 if (NILP (bar->dragging))
5127 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5129 if (whole == 0)
5130 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5131 else
5133 int start = ((double) position * top_range) / whole;
5134 int end = ((double) (position + portion) * top_range) / whole;
5135 x_scroll_bar_set_handle (bar, start, end, 0);
5138 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5140 XSETVECTOR (w->vertical_scroll_bar, bar);
5144 /* The following three hooks are used when we're doing a thorough
5145 redisplay of the frame. We don't explicitly know which scroll bars
5146 are going to be deleted, because keeping track of when windows go
5147 away is a real pain - "Can you say set-window-configuration, boys
5148 and girls?" Instead, we just assert at the beginning of redisplay
5149 that *all* scroll bars are to be removed, and then save a scroll bar
5150 from the fiery pit when we actually redisplay its window. */
5152 /* Arrange for all scroll bars on FRAME to be removed at the next call
5153 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5154 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5156 static void
5157 XTcondemn_scroll_bars (frame)
5158 FRAME_PTR frame;
5160 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5161 while (! NILP (FRAME_SCROLL_BARS (frame)))
5163 Lisp_Object bar;
5164 bar = FRAME_SCROLL_BARS (frame);
5165 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5166 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5167 XSCROLL_BAR (bar)->prev = Qnil;
5168 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5169 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5170 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5175 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5176 Note that WINDOW isn't necessarily condemned at all. */
5178 static void
5179 XTredeem_scroll_bar (window)
5180 struct window *window;
5182 struct scroll_bar *bar;
5183 struct frame *f;
5185 /* We can't redeem this window's scroll bar if it doesn't have one. */
5186 if (NILP (window->vertical_scroll_bar))
5187 abort ();
5189 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5191 /* Unlink it from the condemned list. */
5192 f = XFRAME (WINDOW_FRAME (window));
5193 if (NILP (bar->prev))
5195 /* If the prev pointer is nil, it must be the first in one of
5196 the lists. */
5197 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5198 /* It's not condemned. Everything's fine. */
5199 return;
5200 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5201 window->vertical_scroll_bar))
5202 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5203 else
5204 /* If its prev pointer is nil, it must be at the front of
5205 one or the other! */
5206 abort ();
5208 else
5209 XSCROLL_BAR (bar->prev)->next = bar->next;
5211 if (! NILP (bar->next))
5212 XSCROLL_BAR (bar->next)->prev = bar->prev;
5214 bar->next = FRAME_SCROLL_BARS (f);
5215 bar->prev = Qnil;
5216 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5217 if (! NILP (bar->next))
5218 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5221 /* Remove all scroll bars on FRAME that haven't been saved since the
5222 last call to `*condemn_scroll_bars_hook'. */
5224 static void
5225 XTjudge_scroll_bars (f)
5226 FRAME_PTR f;
5228 Lisp_Object bar, next;
5230 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5232 /* Clear out the condemned list now so we won't try to process any
5233 more events on the hapless scroll bars. */
5234 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5236 for (; ! NILP (bar); bar = next)
5238 struct scroll_bar *b = XSCROLL_BAR (bar);
5240 x_scroll_bar_remove (b);
5242 next = b->next;
5243 b->next = b->prev = Qnil;
5246 /* Now there should be no references to the condemned scroll bars,
5247 and they should get garbage-collected. */
5251 #ifndef USE_TOOLKIT_SCROLL_BARS
5252 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5253 is a no-op when using toolkit scroll bars.
5255 This may be called from a signal handler, so we have to ignore GC
5256 mark bits. */
5258 static void
5259 x_scroll_bar_expose (bar, event)
5260 struct scroll_bar *bar;
5261 XEvent *event;
5263 Window w = SCROLL_BAR_X_WINDOW (bar);
5264 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5265 GC gc = f->output_data.x->normal_gc;
5266 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5268 BLOCK_INPUT;
5270 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
5272 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5273 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5275 /* x, y, width, height */
5276 0, 0,
5277 XINT (bar->width) - 1 - width_trim - width_trim,
5278 XINT (bar->height) - 1);
5280 UNBLOCK_INPUT;
5283 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5285 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5286 is set to something other than NO_EVENT, it is enqueued.
5288 This may be called from a signal handler, so we have to ignore GC
5289 mark bits. */
5292 static void
5293 x_scroll_bar_handle_click (bar, event, emacs_event)
5294 struct scroll_bar *bar;
5295 XEvent *event;
5296 struct input_event *emacs_event;
5298 if (! GC_WINDOWP (bar->window))
5299 abort ();
5301 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5302 emacs_event->code = event->xbutton.button - Button1;
5303 emacs_event->modifiers
5304 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5305 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5306 event->xbutton.state)
5307 | (event->type == ButtonRelease
5308 ? up_modifier
5309 : down_modifier));
5310 emacs_event->frame_or_window = bar->window;
5311 emacs_event->arg = Qnil;
5312 emacs_event->timestamp = event->xbutton.time;
5314 #if 0
5315 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5316 int internal_height
5317 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
5318 #endif
5319 int top_range
5320 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5321 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5323 if (y < 0) y = 0;
5324 if (y > top_range) y = top_range;
5326 if (y < XINT (bar->start))
5327 emacs_event->part = scroll_bar_above_handle;
5328 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5329 emacs_event->part = scroll_bar_handle;
5330 else
5331 emacs_event->part = scroll_bar_below_handle;
5333 /* Just because the user has clicked on the handle doesn't mean
5334 they want to drag it. Lisp code needs to be able to decide
5335 whether or not we're dragging. */
5336 #if 0
5337 /* If the user has just clicked on the handle, record where they're
5338 holding it. */
5339 if (event->type == ButtonPress
5340 && emacs_event->part == scroll_bar_handle)
5341 XSETINT (bar->dragging, y - XINT (bar->start));
5342 #endif
5344 #ifndef USE_TOOLKIT_SCROLL_BARS
5345 /* If the user has released the handle, set it to its final position. */
5346 if (event->type == ButtonRelease
5347 && ! NILP (bar->dragging))
5349 int new_start = y - XINT (bar->dragging);
5350 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
5352 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5353 bar->dragging = Qnil;
5355 #endif
5357 /* Same deal here as the other #if 0. */
5358 #if 0
5359 /* Clicks on the handle are always reported as occurring at the top of
5360 the handle. */
5361 if (emacs_event->part == scroll_bar_handle)
5362 emacs_event->x = bar->start;
5363 else
5364 XSETINT (emacs_event->x, y);
5365 #else
5366 XSETINT (emacs_event->x, y);
5367 #endif
5369 XSETINT (emacs_event->y, top_range);
5373 #ifndef USE_TOOLKIT_SCROLL_BARS
5375 /* Handle some mouse motion while someone is dragging the scroll bar.
5377 This may be called from a signal handler, so we have to ignore GC
5378 mark bits. */
5380 static void
5381 x_scroll_bar_note_movement (bar, event)
5382 struct scroll_bar *bar;
5383 XEvent *event;
5385 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5387 last_mouse_movement_time = event->xmotion.time;
5389 f->mouse_moved = 1;
5390 XSETVECTOR (last_mouse_scroll_bar, bar);
5392 /* If we're dragging the bar, display it. */
5393 if (! GC_NILP (bar->dragging))
5395 /* Where should the handle be now? */
5396 int new_start = event->xmotion.y - XINT (bar->dragging);
5398 if (new_start != XINT (bar->start))
5400 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
5402 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5407 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5409 /* Return information to the user about the current position of the mouse
5410 on the scroll bar. */
5412 static void
5413 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5414 FRAME_PTR *fp;
5415 Lisp_Object *bar_window;
5416 enum scroll_bar_part *part;
5417 Lisp_Object *x, *y;
5418 unsigned long *time;
5420 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5421 Window w = SCROLL_BAR_X_WINDOW (bar);
5422 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5423 int win_x, win_y;
5424 Window dummy_window;
5425 int dummy_coord;
5426 unsigned int dummy_mask;
5428 BLOCK_INPUT;
5430 /* Get the mouse's position relative to the scroll bar window, and
5431 report that. */
5432 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5434 /* Root, child, root x and root y. */
5435 &dummy_window, &dummy_window,
5436 &dummy_coord, &dummy_coord,
5438 /* Position relative to scroll bar. */
5439 &win_x, &win_y,
5441 /* Mouse buttons and modifier keys. */
5442 &dummy_mask))
5444 else
5446 #if 0
5447 int inside_height
5448 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
5449 #endif
5450 int top_range
5451 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5453 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5455 if (! NILP (bar->dragging))
5456 win_y -= XINT (bar->dragging);
5458 if (win_y < 0)
5459 win_y = 0;
5460 if (win_y > top_range)
5461 win_y = top_range;
5463 *fp = f;
5464 *bar_window = bar->window;
5466 if (! NILP (bar->dragging))
5467 *part = scroll_bar_handle;
5468 else if (win_y < XINT (bar->start))
5469 *part = scroll_bar_above_handle;
5470 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5471 *part = scroll_bar_handle;
5472 else
5473 *part = scroll_bar_below_handle;
5475 XSETINT (*x, win_y);
5476 XSETINT (*y, top_range);
5478 f->mouse_moved = 0;
5479 last_mouse_scroll_bar = Qnil;
5482 *time = last_mouse_movement_time;
5484 UNBLOCK_INPUT;
5488 /* The screen has been cleared so we may have changed foreground or
5489 background colors, and the scroll bars may need to be redrawn.
5490 Clear out the scroll bars, and ask for expose events, so we can
5491 redraw them. */
5493 void
5494 x_scroll_bar_clear (f)
5495 FRAME_PTR f;
5497 #ifndef USE_TOOLKIT_SCROLL_BARS
5498 Lisp_Object bar;
5500 /* We can have scroll bars even if this is 0,
5501 if we just turned off scroll bar mode.
5502 But in that case we should not clear them. */
5503 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5504 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5505 bar = XSCROLL_BAR (bar)->next)
5506 XClearArea (FRAME_X_DISPLAY (f),
5507 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
5508 0, 0, 0, 0, True);
5509 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5513 /* The main X event-reading loop - XTread_socket. */
5515 #if 0
5516 /* Time stamp of enter window event. This is only used by XTread_socket,
5517 but we have to put it out here, since static variables within functions
5518 sometimes don't work. */
5520 static Time enter_timestamp;
5521 #endif
5523 /* This holds the state XLookupString needs to implement dead keys
5524 and other tricks known as "compose processing". _X Window System_
5525 says that a portable program can't use this, but Stephen Gildea assures
5526 me that letting the compiler initialize it to zeros will work okay.
5528 This must be defined outside of XTread_socket, for the same reasons
5529 given for enter_timestamp, above. */
5531 static XComposeStatus compose_status;
5533 /* Record the last 100 characters stored
5534 to help debug the loss-of-chars-during-GC problem. */
5536 static int temp_index;
5537 static short temp_buffer[100];
5539 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5540 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5541 temp_index = 0; \
5542 temp_buffer[temp_index++] = (keysym)
5544 /* Set this to nonzero to fake an "X I/O error"
5545 on a particular display. */
5547 struct x_display_info *XTread_socket_fake_io_error;
5549 /* When we find no input here, we occasionally do a no-op command
5550 to verify that the X server is still running and we can still talk with it.
5551 We try all the open displays, one by one.
5552 This variable is used for cycling thru the displays. */
5554 static struct x_display_info *next_noop_dpyinfo;
5556 #define SET_SAVED_MENU_EVENT(size) \
5557 do \
5559 if (f->output_data.x->saved_menu_event == 0) \
5560 f->output_data.x->saved_menu_event \
5561 = (XEvent *) xmalloc (sizeof (XEvent)); \
5562 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5563 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT; \
5564 XSETFRAME (inev.ie.frame_or_window, f); \
5566 while (0)
5568 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5569 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5572 enum
5574 X_EVENT_NORMAL,
5575 X_EVENT_GOTO_OUT,
5576 X_EVENT_DROP
5579 /* Filter events for the current X input method.
5580 DPYINFO is the display this event is for.
5581 EVENT is the X event to filter.
5583 Returns non-zero if the event was filtered, caller shall not process
5584 this event further.
5585 Returns zero if event is wasn't filtered. */
5587 #ifdef HAVE_X_I18N
5588 static int
5589 x_filter_event (dpyinfo, event)
5590 struct x_display_info *dpyinfo;
5591 XEvent *event;
5593 /* XFilterEvent returns non-zero if the input method has
5594 consumed the event. We pass the frame's X window to
5595 XFilterEvent because that's the one for which the IC
5596 was created. */
5598 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5599 event->xclient.window);
5601 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5603 #endif
5605 #ifdef USE_GTK
5606 static int current_count;
5607 static int current_finish;
5608 static struct input_event *current_hold_quit;
5610 /* This is the filter function invoked by the GTK event loop.
5611 It is invoked before the XEvent is translated to a GdkEvent,
5612 so we have a chance to act on the event before GTK. */
5613 static GdkFilterReturn
5614 event_handler_gdk (gxev, ev, data)
5615 GdkXEvent *gxev;
5616 GdkEvent *ev;
5617 gpointer data;
5619 XEvent *xev = (XEvent *) gxev;
5621 if (current_count >= 0)
5623 struct x_display_info *dpyinfo;
5625 dpyinfo = x_display_info_for_display (xev->xany.display);
5627 #ifdef HAVE_X_I18N
5628 /* Filter events for the current X input method.
5629 GTK calls XFilterEvent but not for key press and release,
5630 so we do it here. */
5631 if (xev->type == KeyPress || xev->type == KeyRelease)
5632 if (dpyinfo && x_filter_event (dpyinfo, xev))
5633 return GDK_FILTER_REMOVE;
5634 #endif
5636 if (! dpyinfo)
5637 current_finish = X_EVENT_NORMAL;
5638 else
5640 current_count +=
5641 handle_one_xevent (dpyinfo, xev, &current_finish,
5642 current_hold_quit);
5645 else
5646 current_finish = x_dispatch_event (xev, xev->xany.display);
5648 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5649 return GDK_FILTER_REMOVE;
5651 return GDK_FILTER_CONTINUE;
5653 #endif /* USE_GTK */
5656 /* Handles the XEvent EVENT on display DPYINFO.
5658 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5659 *FINISH is zero if caller should continue reading events.
5660 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5662 We return the number of characters stored into the buffer. */
5664 static int
5665 handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
5666 struct x_display_info *dpyinfo;
5667 XEvent *eventp;
5668 int *finish;
5669 struct input_event *hold_quit;
5671 union {
5672 struct input_event ie;
5673 struct selection_input_event sie;
5674 } inev;
5675 int count = 0;
5676 int do_help = 0;
5677 int nbytes = 0;
5678 struct frame *f = NULL;
5679 struct coding_system coding;
5680 XEvent event = *eventp;
5682 *finish = X_EVENT_NORMAL;
5684 EVENT_INIT (inev.ie);
5685 inev.ie.kind = NO_EVENT;
5686 inev.ie.arg = Qnil;
5688 switch (event.type)
5690 case ClientMessage:
5692 if (event.xclient.message_type
5693 == dpyinfo->Xatom_wm_protocols
5694 && event.xclient.format == 32)
5696 if (event.xclient.data.l[0]
5697 == dpyinfo->Xatom_wm_take_focus)
5699 /* Use x_any_window_to_frame because this
5700 could be the shell widget window
5701 if the frame has no title bar. */
5702 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5703 #ifdef HAVE_X_I18N
5704 /* Not quite sure this is needed -pd */
5705 if (f && FRAME_XIC (f))
5706 XSetICFocus (FRAME_XIC (f));
5707 #endif
5708 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5709 instructs the WM to set the input focus automatically for
5710 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5711 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5712 it has set the focus. So, XSetInputFocus below is not
5713 needed.
5715 The call to XSetInputFocus below has also caused trouble. In
5716 cases where the XSetInputFocus done by the WM and the one
5717 below are temporally close (on a fast machine), the call
5718 below can generate additional FocusIn events which confuse
5719 Emacs. */
5721 /* Since we set WM_TAKE_FOCUS, we must call
5722 XSetInputFocus explicitly. But not if f is null,
5723 since that might be an event for a deleted frame. */
5724 if (f)
5726 Display *d = event.xclient.display;
5727 /* Catch and ignore errors, in case window has been
5728 iconified by a window manager such as GWM. */
5729 int count = x_catch_errors (d);
5730 XSetInputFocus (d, event.xclient.window,
5731 /* The ICCCM says this is
5732 the only valid choice. */
5733 RevertToParent,
5734 event.xclient.data.l[1]);
5735 /* This is needed to detect the error
5736 if there is an error. */
5737 XSync (d, False);
5738 x_uncatch_errors (d, count);
5740 /* Not certain about handling scroll bars here */
5741 #endif /* 0 */
5742 goto done;
5745 if (event.xclient.data.l[0]
5746 == dpyinfo->Xatom_wm_save_yourself)
5748 /* Save state modify the WM_COMMAND property to
5749 something which can reinstate us. This notifies
5750 the session manager, who's looking for such a
5751 PropertyNotify. Can restart processing when
5752 a keyboard or mouse event arrives. */
5753 /* If we have a session manager, don't set this.
5754 KDE will then start two Emacsen, one for the
5755 session manager and one for this. */
5756 #ifdef HAVE_X_SM
5757 if (! x_session_have_connection ())
5758 #endif
5760 f = x_top_window_to_frame (dpyinfo,
5761 event.xclient.window);
5762 /* This is just so we only give real data once
5763 for a single Emacs process. */
5764 if (f == SELECTED_FRAME ())
5765 XSetCommand (FRAME_X_DISPLAY (f),
5766 event.xclient.window,
5767 initial_argv, initial_argc);
5768 else if (f)
5769 XSetCommand (FRAME_X_DISPLAY (f),
5770 event.xclient.window,
5771 0, 0);
5773 goto done;
5776 if (event.xclient.data.l[0]
5777 == dpyinfo->Xatom_wm_delete_window)
5779 f = x_any_window_to_frame (dpyinfo,
5780 event.xclient.window);
5781 if (!f)
5782 goto OTHER; /* May be a dialog that is to be removed */
5784 inev.ie.kind = DELETE_WINDOW_EVENT;
5785 XSETFRAME (inev.ie.frame_or_window, f);
5786 goto done;
5789 goto done;
5792 if (event.xclient.message_type
5793 == dpyinfo->Xatom_wm_configure_denied)
5795 goto done;
5798 if (event.xclient.message_type
5799 == dpyinfo->Xatom_wm_window_moved)
5801 int new_x, new_y;
5802 f = x_window_to_frame (dpyinfo, event.xclient.window);
5804 new_x = event.xclient.data.s[0];
5805 new_y = event.xclient.data.s[1];
5807 if (f)
5809 f->left_pos = new_x;
5810 f->top_pos = new_y;
5812 goto done;
5815 #ifdef HACK_EDITRES
5816 if (event.xclient.message_type
5817 == dpyinfo->Xatom_editres)
5819 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5820 _XEditResCheckMessages (f->output_data.x->widget, NULL,
5821 &event, NULL);
5822 goto done;
5824 #endif /* HACK_EDITRES */
5826 if ((event.xclient.message_type
5827 == dpyinfo->Xatom_DONE)
5828 || (event.xclient.message_type
5829 == dpyinfo->Xatom_PAGE))
5831 /* Ghostview job completed. Kill it. We could
5832 reply with "Next" if we received "Page", but we
5833 currently never do because we are interested in
5834 images, only, which should have 1 page. */
5835 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
5836 f = x_window_to_frame (dpyinfo, event.xclient.window);
5837 x_kill_gs_process (pixmap, f);
5838 expose_frame (f, 0, 0, 0, 0);
5839 goto done;
5842 #ifdef USE_TOOLKIT_SCROLL_BARS
5843 /* Scroll bar callbacks send a ClientMessage from which
5844 we construct an input_event. */
5845 if (event.xclient.message_type
5846 == 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 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5856 if (!f)
5857 goto OTHER;
5859 if (x_handle_dnd_message (f, &event.xclient, dpyinfo, &inev.ie))
5860 *finish = X_EVENT_DROP;
5862 break;
5864 case SelectionNotify:
5865 #ifdef USE_X_TOOLKIT
5866 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
5867 goto OTHER;
5868 #endif /* not USE_X_TOOLKIT */
5869 x_handle_selection_notify (&event.xselection);
5870 break;
5872 case SelectionClear: /* Someone has grabbed ownership. */
5873 #ifdef USE_X_TOOLKIT
5874 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
5875 goto OTHER;
5876 #endif /* USE_X_TOOLKIT */
5878 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
5880 inev.ie.kind = SELECTION_CLEAR_EVENT;
5881 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
5882 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
5883 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
5884 inev.ie.frame_or_window = Qnil;
5886 break;
5888 case SelectionRequest: /* Someone wants our selection. */
5889 #ifdef USE_X_TOOLKIT
5890 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
5891 goto OTHER;
5892 #endif /* USE_X_TOOLKIT */
5894 XSelectionRequestEvent *eventp
5895 = (XSelectionRequestEvent *) &event;
5897 inev.ie.kind = SELECTION_REQUEST_EVENT;
5898 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
5899 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
5900 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
5901 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
5902 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
5903 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
5904 inev.ie.frame_or_window = Qnil;
5906 break;
5908 case PropertyNotify:
5909 #if 0 /* This is plain wrong. In the case that we are waiting for a
5910 PropertyNotify used as an ACK in incremental selection
5911 transfer, the property will be on the receiver's window. */
5912 #if defined USE_X_TOOLKIT
5913 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
5914 goto OTHER;
5915 #endif
5916 #endif
5917 x_handle_property_notify (&event.xproperty);
5918 goto OTHER;
5920 case ReparentNotify:
5921 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
5922 if (f)
5924 int x, y;
5925 f->output_data.x->parent_desc = event.xreparent.parent;
5926 x_real_positions (f, &x, &y);
5927 f->left_pos = x;
5928 f->top_pos = y;
5930 /* Perhaps reparented due to a WM restart. Reset this. */
5931 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
5933 goto OTHER;
5935 case Expose:
5936 f = x_window_to_frame (dpyinfo, event.xexpose.window);
5937 if (f)
5939 x_check_fullscreen (f);
5941 #ifdef USE_GTK
5942 /* This seems to be needed for GTK 2.6. */
5943 x_clear_area (event.xexpose.display,
5944 event.xexpose.window,
5945 event.xexpose.x, event.xexpose.y,
5946 event.xexpose.width, event.xexpose.height,
5947 FALSE);
5948 #endif
5949 if (f->async_visible == 0)
5951 f->async_visible = 1;
5952 f->async_iconified = 0;
5953 f->output_data.x->has_been_visible = 1;
5954 SET_FRAME_GARBAGED (f);
5956 else
5957 expose_frame (f,
5958 event.xexpose.x, event.xexpose.y,
5959 event.xexpose.width, event.xexpose.height);
5961 else
5963 #ifndef USE_TOOLKIT_SCROLL_BARS
5964 struct scroll_bar *bar;
5965 #endif
5966 #if defined USE_LUCID
5967 /* Submenus of the Lucid menu bar aren't widgets
5968 themselves, so there's no way to dispatch events
5969 to them. Recognize this case separately. */
5971 Widget widget
5972 = x_window_to_menu_bar (event.xexpose.window);
5973 if (widget)
5974 xlwmenu_redisplay (widget);
5976 #endif /* USE_LUCID */
5978 #ifdef USE_TOOLKIT_SCROLL_BARS
5979 /* Dispatch event to the widget. */
5980 goto OTHER;
5981 #else /* not USE_TOOLKIT_SCROLL_BARS */
5982 bar = x_window_to_scroll_bar (event.xexpose.display,
5983 event.xexpose.window);
5985 if (bar)
5986 x_scroll_bar_expose (bar, &event);
5987 #ifdef USE_X_TOOLKIT
5988 else
5989 goto OTHER;
5990 #endif /* USE_X_TOOLKIT */
5991 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5993 break;
5995 case GraphicsExpose: /* This occurs when an XCopyArea's
5996 source area was obscured or not
5997 available. */
5998 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
5999 if (f)
6001 expose_frame (f,
6002 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6003 event.xgraphicsexpose.width,
6004 event.xgraphicsexpose.height);
6006 #ifdef USE_X_TOOLKIT
6007 else
6008 goto OTHER;
6009 #endif /* USE_X_TOOLKIT */
6010 break;
6012 case NoExpose: /* This occurs when an XCopyArea's
6013 source area was completely
6014 available. */
6015 break;
6017 case UnmapNotify:
6018 /* Redo the mouse-highlight after the tooltip has gone. */
6019 if (event.xmap.window == tip_window)
6021 tip_window = 0;
6022 redo_mouse_highlight ();
6025 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6026 if (f) /* F may no longer exist if
6027 the frame was deleted. */
6029 /* While a frame is unmapped, display generation is
6030 disabled; you don't want to spend time updating a
6031 display that won't ever be seen. */
6032 f->async_visible = 0;
6033 /* We can't distinguish, from the event, whether the window
6034 has become iconified or invisible. So assume, if it
6035 was previously visible, than now it is iconified.
6036 But x_make_frame_invisible clears both
6037 the visible flag and the iconified flag;
6038 and that way, we know the window is not iconified now. */
6039 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6041 f->async_iconified = 1;
6043 inev.ie.kind = ICONIFY_EVENT;
6044 XSETFRAME (inev.ie.frame_or_window, f);
6047 goto OTHER;
6049 case MapNotify:
6050 if (event.xmap.window == tip_window)
6051 /* The tooltip has been drawn already. Avoid
6052 the SET_FRAME_GARBAGED below. */
6053 goto OTHER;
6055 /* We use x_top_window_to_frame because map events can
6056 come for sub-windows and they don't mean that the
6057 frame is visible. */
6058 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6059 if (f)
6061 /* wait_reading_process_output will notice this and update
6062 the frame's display structures.
6063 If we where iconified, we should not set garbaged,
6064 because that stops redrawing on Expose events. This looks
6065 bad if we are called from a recursive event loop
6066 (x_dispatch_event), for example when a dialog is up. */
6067 if (! f->async_iconified)
6068 SET_FRAME_GARBAGED (f);
6070 f->async_visible = 1;
6071 f->async_iconified = 0;
6072 f->output_data.x->has_been_visible = 1;
6074 if (f->iconified)
6076 inev.ie.kind = DEICONIFY_EVENT;
6077 XSETFRAME (inev.ie.frame_or_window, f);
6079 else if (! NILP (Vframe_list)
6080 && ! NILP (XCDR (Vframe_list)))
6081 /* Force a redisplay sooner or later
6082 to update the frame titles
6083 in case this is the second frame. */
6084 record_asynch_buffer_change ();
6086 goto OTHER;
6088 case KeyPress:
6090 ignore_next_mouse_click_timeout = 0;
6092 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6093 /* Dispatch KeyPress events when in menu. */
6094 if (popup_activated ())
6095 goto OTHER;
6096 #endif
6098 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6100 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
6102 clear_mouse_face (dpyinfo);
6103 dpyinfo->mouse_face_hidden = 1;
6106 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6107 if (f == 0)
6109 /* Scroll bars consume key events, but we want
6110 the keys to go to the scroll bar's frame. */
6111 Widget widget = XtWindowToWidget (dpyinfo->display,
6112 event.xkey.window);
6113 if (widget && XmIsScrollBar (widget))
6115 widget = XtParent (widget);
6116 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6119 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6121 if (f != 0)
6123 KeySym keysym, orig_keysym;
6124 /* al%imercury@uunet.uu.net says that making this 81
6125 instead of 80 fixed a bug whereby meta chars made
6126 his Emacs hang.
6128 It seems that some version of XmbLookupString has
6129 a bug of not returning XBufferOverflow in
6130 status_return even if the input is too long to
6131 fit in 81 bytes. So, we must prepare sufficient
6132 bytes for copy_buffer. 513 bytes (256 chars for
6133 two-byte character set) seems to be a fairly good
6134 approximation. -- 2000.8.10 handa@etl.go.jp */
6135 unsigned char copy_buffer[513];
6136 unsigned char *copy_bufptr = copy_buffer;
6137 int copy_bufsiz = sizeof (copy_buffer);
6138 int modifiers;
6139 Lisp_Object coding_system = Qlatin_1;
6140 Lisp_Object c;
6142 #ifdef USE_GTK
6143 /* Don't pass keys to GTK. A Tab will shift focus to the
6144 tool bar in GTK 2.4. Keys will still go to menus and
6145 dialogs because in that case popup_activated is TRUE
6146 (see above). */
6147 *finish = X_EVENT_DROP;
6148 #endif
6150 event.xkey.state
6151 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6152 extra_keyboard_modifiers);
6153 modifiers = event.xkey.state;
6155 /* This will have to go some day... */
6157 /* make_lispy_event turns chars into control chars.
6158 Don't do it here because XLookupString is too eager. */
6159 event.xkey.state &= ~ControlMask;
6160 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6161 | dpyinfo->super_mod_mask
6162 | dpyinfo->hyper_mod_mask
6163 | dpyinfo->alt_mod_mask);
6165 /* In case Meta is ComposeCharacter,
6166 clear its status. According to Markus Ehrnsperger
6167 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6168 this enables ComposeCharacter to work whether or
6169 not it is combined with Meta. */
6170 if (modifiers & dpyinfo->meta_mod_mask)
6171 bzero (&compose_status, sizeof (compose_status));
6173 #ifdef HAVE_X_I18N
6174 if (FRAME_XIC (f))
6176 Status status_return;
6178 coding_system = Vlocale_coding_system;
6179 nbytes = XmbLookupString (FRAME_XIC (f),
6180 &event.xkey, copy_bufptr,
6181 copy_bufsiz, &keysym,
6182 &status_return);
6183 if (status_return == XBufferOverflow)
6185 copy_bufsiz = nbytes + 1;
6186 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6187 nbytes = XmbLookupString (FRAME_XIC (f),
6188 &event.xkey, copy_bufptr,
6189 copy_bufsiz, &keysym,
6190 &status_return);
6192 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6193 #if 0 && defined X_HAVE_UTF8_STRING
6194 else if (status_return == XLookupKeySym)
6195 { /* Try again but with utf-8. */
6196 coding_system = Qutf_8;
6197 nbytes = Xutf8LookupString (FRAME_XIC (f),
6198 &event.xkey, copy_bufptr,
6199 copy_bufsiz, &keysym,
6200 &status_return);
6201 if (status_return == XBufferOverflow)
6203 copy_bufsiz = nbytes + 1;
6204 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6205 nbytes = Xutf8LookupString (FRAME_XIC (f),
6206 &event.xkey,
6207 copy_bufptr,
6208 copy_bufsiz, &keysym,
6209 &status_return);
6212 #endif
6214 if (status_return == XLookupNone)
6215 break;
6216 else if (status_return == XLookupChars)
6218 keysym = NoSymbol;
6219 modifiers = 0;
6221 else if (status_return != XLookupKeySym
6222 && status_return != XLookupBoth)
6223 abort ();
6225 else
6226 nbytes = XLookupString (&event.xkey, copy_bufptr,
6227 copy_bufsiz, &keysym,
6228 &compose_status);
6229 #else
6230 nbytes = XLookupString (&event.xkey, copy_bufptr,
6231 copy_bufsiz, &keysym,
6232 &compose_status);
6233 #endif
6235 /* If not using XIM/XIC, and a compose sequence is in progress,
6236 we break here. Otherwise, chars_matched is always 0. */
6237 if (compose_status.chars_matched > 0 && nbytes == 0)
6238 break;
6240 bzero (&compose_status, sizeof (compose_status));
6241 orig_keysym = keysym;
6243 /* Common for all keysym input events. */
6244 XSETFRAME (inev.ie.frame_or_window, f);
6245 inev.ie.modifiers
6246 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), modifiers);
6247 inev.ie.timestamp = event.xkey.time;
6249 /* First deal with keysyms which have defined
6250 translations to characters. */
6251 if (keysym >= 32 && keysym < 128)
6252 /* Avoid explicitly decoding each ASCII character. */
6254 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6255 inev.ie.code = keysym;
6256 goto done_keysym;
6259 /* Keysyms directly mapped to supported Unicode characters. */
6260 if ((keysym >= 0x01000000 && keysym <= 0x010033ff)
6261 || (keysym >= 0x0100e000 && keysym <= 0x0100ffff))
6263 int code = keysym & 0xFFFF, charset_id, c1, c2;
6265 if (code < 0x80)
6267 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6268 inev.ie.code = code;
6270 else if (code < 0x100)
6272 if (code < 0xA0)
6273 charset_id = CHARSET_8_BIT_CONTROL;
6274 else
6275 charset_id = charset_latin_iso8859_1;
6276 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6277 inev.ie.code = MAKE_CHAR (charset_id, code, 0);
6279 else
6281 if (code < 0x2500)
6282 charset_id = charset_mule_unicode_0100_24ff,
6283 code -= 0x100;
6284 else if (code < 0xE000)
6285 charset_id = charset_mule_unicode_2500_33ff,
6286 code -= 0x2500;
6287 else
6288 charset_id = charset_mule_unicode_e000_ffff,
6289 code -= 0xE000;
6290 c1 = (code / 96) + 32, c2 = (code % 96) + 32;
6291 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6292 inev.ie.code = MAKE_CHAR (charset_id, c1, c2);
6294 goto done_keysym;
6297 /* Now non-ASCII. */
6298 if (HASH_TABLE_P (Vx_keysym_table)
6299 && (NATNUMP (c = Fgethash (make_number (keysym),
6300 Vx_keysym_table,
6301 Qnil))))
6303 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6304 ? ASCII_KEYSTROKE_EVENT
6305 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6306 inev.ie.code = XFASTINT (c);
6307 goto done_keysym;
6310 /* Random non-modifier sorts of keysyms. */
6311 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6312 || keysym == XK_Delete
6313 #ifdef XK_ISO_Left_Tab
6314 || (keysym >= XK_ISO_Left_Tab
6315 && keysym <= XK_ISO_Enter)
6316 #endif
6317 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6318 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6319 #ifdef HPUX
6320 /* This recognizes the "extended function
6321 keys". It seems there's no cleaner way.
6322 Test IsModifierKey to avoid handling
6323 mode_switch incorrectly. */
6324 || ((unsigned) (keysym) >= XK_Select
6325 && (unsigned)(keysym) < XK_KP_Space)
6326 #endif
6327 #ifdef XK_dead_circumflex
6328 || orig_keysym == XK_dead_circumflex
6329 #endif
6330 #ifdef XK_dead_grave
6331 || orig_keysym == XK_dead_grave
6332 #endif
6333 #ifdef XK_dead_tilde
6334 || orig_keysym == XK_dead_tilde
6335 #endif
6336 #ifdef XK_dead_diaeresis
6337 || orig_keysym == XK_dead_diaeresis
6338 #endif
6339 #ifdef XK_dead_macron
6340 || orig_keysym == XK_dead_macron
6341 #endif
6342 #ifdef XK_dead_degree
6343 || orig_keysym == XK_dead_degree
6344 #endif
6345 #ifdef XK_dead_acute
6346 || orig_keysym == XK_dead_acute
6347 #endif
6348 #ifdef XK_dead_cedilla
6349 || orig_keysym == XK_dead_cedilla
6350 #endif
6351 #ifdef XK_dead_breve
6352 || orig_keysym == XK_dead_breve
6353 #endif
6354 #ifdef XK_dead_ogonek
6355 || orig_keysym == XK_dead_ogonek
6356 #endif
6357 #ifdef XK_dead_caron
6358 || orig_keysym == XK_dead_caron
6359 #endif
6360 #ifdef XK_dead_doubleacute
6361 || orig_keysym == XK_dead_doubleacute
6362 #endif
6363 #ifdef XK_dead_abovedot
6364 || orig_keysym == XK_dead_abovedot
6365 #endif
6366 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6367 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6368 /* Any "vendor-specific" key is ok. */
6369 || (orig_keysym & (1 << 28))
6370 || (keysym != NoSymbol && nbytes == 0))
6371 && ! (IsModifierKey (orig_keysym)
6372 #ifndef HAVE_X11R5
6373 #ifdef XK_Mode_switch
6374 || ((unsigned)(orig_keysym) == XK_Mode_switch)
6375 #endif
6376 #ifdef XK_Num_Lock
6377 || ((unsigned)(orig_keysym) == XK_Num_Lock)
6378 #endif
6379 #endif /* not HAVE_X11R5 */
6380 /* The symbols from XK_ISO_Lock
6381 to XK_ISO_Last_Group_Lock
6382 don't have real modifiers but
6383 should be treated similarly to
6384 Mode_switch by Emacs. */
6385 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6386 || ((unsigned)(orig_keysym)
6387 >= XK_ISO_Lock
6388 && (unsigned)(orig_keysym)
6389 <= XK_ISO_Last_Group_Lock)
6390 #endif
6393 STORE_KEYSYM_FOR_DEBUG (keysym);
6394 /* make_lispy_event will convert this to a symbolic
6395 key. */
6396 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6397 inev.ie.code = keysym;
6398 goto done_keysym;
6401 { /* Raw bytes, not keysym. */
6402 register int i;
6403 register int c;
6404 int nchars, len;
6406 /* The input should be decoded with `coding_system'
6407 which depends on which X*LookupString function
6408 we used just above and the locale. */
6409 setup_coding_system (coding_system, &coding);
6410 coding.src_multibyte = 0;
6411 coding.dst_multibyte = 1;
6412 /* The input is converted to events, thus we can't
6413 handle composition. Anyway, there's no XIM that
6414 gives us composition information. */
6415 coding.composing = COMPOSITION_DISABLED;
6417 for (i = 0; i < nbytes; i++)
6419 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6423 /* Decode the input data. */
6424 int require;
6425 unsigned char *p;
6427 require = decoding_buffer_size (&coding, nbytes);
6428 p = (unsigned char *) alloca (require);
6429 coding.mode |= CODING_MODE_LAST_BLOCK;
6430 /* We explicitly disable composition handling because
6431 key data should not contain any composition sequence. */
6432 coding.composing = COMPOSITION_DISABLED;
6433 decode_coding (&coding, copy_bufptr, p, nbytes, require);
6434 nbytes = coding.produced;
6435 nchars = coding.produced_char;
6436 copy_bufptr = p;
6439 /* Convert the input data to a sequence of
6440 character events. */
6441 for (i = 0; i < nbytes; i += len)
6443 if (nchars == nbytes)
6444 c = copy_bufptr[i], len = 1;
6445 else
6446 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
6447 nbytes - i, len);
6448 inev.ie.kind = (SINGLE_BYTE_CHAR_P (c)
6449 ? ASCII_KEYSTROKE_EVENT
6450 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6451 inev.ie.code = c;
6452 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6455 /* Previous code updated count by nchars rather than nbytes,
6456 but that seems bogus to me. ++kfs */
6457 count += nbytes;
6459 inev.ie.kind = NO_EVENT; /* Already stored above. */
6461 if (keysym == NoSymbol)
6462 break;
6465 done_keysym:
6466 #ifdef HAVE_X_I18N
6467 /* Don't dispatch this event since XtDispatchEvent calls
6468 XFilterEvent, and two calls in a row may freeze the
6469 client. */
6470 break;
6471 #else
6472 goto OTHER;
6473 #endif
6475 case KeyRelease:
6476 #ifdef HAVE_X_I18N
6477 /* Don't dispatch this event since XtDispatchEvent calls
6478 XFilterEvent, and two calls in a row may freeze the
6479 client. */
6480 break;
6481 #else
6482 goto OTHER;
6483 #endif
6485 case EnterNotify:
6486 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6488 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6490 if (f && x_mouse_click_focus_ignore_position)
6491 ignore_next_mouse_click_timeout = event.xmotion.time + 200;
6493 #if 0
6494 if (event.xcrossing.focus)
6496 /* Avoid nasty pop/raise loops. */
6497 if (f && (!(f->auto_raise)
6498 || !(f->auto_lower)
6499 || (event.xcrossing.time - enter_timestamp) > 500))
6501 x_new_focus_frame (dpyinfo, f);
6502 enter_timestamp = event.xcrossing.time;
6505 else if (f == dpyinfo->x_focus_frame)
6506 x_new_focus_frame (dpyinfo, 0);
6507 #endif
6509 /* EnterNotify counts as mouse movement,
6510 so update things that depend on mouse position. */
6511 if (f && !f->output_data.x->hourglass_p)
6512 note_mouse_movement (f, &event.xmotion);
6513 #ifdef USE_GTK
6514 /* We may get an EnterNotify on the buttons in the toolbar. In that
6515 case we moved out of any highlighted area and need to note this. */
6516 if (!f && last_mouse_glyph_frame)
6517 note_mouse_movement (last_mouse_glyph_frame, &event);
6518 #endif
6519 goto OTHER;
6521 case FocusIn:
6522 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6523 goto OTHER;
6525 case LeaveNotify:
6526 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6528 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6529 if (f)
6531 if (f == dpyinfo->mouse_face_mouse_frame)
6533 /* If we move outside the frame, then we're
6534 certainly no longer on any text in the frame. */
6535 clear_mouse_face (dpyinfo);
6536 dpyinfo->mouse_face_mouse_frame = 0;
6539 /* Generate a nil HELP_EVENT to cancel a help-echo.
6540 Do it only if there's something to cancel.
6541 Otherwise, the startup message is cleared when
6542 the mouse leaves the frame. */
6543 if (any_help_event_p)
6544 do_help = -1;
6546 #ifdef USE_GTK
6547 /* See comment in EnterNotify above */
6548 else if (last_mouse_glyph_frame)
6549 note_mouse_movement (last_mouse_glyph_frame, &event);
6550 #endif
6551 goto OTHER;
6553 case FocusOut:
6554 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6555 goto OTHER;
6557 case MotionNotify:
6559 previous_help_echo_string = help_echo_string;
6560 help_echo_string = Qnil;
6562 if (dpyinfo->grabbed && last_mouse_frame
6563 && FRAME_LIVE_P (last_mouse_frame))
6564 f = last_mouse_frame;
6565 else
6566 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6568 if (dpyinfo->mouse_face_hidden)
6570 dpyinfo->mouse_face_hidden = 0;
6571 clear_mouse_face (dpyinfo);
6574 if (f)
6577 /* Generate SELECT_WINDOW_EVENTs when needed. */
6578 if (mouse_autoselect_window)
6580 Lisp_Object window;
6582 window = window_from_coordinates (f,
6583 event.xmotion.x, event.xmotion.y,
6584 0, 0, 0, 0);
6586 /* Window will be selected only when it is not selected now and
6587 last mouse movement event was not in it. Minibuffer window
6588 will be selected iff it is active. */
6589 if (WINDOWP (window)
6590 && !EQ (window, last_window)
6591 && !EQ (window, selected_window))
6593 inev.ie.kind = SELECT_WINDOW_EVENT;
6594 inev.ie.frame_or_window = window;
6597 last_window=window;
6599 if (!note_mouse_movement (f, &event.xmotion))
6600 help_echo_string = previous_help_echo_string;
6602 else
6604 #ifndef USE_TOOLKIT_SCROLL_BARS
6605 struct scroll_bar *bar
6606 = x_window_to_scroll_bar (event.xmotion.display,
6607 event.xmotion.window);
6609 if (bar)
6610 x_scroll_bar_note_movement (bar, &event);
6611 #endif /* USE_TOOLKIT_SCROLL_BARS */
6613 /* If we move outside the frame, then we're
6614 certainly no longer on any text in the frame. */
6615 clear_mouse_face (dpyinfo);
6618 /* If the contents of the global variable help_echo_string
6619 has changed, generate a HELP_EVENT. */
6620 if (!NILP (help_echo_string)
6621 || !NILP (previous_help_echo_string))
6622 do_help = 1;
6623 goto OTHER;
6626 case ConfigureNotify:
6627 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6628 if (f)
6630 #ifndef USE_X_TOOLKIT
6631 #ifdef USE_GTK
6632 xg_resize_widgets (f, event.xconfigure.width,
6633 event.xconfigure.height);
6634 #else /* not USE_GTK */
6635 /* If there is a pending resize for fullscreen, don't
6636 do this one, the right one will come later.
6637 The toolkit version doesn't seem to need this, but we
6638 need to reset it below. */
6639 int dont_resize
6640 = ((f->want_fullscreen & FULLSCREEN_WAIT)
6641 && f->new_text_cols != 0);
6642 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6643 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6645 if (dont_resize)
6646 goto OTHER;
6648 /* In the toolkit version, change_frame_size
6649 is called by the code that handles resizing
6650 of the EmacsFrame widget. */
6652 /* Even if the number of character rows and columns has
6653 not changed, the font size may have changed, so we need
6654 to check the pixel dimensions as well. */
6655 if (columns != FRAME_COLS (f)
6656 || rows != FRAME_LINES (f)
6657 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6658 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6660 change_frame_size (f, rows, columns, 0, 1, 0);
6661 SET_FRAME_GARBAGED (f);
6662 cancel_mouse_face (f);
6664 #endif /* not USE_GTK */
6665 #endif
6667 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6668 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
6670 #ifdef USE_GTK
6671 /* GTK creates windows but doesn't map them.
6672 Only get real positions and check fullscreen when mapped. */
6673 if (FRAME_GTK_OUTER_WIDGET (f)
6674 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6675 #endif
6677 /* What we have now is the position of Emacs's own window.
6678 Convert that to the position of the window manager window. */
6679 x_real_positions (f, &f->left_pos, &f->top_pos);
6681 x_check_expected_move (f);
6682 if (f->want_fullscreen & FULLSCREEN_WAIT)
6683 f->want_fullscreen &= ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
6686 #ifdef HAVE_X_I18N
6687 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6688 xic_set_statusarea (f);
6689 #endif
6691 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6693 /* Since the WM decorations come below top_pos now,
6694 we must put them below top_pos in the future. */
6695 f->win_gravity = NorthWestGravity;
6696 x_wm_set_size_hint (f, (long) 0, 0);
6699 goto OTHER;
6701 case ButtonRelease:
6702 case ButtonPress:
6704 /* If we decide we want to generate an event to be seen
6705 by the rest of Emacs, we put it here. */
6706 int tool_bar_p = 0;
6708 bzero (&compose_status, sizeof (compose_status));
6709 last_mouse_glyph_frame = 0;
6711 if (dpyinfo->grabbed
6712 && last_mouse_frame
6713 && FRAME_LIVE_P (last_mouse_frame))
6714 f = last_mouse_frame;
6715 else
6716 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6718 if (f)
6720 /* Is this in the tool-bar? */
6721 if (WINDOWP (f->tool_bar_window)
6722 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6724 Lisp_Object window;
6725 int x = event.xbutton.x;
6726 int y = event.xbutton.y;
6728 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
6729 if (EQ (window, f->tool_bar_window))
6731 if (event.xbutton.type == ButtonPress)
6732 handle_tool_bar_click (f, x, y, 1, 0);
6733 else
6734 handle_tool_bar_click (f, x, y, 0,
6735 x_x_to_emacs_modifiers (dpyinfo,
6736 event.xbutton.state));
6737 tool_bar_p = 1;
6741 if (!tool_bar_p)
6742 if (!dpyinfo->x_focus_frame
6743 || f == dpyinfo->x_focus_frame)
6745 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6746 if (! popup_activated ())
6747 #endif
6749 if (ignore_next_mouse_click_timeout)
6751 if (event.type == ButtonPress
6752 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
6754 ignore_next_mouse_click_timeout = 0;
6755 construct_mouse_click (&inev.ie, &event.xbutton, f);
6757 if (event.type == ButtonRelease)
6758 ignore_next_mouse_click_timeout = 0;
6760 else
6761 construct_mouse_click (&inev.ie, &event.xbutton, f);
6765 else
6767 struct scroll_bar *bar
6768 = x_window_to_scroll_bar (event.xbutton.display,
6769 event.xbutton.window);
6771 #ifdef USE_TOOLKIT_SCROLL_BARS
6772 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6773 scroll bars. */
6774 if (bar && event.xbutton.state & ControlMask)
6776 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6777 *finish = X_EVENT_DROP;
6779 #else /* not USE_TOOLKIT_SCROLL_BARS */
6780 if (bar)
6781 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6782 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6785 if (event.type == ButtonPress)
6787 dpyinfo->grabbed |= (1 << event.xbutton.button);
6788 last_mouse_frame = f;
6790 if (!tool_bar_p)
6791 last_tool_bar_item = -1;
6793 else
6794 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6796 /* Ignore any mouse motion that happened before this event;
6797 any subsequent mouse-movement Emacs events should reflect
6798 only motion after the ButtonPress/Release. */
6799 if (f != 0)
6800 f->mouse_moved = 0;
6802 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6803 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6804 /* For a down-event in the menu bar,
6805 don't pass it to Xt right now.
6806 Instead, save it away
6807 and we will pass it to Xt from kbd_buffer_get_event.
6808 That way, we can run some Lisp code first. */
6809 if (
6810 #ifdef USE_GTK
6811 ! popup_activated ()
6813 #endif
6814 f && event.type == ButtonPress
6815 /* Verify the event is really within the menu bar
6816 and not just sent to it due to grabbing. */
6817 && event.xbutton.x >= 0
6818 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
6819 && event.xbutton.y >= 0
6820 && event.xbutton.y < f->output_data.x->menubar_height
6821 && event.xbutton.same_screen)
6823 SET_SAVED_BUTTON_EVENT;
6824 XSETFRAME (last_mouse_press_frame, f);
6825 #ifdef USE_GTK
6826 *finish = X_EVENT_DROP;
6827 #endif
6829 else if (event.type == ButtonPress)
6831 last_mouse_press_frame = Qnil;
6832 goto OTHER;
6835 #ifdef USE_MOTIF /* This should do not harm for Lucid,
6836 but I am trying to be cautious. */
6837 else if (event.type == ButtonRelease)
6839 if (!NILP (last_mouse_press_frame))
6841 f = XFRAME (last_mouse_press_frame);
6842 if (f->output_data.x)
6843 SET_SAVED_BUTTON_EVENT;
6845 else
6846 goto OTHER;
6848 #endif /* USE_MOTIF */
6849 else
6850 goto OTHER;
6851 #endif /* USE_X_TOOLKIT || USE_GTK */
6853 break;
6855 case CirculateNotify:
6856 goto OTHER;
6858 case CirculateRequest:
6859 goto OTHER;
6861 case VisibilityNotify:
6862 goto OTHER;
6864 case MappingNotify:
6865 /* Someone has changed the keyboard mapping - update the
6866 local cache. */
6867 switch (event.xmapping.request)
6869 case MappingModifier:
6870 x_find_modifier_meanings (dpyinfo);
6871 /* This is meant to fall through. */
6872 case MappingKeyboard:
6873 XRefreshKeyboardMapping (&event.xmapping);
6875 goto OTHER;
6877 default:
6878 OTHER:
6879 #ifdef USE_X_TOOLKIT
6880 BLOCK_INPUT;
6881 if (*finish != X_EVENT_DROP)
6882 XtDispatchEvent (&event);
6883 UNBLOCK_INPUT;
6884 #endif /* USE_X_TOOLKIT */
6885 break;
6888 done:
6889 if (inev.ie.kind != NO_EVENT)
6891 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6892 count++;
6895 if (do_help
6896 && !(hold_quit && hold_quit->kind != NO_EVENT))
6898 Lisp_Object frame;
6900 if (f)
6901 XSETFRAME (frame, f);
6902 else
6903 frame = Qnil;
6905 if (do_help > 0)
6907 any_help_event_p = 1;
6908 gen_help_event (help_echo_string, frame, help_echo_window,
6909 help_echo_object, help_echo_pos);
6911 else
6913 help_echo_string = Qnil;
6914 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
6916 count++;
6919 *eventp = event;
6920 return count;
6924 /* Handles the XEvent EVENT on display DISPLAY.
6925 This is used for event loops outside the normal event handling,
6926 i.e. looping while a popup menu or a dialog is posted.
6928 Returns the value handle_one_xevent sets in the finish argument. */
6930 x_dispatch_event (event, display)
6931 XEvent *event;
6932 Display *display;
6934 struct x_display_info *dpyinfo;
6935 int finish = X_EVENT_NORMAL;
6937 dpyinfo = x_display_info_for_display (display);
6939 if (dpyinfo)
6940 handle_one_xevent (dpyinfo, event, &finish, 0);
6942 return finish;
6946 /* Read events coming from the X server.
6947 This routine is called by the SIGIO handler.
6948 We return as soon as there are no more events to be read.
6950 We return the number of characters stored into the buffer,
6951 thus pretending to be `read'.
6953 EXPECTED is nonzero if the caller knows input is available. */
6955 static int
6956 XTread_socket (terminal, expected, hold_quit)
6957 struct terminal *terminal;
6958 int expected;
6959 struct input_event *hold_quit;
6961 int count = 0;
6962 XEvent event;
6963 int event_found = 0;
6964 struct x_display_info *dpyinfo;
6966 if (interrupt_input_blocked)
6968 interrupt_input_pending = 1;
6969 return -1;
6972 interrupt_input_pending = 0;
6973 BLOCK_INPUT;
6975 /* So people can tell when we have read the available input. */
6976 input_signal_count++;
6978 ++handling_signal;
6980 /* Find the display we are supposed to read input for.
6981 It's the one communicating on descriptor SD. */
6982 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
6984 #if 0 /* This ought to be unnecessary; let's verify it. */
6985 #ifdef FIOSNBIO
6986 /* If available, Xlib uses FIOSNBIO to make the socket
6987 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
6988 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
6989 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
6990 fcntl (dpyinfo->connection, F_SETFL, 0);
6991 #endif /* ! defined (FIOSNBIO) */
6992 #endif
6994 #if 0 /* This code can't be made to work, with multiple displays,
6995 and appears not to be used on any system any more.
6996 Also keyboard.c doesn't turn O_NDELAY on and off
6997 for X connections. */
6998 #ifndef SIGIO
6999 #ifndef HAVE_SELECT
7000 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
7002 extern int read_alarm_should_throw;
7003 read_alarm_should_throw = 1;
7004 XPeekEvent (dpyinfo->display, &event);
7005 read_alarm_should_throw = 0;
7007 #endif /* HAVE_SELECT */
7008 #endif /* SIGIO */
7009 #endif
7011 /* For debugging, this gives a way to fake an I/O error. */
7012 if (dpyinfo == XTread_socket_fake_io_error)
7014 XTread_socket_fake_io_error = 0;
7015 x_io_error_quitter (dpyinfo->display);
7018 #ifdef HAVE_X_SM
7019 /* Only check session manager input for the primary display. */
7020 if (terminal->id == 1 && x_session_have_connection ())
7022 struct input_event inev;
7023 BLOCK_INPUT;
7024 /* We don't need to EVENT_INIT (inev) here, as
7025 x_session_check_input copies an entire input_event. */
7026 if (x_session_check_input (&inev))
7028 kbd_buffer_store_event_hold (&inev, hold_quit);
7029 count++;
7031 UNBLOCK_INPUT;
7033 #endif
7035 #ifndef USE_GTK
7036 while (XPending (dpyinfo->display))
7038 int finish;
7040 XNextEvent (dpyinfo->display, &event);
7042 #ifdef HAVE_X_I18N
7043 /* Filter events for the current X input method. */
7044 if (x_filter_event (dpyinfo, &event))
7045 break;
7046 #endif
7047 event_found = 1;
7049 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
7051 if (finish == X_EVENT_GOTO_OUT)
7052 goto out;
7054 #endif /* not USE_GTK */
7057 #ifdef USE_GTK
7059 /* For GTK we must use the GTK event loop. But XEvents gets passed
7060 to our filter function above, and then to the big event switch.
7061 We use a bunch of globals to communicate with our filter function,
7062 that is kind of ugly, but it works.
7064 There is no way to do one display at the time, GTK just does events
7065 from all displays. */
7067 while (gtk_events_pending ())
7069 current_count = count;
7070 current_hold_quit = hold_quit;
7072 gtk_main_iteration ();
7074 count = current_count;
7075 current_count = -1;
7076 current_hold_quit = 0;
7078 if (current_finish == X_EVENT_GOTO_OUT)
7079 break;
7081 #endif /* USE_GTK */
7083 out:;
7085 /* On some systems, an X bug causes Emacs to get no more events
7086 when the window is destroyed. Detect that. (1994.) */
7087 if (! event_found)
7089 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7090 One XNOOP in 100 loops will make Emacs terminate.
7091 B. Bretthauer, 1994 */
7092 x_noop_count++;
7093 if (x_noop_count >= 100)
7095 x_noop_count=0;
7097 if (next_noop_dpyinfo == 0)
7098 next_noop_dpyinfo = x_display_list;
7100 XNoOp (next_noop_dpyinfo->display);
7102 /* Each time we get here, cycle through the displays now open. */
7103 next_noop_dpyinfo = next_noop_dpyinfo->next;
7107 /* If the focus was just given to an auto-raising frame,
7108 raise it now. */
7109 /* ??? This ought to be able to handle more than one such frame. */
7110 if (pending_autoraise_frame)
7112 x_raise_frame (pending_autoraise_frame);
7113 pending_autoraise_frame = 0;
7116 --handling_signal;
7117 UNBLOCK_INPUT;
7119 return count;
7125 /***********************************************************************
7126 Text Cursor
7127 ***********************************************************************/
7129 /* Set clipping for output in glyph row ROW. W is the window in which
7130 we operate. GC is the graphics context to set clipping in.
7132 ROW may be a text row or, e.g., a mode line. Text rows must be
7133 clipped to the interior of the window dedicated to text display,
7134 mode lines must be clipped to the whole window. */
7136 static void
7137 x_clip_to_row (w, row, area, gc)
7138 struct window *w;
7139 struct glyph_row *row;
7140 int area;
7141 GC gc;
7143 struct frame *f = XFRAME (WINDOW_FRAME (w));
7144 XRectangle clip_rect;
7145 int window_x, window_y, window_width;
7147 window_box (w, area, &window_x, &window_y, &window_width, 0);
7149 clip_rect.x = window_x;
7150 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7151 clip_rect.y = max (clip_rect.y, window_y);
7152 clip_rect.width = window_width;
7153 clip_rect.height = row->visible_height;
7155 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7159 /* Draw a hollow box cursor on window W in glyph row ROW. */
7161 static void
7162 x_draw_hollow_cursor (w, row)
7163 struct window *w;
7164 struct glyph_row *row;
7166 struct frame *f = XFRAME (WINDOW_FRAME (w));
7167 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7168 Display *dpy = FRAME_X_DISPLAY (f);
7169 int x, y, wd, h;
7170 XGCValues xgcv;
7171 struct glyph *cursor_glyph;
7172 GC gc;
7174 /* Get the glyph the cursor is on. If we can't tell because
7175 the current matrix is invalid or such, give up. */
7176 cursor_glyph = get_phys_cursor_glyph (w);
7177 if (cursor_glyph == NULL)
7178 return;
7180 /* Compute frame-relative coordinates for phys cursor. */
7181 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
7182 y = get_phys_cursor_geometry (w, row, cursor_glyph, &h);
7183 wd = w->phys_cursor_width;
7185 /* The foreground of cursor_gc is typically the same as the normal
7186 background color, which can cause the cursor box to be invisible. */
7187 xgcv.foreground = f->output_data.x->cursor_pixel;
7188 if (dpyinfo->scratch_cursor_gc)
7189 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7190 else
7191 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7192 GCForeground, &xgcv);
7193 gc = dpyinfo->scratch_cursor_gc;
7195 /* Set clipping, draw the rectangle, and reset clipping again. */
7196 x_clip_to_row (w, row, TEXT_AREA, gc);
7197 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
7198 XSetClipMask (dpy, gc, None);
7202 /* Draw a bar cursor on window W in glyph row ROW.
7204 Implementation note: One would like to draw a bar cursor with an
7205 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7206 Unfortunately, I didn't find a font yet that has this property set.
7207 --gerd. */
7209 static void
7210 x_draw_bar_cursor (w, row, width, kind)
7211 struct window *w;
7212 struct glyph_row *row;
7213 int width;
7214 enum text_cursor_kinds kind;
7216 struct frame *f = XFRAME (w->frame);
7217 struct glyph *cursor_glyph;
7219 /* If cursor is out of bounds, don't draw garbage. This can happen
7220 in mini-buffer windows when switching between echo area glyphs
7221 and mini-buffer. */
7222 cursor_glyph = get_phys_cursor_glyph (w);
7223 if (cursor_glyph == NULL)
7224 return;
7226 /* If on an image, draw like a normal cursor. That's usually better
7227 visible than drawing a bar, esp. if the image is large so that
7228 the bar might not be in the window. */
7229 if (cursor_glyph->type == IMAGE_GLYPH)
7231 struct glyph_row *row;
7232 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7233 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7235 else
7237 Display *dpy = FRAME_X_DISPLAY (f);
7238 Window window = FRAME_X_WINDOW (f);
7239 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7240 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7241 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7242 XGCValues xgcv;
7244 /* If the glyph's background equals the color we normally draw
7245 the bar cursor in, the bar cursor in its normal color is
7246 invisible. Use the glyph's foreground color instead in this
7247 case, on the assumption that the glyph's colors are chosen so
7248 that the glyph is legible. */
7249 if (face->background == f->output_data.x->cursor_pixel)
7250 xgcv.background = xgcv.foreground = face->foreground;
7251 else
7252 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7253 xgcv.graphics_exposures = 0;
7255 if (gc)
7256 XChangeGC (dpy, gc, mask, &xgcv);
7257 else
7259 gc = XCreateGC (dpy, window, mask, &xgcv);
7260 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7263 if (width < 0)
7264 width = FRAME_CURSOR_WIDTH (f);
7265 width = min (cursor_glyph->pixel_width, width);
7267 w->phys_cursor_width = width;
7268 x_clip_to_row (w, row, TEXT_AREA, gc);
7270 if (kind == BAR_CURSOR)
7271 XFillRectangle (dpy, window, gc,
7272 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7273 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7274 width, row->height);
7275 else
7276 XFillRectangle (dpy, window, gc,
7277 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7278 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7279 row->height - width),
7280 cursor_glyph->pixel_width,
7281 width);
7283 XSetClipMask (dpy, gc, None);
7288 /* RIF: Define cursor CURSOR on frame F. */
7290 static void
7291 x_define_frame_cursor (f, cursor)
7292 struct frame *f;
7293 Cursor cursor;
7295 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7299 /* RIF: Clear area on frame F. */
7301 static void
7302 x_clear_frame_area (f, x, y, width, height)
7303 struct frame *f;
7304 int x, y, width, height;
7306 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7307 x, y, width, height, False);
7311 /* RIF: Draw cursor on window W. */
7313 static void
7314 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7315 struct window *w;
7316 struct glyph_row *glyph_row;
7317 int x, y;
7318 int cursor_type, cursor_width;
7319 int on_p, active_p;
7321 struct frame *f = XFRAME (WINDOW_FRAME (w));
7323 if (on_p)
7325 w->phys_cursor_type = cursor_type;
7326 w->phys_cursor_on_p = 1;
7328 if (glyph_row->exact_window_width_line_p
7329 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
7331 glyph_row->cursor_in_fringe_p = 1;
7332 draw_fringe_bitmap (w, glyph_row, 0);
7334 else
7335 switch (cursor_type)
7337 case HOLLOW_BOX_CURSOR:
7338 x_draw_hollow_cursor (w, glyph_row);
7339 break;
7341 case FILLED_BOX_CURSOR:
7342 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7343 break;
7345 case BAR_CURSOR:
7346 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7347 break;
7349 case HBAR_CURSOR:
7350 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7351 break;
7353 case NO_CURSOR:
7354 w->phys_cursor_width = 0;
7355 break;
7357 default:
7358 abort ();
7361 #ifdef HAVE_X_I18N
7362 if (w == XWINDOW (f->selected_window))
7363 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7364 xic_set_preeditarea (w, x, y);
7365 #endif
7368 #ifndef XFlush
7369 XFlush (FRAME_X_DISPLAY (f));
7370 #endif
7374 /* Icons. */
7376 /* Make the x-window of frame F use the gnu icon bitmap. */
7379 x_bitmap_icon (f, file)
7380 struct frame *f;
7381 Lisp_Object file;
7383 int bitmap_id;
7385 if (FRAME_X_WINDOW (f) == 0)
7386 return 1;
7388 /* Free up our existing icon bitmap and mask if any. */
7389 if (f->output_data.x->icon_bitmap > 0)
7390 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7391 f->output_data.x->icon_bitmap = 0;
7393 if (STRINGP (file))
7395 #ifdef USE_GTK
7396 /* Use gtk_window_set_icon_from_file () if available,
7397 It's not restricted to bitmaps */
7398 if (xg_set_icon (f, file))
7399 return 0;
7400 #endif /* USE_GTK */
7401 bitmap_id = x_create_bitmap_from_file (f, file);
7402 x_create_bitmap_mask (f, bitmap_id);
7404 else
7406 /* Create the GNU bitmap and mask if necessary. */
7407 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7409 int rc = -1;
7411 #if defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7412 #ifdef USE_GTK
7413 if (xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7414 return 0;
7415 #else
7416 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7417 if (rc != -1)
7418 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7419 #endif /* USE_GTK */
7420 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
7422 /* If all else fails, use the (black and white) xbm image. */
7423 if (rc == -1)
7425 rc = x_create_bitmap_from_data (f, gnu_xbm_bits,
7426 gnu_xbm_width, gnu_xbm_height);
7427 if (rc == -1)
7428 return 1;
7430 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7431 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7435 /* The first time we create the GNU bitmap and mask,
7436 this increments the ref-count one extra time.
7437 As a result, the GNU bitmap and mask are never freed.
7438 That way, we don't have to worry about allocating it again. */
7439 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7441 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7444 x_wm_set_icon_pixmap (f, bitmap_id);
7445 f->output_data.x->icon_bitmap = bitmap_id;
7447 return 0;
7451 /* Make the x-window of frame F use a rectangle with text.
7452 Use ICON_NAME as the text. */
7455 x_text_icon (f, icon_name)
7456 struct frame *f;
7457 char *icon_name;
7459 if (FRAME_X_WINDOW (f) == 0)
7460 return 1;
7462 #ifdef HAVE_X11R4
7464 XTextProperty text;
7465 text.value = (unsigned char *) icon_name;
7466 text.encoding = XA_STRING;
7467 text.format = 8;
7468 text.nitems = strlen (icon_name);
7469 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7471 #else /* not HAVE_X11R4 */
7472 XSetIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), icon_name);
7473 #endif /* not HAVE_X11R4 */
7475 if (f->output_data.x->icon_bitmap > 0)
7476 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7477 f->output_data.x->icon_bitmap = 0;
7478 x_wm_set_icon_pixmap (f, 0);
7480 return 0;
7483 #define X_ERROR_MESSAGE_SIZE 200
7485 /* If non-nil, this should be a string.
7486 It means catch X errors and store the error message in this string. */
7488 struct x_error_message_stack {
7489 char string[X_ERROR_MESSAGE_SIZE];
7490 Display *dpy;
7491 struct x_error_message_stack *prev;
7493 static struct x_error_message_stack *x_error_message;
7495 /* An X error handler which stores the error message in
7496 x_error_message_string. This is called from x_error_handler if
7497 x_catch_errors is in effect. */
7499 static void
7500 x_error_catcher (display, error)
7501 Display *display;
7502 XErrorEvent *error;
7504 XGetErrorText (display, error->error_code,
7505 x_error_message->string,
7506 X_ERROR_MESSAGE_SIZE);
7509 /* Begin trapping X errors for display DPY. Actually we trap X errors
7510 for all displays, but DPY should be the display you are actually
7511 operating on.
7513 After calling this function, X protocol errors no longer cause
7514 Emacs to exit; instead, they are recorded in the string
7515 stored in x_error_message_string.
7517 Calling x_check_errors signals an Emacs error if an X error has
7518 occurred since the last call to x_catch_errors or x_check_errors.
7520 Calling x_uncatch_errors resumes the normal error handling. */
7522 void x_check_errors ();
7523 static Lisp_Object x_catch_errors_unwind ();
7526 x_catch_errors (dpy)
7527 Display *dpy;
7529 int count = SPECPDL_INDEX ();
7530 struct x_error_message_stack *data = malloc (sizeof (*data));
7531 Lisp_Object dummy;
7532 #ifdef ENABLE_CHECKING
7533 dummy = make_number ((EMACS_INT)dpy + (EMACS_INT)x_error_message);
7534 #else
7535 dummy = Qnil;
7536 #endif
7538 /* Make sure any errors from previous requests have been dealt with. */
7539 XSync (dpy, False);
7541 data->dpy = dpy;
7542 data->string[0] = 0;
7543 data->prev = x_error_message;
7544 x_error_message = data;
7546 record_unwind_protect (x_catch_errors_unwind, dummy);
7548 return count;
7551 /* Unbind the binding that we made to check for X errors. */
7553 static Lisp_Object
7554 x_catch_errors_unwind (dummy)
7555 Lisp_Object dummy;
7557 Display *dpy;
7558 struct x_error_message_stack *tmp;
7560 if (!x_error_message)
7561 abort ();
7563 dpy = x_error_message->dpy;
7565 /* The display may have been closed before this function is called.
7566 Check if it is still open before calling XSync. */
7567 if (x_display_info_for_display (dpy) != 0)
7569 BLOCK_INPUT;
7570 XSync (dpy, False);
7571 UNBLOCK_INPUT;
7574 tmp = x_error_message;
7575 x_error_message = x_error_message->prev;
7576 free (tmp);
7578 eassert (EQ (dummy,
7579 make_number ((EMACS_INT)dpy + (EMACS_INT)x_error_message)));
7581 return Qnil;
7584 /* If any X protocol errors have arrived since the last call to
7585 x_catch_errors or x_check_errors, signal an Emacs error using
7586 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7588 void
7589 x_check_errors (dpy, format)
7590 Display *dpy;
7591 char *format;
7593 /* Make sure to catch any errors incurred so far. */
7594 XSync (dpy, False);
7596 if (x_error_message->string[0])
7597 error (format, x_error_message->string);
7600 /* Nonzero if we had any X protocol errors
7601 since we did x_catch_errors on DPY. */
7604 x_had_errors_p (dpy)
7605 Display *dpy;
7607 /* Make sure to catch any errors incurred so far. */
7608 XSync (dpy, False);
7610 return x_error_message->string[0] != 0;
7613 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7615 void
7616 x_clear_errors (dpy)
7617 Display *dpy;
7619 x_error_message->string[0] = 0;
7622 /* Stop catching X protocol errors and let them make Emacs die.
7623 DPY should be the display that was passed to x_catch_errors.
7624 COUNT should be the value that was returned by
7625 the corresponding call to x_catch_errors. */
7627 void
7628 x_uncatch_errors (dpy, count)
7629 Display *dpy;
7630 int count;
7632 unbind_to (count, Qnil);
7635 #if 0
7636 static unsigned int x_wire_count;
7637 x_trace_wire ()
7639 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7641 #endif /* ! 0 */
7644 /* Handle SIGPIPE, which can happen when the connection to a server
7645 simply goes away. SIGPIPE is handled by x_connection_signal.
7646 Don't need to do anything, because the write which caused the
7647 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7648 which will do the appropriate cleanup for us. */
7650 static SIGTYPE
7651 x_connection_signal (signalnum) /* If we don't have an argument, */
7652 int signalnum; /* some compilers complain in signal calls. */
7654 #ifdef USG
7655 /* USG systems forget handlers when they are used;
7656 must reestablish each time */
7657 signal (signalnum, x_connection_signal);
7658 #endif /* USG */
7662 /************************************************************************
7663 Handling X errors
7664 ************************************************************************/
7666 /* Error message passed to x_connection_closed. */
7668 static char *error_msg;
7670 /* Function installed as fatal_error_signal_hook in
7671 x_connection_closed. Print the X error message, and exit normally,
7672 instead of dumping core when XtCloseDisplay fails. */
7674 static void
7675 x_fatal_error_signal ()
7677 fprintf (stderr, "%s\n", error_msg);
7678 exit (70);
7681 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7682 the text of an error message that lead to the connection loss. */
7684 static SIGTYPE
7685 x_connection_closed (dpy, error_message)
7686 Display *dpy;
7687 char *error_message;
7689 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7690 Lisp_Object frame, tail;
7691 int count;
7692 int index = SPECPDL_INDEX ();
7694 error_msg = (char *) alloca (strlen (error_message) + 1);
7695 strcpy (error_msg, error_message);
7696 handling_signal = 0;
7698 /* Prevent being called recursively because of an error condition
7699 below. Otherwise, we might end up with printing ``can't find per
7700 display information'' in the recursive call instead of printing
7701 the original message here. */
7702 count = x_catch_errors (dpy);
7704 /* Inhibit redisplay while frames are being deleted. */
7705 specbind (Qinhibit_redisplay, Qt);
7707 if (dpyinfo)
7709 /* Protect display from being closed when we delete the last
7710 frame on it. */
7711 dpyinfo->reference_count++;
7712 dpyinfo->terminal->reference_count++;
7715 /* First delete frames whose mini-buffers are on frames
7716 that are on the dead display. */
7717 FOR_EACH_FRAME (tail, frame)
7719 Lisp_Object minibuf_frame;
7720 minibuf_frame
7721 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7722 if (FRAME_X_P (XFRAME (frame))
7723 && FRAME_X_P (XFRAME (minibuf_frame))
7724 && ! EQ (frame, minibuf_frame)
7725 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7726 Fdelete_frame (frame, Qt);
7729 /* Now delete all remaining frames on the dead display.
7730 We are now sure none of these is used as the mini-buffer
7731 for another frame that we need to delete. */
7732 FOR_EACH_FRAME (tail, frame)
7733 if (FRAME_X_P (XFRAME (frame))
7734 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7736 /* Set this to t so that Fdelete_frame won't get confused
7737 trying to find a replacement. */
7738 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7739 Fdelete_frame (frame, Qt);
7742 /* We have to close the display to inform Xt that it doesn't
7743 exist anymore. If we don't, Xt will continue to wait for
7744 events from the display. As a consequence, a sequence of
7746 M-x make-frame-on-display RET :1 RET
7747 ...kill the new frame, so that we get an IO error...
7748 M-x make-frame-on-display RET :1 RET
7750 will indefinitely wait in Xt for events for display `:1', opened
7751 in the first class to make-frame-on-display.
7753 Closing the display is reported to lead to a bus error on
7754 OpenWindows in certain situations. I suspect that is a bug
7755 in OpenWindows. I don't know how to cicumvent it here. */
7757 #ifdef USE_X_TOOLKIT
7758 /* If DPYINFO is null, this means we didn't open the display
7759 in the first place, so don't try to close it. */
7760 if (dpyinfo)
7762 extern void (*fatal_error_signal_hook) P_ ((void));
7763 fatal_error_signal_hook = x_fatal_error_signal;
7764 XtCloseDisplay (dpy);
7765 fatal_error_signal_hook = NULL;
7767 #endif
7769 #ifdef USE_GTK
7770 if (dpyinfo)
7771 xg_display_close (dpyinfo->display);
7772 #endif
7774 if (dpyinfo)
7776 /* Indicate that this display is dead. */
7777 dpyinfo->display = 0;
7779 dpyinfo->reference_count--;
7780 dpyinfo->terminal->reference_count--;
7781 if (dpyinfo->reference_count != 0)
7782 /* We have just closed all frames on this display. */
7783 abort ();
7785 x_delete_display (dpyinfo);
7788 x_uncatch_errors (dpy, count);
7790 if (terminal_list == 0)
7792 fprintf (stderr, "%s\n", error_msg);
7793 shut_down_emacs (0, 0, Qnil);
7794 exit (70);
7797 /* Ordinary stack unwind doesn't deal with these. */
7798 #ifdef SIGIO
7799 sigunblock (sigmask (SIGIO));
7800 #endif
7801 sigunblock (sigmask (SIGALRM));
7802 TOTALLY_UNBLOCK_INPUT;
7804 unbind_to (index, Qnil);
7805 clear_waiting_for_input ();
7806 error ("%s", error_msg);
7809 /* We specifically use it before defining it, so that gcc doesn't inline it,
7810 otherwise gdb doesn't know how to properly put a breakpoint on it. */
7811 static void x_error_quitter (Display *display, XErrorEvent *error);
7813 /* This is the first-level handler for X protocol errors.
7814 It calls x_error_quitter or x_error_catcher. */
7816 static int
7817 x_error_handler (display, error)
7818 Display *display;
7819 XErrorEvent *error;
7821 if (x_error_message)
7822 x_error_catcher (display, error);
7823 else
7824 x_error_quitter (display, error);
7825 return 0;
7828 /* This is the usual handler for X protocol errors.
7829 It kills all frames on the display that we got the error for.
7830 If that was the only one, it prints an error message and kills Emacs. */
7832 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
7834 #if __GNUC__ >= 3 /* On GCC 3.0 we might get a warning. */
7835 #define NO_INLINE __attribute__((noinline))
7836 #else
7837 #define NO_INLINE
7838 #endif
7840 /* Some versions of GNU/Linux define noinline in their headers. */
7842 #ifdef noinline
7843 #undef noinline
7844 #endif
7846 /* On older GCC versions, just putting x_error_quitter
7847 after x_error_handler prevents inlining into the former. */
7849 static void NO_INLINE
7850 x_error_quitter (display, error)
7851 Display *display;
7852 XErrorEvent *error;
7854 char buf[256], buf1[356];
7856 /* Note that there is no real way portable across R3/R4 to get the
7857 original error handler. */
7859 XGetErrorText (display, error->error_code, buf, sizeof (buf));
7860 sprintf (buf1, "X protocol error: %s on protocol request %d",
7861 buf, error->request_code);
7862 x_connection_closed (display, buf1);
7866 /* This is the handler for X IO errors, always.
7867 It kills all frames on the display that we lost touch with.
7868 If that was the only one, it prints an error message and kills Emacs. */
7870 static int
7871 x_io_error_quitter (display)
7872 Display *display;
7874 char buf[256];
7876 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
7877 x_connection_closed (display, buf);
7878 return 0;
7881 /* Changing the font of the frame. */
7883 /* Give frame F the font named FONTNAME as its default font, and
7884 return the full name of that font. FONTNAME may be a wildcard
7885 pattern; in that case, we choose some font that fits the pattern.
7886 The return value shows which font we chose. */
7888 Lisp_Object
7889 x_new_font (f, fontname)
7890 struct frame *f;
7891 register char *fontname;
7893 struct font_info *fontp
7894 = FS_LOAD_FONT (f, 0, fontname, -1);
7896 if (!fontp)
7897 return Qnil;
7899 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
7900 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
7901 FRAME_FONTSET (f) = -1;
7903 FRAME_COLUMN_WIDTH (f) = fontp->average_width;
7904 FRAME_SPACE_WIDTH (f) = fontp->space_width;
7905 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (FRAME_FONT (f));
7907 compute_fringe_widths (f, 1);
7909 /* Compute the scroll bar width in character columns. */
7910 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
7912 int wid = FRAME_COLUMN_WIDTH (f);
7913 FRAME_CONFIG_SCROLL_BAR_COLS (f)
7914 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
7916 else
7918 int wid = FRAME_COLUMN_WIDTH (f);
7919 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
7922 /* Now make the frame display the given font. */
7923 if (FRAME_X_WINDOW (f) != 0)
7925 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
7926 FRAME_FONT (f)->fid);
7927 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
7928 FRAME_FONT (f)->fid);
7929 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
7930 FRAME_FONT (f)->fid);
7932 /* Don't change the size of a tip frame; there's no point in
7933 doing it because it's done in Fx_show_tip, and it leads to
7934 problems because the tip frame has no widget. */
7935 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
7936 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
7939 return build_string (fontp->full_name);
7942 /* Give frame F the fontset named FONTSETNAME as its default font, and
7943 return the full name of that fontset. FONTSETNAME may be a wildcard
7944 pattern; in that case, we choose some fontset that fits the pattern.
7945 The return value shows which fontset we chose. */
7947 Lisp_Object
7948 x_new_fontset (f, fontsetname)
7949 struct frame *f;
7950 char *fontsetname;
7952 int fontset = fs_query_fontset (build_string (fontsetname), 0);
7953 Lisp_Object result;
7955 if (fontset < 0)
7956 return Qnil;
7958 if (FRAME_FONTSET (f) == fontset)
7959 /* This fontset is already set in frame F. There's nothing more
7960 to do. */
7961 return fontset_name (fontset);
7963 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
7965 if (!STRINGP (result))
7966 /* Can't load ASCII font. */
7967 return Qnil;
7969 /* Since x_new_font doesn't update any fontset information, do it now. */
7970 FRAME_FONTSET (f) = fontset;
7972 #ifdef HAVE_X_I18N
7973 if (FRAME_XIC (f)
7974 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
7975 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
7976 #endif
7978 return build_string (fontsetname);
7982 /***********************************************************************
7983 X Input Methods
7984 ***********************************************************************/
7986 #ifdef HAVE_X_I18N
7988 #ifdef HAVE_X11R6
7990 /* XIM destroy callback function, which is called whenever the
7991 connection to input method XIM dies. CLIENT_DATA contains a
7992 pointer to the x_display_info structure corresponding to XIM. */
7994 static void
7995 xim_destroy_callback (xim, client_data, call_data)
7996 XIM xim;
7997 XPointer client_data;
7998 XPointer call_data;
8000 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8001 Lisp_Object frame, tail;
8003 BLOCK_INPUT;
8005 /* No need to call XDestroyIC.. */
8006 FOR_EACH_FRAME (tail, frame)
8008 struct frame *f = XFRAME (frame);
8009 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8011 FRAME_XIC (f) = NULL;
8012 xic_free_xfontset (f);
8016 /* No need to call XCloseIM. */
8017 dpyinfo->xim = NULL;
8018 XFree (dpyinfo->xim_styles);
8019 UNBLOCK_INPUT;
8022 #endif /* HAVE_X11R6 */
8024 #ifdef HAVE_X11R6
8025 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8026 extern char *XSetIMValues P_ ((XIM, ...));
8027 #endif
8029 /* Open the connection to the XIM server on display DPYINFO.
8030 RESOURCE_NAME is the resource name Emacs uses. */
8032 static void
8033 xim_open_dpy (dpyinfo, resource_name)
8034 struct x_display_info *dpyinfo;
8035 char *resource_name;
8037 XIM xim;
8039 #ifdef HAVE_XIM
8040 if (use_xim)
8042 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8043 EMACS_CLASS);
8044 dpyinfo->xim = xim;
8046 if (xim)
8048 #ifdef HAVE_X11R6
8049 XIMCallback destroy;
8050 #endif
8052 /* Get supported styles and XIM values. */
8053 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8055 #ifdef HAVE_X11R6
8056 destroy.callback = xim_destroy_callback;
8057 destroy.client_data = (XPointer)dpyinfo;
8058 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8059 #endif
8063 else
8064 #endif /* HAVE_XIM */
8065 dpyinfo->xim = NULL;
8069 #ifdef HAVE_X11R6_XIM
8071 struct xim_inst_t
8073 struct x_display_info *dpyinfo;
8074 char *resource_name;
8077 /* XIM instantiate callback function, which is called whenever an XIM
8078 server is available. DISPLAY is the display of the XIM.
8079 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8080 when the callback was registered. */
8082 static void
8083 xim_instantiate_callback (display, client_data, call_data)
8084 Display *display;
8085 XPointer client_data;
8086 XPointer call_data;
8088 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8089 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8091 /* We don't support multiple XIM connections. */
8092 if (dpyinfo->xim)
8093 return;
8095 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8097 /* Create XIC for the existing frames on the same display, as long
8098 as they have no XIC. */
8099 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8101 Lisp_Object tail, frame;
8103 BLOCK_INPUT;
8104 FOR_EACH_FRAME (tail, frame)
8106 struct frame *f = XFRAME (frame);
8108 if (FRAME_X_P (f)
8109 && FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8110 if (FRAME_XIC (f) == NULL)
8112 create_frame_xic (f);
8113 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8114 xic_set_statusarea (f);
8115 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8117 struct window *w = XWINDOW (f->selected_window);
8118 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8123 UNBLOCK_INPUT;
8127 #endif /* HAVE_X11R6_XIM */
8130 /* Open a connection to the XIM server on display DPYINFO.
8131 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8132 connection only at the first time. On X11R6, open the connection
8133 in the XIM instantiate callback function. */
8135 static void
8136 xim_initialize (dpyinfo, resource_name)
8137 struct x_display_info *dpyinfo;
8138 char *resource_name;
8140 #ifdef HAVE_XIM
8141 if (use_xim)
8143 #ifdef HAVE_X11R6_XIM
8144 struct xim_inst_t *xim_inst;
8145 int len;
8147 dpyinfo->xim = NULL;
8148 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8149 xim_inst->dpyinfo = dpyinfo;
8150 len = strlen (resource_name);
8151 xim_inst->resource_name = (char *) xmalloc (len + 1);
8152 bcopy (resource_name, xim_inst->resource_name, len + 1);
8153 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8154 resource_name, EMACS_CLASS,
8155 xim_instantiate_callback,
8156 /* This is XPointer in XFree86
8157 but (XPointer *) on Tru64, at
8158 least, hence the configure test. */
8159 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8160 #else /* not HAVE_X11R6_XIM */
8161 dpyinfo->xim = NULL;
8162 xim_open_dpy (dpyinfo, resource_name);
8163 #endif /* not HAVE_X11R6_XIM */
8166 else
8167 #endif /* HAVE_XIM */
8168 dpyinfo->xim = NULL;
8172 /* Close the connection to the XIM server on display DPYINFO. */
8174 static void
8175 xim_close_dpy (dpyinfo)
8176 struct x_display_info *dpyinfo;
8178 #ifdef HAVE_XIM
8179 if (use_xim)
8181 #ifdef HAVE_X11R6_XIM
8182 if (dpyinfo->display)
8183 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8184 NULL, EMACS_CLASS,
8185 xim_instantiate_callback, NULL);
8186 #endif /* not HAVE_X11R6_XIM */
8187 if (dpyinfo->display)
8188 XCloseIM (dpyinfo->xim);
8189 dpyinfo->xim = NULL;
8190 XFree (dpyinfo->xim_styles);
8192 #endif /* HAVE_XIM */
8195 #endif /* not HAVE_X11R6_XIM */
8199 /* Calculate the absolute position in frame F
8200 from its current recorded position values and gravity. */
8202 void
8203 x_calc_absolute_position (f)
8204 struct frame *f;
8206 int flags = f->size_hint_flags;
8208 /* We have nothing to do if the current position
8209 is already for the top-left corner. */
8210 if (! ((flags & XNegative) || (flags & YNegative)))
8211 return;
8213 /* Treat negative positions as relative to the leftmost bottommost
8214 position that fits on the screen. */
8215 if (flags & XNegative)
8216 f->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
8217 - FRAME_PIXEL_WIDTH (f) + f->left_pos);
8220 int height = FRAME_PIXEL_HEIGHT (f);
8222 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8223 /* Something is fishy here. When using Motif, starting Emacs with
8224 `-g -0-0', the frame appears too low by a few pixels.
8226 This seems to be so because initially, while Emacs is starting,
8227 the column widget's height and the frame's pixel height are
8228 different. The column widget's height is the right one. In
8229 later invocations, when Emacs is up, the frame's pixel height
8230 is right, though.
8232 It's not obvious where the initial small difference comes from.
8233 2000-12-01, gerd. */
8235 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8236 #endif
8238 if (flags & YNegative)
8239 f->top_pos = (FRAME_X_DISPLAY_INFO (f)->height - height + f->top_pos);
8242 /* The left_pos and top_pos
8243 are now relative to the top and left screen edges,
8244 so the flags should correspond. */
8245 f->size_hint_flags &= ~ (XNegative | YNegative);
8248 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8249 to really change the position, and 0 when calling from
8250 x_make_frame_visible (in that case, XOFF and YOFF are the current
8251 position values). It is -1 when calling from x_set_frame_parameters,
8252 which means, do adjust for borders but don't change the gravity. */
8254 void
8255 x_set_offset (f, xoff, yoff, change_gravity)
8256 struct frame *f;
8257 register int xoff, yoff;
8258 int change_gravity;
8260 int modified_top, modified_left;
8262 if (change_gravity > 0)
8264 f->top_pos = yoff;
8265 f->left_pos = xoff;
8266 f->size_hint_flags &= ~ (XNegative | YNegative);
8267 if (xoff < 0)
8268 f->size_hint_flags |= XNegative;
8269 if (yoff < 0)
8270 f->size_hint_flags |= YNegative;
8271 f->win_gravity = NorthWestGravity;
8273 x_calc_absolute_position (f);
8275 BLOCK_INPUT;
8276 x_wm_set_size_hint (f, (long) 0, 0);
8278 modified_left = f->left_pos;
8279 modified_top = f->top_pos;
8281 if (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8283 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8284 than the WM decorations. So we use the calculated offset instead
8285 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8286 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8287 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8290 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8291 modified_left, modified_top);
8293 if (FRAME_VISIBLE_P (f)
8294 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN)
8296 FRAME_X_OUTPUT (f)->check_expected_move = 1;
8297 FRAME_X_OUTPUT (f)->expected_top = f->top_pos;
8298 FRAME_X_OUTPUT (f)->expected_left = f->left_pos;
8301 UNBLOCK_INPUT;
8304 /* Check if we need to resize the frame due to a fullscreen request.
8305 If so needed, resize the frame. */
8306 static void
8307 x_check_fullscreen (f)
8308 struct frame *f;
8310 if (f->want_fullscreen & FULLSCREEN_BOTH)
8312 int width, height, ign;
8314 x_real_positions (f, &f->left_pos, &f->top_pos);
8316 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
8318 /* We do not need to move the window, it shall be taken care of
8319 when setting WM manager hints.
8320 If the frame is visible already, the position is checked by
8321 x_check_expected_move. */
8322 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
8324 change_frame_size (f, height, width, 0, 1, 0);
8325 SET_FRAME_GARBAGED (f);
8326 cancel_mouse_face (f);
8328 /* Wait for the change of frame size to occur */
8329 f->want_fullscreen |= FULLSCREEN_WAIT;
8334 /* If frame parameters are set after the frame is mapped, we need to move
8335 the window.
8336 Some window managers moves the window to the right position, some
8337 moves the outer window manager window to the specified position.
8338 Here we check that we are in the right spot. If not, make a second
8339 move, assuming we are dealing with the second kind of window manager. */
8340 static void
8341 x_check_expected_move (f)
8342 struct frame *f;
8344 if (FRAME_X_OUTPUT (f)->check_expected_move)
8346 int expect_top = FRAME_X_OUTPUT (f)->expected_top;
8347 int expect_left = FRAME_X_OUTPUT (f)->expected_left;
8349 if (expect_top != f->top_pos || expect_left != f->left_pos)
8351 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8352 FRAME_X_OUTPUT (f)->move_offset_left = expect_left - f->left_pos;
8353 FRAME_X_OUTPUT (f)->move_offset_top = expect_top - f->top_pos;
8355 f->left_pos = expect_left;
8356 f->top_pos = expect_top;
8357 x_set_offset (f, expect_left, expect_top, 0);
8359 else if (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN)
8360 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8362 /* Just do this once */
8363 FRAME_X_OUTPUT (f)->check_expected_move = 0;
8368 /* Change the size of frame F's X window to COLS/ROWS in the case F
8369 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8370 top-left-corner window gravity for this size change and subsequent
8371 size changes. Otherwise we leave the window gravity unchanged. */
8373 static void
8374 x_set_window_size_1 (f, change_gravity, cols, rows)
8375 struct frame *f;
8376 int change_gravity;
8377 int cols, rows;
8379 int pixelwidth, pixelheight;
8381 check_frame_size (f, &rows, &cols);
8382 f->scroll_bar_actual_width
8383 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8385 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
8386 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
8387 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
8389 compute_fringe_widths (f, 0);
8391 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
8392 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
8394 f->win_gravity = NorthWestGravity;
8395 x_wm_set_size_hint (f, (long) 0, 0);
8397 XSync (FRAME_X_DISPLAY (f), False);
8398 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8399 pixelwidth, pixelheight);
8401 /* Now, strictly speaking, we can't be sure that this is accurate,
8402 but the window manager will get around to dealing with the size
8403 change request eventually, and we'll hear how it went when the
8404 ConfigureNotify event gets here.
8406 We could just not bother storing any of this information here,
8407 and let the ConfigureNotify event set everything up, but that
8408 might be kind of confusing to the Lisp code, since size changes
8409 wouldn't be reported in the frame parameters until some random
8410 point in the future when the ConfigureNotify event arrives.
8412 We pass 1 for DELAY since we can't run Lisp code inside of
8413 a BLOCK_INPUT. */
8414 change_frame_size (f, rows, cols, 0, 1, 0);
8415 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8416 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8418 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8419 receive in the ConfigureNotify event; if we get what we asked
8420 for, then the event won't cause the screen to become garbaged, so
8421 we have to make sure to do it here. */
8422 SET_FRAME_GARBAGED (f);
8424 XFlush (FRAME_X_DISPLAY (f));
8428 /* Call this to change the size of frame F's x-window.
8429 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8430 for this size change and subsequent size changes.
8431 Otherwise we leave the window gravity unchanged. */
8433 void
8434 x_set_window_size (f, change_gravity, cols, rows)
8435 struct frame *f;
8436 int change_gravity;
8437 int cols, rows;
8439 BLOCK_INPUT;
8441 #ifdef USE_GTK
8442 if (FRAME_GTK_WIDGET (f))
8443 xg_frame_set_char_size (f, cols, rows);
8444 else
8445 x_set_window_size_1 (f, change_gravity, cols, rows);
8446 #elif USE_X_TOOLKIT
8448 if (f->output_data.x->widget != NULL)
8450 /* The x and y position of the widget is clobbered by the
8451 call to XtSetValues within EmacsFrameSetCharSize.
8452 This is a real kludge, but I don't understand Xt so I can't
8453 figure out a correct fix. Can anyone else tell me? -- rms. */
8454 int xpos = f->output_data.x->widget->core.x;
8455 int ypos = f->output_data.x->widget->core.y;
8456 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
8457 f->output_data.x->widget->core.x = xpos;
8458 f->output_data.x->widget->core.y = ypos;
8460 else
8461 x_set_window_size_1 (f, change_gravity, cols, rows);
8463 #else /* not USE_X_TOOLKIT */
8465 x_set_window_size_1 (f, change_gravity, cols, rows);
8467 #endif /* not USE_X_TOOLKIT */
8469 /* If cursor was outside the new size, mark it as off. */
8470 mark_window_cursors_off (XWINDOW (f->root_window));
8472 /* Clear out any recollection of where the mouse highlighting was,
8473 since it might be in a place that's outside the new frame size.
8474 Actually checking whether it is outside is a pain in the neck,
8475 so don't try--just let the highlighting be done afresh with new size. */
8476 cancel_mouse_face (f);
8478 UNBLOCK_INPUT;
8481 /* Mouse warping. */
8483 void
8484 x_set_mouse_position (f, x, y)
8485 struct frame *f;
8486 int x, y;
8488 int pix_x, pix_y;
8490 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
8491 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
8493 if (pix_x < 0) pix_x = 0;
8494 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
8496 if (pix_y < 0) pix_y = 0;
8497 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
8499 BLOCK_INPUT;
8501 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8502 0, 0, 0, 0, pix_x, pix_y);
8503 UNBLOCK_INPUT;
8506 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
8508 void
8509 x_set_mouse_pixel_position (f, pix_x, pix_y)
8510 struct frame *f;
8511 int pix_x, pix_y;
8513 BLOCK_INPUT;
8515 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8516 0, 0, 0, 0, pix_x, pix_y);
8517 UNBLOCK_INPUT;
8520 /* focus shifting, raising and lowering. */
8522 void
8523 x_focus_on_frame (f)
8524 struct frame *f;
8526 #if 0 /* This proves to be unpleasant. */
8527 x_raise_frame (f);
8528 #endif
8529 #if 0
8530 /* I don't think that the ICCCM allows programs to do things like this
8531 without the interaction of the window manager. Whatever you end up
8532 doing with this code, do it to x_unfocus_frame too. */
8533 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8534 RevertToPointerRoot, CurrentTime);
8535 #endif /* ! 0 */
8538 void
8539 x_unfocus_frame (f)
8540 struct frame *f;
8542 #if 0
8543 /* Look at the remarks in x_focus_on_frame. */
8544 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
8545 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
8546 RevertToPointerRoot, CurrentTime);
8547 #endif /* ! 0 */
8550 /* Raise frame F. */
8552 void
8553 x_raise_frame (f)
8554 struct frame *f;
8556 if (f->async_visible)
8558 BLOCK_INPUT;
8559 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8560 XFlush (FRAME_X_DISPLAY (f));
8561 UNBLOCK_INPUT;
8565 /* Lower frame F. */
8567 void
8568 x_lower_frame (f)
8569 struct frame *f;
8571 if (f->async_visible)
8573 BLOCK_INPUT;
8574 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8575 XFlush (FRAME_X_DISPLAY (f));
8576 UNBLOCK_INPUT;
8580 static void
8581 XTframe_raise_lower (f, raise_flag)
8582 FRAME_PTR f;
8583 int raise_flag;
8585 if (raise_flag)
8586 x_raise_frame (f);
8587 else
8588 x_lower_frame (f);
8591 /* Change of visibility. */
8593 /* This tries to wait until the frame is really visible.
8594 However, if the window manager asks the user where to position
8595 the frame, this will return before the user finishes doing that.
8596 The frame will not actually be visible at that time,
8597 but it will become visible later when the window manager
8598 finishes with it. */
8600 void
8601 x_make_frame_visible (f)
8602 struct frame *f;
8604 Lisp_Object type;
8605 int original_top, original_left;
8606 int retry_count = 2;
8608 retry:
8610 BLOCK_INPUT;
8612 type = x_icon_type (f);
8613 if (!NILP (type))
8614 x_bitmap_icon (f, type);
8616 if (! FRAME_VISIBLE_P (f))
8618 /* We test FRAME_GARBAGED_P here to make sure we don't
8619 call x_set_offset a second time
8620 if we get to x_make_frame_visible a second time
8621 before the window gets really visible. */
8622 if (! FRAME_ICONIFIED_P (f)
8623 && ! f->output_data.x->asked_for_visible)
8624 x_set_offset (f, f->left_pos, f->top_pos, 0);
8626 f->output_data.x->asked_for_visible = 1;
8628 if (! EQ (Vx_no_window_manager, Qt))
8629 x_wm_set_window_state (f, NormalState);
8630 #ifdef USE_X_TOOLKIT
8631 /* This was XtPopup, but that did nothing for an iconified frame. */
8632 XtMapWidget (f->output_data.x->widget);
8633 #else /* not USE_X_TOOLKIT */
8634 #ifdef USE_GTK
8635 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
8636 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
8637 #else
8638 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8639 #endif /* not USE_GTK */
8640 #endif /* not USE_X_TOOLKIT */
8641 #if 0 /* This seems to bring back scroll bars in the wrong places
8642 if the window configuration has changed. They seem
8643 to come back ok without this. */
8644 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8645 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8646 #endif
8649 XFlush (FRAME_X_DISPLAY (f));
8651 /* Synchronize to ensure Emacs knows the frame is visible
8652 before we do anything else. We do this loop with input not blocked
8653 so that incoming events are handled. */
8655 Lisp_Object frame;
8656 int count;
8657 /* This must be before UNBLOCK_INPUT
8658 since events that arrive in response to the actions above
8659 will set it when they are handled. */
8660 int previously_visible = f->output_data.x->has_been_visible;
8662 original_left = f->left_pos;
8663 original_top = f->top_pos;
8665 /* This must come after we set COUNT. */
8666 UNBLOCK_INPUT;
8668 /* We unblock here so that arriving X events are processed. */
8670 /* Now move the window back to where it was "supposed to be".
8671 But don't do it if the gravity is negative.
8672 When the gravity is negative, this uses a position
8673 that is 3 pixels too low. Perhaps that's really the border width.
8675 Don't do this if the window has never been visible before,
8676 because the window manager may choose the position
8677 and we don't want to override it. */
8679 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
8680 && f->win_gravity == NorthWestGravity
8681 && previously_visible)
8683 Drawable rootw;
8684 int x, y;
8685 unsigned int width, height, border, depth;
8687 BLOCK_INPUT;
8689 /* On some window managers (such as FVWM) moving an existing
8690 window, even to the same place, causes the window manager
8691 to introduce an offset. This can cause the window to move
8692 to an unexpected location. Check the geometry (a little
8693 slow here) and then verify that the window is in the right
8694 place. If the window is not in the right place, move it
8695 there, and take the potential window manager hit. */
8696 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8697 &rootw, &x, &y, &width, &height, &border, &depth);
8699 if (original_left != x || original_top != y)
8700 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8701 original_left, original_top);
8703 UNBLOCK_INPUT;
8706 XSETFRAME (frame, f);
8708 /* Wait until the frame is visible. Process X events until a
8709 MapNotify event has been seen, or until we think we won't get a
8710 MapNotify at all.. */
8711 for (count = input_signal_count + 10;
8712 input_signal_count < count && !FRAME_VISIBLE_P (f);)
8714 /* Force processing of queued events. */
8715 x_sync (f);
8717 /* Machines that do polling rather than SIGIO have been
8718 observed to go into a busy-wait here. So we'll fake an
8719 alarm signal to let the handler know that there's something
8720 to be read. We used to raise a real alarm, but it seems
8721 that the handler isn't always enabled here. This is
8722 probably a bug. */
8723 if (input_polling_used ())
8725 /* It could be confusing if a real alarm arrives while
8726 processing the fake one. Turn it off and let the
8727 handler reset it. */
8728 extern void poll_for_input_1 P_ ((void));
8729 int old_poll_suppress_count = poll_suppress_count;
8730 poll_suppress_count = 1;
8731 poll_for_input_1 ();
8732 poll_suppress_count = old_poll_suppress_count;
8735 /* See if a MapNotify event has been processed. */
8736 FRAME_SAMPLE_VISIBILITY (f);
8739 /* 2000-09-28: In
8741 (let ((f (selected-frame)))
8742 (iconify-frame f)
8743 (raise-frame f))
8745 the frame is not raised with various window managers on
8746 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
8747 unknown reason, the call to XtMapWidget is completely ignored.
8748 Mapping the widget a second time works. */
8750 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
8751 goto retry;
8755 /* Change from mapped state to withdrawn state. */
8757 /* Make the frame visible (mapped and not iconified). */
8759 void
8760 x_make_frame_invisible (f)
8761 struct frame *f;
8763 Window window;
8765 /* Use the frame's outermost window, not the one we normally draw on. */
8766 window = FRAME_OUTER_WINDOW (f);
8768 /* Don't keep the highlight on an invisible frame. */
8769 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
8770 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
8772 #if 0/* This might add unreliability; I don't trust it -- rms. */
8773 if (! f->async_visible && ! f->async_iconified)
8774 return;
8775 #endif
8777 BLOCK_INPUT;
8779 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
8780 that the current position of the window is user-specified, rather than
8781 program-specified, so that when the window is mapped again, it will be
8782 placed at the same location, without forcing the user to position it
8783 by hand again (they have already done that once for this window.) */
8784 x_wm_set_size_hint (f, (long) 0, 1);
8786 #ifdef USE_GTK
8787 if (FRAME_GTK_OUTER_WIDGET (f))
8788 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
8789 else
8790 #endif
8792 #ifdef HAVE_X11R4
8794 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
8795 DefaultScreen (FRAME_X_DISPLAY (f))))
8797 UNBLOCK_INPUT_RESIGNAL;
8798 error ("Can't notify window manager of window withdrawal");
8800 #else /* ! defined (HAVE_X11R4) */
8802 /* Tell the window manager what we're going to do. */
8803 if (! EQ (Vx_no_window_manager, Qt))
8805 XEvent unmap;
8807 unmap.xunmap.type = UnmapNotify;
8808 unmap.xunmap.window = window;
8809 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
8810 unmap.xunmap.from_configure = False;
8811 if (! XSendEvent (FRAME_X_DISPLAY (f),
8812 DefaultRootWindow (FRAME_X_DISPLAY (f)),
8813 False,
8814 SubstructureRedirectMaskSubstructureNotifyMask,
8815 &unmap))
8817 UNBLOCK_INPUT_RESIGNAL;
8818 error ("Can't notify window manager of withdrawal");
8822 /* Unmap the window ourselves. Cheeky! */
8823 XUnmapWindow (FRAME_X_DISPLAY (f), window);
8824 #endif /* ! defined (HAVE_X11R4) */
8827 /* We can't distinguish this from iconification
8828 just by the event that we get from the server.
8829 So we can't win using the usual strategy of letting
8830 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
8831 and synchronize with the server to make sure we agree. */
8832 f->visible = 0;
8833 FRAME_ICONIFIED_P (f) = 0;
8834 f->async_visible = 0;
8835 f->async_iconified = 0;
8837 x_sync (f);
8839 UNBLOCK_INPUT;
8842 /* Change window state from mapped to iconified. */
8844 void
8845 x_iconify_frame (f)
8846 struct frame *f;
8848 int result;
8849 Lisp_Object type;
8851 /* Don't keep the highlight on an invisible frame. */
8852 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
8853 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
8855 if (f->async_iconified)
8856 return;
8858 BLOCK_INPUT;
8860 FRAME_SAMPLE_VISIBILITY (f);
8862 type = x_icon_type (f);
8863 if (!NILP (type))
8864 x_bitmap_icon (f, type);
8866 #ifdef USE_GTK
8867 if (FRAME_GTK_OUTER_WIDGET (f))
8869 if (! FRAME_VISIBLE_P (f))
8870 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
8872 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
8873 f->iconified = 1;
8874 f->visible = 1;
8875 f->async_iconified = 1;
8876 f->async_visible = 0;
8877 UNBLOCK_INPUT;
8878 return;
8880 #endif
8882 #ifdef USE_X_TOOLKIT
8884 if (! FRAME_VISIBLE_P (f))
8886 if (! EQ (Vx_no_window_manager, Qt))
8887 x_wm_set_window_state (f, IconicState);
8888 /* This was XtPopup, but that did nothing for an iconified frame. */
8889 XtMapWidget (f->output_data.x->widget);
8890 /* The server won't give us any event to indicate
8891 that an invisible frame was changed to an icon,
8892 so we have to record it here. */
8893 f->iconified = 1;
8894 f->visible = 1;
8895 f->async_iconified = 1;
8896 f->async_visible = 0;
8897 UNBLOCK_INPUT;
8898 return;
8901 result = XIconifyWindow (FRAME_X_DISPLAY (f),
8902 XtWindow (f->output_data.x->widget),
8903 DefaultScreen (FRAME_X_DISPLAY (f)));
8904 UNBLOCK_INPUT;
8906 if (!result)
8907 error ("Can't notify window manager of iconification");
8909 f->async_iconified = 1;
8910 f->async_visible = 0;
8913 BLOCK_INPUT;
8914 XFlush (FRAME_X_DISPLAY (f));
8915 UNBLOCK_INPUT;
8916 #else /* not USE_X_TOOLKIT */
8918 /* Make sure the X server knows where the window should be positioned,
8919 in case the user deiconifies with the window manager. */
8920 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
8921 x_set_offset (f, f->left_pos, f->top_pos, 0);
8923 /* Since we don't know which revision of X we're running, we'll use both
8924 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
8926 /* X11R4: send a ClientMessage to the window manager using the
8927 WM_CHANGE_STATE type. */
8929 XEvent message;
8931 message.xclient.window = FRAME_X_WINDOW (f);
8932 message.xclient.type = ClientMessage;
8933 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
8934 message.xclient.format = 32;
8935 message.xclient.data.l[0] = IconicState;
8937 if (! XSendEvent (FRAME_X_DISPLAY (f),
8938 DefaultRootWindow (FRAME_X_DISPLAY (f)),
8939 False,
8940 SubstructureRedirectMask | SubstructureNotifyMask,
8941 &message))
8943 UNBLOCK_INPUT_RESIGNAL;
8944 error ("Can't notify window manager of iconification");
8948 /* X11R3: set the initial_state field of the window manager hints to
8949 IconicState. */
8950 x_wm_set_window_state (f, IconicState);
8952 if (!FRAME_VISIBLE_P (f))
8954 /* If the frame was withdrawn, before, we must map it. */
8955 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8958 f->async_iconified = 1;
8959 f->async_visible = 0;
8961 XFlush (FRAME_X_DISPLAY (f));
8962 UNBLOCK_INPUT;
8963 #endif /* not USE_X_TOOLKIT */
8967 /* Free X resources of frame F. */
8969 void
8970 x_free_frame_resources (f)
8971 struct frame *f;
8973 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8974 Lisp_Object bar;
8975 struct scroll_bar *b;
8977 BLOCK_INPUT;
8979 /* If a display connection is dead, don't try sending more
8980 commands to the X server. */
8981 if (dpyinfo->display)
8983 if (f->output_data.x->icon_desc)
8984 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
8986 #ifdef USE_X_TOOLKIT
8987 /* Explicitly destroy the scroll bars of the frame. Without
8988 this, we get "BadDrawable" errors from the toolkit later on,
8989 presumably from expose events generated for the disappearing
8990 toolkit scroll bars. */
8991 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
8993 b = XSCROLL_BAR (bar);
8994 x_scroll_bar_remove (b);
8996 #endif
8998 #ifdef HAVE_X_I18N
8999 if (FRAME_XIC (f))
9000 free_frame_xic (f);
9001 #endif
9003 #ifdef USE_X_TOOLKIT
9004 if (f->output_data.x->widget)
9006 XtDestroyWidget (f->output_data.x->widget);
9007 f->output_data.x->widget = NULL;
9009 /* Tooltips don't have widgets, only a simple X window, even if
9010 we are using a toolkit. */
9011 else if (FRAME_X_WINDOW (f))
9012 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9014 free_frame_menubar (f);
9015 #else /* !USE_X_TOOLKIT */
9017 #ifdef USE_GTK
9018 /* In the GTK version, tooltips are normal X
9019 frames. We must check and free both types. */
9020 if (FRAME_GTK_OUTER_WIDGET (f))
9022 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9023 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9024 FRAME_GTK_OUTER_WIDGET (f) = 0;
9026 #endif /* USE_GTK */
9028 if (FRAME_X_WINDOW (f))
9029 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9030 #endif /* !USE_X_TOOLKIT */
9032 unload_color (f, f->output_data.x->foreground_pixel);
9033 unload_color (f, f->output_data.x->background_pixel);
9034 unload_color (f, f->output_data.x->cursor_pixel);
9035 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9036 unload_color (f, f->output_data.x->border_pixel);
9037 unload_color (f, f->output_data.x->mouse_pixel);
9039 if (f->output_data.x->scroll_bar_background_pixel != -1)
9040 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9041 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9042 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9043 #ifdef USE_TOOLKIT_SCROLL_BARS
9044 /* Scrollbar shadow colors. */
9045 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9046 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9047 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9048 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9049 #endif /* USE_TOOLKIT_SCROLL_BARS */
9050 if (f->output_data.x->white_relief.allocated_p)
9051 unload_color (f, f->output_data.x->white_relief.pixel);
9052 if (f->output_data.x->black_relief.allocated_p)
9053 unload_color (f, f->output_data.x->black_relief.pixel);
9055 if (FRAME_FACE_CACHE (f))
9056 free_frame_faces (f);
9058 x_free_gcs (f);
9059 XFlush (FRAME_X_DISPLAY (f));
9062 if (f->output_data.x->saved_menu_event)
9063 xfree (f->output_data.x->saved_menu_event);
9065 xfree (f->output_data.x);
9066 f->output_data.x = NULL;
9068 if (f == dpyinfo->x_focus_frame)
9069 dpyinfo->x_focus_frame = 0;
9070 if (f == dpyinfo->x_focus_event_frame)
9071 dpyinfo->x_focus_event_frame = 0;
9072 if (f == dpyinfo->x_highlight_frame)
9073 dpyinfo->x_highlight_frame = 0;
9075 if (f == dpyinfo->mouse_face_mouse_frame)
9077 dpyinfo->mouse_face_beg_row
9078 = dpyinfo->mouse_face_beg_col = -1;
9079 dpyinfo->mouse_face_end_row
9080 = dpyinfo->mouse_face_end_col = -1;
9081 dpyinfo->mouse_face_window = Qnil;
9082 dpyinfo->mouse_face_deferred_gc = 0;
9083 dpyinfo->mouse_face_mouse_frame = 0;
9086 UNBLOCK_INPUT;
9090 /* Destroy the X window of frame F. */
9092 void
9093 x_destroy_window (f)
9094 struct frame *f;
9096 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9098 /* If a display connection is dead, don't try sending more
9099 commands to the X server. */
9100 if (dpyinfo->display != 0)
9101 x_free_frame_resources (f);
9103 dpyinfo->reference_count--;
9107 /* Setting window manager hints. */
9109 /* Set the normal size hints for the window manager, for frame F.
9110 FLAGS is the flags word to use--or 0 meaning preserve the flags
9111 that the window now has.
9112 If USER_POSITION is nonzero, we set the USPosition
9113 flag (this is useful when FLAGS is 0).
9114 The GTK version is in gtkutils.c */
9116 #ifndef USE_GTK
9117 void
9118 x_wm_set_size_hint (f, flags, user_position)
9119 struct frame *f;
9120 long flags;
9121 int user_position;
9123 XSizeHints size_hints;
9125 #ifdef USE_X_TOOLKIT
9126 Arg al[2];
9127 int ac = 0;
9128 Dimension widget_width, widget_height;
9129 #endif
9131 Window window = FRAME_OUTER_WINDOW (f);
9133 /* Setting PMaxSize caused various problems. */
9134 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9136 size_hints.x = f->left_pos;
9137 size_hints.y = f->top_pos;
9139 #ifdef USE_X_TOOLKIT
9140 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
9141 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
9142 XtGetValues (f->output_data.x->widget, al, ac);
9143 size_hints.height = widget_height;
9144 size_hints.width = widget_width;
9145 #else /* not USE_X_TOOLKIT */
9146 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9147 size_hints.width = FRAME_PIXEL_WIDTH (f);
9148 #endif /* not USE_X_TOOLKIT */
9150 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9151 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9152 size_hints.max_width
9153 = FRAME_X_DISPLAY_INFO (f)->width - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9154 size_hints.max_height
9155 = FRAME_X_DISPLAY_INFO (f)->height - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9157 /* Calculate the base and minimum sizes.
9159 (When we use the X toolkit, we don't do it here.
9160 Instead we copy the values that the widgets are using, below.) */
9161 #ifndef USE_X_TOOLKIT
9163 int base_width, base_height;
9164 int min_rows = 0, min_cols = 0;
9166 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9167 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9169 check_frame_size (f, &min_rows, &min_cols);
9171 /* The window manager uses the base width hints to calculate the
9172 current number of rows and columns in the frame while
9173 resizing; min_width and min_height aren't useful for this
9174 purpose, since they might not give the dimensions for a
9175 zero-row, zero-column frame.
9177 We use the base_width and base_height members if we have
9178 them; otherwise, we set the min_width and min_height members
9179 to the size for a zero x zero frame. */
9181 #ifdef HAVE_X11R4
9182 size_hints.flags |= PBaseSize;
9183 size_hints.base_width = base_width;
9184 size_hints.base_height = base_height;
9185 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9186 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9187 #else
9188 size_hints.min_width = base_width;
9189 size_hints.min_height = base_height;
9190 #endif
9193 /* If we don't need the old flags, we don't need the old hint at all. */
9194 if (flags)
9196 size_hints.flags |= flags;
9197 goto no_read;
9199 #endif /* not USE_X_TOOLKIT */
9202 XSizeHints hints; /* Sometimes I hate X Windows... */
9203 long supplied_return;
9204 int value;
9206 #ifdef HAVE_X11R4
9207 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9208 &supplied_return);
9209 #else
9210 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
9211 #endif
9213 #ifdef USE_X_TOOLKIT
9214 size_hints.base_height = hints.base_height;
9215 size_hints.base_width = hints.base_width;
9216 size_hints.min_height = hints.min_height;
9217 size_hints.min_width = hints.min_width;
9218 #endif
9220 if (flags)
9221 size_hints.flags |= flags;
9222 else
9224 if (value == 0)
9225 hints.flags = 0;
9226 if (hints.flags & PSize)
9227 size_hints.flags |= PSize;
9228 if (hints.flags & PPosition)
9229 size_hints.flags |= PPosition;
9230 if (hints.flags & USPosition)
9231 size_hints.flags |= USPosition;
9232 if (hints.flags & USSize)
9233 size_hints.flags |= USSize;
9237 #ifndef USE_X_TOOLKIT
9238 no_read:
9239 #endif
9241 #ifdef PWinGravity
9242 size_hints.win_gravity = f->win_gravity;
9243 size_hints.flags |= PWinGravity;
9245 if (user_position)
9247 size_hints.flags &= ~ PPosition;
9248 size_hints.flags |= USPosition;
9250 #endif /* PWinGravity */
9252 #ifdef HAVE_X11R4
9253 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9254 #else
9255 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9256 #endif
9258 #endif /* not USE_GTK */
9260 /* Used for IconicState or NormalState */
9262 void
9263 x_wm_set_window_state (f, state)
9264 struct frame *f;
9265 int state;
9267 #ifdef USE_X_TOOLKIT
9268 Arg al[1];
9270 XtSetArg (al[0], XtNinitialState, state);
9271 XtSetValues (f->output_data.x->widget, al, 1);
9272 #else /* not USE_X_TOOLKIT */
9273 Window window = FRAME_X_WINDOW (f);
9275 f->output_data.x->wm_hints.flags |= StateHint;
9276 f->output_data.x->wm_hints.initial_state = state;
9278 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9279 #endif /* not USE_X_TOOLKIT */
9282 void
9283 x_wm_set_icon_pixmap (f, pixmap_id)
9284 struct frame *f;
9285 int pixmap_id;
9287 Pixmap icon_pixmap, icon_mask;
9289 #ifndef USE_X_TOOLKIT
9290 Window window = FRAME_OUTER_WINDOW (f);
9291 #endif
9293 if (pixmap_id > 0)
9295 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9296 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9297 icon_mask = x_bitmap_mask (f, pixmap_id);
9298 f->output_data.x->wm_hints.icon_mask = icon_mask;
9300 else
9302 /* It seems there is no way to turn off use of an icon pixmap.
9303 The following line does it, only if no icon has yet been created,
9304 for some window managers. But with mwm it crashes.
9305 Some people say it should clear the IconPixmapHint bit in this case,
9306 but that doesn't work, and the X consortium said it isn't the
9307 right thing at all. Since there is no way to win,
9308 best to explicitly give up. */
9309 #if 0
9310 f->output_data.x->wm_hints.icon_pixmap = None;
9311 f->output_data.x->wm_hints.icon_mask = None;
9312 #else
9313 return;
9314 #endif
9318 #ifdef USE_GTK
9320 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9321 return;
9324 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
9327 Arg al[1];
9328 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9329 XtSetValues (f->output_data.x->widget, al, 1);
9330 XtSetArg (al[0], XtNiconMask, icon_mask);
9331 XtSetValues (f->output_data.x->widget, al, 1);
9334 #else /* not USE_X_TOOLKIT && not USE_GTK */
9336 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9337 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9339 #endif /* not USE_X_TOOLKIT && not USE_GTK */
9342 void
9343 x_wm_set_icon_position (f, icon_x, icon_y)
9344 struct frame *f;
9345 int icon_x, icon_y;
9347 Window window = FRAME_OUTER_WINDOW (f);
9349 f->output_data.x->wm_hints.flags |= IconPositionHint;
9350 f->output_data.x->wm_hints.icon_x = icon_x;
9351 f->output_data.x->wm_hints.icon_y = icon_y;
9353 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9357 /***********************************************************************
9358 Fonts
9359 ***********************************************************************/
9361 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
9363 struct font_info *
9364 x_get_font_info (f, font_idx)
9365 FRAME_PTR f;
9366 int font_idx;
9368 return (FRAME_X_FONT_TABLE (f) + font_idx);
9372 /* Return a list of names of available fonts matching PATTERN on frame F.
9374 If SIZE is > 0, it is the size (maximum bounds width) of fonts
9375 to be listed.
9377 SIZE < 0 means include scalable fonts.
9379 Frame F null means we have not yet created any frame on X, and
9380 consult the first display in x_display_list. MAXNAMES sets a limit
9381 on how many fonts to match. */
9383 Lisp_Object
9384 x_list_fonts (f, pattern, size, maxnames)
9385 struct frame *f;
9386 Lisp_Object pattern;
9387 int size;
9388 int maxnames;
9390 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
9391 Lisp_Object tem, second_best;
9392 struct x_display_info *dpyinfo
9393 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
9394 Display *dpy = dpyinfo->display;
9395 int try_XLoadQueryFont = 0;
9396 int count;
9397 int allow_auto_scaled_font = 0;
9399 if (size < 0)
9401 allow_auto_scaled_font = 1;
9402 size = 0;
9405 patterns = Fassoc (pattern, Valternate_fontname_alist);
9406 if (NILP (patterns))
9407 patterns = Fcons (pattern, Qnil);
9409 if (maxnames == 1 && !size)
9410 /* We can return any single font matching PATTERN. */
9411 try_XLoadQueryFont = 1;
9413 for (; CONSP (patterns); patterns = XCDR (patterns))
9415 int num_fonts;
9416 char **names = NULL;
9418 pattern = XCAR (patterns);
9419 /* See if we cached the result for this particular query.
9420 The cache is an alist of the form:
9421 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
9422 tem = XCDR (dpyinfo->name_list_element);
9423 key = Fcons (Fcons (pattern, make_number (maxnames)),
9424 allow_auto_scaled_font ? Qt : Qnil);
9425 list = Fassoc (key, tem);
9426 if (!NILP (list))
9428 list = Fcdr_safe (list);
9429 /* We have a cashed list. Don't have to get the list again. */
9430 goto label_cached;
9433 /* At first, put PATTERN in the cache. */
9435 BLOCK_INPUT;
9436 count = x_catch_errors (dpy);
9438 if (try_XLoadQueryFont)
9440 XFontStruct *font;
9441 unsigned long value;
9443 font = XLoadQueryFont (dpy, SDATA (pattern));
9444 if (x_had_errors_p (dpy))
9446 /* This error is perhaps due to insufficient memory on X
9447 server. Let's just ignore it. */
9448 font = NULL;
9449 x_clear_errors (dpy);
9452 if (font
9453 && XGetFontProperty (font, XA_FONT, &value))
9455 char *name = (char *) XGetAtomName (dpy, (Atom) value);
9456 int len = strlen (name);
9457 char *tmp;
9459 /* If DXPC (a Differential X Protocol Compressor)
9460 Ver.3.7 is running, XGetAtomName will return null
9461 string. We must avoid such a name. */
9462 if (len == 0)
9463 try_XLoadQueryFont = 0;
9464 else
9466 num_fonts = 1;
9467 names = (char **) alloca (sizeof (char *));
9468 /* Some systems only allow alloca assigned to a
9469 simple var. */
9470 tmp = (char *) alloca (len + 1); names[0] = tmp;
9471 bcopy (name, names[0], len + 1);
9472 XFree (name);
9475 else
9476 try_XLoadQueryFont = 0;
9478 if (font)
9479 XFreeFont (dpy, font);
9482 if (!try_XLoadQueryFont)
9484 /* We try at least 10 fonts because XListFonts will return
9485 auto-scaled fonts at the head. */
9486 if (maxnames < 0)
9488 int limit;
9490 for (limit = 500;;)
9492 names = XListFonts (dpy, SDATA (pattern), limit, &num_fonts);
9493 if (num_fonts == limit)
9495 BLOCK_INPUT;
9496 XFreeFontNames (names);
9497 UNBLOCK_INPUT;
9498 limit *= 2;
9500 else
9501 break;
9504 else
9505 names = XListFonts (dpy, SDATA (pattern), max (maxnames, 10),
9506 &num_fonts);
9508 if (x_had_errors_p (dpy))
9510 /* This error is perhaps due to insufficient memory on X
9511 server. Let's just ignore it. */
9512 names = NULL;
9513 x_clear_errors (dpy);
9517 x_uncatch_errors (dpy, count);
9518 UNBLOCK_INPUT;
9520 if (names)
9522 int i;
9524 /* Make a list of all the fonts we got back.
9525 Store that in the font cache for the display. */
9526 for (i = 0; i < num_fonts; i++)
9528 int width = 0;
9529 char *p = names[i];
9530 int average_width = -1, resx = 0, dashes = 0;
9532 /* Count the number of dashes in NAMES[I]. If there are
9533 14 dashes, the field value following 9th dash
9534 (RESOLUTION_X) is nonzero, and the field value
9535 following 12th dash (AVERAGE_WIDTH) is 0, this is a
9536 auto-scaled font which is usually too ugly to be used
9537 for editing. Let's ignore it. */
9538 while (*p)
9539 if (*p++ == '-')
9541 dashes++;
9542 if (dashes == 7) /* PIXEL_SIZE field */
9543 width = atoi (p);
9544 else if (dashes == 9)
9545 resx = atoi (p);
9546 else if (dashes == 12) /* AVERAGE_WIDTH field */
9547 average_width = atoi (p);
9550 if (allow_auto_scaled_font
9551 || dashes < 14 || average_width != 0 || resx == 0)
9553 tem = build_string (names[i]);
9554 if (NILP (Fassoc (tem, list)))
9556 if (STRINGP (Vx_pixel_size_width_font_regexp)
9557 && ((fast_c_string_match_ignore_case
9558 (Vx_pixel_size_width_font_regexp, names[i]))
9559 >= 0))
9560 /* We can set the value of PIXEL_SIZE to the
9561 width of this font. */
9562 list = Fcons (Fcons (tem, make_number (width)), list);
9563 else
9564 /* For the moment, width is not known. */
9565 list = Fcons (Fcons (tem, Qnil), list);
9570 if (!try_XLoadQueryFont)
9572 BLOCK_INPUT;
9573 XFreeFontNames (names);
9574 UNBLOCK_INPUT;
9578 /* Now store the result in the cache. */
9579 XSETCDR (dpyinfo->name_list_element,
9580 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
9582 label_cached:
9583 if (NILP (list)) continue; /* Try the remaining alternatives. */
9585 newlist = second_best = Qnil;
9586 /* Make a list of the fonts that have the right width. */
9587 for (; CONSP (list); list = XCDR (list))
9589 int found_size;
9591 tem = XCAR (list);
9593 if (!CONSP (tem) || NILP (XCAR (tem)))
9594 continue;
9595 if (!size)
9597 newlist = Fcons (XCAR (tem), newlist);
9598 continue;
9601 if (!INTEGERP (XCDR (tem)))
9603 /* Since we have not yet known the size of this font, we
9604 must try slow function call XLoadQueryFont. */
9605 XFontStruct *thisinfo;
9607 BLOCK_INPUT;
9608 count = x_catch_errors (dpy);
9609 thisinfo = XLoadQueryFont (dpy,
9610 SDATA (XCAR (tem)));
9611 if (x_had_errors_p (dpy))
9613 /* This error is perhaps due to insufficient memory on X
9614 server. Let's just ignore it. */
9615 thisinfo = NULL;
9616 x_clear_errors (dpy);
9618 x_uncatch_errors (dpy, count);
9619 UNBLOCK_INPUT;
9621 if (thisinfo)
9623 XSETCDR (tem,
9624 (thisinfo->min_bounds.width == 0
9625 ? make_number (0)
9626 : make_number (thisinfo->max_bounds.width)));
9627 BLOCK_INPUT;
9628 XFreeFont (dpy, thisinfo);
9629 UNBLOCK_INPUT;
9631 else
9632 /* For unknown reason, the previous call of XListFont had
9633 returned a font which can't be opened. Record the size
9634 as 0 not to try to open it again. */
9635 XSETCDR (tem, make_number (0));
9638 found_size = XINT (XCDR (tem));
9639 if (found_size == size)
9640 newlist = Fcons (XCAR (tem), newlist);
9641 else if (found_size > 0)
9643 if (NILP (second_best))
9644 second_best = tem;
9645 else if (found_size < size)
9647 if (XINT (XCDR (second_best)) > size
9648 || XINT (XCDR (second_best)) < found_size)
9649 second_best = tem;
9651 else
9653 if (XINT (XCDR (second_best)) > size
9654 && XINT (XCDR (second_best)) > found_size)
9655 second_best = tem;
9659 if (!NILP (newlist))
9660 break;
9661 else if (!NILP (second_best))
9663 newlist = Fcons (XCAR (second_best), Qnil);
9664 break;
9668 return newlist;
9672 #if GLYPH_DEBUG
9674 /* Check that FONT is valid on frame F. It is if it can be found in F's
9675 font table. */
9677 static void
9678 x_check_font (f, font)
9679 struct frame *f;
9680 XFontStruct *font;
9682 int i;
9683 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9685 xassert (font != NULL);
9687 for (i = 0; i < dpyinfo->n_fonts; i++)
9688 if (dpyinfo->font_table[i].name
9689 && font == dpyinfo->font_table[i].font)
9690 break;
9692 xassert (i < dpyinfo->n_fonts);
9695 #endif /* GLYPH_DEBUG != 0 */
9697 /* Set *W to the minimum width, *H to the minimum font height of FONT.
9698 Note: There are (broken) X fonts out there with invalid XFontStruct
9699 min_bounds contents. For example, handa@etl.go.jp reports that
9700 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
9701 have font->min_bounds.width == 0. */
9703 static INLINE void
9704 x_font_min_bounds (font, w, h)
9705 XFontStruct *font;
9706 int *w, *h;
9708 *h = FONT_HEIGHT (font);
9709 *w = font->min_bounds.width;
9711 /* Try to handle the case where FONT->min_bounds has invalid
9712 contents. Since the only font known to have invalid min_bounds
9713 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
9714 if (*w <= 0)
9715 *w = font->max_bounds.width;
9719 /* Compute the smallest character width and smallest font height over
9720 all fonts available on frame F. Set the members smallest_char_width
9721 and smallest_font_height in F's x_display_info structure to
9722 the values computed. Value is non-zero if smallest_font_height or
9723 smallest_char_width become smaller than they were before. */
9725 static int
9726 x_compute_min_glyph_bounds (f)
9727 struct frame *f;
9729 int i;
9730 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9731 XFontStruct *font;
9732 int old_width = dpyinfo->smallest_char_width;
9733 int old_height = dpyinfo->smallest_font_height;
9735 dpyinfo->smallest_font_height = 100000;
9736 dpyinfo->smallest_char_width = 100000;
9738 for (i = 0; i < dpyinfo->n_fonts; ++i)
9739 if (dpyinfo->font_table[i].name)
9741 struct font_info *fontp = dpyinfo->font_table + i;
9742 int w, h;
9744 font = (XFontStruct *) fontp->font;
9745 xassert (font != (XFontStruct *) ~0);
9746 x_font_min_bounds (font, &w, &h);
9748 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
9749 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
9752 xassert (dpyinfo->smallest_char_width > 0
9753 && dpyinfo->smallest_font_height > 0);
9755 return (dpyinfo->n_fonts == 1
9756 || dpyinfo->smallest_char_width < old_width
9757 || dpyinfo->smallest_font_height < old_height);
9761 /* Load font named FONTNAME of the size SIZE for frame F, and return a
9762 pointer to the structure font_info while allocating it dynamically.
9763 If SIZE is 0, load any size of font.
9764 If loading is failed, return NULL. */
9766 struct font_info *
9767 x_load_font (f, fontname, size)
9768 struct frame *f;
9769 register char *fontname;
9770 int size;
9772 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9773 Lisp_Object font_names;
9774 int count;
9776 /* Get a list of all the fonts that match this name. Once we
9777 have a list of matching fonts, we compare them against the fonts
9778 we already have by comparing names. */
9779 font_names = x_list_fonts (f, build_string (fontname), size, 1);
9781 if (!NILP (font_names))
9783 Lisp_Object tail;
9784 int i;
9786 for (i = 0; i < dpyinfo->n_fonts; i++)
9787 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
9788 if (dpyinfo->font_table[i].name
9789 && (!strcmp (dpyinfo->font_table[i].name,
9790 SDATA (XCAR (tail)))
9791 || !strcmp (dpyinfo->font_table[i].full_name,
9792 SDATA (XCAR (tail)))))
9793 return (dpyinfo->font_table + i);
9796 /* Load the font and add it to the table. */
9798 char *full_name;
9799 XFontStruct *font;
9800 struct font_info *fontp;
9801 unsigned long value;
9802 int i;
9804 /* If we have found fonts by x_list_font, load one of them. If
9805 not, we still try to load a font by the name given as FONTNAME
9806 because XListFonts (called in x_list_font) of some X server has
9807 a bug of not finding a font even if the font surely exists and
9808 is loadable by XLoadQueryFont. */
9809 if (size > 0 && !NILP (font_names))
9810 fontname = (char *) SDATA (XCAR (font_names));
9812 BLOCK_INPUT;
9813 count = x_catch_errors (FRAME_X_DISPLAY (f));
9814 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
9815 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
9817 /* This error is perhaps due to insufficient memory on X
9818 server. Let's just ignore it. */
9819 font = NULL;
9820 x_clear_errors (FRAME_X_DISPLAY (f));
9822 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
9823 UNBLOCK_INPUT;
9824 if (!font)
9825 return NULL;
9827 /* Find a free slot in the font table. */
9828 for (i = 0; i < dpyinfo->n_fonts; ++i)
9829 if (dpyinfo->font_table[i].name == NULL)
9830 break;
9832 /* If no free slot found, maybe enlarge the font table. */
9833 if (i == dpyinfo->n_fonts
9834 && dpyinfo->n_fonts == dpyinfo->font_table_size)
9836 int sz;
9837 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
9838 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
9839 dpyinfo->font_table
9840 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
9843 fontp = dpyinfo->font_table + i;
9844 if (i == dpyinfo->n_fonts)
9845 ++dpyinfo->n_fonts;
9847 /* Now fill in the slots of *FONTP. */
9848 BLOCK_INPUT;
9849 bzero (fontp, sizeof (*fontp));
9850 fontp->font = font;
9851 fontp->font_idx = i;
9852 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
9853 bcopy (fontname, fontp->name, strlen (fontname) + 1);
9855 if (font->min_bounds.width == font->max_bounds.width)
9857 /* Fixed width font. */
9858 fontp->average_width = fontp->space_width = font->min_bounds.width;
9860 else
9862 XChar2b char2b;
9863 XCharStruct *pcm;
9865 char2b.byte1 = 0x00, char2b.byte2 = 0x20;
9866 pcm = x_per_char_metric (font, &char2b, 0);
9867 if (pcm)
9868 fontp->space_width = pcm->width;
9869 else
9870 fontp->space_width = FONT_WIDTH (font);
9872 fontp->average_width
9873 = (XGetFontProperty (font, dpyinfo->Xatom_AVERAGE_WIDTH, &value)
9874 ? (long) value / 10 : 0);
9875 if (fontp->average_width < 0)
9876 fontp->average_width = - fontp->average_width;
9877 if (fontp->average_width == 0)
9879 if (pcm)
9881 int width = pcm->width;
9882 for (char2b.byte2 = 33; char2b.byte2 <= 126; char2b.byte2++)
9883 if ((pcm = x_per_char_metric (font, &char2b, 0)) != NULL)
9884 width += pcm->width;
9885 fontp->average_width = width / 95;
9887 else
9888 fontp->average_width = FONT_WIDTH (font);
9892 /* Try to get the full name of FONT. Put it in FULL_NAME. */
9893 full_name = 0;
9894 if (XGetFontProperty (font, XA_FONT, &value))
9896 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
9897 char *p = name;
9898 int dashes = 0;
9900 /* Count the number of dashes in the "full name".
9901 If it is too few, this isn't really the font's full name,
9902 so don't use it.
9903 In X11R4, the fonts did not come with their canonical names
9904 stored in them. */
9905 while (*p)
9907 if (*p == '-')
9908 dashes++;
9909 p++;
9912 if (dashes >= 13)
9914 full_name = (char *) xmalloc (p - name + 1);
9915 bcopy (name, full_name, p - name + 1);
9918 XFree (name);
9921 if (full_name != 0)
9922 fontp->full_name = full_name;
9923 else
9924 fontp->full_name = fontp->name;
9926 fontp->size = font->max_bounds.width;
9927 fontp->height = FONT_HEIGHT (font);
9929 if (NILP (font_names))
9931 /* We come here because of a bug of XListFonts mentioned at
9932 the head of this block. Let's store this information in
9933 the cache for x_list_fonts. */
9934 Lisp_Object lispy_name = build_string (fontname);
9935 Lisp_Object lispy_full_name = build_string (fontp->full_name);
9936 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
9937 Qnil);
9939 XSETCDR (dpyinfo->name_list_element,
9940 Fcons (Fcons (key,
9941 Fcons (Fcons (lispy_full_name,
9942 make_number (fontp->size)),
9943 Qnil)),
9944 XCDR (dpyinfo->name_list_element)));
9945 if (full_name)
9947 key = Fcons (Fcons (lispy_full_name, make_number (256)),
9948 Qnil);
9949 XSETCDR (dpyinfo->name_list_element,
9950 Fcons (Fcons (key,
9951 Fcons (Fcons (lispy_full_name,
9952 make_number (fontp->size)),
9953 Qnil)),
9954 XCDR (dpyinfo->name_list_element)));
9958 /* The slot `encoding' specifies how to map a character
9959 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
9960 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
9961 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
9962 2:0xA020..0xFF7F). For the moment, we don't know which charset
9963 uses this font. So, we set information in fontp->encoding[1]
9964 which is never used by any charset. If mapping can't be
9965 decided, set FONT_ENCODING_NOT_DECIDED. */
9966 fontp->encoding[1]
9967 = (font->max_byte1 == 0
9968 /* 1-byte font */
9969 ? (font->min_char_or_byte2 < 0x80
9970 ? (font->max_char_or_byte2 < 0x80
9971 ? 0 /* 0x20..0x7F */
9972 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
9973 : 1) /* 0xA0..0xFF */
9974 /* 2-byte font */
9975 : (font->min_byte1 < 0x80
9976 ? (font->max_byte1 < 0x80
9977 ? (font->min_char_or_byte2 < 0x80
9978 ? (font->max_char_or_byte2 < 0x80
9979 ? 0 /* 0x2020..0x7F7F */
9980 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
9981 : 3) /* 0x20A0..0x7FFF */
9982 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
9983 : (font->min_char_or_byte2 < 0x80
9984 ? (font->max_char_or_byte2 < 0x80
9985 ? 2 /* 0xA020..0xFF7F */
9986 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
9987 : 1))); /* 0xA0A0..0xFFFF */
9989 fontp->baseline_offset
9990 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
9991 ? (long) value : 0);
9992 fontp->relative_compose
9993 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
9994 ? (long) value : 0);
9995 fontp->default_ascent
9996 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
9997 ? (long) value : 0);
9999 /* Set global flag fonts_changed_p to non-zero if the font loaded
10000 has a character with a smaller width than any other character
10001 before, or if the font loaded has a smaller height than any
10002 other font loaded before. If this happens, it will make a
10003 glyph matrix reallocation necessary. */
10004 fonts_changed_p |= x_compute_min_glyph_bounds (f);
10005 UNBLOCK_INPUT;
10006 return fontp;
10011 /* Return a pointer to struct font_info of a font named FONTNAME for
10012 frame F. If no such font is loaded, return NULL. */
10014 struct font_info *
10015 x_query_font (f, fontname)
10016 struct frame *f;
10017 register char *fontname;
10019 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10020 int i;
10022 for (i = 0; i < dpyinfo->n_fonts; i++)
10023 if (dpyinfo->font_table[i].name
10024 && (!strcmp (dpyinfo->font_table[i].name, fontname)
10025 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
10026 return (dpyinfo->font_table + i);
10027 return NULL;
10031 /* Find a CCL program for a font specified by FONTP, and set the member
10032 `encoder' of the structure. */
10034 void
10035 x_find_ccl_program (fontp)
10036 struct font_info *fontp;
10038 Lisp_Object list, elt;
10040 elt = Qnil;
10041 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
10043 elt = XCAR (list);
10044 if (CONSP (elt)
10045 && STRINGP (XCAR (elt))
10046 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
10047 >= 0)
10048 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
10049 >= 0)))
10050 break;
10053 if (! NILP (list))
10055 struct ccl_program *ccl
10056 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
10058 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
10059 xfree (ccl);
10060 else
10061 fontp->font_encoder = ccl;
10067 /***********************************************************************
10068 Initialization
10069 ***********************************************************************/
10071 #ifdef USE_X_TOOLKIT
10072 static XrmOptionDescRec emacs_options[] = {
10073 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
10074 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
10076 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
10077 XrmoptionSepArg, NULL},
10078 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
10080 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10081 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10082 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10083 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10084 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10085 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
10086 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
10088 #endif /* USE_X_TOOLKIT */
10090 static int x_initialized;
10092 #ifdef MULTI_KBOARD
10093 /* Test whether two display-name strings agree up to the dot that separates
10094 the screen number from the server number. */
10095 static int
10096 same_x_server (name1, name2)
10097 const char *name1, *name2;
10099 int seen_colon = 0;
10100 const unsigned char *system_name = SDATA (Vsystem_name);
10101 int system_name_length = strlen (system_name);
10102 int length_until_period = 0;
10104 while (system_name[length_until_period] != 0
10105 && system_name[length_until_period] != '.')
10106 length_until_period++;
10108 /* Treat `unix' like an empty host name. */
10109 if (! strncmp (name1, "unix:", 5))
10110 name1 += 4;
10111 if (! strncmp (name2, "unix:", 5))
10112 name2 += 4;
10113 /* Treat this host's name like an empty host name. */
10114 if (! strncmp (name1, system_name, system_name_length)
10115 && name1[system_name_length] == ':')
10116 name1 += system_name_length;
10117 if (! strncmp (name2, system_name, system_name_length)
10118 && name2[system_name_length] == ':')
10119 name2 += system_name_length;
10120 /* Treat this host's domainless name like an empty host name. */
10121 if (! strncmp (name1, system_name, length_until_period)
10122 && name1[length_until_period] == ':')
10123 name1 += length_until_period;
10124 if (! strncmp (name2, system_name, length_until_period)
10125 && name2[length_until_period] == ':')
10126 name2 += length_until_period;
10128 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
10130 if (*name1 == ':')
10131 seen_colon++;
10132 if (seen_colon && *name1 == '.')
10133 return 1;
10135 return (seen_colon
10136 && (*name1 == '.' || *name1 == '\0')
10137 && (*name2 == '.' || *name2 == '\0'));
10139 #endif
10141 /* Count number of set bits in mask and number of bits to shift to
10142 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
10143 to 5. */
10144 static void
10145 get_bits_and_offset (mask, bits, offset)
10146 unsigned long mask;
10147 int *bits;
10148 int *offset;
10150 int nr = 0;
10151 int off = 0;
10153 while (!(mask & 1))
10155 off++;
10156 mask >>= 1;
10159 while (mask & 1)
10161 nr++;
10162 mask >>= 1;
10165 *offset = off;
10166 *bits = nr;
10169 struct x_display_info *
10170 x_term_init (display_name, xrm_option, resource_name)
10171 Lisp_Object display_name;
10172 char *xrm_option;
10173 char *resource_name;
10175 int connection;
10176 Display *dpy;
10177 struct terminal *terminal;
10178 struct x_display_info *dpyinfo;
10179 XrmDatabase xrdb;
10181 BLOCK_INPUT;
10183 if (!x_initialized)
10185 x_initialize ();
10186 ++x_initialized;
10189 #ifdef USE_GTK
10191 #define NUM_ARGV 10
10192 int argc;
10193 char *argv[NUM_ARGV];
10194 char **argv2 = argv;
10195 GdkAtom atom;
10197 #ifndef HAVE_GTK_MULTIDISPLAY
10198 if (!EQ (Vinitial_window_system, intern ("x")))
10199 error ("Sorry, you cannot connect to X servers with the GTK toolkit");
10200 #endif
10202 if (x_initialized++ > 1)
10204 #ifdef HAVE_GTK_MULTIDISPLAY
10205 /* Opening another display. If xg_display_open returns less
10206 than zero, we are probably on GTK 2.0, which can only handle
10207 one display. GTK 2.2 or later can handle more than one. */
10208 if (xg_display_open (SDATA (display_name), &dpy) < 0)
10209 error ("Sorry, this version of GTK can only handle one display");
10210 #else
10211 /* XXX Unfortunately, multiple display support is severely broken
10212 in recent GTK versions, so HAVE_GTK_MULTIDISPLAY is
10213 unconditionally disabled in configure.in. */
10214 error ("Sorry, multiple display support is broken in current GTK versions");
10215 #endif
10217 else
10219 for (argc = 0; argc < NUM_ARGV; ++argc)
10220 argv[argc] = 0;
10222 argc = 0;
10223 argv[argc++] = initial_argv[0];
10225 if (! NILP (display_name))
10227 argv[argc++] = "--display";
10228 argv[argc++] = SDATA (display_name);
10231 argv[argc++] = "--name";
10232 argv[argc++] = resource_name;
10234 #ifdef HAVE_X11R5
10235 XSetLocaleModifiers ("");
10236 #endif
10238 gtk_init (&argc, &argv2);
10240 /* gtk_init does set_locale. We must fix locale after calling it. */
10241 fixup_locale ();
10242 xg_initialize ();
10244 dpy = GDK_DISPLAY ();
10246 /* NULL window -> events for all windows go to our function */
10247 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10249 /* Load our own gtkrc if it exists. */
10251 struct gcpro gcpro1, gcpro2;
10252 char *file = "~/.emacs.d/gtkrc";
10253 Lisp_Object s, abs_file;
10255 GCPRO2 (s, abs_file);
10256 s = make_string (file, strlen (file));
10257 abs_file = Fexpand_file_name (s, Qnil);
10259 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10260 gtk_rc_parse (SDATA (abs_file));
10262 UNGCPRO;
10265 XSetErrorHandler (x_error_handler);
10266 XSetIOErrorHandler (x_io_error_quitter);
10269 #else /* not USE_GTK */
10270 #ifdef USE_X_TOOLKIT
10271 /* weiner@footloose.sps.mot.com reports that this causes
10272 errors with X11R5:
10273 X protocol error: BadAtom (invalid Atom parameter)
10274 on protocol request 18skiloaf.
10275 So let's not use it until R6. */
10276 #ifdef HAVE_X11XTR6
10277 XtSetLanguageProc (NULL, NULL, NULL);
10278 #endif
10281 int argc = 0;
10282 char *argv[3];
10284 argv[0] = "";
10285 argc = 1;
10286 if (xrm_option)
10288 argv[argc++] = "-xrm";
10289 argv[argc++] = xrm_option;
10291 turn_on_atimers (0);
10292 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10293 resource_name, EMACS_CLASS,
10294 emacs_options, XtNumber (emacs_options),
10295 &argc, argv);
10296 turn_on_atimers (1);
10298 #ifdef HAVE_X11XTR6
10299 /* I think this is to compensate for XtSetLanguageProc. */
10300 fixup_locale ();
10301 #endif
10304 #else /* not USE_X_TOOLKIT */
10305 #ifdef HAVE_X11R5
10306 XSetLocaleModifiers ("");
10307 #endif
10308 dpy = XOpenDisplay (SDATA (display_name));
10309 #endif /* not USE_X_TOOLKIT */
10310 #endif /* not USE_GTK*/
10312 /* Detect failure. */
10313 if (dpy == 0)
10315 UNBLOCK_INPUT;
10316 return 0;
10319 /* We have definitely succeeded. Record the new connection. */
10321 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10322 bzero (dpyinfo, sizeof *dpyinfo);
10324 terminal = x_create_terminal (dpyinfo);
10326 #ifdef MULTI_KBOARD
10328 struct x_display_info *share;
10329 Lisp_Object tail;
10331 for (share = x_display_list, tail = x_display_name_list; share;
10332 share = share->next, tail = XCDR (tail))
10333 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10334 SDATA (display_name)))
10335 break;
10336 if (share)
10337 terminal->kboard = share->terminal->kboard;
10338 else
10340 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10341 init_kboard (terminal->kboard);
10342 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10344 char *vendor = ServerVendor (dpy);
10345 UNBLOCK_INPUT;
10346 terminal->kboard->Vsystem_key_alist
10347 = call1 (Qvendor_specific_keysyms,
10348 build_string (vendor ? vendor : ""));
10349 BLOCK_INPUT;
10352 terminal->kboard->next_kboard = all_kboards;
10353 all_kboards = terminal->kboard;
10354 /* Don't let the initial kboard remain current longer than necessary.
10355 That would cause problems if a file loaded on startup tries to
10356 prompt in the mini-buffer. */
10357 if (current_kboard == initial_kboard)
10358 current_kboard = terminal->kboard;
10360 terminal->kboard->reference_count++;
10362 #endif
10364 /* Put this display on the chain. */
10365 dpyinfo->next = x_display_list;
10366 x_display_list = dpyinfo;
10368 /* Put it on x_display_name_list as well, to keep them parallel. */
10369 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10370 x_display_name_list);
10371 dpyinfo->name_list_element = XCAR (x_display_name_list);
10373 dpyinfo->display = dpy;
10375 /* Set the name of the terminal. */
10376 terminal->name = (char *) xmalloc (SBYTES (display_name) + 1);
10377 strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
10378 terminal->name[SBYTES (display_name)] = 0;
10380 #if 0
10381 XSetAfterFunction (x_current_display, x_trace_wire);
10382 #endif /* ! 0 */
10384 dpyinfo->x_id_name
10385 = (char *) xmalloc (SBYTES (Vinvocation_name)
10386 + SBYTES (Vsystem_name)
10387 + 2);
10388 sprintf (dpyinfo->x_id_name, "%s@%s",
10389 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10391 /* Figure out which modifier bits mean what. */
10392 x_find_modifier_meanings (dpyinfo);
10394 /* Get the scroll bar cursor. */
10395 #ifdef USE_GTK
10396 /* We must create a GTK cursor, it is required for GTK widgets. */
10397 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10398 #endif /* USE_GTK */
10400 dpyinfo->vertical_scroll_bar_cursor
10401 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10403 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10404 resource_name, EMACS_CLASS);
10405 #ifdef HAVE_XRMSETDATABASE
10406 XrmSetDatabase (dpyinfo->display, xrdb);
10407 #else
10408 dpyinfo->display->db = xrdb;
10409 #endif
10410 /* Put the rdb where we can find it in a way that works on
10411 all versions. */
10412 dpyinfo->xrdb = xrdb;
10414 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10415 DefaultScreen (dpyinfo->display));
10416 select_visual (dpyinfo);
10417 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10418 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
10419 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
10420 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10421 dpyinfo->client_leader_window = 0;
10422 dpyinfo->grabbed = 0;
10423 dpyinfo->reference_count = 0;
10424 dpyinfo->icon_bitmap_id = -1;
10425 dpyinfo->font_table = NULL;
10426 dpyinfo->n_fonts = 0;
10427 dpyinfo->font_table_size = 0;
10428 dpyinfo->bitmaps = 0;
10429 dpyinfo->bitmaps_size = 0;
10430 dpyinfo->bitmaps_last = 0;
10431 dpyinfo->scratch_cursor_gc = 0;
10432 dpyinfo->mouse_face_mouse_frame = 0;
10433 dpyinfo->mouse_face_deferred_gc = 0;
10434 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10435 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10436 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10437 dpyinfo->mouse_face_window = Qnil;
10438 dpyinfo->mouse_face_overlay = Qnil;
10439 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10440 dpyinfo->mouse_face_defer = 0;
10441 dpyinfo->mouse_face_hidden = 0;
10442 dpyinfo->x_focus_frame = 0;
10443 dpyinfo->x_focus_event_frame = 0;
10444 dpyinfo->x_highlight_frame = 0;
10445 dpyinfo->image_cache = make_image_cache ();
10446 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10448 /* See if we can construct pixel values from RGB values. */
10449 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
10450 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
10452 if (dpyinfo->visual->class == TrueColor)
10454 get_bits_and_offset (dpyinfo->visual->red_mask,
10455 &dpyinfo->red_bits, &dpyinfo->red_offset);
10456 get_bits_and_offset (dpyinfo->visual->blue_mask,
10457 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10458 get_bits_and_offset (dpyinfo->visual->green_mask,
10459 &dpyinfo->green_bits, &dpyinfo->green_offset);
10462 /* See if a private colormap is requested. */
10463 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10465 if (dpyinfo->visual->class == PseudoColor)
10467 Lisp_Object value;
10468 value = display_x_get_resource (dpyinfo,
10469 build_string ("privateColormap"),
10470 build_string ("PrivateColormap"),
10471 Qnil, Qnil);
10472 if (STRINGP (value)
10473 && (!strcmp (SDATA (value), "true")
10474 || !strcmp (SDATA (value), "on")))
10475 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10478 else
10479 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10480 dpyinfo->visual, AllocNone);
10483 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10484 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10485 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10486 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10487 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
10488 pixels = DisplayWidth (dpyinfo->display, screen_number);
10489 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10490 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10491 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
10494 dpyinfo->Xatom_wm_protocols
10495 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10496 dpyinfo->Xatom_wm_take_focus
10497 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10498 dpyinfo->Xatom_wm_save_yourself
10499 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10500 dpyinfo->Xatom_wm_delete_window
10501 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10502 dpyinfo->Xatom_wm_change_state
10503 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10504 dpyinfo->Xatom_wm_configure_denied
10505 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10506 dpyinfo->Xatom_wm_window_moved
10507 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10508 dpyinfo->Xatom_wm_client_leader
10509 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
10510 dpyinfo->Xatom_editres
10511 = XInternAtom (dpyinfo->display, "Editres", False);
10512 dpyinfo->Xatom_CLIPBOARD
10513 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10514 dpyinfo->Xatom_TIMESTAMP
10515 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10516 dpyinfo->Xatom_TEXT
10517 = XInternAtom (dpyinfo->display, "TEXT", False);
10518 dpyinfo->Xatom_COMPOUND_TEXT
10519 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10520 dpyinfo->Xatom_UTF8_STRING
10521 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10522 dpyinfo->Xatom_DELETE
10523 = XInternAtom (dpyinfo->display, "DELETE", False);
10524 dpyinfo->Xatom_MULTIPLE
10525 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10526 dpyinfo->Xatom_INCR
10527 = XInternAtom (dpyinfo->display, "INCR", False);
10528 dpyinfo->Xatom_EMACS_TMP
10529 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10530 dpyinfo->Xatom_TARGETS
10531 = XInternAtom (dpyinfo->display, "TARGETS", False);
10532 dpyinfo->Xatom_NULL
10533 = XInternAtom (dpyinfo->display, "NULL", False);
10534 dpyinfo->Xatom_ATOM_PAIR
10535 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10536 /* For properties of font. */
10537 dpyinfo->Xatom_PIXEL_SIZE
10538 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10539 dpyinfo->Xatom_AVERAGE_WIDTH
10540 = XInternAtom (dpyinfo->display, "AVERAGE_WIDTH", False);
10541 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10542 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10543 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10544 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10545 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10546 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10548 /* Ghostscript support. */
10549 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10550 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10552 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10553 False);
10555 dpyinfo->cut_buffers_initialized = 0;
10557 connection = ConnectionNumber (dpyinfo->display);
10558 dpyinfo->connection = connection;
10561 char null_bits[1];
10563 null_bits[0] = 0x00;
10565 dpyinfo->null_pixel
10566 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10567 null_bits, 1, 1, (long) 0, (long) 0,
10572 extern int gray_bitmap_width, gray_bitmap_height;
10573 extern char *gray_bitmap_bits;
10574 dpyinfo->gray
10575 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10576 gray_bitmap_bits,
10577 gray_bitmap_width, gray_bitmap_height,
10578 (unsigned long) 1, (unsigned long) 0, 1);
10581 #ifdef HAVE_X_I18N
10582 xim_initialize (dpyinfo, resource_name);
10583 #endif
10585 #ifdef subprocesses
10586 /* This is only needed for distinguishing keyboard and process input. */
10587 if (connection != 0)
10588 add_keyboard_wait_descriptor (connection);
10589 #endif
10591 #ifndef F_SETOWN_BUG
10592 #ifdef F_SETOWN
10593 #ifdef F_SETOWN_SOCK_NEG
10594 /* stdin is a socket here */
10595 fcntl (connection, F_SETOWN, -getpid ());
10596 #else /* ! defined (F_SETOWN_SOCK_NEG) */
10597 fcntl (connection, F_SETOWN, getpid ());
10598 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
10599 #endif /* ! defined (F_SETOWN) */
10600 #endif /* F_SETOWN_BUG */
10602 #ifdef SIGIO
10603 if (interrupt_input)
10604 init_sigio (connection);
10605 #endif /* ! defined (SIGIO) */
10607 #ifdef USE_LUCID
10608 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
10609 /* Make sure that we have a valid font for dialog boxes
10610 so that Xt does not crash. */
10612 Display *dpy = dpyinfo->display;
10613 XrmValue d, fr, to;
10614 Font font;
10615 int count;
10617 d.addr = (XPointer)&dpy;
10618 d.size = sizeof (Display *);
10619 fr.addr = XtDefaultFont;
10620 fr.size = sizeof (XtDefaultFont);
10621 to.size = sizeof (Font *);
10622 to.addr = (XPointer)&font;
10623 count = x_catch_errors (dpy);
10624 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10625 abort ();
10626 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10627 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10628 x_uncatch_errors (dpy, count);
10630 #endif
10631 #endif
10633 /* See if we should run in synchronous mode. This is useful
10634 for debugging X code. */
10636 Lisp_Object value;
10637 value = display_x_get_resource (dpyinfo,
10638 build_string ("synchronous"),
10639 build_string ("Synchronous"),
10640 Qnil, Qnil);
10641 if (STRINGP (value)
10642 && (!strcmp (SDATA (value), "true")
10643 || !strcmp (SDATA (value), "on")))
10644 XSynchronize (dpyinfo->display, True);
10648 Lisp_Object value;
10649 value = display_x_get_resource (dpyinfo,
10650 build_string ("useXIM"),
10651 build_string ("UseXIM"),
10652 Qnil, Qnil);
10653 #ifdef USE_XIM
10654 if (STRINGP (value)
10655 && (!strcmp (XSTRING (value)->data, "false")
10656 || !strcmp (XSTRING (value)->data, "off")))
10657 use_xim = 0;
10658 #else
10659 if (STRINGP (value)
10660 && (!strcmp (XSTRING (value)->data, "true")
10661 || !strcmp (XSTRING (value)->data, "on")))
10662 use_xim = 1;
10663 #endif
10666 #ifdef HAVE_X_SM
10667 /* Only do this for the very first display in the Emacs session.
10668 Ignore X session management when Emacs was first started on a
10669 tty. */
10670 if (terminal->id == 1)
10671 x_session_initialize (dpyinfo);
10672 #endif
10674 UNBLOCK_INPUT;
10676 return dpyinfo;
10679 /* Get rid of display DPYINFO, assuming all frames are already gone,
10680 and without sending any more commands to the X server. */
10682 void
10683 x_delete_display (dpyinfo)
10684 struct x_display_info *dpyinfo;
10686 int i;
10687 struct terminal *t;
10689 /* Delete the generic struct terminal for this X display. */
10690 for (t = terminal_list; t; t = t->next_terminal)
10691 if (t->type == output_x_window && t->display_info.x == dpyinfo)
10693 /* Close X session management when we close its display. */
10694 if (t->id == 1 && x_session_have_connection ())
10695 x_session_close();
10697 delete_terminal (t);
10698 break;
10701 delete_keyboard_wait_descriptor (dpyinfo->connection);
10703 /* Discard this display from x_display_name_list and x_display_list.
10704 We can't use Fdelq because that can quit. */
10705 if (! NILP (x_display_name_list)
10706 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10707 x_display_name_list = XCDR (x_display_name_list);
10708 else
10710 Lisp_Object tail;
10712 tail = x_display_name_list;
10713 while (CONSP (tail) && CONSP (XCDR (tail)))
10715 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10717 XSETCDR (tail, XCDR (XCDR (tail)));
10718 break;
10720 tail = XCDR (tail);
10724 if (next_noop_dpyinfo == dpyinfo)
10725 next_noop_dpyinfo = dpyinfo->next;
10727 if (x_display_list == dpyinfo)
10728 x_display_list = dpyinfo->next;
10729 else
10731 struct x_display_info *tail;
10733 for (tail = x_display_list; tail; tail = tail->next)
10734 if (tail->next == dpyinfo)
10735 tail->next = tail->next->next;
10738 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
10739 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
10740 XrmDestroyDatabase (dpyinfo->xrdb);
10741 #endif
10742 #endif
10743 #ifdef HAVE_X_I18N
10744 if (dpyinfo->xim)
10745 xim_close_dpy (dpyinfo);
10746 #endif
10748 /* Free the font names in the font table. */
10749 for (i = 0; i < dpyinfo->n_fonts; i++)
10750 if (dpyinfo->font_table[i].name)
10752 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
10753 xfree (dpyinfo->font_table[i].full_name);
10754 xfree (dpyinfo->font_table[i].name);
10757 if (dpyinfo->font_table && dpyinfo->font_table->font_encoder)
10758 xfree (dpyinfo->font_table->font_encoder);
10760 if (dpyinfo->font_table)
10761 xfree (dpyinfo->font_table);
10762 xfree (dpyinfo->x_id_name);
10763 xfree (dpyinfo->color_cells);
10764 xfree (dpyinfo);
10767 #ifdef USE_X_TOOLKIT
10769 /* Atimer callback function for TIMER. Called every 0.1s to process
10770 Xt timeouts, if needed. We must avoid calling XtAppPending as
10771 much as possible because that function does an implicit XFlush
10772 that slows us down. */
10774 static void
10775 x_process_timeouts (timer)
10776 struct atimer *timer;
10778 if (toolkit_scroll_bar_interaction || popup_activated ())
10780 BLOCK_INPUT;
10781 while (XtAppPending (Xt_app_con) & XtIMTimer)
10782 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10783 UNBLOCK_INPUT;
10787 #endif /* USE_X_TOOLKIT */
10790 /* Set up use of X before we make the first connection. */
10792 extern frame_parm_handler x_frame_parm_handlers[];
10794 static struct redisplay_interface x_redisplay_interface =
10796 x_frame_parm_handlers,
10797 x_produce_glyphs,
10798 x_write_glyphs,
10799 x_insert_glyphs,
10800 x_clear_end_of_line,
10801 x_scroll_run,
10802 x_after_update_window_line,
10803 x_update_window_begin,
10804 x_update_window_end,
10805 x_cursor_to,
10806 x_flush,
10807 #ifdef XFlush
10808 x_flush,
10809 #else
10810 0, /* flush_display_optional */
10811 #endif
10812 x_clear_window_mouse_face,
10813 x_get_glyph_overhangs,
10814 x_fix_overlapping_area,
10815 x_draw_fringe_bitmap,
10816 0, /* define_fringe_bitmap */
10817 0, /* destroy_fringe_bitmap */
10818 x_per_char_metric,
10819 x_encode_char,
10820 x_compute_glyph_string_overhangs,
10821 x_draw_glyph_string,
10822 x_define_frame_cursor,
10823 x_clear_frame_area,
10824 x_draw_window_cursor,
10825 x_draw_vertical_window_border,
10826 x_shift_glyphs_for_insert
10830 /* This function is called when the last frame on a display is deleted. */
10831 void
10832 x_delete_terminal (struct terminal *terminal)
10834 struct x_display_info *dpyinfo = terminal->display_info.x;
10835 int i;
10837 BLOCK_INPUT;
10838 /* Free the fonts in the font table. */
10839 for (i = 0; i < dpyinfo->n_fonts; i++)
10840 if (dpyinfo->font_table[i].name)
10842 XFreeFont (dpyinfo->display, dpyinfo->font_table[i].font);
10845 x_destroy_all_bitmaps (dpyinfo);
10846 XSetCloseDownMode (dpyinfo->display, DestroyAll);
10848 #ifdef USE_X_TOOLKIT
10849 XtCloseDisplay (dpyinfo->display);
10850 #else
10851 #ifdef USE_GTK
10852 xg_display_close (dpyinfo->display);
10853 #else
10854 XCloseDisplay (dpyinfo->display);
10855 #endif
10856 #endif
10858 x_delete_display (dpyinfo);
10859 UNBLOCK_INPUT;
10863 static struct terminal *
10864 x_create_terminal (struct x_display_info *dpyinfo)
10866 struct terminal *terminal;
10868 terminal = create_terminal ();
10870 terminal->type = output_x_window;
10871 terminal->display_info.x = dpyinfo;
10872 dpyinfo->terminal = terminal;
10874 /* kboard is initialized in x_term_init. */
10876 terminal->clear_frame_hook = x_clear_frame;
10877 terminal->ins_del_lines_hook = x_ins_del_lines;
10878 terminal->delete_glyphs_hook = x_delete_glyphs;
10879 terminal->ring_bell_hook = XTring_bell;
10880 terminal->reset_terminal_modes_hook = XTreset_terminal_modes;
10881 terminal->set_terminal_modes_hook = XTset_terminal_modes;
10882 terminal->update_begin_hook = x_update_begin;
10883 terminal->update_end_hook = x_update_end;
10884 terminal->set_terminal_window_hook = XTset_terminal_window;
10885 terminal->read_socket_hook = XTread_socket;
10886 terminal->frame_up_to_date_hook = XTframe_up_to_date;
10887 terminal->mouse_position_hook = XTmouse_position;
10888 terminal->frame_rehighlight_hook = XTframe_rehighlight;
10889 terminal->frame_raise_lower_hook = XTframe_raise_lower;
10890 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10891 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10892 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
10893 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
10895 terminal->delete_frame_hook = x_destroy_window;
10896 terminal->delete_terminal_hook = x_delete_terminal;
10898 terminal->rif = &x_redisplay_interface;
10899 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
10900 terminal->char_ins_del_ok = 1;
10901 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
10902 terminal->fast_clear_end_of_line = 1; /* X does this well. */
10903 terminal->memory_below_frame = 0; /* We don't remember what scrolls
10904 off the bottom. */
10906 return terminal;
10909 void
10910 x_initialize ()
10912 baud_rate = 19200;
10914 x_noop_count = 0;
10915 last_tool_bar_item = -1;
10916 any_help_event_p = 0;
10917 ignore_next_mouse_click_timeout = 0;
10919 #ifdef USE_GTK
10920 current_count = -1;
10921 #endif
10923 /* Try to use interrupt input; if we can't, then start polling. */
10924 Fset_input_interrupt_mode (Qt);
10926 #ifdef USE_X_TOOLKIT
10927 XtToolkitInitialize ();
10929 Xt_app_con = XtCreateApplicationContext ();
10931 /* Register a converter from strings to pixels, which uses
10932 Emacs' color allocation infrastructure. */
10933 XtAppSetTypeConverter (Xt_app_con,
10934 XtRString, XtRPixel, cvt_string_to_pixel,
10935 cvt_string_to_pixel_args,
10936 XtNumber (cvt_string_to_pixel_args),
10937 XtCacheByDisplay, cvt_pixel_dtor);
10939 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10941 /* Install an asynchronous timer that processes Xt timeout events
10942 every 0.1s. This is necessary because some widget sets use
10943 timeouts internally, for example the LessTif menu bar, or the
10944 Xaw3d scroll bar. When Xt timouts aren't processed, these
10945 widgets don't behave normally. */
10947 EMACS_TIME interval;
10948 EMACS_SET_SECS_USECS (interval, 0, 100000);
10949 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
10951 #endif
10953 #ifdef USE_TOOLKIT_SCROLL_BARS
10954 #ifndef USE_GTK
10955 xaw3d_arrow_scroll = False;
10956 xaw3d_pick_top = True;
10957 #endif
10958 #endif
10960 /* Note that there is no real way portable across R3/R4 to get the
10961 original error handler. */
10962 XSetErrorHandler (x_error_handler);
10963 XSetIOErrorHandler (x_io_error_quitter);
10965 /* Disable Window Change signals; they are handled by X events. */
10966 #if 0 /* Don't. We may want to open tty frames later. */
10967 #ifdef SIGWINCH
10968 signal (SIGWINCH, SIG_DFL);
10969 #endif /* SIGWINCH */
10970 #endif
10972 signal (SIGPIPE, x_connection_signal);
10976 void
10977 syms_of_xterm ()
10979 x_error_message = NULL;
10981 staticpro (&x_display_name_list);
10982 x_display_name_list = Qnil;
10984 staticpro (&last_mouse_scroll_bar);
10985 last_mouse_scroll_bar = Qnil;
10987 staticpro (&Qvendor_specific_keysyms);
10988 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
10990 staticpro (&Qutf_8);
10991 Qutf_8 = intern ("utf-8");
10992 staticpro (&Qlatin_1);
10993 Qlatin_1 = intern ("latin-1");
10995 staticpro (&last_mouse_press_frame);
10996 last_mouse_press_frame = Qnil;
10998 DEFVAR_BOOL ("x-use-underline-position-properties",
10999 &x_use_underline_position_properties,
11000 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
11001 nil means ignore them. If you encounter fonts with bogus
11002 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
11003 to 4.1, set this to nil. */);
11004 x_use_underline_position_properties = 1;
11006 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
11007 &x_mouse_click_focus_ignore_position,
11008 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
11009 This variable is only used when the window manager requires that you
11010 click on a frame to select it (give it focus). In that case, a value
11011 of nil, means that the selected window and cursor position changes to
11012 reflect the mouse click position, while a non-nil value means that the
11013 selected window or cursor position is preserved. */);
11014 x_mouse_click_focus_ignore_position = 0;
11016 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
11017 doc: /* What X toolkit scroll bars Emacs uses.
11018 A value of nil means Emacs doesn't use X toolkit scroll bars.
11019 Otherwise, value is a symbol describing the X toolkit. */);
11020 #ifdef USE_TOOLKIT_SCROLL_BARS
11021 #ifdef USE_MOTIF
11022 Vx_toolkit_scroll_bars = intern ("motif");
11023 #elif defined HAVE_XAW3D
11024 Vx_toolkit_scroll_bars = intern ("xaw3d");
11025 #elif USE_GTK
11026 Vx_toolkit_scroll_bars = intern ("gtk");
11027 #else
11028 Vx_toolkit_scroll_bars = intern ("xaw");
11029 #endif
11030 #else
11031 Vx_toolkit_scroll_bars = Qnil;
11032 #endif
11034 staticpro (&last_mouse_motion_frame);
11035 last_mouse_motion_frame = Qnil;
11037 Qmodifier_value = intern ("modifier-value");
11038 Qalt = intern ("alt");
11039 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
11040 Qhyper = intern ("hyper");
11041 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
11042 Qmeta = intern ("meta");
11043 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
11044 Qsuper = intern ("super");
11045 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
11047 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
11048 doc: /* Which keys Emacs uses for the alt modifier.
11049 This should be one of the symbols `alt', `hyper', `meta', `super'.
11050 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
11051 is nil, which is the same as `alt'. */);
11052 Vx_alt_keysym = Qnil;
11054 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
11055 doc: /* Which keys Emacs uses for the hyper modifier.
11056 This should be one of the symbols `alt', `hyper', `meta', `super'.
11057 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
11058 default is nil, which is the same as `hyper'. */);
11059 Vx_hyper_keysym = Qnil;
11061 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
11062 doc: /* Which keys Emacs uses for the meta modifier.
11063 This should be one of the symbols `alt', `hyper', `meta', `super'.
11064 For example, `meta' means use the Meta_L and Meta_R keysyms. The
11065 default is nil, which is the same as `meta'. */);
11066 Vx_meta_keysym = Qnil;
11068 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
11069 doc: /* Which keys Emacs uses for the super modifier.
11070 This should be one of the symbols `alt', `hyper', `meta', `super'.
11071 For example, `super' means use the Super_L and Super_R keysyms. The
11072 default is nil, which is the same as `super'. */);
11073 Vx_super_keysym = Qnil;
11075 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
11076 doc: /* Hash table of character codes indexed by X keysym codes. */);
11077 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
11078 make_float (DEFAULT_REHASH_SIZE),
11079 make_float (DEFAULT_REHASH_THRESHOLD),
11080 Qnil, Qnil, Qnil);
11083 #endif /* HAVE_X_WINDOWS */
11085 /* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
11086 (do not change this comment) */