(yank-pop-change-selection): New option.
[emacs.git] / src / xterm.c
blob895d61df71400827d1475dceafc6a7bd2face454
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, 2007 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, 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 /* Default to using XIM if available. */
158 #ifdef USE_XIM
159 int use_xim = 1;
160 #else
161 int use_xim = 0; /* configure --without-xim */
162 #endif
166 /* Non-nil means Emacs uses toolkit scroll bars. */
168 Lisp_Object Vx_toolkit_scroll_bars;
170 /* Non-zero means that a HELP_EVENT has been generated since Emacs
171 start. */
173 static int any_help_event_p;
175 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
176 static Lisp_Object last_window;
178 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
180 int x_use_underline_position_properties;
182 /* Non-zero means to draw the underline at the same place as the descent line. */
184 int x_underline_at_descent_line;
186 /* This is a chain of structures for all the X displays currently in
187 use. */
189 struct x_display_info *x_display_list;
191 /* This is a list of cons cells, each of the form (NAME
192 . FONT-LIST-CACHE), one for each element of x_display_list and in
193 the same order. NAME is the name of the frame. FONT-LIST-CACHE
194 records previous values returned by x-list-fonts. */
196 Lisp_Object x_display_name_list;
198 /* Frame being updated by update_frame. This is declared in term.c.
199 This is set by update_begin and looked at by all the XT functions.
200 It is zero while not inside an update. In that case, the XT
201 functions assume that `selected_frame' is the frame to apply to. */
203 extern struct frame *updating_frame;
205 /* This is a frame waiting to be auto-raised, within XTread_socket. */
207 struct frame *pending_autoraise_frame;
209 #ifdef USE_X_TOOLKIT
210 /* The application context for Xt use. */
211 XtAppContext Xt_app_con;
212 static String Xt_default_resources[] = {0};
213 #endif /* USE_X_TOOLKIT */
215 /* Non-zero means user is interacting with a toolkit scroll bar. */
217 static int toolkit_scroll_bar_interaction;
219 /* Non-zero means to not move point as a result of clicking on a
220 frame to focus it (when focus-follows-mouse is nil). */
222 int x_mouse_click_focus_ignore_position;
224 /* Non-zero timeout value means ignore next mouse click if it arrives
225 before that timeout elapses (i.e. as part of the same sequence of
226 events resulting from clicking on a frame to select it). */
228 static unsigned long ignore_next_mouse_click_timeout;
230 /* Mouse movement.
232 Formerly, we used PointerMotionHintMask (in standard_event_mask)
233 so that we would have to call XQueryPointer after each MotionNotify
234 event to ask for another such event. However, this made mouse tracking
235 slow, and there was a bug that made it eventually stop.
237 Simply asking for MotionNotify all the time seems to work better.
239 In order to avoid asking for motion events and then throwing most
240 of them away or busy-polling the server for mouse positions, we ask
241 the server for pointer motion hints. This means that we get only
242 one event per group of mouse movements. "Groups" are delimited by
243 other kinds of events (focus changes and button clicks, for
244 example), or by XQueryPointer calls; when one of these happens, we
245 get another MotionNotify event the next time the mouse moves. This
246 is at least as efficient as getting motion events when mouse
247 tracking is on, and I suspect only negligibly worse when tracking
248 is off. */
250 /* Where the mouse was last time we reported a mouse event. */
252 static XRectangle last_mouse_glyph;
253 static FRAME_PTR last_mouse_glyph_frame;
254 static Lisp_Object last_mouse_press_frame;
256 /* The scroll bar in which the last X motion event occurred.
258 If the last X motion event occurred in a scroll bar, we set this so
259 XTmouse_position can know whether to report a scroll bar motion or
260 an ordinary motion.
262 If the last X motion event didn't occur in a scroll bar, we set
263 this to Qnil, to tell XTmouse_position to return an ordinary motion
264 event. */
266 static Lisp_Object last_mouse_scroll_bar;
268 /* This is a hack. We would really prefer that XTmouse_position would
269 return the time associated with the position it returns, but there
270 doesn't seem to be any way to wrest the time-stamp from the server
271 along with the position query. So, we just keep track of the time
272 of the last movement we received, and return that in hopes that
273 it's somewhat accurate. */
275 static Time last_mouse_movement_time;
277 /* Time for last user interaction as returned in X events. */
279 static Time last_user_time;
281 /* Incremented by XTread_socket whenever it really tries to read
282 events. */
284 #ifdef __STDC__
285 static int volatile input_signal_count;
286 #else
287 static int input_signal_count;
288 #endif
290 /* Used locally within XTread_socket. */
292 static int x_noop_count;
294 /* Initial values of argv and argc. */
296 extern char **initial_argv;
297 extern int initial_argc;
299 extern Lisp_Object Vcommand_line_args, Vsystem_name;
301 /* Tells if a window manager is present or not. */
303 extern Lisp_Object Vx_no_window_manager;
305 extern Lisp_Object Qeql;
307 extern int errno;
309 /* A mask of extra modifier bits to put into every keyboard char. */
311 extern EMACS_INT extra_keyboard_modifiers;
313 /* The keysyms to use for the various modifiers. */
315 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
316 Lisp_Object Vx_keysym_table;
317 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
319 static Lisp_Object Qvendor_specific_keysyms;
320 static Lisp_Object Qlatin_1;
322 /* Used in x_flush. */
324 extern Lisp_Object Vinhibit_redisplay;
326 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
327 extern int x_bitmap_mask P_ ((FRAME_PTR, int));
329 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
330 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
331 static const XColor *x_color_cells P_ ((Display *, int *));
332 static void x_update_window_end P_ ((struct window *, int, int));
334 static int x_io_error_quitter P_ ((Display *));
335 static struct terminal *x_create_terminal P_ ((struct x_display_info *));
336 void x_delete_terminal P_ ((struct terminal *));
337 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
338 static int x_compute_min_glyph_bounds P_ ((struct frame *));
339 static void x_update_end P_ ((struct frame *));
340 static void XTframe_up_to_date P_ ((struct frame *));
341 static void XTset_terminal_modes P_ ((struct terminal *));
342 static void XTreset_terminal_modes P_ ((struct terminal *));
343 static void x_clear_frame P_ ((struct frame *));
344 static void frame_highlight P_ ((struct frame *));
345 static void frame_unhighlight P_ ((struct frame *));
346 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
347 static void x_focus_changed P_ ((int, int, struct x_display_info *,
348 struct frame *, struct input_event *));
349 static void x_detect_focus_change P_ ((struct x_display_info *,
350 XEvent *, struct input_event *));
351 static void XTframe_rehighlight P_ ((struct frame *));
352 static void x_frame_rehighlight P_ ((struct x_display_info *));
353 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
354 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
355 enum text_cursor_kinds));
357 static void x_clip_to_row P_ ((struct window *, struct glyph_row *, int, GC));
358 static void x_flush P_ ((struct frame *f));
359 static void x_update_begin P_ ((struct frame *));
360 static void x_update_window_begin P_ ((struct window *));
361 static void x_after_update_window_line P_ ((struct glyph_row *));
362 static struct scroll_bar *x_window_to_scroll_bar P_ ((Display *, Window));
363 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
364 enum scroll_bar_part *,
365 Lisp_Object *, Lisp_Object *,
366 unsigned long *));
367 static void x_check_fullscreen P_ ((struct frame *));
368 static void x_check_expected_move P_ ((struct frame *, int, int));
369 static void x_sync_with_move P_ ((struct frame *, int, int, int));
370 static int handle_one_xevent P_ ((struct x_display_info *, XEvent *,
371 int *, struct input_event *));
372 /* Don't declare this NO_RETURN because we want no
373 interference with debugging failing X calls. */
374 static SIGTYPE x_connection_closed P_ ((Display *, char *));
377 /* Flush display of frame F, or of all frames if F is null. */
379 static void
380 x_flush (f)
381 struct frame *f;
383 /* Don't call XFlush when it is not safe to redisplay; the X
384 connection may be broken. */
385 if (!NILP (Vinhibit_redisplay))
386 return;
388 BLOCK_INPUT;
389 if (f == NULL)
391 Lisp_Object rest, frame;
392 FOR_EACH_FRAME (rest, frame)
393 if (FRAME_X_P (XFRAME (frame)))
394 x_flush (XFRAME (frame));
396 else if (FRAME_X_P (f))
397 XFlush (FRAME_X_DISPLAY (f));
398 UNBLOCK_INPUT;
402 /* Remove calls to XFlush by defining XFlush to an empty replacement.
403 Calls to XFlush should be unnecessary because the X output buffer
404 is flushed automatically as needed by calls to XPending,
405 XNextEvent, or XWindowEvent according to the XFlush man page.
406 XTread_socket calls XPending. Removing XFlush improves
407 performance. */
409 #define XFlush(DISPLAY) (void) 0
412 /***********************************************************************
413 Debugging
414 ***********************************************************************/
416 #if 0
418 /* This is a function useful for recording debugging information about
419 the sequence of occurrences in this file. */
421 struct record
423 char *locus;
424 int type;
427 struct record event_record[100];
429 int event_record_index;
431 record_event (locus, type)
432 char *locus;
433 int type;
435 if (event_record_index == sizeof (event_record) / sizeof (struct record))
436 event_record_index = 0;
438 event_record[event_record_index].locus = locus;
439 event_record[event_record_index].type = type;
440 event_record_index++;
443 #endif /* 0 */
447 /* Return the struct x_display_info corresponding to DPY. */
449 struct x_display_info *
450 x_display_info_for_display (dpy)
451 Display *dpy;
453 struct x_display_info *dpyinfo;
455 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
456 if (dpyinfo->display == dpy)
457 return dpyinfo;
459 return 0;
464 /***********************************************************************
465 Starting and ending an update
466 ***********************************************************************/
468 /* Start an update of frame F. This function is installed as a hook
469 for update_begin, i.e. it is called when update_begin is called.
470 This function is called prior to calls to x_update_window_begin for
471 each window being updated. Currently, there is nothing to do here
472 because all interesting stuff is done on a window basis. */
474 static void
475 x_update_begin (f)
476 struct frame *f;
478 /* Nothing to do. */
482 /* Start update of window W. Set the global variable updated_window
483 to the window being updated and set output_cursor to the cursor
484 position of W. */
486 static void
487 x_update_window_begin (w)
488 struct window *w;
490 struct frame *f = XFRAME (WINDOW_FRAME (w));
491 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
493 updated_window = w;
494 set_output_cursor (&w->cursor);
496 BLOCK_INPUT;
498 if (f == display_info->mouse_face_mouse_frame)
500 /* Don't do highlighting for mouse motion during the update. */
501 display_info->mouse_face_defer = 1;
503 /* If F needs to be redrawn, simply forget about any prior mouse
504 highlighting. */
505 if (FRAME_GARBAGED_P (f))
506 display_info->mouse_face_window = Qnil;
508 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
509 their mouse_face_p flag set, which means that they are always
510 unequal to rows in a desired matrix which never have that
511 flag set. So, rows containing mouse-face glyphs are never
512 scrolled, and we don't have to switch the mouse highlight off
513 here to prevent it from being scrolled. */
515 /* Can we tell that this update does not affect the window
516 where the mouse highlight is? If so, no need to turn off.
517 Likewise, don't do anything if the frame is garbaged;
518 in that case, the frame's current matrix that we would use
519 is all wrong, and we will redisplay that line anyway. */
520 if (!NILP (display_info->mouse_face_window)
521 && w == XWINDOW (display_info->mouse_face_window))
523 int i;
525 for (i = 0; i < w->desired_matrix->nrows; ++i)
526 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
527 break;
529 if (i < w->desired_matrix->nrows)
530 clear_mouse_face (display_info);
532 #endif /* 0 */
535 UNBLOCK_INPUT;
539 /* Draw a vertical window border from (x,y0) to (x,y1) */
541 static void
542 x_draw_vertical_window_border (w, x, y0, y1)
543 struct window *w;
544 int x, y0, y1;
546 struct frame *f = XFRAME (WINDOW_FRAME (w));
547 struct face *face;
549 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
550 if (face)
551 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
552 face->foreground);
554 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
555 f->output_data.x->normal_gc, x, y0, x, y1);
558 /* End update of window W (which is equal to updated_window).
560 Draw vertical borders between horizontally adjacent windows, and
561 display W's cursor if CURSOR_ON_P is non-zero.
563 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
564 glyphs in mouse-face were overwritten. In that case we have to
565 make sure that the mouse-highlight is properly redrawn.
567 W may be a menu bar pseudo-window in case we don't have X toolkit
568 support. Such windows don't have a cursor, so don't display it
569 here. */
571 static void
572 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
573 struct window *w;
574 int cursor_on_p, mouse_face_overwritten_p;
576 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
578 if (!w->pseudo_window_p)
580 BLOCK_INPUT;
582 if (cursor_on_p)
583 display_and_set_cursor (w, 1, output_cursor.hpos,
584 output_cursor.vpos,
585 output_cursor.x, output_cursor.y);
587 if (draw_window_fringes (w, 1))
588 x_draw_vertical_border (w);
590 UNBLOCK_INPUT;
593 /* If a row with mouse-face was overwritten, arrange for
594 XTframe_up_to_date to redisplay the mouse highlight. */
595 if (mouse_face_overwritten_p)
597 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
598 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
599 dpyinfo->mouse_face_window = Qnil;
602 updated_window = NULL;
606 /* End update of frame F. This function is installed as a hook in
607 update_end. */
609 static void
610 x_update_end (f)
611 struct frame *f;
613 /* Mouse highlight may be displayed again. */
614 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
616 #ifndef XFlush
617 BLOCK_INPUT;
618 XFlush (FRAME_X_DISPLAY (f));
619 UNBLOCK_INPUT;
620 #endif
624 /* This function is called from various places in xdisp.c whenever a
625 complete update has been performed. The global variable
626 updated_window is not available here. */
628 static void
629 XTframe_up_to_date (f)
630 struct frame *f;
632 if (FRAME_X_P (f))
634 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
636 if (dpyinfo->mouse_face_deferred_gc
637 || f == dpyinfo->mouse_face_mouse_frame)
639 BLOCK_INPUT;
640 if (dpyinfo->mouse_face_mouse_frame)
641 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
642 dpyinfo->mouse_face_mouse_x,
643 dpyinfo->mouse_face_mouse_y);
644 dpyinfo->mouse_face_deferred_gc = 0;
645 UNBLOCK_INPUT;
651 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
652 arrow bitmaps, or clear the fringes if no bitmaps are required
653 before DESIRED_ROW is made current. The window being updated is
654 found in updated_window. This function It is called from
655 update_window_line only if it is known that there are differences
656 between bitmaps to be drawn between current row and DESIRED_ROW. */
658 static void
659 x_after_update_window_line (desired_row)
660 struct glyph_row *desired_row;
662 struct window *w = updated_window;
663 struct frame *f;
664 int width, height;
666 xassert (w);
668 if (!desired_row->mode_line_p && !w->pseudo_window_p)
669 desired_row->redraw_fringe_bitmaps_p = 1;
671 /* When a window has disappeared, make sure that no rest of
672 full-width rows stays visible in the internal border. Could
673 check here if updated_window is the leftmost/rightmost window,
674 but I guess it's not worth doing since vertically split windows
675 are almost never used, internal border is rarely set, and the
676 overhead is very small. */
677 if (windows_or_buffers_changed
678 && desired_row->full_width_p
679 && (f = XFRAME (w->frame),
680 width = FRAME_INTERNAL_BORDER_WIDTH (f),
681 width != 0)
682 && (height = desired_row->visible_height,
683 height > 0))
685 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
687 /* Internal border is drawn below the tool bar. */
688 if (WINDOWP (f->tool_bar_window)
689 && w == XWINDOW (f->tool_bar_window))
690 y -= width;
692 BLOCK_INPUT;
693 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
694 0, y, width, height, False);
695 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
696 FRAME_PIXEL_WIDTH (f) - width,
697 y, width, height, False);
698 UNBLOCK_INPUT;
702 static void
703 x_draw_fringe_bitmap (w, row, p)
704 struct window *w;
705 struct glyph_row *row;
706 struct draw_fringe_bitmap_params *p;
708 struct frame *f = XFRAME (WINDOW_FRAME (w));
709 Display *display = FRAME_X_DISPLAY (f);
710 Window window = FRAME_X_WINDOW (f);
711 GC gc = f->output_data.x->normal_gc;
712 struct face *face = p->face;
713 int rowY;
715 /* Must clip because of partially visible lines. */
716 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
717 if (p->y < rowY)
719 /* Adjust position of "bottom aligned" bitmap on partially
720 visible last row. */
721 int oldY = row->y;
722 int oldVH = row->visible_height;
723 row->visible_height = p->h;
724 row->y -= rowY - p->y;
725 x_clip_to_row (w, row, -1, gc);
726 row->y = oldY;
727 row->visible_height = oldVH;
729 else
730 x_clip_to_row (w, row, -1, gc);
732 if (!p->overlay_p)
734 int bx = p->bx, by = p->by, nx = p->nx, ny = p->ny;
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 #ifdef USE_TOOLKIT_SCROLL_BARS
746 /* If the fringe is adjacent to the left (right) scroll bar of a
747 leftmost (rightmost, respectively) window, then extend its
748 background to the gap between the fringe and the bar. */
749 if ((WINDOW_LEFTMOST_P (w)
750 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
751 || (WINDOW_RIGHTMOST_P (w)
752 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
754 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
756 if (sb_width > 0)
758 int left = WINDOW_SCROLL_BAR_AREA_X (w);
759 int width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
760 * FRAME_COLUMN_WIDTH (f));
762 if (bx < 0)
764 /* Bitmap fills the fringe. */
765 if (left + width == p->x)
766 bx = left + sb_width;
767 else if (p->x + p->wd == left)
768 bx = left;
769 if (bx >= 0)
771 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
773 nx = width - sb_width;
774 by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
775 row->y));
776 ny = row->visible_height;
779 else
781 if (left + width == bx)
783 bx = left + sb_width;
784 nx += width - sb_width;
786 else if (bx + nx == left)
787 nx += width - sb_width;
791 #endif
792 if (bx >= 0 && nx > 0)
793 XFillRectangle (display, window, face->gc, bx, by, nx, ny);
795 if (!face->stipple)
796 XSetForeground (display, face->gc, face->foreground);
799 if (p->which)
801 unsigned char *bits;
802 Pixmap pixmap, clipmask = (Pixmap) 0;
803 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
804 XGCValues gcv;
806 if (p->wd > 8)
807 bits = (unsigned char *)(p->bits + p->dh);
808 else
809 bits = (unsigned char *)p->bits + p->dh;
811 /* Draw the bitmap. I believe these small pixmaps can be cached
812 by the server. */
813 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
814 (p->cursor_p
815 ? (p->overlay_p ? face->background
816 : f->output_data.x->cursor_pixel)
817 : face->foreground),
818 face->background, depth);
820 if (p->overlay_p)
822 clipmask = XCreatePixmapFromBitmapData (display,
823 FRAME_X_DISPLAY_INFO (f)->root_window,
824 bits, p->wd, p->h,
825 1, 0, 1);
826 gcv.clip_mask = clipmask;
827 gcv.clip_x_origin = p->x;
828 gcv.clip_y_origin = p->y;
829 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
832 XCopyArea (display, pixmap, window, gc, 0, 0,
833 p->wd, p->h, p->x, p->y);
834 XFreePixmap (display, pixmap);
836 if (p->overlay_p)
838 gcv.clip_mask = (Pixmap) 0;
839 XChangeGC (display, gc, GCClipMask, &gcv);
840 XFreePixmap (display, clipmask);
844 XSetClipMask (display, gc, None);
849 /* This is called when starting Emacs and when restarting after
850 suspend. When starting Emacs, no X window is mapped. And nothing
851 must be done to Emacs's own window if it is suspended (though that
852 rarely happens). */
854 static void
855 XTset_terminal_modes (struct terminal *terminal)
859 /* This is called when exiting or suspending Emacs. Exiting will make
860 the X-windows go away, and suspending requires no action. */
862 static void
863 XTreset_terminal_modes (struct terminal *terminal)
869 /***********************************************************************
870 Display Iterator
871 ***********************************************************************/
873 /* Function prototypes of this page. */
875 static int x_encode_char P_ ((int, XChar2b *, struct font_info *, int *));
878 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
879 is not contained in the font. */
881 static XCharStruct *
882 x_per_char_metric (font, char2b, font_type)
883 XFontStruct *font;
884 XChar2b *char2b;
885 int font_type; /* unused on X */
887 /* The result metric information. */
888 XCharStruct *pcm = NULL;
890 xassert (font && char2b);
892 if (font->per_char != NULL)
894 if (font->min_byte1 == 0 && font->max_byte1 == 0)
896 /* min_char_or_byte2 specifies the linear character index
897 corresponding to the first element of the per_char array,
898 max_char_or_byte2 is the index of the last character. A
899 character with non-zero CHAR2B->byte1 is not in the font.
900 A character with byte2 less than min_char_or_byte2 or
901 greater max_char_or_byte2 is not in the font. */
902 if (char2b->byte1 == 0
903 && char2b->byte2 >= font->min_char_or_byte2
904 && char2b->byte2 <= font->max_char_or_byte2)
905 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
907 else
909 /* If either min_byte1 or max_byte1 are nonzero, both
910 min_char_or_byte2 and max_char_or_byte2 are less than
911 256, and the 2-byte character index values corresponding
912 to the per_char array element N (counting from 0) are:
914 byte1 = N/D + min_byte1
915 byte2 = N\D + min_char_or_byte2
917 where:
919 D = max_char_or_byte2 - min_char_or_byte2 + 1
920 / = integer division
921 \ = integer modulus */
922 if (char2b->byte1 >= font->min_byte1
923 && char2b->byte1 <= font->max_byte1
924 && char2b->byte2 >= font->min_char_or_byte2
925 && char2b->byte2 <= font->max_char_or_byte2)
927 pcm = (font->per_char
928 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
929 * (char2b->byte1 - font->min_byte1))
930 + (char2b->byte2 - font->min_char_or_byte2));
934 else
936 /* If the per_char pointer is null, all glyphs between the first
937 and last character indexes inclusive have the same
938 information, as given by both min_bounds and max_bounds. */
939 if (char2b->byte2 >= font->min_char_or_byte2
940 && char2b->byte2 <= font->max_char_or_byte2)
941 pcm = &font->max_bounds;
944 return ((pcm == NULL
945 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
946 ? NULL : pcm);
950 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
951 the two-byte form of C. Encoding is returned in *CHAR2B. */
953 static int
954 x_encode_char (c, char2b, font_info, two_byte_p)
955 int c;
956 XChar2b *char2b;
957 struct font_info *font_info;
958 int *two_byte_p;
960 int charset = CHAR_CHARSET (c);
961 XFontStruct *font = font_info->font;
963 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
964 This may be either a program in a special encoder language or a
965 fixed encoding. */
966 if (font_info->font_encoder)
968 /* It's a program. */
969 struct ccl_program *ccl = font_info->font_encoder;
971 check_ccl_update (ccl);
972 if (CHARSET_DIMENSION (charset) == 1)
974 ccl->reg[0] = charset;
975 ccl->reg[1] = char2b->byte2;
976 ccl->reg[2] = -1;
978 else
980 ccl->reg[0] = charset;
981 ccl->reg[1] = char2b->byte1;
982 ccl->reg[2] = char2b->byte2;
985 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
987 /* We assume that MSBs are appropriately set/reset by CCL
988 program. */
989 if (font->max_byte1 == 0) /* 1-byte font */
990 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
991 else
992 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
994 else if (font_info->encoding[charset])
996 /* Fixed encoding scheme. See fontset.h for the meaning of the
997 encoding numbers. */
998 int enc = font_info->encoding[charset];
1000 if ((enc == 1 || enc == 2)
1001 && CHARSET_DIMENSION (charset) == 2)
1002 char2b->byte1 |= 0x80;
1004 if (enc == 1 || enc == 3)
1005 char2b->byte2 |= 0x80;
1008 if (two_byte_p)
1009 *two_byte_p = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1011 return FONT_TYPE_UNKNOWN;
1016 /***********************************************************************
1017 Glyph display
1018 ***********************************************************************/
1022 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
1023 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
1024 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
1025 int));
1026 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
1027 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
1028 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
1029 static void x_draw_glyph_string P_ ((struct glyph_string *));
1030 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
1031 static void x_set_cursor_gc P_ ((struct glyph_string *));
1032 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
1033 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
1034 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
1035 unsigned long *, double, int));
1036 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
1037 double, int, unsigned long));
1038 static void x_setup_relief_colors P_ ((struct glyph_string *));
1039 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
1040 static void x_draw_image_relief P_ ((struct glyph_string *));
1041 static void x_draw_image_foreground P_ ((struct glyph_string *));
1042 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
1043 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
1044 int, int, int));
1045 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
1046 int, int, int, int, int, int,
1047 XRectangle *));
1048 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
1049 int, int, int, XRectangle *));
1051 #if GLYPH_DEBUG
1052 static void x_check_font P_ ((struct frame *, XFontStruct *));
1053 #endif
1056 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
1057 face. */
1059 static void
1060 x_set_cursor_gc (s)
1061 struct glyph_string *s;
1063 if (s->font == FRAME_FONT (s->f)
1064 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
1065 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
1066 && !s->cmp)
1067 s->gc = s->f->output_data.x->cursor_gc;
1068 else
1070 /* Cursor on non-default face: must merge. */
1071 XGCValues xgcv;
1072 unsigned long mask;
1074 xgcv.background = s->f->output_data.x->cursor_pixel;
1075 xgcv.foreground = s->face->background;
1077 /* If the glyph would be invisible, try a different foreground. */
1078 if (xgcv.foreground == xgcv.background)
1079 xgcv.foreground = s->face->foreground;
1080 if (xgcv.foreground == xgcv.background)
1081 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1082 if (xgcv.foreground == xgcv.background)
1083 xgcv.foreground = s->face->foreground;
1085 /* Make sure the cursor is distinct from text in this face. */
1086 if (xgcv.background == s->face->background
1087 && xgcv.foreground == s->face->foreground)
1089 xgcv.background = s->face->foreground;
1090 xgcv.foreground = s->face->background;
1093 IF_DEBUG (x_check_font (s->f, s->font));
1094 xgcv.font = s->font->fid;
1095 xgcv.graphics_exposures = False;
1096 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
1098 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1099 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1100 mask, &xgcv);
1101 else
1102 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1103 = XCreateGC (s->display, s->window, mask, &xgcv);
1105 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1110 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1112 static void
1113 x_set_mouse_face_gc (s)
1114 struct glyph_string *s;
1116 int face_id;
1117 struct face *face;
1119 /* What face has to be used last for the mouse face? */
1120 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
1121 face = FACE_FROM_ID (s->f, face_id);
1122 if (face == NULL)
1123 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1125 if (s->first_glyph->type == CHAR_GLYPH)
1126 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
1127 else
1128 face_id = FACE_FOR_CHAR (s->f, face, 0);
1129 s->face = FACE_FROM_ID (s->f, face_id);
1130 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1132 /* If font in this face is same as S->font, use it. */
1133 if (s->font == s->face->font)
1134 s->gc = s->face->gc;
1135 else
1137 /* Otherwise construct scratch_cursor_gc with values from FACE
1138 but font FONT. */
1139 XGCValues xgcv;
1140 unsigned long mask;
1142 xgcv.background = s->face->background;
1143 xgcv.foreground = s->face->foreground;
1144 IF_DEBUG (x_check_font (s->f, s->font));
1145 xgcv.font = s->font->fid;
1146 xgcv.graphics_exposures = False;
1147 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
1149 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1150 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1151 mask, &xgcv);
1152 else
1153 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1154 = XCreateGC (s->display, s->window, mask, &xgcv);
1156 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1159 xassert (s->gc != 0);
1163 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1164 Faces to use in the mode line have already been computed when the
1165 matrix was built, so there isn't much to do, here. */
1167 static INLINE void
1168 x_set_mode_line_face_gc (s)
1169 struct glyph_string *s;
1171 s->gc = s->face->gc;
1175 /* Set S->gc of glyph string S for drawing that glyph string. Set
1176 S->stippled_p to a non-zero value if the face of S has a stipple
1177 pattern. */
1179 static INLINE void
1180 x_set_glyph_string_gc (s)
1181 struct glyph_string *s;
1183 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1185 if (s->hl == DRAW_NORMAL_TEXT)
1187 s->gc = s->face->gc;
1188 s->stippled_p = s->face->stipple != 0;
1190 else if (s->hl == DRAW_INVERSE_VIDEO)
1192 x_set_mode_line_face_gc (s);
1193 s->stippled_p = s->face->stipple != 0;
1195 else if (s->hl == DRAW_CURSOR)
1197 x_set_cursor_gc (s);
1198 s->stippled_p = 0;
1200 else if (s->hl == DRAW_MOUSE_FACE)
1202 x_set_mouse_face_gc (s);
1203 s->stippled_p = s->face->stipple != 0;
1205 else if (s->hl == DRAW_IMAGE_RAISED
1206 || s->hl == DRAW_IMAGE_SUNKEN)
1208 s->gc = s->face->gc;
1209 s->stippled_p = s->face->stipple != 0;
1211 else
1213 s->gc = s->face->gc;
1214 s->stippled_p = s->face->stipple != 0;
1217 /* GC must have been set. */
1218 xassert (s->gc != 0);
1222 /* Set clipping for output of glyph string S. S may be part of a mode
1223 line or menu if we don't have X toolkit support. */
1225 static INLINE void
1226 x_set_glyph_string_clipping (s)
1227 struct glyph_string *s;
1229 XRectangle r;
1230 get_glyph_string_clip_rect (s, &r);
1231 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
1235 /* RIF:
1236 Compute left and right overhang of glyph string S. If S is a glyph
1237 string for a composition, assume overhangs don't exist. */
1239 static void
1240 x_compute_glyph_string_overhangs (s)
1241 struct glyph_string *s;
1243 if (s->cmp == NULL
1244 && s->first_glyph->type == CHAR_GLYPH)
1246 XCharStruct cs;
1247 int direction, font_ascent, font_descent;
1248 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
1249 &font_ascent, &font_descent, &cs);
1250 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
1251 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
1256 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1258 static INLINE void
1259 x_clear_glyph_string_rect (s, x, y, w, h)
1260 struct glyph_string *s;
1261 int x, y, w, h;
1263 XGCValues xgcv;
1264 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1265 XSetForeground (s->display, s->gc, xgcv.background);
1266 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1267 XSetForeground (s->display, s->gc, xgcv.foreground);
1271 /* Draw the background of glyph_string S. If S->background_filled_p
1272 is non-zero don't draw it. FORCE_P non-zero means draw the
1273 background even if it wouldn't be drawn normally. This is used
1274 when a string preceding S draws into the background of S, or S
1275 contains the first component of a composition. */
1277 static void
1278 x_draw_glyph_string_background (s, force_p)
1279 struct glyph_string *s;
1280 int force_p;
1282 /* Nothing to do if background has already been drawn or if it
1283 shouldn't be drawn in the first place. */
1284 if (!s->background_filled_p)
1286 int box_line_width = max (s->face->box_line_width, 0);
1288 if (s->stippled_p)
1290 /* Fill background with a stipple pattern. */
1291 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1292 XFillRectangle (s->display, s->window, s->gc, s->x,
1293 s->y + box_line_width,
1294 s->background_width,
1295 s->height - 2 * box_line_width);
1296 XSetFillStyle (s->display, s->gc, FillSolid);
1297 s->background_filled_p = 1;
1299 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1300 || s->font_not_found_p
1301 || s->extends_to_end_of_line_p
1302 || force_p)
1304 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1305 s->background_width,
1306 s->height - 2 * box_line_width);
1307 s->background_filled_p = 1;
1313 /* Draw the foreground of glyph string S. */
1315 static void
1316 x_draw_glyph_string_foreground (s)
1317 struct glyph_string *s;
1319 int i, x;
1321 /* If first glyph of S has a left box line, start drawing the text
1322 of S to the right of that box line. */
1323 if (s->face->box != FACE_NO_BOX
1324 && s->first_glyph->left_box_line_p)
1325 x = s->x + eabs (s->face->box_line_width);
1326 else
1327 x = s->x;
1329 /* Draw characters of S as rectangles if S's font could not be
1330 loaded. */
1331 if (s->font_not_found_p)
1333 for (i = 0; i < s->nchars; ++i)
1335 struct glyph *g = s->first_glyph + i;
1336 XDrawRectangle (s->display, s->window,
1337 s->gc, x, s->y, g->pixel_width - 1,
1338 s->height - 1);
1339 x += g->pixel_width;
1342 else
1344 char *char1b = (char *) s->char2b;
1345 int boff = s->font_info->baseline_offset;
1347 if (s->font_info->vertical_centering)
1348 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
1350 /* If we can use 8-bit functions, condense S->char2b. */
1351 if (!s->two_byte_p)
1352 for (i = 0; i < s->nchars; ++i)
1353 char1b[i] = s->char2b[i].byte2;
1355 /* Draw text with XDrawString if background has already been
1356 filled. Otherwise, use XDrawImageString. (Note that
1357 XDrawImageString is usually faster than XDrawString.) Always
1358 use XDrawImageString when drawing the cursor so that there is
1359 no chance that characters under a box cursor are invisible. */
1360 if (s->for_overlaps
1361 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1363 /* Draw characters with 16-bit or 8-bit functions. */
1364 if (s->two_byte_p)
1365 XDrawString16 (s->display, s->window, s->gc, x,
1366 s->ybase - boff, s->char2b, s->nchars);
1367 else
1368 XDrawString (s->display, s->window, s->gc, x,
1369 s->ybase - boff, char1b, s->nchars);
1371 else
1373 if (s->two_byte_p)
1374 XDrawImageString16 (s->display, s->window, s->gc, x,
1375 s->ybase - boff, s->char2b, s->nchars);
1376 else
1377 XDrawImageString (s->display, s->window, s->gc, x,
1378 s->ybase - boff, char1b, s->nchars);
1381 if (s->face->overstrike)
1383 /* For overstriking (to simulate bold-face), draw the
1384 characters again shifted to the right by one pixel. */
1385 if (s->two_byte_p)
1386 XDrawString16 (s->display, s->window, s->gc, x + 1,
1387 s->ybase - boff, s->char2b, s->nchars);
1388 else
1389 XDrawString (s->display, s->window, s->gc, x + 1,
1390 s->ybase - boff, char1b, s->nchars);
1395 /* Draw the foreground of composite glyph string S. */
1397 static void
1398 x_draw_composite_glyph_string_foreground (s)
1399 struct glyph_string *s;
1401 int i, x;
1403 /* If first glyph of S has a left box line, start drawing the text
1404 of S to the right of that box line. */
1405 if (s->face->box != FACE_NO_BOX
1406 && s->first_glyph->left_box_line_p)
1407 x = s->x + eabs (s->face->box_line_width);
1408 else
1409 x = s->x;
1411 /* S is a glyph string for a composition. S->gidx is the index of
1412 the first character drawn for glyphs of this composition.
1413 S->gidx == 0 means we are drawing the very first character of
1414 this composition. */
1416 /* Draw a rectangle for the composition if the font for the very
1417 first character of the composition could not be loaded. */
1418 if (s->font_not_found_p)
1420 if (s->gidx == 0)
1421 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1422 s->width - 1, s->height - 1);
1424 else
1426 for (i = 0; i < s->nchars; i++, ++s->gidx)
1428 XDrawString16 (s->display, s->window, s->gc,
1429 x + s->cmp->offsets[s->gidx * 2],
1430 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1431 s->char2b + i, 1);
1432 if (s->face->overstrike)
1433 XDrawString16 (s->display, s->window, s->gc,
1434 x + s->cmp->offsets[s->gidx * 2] + 1,
1435 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1436 s->char2b + i, 1);
1442 #ifdef USE_X_TOOLKIT
1444 static struct frame *x_frame_of_widget P_ ((Widget));
1445 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1446 XrmValue *, XrmValue *, XtPointer *));
1447 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1448 XrmValue *, Cardinal *));
1451 /* Return the frame on which widget WIDGET is used.. Abort if frame
1452 cannot be determined. */
1454 static struct frame *
1455 x_frame_of_widget (widget)
1456 Widget widget;
1458 struct x_display_info *dpyinfo;
1459 Lisp_Object tail;
1460 struct frame *f;
1462 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1464 /* Find the top-level shell of the widget. Note that this function
1465 can be called when the widget is not yet realized, so XtWindow
1466 (widget) == 0. That's the reason we can't simply use
1467 x_any_window_to_frame. */
1468 while (!XtIsTopLevelShell (widget))
1469 widget = XtParent (widget);
1471 /* Look for a frame with that top-level widget. Allocate the color
1472 on that frame to get the right gamma correction value. */
1473 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1474 if (GC_FRAMEP (XCAR (tail))
1475 && (f = XFRAME (XCAR (tail)),
1476 (FRAME_X_P (f)
1477 && f->output_data.nothing != 1
1478 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1479 && f->output_data.x->widget == widget)
1480 return f;
1482 abort ();
1486 /* Allocate the color COLOR->pixel on the screen and display of
1487 widget WIDGET in colormap CMAP. If an exact match cannot be
1488 allocated, try the nearest color available. Value is non-zero
1489 if successful. This is called from lwlib. */
1492 x_alloc_nearest_color_for_widget (widget, cmap, color)
1493 Widget widget;
1494 Colormap cmap;
1495 XColor *color;
1497 struct frame *f = x_frame_of_widget (widget);
1498 return x_alloc_nearest_color (f, cmap, color);
1502 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1503 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1504 If this produces the same color as PIXEL, try a color where all RGB
1505 values have DELTA added. Return the allocated color in *PIXEL.
1506 DISPLAY is the X display, CMAP is the colormap to operate on.
1507 Value is non-zero if successful. */
1510 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1511 Widget widget;
1512 Display *display;
1513 Colormap cmap;
1514 unsigned long *pixel;
1515 double factor;
1516 int delta;
1518 struct frame *f = x_frame_of_widget (widget);
1519 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1523 /* Structure specifying which arguments should be passed by Xt to
1524 cvt_string_to_pixel. We want the widget's screen and colormap. */
1526 static XtConvertArgRec cvt_string_to_pixel_args[] =
1528 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1529 sizeof (Screen *)},
1530 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1531 sizeof (Colormap)}
1535 /* The address of this variable is returned by
1536 cvt_string_to_pixel. */
1538 static Pixel cvt_string_to_pixel_value;
1541 /* Convert a color name to a pixel color.
1543 DPY is the display we are working on.
1545 ARGS is an array of *NARGS XrmValue structures holding additional
1546 information about the widget for which the conversion takes place.
1547 The contents of this array are determined by the specification
1548 in cvt_string_to_pixel_args.
1550 FROM is a pointer to an XrmValue which points to the color name to
1551 convert. TO is an XrmValue in which to return the pixel color.
1553 CLOSURE_RET is a pointer to user-data, in which we record if
1554 we allocated the color or not.
1556 Value is True if successful, False otherwise. */
1558 static Boolean
1559 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1560 Display *dpy;
1561 XrmValue *args;
1562 Cardinal *nargs;
1563 XrmValue *from, *to;
1564 XtPointer *closure_ret;
1566 Screen *screen;
1567 Colormap cmap;
1568 Pixel pixel;
1569 String color_name;
1570 XColor color;
1572 if (*nargs != 2)
1574 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1575 "wrongParameters", "cvt_string_to_pixel",
1576 "XtToolkitError",
1577 "Screen and colormap args required", NULL, NULL);
1578 return False;
1581 screen = *(Screen **) args[0].addr;
1582 cmap = *(Colormap *) args[1].addr;
1583 color_name = (String) from->addr;
1585 if (strcmp (color_name, XtDefaultBackground) == 0)
1587 *closure_ret = (XtPointer) False;
1588 pixel = WhitePixelOfScreen (screen);
1590 else if (strcmp (color_name, XtDefaultForeground) == 0)
1592 *closure_ret = (XtPointer) False;
1593 pixel = BlackPixelOfScreen (screen);
1595 else if (XParseColor (dpy, cmap, color_name, &color)
1596 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1598 pixel = color.pixel;
1599 *closure_ret = (XtPointer) True;
1601 else
1603 String params[1];
1604 Cardinal nparams = 1;
1606 params[0] = color_name;
1607 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1608 "badValue", "cvt_string_to_pixel",
1609 "XtToolkitError", "Invalid color `%s'",
1610 params, &nparams);
1611 return False;
1614 if (to->addr != NULL)
1616 if (to->size < sizeof (Pixel))
1618 to->size = sizeof (Pixel);
1619 return False;
1622 *(Pixel *) to->addr = pixel;
1624 else
1626 cvt_string_to_pixel_value = pixel;
1627 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1630 to->size = sizeof (Pixel);
1631 return True;
1635 /* Free a pixel color which was previously allocated via
1636 cvt_string_to_pixel. This is registered as the destructor
1637 for this type of resource via XtSetTypeConverter.
1639 APP is the application context in which we work.
1641 TO is a pointer to an XrmValue holding the color to free.
1642 CLOSURE is the value we stored in CLOSURE_RET for this color
1643 in cvt_string_to_pixel.
1645 ARGS and NARGS are like for cvt_string_to_pixel. */
1647 static void
1648 cvt_pixel_dtor (app, to, closure, args, nargs)
1649 XtAppContext app;
1650 XrmValuePtr to;
1651 XtPointer closure;
1652 XrmValuePtr args;
1653 Cardinal *nargs;
1655 if (*nargs != 2)
1657 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1658 "XtToolkitError",
1659 "Screen and colormap arguments required",
1660 NULL, NULL);
1662 else if (closure != NULL)
1664 /* We did allocate the pixel, so free it. */
1665 Screen *screen = *(Screen **) args[0].addr;
1666 Colormap cmap = *(Colormap *) args[1].addr;
1667 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1668 (Pixel *) to->addr, 1);
1673 #endif /* USE_X_TOOLKIT */
1676 /* Value is an array of XColor structures for the contents of the
1677 color map of display DPY. Set *NCELLS to the size of the array.
1678 Note that this probably shouldn't be called for large color maps,
1679 say a 24-bit TrueColor map. */
1681 static const XColor *
1682 x_color_cells (dpy, ncells)
1683 Display *dpy;
1684 int *ncells;
1686 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1688 if (dpyinfo->color_cells == NULL)
1690 Screen *screen = dpyinfo->screen;
1691 int i;
1693 dpyinfo->ncolor_cells
1694 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1695 dpyinfo->color_cells
1696 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1697 * sizeof *dpyinfo->color_cells);
1699 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1700 dpyinfo->color_cells[i].pixel = i;
1702 XQueryColors (dpy, dpyinfo->cmap,
1703 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1706 *ncells = dpyinfo->ncolor_cells;
1707 return dpyinfo->color_cells;
1711 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1712 colors in COLORS. Use cached information, if available. */
1714 void
1715 x_query_colors (f, colors, ncolors)
1716 struct frame *f;
1717 XColor *colors;
1718 int ncolors;
1720 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1722 if (dpyinfo->color_cells)
1724 int i;
1725 for (i = 0; i < ncolors; ++i)
1727 unsigned long pixel = colors[i].pixel;
1728 xassert (pixel < dpyinfo->ncolor_cells);
1729 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1730 colors[i] = dpyinfo->color_cells[pixel];
1733 else
1734 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1738 /* On frame F, translate pixel color to RGB values for the color in
1739 COLOR. Use cached information, if available. */
1741 void
1742 x_query_color (f, color)
1743 struct frame *f;
1744 XColor *color;
1746 x_query_colors (f, color, 1);
1750 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1751 exact match can't be allocated, try the nearest color available.
1752 Value is non-zero if successful. Set *COLOR to the color
1753 allocated. */
1755 static int
1756 x_alloc_nearest_color_1 (dpy, cmap, color)
1757 Display *dpy;
1758 Colormap cmap;
1759 XColor *color;
1761 int rc;
1763 rc = XAllocColor (dpy, cmap, color);
1764 if (rc == 0)
1766 /* If we got to this point, the colormap is full, so we're going
1767 to try to get the next closest color. The algorithm used is
1768 a least-squares matching, which is what X uses for closest
1769 color matching with StaticColor visuals. */
1770 int nearest, i;
1771 unsigned long nearest_delta = ~0;
1772 int ncells;
1773 const XColor *cells = x_color_cells (dpy, &ncells);
1775 for (nearest = i = 0; i < ncells; ++i)
1777 long dred = (color->red >> 8) - (cells[i].red >> 8);
1778 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1779 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1780 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1782 if (delta < nearest_delta)
1784 nearest = i;
1785 nearest_delta = delta;
1789 color->red = cells[nearest].red;
1790 color->green = cells[nearest].green;
1791 color->blue = cells[nearest].blue;
1792 rc = XAllocColor (dpy, cmap, color);
1794 else
1796 /* If allocation succeeded, and the allocated pixel color is not
1797 equal to a cached pixel color recorded earlier, there was a
1798 change in the colormap, so clear the color cache. */
1799 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1800 XColor *cached_color;
1802 if (dpyinfo->color_cells
1803 && (cached_color = &dpyinfo->color_cells[color->pixel],
1804 (cached_color->red != color->red
1805 || cached_color->blue != color->blue
1806 || cached_color->green != color->green)))
1808 xfree (dpyinfo->color_cells);
1809 dpyinfo->color_cells = NULL;
1810 dpyinfo->ncolor_cells = 0;
1814 #ifdef DEBUG_X_COLORS
1815 if (rc)
1816 register_color (color->pixel);
1817 #endif /* DEBUG_X_COLORS */
1819 return rc;
1823 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1824 exact match can't be allocated, try the nearest color available.
1825 Value is non-zero if successful. Set *COLOR to the color
1826 allocated. */
1829 x_alloc_nearest_color (f, cmap, color)
1830 struct frame *f;
1831 Colormap cmap;
1832 XColor *color;
1834 gamma_correct (f, color);
1835 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1839 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1840 It's necessary to do this instead of just using PIXEL directly to
1841 get color reference counts right. */
1843 unsigned long
1844 x_copy_color (f, pixel)
1845 struct frame *f;
1846 unsigned long pixel;
1848 XColor color;
1850 color.pixel = pixel;
1851 BLOCK_INPUT;
1852 x_query_color (f, &color);
1853 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1854 UNBLOCK_INPUT;
1855 #ifdef DEBUG_X_COLORS
1856 register_color (pixel);
1857 #endif
1858 return color.pixel;
1862 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
1863 It's necessary to do this instead of just using PIXEL directly to
1864 get color reference counts right. */
1866 unsigned long
1867 x_copy_dpy_color (dpy, cmap, pixel)
1868 Display *dpy;
1869 Colormap cmap;
1870 unsigned long pixel;
1872 XColor color;
1874 color.pixel = pixel;
1875 BLOCK_INPUT;
1876 XQueryColor (dpy, cmap, &color);
1877 XAllocColor (dpy, cmap, &color);
1878 UNBLOCK_INPUT;
1879 #ifdef DEBUG_X_COLORS
1880 register_color (pixel);
1881 #endif
1882 return color.pixel;
1886 /* Brightness beyond which a color won't have its highlight brightness
1887 boosted.
1889 Nominally, highlight colors for `3d' faces are calculated by
1890 brightening an object's color by a constant scale factor, but this
1891 doesn't yield good results for dark colors, so for colors who's
1892 brightness is less than this value (on a scale of 0-65535) have an
1893 use an additional additive factor.
1895 The value here is set so that the default menu-bar/mode-line color
1896 (grey75) will not have its highlights changed at all. */
1897 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1900 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1901 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1902 If this produces the same color as PIXEL, try a color where all RGB
1903 values have DELTA added. Return the allocated color in *PIXEL.
1904 DISPLAY is the X display, CMAP is the colormap to operate on.
1905 Value is non-zero if successful. */
1907 static int
1908 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
1909 struct frame *f;
1910 Display *display;
1911 Colormap cmap;
1912 unsigned long *pixel;
1913 double factor;
1914 int delta;
1916 XColor color, new;
1917 long bright;
1918 int success_p;
1920 /* Get RGB color values. */
1921 color.pixel = *pixel;
1922 x_query_color (f, &color);
1924 /* Change RGB values by specified FACTOR. Avoid overflow! */
1925 xassert (factor >= 0);
1926 new.red = min (0xffff, factor * color.red);
1927 new.green = min (0xffff, factor * color.green);
1928 new.blue = min (0xffff, factor * color.blue);
1930 /* Calculate brightness of COLOR. */
1931 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1933 /* We only boost colors that are darker than
1934 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1935 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1936 /* Make an additive adjustment to NEW, because it's dark enough so
1937 that scaling by FACTOR alone isn't enough. */
1939 /* How far below the limit this color is (0 - 1, 1 being darker). */
1940 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1941 /* The additive adjustment. */
1942 int min_delta = delta * dimness * factor / 2;
1944 if (factor < 1)
1946 new.red = max (0, new.red - min_delta);
1947 new.green = max (0, new.green - min_delta);
1948 new.blue = max (0, new.blue - min_delta);
1950 else
1952 new.red = min (0xffff, min_delta + new.red);
1953 new.green = min (0xffff, min_delta + new.green);
1954 new.blue = min (0xffff, min_delta + new.blue);
1958 /* Try to allocate the color. */
1959 success_p = x_alloc_nearest_color (f, cmap, &new);
1960 if (success_p)
1962 if (new.pixel == *pixel)
1964 /* If we end up with the same color as before, try adding
1965 delta to the RGB values. */
1966 x_free_colors (f, &new.pixel, 1);
1968 new.red = min (0xffff, delta + color.red);
1969 new.green = min (0xffff, delta + color.green);
1970 new.blue = min (0xffff, delta + color.blue);
1971 success_p = x_alloc_nearest_color (f, cmap, &new);
1973 else
1974 success_p = 1;
1975 *pixel = new.pixel;
1978 return success_p;
1982 /* Set up the foreground color for drawing relief lines of glyph
1983 string S. RELIEF is a pointer to a struct relief containing the GC
1984 with which lines will be drawn. Use a color that is FACTOR or
1985 DELTA lighter or darker than the relief's background which is found
1986 in S->f->output_data.x->relief_background. If such a color cannot
1987 be allocated, use DEFAULT_PIXEL, instead. */
1989 static void
1990 x_setup_relief_color (f, relief, factor, delta, default_pixel)
1991 struct frame *f;
1992 struct relief *relief;
1993 double factor;
1994 int delta;
1995 unsigned long default_pixel;
1997 XGCValues xgcv;
1998 struct x_output *di = f->output_data.x;
1999 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
2000 unsigned long pixel;
2001 unsigned long background = di->relief_background;
2002 Colormap cmap = FRAME_X_COLORMAP (f);
2003 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2004 Display *dpy = FRAME_X_DISPLAY (f);
2006 xgcv.graphics_exposures = False;
2007 xgcv.line_width = 1;
2009 /* Free previously allocated color. The color cell will be reused
2010 when it has been freed as many times as it was allocated, so this
2011 doesn't affect faces using the same colors. */
2012 if (relief->gc
2013 && relief->allocated_p)
2015 x_free_colors (f, &relief->pixel, 1);
2016 relief->allocated_p = 0;
2019 /* Allocate new color. */
2020 xgcv.foreground = default_pixel;
2021 pixel = background;
2022 if (dpyinfo->n_planes != 1
2023 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
2025 relief->allocated_p = 1;
2026 xgcv.foreground = relief->pixel = pixel;
2029 if (relief->gc == 0)
2031 xgcv.stipple = dpyinfo->gray;
2032 mask |= GCStipple;
2033 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
2035 else
2036 XChangeGC (dpy, relief->gc, mask, &xgcv);
2040 /* Set up colors for the relief lines around glyph string S. */
2042 static void
2043 x_setup_relief_colors (s)
2044 struct glyph_string *s;
2046 struct x_output *di = s->f->output_data.x;
2047 unsigned long color;
2049 if (s->face->use_box_color_for_shadows_p)
2050 color = s->face->box_color;
2051 else if (s->first_glyph->type == IMAGE_GLYPH
2052 && s->img->pixmap
2053 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2054 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2055 else
2057 XGCValues xgcv;
2059 /* Get the background color of the face. */
2060 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2061 color = xgcv.background;
2064 if (di->white_relief.gc == 0
2065 || color != di->relief_background)
2067 di->relief_background = color;
2068 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2069 WHITE_PIX_DEFAULT (s->f));
2070 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2071 BLACK_PIX_DEFAULT (s->f));
2076 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2077 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2078 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
2079 relief. LEFT_P non-zero means draw a relief on the left side of
2080 the rectangle. RIGHT_P non-zero means draw a relief on the right
2081 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2082 when drawing. */
2084 static void
2085 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
2086 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
2087 struct frame *f;
2088 int left_x, top_y, right_x, bottom_y, width;
2089 int top_p, bot_p, left_p, right_p, raised_p;
2090 XRectangle *clip_rect;
2092 Display *dpy = FRAME_X_DISPLAY (f);
2093 Window window = FRAME_X_WINDOW (f);
2094 int i;
2095 GC gc;
2097 if (raised_p)
2098 gc = f->output_data.x->white_relief.gc;
2099 else
2100 gc = f->output_data.x->black_relief.gc;
2101 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2103 /* Top. */
2104 if (top_p)
2105 for (i = 0; i < width; ++i)
2106 XDrawLine (dpy, window, gc,
2107 left_x + i * left_p, top_y + i,
2108 right_x + 1 - i * right_p, top_y + i);
2110 /* Left. */
2111 if (left_p)
2112 for (i = 0; i < width; ++i)
2113 XDrawLine (dpy, window, gc,
2114 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
2116 XSetClipMask (dpy, gc, None);
2117 if (raised_p)
2118 gc = f->output_data.x->black_relief.gc;
2119 else
2120 gc = f->output_data.x->white_relief.gc;
2121 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2123 /* Bottom. */
2124 if (bot_p)
2125 for (i = 0; i < width; ++i)
2126 XDrawLine (dpy, window, gc,
2127 left_x + i * left_p, bottom_y - i,
2128 right_x + 1 - i * right_p, bottom_y - i);
2130 /* Right. */
2131 if (right_p)
2132 for (i = 0; i < width; ++i)
2133 XDrawLine (dpy, window, gc,
2134 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2136 XSetClipMask (dpy, gc, None);
2140 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2141 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2142 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2143 left side of the rectangle. RIGHT_P non-zero means draw a line
2144 on the right side of the rectangle. CLIP_RECT is the clipping
2145 rectangle to use when drawing. */
2147 static void
2148 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2149 left_p, right_p, clip_rect)
2150 struct glyph_string *s;
2151 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
2152 XRectangle *clip_rect;
2154 XGCValues xgcv;
2156 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2157 XSetForeground (s->display, s->gc, s->face->box_color);
2158 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2160 /* Top. */
2161 XFillRectangle (s->display, s->window, s->gc,
2162 left_x, top_y, right_x - left_x + 1, width);
2164 /* Left. */
2165 if (left_p)
2166 XFillRectangle (s->display, s->window, s->gc,
2167 left_x, top_y, width, bottom_y - top_y + 1);
2169 /* Bottom. */
2170 XFillRectangle (s->display, s->window, s->gc,
2171 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2173 /* Right. */
2174 if (right_p)
2175 XFillRectangle (s->display, s->window, s->gc,
2176 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2178 XSetForeground (s->display, s->gc, xgcv.foreground);
2179 XSetClipMask (s->display, s->gc, None);
2183 /* Draw a box around glyph string S. */
2185 static void
2186 x_draw_glyph_string_box (s)
2187 struct glyph_string *s;
2189 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2190 int left_p, right_p;
2191 struct glyph *last_glyph;
2192 XRectangle clip_rect;
2194 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2195 ? WINDOW_RIGHT_EDGE_X (s->w)
2196 : window_box_right (s->w, s->area));
2198 /* The glyph that may have a right box line. */
2199 last_glyph = (s->cmp || s->img
2200 ? s->first_glyph
2201 : s->first_glyph + s->nchars - 1);
2203 width = eabs (s->face->box_line_width);
2204 raised_p = s->face->box == FACE_RAISED_BOX;
2205 left_x = s->x;
2206 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2207 ? last_x - 1
2208 : min (last_x, s->x + s->background_width) - 1);
2209 top_y = s->y;
2210 bottom_y = top_y + s->height - 1;
2212 left_p = (s->first_glyph->left_box_line_p
2213 || (s->hl == DRAW_MOUSE_FACE
2214 && (s->prev == NULL
2215 || s->prev->hl != s->hl)));
2216 right_p = (last_glyph->right_box_line_p
2217 || (s->hl == DRAW_MOUSE_FACE
2218 && (s->next == NULL
2219 || s->next->hl != s->hl)));
2221 get_glyph_string_clip_rect (s, &clip_rect);
2223 if (s->face->box == FACE_SIMPLE_BOX)
2224 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2225 left_p, right_p, &clip_rect);
2226 else
2228 x_setup_relief_colors (s);
2229 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2230 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2235 /* Draw foreground of image glyph string S. */
2237 static void
2238 x_draw_image_foreground (s)
2239 struct glyph_string *s;
2241 int x = s->x;
2242 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2244 /* If first glyph of S has a left box line, start drawing it to the
2245 right of that line. */
2246 if (s->face->box != FACE_NO_BOX
2247 && s->first_glyph->left_box_line_p
2248 && s->slice.x == 0)
2249 x += eabs (s->face->box_line_width);
2251 /* If there is a margin around the image, adjust x- and y-position
2252 by that margin. */
2253 if (s->slice.x == 0)
2254 x += s->img->hmargin;
2255 if (s->slice.y == 0)
2256 y += s->img->vmargin;
2258 if (s->img->pixmap)
2260 if (s->img->mask)
2262 /* We can't set both a clip mask and use XSetClipRectangles
2263 because the latter also sets a clip mask. We also can't
2264 trust on the shape extension to be available
2265 (XShapeCombineRegion). So, compute the rectangle to draw
2266 manually. */
2267 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2268 | GCFunction);
2269 XGCValues xgcv;
2270 XRectangle clip_rect, image_rect, r;
2272 xgcv.clip_mask = s->img->mask;
2273 xgcv.clip_x_origin = x;
2274 xgcv.clip_y_origin = y;
2275 xgcv.function = GXcopy;
2276 XChangeGC (s->display, s->gc, mask, &xgcv);
2278 get_glyph_string_clip_rect (s, &clip_rect);
2279 image_rect.x = x;
2280 image_rect.y = y;
2281 image_rect.width = s->slice.width;
2282 image_rect.height = s->slice.height;
2283 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2284 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2285 s->slice.x + r.x - x, s->slice.y + r.y - y,
2286 r.width, r.height, r.x, r.y);
2288 else
2290 XRectangle clip_rect, image_rect, r;
2292 get_glyph_string_clip_rect (s, &clip_rect);
2293 image_rect.x = x;
2294 image_rect.y = y;
2295 image_rect.width = s->slice.width;
2296 image_rect.height = s->slice.height;
2297 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2298 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2299 s->slice.x + r.x - x, s->slice.y + r.y - y,
2300 r.width, r.height, r.x, r.y);
2302 /* When the image has a mask, we can expect that at
2303 least part of a mouse highlight or a block cursor will
2304 be visible. If the image doesn't have a mask, make
2305 a block cursor visible by drawing a rectangle around
2306 the image. I believe it's looking better if we do
2307 nothing here for mouse-face. */
2308 if (s->hl == DRAW_CURSOR)
2310 int r = s->img->relief;
2311 if (r < 0) r = -r;
2312 XDrawRectangle (s->display, s->window, s->gc,
2313 x - r, y - r,
2314 s->slice.width + r*2 - 1,
2315 s->slice.height + r*2 - 1);
2319 else
2320 /* Draw a rectangle if image could not be loaded. */
2321 XDrawRectangle (s->display, s->window, s->gc, x, y,
2322 s->slice.width - 1, s->slice.height - 1);
2326 /* Draw a relief around the image glyph string S. */
2328 static void
2329 x_draw_image_relief (s)
2330 struct glyph_string *s;
2332 int x0, y0, x1, y1, thick, raised_p;
2333 XRectangle r;
2334 int x = s->x;
2335 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2337 /* If first glyph of S has a left box line, start drawing it to the
2338 right of that line. */
2339 if (s->face->box != FACE_NO_BOX
2340 && s->first_glyph->left_box_line_p
2341 && s->slice.x == 0)
2342 x += eabs (s->face->box_line_width);
2344 /* If there is a margin around the image, adjust x- and y-position
2345 by that margin. */
2346 if (s->slice.x == 0)
2347 x += s->img->hmargin;
2348 if (s->slice.y == 0)
2349 y += s->img->vmargin;
2351 if (s->hl == DRAW_IMAGE_SUNKEN
2352 || s->hl == DRAW_IMAGE_RAISED)
2354 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2355 raised_p = s->hl == DRAW_IMAGE_RAISED;
2357 else
2359 thick = eabs (s->img->relief);
2360 raised_p = s->img->relief > 0;
2363 x0 = x - thick;
2364 y0 = y - thick;
2365 x1 = x + s->slice.width + thick - 1;
2366 y1 = y + s->slice.height + thick - 1;
2368 x_setup_relief_colors (s);
2369 get_glyph_string_clip_rect (s, &r);
2370 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
2371 s->slice.y == 0,
2372 s->slice.y + s->slice.height == s->img->height,
2373 s->slice.x == 0,
2374 s->slice.x + s->slice.width == s->img->width,
2375 &r);
2379 /* Draw the foreground of image glyph string S to PIXMAP. */
2381 static void
2382 x_draw_image_foreground_1 (s, pixmap)
2383 struct glyph_string *s;
2384 Pixmap pixmap;
2386 int x = 0;
2387 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2389 /* If first glyph of S has a left box line, start drawing it to the
2390 right of that line. */
2391 if (s->face->box != FACE_NO_BOX
2392 && s->first_glyph->left_box_line_p
2393 && s->slice.x == 0)
2394 x += eabs (s->face->box_line_width);
2396 /* If there is a margin around the image, adjust x- and y-position
2397 by that margin. */
2398 if (s->slice.x == 0)
2399 x += s->img->hmargin;
2400 if (s->slice.y == 0)
2401 y += s->img->vmargin;
2403 if (s->img->pixmap)
2405 if (s->img->mask)
2407 /* We can't set both a clip mask and use XSetClipRectangles
2408 because the latter also sets a clip mask. We also can't
2409 trust on the shape extension to be available
2410 (XShapeCombineRegion). So, compute the rectangle to draw
2411 manually. */
2412 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2413 | GCFunction);
2414 XGCValues xgcv;
2416 xgcv.clip_mask = s->img->mask;
2417 xgcv.clip_x_origin = x - s->slice.x;
2418 xgcv.clip_y_origin = y - s->slice.y;
2419 xgcv.function = GXcopy;
2420 XChangeGC (s->display, s->gc, mask, &xgcv);
2422 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2423 s->slice.x, s->slice.y,
2424 s->slice.width, s->slice.height, x, y);
2425 XSetClipMask (s->display, s->gc, None);
2427 else
2429 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2430 s->slice.x, s->slice.y,
2431 s->slice.width, s->slice.height, x, y);
2433 /* When the image has a mask, we can expect that at
2434 least part of a mouse highlight or a block cursor will
2435 be visible. If the image doesn't have a mask, make
2436 a block cursor visible by drawing a rectangle around
2437 the image. I believe it's looking better if we do
2438 nothing here for mouse-face. */
2439 if (s->hl == DRAW_CURSOR)
2441 int r = s->img->relief;
2442 if (r < 0) r = -r;
2443 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2444 s->slice.width + r*2 - 1,
2445 s->slice.height + r*2 - 1);
2449 else
2450 /* Draw a rectangle if image could not be loaded. */
2451 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2452 s->slice.width - 1, s->slice.height - 1);
2456 /* Draw part of the background of glyph string S. X, Y, W, and H
2457 give the rectangle to draw. */
2459 static void
2460 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2461 struct glyph_string *s;
2462 int x, y, w, h;
2464 if (s->stippled_p)
2466 /* Fill background with a stipple pattern. */
2467 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2468 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2469 XSetFillStyle (s->display, s->gc, FillSolid);
2471 else
2472 x_clear_glyph_string_rect (s, x, y, w, h);
2476 /* Draw image glyph string S.
2478 s->y
2479 s->x +-------------------------
2480 | s->face->box
2482 | +-------------------------
2483 | | s->img->margin
2485 | | +-------------------
2486 | | | the image
2490 static void
2491 x_draw_image_glyph_string (s)
2492 struct glyph_string *s;
2494 int box_line_hwidth = eabs (s->face->box_line_width);
2495 int box_line_vwidth = max (s->face->box_line_width, 0);
2496 int height;
2497 Pixmap pixmap = None;
2499 height = s->height;
2500 if (s->slice.y == 0)
2501 height -= box_line_vwidth;
2502 if (s->slice.y + s->slice.height >= s->img->height)
2503 height -= box_line_vwidth;
2505 /* Fill background with face under the image. Do it only if row is
2506 taller than image or if image has a clip mask to reduce
2507 flickering. */
2508 s->stippled_p = s->face->stipple != 0;
2509 if (height > s->slice.height
2510 || s->img->hmargin
2511 || s->img->vmargin
2512 || s->img->mask
2513 || s->img->pixmap == 0
2514 || s->width != s->background_width)
2516 if (s->img->mask)
2518 /* Create a pixmap as large as the glyph string. Fill it
2519 with the background color. Copy the image to it, using
2520 its mask. Copy the temporary pixmap to the display. */
2521 Screen *screen = FRAME_X_SCREEN (s->f);
2522 int depth = DefaultDepthOfScreen (screen);
2524 /* Create a pixmap as large as the glyph string. */
2525 pixmap = XCreatePixmap (s->display, s->window,
2526 s->background_width,
2527 s->height, depth);
2529 /* Don't clip in the following because we're working on the
2530 pixmap. */
2531 XSetClipMask (s->display, s->gc, None);
2533 /* Fill the pixmap with the background color/stipple. */
2534 if (s->stippled_p)
2536 /* Fill background with a stipple pattern. */
2537 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2538 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
2539 XFillRectangle (s->display, pixmap, s->gc,
2540 0, 0, s->background_width, s->height);
2541 XSetFillStyle (s->display, s->gc, FillSolid);
2542 XSetTSOrigin (s->display, s->gc, 0, 0);
2544 else
2546 XGCValues xgcv;
2547 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2548 &xgcv);
2549 XSetForeground (s->display, s->gc, xgcv.background);
2550 XFillRectangle (s->display, pixmap, s->gc,
2551 0, 0, s->background_width, s->height);
2552 XSetForeground (s->display, s->gc, xgcv.foreground);
2555 else
2557 int x = s->x;
2558 int y = s->y;
2560 if (s->first_glyph->left_box_line_p
2561 && s->slice.x == 0)
2562 x += box_line_hwidth;
2564 if (s->slice.y == 0)
2565 y += box_line_vwidth;
2567 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2570 s->background_filled_p = 1;
2573 /* Draw the foreground. */
2574 if (pixmap != None)
2576 x_draw_image_foreground_1 (s, pixmap);
2577 x_set_glyph_string_clipping (s);
2578 XCopyArea (s->display, pixmap, s->window, s->gc,
2579 0, 0, s->background_width, s->height, s->x, s->y);
2580 XFreePixmap (s->display, pixmap);
2582 else
2583 x_draw_image_foreground (s);
2585 /* If we must draw a relief around the image, do it. */
2586 if (s->img->relief
2587 || s->hl == DRAW_IMAGE_RAISED
2588 || s->hl == DRAW_IMAGE_SUNKEN)
2589 x_draw_image_relief (s);
2593 /* Draw stretch glyph string S. */
2595 static void
2596 x_draw_stretch_glyph_string (s)
2597 struct glyph_string *s;
2599 xassert (s->first_glyph->type == STRETCH_GLYPH);
2601 if (s->hl == DRAW_CURSOR
2602 && !x_stretch_cursor_p)
2604 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2605 as wide as the stretch glyph. */
2606 int width, background_width = s->background_width;
2607 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2609 if (x < left_x)
2611 background_width -= left_x - x;
2612 x = left_x;
2614 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2616 /* Draw cursor. */
2617 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2619 /* Clear rest using the GC of the original non-cursor face. */
2620 if (width < background_width)
2622 int y = s->y;
2623 int w = background_width - width, h = s->height;
2624 XRectangle r;
2625 GC gc;
2627 x += width;
2628 if (s->row->mouse_face_p
2629 && cursor_in_mouse_face_p (s->w))
2631 x_set_mouse_face_gc (s);
2632 gc = s->gc;
2634 else
2635 gc = s->face->gc;
2637 get_glyph_string_clip_rect (s, &r);
2638 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2640 if (s->face->stipple)
2642 /* Fill background with a stipple pattern. */
2643 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2644 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2645 XSetFillStyle (s->display, gc, FillSolid);
2647 else
2649 XGCValues xgcv;
2650 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2651 XSetForeground (s->display, gc, xgcv.background);
2652 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2653 XSetForeground (s->display, gc, xgcv.foreground);
2657 else if (!s->background_filled_p)
2659 int background_width = s->background_width;
2660 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2662 /* Don't draw into left margin, fringe or scrollbar area
2663 except for header line and mode line. */
2664 if (x < left_x && !s->row->mode_line_p)
2666 background_width -= left_x - x;
2667 x = left_x;
2669 if (background_width > 0)
2670 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2673 s->background_filled_p = 1;
2677 /* Draw glyph string S. */
2679 static void
2680 x_draw_glyph_string (s)
2681 struct glyph_string *s;
2683 int relief_drawn_p = 0;
2685 /* If S draws into the background of its successor, draw the
2686 background of the successor first so that S can draw into it.
2687 This makes S->next use XDrawString instead of XDrawImageString. */
2688 if (s->next && s->right_overhang && !s->for_overlaps)
2690 xassert (s->next->img == NULL);
2691 x_set_glyph_string_gc (s->next);
2692 x_set_glyph_string_clipping (s->next);
2693 x_draw_glyph_string_background (s->next, 1);
2696 /* Set up S->gc, set clipping and draw S. */
2697 x_set_glyph_string_gc (s);
2699 /* Draw relief (if any) in advance for char/composition so that the
2700 glyph string can be drawn over it. */
2701 if (!s->for_overlaps
2702 && s->face->box != FACE_NO_BOX
2703 && (s->first_glyph->type == CHAR_GLYPH
2704 || s->first_glyph->type == COMPOSITE_GLYPH))
2707 x_set_glyph_string_clipping (s);
2708 x_draw_glyph_string_background (s, 1);
2709 x_draw_glyph_string_box (s);
2710 x_set_glyph_string_clipping (s);
2711 relief_drawn_p = 1;
2713 else
2714 x_set_glyph_string_clipping (s);
2716 switch (s->first_glyph->type)
2718 case IMAGE_GLYPH:
2719 x_draw_image_glyph_string (s);
2720 break;
2722 case STRETCH_GLYPH:
2723 x_draw_stretch_glyph_string (s);
2724 break;
2726 case CHAR_GLYPH:
2727 if (s->for_overlaps)
2728 s->background_filled_p = 1;
2729 else
2730 x_draw_glyph_string_background (s, 0);
2731 x_draw_glyph_string_foreground (s);
2732 break;
2734 case COMPOSITE_GLYPH:
2735 if (s->for_overlaps || s->gidx > 0)
2736 s->background_filled_p = 1;
2737 else
2738 x_draw_glyph_string_background (s, 1);
2739 x_draw_composite_glyph_string_foreground (s);
2740 break;
2742 default:
2743 abort ();
2746 if (!s->for_overlaps)
2748 /* Draw underline. */
2749 if (s->face->underline_p)
2751 unsigned long tem, h;
2752 int y;
2754 /* Get the underline thickness. Default is 1 pixel. */
2755 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
2756 h = 1;
2758 y = s->y + s->height - h;
2759 if (!x_underline_at_descent_line)
2761 /* Get the underline position. This is the recommended
2762 vertical offset in pixels from the baseline to the top of
2763 the underline. This is a signed value according to the
2764 specs, and its default is
2766 ROUND ((maximum descent) / 2), with
2767 ROUND(x) = floor (x + 0.5) */
2769 if (x_use_underline_position_properties
2770 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
2771 y = s->ybase + (long) tem;
2772 else if (s->face->font)
2773 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
2776 if (s->face->underline_defaulted_p)
2777 XFillRectangle (s->display, s->window, s->gc,
2778 s->x, y, s->background_width, h);
2779 else
2781 XGCValues xgcv;
2782 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2783 XSetForeground (s->display, s->gc, s->face->underline_color);
2784 XFillRectangle (s->display, s->window, s->gc,
2785 s->x, y, s->background_width, h);
2786 XSetForeground (s->display, s->gc, xgcv.foreground);
2790 /* Draw overline. */
2791 if (s->face->overline_p)
2793 unsigned long dy = 0, h = 1;
2795 if (s->face->overline_color_defaulted_p)
2796 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2797 s->background_width, h);
2798 else
2800 XGCValues xgcv;
2801 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2802 XSetForeground (s->display, s->gc, s->face->overline_color);
2803 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2804 s->background_width, h);
2805 XSetForeground (s->display, s->gc, xgcv.foreground);
2809 /* Draw strike-through. */
2810 if (s->face->strike_through_p)
2812 unsigned long h = 1;
2813 unsigned long dy = (s->height - h) / 2;
2815 if (s->face->strike_through_color_defaulted_p)
2816 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2817 s->width, h);
2818 else
2820 XGCValues xgcv;
2821 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2822 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2823 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2824 s->width, h);
2825 XSetForeground (s->display, s->gc, xgcv.foreground);
2829 /* Draw relief if not yet drawn. */
2830 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2831 x_draw_glyph_string_box (s);
2834 /* Reset clipping. */
2835 XSetClipMask (s->display, s->gc, None);
2838 /* Shift display to make room for inserted glyphs. */
2840 void
2841 x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2842 struct frame *f;
2843 int x, y, width, height, shift_by;
2845 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2846 f->output_data.x->normal_gc,
2847 x, y, width, height,
2848 x + shift_by, y);
2851 /* Delete N glyphs at the nominal cursor position. Not implemented
2852 for X frames. */
2854 static void
2855 x_delete_glyphs (f, n)
2856 struct frame *f;
2857 register int n;
2859 abort ();
2863 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2864 If they are <= 0, this is probably an error. */
2866 void
2867 x_clear_area (dpy, window, x, y, width, height, exposures)
2868 Display *dpy;
2869 Window window;
2870 int x, y;
2871 int width, height;
2872 int exposures;
2874 xassert (width > 0 && height > 0);
2875 XClearArea (dpy, window, x, y, width, height, exposures);
2879 /* Clear an entire frame. */
2881 static void
2882 x_clear_frame (struct frame *f)
2884 /* Clearing the frame will erase any cursor, so mark them all as no
2885 longer visible. */
2886 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2887 output_cursor.hpos = output_cursor.vpos = 0;
2888 output_cursor.x = -1;
2890 /* We don't set the output cursor here because there will always
2891 follow an explicit cursor_to. */
2892 BLOCK_INPUT;
2893 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2895 /* We have to clear the scroll bars, too. If we have changed
2896 colors or something like that, then they should be notified. */
2897 x_scroll_bar_clear (f);
2899 XFlush (FRAME_X_DISPLAY (f));
2901 UNBLOCK_INPUT;
2906 /* Invert the middle quarter of the frame for .15 sec. */
2908 /* We use the select system call to do the waiting, so we have to make
2909 sure it's available. If it isn't, we just won't do visual bells. */
2911 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2914 /* Subtract the `struct timeval' values X and Y, storing the result in
2915 *RESULT. Return 1 if the difference is negative, otherwise 0. */
2917 static int
2918 timeval_subtract (result, x, y)
2919 struct timeval *result, x, y;
2921 /* Perform the carry for the later subtraction by updating y. This
2922 is safer because on some systems the tv_sec member is unsigned. */
2923 if (x.tv_usec < y.tv_usec)
2925 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
2926 y.tv_usec -= 1000000 * nsec;
2927 y.tv_sec += nsec;
2930 if (x.tv_usec - y.tv_usec > 1000000)
2932 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
2933 y.tv_usec += 1000000 * nsec;
2934 y.tv_sec -= nsec;
2937 /* Compute the time remaining to wait. tv_usec is certainly
2938 positive. */
2939 result->tv_sec = x.tv_sec - y.tv_sec;
2940 result->tv_usec = x.tv_usec - y.tv_usec;
2942 /* Return indication of whether the result should be considered
2943 negative. */
2944 return x.tv_sec < y.tv_sec;
2947 void
2948 XTflash (f)
2949 struct frame *f;
2951 BLOCK_INPUT;
2954 GC gc;
2956 /* Create a GC that will use the GXxor function to flip foreground
2957 pixels into background pixels. */
2959 XGCValues values;
2961 values.function = GXxor;
2962 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
2963 ^ FRAME_BACKGROUND_PIXEL (f));
2965 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2966 GCFunction | GCForeground, &values);
2970 /* Get the height not including a menu bar widget. */
2971 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
2972 /* Height of each line to flash. */
2973 int flash_height = FRAME_LINE_HEIGHT (f);
2974 /* These will be the left and right margins of the rectangles. */
2975 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
2976 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
2978 int width;
2980 /* Don't flash the area between a scroll bar and the frame
2981 edge it is next to. */
2982 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
2984 case vertical_scroll_bar_left:
2985 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2986 break;
2988 case vertical_scroll_bar_right:
2989 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2990 break;
2992 default:
2993 break;
2996 width = flash_right - flash_left;
2998 /* If window is tall, flash top and bottom line. */
2999 if (height > 3 * FRAME_LINE_HEIGHT (f))
3001 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3002 flash_left,
3003 (FRAME_INTERNAL_BORDER_WIDTH (f)
3004 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
3005 width, flash_height);
3006 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3007 flash_left,
3008 (height - flash_height
3009 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3010 width, flash_height);
3012 else
3013 /* If it is short, flash it all. */
3014 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3015 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3016 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3018 x_flush (f);
3021 struct timeval wakeup;
3023 EMACS_GET_TIME (wakeup);
3025 /* Compute time to wait until, propagating carry from usecs. */
3026 wakeup.tv_usec += 150000;
3027 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
3028 wakeup.tv_usec %= 1000000;
3030 /* Keep waiting until past the time wakeup or any input gets
3031 available. */
3032 while (! detect_input_pending ())
3034 struct timeval current;
3035 struct timeval timeout;
3037 EMACS_GET_TIME (current);
3039 /* Break if result would be negative. */
3040 if (timeval_subtract (&current, wakeup, current))
3041 break;
3043 /* How long `select' should wait. */
3044 timeout.tv_sec = 0;
3045 timeout.tv_usec = 10000;
3047 /* Try to wait that long--but we might wake up sooner. */
3048 select (0, NULL, NULL, NULL, &timeout);
3052 /* If window is tall, flash top and bottom line. */
3053 if (height > 3 * FRAME_LINE_HEIGHT (f))
3055 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3056 flash_left,
3057 (FRAME_INTERNAL_BORDER_WIDTH (f)
3058 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
3059 width, flash_height);
3060 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3061 flash_left,
3062 (height - flash_height
3063 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3064 width, flash_height);
3066 else
3067 /* If it is short, flash it all. */
3068 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3069 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3070 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3072 XFreeGC (FRAME_X_DISPLAY (f), gc);
3073 x_flush (f);
3077 UNBLOCK_INPUT;
3080 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
3083 /* Make audible bell. */
3085 void
3086 XTring_bell ()
3088 struct frame *f = SELECTED_FRAME ();
3090 if (FRAME_X_DISPLAY (f))
3092 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3093 if (visible_bell)
3094 XTflash (f);
3095 else
3096 #endif
3098 BLOCK_INPUT;
3099 XBell (FRAME_X_DISPLAY (f), 0);
3100 XFlush (FRAME_X_DISPLAY (f));
3101 UNBLOCK_INPUT;
3107 /* Specify how many text lines, from the top of the window,
3108 should be affected by insert-lines and delete-lines operations.
3109 This, and those operations, are used only within an update
3110 that is bounded by calls to x_update_begin and x_update_end. */
3112 static void
3113 XTset_terminal_window (n)
3114 register int n;
3116 /* This function intentionally left blank. */
3121 /***********************************************************************
3122 Line Dance
3123 ***********************************************************************/
3125 /* Perform an insert-lines or delete-lines operation, inserting N
3126 lines or deleting -N lines at vertical position VPOS. */
3128 static void
3129 x_ins_del_lines (f, vpos, n)
3130 struct frame *f;
3131 int vpos, n;
3133 abort ();
3137 /* Scroll part of the display as described by RUN. */
3139 static void
3140 x_scroll_run (w, run)
3141 struct window *w;
3142 struct run *run;
3144 struct frame *f = XFRAME (w->frame);
3145 int x, y, width, height, from_y, to_y, bottom_y;
3147 /* Get frame-relative bounding box of the text display area of W,
3148 without mode lines. Include in this box the left and right
3149 fringe of W. */
3150 window_box (w, -1, &x, &y, &width, &height);
3152 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3153 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3154 bottom_y = y + height;
3156 if (to_y < from_y)
3158 /* Scrolling up. Make sure we don't copy part of the mode
3159 line at the bottom. */
3160 if (from_y + run->height > bottom_y)
3161 height = bottom_y - from_y;
3162 else
3163 height = run->height;
3165 else
3167 /* Scolling down. Make sure we don't copy over the mode line.
3168 at the bottom. */
3169 if (to_y + run->height > bottom_y)
3170 height = bottom_y - to_y;
3171 else
3172 height = run->height;
3175 BLOCK_INPUT;
3177 /* Cursor off. Will be switched on again in x_update_window_end. */
3178 updated_window = w;
3179 x_clear_cursor (w);
3181 XCopyArea (FRAME_X_DISPLAY (f),
3182 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3183 f->output_data.x->normal_gc,
3184 x, from_y,
3185 width, height,
3186 x, to_y);
3188 UNBLOCK_INPUT;
3193 /***********************************************************************
3194 Exposure Events
3195 ***********************************************************************/
3198 static void
3199 frame_highlight (f)
3200 struct frame *f;
3202 /* We used to only do this if Vx_no_window_manager was non-nil, but
3203 the ICCCM (section 4.1.6) says that the window's border pixmap
3204 and border pixel are window attributes which are "private to the
3205 client", so we can always change it to whatever we want. */
3206 BLOCK_INPUT;
3207 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3208 f->output_data.x->border_pixel);
3209 UNBLOCK_INPUT;
3210 x_update_cursor (f, 1);
3213 static void
3214 frame_unhighlight (f)
3215 struct frame *f;
3217 /* We used to only do this if Vx_no_window_manager was non-nil, but
3218 the ICCCM (section 4.1.6) says that the window's border pixmap
3219 and border pixel are window attributes which are "private to the
3220 client", so we can always change it to whatever we want. */
3221 BLOCK_INPUT;
3222 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3223 f->output_data.x->border_tile);
3224 UNBLOCK_INPUT;
3225 x_update_cursor (f, 1);
3228 /* The focus has changed. Update the frames as necessary to reflect
3229 the new situation. Note that we can't change the selected frame
3230 here, because the Lisp code we are interrupting might become confused.
3231 Each event gets marked with the frame in which it occurred, so the
3232 Lisp code can tell when the switch took place by examining the events. */
3234 static void
3235 x_new_focus_frame (dpyinfo, frame)
3236 struct x_display_info *dpyinfo;
3237 struct frame *frame;
3239 struct frame *old_focus = dpyinfo->x_focus_frame;
3241 if (frame != dpyinfo->x_focus_frame)
3243 /* Set this before calling other routines, so that they see
3244 the correct value of x_focus_frame. */
3245 dpyinfo->x_focus_frame = frame;
3247 if (old_focus && old_focus->auto_lower)
3248 x_lower_frame (old_focus);
3250 #if 0
3251 selected_frame = frame;
3252 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
3253 selected_frame);
3254 Fselect_window (selected_frame->selected_window, Qnil);
3255 choose_minibuf_frame ();
3256 #endif /* ! 0 */
3258 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3259 pending_autoraise_frame = dpyinfo->x_focus_frame;
3260 else
3261 pending_autoraise_frame = 0;
3264 x_frame_rehighlight (dpyinfo);
3267 /* Handle FocusIn and FocusOut state changes for FRAME.
3268 If FRAME has focus and there exists more than one frame, puts
3269 a FOCUS_IN_EVENT into *BUFP. */
3271 static void
3272 x_focus_changed (type, state, dpyinfo, frame, bufp)
3273 int type;
3274 int state;
3275 struct x_display_info *dpyinfo;
3276 struct frame *frame;
3277 struct input_event *bufp;
3279 if (type == FocusIn)
3281 if (dpyinfo->x_focus_event_frame != frame)
3283 x_new_focus_frame (dpyinfo, frame);
3284 dpyinfo->x_focus_event_frame = frame;
3286 /* Don't stop displaying the initial startup message
3287 for a switch-frame event we don't need. */
3288 if (GC_NILP (Vterminal_frame)
3289 && GC_CONSP (Vframe_list)
3290 && !GC_NILP (XCDR (Vframe_list)))
3292 bufp->kind = FOCUS_IN_EVENT;
3293 XSETFRAME (bufp->frame_or_window, frame);
3297 frame->output_data.x->focus_state |= state;
3299 #ifdef HAVE_X_I18N
3300 if (FRAME_XIC (frame))
3301 XSetICFocus (FRAME_XIC (frame));
3302 #endif
3304 else if (type == FocusOut)
3306 frame->output_data.x->focus_state &= ~state;
3308 if (dpyinfo->x_focus_event_frame == frame)
3310 dpyinfo->x_focus_event_frame = 0;
3311 x_new_focus_frame (dpyinfo, 0);
3314 #ifdef HAVE_X_I18N
3315 if (FRAME_XIC (frame))
3316 XUnsetICFocus (FRAME_XIC (frame));
3317 #endif
3321 /* The focus may have changed. Figure out if it is a real focus change,
3322 by checking both FocusIn/Out and Enter/LeaveNotify events.
3324 Returns FOCUS_IN_EVENT event in *BUFP. */
3326 static void
3327 x_detect_focus_change (dpyinfo, event, bufp)
3328 struct x_display_info *dpyinfo;
3329 XEvent *event;
3330 struct input_event *bufp;
3332 struct frame *frame;
3334 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3335 if (! frame)
3336 return;
3338 switch (event->type)
3340 case EnterNotify:
3341 case LeaveNotify:
3343 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3344 int focus_state
3345 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3347 if (event->xcrossing.detail != NotifyInferior
3348 && event->xcrossing.focus
3349 && ! (focus_state & FOCUS_EXPLICIT))
3350 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3351 FOCUS_IMPLICIT,
3352 dpyinfo, frame, bufp);
3354 break;
3356 case FocusIn:
3357 case FocusOut:
3358 x_focus_changed (event->type,
3359 (event->xfocus.detail == NotifyPointer ?
3360 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3361 dpyinfo, frame, bufp);
3362 break;
3367 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3369 void
3370 x_mouse_leave (dpyinfo)
3371 struct x_display_info *dpyinfo;
3373 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3376 /* The focus has changed, or we have redirected a frame's focus to
3377 another frame (this happens when a frame uses a surrogate
3378 mini-buffer frame). Shift the highlight as appropriate.
3380 The FRAME argument doesn't necessarily have anything to do with which
3381 frame is being highlighted or un-highlighted; we only use it to find
3382 the appropriate X display info. */
3384 static void
3385 XTframe_rehighlight (frame)
3386 struct frame *frame;
3388 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3391 static void
3392 x_frame_rehighlight (dpyinfo)
3393 struct x_display_info *dpyinfo;
3395 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3397 if (dpyinfo->x_focus_frame)
3399 dpyinfo->x_highlight_frame
3400 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3401 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3402 : dpyinfo->x_focus_frame);
3403 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3405 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3406 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3409 else
3410 dpyinfo->x_highlight_frame = 0;
3412 if (dpyinfo->x_highlight_frame != old_highlight)
3414 if (old_highlight)
3415 frame_unhighlight (old_highlight);
3416 if (dpyinfo->x_highlight_frame)
3417 frame_highlight (dpyinfo->x_highlight_frame);
3423 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3425 /* Initialize mode_switch_bit and modifier_meaning. */
3426 static void
3427 x_find_modifier_meanings (dpyinfo)
3428 struct x_display_info *dpyinfo;
3430 int min_code, max_code;
3431 KeySym *syms;
3432 int syms_per_code;
3433 XModifierKeymap *mods;
3435 dpyinfo->meta_mod_mask = 0;
3436 dpyinfo->shift_lock_mask = 0;
3437 dpyinfo->alt_mod_mask = 0;
3438 dpyinfo->super_mod_mask = 0;
3439 dpyinfo->hyper_mod_mask = 0;
3441 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3443 syms = XGetKeyboardMapping (dpyinfo->display,
3444 min_code, max_code - min_code + 1,
3445 &syms_per_code);
3446 mods = XGetModifierMapping (dpyinfo->display);
3448 /* Scan the modifier table to see which modifier bits the Meta and
3449 Alt keysyms are on. */
3451 int row, col; /* The row and column in the modifier table. */
3452 int found_alt_or_meta;
3454 for (row = 3; row < 8; row++)
3456 found_alt_or_meta = 0;
3457 for (col = 0; col < mods->max_keypermod; col++)
3459 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3461 /* Zeroes are used for filler. Skip them. */
3462 if (code == 0)
3463 continue;
3465 /* Are any of this keycode's keysyms a meta key? */
3467 int code_col;
3469 for (code_col = 0; code_col < syms_per_code; code_col++)
3471 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3473 switch (sym)
3475 case XK_Meta_L:
3476 case XK_Meta_R:
3477 found_alt_or_meta = 1;
3478 dpyinfo->meta_mod_mask |= (1 << row);
3479 break;
3481 case XK_Alt_L:
3482 case XK_Alt_R:
3483 found_alt_or_meta = 1;
3484 dpyinfo->alt_mod_mask |= (1 << row);
3485 break;
3487 case XK_Hyper_L:
3488 case XK_Hyper_R:
3489 if (!found_alt_or_meta)
3490 dpyinfo->hyper_mod_mask |= (1 << row);
3491 code_col = syms_per_code;
3492 col = mods->max_keypermod;
3493 break;
3495 case XK_Super_L:
3496 case XK_Super_R:
3497 if (!found_alt_or_meta)
3498 dpyinfo->super_mod_mask |= (1 << row);
3499 code_col = syms_per_code;
3500 col = mods->max_keypermod;
3501 break;
3503 case XK_Shift_Lock:
3504 /* Ignore this if it's not on the lock modifier. */
3505 if (!found_alt_or_meta && ((1 << row) == LockMask))
3506 dpyinfo->shift_lock_mask = LockMask;
3507 code_col = syms_per_code;
3508 col = mods->max_keypermod;
3509 break;
3517 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3518 if (! dpyinfo->meta_mod_mask)
3520 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3521 dpyinfo->alt_mod_mask = 0;
3524 /* If some keys are both alt and meta,
3525 make them just meta, not alt. */
3526 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3528 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3531 XFree ((char *) syms);
3532 XFreeModifiermap (mods);
3535 /* Convert between the modifier bits X uses and the modifier bits
3536 Emacs uses. */
3538 unsigned int
3539 x_x_to_emacs_modifiers (dpyinfo, state)
3540 struct x_display_info *dpyinfo;
3541 unsigned int state;
3543 EMACS_UINT mod_meta = meta_modifier;
3544 EMACS_UINT mod_alt = alt_modifier;
3545 EMACS_UINT mod_hyper = hyper_modifier;
3546 EMACS_UINT mod_super = super_modifier;
3547 Lisp_Object tem;
3549 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3550 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3551 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3552 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3553 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3554 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3555 tem = Fget (Vx_super_keysym, Qmodifier_value);
3556 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3559 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3560 | ((state & ControlMask) ? ctrl_modifier : 0)
3561 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3562 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3563 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3564 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3567 static unsigned int
3568 x_emacs_to_x_modifiers (dpyinfo, state)
3569 struct x_display_info *dpyinfo;
3570 unsigned int state;
3572 EMACS_UINT mod_meta = meta_modifier;
3573 EMACS_UINT mod_alt = alt_modifier;
3574 EMACS_UINT mod_hyper = hyper_modifier;
3575 EMACS_UINT mod_super = super_modifier;
3577 Lisp_Object tem;
3579 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3580 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3581 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3582 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3583 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3584 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3585 tem = Fget (Vx_super_keysym, Qmodifier_value);
3586 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3589 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3590 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3591 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3592 | ((state & shift_modifier) ? ShiftMask : 0)
3593 | ((state & ctrl_modifier) ? ControlMask : 0)
3594 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3597 /* Convert a keysym to its name. */
3599 char *
3600 x_get_keysym_name (keysym)
3601 KeySym keysym;
3603 char *value;
3605 BLOCK_INPUT;
3606 value = XKeysymToString (keysym);
3607 UNBLOCK_INPUT;
3609 return value;
3614 /* Mouse clicks and mouse movement. Rah. */
3616 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3618 If the event is a button press, then note that we have grabbed
3619 the mouse. */
3621 static Lisp_Object
3622 construct_mouse_click (result, event, f)
3623 struct input_event *result;
3624 XButtonEvent *event;
3625 struct frame *f;
3627 /* Make the event type NO_EVENT; we'll change that when we decide
3628 otherwise. */
3629 result->kind = MOUSE_CLICK_EVENT;
3630 result->code = event->button - Button1;
3631 result->timestamp = event->time;
3632 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3633 event->state)
3634 | (event->type == ButtonRelease
3635 ? up_modifier
3636 : down_modifier));
3638 XSETINT (result->x, event->x);
3639 XSETINT (result->y, event->y);
3640 XSETFRAME (result->frame_or_window, f);
3641 result->arg = Qnil;
3642 return Qnil;
3646 /* Function to report a mouse movement to the mainstream Emacs code.
3647 The input handler calls this.
3649 We have received a mouse movement event, which is given in *event.
3650 If the mouse is over a different glyph than it was last time, tell
3651 the mainstream emacs code by setting mouse_moved. If not, ask for
3652 another motion event, so we can check again the next time it moves. */
3654 static XMotionEvent last_mouse_motion_event;
3655 static Lisp_Object last_mouse_motion_frame;
3657 static int
3658 note_mouse_movement (frame, event)
3659 FRAME_PTR frame;
3660 XMotionEvent *event;
3662 last_mouse_movement_time = event->time;
3663 last_mouse_motion_event = *event;
3664 XSETFRAME (last_mouse_motion_frame, frame);
3666 if (!FRAME_X_OUTPUT (frame))
3667 return 0;
3669 if (event->window != FRAME_X_WINDOW (frame))
3671 frame->mouse_moved = 1;
3672 last_mouse_scroll_bar = Qnil;
3673 note_mouse_highlight (frame, -1, -1);
3674 last_mouse_glyph_frame = 0;
3675 return 1;
3679 /* Has the mouse moved off the glyph it was on at the last sighting? */
3680 if (frame != last_mouse_glyph_frame
3681 || event->x < last_mouse_glyph.x
3682 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3683 || event->y < last_mouse_glyph.y
3684 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3686 frame->mouse_moved = 1;
3687 last_mouse_scroll_bar = Qnil;
3688 note_mouse_highlight (frame, event->x, event->y);
3689 /* Remember which glyph we're now on. */
3690 remember_mouse_glyph (frame, event->x, event->y, &last_mouse_glyph);
3691 last_mouse_glyph_frame = frame;
3692 return 1;
3695 return 0;
3699 /************************************************************************
3700 Mouse Face
3701 ************************************************************************/
3703 static void
3704 redo_mouse_highlight ()
3706 if (!NILP (last_mouse_motion_frame)
3707 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3708 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3709 last_mouse_motion_event.x,
3710 last_mouse_motion_event.y);
3715 /* Return the current position of the mouse.
3716 *FP should be a frame which indicates which display to ask about.
3718 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3719 and *PART to the frame, window, and scroll bar part that the mouse
3720 is over. Set *X and *Y to the portion and whole of the mouse's
3721 position on the scroll bar.
3723 If the mouse movement started elsewhere, set *FP to the frame the
3724 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3725 the mouse is over.
3727 Set *TIME to the server time-stamp for the time at which the mouse
3728 was at this position.
3730 Don't store anything if we don't have a valid set of values to report.
3732 This clears the mouse_moved flag, so we can wait for the next mouse
3733 movement. */
3735 static void
3736 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3737 FRAME_PTR *fp;
3738 int insist;
3739 Lisp_Object *bar_window;
3740 enum scroll_bar_part *part;
3741 Lisp_Object *x, *y;
3742 unsigned long *time;
3744 FRAME_PTR f1;
3746 BLOCK_INPUT;
3748 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3749 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3750 else
3752 Window root;
3753 int root_x, root_y;
3755 Window dummy_window;
3756 int dummy;
3758 Lisp_Object frame, tail;
3760 /* Clear the mouse-moved flag for every frame on this display. */
3761 FOR_EACH_FRAME (tail, frame)
3762 if (FRAME_X_P (XFRAME (frame))
3763 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3764 XFRAME (frame)->mouse_moved = 0;
3766 last_mouse_scroll_bar = Qnil;
3768 /* Figure out which root window we're on. */
3769 XQueryPointer (FRAME_X_DISPLAY (*fp),
3770 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3772 /* The root window which contains the pointer. */
3773 &root,
3775 /* Trash which we can't trust if the pointer is on
3776 a different screen. */
3777 &dummy_window,
3779 /* The position on that root window. */
3780 &root_x, &root_y,
3782 /* More trash we can't trust. */
3783 &dummy, &dummy,
3785 /* Modifier keys and pointer buttons, about which
3786 we don't care. */
3787 (unsigned int *) &dummy);
3789 /* Now we have a position on the root; find the innermost window
3790 containing the pointer. */
3792 Window win, child;
3793 int win_x, win_y;
3794 int parent_x = 0, parent_y = 0;
3796 win = root;
3798 /* XTranslateCoordinates can get errors if the window
3799 structure is changing at the same time this function
3800 is running. So at least we must not crash from them. */
3802 x_catch_errors (FRAME_X_DISPLAY (*fp));
3804 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3805 && FRAME_LIVE_P (last_mouse_frame))
3807 /* If mouse was grabbed on a frame, give coords for that frame
3808 even if the mouse is now outside it. */
3809 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3811 /* From-window, to-window. */
3812 root, FRAME_X_WINDOW (last_mouse_frame),
3814 /* From-position, to-position. */
3815 root_x, root_y, &win_x, &win_y,
3817 /* Child of win. */
3818 &child);
3819 f1 = last_mouse_frame;
3821 else
3823 while (1)
3825 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3827 /* From-window, to-window. */
3828 root, win,
3830 /* From-position, to-position. */
3831 root_x, root_y, &win_x, &win_y,
3833 /* Child of win. */
3834 &child);
3836 if (child == None || child == win)
3837 break;
3839 win = child;
3840 parent_x = win_x;
3841 parent_y = win_y;
3844 /* Now we know that:
3845 win is the innermost window containing the pointer
3846 (XTC says it has no child containing the pointer),
3847 win_x and win_y are the pointer's position in it
3848 (XTC did this the last time through), and
3849 parent_x and parent_y are the pointer's position in win's parent.
3850 (They are what win_x and win_y were when win was child.
3851 If win is the root window, it has no parent, and
3852 parent_{x,y} are invalid, but that's okay, because we'll
3853 never use them in that case.) */
3855 /* Is win one of our frames? */
3856 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3858 #ifdef USE_X_TOOLKIT
3859 /* If we end up with the menu bar window, say it's not
3860 on the frame. */
3861 if (f1 != NULL
3862 && f1->output_data.x->menubar_widget
3863 && win == XtWindow (f1->output_data.x->menubar_widget))
3864 f1 = NULL;
3865 #endif /* USE_X_TOOLKIT */
3868 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3869 f1 = 0;
3871 x_uncatch_errors ();
3873 /* If not, is it one of our scroll bars? */
3874 if (! f1)
3876 struct scroll_bar *bar;
3878 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
3880 if (bar)
3882 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3883 win_x = parent_x;
3884 win_y = parent_y;
3888 if (f1 == 0 && insist > 0)
3889 f1 = SELECTED_FRAME ();
3891 if (f1)
3893 /* Ok, we found a frame. Store all the values.
3894 last_mouse_glyph is a rectangle used to reduce the
3895 generation of mouse events. To not miss any motion
3896 events, we must divide the frame into rectangles of the
3897 size of the smallest character that could be displayed
3898 on it, i.e. into the same rectangles that matrices on
3899 the frame are divided into. */
3901 remember_mouse_glyph (f1, win_x, win_y, &last_mouse_glyph);
3902 last_mouse_glyph_frame = f1;
3904 *bar_window = Qnil;
3905 *part = 0;
3906 *fp = f1;
3907 XSETINT (*x, win_x);
3908 XSETINT (*y, win_y);
3909 *time = last_mouse_movement_time;
3914 UNBLOCK_INPUT;
3919 /***********************************************************************
3920 Scroll bars
3921 ***********************************************************************/
3923 /* Scroll bar support. */
3925 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
3926 manages it.
3927 This can be called in GC, so we have to make sure to strip off mark
3928 bits. */
3930 static struct scroll_bar *
3931 x_window_to_scroll_bar (display, window_id)
3932 Display *display;
3933 Window window_id;
3935 Lisp_Object tail;
3937 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3938 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
3939 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
3941 for (tail = Vframe_list;
3942 XGCTYPE (tail) == Lisp_Cons;
3943 tail = XCDR (tail))
3945 Lisp_Object frame, bar, condemned;
3947 frame = XCAR (tail);
3948 /* All elements of Vframe_list should be frames. */
3949 if (! GC_FRAMEP (frame))
3950 abort ();
3952 if (! FRAME_X_P (XFRAME (frame)))
3953 continue;
3955 /* Scan this frame's scroll bar list for a scroll bar with the
3956 right window ID. */
3957 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3958 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3959 /* This trick allows us to search both the ordinary and
3960 condemned scroll bar lists with one loop. */
3961 ! GC_NILP (bar) || (bar = condemned,
3962 condemned = Qnil,
3963 ! GC_NILP (bar));
3964 bar = XSCROLL_BAR (bar)->next)
3965 if (XSCROLL_BAR (bar)->x_window == window_id &&
3966 FRAME_X_DISPLAY (XFRAME (frame)) == display)
3967 return XSCROLL_BAR (bar);
3970 return 0;
3974 #if defined USE_LUCID
3976 /* Return the Lucid menu bar WINDOW is part of. Return null
3977 if WINDOW is not part of a menu bar. */
3979 static Widget
3980 x_window_to_menu_bar (window)
3981 Window window;
3983 Lisp_Object tail;
3985 for (tail = Vframe_list;
3986 XGCTYPE (tail) == Lisp_Cons;
3987 tail = XCDR (tail))
3989 if (FRAME_X_P (XFRAME (XCAR (tail))))
3991 Lisp_Object frame = XCAR (tail);
3992 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
3994 if (menu_bar && xlwmenu_window_p (menu_bar, window))
3995 return menu_bar;
3999 return NULL;
4002 #endif /* USE_LUCID */
4005 /************************************************************************
4006 Toolkit scroll bars
4007 ************************************************************************/
4009 #ifdef USE_TOOLKIT_SCROLL_BARS
4011 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
4012 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
4013 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
4014 struct scroll_bar *));
4015 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
4016 int, int, int));
4019 /* Lisp window being scrolled. Set when starting to interact with
4020 a toolkit scroll bar, reset to nil when ending the interaction. */
4022 static Lisp_Object window_being_scrolled;
4024 /* Last scroll bar part sent in xm_scroll_callback. */
4026 static int last_scroll_bar_part;
4028 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4029 that movements of 1/20 of the screen size are mapped to up/down. */
4031 #ifndef USE_GTK
4032 /* Id of action hook installed for scroll bars. */
4034 static XtActionHookId action_hook_id;
4036 static Boolean xaw3d_arrow_scroll;
4038 /* Whether the drag scrolling maintains the mouse at the top of the
4039 thumb. If not, resizing the thumb needs to be done more carefully
4040 to avoid jerkyness. */
4042 static Boolean xaw3d_pick_top;
4044 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4045 bars are used.. The hook is responsible for detecting when
4046 the user ends an interaction with the scroll bar, and generates
4047 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4049 static void
4050 xt_action_hook (widget, client_data, action_name, event, params,
4051 num_params)
4052 Widget widget;
4053 XtPointer client_data;
4054 String action_name;
4055 XEvent *event;
4056 String *params;
4057 Cardinal *num_params;
4059 int scroll_bar_p;
4060 char *end_action;
4062 #ifdef USE_MOTIF
4063 scroll_bar_p = XmIsScrollBar (widget);
4064 end_action = "Release";
4065 #else /* !USE_MOTIF i.e. use Xaw */
4066 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4067 end_action = "EndScroll";
4068 #endif /* USE_MOTIF */
4070 if (scroll_bar_p
4071 && strcmp (action_name, end_action) == 0
4072 && WINDOWP (window_being_scrolled))
4074 struct window *w;
4076 x_send_scroll_bar_event (window_being_scrolled,
4077 scroll_bar_end_scroll, 0, 0);
4078 w = XWINDOW (window_being_scrolled);
4080 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
4082 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4083 /* The thumb size is incorrect while dragging: fix it. */
4084 set_vertical_scroll_bar (w);
4086 window_being_scrolled = Qnil;
4087 last_scroll_bar_part = -1;
4089 /* Xt timeouts no longer needed. */
4090 toolkit_scroll_bar_interaction = 0;
4093 #endif /* not USE_GTK */
4095 /* A vector of windows used for communication between
4096 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4098 static struct window **scroll_bar_windows;
4099 static int scroll_bar_windows_size;
4102 /* Send a client message with message type Xatom_Scrollbar for a
4103 scroll action to the frame of WINDOW. PART is a value identifying
4104 the part of the scroll bar that was clicked on. PORTION is the
4105 amount to scroll of a whole of WHOLE. */
4107 static void
4108 x_send_scroll_bar_event (window, part, portion, whole)
4109 Lisp_Object window;
4110 int part, portion, whole;
4112 XEvent event;
4113 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4114 struct window *w = XWINDOW (window);
4115 struct frame *f = XFRAME (w->frame);
4116 int i;
4118 BLOCK_INPUT;
4120 /* Construct a ClientMessage event to send to the frame. */
4121 ev->type = ClientMessage;
4122 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4123 ev->display = FRAME_X_DISPLAY (f);
4124 ev->window = FRAME_X_WINDOW (f);
4125 ev->format = 32;
4127 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4128 not enough to store a pointer or Lisp_Object on a 64 bit system.
4129 So, store the window in scroll_bar_windows and pass the index
4130 into that array in the event. */
4131 for (i = 0; i < scroll_bar_windows_size; ++i)
4132 if (scroll_bar_windows[i] == NULL)
4133 break;
4135 if (i == scroll_bar_windows_size)
4137 int new_size = max (10, 2 * scroll_bar_windows_size);
4138 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4139 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4141 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4142 nbytes);
4143 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4144 scroll_bar_windows_size = new_size;
4147 scroll_bar_windows[i] = w;
4148 ev->data.l[0] = (long) i;
4149 ev->data.l[1] = (long) part;
4150 ev->data.l[2] = (long) 0;
4151 ev->data.l[3] = (long) portion;
4152 ev->data.l[4] = (long) whole;
4154 /* Make Xt timeouts work while the scroll bar is active. */
4155 toolkit_scroll_bar_interaction = 1;
4156 #ifdef USE_X_TOOLKIT
4157 x_activate_timeout_atimer ();
4158 #endif
4160 /* Setting the event mask to zero means that the message will
4161 be sent to the client that created the window, and if that
4162 window no longer exists, no event will be sent. */
4163 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4164 UNBLOCK_INPUT;
4168 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4169 in *IEVENT. */
4171 static void
4172 x_scroll_bar_to_input_event (event, ievent)
4173 XEvent *event;
4174 struct input_event *ievent;
4176 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4177 Lisp_Object window;
4178 struct frame *f;
4179 struct window *w;
4181 w = scroll_bar_windows[ev->data.l[0]];
4182 scroll_bar_windows[ev->data.l[0]] = NULL;
4184 XSETWINDOW (window, w);
4185 f = XFRAME (w->frame);
4187 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4188 ievent->frame_or_window = window;
4189 ievent->arg = Qnil;
4190 #ifdef USE_GTK
4191 ievent->timestamp = CurrentTime;
4192 #else
4193 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4194 #endif
4195 ievent->part = ev->data.l[1];
4196 ievent->code = ev->data.l[2];
4197 ievent->x = make_number ((int) ev->data.l[3]);
4198 ievent->y = make_number ((int) ev->data.l[4]);
4199 ievent->modifiers = 0;
4203 #ifdef USE_MOTIF
4205 /* Minimum and maximum values used for Motif scroll bars. */
4207 #define XM_SB_MAX 10000000
4210 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4211 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4212 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4214 static void
4215 xm_scroll_callback (widget, client_data, call_data)
4216 Widget widget;
4217 XtPointer client_data, call_data;
4219 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4220 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4221 int part = -1, whole = 0, portion = 0;
4223 switch (cs->reason)
4225 case XmCR_DECREMENT:
4226 bar->dragging = Qnil;
4227 part = scroll_bar_up_arrow;
4228 break;
4230 case XmCR_INCREMENT:
4231 bar->dragging = Qnil;
4232 part = scroll_bar_down_arrow;
4233 break;
4235 case XmCR_PAGE_DECREMENT:
4236 bar->dragging = Qnil;
4237 part = scroll_bar_above_handle;
4238 break;
4240 case XmCR_PAGE_INCREMENT:
4241 bar->dragging = Qnil;
4242 part = scroll_bar_below_handle;
4243 break;
4245 case XmCR_TO_TOP:
4246 bar->dragging = Qnil;
4247 part = scroll_bar_to_top;
4248 break;
4250 case XmCR_TO_BOTTOM:
4251 bar->dragging = Qnil;
4252 part = scroll_bar_to_bottom;
4253 break;
4255 case XmCR_DRAG:
4257 int slider_size;
4259 /* Get the slider size. */
4260 BLOCK_INPUT;
4261 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4262 UNBLOCK_INPUT;
4264 whole = XM_SB_MAX - slider_size;
4265 portion = min (cs->value, whole);
4266 part = scroll_bar_handle;
4267 bar->dragging = make_number (cs->value);
4269 break;
4271 case XmCR_VALUE_CHANGED:
4272 break;
4275 if (part >= 0)
4277 window_being_scrolled = bar->window;
4278 last_scroll_bar_part = part;
4279 x_send_scroll_bar_event (bar->window, part, portion, whole);
4283 #elif defined USE_GTK
4285 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4286 bar widget. DATA is a pointer to the scroll_bar structure. */
4288 static void
4289 xg_scroll_callback (widget, data)
4290 GtkRange *widget;
4291 gpointer data;
4293 struct scroll_bar *bar = (struct scroll_bar *) data;
4294 gdouble previous;
4295 gdouble position;
4296 gdouble *p;
4297 int diff;
4299 int part = -1, whole = 0, portion = 0;
4300 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (widget));
4302 position = gtk_adjustment_get_value (adj);
4304 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
4305 if (! p)
4307 p = (gdouble*) xmalloc (sizeof (gdouble));
4308 *p = XG_SB_MIN;
4309 g_object_set_data (G_OBJECT (widget), XG_LAST_SB_DATA, p);
4312 previous = *p;
4313 *p = position;
4315 if (xg_ignore_gtk_scrollbar) return;
4317 diff = (int) (position - previous);
4319 if (diff == (int) adj->step_increment)
4321 part = scroll_bar_down_arrow;
4322 bar->dragging = Qnil;
4324 else if (-diff == (int) adj->step_increment)
4326 part = scroll_bar_up_arrow;
4327 bar->dragging = Qnil;
4329 else if (diff == (int) adj->page_increment)
4331 part = scroll_bar_below_handle;
4332 bar->dragging = Qnil;
4334 else if (-diff == (int) adj->page_increment)
4336 part = scroll_bar_above_handle;
4337 bar->dragging = Qnil;
4339 else
4341 part = scroll_bar_handle;
4342 whole = adj->upper - adj->page_size;
4343 portion = min ((int)position, whole);
4344 bar->dragging = make_number ((int)portion);
4347 if (part >= 0)
4349 window_being_scrolled = bar->window;
4350 last_scroll_bar_part = part;
4351 x_send_scroll_bar_event (bar->window, part, portion, whole);
4355 #else /* not USE_GTK and not USE_MOTIF */
4357 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4358 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4359 scroll bar struct. CALL_DATA is a pointer to a float saying where
4360 the thumb is. */
4362 static void
4363 xaw_jump_callback (widget, client_data, call_data)
4364 Widget widget;
4365 XtPointer client_data, call_data;
4367 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4368 float top = *(float *) call_data;
4369 float shown;
4370 int whole, portion, height;
4371 int part;
4373 /* Get the size of the thumb, a value between 0 and 1. */
4374 BLOCK_INPUT;
4375 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4376 UNBLOCK_INPUT;
4378 whole = 10000000;
4379 portion = shown < 1 ? top * whole : 0;
4381 if (shown < 1 && (eabs (top + shown - 1) < 1.0/height))
4382 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4383 the bottom, so we force the scrolling whenever we see that we're
4384 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4385 we try to ensure that we always stay two pixels away from the
4386 bottom). */
4387 part = scroll_bar_down_arrow;
4388 else
4389 part = scroll_bar_handle;
4391 window_being_scrolled = bar->window;
4392 bar->dragging = make_number (portion);
4393 last_scroll_bar_part = part;
4394 x_send_scroll_bar_event (bar->window, part, portion, whole);
4398 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4399 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4400 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4401 the scroll bar. CALL_DATA is an integer specifying the action that
4402 has taken place. Its magnitude is in the range 0..height of the
4403 scroll bar. Negative values mean scroll towards buffer start.
4404 Values < height of scroll bar mean line-wise movement. */
4406 static void
4407 xaw_scroll_callback (widget, client_data, call_data)
4408 Widget widget;
4409 XtPointer client_data, call_data;
4411 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4412 /* The position really is stored cast to a pointer. */
4413 int position = (long) call_data;
4414 Dimension height;
4415 int part;
4417 /* Get the height of the scroll bar. */
4418 BLOCK_INPUT;
4419 XtVaGetValues (widget, XtNheight, &height, NULL);
4420 UNBLOCK_INPUT;
4422 if (eabs (position) >= height)
4423 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4425 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4426 it maps line-movement to call_data = max(5, height/20). */
4427 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
4428 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4429 else
4430 part = scroll_bar_move_ratio;
4432 window_being_scrolled = bar->window;
4433 bar->dragging = Qnil;
4434 last_scroll_bar_part = part;
4435 x_send_scroll_bar_event (bar->window, part, position, height);
4438 #endif /* not USE_GTK and not USE_MOTIF */
4440 #define SCROLL_BAR_NAME "verticalScrollBar"
4442 /* Create the widget for scroll bar BAR on frame F. Record the widget
4443 and X window of the scroll bar in BAR. */
4445 #ifdef USE_GTK
4446 static void
4447 x_create_toolkit_scroll_bar (f, bar)
4448 struct frame *f;
4449 struct scroll_bar *bar;
4451 char *scroll_bar_name = SCROLL_BAR_NAME;
4453 BLOCK_INPUT;
4454 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4455 scroll_bar_name);
4456 UNBLOCK_INPUT;
4459 #else /* not USE_GTK */
4461 static void
4462 x_create_toolkit_scroll_bar (f, bar)
4463 struct frame *f;
4464 struct scroll_bar *bar;
4466 Window xwindow;
4467 Widget widget;
4468 Arg av[20];
4469 int ac = 0;
4470 char *scroll_bar_name = SCROLL_BAR_NAME;
4471 unsigned long pixel;
4473 BLOCK_INPUT;
4475 #ifdef USE_MOTIF
4476 /* Set resources. Create the widget. */
4477 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4478 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4479 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4480 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4481 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4482 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4483 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4485 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4486 if (pixel != -1)
4488 XtSetArg (av[ac], XmNforeground, pixel);
4489 ++ac;
4492 pixel = f->output_data.x->scroll_bar_background_pixel;
4493 if (pixel != -1)
4495 XtSetArg (av[ac], XmNbackground, pixel);
4496 ++ac;
4499 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4500 scroll_bar_name, av, ac);
4502 /* Add one callback for everything that can happen. */
4503 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4504 (XtPointer) bar);
4505 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4506 (XtPointer) bar);
4507 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4508 (XtPointer) bar);
4509 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4510 (XtPointer) bar);
4511 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4512 (XtPointer) bar);
4513 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4514 (XtPointer) bar);
4515 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4516 (XtPointer) bar);
4518 /* Realize the widget. Only after that is the X window created. */
4519 XtRealizeWidget (widget);
4521 /* Set the cursor to an arrow. I didn't find a resource to do that.
4522 And I'm wondering why it hasn't an arrow cursor by default. */
4523 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4524 f->output_data.x->nontext_cursor);
4526 #else /* !USE_MOTIF i.e. use Xaw */
4528 /* Set resources. Create the widget. The background of the
4529 Xaw3d scroll bar widget is a little bit light for my taste.
4530 We don't alter it here to let users change it according
4531 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4532 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4533 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4534 /* For smoother scrolling with Xaw3d -sm */
4535 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4537 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4538 if (pixel != -1)
4540 XtSetArg (av[ac], XtNforeground, pixel);
4541 ++ac;
4544 pixel = f->output_data.x->scroll_bar_background_pixel;
4545 if (pixel != -1)
4547 XtSetArg (av[ac], XtNbackground, pixel);
4548 ++ac;
4551 /* Top/bottom shadow colors. */
4553 /* Allocate them, if necessary. */
4554 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4556 pixel = f->output_data.x->scroll_bar_background_pixel;
4557 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4558 &pixel, 1.2, 0x8000))
4559 pixel = -1;
4560 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4562 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4564 pixel = f->output_data.x->scroll_bar_background_pixel;
4565 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4566 &pixel, 0.6, 0x4000))
4567 pixel = -1;
4568 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4571 #ifdef XtNbeNiceToColormap
4572 /* Tell the toolkit about them. */
4573 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4574 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4575 /* We tried to allocate a color for the top/bottom shadow, and
4576 failed, so tell Xaw3d to use dithering instead. */
4578 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4579 ++ac;
4581 else
4582 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4583 be more consistent with other emacs 3d colors, and since Xaw3d is
4584 not good at dealing with allocation failure. */
4586 /* This tells Xaw3d to use real colors instead of dithering for
4587 the shadows. */
4588 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4589 ++ac;
4591 /* Specify the colors. */
4592 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4593 if (pixel != -1)
4595 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4596 ++ac;
4598 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4599 if (pixel != -1)
4601 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4602 ++ac;
4605 #endif
4607 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4608 f->output_data.x->edit_widget, av, ac);
4611 char *initial = "";
4612 char *val = initial;
4613 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4614 #ifdef XtNarrowScrollbars
4615 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4616 #endif
4617 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4618 if (xaw3d_arrow_scroll || val == initial)
4619 { /* ARROW_SCROLL */
4620 xaw3d_arrow_scroll = True;
4621 /* Isn't that just a personal preference ? --Stef */
4622 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4626 /* Define callbacks. */
4627 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4628 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4629 (XtPointer) bar);
4631 /* Realize the widget. Only after that is the X window created. */
4632 XtRealizeWidget (widget);
4634 #endif /* !USE_MOTIF */
4636 /* Install an action hook that lets us detect when the user
4637 finishes interacting with a scroll bar. */
4638 if (action_hook_id == 0)
4639 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4641 /* Remember X window and widget in the scroll bar vector. */
4642 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4643 xwindow = XtWindow (widget);
4644 bar->x_window = xwindow;
4646 UNBLOCK_INPUT;
4648 #endif /* not USE_GTK */
4651 /* Set the thumb size and position of scroll bar BAR. We are currently
4652 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4654 #ifdef USE_GTK
4655 static void
4656 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4657 struct scroll_bar *bar;
4658 int portion, position, whole;
4660 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4663 #else /* not USE_GTK */
4664 static void
4665 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4666 struct scroll_bar *bar;
4667 int portion, position, whole;
4669 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4670 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4671 float top, shown;
4673 BLOCK_INPUT;
4675 #ifdef USE_MOTIF
4677 /* We use an estimate of 30 chars per line rather than the real
4678 `portion' value. This has the disadvantage that the thumb size
4679 is not very representative, but it makes our life a lot easier.
4680 Otherwise, we have to constantly adjust the thumb size, which
4681 we can't always do quickly enough: while dragging, the size of
4682 the thumb might prevent the user from dragging the thumb all the
4683 way to the end. but Motif and some versions of Xaw3d don't allow
4684 updating the thumb size while dragging. Also, even if we can update
4685 its size, the update will often happen too late.
4686 If you don't believe it, check out revision 1.650 of xterm.c to see
4687 what hoops we were going through and the still poor behavior we got. */
4688 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4689 /* When the thumb is at the bottom, position == whole.
4690 So we need to increase `whole' to make space for the thumb. */
4691 whole += portion;
4693 if (whole <= 0)
4694 top = 0, shown = 1;
4695 else
4697 top = (float) position / whole;
4698 shown = (float) portion / whole;
4701 if (NILP (bar->dragging))
4703 int size, value;
4705 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4706 is the scroll bar's maximum and MIN is the scroll bar's minimum
4707 value. */
4708 size = shown * XM_SB_MAX;
4709 size = min (size, XM_SB_MAX);
4710 size = max (size, 1);
4712 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4713 value = top * XM_SB_MAX;
4714 value = min (value, XM_SB_MAX - size);
4716 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4718 #else /* !USE_MOTIF i.e. use Xaw */
4720 if (whole == 0)
4721 top = 0, shown = 1;
4722 else
4724 top = (float) position / whole;
4725 shown = (float) portion / whole;
4729 float old_top, old_shown;
4730 Dimension height;
4731 XtVaGetValues (widget,
4732 XtNtopOfThumb, &old_top,
4733 XtNshown, &old_shown,
4734 XtNheight, &height,
4735 NULL);
4737 /* Massage the top+shown values. */
4738 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4739 top = max (0, min (1, top));
4740 else
4741 top = old_top;
4742 /* Keep two pixels available for moving the thumb down. */
4743 shown = max (0, min (1 - top - (2.0 / height), shown));
4745 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4746 check that your system's configuration file contains a define
4747 for `NARROWPROTO'. See s/freebsd.h for an example. */
4748 if (top != old_top || shown != old_shown)
4750 if (NILP (bar->dragging))
4751 XawScrollbarSetThumb (widget, top, shown);
4752 else
4754 /* Try to make the scrolling a tad smoother. */
4755 if (!xaw3d_pick_top)
4756 shown = min (shown, old_shown);
4758 XawScrollbarSetThumb (widget, top, shown);
4762 #endif /* !USE_MOTIF */
4764 UNBLOCK_INPUT;
4766 #endif /* not USE_GTK */
4768 #endif /* USE_TOOLKIT_SCROLL_BARS */
4772 /************************************************************************
4773 Scroll bars, general
4774 ************************************************************************/
4776 /* Create a scroll bar and return the scroll bar vector for it. W is
4777 the Emacs window on which to create the scroll bar. TOP, LEFT,
4778 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4779 scroll bar. */
4781 static struct scroll_bar *
4782 x_scroll_bar_create (w, top, left, width, height)
4783 struct window *w;
4784 int top, left, width, height;
4786 struct frame *f = XFRAME (w->frame);
4787 struct scroll_bar *bar
4788 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
4790 BLOCK_INPUT;
4792 #ifdef USE_TOOLKIT_SCROLL_BARS
4793 x_create_toolkit_scroll_bar (f, bar);
4794 #else /* not USE_TOOLKIT_SCROLL_BARS */
4796 XSetWindowAttributes a;
4797 unsigned long mask;
4798 Window window;
4800 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4801 if (a.background_pixel == -1)
4802 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4804 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4805 | ButtonMotionMask | PointerMotionHintMask
4806 | ExposureMask);
4807 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4809 mask = (CWBackPixel | CWEventMask | CWCursor);
4811 /* Clear the area of W that will serve as a scroll bar. This is
4812 for the case that a window has been split horizontally. In
4813 this case, no clear_frame is generated to reduce flickering. */
4814 if (width > 0 && height > 0)
4815 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4816 left, top, width,
4817 window_box_height (w), False);
4819 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4820 /* Position and size of scroll bar. */
4821 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4822 top,
4823 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4824 height,
4825 /* Border width, depth, class, and visual. */
4827 CopyFromParent,
4828 CopyFromParent,
4829 CopyFromParent,
4830 /* Attributes. */
4831 mask, &a);
4832 bar->x_window = window;
4834 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4836 XSETWINDOW (bar->window, w);
4837 bar->top = top;
4838 bar->left = left;
4839 bar->width = width;
4840 bar->height = height;
4841 bar->start = 0;
4842 bar->end = 0;
4843 bar->dragging = Qnil;
4844 bar->fringe_extended_p = 0;
4846 /* Add bar to its frame's list of scroll bars. */
4847 bar->next = FRAME_SCROLL_BARS (f);
4848 bar->prev = Qnil;
4849 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4850 if (!NILP (bar->next))
4851 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4853 /* Map the window/widget. */
4854 #ifdef USE_TOOLKIT_SCROLL_BARS
4856 #ifdef USE_GTK
4857 xg_update_scrollbar_pos (f,
4858 bar->x_window,
4859 top,
4860 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4861 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4862 max (height, 1));
4863 xg_show_scroll_bar (bar->x_window);
4864 #else /* not USE_GTK */
4865 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4866 XtConfigureWidget (scroll_bar,
4867 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4868 top,
4869 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4870 max (height, 1), 0);
4871 XtMapWidget (scroll_bar);
4872 #endif /* not USE_GTK */
4874 #else /* not USE_TOOLKIT_SCROLL_BARS */
4875 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
4876 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4878 UNBLOCK_INPUT;
4879 return bar;
4883 #ifndef USE_TOOLKIT_SCROLL_BARS
4885 /* Draw BAR's handle in the proper position.
4887 If the handle is already drawn from START to END, don't bother
4888 redrawing it, unless REBUILD is non-zero; in that case, always
4889 redraw it. (REBUILD is handy for drawing the handle after expose
4890 events.)
4892 Normally, we want to constrain the start and end of the handle to
4893 fit inside its rectangle, but if the user is dragging the scroll
4894 bar handle, we want to let them drag it down all the way, so that
4895 the bar's top is as far down as it goes; otherwise, there's no way
4896 to move to the very end of the buffer. */
4898 static void
4899 x_scroll_bar_set_handle (bar, start, end, rebuild)
4900 struct scroll_bar *bar;
4901 int start, end;
4902 int rebuild;
4904 int dragging = ! NILP (bar->dragging);
4905 Window w = bar->x_window;
4906 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4907 GC gc = f->output_data.x->normal_gc;
4909 /* If the display is already accurate, do nothing. */
4910 if (! rebuild
4911 && start == bar->start
4912 && end == bar->end)
4913 return;
4915 BLOCK_INPUT;
4918 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
4919 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
4920 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
4922 /* Make sure the values are reasonable, and try to preserve
4923 the distance between start and end. */
4925 int length = end - start;
4927 if (start < 0)
4928 start = 0;
4929 else if (start > top_range)
4930 start = top_range;
4931 end = start + length;
4933 if (end < start)
4934 end = start;
4935 else if (end > top_range && ! dragging)
4936 end = top_range;
4939 /* Store the adjusted setting in the scroll bar. */
4940 bar->start = start;
4941 bar->end = end;
4943 /* Clip the end position, just for display. */
4944 if (end > top_range)
4945 end = top_range;
4947 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
4948 below top positions, to make sure the handle is always at least
4949 that many pixels tall. */
4950 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
4952 /* Draw the empty space above the handle. Note that we can't clear
4953 zero-height areas; that means "clear to end of window." */
4954 if (0 < start)
4955 x_clear_area (FRAME_X_DISPLAY (f), w,
4956 /* x, y, width, height, and exposures. */
4957 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4958 VERTICAL_SCROLL_BAR_TOP_BORDER,
4959 inside_width, start,
4960 False);
4962 /* Change to proper foreground color if one is specified. */
4963 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4964 XSetForeground (FRAME_X_DISPLAY (f), gc,
4965 f->output_data.x->scroll_bar_foreground_pixel);
4967 /* Draw the handle itself. */
4968 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
4969 /* x, y, width, height */
4970 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4971 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
4972 inside_width, end - start);
4974 /* Restore the foreground color of the GC if we changed it above. */
4975 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4976 XSetForeground (FRAME_X_DISPLAY (f), gc,
4977 FRAME_FOREGROUND_PIXEL (f));
4979 /* Draw the empty space below the handle. Note that we can't
4980 clear zero-height areas; that means "clear to end of window." */
4981 if (end < inside_height)
4982 x_clear_area (FRAME_X_DISPLAY (f), w,
4983 /* x, y, width, height, and exposures. */
4984 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4985 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
4986 inside_width, inside_height - end,
4987 False);
4991 UNBLOCK_INPUT;
4994 #endif /* !USE_TOOLKIT_SCROLL_BARS */
4996 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
4997 nil. */
4999 static void
5000 x_scroll_bar_remove (bar)
5001 struct scroll_bar *bar;
5003 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5004 BLOCK_INPUT;
5006 #ifdef USE_TOOLKIT_SCROLL_BARS
5007 #ifdef USE_GTK
5008 xg_remove_scroll_bar (f, bar->x_window);
5009 #else /* not USE_GTK */
5010 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5011 #endif /* not USE_GTK */
5012 #else
5013 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
5014 #endif
5016 /* Disassociate this scroll bar from its window. */
5017 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
5019 UNBLOCK_INPUT;
5023 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5024 that we are displaying PORTION characters out of a total of WHOLE
5025 characters, starting at POSITION. If WINDOW has no scroll bar,
5026 create one. */
5028 static void
5029 XTset_vertical_scroll_bar (w, portion, whole, position)
5030 struct window *w;
5031 int portion, whole, position;
5033 struct frame *f = XFRAME (w->frame);
5034 struct scroll_bar *bar;
5035 int top, height, left, sb_left, width, sb_width;
5036 int window_y, window_height;
5037 #ifdef USE_TOOLKIT_SCROLL_BARS
5038 int fringe_extended_p;
5039 #endif
5041 /* Get window dimensions. */
5042 window_box (w, -1, 0, &window_y, 0, &window_height);
5043 top = window_y;
5044 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5045 height = window_height;
5047 /* Compute the left edge of the scroll bar area. */
5048 left = WINDOW_SCROLL_BAR_AREA_X (w);
5050 /* Compute the width of the scroll bar which might be less than
5051 the width of the area reserved for the scroll bar. */
5052 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
5053 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
5054 else
5055 sb_width = width;
5057 /* Compute the left edge of the scroll bar. */
5058 #ifdef USE_TOOLKIT_SCROLL_BARS
5059 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5060 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
5061 else
5062 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
5063 #else
5064 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5065 sb_left = left + width - sb_width;
5066 else
5067 sb_left = left;
5068 #endif
5070 #ifdef USE_TOOLKIT_SCROLL_BARS
5071 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5072 fringe_extended_p = (WINDOW_LEFTMOST_P (w)
5073 && WINDOW_LEFT_FRINGE_WIDTH (w)
5074 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5075 || WINDOW_LEFT_MARGIN_COLS (w) == 0));
5076 else
5077 fringe_extended_p = (WINDOW_RIGHTMOST_P (w)
5078 && WINDOW_RIGHT_FRINGE_WIDTH (w)
5079 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5080 || WINDOW_RIGHT_MARGIN_COLS (w) == 0));
5081 #endif
5083 /* Does the scroll bar exist yet? */
5084 if (NILP (w->vertical_scroll_bar))
5086 if (width > 0 && height > 0)
5088 BLOCK_INPUT;
5089 #ifdef USE_TOOLKIT_SCROLL_BARS
5090 if (fringe_extended_p)
5091 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5092 sb_left, top, sb_width, height, False);
5093 else
5094 #endif
5095 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5096 left, top, width, height, False);
5097 UNBLOCK_INPUT;
5100 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5102 else
5104 /* It may just need to be moved and resized. */
5105 unsigned int mask = 0;
5107 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5109 BLOCK_INPUT;
5111 if (sb_left != bar->left)
5112 mask |= CWX;
5113 if (top != bar->top)
5114 mask |= CWY;
5115 if (sb_width != bar->width)
5116 mask |= CWWidth;
5117 if (height != bar->height)
5118 mask |= CWHeight;
5120 #ifdef USE_TOOLKIT_SCROLL_BARS
5122 /* Move/size the scroll bar widget. */
5123 if (mask || bar->fringe_extended_p != fringe_extended_p)
5125 /* Since toolkit scroll bars are smaller than the space reserved
5126 for them on the frame, we have to clear "under" them. */
5127 if (width > 0 && height > 0)
5129 if (fringe_extended_p)
5130 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5131 sb_left, top, sb_width, height, False);
5132 else
5133 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5134 left, top, width, height, False);
5136 #ifdef USE_GTK
5137 xg_update_scrollbar_pos (f,
5138 bar->x_window,
5139 top,
5140 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5141 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM *2,
5142 max (height, 1));
5143 #else /* not USE_GTK */
5144 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5145 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5146 top,
5147 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5148 max (height, 1), 0);
5149 #endif /* not USE_GTK */
5151 #else /* not USE_TOOLKIT_SCROLL_BARS */
5153 /* Clear areas not covered by the scroll bar because of
5154 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5155 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5157 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5158 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5159 height, False);
5160 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5161 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5162 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5163 height, False);
5166 /* Clear areas not covered by the scroll bar because it's not as
5167 wide as the area reserved for it. This makes sure a
5168 previous mode line display is cleared after C-x 2 C-x 1, for
5169 example. */
5171 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5172 int rest = area_width - sb_width;
5173 if (rest > 0 && height > 0)
5175 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5176 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5177 left + area_width - rest, top,
5178 rest, height, False);
5179 else
5180 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5181 left, top, rest, height, False);
5185 /* Move/size the scroll bar window. */
5186 if (mask)
5188 XWindowChanges wc;
5190 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5191 wc.y = top;
5192 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5193 wc.height = height;
5194 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5195 mask, &wc);
5198 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5200 /* Remember new settings. */
5201 bar->left = sb_left;
5202 bar->top = top;
5203 bar->width = sb_width;
5204 bar->height = height;
5206 UNBLOCK_INPUT;
5209 #ifdef USE_TOOLKIT_SCROLL_BARS
5210 bar->fringe_extended_p = fringe_extended_p;
5212 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5213 #else /* not USE_TOOLKIT_SCROLL_BARS */
5214 /* Set the scroll bar's current state, unless we're currently being
5215 dragged. */
5216 if (NILP (bar->dragging))
5218 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5220 if (whole == 0)
5221 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5222 else
5224 int start = ((double) position * top_range) / whole;
5225 int end = ((double) (position + portion) * top_range) / whole;
5226 x_scroll_bar_set_handle (bar, start, end, 0);
5229 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5231 XSETVECTOR (w->vertical_scroll_bar, bar);
5235 /* The following three hooks are used when we're doing a thorough
5236 redisplay of the frame. We don't explicitly know which scroll bars
5237 are going to be deleted, because keeping track of when windows go
5238 away is a real pain - "Can you say set-window-configuration, boys
5239 and girls?" Instead, we just assert at the beginning of redisplay
5240 that *all* scroll bars are to be removed, and then save a scroll bar
5241 from the fiery pit when we actually redisplay its window. */
5243 /* Arrange for all scroll bars on FRAME to be removed at the next call
5244 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5245 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5247 static void
5248 XTcondemn_scroll_bars (frame)
5249 FRAME_PTR frame;
5251 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5252 while (! NILP (FRAME_SCROLL_BARS (frame)))
5254 Lisp_Object bar;
5255 bar = FRAME_SCROLL_BARS (frame);
5256 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5257 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5258 XSCROLL_BAR (bar)->prev = Qnil;
5259 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5260 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5261 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5266 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5267 Note that WINDOW isn't necessarily condemned at all. */
5269 static void
5270 XTredeem_scroll_bar (window)
5271 struct window *window;
5273 struct scroll_bar *bar;
5274 struct frame *f;
5276 /* We can't redeem this window's scroll bar if it doesn't have one. */
5277 if (NILP (window->vertical_scroll_bar))
5278 abort ();
5280 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5282 /* Unlink it from the condemned list. */
5283 f = XFRAME (WINDOW_FRAME (window));
5284 if (NILP (bar->prev))
5286 /* If the prev pointer is nil, it must be the first in one of
5287 the lists. */
5288 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5289 /* It's not condemned. Everything's fine. */
5290 return;
5291 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5292 window->vertical_scroll_bar))
5293 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5294 else
5295 /* If its prev pointer is nil, it must be at the front of
5296 one or the other! */
5297 abort ();
5299 else
5300 XSCROLL_BAR (bar->prev)->next = bar->next;
5302 if (! NILP (bar->next))
5303 XSCROLL_BAR (bar->next)->prev = bar->prev;
5305 bar->next = FRAME_SCROLL_BARS (f);
5306 bar->prev = Qnil;
5307 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5308 if (! NILP (bar->next))
5309 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5312 /* Remove all scroll bars on FRAME that haven't been saved since the
5313 last call to `*condemn_scroll_bars_hook'. */
5315 static void
5316 XTjudge_scroll_bars (f)
5317 FRAME_PTR f;
5319 Lisp_Object bar, next;
5321 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5323 /* Clear out the condemned list now so we won't try to process any
5324 more events on the hapless scroll bars. */
5325 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5327 for (; ! NILP (bar); bar = next)
5329 struct scroll_bar *b = XSCROLL_BAR (bar);
5331 x_scroll_bar_remove (b);
5333 next = b->next;
5334 b->next = b->prev = Qnil;
5337 /* Now there should be no references to the condemned scroll bars,
5338 and they should get garbage-collected. */
5342 #ifndef USE_TOOLKIT_SCROLL_BARS
5343 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5344 is a no-op when using toolkit scroll bars.
5346 This may be called from a signal handler, so we have to ignore GC
5347 mark bits. */
5349 static void
5350 x_scroll_bar_expose (bar, event)
5351 struct scroll_bar *bar;
5352 XEvent *event;
5354 Window w = bar->x_window;
5355 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5356 GC gc = f->output_data.x->normal_gc;
5357 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5359 BLOCK_INPUT;
5361 x_scroll_bar_set_handle (bar, bar->start, bar->end, 1);
5363 /* Switch to scroll bar foreground color. */
5364 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5365 XSetForeground (FRAME_X_DISPLAY (f), gc,
5366 f->output_data.x->scroll_bar_foreground_pixel);
5368 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5369 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5371 /* x, y, width, height */
5372 0, 0,
5373 bar->width - 1 - width_trim - width_trim,
5374 bar->height - 1);
5376 /* Restore the foreground color of the GC if we changed it above. */
5377 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5378 XSetForeground (FRAME_X_DISPLAY (f), gc,
5379 FRAME_FOREGROUND_PIXEL (f));
5381 UNBLOCK_INPUT;
5384 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5386 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5387 is set to something other than NO_EVENT, it is enqueued.
5389 This may be called from a signal handler, so we have to ignore GC
5390 mark bits. */
5393 static void
5394 x_scroll_bar_handle_click (bar, event, emacs_event)
5395 struct scroll_bar *bar;
5396 XEvent *event;
5397 struct input_event *emacs_event;
5399 if (! GC_WINDOWP (bar->window))
5400 abort ();
5402 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5403 emacs_event->code = event->xbutton.button - Button1;
5404 emacs_event->modifiers
5405 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5406 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5407 event->xbutton.state)
5408 | (event->type == ButtonRelease
5409 ? up_modifier
5410 : down_modifier));
5411 emacs_event->frame_or_window = bar->window;
5412 emacs_event->arg = Qnil;
5413 emacs_event->timestamp = event->xbutton.time;
5415 #if 0
5416 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5417 int internal_height
5418 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5419 #endif
5420 int top_range
5421 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5422 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5424 if (y < 0) y = 0;
5425 if (y > top_range) y = top_range;
5427 if (y < bar->start)
5428 emacs_event->part = scroll_bar_above_handle;
5429 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5430 emacs_event->part = scroll_bar_handle;
5431 else
5432 emacs_event->part = scroll_bar_below_handle;
5434 /* Just because the user has clicked on the handle doesn't mean
5435 they want to drag it. Lisp code needs to be able to decide
5436 whether or not we're dragging. */
5437 #if 0
5438 /* If the user has just clicked on the handle, record where they're
5439 holding it. */
5440 if (event->type == ButtonPress
5441 && emacs_event->part == scroll_bar_handle)
5442 XSETINT (bar->dragging, y - bar->start);
5443 #endif
5445 #ifndef USE_TOOLKIT_SCROLL_BARS
5446 /* If the user has released the handle, set it to its final position. */
5447 if (event->type == ButtonRelease
5448 && ! NILP (bar->dragging))
5450 int new_start = y - XINT (bar->dragging);
5451 int new_end = new_start + bar->end - bar->start;
5453 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5454 bar->dragging = Qnil;
5456 #endif
5458 /* Same deal here as the other #if 0. */
5459 #if 0
5460 /* Clicks on the handle are always reported as occurring at the top of
5461 the handle. */
5462 if (emacs_event->part == scroll_bar_handle)
5463 emacs_event->x = bar->start;
5464 else
5465 XSETINT (emacs_event->x, y);
5466 #else
5467 XSETINT (emacs_event->x, y);
5468 #endif
5470 XSETINT (emacs_event->y, top_range);
5474 #ifndef USE_TOOLKIT_SCROLL_BARS
5476 /* Handle some mouse motion while someone is dragging the scroll bar.
5478 This may be called from a signal handler, so we have to ignore GC
5479 mark bits. */
5481 static void
5482 x_scroll_bar_note_movement (bar, event)
5483 struct scroll_bar *bar;
5484 XEvent *event;
5486 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5488 last_mouse_movement_time = event->xmotion.time;
5490 f->mouse_moved = 1;
5491 XSETVECTOR (last_mouse_scroll_bar, bar);
5493 /* If we're dragging the bar, display it. */
5494 if (! GC_NILP (bar->dragging))
5496 /* Where should the handle be now? */
5497 int new_start = event->xmotion.y - XINT (bar->dragging);
5499 if (new_start != bar->start)
5501 int new_end = new_start + bar->end - bar->start;
5503 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5508 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5510 /* Return information to the user about the current position of the mouse
5511 on the scroll bar. */
5513 static void
5514 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5515 FRAME_PTR *fp;
5516 Lisp_Object *bar_window;
5517 enum scroll_bar_part *part;
5518 Lisp_Object *x, *y;
5519 unsigned long *time;
5521 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5522 Window w = bar->x_window;
5523 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5524 int win_x, win_y;
5525 Window dummy_window;
5526 int dummy_coord;
5527 unsigned int dummy_mask;
5529 BLOCK_INPUT;
5531 /* Get the mouse's position relative to the scroll bar window, and
5532 report that. */
5533 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5535 /* Root, child, root x and root y. */
5536 &dummy_window, &dummy_window,
5537 &dummy_coord, &dummy_coord,
5539 /* Position relative to scroll bar. */
5540 &win_x, &win_y,
5542 /* Mouse buttons and modifier keys. */
5543 &dummy_mask))
5545 else
5547 #if 0
5548 int inside_height
5549 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5550 #endif
5551 int top_range
5552 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5554 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5556 if (! NILP (bar->dragging))
5557 win_y -= XINT (bar->dragging);
5559 if (win_y < 0)
5560 win_y = 0;
5561 if (win_y > top_range)
5562 win_y = top_range;
5564 *fp = f;
5565 *bar_window = bar->window;
5567 if (! NILP (bar->dragging))
5568 *part = scroll_bar_handle;
5569 else if (win_y < bar->start)
5570 *part = scroll_bar_above_handle;
5571 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5572 *part = scroll_bar_handle;
5573 else
5574 *part = scroll_bar_below_handle;
5576 XSETINT (*x, win_y);
5577 XSETINT (*y, top_range);
5579 f->mouse_moved = 0;
5580 last_mouse_scroll_bar = Qnil;
5583 *time = last_mouse_movement_time;
5585 UNBLOCK_INPUT;
5589 /* The screen has been cleared so we may have changed foreground or
5590 background colors, and the scroll bars may need to be redrawn.
5591 Clear out the scroll bars, and ask for expose events, so we can
5592 redraw them. */
5594 void
5595 x_scroll_bar_clear (f)
5596 FRAME_PTR f;
5598 #ifndef USE_TOOLKIT_SCROLL_BARS
5599 Lisp_Object bar;
5601 /* We can have scroll bars even if this is 0,
5602 if we just turned off scroll bar mode.
5603 But in that case we should not clear them. */
5604 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5605 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5606 bar = XSCROLL_BAR (bar)->next)
5607 XClearArea (FRAME_X_DISPLAY (f),
5608 XSCROLL_BAR (bar)->x_window,
5609 0, 0, 0, 0, True);
5610 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5614 /* The main X event-reading loop - XTread_socket. */
5616 #if 0
5617 /* Time stamp of enter window event. This is only used by XTread_socket,
5618 but we have to put it out here, since static variables within functions
5619 sometimes don't work. */
5621 static Time enter_timestamp;
5622 #endif
5624 /* This holds the state XLookupString needs to implement dead keys
5625 and other tricks known as "compose processing". _X Window System_
5626 says that a portable program can't use this, but Stephen Gildea assures
5627 me that letting the compiler initialize it to zeros will work okay.
5629 This must be defined outside of XTread_socket, for the same reasons
5630 given for enter_timestamp, above. */
5632 static XComposeStatus compose_status;
5634 /* Record the last 100 characters stored
5635 to help debug the loss-of-chars-during-GC problem. */
5637 static int temp_index;
5638 static short temp_buffer[100];
5640 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5641 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5642 temp_index = 0; \
5643 temp_buffer[temp_index++] = (keysym)
5645 /* Set this to nonzero to fake an "X I/O error"
5646 on a particular display. */
5648 struct x_display_info *XTread_socket_fake_io_error;
5650 /* When we find no input here, we occasionally do a no-op command
5651 to verify that the X server is still running and we can still talk with it.
5652 We try all the open displays, one by one.
5653 This variable is used for cycling thru the displays. */
5655 static struct x_display_info *next_noop_dpyinfo;
5657 #define SET_SAVED_MENU_EVENT(size) \
5658 do \
5660 if (f->output_data.x->saved_menu_event == 0) \
5661 f->output_data.x->saved_menu_event \
5662 = (XEvent *) xmalloc (sizeof (XEvent)); \
5663 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5664 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT; \
5665 XSETFRAME (inev.ie.frame_or_window, f); \
5667 while (0)
5669 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5670 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5673 enum
5675 X_EVENT_NORMAL,
5676 X_EVENT_GOTO_OUT,
5677 X_EVENT_DROP
5680 /* Filter events for the current X input method.
5681 DPYINFO is the display this event is for.
5682 EVENT is the X event to filter.
5684 Returns non-zero if the event was filtered, caller shall not process
5685 this event further.
5686 Returns zero if event is wasn't filtered. */
5688 #ifdef HAVE_X_I18N
5689 static int
5690 x_filter_event (dpyinfo, event)
5691 struct x_display_info *dpyinfo;
5692 XEvent *event;
5694 /* XFilterEvent returns non-zero if the input method has
5695 consumed the event. We pass the frame's X window to
5696 XFilterEvent because that's the one for which the IC
5697 was created. */
5699 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5700 event->xclient.window);
5702 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5704 #endif
5706 #ifdef USE_GTK
5707 static int current_count;
5708 static int current_finish;
5709 static struct input_event *current_hold_quit;
5711 /* This is the filter function invoked by the GTK event loop.
5712 It is invoked before the XEvent is translated to a GdkEvent,
5713 so we have a chance to act on the event before GTK. */
5714 static GdkFilterReturn
5715 event_handler_gdk (gxev, ev, data)
5716 GdkXEvent *gxev;
5717 GdkEvent *ev;
5718 gpointer data;
5720 XEvent *xev = (XEvent *) gxev;
5722 if (current_count >= 0)
5724 struct x_display_info *dpyinfo;
5726 dpyinfo = x_display_info_for_display (xev->xany.display);
5728 #ifdef HAVE_X_I18N
5729 /* Filter events for the current X input method.
5730 GTK calls XFilterEvent but not for key press and release,
5731 so we do it here. */
5732 if (xev->type == KeyPress || xev->type == KeyRelease)
5733 if (dpyinfo && x_filter_event (dpyinfo, xev))
5734 return GDK_FILTER_REMOVE;
5735 #endif
5737 if (! dpyinfo)
5738 current_finish = X_EVENT_NORMAL;
5739 else
5741 current_count +=
5742 handle_one_xevent (dpyinfo, xev, &current_finish,
5743 current_hold_quit);
5746 else
5747 current_finish = x_dispatch_event (xev, xev->xany.display);
5749 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5750 return GDK_FILTER_REMOVE;
5752 return GDK_FILTER_CONTINUE;
5754 #endif /* USE_GTK */
5757 /* Handles the XEvent EVENT on display DPYINFO.
5759 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5760 *FINISH is zero if caller should continue reading events.
5761 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5763 We return the number of characters stored into the buffer. */
5765 static int
5766 handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
5767 struct x_display_info *dpyinfo;
5768 XEvent *eventp;
5769 int *finish;
5770 struct input_event *hold_quit;
5772 union {
5773 struct input_event ie;
5774 struct selection_input_event sie;
5775 } inev;
5776 int count = 0;
5777 int do_help = 0;
5778 int nbytes = 0;
5779 struct frame *f = NULL;
5780 struct coding_system coding;
5781 XEvent event = *eventp;
5783 *finish = X_EVENT_NORMAL;
5785 EVENT_INIT (inev.ie);
5786 inev.ie.kind = NO_EVENT;
5787 inev.ie.arg = Qnil;
5789 switch (event.type)
5791 case ClientMessage:
5793 if (event.xclient.message_type
5794 == dpyinfo->Xatom_wm_protocols
5795 && event.xclient.format == 32)
5797 if (event.xclient.data.l[0]
5798 == dpyinfo->Xatom_wm_take_focus)
5800 /* Use x_any_window_to_frame because this
5801 could be the shell widget window
5802 if the frame has no title bar. */
5803 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5804 #ifdef HAVE_X_I18N
5805 /* Not quite sure this is needed -pd */
5806 if (f && FRAME_XIC (f))
5807 XSetICFocus (FRAME_XIC (f));
5808 #endif
5809 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5810 instructs the WM to set the input focus automatically for
5811 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5812 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5813 it has set the focus. So, XSetInputFocus below is not
5814 needed.
5816 The call to XSetInputFocus below has also caused trouble. In
5817 cases where the XSetInputFocus done by the WM and the one
5818 below are temporally close (on a fast machine), the call
5819 below can generate additional FocusIn events which confuse
5820 Emacs. */
5822 /* Since we set WM_TAKE_FOCUS, we must call
5823 XSetInputFocus explicitly. But not if f is null,
5824 since that might be an event for a deleted frame. */
5825 if (f)
5827 Display *d = event.xclient.display;
5828 /* Catch and ignore errors, in case window has been
5829 iconified by a window manager such as GWM. */
5830 x_catch_errors (d);
5831 XSetInputFocus (d, event.xclient.window,
5832 /* The ICCCM says this is
5833 the only valid choice. */
5834 RevertToParent,
5835 event.xclient.data.l[1]);
5836 /* This is needed to detect the error
5837 if there is an error. */
5838 XSync (d, False);
5839 x_uncatch_errors ();
5841 /* Not certain about handling scroll bars here */
5842 #endif /* 0 */
5843 goto done;
5846 if (event.xclient.data.l[0]
5847 == dpyinfo->Xatom_wm_save_yourself)
5849 /* Save state modify the WM_COMMAND property to
5850 something which can reinstate us. This notifies
5851 the session manager, who's looking for such a
5852 PropertyNotify. Can restart processing when
5853 a keyboard or mouse event arrives. */
5854 /* If we have a session manager, don't set this.
5855 KDE will then start two Emacsen, one for the
5856 session manager and one for this. */
5857 #ifdef HAVE_X_SM
5858 if (! x_session_have_connection ())
5859 #endif
5861 f = x_top_window_to_frame (dpyinfo,
5862 event.xclient.window);
5863 /* This is just so we only give real data once
5864 for a single Emacs process. */
5865 if (f == SELECTED_FRAME ())
5866 XSetCommand (FRAME_X_DISPLAY (f),
5867 event.xclient.window,
5868 initial_argv, initial_argc);
5869 else if (f)
5870 XSetCommand (FRAME_X_DISPLAY (f),
5871 event.xclient.window,
5872 0, 0);
5874 goto done;
5877 if (event.xclient.data.l[0]
5878 == dpyinfo->Xatom_wm_delete_window)
5880 f = x_any_window_to_frame (dpyinfo,
5881 event.xclient.window);
5882 if (!f)
5883 goto OTHER; /* May be a dialog that is to be removed */
5885 inev.ie.kind = DELETE_WINDOW_EVENT;
5886 XSETFRAME (inev.ie.frame_or_window, f);
5887 goto done;
5890 goto done;
5893 if (event.xclient.message_type
5894 == dpyinfo->Xatom_wm_configure_denied)
5896 goto done;
5899 if (event.xclient.message_type
5900 == dpyinfo->Xatom_wm_window_moved)
5902 int new_x, new_y;
5903 f = x_window_to_frame (dpyinfo, event.xclient.window);
5905 new_x = event.xclient.data.s[0];
5906 new_y = event.xclient.data.s[1];
5908 if (f)
5910 f->left_pos = new_x;
5911 f->top_pos = new_y;
5913 goto done;
5916 #ifdef HACK_EDITRES
5917 if (event.xclient.message_type
5918 == dpyinfo->Xatom_editres)
5920 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5921 if (f)
5922 _XEditResCheckMessages (f->output_data.x->widget, NULL,
5923 &event, NULL);
5924 goto done;
5926 #endif /* HACK_EDITRES */
5928 if ((event.xclient.message_type
5929 == dpyinfo->Xatom_DONE)
5930 || (event.xclient.message_type
5931 == dpyinfo->Xatom_PAGE))
5933 /* Ghostview job completed. Kill it. We could
5934 reply with "Next" if we received "Page", but we
5935 currently never do because we are interested in
5936 images, only, which should have 1 page. */
5937 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
5938 f = x_window_to_frame (dpyinfo, event.xclient.window);
5939 if (!f)
5940 goto OTHER;
5941 x_kill_gs_process (pixmap, f);
5942 expose_frame (f, 0, 0, 0, 0);
5943 goto done;
5946 #ifdef USE_TOOLKIT_SCROLL_BARS
5947 /* Scroll bar callbacks send a ClientMessage from which
5948 we construct an input_event. */
5949 if (event.xclient.message_type
5950 == dpyinfo->Xatom_Scrollbar)
5952 x_scroll_bar_to_input_event (&event, &inev.ie);
5953 *finish = X_EVENT_GOTO_OUT;
5954 goto done;
5956 #endif /* USE_TOOLKIT_SCROLL_BARS */
5958 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5959 if (!f)
5960 goto OTHER;
5961 if (x_handle_dnd_message (f, &event.xclient, dpyinfo, &inev.ie))
5962 *finish = X_EVENT_DROP;
5964 break;
5966 case SelectionNotify:
5967 last_user_time = event.xselection.time;
5968 #ifdef USE_X_TOOLKIT
5969 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
5970 goto OTHER;
5971 #endif /* not USE_X_TOOLKIT */
5972 x_handle_selection_notify (&event.xselection);
5973 break;
5975 case SelectionClear: /* Someone has grabbed ownership. */
5976 last_user_time = event.xselectionclear.time;
5977 #ifdef USE_X_TOOLKIT
5978 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
5979 goto OTHER;
5980 #endif /* USE_X_TOOLKIT */
5982 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
5984 inev.ie.kind = SELECTION_CLEAR_EVENT;
5985 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
5986 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
5987 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
5988 inev.ie.frame_or_window = Qnil;
5990 break;
5992 case SelectionRequest: /* Someone wants our selection. */
5993 last_user_time = event.xselectionrequest.time;
5994 #ifdef USE_X_TOOLKIT
5995 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
5996 goto OTHER;
5997 #endif /* USE_X_TOOLKIT */
5999 XSelectionRequestEvent *eventp
6000 = (XSelectionRequestEvent *) &event;
6002 inev.ie.kind = SELECTION_REQUEST_EVENT;
6003 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6004 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
6005 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6006 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
6007 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
6008 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6009 inev.ie.frame_or_window = Qnil;
6011 break;
6013 case PropertyNotify:
6014 last_user_time = event.xproperty.time;
6015 #if 0 /* This is plain wrong. In the case that we are waiting for a
6016 PropertyNotify used as an ACK in incremental selection
6017 transfer, the property will be on the receiver's window. */
6018 #if defined USE_X_TOOLKIT
6019 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
6020 goto OTHER;
6021 #endif
6022 #endif
6023 x_handle_property_notify (&event.xproperty);
6024 goto OTHER;
6026 case ReparentNotify:
6027 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
6028 if (f)
6030 int x, y;
6031 f->output_data.x->parent_desc = event.xreparent.parent;
6032 x_real_positions (f, &x, &y);
6033 f->left_pos = x;
6034 f->top_pos = y;
6036 /* Perhaps reparented due to a WM restart. Reset this. */
6037 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6038 FRAME_X_DISPLAY_INFO (f)->net_supported_window = 0;
6040 goto OTHER;
6042 case Expose:
6043 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6044 if (f)
6046 x_check_fullscreen (f);
6048 #ifdef USE_GTK
6049 /* This seems to be needed for GTK 2.6. */
6050 x_clear_area (event.xexpose.display,
6051 event.xexpose.window,
6052 event.xexpose.x, event.xexpose.y,
6053 event.xexpose.width, event.xexpose.height,
6054 FALSE);
6055 #endif
6056 if (f->async_visible == 0)
6058 f->async_visible = 1;
6059 f->async_iconified = 0;
6060 f->output_data.x->has_been_visible = 1;
6061 SET_FRAME_GARBAGED (f);
6063 else
6064 expose_frame (f,
6065 event.xexpose.x, event.xexpose.y,
6066 event.xexpose.width, event.xexpose.height);
6068 else
6070 #ifndef USE_TOOLKIT_SCROLL_BARS
6071 struct scroll_bar *bar;
6072 #endif
6073 #if defined USE_LUCID
6074 /* Submenus of the Lucid menu bar aren't widgets
6075 themselves, so there's no way to dispatch events
6076 to them. Recognize this case separately. */
6078 Widget widget
6079 = x_window_to_menu_bar (event.xexpose.window);
6080 if (widget)
6081 xlwmenu_redisplay (widget);
6083 #endif /* USE_LUCID */
6085 #ifdef USE_TOOLKIT_SCROLL_BARS
6086 /* Dispatch event to the widget. */
6087 goto OTHER;
6088 #else /* not USE_TOOLKIT_SCROLL_BARS */
6089 bar = x_window_to_scroll_bar (event.xexpose.display,
6090 event.xexpose.window);
6092 if (bar)
6093 x_scroll_bar_expose (bar, &event);
6094 #ifdef USE_X_TOOLKIT
6095 else
6096 goto OTHER;
6097 #endif /* USE_X_TOOLKIT */
6098 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6100 break;
6102 case GraphicsExpose: /* This occurs when an XCopyArea's
6103 source area was obscured or not
6104 available. */
6105 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6106 if (f)
6108 expose_frame (f,
6109 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6110 event.xgraphicsexpose.width,
6111 event.xgraphicsexpose.height);
6113 #ifdef USE_X_TOOLKIT
6114 else
6115 goto OTHER;
6116 #endif /* USE_X_TOOLKIT */
6117 break;
6119 case NoExpose: /* This occurs when an XCopyArea's
6120 source area was completely
6121 available. */
6122 break;
6124 case UnmapNotify:
6125 /* Redo the mouse-highlight after the tooltip has gone. */
6126 if (event.xmap.window == tip_window)
6128 tip_window = 0;
6129 redo_mouse_highlight ();
6132 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6133 if (f) /* F may no longer exist if
6134 the frame was deleted. */
6136 /* While a frame is unmapped, display generation is
6137 disabled; you don't want to spend time updating a
6138 display that won't ever be seen. */
6139 f->async_visible = 0;
6140 /* We can't distinguish, from the event, whether the window
6141 has become iconified or invisible. So assume, if it
6142 was previously visible, than now it is iconified.
6143 But x_make_frame_invisible clears both
6144 the visible flag and the iconified flag;
6145 and that way, we know the window is not iconified now. */
6146 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6148 f->async_iconified = 1;
6150 inev.ie.kind = ICONIFY_EVENT;
6151 XSETFRAME (inev.ie.frame_or_window, f);
6154 goto OTHER;
6156 case MapNotify:
6157 if (event.xmap.window == tip_window)
6158 /* The tooltip has been drawn already. Avoid
6159 the SET_FRAME_GARBAGED below. */
6160 goto OTHER;
6162 /* We use x_top_window_to_frame because map events can
6163 come for sub-windows and they don't mean that the
6164 frame is visible. */
6165 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6166 if (f)
6168 /* wait_reading_process_output will notice this and update
6169 the frame's display structures.
6170 If we where iconified, we should not set garbaged,
6171 because that stops redrawing on Expose events. This looks
6172 bad if we are called from a recursive event loop
6173 (x_dispatch_event), for example when a dialog is up. */
6174 if (! f->async_iconified)
6175 SET_FRAME_GARBAGED (f);
6177 f->async_visible = 1;
6178 f->async_iconified = 0;
6179 f->output_data.x->has_been_visible = 1;
6181 if (f->iconified)
6183 inev.ie.kind = DEICONIFY_EVENT;
6184 XSETFRAME (inev.ie.frame_or_window, f);
6186 else if (! NILP (Vframe_list)
6187 && ! NILP (XCDR (Vframe_list)))
6188 /* Force a redisplay sooner or later
6189 to update the frame titles
6190 in case this is the second frame. */
6191 record_asynch_buffer_change ();
6193 goto OTHER;
6195 case KeyPress:
6197 last_user_time = event.xkey.time;
6198 ignore_next_mouse_click_timeout = 0;
6200 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6201 /* Dispatch KeyPress events when in menu. */
6202 if (popup_activated ())
6203 goto OTHER;
6204 #endif
6206 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6208 /* If mouse-highlight is an integer, input clears out
6209 mouse highlighting. */
6210 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
6211 && (f == 0
6212 || !EQ (f->tool_bar_window, dpyinfo->mouse_face_window)))
6214 clear_mouse_face (dpyinfo);
6215 dpyinfo->mouse_face_hidden = 1;
6218 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6219 if (f == 0)
6221 /* Scroll bars consume key events, but we want
6222 the keys to go to the scroll bar's frame. */
6223 Widget widget = XtWindowToWidget (dpyinfo->display,
6224 event.xkey.window);
6225 if (widget && XmIsScrollBar (widget))
6227 widget = XtParent (widget);
6228 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6231 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6233 if (f != 0)
6235 KeySym keysym, orig_keysym;
6236 /* al%imercury@uunet.uu.net says that making this 81
6237 instead of 80 fixed a bug whereby meta chars made
6238 his Emacs hang.
6240 It seems that some version of XmbLookupString has
6241 a bug of not returning XBufferOverflow in
6242 status_return even if the input is too long to
6243 fit in 81 bytes. So, we must prepare sufficient
6244 bytes for copy_buffer. 513 bytes (256 chars for
6245 two-byte character set) seems to be a fairly good
6246 approximation. -- 2000.8.10 handa@etl.go.jp */
6247 unsigned char copy_buffer[513];
6248 unsigned char *copy_bufptr = copy_buffer;
6249 int copy_bufsiz = sizeof (copy_buffer);
6250 int modifiers;
6251 Lisp_Object coding_system = Qlatin_1;
6252 Lisp_Object c;
6254 #ifdef USE_GTK
6255 /* Don't pass keys to GTK. A Tab will shift focus to the
6256 tool bar in GTK 2.4. Keys will still go to menus and
6257 dialogs because in that case popup_activated is TRUE
6258 (see above). */
6259 *finish = X_EVENT_DROP;
6260 #endif
6262 event.xkey.state
6263 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6264 extra_keyboard_modifiers);
6265 modifiers = event.xkey.state;
6267 /* This will have to go some day... */
6269 /* make_lispy_event turns chars into control chars.
6270 Don't do it here because XLookupString is too eager. */
6271 event.xkey.state &= ~ControlMask;
6272 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6273 | dpyinfo->super_mod_mask
6274 | dpyinfo->hyper_mod_mask
6275 | dpyinfo->alt_mod_mask);
6277 /* In case Meta is ComposeCharacter,
6278 clear its status. According to Markus Ehrnsperger
6279 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6280 this enables ComposeCharacter to work whether or
6281 not it is combined with Meta. */
6282 if (modifiers & dpyinfo->meta_mod_mask)
6283 bzero (&compose_status, sizeof (compose_status));
6285 #ifdef HAVE_X_I18N
6286 if (FRAME_XIC (f))
6288 Status status_return;
6290 coding_system = Vlocale_coding_system;
6291 nbytes = XmbLookupString (FRAME_XIC (f),
6292 &event.xkey, copy_bufptr,
6293 copy_bufsiz, &keysym,
6294 &status_return);
6295 if (status_return == XBufferOverflow)
6297 copy_bufsiz = nbytes + 1;
6298 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6299 nbytes = XmbLookupString (FRAME_XIC (f),
6300 &event.xkey, copy_bufptr,
6301 copy_bufsiz, &keysym,
6302 &status_return);
6304 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6305 #if 0 && defined X_HAVE_UTF8_STRING
6306 else if (status_return == XLookupKeySym)
6307 { /* Try again but with utf-8. */
6308 coding_system = Qutf_8;
6309 nbytes = Xutf8LookupString (FRAME_XIC (f),
6310 &event.xkey, copy_bufptr,
6311 copy_bufsiz, &keysym,
6312 &status_return);
6313 if (status_return == XBufferOverflow)
6315 copy_bufsiz = nbytes + 1;
6316 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6317 nbytes = Xutf8LookupString (FRAME_XIC (f),
6318 &event.xkey,
6319 copy_bufptr,
6320 copy_bufsiz, &keysym,
6321 &status_return);
6324 #endif
6326 if (status_return == XLookupNone)
6327 break;
6328 else if (status_return == XLookupChars)
6330 keysym = NoSymbol;
6331 modifiers = 0;
6333 else if (status_return != XLookupKeySym
6334 && status_return != XLookupBoth)
6335 abort ();
6337 else
6338 nbytes = XLookupString (&event.xkey, copy_bufptr,
6339 copy_bufsiz, &keysym,
6340 &compose_status);
6341 #else
6342 nbytes = XLookupString (&event.xkey, copy_bufptr,
6343 copy_bufsiz, &keysym,
6344 &compose_status);
6345 #endif
6347 /* If not using XIM/XIC, and a compose sequence is in progress,
6348 we break here. Otherwise, chars_matched is always 0. */
6349 if (compose_status.chars_matched > 0 && nbytes == 0)
6350 break;
6352 bzero (&compose_status, sizeof (compose_status));
6353 orig_keysym = keysym;
6355 /* Common for all keysym input events. */
6356 XSETFRAME (inev.ie.frame_or_window, f);
6357 inev.ie.modifiers
6358 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), modifiers);
6359 inev.ie.timestamp = event.xkey.time;
6361 /* First deal with keysyms which have defined
6362 translations to characters. */
6363 if (keysym >= 32 && keysym < 128)
6364 /* Avoid explicitly decoding each ASCII character. */
6366 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6367 inev.ie.code = keysym;
6368 goto done_keysym;
6371 /* Keysyms directly mapped to supported Unicode characters. */
6372 if ((keysym >= 0x01000000 && keysym <= 0x010033ff)
6373 || (keysym >= 0x0100e000 && keysym <= 0x0100ffff))
6375 int code = keysym & 0xFFFF, charset_id, c1, c2;
6377 if (code < 0x80)
6379 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6380 inev.ie.code = code;
6382 else if (code < 0x100)
6384 if (code < 0xA0)
6385 charset_id = CHARSET_8_BIT_CONTROL;
6386 else
6387 charset_id = charset_latin_iso8859_1;
6388 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6389 inev.ie.code = MAKE_CHAR (charset_id, code, 0);
6391 else
6393 if (code < 0x2500)
6394 charset_id = charset_mule_unicode_0100_24ff,
6395 code -= 0x100;
6396 else if (code < 0xE000)
6397 charset_id = charset_mule_unicode_2500_33ff,
6398 code -= 0x2500;
6399 else
6400 charset_id = charset_mule_unicode_e000_ffff,
6401 code -= 0xE000;
6402 c1 = (code / 96) + 32, c2 = (code % 96) + 32;
6403 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6404 inev.ie.code = MAKE_CHAR (charset_id, c1, c2);
6406 goto done_keysym;
6409 /* Now non-ASCII. */
6410 if (HASH_TABLE_P (Vx_keysym_table)
6411 && (NATNUMP (c = Fgethash (make_number (keysym),
6412 Vx_keysym_table,
6413 Qnil))))
6415 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6416 ? ASCII_KEYSTROKE_EVENT
6417 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6418 inev.ie.code = XFASTINT (c);
6419 goto done_keysym;
6422 /* Random non-modifier sorts of keysyms. */
6423 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6424 || keysym == XK_Delete
6425 #ifdef XK_ISO_Left_Tab
6426 || (keysym >= XK_ISO_Left_Tab
6427 && keysym <= XK_ISO_Enter)
6428 #endif
6429 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6430 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6431 #ifdef HPUX
6432 /* This recognizes the "extended function
6433 keys". It seems there's no cleaner way.
6434 Test IsModifierKey to avoid handling
6435 mode_switch incorrectly. */
6436 || ((unsigned) (keysym) >= XK_Select
6437 && (unsigned)(keysym) < XK_KP_Space)
6438 #endif
6439 #ifdef XK_dead_circumflex
6440 || orig_keysym == XK_dead_circumflex
6441 #endif
6442 #ifdef XK_dead_grave
6443 || orig_keysym == XK_dead_grave
6444 #endif
6445 #ifdef XK_dead_tilde
6446 || orig_keysym == XK_dead_tilde
6447 #endif
6448 #ifdef XK_dead_diaeresis
6449 || orig_keysym == XK_dead_diaeresis
6450 #endif
6451 #ifdef XK_dead_macron
6452 || orig_keysym == XK_dead_macron
6453 #endif
6454 #ifdef XK_dead_degree
6455 || orig_keysym == XK_dead_degree
6456 #endif
6457 #ifdef XK_dead_acute
6458 || orig_keysym == XK_dead_acute
6459 #endif
6460 #ifdef XK_dead_cedilla
6461 || orig_keysym == XK_dead_cedilla
6462 #endif
6463 #ifdef XK_dead_breve
6464 || orig_keysym == XK_dead_breve
6465 #endif
6466 #ifdef XK_dead_ogonek
6467 || orig_keysym == XK_dead_ogonek
6468 #endif
6469 #ifdef XK_dead_caron
6470 || orig_keysym == XK_dead_caron
6471 #endif
6472 #ifdef XK_dead_doubleacute
6473 || orig_keysym == XK_dead_doubleacute
6474 #endif
6475 #ifdef XK_dead_abovedot
6476 || orig_keysym == XK_dead_abovedot
6477 #endif
6478 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6479 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6480 /* Any "vendor-specific" key is ok. */
6481 || (orig_keysym & (1 << 28))
6482 || (keysym != NoSymbol && nbytes == 0))
6483 && ! (IsModifierKey (orig_keysym)
6484 /* The symbols from XK_ISO_Lock
6485 to XK_ISO_Last_Group_Lock
6486 don't have real modifiers but
6487 should be treated similarly to
6488 Mode_switch by Emacs. */
6489 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6490 || ((unsigned)(orig_keysym)
6491 >= XK_ISO_Lock
6492 && (unsigned)(orig_keysym)
6493 <= XK_ISO_Last_Group_Lock)
6494 #endif
6497 STORE_KEYSYM_FOR_DEBUG (keysym);
6498 /* make_lispy_event will convert this to a symbolic
6499 key. */
6500 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6501 inev.ie.code = keysym;
6502 goto done_keysym;
6505 { /* Raw bytes, not keysym. */
6506 register int i;
6507 register int c;
6508 int nchars, len;
6510 /* The input should be decoded with `coding_system'
6511 which depends on which X*LookupString function
6512 we used just above and the locale. */
6513 setup_coding_system (coding_system, &coding);
6514 coding.src_multibyte = 0;
6515 coding.dst_multibyte = 1;
6516 /* The input is converted to events, thus we can't
6517 handle composition. Anyway, there's no XIM that
6518 gives us composition information. */
6519 coding.composing = COMPOSITION_DISABLED;
6521 if (nbytes > 0)
6523 /* Decode the input data. */
6524 int require;
6525 unsigned char *p;
6527 for (i = 0; i < nbytes; i++)
6529 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6532 require = decoding_buffer_size (&coding, nbytes);
6533 p = (unsigned char *) alloca (require);
6534 coding.mode |= CODING_MODE_LAST_BLOCK;
6535 /* We explicitly disable composition handling because
6536 key data should not contain any composition sequence. */
6537 coding.composing = COMPOSITION_DISABLED;
6538 decode_coding (&coding, copy_bufptr, p, nbytes, require);
6539 nbytes = coding.produced;
6540 nchars = coding.produced_char;
6541 copy_bufptr = p;
6543 /* Convert the input data to a sequence of
6544 character events. */
6545 for (i = 0; i < nbytes; i += len)
6547 if (nchars == nbytes)
6548 c = copy_bufptr[i], len = 1;
6549 else
6550 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
6551 nbytes - i, len);
6552 inev.ie.kind = (SINGLE_BYTE_CHAR_P (c)
6553 ? ASCII_KEYSTROKE_EVENT
6554 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6555 inev.ie.code = c;
6556 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6560 /* Previous code updated count by nchars rather than nbytes,
6561 but that seems bogus to me. ++kfs */
6562 count += nbytes;
6564 inev.ie.kind = NO_EVENT; /* Already stored above. */
6566 if (keysym == NoSymbol)
6567 break;
6570 done_keysym:
6571 #ifdef HAVE_X_I18N
6572 /* Don't dispatch this event since XtDispatchEvent calls
6573 XFilterEvent, and two calls in a row may freeze the
6574 client. */
6575 break;
6576 #else
6577 goto OTHER;
6578 #endif
6580 case KeyRelease:
6581 last_user_time = event.xkey.time;
6582 #ifdef HAVE_X_I18N
6583 /* Don't dispatch this event since XtDispatchEvent calls
6584 XFilterEvent, and two calls in a row may freeze the
6585 client. */
6586 break;
6587 #else
6588 goto OTHER;
6589 #endif
6591 case EnterNotify:
6592 last_user_time = event.xcrossing.time;
6593 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6595 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6597 if (f && x_mouse_click_focus_ignore_position)
6598 ignore_next_mouse_click_timeout = event.xmotion.time + 200;
6600 #if 0
6601 if (event.xcrossing.focus)
6603 /* Avoid nasty pop/raise loops. */
6604 if (f && (!(f->auto_raise)
6605 || !(f->auto_lower)
6606 || (event.xcrossing.time - enter_timestamp) > 500))
6608 x_new_focus_frame (dpyinfo, f);
6609 enter_timestamp = event.xcrossing.time;
6612 else if (f == dpyinfo->x_focus_frame)
6613 x_new_focus_frame (dpyinfo, 0);
6614 #endif
6616 /* EnterNotify counts as mouse movement,
6617 so update things that depend on mouse position. */
6618 if (f && !f->output_data.x->hourglass_p)
6619 note_mouse_movement (f, &event.xmotion);
6620 #ifdef USE_GTK
6621 /* We may get an EnterNotify on the buttons in the toolbar. In that
6622 case we moved out of any highlighted area and need to note this. */
6623 if (!f && last_mouse_glyph_frame)
6624 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6625 #endif
6626 goto OTHER;
6628 case FocusIn:
6629 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6630 goto OTHER;
6632 case LeaveNotify:
6633 last_user_time = event.xcrossing.time;
6634 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6636 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6637 if (f)
6639 if (f == dpyinfo->mouse_face_mouse_frame)
6641 /* If we move outside the frame, then we're
6642 certainly no longer on any text in the frame. */
6643 clear_mouse_face (dpyinfo);
6644 dpyinfo->mouse_face_mouse_frame = 0;
6647 /* Generate a nil HELP_EVENT to cancel a help-echo.
6648 Do it only if there's something to cancel.
6649 Otherwise, the startup message is cleared when
6650 the mouse leaves the frame. */
6651 if (any_help_event_p)
6652 do_help = -1;
6654 #ifdef USE_GTK
6655 /* See comment in EnterNotify above */
6656 else if (last_mouse_glyph_frame)
6657 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6658 #endif
6659 goto OTHER;
6661 case FocusOut:
6662 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6663 goto OTHER;
6665 case MotionNotify:
6667 last_user_time = event.xmotion.time;
6668 previous_help_echo_string = help_echo_string;
6669 help_echo_string = Qnil;
6671 if (dpyinfo->grabbed && last_mouse_frame
6672 && FRAME_LIVE_P (last_mouse_frame))
6673 f = last_mouse_frame;
6674 else
6675 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6677 if (dpyinfo->mouse_face_hidden)
6679 dpyinfo->mouse_face_hidden = 0;
6680 clear_mouse_face (dpyinfo);
6683 if (f)
6686 /* Generate SELECT_WINDOW_EVENTs when needed. */
6687 if (!NILP (Vmouse_autoselect_window))
6689 Lisp_Object window;
6691 window = window_from_coordinates (f,
6692 event.xmotion.x, event.xmotion.y,
6693 0, 0, 0, 0);
6695 /* Window will be selected only when it is not selected now and
6696 last mouse movement event was not in it. Minibuffer window
6697 will be selected only when it is active. */
6698 if (WINDOWP (window)
6699 && !EQ (window, last_window)
6700 && !EQ (window, selected_window)
6701 /* For click-to-focus window managers
6702 create event iff we don't leave the
6703 selected frame. */
6704 && (focus_follows_mouse
6705 || (EQ (XWINDOW (window)->frame,
6706 XWINDOW (selected_window)->frame))))
6708 inev.ie.kind = SELECT_WINDOW_EVENT;
6709 inev.ie.frame_or_window = window;
6712 last_window=window;
6714 if (!note_mouse_movement (f, &event.xmotion))
6715 help_echo_string = previous_help_echo_string;
6717 else
6719 #ifndef USE_TOOLKIT_SCROLL_BARS
6720 struct scroll_bar *bar
6721 = x_window_to_scroll_bar (event.xmotion.display,
6722 event.xmotion.window);
6724 if (bar)
6725 x_scroll_bar_note_movement (bar, &event);
6726 #endif /* USE_TOOLKIT_SCROLL_BARS */
6728 /* If we move outside the frame, then we're
6729 certainly no longer on any text in the frame. */
6730 clear_mouse_face (dpyinfo);
6733 /* If the contents of the global variable help_echo_string
6734 has changed, generate a HELP_EVENT. */
6735 if (!NILP (help_echo_string)
6736 || !NILP (previous_help_echo_string))
6737 do_help = 1;
6738 goto OTHER;
6741 case ConfigureNotify:
6742 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6743 if (f)
6745 #ifndef USE_X_TOOLKIT
6746 #ifdef USE_GTK
6747 xg_resize_widgets (f, event.xconfigure.width,
6748 event.xconfigure.height);
6749 #else /* not USE_GTK */
6750 /* If there is a pending resize for fullscreen, don't
6751 do this one, the right one will come later.
6752 The toolkit version doesn't seem to need this, but we
6753 need to reset it below. */
6754 int dont_resize
6755 = ((f->want_fullscreen & FULLSCREEN_WAIT)
6756 && f->new_text_cols != 0);
6757 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6758 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6760 if (dont_resize)
6761 goto OTHER;
6763 /* In the toolkit version, change_frame_size
6764 is called by the code that handles resizing
6765 of the EmacsFrame widget. */
6767 /* Even if the number of character rows and columns has
6768 not changed, the font size may have changed, so we need
6769 to check the pixel dimensions as well. */
6770 if (columns != FRAME_COLS (f)
6771 || rows != FRAME_LINES (f)
6772 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6773 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6775 change_frame_size (f, rows, columns, 0, 1, 0);
6776 SET_FRAME_GARBAGED (f);
6777 cancel_mouse_face (f);
6779 #endif /* not USE_GTK */
6780 #endif
6782 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6783 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
6785 #ifdef USE_GTK
6786 /* GTK creates windows but doesn't map them.
6787 Only get real positions and check fullscreen when mapped. */
6788 if (FRAME_GTK_OUTER_WIDGET (f)
6789 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6790 #endif
6792 x_real_positions (f, &f->left_pos, &f->top_pos);
6794 if (f->want_fullscreen & FULLSCREEN_WAIT)
6795 f->want_fullscreen &= ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
6798 #ifdef HAVE_X_I18N
6799 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6800 xic_set_statusarea (f);
6801 #endif
6803 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6805 /* Since the WM decorations come below top_pos now,
6806 we must put them below top_pos in the future. */
6807 f->win_gravity = NorthWestGravity;
6808 x_wm_set_size_hint (f, (long) 0, 0);
6811 goto OTHER;
6813 case ButtonRelease:
6814 case ButtonPress:
6816 /* If we decide we want to generate an event to be seen
6817 by the rest of Emacs, we put it here. */
6818 int tool_bar_p = 0;
6820 bzero (&compose_status, sizeof (compose_status));
6821 last_mouse_glyph_frame = 0;
6822 last_user_time = event.xbutton.time;
6824 if (dpyinfo->grabbed
6825 && last_mouse_frame
6826 && FRAME_LIVE_P (last_mouse_frame))
6827 f = last_mouse_frame;
6828 else
6829 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6831 if (f)
6833 /* Is this in the tool-bar? */
6834 if (WINDOWP (f->tool_bar_window)
6835 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6837 Lisp_Object window;
6838 int x = event.xbutton.x;
6839 int y = event.xbutton.y;
6841 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
6842 tool_bar_p = EQ (window, f->tool_bar_window);
6844 if (tool_bar_p && event.xbutton.button < 4)
6846 handle_tool_bar_click (f, x, y,
6847 event.xbutton.type == ButtonPress,
6848 x_x_to_emacs_modifiers (dpyinfo,
6849 event.xbutton.state));
6853 if (!tool_bar_p)
6854 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6855 if (! popup_activated ())
6856 #endif
6858 if (ignore_next_mouse_click_timeout)
6860 if (event.type == ButtonPress
6861 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
6863 ignore_next_mouse_click_timeout = 0;
6864 construct_mouse_click (&inev.ie, &event.xbutton, f);
6866 if (event.type == ButtonRelease)
6867 ignore_next_mouse_click_timeout = 0;
6869 else
6870 construct_mouse_click (&inev.ie, &event.xbutton, f);
6873 else
6875 struct scroll_bar *bar
6876 = x_window_to_scroll_bar (event.xbutton.display,
6877 event.xbutton.window);
6879 #ifdef USE_TOOLKIT_SCROLL_BARS
6880 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6881 scroll bars. */
6882 if (bar && event.xbutton.state & ControlMask)
6884 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6885 *finish = X_EVENT_DROP;
6887 #else /* not USE_TOOLKIT_SCROLL_BARS */
6888 if (bar)
6889 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6890 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6893 if (event.type == ButtonPress)
6895 dpyinfo->grabbed |= (1 << event.xbutton.button);
6896 last_mouse_frame = f;
6898 if (!tool_bar_p)
6899 last_tool_bar_item = -1;
6901 else
6902 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6904 /* Ignore any mouse motion that happened before this event;
6905 any subsequent mouse-movement Emacs events should reflect
6906 only motion after the ButtonPress/Release. */
6907 if (f != 0)
6908 f->mouse_moved = 0;
6910 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6911 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6912 /* For a down-event in the menu bar,
6913 don't pass it to Xt right now.
6914 Instead, save it away
6915 and we will pass it to Xt from kbd_buffer_get_event.
6916 That way, we can run some Lisp code first. */
6917 if (
6918 #ifdef USE_GTK
6919 ! popup_activated ()
6921 #endif
6922 f && event.type == ButtonPress
6923 /* Verify the event is really within the menu bar
6924 and not just sent to it due to grabbing. */
6925 && event.xbutton.x >= 0
6926 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
6927 && event.xbutton.y >= 0
6928 && event.xbutton.y < f->output_data.x->menubar_height
6929 && event.xbutton.same_screen)
6931 SET_SAVED_BUTTON_EVENT;
6932 XSETFRAME (last_mouse_press_frame, f);
6933 #ifdef USE_GTK
6934 *finish = X_EVENT_DROP;
6935 #endif
6937 else if (event.type == ButtonPress)
6939 last_mouse_press_frame = Qnil;
6940 goto OTHER;
6943 #ifdef USE_MOTIF /* This should do not harm for Lucid,
6944 but I am trying to be cautious. */
6945 else if (event.type == ButtonRelease)
6947 if (!NILP (last_mouse_press_frame))
6949 f = XFRAME (last_mouse_press_frame);
6950 if (f->output_data.x)
6951 SET_SAVED_BUTTON_EVENT;
6953 else
6954 goto OTHER;
6956 #endif /* USE_MOTIF */
6957 else
6958 goto OTHER;
6959 #endif /* USE_X_TOOLKIT || USE_GTK */
6961 break;
6963 case CirculateNotify:
6964 goto OTHER;
6966 case CirculateRequest:
6967 goto OTHER;
6969 case VisibilityNotify:
6970 goto OTHER;
6972 case MappingNotify:
6973 /* Someone has changed the keyboard mapping - update the
6974 local cache. */
6975 switch (event.xmapping.request)
6977 case MappingModifier:
6978 x_find_modifier_meanings (dpyinfo);
6979 /* This is meant to fall through. */
6980 case MappingKeyboard:
6981 XRefreshKeyboardMapping (&event.xmapping);
6983 goto OTHER;
6985 default:
6986 OTHER:
6987 #ifdef USE_X_TOOLKIT
6988 BLOCK_INPUT;
6989 if (*finish != X_EVENT_DROP)
6990 XtDispatchEvent (&event);
6991 UNBLOCK_INPUT;
6992 #endif /* USE_X_TOOLKIT */
6993 break;
6996 done:
6997 if (inev.ie.kind != NO_EVENT)
6999 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
7000 count++;
7003 if (do_help
7004 && !(hold_quit && hold_quit->kind != NO_EVENT))
7006 Lisp_Object frame;
7008 if (f)
7009 XSETFRAME (frame, f);
7010 else
7011 frame = Qnil;
7013 if (do_help > 0)
7015 any_help_event_p = 1;
7016 gen_help_event (help_echo_string, frame, help_echo_window,
7017 help_echo_object, help_echo_pos);
7019 else
7021 help_echo_string = Qnil;
7022 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
7024 count++;
7027 *eventp = event;
7028 return count;
7032 /* Handles the XEvent EVENT on display DISPLAY.
7033 This is used for event loops outside the normal event handling,
7034 i.e. looping while a popup menu or a dialog is posted.
7036 Returns the value handle_one_xevent sets in the finish argument. */
7038 x_dispatch_event (event, display)
7039 XEvent *event;
7040 Display *display;
7042 struct x_display_info *dpyinfo;
7043 int finish = X_EVENT_NORMAL;
7045 dpyinfo = x_display_info_for_display (display);
7047 if (dpyinfo)
7048 handle_one_xevent (dpyinfo, event, &finish, 0);
7050 return finish;
7054 /* Read events coming from the X server.
7055 This routine is called by the SIGIO handler.
7056 We return as soon as there are no more events to be read.
7058 We return the number of characters stored into the buffer,
7059 thus pretending to be `read'.
7061 EXPECTED is nonzero if the caller knows input is available. */
7063 static int
7064 XTread_socket (terminal, expected, hold_quit)
7065 struct terminal *terminal;
7066 int expected;
7067 struct input_event *hold_quit;
7069 int count = 0;
7070 XEvent event;
7071 int event_found = 0;
7072 struct x_display_info *dpyinfo;
7074 if (interrupt_input_blocked)
7076 interrupt_input_pending = 1;
7077 return -1;
7080 interrupt_input_pending = 0;
7081 BLOCK_INPUT;
7083 /* So people can tell when we have read the available input. */
7084 input_signal_count++;
7086 ++handling_signal;
7088 #ifdef HAVE_X_SM
7089 /* Only check session manager input for the primary display. */
7090 if (terminal->id == 1 && x_session_have_connection ())
7092 struct input_event inev;
7093 BLOCK_INPUT;
7094 /* We don't need to EVENT_INIT (inev) here, as
7095 x_session_check_input copies an entire input_event. */
7096 if (x_session_check_input (&inev))
7098 kbd_buffer_store_event_hold (&inev, hold_quit);
7099 count++;
7101 UNBLOCK_INPUT;
7103 #endif
7105 /* For debugging, this gives a way to fake an I/O error. */
7106 if (terminal->display_info.x == XTread_socket_fake_io_error)
7108 XTread_socket_fake_io_error = 0;
7109 x_io_error_quitter (dpyinfo->display);
7112 #if 0 /* This loop is a noop now. */
7113 /* Find the display we are supposed to read input for.
7114 It's the one communicating on descriptor SD. */
7115 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
7117 #if 0 /* This ought to be unnecessary; let's verify it. */
7118 #ifdef FIOSNBIO
7119 /* If available, Xlib uses FIOSNBIO to make the socket
7120 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
7121 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
7122 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7123 fcntl (dpyinfo->connection, F_SETFL, 0);
7124 #endif /* ! defined (FIOSNBIO) */
7125 #endif
7127 #if 0 /* This code can't be made to work, with multiple displays,
7128 and appears not to be used on any system any more.
7129 Also keyboard.c doesn't turn O_NDELAY on and off
7130 for X connections. */
7131 #ifndef SIGIO
7132 #ifndef HAVE_SELECT
7133 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
7135 extern int read_alarm_should_throw;
7136 read_alarm_should_throw = 1;
7137 XPeekEvent (dpyinfo->display, &event);
7138 read_alarm_should_throw = 0;
7140 #endif /* HAVE_SELECT */
7141 #endif /* SIGIO */
7142 #endif
7144 #endif
7146 #ifndef USE_GTK
7147 while (XPending (terminal->display_info.x->display))
7149 int finish;
7151 XNextEvent (terminal->display_info.x->display, &event);
7153 #ifdef HAVE_X_I18N
7154 /* Filter events for the current X input method. */
7155 if (x_filter_event (terminal->display_info.x, &event))
7156 break;
7157 #endif
7158 event_found = 1;
7160 count += handle_one_xevent (terminal->display_info.x,
7161 &event, &finish, hold_quit);
7163 if (finish == X_EVENT_GOTO_OUT)
7164 goto out;
7167 #else /* USE_GTK */
7169 /* For GTK we must use the GTK event loop. But XEvents gets passed
7170 to our filter function above, and then to the big event switch.
7171 We use a bunch of globals to communicate with our filter function,
7172 that is kind of ugly, but it works.
7174 There is no way to do one display at the time, GTK just does events
7175 from all displays. */
7177 while (gtk_events_pending ())
7179 current_count = count;
7180 current_hold_quit = hold_quit;
7182 gtk_main_iteration ();
7184 count = current_count;
7185 current_count = -1;
7186 current_hold_quit = 0;
7188 if (current_finish == X_EVENT_GOTO_OUT)
7189 break;
7191 #endif /* USE_GTK */
7193 out:;
7195 /* On some systems, an X bug causes Emacs to get no more events
7196 when the window is destroyed. Detect that. (1994.) */
7197 if (! event_found)
7199 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7200 One XNOOP in 100 loops will make Emacs terminate.
7201 B. Bretthauer, 1994 */
7202 x_noop_count++;
7203 if (x_noop_count >= 100)
7205 x_noop_count=0;
7207 if (next_noop_dpyinfo == 0)
7208 next_noop_dpyinfo = x_display_list;
7210 XNoOp (next_noop_dpyinfo->display);
7212 /* Each time we get here, cycle through the displays now open. */
7213 next_noop_dpyinfo = next_noop_dpyinfo->next;
7217 /* If the focus was just given to an auto-raising frame,
7218 raise it now. */
7219 /* ??? This ought to be able to handle more than one such frame. */
7220 if (pending_autoraise_frame)
7222 x_raise_frame (pending_autoraise_frame);
7223 pending_autoraise_frame = 0;
7226 --handling_signal;
7227 UNBLOCK_INPUT;
7229 return count;
7235 /***********************************************************************
7236 Text Cursor
7237 ***********************************************************************/
7239 /* Set clipping for output in glyph row ROW. W is the window in which
7240 we operate. GC is the graphics context to set clipping in.
7242 ROW may be a text row or, e.g., a mode line. Text rows must be
7243 clipped to the interior of the window dedicated to text display,
7244 mode lines must be clipped to the whole window. */
7246 static void
7247 x_clip_to_row (w, row, area, gc)
7248 struct window *w;
7249 struct glyph_row *row;
7250 int area;
7251 GC gc;
7253 struct frame *f = XFRAME (WINDOW_FRAME (w));
7254 XRectangle clip_rect;
7255 int window_x, window_y, window_width;
7257 window_box (w, area, &window_x, &window_y, &window_width, 0);
7259 clip_rect.x = window_x;
7260 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7261 clip_rect.y = max (clip_rect.y, window_y);
7262 clip_rect.width = window_width;
7263 clip_rect.height = row->visible_height;
7265 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7269 /* Draw a hollow box cursor on window W in glyph row ROW. */
7271 static void
7272 x_draw_hollow_cursor (w, row)
7273 struct window *w;
7274 struct glyph_row *row;
7276 struct frame *f = XFRAME (WINDOW_FRAME (w));
7277 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7278 Display *dpy = FRAME_X_DISPLAY (f);
7279 int x, y, wd, h;
7280 XGCValues xgcv;
7281 struct glyph *cursor_glyph;
7282 GC gc;
7284 /* Get the glyph the cursor is on. If we can't tell because
7285 the current matrix is invalid or such, give up. */
7286 cursor_glyph = get_phys_cursor_glyph (w);
7287 if (cursor_glyph == NULL)
7288 return;
7290 /* Compute frame-relative coordinates for phys cursor. */
7291 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7292 wd = w->phys_cursor_width;
7294 /* The foreground of cursor_gc is typically the same as the normal
7295 background color, which can cause the cursor box to be invisible. */
7296 xgcv.foreground = f->output_data.x->cursor_pixel;
7297 if (dpyinfo->scratch_cursor_gc)
7298 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7299 else
7300 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7301 GCForeground, &xgcv);
7302 gc = dpyinfo->scratch_cursor_gc;
7304 /* Set clipping, draw the rectangle, and reset clipping again. */
7305 x_clip_to_row (w, row, TEXT_AREA, gc);
7306 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7307 XSetClipMask (dpy, gc, None);
7311 /* Draw a bar cursor on window W in glyph row ROW.
7313 Implementation note: One would like to draw a bar cursor with an
7314 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7315 Unfortunately, I didn't find a font yet that has this property set.
7316 --gerd. */
7318 static void
7319 x_draw_bar_cursor (w, row, width, kind)
7320 struct window *w;
7321 struct glyph_row *row;
7322 int width;
7323 enum text_cursor_kinds kind;
7325 struct frame *f = XFRAME (w->frame);
7326 struct glyph *cursor_glyph;
7328 /* If cursor is out of bounds, don't draw garbage. This can happen
7329 in mini-buffer windows when switching between echo area glyphs
7330 and mini-buffer. */
7331 cursor_glyph = get_phys_cursor_glyph (w);
7332 if (cursor_glyph == NULL)
7333 return;
7335 /* If on an image, draw like a normal cursor. That's usually better
7336 visible than drawing a bar, esp. if the image is large so that
7337 the bar might not be in the window. */
7338 if (cursor_glyph->type == IMAGE_GLYPH)
7340 struct glyph_row *row;
7341 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7342 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7344 else
7346 Display *dpy = FRAME_X_DISPLAY (f);
7347 Window window = FRAME_X_WINDOW (f);
7348 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7349 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7350 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7351 XGCValues xgcv;
7353 /* If the glyph's background equals the color we normally draw
7354 the bar cursor in, the bar cursor in its normal color is
7355 invisible. Use the glyph's foreground color instead in this
7356 case, on the assumption that the glyph's colors are chosen so
7357 that the glyph is legible. */
7358 if (face->background == f->output_data.x->cursor_pixel)
7359 xgcv.background = xgcv.foreground = face->foreground;
7360 else
7361 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7362 xgcv.graphics_exposures = 0;
7364 if (gc)
7365 XChangeGC (dpy, gc, mask, &xgcv);
7366 else
7368 gc = XCreateGC (dpy, window, mask, &xgcv);
7369 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7372 if (width < 0)
7373 width = FRAME_CURSOR_WIDTH (f);
7374 width = min (cursor_glyph->pixel_width, width);
7376 w->phys_cursor_width = width;
7377 x_clip_to_row (w, row, TEXT_AREA, gc);
7379 if (kind == BAR_CURSOR)
7380 XFillRectangle (dpy, window, gc,
7381 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7382 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7383 width, row->height);
7384 else
7385 XFillRectangle (dpy, window, gc,
7386 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7387 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7388 row->height - width),
7389 cursor_glyph->pixel_width,
7390 width);
7392 XSetClipMask (dpy, gc, None);
7397 /* RIF: Define cursor CURSOR on frame F. */
7399 static void
7400 x_define_frame_cursor (f, cursor)
7401 struct frame *f;
7402 Cursor cursor;
7404 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7408 /* RIF: Clear area on frame F. */
7410 static void
7411 x_clear_frame_area (f, x, y, width, height)
7412 struct frame *f;
7413 int x, y, width, height;
7415 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7416 x, y, width, height, False);
7420 /* RIF: Draw cursor on window W. */
7422 static void
7423 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7424 struct window *w;
7425 struct glyph_row *glyph_row;
7426 int x, y;
7427 int cursor_type, cursor_width;
7428 int on_p, active_p;
7430 struct frame *f = XFRAME (WINDOW_FRAME (w));
7432 if (on_p)
7434 w->phys_cursor_type = cursor_type;
7435 w->phys_cursor_on_p = 1;
7437 if (glyph_row->exact_window_width_line_p
7438 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
7440 glyph_row->cursor_in_fringe_p = 1;
7441 draw_fringe_bitmap (w, glyph_row, 0);
7443 else
7444 switch (cursor_type)
7446 case HOLLOW_BOX_CURSOR:
7447 x_draw_hollow_cursor (w, glyph_row);
7448 break;
7450 case FILLED_BOX_CURSOR:
7451 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7452 break;
7454 case BAR_CURSOR:
7455 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7456 break;
7458 case HBAR_CURSOR:
7459 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7460 break;
7462 case NO_CURSOR:
7463 w->phys_cursor_width = 0;
7464 break;
7466 default:
7467 abort ();
7470 #ifdef HAVE_X_I18N
7471 if (w == XWINDOW (f->selected_window))
7472 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7473 xic_set_preeditarea (w, x, y);
7474 #endif
7477 #ifndef XFlush
7478 XFlush (FRAME_X_DISPLAY (f));
7479 #endif
7483 /* Icons. */
7485 /* Make the x-window of frame F use the gnu icon bitmap. */
7488 x_bitmap_icon (f, file)
7489 struct frame *f;
7490 Lisp_Object file;
7492 int bitmap_id;
7494 if (FRAME_X_WINDOW (f) == 0)
7495 return 1;
7497 /* Free up our existing icon bitmap and mask if any. */
7498 if (f->output_data.x->icon_bitmap > 0)
7499 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7500 f->output_data.x->icon_bitmap = 0;
7502 if (STRINGP (file))
7504 #ifdef USE_GTK
7505 /* Use gtk_window_set_icon_from_file () if available,
7506 It's not restricted to bitmaps */
7507 if (xg_set_icon (f, file))
7508 return 0;
7509 #endif /* USE_GTK */
7510 bitmap_id = x_create_bitmap_from_file (f, file);
7511 x_create_bitmap_mask (f, bitmap_id);
7513 else
7515 /* Create the GNU bitmap and mask if necessary. */
7516 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7518 int rc = -1;
7520 #if defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7521 #ifdef USE_GTK
7522 if (xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7523 return 0;
7524 #else
7525 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7526 if (rc != -1)
7527 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7528 #endif /* USE_GTK */
7529 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
7531 /* If all else fails, use the (black and white) xbm image. */
7532 if (rc == -1)
7534 rc = x_create_bitmap_from_data (f, gnu_xbm_bits,
7535 gnu_xbm_width, gnu_xbm_height);
7536 if (rc == -1)
7537 return 1;
7539 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7540 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7544 /* The first time we create the GNU bitmap and mask,
7545 this increments the ref-count one extra time.
7546 As a result, the GNU bitmap and mask are never freed.
7547 That way, we don't have to worry about allocating it again. */
7548 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7550 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7553 x_wm_set_icon_pixmap (f, bitmap_id);
7554 f->output_data.x->icon_bitmap = bitmap_id;
7556 return 0;
7560 /* Make the x-window of frame F use a rectangle with text.
7561 Use ICON_NAME as the text. */
7564 x_text_icon (f, icon_name)
7565 struct frame *f;
7566 char *icon_name;
7568 if (FRAME_X_WINDOW (f) == 0)
7569 return 1;
7572 XTextProperty text;
7573 text.value = (unsigned char *) icon_name;
7574 text.encoding = XA_STRING;
7575 text.format = 8;
7576 text.nitems = strlen (icon_name);
7577 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7580 if (f->output_data.x->icon_bitmap > 0)
7581 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7582 f->output_data.x->icon_bitmap = 0;
7583 x_wm_set_icon_pixmap (f, 0);
7585 return 0;
7588 #define X_ERROR_MESSAGE_SIZE 200
7590 /* If non-nil, this should be a string.
7591 It means catch X errors and store the error message in this string.
7593 The reason we use a stack is that x_catch_error/x_uncatch_error can
7594 be called from a signal handler.
7597 struct x_error_message_stack {
7598 char string[X_ERROR_MESSAGE_SIZE];
7599 Display *dpy;
7600 struct x_error_message_stack *prev;
7602 static struct x_error_message_stack *x_error_message;
7604 /* An X error handler which stores the error message in
7605 *x_error_message. This is called from x_error_handler if
7606 x_catch_errors is in effect. */
7608 static void
7609 x_error_catcher (display, error)
7610 Display *display;
7611 XErrorEvent *error;
7613 XGetErrorText (display, error->error_code,
7614 x_error_message->string,
7615 X_ERROR_MESSAGE_SIZE);
7618 /* Begin trapping X errors for display DPY. Actually we trap X errors
7619 for all displays, but DPY should be the display you are actually
7620 operating on.
7622 After calling this function, X protocol errors no longer cause
7623 Emacs to exit; instead, they are recorded in the string
7624 stored in *x_error_message.
7626 Calling x_check_errors signals an Emacs error if an X error has
7627 occurred since the last call to x_catch_errors or x_check_errors.
7629 Calling x_uncatch_errors resumes the normal error handling. */
7631 void x_check_errors ();
7633 void
7634 x_catch_errors (dpy)
7635 Display *dpy;
7637 struct x_error_message_stack *data = xmalloc (sizeof (*data));
7639 /* Make sure any errors from previous requests have been dealt with. */
7640 XSync (dpy, False);
7642 data->dpy = dpy;
7643 data->string[0] = 0;
7644 data->prev = x_error_message;
7645 x_error_message = data;
7648 /* Undo the last x_catch_errors call.
7649 DPY should be the display that was passed to x_catch_errors. */
7651 void
7652 x_uncatch_errors ()
7654 struct x_error_message_stack *tmp;
7656 BLOCK_INPUT;
7658 /* The display may have been closed before this function is called.
7659 Check if it is still open before calling XSync. */
7660 if (x_display_info_for_display (x_error_message->dpy) != 0)
7661 XSync (x_error_message->dpy, False);
7663 tmp = x_error_message;
7664 x_error_message = x_error_message->prev;
7665 xfree (tmp);
7666 UNBLOCK_INPUT;
7669 /* If any X protocol errors have arrived since the last call to
7670 x_catch_errors or x_check_errors, signal an Emacs error using
7671 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7673 void
7674 x_check_errors (dpy, format)
7675 Display *dpy;
7676 char *format;
7678 /* Make sure to catch any errors incurred so far. */
7679 XSync (dpy, False);
7681 if (x_error_message->string[0])
7683 char string[X_ERROR_MESSAGE_SIZE];
7684 bcopy (x_error_message->string, string, X_ERROR_MESSAGE_SIZE);
7685 x_uncatch_errors ();
7686 error (format, string);
7690 /* Nonzero if we had any X protocol errors
7691 since we did x_catch_errors on DPY. */
7694 x_had_errors_p (dpy)
7695 Display *dpy;
7697 /* Make sure to catch any errors incurred so far. */
7698 XSync (dpy, False);
7700 return x_error_message->string[0] != 0;
7703 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7705 void
7706 x_clear_errors (dpy)
7707 Display *dpy;
7709 x_error_message->string[0] = 0;
7712 #if 0 /* See comment in unwind_to_catch why calling this is a bad
7713 * idea. --lorentey */
7714 /* Close off all unclosed x_catch_errors calls. */
7716 void
7717 x_fully_uncatch_errors ()
7719 while (x_error_message)
7720 x_uncatch_errors ();
7722 #endif
7724 /* Nonzero if x_catch_errors has been done and not yet canceled. */
7727 x_catching_errors ()
7729 return x_error_message != 0;
7732 #if 0
7733 static unsigned int x_wire_count;
7734 x_trace_wire ()
7736 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7738 #endif /* ! 0 */
7741 /* Handle SIGPIPE, which can happen when the connection to a server
7742 simply goes away. SIGPIPE is handled by x_connection_signal.
7743 Don't need to do anything, because the write which caused the
7744 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7745 which will do the appropriate cleanup for us. */
7747 static SIGTYPE
7748 x_connection_signal (signalnum) /* If we don't have an argument, */
7749 int signalnum; /* some compilers complain in signal calls. */
7751 #ifdef USG
7752 /* USG systems forget handlers when they are used;
7753 must reestablish each time */
7754 signal (signalnum, x_connection_signal);
7755 #endif /* USG */
7759 /************************************************************************
7760 Handling X errors
7761 ************************************************************************/
7763 /* Error message passed to x_connection_closed. */
7765 static char *error_msg;
7767 /* Function installed as fatal_error_signal_hook in
7768 x_connection_closed. Print the X error message, and exit normally,
7769 instead of dumping core when XtCloseDisplay fails. */
7771 static void
7772 x_fatal_error_signal ()
7774 fprintf (stderr, "%s\n", error_msg);
7775 exit (70);
7778 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7779 the text of an error message that lead to the connection loss. */
7781 static SIGTYPE
7782 x_connection_closed (dpy, error_message)
7783 Display *dpy;
7784 char *error_message;
7786 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7787 Lisp_Object frame, tail;
7788 int index = SPECPDL_INDEX ();
7790 error_msg = (char *) alloca (strlen (error_message) + 1);
7791 strcpy (error_msg, error_message);
7792 handling_signal = 0;
7794 /* Prevent being called recursively because of an error condition
7795 below. Otherwise, we might end up with printing ``can't find per
7796 display information'' in the recursive call instead of printing
7797 the original message here. */
7798 x_catch_errors (dpy);
7800 /* Inhibit redisplay while frames are being deleted. */
7801 specbind (Qinhibit_redisplay, Qt);
7803 if (dpyinfo)
7805 /* Protect display from being closed when we delete the last
7806 frame on it. */
7807 dpyinfo->reference_count++;
7808 dpyinfo->terminal->reference_count++;
7811 /* First delete frames whose mini-buffers are on frames
7812 that are on the dead display. */
7813 FOR_EACH_FRAME (tail, frame)
7815 Lisp_Object minibuf_frame;
7816 minibuf_frame
7817 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7818 if (FRAME_X_P (XFRAME (frame))
7819 && FRAME_X_P (XFRAME (minibuf_frame))
7820 && ! EQ (frame, minibuf_frame)
7821 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7822 Fdelete_frame (frame, Qt);
7825 /* Now delete all remaining frames on the dead display.
7826 We are now sure none of these is used as the mini-buffer
7827 for another frame that we need to delete. */
7828 FOR_EACH_FRAME (tail, frame)
7829 if (FRAME_X_P (XFRAME (frame))
7830 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7832 /* Set this to t so that Fdelete_frame won't get confused
7833 trying to find a replacement. */
7834 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7835 Fdelete_frame (frame, Qt);
7838 /* We have to close the display to inform Xt that it doesn't
7839 exist anymore. If we don't, Xt will continue to wait for
7840 events from the display. As a consequence, a sequence of
7842 M-x make-frame-on-display RET :1 RET
7843 ...kill the new frame, so that we get an IO error...
7844 M-x make-frame-on-display RET :1 RET
7846 will indefinitely wait in Xt for events for display `:1', opened
7847 in the first class to make-frame-on-display.
7849 Closing the display is reported to lead to a bus error on
7850 OpenWindows in certain situations. I suspect that is a bug
7851 in OpenWindows. I don't know how to circumvent it here. */
7853 #ifdef USE_X_TOOLKIT
7854 /* If DPYINFO is null, this means we didn't open the display
7855 in the first place, so don't try to close it. */
7856 if (dpyinfo)
7858 extern void (*fatal_error_signal_hook) P_ ((void));
7859 fatal_error_signal_hook = x_fatal_error_signal;
7860 XtCloseDisplay (dpy);
7861 fatal_error_signal_hook = NULL;
7863 #endif
7865 #ifdef USE_GTK
7866 if (dpyinfo)
7867 xg_display_close (dpyinfo->display);
7868 #endif
7870 if (dpyinfo)
7872 /* Indicate that this display is dead. */
7873 dpyinfo->display = 0;
7875 dpyinfo->reference_count--;
7876 dpyinfo->terminal->reference_count--;
7877 if (dpyinfo->reference_count != 0)
7878 /* We have just closed all frames on this display. */
7879 abort ();
7881 x_delete_display (dpyinfo);
7884 x_uncatch_errors ();
7886 if (terminal_list == 0)
7888 fprintf (stderr, "%s\n", error_msg);
7889 shut_down_emacs (0, 0, Qnil);
7890 exit (70);
7893 /* Ordinary stack unwind doesn't deal with these. */
7894 #ifdef SIGIO
7895 sigunblock (sigmask (SIGIO));
7896 #endif
7897 sigunblock (sigmask (SIGALRM));
7898 TOTALLY_UNBLOCK_INPUT;
7900 unbind_to (index, Qnil);
7901 clear_waiting_for_input ();
7902 /* FIXME: This is an asynchronous interrupt w.r.t elisp, so signalling an
7903 error might not be the best thing to do. I'd vote for creating an
7904 elisp event and stuffing it in the queue so people can bind to it via
7905 the global map. --Stef */
7906 error ("%s", error_msg);
7909 /* We specifically use it before defining it, so that gcc doesn't inline it,
7910 otherwise gdb doesn't know how to properly put a breakpoint on it. */
7911 static void x_error_quitter P_ ((Display *, XErrorEvent *));
7913 /* This is the first-level handler for X protocol errors.
7914 It calls x_error_quitter or x_error_catcher. */
7916 static int
7917 x_error_handler (display, error)
7918 Display *display;
7919 XErrorEvent *error;
7921 if (x_error_message)
7922 x_error_catcher (display, error);
7923 else
7924 x_error_quitter (display, error);
7925 return 0;
7928 /* This is the usual handler for X protocol errors.
7929 It kills all frames on the display that we got the error for.
7930 If that was the only one, it prints an error message and kills Emacs. */
7932 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
7934 #if __GNUC__ >= 3 /* On GCC 3.0 we might get a warning. */
7935 #define NO_INLINE __attribute__((noinline))
7936 #else
7937 #define NO_INLINE
7938 #endif
7940 /* Some versions of GNU/Linux define noinline in their headers. */
7942 #ifdef noinline
7943 #undef noinline
7944 #endif
7946 /* On older GCC versions, just putting x_error_quitter
7947 after x_error_handler prevents inlining into the former. */
7949 static void NO_INLINE
7950 x_error_quitter (display, error)
7951 Display *display;
7952 XErrorEvent *error;
7954 char buf[256], buf1[356];
7956 /* Ignore BadName errors. They can happen because of fonts
7957 or colors that are not defined. */
7959 if (error->error_code == BadName)
7960 return;
7962 /* Note that there is no real way portable across R3/R4 to get the
7963 original error handler. */
7965 XGetErrorText (display, error->error_code, buf, sizeof (buf));
7966 sprintf (buf1, "X protocol error: %s on protocol request %d",
7967 buf, error->request_code);
7968 x_connection_closed (display, buf1);
7972 /* This is the handler for X IO errors, always.
7973 It kills all frames on the display that we lost touch with.
7974 If that was the only one, it prints an error message and kills Emacs. */
7976 static int
7977 x_io_error_quitter (display)
7978 Display *display;
7980 char buf[256];
7982 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
7983 x_connection_closed (display, buf);
7984 return 0;
7987 /* Changing the font of the frame. */
7989 /* Give frame F the font named FONTNAME as its default font, and
7990 return the full name of that font. FONTNAME may be a wildcard
7991 pattern; in that case, we choose some font that fits the pattern.
7992 The return value shows which font we chose. */
7994 Lisp_Object
7995 x_new_font (f, fontname)
7996 struct frame *f;
7997 register char *fontname;
7999 struct font_info *fontp
8000 = FS_LOAD_FONT (f, 0, fontname, -1);
8002 if (!fontp)
8003 return Qnil;
8005 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
8006 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
8007 FRAME_FONTSET (f) = -1;
8009 FRAME_COLUMN_WIDTH (f) = fontp->average_width;
8010 FRAME_SPACE_WIDTH (f) = fontp->space_width;
8011 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (FRAME_FONT (f));
8013 compute_fringe_widths (f, 1);
8015 /* Compute the scroll bar width in character columns. */
8016 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8018 int wid = FRAME_COLUMN_WIDTH (f);
8019 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8020 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
8022 else
8024 int wid = FRAME_COLUMN_WIDTH (f);
8025 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
8028 /* Now make the frame display the given font. */
8029 if (FRAME_X_WINDOW (f) != 0)
8031 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
8032 FRAME_FONT (f)->fid);
8033 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
8034 FRAME_FONT (f)->fid);
8035 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
8036 FRAME_FONT (f)->fid);
8038 /* Don't change the size of a tip frame; there's no point in
8039 doing it because it's done in Fx_show_tip, and it leads to
8040 problems because the tip frame has no widget. */
8041 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8042 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
8045 return build_string (fontp->full_name);
8048 /* Give frame F the fontset named FONTSETNAME as its default font, and
8049 return the full name of that fontset. FONTSETNAME may be a wildcard
8050 pattern; in that case, we choose some fontset that fits the pattern.
8051 The return value shows which fontset we chose. */
8053 Lisp_Object
8054 x_new_fontset (f, fontsetname)
8055 struct frame *f;
8056 char *fontsetname;
8058 int fontset = fs_query_fontset (build_string (fontsetname), 0);
8059 Lisp_Object result;
8061 if (fontset < 0)
8062 return Qnil;
8064 if (FRAME_FONTSET (f) == fontset)
8065 /* This fontset is already set in frame F. There's nothing more
8066 to do. */
8067 return fontset_name (fontset);
8069 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
8071 if (!STRINGP (result))
8072 /* Can't load ASCII font. */
8073 return Qnil;
8075 /* Since x_new_font doesn't update any fontset information, do it now. */
8076 FRAME_FONTSET (f) = fontset;
8078 #ifdef HAVE_X_I18N
8079 if (FRAME_XIC (f)
8080 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8081 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
8082 #endif
8084 return build_string (fontsetname);
8088 /***********************************************************************
8089 X Input Methods
8090 ***********************************************************************/
8092 #ifdef HAVE_X_I18N
8094 #ifdef HAVE_X11R6
8096 /* XIM destroy callback function, which is called whenever the
8097 connection to input method XIM dies. CLIENT_DATA contains a
8098 pointer to the x_display_info structure corresponding to XIM. */
8100 static void
8101 xim_destroy_callback (xim, client_data, call_data)
8102 XIM xim;
8103 XPointer client_data;
8104 XPointer call_data;
8106 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8107 Lisp_Object frame, tail;
8109 BLOCK_INPUT;
8111 /* No need to call XDestroyIC.. */
8112 FOR_EACH_FRAME (tail, frame)
8114 struct frame *f = XFRAME (frame);
8115 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8117 FRAME_XIC (f) = NULL;
8118 xic_free_xfontset (f);
8122 /* No need to call XCloseIM. */
8123 dpyinfo->xim = NULL;
8124 XFree (dpyinfo->xim_styles);
8125 UNBLOCK_INPUT;
8128 #endif /* HAVE_X11R6 */
8130 #ifdef HAVE_X11R6
8131 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8132 extern char *XSetIMValues P_ ((XIM, ...));
8133 #endif
8135 /* Open the connection to the XIM server on display DPYINFO.
8136 RESOURCE_NAME is the resource name Emacs uses. */
8138 static void
8139 xim_open_dpy (dpyinfo, resource_name)
8140 struct x_display_info *dpyinfo;
8141 char *resource_name;
8143 XIM xim;
8145 #ifdef HAVE_XIM
8146 if (use_xim)
8148 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8149 EMACS_CLASS);
8150 dpyinfo->xim = xim;
8152 if (xim)
8154 #ifdef HAVE_X11R6
8155 XIMCallback destroy;
8156 #endif
8158 /* Get supported styles and XIM values. */
8159 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8161 #ifdef HAVE_X11R6
8162 destroy.callback = xim_destroy_callback;
8163 destroy.client_data = (XPointer)dpyinfo;
8164 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8165 #endif
8169 else
8170 #endif /* HAVE_XIM */
8171 dpyinfo->xim = NULL;
8175 #ifdef HAVE_X11R6_XIM
8177 struct xim_inst_t
8179 struct x_display_info *dpyinfo;
8180 char *resource_name;
8183 /* XIM instantiate callback function, which is called whenever an XIM
8184 server is available. DISPLAY is the display of the XIM.
8185 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8186 when the callback was registered. */
8188 static void
8189 xim_instantiate_callback (display, client_data, call_data)
8190 Display *display;
8191 XPointer client_data;
8192 XPointer call_data;
8194 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8195 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8197 /* We don't support multiple XIM connections. */
8198 if (dpyinfo->xim)
8199 return;
8201 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8203 /* Create XIC for the existing frames on the same display, as long
8204 as they have no XIC. */
8205 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8207 Lisp_Object tail, frame;
8209 BLOCK_INPUT;
8210 FOR_EACH_FRAME (tail, frame)
8212 struct frame *f = XFRAME (frame);
8214 if (FRAME_X_P (f)
8215 && FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8216 if (FRAME_XIC (f) == NULL)
8218 create_frame_xic (f);
8219 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8220 xic_set_statusarea (f);
8221 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8223 struct window *w = XWINDOW (f->selected_window);
8224 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8229 UNBLOCK_INPUT;
8233 #endif /* HAVE_X11R6_XIM */
8236 /* Open a connection to the XIM server on display DPYINFO.
8237 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8238 connection only at the first time. On X11R6, open the connection
8239 in the XIM instantiate callback function. */
8241 static void
8242 xim_initialize (dpyinfo, resource_name)
8243 struct x_display_info *dpyinfo;
8244 char *resource_name;
8246 #ifdef HAVE_XIM
8247 if (use_xim)
8249 #ifdef HAVE_X11R6_XIM
8250 struct xim_inst_t *xim_inst;
8251 int len;
8253 dpyinfo->xim = NULL;
8254 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8255 xim_inst->dpyinfo = dpyinfo;
8256 len = strlen (resource_name);
8257 xim_inst->resource_name = (char *) xmalloc (len + 1);
8258 bcopy (resource_name, xim_inst->resource_name, len + 1);
8259 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8260 resource_name, EMACS_CLASS,
8261 xim_instantiate_callback,
8262 /* This is XPointer in XFree86
8263 but (XPointer *) on Tru64, at
8264 least, hence the configure test. */
8265 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8266 #else /* not HAVE_X11R6_XIM */
8267 dpyinfo->xim = NULL;
8268 xim_open_dpy (dpyinfo, resource_name);
8269 #endif /* not HAVE_X11R6_XIM */
8272 else
8273 #endif /* HAVE_XIM */
8274 dpyinfo->xim = NULL;
8278 /* Close the connection to the XIM server on display DPYINFO. */
8280 static void
8281 xim_close_dpy (dpyinfo)
8282 struct x_display_info *dpyinfo;
8284 #ifdef HAVE_XIM
8285 if (use_xim)
8287 #ifdef HAVE_X11R6_XIM
8288 if (dpyinfo->display)
8289 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8290 NULL, EMACS_CLASS,
8291 xim_instantiate_callback, NULL);
8292 #endif /* not HAVE_X11R6_XIM */
8293 if (dpyinfo->display)
8294 XCloseIM (dpyinfo->xim);
8295 dpyinfo->xim = NULL;
8296 XFree (dpyinfo->xim_styles);
8298 #endif /* HAVE_XIM */
8301 #endif /* not HAVE_X11R6_XIM */
8305 /* Calculate the absolute position in frame F
8306 from its current recorded position values and gravity. */
8308 void
8309 x_calc_absolute_position (f)
8310 struct frame *f;
8312 int flags = f->size_hint_flags;
8314 /* We have nothing to do if the current position
8315 is already for the top-left corner. */
8316 if (! ((flags & XNegative) || (flags & YNegative)))
8317 return;
8319 /* Treat negative positions as relative to the leftmost bottommost
8320 position that fits on the screen. */
8321 if (flags & XNegative)
8322 f->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
8323 - FRAME_PIXEL_WIDTH (f) + f->left_pos);
8326 int height = FRAME_PIXEL_HEIGHT (f);
8328 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8329 /* Something is fishy here. When using Motif, starting Emacs with
8330 `-g -0-0', the frame appears too low by a few pixels.
8332 This seems to be so because initially, while Emacs is starting,
8333 the column widget's height and the frame's pixel height are
8334 different. The column widget's height is the right one. In
8335 later invocations, when Emacs is up, the frame's pixel height
8336 is right, though.
8338 It's not obvious where the initial small difference comes from.
8339 2000-12-01, gerd. */
8341 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8342 #endif
8344 if (flags & YNegative)
8345 f->top_pos = (FRAME_X_DISPLAY_INFO (f)->height - height + f->top_pos);
8348 /* The left_pos and top_pos
8349 are now relative to the top and left screen edges,
8350 so the flags should correspond. */
8351 f->size_hint_flags &= ~ (XNegative | YNegative);
8354 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8355 to really change the position, and 0 when calling from
8356 x_make_frame_visible (in that case, XOFF and YOFF are the current
8357 position values). It is -1 when calling from x_set_frame_parameters,
8358 which means, do adjust for borders but don't change the gravity. */
8360 void
8361 x_set_offset (f, xoff, yoff, change_gravity)
8362 struct frame *f;
8363 register int xoff, yoff;
8364 int change_gravity;
8366 int modified_top, modified_left;
8368 if (change_gravity != 0)
8370 FRAME_X_OUTPUT (f)->left_before_move = f->left_pos;
8371 FRAME_X_OUTPUT (f)->top_before_move = f->top_pos;
8373 f->top_pos = yoff;
8374 f->left_pos = xoff;
8375 f->size_hint_flags &= ~ (XNegative | YNegative);
8376 if (xoff < 0)
8377 f->size_hint_flags |= XNegative;
8378 if (yoff < 0)
8379 f->size_hint_flags |= YNegative;
8380 f->win_gravity = NorthWestGravity;
8382 x_calc_absolute_position (f);
8384 BLOCK_INPUT;
8385 x_wm_set_size_hint (f, (long) 0, 0);
8387 modified_left = f->left_pos;
8388 modified_top = f->top_pos;
8390 if (change_gravity != 0 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8392 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8393 than the WM decorations. So we use the calculated offset instead
8394 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8395 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8396 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8399 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8400 modified_left, modified_top);
8402 x_sync_with_move (f, f->left_pos, f->top_pos,
8403 FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8404 ? 1 : 0);
8406 /* change_gravity is non-zero when this function is called from Lisp to
8407 programmatically move a frame. In that case, we call
8408 x_check_expected_move to discover if we have a "Type A" or "Type B"
8409 window manager, and, for a "Type A" window manager, adjust the position
8410 of the frame.
8412 We call x_check_expected_move if a programmatic move occurred, and
8413 either the window manager type (A/B) is unknown or it is Type A but we
8414 need to compute the top/left offset adjustment for this frame. */
8416 if (change_gravity != 0 &&
8417 (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8418 || (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8419 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8420 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8421 x_check_expected_move (f, modified_left, modified_top);
8423 UNBLOCK_INPUT;
8426 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8427 on the root window for frame F contains ATOMNAME.
8428 This is how a WM check shall be done according to the Window Manager
8429 Specification/Extended Window Manager Hints at
8430 http://freedesktop.org/wiki/Specifications/wm-spec. */
8432 static int
8433 wm_supports (f, atomname)
8434 struct frame *f;
8435 const char *atomname;
8437 Atom actual_type;
8438 unsigned long actual_size, bytes_remaining;
8439 int i, rc, actual_format;
8440 Atom prop_atom;
8441 Window wmcheck_window;
8442 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8443 Window target_window = dpyinfo->root_window;
8444 long max_len = 65536;
8445 Display *dpy = FRAME_X_DISPLAY (f);
8446 unsigned char *tmp_data = NULL;
8447 Atom target_type = XA_WINDOW;
8448 Atom want_atom;
8450 BLOCK_INPUT;
8452 prop_atom = XInternAtom (dpy, "_NET_SUPPORTING_WM_CHECK", False);
8454 x_catch_errors (dpy);
8455 rc = XGetWindowProperty (dpy, target_window,
8456 prop_atom, 0, max_len, False, target_type,
8457 &actual_type, &actual_format, &actual_size,
8458 &bytes_remaining, &tmp_data);
8460 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8462 if (tmp_data) XFree (tmp_data);
8463 x_uncatch_errors ();
8464 UNBLOCK_INPUT;
8465 return 0;
8468 wmcheck_window = *(Window *) tmp_data;
8469 XFree (tmp_data);
8471 /* Check if window exists. */
8472 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8473 x_sync (f);
8474 if (x_had_errors_p (dpy))
8476 x_uncatch_errors ();
8477 UNBLOCK_INPUT;
8478 return 0;
8481 if (dpyinfo->net_supported_window != wmcheck_window)
8483 /* Window changed, reload atoms */
8484 if (dpyinfo->net_supported_atoms != NULL)
8485 XFree (dpyinfo->net_supported_atoms);
8486 dpyinfo->net_supported_atoms = NULL;
8487 dpyinfo->nr_net_supported_atoms = 0;
8488 dpyinfo->net_supported_window = 0;
8490 target_type = XA_ATOM;
8491 prop_atom = XInternAtom (dpy, "_NET_SUPPORTED", False);
8492 tmp_data = NULL;
8493 rc = XGetWindowProperty (dpy, target_window,
8494 prop_atom, 0, max_len, False, target_type,
8495 &actual_type, &actual_format, &actual_size,
8496 &bytes_remaining, &tmp_data);
8498 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
8500 if (tmp_data) XFree (tmp_data);
8501 x_uncatch_errors ();
8502 UNBLOCK_INPUT;
8503 return 0;
8506 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8507 dpyinfo->nr_net_supported_atoms = actual_size;
8508 dpyinfo->net_supported_window = wmcheck_window;
8511 rc = 0;
8512 want_atom = XInternAtom (dpy, atomname, False);
8514 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
8515 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8517 x_uncatch_errors ();
8518 UNBLOCK_INPUT;
8520 return rc;
8523 /* Do fullscreen as specified in extended window manager hints */
8525 static int
8526 do_ewmh_fullscreen (f)
8527 struct frame *f;
8529 int have_net_atom = wm_supports (f, "_NET_WM_STATE");
8531 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8532 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8533 if (!have_net_atom)
8534 have_net_atom = wm_supports (f, "_NET_WM_STATE_FULLSCREEN");
8536 if (have_net_atom)
8538 Lisp_Object frame;
8539 const char *atom = "_NET_WM_STATE";
8540 const char *fs = "_NET_WM_STATE_FULLSCREEN";
8541 const char *fw = "_NET_WM_STATE_MAXIMIZED_HORZ";
8542 const char *fh = "_NET_WM_STATE_MAXIMIZED_VERT";
8543 const char *what = NULL;
8545 XSETFRAME (frame, f);
8547 /* If there are _NET_ atoms we assume we have extended window manager
8548 hints. */
8549 switch (f->want_fullscreen)
8551 case FULLSCREEN_BOTH:
8552 what = fs;
8553 break;
8554 case FULLSCREEN_WIDTH:
8555 what = fw;
8556 break;
8557 case FULLSCREEN_HEIGHT:
8558 what = fh;
8559 break;
8562 if (what != NULL && !wm_supports (f, what)) return 0;
8565 Fx_send_client_event (frame, make_number (0), frame,
8566 make_unibyte_string (atom, strlen (atom)),
8567 make_number (32),
8568 Fcons (make_number (0), /* Remove */
8569 Fcons
8570 (make_unibyte_string (fs,
8571 strlen (fs)),
8572 Qnil)));
8573 Fx_send_client_event (frame, make_number (0), frame,
8574 make_unibyte_string (atom, strlen (atom)),
8575 make_number (32),
8576 Fcons (make_number (0), /* Remove */
8577 Fcons
8578 (make_unibyte_string (fh,
8579 strlen (fh)),
8580 Qnil)));
8581 Fx_send_client_event (frame, make_number (0), frame,
8582 make_unibyte_string (atom, strlen (atom)),
8583 make_number (32),
8584 Fcons (make_number (0), /* Remove */
8585 Fcons
8586 (make_unibyte_string (fw,
8587 strlen (fw)),
8588 Qnil)));
8589 f->want_fullscreen = FULLSCREEN_NONE;
8590 if (what != NULL)
8591 Fx_send_client_event (frame, make_number (0), frame,
8592 make_unibyte_string (atom, strlen (atom)),
8593 make_number (32),
8594 Fcons (make_number (1), /* Add */
8595 Fcons
8596 (make_unibyte_string (what,
8597 strlen (what)),
8598 Qnil)));
8601 return have_net_atom;
8604 static void
8605 XTfullscreen_hook (f)
8606 FRAME_PTR f;
8608 if (f->async_visible)
8610 BLOCK_INPUT;
8611 do_ewmh_fullscreen (f);
8612 x_sync (f);
8613 UNBLOCK_INPUT;
8618 /* Check if we need to resize the frame due to a fullscreen request.
8619 If so needed, resize the frame. */
8620 static void
8621 x_check_fullscreen (f)
8622 struct frame *f;
8624 if (f->want_fullscreen & FULLSCREEN_BOTH)
8626 int width, height, ign;
8628 if (do_ewmh_fullscreen (f))
8629 return;
8631 x_real_positions (f, &f->left_pos, &f->top_pos);
8633 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
8635 /* We do not need to move the window, it shall be taken care of
8636 when setting WM manager hints.
8637 If the frame is visible already, the position is checked by
8638 x_check_expected_move. */
8639 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
8641 change_frame_size (f, height, width, 0, 1, 0);
8642 SET_FRAME_GARBAGED (f);
8643 cancel_mouse_face (f);
8645 /* Wait for the change of frame size to occur */
8646 f->want_fullscreen |= FULLSCREEN_WAIT;
8651 /* This function is called by x_set_offset to determine whether the window
8652 manager interfered with the positioning of the frame. Type A window
8653 managers position the surrounding window manager decorations a small
8654 amount above and left of the user-supplied position. Type B window
8655 managers position the surrounding window manager decorations at the
8656 user-specified position. If we detect a Type A window manager, we
8657 compensate by moving the window right and down by the proper amount. */
8659 static void
8660 x_check_expected_move (f, expected_left, expected_top)
8661 struct frame *f;
8662 int expected_left;
8663 int expected_top;
8665 int current_left = 0, current_top = 0;
8667 /* x_real_positions returns the left and top offsets of the outermost
8668 window manager window around the frame. */
8670 x_real_positions (f, &current_left, &current_top);
8672 if (current_left != expected_left || current_top != expected_top)
8674 /* It's a "Type A" window manager. */
8676 int adjusted_left;
8677 int adjusted_top;
8679 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8680 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
8681 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
8683 /* Now fix the mispositioned frame's location. */
8685 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
8686 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
8688 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8689 adjusted_left, adjusted_top);
8691 x_sync_with_move (f, expected_left, expected_top, 0);
8693 else
8694 /* It's a "Type B" window manager. We don't have to adjust the
8695 frame's position. */
8697 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8701 /* Wait for XGetGeometry to return up-to-date position information for a
8702 recently-moved frame. Call this immediately after calling XMoveWindow.
8703 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
8704 frame has been moved to, so we use a fuzzy position comparison instead
8705 of an exact comparison. */
8707 static void
8708 x_sync_with_move (f, left, top, fuzzy)
8709 struct frame *f;
8710 int left, top, fuzzy;
8712 int count = 0;
8714 while (count++ < 50)
8716 int current_left = 0, current_top = 0;
8718 /* In theory, this call to XSync only needs to happen once, but in
8719 practice, it doesn't seem to work, hence the need for the surrounding
8720 loop. */
8722 XSync (FRAME_X_DISPLAY (f), False);
8723 x_real_positions (f, &current_left, &current_top);
8725 if (fuzzy)
8727 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
8728 pixels. */
8730 if (eabs (current_left - left) <= 10
8731 && eabs (current_top - top) <= 40)
8732 return;
8734 else if (current_left == left && current_top == top)
8735 return;
8738 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
8739 will then return up-to-date position info. */
8741 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
8745 /* Change the size of frame F's X window to COLS/ROWS in the case F
8746 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8747 top-left-corner window gravity for this size change and subsequent
8748 size changes. Otherwise we leave the window gravity unchanged. */
8750 static void
8751 x_set_window_size_1 (f, change_gravity, cols, rows)
8752 struct frame *f;
8753 int change_gravity;
8754 int cols, rows;
8756 int pixelwidth, pixelheight;
8758 check_frame_size (f, &rows, &cols);
8759 f->scroll_bar_actual_width
8760 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8762 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
8763 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
8764 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
8766 compute_fringe_widths (f, 0);
8768 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
8769 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
8771 f->win_gravity = NorthWestGravity;
8772 x_wm_set_size_hint (f, (long) 0, 0);
8774 XSync (FRAME_X_DISPLAY (f), False);
8775 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8776 pixelwidth, pixelheight);
8778 /* Now, strictly speaking, we can't be sure that this is accurate,
8779 but the window manager will get around to dealing with the size
8780 change request eventually, and we'll hear how it went when the
8781 ConfigureNotify event gets here.
8783 We could just not bother storing any of this information here,
8784 and let the ConfigureNotify event set everything up, but that
8785 might be kind of confusing to the Lisp code, since size changes
8786 wouldn't be reported in the frame parameters until some random
8787 point in the future when the ConfigureNotify event arrives.
8789 We pass 1 for DELAY since we can't run Lisp code inside of
8790 a BLOCK_INPUT. */
8791 change_frame_size (f, rows, cols, 0, 1, 0);
8792 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8793 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8795 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8796 receive in the ConfigureNotify event; if we get what we asked
8797 for, then the event won't cause the screen to become garbaged, so
8798 we have to make sure to do it here. */
8799 SET_FRAME_GARBAGED (f);
8801 XFlush (FRAME_X_DISPLAY (f));
8805 /* Call this to change the size of frame F's x-window.
8806 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8807 for this size change and subsequent size changes.
8808 Otherwise we leave the window gravity unchanged. */
8810 void
8811 x_set_window_size (f, change_gravity, cols, rows)
8812 struct frame *f;
8813 int change_gravity;
8814 int cols, rows;
8816 BLOCK_INPUT;
8818 #ifdef USE_GTK
8819 if (FRAME_GTK_WIDGET (f))
8820 xg_frame_set_char_size (f, cols, rows);
8821 else
8822 x_set_window_size_1 (f, change_gravity, cols, rows);
8823 #elif USE_X_TOOLKIT
8825 if (f->output_data.x->widget != NULL)
8827 /* The x and y position of the widget is clobbered by the
8828 call to XtSetValues within EmacsFrameSetCharSize.
8829 This is a real kludge, but I don't understand Xt so I can't
8830 figure out a correct fix. Can anyone else tell me? -- rms. */
8831 int xpos = f->output_data.x->widget->core.x;
8832 int ypos = f->output_data.x->widget->core.y;
8833 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
8834 f->output_data.x->widget->core.x = xpos;
8835 f->output_data.x->widget->core.y = ypos;
8837 else
8838 x_set_window_size_1 (f, change_gravity, cols, rows);
8840 #else /* not USE_X_TOOLKIT */
8842 x_set_window_size_1 (f, change_gravity, cols, rows);
8844 #endif /* not USE_X_TOOLKIT */
8846 /* If cursor was outside the new size, mark it as off. */
8847 mark_window_cursors_off (XWINDOW (f->root_window));
8849 /* Clear out any recollection of where the mouse highlighting was,
8850 since it might be in a place that's outside the new frame size.
8851 Actually checking whether it is outside is a pain in the neck,
8852 so don't try--just let the highlighting be done afresh with new size. */
8853 cancel_mouse_face (f);
8855 UNBLOCK_INPUT;
8858 /* Mouse warping. */
8860 void
8861 x_set_mouse_position (f, x, y)
8862 struct frame *f;
8863 int x, y;
8865 int pix_x, pix_y;
8867 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
8868 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
8870 if (pix_x < 0) pix_x = 0;
8871 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
8873 if (pix_y < 0) pix_y = 0;
8874 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
8876 BLOCK_INPUT;
8878 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8879 0, 0, 0, 0, pix_x, pix_y);
8880 UNBLOCK_INPUT;
8883 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
8885 void
8886 x_set_mouse_pixel_position (f, pix_x, pix_y)
8887 struct frame *f;
8888 int pix_x, pix_y;
8890 BLOCK_INPUT;
8892 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8893 0, 0, 0, 0, pix_x, pix_y);
8894 UNBLOCK_INPUT;
8897 /* focus shifting, raising and lowering. */
8899 void
8900 x_focus_on_frame (f)
8901 struct frame *f;
8903 #if 0 /* This proves to be unpleasant. */
8904 x_raise_frame (f);
8905 #endif
8906 #if 0
8907 /* I don't think that the ICCCM allows programs to do things like this
8908 without the interaction of the window manager. Whatever you end up
8909 doing with this code, do it to x_unfocus_frame too. */
8910 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8911 RevertToPointerRoot, CurrentTime);
8912 #endif /* ! 0 */
8915 void
8916 x_unfocus_frame (f)
8917 struct frame *f;
8919 #if 0
8920 /* Look at the remarks in x_focus_on_frame. */
8921 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
8922 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
8923 RevertToPointerRoot, CurrentTime);
8924 #endif /* ! 0 */
8927 /* Raise frame F. */
8929 void
8930 x_raise_frame (f)
8931 struct frame *f;
8933 BLOCK_INPUT;
8934 if (f->async_visible)
8935 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8937 XFlush (FRAME_X_DISPLAY (f));
8938 UNBLOCK_INPUT;
8941 /* Lower frame F. */
8943 void
8944 x_lower_frame (f)
8945 struct frame *f;
8947 if (f->async_visible)
8949 BLOCK_INPUT;
8950 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8951 XFlush (FRAME_X_DISPLAY (f));
8952 UNBLOCK_INPUT;
8956 /* Activate frame with Extended Window Manager Hints */
8958 void
8959 x_ewmh_activate_frame (f)
8960 FRAME_PTR f;
8962 /* See Window Manager Specification/Extended Window Manager Hints at
8963 http://freedesktop.org/wiki/Specifications/wm-spec */
8965 const char *atom = "_NET_ACTIVE_WINDOW";
8966 if (f->async_visible && wm_supports (f, atom))
8968 Lisp_Object frame;
8969 XSETFRAME (frame, f);
8970 Fx_send_client_event (frame, make_number (0), frame,
8971 make_unibyte_string (atom, strlen (atom)),
8972 make_number (32),
8973 Fcons (make_number (1),
8974 Fcons (make_number (last_user_time),
8975 Qnil)));
8979 static void
8980 XTframe_raise_lower (f, raise_flag)
8981 FRAME_PTR f;
8982 int raise_flag;
8984 if (raise_flag)
8985 x_raise_frame (f);
8986 else
8987 x_lower_frame (f);
8990 /* Change of visibility. */
8992 /* This tries to wait until the frame is really visible.
8993 However, if the window manager asks the user where to position
8994 the frame, this will return before the user finishes doing that.
8995 The frame will not actually be visible at that time,
8996 but it will become visible later when the window manager
8997 finishes with it. */
8999 void
9000 x_make_frame_visible (f)
9001 struct frame *f;
9003 Lisp_Object type;
9004 int original_top, original_left;
9005 int retry_count = 2;
9007 retry:
9009 BLOCK_INPUT;
9011 type = x_icon_type (f);
9012 if (!NILP (type))
9013 x_bitmap_icon (f, type);
9015 if (! FRAME_VISIBLE_P (f))
9017 /* We test FRAME_GARBAGED_P here to make sure we don't
9018 call x_set_offset a second time
9019 if we get to x_make_frame_visible a second time
9020 before the window gets really visible. */
9021 if (! FRAME_ICONIFIED_P (f)
9022 && ! f->output_data.x->asked_for_visible)
9023 x_set_offset (f, f->left_pos, f->top_pos, 0);
9025 f->output_data.x->asked_for_visible = 1;
9027 if (! EQ (Vx_no_window_manager, Qt))
9028 x_wm_set_window_state (f, NormalState);
9029 #ifdef USE_X_TOOLKIT
9030 /* This was XtPopup, but that did nothing for an iconified frame. */
9031 XtMapWidget (f->output_data.x->widget);
9032 #else /* not USE_X_TOOLKIT */
9033 #ifdef USE_GTK
9034 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9035 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9036 #else
9037 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9038 #endif /* not USE_GTK */
9039 #endif /* not USE_X_TOOLKIT */
9040 #if 0 /* This seems to bring back scroll bars in the wrong places
9041 if the window configuration has changed. They seem
9042 to come back ok without this. */
9043 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9044 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9045 #endif
9048 XFlush (FRAME_X_DISPLAY (f));
9050 /* Synchronize to ensure Emacs knows the frame is visible
9051 before we do anything else. We do this loop with input not blocked
9052 so that incoming events are handled. */
9054 Lisp_Object frame;
9055 int count;
9056 /* This must be before UNBLOCK_INPUT
9057 since events that arrive in response to the actions above
9058 will set it when they are handled. */
9059 int previously_visible = f->output_data.x->has_been_visible;
9061 original_left = f->left_pos;
9062 original_top = f->top_pos;
9064 /* This must come after we set COUNT. */
9065 UNBLOCK_INPUT;
9067 /* We unblock here so that arriving X events are processed. */
9069 /* Now move the window back to where it was "supposed to be".
9070 But don't do it if the gravity is negative.
9071 When the gravity is negative, this uses a position
9072 that is 3 pixels too low. Perhaps that's really the border width.
9074 Don't do this if the window has never been visible before,
9075 because the window manager may choose the position
9076 and we don't want to override it. */
9078 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
9079 && f->win_gravity == NorthWestGravity
9080 && previously_visible)
9082 Drawable rootw;
9083 int x, y;
9084 unsigned int width, height, border, depth;
9086 BLOCK_INPUT;
9088 /* On some window managers (such as FVWM) moving an existing
9089 window, even to the same place, causes the window manager
9090 to introduce an offset. This can cause the window to move
9091 to an unexpected location. Check the geometry (a little
9092 slow here) and then verify that the window is in the right
9093 place. If the window is not in the right place, move it
9094 there, and take the potential window manager hit. */
9095 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9096 &rootw, &x, &y, &width, &height, &border, &depth);
9098 if (original_left != x || original_top != y)
9099 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9100 original_left, original_top);
9102 UNBLOCK_INPUT;
9105 XSETFRAME (frame, f);
9107 /* Wait until the frame is visible. Process X events until a
9108 MapNotify event has been seen, or until we think we won't get a
9109 MapNotify at all.. */
9110 for (count = input_signal_count + 10;
9111 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9113 /* Force processing of queued events. */
9114 x_sync (f);
9116 /* Machines that do polling rather than SIGIO have been
9117 observed to go into a busy-wait here. So we'll fake an
9118 alarm signal to let the handler know that there's something
9119 to be read. We used to raise a real alarm, but it seems
9120 that the handler isn't always enabled here. This is
9121 probably a bug. */
9122 if (input_polling_used ())
9124 /* It could be confusing if a real alarm arrives while
9125 processing the fake one. Turn it off and let the
9126 handler reset it. */
9127 extern void poll_for_input_1 P_ ((void));
9128 int old_poll_suppress_count = poll_suppress_count;
9129 poll_suppress_count = 1;
9130 poll_for_input_1 ();
9131 poll_suppress_count = old_poll_suppress_count;
9134 /* See if a MapNotify event has been processed. */
9135 FRAME_SAMPLE_VISIBILITY (f);
9138 /* 2000-09-28: In
9140 (let ((f (selected-frame)))
9141 (iconify-frame f)
9142 (raise-frame f))
9144 the frame is not raised with various window managers on
9145 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
9146 unknown reason, the call to XtMapWidget is completely ignored.
9147 Mapping the widget a second time works. */
9149 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
9150 goto retry;
9154 /* Change from mapped state to withdrawn state. */
9156 /* Make the frame visible (mapped and not iconified). */
9158 void
9159 x_make_frame_invisible (f)
9160 struct frame *f;
9162 Window window;
9164 /* Use the frame's outermost window, not the one we normally draw on. */
9165 window = FRAME_OUTER_WINDOW (f);
9167 /* Don't keep the highlight on an invisible frame. */
9168 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9169 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9171 #if 0/* This might add unreliability; I don't trust it -- rms. */
9172 if (! f->async_visible && ! f->async_iconified)
9173 return;
9174 #endif
9176 BLOCK_INPUT;
9178 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9179 that the current position of the window is user-specified, rather than
9180 program-specified, so that when the window is mapped again, it will be
9181 placed at the same location, without forcing the user to position it
9182 by hand again (they have already done that once for this window.) */
9183 x_wm_set_size_hint (f, (long) 0, 1);
9185 #ifdef USE_GTK
9186 if (FRAME_GTK_OUTER_WIDGET (f))
9187 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9188 else
9189 #endif
9192 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9193 DefaultScreen (FRAME_X_DISPLAY (f))))
9195 UNBLOCK_INPUT_RESIGNAL;
9196 error ("Can't notify window manager of window withdrawal");
9200 /* We can't distinguish this from iconification
9201 just by the event that we get from the server.
9202 So we can't win using the usual strategy of letting
9203 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9204 and synchronize with the server to make sure we agree. */
9205 f->visible = 0;
9206 FRAME_ICONIFIED_P (f) = 0;
9207 f->async_visible = 0;
9208 f->async_iconified = 0;
9210 x_sync (f);
9212 UNBLOCK_INPUT;
9215 /* Change window state from mapped to iconified. */
9217 void
9218 x_iconify_frame (f)
9219 struct frame *f;
9221 int result;
9222 Lisp_Object type;
9224 /* Don't keep the highlight on an invisible frame. */
9225 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9226 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9228 if (f->async_iconified)
9229 return;
9231 BLOCK_INPUT;
9233 FRAME_SAMPLE_VISIBILITY (f);
9235 type = x_icon_type (f);
9236 if (!NILP (type))
9237 x_bitmap_icon (f, type);
9239 #ifdef USE_GTK
9240 if (FRAME_GTK_OUTER_WIDGET (f))
9242 if (! FRAME_VISIBLE_P (f))
9243 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9245 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9246 f->iconified = 1;
9247 f->visible = 1;
9248 f->async_iconified = 1;
9249 f->async_visible = 0;
9250 UNBLOCK_INPUT;
9251 return;
9253 #endif
9255 #ifdef USE_X_TOOLKIT
9257 if (! FRAME_VISIBLE_P (f))
9259 if (! EQ (Vx_no_window_manager, Qt))
9260 x_wm_set_window_state (f, IconicState);
9261 /* This was XtPopup, but that did nothing for an iconified frame. */
9262 XtMapWidget (f->output_data.x->widget);
9263 /* The server won't give us any event to indicate
9264 that an invisible frame was changed to an icon,
9265 so we have to record it here. */
9266 f->iconified = 1;
9267 f->visible = 1;
9268 f->async_iconified = 1;
9269 f->async_visible = 0;
9270 UNBLOCK_INPUT;
9271 return;
9274 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9275 XtWindow (f->output_data.x->widget),
9276 DefaultScreen (FRAME_X_DISPLAY (f)));
9277 UNBLOCK_INPUT;
9279 if (!result)
9280 error ("Can't notify window manager of iconification");
9282 f->async_iconified = 1;
9283 f->async_visible = 0;
9286 BLOCK_INPUT;
9287 XFlush (FRAME_X_DISPLAY (f));
9288 UNBLOCK_INPUT;
9289 #else /* not USE_X_TOOLKIT */
9291 /* Make sure the X server knows where the window should be positioned,
9292 in case the user deiconifies with the window manager. */
9293 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
9294 x_set_offset (f, f->left_pos, f->top_pos, 0);
9296 /* Since we don't know which revision of X we're running, we'll use both
9297 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9299 /* X11R4: send a ClientMessage to the window manager using the
9300 WM_CHANGE_STATE type. */
9302 XEvent message;
9304 message.xclient.window = FRAME_X_WINDOW (f);
9305 message.xclient.type = ClientMessage;
9306 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
9307 message.xclient.format = 32;
9308 message.xclient.data.l[0] = IconicState;
9310 if (! XSendEvent (FRAME_X_DISPLAY (f),
9311 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9312 False,
9313 SubstructureRedirectMask | SubstructureNotifyMask,
9314 &message))
9316 UNBLOCK_INPUT_RESIGNAL;
9317 error ("Can't notify window manager of iconification");
9321 /* X11R3: set the initial_state field of the window manager hints to
9322 IconicState. */
9323 x_wm_set_window_state (f, IconicState);
9325 if (!FRAME_VISIBLE_P (f))
9327 /* If the frame was withdrawn, before, we must map it. */
9328 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9331 f->async_iconified = 1;
9332 f->async_visible = 0;
9334 XFlush (FRAME_X_DISPLAY (f));
9335 UNBLOCK_INPUT;
9336 #endif /* not USE_X_TOOLKIT */
9340 /* Free X resources of frame F. */
9342 void
9343 x_free_frame_resources (f)
9344 struct frame *f;
9346 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9347 Lisp_Object bar;
9348 struct scroll_bar *b;
9350 BLOCK_INPUT;
9352 /* If a display connection is dead, don't try sending more
9353 commands to the X server. */
9354 if (dpyinfo->display)
9356 if (f->output_data.x->icon_desc)
9357 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9359 #ifdef USE_X_TOOLKIT
9360 /* Explicitly destroy the scroll bars of the frame. Without
9361 this, we get "BadDrawable" errors from the toolkit later on,
9362 presumably from expose events generated for the disappearing
9363 toolkit scroll bars. */
9364 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9366 b = XSCROLL_BAR (bar);
9367 x_scroll_bar_remove (b);
9369 #endif
9371 #ifdef HAVE_X_I18N
9372 if (FRAME_XIC (f))
9373 free_frame_xic (f);
9374 #endif
9376 #ifdef USE_X_TOOLKIT
9377 if (f->output_data.x->widget)
9379 XtDestroyWidget (f->output_data.x->widget);
9380 f->output_data.x->widget = NULL;
9382 /* Tooltips don't have widgets, only a simple X window, even if
9383 we are using a toolkit. */
9384 else if (FRAME_X_WINDOW (f))
9385 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9387 free_frame_menubar (f);
9388 #else /* !USE_X_TOOLKIT */
9390 #ifdef USE_GTK
9391 /* In the GTK version, tooltips are normal X
9392 frames. We must check and free both types. */
9393 if (FRAME_GTK_OUTER_WIDGET (f))
9395 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9396 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9397 FRAME_GTK_OUTER_WIDGET (f) = 0;
9399 #endif /* USE_GTK */
9401 if (FRAME_X_WINDOW (f))
9402 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9403 #endif /* !USE_X_TOOLKIT */
9405 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
9406 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
9407 unload_color (f, f->output_data.x->cursor_pixel);
9408 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9409 unload_color (f, f->output_data.x->border_pixel);
9410 unload_color (f, f->output_data.x->mouse_pixel);
9412 if (f->output_data.x->scroll_bar_background_pixel != -1)
9413 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9414 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9415 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9416 #ifdef USE_TOOLKIT_SCROLL_BARS
9417 /* Scrollbar shadow colors. */
9418 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9419 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9420 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9421 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9422 #endif /* USE_TOOLKIT_SCROLL_BARS */
9423 if (f->output_data.x->white_relief.allocated_p)
9424 unload_color (f, f->output_data.x->white_relief.pixel);
9425 if (f->output_data.x->black_relief.allocated_p)
9426 unload_color (f, f->output_data.x->black_relief.pixel);
9428 if (FRAME_FACE_CACHE (f))
9429 free_frame_faces (f);
9431 x_free_gcs (f);
9432 XFlush (FRAME_X_DISPLAY (f));
9435 if (f->output_data.x->saved_menu_event)
9436 xfree (f->output_data.x->saved_menu_event);
9438 xfree (f->output_data.x);
9439 f->output_data.x = NULL;
9441 if (f == dpyinfo->x_focus_frame)
9442 dpyinfo->x_focus_frame = 0;
9443 if (f == dpyinfo->x_focus_event_frame)
9444 dpyinfo->x_focus_event_frame = 0;
9445 if (f == dpyinfo->x_highlight_frame)
9446 dpyinfo->x_highlight_frame = 0;
9448 if (f == dpyinfo->mouse_face_mouse_frame)
9450 dpyinfo->mouse_face_beg_row
9451 = dpyinfo->mouse_face_beg_col = -1;
9452 dpyinfo->mouse_face_end_row
9453 = dpyinfo->mouse_face_end_col = -1;
9454 dpyinfo->mouse_face_window = Qnil;
9455 dpyinfo->mouse_face_deferred_gc = 0;
9456 dpyinfo->mouse_face_mouse_frame = 0;
9459 UNBLOCK_INPUT;
9463 /* Destroy the X window of frame F. */
9465 void
9466 x_destroy_window (f)
9467 struct frame *f;
9469 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9471 /* If a display connection is dead, don't try sending more
9472 commands to the X server. */
9473 if (dpyinfo->display != 0)
9474 x_free_frame_resources (f);
9476 dpyinfo->reference_count--;
9480 /* Setting window manager hints. */
9482 /* Set the normal size hints for the window manager, for frame F.
9483 FLAGS is the flags word to use--or 0 meaning preserve the flags
9484 that the window now has.
9485 If USER_POSITION is nonzero, we set the USPosition
9486 flag (this is useful when FLAGS is 0).
9487 The GTK version is in gtkutils.c */
9489 #ifndef USE_GTK
9490 void
9491 x_wm_set_size_hint (f, flags, user_position)
9492 struct frame *f;
9493 long flags;
9494 int user_position;
9496 XSizeHints size_hints;
9498 #ifdef USE_X_TOOLKIT
9499 Arg al[2];
9500 int ac = 0;
9501 Dimension widget_width, widget_height;
9502 #endif
9504 Window window = FRAME_OUTER_WINDOW (f);
9506 /* Setting PMaxSize caused various problems. */
9507 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9509 size_hints.x = f->left_pos;
9510 size_hints.y = f->top_pos;
9512 #ifdef USE_X_TOOLKIT
9513 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
9514 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
9515 XtGetValues (f->output_data.x->widget, al, ac);
9516 size_hints.height = widget_height;
9517 size_hints.width = widget_width;
9518 #else /* not USE_X_TOOLKIT */
9519 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9520 size_hints.width = FRAME_PIXEL_WIDTH (f);
9521 #endif /* not USE_X_TOOLKIT */
9523 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9524 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9525 size_hints.max_width
9526 = FRAME_X_DISPLAY_INFO (f)->width - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9527 size_hints.max_height
9528 = FRAME_X_DISPLAY_INFO (f)->height - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9530 /* Calculate the base and minimum sizes.
9532 (When we use the X toolkit, we don't do it here.
9533 Instead we copy the values that the widgets are using, below.) */
9534 #ifndef USE_X_TOOLKIT
9536 int base_width, base_height;
9537 int min_rows = 0, min_cols = 0;
9539 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9540 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9542 check_frame_size (f, &min_rows, &min_cols);
9544 /* The window manager uses the base width hints to calculate the
9545 current number of rows and columns in the frame while
9546 resizing; min_width and min_height aren't useful for this
9547 purpose, since they might not give the dimensions for a
9548 zero-row, zero-column frame.
9550 We use the base_width and base_height members if we have
9551 them; otherwise, we set the min_width and min_height members
9552 to the size for a zero x zero frame. */
9554 size_hints.flags |= PBaseSize;
9555 size_hints.base_width = base_width;
9556 size_hints.base_height = base_height;
9557 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9558 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9561 /* If we don't need the old flags, we don't need the old hint at all. */
9562 if (flags)
9564 size_hints.flags |= flags;
9565 goto no_read;
9567 #endif /* not USE_X_TOOLKIT */
9570 XSizeHints hints; /* Sometimes I hate X Windows... */
9571 long supplied_return;
9572 int value;
9574 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9575 &supplied_return);
9577 #ifdef USE_X_TOOLKIT
9578 size_hints.base_height = hints.base_height;
9579 size_hints.base_width = hints.base_width;
9580 size_hints.min_height = hints.min_height;
9581 size_hints.min_width = hints.min_width;
9582 #endif
9584 if (flags)
9585 size_hints.flags |= flags;
9586 else
9588 if (value == 0)
9589 hints.flags = 0;
9590 if (hints.flags & PSize)
9591 size_hints.flags |= PSize;
9592 if (hints.flags & PPosition)
9593 size_hints.flags |= PPosition;
9594 if (hints.flags & USPosition)
9595 size_hints.flags |= USPosition;
9596 if (hints.flags & USSize)
9597 size_hints.flags |= USSize;
9601 #ifndef USE_X_TOOLKIT
9602 no_read:
9603 #endif
9605 #ifdef PWinGravity
9606 size_hints.win_gravity = f->win_gravity;
9607 size_hints.flags |= PWinGravity;
9609 if (user_position)
9611 size_hints.flags &= ~ PPosition;
9612 size_hints.flags |= USPosition;
9614 #endif /* PWinGravity */
9616 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9618 #endif /* not USE_GTK */
9620 /* Used for IconicState or NormalState */
9622 void
9623 x_wm_set_window_state (f, state)
9624 struct frame *f;
9625 int state;
9627 #ifdef USE_X_TOOLKIT
9628 Arg al[1];
9630 XtSetArg (al[0], XtNinitialState, state);
9631 XtSetValues (f->output_data.x->widget, al, 1);
9632 #else /* not USE_X_TOOLKIT */
9633 Window window = FRAME_X_WINDOW (f);
9635 f->output_data.x->wm_hints.flags |= StateHint;
9636 f->output_data.x->wm_hints.initial_state = state;
9638 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9639 #endif /* not USE_X_TOOLKIT */
9642 void
9643 x_wm_set_icon_pixmap (f, pixmap_id)
9644 struct frame *f;
9645 int pixmap_id;
9647 Pixmap icon_pixmap, icon_mask;
9649 #ifndef USE_X_TOOLKIT
9650 Window window = FRAME_OUTER_WINDOW (f);
9651 #endif
9653 if (pixmap_id > 0)
9655 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9656 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9657 icon_mask = x_bitmap_mask (f, pixmap_id);
9658 f->output_data.x->wm_hints.icon_mask = icon_mask;
9660 else
9662 /* It seems there is no way to turn off use of an icon pixmap.
9663 The following line does it, only if no icon has yet been created,
9664 for some window managers. But with mwm it crashes.
9665 Some people say it should clear the IconPixmapHint bit in this case,
9666 but that doesn't work, and the X consortium said it isn't the
9667 right thing at all. Since there is no way to win,
9668 best to explicitly give up. */
9669 #if 0
9670 f->output_data.x->wm_hints.icon_pixmap = None;
9671 f->output_data.x->wm_hints.icon_mask = None;
9672 #else
9673 return;
9674 #endif
9678 #ifdef USE_GTK
9680 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9681 return;
9684 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
9687 Arg al[1];
9688 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9689 XtSetValues (f->output_data.x->widget, al, 1);
9690 XtSetArg (al[0], XtNiconMask, icon_mask);
9691 XtSetValues (f->output_data.x->widget, al, 1);
9694 #else /* not USE_X_TOOLKIT && not USE_GTK */
9696 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9697 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9699 #endif /* not USE_X_TOOLKIT && not USE_GTK */
9702 void
9703 x_wm_set_icon_position (f, icon_x, icon_y)
9704 struct frame *f;
9705 int icon_x, icon_y;
9707 Window window = FRAME_OUTER_WINDOW (f);
9709 f->output_data.x->wm_hints.flags |= IconPositionHint;
9710 f->output_data.x->wm_hints.icon_x = icon_x;
9711 f->output_data.x->wm_hints.icon_y = icon_y;
9713 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9717 /***********************************************************************
9718 Fonts
9719 ***********************************************************************/
9721 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
9723 struct font_info *
9724 x_get_font_info (f, font_idx)
9725 FRAME_PTR f;
9726 int font_idx;
9728 return (FRAME_X_FONT_TABLE (f) + font_idx);
9732 /* Return a list of names of available fonts matching PATTERN on frame F.
9734 If SIZE is > 0, it is the size (maximum bounds width) of fonts
9735 to be listed.
9737 SIZE < 0 means include scalable fonts.
9739 Frame F null means we have not yet created any frame on X, and
9740 consult the first display in x_display_list. MAXNAMES sets a limit
9741 on how many fonts to match. */
9743 Lisp_Object
9744 x_list_fonts (f, pattern, size, maxnames)
9745 struct frame *f;
9746 Lisp_Object pattern;
9747 int size;
9748 int maxnames;
9750 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
9751 Lisp_Object tem, second_best;
9752 struct x_display_info *dpyinfo
9753 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
9754 Display *dpy = dpyinfo->display;
9755 int try_XLoadQueryFont = 0;
9756 int allow_auto_scaled_font = 0;
9758 if (size < 0)
9760 allow_auto_scaled_font = 1;
9761 size = 0;
9764 patterns = Fassoc (pattern, Valternate_fontname_alist);
9765 if (NILP (patterns))
9766 patterns = Fcons (pattern, Qnil);
9768 if (maxnames == 1 && !size)
9769 /* We can return any single font matching PATTERN. */
9770 try_XLoadQueryFont = 1;
9772 for (; CONSP (patterns); patterns = XCDR (patterns))
9774 int num_fonts;
9775 char **names = NULL;
9777 pattern = XCAR (patterns);
9778 /* See if we cached the result for this particular query.
9779 The cache is an alist of the form:
9780 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
9781 tem = XCDR (dpyinfo->name_list_element);
9782 key = Fcons (Fcons (pattern, make_number (maxnames)),
9783 allow_auto_scaled_font ? Qt : Qnil);
9784 list = Fassoc (key, tem);
9785 if (!NILP (list))
9787 list = Fcdr_safe (list);
9788 /* We have a cashed list. Don't have to get the list again. */
9789 goto label_cached;
9792 /* At first, put PATTERN in the cache. */
9794 BLOCK_INPUT;
9795 x_catch_errors (dpy);
9797 if (try_XLoadQueryFont)
9799 XFontStruct *font;
9800 unsigned long value;
9802 font = XLoadQueryFont (dpy, SDATA (pattern));
9803 if (x_had_errors_p (dpy))
9805 /* This error is perhaps due to insufficient memory on X
9806 server. Let's just ignore it. */
9807 font = NULL;
9808 x_clear_errors (dpy);
9811 if (font
9812 && XGetFontProperty (font, XA_FONT, &value))
9814 char *name = (char *) XGetAtomName (dpy, (Atom) value);
9815 int len = strlen (name);
9816 char *tmp;
9818 /* If DXPC (a Differential X Protocol Compressor)
9819 Ver.3.7 is running, XGetAtomName will return null
9820 string. We must avoid such a name. */
9821 if (len == 0)
9822 try_XLoadQueryFont = 0;
9823 else
9825 num_fonts = 1;
9826 names = (char **) alloca (sizeof (char *));
9827 /* Some systems only allow alloca assigned to a
9828 simple var. */
9829 tmp = (char *) alloca (len + 1); names[0] = tmp;
9830 bcopy (name, names[0], len + 1);
9831 XFree (name);
9834 else
9835 try_XLoadQueryFont = 0;
9837 if (font)
9838 XFreeFont (dpy, font);
9841 if (!try_XLoadQueryFont)
9843 /* We try at least 10 fonts because XListFonts will return
9844 auto-scaled fonts at the head. */
9845 if (maxnames < 0)
9847 int limit;
9849 for (limit = 500;;)
9851 names = XListFonts (dpy, SDATA (pattern), limit, &num_fonts);
9852 if (num_fonts == limit)
9854 BLOCK_INPUT;
9855 XFreeFontNames (names);
9856 UNBLOCK_INPUT;
9857 limit *= 2;
9859 else
9860 break;
9863 else
9864 names = XListFonts (dpy, SDATA (pattern), max (maxnames, 10),
9865 &num_fonts);
9867 if (x_had_errors_p (dpy))
9869 /* This error is perhaps due to insufficient memory on X
9870 server. Let's just ignore it. */
9871 names = NULL;
9872 x_clear_errors (dpy);
9876 x_uncatch_errors ();
9877 UNBLOCK_INPUT;
9879 if (names)
9881 int i;
9883 /* Make a list of all the fonts we got back.
9884 Store that in the font cache for the display. */
9885 for (i = 0; i < num_fonts; i++)
9887 int width = 0;
9888 char *p = names[i];
9889 int average_width = -1, resx = 0, dashes = 0;
9891 /* Count the number of dashes in NAMES[I]. If there are
9892 14 dashes, the field value following 9th dash
9893 (RESOLUTION_X) is nonzero, and the field value
9894 following 12th dash (AVERAGE_WIDTH) is 0, this is a
9895 auto-scaled font which is usually too ugly to be used
9896 for editing. Let's ignore it. */
9897 while (*p)
9898 if (*p++ == '-')
9900 dashes++;
9901 if (dashes == 7) /* PIXEL_SIZE field */
9902 width = atoi (p);
9903 else if (dashes == 9)
9904 resx = atoi (p);
9905 else if (dashes == 12) /* AVERAGE_WIDTH field */
9906 average_width = atoi (p);
9909 if (allow_auto_scaled_font
9910 || dashes < 14 || average_width != 0 || resx == 0)
9912 tem = build_string (names[i]);
9913 if (NILP (Fassoc (tem, list)))
9915 if (STRINGP (Vx_pixel_size_width_font_regexp)
9916 && ((fast_c_string_match_ignore_case
9917 (Vx_pixel_size_width_font_regexp, names[i]))
9918 >= 0))
9919 /* We can set the value of PIXEL_SIZE to the
9920 width of this font. */
9921 list = Fcons (Fcons (tem, make_number (width)), list);
9922 else
9923 /* For the moment, width is not known. */
9924 list = Fcons (Fcons (tem, Qnil), list);
9929 if (!try_XLoadQueryFont)
9931 BLOCK_INPUT;
9932 XFreeFontNames (names);
9933 UNBLOCK_INPUT;
9937 /* Now store the result in the cache. */
9938 XSETCDR (dpyinfo->name_list_element,
9939 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
9941 label_cached:
9942 if (NILP (list)) continue; /* Try the remaining alternatives. */
9944 newlist = second_best = Qnil;
9945 /* Make a list of the fonts that have the right width. */
9946 for (; CONSP (list); list = XCDR (list))
9948 int found_size;
9950 tem = XCAR (list);
9952 if (!CONSP (tem) || NILP (XCAR (tem)))
9953 continue;
9954 if (!size)
9956 newlist = Fcons (XCAR (tem), newlist);
9957 continue;
9960 if (!INTEGERP (XCDR (tem)))
9962 /* Since we have not yet known the size of this font, we
9963 must try slow function call XLoadQueryFont. */
9964 XFontStruct *thisinfo;
9966 BLOCK_INPUT;
9967 x_catch_errors (dpy);
9968 thisinfo = XLoadQueryFont (dpy,
9969 SDATA (XCAR (tem)));
9970 if (x_had_errors_p (dpy))
9972 /* This error is perhaps due to insufficient memory on X
9973 server. Let's just ignore it. */
9974 thisinfo = NULL;
9975 x_clear_errors (dpy);
9977 x_uncatch_errors ();
9978 UNBLOCK_INPUT;
9980 if (thisinfo)
9982 XSETCDR (tem,
9983 (thisinfo->min_bounds.width == 0
9984 ? make_number (0)
9985 : make_number (thisinfo->max_bounds.width)));
9986 BLOCK_INPUT;
9987 XFreeFont (dpy, thisinfo);
9988 UNBLOCK_INPUT;
9990 else
9991 /* For unknown reason, the previous call of XListFont had
9992 returned a font which can't be opened. Record the size
9993 as 0 not to try to open it again. */
9994 XSETCDR (tem, make_number (0));
9997 found_size = XINT (XCDR (tem));
9998 if (found_size == size)
9999 newlist = Fcons (XCAR (tem), newlist);
10000 else if (found_size > 0)
10002 if (NILP (second_best))
10003 second_best = tem;
10004 else if (found_size < size)
10006 if (XINT (XCDR (second_best)) > size
10007 || XINT (XCDR (second_best)) < found_size)
10008 second_best = tem;
10010 else
10012 if (XINT (XCDR (second_best)) > size
10013 && XINT (XCDR (second_best)) > found_size)
10014 second_best = tem;
10018 if (!NILP (newlist))
10019 break;
10020 else if (!NILP (second_best))
10022 newlist = Fcons (XCAR (second_best), Qnil);
10023 break;
10027 return newlist;
10031 #if GLYPH_DEBUG
10033 /* Check that FONT is valid on frame F. It is if it can be found in F's
10034 font table. */
10036 static void
10037 x_check_font (f, font)
10038 struct frame *f;
10039 XFontStruct *font;
10041 int i;
10042 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10044 xassert (font != NULL);
10046 for (i = 0; i < dpyinfo->n_fonts; i++)
10047 if (dpyinfo->font_table[i].name
10048 && font == dpyinfo->font_table[i].font)
10049 break;
10051 xassert (i < dpyinfo->n_fonts);
10054 #endif /* GLYPH_DEBUG != 0 */
10056 /* Set *W to the minimum width, *H to the minimum font height of FONT.
10057 Note: There are (broken) X fonts out there with invalid XFontStruct
10058 min_bounds contents. For example, handa@etl.go.jp reports that
10059 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
10060 have font->min_bounds.width == 0. */
10062 static INLINE void
10063 x_font_min_bounds (font, w, h)
10064 XFontStruct *font;
10065 int *w, *h;
10067 *h = FONT_HEIGHT (font);
10068 *w = font->min_bounds.width;
10070 /* Try to handle the case where FONT->min_bounds has invalid
10071 contents. Since the only font known to have invalid min_bounds
10072 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
10073 if (*w <= 0)
10074 *w = font->max_bounds.width;
10078 /* Compute the smallest character width and smallest font height over
10079 all fonts available on frame F. Set the members smallest_char_width
10080 and smallest_font_height in F's x_display_info structure to
10081 the values computed. Value is non-zero if smallest_font_height or
10082 smallest_char_width become smaller than they were before. */
10084 static int
10085 x_compute_min_glyph_bounds (f)
10086 struct frame *f;
10088 int i;
10089 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10090 XFontStruct *font;
10091 int old_width = dpyinfo->smallest_char_width;
10092 int old_height = dpyinfo->smallest_font_height;
10094 dpyinfo->smallest_font_height = 100000;
10095 dpyinfo->smallest_char_width = 100000;
10097 for (i = 0; i < dpyinfo->n_fonts; ++i)
10098 if (dpyinfo->font_table[i].name)
10100 struct font_info *fontp = dpyinfo->font_table + i;
10101 int w, h;
10103 font = (XFontStruct *) fontp->font;
10104 xassert (font != (XFontStruct *) ~0);
10105 x_font_min_bounds (font, &w, &h);
10107 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
10108 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
10111 xassert (dpyinfo->smallest_char_width > 0
10112 && dpyinfo->smallest_font_height > 0);
10114 return (dpyinfo->n_fonts == 1
10115 || dpyinfo->smallest_char_width < old_width
10116 || dpyinfo->smallest_font_height < old_height);
10120 /* Load font named FONTNAME of the size SIZE for frame F, and return a
10121 pointer to the structure font_info while allocating it dynamically.
10122 If SIZE is 0, load any size of font.
10123 If loading is failed, return NULL. */
10125 struct font_info *
10126 x_load_font (f, fontname, size)
10127 struct frame *f;
10128 register char *fontname;
10129 int size;
10131 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10132 Lisp_Object font_names;
10134 /* Get a list of all the fonts that match this name. Once we
10135 have a list of matching fonts, we compare them against the fonts
10136 we already have by comparing names. */
10137 font_names = x_list_fonts (f, build_string (fontname), size, 1);
10139 if (!NILP (font_names))
10141 Lisp_Object tail;
10142 int i;
10144 for (i = 0; i < dpyinfo->n_fonts; i++)
10145 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
10146 if (dpyinfo->font_table[i].name
10147 && (!strcmp (dpyinfo->font_table[i].name,
10148 SDATA (XCAR (tail)))
10149 || !strcmp (dpyinfo->font_table[i].full_name,
10150 SDATA (XCAR (tail)))))
10151 return (dpyinfo->font_table + i);
10154 /* Load the font and add it to the table. */
10156 char *full_name;
10157 XFontStruct *font;
10158 struct font_info *fontp;
10159 unsigned long value;
10160 int i;
10162 /* If we have found fonts by x_list_font, load one of them. If
10163 not, we still try to load a font by the name given as FONTNAME
10164 because XListFonts (called in x_list_font) of some X server has
10165 a bug of not finding a font even if the font surely exists and
10166 is loadable by XLoadQueryFont. */
10167 if (size > 0 && !NILP (font_names))
10168 fontname = (char *) SDATA (XCAR (font_names));
10170 BLOCK_INPUT;
10171 x_catch_errors (FRAME_X_DISPLAY (f));
10172 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
10173 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
10175 /* This error is perhaps due to insufficient memory on X
10176 server. Let's just ignore it. */
10177 font = NULL;
10178 x_clear_errors (FRAME_X_DISPLAY (f));
10180 x_uncatch_errors ();
10181 UNBLOCK_INPUT;
10182 if (!font)
10183 return NULL;
10185 /* Find a free slot in the font table. */
10186 for (i = 0; i < dpyinfo->n_fonts; ++i)
10187 if (dpyinfo->font_table[i].name == NULL)
10188 break;
10190 /* If no free slot found, maybe enlarge the font table. */
10191 if (i == dpyinfo->n_fonts
10192 && dpyinfo->n_fonts == dpyinfo->font_table_size)
10194 int sz;
10195 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
10196 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
10197 dpyinfo->font_table
10198 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
10201 fontp = dpyinfo->font_table + i;
10202 if (i == dpyinfo->n_fonts)
10203 ++dpyinfo->n_fonts;
10205 /* Now fill in the slots of *FONTP. */
10206 BLOCK_INPUT;
10207 bzero (fontp, sizeof (*fontp));
10208 fontp->font = font;
10209 fontp->font_idx = i;
10210 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
10211 bcopy (fontname, fontp->name, strlen (fontname) + 1);
10213 if (font->min_bounds.width == font->max_bounds.width)
10215 /* Fixed width font. */
10216 fontp->average_width = fontp->space_width = font->min_bounds.width;
10218 else
10220 XChar2b char2b;
10221 XCharStruct *pcm;
10223 char2b.byte1 = 0x00, char2b.byte2 = 0x20;
10224 pcm = x_per_char_metric (font, &char2b, 0);
10225 if (pcm)
10226 fontp->space_width = pcm->width;
10227 else
10228 fontp->space_width = FONT_WIDTH (font);
10230 fontp->average_width
10231 = (XGetFontProperty (font, dpyinfo->Xatom_AVERAGE_WIDTH, &value)
10232 ? (long) value / 10 : 0);
10233 if (fontp->average_width < 0)
10234 fontp->average_width = - fontp->average_width;
10235 if (fontp->average_width == 0)
10237 if (pcm)
10239 int width = pcm->width;
10240 for (char2b.byte2 = 33; char2b.byte2 <= 126; char2b.byte2++)
10241 if ((pcm = x_per_char_metric (font, &char2b, 0)) != NULL)
10242 width += pcm->width;
10243 fontp->average_width = width / 95;
10245 else
10246 fontp->average_width = FONT_WIDTH (font);
10250 /* Try to get the full name of FONT. Put it in FULL_NAME. */
10251 full_name = 0;
10252 if (XGetFontProperty (font, XA_FONT, &value))
10254 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
10255 char *p = name;
10256 int dashes = 0;
10258 /* Count the number of dashes in the "full name".
10259 If it is too few, this isn't really the font's full name,
10260 so don't use it.
10261 In X11R4, the fonts did not come with their canonical names
10262 stored in them. */
10263 while (*p)
10265 if (*p == '-')
10266 dashes++;
10267 p++;
10270 if (dashes >= 13)
10272 full_name = (char *) xmalloc (p - name + 1);
10273 bcopy (name, full_name, p - name + 1);
10276 XFree (name);
10279 if (full_name != 0)
10280 fontp->full_name = full_name;
10281 else
10282 fontp->full_name = fontp->name;
10284 fontp->size = font->max_bounds.width;
10285 fontp->height = FONT_HEIGHT (font);
10287 if (NILP (font_names))
10289 /* We come here because of a bug of XListFonts mentioned at
10290 the head of this block. Let's store this information in
10291 the cache for x_list_fonts. */
10292 Lisp_Object lispy_name = build_string (fontname);
10293 Lisp_Object lispy_full_name = build_string (fontp->full_name);
10294 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
10295 Qnil);
10297 XSETCDR (dpyinfo->name_list_element,
10298 Fcons (Fcons (key,
10299 Fcons (Fcons (lispy_full_name,
10300 make_number (fontp->size)),
10301 Qnil)),
10302 XCDR (dpyinfo->name_list_element)));
10303 if (full_name)
10305 key = Fcons (Fcons (lispy_full_name, make_number (256)),
10306 Qnil);
10307 XSETCDR (dpyinfo->name_list_element,
10308 Fcons (Fcons (key,
10309 Fcons (Fcons (lispy_full_name,
10310 make_number (fontp->size)),
10311 Qnil)),
10312 XCDR (dpyinfo->name_list_element)));
10316 /* The slot `encoding' specifies how to map a character
10317 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
10318 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
10319 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
10320 2:0xA020..0xFF7F). For the moment, we don't know which charset
10321 uses this font. So, we set information in fontp->encoding[1]
10322 which is never used by any charset. If mapping can't be
10323 decided, set FONT_ENCODING_NOT_DECIDED. */
10324 fontp->encoding[1]
10325 = (font->max_byte1 == 0
10326 /* 1-byte font */
10327 ? (font->min_char_or_byte2 < 0x80
10328 ? (font->max_char_or_byte2 < 0x80
10329 ? 0 /* 0x20..0x7F */
10330 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
10331 : 1) /* 0xA0..0xFF */
10332 /* 2-byte font */
10333 : (font->min_byte1 < 0x80
10334 ? (font->max_byte1 < 0x80
10335 ? (font->min_char_or_byte2 < 0x80
10336 ? (font->max_char_or_byte2 < 0x80
10337 ? 0 /* 0x2020..0x7F7F */
10338 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
10339 : 3) /* 0x20A0..0x7FFF */
10340 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
10341 : (font->min_char_or_byte2 < 0x80
10342 ? (font->max_char_or_byte2 < 0x80
10343 ? 2 /* 0xA020..0xFF7F */
10344 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
10345 : 1))); /* 0xA0A0..0xFFFF */
10347 fontp->baseline_offset
10348 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
10349 ? (long) value : 0);
10350 fontp->relative_compose
10351 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
10352 ? (long) value : 0);
10353 fontp->default_ascent
10354 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
10355 ? (long) value : 0);
10357 /* Set global flag fonts_changed_p to non-zero if the font loaded
10358 has a character with a smaller width than any other character
10359 before, or if the font loaded has a smaller height than any
10360 other font loaded before. If this happens, it will make a
10361 glyph matrix reallocation necessary. */
10362 fonts_changed_p |= x_compute_min_glyph_bounds (f);
10363 UNBLOCK_INPUT;
10364 return fontp;
10369 /* Return a pointer to struct font_info of a font named FONTNAME for
10370 frame F. If no such font is loaded, return NULL. */
10372 struct font_info *
10373 x_query_font (f, fontname)
10374 struct frame *f;
10375 register char *fontname;
10377 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10378 int i;
10380 for (i = 0; i < dpyinfo->n_fonts; i++)
10381 if (dpyinfo->font_table[i].name
10382 && (!xstricmp (dpyinfo->font_table[i].name, fontname)
10383 || !xstricmp (dpyinfo->font_table[i].full_name, fontname)))
10384 return (dpyinfo->font_table + i);
10385 return NULL;
10389 /* Find a CCL program for a font specified by FONTP, and set the member
10390 `encoder' of the structure. */
10392 void
10393 x_find_ccl_program (fontp)
10394 struct font_info *fontp;
10396 Lisp_Object list, elt;
10398 elt = Qnil;
10399 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
10401 elt = XCAR (list);
10402 if (CONSP (elt)
10403 && STRINGP (XCAR (elt))
10404 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
10405 >= 0)
10406 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
10407 >= 0)))
10408 break;
10411 if (! NILP (list))
10413 struct ccl_program *ccl
10414 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
10416 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
10417 xfree (ccl);
10418 else
10419 fontp->font_encoder = ccl;
10425 /***********************************************************************
10426 Initialization
10427 ***********************************************************************/
10429 #ifdef USE_X_TOOLKIT
10430 static XrmOptionDescRec emacs_options[] = {
10431 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
10432 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
10434 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
10435 XrmoptionSepArg, NULL},
10436 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
10438 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10439 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10440 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10441 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10442 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10443 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
10444 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
10447 /* Whether atimer for Xt timeouts is activated or not. */
10449 static int x_timeout_atimer_activated_flag;
10451 #endif /* USE_X_TOOLKIT */
10453 static int x_initialized;
10455 #ifdef HAVE_X_SM
10456 static int x_session_initialized;
10457 #endif
10459 #ifdef MULTI_KBOARD
10460 /* Test whether two display-name strings agree up to the dot that separates
10461 the screen number from the server number. */
10462 static int
10463 same_x_server (name1, name2)
10464 const char *name1, *name2;
10466 int seen_colon = 0;
10467 const unsigned char *system_name = SDATA (Vsystem_name);
10468 int system_name_length = strlen (system_name);
10469 int length_until_period = 0;
10471 while (system_name[length_until_period] != 0
10472 && system_name[length_until_period] != '.')
10473 length_until_period++;
10475 /* Treat `unix' like an empty host name. */
10476 if (! strncmp (name1, "unix:", 5))
10477 name1 += 4;
10478 if (! strncmp (name2, "unix:", 5))
10479 name2 += 4;
10480 /* Treat this host's name like an empty host name. */
10481 if (! strncmp (name1, system_name, system_name_length)
10482 && name1[system_name_length] == ':')
10483 name1 += system_name_length;
10484 if (! strncmp (name2, system_name, system_name_length)
10485 && name2[system_name_length] == ':')
10486 name2 += system_name_length;
10487 /* Treat this host's domainless name like an empty host name. */
10488 if (! strncmp (name1, system_name, length_until_period)
10489 && name1[length_until_period] == ':')
10490 name1 += length_until_period;
10491 if (! strncmp (name2, system_name, length_until_period)
10492 && name2[length_until_period] == ':')
10493 name2 += length_until_period;
10495 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
10497 if (*name1 == ':')
10498 seen_colon++;
10499 if (seen_colon && *name1 == '.')
10500 return 1;
10502 return (seen_colon
10503 && (*name1 == '.' || *name1 == '\0')
10504 && (*name2 == '.' || *name2 == '\0'));
10506 #endif
10508 /* Count number of set bits in mask and number of bits to shift to
10509 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
10510 to 5. */
10511 static void
10512 get_bits_and_offset (mask, bits, offset)
10513 unsigned long mask;
10514 int *bits;
10515 int *offset;
10517 int nr = 0;
10518 int off = 0;
10520 while (!(mask & 1))
10522 off++;
10523 mask >>= 1;
10526 while (mask & 1)
10528 nr++;
10529 mask >>= 1;
10532 *offset = off;
10533 *bits = nr;
10536 /* Return 1 if display DISPLAY is available for use, 0 otherwise.
10537 But don't permanently open it, just test its availability. */
10540 x_display_ok (display)
10541 const char *display;
10543 int dpy_ok = 1;
10544 Display *dpy;
10546 dpy = XOpenDisplay (display);
10547 if (dpy)
10548 XCloseDisplay (dpy);
10549 else
10550 dpy_ok = 0;
10551 return dpy_ok;
10554 /* Open a connection to X display DISPLAY_NAME, and return
10555 the structure that describes the open display.
10556 If we cannot contact the display, return null. */
10558 struct x_display_info *
10559 x_term_init (display_name, xrm_option, resource_name)
10560 Lisp_Object display_name;
10561 char *xrm_option;
10562 char *resource_name;
10564 int connection;
10565 Display *dpy;
10566 struct terminal *terminal;
10567 struct x_display_info *dpyinfo;
10568 XrmDatabase xrdb;
10570 BLOCK_INPUT;
10572 if (!x_initialized)
10574 x_initialize ();
10575 ++x_initialized;
10578 if (! x_display_ok (SDATA (display_name)))
10579 error ("Display %s can't be opened", SDATA (display_name));
10581 #ifdef USE_GTK
10583 #define NUM_ARGV 10
10584 int argc;
10585 char *argv[NUM_ARGV];
10586 char **argv2 = argv;
10587 GdkAtom atom;
10589 #ifndef HAVE_GTK_MULTIDISPLAY
10590 if (!EQ (Vinitial_window_system, intern ("x")))
10591 error ("Sorry, you cannot connect to X servers with the GTK toolkit");
10592 #endif
10594 if (x_initialized++ > 1)
10596 #ifdef HAVE_GTK_MULTIDISPLAY
10597 /* Opening another display. If xg_display_open returns less
10598 than zero, we are probably on GTK 2.0, which can only handle
10599 one display. GTK 2.2 or later can handle more than one. */
10600 if (xg_display_open (SDATA (display_name), &dpy) < 0)
10601 #endif
10602 error ("Sorry, this version of GTK can only handle one display");
10604 else
10606 for (argc = 0; argc < NUM_ARGV; ++argc)
10607 argv[argc] = 0;
10609 argc = 0;
10610 argv[argc++] = initial_argv[0];
10612 if (! NILP (display_name))
10614 argv[argc++] = "--display";
10615 argv[argc++] = SDATA (display_name);
10618 argv[argc++] = "--name";
10619 argv[argc++] = resource_name;
10621 XSetLocaleModifiers ("");
10623 gtk_init (&argc, &argv2);
10625 /* gtk_init does set_locale. We must fix locale after calling it. */
10626 fixup_locale ();
10627 xg_initialize ();
10629 dpy = GDK_DISPLAY ();
10631 /* NULL window -> events for all windows go to our function */
10632 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10634 /* Load our own gtkrc if it exists. */
10636 char *file = "~/.emacs.d/gtkrc";
10637 Lisp_Object s, abs_file;
10639 s = make_string (file, strlen (file));
10640 abs_file = Fexpand_file_name (s, Qnil);
10642 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10643 gtk_rc_parse (SDATA (abs_file));
10646 XSetErrorHandler (x_error_handler);
10647 XSetIOErrorHandler (x_io_error_quitter);
10650 #else /* not USE_GTK */
10651 #ifdef USE_X_TOOLKIT
10652 /* weiner@footloose.sps.mot.com reports that this causes
10653 errors with X11R5:
10654 X protocol error: BadAtom (invalid Atom parameter)
10655 on protocol request 18skiloaf.
10656 So let's not use it until R6. */
10657 #ifdef HAVE_X11XTR6
10658 XtSetLanguageProc (NULL, NULL, NULL);
10659 #endif
10662 int argc = 0;
10663 char *argv[3];
10665 argv[0] = "";
10666 argc = 1;
10667 if (xrm_option)
10669 argv[argc++] = "-xrm";
10670 argv[argc++] = xrm_option;
10672 turn_on_atimers (0);
10673 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10674 resource_name, EMACS_CLASS,
10675 emacs_options, XtNumber (emacs_options),
10676 &argc, argv);
10677 turn_on_atimers (1);
10679 #ifdef HAVE_X11XTR6
10680 /* I think this is to compensate for XtSetLanguageProc. */
10681 fixup_locale ();
10682 #endif
10685 #else /* not USE_X_TOOLKIT */
10686 XSetLocaleModifiers ("");
10687 dpy = XOpenDisplay (SDATA (display_name));
10688 #endif /* not USE_X_TOOLKIT */
10689 #endif /* not USE_GTK*/
10691 /* Detect failure. */
10692 if (dpy == 0)
10694 UNBLOCK_INPUT;
10695 return 0;
10698 /* We have definitely succeeded. Record the new connection. */
10700 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10701 bzero (dpyinfo, sizeof *dpyinfo);
10703 terminal = x_create_terminal (dpyinfo);
10705 #ifdef MULTI_KBOARD
10707 struct x_display_info *share;
10708 Lisp_Object tail;
10710 for (share = x_display_list, tail = x_display_name_list; share;
10711 share = share->next, tail = XCDR (tail))
10712 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10713 SDATA (display_name)))
10714 break;
10715 if (share)
10716 terminal->kboard = share->terminal->kboard;
10717 else
10719 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10720 init_kboard (terminal->kboard);
10721 terminal->kboard->Vwindow_system = intern ("x");
10722 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10724 char *vendor = ServerVendor (dpy);
10725 UNBLOCK_INPUT;
10726 terminal->kboard->Vsystem_key_alist
10727 = call1 (Qvendor_specific_keysyms,
10728 vendor ? build_string (vendor) : empty_unibyte_string);
10729 BLOCK_INPUT;
10732 terminal->kboard->next_kboard = all_kboards;
10733 all_kboards = terminal->kboard;
10734 /* Don't let the initial kboard remain current longer than necessary.
10735 That would cause problems if a file loaded on startup tries to
10736 prompt in the mini-buffer. */
10737 if (current_kboard == initial_kboard)
10738 current_kboard = terminal->kboard;
10740 terminal->kboard->reference_count++;
10742 #endif
10744 /* Put this display on the chain. */
10745 dpyinfo->next = x_display_list;
10746 x_display_list = dpyinfo;
10748 /* Put it on x_display_name_list as well, to keep them parallel. */
10749 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10750 x_display_name_list);
10751 dpyinfo->name_list_element = XCAR (x_display_name_list);
10753 dpyinfo->display = dpy;
10755 /* Set the name of the terminal. */
10756 terminal->name = (char *) xmalloc (SBYTES (display_name) + 1);
10757 strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
10758 terminal->name[SBYTES (display_name)] = 0;
10760 #if 0
10761 XSetAfterFunction (x_current_display, x_trace_wire);
10762 #endif /* ! 0 */
10764 dpyinfo->x_id_name
10765 = (char *) xmalloc (SBYTES (Vinvocation_name)
10766 + SBYTES (Vsystem_name)
10767 + 2);
10768 sprintf (dpyinfo->x_id_name, "%s@%s",
10769 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10771 /* Figure out which modifier bits mean what. */
10772 x_find_modifier_meanings (dpyinfo);
10774 /* Get the scroll bar cursor. */
10775 #ifdef USE_GTK
10776 /* We must create a GTK cursor, it is required for GTK widgets. */
10777 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10778 #endif /* USE_GTK */
10780 dpyinfo->vertical_scroll_bar_cursor
10781 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10783 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10784 resource_name, EMACS_CLASS);
10785 #ifdef HAVE_XRMSETDATABASE
10786 XrmSetDatabase (dpyinfo->display, xrdb);
10787 #else
10788 dpyinfo->display->db = xrdb;
10789 #endif
10790 /* Put the rdb where we can find it in a way that works on
10791 all versions. */
10792 dpyinfo->xrdb = xrdb;
10794 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10795 DefaultScreen (dpyinfo->display));
10796 select_visual (dpyinfo);
10797 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10798 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
10799 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
10800 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10801 dpyinfo->client_leader_window = 0;
10802 dpyinfo->grabbed = 0;
10803 dpyinfo->reference_count = 0;
10804 dpyinfo->icon_bitmap_id = -1;
10805 dpyinfo->font_table = NULL;
10806 dpyinfo->n_fonts = 0;
10807 dpyinfo->font_table_size = 0;
10808 dpyinfo->bitmaps = 0;
10809 dpyinfo->bitmaps_size = 0;
10810 dpyinfo->bitmaps_last = 0;
10811 dpyinfo->scratch_cursor_gc = 0;
10812 dpyinfo->mouse_face_mouse_frame = 0;
10813 dpyinfo->mouse_face_deferred_gc = 0;
10814 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10815 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10816 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10817 dpyinfo->mouse_face_window = Qnil;
10818 dpyinfo->mouse_face_overlay = Qnil;
10819 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10820 dpyinfo->mouse_face_defer = 0;
10821 dpyinfo->mouse_face_hidden = 0;
10822 dpyinfo->x_focus_frame = 0;
10823 dpyinfo->x_focus_event_frame = 0;
10824 dpyinfo->x_highlight_frame = 0;
10825 dpyinfo->image_cache = make_image_cache ();
10826 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10828 /* See if we can construct pixel values from RGB values. */
10829 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
10830 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
10832 if (dpyinfo->visual->class == TrueColor)
10834 get_bits_and_offset (dpyinfo->visual->red_mask,
10835 &dpyinfo->red_bits, &dpyinfo->red_offset);
10836 get_bits_and_offset (dpyinfo->visual->blue_mask,
10837 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10838 get_bits_and_offset (dpyinfo->visual->green_mask,
10839 &dpyinfo->green_bits, &dpyinfo->green_offset);
10842 /* See if a private colormap is requested. */
10843 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10845 if (dpyinfo->visual->class == PseudoColor)
10847 Lisp_Object value;
10848 value = display_x_get_resource (dpyinfo,
10849 build_string ("privateColormap"),
10850 build_string ("PrivateColormap"),
10851 Qnil, Qnil);
10852 if (STRINGP (value)
10853 && (!strcmp (SDATA (value), "true")
10854 || !strcmp (SDATA (value), "on")))
10855 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10858 else
10859 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10860 dpyinfo->visual, AllocNone);
10863 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10864 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10865 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10866 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10867 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
10868 pixels = DisplayWidth (dpyinfo->display, screen_number);
10869 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10870 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10871 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
10874 dpyinfo->Xatom_wm_protocols
10875 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10876 dpyinfo->Xatom_wm_take_focus
10877 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10878 dpyinfo->Xatom_wm_save_yourself
10879 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10880 dpyinfo->Xatom_wm_delete_window
10881 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10882 dpyinfo->Xatom_wm_change_state
10883 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10884 dpyinfo->Xatom_wm_configure_denied
10885 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10886 dpyinfo->Xatom_wm_window_moved
10887 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10888 dpyinfo->Xatom_wm_client_leader
10889 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
10890 dpyinfo->Xatom_editres
10891 = XInternAtom (dpyinfo->display, "Editres", False);
10892 dpyinfo->Xatom_CLIPBOARD
10893 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10894 dpyinfo->Xatom_TIMESTAMP
10895 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10896 dpyinfo->Xatom_TEXT
10897 = XInternAtom (dpyinfo->display, "TEXT", False);
10898 dpyinfo->Xatom_COMPOUND_TEXT
10899 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10900 dpyinfo->Xatom_UTF8_STRING
10901 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10902 dpyinfo->Xatom_DELETE
10903 = XInternAtom (dpyinfo->display, "DELETE", False);
10904 dpyinfo->Xatom_MULTIPLE
10905 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10906 dpyinfo->Xatom_INCR
10907 = XInternAtom (dpyinfo->display, "INCR", False);
10908 dpyinfo->Xatom_EMACS_TMP
10909 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10910 dpyinfo->Xatom_TARGETS
10911 = XInternAtom (dpyinfo->display, "TARGETS", False);
10912 dpyinfo->Xatom_NULL
10913 = XInternAtom (dpyinfo->display, "NULL", False);
10914 dpyinfo->Xatom_ATOM_PAIR
10915 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10916 /* For properties of font. */
10917 dpyinfo->Xatom_PIXEL_SIZE
10918 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10919 dpyinfo->Xatom_AVERAGE_WIDTH
10920 = XInternAtom (dpyinfo->display, "AVERAGE_WIDTH", False);
10921 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10922 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10923 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10924 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10925 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10926 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10928 /* Ghostscript support. */
10929 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10930 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10932 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10933 False);
10935 dpyinfo->cut_buffers_initialized = 0;
10937 dpyinfo->x_dnd_atoms_size = 8;
10938 dpyinfo->x_dnd_atoms_length = 0;
10939 dpyinfo->x_dnd_atoms = xmalloc (sizeof (*dpyinfo->x_dnd_atoms)
10940 * dpyinfo->x_dnd_atoms_size);
10942 dpyinfo->net_supported_atoms = NULL;
10943 dpyinfo->nr_net_supported_atoms = 0;
10944 dpyinfo->net_supported_window = 0;
10946 connection = ConnectionNumber (dpyinfo->display);
10947 dpyinfo->connection = connection;
10950 char null_bits[1];
10952 null_bits[0] = 0x00;
10954 dpyinfo->null_pixel
10955 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10956 null_bits, 1, 1, (long) 0, (long) 0,
10961 extern int gray_bitmap_width, gray_bitmap_height;
10962 extern char *gray_bitmap_bits;
10963 dpyinfo->gray
10964 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10965 gray_bitmap_bits,
10966 gray_bitmap_width, gray_bitmap_height,
10967 (unsigned long) 1, (unsigned long) 0, 1);
10970 #ifdef HAVE_X_I18N
10971 xim_initialize (dpyinfo, resource_name);
10972 #endif
10974 #ifdef subprocesses
10975 /* This is only needed for distinguishing keyboard and process input. */
10976 if (connection != 0)
10977 add_keyboard_wait_descriptor (connection);
10978 #endif
10980 #ifndef F_SETOWN_BUG
10981 #ifdef F_SETOWN
10982 #ifdef F_SETOWN_SOCK_NEG
10983 /* stdin is a socket here */
10984 fcntl (connection, F_SETOWN, -getpid ());
10985 #else /* ! defined (F_SETOWN_SOCK_NEG) */
10986 fcntl (connection, F_SETOWN, getpid ());
10987 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
10988 #endif /* ! defined (F_SETOWN) */
10989 #endif /* F_SETOWN_BUG */
10991 #ifdef SIGIO
10992 if (interrupt_input)
10993 init_sigio (connection);
10994 #endif /* ! defined (SIGIO) */
10996 #ifdef USE_LUCID
10998 Display *dpy = dpyinfo->display;
10999 XrmValue d, fr, to;
11000 Font font;
11002 d.addr = (XPointer)&dpy;
11003 d.size = sizeof (Display *);
11004 fr.addr = XtDefaultFont;
11005 fr.size = sizeof (XtDefaultFont);
11006 to.size = sizeof (Font *);
11007 to.addr = (XPointer)&font;
11008 x_catch_errors (dpy);
11009 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
11010 abort ();
11011 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
11012 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
11013 x_uncatch_errors ();
11015 #endif
11017 /* See if we should run in synchronous mode. This is useful
11018 for debugging X code. */
11020 Lisp_Object value;
11021 value = display_x_get_resource (dpyinfo,
11022 build_string ("synchronous"),
11023 build_string ("Synchronous"),
11024 Qnil, Qnil);
11025 if (STRINGP (value)
11026 && (!strcmp (SDATA (value), "true")
11027 || !strcmp (SDATA (value), "on")))
11028 XSynchronize (dpyinfo->display, True);
11032 Lisp_Object value;
11033 value = display_x_get_resource (dpyinfo,
11034 build_string ("useXIM"),
11035 build_string ("UseXIM"),
11036 Qnil, Qnil);
11037 #ifdef USE_XIM
11038 if (STRINGP (value)
11039 && (!strcmp (SDATA (value), "false")
11040 || !strcmp (SDATA (value), "off")))
11041 use_xim = 0;
11042 #else
11043 if (STRINGP (value)
11044 && (!strcmp (SDATA (value), "true")
11045 || !strcmp (SDATA (value), "on")))
11046 use_xim = 1;
11047 #endif
11050 #ifdef HAVE_X_SM
11051 /* Only do this for the very first display in the Emacs session.
11052 Ignore X session management when Emacs was first started on a
11053 tty. */
11054 if (terminal->id == 1)
11055 x_session_initialize (dpyinfo);
11056 #endif
11058 UNBLOCK_INPUT;
11060 return dpyinfo;
11063 /* Get rid of display DPYINFO, deleting all frames on it,
11064 and without sending any more commands to the X server. */
11066 void
11067 x_delete_display (dpyinfo)
11068 struct x_display_info *dpyinfo;
11070 int i;
11071 struct terminal *t;
11073 /* Close all frames and delete the generic struct terminal for this
11074 X display. */
11075 for (t = terminal_list; t; t = t->next_terminal)
11076 if (t->type == output_x_window && t->display_info.x == dpyinfo)
11078 #ifdef HAVE_X_SM
11079 /* Close X session management when we close its display. */
11080 if (t->id == 1 && x_session_have_connection ())
11081 x_session_close();
11082 #endif
11083 delete_terminal (t);
11084 break;
11087 delete_keyboard_wait_descriptor (dpyinfo->connection);
11089 /* Discard this display from x_display_name_list and x_display_list.
11090 We can't use Fdelq because that can quit. */
11091 if (! NILP (x_display_name_list)
11092 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
11093 x_display_name_list = XCDR (x_display_name_list);
11094 else
11096 Lisp_Object tail;
11098 tail = x_display_name_list;
11099 while (CONSP (tail) && CONSP (XCDR (tail)))
11101 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
11103 XSETCDR (tail, XCDR (XCDR (tail)));
11104 break;
11106 tail = XCDR (tail);
11110 if (next_noop_dpyinfo == dpyinfo)
11111 next_noop_dpyinfo = dpyinfo->next;
11113 if (x_display_list == dpyinfo)
11114 x_display_list = dpyinfo->next;
11115 else
11117 struct x_display_info *tail;
11119 for (tail = x_display_list; tail; tail = tail->next)
11120 if (tail->next == dpyinfo)
11121 tail->next = tail->next->next;
11124 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
11125 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
11126 XrmDestroyDatabase (dpyinfo->xrdb);
11127 #endif
11128 #endif
11129 #ifdef HAVE_X_I18N
11130 if (dpyinfo->xim)
11131 xim_close_dpy (dpyinfo);
11132 #endif
11134 /* Free the font names in the font table. */
11135 for (i = 0; i < dpyinfo->n_fonts; i++)
11136 if (dpyinfo->font_table[i].name)
11138 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
11139 xfree (dpyinfo->font_table[i].full_name);
11140 xfree (dpyinfo->font_table[i].name);
11143 if (dpyinfo->font_table)
11145 if (dpyinfo->font_table->font_encoder)
11146 xfree (dpyinfo->font_table->font_encoder);
11147 xfree (dpyinfo->font_table);
11149 if (dpyinfo->x_id_name)
11150 xfree (dpyinfo->x_id_name);
11151 if (dpyinfo->color_cells)
11152 xfree (dpyinfo->color_cells);
11153 xfree (dpyinfo);
11156 #ifdef USE_X_TOOLKIT
11158 /* Atimer callback function for TIMER. Called every 0.1s to process
11159 Xt timeouts, if needed. We must avoid calling XtAppPending as
11160 much as possible because that function does an implicit XFlush
11161 that slows us down. */
11163 static void
11164 x_process_timeouts (timer)
11165 struct atimer *timer;
11167 BLOCK_INPUT;
11168 x_timeout_atimer_activated_flag = 0;
11169 if (toolkit_scroll_bar_interaction || popup_activated ())
11171 while (XtAppPending (Xt_app_con) & XtIMTimer)
11172 XtAppProcessEvent (Xt_app_con, XtIMTimer);
11173 /* Reactivate the atimer for next time. */
11174 x_activate_timeout_atimer ();
11176 UNBLOCK_INPUT;
11179 /* Install an asynchronous timer that processes Xt timeout events
11180 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
11181 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
11182 function whenever these variables are set. This is necessary
11183 because some widget sets use timeouts internally, for example the
11184 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
11185 processed, these widgets don't behave normally. */
11187 void
11188 x_activate_timeout_atimer ()
11190 BLOCK_INPUT;
11191 if (!x_timeout_atimer_activated_flag)
11193 EMACS_TIME interval;
11195 EMACS_SET_SECS_USECS (interval, 0, 100000);
11196 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
11197 x_timeout_atimer_activated_flag = 1;
11199 UNBLOCK_INPUT;
11202 #endif /* USE_X_TOOLKIT */
11205 /* Set up use of X before we make the first connection. */
11207 extern frame_parm_handler x_frame_parm_handlers[];
11209 static struct redisplay_interface x_redisplay_interface =
11211 x_frame_parm_handlers,
11212 x_produce_glyphs,
11213 x_write_glyphs,
11214 x_insert_glyphs,
11215 x_clear_end_of_line,
11216 x_scroll_run,
11217 x_after_update_window_line,
11218 x_update_window_begin,
11219 x_update_window_end,
11220 x_cursor_to,
11221 x_flush,
11222 #ifdef XFlush
11223 x_flush,
11224 #else
11225 0, /* flush_display_optional */
11226 #endif
11227 x_clear_window_mouse_face,
11228 x_get_glyph_overhangs,
11229 x_fix_overlapping_area,
11230 x_draw_fringe_bitmap,
11231 0, /* define_fringe_bitmap */
11232 0, /* destroy_fringe_bitmap */
11233 x_per_char_metric,
11234 x_encode_char,
11235 x_compute_glyph_string_overhangs,
11236 x_draw_glyph_string,
11237 x_define_frame_cursor,
11238 x_clear_frame_area,
11239 x_draw_window_cursor,
11240 x_draw_vertical_window_border,
11241 x_shift_glyphs_for_insert
11245 /* This function is called when the last frame on a display is deleted. */
11246 void
11247 x_delete_terminal (struct terminal *terminal)
11249 struct x_display_info *dpyinfo = terminal->display_info.x;
11250 int i;
11252 /* Protect against recursive calls. Fdelete_frame in
11253 delete_terminal calls us back when it deletes our last frame. */
11254 if (!terminal->name)
11255 return;
11257 BLOCK_INPUT;
11258 /* Free the fonts in the font table. */
11259 for (i = 0; i < dpyinfo->n_fonts; i++)
11260 if (dpyinfo->font_table[i].name)
11262 XFreeFont (dpyinfo->display, dpyinfo->font_table[i].font);
11265 x_destroy_all_bitmaps (dpyinfo);
11266 XSetCloseDownMode (dpyinfo->display, DestroyAll);
11268 #ifdef USE_GTK
11269 xg_display_close (dpyinfo->display);
11270 #else
11271 #ifdef USE_X_TOOLKIT
11272 XtCloseDisplay (dpyinfo->display);
11273 #else
11274 XCloseDisplay (dpyinfo->display);
11275 #endif
11276 #endif /* ! USE_GTK */
11278 x_delete_display (dpyinfo);
11279 UNBLOCK_INPUT;
11282 /* Create a struct terminal, initialize it with the X11 specific
11283 functions and make DISPLAY->TERMINAL point to it. */
11285 static struct terminal *
11286 x_create_terminal (struct x_display_info *dpyinfo)
11288 struct terminal *terminal;
11290 terminal = create_terminal ();
11292 terminal->type = output_x_window;
11293 terminal->display_info.x = dpyinfo;
11294 dpyinfo->terminal = terminal;
11296 /* kboard is initialized in x_term_init. */
11298 terminal->clear_frame_hook = x_clear_frame;
11299 terminal->ins_del_lines_hook = x_ins_del_lines;
11300 terminal->delete_glyphs_hook = x_delete_glyphs;
11301 terminal->ring_bell_hook = XTring_bell;
11302 terminal->reset_terminal_modes_hook = XTreset_terminal_modes;
11303 terminal->set_terminal_modes_hook = XTset_terminal_modes;
11304 terminal->update_begin_hook = x_update_begin;
11305 terminal->update_end_hook = x_update_end;
11306 terminal->set_terminal_window_hook = XTset_terminal_window;
11307 terminal->read_socket_hook = XTread_socket;
11308 terminal->frame_up_to_date_hook = XTframe_up_to_date;
11309 terminal->mouse_position_hook = XTmouse_position;
11310 terminal->frame_rehighlight_hook = XTframe_rehighlight;
11311 terminal->frame_raise_lower_hook = XTframe_raise_lower;
11312 terminal->fullscreen_hook = XTfullscreen_hook;
11313 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
11314 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
11315 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
11316 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
11318 terminal->delete_frame_hook = x_destroy_window;
11319 terminal->delete_terminal_hook = x_delete_terminal;
11321 terminal->rif = &x_redisplay_interface;
11322 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
11323 terminal->char_ins_del_ok = 1;
11324 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
11325 terminal->fast_clear_end_of_line = 1; /* X does this well. */
11326 terminal->memory_below_frame = 0; /* We don't remember what scrolls
11327 off the bottom. */
11329 return terminal;
11332 void
11333 x_initialize ()
11335 baud_rate = 19200;
11337 x_noop_count = 0;
11338 last_tool_bar_item = -1;
11339 any_help_event_p = 0;
11340 ignore_next_mouse_click_timeout = 0;
11341 #ifdef HAVE_X_SM
11342 x_session_initialized = 0;
11343 #endif
11345 #ifdef USE_GTK
11346 current_count = -1;
11347 #endif
11349 /* Try to use interrupt input; if we can't, then start polling. */
11350 Fset_input_interrupt_mode (Qt);
11352 #ifdef USE_X_TOOLKIT
11353 XtToolkitInitialize ();
11355 Xt_app_con = XtCreateApplicationContext ();
11357 /* Register a converter from strings to pixels, which uses
11358 Emacs' color allocation infrastructure. */
11359 XtAppSetTypeConverter (Xt_app_con,
11360 XtRString, XtRPixel, cvt_string_to_pixel,
11361 cvt_string_to_pixel_args,
11362 XtNumber (cvt_string_to_pixel_args),
11363 XtCacheByDisplay, cvt_pixel_dtor);
11365 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
11366 #endif
11368 #ifdef USE_TOOLKIT_SCROLL_BARS
11369 #ifndef USE_GTK
11370 xaw3d_arrow_scroll = False;
11371 xaw3d_pick_top = True;
11372 #endif
11373 #endif
11375 /* Note that there is no real way portable across R3/R4 to get the
11376 original error handler. */
11377 XSetErrorHandler (x_error_handler);
11378 XSetIOErrorHandler (x_io_error_quitter);
11380 /* Disable Window Change signals; they are handled by X events. */
11381 #if 0 /* Don't. We may want to open tty frames later. */
11382 #ifdef SIGWINCH
11383 signal (SIGWINCH, SIG_DFL);
11384 #endif /* SIGWINCH */
11385 #endif
11387 signal (SIGPIPE, x_connection_signal);
11391 void
11392 syms_of_xterm ()
11394 x_error_message = NULL;
11396 staticpro (&x_display_name_list);
11397 x_display_name_list = Qnil;
11399 staticpro (&last_mouse_scroll_bar);
11400 last_mouse_scroll_bar = Qnil;
11402 staticpro (&Qvendor_specific_keysyms);
11403 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
11405 staticpro (&Qutf_8);
11406 Qutf_8 = intern ("utf-8");
11407 staticpro (&Qlatin_1);
11408 Qlatin_1 = intern ("latin-1");
11410 staticpro (&last_mouse_press_frame);
11411 last_mouse_press_frame = Qnil;
11413 DEFVAR_BOOL ("x-use-underline-position-properties",
11414 &x_use_underline_position_properties,
11415 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
11416 A value of nil means ignore them. If you encounter fonts with bogus
11417 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
11418 to 4.1, set this to nil. */);
11419 x_use_underline_position_properties = 1;
11421 DEFVAR_BOOL ("x-underline-at-descent-line",
11422 &x_underline_at_descent_line,
11423 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
11424 A value of nil means to draw the underline according to the value of the
11425 variable `x-use-underline-position-properties', which is usually at the
11426 baseline level. The default value is nil. */);
11427 x_underline_at_descent_line = 0;
11429 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
11430 &x_mouse_click_focus_ignore_position,
11431 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
11432 This variable is only used when the window manager requires that you
11433 click on a frame to select it (give it focus). In that case, a value
11434 of nil, means that the selected window and cursor position changes to
11435 reflect the mouse click position, while a non-nil value means that the
11436 selected window or cursor position is preserved. */);
11437 x_mouse_click_focus_ignore_position = 0;
11439 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
11440 doc: /* What X toolkit scroll bars Emacs uses.
11441 A value of nil means Emacs doesn't use X toolkit scroll bars.
11442 Otherwise, value is a symbol describing the X toolkit. */);
11443 #ifdef USE_TOOLKIT_SCROLL_BARS
11444 #ifdef USE_MOTIF
11445 Vx_toolkit_scroll_bars = intern ("motif");
11446 #elif defined HAVE_XAW3D
11447 Vx_toolkit_scroll_bars = intern ("xaw3d");
11448 #elif USE_GTK
11449 Vx_toolkit_scroll_bars = intern ("gtk");
11450 #else
11451 Vx_toolkit_scroll_bars = intern ("xaw");
11452 #endif
11453 #else
11454 Vx_toolkit_scroll_bars = Qnil;
11455 #endif
11457 staticpro (&last_mouse_motion_frame);
11458 last_mouse_motion_frame = Qnil;
11460 Qmodifier_value = intern ("modifier-value");
11461 Qalt = intern ("alt");
11462 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
11463 Qhyper = intern ("hyper");
11464 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
11465 Qmeta = intern ("meta");
11466 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
11467 Qsuper = intern ("super");
11468 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
11470 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
11471 doc: /* Which keys Emacs uses for the alt modifier.
11472 This should be one of the symbols `alt', `hyper', `meta', `super'.
11473 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
11474 is nil, which is the same as `alt'. */);
11475 Vx_alt_keysym = Qnil;
11477 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
11478 doc: /* Which keys Emacs uses for the hyper modifier.
11479 This should be one of the symbols `alt', `hyper', `meta', `super'.
11480 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
11481 default is nil, which is the same as `hyper'. */);
11482 Vx_hyper_keysym = Qnil;
11484 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
11485 doc: /* Which keys Emacs uses for the meta modifier.
11486 This should be one of the symbols `alt', `hyper', `meta', `super'.
11487 For example, `meta' means use the Meta_L and Meta_R keysyms. The
11488 default is nil, which is the same as `meta'. */);
11489 Vx_meta_keysym = Qnil;
11491 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
11492 doc: /* Which keys Emacs uses for the super modifier.
11493 This should be one of the symbols `alt', `hyper', `meta', `super'.
11494 For example, `super' means use the Super_L and Super_R keysyms. The
11495 default is nil, which is the same as `super'. */);
11496 Vx_super_keysym = Qnil;
11498 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
11499 doc: /* Hash table of character codes indexed by X keysym codes. */);
11500 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
11501 make_float (DEFAULT_REHASH_SIZE),
11502 make_float (DEFAULT_REHASH_THRESHOLD),
11503 Qnil, Qnil, Qnil);
11506 #endif /* HAVE_X_WINDOWS */
11508 /* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
11509 (do not change this comment) */