Merge from emacs--rel--22
[emacs.git] / src / xterm.c
blob77a1b3745f1ef4bb40b7467bd10367315f34e490
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, 2008
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
24 /* Xt features made by Fred Pierresteguy. */
26 #include <config.h>
28 /* On 4.3 these lose if they come after xterm.h. */
29 /* Putting these at the beginning seems to be standard for other .c files. */
30 #include <signal.h>
32 #include <stdio.h>
34 #ifdef HAVE_X_WINDOWS
36 #include "lisp.h"
37 #include "blockinput.h"
39 /* Need syssignal.h for various externs and definitions that may be required
40 by some configurations for calls to signal later in this source file. */
41 #include "syssignal.h"
43 /* This may include sys/types.h, and that somehow loses
44 if this is not done before the other system files. */
45 #include "xterm.h"
46 #include <X11/cursorfont.h>
48 /* Load sys/types.h if not already loaded.
49 In some systems loading it twice is suicidal. */
50 #ifndef makedev
51 #include <sys/types.h>
52 #endif /* makedev */
54 #ifdef BSD_SYSTEM
55 #include <sys/ioctl.h>
56 #endif /* ! defined (BSD_SYSTEM) */
58 #include "systime.h"
60 #ifndef INCLUDED_FCNTL
61 #include <fcntl.h>
62 #endif
63 #include <ctype.h>
64 #include <errno.h>
65 #include <setjmp.h>
66 #include <sys/stat.h>
67 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68 /* #include <sys/param.h> */
70 #include "charset.h"
71 #include "coding.h"
72 #include "ccl.h"
73 #include "frame.h"
74 #include "dispextern.h"
75 #include "fontset.h"
76 #include "termhooks.h"
77 #include "termopts.h"
78 #include "termchar.h"
79 #include "gnu.h"
80 #include "disptab.h"
81 #include "buffer.h"
82 #include "window.h"
83 #include "keyboard.h"
84 #include "intervals.h"
85 #include "process.h"
86 #include "atimer.h"
87 #include "keymap.h"
89 #ifdef USE_X_TOOLKIT
90 #include <X11/Shell.h>
91 #endif
93 #ifdef HAVE_SYS_TIME_H
94 #include <sys/time.h>
95 #endif
96 #ifdef HAVE_UNISTD_H
97 #include <unistd.h>
98 #endif
100 #ifdef USE_GTK
101 #include "gtkutil.h"
102 #endif
104 #ifdef USE_LUCID
105 extern int xlwmenu_window_p P_ ((Widget w, Window window));
106 extern void xlwmenu_redisplay P_ ((Widget));
107 #endif
109 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
111 extern void free_frame_menubar P_ ((struct frame *));
112 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
113 int));
114 #endif
116 #ifdef USE_X_TOOLKIT
117 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
118 #define HACK_EDITRES
119 extern void _XEditResCheckMessages ();
120 #endif /* not NO_EDITRES */
122 /* Include toolkit specific headers for the scroll bar widget. */
124 #ifdef USE_TOOLKIT_SCROLL_BARS
125 #if defined USE_MOTIF
126 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
127 #include <Xm/ScrollBar.h>
128 #else /* !USE_MOTIF i.e. use Xaw */
130 #ifdef HAVE_XAW3D
131 #include <X11/Xaw3d/Simple.h>
132 #include <X11/Xaw3d/Scrollbar.h>
133 #include <X11/Xaw3d/ThreeD.h>
134 #else /* !HAVE_XAW3D */
135 #include <X11/Xaw/Simple.h>
136 #include <X11/Xaw/Scrollbar.h>
137 #endif /* !HAVE_XAW3D */
138 #ifndef XtNpickTop
139 #define XtNpickTop "pickTop"
140 #endif /* !XtNpickTop */
141 #endif /* !USE_MOTIF */
142 #endif /* USE_TOOLKIT_SCROLL_BARS */
144 #endif /* USE_X_TOOLKIT */
146 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
147 #define x_any_window_to_frame x_window_to_frame
148 #define x_top_window_to_frame x_window_to_frame
149 #endif
151 #ifdef USE_X_TOOLKIT
152 #include "widget.h"
153 #ifndef XtNinitialState
154 #define XtNinitialState "initialState"
155 #endif
156 #endif
158 /* Default to using XIM if available. */
159 #ifdef USE_XIM
160 int use_xim = 1;
161 #else
162 int use_xim = 0; /* configure --without-xim */
163 #endif
167 /* Non-nil means Emacs uses toolkit scroll bars. */
169 Lisp_Object Vx_toolkit_scroll_bars;
171 /* Non-zero means that a HELP_EVENT has been generated since Emacs
172 start. */
174 static int any_help_event_p;
176 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
177 static Lisp_Object last_window;
179 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
181 int x_use_underline_position_properties;
183 /* Non-zero means to draw the underline at the same place as the descent line. */
185 int x_underline_at_descent_line;
187 /* This is a chain of structures for all the X displays currently in
188 use. */
190 struct x_display_info *x_display_list;
192 /* This is a list of cons cells, each of the form (NAME
193 . FONT-LIST-CACHE), one for each element of x_display_list and in
194 the same order. NAME is the name of the frame. FONT-LIST-CACHE
195 records previous values returned by x-list-fonts. */
197 Lisp_Object x_display_name_list;
199 /* Frame being updated by update_frame. This is declared in term.c.
200 This is set by update_begin and looked at by all the XT functions.
201 It is zero while not inside an update. In that case, the XT
202 functions assume that `selected_frame' is the frame to apply to. */
204 extern struct frame *updating_frame;
206 /* This is a frame waiting to be auto-raised, within XTread_socket. */
208 struct frame *pending_autoraise_frame;
210 #ifdef USE_X_TOOLKIT
211 /* The application context for Xt use. */
212 XtAppContext Xt_app_con;
213 static String Xt_default_resources[] = {0};
214 #endif /* USE_X_TOOLKIT */
216 /* Non-zero means user is interacting with a toolkit scroll bar. */
218 static int toolkit_scroll_bar_interaction;
220 /* Non-zero means to not move point as a result of clicking on a
221 frame to focus it (when focus-follows-mouse is nil). */
223 int x_mouse_click_focus_ignore_position;
225 /* Non-zero timeout value means ignore next mouse click if it arrives
226 before that timeout elapses (i.e. as part of the same sequence of
227 events resulting from clicking on a frame to select it). */
229 static unsigned long ignore_next_mouse_click_timeout;
231 /* Mouse movement.
233 Formerly, we used PointerMotionHintMask (in standard_event_mask)
234 so that we would have to call XQueryPointer after each MotionNotify
235 event to ask for another such event. However, this made mouse tracking
236 slow, and there was a bug that made it eventually stop.
238 Simply asking for MotionNotify all the time seems to work better.
240 In order to avoid asking for motion events and then throwing most
241 of them away or busy-polling the server for mouse positions, we ask
242 the server for pointer motion hints. This means that we get only
243 one event per group of mouse movements. "Groups" are delimited by
244 other kinds of events (focus changes and button clicks, for
245 example), or by XQueryPointer calls; when one of these happens, we
246 get another MotionNotify event the next time the mouse moves. This
247 is at least as efficient as getting motion events when mouse
248 tracking is on, and I suspect only negligibly worse when tracking
249 is off. */
251 /* Where the mouse was last time we reported a mouse event. */
253 static XRectangle last_mouse_glyph;
254 static FRAME_PTR last_mouse_glyph_frame;
255 static Lisp_Object last_mouse_press_frame;
257 /* The scroll bar in which the last X motion event occurred.
259 If the last X motion event occurred in a scroll bar, we set this so
260 XTmouse_position can know whether to report a scroll bar motion or
261 an ordinary motion.
263 If the last X motion event didn't occur in a scroll bar, we set
264 this to Qnil, to tell XTmouse_position to return an ordinary motion
265 event. */
267 static Lisp_Object last_mouse_scroll_bar;
269 /* This is a hack. We would really prefer that XTmouse_position would
270 return the time associated with the position it returns, but there
271 doesn't seem to be any way to wrest the time-stamp from the server
272 along with the position query. So, we just keep track of the time
273 of the last movement we received, and return that in hopes that
274 it's somewhat accurate. */
276 static Time last_mouse_movement_time;
278 /* Time for last user interaction as returned in X events. */
280 static Time last_user_time;
282 /* Incremented by XTread_socket whenever it really tries to read
283 events. */
285 #ifdef __STDC__
286 static int volatile input_signal_count;
287 #else
288 static int input_signal_count;
289 #endif
291 /* Used locally within XTread_socket. */
293 static int x_noop_count;
295 /* Initial values of argv and argc. */
297 extern char **initial_argv;
298 extern int initial_argc;
300 extern Lisp_Object Vcommand_line_args, Vsystem_name;
302 /* Tells if a window manager is present or not. */
304 extern Lisp_Object Vx_no_window_manager;
306 extern Lisp_Object Qeql;
308 extern int errno;
310 /* A mask of extra modifier bits to put into every keyboard char. */
312 extern EMACS_INT extra_keyboard_modifiers;
314 /* The keysyms to use for the various modifiers. */
316 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
317 Lisp_Object Vx_keysym_table;
318 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
320 static Lisp_Object Qvendor_specific_keysyms;
321 static Lisp_Object Qlatin_1;
323 /* Used in x_flush. */
325 extern Lisp_Object Vinhibit_redisplay;
327 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
328 extern int x_bitmap_mask P_ ((FRAME_PTR, int));
330 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
331 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
332 static const XColor *x_color_cells P_ ((Display *, int *));
333 static void x_update_window_end P_ ((struct window *, int, int));
335 static int x_io_error_quitter P_ ((Display *));
336 static struct terminal *x_create_terminal P_ ((struct x_display_info *));
337 void x_delete_terminal P_ ((struct terminal *));
338 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
339 static int x_compute_min_glyph_bounds P_ ((struct frame *));
340 static void x_update_end P_ ((struct frame *));
341 static void XTframe_up_to_date P_ ((struct frame *));
342 static void XTset_terminal_modes P_ ((struct terminal *));
343 static void XTreset_terminal_modes P_ ((struct terminal *));
344 static void x_clear_frame P_ ((struct frame *));
345 static void frame_highlight P_ ((struct frame *));
346 static void frame_unhighlight P_ ((struct frame *));
347 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
348 static void x_focus_changed P_ ((int, int, struct x_display_info *,
349 struct frame *, struct input_event *));
350 static void x_detect_focus_change P_ ((struct x_display_info *,
351 XEvent *, struct input_event *));
352 static void XTframe_rehighlight P_ ((struct frame *));
353 static void x_frame_rehighlight P_ ((struct x_display_info *));
354 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
355 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
356 enum text_cursor_kinds));
358 static void x_clip_to_row P_ ((struct window *, struct glyph_row *, int, GC));
359 static void x_flush P_ ((struct frame *f));
360 static void x_update_begin P_ ((struct frame *));
361 static void x_update_window_begin P_ ((struct window *));
362 static void x_after_update_window_line P_ ((struct glyph_row *));
363 static struct scroll_bar *x_window_to_scroll_bar P_ ((Display *, Window));
364 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
365 enum scroll_bar_part *,
366 Lisp_Object *, Lisp_Object *,
367 unsigned long *));
368 static void x_check_fullscreen P_ ((struct frame *));
369 static void x_check_expected_move P_ ((struct frame *, int, int));
370 static void x_sync_with_move P_ ((struct frame *, int, int, int));
371 static int handle_one_xevent P_ ((struct x_display_info *, XEvent *,
372 int *, struct input_event *));
373 /* Don't declare this NO_RETURN because we want no
374 interference with debugging failing X calls. */
375 static SIGTYPE x_connection_closed P_ ((Display *, char *));
378 /* Flush display of frame F, or of all frames if F is null. */
380 static void
381 x_flush (f)
382 struct frame *f;
384 /* Don't call XFlush when it is not safe to redisplay; the X
385 connection may be broken. */
386 if (!NILP (Vinhibit_redisplay))
387 return;
389 BLOCK_INPUT;
390 if (f == NULL)
392 Lisp_Object rest, frame;
393 FOR_EACH_FRAME (rest, frame)
394 if (FRAME_X_P (XFRAME (frame)))
395 x_flush (XFRAME (frame));
397 else if (FRAME_X_P (f))
398 XFlush (FRAME_X_DISPLAY (f));
399 UNBLOCK_INPUT;
403 /* Remove calls to XFlush by defining XFlush to an empty replacement.
404 Calls to XFlush should be unnecessary because the X output buffer
405 is flushed automatically as needed by calls to XPending,
406 XNextEvent, or XWindowEvent according to the XFlush man page.
407 XTread_socket calls XPending. Removing XFlush improves
408 performance. */
410 #define XFlush(DISPLAY) (void) 0
413 /***********************************************************************
414 Debugging
415 ***********************************************************************/
417 #if 0
419 /* This is a function useful for recording debugging information about
420 the sequence of occurrences in this file. */
422 struct record
424 char *locus;
425 int type;
428 struct record event_record[100];
430 int event_record_index;
432 record_event (locus, type)
433 char *locus;
434 int type;
436 if (event_record_index == sizeof (event_record) / sizeof (struct record))
437 event_record_index = 0;
439 event_record[event_record_index].locus = locus;
440 event_record[event_record_index].type = type;
441 event_record_index++;
444 #endif /* 0 */
448 /* Return the struct x_display_info corresponding to DPY. */
450 struct x_display_info *
451 x_display_info_for_display (dpy)
452 Display *dpy;
454 struct x_display_info *dpyinfo;
456 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
457 if (dpyinfo->display == dpy)
458 return dpyinfo;
460 return 0;
465 /***********************************************************************
466 Starting and ending an update
467 ***********************************************************************/
469 /* Start an update of frame F. This function is installed as a hook
470 for update_begin, i.e. it is called when update_begin is called.
471 This function is called prior to calls to x_update_window_begin for
472 each window being updated. Currently, there is nothing to do here
473 because all interesting stuff is done on a window basis. */
475 static void
476 x_update_begin (f)
477 struct frame *f;
479 /* Nothing to do. */
483 /* Start update of window W. Set the global variable updated_window
484 to the window being updated and set output_cursor to the cursor
485 position of W. */
487 static void
488 x_update_window_begin (w)
489 struct window *w;
491 struct frame *f = XFRAME (WINDOW_FRAME (w));
492 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
494 updated_window = w;
495 set_output_cursor (&w->cursor);
497 BLOCK_INPUT;
499 if (f == display_info->mouse_face_mouse_frame)
501 /* Don't do highlighting for mouse motion during the update. */
502 display_info->mouse_face_defer = 1;
504 /* If F needs to be redrawn, simply forget about any prior mouse
505 highlighting. */
506 if (FRAME_GARBAGED_P (f))
507 display_info->mouse_face_window = Qnil;
509 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
510 their mouse_face_p flag set, which means that they are always
511 unequal to rows in a desired matrix which never have that
512 flag set. So, rows containing mouse-face glyphs are never
513 scrolled, and we don't have to switch the mouse highlight off
514 here to prevent it from being scrolled. */
516 /* Can we tell that this update does not affect the window
517 where the mouse highlight is? If so, no need to turn off.
518 Likewise, don't do anything if the frame is garbaged;
519 in that case, the frame's current matrix that we would use
520 is all wrong, and we will redisplay that line anyway. */
521 if (!NILP (display_info->mouse_face_window)
522 && w == XWINDOW (display_info->mouse_face_window))
524 int i;
526 for (i = 0; i < w->desired_matrix->nrows; ++i)
527 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
528 break;
530 if (i < w->desired_matrix->nrows)
531 clear_mouse_face (display_info);
533 #endif /* 0 */
536 UNBLOCK_INPUT;
540 /* Draw a vertical window border from (x,y0) to (x,y1) */
542 static void
543 x_draw_vertical_window_border (w, x, y0, y1)
544 struct window *w;
545 int x, y0, y1;
547 struct frame *f = XFRAME (WINDOW_FRAME (w));
548 struct face *face;
550 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
551 if (face)
552 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
553 face->foreground);
555 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
556 f->output_data.x->normal_gc, x, y0, x, y1);
559 /* End update of window W (which is equal to updated_window).
561 Draw vertical borders between horizontally adjacent windows, and
562 display W's cursor if CURSOR_ON_P is non-zero.
564 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
565 glyphs in mouse-face were overwritten. In that case we have to
566 make sure that the mouse-highlight is properly redrawn.
568 W may be a menu bar pseudo-window in case we don't have X toolkit
569 support. Such windows don't have a cursor, so don't display it
570 here. */
572 static void
573 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
574 struct window *w;
575 int cursor_on_p, mouse_face_overwritten_p;
577 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
579 if (!w->pseudo_window_p)
581 BLOCK_INPUT;
583 if (cursor_on_p)
584 display_and_set_cursor (w, 1, output_cursor.hpos,
585 output_cursor.vpos,
586 output_cursor.x, output_cursor.y);
588 if (draw_window_fringes (w, 1))
589 x_draw_vertical_border (w);
591 UNBLOCK_INPUT;
594 /* If a row with mouse-face was overwritten, arrange for
595 XTframe_up_to_date to redisplay the mouse highlight. */
596 if (mouse_face_overwritten_p)
598 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
599 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
600 dpyinfo->mouse_face_window = Qnil;
603 updated_window = NULL;
607 /* End update of frame F. This function is installed as a hook in
608 update_end. */
610 static void
611 x_update_end (f)
612 struct frame *f;
614 /* Mouse highlight may be displayed again. */
615 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
617 #ifndef XFlush
618 BLOCK_INPUT;
619 XFlush (FRAME_X_DISPLAY (f));
620 UNBLOCK_INPUT;
621 #endif
625 /* This function is called from various places in xdisp.c whenever a
626 complete update has been performed. The global variable
627 updated_window is not available here. */
629 static void
630 XTframe_up_to_date (f)
631 struct frame *f;
633 if (FRAME_X_P (f))
635 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
637 if (dpyinfo->mouse_face_deferred_gc
638 || f == dpyinfo->mouse_face_mouse_frame)
640 BLOCK_INPUT;
641 if (dpyinfo->mouse_face_mouse_frame)
642 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
643 dpyinfo->mouse_face_mouse_x,
644 dpyinfo->mouse_face_mouse_y);
645 dpyinfo->mouse_face_deferred_gc = 0;
646 UNBLOCK_INPUT;
652 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
653 arrow bitmaps, or clear the fringes if no bitmaps are required
654 before DESIRED_ROW is made current. The window being updated is
655 found in updated_window. This function It is called from
656 update_window_line only if it is known that there are differences
657 between bitmaps to be drawn between current row and DESIRED_ROW. */
659 static void
660 x_after_update_window_line (desired_row)
661 struct glyph_row *desired_row;
663 struct window *w = updated_window;
664 struct frame *f;
665 int width, height;
667 xassert (w);
669 if (!desired_row->mode_line_p && !w->pseudo_window_p)
670 desired_row->redraw_fringe_bitmaps_p = 1;
672 /* When a window has disappeared, make sure that no rest of
673 full-width rows stays visible in the internal border. Could
674 check here if updated_window is the leftmost/rightmost window,
675 but I guess it's not worth doing since vertically split windows
676 are almost never used, internal border is rarely set, and the
677 overhead is very small. */
678 if (windows_or_buffers_changed
679 && desired_row->full_width_p
680 && (f = XFRAME (w->frame),
681 width = FRAME_INTERNAL_BORDER_WIDTH (f),
682 width != 0)
683 && (height = desired_row->visible_height,
684 height > 0))
686 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
688 /* Internal border is drawn below the tool bar. */
689 if (WINDOWP (f->tool_bar_window)
690 && w == XWINDOW (f->tool_bar_window))
691 y -= width;
693 BLOCK_INPUT;
694 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
695 0, y, width, height, False);
696 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
697 FRAME_PIXEL_WIDTH (f) - width,
698 y, width, height, False);
699 UNBLOCK_INPUT;
703 static void
704 x_draw_fringe_bitmap (w, row, p)
705 struct window *w;
706 struct glyph_row *row;
707 struct draw_fringe_bitmap_params *p;
709 struct frame *f = XFRAME (WINDOW_FRAME (w));
710 Display *display = FRAME_X_DISPLAY (f);
711 Window window = FRAME_X_WINDOW (f);
712 GC gc = f->output_data.x->normal_gc;
713 struct face *face = p->face;
714 int rowY;
716 /* Must clip because of partially visible lines. */
717 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
718 if (p->y < rowY)
720 /* Adjust position of "bottom aligned" bitmap on partially
721 visible last row. */
722 int oldY = row->y;
723 int oldVH = row->visible_height;
724 row->visible_height = p->h;
725 row->y -= rowY - p->y;
726 x_clip_to_row (w, row, -1, gc);
727 row->y = oldY;
728 row->visible_height = oldVH;
730 else
731 x_clip_to_row (w, row, -1, gc);
733 if (!p->overlay_p)
735 int bx = p->bx, by = p->by, nx = p->nx, ny = p->ny;
737 /* In case the same realized face is used for fringes and
738 for something displayed in the text (e.g. face `region' on
739 mono-displays, the fill style may have been changed to
740 FillSolid in x_draw_glyph_string_background. */
741 if (face->stipple)
742 XSetFillStyle (display, face->gc, FillOpaqueStippled);
743 else
744 XSetForeground (display, face->gc, face->background);
746 #ifdef USE_TOOLKIT_SCROLL_BARS
747 /* If the fringe is adjacent to the left (right) scroll bar of a
748 leftmost (rightmost, respectively) window, then extend its
749 background to the gap between the fringe and the bar. */
750 if ((WINDOW_LEFTMOST_P (w)
751 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
752 || (WINDOW_RIGHTMOST_P (w)
753 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
755 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
757 if (sb_width > 0)
759 int left = WINDOW_SCROLL_BAR_AREA_X (w);
760 int width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
761 * FRAME_COLUMN_WIDTH (f));
763 if (bx < 0)
765 /* Bitmap fills the fringe. */
766 if (left + width == p->x)
767 bx = left + sb_width;
768 else if (p->x + p->wd == left)
769 bx = left;
770 if (bx >= 0)
772 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
774 nx = width - sb_width;
775 by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
776 row->y));
777 ny = row->visible_height;
780 else
782 if (left + width == bx)
784 bx = left + sb_width;
785 nx += width - sb_width;
787 else if (bx + nx == left)
788 nx += width - sb_width;
792 #endif
793 if (bx >= 0 && nx > 0)
794 XFillRectangle (display, window, face->gc, bx, by, nx, ny);
796 if (!face->stipple)
797 XSetForeground (display, face->gc, face->foreground);
800 if (p->which)
802 unsigned char *bits;
803 Pixmap pixmap, clipmask = (Pixmap) 0;
804 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
805 XGCValues gcv;
807 if (p->wd > 8)
808 bits = (unsigned char *)(p->bits + p->dh);
809 else
810 bits = (unsigned char *)p->bits + p->dh;
812 /* Draw the bitmap. I believe these small pixmaps can be cached
813 by the server. */
814 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
815 (p->cursor_p
816 ? (p->overlay_p ? face->background
817 : f->output_data.x->cursor_pixel)
818 : face->foreground),
819 face->background, depth);
821 if (p->overlay_p)
823 clipmask = XCreatePixmapFromBitmapData (display,
824 FRAME_X_DISPLAY_INFO (f)->root_window,
825 bits, p->wd, p->h,
826 1, 0, 1);
827 gcv.clip_mask = clipmask;
828 gcv.clip_x_origin = p->x;
829 gcv.clip_y_origin = p->y;
830 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
833 XCopyArea (display, pixmap, window, gc, 0, 0,
834 p->wd, p->h, p->x, p->y);
835 XFreePixmap (display, pixmap);
837 if (p->overlay_p)
839 gcv.clip_mask = (Pixmap) 0;
840 XChangeGC (display, gc, GCClipMask, &gcv);
841 XFreePixmap (display, clipmask);
845 XSetClipMask (display, gc, None);
850 /* This is called when starting Emacs and when restarting after
851 suspend. When starting Emacs, no X window is mapped. And nothing
852 must be done to Emacs's own window if it is suspended (though that
853 rarely happens). */
855 static void
856 XTset_terminal_modes (struct terminal *terminal)
860 /* This is called when exiting or suspending Emacs. Exiting will make
861 the X-windows go away, and suspending requires no action. */
863 static void
864 XTreset_terminal_modes (struct terminal *terminal)
870 /***********************************************************************
871 Display Iterator
872 ***********************************************************************/
874 /* Function prototypes of this page. */
876 static int x_encode_char P_ ((int, XChar2b *, struct font_info *, int *));
879 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
880 is not contained in the font. */
882 static XCharStruct *
883 x_per_char_metric (font, char2b, font_type)
884 XFontStruct *font;
885 XChar2b *char2b;
886 int font_type; /* unused on X */
888 /* The result metric information. */
889 XCharStruct *pcm = NULL;
891 xassert (font && char2b);
893 if (font->per_char != NULL)
895 if (font->min_byte1 == 0 && font->max_byte1 == 0)
897 /* min_char_or_byte2 specifies the linear character index
898 corresponding to the first element of the per_char array,
899 max_char_or_byte2 is the index of the last character. A
900 character with non-zero CHAR2B->byte1 is not in the font.
901 A character with byte2 less than min_char_or_byte2 or
902 greater max_char_or_byte2 is not in the font. */
903 if (char2b->byte1 == 0
904 && char2b->byte2 >= font->min_char_or_byte2
905 && char2b->byte2 <= font->max_char_or_byte2)
906 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
908 else
910 /* If either min_byte1 or max_byte1 are nonzero, both
911 min_char_or_byte2 and max_char_or_byte2 are less than
912 256, and the 2-byte character index values corresponding
913 to the per_char array element N (counting from 0) are:
915 byte1 = N/D + min_byte1
916 byte2 = N\D + min_char_or_byte2
918 where:
920 D = max_char_or_byte2 - min_char_or_byte2 + 1
921 / = integer division
922 \ = integer modulus */
923 if (char2b->byte1 >= font->min_byte1
924 && char2b->byte1 <= font->max_byte1
925 && char2b->byte2 >= font->min_char_or_byte2
926 && char2b->byte2 <= font->max_char_or_byte2)
928 pcm = (font->per_char
929 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
930 * (char2b->byte1 - font->min_byte1))
931 + (char2b->byte2 - font->min_char_or_byte2));
935 else
937 /* If the per_char pointer is null, all glyphs between the first
938 and last character indexes inclusive have the same
939 information, as given by both min_bounds and max_bounds. */
940 if (char2b->byte2 >= font->min_char_or_byte2
941 && char2b->byte2 <= font->max_char_or_byte2)
942 pcm = &font->max_bounds;
945 return ((pcm == NULL
946 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
947 ? NULL : pcm);
951 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
952 the two-byte form of C. Encoding is returned in *CHAR2B. */
954 static int
955 x_encode_char (c, char2b, font_info, two_byte_p)
956 int c;
957 XChar2b *char2b;
958 struct font_info *font_info;
959 int *two_byte_p;
961 int charset = CHAR_CHARSET (c);
962 XFontStruct *font = font_info->font;
964 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
965 This may be either a program in a special encoder language or a
966 fixed encoding. */
967 if (font_info->font_encoder)
969 /* It's a program. */
970 struct ccl_program *ccl = font_info->font_encoder;
972 check_ccl_update (ccl);
973 if (CHARSET_DIMENSION (charset) == 1)
975 ccl->reg[0] = charset;
976 ccl->reg[1] = char2b->byte2;
977 ccl->reg[2] = -1;
979 else
981 ccl->reg[0] = charset;
982 ccl->reg[1] = char2b->byte1;
983 ccl->reg[2] = char2b->byte2;
986 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
988 /* We assume that MSBs are appropriately set/reset by CCL
989 program. */
990 if (font->max_byte1 == 0) /* 1-byte font */
991 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
992 else
993 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
995 else if (font_info->encoding[charset])
997 /* Fixed encoding scheme. See fontset.h for the meaning of the
998 encoding numbers. */
999 int enc = font_info->encoding[charset];
1001 if ((enc == 1 || enc == 2)
1002 && CHARSET_DIMENSION (charset) == 2)
1003 char2b->byte1 |= 0x80;
1005 if (enc == 1 || enc == 3)
1006 char2b->byte2 |= 0x80;
1009 if (two_byte_p)
1010 *two_byte_p = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1012 return FONT_TYPE_UNKNOWN;
1017 /***********************************************************************
1018 Glyph display
1019 ***********************************************************************/
1023 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
1024 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
1025 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
1026 int));
1027 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
1028 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
1029 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
1030 static void x_draw_glyph_string P_ ((struct glyph_string *));
1031 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
1032 static void x_set_cursor_gc P_ ((struct glyph_string *));
1033 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
1034 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
1035 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
1036 unsigned long *, double, int));
1037 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
1038 double, int, unsigned long));
1039 static void x_setup_relief_colors P_ ((struct glyph_string *));
1040 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
1041 static void x_draw_image_relief P_ ((struct glyph_string *));
1042 static void x_draw_image_foreground P_ ((struct glyph_string *));
1043 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
1044 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
1045 int, int, int));
1046 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
1047 int, int, int, int, int, int,
1048 XRectangle *));
1049 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
1050 int, int, int, XRectangle *));
1052 #if GLYPH_DEBUG
1053 static void x_check_font P_ ((struct frame *, XFontStruct *));
1054 #endif
1057 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
1058 face. */
1060 static void
1061 x_set_cursor_gc (s)
1062 struct glyph_string *s;
1064 if (s->font == FRAME_FONT (s->f)
1065 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
1066 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
1067 && !s->cmp)
1068 s->gc = s->f->output_data.x->cursor_gc;
1069 else
1071 /* Cursor on non-default face: must merge. */
1072 XGCValues xgcv;
1073 unsigned long mask;
1075 xgcv.background = s->f->output_data.x->cursor_pixel;
1076 xgcv.foreground = s->face->background;
1078 /* If the glyph would be invisible, try a different foreground. */
1079 if (xgcv.foreground == xgcv.background)
1080 xgcv.foreground = s->face->foreground;
1081 if (xgcv.foreground == xgcv.background)
1082 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1083 if (xgcv.foreground == xgcv.background)
1084 xgcv.foreground = s->face->foreground;
1086 /* Make sure the cursor is distinct from text in this face. */
1087 if (xgcv.background == s->face->background
1088 && xgcv.foreground == s->face->foreground)
1090 xgcv.background = s->face->foreground;
1091 xgcv.foreground = s->face->background;
1094 IF_DEBUG (x_check_font (s->f, s->font));
1095 xgcv.font = s->font->fid;
1096 xgcv.graphics_exposures = False;
1097 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
1099 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1100 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1101 mask, &xgcv);
1102 else
1103 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1104 = XCreateGC (s->display, s->window, mask, &xgcv);
1106 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1111 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1113 static void
1114 x_set_mouse_face_gc (s)
1115 struct glyph_string *s;
1117 int face_id;
1118 struct face *face;
1120 /* What face has to be used last for the mouse face? */
1121 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
1122 face = FACE_FROM_ID (s->f, face_id);
1123 if (face == NULL)
1124 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1126 if (s->first_glyph->type == CHAR_GLYPH)
1127 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
1128 else
1129 face_id = FACE_FOR_CHAR (s->f, face, 0);
1130 s->face = FACE_FROM_ID (s->f, face_id);
1131 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1133 /* If font in this face is same as S->font, use it. */
1134 if (s->font == s->face->font)
1135 s->gc = s->face->gc;
1136 else
1138 /* Otherwise construct scratch_cursor_gc with values from FACE
1139 but font FONT. */
1140 XGCValues xgcv;
1141 unsigned long mask;
1143 xgcv.background = s->face->background;
1144 xgcv.foreground = s->face->foreground;
1145 IF_DEBUG (x_check_font (s->f, s->font));
1146 xgcv.font = s->font->fid;
1147 xgcv.graphics_exposures = False;
1148 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
1150 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1151 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1152 mask, &xgcv);
1153 else
1154 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1155 = XCreateGC (s->display, s->window, mask, &xgcv);
1157 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1160 xassert (s->gc != 0);
1164 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1165 Faces to use in the mode line have already been computed when the
1166 matrix was built, so there isn't much to do, here. */
1168 static INLINE void
1169 x_set_mode_line_face_gc (s)
1170 struct glyph_string *s;
1172 s->gc = s->face->gc;
1176 /* Set S->gc of glyph string S for drawing that glyph string. Set
1177 S->stippled_p to a non-zero value if the face of S has a stipple
1178 pattern. */
1180 static INLINE void
1181 x_set_glyph_string_gc (s)
1182 struct glyph_string *s;
1184 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1186 if (s->hl == DRAW_NORMAL_TEXT)
1188 s->gc = s->face->gc;
1189 s->stippled_p = s->face->stipple != 0;
1191 else if (s->hl == DRAW_INVERSE_VIDEO)
1193 x_set_mode_line_face_gc (s);
1194 s->stippled_p = s->face->stipple != 0;
1196 else if (s->hl == DRAW_CURSOR)
1198 x_set_cursor_gc (s);
1199 s->stippled_p = 0;
1201 else if (s->hl == DRAW_MOUSE_FACE)
1203 x_set_mouse_face_gc (s);
1204 s->stippled_p = s->face->stipple != 0;
1206 else if (s->hl == DRAW_IMAGE_RAISED
1207 || s->hl == DRAW_IMAGE_SUNKEN)
1209 s->gc = s->face->gc;
1210 s->stippled_p = s->face->stipple != 0;
1212 else
1214 s->gc = s->face->gc;
1215 s->stippled_p = s->face->stipple != 0;
1218 /* GC must have been set. */
1219 xassert (s->gc != 0);
1223 /* Set clipping for output of glyph string S. S may be part of a mode
1224 line or menu if we don't have X toolkit support. */
1226 static INLINE void
1227 x_set_glyph_string_clipping (s)
1228 struct glyph_string *s;
1230 XRectangle r;
1231 get_glyph_string_clip_rect (s, &r);
1232 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
1236 /* RIF:
1237 Compute left and right overhang of glyph string S. If S is a glyph
1238 string for a composition, assume overhangs don't exist. */
1240 static void
1241 x_compute_glyph_string_overhangs (s)
1242 struct glyph_string *s;
1244 if (s->cmp == NULL
1245 && s->first_glyph->type == CHAR_GLYPH)
1247 XCharStruct cs;
1248 int direction, font_ascent, font_descent;
1249 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
1250 &font_ascent, &font_descent, &cs);
1251 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
1252 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
1257 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1259 static INLINE void
1260 x_clear_glyph_string_rect (s, x, y, w, h)
1261 struct glyph_string *s;
1262 int x, y, w, h;
1264 XGCValues xgcv;
1265 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1266 XSetForeground (s->display, s->gc, xgcv.background);
1267 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1268 XSetForeground (s->display, s->gc, xgcv.foreground);
1272 /* Draw the background of glyph_string S. If S->background_filled_p
1273 is non-zero don't draw it. FORCE_P non-zero means draw the
1274 background even if it wouldn't be drawn normally. This is used
1275 when a string preceding S draws into the background of S, or S
1276 contains the first component of a composition. */
1278 static void
1279 x_draw_glyph_string_background (s, force_p)
1280 struct glyph_string *s;
1281 int force_p;
1283 /* Nothing to do if background has already been drawn or if it
1284 shouldn't be drawn in the first place. */
1285 if (!s->background_filled_p)
1287 int box_line_width = max (s->face->box_line_width, 0);
1289 if (s->stippled_p)
1291 /* Fill background with a stipple pattern. */
1292 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1293 XFillRectangle (s->display, s->window, s->gc, s->x,
1294 s->y + box_line_width,
1295 s->background_width,
1296 s->height - 2 * box_line_width);
1297 XSetFillStyle (s->display, s->gc, FillSolid);
1298 s->background_filled_p = 1;
1300 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1301 || s->font_not_found_p
1302 || s->extends_to_end_of_line_p
1303 || force_p)
1305 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1306 s->background_width,
1307 s->height - 2 * box_line_width);
1308 s->background_filled_p = 1;
1314 /* Draw the foreground of glyph string S. */
1316 static void
1317 x_draw_glyph_string_foreground (s)
1318 struct glyph_string *s;
1320 int i, x;
1322 /* If first glyph of S has a left box line, start drawing the text
1323 of S to the right of that box line. */
1324 if (s->face->box != FACE_NO_BOX
1325 && s->first_glyph->left_box_line_p)
1326 x = s->x + eabs (s->face->box_line_width);
1327 else
1328 x = s->x;
1330 /* Draw characters of S as rectangles if S's font could not be
1331 loaded. */
1332 if (s->font_not_found_p)
1334 for (i = 0; i < s->nchars; ++i)
1336 struct glyph *g = s->first_glyph + i;
1337 XDrawRectangle (s->display, s->window,
1338 s->gc, x, s->y, g->pixel_width - 1,
1339 s->height - 1);
1340 x += g->pixel_width;
1343 else
1345 char *char1b = (char *) s->char2b;
1346 int boff = s->font_info->baseline_offset;
1348 if (s->font_info->vertical_centering)
1349 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
1351 /* If we can use 8-bit functions, condense S->char2b. */
1352 if (!s->two_byte_p)
1353 for (i = 0; i < s->nchars; ++i)
1354 char1b[i] = s->char2b[i].byte2;
1356 /* Draw text with XDrawString if background has already been
1357 filled. Otherwise, use XDrawImageString. (Note that
1358 XDrawImageString is usually faster than XDrawString.) Always
1359 use XDrawImageString when drawing the cursor so that there is
1360 no chance that characters under a box cursor are invisible. */
1361 if (s->for_overlaps
1362 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1364 /* Draw characters with 16-bit or 8-bit functions. */
1365 if (s->two_byte_p)
1366 XDrawString16 (s->display, s->window, s->gc, x,
1367 s->ybase - boff, s->char2b, s->nchars);
1368 else
1369 XDrawString (s->display, s->window, s->gc, x,
1370 s->ybase - boff, char1b, s->nchars);
1372 else
1374 if (s->two_byte_p)
1375 XDrawImageString16 (s->display, s->window, s->gc, x,
1376 s->ybase - boff, s->char2b, s->nchars);
1377 else
1378 XDrawImageString (s->display, s->window, s->gc, x,
1379 s->ybase - boff, char1b, s->nchars);
1382 if (s->face->overstrike)
1384 /* For overstriking (to simulate bold-face), draw the
1385 characters again shifted to the right by one pixel. */
1386 if (s->two_byte_p)
1387 XDrawString16 (s->display, s->window, s->gc, x + 1,
1388 s->ybase - boff, s->char2b, s->nchars);
1389 else
1390 XDrawString (s->display, s->window, s->gc, x + 1,
1391 s->ybase - boff, char1b, s->nchars);
1396 /* Draw the foreground of composite glyph string S. */
1398 static void
1399 x_draw_composite_glyph_string_foreground (s)
1400 struct glyph_string *s;
1402 int i, x;
1404 /* If first glyph of S has a left box line, start drawing the text
1405 of S to the right of that box line. */
1406 if (s->face->box != FACE_NO_BOX
1407 && s->first_glyph->left_box_line_p)
1408 x = s->x + eabs (s->face->box_line_width);
1409 else
1410 x = s->x;
1412 /* S is a glyph string for a composition. S->gidx is the index of
1413 the first character drawn for glyphs of this composition.
1414 S->gidx == 0 means we are drawing the very first character of
1415 this composition. */
1417 /* Draw a rectangle for the composition if the font for the very
1418 first character of the composition could not be loaded. */
1419 if (s->font_not_found_p)
1421 if (s->gidx == 0)
1422 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1423 s->width - 1, s->height - 1);
1425 else
1427 for (i = 0; i < s->nchars; i++, ++s->gidx)
1429 XDrawString16 (s->display, s->window, s->gc,
1430 x + s->cmp->offsets[s->gidx * 2],
1431 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1432 s->char2b + i, 1);
1433 if (s->face->overstrike)
1434 XDrawString16 (s->display, s->window, s->gc,
1435 x + s->cmp->offsets[s->gidx * 2] + 1,
1436 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1437 s->char2b + i, 1);
1443 #ifdef USE_X_TOOLKIT
1445 static struct frame *x_frame_of_widget P_ ((Widget));
1446 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1447 XrmValue *, XrmValue *, XtPointer *));
1448 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1449 XrmValue *, Cardinal *));
1452 /* Return the frame on which widget WIDGET is used.. Abort if frame
1453 cannot be determined. */
1455 static struct frame *
1456 x_frame_of_widget (widget)
1457 Widget widget;
1459 struct x_display_info *dpyinfo;
1460 Lisp_Object tail;
1461 struct frame *f;
1463 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1465 /* Find the top-level shell of the widget. Note that this function
1466 can be called when the widget is not yet realized, so XtWindow
1467 (widget) == 0. That's the reason we can't simply use
1468 x_any_window_to_frame. */
1469 while (!XtIsTopLevelShell (widget))
1470 widget = XtParent (widget);
1472 /* Look for a frame with that top-level widget. Allocate the color
1473 on that frame to get the right gamma correction value. */
1474 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1475 if (GC_FRAMEP (XCAR (tail))
1476 && (f = XFRAME (XCAR (tail)),
1477 (FRAME_X_P (f)
1478 && f->output_data.nothing != 1
1479 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1480 && f->output_data.x->widget == widget)
1481 return f;
1483 abort ();
1487 /* Allocate the color COLOR->pixel on the screen and display of
1488 widget WIDGET in colormap CMAP. If an exact match cannot be
1489 allocated, try the nearest color available. Value is non-zero
1490 if successful. This is called from lwlib. */
1493 x_alloc_nearest_color_for_widget (widget, cmap, color)
1494 Widget widget;
1495 Colormap cmap;
1496 XColor *color;
1498 struct frame *f = x_frame_of_widget (widget);
1499 return x_alloc_nearest_color (f, cmap, color);
1503 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1504 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1505 If this produces the same color as PIXEL, try a color where all RGB
1506 values have DELTA added. Return the allocated color in *PIXEL.
1507 DISPLAY is the X display, CMAP is the colormap to operate on.
1508 Value is non-zero if successful. */
1511 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1512 Widget widget;
1513 Display *display;
1514 Colormap cmap;
1515 unsigned long *pixel;
1516 double factor;
1517 int delta;
1519 struct frame *f = x_frame_of_widget (widget);
1520 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1524 /* Structure specifying which arguments should be passed by Xt to
1525 cvt_string_to_pixel. We want the widget's screen and colormap. */
1527 static XtConvertArgRec cvt_string_to_pixel_args[] =
1529 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1530 sizeof (Screen *)},
1531 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1532 sizeof (Colormap)}
1536 /* The address of this variable is returned by
1537 cvt_string_to_pixel. */
1539 static Pixel cvt_string_to_pixel_value;
1542 /* Convert a color name to a pixel color.
1544 DPY is the display we are working on.
1546 ARGS is an array of *NARGS XrmValue structures holding additional
1547 information about the widget for which the conversion takes place.
1548 The contents of this array are determined by the specification
1549 in cvt_string_to_pixel_args.
1551 FROM is a pointer to an XrmValue which points to the color name to
1552 convert. TO is an XrmValue in which to return the pixel color.
1554 CLOSURE_RET is a pointer to user-data, in which we record if
1555 we allocated the color or not.
1557 Value is True if successful, False otherwise. */
1559 static Boolean
1560 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1561 Display *dpy;
1562 XrmValue *args;
1563 Cardinal *nargs;
1564 XrmValue *from, *to;
1565 XtPointer *closure_ret;
1567 Screen *screen;
1568 Colormap cmap;
1569 Pixel pixel;
1570 String color_name;
1571 XColor color;
1573 if (*nargs != 2)
1575 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1576 "wrongParameters", "cvt_string_to_pixel",
1577 "XtToolkitError",
1578 "Screen and colormap args required", NULL, NULL);
1579 return False;
1582 screen = *(Screen **) args[0].addr;
1583 cmap = *(Colormap *) args[1].addr;
1584 color_name = (String) from->addr;
1586 if (strcmp (color_name, XtDefaultBackground) == 0)
1588 *closure_ret = (XtPointer) False;
1589 pixel = WhitePixelOfScreen (screen);
1591 else if (strcmp (color_name, XtDefaultForeground) == 0)
1593 *closure_ret = (XtPointer) False;
1594 pixel = BlackPixelOfScreen (screen);
1596 else if (XParseColor (dpy, cmap, color_name, &color)
1597 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1599 pixel = color.pixel;
1600 *closure_ret = (XtPointer) True;
1602 else
1604 String params[1];
1605 Cardinal nparams = 1;
1607 params[0] = color_name;
1608 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1609 "badValue", "cvt_string_to_pixel",
1610 "XtToolkitError", "Invalid color `%s'",
1611 params, &nparams);
1612 return False;
1615 if (to->addr != NULL)
1617 if (to->size < sizeof (Pixel))
1619 to->size = sizeof (Pixel);
1620 return False;
1623 *(Pixel *) to->addr = pixel;
1625 else
1627 cvt_string_to_pixel_value = pixel;
1628 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1631 to->size = sizeof (Pixel);
1632 return True;
1636 /* Free a pixel color which was previously allocated via
1637 cvt_string_to_pixel. This is registered as the destructor
1638 for this type of resource via XtSetTypeConverter.
1640 APP is the application context in which we work.
1642 TO is a pointer to an XrmValue holding the color to free.
1643 CLOSURE is the value we stored in CLOSURE_RET for this color
1644 in cvt_string_to_pixel.
1646 ARGS and NARGS are like for cvt_string_to_pixel. */
1648 static void
1649 cvt_pixel_dtor (app, to, closure, args, nargs)
1650 XtAppContext app;
1651 XrmValuePtr to;
1652 XtPointer closure;
1653 XrmValuePtr args;
1654 Cardinal *nargs;
1656 if (*nargs != 2)
1658 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1659 "XtToolkitError",
1660 "Screen and colormap arguments required",
1661 NULL, NULL);
1663 else if (closure != NULL)
1665 /* We did allocate the pixel, so free it. */
1666 Screen *screen = *(Screen **) args[0].addr;
1667 Colormap cmap = *(Colormap *) args[1].addr;
1668 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1669 (Pixel *) to->addr, 1);
1674 #endif /* USE_X_TOOLKIT */
1677 /* Value is an array of XColor structures for the contents of the
1678 color map of display DPY. Set *NCELLS to the size of the array.
1679 Note that this probably shouldn't be called for large color maps,
1680 say a 24-bit TrueColor map. */
1682 static const XColor *
1683 x_color_cells (dpy, ncells)
1684 Display *dpy;
1685 int *ncells;
1687 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1689 if (dpyinfo->color_cells == NULL)
1691 Screen *screen = dpyinfo->screen;
1692 int i;
1694 dpyinfo->ncolor_cells
1695 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1696 dpyinfo->color_cells
1697 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1698 * sizeof *dpyinfo->color_cells);
1700 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1701 dpyinfo->color_cells[i].pixel = i;
1703 XQueryColors (dpy, dpyinfo->cmap,
1704 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1707 *ncells = dpyinfo->ncolor_cells;
1708 return dpyinfo->color_cells;
1712 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1713 colors in COLORS. Use cached information, if available. */
1715 void
1716 x_query_colors (f, colors, ncolors)
1717 struct frame *f;
1718 XColor *colors;
1719 int ncolors;
1721 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1723 if (dpyinfo->color_cells)
1725 int i;
1726 for (i = 0; i < ncolors; ++i)
1728 unsigned long pixel = colors[i].pixel;
1729 xassert (pixel < dpyinfo->ncolor_cells);
1730 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1731 colors[i] = dpyinfo->color_cells[pixel];
1734 else
1735 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1739 /* On frame F, translate pixel color to RGB values for the color in
1740 COLOR. Use cached information, if available. */
1742 void
1743 x_query_color (f, color)
1744 struct frame *f;
1745 XColor *color;
1747 x_query_colors (f, color, 1);
1751 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1752 exact match can't be allocated, try the nearest color available.
1753 Value is non-zero if successful. Set *COLOR to the color
1754 allocated. */
1756 static int
1757 x_alloc_nearest_color_1 (dpy, cmap, color)
1758 Display *dpy;
1759 Colormap cmap;
1760 XColor *color;
1762 int rc;
1764 rc = XAllocColor (dpy, cmap, color);
1765 if (rc == 0)
1767 /* If we got to this point, the colormap is full, so we're going
1768 to try to get the next closest color. The algorithm used is
1769 a least-squares matching, which is what X uses for closest
1770 color matching with StaticColor visuals. */
1771 int nearest, i;
1772 unsigned long nearest_delta = ~0;
1773 int ncells;
1774 const XColor *cells = x_color_cells (dpy, &ncells);
1776 for (nearest = i = 0; i < ncells; ++i)
1778 long dred = (color->red >> 8) - (cells[i].red >> 8);
1779 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1780 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1781 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1783 if (delta < nearest_delta)
1785 nearest = i;
1786 nearest_delta = delta;
1790 color->red = cells[nearest].red;
1791 color->green = cells[nearest].green;
1792 color->blue = cells[nearest].blue;
1793 rc = XAllocColor (dpy, cmap, color);
1795 else
1797 /* If allocation succeeded, and the allocated pixel color is not
1798 equal to a cached pixel color recorded earlier, there was a
1799 change in the colormap, so clear the color cache. */
1800 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1801 XColor *cached_color;
1803 if (dpyinfo->color_cells
1804 && (cached_color = &dpyinfo->color_cells[color->pixel],
1805 (cached_color->red != color->red
1806 || cached_color->blue != color->blue
1807 || cached_color->green != color->green)))
1809 xfree (dpyinfo->color_cells);
1810 dpyinfo->color_cells = NULL;
1811 dpyinfo->ncolor_cells = 0;
1815 #ifdef DEBUG_X_COLORS
1816 if (rc)
1817 register_color (color->pixel);
1818 #endif /* DEBUG_X_COLORS */
1820 return rc;
1824 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1825 exact match can't be allocated, try the nearest color available.
1826 Value is non-zero if successful. Set *COLOR to the color
1827 allocated. */
1830 x_alloc_nearest_color (f, cmap, color)
1831 struct frame *f;
1832 Colormap cmap;
1833 XColor *color;
1835 gamma_correct (f, color);
1836 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1840 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1841 It's necessary to do this instead of just using PIXEL directly to
1842 get color reference counts right. */
1844 unsigned long
1845 x_copy_color (f, pixel)
1846 struct frame *f;
1847 unsigned long pixel;
1849 XColor color;
1851 color.pixel = pixel;
1852 BLOCK_INPUT;
1853 x_query_color (f, &color);
1854 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1855 UNBLOCK_INPUT;
1856 #ifdef DEBUG_X_COLORS
1857 register_color (pixel);
1858 #endif
1859 return color.pixel;
1863 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
1864 It's necessary to do this instead of just using PIXEL directly to
1865 get color reference counts right. */
1867 unsigned long
1868 x_copy_dpy_color (dpy, cmap, pixel)
1869 Display *dpy;
1870 Colormap cmap;
1871 unsigned long pixel;
1873 XColor color;
1875 color.pixel = pixel;
1876 BLOCK_INPUT;
1877 XQueryColor (dpy, cmap, &color);
1878 XAllocColor (dpy, cmap, &color);
1879 UNBLOCK_INPUT;
1880 #ifdef DEBUG_X_COLORS
1881 register_color (pixel);
1882 #endif
1883 return color.pixel;
1887 /* Brightness beyond which a color won't have its highlight brightness
1888 boosted.
1890 Nominally, highlight colors for `3d' faces are calculated by
1891 brightening an object's color by a constant scale factor, but this
1892 doesn't yield good results for dark colors, so for colors who's
1893 brightness is less than this value (on a scale of 0-65535) have an
1894 use an additional additive factor.
1896 The value here is set so that the default menu-bar/mode-line color
1897 (grey75) will not have its highlights changed at all. */
1898 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1901 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1902 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1903 If this produces the same color as PIXEL, try a color where all RGB
1904 values have DELTA added. Return the allocated color in *PIXEL.
1905 DISPLAY is the X display, CMAP is the colormap to operate on.
1906 Value is non-zero if successful. */
1908 static int
1909 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
1910 struct frame *f;
1911 Display *display;
1912 Colormap cmap;
1913 unsigned long *pixel;
1914 double factor;
1915 int delta;
1917 XColor color, new;
1918 long bright;
1919 int success_p;
1921 /* Get RGB color values. */
1922 color.pixel = *pixel;
1923 x_query_color (f, &color);
1925 /* Change RGB values by specified FACTOR. Avoid overflow! */
1926 xassert (factor >= 0);
1927 new.red = min (0xffff, factor * color.red);
1928 new.green = min (0xffff, factor * color.green);
1929 new.blue = min (0xffff, factor * color.blue);
1931 /* Calculate brightness of COLOR. */
1932 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1934 /* We only boost colors that are darker than
1935 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1936 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1937 /* Make an additive adjustment to NEW, because it's dark enough so
1938 that scaling by FACTOR alone isn't enough. */
1940 /* How far below the limit this color is (0 - 1, 1 being darker). */
1941 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1942 /* The additive adjustment. */
1943 int min_delta = delta * dimness * factor / 2;
1945 if (factor < 1)
1947 new.red = max (0, new.red - min_delta);
1948 new.green = max (0, new.green - min_delta);
1949 new.blue = max (0, new.blue - min_delta);
1951 else
1953 new.red = min (0xffff, min_delta + new.red);
1954 new.green = min (0xffff, min_delta + new.green);
1955 new.blue = min (0xffff, min_delta + new.blue);
1959 /* Try to allocate the color. */
1960 success_p = x_alloc_nearest_color (f, cmap, &new);
1961 if (success_p)
1963 if (new.pixel == *pixel)
1965 /* If we end up with the same color as before, try adding
1966 delta to the RGB values. */
1967 x_free_colors (f, &new.pixel, 1);
1969 new.red = min (0xffff, delta + color.red);
1970 new.green = min (0xffff, delta + color.green);
1971 new.blue = min (0xffff, delta + color.blue);
1972 success_p = x_alloc_nearest_color (f, cmap, &new);
1974 else
1975 success_p = 1;
1976 *pixel = new.pixel;
1979 return success_p;
1983 /* Set up the foreground color for drawing relief lines of glyph
1984 string S. RELIEF is a pointer to a struct relief containing the GC
1985 with which lines will be drawn. Use a color that is FACTOR or
1986 DELTA lighter or darker than the relief's background which is found
1987 in S->f->output_data.x->relief_background. If such a color cannot
1988 be allocated, use DEFAULT_PIXEL, instead. */
1990 static void
1991 x_setup_relief_color (f, relief, factor, delta, default_pixel)
1992 struct frame *f;
1993 struct relief *relief;
1994 double factor;
1995 int delta;
1996 unsigned long default_pixel;
1998 XGCValues xgcv;
1999 struct x_output *di = f->output_data.x;
2000 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
2001 unsigned long pixel;
2002 unsigned long background = di->relief_background;
2003 Colormap cmap = FRAME_X_COLORMAP (f);
2004 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2005 Display *dpy = FRAME_X_DISPLAY (f);
2007 xgcv.graphics_exposures = False;
2008 xgcv.line_width = 1;
2010 /* Free previously allocated color. The color cell will be reused
2011 when it has been freed as many times as it was allocated, so this
2012 doesn't affect faces using the same colors. */
2013 if (relief->gc
2014 && relief->allocated_p)
2016 x_free_colors (f, &relief->pixel, 1);
2017 relief->allocated_p = 0;
2020 /* Allocate new color. */
2021 xgcv.foreground = default_pixel;
2022 pixel = background;
2023 if (dpyinfo->n_planes != 1
2024 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
2026 relief->allocated_p = 1;
2027 xgcv.foreground = relief->pixel = pixel;
2030 if (relief->gc == 0)
2032 xgcv.stipple = dpyinfo->gray;
2033 mask |= GCStipple;
2034 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
2036 else
2037 XChangeGC (dpy, relief->gc, mask, &xgcv);
2041 /* Set up colors for the relief lines around glyph string S. */
2043 static void
2044 x_setup_relief_colors (s)
2045 struct glyph_string *s;
2047 struct x_output *di = s->f->output_data.x;
2048 unsigned long color;
2050 if (s->face->use_box_color_for_shadows_p)
2051 color = s->face->box_color;
2052 else if (s->first_glyph->type == IMAGE_GLYPH
2053 && s->img->pixmap
2054 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2055 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2056 else
2058 XGCValues xgcv;
2060 /* Get the background color of the face. */
2061 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2062 color = xgcv.background;
2065 if (di->white_relief.gc == 0
2066 || color != di->relief_background)
2068 di->relief_background = color;
2069 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2070 WHITE_PIX_DEFAULT (s->f));
2071 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2072 BLACK_PIX_DEFAULT (s->f));
2077 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2078 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2079 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
2080 relief. LEFT_P non-zero means draw a relief on the left side of
2081 the rectangle. RIGHT_P non-zero means draw a relief on the right
2082 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2083 when drawing. */
2085 static void
2086 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
2087 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
2088 struct frame *f;
2089 int left_x, top_y, right_x, bottom_y, width;
2090 int top_p, bot_p, left_p, right_p, raised_p;
2091 XRectangle *clip_rect;
2093 Display *dpy = FRAME_X_DISPLAY (f);
2094 Window window = FRAME_X_WINDOW (f);
2095 int i;
2096 GC gc;
2098 if (raised_p)
2099 gc = f->output_data.x->white_relief.gc;
2100 else
2101 gc = f->output_data.x->black_relief.gc;
2102 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2104 /* Top. */
2105 if (top_p)
2106 for (i = 0; i < width; ++i)
2107 XDrawLine (dpy, window, gc,
2108 left_x + i * left_p, top_y + i,
2109 right_x + 1 - i * right_p, top_y + i);
2111 /* Left. */
2112 if (left_p)
2113 for (i = 0; i < width; ++i)
2114 XDrawLine (dpy, window, gc,
2115 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
2117 XSetClipMask (dpy, gc, None);
2118 if (raised_p)
2119 gc = f->output_data.x->black_relief.gc;
2120 else
2121 gc = f->output_data.x->white_relief.gc;
2122 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2124 /* Bottom. */
2125 if (bot_p)
2126 for (i = 0; i < width; ++i)
2127 XDrawLine (dpy, window, gc,
2128 left_x + i * left_p, bottom_y - i,
2129 right_x + 1 - i * right_p, bottom_y - i);
2131 /* Right. */
2132 if (right_p)
2133 for (i = 0; i < width; ++i)
2134 XDrawLine (dpy, window, gc,
2135 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2137 XSetClipMask (dpy, gc, None);
2141 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2142 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2143 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2144 left side of the rectangle. RIGHT_P non-zero means draw a line
2145 on the right side of the rectangle. CLIP_RECT is the clipping
2146 rectangle to use when drawing. */
2148 static void
2149 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2150 left_p, right_p, clip_rect)
2151 struct glyph_string *s;
2152 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
2153 XRectangle *clip_rect;
2155 XGCValues xgcv;
2157 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2158 XSetForeground (s->display, s->gc, s->face->box_color);
2159 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2161 /* Top. */
2162 XFillRectangle (s->display, s->window, s->gc,
2163 left_x, top_y, right_x - left_x + 1, width);
2165 /* Left. */
2166 if (left_p)
2167 XFillRectangle (s->display, s->window, s->gc,
2168 left_x, top_y, width, bottom_y - top_y + 1);
2170 /* Bottom. */
2171 XFillRectangle (s->display, s->window, s->gc,
2172 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2174 /* Right. */
2175 if (right_p)
2176 XFillRectangle (s->display, s->window, s->gc,
2177 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2179 XSetForeground (s->display, s->gc, xgcv.foreground);
2180 XSetClipMask (s->display, s->gc, None);
2184 /* Draw a box around glyph string S. */
2186 static void
2187 x_draw_glyph_string_box (s)
2188 struct glyph_string *s;
2190 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2191 int left_p, right_p;
2192 struct glyph *last_glyph;
2193 XRectangle clip_rect;
2195 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2196 ? WINDOW_RIGHT_EDGE_X (s->w)
2197 : window_box_right (s->w, s->area));
2199 /* The glyph that may have a right box line. */
2200 last_glyph = (s->cmp || s->img
2201 ? s->first_glyph
2202 : s->first_glyph + s->nchars - 1);
2204 width = eabs (s->face->box_line_width);
2205 raised_p = s->face->box == FACE_RAISED_BOX;
2206 left_x = s->x;
2207 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2208 ? last_x - 1
2209 : min (last_x, s->x + s->background_width) - 1);
2210 top_y = s->y;
2211 bottom_y = top_y + s->height - 1;
2213 left_p = (s->first_glyph->left_box_line_p
2214 || (s->hl == DRAW_MOUSE_FACE
2215 && (s->prev == NULL
2216 || s->prev->hl != s->hl)));
2217 right_p = (last_glyph->right_box_line_p
2218 || (s->hl == DRAW_MOUSE_FACE
2219 && (s->next == NULL
2220 || s->next->hl != s->hl)));
2222 get_glyph_string_clip_rect (s, &clip_rect);
2224 if (s->face->box == FACE_SIMPLE_BOX)
2225 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2226 left_p, right_p, &clip_rect);
2227 else
2229 x_setup_relief_colors (s);
2230 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2231 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2236 /* Draw foreground of image glyph string S. */
2238 static void
2239 x_draw_image_foreground (s)
2240 struct glyph_string *s;
2242 int x = s->x;
2243 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2245 /* If first glyph of S has a left box line, start drawing it to the
2246 right of that line. */
2247 if (s->face->box != FACE_NO_BOX
2248 && s->first_glyph->left_box_line_p
2249 && s->slice.x == 0)
2250 x += eabs (s->face->box_line_width);
2252 /* If there is a margin around the image, adjust x- and y-position
2253 by that margin. */
2254 if (s->slice.x == 0)
2255 x += s->img->hmargin;
2256 if (s->slice.y == 0)
2257 y += s->img->vmargin;
2259 if (s->img->pixmap)
2261 if (s->img->mask)
2263 /* We can't set both a clip mask and use XSetClipRectangles
2264 because the latter also sets a clip mask. We also can't
2265 trust on the shape extension to be available
2266 (XShapeCombineRegion). So, compute the rectangle to draw
2267 manually. */
2268 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2269 | GCFunction);
2270 XGCValues xgcv;
2271 XRectangle clip_rect, image_rect, r;
2273 xgcv.clip_mask = s->img->mask;
2274 xgcv.clip_x_origin = x;
2275 xgcv.clip_y_origin = y;
2276 xgcv.function = GXcopy;
2277 XChangeGC (s->display, s->gc, mask, &xgcv);
2279 get_glyph_string_clip_rect (s, &clip_rect);
2280 image_rect.x = x;
2281 image_rect.y = y;
2282 image_rect.width = s->slice.width;
2283 image_rect.height = s->slice.height;
2284 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2285 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2286 s->slice.x + r.x - x, s->slice.y + r.y - y,
2287 r.width, r.height, r.x, r.y);
2289 else
2291 XRectangle clip_rect, image_rect, r;
2293 get_glyph_string_clip_rect (s, &clip_rect);
2294 image_rect.x = x;
2295 image_rect.y = y;
2296 image_rect.width = s->slice.width;
2297 image_rect.height = s->slice.height;
2298 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2299 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2300 s->slice.x + r.x - x, s->slice.y + r.y - y,
2301 r.width, r.height, r.x, r.y);
2303 /* When the image has a mask, we can expect that at
2304 least part of a mouse highlight or a block cursor will
2305 be visible. If the image doesn't have a mask, make
2306 a block cursor visible by drawing a rectangle around
2307 the image. I believe it's looking better if we do
2308 nothing here for mouse-face. */
2309 if (s->hl == DRAW_CURSOR)
2311 int r = s->img->relief;
2312 if (r < 0) r = -r;
2313 XDrawRectangle (s->display, s->window, s->gc,
2314 x - r, y - r,
2315 s->slice.width + r*2 - 1,
2316 s->slice.height + r*2 - 1);
2320 else
2321 /* Draw a rectangle if image could not be loaded. */
2322 XDrawRectangle (s->display, s->window, s->gc, x, y,
2323 s->slice.width - 1, s->slice.height - 1);
2327 /* Draw a relief around the image glyph string S. */
2329 static void
2330 x_draw_image_relief (s)
2331 struct glyph_string *s;
2333 int x0, y0, x1, y1, thick, raised_p;
2334 XRectangle r;
2335 int x = s->x;
2336 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2338 /* If first glyph of S has a left box line, start drawing it to the
2339 right of that line. */
2340 if (s->face->box != FACE_NO_BOX
2341 && s->first_glyph->left_box_line_p
2342 && s->slice.x == 0)
2343 x += eabs (s->face->box_line_width);
2345 /* If there is a margin around the image, adjust x- and y-position
2346 by that margin. */
2347 if (s->slice.x == 0)
2348 x += s->img->hmargin;
2349 if (s->slice.y == 0)
2350 y += s->img->vmargin;
2352 if (s->hl == DRAW_IMAGE_SUNKEN
2353 || s->hl == DRAW_IMAGE_RAISED)
2355 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2356 raised_p = s->hl == DRAW_IMAGE_RAISED;
2358 else
2360 thick = eabs (s->img->relief);
2361 raised_p = s->img->relief > 0;
2364 x0 = x - thick;
2365 y0 = y - thick;
2366 x1 = x + s->slice.width + thick - 1;
2367 y1 = y + s->slice.height + thick - 1;
2369 x_setup_relief_colors (s);
2370 get_glyph_string_clip_rect (s, &r);
2371 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
2372 s->slice.y == 0,
2373 s->slice.y + s->slice.height == s->img->height,
2374 s->slice.x == 0,
2375 s->slice.x + s->slice.width == s->img->width,
2376 &r);
2380 /* Draw the foreground of image glyph string S to PIXMAP. */
2382 static void
2383 x_draw_image_foreground_1 (s, pixmap)
2384 struct glyph_string *s;
2385 Pixmap pixmap;
2387 int x = 0;
2388 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2390 /* If first glyph of S has a left box line, start drawing it to the
2391 right of that line. */
2392 if (s->face->box != FACE_NO_BOX
2393 && s->first_glyph->left_box_line_p
2394 && s->slice.x == 0)
2395 x += eabs (s->face->box_line_width);
2397 /* If there is a margin around the image, adjust x- and y-position
2398 by that margin. */
2399 if (s->slice.x == 0)
2400 x += s->img->hmargin;
2401 if (s->slice.y == 0)
2402 y += s->img->vmargin;
2404 if (s->img->pixmap)
2406 if (s->img->mask)
2408 /* We can't set both a clip mask and use XSetClipRectangles
2409 because the latter also sets a clip mask. We also can't
2410 trust on the shape extension to be available
2411 (XShapeCombineRegion). So, compute the rectangle to draw
2412 manually. */
2413 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2414 | GCFunction);
2415 XGCValues xgcv;
2417 xgcv.clip_mask = s->img->mask;
2418 xgcv.clip_x_origin = x - s->slice.x;
2419 xgcv.clip_y_origin = y - s->slice.y;
2420 xgcv.function = GXcopy;
2421 XChangeGC (s->display, s->gc, mask, &xgcv);
2423 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2424 s->slice.x, s->slice.y,
2425 s->slice.width, s->slice.height, x, y);
2426 XSetClipMask (s->display, s->gc, None);
2428 else
2430 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2431 s->slice.x, s->slice.y,
2432 s->slice.width, s->slice.height, x, y);
2434 /* When the image has a mask, we can expect that at
2435 least part of a mouse highlight or a block cursor will
2436 be visible. If the image doesn't have a mask, make
2437 a block cursor visible by drawing a rectangle around
2438 the image. I believe it's looking better if we do
2439 nothing here for mouse-face. */
2440 if (s->hl == DRAW_CURSOR)
2442 int r = s->img->relief;
2443 if (r < 0) r = -r;
2444 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2445 s->slice.width + r*2 - 1,
2446 s->slice.height + r*2 - 1);
2450 else
2451 /* Draw a rectangle if image could not be loaded. */
2452 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2453 s->slice.width - 1, s->slice.height - 1);
2457 /* Draw part of the background of glyph string S. X, Y, W, and H
2458 give the rectangle to draw. */
2460 static void
2461 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2462 struct glyph_string *s;
2463 int x, y, w, h;
2465 if (s->stippled_p)
2467 /* Fill background with a stipple pattern. */
2468 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2469 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2470 XSetFillStyle (s->display, s->gc, FillSolid);
2472 else
2473 x_clear_glyph_string_rect (s, x, y, w, h);
2477 /* Draw image glyph string S.
2479 s->y
2480 s->x +-------------------------
2481 | s->face->box
2483 | +-------------------------
2484 | | s->img->margin
2486 | | +-------------------
2487 | | | the image
2491 static void
2492 x_draw_image_glyph_string (s)
2493 struct glyph_string *s;
2495 int box_line_hwidth = eabs (s->face->box_line_width);
2496 int box_line_vwidth = max (s->face->box_line_width, 0);
2497 int height;
2498 Pixmap pixmap = None;
2500 height = s->height;
2501 if (s->slice.y == 0)
2502 height -= box_line_vwidth;
2503 if (s->slice.y + s->slice.height >= s->img->height)
2504 height -= box_line_vwidth;
2506 /* Fill background with face under the image. Do it only if row is
2507 taller than image or if image has a clip mask to reduce
2508 flickering. */
2509 s->stippled_p = s->face->stipple != 0;
2510 if (height > s->slice.height
2511 || s->img->hmargin
2512 || s->img->vmargin
2513 || s->img->mask
2514 || s->img->pixmap == 0
2515 || s->width != s->background_width)
2517 if (s->img->mask)
2519 /* Create a pixmap as large as the glyph string. Fill it
2520 with the background color. Copy the image to it, using
2521 its mask. Copy the temporary pixmap to the display. */
2522 Screen *screen = FRAME_X_SCREEN (s->f);
2523 int depth = DefaultDepthOfScreen (screen);
2525 /* Create a pixmap as large as the glyph string. */
2526 pixmap = XCreatePixmap (s->display, s->window,
2527 s->background_width,
2528 s->height, depth);
2530 /* Don't clip in the following because we're working on the
2531 pixmap. */
2532 XSetClipMask (s->display, s->gc, None);
2534 /* Fill the pixmap with the background color/stipple. */
2535 if (s->stippled_p)
2537 /* Fill background with a stipple pattern. */
2538 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2539 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
2540 XFillRectangle (s->display, pixmap, s->gc,
2541 0, 0, s->background_width, s->height);
2542 XSetFillStyle (s->display, s->gc, FillSolid);
2543 XSetTSOrigin (s->display, s->gc, 0, 0);
2545 else
2547 XGCValues xgcv;
2548 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2549 &xgcv);
2550 XSetForeground (s->display, s->gc, xgcv.background);
2551 XFillRectangle (s->display, pixmap, s->gc,
2552 0, 0, s->background_width, s->height);
2553 XSetForeground (s->display, s->gc, xgcv.foreground);
2556 else
2558 int x = s->x;
2559 int y = s->y;
2561 if (s->first_glyph->left_box_line_p
2562 && s->slice.x == 0)
2563 x += box_line_hwidth;
2565 if (s->slice.y == 0)
2566 y += box_line_vwidth;
2568 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2571 s->background_filled_p = 1;
2574 /* Draw the foreground. */
2575 if (pixmap != None)
2577 x_draw_image_foreground_1 (s, pixmap);
2578 x_set_glyph_string_clipping (s);
2579 XCopyArea (s->display, pixmap, s->window, s->gc,
2580 0, 0, s->background_width, s->height, s->x, s->y);
2581 XFreePixmap (s->display, pixmap);
2583 else
2584 x_draw_image_foreground (s);
2586 /* If we must draw a relief around the image, do it. */
2587 if (s->img->relief
2588 || s->hl == DRAW_IMAGE_RAISED
2589 || s->hl == DRAW_IMAGE_SUNKEN)
2590 x_draw_image_relief (s);
2594 /* Draw stretch glyph string S. */
2596 static void
2597 x_draw_stretch_glyph_string (s)
2598 struct glyph_string *s;
2600 xassert (s->first_glyph->type == STRETCH_GLYPH);
2602 if (s->hl == DRAW_CURSOR
2603 && !x_stretch_cursor_p)
2605 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2606 as wide as the stretch glyph. */
2607 int width, background_width = s->background_width;
2608 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2610 if (x < left_x)
2612 background_width -= left_x - x;
2613 x = left_x;
2615 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2617 /* Draw cursor. */
2618 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2620 /* Clear rest using the GC of the original non-cursor face. */
2621 if (width < background_width)
2623 int y = s->y;
2624 int w = background_width - width, h = s->height;
2625 XRectangle r;
2626 GC gc;
2628 x += width;
2629 if (s->row->mouse_face_p
2630 && cursor_in_mouse_face_p (s->w))
2632 x_set_mouse_face_gc (s);
2633 gc = s->gc;
2635 else
2636 gc = s->face->gc;
2638 get_glyph_string_clip_rect (s, &r);
2639 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2641 if (s->face->stipple)
2643 /* Fill background with a stipple pattern. */
2644 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2645 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2646 XSetFillStyle (s->display, gc, FillSolid);
2648 else
2650 XGCValues xgcv;
2651 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2652 XSetForeground (s->display, gc, xgcv.background);
2653 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2654 XSetForeground (s->display, gc, xgcv.foreground);
2658 else if (!s->background_filled_p)
2660 int background_width = s->background_width;
2661 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2663 /* Don't draw into left margin, fringe or scrollbar area
2664 except for header line and mode line. */
2665 if (x < left_x && !s->row->mode_line_p)
2667 background_width -= left_x - x;
2668 x = left_x;
2670 if (background_width > 0)
2671 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2674 s->background_filled_p = 1;
2678 /* Draw glyph string S. */
2680 static void
2681 x_draw_glyph_string (s)
2682 struct glyph_string *s;
2684 int relief_drawn_p = 0;
2686 /* If S draws into the background of its successor, draw the
2687 background of the successor first so that S can draw into it.
2688 This makes S->next use XDrawString instead of XDrawImageString. */
2689 if (s->next && s->right_overhang && !s->for_overlaps)
2691 xassert (s->next->img == NULL);
2692 x_set_glyph_string_gc (s->next);
2693 x_set_glyph_string_clipping (s->next);
2694 x_draw_glyph_string_background (s->next, 1);
2697 /* Set up S->gc, set clipping and draw S. */
2698 x_set_glyph_string_gc (s);
2700 /* Draw relief (if any) in advance for char/composition so that the
2701 glyph string can be drawn over it. */
2702 if (!s->for_overlaps
2703 && s->face->box != FACE_NO_BOX
2704 && (s->first_glyph->type == CHAR_GLYPH
2705 || s->first_glyph->type == COMPOSITE_GLYPH))
2708 x_set_glyph_string_clipping (s);
2709 x_draw_glyph_string_background (s, 1);
2710 x_draw_glyph_string_box (s);
2711 x_set_glyph_string_clipping (s);
2712 relief_drawn_p = 1;
2714 else
2715 x_set_glyph_string_clipping (s);
2717 switch (s->first_glyph->type)
2719 case IMAGE_GLYPH:
2720 x_draw_image_glyph_string (s);
2721 break;
2723 case STRETCH_GLYPH:
2724 x_draw_stretch_glyph_string (s);
2725 break;
2727 case CHAR_GLYPH:
2728 if (s->for_overlaps)
2729 s->background_filled_p = 1;
2730 else
2731 x_draw_glyph_string_background (s, 0);
2732 x_draw_glyph_string_foreground (s);
2733 break;
2735 case COMPOSITE_GLYPH:
2736 if (s->for_overlaps || s->gidx > 0)
2737 s->background_filled_p = 1;
2738 else
2739 x_draw_glyph_string_background (s, 1);
2740 x_draw_composite_glyph_string_foreground (s);
2741 break;
2743 default:
2744 abort ();
2747 if (!s->for_overlaps)
2749 /* Draw underline. */
2750 if (s->face->underline_p)
2752 unsigned long tem, h;
2753 int y;
2755 /* Get the underline thickness. Default is 1 pixel. */
2756 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
2757 h = 1;
2759 y = s->y + s->height - h;
2760 if (!x_underline_at_descent_line)
2762 /* Get the underline position. This is the recommended
2763 vertical offset in pixels from the baseline to the top of
2764 the underline. This is a signed value according to the
2765 specs, and its default is
2767 ROUND ((maximum descent) / 2), with
2768 ROUND(x) = floor (x + 0.5) */
2770 if (x_use_underline_position_properties
2771 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
2772 y = s->ybase + (long) tem;
2773 else if (s->face->font)
2774 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
2777 if (s->face->underline_defaulted_p)
2778 XFillRectangle (s->display, s->window, s->gc,
2779 s->x, y, s->background_width, h);
2780 else
2782 XGCValues xgcv;
2783 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2784 XSetForeground (s->display, s->gc, s->face->underline_color);
2785 XFillRectangle (s->display, s->window, s->gc,
2786 s->x, y, s->background_width, h);
2787 XSetForeground (s->display, s->gc, xgcv.foreground);
2791 /* Draw overline. */
2792 if (s->face->overline_p)
2794 unsigned long dy = 0, h = 1;
2796 if (s->face->overline_color_defaulted_p)
2797 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2798 s->background_width, h);
2799 else
2801 XGCValues xgcv;
2802 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2803 XSetForeground (s->display, s->gc, s->face->overline_color);
2804 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2805 s->background_width, h);
2806 XSetForeground (s->display, s->gc, xgcv.foreground);
2810 /* Draw strike-through. */
2811 if (s->face->strike_through_p)
2813 unsigned long h = 1;
2814 unsigned long dy = (s->height - h) / 2;
2816 if (s->face->strike_through_color_defaulted_p)
2817 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2818 s->width, h);
2819 else
2821 XGCValues xgcv;
2822 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2823 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2824 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2825 s->width, h);
2826 XSetForeground (s->display, s->gc, xgcv.foreground);
2830 /* Draw relief if not yet drawn. */
2831 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2832 x_draw_glyph_string_box (s);
2835 /* Reset clipping. */
2836 XSetClipMask (s->display, s->gc, None);
2839 /* Shift display to make room for inserted glyphs. */
2841 void
2842 x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2843 struct frame *f;
2844 int x, y, width, height, shift_by;
2846 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2847 f->output_data.x->normal_gc,
2848 x, y, width, height,
2849 x + shift_by, y);
2852 /* Delete N glyphs at the nominal cursor position. Not implemented
2853 for X frames. */
2855 static void
2856 x_delete_glyphs (f, n)
2857 struct frame *f;
2858 register int n;
2860 abort ();
2864 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2865 If they are <= 0, this is probably an error. */
2867 void
2868 x_clear_area (dpy, window, x, y, width, height, exposures)
2869 Display *dpy;
2870 Window window;
2871 int x, y;
2872 int width, height;
2873 int exposures;
2875 xassert (width > 0 && height > 0);
2876 XClearArea (dpy, window, x, y, width, height, exposures);
2880 /* Clear an entire frame. */
2882 static void
2883 x_clear_frame (struct frame *f)
2885 /* Clearing the frame will erase any cursor, so mark them all as no
2886 longer visible. */
2887 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2888 output_cursor.hpos = output_cursor.vpos = 0;
2889 output_cursor.x = -1;
2891 /* We don't set the output cursor here because there will always
2892 follow an explicit cursor_to. */
2893 BLOCK_INPUT;
2894 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2896 /* We have to clear the scroll bars, too. If we have changed
2897 colors or something like that, then they should be notified. */
2898 x_scroll_bar_clear (f);
2900 XFlush (FRAME_X_DISPLAY (f));
2902 UNBLOCK_INPUT;
2907 /* Invert the middle quarter of the frame for .15 sec. */
2909 /* We use the select system call to do the waiting, so we have to make
2910 sure it's available. If it isn't, we just won't do visual bells. */
2912 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2915 /* Subtract the `struct timeval' values X and Y, storing the result in
2916 *RESULT. Return 1 if the difference is negative, otherwise 0. */
2918 static int
2919 timeval_subtract (result, x, y)
2920 struct timeval *result, x, y;
2922 /* Perform the carry for the later subtraction by updating y. This
2923 is safer because on some systems the tv_sec member is unsigned. */
2924 if (x.tv_usec < y.tv_usec)
2926 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
2927 y.tv_usec -= 1000000 * nsec;
2928 y.tv_sec += nsec;
2931 if (x.tv_usec - y.tv_usec > 1000000)
2933 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
2934 y.tv_usec += 1000000 * nsec;
2935 y.tv_sec -= nsec;
2938 /* Compute the time remaining to wait. tv_usec is certainly
2939 positive. */
2940 result->tv_sec = x.tv_sec - y.tv_sec;
2941 result->tv_usec = x.tv_usec - y.tv_usec;
2943 /* Return indication of whether the result should be considered
2944 negative. */
2945 return x.tv_sec < y.tv_sec;
2948 void
2949 XTflash (f)
2950 struct frame *f;
2952 BLOCK_INPUT;
2955 GC gc;
2957 /* Create a GC that will use the GXxor function to flip foreground
2958 pixels into background pixels. */
2960 XGCValues values;
2962 values.function = GXxor;
2963 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
2964 ^ FRAME_BACKGROUND_PIXEL (f));
2966 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2967 GCFunction | GCForeground, &values);
2971 /* Get the height not including a menu bar widget. */
2972 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
2973 /* Height of each line to flash. */
2974 int flash_height = FRAME_LINE_HEIGHT (f);
2975 /* These will be the left and right margins of the rectangles. */
2976 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
2977 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
2979 int width;
2981 /* Don't flash the area between a scroll bar and the frame
2982 edge it is next to. */
2983 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
2985 case vertical_scroll_bar_left:
2986 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2987 break;
2989 case vertical_scroll_bar_right:
2990 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2991 break;
2993 default:
2994 break;
2997 width = flash_right - flash_left;
2999 /* If window is tall, flash top and bottom line. */
3000 if (height > 3 * FRAME_LINE_HEIGHT (f))
3002 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3003 flash_left,
3004 (FRAME_INTERNAL_BORDER_WIDTH (f)
3005 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
3006 width, flash_height);
3007 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3008 flash_left,
3009 (height - flash_height
3010 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3011 width, flash_height);
3013 else
3014 /* If it is short, flash it all. */
3015 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3016 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3017 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3019 x_flush (f);
3022 struct timeval wakeup;
3024 EMACS_GET_TIME (wakeup);
3026 /* Compute time to wait until, propagating carry from usecs. */
3027 wakeup.tv_usec += 150000;
3028 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
3029 wakeup.tv_usec %= 1000000;
3031 /* Keep waiting until past the time wakeup or any input gets
3032 available. */
3033 while (! detect_input_pending ())
3035 struct timeval current;
3036 struct timeval timeout;
3038 EMACS_GET_TIME (current);
3040 /* Break if result would be negative. */
3041 if (timeval_subtract (&current, wakeup, current))
3042 break;
3044 /* How long `select' should wait. */
3045 timeout.tv_sec = 0;
3046 timeout.tv_usec = 10000;
3048 /* Try to wait that long--but we might wake up sooner. */
3049 select (0, NULL, NULL, NULL, &timeout);
3053 /* If window is tall, flash top and bottom line. */
3054 if (height > 3 * FRAME_LINE_HEIGHT (f))
3056 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3057 flash_left,
3058 (FRAME_INTERNAL_BORDER_WIDTH (f)
3059 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
3060 width, flash_height);
3061 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3062 flash_left,
3063 (height - flash_height
3064 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3065 width, flash_height);
3067 else
3068 /* If it is short, flash it all. */
3069 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3070 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3071 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3073 XFreeGC (FRAME_X_DISPLAY (f), gc);
3074 x_flush (f);
3078 UNBLOCK_INPUT;
3081 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
3084 /* Make audible bell. */
3086 void
3087 XTring_bell ()
3089 struct frame *f = SELECTED_FRAME ();
3091 if (FRAME_X_DISPLAY (f))
3093 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3094 if (visible_bell)
3095 XTflash (f);
3096 else
3097 #endif
3099 BLOCK_INPUT;
3100 XBell (FRAME_X_DISPLAY (f), 0);
3101 XFlush (FRAME_X_DISPLAY (f));
3102 UNBLOCK_INPUT;
3108 /* Specify how many text lines, from the top of the window,
3109 should be affected by insert-lines and delete-lines operations.
3110 This, and those operations, are used only within an update
3111 that is bounded by calls to x_update_begin and x_update_end. */
3113 static void
3114 XTset_terminal_window (n)
3115 register int n;
3117 /* This function intentionally left blank. */
3122 /***********************************************************************
3123 Line Dance
3124 ***********************************************************************/
3126 /* Perform an insert-lines or delete-lines operation, inserting N
3127 lines or deleting -N lines at vertical position VPOS. */
3129 static void
3130 x_ins_del_lines (f, vpos, n)
3131 struct frame *f;
3132 int vpos, n;
3134 abort ();
3138 /* Scroll part of the display as described by RUN. */
3140 static void
3141 x_scroll_run (w, run)
3142 struct window *w;
3143 struct run *run;
3145 struct frame *f = XFRAME (w->frame);
3146 int x, y, width, height, from_y, to_y, bottom_y;
3148 /* Get frame-relative bounding box of the text display area of W,
3149 without mode lines. Include in this box the left and right
3150 fringe of W. */
3151 window_box (w, -1, &x, &y, &width, &height);
3153 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3154 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3155 bottom_y = y + height;
3157 if (to_y < from_y)
3159 /* Scrolling up. Make sure we don't copy part of the mode
3160 line at the bottom. */
3161 if (from_y + run->height > bottom_y)
3162 height = bottom_y - from_y;
3163 else
3164 height = run->height;
3166 else
3168 /* Scolling down. Make sure we don't copy over the mode line.
3169 at the bottom. */
3170 if (to_y + run->height > bottom_y)
3171 height = bottom_y - to_y;
3172 else
3173 height = run->height;
3176 BLOCK_INPUT;
3178 /* Cursor off. Will be switched on again in x_update_window_end. */
3179 updated_window = w;
3180 x_clear_cursor (w);
3182 XCopyArea (FRAME_X_DISPLAY (f),
3183 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3184 f->output_data.x->normal_gc,
3185 x, from_y,
3186 width, height,
3187 x, to_y);
3189 UNBLOCK_INPUT;
3194 /***********************************************************************
3195 Exposure Events
3196 ***********************************************************************/
3199 static void
3200 frame_highlight (f)
3201 struct frame *f;
3203 /* We used to only do this if Vx_no_window_manager was non-nil, but
3204 the ICCCM (section 4.1.6) says that the window's border pixmap
3205 and border pixel are window attributes which are "private to the
3206 client", so we can always change it to whatever we want. */
3207 BLOCK_INPUT;
3208 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3209 f->output_data.x->border_pixel);
3210 UNBLOCK_INPUT;
3211 x_update_cursor (f, 1);
3214 static void
3215 frame_unhighlight (f)
3216 struct frame *f;
3218 /* We used to only do this if Vx_no_window_manager was non-nil, but
3219 the ICCCM (section 4.1.6) says that the window's border pixmap
3220 and border pixel are window attributes which are "private to the
3221 client", so we can always change it to whatever we want. */
3222 BLOCK_INPUT;
3223 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3224 f->output_data.x->border_tile);
3225 UNBLOCK_INPUT;
3226 x_update_cursor (f, 1);
3229 /* The focus has changed. Update the frames as necessary to reflect
3230 the new situation. Note that we can't change the selected frame
3231 here, because the Lisp code we are interrupting might become confused.
3232 Each event gets marked with the frame in which it occurred, so the
3233 Lisp code can tell when the switch took place by examining the events. */
3235 static void
3236 x_new_focus_frame (dpyinfo, frame)
3237 struct x_display_info *dpyinfo;
3238 struct frame *frame;
3240 struct frame *old_focus = dpyinfo->x_focus_frame;
3242 if (frame != dpyinfo->x_focus_frame)
3244 /* Set this before calling other routines, so that they see
3245 the correct value of x_focus_frame. */
3246 dpyinfo->x_focus_frame = frame;
3248 if (old_focus && old_focus->auto_lower)
3249 x_lower_frame (old_focus);
3251 #if 0
3252 selected_frame = frame;
3253 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
3254 selected_frame);
3255 Fselect_window (selected_frame->selected_window, Qnil);
3256 choose_minibuf_frame ();
3257 #endif /* ! 0 */
3259 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3260 pending_autoraise_frame = dpyinfo->x_focus_frame;
3261 else
3262 pending_autoraise_frame = 0;
3265 x_frame_rehighlight (dpyinfo);
3268 /* Handle FocusIn and FocusOut state changes for FRAME.
3269 If FRAME has focus and there exists more than one frame, puts
3270 a FOCUS_IN_EVENT into *BUFP. */
3272 static void
3273 x_focus_changed (type, state, dpyinfo, frame, bufp)
3274 int type;
3275 int state;
3276 struct x_display_info *dpyinfo;
3277 struct frame *frame;
3278 struct input_event *bufp;
3280 if (type == FocusIn)
3282 if (dpyinfo->x_focus_event_frame != frame)
3284 x_new_focus_frame (dpyinfo, frame);
3285 dpyinfo->x_focus_event_frame = frame;
3287 /* Don't stop displaying the initial startup message
3288 for a switch-frame event we don't need. */
3289 if (GC_NILP (Vterminal_frame)
3290 && GC_CONSP (Vframe_list)
3291 && !GC_NILP (XCDR (Vframe_list)))
3293 bufp->kind = FOCUS_IN_EVENT;
3294 XSETFRAME (bufp->frame_or_window, frame);
3298 frame->output_data.x->focus_state |= state;
3300 #ifdef HAVE_X_I18N
3301 if (FRAME_XIC (frame))
3302 XSetICFocus (FRAME_XIC (frame));
3303 #endif
3305 else if (type == FocusOut)
3307 frame->output_data.x->focus_state &= ~state;
3309 if (dpyinfo->x_focus_event_frame == frame)
3311 dpyinfo->x_focus_event_frame = 0;
3312 x_new_focus_frame (dpyinfo, 0);
3315 #ifdef HAVE_X_I18N
3316 if (FRAME_XIC (frame))
3317 XUnsetICFocus (FRAME_XIC (frame));
3318 #endif
3322 /* The focus may have changed. Figure out if it is a real focus change,
3323 by checking both FocusIn/Out and Enter/LeaveNotify events.
3325 Returns FOCUS_IN_EVENT event in *BUFP. */
3327 static void
3328 x_detect_focus_change (dpyinfo, event, bufp)
3329 struct x_display_info *dpyinfo;
3330 XEvent *event;
3331 struct input_event *bufp;
3333 struct frame *frame;
3335 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3336 if (! frame)
3337 return;
3339 switch (event->type)
3341 case EnterNotify:
3342 case LeaveNotify:
3344 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3345 int focus_state
3346 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3348 if (event->xcrossing.detail != NotifyInferior
3349 && event->xcrossing.focus
3350 && ! (focus_state & FOCUS_EXPLICIT))
3351 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3352 FOCUS_IMPLICIT,
3353 dpyinfo, frame, bufp);
3355 break;
3357 case FocusIn:
3358 case FocusOut:
3359 x_focus_changed (event->type,
3360 (event->xfocus.detail == NotifyPointer ?
3361 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3362 dpyinfo, frame, bufp);
3363 break;
3368 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3370 void
3371 x_mouse_leave (dpyinfo)
3372 struct x_display_info *dpyinfo;
3374 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3377 /* The focus has changed, or we have redirected a frame's focus to
3378 another frame (this happens when a frame uses a surrogate
3379 mini-buffer frame). Shift the highlight as appropriate.
3381 The FRAME argument doesn't necessarily have anything to do with which
3382 frame is being highlighted or un-highlighted; we only use it to find
3383 the appropriate X display info. */
3385 static void
3386 XTframe_rehighlight (frame)
3387 struct frame *frame;
3389 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3392 static void
3393 x_frame_rehighlight (dpyinfo)
3394 struct x_display_info *dpyinfo;
3396 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3398 if (dpyinfo->x_focus_frame)
3400 dpyinfo->x_highlight_frame
3401 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3402 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3403 : dpyinfo->x_focus_frame);
3404 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3406 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3407 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3410 else
3411 dpyinfo->x_highlight_frame = 0;
3413 if (dpyinfo->x_highlight_frame != old_highlight)
3415 if (old_highlight)
3416 frame_unhighlight (old_highlight);
3417 if (dpyinfo->x_highlight_frame)
3418 frame_highlight (dpyinfo->x_highlight_frame);
3424 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3426 /* Initialize mode_switch_bit and modifier_meaning. */
3427 static void
3428 x_find_modifier_meanings (dpyinfo)
3429 struct x_display_info *dpyinfo;
3431 int min_code, max_code;
3432 KeySym *syms;
3433 int syms_per_code;
3434 XModifierKeymap *mods;
3436 dpyinfo->meta_mod_mask = 0;
3437 dpyinfo->shift_lock_mask = 0;
3438 dpyinfo->alt_mod_mask = 0;
3439 dpyinfo->super_mod_mask = 0;
3440 dpyinfo->hyper_mod_mask = 0;
3442 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3444 syms = XGetKeyboardMapping (dpyinfo->display,
3445 min_code, max_code - min_code + 1,
3446 &syms_per_code);
3447 mods = XGetModifierMapping (dpyinfo->display);
3449 /* Scan the modifier table to see which modifier bits the Meta and
3450 Alt keysyms are on. */
3452 int row, col; /* The row and column in the modifier table. */
3453 int found_alt_or_meta;
3455 for (row = 3; row < 8; row++)
3457 found_alt_or_meta = 0;
3458 for (col = 0; col < mods->max_keypermod; col++)
3460 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3462 /* Zeroes are used for filler. Skip them. */
3463 if (code == 0)
3464 continue;
3466 /* Are any of this keycode's keysyms a meta key? */
3468 int code_col;
3470 for (code_col = 0; code_col < syms_per_code; code_col++)
3472 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3474 switch (sym)
3476 case XK_Meta_L:
3477 case XK_Meta_R:
3478 found_alt_or_meta = 1;
3479 dpyinfo->meta_mod_mask |= (1 << row);
3480 break;
3482 case XK_Alt_L:
3483 case XK_Alt_R:
3484 found_alt_or_meta = 1;
3485 dpyinfo->alt_mod_mask |= (1 << row);
3486 break;
3488 case XK_Hyper_L:
3489 case XK_Hyper_R:
3490 if (!found_alt_or_meta)
3491 dpyinfo->hyper_mod_mask |= (1 << row);
3492 code_col = syms_per_code;
3493 col = mods->max_keypermod;
3494 break;
3496 case XK_Super_L:
3497 case XK_Super_R:
3498 if (!found_alt_or_meta)
3499 dpyinfo->super_mod_mask |= (1 << row);
3500 code_col = syms_per_code;
3501 col = mods->max_keypermod;
3502 break;
3504 case XK_Shift_Lock:
3505 /* Ignore this if it's not on the lock modifier. */
3506 if (!found_alt_or_meta && ((1 << row) == LockMask))
3507 dpyinfo->shift_lock_mask = LockMask;
3508 code_col = syms_per_code;
3509 col = mods->max_keypermod;
3510 break;
3518 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3519 if (! dpyinfo->meta_mod_mask)
3521 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3522 dpyinfo->alt_mod_mask = 0;
3525 /* If some keys are both alt and meta,
3526 make them just meta, not alt. */
3527 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3529 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3532 XFree ((char *) syms);
3533 XFreeModifiermap (mods);
3536 /* Convert between the modifier bits X uses and the modifier bits
3537 Emacs uses. */
3539 unsigned int
3540 x_x_to_emacs_modifiers (dpyinfo, state)
3541 struct x_display_info *dpyinfo;
3542 unsigned int state;
3544 EMACS_UINT mod_meta = meta_modifier;
3545 EMACS_UINT mod_alt = alt_modifier;
3546 EMACS_UINT mod_hyper = hyper_modifier;
3547 EMACS_UINT mod_super = super_modifier;
3548 Lisp_Object tem;
3550 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3551 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3552 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3553 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3554 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3555 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3556 tem = Fget (Vx_super_keysym, Qmodifier_value);
3557 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3560 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3561 | ((state & ControlMask) ? ctrl_modifier : 0)
3562 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3563 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3564 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3565 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3568 static unsigned int
3569 x_emacs_to_x_modifiers (dpyinfo, state)
3570 struct x_display_info *dpyinfo;
3571 unsigned int state;
3573 EMACS_UINT mod_meta = meta_modifier;
3574 EMACS_UINT mod_alt = alt_modifier;
3575 EMACS_UINT mod_hyper = hyper_modifier;
3576 EMACS_UINT mod_super = super_modifier;
3578 Lisp_Object tem;
3580 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3581 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3582 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3583 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3584 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3585 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3586 tem = Fget (Vx_super_keysym, Qmodifier_value);
3587 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3590 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3591 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3592 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3593 | ((state & shift_modifier) ? ShiftMask : 0)
3594 | ((state & ctrl_modifier) ? ControlMask : 0)
3595 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3598 /* Convert a keysym to its name. */
3600 char *
3601 x_get_keysym_name (keysym)
3602 KeySym keysym;
3604 char *value;
3606 BLOCK_INPUT;
3607 value = XKeysymToString (keysym);
3608 UNBLOCK_INPUT;
3610 return value;
3615 /* Mouse clicks and mouse movement. Rah. */
3617 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3619 If the event is a button press, then note that we have grabbed
3620 the mouse. */
3622 static Lisp_Object
3623 construct_mouse_click (result, event, f)
3624 struct input_event *result;
3625 XButtonEvent *event;
3626 struct frame *f;
3628 /* Make the event type NO_EVENT; we'll change that when we decide
3629 otherwise. */
3630 result->kind = MOUSE_CLICK_EVENT;
3631 result->code = event->button - Button1;
3632 result->timestamp = event->time;
3633 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3634 event->state)
3635 | (event->type == ButtonRelease
3636 ? up_modifier
3637 : down_modifier));
3639 XSETINT (result->x, event->x);
3640 XSETINT (result->y, event->y);
3641 XSETFRAME (result->frame_or_window, f);
3642 result->arg = Qnil;
3643 return Qnil;
3647 /* Function to report a mouse movement to the mainstream Emacs code.
3648 The input handler calls this.
3650 We have received a mouse movement event, which is given in *event.
3651 If the mouse is over a different glyph than it was last time, tell
3652 the mainstream emacs code by setting mouse_moved. If not, ask for
3653 another motion event, so we can check again the next time it moves. */
3655 static XMotionEvent last_mouse_motion_event;
3656 static Lisp_Object last_mouse_motion_frame;
3658 static int
3659 note_mouse_movement (frame, event)
3660 FRAME_PTR frame;
3661 XMotionEvent *event;
3663 last_mouse_movement_time = event->time;
3664 last_mouse_motion_event = *event;
3665 XSETFRAME (last_mouse_motion_frame, frame);
3667 if (!FRAME_X_OUTPUT (frame))
3668 return 0;
3670 if (event->window != FRAME_X_WINDOW (frame))
3672 frame->mouse_moved = 1;
3673 last_mouse_scroll_bar = Qnil;
3674 note_mouse_highlight (frame, -1, -1);
3675 last_mouse_glyph_frame = 0;
3676 return 1;
3680 /* Has the mouse moved off the glyph it was on at the last sighting? */
3681 if (frame != last_mouse_glyph_frame
3682 || event->x < last_mouse_glyph.x
3683 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3684 || event->y < last_mouse_glyph.y
3685 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3687 frame->mouse_moved = 1;
3688 last_mouse_scroll_bar = Qnil;
3689 note_mouse_highlight (frame, event->x, event->y);
3690 /* Remember which glyph we're now on. */
3691 remember_mouse_glyph (frame, event->x, event->y, &last_mouse_glyph);
3692 last_mouse_glyph_frame = frame;
3693 return 1;
3696 return 0;
3700 /************************************************************************
3701 Mouse Face
3702 ************************************************************************/
3704 static void
3705 redo_mouse_highlight ()
3707 if (!NILP (last_mouse_motion_frame)
3708 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3709 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3710 last_mouse_motion_event.x,
3711 last_mouse_motion_event.y);
3716 /* Return the current position of the mouse.
3717 *FP should be a frame which indicates which display to ask about.
3719 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3720 and *PART to the frame, window, and scroll bar part that the mouse
3721 is over. Set *X and *Y to the portion and whole of the mouse's
3722 position on the scroll bar.
3724 If the mouse movement started elsewhere, set *FP to the frame the
3725 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3726 the mouse is over.
3728 Set *TIME to the server time-stamp for the time at which the mouse
3729 was at this position.
3731 Don't store anything if we don't have a valid set of values to report.
3733 This clears the mouse_moved flag, so we can wait for the next mouse
3734 movement. */
3736 static void
3737 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3738 FRAME_PTR *fp;
3739 int insist;
3740 Lisp_Object *bar_window;
3741 enum scroll_bar_part *part;
3742 Lisp_Object *x, *y;
3743 unsigned long *time;
3745 FRAME_PTR f1;
3747 BLOCK_INPUT;
3749 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3750 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3751 else
3753 Window root;
3754 int root_x, root_y;
3756 Window dummy_window;
3757 int dummy;
3759 Lisp_Object frame, tail;
3761 /* Clear the mouse-moved flag for every frame on this display. */
3762 FOR_EACH_FRAME (tail, frame)
3763 if (FRAME_X_P (XFRAME (frame))
3764 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3765 XFRAME (frame)->mouse_moved = 0;
3767 last_mouse_scroll_bar = Qnil;
3769 /* Figure out which root window we're on. */
3770 XQueryPointer (FRAME_X_DISPLAY (*fp),
3771 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3773 /* The root window which contains the pointer. */
3774 &root,
3776 /* Trash which we can't trust if the pointer is on
3777 a different screen. */
3778 &dummy_window,
3780 /* The position on that root window. */
3781 &root_x, &root_y,
3783 /* More trash we can't trust. */
3784 &dummy, &dummy,
3786 /* Modifier keys and pointer buttons, about which
3787 we don't care. */
3788 (unsigned int *) &dummy);
3790 /* Now we have a position on the root; find the innermost window
3791 containing the pointer. */
3793 Window win, child;
3794 int win_x, win_y;
3795 int parent_x = 0, parent_y = 0;
3797 win = root;
3799 /* XTranslateCoordinates can get errors if the window
3800 structure is changing at the same time this function
3801 is running. So at least we must not crash from them. */
3803 x_catch_errors (FRAME_X_DISPLAY (*fp));
3805 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3806 && FRAME_LIVE_P (last_mouse_frame))
3808 /* If mouse was grabbed on a frame, give coords for that frame
3809 even if the mouse is now outside it. */
3810 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3812 /* From-window, to-window. */
3813 root, FRAME_X_WINDOW (last_mouse_frame),
3815 /* From-position, to-position. */
3816 root_x, root_y, &win_x, &win_y,
3818 /* Child of win. */
3819 &child);
3820 f1 = last_mouse_frame;
3822 else
3824 while (1)
3826 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3828 /* From-window, to-window. */
3829 root, win,
3831 /* From-position, to-position. */
3832 root_x, root_y, &win_x, &win_y,
3834 /* Child of win. */
3835 &child);
3837 if (child == None || child == win)
3838 break;
3840 win = child;
3841 parent_x = win_x;
3842 parent_y = win_y;
3845 /* Now we know that:
3846 win is the innermost window containing the pointer
3847 (XTC says it has no child containing the pointer),
3848 win_x and win_y are the pointer's position in it
3849 (XTC did this the last time through), and
3850 parent_x and parent_y are the pointer's position in win's parent.
3851 (They are what win_x and win_y were when win was child.
3852 If win is the root window, it has no parent, and
3853 parent_{x,y} are invalid, but that's okay, because we'll
3854 never use them in that case.) */
3856 /* Is win one of our frames? */
3857 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3859 #ifdef USE_X_TOOLKIT
3860 /* If we end up with the menu bar window, say it's not
3861 on the frame. */
3862 if (f1 != NULL
3863 && f1->output_data.x->menubar_widget
3864 && win == XtWindow (f1->output_data.x->menubar_widget))
3865 f1 = NULL;
3866 #endif /* USE_X_TOOLKIT */
3869 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3870 f1 = 0;
3872 x_uncatch_errors ();
3874 /* If not, is it one of our scroll bars? */
3875 if (! f1)
3877 struct scroll_bar *bar;
3879 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
3881 if (bar)
3883 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3884 win_x = parent_x;
3885 win_y = parent_y;
3889 if (f1 == 0 && insist > 0)
3890 f1 = SELECTED_FRAME ();
3892 if (f1)
3894 /* Ok, we found a frame. Store all the values.
3895 last_mouse_glyph is a rectangle used to reduce the
3896 generation of mouse events. To not miss any motion
3897 events, we must divide the frame into rectangles of the
3898 size of the smallest character that could be displayed
3899 on it, i.e. into the same rectangles that matrices on
3900 the frame are divided into. */
3902 remember_mouse_glyph (f1, win_x, win_y, &last_mouse_glyph);
3903 last_mouse_glyph_frame = f1;
3905 *bar_window = Qnil;
3906 *part = 0;
3907 *fp = f1;
3908 XSETINT (*x, win_x);
3909 XSETINT (*y, win_y);
3910 *time = last_mouse_movement_time;
3915 UNBLOCK_INPUT;
3920 /***********************************************************************
3921 Scroll bars
3922 ***********************************************************************/
3924 /* Scroll bar support. */
3926 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
3927 manages it.
3928 This can be called in GC, so we have to make sure to strip off mark
3929 bits. */
3931 static struct scroll_bar *
3932 x_window_to_scroll_bar (display, window_id)
3933 Display *display;
3934 Window window_id;
3936 Lisp_Object tail;
3938 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3939 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
3940 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
3942 for (tail = Vframe_list;
3943 XGCTYPE (tail) == Lisp_Cons;
3944 tail = XCDR (tail))
3946 Lisp_Object frame, bar, condemned;
3948 frame = XCAR (tail);
3949 /* All elements of Vframe_list should be frames. */
3950 if (! GC_FRAMEP (frame))
3951 abort ();
3953 if (! FRAME_X_P (XFRAME (frame)))
3954 continue;
3956 /* Scan this frame's scroll bar list for a scroll bar with the
3957 right window ID. */
3958 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3959 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3960 /* This trick allows us to search both the ordinary and
3961 condemned scroll bar lists with one loop. */
3962 ! GC_NILP (bar) || (bar = condemned,
3963 condemned = Qnil,
3964 ! GC_NILP (bar));
3965 bar = XSCROLL_BAR (bar)->next)
3966 if (XSCROLL_BAR (bar)->x_window == window_id &&
3967 FRAME_X_DISPLAY (XFRAME (frame)) == display)
3968 return XSCROLL_BAR (bar);
3971 return 0;
3975 #if defined USE_LUCID
3977 /* Return the Lucid menu bar WINDOW is part of. Return null
3978 if WINDOW is not part of a menu bar. */
3980 static Widget
3981 x_window_to_menu_bar (window)
3982 Window window;
3984 Lisp_Object tail;
3986 for (tail = Vframe_list;
3987 XGCTYPE (tail) == Lisp_Cons;
3988 tail = XCDR (tail))
3990 if (FRAME_X_P (XFRAME (XCAR (tail))))
3992 Lisp_Object frame = XCAR (tail);
3993 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
3995 if (menu_bar && xlwmenu_window_p (menu_bar, window))
3996 return menu_bar;
4000 return NULL;
4003 #endif /* USE_LUCID */
4006 /************************************************************************
4007 Toolkit scroll bars
4008 ************************************************************************/
4010 #ifdef USE_TOOLKIT_SCROLL_BARS
4012 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
4013 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
4014 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
4015 struct scroll_bar *));
4016 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
4017 int, int, int));
4020 /* Lisp window being scrolled. Set when starting to interact with
4021 a toolkit scroll bar, reset to nil when ending the interaction. */
4023 static Lisp_Object window_being_scrolled;
4025 /* Last scroll bar part sent in xm_scroll_callback. */
4027 static int last_scroll_bar_part;
4029 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4030 that movements of 1/20 of the screen size are mapped to up/down. */
4032 #ifndef USE_GTK
4033 /* Id of action hook installed for scroll bars. */
4035 static XtActionHookId action_hook_id;
4037 static Boolean xaw3d_arrow_scroll;
4039 /* Whether the drag scrolling maintains the mouse at the top of the
4040 thumb. If not, resizing the thumb needs to be done more carefully
4041 to avoid jerkyness. */
4043 static Boolean xaw3d_pick_top;
4045 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4046 bars are used.. The hook is responsible for detecting when
4047 the user ends an interaction with the scroll bar, and generates
4048 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4050 static void
4051 xt_action_hook (widget, client_data, action_name, event, params,
4052 num_params)
4053 Widget widget;
4054 XtPointer client_data;
4055 String action_name;
4056 XEvent *event;
4057 String *params;
4058 Cardinal *num_params;
4060 int scroll_bar_p;
4061 char *end_action;
4063 #ifdef USE_MOTIF
4064 scroll_bar_p = XmIsScrollBar (widget);
4065 end_action = "Release";
4066 #else /* !USE_MOTIF i.e. use Xaw */
4067 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4068 end_action = "EndScroll";
4069 #endif /* USE_MOTIF */
4071 if (scroll_bar_p
4072 && strcmp (action_name, end_action) == 0
4073 && WINDOWP (window_being_scrolled))
4075 struct window *w;
4077 x_send_scroll_bar_event (window_being_scrolled,
4078 scroll_bar_end_scroll, 0, 0);
4079 w = XWINDOW (window_being_scrolled);
4081 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
4083 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4084 /* The thumb size is incorrect while dragging: fix it. */
4085 set_vertical_scroll_bar (w);
4087 window_being_scrolled = Qnil;
4088 last_scroll_bar_part = -1;
4090 /* Xt timeouts no longer needed. */
4091 toolkit_scroll_bar_interaction = 0;
4094 #endif /* not USE_GTK */
4096 /* A vector of windows used for communication between
4097 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4099 static struct window **scroll_bar_windows;
4100 static int scroll_bar_windows_size;
4103 /* Send a client message with message type Xatom_Scrollbar for a
4104 scroll action to the frame of WINDOW. PART is a value identifying
4105 the part of the scroll bar that was clicked on. PORTION is the
4106 amount to scroll of a whole of WHOLE. */
4108 static void
4109 x_send_scroll_bar_event (window, part, portion, whole)
4110 Lisp_Object window;
4111 int part, portion, whole;
4113 XEvent event;
4114 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4115 struct window *w = XWINDOW (window);
4116 struct frame *f = XFRAME (w->frame);
4117 int i;
4119 BLOCK_INPUT;
4121 /* Construct a ClientMessage event to send to the frame. */
4122 ev->type = ClientMessage;
4123 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4124 ev->display = FRAME_X_DISPLAY (f);
4125 ev->window = FRAME_X_WINDOW (f);
4126 ev->format = 32;
4128 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4129 not enough to store a pointer or Lisp_Object on a 64 bit system.
4130 So, store the window in scroll_bar_windows and pass the index
4131 into that array in the event. */
4132 for (i = 0; i < scroll_bar_windows_size; ++i)
4133 if (scroll_bar_windows[i] == NULL)
4134 break;
4136 if (i == scroll_bar_windows_size)
4138 int new_size = max (10, 2 * scroll_bar_windows_size);
4139 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4140 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4142 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4143 nbytes);
4144 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4145 scroll_bar_windows_size = new_size;
4148 scroll_bar_windows[i] = w;
4149 ev->data.l[0] = (long) i;
4150 ev->data.l[1] = (long) part;
4151 ev->data.l[2] = (long) 0;
4152 ev->data.l[3] = (long) portion;
4153 ev->data.l[4] = (long) whole;
4155 /* Make Xt timeouts work while the scroll bar is active. */
4156 toolkit_scroll_bar_interaction = 1;
4157 #ifdef USE_X_TOOLKIT
4158 x_activate_timeout_atimer ();
4159 #endif
4161 /* Setting the event mask to zero means that the message will
4162 be sent to the client that created the window, and if that
4163 window no longer exists, no event will be sent. */
4164 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4165 UNBLOCK_INPUT;
4169 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4170 in *IEVENT. */
4172 static void
4173 x_scroll_bar_to_input_event (event, ievent)
4174 XEvent *event;
4175 struct input_event *ievent;
4177 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4178 Lisp_Object window;
4179 struct frame *f;
4180 struct window *w;
4182 w = scroll_bar_windows[ev->data.l[0]];
4183 scroll_bar_windows[ev->data.l[0]] = NULL;
4185 XSETWINDOW (window, w);
4186 f = XFRAME (w->frame);
4188 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4189 ievent->frame_or_window = window;
4190 ievent->arg = Qnil;
4191 #ifdef USE_GTK
4192 ievent->timestamp = CurrentTime;
4193 #else
4194 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4195 #endif
4196 ievent->part = ev->data.l[1];
4197 ievent->code = ev->data.l[2];
4198 ievent->x = make_number ((int) ev->data.l[3]);
4199 ievent->y = make_number ((int) ev->data.l[4]);
4200 ievent->modifiers = 0;
4204 #ifdef USE_MOTIF
4206 /* Minimum and maximum values used for Motif scroll bars. */
4208 #define XM_SB_MAX 10000000
4211 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4212 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4213 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4215 static void
4216 xm_scroll_callback (widget, client_data, call_data)
4217 Widget widget;
4218 XtPointer client_data, call_data;
4220 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4221 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4222 int part = -1, whole = 0, portion = 0;
4224 switch (cs->reason)
4226 case XmCR_DECREMENT:
4227 bar->dragging = Qnil;
4228 part = scroll_bar_up_arrow;
4229 break;
4231 case XmCR_INCREMENT:
4232 bar->dragging = Qnil;
4233 part = scroll_bar_down_arrow;
4234 break;
4236 case XmCR_PAGE_DECREMENT:
4237 bar->dragging = Qnil;
4238 part = scroll_bar_above_handle;
4239 break;
4241 case XmCR_PAGE_INCREMENT:
4242 bar->dragging = Qnil;
4243 part = scroll_bar_below_handle;
4244 break;
4246 case XmCR_TO_TOP:
4247 bar->dragging = Qnil;
4248 part = scroll_bar_to_top;
4249 break;
4251 case XmCR_TO_BOTTOM:
4252 bar->dragging = Qnil;
4253 part = scroll_bar_to_bottom;
4254 break;
4256 case XmCR_DRAG:
4258 int slider_size;
4260 /* Get the slider size. */
4261 BLOCK_INPUT;
4262 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4263 UNBLOCK_INPUT;
4265 whole = XM_SB_MAX - slider_size;
4266 portion = min (cs->value, whole);
4267 part = scroll_bar_handle;
4268 bar->dragging = make_number (cs->value);
4270 break;
4272 case XmCR_VALUE_CHANGED:
4273 break;
4276 if (part >= 0)
4278 window_being_scrolled = bar->window;
4279 last_scroll_bar_part = part;
4280 x_send_scroll_bar_event (bar->window, part, portion, whole);
4284 #elif defined USE_GTK
4286 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4287 bar widget. DATA is a pointer to the scroll_bar structure. */
4289 static void
4290 xg_scroll_callback (widget, data)
4291 GtkRange *widget;
4292 gpointer data;
4294 struct scroll_bar *bar = (struct scroll_bar *) data;
4295 gdouble previous;
4296 gdouble position;
4297 gdouble *p;
4298 int diff;
4300 int part = -1, whole = 0, portion = 0;
4301 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (widget));
4303 position = gtk_adjustment_get_value (adj);
4305 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
4306 if (! p)
4308 p = (gdouble*) xmalloc (sizeof (gdouble));
4309 *p = XG_SB_MIN;
4310 g_object_set_data (G_OBJECT (widget), XG_LAST_SB_DATA, p);
4313 previous = *p;
4314 *p = position;
4316 if (xg_ignore_gtk_scrollbar) return;
4318 diff = (int) (position - previous);
4320 if (diff == (int) adj->step_increment)
4322 part = scroll_bar_down_arrow;
4323 bar->dragging = Qnil;
4325 else if (-diff == (int) adj->step_increment)
4327 part = scroll_bar_up_arrow;
4328 bar->dragging = Qnil;
4330 else if (diff == (int) adj->page_increment)
4332 part = scroll_bar_below_handle;
4333 bar->dragging = Qnil;
4335 else if (-diff == (int) adj->page_increment)
4337 part = scroll_bar_above_handle;
4338 bar->dragging = Qnil;
4340 else
4342 part = scroll_bar_handle;
4343 whole = adj->upper - adj->page_size;
4344 portion = min ((int)position, whole);
4345 bar->dragging = make_number ((int)portion);
4348 if (part >= 0)
4350 window_being_scrolled = bar->window;
4351 last_scroll_bar_part = part;
4352 x_send_scroll_bar_event (bar->window, part, portion, whole);
4356 #else /* not USE_GTK and not USE_MOTIF */
4358 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4359 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4360 scroll bar struct. CALL_DATA is a pointer to a float saying where
4361 the thumb is. */
4363 static void
4364 xaw_jump_callback (widget, client_data, call_data)
4365 Widget widget;
4366 XtPointer client_data, call_data;
4368 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4369 float top = *(float *) call_data;
4370 float shown;
4371 int whole, portion, height;
4372 int part;
4374 /* Get the size of the thumb, a value between 0 and 1. */
4375 BLOCK_INPUT;
4376 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4377 UNBLOCK_INPUT;
4379 whole = 10000000;
4380 portion = shown < 1 ? top * whole : 0;
4382 if (shown < 1 && (eabs (top + shown - 1) < 1.0/height))
4383 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4384 the bottom, so we force the scrolling whenever we see that we're
4385 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4386 we try to ensure that we always stay two pixels away from the
4387 bottom). */
4388 part = scroll_bar_down_arrow;
4389 else
4390 part = scroll_bar_handle;
4392 window_being_scrolled = bar->window;
4393 bar->dragging = make_number (portion);
4394 last_scroll_bar_part = part;
4395 x_send_scroll_bar_event (bar->window, part, portion, whole);
4399 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4400 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4401 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4402 the scroll bar. CALL_DATA is an integer specifying the action that
4403 has taken place. Its magnitude is in the range 0..height of the
4404 scroll bar. Negative values mean scroll towards buffer start.
4405 Values < height of scroll bar mean line-wise movement. */
4407 static void
4408 xaw_scroll_callback (widget, client_data, call_data)
4409 Widget widget;
4410 XtPointer client_data, call_data;
4412 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4413 /* The position really is stored cast to a pointer. */
4414 int position = (long) call_data;
4415 Dimension height;
4416 int part;
4418 /* Get the height of the scroll bar. */
4419 BLOCK_INPUT;
4420 XtVaGetValues (widget, XtNheight, &height, NULL);
4421 UNBLOCK_INPUT;
4423 if (eabs (position) >= height)
4424 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4426 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4427 it maps line-movement to call_data = max(5, height/20). */
4428 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
4429 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4430 else
4431 part = scroll_bar_move_ratio;
4433 window_being_scrolled = bar->window;
4434 bar->dragging = Qnil;
4435 last_scroll_bar_part = part;
4436 x_send_scroll_bar_event (bar->window, part, position, height);
4439 #endif /* not USE_GTK and not USE_MOTIF */
4441 #define SCROLL_BAR_NAME "verticalScrollBar"
4443 /* Create the widget for scroll bar BAR on frame F. Record the widget
4444 and X window of the scroll bar in BAR. */
4446 #ifdef USE_GTK
4447 static void
4448 x_create_toolkit_scroll_bar (f, bar)
4449 struct frame *f;
4450 struct scroll_bar *bar;
4452 char *scroll_bar_name = SCROLL_BAR_NAME;
4454 BLOCK_INPUT;
4455 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4456 scroll_bar_name);
4457 UNBLOCK_INPUT;
4460 #else /* not USE_GTK */
4462 static void
4463 x_create_toolkit_scroll_bar (f, bar)
4464 struct frame *f;
4465 struct scroll_bar *bar;
4467 Window xwindow;
4468 Widget widget;
4469 Arg av[20];
4470 int ac = 0;
4471 char *scroll_bar_name = SCROLL_BAR_NAME;
4472 unsigned long pixel;
4474 BLOCK_INPUT;
4476 #ifdef USE_MOTIF
4477 /* Set resources. Create the widget. */
4478 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4479 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4480 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4481 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4482 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4483 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4484 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4486 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4487 if (pixel != -1)
4489 XtSetArg (av[ac], XmNforeground, pixel);
4490 ++ac;
4493 pixel = f->output_data.x->scroll_bar_background_pixel;
4494 if (pixel != -1)
4496 XtSetArg (av[ac], XmNbackground, pixel);
4497 ++ac;
4500 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4501 scroll_bar_name, av, ac);
4503 /* Add one callback for everything that can happen. */
4504 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4505 (XtPointer) bar);
4506 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4507 (XtPointer) bar);
4508 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4509 (XtPointer) bar);
4510 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4511 (XtPointer) bar);
4512 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4513 (XtPointer) bar);
4514 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4515 (XtPointer) bar);
4516 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4517 (XtPointer) bar);
4519 /* Realize the widget. Only after that is the X window created. */
4520 XtRealizeWidget (widget);
4522 /* Set the cursor to an arrow. I didn't find a resource to do that.
4523 And I'm wondering why it hasn't an arrow cursor by default. */
4524 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4525 f->output_data.x->nontext_cursor);
4527 #else /* !USE_MOTIF i.e. use Xaw */
4529 /* Set resources. Create the widget. The background of the
4530 Xaw3d scroll bar widget is a little bit light for my taste.
4531 We don't alter it here to let users change it according
4532 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4533 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4534 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4535 /* For smoother scrolling with Xaw3d -sm */
4536 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4538 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4539 if (pixel != -1)
4541 XtSetArg (av[ac], XtNforeground, pixel);
4542 ++ac;
4545 pixel = f->output_data.x->scroll_bar_background_pixel;
4546 if (pixel != -1)
4548 XtSetArg (av[ac], XtNbackground, pixel);
4549 ++ac;
4552 /* Top/bottom shadow colors. */
4554 /* Allocate them, if necessary. */
4555 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4557 pixel = f->output_data.x->scroll_bar_background_pixel;
4558 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4559 &pixel, 1.2, 0x8000))
4560 pixel = -1;
4561 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4563 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4565 pixel = f->output_data.x->scroll_bar_background_pixel;
4566 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4567 &pixel, 0.6, 0x4000))
4568 pixel = -1;
4569 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4572 #ifdef XtNbeNiceToColormap
4573 /* Tell the toolkit about them. */
4574 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4575 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4576 /* We tried to allocate a color for the top/bottom shadow, and
4577 failed, so tell Xaw3d to use dithering instead. */
4579 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4580 ++ac;
4582 else
4583 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4584 be more consistent with other emacs 3d colors, and since Xaw3d is
4585 not good at dealing with allocation failure. */
4587 /* This tells Xaw3d to use real colors instead of dithering for
4588 the shadows. */
4589 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4590 ++ac;
4592 /* Specify the colors. */
4593 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4594 if (pixel != -1)
4596 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4597 ++ac;
4599 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4600 if (pixel != -1)
4602 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4603 ++ac;
4606 #endif
4608 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4609 f->output_data.x->edit_widget, av, ac);
4612 char *initial = "";
4613 char *val = initial;
4614 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4615 #ifdef XtNarrowScrollbars
4616 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4617 #endif
4618 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4619 if (xaw3d_arrow_scroll || val == initial)
4620 { /* ARROW_SCROLL */
4621 xaw3d_arrow_scroll = True;
4622 /* Isn't that just a personal preference ? --Stef */
4623 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4627 /* Define callbacks. */
4628 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4629 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4630 (XtPointer) bar);
4632 /* Realize the widget. Only after that is the X window created. */
4633 XtRealizeWidget (widget);
4635 #endif /* !USE_MOTIF */
4637 /* Install an action hook that lets us detect when the user
4638 finishes interacting with a scroll bar. */
4639 if (action_hook_id == 0)
4640 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4642 /* Remember X window and widget in the scroll bar vector. */
4643 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4644 xwindow = XtWindow (widget);
4645 bar->x_window = xwindow;
4647 UNBLOCK_INPUT;
4649 #endif /* not USE_GTK */
4652 /* Set the thumb size and position of scroll bar BAR. We are currently
4653 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4655 #ifdef USE_GTK
4656 static void
4657 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4658 struct scroll_bar *bar;
4659 int portion, position, whole;
4661 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4664 #else /* not USE_GTK */
4665 static void
4666 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4667 struct scroll_bar *bar;
4668 int portion, position, whole;
4670 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4671 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4672 float top, shown;
4674 BLOCK_INPUT;
4676 #ifdef USE_MOTIF
4678 /* We use an estimate of 30 chars per line rather than the real
4679 `portion' value. This has the disadvantage that the thumb size
4680 is not very representative, but it makes our life a lot easier.
4681 Otherwise, we have to constantly adjust the thumb size, which
4682 we can't always do quickly enough: while dragging, the size of
4683 the thumb might prevent the user from dragging the thumb all the
4684 way to the end. but Motif and some versions of Xaw3d don't allow
4685 updating the thumb size while dragging. Also, even if we can update
4686 its size, the update will often happen too late.
4687 If you don't believe it, check out revision 1.650 of xterm.c to see
4688 what hoops we were going through and the still poor behavior we got. */
4689 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4690 /* When the thumb is at the bottom, position == whole.
4691 So we need to increase `whole' to make space for the thumb. */
4692 whole += portion;
4694 if (whole <= 0)
4695 top = 0, shown = 1;
4696 else
4698 top = (float) position / whole;
4699 shown = (float) portion / whole;
4702 if (NILP (bar->dragging))
4704 int size, value;
4706 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4707 is the scroll bar's maximum and MIN is the scroll bar's minimum
4708 value. */
4709 size = shown * XM_SB_MAX;
4710 size = min (size, XM_SB_MAX);
4711 size = max (size, 1);
4713 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4714 value = top * XM_SB_MAX;
4715 value = min (value, XM_SB_MAX - size);
4717 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4719 #else /* !USE_MOTIF i.e. use Xaw */
4721 if (whole == 0)
4722 top = 0, shown = 1;
4723 else
4725 top = (float) position / whole;
4726 shown = (float) portion / whole;
4730 float old_top, old_shown;
4731 Dimension height;
4732 XtVaGetValues (widget,
4733 XtNtopOfThumb, &old_top,
4734 XtNshown, &old_shown,
4735 XtNheight, &height,
4736 NULL);
4738 /* Massage the top+shown values. */
4739 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4740 top = max (0, min (1, top));
4741 else
4742 top = old_top;
4743 /* Keep two pixels available for moving the thumb down. */
4744 shown = max (0, min (1 - top - (2.0 / height), shown));
4746 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4747 check that your system's configuration file contains a define
4748 for `NARROWPROTO'. See s/freebsd.h for an example. */
4749 if (top != old_top || shown != old_shown)
4751 if (NILP (bar->dragging))
4752 XawScrollbarSetThumb (widget, top, shown);
4753 else
4755 /* Try to make the scrolling a tad smoother. */
4756 if (!xaw3d_pick_top)
4757 shown = min (shown, old_shown);
4759 XawScrollbarSetThumb (widget, top, shown);
4763 #endif /* !USE_MOTIF */
4765 UNBLOCK_INPUT;
4767 #endif /* not USE_GTK */
4769 #endif /* USE_TOOLKIT_SCROLL_BARS */
4773 /************************************************************************
4774 Scroll bars, general
4775 ************************************************************************/
4777 /* Create a scroll bar and return the scroll bar vector for it. W is
4778 the Emacs window on which to create the scroll bar. TOP, LEFT,
4779 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4780 scroll bar. */
4782 static struct scroll_bar *
4783 x_scroll_bar_create (w, top, left, width, height)
4784 struct window *w;
4785 int top, left, width, height;
4787 struct frame *f = XFRAME (w->frame);
4788 struct scroll_bar *bar
4789 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
4791 BLOCK_INPUT;
4793 #ifdef USE_TOOLKIT_SCROLL_BARS
4794 x_create_toolkit_scroll_bar (f, bar);
4795 #else /* not USE_TOOLKIT_SCROLL_BARS */
4797 XSetWindowAttributes a;
4798 unsigned long mask;
4799 Window window;
4801 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4802 if (a.background_pixel == -1)
4803 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4805 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4806 | ButtonMotionMask | PointerMotionHintMask
4807 | ExposureMask);
4808 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4810 mask = (CWBackPixel | CWEventMask | CWCursor);
4812 /* Clear the area of W that will serve as a scroll bar. This is
4813 for the case that a window has been split horizontally. In
4814 this case, no clear_frame is generated to reduce flickering. */
4815 if (width > 0 && height > 0)
4816 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4817 left, top, width,
4818 window_box_height (w), False);
4820 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4821 /* Position and size of scroll bar. */
4822 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4823 top,
4824 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4825 height,
4826 /* Border width, depth, class, and visual. */
4828 CopyFromParent,
4829 CopyFromParent,
4830 CopyFromParent,
4831 /* Attributes. */
4832 mask, &a);
4833 bar->x_window = window;
4835 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4837 XSETWINDOW (bar->window, w);
4838 bar->top = top;
4839 bar->left = left;
4840 bar->width = width;
4841 bar->height = height;
4842 bar->start = 0;
4843 bar->end = 0;
4844 bar->dragging = Qnil;
4845 bar->fringe_extended_p = 0;
4847 /* Add bar to its frame's list of scroll bars. */
4848 bar->next = FRAME_SCROLL_BARS (f);
4849 bar->prev = Qnil;
4850 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4851 if (!NILP (bar->next))
4852 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4854 /* Map the window/widget. */
4855 #ifdef USE_TOOLKIT_SCROLL_BARS
4857 #ifdef USE_GTK
4858 xg_update_scrollbar_pos (f,
4859 bar->x_window,
4860 top,
4861 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4862 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4863 max (height, 1));
4864 xg_show_scroll_bar (bar->x_window);
4865 #else /* not USE_GTK */
4866 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4867 XtConfigureWidget (scroll_bar,
4868 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4869 top,
4870 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4871 max (height, 1), 0);
4872 XtMapWidget (scroll_bar);
4873 #endif /* not USE_GTK */
4875 #else /* not USE_TOOLKIT_SCROLL_BARS */
4876 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
4877 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4879 UNBLOCK_INPUT;
4880 return bar;
4884 #ifndef USE_TOOLKIT_SCROLL_BARS
4886 /* Draw BAR's handle in the proper position.
4888 If the handle is already drawn from START to END, don't bother
4889 redrawing it, unless REBUILD is non-zero; in that case, always
4890 redraw it. (REBUILD is handy for drawing the handle after expose
4891 events.)
4893 Normally, we want to constrain the start and end of the handle to
4894 fit inside its rectangle, but if the user is dragging the scroll
4895 bar handle, we want to let them drag it down all the way, so that
4896 the bar's top is as far down as it goes; otherwise, there's no way
4897 to move to the very end of the buffer. */
4899 static void
4900 x_scroll_bar_set_handle (bar, start, end, rebuild)
4901 struct scroll_bar *bar;
4902 int start, end;
4903 int rebuild;
4905 int dragging = ! NILP (bar->dragging);
4906 Window w = bar->x_window;
4907 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4908 GC gc = f->output_data.x->normal_gc;
4910 /* If the display is already accurate, do nothing. */
4911 if (! rebuild
4912 && start == bar->start
4913 && end == bar->end)
4914 return;
4916 BLOCK_INPUT;
4919 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
4920 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
4921 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
4923 /* Make sure the values are reasonable, and try to preserve
4924 the distance between start and end. */
4926 int length = end - start;
4928 if (start < 0)
4929 start = 0;
4930 else if (start > top_range)
4931 start = top_range;
4932 end = start + length;
4934 if (end < start)
4935 end = start;
4936 else if (end > top_range && ! dragging)
4937 end = top_range;
4940 /* Store the adjusted setting in the scroll bar. */
4941 bar->start = start;
4942 bar->end = end;
4944 /* Clip the end position, just for display. */
4945 if (end > top_range)
4946 end = top_range;
4948 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
4949 below top positions, to make sure the handle is always at least
4950 that many pixels tall. */
4951 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
4953 /* Draw the empty space above the handle. Note that we can't clear
4954 zero-height areas; that means "clear to end of window." */
4955 if (0 < start)
4956 x_clear_area (FRAME_X_DISPLAY (f), w,
4957 /* x, y, width, height, and exposures. */
4958 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4959 VERTICAL_SCROLL_BAR_TOP_BORDER,
4960 inside_width, start,
4961 False);
4963 /* Change to proper foreground color if one is specified. */
4964 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4965 XSetForeground (FRAME_X_DISPLAY (f), gc,
4966 f->output_data.x->scroll_bar_foreground_pixel);
4968 /* Draw the handle itself. */
4969 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
4970 /* x, y, width, height */
4971 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4972 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
4973 inside_width, end - start);
4975 /* Restore the foreground color of the GC if we changed it above. */
4976 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4977 XSetForeground (FRAME_X_DISPLAY (f), gc,
4978 FRAME_FOREGROUND_PIXEL (f));
4980 /* Draw the empty space below the handle. Note that we can't
4981 clear zero-height areas; that means "clear to end of window." */
4982 if (end < inside_height)
4983 x_clear_area (FRAME_X_DISPLAY (f), w,
4984 /* x, y, width, height, and exposures. */
4985 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4986 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
4987 inside_width, inside_height - end,
4988 False);
4992 UNBLOCK_INPUT;
4995 #endif /* !USE_TOOLKIT_SCROLL_BARS */
4997 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
4998 nil. */
5000 static void
5001 x_scroll_bar_remove (bar)
5002 struct scroll_bar *bar;
5004 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5005 BLOCK_INPUT;
5007 #ifdef USE_TOOLKIT_SCROLL_BARS
5008 #ifdef USE_GTK
5009 xg_remove_scroll_bar (f, bar->x_window);
5010 #else /* not USE_GTK */
5011 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5012 #endif /* not USE_GTK */
5013 #else
5014 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
5015 #endif
5017 /* Disassociate this scroll bar from its window. */
5018 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
5020 UNBLOCK_INPUT;
5024 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5025 that we are displaying PORTION characters out of a total of WHOLE
5026 characters, starting at POSITION. If WINDOW has no scroll bar,
5027 create one. */
5029 static void
5030 XTset_vertical_scroll_bar (w, portion, whole, position)
5031 struct window *w;
5032 int portion, whole, position;
5034 struct frame *f = XFRAME (w->frame);
5035 struct scroll_bar *bar;
5036 int top, height, left, sb_left, width, sb_width;
5037 int window_y, window_height;
5038 #ifdef USE_TOOLKIT_SCROLL_BARS
5039 int fringe_extended_p;
5040 #endif
5042 /* Get window dimensions. */
5043 window_box (w, -1, 0, &window_y, 0, &window_height);
5044 top = window_y;
5045 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5046 height = window_height;
5048 /* Compute the left edge of the scroll bar area. */
5049 left = WINDOW_SCROLL_BAR_AREA_X (w);
5051 /* Compute the width of the scroll bar which might be less than
5052 the width of the area reserved for the scroll bar. */
5053 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
5054 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
5055 else
5056 sb_width = width;
5058 /* Compute the left edge of the scroll bar. */
5059 #ifdef USE_TOOLKIT_SCROLL_BARS
5060 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5061 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
5062 else
5063 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
5064 #else
5065 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5066 sb_left = left + width - sb_width;
5067 else
5068 sb_left = left;
5069 #endif
5071 #ifdef USE_TOOLKIT_SCROLL_BARS
5072 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5073 fringe_extended_p = (WINDOW_LEFTMOST_P (w)
5074 && WINDOW_LEFT_FRINGE_WIDTH (w)
5075 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5076 || WINDOW_LEFT_MARGIN_COLS (w) == 0));
5077 else
5078 fringe_extended_p = (WINDOW_RIGHTMOST_P (w)
5079 && WINDOW_RIGHT_FRINGE_WIDTH (w)
5080 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5081 || WINDOW_RIGHT_MARGIN_COLS (w) == 0));
5082 #endif
5084 /* Does the scroll bar exist yet? */
5085 if (NILP (w->vertical_scroll_bar))
5087 if (width > 0 && height > 0)
5089 BLOCK_INPUT;
5090 #ifdef USE_TOOLKIT_SCROLL_BARS
5091 if (fringe_extended_p)
5092 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5093 sb_left, top, sb_width, height, False);
5094 else
5095 #endif
5096 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5097 left, top, width, height, False);
5098 UNBLOCK_INPUT;
5101 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5103 else
5105 /* It may just need to be moved and resized. */
5106 unsigned int mask = 0;
5108 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5110 BLOCK_INPUT;
5112 if (sb_left != bar->left)
5113 mask |= CWX;
5114 if (top != bar->top)
5115 mask |= CWY;
5116 if (sb_width != bar->width)
5117 mask |= CWWidth;
5118 if (height != bar->height)
5119 mask |= CWHeight;
5121 #ifdef USE_TOOLKIT_SCROLL_BARS
5123 /* Move/size the scroll bar widget. */
5124 if (mask || bar->fringe_extended_p != fringe_extended_p)
5126 /* Since toolkit scroll bars are smaller than the space reserved
5127 for them on the frame, we have to clear "under" them. */
5128 if (width > 0 && height > 0)
5130 if (fringe_extended_p)
5131 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5132 sb_left, top, sb_width, height, False);
5133 else
5134 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5135 left, top, width, height, False);
5137 #ifdef USE_GTK
5138 xg_update_scrollbar_pos (f,
5139 bar->x_window,
5140 top,
5141 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5142 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM *2,
5143 max (height, 1));
5144 #else /* not USE_GTK */
5145 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5146 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5147 top,
5148 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5149 max (height, 1), 0);
5150 #endif /* not USE_GTK */
5152 #else /* not USE_TOOLKIT_SCROLL_BARS */
5154 /* Clear areas not covered by the scroll bar because of
5155 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5156 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5158 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5159 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5160 height, False);
5161 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5162 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5163 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5164 height, False);
5167 /* Clear areas not covered by the scroll bar because it's not as
5168 wide as the area reserved for it. This makes sure a
5169 previous mode line display is cleared after C-x 2 C-x 1, for
5170 example. */
5172 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5173 int rest = area_width - sb_width;
5174 if (rest > 0 && height > 0)
5176 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5177 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5178 left + area_width - rest, top,
5179 rest, height, False);
5180 else
5181 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5182 left, top, rest, height, False);
5186 /* Move/size the scroll bar window. */
5187 if (mask)
5189 XWindowChanges wc;
5191 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5192 wc.y = top;
5193 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5194 wc.height = height;
5195 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5196 mask, &wc);
5199 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5201 /* Remember new settings. */
5202 bar->left = sb_left;
5203 bar->top = top;
5204 bar->width = sb_width;
5205 bar->height = height;
5207 UNBLOCK_INPUT;
5210 #ifdef USE_TOOLKIT_SCROLL_BARS
5211 bar->fringe_extended_p = fringe_extended_p;
5213 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5214 #else /* not USE_TOOLKIT_SCROLL_BARS */
5215 /* Set the scroll bar's current state, unless we're currently being
5216 dragged. */
5217 if (NILP (bar->dragging))
5219 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5221 if (whole == 0)
5222 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5223 else
5225 int start = ((double) position * top_range) / whole;
5226 int end = ((double) (position + portion) * top_range) / whole;
5227 x_scroll_bar_set_handle (bar, start, end, 0);
5230 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5232 XSETVECTOR (w->vertical_scroll_bar, bar);
5236 /* The following three hooks are used when we're doing a thorough
5237 redisplay of the frame. We don't explicitly know which scroll bars
5238 are going to be deleted, because keeping track of when windows go
5239 away is a real pain - "Can you say set-window-configuration, boys
5240 and girls?" Instead, we just assert at the beginning of redisplay
5241 that *all* scroll bars are to be removed, and then save a scroll bar
5242 from the fiery pit when we actually redisplay its window. */
5244 /* Arrange for all scroll bars on FRAME to be removed at the next call
5245 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5246 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5248 static void
5249 XTcondemn_scroll_bars (frame)
5250 FRAME_PTR frame;
5252 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5253 while (! NILP (FRAME_SCROLL_BARS (frame)))
5255 Lisp_Object bar;
5256 bar = FRAME_SCROLL_BARS (frame);
5257 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5258 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5259 XSCROLL_BAR (bar)->prev = Qnil;
5260 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5261 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5262 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5267 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5268 Note that WINDOW isn't necessarily condemned at all. */
5270 static void
5271 XTredeem_scroll_bar (window)
5272 struct window *window;
5274 struct scroll_bar *bar;
5275 struct frame *f;
5277 /* We can't redeem this window's scroll bar if it doesn't have one. */
5278 if (NILP (window->vertical_scroll_bar))
5279 abort ();
5281 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5283 /* Unlink it from the condemned list. */
5284 f = XFRAME (WINDOW_FRAME (window));
5285 if (NILP (bar->prev))
5287 /* If the prev pointer is nil, it must be the first in one of
5288 the lists. */
5289 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5290 /* It's not condemned. Everything's fine. */
5291 return;
5292 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5293 window->vertical_scroll_bar))
5294 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5295 else
5296 /* If its prev pointer is nil, it must be at the front of
5297 one or the other! */
5298 abort ();
5300 else
5301 XSCROLL_BAR (bar->prev)->next = bar->next;
5303 if (! NILP (bar->next))
5304 XSCROLL_BAR (bar->next)->prev = bar->prev;
5306 bar->next = FRAME_SCROLL_BARS (f);
5307 bar->prev = Qnil;
5308 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5309 if (! NILP (bar->next))
5310 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5313 /* Remove all scroll bars on FRAME that haven't been saved since the
5314 last call to `*condemn_scroll_bars_hook'. */
5316 static void
5317 XTjudge_scroll_bars (f)
5318 FRAME_PTR f;
5320 Lisp_Object bar, next;
5322 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5324 /* Clear out the condemned list now so we won't try to process any
5325 more events on the hapless scroll bars. */
5326 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5328 for (; ! NILP (bar); bar = next)
5330 struct scroll_bar *b = XSCROLL_BAR (bar);
5332 x_scroll_bar_remove (b);
5334 next = b->next;
5335 b->next = b->prev = Qnil;
5338 /* Now there should be no references to the condemned scroll bars,
5339 and they should get garbage-collected. */
5343 #ifndef USE_TOOLKIT_SCROLL_BARS
5344 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5345 is a no-op when using toolkit scroll bars.
5347 This may be called from a signal handler, so we have to ignore GC
5348 mark bits. */
5350 static void
5351 x_scroll_bar_expose (bar, event)
5352 struct scroll_bar *bar;
5353 XEvent *event;
5355 Window w = bar->x_window;
5356 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5357 GC gc = f->output_data.x->normal_gc;
5358 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5360 BLOCK_INPUT;
5362 x_scroll_bar_set_handle (bar, bar->start, bar->end, 1);
5364 /* Switch to scroll bar foreground color. */
5365 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5366 XSetForeground (FRAME_X_DISPLAY (f), gc,
5367 f->output_data.x->scroll_bar_foreground_pixel);
5369 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5370 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5372 /* x, y, width, height */
5373 0, 0,
5374 bar->width - 1 - width_trim - width_trim,
5375 bar->height - 1);
5377 /* Restore the foreground color of the GC if we changed it above. */
5378 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5379 XSetForeground (FRAME_X_DISPLAY (f), gc,
5380 FRAME_FOREGROUND_PIXEL (f));
5382 UNBLOCK_INPUT;
5385 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5387 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5388 is set to something other than NO_EVENT, it is enqueued.
5390 This may be called from a signal handler, so we have to ignore GC
5391 mark bits. */
5394 static void
5395 x_scroll_bar_handle_click (bar, event, emacs_event)
5396 struct scroll_bar *bar;
5397 XEvent *event;
5398 struct input_event *emacs_event;
5400 if (! GC_WINDOWP (bar->window))
5401 abort ();
5403 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5404 emacs_event->code = event->xbutton.button - Button1;
5405 emacs_event->modifiers
5406 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5407 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5408 event->xbutton.state)
5409 | (event->type == ButtonRelease
5410 ? up_modifier
5411 : down_modifier));
5412 emacs_event->frame_or_window = bar->window;
5413 emacs_event->arg = Qnil;
5414 emacs_event->timestamp = event->xbutton.time;
5416 #if 0
5417 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5418 int internal_height
5419 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5420 #endif
5421 int top_range
5422 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5423 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5425 if (y < 0) y = 0;
5426 if (y > top_range) y = top_range;
5428 if (y < bar->start)
5429 emacs_event->part = scroll_bar_above_handle;
5430 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5431 emacs_event->part = scroll_bar_handle;
5432 else
5433 emacs_event->part = scroll_bar_below_handle;
5435 /* Just because the user has clicked on the handle doesn't mean
5436 they want to drag it. Lisp code needs to be able to decide
5437 whether or not we're dragging. */
5438 #if 0
5439 /* If the user has just clicked on the handle, record where they're
5440 holding it. */
5441 if (event->type == ButtonPress
5442 && emacs_event->part == scroll_bar_handle)
5443 XSETINT (bar->dragging, y - bar->start);
5444 #endif
5446 #ifndef USE_TOOLKIT_SCROLL_BARS
5447 /* If the user has released the handle, set it to its final position. */
5448 if (event->type == ButtonRelease
5449 && ! NILP (bar->dragging))
5451 int new_start = y - XINT (bar->dragging);
5452 int new_end = new_start + bar->end - bar->start;
5454 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5455 bar->dragging = Qnil;
5457 #endif
5459 /* Same deal here as the other #if 0. */
5460 #if 0
5461 /* Clicks on the handle are always reported as occurring at the top of
5462 the handle. */
5463 if (emacs_event->part == scroll_bar_handle)
5464 emacs_event->x = bar->start;
5465 else
5466 XSETINT (emacs_event->x, y);
5467 #else
5468 XSETINT (emacs_event->x, y);
5469 #endif
5471 XSETINT (emacs_event->y, top_range);
5475 #ifndef USE_TOOLKIT_SCROLL_BARS
5477 /* Handle some mouse motion while someone is dragging the scroll bar.
5479 This may be called from a signal handler, so we have to ignore GC
5480 mark bits. */
5482 static void
5483 x_scroll_bar_note_movement (bar, event)
5484 struct scroll_bar *bar;
5485 XEvent *event;
5487 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5489 last_mouse_movement_time = event->xmotion.time;
5491 f->mouse_moved = 1;
5492 XSETVECTOR (last_mouse_scroll_bar, bar);
5494 /* If we're dragging the bar, display it. */
5495 if (! GC_NILP (bar->dragging))
5497 /* Where should the handle be now? */
5498 int new_start = event->xmotion.y - XINT (bar->dragging);
5500 if (new_start != bar->start)
5502 int new_end = new_start + bar->end - bar->start;
5504 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5509 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5511 /* Return information to the user about the current position of the mouse
5512 on the scroll bar. */
5514 static void
5515 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5516 FRAME_PTR *fp;
5517 Lisp_Object *bar_window;
5518 enum scroll_bar_part *part;
5519 Lisp_Object *x, *y;
5520 unsigned long *time;
5522 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5523 Window w = bar->x_window;
5524 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5525 int win_x, win_y;
5526 Window dummy_window;
5527 int dummy_coord;
5528 unsigned int dummy_mask;
5530 BLOCK_INPUT;
5532 /* Get the mouse's position relative to the scroll bar window, and
5533 report that. */
5534 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5536 /* Root, child, root x and root y. */
5537 &dummy_window, &dummy_window,
5538 &dummy_coord, &dummy_coord,
5540 /* Position relative to scroll bar. */
5541 &win_x, &win_y,
5543 /* Mouse buttons and modifier keys. */
5544 &dummy_mask))
5546 else
5548 #if 0
5549 int inside_height
5550 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5551 #endif
5552 int top_range
5553 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5555 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5557 if (! NILP (bar->dragging))
5558 win_y -= XINT (bar->dragging);
5560 if (win_y < 0)
5561 win_y = 0;
5562 if (win_y > top_range)
5563 win_y = top_range;
5565 *fp = f;
5566 *bar_window = bar->window;
5568 if (! NILP (bar->dragging))
5569 *part = scroll_bar_handle;
5570 else if (win_y < bar->start)
5571 *part = scroll_bar_above_handle;
5572 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5573 *part = scroll_bar_handle;
5574 else
5575 *part = scroll_bar_below_handle;
5577 XSETINT (*x, win_y);
5578 XSETINT (*y, top_range);
5580 f->mouse_moved = 0;
5581 last_mouse_scroll_bar = Qnil;
5584 *time = last_mouse_movement_time;
5586 UNBLOCK_INPUT;
5590 /* The screen has been cleared so we may have changed foreground or
5591 background colors, and the scroll bars may need to be redrawn.
5592 Clear out the scroll bars, and ask for expose events, so we can
5593 redraw them. */
5595 void
5596 x_scroll_bar_clear (f)
5597 FRAME_PTR f;
5599 #ifndef USE_TOOLKIT_SCROLL_BARS
5600 Lisp_Object bar;
5602 /* We can have scroll bars even if this is 0,
5603 if we just turned off scroll bar mode.
5604 But in that case we should not clear them. */
5605 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5606 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5607 bar = XSCROLL_BAR (bar)->next)
5608 XClearArea (FRAME_X_DISPLAY (f),
5609 XSCROLL_BAR (bar)->x_window,
5610 0, 0, 0, 0, True);
5611 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5615 /* The main X event-reading loop - XTread_socket. */
5617 #if 0
5618 /* Time stamp of enter window event. This is only used by XTread_socket,
5619 but we have to put it out here, since static variables within functions
5620 sometimes don't work. */
5622 static Time enter_timestamp;
5623 #endif
5625 /* This holds the state XLookupString needs to implement dead keys
5626 and other tricks known as "compose processing". _X Window System_
5627 says that a portable program can't use this, but Stephen Gildea assures
5628 me that letting the compiler initialize it to zeros will work okay.
5630 This must be defined outside of XTread_socket, for the same reasons
5631 given for enter_timestamp, above. */
5633 static XComposeStatus compose_status;
5635 /* Record the last 100 characters stored
5636 to help debug the loss-of-chars-during-GC problem. */
5638 static int temp_index;
5639 static short temp_buffer[100];
5641 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5642 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5643 temp_index = 0; \
5644 temp_buffer[temp_index++] = (keysym)
5646 /* Set this to nonzero to fake an "X I/O error"
5647 on a particular display. */
5649 struct x_display_info *XTread_socket_fake_io_error;
5651 /* When we find no input here, we occasionally do a no-op command
5652 to verify that the X server is still running and we can still talk with it.
5653 We try all the open displays, one by one.
5654 This variable is used for cycling thru the displays. */
5656 static struct x_display_info *next_noop_dpyinfo;
5658 #define SET_SAVED_MENU_EVENT(size) \
5659 do \
5661 if (f->output_data.x->saved_menu_event == 0) \
5662 f->output_data.x->saved_menu_event \
5663 = (XEvent *) xmalloc (sizeof (XEvent)); \
5664 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5665 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT; \
5666 XSETFRAME (inev.ie.frame_or_window, f); \
5668 while (0)
5670 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5671 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5674 enum
5676 X_EVENT_NORMAL,
5677 X_EVENT_GOTO_OUT,
5678 X_EVENT_DROP
5681 /* Filter events for the current X input method.
5682 DPYINFO is the display this event is for.
5683 EVENT is the X event to filter.
5685 Returns non-zero if the event was filtered, caller shall not process
5686 this event further.
5687 Returns zero if event is wasn't filtered. */
5689 #ifdef HAVE_X_I18N
5690 static int
5691 x_filter_event (dpyinfo, event)
5692 struct x_display_info *dpyinfo;
5693 XEvent *event;
5695 /* XFilterEvent returns non-zero if the input method has
5696 consumed the event. We pass the frame's X window to
5697 XFilterEvent because that's the one for which the IC
5698 was created. */
5700 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5701 event->xclient.window);
5703 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5705 #endif
5707 #ifdef USE_GTK
5708 static int current_count;
5709 static int current_finish;
5710 static struct input_event *current_hold_quit;
5712 /* This is the filter function invoked by the GTK event loop.
5713 It is invoked before the XEvent is translated to a GdkEvent,
5714 so we have a chance to act on the event before GTK. */
5715 static GdkFilterReturn
5716 event_handler_gdk (gxev, ev, data)
5717 GdkXEvent *gxev;
5718 GdkEvent *ev;
5719 gpointer data;
5721 XEvent *xev = (XEvent *) gxev;
5723 if (current_count >= 0)
5725 struct x_display_info *dpyinfo;
5727 dpyinfo = x_display_info_for_display (xev->xany.display);
5729 #ifdef HAVE_X_I18N
5730 /* Filter events for the current X input method.
5731 GTK calls XFilterEvent but not for key press and release,
5732 so we do it here. */
5733 if (xev->type == KeyPress || xev->type == KeyRelease)
5734 if (dpyinfo && x_filter_event (dpyinfo, xev))
5735 return GDK_FILTER_REMOVE;
5736 #endif
5738 if (! dpyinfo)
5739 current_finish = X_EVENT_NORMAL;
5740 else
5742 current_count +=
5743 handle_one_xevent (dpyinfo, xev, &current_finish,
5744 current_hold_quit);
5747 else
5748 current_finish = x_dispatch_event (xev, xev->xany.display);
5750 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5751 return GDK_FILTER_REMOVE;
5753 return GDK_FILTER_CONTINUE;
5755 #endif /* USE_GTK */
5758 /* Handles the XEvent EVENT on display DPYINFO.
5760 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5761 *FINISH is zero if caller should continue reading events.
5762 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5764 We return the number of characters stored into the buffer. */
5766 static int
5767 handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
5768 struct x_display_info *dpyinfo;
5769 XEvent *eventp;
5770 int *finish;
5771 struct input_event *hold_quit;
5773 union {
5774 struct input_event ie;
5775 struct selection_input_event sie;
5776 } inev;
5777 int count = 0;
5778 int do_help = 0;
5779 int nbytes = 0;
5780 struct frame *f = NULL;
5781 struct coding_system coding;
5782 XEvent event = *eventp;
5784 *finish = X_EVENT_NORMAL;
5786 EVENT_INIT (inev.ie);
5787 inev.ie.kind = NO_EVENT;
5788 inev.ie.arg = Qnil;
5790 switch (event.type)
5792 case ClientMessage:
5794 if (event.xclient.message_type
5795 == dpyinfo->Xatom_wm_protocols
5796 && event.xclient.format == 32)
5798 if (event.xclient.data.l[0]
5799 == dpyinfo->Xatom_wm_take_focus)
5801 /* Use x_any_window_to_frame because this
5802 could be the shell widget window
5803 if the frame has no title bar. */
5804 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5805 #ifdef HAVE_X_I18N
5806 /* Not quite sure this is needed -pd */
5807 if (f && FRAME_XIC (f))
5808 XSetICFocus (FRAME_XIC (f));
5809 #endif
5810 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5811 instructs the WM to set the input focus automatically for
5812 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5813 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5814 it has set the focus. So, XSetInputFocus below is not
5815 needed.
5817 The call to XSetInputFocus below has also caused trouble. In
5818 cases where the XSetInputFocus done by the WM and the one
5819 below are temporally close (on a fast machine), the call
5820 below can generate additional FocusIn events which confuse
5821 Emacs. */
5823 /* Since we set WM_TAKE_FOCUS, we must call
5824 XSetInputFocus explicitly. But not if f is null,
5825 since that might be an event for a deleted frame. */
5826 if (f)
5828 Display *d = event.xclient.display;
5829 /* Catch and ignore errors, in case window has been
5830 iconified by a window manager such as GWM. */
5831 x_catch_errors (d);
5832 XSetInputFocus (d, event.xclient.window,
5833 /* The ICCCM says this is
5834 the only valid choice. */
5835 RevertToParent,
5836 event.xclient.data.l[1]);
5837 /* This is needed to detect the error
5838 if there is an error. */
5839 XSync (d, False);
5840 x_uncatch_errors ();
5842 /* Not certain about handling scroll bars here */
5843 #endif /* 0 */
5844 goto done;
5847 if (event.xclient.data.l[0]
5848 == dpyinfo->Xatom_wm_save_yourself)
5850 /* Save state modify the WM_COMMAND property to
5851 something which can reinstate us. This notifies
5852 the session manager, who's looking for such a
5853 PropertyNotify. Can restart processing when
5854 a keyboard or mouse event arrives. */
5855 /* If we have a session manager, don't set this.
5856 KDE will then start two Emacsen, one for the
5857 session manager and one for this. */
5858 #ifdef HAVE_X_SM
5859 if (! x_session_have_connection ())
5860 #endif
5862 f = x_top_window_to_frame (dpyinfo,
5863 event.xclient.window);
5864 /* This is just so we only give real data once
5865 for a single Emacs process. */
5866 if (f == SELECTED_FRAME ())
5867 XSetCommand (FRAME_X_DISPLAY (f),
5868 event.xclient.window,
5869 initial_argv, initial_argc);
5870 else if (f)
5871 XSetCommand (FRAME_X_DISPLAY (f),
5872 event.xclient.window,
5873 0, 0);
5875 goto done;
5878 if (event.xclient.data.l[0]
5879 == dpyinfo->Xatom_wm_delete_window)
5881 f = x_any_window_to_frame (dpyinfo,
5882 event.xclient.window);
5883 if (!f)
5884 goto OTHER; /* May be a dialog that is to be removed */
5886 inev.ie.kind = DELETE_WINDOW_EVENT;
5887 XSETFRAME (inev.ie.frame_or_window, f);
5888 goto done;
5891 goto done;
5894 if (event.xclient.message_type
5895 == dpyinfo->Xatom_wm_configure_denied)
5897 goto done;
5900 if (event.xclient.message_type
5901 == dpyinfo->Xatom_wm_window_moved)
5903 int new_x, new_y;
5904 f = x_window_to_frame (dpyinfo, event.xclient.window);
5906 new_x = event.xclient.data.s[0];
5907 new_y = event.xclient.data.s[1];
5909 if (f)
5911 f->left_pos = new_x;
5912 f->top_pos = new_y;
5914 goto done;
5917 #ifdef HACK_EDITRES
5918 if (event.xclient.message_type
5919 == dpyinfo->Xatom_editres)
5921 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5922 if (f)
5923 _XEditResCheckMessages (f->output_data.x->widget, NULL,
5924 &event, NULL);
5925 goto done;
5927 #endif /* HACK_EDITRES */
5929 if ((event.xclient.message_type
5930 == dpyinfo->Xatom_DONE)
5931 || (event.xclient.message_type
5932 == dpyinfo->Xatom_PAGE))
5934 /* Ghostview job completed. Kill it. We could
5935 reply with "Next" if we received "Page", but we
5936 currently never do because we are interested in
5937 images, only, which should have 1 page. */
5938 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
5939 f = x_window_to_frame (dpyinfo, event.xclient.window);
5940 if (!f)
5941 goto OTHER;
5942 x_kill_gs_process (pixmap, f);
5943 expose_frame (f, 0, 0, 0, 0);
5944 goto done;
5947 #ifdef USE_TOOLKIT_SCROLL_BARS
5948 /* Scroll bar callbacks send a ClientMessage from which
5949 we construct an input_event. */
5950 if (event.xclient.message_type
5951 == dpyinfo->Xatom_Scrollbar)
5953 x_scroll_bar_to_input_event (&event, &inev.ie);
5954 *finish = X_EVENT_GOTO_OUT;
5955 goto done;
5957 #endif /* USE_TOOLKIT_SCROLL_BARS */
5959 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5960 if (!f)
5961 goto OTHER;
5962 if (x_handle_dnd_message (f, &event.xclient, dpyinfo, &inev.ie))
5963 *finish = X_EVENT_DROP;
5965 break;
5967 case SelectionNotify:
5968 last_user_time = event.xselection.time;
5969 #ifdef USE_X_TOOLKIT
5970 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
5971 goto OTHER;
5972 #endif /* not USE_X_TOOLKIT */
5973 x_handle_selection_notify (&event.xselection);
5974 break;
5976 case SelectionClear: /* Someone has grabbed ownership. */
5977 last_user_time = event.xselectionclear.time;
5978 #ifdef USE_X_TOOLKIT
5979 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
5980 goto OTHER;
5981 #endif /* USE_X_TOOLKIT */
5983 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
5985 inev.ie.kind = SELECTION_CLEAR_EVENT;
5986 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
5987 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
5988 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
5989 inev.ie.frame_or_window = Qnil;
5991 break;
5993 case SelectionRequest: /* Someone wants our selection. */
5994 last_user_time = event.xselectionrequest.time;
5995 #ifdef USE_X_TOOLKIT
5996 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
5997 goto OTHER;
5998 #endif /* USE_X_TOOLKIT */
6000 XSelectionRequestEvent *eventp
6001 = (XSelectionRequestEvent *) &event;
6003 inev.ie.kind = SELECTION_REQUEST_EVENT;
6004 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6005 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
6006 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6007 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
6008 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
6009 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6010 inev.ie.frame_or_window = Qnil;
6012 break;
6014 case PropertyNotify:
6015 last_user_time = event.xproperty.time;
6016 #if 0 /* This is plain wrong. In the case that we are waiting for a
6017 PropertyNotify used as an ACK in incremental selection
6018 transfer, the property will be on the receiver's window. */
6019 #if defined USE_X_TOOLKIT
6020 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
6021 goto OTHER;
6022 #endif
6023 #endif
6024 x_handle_property_notify (&event.xproperty);
6025 goto OTHER;
6027 case ReparentNotify:
6028 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
6029 if (f)
6031 int x, y;
6032 f->output_data.x->parent_desc = event.xreparent.parent;
6033 x_real_positions (f, &x, &y);
6034 f->left_pos = x;
6035 f->top_pos = y;
6037 /* Perhaps reparented due to a WM restart. Reset this. */
6038 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6039 FRAME_X_DISPLAY_INFO (f)->net_supported_window = 0;
6041 goto OTHER;
6043 case Expose:
6044 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6045 if (f)
6047 x_check_fullscreen (f);
6049 #ifdef USE_GTK
6050 /* This seems to be needed for GTK 2.6. */
6051 x_clear_area (event.xexpose.display,
6052 event.xexpose.window,
6053 event.xexpose.x, event.xexpose.y,
6054 event.xexpose.width, event.xexpose.height,
6055 FALSE);
6056 #endif
6057 if (f->async_visible == 0)
6059 f->async_visible = 1;
6060 f->async_iconified = 0;
6061 f->output_data.x->has_been_visible = 1;
6062 SET_FRAME_GARBAGED (f);
6064 else
6065 expose_frame (f,
6066 event.xexpose.x, event.xexpose.y,
6067 event.xexpose.width, event.xexpose.height);
6069 else
6071 #ifndef USE_TOOLKIT_SCROLL_BARS
6072 struct scroll_bar *bar;
6073 #endif
6074 #if defined USE_LUCID
6075 /* Submenus of the Lucid menu bar aren't widgets
6076 themselves, so there's no way to dispatch events
6077 to them. Recognize this case separately. */
6079 Widget widget
6080 = x_window_to_menu_bar (event.xexpose.window);
6081 if (widget)
6082 xlwmenu_redisplay (widget);
6084 #endif /* USE_LUCID */
6086 #ifdef USE_TOOLKIT_SCROLL_BARS
6087 /* Dispatch event to the widget. */
6088 goto OTHER;
6089 #else /* not USE_TOOLKIT_SCROLL_BARS */
6090 bar = x_window_to_scroll_bar (event.xexpose.display,
6091 event.xexpose.window);
6093 if (bar)
6094 x_scroll_bar_expose (bar, &event);
6095 #ifdef USE_X_TOOLKIT
6096 else
6097 goto OTHER;
6098 #endif /* USE_X_TOOLKIT */
6099 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6101 break;
6103 case GraphicsExpose: /* This occurs when an XCopyArea's
6104 source area was obscured or not
6105 available. */
6106 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6107 if (f)
6109 expose_frame (f,
6110 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6111 event.xgraphicsexpose.width,
6112 event.xgraphicsexpose.height);
6114 #ifdef USE_X_TOOLKIT
6115 else
6116 goto OTHER;
6117 #endif /* USE_X_TOOLKIT */
6118 break;
6120 case NoExpose: /* This occurs when an XCopyArea's
6121 source area was completely
6122 available. */
6123 break;
6125 case UnmapNotify:
6126 /* Redo the mouse-highlight after the tooltip has gone. */
6127 if (event.xmap.window == tip_window)
6129 tip_window = 0;
6130 redo_mouse_highlight ();
6133 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6134 if (f) /* F may no longer exist if
6135 the frame was deleted. */
6137 /* While a frame is unmapped, display generation is
6138 disabled; you don't want to spend time updating a
6139 display that won't ever be seen. */
6140 f->async_visible = 0;
6141 /* We can't distinguish, from the event, whether the window
6142 has become iconified or invisible. So assume, if it
6143 was previously visible, than now it is iconified.
6144 But x_make_frame_invisible clears both
6145 the visible flag and the iconified flag;
6146 and that way, we know the window is not iconified now. */
6147 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6149 f->async_iconified = 1;
6151 inev.ie.kind = ICONIFY_EVENT;
6152 XSETFRAME (inev.ie.frame_or_window, f);
6155 goto OTHER;
6157 case MapNotify:
6158 if (event.xmap.window == tip_window)
6159 /* The tooltip has been drawn already. Avoid
6160 the SET_FRAME_GARBAGED below. */
6161 goto OTHER;
6163 /* We use x_top_window_to_frame because map events can
6164 come for sub-windows and they don't mean that the
6165 frame is visible. */
6166 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6167 if (f)
6169 /* wait_reading_process_output will notice this and update
6170 the frame's display structures.
6171 If we where iconified, we should not set garbaged,
6172 because that stops redrawing on Expose events. This looks
6173 bad if we are called from a recursive event loop
6174 (x_dispatch_event), for example when a dialog is up. */
6175 if (! f->async_iconified)
6176 SET_FRAME_GARBAGED (f);
6178 f->async_visible = 1;
6179 f->async_iconified = 0;
6180 f->output_data.x->has_been_visible = 1;
6182 if (f->iconified)
6184 inev.ie.kind = DEICONIFY_EVENT;
6185 XSETFRAME (inev.ie.frame_or_window, f);
6187 else if (! NILP (Vframe_list)
6188 && ! NILP (XCDR (Vframe_list)))
6189 /* Force a redisplay sooner or later
6190 to update the frame titles
6191 in case this is the second frame. */
6192 record_asynch_buffer_change ();
6194 goto OTHER;
6196 case KeyPress:
6198 last_user_time = event.xkey.time;
6199 ignore_next_mouse_click_timeout = 0;
6201 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6202 /* Dispatch KeyPress events when in menu. */
6203 if (popup_activated ())
6204 goto OTHER;
6205 #endif
6207 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6209 /* If mouse-highlight is an integer, input clears out
6210 mouse highlighting. */
6211 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
6212 && (f == 0
6213 || !EQ (f->tool_bar_window, dpyinfo->mouse_face_window)))
6215 clear_mouse_face (dpyinfo);
6216 dpyinfo->mouse_face_hidden = 1;
6219 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6220 if (f == 0)
6222 /* Scroll bars consume key events, but we want
6223 the keys to go to the scroll bar's frame. */
6224 Widget widget = XtWindowToWidget (dpyinfo->display,
6225 event.xkey.window);
6226 if (widget && XmIsScrollBar (widget))
6228 widget = XtParent (widget);
6229 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6232 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6234 if (f != 0)
6236 KeySym keysym, orig_keysym;
6237 /* al%imercury@uunet.uu.net says that making this 81
6238 instead of 80 fixed a bug whereby meta chars made
6239 his Emacs hang.
6241 It seems that some version of XmbLookupString has
6242 a bug of not returning XBufferOverflow in
6243 status_return even if the input is too long to
6244 fit in 81 bytes. So, we must prepare sufficient
6245 bytes for copy_buffer. 513 bytes (256 chars for
6246 two-byte character set) seems to be a fairly good
6247 approximation. -- 2000.8.10 handa@etl.go.jp */
6248 unsigned char copy_buffer[513];
6249 unsigned char *copy_bufptr = copy_buffer;
6250 int copy_bufsiz = sizeof (copy_buffer);
6251 int modifiers;
6252 Lisp_Object coding_system = Qlatin_1;
6253 Lisp_Object c;
6255 #ifdef USE_GTK
6256 /* Don't pass keys to GTK. A Tab will shift focus to the
6257 tool bar in GTK 2.4. Keys will still go to menus and
6258 dialogs because in that case popup_activated is TRUE
6259 (see above). */
6260 *finish = X_EVENT_DROP;
6261 #endif
6263 event.xkey.state
6264 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6265 extra_keyboard_modifiers);
6266 modifiers = event.xkey.state;
6268 /* This will have to go some day... */
6270 /* make_lispy_event turns chars into control chars.
6271 Don't do it here because XLookupString is too eager. */
6272 event.xkey.state &= ~ControlMask;
6273 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6274 | dpyinfo->super_mod_mask
6275 | dpyinfo->hyper_mod_mask
6276 | dpyinfo->alt_mod_mask);
6278 /* In case Meta is ComposeCharacter,
6279 clear its status. According to Markus Ehrnsperger
6280 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6281 this enables ComposeCharacter to work whether or
6282 not it is combined with Meta. */
6283 if (modifiers & dpyinfo->meta_mod_mask)
6284 bzero (&compose_status, sizeof (compose_status));
6286 #ifdef HAVE_X_I18N
6287 if (FRAME_XIC (f))
6289 Status status_return;
6291 coding_system = Vlocale_coding_system;
6292 nbytes = XmbLookupString (FRAME_XIC (f),
6293 &event.xkey, copy_bufptr,
6294 copy_bufsiz, &keysym,
6295 &status_return);
6296 if (status_return == XBufferOverflow)
6298 copy_bufsiz = nbytes + 1;
6299 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6300 nbytes = XmbLookupString (FRAME_XIC (f),
6301 &event.xkey, copy_bufptr,
6302 copy_bufsiz, &keysym,
6303 &status_return);
6305 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6306 #if 0 && defined X_HAVE_UTF8_STRING
6307 else if (status_return == XLookupKeySym)
6308 { /* Try again but with utf-8. */
6309 coding_system = Qutf_8;
6310 nbytes = Xutf8LookupString (FRAME_XIC (f),
6311 &event.xkey, copy_bufptr,
6312 copy_bufsiz, &keysym,
6313 &status_return);
6314 if (status_return == XBufferOverflow)
6316 copy_bufsiz = nbytes + 1;
6317 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6318 nbytes = Xutf8LookupString (FRAME_XIC (f),
6319 &event.xkey,
6320 copy_bufptr,
6321 copy_bufsiz, &keysym,
6322 &status_return);
6325 #endif
6327 if (status_return == XLookupNone)
6328 break;
6329 else if (status_return == XLookupChars)
6331 keysym = NoSymbol;
6332 modifiers = 0;
6334 else if (status_return != XLookupKeySym
6335 && status_return != XLookupBoth)
6336 abort ();
6338 else
6339 nbytes = XLookupString (&event.xkey, copy_bufptr,
6340 copy_bufsiz, &keysym,
6341 &compose_status);
6342 #else
6343 nbytes = XLookupString (&event.xkey, copy_bufptr,
6344 copy_bufsiz, &keysym,
6345 &compose_status);
6346 #endif
6348 /* If not using XIM/XIC, and a compose sequence is in progress,
6349 we break here. Otherwise, chars_matched is always 0. */
6350 if (compose_status.chars_matched > 0 && nbytes == 0)
6351 break;
6353 bzero (&compose_status, sizeof (compose_status));
6354 orig_keysym = keysym;
6356 /* Common for all keysym input events. */
6357 XSETFRAME (inev.ie.frame_or_window, f);
6358 inev.ie.modifiers
6359 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), modifiers);
6360 inev.ie.timestamp = event.xkey.time;
6362 /* First deal with keysyms which have defined
6363 translations to characters. */
6364 if (keysym >= 32 && keysym < 128)
6365 /* Avoid explicitly decoding each ASCII character. */
6367 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6368 inev.ie.code = keysym;
6369 goto done_keysym;
6372 /* Keysyms directly mapped to supported Unicode characters. */
6373 if ((keysym >= 0x01000000 && keysym <= 0x010033ff)
6374 || (keysym >= 0x0100e000 && keysym <= 0x0100ffff))
6376 int code = keysym & 0xFFFF, charset_id, c1, c2;
6378 if (code < 0x80)
6380 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6381 inev.ie.code = code;
6383 else if (code < 0x100)
6385 if (code < 0xA0)
6386 charset_id = CHARSET_8_BIT_CONTROL;
6387 else
6388 charset_id = charset_latin_iso8859_1;
6389 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6390 inev.ie.code = MAKE_CHAR (charset_id, code, 0);
6392 else
6394 if (code < 0x2500)
6395 charset_id = charset_mule_unicode_0100_24ff,
6396 code -= 0x100;
6397 else if (code < 0xE000)
6398 charset_id = charset_mule_unicode_2500_33ff,
6399 code -= 0x2500;
6400 else
6401 charset_id = charset_mule_unicode_e000_ffff,
6402 code -= 0xE000;
6403 c1 = (code / 96) + 32, c2 = (code % 96) + 32;
6404 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6405 inev.ie.code = MAKE_CHAR (charset_id, c1, c2);
6407 goto done_keysym;
6410 /* Now non-ASCII. */
6411 if (HASH_TABLE_P (Vx_keysym_table)
6412 && (NATNUMP (c = Fgethash (make_number (keysym),
6413 Vx_keysym_table,
6414 Qnil))))
6416 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6417 ? ASCII_KEYSTROKE_EVENT
6418 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6419 inev.ie.code = XFASTINT (c);
6420 goto done_keysym;
6423 /* Random non-modifier sorts of keysyms. */
6424 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6425 || keysym == XK_Delete
6426 #ifdef XK_ISO_Left_Tab
6427 || (keysym >= XK_ISO_Left_Tab
6428 && keysym <= XK_ISO_Enter)
6429 #endif
6430 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6431 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6432 #ifdef HPUX
6433 /* This recognizes the "extended function
6434 keys". It seems there's no cleaner way.
6435 Test IsModifierKey to avoid handling
6436 mode_switch incorrectly. */
6437 || ((unsigned) (keysym) >= XK_Select
6438 && (unsigned)(keysym) < XK_KP_Space)
6439 #endif
6440 #ifdef XK_dead_circumflex
6441 || orig_keysym == XK_dead_circumflex
6442 #endif
6443 #ifdef XK_dead_grave
6444 || orig_keysym == XK_dead_grave
6445 #endif
6446 #ifdef XK_dead_tilde
6447 || orig_keysym == XK_dead_tilde
6448 #endif
6449 #ifdef XK_dead_diaeresis
6450 || orig_keysym == XK_dead_diaeresis
6451 #endif
6452 #ifdef XK_dead_macron
6453 || orig_keysym == XK_dead_macron
6454 #endif
6455 #ifdef XK_dead_degree
6456 || orig_keysym == XK_dead_degree
6457 #endif
6458 #ifdef XK_dead_acute
6459 || orig_keysym == XK_dead_acute
6460 #endif
6461 #ifdef XK_dead_cedilla
6462 || orig_keysym == XK_dead_cedilla
6463 #endif
6464 #ifdef XK_dead_breve
6465 || orig_keysym == XK_dead_breve
6466 #endif
6467 #ifdef XK_dead_ogonek
6468 || orig_keysym == XK_dead_ogonek
6469 #endif
6470 #ifdef XK_dead_caron
6471 || orig_keysym == XK_dead_caron
6472 #endif
6473 #ifdef XK_dead_doubleacute
6474 || orig_keysym == XK_dead_doubleacute
6475 #endif
6476 #ifdef XK_dead_abovedot
6477 || orig_keysym == XK_dead_abovedot
6478 #endif
6479 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6480 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6481 /* Any "vendor-specific" key is ok. */
6482 || (orig_keysym & (1 << 28))
6483 || (keysym != NoSymbol && nbytes == 0))
6484 && ! (IsModifierKey (orig_keysym)
6485 /* The symbols from XK_ISO_Lock
6486 to XK_ISO_Last_Group_Lock
6487 don't have real modifiers but
6488 should be treated similarly to
6489 Mode_switch by Emacs. */
6490 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6491 || ((unsigned)(orig_keysym)
6492 >= XK_ISO_Lock
6493 && (unsigned)(orig_keysym)
6494 <= XK_ISO_Last_Group_Lock)
6495 #endif
6498 STORE_KEYSYM_FOR_DEBUG (keysym);
6499 /* make_lispy_event will convert this to a symbolic
6500 key. */
6501 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6502 inev.ie.code = keysym;
6503 goto done_keysym;
6506 { /* Raw bytes, not keysym. */
6507 register int i;
6508 register int c;
6509 int nchars, len;
6511 /* The input should be decoded with `coding_system'
6512 which depends on which X*LookupString function
6513 we used just above and the locale. */
6514 setup_coding_system (coding_system, &coding);
6515 coding.src_multibyte = 0;
6516 coding.dst_multibyte = 1;
6517 /* The input is converted to events, thus we can't
6518 handle composition. Anyway, there's no XIM that
6519 gives us composition information. */
6520 coding.composing = COMPOSITION_DISABLED;
6522 if (nbytes > 0)
6524 /* Decode the input data. */
6525 int require;
6526 unsigned char *p;
6528 for (i = 0; i < nbytes; i++)
6530 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6533 require = decoding_buffer_size (&coding, nbytes);
6534 p = (unsigned char *) alloca (require);
6535 coding.mode |= CODING_MODE_LAST_BLOCK;
6536 /* We explicitly disable composition handling because
6537 key data should not contain any composition sequence. */
6538 coding.composing = COMPOSITION_DISABLED;
6539 decode_coding (&coding, copy_bufptr, p, nbytes, require);
6540 nbytes = coding.produced;
6541 nchars = coding.produced_char;
6542 copy_bufptr = p;
6544 /* Convert the input data to a sequence of
6545 character events. */
6546 for (i = 0; i < nbytes; i += len)
6548 if (nchars == nbytes)
6549 c = copy_bufptr[i], len = 1;
6550 else
6551 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
6552 nbytes - i, len);
6553 inev.ie.kind = (SINGLE_BYTE_CHAR_P (c)
6554 ? ASCII_KEYSTROKE_EVENT
6555 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6556 inev.ie.code = c;
6557 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6561 count += nchars;
6563 inev.ie.kind = NO_EVENT; /* Already stored above. */
6565 if (keysym == NoSymbol)
6566 break;
6569 done_keysym:
6570 #ifdef HAVE_X_I18N
6571 /* Don't dispatch this event since XtDispatchEvent calls
6572 XFilterEvent, and two calls in a row may freeze the
6573 client. */
6574 break;
6575 #else
6576 goto OTHER;
6577 #endif
6579 case KeyRelease:
6580 last_user_time = event.xkey.time;
6581 #ifdef HAVE_X_I18N
6582 /* Don't dispatch this event since XtDispatchEvent calls
6583 XFilterEvent, and two calls in a row may freeze the
6584 client. */
6585 break;
6586 #else
6587 goto OTHER;
6588 #endif
6590 case EnterNotify:
6591 last_user_time = event.xcrossing.time;
6592 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6594 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6596 if (f && x_mouse_click_focus_ignore_position)
6597 ignore_next_mouse_click_timeout = event.xmotion.time + 200;
6599 #if 0
6600 if (event.xcrossing.focus)
6602 /* Avoid nasty pop/raise loops. */
6603 if (f && (!(f->auto_raise)
6604 || !(f->auto_lower)
6605 || (event.xcrossing.time - enter_timestamp) > 500))
6607 x_new_focus_frame (dpyinfo, f);
6608 enter_timestamp = event.xcrossing.time;
6611 else if (f == dpyinfo->x_focus_frame)
6612 x_new_focus_frame (dpyinfo, 0);
6613 #endif
6615 /* EnterNotify counts as mouse movement,
6616 so update things that depend on mouse position. */
6617 if (f && !f->output_data.x->hourglass_p)
6618 note_mouse_movement (f, &event.xmotion);
6619 #ifdef USE_GTK
6620 /* We may get an EnterNotify on the buttons in the toolbar. In that
6621 case we moved out of any highlighted area and need to note this. */
6622 if (!f && last_mouse_glyph_frame)
6623 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6624 #endif
6625 goto OTHER;
6627 case FocusIn:
6628 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6629 goto OTHER;
6631 case LeaveNotify:
6632 last_user_time = event.xcrossing.time;
6633 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6635 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6636 if (f)
6638 if (f == dpyinfo->mouse_face_mouse_frame)
6640 /* If we move outside the frame, then we're
6641 certainly no longer on any text in the frame. */
6642 clear_mouse_face (dpyinfo);
6643 dpyinfo->mouse_face_mouse_frame = 0;
6646 /* Generate a nil HELP_EVENT to cancel a help-echo.
6647 Do it only if there's something to cancel.
6648 Otherwise, the startup message is cleared when
6649 the mouse leaves the frame. */
6650 if (any_help_event_p)
6651 do_help = -1;
6653 #ifdef USE_GTK
6654 /* See comment in EnterNotify above */
6655 else if (last_mouse_glyph_frame)
6656 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6657 #endif
6658 goto OTHER;
6660 case FocusOut:
6661 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6662 goto OTHER;
6664 case MotionNotify:
6666 last_user_time = event.xmotion.time;
6667 previous_help_echo_string = help_echo_string;
6668 help_echo_string = Qnil;
6670 if (dpyinfo->grabbed && last_mouse_frame
6671 && FRAME_LIVE_P (last_mouse_frame))
6672 f = last_mouse_frame;
6673 else
6674 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6676 if (dpyinfo->mouse_face_hidden)
6678 dpyinfo->mouse_face_hidden = 0;
6679 clear_mouse_face (dpyinfo);
6682 if (f)
6685 /* Generate SELECT_WINDOW_EVENTs when needed. */
6686 if (!NILP (Vmouse_autoselect_window))
6688 Lisp_Object window;
6690 window = window_from_coordinates (f,
6691 event.xmotion.x, event.xmotion.y,
6692 0, 0, 0, 0);
6694 /* Window will be selected only when it is not selected now and
6695 last mouse movement event was not in it. Minibuffer window
6696 will be selected only when it is active. */
6697 if (WINDOWP (window)
6698 && !EQ (window, last_window)
6699 && !EQ (window, selected_window)
6700 /* For click-to-focus window managers
6701 create event iff we don't leave the
6702 selected frame. */
6703 && (focus_follows_mouse
6704 || (EQ (XWINDOW (window)->frame,
6705 XWINDOW (selected_window)->frame))))
6707 inev.ie.kind = SELECT_WINDOW_EVENT;
6708 inev.ie.frame_or_window = window;
6711 last_window=window;
6713 if (!note_mouse_movement (f, &event.xmotion))
6714 help_echo_string = previous_help_echo_string;
6716 else
6718 #ifndef USE_TOOLKIT_SCROLL_BARS
6719 struct scroll_bar *bar
6720 = x_window_to_scroll_bar (event.xmotion.display,
6721 event.xmotion.window);
6723 if (bar)
6724 x_scroll_bar_note_movement (bar, &event);
6725 #endif /* USE_TOOLKIT_SCROLL_BARS */
6727 /* If we move outside the frame, then we're
6728 certainly no longer on any text in the frame. */
6729 clear_mouse_face (dpyinfo);
6732 /* If the contents of the global variable help_echo_string
6733 has changed, generate a HELP_EVENT. */
6734 if (!NILP (help_echo_string)
6735 || !NILP (previous_help_echo_string))
6736 do_help = 1;
6737 goto OTHER;
6740 case ConfigureNotify:
6741 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6742 if (f)
6744 #ifndef USE_X_TOOLKIT
6745 #ifdef USE_GTK
6746 xg_resize_widgets (f, event.xconfigure.width,
6747 event.xconfigure.height);
6748 #else /* not USE_GTK */
6749 /* If there is a pending resize for fullscreen, don't
6750 do this one, the right one will come later.
6751 The toolkit version doesn't seem to need this, but we
6752 need to reset it below. */
6753 int dont_resize
6754 = ((f->want_fullscreen & FULLSCREEN_WAIT)
6755 && f->new_text_cols != 0);
6756 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6757 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6759 if (dont_resize)
6760 goto OTHER;
6762 /* In the toolkit version, change_frame_size
6763 is called by the code that handles resizing
6764 of the EmacsFrame widget. */
6766 /* Even if the number of character rows and columns has
6767 not changed, the font size may have changed, so we need
6768 to check the pixel dimensions as well. */
6769 if (columns != FRAME_COLS (f)
6770 || rows != FRAME_LINES (f)
6771 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6772 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6774 change_frame_size (f, rows, columns, 0, 1, 0);
6775 SET_FRAME_GARBAGED (f);
6776 cancel_mouse_face (f);
6778 #endif /* not USE_GTK */
6779 #endif
6781 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6782 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
6784 #ifdef USE_GTK
6785 /* GTK creates windows but doesn't map them.
6786 Only get real positions and check fullscreen when mapped. */
6787 if (FRAME_GTK_OUTER_WIDGET (f)
6788 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6789 #endif
6791 x_real_positions (f, &f->left_pos, &f->top_pos);
6793 if (f->want_fullscreen & FULLSCREEN_WAIT)
6794 f->want_fullscreen &= ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
6797 #ifdef HAVE_X_I18N
6798 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6799 xic_set_statusarea (f);
6800 #endif
6802 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6804 /* Since the WM decorations come below top_pos now,
6805 we must put them below top_pos in the future. */
6806 f->win_gravity = NorthWestGravity;
6807 x_wm_set_size_hint (f, (long) 0, 0);
6810 goto OTHER;
6812 case ButtonRelease:
6813 case ButtonPress:
6815 /* If we decide we want to generate an event to be seen
6816 by the rest of Emacs, we put it here. */
6817 int tool_bar_p = 0;
6819 bzero (&compose_status, sizeof (compose_status));
6820 last_mouse_glyph_frame = 0;
6821 last_user_time = event.xbutton.time;
6823 if (dpyinfo->grabbed
6824 && last_mouse_frame
6825 && FRAME_LIVE_P (last_mouse_frame))
6826 f = last_mouse_frame;
6827 else
6828 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6830 if (f)
6832 /* Is this in the tool-bar? */
6833 if (WINDOWP (f->tool_bar_window)
6834 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6836 Lisp_Object window;
6837 int x = event.xbutton.x;
6838 int y = event.xbutton.y;
6840 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
6841 tool_bar_p = EQ (window, f->tool_bar_window);
6843 if (tool_bar_p && event.xbutton.button < 4)
6845 handle_tool_bar_click (f, x, y,
6846 event.xbutton.type == ButtonPress,
6847 x_x_to_emacs_modifiers (dpyinfo,
6848 event.xbutton.state));
6852 if (!tool_bar_p)
6853 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6854 if (! popup_activated ())
6855 #endif
6857 if (ignore_next_mouse_click_timeout)
6859 if (event.type == ButtonPress
6860 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
6862 ignore_next_mouse_click_timeout = 0;
6863 construct_mouse_click (&inev.ie, &event.xbutton, f);
6865 if (event.type == ButtonRelease)
6866 ignore_next_mouse_click_timeout = 0;
6868 else
6869 construct_mouse_click (&inev.ie, &event.xbutton, f);
6872 else
6874 struct scroll_bar *bar
6875 = x_window_to_scroll_bar (event.xbutton.display,
6876 event.xbutton.window);
6878 #ifdef USE_TOOLKIT_SCROLL_BARS
6879 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6880 scroll bars. */
6881 if (bar && event.xbutton.state & ControlMask)
6883 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6884 *finish = X_EVENT_DROP;
6886 #else /* not USE_TOOLKIT_SCROLL_BARS */
6887 if (bar)
6888 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6889 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6892 if (event.type == ButtonPress)
6894 dpyinfo->grabbed |= (1 << event.xbutton.button);
6895 last_mouse_frame = f;
6897 if (!tool_bar_p)
6898 last_tool_bar_item = -1;
6900 else
6901 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6903 /* Ignore any mouse motion that happened before this event;
6904 any subsequent mouse-movement Emacs events should reflect
6905 only motion after the ButtonPress/Release. */
6906 if (f != 0)
6907 f->mouse_moved = 0;
6909 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6910 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6911 /* For a down-event in the menu bar,
6912 don't pass it to Xt right now.
6913 Instead, save it away
6914 and we will pass it to Xt from kbd_buffer_get_event.
6915 That way, we can run some Lisp code first. */
6916 if (
6917 #ifdef USE_GTK
6918 ! popup_activated ()
6920 #endif
6921 f && event.type == ButtonPress
6922 /* Verify the event is really within the menu bar
6923 and not just sent to it due to grabbing. */
6924 && event.xbutton.x >= 0
6925 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
6926 && event.xbutton.y >= 0
6927 && event.xbutton.y < f->output_data.x->menubar_height
6928 && event.xbutton.same_screen)
6930 SET_SAVED_BUTTON_EVENT;
6931 XSETFRAME (last_mouse_press_frame, f);
6932 #ifdef USE_GTK
6933 *finish = X_EVENT_DROP;
6934 #endif
6936 else if (event.type == ButtonPress)
6938 last_mouse_press_frame = Qnil;
6939 goto OTHER;
6942 #ifdef USE_MOTIF /* This should do not harm for Lucid,
6943 but I am trying to be cautious. */
6944 else if (event.type == ButtonRelease)
6946 if (!NILP (last_mouse_press_frame))
6948 f = XFRAME (last_mouse_press_frame);
6949 if (f->output_data.x)
6950 SET_SAVED_BUTTON_EVENT;
6952 else
6953 goto OTHER;
6955 #endif /* USE_MOTIF */
6956 else
6957 goto OTHER;
6958 #endif /* USE_X_TOOLKIT || USE_GTK */
6960 break;
6962 case CirculateNotify:
6963 goto OTHER;
6965 case CirculateRequest:
6966 goto OTHER;
6968 case VisibilityNotify:
6969 goto OTHER;
6971 case MappingNotify:
6972 /* Someone has changed the keyboard mapping - update the
6973 local cache. */
6974 switch (event.xmapping.request)
6976 case MappingModifier:
6977 x_find_modifier_meanings (dpyinfo);
6978 /* This is meant to fall through. */
6979 case MappingKeyboard:
6980 XRefreshKeyboardMapping (&event.xmapping);
6982 goto OTHER;
6984 default:
6985 OTHER:
6986 #ifdef USE_X_TOOLKIT
6987 BLOCK_INPUT;
6988 if (*finish != X_EVENT_DROP)
6989 XtDispatchEvent (&event);
6990 UNBLOCK_INPUT;
6991 #endif /* USE_X_TOOLKIT */
6992 break;
6995 done:
6996 if (inev.ie.kind != NO_EVENT)
6998 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6999 count++;
7002 if (do_help
7003 && !(hold_quit && hold_quit->kind != NO_EVENT))
7005 Lisp_Object frame;
7007 if (f)
7008 XSETFRAME (frame, f);
7009 else
7010 frame = Qnil;
7012 if (do_help > 0)
7014 any_help_event_p = 1;
7015 gen_help_event (help_echo_string, frame, help_echo_window,
7016 help_echo_object, help_echo_pos);
7018 else
7020 help_echo_string = Qnil;
7021 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
7023 count++;
7026 *eventp = event;
7027 return count;
7031 /* Handles the XEvent EVENT on display DISPLAY.
7032 This is used for event loops outside the normal event handling,
7033 i.e. looping while a popup menu or a dialog is posted.
7035 Returns the value handle_one_xevent sets in the finish argument. */
7037 x_dispatch_event (event, display)
7038 XEvent *event;
7039 Display *display;
7041 struct x_display_info *dpyinfo;
7042 int finish = X_EVENT_NORMAL;
7044 dpyinfo = x_display_info_for_display (display);
7046 if (dpyinfo)
7047 handle_one_xevent (dpyinfo, event, &finish, 0);
7049 return finish;
7053 /* Read events coming from the X server.
7054 This routine is called by the SIGIO handler.
7055 We return as soon as there are no more events to be read.
7057 We return the number of characters stored into the buffer,
7058 thus pretending to be `read' (except the characters we store
7059 in the keyboard buffer can be multibyte, so are not necessarily
7060 C chars).
7062 EXPECTED is nonzero if the caller knows input is available. */
7064 static int
7065 XTread_socket (terminal, expected, hold_quit)
7066 struct terminal *terminal;
7067 int expected;
7068 struct input_event *hold_quit;
7070 int count = 0;
7071 XEvent event;
7072 int event_found = 0;
7073 struct x_display_info *dpyinfo;
7075 if (interrupt_input_blocked)
7077 interrupt_input_pending = 1;
7078 return -1;
7081 interrupt_input_pending = 0;
7082 BLOCK_INPUT;
7084 /* So people can tell when we have read the available input. */
7085 input_signal_count++;
7087 ++handling_signal;
7089 #ifdef HAVE_X_SM
7090 /* Only check session manager input for the primary display. */
7091 if (terminal->id == 1 && x_session_have_connection ())
7093 struct input_event inev;
7094 BLOCK_INPUT;
7095 /* We don't need to EVENT_INIT (inev) here, as
7096 x_session_check_input copies an entire input_event. */
7097 if (x_session_check_input (&inev))
7099 kbd_buffer_store_event_hold (&inev, hold_quit);
7100 count++;
7102 UNBLOCK_INPUT;
7104 #endif
7106 /* For debugging, this gives a way to fake an I/O error. */
7107 if (terminal->display_info.x == XTread_socket_fake_io_error)
7109 XTread_socket_fake_io_error = 0;
7110 x_io_error_quitter (dpyinfo->display);
7113 #if 0 /* This loop is a noop now. */
7114 /* Find the display we are supposed to read input for.
7115 It's the one communicating on descriptor SD. */
7116 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
7118 #if 0 /* This ought to be unnecessary; let's verify it. */
7119 #ifdef FIOSNBIO
7120 /* If available, Xlib uses FIOSNBIO to make the socket
7121 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
7122 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
7123 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7124 fcntl (dpyinfo->connection, F_SETFL, 0);
7125 #endif /* ! defined (FIOSNBIO) */
7126 #endif
7128 #if 0 /* This code can't be made to work, with multiple displays,
7129 and appears not to be used on any system any more.
7130 Also keyboard.c doesn't turn O_NDELAY on and off
7131 for X connections. */
7132 #ifndef SIGIO
7133 #ifndef HAVE_SELECT
7134 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
7136 extern int read_alarm_should_throw;
7137 read_alarm_should_throw = 1;
7138 XPeekEvent (dpyinfo->display, &event);
7139 read_alarm_should_throw = 0;
7141 #endif /* HAVE_SELECT */
7142 #endif /* SIGIO */
7143 #endif
7145 #endif
7147 #ifndef USE_GTK
7148 while (XPending (terminal->display_info.x->display))
7150 int finish;
7152 XNextEvent (terminal->display_info.x->display, &event);
7154 #ifdef HAVE_X_I18N
7155 /* Filter events for the current X input method. */
7156 if (x_filter_event (terminal->display_info.x, &event))
7157 break;
7158 #endif
7159 event_found = 1;
7161 count += handle_one_xevent (terminal->display_info.x,
7162 &event, &finish, hold_quit);
7164 if (finish == X_EVENT_GOTO_OUT)
7165 goto out;
7168 #else /* USE_GTK */
7170 /* For GTK we must use the GTK event loop. But XEvents gets passed
7171 to our filter function above, and then to the big event switch.
7172 We use a bunch of globals to communicate with our filter function,
7173 that is kind of ugly, but it works.
7175 There is no way to do one display at the time, GTK just does events
7176 from all displays. */
7178 while (gtk_events_pending ())
7180 current_count = count;
7181 current_hold_quit = hold_quit;
7183 gtk_main_iteration ();
7185 count = current_count;
7186 current_count = -1;
7187 current_hold_quit = 0;
7189 if (current_finish == X_EVENT_GOTO_OUT)
7190 break;
7192 #endif /* USE_GTK */
7194 out:;
7196 /* On some systems, an X bug causes Emacs to get no more events
7197 when the window is destroyed. Detect that. (1994.) */
7198 if (! event_found)
7200 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7201 One XNOOP in 100 loops will make Emacs terminate.
7202 B. Bretthauer, 1994 */
7203 x_noop_count++;
7204 if (x_noop_count >= 100)
7206 x_noop_count=0;
7208 if (next_noop_dpyinfo == 0)
7209 next_noop_dpyinfo = x_display_list;
7211 XNoOp (next_noop_dpyinfo->display);
7213 /* Each time we get here, cycle through the displays now open. */
7214 next_noop_dpyinfo = next_noop_dpyinfo->next;
7218 /* If the focus was just given to an auto-raising frame,
7219 raise it now. */
7220 /* ??? This ought to be able to handle more than one such frame. */
7221 if (pending_autoraise_frame)
7223 x_raise_frame (pending_autoraise_frame);
7224 pending_autoraise_frame = 0;
7227 --handling_signal;
7228 UNBLOCK_INPUT;
7230 return count;
7236 /***********************************************************************
7237 Text Cursor
7238 ***********************************************************************/
7240 /* Set clipping for output in glyph row ROW. W is the window in which
7241 we operate. GC is the graphics context to set clipping in.
7243 ROW may be a text row or, e.g., a mode line. Text rows must be
7244 clipped to the interior of the window dedicated to text display,
7245 mode lines must be clipped to the whole window. */
7247 static void
7248 x_clip_to_row (w, row, area, gc)
7249 struct window *w;
7250 struct glyph_row *row;
7251 int area;
7252 GC gc;
7254 struct frame *f = XFRAME (WINDOW_FRAME (w));
7255 XRectangle clip_rect;
7256 int window_x, window_y, window_width;
7258 window_box (w, area, &window_x, &window_y, &window_width, 0);
7260 clip_rect.x = window_x;
7261 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7262 clip_rect.y = max (clip_rect.y, window_y);
7263 clip_rect.width = window_width;
7264 clip_rect.height = row->visible_height;
7266 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7270 /* Draw a hollow box cursor on window W in glyph row ROW. */
7272 static void
7273 x_draw_hollow_cursor (w, row)
7274 struct window *w;
7275 struct glyph_row *row;
7277 struct frame *f = XFRAME (WINDOW_FRAME (w));
7278 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7279 Display *dpy = FRAME_X_DISPLAY (f);
7280 int x, y, wd, h;
7281 XGCValues xgcv;
7282 struct glyph *cursor_glyph;
7283 GC gc;
7285 /* Get the glyph the cursor is on. If we can't tell because
7286 the current matrix is invalid or such, give up. */
7287 cursor_glyph = get_phys_cursor_glyph (w);
7288 if (cursor_glyph == NULL)
7289 return;
7291 /* Compute frame-relative coordinates for phys cursor. */
7292 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7293 wd = w->phys_cursor_width;
7295 /* The foreground of cursor_gc is typically the same as the normal
7296 background color, which can cause the cursor box to be invisible. */
7297 xgcv.foreground = f->output_data.x->cursor_pixel;
7298 if (dpyinfo->scratch_cursor_gc)
7299 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7300 else
7301 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7302 GCForeground, &xgcv);
7303 gc = dpyinfo->scratch_cursor_gc;
7305 /* Set clipping, draw the rectangle, and reset clipping again. */
7306 x_clip_to_row (w, row, TEXT_AREA, gc);
7307 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7308 XSetClipMask (dpy, gc, None);
7312 /* Draw a bar cursor on window W in glyph row ROW.
7314 Implementation note: One would like to draw a bar cursor with an
7315 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7316 Unfortunately, I didn't find a font yet that has this property set.
7317 --gerd. */
7319 static void
7320 x_draw_bar_cursor (w, row, width, kind)
7321 struct window *w;
7322 struct glyph_row *row;
7323 int width;
7324 enum text_cursor_kinds kind;
7326 struct frame *f = XFRAME (w->frame);
7327 struct glyph *cursor_glyph;
7329 /* If cursor is out of bounds, don't draw garbage. This can happen
7330 in mini-buffer windows when switching between echo area glyphs
7331 and mini-buffer. */
7332 cursor_glyph = get_phys_cursor_glyph (w);
7333 if (cursor_glyph == NULL)
7334 return;
7336 /* If on an image, draw like a normal cursor. That's usually better
7337 visible than drawing a bar, esp. if the image is large so that
7338 the bar might not be in the window. */
7339 if (cursor_glyph->type == IMAGE_GLYPH)
7341 struct glyph_row *row;
7342 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7343 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7345 else
7347 Display *dpy = FRAME_X_DISPLAY (f);
7348 Window window = FRAME_X_WINDOW (f);
7349 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7350 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7351 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7352 XGCValues xgcv;
7354 /* If the glyph's background equals the color we normally draw
7355 the bar cursor in, the bar cursor in its normal color is
7356 invisible. Use the glyph's foreground color instead in this
7357 case, on the assumption that the glyph's colors are chosen so
7358 that the glyph is legible. */
7359 if (face->background == f->output_data.x->cursor_pixel)
7360 xgcv.background = xgcv.foreground = face->foreground;
7361 else
7362 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7363 xgcv.graphics_exposures = 0;
7365 if (gc)
7366 XChangeGC (dpy, gc, mask, &xgcv);
7367 else
7369 gc = XCreateGC (dpy, window, mask, &xgcv);
7370 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7373 if (width < 0)
7374 width = FRAME_CURSOR_WIDTH (f);
7375 width = min (cursor_glyph->pixel_width, width);
7377 w->phys_cursor_width = width;
7378 x_clip_to_row (w, row, TEXT_AREA, gc);
7380 if (kind == BAR_CURSOR)
7381 XFillRectangle (dpy, window, gc,
7382 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7383 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7384 width, row->height);
7385 else
7386 XFillRectangle (dpy, window, gc,
7387 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7388 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7389 row->height - width),
7390 cursor_glyph->pixel_width,
7391 width);
7393 XSetClipMask (dpy, gc, None);
7398 /* RIF: Define cursor CURSOR on frame F. */
7400 static void
7401 x_define_frame_cursor (f, cursor)
7402 struct frame *f;
7403 Cursor cursor;
7405 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7409 /* RIF: Clear area on frame F. */
7411 static void
7412 x_clear_frame_area (f, x, y, width, height)
7413 struct frame *f;
7414 int x, y, width, height;
7416 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7417 x, y, width, height, False);
7421 /* RIF: Draw cursor on window W. */
7423 static void
7424 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7425 struct window *w;
7426 struct glyph_row *glyph_row;
7427 int x, y;
7428 int cursor_type, cursor_width;
7429 int on_p, active_p;
7431 struct frame *f = XFRAME (WINDOW_FRAME (w));
7433 if (on_p)
7435 w->phys_cursor_type = cursor_type;
7436 w->phys_cursor_on_p = 1;
7438 if (glyph_row->exact_window_width_line_p
7439 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
7441 glyph_row->cursor_in_fringe_p = 1;
7442 draw_fringe_bitmap (w, glyph_row, 0);
7444 else
7445 switch (cursor_type)
7447 case HOLLOW_BOX_CURSOR:
7448 x_draw_hollow_cursor (w, glyph_row);
7449 break;
7451 case FILLED_BOX_CURSOR:
7452 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7453 break;
7455 case BAR_CURSOR:
7456 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7457 break;
7459 case HBAR_CURSOR:
7460 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7461 break;
7463 case NO_CURSOR:
7464 w->phys_cursor_width = 0;
7465 break;
7467 default:
7468 abort ();
7471 #ifdef HAVE_X_I18N
7472 if (w == XWINDOW (f->selected_window))
7473 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7474 xic_set_preeditarea (w, x, y);
7475 #endif
7478 #ifndef XFlush
7479 XFlush (FRAME_X_DISPLAY (f));
7480 #endif
7484 /* Icons. */
7486 /* Make the x-window of frame F use the gnu icon bitmap. */
7489 x_bitmap_icon (f, file)
7490 struct frame *f;
7491 Lisp_Object file;
7493 int bitmap_id;
7495 if (FRAME_X_WINDOW (f) == 0)
7496 return 1;
7498 /* Free up our existing icon bitmap and mask if any. */
7499 if (f->output_data.x->icon_bitmap > 0)
7500 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7501 f->output_data.x->icon_bitmap = 0;
7503 if (STRINGP (file))
7505 #ifdef USE_GTK
7506 /* Use gtk_window_set_icon_from_file () if available,
7507 It's not restricted to bitmaps */
7508 if (xg_set_icon (f, file))
7509 return 0;
7510 #endif /* USE_GTK */
7511 bitmap_id = x_create_bitmap_from_file (f, file);
7512 x_create_bitmap_mask (f, bitmap_id);
7514 else
7516 /* Create the GNU bitmap and mask if necessary. */
7517 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7519 int rc = -1;
7521 #if defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7522 #ifdef USE_GTK
7523 if (xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7524 return 0;
7525 #else
7526 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7527 if (rc != -1)
7528 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7529 #endif /* USE_GTK */
7530 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
7532 /* If all else fails, use the (black and white) xbm image. */
7533 if (rc == -1)
7535 rc = x_create_bitmap_from_data (f, gnu_xbm_bits,
7536 gnu_xbm_width, gnu_xbm_height);
7537 if (rc == -1)
7538 return 1;
7540 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7541 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7545 /* The first time we create the GNU bitmap and mask,
7546 this increments the ref-count one extra time.
7547 As a result, the GNU bitmap and mask are never freed.
7548 That way, we don't have to worry about allocating it again. */
7549 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7551 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7554 x_wm_set_icon_pixmap (f, bitmap_id);
7555 f->output_data.x->icon_bitmap = bitmap_id;
7557 return 0;
7561 /* Make the x-window of frame F use a rectangle with text.
7562 Use ICON_NAME as the text. */
7565 x_text_icon (f, icon_name)
7566 struct frame *f;
7567 char *icon_name;
7569 if (FRAME_X_WINDOW (f) == 0)
7570 return 1;
7573 XTextProperty text;
7574 text.value = (unsigned char *) icon_name;
7575 text.encoding = XA_STRING;
7576 text.format = 8;
7577 text.nitems = strlen (icon_name);
7578 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7581 if (f->output_data.x->icon_bitmap > 0)
7582 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7583 f->output_data.x->icon_bitmap = 0;
7584 x_wm_set_icon_pixmap (f, 0);
7586 return 0;
7589 #define X_ERROR_MESSAGE_SIZE 200
7591 /* If non-nil, this should be a string.
7592 It means catch X errors and store the error message in this string.
7594 The reason we use a stack is that x_catch_error/x_uncatch_error can
7595 be called from a signal handler.
7598 struct x_error_message_stack {
7599 char string[X_ERROR_MESSAGE_SIZE];
7600 Display *dpy;
7601 struct x_error_message_stack *prev;
7603 static struct x_error_message_stack *x_error_message;
7605 /* An X error handler which stores the error message in
7606 *x_error_message. This is called from x_error_handler if
7607 x_catch_errors is in effect. */
7609 static void
7610 x_error_catcher (display, error)
7611 Display *display;
7612 XErrorEvent *error;
7614 XGetErrorText (display, error->error_code,
7615 x_error_message->string,
7616 X_ERROR_MESSAGE_SIZE);
7619 /* Begin trapping X errors for display DPY. Actually we trap X errors
7620 for all displays, but DPY should be the display you are actually
7621 operating on.
7623 After calling this function, X protocol errors no longer cause
7624 Emacs to exit; instead, they are recorded in the string
7625 stored in *x_error_message.
7627 Calling x_check_errors signals an Emacs error if an X error has
7628 occurred since the last call to x_catch_errors or x_check_errors.
7630 Calling x_uncatch_errors resumes the normal error handling. */
7632 void x_check_errors ();
7634 void
7635 x_catch_errors (dpy)
7636 Display *dpy;
7638 struct x_error_message_stack *data = xmalloc (sizeof (*data));
7640 /* Make sure any errors from previous requests have been dealt with. */
7641 XSync (dpy, False);
7643 data->dpy = dpy;
7644 data->string[0] = 0;
7645 data->prev = x_error_message;
7646 x_error_message = data;
7649 /* Undo the last x_catch_errors call.
7650 DPY should be the display that was passed to x_catch_errors. */
7652 void
7653 x_uncatch_errors ()
7655 struct x_error_message_stack *tmp;
7657 BLOCK_INPUT;
7659 /* The display may have been closed before this function is called.
7660 Check if it is still open before calling XSync. */
7661 if (x_display_info_for_display (x_error_message->dpy) != 0)
7662 XSync (x_error_message->dpy, False);
7664 tmp = x_error_message;
7665 x_error_message = x_error_message->prev;
7666 xfree (tmp);
7667 UNBLOCK_INPUT;
7670 /* If any X protocol errors have arrived since the last call to
7671 x_catch_errors or x_check_errors, signal an Emacs error using
7672 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7674 void
7675 x_check_errors (dpy, format)
7676 Display *dpy;
7677 char *format;
7679 /* Make sure to catch any errors incurred so far. */
7680 XSync (dpy, False);
7682 if (x_error_message->string[0])
7684 char string[X_ERROR_MESSAGE_SIZE];
7685 bcopy (x_error_message->string, string, X_ERROR_MESSAGE_SIZE);
7686 x_uncatch_errors ();
7687 error (format, string);
7691 /* Nonzero if we had any X protocol errors
7692 since we did x_catch_errors on DPY. */
7695 x_had_errors_p (dpy)
7696 Display *dpy;
7698 /* Make sure to catch any errors incurred so far. */
7699 XSync (dpy, False);
7701 return x_error_message->string[0] != 0;
7704 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7706 void
7707 x_clear_errors (dpy)
7708 Display *dpy;
7710 x_error_message->string[0] = 0;
7713 #if 0 /* See comment in unwind_to_catch why calling this is a bad
7714 * idea. --lorentey */
7715 /* Close off all unclosed x_catch_errors calls. */
7717 void
7718 x_fully_uncatch_errors ()
7720 while (x_error_message)
7721 x_uncatch_errors ();
7723 #endif
7725 /* Nonzero if x_catch_errors has been done and not yet canceled. */
7728 x_catching_errors ()
7730 return x_error_message != 0;
7733 #if 0
7734 static unsigned int x_wire_count;
7735 x_trace_wire ()
7737 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7739 #endif /* ! 0 */
7742 /* Handle SIGPIPE, which can happen when the connection to a server
7743 simply goes away. SIGPIPE is handled by x_connection_signal.
7744 Don't need to do anything, because the write which caused the
7745 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7746 which will do the appropriate cleanup for us. */
7748 static SIGTYPE
7749 x_connection_signal (signalnum) /* If we don't have an argument, */
7750 int signalnum; /* some compilers complain in signal calls. */
7752 #ifdef USG
7753 /* USG systems forget handlers when they are used;
7754 must reestablish each time */
7755 signal (signalnum, x_connection_signal);
7756 #endif /* USG */
7760 /************************************************************************
7761 Handling X errors
7762 ************************************************************************/
7764 /* Error message passed to x_connection_closed. */
7766 static char *error_msg;
7768 /* Function installed as fatal_error_signal_hook in
7769 x_connection_closed. Print the X error message, and exit normally,
7770 instead of dumping core when XtCloseDisplay fails. */
7772 static void
7773 x_fatal_error_signal ()
7775 fprintf (stderr, "%s\n", error_msg);
7776 exit (70);
7779 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7780 the text of an error message that lead to the connection loss. */
7782 static SIGTYPE
7783 x_connection_closed (dpy, error_message)
7784 Display *dpy;
7785 char *error_message;
7787 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7788 Lisp_Object frame, tail;
7789 int index = SPECPDL_INDEX ();
7791 error_msg = (char *) alloca (strlen (error_message) + 1);
7792 strcpy (error_msg, error_message);
7793 handling_signal = 0;
7795 /* Prevent being called recursively because of an error condition
7796 below. Otherwise, we might end up with printing ``can't find per
7797 display information'' in the recursive call instead of printing
7798 the original message here. */
7799 x_catch_errors (dpy);
7801 /* Inhibit redisplay while frames are being deleted. */
7802 specbind (Qinhibit_redisplay, Qt);
7804 if (dpyinfo)
7806 /* Protect display from being closed when we delete the last
7807 frame on it. */
7808 dpyinfo->reference_count++;
7809 dpyinfo->terminal->reference_count++;
7812 /* First delete frames whose mini-buffers are on frames
7813 that are on the dead display. */
7814 FOR_EACH_FRAME (tail, frame)
7816 Lisp_Object minibuf_frame;
7817 minibuf_frame
7818 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7819 if (FRAME_X_P (XFRAME (frame))
7820 && FRAME_X_P (XFRAME (minibuf_frame))
7821 && ! EQ (frame, minibuf_frame)
7822 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7823 Fdelete_frame (frame, Qt);
7826 /* Now delete all remaining frames on the dead display.
7827 We are now sure none of these is used as the mini-buffer
7828 for another frame that we need to delete. */
7829 FOR_EACH_FRAME (tail, frame)
7830 if (FRAME_X_P (XFRAME (frame))
7831 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7833 /* Set this to t so that Fdelete_frame won't get confused
7834 trying to find a replacement. */
7835 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7836 Fdelete_frame (frame, Qt);
7839 /* We have to close the display to inform Xt that it doesn't
7840 exist anymore. If we don't, Xt will continue to wait for
7841 events from the display. As a consequence, a sequence of
7843 M-x make-frame-on-display RET :1 RET
7844 ...kill the new frame, so that we get an IO error...
7845 M-x make-frame-on-display RET :1 RET
7847 will indefinitely wait in Xt for events for display `:1', opened
7848 in the first class to make-frame-on-display.
7850 Closing the display is reported to lead to a bus error on
7851 OpenWindows in certain situations. I suspect that is a bug
7852 in OpenWindows. I don't know how to circumvent it here. */
7854 #ifdef USE_X_TOOLKIT
7855 /* If DPYINFO is null, this means we didn't open the display
7856 in the first place, so don't try to close it. */
7857 if (dpyinfo)
7859 extern void (*fatal_error_signal_hook) P_ ((void));
7860 fatal_error_signal_hook = x_fatal_error_signal;
7861 XtCloseDisplay (dpy);
7862 fatal_error_signal_hook = NULL;
7864 #endif
7866 #ifdef USE_GTK
7867 if (dpyinfo)
7868 xg_display_close (dpyinfo->display);
7869 #endif
7871 if (dpyinfo)
7873 /* Indicate that this display is dead. */
7874 dpyinfo->display = 0;
7876 dpyinfo->reference_count--;
7877 dpyinfo->terminal->reference_count--;
7878 if (dpyinfo->reference_count != 0)
7879 /* We have just closed all frames on this display. */
7880 abort ();
7882 x_delete_display (dpyinfo);
7885 x_uncatch_errors ();
7887 if (terminal_list == 0)
7889 fprintf (stderr, "%s\n", error_msg);
7890 shut_down_emacs (0, 0, Qnil);
7891 exit (70);
7894 /* Ordinary stack unwind doesn't deal with these. */
7895 #ifdef SIGIO
7896 sigunblock (sigmask (SIGIO));
7897 #endif
7898 sigunblock (sigmask (SIGALRM));
7899 TOTALLY_UNBLOCK_INPUT;
7901 unbind_to (index, Qnil);
7902 clear_waiting_for_input ();
7903 /* FIXME: This is an asynchronous interrupt w.r.t elisp, so signalling an
7904 error might not be the best thing to do. I'd vote for creating an
7905 elisp event and stuffing it in the queue so people can bind to it via
7906 the global map. --Stef */
7907 error ("%s", error_msg);
7910 /* We specifically use it before defining it, so that gcc doesn't inline it,
7911 otherwise gdb doesn't know how to properly put a breakpoint on it. */
7912 static void x_error_quitter P_ ((Display *, XErrorEvent *));
7914 /* This is the first-level handler for X protocol errors.
7915 It calls x_error_quitter or x_error_catcher. */
7917 static int
7918 x_error_handler (display, error)
7919 Display *display;
7920 XErrorEvent *error;
7922 if (x_error_message)
7923 x_error_catcher (display, error);
7924 else
7925 x_error_quitter (display, error);
7926 return 0;
7929 /* This is the usual handler for X protocol errors.
7930 It kills all frames on the display that we got the error for.
7931 If that was the only one, it prints an error message and kills Emacs. */
7933 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
7935 #if __GNUC__ >= 3 /* On GCC 3.0 we might get a warning. */
7936 #define NO_INLINE __attribute__((noinline))
7937 #else
7938 #define NO_INLINE
7939 #endif
7941 /* Some versions of GNU/Linux define noinline in their headers. */
7943 #ifdef noinline
7944 #undef noinline
7945 #endif
7947 /* On older GCC versions, just putting x_error_quitter
7948 after x_error_handler prevents inlining into the former. */
7950 static void NO_INLINE
7951 x_error_quitter (display, error)
7952 Display *display;
7953 XErrorEvent *error;
7955 char buf[256], buf1[356];
7957 /* Ignore BadName errors. They can happen because of fonts
7958 or colors that are not defined. */
7960 if (error->error_code == BadName)
7961 return;
7963 /* Note that there is no real way portable across R3/R4 to get the
7964 original error handler. */
7966 XGetErrorText (display, error->error_code, buf, sizeof (buf));
7967 sprintf (buf1, "X protocol error: %s on protocol request %d",
7968 buf, error->request_code);
7969 x_connection_closed (display, buf1);
7973 /* This is the handler for X IO errors, always.
7974 It kills all frames on the display that we lost touch with.
7975 If that was the only one, it prints an error message and kills Emacs. */
7977 static int
7978 x_io_error_quitter (display)
7979 Display *display;
7981 char buf[256];
7983 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
7984 x_connection_closed (display, buf);
7985 return 0;
7988 /* Changing the font of the frame. */
7990 /* Give frame F the font named FONTNAME as its default font, and
7991 return the full name of that font. FONTNAME may be a wildcard
7992 pattern; in that case, we choose some font that fits the pattern.
7993 The return value shows which font we chose. */
7995 Lisp_Object
7996 x_new_font (f, fontname)
7997 struct frame *f;
7998 register char *fontname;
8000 struct font_info *fontp
8001 = FS_LOAD_FONT (f, 0, fontname, -1);
8003 if (!fontp)
8004 return Qnil;
8006 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
8007 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
8008 FRAME_FONTSET (f) = -1;
8010 FRAME_COLUMN_WIDTH (f) = fontp->average_width;
8011 FRAME_SPACE_WIDTH (f) = fontp->space_width;
8012 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (FRAME_FONT (f));
8014 compute_fringe_widths (f, 1);
8016 /* Compute the scroll bar width in character columns. */
8017 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8019 int wid = FRAME_COLUMN_WIDTH (f);
8020 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8021 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
8023 else
8025 int wid = FRAME_COLUMN_WIDTH (f);
8026 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
8029 /* Now make the frame display the given font. */
8030 if (FRAME_X_WINDOW (f) != 0)
8032 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
8033 FRAME_FONT (f)->fid);
8034 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
8035 FRAME_FONT (f)->fid);
8036 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
8037 FRAME_FONT (f)->fid);
8039 /* Don't change the size of a tip frame; there's no point in
8040 doing it because it's done in Fx_show_tip, and it leads to
8041 problems because the tip frame has no widget. */
8042 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8043 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
8046 return build_string (fontp->full_name);
8049 /* Give frame F the fontset named FONTSETNAME as its default font, and
8050 return the full name of that fontset. FONTSETNAME may be a wildcard
8051 pattern; in that case, we choose some fontset that fits the pattern.
8052 The return value shows which fontset we chose. */
8054 Lisp_Object
8055 x_new_fontset (f, fontsetname)
8056 struct frame *f;
8057 char *fontsetname;
8059 int fontset = fs_query_fontset (build_string (fontsetname), 0);
8060 Lisp_Object result;
8062 if (fontset < 0)
8063 return Qnil;
8065 if (FRAME_FONTSET (f) == fontset)
8066 /* This fontset is already set in frame F. There's nothing more
8067 to do. */
8068 return fontset_name (fontset);
8070 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
8072 if (!STRINGP (result))
8073 /* Can't load ASCII font. */
8074 return Qnil;
8076 /* Since x_new_font doesn't update any fontset information, do it now. */
8077 FRAME_FONTSET (f) = fontset;
8079 #ifdef HAVE_X_I18N
8080 if (FRAME_XIC (f)
8081 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8082 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
8083 #endif
8085 return build_string (fontsetname);
8089 /***********************************************************************
8090 X Input Methods
8091 ***********************************************************************/
8093 #ifdef HAVE_X_I18N
8095 #ifdef HAVE_X11R6
8097 /* XIM destroy callback function, which is called whenever the
8098 connection to input method XIM dies. CLIENT_DATA contains a
8099 pointer to the x_display_info structure corresponding to XIM. */
8101 static void
8102 xim_destroy_callback (xim, client_data, call_data)
8103 XIM xim;
8104 XPointer client_data;
8105 XPointer call_data;
8107 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8108 Lisp_Object frame, tail;
8110 BLOCK_INPUT;
8112 /* No need to call XDestroyIC.. */
8113 FOR_EACH_FRAME (tail, frame)
8115 struct frame *f = XFRAME (frame);
8116 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8118 FRAME_XIC (f) = NULL;
8119 xic_free_xfontset (f);
8123 /* No need to call XCloseIM. */
8124 dpyinfo->xim = NULL;
8125 XFree (dpyinfo->xim_styles);
8126 UNBLOCK_INPUT;
8129 #endif /* HAVE_X11R6 */
8131 #ifdef HAVE_X11R6
8132 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8133 extern char *XSetIMValues P_ ((XIM, ...));
8134 #endif
8136 /* Open the connection to the XIM server on display DPYINFO.
8137 RESOURCE_NAME is the resource name Emacs uses. */
8139 static void
8140 xim_open_dpy (dpyinfo, resource_name)
8141 struct x_display_info *dpyinfo;
8142 char *resource_name;
8144 XIM xim;
8146 #ifdef HAVE_XIM
8147 if (use_xim)
8149 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8150 EMACS_CLASS);
8151 dpyinfo->xim = xim;
8153 if (xim)
8155 #ifdef HAVE_X11R6
8156 XIMCallback destroy;
8157 #endif
8159 /* Get supported styles and XIM values. */
8160 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8162 #ifdef HAVE_X11R6
8163 destroy.callback = xim_destroy_callback;
8164 destroy.client_data = (XPointer)dpyinfo;
8165 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8166 #endif
8170 else
8171 #endif /* HAVE_XIM */
8172 dpyinfo->xim = NULL;
8176 #ifdef HAVE_X11R6_XIM
8178 struct xim_inst_t
8180 struct x_display_info *dpyinfo;
8181 char *resource_name;
8184 /* XIM instantiate callback function, which is called whenever an XIM
8185 server is available. DISPLAY is the display of the XIM.
8186 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8187 when the callback was registered. */
8189 static void
8190 xim_instantiate_callback (display, client_data, call_data)
8191 Display *display;
8192 XPointer client_data;
8193 XPointer call_data;
8195 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8196 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8198 /* We don't support multiple XIM connections. */
8199 if (dpyinfo->xim)
8200 return;
8202 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8204 /* Create XIC for the existing frames on the same display, as long
8205 as they have no XIC. */
8206 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8208 Lisp_Object tail, frame;
8210 BLOCK_INPUT;
8211 FOR_EACH_FRAME (tail, frame)
8213 struct frame *f = XFRAME (frame);
8215 if (FRAME_X_P (f)
8216 && FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8217 if (FRAME_XIC (f) == NULL)
8219 create_frame_xic (f);
8220 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8221 xic_set_statusarea (f);
8222 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8224 struct window *w = XWINDOW (f->selected_window);
8225 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8230 UNBLOCK_INPUT;
8234 #endif /* HAVE_X11R6_XIM */
8237 /* Open a connection to the XIM server on display DPYINFO.
8238 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8239 connection only at the first time. On X11R6, open the connection
8240 in the XIM instantiate callback function. */
8242 static void
8243 xim_initialize (dpyinfo, resource_name)
8244 struct x_display_info *dpyinfo;
8245 char *resource_name;
8247 #ifdef HAVE_XIM
8248 if (use_xim)
8250 #ifdef HAVE_X11R6_XIM
8251 struct xim_inst_t *xim_inst;
8252 int len;
8254 dpyinfo->xim = NULL;
8255 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8256 xim_inst->dpyinfo = dpyinfo;
8257 len = strlen (resource_name);
8258 xim_inst->resource_name = (char *) xmalloc (len + 1);
8259 bcopy (resource_name, xim_inst->resource_name, len + 1);
8260 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8261 resource_name, EMACS_CLASS,
8262 xim_instantiate_callback,
8263 /* This is XPointer in XFree86
8264 but (XPointer *) on Tru64, at
8265 least, hence the configure test. */
8266 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8267 #else /* not HAVE_X11R6_XIM */
8268 dpyinfo->xim = NULL;
8269 xim_open_dpy (dpyinfo, resource_name);
8270 #endif /* not HAVE_X11R6_XIM */
8273 else
8274 #endif /* HAVE_XIM */
8275 dpyinfo->xim = NULL;
8279 /* Close the connection to the XIM server on display DPYINFO. */
8281 static void
8282 xim_close_dpy (dpyinfo)
8283 struct x_display_info *dpyinfo;
8285 #ifdef HAVE_XIM
8286 if (use_xim)
8288 #ifdef HAVE_X11R6_XIM
8289 if (dpyinfo->display)
8290 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8291 NULL, EMACS_CLASS,
8292 xim_instantiate_callback, NULL);
8293 #endif /* not HAVE_X11R6_XIM */
8294 if (dpyinfo->display)
8295 XCloseIM (dpyinfo->xim);
8296 dpyinfo->xim = NULL;
8297 XFree (dpyinfo->xim_styles);
8299 #endif /* HAVE_XIM */
8302 #endif /* not HAVE_X11R6_XIM */
8306 /* Calculate the absolute position in frame F
8307 from its current recorded position values and gravity. */
8309 void
8310 x_calc_absolute_position (f)
8311 struct frame *f;
8313 int flags = f->size_hint_flags;
8315 /* We have nothing to do if the current position
8316 is already for the top-left corner. */
8317 if (! ((flags & XNegative) || (flags & YNegative)))
8318 return;
8320 /* Treat negative positions as relative to the leftmost bottommost
8321 position that fits on the screen. */
8322 if (flags & XNegative)
8323 f->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
8324 - FRAME_PIXEL_WIDTH (f) + f->left_pos);
8327 int height = FRAME_PIXEL_HEIGHT (f);
8329 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8330 /* Something is fishy here. When using Motif, starting Emacs with
8331 `-g -0-0', the frame appears too low by a few pixels.
8333 This seems to be so because initially, while Emacs is starting,
8334 the column widget's height and the frame's pixel height are
8335 different. The column widget's height is the right one. In
8336 later invocations, when Emacs is up, the frame's pixel height
8337 is right, though.
8339 It's not obvious where the initial small difference comes from.
8340 2000-12-01, gerd. */
8342 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8343 #endif
8345 if (flags & YNegative)
8346 f->top_pos = (FRAME_X_DISPLAY_INFO (f)->height - height + f->top_pos);
8349 /* The left_pos and top_pos
8350 are now relative to the top and left screen edges,
8351 so the flags should correspond. */
8352 f->size_hint_flags &= ~ (XNegative | YNegative);
8355 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8356 to really change the position, and 0 when calling from
8357 x_make_frame_visible (in that case, XOFF and YOFF are the current
8358 position values). It is -1 when calling from x_set_frame_parameters,
8359 which means, do adjust for borders but don't change the gravity. */
8361 void
8362 x_set_offset (f, xoff, yoff, change_gravity)
8363 struct frame *f;
8364 register int xoff, yoff;
8365 int change_gravity;
8367 int modified_top, modified_left;
8369 if (change_gravity != 0)
8371 FRAME_X_OUTPUT (f)->left_before_move = f->left_pos;
8372 FRAME_X_OUTPUT (f)->top_before_move = f->top_pos;
8374 f->top_pos = yoff;
8375 f->left_pos = xoff;
8376 f->size_hint_flags &= ~ (XNegative | YNegative);
8377 if (xoff < 0)
8378 f->size_hint_flags |= XNegative;
8379 if (yoff < 0)
8380 f->size_hint_flags |= YNegative;
8381 f->win_gravity = NorthWestGravity;
8383 x_calc_absolute_position (f);
8385 BLOCK_INPUT;
8386 x_wm_set_size_hint (f, (long) 0, 0);
8388 modified_left = f->left_pos;
8389 modified_top = f->top_pos;
8391 if (change_gravity != 0 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8393 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8394 than the WM decorations. So we use the calculated offset instead
8395 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8396 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8397 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8400 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8401 modified_left, modified_top);
8403 x_sync_with_move (f, f->left_pos, f->top_pos,
8404 FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8405 ? 1 : 0);
8407 /* change_gravity is non-zero when this function is called from Lisp to
8408 programmatically move a frame. In that case, we call
8409 x_check_expected_move to discover if we have a "Type A" or "Type B"
8410 window manager, and, for a "Type A" window manager, adjust the position
8411 of the frame.
8413 We call x_check_expected_move if a programmatic move occurred, and
8414 either the window manager type (A/B) is unknown or it is Type A but we
8415 need to compute the top/left offset adjustment for this frame. */
8417 if (change_gravity != 0 &&
8418 (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8419 || (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8420 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8421 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8422 x_check_expected_move (f, modified_left, modified_top);
8424 UNBLOCK_INPUT;
8427 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8428 on the root window for frame F contains ATOMNAME.
8429 This is how a WM check shall be done according to the Window Manager
8430 Specification/Extended Window Manager Hints at
8431 http://freedesktop.org/wiki/Specifications/wm-spec. */
8433 static int
8434 wm_supports (f, atomname)
8435 struct frame *f;
8436 const char *atomname;
8438 Atom actual_type;
8439 unsigned long actual_size, bytes_remaining;
8440 int i, rc, actual_format;
8441 Atom prop_atom;
8442 Window wmcheck_window;
8443 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8444 Window target_window = dpyinfo->root_window;
8445 long max_len = 65536;
8446 Display *dpy = FRAME_X_DISPLAY (f);
8447 unsigned char *tmp_data = NULL;
8448 Atom target_type = XA_WINDOW;
8449 Atom want_atom;
8451 BLOCK_INPUT;
8453 prop_atom = XInternAtom (dpy, "_NET_SUPPORTING_WM_CHECK", False);
8455 x_catch_errors (dpy);
8456 rc = XGetWindowProperty (dpy, target_window,
8457 prop_atom, 0, max_len, False, target_type,
8458 &actual_type, &actual_format, &actual_size,
8459 &bytes_remaining, &tmp_data);
8461 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8463 if (tmp_data) XFree (tmp_data);
8464 x_uncatch_errors ();
8465 UNBLOCK_INPUT;
8466 return 0;
8469 wmcheck_window = *(Window *) tmp_data;
8470 XFree (tmp_data);
8472 /* Check if window exists. */
8473 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8474 x_sync (f);
8475 if (x_had_errors_p (dpy))
8477 x_uncatch_errors ();
8478 UNBLOCK_INPUT;
8479 return 0;
8482 if (dpyinfo->net_supported_window != wmcheck_window)
8484 /* Window changed, reload atoms */
8485 if (dpyinfo->net_supported_atoms != NULL)
8486 XFree (dpyinfo->net_supported_atoms);
8487 dpyinfo->net_supported_atoms = NULL;
8488 dpyinfo->nr_net_supported_atoms = 0;
8489 dpyinfo->net_supported_window = 0;
8491 target_type = XA_ATOM;
8492 prop_atom = XInternAtom (dpy, "_NET_SUPPORTED", False);
8493 tmp_data = NULL;
8494 rc = XGetWindowProperty (dpy, target_window,
8495 prop_atom, 0, max_len, False, target_type,
8496 &actual_type, &actual_format, &actual_size,
8497 &bytes_remaining, &tmp_data);
8499 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
8501 if (tmp_data) XFree (tmp_data);
8502 x_uncatch_errors ();
8503 UNBLOCK_INPUT;
8504 return 0;
8507 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8508 dpyinfo->nr_net_supported_atoms = actual_size;
8509 dpyinfo->net_supported_window = wmcheck_window;
8512 rc = 0;
8513 want_atom = XInternAtom (dpy, atomname, False);
8515 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
8516 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8518 x_uncatch_errors ();
8519 UNBLOCK_INPUT;
8521 return rc;
8524 /* Do fullscreen as specified in extended window manager hints */
8526 static int
8527 do_ewmh_fullscreen (f)
8528 struct frame *f;
8530 int have_net_atom = wm_supports (f, "_NET_WM_STATE");
8532 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8533 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8534 if (!have_net_atom)
8535 have_net_atom = wm_supports (f, "_NET_WM_STATE_FULLSCREEN");
8537 if (have_net_atom)
8539 Lisp_Object frame;
8540 const char *atom = "_NET_WM_STATE";
8541 const char *fs = "_NET_WM_STATE_FULLSCREEN";
8542 const char *fw = "_NET_WM_STATE_MAXIMIZED_HORZ";
8543 const char *fh = "_NET_WM_STATE_MAXIMIZED_VERT";
8544 const char *what = NULL;
8546 XSETFRAME (frame, f);
8548 /* If there are _NET_ atoms we assume we have extended window manager
8549 hints. */
8550 switch (f->want_fullscreen)
8552 case FULLSCREEN_BOTH:
8553 what = fs;
8554 break;
8555 case FULLSCREEN_WIDTH:
8556 what = fw;
8557 break;
8558 case FULLSCREEN_HEIGHT:
8559 what = fh;
8560 break;
8563 if (what != NULL && !wm_supports (f, what)) return 0;
8566 Fx_send_client_event (frame, make_number (0), frame,
8567 make_unibyte_string (atom, strlen (atom)),
8568 make_number (32),
8569 Fcons (make_number (0), /* Remove */
8570 Fcons
8571 (make_unibyte_string (fs,
8572 strlen (fs)),
8573 Qnil)));
8574 Fx_send_client_event (frame, make_number (0), frame,
8575 make_unibyte_string (atom, strlen (atom)),
8576 make_number (32),
8577 Fcons (make_number (0), /* Remove */
8578 Fcons
8579 (make_unibyte_string (fh,
8580 strlen (fh)),
8581 Qnil)));
8582 Fx_send_client_event (frame, make_number (0), frame,
8583 make_unibyte_string (atom, strlen (atom)),
8584 make_number (32),
8585 Fcons (make_number (0), /* Remove */
8586 Fcons
8587 (make_unibyte_string (fw,
8588 strlen (fw)),
8589 Qnil)));
8590 f->want_fullscreen = FULLSCREEN_NONE;
8591 if (what != NULL)
8592 Fx_send_client_event (frame, make_number (0), frame,
8593 make_unibyte_string (atom, strlen (atom)),
8594 make_number (32),
8595 Fcons (make_number (1), /* Add */
8596 Fcons
8597 (make_unibyte_string (what,
8598 strlen (what)),
8599 Qnil)));
8602 return have_net_atom;
8605 static void
8606 XTfullscreen_hook (f)
8607 FRAME_PTR f;
8609 if (f->async_visible)
8611 BLOCK_INPUT;
8612 do_ewmh_fullscreen (f);
8613 x_sync (f);
8614 UNBLOCK_INPUT;
8619 /* Check if we need to resize the frame due to a fullscreen request.
8620 If so needed, resize the frame. */
8621 static void
8622 x_check_fullscreen (f)
8623 struct frame *f;
8625 if (f->want_fullscreen & FULLSCREEN_BOTH)
8627 int width, height, ign;
8629 if (do_ewmh_fullscreen (f))
8630 return;
8632 x_real_positions (f, &f->left_pos, &f->top_pos);
8634 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
8636 /* We do not need to move the window, it shall be taken care of
8637 when setting WM manager hints.
8638 If the frame is visible already, the position is checked by
8639 x_check_expected_move. */
8640 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
8642 change_frame_size (f, height, width, 0, 1, 0);
8643 SET_FRAME_GARBAGED (f);
8644 cancel_mouse_face (f);
8646 /* Wait for the change of frame size to occur */
8647 f->want_fullscreen |= FULLSCREEN_WAIT;
8652 /* This function is called by x_set_offset to determine whether the window
8653 manager interfered with the positioning of the frame. Type A window
8654 managers position the surrounding window manager decorations a small
8655 amount above and left of the user-supplied position. Type B window
8656 managers position the surrounding window manager decorations at the
8657 user-specified position. If we detect a Type A window manager, we
8658 compensate by moving the window right and down by the proper amount. */
8660 static void
8661 x_check_expected_move (f, expected_left, expected_top)
8662 struct frame *f;
8663 int expected_left;
8664 int expected_top;
8666 int current_left = 0, current_top = 0;
8668 /* x_real_positions returns the left and top offsets of the outermost
8669 window manager window around the frame. */
8671 x_real_positions (f, &current_left, &current_top);
8673 if (current_left != expected_left || current_top != expected_top)
8675 /* It's a "Type A" window manager. */
8677 int adjusted_left;
8678 int adjusted_top;
8680 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8681 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
8682 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
8684 /* Now fix the mispositioned frame's location. */
8686 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
8687 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
8689 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8690 adjusted_left, adjusted_top);
8692 x_sync_with_move (f, expected_left, expected_top, 0);
8694 else
8695 /* It's a "Type B" window manager. We don't have to adjust the
8696 frame's position. */
8698 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8702 /* Wait for XGetGeometry to return up-to-date position information for a
8703 recently-moved frame. Call this immediately after calling XMoveWindow.
8704 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
8705 frame has been moved to, so we use a fuzzy position comparison instead
8706 of an exact comparison. */
8708 static void
8709 x_sync_with_move (f, left, top, fuzzy)
8710 struct frame *f;
8711 int left, top, fuzzy;
8713 int count = 0;
8715 while (count++ < 50)
8717 int current_left = 0, current_top = 0;
8719 /* In theory, this call to XSync only needs to happen once, but in
8720 practice, it doesn't seem to work, hence the need for the surrounding
8721 loop. */
8723 XSync (FRAME_X_DISPLAY (f), False);
8724 x_real_positions (f, &current_left, &current_top);
8726 if (fuzzy)
8728 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
8729 pixels. */
8731 if (eabs (current_left - left) <= 10
8732 && eabs (current_top - top) <= 40)
8733 return;
8735 else if (current_left == left && current_top == top)
8736 return;
8739 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
8740 will then return up-to-date position info. */
8742 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
8746 /* Change the size of frame F's X window to COLS/ROWS in the case F
8747 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8748 top-left-corner window gravity for this size change and subsequent
8749 size changes. Otherwise we leave the window gravity unchanged. */
8751 static void
8752 x_set_window_size_1 (f, change_gravity, cols, rows)
8753 struct frame *f;
8754 int change_gravity;
8755 int cols, rows;
8757 int pixelwidth, pixelheight;
8759 check_frame_size (f, &rows, &cols);
8760 f->scroll_bar_actual_width
8761 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8763 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
8764 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
8765 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
8767 compute_fringe_widths (f, 0);
8769 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
8770 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
8772 f->win_gravity = NorthWestGravity;
8773 x_wm_set_size_hint (f, (long) 0, 0);
8775 XSync (FRAME_X_DISPLAY (f), False);
8776 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8777 pixelwidth, pixelheight);
8779 /* Now, strictly speaking, we can't be sure that this is accurate,
8780 but the window manager will get around to dealing with the size
8781 change request eventually, and we'll hear how it went when the
8782 ConfigureNotify event gets here.
8784 We could just not bother storing any of this information here,
8785 and let the ConfigureNotify event set everything up, but that
8786 might be kind of confusing to the Lisp code, since size changes
8787 wouldn't be reported in the frame parameters until some random
8788 point in the future when the ConfigureNotify event arrives.
8790 We pass 1 for DELAY since we can't run Lisp code inside of
8791 a BLOCK_INPUT. */
8792 change_frame_size (f, rows, cols, 0, 1, 0);
8793 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8794 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8796 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8797 receive in the ConfigureNotify event; if we get what we asked
8798 for, then the event won't cause the screen to become garbaged, so
8799 we have to make sure to do it here. */
8800 SET_FRAME_GARBAGED (f);
8802 XFlush (FRAME_X_DISPLAY (f));
8806 /* Call this to change the size of frame F's x-window.
8807 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8808 for this size change and subsequent size changes.
8809 Otherwise we leave the window gravity unchanged. */
8811 void
8812 x_set_window_size (f, change_gravity, cols, rows)
8813 struct frame *f;
8814 int change_gravity;
8815 int cols, rows;
8817 BLOCK_INPUT;
8819 #ifdef USE_GTK
8820 if (FRAME_GTK_WIDGET (f))
8821 xg_frame_set_char_size (f, cols, rows);
8822 else
8823 x_set_window_size_1 (f, change_gravity, cols, rows);
8824 #elif USE_X_TOOLKIT
8826 if (f->output_data.x->widget != NULL)
8828 /* The x and y position of the widget is clobbered by the
8829 call to XtSetValues within EmacsFrameSetCharSize.
8830 This is a real kludge, but I don't understand Xt so I can't
8831 figure out a correct fix. Can anyone else tell me? -- rms. */
8832 int xpos = f->output_data.x->widget->core.x;
8833 int ypos = f->output_data.x->widget->core.y;
8834 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
8835 f->output_data.x->widget->core.x = xpos;
8836 f->output_data.x->widget->core.y = ypos;
8838 else
8839 x_set_window_size_1 (f, change_gravity, cols, rows);
8841 #else /* not USE_X_TOOLKIT */
8843 x_set_window_size_1 (f, change_gravity, cols, rows);
8845 #endif /* not USE_X_TOOLKIT */
8847 /* If cursor was outside the new size, mark it as off. */
8848 mark_window_cursors_off (XWINDOW (f->root_window));
8850 /* Clear out any recollection of where the mouse highlighting was,
8851 since it might be in a place that's outside the new frame size.
8852 Actually checking whether it is outside is a pain in the neck,
8853 so don't try--just let the highlighting be done afresh with new size. */
8854 cancel_mouse_face (f);
8856 UNBLOCK_INPUT;
8859 /* Mouse warping. */
8861 void
8862 x_set_mouse_position (f, x, y)
8863 struct frame *f;
8864 int x, y;
8866 int pix_x, pix_y;
8868 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
8869 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
8871 if (pix_x < 0) pix_x = 0;
8872 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
8874 if (pix_y < 0) pix_y = 0;
8875 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
8877 BLOCK_INPUT;
8879 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8880 0, 0, 0, 0, pix_x, pix_y);
8881 UNBLOCK_INPUT;
8884 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
8886 void
8887 x_set_mouse_pixel_position (f, pix_x, pix_y)
8888 struct frame *f;
8889 int pix_x, pix_y;
8891 BLOCK_INPUT;
8893 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8894 0, 0, 0, 0, pix_x, pix_y);
8895 UNBLOCK_INPUT;
8898 /* focus shifting, raising and lowering. */
8900 void
8901 x_focus_on_frame (f)
8902 struct frame *f;
8904 #if 0 /* This proves to be unpleasant. */
8905 x_raise_frame (f);
8906 #endif
8907 #if 0
8908 /* I don't think that the ICCCM allows programs to do things like this
8909 without the interaction of the window manager. Whatever you end up
8910 doing with this code, do it to x_unfocus_frame too. */
8911 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8912 RevertToPointerRoot, CurrentTime);
8913 #endif /* ! 0 */
8916 void
8917 x_unfocus_frame (f)
8918 struct frame *f;
8920 #if 0
8921 /* Look at the remarks in x_focus_on_frame. */
8922 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
8923 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
8924 RevertToPointerRoot, CurrentTime);
8925 #endif /* ! 0 */
8928 /* Raise frame F. */
8930 void
8931 x_raise_frame (f)
8932 struct frame *f;
8934 BLOCK_INPUT;
8935 if (f->async_visible)
8936 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8938 XFlush (FRAME_X_DISPLAY (f));
8939 UNBLOCK_INPUT;
8942 /* Lower frame F. */
8944 void
8945 x_lower_frame (f)
8946 struct frame *f;
8948 if (f->async_visible)
8950 BLOCK_INPUT;
8951 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8952 XFlush (FRAME_X_DISPLAY (f));
8953 UNBLOCK_INPUT;
8957 /* Activate frame with Extended Window Manager Hints */
8959 void
8960 x_ewmh_activate_frame (f)
8961 FRAME_PTR f;
8963 /* See Window Manager Specification/Extended Window Manager Hints at
8964 http://freedesktop.org/wiki/Specifications/wm-spec */
8966 const char *atom = "_NET_ACTIVE_WINDOW";
8967 if (f->async_visible && wm_supports (f, atom))
8969 Lisp_Object frame;
8970 XSETFRAME (frame, f);
8971 Fx_send_client_event (frame, make_number (0), frame,
8972 make_unibyte_string (atom, strlen (atom)),
8973 make_number (32),
8974 Fcons (make_number (1),
8975 Fcons (make_number (last_user_time),
8976 Qnil)));
8980 static void
8981 XTframe_raise_lower (f, raise_flag)
8982 FRAME_PTR f;
8983 int raise_flag;
8985 if (raise_flag)
8986 x_raise_frame (f);
8987 else
8988 x_lower_frame (f);
8991 /* Change of visibility. */
8993 /* This tries to wait until the frame is really visible.
8994 However, if the window manager asks the user where to position
8995 the frame, this will return before the user finishes doing that.
8996 The frame will not actually be visible at that time,
8997 but it will become visible later when the window manager
8998 finishes with it. */
9000 void
9001 x_make_frame_visible (f)
9002 struct frame *f;
9004 Lisp_Object type;
9005 int original_top, original_left;
9006 int retry_count = 2;
9008 retry:
9010 BLOCK_INPUT;
9012 type = x_icon_type (f);
9013 if (!NILP (type))
9014 x_bitmap_icon (f, type);
9016 if (! FRAME_VISIBLE_P (f))
9018 /* We test FRAME_GARBAGED_P here to make sure we don't
9019 call x_set_offset a second time
9020 if we get to x_make_frame_visible a second time
9021 before the window gets really visible. */
9022 if (! FRAME_ICONIFIED_P (f)
9023 && ! f->output_data.x->asked_for_visible)
9024 x_set_offset (f, f->left_pos, f->top_pos, 0);
9026 f->output_data.x->asked_for_visible = 1;
9028 if (! EQ (Vx_no_window_manager, Qt))
9029 x_wm_set_window_state (f, NormalState);
9030 #ifdef USE_X_TOOLKIT
9031 /* This was XtPopup, but that did nothing for an iconified frame. */
9032 XtMapWidget (f->output_data.x->widget);
9033 #else /* not USE_X_TOOLKIT */
9034 #ifdef USE_GTK
9035 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9036 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9037 #else
9038 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9039 #endif /* not USE_GTK */
9040 #endif /* not USE_X_TOOLKIT */
9041 #if 0 /* This seems to bring back scroll bars in the wrong places
9042 if the window configuration has changed. They seem
9043 to come back ok without this. */
9044 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9045 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9046 #endif
9049 XFlush (FRAME_X_DISPLAY (f));
9051 /* Synchronize to ensure Emacs knows the frame is visible
9052 before we do anything else. We do this loop with input not blocked
9053 so that incoming events are handled. */
9055 Lisp_Object frame;
9056 int count;
9057 /* This must be before UNBLOCK_INPUT
9058 since events that arrive in response to the actions above
9059 will set it when they are handled. */
9060 int previously_visible = f->output_data.x->has_been_visible;
9062 original_left = f->left_pos;
9063 original_top = f->top_pos;
9065 /* This must come after we set COUNT. */
9066 UNBLOCK_INPUT;
9068 /* We unblock here so that arriving X events are processed. */
9070 /* Now move the window back to where it was "supposed to be".
9071 But don't do it if the gravity is negative.
9072 When the gravity is negative, this uses a position
9073 that is 3 pixels too low. Perhaps that's really the border width.
9075 Don't do this if the window has never been visible before,
9076 because the window manager may choose the position
9077 and we don't want to override it. */
9079 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
9080 && f->win_gravity == NorthWestGravity
9081 && previously_visible)
9083 Drawable rootw;
9084 int x, y;
9085 unsigned int width, height, border, depth;
9087 BLOCK_INPUT;
9089 /* On some window managers (such as FVWM) moving an existing
9090 window, even to the same place, causes the window manager
9091 to introduce an offset. This can cause the window to move
9092 to an unexpected location. Check the geometry (a little
9093 slow here) and then verify that the window is in the right
9094 place. If the window is not in the right place, move it
9095 there, and take the potential window manager hit. */
9096 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9097 &rootw, &x, &y, &width, &height, &border, &depth);
9099 if (original_left != x || original_top != y)
9100 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9101 original_left, original_top);
9103 UNBLOCK_INPUT;
9106 XSETFRAME (frame, f);
9108 /* Wait until the frame is visible. Process X events until a
9109 MapNotify event has been seen, or until we think we won't get a
9110 MapNotify at all.. */
9111 for (count = input_signal_count + 10;
9112 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9114 /* Force processing of queued events. */
9115 x_sync (f);
9117 /* Machines that do polling rather than SIGIO have been
9118 observed to go into a busy-wait here. So we'll fake an
9119 alarm signal to let the handler know that there's something
9120 to be read. We used to raise a real alarm, but it seems
9121 that the handler isn't always enabled here. This is
9122 probably a bug. */
9123 if (input_polling_used ())
9125 /* It could be confusing if a real alarm arrives while
9126 processing the fake one. Turn it off and let the
9127 handler reset it. */
9128 extern void poll_for_input_1 P_ ((void));
9129 int old_poll_suppress_count = poll_suppress_count;
9130 poll_suppress_count = 1;
9131 poll_for_input_1 ();
9132 poll_suppress_count = old_poll_suppress_count;
9135 /* See if a MapNotify event has been processed. */
9136 FRAME_SAMPLE_VISIBILITY (f);
9139 /* 2000-09-28: In
9141 (let ((f (selected-frame)))
9142 (iconify-frame f)
9143 (raise-frame f))
9145 the frame is not raised with various window managers on
9146 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
9147 unknown reason, the call to XtMapWidget is completely ignored.
9148 Mapping the widget a second time works. */
9150 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
9151 goto retry;
9155 /* Change from mapped state to withdrawn state. */
9157 /* Make the frame visible (mapped and not iconified). */
9159 void
9160 x_make_frame_invisible (f)
9161 struct frame *f;
9163 Window window;
9165 /* Use the frame's outermost window, not the one we normally draw on. */
9166 window = FRAME_OUTER_WINDOW (f);
9168 /* Don't keep the highlight on an invisible frame. */
9169 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9170 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9172 #if 0/* This might add unreliability; I don't trust it -- rms. */
9173 if (! f->async_visible && ! f->async_iconified)
9174 return;
9175 #endif
9177 BLOCK_INPUT;
9179 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9180 that the current position of the window is user-specified, rather than
9181 program-specified, so that when the window is mapped again, it will be
9182 placed at the same location, without forcing the user to position it
9183 by hand again (they have already done that once for this window.) */
9184 x_wm_set_size_hint (f, (long) 0, 1);
9186 #ifdef USE_GTK
9187 if (FRAME_GTK_OUTER_WIDGET (f))
9188 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9189 else
9190 #endif
9193 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9194 DefaultScreen (FRAME_X_DISPLAY (f))))
9196 UNBLOCK_INPUT_RESIGNAL;
9197 error ("Can't notify window manager of window withdrawal");
9201 /* We can't distinguish this from iconification
9202 just by the event that we get from the server.
9203 So we can't win using the usual strategy of letting
9204 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9205 and synchronize with the server to make sure we agree. */
9206 f->visible = 0;
9207 FRAME_ICONIFIED_P (f) = 0;
9208 f->async_visible = 0;
9209 f->async_iconified = 0;
9211 x_sync (f);
9213 UNBLOCK_INPUT;
9216 /* Change window state from mapped to iconified. */
9218 void
9219 x_iconify_frame (f)
9220 struct frame *f;
9222 int result;
9223 Lisp_Object type;
9225 /* Don't keep the highlight on an invisible frame. */
9226 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9227 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9229 if (f->async_iconified)
9230 return;
9232 BLOCK_INPUT;
9234 FRAME_SAMPLE_VISIBILITY (f);
9236 type = x_icon_type (f);
9237 if (!NILP (type))
9238 x_bitmap_icon (f, type);
9240 #ifdef USE_GTK
9241 if (FRAME_GTK_OUTER_WIDGET (f))
9243 if (! FRAME_VISIBLE_P (f))
9244 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9246 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9247 f->iconified = 1;
9248 f->visible = 1;
9249 f->async_iconified = 1;
9250 f->async_visible = 0;
9251 UNBLOCK_INPUT;
9252 return;
9254 #endif
9256 #ifdef USE_X_TOOLKIT
9258 if (! FRAME_VISIBLE_P (f))
9260 if (! EQ (Vx_no_window_manager, Qt))
9261 x_wm_set_window_state (f, IconicState);
9262 /* This was XtPopup, but that did nothing for an iconified frame. */
9263 XtMapWidget (f->output_data.x->widget);
9264 /* The server won't give us any event to indicate
9265 that an invisible frame was changed to an icon,
9266 so we have to record it here. */
9267 f->iconified = 1;
9268 f->visible = 1;
9269 f->async_iconified = 1;
9270 f->async_visible = 0;
9271 UNBLOCK_INPUT;
9272 return;
9275 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9276 XtWindow (f->output_data.x->widget),
9277 DefaultScreen (FRAME_X_DISPLAY (f)));
9278 UNBLOCK_INPUT;
9280 if (!result)
9281 error ("Can't notify window manager of iconification");
9283 f->async_iconified = 1;
9284 f->async_visible = 0;
9287 BLOCK_INPUT;
9288 XFlush (FRAME_X_DISPLAY (f));
9289 UNBLOCK_INPUT;
9290 #else /* not USE_X_TOOLKIT */
9292 /* Make sure the X server knows where the window should be positioned,
9293 in case the user deiconifies with the window manager. */
9294 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
9295 x_set_offset (f, f->left_pos, f->top_pos, 0);
9297 /* Since we don't know which revision of X we're running, we'll use both
9298 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9300 /* X11R4: send a ClientMessage to the window manager using the
9301 WM_CHANGE_STATE type. */
9303 XEvent message;
9305 message.xclient.window = FRAME_X_WINDOW (f);
9306 message.xclient.type = ClientMessage;
9307 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
9308 message.xclient.format = 32;
9309 message.xclient.data.l[0] = IconicState;
9311 if (! XSendEvent (FRAME_X_DISPLAY (f),
9312 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9313 False,
9314 SubstructureRedirectMask | SubstructureNotifyMask,
9315 &message))
9317 UNBLOCK_INPUT_RESIGNAL;
9318 error ("Can't notify window manager of iconification");
9322 /* X11R3: set the initial_state field of the window manager hints to
9323 IconicState. */
9324 x_wm_set_window_state (f, IconicState);
9326 if (!FRAME_VISIBLE_P (f))
9328 /* If the frame was withdrawn, before, we must map it. */
9329 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9332 f->async_iconified = 1;
9333 f->async_visible = 0;
9335 XFlush (FRAME_X_DISPLAY (f));
9336 UNBLOCK_INPUT;
9337 #endif /* not USE_X_TOOLKIT */
9341 /* Free X resources of frame F. */
9343 void
9344 x_free_frame_resources (f)
9345 struct frame *f;
9347 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9348 Lisp_Object bar;
9349 struct scroll_bar *b;
9351 BLOCK_INPUT;
9353 /* If a display connection is dead, don't try sending more
9354 commands to the X server. */
9355 if (dpyinfo->display)
9357 if (f->output_data.x->icon_desc)
9358 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9360 #ifdef USE_X_TOOLKIT
9361 /* Explicitly destroy the scroll bars of the frame. Without
9362 this, we get "BadDrawable" errors from the toolkit later on,
9363 presumably from expose events generated for the disappearing
9364 toolkit scroll bars. */
9365 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9367 b = XSCROLL_BAR (bar);
9368 x_scroll_bar_remove (b);
9370 #endif
9372 #ifdef HAVE_X_I18N
9373 if (FRAME_XIC (f))
9374 free_frame_xic (f);
9375 #endif
9377 #ifdef USE_X_TOOLKIT
9378 if (f->output_data.x->widget)
9380 XtDestroyWidget (f->output_data.x->widget);
9381 f->output_data.x->widget = NULL;
9383 /* Tooltips don't have widgets, only a simple X window, even if
9384 we are using a toolkit. */
9385 else if (FRAME_X_WINDOW (f))
9386 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9388 free_frame_menubar (f);
9389 #else /* !USE_X_TOOLKIT */
9391 #ifdef USE_GTK
9392 /* In the GTK version, tooltips are normal X
9393 frames. We must check and free both types. */
9394 if (FRAME_GTK_OUTER_WIDGET (f))
9396 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9397 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9398 FRAME_GTK_OUTER_WIDGET (f) = 0;
9400 #endif /* USE_GTK */
9402 if (FRAME_X_WINDOW (f))
9403 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9404 #endif /* !USE_X_TOOLKIT */
9406 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
9407 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
9408 unload_color (f, f->output_data.x->cursor_pixel);
9409 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9410 unload_color (f, f->output_data.x->border_pixel);
9411 unload_color (f, f->output_data.x->mouse_pixel);
9413 if (f->output_data.x->scroll_bar_background_pixel != -1)
9414 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9415 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9416 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9417 #ifdef USE_TOOLKIT_SCROLL_BARS
9418 /* Scrollbar shadow colors. */
9419 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9420 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9421 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9422 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9423 #endif /* USE_TOOLKIT_SCROLL_BARS */
9424 if (f->output_data.x->white_relief.allocated_p)
9425 unload_color (f, f->output_data.x->white_relief.pixel);
9426 if (f->output_data.x->black_relief.allocated_p)
9427 unload_color (f, f->output_data.x->black_relief.pixel);
9429 if (FRAME_FACE_CACHE (f))
9430 free_frame_faces (f);
9432 x_free_gcs (f);
9433 XFlush (FRAME_X_DISPLAY (f));
9436 if (f->output_data.x->saved_menu_event)
9437 xfree (f->output_data.x->saved_menu_event);
9439 xfree (f->output_data.x);
9440 f->output_data.x = NULL;
9442 if (f == dpyinfo->x_focus_frame)
9443 dpyinfo->x_focus_frame = 0;
9444 if (f == dpyinfo->x_focus_event_frame)
9445 dpyinfo->x_focus_event_frame = 0;
9446 if (f == dpyinfo->x_highlight_frame)
9447 dpyinfo->x_highlight_frame = 0;
9449 if (f == dpyinfo->mouse_face_mouse_frame)
9451 dpyinfo->mouse_face_beg_row
9452 = dpyinfo->mouse_face_beg_col = -1;
9453 dpyinfo->mouse_face_end_row
9454 = dpyinfo->mouse_face_end_col = -1;
9455 dpyinfo->mouse_face_window = Qnil;
9456 dpyinfo->mouse_face_deferred_gc = 0;
9457 dpyinfo->mouse_face_mouse_frame = 0;
9460 UNBLOCK_INPUT;
9464 /* Destroy the X window of frame F. */
9466 void
9467 x_destroy_window (f)
9468 struct frame *f;
9470 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9472 /* If a display connection is dead, don't try sending more
9473 commands to the X server. */
9474 if (dpyinfo->display != 0)
9475 x_free_frame_resources (f);
9477 dpyinfo->reference_count--;
9481 /* Setting window manager hints. */
9483 /* Set the normal size hints for the window manager, for frame F.
9484 FLAGS is the flags word to use--or 0 meaning preserve the flags
9485 that the window now has.
9486 If USER_POSITION is nonzero, we set the USPosition
9487 flag (this is useful when FLAGS is 0).
9488 The GTK version is in gtkutils.c */
9490 #ifndef USE_GTK
9491 void
9492 x_wm_set_size_hint (f, flags, user_position)
9493 struct frame *f;
9494 long flags;
9495 int user_position;
9497 XSizeHints size_hints;
9499 #ifdef USE_X_TOOLKIT
9500 Arg al[2];
9501 int ac = 0;
9502 Dimension widget_width, widget_height;
9503 #endif
9505 Window window = FRAME_OUTER_WINDOW (f);
9507 /* Setting PMaxSize caused various problems. */
9508 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9510 size_hints.x = f->left_pos;
9511 size_hints.y = f->top_pos;
9513 #ifdef USE_X_TOOLKIT
9514 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
9515 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
9516 XtGetValues (f->output_data.x->widget, al, ac);
9517 size_hints.height = widget_height;
9518 size_hints.width = widget_width;
9519 #else /* not USE_X_TOOLKIT */
9520 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9521 size_hints.width = FRAME_PIXEL_WIDTH (f);
9522 #endif /* not USE_X_TOOLKIT */
9524 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9525 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9526 size_hints.max_width
9527 = FRAME_X_DISPLAY_INFO (f)->width - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9528 size_hints.max_height
9529 = FRAME_X_DISPLAY_INFO (f)->height - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9531 /* Calculate the base and minimum sizes.
9533 (When we use the X toolkit, we don't do it here.
9534 Instead we copy the values that the widgets are using, below.) */
9535 #ifndef USE_X_TOOLKIT
9537 int base_width, base_height;
9538 int min_rows = 0, min_cols = 0;
9540 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9541 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9543 check_frame_size (f, &min_rows, &min_cols);
9545 /* The window manager uses the base width hints to calculate the
9546 current number of rows and columns in the frame while
9547 resizing; min_width and min_height aren't useful for this
9548 purpose, since they might not give the dimensions for a
9549 zero-row, zero-column frame.
9551 We use the base_width and base_height members if we have
9552 them; otherwise, we set the min_width and min_height members
9553 to the size for a zero x zero frame. */
9555 size_hints.flags |= PBaseSize;
9556 size_hints.base_width = base_width;
9557 size_hints.base_height = base_height;
9558 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9559 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9562 /* If we don't need the old flags, we don't need the old hint at all. */
9563 if (flags)
9565 size_hints.flags |= flags;
9566 goto no_read;
9568 #endif /* not USE_X_TOOLKIT */
9571 XSizeHints hints; /* Sometimes I hate X Windows... */
9572 long supplied_return;
9573 int value;
9575 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9576 &supplied_return);
9578 #ifdef USE_X_TOOLKIT
9579 size_hints.base_height = hints.base_height;
9580 size_hints.base_width = hints.base_width;
9581 size_hints.min_height = hints.min_height;
9582 size_hints.min_width = hints.min_width;
9583 #endif
9585 if (flags)
9586 size_hints.flags |= flags;
9587 else
9589 if (value == 0)
9590 hints.flags = 0;
9591 if (hints.flags & PSize)
9592 size_hints.flags |= PSize;
9593 if (hints.flags & PPosition)
9594 size_hints.flags |= PPosition;
9595 if (hints.flags & USPosition)
9596 size_hints.flags |= USPosition;
9597 if (hints.flags & USSize)
9598 size_hints.flags |= USSize;
9602 #ifndef USE_X_TOOLKIT
9603 no_read:
9604 #endif
9606 #ifdef PWinGravity
9607 size_hints.win_gravity = f->win_gravity;
9608 size_hints.flags |= PWinGravity;
9610 if (user_position)
9612 size_hints.flags &= ~ PPosition;
9613 size_hints.flags |= USPosition;
9615 #endif /* PWinGravity */
9617 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9619 #endif /* not USE_GTK */
9621 /* Used for IconicState or NormalState */
9623 void
9624 x_wm_set_window_state (f, state)
9625 struct frame *f;
9626 int state;
9628 #ifdef USE_X_TOOLKIT
9629 Arg al[1];
9631 XtSetArg (al[0], XtNinitialState, state);
9632 XtSetValues (f->output_data.x->widget, al, 1);
9633 #else /* not USE_X_TOOLKIT */
9634 Window window = FRAME_X_WINDOW (f);
9636 f->output_data.x->wm_hints.flags |= StateHint;
9637 f->output_data.x->wm_hints.initial_state = state;
9639 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9640 #endif /* not USE_X_TOOLKIT */
9643 void
9644 x_wm_set_icon_pixmap (f, pixmap_id)
9645 struct frame *f;
9646 int pixmap_id;
9648 Pixmap icon_pixmap, icon_mask;
9650 #ifndef USE_X_TOOLKIT
9651 Window window = FRAME_OUTER_WINDOW (f);
9652 #endif
9654 if (pixmap_id > 0)
9656 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9657 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9658 icon_mask = x_bitmap_mask (f, pixmap_id);
9659 f->output_data.x->wm_hints.icon_mask = icon_mask;
9661 else
9663 /* It seems there is no way to turn off use of an icon pixmap.
9664 The following line does it, only if no icon has yet been created,
9665 for some window managers. But with mwm it crashes.
9666 Some people say it should clear the IconPixmapHint bit in this case,
9667 but that doesn't work, and the X consortium said it isn't the
9668 right thing at all. Since there is no way to win,
9669 best to explicitly give up. */
9670 #if 0
9671 f->output_data.x->wm_hints.icon_pixmap = None;
9672 f->output_data.x->wm_hints.icon_mask = None;
9673 #else
9674 return;
9675 #endif
9679 #ifdef USE_GTK
9681 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9682 return;
9685 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
9688 Arg al[1];
9689 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9690 XtSetValues (f->output_data.x->widget, al, 1);
9691 XtSetArg (al[0], XtNiconMask, icon_mask);
9692 XtSetValues (f->output_data.x->widget, al, 1);
9695 #else /* not USE_X_TOOLKIT && not USE_GTK */
9697 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9698 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9700 #endif /* not USE_X_TOOLKIT && not USE_GTK */
9703 void
9704 x_wm_set_icon_position (f, icon_x, icon_y)
9705 struct frame *f;
9706 int icon_x, icon_y;
9708 Window window = FRAME_OUTER_WINDOW (f);
9710 f->output_data.x->wm_hints.flags |= IconPositionHint;
9711 f->output_data.x->wm_hints.icon_x = icon_x;
9712 f->output_data.x->wm_hints.icon_y = icon_y;
9714 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9718 /***********************************************************************
9719 Fonts
9720 ***********************************************************************/
9722 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
9724 struct font_info *
9725 x_get_font_info (f, font_idx)
9726 FRAME_PTR f;
9727 int font_idx;
9729 return (FRAME_X_FONT_TABLE (f) + font_idx);
9733 /* Return a list of names of available fonts matching PATTERN on frame F.
9735 If SIZE is > 0, it is the size (maximum bounds width) of fonts
9736 to be listed.
9738 SIZE < 0 means include scalable fonts.
9740 Frame F null means we have not yet created any frame on X, and
9741 consult the first display in x_display_list. MAXNAMES sets a limit
9742 on how many fonts to match. */
9744 Lisp_Object
9745 x_list_fonts (f, pattern, size, maxnames)
9746 struct frame *f;
9747 Lisp_Object pattern;
9748 int size;
9749 int maxnames;
9751 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
9752 Lisp_Object tem, second_best;
9753 struct x_display_info *dpyinfo
9754 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
9755 Display *dpy = dpyinfo->display;
9756 int try_XLoadQueryFont = 0;
9757 int allow_auto_scaled_font = 0;
9759 if (size < 0)
9761 allow_auto_scaled_font = 1;
9762 size = 0;
9765 patterns = Fassoc (pattern, Valternate_fontname_alist);
9766 if (NILP (patterns))
9767 patterns = Fcons (pattern, Qnil);
9769 if (maxnames == 1 && !size)
9770 /* We can return any single font matching PATTERN. */
9771 try_XLoadQueryFont = 1;
9773 for (; CONSP (patterns); patterns = XCDR (patterns))
9775 int num_fonts;
9776 char **names = NULL;
9778 pattern = XCAR (patterns);
9779 /* See if we cached the result for this particular query.
9780 The cache is an alist of the form:
9781 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
9782 tem = XCDR (dpyinfo->name_list_element);
9783 key = Fcons (Fcons (pattern, make_number (maxnames)),
9784 allow_auto_scaled_font ? Qt : Qnil);
9785 list = Fassoc (key, tem);
9786 if (!NILP (list))
9788 list = Fcdr_safe (list);
9789 /* We have a cashed list. Don't have to get the list again. */
9790 goto label_cached;
9793 /* At first, put PATTERN in the cache. */
9795 BLOCK_INPUT;
9796 x_catch_errors (dpy);
9798 if (try_XLoadQueryFont)
9800 XFontStruct *font;
9801 unsigned long value;
9803 font = XLoadQueryFont (dpy, SDATA (pattern));
9804 if (x_had_errors_p (dpy))
9806 /* This error is perhaps due to insufficient memory on X
9807 server. Let's just ignore it. */
9808 font = NULL;
9809 x_clear_errors (dpy);
9812 if (font
9813 && XGetFontProperty (font, XA_FONT, &value))
9815 char *name = (char *) XGetAtomName (dpy, (Atom) value);
9816 int len = strlen (name);
9817 char *tmp;
9819 /* If DXPC (a Differential X Protocol Compressor)
9820 Ver.3.7 is running, XGetAtomName will return null
9821 string. We must avoid such a name. */
9822 if (len == 0)
9823 try_XLoadQueryFont = 0;
9824 else
9826 num_fonts = 1;
9827 names = (char **) alloca (sizeof (char *));
9828 /* Some systems only allow alloca assigned to a
9829 simple var. */
9830 tmp = (char *) alloca (len + 1); names[0] = tmp;
9831 bcopy (name, names[0], len + 1);
9832 XFree (name);
9835 else
9836 try_XLoadQueryFont = 0;
9838 if (font)
9839 XFreeFont (dpy, font);
9842 if (!try_XLoadQueryFont)
9844 /* We try at least 10 fonts because XListFonts will return
9845 auto-scaled fonts at the head. */
9846 if (maxnames < 0)
9848 int limit;
9850 for (limit = 500;;)
9852 names = XListFonts (dpy, SDATA (pattern), limit, &num_fonts);
9853 if (num_fonts == limit)
9855 BLOCK_INPUT;
9856 XFreeFontNames (names);
9857 UNBLOCK_INPUT;
9858 limit *= 2;
9860 else
9861 break;
9864 else
9865 names = XListFonts (dpy, SDATA (pattern), max (maxnames, 10),
9866 &num_fonts);
9868 if (x_had_errors_p (dpy))
9870 /* This error is perhaps due to insufficient memory on X
9871 server. Let's just ignore it. */
9872 names = NULL;
9873 x_clear_errors (dpy);
9877 x_uncatch_errors ();
9878 UNBLOCK_INPUT;
9880 if (names)
9882 int i;
9884 /* Make a list of all the fonts we got back.
9885 Store that in the font cache for the display. */
9886 for (i = 0; i < num_fonts; i++)
9888 int width = 0;
9889 char *p = names[i];
9890 int average_width = -1, resx = 0, dashes = 0;
9892 /* Count the number of dashes in NAMES[I]. If there are
9893 14 dashes, the field value following 9th dash
9894 (RESOLUTION_X) is nonzero, and the field value
9895 following 12th dash (AVERAGE_WIDTH) is 0, this is a
9896 auto-scaled font which is usually too ugly to be used
9897 for editing. Let's ignore it. */
9898 while (*p)
9899 if (*p++ == '-')
9901 dashes++;
9902 if (dashes == 7) /* PIXEL_SIZE field */
9903 width = atoi (p);
9904 else if (dashes == 9)
9905 resx = atoi (p);
9906 else if (dashes == 12) /* AVERAGE_WIDTH field */
9907 average_width = atoi (p);
9910 if (allow_auto_scaled_font
9911 || dashes < 14 || average_width != 0 || resx == 0)
9913 tem = build_string (names[i]);
9914 if (NILP (Fassoc (tem, list)))
9916 if (STRINGP (Vx_pixel_size_width_font_regexp)
9917 && ((fast_c_string_match_ignore_case
9918 (Vx_pixel_size_width_font_regexp, names[i]))
9919 >= 0))
9920 /* We can set the value of PIXEL_SIZE to the
9921 width of this font. */
9922 list = Fcons (Fcons (tem, make_number (width)), list);
9923 else
9924 /* For the moment, width is not known. */
9925 list = Fcons (Fcons (tem, Qnil), list);
9930 if (!try_XLoadQueryFont)
9932 BLOCK_INPUT;
9933 XFreeFontNames (names);
9934 UNBLOCK_INPUT;
9938 /* Now store the result in the cache. */
9939 XSETCDR (dpyinfo->name_list_element,
9940 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
9942 label_cached:
9943 if (NILP (list)) continue; /* Try the remaining alternatives. */
9945 newlist = second_best = Qnil;
9946 /* Make a list of the fonts that have the right width. */
9947 for (; CONSP (list); list = XCDR (list))
9949 int found_size;
9951 tem = XCAR (list);
9953 if (!CONSP (tem) || NILP (XCAR (tem)))
9954 continue;
9955 if (!size)
9957 newlist = Fcons (XCAR (tem), newlist);
9958 continue;
9961 if (!INTEGERP (XCDR (tem)))
9963 /* Since we have not yet known the size of this font, we
9964 must try slow function call XLoadQueryFont. */
9965 XFontStruct *thisinfo;
9967 BLOCK_INPUT;
9968 x_catch_errors (dpy);
9969 thisinfo = XLoadQueryFont (dpy,
9970 SDATA (XCAR (tem)));
9971 if (x_had_errors_p (dpy))
9973 /* This error is perhaps due to insufficient memory on X
9974 server. Let's just ignore it. */
9975 thisinfo = NULL;
9976 x_clear_errors (dpy);
9978 x_uncatch_errors ();
9979 UNBLOCK_INPUT;
9981 if (thisinfo)
9983 XSETCDR (tem,
9984 (thisinfo->min_bounds.width == 0
9985 ? make_number (0)
9986 : make_number (thisinfo->max_bounds.width)));
9987 BLOCK_INPUT;
9988 XFreeFont (dpy, thisinfo);
9989 UNBLOCK_INPUT;
9991 else
9992 /* For unknown reason, the previous call of XListFont had
9993 returned a font which can't be opened. Record the size
9994 as 0 not to try to open it again. */
9995 XSETCDR (tem, make_number (0));
9998 found_size = XINT (XCDR (tem));
9999 if (found_size == size)
10000 newlist = Fcons (XCAR (tem), newlist);
10001 else if (found_size > 0)
10003 if (NILP (second_best))
10004 second_best = tem;
10005 else if (found_size < size)
10007 if (XINT (XCDR (second_best)) > size
10008 || XINT (XCDR (second_best)) < found_size)
10009 second_best = tem;
10011 else
10013 if (XINT (XCDR (second_best)) > size
10014 && XINT (XCDR (second_best)) > found_size)
10015 second_best = tem;
10019 if (!NILP (newlist))
10020 break;
10021 else if (!NILP (second_best))
10023 newlist = Fcons (XCAR (second_best), Qnil);
10024 break;
10028 return newlist;
10032 #if GLYPH_DEBUG
10034 /* Check that FONT is valid on frame F. It is if it can be found in F's
10035 font table. */
10037 static void
10038 x_check_font (f, font)
10039 struct frame *f;
10040 XFontStruct *font;
10042 int i;
10043 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10045 xassert (font != NULL);
10047 for (i = 0; i < dpyinfo->n_fonts; i++)
10048 if (dpyinfo->font_table[i].name
10049 && font == dpyinfo->font_table[i].font)
10050 break;
10052 xassert (i < dpyinfo->n_fonts);
10055 #endif /* GLYPH_DEBUG != 0 */
10057 /* Set *W to the minimum width, *H to the minimum font height of FONT.
10058 Note: There are (broken) X fonts out there with invalid XFontStruct
10059 min_bounds contents. For example, handa@etl.go.jp reports that
10060 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
10061 have font->min_bounds.width == 0. */
10063 static INLINE void
10064 x_font_min_bounds (font, w, h)
10065 XFontStruct *font;
10066 int *w, *h;
10068 *h = FONT_HEIGHT (font);
10069 *w = font->min_bounds.width;
10071 /* Try to handle the case where FONT->min_bounds has invalid
10072 contents. Since the only font known to have invalid min_bounds
10073 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
10074 if (*w <= 0)
10075 *w = font->max_bounds.width;
10079 /* Compute the smallest character width and smallest font height over
10080 all fonts available on frame F. Set the members smallest_char_width
10081 and smallest_font_height in F's x_display_info structure to
10082 the values computed. Value is non-zero if smallest_font_height or
10083 smallest_char_width become smaller than they were before. */
10085 static int
10086 x_compute_min_glyph_bounds (f)
10087 struct frame *f;
10089 int i;
10090 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10091 XFontStruct *font;
10092 int old_width = dpyinfo->smallest_char_width;
10093 int old_height = dpyinfo->smallest_font_height;
10095 dpyinfo->smallest_font_height = 100000;
10096 dpyinfo->smallest_char_width = 100000;
10098 for (i = 0; i < dpyinfo->n_fonts; ++i)
10099 if (dpyinfo->font_table[i].name)
10101 struct font_info *fontp = dpyinfo->font_table + i;
10102 int w, h;
10104 font = (XFontStruct *) fontp->font;
10105 xassert (font != (XFontStruct *) ~0);
10106 x_font_min_bounds (font, &w, &h);
10108 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
10109 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
10112 xassert (dpyinfo->smallest_char_width > 0
10113 && dpyinfo->smallest_font_height > 0);
10115 return (dpyinfo->n_fonts == 1
10116 || dpyinfo->smallest_char_width < old_width
10117 || dpyinfo->smallest_font_height < old_height);
10121 /* Load font named FONTNAME of the size SIZE for frame F, and return a
10122 pointer to the structure font_info while allocating it dynamically.
10123 If SIZE is 0, load any size of font.
10124 If loading is failed, return NULL. */
10126 struct font_info *
10127 x_load_font (f, fontname, size)
10128 struct frame *f;
10129 register char *fontname;
10130 int size;
10132 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10133 Lisp_Object font_names;
10135 /* Get a list of all the fonts that match this name. Once we
10136 have a list of matching fonts, we compare them against the fonts
10137 we already have by comparing names. */
10138 font_names = x_list_fonts (f, build_string (fontname), size, 1);
10140 if (!NILP (font_names))
10142 Lisp_Object tail;
10143 int i;
10145 for (i = 0; i < dpyinfo->n_fonts; i++)
10146 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
10147 if (dpyinfo->font_table[i].name
10148 && (!strcmp (dpyinfo->font_table[i].name,
10149 SDATA (XCAR (tail)))
10150 || !strcmp (dpyinfo->font_table[i].full_name,
10151 SDATA (XCAR (tail)))))
10152 return (dpyinfo->font_table + i);
10155 /* Load the font and add it to the table. */
10157 char *full_name;
10158 XFontStruct *font;
10159 struct font_info *fontp;
10160 unsigned long value;
10161 int i;
10163 /* If we have found fonts by x_list_font, load one of them. If
10164 not, we still try to load a font by the name given as FONTNAME
10165 because XListFonts (called in x_list_font) of some X server has
10166 a bug of not finding a font even if the font surely exists and
10167 is loadable by XLoadQueryFont. */
10168 if (size > 0 && !NILP (font_names))
10169 fontname = (char *) SDATA (XCAR (font_names));
10171 BLOCK_INPUT;
10172 x_catch_errors (FRAME_X_DISPLAY (f));
10173 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
10174 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
10176 /* This error is perhaps due to insufficient memory on X
10177 server. Let's just ignore it. */
10178 font = NULL;
10179 x_clear_errors (FRAME_X_DISPLAY (f));
10181 x_uncatch_errors ();
10182 UNBLOCK_INPUT;
10183 if (!font)
10184 return NULL;
10186 /* Find a free slot in the font table. */
10187 for (i = 0; i < dpyinfo->n_fonts; ++i)
10188 if (dpyinfo->font_table[i].name == NULL)
10189 break;
10191 /* If no free slot found, maybe enlarge the font table. */
10192 if (i == dpyinfo->n_fonts
10193 && dpyinfo->n_fonts == dpyinfo->font_table_size)
10195 int sz;
10196 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
10197 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
10198 dpyinfo->font_table
10199 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
10202 fontp = dpyinfo->font_table + i;
10203 if (i == dpyinfo->n_fonts)
10204 ++dpyinfo->n_fonts;
10206 /* Now fill in the slots of *FONTP. */
10207 BLOCK_INPUT;
10208 bzero (fontp, sizeof (*fontp));
10209 fontp->font = font;
10210 fontp->font_idx = i;
10211 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
10212 bcopy (fontname, fontp->name, strlen (fontname) + 1);
10214 if (font->min_bounds.width == font->max_bounds.width)
10216 /* Fixed width font. */
10217 fontp->average_width = fontp->space_width = font->min_bounds.width;
10219 else
10221 XChar2b char2b;
10222 XCharStruct *pcm;
10224 char2b.byte1 = 0x00, char2b.byte2 = 0x20;
10225 pcm = x_per_char_metric (font, &char2b, 0);
10226 if (pcm)
10227 fontp->space_width = pcm->width;
10228 else
10229 fontp->space_width = FONT_WIDTH (font);
10231 fontp->average_width
10232 = (XGetFontProperty (font, dpyinfo->Xatom_AVERAGE_WIDTH, &value)
10233 ? (long) value / 10 : 0);
10234 if (fontp->average_width < 0)
10235 fontp->average_width = - fontp->average_width;
10236 if (fontp->average_width == 0)
10238 if (pcm)
10240 int width = pcm->width;
10241 for (char2b.byte2 = 33; char2b.byte2 <= 126; char2b.byte2++)
10242 if ((pcm = x_per_char_metric (font, &char2b, 0)) != NULL)
10243 width += pcm->width;
10244 fontp->average_width = width / 95;
10246 else
10247 fontp->average_width = FONT_WIDTH (font);
10251 /* Try to get the full name of FONT. Put it in FULL_NAME. */
10252 full_name = 0;
10253 if (XGetFontProperty (font, XA_FONT, &value))
10255 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
10256 char *p = name;
10257 int dashes = 0;
10259 /* Count the number of dashes in the "full name".
10260 If it is too few, this isn't really the font's full name,
10261 so don't use it.
10262 In X11R4, the fonts did not come with their canonical names
10263 stored in them. */
10264 while (*p)
10266 if (*p == '-')
10267 dashes++;
10268 p++;
10271 if (dashes >= 13)
10273 full_name = (char *) xmalloc (p - name + 1);
10274 bcopy (name, full_name, p - name + 1);
10277 XFree (name);
10280 if (full_name != 0)
10281 fontp->full_name = full_name;
10282 else
10283 fontp->full_name = fontp->name;
10285 fontp->size = font->max_bounds.width;
10286 fontp->height = FONT_HEIGHT (font);
10288 if (NILP (font_names))
10290 /* We come here because of a bug of XListFonts mentioned at
10291 the head of this block. Let's store this information in
10292 the cache for x_list_fonts. */
10293 Lisp_Object lispy_name = build_string (fontname);
10294 Lisp_Object lispy_full_name = build_string (fontp->full_name);
10295 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
10296 Qnil);
10298 XSETCDR (dpyinfo->name_list_element,
10299 Fcons (Fcons (key,
10300 Fcons (Fcons (lispy_full_name,
10301 make_number (fontp->size)),
10302 Qnil)),
10303 XCDR (dpyinfo->name_list_element)));
10304 if (full_name)
10306 key = Fcons (Fcons (lispy_full_name, make_number (256)),
10307 Qnil);
10308 XSETCDR (dpyinfo->name_list_element,
10309 Fcons (Fcons (key,
10310 Fcons (Fcons (lispy_full_name,
10311 make_number (fontp->size)),
10312 Qnil)),
10313 XCDR (dpyinfo->name_list_element)));
10317 /* The slot `encoding' specifies how to map a character
10318 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
10319 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
10320 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
10321 2:0xA020..0xFF7F). For the moment, we don't know which charset
10322 uses this font. So, we set information in fontp->encoding[1]
10323 which is never used by any charset. If mapping can't be
10324 decided, set FONT_ENCODING_NOT_DECIDED. */
10325 fontp->encoding[1]
10326 = (font->max_byte1 == 0
10327 /* 1-byte font */
10328 ? (font->min_char_or_byte2 < 0x80
10329 ? (font->max_char_or_byte2 < 0x80
10330 ? 0 /* 0x20..0x7F */
10331 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
10332 : 1) /* 0xA0..0xFF */
10333 /* 2-byte font */
10334 : (font->min_byte1 < 0x80
10335 ? (font->max_byte1 < 0x80
10336 ? (font->min_char_or_byte2 < 0x80
10337 ? (font->max_char_or_byte2 < 0x80
10338 ? 0 /* 0x2020..0x7F7F */
10339 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
10340 : 3) /* 0x20A0..0x7FFF */
10341 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
10342 : (font->min_char_or_byte2 < 0x80
10343 ? (font->max_char_or_byte2 < 0x80
10344 ? 2 /* 0xA020..0xFF7F */
10345 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
10346 : 1))); /* 0xA0A0..0xFFFF */
10348 fontp->baseline_offset
10349 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
10350 ? (long) value : 0);
10351 fontp->relative_compose
10352 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
10353 ? (long) value : 0);
10354 fontp->default_ascent
10355 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
10356 ? (long) value : 0);
10358 /* Set global flag fonts_changed_p to non-zero if the font loaded
10359 has a character with a smaller width than any other character
10360 before, or if the font loaded has a smaller height than any
10361 other font loaded before. If this happens, it will make a
10362 glyph matrix reallocation necessary. */
10363 fonts_changed_p |= x_compute_min_glyph_bounds (f);
10364 UNBLOCK_INPUT;
10365 return fontp;
10370 /* Return a pointer to struct font_info of a font named FONTNAME for
10371 frame F. If no such font is loaded, return NULL. */
10373 struct font_info *
10374 x_query_font (f, fontname)
10375 struct frame *f;
10376 register char *fontname;
10378 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10379 int i;
10381 for (i = 0; i < dpyinfo->n_fonts; i++)
10382 if (dpyinfo->font_table[i].name
10383 && (!xstricmp (dpyinfo->font_table[i].name, fontname)
10384 || !xstricmp (dpyinfo->font_table[i].full_name, fontname)))
10385 return (dpyinfo->font_table + i);
10386 return NULL;
10390 /* Find a CCL program for a font specified by FONTP, and set the member
10391 `encoder' of the structure. */
10393 void
10394 x_find_ccl_program (fontp)
10395 struct font_info *fontp;
10397 Lisp_Object list, elt;
10399 elt = Qnil;
10400 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
10402 elt = XCAR (list);
10403 if (CONSP (elt)
10404 && STRINGP (XCAR (elt))
10405 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
10406 >= 0)
10407 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
10408 >= 0)))
10409 break;
10412 if (! NILP (list))
10414 struct ccl_program *ccl
10415 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
10417 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
10418 xfree (ccl);
10419 else
10420 fontp->font_encoder = ccl;
10426 /***********************************************************************
10427 Initialization
10428 ***********************************************************************/
10430 #ifdef USE_X_TOOLKIT
10431 static XrmOptionDescRec emacs_options[] = {
10432 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
10433 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
10435 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
10436 XrmoptionSepArg, NULL},
10437 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
10439 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10440 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10441 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10442 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10443 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10444 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
10445 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
10448 /* Whether atimer for Xt timeouts is activated or not. */
10450 static int x_timeout_atimer_activated_flag;
10452 #endif /* USE_X_TOOLKIT */
10454 static int x_initialized;
10456 #ifdef HAVE_X_SM
10457 static int x_session_initialized;
10458 #endif
10460 #ifdef MULTI_KBOARD
10461 /* Test whether two display-name strings agree up to the dot that separates
10462 the screen number from the server number. */
10463 static int
10464 same_x_server (name1, name2)
10465 const char *name1, *name2;
10467 int seen_colon = 0;
10468 const unsigned char *system_name = SDATA (Vsystem_name);
10469 int system_name_length = strlen (system_name);
10470 int length_until_period = 0;
10472 while (system_name[length_until_period] != 0
10473 && system_name[length_until_period] != '.')
10474 length_until_period++;
10476 /* Treat `unix' like an empty host name. */
10477 if (! strncmp (name1, "unix:", 5))
10478 name1 += 4;
10479 if (! strncmp (name2, "unix:", 5))
10480 name2 += 4;
10481 /* Treat this host's name like an empty host name. */
10482 if (! strncmp (name1, system_name, system_name_length)
10483 && name1[system_name_length] == ':')
10484 name1 += system_name_length;
10485 if (! strncmp (name2, system_name, system_name_length)
10486 && name2[system_name_length] == ':')
10487 name2 += system_name_length;
10488 /* Treat this host's domainless name like an empty host name. */
10489 if (! strncmp (name1, system_name, length_until_period)
10490 && name1[length_until_period] == ':')
10491 name1 += length_until_period;
10492 if (! strncmp (name2, system_name, length_until_period)
10493 && name2[length_until_period] == ':')
10494 name2 += length_until_period;
10496 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
10498 if (*name1 == ':')
10499 seen_colon++;
10500 if (seen_colon && *name1 == '.')
10501 return 1;
10503 return (seen_colon
10504 && (*name1 == '.' || *name1 == '\0')
10505 && (*name2 == '.' || *name2 == '\0'));
10507 #endif
10509 /* Count number of set bits in mask and number of bits to shift to
10510 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
10511 to 5. */
10512 static void
10513 get_bits_and_offset (mask, bits, offset)
10514 unsigned long mask;
10515 int *bits;
10516 int *offset;
10518 int nr = 0;
10519 int off = 0;
10521 while (!(mask & 1))
10523 off++;
10524 mask >>= 1;
10527 while (mask & 1)
10529 nr++;
10530 mask >>= 1;
10533 *offset = off;
10534 *bits = nr;
10537 /* Return 1 if display DISPLAY is available for use, 0 otherwise.
10538 But don't permanently open it, just test its availability. */
10541 x_display_ok (display)
10542 const char *display;
10544 int dpy_ok = 1;
10545 Display *dpy;
10547 dpy = XOpenDisplay (display);
10548 if (dpy)
10549 XCloseDisplay (dpy);
10550 else
10551 dpy_ok = 0;
10552 return dpy_ok;
10555 /* Open a connection to X display DISPLAY_NAME, and return
10556 the structure that describes the open display.
10557 If we cannot contact the display, return null. */
10559 struct x_display_info *
10560 x_term_init (display_name, xrm_option, resource_name)
10561 Lisp_Object display_name;
10562 char *xrm_option;
10563 char *resource_name;
10565 int connection;
10566 Display *dpy;
10567 struct terminal *terminal;
10568 struct x_display_info *dpyinfo;
10569 XrmDatabase xrdb;
10571 BLOCK_INPUT;
10573 if (!x_initialized)
10575 x_initialize ();
10576 ++x_initialized;
10579 if (! x_display_ok (SDATA (display_name)))
10580 error ("Display %s can't be opened", SDATA (display_name));
10582 #ifdef USE_GTK
10584 #define NUM_ARGV 10
10585 int argc;
10586 char *argv[NUM_ARGV];
10587 char **argv2 = argv;
10588 GdkAtom atom;
10590 #ifndef HAVE_GTK_MULTIDISPLAY
10591 if (!EQ (Vinitial_window_system, intern ("x")))
10592 error ("Sorry, you cannot connect to X servers with the GTK toolkit");
10593 #endif
10595 if (x_initialized++ > 1)
10597 #ifdef HAVE_GTK_MULTIDISPLAY
10598 /* Opening another display. If xg_display_open returns less
10599 than zero, we are probably on GTK 2.0, which can only handle
10600 one display. GTK 2.2 or later can handle more than one. */
10601 if (xg_display_open (SDATA (display_name), &dpy) < 0)
10602 #endif
10603 error ("Sorry, this version of GTK can only handle one display");
10605 else
10607 for (argc = 0; argc < NUM_ARGV; ++argc)
10608 argv[argc] = 0;
10610 argc = 0;
10611 argv[argc++] = initial_argv[0];
10613 if (! NILP (display_name))
10615 argv[argc++] = "--display";
10616 argv[argc++] = SDATA (display_name);
10619 argv[argc++] = "--name";
10620 argv[argc++] = resource_name;
10622 XSetLocaleModifiers ("");
10624 gtk_init (&argc, &argv2);
10626 /* gtk_init does set_locale. We must fix locale after calling it. */
10627 fixup_locale ();
10628 xg_initialize ();
10630 dpy = GDK_DISPLAY ();
10632 /* NULL window -> events for all windows go to our function */
10633 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10635 /* Load our own gtkrc if it exists. */
10637 char *file = "~/.emacs.d/gtkrc";
10638 Lisp_Object s, abs_file;
10640 s = make_string (file, strlen (file));
10641 abs_file = Fexpand_file_name (s, Qnil);
10643 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10644 gtk_rc_parse (SDATA (abs_file));
10647 XSetErrorHandler (x_error_handler);
10648 XSetIOErrorHandler (x_io_error_quitter);
10651 #else /* not USE_GTK */
10652 #ifdef USE_X_TOOLKIT
10653 /* weiner@footloose.sps.mot.com reports that this causes
10654 errors with X11R5:
10655 X protocol error: BadAtom (invalid Atom parameter)
10656 on protocol request 18skiloaf.
10657 So let's not use it until R6. */
10658 #ifdef HAVE_X11XTR6
10659 XtSetLanguageProc (NULL, NULL, NULL);
10660 #endif
10663 int argc = 0;
10664 char *argv[3];
10666 argv[0] = "";
10667 argc = 1;
10668 if (xrm_option)
10670 argv[argc++] = "-xrm";
10671 argv[argc++] = xrm_option;
10673 turn_on_atimers (0);
10674 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10675 resource_name, EMACS_CLASS,
10676 emacs_options, XtNumber (emacs_options),
10677 &argc, argv);
10678 turn_on_atimers (1);
10680 #ifdef HAVE_X11XTR6
10681 /* I think this is to compensate for XtSetLanguageProc. */
10682 fixup_locale ();
10683 #endif
10686 #else /* not USE_X_TOOLKIT */
10687 XSetLocaleModifiers ("");
10688 dpy = XOpenDisplay (SDATA (display_name));
10689 #endif /* not USE_X_TOOLKIT */
10690 #endif /* not USE_GTK*/
10692 /* Detect failure. */
10693 if (dpy == 0)
10695 UNBLOCK_INPUT;
10696 return 0;
10699 /* We have definitely succeeded. Record the new connection. */
10701 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10702 bzero (dpyinfo, sizeof *dpyinfo);
10704 terminal = x_create_terminal (dpyinfo);
10706 #ifdef MULTI_KBOARD
10708 struct x_display_info *share;
10709 Lisp_Object tail;
10711 for (share = x_display_list, tail = x_display_name_list; share;
10712 share = share->next, tail = XCDR (tail))
10713 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10714 SDATA (display_name)))
10715 break;
10716 if (share)
10717 terminal->kboard = share->terminal->kboard;
10718 else
10720 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10721 init_kboard (terminal->kboard);
10722 terminal->kboard->Vwindow_system = intern ("x");
10723 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10725 char *vendor = ServerVendor (dpy);
10726 UNBLOCK_INPUT;
10727 terminal->kboard->Vsystem_key_alist
10728 = call1 (Qvendor_specific_keysyms,
10729 vendor ? build_string (vendor) : empty_unibyte_string);
10730 BLOCK_INPUT;
10733 terminal->kboard->next_kboard = all_kboards;
10734 all_kboards = terminal->kboard;
10735 /* Don't let the initial kboard remain current longer than necessary.
10736 That would cause problems if a file loaded on startup tries to
10737 prompt in the mini-buffer. */
10738 if (current_kboard == initial_kboard)
10739 current_kboard = terminal->kboard;
10741 terminal->kboard->reference_count++;
10743 #endif
10745 /* Put this display on the chain. */
10746 dpyinfo->next = x_display_list;
10747 x_display_list = dpyinfo;
10749 /* Put it on x_display_name_list as well, to keep them parallel. */
10750 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10751 x_display_name_list);
10752 dpyinfo->name_list_element = XCAR (x_display_name_list);
10754 dpyinfo->display = dpy;
10756 /* Set the name of the terminal. */
10757 terminal->name = (char *) xmalloc (SBYTES (display_name) + 1);
10758 strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
10759 terminal->name[SBYTES (display_name)] = 0;
10761 #if 0
10762 XSetAfterFunction (x_current_display, x_trace_wire);
10763 #endif /* ! 0 */
10765 dpyinfo->x_id_name
10766 = (char *) xmalloc (SBYTES (Vinvocation_name)
10767 + SBYTES (Vsystem_name)
10768 + 2);
10769 sprintf (dpyinfo->x_id_name, "%s@%s",
10770 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10772 /* Figure out which modifier bits mean what. */
10773 x_find_modifier_meanings (dpyinfo);
10775 /* Get the scroll bar cursor. */
10776 #ifdef USE_GTK
10777 /* We must create a GTK cursor, it is required for GTK widgets. */
10778 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10779 #endif /* USE_GTK */
10781 dpyinfo->vertical_scroll_bar_cursor
10782 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10784 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10785 resource_name, EMACS_CLASS);
10786 #ifdef HAVE_XRMSETDATABASE
10787 XrmSetDatabase (dpyinfo->display, xrdb);
10788 #else
10789 dpyinfo->display->db = xrdb;
10790 #endif
10791 /* Put the rdb where we can find it in a way that works on
10792 all versions. */
10793 dpyinfo->xrdb = xrdb;
10795 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10796 DefaultScreen (dpyinfo->display));
10797 select_visual (dpyinfo);
10798 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10799 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
10800 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
10801 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10802 dpyinfo->client_leader_window = 0;
10803 dpyinfo->grabbed = 0;
10804 dpyinfo->reference_count = 0;
10805 dpyinfo->icon_bitmap_id = -1;
10806 dpyinfo->font_table = NULL;
10807 dpyinfo->n_fonts = 0;
10808 dpyinfo->font_table_size = 0;
10809 dpyinfo->bitmaps = 0;
10810 dpyinfo->bitmaps_size = 0;
10811 dpyinfo->bitmaps_last = 0;
10812 dpyinfo->scratch_cursor_gc = 0;
10813 dpyinfo->mouse_face_mouse_frame = 0;
10814 dpyinfo->mouse_face_deferred_gc = 0;
10815 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10816 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10817 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10818 dpyinfo->mouse_face_window = Qnil;
10819 dpyinfo->mouse_face_overlay = Qnil;
10820 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10821 dpyinfo->mouse_face_defer = 0;
10822 dpyinfo->mouse_face_hidden = 0;
10823 dpyinfo->x_focus_frame = 0;
10824 dpyinfo->x_focus_event_frame = 0;
10825 dpyinfo->x_highlight_frame = 0;
10826 dpyinfo->image_cache = make_image_cache ();
10827 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10829 /* See if we can construct pixel values from RGB values. */
10830 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
10831 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
10833 if (dpyinfo->visual->class == TrueColor)
10835 get_bits_and_offset (dpyinfo->visual->red_mask,
10836 &dpyinfo->red_bits, &dpyinfo->red_offset);
10837 get_bits_and_offset (dpyinfo->visual->blue_mask,
10838 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10839 get_bits_and_offset (dpyinfo->visual->green_mask,
10840 &dpyinfo->green_bits, &dpyinfo->green_offset);
10843 /* See if a private colormap is requested. */
10844 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10846 if (dpyinfo->visual->class == PseudoColor)
10848 Lisp_Object value;
10849 value = display_x_get_resource (dpyinfo,
10850 build_string ("privateColormap"),
10851 build_string ("PrivateColormap"),
10852 Qnil, Qnil);
10853 if (STRINGP (value)
10854 && (!strcmp (SDATA (value), "true")
10855 || !strcmp (SDATA (value), "on")))
10856 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10859 else
10860 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10861 dpyinfo->visual, AllocNone);
10864 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10865 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10866 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10867 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10868 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
10869 pixels = DisplayWidth (dpyinfo->display, screen_number);
10870 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10871 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10872 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
10875 dpyinfo->Xatom_wm_protocols
10876 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10877 dpyinfo->Xatom_wm_take_focus
10878 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10879 dpyinfo->Xatom_wm_save_yourself
10880 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10881 dpyinfo->Xatom_wm_delete_window
10882 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10883 dpyinfo->Xatom_wm_change_state
10884 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10885 dpyinfo->Xatom_wm_configure_denied
10886 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10887 dpyinfo->Xatom_wm_window_moved
10888 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10889 dpyinfo->Xatom_wm_client_leader
10890 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
10891 dpyinfo->Xatom_editres
10892 = XInternAtom (dpyinfo->display, "Editres", False);
10893 dpyinfo->Xatom_CLIPBOARD
10894 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10895 dpyinfo->Xatom_TIMESTAMP
10896 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10897 dpyinfo->Xatom_TEXT
10898 = XInternAtom (dpyinfo->display, "TEXT", False);
10899 dpyinfo->Xatom_COMPOUND_TEXT
10900 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10901 dpyinfo->Xatom_UTF8_STRING
10902 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10903 dpyinfo->Xatom_DELETE
10904 = XInternAtom (dpyinfo->display, "DELETE", False);
10905 dpyinfo->Xatom_MULTIPLE
10906 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10907 dpyinfo->Xatom_INCR
10908 = XInternAtom (dpyinfo->display, "INCR", False);
10909 dpyinfo->Xatom_EMACS_TMP
10910 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10911 dpyinfo->Xatom_TARGETS
10912 = XInternAtom (dpyinfo->display, "TARGETS", False);
10913 dpyinfo->Xatom_NULL
10914 = XInternAtom (dpyinfo->display, "NULL", False);
10915 dpyinfo->Xatom_ATOM_PAIR
10916 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10917 /* For properties of font. */
10918 dpyinfo->Xatom_PIXEL_SIZE
10919 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10920 dpyinfo->Xatom_AVERAGE_WIDTH
10921 = XInternAtom (dpyinfo->display, "AVERAGE_WIDTH", False);
10922 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10923 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10924 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10925 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10926 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10927 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10929 /* Ghostscript support. */
10930 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10931 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10933 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10934 False);
10936 dpyinfo->cut_buffers_initialized = 0;
10938 dpyinfo->x_dnd_atoms_size = 8;
10939 dpyinfo->x_dnd_atoms_length = 0;
10940 dpyinfo->x_dnd_atoms = xmalloc (sizeof (*dpyinfo->x_dnd_atoms)
10941 * dpyinfo->x_dnd_atoms_size);
10943 dpyinfo->net_supported_atoms = NULL;
10944 dpyinfo->nr_net_supported_atoms = 0;
10945 dpyinfo->net_supported_window = 0;
10947 connection = ConnectionNumber (dpyinfo->display);
10948 dpyinfo->connection = connection;
10951 char null_bits[1];
10953 null_bits[0] = 0x00;
10955 dpyinfo->null_pixel
10956 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10957 null_bits, 1, 1, (long) 0, (long) 0,
10962 extern int gray_bitmap_width, gray_bitmap_height;
10963 extern char *gray_bitmap_bits;
10964 dpyinfo->gray
10965 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10966 gray_bitmap_bits,
10967 gray_bitmap_width, gray_bitmap_height,
10968 (unsigned long) 1, (unsigned long) 0, 1);
10971 #ifdef HAVE_X_I18N
10972 xim_initialize (dpyinfo, resource_name);
10973 #endif
10975 #ifdef subprocesses
10976 /* This is only needed for distinguishing keyboard and process input. */
10977 if (connection != 0)
10978 add_keyboard_wait_descriptor (connection);
10979 #endif
10981 #ifndef F_SETOWN_BUG
10982 #ifdef F_SETOWN
10983 #ifdef F_SETOWN_SOCK_NEG
10984 /* stdin is a socket here */
10985 fcntl (connection, F_SETOWN, -getpid ());
10986 #else /* ! defined (F_SETOWN_SOCK_NEG) */
10987 fcntl (connection, F_SETOWN, getpid ());
10988 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
10989 #endif /* ! defined (F_SETOWN) */
10990 #endif /* F_SETOWN_BUG */
10992 #ifdef SIGIO
10993 if (interrupt_input)
10994 init_sigio (connection);
10995 #endif /* ! defined (SIGIO) */
10997 #ifdef USE_LUCID
10999 Display *dpy = dpyinfo->display;
11000 XrmValue d, fr, to;
11001 Font font;
11003 d.addr = (XPointer)&dpy;
11004 d.size = sizeof (Display *);
11005 fr.addr = XtDefaultFont;
11006 fr.size = sizeof (XtDefaultFont);
11007 to.size = sizeof (Font *);
11008 to.addr = (XPointer)&font;
11009 x_catch_errors (dpy);
11010 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
11011 abort ();
11012 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
11013 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
11014 x_uncatch_errors ();
11016 #endif
11018 /* See if we should run in synchronous mode. This is useful
11019 for debugging X code. */
11021 Lisp_Object value;
11022 value = display_x_get_resource (dpyinfo,
11023 build_string ("synchronous"),
11024 build_string ("Synchronous"),
11025 Qnil, Qnil);
11026 if (STRINGP (value)
11027 && (!strcmp (SDATA (value), "true")
11028 || !strcmp (SDATA (value), "on")))
11029 XSynchronize (dpyinfo->display, True);
11033 Lisp_Object value;
11034 value = display_x_get_resource (dpyinfo,
11035 build_string ("useXIM"),
11036 build_string ("UseXIM"),
11037 Qnil, Qnil);
11038 #ifdef USE_XIM
11039 if (STRINGP (value)
11040 && (!strcmp (SDATA (value), "false")
11041 || !strcmp (SDATA (value), "off")))
11042 use_xim = 0;
11043 #else
11044 if (STRINGP (value)
11045 && (!strcmp (SDATA (value), "true")
11046 || !strcmp (SDATA (value), "on")))
11047 use_xim = 1;
11048 #endif
11051 #ifdef HAVE_X_SM
11052 /* Only do this for the very first display in the Emacs session.
11053 Ignore X session management when Emacs was first started on a
11054 tty. */
11055 if (terminal->id == 1)
11056 x_session_initialize (dpyinfo);
11057 #endif
11059 UNBLOCK_INPUT;
11061 return dpyinfo;
11064 /* Get rid of display DPYINFO, deleting all frames on it,
11065 and without sending any more commands to the X server. */
11067 void
11068 x_delete_display (dpyinfo)
11069 struct x_display_info *dpyinfo;
11071 int i;
11072 struct terminal *t;
11074 /* Close all frames and delete the generic struct terminal for this
11075 X display. */
11076 for (t = terminal_list; t; t = t->next_terminal)
11077 if (t->type == output_x_window && t->display_info.x == dpyinfo)
11079 #ifdef HAVE_X_SM
11080 /* Close X session management when we close its display. */
11081 if (t->id == 1 && x_session_have_connection ())
11082 x_session_close();
11083 #endif
11084 delete_terminal (t);
11085 break;
11088 delete_keyboard_wait_descriptor (dpyinfo->connection);
11090 /* Discard this display from x_display_name_list and x_display_list.
11091 We can't use Fdelq because that can quit. */
11092 if (! NILP (x_display_name_list)
11093 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
11094 x_display_name_list = XCDR (x_display_name_list);
11095 else
11097 Lisp_Object tail;
11099 tail = x_display_name_list;
11100 while (CONSP (tail) && CONSP (XCDR (tail)))
11102 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
11104 XSETCDR (tail, XCDR (XCDR (tail)));
11105 break;
11107 tail = XCDR (tail);
11111 if (next_noop_dpyinfo == dpyinfo)
11112 next_noop_dpyinfo = dpyinfo->next;
11114 if (x_display_list == dpyinfo)
11115 x_display_list = dpyinfo->next;
11116 else
11118 struct x_display_info *tail;
11120 for (tail = x_display_list; tail; tail = tail->next)
11121 if (tail->next == dpyinfo)
11122 tail->next = tail->next->next;
11125 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
11126 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
11127 XrmDestroyDatabase (dpyinfo->xrdb);
11128 #endif
11129 #endif
11130 #ifdef HAVE_X_I18N
11131 if (dpyinfo->xim)
11132 xim_close_dpy (dpyinfo);
11133 #endif
11135 /* Free the font names in the font table. */
11136 for (i = 0; i < dpyinfo->n_fonts; i++)
11137 if (dpyinfo->font_table[i].name)
11139 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
11140 xfree (dpyinfo->font_table[i].full_name);
11141 xfree (dpyinfo->font_table[i].name);
11144 if (dpyinfo->font_table)
11146 if (dpyinfo->font_table->font_encoder)
11147 xfree (dpyinfo->font_table->font_encoder);
11148 xfree (dpyinfo->font_table);
11150 if (dpyinfo->x_id_name)
11151 xfree (dpyinfo->x_id_name);
11152 if (dpyinfo->color_cells)
11153 xfree (dpyinfo->color_cells);
11154 xfree (dpyinfo);
11157 #ifdef USE_X_TOOLKIT
11159 /* Atimer callback function for TIMER. Called every 0.1s to process
11160 Xt timeouts, if needed. We must avoid calling XtAppPending as
11161 much as possible because that function does an implicit XFlush
11162 that slows us down. */
11164 static void
11165 x_process_timeouts (timer)
11166 struct atimer *timer;
11168 BLOCK_INPUT;
11169 x_timeout_atimer_activated_flag = 0;
11170 if (toolkit_scroll_bar_interaction || popup_activated ())
11172 while (XtAppPending (Xt_app_con) & XtIMTimer)
11173 XtAppProcessEvent (Xt_app_con, XtIMTimer);
11174 /* Reactivate the atimer for next time. */
11175 x_activate_timeout_atimer ();
11177 UNBLOCK_INPUT;
11180 /* Install an asynchronous timer that processes Xt timeout events
11181 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
11182 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
11183 function whenever these variables are set. This is necessary
11184 because some widget sets use timeouts internally, for example the
11185 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
11186 processed, these widgets don't behave normally. */
11188 void
11189 x_activate_timeout_atimer ()
11191 BLOCK_INPUT;
11192 if (!x_timeout_atimer_activated_flag)
11194 EMACS_TIME interval;
11196 EMACS_SET_SECS_USECS (interval, 0, 100000);
11197 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
11198 x_timeout_atimer_activated_flag = 1;
11200 UNBLOCK_INPUT;
11203 #endif /* USE_X_TOOLKIT */
11206 /* Set up use of X before we make the first connection. */
11208 extern frame_parm_handler x_frame_parm_handlers[];
11210 static struct redisplay_interface x_redisplay_interface =
11212 x_frame_parm_handlers,
11213 x_produce_glyphs,
11214 x_write_glyphs,
11215 x_insert_glyphs,
11216 x_clear_end_of_line,
11217 x_scroll_run,
11218 x_after_update_window_line,
11219 x_update_window_begin,
11220 x_update_window_end,
11221 x_cursor_to,
11222 x_flush,
11223 #ifdef XFlush
11224 x_flush,
11225 #else
11226 0, /* flush_display_optional */
11227 #endif
11228 x_clear_window_mouse_face,
11229 x_get_glyph_overhangs,
11230 x_fix_overlapping_area,
11231 x_draw_fringe_bitmap,
11232 0, /* define_fringe_bitmap */
11233 0, /* destroy_fringe_bitmap */
11234 x_per_char_metric,
11235 x_encode_char,
11236 x_compute_glyph_string_overhangs,
11237 x_draw_glyph_string,
11238 x_define_frame_cursor,
11239 x_clear_frame_area,
11240 x_draw_window_cursor,
11241 x_draw_vertical_window_border,
11242 x_shift_glyphs_for_insert
11246 /* This function is called when the last frame on a display is deleted. */
11247 void
11248 x_delete_terminal (struct terminal *terminal)
11250 struct x_display_info *dpyinfo = terminal->display_info.x;
11251 int i;
11253 /* Protect against recursive calls. Fdelete_frame in
11254 delete_terminal calls us back when it deletes our last frame. */
11255 if (!terminal->name)
11256 return;
11258 BLOCK_INPUT;
11259 /* Free the fonts in the font table. */
11260 for (i = 0; i < dpyinfo->n_fonts; i++)
11261 if (dpyinfo->font_table[i].name)
11263 XFreeFont (dpyinfo->display, dpyinfo->font_table[i].font);
11266 x_destroy_all_bitmaps (dpyinfo);
11267 XSetCloseDownMode (dpyinfo->display, DestroyAll);
11269 #ifdef USE_GTK
11270 xg_display_close (dpyinfo->display);
11271 #else
11272 #ifdef USE_X_TOOLKIT
11273 XtCloseDisplay (dpyinfo->display);
11274 #else
11275 XCloseDisplay (dpyinfo->display);
11276 #endif
11277 #endif /* ! USE_GTK */
11279 x_delete_display (dpyinfo);
11280 UNBLOCK_INPUT;
11283 /* Create a struct terminal, initialize it with the X11 specific
11284 functions and make DISPLAY->TERMINAL point to it. */
11286 static struct terminal *
11287 x_create_terminal (struct x_display_info *dpyinfo)
11289 struct terminal *terminal;
11291 terminal = create_terminal ();
11293 terminal->type = output_x_window;
11294 terminal->display_info.x = dpyinfo;
11295 dpyinfo->terminal = terminal;
11297 /* kboard is initialized in x_term_init. */
11299 terminal->clear_frame_hook = x_clear_frame;
11300 terminal->ins_del_lines_hook = x_ins_del_lines;
11301 terminal->delete_glyphs_hook = x_delete_glyphs;
11302 terminal->ring_bell_hook = XTring_bell;
11303 terminal->reset_terminal_modes_hook = XTreset_terminal_modes;
11304 terminal->set_terminal_modes_hook = XTset_terminal_modes;
11305 terminal->update_begin_hook = x_update_begin;
11306 terminal->update_end_hook = x_update_end;
11307 terminal->set_terminal_window_hook = XTset_terminal_window;
11308 terminal->read_socket_hook = XTread_socket;
11309 terminal->frame_up_to_date_hook = XTframe_up_to_date;
11310 terminal->mouse_position_hook = XTmouse_position;
11311 terminal->frame_rehighlight_hook = XTframe_rehighlight;
11312 terminal->frame_raise_lower_hook = XTframe_raise_lower;
11313 terminal->fullscreen_hook = XTfullscreen_hook;
11314 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
11315 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
11316 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
11317 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
11319 terminal->delete_frame_hook = x_destroy_window;
11320 terminal->delete_terminal_hook = x_delete_terminal;
11322 terminal->rif = &x_redisplay_interface;
11323 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
11324 terminal->char_ins_del_ok = 1;
11325 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
11326 terminal->fast_clear_end_of_line = 1; /* X does this well. */
11327 terminal->memory_below_frame = 0; /* We don't remember what scrolls
11328 off the bottom. */
11330 return terminal;
11333 void
11334 x_initialize ()
11336 baud_rate = 19200;
11338 x_noop_count = 0;
11339 last_tool_bar_item = -1;
11340 any_help_event_p = 0;
11341 ignore_next_mouse_click_timeout = 0;
11342 #ifdef HAVE_X_SM
11343 x_session_initialized = 0;
11344 #endif
11346 #ifdef USE_GTK
11347 current_count = -1;
11348 #endif
11350 /* Try to use interrupt input; if we can't, then start polling. */
11351 Fset_input_interrupt_mode (Qt);
11353 #ifdef USE_X_TOOLKIT
11354 XtToolkitInitialize ();
11356 Xt_app_con = XtCreateApplicationContext ();
11358 /* Register a converter from strings to pixels, which uses
11359 Emacs' color allocation infrastructure. */
11360 XtAppSetTypeConverter (Xt_app_con,
11361 XtRString, XtRPixel, cvt_string_to_pixel,
11362 cvt_string_to_pixel_args,
11363 XtNumber (cvt_string_to_pixel_args),
11364 XtCacheByDisplay, cvt_pixel_dtor);
11366 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
11367 #endif
11369 #ifdef USE_TOOLKIT_SCROLL_BARS
11370 #ifndef USE_GTK
11371 xaw3d_arrow_scroll = False;
11372 xaw3d_pick_top = True;
11373 #endif
11374 #endif
11376 /* Note that there is no real way portable across R3/R4 to get the
11377 original error handler. */
11378 XSetErrorHandler (x_error_handler);
11379 XSetIOErrorHandler (x_io_error_quitter);
11381 /* Disable Window Change signals; they are handled by X events. */
11382 #if 0 /* Don't. We may want to open tty frames later. */
11383 #ifdef SIGWINCH
11384 signal (SIGWINCH, SIG_DFL);
11385 #endif /* SIGWINCH */
11386 #endif
11388 signal (SIGPIPE, x_connection_signal);
11392 void
11393 syms_of_xterm ()
11395 x_error_message = NULL;
11397 staticpro (&x_display_name_list);
11398 x_display_name_list = Qnil;
11400 staticpro (&last_mouse_scroll_bar);
11401 last_mouse_scroll_bar = Qnil;
11403 staticpro (&Qvendor_specific_keysyms);
11404 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
11406 staticpro (&Qutf_8);
11407 Qutf_8 = intern ("utf-8");
11408 staticpro (&Qlatin_1);
11409 Qlatin_1 = intern ("latin-1");
11411 staticpro (&last_mouse_press_frame);
11412 last_mouse_press_frame = Qnil;
11414 DEFVAR_BOOL ("x-use-underline-position-properties",
11415 &x_use_underline_position_properties,
11416 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
11417 A value of nil means ignore them. If you encounter fonts with bogus
11418 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
11419 to 4.1, set this to nil. */);
11420 x_use_underline_position_properties = 1;
11422 DEFVAR_BOOL ("x-underline-at-descent-line",
11423 &x_underline_at_descent_line,
11424 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
11425 A value of nil means to draw the underline according to the value of the
11426 variable `x-use-underline-position-properties', which is usually at the
11427 baseline level. The default value is nil. */);
11428 x_underline_at_descent_line = 0;
11430 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
11431 &x_mouse_click_focus_ignore_position,
11432 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
11433 This variable is only used when the window manager requires that you
11434 click on a frame to select it (give it focus). In that case, a value
11435 of nil, means that the selected window and cursor position changes to
11436 reflect the mouse click position, while a non-nil value means that the
11437 selected window or cursor position is preserved. */);
11438 x_mouse_click_focus_ignore_position = 0;
11440 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
11441 doc: /* What X toolkit scroll bars Emacs uses.
11442 A value of nil means Emacs doesn't use X toolkit scroll bars.
11443 Otherwise, value is a symbol describing the X toolkit. */);
11444 #ifdef USE_TOOLKIT_SCROLL_BARS
11445 #ifdef USE_MOTIF
11446 Vx_toolkit_scroll_bars = intern ("motif");
11447 #elif defined HAVE_XAW3D
11448 Vx_toolkit_scroll_bars = intern ("xaw3d");
11449 #elif USE_GTK
11450 Vx_toolkit_scroll_bars = intern ("gtk");
11451 #else
11452 Vx_toolkit_scroll_bars = intern ("xaw");
11453 #endif
11454 #else
11455 Vx_toolkit_scroll_bars = Qnil;
11456 #endif
11458 staticpro (&last_mouse_motion_frame);
11459 last_mouse_motion_frame = Qnil;
11461 Qmodifier_value = intern ("modifier-value");
11462 Qalt = intern ("alt");
11463 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
11464 Qhyper = intern ("hyper");
11465 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
11466 Qmeta = intern ("meta");
11467 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
11468 Qsuper = intern ("super");
11469 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
11471 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
11472 doc: /* Which keys Emacs uses for the alt modifier.
11473 This should be one of the symbols `alt', `hyper', `meta', `super'.
11474 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
11475 is nil, which is the same as `alt'. */);
11476 Vx_alt_keysym = Qnil;
11478 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
11479 doc: /* Which keys Emacs uses for the hyper modifier.
11480 This should be one of the symbols `alt', `hyper', `meta', `super'.
11481 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
11482 default is nil, which is the same as `hyper'. */);
11483 Vx_hyper_keysym = Qnil;
11485 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
11486 doc: /* Which keys Emacs uses for the meta modifier.
11487 This should be one of the symbols `alt', `hyper', `meta', `super'.
11488 For example, `meta' means use the Meta_L and Meta_R keysyms. The
11489 default is nil, which is the same as `meta'. */);
11490 Vx_meta_keysym = Qnil;
11492 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
11493 doc: /* Which keys Emacs uses for the super modifier.
11494 This should be one of the symbols `alt', `hyper', `meta', `super'.
11495 For example, `super' means use the Super_L and Super_R keysyms. The
11496 default is nil, which is the same as `super'. */);
11497 Vx_super_keysym = Qnil;
11499 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
11500 doc: /* Hash table of character codes indexed by X keysym codes. */);
11501 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
11502 make_float (DEFAULT_REHASH_SIZE),
11503 make_float (DEFAULT_REHASH_THRESHOLD),
11504 Qnil, Qnil, Qnil);
11507 #endif /* HAVE_X_WINDOWS */
11509 /* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
11510 (do not change this comment) */